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