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>
126 /* --------------------------------------------------------------------- */
128 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
130 /* --------------------------------------------------------------------- */
132 #ifndef PCI_VENDOR_ID_S3
133 #define PCI_VENDOR_ID_S3 0x5333
135 #ifndef PCI_DEVICE_ID_S3_SONICVIBES
136 #define PCI_DEVICE_ID_S3_SONICVIBES 0xca00
139 #define SV_MAGIC ((PCI_VENDOR_ID_S3<<16)|PCI_DEVICE_ID_S3_SONICVIBES)
141 #define SV_EXTENT_SB 0x10
142 #define SV_EXTENT_ENH 0x10
143 #define SV_EXTENT_SYNTH 0x4
144 #define SV_EXTENT_MIDI 0x4
145 #define SV_EXTENT_GAME 0x8
146 #define SV_EXTENT_DMA 0x10
149 * we are not a bridge and thus use a resource for DDMA that is used for bridges but
150 * left empty for normal devices
152 #define RESOURCE_SB 0
153 #define RESOURCE_ENH 1
154 #define RESOURCE_SYNTH 2
155 #define RESOURCE_MIDI 3
156 #define RESOURCE_GAME 4
157 #define RESOURCE_DDMA 7
159 #define SV_MIDI_DATA 0
160 #define SV_MIDI_COMMAND 1
161 #define SV_MIDI_STATUS 1
163 #define SV_DMA_ADDR0 0
164 #define SV_DMA_ADDR1 1
165 #define SV_DMA_ADDR2 2
166 #define SV_DMA_ADDR3 3
167 #define SV_DMA_COUNT0 4
168 #define SV_DMA_COUNT1 5
169 #define SV_DMA_COUNT2 6
170 #define SV_DMA_MODE 0xb
171 #define SV_DMA_RESET 0xd
172 #define SV_DMA_MASK 0xf
175 * DONT reset the DMA controllers unless you understand
176 * the reset semantics. Assuming reset semantics as in
177 * the 8237 does not work.
180 #define DMA_MODE_AUTOINIT 0x10
181 #define DMA_MODE_READ 0x44 /* I/O to memory, no autoinit, increment, single mode */
182 #define DMA_MODE_WRITE 0x48 /* memory to I/O, no autoinit, increment, single mode */
184 #define SV_CODEC_CONTROL 0
185 #define SV_CODEC_INTMASK 1
186 #define SV_CODEC_STATUS 2
187 #define SV_CODEC_IADDR 4
188 #define SV_CODEC_IDATA 5
190 #define SV_CCTRL_RESET 0x80
191 #define SV_CCTRL_INTADRIVE 0x20
192 #define SV_CCTRL_WAVETABLE 0x08
193 #define SV_CCTRL_REVERB 0x04
194 #define SV_CCTRL_ENHANCED 0x01
196 #define SV_CINTMASK_DMAA 0x01
197 #define SV_CINTMASK_DMAC 0x04
198 #define SV_CINTMASK_SPECIAL 0x08
199 #define SV_CINTMASK_UPDOWN 0x40
200 #define SV_CINTMASK_MIDI 0x80
202 #define SV_CSTAT_DMAA 0x01
203 #define SV_CSTAT_DMAC 0x04
204 #define SV_CSTAT_SPECIAL 0x08
205 #define SV_CSTAT_UPDOWN 0x40
206 #define SV_CSTAT_MIDI 0x80
208 #define SV_CIADDR_TRD 0x80
209 #define SV_CIADDR_MCE 0x40
211 /* codec indirect registers */
212 #define SV_CIMIX_ADCINL 0x00
213 #define SV_CIMIX_ADCINR 0x01
214 #define SV_CIMIX_AUX1INL 0x02
215 #define SV_CIMIX_AUX1INR 0x03
216 #define SV_CIMIX_CDINL 0x04
217 #define SV_CIMIX_CDINR 0x05
218 #define SV_CIMIX_LINEINL 0x06
219 #define SV_CIMIX_LINEINR 0x07
220 #define SV_CIMIX_MICIN 0x08
221 #define SV_CIMIX_SYNTHINL 0x0A
222 #define SV_CIMIX_SYNTHINR 0x0B
223 #define SV_CIMIX_AUX2INL 0x0C
224 #define SV_CIMIX_AUX2INR 0x0D
225 #define SV_CIMIX_ANALOGINL 0x0E
226 #define SV_CIMIX_ANALOGINR 0x0F
227 #define SV_CIMIX_PCMINL 0x10
228 #define SV_CIMIX_PCMINR 0x11
230 #define SV_CIGAMECONTROL 0x09
231 #define SV_CIDATAFMT 0x12
232 #define SV_CIENABLE 0x13
233 #define SV_CIUPDOWN 0x14
234 #define SV_CIREVISION 0x15
235 #define SV_CIADCOUTPUT 0x16
236 #define SV_CIDMAABASECOUNT1 0x18
237 #define SV_CIDMAABASECOUNT0 0x19
238 #define SV_CIDMACBASECOUNT1 0x1c
239 #define SV_CIDMACBASECOUNT0 0x1d
240 #define SV_CIPCMSR0 0x1e
241 #define SV_CIPCMSR1 0x1f
242 #define SV_CISYNTHSR0 0x20
243 #define SV_CISYNTHSR1 0x21
244 #define SV_CIADCCLKSOURCE 0x22
245 #define SV_CIADCALTSR 0x23
246 #define SV_CIADCPLLM 0x24
247 #define SV_CIADCPLLN 0x25
248 #define SV_CISYNTHPLLM 0x26
249 #define SV_CISYNTHPLLN 0x27
250 #define SV_CIUARTCONTROL 0x2a
251 #define SV_CIDRIVECONTROL 0x2b
252 #define SV_CISRSSPACE 0x2c
253 #define SV_CISRSCENTER 0x2d
254 #define SV_CIWAVETABLESRC 0x2e
255 #define SV_CIANALOGPWRDOWN 0x30
256 #define SV_CIDIGITALPWRDOWN 0x31
259 #define SV_CIMIX_ADCSRC_CD 0x20
260 #define SV_CIMIX_ADCSRC_DAC 0x40
261 #define SV_CIMIX_ADCSRC_AUX2 0x60
262 #define SV_CIMIX_ADCSRC_LINE 0x80
263 #define SV_CIMIX_ADCSRC_AUX1 0xa0
264 #define SV_CIMIX_ADCSRC_MIC 0xc0
265 #define SV_CIMIX_ADCSRC_MIXOUT 0xe0
266 #define SV_CIMIX_ADCSRC_MASK 0xe0
268 #define SV_CFMT_STEREO 0x01
269 #define SV_CFMT_16BIT 0x02
270 #define SV_CFMT_MASK 0x03
271 #define SV_CFMT_ASHIFT 0
272 #define SV_CFMT_CSHIFT 4
274 static const unsigned sample_size[] = { 1, 2, 2, 4 };
275 static const unsigned sample_shift[] = { 0, 1, 1, 2 };
277 #define SV_CENABLE_PPE 0x4
278 #define SV_CENABLE_RE 0x2
279 #define SV_CENABLE_PE 0x1
282 /* MIDI buffer sizes */
284 #define MIDIINBUF 256
285 #define MIDIOUTBUF 256
287 #define FMODE_MIDI_SHIFT 2
288 #define FMODE_MIDI_READ (FMODE_READ << FMODE_MIDI_SHIFT)
289 #define FMODE_MIDI_WRITE (FMODE_WRITE << FMODE_MIDI_SHIFT)
291 #define FMODE_DMFM 0x10
293 /* --------------------------------------------------------------------- */
299 /* list of sonicvibes devices */
300 struct list_head devs;
302 /* the corresponding pci_dev structure */
305 /* soundcore stuff */
311 /* hardware resources */
312 unsigned long iosb, ioenh, iosynth, iomidi; /* long for SPARC */
313 unsigned int iodmaa, iodmac, irq;
318 #ifndef OSS_DOCUMENTED_MIXER_SEMANTICS
319 unsigned short vol[13];
320 #endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
324 unsigned int rateadc, ratedac;
325 unsigned char fmt, enable;
328 struct semaphore open_sem;
330 wait_queue_head_t open_wait;
338 unsigned hwptr, swptr;
339 unsigned total_bytes;
341 unsigned error; /* over/underrun */
342 wait_queue_head_t wait;
343 /* redundant, but makes calculations easier */
346 unsigned fragsamples;
350 unsigned endcleared:1;
352 unsigned ossfragshift;
354 unsigned subdivision;
359 unsigned ird, iwr, icnt;
360 unsigned ord, owr, ocnt;
361 wait_queue_head_t iwait;
362 wait_queue_head_t owait;
363 struct timer_list timer;
364 unsigned char ibuf[MIDIINBUF];
365 unsigned char obuf[MIDIOUTBUF];
368 struct gameport *gameport;
371 /* --------------------------------------------------------------------- */
373 static LIST_HEAD(devs);
374 static unsigned long wavetable_mem;
376 /* --------------------------------------------------------------------- */
378 static inline unsigned ld2(unsigned int x)
404 * hweightN: returns the hamming weight (i.e. the number
405 * of bits set) of a N-bit word
412 static inline unsigned int hweight32(unsigned int w)
414 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
415 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
416 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
417 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
418 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
421 /* --------------------------------------------------------------------- */
424 * Why use byte IO? Nobody knows, but S3 does it also in their Windows driver.
429 static void set_dmaa(struct sv_state *s, unsigned int addr, unsigned int count)
432 unsigned io = s->iodmaa, u;
435 for (u = 4; u > 0; u--, addr >>= 8, io++)
436 outb(addr & 0xff, io);
437 for (u = 3; u > 0; u--, count >>= 8, io++)
438 outb(count & 0xff, io);
439 #else /* DMABYTEIO */
441 outl(addr, s->iodmaa + SV_DMA_ADDR0);
442 outl(count, s->iodmaa + SV_DMA_COUNT0);
443 #endif /* DMABYTEIO */
444 outb(0x18, s->iodmaa + SV_DMA_MODE);
447 static void set_dmac(struct sv_state *s, unsigned int addr, unsigned int count)
450 unsigned io = s->iodmac, u;
454 for (u = 4; u > 0; u--, addr >>= 8, io++)
455 outb(addr & 0xff, io);
456 for (u = 3; u > 0; u--, count >>= 8, io++)
457 outb(count & 0xff, io);
458 #else /* DMABYTEIO */
461 outl(addr, s->iodmac + SV_DMA_ADDR0);
462 outl(count, s->iodmac + SV_DMA_COUNT0);
463 #endif /* DMABYTEIO */
464 outb(0x14, s->iodmac + SV_DMA_MODE);
467 static inline unsigned get_dmaa(struct sv_state *s)
470 unsigned io = s->iodmaa+6, v = 0, u;
472 for (u = 3; u > 0; u--, io--) {
477 #else /* DMABYTEIO */
478 return (inl(s->iodmaa + SV_DMA_COUNT0) & 0xffffff) + 1;
479 #endif /* DMABYTEIO */
482 static inline unsigned get_dmac(struct sv_state *s)
485 unsigned io = s->iodmac+6, v = 0, u;
487 for (u = 3; u > 0; u--, io--) {
492 #else /* DMABYTEIO */
493 return ((inl(s->iodmac + SV_DMA_COUNT0) & 0xffffff) + 1) << 1;
494 #endif /* DMABYTEIO */
497 static void wrindir(struct sv_state *s, unsigned char idx, unsigned char data)
499 outb(idx & 0x3f, s->ioenh + SV_CODEC_IADDR);
501 outb(data, s->ioenh + SV_CODEC_IDATA);
505 static unsigned char rdindir(struct sv_state *s, unsigned char idx)
509 outb(idx & 0x3f, s->ioenh + SV_CODEC_IADDR);
511 v = inb(s->ioenh + SV_CODEC_IDATA);
516 static void set_fmt(struct sv_state *s, unsigned char mask, unsigned char data)
520 spin_lock_irqsave(&s->lock, flags);
521 outb(SV_CIDATAFMT | SV_CIADDR_MCE, s->ioenh + SV_CODEC_IADDR);
523 s->fmt = inb(s->ioenh + SV_CODEC_IDATA);
526 s->fmt = (s->fmt & mask) | data;
527 outb(s->fmt, s->ioenh + SV_CODEC_IDATA);
529 outb(0, s->ioenh + SV_CODEC_IADDR);
530 spin_unlock_irqrestore(&s->lock, flags);
534 static void frobindir(struct sv_state *s, unsigned char idx, unsigned char mask, unsigned char data)
536 outb(idx & 0x3f, s->ioenh + SV_CODEC_IADDR);
538 outb((inb(s->ioenh + SV_CODEC_IDATA) & mask) ^ data, s->ioenh + SV_CODEC_IDATA);
542 #define REFFREQUENCY 24576000
544 #define FULLRATE 48000
546 static unsigned setpll(struct sv_state *s, unsigned char reg, unsigned rate)
549 unsigned char r, m=0, n=0;
550 unsigned xm, xn, xr, xd, metric = ~0U;
551 /* the warnings about m and n used uninitialized are bogus and may safely be ignored */
553 if (rate < 625000/ADCMULT)
554 rate = 625000/ADCMULT;
555 if (rate > 150000000/ADCMULT)
556 rate = 150000000/ADCMULT;
557 /* slight violation of specs, needed for continuous sampling rates */
558 for (r = 0; rate < 75000000/ADCMULT; r += 0x20, rate <<= 1);
559 for (xn = 3; xn < 35; xn++)
560 for (xm = 3; xm < 130; xm++) {
561 xr = REFFREQUENCY/ADCMULT * xm / xn;
562 xd = abs((signed)(xr - rate));
570 spin_lock_irqsave(&s->lock, flags);
571 outb(reg, s->ioenh + SV_CODEC_IADDR);
573 outb(m, s->ioenh + SV_CODEC_IDATA);
575 outb(reg+1, s->ioenh + SV_CODEC_IADDR);
577 outb(r | n, s->ioenh + SV_CODEC_IDATA);
578 spin_unlock_irqrestore(&s->lock, flags);
580 return (REFFREQUENCY/ADCMULT * (m + 2) / (n + 2)) >> ((r >> 5) & 7);
585 static unsigned getpll(struct sv_state *s, unsigned char reg)
591 spin_lock_irqsave(&s->lock, flags);
592 outb(reg, s->ioenh + SV_CODEC_IADDR);
594 m = inb(s->ioenh + SV_CODEC_IDATA);
596 outb(reg+1, s->ioenh + SV_CODEC_IADDR);
598 n = inb(s->ioenh + SV_CODEC_IDATA);
599 spin_unlock_irqrestore(&s->lock, flags);
601 return (REFFREQUENCY/ADCMULT * (m + 2) / ((n & 0x1f) + 2)) >> ((n >> 5) & 7);
606 static void set_dac_rate(struct sv_state *s, unsigned rate)
615 div = (rate * 65536 + FULLRATE/2) / FULLRATE;
618 spin_lock_irqsave(&s->lock, flags);
619 wrindir(s, SV_CIPCMSR1, div >> 8);
620 wrindir(s, SV_CIPCMSR0, div);
621 spin_unlock_irqrestore(&s->lock, flags);
622 s->ratedac = (div * FULLRATE + 32768) / 65536;
625 static void set_adc_rate(struct sv_state *s, unsigned rate)
628 unsigned rate1, rate2, div;
634 rate1 = setpll(s, SV_CIADCPLLM, rate);
635 div = (48000 + rate/2) / rate;
638 rate2 = (48000 + div/2) / div;
639 spin_lock_irqsave(&s->lock, flags);
640 wrindir(s, SV_CIADCALTSR, (div-1) << 4);
641 if (abs((signed)(rate-rate2)) <= abs((signed)(rate-rate1))) {
642 wrindir(s, SV_CIADCCLKSOURCE, 0x10);
645 wrindir(s, SV_CIADCCLKSOURCE, 0x00);
648 spin_unlock_irqrestore(&s->lock, flags);
651 /* --------------------------------------------------------------------- */
653 static inline void stop_adc(struct sv_state *s)
657 spin_lock_irqsave(&s->lock, flags);
658 s->enable &= ~SV_CENABLE_RE;
659 wrindir(s, SV_CIENABLE, s->enable);
660 spin_unlock_irqrestore(&s->lock, flags);
663 static inline void stop_dac(struct sv_state *s)
667 spin_lock_irqsave(&s->lock, flags);
668 s->enable &= ~(SV_CENABLE_PPE | SV_CENABLE_PE);
669 wrindir(s, SV_CIENABLE, s->enable);
670 spin_unlock_irqrestore(&s->lock, flags);
673 static void start_dac(struct sv_state *s)
677 spin_lock_irqsave(&s->lock, flags);
678 if ((s->dma_dac.mapped || s->dma_dac.count > 0) && s->dma_dac.ready) {
679 s->enable = (s->enable & ~SV_CENABLE_PPE) | SV_CENABLE_PE;
680 wrindir(s, SV_CIENABLE, s->enable);
682 spin_unlock_irqrestore(&s->lock, flags);
685 static void start_adc(struct sv_state *s)
689 spin_lock_irqsave(&s->lock, flags);
690 if ((s->dma_adc.mapped || s->dma_adc.count < (signed)(s->dma_adc.dmasize - 2*s->dma_adc.fragsize))
691 && s->dma_adc.ready) {
692 s->enable |= SV_CENABLE_RE;
693 wrindir(s, SV_CIENABLE, s->enable);
695 spin_unlock_irqrestore(&s->lock, flags);
698 /* --------------------------------------------------------------------- */
700 #define DMABUF_DEFAULTORDER (17-PAGE_SHIFT)
701 #define DMABUF_MINORDER 1
703 static void dealloc_dmabuf(struct sv_state *s, struct dmabuf *db)
705 struct page *page, *pend;
708 /* undo marking the pages as reserved */
709 pend = virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
710 for (page = virt_to_page(db->rawbuf); page <= pend; page++)
711 ClearPageReserved(page);
712 pci_free_consistent(s->dev, PAGE_SIZE << db->buforder, db->rawbuf, db->dmaaddr);
715 db->mapped = db->ready = 0;
719 /* DMAA is used for playback, DMAC is used for recording */
721 static int prog_dmabuf(struct sv_state *s, unsigned rec)
723 struct dmabuf *db = rec ? &s->dma_adc : &s->dma_dac;
724 unsigned rate = rec ? s->rateadc : s->ratedac;
728 struct page *page, *pend;
732 spin_lock_irqsave(&s->lock, flags);
735 s->enable &= ~SV_CENABLE_RE;
736 fmt >>= SV_CFMT_CSHIFT;
738 s->enable &= ~SV_CENABLE_PE;
739 fmt >>= SV_CFMT_ASHIFT;
741 wrindir(s, SV_CIENABLE, s->enable);
742 spin_unlock_irqrestore(&s->lock, flags);
744 db->hwptr = db->swptr = db->total_bytes = db->count = db->error = db->endcleared = 0;
746 db->ready = db->mapped = 0;
747 for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--)
748 if ((db->rawbuf = pci_alloc_consistent(s->dev, PAGE_SIZE << order, &db->dmaaddr)))
752 db->buforder = order;
753 if ((virt_to_bus(db->rawbuf) ^ (virt_to_bus(db->rawbuf) + (PAGE_SIZE << db->buforder) - 1)) & ~0xffff)
754 printk(KERN_DEBUG "sv: DMA buffer crosses 64k boundary: busaddr 0x%lx size %ld\n",
755 virt_to_bus(db->rawbuf), PAGE_SIZE << db->buforder);
756 if ((virt_to_bus(db->rawbuf) + (PAGE_SIZE << db->buforder) - 1) & ~0xffffff)
757 printk(KERN_DEBUG "sv: DMA buffer beyond 16MB: busaddr 0x%lx size %ld\n",
758 virt_to_bus(db->rawbuf), PAGE_SIZE << db->buforder);
759 /* now mark the pages as reserved; otherwise remap_pfn_range doesn't do what we want */
760 pend = virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
761 for (page = virt_to_page(db->rawbuf); page <= pend; page++)
762 SetPageReserved(page);
764 bytepersec = rate << sample_shift[fmt];
765 bufs = PAGE_SIZE << db->buforder;
766 if (db->ossfragshift) {
767 if ((1000 << db->ossfragshift) < bytepersec)
768 db->fragshift = ld2(bytepersec/1000);
770 db->fragshift = db->ossfragshift;
772 db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1));
773 if (db->fragshift < 3)
776 db->numfrag = bufs >> db->fragshift;
777 while (db->numfrag < 4 && db->fragshift > 3) {
779 db->numfrag = bufs >> db->fragshift;
781 db->fragsize = 1 << db->fragshift;
782 if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
783 db->numfrag = db->ossmaxfrags;
784 db->fragsamples = db->fragsize >> sample_shift[fmt];
785 db->dmasize = db->numfrag << db->fragshift;
786 memset(db->rawbuf, (fmt & SV_CFMT_16BIT) ? 0 : 0x80, db->dmasize);
787 spin_lock_irqsave(&s->lock, flags);
789 set_dmac(s, db->dmaaddr, db->numfrag << db->fragshift);
790 /* program enhanced mode registers */
791 wrindir(s, SV_CIDMACBASECOUNT1, (db->fragsamples-1) >> 8);
792 wrindir(s, SV_CIDMACBASECOUNT0, db->fragsamples-1);
794 set_dmaa(s, db->dmaaddr, db->numfrag << db->fragshift);
795 /* program enhanced mode registers */
796 wrindir(s, SV_CIDMAABASECOUNT1, (db->fragsamples-1) >> 8);
797 wrindir(s, SV_CIDMAABASECOUNT0, db->fragsamples-1);
799 spin_unlock_irqrestore(&s->lock, flags);
805 static inline void clear_advance(struct sv_state *s)
807 unsigned char c = (s->fmt & (SV_CFMT_16BIT << SV_CFMT_ASHIFT)) ? 0 : 0x80;
808 unsigned char *buf = s->dma_dac.rawbuf;
809 unsigned bsize = s->dma_dac.dmasize;
810 unsigned bptr = s->dma_dac.swptr;
811 unsigned len = s->dma_dac.fragsize;
813 if (bptr + len > bsize) {
814 unsigned x = bsize - bptr;
815 memset(buf + bptr, c, x);
819 memset(buf + bptr, c, len);
822 /* call with spinlock held! */
823 static void sv_update_ptr(struct sv_state *s)
828 /* update ADC pointer */
829 if (s->dma_adc.ready) {
830 hwptr = (s->dma_adc.dmasize - get_dmac(s)) % s->dma_adc.dmasize;
831 diff = (s->dma_adc.dmasize + hwptr - s->dma_adc.hwptr) % s->dma_adc.dmasize;
832 s->dma_adc.hwptr = hwptr;
833 s->dma_adc.total_bytes += diff;
834 s->dma_adc.count += diff;
835 if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
836 wake_up(&s->dma_adc.wait);
837 if (!s->dma_adc.mapped) {
838 if (s->dma_adc.count > (signed)(s->dma_adc.dmasize - ((3 * s->dma_adc.fragsize) >> 1))) {
839 s->enable &= ~SV_CENABLE_RE;
840 wrindir(s, SV_CIENABLE, s->enable);
845 /* update DAC pointer */
846 if (s->dma_dac.ready) {
847 hwptr = (s->dma_dac.dmasize - get_dmaa(s)) % s->dma_dac.dmasize;
848 diff = (s->dma_dac.dmasize + hwptr - s->dma_dac.hwptr) % s->dma_dac.dmasize;
849 s->dma_dac.hwptr = hwptr;
850 s->dma_dac.total_bytes += diff;
851 if (s->dma_dac.mapped) {
852 s->dma_dac.count += diff;
853 if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
854 wake_up(&s->dma_dac.wait);
856 s->dma_dac.count -= diff;
857 if (s->dma_dac.count <= 0) {
858 s->enable &= ~SV_CENABLE_PE;
859 wrindir(s, SV_CIENABLE, s->enable);
861 } else if (s->dma_dac.count <= (signed)s->dma_dac.fragsize && !s->dma_dac.endcleared) {
863 s->dma_dac.endcleared = 1;
865 if (s->dma_dac.count + (signed)s->dma_dac.fragsize <= (signed)s->dma_dac.dmasize)
866 wake_up(&s->dma_dac.wait);
871 /* hold spinlock for the following! */
872 static void sv_handle_midi(struct sv_state *s)
878 while (!(inb(s->iomidi+1) & 0x80)) {
880 if (s->midi.icnt < MIDIINBUF) {
881 s->midi.ibuf[s->midi.iwr] = ch;
882 s->midi.iwr = (s->midi.iwr + 1) % MIDIINBUF;
888 wake_up(&s->midi.iwait);
890 while (!(inb(s->iomidi+1) & 0x40) && s->midi.ocnt > 0) {
891 outb(s->midi.obuf[s->midi.ord], s->iomidi);
892 s->midi.ord = (s->midi.ord + 1) % MIDIOUTBUF;
894 if (s->midi.ocnt < MIDIOUTBUF-16)
898 wake_up(&s->midi.owait);
901 static irqreturn_t sv_interrupt(int irq, void *dev_id, struct pt_regs *regs)
903 struct sv_state *s = (struct sv_state *)dev_id;
906 /* fastpath out, to ease interrupt sharing */
907 intsrc = inb(s->ioenh + SV_CODEC_STATUS);
908 if (!(intsrc & (SV_CSTAT_DMAA | SV_CSTAT_DMAC | SV_CSTAT_MIDI)))
913 spin_unlock(&s->lock);
917 static void sv_midi_timer(unsigned long data)
919 struct sv_state *s = (struct sv_state *)data;
922 spin_lock_irqsave(&s->lock, flags);
924 spin_unlock_irqrestore(&s->lock, flags);
925 s->midi.timer.expires = jiffies+1;
926 add_timer(&s->midi.timer);
929 /* --------------------------------------------------------------------- */
931 static const char invalid_magic[] = KERN_CRIT "sv: invalid magic value\n";
933 #define VALIDATE_STATE(s) \
935 if (!(s) || (s)->magic != SV_MAGIC) { \
936 printk(invalid_magic); \
941 /* --------------------------------------------------------------------- */
945 #define MT_4MUTEMONO 3
948 static const struct {
953 } mixtable[SOUND_MIXER_NRDEVICES] = {
954 [SOUND_MIXER_RECLEV] = { SV_CIMIX_ADCINL, SV_CIMIX_ADCINR, MT_4, 0 },
955 [SOUND_MIXER_LINE1] = { SV_CIMIX_AUX1INL, SV_CIMIX_AUX1INR, MT_5MUTE, 5 },
956 [SOUND_MIXER_CD] = { SV_CIMIX_CDINL, SV_CIMIX_CDINR, MT_5MUTE, 1 },
957 [SOUND_MIXER_LINE] = { SV_CIMIX_LINEINL, SV_CIMIX_LINEINR, MT_5MUTE, 4 },
958 [SOUND_MIXER_MIC] = { SV_CIMIX_MICIN, SV_CIMIX_ADCINL, MT_4MUTEMONO, 6 },
959 [SOUND_MIXER_SYNTH] = { SV_CIMIX_SYNTHINL, SV_CIMIX_SYNTHINR, MT_5MUTE, 2 },
960 [SOUND_MIXER_LINE2] = { SV_CIMIX_AUX2INL, SV_CIMIX_AUX2INR, MT_5MUTE, 3 },
961 [SOUND_MIXER_VOLUME] = { SV_CIMIX_ANALOGINL, SV_CIMIX_ANALOGINR, MT_5MUTE, 7 },
962 [SOUND_MIXER_PCM] = { SV_CIMIX_PCMINL, SV_CIMIX_PCMINR, MT_6MUTE, 0 }
965 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
967 static int return_mixval(struct sv_state *s, unsigned i, int *arg)
970 unsigned char l, r, rl, rr;
972 spin_lock_irqsave(&s->lock, flags);
973 l = rdindir(s, mixtable[i].left);
974 r = rdindir(s, mixtable[i].right);
975 spin_unlock_irqrestore(&s->lock, flags);
976 switch (mixtable[i].type) {
980 rl = 10 + 6 * (l & 15);
981 rr = 10 + 6 * (r & 15);
985 rl = 55 - 3 * (l & 15);
994 rl = 100 - 3 * (l & 31);
995 rr = 100 - 3 * (r & 31);
999 rl = 100 - 3 * (l & 63) / 2;
1000 rr = 100 - 3 * (r & 63) / 2;
1007 return put_user((rr << 8) | rl, arg);
1010 #else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
1012 static const unsigned char volidx[SOUND_MIXER_NRDEVICES] =
1014 [SOUND_MIXER_RECLEV] = 1,
1015 [SOUND_MIXER_LINE1] = 2,
1016 [SOUND_MIXER_CD] = 3,
1017 [SOUND_MIXER_LINE] = 4,
1018 [SOUND_MIXER_MIC] = 5,
1019 [SOUND_MIXER_SYNTH] = 6,
1020 [SOUND_MIXER_LINE2] = 7,
1021 [SOUND_MIXER_VOLUME] = 8,
1022 [SOUND_MIXER_PCM] = 9
1025 #endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
1027 static unsigned mixer_recmask(struct sv_state *s)
1029 unsigned long flags;
1032 spin_lock_irqsave(&s->lock, flags);
1033 j = rdindir(s, SV_CIMIX_ADCINL) >> 5;
1034 spin_unlock_irqrestore(&s->lock, flags);
1036 for (i = 0; i < SOUND_MIXER_NRDEVICES && mixtable[i].rec != j; i++);
1040 static int mixer_ioctl(struct sv_state *s, unsigned int cmd, unsigned long arg)
1042 unsigned long flags;
1044 unsigned char l, r, rl, rr;
1045 int __user *p = (int __user *)arg;
1048 if (cmd == SOUND_MIXER_INFO) {
1050 memset(&info, 0, sizeof(info));
1051 strlcpy(info.id, "SonicVibes", sizeof(info.id));
1052 strlcpy(info.name, "S3 SonicVibes", sizeof(info.name));
1053 info.modify_counter = s->mix.modcnt;
1054 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
1058 if (cmd == SOUND_OLD_MIXER_INFO) {
1059 _old_mixer_info info;
1060 memset(&info, 0, sizeof(info));
1061 strlcpy(info.id, "SonicVibes", sizeof(info.id));
1062 strlcpy(info.name, "S3 SonicVibes", sizeof(info.name));
1063 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
1067 if (cmd == OSS_GETVERSION)
1068 return put_user(SOUND_VERSION, p);
1069 if (cmd == SOUND_MIXER_PRIVATE1) { /* SRS settings */
1070 if (get_user(val, p))
1072 spin_lock_irqsave(&s->lock, flags);
1075 l = 4 - ((val >> 2) & 7);
1078 r = 4 - ((val >> 5) & 7);
1081 wrindir(s, SV_CISRSSPACE, l);
1082 wrindir(s, SV_CISRSCENTER, r);
1084 wrindir(s, SV_CISRSSPACE, 0x80);
1086 l = rdindir(s, SV_CISRSSPACE);
1087 r = rdindir(s, SV_CISRSCENTER);
1088 spin_unlock_irqrestore(&s->lock, flags);
1090 return put_user(0, p);
1091 return put_user(((4 - (l & 7)) << 2) | ((4 - (r & 7)) << 5) | 2, p);
1093 if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
1095 if (_SIOC_DIR(cmd) == _SIOC_READ) {
1096 switch (_IOC_NR(cmd)) {
1097 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
1098 return put_user(mixer_recmask(s), p);
1100 case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */
1101 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1102 if (mixtable[i].type)
1104 return put_user(val, p);
1106 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
1107 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1108 if (mixtable[i].rec)
1110 return put_user(val, p);
1112 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
1113 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1114 if (mixtable[i].type && mixtable[i].type != MT_4MUTEMONO)
1116 return put_user(val, p);
1118 case SOUND_MIXER_CAPS:
1119 return put_user(SOUND_CAP_EXCL_INPUT, p);
1123 if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
1125 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1126 return return_mixval(s, i, p);
1127 #else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
1130 return put_user(s->mix.vol[volidx[i]-1], p);
1131 #endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
1134 if (_SIOC_DIR(cmd) != (_SIOC_READ|_SIOC_WRITE))
1137 switch (_IOC_NR(cmd)) {
1138 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
1139 if (get_user(val, p))
1143 return 0; /*val = mixer_recmask(s);*/
1145 val &= ~mixer_recmask(s);
1146 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1147 if (!(val & (1 << i)))
1149 if (mixtable[i].rec)
1152 if (i == SOUND_MIXER_NRDEVICES)
1154 spin_lock_irqsave(&s->lock, flags);
1155 frobindir(s, SV_CIMIX_ADCINL, 0x1f, mixtable[i].rec << 5);
1156 frobindir(s, SV_CIMIX_ADCINR, 0x1f, mixtable[i].rec << 5);
1157 spin_unlock_irqrestore(&s->lock, flags);
1162 if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
1164 if (get_user(val, p))
1167 r = (val >> 8) & 0xff;
1168 if (mixtable[i].type == MT_4MUTEMONO)
1174 spin_lock_irqsave(&s->lock, flags);
1175 switch (mixtable[i].type) {
1181 frobindir(s, mixtable[i].left, 0xf0, l / 6);
1182 frobindir(s, mixtable[i].right, 0xf0, l / 6);
1196 wrindir(s, mixtable[i].left, rl);
1197 frobindir(s, mixtable[i].right, ~0x10, rr);
1209 wrindir(s, mixtable[i].left, rl);
1210 wrindir(s, mixtable[i].right, rr);
1217 rl = (100 - l) * 2 / 3;
1221 rr = (100 - r) * 2 / 3;
1222 wrindir(s, mixtable[i].left, rl);
1223 wrindir(s, mixtable[i].right, rr);
1226 spin_unlock_irqrestore(&s->lock, flags);
1227 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1228 return return_mixval(s, i, p);
1229 #else /* OSS_DOCUMENTED_MIXER_SEMANTICS */
1232 s->mix.vol[volidx[i]-1] = val;
1233 return put_user(s->mix.vol[volidx[i]-1], p);
1234 #endif /* OSS_DOCUMENTED_MIXER_SEMANTICS */
1238 /* --------------------------------------------------------------------- */
1240 static int sv_open_mixdev(struct inode *inode, struct file *file)
1242 int minor = iminor(inode);
1243 struct list_head *list;
1246 for (list = devs.next; ; list = list->next) {
1249 s = list_entry(list, struct sv_state, devs);
1250 if (s->dev_mixer == minor)
1254 file->private_data = s;
1255 return nonseekable_open(inode, file);
1258 static int sv_release_mixdev(struct inode *inode, struct file *file)
1260 struct sv_state *s = (struct sv_state *)file->private_data;
1266 static int sv_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1268 return mixer_ioctl((struct sv_state *)file->private_data, cmd, arg);
1271 static /*const*/ struct file_operations sv_mixer_fops = {
1272 .owner = THIS_MODULE,
1273 .llseek = no_llseek,
1274 .ioctl = sv_ioctl_mixdev,
1275 .open = sv_open_mixdev,
1276 .release = sv_release_mixdev,
1279 /* --------------------------------------------------------------------- */
1281 static int drain_dac(struct sv_state *s, int nonblock)
1283 DECLARE_WAITQUEUE(wait, current);
1284 unsigned long flags;
1287 if (s->dma_dac.mapped || !s->dma_dac.ready)
1289 add_wait_queue(&s->dma_dac.wait, &wait);
1291 __set_current_state(TASK_INTERRUPTIBLE);
1292 spin_lock_irqsave(&s->lock, flags);
1293 count = s->dma_dac.count;
1294 spin_unlock_irqrestore(&s->lock, flags);
1297 if (signal_pending(current))
1300 remove_wait_queue(&s->dma_dac.wait, &wait);
1301 set_current_state(TASK_RUNNING);
1304 tmo = 3 * HZ * (count + s->dma_dac.fragsize) / 2 / s->ratedac;
1305 tmo >>= sample_shift[(s->fmt >> SV_CFMT_ASHIFT) & SV_CFMT_MASK];
1306 if (!schedule_timeout(tmo + 1))
1307 printk(KERN_DEBUG "sv: dma timed out??\n");
1309 remove_wait_queue(&s->dma_dac.wait, &wait);
1310 set_current_state(TASK_RUNNING);
1311 if (signal_pending(current))
1312 return -ERESTARTSYS;
1316 /* --------------------------------------------------------------------- */
1318 static ssize_t sv_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
1320 struct sv_state *s = (struct sv_state *)file->private_data;
1321 DECLARE_WAITQUEUE(wait, current);
1323 unsigned long flags;
1328 if (s->dma_adc.mapped)
1330 if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
1332 if (!access_ok(VERIFY_WRITE, buffer, count))
1336 spin_lock_irqsave(&s->lock, flags);
1338 spin_unlock_irqrestore(&s->lock, flags);
1340 add_wait_queue(&s->dma_adc.wait, &wait);
1342 spin_lock_irqsave(&s->lock, flags);
1343 swptr = s->dma_adc.swptr;
1344 cnt = s->dma_adc.dmasize-swptr;
1345 if (s->dma_adc.count < cnt)
1346 cnt = s->dma_adc.count;
1348 __set_current_state(TASK_INTERRUPTIBLE);
1349 spin_unlock_irqrestore(&s->lock, flags);
1353 if (s->dma_adc.enabled)
1355 if (file->f_flags & O_NONBLOCK) {
1360 if (!schedule_timeout(HZ)) {
1361 printk(KERN_DEBUG "sv: read: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1362 s->dma_adc.dmasize, s->dma_adc.fragsize, s->dma_adc.count,
1363 s->dma_adc.hwptr, s->dma_adc.swptr);
1365 spin_lock_irqsave(&s->lock, flags);
1366 set_dmac(s, virt_to_bus(s->dma_adc.rawbuf), s->dma_adc.numfrag << s->dma_adc.fragshift);
1367 /* program enhanced mode registers */
1368 wrindir(s, SV_CIDMACBASECOUNT1, (s->dma_adc.fragsamples-1) >> 8);
1369 wrindir(s, SV_CIDMACBASECOUNT0, s->dma_adc.fragsamples-1);
1370 s->dma_adc.count = s->dma_adc.hwptr = s->dma_adc.swptr = 0;
1371 spin_unlock_irqrestore(&s->lock, flags);
1373 if (signal_pending(current)) {
1380 if (copy_to_user(buffer, s->dma_adc.rawbuf + swptr, cnt)) {
1385 swptr = (swptr + cnt) % s->dma_adc.dmasize;
1386 spin_lock_irqsave(&s->lock, flags);
1387 s->dma_adc.swptr = swptr;
1388 s->dma_adc.count -= cnt;
1389 spin_unlock_irqrestore(&s->lock, flags);
1393 if (s->dma_adc.enabled)
1396 remove_wait_queue(&s->dma_adc.wait, &wait);
1397 set_current_state(TASK_RUNNING);
1401 static ssize_t sv_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
1403 struct sv_state *s = (struct sv_state *)file->private_data;
1404 DECLARE_WAITQUEUE(wait, current);
1406 unsigned long flags;
1411 if (s->dma_dac.mapped)
1413 if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
1415 if (!access_ok(VERIFY_READ, buffer, count))
1419 spin_lock_irqsave(&s->lock, flags);
1421 spin_unlock_irqrestore(&s->lock, flags);
1423 add_wait_queue(&s->dma_dac.wait, &wait);
1425 spin_lock_irqsave(&s->lock, flags);
1426 if (s->dma_dac.count < 0) {
1427 s->dma_dac.count = 0;
1428 s->dma_dac.swptr = s->dma_dac.hwptr;
1430 swptr = s->dma_dac.swptr;
1431 cnt = s->dma_dac.dmasize-swptr;
1432 if (s->dma_dac.count + cnt > s->dma_dac.dmasize)
1433 cnt = s->dma_dac.dmasize - s->dma_dac.count;
1435 __set_current_state(TASK_INTERRUPTIBLE);
1436 spin_unlock_irqrestore(&s->lock, flags);
1440 if (s->dma_dac.enabled)
1442 if (file->f_flags & O_NONBLOCK) {
1447 if (!schedule_timeout(HZ)) {
1448 printk(KERN_DEBUG "sv: write: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1449 s->dma_dac.dmasize, s->dma_dac.fragsize, s->dma_dac.count,
1450 s->dma_dac.hwptr, s->dma_dac.swptr);
1452 spin_lock_irqsave(&s->lock, flags);
1453 set_dmaa(s, virt_to_bus(s->dma_dac.rawbuf), s->dma_dac.numfrag << s->dma_dac.fragshift);
1454 /* program enhanced mode registers */
1455 wrindir(s, SV_CIDMAABASECOUNT1, (s->dma_dac.fragsamples-1) >> 8);
1456 wrindir(s, SV_CIDMAABASECOUNT0, s->dma_dac.fragsamples-1);
1457 s->dma_dac.count = s->dma_dac.hwptr = s->dma_dac.swptr = 0;
1458 spin_unlock_irqrestore(&s->lock, flags);
1460 if (signal_pending(current)) {
1467 if (copy_from_user(s->dma_dac.rawbuf + swptr, buffer, cnt)) {
1472 swptr = (swptr + cnt) % s->dma_dac.dmasize;
1473 spin_lock_irqsave(&s->lock, flags);
1474 s->dma_dac.swptr = swptr;
1475 s->dma_dac.count += cnt;
1476 s->dma_dac.endcleared = 0;
1477 spin_unlock_irqrestore(&s->lock, flags);
1481 if (s->dma_dac.enabled)
1484 remove_wait_queue(&s->dma_dac.wait, &wait);
1485 set_current_state(TASK_RUNNING);
1489 /* No kernel lock - we have our own spinlock */
1490 static unsigned int sv_poll(struct file *file, struct poll_table_struct *wait)
1492 struct sv_state *s = (struct sv_state *)file->private_data;
1493 unsigned long flags;
1494 unsigned int mask = 0;
1497 if (file->f_mode & FMODE_WRITE) {
1498 if (!s->dma_dac.ready && prog_dmabuf(s, 1))
1500 poll_wait(file, &s->dma_dac.wait, wait);
1502 if (file->f_mode & FMODE_READ) {
1503 if (!s->dma_adc.ready && prog_dmabuf(s, 0))
1505 poll_wait(file, &s->dma_adc.wait, wait);
1507 spin_lock_irqsave(&s->lock, flags);
1509 if (file->f_mode & FMODE_READ) {
1510 if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
1511 mask |= POLLIN | POLLRDNORM;
1513 if (file->f_mode & FMODE_WRITE) {
1514 if (s->dma_dac.mapped) {
1515 if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
1516 mask |= POLLOUT | POLLWRNORM;
1518 if ((signed)s->dma_dac.dmasize >= s->dma_dac.count + (signed)s->dma_dac.fragsize)
1519 mask |= POLLOUT | POLLWRNORM;
1522 spin_unlock_irqrestore(&s->lock, flags);
1526 static int sv_mmap(struct file *file, struct vm_area_struct *vma)
1528 struct sv_state *s = (struct sv_state *)file->private_data;
1535 if (vma->vm_flags & VM_WRITE) {
1536 if ((ret = prog_dmabuf(s, 1)) != 0)
1539 } else if (vma->vm_flags & VM_READ) {
1540 if ((ret = prog_dmabuf(s, 0)) != 0)
1546 if (vma->vm_pgoff != 0)
1548 size = vma->vm_end - vma->vm_start;
1549 if (size > (PAGE_SIZE << db->buforder))
1552 if (remap_pfn_range(vma, vma->vm_start,
1553 virt_to_phys(db->rawbuf) >> PAGE_SHIFT,
1554 size, vma->vm_page_prot))
1563 static int sv_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1565 struct sv_state *s = (struct sv_state *)file->private_data;
1566 unsigned long flags;
1567 audio_buf_info abinfo;
1570 int val, mapped, ret;
1571 unsigned char fmtm, fmtd;
1572 void __user *argp = (void __user *)arg;
1573 int __user *p = argp;
1576 mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
1577 ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
1579 case OSS_GETVERSION:
1580 return put_user(SOUND_VERSION, p);
1582 case SNDCTL_DSP_SYNC:
1583 if (file->f_mode & FMODE_WRITE)
1584 return drain_dac(s, 0/*file->f_flags & O_NONBLOCK*/);
1587 case SNDCTL_DSP_SETDUPLEX:
1590 case SNDCTL_DSP_GETCAPS:
1591 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP, p);
1593 case SNDCTL_DSP_RESET:
1594 if (file->f_mode & FMODE_WRITE) {
1596 synchronize_irq(s->irq);
1597 s->dma_dac.swptr = s->dma_dac.hwptr = s->dma_dac.count = s->dma_dac.total_bytes = 0;
1599 if (file->f_mode & FMODE_READ) {
1601 synchronize_irq(s->irq);
1602 s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes = 0;
1606 case SNDCTL_DSP_SPEED:
1607 if (get_user(val, p))
1610 if (file->f_mode & FMODE_READ) {
1612 s->dma_adc.ready = 0;
1613 set_adc_rate(s, val);
1615 if (file->f_mode & FMODE_WRITE) {
1617 s->dma_dac.ready = 0;
1618 set_dac_rate(s, val);
1621 return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
1623 case SNDCTL_DSP_STEREO:
1624 if (get_user(val, p))
1628 if (file->f_mode & FMODE_READ) {
1630 s->dma_adc.ready = 0;
1632 fmtd |= SV_CFMT_STEREO << SV_CFMT_CSHIFT;
1634 fmtm &= ~(SV_CFMT_STEREO << SV_CFMT_CSHIFT);
1636 if (file->f_mode & FMODE_WRITE) {
1638 s->dma_dac.ready = 0;
1640 fmtd |= SV_CFMT_STEREO << SV_CFMT_ASHIFT;
1642 fmtm &= ~(SV_CFMT_STEREO << SV_CFMT_ASHIFT);
1644 set_fmt(s, fmtm, fmtd);
1647 case SNDCTL_DSP_CHANNELS:
1648 if (get_user(val, p))
1653 if (file->f_mode & FMODE_READ) {
1655 s->dma_adc.ready = 0;
1657 fmtd |= SV_CFMT_STEREO << SV_CFMT_CSHIFT;
1659 fmtm &= ~(SV_CFMT_STEREO << SV_CFMT_CSHIFT);
1661 if (file->f_mode & FMODE_WRITE) {
1663 s->dma_dac.ready = 0;
1665 fmtd |= SV_CFMT_STEREO << SV_CFMT_ASHIFT;
1667 fmtm &= ~(SV_CFMT_STEREO << SV_CFMT_ASHIFT);
1669 set_fmt(s, fmtm, fmtd);
1671 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (SV_CFMT_STEREO << SV_CFMT_CSHIFT)
1672 : (SV_CFMT_STEREO << SV_CFMT_ASHIFT))) ? 2 : 1, p);
1674 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
1675 return put_user(AFMT_S16_LE|AFMT_U8, p);
1677 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
1678 if (get_user(val, p))
1680 if (val != AFMT_QUERY) {
1683 if (file->f_mode & FMODE_READ) {
1685 s->dma_adc.ready = 0;
1686 if (val == AFMT_S16_LE)
1687 fmtd |= SV_CFMT_16BIT << SV_CFMT_CSHIFT;
1689 fmtm &= ~(SV_CFMT_16BIT << SV_CFMT_CSHIFT);
1691 if (file->f_mode & FMODE_WRITE) {
1693 s->dma_dac.ready = 0;
1694 if (val == AFMT_S16_LE)
1695 fmtd |= SV_CFMT_16BIT << SV_CFMT_ASHIFT;
1697 fmtm &= ~(SV_CFMT_16BIT << SV_CFMT_ASHIFT);
1699 set_fmt(s, fmtm, fmtd);
1701 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (SV_CFMT_16BIT << SV_CFMT_CSHIFT)
1702 : (SV_CFMT_16BIT << SV_CFMT_ASHIFT))) ? AFMT_S16_LE : AFMT_U8, p);
1704 case SNDCTL_DSP_POST:
1707 case SNDCTL_DSP_GETTRIGGER:
1709 if (file->f_mode & FMODE_READ && s->enable & SV_CENABLE_RE)
1710 val |= PCM_ENABLE_INPUT;
1711 if (file->f_mode & FMODE_WRITE && s->enable & SV_CENABLE_PE)
1712 val |= PCM_ENABLE_OUTPUT;
1713 return put_user(val, p);
1715 case SNDCTL_DSP_SETTRIGGER:
1716 if (get_user(val, p))
1718 if (file->f_mode & FMODE_READ) {
1719 if (val & PCM_ENABLE_INPUT) {
1720 if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
1722 s->dma_adc.enabled = 1;
1725 s->dma_adc.enabled = 0;
1729 if (file->f_mode & FMODE_WRITE) {
1730 if (val & PCM_ENABLE_OUTPUT) {
1731 if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
1733 s->dma_dac.enabled = 1;
1736 s->dma_dac.enabled = 0;
1742 case SNDCTL_DSP_GETOSPACE:
1743 if (!(file->f_mode & FMODE_WRITE))
1745 if (!s->dma_dac.ready && (val = prog_dmabuf(s, 0)) != 0)
1747 spin_lock_irqsave(&s->lock, flags);
1749 abinfo.fragsize = s->dma_dac.fragsize;
1750 count = s->dma_dac.count;
1753 abinfo.bytes = s->dma_dac.dmasize - count;
1754 abinfo.fragstotal = s->dma_dac.numfrag;
1755 abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
1756 spin_unlock_irqrestore(&s->lock, flags);
1757 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
1759 case SNDCTL_DSP_GETISPACE:
1760 if (!(file->f_mode & FMODE_READ))
1762 if (!s->dma_adc.ready && (val = prog_dmabuf(s, 1)) != 0)
1764 spin_lock_irqsave(&s->lock, flags);
1766 abinfo.fragsize = s->dma_adc.fragsize;
1767 count = s->dma_adc.count;
1770 abinfo.bytes = count;
1771 abinfo.fragstotal = s->dma_adc.numfrag;
1772 abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;
1773 spin_unlock_irqrestore(&s->lock, flags);
1774 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
1776 case SNDCTL_DSP_NONBLOCK:
1777 file->f_flags |= O_NONBLOCK;
1780 case SNDCTL_DSP_GETODELAY:
1781 if (!(file->f_mode & FMODE_WRITE))
1783 if (!s->dma_dac.ready && (val = prog_dmabuf(s, 0)) != 0)
1785 spin_lock_irqsave(&s->lock, flags);
1787 count = s->dma_dac.count;
1788 spin_unlock_irqrestore(&s->lock, flags);
1791 return put_user(count, p);
1793 case SNDCTL_DSP_GETIPTR:
1794 if (!(file->f_mode & FMODE_READ))
1796 if (!s->dma_adc.ready && (val = prog_dmabuf(s, 1)) != 0)
1798 spin_lock_irqsave(&s->lock, flags);
1800 cinfo.bytes = s->dma_adc.total_bytes;
1801 count = s->dma_adc.count;
1804 cinfo.blocks = count >> s->dma_adc.fragshift;
1805 cinfo.ptr = s->dma_adc.hwptr;
1806 if (s->dma_adc.mapped)
1807 s->dma_adc.count &= s->dma_adc.fragsize-1;
1808 spin_unlock_irqrestore(&s->lock, flags);
1809 if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
1813 case SNDCTL_DSP_GETOPTR:
1814 if (!(file->f_mode & FMODE_WRITE))
1816 if (!s->dma_dac.ready && (val = prog_dmabuf(s, 0)) != 0)
1818 spin_lock_irqsave(&s->lock, flags);
1820 cinfo.bytes = s->dma_dac.total_bytes;
1821 count = s->dma_dac.count;
1824 cinfo.blocks = count >> s->dma_dac.fragshift;
1825 cinfo.ptr = s->dma_dac.hwptr;
1826 if (s->dma_dac.mapped)
1827 s->dma_dac.count &= s->dma_dac.fragsize-1;
1828 spin_unlock_irqrestore(&s->lock, flags);
1829 if (copy_to_user(argp, &cinfo, sizeof(cinfo)))
1833 case SNDCTL_DSP_GETBLKSIZE:
1834 if (file->f_mode & FMODE_WRITE) {
1835 if ((val = prog_dmabuf(s, 0)))
1837 return put_user(s->dma_dac.fragsize, p);
1839 if ((val = prog_dmabuf(s, 1)))
1841 return put_user(s->dma_adc.fragsize, p);
1843 case SNDCTL_DSP_SETFRAGMENT:
1844 if (get_user(val, p))
1846 if (file->f_mode & FMODE_READ) {
1847 s->dma_adc.ossfragshift = val & 0xffff;
1848 s->dma_adc.ossmaxfrags = (val >> 16) & 0xffff;
1849 if (s->dma_adc.ossfragshift < 4)
1850 s->dma_adc.ossfragshift = 4;
1851 if (s->dma_adc.ossfragshift > 15)
1852 s->dma_adc.ossfragshift = 15;
1853 if (s->dma_adc.ossmaxfrags < 4)
1854 s->dma_adc.ossmaxfrags = 4;
1856 if (file->f_mode & FMODE_WRITE) {
1857 s->dma_dac.ossfragshift = val & 0xffff;
1858 s->dma_dac.ossmaxfrags = (val >> 16) & 0xffff;
1859 if (s->dma_dac.ossfragshift < 4)
1860 s->dma_dac.ossfragshift = 4;
1861 if (s->dma_dac.ossfragshift > 15)
1862 s->dma_dac.ossfragshift = 15;
1863 if (s->dma_dac.ossmaxfrags < 4)
1864 s->dma_dac.ossmaxfrags = 4;
1868 case SNDCTL_DSP_SUBDIVIDE:
1869 if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
1870 (file->f_mode & FMODE_WRITE && s->dma_dac.subdivision))
1872 if (get_user(val, p))
1874 if (val != 1 && val != 2 && val != 4)
1876 if (file->f_mode & FMODE_READ)
1877 s->dma_adc.subdivision = val;
1878 if (file->f_mode & FMODE_WRITE)
1879 s->dma_dac.subdivision = val;
1882 case SOUND_PCM_READ_RATE:
1883 return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
1885 case SOUND_PCM_READ_CHANNELS:
1886 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (SV_CFMT_STEREO << SV_CFMT_CSHIFT)
1887 : (SV_CFMT_STEREO << SV_CFMT_ASHIFT))) ? 2 : 1, p);
1889 case SOUND_PCM_READ_BITS:
1890 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (SV_CFMT_16BIT << SV_CFMT_CSHIFT)
1891 : (SV_CFMT_16BIT << SV_CFMT_ASHIFT))) ? 16 : 8, p);
1893 case SOUND_PCM_WRITE_FILTER:
1894 case SNDCTL_DSP_SETSYNCRO:
1895 case SOUND_PCM_READ_FILTER:
1899 return mixer_ioctl(s, cmd, arg);
1902 static int sv_open(struct inode *inode, struct file *file)
1904 int minor = iminor(inode);
1905 DECLARE_WAITQUEUE(wait, current);
1906 unsigned char fmtm = ~0, fmts = 0;
1907 struct list_head *list;
1910 for (list = devs.next; ; list = list->next) {
1913 s = list_entry(list, struct sv_state, devs);
1914 if (!((s->dev_audio ^ minor) & ~0xf))
1918 file->private_data = s;
1919 /* wait for device to become free */
1921 while (s->open_mode & file->f_mode) {
1922 if (file->f_flags & O_NONBLOCK) {
1926 add_wait_queue(&s->open_wait, &wait);
1927 __set_current_state(TASK_INTERRUPTIBLE);
1930 remove_wait_queue(&s->open_wait, &wait);
1931 set_current_state(TASK_RUNNING);
1932 if (signal_pending(current))
1933 return -ERESTARTSYS;
1936 if (file->f_mode & FMODE_READ) {
1937 fmtm &= ~((SV_CFMT_STEREO | SV_CFMT_16BIT) << SV_CFMT_CSHIFT);
1938 if ((minor & 0xf) == SND_DEV_DSP16)
1939 fmts |= SV_CFMT_16BIT << SV_CFMT_CSHIFT;
1940 s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags = s->dma_adc.subdivision = 0;
1941 s->dma_adc.enabled = 1;
1942 set_adc_rate(s, 8000);
1944 if (file->f_mode & FMODE_WRITE) {
1945 fmtm &= ~((SV_CFMT_STEREO | SV_CFMT_16BIT) << SV_CFMT_ASHIFT);
1946 if ((minor & 0xf) == SND_DEV_DSP16)
1947 fmts |= SV_CFMT_16BIT << SV_CFMT_ASHIFT;
1948 s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags = s->dma_dac.subdivision = 0;
1949 s->dma_dac.enabled = 1;
1950 set_dac_rate(s, 8000);
1952 set_fmt(s, fmtm, fmts);
1953 s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
1955 return nonseekable_open(inode, file);
1958 static int sv_release(struct inode *inode, struct file *file)
1960 struct sv_state *s = (struct sv_state *)file->private_data;
1964 if (file->f_mode & FMODE_WRITE)
1965 drain_dac(s, file->f_flags & O_NONBLOCK);
1967 if (file->f_mode & FMODE_WRITE) {
1969 dealloc_dmabuf(s, &s->dma_dac);
1971 if (file->f_mode & FMODE_READ) {
1973 dealloc_dmabuf(s, &s->dma_adc);
1975 s->open_mode &= ~(file->f_mode & (FMODE_READ|FMODE_WRITE));
1976 wake_up(&s->open_wait);
1982 static /*const*/ struct file_operations sv_audio_fops = {
1983 .owner = THIS_MODULE,
1984 .llseek = no_llseek,
1991 .release = sv_release,
1994 /* --------------------------------------------------------------------- */
1996 static ssize_t sv_midi_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
1998 struct sv_state *s = (struct sv_state *)file->private_data;
1999 DECLARE_WAITQUEUE(wait, current);
2001 unsigned long flags;
2006 if (!access_ok(VERIFY_WRITE, buffer, count))
2011 add_wait_queue(&s->midi.iwait, &wait);
2013 spin_lock_irqsave(&s->lock, flags);
2015 cnt = MIDIINBUF - ptr;
2016 if (s->midi.icnt < cnt)
2019 __set_current_state(TASK_INTERRUPTIBLE);
2020 spin_unlock_irqrestore(&s->lock, flags);
2024 if (file->f_flags & O_NONBLOCK) {
2030 if (signal_pending(current)) {
2037 if (copy_to_user(buffer, s->midi.ibuf + ptr, cnt)) {
2042 ptr = (ptr + cnt) % MIDIINBUF;
2043 spin_lock_irqsave(&s->lock, flags);
2045 s->midi.icnt -= cnt;
2046 spin_unlock_irqrestore(&s->lock, flags);
2052 __set_current_state(TASK_RUNNING);
2053 remove_wait_queue(&s->midi.iwait, &wait);
2057 static ssize_t sv_midi_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2059 struct sv_state *s = (struct sv_state *)file->private_data;
2060 DECLARE_WAITQUEUE(wait, current);
2062 unsigned long flags;
2067 if (!access_ok(VERIFY_READ, buffer, count))
2072 add_wait_queue(&s->midi.owait, &wait);
2074 spin_lock_irqsave(&s->lock, flags);
2076 cnt = MIDIOUTBUF - ptr;
2077 if (s->midi.ocnt + cnt > MIDIOUTBUF)
2078 cnt = MIDIOUTBUF - s->midi.ocnt;
2080 __set_current_state(TASK_INTERRUPTIBLE);
2083 spin_unlock_irqrestore(&s->lock, flags);
2087 if (file->f_flags & O_NONBLOCK) {
2093 if (signal_pending(current)) {
2100 if (copy_from_user(s->midi.obuf + ptr, buffer, cnt)) {
2105 ptr = (ptr + cnt) % MIDIOUTBUF;
2106 spin_lock_irqsave(&s->lock, flags);
2108 s->midi.ocnt += cnt;
2109 spin_unlock_irqrestore(&s->lock, flags);
2113 spin_lock_irqsave(&s->lock, flags);
2115 spin_unlock_irqrestore(&s->lock, flags);
2117 __set_current_state(TASK_RUNNING);
2118 remove_wait_queue(&s->midi.owait, &wait);
2122 /* No kernel lock - we have our own spinlock */
2123 static unsigned int sv_midi_poll(struct file *file, struct poll_table_struct *wait)
2125 struct sv_state *s = (struct sv_state *)file->private_data;
2126 unsigned long flags;
2127 unsigned int mask = 0;
2130 if (file->f_mode & FMODE_WRITE)
2131 poll_wait(file, &s->midi.owait, wait);
2132 if (file->f_mode & FMODE_READ)
2133 poll_wait(file, &s->midi.iwait, wait);
2134 spin_lock_irqsave(&s->lock, flags);
2135 if (file->f_mode & FMODE_READ) {
2136 if (s->midi.icnt > 0)
2137 mask |= POLLIN | POLLRDNORM;
2139 if (file->f_mode & FMODE_WRITE) {
2140 if (s->midi.ocnt < MIDIOUTBUF)
2141 mask |= POLLOUT | POLLWRNORM;
2143 spin_unlock_irqrestore(&s->lock, flags);
2147 static int sv_midi_open(struct inode *inode, struct file *file)
2149 int minor = iminor(inode);
2150 DECLARE_WAITQUEUE(wait, current);
2151 unsigned long flags;
2152 struct list_head *list;
2155 for (list = devs.next; ; list = list->next) {
2158 s = list_entry(list, struct sv_state, devs);
2159 if (s->dev_midi == minor)
2163 file->private_data = s;
2164 /* wait for device to become free */
2166 while (s->open_mode & (file->f_mode << FMODE_MIDI_SHIFT)) {
2167 if (file->f_flags & O_NONBLOCK) {
2171 add_wait_queue(&s->open_wait, &wait);
2172 __set_current_state(TASK_INTERRUPTIBLE);
2175 remove_wait_queue(&s->open_wait, &wait);
2176 set_current_state(TASK_RUNNING);
2177 if (signal_pending(current))
2178 return -ERESTARTSYS;
2181 spin_lock_irqsave(&s->lock, flags);
2182 if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
2183 s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
2184 s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
2185 //outb(inb(s->ioenh + SV_CODEC_CONTROL) | SV_CCTRL_WAVETABLE, s->ioenh + SV_CODEC_CONTROL);
2186 outb(inb(s->ioenh + SV_CODEC_INTMASK) | SV_CINTMASK_MIDI, s->ioenh + SV_CODEC_INTMASK);
2187 wrindir(s, SV_CIUARTCONTROL, 5); /* output MIDI data to external and internal synth */
2188 wrindir(s, SV_CIWAVETABLESRC, 1); /* Wavetable in PC RAM */
2189 outb(0xff, s->iomidi+1); /* reset command */
2190 outb(0x3f, s->iomidi+1); /* uart command */
2191 if (!(inb(s->iomidi+1) & 0x80))
2193 s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
2194 init_timer(&s->midi.timer);
2195 s->midi.timer.expires = jiffies+1;
2196 s->midi.timer.data = (unsigned long)s;
2197 s->midi.timer.function = sv_midi_timer;
2198 add_timer(&s->midi.timer);
2200 if (file->f_mode & FMODE_READ) {
2201 s->midi.ird = s->midi.iwr = s->midi.icnt = 0;
2203 if (file->f_mode & FMODE_WRITE) {
2204 s->midi.ord = s->midi.owr = s->midi.ocnt = 0;
2206 spin_unlock_irqrestore(&s->lock, flags);
2207 s->open_mode |= (file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ | FMODE_MIDI_WRITE);
2209 return nonseekable_open(inode, file);
2212 static int sv_midi_release(struct inode *inode, struct file *file)
2214 struct sv_state *s = (struct sv_state *)file->private_data;
2215 DECLARE_WAITQUEUE(wait, current);
2216 unsigned long flags;
2217 unsigned count, tmo;
2222 if (file->f_mode & FMODE_WRITE) {
2223 add_wait_queue(&s->midi.owait, &wait);
2225 __set_current_state(TASK_INTERRUPTIBLE);
2226 spin_lock_irqsave(&s->lock, flags);
2227 count = s->midi.ocnt;
2228 spin_unlock_irqrestore(&s->lock, flags);
2231 if (signal_pending(current))
2233 if (file->f_flags & O_NONBLOCK) {
2234 remove_wait_queue(&s->midi.owait, &wait);
2235 set_current_state(TASK_RUNNING);
2239 tmo = (count * HZ) / 3100;
2240 if (!schedule_timeout(tmo ? : 1) && tmo)
2241 printk(KERN_DEBUG "sv: midi timed out??\n");
2243 remove_wait_queue(&s->midi.owait, &wait);
2244 set_current_state(TASK_RUNNING);
2247 s->open_mode &= ~((file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ|FMODE_MIDI_WRITE));
2248 spin_lock_irqsave(&s->lock, flags);
2249 if (!(s->open_mode & (FMODE_MIDI_READ | FMODE_MIDI_WRITE))) {
2250 outb(inb(s->ioenh + SV_CODEC_INTMASK) & ~SV_CINTMASK_MIDI, s->ioenh + SV_CODEC_INTMASK);
2251 del_timer(&s->midi.timer);
2253 spin_unlock_irqrestore(&s->lock, flags);
2254 wake_up(&s->open_wait);
2260 static /*const*/ struct file_operations sv_midi_fops = {
2261 .owner = THIS_MODULE,
2262 .llseek = no_llseek,
2263 .read = sv_midi_read,
2264 .write = sv_midi_write,
2265 .poll = sv_midi_poll,
2266 .open = sv_midi_open,
2267 .release = sv_midi_release,
2270 /* --------------------------------------------------------------------- */
2272 static int sv_dmfm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2274 static const unsigned char op_offset[18] = {
2275 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
2276 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
2277 0x10, 0x11, 0x12, 0x13, 0x14, 0x15
2279 struct sv_state *s = (struct sv_state *)file->private_data;
2280 struct dm_fm_voice v;
2281 struct dm_fm_note n;
2282 struct dm_fm_params p;
2287 case FM_IOCTL_RESET:
2288 for (regb = 0xb0; regb < 0xb9; regb++) {
2289 outb(regb, s->iosynth);
2290 outb(0, s->iosynth+1);
2291 outb(regb, s->iosynth+2);
2292 outb(0, s->iosynth+3);
2296 case FM_IOCTL_PLAY_NOTE:
2297 if (copy_from_user(&n, (void __user *)arg, sizeof(n)))
2308 outb(0xa0 + regb, io);
2309 outb(n.fnum & 0xff, io+1);
2310 outb(0xb0 + regb, io);
2311 outb(((n.fnum >> 8) & 3) | ((n.octave & 7) << 2) | ((n.key_on & 1) << 5), io+1);
2314 case FM_IOCTL_SET_VOICE:
2315 if (copy_from_user(&v, (void __user *)arg, sizeof(v)))
2319 regb = op_offset[v.voice];
2320 io = s->iosynth + ((v.op & 1) << 1);
2321 outb(0x20 + regb, io);
2322 outb(((v.am & 1) << 7) | ((v.vibrato & 1) << 6) | ((v.do_sustain & 1) << 5) |
2323 ((v.kbd_scale & 1) << 4) | (v.harmonic & 0xf), io+1);
2324 outb(0x40 + regb, io);
2325 outb(((v.scale_level & 0x3) << 6) | (v.volume & 0x3f), io+1);
2326 outb(0x60 + regb, io);
2327 outb(((v.attack & 0xf) << 4) | (v.decay & 0xf), io+1);
2328 outb(0x80 + regb, io);
2329 outb(((v.sustain & 0xf) << 4) | (v.release & 0xf), io+1);
2330 outb(0xe0 + regb, io);
2331 outb(v.waveform & 0x7, io+1);
2339 outb(0xc0 + regb, io);
2340 outb(((v.right & 1) << 5) | ((v.left & 1) << 4) | ((v.feedback & 7) << 1) |
2341 (v.connection & 1), io+1);
2344 case FM_IOCTL_SET_PARAMS:
2345 if (copy_from_user(&p, (void *__user )arg, sizeof(p)))
2347 outb(0x08, s->iosynth);
2348 outb((p.kbd_split & 1) << 6, s->iosynth+1);
2349 outb(0xbd, s->iosynth);
2350 outb(((p.am_depth & 1) << 7) | ((p.vib_depth & 1) << 6) | ((p.rhythm & 1) << 5) | ((p.bass & 1) << 4) |
2351 ((p.snare & 1) << 3) | ((p.tomtom & 1) << 2) | ((p.cymbal & 1) << 1) | (p.hihat & 1), s->iosynth+1);
2354 case FM_IOCTL_SET_OPL:
2355 outb(4, s->iosynth+2);
2356 outb(arg, s->iosynth+3);
2359 case FM_IOCTL_SET_MODE:
2360 outb(5, s->iosynth+2);
2361 outb(arg & 1, s->iosynth+3);
2369 static int sv_dmfm_open(struct inode *inode, struct file *file)
2371 int minor = iminor(inode);
2372 DECLARE_WAITQUEUE(wait, current);
2373 struct list_head *list;
2376 for (list = devs.next; ; list = list->next) {
2379 s = list_entry(list, struct sv_state, devs);
2380 if (s->dev_dmfm == minor)
2384 file->private_data = s;
2385 /* wait for device to become free */
2387 while (s->open_mode & FMODE_DMFM) {
2388 if (file->f_flags & O_NONBLOCK) {
2392 add_wait_queue(&s->open_wait, &wait);
2393 __set_current_state(TASK_INTERRUPTIBLE);
2396 remove_wait_queue(&s->open_wait, &wait);
2397 set_current_state(TASK_RUNNING);
2398 if (signal_pending(current))
2399 return -ERESTARTSYS;
2402 /* init the stuff */
2403 outb(1, s->iosynth);
2404 outb(0x20, s->iosynth+1); /* enable waveforms */
2405 outb(4, s->iosynth+2);
2406 outb(0, s->iosynth+3); /* no 4op enabled */
2407 outb(5, s->iosynth+2);
2408 outb(1, s->iosynth+3); /* enable OPL3 */
2409 s->open_mode |= FMODE_DMFM;
2411 return nonseekable_open(inode, file);
2414 static int sv_dmfm_release(struct inode *inode, struct file *file)
2416 struct sv_state *s = (struct sv_state *)file->private_data;
2422 s->open_mode &= ~FMODE_DMFM;
2423 for (regb = 0xb0; regb < 0xb9; regb++) {
2424 outb(regb, s->iosynth);
2425 outb(0, s->iosynth+1);
2426 outb(regb, s->iosynth+2);
2427 outb(0, s->iosynth+3);
2429 wake_up(&s->open_wait);
2435 static /*const*/ struct file_operations sv_dmfm_fops = {
2436 .owner = THIS_MODULE,
2437 .llseek = no_llseek,
2438 .ioctl = sv_dmfm_ioctl,
2439 .open = sv_dmfm_open,
2440 .release = sv_dmfm_release,
2443 /* --------------------------------------------------------------------- */
2445 /* maximum number of devices; only used for command line params */
2448 static int reverb[NR_DEVICE];
2451 static int wavetable[NR_DEVICE];
2454 static unsigned int devindex;
2456 module_param_array(reverb, bool, NULL, 0);
2457 MODULE_PARM_DESC(reverb, "if 1 enables the reverb circuitry. NOTE: your card must have the reverb RAM");
2459 MODULE_PARM(wavetable, "1-" __MODULE_STRING(NR_DEVICE) "i");
2460 MODULE_PARM_DESC(wavetable, "if 1 the wavetable synth is enabled");
2463 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
2464 MODULE_DESCRIPTION("S3 SonicVibes Driver");
2465 MODULE_LICENSE("GPL");
2468 /* --------------------------------------------------------------------- */
2470 static struct initvol {
2473 } initvol[] __devinitdata = {
2474 { SOUND_MIXER_WRITE_RECLEV, 0x4040 },
2475 { SOUND_MIXER_WRITE_LINE1, 0x4040 },
2476 { SOUND_MIXER_WRITE_CD, 0x4040 },
2477 { SOUND_MIXER_WRITE_LINE, 0x4040 },
2478 { SOUND_MIXER_WRITE_MIC, 0x4040 },
2479 { SOUND_MIXER_WRITE_SYNTH, 0x4040 },
2480 { SOUND_MIXER_WRITE_LINE2, 0x4040 },
2481 { SOUND_MIXER_WRITE_VOLUME, 0x4040 },
2482 { SOUND_MIXER_WRITE_PCM, 0x4040 }
2485 #define RSRCISIOREGION(dev,num) (pci_resource_start((dev), (num)) != 0 && \
2486 (pci_resource_flags((dev), (num)) & IORESOURCE_IO))
2488 static int __devinit sv_register_gameport(struct sv_state *s, int io_port)
2490 struct gameport *gp;
2492 if (!request_region(io_port, SV_EXTENT_GAME, "S3 SonicVibes Gameport")) {
2493 printk(KERN_ERR "sv: gameport io ports are in use\n");
2497 s->gameport = gp = gameport_allocate_port();
2499 printk(KERN_ERR "sv: can not allocate memory for gameport\n");
2500 release_region(io_port, SV_EXTENT_GAME);
2504 gameport_set_name(gp, "S3 SonicVibes Gameport");
2505 gameport_set_phys(gp, "isa%04x/gameport0", io_port);
2506 gp->dev.parent = &s->dev->dev;
2509 gameport_register_port(gp);
2514 static int __devinit sv_probe(struct pci_dev *pcidev, const struct pci_device_id *pciid)
2516 static char __devinitdata sv_ddma_name[] = "S3 Inc. SonicVibes DDMA Controller";
2522 unsigned ddmanamelen;
2524 if ((ret=pci_enable_device(pcidev)))
2527 if (!RSRCISIOREGION(pcidev, RESOURCE_SB) ||
2528 !RSRCISIOREGION(pcidev, RESOURCE_ENH) ||
2529 !RSRCISIOREGION(pcidev, RESOURCE_SYNTH) ||
2530 !RSRCISIOREGION(pcidev, RESOURCE_MIDI) ||
2531 !RSRCISIOREGION(pcidev, RESOURCE_GAME))
2533 if (pcidev->irq == 0)
2535 if (pci_set_dma_mask(pcidev, 0x00ffffff)) {
2536 printk(KERN_WARNING "sonicvibes: architecture does not support 24bit PCI busmaster DMA\n");
2539 /* try to allocate a DDMA resource if not already available */
2540 if (!RSRCISIOREGION(pcidev, RESOURCE_DDMA)) {
2541 pcidev->resource[RESOURCE_DDMA].start = 0;
2542 pcidev->resource[RESOURCE_DDMA].end = 2*SV_EXTENT_DMA-1;
2543 pcidev->resource[RESOURCE_DDMA].flags = PCI_BASE_ADDRESS_SPACE_IO | IORESOURCE_IO;
2544 ddmanamelen = strlen(sv_ddma_name)+1;
2545 if (!(ddmaname = kmalloc(ddmanamelen, GFP_KERNEL)))
2547 memcpy(ddmaname, sv_ddma_name, ddmanamelen);
2548 pcidev->resource[RESOURCE_DDMA].name = ddmaname;
2549 if (pci_assign_resource(pcidev, RESOURCE_DDMA)) {
2550 pcidev->resource[RESOURCE_DDMA].name = NULL;
2552 printk(KERN_ERR "sv: cannot allocate DDMA controller io ports\n");
2556 if (!(s = kmalloc(sizeof(struct sv_state), GFP_KERNEL))) {
2557 printk(KERN_WARNING "sv: out of memory\n");
2560 memset(s, 0, sizeof(struct sv_state));
2561 init_waitqueue_head(&s->dma_adc.wait);
2562 init_waitqueue_head(&s->dma_dac.wait);
2563 init_waitqueue_head(&s->open_wait);
2564 init_waitqueue_head(&s->midi.iwait);
2565 init_waitqueue_head(&s->midi.owait);
2566 init_MUTEX(&s->open_sem);
2567 spin_lock_init(&s->lock);
2568 s->magic = SV_MAGIC;
2570 s->iosb = pci_resource_start(pcidev, RESOURCE_SB);
2571 s->ioenh = pci_resource_start(pcidev, RESOURCE_ENH);
2572 s->iosynth = pci_resource_start(pcidev, RESOURCE_SYNTH);
2573 s->iomidi = pci_resource_start(pcidev, RESOURCE_MIDI);
2574 s->iodmaa = pci_resource_start(pcidev, RESOURCE_DDMA);
2575 s->iodmac = pci_resource_start(pcidev, RESOURCE_DDMA) + SV_EXTENT_DMA;
2576 gpio = pci_resource_start(pcidev, RESOURCE_GAME);
2577 pci_write_config_dword(pcidev, 0x40, s->iodmaa | 9); /* enable and use extended mode */
2578 pci_write_config_dword(pcidev, 0x48, s->iodmac | 9); /* enable */
2579 printk(KERN_DEBUG "sv: io ports: %#lx %#lx %#lx %#lx %#x %#x %#x\n",
2580 s->iosb, s->ioenh, s->iosynth, s->iomidi, gpio, s->iodmaa, s->iodmac);
2581 s->irq = pcidev->irq;
2584 pci_write_config_dword(pcidev, 0x60, wavetable_mem >> 12); /* wavetable base address */
2587 if (!request_region(s->ioenh, SV_EXTENT_ENH, "S3 SonicVibes PCM")) {
2588 printk(KERN_ERR "sv: io ports %#lx-%#lx in use\n", s->ioenh, s->ioenh+SV_EXTENT_ENH-1);
2591 if (!request_region(s->iodmaa, SV_EXTENT_DMA, "S3 SonicVibes DMAA")) {
2592 printk(KERN_ERR "sv: io ports %#x-%#x in use\n", s->iodmaa, s->iodmaa+SV_EXTENT_DMA-1);
2595 if (!request_region(s->iodmac, SV_EXTENT_DMA, "S3 SonicVibes DMAC")) {
2596 printk(KERN_ERR "sv: io ports %#x-%#x in use\n", s->iodmac, s->iodmac+SV_EXTENT_DMA-1);
2599 if (!request_region(s->iomidi, SV_EXTENT_MIDI, "S3 SonicVibes Midi")) {
2600 printk(KERN_ERR "sv: io ports %#lx-%#lx in use\n", s->iomidi, s->iomidi+SV_EXTENT_MIDI-1);
2603 if (!request_region(s->iosynth, SV_EXTENT_SYNTH, "S3 SonicVibes Synth")) {
2604 printk(KERN_ERR "sv: io ports %#lx-%#lx in use\n", s->iosynth, s->iosynth+SV_EXTENT_SYNTH-1);
2608 /* initialize codec registers */
2609 outb(0x80, s->ioenh + SV_CODEC_CONTROL); /* assert reset */
2611 outb(0x00, s->ioenh + SV_CODEC_CONTROL); /* deassert reset */
2613 outb(SV_CCTRL_INTADRIVE | SV_CCTRL_ENHANCED /*| SV_CCTRL_WAVETABLE */
2614 | (reverb[devindex] ? SV_CCTRL_REVERB : 0), s->ioenh + SV_CODEC_CONTROL);
2615 inb(s->ioenh + SV_CODEC_STATUS); /* clear ints */
2616 wrindir(s, SV_CIDRIVECONTROL, 0); /* drive current 16mA */
2617 wrindir(s, SV_CIENABLE, s->enable = 0); /* disable DMAA and DMAC */
2618 outb(~(SV_CINTMASK_DMAA | SV_CINTMASK_DMAC), s->ioenh + SV_CODEC_INTMASK);
2619 /* outb(0xff, s->iodmaa + SV_DMA_RESET); */
2620 /* outb(0xff, s->iodmac + SV_DMA_RESET); */
2621 inb(s->ioenh + SV_CODEC_STATUS); /* ack interrupts */
2622 wrindir(s, SV_CIADCCLKSOURCE, 0); /* use pll as ADC clock source */
2623 wrindir(s, SV_CIANALOGPWRDOWN, 0); /* power up the analog parts of the device */
2624 wrindir(s, SV_CIDIGITALPWRDOWN, 0); /* power up the digital parts of the device */
2625 setpll(s, SV_CIADCPLLM, 8000);
2626 wrindir(s, SV_CISRSSPACE, 0x80); /* SRS off */
2627 wrindir(s, SV_CIPCMSR0, (8000 * 65536 / FULLRATE) & 0xff);
2628 wrindir(s, SV_CIPCMSR1, ((8000 * 65536 / FULLRATE) >> 8) & 0xff);
2629 wrindir(s, SV_CIADCOUTPUT, 0);
2631 if ((ret=request_irq(s->irq,sv_interrupt,SA_SHIRQ,"S3 SonicVibes",s))) {
2632 printk(KERN_ERR "sv: irq %u in use\n", s->irq);
2635 printk(KERN_INFO "sv: found adapter at io %#lx irq %u dmaa %#06x dmac %#06x revision %u\n",
2636 s->ioenh, s->irq, s->iodmaa, s->iodmac, rdindir(s, SV_CIREVISION));
2637 /* register devices */
2638 if ((s->dev_audio = register_sound_dsp(&sv_audio_fops, -1)) < 0) {
2642 if ((s->dev_mixer = register_sound_mixer(&sv_mixer_fops, -1)) < 0) {
2646 if ((s->dev_midi = register_sound_midi(&sv_midi_fops, -1)) < 0) {
2650 if ((s->dev_dmfm = register_sound_special(&sv_dmfm_fops, 15 /* ?? */)) < 0) {
2654 pci_set_master(pcidev); /* enable bus mastering */
2655 /* initialize the chips */
2658 val = SOUND_MASK_LINE|SOUND_MASK_SYNTH;
2659 mixer_ioctl(s, SOUND_MIXER_WRITE_RECSRC, (unsigned long)&val);
2660 for (i = 0; i < sizeof(initvol)/sizeof(initvol[0]); i++) {
2661 val = initvol[i].vol;
2662 mixer_ioctl(s, initvol[i].mixch, (unsigned long)&val);
2665 /* register gameport */
2666 sv_register_gameport(s, gpio);
2667 /* store it in the driver field */
2668 pci_set_drvdata(pcidev, s);
2669 /* put it into driver list */
2670 list_add_tail(&s->devs, &devs);
2671 /* increment devindex */
2672 if (devindex < NR_DEVICE-1)
2677 unregister_sound_midi(s->dev_midi);
2679 unregister_sound_mixer(s->dev_mixer);
2681 unregister_sound_dsp(s->dev_audio);
2683 printk(KERN_ERR "sv: cannot register misc device\n");
2684 free_irq(s->irq, s);
2686 release_region(s->iosynth, SV_EXTENT_SYNTH);
2688 release_region(s->iomidi, SV_EXTENT_MIDI);
2690 release_region(s->iodmac, SV_EXTENT_DMA);
2692 release_region(s->iodmaa, SV_EXTENT_DMA);
2694 release_region(s->ioenh, SV_EXTENT_ENH);
2700 static void __devexit sv_remove(struct pci_dev *dev)
2702 struct sv_state *s = pci_get_drvdata(dev);
2707 outb(~0, s->ioenh + SV_CODEC_INTMASK); /* disable ints */
2708 synchronize_irq(s->irq);
2709 inb(s->ioenh + SV_CODEC_STATUS); /* ack interrupts */
2710 wrindir(s, SV_CIENABLE, 0); /* disable DMAA and DMAC */
2711 /*outb(0, s->iodmaa + SV_DMA_RESET);*/
2712 /*outb(0, s->iodmac + SV_DMA_RESET);*/
2713 free_irq(s->irq, s);
2715 int gpio = s->gameport->io;
2716 gameport_unregister_port(s->gameport);
2717 release_region(gpio, SV_EXTENT_GAME);
2719 release_region(s->iodmac, SV_EXTENT_DMA);
2720 release_region(s->iodmaa, SV_EXTENT_DMA);
2721 release_region(s->ioenh, SV_EXTENT_ENH);
2722 release_region(s->iomidi, SV_EXTENT_MIDI);
2723 release_region(s->iosynth, SV_EXTENT_SYNTH);
2724 unregister_sound_dsp(s->dev_audio);
2725 unregister_sound_mixer(s->dev_mixer);
2726 unregister_sound_midi(s->dev_midi);
2727 unregister_sound_special(s->dev_dmfm);
2729 pci_set_drvdata(dev, NULL);
2732 static struct pci_device_id id_table[] = {
2733 { PCI_VENDOR_ID_S3, PCI_DEVICE_ID_S3_SONICVIBES, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
2737 MODULE_DEVICE_TABLE(pci, id_table);
2739 static struct pci_driver sv_driver = {
2740 .name = "sonicvibes",
2741 .id_table = id_table,
2743 .remove = __devexit_p(sv_remove),
2746 static int __init init_sonicvibes(void)
2748 printk(KERN_INFO "sv: version v0.31 time " __TIME__ " " __DATE__ "\n");
2750 if (!(wavetable_mem = __get_free_pages(GFP_KERNEL, 20-PAGE_SHIFT)))
2751 printk(KERN_INFO "sv: cannot allocate 1MB of contiguous nonpageable memory for wavetable data\n");
2753 return pci_module_init(&sv_driver);
2756 static void __exit cleanup_sonicvibes(void)
2758 printk(KERN_INFO "sv: unloading\n");
2759 pci_unregister_driver(&sv_driver);
2761 free_pages(wavetable_mem, 20-PAGE_SHIFT);
2764 module_init(init_sonicvibes);
2765 module_exit(cleanup_sonicvibes);
2767 /* --------------------------------------------------------------------- */
2771 /* format is: sonicvibes=[reverb] sonicvibesdmaio=dmaioaddr */
2773 static int __init sonicvibes_setup(char *str)
2775 static unsigned __initdata nr_dev = 0;
2777 if (nr_dev >= NR_DEVICE)
2780 if (get_option(&str, &reverb[nr_dev]) == 2)
2781 (void)get_option(&str, &wavetable[nr_dev]);
2783 (void)get_option(&str, &reverb[nr_dev]);
2790 __setup("sonicvibes=", sonicvibes_setup);