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