Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / sound / es1370.c
blob885612f8ce5a693970d7ac19b19282553a6fcd84
1 /*****************************************************************************/
3 /*
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
30 * not ensure that.
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
40 * clock source)
42 * Supported devices:
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.
54 * Revision history
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
59 * settings
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
66 * miscdevice
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
94 * other than i386
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.
125 * 5512sps: 254
126 * 44100sps: 30
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>
154 #include <asm/io.h>
155 #include <asm/dma.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
165 #define DBG(x) {}
166 /*#define DBG(x) {x}*/
168 /* --------------------------------------------------------------------- */
170 #ifndef PCI_VENDOR_ID_ENSONIQ
171 #define PCI_VENDOR_ID_ENSONIQ 0x1274
172 #endif
173 #ifndef PCI_DEVICE_ID_ENSONIQ_ES1370
174 #define PCI_DEVICE_ID_ENSONIQ_ES1370 0x5000
175 #endif
177 #define ES1370_MAGIC ((PCI_VENDOR_ID_ENSONIQ<<16)|PCI_DEVICE_ID_ENSONIQ_ES1370)
179 #define ES1370_EXTENT 0x40
180 #define JOY_EXTENT 8
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 */
247 #define STAT_SH_VC 5
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
292 /* misc stuff */
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 {
308 /* magic */
309 unsigned int magic;
311 /* list of es1370 devices */
312 struct list_head devs;
314 /* the corresponding pci_dev structure */
315 struct pci_dev *dev;
317 /* soundcore stuff */
318 int dev_audio;
319 int dev_mixer;
320 int dev_dac;
321 int dev_midi;
323 /* hardware resources */
324 unsigned long io; /* long for SPARC */
325 unsigned int irq;
327 /* mixer registers; there is no HW readback */
328 struct {
329 unsigned short vol[10];
330 unsigned int recsrc;
331 unsigned int modcnt;
332 unsigned short micpreamp;
333 unsigned int imix;
334 } mix;
336 /* wave stuff */
337 unsigned ctrl;
338 unsigned sctrl;
340 spinlock_t lock;
341 struct semaphore open_sem;
342 mode_t open_mode;
343 wait_queue_head_t open_wait;
345 struct dmabuf {
346 void *rawbuf;
347 dma_addr_t dmaaddr;
348 unsigned buforder;
349 unsigned numfrag;
350 unsigned fragshift;
351 unsigned hwptr, swptr;
352 unsigned total_bytes;
353 int count;
354 unsigned error; /* over/underrun */
355 wait_queue_head_t wait;
356 /* redundant, but makes calculations easier */
357 unsigned fragsize;
358 unsigned dmasize;
359 unsigned fragsamples;
360 /* OSS stuff */
361 unsigned mapped:1;
362 unsigned ready:1;
363 unsigned endcleared:1;
364 unsigned ossfragshift;
365 int ossmaxfrags;
366 unsigned subdivision;
367 } dma_dac1, dma_dac2, dma_adc;
369 /* midi stuff */
370 struct {
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];
377 } midi;
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)
395 unsigned r = 0;
397 if (x >= 0x10000) {
398 x >>= 16;
399 r += 16;
401 if (x >= 0x100) {
402 x >>= 8;
403 r += 8;
405 if (x >= 0x10) {
406 x >>= 4;
407 r += 4;
409 if (x >= 4) {
410 x >>= 2;
411 r += 2;
413 if (x >= 2)
414 r++;
415 return r;
418 /* --------------------------------------------------------------------- */
420 static void wrcodec(struct es1370_state *s, unsigned char idx, unsigned char data)
422 unsigned long tmo = jiffies + HZ/10;
424 do {
425 if (!(inl(s->io+ES1370_REG_STATUS) & STAT_CSTAT)) {
426 outw((((unsigned short)idx)<<8)|data, s->io+ES1370_REG_CODEC);
427 return;
429 schedule();
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)
438 unsigned long flags;
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)
448 unsigned long flags;
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)
458 unsigned long flags;
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)
468 unsigned long flags;
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)
490 unsigned long flags;
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)
515 unsigned long flags;
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;
544 if (db->rawbuf) {
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);
551 db->rawbuf = NULL;
552 db->mapped = db->ready = 0;
555 static int prog_dmabuf(struct es1370_state *s, struct dmabuf *db, unsigned rate, unsigned fmt, unsigned reg)
557 int order;
558 unsigned bytepersec;
559 unsigned bufs;
560 struct page *page, *pend;
562 db->hwptr = db->swptr = db->total_bytes = db->count = db->error = db->endcleared = 0;
563 if (!db->rawbuf) {
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)))
567 break;
568 if (!db->rawbuf)
569 return -ENOMEM;
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);
582 else
583 db->fragshift = db->ossfragshift;
584 } else {
585 db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1));
586 if (db->fragshift < 3)
587 db->fragshift = 3;
589 db->numfrag = bufs >> db->fragshift;
590 while (db->numfrag < 4 && db->fragshift > 3) {
591 db->fragshift--;
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));
603 db->ready = 1;
604 return 0;
607 extern inline int prog_dmabuf_adc(struct es1370_state *s)
609 stop_adc(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)
616 stop_dac2(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)
623 stop_dac1(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;
635 db->hwptr = hwptr;
636 return diff;
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);
644 bptr = 0;
645 len -= x;
647 memset(((char *)buf) + bptr, c, len);
650 /* call with spinlock held! */
651 static void es1370_update_ptr(struct es1370_state *s)
653 int diff;
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);
666 s->dma_adc.error++;
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);
678 } else {
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);
683 s->dma_dac1.error++;
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);
701 } else {
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);
706 s->dma_dac2.error++;
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)
721 unsigned char ch;
722 int wake;
724 if (!(s->ctrl & CTRL_UART_EN))
725 return;
726 wake = 0;
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;
732 s->midi.icnt++;
734 wake = 1;
736 if (wake)
737 wake_up(&s->midi.iwait);
738 wake = 0;
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;
742 s->midi.ocnt--;
743 if (s->midi.ocnt < MIDIOUTBUF-16)
744 wake = 1;
746 if (wake)
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))
759 return;
760 spin_lock(&s->lock);
761 /* clear audio interrupts first */
762 sctl = s->sctrl;
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) \
781 ({ \
782 if (!(s) || (s)->magic != ES1370_MAGIC) { \
783 printk(invalid_magic); \
784 return -ENXIO; \
788 /* --------------------------------------------------------------------- */
790 static const struct {
791 unsigned volidx:4;
792 unsigned left:4;
793 unsigned right:4;
794 unsigned stereo:1;
795 unsigned recmask:13;
796 unsigned avail:1;
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)
812 unsigned int i, j;
814 for (j = i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
815 if (!(val & (1 << i)))
816 continue;
817 if (!mixtable[i].recmask) {
818 val &= ~(1 << i);
819 continue;
821 j |= mixtable[i].recmask;
823 s->mix.recsrc = val;
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;
829 if (!s->mix.imix) {
830 i &= 0xff60; /* mute record and line monitor */
832 wrcodec(s, 0x10, i);
833 wrcodec(s, 0x11, i >> 8);
836 static int mixer_ioctl(struct es1370_state *s, unsigned int cmd, unsigned long arg)
838 unsigned long flags;
839 int i, val;
840 unsigned char l, r, rl, rr;
842 VALIDATE_STATE(s);
843 if (cmd == SOUND_MIXER_PRIVATE1) {
844 /* enable/disable/query mixer preamp */
845 if (get_user(val, (int *)arg))
846 return -EFAULT;
847 if (val != -1) {
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))
856 return -EFAULT;
857 if (val != -1) {
858 spin_lock_irqsave(&s->lock, flags);
859 if (val)
860 s->ctrl |= CTRL_XCTL0;
861 else
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))
871 return -EFAULT;
872 if (val != -1) {
873 spin_lock_irqsave(&s->lock, flags);
874 if (val)
875 s->ctrl |= CTRL_XCTL1;
876 else
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) {
884 mixer_info 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)))
889 return -EFAULT;
890 return 0;
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)))
897 return -EFAULT;
898 return 0;
900 if (cmd == OSS_GETVERSION)
901 return put_user(SOUND_VERSION, (int *)arg);
902 if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
903 return -EINVAL;
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)
913 val |= 1 << i;
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)
919 val |= 1 << i;
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)
925 val |= 1 << i;
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);
934 default:
935 i = _IOC_NR(cmd);
936 if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].avail)
937 return -EINVAL;
938 return put_user(s->mix.vol[mixtable[i].volidx], (int *)arg);
941 if (_SIOC_DIR(cmd) != (_SIOC_READ|_SIOC_WRITE))
942 return -EINVAL;
943 s->mix.modcnt++;
944 switch (_IOC_NR(cmd)) {
946 case SOUND_MIXER_IMIX:
947 if (get_user(s->mix.imix, (int *)arg))
948 return -EFAULT;
949 set_recsrc(s, s->mix.recsrc);
950 return 0;
952 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
953 if (get_user(val, (int *)arg))
954 return -EFAULT;
955 set_recsrc(s, val);
956 return 0;
958 default:
959 i = _IOC_NR(cmd);
960 if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].avail)
961 return -EINVAL;
962 if (get_user(val, (int *)arg))
963 return -EFAULT;
964 l = val & 0xff;
965 if (l > 100)
966 l = 100;
967 if (mixtable[i].stereo) {
968 r = (val >> 8) & 0xff;
969 if (r > 100)
970 r = 100;
971 if (l < 7) {
972 rl = 0x80;
973 l = 0;
974 } else {
975 rl = 31 - ((l - 7) / 3);
976 l = (31 - rl) * 3 + 7;
978 if (r < 7) {
979 rr = 0x80;
980 r = 0;
981 } else {
982 rr = 31 - ((r - 7) / 3);
983 r = (31 - rr) * 3 + 7;
985 wrcodec(s, mixtable[i].right, rr);
986 } else {
987 if (mixtable[i].left == 15) {
988 if (l < 2) {
989 rr = rl = 0x80;
990 r = l = 0;
991 } else {
992 rl = 7 - ((l - 2) / 14);
993 r = l = (7 - rl) * 14 + 2;
995 } else {
996 if (l < 7) {
997 rl = 0x80;
998 r = l = 0;
999 } else {
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;
1008 #else
1009 s->mix.vol[mixtable[i].volidx] = val;
1010 #endif
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)
1019 return -ESPIPE;
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) {
1031 if (list == &devs)
1032 return -ENODEV;
1033 s = list_entry(list, struct es1370_state, devs);
1034 if (s->dev_mixer == minor)
1035 break;
1037 VALIDATE_STATE(s);
1038 file->private_data = s;
1039 return 0;
1042 static int es1370_release_mixdev(struct inode *inode, struct file *file)
1044 struct es1370_state *s = (struct es1370_state *)file->private_data;
1046 VALIDATE_STATE(s);
1047 return 0;
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 = {
1056 owner: THIS_MODULE,
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;
1069 int count, tmo;
1071 if (s->dma_dac1.mapped || !s->dma_dac1.ready)
1072 return 0;
1073 add_wait_queue(&s->dma_dac1.wait, &wait);
1074 for (;;) {
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);
1079 if (count <= 0)
1080 break;
1081 if (signal_pending(current))
1082 break;
1083 if (nonblock) {
1084 remove_wait_queue(&s->dma_dac1.wait, &wait);
1085 set_current_state(TASK_RUNNING);
1086 return -EBUSY;
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;
1098 return 0;
1101 static int drain_dac2(struct es1370_state *s, int nonblock)
1103 DECLARE_WAITQUEUE(wait, current);
1104 unsigned long flags;
1105 int count, tmo;
1107 if (s->dma_dac2.mapped || !s->dma_dac2.ready)
1108 return 0;
1109 add_wait_queue(&s->dma_dac2.wait, &wait);
1110 for (;;) {
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);
1115 if (count <= 0)
1116 break;
1117 if (signal_pending(current))
1118 break;
1119 if (nonblock) {
1120 remove_wait_queue(&s->dma_dac2.wait, &wait);
1121 set_current_state(TASK_RUNNING);
1122 return -EBUSY;
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;
1134 return 0;
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);
1143 ssize_t ret;
1144 unsigned long flags;
1145 unsigned swptr;
1146 int cnt;
1148 VALIDATE_STATE(s);
1149 if (ppos != &file->f_pos)
1150 return -ESPIPE;
1151 if (s->dma_adc.mapped)
1152 return -ENXIO;
1153 if (!s->dma_adc.ready && (ret = prog_dmabuf_adc(s)))
1154 return ret;
1155 if (!access_ok(VERIFY_WRITE, buffer, count))
1156 return -EFAULT;
1157 ret = 0;
1158 add_wait_queue(&s->dma_adc.wait, &wait);
1159 while (count > 0) {
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;
1165 if (cnt <= 0)
1166 __set_current_state(TASK_INTERRUPTIBLE);
1167 spin_unlock_irqrestore(&s->lock, flags);
1168 if (cnt > count)
1169 cnt = count;
1170 if (cnt <= 0) {
1171 start_adc(s);
1172 if (file->f_flags & O_NONBLOCK) {
1173 if (!ret)
1174 ret = -EAGAIN;
1175 break;
1177 schedule();
1178 if (signal_pending(current)) {
1179 if (!ret)
1180 ret = -ERESTARTSYS;
1181 break;
1183 continue;
1185 if (copy_to_user(buffer, s->dma_adc.rawbuf + swptr, cnt)) {
1186 if (!ret)
1187 ret = -EFAULT;
1188 break;
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);
1195 count -= cnt;
1196 buffer += cnt;
1197 ret += cnt;
1198 start_adc(s);
1200 remove_wait_queue(&s->dma_adc.wait, &wait);
1201 set_current_state(TASK_RUNNING);
1202 return ret;
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);
1209 ssize_t ret;
1210 unsigned long flags;
1211 unsigned swptr;
1212 int cnt;
1214 VALIDATE_STATE(s);
1215 if (ppos != &file->f_pos)
1216 return -ESPIPE;
1217 if (s->dma_dac2.mapped)
1218 return -ENXIO;
1219 if (!s->dma_dac2.ready && (ret = prog_dmabuf_dac2(s)))
1220 return ret;
1221 if (!access_ok(VERIFY_READ, buffer, count))
1222 return -EFAULT;
1223 ret = 0;
1224 add_wait_queue(&s->dma_dac2.wait, &wait);
1225 while (count > 0) {
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;
1235 if (cnt <= 0)
1236 __set_current_state(TASK_INTERRUPTIBLE);
1237 spin_unlock_irqrestore(&s->lock, flags);
1238 if (cnt > count)
1239 cnt = count;
1240 if (cnt <= 0) {
1241 start_dac2(s);
1242 if (file->f_flags & O_NONBLOCK) {
1243 if (!ret)
1244 ret = -EAGAIN;
1245 break;
1247 schedule();
1248 if (signal_pending(current)) {
1249 if (!ret)
1250 ret = -ERESTARTSYS;
1251 break;
1253 continue;
1255 if (copy_from_user(s->dma_dac2.rawbuf + swptr, buffer, cnt)) {
1256 if (!ret)
1257 ret = -EFAULT;
1258 break;
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);
1266 count -= cnt;
1267 buffer += cnt;
1268 ret += cnt;
1269 start_dac2(s);
1271 remove_wait_queue(&s->dma_dac2.wait, &wait);
1272 set_current_state(TASK_RUNNING);
1273 return ret;
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;
1283 VALIDATE_STATE(s);
1284 if (file->f_mode & FMODE_WRITE) {
1285 if (!s->dma_dac2.ready && prog_dmabuf_dac2(s))
1286 return 0;
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))
1291 return 0;
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;
1304 } else {
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);
1310 return mask;
1313 static int es1370_mmap(struct file *file, struct vm_area_struct *vma)
1315 struct es1370_state *s = (struct es1370_state *)file->private_data;
1316 struct dmabuf *db;
1317 int ret;
1318 unsigned long size;
1320 VALIDATE_STATE(s);
1321 lock_kernel();
1322 if (vma->vm_flags & VM_WRITE) {
1323 if ((ret = prog_dmabuf_dac2(s)) != 0) {
1324 unlock_kernel();
1325 return ret;
1327 db = &s->dma_dac2;
1328 } else if (vma->vm_flags & VM_READ) {
1329 if ((ret = prog_dmabuf_adc(s)) != 0) {
1330 unlock_kernel();
1331 return ret;
1333 db = &s->dma_adc;
1334 } else {
1335 unlock_kernel();
1336 return -EINVAL;
1338 if (vma->vm_pgoff != 0) {
1339 unlock_kernel();
1340 return -EINVAL;
1342 size = vma->vm_end - vma->vm_start;
1343 if (size > (PAGE_SIZE << db->buforder)) {
1344 unlock_kernel();
1345 return -EINVAL;
1347 if (remap_page_range(vma->vm_start, virt_to_phys(db->rawbuf), size, vma->vm_page_prot)) {
1348 unlock_kernel();
1349 return -EAGAIN;
1351 db->mapped = 1;
1352 unlock_kernel();
1353 return 0;
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;
1361 count_info cinfo;
1362 int count;
1363 int val, mapped, ret;
1365 VALIDATE_STATE(s);
1366 mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac2.mapped) ||
1367 ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
1368 switch (cmd) {
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*/);
1375 return 0;
1377 case SNDCTL_DSP_SETDUPLEX:
1378 return 0;
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) {
1385 stop_dac2(s);
1386 synchronize_irq();
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) {
1390 stop_adc(s);
1391 synchronize_irq();
1392 s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes = 0;
1394 return 0;
1396 case SNDCTL_DSP_SPEED:
1397 if (get_user(val, (int *)arg))
1398 return -EFAULT;
1399 if (val >= 0) {
1400 if (s->open_mode & (~file->f_mode) & (FMODE_READ|FMODE_WRITE))
1401 return -EINVAL;
1402 if (val < 4000)
1403 val = 4000;
1404 if (val > 50000)
1405 val = 50000;
1406 stop_adc(s);
1407 stop_dac2(s);
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))
1418 return -EFAULT;
1419 if (file->f_mode & FMODE_READ) {
1420 stop_adc(s);
1421 s->dma_adc.ready = 0;
1422 spin_lock_irqsave(&s->lock, flags);
1423 if (val)
1424 s->sctrl |= SCTRL_R1SMB;
1425 else
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) {
1431 stop_dac2(s);
1432 s->dma_dac2.ready = 0;
1433 spin_lock_irqsave(&s->lock, flags);
1434 if (val)
1435 s->sctrl |= SCTRL_P2SMB;
1436 else
1437 s->sctrl &= ~SCTRL_P2SMB;
1438 outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
1439 spin_unlock_irqrestore(&s->lock, flags);
1441 return 0;
1443 case SNDCTL_DSP_CHANNELS:
1444 if (get_user(val, (int *)arg))
1445 return -EFAULT;
1446 if (val != 0) {
1447 if (file->f_mode & FMODE_READ) {
1448 stop_adc(s);
1449 s->dma_adc.ready = 0;
1450 spin_lock_irqsave(&s->lock, flags);
1451 if (val >= 2)
1452 s->sctrl |= SCTRL_R1SMB;
1453 else
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) {
1459 stop_dac2(s);
1460 s->dma_dac2.ready = 0;
1461 spin_lock_irqsave(&s->lock, flags);
1462 if (val >= 2)
1463 s->sctrl |= SCTRL_P2SMB;
1464 else
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))
1477 return -EFAULT;
1478 if (val != AFMT_QUERY) {
1479 if (file->f_mode & FMODE_READ) {
1480 stop_adc(s);
1481 s->dma_adc.ready = 0;
1482 spin_lock_irqsave(&s->lock, flags);
1483 if (val == AFMT_S16_LE)
1484 s->sctrl |= SCTRL_R1SEB;
1485 else
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) {
1491 stop_dac2(s);
1492 s->dma_dac2.ready = 0;
1493 spin_lock_irqsave(&s->lock, flags);
1494 if (val == AFMT_S16_LE)
1495 s->sctrl |= SCTRL_P2SEB;
1496 else
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:
1506 return 0;
1508 case SNDCTL_DSP_GETTRIGGER:
1509 val = 0;
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))
1518 return -EFAULT;
1519 if (file->f_mode & FMODE_READ) {
1520 if (val & PCM_ENABLE_INPUT) {
1521 if (!s->dma_adc.ready && (ret = prog_dmabuf_adc(s)))
1522 return ret;
1523 start_adc(s);
1524 } else
1525 stop_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)))
1530 return ret;
1531 start_dac2(s);
1532 } else
1533 stop_dac2(s);
1535 return 0;
1537 case SNDCTL_DSP_GETOSPACE:
1538 if (!(file->f_mode & FMODE_WRITE))
1539 return -EINVAL;
1540 if (!s->dma_dac2.ready && (ret = prog_dmabuf_dac2(s)))
1541 return ret;
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;
1546 if (count < 0)
1547 count = 0;
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))
1556 return -EINVAL;
1557 if (!s->dma_adc.ready && (ret = prog_dmabuf_adc(s)))
1558 return ret;
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;
1563 if (count < 0)
1564 count = 0;
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;
1573 return 0;
1575 case SNDCTL_DSP_GETODELAY:
1576 if (!(file->f_mode & FMODE_WRITE))
1577 return -EINVAL;
1578 if (!s->dma_dac2.ready && (ret = prog_dmabuf_dac2(s)))
1579 return ret;
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);
1584 if (count < 0)
1585 count = 0;
1586 return put_user(count, (int *)arg);
1588 case SNDCTL_DSP_GETIPTR:
1589 if (!(file->f_mode & FMODE_READ))
1590 return -EINVAL;
1591 if (!s->dma_adc.ready && (ret = prog_dmabuf_adc(s)))
1592 return ret;
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;
1597 if (count < 0)
1598 count = 0;
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))
1608 return -EINVAL;
1609 if (!s->dma_dac2.ready && (ret = prog_dmabuf_dac2(s)))
1610 return ret;
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;
1615 if (count < 0)
1616 count = 0;
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)))
1627 return val;
1628 return put_user(s->dma_dac2.fragsize, (int *)arg);
1630 if ((val = prog_dmabuf_adc(s)))
1631 return val;
1632 return put_user(s->dma_adc.fragsize, (int *)arg);
1634 case SNDCTL_DSP_SETFRAGMENT:
1635 if (get_user(val, (int *)arg))
1636 return -EFAULT;
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;
1657 return 0;
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))
1662 return -EINVAL;
1663 if (get_user(val, (int *)arg))
1664 return -EFAULT;
1665 if (val != 1 && val != 2 && val != 4)
1666 return -EINVAL;
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;
1671 return 0;
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)) ?
1678 2 : 1, (int *)arg);
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:
1687 return -EINVAL;
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) {
1702 if (list == &devs)
1703 return -ENODEV;
1704 s = list_entry(list, struct es1370_state, devs);
1705 if (!((s->dev_audio ^ minor) & ~0xf))
1706 break;
1708 VALIDATE_STATE(s);
1709 file->private_data = s;
1710 /* wait for device to become free */
1711 down(&s->open_sem);
1712 while (s->open_mode & file->f_mode) {
1713 if (file->f_flags & O_NONBLOCK) {
1714 up(&s->open_sem);
1715 return -EBUSY;
1717 add_wait_queue(&s->open_wait, &wait);
1718 __set_current_state(TASK_INTERRUPTIBLE);
1719 up(&s->open_sem);
1720 schedule();
1721 remove_wait_queue(&s->open_wait, &wait);
1722 set_current_state(TASK_RUNNING);
1723 if (signal_pending(current))
1724 return -ERESTARTSYS;
1725 down(&s->open_sem);
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;
1735 else
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;
1743 else
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);
1750 up(&s->open_sem);
1751 return 0;
1754 static int es1370_release(struct inode *inode, struct file *file)
1756 struct es1370_state *s = (struct es1370_state *)file->private_data;
1758 VALIDATE_STATE(s);
1759 lock_kernel();
1760 if (file->f_mode & FMODE_WRITE)
1761 drain_dac2(s, file->f_flags & O_NONBLOCK);
1762 down(&s->open_sem);
1763 if (file->f_mode & FMODE_WRITE) {
1764 stop_dac2(s);
1765 synchronize_irq();
1766 dealloc_dmabuf(s, &s->dma_dac2);
1768 if (file->f_mode & FMODE_READ) {
1769 stop_adc(s);
1770 dealloc_dmabuf(s, &s->dma_adc);
1772 s->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
1773 wake_up(&s->open_wait);
1774 up(&s->open_sem);
1775 return 0;
1776 unlock_kernel();
1779 static /*const*/ struct file_operations es1370_audio_fops = {
1780 owner: THIS_MODULE,
1781 llseek: es1370_llseek,
1782 read: es1370_read,
1783 write: es1370_write,
1784 poll: es1370_poll,
1785 ioctl: es1370_ioctl,
1786 mmap: es1370_mmap,
1787 open: es1370_open,
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);
1797 ssize_t ret = 0;
1798 unsigned long flags;
1799 unsigned swptr;
1800 int cnt;
1802 VALIDATE_STATE(s);
1803 if (ppos != &file->f_pos)
1804 return -ESPIPE;
1805 if (s->dma_dac1.mapped)
1806 return -ENXIO;
1807 if (!s->dma_dac1.ready && (ret = prog_dmabuf_dac1(s)))
1808 return ret;
1809 if (!access_ok(VERIFY_READ, buffer, count))
1810 return -EFAULT;
1811 add_wait_queue(&s->dma_dac1.wait, &wait);
1812 while (count > 0) {
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;
1822 if (cnt <= 0)
1823 __set_current_state(TASK_INTERRUPTIBLE);
1824 spin_unlock_irqrestore(&s->lock, flags);
1825 if (cnt > count)
1826 cnt = count;
1827 if (cnt <= 0) {
1828 start_dac1(s);
1829 if (file->f_flags & O_NONBLOCK) {
1830 if (!ret)
1831 ret = -EAGAIN;
1832 break;
1834 schedule();
1835 if (signal_pending(current)) {
1836 if (!ret)
1837 ret = -ERESTARTSYS;
1838 break;
1840 continue;
1842 if (copy_from_user(s->dma_dac1.rawbuf + swptr, buffer, cnt)) {
1843 if (!ret)
1844 ret = -EFAULT;
1845 break;
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);
1853 count -= cnt;
1854 buffer += cnt;
1855 ret += cnt;
1856 start_dac1(s);
1858 remove_wait_queue(&s->dma_dac1.wait, &wait);
1859 set_current_state(TASK_RUNNING);
1860 return ret;
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;
1870 VALIDATE_STATE(s);
1871 if (!s->dma_dac1.ready && prog_dmabuf_dac1(s))
1872 return 0;
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;
1879 } else {
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);
1884 return mask;
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;
1890 int ret;
1891 unsigned long size;
1893 VALIDATE_STATE(s);
1894 if (!(vma->vm_flags & VM_WRITE))
1895 return -EINVAL;
1896 lock_kernel();
1897 if ((ret = prog_dmabuf_dac1(s)) != 0)
1898 goto out;
1899 ret = -EINVAL;
1900 if (vma->vm_pgoff != 0)
1901 goto out;
1902 size = vma->vm_end - vma->vm_start;
1903 if (size > (PAGE_SIZE << s->dma_dac1.buforder))
1904 goto out;
1905 ret = -EAGAIN;
1906 if (remap_page_range(vma->vm_start, virt_to_phys(s->dma_dac1.rawbuf), size, vma->vm_page_prot))
1907 goto out;
1908 s->dma_dac1.mapped = 1;
1909 ret = 0;
1910 out:
1911 unlock_kernel();
1912 return ret;
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;
1920 count_info cinfo;
1921 int count;
1922 unsigned ctrl;
1923 int val, ret;
1925 VALIDATE_STATE(s);
1926 switch (cmd) {
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:
1934 return -EINVAL;
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:
1940 stop_dac1(s);
1941 synchronize_irq();
1942 s->dma_dac1.swptr = s->dma_dac1.hwptr = s->dma_dac1.count = s->dma_dac1.total_bytes = 0;
1943 return 0;
1945 case SNDCTL_DSP_SPEED:
1946 if (get_user(val, (int *)arg))
1947 return -EFAULT;
1948 if (val >= 0) {
1949 stop_dac1(s);
1950 s->dma_dac1.ready = 0;
1951 for (ctrl = 0; ctrl <= 2; ctrl++)
1952 if (val < (dac1_samplerate[ctrl] + dac1_samplerate[ctrl+1]) / 2)
1953 break;
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))
1963 return -EFAULT;
1964 stop_dac1(s);
1965 s->dma_dac1.ready = 0;
1966 spin_lock_irqsave(&s->lock, flags);
1967 if (val)
1968 s->sctrl |= SCTRL_P1SMB;
1969 else
1970 s->sctrl &= ~SCTRL_P1SMB;
1971 outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
1972 spin_unlock_irqrestore(&s->lock, flags);
1973 return 0;
1975 case SNDCTL_DSP_CHANNELS:
1976 if (get_user(val, (int *)arg))
1977 return -EFAULT;
1978 if (val != 0) {
1979 if (s->dma_dac1.mapped)
1980 return -EINVAL;
1981 stop_dac1(s);
1982 s->dma_dac1.ready = 0;
1983 spin_lock_irqsave(&s->lock, flags);
1984 if (val >= 2)
1985 s->sctrl |= SCTRL_P1SMB;
1986 else
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))
1998 return -EFAULT;
1999 if (val != AFMT_QUERY) {
2000 stop_dac1(s);
2001 s->dma_dac1.ready = 0;
2002 spin_lock_irqsave(&s->lock, flags);
2003 if (val == AFMT_S16_LE)
2004 s->sctrl |= SCTRL_P1SEB;
2005 else
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:
2013 return 0;
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))
2020 return -EFAULT;
2021 if (val & PCM_ENABLE_OUTPUT) {
2022 if (!s->dma_dac1.ready && (ret = prog_dmabuf_dac1(s)))
2023 return ret;
2024 start_dac1(s);
2025 } else
2026 stop_dac1(s);
2027 return 0;
2029 case SNDCTL_DSP_GETOSPACE:
2030 if (!(s->ctrl & CTRL_DAC1_EN) && (val = prog_dmabuf_dac1(s)) != 0)
2031 return val;
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;
2036 if (count < 0)
2037 count = 0;
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;
2046 return 0;
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);
2053 if (count < 0)
2054 count = 0;
2055 return put_user(count, (int *)arg);
2057 case SNDCTL_DSP_GETOPTR:
2058 if (!(file->f_mode & FMODE_WRITE))
2059 return -EINVAL;
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;
2064 if (count < 0)
2065 count = 0;
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)))
2075 return val;
2076 return put_user(s->dma_dac1.fragsize, (int *)arg);
2078 case SNDCTL_DSP_SETFRAGMENT:
2079 if (get_user(val, (int *)arg))
2080 return -EFAULT;
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;
2089 return 0;
2091 case SNDCTL_DSP_SUBDIVIDE:
2092 if (s->dma_dac1.subdivision)
2093 return -EINVAL;
2094 if (get_user(val, (int *)arg))
2095 return -EFAULT;
2096 if (val != 1 && val != 2 && val != 4)
2097 return -EINVAL;
2098 s->dma_dac1.subdivision = val;
2099 return 0;
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:
2113 return -EINVAL;
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) {
2128 if (list == &devs)
2129 return -ENODEV;
2130 s = list_entry(list, struct es1370_state, devs);
2131 if (!((s->dev_dac ^ minor) & ~0xf))
2132 break;
2134 VALIDATE_STATE(s);
2135 /* we allow opening with O_RDWR, most programs do it although they will only write */
2136 #if 0
2137 if (file->f_mode & FMODE_READ)
2138 return -EPERM;
2139 #endif
2140 if (!(file->f_mode & FMODE_WRITE))
2141 return -EINVAL;
2142 file->private_data = s;
2143 /* wait for device to become free */
2144 down(&s->open_sem);
2145 while (s->open_mode & FMODE_DAC) {
2146 if (file->f_flags & O_NONBLOCK) {
2147 up(&s->open_sem);
2148 return -EBUSY;
2150 add_wait_queue(&s->open_wait, &wait);
2151 __set_current_state(TASK_INTERRUPTIBLE);
2152 up(&s->open_sem);
2153 schedule();
2154 remove_wait_queue(&s->open_wait, &wait);
2155 set_current_state(TASK_RUNNING);
2156 if (signal_pending(current))
2157 return -ERESTARTSYS;
2158 down(&s->open_sem);
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;
2166 else
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;
2172 up(&s->open_sem);
2173 return 0;
2176 static int es1370_release_dac(struct inode *inode, struct file *file)
2178 struct es1370_state *s = (struct es1370_state *)file->private_data;
2180 VALIDATE_STATE(s);
2181 lock_kernel();
2182 drain_dac1(s, file->f_flags & O_NONBLOCK);
2183 down(&s->open_sem);
2184 stop_dac1(s);
2185 dealloc_dmabuf(s, &s->dma_dac1);
2186 s->open_mode &= ~FMODE_DAC;
2187 wake_up(&s->open_wait);
2188 up(&s->open_sem);
2189 unlock_kernel();
2190 return 0;
2193 static /*const*/ struct file_operations es1370_dac_fops = {
2194 owner: THIS_MODULE,
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);
2210 ssize_t ret;
2211 unsigned long flags;
2212 unsigned ptr;
2213 int cnt;
2215 VALIDATE_STATE(s);
2216 if (ppos != &file->f_pos)
2217 return -ESPIPE;
2218 if (!access_ok(VERIFY_WRITE, buffer, count))
2219 return -EFAULT;
2220 if (count == 0)
2221 return 0;
2222 ret = 0;
2223 add_wait_queue(&s->midi.iwait, &wait);
2224 while (count > 0) {
2225 spin_lock_irqsave(&s->lock, flags);
2226 ptr = s->midi.ird;
2227 cnt = MIDIINBUF - ptr;
2228 if (s->midi.icnt < cnt)
2229 cnt = s->midi.icnt;
2230 if (cnt <= 0)
2231 __set_current_state(TASK_INTERRUPTIBLE);
2232 spin_unlock_irqrestore(&s->lock, flags);
2233 if (cnt > count)
2234 cnt = count;
2235 if (cnt <= 0) {
2236 if (file->f_flags & O_NONBLOCK) {
2237 if (!ret)
2238 ret = -EAGAIN;
2239 break;
2241 schedule();
2242 if (signal_pending(current)) {
2243 if (!ret)
2244 ret = -ERESTARTSYS;
2245 break;
2247 continue;
2249 if (copy_to_user(buffer, s->midi.ibuf + ptr, cnt)) {
2250 if (!ret)
2251 ret = -EFAULT;
2252 break;
2254 ptr = (ptr + cnt) % MIDIINBUF;
2255 spin_lock_irqsave(&s->lock, flags);
2256 s->midi.ird = ptr;
2257 s->midi.icnt -= cnt;
2258 spin_unlock_irqrestore(&s->lock, flags);
2259 count -= cnt;
2260 buffer += cnt;
2261 ret += cnt;
2262 break;
2264 __set_current_state(TASK_RUNNING);
2265 remove_wait_queue(&s->midi.iwait, &wait);
2266 return ret;
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);
2273 ssize_t ret;
2274 unsigned long flags;
2275 unsigned ptr;
2276 int cnt;
2278 VALIDATE_STATE(s);
2279 if (ppos != &file->f_pos)
2280 return -ESPIPE;
2281 if (!access_ok(VERIFY_READ, buffer, count))
2282 return -EFAULT;
2283 if (count == 0)
2284 return 0;
2285 ret = 0;
2286 add_wait_queue(&s->midi.owait, &wait);
2287 while (count > 0) {
2288 spin_lock_irqsave(&s->lock, flags);
2289 ptr = s->midi.owr;
2290 cnt = MIDIOUTBUF - ptr;
2291 if (s->midi.ocnt + cnt > MIDIOUTBUF)
2292 cnt = MIDIOUTBUF - s->midi.ocnt;
2293 if (cnt <= 0) {
2294 __set_current_state(TASK_INTERRUPTIBLE);
2295 es1370_handle_midi(s);
2297 spin_unlock_irqrestore(&s->lock, flags);
2298 if (cnt > count)
2299 cnt = count;
2300 if (cnt <= 0) {
2301 if (file->f_flags & O_NONBLOCK) {
2302 if (!ret)
2303 ret = -EAGAIN;
2304 break;
2306 schedule();
2307 if (signal_pending(current)) {
2308 if (!ret)
2309 ret = -ERESTARTSYS;
2310 break;
2312 continue;
2314 if (copy_from_user(s->midi.obuf + ptr, buffer, cnt)) {
2315 if (!ret)
2316 ret = -EFAULT;
2317 break;
2319 ptr = (ptr + cnt) % MIDIOUTBUF;
2320 spin_lock_irqsave(&s->lock, flags);
2321 s->midi.owr = ptr;
2322 s->midi.ocnt += cnt;
2323 spin_unlock_irqrestore(&s->lock, flags);
2324 count -= cnt;
2325 buffer += cnt;
2326 ret += cnt;
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);
2333 return ret;
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;
2343 VALIDATE_STATE(s);
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);
2358 return mask;
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) {
2370 if (list == &devs)
2371 return -ENODEV;
2372 s = list_entry(list, struct es1370_state, devs);
2373 if (s->dev_midi == minor)
2374 break;
2376 VALIDATE_STATE(s);
2377 file->private_data = s;
2378 /* wait for device to become free */
2379 down(&s->open_sem);
2380 while (s->open_mode & (file->f_mode << FMODE_MIDI_SHIFT)) {
2381 if (file->f_flags & O_NONBLOCK) {
2382 up(&s->open_sem);
2383 return -EBUSY;
2385 add_wait_queue(&s->open_wait, &wait);
2386 __set_current_state(TASK_INTERRUPTIBLE);
2387 up(&s->open_sem);
2388 schedule();
2389 remove_wait_queue(&s->open_wait, &wait);
2390 set_current_state(TASK_RUNNING);
2391 if (signal_pending(current))
2392 return -ERESTARTSYS;
2393 down(&s->open_sem);
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);
2414 up(&s->open_sem);
2415 return 0;
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;
2425 VALIDATE_STATE(s);
2427 lock_kernel();
2428 if (file->f_mode & FMODE_WRITE) {
2429 add_wait_queue(&s->midi.owait, &wait);
2430 for (;;) {
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);
2435 if (count <= 0)
2436 break;
2437 if (signal_pending(current))
2438 break;
2439 if (file->f_flags & O_NONBLOCK) {
2440 remove_wait_queue(&s->midi.owait, &wait);
2441 set_current_state(TASK_RUNNING);
2442 return -EBUSY;
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);
2451 down(&s->open_sem);
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);
2460 up(&s->open_sem);
2461 unlock_kernel();
2462 return 0;
2465 static /*const*/ struct file_operations es1370_midi_fops = {
2466 owner: THIS_MODULE,
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 */
2478 #define NR_DEVICE 5
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 {
2499 int mixch;
2500 int vol;
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;
2520 mm_segment_t fs;
2521 int i, val;
2523 if (!RSRCISIOREGION(pcidev, 0))
2524 return -1;
2525 if (pcidev->irq == 0)
2526 return -1;
2527 if (!pci_dma_supported(pcidev, 0xffffffff)) {
2528 printk(KERN_WARNING "es1370: architecture does not support 32bit PCI busmaster DMA\n");
2529 return -1;
2531 if (!(s = kmalloc(sizeof(struct es1370_state), GFP_KERNEL))) {
2532 printk(KERN_WARNING "es1370: out of memory\n");
2533 return -1;
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;
2545 s->dev = pcidev;
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);
2550 goto err_region;
2552 if (pci_enable_device(pcidev))
2553 goto err_irq;
2554 if (request_irq(s->irq, es1370_interrupt, SA_SHIRQ, "es1370", s)) {
2555 printk(KERN_ERR "es1370: irq %u in use\n", s->irq);
2556 goto err_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");
2565 else
2566 s->ctrl |= CTRL_JYSTK_EN;
2568 if (lineout[devindex])
2569 s->ctrl |= CTRL_XCTL0;
2570 if (micbias[devindex])
2571 s->ctrl |= CTRL_XCTL1;
2572 s->sctrl = 0;
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)
2580 goto err_dev1;
2581 if ((s->dev_mixer = register_sound_mixer(&es1370_mixer_fops, -1)) < 0)
2582 goto err_dev2;
2583 if ((s->dev_dac = register_sound_dsp(&es1370_dac_fops, -1)) < 0)
2584 goto err_dev3;
2585 if ((s->dev_midi = register_sound_midi(&es1370_midi_fops, -1)) < 0)
2586 goto err_dev4;
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 */
2599 s->mix.imix = 1;
2600 fs = get_fs();
2601 set_fs(KERNEL_DS);
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);
2608 set_fs(fs);
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)
2616 devindex++;
2617 return 0;
2619 err_dev4:
2620 unregister_sound_dsp(s->dev_dac);
2621 err_dev3:
2622 unregister_sound_mixer(s->dev_mixer);
2623 err_dev2:
2624 unregister_sound_dsp(s->dev_audio);
2625 err_dev1:
2626 printk(KERN_ERR "es1370: cannot register misc device\n");
2627 free_irq(s->irq, s);
2628 err_irq:
2629 release_region(s->io, ES1370_EXTENT);
2630 err_region:
2631 kfree(s);
2632 return -1;
2635 static void __devinit es1370_remove(struct pci_dev *dev)
2637 struct es1370_state *s = pci_get_drvdata(dev);
2639 if (!s)
2640 return;
2641 list_del(&s->devs);
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 */
2644 synchronize_irq();
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);
2651 kfree(s);
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 },
2657 { 0, }
2660 MODULE_DEVICE_TABLE(pci, id_table);
2662 static struct pci_driver es1370_driver = {
2663 name: "es1370",
2664 id_table: id_table,
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! */
2672 return -ENODEV;
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 /* --------------------------------------------------------------------- */
2688 #ifndef MODULE
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)
2697 return 0;
2699 (void)
2700 ( (get_option(&str,&joystick[nr_dev]) == 2)
2701 && (get_option(&str,&lineout [nr_dev]) == 2)
2702 && get_option(&str,&micbias [nr_dev])
2705 nr_dev++;
2706 return 1;
2709 __setup("es1370=", es1370_setup);
2711 #endif /* MODULE */