1 /*****************************************************************************/
4 * sonicvibes.c -- S3 Sonic Vibes 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:
30 * /dev/dsp standard /dev/dsp device, (mostly) OSS compatible
31 * /dev/mixer standard /dev/mixer device, (mostly) OSS compatible
32 * /dev/midi simple MIDI UART interface, no ioctl
34 * The card has both an FM and a Wavetable synth, but I have to figure
35 * out first how to drive them...
38 * 06.05.1998 0.1 Initial release
39 * 10.05.1998 0.2 Fixed many bugs, esp. ADC rate calculation
40 * First stab at a simple midi interface (no bells&whistles)
41 * 13.05.1998 0.3 Fix stupid cut&paste error: set_adc_rate was called instead of
42 * set_dac_rate in the FMODE_WRITE case in sv_open
43 * Fix hwptr out of bounds (now mpg123 works)
44 * 14.05.1998 0.4 Don't allow excessive interrupt rates
45 * 08.06.1998 0.5 First release using Alan Cox' soundcore instead of miscdevice
46 * 03.08.1998 0.6 Do not include modversions.h
47 * Now mixer behaviour can basically be selected between
48 * "OSS documented" and "OSS actual" behaviour
49 * 31.08.1998 0.7 Fix realplayer problems - dac.count issues
50 * 10.12.1998 0.8 Fix drain_dac trying to wait on not yet initialized DMA
51 * 16.12.1998 0.9 Fix a few f_file & FMODE_ bugs
52 * 06.01.1999 0.10 remove the silly SA_INTERRUPT flag.
53 * hopefully killed the egcs section type conflict
54 * 12.03.1999 0.11 cinfo.blocks should be reset after GETxPTR ioctl.
55 * reported by Johan Maes <joma@telindus.be>
56 * 22.03.1999 0.12 return EAGAIN instead of EBUSY when O_NONBLOCK
57 * read/write cannot be executed
58 * 05.04.1999 0.13 added code to sv_read and sv_write which should detect
59 * lockups of the sound chip and revive it. This is basically
60 * an ugly hack, but at least applications using this driver
61 * won't hang forever. I don't know why these lockups happen,
62 * it might well be the motherboard chipset (an early 486 PCI
63 * board with ALI chipset), since every busmastering 100MB
64 * ethernet card I've tried (Realtek 8139 and Macronix tulip clone)
65 * exhibit similar behaviour (they work for a couple of packets
66 * and then lock up and can be revived by ifconfig down/up).
67 * 07.04.1999 0.14 implemented the following ioctl's: SOUND_PCM_READ_RATE,
68 * SOUND_PCM_READ_CHANNELS, SOUND_PCM_READ_BITS;
69 * Alpha fixes reported by Peter Jones <pjones@redhat.com>
70 * Note: dmaio hack might still be wrong on archs other than i386
71 * 15.06.1999 0.15 Fix bad allocation bug.
72 * Thanks to Deti Fliegl <fliegl@in.tum.de>
73 * 28.06.1999 0.16 Add pci_set_master
74 * 03.08.1999 0.17 adapt to Linus' new __setup/__initcall
75 * added kernel command line options "sonicvibes=reverb" and "sonicvibesdmaio=dmaioaddr"
76 * 12.08.1999 0.18 module_init/__setup fixes
77 * 24.08.1999 0.19 get rid of the dmaio kludge, replace with allocate_resource
78 * 31.08.1999 0.20 add spin_lock_init
79 * use new resource allocation to allocate DDMA IO space
80 * replaced current->state = x with set_current_state(x)
81 * 03.09.1999 0.21 change read semantics for MIDI to match
82 * OSS more closely; remove possible wakeup race
83 * 28.10.1999 0.22 More waitqueue races fixed
84 * 01.12.1999 0.23 New argument to allocate_resource
85 * 07.12.1999 0.24 More allocate_resource semantics change
86 * 08.01.2000 0.25 Prevent some ioctl's from returning bad count values on underrun/overrun;
87 * Tim Janik's BSE (Bedevilled Sound Engine) found this
88 * use Martin Mares' pci_assign_resource
89 * 07.02.2000 0.26 Use pci_alloc_consistent and pci_register_driver
90 * 21.11.2000 0.27 Initialize dma buffers in poll, otherwise poll may return a bogus mask
91 * 12.12.2000 0.28 More dma buffer initializations, patch from
92 * Tjeerd Mulder <tjeerd.mulder@fujitsu-siemens.com>
93 * 31.01.2001 0.29 Register/Unregister gameport
94 * Fix SETTRIGGER non OSS API conformity
95 * 18.05.2001 0.30 PCI probing and error values cleaned up by Marcus
96 * Meissner <mm@caldera.de>
97 * 03.01.2003 0.31 open_mode fixes from Georg Acher <acher@in.tum.de>
101 /*****************************************************************************/
103 #include <linux/module.h>
104 #include <linux/string.h>
105 #include <linux/ioport.h>
106 #include <linux/interrupt.h>
107 #include <linux/wait.h>
108 #include <linux/mm.h>
109 #include <linux/delay.h>
110 #include <linux/sound.h>
111 #include <linux/slab.h>
112 #include <linux/soundcard.h>
113 #include <linux/pci.h>
114 #include <linux/init.h>
115 #include <linux/poll.h>
116 #include <linux/spinlock.h>
117 #include <linux/smp_lock.h>
118 #include <linux/gameport.h>
121 #include <asm/uaccess.h>
125 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
126 #define SUPPORT_JOYSTICK 1
129 /* --------------------------------------------------------------------- */
131 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
133 /* --------------------------------------------------------------------- */
135 #ifndef PCI_VENDOR_ID_S3
136 #define PCI_VENDOR_ID_S3 0x5333
138 #ifndef PCI_DEVICE_ID_S3_SONICVIBES
139 #define PCI_DEVICE_ID_S3_SONICVIBES 0xca00
142 #define SV_MAGIC ((PCI_VENDOR_ID_S3<<16)|PCI_DEVICE_ID_S3_SONICVIBES)
144 #define SV_EXTENT_SB 0x10
145 #define SV_EXTENT_ENH 0x10
146 #define SV_EXTENT_SYNTH 0x4
147 #define SV_EXTENT_MIDI 0x4
148 #define SV_EXTENT_GAME 0x8
149 #define SV_EXTENT_DMA 0x10
152 * we are not a bridge and thus use a resource for DDMA that is used for bridges but
153 * left empty for normal devices
155 #define RESOURCE_SB 0
156 #define RESOURCE_ENH 1
157 #define RESOURCE_SYNTH 2
158 #define RESOURCE_MIDI 3
159 #define RESOURCE_GAME 4
160 #define RESOURCE_DDMA 7
162 #define SV_MIDI_DATA 0
163 #define SV_MIDI_COMMAND 1
164 #define SV_MIDI_STATUS 1
166 #define SV_DMA_ADDR0 0
167 #define SV_DMA_ADDR1 1
168 #define SV_DMA_ADDR2 2
169 #define SV_DMA_ADDR3 3
170 #define SV_DMA_COUNT0 4
171 #define SV_DMA_COUNT1 5
172 #define SV_DMA_COUNT2 6
173 #define SV_DMA_MODE 0xb
174 #define SV_DMA_RESET 0xd
175 #define SV_DMA_MASK 0xf
178 * DONT reset the DMA controllers unless you understand
179 * the reset semantics. Assuming reset semantics as in
180 * the 8237 does not work.
183 #define DMA_MODE_AUTOINIT 0x10
184 #define DMA_MODE_READ 0x44 /* I/O to memory, no autoinit, increment, single mode */
185 #define DMA_MODE_WRITE 0x48 /* memory to I/O, no autoinit, increment, single mode */
187 #define SV_CODEC_CONTROL 0
188 #define SV_CODEC_INTMASK 1
189 #define SV_CODEC_STATUS 2
190 #define SV_CODEC_IADDR 4
191 #define SV_CODEC_IDATA 5
193 #define SV_CCTRL_RESET 0x80
194 #define SV_CCTRL_INTADRIVE 0x20
195 #define SV_CCTRL_WAVETABLE 0x08
196 #define SV_CCTRL_REVERB 0x04
197 #define SV_CCTRL_ENHANCED 0x01
199 #define SV_CINTMASK_DMAA 0x01
200 #define SV_CINTMASK_DMAC 0x04
201 #define SV_CINTMASK_SPECIAL 0x08
202 #define SV_CINTMASK_UPDOWN 0x40
203 #define SV_CINTMASK_MIDI 0x80
205 #define SV_CSTAT_DMAA 0x01
206 #define SV_CSTAT_DMAC 0x04
207 #define SV_CSTAT_SPECIAL 0x08
208 #define SV_CSTAT_UPDOWN 0x40
209 #define SV_CSTAT_MIDI 0x80
211 #define SV_CIADDR_TRD 0x80
212 #define SV_CIADDR_MCE 0x40
214 /* codec indirect registers */
215 #define SV_CIMIX_ADCINL 0x00
216 #define SV_CIMIX_ADCINR 0x01
217 #define SV_CIMIX_AUX1INL 0x02
218 #define SV_CIMIX_AUX1INR 0x03
219 #define SV_CIMIX_CDINL 0x04
220 #define SV_CIMIX_CDINR 0x05
221 #define SV_CIMIX_LINEINL 0x06
222 #define SV_CIMIX_LINEINR 0x07
223 #define SV_CIMIX_MICIN 0x08
224 #define SV_CIMIX_SYNTHINL 0x0A
225 #define SV_CIMIX_SYNTHINR 0x0B
226 #define SV_CIMIX_AUX2INL 0x0C
227 #define SV_CIMIX_AUX2INR 0x0D
228 #define SV_CIMIX_ANALOGINL 0x0E
229 #define SV_CIMIX_ANALOGINR 0x0F
230 #define SV_CIMIX_PCMINL 0x10
231 #define SV_CIMIX_PCMINR 0x11
233 #define SV_CIGAMECONTROL 0x09
234 #define SV_CIDATAFMT 0x12
235 #define SV_CIENABLE 0x13
236 #define SV_CIUPDOWN 0x14
237 #define SV_CIREVISION 0x15
238 #define SV_CIADCOUTPUT 0x16
239 #define SV_CIDMAABASECOUNT1 0x18
240 #define SV_CIDMAABASECOUNT0 0x19
241 #define SV_CIDMACBASECOUNT1 0x1c
242 #define SV_CIDMACBASECOUNT0 0x1d
243 #define SV_CIPCMSR0 0x1e
244 #define SV_CIPCMSR1 0x1f
245 #define SV_CISYNTHSR0 0x20
246 #define SV_CISYNTHSR1 0x21
247 #define SV_CIADCCLKSOURCE 0x22
248 #define SV_CIADCALTSR 0x23
249 #define SV_CIADCPLLM 0x24
250 #define SV_CIADCPLLN 0x25
251 #define SV_CISYNTHPLLM 0x26
252 #define SV_CISYNTHPLLN 0x27
253 #define SV_CIUARTCONTROL 0x2a
254 #define SV_CIDRIVECONTROL 0x2b
255 #define SV_CISRSSPACE 0x2c
256 #define SV_CISRSCENTER 0x2d
257 #define SV_CIWAVETABLESRC 0x2e
258 #define SV_CIANALOGPWRDOWN 0x30
259 #define SV_CIDIGITALPWRDOWN 0x31
262 #define SV_CIMIX_ADCSRC_CD 0x20
263 #define SV_CIMIX_ADCSRC_DAC 0x40
264 #define SV_CIMIX_ADCSRC_AUX2 0x60
265 #define SV_CIMIX_ADCSRC_LINE 0x80
266 #define SV_CIMIX_ADCSRC_AUX1 0xa0
267 #define SV_CIMIX_ADCSRC_MIC 0xc0
268 #define SV_CIMIX_ADCSRC_MIXOUT 0xe0
269 #define SV_CIMIX_ADCSRC_MASK 0xe0
271 #define SV_CFMT_STEREO 0x01
272 #define SV_CFMT_16BIT 0x02
273 #define SV_CFMT_MASK 0x03
274 #define SV_CFMT_ASHIFT 0
275 #define SV_CFMT_CSHIFT 4
277 static const unsigned sample_size[] = { 1, 2, 2, 4 };
278 static const unsigned sample_shift[] = { 0, 1, 1, 2 };
280 #define SV_CENABLE_PPE 0x4
281 #define SV_CENABLE_RE 0x2
282 #define SV_CENABLE_PE 0x1
285 /* MIDI buffer sizes */
287 #define MIDIINBUF 256
288 #define MIDIOUTBUF 256
290 #define FMODE_MIDI_SHIFT 2
291 #define FMODE_MIDI_READ (FMODE_READ << FMODE_MIDI_SHIFT)
292 #define FMODE_MIDI_WRITE (FMODE_WRITE << FMODE_MIDI_SHIFT)
294 #define FMODE_DMFM 0x10
296 /* --------------------------------------------------------------------- */
302 /* list of sonicvibes devices */
303 struct list_head devs;
305 /* the corresponding pci_dev structure */
308 /* soundcore stuff */
314 /* hardware resources */
315 unsigned long iosb, ioenh, iosynth, iomidi; /* long for SPARC */
316 unsigned int iodmaa, iodmac, irq;
321 #ifndef OSS_DOCUMENTED_MIXER_SEMANTICS
322 unsigned short vol[13];
323 #endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
327 unsigned int rateadc, ratedac;
328 unsigned char fmt, enable;
331 struct semaphore open_sem;
333 wait_queue_head_t open_wait;
341 unsigned hwptr, swptr;
342 unsigned total_bytes;
344 unsigned error; /* over/underrun */
345 wait_queue_head_t wait;
346 /* redundant, but makes calculations easier */
349 unsigned fragsamples;
353 unsigned endcleared:1;
355 unsigned ossfragshift;
357 unsigned subdivision;
362 unsigned ird, iwr, icnt;
363 unsigned ord, owr, ocnt;
364 wait_queue_head_t iwait;
365 wait_queue_head_t owait;
366 struct timer_list timer;
367 unsigned char ibuf[MIDIINBUF];
368 unsigned char obuf[MIDIOUTBUF];
372 struct gameport *gameport;
376 /* --------------------------------------------------------------------- */
378 static LIST_HEAD(devs);
379 static unsigned long wavetable_mem;
381 /* --------------------------------------------------------------------- */
383 static inline unsigned ld2(unsigned int x)
409 * hweightN: returns the hamming weight (i.e. the number
410 * of bits set) of a N-bit word
417 static inline unsigned int hweight32(unsigned int w)
419 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
420 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
421 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
422 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
423 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
426 /* --------------------------------------------------------------------- */
429 * Why use byte IO? Nobody knows, but S3 does it also in their Windows driver.
434 static void set_dmaa(struct sv_state *s, unsigned int addr, unsigned int count)
437 unsigned io = s->iodmaa, u;
440 for (u = 4; u > 0; u--, addr >>= 8, io++)
441 outb(addr & 0xff, io);
442 for (u = 3; u > 0; u--, count >>= 8, io++)
443 outb(count & 0xff, io);
444 #else /* DMABYTEIO */
446 outl(addr, s->iodmaa + SV_DMA_ADDR0);
447 outl(count, s->iodmaa + SV_DMA_COUNT0);
448 #endif /* DMABYTEIO */
449 outb(0x18, s->iodmaa + SV_DMA_MODE);
452 static void set_dmac(struct sv_state *s, unsigned int addr, unsigned int count)
455 unsigned io = s->iodmac, u;
459 for (u = 4; u > 0; u--, addr >>= 8, io++)
460 outb(addr & 0xff, io);
461 for (u = 3; u > 0; u--, count >>= 8, io++)
462 outb(count & 0xff, io);
463 #else /* DMABYTEIO */
466 outl(addr, s->iodmac + SV_DMA_ADDR0);
467 outl(count, s->iodmac + SV_DMA_COUNT0);
468 #endif /* DMABYTEIO */
469 outb(0x14, s->iodmac + SV_DMA_MODE);
472 static inline unsigned get_dmaa(struct sv_state *s)
475 unsigned io = s->iodmaa+6, v = 0, u;
477 for (u = 3; u > 0; u--, io--) {
482 #else /* DMABYTEIO */
483 return (inl(s->iodmaa + SV_DMA_COUNT0) & 0xffffff) + 1;
484 #endif /* DMABYTEIO */
487 static inline unsigned get_dmac(struct sv_state *s)
490 unsigned io = s->iodmac+6, v = 0, u;
492 for (u = 3; u > 0; u--, io--) {
497 #else /* DMABYTEIO */
498 return ((inl(s->iodmac + SV_DMA_COUNT0) & 0xffffff) + 1) << 1;
499 #endif /* DMABYTEIO */
502 static void wrindir(struct sv_state *s, unsigned char idx, unsigned char data)
504 outb(idx & 0x3f, s->ioenh + SV_CODEC_IADDR);
506 outb(data, s->ioenh + SV_CODEC_IDATA);
510 static unsigned char rdindir(struct sv_state *s, unsigned char idx)
514 outb(idx & 0x3f, s->ioenh + SV_CODEC_IADDR);
516 v = inb(s->ioenh + SV_CODEC_IDATA);
521 static void set_fmt(struct sv_state *s, unsigned char mask, unsigned char data)
525 spin_lock_irqsave(&s->lock, flags);
526 outb(SV_CIDATAFMT | SV_CIADDR_MCE, s->ioenh + SV_CODEC_IADDR);
528 s->fmt = inb(s->ioenh + SV_CODEC_IDATA);
531 s->fmt = (s->fmt & mask) | data;
532 outb(s->fmt, s->ioenh + SV_CODEC_IDATA);
534 outb(0, s->ioenh + SV_CODEC_IADDR);
535 spin_unlock_irqrestore(&s->lock, flags);
539 static void frobindir(struct sv_state *s, unsigned char idx, unsigned char mask, unsigned char data)
541 outb(idx & 0x3f, s->ioenh + SV_CODEC_IADDR);
543 outb((inb(s->ioenh + SV_CODEC_IDATA) & mask) ^ data, s->ioenh + SV_CODEC_IDATA);
547 #define REFFREQUENCY 24576000
549 #define FULLRATE 48000
551 static unsigned setpll(struct sv_state *s, unsigned char reg, unsigned rate)
554 unsigned char r, m=0, n=0;
555 unsigned xm, xn, xr, xd, metric = ~0U;
556 /* the warnings about m and n used uninitialized are bogus and may safely be ignored */
558 if (rate < 625000/ADCMULT)
559 rate = 625000/ADCMULT;
560 if (rate > 150000000/ADCMULT)
561 rate = 150000000/ADCMULT;
562 /* slight violation of specs, needed for continuous sampling rates */
563 for (r = 0; rate < 75000000/ADCMULT; r += 0x20, rate <<= 1);
564 for (xn = 3; xn < 35; xn++)
565 for (xm = 3; xm < 130; xm++) {
566 xr = REFFREQUENCY/ADCMULT * xm / xn;
567 xd = abs((signed)(xr - rate));
575 spin_lock_irqsave(&s->lock, flags);
576 outb(reg, s->ioenh + SV_CODEC_IADDR);
578 outb(m, s->ioenh + SV_CODEC_IDATA);
580 outb(reg+1, s->ioenh + SV_CODEC_IADDR);
582 outb(r | n, s->ioenh + SV_CODEC_IDATA);
583 spin_unlock_irqrestore(&s->lock, flags);
585 return (REFFREQUENCY/ADCMULT * (m + 2) / (n + 2)) >> ((r >> 5) & 7);
590 static unsigned getpll(struct sv_state *s, unsigned char reg)
596 spin_lock_irqsave(&s->lock, flags);
597 outb(reg, s->ioenh + SV_CODEC_IADDR);
599 m = inb(s->ioenh + SV_CODEC_IDATA);
601 outb(reg+1, s->ioenh + SV_CODEC_IADDR);
603 n = inb(s->ioenh + SV_CODEC_IDATA);
604 spin_unlock_irqrestore(&s->lock, flags);
606 return (REFFREQUENCY/ADCMULT * (m + 2) / ((n & 0x1f) + 2)) >> ((n >> 5) & 7);
611 static void set_dac_rate(struct sv_state *s, unsigned rate)
620 div = (rate * 65536 + FULLRATE/2) / FULLRATE;
623 spin_lock_irqsave(&s->lock, flags);
624 wrindir(s, SV_CIPCMSR1, div >> 8);
625 wrindir(s, SV_CIPCMSR0, div);
626 spin_unlock_irqrestore(&s->lock, flags);
627 s->ratedac = (div * FULLRATE + 32768) / 65536;
630 static void set_adc_rate(struct sv_state *s, unsigned rate)
633 unsigned rate1, rate2, div;
639 rate1 = setpll(s, SV_CIADCPLLM, rate);
640 div = (48000 + rate/2) / rate;
643 rate2 = (48000 + div/2) / div;
644 spin_lock_irqsave(&s->lock, flags);
645 wrindir(s, SV_CIADCALTSR, (div-1) << 4);
646 if (abs((signed)(rate-rate2)) <= abs((signed)(rate-rate1))) {
647 wrindir(s, SV_CIADCCLKSOURCE, 0x10);
650 wrindir(s, SV_CIADCCLKSOURCE, 0x00);
653 spin_unlock_irqrestore(&s->lock, flags);
656 /* --------------------------------------------------------------------- */
658 static inline void stop_adc(struct sv_state *s)
662 spin_lock_irqsave(&s->lock, flags);
663 s->enable &= ~SV_CENABLE_RE;
664 wrindir(s, SV_CIENABLE, s->enable);
665 spin_unlock_irqrestore(&s->lock, flags);
668 static inline void stop_dac(struct sv_state *s)
672 spin_lock_irqsave(&s->lock, flags);
673 s->enable &= ~(SV_CENABLE_PPE | SV_CENABLE_PE);
674 wrindir(s, SV_CIENABLE, s->enable);
675 spin_unlock_irqrestore(&s->lock, flags);
678 static void start_dac(struct sv_state *s)
682 spin_lock_irqsave(&s->lock, flags);
683 if ((s->dma_dac.mapped || s->dma_dac.count > 0) && s->dma_dac.ready) {
684 s->enable = (s->enable & ~SV_CENABLE_PPE) | SV_CENABLE_PE;
685 wrindir(s, SV_CIENABLE, s->enable);
687 spin_unlock_irqrestore(&s->lock, flags);
690 static void start_adc(struct sv_state *s)
694 spin_lock_irqsave(&s->lock, flags);
695 if ((s->dma_adc.mapped || s->dma_adc.count < (signed)(s->dma_adc.dmasize - 2*s->dma_adc.fragsize))
696 && s->dma_adc.ready) {
697 s->enable |= SV_CENABLE_RE;
698 wrindir(s, SV_CIENABLE, s->enable);
700 spin_unlock_irqrestore(&s->lock, flags);
703 /* --------------------------------------------------------------------- */
705 #define DMABUF_DEFAULTORDER (17-PAGE_SHIFT)
706 #define DMABUF_MINORDER 1
708 static void dealloc_dmabuf(struct sv_state *s, struct dmabuf *db)
710 struct page *page, *pend;
713 /* undo marking the pages as reserved */
714 pend = virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
715 for (page = virt_to_page(db->rawbuf); page <= pend; page++)
716 ClearPageReserved(page);
717 pci_free_consistent(s->dev, PAGE_SIZE << db->buforder, db->rawbuf, db->dmaaddr);
720 db->mapped = db->ready = 0;
724 /* DMAA is used for playback, DMAC is used for recording */
726 static int prog_dmabuf(struct sv_state *s, unsigned rec)
728 struct dmabuf *db = rec ? &s->dma_adc : &s->dma_dac;
729 unsigned rate = rec ? s->rateadc : s->ratedac;
733 struct page *page, *pend;
737 spin_lock_irqsave(&s->lock, flags);
740 s->enable &= ~SV_CENABLE_RE;
741 fmt >>= SV_CFMT_CSHIFT;
743 s->enable &= ~SV_CENABLE_PE;
744 fmt >>= SV_CFMT_ASHIFT;
746 wrindir(s, SV_CIENABLE, s->enable);
747 spin_unlock_irqrestore(&s->lock, flags);
749 db->hwptr = db->swptr = db->total_bytes = db->count = db->error = db->endcleared = 0;
751 db->ready = db->mapped = 0;
752 for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--)
753 if ((db->rawbuf = pci_alloc_consistent(s->dev, PAGE_SIZE << order, &db->dmaaddr)))
757 db->buforder = order;
758 if ((virt_to_bus(db->rawbuf) ^ (virt_to_bus(db->rawbuf) + (PAGE_SIZE << db->buforder) - 1)) & ~0xffff)
759 printk(KERN_DEBUG "sv: DMA buffer crosses 64k boundary: busaddr 0x%lx size %ld\n",
760 virt_to_bus(db->rawbuf), PAGE_SIZE << db->buforder);
761 if ((virt_to_bus(db->rawbuf) + (PAGE_SIZE << db->buforder) - 1) & ~0xffffff)
762 printk(KERN_DEBUG "sv: DMA buffer beyond 16MB: busaddr 0x%lx size %ld\n",
763 virt_to_bus(db->rawbuf), PAGE_SIZE << db->buforder);
764 /* now mark the pages as reserved; otherwise remap_pfn_range doesn't do what we want */
765 pend = virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
766 for (page = virt_to_page(db->rawbuf); page <= pend; page++)
767 SetPageReserved(page);
769 bytepersec = rate << sample_shift[fmt];
770 bufs = PAGE_SIZE << db->buforder;
771 if (db->ossfragshift) {
772 if ((1000 << db->ossfragshift) < bytepersec)
773 db->fragshift = ld2(bytepersec/1000);
775 db->fragshift = db->ossfragshift;
777 db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1));
778 if (db->fragshift < 3)
781 db->numfrag = bufs >> db->fragshift;
782 while (db->numfrag < 4 && db->fragshift > 3) {
784 db->numfrag = bufs >> db->fragshift;
786 db->fragsize = 1 << db->fragshift;
787 if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
788 db->numfrag = db->ossmaxfrags;
789 db->fragsamples = db->fragsize >> sample_shift[fmt];
790 db->dmasize = db->numfrag << db->fragshift;
791 memset(db->rawbuf, (fmt & SV_CFMT_16BIT) ? 0 : 0x80, db->dmasize);
792 spin_lock_irqsave(&s->lock, flags);
794 set_dmac(s, db->dmaaddr, db->numfrag << db->fragshift);
795 /* program enhanced mode registers */
796 wrindir(s, SV_CIDMACBASECOUNT1, (db->fragsamples-1) >> 8);
797 wrindir(s, SV_CIDMACBASECOUNT0, db->fragsamples-1);
799 set_dmaa(s, db->dmaaddr, db->numfrag << db->fragshift);
800 /* program enhanced mode registers */
801 wrindir(s, SV_CIDMAABASECOUNT1, (db->fragsamples-1) >> 8);
802 wrindir(s, SV_CIDMAABASECOUNT0, db->fragsamples-1);
804 spin_unlock_irqrestore(&s->lock, flags);
810 static inline void clear_advance(struct sv_state *s)
812 unsigned char c = (s->fmt & (SV_CFMT_16BIT << SV_CFMT_ASHIFT)) ? 0 : 0x80;
813 unsigned char *buf = s->dma_dac.rawbuf;
814 unsigned bsize = s->dma_dac.dmasize;
815 unsigned bptr = s->dma_dac.swptr;
816 unsigned len = s->dma_dac.fragsize;
818 if (bptr + len > bsize) {
819 unsigned x = bsize - bptr;
820 memset(buf + bptr, c, x);
824 memset(buf + bptr, c, len);
827 /* call with spinlock held! */
828 static void sv_update_ptr(struct sv_state *s)
833 /* update ADC pointer */
834 if (s->dma_adc.ready) {
835 hwptr = (s->dma_adc.dmasize - get_dmac(s)) % s->dma_adc.dmasize;
836 diff = (s->dma_adc.dmasize + hwptr - s->dma_adc.hwptr) % s->dma_adc.dmasize;
837 s->dma_adc.hwptr = hwptr;
838 s->dma_adc.total_bytes += diff;
839 s->dma_adc.count += diff;
840 if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
841 wake_up(&s->dma_adc.wait);
842 if (!s->dma_adc.mapped) {
843 if (s->dma_adc.count > (signed)(s->dma_adc.dmasize - ((3 * s->dma_adc.fragsize) >> 1))) {
844 s->enable &= ~SV_CENABLE_RE;
845 wrindir(s, SV_CIENABLE, s->enable);
850 /* update DAC pointer */
851 if (s->dma_dac.ready) {
852 hwptr = (s->dma_dac.dmasize - get_dmaa(s)) % s->dma_dac.dmasize;
853 diff = (s->dma_dac.dmasize + hwptr - s->dma_dac.hwptr) % s->dma_dac.dmasize;
854 s->dma_dac.hwptr = hwptr;
855 s->dma_dac.total_bytes += diff;
856 if (s->dma_dac.mapped) {
857 s->dma_dac.count += diff;
858 if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
859 wake_up(&s->dma_dac.wait);
861 s->dma_dac.count -= diff;
862 if (s->dma_dac.count <= 0) {
863 s->enable &= ~SV_CENABLE_PE;
864 wrindir(s, SV_CIENABLE, s->enable);
866 } else if (s->dma_dac.count <= (signed)s->dma_dac.fragsize && !s->dma_dac.endcleared) {
868 s->dma_dac.endcleared = 1;
870 if (s->dma_dac.count + (signed)s->dma_dac.fragsize <= (signed)s->dma_dac.dmasize)
871 wake_up(&s->dma_dac.wait);
876 /* hold spinlock for the following! */
877 static void sv_handle_midi(struct sv_state *s)
883 while (!(inb(s->iomidi+1) & 0x80)) {
885 if (s->midi.icnt < MIDIINBUF) {
886 s->midi.ibuf[s->midi.iwr] = ch;
887 s->midi.iwr = (s->midi.iwr + 1) % MIDIINBUF;
893 wake_up(&s->midi.iwait);
895 while (!(inb(s->iomidi+1) & 0x40) && s->midi.ocnt > 0) {
896 outb(s->midi.obuf[s->midi.ord], s->iomidi);
897 s->midi.ord = (s->midi.ord + 1) % MIDIOUTBUF;
899 if (s->midi.ocnt < MIDIOUTBUF-16)
903 wake_up(&s->midi.owait);
906 static irqreturn_t sv_interrupt(int irq, void *dev_id, struct pt_regs *regs)
908 struct sv_state *s = (struct sv_state *)dev_id;
911 /* fastpath out, to ease interrupt sharing */
912 intsrc = inb(s->ioenh + SV_CODEC_STATUS);
913 if (!(intsrc & (SV_CSTAT_DMAA | SV_CSTAT_DMAC | SV_CSTAT_MIDI)))
918 spin_unlock(&s->lock);
922 static void sv_midi_timer(unsigned long data)
924 struct sv_state *s = (struct sv_state *)data;
927 spin_lock_irqsave(&s->lock, flags);
929 spin_unlock_irqrestore(&s->lock, flags);
930 s->midi.timer.expires = jiffies+1;
931 add_timer(&s->midi.timer);
934 /* --------------------------------------------------------------------- */
936 static const char invalid_magic[] = KERN_CRIT "sv: invalid magic value\n";
938 #define VALIDATE_STATE(s) \
940 if (!(s) || (s)->magic != SV_MAGIC) { \
941 printk(invalid_magic); \
946 /* --------------------------------------------------------------------- */
950 #define MT_4MUTEMONO 3
953 static const struct {
958 } mixtable[SOUND_MIXER_NRDEVICES] = {
959 [SOUND_MIXER_RECLEV] = { SV_CIMIX_ADCINL, SV_CIMIX_ADCINR, MT_4, 0 },
960 [SOUND_MIXER_LINE1] = { SV_CIMIX_AUX1INL, SV_CIMIX_AUX1INR, MT_5MUTE, 5 },
961 [SOUND_MIXER_CD] = { SV_CIMIX_CDINL, SV_CIMIX_CDINR, MT_5MUTE, 1 },
962 [SOUND_MIXER_LINE] = { SV_CIMIX_LINEINL, SV_CIMIX_LINEINR, MT_5MUTE, 4 },
963 [SOUND_MIXER_MIC] = { SV_CIMIX_MICIN, SV_CIMIX_ADCINL, MT_4MUTEMONO, 6 },
964 [SOUND_MIXER_SYNTH] = { SV_CIMIX_SYNTHINL, SV_CIMIX_SYNTHINR, MT_5MUTE, 2 },
965 [SOUND_MIXER_LINE2] = { SV_CIMIX_AUX2INL, SV_CIMIX_AUX2INR, MT_5MUTE, 3 },
966 [SOUND_MIXER_VOLUME] = { SV_CIMIX_ANALOGINL, SV_CIMIX_ANALOGINR, MT_5MUTE, 7 },
967 [SOUND_MIXER_PCM] = { SV_CIMIX_PCMINL, SV_CIMIX_PCMINR, MT_6MUTE, 0 }
970 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
972 static int return_mixval(struct sv_state *s, unsigned i, int *arg)
975 unsigned char l, r, rl, rr;
977 spin_lock_irqsave(&s->lock, flags);
978 l = rdindir(s, mixtable[i].left);
979 r = rdindir(s, mixtable[i].right);
980 spin_unlock_irqrestore(&s->lock, flags);
981 switch (mixtable[i].type) {
985 rl = 10 + 6 * (l & 15);
986 rr = 10 + 6 * (r & 15);
990 rl = 55 - 3 * (l & 15);
999 rl = 100 - 3 * (l & 31);
1000 rr = 100 - 3 * (r & 31);
1004 rl = 100 - 3 * (l & 63) / 2;
1005 rr = 100 - 3 * (r & 63) / 2;
1012 return put_user((rr << 8) | rl, arg);
1015 #else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
1017 static const unsigned char volidx[SOUND_MIXER_NRDEVICES] =
1019 [SOUND_MIXER_RECLEV] = 1,
1020 [SOUND_MIXER_LINE1] = 2,
1021 [SOUND_MIXER_CD] = 3,
1022 [SOUND_MIXER_LINE] = 4,
1023 [SOUND_MIXER_MIC] = 5,
1024 [SOUND_MIXER_SYNTH] = 6,
1025 [SOUND_MIXER_LINE2] = 7,
1026 [SOUND_MIXER_VOLUME] = 8,
1027 [SOUND_MIXER_PCM] = 9
1030 #endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
1032 static unsigned mixer_recmask(struct sv_state *s)
1034 unsigned long flags;
1037 spin_lock_irqsave(&s->lock, flags);
1038 j = rdindir(s, SV_CIMIX_ADCINL) >> 5;
1039 spin_unlock_irqrestore(&s->lock, flags);
1041 for (i = 0; i < SOUND_MIXER_NRDEVICES && mixtable[i].rec != j; i++);
1045 static int mixer_ioctl(struct sv_state *s, unsigned int cmd, unsigned long arg)
1047 unsigned long flags;
1049 unsigned char l, r, rl, rr;
1050 int __user *p = (int __user *)arg;
1053 if (cmd == SOUND_MIXER_INFO) {
1055 memset(&info, 0, sizeof(info));
1056 strlcpy(info.id, "SonicVibes", sizeof(info.id));
1057 strlcpy(info.name, "S3 SonicVibes", sizeof(info.name));
1058 info.modify_counter = s->mix.modcnt;
1059 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
1063 if (cmd == SOUND_OLD_MIXER_INFO) {
1064 _old_mixer_info info;
1065 memset(&info, 0, sizeof(info));
1066 strlcpy(info.id, "SonicVibes", sizeof(info.id));
1067 strlcpy(info.name, "S3 SonicVibes", sizeof(info.name));
1068 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
1072 if (cmd == OSS_GETVERSION)
1073 return put_user(SOUND_VERSION, p);
1074 if (cmd == SOUND_MIXER_PRIVATE1) { /* SRS settings */
1075 if (get_user(val, p))
1077 spin_lock_irqsave(&s->lock, flags);
1080 l = 4 - ((val >> 2) & 7);
1083 r = 4 - ((val >> 5) & 7);
1086 wrindir(s, SV_CISRSSPACE, l);
1087 wrindir(s, SV_CISRSCENTER, r);
1089 wrindir(s, SV_CISRSSPACE, 0x80);
1091 l = rdindir(s, SV_CISRSSPACE);
1092 r = rdindir(s, SV_CISRSCENTER);
1093 spin_unlock_irqrestore(&s->lock, flags);
1095 return put_user(0, p);
1096 return put_user(((4 - (l & 7)) << 2) | ((4 - (r & 7)) << 5) | 2, p);
1098 if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
1100 if (_SIOC_DIR(cmd) == _SIOC_READ) {
1101 switch (_IOC_NR(cmd)) {
1102 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
1103 return put_user(mixer_recmask(s), p);
1105 case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */
1106 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1107 if (mixtable[i].type)
1109 return put_user(val, p);
1111 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
1112 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1113 if (mixtable[i].rec)
1115 return put_user(val, p);
1117 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
1118 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1119 if (mixtable[i].type && mixtable[i].type != MT_4MUTEMONO)
1121 return put_user(val, p);
1123 case SOUND_MIXER_CAPS:
1124 return put_user(SOUND_CAP_EXCL_INPUT, p);
1128 if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
1130 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1131 return return_mixval(s, i, p);
1132 #else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
1135 return put_user(s->mix.vol[volidx[i]-1], p);
1136 #endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
1139 if (_SIOC_DIR(cmd) != (_SIOC_READ|_SIOC_WRITE))
1142 switch (_IOC_NR(cmd)) {
1143 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
1144 if (get_user(val, p))
1148 return 0; /*val = mixer_recmask(s);*/
1150 val &= ~mixer_recmask(s);
1151 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1152 if (!(val & (1 << i)))
1154 if (mixtable[i].rec)
1157 if (i == SOUND_MIXER_NRDEVICES)
1159 spin_lock_irqsave(&s->lock, flags);
1160 frobindir(s, SV_CIMIX_ADCINL, 0x1f, mixtable[i].rec << 5);
1161 frobindir(s, SV_CIMIX_ADCINR, 0x1f, mixtable[i].rec << 5);
1162 spin_unlock_irqrestore(&s->lock, flags);
1167 if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
1169 if (get_user(val, p))
1172 r = (val >> 8) & 0xff;
1173 if (mixtable[i].type == MT_4MUTEMONO)
1179 spin_lock_irqsave(&s->lock, flags);
1180 switch (mixtable[i].type) {
1186 frobindir(s, mixtable[i].left, 0xf0, l / 6);
1187 frobindir(s, mixtable[i].right, 0xf0, l / 6);
1201 wrindir(s, mixtable[i].left, rl);
1202 frobindir(s, mixtable[i].right, ~0x10, rr);
1214 wrindir(s, mixtable[i].left, rl);
1215 wrindir(s, mixtable[i].right, rr);
1222 rl = (100 - l) * 2 / 3;
1226 rr = (100 - r) * 2 / 3;
1227 wrindir(s, mixtable[i].left, rl);
1228 wrindir(s, mixtable[i].right, rr);
1231 spin_unlock_irqrestore(&s->lock, flags);
1232 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1233 return return_mixval(s, i, p);
1234 #else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
1237 s->mix.vol[volidx[i]-1] = val;
1238 return put_user(s->mix.vol[volidx[i]-1], p);
1239 #endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
1243 /* --------------------------------------------------------------------- */
1245 static int sv_open_mixdev(struct inode *inode, struct file *file)
1247 int minor = iminor(inode);
1248 struct list_head *list;
1251 for (list = devs.next; ; list = list->next) {
1254 s = list_entry(list, struct sv_state, devs);
1255 if (s->dev_mixer == minor)
1259 file->private_data = s;
1260 return nonseekable_open(inode, file);
1263 static int sv_release_mixdev(struct inode *inode, struct file *file)
1265 struct sv_state *s = (struct sv_state *)file->private_data;
1271 static int sv_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1273 return mixer_ioctl((struct sv_state *)file->private_data, cmd, arg);
1276 static /*const*/ struct file_operations sv_mixer_fops = {
1277 .owner = THIS_MODULE,
1278 .llseek = no_llseek,
1279 .ioctl = sv_ioctl_mixdev,
1280 .open = sv_open_mixdev,
1281 .release = sv_release_mixdev,
1284 /* --------------------------------------------------------------------- */
1286 static int drain_dac(struct sv_state *s, int nonblock)
1288 DECLARE_WAITQUEUE(wait, current);
1289 unsigned long flags;
1292 if (s->dma_dac.mapped || !s->dma_dac.ready)
1294 add_wait_queue(&s->dma_dac.wait, &wait);
1296 __set_current_state(TASK_INTERRUPTIBLE);
1297 spin_lock_irqsave(&s->lock, flags);
1298 count = s->dma_dac.count;
1299 spin_unlock_irqrestore(&s->lock, flags);
1302 if (signal_pending(current))
1305 remove_wait_queue(&s->dma_dac.wait, &wait);
1306 set_current_state(TASK_RUNNING);
1309 tmo = 3 * HZ * (count + s->dma_dac.fragsize) / 2 / s->ratedac;
1310 tmo >>= sample_shift[(s->fmt >> SV_CFMT_ASHIFT) & SV_CFMT_MASK];
1311 if (!schedule_timeout(tmo + 1))
1312 printk(KERN_DEBUG "sv: dma timed out??\n");
1314 remove_wait_queue(&s->dma_dac.wait, &wait);
1315 set_current_state(TASK_RUNNING);
1316 if (signal_pending(current))
1317 return -ERESTARTSYS;
1321 /* --------------------------------------------------------------------- */
1323 static ssize_t sv_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
1325 struct sv_state *s = (struct sv_state *)file->private_data;
1326 DECLARE_WAITQUEUE(wait, current);
1328 unsigned long flags;
1333 if (s->dma_adc.mapped)
1335 if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
1337 if (!access_ok(VERIFY_WRITE, buffer, count))
1341 spin_lock_irqsave(&s->lock, flags);
1343 spin_unlock_irqrestore(&s->lock, flags);
1345 add_wait_queue(&s->dma_adc.wait, &wait);
1347 spin_lock_irqsave(&s->lock, flags);
1348 swptr = s->dma_adc.swptr;
1349 cnt = s->dma_adc.dmasize-swptr;
1350 if (s->dma_adc.count < cnt)
1351 cnt = s->dma_adc.count;
1353 __set_current_state(TASK_INTERRUPTIBLE);
1354 spin_unlock_irqrestore(&s->lock, flags);
1358 if (s->dma_adc.enabled)
1360 if (file->f_flags & O_NONBLOCK) {
1365 if (!schedule_timeout(HZ)) {
1366 printk(KERN_DEBUG "sv: read: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1367 s->dma_adc.dmasize, s->dma_adc.fragsize, s->dma_adc.count,
1368 s->dma_adc.hwptr, s->dma_adc.swptr);
1370 spin_lock_irqsave(&s->lock, flags);
1371 set_dmac(s, virt_to_bus(s->dma_adc.rawbuf), s->dma_adc.numfrag << s->dma_adc.fragshift);
1372 /* program enhanced mode registers */
1373 wrindir(s, SV_CIDMACBASECOUNT1, (s->dma_adc.fragsamples-1) >> 8);
1374 wrindir(s, SV_CIDMACBASECOUNT0, s->dma_adc.fragsamples-1);
1375 s->dma_adc.count = s->dma_adc.hwptr = s->dma_adc.swptr = 0;
1376 spin_unlock_irqrestore(&s->lock, flags);
1378 if (signal_pending(current)) {
1385 if (copy_to_user(buffer, s->dma_adc.rawbuf + swptr, cnt)) {
1390 swptr = (swptr + cnt) % s->dma_adc.dmasize;
1391 spin_lock_irqsave(&s->lock, flags);
1392 s->dma_adc.swptr = swptr;
1393 s->dma_adc.count -= cnt;
1394 spin_unlock_irqrestore(&s->lock, flags);
1398 if (s->dma_adc.enabled)
1401 remove_wait_queue(&s->dma_adc.wait, &wait);
1402 set_current_state(TASK_RUNNING);
1406 static ssize_t sv_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
1408 struct sv_state *s = (struct sv_state *)file->private_data;
1409 DECLARE_WAITQUEUE(wait, current);
1411 unsigned long flags;
1416 if (s->dma_dac.mapped)
1418 if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
1420 if (!access_ok(VERIFY_READ, buffer, count))
1424 spin_lock_irqsave(&s->lock, flags);
1426 spin_unlock_irqrestore(&s->lock, flags);
1428 add_wait_queue(&s->dma_dac.wait, &wait);
1430 spin_lock_irqsave(&s->lock, flags);
1431 if (s->dma_dac.count < 0) {
1432 s->dma_dac.count = 0;
1433 s->dma_dac.swptr = s->dma_dac.hwptr;
1435 swptr = s->dma_dac.swptr;
1436 cnt = s->dma_dac.dmasize-swptr;
1437 if (s->dma_dac.count + cnt > s->dma_dac.dmasize)
1438 cnt = s->dma_dac.dmasize - s->dma_dac.count;
1440 __set_current_state(TASK_INTERRUPTIBLE);
1441 spin_unlock_irqrestore(&s->lock, flags);
1445 if (s->dma_dac.enabled)
1447 if (file->f_flags & O_NONBLOCK) {
1452 if (!schedule_timeout(HZ)) {
1453 printk(KERN_DEBUG "sv: write: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1454 s->dma_dac.dmasize, s->dma_dac.fragsize, s->dma_dac.count,
1455 s->dma_dac.hwptr, s->dma_dac.swptr);
1457 spin_lock_irqsave(&s->lock, flags);
1458 set_dmaa(s, virt_to_bus(s->dma_dac.rawbuf), s->dma_dac.numfrag << s->dma_dac.fragshift);
1459 /* program enhanced mode registers */
1460 wrindir(s, SV_CIDMAABASECOUNT1, (s->dma_dac.fragsamples-1) >> 8);
1461 wrindir(s, SV_CIDMAABASECOUNT0, s->dma_dac.fragsamples-1);
1462 s->dma_dac.count = s->dma_dac.hwptr = s->dma_dac.swptr = 0;
1463 spin_unlock_irqrestore(&s->lock, flags);
1465 if (signal_pending(current)) {
1472 if (copy_from_user(s->dma_dac.rawbuf + swptr, buffer, cnt)) {
1477 swptr = (swptr + cnt) % s->dma_dac.dmasize;
1478 spin_lock_irqsave(&s->lock, flags);
1479 s->dma_dac.swptr = swptr;
1480 s->dma_dac.count += cnt;
1481 s->dma_dac.endcleared = 0;
1482 spin_unlock_irqrestore(&s->lock, flags);
1486 if (s->dma_dac.enabled)
1489 remove_wait_queue(&s->dma_dac.wait, &wait);
1490 set_current_state(TASK_RUNNING);
1494 /* No kernel lock - we have our own spinlock */
1495 static unsigned int sv_poll(struct file *file, struct poll_table_struct *wait)
1497 struct sv_state *s = (struct sv_state *)file->private_data;
1498 unsigned long flags;
1499 unsigned int mask = 0;
1502 if (file->f_mode & FMODE_WRITE) {
1503 if (!s->dma_dac.ready && prog_dmabuf(s, 1))
1505 poll_wait(file, &s->dma_dac.wait, wait);
1507 if (file->f_mode & FMODE_READ) {
1508 if (!s->dma_adc.ready && prog_dmabuf(s, 0))
1510 poll_wait(file, &s->dma_adc.wait, wait);
1512 spin_lock_irqsave(&s->lock, flags);
1514 if (file->f_mode & FMODE_READ) {
1515 if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
1516 mask |= POLLIN | POLLRDNORM;
1518 if (file->f_mode & FMODE_WRITE) {
1519 if (s->dma_dac.mapped) {
1520 if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
1521 mask |= POLLOUT | POLLWRNORM;
1523 if ((signed)s->dma_dac.dmasize >= s->dma_dac.count + (signed)s->dma_dac.fragsize)
1524 mask |= POLLOUT | POLLWRNORM;
1527 spin_unlock_irqrestore(&s->lock, flags);
1531 static int sv_mmap(struct file *file, struct vm_area_struct *vma)
1533 struct sv_state *s = (struct sv_state *)file->private_data;
1540 if (vma->vm_flags & VM_WRITE) {
1541 if ((ret = prog_dmabuf(s, 1)) != 0)
1544 } else if (vma->vm_flags & VM_READ) {
1545 if ((ret = prog_dmabuf(s, 0)) != 0)
1551 if (vma->vm_pgoff != 0)
1553 size = vma->vm_end - vma->vm_start;
1554 if (size > (PAGE_SIZE << db->buforder))
1557 if (remap_pfn_range(vma, vma->vm_start,
1558 virt_to_phys(db->rawbuf) >> PAGE_SHIFT,
1559 size, vma->vm_page_prot))
1568 static int sv_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1570 struct sv_state *s = (struct sv_state *)file->private_data;
1571 unsigned long flags;
1572 audio_buf_info abinfo;
1575 int val, mapped, ret;
1576 unsigned char fmtm, fmtd;
1577 void __user *argp = (void __user *)arg;
1578 int __user *p = argp;
1581 mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
1582 ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
1584 case OSS_GETVERSION:
1585 return put_user(SOUND_VERSION, p);
1587 case SNDCTL_DSP_SYNC:
1588 if (file->f_mode & FMODE_WRITE)
1589 return drain_dac(s, 0/*file->f_flags & O_NONBLOCK*/);
1592 case SNDCTL_DSP_SETDUPLEX:
1595 case SNDCTL_DSP_GETCAPS:
1596 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP, p);
1598 case SNDCTL_DSP_RESET:
1599 if (file->f_mode & FMODE_WRITE) {
1601 synchronize_irq(s->irq);
1602 s->dma_dac.swptr = s->dma_dac.hwptr = s->dma_dac.count = s->dma_dac.total_bytes = 0;
1604 if (file->f_mode & FMODE_READ) {
1606 synchronize_irq(s->irq);
1607 s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes = 0;
1611 case SNDCTL_DSP_SPEED:
1612 if (get_user(val, p))
1615 if (file->f_mode & FMODE_READ) {
1617 s->dma_adc.ready = 0;
1618 set_adc_rate(s, val);
1620 if (file->f_mode & FMODE_WRITE) {
1622 s->dma_dac.ready = 0;
1623 set_dac_rate(s, val);
1626 return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
1628 case SNDCTL_DSP_STEREO:
1629 if (get_user(val, p))
1633 if (file->f_mode & FMODE_READ) {
1635 s->dma_adc.ready = 0;
1637 fmtd |= SV_CFMT_STEREO << SV_CFMT_CSHIFT;
1639 fmtm &= ~(SV_CFMT_STEREO << SV_CFMT_CSHIFT);
1641 if (file->f_mode & FMODE_WRITE) {
1643 s->dma_dac.ready = 0;
1645 fmtd |= SV_CFMT_STEREO << SV_CFMT_ASHIFT;
1647 fmtm &= ~(SV_CFMT_STEREO << SV_CFMT_ASHIFT);
1649 set_fmt(s, fmtm, fmtd);
1652 case SNDCTL_DSP_CHANNELS:
1653 if (get_user(val, p))
1658 if (file->f_mode & FMODE_READ) {
1660 s->dma_adc.ready = 0;
1662 fmtd |= SV_CFMT_STEREO << SV_CFMT_CSHIFT;
1664 fmtm &= ~(SV_CFMT_STEREO << SV_CFMT_CSHIFT);
1666 if (file->f_mode & FMODE_WRITE) {
1668 s->dma_dac.ready = 0;
1670 fmtd |= SV_CFMT_STEREO << SV_CFMT_ASHIFT;
1672 fmtm &= ~(SV_CFMT_STEREO << SV_CFMT_ASHIFT);
1674 set_fmt(s, fmtm, fmtd);
1676 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (SV_CFMT_STEREO << SV_CFMT_CSHIFT)
1677 : (SV_CFMT_STEREO << SV_CFMT_ASHIFT))) ? 2 : 1, p);
1679 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
1680 return put_user(AFMT_S16_LE|AFMT_U8, p);
1682 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
1683 if (get_user(val, p))
1685 if (val != AFMT_QUERY) {
1688 if (file->f_mode & FMODE_READ) {
1690 s->dma_adc.ready = 0;
1691 if (val == AFMT_S16_LE)
1692 fmtd |= SV_CFMT_16BIT << SV_CFMT_CSHIFT;
1694 fmtm &= ~(SV_CFMT_16BIT << SV_CFMT_CSHIFT);
1696 if (file->f_mode & FMODE_WRITE) {
1698 s->dma_dac.ready = 0;
1699 if (val == AFMT_S16_LE)
1700 fmtd |= SV_CFMT_16BIT << SV_CFMT_ASHIFT;
1702 fmtm &= ~(SV_CFMT_16BIT << SV_CFMT_ASHIFT);
1704 set_fmt(s, fmtm, fmtd);
1706 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (SV_CFMT_16BIT << SV_CFMT_CSHIFT)
1707 : (SV_CFMT_16BIT << SV_CFMT_ASHIFT))) ? AFMT_S16_LE : AFMT_U8, p);
1709 case SNDCTL_DSP_POST:
1712 case SNDCTL_DSP_GETTRIGGER:
1714 if (file->f_mode & FMODE_READ && s->enable & SV_CENABLE_RE)
1715 val |= PCM_ENABLE_INPUT;
1716 if (file->f_mode & FMODE_WRITE && s->enable & SV_CENABLE_PE)
1717 val |= PCM_ENABLE_OUTPUT;
1718 return put_user(val, p);
1720 case SNDCTL_DSP_SETTRIGGER:
1721 if (get_user(val, p))
1723 if (file->f_mode & FMODE_READ) {
1724 if (val & PCM_ENABLE_INPUT) {
1725 if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
1727 s->dma_adc.enabled = 1;
1730 s->dma_adc.enabled = 0;
1734 if (file->f_mode & FMODE_WRITE) {
1735 if (val & PCM_ENABLE_OUTPUT) {
1736 if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
1738 s->dma_dac.enabled = 1;
1741 s->dma_dac.enabled = 0;
1747 case SNDCTL_DSP_GETOSPACE:
1748 if (!(file->f_mode & FMODE_WRITE))
1750 if (!s->dma_dac.ready && (val = prog_dmabuf(s, 0)) != 0)
1752 spin_lock_irqsave(&s->lock, flags);
1754 abinfo.fragsize = s->dma_dac.fragsize;
1755 count = s->dma_dac.count;
1758 abinfo.bytes = s->dma_dac.dmasize - count;
1759 abinfo.fragstotal = s->dma_dac.numfrag;
1760 abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
1761 spin_unlock_irqrestore(&s->lock, flags);
1762 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
1764 case SNDCTL_DSP_GETISPACE:
1765 if (!(file->f_mode & FMODE_READ))
1767 if (!s->dma_adc.ready && (val = prog_dmabuf(s, 1)) != 0)
1769 spin_lock_irqsave(&s->lock, flags);
1771 abinfo.fragsize = s->dma_adc.fragsize;
1772 count = s->dma_adc.count;
1775 abinfo.bytes = count;
1776 abinfo.fragstotal = s->dma_adc.numfrag;
1777 abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;
1778 spin_unlock_irqrestore(&s->lock, flags);
1779 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
1781 case SNDCTL_DSP_NONBLOCK:
1782 file->f_flags |= O_NONBLOCK;
1785 case SNDCTL_DSP_GETODELAY:
1786 if (!(file->f_mode & FMODE_WRITE))
1788 if (!s->dma_dac.ready && (val = prog_dmabuf(s, 0)) != 0)
1790 spin_lock_irqsave(&s->lock, flags);
1792 count = s->dma_dac.count;
1793 spin_unlock_irqrestore(&s->lock, flags);
1796 return put_user(count, p);
1798 case SNDCTL_DSP_GETIPTR:
1799 if (!(file->f_mode & FMODE_READ))
1801 if (!s->dma_adc.ready && (val = prog_dmabuf(s, 1)) != 0)
1803 spin_lock_irqsave(&s->lock, flags);
1805 cinfo.bytes = s->dma_adc.total_bytes;
1806 count = s->dma_adc.count;
1809 cinfo.blocks = count >> s->dma_adc.fragshift;
1810 cinfo.ptr = s->dma_adc.hwptr;
1811 if (s->dma_adc.mapped)
1812 s->dma_adc.count &= s->dma_adc.fragsize-1;
1813 spin_unlock_irqrestore(&s->lock, flags);
1814 if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
1818 case SNDCTL_DSP_GETOPTR:
1819 if (!(file->f_mode & FMODE_WRITE))
1821 if (!s->dma_dac.ready && (val = prog_dmabuf(s, 0)) != 0)
1823 spin_lock_irqsave(&s->lock, flags);
1825 cinfo.bytes = s->dma_dac.total_bytes;
1826 count = s->dma_dac.count;
1829 cinfo.blocks = count >> s->dma_dac.fragshift;
1830 cinfo.ptr = s->dma_dac.hwptr;
1831 if (s->dma_dac.mapped)
1832 s->dma_dac.count &= s->dma_dac.fragsize-1;
1833 spin_unlock_irqrestore(&s->lock, flags);
1834 if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
1838 case SNDCTL_DSP_GETBLKSIZE:
1839 if (file->f_mode & FMODE_WRITE) {
1840 if ((val = prog_dmabuf(s, 0)))
1842 return put_user(s->dma_dac.fragsize, p);
1844 if ((val = prog_dmabuf(s, 1)))
1846 return put_user(s->dma_adc.fragsize, p);
1848 case SNDCTL_DSP_SETFRAGMENT:
1849 if (get_user(val, p))
1851 if (file->f_mode & FMODE_READ) {
1852 s->dma_adc.ossfragshift = val & 0xffff;
1853 s->dma_adc.ossmaxfrags = (val >> 16) & 0xffff;
1854 if (s->dma_adc.ossfragshift < 4)
1855 s->dma_adc.ossfragshift = 4;
1856 if (s->dma_adc.ossfragshift > 15)
1857 s->dma_adc.ossfragshift = 15;
1858 if (s->dma_adc.ossmaxfrags < 4)
1859 s->dma_adc.ossmaxfrags = 4;
1861 if (file->f_mode & FMODE_WRITE) {
1862 s->dma_dac.ossfragshift = val & 0xffff;
1863 s->dma_dac.ossmaxfrags = (val >> 16) & 0xffff;
1864 if (s->dma_dac.ossfragshift < 4)
1865 s->dma_dac.ossfragshift = 4;
1866 if (s->dma_dac.ossfragshift > 15)
1867 s->dma_dac.ossfragshift = 15;
1868 if (s->dma_dac.ossmaxfrags < 4)
1869 s->dma_dac.ossmaxfrags = 4;
1873 case SNDCTL_DSP_SUBDIVIDE:
1874 if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
1875 (file->f_mode & FMODE_WRITE && s->dma_dac.subdivision))
1877 if (get_user(val, p))
1879 if (val != 1 && val != 2 && val != 4)
1881 if (file->f_mode & FMODE_READ)
1882 s->dma_adc.subdivision = val;
1883 if (file->f_mode & FMODE_WRITE)
1884 s->dma_dac.subdivision = val;
1887 case SOUND_PCM_READ_RATE:
1888 return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
1890 case SOUND_PCM_READ_CHANNELS:
1891 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (SV_CFMT_STEREO << SV_CFMT_CSHIFT)
1892 : (SV_CFMT_STEREO << SV_CFMT_ASHIFT))) ? 2 : 1, p);
1894 case SOUND_PCM_READ_BITS:
1895 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (SV_CFMT_16BIT << SV_CFMT_CSHIFT)
1896 : (SV_CFMT_16BIT << SV_CFMT_ASHIFT))) ? 16 : 8, p);
1898 case SOUND_PCM_WRITE_FILTER:
1899 case SNDCTL_DSP_SETSYNCRO:
1900 case SOUND_PCM_READ_FILTER:
1904 return mixer_ioctl(s, cmd, arg);
1907 static int sv_open(struct inode *inode, struct file *file)
1909 int minor = iminor(inode);
1910 DECLARE_WAITQUEUE(wait, current);
1911 unsigned char fmtm = ~0, fmts = 0;
1912 struct list_head *list;
1915 for (list = devs.next; ; list = list->next) {
1918 s = list_entry(list, struct sv_state, devs);
1919 if (!((s->dev_audio ^ minor) & ~0xf))
1923 file->private_data = s;
1924 /* wait for device to become free */
1926 while (s->open_mode & file->f_mode) {
1927 if (file->f_flags & O_NONBLOCK) {
1931 add_wait_queue(&s->open_wait, &wait);
1932 __set_current_state(TASK_INTERRUPTIBLE);
1935 remove_wait_queue(&s->open_wait, &wait);
1936 set_current_state(TASK_RUNNING);
1937 if (signal_pending(current))
1938 return -ERESTARTSYS;
1941 if (file->f_mode & FMODE_READ) {
1942 fmtm &= ~((SV_CFMT_STEREO | SV_CFMT_16BIT) << SV_CFMT_CSHIFT);
1943 if ((minor & 0xf) == SND_DEV_DSP16)
1944 fmts |= SV_CFMT_16BIT << SV_CFMT_CSHIFT;
1945 s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags = s->dma_adc.subdivision = 0;
1946 s->dma_adc.enabled = 1;
1947 set_adc_rate(s, 8000);
1949 if (file->f_mode & FMODE_WRITE) {
1950 fmtm &= ~((SV_CFMT_STEREO | SV_CFMT_16BIT) << SV_CFMT_ASHIFT);
1951 if ((minor & 0xf) == SND_DEV_DSP16)
1952 fmts |= SV_CFMT_16BIT << SV_CFMT_ASHIFT;
1953 s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags = s->dma_dac.subdivision = 0;
1954 s->dma_dac.enabled = 1;
1955 set_dac_rate(s, 8000);
1957 set_fmt(s, fmtm, fmts);
1958 s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
1960 return nonseekable_open(inode, file);
1963 static int sv_release(struct inode *inode, struct file *file)
1965 struct sv_state *s = (struct sv_state *)file->private_data;
1969 if (file->f_mode & FMODE_WRITE)
1970 drain_dac(s, file->f_flags & O_NONBLOCK);
1972 if (file->f_mode & FMODE_WRITE) {
1974 dealloc_dmabuf(s, &s->dma_dac);
1976 if (file->f_mode & FMODE_READ) {
1978 dealloc_dmabuf(s, &s->dma_adc);
1980 s->open_mode &= ~(file->f_mode & (FMODE_READ|FMODE_WRITE));
1981 wake_up(&s->open_wait);
1987 static /*const*/ struct file_operations sv_audio_fops = {
1988 .owner = THIS_MODULE,
1989 .llseek = no_llseek,
1996 .release = sv_release,
1999 /* --------------------------------------------------------------------- */
2001 static ssize_t sv_midi_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
2003 struct sv_state *s = (struct sv_state *)file->private_data;
2004 DECLARE_WAITQUEUE(wait, current);
2006 unsigned long flags;
2011 if (!access_ok(VERIFY_WRITE, buffer, count))
2016 add_wait_queue(&s->midi.iwait, &wait);
2018 spin_lock_irqsave(&s->lock, flags);
2020 cnt = MIDIINBUF - ptr;
2021 if (s->midi.icnt < cnt)
2024 __set_current_state(TASK_INTERRUPTIBLE);
2025 spin_unlock_irqrestore(&s->lock, flags);
2029 if (file->f_flags & O_NONBLOCK) {
2035 if (signal_pending(current)) {
2042 if (copy_to_user(buffer, s->midi.ibuf + ptr, cnt)) {
2047 ptr = (ptr + cnt) % MIDIINBUF;
2048 spin_lock_irqsave(&s->lock, flags);
2050 s->midi.icnt -= cnt;
2051 spin_unlock_irqrestore(&s->lock, flags);
2057 __set_current_state(TASK_RUNNING);
2058 remove_wait_queue(&s->midi.iwait, &wait);
2062 static ssize_t sv_midi_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2064 struct sv_state *s = (struct sv_state *)file->private_data;
2065 DECLARE_WAITQUEUE(wait, current);
2067 unsigned long flags;
2072 if (!access_ok(VERIFY_READ, buffer, count))
2077 add_wait_queue(&s->midi.owait, &wait);
2079 spin_lock_irqsave(&s->lock, flags);
2081 cnt = MIDIOUTBUF - ptr;
2082 if (s->midi.ocnt + cnt > MIDIOUTBUF)
2083 cnt = MIDIOUTBUF - s->midi.ocnt;
2085 __set_current_state(TASK_INTERRUPTIBLE);
2088 spin_unlock_irqrestore(&s->lock, flags);
2092 if (file->f_flags & O_NONBLOCK) {
2098 if (signal_pending(current)) {
2105 if (copy_from_user(s->midi.obuf + ptr, buffer, cnt)) {
2110 ptr = (ptr + cnt) % MIDIOUTBUF;
2111 spin_lock_irqsave(&s->lock, flags);
2113 s->midi.ocnt += cnt;
2114 spin_unlock_irqrestore(&s->lock, flags);
2118 spin_lock_irqsave(&s->lock, flags);
2120 spin_unlock_irqrestore(&s->lock, flags);
2122 __set_current_state(TASK_RUNNING);
2123 remove_wait_queue(&s->midi.owait, &wait);
2127 /* No kernel lock - we have our own spinlock */
2128 static unsigned int sv_midi_poll(struct file *file, struct poll_table_struct *wait)
2130 struct sv_state *s = (struct sv_state *)file->private_data;
2131 unsigned long flags;
2132 unsigned int mask = 0;
2135 if (file->f_mode & FMODE_WRITE)
2136 poll_wait(file, &s->midi.owait, wait);
2137 if (file->f_mode & FMODE_READ)
2138 poll_wait(file, &s->midi.iwait, wait);
2139 spin_lock_irqsave(&s->lock, flags);
2140 if (file->f_mode & FMODE_READ) {
2141 if (s->midi.icnt > 0)
2142 mask |= POLLIN | POLLRDNORM;
2144 if (file->f_mode & FMODE_WRITE) {
2145 if (s->midi.ocnt < MIDIOUTBUF)
2146 mask |= POLLOUT | POLLWRNORM;
2148 spin_unlock_irqrestore(&s->lock, flags);
2152 static int sv_midi_open(struct inode *inode, struct file *file)
2154 int minor = iminor(inode);
2155 DECLARE_WAITQUEUE(wait, current);
2156 unsigned long flags;
2157 struct list_head *list;
2160 for (list = devs.next; ; list = list->next) {
2163 s = list_entry(list, struct sv_state, devs);
2164 if (s->dev_midi == minor)
2168 file->private_data = s;
2169 /* wait for device to become free */
2171 while (s->open_mode & (file->f_mode << FMODE_MIDI_SHIFT)) {
2172 if (file->f_flags & O_NONBLOCK) {
2176 add_wait_queue(&s->open_wait, &wait);
2177 __set_current_state(TASK_INTERRUPTIBLE);
2180 remove_wait_queue(&s->open_wait, &wait);
2181 set_current_state(TASK_RUNNING);
2182 if (signal_pending(current))
2183 return -ERESTARTSYS;
2186 spin_lock_irqsave(&s->lock, flags);
2187 if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
2188 s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
2189 s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
2190 //outb(inb(s->ioenh + SV_CODEC_CONTROL) | SV_CCTRL_WAVETABLE, s->ioenh + SV_CODEC_CONTROL);
2191 outb(inb(s->ioenh + SV_CODEC_INTMASK) | SV_CINTMASK_MIDI, s->ioenh + SV_CODEC_INTMASK);
2192 wrindir(s, SV_CIUARTCONTROL, 5); /* output MIDI data to external and internal synth */
2193 wrindir(s, SV_CIWAVETABLESRC, 1); /* Wavetable in PC RAM */
2194 outb(0xff, s->iomidi+1); /* reset command */
2195 outb(0x3f, s->iomidi+1); /* uart command */
2196 if (!(inb(s->iomidi+1) & 0x80))
2198 s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
2199 init_timer(&s->midi.timer);
2200 s->midi.timer.expires = jiffies+1;
2201 s->midi.timer.data = (unsigned long)s;
2202 s->midi.timer.function = sv_midi_timer;
2203 add_timer(&s->midi.timer);
2205 if (file->f_mode & FMODE_READ) {
2206 s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
2208 if (file->f_mode & FMODE_WRITE) {
2209 s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
2211 spin_unlock_irqrestore(&s->lock, flags);
2212 s->open_mode |= (file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ | FMODE_MIDI_WRITE);
2214 return nonseekable_open(inode, file);
2217 static int sv_midi_release(struct inode *inode, struct file *file)
2219 struct sv_state *s = (struct sv_state *)file->private_data;
2220 DECLARE_WAITQUEUE(wait, current);
2221 unsigned long flags;
2222 unsigned count, tmo;
2227 if (file->f_mode & FMODE_WRITE) {
2228 add_wait_queue(&s->midi.owait, &wait);
2230 __set_current_state(TASK_INTERRUPTIBLE);
2231 spin_lock_irqsave(&s->lock, flags);
2232 count = s->midi.ocnt;
2233 spin_unlock_irqrestore(&s->lock, flags);
2236 if (signal_pending(current))
2238 if (file->f_flags & O_NONBLOCK) {
2239 remove_wait_queue(&s->midi.owait, &wait);
2240 set_current_state(TASK_RUNNING);
2244 tmo = (count * HZ) / 3100;
2245 if (!schedule_timeout(tmo ? : 1) && tmo)
2246 printk(KERN_DEBUG "sv: midi timed out??\n");
2248 remove_wait_queue(&s->midi.owait, &wait);
2249 set_current_state(TASK_RUNNING);
2252 s->open_mode &= ~((file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ|FMODE_MIDI_WRITE));
2253 spin_lock_irqsave(&s->lock, flags);
2254 if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
2255 outb(inb(s->ioenh + SV_CODEC_INTMASK) & ~SV_CINTMASK_MIDI, s->ioenh + SV_CODEC_INTMASK);
2256 del_timer(&s->midi.timer);
2258 spin_unlock_irqrestore(&s->lock, flags);
2259 wake_up(&s->open_wait);
2265 static /*const*/ struct file_operations sv_midi_fops = {
2266 .owner = THIS_MODULE,
2267 .llseek = no_llseek,
2268 .read = sv_midi_read,
2269 .write = sv_midi_write,
2270 .poll = sv_midi_poll,
2271 .open = sv_midi_open,
2272 .release = sv_midi_release,
2275 /* --------------------------------------------------------------------- */
2277 static int sv_dmfm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2279 static const unsigned char op_offset[18] = {
2280 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
2281 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
2282 0x10, 0x11, 0x12, 0x13, 0x14, 0x15
2284 struct sv_state *s = (struct sv_state *)file->private_data;
2285 struct dm_fm_voice v;
2286 struct dm_fm_note n;
2287 struct dm_fm_params p;
2292 case FM_IOCTL_RESET:
2293 for (regb = 0xb0; regb < 0xb9; regb++) {
2294 outb(regb, s->iosynth);
2295 outb(0, s->iosynth+1);
2296 outb(regb, s->iosynth+2);
2297 outb(0, s->iosynth+3);
2301 case FM_IOCTL_PLAY_NOTE:
2302 if (copy_from_user(&n, (void __user *)arg, sizeof(n)))
2313 outb(0xa0 + regb, io);
2314 outb(n.fnum & 0xff, io+1);
2315 outb(0xb0 + regb, io);
2316 outb(((n.fnum >> 8) & 3) | ((n.octave & 7) << 2) | ((n.key_on & 1) << 5), io+1);
2319 case FM_IOCTL_SET_VOICE:
2320 if (copy_from_user(&v, (void __user *)arg, sizeof(v)))
2324 regb = op_offset[v.voice];
2325 io = s->iosynth + ((v.op & 1) << 1);
2326 outb(0x20 + regb, io);
2327 outb(((v.am & 1) << 7) | ((v.vibrato & 1) << 6) | ((v.do_sustain & 1) << 5) |
2328 ((v.kbd_scale & 1) << 4) | (v.harmonic & 0xf), io+1);
2329 outb(0x40 + regb, io);
2330 outb(((v.scale_level & 0x3) << 6) | (v.volume & 0x3f), io+1);
2331 outb(0x60 + regb, io);
2332 outb(((v.attack & 0xf) << 4) | (v.decay & 0xf), io+1);
2333 outb(0x80 + regb, io);
2334 outb(((v.sustain & 0xf) << 4) | (v.release & 0xf), io+1);
2335 outb(0xe0 + regb, io);
2336 outb(v.waveform & 0x7, io+1);
2344 outb(0xc0 + regb, io);
2345 outb(((v.right & 1) << 5) | ((v.left & 1) << 4) | ((v.feedback & 7) << 1) |
2346 (v.connection & 1), io+1);
2349 case FM_IOCTL_SET_PARAMS:
2350 if (copy_from_user(&p, (void *__user )arg, sizeof(p)))
2352 outb(0x08, s->iosynth);
2353 outb((p.kbd_split & 1) << 6, s->iosynth+1);
2354 outb(0xbd, s->iosynth);
2355 outb(((p.am_depth & 1) << 7) | ((p.vib_depth & 1) << 6) | ((p.rhythm & 1) << 5) | ((p.bass & 1) << 4) |
2356 ((p.snare & 1) << 3) | ((p.tomtom & 1) << 2) | ((p.cymbal & 1) << 1) | (p.hihat & 1), s->iosynth+1);
2359 case FM_IOCTL_SET_OPL:
2360 outb(4, s->iosynth+2);
2361 outb(arg, s->iosynth+3);
2364 case FM_IOCTL_SET_MODE:
2365 outb(5, s->iosynth+2);
2366 outb(arg & 1, s->iosynth+3);
2374 static int sv_dmfm_open(struct inode *inode, struct file *file)
2376 int minor = iminor(inode);
2377 DECLARE_WAITQUEUE(wait, current);
2378 struct list_head *list;
2381 for (list = devs.next; ; list = list->next) {
2384 s = list_entry(list, struct sv_state, devs);
2385 if (s->dev_dmfm == minor)
2389 file->private_data = s;
2390 /* wait for device to become free */
2392 while (s->open_mode & FMODE_DMFM) {
2393 if (file->f_flags & O_NONBLOCK) {
2397 add_wait_queue(&s->open_wait, &wait);
2398 __set_current_state(TASK_INTERRUPTIBLE);
2401 remove_wait_queue(&s->open_wait, &wait);
2402 set_current_state(TASK_RUNNING);
2403 if (signal_pending(current))
2404 return -ERESTARTSYS;
2407 /* init the stuff */
2408 outb(1, s->iosynth);
2409 outb(0x20, s->iosynth+1); /* enable waveforms */
2410 outb(4, s->iosynth+2);
2411 outb(0, s->iosynth+3); /* no 4op enabled */
2412 outb(5, s->iosynth+2);
2413 outb(1, s->iosynth+3); /* enable OPL3 */
2414 s->open_mode |= FMODE_DMFM;
2416 return nonseekable_open(inode, file);
2419 static int sv_dmfm_release(struct inode *inode, struct file *file)
2421 struct sv_state *s = (struct sv_state *)file->private_data;
2427 s->open_mode &= ~FMODE_DMFM;
2428 for (regb = 0xb0; regb < 0xb9; regb++) {
2429 outb(regb, s->iosynth);
2430 outb(0, s->iosynth+1);
2431 outb(regb, s->iosynth+2);
2432 outb(0, s->iosynth+3);
2434 wake_up(&s->open_wait);
2440 static /*const*/ struct file_operations sv_dmfm_fops = {
2441 .owner = THIS_MODULE,
2442 .llseek = no_llseek,
2443 .ioctl = sv_dmfm_ioctl,
2444 .open = sv_dmfm_open,
2445 .release = sv_dmfm_release,
2448 /* --------------------------------------------------------------------- */
2450 /* maximum number of devices; only used for command line params */
2453 static int reverb[NR_DEVICE];
2456 static int wavetable[NR_DEVICE];
2459 static unsigned int devindex;
2461 module_param_array(reverb, bool, NULL, 0);
2462 MODULE_PARM_DESC(reverb, "if 1 enables the reverb circuitry. NOTE: your card must have the reverb RAM");
2464 MODULE_PARM(wavetable, "1-" __MODULE_STRING(NR_DEVICE) "i");
2465 MODULE_PARM_DESC(wavetable, "if 1 the wavetable synth is enabled");
2468 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
2469 MODULE_DESCRIPTION("S3 SonicVibes Driver");
2470 MODULE_LICENSE("GPL");
2473 /* --------------------------------------------------------------------- */
2475 static struct initvol {
2478 } initvol[] __devinitdata = {
2479 { SOUND_MIXER_WRITE_RECLEV, 0x4040 },
2480 { SOUND_MIXER_WRITE_LINE1, 0x4040 },
2481 { SOUND_MIXER_WRITE_CD, 0x4040 },
2482 { SOUND_MIXER_WRITE_LINE, 0x4040 },
2483 { SOUND_MIXER_WRITE_MIC, 0x4040 },
2484 { SOUND_MIXER_WRITE_SYNTH, 0x4040 },
2485 { SOUND_MIXER_WRITE_LINE2, 0x4040 },
2486 { SOUND_MIXER_WRITE_VOLUME, 0x4040 },
2487 { SOUND_MIXER_WRITE_PCM, 0x4040 }
2490 #define RSRCISIOREGION(dev,num) (pci_resource_start((dev), (num)) != 0 && \
2491 (pci_resource_flags((dev), (num)) & IORESOURCE_IO))
2493 #ifdef SUPPORT_JOYSTICK
2494 static int __devinit sv_register_gameport(struct sv_state *s, int io_port)
2496 struct gameport *gp;
2498 if (!request_region(io_port, SV_EXTENT_GAME, "S3 SonicVibes Gameport")) {
2499 printk(KERN_ERR "sv: gameport io ports are in use\n");
2503 s->gameport = gp = gameport_allocate_port();
2505 printk(KERN_ERR "sv: can not allocate memory for gameport\n");
2506 release_region(io_port, SV_EXTENT_GAME);
2510 gameport_set_name(gp, "S3 SonicVibes Gameport");
2511 gameport_set_phys(gp, "isa%04x/gameport0", io_port);
2512 gp->dev.parent = &s->dev->dev;
2515 gameport_register_port(gp);
2520 static inline void sv_unregister_gameport(struct sv_state *s)
2523 int gpio = s->gameport->io;
2524 gameport_unregister_port(s->gameport);
2525 release_region(gpio, SV_EXTENT_GAME);
2529 static inline int sv_register_gameport(struct sv_state *s, int io_port) { return -ENOSYS; }
2530 static inline void sv_unregister_gameport(struct sv_state *s) { }
2531 #endif /* SUPPORT_JOYSTICK */
2533 static int __devinit sv_probe(struct pci_dev *pcidev, const struct pci_device_id *pciid)
2535 static char __devinitdata sv_ddma_name[] = "S3 Inc. SonicVibes DDMA Controller";
2541 unsigned ddmanamelen;
2543 if ((ret=pci_enable_device(pcidev)))
2546 if (!RSRCISIOREGION(pcidev, RESOURCE_SB) ||
2547 !RSRCISIOREGION(pcidev, RESOURCE_ENH) ||
2548 !RSRCISIOREGION(pcidev, RESOURCE_SYNTH) ||
2549 !RSRCISIOREGION(pcidev, RESOURCE_MIDI) ||
2550 !RSRCISIOREGION(pcidev, RESOURCE_GAME))
2552 if (pcidev->irq == 0)
2554 if (pci_set_dma_mask(pcidev, 0x00ffffff)) {
2555 printk(KERN_WARNING "sonicvibes: architecture does not support 24bit PCI busmaster DMA\n");
2558 /* try to allocate a DDMA resource if not already available */
2559 if (!RSRCISIOREGION(pcidev, RESOURCE_DDMA)) {
2560 pcidev->resource[RESOURCE_DDMA].start = 0;
2561 pcidev->resource[RESOURCE_DDMA].end = 2*SV_EXTENT_DMA-1;
2562 pcidev->resource[RESOURCE_DDMA].flags = PCI_BASE_ADDRESS_SPACE_IO | IORESOURCE_IO;
2563 ddmanamelen = strlen(sv_ddma_name)+1;
2564 if (!(ddmaname = kmalloc(ddmanamelen, GFP_KERNEL)))
2566 memcpy(ddmaname, sv_ddma_name, ddmanamelen);
2567 pcidev->resource[RESOURCE_DDMA].name = ddmaname;
2568 if (pci_assign_resource(pcidev, RESOURCE_DDMA)) {
2569 pcidev->resource[RESOURCE_DDMA].name = NULL;
2571 printk(KERN_ERR "sv: cannot allocate DDMA controller io ports\n");
2575 if (!(s = kmalloc(sizeof(struct sv_state), GFP_KERNEL))) {
2576 printk(KERN_WARNING "sv: out of memory\n");
2579 memset(s, 0, sizeof(struct sv_state));
2580 init_waitqueue_head(&s->dma_adc.wait);
2581 init_waitqueue_head(&s->dma_dac.wait);
2582 init_waitqueue_head(&s->open_wait);
2583 init_waitqueue_head(&s->midi.iwait);
2584 init_waitqueue_head(&s->midi.owait);
2585 init_MUTEX(&s->open_sem);
2586 spin_lock_init(&s->lock);
2587 s->magic = SV_MAGIC;
2589 s->iosb = pci_resource_start(pcidev, RESOURCE_SB);
2590 s->ioenh = pci_resource_start(pcidev, RESOURCE_ENH);
2591 s->iosynth = pci_resource_start(pcidev, RESOURCE_SYNTH);
2592 s->iomidi = pci_resource_start(pcidev, RESOURCE_MIDI);
2593 s->iodmaa = pci_resource_start(pcidev, RESOURCE_DDMA);
2594 s->iodmac = pci_resource_start(pcidev, RESOURCE_DDMA) + SV_EXTENT_DMA;
2595 gpio = pci_resource_start(pcidev, RESOURCE_GAME);
2596 pci_write_config_dword(pcidev, 0x40, s->iodmaa | 9); /* enable and use extended mode */
2597 pci_write_config_dword(pcidev, 0x48, s->iodmac | 9); /* enable */
2598 printk(KERN_DEBUG "sv: io ports: %#lx %#lx %#lx %#lx %#x %#x %#x\n",
2599 s->iosb, s->ioenh, s->iosynth, s->iomidi, gpio, s->iodmaa, s->iodmac);
2600 s->irq = pcidev->irq;
2603 pci_write_config_dword(pcidev, 0x60, wavetable_mem >> 12); /* wavetable base address */
2606 if (!request_region(s->ioenh, SV_EXTENT_ENH, "S3 SonicVibes PCM")) {
2607 printk(KERN_ERR "sv: io ports %#lx-%#lx in use\n", s->ioenh, s->ioenh+SV_EXTENT_ENH-1);
2610 if (!request_region(s->iodmaa, SV_EXTENT_DMA, "S3 SonicVibes DMAA")) {
2611 printk(KERN_ERR "sv: io ports %#x-%#x in use\n", s->iodmaa, s->iodmaa+SV_EXTENT_DMA-1);
2614 if (!request_region(s->iodmac, SV_EXTENT_DMA, "S3 SonicVibes DMAC")) {
2615 printk(KERN_ERR "sv: io ports %#x-%#x in use\n", s->iodmac, s->iodmac+SV_EXTENT_DMA-1);
2618 if (!request_region(s->iomidi, SV_EXTENT_MIDI, "S3 SonicVibes Midi")) {
2619 printk(KERN_ERR "sv: io ports %#lx-%#lx in use\n", s->iomidi, s->iomidi+SV_EXTENT_MIDI-1);
2622 if (!request_region(s->iosynth, SV_EXTENT_SYNTH, "S3 SonicVibes Synth")) {
2623 printk(KERN_ERR "sv: io ports %#lx-%#lx in use\n", s->iosynth, s->iosynth+SV_EXTENT_SYNTH-1);
2627 /* initialize codec registers */
2628 outb(0x80, s->ioenh + SV_CODEC_CONTROL); /* assert reset */
2630 outb(0x00, s->ioenh + SV_CODEC_CONTROL); /* deassert reset */
2632 outb(SV_CCTRL_INTADRIVE | SV_CCTRL_ENHANCED /*| SV_CCTRL_WAVETABLE */
2633 | (reverb[devindex] ? SV_CCTRL_REVERB : 0), s->ioenh + SV_CODEC_CONTROL);
2634 inb(s->ioenh + SV_CODEC_STATUS); /* clear ints */
2635 wrindir(s, SV_CIDRIVECONTROL, 0); /* drive current 16mA */
2636 wrindir(s, SV_CIENABLE, s->enable = 0); /* disable DMAA and DMAC */
2637 outb(~(SV_CINTMASK_DMAA | SV_CINTMASK_DMAC), s->ioenh + SV_CODEC_INTMASK);
2638 /* outb(0xff, s->iodmaa + SV_DMA_RESET); */
2639 /* outb(0xff, s->iodmac + SV_DMA_RESET); */
2640 inb(s->ioenh + SV_CODEC_STATUS); /* ack interrupts */
2641 wrindir(s, SV_CIADCCLKSOURCE, 0); /* use pll as ADC clock source */
2642 wrindir(s, SV_CIANALOGPWRDOWN, 0); /* power up the analog parts of the device */
2643 wrindir(s, SV_CIDIGITALPWRDOWN, 0); /* power up the digital parts of the device */
2644 setpll(s, SV_CIADCPLLM, 8000);
2645 wrindir(s, SV_CISRSSPACE, 0x80); /* SRS off */
2646 wrindir(s, SV_CIPCMSR0, (8000 * 65536 / FULLRATE) & 0xff);
2647 wrindir(s, SV_CIPCMSR1, ((8000 * 65536 / FULLRATE) >> 8) & 0xff);
2648 wrindir(s, SV_CIADCOUTPUT, 0);
2650 if ((ret=request_irq(s->irq,sv_interrupt,SA_SHIRQ,"S3 SonicVibes",s))) {
2651 printk(KERN_ERR "sv: irq %u in use\n", s->irq);
2654 printk(KERN_INFO "sv: found adapter at io %#lx irq %u dmaa %#06x dmac %#06x revision %u\n",
2655 s->ioenh, s->irq, s->iodmaa, s->iodmac, rdindir(s, SV_CIREVISION));
2656 /* register devices */
2657 if ((s->dev_audio = register_sound_dsp(&sv_audio_fops, -1)) < 0) {
2661 if ((s->dev_mixer = register_sound_mixer(&sv_mixer_fops, -1)) < 0) {
2665 if ((s->dev_midi = register_sound_midi(&sv_midi_fops, -1)) < 0) {
2669 if ((s->dev_dmfm = register_sound_special(&sv_dmfm_fops, 15 /* ?? */)) < 0) {
2673 pci_set_master(pcidev); /* enable bus mastering */
2674 /* initialize the chips */
2677 val = SOUND_MASK_LINE|SOUND_MASK_SYNTH;
2678 mixer_ioctl(s, SOUND_MIXER_WRITE_RECSRC, (unsigned long)&val);
2679 for (i = 0; i < sizeof(initvol)/sizeof(initvol[0]); i++) {
2680 val = initvol[i].vol;
2681 mixer_ioctl(s, initvol[i].mixch, (unsigned long)&val);
2684 /* register gameport */
2685 sv_register_gameport(s, gpio);
2686 /* store it in the driver field */
2687 pci_set_drvdata(pcidev, s);
2688 /* put it into driver list */
2689 list_add_tail(&s->devs, &devs);
2690 /* increment devindex */
2691 if (devindex < NR_DEVICE-1)
2696 unregister_sound_midi(s->dev_midi);
2698 unregister_sound_mixer(s->dev_mixer);
2700 unregister_sound_dsp(s->dev_audio);
2702 printk(KERN_ERR "sv: cannot register misc device\n");
2703 free_irq(s->irq, s);
2705 release_region(s->iosynth, SV_EXTENT_SYNTH);
2707 release_region(s->iomidi, SV_EXTENT_MIDI);
2709 release_region(s->iodmac, SV_EXTENT_DMA);
2711 release_region(s->iodmaa, SV_EXTENT_DMA);
2713 release_region(s->ioenh, SV_EXTENT_ENH);
2719 static void __devexit sv_remove(struct pci_dev *dev)
2721 struct sv_state *s = pci_get_drvdata(dev);
2726 outb(~0, s->ioenh + SV_CODEC_INTMASK); /* disable ints */
2727 synchronize_irq(s->irq);
2728 inb(s->ioenh + SV_CODEC_STATUS); /* ack interrupts */
2729 wrindir(s, SV_CIENABLE, 0); /* disable DMAA and DMAC */
2730 /*outb(0, s->iodmaa + SV_DMA_RESET);*/
2731 /*outb(0, s->iodmac + SV_DMA_RESET);*/
2732 free_irq(s->irq, s);
2733 sv_unregister_gameport(s);
2734 release_region(s->iodmac, SV_EXTENT_DMA);
2735 release_region(s->iodmaa, SV_EXTENT_DMA);
2736 release_region(s->ioenh, SV_EXTENT_ENH);
2737 release_region(s->iomidi, SV_EXTENT_MIDI);
2738 release_region(s->iosynth, SV_EXTENT_SYNTH);
2739 unregister_sound_dsp(s->dev_audio);
2740 unregister_sound_mixer(s->dev_mixer);
2741 unregister_sound_midi(s->dev_midi);
2742 unregister_sound_special(s->dev_dmfm);
2744 pci_set_drvdata(dev, NULL);
2747 static struct pci_device_id id_table[] = {
2748 { PCI_VENDOR_ID_S3, PCI_DEVICE_ID_S3_SONICVIBES, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
2752 MODULE_DEVICE_TABLE(pci, id_table);
2754 static struct pci_driver sv_driver = {
2755 .name = "sonicvibes",
2756 .id_table = id_table,
2758 .remove = __devexit_p(sv_remove),
2761 static int __init init_sonicvibes(void)
2763 printk(KERN_INFO "sv: version v0.31 time " __TIME__ " " __DATE__ "\n");
2765 if (!(wavetable_mem = __get_free_pages(GFP_KERNEL, 20-PAGE_SHIFT)))
2766 printk(KERN_INFO "sv: cannot allocate 1MB of contiguous nonpageable memory for wavetable data\n");
2768 return pci_register_driver(&sv_driver);
2771 static void __exit cleanup_sonicvibes(void)
2773 printk(KERN_INFO "sv: unloading\n");
2774 pci_unregister_driver(&sv_driver);
2776 free_pages(wavetable_mem, 20-PAGE_SHIFT);
2779 module_init(init_sonicvibes);
2780 module_exit(cleanup_sonicvibes);
2782 /* --------------------------------------------------------------------- */
2786 /* format is: sonicvibes=[reverb] sonicvibesdmaio=dmaioaddr */
2788 static int __init sonicvibes_setup(char *str)
2790 static unsigned __initdata nr_dev = 0;
2792 if (nr_dev >= NR_DEVICE)
2795 if (get_option(&str, &reverb[nr_dev]) == 2)
2796 (void)get_option(&str, &wavetable[nr_dev]);
2798 (void)get_option(&str, &reverb[nr_dev]);
2805 __setup("sonicvibes=", sonicvibes_setup);