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