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_page_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 (!mixtable
[i
].rec
)
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_page_range(vma
, vma
->vm_start
, virt_to_phys(db
->rawbuf
), size
, vma
->vm_page_prot
))
1561 static int sv_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1563 struct sv_state
*s
= (struct sv_state
*)file
->private_data
;
1564 unsigned long flags
;
1565 audio_buf_info abinfo
;
1568 int val
, mapped
, ret
;
1569 unsigned char fmtm
, fmtd
;
1570 void __user
*argp
= (void __user
*)arg
;
1571 int __user
*p
= argp
;
1574 mapped
= ((file
->f_mode
& FMODE_WRITE
) && s
->dma_dac
.mapped
) ||
1575 ((file
->f_mode
& FMODE_READ
) && s
->dma_adc
.mapped
);
1577 case OSS_GETVERSION
:
1578 return put_user(SOUND_VERSION
, p
);
1580 case SNDCTL_DSP_SYNC
:
1581 if (file
->f_mode
& FMODE_WRITE
)
1582 return drain_dac(s
, 0/*file->f_flags & O_NONBLOCK*/);
1585 case SNDCTL_DSP_SETDUPLEX
:
1588 case SNDCTL_DSP_GETCAPS
:
1589 return put_user(DSP_CAP_DUPLEX
| DSP_CAP_REALTIME
| DSP_CAP_TRIGGER
| DSP_CAP_MMAP
, p
);
1591 case SNDCTL_DSP_RESET
:
1592 if (file
->f_mode
& FMODE_WRITE
) {
1594 synchronize_irq(s
->irq
);
1595 s
->dma_dac
.swptr
= s
->dma_dac
.hwptr
= s
->dma_dac
.count
= s
->dma_dac
.total_bytes
= 0;
1597 if (file
->f_mode
& FMODE_READ
) {
1599 synchronize_irq(s
->irq
);
1600 s
->dma_adc
.swptr
= s
->dma_adc
.hwptr
= s
->dma_adc
.count
= s
->dma_adc
.total_bytes
= 0;
1604 case SNDCTL_DSP_SPEED
:
1605 if (get_user(val
, p
))
1608 if (file
->f_mode
& FMODE_READ
) {
1610 s
->dma_adc
.ready
= 0;
1611 set_adc_rate(s
, val
);
1613 if (file
->f_mode
& FMODE_WRITE
) {
1615 s
->dma_dac
.ready
= 0;
1616 set_dac_rate(s
, val
);
1619 return put_user((file
->f_mode
& FMODE_READ
) ? s
->rateadc
: s
->ratedac
, p
);
1621 case SNDCTL_DSP_STEREO
:
1622 if (get_user(val
, p
))
1626 if (file
->f_mode
& FMODE_READ
) {
1628 s
->dma_adc
.ready
= 0;
1630 fmtd
|= SV_CFMT_STEREO
<< SV_CFMT_CSHIFT
;
1632 fmtm
&= ~(SV_CFMT_STEREO
<< SV_CFMT_CSHIFT
);
1634 if (file
->f_mode
& FMODE_WRITE
) {
1636 s
->dma_dac
.ready
= 0;
1638 fmtd
|= SV_CFMT_STEREO
<< SV_CFMT_ASHIFT
;
1640 fmtm
&= ~(SV_CFMT_STEREO
<< SV_CFMT_ASHIFT
);
1642 set_fmt(s
, fmtm
, fmtd
);
1645 case SNDCTL_DSP_CHANNELS
:
1646 if (get_user(val
, p
))
1651 if (file
->f_mode
& FMODE_READ
) {
1653 s
->dma_adc
.ready
= 0;
1655 fmtd
|= SV_CFMT_STEREO
<< SV_CFMT_CSHIFT
;
1657 fmtm
&= ~(SV_CFMT_STEREO
<< SV_CFMT_CSHIFT
);
1659 if (file
->f_mode
& FMODE_WRITE
) {
1661 s
->dma_dac
.ready
= 0;
1663 fmtd
|= SV_CFMT_STEREO
<< SV_CFMT_ASHIFT
;
1665 fmtm
&= ~(SV_CFMT_STEREO
<< SV_CFMT_ASHIFT
);
1667 set_fmt(s
, fmtm
, fmtd
);
1669 return put_user((s
->fmt
& ((file
->f_mode
& FMODE_READ
) ? (SV_CFMT_STEREO
<< SV_CFMT_CSHIFT
)
1670 : (SV_CFMT_STEREO
<< SV_CFMT_ASHIFT
))) ? 2 : 1, p
);
1672 case SNDCTL_DSP_GETFMTS
: /* Returns a mask */
1673 return put_user(AFMT_S16_LE
|AFMT_U8
, p
);
1675 case SNDCTL_DSP_SETFMT
: /* Selects ONE fmt*/
1676 if (get_user(val
, p
))
1678 if (val
!= AFMT_QUERY
) {
1681 if (file
->f_mode
& FMODE_READ
) {
1683 s
->dma_adc
.ready
= 0;
1684 if (val
== AFMT_S16_LE
)
1685 fmtd
|= SV_CFMT_16BIT
<< SV_CFMT_CSHIFT
;
1687 fmtm
&= ~(SV_CFMT_16BIT
<< SV_CFMT_CSHIFT
);
1689 if (file
->f_mode
& FMODE_WRITE
) {
1691 s
->dma_dac
.ready
= 0;
1692 if (val
== AFMT_S16_LE
)
1693 fmtd
|= SV_CFMT_16BIT
<< SV_CFMT_ASHIFT
;
1695 fmtm
&= ~(SV_CFMT_16BIT
<< SV_CFMT_ASHIFT
);
1697 set_fmt(s
, fmtm
, fmtd
);
1699 return put_user((s
->fmt
& ((file
->f_mode
& FMODE_READ
) ? (SV_CFMT_16BIT
<< SV_CFMT_CSHIFT
)
1700 : (SV_CFMT_16BIT
<< SV_CFMT_ASHIFT
))) ? AFMT_S16_LE
: AFMT_U8
, p
);
1702 case SNDCTL_DSP_POST
:
1705 case SNDCTL_DSP_GETTRIGGER
:
1707 if (file
->f_mode
& FMODE_READ
&& s
->enable
& SV_CENABLE_RE
)
1708 val
|= PCM_ENABLE_INPUT
;
1709 if (file
->f_mode
& FMODE_WRITE
&& s
->enable
& SV_CENABLE_PE
)
1710 val
|= PCM_ENABLE_OUTPUT
;
1711 return put_user(val
, p
);
1713 case SNDCTL_DSP_SETTRIGGER
:
1714 if (get_user(val
, p
))
1716 if (file
->f_mode
& FMODE_READ
) {
1717 if (val
& PCM_ENABLE_INPUT
) {
1718 if (!s
->dma_adc
.ready
&& (ret
= prog_dmabuf(s
, 1)))
1720 s
->dma_adc
.enabled
= 1;
1723 s
->dma_adc
.enabled
= 0;
1727 if (file
->f_mode
& FMODE_WRITE
) {
1728 if (val
& PCM_ENABLE_OUTPUT
) {
1729 if (!s
->dma_dac
.ready
&& (ret
= prog_dmabuf(s
, 0)))
1731 s
->dma_dac
.enabled
= 1;
1734 s
->dma_dac
.enabled
= 0;
1740 case SNDCTL_DSP_GETOSPACE
:
1741 if (!(file
->f_mode
& FMODE_WRITE
))
1743 if (!s
->dma_dac
.ready
&& (val
= prog_dmabuf(s
, 0)) != 0)
1745 spin_lock_irqsave(&s
->lock
, flags
);
1747 abinfo
.fragsize
= s
->dma_dac
.fragsize
;
1748 count
= s
->dma_dac
.count
;
1751 abinfo
.bytes
= s
->dma_dac
.dmasize
- count
;
1752 abinfo
.fragstotal
= s
->dma_dac
.numfrag
;
1753 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_dac
.fragshift
;
1754 spin_unlock_irqrestore(&s
->lock
, flags
);
1755 return copy_to_user(argp
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1757 case SNDCTL_DSP_GETISPACE
:
1758 if (!(file
->f_mode
& FMODE_READ
))
1760 if (!s
->dma_adc
.ready
&& (val
= prog_dmabuf(s
, 1)) != 0)
1762 spin_lock_irqsave(&s
->lock
, flags
);
1764 abinfo
.fragsize
= s
->dma_adc
.fragsize
;
1765 count
= s
->dma_adc
.count
;
1768 abinfo
.bytes
= count
;
1769 abinfo
.fragstotal
= s
->dma_adc
.numfrag
;
1770 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_adc
.fragshift
;
1771 spin_unlock_irqrestore(&s
->lock
, flags
);
1772 return copy_to_user(argp
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1774 case SNDCTL_DSP_NONBLOCK
:
1775 file
->f_flags
|= O_NONBLOCK
;
1778 case SNDCTL_DSP_GETODELAY
:
1779 if (!(file
->f_mode
& FMODE_WRITE
))
1781 if (!s
->dma_dac
.ready
&& (val
= prog_dmabuf(s
, 0)) != 0)
1783 spin_lock_irqsave(&s
->lock
, flags
);
1785 count
= s
->dma_dac
.count
;
1786 spin_unlock_irqrestore(&s
->lock
, flags
);
1789 return put_user(count
, p
);
1791 case SNDCTL_DSP_GETIPTR
:
1792 if (!(file
->f_mode
& FMODE_READ
))
1794 if (!s
->dma_adc
.ready
&& (val
= prog_dmabuf(s
, 1)) != 0)
1796 spin_lock_irqsave(&s
->lock
, flags
);
1798 cinfo
.bytes
= s
->dma_adc
.total_bytes
;
1799 count
= s
->dma_adc
.count
;
1802 cinfo
.blocks
= count
>> s
->dma_adc
.fragshift
;
1803 cinfo
.ptr
= s
->dma_adc
.hwptr
;
1804 if (s
->dma_adc
.mapped
)
1805 s
->dma_adc
.count
&= s
->dma_adc
.fragsize
-1;
1806 spin_unlock_irqrestore(&s
->lock
, flags
);
1807 if (copy_to_user(argp
, &cinfo
, sizeof(cinfo
)))
1811 case SNDCTL_DSP_GETOPTR
:
1812 if (!(file
->f_mode
& FMODE_WRITE
))
1814 if (!s
->dma_dac
.ready
&& (val
= prog_dmabuf(s
, 0)) != 0)
1816 spin_lock_irqsave(&s
->lock
, flags
);
1818 cinfo
.bytes
= s
->dma_dac
.total_bytes
;
1819 count
= s
->dma_dac
.count
;
1822 cinfo
.blocks
= count
>> s
->dma_dac
.fragshift
;
1823 cinfo
.ptr
= s
->dma_dac
.hwptr
;
1824 if (s
->dma_dac
.mapped
)
1825 s
->dma_dac
.count
&= s
->dma_dac
.fragsize
-1;
1826 spin_unlock_irqrestore(&s
->lock
, flags
);
1827 if (copy_to_user(argp
, &cinfo
, sizeof(cinfo
)))
1831 case SNDCTL_DSP_GETBLKSIZE
:
1832 if (file
->f_mode
& FMODE_WRITE
) {
1833 if ((val
= prog_dmabuf(s
, 0)))
1835 return put_user(s
->dma_dac
.fragsize
, p
);
1837 if ((val
= prog_dmabuf(s
, 1)))
1839 return put_user(s
->dma_adc
.fragsize
, p
);
1841 case SNDCTL_DSP_SETFRAGMENT
:
1842 if (get_user(val
, p
))
1844 if (file
->f_mode
& FMODE_READ
) {
1845 s
->dma_adc
.ossfragshift
= val
& 0xffff;
1846 s
->dma_adc
.ossmaxfrags
= (val
>> 16) & 0xffff;
1847 if (s
->dma_adc
.ossfragshift
< 4)
1848 s
->dma_adc
.ossfragshift
= 4;
1849 if (s
->dma_adc
.ossfragshift
> 15)
1850 s
->dma_adc
.ossfragshift
= 15;
1851 if (s
->dma_adc
.ossmaxfrags
< 4)
1852 s
->dma_adc
.ossmaxfrags
= 4;
1854 if (file
->f_mode
& FMODE_WRITE
) {
1855 s
->dma_dac
.ossfragshift
= val
& 0xffff;
1856 s
->dma_dac
.ossmaxfrags
= (val
>> 16) & 0xffff;
1857 if (s
->dma_dac
.ossfragshift
< 4)
1858 s
->dma_dac
.ossfragshift
= 4;
1859 if (s
->dma_dac
.ossfragshift
> 15)
1860 s
->dma_dac
.ossfragshift
= 15;
1861 if (s
->dma_dac
.ossmaxfrags
< 4)
1862 s
->dma_dac
.ossmaxfrags
= 4;
1866 case SNDCTL_DSP_SUBDIVIDE
:
1867 if ((file
->f_mode
& FMODE_READ
&& s
->dma_adc
.subdivision
) ||
1868 (file
->f_mode
& FMODE_WRITE
&& s
->dma_dac
.subdivision
))
1870 if (get_user(val
, p
))
1872 if (val
!= 1 && val
!= 2 && val
!= 4)
1874 if (file
->f_mode
& FMODE_READ
)
1875 s
->dma_adc
.subdivision
= val
;
1876 if (file
->f_mode
& FMODE_WRITE
)
1877 s
->dma_dac
.subdivision
= val
;
1880 case SOUND_PCM_READ_RATE
:
1881 return put_user((file
->f_mode
& FMODE_READ
) ? s
->rateadc
: s
->ratedac
, p
);
1883 case SOUND_PCM_READ_CHANNELS
:
1884 return put_user((s
->fmt
& ((file
->f_mode
& FMODE_READ
) ? (SV_CFMT_STEREO
<< SV_CFMT_CSHIFT
)
1885 : (SV_CFMT_STEREO
<< SV_CFMT_ASHIFT
))) ? 2 : 1, p
);
1887 case SOUND_PCM_READ_BITS
:
1888 return put_user((s
->fmt
& ((file
->f_mode
& FMODE_READ
) ? (SV_CFMT_16BIT
<< SV_CFMT_CSHIFT
)
1889 : (SV_CFMT_16BIT
<< SV_CFMT_ASHIFT
))) ? 16 : 8, p
);
1891 case SOUND_PCM_WRITE_FILTER
:
1892 case SNDCTL_DSP_SETSYNCRO
:
1893 case SOUND_PCM_READ_FILTER
:
1897 return mixer_ioctl(s
, cmd
, arg
);
1900 static int sv_open(struct inode
*inode
, struct file
*file
)
1902 int minor
= iminor(inode
);
1903 DECLARE_WAITQUEUE(wait
, current
);
1904 unsigned char fmtm
= ~0, fmts
= 0;
1905 struct list_head
*list
;
1908 for (list
= devs
.next
; ; list
= list
->next
) {
1911 s
= list_entry(list
, struct sv_state
, devs
);
1912 if (!((s
->dev_audio
^ minor
) & ~0xf))
1916 file
->private_data
= s
;
1917 /* wait for device to become free */
1919 while (s
->open_mode
& file
->f_mode
) {
1920 if (file
->f_flags
& O_NONBLOCK
) {
1924 add_wait_queue(&s
->open_wait
, &wait
);
1925 __set_current_state(TASK_INTERRUPTIBLE
);
1928 remove_wait_queue(&s
->open_wait
, &wait
);
1929 set_current_state(TASK_RUNNING
);
1930 if (signal_pending(current
))
1931 return -ERESTARTSYS
;
1934 if (file
->f_mode
& FMODE_READ
) {
1935 fmtm
&= ~((SV_CFMT_STEREO
| SV_CFMT_16BIT
) << SV_CFMT_CSHIFT
);
1936 if ((minor
& 0xf) == SND_DEV_DSP16
)
1937 fmts
|= SV_CFMT_16BIT
<< SV_CFMT_CSHIFT
;
1938 s
->dma_adc
.ossfragshift
= s
->dma_adc
.ossmaxfrags
= s
->dma_adc
.subdivision
= 0;
1939 s
->dma_adc
.enabled
= 1;
1940 set_adc_rate(s
, 8000);
1942 if (file
->f_mode
& FMODE_WRITE
) {
1943 fmtm
&= ~((SV_CFMT_STEREO
| SV_CFMT_16BIT
) << SV_CFMT_ASHIFT
);
1944 if ((minor
& 0xf) == SND_DEV_DSP16
)
1945 fmts
|= SV_CFMT_16BIT
<< SV_CFMT_ASHIFT
;
1946 s
->dma_dac
.ossfragshift
= s
->dma_dac
.ossmaxfrags
= s
->dma_dac
.subdivision
= 0;
1947 s
->dma_dac
.enabled
= 1;
1948 set_dac_rate(s
, 8000);
1950 set_fmt(s
, fmtm
, fmts
);
1951 s
->open_mode
|= file
->f_mode
& (FMODE_READ
| FMODE_WRITE
);
1953 return nonseekable_open(inode
, file
);
1956 static int sv_release(struct inode
*inode
, struct file
*file
)
1958 struct sv_state
*s
= (struct sv_state
*)file
->private_data
;
1962 if (file
->f_mode
& FMODE_WRITE
)
1963 drain_dac(s
, file
->f_flags
& O_NONBLOCK
);
1965 if (file
->f_mode
& FMODE_WRITE
) {
1967 dealloc_dmabuf(s
, &s
->dma_dac
);
1969 if (file
->f_mode
& FMODE_READ
) {
1971 dealloc_dmabuf(s
, &s
->dma_adc
);
1973 s
->open_mode
&= ~(file
->f_mode
& (FMODE_READ
|FMODE_WRITE
));
1974 wake_up(&s
->open_wait
);
1980 static /*const*/ struct file_operations sv_audio_fops
= {
1981 .owner
= THIS_MODULE
,
1982 .llseek
= no_llseek
,
1989 .release
= sv_release
,
1992 /* --------------------------------------------------------------------- */
1994 static ssize_t
sv_midi_read(struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
1996 struct sv_state
*s
= (struct sv_state
*)file
->private_data
;
1997 DECLARE_WAITQUEUE(wait
, current
);
1999 unsigned long flags
;
2004 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
2009 add_wait_queue(&s
->midi
.iwait
, &wait
);
2011 spin_lock_irqsave(&s
->lock
, flags
);
2013 cnt
= MIDIINBUF
- ptr
;
2014 if (s
->midi
.icnt
< cnt
)
2017 __set_current_state(TASK_INTERRUPTIBLE
);
2018 spin_unlock_irqrestore(&s
->lock
, flags
);
2022 if (file
->f_flags
& O_NONBLOCK
) {
2028 if (signal_pending(current
)) {
2035 if (copy_to_user(buffer
, s
->midi
.ibuf
+ ptr
, cnt
)) {
2040 ptr
= (ptr
+ cnt
) % MIDIINBUF
;
2041 spin_lock_irqsave(&s
->lock
, flags
);
2043 s
->midi
.icnt
-= cnt
;
2044 spin_unlock_irqrestore(&s
->lock
, flags
);
2050 __set_current_state(TASK_RUNNING
);
2051 remove_wait_queue(&s
->midi
.iwait
, &wait
);
2055 static ssize_t
sv_midi_write(struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
2057 struct sv_state
*s
= (struct sv_state
*)file
->private_data
;
2058 DECLARE_WAITQUEUE(wait
, current
);
2060 unsigned long flags
;
2065 if (!access_ok(VERIFY_READ
, buffer
, count
))
2070 add_wait_queue(&s
->midi
.owait
, &wait
);
2072 spin_lock_irqsave(&s
->lock
, flags
);
2074 cnt
= MIDIOUTBUF
- ptr
;
2075 if (s
->midi
.ocnt
+ cnt
> MIDIOUTBUF
)
2076 cnt
= MIDIOUTBUF
- s
->midi
.ocnt
;
2078 __set_current_state(TASK_INTERRUPTIBLE
);
2081 spin_unlock_irqrestore(&s
->lock
, flags
);
2085 if (file
->f_flags
& O_NONBLOCK
) {
2091 if (signal_pending(current
)) {
2098 if (copy_from_user(s
->midi
.obuf
+ ptr
, buffer
, cnt
)) {
2103 ptr
= (ptr
+ cnt
) % MIDIOUTBUF
;
2104 spin_lock_irqsave(&s
->lock
, flags
);
2106 s
->midi
.ocnt
+= cnt
;
2107 spin_unlock_irqrestore(&s
->lock
, flags
);
2111 spin_lock_irqsave(&s
->lock
, flags
);
2113 spin_unlock_irqrestore(&s
->lock
, flags
);
2115 __set_current_state(TASK_RUNNING
);
2116 remove_wait_queue(&s
->midi
.owait
, &wait
);
2120 /* No kernel lock - we have our own spinlock */
2121 static unsigned int sv_midi_poll(struct file
*file
, struct poll_table_struct
*wait
)
2123 struct sv_state
*s
= (struct sv_state
*)file
->private_data
;
2124 unsigned long flags
;
2125 unsigned int mask
= 0;
2128 if (file
->f_mode
& FMODE_WRITE
)
2129 poll_wait(file
, &s
->midi
.owait
, wait
);
2130 if (file
->f_mode
& FMODE_READ
)
2131 poll_wait(file
, &s
->midi
.iwait
, wait
);
2132 spin_lock_irqsave(&s
->lock
, flags
);
2133 if (file
->f_mode
& FMODE_READ
) {
2134 if (s
->midi
.icnt
> 0)
2135 mask
|= POLLIN
| POLLRDNORM
;
2137 if (file
->f_mode
& FMODE_WRITE
) {
2138 if (s
->midi
.ocnt
< MIDIOUTBUF
)
2139 mask
|= POLLOUT
| POLLWRNORM
;
2141 spin_unlock_irqrestore(&s
->lock
, flags
);
2145 static int sv_midi_open(struct inode
*inode
, struct file
*file
)
2147 int minor
= iminor(inode
);
2148 DECLARE_WAITQUEUE(wait
, current
);
2149 unsigned long flags
;
2150 struct list_head
*list
;
2153 for (list
= devs
.next
; ; list
= list
->next
) {
2156 s
= list_entry(list
, struct sv_state
, devs
);
2157 if (s
->dev_midi
== minor
)
2161 file
->private_data
= s
;
2162 /* wait for device to become free */
2164 while (s
->open_mode
& (file
->f_mode
<< FMODE_MIDI_SHIFT
)) {
2165 if (file
->f_flags
& O_NONBLOCK
) {
2169 add_wait_queue(&s
->open_wait
, &wait
);
2170 __set_current_state(TASK_INTERRUPTIBLE
);
2173 remove_wait_queue(&s
->open_wait
, &wait
);
2174 set_current_state(TASK_RUNNING
);
2175 if (signal_pending(current
))
2176 return -ERESTARTSYS
;
2179 spin_lock_irqsave(&s
->lock
, flags
);
2180 if (!(s
->open_mode
& (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
))) {
2181 s
->midi
.ird
= s
->midi
.iwr
= s
->midi
.icnt
= 0;
2182 s
->midi
.ord
= s
->midi
.owr
= s
->midi
.ocnt
= 0;
2183 //outb(inb(s->ioenh + SV_CODEC_CONTROL) | SV_CCTRL_WAVETABLE, s->ioenh + SV_CODEC_CONTROL);
2184 outb(inb(s
->ioenh
+ SV_CODEC_INTMASK
) | SV_CINTMASK_MIDI
, s
->ioenh
+ SV_CODEC_INTMASK
);
2185 wrindir(s
, SV_CIUARTCONTROL
, 5); /* output MIDI data to external and internal synth */
2186 wrindir(s
, SV_CIWAVETABLESRC
, 1); /* Wavetable in PC RAM */
2187 outb(0xff, s
->iomidi
+1); /* reset command */
2188 outb(0x3f, s
->iomidi
+1); /* uart command */
2189 if (!(inb(s
->iomidi
+1) & 0x80))
2191 s
->midi
.ird
= s
->midi
.iwr
= s
->midi
.icnt
= 0;
2192 init_timer(&s
->midi
.timer
);
2193 s
->midi
.timer
.expires
= jiffies
+1;
2194 s
->midi
.timer
.data
= (unsigned long)s
;
2195 s
->midi
.timer
.function
= sv_midi_timer
;
2196 add_timer(&s
->midi
.timer
);
2198 if (file
->f_mode
& FMODE_READ
) {
2199 s
->midi
.ird
= s
->midi
.iwr
= s
->midi
.icnt
= 0;
2201 if (file
->f_mode
& FMODE_WRITE
) {
2202 s
->midi
.ord
= s
->midi
.owr
= s
->midi
.ocnt
= 0;
2204 spin_unlock_irqrestore(&s
->lock
, flags
);
2205 s
->open_mode
|= (file
->f_mode
<< FMODE_MIDI_SHIFT
) & (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
);
2207 return nonseekable_open(inode
, file
);
2210 static int sv_midi_release(struct inode
*inode
, struct file
*file
)
2212 struct sv_state
*s
= (struct sv_state
*)file
->private_data
;
2213 DECLARE_WAITQUEUE(wait
, current
);
2214 unsigned long flags
;
2215 unsigned count
, tmo
;
2220 if (file
->f_mode
& FMODE_WRITE
) {
2221 add_wait_queue(&s
->midi
.owait
, &wait
);
2223 __set_current_state(TASK_INTERRUPTIBLE
);
2224 spin_lock_irqsave(&s
->lock
, flags
);
2225 count
= s
->midi
.ocnt
;
2226 spin_unlock_irqrestore(&s
->lock
, flags
);
2229 if (signal_pending(current
))
2231 if (file
->f_flags
& O_NONBLOCK
) {
2232 remove_wait_queue(&s
->midi
.owait
, &wait
);
2233 set_current_state(TASK_RUNNING
);
2237 tmo
= (count
* HZ
) / 3100;
2238 if (!schedule_timeout(tmo
? : 1) && tmo
)
2239 printk(KERN_DEBUG
"sv: midi timed out??\n");
2241 remove_wait_queue(&s
->midi
.owait
, &wait
);
2242 set_current_state(TASK_RUNNING
);
2245 s
->open_mode
&= ~((file
->f_mode
<< FMODE_MIDI_SHIFT
) & (FMODE_MIDI_READ
|FMODE_MIDI_WRITE
));
2246 spin_lock_irqsave(&s
->lock
, flags
);
2247 if (!(s
->open_mode
& (FMODE_MIDI_READ
| FMODE_MIDI_WRITE
))) {
2248 outb(inb(s
->ioenh
+ SV_CODEC_INTMASK
) & ~SV_CINTMASK_MIDI
, s
->ioenh
+ SV_CODEC_INTMASK
);
2249 del_timer(&s
->midi
.timer
);
2251 spin_unlock_irqrestore(&s
->lock
, flags
);
2252 wake_up(&s
->open_wait
);
2258 static /*const*/ struct file_operations sv_midi_fops
= {
2259 .owner
= THIS_MODULE
,
2260 .llseek
= no_llseek
,
2261 .read
= sv_midi_read
,
2262 .write
= sv_midi_write
,
2263 .poll
= sv_midi_poll
,
2264 .open
= sv_midi_open
,
2265 .release
= sv_midi_release
,
2268 /* --------------------------------------------------------------------- */
2270 static int sv_dmfm_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
2272 static const unsigned char op_offset
[18] = {
2273 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
2274 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
2275 0x10, 0x11, 0x12, 0x13, 0x14, 0x15
2277 struct sv_state
*s
= (struct sv_state
*)file
->private_data
;
2278 struct dm_fm_voice v
;
2279 struct dm_fm_note n
;
2280 struct dm_fm_params p
;
2285 case FM_IOCTL_RESET
:
2286 for (regb
= 0xb0; regb
< 0xb9; regb
++) {
2287 outb(regb
, s
->iosynth
);
2288 outb(0, s
->iosynth
+1);
2289 outb(regb
, s
->iosynth
+2);
2290 outb(0, s
->iosynth
+3);
2294 case FM_IOCTL_PLAY_NOTE
:
2295 if (copy_from_user(&n
, (void __user
*)arg
, sizeof(n
)))
2306 outb(0xa0 + regb
, io
);
2307 outb(n
.fnum
& 0xff, io
+1);
2308 outb(0xb0 + regb
, io
);
2309 outb(((n
.fnum
>> 8) & 3) | ((n
.octave
& 7) << 2) | ((n
.key_on
& 1) << 5), io
+1);
2312 case FM_IOCTL_SET_VOICE
:
2313 if (copy_from_user(&v
, (void __user
*)arg
, sizeof(v
)))
2317 regb
= op_offset
[v
.voice
];
2318 io
= s
->iosynth
+ ((v
.op
& 1) << 1);
2319 outb(0x20 + regb
, io
);
2320 outb(((v
.am
& 1) << 7) | ((v
.vibrato
& 1) << 6) | ((v
.do_sustain
& 1) << 5) |
2321 ((v
.kbd_scale
& 1) << 4) | (v
.harmonic
& 0xf), io
+1);
2322 outb(0x40 + regb
, io
);
2323 outb(((v
.scale_level
& 0x3) << 6) | (v
.volume
& 0x3f), io
+1);
2324 outb(0x60 + regb
, io
);
2325 outb(((v
.attack
& 0xf) << 4) | (v
.decay
& 0xf), io
+1);
2326 outb(0x80 + regb
, io
);
2327 outb(((v
.sustain
& 0xf) << 4) | (v
.release
& 0xf), io
+1);
2328 outb(0xe0 + regb
, io
);
2329 outb(v
.waveform
& 0x7, io
+1);
2337 outb(0xc0 + regb
, io
);
2338 outb(((v
.right
& 1) << 5) | ((v
.left
& 1) << 4) | ((v
.feedback
& 7) << 1) |
2339 (v
.connection
& 1), io
+1);
2342 case FM_IOCTL_SET_PARAMS
:
2343 if (copy_from_user(&p
, (void *__user
)arg
, sizeof(p
)))
2345 outb(0x08, s
->iosynth
);
2346 outb((p
.kbd_split
& 1) << 6, s
->iosynth
+1);
2347 outb(0xbd, s
->iosynth
);
2348 outb(((p
.am_depth
& 1) << 7) | ((p
.vib_depth
& 1) << 6) | ((p
.rhythm
& 1) << 5) | ((p
.bass
& 1) << 4) |
2349 ((p
.snare
& 1) << 3) | ((p
.tomtom
& 1) << 2) | ((p
.cymbal
& 1) << 1) | (p
.hihat
& 1), s
->iosynth
+1);
2352 case FM_IOCTL_SET_OPL
:
2353 outb(4, s
->iosynth
+2);
2354 outb(arg
, s
->iosynth
+3);
2357 case FM_IOCTL_SET_MODE
:
2358 outb(5, s
->iosynth
+2);
2359 outb(arg
& 1, s
->iosynth
+3);
2367 static int sv_dmfm_open(struct inode
*inode
, struct file
*file
)
2369 int minor
= iminor(inode
);
2370 DECLARE_WAITQUEUE(wait
, current
);
2371 struct list_head
*list
;
2374 for (list
= devs
.next
; ; list
= list
->next
) {
2377 s
= list_entry(list
, struct sv_state
, devs
);
2378 if (s
->dev_dmfm
== minor
)
2382 file
->private_data
= s
;
2383 /* wait for device to become free */
2385 while (s
->open_mode
& FMODE_DMFM
) {
2386 if (file
->f_flags
& O_NONBLOCK
) {
2390 add_wait_queue(&s
->open_wait
, &wait
);
2391 __set_current_state(TASK_INTERRUPTIBLE
);
2394 remove_wait_queue(&s
->open_wait
, &wait
);
2395 set_current_state(TASK_RUNNING
);
2396 if (signal_pending(current
))
2397 return -ERESTARTSYS
;
2400 /* init the stuff */
2401 outb(1, s
->iosynth
);
2402 outb(0x20, s
->iosynth
+1); /* enable waveforms */
2403 outb(4, s
->iosynth
+2);
2404 outb(0, s
->iosynth
+3); /* no 4op enabled */
2405 outb(5, s
->iosynth
+2);
2406 outb(1, s
->iosynth
+3); /* enable OPL3 */
2407 s
->open_mode
|= FMODE_DMFM
;
2409 return nonseekable_open(inode
, file
);
2412 static int sv_dmfm_release(struct inode
*inode
, struct file
*file
)
2414 struct sv_state
*s
= (struct sv_state
*)file
->private_data
;
2420 s
->open_mode
&= ~FMODE_DMFM
;
2421 for (regb
= 0xb0; regb
< 0xb9; regb
++) {
2422 outb(regb
, s
->iosynth
);
2423 outb(0, s
->iosynth
+1);
2424 outb(regb
, s
->iosynth
+2);
2425 outb(0, s
->iosynth
+3);
2427 wake_up(&s
->open_wait
);
2433 static /*const*/ struct file_operations sv_dmfm_fops
= {
2434 .owner
= THIS_MODULE
,
2435 .llseek
= no_llseek
,
2436 .ioctl
= sv_dmfm_ioctl
,
2437 .open
= sv_dmfm_open
,
2438 .release
= sv_dmfm_release
,
2441 /* --------------------------------------------------------------------- */
2443 /* maximum number of devices; only used for command line params */
2446 static int reverb
[NR_DEVICE
];
2449 static int wavetable
[NR_DEVICE
];
2452 static unsigned int devindex
;
2454 MODULE_PARM(reverb
, "1-" __MODULE_STRING(NR_DEVICE
) "i");
2455 MODULE_PARM_DESC(reverb
, "if 1 enables the reverb circuitry. NOTE: your card must have the reverb RAM");
2457 MODULE_PARM(wavetable
, "1-" __MODULE_STRING(NR_DEVICE
) "i");
2458 MODULE_PARM_DESC(wavetable
, "if 1 the wavetable synth is enabled");
2461 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
2462 MODULE_DESCRIPTION("S3 SonicVibes Driver");
2463 MODULE_LICENSE("GPL");
2466 /* --------------------------------------------------------------------- */
2468 static struct initvol
{
2471 } initvol
[] __initdata
= {
2472 { SOUND_MIXER_WRITE_RECLEV
, 0x4040 },
2473 { SOUND_MIXER_WRITE_LINE1
, 0x4040 },
2474 { SOUND_MIXER_WRITE_CD
, 0x4040 },
2475 { SOUND_MIXER_WRITE_LINE
, 0x4040 },
2476 { SOUND_MIXER_WRITE_MIC
, 0x4040 },
2477 { SOUND_MIXER_WRITE_SYNTH
, 0x4040 },
2478 { SOUND_MIXER_WRITE_LINE2
, 0x4040 },
2479 { SOUND_MIXER_WRITE_VOLUME
, 0x4040 },
2480 { SOUND_MIXER_WRITE_PCM
, 0x4040 }
2483 #define RSRCISIOREGION(dev,num) (pci_resource_start((dev), (num)) != 0 && \
2484 (pci_resource_flags((dev), (num)) & IORESOURCE_IO))
2486 static int __devinit
sv_probe(struct pci_dev
*pcidev
, const struct pci_device_id
*pciid
)
2488 static char __initdata sv_ddma_name
[] = "S3 Inc. SonicVibes DDMA Controller";
2493 unsigned ddmanamelen
;
2495 if ((ret
=pci_enable_device(pcidev
)))
2498 if (!RSRCISIOREGION(pcidev
, RESOURCE_SB
) ||
2499 !RSRCISIOREGION(pcidev
, RESOURCE_ENH
) ||
2500 !RSRCISIOREGION(pcidev
, RESOURCE_SYNTH
) ||
2501 !RSRCISIOREGION(pcidev
, RESOURCE_MIDI
) ||
2502 !RSRCISIOREGION(pcidev
, RESOURCE_GAME
))
2504 if (pcidev
->irq
== 0)
2506 if (pci_set_dma_mask(pcidev
, 0x00ffffff)) {
2507 printk(KERN_WARNING
"sonicvibes: architecture does not support 24bit PCI busmaster DMA\n");
2510 /* try to allocate a DDMA resource if not already available */
2511 if (!RSRCISIOREGION(pcidev
, RESOURCE_DDMA
)) {
2512 pcidev
->resource
[RESOURCE_DDMA
].start
= 0;
2513 pcidev
->resource
[RESOURCE_DDMA
].end
= 2*SV_EXTENT_DMA
-1;
2514 pcidev
->resource
[RESOURCE_DDMA
].flags
= PCI_BASE_ADDRESS_SPACE_IO
| IORESOURCE_IO
;
2515 ddmanamelen
= strlen(sv_ddma_name
)+1;
2516 if (!(ddmaname
= kmalloc(ddmanamelen
, GFP_KERNEL
)))
2518 memcpy(ddmaname
, sv_ddma_name
, ddmanamelen
);
2519 pcidev
->resource
[RESOURCE_DDMA
].name
= ddmaname
;
2520 if (pci_assign_resource(pcidev
, RESOURCE_DDMA
)) {
2521 pcidev
->resource
[RESOURCE_DDMA
].name
= NULL
;
2523 printk(KERN_ERR
"sv: cannot allocate DDMA controller io ports\n");
2527 if (!(s
= kmalloc(sizeof(struct sv_state
), GFP_KERNEL
))) {
2528 printk(KERN_WARNING
"sv: out of memory\n");
2531 memset(s
, 0, sizeof(struct sv_state
));
2532 init_waitqueue_head(&s
->dma_adc
.wait
);
2533 init_waitqueue_head(&s
->dma_dac
.wait
);
2534 init_waitqueue_head(&s
->open_wait
);
2535 init_waitqueue_head(&s
->midi
.iwait
);
2536 init_waitqueue_head(&s
->midi
.owait
);
2537 init_MUTEX(&s
->open_sem
);
2538 spin_lock_init(&s
->lock
);
2539 s
->magic
= SV_MAGIC
;
2541 s
->iosb
= pci_resource_start(pcidev
, RESOURCE_SB
);
2542 s
->ioenh
= pci_resource_start(pcidev
, RESOURCE_ENH
);
2543 s
->iosynth
= pci_resource_start(pcidev
, RESOURCE_SYNTH
);
2544 s
->iomidi
= pci_resource_start(pcidev
, RESOURCE_MIDI
);
2545 s
->iodmaa
= pci_resource_start(pcidev
, RESOURCE_DDMA
);
2546 s
->iodmac
= pci_resource_start(pcidev
, RESOURCE_DDMA
) + SV_EXTENT_DMA
;
2547 s
->gameport
.io
= pci_resource_start(pcidev
, RESOURCE_GAME
);
2548 pci_write_config_dword(pcidev
, 0x40, s
->iodmaa
| 9); /* enable and use extended mode */
2549 pci_write_config_dword(pcidev
, 0x48, s
->iodmac
| 9); /* enable */
2550 printk(KERN_DEBUG
"sv: io ports: %#lx %#lx %#lx %#lx %#x %#x %#x\n",
2551 s
->iosb
, s
->ioenh
, s
->iosynth
, s
->iomidi
, s
->gameport
.io
, s
->iodmaa
, s
->iodmac
);
2552 s
->irq
= pcidev
->irq
;
2555 pci_write_config_dword(pcidev
, 0x60, wavetable_mem
>> 12); /* wavetable base address */
2558 if (!request_region(s
->ioenh
, SV_EXTENT_ENH
, "S3 SonicVibes PCM")) {
2559 printk(KERN_ERR
"sv: io ports %#lx-%#lx in use\n", s
->ioenh
, s
->ioenh
+SV_EXTENT_ENH
-1);
2562 if (!request_region(s
->iodmaa
, SV_EXTENT_DMA
, "S3 SonicVibes DMAA")) {
2563 printk(KERN_ERR
"sv: io ports %#x-%#x in use\n", s
->iodmaa
, s
->iodmaa
+SV_EXTENT_DMA
-1);
2566 if (!request_region(s
->iodmac
, SV_EXTENT_DMA
, "S3 SonicVibes DMAC")) {
2567 printk(KERN_ERR
"sv: io ports %#x-%#x in use\n", s
->iodmac
, s
->iodmac
+SV_EXTENT_DMA
-1);
2570 if (!request_region(s
->iomidi
, SV_EXTENT_MIDI
, "S3 SonicVibes Midi")) {
2571 printk(KERN_ERR
"sv: io ports %#lx-%#lx in use\n", s
->iomidi
, s
->iomidi
+SV_EXTENT_MIDI
-1);
2574 if (!request_region(s
->iosynth
, SV_EXTENT_SYNTH
, "S3 SonicVibes Synth")) {
2575 printk(KERN_ERR
"sv: io ports %#lx-%#lx in use\n", s
->iosynth
, s
->iosynth
+SV_EXTENT_SYNTH
-1);
2578 if (s
->gameport
.io
&& !request_region(s
->gameport
.io
, SV_EXTENT_GAME
, "ESS Solo1")) {
2579 printk(KERN_ERR
"sv: gameport io ports in use\n");
2582 /* initialize codec registers */
2583 outb(0x80, s
->ioenh
+ SV_CODEC_CONTROL
); /* assert reset */
2585 outb(0x00, s
->ioenh
+ SV_CODEC_CONTROL
); /* deassert reset */
2587 outb(SV_CCTRL_INTADRIVE
| SV_CCTRL_ENHANCED
/*| SV_CCTRL_WAVETABLE */
2588 | (reverb
[devindex
] ? SV_CCTRL_REVERB
: 0), s
->ioenh
+ SV_CODEC_CONTROL
);
2589 inb(s
->ioenh
+ SV_CODEC_STATUS
); /* clear ints */
2590 wrindir(s
, SV_CIDRIVECONTROL
, 0); /* drive current 16mA */
2591 wrindir(s
, SV_CIENABLE
, s
->enable
= 0); /* disable DMAA and DMAC */
2592 outb(~(SV_CINTMASK_DMAA
| SV_CINTMASK_DMAC
), s
->ioenh
+ SV_CODEC_INTMASK
);
2593 /* outb(0xff, s->iodmaa + SV_DMA_RESET); */
2594 /* outb(0xff, s->iodmac + SV_DMA_RESET); */
2595 inb(s
->ioenh
+ SV_CODEC_STATUS
); /* ack interrupts */
2596 wrindir(s
, SV_CIADCCLKSOURCE
, 0); /* use pll as ADC clock source */
2597 wrindir(s
, SV_CIANALOGPWRDOWN
, 0); /* power up the analog parts of the device */
2598 wrindir(s
, SV_CIDIGITALPWRDOWN
, 0); /* power up the digital parts of the device */
2599 setpll(s
, SV_CIADCPLLM
, 8000);
2600 wrindir(s
, SV_CISRSSPACE
, 0x80); /* SRS off */
2601 wrindir(s
, SV_CIPCMSR0
, (8000 * 65536 / FULLRATE
) & 0xff);
2602 wrindir(s
, SV_CIPCMSR1
, ((8000 * 65536 / FULLRATE
) >> 8) & 0xff);
2603 wrindir(s
, SV_CIADCOUTPUT
, 0);
2605 if ((ret
=request_irq(s
->irq
,sv_interrupt
,SA_SHIRQ
,"S3 SonicVibes",s
))) {
2606 printk(KERN_ERR
"sv: irq %u in use\n", s
->irq
);
2609 printk(KERN_INFO
"sv: found adapter at io %#lx irq %u dmaa %#06x dmac %#06x revision %u\n",
2610 s
->ioenh
, s
->irq
, s
->iodmaa
, s
->iodmac
, rdindir(s
, SV_CIREVISION
));
2611 /* register devices */
2612 if ((s
->dev_audio
= register_sound_dsp(&sv_audio_fops
, -1)) < 0) {
2616 if ((s
->dev_mixer
= register_sound_mixer(&sv_mixer_fops
, -1)) < 0) {
2620 if ((s
->dev_midi
= register_sound_midi(&sv_midi_fops
, -1)) < 0) {
2624 if ((s
->dev_dmfm
= register_sound_special(&sv_dmfm_fops
, 15 /* ?? */)) < 0) {
2628 pci_set_master(pcidev
); /* enable bus mastering */
2629 /* initialize the chips */
2632 val
= SOUND_MASK_LINE
|SOUND_MASK_SYNTH
;
2633 mixer_ioctl(s
, SOUND_MIXER_WRITE_RECSRC
, (unsigned long)&val
);
2634 for (i
= 0; i
< sizeof(initvol
)/sizeof(initvol
[0]); i
++) {
2635 val
= initvol
[i
].vol
;
2636 mixer_ioctl(s
, initvol
[i
].mixch
, (unsigned long)&val
);
2639 /* register gameport */
2640 gameport_register_port(&s
->gameport
);
2641 /* store it in the driver field */
2642 pci_set_drvdata(pcidev
, s
);
2643 /* put it into driver list */
2644 list_add_tail(&s
->devs
, &devs
);
2645 /* increment devindex */
2646 if (devindex
< NR_DEVICE
-1)
2651 unregister_sound_midi(s
->dev_midi
);
2653 unregister_sound_mixer(s
->dev_mixer
);
2655 unregister_sound_dsp(s
->dev_audio
);
2657 printk(KERN_ERR
"sv: cannot register misc device\n");
2658 free_irq(s
->irq
, s
);
2661 release_region(s
->gameport
.io
, SV_EXTENT_GAME
);
2662 release_region(s
->iosynth
, SV_EXTENT_SYNTH
);
2664 release_region(s
->iomidi
, SV_EXTENT_MIDI
);
2666 release_region(s
->iodmac
, SV_EXTENT_DMA
);
2668 release_region(s
->iodmaa
, SV_EXTENT_DMA
);
2670 release_region(s
->ioenh
, SV_EXTENT_ENH
);
2676 static void __devexit
sv_remove(struct pci_dev
*dev
)
2678 struct sv_state
*s
= pci_get_drvdata(dev
);
2683 outb(~0, s
->ioenh
+ SV_CODEC_INTMASK
); /* disable ints */
2684 synchronize_irq(s
->irq
);
2685 inb(s
->ioenh
+ SV_CODEC_STATUS
); /* ack interrupts */
2686 wrindir(s
, SV_CIENABLE
, 0); /* disable DMAA and DMAC */
2687 /*outb(0, s->iodmaa + SV_DMA_RESET);*/
2688 /*outb(0, s->iodmac + SV_DMA_RESET);*/
2689 free_irq(s
->irq
, s
);
2690 if (s
->gameport
.io
) {
2691 gameport_unregister_port(&s
->gameport
);
2692 release_region(s
->gameport
.io
, SV_EXTENT_GAME
);
2694 release_region(s
->iodmac
, SV_EXTENT_DMA
);
2695 release_region(s
->iodmaa
, SV_EXTENT_DMA
);
2696 release_region(s
->ioenh
, SV_EXTENT_ENH
);
2697 release_region(s
->iomidi
, SV_EXTENT_MIDI
);
2698 release_region(s
->iosynth
, SV_EXTENT_SYNTH
);
2699 unregister_sound_dsp(s
->dev_audio
);
2700 unregister_sound_mixer(s
->dev_mixer
);
2701 unregister_sound_midi(s
->dev_midi
);
2702 unregister_sound_special(s
->dev_dmfm
);
2704 pci_set_drvdata(dev
, NULL
);
2707 static struct pci_device_id id_table
[] = {
2708 { PCI_VENDOR_ID_S3
, PCI_DEVICE_ID_S3_SONICVIBES
, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0 },
2712 MODULE_DEVICE_TABLE(pci
, id_table
);
2714 static struct pci_driver sv_driver
= {
2715 .name
= "sonicvibes",
2716 .id_table
= id_table
,
2718 .remove
= __devexit_p(sv_remove
),
2721 static int __init
init_sonicvibes(void)
2723 printk(KERN_INFO
"sv: version v0.31 time " __TIME__
" " __DATE__
"\n");
2725 if (!(wavetable_mem
= __get_free_pages(GFP_KERNEL
, 20-PAGE_SHIFT
)))
2726 printk(KERN_INFO
"sv: cannot allocate 1MB of contiguous nonpageable memory for wavetable data\n");
2728 return pci_module_init(&sv_driver
);
2731 static void __exit
cleanup_sonicvibes(void)
2733 printk(KERN_INFO
"sv: unloading\n");
2734 pci_unregister_driver(&sv_driver
);
2736 free_pages(wavetable_mem
, 20-PAGE_SHIFT
);
2739 module_init(init_sonicvibes
);
2740 module_exit(cleanup_sonicvibes
);
2742 /* --------------------------------------------------------------------- */
2746 /* format is: sonicvibes=[reverb] sonicvibesdmaio=dmaioaddr */
2748 static int __init
sonicvibes_setup(char *str
)
2750 static unsigned __initdata nr_dev
= 0;
2752 if (nr_dev
>= NR_DEVICE
)
2755 if (get_option(&str
, &reverb
[nr_dev
]) == 2)
2756 (void)get_option(&str
, &wavetable
[nr_dev
]);
2758 (void)get_option(&str
, &reverb
[nr_dev
]);
2765 __setup("sonicvibes=", sonicvibes_setup
);