2 * cmpci.c -- C-Media PCI audio driver.
4 * Copyright (C) 1999 C-media support (support@cmedia.com.tw)
6 * Based on the PCI drivers by Thomas Sailer (sailer@ife.ee.ethz.ch)
9 * http://www.cmedia.com.tw
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Special thanks to David C. Niemi, Jan Pfeifer
28 * Module command line parameters:
33 * /dev/dsp standard /dev/dsp device, (mostly) OSS compatible
34 * /dev/mixer standard /dev/mixer device, (mostly) OSS compatible
35 * /dev/midi simple MIDI UART interface, no ioctl
37 * The card has both an FM and a Wavetable synth, but I have to figure
38 * out first how to drive them...
41 * 06.05.98 0.1 Initial release
42 * 10.05.98 0.2 Fixed many bugs, esp. ADC rate calculation
43 * First stab at a simple midi interface (no bells&whistles)
44 * 13.05.98 0.3 Fix stupid cut&paste error: set_adc_rate was called instead of
45 * set_dac_rate in the FMODE_WRITE case in cm_open
46 * Fix hwptr out of bounds (now mpg123 works)
47 * 14.05.98 0.4 Don't allow excessive interrupt rates
48 * 08.06.98 0.5 First release using Alan Cox' soundcore instead of miscdevice
49 * 03.08.98 0.6 Do not include modversions.h
50 * Now mixer behaviour can basically be selected between
51 * "OSS documented" and "OSS actual" behaviour
52 * 31.08.98 0.7 Fix realplayer problems - dac.count issues
53 * 10.12.98 0.8 Fix drain_dac trying to wait on not yet initialized DMA
54 * 16.12.98 0.9 Fix a few f_file & FMODE_ bugs
55 * 06.01.99 0.10 remove the silly SA_INTERRUPT flag.
56 * hopefully killed the egcs section type conflict
57 * 12.03.99 0.11 cinfo.blocks should be reset after GETxPTR ioctl.
58 * reported by Johan Maes <joma@telindus.be>
59 * 22.03.99 0.12 return EAGAIN instead of EBUSY when O_NONBLOCK
60 * read/write cannot be executed
61 * 18.08.99 1.5 Only deallocate DMA buffer when unloading.
62 * 02.09.99 1.6 Enable SPDIF LOOP
63 * Change the mixer read back
64 * 21.09.99 2.33 Use RCS version as driver version.
65 * Add support for modem, S/PDIF loop and 4 channels.
67 * Fix bug cause x11amp cannot play.
70 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
71 * 18/05/2001 - .bss nitpicks, fix a bug in set_dac_channels where it
72 * was calling prog_dmabuf with s->lock held, call missing
73 * unlock_kernel in cm_midi_release
74 * 08/10/2001 - use set_current_state in some more places
76 * Carlos Eduardo Gorges <carlos@techlinux.com.br>
78 * - SMP support ( spin[un]lock* revision )
79 * - speaker mixer support
81 * - optimizations and cleanups
83 * 03/01/2003 - open_mode fixes from Georg Acher <acher@in.tum.de>
84 * Simon Braunschmidt <brasimon@web.de>
86 * - provide support for opl3 FM by releasing IO range after initialization
88 * ChenLi Tien <cltien@cmedia.com.tw>
90 * - Fix S/PDIF out if spdif_loop enabled
91 * - Load opl3 driver if enabled (fmio in proper range)
92 * - Load mpu401 if enabled (mpuio in proper range)
94 * - Fix DUAL_DAC dma synchronization bug
95 * - Check exist FM/MPU401 I/O before activate.
96 * - Add AFTM_S16_BE format support, so MPlayer/Xine can play AC3/mutlichannel
98 * - Change to support kernel 2.6 so only small patch needed
99 * - All parameters default to 0
100 * - Add spdif_out to send PCM through S/PDIF out jack
101 * - Add hw_copy to get 4-spaker output for general PCM/analog output
103 * Stefan Thater <stefan.thaeter@gmx.de>
105 * - Fix mute single channel for CD/Line-in/AUX-in
107 /*****************************************************************************/
109 #include <linux/config.h>
110 #include <linux/module.h>
111 #include <linux/string.h>
112 #include <linux/interrupt.h>
113 #include <linux/ioport.h>
114 #include <linux/sched.h>
115 #include <linux/delay.h>
116 #include <linux/sound.h>
117 #include <linux/slab.h>
118 #include <linux/soundcard.h>
119 #include <linux/pci.h>
120 #include <linux/init.h>
121 #include <linux/poll.h>
122 #include <linux/spinlock.h>
123 #include <linux/smp_lock.h>
124 #include <linux/bitops.h>
125 #include <linux/wait.h>
126 #include <linux/dma-mapping.h>
129 #include <asm/page.h>
130 #include <asm/uaccess.h>
132 #ifdef CONFIG_SOUND_CMPCI_MIDI
133 #include "sound_config.h"
136 #ifdef CONFIG_SOUND_CMPCI_FM
139 #ifdef CONFIG_SOUND_CMPCI_JOYSTICK
140 #include <linux/gameport.h>
141 #include <linux/mutex.h>
145 /* --------------------------------------------------------------------- */
146 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
149 /* --------------------------------------------------------------------- */
151 #define CM_MAGIC ((PCI_VENDOR_ID_CMEDIA<<16)|PCI_DEVICE_ID_CMEDIA_CM8338A)
153 /* CM8338 registers definition ****************/
155 #define CODEC_CMI_FUNCTRL0 (0x00)
156 #define CODEC_CMI_FUNCTRL1 (0x04)
157 #define CODEC_CMI_CHFORMAT (0x08)
158 #define CODEC_CMI_INT_HLDCLR (0x0C)
159 #define CODEC_CMI_INT_STATUS (0x10)
160 #define CODEC_CMI_LEGACY_CTRL (0x14)
161 #define CODEC_CMI_MISC_CTRL (0x18)
162 #define CODEC_CMI_TDMA_POS (0x1C)
163 #define CODEC_CMI_MIXER (0x20)
164 #define CODEC_SB16_DATA (0x22)
165 #define CODEC_SB16_ADDR (0x23)
166 #define CODEC_CMI_MIXER1 (0x24)
167 #define CODEC_CMI_MIXER2 (0x25)
168 #define CODEC_CMI_AUX_VOL (0x26)
169 #define CODEC_CMI_MISC (0x27)
170 #define CODEC_CMI_AC97 (0x28)
172 #define CODEC_CMI_CH0_FRAME1 (0x80)
173 #define CODEC_CMI_CH0_FRAME2 (0x84)
174 #define CODEC_CMI_CH1_FRAME1 (0x88)
175 #define CODEC_CMI_CH1_FRAME2 (0x8C)
177 #define CODEC_CMI_SPDIF_CTRL (0x90)
178 #define CODEC_CMI_MISC_CTRL2 (0x92)
180 #define CODEC_CMI_EXT_REG (0xF0)
182 /* Mixer registers for SB16 ******************/
184 #define DSP_MIX_DATARESETIDX ((unsigned char)(0x00))
186 #define DSP_MIX_MASTERVOLIDX_L ((unsigned char)(0x30))
187 #define DSP_MIX_MASTERVOLIDX_R ((unsigned char)(0x31))
188 #define DSP_MIX_VOICEVOLIDX_L ((unsigned char)(0x32))
189 #define DSP_MIX_VOICEVOLIDX_R ((unsigned char)(0x33))
190 #define DSP_MIX_FMVOLIDX_L ((unsigned char)(0x34))
191 #define DSP_MIX_FMVOLIDX_R ((unsigned char)(0x35))
192 #define DSP_MIX_CDVOLIDX_L ((unsigned char)(0x36))
193 #define DSP_MIX_CDVOLIDX_R ((unsigned char)(0x37))
194 #define DSP_MIX_LINEVOLIDX_L ((unsigned char)(0x38))
195 #define DSP_MIX_LINEVOLIDX_R ((unsigned char)(0x39))
197 #define DSP_MIX_MICVOLIDX ((unsigned char)(0x3A))
198 #define DSP_MIX_SPKRVOLIDX ((unsigned char)(0x3B))
200 #define DSP_MIX_OUTMIXIDX ((unsigned char)(0x3C))
202 #define DSP_MIX_ADCMIXIDX_L ((unsigned char)(0x3D))
203 #define DSP_MIX_ADCMIXIDX_R ((unsigned char)(0x3E))
205 #define DSP_MIX_INGAINIDX_L ((unsigned char)(0x3F))
206 #define DSP_MIX_INGAINIDX_R ((unsigned char)(0x40))
207 #define DSP_MIX_OUTGAINIDX_L ((unsigned char)(0x41))
208 #define DSP_MIX_OUTGAINIDX_R ((unsigned char)(0x42))
210 #define DSP_MIX_AGCIDX ((unsigned char)(0x43))
212 #define DSP_MIX_TREBLEIDX_L ((unsigned char)(0x44))
213 #define DSP_MIX_TREBLEIDX_R ((unsigned char)(0x45))
214 #define DSP_MIX_BASSIDX_L ((unsigned char)(0x46))
215 #define DSP_MIX_BASSIDX_R ((unsigned char)(0x47))
216 #define DSP_MIX_EXTENSION ((unsigned char)(0xf0))
217 // pseudo register for AUX
218 #define DSP_MIX_AUXVOL_L ((unsigned char)(0x50))
219 #define DSP_MIX_AUXVOL_R ((unsigned char)(0x51))
222 #define CM_EXTENT_CODEC 0x100
223 #define CM_EXTENT_MIDI 0x2
224 #define CM_EXTENT_SYNTH 0x4
225 #define CM_EXTENT_GAME 0x8
227 // Function Control Register 0 (00h)
233 // Function Control Register 0+2 (02h)
239 // Function Control Register 1 (04h)
240 #define JYSTK_EN 0x02
242 #define SPDO2DAC 0x40
243 #define SPDFLOOP 0x80
245 // Function Control Register 1+1 (05h)
250 #define SPDIF2DAC (SPDF_1 << 8 | SPDO2DAC)
252 // Channel Format Register (08h)
253 #define CM_CFMT_STEREO 0x01
254 #define CM_CFMT_16BIT 0x02
255 #define CM_CFMT_MASK 0x03
256 #define POLVALID 0x20
257 #define INVSPDIFI 0x80
259 // Channel Format Register+2 (0ah)
260 #define SPD24SEL 0x20
262 // Channel Format Register+3 (0bh)
266 // Interrupt Hold/Clear Register+2 (0eh)
267 #define CH0_INT_EN 0x01
268 #define CH1_INT_EN 0x02
270 // Interrupt Register (10h)
276 // Legacy Control/Status Register+1 (15h)
278 #define BASE2LIN 0x20
279 #define CENTR2LIN 0x40
280 #define CB2LIN (BASE2LIN | CENTR2LIN)
283 // Legacy Control/Status Register+2 (16h)
284 #define DAC2SPDO 0x20
285 #define SPDCOPYRHT 0x40
286 #define ENSPDOUT 0x80
288 // Legacy Control/Status Register+3 (17h)
294 // Miscellaneous Control Register (18h)
295 #define REAR2LIN 0x20
297 #define ENCENTER 0x80
299 // Miscellaneous Control Register+1 (19h)
300 #define SELSPDIFI2 0x01
301 #define SPDF_AC97 0x80
303 // Miscellaneous Control Register+2 (1ah)
306 #define SPD32SEL 0x20
310 // Miscellaneous Control Register+3 (1bh)
311 #define SPDIFI48K 0x01
316 #define SPDIF48K (SPDIFI48K << 24 | SPDF_AC97 << 8)
321 #define REAR2FRONT 0x10
326 // Miscellaneous Register (27h)
327 #define SPDVALID 0x02
328 #define CENTR2MIC 0x04
330 // Miscellaneous Register2 (92h)
331 #define SPD32KFMT 0x10
333 #define CM_CFMT_DACSHIFT 2
334 #define CM_CFMT_ADCSHIFT 0
335 #define CM_FREQ_DACSHIFT 5
336 #define CM_FREQ_ADCSHIFT 2
337 #define RSTDAC RST_CH1
338 #define RSTADC RST_CH0
341 #define PAUSEDAC PAUSE1
342 #define PAUSEADC PAUSE0
343 #define CODEC_CMI_ADC_FRAME1 CODEC_CMI_CH0_FRAME1
344 #define CODEC_CMI_ADC_FRAME2 CODEC_CMI_CH0_FRAME2
345 #define CODEC_CMI_DAC_FRAME1 CODEC_CMI_CH1_FRAME1
346 #define CODEC_CMI_DAC_FRAME2 CODEC_CMI_CH1_FRAME2
347 #define DACINT CHINT1
348 #define ADCINT CHINT0
349 #define DACBUSY CH1BUSY
350 #define ADCBUSY CH0BUSY
351 #define ENDACINT CH1_INT_EN
352 #define ENADCINT CH0_INT_EN
354 static const unsigned sample_size[] = { 1, 2, 2, 4 };
355 static const unsigned sample_shift[] = { 0, 1, 1, 2 };
357 #define SND_DEV_DSP16 5
359 #define NR_DEVICE 3 /* maximum number of devices */
361 #define set_dac1_rate set_adc_rate
362 #define set_dac1_rate_unlocked set_adc_rate_unlocked
363 #define stop_dac1 stop_adc
364 #define stop_dac1_unlocked stop_adc_unlocked
365 #define get_dmadac1 get_dmaadc
367 static unsigned int devindex = 0;
369 //*********************************************/
375 /* list of cmedia devices */
376 struct list_head devs;
378 /* the corresponding pci_dev structure */
381 int dev_audio; /* soundcore stuff */
384 unsigned int iosb, iobase, iosynth,
385 iomidi, iogame, irq; /* hardware resources */
386 unsigned short deviceid; /* pci_id */
388 struct { /* mixer stuff */
390 unsigned short vol[13];
393 unsigned int rateadc, ratedac; /* wave stuff */
394 unsigned char fmt, enable;
397 struct mutex open_mutex;
399 wait_queue_head_t open_wait;
407 unsigned hwptr, swptr;
408 unsigned total_bytes;
410 unsigned error; /* over/underrun */
411 wait_queue_head_t wait;
413 unsigned fragsize; /* redundant, but makes calculations easier */
415 unsigned fragsamples;
418 unsigned mapped:1; /* OSS stuff */
420 unsigned endcleared:1;
422 unsigned ossfragshift;
424 unsigned subdivision;
427 #ifdef CONFIG_SOUND_CMPCI_MIDI
429 struct address_info mpu_data;
431 #ifdef CONFIG_SOUND_CMPCI_JOYSTICK
432 struct gameport *gameport;
438 int capability; /* HW capability, various for chip versions */
440 int status; /* HW or SW state */
442 int spdif_counter; /* spdif frame counter */
445 /* flags used for capability */
446 #define CAN_AC3_HW 0x00000001 /* 037 or later */
447 #define CAN_AC3_SW 0x00000002 /* 033 or later */
448 #define CAN_AC3 (CAN_AC3_HW | CAN_AC3_SW)
449 #define CAN_DUAL_DAC 0x00000004 /* 033 or later */
450 #define CAN_MULTI_CH_HW 0x00000008 /* 039 or later */
451 #define CAN_MULTI_CH (CAN_MULTI_CH_HW | CAN_DUAL_DAC)
452 #define CAN_LINE_AS_REAR 0x00000010 /* 033 or later */
453 #define CAN_LINE_AS_BASS 0x00000020 /* 039 or later */
454 #define CAN_MIC_AS_BASS 0x00000040 /* 039 or later */
456 /* flags used for status */
457 #define DO_AC3_HW 0x00000001
458 #define DO_AC3_SW 0x00000002
459 #define DO_AC3 (DO_AC3_HW | DO_AC3_SW)
460 #define DO_DUAL_DAC 0x00000004
461 #define DO_MULTI_CH_HW 0x00000008
462 #define DO_MULTI_CH (DO_MULTI_CH_HW | DO_DUAL_DAC)
463 #define DO_LINE_AS_REAR 0x00000010 /* 033 or later */
464 #define DO_LINE_AS_BASS 0x00000020 /* 039 or later */
465 #define DO_MIC_AS_BASS 0x00000040 /* 039 or later */
466 #define DO_SPDIF_OUT 0x00000100
467 #define DO_SPDIF_IN 0x00000200
468 #define DO_SPDIF_LOOP 0x00000400
469 #define DO_BIGENDIAN_W 0x00001000 /* used in PowerPC */
470 #define DO_BIGENDIAN_R 0x00002000 /* used in PowerPC */
472 static LIST_HEAD(devs);
477 static int spdif_inverse;
478 static int spdif_loop;
479 static int spdif_out;
480 static int use_line_as_rear;
481 static int use_line_as_bass;
482 static int use_mic_as_bass;
483 static int mic_boost;
485 module_param(mpuio, int, 0);
486 module_param(fmio, int, 0);
487 module_param(joystick, bool, 0);
488 module_param(spdif_inverse, bool, 0);
489 module_param(spdif_loop, bool, 0);
490 module_param(spdif_out, bool, 0);
491 module_param(use_line_as_rear, bool, 0);
492 module_param(use_line_as_bass, bool, 0);
493 module_param(use_mic_as_bass, bool, 0);
494 module_param(mic_boost, bool, 0);
495 module_param(hw_copy, bool, 0);
496 MODULE_PARM_DESC(mpuio, "(0x330, 0x320, 0x310, 0x300) Base of MPU-401, 0 to disable");
497 MODULE_PARM_DESC(fmio, "(0x388, 0x3C8, 0x3E0) Base of OPL3, 0 to disable");
498 MODULE_PARM_DESC(joystick, "(1/0) Enable joystick interface, still need joystick driver");
499 MODULE_PARM_DESC(spdif_inverse, "(1/0) Invert S/PDIF-in signal");
500 MODULE_PARM_DESC(spdif_loop, "(1/0) Route S/PDIF-in to S/PDIF-out directly");
501 MODULE_PARM_DESC(spdif_out, "(1/0) Send PCM to S/PDIF-out (PCM volume will not function)");
502 MODULE_PARM_DESC(use_line_as_rear, "(1/0) Use line-in jack as rear-out");
503 MODULE_PARM_DESC(use_line_as_bass, "(1/0) Use line-in jack as bass/center");
504 MODULE_PARM_DESC(use_mic_as_bass, "(1/0) Use mic-in jack as bass/center");
505 MODULE_PARM_DESC(mic_boost, "(1/0) Enable microphone boost");
506 MODULE_PARM_DESC(hw_copy, "Copy front channel to surround channel");
508 /* --------------------------------------------------------------------- */
510 static inline unsigned ld2(unsigned int x)
512 unsigned exp=16,l=5,r=0;
513 static const unsigned num[]={0x2,0x4,0x10,0x100,0x10000};
515 /* num: 2, 4, 16, 256, 65536 */
516 /* exp: 1, 2, 4, 8, 16 */
520 if(num[l]>2) x >>= exp;
529 /* --------------------------------------------------------------------- */
531 static void maskb(unsigned int addr, unsigned int mask, unsigned int value)
533 outb((inb(addr) & mask) | value, addr);
536 static void maskw(unsigned int addr, unsigned int mask, unsigned int value)
538 outw((inw(addr) & mask) | value, addr);
541 static void maskl(unsigned int addr, unsigned int mask, unsigned int value)
543 outl((inl(addr) & mask) | value, addr);
546 static void set_dmadac1(struct cm_state *s, unsigned int addr, unsigned int count)
549 outl(addr, s->iobase + CODEC_CMI_ADC_FRAME1);
550 outw(count - 1, s->iobase + CODEC_CMI_ADC_FRAME2);
551 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~CHADC0, 0);
554 static void set_dmaadc(struct cm_state *s, unsigned int addr, unsigned int count)
556 outl(addr, s->iobase + CODEC_CMI_ADC_FRAME1);
557 outw(count - 1, s->iobase + CODEC_CMI_ADC_FRAME2);
558 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~0, CHADC0);
561 static void set_dmadac(struct cm_state *s, unsigned int addr, unsigned int count)
563 outl(addr, s->iobase + CODEC_CMI_DAC_FRAME1);
564 outw(count - 1, s->iobase + CODEC_CMI_DAC_FRAME2);
565 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~CHADC1, 0);
566 if (s->status & DO_DUAL_DAC)
567 set_dmadac1(s, 0, count);
570 static void set_countadc(struct cm_state *s, unsigned count)
572 outw(count - 1, s->iobase + CODEC_CMI_ADC_FRAME2 + 2);
575 static void set_countdac(struct cm_state *s, unsigned count)
577 outw(count - 1, s->iobase + CODEC_CMI_DAC_FRAME2 + 2);
578 if (s->status & DO_DUAL_DAC)
579 set_countadc(s, count);
582 static unsigned get_dmadac(struct cm_state *s)
584 unsigned int curr_addr;
586 curr_addr = inw(s->iobase + CODEC_CMI_DAC_FRAME2) + 1;
587 curr_addr <<= sample_shift[(s->fmt >> CM_CFMT_DACSHIFT) & CM_CFMT_MASK];
588 curr_addr = s->dma_dac.dmasize - curr_addr;
593 static unsigned get_dmaadc(struct cm_state *s)
595 unsigned int curr_addr;
597 curr_addr = inw(s->iobase + CODEC_CMI_ADC_FRAME2) + 1;
598 curr_addr <<= sample_shift[(s->fmt >> CM_CFMT_ADCSHIFT) & CM_CFMT_MASK];
599 curr_addr = s->dma_adc.dmasize - curr_addr;
604 static void wrmixer(struct cm_state *s, unsigned char idx, unsigned char data)
606 unsigned char regval, pseudo;
609 if (idx == DSP_MIX_AUXVOL_L) {
612 regval = inb(s->iobase + CODEC_CMI_AUX_VOL) & ~0x0f;
613 outb(regval | data, s->iobase + CODEC_CMI_AUX_VOL);
616 if (idx == DSP_MIX_AUXVOL_R) {
618 regval = inb(s->iobase + CODEC_CMI_AUX_VOL) & ~0xf0;
619 outb(regval | data, s->iobase + CODEC_CMI_AUX_VOL);
622 outb(idx, s->iobase + CODEC_SB16_ADDR);
625 if (idx == DSP_MIX_OUTMIXIDX) {
626 pseudo = data & ~0x1f;
628 regval = inb(s->iobase + CODEC_CMI_MIXER2) & ~0x30;
629 outb(regval | pseudo, s->iobase + CODEC_CMI_MIXER2);
631 if (idx == DSP_MIX_ADCMIXIDX_L) {
632 pseudo = data & 0x80;
634 regval = inb(s->iobase + CODEC_CMI_MIXER2) & ~0x40;
635 outb(regval | pseudo, s->iobase + CODEC_CMI_MIXER2);
637 if (idx == DSP_MIX_ADCMIXIDX_R) {
638 pseudo = data & 0x80;
639 regval = inb(s->iobase + CODEC_CMI_MIXER2) & ~0x80;
640 outb(regval | pseudo, s->iobase + CODEC_CMI_MIXER2);
642 outb(data, s->iobase + CODEC_SB16_DATA);
646 static unsigned char rdmixer(struct cm_state *s, unsigned char idx)
648 unsigned char v, pseudo;
651 if (idx == DSP_MIX_AUXVOL_L) {
652 v = inb(s->iobase + CODEC_CMI_AUX_VOL) & 0x0f;
656 if (idx == DSP_MIX_AUXVOL_L) {
657 v = inb(s->iobase + CODEC_CMI_AUX_VOL) & 0xf0;
660 outb(idx, s->iobase + CODEC_SB16_ADDR);
662 v = inb(s->iobase + CODEC_SB16_DATA);
665 if (idx == DSP_MIX_OUTMIXIDX) {
666 pseudo = inb(s->iobase + CODEC_CMI_MIXER2) & 0x30;
670 if (idx == DSP_MIX_ADCMIXIDX_L) {
671 pseudo = inb(s->iobase + CODEC_CMI_MIXER2) & 0x40;
675 if (idx == DSP_MIX_ADCMIXIDX_R) {
676 pseudo = inb(s->iobase + CODEC_CMI_MIXER2) & 0x80;
682 static void set_fmt_unlocked(struct cm_state *s, unsigned char mask, unsigned char data)
684 if (mask && s->chip_version > 0) { /* 8338 cannot keep this */
685 s->fmt = inb(s->iobase + CODEC_CMI_CHFORMAT);
688 s->fmt = (s->fmt & mask) | data;
689 outb(s->fmt, s->iobase + CODEC_CMI_CHFORMAT);
693 static void set_fmt(struct cm_state *s, unsigned char mask, unsigned char data)
697 spin_lock_irqsave(&s->lock, flags);
698 set_fmt_unlocked(s,mask,data);
699 spin_unlock_irqrestore(&s->lock, flags);
702 static void frobindir(struct cm_state *s, unsigned char idx, unsigned char mask, unsigned char data)
704 outb(idx, s->iobase + CODEC_SB16_ADDR);
706 outb((inb(s->iobase + CODEC_SB16_DATA) & mask) | data, s->iobase + CODEC_SB16_DATA);
717 { 5512, (0 + 5512) / 2, (5512 + 8000) / 2, 0 },
718 { 8000, (5512 + 8000) / 2, (8000 + 11025) / 2, 4 },
719 { 11025, (8000 + 11025) / 2, (11025 + 16000) / 2, 1 },
720 { 16000, (11025 + 16000) / 2, (16000 + 22050) / 2, 5 },
721 { 22050, (16000 + 22050) / 2, (22050 + 32000) / 2, 2 },
722 { 32000, (22050 + 32000) / 2, (32000 + 44100) / 2, 6 },
723 { 44100, (32000 + 44100) / 2, (44100 + 48000) / 2, 3 },
724 { 48000, (44100 + 48000) / 2, 48000, 7 }
727 static void set_spdif_copyright(struct cm_state *s, int spdif_copyright)
729 /* enable SPDIF-in Copyright */
730 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 2, ~SPDCOPYRHT, spdif_copyright ? SPDCOPYRHT : 0);
733 static void set_spdif_loop(struct cm_state *s, int spdif_loop)
735 /* enable SPDIF loop */
737 s->status |= DO_SPDIF_LOOP;
738 /* turn on spdif-in to spdif-out */
739 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, SPDFLOOP);
741 s->status &= ~DO_SPDIF_LOOP;
742 /* turn off spdif-in to spdif-out */
743 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~SPDFLOOP, 0);
747 static void set_spdif_monitor(struct cm_state *s, int channel)
750 maskw(s->iobase + CODEC_CMI_FUNCTRL1, ~SPDO2DAC, channel == 2 ? SPDO2DAC : 0);
752 if (s->chip_version >= 39)
753 maskb(s->iobase + CODEC_CMI_MIXER1, ~CDPLAY, channel ? CDPLAY : 0);
756 static void set_spdifout_level(struct cm_state *s, int level5v)
759 if (s->chip_version > 0)
760 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 3, ~SPDO5V, level5v ? SPDO5V : 0);
763 static void set_spdifin_inverse(struct cm_state *s, int spdif_inverse)
765 if (s->chip_version == 0) /* 8338 has not this feature */
768 /* turn on spdif-in inverse */
769 if (s->chip_version >= 39)
770 maskb(s->iobase + CODEC_CMI_CHFORMAT, ~0, INVSPDIFI);
772 maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~0, 1);
774 /* turn off spdif-ininverse */
775 if (s->chip_version >= 39)
776 maskb(s->iobase + CODEC_CMI_CHFORMAT, ~INVSPDIFI, 0);
778 maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~1, 0);
782 static void set_spdifin_channel2(struct cm_state *s, int channel2)
785 if (s->chip_version >= 39)
786 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 1, ~SELSPDIFI2, channel2 ? SELSPDIFI2 : 0);
789 static void set_spdifin_valid(struct cm_state *s, int valid)
792 maskb(s->iobase + CODEC_CMI_MISC, ~SPDVALID, valid ? SPDVALID : 0);
795 static void set_spdifout_unlocked(struct cm_state *s, unsigned rate)
797 if (rate != 48000 && rate != 44100)
799 if (rate == 48000 || rate == 44100) {
800 set_spdif_loop(s, 0);
802 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~0, SPDF_1);
803 // SPDIFI48K SPDF_AC97
804 maskl(s->iobase + CODEC_CMI_MISC_CTRL, ~SPDIF48K, rate == 48000 ? SPDIF48K : 0);
805 if (s->chip_version >= 55)
807 maskb(s->iobase + CODEC_CMI_MISC_CTRL2, ~SPD32KFMT, rate == 48000 ? SPD32KFMT : 0);
808 if (s->chip_version > 0)
810 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 2, ~0, ENSPDOUT);
812 set_spdif_monitor(s, 2);
813 s->status |= DO_SPDIF_OUT;
815 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~SPDF_1, 0);
816 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 2, ~ENSPDOUT, 0);
818 set_spdif_monitor(s, 0);
819 s->status &= ~DO_SPDIF_OUT;
823 static void set_spdifout(struct cm_state *s, unsigned rate)
827 spin_lock_irqsave(&s->lock, flags);
828 set_spdifout_unlocked(s,rate);
829 spin_unlock_irqrestore(&s->lock, flags);
832 static void set_spdifin_unlocked(struct cm_state *s, unsigned rate)
834 if (rate == 48000 || rate == 44100) {
836 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~0, SPDF_1);
837 // SPDIFI48K SPDF_AC97
838 maskl(s->iobase + CODEC_CMI_MISC_CTRL, ~SPDIF48K, rate == 48000 ? SPDIF48K : 0);
839 s->status |= DO_SPDIF_IN;
841 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~SPDF_1, 0);
842 s->status &= ~DO_SPDIF_IN;
846 static void set_spdifin(struct cm_state *s, unsigned rate)
850 spin_lock_irqsave(&s->lock, flags);
851 set_spdifin_unlocked(s,rate);
852 spin_unlock_irqrestore(&s->lock, flags);
855 /* find parity for bit 4~30 */
856 static unsigned parity(unsigned data)
861 data >>= 4; // start from bit 4
862 while (counter <= 30) {
871 static void set_ac3_unlocked(struct cm_state *s, unsigned rate)
873 if (!(s->capability & CAN_AC3))
876 if (rate && rate != 44100)
878 if (rate == 48000 || rate == 44100) {
880 maskb(s->iobase + CODEC_CMI_MIXER1, ~0, WSMUTE);
881 if (s->chip_version >= 39)
882 maskb(s->iobase + CODEC_CMI_MISC_CTRL, ~0, MUTECH1);
883 // AC3EN for 039, 0x04
884 if (s->chip_version >= 39) {
885 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0, AC3_EN);
886 if (s->chip_version == 55)
887 maskb(s->iobase + CODEC_CMI_SPDIF_CTRL, ~2, 0);
888 // AC3EN for 037, 0x10
889 } else if (s->chip_version == 37)
890 maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~0, 0x10);
891 if (s->capability & CAN_AC3_HW) {
892 // SPD24SEL for 039, 0x20, but cannot be set
893 if (s->chip_version == 39)
894 maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~0, SPD24SEL);
895 // SPD24SEL for 037, 0x02
896 else if (s->chip_version == 37)
897 maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~0, 0x02);
898 if (s->chip_version >= 39)
899 maskb(s->iobase + CODEC_CMI_MIXER1, ~CDPLAY, 0);
901 s->status |= DO_AC3_HW;
903 // SPD32SEL for 037 & 039
904 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0, SPD32SEL);
905 // set 176K sample rate to fix 033 HW bug
906 if (s->chip_version == 33) {
908 maskb(s->iobase + CODEC_CMI_CHFORMAT + 1, ~0, 0x08);
910 maskb(s->iobase + CODEC_CMI_CHFORMAT + 1, ~0x08, 0);
912 s->status |= DO_AC3_SW;
915 maskb(s->iobase + CODEC_CMI_MIXER1, ~WSMUTE, 0);
916 if (s->chip_version >= 39)
917 maskb(s->iobase + CODEC_CMI_MISC_CTRL, ~MUTECH1, 0);
918 maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~(SPD24SEL|0x12), 0);
919 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~(SPD32SEL|AC3_EN), 0);
920 if (s->chip_version == 33)
921 maskb(s->iobase + CODEC_CMI_CHFORMAT + 1, ~0x08, 0);
922 if (s->chip_version >= 39)
923 maskb(s->iobase + CODEC_CMI_MIXER1, ~0, CDPLAY);
924 s->status &= ~DO_AC3;
926 s->spdif_counter = 0;
929 static void set_line_as_rear(struct cm_state *s, int use_line_as_rear)
931 if (!(s->capability & CAN_LINE_AS_REAR))
933 if (use_line_as_rear) {
934 maskb(s->iobase + CODEC_CMI_MIXER1, ~0, SPK4);
935 s->status |= DO_LINE_AS_REAR;
937 maskb(s->iobase + CODEC_CMI_MIXER1, ~SPK4, 0);
938 s->status &= ~DO_LINE_AS_REAR;
942 static void set_line_as_bass(struct cm_state *s, int use_line_as_bass)
944 if (!(s->capability & CAN_LINE_AS_BASS))
946 if (use_line_as_bass) {
947 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 1, ~0, CB2LIN);
948 s->status |= DO_LINE_AS_BASS;
950 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 1, ~CB2LIN, 0);
951 s->status &= ~DO_LINE_AS_BASS;
955 static void set_mic_as_bass(struct cm_state *s, int use_mic_as_bass)
957 if (!(s->capability & CAN_MIC_AS_BASS))
959 if (use_mic_as_bass) {
960 maskb(s->iobase + CODEC_CMI_MISC, ~0, 0x04);
961 s->status |= DO_MIC_AS_BASS;
963 maskb(s->iobase + CODEC_CMI_MISC, ~0x04, 0);
964 s->status &= ~DO_MIC_AS_BASS;
968 static void set_hw_copy(struct cm_state *s, int hw_copy)
970 if (s->max_channels > 2 && hw_copy)
971 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 3, ~0, N4SPK3D);
973 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 3, ~N4SPK3D, 0);
976 static void set_ac3(struct cm_state *s, unsigned rate)
980 spin_lock_irqsave(&s->lock, flags);
981 set_spdifout_unlocked(s, rate);
982 set_ac3_unlocked(s, rate);
983 spin_unlock_irqrestore(&s->lock, flags);
986 static int trans_ac3(struct cm_state *s, void *dest, const char __user *source, int size)
990 unsigned short data16;
991 unsigned long *dst = (unsigned long *) dest;
992 unsigned short __user *src = (unsigned short __user *)source;
996 if ((err = __get_user(data16, src++)))
998 data = (unsigned long)le16_to_cpu(data16);
999 data <<= 12; // ok for 16-bit data
1000 if (s->spdif_counter == 2 || s->spdif_counter == 3)
1001 data |= 0x40000000; // indicate AC-3 raw data
1003 data |= 0x80000000; // parity
1004 if (s->spdif_counter == 0)
1005 data |= 3; // preamble 'M'
1006 else if (s->spdif_counter & 1)
1007 data |= 5; // odd, 'W'
1009 data |= 9; // even, 'M'
1010 *dst++ = cpu_to_le32(data);
1012 if (s->spdif_counter == 384)
1013 s->spdif_counter = 0;
1019 static void set_adc_rate_unlocked(struct cm_state *s, unsigned rate)
1021 unsigned char freq = 4;
1028 for (i = 0; i < sizeof(rate_lookup) / sizeof(rate_lookup[0]); i++) {
1029 if (rate > rate_lookup[i].lower && rate <= rate_lookup[i].upper) {
1030 rate = rate_lookup[i].rate;
1031 freq = rate_lookup[i].freq;
1036 freq <<= CM_FREQ_ADCSHIFT;
1038 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~ASFC, freq);
1041 static void set_adc_rate(struct cm_state *s, unsigned rate)
1043 unsigned long flags;
1044 unsigned char freq = 4;
1051 for (i = 0; i < sizeof(rate_lookup) / sizeof(rate_lookup[0]); i++) {
1052 if (rate > rate_lookup[i].lower && rate <= rate_lookup[i].upper) {
1053 rate = rate_lookup[i].rate;
1054 freq = rate_lookup[i].freq;
1059 freq <<= CM_FREQ_ADCSHIFT;
1061 spin_lock_irqsave(&s->lock, flags);
1062 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~ASFC, freq);
1063 spin_unlock_irqrestore(&s->lock, flags);
1066 static void set_dac_rate(struct cm_state *s, unsigned rate)
1068 unsigned long flags;
1069 unsigned char freq = 4;
1076 for (i = 0; i < sizeof(rate_lookup) / sizeof(rate_lookup[0]); i++) {
1077 if (rate > rate_lookup[i].lower && rate <= rate_lookup[i].upper) {
1078 rate = rate_lookup[i].rate;
1079 freq = rate_lookup[i].freq;
1084 freq <<= CM_FREQ_DACSHIFT;
1086 spin_lock_irqsave(&s->lock, flags);
1087 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~DSFC, freq);
1088 spin_unlock_irqrestore(&s->lock, flags);
1090 if (s->curr_channels <= 2 && spdif_out)
1091 set_spdifout(s, rate);
1092 if (s->status & DO_DUAL_DAC)
1093 set_dac1_rate(s, rate);
1096 /* --------------------------------------------------------------------- */
1097 static inline void reset_adc(struct cm_state *s)
1099 /* reset bus master */
1100 outb(s->enable | RSTADC, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1102 outb(s->enable & ~RSTADC, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1105 static inline void reset_dac(struct cm_state *s)
1107 /* reset bus master */
1108 outb(s->enable | RSTDAC, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1110 outb(s->enable & ~RSTDAC, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1111 if (s->status & DO_DUAL_DAC)
1115 static inline void pause_adc(struct cm_state *s)
1117 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~0, PAUSEADC);
1120 static inline void pause_dac(struct cm_state *s)
1122 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~0, PAUSEDAC);
1123 if (s->status & DO_DUAL_DAC)
1127 static inline void disable_adc(struct cm_state *s)
1129 /* disable channel */
1130 s->enable &= ~ENADC;
1131 outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1135 static inline void disable_dac(struct cm_state *s)
1137 /* disable channel */
1138 s->enable &= ~ENDAC;
1139 outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1141 if (s->status & DO_DUAL_DAC)
1145 static inline void enable_adc(struct cm_state *s)
1147 if (!(s->enable & ENADC)) {
1148 /* enable channel */
1150 outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1152 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~PAUSEADC, 0);
1155 static inline void enable_dac_unlocked(struct cm_state *s)
1157 if (!(s->enable & ENDAC)) {
1158 /* enable channel */
1160 outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1162 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~PAUSEDAC, 0);
1164 if (s->status & DO_DUAL_DAC)
1168 static inline void stop_adc_unlocked(struct cm_state *s)
1170 if (s->enable & ENADC) {
1171 /* disable interrupt */
1172 maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~ENADCINT, 0);
1177 static inline void stop_adc(struct cm_state *s)
1179 unsigned long flags;
1181 spin_lock_irqsave(&s->lock, flags);
1182 stop_adc_unlocked(s);
1183 spin_unlock_irqrestore(&s->lock, flags);
1187 static inline void stop_dac_unlocked(struct cm_state *s)
1189 if (s->enable & ENDAC) {
1190 /* disable interrupt */
1191 maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~ENDACINT, 0);
1194 if (s->status & DO_DUAL_DAC)
1195 stop_dac1_unlocked(s);
1198 static inline void stop_dac(struct cm_state *s)
1200 unsigned long flags;
1202 spin_lock_irqsave(&s->lock, flags);
1203 stop_dac_unlocked(s);
1204 spin_unlock_irqrestore(&s->lock, flags);
1207 static inline void start_adc_unlocked(struct cm_state *s)
1209 if ((s->dma_adc.mapped || s->dma_adc.count < (signed)(s->dma_adc.dmasize - 2*s->dma_adc.fragsize))
1210 && s->dma_adc.ready) {
1211 /* enable interrupt */
1212 maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~0, ENADCINT);
1217 static void start_adc(struct cm_state *s)
1219 unsigned long flags;
1221 spin_lock_irqsave(&s->lock, flags);
1222 start_adc_unlocked(s);
1223 spin_unlock_irqrestore(&s->lock, flags);
1226 static void start_dac1_unlocked(struct cm_state *s)
1228 if ((s->dma_adc.mapped || s->dma_adc.count > 0) && s->dma_adc.ready) {
1229 /* enable interrupt */
1230 maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~0, ENADCINT);
1231 enable_dac_unlocked(s);
1235 static void start_dac_unlocked(struct cm_state *s)
1237 if ((s->dma_dac.mapped || s->dma_dac.count > 0) && s->dma_dac.ready) {
1238 /* enable interrupt */
1239 maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~0, ENDACINT);
1240 enable_dac_unlocked(s);
1242 if (s->status & DO_DUAL_DAC)
1243 start_dac1_unlocked(s);
1246 static void start_dac(struct cm_state *s)
1248 unsigned long flags;
1250 spin_lock_irqsave(&s->lock, flags);
1251 start_dac_unlocked(s);
1252 spin_unlock_irqrestore(&s->lock, flags);
1255 static int prog_dmabuf(struct cm_state *s, unsigned rec);
1257 static int set_dac_channels(struct cm_state *s, int channels)
1259 unsigned long flags;
1260 static unsigned int fmmute = 0;
1262 spin_lock_irqsave(&s->lock, flags);
1264 if ((channels > 2) && (channels <= s->max_channels)
1265 && (((s->fmt >> CM_CFMT_DACSHIFT) & CM_CFMT_MASK) == (CM_CFMT_STEREO | CM_CFMT_16BIT))) {
1266 set_spdifout_unlocked(s, 0);
1267 if (s->capability & CAN_MULTI_CH_HW) {
1269 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3, ~0, NXCHG);
1271 maskb(s->iobase + CODEC_CMI_CHFORMAT + 3, ~(CHB3D5C|CHB3D), channels > 4 ? CHB3D5C : CHB3D);
1273 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 1, ~CHB3D6C, channels == 6 ? CHB3D6C : 0);
1275 maskb(s->iobase + CODEC_CMI_MISC_CTRL, ~ENCENTER, channels == 6 ? ENCENTER : 0);
1276 s->status |= DO_MULTI_CH_HW;
1277 } else if (s->capability & CAN_DUAL_DAC) {
1278 unsigned char fmtm = ~0, fmts = 0;
1281 // ENDBDAC, turn on double DAC mode
1282 // XCHGDAC, CH0 -> back, CH1->front
1283 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0, ENDBDAC|XCHGDAC);
1285 fmmute = inb(s->iobase + CODEC_CMI_MIXER1) & FMMUTE;
1286 maskb(s->iobase + CODEC_CMI_MIXER1, ~0, FMMUTE);
1287 s->status |= DO_DUAL_DAC;
1288 // prepare secondary buffer
1289 spin_unlock_irqrestore(&s->lock, flags);
1290 ret = prog_dmabuf(s, 1);
1291 if (ret) return ret;
1292 spin_lock_irqsave(&s->lock, flags);
1294 // copy the hw state
1295 fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_DACSHIFT);
1296 fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_ADCSHIFT);
1297 // the HW only support 16-bit stereo
1298 fmts |= CM_CFMT_16BIT << CM_CFMT_DACSHIFT;
1299 fmts |= CM_CFMT_16BIT << CM_CFMT_ADCSHIFT;
1300 fmts |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
1301 fmts |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
1303 set_fmt_unlocked(s, fmtm, fmts);
1304 set_adc_rate_unlocked(s, s->ratedac);
1306 // disable 4 speaker mode (analog duplicate)
1308 s->curr_channels = channels;
1310 // enable jack redirect
1311 set_line_as_rear(s, use_line_as_rear);
1313 set_line_as_bass(s, use_line_as_bass);
1314 set_mic_as_bass(s, use_mic_as_bass);
1317 if (s->status & DO_MULTI_CH_HW) {
1318 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3, ~NXCHG, 0);
1319 maskb(s->iobase + CODEC_CMI_CHFORMAT + 3, ~(CHB3D5C|CHB3D), 0);
1320 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 1, ~CHB3D6C, 0);
1321 } else if (s->status & DO_DUAL_DAC) {
1322 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~ENDBDAC, 0);
1323 maskb(s->iobase + CODEC_CMI_MIXER1, ~FMMUTE, fmmute);
1325 // enable 4 speaker mode (analog duplicate)
1326 set_hw_copy(s, hw_copy);
1327 s->status &= ~DO_MULTI_CH;
1328 s->curr_channels = s->fmt & (CM_CFMT_STEREO << CM_CFMT_DACSHIFT) ? 2 : 1;
1329 // disable jack redirect
1330 set_line_as_rear(s, hw_copy ? use_line_as_rear : 0);
1331 set_line_as_bass(s, 0);
1332 set_mic_as_bass(s, 0);
1334 spin_unlock_irqrestore(&s->lock, flags);
1335 return s->curr_channels;
1338 /* --------------------------------------------------------------------- */
1340 #define DMABUF_DEFAULTORDER (16-PAGE_SHIFT)
1341 #define DMABUF_MINORDER 1
1343 static void dealloc_dmabuf(struct cm_state *s, struct dmabuf *db)
1345 struct page *pstart, *pend;
1348 /* undo marking the pages as reserved */
1349 pend = virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
1350 for (pstart = virt_to_page(db->rawbuf); pstart <= pend; pstart++)
1351 ClearPageReserved(pstart);
1352 pci_free_consistent(s->dev, PAGE_SIZE << db->buforder, db->rawbuf, db->dmaaddr);
1355 db->mapped = db->ready = 0;
1358 /* Ch1 is used for playback, Ch0 is used for recording */
1360 static int prog_dmabuf(struct cm_state *s, unsigned rec)
1362 struct dmabuf *db = rec ? &s->dma_adc : &s->dma_dac;
1363 unsigned rate = rec ? s->rateadc : s->ratedac;
1365 unsigned bytepersec;
1367 struct page *pstart, *pend;
1369 unsigned long flags;
1374 fmt >>= CM_CFMT_ADCSHIFT;
1377 fmt >>= CM_CFMT_DACSHIFT;
1380 fmt &= CM_CFMT_MASK;
1381 db->hwptr = db->swptr = db->total_bytes = db->count = db->error = db->endcleared = 0;
1383 db->ready = db->mapped = 0;
1384 for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--)
1385 if ((db->rawbuf = pci_alloc_consistent(s->dev, PAGE_SIZE << order, &db->dmaaddr)))
1387 if (!db->rawbuf || !db->dmaaddr)
1389 db->buforder = order;
1390 /* now mark the pages as reserved; otherwise remap_pfn_range doesn't do what we want */
1391 pend = virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
1392 for (pstart = virt_to_page(db->rawbuf); pstart <= pend; pstart++)
1393 SetPageReserved(pstart);
1395 bytepersec = rate << sample_shift[fmt];
1396 bufs = PAGE_SIZE << db->buforder;
1397 if (db->ossfragshift) {
1398 if ((1000 << db->ossfragshift) < bytepersec)
1399 db->fragshift = ld2(bytepersec/1000);
1401 db->fragshift = db->ossfragshift;
1403 db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1));
1404 if (db->fragshift < 3)
1407 db->numfrag = bufs >> db->fragshift;
1408 while (db->numfrag < 4 && db->fragshift > 3) {
1410 db->numfrag = bufs >> db->fragshift;
1412 db->fragsize = 1 << db->fragshift;
1413 if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
1414 db->numfrag = db->ossmaxfrags;
1415 /* to make fragsize >= 4096 */
1416 db->fragsamples = db->fragsize >> sample_shift[fmt];
1417 db->dmasize = db->numfrag << db->fragshift;
1418 db->dmasamples = db->dmasize >> sample_shift[fmt];
1419 memset(db->rawbuf, (fmt & CM_CFMT_16BIT) ? 0 : 0x80, db->dmasize);
1420 spin_lock_irqsave(&s->lock, flags);
1422 if (s->status & DO_DUAL_DAC)
1423 set_dmadac1(s, db->dmaaddr, db->dmasize >> sample_shift[fmt]);
1425 set_dmaadc(s, db->dmaaddr, db->dmasize >> sample_shift[fmt]);
1426 /* program sample counts */
1427 set_countdac(s, db->fragsamples);
1429 set_dmadac(s, db->dmaaddr, db->dmasize >> sample_shift[fmt]);
1430 /* program sample counts */
1431 set_countdac(s, db->fragsamples);
1433 spin_unlock_irqrestore(&s->lock, flags);
1439 static inline void clear_advance(struct cm_state *s)
1441 unsigned char c = (s->fmt & (CM_CFMT_16BIT << CM_CFMT_DACSHIFT)) ? 0 : 0x80;
1442 unsigned char *buf = s->dma_dac.rawbuf;
1443 unsigned char *buf1 = s->dma_adc.rawbuf;
1444 unsigned bsize = s->dma_dac.dmasize;
1445 unsigned bptr = s->dma_dac.swptr;
1446 unsigned len = s->dma_dac.fragsize;
1448 if (bptr + len > bsize) {
1449 unsigned x = bsize - bptr;
1450 memset(buf + bptr, c, x);
1451 if (s->status & DO_DUAL_DAC)
1452 memset(buf1 + bptr, c, x);
1456 memset(buf + bptr, c, len);
1457 if (s->status & DO_DUAL_DAC)
1458 memset(buf1 + bptr, c, len);
1461 /* call with spinlock held! */
1462 static void cm_update_ptr(struct cm_state *s)
1467 /* update ADC pointer */
1468 if (s->dma_adc.ready) {
1469 if (s->status & DO_DUAL_DAC) {
1470 /* the dac part will finish for this */
1472 hwptr = get_dmaadc(s) % s->dma_adc.dmasize;
1473 diff = (s->dma_adc.dmasize + hwptr - s->dma_adc.hwptr) % s->dma_adc.dmasize;
1474 s->dma_adc.hwptr = hwptr;
1475 s->dma_adc.total_bytes += diff;
1476 s->dma_adc.count += diff;
1477 if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
1478 wake_up(&s->dma_adc.wait);
1479 if (!s->dma_adc.mapped) {
1480 if (s->dma_adc.count > (signed)(s->dma_adc.dmasize - ((3 * s->dma_adc.fragsize) >> 1))) {
1487 /* update DAC pointer */
1488 if (s->dma_dac.ready) {
1489 hwptr = get_dmadac(s) % s->dma_dac.dmasize;
1490 diff = (s->dma_dac.dmasize + hwptr - s->dma_dac.hwptr) % s->dma_dac.dmasize;
1491 s->dma_dac.hwptr = hwptr;
1492 s->dma_dac.total_bytes += diff;
1493 if (s->status & DO_DUAL_DAC) {
1494 s->dma_adc.hwptr = hwptr;
1495 s->dma_adc.total_bytes += diff;
1497 if (s->dma_dac.mapped) {
1498 s->dma_dac.count += diff;
1499 if (s->status & DO_DUAL_DAC)
1500 s->dma_adc.count += diff;
1501 if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
1502 wake_up(&s->dma_dac.wait);
1504 s->dma_dac.count -= diff;
1505 if (s->status & DO_DUAL_DAC)
1506 s->dma_adc.count -= diff;
1507 if (s->dma_dac.count <= 0) {
1510 } else if (s->dma_dac.count <= (signed)s->dma_dac.fragsize && !s->dma_dac.endcleared) {
1512 s->dma_dac.endcleared = 1;
1513 if (s->status & DO_DUAL_DAC)
1514 s->dma_adc.endcleared = 1;
1516 if (s->dma_dac.count + (signed)s->dma_dac.fragsize <= (signed)s->dma_dac.dmasize)
1517 wake_up(&s->dma_dac.wait);
1522 static irqreturn_t cm_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1524 struct cm_state *s = (struct cm_state *)dev_id;
1525 unsigned int intsrc, intstat;
1526 unsigned char mask = 0;
1528 /* fastpath out, to ease interrupt sharing */
1529 intsrc = inl(s->iobase + CODEC_CMI_INT_STATUS);
1530 if (!(intsrc & 0x80000000))
1532 spin_lock(&s->lock);
1533 intstat = inb(s->iobase + CODEC_CMI_INT_HLDCLR + 2);
1534 /* acknowledge interrupt */
1535 if (intsrc & ADCINT)
1537 if (intsrc & DACINT)
1539 outb(intstat & ~mask, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
1540 outb(intstat | mask, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
1542 spin_unlock(&s->lock);
1543 #ifdef CONFIG_SOUND_CMPCI_MIDI
1544 if (intsrc & 0x00010000) { // UART interrupt
1545 if (s->midi_devc && intchk_mpu401((void *)s->midi_devc))
1546 mpuintr(irq, (void *)s->midi_devc, regs);
1548 inb(s->iomidi);// dummy read
1554 /* --------------------------------------------------------------------- */
1556 static const char invalid_magic[] = KERN_CRIT "cmpci: invalid magic value\n";
1558 #define VALIDATE_STATE(s) \
1560 if (!(s) || (s)->magic != CM_MAGIC) { \
1561 printk(invalid_magic); \
1566 /* --------------------------------------------------------------------- */
1570 #define MT_4MUTEMONO 3
1572 #define MT_5MUTEMONO 5
1574 static const struct {
1580 } mixtable[SOUND_MIXER_NRDEVICES] = {
1581 [SOUND_MIXER_CD] = { DSP_MIX_CDVOLIDX_L, DSP_MIX_CDVOLIDX_R, MT_5MUTE, 0x04, 0x06 },
1582 [SOUND_MIXER_LINE] = { DSP_MIX_LINEVOLIDX_L, DSP_MIX_LINEVOLIDX_R, MT_5MUTE, 0x10, 0x18 },
1583 [SOUND_MIXER_MIC] = { DSP_MIX_MICVOLIDX, DSP_MIX_MICVOLIDX, MT_5MUTEMONO, 0x01, 0x01 },
1584 [SOUND_MIXER_SYNTH] = { DSP_MIX_FMVOLIDX_L, DSP_MIX_FMVOLIDX_R, MT_5MUTE, 0x40, 0x00 },
1585 [SOUND_MIXER_VOLUME] = { DSP_MIX_MASTERVOLIDX_L, DSP_MIX_MASTERVOLIDX_R, MT_5MUTE, 0x00, 0x00 },
1586 [SOUND_MIXER_PCM] = { DSP_MIX_VOICEVOLIDX_L, DSP_MIX_VOICEVOLIDX_R, MT_5MUTE, 0x00, 0x00 },
1587 [SOUND_MIXER_LINE1] = { DSP_MIX_AUXVOL_L, DSP_MIX_AUXVOL_R, MT_5MUTE, 0x80, 0x60 },
1588 [SOUND_MIXER_SPEAKER]= { DSP_MIX_SPKRVOLIDX, DSP_MIX_SPKRVOLIDX, MT_5MUTEMONO, 0x00, 0x01 }
1591 static const unsigned char volidx[SOUND_MIXER_NRDEVICES] =
1593 [SOUND_MIXER_CD] = 1,
1594 [SOUND_MIXER_LINE] = 2,
1595 [SOUND_MIXER_MIC] = 3,
1596 [SOUND_MIXER_SYNTH] = 4,
1597 [SOUND_MIXER_VOLUME] = 5,
1598 [SOUND_MIXER_PCM] = 6,
1599 [SOUND_MIXER_LINE1] = 7,
1600 [SOUND_MIXER_SPEAKER]= 8
1603 static unsigned mixer_outmask(struct cm_state *s)
1605 unsigned long flags;
1608 spin_lock_irqsave(&s->lock, flags);
1609 j = rdmixer(s, DSP_MIX_OUTMIXIDX);
1610 spin_unlock_irqrestore(&s->lock, flags);
1611 for (k = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1612 if (j & mixtable[i].play)
1617 static unsigned mixer_recmask(struct cm_state *s)
1619 unsigned long flags;
1622 spin_lock_irqsave(&s->lock, flags);
1623 j = rdmixer(s, DSP_MIX_ADCMIXIDX_L);
1624 spin_unlock_irqrestore(&s->lock, flags);
1625 for (k = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1626 if (j & mixtable[i].rec)
1631 static int mixer_ioctl(struct cm_state *s, unsigned int cmd, unsigned long arg)
1633 unsigned long flags;
1635 unsigned char l, r, rl, rr;
1636 void __user *argp = (void __user *)arg;
1637 int __user *p = argp;
1640 if (cmd == SOUND_MIXER_INFO) {
1642 memset(&info, 0, sizeof(info));
1643 strlcpy(info.id, "cmpci", sizeof(info.id));
1644 strlcpy(info.name, "C-Media PCI", sizeof(info.name));
1645 info.modify_counter = s->mix.modcnt;
1646 if (copy_to_user(argp, &info, sizeof(info)))
1650 if (cmd == SOUND_OLD_MIXER_INFO) {
1651 _old_mixer_info info;
1652 memset(&info, 0, sizeof(info));
1653 strlcpy(info.id, "cmpci", sizeof(info.id));
1654 strlcpy(info.name, "C-Media cmpci", sizeof(info.name));
1655 if (copy_to_user(argp, &info, sizeof(info)))
1659 if (cmd == OSS_GETVERSION)
1660 return put_user(SOUND_VERSION, p);
1661 if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
1663 if (_SIOC_DIR(cmd) == _SIOC_READ) {
1664 switch (_IOC_NR(cmd)) {
1665 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
1666 val = mixer_recmask(s);
1667 return put_user(val, p);
1669 case SOUND_MIXER_OUTSRC: /* Arg contains a bit for each recording source */
1670 val = mixer_outmask(s);
1671 return put_user(val, p);
1673 case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */
1674 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1675 if (mixtable[i].type)
1677 return put_user(val, p);
1679 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
1680 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1681 if (mixtable[i].rec)
1683 return put_user(val, p);
1685 case SOUND_MIXER_OUTMASK: /* Arg contains a bit for each supported recording source */
1686 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1687 if (mixtable[i].play)
1689 return put_user(val, p);
1691 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
1692 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1693 if (mixtable[i].type && mixtable[i].type != MT_4MUTEMONO)
1695 return put_user(val, p);
1697 case SOUND_MIXER_CAPS:
1698 return put_user(0, p);
1702 if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
1706 return put_user(s->mix.vol[volidx[i]-1], p);
1709 if (_SIOC_DIR(cmd) != (_SIOC_READ|_SIOC_WRITE))
1712 switch (_IOC_NR(cmd)) {
1713 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
1714 if (get_user(val, p))
1716 i = generic_hweight32(val);
1717 for (j = i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1718 if (!(val & (1 << i)))
1720 if (!mixtable[i].rec) {
1724 j |= mixtable[i].rec;
1726 spin_lock_irqsave(&s->lock, flags);
1727 wrmixer(s, DSP_MIX_ADCMIXIDX_L, j);
1728 wrmixer(s, DSP_MIX_ADCMIXIDX_R, (j & 1) | (j>>1) | (j & 0x80));
1729 spin_unlock_irqrestore(&s->lock, flags);
1732 case SOUND_MIXER_OUTSRC: /* Arg contains a bit for each recording source */
1733 if (get_user(val, p))
1735 for (j = i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1736 if (!(val & (1 << i)))
1738 if (!mixtable[i].play) {
1742 j |= mixtable[i].play;
1744 spin_lock_irqsave(&s->lock, flags);
1745 wrmixer(s, DSP_MIX_OUTMIXIDX, j);
1746 spin_unlock_irqrestore(&s->lock, flags);
1751 if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
1753 if (get_user(val, p))
1756 r = (val >> 8) & 0xff;
1761 spin_lock_irqsave(&s->lock, flags);
1762 switch (mixtable[i].type) {
1768 frobindir(s, mixtable[i].left, 0xf0, l / 6);
1769 frobindir(s, mixtable[i].right, 0xf0, l / 6);
1773 rl = (l < 4 ? 0 : (l - 5) / 3) & 31;
1775 wrmixer(s, mixtable[i].left, rl<<3);
1776 if (i == SOUND_MIXER_MIC)
1777 maskb(s->iobase + CODEC_CMI_MIXER2, ~0x0e, rr<<1);
1781 rl = l < 4 ? 0 : (l - 5) / 3;
1782 wrmixer(s, mixtable[i].left, rl<<3);
1783 l = rdmixer(s, DSP_MIX_OUTMIXIDX) & ~mixtable[i].play;
1784 r = rl ? mixtable[i].play : 0;
1785 wrmixer(s, DSP_MIX_OUTMIXIDX, l | r);
1787 if (i == SOUND_MIXER_MIC) {
1788 if (s->chip_version >= 37) {
1790 maskb(s->iobase + CODEC_CMI_MIXER2, ~0x0e, (rr&0x07)<<1);
1791 frobindir(s, DSP_MIX_EXTENSION, ~0x01, rr>>3);
1794 maskb(s->iobase + CODEC_CMI_MIXER2, ~0x0e, rr<<1);
1800 rl = l < 4 ? 0 : (l - 5) / 3;
1801 rr = r < 4 ? 0 : (r - 5) / 3;
1802 wrmixer(s, mixtable[i].left, rl<<3);
1803 wrmixer(s, mixtable[i].right, rr<<3);
1804 l = rdmixer(s, DSP_MIX_OUTMIXIDX);
1805 l &= ~mixtable[i].play;
1806 r = (rl|rr) ? mixtable[i].play : 0;
1807 wrmixer(s, DSP_MIX_OUTMIXIDX, l | r);
1819 wrmixer(s, mixtable[i].left, rl);
1820 wrmixer(s, mixtable[i].right, rr);
1823 spin_unlock_irqrestore(&s->lock, flags);
1827 s->mix.vol[volidx[i]-1] = val;
1828 return put_user(s->mix.vol[volidx[i]-1], p);
1832 /* --------------------------------------------------------------------- */
1834 static int cm_open_mixdev(struct inode *inode, struct file *file)
1836 int minor = iminor(inode);
1837 struct list_head *list;
1840 for (list = devs.next; ; list = list->next) {
1843 s = list_entry(list, struct cm_state, devs);
1844 if (s->dev_mixer == minor)
1848 file->private_data = s;
1849 return nonseekable_open(inode, file);
1852 static int cm_release_mixdev(struct inode *inode, struct file *file)
1854 struct cm_state *s = (struct cm_state *)file->private_data;
1860 static int cm_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1862 return mixer_ioctl((struct cm_state *)file->private_data, cmd, arg);
1865 static /*const*/ struct file_operations cm_mixer_fops = {
1866 .owner = THIS_MODULE,
1867 .llseek = no_llseek,
1868 .ioctl = cm_ioctl_mixdev,
1869 .open = cm_open_mixdev,
1870 .release = cm_release_mixdev,
1874 /* --------------------------------------------------------------------- */
1876 static int drain_dac(struct cm_state *s, int nonblock)
1878 DECLARE_WAITQUEUE(wait, current);
1879 unsigned long flags;
1882 if (s->dma_dac.mapped || !s->dma_dac.ready)
1884 add_wait_queue(&s->dma_dac.wait, &wait);
1886 __set_current_state(TASK_INTERRUPTIBLE);
1887 spin_lock_irqsave(&s->lock, flags);
1888 count = s->dma_dac.count;
1889 spin_unlock_irqrestore(&s->lock, flags);
1892 if (signal_pending(current))
1895 remove_wait_queue(&s->dma_dac.wait, &wait);
1896 set_current_state(TASK_RUNNING);
1899 tmo = 3 * HZ * (count + s->dma_dac.fragsize) / 2 / s->ratedac;
1900 tmo >>= sample_shift[(s->fmt >> CM_CFMT_DACSHIFT) & CM_CFMT_MASK];
1901 if (!schedule_timeout(tmo + 1))
1902 DBG(printk(KERN_DEBUG "cmpci: dma timed out??\n");)
1904 remove_wait_queue(&s->dma_dac.wait, &wait);
1905 set_current_state(TASK_RUNNING);
1906 if (signal_pending(current))
1907 return -ERESTARTSYS;
1911 /* --------------------------------------------------------------------- */
1913 static ssize_t cm_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
1915 struct cm_state *s = (struct cm_state *)file->private_data;
1916 DECLARE_WAITQUEUE(wait, current);
1918 unsigned long flags;
1923 if (s->dma_adc.mapped)
1925 if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
1927 if (!access_ok(VERIFY_WRITE, buffer, count))
1931 add_wait_queue(&s->dma_adc.wait, &wait);
1933 spin_lock_irqsave(&s->lock, flags);
1934 swptr = s->dma_adc.swptr;
1935 cnt = s->dma_adc.dmasize-swptr;
1936 if (s->dma_adc.count < cnt)
1937 cnt = s->dma_adc.count;
1939 __set_current_state(TASK_INTERRUPTIBLE);
1940 spin_unlock_irqrestore(&s->lock, flags);
1944 if (s->dma_adc.enabled)
1946 if (file->f_flags & O_NONBLOCK) {
1951 if (!schedule_timeout(HZ)) {
1952 printk(KERN_DEBUG "cmpci: read: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1953 s->dma_adc.dmasize, s->dma_adc.fragsize, s->dma_adc.count,
1954 s->dma_adc.hwptr, s->dma_adc.swptr);
1955 spin_lock_irqsave(&s->lock, flags);
1956 stop_adc_unlocked(s);
1957 set_dmaadc(s, s->dma_adc.dmaaddr, s->dma_adc.dmasamples);
1958 /* program sample counts */
1959 set_countadc(s, s->dma_adc.fragsamples);
1960 s->dma_adc.count = s->dma_adc.hwptr = s->dma_adc.swptr = 0;
1961 spin_unlock_irqrestore(&s->lock, flags);
1963 if (signal_pending(current)) {
1970 if (s->status & DO_BIGENDIAN_R) {
1973 char __user *dst = buffer;
1974 unsigned char data[2];
1976 src = (unsigned char *) (s->dma_adc.rawbuf + swptr);
1977 // copy left/right sample at one time
1978 for (i = 0; i < cnt / 2; i++) {
1981 if ((err = __put_user(data[0], dst++))) {
1985 if ((err = __put_user(data[1], dst++))) {
1991 } else if (copy_to_user(buffer, s->dma_adc.rawbuf + swptr, cnt)) {
1996 swptr = (swptr + cnt) % s->dma_adc.dmasize;
1997 spin_lock_irqsave(&s->lock, flags);
1998 s->dma_adc.swptr = swptr;
1999 s->dma_adc.count -= cnt;
2003 if (s->dma_adc.enabled)
2004 start_adc_unlocked(s);
2005 spin_unlock_irqrestore(&s->lock, flags);
2008 remove_wait_queue(&s->dma_adc.wait, &wait);
2009 set_current_state(TASK_RUNNING);
2013 static ssize_t cm_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2015 struct cm_state *s = (struct cm_state *)file->private_data;
2016 DECLARE_WAITQUEUE(wait, current);
2018 unsigned long flags;
2023 if (s->dma_dac.mapped)
2025 if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
2027 if (!access_ok(VERIFY_READ, buffer, count))
2029 if (s->status & DO_DUAL_DAC) {
2030 if (s->dma_adc.mapped)
2032 if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
2035 if (!access_ok(VERIFY_READ, buffer, count))
2039 add_wait_queue(&s->dma_dac.wait, &wait);
2041 spin_lock_irqsave(&s->lock, flags);
2042 if (s->dma_dac.count < 0) {
2043 s->dma_dac.count = 0;
2044 s->dma_dac.swptr = s->dma_dac.hwptr;
2046 if (s->status & DO_DUAL_DAC) {
2047 s->dma_adc.swptr = s->dma_dac.swptr;
2048 s->dma_adc.count = s->dma_dac.count;
2049 s->dma_adc.endcleared = s->dma_dac.endcleared;
2051 swptr = s->dma_dac.swptr;
2052 cnt = s->dma_dac.dmasize-swptr;
2053 if (s->status & DO_AC3_SW) {
2054 if (s->dma_dac.count + 2 * cnt > s->dma_dac.dmasize)
2055 cnt = (s->dma_dac.dmasize - s->dma_dac.count) / 2;
2057 if (s->dma_dac.count + cnt > s->dma_dac.dmasize)
2058 cnt = s->dma_dac.dmasize - s->dma_dac.count;
2061 __set_current_state(TASK_INTERRUPTIBLE);
2062 spin_unlock_irqrestore(&s->lock, flags);
2065 if ((s->status & DO_DUAL_DAC) && (cnt > count / 2))
2068 if (s->dma_dac.enabled)
2070 if (file->f_flags & O_NONBLOCK) {
2075 if (!schedule_timeout(HZ)) {
2076 printk(KERN_DEBUG "cmpci: write: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
2077 s->dma_dac.dmasize, s->dma_dac.fragsize, s->dma_dac.count,
2078 s->dma_dac.hwptr, s->dma_dac.swptr);
2079 spin_lock_irqsave(&s->lock, flags);
2080 stop_dac_unlocked(s);
2081 set_dmadac(s, s->dma_dac.dmaaddr, s->dma_dac.dmasamples);
2082 /* program sample counts */
2083 set_countdac(s, s->dma_dac.fragsamples);
2084 s->dma_dac.count = s->dma_dac.hwptr = s->dma_dac.swptr = 0;
2085 if (s->status & DO_DUAL_DAC) {
2086 set_dmadac1(s, s->dma_adc.dmaaddr, s->dma_adc.dmasamples);
2087 s->dma_adc.count = s->dma_adc.hwptr = s->dma_adc.swptr = 0;
2089 spin_unlock_irqrestore(&s->lock, flags);
2091 if (signal_pending(current)) {
2098 if (s->status & DO_AC3_SW) {
2101 // clip exceeded data, caught by 033 and 037
2102 if (swptr + 2 * cnt > s->dma_dac.dmasize)
2103 cnt = (s->dma_dac.dmasize - swptr) / 2;
2104 if ((err = trans_ac3(s, s->dma_dac.rawbuf + swptr, buffer, cnt))) {
2108 swptr = (swptr + 2 * cnt) % s->dma_dac.dmasize;
2109 } else if ((s->status & DO_DUAL_DAC) && (s->status & DO_BIGENDIAN_W)) {
2111 const char __user *src = buffer;
2112 unsigned char *dst0, *dst1;
2113 unsigned char data[8];
2115 dst0 = (unsigned char *) (s->dma_dac.rawbuf + swptr);
2116 dst1 = (unsigned char *) (s->dma_adc.rawbuf + swptr);
2117 // copy left/right sample at one time
2118 for (i = 0; i < cnt / 4; i++) {
2119 if ((err = __get_user(data[0], src++))) {
2123 if ((err = __get_user(data[1], src++))) {
2127 if ((err = __get_user(data[2], src++))) {
2131 if ((err = __get_user(data[3], src++))) {
2135 if ((err = __get_user(data[4], src++))) {
2139 if ((err = __get_user(data[5], src++))) {
2143 if ((err = __get_user(data[6], src++))) {
2147 if ((err = __get_user(data[7], src++))) {
2162 swptr = (swptr + cnt) % s->dma_dac.dmasize;
2163 } else if (s->status & DO_DUAL_DAC) {
2165 unsigned long __user *src = (unsigned long __user *) buffer;
2166 unsigned long *dst0, *dst1;
2168 dst0 = (unsigned long *) (s->dma_dac.rawbuf + swptr);
2169 dst1 = (unsigned long *) (s->dma_adc.rawbuf + swptr);
2170 // copy left/right sample at one time
2171 for (i = 0; i < cnt / 4; i++) {
2172 if ((err = __get_user(*dst0++, src++))) {
2176 if ((err = __get_user(*dst1++, src++))) {
2181 swptr = (swptr + cnt) % s->dma_dac.dmasize;
2182 } else if (s->status & DO_BIGENDIAN_W) {
2184 const char __user *src = buffer;
2186 unsigned char data[2];
2188 dst = (unsigned char *) (s->dma_dac.rawbuf + swptr);
2189 // swap hi/lo bytes for each sample
2190 for (i = 0; i < cnt / 2; i++) {
2191 if ((err = __get_user(data[0], src++))) {
2195 if ((err = __get_user(data[1], src++))) {
2203 swptr = (swptr + cnt) % s->dma_dac.dmasize;
2205 if (copy_from_user(s->dma_dac.rawbuf + swptr, buffer, cnt)) {
2210 swptr = (swptr + cnt) % s->dma_dac.dmasize;
2212 spin_lock_irqsave(&s->lock, flags);
2213 s->dma_dac.swptr = swptr;
2214 s->dma_dac.count += cnt;
2215 if (s->status & DO_AC3_SW)
2216 s->dma_dac.count += cnt;
2217 s->dma_dac.endcleared = 0;
2218 spin_unlock_irqrestore(&s->lock, flags);
2222 if (s->status & DO_DUAL_DAC) {
2227 if (s->dma_dac.enabled)
2231 remove_wait_queue(&s->dma_dac.wait, &wait);
2232 set_current_state(TASK_RUNNING);
2236 static unsigned int cm_poll(struct file *file, struct poll_table_struct *wait)
2238 struct cm_state *s = (struct cm_state *)file->private_data;
2239 unsigned long flags;
2240 unsigned int mask = 0;
2243 if (file->f_mode & FMODE_WRITE) {
2244 if (!s->dma_dac.ready && prog_dmabuf(s, 0))
2246 poll_wait(file, &s->dma_dac.wait, wait);
2248 if (file->f_mode & FMODE_READ) {
2249 if (!s->dma_adc.ready && prog_dmabuf(s, 1))
2251 poll_wait(file, &s->dma_adc.wait, wait);
2253 spin_lock_irqsave(&s->lock, flags);
2255 if (file->f_mode & FMODE_READ) {
2256 if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
2257 mask |= POLLIN | POLLRDNORM;
2259 if (file->f_mode & FMODE_WRITE) {
2260 if (s->dma_dac.mapped) {
2261 if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
2262 mask |= POLLOUT | POLLWRNORM;
2264 if ((signed)s->dma_dac.dmasize >= s->dma_dac.count + (signed)s->dma_dac.fragsize)
2265 mask |= POLLOUT | POLLWRNORM;
2268 spin_unlock_irqrestore(&s->lock, flags);
2272 static int cm_mmap(struct file *file, struct vm_area_struct *vma)
2274 struct cm_state *s = (struct cm_state *)file->private_data;
2281 if (vma->vm_flags & VM_WRITE) {
2282 if ((ret = prog_dmabuf(s, 0)) != 0)
2285 } else if (vma->vm_flags & VM_READ) {
2286 if ((ret = prog_dmabuf(s, 1)) != 0)
2292 if (vma->vm_pgoff != 0)
2294 size = vma->vm_end - vma->vm_start;
2295 if (size > (PAGE_SIZE << db->buforder))
2298 if (remap_pfn_range(vma, vma->vm_start,
2299 virt_to_phys(db->rawbuf) >> PAGE_SHIFT,
2300 size, vma->vm_page_prot))
2309 #define SNDCTL_SPDIF_COPYRIGHT _SIOW('S', 0, int) // set/reset S/PDIF copy protection
2310 #define SNDCTL_SPDIF_LOOP _SIOW('S', 1, int) // set/reset S/PDIF loop
2311 #define SNDCTL_SPDIF_MONITOR _SIOW('S', 2, int) // set S/PDIF monitor
2312 #define SNDCTL_SPDIF_LEVEL _SIOW('S', 3, int) // set/reset S/PDIF out level
2313 #define SNDCTL_SPDIF_INV _SIOW('S', 4, int) // set/reset S/PDIF in inverse
2314 #define SNDCTL_SPDIF_SEL2 _SIOW('S', 5, int) // set S/PDIF in #2
2315 #define SNDCTL_SPDIF_VALID _SIOW('S', 6, int) // set S/PDIF valid
2316 #define SNDCTL_SPDIFOUT _SIOW('S', 7, int) // set S/PDIF out
2317 #define SNDCTL_SPDIFIN _SIOW('S', 8, int) // set S/PDIF out
2319 static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2321 struct cm_state *s = (struct cm_state *)file->private_data;
2322 unsigned long flags;
2323 audio_buf_info abinfo;
2325 int val, mapped, ret;
2326 unsigned char fmtm, fmtd;
2327 void __user *argp = (void __user *)arg;
2328 int __user *p = argp;
2331 mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
2332 ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
2334 case OSS_GETVERSION:
2335 return put_user(SOUND_VERSION, p);
2337 case SNDCTL_DSP_SYNC:
2338 if (file->f_mode & FMODE_WRITE)
2339 return drain_dac(s, 0/*file->f_flags & O_NONBLOCK*/);
2342 case SNDCTL_DSP_SETDUPLEX:
2345 case SNDCTL_DSP_GETCAPS:
2346 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_BIND, p);
2348 case SNDCTL_DSP_RESET:
2349 if (file->f_mode & FMODE_WRITE) {
2351 synchronize_irq(s->irq);
2352 s->dma_dac.swptr = s->dma_dac.hwptr = s->dma_dac.count = s->dma_dac.total_bytes = 0;
2353 if (s->status & DO_DUAL_DAC)
2354 s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes = 0;
2356 if (file->f_mode & FMODE_READ) {
2358 synchronize_irq(s->irq);
2359 s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes = 0;
2363 case SNDCTL_DSP_SPEED:
2364 if (get_user(val, p))
2367 if (file->f_mode & FMODE_READ) {
2368 spin_lock_irqsave(&s->lock, flags);
2369 stop_adc_unlocked(s);
2370 s->dma_adc.ready = 0;
2371 set_adc_rate_unlocked(s, val);
2372 spin_unlock_irqrestore(&s->lock, flags);
2374 if (file->f_mode & FMODE_WRITE) {
2376 s->dma_dac.ready = 0;
2377 if (s->status & DO_DUAL_DAC)
2378 s->dma_adc.ready = 0;
2379 set_dac_rate(s, val);
2382 return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
2384 case SNDCTL_DSP_STEREO:
2385 if (get_user(val, p))
2389 if (file->f_mode & FMODE_READ) {
2391 s->dma_adc.ready = 0;
2393 fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
2395 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
2397 if (file->f_mode & FMODE_WRITE) {
2399 s->dma_dac.ready = 0;
2401 fmtd |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
2403 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_DACSHIFT);
2404 if (s->status & DO_DUAL_DAC) {
2405 s->dma_adc.ready = 0;
2407 fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
2409 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
2412 set_fmt(s, fmtm, fmtd);
2415 case SNDCTL_DSP_CHANNELS:
2416 if (get_user(val, p))
2421 if (file->f_mode & FMODE_READ) {
2423 s->dma_adc.ready = 0;
2425 fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
2427 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
2429 if (file->f_mode & FMODE_WRITE) {
2431 s->dma_dac.ready = 0;
2433 fmtd |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
2435 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_DACSHIFT);
2436 if (s->status & DO_DUAL_DAC) {
2437 s->dma_adc.ready = 0;
2439 fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
2441 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
2444 set_fmt(s, fmtm, fmtd);
2445 if ((s->capability & CAN_MULTI_CH)
2446 && (file->f_mode & FMODE_WRITE)) {
2447 val = set_dac_channels(s, val);
2448 return put_user(val, p);
2451 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_STEREO << CM_CFMT_ADCSHIFT)
2452 : (CM_CFMT_STEREO << CM_CFMT_DACSHIFT))) ? 2 : 1, p);
2454 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
2455 return put_user(AFMT_S16_BE|AFMT_S16_LE|AFMT_U8|
2456 ((s->capability & CAN_AC3) ? AFMT_AC3 : 0), p);
2458 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
2459 if (get_user(val, p))
2461 if (val != AFMT_QUERY) {
2464 if (file->f_mode & FMODE_READ) {
2466 s->dma_adc.ready = 0;
2467 if (val == AFMT_S16_BE || val == AFMT_S16_LE)
2468 fmtd |= CM_CFMT_16BIT << CM_CFMT_ADCSHIFT;
2470 fmtm &= ~(CM_CFMT_16BIT << CM_CFMT_ADCSHIFT);
2471 if (val == AFMT_S16_BE)
2472 s->status |= DO_BIGENDIAN_R;
2474 s->status &= ~DO_BIGENDIAN_R;
2476 if (file->f_mode & FMODE_WRITE) {
2478 s->dma_dac.ready = 0;
2479 if (val == AFMT_S16_BE || val == AFMT_S16_LE || val == AFMT_AC3)
2480 fmtd |= CM_CFMT_16BIT << CM_CFMT_DACSHIFT;
2482 fmtm &= ~(CM_CFMT_16BIT << CM_CFMT_DACSHIFT);
2483 if (val == AFMT_AC3) {
2484 fmtd |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
2488 if (s->status & DO_DUAL_DAC) {
2489 s->dma_adc.ready = 0;
2490 if (val == AFMT_S16_BE || val == AFMT_S16_LE)
2491 fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
2493 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
2495 if (val == AFMT_S16_BE)
2496 s->status |= DO_BIGENDIAN_W;
2498 s->status &= ~DO_BIGENDIAN_W;
2500 set_fmt(s, fmtm, fmtd);
2502 if (s->status & DO_AC3) return put_user(AFMT_AC3, p);
2503 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_16BIT << CM_CFMT_ADCSHIFT)
2504 : (CM_CFMT_16BIT << CM_CFMT_DACSHIFT))) ? val : AFMT_U8, p);
2506 case SNDCTL_DSP_POST:
2509 case SNDCTL_DSP_GETTRIGGER:
2511 if (s->status & DO_DUAL_DAC) {
2512 if (file->f_mode & FMODE_WRITE &&
2513 (s->enable & ENDAC) &&
2514 (s->enable & ENADC))
2515 val |= PCM_ENABLE_OUTPUT;
2516 return put_user(val, p);
2518 if (file->f_mode & FMODE_READ && s->enable & ENADC)
2519 val |= PCM_ENABLE_INPUT;
2520 if (file->f_mode & FMODE_WRITE && s->enable & ENDAC)
2521 val |= PCM_ENABLE_OUTPUT;
2522 return put_user(val, p);
2524 case SNDCTL_DSP_SETTRIGGER:
2525 if (get_user(val, p))
2527 if (file->f_mode & FMODE_READ) {
2528 if (val & PCM_ENABLE_INPUT) {
2529 if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
2531 s->dma_adc.enabled = 1;
2534 s->dma_adc.enabled = 0;
2538 if (file->f_mode & FMODE_WRITE) {
2539 if (val & PCM_ENABLE_OUTPUT) {
2540 if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
2542 if (s->status & DO_DUAL_DAC) {
2543 if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
2546 s->dma_dac.enabled = 1;
2549 s->dma_dac.enabled = 0;
2555 case SNDCTL_DSP_GETOSPACE:
2556 if (!(file->f_mode & FMODE_WRITE))
2558 if (!(s->enable & ENDAC) && (val = prog_dmabuf(s, 0)) != 0)
2560 spin_lock_irqsave(&s->lock, flags);
2562 abinfo.fragsize = s->dma_dac.fragsize;
2563 abinfo.bytes = s->dma_dac.dmasize - s->dma_dac.count;
2564 abinfo.fragstotal = s->dma_dac.numfrag;
2565 abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
2566 spin_unlock_irqrestore(&s->lock, flags);
2567 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2569 case SNDCTL_DSP_GETISPACE:
2570 if (!(file->f_mode & FMODE_READ))
2572 if (!(s->enable & ENADC) && (val = prog_dmabuf(s, 1)) != 0)
2574 spin_lock_irqsave(&s->lock, flags);
2576 abinfo.fragsize = s->dma_adc.fragsize;
2577 abinfo.bytes = s->dma_adc.count;
2578 abinfo.fragstotal = s->dma_adc.numfrag;
2579 abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;
2580 spin_unlock_irqrestore(&s->lock, flags);
2581 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2583 case SNDCTL_DSP_NONBLOCK:
2584 file->f_flags |= O_NONBLOCK;
2587 case SNDCTL_DSP_GETODELAY:
2588 if (!(file->f_mode & FMODE_WRITE))
2590 spin_lock_irqsave(&s->lock, flags);
2592 val = s->dma_dac.count;
2593 spin_unlock_irqrestore(&s->lock, flags);
2594 return put_user(val, p);
2596 case SNDCTL_DSP_GETIPTR:
2597 if (!(file->f_mode & FMODE_READ))
2599 spin_lock_irqsave(&s->lock, flags);
2601 cinfo.bytes = s->dma_adc.total_bytes;
2602 cinfo.blocks = s->dma_adc.count >> s->dma_adc.fragshift;
2603 cinfo.ptr = s->dma_adc.hwptr;
2604 if (s->dma_adc.mapped)
2605 s->dma_adc.count &= s->dma_adc.fragsize-1;
2606 spin_unlock_irqrestore(&s->lock, flags);
2607 return copy_to_user(argp, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
2609 case SNDCTL_DSP_GETOPTR:
2610 if (!(file->f_mode & FMODE_WRITE))
2612 spin_lock_irqsave(&s->lock, flags);
2614 cinfo.bytes = s->dma_dac.total_bytes;
2615 cinfo.blocks = s->dma_dac.count >> s->dma_dac.fragshift;
2616 cinfo.ptr = s->dma_dac.hwptr;
2617 if (s->dma_dac.mapped)
2618 s->dma_dac.count &= s->dma_dac.fragsize-1;
2619 if (s->status & DO_DUAL_DAC) {
2620 if (s->dma_adc.mapped)
2621 s->dma_adc.count &= s->dma_adc.fragsize-1;
2623 spin_unlock_irqrestore(&s->lock, flags);
2624 return copy_to_user(argp, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
2626 case SNDCTL_DSP_GETBLKSIZE:
2627 if (file->f_mode & FMODE_WRITE) {
2628 if ((val = prog_dmabuf(s, 0)))
2630 if (s->status & DO_DUAL_DAC) {
2631 if ((val = prog_dmabuf(s, 1)))
2633 return put_user(2 * s->dma_dac.fragsize, p);
2635 return put_user(s->dma_dac.fragsize, p);
2637 if ((val = prog_dmabuf(s, 1)))
2639 return put_user(s->dma_adc.fragsize, p);
2641 case SNDCTL_DSP_SETFRAGMENT:
2642 if (get_user(val, p))
2644 if (file->f_mode & FMODE_READ) {
2645 s->dma_adc.ossfragshift = val & 0xffff;
2646 s->dma_adc.ossmaxfrags = (val >> 16) & 0xffff;
2647 if (s->dma_adc.ossfragshift < 4)
2648 s->dma_adc.ossfragshift = 4;
2649 if (s->dma_adc.ossfragshift > 15)
2650 s->dma_adc.ossfragshift = 15;
2651 if (s->dma_adc.ossmaxfrags < 4)
2652 s->dma_adc.ossmaxfrags = 4;
2654 if (file->f_mode & FMODE_WRITE) {
2655 s->dma_dac.ossfragshift = val & 0xffff;
2656 s->dma_dac.ossmaxfrags = (val >> 16) & 0xffff;
2657 if (s->dma_dac.ossfragshift < 4)
2658 s->dma_dac.ossfragshift = 4;
2659 if (s->dma_dac.ossfragshift > 15)
2660 s->dma_dac.ossfragshift = 15;
2661 if (s->dma_dac.ossmaxfrags < 4)
2662 s->dma_dac.ossmaxfrags = 4;
2663 if (s->status & DO_DUAL_DAC) {
2664 s->dma_adc.ossfragshift = s->dma_dac.ossfragshift;
2665 s->dma_adc.ossmaxfrags = s->dma_dac.ossmaxfrags;
2670 case SNDCTL_DSP_SUBDIVIDE:
2671 if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
2672 (file->f_mode & FMODE_WRITE && s->dma_dac.subdivision))
2674 if (get_user(val, p))
2676 if (val != 1 && val != 2 && val != 4)
2678 if (file->f_mode & FMODE_READ)
2679 s->dma_adc.subdivision = val;
2680 if (file->f_mode & FMODE_WRITE) {
2681 s->dma_dac.subdivision = val;
2682 if (s->status & DO_DUAL_DAC)
2683 s->dma_adc.subdivision = val;
2687 case SOUND_PCM_READ_RATE:
2688 return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
2690 case SOUND_PCM_READ_CHANNELS:
2691 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_STEREO << CM_CFMT_ADCSHIFT) : (CM_CFMT_STEREO << CM_CFMT_DACSHIFT))) ? 2 : 1, p);
2693 case SOUND_PCM_READ_BITS:
2694 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_16BIT << CM_CFMT_ADCSHIFT) : (CM_CFMT_16BIT << CM_CFMT_DACSHIFT))) ? 16 : 8, p);
2696 case SOUND_PCM_READ_FILTER:
2697 return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
2699 case SNDCTL_DSP_GETCHANNELMASK:
2700 return put_user(DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE|DSP_BIND_SPDIF, p);
2702 case SNDCTL_DSP_BIND_CHANNEL:
2703 if (get_user(val, p))
2705 if (val == DSP_BIND_QUERY) {
2706 val = DSP_BIND_FRONT;
2707 if (s->status & DO_SPDIF_OUT)
2708 val |= DSP_BIND_SPDIF;
2710 if (s->curr_channels == 4)
2711 val |= DSP_BIND_SURR;
2712 if (s->curr_channels > 4)
2713 val |= DSP_BIND_CENTER_LFE;
2716 if (file->f_mode & FMODE_READ) {
2718 s->dma_adc.ready = 0;
2719 if (val & DSP_BIND_SPDIF) {
2720 set_spdifin(s, s->rateadc);
2721 if (!(s->status & DO_SPDIF_OUT))
2722 val &= ~DSP_BIND_SPDIF;
2725 if (file->f_mode & FMODE_WRITE) {
2727 s->dma_dac.ready = 0;
2728 if (val & DSP_BIND_SPDIF) {
2729 set_spdifout(s, s->ratedac);
2730 set_dac_channels(s, s->fmt & (CM_CFMT_STEREO << CM_CFMT_DACSHIFT) ? 2 : 1);
2731 if (!(s->status & DO_SPDIF_OUT))
2732 val &= ~DSP_BIND_SPDIF;
2737 mask = val & (DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE);
2739 case DSP_BIND_FRONT:
2742 case DSP_BIND_FRONT|DSP_BIND_SURR:
2745 case DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE:
2749 channels = s->fmt & (CM_CFMT_STEREO << CM_CFMT_DACSHIFT) ? 2 : 1;
2752 set_dac_channels(s, channels);
2756 return put_user(val, p);
2758 case SOUND_PCM_WRITE_FILTER:
2759 case SNDCTL_DSP_MAPINBUF:
2760 case SNDCTL_DSP_MAPOUTBUF:
2761 case SNDCTL_DSP_SETSYNCRO:
2763 case SNDCTL_SPDIF_COPYRIGHT:
2764 if (get_user(val, p))
2766 set_spdif_copyright(s, val);
2768 case SNDCTL_SPDIF_LOOP:
2769 if (get_user(val, p))
2771 set_spdif_loop(s, val);
2773 case SNDCTL_SPDIF_MONITOR:
2774 if (get_user(val, p))
2776 set_spdif_monitor(s, val);
2778 case SNDCTL_SPDIF_LEVEL:
2779 if (get_user(val, p))
2781 set_spdifout_level(s, val);
2783 case SNDCTL_SPDIF_INV:
2784 if (get_user(val, p))
2786 set_spdifin_inverse(s, val);
2788 case SNDCTL_SPDIF_SEL2:
2789 if (get_user(val, p))
2791 set_spdifin_channel2(s, val);
2793 case SNDCTL_SPDIF_VALID:
2794 if (get_user(val, p))
2796 set_spdifin_valid(s, val);
2798 case SNDCTL_SPDIFOUT:
2799 if (get_user(val, p))
2801 set_spdifout(s, val ? s->ratedac : 0);
2803 case SNDCTL_SPDIFIN:
2804 if (get_user(val, p))
2806 set_spdifin(s, val ? s->rateadc : 0);
2809 return mixer_ioctl(s, cmd, arg);
2812 static int cm_open(struct inode *inode, struct file *file)
2814 int minor = iminor(inode);
2815 DECLARE_WAITQUEUE(wait, current);
2816 unsigned char fmtm = ~0, fmts = 0;
2817 struct list_head *list;
2820 for (list = devs.next; ; list = list->next) {
2823 s = list_entry(list, struct cm_state, devs);
2824 if (!((s->dev_audio ^ minor) & ~0xf))
2828 file->private_data = s;
2829 /* wait for device to become free */
2830 mutex_lock(&s->open_mutex);
2831 while (s->open_mode & file->f_mode) {
2832 if (file->f_flags & O_NONBLOCK) {
2833 mutex_unlock(&s->open_mutex);
2836 add_wait_queue(&s->open_wait, &wait);
2837 __set_current_state(TASK_INTERRUPTIBLE);
2838 mutex_unlock(&s->open_mutex);
2840 remove_wait_queue(&s->open_wait, &wait);
2841 set_current_state(TASK_RUNNING);
2842 if (signal_pending(current))
2843 return -ERESTARTSYS;
2844 mutex_lock(&s->open_mutex);
2846 if (file->f_mode & FMODE_READ) {
2847 s->status &= ~DO_BIGENDIAN_R;
2848 fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_ADCSHIFT);
2849 if ((minor & 0xf) == SND_DEV_DSP16)
2850 fmts |= CM_CFMT_16BIT << CM_CFMT_ADCSHIFT;
2851 s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags = s->dma_adc.subdivision = 0;
2852 s->dma_adc.enabled = 1;
2853 set_adc_rate(s, 8000);
2854 // spdif-in is turnned off by default
2857 if (file->f_mode & FMODE_WRITE) {
2858 s->status &= ~DO_BIGENDIAN_W;
2859 fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_DACSHIFT);
2860 if ((minor & 0xf) == SND_DEV_DSP16)
2861 fmts |= CM_CFMT_16BIT << CM_CFMT_DACSHIFT;
2862 s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags = s->dma_dac.subdivision = 0;
2863 s->dma_dac.enabled = 1;
2864 set_dac_rate(s, 8000);
2865 // clear previous multichannel, spdif, ac3 state
2868 set_dac_channels(s, 1);
2870 set_fmt(s, fmtm, fmts);
2871 s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
2872 mutex_unlock(&s->open_mutex);
2873 return nonseekable_open(inode, file);
2876 static int cm_release(struct inode *inode, struct file *file)
2878 struct cm_state *s = (struct cm_state *)file->private_data;
2882 if (file->f_mode & FMODE_WRITE)
2883 drain_dac(s, file->f_flags & O_NONBLOCK);
2884 mutex_lock(&s->open_mutex);
2885 if (file->f_mode & FMODE_WRITE) {
2888 dealloc_dmabuf(s, &s->dma_dac);
2889 if (s->status & DO_DUAL_DAC)
2890 dealloc_dmabuf(s, &s->dma_adc);
2892 if (s->status & DO_MULTI_CH)
2893 set_dac_channels(s, 1);
2894 if (s->status & DO_AC3)
2896 if (s->status & DO_SPDIF_OUT)
2898 /* enable SPDIF loop */
2899 set_spdif_loop(s, spdif_loop);
2900 s->status &= ~DO_BIGENDIAN_W;
2902 if (file->f_mode & FMODE_READ) {
2904 dealloc_dmabuf(s, &s->dma_adc);
2905 s->status &= ~DO_BIGENDIAN_R;
2907 s->open_mode &= ~(file->f_mode & (FMODE_READ|FMODE_WRITE));
2908 mutex_unlock(&s->open_mutex);
2909 wake_up(&s->open_wait);
2914 static /*const*/ struct file_operations cm_audio_fops = {
2915 .owner = THIS_MODULE,
2916 .llseek = no_llseek,
2923 .release = cm_release,
2926 /* --------------------------------------------------------------------- */
2928 static struct initvol {
2931 } initvol[] __devinitdata = {
2932 { SOUND_MIXER_WRITE_CD, 0x4f4f },
2933 { SOUND_MIXER_WRITE_LINE, 0x4f4f },
2934 { SOUND_MIXER_WRITE_MIC, 0x4f4f },
2935 { SOUND_MIXER_WRITE_SYNTH, 0x4f4f },
2936 { SOUND_MIXER_WRITE_VOLUME, 0x4f4f },
2937 { SOUND_MIXER_WRITE_PCM, 0x4f4f }
2940 /* check chip version and capability */
2941 static int query_chip(struct cm_state *s)
2943 int ChipVersion = -1;
2944 unsigned char RegValue;
2946 // check reg 0Ch, bit 24-31
2947 RegValue = inb(s->iobase + CODEC_CMI_INT_HLDCLR + 3);
2948 if (RegValue == 0) {
2949 // check reg 08h, bit 24-28
2950 RegValue = inb(s->iobase + CODEC_CMI_CHFORMAT + 3);
2952 if (RegValue == 0) {
2954 s->max_channels = 4;
2955 s->capability |= CAN_AC3_SW;
2956 s->capability |= CAN_DUAL_DAC;
2959 s->max_channels = 4;
2960 s->capability |= CAN_AC3_HW;
2961 s->capability |= CAN_DUAL_DAC;
2964 // check reg 0Ch, bit 26
2965 if (RegValue & (1 << (26-24))) {
2967 if (RegValue & (1 << (24-24)))
2968 s->max_channels = 6;
2970 s->max_channels = 4;
2971 s->capability |= CAN_AC3_HW;
2972 s->capability |= CAN_DUAL_DAC;
2973 s->capability |= CAN_MULTI_CH_HW;
2974 s->capability |= CAN_LINE_AS_BASS;
2975 s->capability |= CAN_MIC_AS_BASS;
2977 ChipVersion = 55; // 4 or 6 channels
2978 s->max_channels = 6;
2979 s->capability |= CAN_AC3_HW;
2980 s->capability |= CAN_DUAL_DAC;
2981 s->capability |= CAN_MULTI_CH_HW;
2982 s->capability |= CAN_LINE_AS_BASS;
2983 s->capability |= CAN_MIC_AS_BASS;
2986 s->capability |= CAN_LINE_AS_REAR;
2990 #ifdef CONFIG_SOUND_CMPCI_JOYSTICK
2991 static int __devinit cm_create_gameport(struct cm_state *s, int io_port)
2993 struct gameport *gp;
2995 if (!request_region(io_port, CM_EXTENT_GAME, "cmpci GAME")) {
2996 printk(KERN_ERR "cmpci: gameport io ports 0x%#x in use\n", io_port);
3000 if (!(s->gameport = gp = gameport_allocate_port())) {
3001 printk(KERN_ERR "cmpci: can not allocate memory for gameport\n");
3002 release_region(io_port, CM_EXTENT_GAME);
3006 gameport_set_name(gp, "C-Media GP");
3007 gameport_set_phys(gp, "pci%s/gameport0", pci_name(s->dev));
3008 gp->dev.parent = &s->dev->dev;
3011 /* enable joystick */
3012 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x02);
3014 gameport_register_port(gp);
3019 static void __devexit cm_free_gameport(struct cm_state *s)
3022 int gpio = s->gameport->io;
3024 gameport_unregister_port(s->gameport);
3026 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x02, 0);
3027 release_region(gpio, CM_EXTENT_GAME);
3031 static inline int cm_create_gameport(struct cm_state *s, int io_port) { return -ENOSYS; }
3032 static inline void cm_free_gameport(struct cm_state *s) { }
3035 #define echo_option(x)\
3036 if (x) strcat(options, "" #x " ")
3038 static int __devinit cm_probe(struct pci_dev *pcidev, const struct pci_device_id *pciid)
3043 unsigned char reg_mask;
3045 struct resource *ports;
3047 unsigned short deviceid;
3050 { PCI_DEVICE_ID_CMEDIA_CM8338A, "CM8338A" },
3051 { PCI_DEVICE_ID_CMEDIA_CM8338B, "CM8338B" },
3052 { PCI_DEVICE_ID_CMEDIA_CM8738, "CM8738" },
3053 { PCI_DEVICE_ID_CMEDIA_CM8738B, "CM8738B" },
3055 char *devicename = "unknown";
3058 if ((ret = pci_enable_device(pcidev)))
3060 if (!(pci_resource_flags(pcidev, 0) & IORESOURCE_IO))
3062 if (pcidev->irq == 0)
3064 i = pci_set_dma_mask(pcidev, DMA_32BIT_MASK);
3066 printk(KERN_WARNING "cmpci: architecture does not support 32bit PCI busmaster DMA\n");
3069 s = kmalloc(sizeof(*s), GFP_KERNEL);
3071 printk(KERN_WARNING "cmpci: out of memory\n");
3074 /* search device name */
3075 for (i = 0; i < sizeof(devicetable) / sizeof(devicetable[0]); i++) {
3076 if (devicetable[i].deviceid == pcidev->device) {
3077 devicename = devicetable[i].devicename;
3081 memset(s, 0, sizeof(struct cm_state));
3082 init_waitqueue_head(&s->dma_adc.wait);
3083 init_waitqueue_head(&s->dma_dac.wait);
3084 init_waitqueue_head(&s->open_wait);
3085 mutex_init(&s->open_mutex);
3086 spin_lock_init(&s->lock);
3087 s->magic = CM_MAGIC;
3089 s->iobase = pci_resource_start(pcidev, 0);
3092 #ifdef CONFIG_SOUND_CMPCI_MIDI
3098 s->irq = pcidev->irq;
3100 if (!request_region(s->iobase, CM_EXTENT_CODEC, "cmpci")) {
3101 printk(KERN_ERR "cmpci: io ports %#x-%#x in use\n", s->iobase, s->iobase+CM_EXTENT_CODEC-1);
3105 /* dump parameters */
3106 strcpy(options, "cmpci: ");
3107 echo_option(joystick);
3108 echo_option(spdif_inverse);
3109 echo_option(spdif_loop);
3110 echo_option(spdif_out);
3111 echo_option(use_line_as_rear);
3112 echo_option(use_line_as_bass);
3113 echo_option(use_mic_as_bass);
3114 echo_option(mic_boost);
3115 echo_option(hw_copy);
3116 printk(KERN_INFO "%s\n", options);
3118 /* initialize codec registers */
3119 outb(0, s->iobase + CODEC_CMI_INT_HLDCLR + 2); /* disable ints */
3120 outb(0, s->iobase + CODEC_CMI_FUNCTRL0 + 2); /* disable channels */
3122 wrmixer(s, DSP_MIX_DATARESETIDX, 0);
3125 if ((ret = request_irq(s->irq, cm_interrupt, SA_SHIRQ, "cmpci", s))) {
3126 printk(KERN_ERR "cmpci: irq %u in use\n", s->irq);
3129 printk(KERN_INFO "cmpci: found %s adapter at io %#x irq %u\n",
3130 devicename, s->iobase, s->irq);
3131 /* register devices */
3132 if ((s->dev_audio = register_sound_dsp(&cm_audio_fops, -1)) < 0) {
3136 if ((s->dev_mixer = register_sound_mixer(&cm_mixer_fops, -1)) < 0) {
3140 pci_set_master(pcidev); /* enable bus mastering */
3141 /* initialize the chips */
3144 /* set mixer output */
3145 frobindir(s, DSP_MIX_OUTMIXIDX, 0x1f, 0x1f);
3146 /* set mixer input */
3147 val = SOUND_MASK_LINE|SOUND_MASK_SYNTH|SOUND_MASK_CD|SOUND_MASK_MIC;
3148 mixer_ioctl(s, SOUND_MIXER_WRITE_RECSRC, (unsigned long)&val);
3149 for (i = 0; i < sizeof(initvol)/sizeof(initvol[0]); i++) {
3150 val = initvol[i].vol;
3151 mixer_ioctl(s, initvol[i].mixch, (unsigned long)&val);
3154 /* use channel 1 for playback, channel 0 for record */
3155 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~CHADC1, CHADC0);
3156 /* turn off VMIC3 - mic boost */
3158 maskb(s->iobase + CODEC_CMI_MIXER2, ~1, 0);
3160 maskb(s->iobase + CODEC_CMI_MIXER2, ~0, 1);
3161 s->deviceid = pcidev->device;
3163 if (pcidev->device == PCI_DEVICE_ID_CMEDIA_CM8738
3164 || pcidev->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
3166 /* chip version and hw capability check */
3167 s->chip_version = query_chip(s);
3168 printk(KERN_INFO "cmpci: chip version = 0%d\n", s->chip_version);
3170 /* set SPDIF-in inverse before enable SPDIF loop */
3171 set_spdifin_inverse(s, spdif_inverse);
3173 /* use SPDIF in #1 */
3174 set_spdifin_channel2(s, 0);
3176 s->chip_version = 0;
3177 /* 8338 will fall here */
3178 s->max_channels = 4;
3179 s->capability |= CAN_DUAL_DAC;
3180 s->capability |= CAN_LINE_AS_REAR;
3182 /* enable SPDIF loop */
3183 set_spdif_loop(s, spdif_loop);
3185 // enable 4 speaker mode (analog duplicate)
3186 set_hw_copy(s, hw_copy);
3189 #ifdef CONFIG_SOUND_CMPCI_FM
3191 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~8, 0);
3193 /* don't enable OPL3 if there is one */
3194 if (opl3_detect(s->iosynth, NULL)) {
3197 /* set IO based at 0x388 */
3198 switch (s->iosynth) {
3215 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3, ~0x03, reg_mask);
3218 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0, 8);
3219 if (opl3_detect(s->iosynth, NULL))
3220 ret = opl3_init(s->iosynth, NULL, THIS_MODULE);
3222 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~8, 0);
3229 #ifdef CONFIG_SOUND_CMPCI_MIDI
3230 switch (s->iomidi) {
3247 ports = request_region(s->iomidi, 2, "mpu401");
3250 /* disable MPU-401 */
3251 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x04, 0);
3252 s->mpu_data.name = "cmpci mpu";
3253 s->mpu_data.io_base = s->iomidi;
3254 s->mpu_data.irq = -s->irq; // tell mpu401 to share irq
3255 if (probe_mpu401(&s->mpu_data, ports)) {
3256 release_region(s->iomidi, 2);
3260 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3, ~0x60, reg_mask);
3261 /* enable MPU-401 */
3262 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x04);
3263 /* clear all previously received interrupt */
3264 for (timeout = 900000; timeout > 0; timeout--) {
3265 if ((inb(s->iomidi + 1) && 0x80) == 0)
3270 if (!probe_mpu401(&s->mpu_data, ports)) {
3271 release_region(s->iomidi, 2);
3273 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x04);
3275 attach_mpu401(&s->mpu_data, THIS_MODULE);
3276 s->midi_devc = s->mpu_data.slots[1];
3280 /* disable joystick port */
3281 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x02, 0);
3283 cm_create_gameport(s, 0x200);
3285 /* store it in the driver field */
3286 pci_set_drvdata(pcidev, s);
3287 /* put it into driver list */
3288 list_add_tail(&s->devs, &devs);
3289 /* increment devindex */
3290 if (devindex < NR_DEVICE-1)
3295 unregister_sound_dsp(s->dev_audio);
3297 printk(KERN_ERR "cmpci: cannot register misc device\n");
3298 free_irq(s->irq, s);
3300 release_region(s->iobase, CM_EXTENT_CODEC);
3306 /* --------------------------------------------------------------------- */
3308 MODULE_AUTHOR("ChenLi Tien, cltien@cmedia.com.tw");
3309 MODULE_DESCRIPTION("CM8x38 Audio Driver");
3310 MODULE_LICENSE("GPL");
3312 static void __devexit cm_remove(struct pci_dev *dev)
3314 struct cm_state *s = pci_get_drvdata(dev);
3319 cm_free_gameport(s);
3321 #ifdef CONFIG_SOUND_CMPCI_FM
3324 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~8, 0);
3327 #ifdef CONFIG_SOUND_CMPCI_MIDI
3329 unload_mpu401(&s->mpu_data);
3330 /* disable MPU-401 */
3331 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x04, 0);
3334 set_spdif_loop(s, 0);
3336 outb(0, s->iobase + CODEC_CMI_INT_HLDCLR + 2); /* disable ints */
3337 synchronize_irq(s->irq);
3338 outb(0, s->iobase + CODEC_CMI_FUNCTRL0 + 2); /* disable channels */
3339 free_irq(s->irq, s);
3342 wrmixer(s, DSP_MIX_DATARESETIDX, 0);
3344 release_region(s->iobase, CM_EXTENT_CODEC);
3345 unregister_sound_dsp(s->dev_audio);
3346 unregister_sound_mixer(s->dev_mixer);
3348 pci_set_drvdata(dev, NULL);
3351 static struct pci_device_id id_table[] __devinitdata = {
3352 { PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738B, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
3353 { PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
3354 { PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
3355 { PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
3359 MODULE_DEVICE_TABLE(pci, id_table);
3361 static struct pci_driver cm_driver = {
3363 .id_table = id_table,
3365 .remove = __devexit_p(cm_remove)
3368 static int __init init_cmpci(void)
3370 printk(KERN_INFO "cmpci: version $Revision: 6.82 $ time " __TIME__ " " __DATE__ "\n");
3371 return pci_register_driver(&cm_driver);
3374 static void __exit cleanup_cmpci(void)
3376 printk(KERN_INFO "cmpci: unloading\n");
3377 pci_unregister_driver(&cm_driver);
3380 module_init(init_cmpci);
3381 module_exit(cleanup_cmpci);