v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / drivers / sound / es1370.c
1 /*****************************************************************************/
2
3 /*
4  *      es1370.c  --  Ensoniq ES1370/Asahi Kasei AK4531 audio driver.
5  *
6  *      Copyright (C) 1998-2001  Thomas Sailer (t.sailer@alumni.ethz.ch)
7  *
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.
12  *
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.
17  *
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.
21  *
22  * Special thanks to David C. Niemi
23  *
24  *
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.
37  *            
38  *
39  *  Note: sync mode is not yet supported (i.e. running dsp and dac from the same
40  *  clock source)
41  *
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
49  *
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.
53  *
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
120  *    12.12.2000   0.35  More dma buffer initializations, patch from
121  *                       Tjeerd Mulder <tjeerd.mulder@fujitsu-siemens.com>
122  *    07.01.2001   0.36  Timeout change in wrcodec as requested by Frank Klemm <pfk@fuchs.offl.uni-jena.de>
123  *    31.01.2001   0.37  Register/Unregister gameport
124  *                       Fix SETTRIGGER non OSS API conformity
125  *
126  * some important things missing in Ensoniq documentation:
127  *
128  * Experimental PCLKDIV results:  play the same waveforms on both DAC1 and DAC2
129  * and vary PCLKDIV to obtain zero beat.
130  *  5512sps:  254
131  * 44100sps:   30
132  * seems to be fs = 1411200/(PCLKDIV+2)
133  *
134  * should find out when curr_sample_ct is cleared and
135  * where exactly the CCB fetches data
136  *
137  * The card uses a 22.5792 MHz crystal.
138  * The LINEIN jack may be converted to an AOUT jack by
139  * setting pin 47 (XCTL0) of the ES1370 to high.
140  * Pin 48 (XCTL1) of the ES1370 sets the +5V bias for an electretmic
141  * 
142  *
143  */
144
145 /*****************************************************************************/
146       
147 #include <linux/version.h>
148 #include <linux/module.h>
149 #include <linux/string.h>
150 #include <linux/ioport.h>
151 #include <linux/sched.h>
152 #include <linux/delay.h>
153 #include <linux/sound.h>
154 #include <linux/slab.h>
155 #include <linux/soundcard.h>
156 #include <linux/pci.h>
157 #include <linux/smp_lock.h>
158 #include <linux/wrapper.h>
159 #include <asm/io.h>
160 #include <asm/dma.h>
161 #include <linux/init.h>
162 #include <linux/poll.h>
163 #include <linux/spinlock.h>
164 #include <asm/uaccess.h>
165 #include <asm/hardirq.h>
166
167 #if defined(CONFIG_INPUT_ANALOG) || defined(CONFIG_INPUT_ANALOG_MODULE)
168 #include <linux/gameport.h>
169 #else
170 struct gameport {
171         int io;
172         int size;
173 };
174
175 extern inline void gameport_register_port(struct gameport *gameport)
176 {
177 }
178
179 extern inline void gameport_unregister_port(struct gameport *gameport)
180 {
181 }
182 #endif
183
184 /* --------------------------------------------------------------------- */
185
186 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
187 #define DBG(x) {}
188 /*#define DBG(x) {x}*/
189
190 /* --------------------------------------------------------------------- */
191
192 #ifndef PCI_VENDOR_ID_ENSONIQ
193 #define PCI_VENDOR_ID_ENSONIQ        0x1274    
194 #endif
195
196 #ifndef PCI_DEVICE_ID_ENSONIQ_ES1370
197 #define PCI_DEVICE_ID_ENSONIQ_ES1370 0x5000
198 #endif
199
200 #define ES1370_MAGIC  ((PCI_VENDOR_ID_ENSONIQ<<16)|PCI_DEVICE_ID_ENSONIQ_ES1370)
201
202 #define ES1370_EXTENT             0x40
203 #define JOY_EXTENT                8
204
205 #define ES1370_REG_CONTROL        0x00
206 #define ES1370_REG_STATUS         0x04
207 #define ES1370_REG_UART_DATA      0x08
208 #define ES1370_REG_UART_STATUS    0x09
209 #define ES1370_REG_UART_CONTROL   0x09
210 #define ES1370_REG_UART_TEST      0x0a
211 #define ES1370_REG_MEMPAGE        0x0c
212 #define ES1370_REG_CODEC          0x10
213 #define ES1370_REG_SERIAL_CONTROL 0x20
214 #define ES1370_REG_DAC1_SCOUNT    0x24
215 #define ES1370_REG_DAC2_SCOUNT    0x28
216 #define ES1370_REG_ADC_SCOUNT     0x2c
217
218 #define ES1370_REG_DAC1_FRAMEADR    0xc30
219 #define ES1370_REG_DAC1_FRAMECNT    0xc34
220 #define ES1370_REG_DAC2_FRAMEADR    0xc38
221 #define ES1370_REG_DAC2_FRAMECNT    0xc3c
222 #define ES1370_REG_ADC_FRAMEADR     0xd30
223 #define ES1370_REG_ADC_FRAMECNT     0xd34
224 #define ES1370_REG_PHANTOM_FRAMEADR 0xd38
225 #define ES1370_REG_PHANTOM_FRAMECNT 0xd3c
226
227 #define ES1370_FMT_U8_MONO     0
228 #define ES1370_FMT_U8_STEREO   1
229 #define ES1370_FMT_S16_MONO    2
230 #define ES1370_FMT_S16_STEREO  3
231 #define ES1370_FMT_STEREO      1
232 #define ES1370_FMT_S16         2
233 #define ES1370_FMT_MASK        3
234
235 static const unsigned sample_size[] = { 1, 2, 2, 4 };
236 static const unsigned sample_shift[] = { 0, 1, 1, 2 };
237
238 static const unsigned dac1_samplerate[] = { 5512, 11025, 22050, 44100 };
239
240 #define DAC2_SRTODIV(x) (((1411200+(x)/2)/(x))-2)
241 #define DAC2_DIVTOSR(x) (1411200/((x)+2))
242
243 #define CTRL_ADC_STOP   0x80000000  /* 1 = ADC stopped */
244 #define CTRL_XCTL1      0x40000000  /* electret mic bias */
245 #define CTRL_OPEN       0x20000000  /* no function, can be read and written */
246 #define CTRL_PCLKDIV    0x1fff0000  /* ADC/DAC2 clock divider */
247 #define CTRL_SH_PCLKDIV 16
248 #define CTRL_MSFMTSEL   0x00008000  /* MPEG serial data fmt: 0 = Sony, 1 = I2S */
249 #define CTRL_M_SBB      0x00004000  /* DAC2 clock: 0 = PCLKDIV, 1 = MPEG */
250 #define CTRL_WTSRSEL    0x00003000  /* DAC1 clock freq: 0=5512, 1=11025, 2=22050, 3=44100 */
251 #define CTRL_SH_WTSRSEL 12
252 #define CTRL_DAC_SYNC   0x00000800  /* 1 = DAC2 runs off DAC1 clock */
253 #define CTRL_CCB_INTRM  0x00000400  /* 1 = CCB "voice" ints enabled */
254 #define CTRL_M_CB       0x00000200  /* recording source: 0 = ADC, 1 = MPEG */
255 #define CTRL_XCTL0      0x00000100  /* 0 = Line in, 1 = Line out */
256 #define CTRL_BREQ       0x00000080  /* 1 = test mode (internal mem test) */
257 #define CTRL_DAC1_EN    0x00000040  /* enable DAC1 */
258 #define CTRL_DAC2_EN    0x00000020  /* enable DAC2 */
259 #define CTRL_ADC_EN     0x00000010  /* enable ADC */
260 #define CTRL_UART_EN    0x00000008  /* enable MIDI uart */
261 #define CTRL_JYSTK_EN   0x00000004  /* enable Joystick port (presumably at address 0x200) */
262 #define CTRL_CDC_EN     0x00000002  /* enable serial (CODEC) interface */
263 #define CTRL_SERR_DIS   0x00000001  /* 1 = disable PCI SERR signal */
264
265 #define STAT_INTR       0x80000000  /* wired or of all interrupt bits */
266 #define STAT_CSTAT      0x00000400  /* 1 = codec busy or codec write in progress */
267 #define STAT_CBUSY      0x00000200  /* 1 = codec busy */
268 #define STAT_CWRIP      0x00000100  /* 1 = codec write in progress */
269 #define STAT_VC         0x00000060  /* CCB int source, 0=DAC1, 1=DAC2, 2=ADC, 3=undef */
270 #define STAT_SH_VC      5
271 #define STAT_MCCB       0x00000010  /* CCB int pending */
272 #define STAT_UART       0x00000008  /* UART int pending */
273 #define STAT_DAC1       0x00000004  /* DAC1 int pending */
274 #define STAT_DAC2       0x00000002  /* DAC2 int pending */
275 #define STAT_ADC        0x00000001  /* ADC int pending */
276
277 #define USTAT_RXINT     0x80        /* UART rx int pending */
278 #define USTAT_TXINT     0x04        /* UART tx int pending */
279 #define USTAT_TXRDY     0x02        /* UART tx ready */
280 #define USTAT_RXRDY     0x01        /* UART rx ready */
281
282 #define UCTRL_RXINTEN   0x80        /* 1 = enable RX ints */
283 #define UCTRL_TXINTEN   0x60        /* TX int enable field mask */
284 #define UCTRL_ENA_TXINT 0x20        /* enable TX int */
285 #define UCTRL_CNTRL     0x03        /* control field */
286 #define UCTRL_CNTRL_SWR 0x03        /* software reset command */
287
288 #define SCTRL_P2ENDINC    0x00380000  /*  */
289 #define SCTRL_SH_P2ENDINC 19
290 #define SCTRL_P2STINC     0x00070000  /*  */
291 #define SCTRL_SH_P2STINC  16
292 #define SCTRL_R1LOOPSEL   0x00008000  /* 0 = loop mode */
293 #define SCTRL_P2LOOPSEL   0x00004000  /* 0 = loop mode */
294 #define SCTRL_P1LOOPSEL   0x00002000  /* 0 = loop mode */
295 #define SCTRL_P2PAUSE     0x00001000  /* 1 = pause mode */
296 #define SCTRL_P1PAUSE     0x00000800  /* 1 = pause mode */
297 #define SCTRL_R1INTEN     0x00000400  /* enable interrupt */
298 #define SCTRL_P2INTEN     0x00000200  /* enable interrupt */
299 #define SCTRL_P1INTEN     0x00000100  /* enable interrupt */
300 #define SCTRL_P1SCTRLD    0x00000080  /* reload sample count register for DAC1 */
301 #define SCTRL_P2DACSEN    0x00000040  /* 1 = DAC2 play back last sample when disabled */
302 #define SCTRL_R1SEB       0x00000020  /* 1 = 16bit */
303 #define SCTRL_R1SMB       0x00000010  /* 1 = stereo */
304 #define SCTRL_R1FMT       0x00000030  /* format mask */
305 #define SCTRL_SH_R1FMT    4
306 #define SCTRL_P2SEB       0x00000008  /* 1 = 16bit */
307 #define SCTRL_P2SMB       0x00000004  /* 1 = stereo */
308 #define SCTRL_P2FMT       0x0000000c  /* format mask */
309 #define SCTRL_SH_P2FMT    2
310 #define SCTRL_P1SEB       0x00000002  /* 1 = 16bit */
311 #define SCTRL_P1SMB       0x00000001  /* 1 = stereo */
312 #define SCTRL_P1FMT       0x00000003  /* format mask */
313 #define SCTRL_SH_P1FMT    0
314
315 /* misc stuff */
316
317 #define FMODE_DAC         4           /* slight misuse of mode_t */
318
319 /* MIDI buffer sizes */
320
321 #define MIDIINBUF  256
322 #define MIDIOUTBUF 256
323
324 #define FMODE_MIDI_SHIFT 3
325 #define FMODE_MIDI_READ  (FMODE_READ << FMODE_MIDI_SHIFT)
326 #define FMODE_MIDI_WRITE (FMODE_WRITE << FMODE_MIDI_SHIFT)
327
328 /* --------------------------------------------------------------------- */
329
330 struct es1370_state {
331         /* magic */
332         unsigned int magic;
333
334         /* list of es1370 devices */
335         struct list_head devs;
336
337         /* the corresponding pci_dev structure */
338         struct pci_dev *dev;
339
340         /* soundcore stuff */
341         int dev_audio;
342         int dev_mixer;
343         int dev_dac;
344         int dev_midi;
345         
346         /* hardware resources */
347         unsigned long io; /* long for SPARC */
348         unsigned int irq;
349
350         /* mixer registers; there is no HW readback */
351         struct {
352                 unsigned short vol[10];
353                 unsigned int recsrc;
354                 unsigned int modcnt;
355                 unsigned short micpreamp;
356                 unsigned int imix;
357         } mix;
358
359         /* wave stuff */
360         unsigned ctrl;
361         unsigned sctrl;
362
363         spinlock_t lock;
364         struct semaphore open_sem;
365         mode_t open_mode;
366         wait_queue_head_t open_wait;
367
368         struct dmabuf {
369                 void *rawbuf;
370                 dma_addr_t dmaaddr;
371                 unsigned buforder;
372                 unsigned numfrag;
373                 unsigned fragshift;
374                 unsigned hwptr, swptr;
375                 unsigned total_bytes;
376                 int count;
377                 unsigned error; /* over/underrun */
378                 wait_queue_head_t wait;
379                 /* redundant, but makes calculations easier */
380                 unsigned fragsize;
381                 unsigned dmasize;
382                 unsigned fragsamples;
383                 /* OSS stuff */
384                 unsigned mapped:1;
385                 unsigned ready:1;
386                 unsigned endcleared:1;
387                 unsigned enabled:1;
388                 unsigned ossfragshift;
389                 int ossmaxfrags;
390                 unsigned subdivision;
391         } dma_dac1, dma_dac2, dma_adc;
392
393         /* midi stuff */
394         struct {
395                 unsigned ird, iwr, icnt;
396                 unsigned ord, owr, ocnt;
397                 wait_queue_head_t iwait;
398                 wait_queue_head_t owait;
399                 unsigned char ibuf[MIDIINBUF];
400                 unsigned char obuf[MIDIOUTBUF];
401         } midi;
402
403         struct gameport gameport;
404         struct semaphore sem;
405 };
406
407 /* --------------------------------------------------------------------- */
408
409 static LIST_HEAD(devs);
410
411 /*
412  * The following buffer is used to point the phantom write channel to,
413  * so that it cannot wreak havoc. The attribute makes sure it doesn't
414  * cross a page boundary and ensures dword alignment for the DMA engine
415  */
416 static unsigned char bugbuf[16] __attribute__ ((aligned (16)));
417
418 /* --------------------------------------------------------------------- */
419
420 static inline unsigned ld2(unsigned int x)
421 {
422         unsigned r = 0;
423         
424         if (x >= 0x10000) {
425                 x >>= 16;
426                 r += 16;
427         }
428         if (x >= 0x100) {
429                 x >>= 8;
430                 r += 8;
431         }
432         if (x >= 0x10) {
433                 x >>= 4;
434                 r += 4;
435         }
436         if (x >= 4) {
437                 x >>= 2;
438                 r += 2;
439         }
440         if (x >= 2)
441                 r++;
442         return r;
443 }
444
445 /* --------------------------------------------------------------------- */
446
447 static void wrcodec(struct es1370_state *s, unsigned char idx, unsigned char data)
448 {
449         unsigned long tmo = jiffies + HZ/10, j;
450         
451         do {
452                 j = jiffies;
453                 if (!(inl(s->io+ES1370_REG_STATUS) & STAT_CSTAT)) {
454                         outw((((unsigned short)idx)<<8)|data, s->io+ES1370_REG_CODEC);
455                         return;
456                 }
457                 schedule();
458         } while ((signed)(tmo-j) > 0);
459         printk(KERN_ERR "es1370: write to codec register timeout\n");
460 }
461
462 /* --------------------------------------------------------------------- */
463
464 static inline void stop_adc(struct es1370_state *s)
465 {
466         unsigned long flags;
467
468         spin_lock_irqsave(&s->lock, flags);
469         s->ctrl &= ~CTRL_ADC_EN;
470         outl(s->ctrl, s->io+ES1370_REG_CONTROL);
471         spin_unlock_irqrestore(&s->lock, flags);
472 }       
473
474 static inline void stop_dac1(struct es1370_state *s)
475 {
476         unsigned long flags;
477
478         spin_lock_irqsave(&s->lock, flags);
479         s->ctrl &= ~CTRL_DAC1_EN;
480         outl(s->ctrl, s->io+ES1370_REG_CONTROL);
481         spin_unlock_irqrestore(&s->lock, flags);
482 }       
483
484 static inline void stop_dac2(struct es1370_state *s)
485 {
486         unsigned long flags;
487
488         spin_lock_irqsave(&s->lock, flags);
489         s->ctrl &= ~CTRL_DAC2_EN;
490         outl(s->ctrl, s->io+ES1370_REG_CONTROL);
491         spin_unlock_irqrestore(&s->lock, flags);
492 }       
493
494 static void start_dac1(struct es1370_state *s)
495 {
496         unsigned long flags;
497         unsigned fragremain, fshift;
498
499         spin_lock_irqsave(&s->lock, flags);
500         if (!(s->ctrl & CTRL_DAC1_EN) && (s->dma_dac1.mapped || s->dma_dac1.count > 0)
501             && s->dma_dac1.ready) {
502                 s->ctrl |= CTRL_DAC1_EN;
503                 s->sctrl = (s->sctrl & ~(SCTRL_P1LOOPSEL | SCTRL_P1PAUSE | SCTRL_P1SCTRLD)) | SCTRL_P1INTEN;
504                 outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
505                 fragremain = ((- s->dma_dac1.hwptr) & (s->dma_dac1.fragsize-1));
506                 fshift = sample_shift[(s->sctrl & SCTRL_P1FMT) >> SCTRL_SH_P1FMT];
507                 if (fragremain < 2*fshift)
508                         fragremain = s->dma_dac1.fragsize;
509                 outl((fragremain >> fshift) - 1, s->io+ES1370_REG_DAC1_SCOUNT);
510                 outl(s->ctrl, s->io+ES1370_REG_CONTROL);
511                 outl((s->dma_dac1.fragsize >> fshift) - 1, s->io+ES1370_REG_DAC1_SCOUNT);
512         }
513         spin_unlock_irqrestore(&s->lock, flags);
514 }       
515
516 static void start_dac2(struct es1370_state *s)
517 {
518         unsigned long flags;
519         unsigned fragremain, fshift;
520
521         spin_lock_irqsave(&s->lock, flags);
522         if (!(s->ctrl & CTRL_DAC2_EN) && (s->dma_dac2.mapped || s->dma_dac2.count > 0)
523             && s->dma_dac2.ready) {
524                 s->ctrl |= CTRL_DAC2_EN;
525                 s->sctrl = (s->sctrl & ~(SCTRL_P2LOOPSEL | SCTRL_P2PAUSE | SCTRL_P2DACSEN | 
526                                          SCTRL_P2ENDINC | SCTRL_P2STINC)) | SCTRL_P2INTEN |
527                         (((s->sctrl & SCTRL_P2FMT) ? 2 : 1) << SCTRL_SH_P2ENDINC) | 
528                         (0 << SCTRL_SH_P2STINC);
529                 outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
530                 fragremain = ((- s->dma_dac2.hwptr) & (s->dma_dac2.fragsize-1));
531                 fshift = sample_shift[(s->sctrl & SCTRL_P2FMT) >> SCTRL_SH_P2FMT];
532                 if (fragremain < 2*fshift)
533                         fragremain = s->dma_dac2.fragsize;
534                 outl((fragremain >> fshift) - 1, s->io+ES1370_REG_DAC2_SCOUNT);
535                 outl(s->ctrl, s->io+ES1370_REG_CONTROL);
536                 outl((s->dma_dac2.fragsize >> fshift) - 1, s->io+ES1370_REG_DAC2_SCOUNT);
537         }
538         spin_unlock_irqrestore(&s->lock, flags);
539 }       
540
541 static void start_adc(struct es1370_state *s)
542 {
543         unsigned long flags;
544         unsigned fragremain, fshift;
545
546         spin_lock_irqsave(&s->lock, flags);
547         if (!(s->ctrl & CTRL_ADC_EN) && (s->dma_adc.mapped || s->dma_adc.count < (signed)(s->dma_adc.dmasize - 2*s->dma_adc.fragsize))
548             && s->dma_adc.ready) {
549                 s->ctrl |= CTRL_ADC_EN;
550                 s->sctrl = (s->sctrl & ~SCTRL_R1LOOPSEL) | SCTRL_R1INTEN;
551                 outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
552                 fragremain = ((- s->dma_adc.hwptr) & (s->dma_adc.fragsize-1));
553                 fshift = sample_shift[(s->sctrl & SCTRL_R1FMT) >> SCTRL_SH_R1FMT];
554                 if (fragremain < 2*fshift)
555                         fragremain = s->dma_adc.fragsize;
556                 outl((fragremain >> fshift) - 1, s->io+ES1370_REG_ADC_SCOUNT);
557                 outl(s->ctrl, s->io+ES1370_REG_CONTROL);
558                 outl((s->dma_adc.fragsize >> fshift) - 1, s->io+ES1370_REG_ADC_SCOUNT);
559         }
560         spin_unlock_irqrestore(&s->lock, flags);
561 }       
562
563 /* --------------------------------------------------------------------- */
564
565 #define DMABUF_DEFAULTORDER (17-PAGE_SHIFT)
566 #define DMABUF_MINORDER 1
567
568 static inline void dealloc_dmabuf(struct es1370_state *s, struct dmabuf *db)
569 {
570         struct page *page, *pend;
571
572         if (db->rawbuf) {
573                 /* undo marking the pages as reserved */
574                 pend = virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
575                 for (page = virt_to_page(db->rawbuf); page <= pend; page++)
576                         mem_map_unreserve(page);
577                 pci_free_consistent(s->dev, PAGE_SIZE << db->buforder, db->rawbuf, db->dmaaddr);
578         }
579         db->rawbuf = NULL;
580         db->mapped = db->ready = 0;
581 }
582
583 static int prog_dmabuf(struct es1370_state *s, struct dmabuf *db, unsigned rate, unsigned fmt, unsigned reg)
584 {
585         int order;
586         unsigned bytepersec;
587         unsigned bufs;
588         struct page *page, *pend;
589
590         db->hwptr = db->swptr = db->total_bytes = db->count = db->error = db->endcleared = 0;
591         if (!db->rawbuf) {
592                 db->ready = db->mapped = 0;
593                 for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--)
594                         if ((db->rawbuf = pci_alloc_consistent(s->dev, PAGE_SIZE << order, &db->dmaaddr)))
595                                 break;
596                 if (!db->rawbuf)
597                         return -ENOMEM;
598                 db->buforder = order;
599                 /* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
600                 pend = virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
601                 for (page = virt_to_page(db->rawbuf); page <= pend; page++)
602                         mem_map_reserve(page);
603         }
604         fmt &= ES1370_FMT_MASK;
605         bytepersec = rate << sample_shift[fmt];
606         bufs = PAGE_SIZE << db->buforder;
607         if (db->ossfragshift) {
608                 if ((1000 << db->ossfragshift) < bytepersec)
609                         db->fragshift = ld2(bytepersec/1000);
610                 else
611                         db->fragshift = db->ossfragshift;
612         } else {
613                 db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1));
614                 if (db->fragshift < 3)
615                         db->fragshift = 3;
616         }
617         db->numfrag = bufs >> db->fragshift;
618         while (db->numfrag < 4 && db->fragshift > 3) {
619                 db->fragshift--;
620                 db->numfrag = bufs >> db->fragshift;
621         }
622         db->fragsize = 1 << db->fragshift;
623         if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
624                 db->numfrag = db->ossmaxfrags;
625         db->fragsamples = db->fragsize >> sample_shift[fmt];
626         db->dmasize = db->numfrag << db->fragshift;
627         memset(db->rawbuf, (fmt & ES1370_FMT_S16) ? 0 : 0x80, db->dmasize);
628         outl((reg >> 8) & 15, s->io+ES1370_REG_MEMPAGE);
629         outl(db->dmaaddr, s->io+(reg & 0xff));
630         outl((db->dmasize >> 2)-1, s->io+((reg + 4) & 0xff));
631         db->enabled = 1;
632         db->ready = 1;
633         return 0;
634 }
635
636 static inline int prog_dmabuf_adc(struct es1370_state *s)
637 {
638         stop_adc(s);
639         return prog_dmabuf(s, &s->dma_adc, DAC2_DIVTOSR((s->ctrl & CTRL_PCLKDIV) >> CTRL_SH_PCLKDIV),
640                            (s->sctrl >> SCTRL_SH_R1FMT) & ES1370_FMT_MASK, ES1370_REG_ADC_FRAMEADR);
641 }
642
643 static inline int prog_dmabuf_dac2(struct es1370_state *s)
644 {
645         stop_dac2(s);
646         return prog_dmabuf(s, &s->dma_dac2, DAC2_DIVTOSR((s->ctrl & CTRL_PCLKDIV) >> CTRL_SH_PCLKDIV),
647                            (s->sctrl >> SCTRL_SH_P2FMT) & ES1370_FMT_MASK, ES1370_REG_DAC2_FRAMEADR);
648 }
649
650 static inline int prog_dmabuf_dac1(struct es1370_state *s)
651 {
652         stop_dac1(s);
653         return prog_dmabuf(s, &s->dma_dac1, dac1_samplerate[(s->ctrl & CTRL_WTSRSEL) >> CTRL_SH_WTSRSEL],
654                            (s->sctrl >> SCTRL_SH_P1FMT) & ES1370_FMT_MASK, ES1370_REG_DAC1_FRAMEADR);
655 }
656
657 static inline unsigned get_hwptr(struct es1370_state *s, struct dmabuf *db, unsigned reg)
658 {
659         unsigned hwptr, diff;
660
661         outl((reg >> 8) & 15, s->io+ES1370_REG_MEMPAGE);
662         hwptr = (inl(s->io+(reg & 0xff)) >> 14) & 0x3fffc;
663         diff = (db->dmasize + hwptr - db->hwptr) % db->dmasize;
664         db->hwptr = hwptr;
665         return diff;
666 }
667
668 static inline void clear_advance(void *buf, unsigned bsize, unsigned bptr, unsigned len, unsigned char c)
669 {
670         if (bptr + len > bsize) {
671                 unsigned x = bsize - bptr;
672                 memset(((char *)buf) + bptr, c, x);
673                 bptr = 0;
674                 len -= x;
675         }
676         memset(((char *)buf) + bptr, c, len);
677 }
678
679 /* call with spinlock held! */
680 static void es1370_update_ptr(struct es1370_state *s)
681 {
682         int diff;
683
684         /* update ADC pointer */
685         if (s->ctrl & CTRL_ADC_EN) {
686                 diff = get_hwptr(s, &s->dma_adc, ES1370_REG_ADC_FRAMECNT);
687                 s->dma_adc.total_bytes += diff;
688                 s->dma_adc.count += diff;
689                 if (s->dma_adc.count >= (signed)s->dma_adc.fragsize) 
690                         wake_up(&s->dma_adc.wait);
691                 if (!s->dma_adc.mapped) {
692                         if (s->dma_adc.count > (signed)(s->dma_adc.dmasize - ((3 * s->dma_adc.fragsize) >> 1))) {
693                                 s->ctrl &= ~CTRL_ADC_EN;
694                                 outl(s->ctrl, s->io+ES1370_REG_CONTROL);
695                                 s->dma_adc.error++;
696                         }
697                 }
698         }
699         /* update DAC1 pointer */
700         if (s->ctrl & CTRL_DAC1_EN) {
701                 diff = get_hwptr(s, &s->dma_dac1, ES1370_REG_DAC1_FRAMECNT);
702                 s->dma_dac1.total_bytes += diff;
703                 if (s->dma_dac1.mapped) {
704                         s->dma_dac1.count += diff;
705                         if (s->dma_dac1.count >= (signed)s->dma_dac1.fragsize)
706                                 wake_up(&s->dma_dac1.wait);
707                 } else {
708                         s->dma_dac1.count -= diff;
709                         if (s->dma_dac1.count <= 0) {
710                                 s->ctrl &= ~CTRL_DAC1_EN;
711                                 outl(s->ctrl, s->io+ES1370_REG_CONTROL);
712                                 s->dma_dac1.error++;
713                         } else if (s->dma_dac1.count <= (signed)s->dma_dac1.fragsize && !s->dma_dac1.endcleared) {
714                                 clear_advance(s->dma_dac1.rawbuf, s->dma_dac1.dmasize, s->dma_dac1.swptr, 
715                                               s->dma_dac1.fragsize, (s->sctrl & SCTRL_P1SEB) ? 0 : 0x80);
716                                 s->dma_dac1.endcleared = 1;
717                         }
718                         if (s->dma_dac1.count + (signed)s->dma_dac1.fragsize <= (signed)s->dma_dac1.dmasize)
719                                 wake_up(&s->dma_dac1.wait);
720                 }
721         }
722         /* update DAC2 pointer */
723         if (s->ctrl & CTRL_DAC2_EN) {
724                 diff = get_hwptr(s, &s->dma_dac2, ES1370_REG_DAC2_FRAMECNT);
725                 s->dma_dac2.total_bytes += diff;
726                 if (s->dma_dac2.mapped) {
727                         s->dma_dac2.count += diff;
728                         if (s->dma_dac2.count >= (signed)s->dma_dac2.fragsize)
729                                 wake_up(&s->dma_dac2.wait);
730                 } else {
731                         s->dma_dac2.count -= diff;
732                         if (s->dma_dac2.count <= 0) {
733                                 s->ctrl &= ~CTRL_DAC2_EN;
734                                 outl(s->ctrl, s->io+ES1370_REG_CONTROL);
735                                 s->dma_dac2.error++;
736                         } else if (s->dma_dac2.count <= (signed)s->dma_dac2.fragsize && !s->dma_dac2.endcleared) {
737                                 clear_advance(s->dma_dac2.rawbuf, s->dma_dac2.dmasize, s->dma_dac2.swptr, 
738                                               s->dma_dac2.fragsize, (s->sctrl & SCTRL_P2SEB) ? 0 : 0x80);
739                                 s->dma_dac2.endcleared = 1;
740                         }
741                         if (s->dma_dac2.count + (signed)s->dma_dac2.fragsize <= (signed)s->dma_dac2.dmasize)
742                                 wake_up(&s->dma_dac2.wait);
743                 }
744         }
745 }
746
747 /* hold spinlock for the following! */
748 static void es1370_handle_midi(struct es1370_state *s)
749 {
750         unsigned char ch;
751         int wake;
752
753         if (!(s->ctrl & CTRL_UART_EN))
754                 return;
755         wake = 0;
756         while (inb(s->io+ES1370_REG_UART_STATUS) & USTAT_RXRDY) {
757                 ch = inb(s->io+ES1370_REG_UART_DATA);
758                 if (s->midi.icnt < MIDIINBUF) {
759                         s->midi.ibuf[s->midi.iwr] = ch;
760                         s->midi.iwr = (s->midi.iwr + 1) % MIDIINBUF;
761                         s->midi.icnt++;
762                 }
763                 wake = 1;
764         }
765         if (wake)
766                 wake_up(&s->midi.iwait);
767         wake = 0;
768         while ((inb(s->io+ES1370_REG_UART_STATUS) & USTAT_TXRDY) && s->midi.ocnt > 0) {
769                 outb(s->midi.obuf[s->midi.ord], s->io+ES1370_REG_UART_DATA);
770                 s->midi.ord = (s->midi.ord + 1) % MIDIOUTBUF;
771                 s->midi.ocnt--;
772                 if (s->midi.ocnt < MIDIOUTBUF-16)
773                         wake = 1;
774         }
775         if (wake)
776                 wake_up(&s->midi.owait);
777         outb((s->midi.ocnt > 0) ? UCTRL_RXINTEN | UCTRL_ENA_TXINT : UCTRL_RXINTEN, s->io+ES1370_REG_UART_CONTROL);
778 }
779
780 static void es1370_interrupt(int irq, void *dev_id, struct pt_regs *regs)
781 {
782         struct es1370_state *s = (struct es1370_state *)dev_id;
783         unsigned int intsrc, sctl;
784         
785         /* fastpath out, to ease interrupt sharing */
786         intsrc = inl(s->io+ES1370_REG_STATUS);
787         if (!(intsrc & 0x80000000))
788                 return;
789         spin_lock(&s->lock);
790         /* clear audio interrupts first */
791         sctl = s->sctrl;
792         if (intsrc & STAT_ADC)
793                 sctl &= ~SCTRL_R1INTEN;
794         if (intsrc & STAT_DAC1)
795                 sctl &= ~SCTRL_P1INTEN;
796         if (intsrc & STAT_DAC2)
797                 sctl &= ~SCTRL_P2INTEN;
798         outl(sctl, s->io+ES1370_REG_SERIAL_CONTROL);
799         outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
800         es1370_update_ptr(s);
801         es1370_handle_midi(s);
802         spin_unlock(&s->lock);
803 }
804
805 /* --------------------------------------------------------------------- */
806
807 static const char invalid_magic[] = KERN_CRIT "es1370: invalid magic value\n";
808
809 #define VALIDATE_STATE(s)                         \
810 ({                                                \
811         if (!(s) || (s)->magic != ES1370_MAGIC) { \
812                 printk(invalid_magic);            \
813                 return -ENXIO;                    \
814         }                                         \
815 })
816
817 /* --------------------------------------------------------------------- */
818
819 static const struct {
820         unsigned volidx:4;
821         unsigned left:4;
822         unsigned right:4;
823         unsigned stereo:1;
824         unsigned recmask:13;
825         unsigned avail:1;
826 } mixtable[SOUND_MIXER_NRDEVICES] = {
827         [SOUND_MIXER_VOLUME] = { 0, 0x0, 0x1, 1, 0x0000, 1 },   /* master */
828         [SOUND_MIXER_PCM]    = { 1, 0x2, 0x3, 1, 0x0400, 1 },   /* voice */
829         [SOUND_MIXER_SYNTH]  = { 2, 0x4, 0x5, 1, 0x0060, 1 },   /* FM */
830         [SOUND_MIXER_CD]     = { 3, 0x6, 0x7, 1, 0x0006, 1 },   /* CD */
831         [SOUND_MIXER_LINE]   = { 4, 0x8, 0x9, 1, 0x0018, 1 },   /* Line */
832         [SOUND_MIXER_LINE1]  = { 5, 0xa, 0xb, 1, 0x1800, 1 },   /* AUX */
833         [SOUND_MIXER_LINE2]  = { 6, 0xc, 0x0, 0, 0x0100, 1 },   /* Mono1 */
834         [SOUND_MIXER_LINE3]  = { 7, 0xd, 0x0, 0, 0x0200, 1 },   /* Mono2 */
835         [SOUND_MIXER_MIC]    = { 8, 0xe, 0x0, 0, 0x0001, 1 },   /* Mic */
836         [SOUND_MIXER_OGAIN]  = { 9, 0xf, 0x0, 0, 0x0000, 1 }    /* mono out */
837 };
838
839 static void set_recsrc(struct es1370_state *s, unsigned int val)
840 {
841         unsigned int i, j;
842
843         for (j = i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
844                 if (!(val & (1 << i)))
845                         continue;
846                 if (!mixtable[i].recmask) {
847                         val &= ~(1 << i);
848                         continue;
849                 }
850                 j |= mixtable[i].recmask;
851         }
852         s->mix.recsrc = val;
853         wrcodec(s, 0x12, j & 0xd5);
854         wrcodec(s, 0x13, j & 0xaa);
855         wrcodec(s, 0x14, (j >> 8) & 0x17);
856         wrcodec(s, 0x15, (j >> 8) & 0x0f);
857         i = (j & 0x37f) | ((j << 1) & 0x3000) | 0xc60;
858         if (!s->mix.imix) {
859                 i &= 0xff60;  /* mute record and line monitor */
860         }
861         wrcodec(s, 0x10, i);
862         wrcodec(s, 0x11, i >> 8);
863 }
864
865 static int mixer_ioctl(struct es1370_state *s, unsigned int cmd, unsigned long arg)
866 {
867         unsigned long flags;
868         int i, val;
869         unsigned char l, r, rl, rr;
870
871         VALIDATE_STATE(s);
872         if (cmd == SOUND_MIXER_PRIVATE1) {
873                 /* enable/disable/query mixer preamp */
874                 if (get_user(val, (int *)arg))
875                         return -EFAULT;
876                 if (val != -1) {
877                         s->mix.micpreamp = !!val;
878                         wrcodec(s, 0x19, s->mix.micpreamp);
879                 }
880                 return put_user(s->mix.micpreamp, (int *)arg);
881         }
882         if (cmd == SOUND_MIXER_PRIVATE2) {
883                 /* enable/disable/query use of linein as second lineout */
884                 if (get_user(val, (int *)arg))
885                         return -EFAULT;
886                 if (val != -1) {
887                         spin_lock_irqsave(&s->lock, flags);
888                         if (val)
889                                 s->ctrl |= CTRL_XCTL0;
890                         else
891                                 s->ctrl &= ~CTRL_XCTL0;
892                         outl(s->ctrl, s->io+ES1370_REG_CONTROL);
893                         spin_unlock_irqrestore(&s->lock, flags);
894                 }
895                 return put_user((s->ctrl & CTRL_XCTL0) ? 1 : 0, (int *)arg);
896         }
897         if (cmd == SOUND_MIXER_PRIVATE3) {
898                 /* enable/disable/query microphone impedance setting */
899                 if (get_user(val, (int *)arg))
900                         return -EFAULT;
901                 if (val != -1) {
902                         spin_lock_irqsave(&s->lock, flags);
903                         if (val)
904                                 s->ctrl |= CTRL_XCTL1;
905                         else
906                                 s->ctrl &= ~CTRL_XCTL1;
907                         outl(s->ctrl, s->io+ES1370_REG_CONTROL);
908                         spin_unlock_irqrestore(&s->lock, flags);
909                 }
910                 return put_user((s->ctrl & CTRL_XCTL1) ? 1 : 0, (int *)arg);
911         }
912         if (cmd == SOUND_MIXER_INFO) {
913                 mixer_info info;
914                 strncpy(info.id, "ES1370", sizeof(info.id));
915                 strncpy(info.name, "Ensoniq ES1370", sizeof(info.name));
916                 info.modify_counter = s->mix.modcnt;
917                 if (copy_to_user((void *)arg, &info, sizeof(info)))
918                         return -EFAULT;
919                 return 0;
920         }
921         if (cmd == SOUND_OLD_MIXER_INFO) {
922                 _old_mixer_info info;
923                 strncpy(info.id, "ES1370", sizeof(info.id));
924                 strncpy(info.name, "Ensoniq ES1370", sizeof(info.name));
925                 if (copy_to_user((void *)arg, &info, sizeof(info)))
926                         return -EFAULT;
927                 return 0;
928         }
929         if (cmd == OSS_GETVERSION)
930                 return put_user(SOUND_VERSION, (int *)arg);
931         if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
932                 return -EINVAL;
933         if (_SIOC_DIR(cmd) == _SIOC_READ) {
934                 switch (_IOC_NR(cmd)) {
935                 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
936                         return put_user(s->mix.recsrc, (int *)arg);
937                         
938                 case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */
939                         val = SOUND_MASK_IMIX;
940                         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
941                                 if (mixtable[i].avail)
942                                         val |= 1 << i;
943                         return put_user(val, (int *)arg);
944
945                 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
946                         for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
947                                 if (mixtable[i].recmask)
948                                         val |= 1 << i;
949                         return put_user(val, (int *)arg);
950                         
951                 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
952                         for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
953                                 if (mixtable[i].stereo)
954                                         val |= 1 << i;
955                         return put_user(val, (int *)arg);
956                         
957                 case SOUND_MIXER_CAPS:
958                         return put_user(0, (int *)arg);
959                 
960                 case SOUND_MIXER_IMIX:
961                         return put_user(s->mix.imix, (int *)arg);
962
963                 default:
964                         i = _IOC_NR(cmd);
965                         if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].avail)
966                                 return -EINVAL;
967                         return put_user(s->mix.vol[mixtable[i].volidx], (int *)arg);
968                 }
969         }
970         if (_SIOC_DIR(cmd) != (_SIOC_READ|_SIOC_WRITE)) 
971                 return -EINVAL;
972         s->mix.modcnt++;
973         switch (_IOC_NR(cmd)) {
974
975         case SOUND_MIXER_IMIX:
976                 if (get_user(s->mix.imix, (int *)arg))
977                         return -EFAULT;
978                 set_recsrc(s, s->mix.recsrc);
979                 return 0;
980
981         case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
982                 if (get_user(val, (int *)arg))
983                         return -EFAULT;
984                 set_recsrc(s, val);
985                 return 0;
986
987         default:
988                 i = _IOC_NR(cmd);
989                 if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].avail)
990                         return -EINVAL;
991                 if (get_user(val, (int *)arg))
992                         return -EFAULT;
993                 l = val & 0xff;
994                 if (l > 100)
995                         l = 100;
996                 if (mixtable[i].stereo) {
997                         r = (val >> 8) & 0xff;
998                         if (r > 100)
999                                 r = 100;
1000                         if (l < 7) {
1001                                 rl = 0x80;
1002                                 l = 0;
1003                         } else {
1004                                 rl = 31 - ((l - 7) / 3);
1005                                 l = (31 - rl) * 3 + 7;
1006                         }
1007                         if (r < 7) {
1008                                 rr = 0x80;
1009                                 r = 0;
1010                         } else {
1011                                 rr =  31 - ((r - 7) / 3);
1012                                 r = (31 - rr) * 3 + 7;
1013                         }
1014                         wrcodec(s, mixtable[i].right, rr);
1015                 } else { 
1016                         if (mixtable[i].left == 15) {
1017                                 if (l < 2) {
1018                                         rr = rl = 0x80;
1019                                         r = l = 0;
1020                                 } else {
1021                                         rl = 7 - ((l - 2) / 14);
1022                                         r = l = (7 - rl) * 14 + 2;
1023                                 }
1024                         } else {
1025                                 if (l < 7) {
1026                                         rl = 0x80;
1027                                         r = l = 0;
1028                                 } else {
1029                                         rl = 31 - ((l - 7) / 3);
1030                                         r = l = (31 - rl) * 3 + 7;
1031                                 }
1032                         }
1033                 }
1034                 wrcodec(s, mixtable[i].left, rl);
1035 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1036                 s->mix.vol[mixtable[i].volidx] = ((unsigned int)r << 8) | l;
1037 #else
1038                 s->mix.vol[mixtable[i].volidx] = val;
1039 #endif
1040                 return put_user(s->mix.vol[mixtable[i].volidx], (int *)arg);
1041         }
1042 }
1043
1044 /* --------------------------------------------------------------------- */
1045
1046 static int es1370_open_mixdev(struct inode *inode, struct file *file)
1047 {
1048         int minor = MINOR(inode->i_rdev);
1049         struct list_head *list;
1050         struct es1370_state *s;
1051
1052         for (list = devs.next; ; list = list->next) {
1053                 if (list == &devs)
1054                         return -ENODEV;
1055                 s = list_entry(list, struct es1370_state, devs);
1056                 if (s->dev_mixer == minor)
1057                         break;
1058         }
1059         VALIDATE_STATE(s);
1060         file->private_data = s;
1061         return 0;
1062 }
1063
1064 static int es1370_release_mixdev(struct inode *inode, struct file *file)
1065 {
1066         struct es1370_state *s = (struct es1370_state *)file->private_data;
1067         
1068         VALIDATE_STATE(s);
1069         return 0;
1070 }
1071
1072 static int es1370_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1073 {
1074         return mixer_ioctl((struct es1370_state *)file->private_data, cmd, arg);
1075 }
1076
1077 static /*const*/ struct file_operations es1370_mixer_fops = {
1078         owner:          THIS_MODULE,
1079         llseek:         no_llseek,
1080         ioctl:          es1370_ioctl_mixdev,
1081         open:           es1370_open_mixdev,
1082         release:        es1370_release_mixdev,
1083 };
1084
1085 /* --------------------------------------------------------------------- */
1086
1087 static int drain_dac1(struct es1370_state *s, int nonblock)
1088 {
1089         DECLARE_WAITQUEUE(wait, current);
1090         unsigned long flags;
1091         int count, tmo;
1092         
1093         if (s->dma_dac1.mapped || !s->dma_dac1.ready)
1094                 return 0;
1095         add_wait_queue(&s->dma_dac1.wait, &wait);
1096         for (;;) {
1097                 __set_current_state(TASK_INTERRUPTIBLE);
1098                 spin_lock_irqsave(&s->lock, flags);
1099                 count = s->dma_dac1.count;
1100                 spin_unlock_irqrestore(&s->lock, flags);
1101                 if (count <= 0)
1102                         break;
1103                 if (signal_pending(current))
1104                         break;
1105                 if (nonblock) {
1106                         remove_wait_queue(&s->dma_dac1.wait, &wait);
1107                         set_current_state(TASK_RUNNING);
1108                         return -EBUSY;
1109                 }
1110                 tmo = 3 * HZ * (count + s->dma_dac1.fragsize) / 2
1111                         / dac1_samplerate[(s->ctrl & CTRL_WTSRSEL) >> CTRL_SH_WTSRSEL];
1112                 tmo >>= sample_shift[(s->sctrl & SCTRL_P1FMT) >> SCTRL_SH_P1FMT];
1113                 if (!schedule_timeout(tmo + 1))
1114                         DBG(printk(KERN_DEBUG "es1370: dma timed out??\n");)
1115         }
1116         remove_wait_queue(&s->dma_dac1.wait, &wait);
1117         set_current_state(TASK_RUNNING);
1118         if (signal_pending(current))
1119                 return -ERESTARTSYS;
1120         return 0;
1121 }
1122
1123 static int drain_dac2(struct es1370_state *s, int nonblock)
1124 {
1125         DECLARE_WAITQUEUE(wait, current);
1126         unsigned long flags;
1127         int count, tmo;
1128
1129         if (s->dma_dac2.mapped || !s->dma_dac2.ready)
1130                 return 0;
1131         add_wait_queue(&s->dma_dac2.wait, &wait);
1132         for (;;) {
1133                 __set_current_state(TASK_INTERRUPTIBLE);
1134                 spin_lock_irqsave(&s->lock, flags);
1135                 count = s->dma_dac2.count;
1136                 spin_unlock_irqrestore(&s->lock, flags);
1137                 if (count <= 0)
1138                         break;
1139                 if (signal_pending(current))
1140                         break;
1141                 if (nonblock) {
1142                         remove_wait_queue(&s->dma_dac2.wait, &wait);
1143                         set_current_state(TASK_RUNNING);
1144                         return -EBUSY;
1145                 }
1146                 tmo = 3 * HZ * (count + s->dma_dac2.fragsize) / 2
1147                         / DAC2_DIVTOSR((s->ctrl & CTRL_PCLKDIV) >> CTRL_SH_PCLKDIV);
1148                 tmo >>= sample_shift[(s->sctrl & SCTRL_P2FMT) >> SCTRL_SH_P2FMT];
1149                 if (!schedule_timeout(tmo + 1))
1150                         DBG(printk(KERN_DEBUG "es1370: dma timed out??\n");)
1151         }
1152         remove_wait_queue(&s->dma_dac2.wait, &wait);
1153         set_current_state(TASK_RUNNING);
1154         if (signal_pending(current))
1155                 return -ERESTARTSYS;
1156         return 0;
1157 }
1158
1159 /* --------------------------------------------------------------------- */
1160
1161 static ssize_t es1370_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
1162 {
1163         struct es1370_state *s = (struct es1370_state *)file->private_data;
1164         DECLARE_WAITQUEUE(wait, current);
1165         ssize_t ret = 0;
1166         unsigned long flags;
1167         unsigned swptr;
1168         int cnt;
1169
1170         VALIDATE_STATE(s);
1171         if (ppos != &file->f_pos)
1172                 return -ESPIPE;
1173         if (s->dma_adc.mapped)
1174                 return -ENXIO;
1175         if (!access_ok(VERIFY_WRITE, buffer, count))
1176                 return -EFAULT;
1177         down(&s->sem);  
1178         if (!s->dma_adc.ready && (ret = prog_dmabuf_adc(s)))
1179                 goto out;
1180         
1181         add_wait_queue(&s->dma_adc.wait, &wait);
1182         while (count > 0) {
1183                 spin_lock_irqsave(&s->lock, flags);
1184                 swptr = s->dma_adc.swptr;
1185                 cnt = s->dma_adc.dmasize-swptr;
1186                 if (s->dma_adc.count < cnt)
1187                         cnt = s->dma_adc.count;
1188                 if (cnt <= 0)
1189                         __set_current_state(TASK_INTERRUPTIBLE);
1190                 spin_unlock_irqrestore(&s->lock, flags);
1191                 if (cnt > count)
1192                         cnt = count;
1193                 if (cnt <= 0) {
1194                         if (s->dma_adc.enabled)
1195                                 start_adc(s);
1196                         if (file->f_flags & O_NONBLOCK) {
1197                                 if (!ret)
1198                                         ret = -EAGAIN;
1199                                 goto out;
1200                         }
1201                         up(&s->sem);
1202                         schedule();
1203                         if (signal_pending(current)) {
1204                                 if (!ret)
1205                                         ret = -ERESTARTSYS;
1206                                 goto out;
1207                         }
1208                         down(&s->sem);
1209                         if (s->dma_adc.mapped)
1210                         {
1211                                 ret = -ENXIO;
1212                                 goto out;
1213                         }
1214                         continue;
1215                 }
1216                 if (copy_to_user(buffer, s->dma_adc.rawbuf + swptr, cnt)) {
1217                         if (!ret)
1218                                 ret = -EFAULT;
1219                         goto out;
1220                 }
1221                 swptr = (swptr + cnt) % s->dma_adc.dmasize;
1222                 spin_lock_irqsave(&s->lock, flags);
1223                 s->dma_adc.swptr = swptr;
1224                 s->dma_adc.count -= cnt;
1225                 spin_unlock_irqrestore(&s->lock, flags);
1226                 count -= cnt;
1227                 buffer += cnt;
1228                 ret += cnt;
1229                 if (s->dma_adc.enabled)
1230                         start_adc(s);
1231         }
1232 out:
1233         up(&s->sem);
1234         remove_wait_queue(&s->dma_adc.wait, &wait);
1235         set_current_state(TASK_RUNNING);
1236         return ret;
1237 }
1238
1239 static ssize_t es1370_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
1240 {
1241         struct es1370_state *s = (struct es1370_state *)file->private_data;
1242         DECLARE_WAITQUEUE(wait, current);
1243         ssize_t ret;
1244         unsigned long flags;
1245         unsigned swptr;
1246         int cnt;
1247
1248         VALIDATE_STATE(s);
1249         if (ppos != &file->f_pos)
1250                 return -ESPIPE;
1251         if (s->dma_dac2.mapped)
1252                 return -ENXIO;
1253         if (!access_ok(VERIFY_READ, buffer, count))
1254                 return -EFAULT;
1255         down(&s->sem);  
1256         if (!s->dma_dac2.ready && (ret = prog_dmabuf_dac2(s)))
1257                 goto out;
1258         ret = 0;
1259         add_wait_queue(&s->dma_dac2.wait, &wait);
1260         while (count > 0) {
1261                 spin_lock_irqsave(&s->lock, flags);
1262                 if (s->dma_dac2.count < 0) {
1263                         s->dma_dac2.count = 0;
1264                         s->dma_dac2.swptr = s->dma_dac2.hwptr;
1265                 }
1266                 swptr = s->dma_dac2.swptr;
1267                 cnt = s->dma_dac2.dmasize-swptr;
1268                 if (s->dma_dac2.count + cnt > s->dma_dac2.dmasize)
1269                         cnt = s->dma_dac2.dmasize - s->dma_dac2.count;
1270                 if (cnt <= 0)
1271                         __set_current_state(TASK_INTERRUPTIBLE);
1272                 spin_unlock_irqrestore(&s->lock, flags);
1273                 if (cnt > count)
1274                         cnt = count;
1275                 if (cnt <= 0) {
1276                         if (s->dma_dac2.enabled)
1277                                 start_dac2(s);
1278                         if (file->f_flags & O_NONBLOCK) {
1279                                 if (!ret)
1280                                         ret = -EAGAIN;
1281                                 goto out;
1282                         }
1283                         up(&s->sem);
1284                         schedule();
1285                         if (signal_pending(current)) {
1286                                 if (!ret)
1287                                         ret = -ERESTARTSYS;
1288                                 goto out;       
1289                         }
1290                         down(&s->sem);
1291                         if (s->dma_dac2.mapped)
1292                         {
1293                         ret = -ENXIO;
1294                         goto out;
1295                         }
1296                         continue;
1297                 }
1298                 if (copy_from_user(s->dma_dac2.rawbuf + swptr, buffer, cnt)) {
1299                         if (!ret)
1300                                 ret = -EFAULT;
1301                         goto out;
1302                 }
1303                 swptr = (swptr + cnt) % s->dma_dac2.dmasize;
1304                 spin_lock_irqsave(&s->lock, flags);
1305                 s->dma_dac2.swptr = swptr;
1306                 s->dma_dac2.count += cnt;
1307                 s->dma_dac2.endcleared = 0;
1308                 spin_unlock_irqrestore(&s->lock, flags);
1309                 count -= cnt;
1310                 buffer += cnt;
1311                 ret += cnt;
1312                 if (s->dma_dac2.enabled)
1313                         start_dac2(s);
1314         }
1315 out:
1316         up(&s->sem);
1317         remove_wait_queue(&s->dma_dac2.wait, &wait);
1318         set_current_state(TASK_RUNNING);
1319         return ret;
1320 }
1321
1322 /* No kernel lock - we have our own spinlock */
1323 static unsigned int es1370_poll(struct file *file, struct poll_table_struct *wait)
1324 {
1325         struct es1370_state *s = (struct es1370_state *)file->private_data;
1326         unsigned long flags;
1327         unsigned int mask = 0;
1328
1329         VALIDATE_STATE(s);
1330         if (file->f_mode & FMODE_WRITE) {
1331                 if (!s->dma_dac2.ready && prog_dmabuf_dac2(s))
1332                         return 0;
1333                 poll_wait(file, &s->dma_dac2.wait, wait);
1334         }
1335         if (file->f_mode & FMODE_READ) {
1336                 if (!s->dma_adc.ready && prog_dmabuf_adc(s))
1337                         return 0;
1338                 poll_wait(file, &s->dma_adc.wait, wait);
1339         }
1340         spin_lock_irqsave(&s->lock, flags);
1341         es1370_update_ptr(s);
1342         if (file->f_mode & FMODE_READ) {
1343                 if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
1344                         mask |= POLLIN | POLLRDNORM;
1345         }
1346         if (file->f_mode & FMODE_WRITE) {
1347                 if (s->dma_dac2.mapped) {
1348                         if (s->dma_dac2.count >= (signed)s->dma_dac2.fragsize) 
1349                                 mask |= POLLOUT | POLLWRNORM;
1350                 } else {
1351                         if ((signed)s->dma_dac2.dmasize >= s->dma_dac2.count + (signed)s->dma_dac2.fragsize)
1352                                 mask |= POLLOUT | POLLWRNORM;
1353                 }
1354         }
1355         spin_unlock_irqrestore(&s->lock, flags);
1356         return mask;
1357 }
1358
1359 static int es1370_mmap(struct file *file, struct vm_area_struct *vma)
1360 {
1361         struct es1370_state *s = (struct es1370_state *)file->private_data;
1362         struct dmabuf *db;
1363         int ret = 0;
1364         unsigned long size;
1365
1366         VALIDATE_STATE(s);
1367         lock_kernel();
1368         down(&s->sem);
1369         if (vma->vm_flags & VM_WRITE) {
1370                 if ((ret = prog_dmabuf_dac2(s)) != 0) {
1371                         goto out;
1372                 }
1373                 db = &s->dma_dac2;
1374         } else if (vma->vm_flags & VM_READ) {
1375                 if ((ret = prog_dmabuf_adc(s)) != 0) {
1376                         goto out;
1377                 }
1378                 db = &s->dma_adc;
1379         } else  {
1380                 ret = -EINVAL;
1381                 goto out;
1382         }
1383         if (vma->vm_pgoff != 0) {
1384                 ret = -EINVAL;
1385                 goto out;
1386         }
1387         size = vma->vm_end - vma->vm_start;
1388         if (size > (PAGE_SIZE << db->buforder)) {
1389                 ret = -EINVAL;
1390                 goto out;
1391         }
1392         if (remap_page_range(vma->vm_start, virt_to_phys(db->rawbuf), size, vma->vm_page_prot)) {
1393                 ret = -EAGAIN;
1394                 goto out;
1395         }
1396         db->mapped = 1;
1397 out:
1398         up(&s->sem);
1399         unlock_kernel();
1400         return ret;
1401 }
1402
1403 static int es1370_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1404 {
1405         struct es1370_state *s = (struct es1370_state *)file->private_data;
1406         unsigned long flags;
1407         audio_buf_info abinfo;
1408         count_info cinfo;
1409         int count;
1410         int val, mapped, ret;
1411
1412         VALIDATE_STATE(s);
1413         mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac2.mapped) ||
1414                 ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
1415         switch (cmd) {
1416         case OSS_GETVERSION:
1417                 return put_user(SOUND_VERSION, (int *)arg);
1418
1419         case SNDCTL_DSP_SYNC:
1420                 if (file->f_mode & FMODE_WRITE)
1421                         return drain_dac2(s, 0/*file->f_flags & O_NONBLOCK*/);
1422                 return 0;
1423                 
1424         case SNDCTL_DSP_SETDUPLEX:
1425                 return 0;
1426
1427         case SNDCTL_DSP_GETCAPS:
1428                 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP, (int *)arg);
1429                 
1430         case SNDCTL_DSP_RESET:
1431                 if (file->f_mode & FMODE_WRITE) {
1432                         stop_dac2(s);
1433                         synchronize_irq();
1434                         s->dma_dac2.swptr = s->dma_dac2.hwptr = s->dma_dac2.count = s->dma_dac2.total_bytes = 0;
1435                 }
1436                 if (file->f_mode & FMODE_READ) {
1437                         stop_adc(s);
1438                         synchronize_irq();
1439                         s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes = 0;
1440                 }
1441                 return 0;
1442
1443         case SNDCTL_DSP_SPEED:
1444                 if (get_user(val, (int *)arg))
1445                         return -EFAULT;
1446                 if (val >= 0) {
1447                         if (s->open_mode & (~file->f_mode) & (FMODE_READ|FMODE_WRITE))
1448                                 return -EINVAL;
1449                         if (val < 4000)
1450                                 val = 4000;
1451                         if (val > 50000)
1452                                 val = 50000;
1453                         stop_adc(s);
1454                         stop_dac2(s);
1455                         s->dma_adc.ready = s->dma_dac2.ready = 0;
1456                         spin_lock_irqsave(&s->lock, flags);
1457                         s->ctrl = (s->ctrl & ~CTRL_PCLKDIV) | (DAC2_SRTODIV(val) << CTRL_SH_PCLKDIV);
1458                         outl(s->ctrl, s->io+ES1370_REG_CONTROL);
1459                         spin_unlock_irqrestore(&s->lock, flags);
1460                 }
1461                 return put_user(DAC2_DIVTOSR((s->ctrl & CTRL_PCLKDIV) >> CTRL_SH_PCLKDIV), (int *)arg);
1462                 
1463         case SNDCTL_DSP_STEREO:
1464                 if (get_user(val, (int *)arg))
1465                         return -EFAULT;
1466                 if (file->f_mode & FMODE_READ) {
1467                         stop_adc(s);
1468                         s->dma_adc.ready = 0;
1469                         spin_lock_irqsave(&s->lock, flags);
1470                         if (val)
1471                                 s->sctrl |= SCTRL_R1SMB;
1472                         else
1473                                 s->sctrl &= ~SCTRL_R1SMB;
1474                         outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
1475                         spin_unlock_irqrestore(&s->lock, flags);
1476                 }
1477                 if (file->f_mode & FMODE_WRITE) {
1478                         stop_dac2(s);
1479                         s->dma_dac2.ready = 0;
1480                         spin_lock_irqsave(&s->lock, flags);
1481                         if (val)
1482                                 s->sctrl |= SCTRL_P2SMB;
1483                         else
1484                                 s->sctrl &= ~SCTRL_P2SMB;
1485                         outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
1486                         spin_unlock_irqrestore(&s->lock, flags);
1487                 }
1488                 return 0;
1489
1490         case SNDCTL_DSP_CHANNELS:
1491                 if (get_user(val, (int *)arg))
1492                         return -EFAULT;
1493                 if (val != 0) {
1494                         if (file->f_mode & FMODE_READ) {
1495                                 stop_adc(s);
1496                                 s->dma_adc.ready = 0;
1497                                 spin_lock_irqsave(&s->lock, flags);
1498                                 if (val >= 2)
1499                                         s->sctrl |= SCTRL_R1SMB;
1500                                 else
1501                                         s->sctrl &= ~SCTRL_R1SMB;
1502                                 outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
1503                                 spin_unlock_irqrestore(&s->lock, flags);
1504                         }
1505                         if (file->f_mode & FMODE_WRITE) {
1506                                 stop_dac2(s);
1507                                 s->dma_dac2.ready = 0;
1508                                 spin_lock_irqsave(&s->lock, flags);
1509                                 if (val >= 2)
1510                                         s->sctrl |= SCTRL_P2SMB;
1511                                 else
1512                                         s->sctrl &= ~SCTRL_P2SMB;
1513                                 outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
1514                                 spin_unlock_irqrestore(&s->lock, flags);
1515                         }
1516                 }
1517                 return put_user((s->sctrl & ((file->f_mode & FMODE_READ) ? SCTRL_R1SMB : SCTRL_P2SMB)) ? 2 : 1, (int *)arg);
1518                 
1519         case SNDCTL_DSP_GETFMTS: /* Returns a mask */
1520                 return put_user(AFMT_S16_LE|AFMT_U8, (int *)arg);
1521                 
1522         case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
1523                 if (get_user(val, (int *)arg))
1524                         return -EFAULT;
1525                 if (val != AFMT_QUERY) {
1526                         if (file->f_mode & FMODE_READ) {
1527                                 stop_adc(s);
1528                                 s->dma_adc.ready = 0;
1529                                 spin_lock_irqsave(&s->lock, flags);
1530                                 if (val == AFMT_S16_LE)
1531                                         s->sctrl |= SCTRL_R1SEB;
1532                                 else
1533                                         s->sctrl &= ~SCTRL_R1SEB;
1534                                 outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
1535                                 spin_unlock_irqrestore(&s->lock, flags);
1536                         }
1537                         if (file->f_mode & FMODE_WRITE) {
1538                                 stop_dac2(s);
1539                                 s->dma_dac2.ready = 0;
1540                                 spin_lock_irqsave(&s->lock, flags);
1541                                 if (val == AFMT_S16_LE)
1542                                         s->sctrl |= SCTRL_P2SEB;
1543                                 else
1544                                         s->sctrl &= ~SCTRL_P2SEB;
1545                                 outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
1546                                 spin_unlock_irqrestore(&s->lock, flags);
1547                         }
1548                 }
1549                 return put_user((s->sctrl & ((file->f_mode & FMODE_READ) ? SCTRL_R1SEB : SCTRL_P2SEB)) ? 
1550                                 AFMT_S16_LE : AFMT_U8, (int *)arg);
1551                 
1552         case SNDCTL_DSP_POST:
1553                 return 0;
1554
1555         case SNDCTL_DSP_GETTRIGGER:
1556                 val = 0;
1557                 if (file->f_mode & FMODE_READ && s->ctrl & CTRL_ADC_EN) 
1558                         val |= PCM_ENABLE_INPUT;
1559                 if (file->f_mode & FMODE_WRITE && s->ctrl & CTRL_DAC2_EN) 
1560                         val |= PCM_ENABLE_OUTPUT;
1561                 return put_user(val, (int *)arg);
1562                 
1563         case SNDCTL_DSP_SETTRIGGER:
1564                 if (get_user(val, (int *)arg))
1565                         return -EFAULT;
1566                 if (file->f_mode & FMODE_READ) {
1567                         if (val & PCM_ENABLE_INPUT) {
1568                                 if (!s->dma_adc.ready && (ret = prog_dmabuf_adc(s)))
1569                                         return ret;
1570                                 s->dma_adc.enabled = 1;
1571                                 start_adc(s);
1572                         } else {
1573                                 s->dma_adc.enabled = 0;
1574                                 stop_adc(s);
1575                         }
1576                 }
1577                 if (file->f_mode & FMODE_WRITE) {
1578                         if (val & PCM_ENABLE_OUTPUT) {
1579                                 if (!s->dma_dac2.ready && (ret = prog_dmabuf_dac2(s)))
1580                                         return ret;
1581                                 s->dma_dac2.enabled = 1;
1582                                 start_dac2(s);
1583                         } else {
1584                                 s->dma_dac2.enabled = 0;
1585                                 stop_dac2(s);
1586                         }
1587                 }
1588                 return 0;
1589
1590         case SNDCTL_DSP_GETOSPACE:
1591                 if (!(file->f_mode & FMODE_WRITE))
1592                         return -EINVAL;
1593                 if (!s->dma_dac2.ready && (val = prog_dmabuf_dac2(s)) != 0)
1594                         return val;
1595                 spin_lock_irqsave(&s->lock, flags);
1596                 es1370_update_ptr(s);
1597                 abinfo.fragsize = s->dma_dac2.fragsize;
1598                 count = s->dma_dac2.count;
1599                 if (count < 0)
1600                         count = 0;
1601                 abinfo.bytes = s->dma_dac2.dmasize - count;
1602                 abinfo.fragstotal = s->dma_dac2.numfrag;
1603                 abinfo.fragments = abinfo.bytes >> s->dma_dac2.fragshift;      
1604                 spin_unlock_irqrestore(&s->lock, flags);
1605                 return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
1606
1607         case SNDCTL_DSP_GETISPACE:
1608                 if (!(file->f_mode & FMODE_READ))
1609                         return -EINVAL;
1610                 if (!s->dma_adc.ready && (val = prog_dmabuf_adc(s)) != 0)
1611                         return val;
1612                 spin_lock_irqsave(&s->lock, flags);
1613                 es1370_update_ptr(s);
1614                 abinfo.fragsize = s->dma_adc.fragsize;
1615                 count = s->dma_adc.count;
1616                 if (count < 0)
1617                         count = 0;
1618                 abinfo.bytes = count;
1619                 abinfo.fragstotal = s->dma_adc.numfrag;
1620                 abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;      
1621                 spin_unlock_irqrestore(&s->lock, flags);
1622                 return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
1623                 
1624         case SNDCTL_DSP_NONBLOCK:
1625                 file->f_flags |= O_NONBLOCK;
1626                 return 0;
1627
1628         case SNDCTL_DSP_GETODELAY:
1629                 if (!(file->f_mode & FMODE_WRITE))
1630                         return -EINVAL;
1631                 if (!s->dma_dac2.ready && (val = prog_dmabuf_dac2(s)) != 0)
1632                         return val;
1633                 spin_lock_irqsave(&s->lock, flags);
1634                 es1370_update_ptr(s);
1635                 count = s->dma_dac2.count;
1636                 spin_unlock_irqrestore(&s->lock, flags);
1637                 if (count < 0)
1638                         count = 0;
1639                 return put_user(count, (int *)arg);
1640
1641         case SNDCTL_DSP_GETIPTR:
1642                 if (!(file->f_mode & FMODE_READ))
1643                         return -EINVAL;
1644                 if (!s->dma_adc.ready && (val = prog_dmabuf_adc(s)) != 0)
1645                         return val;
1646                 spin_lock_irqsave(&s->lock, flags);
1647                 es1370_update_ptr(s);
1648                 cinfo.bytes = s->dma_adc.total_bytes;
1649                 count = s->dma_adc.count;
1650                 if (count < 0)
1651                         count = 0;
1652                 cinfo.blocks = count >> s->dma_adc.fragshift;
1653                 cinfo.ptr = s->dma_adc.hwptr;
1654                 if (s->dma_adc.mapped)
1655                         s->dma_adc.count &= s->dma_adc.fragsize-1;
1656                 spin_unlock_irqrestore(&s->lock, flags);
1657                 return copy_to_user((void *)arg, &cinfo, sizeof(cinfo));
1658
1659         case SNDCTL_DSP_GETOPTR:
1660                 if (!(file->f_mode & FMODE_WRITE))
1661                         return -EINVAL;
1662                 if (!s->dma_dac2.ready && (val = prog_dmabuf_dac2(s)) != 0)
1663                         return val;
1664                 spin_lock_irqsave(&s->lock, flags);
1665                 es1370_update_ptr(s);
1666                 cinfo.bytes = s->dma_dac2.total_bytes;
1667                 count = s->dma_dac2.count;
1668                 if (count < 0)
1669                         count = 0;
1670                 cinfo.blocks = count >> s->dma_dac2.fragshift;
1671                 cinfo.ptr = s->dma_dac2.hwptr;
1672                 if (s->dma_dac2.mapped)
1673                         s->dma_dac2.count &= s->dma_dac2.fragsize-1;
1674                 spin_unlock_irqrestore(&s->lock, flags);
1675                 return copy_to_user((void *)arg, &cinfo, sizeof(cinfo));
1676
1677         case SNDCTL_DSP_GETBLKSIZE:
1678                 if (file->f_mode & FMODE_WRITE) {
1679                         if ((val = prog_dmabuf_dac2(s)))
1680                                 return val;
1681                         return put_user(s->dma_dac2.fragsize, (int *)arg);
1682                 }
1683                 if ((val = prog_dmabuf_adc(s)))
1684                         return val;
1685                 return put_user(s->dma_adc.fragsize, (int *)arg);
1686
1687         case SNDCTL_DSP_SETFRAGMENT:
1688                 if (get_user(val, (int *)arg))
1689                         return -EFAULT;
1690                 if (file->f_mode & FMODE_READ) {
1691                         s->dma_adc.ossfragshift = val & 0xffff;
1692                         s->dma_adc.ossmaxfrags = (val >> 16) & 0xffff;
1693                         if (s->dma_adc.ossfragshift < 4)
1694                                 s->dma_adc.ossfragshift = 4;
1695                         if (s->dma_adc.ossfragshift > 15)
1696                                 s->dma_adc.ossfragshift = 15;
1697                         if (s->dma_adc.ossmaxfrags < 4)
1698                                 s->dma_adc.ossmaxfrags = 4;
1699                 }
1700                 if (file->f_mode & FMODE_WRITE) {
1701                         s->dma_dac2.ossfragshift = val & 0xffff;
1702                         s->dma_dac2.ossmaxfrags = (val >> 16) & 0xffff;
1703                         if (s->dma_dac2.ossfragshift < 4)
1704                                 s->dma_dac2.ossfragshift = 4;
1705                         if (s->dma_dac2.ossfragshift > 15)
1706                                 s->dma_dac2.ossfragshift = 15;
1707                         if (s->dma_dac2.ossmaxfrags < 4)
1708                                 s->dma_dac2.ossmaxfrags = 4;
1709                 }
1710                 return 0;
1711
1712         case SNDCTL_DSP_SUBDIVIDE:
1713                 if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
1714                     (file->f_mode & FMODE_WRITE && s->dma_dac2.subdivision))
1715                         return -EINVAL;
1716                 if (get_user(val, (int *)arg))
1717                         return -EFAULT;
1718                 if (val != 1 && val != 2 && val != 4)
1719                         return -EINVAL;
1720                 if (file->f_mode & FMODE_READ)
1721                         s->dma_adc.subdivision = val;
1722                 if (file->f_mode & FMODE_WRITE)
1723                         s->dma_dac2.subdivision = val;
1724                 return 0;
1725
1726         case SOUND_PCM_READ_RATE:
1727                 return put_user(DAC2_DIVTOSR((s->ctrl & CTRL_PCLKDIV) >> CTRL_SH_PCLKDIV), (int *)arg);
1728
1729         case SOUND_PCM_READ_CHANNELS:
1730                 return put_user((s->sctrl & ((file->f_mode & FMODE_READ) ? SCTRL_R1SMB : SCTRL_P2SMB)) ?
1731                                 2 : 1, (int *)arg);
1732
1733         case SOUND_PCM_READ_BITS:
1734                 return put_user((s->sctrl & ((file->f_mode & FMODE_READ) ? SCTRL_R1SEB : SCTRL_P2SEB)) ? 
1735                                 16 : 8, (int *)arg);
1736
1737         case SOUND_PCM_WRITE_FILTER:
1738         case SNDCTL_DSP_SETSYNCRO:
1739         case SOUND_PCM_READ_FILTER:
1740                 return -EINVAL;
1741                 
1742         }
1743         return mixer_ioctl(s, cmd, arg);
1744 }
1745
1746 static int es1370_open(struct inode *inode, struct file *file)
1747 {
1748         int minor = MINOR(inode->i_rdev);
1749         DECLARE_WAITQUEUE(wait, current);
1750         unsigned long flags;
1751         struct list_head *list;
1752         struct es1370_state *s;
1753
1754         for (list = devs.next; ; list = list->next) {
1755                 if (list == &devs)
1756                         return -ENODEV;
1757                 s = list_entry(list, struct es1370_state, devs);
1758                 if (!((s->dev_audio ^ minor) & ~0xf))
1759                         break;
1760         }
1761         VALIDATE_STATE(s);
1762         file->private_data = s;
1763         /* wait for device to become free */
1764         down(&s->open_sem);
1765         while (s->open_mode & file->f_mode) {
1766                 if (file->f_flags & O_NONBLOCK) {
1767                         up(&s->open_sem);
1768                         return -EBUSY;
1769                 }
1770                 add_wait_queue(&s->open_wait, &wait);
1771                 __set_current_state(TASK_INTERRUPTIBLE);
1772                 up(&s->open_sem);
1773                 schedule();
1774                 remove_wait_queue(&s->open_wait, &wait);
1775                 set_current_state(TASK_RUNNING);
1776                 if (signal_pending(current))
1777                         return -ERESTARTSYS;
1778                 down(&s->open_sem);
1779         }
1780         spin_lock_irqsave(&s->lock, flags);
1781         if (!(s->open_mode & (FMODE_READ|FMODE_WRITE)))
1782                 s->ctrl = (s->ctrl & ~CTRL_PCLKDIV) | (DAC2_SRTODIV(8000) << CTRL_SH_PCLKDIV);
1783         if (file->f_mode & FMODE_READ) {
1784                 s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags = s->dma_adc.subdivision = 0;
1785                 s->dma_adc.enabled = 1;
1786                 s->sctrl &= ~SCTRL_R1FMT;
1787                 if ((minor & 0xf) == SND_DEV_DSP16)
1788                         s->sctrl |= ES1370_FMT_S16_MONO << SCTRL_SH_R1FMT;
1789                 else
1790                         s->sctrl |= ES1370_FMT_U8_MONO << SCTRL_SH_R1FMT;
1791         }
1792         if (file->f_mode & FMODE_WRITE) {
1793                 s->dma_dac2.ossfragshift = s->dma_dac2.ossmaxfrags = s->dma_dac2.subdivision = 0;
1794                 s->dma_dac2.enabled = 1;
1795                 s->sctrl &= ~SCTRL_P2FMT;
1796                 if ((minor & 0xf) == SND_DEV_DSP16)
1797                         s->sctrl |= ES1370_FMT_S16_MONO << SCTRL_SH_P2FMT;
1798                 else
1799                         s->sctrl |= ES1370_FMT_U8_MONO << SCTRL_SH_P2FMT;
1800         }
1801         outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
1802         outl(s->ctrl, s->io+ES1370_REG_CONTROL);
1803         spin_unlock_irqrestore(&s->lock, flags);
1804         s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
1805         up(&s->open_sem);
1806         init_MUTEX(&s->sem);
1807         return 0;
1808 }
1809
1810 static int es1370_release(struct inode *inode, struct file *file)
1811 {
1812         struct es1370_state *s = (struct es1370_state *)file->private_data;
1813
1814         VALIDATE_STATE(s);
1815         lock_kernel();
1816         if (file->f_mode & FMODE_WRITE)
1817                 drain_dac2(s, file->f_flags & O_NONBLOCK);
1818         down(&s->open_sem);
1819         if (file->f_mode & FMODE_WRITE) {
1820                 stop_dac2(s);
1821                 synchronize_irq();
1822                 dealloc_dmabuf(s, &s->dma_dac2);
1823         }
1824         if (file->f_mode & FMODE_READ) {
1825                 stop_adc(s);
1826                 dealloc_dmabuf(s, &s->dma_adc);
1827         }
1828         s->open_mode &= (~file->f_mode) & (FMODE_READ|FMODE_WRITE);
1829         wake_up(&s->open_wait);
1830         up(&s->open_sem);
1831         unlock_kernel();
1832         return 0;
1833 }
1834
1835 static /*const*/ struct file_operations es1370_audio_fops = {
1836         owner:          THIS_MODULE,
1837         llseek:         no_llseek,
1838         read:           es1370_read,
1839         write:          es1370_write,
1840         poll:           es1370_poll,
1841         ioctl:          es1370_ioctl,
1842         mmap:           es1370_mmap,
1843         open:           es1370_open,
1844         release:        es1370_release,
1845 };
1846
1847 /* --------------------------------------------------------------------- */
1848
1849 static ssize_t es1370_write_dac(struct file *file, const char *buffer, size_t count, loff_t *ppos)
1850 {
1851         struct es1370_state *s = (struct es1370_state *)file->private_data;
1852         DECLARE_WAITQUEUE(wait, current);
1853         ssize_t ret = 0;
1854         unsigned long flags;
1855         unsigned swptr;
1856         int cnt;
1857
1858         VALIDATE_STATE(s);
1859         if (ppos != &file->f_pos)
1860                 return -ESPIPE;
1861         if (s->dma_dac1.mapped)
1862                 return -ENXIO;
1863         if (!s->dma_dac1.ready && (ret = prog_dmabuf_dac1(s)))
1864                 return ret;
1865         if (!access_ok(VERIFY_READ, buffer, count))
1866                 return -EFAULT;
1867         add_wait_queue(&s->dma_dac1.wait, &wait);
1868         while (count > 0) {
1869                 spin_lock_irqsave(&s->lock, flags);
1870                 if (s->dma_dac1.count < 0) {
1871                         s->dma_dac1.count = 0;
1872                         s->dma_dac1.swptr = s->dma_dac1.hwptr;
1873                 }
1874                 swptr = s->dma_dac1.swptr;
1875                 cnt = s->dma_dac1.dmasize-swptr;
1876                 if (s->dma_dac1.count + cnt > s->dma_dac1.dmasize)
1877                         cnt = s->dma_dac1.dmasize - s->dma_dac1.count;
1878                 if (cnt <= 0)
1879                         __set_current_state(TASK_INTERRUPTIBLE);
1880                 spin_unlock_irqrestore(&s->lock, flags);
1881                 if (cnt > count)
1882                         cnt = count;
1883                 if (cnt <= 0) {
1884                         if (s->dma_dac1.enabled)
1885                                 start_dac1(s);
1886                         if (file->f_flags & O_NONBLOCK) {
1887                                 if (!ret)
1888                                         ret = -EAGAIN;
1889                                 break;
1890                         }
1891                         schedule();
1892                         if (signal_pending(current)) {
1893                                 if (!ret)
1894                                         ret = -ERESTARTSYS;
1895                                 break;
1896                         }
1897                         continue;
1898                 }
1899                 if (copy_from_user(s->dma_dac1.rawbuf + swptr, buffer, cnt)) {
1900                         if (!ret)
1901                                 ret = -EFAULT;
1902                         break;
1903                 }
1904                 swptr = (swptr + cnt) % s->dma_dac1.dmasize;
1905                 spin_lock_irqsave(&s->lock, flags);
1906                 s->dma_dac1.swptr = swptr;
1907                 s->dma_dac1.count += cnt;
1908                 s->dma_dac1.endcleared = 0;
1909                 spin_unlock_irqrestore(&s->lock, flags);
1910                 count -= cnt;
1911                 buffer += cnt;
1912                 ret += cnt;
1913                 if (s->dma_dac1.enabled)
1914                         start_dac1(s);
1915         }
1916         remove_wait_queue(&s->dma_dac1.wait, &wait);
1917         set_current_state(TASK_RUNNING);
1918         return ret;
1919 }
1920
1921 /* No kernel lock - we have our own spinlock */
1922 static unsigned int es1370_poll_dac(struct file *file, struct poll_table_struct *wait)
1923 {
1924         struct es1370_state *s = (struct es1370_state *)file->private_data;
1925         unsigned long flags;
1926         unsigned int mask = 0;
1927
1928         VALIDATE_STATE(s);
1929         if (!s->dma_dac1.ready && prog_dmabuf_dac1(s))
1930                 return 0;
1931         poll_wait(file, &s->dma_dac1.wait, wait);
1932         spin_lock_irqsave(&s->lock, flags);
1933         es1370_update_ptr(s);
1934         if (s->dma_dac1.mapped) {
1935                 if (s->dma_dac1.count >= (signed)s->dma_dac1.fragsize)
1936                         mask |= POLLOUT | POLLWRNORM;
1937         } else {
1938                 if ((signed)s->dma_dac1.dmasize >= s->dma_dac1.count + (signed)s->dma_dac1.fragsize)
1939                         mask |= POLLOUT | POLLWRNORM;
1940         }
1941         spin_unlock_irqrestore(&s->lock, flags);
1942         return mask;
1943 }
1944
1945 static int es1370_mmap_dac(struct file *file, struct vm_area_struct *vma)
1946 {
1947         struct es1370_state *s = (struct es1370_state *)file->private_data;
1948         int ret;
1949         unsigned long size;
1950
1951         VALIDATE_STATE(s);
1952         if (!(vma->vm_flags & VM_WRITE))
1953                 return -EINVAL;
1954         lock_kernel();
1955         if ((ret = prog_dmabuf_dac1(s)) != 0)
1956                 goto out;
1957         ret = -EINVAL;
1958         if (vma->vm_pgoff != 0)
1959                 goto out;
1960         size = vma->vm_end - vma->vm_start;
1961         if (size > (PAGE_SIZE << s->dma_dac1.buforder))
1962                 goto out;
1963         ret = -EAGAIN;
1964         if (remap_page_range(vma->vm_start, virt_to_phys(s->dma_dac1.rawbuf), size, vma->vm_page_prot))
1965                 goto out;
1966         s->dma_dac1.mapped = 1;
1967         ret = 0;
1968 out:
1969         unlock_kernel();
1970         return ret;
1971 }
1972
1973 static int es1370_ioctl_dac(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1974 {
1975         struct es1370_state *s = (struct es1370_state *)file->private_data;
1976         unsigned long flags;
1977         audio_buf_info abinfo;
1978         count_info cinfo;
1979         int count;
1980         unsigned ctrl;
1981         int val, ret;
1982
1983         VALIDATE_STATE(s);
1984         switch (cmd) {
1985         case OSS_GETVERSION:
1986                 return put_user(SOUND_VERSION, (int *)arg);
1987
1988         case SNDCTL_DSP_SYNC:
1989                 return drain_dac1(s, 0/*file->f_flags & O_NONBLOCK*/);
1990                 
1991         case SNDCTL_DSP_SETDUPLEX:
1992                 return -EINVAL;
1993
1994         case SNDCTL_DSP_GETCAPS:
1995                 return put_user(DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP, (int *)arg);
1996                 
1997         case SNDCTL_DSP_RESET:
1998                 stop_dac1(s);
1999                 synchronize_irq();
2000                 s->dma_dac1.swptr = s->dma_dac1.hwptr = s->dma_dac1.count = s->dma_dac1.total_bytes = 0;
2001                 return 0;
2002
2003         case SNDCTL_DSP_SPEED:
2004                 if (get_user(val, (int *)arg))
2005                         return -EFAULT;
2006                 if (val >= 0) {
2007                         stop_dac1(s);
2008                         s->dma_dac1.ready = 0;
2009                         for (ctrl = 0; ctrl <= 2; ctrl++)
2010                                 if (val < (dac1_samplerate[ctrl] + dac1_samplerate[ctrl+1]) / 2)
2011                                         break;
2012                         spin_lock_irqsave(&s->lock, flags);
2013                         s->ctrl = (s->ctrl & ~CTRL_WTSRSEL) | (ctrl << CTRL_SH_WTSRSEL);
2014                         outl(s->ctrl, s->io+ES1370_REG_CONTROL);
2015                         spin_unlock_irqrestore(&s->lock, flags);
2016                 }
2017                 return put_user(dac1_samplerate[(s->ctrl & CTRL_WTSRSEL) >> CTRL_SH_WTSRSEL], (int *)arg);
2018                 
2019         case SNDCTL_DSP_STEREO:
2020                 if (get_user(val, (int *)arg))
2021                         return -EFAULT;
2022                 stop_dac1(s);
2023                 s->dma_dac1.ready = 0;
2024                 spin_lock_irqsave(&s->lock, flags);
2025                 if (val)
2026                         s->sctrl |= SCTRL_P1SMB;
2027                 else
2028                         s->sctrl &= ~SCTRL_P1SMB;
2029                 outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
2030                 spin_unlock_irqrestore(&s->lock, flags);
2031                 return 0;
2032
2033         case SNDCTL_DSP_CHANNELS:
2034                 if (get_user(val, (int *)arg))
2035                         return -EFAULT;
2036                 if (val != 0) {
2037                         if (s->dma_dac1.mapped)
2038                                 return -EINVAL;
2039                         stop_dac1(s);
2040                         s->dma_dac1.ready = 0;
2041                         spin_lock_irqsave(&s->lock, flags);
2042                         if (val >= 2)
2043                                 s->sctrl |= SCTRL_P1SMB;
2044                         else
2045                                 s->sctrl &= ~SCTRL_P1SMB;
2046                         outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
2047                         spin_unlock_irqrestore(&s->lock, flags);
2048                 }
2049                 return put_user((s->sctrl & SCTRL_P1SMB) ? 2 : 1, (int *)arg);
2050                 
2051         case SNDCTL_DSP_GETFMTS: /* Returns a mask */
2052                 return put_user(AFMT_S16_LE|AFMT_U8, (int *)arg);
2053                 
2054         case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
2055                 if (get_user(val, (int *)arg))
2056                         return -EFAULT;
2057                 if (val != AFMT_QUERY) {
2058                         stop_dac1(s);
2059                         s->dma_dac1.ready = 0;
2060                         spin_lock_irqsave(&s->lock, flags);
2061                         if (val == AFMT_S16_LE)
2062                                 s->sctrl |= SCTRL_P1SEB;
2063                         else
2064                                 s->sctrl &= ~SCTRL_P1SEB;
2065                         outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
2066                         spin_unlock_irqrestore(&s->lock, flags);
2067                 }
2068                 return put_user((s->sctrl & SCTRL_P1SEB) ? AFMT_S16_LE : AFMT_U8, (int *)arg);
2069
2070         case SNDCTL_DSP_POST:
2071                 return 0;
2072
2073         case SNDCTL_DSP_GETTRIGGER:
2074                 return put_user((s->ctrl & CTRL_DAC1_EN) ? PCM_ENABLE_OUTPUT : 0, (int *)arg);
2075                                                 
2076         case SNDCTL_DSP_SETTRIGGER:
2077                 if (get_user(val, (int *)arg))
2078                         return -EFAULT;
2079                 if (val & PCM_ENABLE_OUTPUT) {
2080                         if (!s->dma_dac1.ready && (ret = prog_dmabuf_dac1(s)))
2081                                 return ret;
2082                         s->dma_dac1.enabled = 1;
2083                         start_dac1(s);
2084                 } else {
2085                         s->dma_dac1.enabled = 0;
2086                         stop_dac1(s);
2087                 }
2088                 return 0;
2089
2090         case SNDCTL_DSP_GETOSPACE:
2091                 if (!s->dma_dac1.ready && (val = prog_dmabuf_dac1(s)) != 0)
2092                         return val;
2093                 spin_lock_irqsave(&s->lock, flags);
2094                 es1370_update_ptr(s);
2095                 abinfo.fragsize = s->dma_dac1.fragsize;
2096                 count = s->dma_dac1.count;
2097                 if (count < 0)
2098                         count = 0;
2099                 abinfo.bytes = s->dma_dac1.dmasize - count;
2100                 abinfo.fragstotal = s->dma_dac1.numfrag;
2101                 abinfo.fragments = abinfo.bytes >> s->dma_dac1.fragshift;      
2102                 spin_unlock_irqrestore(&s->lock, flags);
2103                 return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2104
2105         case SNDCTL_DSP_NONBLOCK:
2106                 file->f_flags |= O_NONBLOCK;
2107                 return 0;
2108
2109         case SNDCTL_DSP_GETODELAY:
2110                 if (!s->dma_dac1.ready && (val = prog_dmabuf_dac1(s)) != 0)
2111                         return val;
2112                 spin_lock_irqsave(&s->lock, flags);
2113                 es1370_update_ptr(s);
2114                 count = s->dma_dac1.count;
2115                 spin_unlock_irqrestore(&s->lock, flags);
2116                 if (count < 0)
2117                         count = 0;
2118                 return put_user(count, (int *)arg);
2119
2120         case SNDCTL_DSP_GETOPTR:
2121                 if (!s->dma_dac1.ready && (val = prog_dmabuf_dac1(s)) != 0)
2122                         return val;
2123                 spin_lock_irqsave(&s->lock, flags);
2124                 es1370_update_ptr(s);
2125                 cinfo.bytes = s->dma_dac1.total_bytes;
2126                 count = s->dma_dac1.count;
2127                 if (count < 0)
2128                         count = 0;
2129                 cinfo.blocks = count >> s->dma_dac1.fragshift;
2130                 cinfo.ptr = s->dma_dac1.hwptr;
2131                 if (s->dma_dac1.mapped)
2132                         s->dma_dac1.count &= s->dma_dac1.fragsize-1;
2133                 spin_unlock_irqrestore(&s->lock, flags);
2134                 return copy_to_user((void *)arg, &cinfo, sizeof(cinfo));
2135
2136         case SNDCTL_DSP_GETBLKSIZE:
2137                 if ((val = prog_dmabuf_dac1(s)))
2138                         return val;
2139                 return put_user(s->dma_dac1.fragsize, (int *)arg);
2140
2141         case SNDCTL_DSP_SETFRAGMENT:
2142                 if (get_user(val, (int *)arg))
2143                         return -EFAULT;
2144                 s->dma_dac1.ossfragshift = val & 0xffff;
2145                 s->dma_dac1.ossmaxfrags = (val >> 16) & 0xffff;
2146                 if (s->dma_dac1.ossfragshift < 4)
2147                         s->dma_dac1.ossfragshift = 4;
2148                 if (s->dma_dac1.ossfragshift > 15)
2149                         s->dma_dac1.ossfragshift = 15;
2150                 if (s->dma_dac1.ossmaxfrags < 4)
2151                         s->dma_dac1.ossmaxfrags = 4;
2152                 return 0;
2153
2154         case SNDCTL_DSP_SUBDIVIDE:
2155                 if (s->dma_dac1.subdivision)
2156                         return -EINVAL;
2157                 if (get_user(val, (int *)arg))
2158                         return -EFAULT;
2159                 if (val != 1 && val != 2 && val != 4)
2160                         return -EINVAL;
2161                 s->dma_dac1.subdivision = val;
2162                 return 0;
2163
2164         case SOUND_PCM_READ_RATE:
2165                 return put_user(dac1_samplerate[(s->ctrl & CTRL_WTSRSEL) >> CTRL_SH_WTSRSEL], (int *)arg);
2166
2167         case SOUND_PCM_READ_CHANNELS:
2168                 return put_user((s->sctrl & SCTRL_P1SMB) ? 2 : 1, (int *)arg);
2169
2170         case SOUND_PCM_READ_BITS:
2171                 return put_user((s->sctrl & SCTRL_P1SEB) ? 16 : 8, (int *)arg);
2172
2173         case SOUND_PCM_WRITE_FILTER:
2174         case SNDCTL_DSP_SETSYNCRO:
2175         case SOUND_PCM_READ_FILTER:
2176                 return -EINVAL;
2177                 
2178         }
2179         return mixer_ioctl(s, cmd, arg);
2180 }
2181
2182 static int es1370_open_dac(struct inode *inode, struct file *file)
2183 {
2184         int minor = MINOR(inode->i_rdev);
2185         DECLARE_WAITQUEUE(wait, current);
2186         unsigned long flags;
2187         struct list_head *list;
2188         struct es1370_state *s;
2189
2190         for (list = devs.next; ; list = list->next) {
2191                 if (list == &devs)
2192                         return -ENODEV;
2193                 s = list_entry(list, struct es1370_state, devs);
2194                 if (!((s->dev_dac ^ minor) & ~0xf))
2195                         break;
2196         }
2197         VALIDATE_STATE(s);
2198         /* we allow opening with O_RDWR, most programs do it although they will only write */
2199 #if 0
2200         if (file->f_mode & FMODE_READ)
2201                 return -EPERM;
2202 #endif
2203         if (!(file->f_mode & FMODE_WRITE))
2204                 return -EINVAL;
2205         file->private_data = s;
2206         /* wait for device to become free */
2207         down(&s->open_sem);
2208         while (s->open_mode & FMODE_DAC) {
2209                 if (file->f_flags & O_NONBLOCK) {
2210                         up(&s->open_sem);
2211                         return -EBUSY;
2212                 }
2213                 add_wait_queue(&s->open_wait, &wait);
2214                 __set_current_state(TASK_INTERRUPTIBLE);
2215                 up(&s->open_sem);
2216                 schedule();
2217                 remove_wait_queue(&s->open_wait, &wait);
2218                 set_current_state(TASK_RUNNING);
2219                 if (signal_pending(current))
2220                         return -ERESTARTSYS;
2221                 down(&s->open_sem);
2222         }
2223         s->dma_dac1.ossfragshift = s->dma_dac1.ossmaxfrags = s->dma_dac1.subdivision = 0;
2224         s->dma_dac1.enabled = 1;
2225         spin_lock_irqsave(&s->lock, flags);
2226         s->ctrl = (s->ctrl & ~CTRL_WTSRSEL) | (1 << CTRL_SH_WTSRSEL);
2227         s->sctrl &= ~SCTRL_P1FMT;
2228         if ((minor & 0xf) == SND_DEV_DSP16)
2229                 s->sctrl |= ES1370_FMT_S16_MONO << SCTRL_SH_P1FMT;
2230         else
2231                 s->sctrl |= ES1370_FMT_U8_MONO << SCTRL_SH_P1FMT;
2232         outl(s->sctrl, s->io+ES1370_REG_SERIAL_CONTROL);
2233         outl(s->ctrl, s->io+ES1370_REG_CONTROL);
2234         spin_unlock_irqrestore(&s->lock, flags);
2235         s->open_mode |= FMODE_DAC;
2236         up(&s->open_sem);
2237         return 0;
2238 }
2239
2240 static int es1370_release_dac(struct inode *inode, struct file *file)
2241 {
2242         struct es1370_state *s = (struct es1370_state *)file->private_data;
2243
2244         VALIDATE_STATE(s);
2245         lock_kernel();
2246         drain_dac1(s, file->f_flags & O_NONBLOCK);
2247         down(&s->open_sem);
2248         stop_dac1(s);
2249         dealloc_dmabuf(s, &s->dma_dac1);
2250         s->open_mode &= ~FMODE_DAC;
2251         wake_up(&s->open_wait);
2252         up(&s->open_sem);
2253         unlock_kernel();
2254         return 0;
2255 }
2256
2257 static /*const*/ struct file_operations es1370_dac_fops = {
2258         owner:          THIS_MODULE,
2259         llseek:         no_llseek,
2260         write:          es1370_write_dac,
2261         poll:           es1370_poll_dac,
2262         ioctl:          es1370_ioctl_dac,
2263         mmap:           es1370_mmap_dac,
2264         open:           es1370_open_dac,
2265         release:        es1370_release_dac,
2266 };
2267
2268 /* --------------------------------------------------------------------- */
2269
2270 static ssize_t es1370_midi_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
2271 {
2272         struct es1370_state *s = (struct es1370_state *)file->private_data;
2273         DECLARE_WAITQUEUE(wait, current);
2274         ssize_t ret;
2275         unsigned long flags;
2276         unsigned ptr;
2277         int cnt;
2278
2279         VALIDATE_STATE(s);
2280         if (ppos != &file->f_pos)
2281                 return -ESPIPE;
2282         if (!access_ok(VERIFY_WRITE, buffer, count))
2283                 return -EFAULT;
2284         if (count == 0)
2285                 return 0;
2286         ret = 0;
2287         add_wait_queue(&s->midi.iwait, &wait);
2288         while (count > 0) {
2289                 spin_lock_irqsave(&s->lock, flags);
2290                 ptr = s->midi.ird;
2291                 cnt = MIDIINBUF - ptr;
2292                 if (s->midi.icnt < cnt)
2293                         cnt = s->midi.icnt;
2294                 if (cnt <= 0)
2295                         __set_current_state(TASK_INTERRUPTIBLE);
2296                 spin_unlock_irqrestore(&s->lock, flags);
2297                 if (cnt > count)
2298                         cnt = count;
2299                 if (cnt <= 0) {
2300                         if (file->f_flags & O_NONBLOCK) {
2301                                 if (!ret)
2302                                         ret = -EAGAIN;
2303                                 break;
2304                         }
2305                         schedule();
2306                         if (signal_pending(current)) {
2307                                 if (!ret)
2308                                         ret = -ERESTARTSYS;
2309                                 break;
2310                         }
2311                         continue;
2312                 }
2313                 if (copy_to_user(buffer, s->midi.ibuf + ptr, cnt)) {
2314                         if (!ret)
2315                                 ret = -EFAULT;
2316                         break;
2317                 }
2318                 ptr = (ptr + cnt) % MIDIINBUF;
2319                 spin_lock_irqsave(&s->lock, flags);
2320                 s->midi.ird = ptr;
2321                 s->midi.icnt -= cnt;
2322                 spin_unlock_irqrestore(&s->lock, flags);
2323                 count -= cnt;
2324                 buffer += cnt;
2325                 ret += cnt;
2326                 break;
2327         }
2328         __set_current_state(TASK_RUNNING);
2329         remove_wait_queue(&s->midi.iwait, &wait);
2330         return ret;
2331 }
2332
2333 static ssize_t es1370_midi_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
2334 {
2335         struct es1370_state *s = (struct es1370_state *)file->private_data;
2336         DECLARE_WAITQUEUE(wait, current);
2337         ssize_t ret;
2338         unsigned long flags;
2339         unsigned ptr;
2340         int cnt;
2341
2342         VALIDATE_STATE(s);
2343         if (ppos != &file->f_pos)
2344                 return -ESPIPE;
2345         if (!access_ok(VERIFY_READ, buffer, count))
2346                 return -EFAULT;
2347         if (count == 0)
2348                 return 0;
2349         ret = 0;
2350         add_wait_queue(&s->midi.owait, &wait);
2351         while (count > 0) {
2352                 spin_lock_irqsave(&s->lock, flags);
2353                 ptr = s->midi.owr;
2354                 cnt = MIDIOUTBUF - ptr;
2355                 if (s->midi.ocnt + cnt > MIDIOUTBUF)
2356                         cnt = MIDIOUTBUF - s->midi.ocnt;
2357                 if (cnt <= 0) {
2358                         __set_current_state(TASK_INTERRUPTIBLE);
2359                         es1370_handle_midi(s);
2360                 }
2361                 spin_unlock_irqrestore(&s->lock, flags);
2362                 if (cnt > count)
2363                         cnt = count;
2364                 if (cnt <= 0) {
2365                         if (file->f_flags & O_NONBLOCK) {
2366                                 if (!ret)
2367                                         ret = -EAGAIN;
2368                                 break;
2369                         }
2370                         schedule();
2371                         if (signal_pending(current)) {
2372                                 if (!ret)
2373                                         ret = -ERESTARTSYS;
2374                                 break;
2375                         }
2376                         continue;
2377                 }
2378                 if (copy_from_user(s->midi.obuf + ptr, buffer, cnt)) {
2379                         if (!ret)
2380                                 ret = -EFAULT;
2381                         break;
2382                 }
2383                 ptr = (ptr + cnt) % MIDIOUTBUF;
2384                 spin_lock_irqsave(&s->lock, flags);
2385                 s->midi.owr = ptr;
2386                 s->midi.ocnt += cnt;
2387                 spin_unlock_irqrestore(&s->lock, flags);
2388                 count -= cnt;
2389                 buffer += cnt;
2390                 ret += cnt;
2391                 spin_lock_irqsave(&s->lock, flags);
2392                 es1370_handle_midi(s);
2393                 spin_unlock_irqrestore(&s->lock, flags);
2394         }
2395         __set_current_state(TASK_RUNNING);
2396         remove_wait_queue(&s->midi.owait, &wait);
2397         return ret;
2398 }
2399
2400 /* No kernel lock - we have our own spinlock */
2401 static unsigned int es1370_midi_poll(struct file *file, struct poll_table_struct *wait)
2402 {
2403         struct es1370_state *s = (struct es1370_state *)file->private_data;
2404         unsigned long flags;
2405         unsigned int mask = 0;
2406
2407         VALIDATE_STATE(s);
2408         if (file->f_mode & FMODE_WRITE)
2409                 poll_wait(file, &s->midi.owait, wait);
2410         if (file->f_mode & FMODE_READ)
2411                 poll_wait(file, &s->midi.iwait, wait);
2412         spin_lock_irqsave(&s->lock, flags);
2413         if (file->f_mode & FMODE_READ) {
2414                 if (s->midi.icnt > 0)
2415                         mask |= POLLIN | POLLRDNORM;
2416         }
2417         if (file->f_mode & FMODE_WRITE) {
2418                 if (s->midi.ocnt < MIDIOUTBUF)
2419                         mask |= POLLOUT | POLLWRNORM;
2420         }
2421         spin_unlock_irqrestore(&s->lock, flags);
2422         return mask;
2423 }
2424
2425 static int es1370_midi_open(struct inode *inode, struct file *file)
2426 {
2427         int minor = MINOR(inode->i_rdev);
2428         DECLARE_WAITQUEUE(wait, current);
2429         unsigned long flags;
2430         struct list_head *list;
2431         struct es1370_state *s;
2432
2433         for (list = devs.next; ; list = list->next) {
2434                 if (list == &devs)
2435                         return -ENODEV;
2436                 s = list_entry(list, struct es1370_state, devs);
2437                 if (s->dev_midi == minor)
2438                         break;
2439         }
2440         VALIDATE_STATE(s);
2441         file->private_data = s;
2442         /* wait for device to become free */
2443         down(&s->open_sem);
2444         while (s->open_mode & (file->f_mode << FMODE_MIDI_SHIFT)) {
2445                 if (file->f_flags & O_NONBLOCK) {
2446                         up(&s->open_sem);
2447                         return -EBUSY;
2448                 }
2449                 add_wait_queue(&s->open_wait, &wait);
2450                 __set_current_state(TASK_INTERRUPTIBLE);
2451                 up(&s->open_sem);
2452                 schedule();
2453                 remove_wait_queue(&s->open_wait, &wait);
2454                 set_current_state(TASK_RUNNING);
2455                 if (signal_pending(current))
2456                         return -ERESTARTSYS;
2457                 down(&s->open_sem);
2458         }
2459         spin_lock_irqsave(&s->lock, flags);
2460         if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
2461                 s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
2462                 s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
2463                 outb(UCTRL_CNTRL_SWR, s->io+ES1370_REG_UART_CONTROL);
2464                 outb(0, s->io+ES1370_REG_UART_CONTROL);
2465                 outb(0, s->io+ES1370_REG_UART_TEST);
2466         }
2467         if (file->f_mode & FMODE_READ) {
2468                 s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
2469         }
2470         if (file->f_mode & FMODE_WRITE) {
2471                 s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
2472         }
2473         s->ctrl |= CTRL_UART_EN;
2474         outl(s->ctrl, s->io+ES1370_REG_CONTROL);
2475         es1370_handle_midi(s);
2476         spin_unlock_irqrestore(&s->lock, flags);
2477         s->open_mode |= (file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ | FMODE_MIDI_WRITE);
2478         up(&s->open_sem);
2479         return 0;
2480 }
2481
2482 static int es1370_midi_release(struct inode *inode, struct file *file)
2483 {
2484         struct es1370_state *s = (struct es1370_state *)file->private_data;
2485         DECLARE_WAITQUEUE(wait, current);
2486         unsigned long flags;
2487         unsigned count, tmo;
2488
2489         VALIDATE_STATE(s);
2490
2491         lock_kernel();
2492         if (file->f_mode & FMODE_WRITE) {
2493                 add_wait_queue(&s->midi.owait, &wait);
2494                 for (;;) {
2495                         __set_current_state(TASK_INTERRUPTIBLE);
2496                         spin_lock_irqsave(&s->lock, flags);
2497                         count = s->midi.ocnt;
2498                         spin_unlock_irqrestore(&s->lock, flags);
2499                         if (count <= 0)
2500                                 break;
2501                         if (signal_pending(current))
2502                                 break;
2503                         if (file->f_flags & O_NONBLOCK) {
2504                                 remove_wait_queue(&s->midi.owait, &wait);
2505                                 set_current_state(TASK_RUNNING);
2506                                 unlock_kernel();
2507                                 return -EBUSY;
2508                         }
2509                         tmo = (count * HZ) / 3100;
2510                         if (!schedule_timeout(tmo ? : 1) && tmo)
2511                                 DBG(printk(KERN_DEBUG "es1370: midi timed out??\n");)
2512                 }
2513                 remove_wait_queue(&s->midi.owait, &wait);
2514                 set_current_state(TASK_RUNNING);
2515         }
2516         down(&s->open_sem);
2517         s->open_mode &= (~(file->f_mode << FMODE_MIDI_SHIFT)) & (FMODE_MIDI_READ|FMODE_MIDI_WRITE);
2518         spin_lock_irqsave(&s->lock, flags);
2519         if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
2520                 s->ctrl &= ~CTRL_UART_EN;
2521                 outl(s->ctrl, s->io+ES1370_REG_CONTROL);
2522         }
2523         spin_unlock_irqrestore(&s->lock, flags);
2524         wake_up(&s->open_wait);
2525         up(&s->open_sem);
2526         unlock_kernel();
2527         return 0;
2528 }
2529
2530 static /*const*/ struct file_operations es1370_midi_fops = {
2531         owner:          THIS_MODULE,
2532         llseek:         no_llseek,
2533         read:           es1370_midi_read,
2534         write:          es1370_midi_write,
2535         poll:           es1370_midi_poll,
2536         open:           es1370_midi_open,
2537         release:        es1370_midi_release,
2538 };
2539
2540 /* --------------------------------------------------------------------- */
2541
2542 /* maximum number of devices; only used for command line params */
2543 #define NR_DEVICE 5
2544
2545 static int joystick[NR_DEVICE] = { 0, };
2546 static int lineout[NR_DEVICE] = { 0, };
2547 static int micbias[NR_DEVICE] = { 0, };
2548
2549 static unsigned int devindex = 0;
2550
2551 MODULE_PARM(joystick, "1-" __MODULE_STRING(NR_DEVICE) "i");
2552 MODULE_PARM_DESC(joystick, "if 1 enables joystick interface (still need separate driver)");
2553 MODULE_PARM(lineout, "1-" __MODULE_STRING(NR_DEVICE) "i");
2554 MODULE_PARM_DESC(lineout, "if 1 the LINE input is converted to LINE out");
2555 MODULE_PARM(micbias, "1-" __MODULE_STRING(NR_DEVICE) "i");
2556 MODULE_PARM_DESC(micbias, "sets the +5V bias for an electret microphone");
2557
2558 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
2559 MODULE_DESCRIPTION("ES1370 AudioPCI Driver");
2560
2561 /* --------------------------------------------------------------------- */
2562
2563 static struct initvol {
2564         int mixch;
2565         int vol;
2566 } initvol[] __initdata = {
2567         { SOUND_MIXER_WRITE_VOLUME, 0x4040 },
2568         { SOUND_MIXER_WRITE_PCM, 0x4040 },
2569         { SOUND_MIXER_WRITE_SYNTH, 0x4040 },
2570         { SOUND_MIXER_WRITE_CD, 0x4040 },
2571         { SOUND_MIXER_WRITE_LINE, 0x4040 },
2572         { SOUND_MIXER_WRITE_LINE1, 0x4040 },
2573         { SOUND_MIXER_WRITE_LINE2, 0x4040 },
2574         { SOUND_MIXER_WRITE_LINE3, 0x4040 },
2575         { SOUND_MIXER_WRITE_MIC, 0x4040 },
2576         { SOUND_MIXER_WRITE_OGAIN, 0x4040 }
2577 };
2578
2579 static int __devinit es1370_probe(struct pci_dev *pcidev, const struct pci_device_id *pciid)
2580 {
2581         struct es1370_state *s;
2582         mm_segment_t fs;
2583         int i, val, ret;
2584
2585         if ((ret=pci_enable_device(pcidev)))
2586                 return ret;
2587
2588         if ( !(pci_resource_flags(pcidev, 0) & IORESOURCE_IO) ||
2589              !pci_resource_start(pcidev, 0)
2590         )
2591                 return -ENODEV;
2592         if (pcidev->irq == 0) 
2593                 return -ENODEV;
2594         i = pci_set_dma_mask(pcidev, 0xffffffff);
2595         if (i) {
2596                 printk(KERN_WARNING "es1370: architecture does not support 32bit PCI busmaster DMA\n");
2597                 return i;
2598         }
2599         if (!(s = kmalloc(sizeof(struct es1370_state), GFP_KERNEL))) {
2600           &n