1 /*****************************************************************************/
4 * es1370.c -- Ensoniq ES1370/Asahi Kasei AK4531 audio driver.
6 * Copyright (C) 1998-2001, 2003 Thomas Sailer (t.sailer@alumni.ethz.ch)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Special thanks to David C. Niemi
25 * Module command line parameters:
26 * lineout if 1 the LINE jack is used as an output instead of an input.
27 * LINE then contains the unmixed dsp output. This can be used
28 * to make the card a four channel one: use dsp to output two
29 * channels to LINE and dac to output the other two channels to
30 * SPKR. Set the mixer to only output synth to SPKR.
31 * micbias sets the +5V bias to the mic if using an electretmic.
34 * Note: sync mode is not yet supported (i.e. running dsp and dac from the same
38 * /dev/dsp standard /dev/dsp device, (mostly) OSS compatible
39 * /dev/mixer standard /dev/mixer device, (mostly) OSS compatible
40 * /dev/dsp1 additional DAC, like /dev/dsp, but output only,
41 * only 5512, 11025, 22050 and 44100 samples/s,
42 * outputs to mixer "SYNTH" setting
43 * /dev/midi simple MIDI UART interface, no ioctl
45 * NOTE: the card does not have any FM/Wavetable synthesizer, it is supposed
46 * to be done in software. That is what /dev/dac is for. By now (Q2 1998)
47 * there are several MIDI to PCM (WAV) packages, one of them is timidity.
50 * 26.03.1998 0.1 Initial release
51 * 31.03.1998 0.2 Fix bug in GETOSPACE
52 * 04.04.1998 0.3 Make it work (again) under 2.0.33
53 * Fix mixer write operation not returning the actual
55 * 05.04.1998 0.4 First attempt at using the new PCI stuff
56 * 29.04.1998 0.5 Fix hang when ^C is pressed on amp
57 * 07.05.1998 0.6 Don't double lock around stop_*() in *_release()
58 * 10.05.1998 0.7 First stab at a simple midi interface (no bells&whistles)
59 * 14.05.1998 0.8 Don't allow excessive interrupt rates
60 * 08.06.1998 0.9 First release using Alan Cox' soundcore instead of
62 * 05.07.1998 0.10 Fixed the driver to correctly maintin OSS style volume
63 * settings (not sure if this should be standard)
64 * Fixed many references: f_flags should be f_mode
65 * -- Gerald Britton <gbritton@mit.edu>
66 * 03.08.1998 0.11 Now mixer behaviour can basically be selected between
67 * "OSS documented" and "OSS actual" behaviour
68 * Fixed mixer table thanks to Hakan.Lennestal@lu.erisoft.se
69 * On module startup, set DAC2 to 11kSPS instead of 5.5kSPS,
70 * as it produces an annoying ssssh in the lower sampling rate
71 * Do not include modversions.h
72 * 22.08.1998 0.12 Mixer registers actually have 5 instead of 4 bits
73 * pointed out by Itai Nahshon
74 * 31.08.1998 0.13 Fix realplayer problems - dac.count issues
75 * 08.10.1998 0.14 Joystick support fixed
76 * -- Oliver Neukum <c188@org.chemie.uni-muenchen.de>
77 * 10.12.1998 0.15 Fix drain_dac trying to wait on not yet initialized DMA
78 * 16.12.1998 0.16 Don't wake up app until there are fragsize bytes to read/write
79 * 06.01.1999 0.17 remove the silly SA_INTERRUPT flag.
80 * hopefully killed the egcs section type conflict
81 * 12.03.1999 0.18 cinfo.blocks should be reset after GETxPTR ioctl.
82 * reported by Johan Maes <joma@telindus.be>
83 * 22.03.1999 0.19 return EAGAIN instead of EBUSY when O_NONBLOCK
84 * read/write cannot be executed
85 * 07.04.1999 0.20 implemented the following ioctl's: SOUND_PCM_READ_RATE,
86 * SOUND_PCM_READ_CHANNELS, SOUND_PCM_READ_BITS;
87 * Alpha fixes reported by Peter Jones <pjones@redhat.com>
88 * Note: joystick address handling might still be wrong on archs
90 * 10.05.1999 0.21 Added support for an electret mic for SB PCI64
91 * to the Linux kernel sound driver. This mod also straighten
92 * out the question marks around the mic impedance setting
93 * (micz). From Kim.Berts@fisub.mail.abb.com
94 * 11.05.1999 0.22 Implemented the IMIX call to mute recording monitor.
95 * Guenter Geiger <geiger@epy.co.at>
96 * 15.06.1999 0.23 Fix bad allocation bug.
97 * Thanks to Deti Fliegl <fliegl@in.tum.de>
98 * 28.06.1999 0.24 Add pci_set_master
99 * 02.08.1999 0.25 Added workaround for the "phantom write" bug first
100 * documented by Dave Sharpless from Anchor Games
101 * 03.08.1999 0.26 adapt to Linus' new __setup/__initcall
102 * added kernel command line option "es1370=joystick[,lineout[,micbias]]"
103 * removed CONFIG_SOUND_ES1370_JOYPORT_BOOT kludge
104 * 12.08.1999 0.27 module_init/__setup fixes
105 * 19.08.1999 0.28 SOUND_MIXER_IMIX fixes, reported by Gianluca <gialluca@mail.tiscalinet.it>
106 * 31.08.1999 0.29 add spin_lock_init
107 * replaced current->state = x with set_current_state(x)
108 * 03.09.1999 0.30 change read semantics for MIDI to match
109 * OSS more closely; remove possible wakeup race
110 * 28.10.1999 0.31 More waitqueue races fixed
111 * 08.01.2000 0.32 Prevent some ioctl's from returning bad count values on underrun/overrun;
112 * Tim Janik's BSE (Bedevilled Sound Engine) found this
113 * 07.02.2000 0.33 Use pci_alloc_consistent and pci_register_driver
114 * 21.11.2000 0.34 Initialize dma buffers in poll, otherwise poll may return a bogus mask
115 * 12.12.2000 0.35 More dma buffer initializations, patch from
116 * Tjeerd Mulder <tjeerd.mulder@fujitsu-siemens.com>
117 * 07.01.2001 0.36 Timeout change in wrcodec as requested by Frank Klemm <pfk@fuchs.offl.uni-jena.de>
118 * 31.01.2001 0.37 Register/Unregister gameport
119 * Fix SETTRIGGER non OSS API conformity
120 * 03.01.2003 0.38 open_mode fixes from Georg Acher <acher@in.tum.de>
122 * some important things missing in Ensoniq documentation:
124 * Experimental PCLKDIV results: play the same waveforms on both DAC1 and DAC2
125 * and vary PCLKDIV to obtain zero beat.
128 * seems to be fs = 1411200/(PCLKDIV+2)
130 * should find out when curr_sample_ct is cleared and
131 * where exactly the CCB fetches data
133 * The card uses a 22.5792 MHz crystal.
134 * The LINEIN jack may be converted to an AOUT jack by
135 * setting pin 47 (XCTL0) of the ES1370 to high.
136 * Pin 48 (XCTL1) of the ES1370 sets the +5V bias for an electretmic
141 /*****************************************************************************/
143 #include <linux/interrupt.h>
144 #include <linux/module.h>
145 #include <linux/string.h>
146 #include <linux/ioport.h>
147 #include <linux/sched.h>
148 #include <linux/delay.h>
149 #include <linux/sound.h>
150 #include <linux/slab.h>
151 #include <linux/soundcard.h>
152 #include <linux/pci.h>
153 #include <linux/smp_lock.h>
154 #include <linux/init.h>
155 #include <linux/poll.h>
156 #include <linux/spinlock.h>
157 #include <linux/gameport.h>
158 #include <linux/wait.h>
159 #include <linux/dma-mapping.h>
162 #include <asm/page.h>
163 #include <asm/uaccess.h>
165 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
166 #define SUPPORT_JOYSTICK
169 /* --------------------------------------------------------------------- */
171 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
173 /*#define DBG(x) {x}*/
175 /* --------------------------------------------------------------------- */
177 #ifndef PCI_VENDOR_ID_ENSONIQ
178 #define PCI_VENDOR_ID_ENSONIQ 0x1274
181 #ifndef PCI_DEVICE_ID_ENSONIQ_ES1370
182 #define PCI_DEVICE_ID_ENSONIQ_ES1370 0x5000
185 #define ES1370_MAGIC ((PCI_VENDOR_ID_ENSONIQ<<16)|PCI_DEVICE_ID_ENSONIQ_ES1370)
187 #define ES1370_EXTENT 0x40
190 #define ES1370_REG_CONTROL 0x00
191 #define ES1370_REG_STATUS 0x04
192 #define ES1370_REG_UART_DATA 0x08
193 #define ES1370_REG_UART_STATUS 0x09
194 #define ES1370_REG_UART_CONTROL 0x09
195 #define ES1370_REG_UART_TEST 0x0a
196 #define ES1370_REG_MEMPAGE 0x0c
197 #define ES1370_REG_CODEC 0x10
198 #define ES1370_REG_SERIAL_CONTROL 0x20
199 #define ES1370_REG_DAC1_SCOUNT 0x24
200 #define ES1370_REG_DAC2_SCOUNT 0x28
201 #define ES1370_REG_ADC_SCOUNT 0x2c
203 #define ES1370_REG_DAC1_FRAMEADR 0xc30
204 #define ES1370_REG_DAC1_FRAMECNT 0xc34
205 #define ES1370_REG_DAC2_FRAMEADR 0xc38
206 #define ES1370_REG_DAC2_FRAMECNT 0xc3c
207 #define ES1370_REG_ADC_FRAMEADR 0xd30
208 #define ES1370_REG_ADC_FRAMECNT 0xd34
209 #define ES1370_REG_PHANTOM_FRAMEADR 0xd38
210 #define ES1370_REG_PHANTOM_FRAMECNT 0xd3c
212 #define ES1370_FMT_U8_MONO 0
213 #define ES1370_FMT_U8_STEREO 1
214 #define ES1370_FMT_S16_MONO 2
215 #define ES1370_FMT_S16_STEREO 3
216 #define ES1370_FMT_STEREO 1
217 #define ES1370_FMT_S16 2
218 #define ES1370_FMT_MASK 3
220 static const unsigned sample_size
[] = { 1, 2, 2, 4 };
221 static const unsigned sample_shift
[] = { 0, 1, 1, 2 };
223 static const unsigned dac1_samplerate
[] = { 5512, 11025, 22050, 44100 };
225 #define DAC2_SRTODIV(x) (((1411200+(x)/2)/(x))-2)
226 #define DAC2_DIVTOSR(x) (1411200/((x)+2))
228 #define CTRL_ADC_STOP 0x80000000 /* 1 = ADC stopped */
229 #define CTRL_XCTL1 0x40000000 /* electret mic bias */
230 #define CTRL_OPEN 0x20000000 /* no function, can be read and written */
231 #define CTRL_PCLKDIV 0x1fff0000 /* ADC/DAC2 clock divider */
232 #define CTRL_SH_PCLKDIV 16
233 #define CTRL_MSFMTSEL 0x00008000 /* MPEG serial data fmt: 0 = Sony, 1 = I2S */
234 #define CTRL_M_SBB 0x00004000 /* DAC2 clock: 0 = PCLKDIV, 1 = MPEG */
235 #define CTRL_WTSRSEL 0x00003000 /* DAC1 clock freq: 0=5512, 1=11025, 2=22050, 3=44100 */
236 #define CTRL_SH_WTSRSEL 12
237 #define CTRL_DAC_SYNC 0x00000800 /* 1 = DAC2 runs off DAC1 clock */
238 #define CTRL_CCB_INTRM 0x00000400 /* 1 = CCB "voice" ints enabled */
239 #define CTRL_M_CB 0x00000200 /* recording source: 0 = ADC, 1 = MPEG */
240 #define CTRL_XCTL0 0x00000100 /* 0 = Line in, 1 = Line out */
241 #define CTRL_BREQ 0x00000080 /* 1 = test mode (internal mem test) */
242 #define CTRL_DAC1_EN 0x00000040 /* enable DAC1 */
243 #define CTRL_DAC2_EN 0x00000020 /* enable DAC2 */
244 #define CTRL_ADC_EN 0x00000010 /* enable ADC */
245 #define CTRL_UART_EN 0x00000008 /* enable MIDI uart */
246 #define CTRL_JYSTK_EN 0x00000004 /* enable Joystick port (presumably at address 0x200) */
247 #define CTRL_CDC_EN 0x00000002 /* enable serial (CODEC) interface */
248 #define CTRL_SERR_DIS 0x00000001 /* 1 = disable PCI SERR signal */
250 #define STAT_INTR 0x80000000 /* wired or of all interrupt bits */
251 #define STAT_CSTAT 0x00000400 /* 1 = codec busy or codec write in progress */
252 #define STAT_CBUSY 0x00000200 /* 1 = codec busy */
253 #define STAT_CWRIP 0x00000100 /* 1 = codec write in progress */
254 #define STAT_VC 0x00000060 /* CCB int source, 0=DAC1, 1=DAC2, 2=ADC, 3=undef */
256 #define STAT_MCCB 0x00000010 /* CCB int pending */
257 #define STAT_UART 0x00000008 /* UART int pending */
258 #define STAT_DAC1 0x00000004 /* DAC1 int pending */
259 #define STAT_DAC2 0x00000002 /* DAC2 int pending */
260 #define STAT_ADC 0x00000001 /* ADC int pending */
262 #define USTAT_RXINT 0x80 /* UART rx int pending */
263 #define USTAT_TXINT 0x04 /* UART tx int pending */
264 #define USTAT_TXRDY 0x02 /* UART tx ready */
265 #define USTAT_RXRDY 0x01 /* UART rx ready */
267 #define UCTRL_RXINTEN 0x80 /* 1 = enable RX ints */
268 #define UCTRL_TXINTEN 0x60 /* TX int enable field mask */
269 #define UCTRL_ENA_TXINT 0x20 /* enable TX int */
270 #define UCTRL_CNTRL 0x03 /* control field */
271 #define UCTRL_CNTRL_SWR 0x03 /* software reset command */
273 #define SCTRL_P2ENDINC 0x00380000 /* */
274 #define SCTRL_SH_P2ENDINC 19
275 #define SCTRL_P2STINC 0x00070000 /* */
276 #define SCTRL_SH_P2STINC 16
277 #define SCTRL_R1LOOPSEL 0x00008000 /* 0 = loop mode */
278 #define SCTRL_P2LOOPSEL 0x00004000 /* 0 = loop mode */
279 #define SCTRL_P1LOOPSEL 0x00002000 /* 0 = loop mode */
280 #define SCTRL_P2PAUSE 0x00001000 /* 1 = pause mode */
281 #define SCTRL_P1PAUSE 0x00000800 /* 1 = pause mode */
282 #define SCTRL_R1INTEN 0x00000400 /* enable interrupt */
283 #define SCTRL_P2INTEN 0x00000200 /* enable interrupt */
284 #define SCTRL_P1INTEN 0x00000100 /* enable interrupt */
285 #define SCTRL_P1SCTRLD 0x00000080 /* reload sample count register for DAC1 */
286 #define SCTRL_P2DACSEN 0x00000040 /* 1 = DAC2 play back last sample when disabled */
287 #define SCTRL_R1SEB 0x00000020 /* 1 = 16bit */
288 #define SCTRL_R1SMB 0x00000010 /* 1 = stereo */
289 #define SCTRL_R1FMT 0x00000030 /* format mask */
290 #define SCTRL_SH_R1FMT 4
291 #define SCTRL_P2SEB 0x00000008 /* 1 = 16bit */
292 #define SCTRL_P2SMB 0x00000004 /* 1 = stereo */
293 #define SCTRL_P2FMT 0x0000000c /* format mask */
294 #define SCTRL_SH_P2FMT 2
295 #define SCTRL_P1SEB 0x00000002 /* 1 = 16bit */
296 #define SCTRL_P1SMB 0x00000001 /* 1 = stereo */
297 #define SCTRL_P1FMT 0x00000003 /* format mask */
298 #define SCTRL_SH_P1FMT 0
302 #define FMODE_DAC 4 /* slight misuse of mode_t */
304 /* MIDI buffer sizes */
306 #define MIDIINBUF 256
307 #define MIDIOUTBUF 256
309 #define FMODE_MIDI_SHIFT 3
310 #define FMODE_MIDI_READ (FMODE_READ << FMODE_MIDI_SHIFT)
311 #define FMODE_MIDI_WRITE (FMODE_WRITE << FMODE_MIDI_SHIFT)
313 /* --------------------------------------------------------------------- */
315 struct es1370_state
{
319 /* list of es1370 devices */
320 struct list_head devs
;
322 /* the corresponding pci_dev structure */
325 /* soundcore stuff */
331 /* hardware resources */
332 unsigned long io
; /* long for SPARC */
335 /* mixer registers; there is no HW readback */
337 unsigned short vol
[10];
340 unsigned short micpreamp
;
349 struct semaphore open_sem
;
351 wait_queue_head_t open_wait
;
359 unsigned hwptr
, swptr
;
360 unsigned total_bytes
;
362 unsigned error
; /* over/underrun */
363 wait_queue_head_t wait
;
364 /* redundant, but makes calculations easier */
367 unsigned fragsamples
;
371 unsigned endcleared
:1;
373 unsigned ossfragshift
;
375 unsigned subdivision
;
376 } dma_dac1
, dma_dac2
, dma_adc
;
378 /* The following buffer is used to point the phantom write channel to. */
379 unsigned char *bugbuf_cpu
;
380 dma_addr_t bugbuf_dma
;
384 unsigned ird
, iwr
, icnt
;
385 unsigned ord
, owr
, ocnt
;
386 wait_queue_head_t iwait
;
387 wait_queue_head_t owait
;
388 unsigned char ibuf
[MIDIINBUF
];
389 unsigned char obuf
[MIDIOUTBUF
];
392 #ifdef SUPPORT_JOYSTICK
393 struct gameport
*gameport
;
396 struct semaphore sem
;
399 /* --------------------------------------------------------------------- */
401 static LIST_HEAD(devs
);
403 /* --------------------------------------------------------------------- */
405 static inline unsigned ld2(unsigned int x
)
430 /* --------------------------------------------------------------------- */
432 static void wrcodec(struct es1370_state
*s
, unsigned char idx
, unsigned char data
)
434 unsigned long tmo
= jiffies
+ HZ
/10, j
;
438 if (!(inl(s
->io
+ES1370_REG_STATUS
) & STAT_CSTAT
)) {
439 outw((((unsigned short)idx
)<<8)|data
, s
->io
+ES1370_REG_CODEC
);
443 } while ((signed)(tmo
-j
) > 0);
444 printk(KERN_ERR
"es1370: write to codec register timeout\n");
447 /* --------------------------------------------------------------------- */
449 static inline void stop_adc(struct es1370_state
*s
)
453 spin_lock_irqsave(&s
->lock
, flags
);
454 s
->ctrl
&= ~CTRL_ADC_EN
;
455 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
456 spin_unlock_irqrestore(&s
->lock
, flags
);
459 static inline void stop_dac1(struct es1370_state
*s
)
463 spin_lock_irqsave(&s
->lock
, flags
);
464 s
->ctrl
&= ~CTRL_DAC1_EN
;
465 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
466 spin_unlock_irqrestore(&s
->lock
, flags
);
469 static inline void stop_dac2(struct es1370_state
*s
)
473 spin_lock_irqsave(&s
->lock
, flags
);
474 s
->ctrl
&= ~CTRL_DAC2_EN
;
475 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
476 spin_unlock_irqrestore(&s
->lock
, flags
);
479 static void start_dac1(struct es1370_state
*s
)
482 unsigned fragremain
, fshift
;
484 spin_lock_irqsave(&s
->lock
, flags
);
485 if (!(s
->ctrl
& CTRL_DAC1_EN
) && (s
->dma_dac1
.mapped
|| s
->dma_dac1
.count
> 0)
486 && s
->dma_dac1
.ready
) {
487 s
->ctrl
|= CTRL_DAC1_EN
;
488 s
->sctrl
= (s
->sctrl
& ~(SCTRL_P1LOOPSEL
| SCTRL_P1PAUSE
| SCTRL_P1SCTRLD
)) | SCTRL_P1INTEN
;
489 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
490 fragremain
= ((- s
->dma_dac1
.hwptr
) & (s
->dma_dac1
.fragsize
-1));
491 fshift
= sample_shift
[(s
->sctrl
& SCTRL_P1FMT
) >> SCTRL_SH_P1FMT
];
492 if (fragremain
< 2*fshift
)
493 fragremain
= s
->dma_dac1
.fragsize
;
494 outl((fragremain
>> fshift
) - 1, s
->io
+ES1370_REG_DAC1_SCOUNT
);
495 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
496 outl((s
->dma_dac1
.fragsize
>> fshift
) - 1, s
->io
+ES1370_REG_DAC1_SCOUNT
);
498 spin_unlock_irqrestore(&s
->lock
, flags
);
501 static void start_dac2(struct es1370_state
*s
)
504 unsigned fragremain
, fshift
;
506 spin_lock_irqsave(&s
->lock
, flags
);
507 if (!(s
->ctrl
& CTRL_DAC2_EN
) && (s
->dma_dac2
.mapped
|| s
->dma_dac2
.count
> 0)
508 && s
->dma_dac2
.ready
) {
509 s
->ctrl
|= CTRL_DAC2_EN
;
510 s
->sctrl
= (s
->sctrl
& ~(SCTRL_P2LOOPSEL
| SCTRL_P2PAUSE
| SCTRL_P2DACSEN
|
511 SCTRL_P2ENDINC
| SCTRL_P2STINC
)) | SCTRL_P2INTEN
|
512 (((s
->sctrl
& SCTRL_P2FMT
) ? 2 : 1) << SCTRL_SH_P2ENDINC
) |
513 (0 << SCTRL_SH_P2STINC
);
514 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
515 fragremain
= ((- s
->dma_dac2
.hwptr
) & (s
->dma_dac2
.fragsize
-1));
516 fshift
= sample_shift
[(s
->sctrl
& SCTRL_P2FMT
) >> SCTRL_SH_P2FMT
];
517 if (fragremain
< 2*fshift
)
518 fragremain
= s
->dma_dac2
.fragsize
;
519 outl((fragremain
>> fshift
) - 1, s
->io
+ES1370_REG_DAC2_SCOUNT
);
520 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
521 outl((s
->dma_dac2
.fragsize
>> fshift
) - 1, s
->io
+ES1370_REG_DAC2_SCOUNT
);
523 spin_unlock_irqrestore(&s
->lock
, flags
);
526 static void start_adc(struct es1370_state
*s
)
529 unsigned fragremain
, fshift
;
531 spin_lock_irqsave(&s
->lock
, flags
);
532 if (!(s
->ctrl
& CTRL_ADC_EN
) && (s
->dma_adc
.mapped
|| s
->dma_adc
.count
< (signed)(s
->dma_adc
.dmasize
- 2*s
->dma_adc
.fragsize
))
533 && s
->dma_adc
.ready
) {
534 s
->ctrl
|= CTRL_ADC_EN
;
535 s
->sctrl
= (s
->sctrl
& ~SCTRL_R1LOOPSEL
) | SCTRL_R1INTEN
;
536 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
537 fragremain
= ((- s
->dma_adc
.hwptr
) & (s
->dma_adc
.fragsize
-1));
538 fshift
= sample_shift
[(s
->sctrl
& SCTRL_R1FMT
) >> SCTRL_SH_R1FMT
];
539 if (fragremain
< 2*fshift
)
540 fragremain
= s
->dma_adc
.fragsize
;
541 outl((fragremain
>> fshift
) - 1, s
->io
+ES1370_REG_ADC_SCOUNT
);
542 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
543 outl((s
->dma_adc
.fragsize
>> fshift
) - 1, s
->io
+ES1370_REG_ADC_SCOUNT
);
545 spin_unlock_irqrestore(&s
->lock
, flags
);
548 /* --------------------------------------------------------------------- */
550 #define DMABUF_DEFAULTORDER (17-PAGE_SHIFT)
551 #define DMABUF_MINORDER 1
553 static inline void dealloc_dmabuf(struct es1370_state
*s
, struct dmabuf
*db
)
555 struct page
*page
, *pend
;
558 /* undo marking the pages as reserved */
559 pend
= virt_to_page(db
->rawbuf
+ (PAGE_SIZE
<< db
->buforder
) - 1);
560 for (page
= virt_to_page(db
->rawbuf
); page
<= pend
; page
++)
561 ClearPageReserved(page
);
562 pci_free_consistent(s
->dev
, PAGE_SIZE
<< db
->buforder
, db
->rawbuf
, db
->dmaaddr
);
565 db
->mapped
= db
->ready
= 0;
568 static int prog_dmabuf(struct es1370_state
*s
, struct dmabuf
*db
, unsigned rate
, unsigned fmt
, unsigned reg
)
573 struct page
*page
, *pend
;
575 db
->hwptr
= db
->swptr
= db
->total_bytes
= db
->count
= db
->error
= db
->endcleared
= 0;
577 db
->ready
= db
->mapped
= 0;
578 for (order
= DMABUF_DEFAULTORDER
; order
>= DMABUF_MINORDER
; order
--)
579 if ((db
->rawbuf
= pci_alloc_consistent(s
->dev
, PAGE_SIZE
<< order
, &db
->dmaaddr
)))
583 db
->buforder
= order
;
584 /* now mark the pages as reserved; otherwise remap_pfn_range doesn't do what we want */
585 pend
= virt_to_page(db
->rawbuf
+ (PAGE_SIZE
<< db
->buforder
) - 1);
586 for (page
= virt_to_page(db
->rawbuf
); page
<= pend
; page
++)
587 SetPageReserved(page
);
589 fmt
&= ES1370_FMT_MASK
;
590 bytepersec
= rate
<< sample_shift
[fmt
];
591 bufs
= PAGE_SIZE
<< db
->buforder
;
592 if (db
->ossfragshift
) {
593 if ((1000 << db
->ossfragshift
) < bytepersec
)
594 db
->fragshift
= ld2(bytepersec
/1000);
596 db
->fragshift
= db
->ossfragshift
;
598 db
->fragshift
= ld2(bytepersec
/100/(db
->subdivision
? db
->subdivision
: 1));
599 if (db
->fragshift
< 3)
602 db
->numfrag
= bufs
>> db
->fragshift
;
603 while (db
->numfrag
< 4 && db
->fragshift
> 3) {
605 db
->numfrag
= bufs
>> db
->fragshift
;
607 db
->fragsize
= 1 << db
->fragshift
;
608 if (db
->ossmaxfrags
>= 4 && db
->ossmaxfrags
< db
->numfrag
)
609 db
->numfrag
= db
->ossmaxfrags
;
610 db
->fragsamples
= db
->fragsize
>> sample_shift
[fmt
];
611 db
->dmasize
= db
->numfrag
<< db
->fragshift
;
612 memset(db
->rawbuf
, (fmt
& ES1370_FMT_S16
) ? 0 : 0x80, db
->dmasize
);
613 outl((reg
>> 8) & 15, s
->io
+ES1370_REG_MEMPAGE
);
614 outl(db
->dmaaddr
, s
->io
+(reg
& 0xff));
615 outl((db
->dmasize
>> 2)-1, s
->io
+((reg
+ 4) & 0xff));
621 static inline int prog_dmabuf_adc(struct es1370_state
*s
)
624 return prog_dmabuf(s
, &s
->dma_adc
, DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
),
625 (s
->sctrl
>> SCTRL_SH_R1FMT
) & ES1370_FMT_MASK
, ES1370_REG_ADC_FRAMEADR
);
628 static inline int prog_dmabuf_dac2(struct es1370_state
*s
)
631 return prog_dmabuf(s
, &s
->dma_dac2
, DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
),
632 (s
->sctrl
>> SCTRL_SH_P2FMT
) & ES1370_FMT_MASK
, ES1370_REG_DAC2_FRAMEADR
);
635 static inline int prog_dmabuf_dac1(struct es1370_state
*s
)
638 return prog_dmabuf(s
, &s
->dma_dac1
, dac1_samplerate
[(s
->ctrl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
],
639 (s
->sctrl
>> SCTRL_SH_P1FMT
) & ES1370_FMT_MASK
, ES1370_REG_DAC1_FRAMEADR
);
642 static inline unsigned get_hwptr(struct es1370_state
*s
, struct dmabuf
*db
, unsigned reg
)
644 unsigned hwptr
, diff
;
646 outl((reg
>> 8) & 15, s
->io
+ES1370_REG_MEMPAGE
);
647 hwptr
= (inl(s
->io
+(reg
& 0xff)) >> 14) & 0x3fffc;
648 diff
= (db
->dmasize
+ hwptr
- db
->hwptr
) % db
->dmasize
;
653 static inline void clear_advance(void *buf
, unsigned bsize
, unsigned bptr
, unsigned len
, unsigned char c
)
655 if (bptr
+ len
> bsize
) {
656 unsigned x
= bsize
- bptr
;
657 memset(((char *)buf
) + bptr
, c
, x
);
661 memset(((char *)buf
) + bptr
, c
, len
);
664 /* call with spinlock held! */
665 static void es1370_update_ptr(struct es1370_state
*s
)
669 /* update ADC pointer */
670 if (s
->ctrl
& CTRL_ADC_EN
) {
671 diff
= get_hwptr(s
, &s
->dma_adc
, ES1370_REG_ADC_FRAMECNT
);
672 s
->dma_adc
.total_bytes
+= diff
;
673 s
->dma_adc
.count
+= diff
;
674 if (s
->dma_adc
.count
>= (signed)s
->dma_adc
.fragsize
)
675 wake_up(&s
->dma_adc
.wait
);
676 if (!s
->dma_adc
.mapped
) {
677 if (s
->dma_adc
.count
> (signed)(s
->dma_adc
.dmasize
- ((3 * s
->dma_adc
.fragsize
) >> 1))) {
678 s
->ctrl
&= ~CTRL_ADC_EN
;
679 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
684 /* update DAC1 pointer */
685 if (s
->ctrl
& CTRL_DAC1_EN
) {
686 diff
= get_hwptr(s
, &s
->dma_dac1
, ES1370_REG_DAC1_FRAMECNT
);
687 s
->dma_dac1
.total_bytes
+= diff
;
688 if (s
->dma_dac1
.mapped
) {
689 s
->dma_dac1
.count
+= diff
;
690 if (s
->dma_dac1
.count
>= (signed)s
->dma_dac1
.fragsize
)
691 wake_up(&s
->dma_dac1
.wait
);
693 s
->dma_dac1
.count
-= diff
;
694 if (s
->dma_dac1
.count
<= 0) {
695 s
->ctrl
&= ~CTRL_DAC1_EN
;
696 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
698 } else if (s
->dma_dac1
.count
<= (signed)s
->dma_dac1
.fragsize
&& !s
->dma_dac1
.endcleared
) {
699 clear_advance(s
->dma_dac1
.rawbuf
, s
->dma_dac1
.dmasize
, s
->dma_dac1
.swptr
,
700 s
->dma_dac1
.fragsize
, (s
->sctrl
& SCTRL_P1SEB
) ? 0 : 0x80);
701 s
->dma_dac1
.endcleared
= 1;
703 if (s
->dma_dac1
.count
+ (signed)s
->dma_dac1
.fragsize
<= (signed)s
->dma_dac1
.dmasize
)
704 wake_up(&s
->dma_dac1
.wait
);
707 /* update DAC2 pointer */
708 if (s
->ctrl
& CTRL_DAC2_EN
) {
709 diff
= get_hwptr(s
, &s
->dma_dac2
, ES1370_REG_DAC2_FRAMECNT
);
710 s
->dma_dac2
.total_bytes
+= diff
;
711 if (s
->dma_dac2
.mapped
) {
712 s
->dma_dac2
.count
+= diff
;
713 if (s
->dma_dac2
.count
>= (signed)s
->dma_dac2
.fragsize
)
714 wake_up(&s
->dma_dac2
.wait
);
716 s
->dma_dac2
.count
-= diff
;
717 if (s
->dma_dac2
.count
<= 0) {
718 s
->ctrl
&= ~CTRL_DAC2_EN
;
719 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
721 } else if (s
->dma_dac2
.count
<= (signed)s
->dma_dac2
.fragsize
&& !s
->dma_dac2
.endcleared
) {
722 clear_advance(s
->dma_dac2
.rawbuf
, s
->dma_dac2
.dmasize
, s
->dma_dac2
.swptr
,
723 s
->dma_dac2
.fragsize
, (s
->sctrl
& SCTRL_P2SEB
) ? 0 : 0x80);
724 s
->dma_dac2
.endcleared
= 1;
726 if (s
->dma_dac2
.count
+ (signed)s
->dma_dac2
.fragsize
<= (signed)s
->dma_dac2
.dmasize
)
727 wake_up(&s
->dma_dac2
.wait
);
732 /* hold spinlock for the following! */
733 static void es1370_handle_midi(struct es1370_state
*s
)
738 if (!(s
->ctrl
& CTRL_UART_EN
))
741 while (inb(s
->io
+ES1370_REG_UART_STATUS
) & USTAT_RXRDY
) {
742 ch
= inb(s
->io
+ES1370_REG_UART_DATA
);
743 if (s
->midi
.icnt
< MIDIINBUF
) {
744 s
->midi
.ibuf
[s
->midi
.iwr
] = ch
;
745 s
->midi
.iwr
= (s
->midi
.iwr
+ 1) % MIDIINBUF
;
751 wake_up(&s
->midi
.iwait
);
753 while ((inb(s
->io
+ES1370_REG_UART_STATUS
) & USTAT_TXRDY
) && s
->midi
.ocnt
> 0) {
754 outb(s
->midi
.obuf
[s
->midi
.ord
], s
->io
+ES1370_REG_UART_DATA
);
755 s
->midi
.ord
= (s
->midi
.ord
+ 1) % MIDIOUTBUF
;
757 if (s
->midi
.ocnt
< MIDIOUTBUF
-16)
761 wake_up(&s
->midi
.owait
);
762 outb((s
->midi
.ocnt
> 0) ? UCTRL_RXINTEN
| UCTRL_ENA_TXINT
: UCTRL_RXINTEN
, s
->io
+ES1370_REG_UART_CONTROL
);
765 static irqreturn_t
es1370_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
767 struct es1370_state
*s
= (struct es1370_state
*)dev_id
;
768 unsigned int intsrc
, sctl
;
770 /* fastpath out, to ease interrupt sharing */
771 intsrc
= inl(s
->io
+ES1370_REG_STATUS
);
772 if (!(intsrc
& 0x80000000))
775 /* clear audio interrupts first */
777 if (intsrc
& STAT_ADC
)
778 sctl
&= ~SCTRL_R1INTEN
;
779 if (intsrc
& STAT_DAC1
)
780 sctl
&= ~SCTRL_P1INTEN
;
781 if (intsrc
& STAT_DAC2
)
782 sctl
&= ~SCTRL_P2INTEN
;
783 outl(sctl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
784 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
785 es1370_update_ptr(s
);
786 es1370_handle_midi(s
);
787 spin_unlock(&s
->lock
);
791 /* --------------------------------------------------------------------- */
793 static const char invalid_magic
[] = KERN_CRIT
"es1370: invalid magic value\n";
795 #define VALIDATE_STATE(s) \
797 if (!(s) || (s)->magic != ES1370_MAGIC) { \
798 printk(invalid_magic); \
803 /* --------------------------------------------------------------------- */
805 static const struct {
812 } mixtable
[SOUND_MIXER_NRDEVICES
] = {
813 [SOUND_MIXER_VOLUME
] = { 0, 0x0, 0x1, 1, 0x0000, 1 }, /* master */
814 [SOUND_MIXER_PCM
] = { 1, 0x2, 0x3, 1, 0x0400, 1 }, /* voice */
815 [SOUND_MIXER_SYNTH
] = { 2, 0x4, 0x5, 1, 0x0060, 1 }, /* FM */
816 [SOUND_MIXER_CD
] = { 3, 0x6, 0x7, 1, 0x0006, 1 }, /* CD */
817 [SOUND_MIXER_LINE
] = { 4, 0x8, 0x9, 1, 0x0018, 1 }, /* Line */
818 [SOUND_MIXER_LINE1
] = { 5, 0xa, 0xb, 1, 0x1800, 1 }, /* AUX */
819 [SOUND_MIXER_LINE2
] = { 6, 0xc, 0x0, 0, 0x0100, 1 }, /* Mono1 */
820 [SOUND_MIXER_LINE3
] = { 7, 0xd, 0x0, 0, 0x0200, 1 }, /* Mono2 */
821 [SOUND_MIXER_MIC
] = { 8, 0xe, 0x0, 0, 0x0001, 1 }, /* Mic */
822 [SOUND_MIXER_OGAIN
] = { 9, 0xf, 0x0, 0, 0x0000, 1 } /* mono out */
825 static void set_recsrc(struct es1370_state
*s
, unsigned int val
)
829 for (j
= i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
830 if (!(val
& (1 << i
)))
832 if (!mixtable
[i
].recmask
) {
836 j
|= mixtable
[i
].recmask
;
839 wrcodec(s
, 0x12, j
& 0xd5);
840 wrcodec(s
, 0x13, j
& 0xaa);
841 wrcodec(s
, 0x14, (j
>> 8) & 0x17);
842 wrcodec(s
, 0x15, (j
>> 8) & 0x0f);
843 i
= (j
& 0x37f) | ((j
<< 1) & 0x3000) | 0xc60;
845 i
&= 0xff60; /* mute record and line monitor */
848 wrcodec(s
, 0x11, i
>> 8);
851 static int mixer_ioctl(struct es1370_state
*s
, unsigned int cmd
, unsigned long arg
)
855 unsigned char l
, r
, rl
, rr
;
856 int __user
*p
= (int __user
*)arg
;
859 if (cmd
== SOUND_MIXER_PRIVATE1
) {
860 /* enable/disable/query mixer preamp */
861 if (get_user(val
, p
))
864 s
->mix
.micpreamp
= !!val
;
865 wrcodec(s
, 0x19, s
->mix
.micpreamp
);
867 return put_user(s
->mix
.micpreamp
, p
);
869 if (cmd
== SOUND_MIXER_PRIVATE2
) {
870 /* enable/disable/query use of linein as second lineout */
871 if (get_user(val
, p
))
874 spin_lock_irqsave(&s
->lock
, flags
);
876 s
->ctrl
|= CTRL_XCTL0
;
878 s
->ctrl
&= ~CTRL_XCTL0
;
879 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
880 spin_unlock_irqrestore(&s
->lock
, flags
);
882 return put_user((s
->ctrl
& CTRL_XCTL0
) ? 1 : 0, p
);
884 if (cmd
== SOUND_MIXER_PRIVATE3
) {
885 /* enable/disable/query microphone impedance setting */
886 if (get_user(val
, p
))
889 spin_lock_irqsave(&s
->lock
, flags
);
891 s
->ctrl
|= CTRL_XCTL1
;
893 s
->ctrl
&= ~CTRL_XCTL1
;
894 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
895 spin_unlock_irqrestore(&s
->lock
, flags
);
897 return put_user((s
->ctrl
& CTRL_XCTL1
) ? 1 : 0, p
);
899 if (cmd
== SOUND_MIXER_INFO
) {
901 strncpy(info
.id
, "ES1370", sizeof(info
.id
));
902 strncpy(info
.name
, "Ensoniq ES1370", sizeof(info
.name
));
903 info
.modify_counter
= s
->mix
.modcnt
;
904 if (copy_to_user((void __user
*)arg
, &info
, sizeof(info
)))
908 if (cmd
== SOUND_OLD_MIXER_INFO
) {
909 _old_mixer_info info
;
910 strncpy(info
.id
, "ES1370", sizeof(info
.id
));
911 strncpy(info
.name
, "Ensoniq ES1370", sizeof(info
.name
));
912 if (copy_to_user((void __user
*)arg
, &info
, sizeof(info
)))
916 if (cmd
== OSS_GETVERSION
)
917 return put_user(SOUND_VERSION
, p
);
918 if (_IOC_TYPE(cmd
) != 'M' || _SIOC_SIZE(cmd
) != sizeof(int))
920 if (_SIOC_DIR(cmd
) == _SIOC_READ
) {
921 switch (_IOC_NR(cmd
)) {
922 case SOUND_MIXER_RECSRC
: /* Arg contains a bit for each recording source */
923 return put_user(s
->mix
.recsrc
, p
);
925 case SOUND_MIXER_DEVMASK
: /* Arg contains a bit for each supported device */
926 val
= SOUND_MASK_IMIX
;
927 for (i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++)
928 if (mixtable
[i
].avail
)
930 return put_user(val
, p
);
932 case SOUND_MIXER_RECMASK
: /* Arg contains a bit for each supported recording source */
933 for (val
= i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++)
934 if (mixtable
[i
].recmask
)
936 return put_user(val
, p
);
938 case SOUND_MIXER_STEREODEVS
: /* Mixer channels supporting stereo */
939 for (val
= i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++)
940 if (mixtable
[i
].stereo
)
942 return put_user(val
, p
);
944 case SOUND_MIXER_CAPS
:
945 return put_user(0, p
);
947 case SOUND_MIXER_IMIX
:
948 return put_user(s
->mix
.imix
, p
);
952 if (i
>= SOUND_MIXER_NRDEVICES
|| !mixtable
[i
].avail
)
954 return put_user(s
->mix
.vol
[mixtable
[i
].volidx
], p
);
957 if (_SIOC_DIR(cmd
) != (_SIOC_READ
|_SIOC_WRITE
))
960 switch (_IOC_NR(cmd
)) {
962 case SOUND_MIXER_IMIX
:
963 if (get_user(s
->mix
.imix
, p
))
965 set_recsrc(s
, s
->mix
.recsrc
);
968 case SOUND_MIXER_RECSRC
: /* Arg contains a bit for each recording source */
969 if (get_user(val
, p
))
976 if (i
>= SOUND_MIXER_NRDEVICES
|| !mixtable
[i
].avail
)
978 if (get_user(val
, p
))
983 if (mixtable
[i
].stereo
) {
984 r
= (val
>> 8) & 0xff;
991 rl
= 31 - ((l
- 7) / 3);
992 l
= (31 - rl
) * 3 + 7;
998 rr
= 31 - ((r
- 7) / 3);
999 r
= (31 - rr
) * 3 + 7;
1001 wrcodec(s
, mixtable
[i
].right
, rr
);
1003 if (mixtable
[i
].left
== 15) {
1008 rl
= 7 - ((l
- 2) / 14);
1009 r
= l
= (7 - rl
) * 14 + 2;
1016 rl
= 31 - ((l
- 7) / 3);
1017 r
= l
= (31 - rl
) * 3 + 7;
1021 wrcodec(s
, mixtable
[i
].left
, rl
);
1022 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1023 s
->mix
.vol
[mixtable
[i
].volidx
] = ((unsigned int)r
<< 8) | l
;
1025 s
->mix
.vol
[mixtable
[i
].volidx
] = val
;
1027 return put_user(s
->mix
.vol
[mixtable
[i
].volidx
], p
);
1031 /* --------------------------------------------------------------------- */
1033 static int es1370_open_mixdev(struct inode
*inode
, struct file
*file
)
1035 unsigned int minor
= iminor(inode
);
1036 struct list_head
*list
;
1037 struct es1370_state
*s
;
1039 for (list
= devs
.next
; ; list
= list
->next
) {
1042 s
= list_entry(list
, struct es1370_state
, devs
);
1043 if (s
->dev_mixer
== minor
)
1047 file
->private_data
= s
;
1048 return nonseekable_open(inode
, file
);
1051 static int es1370_release_mixdev(struct inode
*inode
, struct file
*file
)
1053 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1059 static int es1370_ioctl_mixdev(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1061 return mixer_ioctl((struct es1370_state
*)file
->private_data
, cmd
, arg
);
1064 static /*const*/ struct file_operations es1370_mixer_fops
= {
1065 .owner
= THIS_MODULE
,
1066 .llseek
= no_llseek
,
1067 .ioctl
= es1370_ioctl_mixdev
,
1068 .open
= es1370_open_mixdev
,
1069 .release
= es1370_release_mixdev
,
1072 /* --------------------------------------------------------------------- */
1074 static int drain_dac1(struct es1370_state
*s
, int nonblock
)
1076 DECLARE_WAITQUEUE(wait
, current
);
1077 unsigned long flags
;
1080 if (s
->dma_dac1
.mapped
|| !s
->dma_dac1
.ready
)
1082 add_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1084 __set_current_state(TASK_INTERRUPTIBLE
);
1085 spin_lock_irqsave(&s
->lock
, flags
);
1086 count
= s
->dma_dac1
.count
;
1087 spin_unlock_irqrestore(&s
->lock
, flags
);
1090 if (signal_pending(current
))
1093 remove_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1094 set_current_state(TASK_RUNNING
);
1097 tmo
= 3 * HZ
* (count
+ s
->dma_dac1
.fragsize
) / 2
1098 / dac1_samplerate
[(s
->ctrl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
];
1099 tmo
>>= sample_shift
[(s
->sctrl
& SCTRL_P1FMT
) >> SCTRL_SH_P1FMT
];
1100 if (!schedule_timeout(tmo
+ 1))
1101 DBG(printk(KERN_DEBUG
"es1370: dma timed out??\n");)
1103 remove_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1104 set_current_state(TASK_RUNNING
);
1105 if (signal_pending(current
))
1106 return -ERESTARTSYS
;
1110 static int drain_dac2(struct es1370_state
*s
, int nonblock
)
1112 DECLARE_WAITQUEUE(wait
, current
);
1113 unsigned long flags
;
1116 if (s
->dma_dac2
.mapped
|| !s
->dma_dac2
.ready
)
1118 add_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1120 __set_current_state(TASK_INTERRUPTIBLE
);
1121 spin_lock_irqsave(&s
->lock
, flags
);
1122 count
= s
->dma_dac2
.count
;
1123 spin_unlock_irqrestore(&s
->lock
, flags
);
1126 if (signal_pending(current
))
1129 remove_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1130 set_current_state(TASK_RUNNING
);
1133 tmo
= 3 * HZ
* (count
+ s
->dma_dac2
.fragsize
) / 2
1134 / DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
);
1135 tmo
>>= sample_shift
[(s
->sctrl
& SCTRL_P2FMT
) >> SCTRL_SH_P2FMT
];
1136 if (!schedule_timeout(tmo
+ 1))
1137 DBG(printk(KERN_DEBUG
"es1370: dma timed out??\n");)
1139 remove_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1140 set_current_state(TASK_RUNNING
);
1141 if (signal_pending(current
))
1142 return -ERESTARTSYS
;
1146 /* --------------------------------------------------------------------- */
1148 static ssize_t
es1370_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
1150 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1151 DECLARE_WAITQUEUE(wait
, current
);
1153 unsigned long flags
;
1158 if (s
->dma_adc
.mapped
)
1160 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
1163 if (!s
->dma_adc
.ready
&& (ret
= prog_dmabuf_adc(s
)))
1166 add_wait_queue(&s
->dma_adc
.wait
, &wait
);
1168 spin_lock_irqsave(&s
->lock
, flags
);
1169 swptr
= s
->dma_adc
.swptr
;
1170 cnt
= s
->dma_adc
.dmasize
-swptr
;
1171 if (s
->dma_adc
.count
< cnt
)
1172 cnt
= s
->dma_adc
.count
;
1174 __set_current_state(TASK_INTERRUPTIBLE
);
1175 spin_unlock_irqrestore(&s
->lock
, flags
);
1179 if (s
->dma_adc
.enabled
)
1181 if (file
->f_flags
& O_NONBLOCK
) {
1188 if (signal_pending(current
)) {
1194 if (s
->dma_adc
.mapped
)
1201 if (copy_to_user(buffer
, s
->dma_adc
.rawbuf
+ swptr
, cnt
)) {
1206 swptr
= (swptr
+ cnt
) % s
->dma_adc
.dmasize
;
1207 spin_lock_irqsave(&s
->lock
, flags
);
1208 s
->dma_adc
.swptr
= swptr
;
1209 s
->dma_adc
.count
-= cnt
;
1210 spin_unlock_irqrestore(&s
->lock
, flags
);
1214 if (s
->dma_adc
.enabled
)
1219 remove_wait_queue(&s
->dma_adc
.wait
, &wait
);
1220 set_current_state(TASK_RUNNING
);
1224 static ssize_t
es1370_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
1226 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1227 DECLARE_WAITQUEUE(wait
, current
);
1229 unsigned long flags
;
1234 if (s
->dma_dac2
.mapped
)
1236 if (!access_ok(VERIFY_READ
, buffer
, count
))
1239 if (!s
->dma_dac2
.ready
&& (ret
= prog_dmabuf_dac2(s
)))
1242 add_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1244 spin_lock_irqsave(&s
->lock
, flags
);
1245 if (s
->dma_dac2
.count
< 0) {
1246 s
->dma_dac2
.count
= 0;
1247 s
->dma_dac2
.swptr
= s
->dma_dac2
.hwptr
;
1249 swptr
= s
->dma_dac2
.swptr
;
1250 cnt
= s
->dma_dac2
.dmasize
-swptr
;
1251 if (s
->dma_dac2
.count
+ cnt
> s
->dma_dac2
.dmasize
)
1252 cnt
= s
->dma_dac2
.dmasize
- s
->dma_dac2
.count
;
1254 __set_current_state(TASK_INTERRUPTIBLE
);
1255 spin_unlock_irqrestore(&s
->lock
, flags
);
1259 if (s
->dma_dac2
.enabled
)
1261 if (file
->f_flags
& O_NONBLOCK
) {
1268 if (signal_pending(current
)) {
1274 if (s
->dma_dac2
.mapped
)
1281 if (copy_from_user(s
->dma_dac2
.rawbuf
+ swptr
, buffer
, cnt
)) {
1286 swptr
= (swptr
+ cnt
) % s
->dma_dac2
.dmasize
;
1287 spin_lock_irqsave(&s
->lock
, flags
);
1288 s
->dma_dac2
.swptr
= swptr
;
1289 s
->dma_dac2
.count
+= cnt
;
1290 s
->dma_dac2
.endcleared
= 0;
1291 spin_unlock_irqrestore(&s
->lock
, flags
);
1295 if (s
->dma_dac2
.enabled
)
1300 remove_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1301 set_current_state(TASK_RUNNING
);
1305 /* No kernel lock - we have our own spinlock */
1306 static unsigned int es1370_poll(struct file
*file
, struct poll_table_struct
*wait
)
1308 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1309 unsigned long flags
;
1310 unsigned int mask
= 0;
1313 if (file
->f_mode
& FMODE_WRITE
) {
1314 if (!s
->dma_dac2
.ready
&& prog_dmabuf_dac2(s
))
1316 poll_wait(file
, &s
->dma_dac2
.wait
, wait
);
1318 if (file
->f_mode
& FMODE_READ
) {
1319 if (!s
->dma_adc
.ready
&& prog_dmabuf_adc(s
))
1321 poll_wait(file
, &s
->dma_adc
.wait
, wait
);
1323 spin_lock_irqsave(&s
->lock
, flags
);
1324 es1370_update_ptr(s
);
1325 if (file
->f_mode
& FMODE_READ
) {
1326 if (s
->dma_adc
.count
>= (signed)s
->dma_adc
.fragsize
)
1327 mask
|= POLLIN
| POLLRDNORM
;
1329 if (file
->f_mode
& FMODE_WRITE
) {
1330 if (s
->dma_dac2
.mapped
) {
1331 if (s
->dma_dac2
.count
>= (signed)s
->dma_dac2
.fragsize
)
1332 mask
|= POLLOUT
| POLLWRNORM
;
1334 if ((signed)s
->dma_dac2
.dmasize
>= s
->dma_dac2
.count
+ (signed)s
->dma_dac2
.fragsize
)
1335 mask
|= POLLOUT
| POLLWRNORM
;
1338 spin_unlock_irqrestore(&s
->lock
, flags
);
1342 static int es1370_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1344 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1352 if (vma
->vm_flags
& VM_WRITE
) {
1353 if ((ret
= prog_dmabuf_dac2(s
)) != 0) {
1357 } else if (vma
->vm_flags
& VM_READ
) {
1358 if ((ret
= prog_dmabuf_adc(s
)) != 0) {
1366 if (vma
->vm_pgoff
!= 0) {
1370 size
= vma
->vm_end
- vma
->vm_start
;
1371 if (size
> (PAGE_SIZE
<< db
->buforder
)) {
1375 if (remap_pfn_range(vma
, vma
->vm_start
,
1376 virt_to_phys(db
->rawbuf
) >> PAGE_SHIFT
,
1377 size
, vma
->vm_page_prot
)) {
1388 static int es1370_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1390 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1391 unsigned long flags
;
1392 audio_buf_info abinfo
;
1395 int val
, mapped
, ret
;
1396 void __user
*argp
= (void __user
*)arg
;
1397 int __user
*p
= argp
;
1400 mapped
= ((file
->f_mode
& FMODE_WRITE
) && s
->dma_dac2
.mapped
) ||
1401 ((file
->f_mode
& FMODE_READ
) && s
->dma_adc
.mapped
);
1403 case OSS_GETVERSION
:
1404 return put_user(SOUND_VERSION
, p
);
1406 case SNDCTL_DSP_SYNC
:
1407 if (file
->f_mode
& FMODE_WRITE
)
1408 return drain_dac2(s
, 0/*file->f_flags & O_NONBLOCK*/);
1411 case SNDCTL_DSP_SETDUPLEX
:
1414 case SNDCTL_DSP_GETCAPS
:
1415 return put_user(DSP_CAP_DUPLEX
| DSP_CAP_REALTIME
| DSP_CAP_TRIGGER
| DSP_CAP_MMAP
, p
);
1417 case SNDCTL_DSP_RESET
:
1418 if (file
->f_mode
& FMODE_WRITE
) {
1420 synchronize_irq(s
->irq
);
1421 s
->dma_dac2
.swptr
= s
->dma_dac2
.hwptr
= s
->dma_dac2
.count
= s
->dma_dac2
.total_bytes
= 0;
1423 if (file
->f_mode
& FMODE_READ
) {
1425 synchronize_irq(s
->irq
);
1426 s
->dma_adc
.swptr
= s
->dma_adc
.hwptr
= s
->dma_adc
.count
= s
->dma_adc
.total_bytes
= 0;
1430 case SNDCTL_DSP_SPEED
:
1431 if (get_user(val
, p
))
1434 if (s
->open_mode
& (~file
->f_mode
) & (FMODE_READ
|FMODE_WRITE
))
1442 s
->dma_adc
.ready
= s
->dma_dac2
.ready
= 0;
1443 spin_lock_irqsave(&s
->lock
, flags
);
1444 s
->ctrl
= (s
->ctrl
& ~CTRL_PCLKDIV
) | (DAC2_SRTODIV(val
) << CTRL_SH_PCLKDIV
);
1445 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
1446 spin_unlock_irqrestore(&s
->lock
, flags
);
1448 return put_user(DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
), p
);
1450 case SNDCTL_DSP_STEREO
:
1451 if (get_user(val
, p
))
1453 if (file
->f_mode
& FMODE_READ
) {
1455 s
->dma_adc
.ready
= 0;
1456 spin_lock_irqsave(&s
->lock
, flags
);
1458 s
->sctrl
|= SCTRL_R1SMB
;
1460 s
->sctrl
&= ~SCTRL_R1SMB
;
1461 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1462 spin_unlock_irqrestore(&s
->lock
, flags
);
1464 if (file
->f_mode
& FMODE_WRITE
) {
1466 s
->dma_dac2
.ready
= 0;
1467 spin_lock_irqsave(&s
->lock
, flags
);
1469 s
->sctrl
|= SCTRL_P2SMB
;
1471 s
->sctrl
&= ~SCTRL_P2SMB
;
1472 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1473 spin_unlock_irqrestore(&s
->lock
, flags
);
1477 case SNDCTL_DSP_CHANNELS
:
1478 if (get_user(val
, p
))
1481 if (file
->f_mode
& FMODE_READ
) {
1483 s
->dma_adc
.ready
= 0;
1484 spin_lock_irqsave(&s
->lock
, flags
);
1486 s
->sctrl
|= SCTRL_R1SMB
;
1488 s
->sctrl
&= ~SCTRL_R1SMB
;
1489 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1490 spin_unlock_irqrestore(&s
->lock
, flags
);
1492 if (file
->f_mode
& FMODE_WRITE
) {
1494 s
->dma_dac2
.ready
= 0;
1495 spin_lock_irqsave(&s
->lock
, flags
);
1497 s
->sctrl
|= SCTRL_P2SMB
;
1499 s
->sctrl
&= ~SCTRL_P2SMB
;
1500 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1501 spin_unlock_irqrestore(&s
->lock
, flags
);
1504 return put_user((s
->sctrl
& ((file
->f_mode
& FMODE_READ
) ? SCTRL_R1SMB
: SCTRL_P2SMB
)) ? 2 : 1, p
);
1506 case SNDCTL_DSP_GETFMTS
: /* Returns a mask */
1507 return put_user(AFMT_S16_LE
|AFMT_U8
, p
);
1509 case SNDCTL_DSP_SETFMT
: /* Selects ONE fmt*/
1510 if (get_user(val
, p
))
1512 if (val
!= AFMT_QUERY
) {
1513 if (file
->f_mode
& FMODE_READ
) {
1515 s
->dma_adc
.ready
= 0;
1516 spin_lock_irqsave(&s
->lock
, flags
);
1517 if (val
== AFMT_S16_LE
)
1518 s
->sctrl
|= SCTRL_R1SEB
;
1520 s
->sctrl
&= ~SCTRL_R1SEB
;
1521 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1522 spin_unlock_irqrestore(&s
->lock
, flags
);
1524 if (file
->f_mode
& FMODE_WRITE
) {
1526 s
->dma_dac2
.ready
= 0;
1527 spin_lock_irqsave(&s
->lock
, flags
);
1528 if (val
== AFMT_S16_LE
)
1529 s
->sctrl
|= SCTRL_P2SEB
;
1531 s
->sctrl
&= ~SCTRL_P2SEB
;
1532 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1533 spin_unlock_irqrestore(&s
->lock
, flags
);
1536 return put_user((s
->sctrl
& ((file
->f_mode
& FMODE_READ
) ? SCTRL_R1SEB
: SCTRL_P2SEB
)) ?
1537 AFMT_S16_LE
: AFMT_U8
, p
);
1539 case SNDCTL_DSP_POST
:
1542 case SNDCTL_DSP_GETTRIGGER
:
1544 if (file
->f_mode
& FMODE_READ
&& s
->ctrl
& CTRL_ADC_EN
)
1545 val
|= PCM_ENABLE_INPUT
;
1546 if (file
->f_mode
& FMODE_WRITE
&& s
->ctrl
& CTRL_DAC2_EN
)
1547 val
|= PCM_ENABLE_OUTPUT
;
1548 return put_user(val
, p
);
1550 case SNDCTL_DSP_SETTRIGGER
:
1551 if (get_user(val
, p
))
1553 if (file
->f_mode
& FMODE_READ
) {
1554 if (val
& PCM_ENABLE_INPUT
) {
1555 if (!s
->dma_adc
.ready
&& (ret
= prog_dmabuf_adc(s
)))
1557 s
->dma_adc
.enabled
= 1;
1560 s
->dma_adc
.enabled
= 0;
1564 if (file
->f_mode
& FMODE_WRITE
) {
1565 if (val
& PCM_ENABLE_OUTPUT
) {
1566 if (!s
->dma_dac2
.ready
&& (ret
= prog_dmabuf_dac2(s
)))
1568 s
->dma_dac2
.enabled
= 1;
1571 s
->dma_dac2
.enabled
= 0;
1577 case SNDCTL_DSP_GETOSPACE
:
1578 if (!(file
->f_mode
& FMODE_WRITE
))
1580 if (!s
->dma_dac2
.ready
&& (val
= prog_dmabuf_dac2(s
)) != 0)
1582 spin_lock_irqsave(&s
->lock
, flags
);
1583 es1370_update_ptr(s
);
1584 abinfo
.fragsize
= s
->dma_dac2
.fragsize
;
1585 count
= s
->dma_dac2
.count
;
1588 abinfo
.bytes
= s
->dma_dac2
.dmasize
- count
;
1589 abinfo
.fragstotal
= s
->dma_dac2
.numfrag
;
1590 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_dac2
.fragshift
;
1591 spin_unlock_irqrestore(&s
->lock
, flags
);
1592 return copy_to_user(argp
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1594 case SNDCTL_DSP_GETISPACE
:
1595 if (!(file
->f_mode
& FMODE_READ
))
1597 if (!s
->dma_adc
.ready
&& (val
= prog_dmabuf_adc(s
)) != 0)
1599 spin_lock_irqsave(&s
->lock
, flags
);
1600 es1370_update_ptr(s
);
1601 abinfo
.fragsize
= s
->dma_adc
.fragsize
;
1602 count
= s
->dma_adc
.count
;
1605 abinfo
.bytes
= count
;
1606 abinfo
.fragstotal
= s
->dma_adc
.numfrag
;
1607 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_adc
.fragshift
;
1608 spin_unlock_irqrestore(&s
->lock
, flags
);
1609 return copy_to_user(argp
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1611 case SNDCTL_DSP_NONBLOCK
:
1612 file
->f_flags
|= O_NONBLOCK
;
1615 case SNDCTL_DSP_GETODELAY
:
1616 if (!(file
->f_mode
& FMODE_WRITE
))
1618 if (!s
->dma_dac2
.ready
&& (val
= prog_dmabuf_dac2(s
)) != 0)
1620 spin_lock_irqsave(&s
->lock
, flags
);
1621 es1370_update_ptr(s
);
1622 count
= s
->dma_dac2
.count
;
1623 spin_unlock_irqrestore(&s
->lock
, flags
);
1626 return put_user(count
, p
);
1628 case SNDCTL_DSP_GETIPTR
:
1629 if (!(file
->f_mode
& FMODE_READ
))
1631 if (!s
->dma_adc
.ready
&& (val
= prog_dmabuf_adc(s
)) != 0)
1633 spin_lock_irqsave(&s
->lock
, flags
);
1634 es1370_update_ptr(s
);
1635 cinfo
.bytes
= s
->dma_adc
.total_bytes
;
1636 count
= s
->dma_adc
.count
;
1639 cinfo
.blocks
= count
>> s
->dma_adc
.fragshift
;
1640 cinfo
.ptr
= s
->dma_adc
.hwptr
;
1641 if (s
->dma_adc
.mapped
)
1642 s
->dma_adc
.count
&= s
->dma_adc
.fragsize
-1;
1643 spin_unlock_irqrestore(&s
->lock
, flags
);
1644 if (copy_to_user(argp
, &cinfo
, sizeof(cinfo
)))
1648 case SNDCTL_DSP_GETOPTR
:
1649 if (!(file
->f_mode
& FMODE_WRITE
))
1651 if (!s
->dma_dac2
.ready
&& (val
= prog_dmabuf_dac2(s
)) != 0)
1653 spin_lock_irqsave(&s
->lock
, flags
);
1654 es1370_update_ptr(s
);
1655 cinfo
.bytes
= s
->dma_dac2
.total_bytes
;
1656 count
= s
->dma_dac2
.count
;
1659 cinfo
.blocks
= count
>> s
->dma_dac2
.fragshift
;
1660 cinfo
.ptr
= s
->dma_dac2
.hwptr
;
1661 if (s
->dma_dac2
.mapped
)
1662 s
->dma_dac2
.count
&= s
->dma_dac2
.fragsize
-1;
1663 spin_unlock_irqrestore(&s
->lock
, flags
);
1664 if (copy_to_user(argp
, &cinfo
, sizeof(cinfo
)))
1668 case SNDCTL_DSP_GETBLKSIZE
:
1669 if (file
->f_mode
& FMODE_WRITE
) {
1670 if ((val
= prog_dmabuf_dac2(s
)))
1672 return put_user(s
->dma_dac2
.fragsize
, p
);
1674 if ((val
= prog_dmabuf_adc(s
)))
1676 return put_user(s
->dma_adc
.fragsize
, p
);
1678 case SNDCTL_DSP_SETFRAGMENT
:
1679 if (get_user(val
, p
))
1681 if (file
->f_mode
& FMODE_READ
) {
1682 s
->dma_adc
.ossfragshift
= val
& 0xffff;
1683 s
->dma_adc
.ossmaxfrags
= (val
>> 16) & 0xffff;
1684 if (s
->dma_adc
.ossfragshift
< 4)
1685 s
->dma_adc
.ossfragshift
= 4;
1686 if (s
->dma_adc
.ossfragshift
> 15)
1687 s
->dma_adc
.ossfragshift
= 15;
1688 if (s
->dma_adc
.ossmaxfrags
< 4)
1689 s
->dma_adc
.ossmaxfrags
= 4;
1691 if (file
->f_mode
& FMODE_WRITE
) {
1692 s
->dma_dac2
.ossfragshift
= val
& 0xffff;
1693 s
->dma_dac2
.ossmaxfrags
= (val
>> 16) & 0xffff;
1694 if (s
->dma_dac2
.ossfragshift
< 4)
1695 s
->dma_dac2
.ossfragshift
= 4;
1696 if (s
->dma_dac2
.ossfragshift
> 15)
1697 s
->dma_dac2
.ossfragshift
= 15;
1698 if (s
->dma_dac2
.ossmaxfrags
< 4)
1699 s
->dma_dac2
.ossmaxfrags
= 4;
1703 case SNDCTL_DSP_SUBDIVIDE
:
1704 if ((file
->f_mode
& FMODE_READ
&& s
->dma_adc
.subdivision
) ||
1705 (file
->f_mode
& FMODE_WRITE
&& s
->dma_dac2
.subdivision
))
1707 if (get_user(val
, p
))
1709 if (val
!= 1 && val
!= 2 && val
!= 4)
1711 if (file
->f_mode
& FMODE_READ
)
1712 s
->dma_adc
.subdivision
= val
;
1713 if (file
->f_mode
& FMODE_WRITE
)
1714 s
->dma_dac2
.subdivision
= val
;
1717 case SOUND_PCM_READ_RATE
:
1718 return put_user(DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
), p
);
1720 case SOUND_PCM_READ_CHANNELS
:
1721 return put_user((s
->sctrl
& ((file
->f_mode
& FMODE_READ
) ? SCTRL_R1SMB
: SCTRL_P2SMB
)) ?
1724 case SOUND_PCM_READ_BITS
:
1725 return put_user((s
->sctrl
& ((file
->f_mode
& FMODE_READ
) ? SCTRL_R1SEB
: SCTRL_P2SEB
)) ?
1728 case SOUND_PCM_WRITE_FILTER
:
1729 case SNDCTL_DSP_SETSYNCRO
:
1730 case SOUND_PCM_READ_FILTER
:
1734 return mixer_ioctl(s
, cmd
, arg
);
1737 static int es1370_open(struct inode
*inode
, struct file
*file
)
1739 unsigned int minor
= iminor(inode
);
1740 DECLARE_WAITQUEUE(wait
, current
);
1741 unsigned long flags
;
1742 struct list_head
*list
;
1743 struct es1370_state
*s
;
1745 for (list
= devs
.next
; ; list
= list
->next
) {
1748 s
= list_entry(list
, struct es1370_state
, devs
);
1749 if (!((s
->dev_audio
^ minor
) & ~0xf))
1753 file
->private_data
= s
;
1754 /* wait for device to become free */
1756 while (s
->open_mode
& file
->f_mode
) {
1757 if (file
->f_flags
& O_NONBLOCK
) {
1761 add_wait_queue(&s
->open_wait
, &wait
);
1762 __set_current_state(TASK_INTERRUPTIBLE
);
1765 remove_wait_queue(&s
->open_wait
, &wait
);
1766 set_current_state(TASK_RUNNING
);
1767 if (signal_pending(current
))
1768 return -ERESTARTSYS
;
1771 spin_lock_irqsave(&s
->lock
, flags
);
1772 if (!(s
->open_mode
& (FMODE_READ
|FMODE_WRITE
)))
1773 s
->ctrl
= (s
->ctrl
& ~CTRL_PCLKDIV
) | (DAC2_SRTODIV(8000) << CTRL_SH_PCLKDIV
);
1774 if (file
->f_mode
& FMODE_READ
) {
1775 s
->dma_adc
.ossfragshift
= s
->dma_adc
.ossmaxfrags
= s
->dma_adc
.subdivision
= 0;
1776 s
->dma_adc
.enabled
= 1;
1777 s
->sctrl
&= ~SCTRL_R1FMT
;
1778 if ((minor
& 0xf) == SND_DEV_DSP16
)
1779 s
->sctrl
|= ES1370_FMT_S16_MONO
<< SCTRL_SH_R1FMT
;
1781 s
->sctrl
|= ES1370_FMT_U8_MONO
<< SCTRL_SH_R1FMT
;
1783 if (file
->f_mode
& FMODE_WRITE
) {
1784 s
->dma_dac2
.ossfragshift
= s
->dma_dac2
.ossmaxfrags
= s
->dma_dac2
.subdivision
= 0;
1785 s
->dma_dac2
.enabled
= 1;
1786 s
->sctrl
&= ~SCTRL_P2FMT
;
1787 if ((minor
& 0xf) == SND_DEV_DSP16
)
1788 s
->sctrl
|= ES1370_FMT_S16_MONO
<< SCTRL_SH_P2FMT
;
1790 s
->sctrl
|= ES1370_FMT_U8_MONO
<< SCTRL_SH_P2FMT
;
1792 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1793 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
1794 spin_unlock_irqrestore(&s
->lock
, flags
);
1795 s
->open_mode
|= file
->f_mode
& (FMODE_READ
| FMODE_WRITE
);
1797 init_MUTEX(&s
->sem
);
1798 return nonseekable_open(inode
, file
);
1801 static int es1370_release(struct inode
*inode
, struct file
*file
)
1803 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1807 if (file
->f_mode
& FMODE_WRITE
)
1808 drain_dac2(s
, file
->f_flags
& O_NONBLOCK
);
1810 if (file
->f_mode
& FMODE_WRITE
) {
1812 synchronize_irq(s
->irq
);
1813 dealloc_dmabuf(s
, &s
->dma_dac2
);
1815 if (file
->f_mode
& FMODE_READ
) {
1817 dealloc_dmabuf(s
, &s
->dma_adc
);
1819 s
->open_mode
&= ~(file
->f_mode
& (FMODE_READ
|FMODE_WRITE
));
1820 wake_up(&s
->open_wait
);
1826 static /*const*/ struct file_operations es1370_audio_fops
= {
1827 .owner
= THIS_MODULE
,
1828 .llseek
= no_llseek
,
1829 .read
= es1370_read
,
1830 .write
= es1370_write
,
1831 .poll
= es1370_poll
,
1832 .ioctl
= es1370_ioctl
,
1833 .mmap
= es1370_mmap
,
1834 .open
= es1370_open
,
1835 .release
= es1370_release
,
1838 /* --------------------------------------------------------------------- */
1840 static ssize_t
es1370_write_dac(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
1842 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1843 DECLARE_WAITQUEUE(wait
, current
);
1845 unsigned long flags
;
1850 if (s
->dma_dac1
.mapped
)
1852 if (!s
->dma_dac1
.ready
&& (ret
= prog_dmabuf_dac1(s
)))
1854 if (!access_ok(VERIFY_READ
, buffer
, count
))
1856 add_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1858 spin_lock_irqsave(&s
->lock
, flags
);
1859 if (s
->dma_dac1
.count
< 0) {
1860 s
->dma_dac1
.count
= 0;
1861 s
->dma_dac1
.swptr
= s
->dma_dac1
.hwptr
;
1863 swptr
= s
->dma_dac1
.swptr
;
1864 cnt
= s
->dma_dac1
.dmasize
-swptr
;
1865 if (s
->dma_dac1
.count
+ cnt
> s
->dma_dac1
.dmasize
)
1866 cnt
= s
->dma_dac1
.dmasize
- s
->dma_dac1
.count
;
1868 __set_current_state(TASK_INTERRUPTIBLE
);
1869 spin_unlock_irqrestore(&s
->lock
, flags
);
1873 if (s
->dma_dac1
.enabled
)
1875 if (file
->f_flags
& O_NONBLOCK
) {
1881 if (signal_pending(current
)) {
1888 if (copy_from_user(s
->dma_dac1
.rawbuf
+ swptr
, buffer
, cnt
)) {
1893 swptr
= (swptr
+ cnt
) % s
->dma_dac1
.dmasize
;
1894 spin_lock_irqsave(&s
->lock
, flags
);
1895 s
->dma_dac1
.swptr
= swptr
;
1896 s
->dma_dac1
.count
+= cnt
;
1897 s
->dma_dac1
.endcleared
= 0;
1898 spin_unlock_irqrestore(&s
->lock
, flags
);
1902 if (s
->dma_dac1
.enabled
)
1905 remove_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1906 set_current_state(TASK_RUNNING
);
1910 /* No kernel lock - we have our own spinlock */
1911 static unsigned int es1370_poll_dac(struct file
*file
, struct poll_table_struct
*wait
)
1913 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1914 unsigned long flags
;
1915 unsigned int mask
= 0;
1918 if (!s
->dma_dac1
.ready
&& prog_dmabuf_dac1(s
))
1920 poll_wait(file
, &s
->dma_dac1
.wait
, wait
);
1921 spin_lock_irqsave(&s
->lock
, flags
);
1922 es1370_update_ptr(s
);
1923 if (s
->dma_dac1
.mapped
) {
1924 if (s
->dma_dac1
.count
>= (signed)s
->dma_dac1
.fragsize
)
1925 mask
|= POLLOUT
| POLLWRNORM
;
1927 if ((signed)s
->dma_dac1
.dmasize
>= s
->dma_dac1
.count
+ (signed)s
->dma_dac1
.fragsize
)
1928 mask
|= POLLOUT
| POLLWRNORM
;
1930 spin_unlock_irqrestore(&s
->lock
, flags
);
1934 static int es1370_mmap_dac(struct file
*file
, struct vm_area_struct
*vma
)
1936 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1941 if (!(vma
->vm_flags
& VM_WRITE
))
1944 if ((ret
= prog_dmabuf_dac1(s
)) != 0)
1947 if (vma
->vm_pgoff
!= 0)
1949 size
= vma
->vm_end
- vma
->vm_start
;
1950 if (size
> (PAGE_SIZE
<< s
->dma_dac1
.buforder
))
1953 if (remap_pfn_range(vma
, vma
->vm_start
,
1954 virt_to_phys(s
->dma_dac1
.rawbuf
) >> PAGE_SHIFT
,
1955 size
, vma
->vm_page_prot
))
1957 s
->dma_dac1
.mapped
= 1;
1964 static int es1370_ioctl_dac(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1966 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1967 unsigned long flags
;
1968 audio_buf_info abinfo
;
1973 int __user
*p
= (int __user
*)arg
;
1977 case OSS_GETVERSION
:
1978 return put_user(SOUND_VERSION
, p
);
1980 case SNDCTL_DSP_SYNC
:
1981 return drain_dac1(s
, 0/*file->f_flags & O_NONBLOCK*/);
1983 case SNDCTL_DSP_SETDUPLEX
:
1986 case SNDCTL_DSP_GETCAPS
:
1987 return put_user(DSP_CAP_REALTIME
| DSP_CAP_TRIGGER
| DSP_CAP_MMAP
, p
);
1989 case SNDCTL_DSP_RESET
:
1991 synchronize_irq(s
->irq
);
1992 s
->dma_dac1
.swptr
= s
->dma_dac1
.hwptr
= s
->dma_dac1
.count
= s
->dma_dac1
.total_bytes
= 0;
1995 case SNDCTL_DSP_SPEED
:
1996 if (get_user(val
, p
))
2000 s
->dma_dac1
.ready
= 0;
2001 for (ctrl
= 0; ctrl
<= 2; ctrl
++)
2002 if (val
< (dac1_samplerate
[ctrl
] + dac1_samplerate
[ctrl
+1]) / 2)
2004 spin_lock_irqsave(&s
->lock
, flags
);
2005 s
->ctrl
= (s
->ctrl
& ~CTRL_WTSRSEL
) | (ctrl
<< CTRL_SH_WTSRSEL
);
2006 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
2007 spin_unlock_irqrestore(&s
->lock
, flags
);
2009 return put_user(dac1_samplerate
[(s
->ctrl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
], p
);
2011 case SNDCTL_DSP_STEREO
:
2012 if (get_user(val
, p
))
2015 s
->dma_dac1
.ready
= 0;
2016 spin_lock_irqsave(&s
->lock
, flags
);
2018 s
->sctrl
|= SCTRL_P1SMB
;
2020 s
->sctrl
&= ~SCTRL_P1SMB
;
2021 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
2022 spin_unlock_irqrestore(&s
->lock
, flags
);
2025 case SNDCTL_DSP_CHANNELS
:
2026 if (get_user(val
, p
))
2029 if (s
->dma_dac1
.mapped
)
2032 s
->dma_dac1
.ready
= 0;
2033 spin_lock_irqsave(&s
->lock
, flags
);
2035 s
->sctrl
|= SCTRL_P1SMB
;
2037 s
->sctrl
&= ~SCTRL_P1SMB
;
2038 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
2039 spin_unlock_irqrestore(&s
->lock
, flags
);
2041 return put_user((s
->sctrl
& SCTRL_P1SMB
) ? 2 : 1, p
);
2043 case SNDCTL_DSP_GETFMTS
: /* Returns a mask */
2044 return put_user(AFMT_S16_LE
|AFMT_U8
, p
);
2046 case SNDCTL_DSP_SETFMT
: /* Selects ONE fmt*/
2047 if (get_user(val
, p
))
2049 if (val
!= AFMT_QUERY
) {
2051 s
->dma_dac1
.ready
= 0;
2052 spin_lock_irqsave(&s
->lock
, flags
);
2053 if (val
== AFMT_S16_LE
)
2054 s
->sctrl
|= SCTRL_P1SEB
;
2056 s
->sctrl
&= ~SCTRL_P1SEB
;
2057 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
2058 spin_unlock_irqrestore(&s
->lock
, flags
);
2060 return put_user((s
->sctrl
& SCTRL_P1SEB
) ? AFMT_S16_LE
: AFMT_U8
, p
);
2062 case SNDCTL_DSP_POST
:
2065 case SNDCTL_DSP_GETTRIGGER
:
2066 return put_user((s
->ctrl
& CTRL_DAC1_EN
) ? PCM_ENABLE_OUTPUT
: 0, p
);
2068 case SNDCTL_DSP_SETTRIGGER
:
2069 if (get_user(val
, p
))
2071 if (val
& PCM_ENABLE_OUTPUT
) {
2072 if (!s
->dma_dac1
.ready
&& (ret
= prog_dmabuf_dac1(s
)))
2074 s
->dma_dac1
.enabled
= 1;
2077 s
->dma_dac1
.enabled
= 0;
2082 case SNDCTL_DSP_GETOSPACE
:
2083 if (!s
->dma_dac1
.ready
&& (val
= prog_dmabuf_dac1(s
)) != 0)
2085 spin_lock_irqsave(&s
->lock
, flags
);
2086 es1370_update_ptr(s
);
2087 abinfo
.fragsize
= s
->dma_dac1
.fragsize
;
2088 count
= s
->dma_dac1
.count
;
2091 abinfo
.bytes
= s
->dma_dac1
.dmasize
- count
;
2092 abinfo
.fragstotal
= s
->dma_dac1
.numfrag
;
2093 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_dac1
.fragshift
;
2094 spin_unlock_irqrestore(&s
->lock
, flags
);
2095 return copy_to_user((void __user
*)arg
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
2097 case SNDCTL_DSP_NONBLOCK
:
2098 file
->f_flags
|= O_NONBLOCK
;
2101 case SNDCTL_DSP_GETODELAY
:
2102 if (!s
->dma_dac1
.ready
&& (val
= prog_dmabuf_dac1(s
)) != 0)
2104 spin_lock_irqsave(&s
->lock
, flags
);
2105 es1370_update_ptr(s
);
2106 count
= s
->dma_dac1
.count
;
2107 spin_unlock_irqrestore(&s
->lock
, flags
);
2110 return put_user(count
, p
);
2112 case SNDCTL_DSP_GETOPTR
:
2113 if (!s
->dma_dac1
.ready
&& (val
= prog_dmabuf_dac1(s
)) != 0)
2115 spin_lock_irqsave(&s
->lock
, flags
);
2116 es1370_update_ptr(s
);
2117 cinfo
.bytes
= s
->dma_dac1
.total_bytes
;
2118 count
= s
->dma_dac1
.count
;
2121 cinfo
.blocks
= count
>> s
->dma_dac1
.fragshift
;
2122 cinfo
.ptr
= s
->dma_dac1
.hwptr
;
2123 if (s
->dma_dac1
.mapped
)
2124 s
->dma_dac1
.count
&= s
->dma_dac1
.fragsize
-1;
2125 spin_unlock_irqrestore(&s
->lock
, flags
);
2126 if (copy_to_user((void __user
*)arg
, &cinfo
, sizeof(cinfo
)))
2130 case SNDCTL_DSP_GETBLKSIZE
:
2131 if ((val
= prog_dmabuf_dac1(s
)))
2133 return put_user(s
->dma_dac1
.fragsize
, p
);
2135 case SNDCTL_DSP_SETFRAGMENT
:
2136 if (get_user(val
, p
))
2138 s
->dma_dac1
.ossfragshift
= val
& 0xffff;
2139 s
->dma_dac1
.ossmaxfrags
= (val
>> 16) & 0xffff;
2140 if (s
->dma_dac1
.ossfragshift
< 4)
2141 s
->dma_dac1
.ossfragshift
= 4;
2142 if (s
->dma_dac1
.ossfragshift
> 15)
2143 s
->dma_dac1
.ossfragshift
= 15;
2144 if (s
->dma_dac1
.ossmaxfrags
< 4)
2145 s
->dma_dac1
.ossmaxfrags
= 4;
2148 case SNDCTL_DSP_SUBDIVIDE
:
2149 if (s
->dma_dac1
.subdivision
)
2151 if (get_user(val
, p
))
2153 if (val
!= 1 && val
!= 2 && val
!= 4)
2155 s
->dma_dac1
.subdivision
= val
;
2158 case SOUND_PCM_READ_RATE
:
2159 return put_user(dac1_samplerate
[(s
->ctrl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
], p
);
2161 case SOUND_PCM_READ_CHANNELS
:
2162 return put_user((s
->sctrl
& SCTRL_P1SMB
) ? 2 : 1, p
);
2164 case SOUND_PCM_READ_BITS
:
2165 return put_user((s
->sctrl
& SCTRL_P1SEB
) ? 16 : 8, p
);
2167 case SOUND_PCM_WRITE_FILTER
:
2168 case SNDCTL_DSP_SETSYNCRO
:
2169 case SOUND_PCM_READ_FILTER
:
2173 return mixer_ioctl(s
, cmd
, arg
);
2176 static int es1370_open_dac(struct inode
*inode
, struct file
*file
)
2178 unsigned int minor
= iminor(inode
);
2179 DECLARE_WAITQUEUE(wait
, current
);
2180 unsigned long flags
;
2181 struct list_head
*list
;
2182 struct es1370_state
*s
;
2184 for (list
= devs
.next
; ; list
= list
->next
) {
2187 s
= list_entry(list
, struct es1370_state
, devs
);
2188 if (!((s
->dev_dac
^ minor
) & ~0xf))
2192 /* we allow opening with O_RDWR, most programs do it although they will only write */
2194 if (file
->f_mode
& FMODE_READ
)
2197 if (!(file
->f_mode
& FMODE_WRITE
))
2199 file
->private_data
= s
;
2200 /* wait for device to become free */
2202 while (s
->open_mode
& FMODE_DAC
) {
2203 if (file
->f_flags
& O_NONBLOCK
) {
2207 add_wait_queue(&s
->open_wait
, &wait
);
2208 __set_current_state(TASK_INTERRUPTIBLE
);
2211 remove_wait_queue(&s
->open_wait
, &wait
);
2212 set_current_state(TASK_RUNNING
);
2213 if (signal_pending(current
))
2214 return -ERESTARTSYS
;
2217 s
->dma_dac1
.ossfragshift
= s
->dma_dac1
.ossmaxfrags
= s
->dma_dac1
.subdivision
= 0;
2218 s
->dma_dac1
.enabled
= 1;
2219 spin_lock_irqsave(&s
->lock
, flags
);
2220 s
->ctrl
= (s
->ctrl
& ~CTRL_WTSRSEL
) | (1 << CTRL_SH_WTSRSEL
);
2221 s
->sctrl
&= ~SCTRL_P1FMT
;
2222 if ((minor
& 0xf) == SND_DEV_DSP16
)
2223 s
->sctrl
|= ES1370_FMT_S16_MONO
<< SCTRL_SH_P1FMT
;
2225 s
->sctrl
|= ES1370_FMT_U8_MONO
<< SCTRL_SH_P1FMT
;
2226 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
2227 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
2228 spin_unlock_irqrestore(&s
->lock
, flags
);
2229 s
->open_mode
|= FMODE_DAC
;
2231 return nonseekable_open(inode
, file
);
2234 static int es1370_release_dac(struct inode
*inode
, struct file
*file
)
2236 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2240 drain_dac1(s
, file
->f_flags
& O_NONBLOCK
);
2243 dealloc_dmabuf(s
, &s
->dma_dac1
);
2244 s
->open_mode
&= ~FMODE_DAC
;
2245 wake_up(&s
->open_wait
);
2251 static /*const*/ struct file_operations es1370_dac_fops
= {
2252 .owner
= THIS_MODULE
,
2253 .llseek
= no_llseek
,
2254 .write
= es1370_write_dac
,
2255 .poll
= es1370_poll_dac
,
2256 .ioctl
= es1370_ioctl_dac
,
2257 .mmap
= es1370_mmap_dac
,
2258 .open
= es1370_open_dac
,
2259 .release
= es1370_release_dac
,
2262 /* --------------------------------------------------------------------- */
2264 static ssize_t
es1370_midi_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
2266 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2267 DECLARE_WAITQUEUE(wait
, current
);
2269 unsigned long flags
;
2274 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
2279 add_wait_queue(&s
->midi
.iwait
, &wait
);
2281 spin_lock_irqsave(&s
->lock
, flags
);
2283 cnt
= MIDIINBUF
- ptr
;
2284 if (s
->midi
.icnt
< cnt
)
2287 __set_current_state(TASK_INTERRUPTIBLE
);
2288 spin_unlock_irqrestore(&s
->lock
, flags
);
2292 if (file
->f_flags
& O_NONBLOCK
) {
2298 if (signal_pending(current
)) {
2305 if (copy_to_user(buffer
, s
->midi
.ibuf
+ ptr
, cnt
)) {
2310 ptr
= (ptr
+ cnt
) % MIDIINBUF
;
2311 spin_lock_irqsave(&s
->lock
, flags
);
2313 s
->midi
.icnt
-= cnt
;
2314 spin_unlock_irqrestore(&s
->lock
, flags
);
2320 __set_current_state(TASK_RUNNING
);
2321 remove_wait_queue(&s
->midi
.iwait
, &wait
);
2325 static ssize_t
es1370_midi_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
2327 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2328 DECLARE_WAITQUEUE(wait
, current
);
2330 unsigned long flags
;
2335 if (!access_ok(VERIFY_READ
, buffer
, count
))
2340 add_wait_queue(&s
->midi
.owait
, &wait
);
2342 spin_lock_irqsave(&s
->lock
, flags
);
2344 cnt
= MIDIOUTBUF
- ptr
;
2345 if (s
->midi
.ocnt
+ cnt
> MIDIOUTBUF
)
2346 cnt
= MIDIOUTBUF
- s
->midi
.ocnt
;
2348 __set_current_state(TASK_INTERRUPTIBLE
);
2349 es1370_handle_midi(s
);
2351 spin_unlock_irqrestore(&s
->lock
, flags
);
2355 if (file
->f_flags
& O_NONBLOCK
) {
2361 if (signal_pending(current
)) {
2368 if (copy_from_user(s
->midi
.obuf
+ ptr
, buffer
, cnt
)) {
2373 ptr
= (ptr
+ cnt
) % MIDIOUTBUF
;
2374 spin_lock_irqsave(&s
->lock
, flags
);
2376 s
->midi
.ocnt
+= cnt
;
2377 spin_unlock_irqrestore(&s
->lock
, flags
);
2381 spin_lock_irqsave(&s
->lock
, flags
);
2382 es1370_handle_midi(s
);
2383 spin_unlock_irqrestore(&s
->lock
, flags
);
2385 __set_current_state(TASK_RUNNING
);
2386 remove_wait_queue(&s
->midi
.owait
, &wait
);
2390 /* No kernel lock - we have our own spinlock */
2391 static unsigned int es1370_midi_poll(struct file
*file
, struct poll_table_struct
*wait
)
2393 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2394 unsigned long flags
;
2395 unsigned int mask
= 0;
2398 if (file
->f_mode
& FMODE_WRITE
)
2399 poll_wait(file
, &s
->midi
.owait
, wait
);
2400 if (file
->f_mode
& FMODE_READ
)
2401 poll_wait(file
, &s
->midi
.iwait
, wait
);
2402 spin_lock_irqsave(&s
->lock
, flags
);
2403 if (file
->f_mode
& FMODE_READ
) {
2404 if (s
->midi
.icnt
> 0)
2405 mask
|= POLLIN
| POLLRDNORM
;
2407 if (file
->f_mode
& FMODE_WRITE
) {
2408 if (s
->midi
.ocnt
< MIDIOUTBUF
)
2409 mask
|= POLLOUT
| POLLWRNORM
;
2411 spin_unlock_irqrestore(&s
->lock
, flags
);
2415 static int es1370_midi_open(struct inode
*inode
, struct file
*file
)
2417 unsigned int minor
= iminor(inode
);
2418 DECLARE_WAITQUEUE(wait
, current
);
2419 unsigned long flags
;
2420 struct list_head
*list
;
2421 struct es1370_state
*s
;
2423 for (list
= devs
.next
; ; list
= list
->next
) {
2426 s
= list_entry(list
, struct es1370_state
, devs
);
2427 if (s
->dev_midi
== minor
)
2431 file
->private_data
= s
;
2432 /* wait for device to become free */
2434 while (s
->open_mode
& (file
->f_mode
<< FMODE_MIDI_SHIFT
)) {
2435 if (file
->f_flags
& O_NONBLOCK
) {
2439 add_wait_queue(&s
->open_wait
, &wait
);
2440 __set_current_state(TASK_INTERRUPTIBLE
);
2443 remove_wait_queue(&s
->open_wait
, &wait
);
2444 set_current_state(TASK_RUNNING
);
2445 if (signal_pending(current
))
2446 return -ERESTARTSYS
;
2449 spin_lock_irqsave(&s
->lock
, flags
);
2450 if (!(s
->open_mode
& (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
))) {
2451 s
->midi
.ird
= s
->midi
.iwr
= s
->midi
.icnt
= 0;
2452 s
->midi
.ord
= s
->midi
.owr
= s
->midi
.ocnt
= 0;
2453 outb(UCTRL_CNTRL_SWR
, s
->io
+ES1370_REG_UART_CONTROL
);
2454 outb(0, s
->io
+ES1370_REG_UART_CONTROL
);
2455 outb(0, s
->io
+ES1370_REG_UART_TEST
);
2457 if (file
->f_mode
& FMODE_READ
) {
2458 s
->midi
.ird
= s
->midi
.iwr
= s
->midi
.icnt
= 0;
2460 if (file
->f_mode
& FMODE_WRITE
) {
2461 s
->midi
.ord
= s
->midi
.owr
= s
->midi
.ocnt
= 0;
2463 s
->ctrl
|= CTRL_UART_EN
;
2464 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
2465 es1370_handle_midi(s
);
2466 spin_unlock_irqrestore(&s
->lock
, flags
);
2467 s
->open_mode
|= (file
->f_mode
<< FMODE_MIDI_SHIFT
) & (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
);
2469 return nonseekable_open(inode
, file
);
2472 static int es1370_midi_release(struct inode
*inode
, struct file
*file
)
2474 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2475 DECLARE_WAITQUEUE(wait
, current
);
2476 unsigned long flags
;
2477 unsigned count
, tmo
;
2482 if (file
->f_mode
& FMODE_WRITE
) {
2483 add_wait_queue(&s
->midi
.owait
, &wait
);
2485 __set_current_state(TASK_INTERRUPTIBLE
);
2486 spin_lock_irqsave(&s
->lock
, flags
);
2487 count
= s
->midi
.ocnt
;
2488 spin_unlock_irqrestore(&s
->lock
, flags
);
2491 if (signal_pending(current
))
2493 if (file
->f_flags
& O_NONBLOCK
)
2495 tmo
= (count
* HZ
) / 3100;
2496 if (!schedule_timeout(tmo
? : 1) && tmo
)
2497 DBG(printk(KERN_DEBUG
"es1370: midi timed out??\n");)
2499 remove_wait_queue(&s
->midi
.owait
, &wait
);
2500 set_current_state(TASK_RUNNING
);
2503 s
->open_mode
&= ~((file
->f_mode
<< FMODE_MIDI_SHIFT
) & (FMODE_MIDI_READ
|FMODE_MIDI_WRITE
));
2504 spin_lock_irqsave(&s
->lock
, flags
);
2505 if (!(s
->open_mode
& (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
))) {
2506 s
->ctrl
&= ~CTRL_UART_EN
;
2507 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
2509 spin_unlock_irqrestore(&s
->lock
, flags
);
2510 wake_up(&s
->open_wait
);
2516 static /*const*/ struct file_operations es1370_midi_fops
= {
2517 .owner
= THIS_MODULE
,
2518 .llseek
= no_llseek
,
2519 .read
= es1370_midi_read
,
2520 .write
= es1370_midi_write
,
2521 .poll
= es1370_midi_poll
,
2522 .open
= es1370_midi_open
,
2523 .release
= es1370_midi_release
,
2526 /* --------------------------------------------------------------------- */
2528 /* maximum number of devices; only used for command line params */
2531 static int lineout
[NR_DEVICE
];
2532 static int micbias
[NR_DEVICE
];
2534 static unsigned int devindex
;
2536 module_param_array(lineout
, bool, NULL
, 0);
2537 MODULE_PARM_DESC(lineout
, "if 1 the LINE input is converted to LINE out");
2538 module_param_array(micbias
, bool, NULL
, 0);
2539 MODULE_PARM_DESC(micbias
, "sets the +5V bias for an electret microphone");
2541 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
2542 MODULE_DESCRIPTION("ES1370 AudioPCI Driver");
2543 MODULE_LICENSE("GPL");
2546 /* --------------------------------------------------------------------- */
2548 static struct initvol
{
2551 } initvol
[] __devinitdata
= {
2552 { SOUND_MIXER_WRITE_VOLUME
, 0x4040 },
2553 { SOUND_MIXER_WRITE_PCM
, 0x4040 },
2554 { SOUND_MIXER_WRITE_SYNTH
, 0x4040 },
2555 { SOUND_MIXER_WRITE_CD
, 0x4040 },
2556 { SOUND_MIXER_WRITE_LINE
, 0x4040 },
2557 { SOUND_MIXER_WRITE_LINE1
, 0x4040 },
2558 { SOUND_MIXER_WRITE_LINE2
, 0x4040 },
2559 { SOUND_MIXER_WRITE_LINE3
, 0x4040 },
2560 { SOUND_MIXER_WRITE_MIC
, 0x4040 },
2561 { SOUND_MIXER_WRITE_OGAIN
, 0x4040 }
2564 #ifdef SUPPORT_JOYSTICK
2566 static int __devinit
es1370_register_gameport(struct es1370_state
*s
)
2568 struct gameport
*gp
;
2570 if (!request_region(0x200, JOY_EXTENT
, "es1370")) {
2571 printk(KERN_ERR
"es1370: joystick io port 0x200 in use\n");
2575 s
->gameport
= gp
= gameport_allocate_port();
2577 printk(KERN_ERR
"es1370: can not allocate memory for gameport\n");
2578 release_region(0x200, JOY_EXTENT
);
2582 gameport_set_name(gp
, "ESS1370");
2583 gameport_set_phys(gp
, "pci%s/gameport0", pci_name(s
->dev
));
2584 gp
->dev
.parent
= &s
->dev
->dev
;
2587 s
->ctrl
|= CTRL_JYSTK_EN
;
2588 outl(s
->ctrl
, s
->io
+ ES1370_REG_CONTROL
);
2590 gameport_register_port(gp
);
2595 static inline void es1370_unregister_gameport(struct es1370_state
*s
)
2598 int gpio
= s
->gameport
->io
;
2599 gameport_unregister_port(s
->gameport
);
2600 release_region(gpio
, JOY_EXTENT
);
2606 static inline int es1370_register_gameport(struct es1370_state
*s
) { return -ENOSYS
; }
2607 static inline void es1370_unregister_gameport(struct es1370_state
*s
) { }
2608 #endif /* SUPPORT_JOYSTICK */
2610 static int __devinit
es1370_probe(struct pci_dev
*pcidev
, const struct pci_device_id
*pciid
)
2612 struct es1370_state
*s
;
2616 if ((ret
=pci_enable_device(pcidev
)))
2619 if ( !(pci_resource_flags(pcidev
, 0) & IORESOURCE_IO
) ||
2620 !pci_resource_start(pcidev
, 0)
2623 if (pcidev
->irq
== 0)
2625 i
= pci_set_dma_mask(pcidev
, DMA_32BIT_MASK
);
2627 printk(KERN_WARNING
"es1370: architecture does not support 32bit PCI busmaster DMA\n");
2630 if (!(s
= kmalloc(sizeof(struct es1370_state
), GFP_KERNEL
))) {
2631 printk(KERN_WARNING
"es1370: out of memory\n");
2634 memset(s
, 0, sizeof(struct es1370_state
));
2635 init_waitqueue_head(&s
->dma_adc
.wait
);
2636 init_waitqueue_head(&s
->dma_dac1
.wait
);
2637 init_waitqueue_head(&s
->dma_dac2
.wait
);
2638 init_waitqueue_head(&s
->open_wait
);
2639 init_waitqueue_head(&s
->midi
.iwait
);
2640 init_waitqueue_head(&s
->midi
.owait
);
2641 init_MUTEX(&s
->open_sem
);
2642 spin_lock_init(&s
->lock
);
2643 s
->magic
= ES1370_MAGIC
;
2645 s
->io
= pci_resource_start(pcidev
, 0);
2646 s
->irq
= pcidev
->irq
;
2647 if (!request_region(s
->io
, ES1370_EXTENT
, "es1370")) {
2648 printk(KERN_ERR
"es1370: io ports %#lx-%#lx in use\n", s
->io
, s
->io
+ES1370_EXTENT
-1);
2652 if ((ret
=request_irq(s
->irq
, es1370_interrupt
, SA_SHIRQ
, "es1370",s
))) {
2653 printk(KERN_ERR
"es1370: irq %u in use\n", s
->irq
);
2657 /* initialize codec registers */
2658 /* note: setting CTRL_SERR_DIS is reported to break
2659 * mic bias setting (by Kim.Berts@fisub.mail.abb.com) */
2660 s
->ctrl
= CTRL_CDC_EN
| (DAC2_SRTODIV(8000) << CTRL_SH_PCLKDIV
) | (1 << CTRL_SH_WTSRSEL
);
2661 if (lineout
[devindex
])
2662 s
->ctrl
|= CTRL_XCTL0
;
2663 if (micbias
[devindex
])
2664 s
->ctrl
|= CTRL_XCTL1
;
2666 printk(KERN_INFO
"es1370: adapter at io %#lx irq %u, line %s, mic impedance %s\n",
2667 s
->io
, s
->irq
, (s
->ctrl
& CTRL_XCTL0
) ? "out" : "in",
2668 (s
->ctrl
& CTRL_XCTL1
) ? "1" : "0");
2669 /* register devices */
2670 if ((s
->dev_audio
= register_sound_dsp(&es1370_audio_fops
, -1)) < 0) {
2674 if ((s
->dev_mixer
= register_sound_mixer(&es1370_mixer_fops
, -1)) < 0) {
2678 if ((s
->dev_dac
= register_sound_dsp(&es1370_dac_fops
, -1)) < 0) {
2682 if ((s
->dev_midi
= register_sound_midi(&es1370_midi_fops
, -1)) < 0) {
2686 /* initialize the chips */
2687 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
2688 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
2689 /* point phantom write channel to "bugbuf" */
2690 s
->bugbuf_cpu
= pci_alloc_consistent(pcidev
,16,&s
->bugbuf_dma
);
2691 if (!s
->bugbuf_cpu
) {
2695 outl((ES1370_REG_PHANTOM_FRAMEADR
>> 8) & 15, s
->io
+ES1370_REG_MEMPAGE
);
2696 outl(s
->bugbuf_dma
, s
->io
+(ES1370_REG_PHANTOM_FRAMEADR
& 0xff));
2697 outl(0, s
->io
+(ES1370_REG_PHANTOM_FRAMECNT
& 0xff));
2698 pci_set_master(pcidev
); /* enable bus mastering */
2699 wrcodec(s
, 0x16, 3); /* no RST, PD */
2700 wrcodec(s
, 0x17, 0); /* CODEC ADC and CODEC DAC use {LR,B}CLK2 and run off the LRCLK2 PLL; program DAC_SYNC=0!! */
2701 wrcodec(s
, 0x18, 0); /* recording source is mixer */
2702 wrcodec(s
, 0x19, s
->mix
.micpreamp
= 1); /* turn on MIC preamp */
2706 val
= SOUND_MASK_LINE
|SOUND_MASK_SYNTH
|SOUND_MASK_CD
;
2707 mixer_ioctl(s
, SOUND_MIXER_WRITE_RECSRC
, (unsigned long)&val
);
2708 for (i
= 0; i
< sizeof(initvol
)/sizeof(initvol
[0]); i
++) {
2709 val
= initvol
[i
].vol
;
2710 mixer_ioctl(s
, initvol
[i
].mixch
, (unsigned long)&val
);
2714 es1370_register_gameport(s
);
2716 /* store it in the driver field */
2717 pci_set_drvdata(pcidev
, s
);
2718 /* put it into driver list */
2719 list_add_tail(&s
->devs
, &devs
);
2720 /* increment devindex */
2721 if (devindex
< NR_DEVICE
-1)
2726 unregister_sound_midi(s
->dev_midi
);
2728 unregister_sound_dsp(s
->dev_dac
);
2730 unregister_sound_mixer(s
->dev_mixer
);
2732 unregister_sound_dsp(s
->dev_audio
);
2734 printk(KERN_ERR
"es1370: cannot register misc device\n");
2735 free_irq(s
->irq
, s
);
2737 release_region(s
->io
, ES1370_EXTENT
);
2743 static void __devexit
es1370_remove(struct pci_dev
*dev
)
2745 struct es1370_state
*s
= pci_get_drvdata(dev
);
2750 outl(CTRL_SERR_DIS
| (1 << CTRL_SH_WTSRSEL
), s
->io
+ES1370_REG_CONTROL
); /* switch everything off */
2751 outl(0, s
->io
+ES1370_REG_SERIAL_CONTROL
); /* clear serial interrupts */
2752 synchronize_irq(s
->irq
);
2753 free_irq(s
->irq
, s
);
2754 es1370_unregister_gameport(s
);
2755 release_region(s
->io
, ES1370_EXTENT
);
2756 unregister_sound_dsp(s
->dev_audio
);
2757 unregister_sound_mixer(s
->dev_mixer
);
2758 unregister_sound_dsp(s
->dev_dac
);
2759 unregister_sound_midi(s
->dev_midi
);
2760 pci_free_consistent(dev
, 16, s
->bugbuf_cpu
, s
->bugbuf_dma
);
2762 pci_set_drvdata(dev
, NULL
);
2765 static struct pci_device_id id_table
[] = {
2766 { PCI_VENDOR_ID_ENSONIQ
, PCI_DEVICE_ID_ENSONIQ_ES1370
, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0 },
2770 MODULE_DEVICE_TABLE(pci
, id_table
);
2772 static struct pci_driver es1370_driver
= {
2774 .id_table
= id_table
,
2775 .probe
= es1370_probe
,
2776 .remove
= __devexit_p(es1370_remove
),
2779 static int __init
init_es1370(void)
2781 printk(KERN_INFO
"es1370: version v0.38 time " __TIME__
" " __DATE__
"\n");
2782 return pci_module_init(&es1370_driver
);
2785 static void __exit
cleanup_es1370(void)
2787 printk(KERN_INFO
"es1370: unloading\n");
2788 pci_unregister_driver(&es1370_driver
);
2791 module_init(init_es1370
);
2792 module_exit(cleanup_es1370
);
2794 /* --------------------------------------------------------------------- */
2798 /* format is: es1370=lineout[,micbias]] */
2800 static int __init
es1370_setup(char *str
)
2802 static unsigned __initdata nr_dev
= 0;
2804 if (nr_dev
>= NR_DEVICE
)
2808 ((get_option(&str
,&lineout
[nr_dev
]) == 2)
2809 && get_option(&str
,&micbias
[nr_dev
])
2816 __setup("es1370=", es1370_setup
);