1 /*****************************************************************************/
4 * es1370.c -- Ensoniq ES1370/Asahi Kasei AK4531 audio driver.
6 * Copyright (C) 1998-2000 Thomas Sailer (sailer@ife.ee.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 * joystick if 1 enables the joystick interface on the card; but it still
27 * needs a driver for joysticks connected to a standard IBM-PC
28 * joyport. It is tested with the joy-analog driver. This
29 * module must be loaded before the joystick driver. Kmod will
31 * lineout if 1 the LINE jack is used as an output instead of an input.
32 * LINE then contains the unmixed dsp output. This can be used
33 * to make the card a four channel one: use dsp to output two
34 * channels to LINE and dac to output the other two channels to
35 * SPKR. Set the mixer to only output synth to SPKR.
36 * micbias sets the +5V bias to the mic if using an electretmic.
39 * Note: sync mode is not yet supported (i.e. running dsp and dac from the same
43 * /dev/dsp standard /dev/dsp device, (mostly) OSS compatible
44 * /dev/mixer standard /dev/mixer device, (mostly) OSS compatible
45 * /dev/dsp1 additional DAC, like /dev/dsp, but output only,
46 * only 5512, 11025, 22050 and 44100 samples/s,
47 * outputs to mixer "SYNTH" setting
48 * /dev/midi simple MIDI UART interface, no ioctl
50 * NOTE: the card does not have any FM/Wavetable synthesizer, it is supposed
51 * to be done in software. That is what /dev/dac is for. By now (Q2 1998)
52 * there are several MIDI to PCM (WAV) packages, one of them is timidity.
55 * 26.03.1998 0.1 Initial release
56 * 31.03.1998 0.2 Fix bug in GETOSPACE
57 * 04.04.1998 0.3 Make it work (again) under 2.0.33
58 * Fix mixer write operation not returning the actual
60 * 05.04.1998 0.4 First attempt at using the new PCI stuff
61 * 29.04.1998 0.5 Fix hang when ^C is pressed on amp
62 * 07.05.1998 0.6 Don't double lock around stop_*() in *_release()
63 * 10.05.1998 0.7 First stab at a simple midi interface (no bells&whistles)
64 * 14.05.1998 0.8 Don't allow excessive interrupt rates
65 * 08.06.1998 0.9 First release using Alan Cox' soundcore instead of
67 * 05.07.1998 0.10 Fixed the driver to correctly maintin OSS style volume
68 * settings (not sure if this should be standard)
69 * Fixed many references: f_flags should be f_mode
70 * -- Gerald Britton <gbritton@mit.edu>
71 * 03.08.1998 0.11 Now mixer behaviour can basically be selected between
72 * "OSS documented" and "OSS actual" behaviour
73 * Fixed mixer table thanks to Hakan.Lennestal@lu.erisoft.se
74 * On module startup, set DAC2 to 11kSPS instead of 5.5kSPS,
75 * as it produces an annoying ssssh in the lower sampling rate
76 * Do not include modversions.h
77 * 22.08.1998 0.12 Mixer registers actually have 5 instead of 4 bits
78 * pointed out by Itai Nahshon
79 * 31.08.1998 0.13 Fix realplayer problems - dac.count issues
80 * 08.10.1998 0.14 Joystick support fixed
81 * -- Oliver Neukum <c188@org.chemie.uni-muenchen.de>
82 * 10.12.1998 0.15 Fix drain_dac trying to wait on not yet initialized DMA
83 * 16.12.1998 0.16 Don't wake up app until there are fragsize bytes to read/write
84 * 06.01.1999 0.17 remove the silly SA_INTERRUPT flag.
85 * hopefully killed the egcs section type conflict
86 * 12.03.1999 0.18 cinfo.blocks should be reset after GETxPTR ioctl.
87 * reported by Johan Maes <joma@telindus.be>
88 * 22.03.1999 0.19 return EAGAIN instead of EBUSY when O_NONBLOCK
89 * read/write cannot be executed
90 * 07.04.1999 0.20 implemented the following ioctl's: SOUND_PCM_READ_RATE,
91 * SOUND_PCM_READ_CHANNELS, SOUND_PCM_READ_BITS;
92 * Alpha fixes reported by Peter Jones <pjones@redhat.com>
93 * Note: joystick address handling might still be wrong on archs
95 * 10.05.1999 0.21 Added support for an electret mic for SB PCI64
96 * to the Linux kernel sound driver. This mod also straighten
97 * out the question marks around the mic impedance setting
98 * (micz). From Kim.Berts@fisub.mail.abb.com
99 * 11.05.1999 0.22 Implemented the IMIX call to mute recording monitor.
100 * Guenter Geiger <geiger@epy.co.at>
101 * 15.06.1999 0.23 Fix bad allocation bug.
102 * Thanks to Deti Fliegl <fliegl@in.tum.de>
103 * 28.06.1999 0.24 Add pci_set_master
104 * 02.08.1999 0.25 Added workaround for the "phantom write" bug first
105 * documented by Dave Sharpless from Anchor Games
106 * 03.08.1999 0.26 adapt to Linus' new __setup/__initcall
107 * added kernel command line option "es1370=joystick[,lineout[,micbias]]"
108 * removed CONFIG_SOUND_ES1370_JOYPORT_BOOT kludge
109 * 12.08.1999 0.27 module_init/__setup fixes
110 * 19.08.1999 0.28 SOUND_MIXER_IMIX fixes, reported by Gianluca <gialluca@mail.tiscalinet.it>
111 * 31.08.1999 0.29 add spin_lock_init
112 * replaced current->state = x with set_current_state(x)
113 * 03.09.1999 0.30 change read semantics for MIDI to match
114 * OSS more closely; remove possible wakeup race
115 * 28.10.1999 0.31 More waitqueue races fixed
116 * 08.01.2000 0.32 Prevent some ioctl's from returning bad count values on underrun/overrun;
117 * Tim Janik's BSE (Bedevilled Sound Engine) found this
118 * 07.02.2000 0.33 Use pci_alloc_consistent and pci_register_driver
119 * 21.11.2000 0.34 Initialize dma buffers in poll, otherwise poll may return a bogus mask
121 * some important things missing in Ensoniq documentation:
123 * Experimental PCLKDIV results: play the same waveforms on both DAC1 and DAC2
124 * and vary PCLKDIV to obtain zero beat.
127 * seems to be fs = 1411200/(PCLKDIV+2)
129 * should find out when curr_sample_ct is cleared and
130 * where exactly the CCB fetches data
132 * The card uses a 22.5792 MHz crystal.
133 * The LINEIN jack may be converted to an AOUT jack by
134 * setting pin 47 (XCTL0) of the ES1370 to high.
135 * Pin 48 (XCTL1) of the ES1370 sets the +5V bias for an electretmic
140 /*****************************************************************************/
142 #include <linux/version.h>
143 #include <linux/module.h>
144 #include <linux/string.h>
145 #include <linux/ioport.h>
146 #include <linux/sched.h>
147 #include <linux/delay.h>
148 #include <linux/sound.h>
149 #include <linux/malloc.h>
150 #include <linux/soundcard.h>
151 #include <linux/pci.h>
152 #include <linux/smp_lock.h>
153 #include <linux/wrapper.h>
156 #include <linux/init.h>
157 #include <linux/poll.h>
158 #include <linux/spinlock.h>
159 #include <asm/uaccess.h>
160 #include <asm/hardirq.h>
162 /* --------------------------------------------------------------------- */
164 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
166 /*#define DBG(x) {x}*/
168 /* --------------------------------------------------------------------- */
170 #ifndef PCI_VENDOR_ID_ENSONIQ
171 #define PCI_VENDOR_ID_ENSONIQ 0x1274
173 #ifndef PCI_DEVICE_ID_ENSONIQ_ES1370
174 #define PCI_DEVICE_ID_ENSONIQ_ES1370 0x5000
177 #define ES1370_MAGIC ((PCI_VENDOR_ID_ENSONIQ<<16)|PCI_DEVICE_ID_ENSONIQ_ES1370)
179 #define ES1370_EXTENT 0x40
182 #define ES1370_REG_CONTROL 0x00
183 #define ES1370_REG_STATUS 0x04
184 #define ES1370_REG_UART_DATA 0x08
185 #define ES1370_REG_UART_STATUS 0x09
186 #define ES1370_REG_UART_CONTROL 0x09
187 #define ES1370_REG_UART_TEST 0x0a
188 #define ES1370_REG_MEMPAGE 0x0c
189 #define ES1370_REG_CODEC 0x10
190 #define ES1370_REG_SERIAL_CONTROL 0x20
191 #define ES1370_REG_DAC1_SCOUNT 0x24
192 #define ES1370_REG_DAC2_SCOUNT 0x28
193 #define ES1370_REG_ADC_SCOUNT 0x2c
195 #define ES1370_REG_DAC1_FRAMEADR 0xc30
196 #define ES1370_REG_DAC1_FRAMECNT 0xc34
197 #define ES1370_REG_DAC2_FRAMEADR 0xc38
198 #define ES1370_REG_DAC2_FRAMECNT 0xc3c
199 #define ES1370_REG_ADC_FRAMEADR 0xd30
200 #define ES1370_REG_ADC_FRAMECNT 0xd34
201 #define ES1370_REG_PHANTOM_FRAMEADR 0xd38
202 #define ES1370_REG_PHANTOM_FRAMECNT 0xd3c
204 #define ES1370_FMT_U8_MONO 0
205 #define ES1370_FMT_U8_STEREO 1
206 #define ES1370_FMT_S16_MONO 2
207 #define ES1370_FMT_S16_STEREO 3
208 #define ES1370_FMT_STEREO 1
209 #define ES1370_FMT_S16 2
210 #define ES1370_FMT_MASK 3
212 static const unsigned sample_size
[] = { 1, 2, 2, 4 };
213 static const unsigned sample_shift
[] = { 0, 1, 1, 2 };
215 static const unsigned dac1_samplerate
[] = { 5512, 11025, 22050, 44100 };
217 #define DAC2_SRTODIV(x) (((1411200+(x)/2)/(x))-2)
218 #define DAC2_DIVTOSR(x) (1411200/((x)+2))
220 #define CTRL_ADC_STOP 0x80000000 /* 1 = ADC stopped */
221 #define CTRL_XCTL1 0x40000000 /* electret mic bias */
222 #define CTRL_OPEN 0x20000000 /* no function, can be read and written */
223 #define CTRL_PCLKDIV 0x1fff0000 /* ADC/DAC2 clock divider */
224 #define CTRL_SH_PCLKDIV 16
225 #define CTRL_MSFMTSEL 0x00008000 /* MPEG serial data fmt: 0 = Sony, 1 = I2S */
226 #define CTRL_M_SBB 0x00004000 /* DAC2 clock: 0 = PCLKDIV, 1 = MPEG */
227 #define CTRL_WTSRSEL 0x00003000 /* DAC1 clock freq: 0=5512, 1=11025, 2=22050, 3=44100 */
228 #define CTRL_SH_WTSRSEL 12
229 #define CTRL_DAC_SYNC 0x00000800 /* 1 = DAC2 runs off DAC1 clock */
230 #define CTRL_CCB_INTRM 0x00000400 /* 1 = CCB "voice" ints enabled */
231 #define CTRL_M_CB 0x00000200 /* recording source: 0 = ADC, 1 = MPEG */
232 #define CTRL_XCTL0 0x00000100 /* 0 = Line in, 1 = Line out */
233 #define CTRL_BREQ 0x00000080 /* 1 = test mode (internal mem test) */
234 #define CTRL_DAC1_EN 0x00000040 /* enable DAC1 */
235 #define CTRL_DAC2_EN 0x00000020 /* enable DAC2 */
236 #define CTRL_ADC_EN 0x00000010 /* enable ADC */
237 #define CTRL_UART_EN 0x00000008 /* enable MIDI uart */
238 #define CTRL_JYSTK_EN 0x00000004 /* enable Joystick port (presumably at address 0x200) */
239 #define CTRL_CDC_EN 0x00000002 /* enable serial (CODEC) interface */
240 #define CTRL_SERR_DIS 0x00000001 /* 1 = disable PCI SERR signal */
242 #define STAT_INTR 0x80000000 /* wired or of all interrupt bits */
243 #define STAT_CSTAT 0x00000400 /* 1 = codec busy or codec write in progress */
244 #define STAT_CBUSY 0x00000200 /* 1 = codec busy */
245 #define STAT_CWRIP 0x00000100 /* 1 = codec write in progress */
246 #define STAT_VC 0x00000060 /* CCB int source, 0=DAC1, 1=DAC2, 2=ADC, 3=undef */
248 #define STAT_MCCB 0x00000010 /* CCB int pending */
249 #define STAT_UART 0x00000008 /* UART int pending */
250 #define STAT_DAC1 0x00000004 /* DAC1 int pending */
251 #define STAT_DAC2 0x00000002 /* DAC2 int pending */
252 #define STAT_ADC 0x00000001 /* ADC int pending */
254 #define USTAT_RXINT 0x80 /* UART rx int pending */
255 #define USTAT_TXINT 0x04 /* UART tx int pending */
256 #define USTAT_TXRDY 0x02 /* UART tx ready */
257 #define USTAT_RXRDY 0x01 /* UART rx ready */
259 #define UCTRL_RXINTEN 0x80 /* 1 = enable RX ints */
260 #define UCTRL_TXINTEN 0x60 /* TX int enable field mask */
261 #define UCTRL_ENA_TXINT 0x20 /* enable TX int */
262 #define UCTRL_CNTRL 0x03 /* control field */
263 #define UCTRL_CNTRL_SWR 0x03 /* software reset command */
265 #define SCTRL_P2ENDINC 0x00380000 /* */
266 #define SCTRL_SH_P2ENDINC 19
267 #define SCTRL_P2STINC 0x00070000 /* */
268 #define SCTRL_SH_P2STINC 16
269 #define SCTRL_R1LOOPSEL 0x00008000 /* 0 = loop mode */
270 #define SCTRL_P2LOOPSEL 0x00004000 /* 0 = loop mode */
271 #define SCTRL_P1LOOPSEL 0x00002000 /* 0 = loop mode */
272 #define SCTRL_P2PAUSE 0x00001000 /* 1 = pause mode */
273 #define SCTRL_P1PAUSE 0x00000800 /* 1 = pause mode */
274 #define SCTRL_R1INTEN 0x00000400 /* enable interrupt */
275 #define SCTRL_P2INTEN 0x00000200 /* enable interrupt */
276 #define SCTRL_P1INTEN 0x00000100 /* enable interrupt */
277 #define SCTRL_P1SCTRLD 0x00000080 /* reload sample count register for DAC1 */
278 #define SCTRL_P2DACSEN 0x00000040 /* 1 = DAC2 play back last sample when disabled */
279 #define SCTRL_R1SEB 0x00000020 /* 1 = 16bit */
280 #define SCTRL_R1SMB 0x00000010 /* 1 = stereo */
281 #define SCTRL_R1FMT 0x00000030 /* format mask */
282 #define SCTRL_SH_R1FMT 4
283 #define SCTRL_P2SEB 0x00000008 /* 1 = 16bit */
284 #define SCTRL_P2SMB 0x00000004 /* 1 = stereo */
285 #define SCTRL_P2FMT 0x0000000c /* format mask */
286 #define SCTRL_SH_P2FMT 2
287 #define SCTRL_P1SEB 0x00000002 /* 1 = 16bit */
288 #define SCTRL_P1SMB 0x00000001 /* 1 = stereo */
289 #define SCTRL_P1FMT 0x00000003 /* format mask */
290 #define SCTRL_SH_P1FMT 0
294 #define FMODE_DAC 4 /* slight misuse of mode_t */
296 /* MIDI buffer sizes */
298 #define MIDIINBUF 256
299 #define MIDIOUTBUF 256
301 #define FMODE_MIDI_SHIFT 3
302 #define FMODE_MIDI_READ (FMODE_READ << FMODE_MIDI_SHIFT)
303 #define FMODE_MIDI_WRITE (FMODE_WRITE << FMODE_MIDI_SHIFT)
305 /* --------------------------------------------------------------------- */
307 struct es1370_state
{
311 /* list of es1370 devices */
312 struct list_head devs
;
314 /* the corresponding pci_dev structure */
317 /* soundcore stuff */
323 /* hardware resources */
324 unsigned long io
; /* long for SPARC */
327 /* mixer registers; there is no HW readback */
329 unsigned short vol
[10];
332 unsigned short micpreamp
;
341 struct semaphore open_sem
;
343 wait_queue_head_t open_wait
;
351 unsigned hwptr
, swptr
;
352 unsigned total_bytes
;
354 unsigned error
; /* over/underrun */
355 wait_queue_head_t wait
;
356 /* redundant, but makes calculations easier */
359 unsigned fragsamples
;
363 unsigned endcleared
:1;
364 unsigned ossfragshift
;
366 unsigned subdivision
;
367 } dma_dac1
, dma_dac2
, dma_adc
;
371 unsigned ird
, iwr
, icnt
;
372 unsigned ord
, owr
, ocnt
;
373 wait_queue_head_t iwait
;
374 wait_queue_head_t owait
;
375 unsigned char ibuf
[MIDIINBUF
];
376 unsigned char obuf
[MIDIOUTBUF
];
380 /* --------------------------------------------------------------------- */
382 static LIST_HEAD(devs
);
385 * The following buffer is used to point the phantom write channel to,
386 * so that it cannot wreak havoc. The attribute makes sure it doesn't
387 * cross a page boundary and ensures dword alignment for the DMA engine
389 static unsigned char bugbuf
[16] __attribute__ ((aligned (16)));
391 /* --------------------------------------------------------------------- */
393 extern inline unsigned ld2(unsigned int x
)
418 /* --------------------------------------------------------------------- */
420 static void wrcodec(struct es1370_state
*s
, unsigned char idx
, unsigned char data
)
422 unsigned long tmo
= jiffies
+ HZ
/10;
425 if (!(inl(s
->io
+ES1370_REG_STATUS
) & STAT_CSTAT
)) {
426 outw((((unsigned short)idx
)<<8)|data
, s
->io
+ES1370_REG_CODEC
);
430 } while ((signed)(tmo
-jiffies
) > 0);
431 printk(KERN_ERR
"es1370: write to codec register timeout\n");
434 /* --------------------------------------------------------------------- */
436 extern inline void stop_adc(struct es1370_state
*s
)
440 spin_lock_irqsave(&s
->lock
, flags
);
441 s
->ctrl
&= ~CTRL_ADC_EN
;
442 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
443 spin_unlock_irqrestore(&s
->lock
, flags
);
446 extern inline void stop_dac1(struct es1370_state
*s
)
450 spin_lock_irqsave(&s
->lock
, flags
);
451 s
->ctrl
&= ~CTRL_DAC1_EN
;
452 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
453 spin_unlock_irqrestore(&s
->lock
, flags
);
456 extern inline void stop_dac2(struct es1370_state
*s
)
460 spin_lock_irqsave(&s
->lock
, flags
);
461 s
->ctrl
&= ~CTRL_DAC2_EN
;
462 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
463 spin_unlock_irqrestore(&s
->lock
, flags
);
466 static void start_dac1(struct es1370_state
*s
)
469 unsigned fragremain
, fshift
;
471 spin_lock_irqsave(&s
->lock
, flags
);
472 if (!(s
->ctrl
& CTRL_DAC1_EN
) && (s
->dma_dac1
.mapped
|| s
->dma_dac1
.count
> 0)
473 && s
->dma_dac1
.ready
) {
474 s
->ctrl
|= CTRL_DAC1_EN
;
475 s
->sctrl
= (s
->sctrl
& ~(SCTRL_P1LOOPSEL
| SCTRL_P1PAUSE
| SCTRL_P1SCTRLD
)) | SCTRL_P1INTEN
;
476 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
477 fragremain
= ((- s
->dma_dac1
.hwptr
) & (s
->dma_dac1
.fragsize
-1));
478 fshift
= sample_shift
[(s
->sctrl
& SCTRL_P1FMT
) >> SCTRL_SH_P1FMT
];
479 if (fragremain
< 2*fshift
)
480 fragremain
= s
->dma_dac1
.fragsize
;
481 outl((fragremain
>> fshift
) - 1, s
->io
+ES1370_REG_DAC1_SCOUNT
);
482 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
483 outl((s
->dma_dac1
.fragsize
>> fshift
) - 1, s
->io
+ES1370_REG_DAC1_SCOUNT
);
485 spin_unlock_irqrestore(&s
->lock
, flags
);
488 static void start_dac2(struct es1370_state
*s
)
491 unsigned fragremain
, fshift
;
493 spin_lock_irqsave(&s
->lock
, flags
);
494 if (!(s
->ctrl
& CTRL_DAC2_EN
) && (s
->dma_dac2
.mapped
|| s
->dma_dac2
.count
> 0)
495 && s
->dma_dac2
.ready
) {
496 s
->ctrl
|= CTRL_DAC2_EN
;
497 s
->sctrl
= (s
->sctrl
& ~(SCTRL_P2LOOPSEL
| SCTRL_P2PAUSE
| SCTRL_P2DACSEN
|
498 SCTRL_P2ENDINC
| SCTRL_P2STINC
)) | SCTRL_P2INTEN
|
499 (((s
->sctrl
& SCTRL_P2FMT
) ? 2 : 1) << SCTRL_SH_P2ENDINC
) |
500 (0 << SCTRL_SH_P2STINC
);
501 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
502 fragremain
= ((- s
->dma_dac2
.hwptr
) & (s
->dma_dac2
.fragsize
-1));
503 fshift
= sample_shift
[(s
->sctrl
& SCTRL_P2FMT
) >> SCTRL_SH_P2FMT
];
504 if (fragremain
< 2*fshift
)
505 fragremain
= s
->dma_dac2
.fragsize
;
506 outl((fragremain
>> fshift
) - 1, s
->io
+ES1370_REG_DAC2_SCOUNT
);
507 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
508 outl((s
->dma_dac2
.fragsize
>> fshift
) - 1, s
->io
+ES1370_REG_DAC2_SCOUNT
);
510 spin_unlock_irqrestore(&s
->lock
, flags
);
513 static void start_adc(struct es1370_state
*s
)
516 unsigned fragremain
, fshift
;
518 spin_lock_irqsave(&s
->lock
, flags
);
519 if (!(s
->ctrl
& CTRL_ADC_EN
) && (s
->dma_adc
.mapped
|| s
->dma_adc
.count
< (signed)(s
->dma_adc
.dmasize
- 2*s
->dma_adc
.fragsize
))
520 && s
->dma_adc
.ready
) {
521 s
->ctrl
|= CTRL_ADC_EN
;
522 s
->sctrl
= (s
->sctrl
& ~SCTRL_R1LOOPSEL
) | SCTRL_R1INTEN
;
523 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
524 fragremain
= ((- s
->dma_adc
.hwptr
) & (s
->dma_adc
.fragsize
-1));
525 fshift
= sample_shift
[(s
->sctrl
& SCTRL_R1FMT
) >> SCTRL_SH_R1FMT
];
526 if (fragremain
< 2*fshift
)
527 fragremain
= s
->dma_adc
.fragsize
;
528 outl((fragremain
>> fshift
) - 1, s
->io
+ES1370_REG_ADC_SCOUNT
);
529 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
530 outl((s
->dma_adc
.fragsize
>> fshift
) - 1, s
->io
+ES1370_REG_ADC_SCOUNT
);
532 spin_unlock_irqrestore(&s
->lock
, flags
);
535 /* --------------------------------------------------------------------- */
537 #define DMABUF_DEFAULTORDER (17-PAGE_SHIFT)
538 #define DMABUF_MINORDER 1
540 extern inline void dealloc_dmabuf(struct es1370_state
*s
, struct dmabuf
*db
)
542 struct page
*page
, *pend
;
545 /* undo marking the pages as reserved */
546 pend
= virt_to_page(db
->rawbuf
+ (PAGE_SIZE
<< db
->buforder
) - 1);
547 for (page
= virt_to_page(db
->rawbuf
); page
<= pend
; page
++)
548 mem_map_unreserve(page
);
549 pci_free_consistent(s
->dev
, PAGE_SIZE
<< db
->buforder
, db
->rawbuf
, db
->dmaaddr
);
552 db
->mapped
= db
->ready
= 0;
555 static int prog_dmabuf(struct es1370_state
*s
, struct dmabuf
*db
, unsigned rate
, unsigned fmt
, unsigned reg
)
560 struct page
*page
, *pend
;
562 db
->hwptr
= db
->swptr
= db
->total_bytes
= db
->count
= db
->error
= db
->endcleared
= 0;
564 db
->ready
= db
->mapped
= 0;
565 for (order
= DMABUF_DEFAULTORDER
; order
>= DMABUF_MINORDER
; order
--)
566 if ((db
->rawbuf
= pci_alloc_consistent(s
->dev
, PAGE_SIZE
<< order
, &db
->dmaaddr
)))
570 db
->buforder
= order
;
571 /* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
572 pend
= virt_to_page(db
->rawbuf
+ (PAGE_SIZE
<< db
->buforder
) - 1);
573 for (page
= virt_to_page(db
->rawbuf
); page
<= pend
; page
++)
574 mem_map_reserve(page
);
576 fmt
&= ES1370_FMT_MASK
;
577 bytepersec
= rate
<< sample_shift
[fmt
];
578 bufs
= PAGE_SIZE
<< db
->buforder
;
579 if (db
->ossfragshift
) {
580 if ((1000 << db
->ossfragshift
) < bytepersec
)
581 db
->fragshift
= ld2(bytepersec
/1000);
583 db
->fragshift
= db
->ossfragshift
;
585 db
->fragshift
= ld2(bytepersec
/100/(db
->subdivision
? db
->subdivision
: 1));
586 if (db
->fragshift
< 3)
589 db
->numfrag
= bufs
>> db
->fragshift
;
590 while (db
->numfrag
< 4 && db
->fragshift
> 3) {
592 db
->numfrag
= bufs
>> db
->fragshift
;
594 db
->fragsize
= 1 << db
->fragshift
;
595 if (db
->ossmaxfrags
>= 4 && db
->ossmaxfrags
< db
->numfrag
)
596 db
->numfrag
= db
->ossmaxfrags
;
597 db
->fragsamples
= db
->fragsize
>> sample_shift
[fmt
];
598 db
->dmasize
= db
->numfrag
<< db
->fragshift
;
599 memset(db
->rawbuf
, (fmt
& ES1370_FMT_S16
) ? 0 : 0x80, db
->dmasize
);
600 outl((reg
>> 8) & 15, s
->io
+ES1370_REG_MEMPAGE
);
601 outl(db
->dmaaddr
, s
->io
+(reg
& 0xff));
602 outl((db
->dmasize
>> 2)-1, s
->io
+((reg
+ 4) & 0xff));
607 extern inline int prog_dmabuf_adc(struct es1370_state
*s
)
610 return prog_dmabuf(s
, &s
->dma_adc
, DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
),
611 (s
->sctrl
>> SCTRL_SH_R1FMT
) & ES1370_FMT_MASK
, ES1370_REG_ADC_FRAMEADR
);
614 extern inline int prog_dmabuf_dac2(struct es1370_state
*s
)
617 return prog_dmabuf(s
, &s
->dma_dac2
, DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
),
618 (s
->sctrl
>> SCTRL_SH_P2FMT
) & ES1370_FMT_MASK
, ES1370_REG_DAC2_FRAMEADR
);
621 extern inline int prog_dmabuf_dac1(struct es1370_state
*s
)
624 return prog_dmabuf(s
, &s
->dma_dac1
, dac1_samplerate
[(s
->ctrl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
],
625 (s
->sctrl
>> SCTRL_SH_P1FMT
) & ES1370_FMT_MASK
, ES1370_REG_DAC1_FRAMEADR
);
628 extern inline unsigned get_hwptr(struct es1370_state
*s
, struct dmabuf
*db
, unsigned reg
)
630 unsigned hwptr
, diff
;
632 outl((reg
>> 8) & 15, s
->io
+ES1370_REG_MEMPAGE
);
633 hwptr
= (inl(s
->io
+(reg
& 0xff)) >> 14) & 0x3fffc;
634 diff
= (db
->dmasize
+ hwptr
- db
->hwptr
) % db
->dmasize
;
639 extern inline void clear_advance(void *buf
, unsigned bsize
, unsigned bptr
, unsigned len
, unsigned char c
)
641 if (bptr
+ len
> bsize
) {
642 unsigned x
= bsize
- bptr
;
643 memset(((char *)buf
) + bptr
, c
, x
);
647 memset(((char *)buf
) + bptr
, c
, len
);
650 /* call with spinlock held! */
651 static void es1370_update_ptr(struct es1370_state
*s
)
655 /* update ADC pointer */
656 if (s
->ctrl
& CTRL_ADC_EN
) {
657 diff
= get_hwptr(s
, &s
->dma_adc
, ES1370_REG_ADC_FRAMECNT
);
658 s
->dma_adc
.total_bytes
+= diff
;
659 s
->dma_adc
.count
+= diff
;
660 if (s
->dma_adc
.count
>= (signed)s
->dma_adc
.fragsize
)
661 wake_up(&s
->dma_adc
.wait
);
662 if (!s
->dma_adc
.mapped
) {
663 if (s
->dma_adc
.count
> (signed)(s
->dma_adc
.dmasize
- ((3 * s
->dma_adc
.fragsize
) >> 1))) {
664 s
->ctrl
&= ~CTRL_ADC_EN
;
665 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
670 /* update DAC1 pointer */
671 if (s
->ctrl
& CTRL_DAC1_EN
) {
672 diff
= get_hwptr(s
, &s
->dma_dac1
, ES1370_REG_DAC1_FRAMECNT
);
673 s
->dma_dac1
.total_bytes
+= diff
;
674 if (s
->dma_dac1
.mapped
) {
675 s
->dma_dac1
.count
+= diff
;
676 if (s
->dma_dac1
.count
>= (signed)s
->dma_dac1
.fragsize
)
677 wake_up(&s
->dma_dac1
.wait
);
679 s
->dma_dac1
.count
-= diff
;
680 if (s
->dma_dac1
.count
<= 0) {
681 s
->ctrl
&= ~CTRL_DAC1_EN
;
682 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
684 } else if (s
->dma_dac1
.count
<= (signed)s
->dma_dac1
.fragsize
&& !s
->dma_dac1
.endcleared
) {
685 clear_advance(s
->dma_dac1
.rawbuf
, s
->dma_dac1
.dmasize
, s
->dma_dac1
.swptr
,
686 s
->dma_dac1
.fragsize
, (s
->sctrl
& SCTRL_P1SEB
) ? 0 : 0x80);
687 s
->dma_dac1
.endcleared
= 1;
689 if (s
->dma_dac1
.count
+ (signed)s
->dma_dac1
.fragsize
<= (signed)s
->dma_dac1
.dmasize
)
690 wake_up(&s
->dma_dac1
.wait
);
693 /* update DAC2 pointer */
694 if (s
->ctrl
& CTRL_DAC2_EN
) {
695 diff
= get_hwptr(s
, &s
->dma_dac2
, ES1370_REG_DAC2_FRAMECNT
);
696 s
->dma_dac2
.total_bytes
+= diff
;
697 if (s
->dma_dac2
.mapped
) {
698 s
->dma_dac2
.count
+= diff
;
699 if (s
->dma_dac2
.count
>= (signed)s
->dma_dac2
.fragsize
)
700 wake_up(&s
->dma_dac2
.wait
);
702 s
->dma_dac2
.count
-= diff
;
703 if (s
->dma_dac2
.count
<= 0) {
704 s
->ctrl
&= ~CTRL_DAC2_EN
;
705 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
707 } else if (s
->dma_dac2
.count
<= (signed)s
->dma_dac2
.fragsize
&& !s
->dma_dac2
.endcleared
) {
708 clear_advance(s
->dma_dac2
.rawbuf
, s
->dma_dac2
.dmasize
, s
->dma_dac2
.swptr
,
709 s
->dma_dac2
.fragsize
, (s
->sctrl
& SCTRL_P2SEB
) ? 0 : 0x80);
710 s
->dma_dac2
.endcleared
= 1;
712 if (s
->dma_dac2
.count
+ (signed)s
->dma_dac2
.fragsize
<= (signed)s
->dma_dac2
.dmasize
)
713 wake_up(&s
->dma_dac2
.wait
);
718 /* hold spinlock for the following! */
719 static void es1370_handle_midi(struct es1370_state
*s
)
724 if (!(s
->ctrl
& CTRL_UART_EN
))
727 while (inb(s
->io
+ES1370_REG_UART_STATUS
) & USTAT_RXRDY
) {
728 ch
= inb(s
->io
+ES1370_REG_UART_DATA
);
729 if (s
->midi
.icnt
< MIDIINBUF
) {
730 s
->midi
.ibuf
[s
->midi
.iwr
] = ch
;
731 s
->midi
.iwr
= (s
->midi
.iwr
+ 1) % MIDIINBUF
;
737 wake_up(&s
->midi
.iwait
);
739 while ((inb(s
->io
+ES1370_REG_UART_STATUS
) & USTAT_TXRDY
) && s
->midi
.ocnt
> 0) {
740 outb(s
->midi
.obuf
[s
->midi
.ord
], s
->io
+ES1370_REG_UART_DATA
);
741 s
->midi
.ord
= (s
->midi
.ord
+ 1) % MIDIOUTBUF
;
743 if (s
->midi
.ocnt
< MIDIOUTBUF
-16)
747 wake_up(&s
->midi
.owait
);
748 outb((s
->midi
.ocnt
> 0) ? UCTRL_RXINTEN
| UCTRL_ENA_TXINT
: UCTRL_RXINTEN
, s
->io
+ES1370_REG_UART_CONTROL
);
751 static void es1370_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
753 struct es1370_state
*s
= (struct es1370_state
*)dev_id
;
754 unsigned int intsrc
, sctl
;
756 /* fastpath out, to ease interrupt sharing */
757 intsrc
= inl(s
->io
+ES1370_REG_STATUS
);
758 if (!(intsrc
& 0x80000000))
761 /* clear audio interrupts first */
763 if (intsrc
& STAT_ADC
)
764 sctl
&= ~SCTRL_R1INTEN
;
765 if (intsrc
& STAT_DAC1
)
766 sctl
&= ~SCTRL_P1INTEN
;
767 if (intsrc
& STAT_DAC2
)
768 sctl
&= ~SCTRL_P2INTEN
;
769 outl(sctl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
770 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
771 es1370_update_ptr(s
);
772 es1370_handle_midi(s
);
773 spin_unlock(&s
->lock
);
776 /* --------------------------------------------------------------------- */
778 static const char invalid_magic
[] = KERN_CRIT
"es1370: invalid magic value\n";
780 #define VALIDATE_STATE(s) \
782 if (!(s) || (s)->magic != ES1370_MAGIC) { \
783 printk(invalid_magic); \
788 /* --------------------------------------------------------------------- */
790 static const struct {
797 } mixtable
[SOUND_MIXER_NRDEVICES
] = {
798 [SOUND_MIXER_VOLUME
] = { 0, 0x0, 0x1, 1, 0x0000, 1 }, /* master */
799 [SOUND_MIXER_PCM
] = { 1, 0x2, 0x3, 1, 0x0400, 1 }, /* voice */
800 [SOUND_MIXER_SYNTH
] = { 2, 0x4, 0x5, 1, 0x0060, 1 }, /* FM */
801 [SOUND_MIXER_CD
] = { 3, 0x6, 0x7, 1, 0x0006, 1 }, /* CD */
802 [SOUND_MIXER_LINE
] = { 4, 0x8, 0x9, 1, 0x0018, 1 }, /* Line */
803 [SOUND_MIXER_LINE1
] = { 5, 0xa, 0xb, 1, 0x1800, 1 }, /* AUX */
804 [SOUND_MIXER_LINE2
] = { 6, 0xc, 0x0, 0, 0x0100, 1 }, /* Mono1 */
805 [SOUND_MIXER_LINE3
] = { 7, 0xd, 0x0, 0, 0x0200, 1 }, /* Mono2 */
806 [SOUND_MIXER_MIC
] = { 8, 0xe, 0x0, 0, 0x0001, 1 }, /* Mic */
807 [SOUND_MIXER_OGAIN
] = { 9, 0xf, 0x0, 0, 0x0000, 1 } /* mono out */
810 static void set_recsrc(struct es1370_state
*s
, unsigned int val
)
814 for (j
= i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
815 if (!(val
& (1 << i
)))
817 if (!mixtable
[i
].recmask
) {
821 j
|= mixtable
[i
].recmask
;
824 wrcodec(s
, 0x12, j
& 0xd5);
825 wrcodec(s
, 0x13, j
& 0xaa);
826 wrcodec(s
, 0x14, (j
>> 8) & 0x17);
827 wrcodec(s
, 0x15, (j
>> 8) & 0x0f);
828 i
= (j
& 0x37f) | ((j
<< 1) & 0x3000) | 0xc60;
830 i
&= 0xff60; /* mute record and line monitor */
833 wrcodec(s
, 0x11, i
>> 8);
836 static int mixer_ioctl(struct es1370_state
*s
, unsigned int cmd
, unsigned long arg
)
840 unsigned char l
, r
, rl
, rr
;
843 if (cmd
== SOUND_MIXER_PRIVATE1
) {
844 /* enable/disable/query mixer preamp */
845 if (get_user(val
, (int *)arg
))
848 s
->mix
.micpreamp
= !!val
;
849 wrcodec(s
, 0x19, s
->mix
.micpreamp
);
851 return put_user(s
->mix
.micpreamp
, (int *)arg
);
853 if (cmd
== SOUND_MIXER_PRIVATE2
) {
854 /* enable/disable/query use of linein as second lineout */
855 if (get_user(val
, (int *)arg
))
858 spin_lock_irqsave(&s
->lock
, flags
);
860 s
->ctrl
|= CTRL_XCTL0
;
862 s
->ctrl
&= ~CTRL_XCTL0
;
863 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
864 spin_unlock_irqrestore(&s
->lock
, flags
);
866 return put_user((s
->ctrl
& CTRL_XCTL0
) ? 1 : 0, (int *)arg
);
868 if (cmd
== SOUND_MIXER_PRIVATE3
) {
869 /* enable/disable/query microphone impedance setting */
870 if (get_user(val
, (int *)arg
))
873 spin_lock_irqsave(&s
->lock
, flags
);
875 s
->ctrl
|= CTRL_XCTL1
;
877 s
->ctrl
&= ~CTRL_XCTL1
;
878 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
879 spin_unlock_irqrestore(&s
->lock
, flags
);
881 return put_user((s
->ctrl
& CTRL_XCTL1
) ? 1 : 0, (int *)arg
);
883 if (cmd
== SOUND_MIXER_INFO
) {
885 strncpy(info
.id
, "ES1370", sizeof(info
.id
));
886 strncpy(info
.name
, "Ensoniq ES1370", sizeof(info
.name
));
887 info
.modify_counter
= s
->mix
.modcnt
;
888 if (copy_to_user((void *)arg
, &info
, sizeof(info
)))
892 if (cmd
== SOUND_OLD_MIXER_INFO
) {
893 _old_mixer_info info
;
894 strncpy(info
.id
, "ES1370", sizeof(info
.id
));
895 strncpy(info
.name
, "Ensoniq ES1370", sizeof(info
.name
));
896 if (copy_to_user((void *)arg
, &info
, sizeof(info
)))
900 if (cmd
== OSS_GETVERSION
)
901 return put_user(SOUND_VERSION
, (int *)arg
);
902 if (_IOC_TYPE(cmd
) != 'M' || _SIOC_SIZE(cmd
) != sizeof(int))
904 if (_SIOC_DIR(cmd
) == _SIOC_READ
) {
905 switch (_IOC_NR(cmd
)) {
906 case SOUND_MIXER_RECSRC
: /* Arg contains a bit for each recording source */
907 return put_user(s
->mix
.recsrc
, (int *)arg
);
909 case SOUND_MIXER_DEVMASK
: /* Arg contains a bit for each supported device */
910 val
= SOUND_MASK_IMIX
;
911 for (i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++)
912 if (mixtable
[i
].avail
)
914 return put_user(val
, (int *)arg
);
916 case SOUND_MIXER_RECMASK
: /* Arg contains a bit for each supported recording source */
917 for (val
= i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++)
918 if (mixtable
[i
].recmask
)
920 return put_user(val
, (int *)arg
);
922 case SOUND_MIXER_STEREODEVS
: /* Mixer channels supporting stereo */
923 for (val
= i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++)
924 if (mixtable
[i
].stereo
)
926 return put_user(val
, (int *)arg
);
928 case SOUND_MIXER_CAPS
:
929 return put_user(0, (int *)arg
);
931 case SOUND_MIXER_IMIX
:
932 return put_user(s
->mix
.imix
, (int *)arg
);
936 if (i
>= SOUND_MIXER_NRDEVICES
|| !mixtable
[i
].avail
)
938 return put_user(s
->mix
.vol
[mixtable
[i
].volidx
], (int *)arg
);
941 if (_SIOC_DIR(cmd
) != (_SIOC_READ
|_SIOC_WRITE
))
944 switch (_IOC_NR(cmd
)) {
946 case SOUND_MIXER_IMIX
:
947 if (get_user(s
->mix
.imix
, (int *)arg
))
949 set_recsrc(s
, s
->mix
.recsrc
);
952 case SOUND_MIXER_RECSRC
: /* Arg contains a bit for each recording source */
953 if (get_user(val
, (int *)arg
))
960 if (i
>= SOUND_MIXER_NRDEVICES
|| !mixtable
[i
].avail
)
962 if (get_user(val
, (int *)arg
))
967 if (mixtable
[i
].stereo
) {
968 r
= (val
>> 8) & 0xff;
975 rl
= 31 - ((l
- 7) / 3);
976 l
= (31 - rl
) * 3 + 7;
982 rr
= 31 - ((r
- 7) / 3);
983 r
= (31 - rr
) * 3 + 7;
985 wrcodec(s
, mixtable
[i
].right
, rr
);
987 if (mixtable
[i
].left
== 15) {
992 rl
= 7 - ((l
- 2) / 14);
993 r
= l
= (7 - rl
) * 14 + 2;
1000 rl
= 31 - ((l
- 7) / 3);
1001 r
= l
= (31 - rl
) * 3 + 7;
1005 wrcodec(s
, mixtable
[i
].left
, rl
);
1006 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1007 s
->mix
.vol
[mixtable
[i
].volidx
] = ((unsigned int)r
<< 8) | l
;
1009 s
->mix
.vol
[mixtable
[i
].volidx
] = val
;
1011 return put_user(s
->mix
.vol
[mixtable
[i
].volidx
], (int *)arg
);
1015 /* --------------------------------------------------------------------- */
1017 static loff_t
es1370_llseek(struct file
*file
, loff_t offset
, int origin
)
1022 /* --------------------------------------------------------------------- */
1024 static int es1370_open_mixdev(struct inode
*inode
, struct file
*file
)
1026 int minor
= MINOR(inode
->i_rdev
);
1027 struct list_head
*list
;
1028 struct es1370_state
*s
;
1030 for (list
= devs
.next
; ; list
= list
->next
) {
1033 s
= list_entry(list
, struct es1370_state
, devs
);
1034 if (s
->dev_mixer
== minor
)
1038 file
->private_data
= s
;
1042 static int es1370_release_mixdev(struct inode
*inode
, struct file
*file
)
1044 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1050 static int es1370_ioctl_mixdev(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1052 return mixer_ioctl((struct es1370_state
*)file
->private_data
, cmd
, arg
);
1055 static /*const*/ struct file_operations es1370_mixer_fops
= {
1057 llseek
: es1370_llseek
,
1058 ioctl
: es1370_ioctl_mixdev
,
1059 open
: es1370_open_mixdev
,
1060 release
: es1370_release_mixdev
,
1063 /* --------------------------------------------------------------------- */
1065 static int drain_dac1(struct es1370_state
*s
, int nonblock
)
1067 DECLARE_WAITQUEUE(wait
, current
);
1068 unsigned long flags
;
1071 if (s
->dma_dac1
.mapped
|| !s
->dma_dac1
.ready
)
1073 add_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1075 __set_current_state(TASK_INTERRUPTIBLE
);
1076 spin_lock_irqsave(&s
->lock
, flags
);
1077 count
= s
->dma_dac1
.count
;
1078 spin_unlock_irqrestore(&s
->lock
, flags
);
1081 if (signal_pending(current
))
1084 remove_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1085 set_current_state(TASK_RUNNING
);
1088 tmo
= 3 * HZ
* (count
+ s
->dma_dac1
.fragsize
) / 2
1089 / dac1_samplerate
[(s
->ctrl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
];
1090 tmo
>>= sample_shift
[(s
->sctrl
& SCTRL_P1FMT
) >> SCTRL_SH_P1FMT
];
1091 if (!schedule_timeout(tmo
+ 1))
1092 DBG(printk(KERN_DEBUG
"es1370: dma timed out??\n");)
1094 remove_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1095 set_current_state(TASK_RUNNING
);
1096 if (signal_pending(current
))
1097 return -ERESTARTSYS
;
1101 static int drain_dac2(struct es1370_state
*s
, int nonblock
)
1103 DECLARE_WAITQUEUE(wait
, current
);
1104 unsigned long flags
;
1107 if (s
->dma_dac2
.mapped
|| !s
->dma_dac2
.ready
)
1109 add_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1111 __set_current_state(TASK_INTERRUPTIBLE
);
1112 spin_lock_irqsave(&s
->lock
, flags
);
1113 count
= s
->dma_dac2
.count
;
1114 spin_unlock_irqrestore(&s
->lock
, flags
);
1117 if (signal_pending(current
))
1120 remove_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1121 set_current_state(TASK_RUNNING
);
1124 tmo
= 3 * HZ
* (count
+ s
->dma_dac2
.fragsize
) / 2
1125 / DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
);
1126 tmo
>>= sample_shift
[(s
->sctrl
& SCTRL_P2FMT
) >> SCTRL_SH_P2FMT
];
1127 if (!schedule_timeout(tmo
+ 1))
1128 DBG(printk(KERN_DEBUG
"es1370: dma timed out??\n");)
1130 remove_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1131 set_current_state(TASK_RUNNING
);
1132 if (signal_pending(current
))
1133 return -ERESTARTSYS
;
1137 /* --------------------------------------------------------------------- */
1139 static ssize_t
es1370_read(struct file
*file
, char *buffer
, size_t count
, loff_t
*ppos
)
1141 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1142 DECLARE_WAITQUEUE(wait
, current
);
1144 unsigned long flags
;
1149 if (ppos
!= &file
->f_pos
)
1151 if (s
->dma_adc
.mapped
)
1153 if (!s
->dma_adc
.ready
&& (ret
= prog_dmabuf_adc(s
)))
1155 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
1158 add_wait_queue(&s
->dma_adc
.wait
, &wait
);
1160 spin_lock_irqsave(&s
->lock
, flags
);
1161 swptr
= s
->dma_adc
.swptr
;
1162 cnt
= s
->dma_adc
.dmasize
-swptr
;
1163 if (s
->dma_adc
.count
< cnt
)
1164 cnt
= s
->dma_adc
.count
;
1166 __set_current_state(TASK_INTERRUPTIBLE
);
1167 spin_unlock_irqrestore(&s
->lock
, flags
);
1172 if (file
->f_flags
& O_NONBLOCK
) {
1178 if (signal_pending(current
)) {
1185 if (copy_to_user(buffer
, s
->dma_adc
.rawbuf
+ swptr
, cnt
)) {
1190 swptr
= (swptr
+ cnt
) % s
->dma_adc
.dmasize
;
1191 spin_lock_irqsave(&s
->lock
, flags
);
1192 s
->dma_adc
.swptr
= swptr
;
1193 s
->dma_adc
.count
-= cnt
;
1194 spin_unlock_irqrestore(&s
->lock
, flags
);
1200 remove_wait_queue(&s
->dma_adc
.wait
, &wait
);
1201 set_current_state(TASK_RUNNING
);
1205 static ssize_t
es1370_write(struct file
*file
, const char *buffer
, size_t count
, loff_t
*ppos
)
1207 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1208 DECLARE_WAITQUEUE(wait
, current
);
1210 unsigned long flags
;
1215 if (ppos
!= &file
->f_pos
)
1217 if (s
->dma_dac2
.mapped
)
1219 if (!s
->dma_dac2
.ready
&& (ret
= prog_dmabuf_dac2(s
)))
1221 if (!access_ok(VERIFY_READ
, buffer
, count
))
1224 add_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1226 spin_lock_irqsave(&s
->lock
, flags
);
1227 if (s
->dma_dac2
.count
< 0) {
1228 s
->dma_dac2
.count
= 0;
1229 s
->dma_dac2
.swptr
= s
->dma_dac2
.hwptr
;
1231 swptr
= s
->dma_dac2
.swptr
;
1232 cnt
= s
->dma_dac2
.dmasize
-swptr
;
1233 if (s
->dma_dac2
.count
+ cnt
> s
->dma_dac2
.dmasize
)
1234 cnt
= s
->dma_dac2
.dmasize
- s
->dma_dac2
.count
;
1236 __set_current_state(TASK_INTERRUPTIBLE
);
1237 spin_unlock_irqrestore(&s
->lock
, flags
);
1242 if (file
->f_flags
& O_NONBLOCK
) {
1248 if (signal_pending(current
)) {
1255 if (copy_from_user(s
->dma_dac2
.rawbuf
+ swptr
, buffer
, cnt
)) {
1260 swptr
= (swptr
+ cnt
) % s
->dma_dac2
.dmasize
;
1261 spin_lock_irqsave(&s
->lock
, flags
);
1262 s
->dma_dac2
.swptr
= swptr
;
1263 s
->dma_dac2
.count
+= cnt
;
1264 s
->dma_dac2
.endcleared
= 0;
1265 spin_unlock_irqrestore(&s
->lock
, flags
);
1271 remove_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1272 set_current_state(TASK_RUNNING
);
1276 /* No kernel lock - we have our own spinlock */
1277 static unsigned int es1370_poll(struct file
*file
, struct poll_table_struct
*wait
)
1279 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1280 unsigned long flags
;
1281 unsigned int mask
= 0;
1284 if (file
->f_mode
& FMODE_WRITE
) {
1285 if (!s
->dma_dac2
.ready
&& prog_dmabuf_dac2(s
))
1287 poll_wait(file
, &s
->dma_dac2
.wait
, wait
);
1289 if (file
->f_mode
& FMODE_READ
) {
1290 if (!s
->dma_adc
.ready
&& prog_dmabuf_adc(s
))
1292 poll_wait(file
, &s
->dma_adc
.wait
, wait
);
1294 spin_lock_irqsave(&s
->lock
, flags
);
1295 es1370_update_ptr(s
);
1296 if (file
->f_mode
& FMODE_READ
) {
1297 if (s
->dma_adc
.count
>= (signed)s
->dma_adc
.fragsize
)
1298 mask
|= POLLIN
| POLLRDNORM
;
1300 if (file
->f_mode
& FMODE_WRITE
) {
1301 if (s
->dma_dac2
.mapped
) {
1302 if (s
->dma_dac2
.count
>= (signed)s
->dma_dac2
.fragsize
)
1303 mask
|= POLLOUT
| POLLWRNORM
;
1305 if ((signed)s
->dma_dac2
.dmasize
>= s
->dma_dac2
.count
+ (signed)s
->dma_dac2
.fragsize
)
1306 mask
|= POLLOUT
| POLLWRNORM
;
1309 spin_unlock_irqrestore(&s
->lock
, flags
);
1313 static int es1370_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1315 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1322 if (vma
->vm_flags
& VM_WRITE
) {
1323 if ((ret
= prog_dmabuf_dac2(s
)) != 0) {
1328 } else if (vma
->vm_flags
& VM_READ
) {
1329 if ((ret
= prog_dmabuf_adc(s
)) != 0) {
1338 if (vma
->vm_pgoff
!= 0) {
1342 size
= vma
->vm_end
- vma
->vm_start
;
1343 if (size
> (PAGE_SIZE
<< db
->buforder
)) {
1347 if (remap_page_range(vma
->vm_start
, virt_to_phys(db
->rawbuf
), size
, vma
->vm_page_prot
)) {
1356 static int es1370_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1358 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1359 unsigned long flags
;
1360 audio_buf_info abinfo
;
1363 int val
, mapped
, ret
;
1366 mapped
= ((file
->f_mode
& FMODE_WRITE
) && s
->dma_dac2
.mapped
) ||
1367 ((file
->f_mode
& FMODE_READ
) && s
->dma_adc
.mapped
);
1369 case OSS_GETVERSION
:
1370 return put_user(SOUND_VERSION
, (int *)arg
);
1372 case SNDCTL_DSP_SYNC
:
1373 if (file
->f_mode
& FMODE_WRITE
)
1374 return drain_dac2(s
, 0/*file->f_flags & O_NONBLOCK*/);
1377 case SNDCTL_DSP_SETDUPLEX
:
1380 case SNDCTL_DSP_GETCAPS
:
1381 return put_user(DSP_CAP_DUPLEX
| DSP_CAP_REALTIME
| DSP_CAP_TRIGGER
| DSP_CAP_MMAP
, (int *)arg
);
1383 case SNDCTL_DSP_RESET
:
1384 if (file
->f_mode
& FMODE_WRITE
) {
1387 s
->dma_dac2
.swptr
= s
->dma_dac2
.hwptr
= s
->dma_dac2
.count
= s
->dma_dac2
.total_bytes
= 0;
1389 if (file
->f_mode
& FMODE_READ
) {
1392 s
->dma_adc
.swptr
= s
->dma_adc
.hwptr
= s
->dma_adc
.count
= s
->dma_adc
.total_bytes
= 0;
1396 case SNDCTL_DSP_SPEED
:
1397 if (get_user(val
, (int *)arg
))
1400 if (s
->open_mode
& (~file
->f_mode
) & (FMODE_READ
|FMODE_WRITE
))
1408 s
->dma_adc
.ready
= s
->dma_dac2
.ready
= 0;
1409 spin_lock_irqsave(&s
->lock
, flags
);
1410 s
->ctrl
= (s
->ctrl
& ~CTRL_PCLKDIV
) | (DAC2_SRTODIV(val
) << CTRL_SH_PCLKDIV
);
1411 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
1412 spin_unlock_irqrestore(&s
->lock
, flags
);
1414 return put_user(DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
), (int *)arg
);
1416 case SNDCTL_DSP_STEREO
:
1417 if (get_user(val
, (int *)arg
))
1419 if (file
->f_mode
& FMODE_READ
) {
1421 s
->dma_adc
.ready
= 0;
1422 spin_lock_irqsave(&s
->lock
, flags
);
1424 s
->sctrl
|= SCTRL_R1SMB
;
1426 s
->sctrl
&= ~SCTRL_R1SMB
;
1427 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1428 spin_unlock_irqrestore(&s
->lock
, flags
);
1430 if (file
->f_mode
& FMODE_WRITE
) {
1432 s
->dma_dac2
.ready
= 0;
1433 spin_lock_irqsave(&s
->lock
, flags
);
1435 s
->sctrl
|= SCTRL_P2SMB
;
1437 s
->sctrl
&= ~SCTRL_P2SMB
;
1438 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1439 spin_unlock_irqrestore(&s
->lock
, flags
);
1443 case SNDCTL_DSP_CHANNELS
:
1444 if (get_user(val
, (int *)arg
))
1447 if (file
->f_mode
& FMODE_READ
) {
1449 s
->dma_adc
.ready
= 0;
1450 spin_lock_irqsave(&s
->lock
, flags
);
1452 s
->sctrl
|= SCTRL_R1SMB
;
1454 s
->sctrl
&= ~SCTRL_R1SMB
;
1455 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1456 spin_unlock_irqrestore(&s
->lock
, flags
);
1458 if (file
->f_mode
& FMODE_WRITE
) {
1460 s
->dma_dac2
.ready
= 0;
1461 spin_lock_irqsave(&s
->lock
, flags
);
1463 s
->sctrl
|= SCTRL_P2SMB
;
1465 s
->sctrl
&= ~SCTRL_P2SMB
;
1466 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1467 spin_unlock_irqrestore(&s
->lock
, flags
);
1470 return put_user((s
->sctrl
& ((file
->f_mode
& FMODE_READ
) ? SCTRL_R1SMB
: SCTRL_P2SMB
)) ? 2 : 1, (int *)arg
);
1472 case SNDCTL_DSP_GETFMTS
: /* Returns a mask */
1473 return put_user(AFMT_S16_LE
|AFMT_U8
, (int *)arg
);
1475 case SNDCTL_DSP_SETFMT
: /* Selects ONE fmt*/
1476 if (get_user(val
, (int *)arg
))
1478 if (val
!= AFMT_QUERY
) {
1479 if (file
->f_mode
& FMODE_READ
) {
1481 s
->dma_adc
.ready
= 0;
1482 spin_lock_irqsave(&s
->lock
, flags
);
1483 if (val
== AFMT_S16_LE
)
1484 s
->sctrl
|= SCTRL_R1SEB
;
1486 s
->sctrl
&= ~SCTRL_R1SEB
;
1487 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1488 spin_unlock_irqrestore(&s
->lock
, flags
);
1490 if (file
->f_mode
& FMODE_WRITE
) {
1492 s
->dma_dac2
.ready
= 0;
1493 spin_lock_irqsave(&s
->lock
, flags
);
1494 if (val
== AFMT_S16_LE
)
1495 s
->sctrl
|= SCTRL_P2SEB
;
1497 s
->sctrl
&= ~SCTRL_P2SEB
;
1498 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1499 spin_unlock_irqrestore(&s
->lock
, flags
);
1502 return put_user((s
->sctrl
& ((file
->f_mode
& FMODE_READ
) ? SCTRL_R1SEB
: SCTRL_P2SEB
)) ?
1503 AFMT_S16_LE
: AFMT_U8
, (int *)arg
);
1505 case SNDCTL_DSP_POST
:
1508 case SNDCTL_DSP_GETTRIGGER
:
1510 if (file
->f_mode
& FMODE_READ
&& s
->ctrl
& CTRL_ADC_EN
)
1511 val
|= PCM_ENABLE_INPUT
;
1512 if (file
->f_mode
& FMODE_WRITE
&& s
->ctrl
& CTRL_DAC2_EN
)
1513 val
|= PCM_ENABLE_OUTPUT
;
1514 return put_user(val
, (int *)arg
);
1516 case SNDCTL_DSP_SETTRIGGER
:
1517 if (get_user(val
, (int *)arg
))
1519 if (file
->f_mode
& FMODE_READ
) {
1520 if (val
& PCM_ENABLE_INPUT
) {
1521 if (!s
->dma_adc
.ready
&& (ret
= prog_dmabuf_adc(s
)))
1527 if (file
->f_mode
& FMODE_WRITE
) {
1528 if (val
& PCM_ENABLE_OUTPUT
) {
1529 if (!s
->dma_dac2
.ready
&& (ret
= prog_dmabuf_dac2(s
)))
1537 case SNDCTL_DSP_GETOSPACE
:
1538 if (!(file
->f_mode
& FMODE_WRITE
))
1540 if (!s
->dma_dac2
.ready
&& (ret
= prog_dmabuf_dac2(s
)))
1542 spin_lock_irqsave(&s
->lock
, flags
);
1543 es1370_update_ptr(s
);
1544 abinfo
.fragsize
= s
->dma_dac2
.fragsize
;
1545 count
= s
->dma_dac2
.count
;
1548 abinfo
.bytes
= s
->dma_dac2
.dmasize
- count
;
1549 abinfo
.fragstotal
= s
->dma_dac2
.numfrag
;
1550 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_dac2
.fragshift
;
1551 spin_unlock_irqrestore(&s
->lock
, flags
);
1552 return copy_to_user((void *)arg
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1554 case SNDCTL_DSP_GETISPACE
:
1555 if (!(file
->f_mode
& FMODE_READ
))
1557 if (!s
->dma_adc
.ready
&& (ret
= prog_dmabuf_adc(s
)))
1559 spin_lock_irqsave(&s
->lock
, flags
);
1560 es1370_update_ptr(s
);
1561 abinfo
.fragsize
= s
->dma_adc
.fragsize
;
1562 count
= s
->dma_adc
.count
;
1565 abinfo
.bytes
= count
;
1566 abinfo
.fragstotal
= s
->dma_adc
.numfrag
;
1567 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_adc
.fragshift
;
1568 spin_unlock_irqrestore(&s
->lock
, flags
);
1569 return copy_to_user((void *)arg
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1571 case SNDCTL_DSP_NONBLOCK
:
1572 file
->f_flags
|= O_NONBLOCK
;
1575 case SNDCTL_DSP_GETODELAY
:
1576 if (!(file
->f_mode
& FMODE_WRITE
))
1578 if (!s
->dma_dac2
.ready
&& (ret
= prog_dmabuf_dac2(s
)))
1580 spin_lock_irqsave(&s
->lock
, flags
);
1581 es1370_update_ptr(s
);
1582 count
= s
->dma_dac2
.count
;
1583 spin_unlock_irqrestore(&s
->lock
, flags
);
1586 return put_user(count
, (int *)arg
);
1588 case SNDCTL_DSP_GETIPTR
:
1589 if (!(file
->f_mode
& FMODE_READ
))
1591 if (!s
->dma_adc
.ready
&& (ret
= prog_dmabuf_adc(s
)))
1593 spin_lock_irqsave(&s
->lock
, flags
);
1594 es1370_update_ptr(s
);
1595 cinfo
.bytes
= s
->dma_adc
.total_bytes
;
1596 count
= s
->dma_adc
.count
;
1599 cinfo
.blocks
= count
>> s
->dma_adc
.fragshift
;
1600 cinfo
.ptr
= s
->dma_adc
.hwptr
;
1601 if (s
->dma_adc
.mapped
)
1602 s
->dma_adc
.count
&= s
->dma_adc
.fragsize
-1;
1603 spin_unlock_irqrestore(&s
->lock
, flags
);
1604 return copy_to_user((void *)arg
, &cinfo
, sizeof(cinfo
));
1606 case SNDCTL_DSP_GETOPTR
:
1607 if (!(file
->f_mode
& FMODE_WRITE
))
1609 if (!s
->dma_dac2
.ready
&& (ret
= prog_dmabuf_dac2(s
)))
1611 spin_lock_irqsave(&s
->lock
, flags
);
1612 es1370_update_ptr(s
);
1613 cinfo
.bytes
= s
->dma_dac2
.total_bytes
;
1614 count
= s
->dma_dac2
.count
;
1617 cinfo
.blocks
= count
>> s
->dma_dac2
.fragshift
;
1618 cinfo
.ptr
= s
->dma_dac2
.hwptr
;
1619 if (s
->dma_dac2
.mapped
)
1620 s
->dma_dac2
.count
&= s
->dma_dac2
.fragsize
-1;
1621 spin_unlock_irqrestore(&s
->lock
, flags
);
1622 return copy_to_user((void *)arg
, &cinfo
, sizeof(cinfo
));
1624 case SNDCTL_DSP_GETBLKSIZE
:
1625 if (file
->f_mode
& FMODE_WRITE
) {
1626 if ((val
= prog_dmabuf_dac2(s
)))
1628 return put_user(s
->dma_dac2
.fragsize
, (int *)arg
);
1630 if ((val
= prog_dmabuf_adc(s
)))
1632 return put_user(s
->dma_adc
.fragsize
, (int *)arg
);
1634 case SNDCTL_DSP_SETFRAGMENT
:
1635 if (get_user(val
, (int *)arg
))
1637 if (file
->f_mode
& FMODE_READ
) {
1638 s
->dma_adc
.ossfragshift
= val
& 0xffff;
1639 s
->dma_adc
.ossmaxfrags
= (val
>> 16) & 0xffff;
1640 if (s
->dma_adc
.ossfragshift
< 4)
1641 s
->dma_adc
.ossfragshift
= 4;
1642 if (s
->dma_adc
.ossfragshift
> 15)
1643 s
->dma_adc
.ossfragshift
= 15;
1644 if (s
->dma_adc
.ossmaxfrags
< 4)
1645 s
->dma_adc
.ossmaxfrags
= 4;
1647 if (file
->f_mode
& FMODE_WRITE
) {
1648 s
->dma_dac2
.ossfragshift
= val
& 0xffff;
1649 s
->dma_dac2
.ossmaxfrags
= (val
>> 16) & 0xffff;
1650 if (s
->dma_dac2
.ossfragshift
< 4)
1651 s
->dma_dac2
.ossfragshift
= 4;
1652 if (s
->dma_dac2
.ossfragshift
> 15)
1653 s
->dma_dac2
.ossfragshift
= 15;
1654 if (s
->dma_dac2
.ossmaxfrags
< 4)
1655 s
->dma_dac2
.ossmaxfrags
= 4;
1659 case SNDCTL_DSP_SUBDIVIDE
:
1660 if ((file
->f_mode
& FMODE_READ
&& s
->dma_adc
.subdivision
) ||
1661 (file
->f_mode
& FMODE_WRITE
&& s
->dma_dac2
.subdivision
))
1663 if (get_user(val
, (int *)arg
))
1665 if (val
!= 1 && val
!= 2 && val
!= 4)
1667 if (file
->f_mode
& FMODE_READ
)
1668 s
->dma_adc
.subdivision
= val
;
1669 if (file
->f_mode
& FMODE_WRITE
)
1670 s
->dma_dac2
.subdivision
= val
;
1673 case SOUND_PCM_READ_RATE
:
1674 return put_user(DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
), (int *)arg
);
1676 case SOUND_PCM_READ_CHANNELS
:
1677 return put_user((s
->sctrl
& ((file
->f_mode
& FMODE_READ
) ? SCTRL_R1SMB
: SCTRL_P2SMB
)) ?
1680 case SOUND_PCM_READ_BITS
:
1681 return put_user((s
->sctrl
& ((file
->f_mode
& FMODE_READ
) ? SCTRL_R1SEB
: SCTRL_P2SEB
)) ?
1682 16 : 8, (int *)arg
);
1684 case SOUND_PCM_WRITE_FILTER
:
1685 case SNDCTL_DSP_SETSYNCRO
:
1686 case SOUND_PCM_READ_FILTER
:
1690 return mixer_ioctl(s
, cmd
, arg
);
1693 static int es1370_open(struct inode
*inode
, struct file
*file
)
1695 int minor
= MINOR(inode
->i_rdev
);
1696 DECLARE_WAITQUEUE(wait
, current
);
1697 unsigned long flags
;
1698 struct list_head
*list
;
1699 struct es1370_state
*s
;
1701 for (list
= devs
.next
; ; list
= list
->next
) {
1704 s
= list_entry(list
, struct es1370_state
, devs
);
1705 if (!((s
->dev_audio
^ minor
) & ~0xf))
1709 file
->private_data
= s
;
1710 /* wait for device to become free */
1712 while (s
->open_mode
& file
->f_mode
) {
1713 if (file
->f_flags
& O_NONBLOCK
) {
1717 add_wait_queue(&s
->open_wait
, &wait
);
1718 __set_current_state(TASK_INTERRUPTIBLE
);
1721 remove_wait_queue(&s
->open_wait
, &wait
);
1722 set_current_state(TASK_RUNNING
);
1723 if (signal_pending(current
))
1724 return -ERESTARTSYS
;
1727 spin_lock_irqsave(&s
->lock
, flags
);
1728 if (!(s
->open_mode
& (FMODE_READ
|FMODE_WRITE
)))
1729 s
->ctrl
= (s
->ctrl
& ~CTRL_PCLKDIV
) | (DAC2_SRTODIV(8000) << CTRL_SH_PCLKDIV
);
1730 if (file
->f_mode
& FMODE_READ
) {
1731 s
->dma_adc
.ossfragshift
= s
->dma_adc
.ossmaxfrags
= s
->dma_adc
.subdivision
= 0;
1732 s
->sctrl
&= ~SCTRL_R1FMT
;
1733 if ((minor
& 0xf) == SND_DEV_DSP16
)
1734 s
->sctrl
|= ES1370_FMT_S16_MONO
<< SCTRL_SH_R1FMT
;
1736 s
->sctrl
|= ES1370_FMT_U8_MONO
<< SCTRL_SH_R1FMT
;
1738 if (file
->f_mode
& FMODE_WRITE
) {
1739 s
->dma_dac2
.ossfragshift
= s
->dma_dac2
.ossmaxfrags
= s
->dma_dac2
.subdivision
= 0;
1740 s
->sctrl
&= ~SCTRL_P2FMT
;
1741 if ((minor
& 0xf) == SND_DEV_DSP16
)
1742 s
->sctrl
|= ES1370_FMT_S16_MONO
<< SCTRL_SH_P2FMT
;
1744 s
->sctrl
|= ES1370_FMT_U8_MONO
<< SCTRL_SH_P2FMT
;
1746 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1747 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
1748 spin_unlock_irqrestore(&s
->lock
, flags
);
1749 s
->open_mode
|= file
->f_mode
& (FMODE_READ
| FMODE_WRITE
);
1754 static int es1370_release(struct inode
*inode
, struct file
*file
)
1756 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1760 if (file
->f_mode
& FMODE_WRITE
)
1761 drain_dac2(s
, file
->f_flags
& O_NONBLOCK
);
1763 if (file
->f_mode
& FMODE_WRITE
) {
1766 dealloc_dmabuf(s
, &s
->dma_dac2
);
1768 if (file
->f_mode
& FMODE_READ
) {
1770 dealloc_dmabuf(s
, &s
->dma_adc
);
1772 s
->open_mode
&= (~file
->f_mode
) & (FMODE_READ
|FMODE_WRITE
);
1773 wake_up(&s
->open_wait
);
1779 static /*const*/ struct file_operations es1370_audio_fops
= {
1781 llseek
: es1370_llseek
,
1783 write
: es1370_write
,
1785 ioctl
: es1370_ioctl
,
1788 release
: es1370_release
,
1791 /* --------------------------------------------------------------------- */
1793 static ssize_t
es1370_write_dac(struct file
*file
, const char *buffer
, size_t count
, loff_t
*ppos
)
1795 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1796 DECLARE_WAITQUEUE(wait
, current
);
1798 unsigned long flags
;
1803 if (ppos
!= &file
->f_pos
)
1805 if (s
->dma_dac1
.mapped
)
1807 if (!s
->dma_dac1
.ready
&& (ret
= prog_dmabuf_dac1(s
)))
1809 if (!access_ok(VERIFY_READ
, buffer
, count
))
1811 add_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1813 spin_lock_irqsave(&s
->lock
, flags
);
1814 if (s
->dma_dac1
.count
< 0) {
1815 s
->dma_dac1
.count
= 0;
1816 s
->dma_dac1
.swptr
= s
->dma_dac1
.hwptr
;
1818 swptr
= s
->dma_dac1
.swptr
;
1819 cnt
= s
->dma_dac1
.dmasize
-swptr
;
1820 if (s
->dma_dac1
.count
+ cnt
> s
->dma_dac1
.dmasize
)
1821 cnt
= s
->dma_dac1
.dmasize
- s
->dma_dac1
.count
;
1823 __set_current_state(TASK_INTERRUPTIBLE
);
1824 spin_unlock_irqrestore(&s
->lock
, flags
);
1829 if (file
->f_flags
& O_NONBLOCK
) {
1835 if (signal_pending(current
)) {
1842 if (copy_from_user(s
->dma_dac1
.rawbuf
+ swptr
, buffer
, cnt
)) {
1847 swptr
= (swptr
+ cnt
) % s
->dma_dac1
.dmasize
;
1848 spin_lock_irqsave(&s
->lock
, flags
);
1849 s
->dma_dac1
.swptr
= swptr
;
1850 s
->dma_dac1
.count
+= cnt
;
1851 s
->dma_dac1
.endcleared
= 0;
1852 spin_unlock_irqrestore(&s
->lock
, flags
);
1858 remove_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1859 set_current_state(TASK_RUNNING
);
1863 /* No kernel lock - we have our own spinlock */
1864 static unsigned int es1370_poll_dac(struct file
*file
, struct poll_table_struct
*wait
)
1866 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1867 unsigned long flags
;
1868 unsigned int mask
= 0;
1871 if (!s
->dma_dac1
.ready
&& prog_dmabuf_dac1(s
))
1873 poll_wait(file
, &s
->dma_dac1
.wait
, wait
);
1874 spin_lock_irqsave(&s
->lock
, flags
);
1875 es1370_update_ptr(s
);
1876 if (s
->dma_dac1
.mapped
) {
1877 if (s
->dma_dac1
.count
>= (signed)s
->dma_dac1
.fragsize
)
1878 mask
|= POLLOUT
| POLLWRNORM
;
1880 if ((signed)s
->dma_dac1
.dmasize
>= s
->dma_dac1
.count
+ (signed)s
->dma_dac1
.fragsize
)
1881 mask
|= POLLOUT
| POLLWRNORM
;
1883 spin_unlock_irqrestore(&s
->lock
, flags
);
1887 static int es1370_mmap_dac(struct file
*file
, struct vm_area_struct
*vma
)
1889 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1894 if (!(vma
->vm_flags
& VM_WRITE
))
1897 if ((ret
= prog_dmabuf_dac1(s
)) != 0)
1900 if (vma
->vm_pgoff
!= 0)
1902 size
= vma
->vm_end
- vma
->vm_start
;
1903 if (size
> (PAGE_SIZE
<< s
->dma_dac1
.buforder
))
1906 if (remap_page_range(vma
->vm_start
, virt_to_phys(s
->dma_dac1
.rawbuf
), size
, vma
->vm_page_prot
))
1908 s
->dma_dac1
.mapped
= 1;
1915 static int es1370_ioctl_dac(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1917 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1918 unsigned long flags
;
1919 audio_buf_info abinfo
;
1927 case OSS_GETVERSION
:
1928 return put_user(SOUND_VERSION
, (int *)arg
);
1930 case SNDCTL_DSP_SYNC
:
1931 return drain_dac1(s
, 0/*file->f_flags & O_NONBLOCK*/);
1933 case SNDCTL_DSP_SETDUPLEX
:
1936 case SNDCTL_DSP_GETCAPS
:
1937 return put_user(DSP_CAP_REALTIME
| DSP_CAP_TRIGGER
| DSP_CAP_MMAP
, (int *)arg
);
1939 case SNDCTL_DSP_RESET
:
1942 s
->dma_dac1
.swptr
= s
->dma_dac1
.hwptr
= s
->dma_dac1
.count
= s
->dma_dac1
.total_bytes
= 0;
1945 case SNDCTL_DSP_SPEED
:
1946 if (get_user(val
, (int *)arg
))
1950 s
->dma_dac1
.ready
= 0;
1951 for (ctrl
= 0; ctrl
<= 2; ctrl
++)
1952 if (val
< (dac1_samplerate
[ctrl
] + dac1_samplerate
[ctrl
+1]) / 2)
1954 spin_lock_irqsave(&s
->lock
, flags
);
1955 s
->ctrl
= (s
->ctrl
& ~CTRL_WTSRSEL
) | (ctrl
<< CTRL_SH_WTSRSEL
);
1956 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
1957 spin_unlock_irqrestore(&s
->lock
, flags
);
1959 return put_user(dac1_samplerate
[(s
->ctrl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
], (int *)arg
);
1961 case SNDCTL_DSP_STEREO
:
1962 if (get_user(val
, (int *)arg
))
1965 s
->dma_dac1
.ready
= 0;
1966 spin_lock_irqsave(&s
->lock
, flags
);
1968 s
->sctrl
|= SCTRL_P1SMB
;
1970 s
->sctrl
&= ~SCTRL_P1SMB
;
1971 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1972 spin_unlock_irqrestore(&s
->lock
, flags
);
1975 case SNDCTL_DSP_CHANNELS
:
1976 if (get_user(val
, (int *)arg
))
1979 if (s
->dma_dac1
.mapped
)
1982 s
->dma_dac1
.ready
= 0;
1983 spin_lock_irqsave(&s
->lock
, flags
);
1985 s
->sctrl
|= SCTRL_P1SMB
;
1987 s
->sctrl
&= ~SCTRL_P1SMB
;
1988 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1989 spin_unlock_irqrestore(&s
->lock
, flags
);
1991 return put_user((s
->sctrl
& SCTRL_P1SMB
) ? 2 : 1, (int *)arg
);
1993 case SNDCTL_DSP_GETFMTS
: /* Returns a mask */
1994 return put_user(AFMT_S16_LE
|AFMT_U8
, (int *)arg
);
1996 case SNDCTL_DSP_SETFMT
: /* Selects ONE fmt*/
1997 if (get_user(val
, (int *)arg
))
1999 if (val
!= AFMT_QUERY
) {
2001 s
->dma_dac1
.ready
= 0;
2002 spin_lock_irqsave(&s
->lock
, flags
);
2003 if (val
== AFMT_S16_LE
)
2004 s
->sctrl
|= SCTRL_P1SEB
;
2006 s
->sctrl
&= ~SCTRL_P1SEB
;
2007 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
2008 spin_unlock_irqrestore(&s
->lock
, flags
);
2010 return put_user((s
->sctrl
& SCTRL_P1SEB
) ? AFMT_S16_LE
: AFMT_U8
, (int *)arg
);
2012 case SNDCTL_DSP_POST
:
2015 case SNDCTL_DSP_GETTRIGGER
:
2016 return put_user((s
->ctrl
& CTRL_DAC1_EN
) ? PCM_ENABLE_OUTPUT
: 0, (int *)arg
);
2018 case SNDCTL_DSP_SETTRIGGER
:
2019 if (get_user(val
, (int *)arg
))
2021 if (val
& PCM_ENABLE_OUTPUT
) {
2022 if (!s
->dma_dac1
.ready
&& (ret
= prog_dmabuf_dac1(s
)))
2029 case SNDCTL_DSP_GETOSPACE
:
2030 if (!(s
->ctrl
& CTRL_DAC1_EN
) && (val
= prog_dmabuf_dac1(s
)) != 0)
2032 spin_lock_irqsave(&s
->lock
, flags
);
2033 es1370_update_ptr(s
);
2034 abinfo
.fragsize
= s
->dma_dac1
.fragsize
;
2035 count
= s
->dma_dac1
.count
;
2038 abinfo
.bytes
= s
->dma_dac1
.dmasize
- count
;
2039 abinfo
.fragstotal
= s
->dma_dac1
.numfrag
;
2040 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_dac1
.fragshift
;
2041 spin_unlock_irqrestore(&s
->lock
, flags
);
2042 return copy_to_user((void *)arg
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
2044 case SNDCTL_DSP_NONBLOCK
:
2045 file
->f_flags
|= O_NONBLOCK
;
2048 case SNDCTL_DSP_GETODELAY
:
2049 spin_lock_irqsave(&s
->lock
, flags
);
2050 es1370_update_ptr(s
);
2051 count
= s
->dma_dac1
.count
;
2052 spin_unlock_irqrestore(&s
->lock
, flags
);
2055 return put_user(count
, (int *)arg
);
2057 case SNDCTL_DSP_GETOPTR
:
2058 if (!(file
->f_mode
& FMODE_WRITE
))
2060 spin_lock_irqsave(&s
->lock
, flags
);
2061 es1370_update_ptr(s
);
2062 cinfo
.bytes
= s
->dma_dac1
.total_bytes
;
2063 count
= s
->dma_dac1
.count
;
2066 cinfo
.blocks
= count
>> s
->dma_dac1
.fragshift
;
2067 cinfo
.ptr
= s
->dma_dac1
.hwptr
;
2068 if (s
->dma_dac1
.mapped
)
2069 s
->dma_dac1
.count
&= s
->dma_dac1
.fragsize
-1;
2070 spin_unlock_irqrestore(&s
->lock
, flags
);
2071 return copy_to_user((void *)arg
, &cinfo
, sizeof(cinfo
));
2073 case SNDCTL_DSP_GETBLKSIZE
:
2074 if ((val
= prog_dmabuf_dac1(s
)))
2076 return put_user(s
->dma_dac1
.fragsize
, (int *)arg
);
2078 case SNDCTL_DSP_SETFRAGMENT
:
2079 if (get_user(val
, (int *)arg
))
2081 s
->dma_dac1
.ossfragshift
= val
& 0xffff;
2082 s
->dma_dac1
.ossmaxfrags
= (val
>> 16) & 0xffff;
2083 if (s
->dma_dac1
.ossfragshift
< 4)
2084 s
->dma_dac1
.ossfragshift
= 4;
2085 if (s
->dma_dac1
.ossfragshift
> 15)
2086 s
->dma_dac1
.ossfragshift
= 15;
2087 if (s
->dma_dac1
.ossmaxfrags
< 4)
2088 s
->dma_dac1
.ossmaxfrags
= 4;
2091 case SNDCTL_DSP_SUBDIVIDE
:
2092 if (s
->dma_dac1
.subdivision
)
2094 if (get_user(val
, (int *)arg
))
2096 if (val
!= 1 && val
!= 2 && val
!= 4)
2098 s
->dma_dac1
.subdivision
= val
;
2101 case SOUND_PCM_READ_RATE
:
2102 return put_user(dac1_samplerate
[(s
->ctrl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
], (int *)arg
);
2104 case SOUND_PCM_READ_CHANNELS
:
2105 return put_user((s
->sctrl
& SCTRL_P1SMB
) ? 2 : 1, (int *)arg
);
2107 case SOUND_PCM_READ_BITS
:
2108 return put_user((s
->sctrl
& SCTRL_P1SEB
) ? 16 : 8, (int *)arg
);
2110 case SOUND_PCM_WRITE_FILTER
:
2111 case SNDCTL_DSP_SETSYNCRO
:
2112 case SOUND_PCM_READ_FILTER
:
2116 return mixer_ioctl(s
, cmd
, arg
);
2119 static int es1370_open_dac(struct inode
*inode
, struct file
*file
)
2121 int minor
= MINOR(inode
->i_rdev
);
2122 DECLARE_WAITQUEUE(wait
, current
);
2123 unsigned long flags
;
2124 struct list_head
*list
;
2125 struct es1370_state
*s
;
2127 for (list
= devs
.next
; ; list
= list
->next
) {
2130 s
= list_entry(list
, struct es1370_state
, devs
);
2131 if (!((s
->dev_dac
^ minor
) & ~0xf))
2135 /* we allow opening with O_RDWR, most programs do it although they will only write */
2137 if (file
->f_mode
& FMODE_READ
)
2140 if (!(file
->f_mode
& FMODE_WRITE
))
2142 file
->private_data
= s
;
2143 /* wait for device to become free */
2145 while (s
->open_mode
& FMODE_DAC
) {
2146 if (file
->f_flags
& O_NONBLOCK
) {
2150 add_wait_queue(&s
->open_wait
, &wait
);
2151 __set_current_state(TASK_INTERRUPTIBLE
);
2154 remove_wait_queue(&s
->open_wait
, &wait
);
2155 set_current_state(TASK_RUNNING
);
2156 if (signal_pending(current
))
2157 return -ERESTARTSYS
;
2160 s
->dma_dac1
.ossfragshift
= s
->dma_dac1
.ossmaxfrags
= s
->dma_dac1
.subdivision
= 0;
2161 spin_lock_irqsave(&s
->lock
, flags
);
2162 s
->ctrl
= (s
->ctrl
& ~CTRL_WTSRSEL
) | (1 << CTRL_SH_WTSRSEL
);
2163 s
->sctrl
&= ~SCTRL_P1FMT
;
2164 if ((minor
& 0xf) == SND_DEV_DSP16
)
2165 s
->sctrl
|= ES1370_FMT_S16_MONO
<< SCTRL_SH_P1FMT
;
2167 s
->sctrl
|= ES1370_FMT_U8_MONO
<< SCTRL_SH_P1FMT
;
2168 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
2169 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
2170 spin_unlock_irqrestore(&s
->lock
, flags
);
2171 s
->open_mode
|= FMODE_DAC
;
2176 static int es1370_release_dac(struct inode
*inode
, struct file
*file
)
2178 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2182 drain_dac1(s
, file
->f_flags
& O_NONBLOCK
);
2185 dealloc_dmabuf(s
, &s
->dma_dac1
);
2186 s
->open_mode
&= ~FMODE_DAC
;
2187 wake_up(&s
->open_wait
);
2193 static /*const*/ struct file_operations es1370_dac_fops
= {
2195 llseek
: es1370_llseek
,
2196 write
: es1370_write_dac
,
2197 poll
: es1370_poll_dac
,
2198 ioctl
: es1370_ioctl_dac
,
2199 mmap
: es1370_mmap_dac
,
2200 open
: es1370_open_dac
,
2201 release
: es1370_release_dac
,
2204 /* --------------------------------------------------------------------- */
2206 static ssize_t
es1370_midi_read(struct file
*file
, char *buffer
, size_t count
, loff_t
*ppos
)
2208 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2209 DECLARE_WAITQUEUE(wait
, current
);
2211 unsigned long flags
;
2216 if (ppos
!= &file
->f_pos
)
2218 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
2223 add_wait_queue(&s
->midi
.iwait
, &wait
);
2225 spin_lock_irqsave(&s
->lock
, flags
);
2227 cnt
= MIDIINBUF
- ptr
;
2228 if (s
->midi
.icnt
< cnt
)
2231 __set_current_state(TASK_INTERRUPTIBLE
);
2232 spin_unlock_irqrestore(&s
->lock
, flags
);
2236 if (file
->f_flags
& O_NONBLOCK
) {
2242 if (signal_pending(current
)) {
2249 if (copy_to_user(buffer
, s
->midi
.ibuf
+ ptr
, cnt
)) {
2254 ptr
= (ptr
+ cnt
) % MIDIINBUF
;
2255 spin_lock_irqsave(&s
->lock
, flags
);
2257 s
->midi
.icnt
-= cnt
;
2258 spin_unlock_irqrestore(&s
->lock
, flags
);
2264 __set_current_state(TASK_RUNNING
);
2265 remove_wait_queue(&s
->midi
.iwait
, &wait
);
2269 static ssize_t
es1370_midi_write(struct file
*file
, const char *buffer
, size_t count
, loff_t
*ppos
)
2271 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2272 DECLARE_WAITQUEUE(wait
, current
);
2274 unsigned long flags
;
2279 if (ppos
!= &file
->f_pos
)
2281 if (!access_ok(VERIFY_READ
, buffer
, count
))
2286 add_wait_queue(&s
->midi
.owait
, &wait
);
2288 spin_lock_irqsave(&s
->lock
, flags
);
2290 cnt
= MIDIOUTBUF
- ptr
;
2291 if (s
->midi
.ocnt
+ cnt
> MIDIOUTBUF
)
2292 cnt
= MIDIOUTBUF
- s
->midi
.ocnt
;
2294 __set_current_state(TASK_INTERRUPTIBLE
);
2295 es1370_handle_midi(s
);
2297 spin_unlock_irqrestore(&s
->lock
, flags
);
2301 if (file
->f_flags
& O_NONBLOCK
) {
2307 if (signal_pending(current
)) {
2314 if (copy_from_user(s
->midi
.obuf
+ ptr
, buffer
, cnt
)) {
2319 ptr
= (ptr
+ cnt
) % MIDIOUTBUF
;
2320 spin_lock_irqsave(&s
->lock
, flags
);
2322 s
->midi
.ocnt
+= cnt
;
2323 spin_unlock_irqrestore(&s
->lock
, flags
);
2327 spin_lock_irqsave(&s
->lock
, flags
);
2328 es1370_handle_midi(s
);
2329 spin_unlock_irqrestore(&s
->lock
, flags
);
2331 __set_current_state(TASK_RUNNING
);
2332 remove_wait_queue(&s
->midi
.owait
, &wait
);
2336 /* No kernel lock - we have our own spinlock */
2337 static unsigned int es1370_midi_poll(struct file
*file
, struct poll_table_struct
*wait
)
2339 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2340 unsigned long flags
;
2341 unsigned int mask
= 0;
2344 if (file
->f_mode
& FMODE_WRITE
)
2345 poll_wait(file
, &s
->midi
.owait
, wait
);
2346 if (file
->f_mode
& FMODE_READ
)
2347 poll_wait(file
, &s
->midi
.iwait
, wait
);
2348 spin_lock_irqsave(&s
->lock
, flags
);
2349 if (file
->f_mode
& FMODE_READ
) {
2350 if (s
->midi
.icnt
> 0)
2351 mask
|= POLLIN
| POLLRDNORM
;
2353 if (file
->f_mode
& FMODE_WRITE
) {
2354 if (s
->midi
.ocnt
< MIDIOUTBUF
)
2355 mask
|= POLLOUT
| POLLWRNORM
;
2357 spin_unlock_irqrestore(&s
->lock
, flags
);
2361 static int es1370_midi_open(struct inode
*inode
, struct file
*file
)
2363 int minor
= MINOR(inode
->i_rdev
);
2364 DECLARE_WAITQUEUE(wait
, current
);
2365 unsigned long flags
;
2366 struct list_head
*list
;
2367 struct es1370_state
*s
;
2369 for (list
= devs
.next
; ; list
= list
->next
) {
2372 s
= list_entry(list
, struct es1370_state
, devs
);
2373 if (s
->dev_midi
== minor
)
2377 file
->private_data
= s
;
2378 /* wait for device to become free */
2380 while (s
->open_mode
& (file
->f_mode
<< FMODE_MIDI_SHIFT
)) {
2381 if (file
->f_flags
& O_NONBLOCK
) {
2385 add_wait_queue(&s
->open_wait
, &wait
);
2386 __set_current_state(TASK_INTERRUPTIBLE
);
2389 remove_wait_queue(&s
->open_wait
, &wait
);
2390 set_current_state(TASK_RUNNING
);
2391 if (signal_pending(current
))
2392 return -ERESTARTSYS
;
2395 spin_lock_irqsave(&s
->lock
, flags
);
2396 if (!(s
->open_mode
& (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
))) {
2397 s
->midi
.ird
= s
->midi
.iwr
= s
->midi
.icnt
= 0;
2398 s
->midi
.ord
= s
->midi
.owr
= s
->midi
.ocnt
= 0;
2399 outb(UCTRL_CNTRL_SWR
, s
->io
+ES1370_REG_UART_CONTROL
);
2400 outb(0, s
->io
+ES1370_REG_UART_CONTROL
);
2401 outb(0, s
->io
+ES1370_REG_UART_TEST
);
2403 if (file
->f_mode
& FMODE_READ
) {
2404 s
->midi
.ird
= s
->midi
.iwr
= s
->midi
.icnt
= 0;
2406 if (file
->f_mode
& FMODE_WRITE
) {
2407 s
->midi
.ord
= s
->midi
.owr
= s
->midi
.ocnt
= 0;
2409 s
->ctrl
|= CTRL_UART_EN
;
2410 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
2411 es1370_handle_midi(s
);
2412 spin_unlock_irqrestore(&s
->lock
, flags
);
2413 s
->open_mode
|= (file
->f_mode
<< FMODE_MIDI_SHIFT
) & (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
);
2418 static int es1370_midi_release(struct inode
*inode
, struct file
*file
)
2420 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2421 DECLARE_WAITQUEUE(wait
, current
);
2422 unsigned long flags
;
2423 unsigned count
, tmo
;
2428 if (file
->f_mode
& FMODE_WRITE
) {
2429 add_wait_queue(&s
->midi
.owait
, &wait
);
2431 __set_current_state(TASK_INTERRUPTIBLE
);
2432 spin_lock_irqsave(&s
->lock
, flags
);
2433 count
= s
->midi
.ocnt
;
2434 spin_unlock_irqrestore(&s
->lock
, flags
);
2437 if (signal_pending(current
))
2439 if (file
->f_flags
& O_NONBLOCK
) {
2440 remove_wait_queue(&s
->midi
.owait
, &wait
);
2441 set_current_state(TASK_RUNNING
);
2444 tmo
= (count
* HZ
) / 3100;
2445 if (!schedule_timeout(tmo
? : 1) && tmo
)
2446 DBG(printk(KERN_DEBUG
"es1370: midi timed out??\n");)
2448 remove_wait_queue(&s
->midi
.owait
, &wait
);
2449 set_current_state(TASK_RUNNING
);
2452 s
->open_mode
&= (~(file
->f_mode
<< FMODE_MIDI_SHIFT
)) & (FMODE_MIDI_READ
|FMODE_MIDI_WRITE
);
2453 spin_lock_irqsave(&s
->lock
, flags
);
2454 if (!(s
->open_mode
& (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
))) {
2455 s
->ctrl
&= ~CTRL_UART_EN
;
2456 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
2458 spin_unlock_irqrestore(&s
->lock
, flags
);
2459 wake_up(&s
->open_wait
);
2465 static /*const*/ struct file_operations es1370_midi_fops
= {
2467 llseek
: es1370_llseek
,
2468 read
: es1370_midi_read
,
2469 write
: es1370_midi_write
,
2470 poll
: es1370_midi_poll
,
2471 open
: es1370_midi_open
,
2472 release
: es1370_midi_release
,
2475 /* --------------------------------------------------------------------- */
2477 /* maximum number of devices; only used for command line params */
2480 static int joystick
[NR_DEVICE
] = { 0, };
2481 static int lineout
[NR_DEVICE
] = { 0, };
2482 static int micbias
[NR_DEVICE
] = { 0, };
2484 static unsigned int devindex
= 0;
2486 MODULE_PARM(joystick
, "1-" __MODULE_STRING(NR_DEVICE
) "i");
2487 MODULE_PARM_DESC(joystick
, "if 1 enables joystick interface (still need separate driver)");
2488 MODULE_PARM(lineout
, "1-" __MODULE_STRING(NR_DEVICE
) "i");
2489 MODULE_PARM_DESC(lineout
, "if 1 the LINE input is converted to LINE out");
2490 MODULE_PARM(micbias
, "1-" __MODULE_STRING(NR_DEVICE
) "i");
2491 MODULE_PARM_DESC(micbias
, "sets the +5V bias for an electret microphone");
2493 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
2494 MODULE_DESCRIPTION("ES1370 AudioPCI Driver");
2496 /* --------------------------------------------------------------------- */
2498 static struct initvol
{
2501 } initvol
[] __initdata
= {
2502 { SOUND_MIXER_WRITE_VOLUME
, 0x4040 },
2503 { SOUND_MIXER_WRITE_PCM
, 0x4040 },
2504 { SOUND_MIXER_WRITE_SYNTH
, 0x4040 },
2505 { SOUND_MIXER_WRITE_CD
, 0x4040 },
2506 { SOUND_MIXER_WRITE_LINE
, 0x4040 },
2507 { SOUND_MIXER_WRITE_LINE1
, 0x4040 },
2508 { SOUND_MIXER_WRITE_LINE2
, 0x4040 },
2509 { SOUND_MIXER_WRITE_LINE3
, 0x4040 },
2510 { SOUND_MIXER_WRITE_MIC
, 0x4040 },
2511 { SOUND_MIXER_WRITE_OGAIN
, 0x4040 }
2514 #define RSRCISIOREGION(dev,num) (pci_resource_start((dev), (num)) != 0 && \
2515 pci_resource_flags((dev), (num)) & IORESOURCE_IO)
2517 static int __devinit
es1370_probe(struct pci_dev
*pcidev
, const struct pci_device_id
*pciid
)
2519 struct es1370_state
*s
;
2523 if (!RSRCISIOREGION(pcidev
, 0))
2525 if (pcidev
->irq
== 0)
2527 if (!pci_dma_supported(pcidev
, 0xffffffff)) {
2528 printk(KERN_WARNING
"es1370: architecture does not support 32bit PCI busmaster DMA\n");
2531 if (!(s
= kmalloc(sizeof(struct es1370_state
), GFP_KERNEL
))) {
2532 printk(KERN_WARNING
"es1370: out of memory\n");
2535 memset(s
, 0, sizeof(struct es1370_state
));
2536 init_waitqueue_head(&s
->dma_adc
.wait
);
2537 init_waitqueue_head(&s
->dma_dac1
.wait
);
2538 init_waitqueue_head(&s
->dma_dac2
.wait
);
2539 init_waitqueue_head(&s
->open_wait
);
2540 init_waitqueue_head(&s
->midi
.iwait
);
2541 init_waitqueue_head(&s
->midi
.owait
);
2542 init_MUTEX(&s
->open_sem
);
2543 spin_lock_init(&s
->lock
);
2544 s
->magic
= ES1370_MAGIC
;
2546 s
->io
= pci_resource_start(pcidev
, 0);
2547 s
->irq
= pcidev
->irq
;
2548 if (!request_region(s
->io
, ES1370_EXTENT
, "es1370")) {
2549 printk(KERN_ERR
"es1370: io ports %#lx-%#lx in use\n", s
->io
, s
->io
+ES1370_EXTENT
-1);
2552 if (pci_enable_device(pcidev
))
2554 if (request_irq(s
->irq
, es1370_interrupt
, SA_SHIRQ
, "es1370", s
)) {
2555 printk(KERN_ERR
"es1370: irq %u in use\n", s
->irq
);
2558 /* initialize codec registers */
2559 /* note: setting CTRL_SERR_DIS is reported to break
2560 * mic bias setting (by Kim.Berts@fisub.mail.abb.com) */
2561 s
->ctrl
= CTRL_CDC_EN
| (DAC2_SRTODIV(8000) << CTRL_SH_PCLKDIV
) | (1 << CTRL_SH_WTSRSEL
);
2562 if (joystick
[devindex
]) {
2563 if (check_region(0x200, JOY_EXTENT
))
2564 printk(KERN_ERR
"es1370: io port 0x200 in use\n");
2566 s
->ctrl
|= CTRL_JYSTK_EN
;
2568 if (lineout
[devindex
])
2569 s
->ctrl
|= CTRL_XCTL0
;
2570 if (micbias
[devindex
])
2571 s
->ctrl
|= CTRL_XCTL1
;
2573 printk(KERN_INFO
"es1370: found adapter at io %#lx irq %u\n"
2574 KERN_INFO
"es1370: features: joystick %s, line %s, mic impedance %s\n",
2575 s
->io
, s
->irq
, (s
->ctrl
& CTRL_JYSTK_EN
) ? "on" : "off",
2576 (s
->ctrl
& CTRL_XCTL0
) ? "out" : "in",
2577 (s
->ctrl
& CTRL_XCTL1
) ? "1" : "0");
2578 /* register devices */
2579 if ((s
->dev_audio
= register_sound_dsp(&es1370_audio_fops
, -1)) < 0)
2581 if ((s
->dev_mixer
= register_sound_mixer(&es1370_mixer_fops
, -1)) < 0)
2583 if ((s
->dev_dac
= register_sound_dsp(&es1370_dac_fops
, -1)) < 0)
2585 if ((s
->dev_midi
= register_sound_midi(&es1370_midi_fops
, -1)) < 0)
2587 /* initialize the chips */
2588 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
2589 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
2590 /* point phantom write channel to "bugbuf" */
2591 outl((ES1370_REG_PHANTOM_FRAMEADR
>> 8) & 15, s
->io
+ES1370_REG_MEMPAGE
);
2592 outl(virt_to_bus(bugbuf
), s
->io
+(ES1370_REG_PHANTOM_FRAMEADR
& 0xff));
2593 outl(0, s
->io
+(ES1370_REG_PHANTOM_FRAMECNT
& 0xff));
2594 pci_set_master(pcidev
); /* enable bus mastering */
2595 wrcodec(s
, 0x16, 3); /* no RST, PD */
2596 wrcodec(s
, 0x17, 0); /* CODEC ADC and CODEC DAC use {LR,B}CLK2 and run off the LRCLK2 PLL; program DAC_SYNC=0!! */
2597 wrcodec(s
, 0x18, 0); /* recording source is mixer */
2598 wrcodec(s
, 0x19, s
->mix
.micpreamp
= 1); /* turn on MIC preamp */
2602 val
= SOUND_MASK_LINE
|SOUND_MASK_SYNTH
|SOUND_MASK_CD
;
2603 mixer_ioctl(s
, SOUND_MIXER_WRITE_RECSRC
, (unsigned long)&val
);
2604 for (i
= 0; i
< sizeof(initvol
)/sizeof(initvol
[0]); i
++) {
2605 val
= initvol
[i
].vol
;
2606 mixer_ioctl(s
, initvol
[i
].mixch
, (unsigned long)&val
);
2609 /* store it in the driver field */
2610 pci_set_drvdata(pcidev
, s
);
2611 pcidev
->dma_mask
= 0xffffffff;
2612 /* put it into driver list */
2613 list_add_tail(&s
->devs
, &devs
);
2614 /* increment devindex */
2615 if (devindex
< NR_DEVICE
-1)
2620 unregister_sound_dsp(s
->dev_dac
);
2622 unregister_sound_mixer(s
->dev_mixer
);
2624 unregister_sound_dsp(s
->dev_audio
);
2626 printk(KERN_ERR
"es1370: cannot register misc device\n");
2627 free_irq(s
->irq
, s
);
2629 release_region(s
->io
, ES1370_EXTENT
);
2635 static void __devinit
es1370_remove(struct pci_dev
*dev
)
2637 struct es1370_state
*s
= pci_get_drvdata(dev
);
2642 outl(CTRL_SERR_DIS
| (1 << CTRL_SH_WTSRSEL
), s
->io
+ES1370_REG_CONTROL
); /* switch everything off */
2643 outl(0, s
->io
+ES1370_REG_SERIAL_CONTROL
); /* clear serial interrupts */
2645 free_irq(s
->irq
, s
);
2646 release_region(s
->io
, ES1370_EXTENT
);
2647 unregister_sound_dsp(s
->dev_audio
);
2648 unregister_sound_mixer(s
->dev_mixer
);
2649 unregister_sound_dsp(s
->dev_dac
);
2650 unregister_sound_midi(s
->dev_midi
);
2652 pci_set_drvdata(dev
, NULL
);
2655 static struct pci_device_id id_table
[] __devinitdata
= {
2656 { PCI_VENDOR_ID_ENSONIQ
, PCI_DEVICE_ID_ENSONIQ_ES1370
, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0 },
2660 MODULE_DEVICE_TABLE(pci
, id_table
);
2662 static struct pci_driver es1370_driver
= {
2665 probe
: es1370_probe
,
2666 remove
: es1370_remove
2669 static int __init
init_es1370(void)
2671 if (!pci_present()) /* No PCI bus in this machine! */
2673 printk(KERN_INFO
"es1370: version v0.34 time " __TIME__
" " __DATE__
"\n");
2674 return pci_module_init(&es1370_driver
);
2677 static void __exit
cleanup_es1370(void)
2679 printk(KERN_INFO
"es1370: unloading\n");
2680 pci_unregister_driver(&es1370_driver
);
2683 module_init(init_es1370
);
2684 module_exit(cleanup_es1370
);
2686 /* --------------------------------------------------------------------- */
2690 /* format is: es1370=[joystick[,lineout[,micbias]]] */
2692 static int __init
es1370_setup(char *str
)
2694 static unsigned __initdata nr_dev
= 0;
2696 if (nr_dev
>= NR_DEVICE
)
2700 ( (get_option(&str
,&joystick
[nr_dev
]) == 2)
2701 && (get_option(&str
,&lineout
[nr_dev
]) == 2)
2702 && get_option(&str
,&micbias
[nr_dev
])
2709 __setup("es1370=", es1370_setup
);