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