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>
161 #include <asm/page.h>
162 #include <asm/uaccess.h>
164 /* --------------------------------------------------------------------- */
166 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
168 /*#define DBG(x) {x}*/
170 /* --------------------------------------------------------------------- */
172 #ifndef PCI_VENDOR_ID_ENSONIQ
173 #define PCI_VENDOR_ID_ENSONIQ 0x1274
176 #ifndef PCI_DEVICE_ID_ENSONIQ_ES1370
177 #define PCI_DEVICE_ID_ENSONIQ_ES1370 0x5000
180 #define ES1370_MAGIC ((PCI_VENDOR_ID_ENSONIQ<<16)|PCI_DEVICE_ID_ENSONIQ_ES1370)
182 #define ES1370_EXTENT 0x40
185 #define ES1370_REG_CONTROL 0x00
186 #define ES1370_REG_STATUS 0x04
187 #define ES1370_REG_UART_DATA 0x08
188 #define ES1370_REG_UART_STATUS 0x09
189 #define ES1370_REG_UART_CONTROL 0x09
190 #define ES1370_REG_UART_TEST 0x0a
191 #define ES1370_REG_MEMPAGE 0x0c
192 #define ES1370_REG_CODEC 0x10
193 #define ES1370_REG_SERIAL_CONTROL 0x20
194 #define ES1370_REG_DAC1_SCOUNT 0x24
195 #define ES1370_REG_DAC2_SCOUNT 0x28
196 #define ES1370_REG_ADC_SCOUNT 0x2c
198 #define ES1370_REG_DAC1_FRAMEADR 0xc30
199 #define ES1370_REG_DAC1_FRAMECNT 0xc34
200 #define ES1370_REG_DAC2_FRAMEADR 0xc38
201 #define ES1370_REG_DAC2_FRAMECNT 0xc3c
202 #define ES1370_REG_ADC_FRAMEADR 0xd30
203 #define ES1370_REG_ADC_FRAMECNT 0xd34
204 #define ES1370_REG_PHANTOM_FRAMEADR 0xd38
205 #define ES1370_REG_PHANTOM_FRAMECNT 0xd3c
207 #define ES1370_FMT_U8_MONO 0
208 #define ES1370_FMT_U8_STEREO 1
209 #define ES1370_FMT_S16_MONO 2
210 #define ES1370_FMT_S16_STEREO 3
211 #define ES1370_FMT_STEREO 1
212 #define ES1370_FMT_S16 2
213 #define ES1370_FMT_MASK 3
215 static const unsigned sample_size
[] = { 1, 2, 2, 4 };
216 static const unsigned sample_shift
[] = { 0, 1, 1, 2 };
218 static const unsigned dac1_samplerate
[] = { 5512, 11025, 22050, 44100 };
220 #define DAC2_SRTODIV(x) (((1411200+(x)/2)/(x))-2)
221 #define DAC2_DIVTOSR(x) (1411200/((x)+2))
223 #define CTRL_ADC_STOP 0x80000000 /* 1 = ADC stopped */
224 #define CTRL_XCTL1 0x40000000 /* electret mic bias */
225 #define CTRL_OPEN 0x20000000 /* no function, can be read and written */
226 #define CTRL_PCLKDIV 0x1fff0000 /* ADC/DAC2 clock divider */
227 #define CTRL_SH_PCLKDIV 16
228 #define CTRL_MSFMTSEL 0x00008000 /* MPEG serial data fmt: 0 = Sony, 1 = I2S */
229 #define CTRL_M_SBB 0x00004000 /* DAC2 clock: 0 = PCLKDIV, 1 = MPEG */
230 #define CTRL_WTSRSEL 0x00003000 /* DAC1 clock freq: 0=5512, 1=11025, 2=22050, 3=44100 */
231 #define CTRL_SH_WTSRSEL 12
232 #define CTRL_DAC_SYNC 0x00000800 /* 1 = DAC2 runs off DAC1 clock */
233 #define CTRL_CCB_INTRM 0x00000400 /* 1 = CCB "voice" ints enabled */
234 #define CTRL_M_CB 0x00000200 /* recording source: 0 = ADC, 1 = MPEG */
235 #define CTRL_XCTL0 0x00000100 /* 0 = Line in, 1 = Line out */
236 #define CTRL_BREQ 0x00000080 /* 1 = test mode (internal mem test) */
237 #define CTRL_DAC1_EN 0x00000040 /* enable DAC1 */
238 #define CTRL_DAC2_EN 0x00000020 /* enable DAC2 */
239 #define CTRL_ADC_EN 0x00000010 /* enable ADC */
240 #define CTRL_UART_EN 0x00000008 /* enable MIDI uart */
241 #define CTRL_JYSTK_EN 0x00000004 /* enable Joystick port (presumably at address 0x200) */
242 #define CTRL_CDC_EN 0x00000002 /* enable serial (CODEC) interface */
243 #define CTRL_SERR_DIS 0x00000001 /* 1 = disable PCI SERR signal */
245 #define STAT_INTR 0x80000000 /* wired or of all interrupt bits */
246 #define STAT_CSTAT 0x00000400 /* 1 = codec busy or codec write in progress */
247 #define STAT_CBUSY 0x00000200 /* 1 = codec busy */
248 #define STAT_CWRIP 0x00000100 /* 1 = codec write in progress */
249 #define STAT_VC 0x00000060 /* CCB int source, 0=DAC1, 1=DAC2, 2=ADC, 3=undef */
251 #define STAT_MCCB 0x00000010 /* CCB int pending */
252 #define STAT_UART 0x00000008 /* UART int pending */
253 #define STAT_DAC1 0x00000004 /* DAC1 int pending */
254 #define STAT_DAC2 0x00000002 /* DAC2 int pending */
255 #define STAT_ADC 0x00000001 /* ADC int pending */
257 #define USTAT_RXINT 0x80 /* UART rx int pending */
258 #define USTAT_TXINT 0x04 /* UART tx int pending */
259 #define USTAT_TXRDY 0x02 /* UART tx ready */
260 #define USTAT_RXRDY 0x01 /* UART rx ready */
262 #define UCTRL_RXINTEN 0x80 /* 1 = enable RX ints */
263 #define UCTRL_TXINTEN 0x60 /* TX int enable field mask */
264 #define UCTRL_ENA_TXINT 0x20 /* enable TX int */
265 #define UCTRL_CNTRL 0x03 /* control field */
266 #define UCTRL_CNTRL_SWR 0x03 /* software reset command */
268 #define SCTRL_P2ENDINC 0x00380000 /* */
269 #define SCTRL_SH_P2ENDINC 19
270 #define SCTRL_P2STINC 0x00070000 /* */
271 #define SCTRL_SH_P2STINC 16
272 #define SCTRL_R1LOOPSEL 0x00008000 /* 0 = loop mode */
273 #define SCTRL_P2LOOPSEL 0x00004000 /* 0 = loop mode */
274 #define SCTRL_P1LOOPSEL 0x00002000 /* 0 = loop mode */
275 #define SCTRL_P2PAUSE 0x00001000 /* 1 = pause mode */
276 #define SCTRL_P1PAUSE 0x00000800 /* 1 = pause mode */
277 #define SCTRL_R1INTEN 0x00000400 /* enable interrupt */
278 #define SCTRL_P2INTEN 0x00000200 /* enable interrupt */
279 #define SCTRL_P1INTEN 0x00000100 /* enable interrupt */
280 #define SCTRL_P1SCTRLD 0x00000080 /* reload sample count register for DAC1 */
281 #define SCTRL_P2DACSEN 0x00000040 /* 1 = DAC2 play back last sample when disabled */
282 #define SCTRL_R1SEB 0x00000020 /* 1 = 16bit */
283 #define SCTRL_R1SMB 0x00000010 /* 1 = stereo */
284 #define SCTRL_R1FMT 0x00000030 /* format mask */
285 #define SCTRL_SH_R1FMT 4
286 #define SCTRL_P2SEB 0x00000008 /* 1 = 16bit */
287 #define SCTRL_P2SMB 0x00000004 /* 1 = stereo */
288 #define SCTRL_P2FMT 0x0000000c /* format mask */
289 #define SCTRL_SH_P2FMT 2
290 #define SCTRL_P1SEB 0x00000002 /* 1 = 16bit */
291 #define SCTRL_P1SMB 0x00000001 /* 1 = stereo */
292 #define SCTRL_P1FMT 0x00000003 /* format mask */
293 #define SCTRL_SH_P1FMT 0
297 #define FMODE_DAC 4 /* slight misuse of mode_t */
299 /* MIDI buffer sizes */
301 #define MIDIINBUF 256
302 #define MIDIOUTBUF 256
304 #define FMODE_MIDI_SHIFT 3
305 #define FMODE_MIDI_READ (FMODE_READ << FMODE_MIDI_SHIFT)
306 #define FMODE_MIDI_WRITE (FMODE_WRITE << FMODE_MIDI_SHIFT)
308 /* --------------------------------------------------------------------- */
310 struct es1370_state
{
314 /* list of es1370 devices */
315 struct list_head devs
;
317 /* the corresponding pci_dev structure */
320 /* soundcore stuff */
326 /* hardware resources */
327 unsigned long io
; /* long for SPARC */
330 /* mixer registers; there is no HW readback */
332 unsigned short vol
[10];
335 unsigned short micpreamp
;
344 struct semaphore open_sem
;
346 wait_queue_head_t open_wait
;
354 unsigned hwptr
, swptr
;
355 unsigned total_bytes
;
357 unsigned error
; /* over/underrun */
358 wait_queue_head_t wait
;
359 /* redundant, but makes calculations easier */
362 unsigned fragsamples
;
366 unsigned endcleared
:1;
368 unsigned ossfragshift
;
370 unsigned subdivision
;
371 } dma_dac1
, dma_dac2
, dma_adc
;
373 /* The following buffer is used to point the phantom write channel to. */
374 unsigned char *bugbuf_cpu
;
375 dma_addr_t bugbuf_dma
;
379 unsigned ird
, iwr
, icnt
;
380 unsigned ord
, owr
, ocnt
;
381 wait_queue_head_t iwait
;
382 wait_queue_head_t owait
;
383 unsigned char ibuf
[MIDIINBUF
];
384 unsigned char obuf
[MIDIOUTBUF
];
387 struct gameport
*gameport
;
388 struct semaphore sem
;
391 /* --------------------------------------------------------------------- */
393 static LIST_HEAD(devs
);
395 /* --------------------------------------------------------------------- */
397 static inline unsigned ld2(unsigned int x
)
422 /* --------------------------------------------------------------------- */
424 static void wrcodec(struct es1370_state
*s
, unsigned char idx
, unsigned char data
)
426 unsigned long tmo
= jiffies
+ HZ
/10, j
;
430 if (!(inl(s
->io
+ES1370_REG_STATUS
) & STAT_CSTAT
)) {
431 outw((((unsigned short)idx
)<<8)|data
, s
->io
+ES1370_REG_CODEC
);
435 } while ((signed)(tmo
-j
) > 0);
436 printk(KERN_ERR
"es1370: write to codec register timeout\n");
439 /* --------------------------------------------------------------------- */
441 static inline void stop_adc(struct es1370_state
*s
)
445 spin_lock_irqsave(&s
->lock
, flags
);
446 s
->ctrl
&= ~CTRL_ADC_EN
;
447 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
448 spin_unlock_irqrestore(&s
->lock
, flags
);
451 static inline void stop_dac1(struct es1370_state
*s
)
455 spin_lock_irqsave(&s
->lock
, flags
);
456 s
->ctrl
&= ~CTRL_DAC1_EN
;
457 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
458 spin_unlock_irqrestore(&s
->lock
, flags
);
461 static inline void stop_dac2(struct es1370_state
*s
)
465 spin_lock_irqsave(&s
->lock
, flags
);
466 s
->ctrl
&= ~CTRL_DAC2_EN
;
467 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
468 spin_unlock_irqrestore(&s
->lock
, flags
);
471 static void start_dac1(struct es1370_state
*s
)
474 unsigned fragremain
, fshift
;
476 spin_lock_irqsave(&s
->lock
, flags
);
477 if (!(s
->ctrl
& CTRL_DAC1_EN
) && (s
->dma_dac1
.mapped
|| s
->dma_dac1
.count
> 0)
478 && s
->dma_dac1
.ready
) {
479 s
->ctrl
|= CTRL_DAC1_EN
;
480 s
->sctrl
= (s
->sctrl
& ~(SCTRL_P1LOOPSEL
| SCTRL_P1PAUSE
| SCTRL_P1SCTRLD
)) | SCTRL_P1INTEN
;
481 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
482 fragremain
= ((- s
->dma_dac1
.hwptr
) & (s
->dma_dac1
.fragsize
-1));
483 fshift
= sample_shift
[(s
->sctrl
& SCTRL_P1FMT
) >> SCTRL_SH_P1FMT
];
484 if (fragremain
< 2*fshift
)
485 fragremain
= s
->dma_dac1
.fragsize
;
486 outl((fragremain
>> fshift
) - 1, s
->io
+ES1370_REG_DAC1_SCOUNT
);
487 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
488 outl((s
->dma_dac1
.fragsize
>> fshift
) - 1, s
->io
+ES1370_REG_DAC1_SCOUNT
);
490 spin_unlock_irqrestore(&s
->lock
, flags
);
493 static void start_dac2(struct es1370_state
*s
)
496 unsigned fragremain
, fshift
;
498 spin_lock_irqsave(&s
->lock
, flags
);
499 if (!(s
->ctrl
& CTRL_DAC2_EN
) && (s
->dma_dac2
.mapped
|| s
->dma_dac2
.count
> 0)
500 && s
->dma_dac2
.ready
) {
501 s
->ctrl
|= CTRL_DAC2_EN
;
502 s
->sctrl
= (s
->sctrl
& ~(SCTRL_P2LOOPSEL
| SCTRL_P2PAUSE
| SCTRL_P2DACSEN
|
503 SCTRL_P2ENDINC
| SCTRL_P2STINC
)) | SCTRL_P2INTEN
|
504 (((s
->sctrl
& SCTRL_P2FMT
) ? 2 : 1) << SCTRL_SH_P2ENDINC
) |
505 (0 << SCTRL_SH_P2STINC
);
506 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
507 fragremain
= ((- s
->dma_dac2
.hwptr
) & (s
->dma_dac2
.fragsize
-1));
508 fshift
= sample_shift
[(s
->sctrl
& SCTRL_P2FMT
) >> SCTRL_SH_P2FMT
];
509 if (fragremain
< 2*fshift
)
510 fragremain
= s
->dma_dac2
.fragsize
;
511 outl((fragremain
>> fshift
) - 1, s
->io
+ES1370_REG_DAC2_SCOUNT
);
512 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
513 outl((s
->dma_dac2
.fragsize
>> fshift
) - 1, s
->io
+ES1370_REG_DAC2_SCOUNT
);
515 spin_unlock_irqrestore(&s
->lock
, flags
);
518 static void start_adc(struct es1370_state
*s
)
521 unsigned fragremain
, fshift
;
523 spin_lock_irqsave(&s
->lock
, flags
);
524 if (!(s
->ctrl
& CTRL_ADC_EN
) && (s
->dma_adc
.mapped
|| s
->dma_adc
.count
< (signed)(s
->dma_adc
.dmasize
- 2*s
->dma_adc
.fragsize
))
525 && s
->dma_adc
.ready
) {
526 s
->ctrl
|= CTRL_ADC_EN
;
527 s
->sctrl
= (s
->sctrl
& ~SCTRL_R1LOOPSEL
) | SCTRL_R1INTEN
;
528 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
529 fragremain
= ((- s
->dma_adc
.hwptr
) & (s
->dma_adc
.fragsize
-1));
530 fshift
= sample_shift
[(s
->sctrl
& SCTRL_R1FMT
) >> SCTRL_SH_R1FMT
];
531 if (fragremain
< 2*fshift
)
532 fragremain
= s
->dma_adc
.fragsize
;
533 outl((fragremain
>> fshift
) - 1, s
->io
+ES1370_REG_ADC_SCOUNT
);
534 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
535 outl((s
->dma_adc
.fragsize
>> fshift
) - 1, s
->io
+ES1370_REG_ADC_SCOUNT
);
537 spin_unlock_irqrestore(&s
->lock
, flags
);
540 /* --------------------------------------------------------------------- */
542 #define DMABUF_DEFAULTORDER (17-PAGE_SHIFT)
543 #define DMABUF_MINORDER 1
545 static inline void dealloc_dmabuf(struct es1370_state
*s
, struct dmabuf
*db
)
547 struct page
*page
, *pend
;
550 /* undo marking the pages as reserved */
551 pend
= virt_to_page(db
->rawbuf
+ (PAGE_SIZE
<< db
->buforder
) - 1);
552 for (page
= virt_to_page(db
->rawbuf
); page
<= pend
; page
++)
553 ClearPageReserved(page
);
554 pci_free_consistent(s
->dev
, PAGE_SIZE
<< db
->buforder
, db
->rawbuf
, db
->dmaaddr
);
557 db
->mapped
= db
->ready
= 0;
560 static int prog_dmabuf(struct es1370_state
*s
, struct dmabuf
*db
, unsigned rate
, unsigned fmt
, unsigned reg
)
565 struct page
*page
, *pend
;
567 db
->hwptr
= db
->swptr
= db
->total_bytes
= db
->count
= db
->error
= db
->endcleared
= 0;
569 db
->ready
= db
->mapped
= 0;
570 for (order
= DMABUF_DEFAULTORDER
; order
>= DMABUF_MINORDER
; order
--)
571 if ((db
->rawbuf
= pci_alloc_consistent(s
->dev
, PAGE_SIZE
<< order
, &db
->dmaaddr
)))
575 db
->buforder
= order
;
576 /* now mark the pages as reserved; otherwise remap_pfn_range doesn't do what we want */
577 pend
= virt_to_page(db
->rawbuf
+ (PAGE_SIZE
<< db
->buforder
) - 1);
578 for (page
= virt_to_page(db
->rawbuf
); page
<= pend
; page
++)
579 SetPageReserved(page
);
581 fmt
&= ES1370_FMT_MASK
;
582 bytepersec
= rate
<< sample_shift
[fmt
];
583 bufs
= PAGE_SIZE
<< db
->buforder
;
584 if (db
->ossfragshift
) {
585 if ((1000 << db
->ossfragshift
) < bytepersec
)
586 db
->fragshift
= ld2(bytepersec
/1000);
588 db
->fragshift
= db
->ossfragshift
;
590 db
->fragshift
= ld2(bytepersec
/100/(db
->subdivision
? db
->subdivision
: 1));
591 if (db
->fragshift
< 3)
594 db
->numfrag
= bufs
>> db
->fragshift
;
595 while (db
->numfrag
< 4 && db
->fragshift
> 3) {
597 db
->numfrag
= bufs
>> db
->fragshift
;
599 db
->fragsize
= 1 << db
->fragshift
;
600 if (db
->ossmaxfrags
>= 4 && db
->ossmaxfrags
< db
->numfrag
)
601 db
->numfrag
= db
->ossmaxfrags
;
602 db
->fragsamples
= db
->fragsize
>> sample_shift
[fmt
];
603 db
->dmasize
= db
->numfrag
<< db
->fragshift
;
604 memset(db
->rawbuf
, (fmt
& ES1370_FMT_S16
) ? 0 : 0x80, db
->dmasize
);
605 outl((reg
>> 8) & 15, s
->io
+ES1370_REG_MEMPAGE
);
606 outl(db
->dmaaddr
, s
->io
+(reg
& 0xff));
607 outl((db
->dmasize
>> 2)-1, s
->io
+((reg
+ 4) & 0xff));
613 static inline int prog_dmabuf_adc(struct es1370_state
*s
)
616 return prog_dmabuf(s
, &s
->dma_adc
, DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
),
617 (s
->sctrl
>> SCTRL_SH_R1FMT
) & ES1370_FMT_MASK
, ES1370_REG_ADC_FRAMEADR
);
620 static inline int prog_dmabuf_dac2(struct es1370_state
*s
)
623 return prog_dmabuf(s
, &s
->dma_dac2
, DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
),
624 (s
->sctrl
>> SCTRL_SH_P2FMT
) & ES1370_FMT_MASK
, ES1370_REG_DAC2_FRAMEADR
);
627 static inline int prog_dmabuf_dac1(struct es1370_state
*s
)
630 return prog_dmabuf(s
, &s
->dma_dac1
, dac1_samplerate
[(s
->ctrl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
],
631 (s
->sctrl
>> SCTRL_SH_P1FMT
) & ES1370_FMT_MASK
, ES1370_REG_DAC1_FRAMEADR
);
634 static inline unsigned get_hwptr(struct es1370_state
*s
, struct dmabuf
*db
, unsigned reg
)
636 unsigned hwptr
, diff
;
638 outl((reg
>> 8) & 15, s
->io
+ES1370_REG_MEMPAGE
);
639 hwptr
= (inl(s
->io
+(reg
& 0xff)) >> 14) & 0x3fffc;
640 diff
= (db
->dmasize
+ hwptr
- db
->hwptr
) % db
->dmasize
;
645 static inline void clear_advance(void *buf
, unsigned bsize
, unsigned bptr
, unsigned len
, unsigned char c
)
647 if (bptr
+ len
> bsize
) {
648 unsigned x
= bsize
- bptr
;
649 memset(((char *)buf
) + bptr
, c
, x
);
653 memset(((char *)buf
) + bptr
, c
, len
);
656 /* call with spinlock held! */
657 static void es1370_update_ptr(struct es1370_state
*s
)
661 /* update ADC pointer */
662 if (s
->ctrl
& CTRL_ADC_EN
) {
663 diff
= get_hwptr(s
, &s
->dma_adc
, ES1370_REG_ADC_FRAMECNT
);
664 s
->dma_adc
.total_bytes
+= diff
;
665 s
->dma_adc
.count
+= diff
;
666 if (s
->dma_adc
.count
>= (signed)s
->dma_adc
.fragsize
)
667 wake_up(&s
->dma_adc
.wait
);
668 if (!s
->dma_adc
.mapped
) {
669 if (s
->dma_adc
.count
> (signed)(s
->dma_adc
.dmasize
- ((3 * s
->dma_adc
.fragsize
) >> 1))) {
670 s
->ctrl
&= ~CTRL_ADC_EN
;
671 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
676 /* update DAC1 pointer */
677 if (s
->ctrl
& CTRL_DAC1_EN
) {
678 diff
= get_hwptr(s
, &s
->dma_dac1
, ES1370_REG_DAC1_FRAMECNT
);
679 s
->dma_dac1
.total_bytes
+= diff
;
680 if (s
->dma_dac1
.mapped
) {
681 s
->dma_dac1
.count
+= diff
;
682 if (s
->dma_dac1
.count
>= (signed)s
->dma_dac1
.fragsize
)
683 wake_up(&s
->dma_dac1
.wait
);
685 s
->dma_dac1
.count
-= diff
;
686 if (s
->dma_dac1
.count
<= 0) {
687 s
->ctrl
&= ~CTRL_DAC1_EN
;
688 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
690 } else if (s
->dma_dac1
.count
<= (signed)s
->dma_dac1
.fragsize
&& !s
->dma_dac1
.endcleared
) {
691 clear_advance(s
->dma_dac1
.rawbuf
, s
->dma_dac1
.dmasize
, s
->dma_dac1
.swptr
,
692 s
->dma_dac1
.fragsize
, (s
->sctrl
& SCTRL_P1SEB
) ? 0 : 0x80);
693 s
->dma_dac1
.endcleared
= 1;
695 if (s
->dma_dac1
.count
+ (signed)s
->dma_dac1
.fragsize
<= (signed)s
->dma_dac1
.dmasize
)
696 wake_up(&s
->dma_dac1
.wait
);
699 /* update DAC2 pointer */
700 if (s
->ctrl
& CTRL_DAC2_EN
) {
701 diff
= get_hwptr(s
, &s
->dma_dac2
, ES1370_REG_DAC2_FRAMECNT
);
702 s
->dma_dac2
.total_bytes
+= diff
;
703 if (s
->dma_dac2
.mapped
) {
704 s
->dma_dac2
.count
+= diff
;
705 if (s
->dma_dac2
.count
>= (signed)s
->dma_dac2
.fragsize
)
706 wake_up(&s
->dma_dac2
.wait
);
708 s
->dma_dac2
.count
-= diff
;
709 if (s
->dma_dac2
.count
<= 0) {
710 s
->ctrl
&= ~CTRL_DAC2_EN
;
711 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
713 } else if (s
->dma_dac2
.count
<= (signed)s
->dma_dac2
.fragsize
&& !s
->dma_dac2
.endcleared
) {
714 clear_advance(s
->dma_dac2
.rawbuf
, s
->dma_dac2
.dmasize
, s
->dma_dac2
.swptr
,
715 s
->dma_dac2
.fragsize
, (s
->sctrl
& SCTRL_P2SEB
) ? 0 : 0x80);
716 s
->dma_dac2
.endcleared
= 1;
718 if (s
->dma_dac2
.count
+ (signed)s
->dma_dac2
.fragsize
<= (signed)s
->dma_dac2
.dmasize
)
719 wake_up(&s
->dma_dac2
.wait
);
724 /* hold spinlock for the following! */
725 static void es1370_handle_midi(struct es1370_state
*s
)
730 if (!(s
->ctrl
& CTRL_UART_EN
))
733 while (inb(s
->io
+ES1370_REG_UART_STATUS
) & USTAT_RXRDY
) {
734 ch
= inb(s
->io
+ES1370_REG_UART_DATA
);
735 if (s
->midi
.icnt
< MIDIINBUF
) {
736 s
->midi
.ibuf
[s
->midi
.iwr
] = ch
;
737 s
->midi
.iwr
= (s
->midi
.iwr
+ 1) % MIDIINBUF
;
743 wake_up(&s
->midi
.iwait
);
745 while ((inb(s
->io
+ES1370_REG_UART_STATUS
) & USTAT_TXRDY
) && s
->midi
.ocnt
> 0) {
746 outb(s
->midi
.obuf
[s
->midi
.ord
], s
->io
+ES1370_REG_UART_DATA
);
747 s
->midi
.ord
= (s
->midi
.ord
+ 1) % MIDIOUTBUF
;
749 if (s
->midi
.ocnt
< MIDIOUTBUF
-16)
753 wake_up(&s
->midi
.owait
);
754 outb((s
->midi
.ocnt
> 0) ? UCTRL_RXINTEN
| UCTRL_ENA_TXINT
: UCTRL_RXINTEN
, s
->io
+ES1370_REG_UART_CONTROL
);
757 static irqreturn_t
es1370_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
759 struct es1370_state
*s
= (struct es1370_state
*)dev_id
;
760 unsigned int intsrc
, sctl
;
762 /* fastpath out, to ease interrupt sharing */
763 intsrc
= inl(s
->io
+ES1370_REG_STATUS
);
764 if (!(intsrc
& 0x80000000))
767 /* clear audio interrupts first */
769 if (intsrc
& STAT_ADC
)
770 sctl
&= ~SCTRL_R1INTEN
;
771 if (intsrc
& STAT_DAC1
)
772 sctl
&= ~SCTRL_P1INTEN
;
773 if (intsrc
& STAT_DAC2
)
774 sctl
&= ~SCTRL_P2INTEN
;
775 outl(sctl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
776 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
777 es1370_update_ptr(s
);
778 es1370_handle_midi(s
);
779 spin_unlock(&s
->lock
);
783 /* --------------------------------------------------------------------- */
785 static const char invalid_magic
[] = KERN_CRIT
"es1370: invalid magic value\n";
787 #define VALIDATE_STATE(s) \
789 if (!(s) || (s)->magic != ES1370_MAGIC) { \
790 printk(invalid_magic); \
795 /* --------------------------------------------------------------------- */
797 static const struct {
804 } mixtable
[SOUND_MIXER_NRDEVICES
] = {
805 [SOUND_MIXER_VOLUME
] = { 0, 0x0, 0x1, 1, 0x0000, 1 }, /* master */
806 [SOUND_MIXER_PCM
] = { 1, 0x2, 0x3, 1, 0x0400, 1 }, /* voice */
807 [SOUND_MIXER_SYNTH
] = { 2, 0x4, 0x5, 1, 0x0060, 1 }, /* FM */
808 [SOUND_MIXER_CD
] = { 3, 0x6, 0x7, 1, 0x0006, 1 }, /* CD */
809 [SOUND_MIXER_LINE
] = { 4, 0x8, 0x9, 1, 0x0018, 1 }, /* Line */
810 [SOUND_MIXER_LINE1
] = { 5, 0xa, 0xb, 1, 0x1800, 1 }, /* AUX */
811 [SOUND_MIXER_LINE2
] = { 6, 0xc, 0x0, 0, 0x0100, 1 }, /* Mono1 */
812 [SOUND_MIXER_LINE3
] = { 7, 0xd, 0x0, 0, 0x0200, 1 }, /* Mono2 */
813 [SOUND_MIXER_MIC
] = { 8, 0xe, 0x0, 0, 0x0001, 1 }, /* Mic */
814 [SOUND_MIXER_OGAIN
] = { 9, 0xf, 0x0, 0, 0x0000, 1 } /* mono out */
817 static void set_recsrc(struct es1370_state
*s
, unsigned int val
)
821 for (j
= i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
822 if (!(val
& (1 << i
)))
824 if (!mixtable
[i
].recmask
) {
828 j
|= mixtable
[i
].recmask
;
831 wrcodec(s
, 0x12, j
& 0xd5);
832 wrcodec(s
, 0x13, j
& 0xaa);
833 wrcodec(s
, 0x14, (j
>> 8) & 0x17);
834 wrcodec(s
, 0x15, (j
>> 8) & 0x0f);
835 i
= (j
& 0x37f) | ((j
<< 1) & 0x3000) | 0xc60;
837 i
&= 0xff60; /* mute record and line monitor */
840 wrcodec(s
, 0x11, i
>> 8);
843 static int mixer_ioctl(struct es1370_state
*s
, unsigned int cmd
, unsigned long arg
)
847 unsigned char l
, r
, rl
, rr
;
848 int __user
*p
= (int __user
*)arg
;
851 if (cmd
== SOUND_MIXER_PRIVATE1
) {
852 /* enable/disable/query mixer preamp */
853 if (get_user(val
, p
))
856 s
->mix
.micpreamp
= !!val
;
857 wrcodec(s
, 0x19, s
->mix
.micpreamp
);
859 return put_user(s
->mix
.micpreamp
, p
);
861 if (cmd
== SOUND_MIXER_PRIVATE2
) {
862 /* enable/disable/query use of linein as second lineout */
863 if (get_user(val
, p
))
866 spin_lock_irqsave(&s
->lock
, flags
);
868 s
->ctrl
|= CTRL_XCTL0
;
870 s
->ctrl
&= ~CTRL_XCTL0
;
871 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
872 spin_unlock_irqrestore(&s
->lock
, flags
);
874 return put_user((s
->ctrl
& CTRL_XCTL0
) ? 1 : 0, p
);
876 if (cmd
== SOUND_MIXER_PRIVATE3
) {
877 /* enable/disable/query microphone impedance setting */
878 if (get_user(val
, p
))
881 spin_lock_irqsave(&s
->lock
, flags
);
883 s
->ctrl
|= CTRL_XCTL1
;
885 s
->ctrl
&= ~CTRL_XCTL1
;
886 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
887 spin_unlock_irqrestore(&s
->lock
, flags
);
889 return put_user((s
->ctrl
& CTRL_XCTL1
) ? 1 : 0, p
);
891 if (cmd
== SOUND_MIXER_INFO
) {
893 strncpy(info
.id
, "ES1370", sizeof(info
.id
));
894 strncpy(info
.name
, "Ensoniq ES1370", sizeof(info
.name
));
895 info
.modify_counter
= s
->mix
.modcnt
;
896 if (copy_to_user((void __user
*)arg
, &info
, sizeof(info
)))
900 if (cmd
== SOUND_OLD_MIXER_INFO
) {
901 _old_mixer_info info
;
902 strncpy(info
.id
, "ES1370", sizeof(info
.id
));
903 strncpy(info
.name
, "Ensoniq ES1370", sizeof(info
.name
));
904 if (copy_to_user((void __user
*)arg
, &info
, sizeof(info
)))
908 if (cmd
== OSS_GETVERSION
)
909 return put_user(SOUND_VERSION
, p
);
910 if (_IOC_TYPE(cmd
) != 'M' || _SIOC_SIZE(cmd
) != sizeof(int))
912 if (_SIOC_DIR(cmd
) == _SIOC_READ
) {
913 switch (_IOC_NR(cmd
)) {
914 case SOUND_MIXER_RECSRC
: /* Arg contains a bit for each recording source */
915 return put_user(s
->mix
.recsrc
, p
);
917 case SOUND_MIXER_DEVMASK
: /* Arg contains a bit for each supported device */
918 val
= SOUND_MASK_IMIX
;
919 for (i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++)
920 if (mixtable
[i
].avail
)
922 return put_user(val
, p
);
924 case SOUND_MIXER_RECMASK
: /* Arg contains a bit for each supported recording source */
925 for (val
= i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++)
926 if (mixtable
[i
].recmask
)
928 return put_user(val
, p
);
930 case SOUND_MIXER_STEREODEVS
: /* Mixer channels supporting stereo */
931 for (val
= i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++)
932 if (mixtable
[i
].stereo
)
934 return put_user(val
, p
);
936 case SOUND_MIXER_CAPS
:
937 return put_user(0, p
);
939 case SOUND_MIXER_IMIX
:
940 return put_user(s
->mix
.imix
, p
);
944 if (i
>= SOUND_MIXER_NRDEVICES
|| !mixtable
[i
].avail
)
946 return put_user(s
->mix
.vol
[mixtable
[i
].volidx
], p
);
949 if (_SIOC_DIR(cmd
) != (_SIOC_READ
|_SIOC_WRITE
))
952 switch (_IOC_NR(cmd
)) {
954 case SOUND_MIXER_IMIX
:
955 if (get_user(s
->mix
.imix
, p
))
957 set_recsrc(s
, s
->mix
.recsrc
);
960 case SOUND_MIXER_RECSRC
: /* Arg contains a bit for each recording source */
961 if (get_user(val
, p
))
968 if (i
>= SOUND_MIXER_NRDEVICES
|| !mixtable
[i
].avail
)
970 if (get_user(val
, p
))
975 if (mixtable
[i
].stereo
) {
976 r
= (val
>> 8) & 0xff;
983 rl
= 31 - ((l
- 7) / 3);
984 l
= (31 - rl
) * 3 + 7;
990 rr
= 31 - ((r
- 7) / 3);
991 r
= (31 - rr
) * 3 + 7;
993 wrcodec(s
, mixtable
[i
].right
, rr
);
995 if (mixtable
[i
].left
== 15) {
1000 rl
= 7 - ((l
- 2) / 14);
1001 r
= l
= (7 - rl
) * 14 + 2;
1008 rl
= 31 - ((l
- 7) / 3);
1009 r
= l
= (31 - rl
) * 3 + 7;
1013 wrcodec(s
, mixtable
[i
].left
, rl
);
1014 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1015 s
->mix
.vol
[mixtable
[i
].volidx
] = ((unsigned int)r
<< 8) | l
;
1017 s
->mix
.vol
[mixtable
[i
].volidx
] = val
;
1019 return put_user(s
->mix
.vol
[mixtable
[i
].volidx
], p
);
1023 /* --------------------------------------------------------------------- */
1025 static int es1370_open_mixdev(struct inode
*inode
, struct file
*file
)
1027 unsigned int minor
= iminor(inode
);
1028 struct list_head
*list
;
1029 struct es1370_state
*s
;
1031 for (list
= devs
.next
; ; list
= list
->next
) {
1034 s
= list_entry(list
, struct es1370_state
, devs
);
1035 if (s
->dev_mixer
== minor
)
1039 file
->private_data
= s
;
1040 return nonseekable_open(inode
, file
);
1043 static int es1370_release_mixdev(struct inode
*inode
, struct file
*file
)
1045 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1051 static int es1370_ioctl_mixdev(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1053 return mixer_ioctl((struct es1370_state
*)file
->private_data
, cmd
, arg
);
1056 static /*const*/ struct file_operations es1370_mixer_fops
= {
1057 .owner
= THIS_MODULE
,
1058 .llseek
= no_llseek
,
1059 .ioctl
= es1370_ioctl_mixdev
,
1060 .open
= es1370_open_mixdev
,
1061 .release
= es1370_release_mixdev
,
1064 /* --------------------------------------------------------------------- */
1066 static int drain_dac1(struct es1370_state
*s
, int nonblock
)
1068 DECLARE_WAITQUEUE(wait
, current
);
1069 unsigned long flags
;
1072 if (s
->dma_dac1
.mapped
|| !s
->dma_dac1
.ready
)
1074 add_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1076 __set_current_state(TASK_INTERRUPTIBLE
);
1077 spin_lock_irqsave(&s
->lock
, flags
);
1078 count
= s
->dma_dac1
.count
;
1079 spin_unlock_irqrestore(&s
->lock
, flags
);
1082 if (signal_pending(current
))
1085 remove_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1086 set_current_state(TASK_RUNNING
);
1089 tmo
= 3 * HZ
* (count
+ s
->dma_dac1
.fragsize
) / 2
1090 / dac1_samplerate
[(s
->ctrl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
];
1091 tmo
>>= sample_shift
[(s
->sctrl
& SCTRL_P1FMT
) >> SCTRL_SH_P1FMT
];
1092 if (!schedule_timeout(tmo
+ 1))
1093 DBG(printk(KERN_DEBUG
"es1370: dma timed out??\n");)
1095 remove_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1096 set_current_state(TASK_RUNNING
);
1097 if (signal_pending(current
))
1098 return -ERESTARTSYS
;
1102 static int drain_dac2(struct es1370_state
*s
, int nonblock
)
1104 DECLARE_WAITQUEUE(wait
, current
);
1105 unsigned long flags
;
1108 if (s
->dma_dac2
.mapped
|| !s
->dma_dac2
.ready
)
1110 add_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1112 __set_current_state(TASK_INTERRUPTIBLE
);
1113 spin_lock_irqsave(&s
->lock
, flags
);
1114 count
= s
->dma_dac2
.count
;
1115 spin_unlock_irqrestore(&s
->lock
, flags
);
1118 if (signal_pending(current
))
1121 remove_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1122 set_current_state(TASK_RUNNING
);
1125 tmo
= 3 * HZ
* (count
+ s
->dma_dac2
.fragsize
) / 2
1126 / DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
);
1127 tmo
>>= sample_shift
[(s
->sctrl
& SCTRL_P2FMT
) >> SCTRL_SH_P2FMT
];
1128 if (!schedule_timeout(tmo
+ 1))
1129 DBG(printk(KERN_DEBUG
"es1370: dma timed out??\n");)
1131 remove_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1132 set_current_state(TASK_RUNNING
);
1133 if (signal_pending(current
))
1134 return -ERESTARTSYS
;
1138 /* --------------------------------------------------------------------- */
1140 static ssize_t
es1370_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
1142 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1143 DECLARE_WAITQUEUE(wait
, current
);
1145 unsigned long flags
;
1150 if (s
->dma_adc
.mapped
)
1152 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
1155 if (!s
->dma_adc
.ready
&& (ret
= prog_dmabuf_adc(s
)))
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
);
1171 if (s
->dma_adc
.enabled
)
1173 if (file
->f_flags
& O_NONBLOCK
) {
1180 if (signal_pending(current
)) {
1186 if (s
->dma_adc
.mapped
)
1193 if (copy_to_user(buffer
, s
->dma_adc
.rawbuf
+ swptr
, cnt
)) {
1198 swptr
= (swptr
+ cnt
) % s
->dma_adc
.dmasize
;
1199 spin_lock_irqsave(&s
->lock
, flags
);
1200 s
->dma_adc
.swptr
= swptr
;
1201 s
->dma_adc
.count
-= cnt
;
1202 spin_unlock_irqrestore(&s
->lock
, flags
);
1206 if (s
->dma_adc
.enabled
)
1211 remove_wait_queue(&s
->dma_adc
.wait
, &wait
);
1212 set_current_state(TASK_RUNNING
);
1216 static ssize_t
es1370_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
1218 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1219 DECLARE_WAITQUEUE(wait
, current
);
1221 unsigned long flags
;
1226 if (s
->dma_dac2
.mapped
)
1228 if (!access_ok(VERIFY_READ
, buffer
, count
))
1231 if (!s
->dma_dac2
.ready
&& (ret
= prog_dmabuf_dac2(s
)))
1234 add_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1236 spin_lock_irqsave(&s
->lock
, flags
);
1237 if (s
->dma_dac2
.count
< 0) {
1238 s
->dma_dac2
.count
= 0;
1239 s
->dma_dac2
.swptr
= s
->dma_dac2
.hwptr
;
1241 swptr
= s
->dma_dac2
.swptr
;
1242 cnt
= s
->dma_dac2
.dmasize
-swptr
;
1243 if (s
->dma_dac2
.count
+ cnt
> s
->dma_dac2
.dmasize
)
1244 cnt
= s
->dma_dac2
.dmasize
- s
->dma_dac2
.count
;
1246 __set_current_state(TASK_INTERRUPTIBLE
);
1247 spin_unlock_irqrestore(&s
->lock
, flags
);
1251 if (s
->dma_dac2
.enabled
)
1253 if (file
->f_flags
& O_NONBLOCK
) {
1260 if (signal_pending(current
)) {
1266 if (s
->dma_dac2
.mapped
)
1273 if (copy_from_user(s
->dma_dac2
.rawbuf
+ swptr
, buffer
, cnt
)) {
1278 swptr
= (swptr
+ cnt
) % s
->dma_dac2
.dmasize
;
1279 spin_lock_irqsave(&s
->lock
, flags
);
1280 s
->dma_dac2
.swptr
= swptr
;
1281 s
->dma_dac2
.count
+= cnt
;
1282 s
->dma_dac2
.endcleared
= 0;
1283 spin_unlock_irqrestore(&s
->lock
, flags
);
1287 if (s
->dma_dac2
.enabled
)
1292 remove_wait_queue(&s
->dma_dac2
.wait
, &wait
);
1293 set_current_state(TASK_RUNNING
);
1297 /* No kernel lock - we have our own spinlock */
1298 static unsigned int es1370_poll(struct file
*file
, struct poll_table_struct
*wait
)
1300 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1301 unsigned long flags
;
1302 unsigned int mask
= 0;
1305 if (file
->f_mode
& FMODE_WRITE
) {
1306 if (!s
->dma_dac2
.ready
&& prog_dmabuf_dac2(s
))
1308 poll_wait(file
, &s
->dma_dac2
.wait
, wait
);
1310 if (file
->f_mode
& FMODE_READ
) {
1311 if (!s
->dma_adc
.ready
&& prog_dmabuf_adc(s
))
1313 poll_wait(file
, &s
->dma_adc
.wait
, wait
);
1315 spin_lock_irqsave(&s
->lock
, flags
);
1316 es1370_update_ptr(s
);
1317 if (file
->f_mode
& FMODE_READ
) {
1318 if (s
->dma_adc
.count
>= (signed)s
->dma_adc
.fragsize
)
1319 mask
|= POLLIN
| POLLRDNORM
;
1321 if (file
->f_mode
& FMODE_WRITE
) {
1322 if (s
->dma_dac2
.mapped
) {
1323 if (s
->dma_dac2
.count
>= (signed)s
->dma_dac2
.fragsize
)
1324 mask
|= POLLOUT
| POLLWRNORM
;
1326 if ((signed)s
->dma_dac2
.dmasize
>= s
->dma_dac2
.count
+ (signed)s
->dma_dac2
.fragsize
)
1327 mask
|= POLLOUT
| POLLWRNORM
;
1330 spin_unlock_irqrestore(&s
->lock
, flags
);
1334 static int es1370_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1336 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1344 if (vma
->vm_flags
& VM_WRITE
) {
1345 if ((ret
= prog_dmabuf_dac2(s
)) != 0) {
1349 } else if (vma
->vm_flags
& VM_READ
) {
1350 if ((ret
= prog_dmabuf_adc(s
)) != 0) {
1358 if (vma
->vm_pgoff
!= 0) {
1362 size
= vma
->vm_end
- vma
->vm_start
;
1363 if (size
> (PAGE_SIZE
<< db
->buforder
)) {
1367 if (remap_pfn_range(vma
, vma
->vm_start
,
1368 virt_to_phys(db
->rawbuf
) >> PAGE_SHIFT
,
1369 size
, vma
->vm_page_prot
)) {
1380 static int es1370_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1382 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1383 unsigned long flags
;
1384 audio_buf_info abinfo
;
1387 int val
, mapped
, ret
;
1388 void __user
*argp
= (void __user
*)arg
;
1389 int __user
*p
= argp
;
1392 mapped
= ((file
->f_mode
& FMODE_WRITE
) && s
->dma_dac2
.mapped
) ||
1393 ((file
->f_mode
& FMODE_READ
) && s
->dma_adc
.mapped
);
1395 case OSS_GETVERSION
:
1396 return put_user(SOUND_VERSION
, p
);
1398 case SNDCTL_DSP_SYNC
:
1399 if (file
->f_mode
& FMODE_WRITE
)
1400 return drain_dac2(s
, 0/*file->f_flags & O_NONBLOCK*/);
1403 case SNDCTL_DSP_SETDUPLEX
:
1406 case SNDCTL_DSP_GETCAPS
:
1407 return put_user(DSP_CAP_DUPLEX
| DSP_CAP_REALTIME
| DSP_CAP_TRIGGER
| DSP_CAP_MMAP
, p
);
1409 case SNDCTL_DSP_RESET
:
1410 if (file
->f_mode
& FMODE_WRITE
) {
1412 synchronize_irq(s
->irq
);
1413 s
->dma_dac2
.swptr
= s
->dma_dac2
.hwptr
= s
->dma_dac2
.count
= s
->dma_dac2
.total_bytes
= 0;
1415 if (file
->f_mode
& FMODE_READ
) {
1417 synchronize_irq(s
->irq
);
1418 s
->dma_adc
.swptr
= s
->dma_adc
.hwptr
= s
->dma_adc
.count
= s
->dma_adc
.total_bytes
= 0;
1422 case SNDCTL_DSP_SPEED
:
1423 if (get_user(val
, p
))
1426 if (s
->open_mode
& (~file
->f_mode
) & (FMODE_READ
|FMODE_WRITE
))
1434 s
->dma_adc
.ready
= s
->dma_dac2
.ready
= 0;
1435 spin_lock_irqsave(&s
->lock
, flags
);
1436 s
->ctrl
= (s
->ctrl
& ~CTRL_PCLKDIV
) | (DAC2_SRTODIV(val
) << CTRL_SH_PCLKDIV
);
1437 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
1438 spin_unlock_irqrestore(&s
->lock
, flags
);
1440 return put_user(DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
), p
);
1442 case SNDCTL_DSP_STEREO
:
1443 if (get_user(val
, p
))
1445 if (file
->f_mode
& FMODE_READ
) {
1447 s
->dma_adc
.ready
= 0;
1448 spin_lock_irqsave(&s
->lock
, flags
);
1450 s
->sctrl
|= SCTRL_R1SMB
;
1452 s
->sctrl
&= ~SCTRL_R1SMB
;
1453 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1454 spin_unlock_irqrestore(&s
->lock
, flags
);
1456 if (file
->f_mode
& FMODE_WRITE
) {
1458 s
->dma_dac2
.ready
= 0;
1459 spin_lock_irqsave(&s
->lock
, flags
);
1461 s
->sctrl
|= SCTRL_P2SMB
;
1463 s
->sctrl
&= ~SCTRL_P2SMB
;
1464 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1465 spin_unlock_irqrestore(&s
->lock
, flags
);
1469 case SNDCTL_DSP_CHANNELS
:
1470 if (get_user(val
, p
))
1473 if (file
->f_mode
& FMODE_READ
) {
1475 s
->dma_adc
.ready
= 0;
1476 spin_lock_irqsave(&s
->lock
, flags
);
1478 s
->sctrl
|= SCTRL_R1SMB
;
1480 s
->sctrl
&= ~SCTRL_R1SMB
;
1481 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1482 spin_unlock_irqrestore(&s
->lock
, flags
);
1484 if (file
->f_mode
& FMODE_WRITE
) {
1486 s
->dma_dac2
.ready
= 0;
1487 spin_lock_irqsave(&s
->lock
, flags
);
1489 s
->sctrl
|= SCTRL_P2SMB
;
1491 s
->sctrl
&= ~SCTRL_P2SMB
;
1492 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1493 spin_unlock_irqrestore(&s
->lock
, flags
);
1496 return put_user((s
->sctrl
& ((file
->f_mode
& FMODE_READ
) ? SCTRL_R1SMB
: SCTRL_P2SMB
)) ? 2 : 1, p
);
1498 case SNDCTL_DSP_GETFMTS
: /* Returns a mask */
1499 return put_user(AFMT_S16_LE
|AFMT_U8
, p
);
1501 case SNDCTL_DSP_SETFMT
: /* Selects ONE fmt*/
1502 if (get_user(val
, p
))
1504 if (val
!= AFMT_QUERY
) {
1505 if (file
->f_mode
& FMODE_READ
) {
1507 s
->dma_adc
.ready
= 0;
1508 spin_lock_irqsave(&s
->lock
, flags
);
1509 if (val
== AFMT_S16_LE
)
1510 s
->sctrl
|= SCTRL_R1SEB
;
1512 s
->sctrl
&= ~SCTRL_R1SEB
;
1513 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1514 spin_unlock_irqrestore(&s
->lock
, flags
);
1516 if (file
->f_mode
& FMODE_WRITE
) {
1518 s
->dma_dac2
.ready
= 0;
1519 spin_lock_irqsave(&s
->lock
, flags
);
1520 if (val
== AFMT_S16_LE
)
1521 s
->sctrl
|= SCTRL_P2SEB
;
1523 s
->sctrl
&= ~SCTRL_P2SEB
;
1524 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1525 spin_unlock_irqrestore(&s
->lock
, flags
);
1528 return put_user((s
->sctrl
& ((file
->f_mode
& FMODE_READ
) ? SCTRL_R1SEB
: SCTRL_P2SEB
)) ?
1529 AFMT_S16_LE
: AFMT_U8
, p
);
1531 case SNDCTL_DSP_POST
:
1534 case SNDCTL_DSP_GETTRIGGER
:
1536 if (file
->f_mode
& FMODE_READ
&& s
->ctrl
& CTRL_ADC_EN
)
1537 val
|= PCM_ENABLE_INPUT
;
1538 if (file
->f_mode
& FMODE_WRITE
&& s
->ctrl
& CTRL_DAC2_EN
)
1539 val
|= PCM_ENABLE_OUTPUT
;
1540 return put_user(val
, p
);
1542 case SNDCTL_DSP_SETTRIGGER
:
1543 if (get_user(val
, p
))
1545 if (file
->f_mode
& FMODE_READ
) {
1546 if (val
& PCM_ENABLE_INPUT
) {
1547 if (!s
->dma_adc
.ready
&& (ret
= prog_dmabuf_adc(s
)))
1549 s
->dma_adc
.enabled
= 1;
1552 s
->dma_adc
.enabled
= 0;
1556 if (file
->f_mode
& FMODE_WRITE
) {
1557 if (val
& PCM_ENABLE_OUTPUT
) {
1558 if (!s
->dma_dac2
.ready
&& (ret
= prog_dmabuf_dac2(s
)))
1560 s
->dma_dac2
.enabled
= 1;
1563 s
->dma_dac2
.enabled
= 0;
1569 case SNDCTL_DSP_GETOSPACE
:
1570 if (!(file
->f_mode
& FMODE_WRITE
))
1572 if (!s
->dma_dac2
.ready
&& (val
= prog_dmabuf_dac2(s
)) != 0)
1574 spin_lock_irqsave(&s
->lock
, flags
);
1575 es1370_update_ptr(s
);
1576 abinfo
.fragsize
= s
->dma_dac2
.fragsize
;
1577 count
= s
->dma_dac2
.count
;
1580 abinfo
.bytes
= s
->dma_dac2
.dmasize
- count
;
1581 abinfo
.fragstotal
= s
->dma_dac2
.numfrag
;
1582 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_dac2
.fragshift
;
1583 spin_unlock_irqrestore(&s
->lock
, flags
);
1584 return copy_to_user(argp
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1586 case SNDCTL_DSP_GETISPACE
:
1587 if (!(file
->f_mode
& FMODE_READ
))
1589 if (!s
->dma_adc
.ready
&& (val
= prog_dmabuf_adc(s
)) != 0)
1591 spin_lock_irqsave(&s
->lock
, flags
);
1592 es1370_update_ptr(s
);
1593 abinfo
.fragsize
= s
->dma_adc
.fragsize
;
1594 count
= s
->dma_adc
.count
;
1597 abinfo
.bytes
= count
;
1598 abinfo
.fragstotal
= s
->dma_adc
.numfrag
;
1599 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_adc
.fragshift
;
1600 spin_unlock_irqrestore(&s
->lock
, flags
);
1601 return copy_to_user(argp
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1603 case SNDCTL_DSP_NONBLOCK
:
1604 file
->f_flags
|= O_NONBLOCK
;
1607 case SNDCTL_DSP_GETODELAY
:
1608 if (!(file
->f_mode
& FMODE_WRITE
))
1610 if (!s
->dma_dac2
.ready
&& (val
= prog_dmabuf_dac2(s
)) != 0)
1612 spin_lock_irqsave(&s
->lock
, flags
);
1613 es1370_update_ptr(s
);
1614 count
= s
->dma_dac2
.count
;
1615 spin_unlock_irqrestore(&s
->lock
, flags
);
1618 return put_user(count
, p
);
1620 case SNDCTL_DSP_GETIPTR
:
1621 if (!(file
->f_mode
& FMODE_READ
))
1623 if (!s
->dma_adc
.ready
&& (val
= prog_dmabuf_adc(s
)) != 0)
1625 spin_lock_irqsave(&s
->lock
, flags
);
1626 es1370_update_ptr(s
);
1627 cinfo
.bytes
= s
->dma_adc
.total_bytes
;
1628 count
= s
->dma_adc
.count
;
1631 cinfo
.blocks
= count
>> s
->dma_adc
.fragshift
;
1632 cinfo
.ptr
= s
->dma_adc
.hwptr
;
1633 if (s
->dma_adc
.mapped
)
1634 s
->dma_adc
.count
&= s
->dma_adc
.fragsize
-1;
1635 spin_unlock_irqrestore(&s
->lock
, flags
);
1636 if (copy_to_user(argp
, &cinfo
, sizeof(cinfo
)))
1640 case SNDCTL_DSP_GETOPTR
:
1641 if (!(file
->f_mode
& FMODE_WRITE
))
1643 if (!s
->dma_dac2
.ready
&& (val
= prog_dmabuf_dac2(s
)) != 0)
1645 spin_lock_irqsave(&s
->lock
, flags
);
1646 es1370_update_ptr(s
);
1647 cinfo
.bytes
= s
->dma_dac2
.total_bytes
;
1648 count
= s
->dma_dac2
.count
;
1651 cinfo
.blocks
= count
>> s
->dma_dac2
.fragshift
;
1652 cinfo
.ptr
= s
->dma_dac2
.hwptr
;
1653 if (s
->dma_dac2
.mapped
)
1654 s
->dma_dac2
.count
&= s
->dma_dac2
.fragsize
-1;
1655 spin_unlock_irqrestore(&s
->lock
, flags
);
1656 if (copy_to_user(argp
, &cinfo
, sizeof(cinfo
)))
1660 case SNDCTL_DSP_GETBLKSIZE
:
1661 if (file
->f_mode
& FMODE_WRITE
) {
1662 if ((val
= prog_dmabuf_dac2(s
)))
1664 return put_user(s
->dma_dac2
.fragsize
, p
);
1666 if ((val
= prog_dmabuf_adc(s
)))
1668 return put_user(s
->dma_adc
.fragsize
, p
);
1670 case SNDCTL_DSP_SETFRAGMENT
:
1671 if (get_user(val
, p
))
1673 if (file
->f_mode
& FMODE_READ
) {
1674 s
->dma_adc
.ossfragshift
= val
& 0xffff;
1675 s
->dma_adc
.ossmaxfrags
= (val
>> 16) & 0xffff;
1676 if (s
->dma_adc
.ossfragshift
< 4)
1677 s
->dma_adc
.ossfragshift
= 4;
1678 if (s
->dma_adc
.ossfragshift
> 15)
1679 s
->dma_adc
.ossfragshift
= 15;
1680 if (s
->dma_adc
.ossmaxfrags
< 4)
1681 s
->dma_adc
.ossmaxfrags
= 4;
1683 if (file
->f_mode
& FMODE_WRITE
) {
1684 s
->dma_dac2
.ossfragshift
= val
& 0xffff;
1685 s
->dma_dac2
.ossmaxfrags
= (val
>> 16) & 0xffff;
1686 if (s
->dma_dac2
.ossfragshift
< 4)
1687 s
->dma_dac2
.ossfragshift
= 4;
1688 if (s
->dma_dac2
.ossfragshift
> 15)
1689 s
->dma_dac2
.ossfragshift
= 15;
1690 if (s
->dma_dac2
.ossmaxfrags
< 4)
1691 s
->dma_dac2
.ossmaxfrags
= 4;
1695 case SNDCTL_DSP_SUBDIVIDE
:
1696 if ((file
->f_mode
& FMODE_READ
&& s
->dma_adc
.subdivision
) ||
1697 (file
->f_mode
& FMODE_WRITE
&& s
->dma_dac2
.subdivision
))
1699 if (get_user(val
, p
))
1701 if (val
!= 1 && val
!= 2 && val
!= 4)
1703 if (file
->f_mode
& FMODE_READ
)
1704 s
->dma_adc
.subdivision
= val
;
1705 if (file
->f_mode
& FMODE_WRITE
)
1706 s
->dma_dac2
.subdivision
= val
;
1709 case SOUND_PCM_READ_RATE
:
1710 return put_user(DAC2_DIVTOSR((s
->ctrl
& CTRL_PCLKDIV
) >> CTRL_SH_PCLKDIV
), p
);
1712 case SOUND_PCM_READ_CHANNELS
:
1713 return put_user((s
->sctrl
& ((file
->f_mode
& FMODE_READ
) ? SCTRL_R1SMB
: SCTRL_P2SMB
)) ?
1716 case SOUND_PCM_READ_BITS
:
1717 return put_user((s
->sctrl
& ((file
->f_mode
& FMODE_READ
) ? SCTRL_R1SEB
: SCTRL_P2SEB
)) ?
1720 case SOUND_PCM_WRITE_FILTER
:
1721 case SNDCTL_DSP_SETSYNCRO
:
1722 case SOUND_PCM_READ_FILTER
:
1726 return mixer_ioctl(s
, cmd
, arg
);
1729 static int es1370_open(struct inode
*inode
, struct file
*file
)
1731 unsigned int minor
= iminor(inode
);
1732 DECLARE_WAITQUEUE(wait
, current
);
1733 unsigned long flags
;
1734 struct list_head
*list
;
1735 struct es1370_state
*s
;
1737 for (list
= devs
.next
; ; list
= list
->next
) {
1740 s
= list_entry(list
, struct es1370_state
, devs
);
1741 if (!((s
->dev_audio
^ minor
) & ~0xf))
1745 file
->private_data
= s
;
1746 /* wait for device to become free */
1748 while (s
->open_mode
& file
->f_mode
) {
1749 if (file
->f_flags
& O_NONBLOCK
) {
1753 add_wait_queue(&s
->open_wait
, &wait
);
1754 __set_current_state(TASK_INTERRUPTIBLE
);
1757 remove_wait_queue(&s
->open_wait
, &wait
);
1758 set_current_state(TASK_RUNNING
);
1759 if (signal_pending(current
))
1760 return -ERESTARTSYS
;
1763 spin_lock_irqsave(&s
->lock
, flags
);
1764 if (!(s
->open_mode
& (FMODE_READ
|FMODE_WRITE
)))
1765 s
->ctrl
= (s
->ctrl
& ~CTRL_PCLKDIV
) | (DAC2_SRTODIV(8000) << CTRL_SH_PCLKDIV
);
1766 if (file
->f_mode
& FMODE_READ
) {
1767 s
->dma_adc
.ossfragshift
= s
->dma_adc
.ossmaxfrags
= s
->dma_adc
.subdivision
= 0;
1768 s
->dma_adc
.enabled
= 1;
1769 s
->sctrl
&= ~SCTRL_R1FMT
;
1770 if ((minor
& 0xf) == SND_DEV_DSP16
)
1771 s
->sctrl
|= ES1370_FMT_S16_MONO
<< SCTRL_SH_R1FMT
;
1773 s
->sctrl
|= ES1370_FMT_U8_MONO
<< SCTRL_SH_R1FMT
;
1775 if (file
->f_mode
& FMODE_WRITE
) {
1776 s
->dma_dac2
.ossfragshift
= s
->dma_dac2
.ossmaxfrags
= s
->dma_dac2
.subdivision
= 0;
1777 s
->dma_dac2
.enabled
= 1;
1778 s
->sctrl
&= ~SCTRL_P2FMT
;
1779 if ((minor
& 0xf) == SND_DEV_DSP16
)
1780 s
->sctrl
|= ES1370_FMT_S16_MONO
<< SCTRL_SH_P2FMT
;
1782 s
->sctrl
|= ES1370_FMT_U8_MONO
<< SCTRL_SH_P2FMT
;
1784 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
1785 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
1786 spin_unlock_irqrestore(&s
->lock
, flags
);
1787 s
->open_mode
|= file
->f_mode
& (FMODE_READ
| FMODE_WRITE
);
1789 init_MUTEX(&s
->sem
);
1790 return nonseekable_open(inode
, file
);
1793 static int es1370_release(struct inode
*inode
, struct file
*file
)
1795 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1799 if (file
->f_mode
& FMODE_WRITE
)
1800 drain_dac2(s
, file
->f_flags
& O_NONBLOCK
);
1802 if (file
->f_mode
& FMODE_WRITE
) {
1804 synchronize_irq(s
->irq
);
1805 dealloc_dmabuf(s
, &s
->dma_dac2
);
1807 if (file
->f_mode
& FMODE_READ
) {
1809 dealloc_dmabuf(s
, &s
->dma_adc
);
1811 s
->open_mode
&= ~(file
->f_mode
& (FMODE_READ
|FMODE_WRITE
));
1812 wake_up(&s
->open_wait
);
1818 static /*const*/ struct file_operations es1370_audio_fops
= {
1819 .owner
= THIS_MODULE
,
1820 .llseek
= no_llseek
,
1821 .read
= es1370_read
,
1822 .write
= es1370_write
,
1823 .poll
= es1370_poll
,
1824 .ioctl
= es1370_ioctl
,
1825 .mmap
= es1370_mmap
,
1826 .open
= es1370_open
,
1827 .release
= es1370_release
,
1830 /* --------------------------------------------------------------------- */
1832 static ssize_t
es1370_write_dac(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
1834 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1835 DECLARE_WAITQUEUE(wait
, current
);
1837 unsigned long flags
;
1842 if (s
->dma_dac1
.mapped
)
1844 if (!s
->dma_dac1
.ready
&& (ret
= prog_dmabuf_dac1(s
)))
1846 if (!access_ok(VERIFY_READ
, buffer
, count
))
1848 add_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1850 spin_lock_irqsave(&s
->lock
, flags
);
1851 if (s
->dma_dac1
.count
< 0) {
1852 s
->dma_dac1
.count
= 0;
1853 s
->dma_dac1
.swptr
= s
->dma_dac1
.hwptr
;
1855 swptr
= s
->dma_dac1
.swptr
;
1856 cnt
= s
->dma_dac1
.dmasize
-swptr
;
1857 if (s
->dma_dac1
.count
+ cnt
> s
->dma_dac1
.dmasize
)
1858 cnt
= s
->dma_dac1
.dmasize
- s
->dma_dac1
.count
;
1860 __set_current_state(TASK_INTERRUPTIBLE
);
1861 spin_unlock_irqrestore(&s
->lock
, flags
);
1865 if (s
->dma_dac1
.enabled
)
1867 if (file
->f_flags
& O_NONBLOCK
) {
1873 if (signal_pending(current
)) {
1880 if (copy_from_user(s
->dma_dac1
.rawbuf
+ swptr
, buffer
, cnt
)) {
1885 swptr
= (swptr
+ cnt
) % s
->dma_dac1
.dmasize
;
1886 spin_lock_irqsave(&s
->lock
, flags
);
1887 s
->dma_dac1
.swptr
= swptr
;
1888 s
->dma_dac1
.count
+= cnt
;
1889 s
->dma_dac1
.endcleared
= 0;
1890 spin_unlock_irqrestore(&s
->lock
, flags
);
1894 if (s
->dma_dac1
.enabled
)
1897 remove_wait_queue(&s
->dma_dac1
.wait
, &wait
);
1898 set_current_state(TASK_RUNNING
);
1902 /* No kernel lock - we have our own spinlock */
1903 static unsigned int es1370_poll_dac(struct file
*file
, struct poll_table_struct
*wait
)
1905 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1906 unsigned long flags
;
1907 unsigned int mask
= 0;
1910 if (!s
->dma_dac1
.ready
&& prog_dmabuf_dac1(s
))
1912 poll_wait(file
, &s
->dma_dac1
.wait
, wait
);
1913 spin_lock_irqsave(&s
->lock
, flags
);
1914 es1370_update_ptr(s
);
1915 if (s
->dma_dac1
.mapped
) {
1916 if (s
->dma_dac1
.count
>= (signed)s
->dma_dac1
.fragsize
)
1917 mask
|= POLLOUT
| POLLWRNORM
;
1919 if ((signed)s
->dma_dac1
.dmasize
>= s
->dma_dac1
.count
+ (signed)s
->dma_dac1
.fragsize
)
1920 mask
|= POLLOUT
| POLLWRNORM
;
1922 spin_unlock_irqrestore(&s
->lock
, flags
);
1926 static int es1370_mmap_dac(struct file
*file
, struct vm_area_struct
*vma
)
1928 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1933 if (!(vma
->vm_flags
& VM_WRITE
))
1936 if ((ret
= prog_dmabuf_dac1(s
)) != 0)
1939 if (vma
->vm_pgoff
!= 0)
1941 size
= vma
->vm_end
- vma
->vm_start
;
1942 if (size
> (PAGE_SIZE
<< s
->dma_dac1
.buforder
))
1945 if (remap_pfn_range(vma
, vma
->vm_start
,
1946 virt_to_phys(s
->dma_dac1
.rawbuf
) >> PAGE_SHIFT
,
1947 size
, vma
->vm_page_prot
))
1949 s
->dma_dac1
.mapped
= 1;
1956 static int es1370_ioctl_dac(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1958 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
1959 unsigned long flags
;
1960 audio_buf_info abinfo
;
1965 int __user
*p
= (int __user
*)arg
;
1969 case OSS_GETVERSION
:
1970 return put_user(SOUND_VERSION
, p
);
1972 case SNDCTL_DSP_SYNC
:
1973 return drain_dac1(s
, 0/*file->f_flags & O_NONBLOCK*/);
1975 case SNDCTL_DSP_SETDUPLEX
:
1978 case SNDCTL_DSP_GETCAPS
:
1979 return put_user(DSP_CAP_REALTIME
| DSP_CAP_TRIGGER
| DSP_CAP_MMAP
, p
);
1981 case SNDCTL_DSP_RESET
:
1983 synchronize_irq(s
->irq
);
1984 s
->dma_dac1
.swptr
= s
->dma_dac1
.hwptr
= s
->dma_dac1
.count
= s
->dma_dac1
.total_bytes
= 0;
1987 case SNDCTL_DSP_SPEED
:
1988 if (get_user(val
, p
))
1992 s
->dma_dac1
.ready
= 0;
1993 for (ctrl
= 0; ctrl
<= 2; ctrl
++)
1994 if (val
< (dac1_samplerate
[ctrl
] + dac1_samplerate
[ctrl
+1]) / 2)
1996 spin_lock_irqsave(&s
->lock
, flags
);
1997 s
->ctrl
= (s
->ctrl
& ~CTRL_WTSRSEL
) | (ctrl
<< CTRL_SH_WTSRSEL
);
1998 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
1999 spin_unlock_irqrestore(&s
->lock
, flags
);
2001 return put_user(dac1_samplerate
[(s
->ctrl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
], p
);
2003 case SNDCTL_DSP_STEREO
:
2004 if (get_user(val
, p
))
2007 s
->dma_dac1
.ready
= 0;
2008 spin_lock_irqsave(&s
->lock
, flags
);
2010 s
->sctrl
|= SCTRL_P1SMB
;
2012 s
->sctrl
&= ~SCTRL_P1SMB
;
2013 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
2014 spin_unlock_irqrestore(&s
->lock
, flags
);
2017 case SNDCTL_DSP_CHANNELS
:
2018 if (get_user(val
, p
))
2021 if (s
->dma_dac1
.mapped
)
2024 s
->dma_dac1
.ready
= 0;
2025 spin_lock_irqsave(&s
->lock
, flags
);
2027 s
->sctrl
|= SCTRL_P1SMB
;
2029 s
->sctrl
&= ~SCTRL_P1SMB
;
2030 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
2031 spin_unlock_irqrestore(&s
->lock
, flags
);
2033 return put_user((s
->sctrl
& SCTRL_P1SMB
) ? 2 : 1, p
);
2035 case SNDCTL_DSP_GETFMTS
: /* Returns a mask */
2036 return put_user(AFMT_S16_LE
|AFMT_U8
, p
);
2038 case SNDCTL_DSP_SETFMT
: /* Selects ONE fmt*/
2039 if (get_user(val
, p
))
2041 if (val
!= AFMT_QUERY
) {
2043 s
->dma_dac1
.ready
= 0;
2044 spin_lock_irqsave(&s
->lock
, flags
);
2045 if (val
== AFMT_S16_LE
)
2046 s
->sctrl
|= SCTRL_P1SEB
;
2048 s
->sctrl
&= ~SCTRL_P1SEB
;
2049 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
2050 spin_unlock_irqrestore(&s
->lock
, flags
);
2052 return put_user((s
->sctrl
& SCTRL_P1SEB
) ? AFMT_S16_LE
: AFMT_U8
, p
);
2054 case SNDCTL_DSP_POST
:
2057 case SNDCTL_DSP_GETTRIGGER
:
2058 return put_user((s
->ctrl
& CTRL_DAC1_EN
) ? PCM_ENABLE_OUTPUT
: 0, p
);
2060 case SNDCTL_DSP_SETTRIGGER
:
2061 if (get_user(val
, p
))
2063 if (val
& PCM_ENABLE_OUTPUT
) {
2064 if (!s
->dma_dac1
.ready
&& (ret
= prog_dmabuf_dac1(s
)))
2066 s
->dma_dac1
.enabled
= 1;
2069 s
->dma_dac1
.enabled
= 0;
2074 case SNDCTL_DSP_GETOSPACE
:
2075 if (!s
->dma_dac1
.ready
&& (val
= prog_dmabuf_dac1(s
)) != 0)
2077 spin_lock_irqsave(&s
->lock
, flags
);
2078 es1370_update_ptr(s
);
2079 abinfo
.fragsize
= s
->dma_dac1
.fragsize
;
2080 count
= s
->dma_dac1
.count
;
2083 abinfo
.bytes
= s
->dma_dac1
.dmasize
- count
;
2084 abinfo
.fragstotal
= s
->dma_dac1
.numfrag
;
2085 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_dac1
.fragshift
;
2086 spin_unlock_irqrestore(&s
->lock
, flags
);
2087 return copy_to_user((void __user
*)arg
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
2089 case SNDCTL_DSP_NONBLOCK
:
2090 file
->f_flags
|= O_NONBLOCK
;
2093 case SNDCTL_DSP_GETODELAY
:
2094 if (!s
->dma_dac1
.ready
&& (val
= prog_dmabuf_dac1(s
)) != 0)
2096 spin_lock_irqsave(&s
->lock
, flags
);
2097 es1370_update_ptr(s
);
2098 count
= s
->dma_dac1
.count
;
2099 spin_unlock_irqrestore(&s
->lock
, flags
);
2102 return put_user(count
, p
);
2104 case SNDCTL_DSP_GETOPTR
:
2105 if (!s
->dma_dac1
.ready
&& (val
= prog_dmabuf_dac1(s
)) != 0)
2107 spin_lock_irqsave(&s
->lock
, flags
);
2108 es1370_update_ptr(s
);
2109 cinfo
.bytes
= s
->dma_dac1
.total_bytes
;
2110 count
= s
->dma_dac1
.count
;
2113 cinfo
.blocks
= count
>> s
->dma_dac1
.fragshift
;
2114 cinfo
.ptr
= s
->dma_dac1
.hwptr
;
2115 if (s
->dma_dac1
.mapped
)
2116 s
->dma_dac1
.count
&= s
->dma_dac1
.fragsize
-1;
2117 spin_unlock_irqrestore(&s
->lock
, flags
);
2118 if (copy_to_user((void __user
*)arg
, &cinfo
, sizeof(cinfo
)))
2122 case SNDCTL_DSP_GETBLKSIZE
:
2123 if ((val
= prog_dmabuf_dac1(s
)))
2125 return put_user(s
->dma_dac1
.fragsize
, p
);
2127 case SNDCTL_DSP_SETFRAGMENT
:
2128 if (get_user(val
, p
))
2130 s
->dma_dac1
.ossfragshift
= val
& 0xffff;
2131 s
->dma_dac1
.ossmaxfrags
= (val
>> 16) & 0xffff;
2132 if (s
->dma_dac1
.ossfragshift
< 4)
2133 s
->dma_dac1
.ossfragshift
= 4;
2134 if (s
->dma_dac1
.ossfragshift
> 15)
2135 s
->dma_dac1
.ossfragshift
= 15;
2136 if (s
->dma_dac1
.ossmaxfrags
< 4)
2137 s
->dma_dac1
.ossmaxfrags
= 4;
2140 case SNDCTL_DSP_SUBDIVIDE
:
2141 if (s
->dma_dac1
.subdivision
)
2143 if (get_user(val
, p
))
2145 if (val
!= 1 && val
!= 2 && val
!= 4)
2147 s
->dma_dac1
.subdivision
= val
;
2150 case SOUND_PCM_READ_RATE
:
2151 return put_user(dac1_samplerate
[(s
->ctrl
& CTRL_WTSRSEL
) >> CTRL_SH_WTSRSEL
], p
);
2153 case SOUND_PCM_READ_CHANNELS
:
2154 return put_user((s
->sctrl
& SCTRL_P1SMB
) ? 2 : 1, p
);
2156 case SOUND_PCM_READ_BITS
:
2157 return put_user((s
->sctrl
& SCTRL_P1SEB
) ? 16 : 8, p
);
2159 case SOUND_PCM_WRITE_FILTER
:
2160 case SNDCTL_DSP_SETSYNCRO
:
2161 case SOUND_PCM_READ_FILTER
:
2165 return mixer_ioctl(s
, cmd
, arg
);
2168 static int es1370_open_dac(struct inode
*inode
, struct file
*file
)
2170 unsigned int minor
= iminor(inode
);
2171 DECLARE_WAITQUEUE(wait
, current
);
2172 unsigned long flags
;
2173 struct list_head
*list
;
2174 struct es1370_state
*s
;
2176 for (list
= devs
.next
; ; list
= list
->next
) {
2179 s
= list_entry(list
, struct es1370_state
, devs
);
2180 if (!((s
->dev_dac
^ minor
) & ~0xf))
2184 /* we allow opening with O_RDWR, most programs do it although they will only write */
2186 if (file
->f_mode
& FMODE_READ
)
2189 if (!(file
->f_mode
& FMODE_WRITE
))
2191 file
->private_data
= s
;
2192 /* wait for device to become free */
2194 while (s
->open_mode
& FMODE_DAC
) {
2195 if (file
->f_flags
& O_NONBLOCK
) {
2199 add_wait_queue(&s
->open_wait
, &wait
);
2200 __set_current_state(TASK_INTERRUPTIBLE
);
2203 remove_wait_queue(&s
->open_wait
, &wait
);
2204 set_current_state(TASK_RUNNING
);
2205 if (signal_pending(current
))
2206 return -ERESTARTSYS
;
2209 s
->dma_dac1
.ossfragshift
= s
->dma_dac1
.ossmaxfrags
= s
->dma_dac1
.subdivision
= 0;
2210 s
->dma_dac1
.enabled
= 1;
2211 spin_lock_irqsave(&s
->lock
, flags
);
2212 s
->ctrl
= (s
->ctrl
& ~CTRL_WTSRSEL
) | (1 << CTRL_SH_WTSRSEL
);
2213 s
->sctrl
&= ~SCTRL_P1FMT
;
2214 if ((minor
& 0xf) == SND_DEV_DSP16
)
2215 s
->sctrl
|= ES1370_FMT_S16_MONO
<< SCTRL_SH_P1FMT
;
2217 s
->sctrl
|= ES1370_FMT_U8_MONO
<< SCTRL_SH_P1FMT
;
2218 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
2219 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
2220 spin_unlock_irqrestore(&s
->lock
, flags
);
2221 s
->open_mode
|= FMODE_DAC
;
2223 return nonseekable_open(inode
, file
);
2226 static int es1370_release_dac(struct inode
*inode
, struct file
*file
)
2228 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2232 drain_dac1(s
, file
->f_flags
& O_NONBLOCK
);
2235 dealloc_dmabuf(s
, &s
->dma_dac1
);
2236 s
->open_mode
&= ~FMODE_DAC
;
2237 wake_up(&s
->open_wait
);
2243 static /*const*/ struct file_operations es1370_dac_fops
= {
2244 .owner
= THIS_MODULE
,
2245 .llseek
= no_llseek
,
2246 .write
= es1370_write_dac
,
2247 .poll
= es1370_poll_dac
,
2248 .ioctl
= es1370_ioctl_dac
,
2249 .mmap
= es1370_mmap_dac
,
2250 .open
= es1370_open_dac
,
2251 .release
= es1370_release_dac
,
2254 /* --------------------------------------------------------------------- */
2256 static ssize_t
es1370_midi_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
2258 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2259 DECLARE_WAITQUEUE(wait
, current
);
2261 unsigned long flags
;
2266 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
2271 add_wait_queue(&s
->midi
.iwait
, &wait
);
2273 spin_lock_irqsave(&s
->lock
, flags
);
2275 cnt
= MIDIINBUF
- ptr
;
2276 if (s
->midi
.icnt
< cnt
)
2279 __set_current_state(TASK_INTERRUPTIBLE
);
2280 spin_unlock_irqrestore(&s
->lock
, flags
);
2284 if (file
->f_flags
& O_NONBLOCK
) {
2290 if (signal_pending(current
)) {
2297 if (copy_to_user(buffer
, s
->midi
.ibuf
+ ptr
, cnt
)) {
2302 ptr
= (ptr
+ cnt
) % MIDIINBUF
;
2303 spin_lock_irqsave(&s
->lock
, flags
);
2305 s
->midi
.icnt
-= cnt
;
2306 spin_unlock_irqrestore(&s
->lock
, flags
);
2312 __set_current_state(TASK_RUNNING
);
2313 remove_wait_queue(&s
->midi
.iwait
, &wait
);
2317 static ssize_t
es1370_midi_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
2319 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2320 DECLARE_WAITQUEUE(wait
, current
);
2322 unsigned long flags
;
2327 if (!access_ok(VERIFY_READ
, buffer
, count
))
2332 add_wait_queue(&s
->midi
.owait
, &wait
);
2334 spin_lock_irqsave(&s
->lock
, flags
);
2336 cnt
= MIDIOUTBUF
- ptr
;
2337 if (s
->midi
.ocnt
+ cnt
> MIDIOUTBUF
)
2338 cnt
= MIDIOUTBUF
- s
->midi
.ocnt
;
2340 __set_current_state(TASK_INTERRUPTIBLE
);
2341 es1370_handle_midi(s
);
2343 spin_unlock_irqrestore(&s
->lock
, flags
);
2347 if (file
->f_flags
& O_NONBLOCK
) {
2353 if (signal_pending(current
)) {
2360 if (copy_from_user(s
->midi
.obuf
+ ptr
, buffer
, cnt
)) {
2365 ptr
= (ptr
+ cnt
) % MIDIOUTBUF
;
2366 spin_lock_irqsave(&s
->lock
, flags
);
2368 s
->midi
.ocnt
+= cnt
;
2369 spin_unlock_irqrestore(&s
->lock
, flags
);
2373 spin_lock_irqsave(&s
->lock
, flags
);
2374 es1370_handle_midi(s
);
2375 spin_unlock_irqrestore(&s
->lock
, flags
);
2377 __set_current_state(TASK_RUNNING
);
2378 remove_wait_queue(&s
->midi
.owait
, &wait
);
2382 /* No kernel lock - we have our own spinlock */
2383 static unsigned int es1370_midi_poll(struct file
*file
, struct poll_table_struct
*wait
)
2385 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2386 unsigned long flags
;
2387 unsigned int mask
= 0;
2390 if (file
->f_mode
& FMODE_WRITE
)
2391 poll_wait(file
, &s
->midi
.owait
, wait
);
2392 if (file
->f_mode
& FMODE_READ
)
2393 poll_wait(file
, &s
->midi
.iwait
, wait
);
2394 spin_lock_irqsave(&s
->lock
, flags
);
2395 if (file
->f_mode
& FMODE_READ
) {
2396 if (s
->midi
.icnt
> 0)
2397 mask
|= POLLIN
| POLLRDNORM
;
2399 if (file
->f_mode
& FMODE_WRITE
) {
2400 if (s
->midi
.ocnt
< MIDIOUTBUF
)
2401 mask
|= POLLOUT
| POLLWRNORM
;
2403 spin_unlock_irqrestore(&s
->lock
, flags
);
2407 static int es1370_midi_open(struct inode
*inode
, struct file
*file
)
2409 unsigned int minor
= iminor(inode
);
2410 DECLARE_WAITQUEUE(wait
, current
);
2411 unsigned long flags
;
2412 struct list_head
*list
;
2413 struct es1370_state
*s
;
2415 for (list
= devs
.next
; ; list
= list
->next
) {
2418 s
= list_entry(list
, struct es1370_state
, devs
);
2419 if (s
->dev_midi
== minor
)
2423 file
->private_data
= s
;
2424 /* wait for device to become free */
2426 while (s
->open_mode
& (file
->f_mode
<< FMODE_MIDI_SHIFT
)) {
2427 if (file
->f_flags
& O_NONBLOCK
) {
2431 add_wait_queue(&s
->open_wait
, &wait
);
2432 __set_current_state(TASK_INTERRUPTIBLE
);
2435 remove_wait_queue(&s
->open_wait
, &wait
);
2436 set_current_state(TASK_RUNNING
);
2437 if (signal_pending(current
))
2438 return -ERESTARTSYS
;
2441 spin_lock_irqsave(&s
->lock
, flags
);
2442 if (!(s
->open_mode
& (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
))) {
2443 s
->midi
.ird
= s
->midi
.iwr
= s
->midi
.icnt
= 0;
2444 s
->midi
.ord
= s
->midi
.owr
= s
->midi
.ocnt
= 0;
2445 outb(UCTRL_CNTRL_SWR
, s
->io
+ES1370_REG_UART_CONTROL
);
2446 outb(0, s
->io
+ES1370_REG_UART_CONTROL
);
2447 outb(0, s
->io
+ES1370_REG_UART_TEST
);
2449 if (file
->f_mode
& FMODE_READ
) {
2450 s
->midi
.ird
= s
->midi
.iwr
= s
->midi
.icnt
= 0;
2452 if (file
->f_mode
& FMODE_WRITE
) {
2453 s
->midi
.ord
= s
->midi
.owr
= s
->midi
.ocnt
= 0;
2455 s
->ctrl
|= CTRL_UART_EN
;
2456 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
2457 es1370_handle_midi(s
);
2458 spin_unlock_irqrestore(&s
->lock
, flags
);
2459 s
->open_mode
|= (file
->f_mode
<< FMODE_MIDI_SHIFT
) & (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
);
2461 return nonseekable_open(inode
, file
);
2464 static int es1370_midi_release(struct inode
*inode
, struct file
*file
)
2466 struct es1370_state
*s
= (struct es1370_state
*)file
->private_data
;
2467 DECLARE_WAITQUEUE(wait
, current
);
2468 unsigned long flags
;
2469 unsigned count
, tmo
;
2474 if (file
->f_mode
& FMODE_WRITE
) {
2475 add_wait_queue(&s
->midi
.owait
, &wait
);
2477 __set_current_state(TASK_INTERRUPTIBLE
);
2478 spin_lock_irqsave(&s
->lock
, flags
);
2479 count
= s
->midi
.ocnt
;
2480 spin_unlock_irqrestore(&s
->lock
, flags
);
2483 if (signal_pending(current
))
2485 if (file
->f_flags
& O_NONBLOCK
)
2487 tmo
= (count
* HZ
) / 3100;
2488 if (!schedule_timeout(tmo
? : 1) && tmo
)
2489 DBG(printk(KERN_DEBUG
"es1370: midi timed out??\n");)
2491 remove_wait_queue(&s
->midi
.owait
, &wait
);
2492 set_current_state(TASK_RUNNING
);
2495 s
->open_mode
&= ~((file
->f_mode
<< FMODE_MIDI_SHIFT
) & (FMODE_MIDI_READ
|FMODE_MIDI_WRITE
));
2496 spin_lock_irqsave(&s
->lock
, flags
);
2497 if (!(s
->open_mode
& (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
))) {
2498 s
->ctrl
&= ~CTRL_UART_EN
;
2499 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
2501 spin_unlock_irqrestore(&s
->lock
, flags
);
2502 wake_up(&s
->open_wait
);
2508 static /*const*/ struct file_operations es1370_midi_fops
= {
2509 .owner
= THIS_MODULE
,
2510 .llseek
= no_llseek
,
2511 .read
= es1370_midi_read
,
2512 .write
= es1370_midi_write
,
2513 .poll
= es1370_midi_poll
,
2514 .open
= es1370_midi_open
,
2515 .release
= es1370_midi_release
,
2518 /* --------------------------------------------------------------------- */
2520 /* maximum number of devices; only used for command line params */
2523 static int lineout
[NR_DEVICE
];
2524 static int micbias
[NR_DEVICE
];
2526 static unsigned int devindex
;
2528 module_param_array(lineout
, bool, NULL
, 0);
2529 MODULE_PARM_DESC(lineout
, "if 1 the LINE input is converted to LINE out");
2530 module_param_array(micbias
, bool, NULL
, 0);
2531 MODULE_PARM_DESC(micbias
, "sets the +5V bias for an electret microphone");
2533 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
2534 MODULE_DESCRIPTION("ES1370 AudioPCI Driver");
2535 MODULE_LICENSE("GPL");
2538 /* --------------------------------------------------------------------- */
2540 static struct initvol
{
2543 } initvol
[] __devinitdata
= {
2544 { SOUND_MIXER_WRITE_VOLUME
, 0x4040 },
2545 { SOUND_MIXER_WRITE_PCM
, 0x4040 },
2546 { SOUND_MIXER_WRITE_SYNTH
, 0x4040 },
2547 { SOUND_MIXER_WRITE_CD
, 0x4040 },
2548 { SOUND_MIXER_WRITE_LINE
, 0x4040 },
2549 { SOUND_MIXER_WRITE_LINE1
, 0x4040 },
2550 { SOUND_MIXER_WRITE_LINE2
, 0x4040 },
2551 { SOUND_MIXER_WRITE_LINE3
, 0x4040 },
2552 { SOUND_MIXER_WRITE_MIC
, 0x4040 },
2553 { SOUND_MIXER_WRITE_OGAIN
, 0x4040 }
2556 static int __devinit
es1370_probe(struct pci_dev
*pcidev
, const struct pci_device_id
*pciid
)
2558 struct es1370_state
*s
;
2559 struct gameport
*gp
= NULL
;
2563 if ((ret
=pci_enable_device(pcidev
)))
2566 if ( !(pci_resource_flags(pcidev
, 0) & IORESOURCE_IO
) ||
2567 !pci_resource_start(pcidev
, 0)
2570 if (pcidev
->irq
== 0)
2572 i
= pci_set_dma_mask(pcidev
, 0xffffffff);
2574 printk(KERN_WARNING
"es1370: architecture does not support 32bit PCI busmaster DMA\n");
2577 if (!(s
= kmalloc(sizeof(struct es1370_state
), GFP_KERNEL
))) {
2578 printk(KERN_WARNING
"es1370: out of memory\n");
2581 memset(s
, 0, sizeof(struct es1370_state
));
2582 init_waitqueue_head(&s
->dma_adc
.wait
);
2583 init_waitqueue_head(&s
->dma_dac1
.wait
);
2584 init_waitqueue_head(&s
->dma_dac2
.wait
);
2585 init_waitqueue_head(&s
->open_wait
);
2586 init_waitqueue_head(&s
->midi
.iwait
);
2587 init_waitqueue_head(&s
->midi
.owait
);
2588 init_MUTEX(&s
->open_sem
);
2589 spin_lock_init(&s
->lock
);
2590 s
->magic
= ES1370_MAGIC
;
2592 s
->io
= pci_resource_start(pcidev
, 0);
2593 s
->irq
= pcidev
->irq
;
2594 if (!request_region(s
->io
, ES1370_EXTENT
, "es1370")) {
2595 printk(KERN_ERR
"es1370: io ports %#lx-%#lx in use\n", s
->io
, s
->io
+ES1370_EXTENT
-1);
2599 if ((ret
=request_irq(s
->irq
, es1370_interrupt
, SA_SHIRQ
, "es1370",s
))) {
2600 printk(KERN_ERR
"es1370: irq %u in use\n", s
->irq
);
2604 /* initialize codec registers */
2605 /* note: setting CTRL_SERR_DIS is reported to break
2606 * mic bias setting (by Kim.Berts@fisub.mail.abb.com) */
2607 s
->ctrl
= CTRL_CDC_EN
| (DAC2_SRTODIV(8000) << CTRL_SH_PCLKDIV
) | (1 << CTRL_SH_WTSRSEL
);
2608 if (!request_region(0x200, JOY_EXTENT
, "es1370")) {
2609 printk(KERN_ERR
"es1370: joystick io port 0x200 in use\n");
2610 } else if (!(s
->gameport
= gp
= gameport_allocate_port())) {
2611 printk(KERN_ERR
"es1370: can not allocate memory for gameport\n");
2612 release_region(0x200, JOY_EXTENT
);
2614 gameport_set_name(gp
, "ESS1370");
2615 gameport_set_phys(gp
, "pci%s/gameport0", pci_name(s
->dev
));
2616 gp
->dev
.parent
= &s
->dev
->dev
;
2618 s
->ctrl
|= CTRL_JYSTK_EN
;
2620 if (lineout
[devindex
])
2621 s
->ctrl
|= CTRL_XCTL0
;
2622 if (micbias
[devindex
])
2623 s
->ctrl
|= CTRL_XCTL1
;
2625 printk(KERN_INFO
"es1370: found adapter at io %#lx irq %u\n"
2626 KERN_INFO
"es1370: features: joystick %s, line %s, mic impedance %s\n",
2627 s
->io
, s
->irq
, (s
->ctrl
& CTRL_JYSTK_EN
) ? "on" : "off",
2628 (s
->ctrl
& CTRL_XCTL0
) ? "out" : "in",
2629 (s
->ctrl
& CTRL_XCTL1
) ? "1" : "0");
2630 /* register devices */
2631 if ((s
->dev_audio
= register_sound_dsp(&es1370_audio_fops
, -1)) < 0) {
2635 if ((s
->dev_mixer
= register_sound_mixer(&es1370_mixer_fops
, -1)) < 0) {
2639 if ((s
->dev_dac
= register_sound_dsp(&es1370_dac_fops
, -1)) < 0) {
2643 if ((s
->dev_midi
= register_sound_midi(&es1370_midi_fops
, -1)) < 0) {
2647 /* initialize the chips */
2648 outl(s
->ctrl
, s
->io
+ES1370_REG_CONTROL
);
2649 outl(s
->sctrl
, s
->io
+ES1370_REG_SERIAL_CONTROL
);
2650 /* point phantom write channel to "bugbuf" */
2651 s
->bugbuf_cpu
= pci_alloc_consistent(pcidev
,16,&s
->bugbuf_dma
);
2652 if (!s
->bugbuf_cpu
) {
2656 outl((ES1370_REG_PHANTOM_FRAMEADR
>> 8) & 15, s
->io
+ES1370_REG_MEMPAGE
);
2657 outl(s
->bugbuf_dma
, s
->io
+(ES1370_REG_PHANTOM_FRAMEADR
& 0xff));
2658 outl(0, s
->io
+(ES1370_REG_PHANTOM_FRAMECNT
& 0xff));
2659 pci_set_master(pcidev
); /* enable bus mastering */
2660 wrcodec(s
, 0x16, 3); /* no RST, PD */
2661 wrcodec(s
, 0x17, 0); /* CODEC ADC and CODEC DAC use {LR,B}CLK2 and run off the LRCLK2 PLL; program DAC_SYNC=0!! */
2662 wrcodec(s
, 0x18, 0); /* recording source is mixer */
2663 wrcodec(s
, 0x19, s
->mix
.micpreamp
= 1); /* turn on MIC preamp */
2667 val
= SOUND_MASK_LINE
|SOUND_MASK_SYNTH
|SOUND_MASK_CD
;
2668 mixer_ioctl(s
, SOUND_MIXER_WRITE_RECSRC
, (unsigned long)&val
);
2669 for (i
= 0; i
< sizeof(initvol
)/sizeof(initvol
[0]); i
++) {
2670 val
= initvol
[i
].vol
;
2671 mixer_ioctl(s
, initvol
[i
].mixch
, (unsigned long)&val
);
2675 /* register gameport */
2677 gameport_register_port(gp
);
2679 /* store it in the driver field */
2680 pci_set_drvdata(pcidev
, s
);
2681 /* put it into driver list */
2682 list_add_tail(&s
->devs
, &devs
);
2683 /* increment devindex */
2684 if (devindex
< NR_DEVICE
-1)
2689 unregister_sound_midi(s
->dev_midi
);
2691 unregister_sound_dsp(s
->dev_dac
);
2693 unregister_sound_mixer(s
->dev_mixer
);
2695 unregister_sound_dsp(s
->dev_audio
);
2697 printk(KERN_ERR
"es1370: cannot register misc device\n");
2698 free_irq(s
->irq
, s
);
2700 release_region(s
->gameport
->io
, JOY_EXTENT
);
2701 gameport_free_port(s
->gameport
);
2704 release_region(s
->io
, ES1370_EXTENT
);
2710 static void __devexit
es1370_remove(struct pci_dev
*dev
)
2712 struct es1370_state
*s
= pci_get_drvdata(dev
);
2717 outl(CTRL_SERR_DIS
| (1 << CTRL_SH_WTSRSEL
), s
->io
+ES1370_REG_CONTROL
); /* switch everything off */
2718 outl(0, s
->io
+ES1370_REG_SERIAL_CONTROL
); /* clear serial interrupts */
2719 synchronize_irq(s
->irq
);
2720 free_irq(s
->irq
, s
);
2722 int gpio
= s
->gameport
->io
;
2723 gameport_unregister_port(s
->gameport
);
2724 release_region(gpio
, JOY_EXTENT
);
2726 release_region(s
->io
, ES1370_EXTENT
);
2727 unregister_sound_dsp(s
->dev_audio
);
2728 unregister_sound_mixer(s
->dev_mixer
);
2729 unregister_sound_dsp(s
->dev_dac
);
2730 unregister_sound_midi(s
->dev_midi
);
2731 pci_free_consistent(dev
, 16, s
->bugbuf_cpu
, s
->bugbuf_dma
);
2733 pci_set_drvdata(dev
, NULL
);
2736 static struct pci_device_id id_table
[] = {
2737 { PCI_VENDOR_ID_ENSONIQ
, PCI_DEVICE_ID_ENSONIQ_ES1370
, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0 },
2741 MODULE_DEVICE_TABLE(pci
, id_table
);
2743 static struct pci_driver es1370_driver
= {
2745 .id_table
= id_table
,
2746 .probe
= es1370_probe
,
2747 .remove
= __devexit_p(es1370_remove
),
2750 static int __init
init_es1370(void)
2752 printk(KERN_INFO
"es1370: version v0.38 time " __TIME__
" " __DATE__
"\n");
2753 return pci_module_init(&es1370_driver
);
2756 static void __exit
cleanup_es1370(void)
2758 printk(KERN_INFO
"es1370: unloading\n");
2759 pci_unregister_driver(&es1370_driver
);
2762 module_init(init_es1370
);
2763 module_exit(cleanup_es1370
);
2765 /* --------------------------------------------------------------------- */
2769 /* format is: es1370=lineout[,micbias]] */
2771 static int __init
es1370_setup(char *str
)
2773 static unsigned __initdata nr_dev
= 0;
2775 if (nr_dev
>= NR_DEVICE
)
2779 ((get_option(&str
,&lineout
[nr_dev
]) == 2)
2780 && get_option(&str
,&micbias
[nr_dev
])
2787 __setup("es1370=", es1370_setup
);