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>
143 /* --------------------------------------------------------------------- */
144 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
147 /* --------------------------------------------------------------------- */
149 #define CM_MAGIC ((PCI_VENDOR_ID_CMEDIA<<16)|PCI_DEVICE_ID_CMEDIA_CM8338A)
151 /* CM8338 registers definition ****************/
153 #define CODEC_CMI_FUNCTRL0 (0x00)
154 #define CODEC_CMI_FUNCTRL1 (0x04)
155 #define CODEC_CMI_CHFORMAT (0x08)
156 #define CODEC_CMI_INT_HLDCLR (0x0C)
157 #define CODEC_CMI_INT_STATUS (0x10)
158 #define CODEC_CMI_LEGACY_CTRL (0x14)
159 #define CODEC_CMI_MISC_CTRL (0x18)
160 #define CODEC_CMI_TDMA_POS (0x1C)
161 #define CODEC_CMI_MIXER (0x20)
162 #define CODEC_SB16_DATA (0x22)
163 #define CODEC_SB16_ADDR (0x23)
164 #define CODEC_CMI_MIXER1 (0x24)
165 #define CODEC_CMI_MIXER2 (0x25)
166 #define CODEC_CMI_AUX_VOL (0x26)
167 #define CODEC_CMI_MISC (0x27)
168 #define CODEC_CMI_AC97 (0x28)
170 #define CODEC_CMI_CH0_FRAME1 (0x80)
171 #define CODEC_CMI_CH0_FRAME2 (0x84)
172 #define CODEC_CMI_CH1_FRAME1 (0x88)
173 #define CODEC_CMI_CH1_FRAME2 (0x8C)
175 #define CODEC_CMI_SPDIF_CTRL (0x90)
176 #define CODEC_CMI_MISC_CTRL2 (0x92)
178 #define CODEC_CMI_EXT_REG (0xF0)
180 /* Mixer registers for SB16 ******************/
182 #define DSP_MIX_DATARESETIDX ((unsigned char)(0x00))
184 #define DSP_MIX_MASTERVOLIDX_L ((unsigned char)(0x30))
185 #define DSP_MIX_MASTERVOLIDX_R ((unsigned char)(0x31))
186 #define DSP_MIX_VOICEVOLIDX_L ((unsigned char)(0x32))
187 #define DSP_MIX_VOICEVOLIDX_R ((unsigned char)(0x33))
188 #define DSP_MIX_FMVOLIDX_L ((unsigned char)(0x34))
189 #define DSP_MIX_FMVOLIDX_R ((unsigned char)(0x35))
190 #define DSP_MIX_CDVOLIDX_L ((unsigned char)(0x36))
191 #define DSP_MIX_CDVOLIDX_R ((unsigned char)(0x37))
192 #define DSP_MIX_LINEVOLIDX_L ((unsigned char)(0x38))
193 #define DSP_MIX_LINEVOLIDX_R ((unsigned char)(0x39))
195 #define DSP_MIX_MICVOLIDX ((unsigned char)(0x3A))
196 #define DSP_MIX_SPKRVOLIDX ((unsigned char)(0x3B))
198 #define DSP_MIX_OUTMIXIDX ((unsigned char)(0x3C))
200 #define DSP_MIX_ADCMIXIDX_L ((unsigned char)(0x3D))
201 #define DSP_MIX_ADCMIXIDX_R ((unsigned char)(0x3E))
203 #define DSP_MIX_INGAINIDX_L ((unsigned char)(0x3F))
204 #define DSP_MIX_INGAINIDX_R ((unsigned char)(0x40))
205 #define DSP_MIX_OUTGAINIDX_L ((unsigned char)(0x41))
206 #define DSP_MIX_OUTGAINIDX_R ((unsigned char)(0x42))
208 #define DSP_MIX_AGCIDX ((unsigned char)(0x43))
210 #define DSP_MIX_TREBLEIDX_L ((unsigned char)(0x44))
211 #define DSP_MIX_TREBLEIDX_R ((unsigned char)(0x45))
212 #define DSP_MIX_BASSIDX_L ((unsigned char)(0x46))
213 #define DSP_MIX_BASSIDX_R ((unsigned char)(0x47))
214 #define DSP_MIX_EXTENSION ((unsigned char)(0xf0))
215 // pseudo register for AUX
216 #define DSP_MIX_AUXVOL_L ((unsigned char)(0x50))
217 #define DSP_MIX_AUXVOL_R ((unsigned char)(0x51))
220 #define CM_EXTENT_CODEC 0x100
221 #define CM_EXTENT_MIDI 0x2
222 #define CM_EXTENT_SYNTH 0x4
223 #define CM_EXTENT_GAME 0x8
225 // Function Control Register 0 (00h)
231 // Function Control Register 0+2 (02h)
237 // Function Control Register 1 (04h)
238 #define JYSTK_EN 0x02
240 #define SPDO2DAC 0x40
241 #define SPDFLOOP 0x80
243 // Function Control Register 1+1 (05h)
248 #define SPDIF2DAC (SPDF_1 << 8 | SPDO2DAC)
250 // Channel Format Register (08h)
251 #define CM_CFMT_STEREO 0x01
252 #define CM_CFMT_16BIT 0x02
253 #define CM_CFMT_MASK 0x03
254 #define POLVALID 0x20
255 #define INVSPDIFI 0x80
257 // Channel Format Register+2 (0ah)
258 #define SPD24SEL 0x20
260 // Channel Format Register+3 (0bh)
264 // Interrupt Hold/Clear Register+2 (0eh)
265 #define CH0_INT_EN 0x01
266 #define CH1_INT_EN 0x02
268 // Interrupt Register (10h)
274 // Legacy Control/Status Register+1 (15h)
276 #define BASE2LIN 0x20
277 #define CENTR2LIN 0x40
278 #define CB2LIN (BASE2LIN | CENTR2LIN)
281 // Legacy Control/Status Register+2 (16h)
282 #define DAC2SPDO 0x20
283 #define SPDCOPYRHT 0x40
284 #define ENSPDOUT 0x80
286 // Legacy Control/Status Register+3 (17h)
292 // Miscellaneous Control Register (18h)
293 #define REAR2LIN 0x20
295 #define ENCENTER 0x80
297 // Miscellaneous Control Register+1 (19h)
298 #define SELSPDIFI2 0x01
299 #define SPDF_AC97 0x80
301 // Miscellaneous Control Register+2 (1ah)
304 #define SPD32SEL 0x20
308 // Miscellaneous Control Register+3 (1bh)
309 #define SPDIFI48K 0x01
314 #define SPDIF48K (SPDIFI48K << 24 | SPDF_AC97 << 8)
319 #define REAR2FRONT 0x10
324 // Miscellaneous Register (27h)
325 #define SPDVALID 0x02
326 #define CENTR2MIC 0x04
328 // Miscellaneous Register2 (92h)
329 #define SPD32KFMT 0x10
331 #define CM_CFMT_DACSHIFT 2
332 #define CM_CFMT_ADCSHIFT 0
333 #define CM_FREQ_DACSHIFT 5
334 #define CM_FREQ_ADCSHIFT 2
335 #define RSTDAC RST_CH1
336 #define RSTADC RST_CH0
339 #define PAUSEDAC PAUSE1
340 #define PAUSEADC PAUSE0
341 #define CODEC_CMI_ADC_FRAME1 CODEC_CMI_CH0_FRAME1
342 #define CODEC_CMI_ADC_FRAME2 CODEC_CMI_CH0_FRAME2
343 #define CODEC_CMI_DAC_FRAME1 CODEC_CMI_CH1_FRAME1
344 #define CODEC_CMI_DAC_FRAME2 CODEC_CMI_CH1_FRAME2
345 #define DACINT CHINT1
346 #define ADCINT CHINT0
347 #define DACBUSY CH1BUSY
348 #define ADCBUSY CH0BUSY
349 #define ENDACINT CH1_INT_EN
350 #define ENADCINT CH0_INT_EN
352 static const unsigned sample_size[] = { 1, 2, 2, 4 };
353 static const unsigned sample_shift[] = { 0, 1, 1, 2 };
355 #define SND_DEV_DSP16 5
357 #define NR_DEVICE 3 /* maximum number of devices */
359 #define set_dac1_rate set_adc_rate
360 #define set_dac1_rate_unlocked set_adc_rate_unlocked
361 #define stop_dac1 stop_adc
362 #define stop_dac1_unlocked stop_adc_unlocked
363 #define get_dmadac1 get_dmaadc
365 static unsigned int devindex = 0;
367 //*********************************************/
373 /* list of cmedia devices */
374 struct list_head devs;
376 /* the corresponding pci_dev structure */
379 int dev_audio; /* soundcore stuff */
382 unsigned int iosb, iobase, iosynth,
383 iomidi, iogame, irq; /* hardware resources */
384 unsigned short deviceid; /* pci_id */
386 struct { /* mixer stuff */
388 unsigned short vol[13];
391 unsigned int rateadc, ratedac; /* wave stuff */
392 unsigned char fmt, enable;
395 struct semaphore open_sem;
397 wait_queue_head_t open_wait;
405 unsigned hwptr, swptr;
406 unsigned total_bytes;
408 unsigned error; /* over/underrun */
409 wait_queue_head_t wait;
411 unsigned fragsize; /* redundant, but makes calculations easier */
413 unsigned fragsamples;
416 unsigned mapped:1; /* OSS stuff */
418 unsigned endcleared:1;
420 unsigned ossfragshift;
422 unsigned subdivision;
425 #ifdef CONFIG_SOUND_CMPCI_MIDI
427 struct address_info mpu_data;
429 #ifdef CONFIG_SOUND_CMPCI_JOYSTICK
430 struct gameport *gameport;
436 int capability; /* HW capability, various for chip versions */
438 int status; /* HW or SW state */
440 int spdif_counter; /* spdif frame counter */
443 /* flags used for capability */
444 #define CAN_AC3_HW 0x00000001 /* 037 or later */
445 #define CAN_AC3_SW 0x00000002 /* 033 or later */
446 #define CAN_AC3 (CAN_AC3_HW | CAN_AC3_SW)
447 #define CAN_DUAL_DAC 0x00000004 /* 033 or later */
448 #define CAN_MULTI_CH_HW 0x00000008 /* 039 or later */
449 #define CAN_MULTI_CH (CAN_MULTI_CH_HW | CAN_DUAL_DAC)
450 #define CAN_LINE_AS_REAR 0x00000010 /* 033 or later */
451 #define CAN_LINE_AS_BASS 0x00000020 /* 039 or later */
452 #define CAN_MIC_AS_BASS 0x00000040 /* 039 or later */
454 /* flags used for status */
455 #define DO_AC3_HW 0x00000001
456 #define DO_AC3_SW 0x00000002
457 #define DO_AC3 (DO_AC3_HW | DO_AC3_SW)
458 #define DO_DUAL_DAC 0x00000004
459 #define DO_MULTI_CH_HW 0x00000008
460 #define DO_MULTI_CH (DO_MULTI_CH_HW | DO_DUAL_DAC)
461 #define DO_LINE_AS_REAR 0x00000010 /* 033 or later */
462 #define DO_LINE_AS_BASS 0x00000020 /* 039 or later */
463 #define DO_MIC_AS_BASS 0x00000040 /* 039 or later */
464 #define DO_SPDIF_OUT 0x00000100
465 #define DO_SPDIF_IN 0x00000200
466 #define DO_SPDIF_LOOP 0x00000400
467 #define DO_BIGENDIAN_W 0x00001000 /* used in PowerPC */
468 #define DO_BIGENDIAN_R 0x00002000 /* used in PowerPC */
470 static LIST_HEAD(devs);
475 static int spdif_inverse;
476 static int spdif_loop;
477 static int spdif_out;
478 static int use_line_as_rear;
479 static int use_line_as_bass;
480 static int use_mic_as_bass;
481 static int mic_boost;
483 module_param(mpuio, int, 0);
484 module_param(fmio, int, 0);
485 module_param(joystick, bool, 0);
486 module_param(spdif_inverse, bool, 0);
487 module_param(spdif_loop, bool, 0);
488 module_param(spdif_out, bool, 0);
489 module_param(use_line_as_rear, bool, 0);
490 module_param(use_line_as_bass, bool, 0);
491 module_param(use_mic_as_bass, bool, 0);
492 module_param(mic_boost, bool, 0);
493 module_param(hw_copy, bool, 0);
494 MODULE_PARM_DESC(mpuio, "(0x330, 0x320, 0x310, 0x300) Base of MPU-401, 0 to disable");
495 MODULE_PARM_DESC(fmio, "(0x388, 0x3C8, 0x3E0) Base of OPL3, 0 to disable");
496 MODULE_PARM_DESC(joystick, "(1/0) Enable joystick interface, still need joystick driver");
497 MODULE_PARM_DESC(spdif_inverse, "(1/0) Invert S/PDIF-in signal");
498 MODULE_PARM_DESC(spdif_loop, "(1/0) Route S/PDIF-in to S/PDIF-out directly");
499 MODULE_PARM_DESC(spdif_out, "(1/0) Send PCM to S/PDIF-out (PCM volume will not function)");
500 MODULE_PARM_DESC(use_line_as_rear, "(1/0) Use line-in jack as rear-out");
501 MODULE_PARM_DESC(use_line_as_bass, "(1/0) Use line-in jack as bass/center");
502 MODULE_PARM_DESC(use_mic_as_bass, "(1/0) Use mic-in jack as bass/center");
503 MODULE_PARM_DESC(mic_boost, "(1/0) Enable microphone boost");
504 MODULE_PARM_DESC(hw_copy, "Copy front channel to surround channel");
506 /* --------------------------------------------------------------------- */
508 static inline unsigned ld2(unsigned int x)
510 unsigned exp=16,l=5,r=0;
511 static const unsigned num[]={0x2,0x4,0x10,0x100,0x10000};
513 /* num: 2, 4, 16, 256, 65536 */
514 /* exp: 1, 2, 4, 8, 16 */
518 if(num[l]>2) x >>= exp;
527 /* --------------------------------------------------------------------- */
529 static void maskb(unsigned int addr, unsigned int mask, unsigned int value)
531 outb((inb(addr) & mask) | value, addr);
534 static void maskw(unsigned int addr, unsigned int mask, unsigned int value)
536 outw((inw(addr) & mask) | value, addr);
539 static void maskl(unsigned int addr, unsigned int mask, unsigned int value)
541 outl((inl(addr) & mask) | value, addr);
544 static void set_dmadac1(struct cm_state *s, unsigned int addr, unsigned int count)
547 outl(addr, s->iobase + CODEC_CMI_ADC_FRAME1);
548 outw(count - 1, s->iobase + CODEC_CMI_ADC_FRAME2);
549 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~CHADC0, 0);
552 static void set_dmaadc(struct cm_state *s, unsigned int addr, unsigned int count)
554 outl(addr, s->iobase + CODEC_CMI_ADC_FRAME1);
555 outw(count - 1, s->iobase + CODEC_CMI_ADC_FRAME2);
556 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~0, CHADC0);
559 static void set_dmadac(struct cm_state *s, unsigned int addr, unsigned int count)
561 outl(addr, s->iobase + CODEC_CMI_DAC_FRAME1);
562 outw(count - 1, s->iobase + CODEC_CMI_DAC_FRAME2);
563 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~CHADC1, 0);
564 if (s->status & DO_DUAL_DAC)
565 set_dmadac1(s, 0, count);
568 static void set_countadc(struct cm_state *s, unsigned count)
570 outw(count - 1, s->iobase + CODEC_CMI_ADC_FRAME2 + 2);
573 static void set_countdac(struct cm_state *s, unsigned count)
575 outw(count - 1, s->iobase + CODEC_CMI_DAC_FRAME2 + 2);
576 if (s->status & DO_DUAL_DAC)
577 set_countadc(s, count);
580 static unsigned get_dmadac(struct cm_state *s)
582 unsigned int curr_addr;
584 curr_addr = inw(s->iobase + CODEC_CMI_DAC_FRAME2) + 1;
585 curr_addr <<= sample_shift[(s->fmt >> CM_CFMT_DACSHIFT) & CM_CFMT_MASK];
586 curr_addr = s->dma_dac.dmasize - curr_addr;
591 static unsigned get_dmaadc(struct cm_state *s)
593 unsigned int curr_addr;
595 curr_addr = inw(s->iobase + CODEC_CMI_ADC_FRAME2) + 1;
596 curr_addr <<= sample_shift[(s->fmt >> CM_CFMT_ADCSHIFT) & CM_CFMT_MASK];
597 curr_addr = s->dma_adc.dmasize - curr_addr;
602 static void wrmixer(struct cm_state *s, unsigned char idx, unsigned char data)
604 unsigned char regval, pseudo;
607 if (idx == DSP_MIX_AUXVOL_L) {
610 regval = inb(s->iobase + CODEC_CMI_AUX_VOL) & ~0x0f;
611 outb(regval | data, s->iobase + CODEC_CMI_AUX_VOL);
614 if (idx == DSP_MIX_AUXVOL_R) {
616 regval = inb(s->iobase + CODEC_CMI_AUX_VOL) & ~0xf0;
617 outb(regval | data, s->iobase + CODEC_CMI_AUX_VOL);
620 outb(idx, s->iobase + CODEC_SB16_ADDR);
623 if (idx == DSP_MIX_OUTMIXIDX) {
624 pseudo = data & ~0x1f;
626 regval = inb(s->iobase + CODEC_CMI_MIXER2) & ~0x30;
627 outb(regval | pseudo, s->iobase + CODEC_CMI_MIXER2);
629 if (idx == DSP_MIX_ADCMIXIDX_L) {
630 pseudo = data & 0x80;
632 regval = inb(s->iobase + CODEC_CMI_MIXER2) & ~0x40;
633 outb(regval | pseudo, s->iobase + CODEC_CMI_MIXER2);
635 if (idx == DSP_MIX_ADCMIXIDX_R) {
636 pseudo = data & 0x80;
637 regval = inb(s->iobase + CODEC_CMI_MIXER2) & ~0x80;
638 outb(regval | pseudo, s->iobase + CODEC_CMI_MIXER2);
640 outb(data, s->iobase + CODEC_SB16_DATA);
644 static unsigned char rdmixer(struct cm_state *s, unsigned char idx)
646 unsigned char v, pseudo;
649 if (idx == DSP_MIX_AUXVOL_L) {
650 v = inb(s->iobase + CODEC_CMI_AUX_VOL) & 0x0f;
654 if (idx == DSP_MIX_AUXVOL_L) {
655 v = inb(s->iobase + CODEC_CMI_AUX_VOL) & 0xf0;
658 outb(idx, s->iobase + CODEC_SB16_ADDR);
660 v = inb(s->iobase + CODEC_SB16_DATA);
663 if (idx == DSP_MIX_OUTMIXIDX) {
664 pseudo = inb(s->iobase + CODEC_CMI_MIXER2) & 0x30;
668 if (idx == DSP_MIX_ADCMIXIDX_L) {
669 pseudo = inb(s->iobase + CODEC_CMI_MIXER2) & 0x40;
673 if (idx == DSP_MIX_ADCMIXIDX_R) {
674 pseudo = inb(s->iobase + CODEC_CMI_MIXER2) & 0x80;
680 static void set_fmt_unlocked(struct cm_state *s, unsigned char mask, unsigned char data)
682 if (mask && s->chip_version > 0) { /* 8338 cannot keep this */
683 s->fmt = inb(s->iobase + CODEC_CMI_CHFORMAT);
686 s->fmt = (s->fmt & mask) | data;
687 outb(s->fmt, s->iobase + CODEC_CMI_CHFORMAT);
691 static void set_fmt(struct cm_state *s, unsigned char mask, unsigned char data)
695 spin_lock_irqsave(&s->lock, flags);
696 set_fmt_unlocked(s,mask,data);
697 spin_unlock_irqrestore(&s->lock, flags);
700 static void frobindir(struct cm_state *s, unsigned char idx, unsigned char mask, unsigned char data)
702 outb(idx, s->iobase + CODEC_SB16_ADDR);
704 outb((inb(s->iobase + CODEC_SB16_DATA) & mask) | data, s->iobase + CODEC_SB16_DATA);
715 { 5512, (0 + 5512) / 2, (5512 + 8000) / 2, 0 },
716 { 8000, (5512 + 8000) / 2, (8000 + 11025) / 2, 4 },
717 { 11025, (8000 + 11025) / 2, (11025 + 16000) / 2, 1 },
718 { 16000, (11025 + 16000) / 2, (16000 + 22050) / 2, 5 },
719 { 22050, (16000 + 22050) / 2, (22050 + 32000) / 2, 2 },
720 { 32000, (22050 + 32000) / 2, (32000 + 44100) / 2, 6 },
721 { 44100, (32000 + 44100) / 2, (44100 + 48000) / 2, 3 },
722 { 48000, (44100 + 48000) / 2, 48000, 7 }
725 static void set_spdif_copyright(struct cm_state *s, int spdif_copyright)
727 /* enable SPDIF-in Copyright */
728 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 2, ~SPDCOPYRHT, spdif_copyright ? SPDCOPYRHT : 0);
731 static void set_spdif_loop(struct cm_state *s, int spdif_loop)
733 /* enable SPDIF loop */
735 s->status |= DO_SPDIF_LOOP;
736 /* turn on spdif-in to spdif-out */
737 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, SPDFLOOP);
739 s->status &= ~DO_SPDIF_LOOP;
740 /* turn off spdif-in to spdif-out */
741 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~SPDFLOOP, 0);
745 static void set_spdif_monitor(struct cm_state *s, int channel)
748 maskw(s->iobase + CODEC_CMI_FUNCTRL1, ~SPDO2DAC, channel == 2 ? SPDO2DAC : 0);
750 if (s->chip_version >= 39)
751 maskb(s->iobase + CODEC_CMI_MIXER1, ~CDPLAY, channel ? CDPLAY : 0);
754 static void set_spdifout_level(struct cm_state *s, int level5v)
757 if (s->chip_version > 0)
758 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 3, ~SPDO5V, level5v ? SPDO5V : 0);
761 static void set_spdifin_inverse(struct cm_state *s, int spdif_inverse)
763 if (s->chip_version == 0) /* 8338 has not this feature */
766 /* turn on spdif-in inverse */
767 if (s->chip_version >= 39)
768 maskb(s->iobase + CODEC_CMI_CHFORMAT, ~0, INVSPDIFI);
770 maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~0, 1);
772 /* turn off spdif-ininverse */
773 if (s->chip_version >= 39)
774 maskb(s->iobase + CODEC_CMI_CHFORMAT, ~INVSPDIFI, 0);
776 maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~1, 0);
780 static void set_spdifin_channel2(struct cm_state *s, int channel2)
783 if (s->chip_version >= 39)
784 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 1, ~SELSPDIFI2, channel2 ? SELSPDIFI2 : 0);
787 static void set_spdifin_valid(struct cm_state *s, int valid)
790 maskb(s->iobase + CODEC_CMI_MISC, ~SPDVALID, valid ? SPDVALID : 0);
793 static void set_spdifout_unlocked(struct cm_state *s, unsigned rate)
795 if (rate != 48000 && rate != 44100)
797 if (rate == 48000 || rate == 44100) {
798 set_spdif_loop(s, 0);
800 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~0, SPDF_1);
801 // SPDIFI48K SPDF_AC97
802 maskl(s->iobase + CODEC_CMI_MISC_CTRL, ~SPDIF48K, rate == 48000 ? SPDIF48K : 0);
803 if (s->chip_version >= 55)
805 maskb(s->iobase + CODEC_CMI_MISC_CTRL2, ~SPD32KFMT, rate == 48000 ? SPD32KFMT : 0);
806 if (s->chip_version > 0)
808 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 2, ~0, ENSPDOUT);
810 set_spdif_monitor(s, 2);
811 s->status |= DO_SPDIF_OUT;
813 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~SPDF_1, 0);
814 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 2, ~ENSPDOUT, 0);
816 set_spdif_monitor(s, 0);
817 s->status &= ~DO_SPDIF_OUT;
821 static void set_spdifout(struct cm_state *s, unsigned rate)
825 spin_lock_irqsave(&s->lock, flags);
826 set_spdifout_unlocked(s,rate);
827 spin_unlock_irqrestore(&s->lock, flags);
830 static void set_spdifin_unlocked(struct cm_state *s, unsigned rate)
832 if (rate == 48000 || rate == 44100) {
834 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~0, SPDF_1);
835 // SPDIFI48K SPDF_AC97
836 maskl(s->iobase + CODEC_CMI_MISC_CTRL, ~SPDIF48K, rate == 48000 ? SPDIF48K : 0);
837 s->status |= DO_SPDIF_IN;
839 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~SPDF_1, 0);
840 s->status &= ~DO_SPDIF_IN;
844 static void set_spdifin(struct cm_state *s, unsigned rate)
848 spin_lock_irqsave(&s->lock, flags);
849 set_spdifin_unlocked(s,rate);
850 spin_unlock_irqrestore(&s->lock, flags);
853 /* find parity for bit 4~30 */
854 static unsigned parity(unsigned data)
859 data >>= 4; // start from bit 4
860 while (counter <= 30) {
869 static void set_ac3_unlocked(struct cm_state *s, unsigned rate)
871 if (!(s->capability & CAN_AC3))
874 if (rate && rate != 44100)
876 if (rate == 48000 || rate == 44100) {
878 maskb(s->iobase + CODEC_CMI_MIXER1, ~0, WSMUTE);
879 if (s->chip_version >= 39)
880 maskb(s->iobase + CODEC_CMI_MISC_CTRL, ~0, MUTECH1);
881 // AC3EN for 039, 0x04
882 if (s->chip_version >= 39) {
883 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0, AC3_EN);
884 if (s->chip_version == 55)
885 maskb(s->iobase + CODEC_CMI_SPDIF_CTRL, ~2, 0);
886 // AC3EN for 037, 0x10
887 } else if (s->chip_version == 37)
888 maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~0, 0x10);
889 if (s->capability & CAN_AC3_HW) {
890 // SPD24SEL for 039, 0x20, but cannot be set
891 if (s->chip_version == 39)
892 maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~0, SPD24SEL);
893 // SPD24SEL for 037, 0x02
894 else if (s->chip_version == 37)
895 maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~0, 0x02);
896 if (s->chip_version >= 39)
897 maskb(s->iobase + CODEC_CMI_MIXER1, ~CDPLAY, 0);
899 s->status |= DO_AC3_HW;
901 // SPD32SEL for 037 & 039
902 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0, SPD32SEL);
903 // set 176K sample rate to fix 033 HW bug
904 if (s->chip_version == 33) {
906 maskb(s->iobase + CODEC_CMI_CHFORMAT + 1, ~0, 0x08);
908 maskb(s->iobase + CODEC_CMI_CHFORMAT + 1, ~0x08, 0);
910 s->status |= DO_AC3_SW;
913 maskb(s->iobase + CODEC_CMI_MIXER1, ~WSMUTE, 0);
914 if (s->chip_version >= 39)
915 maskb(s->iobase + CODEC_CMI_MISC_CTRL, ~MUTECH1, 0);
916 maskb(s->iobase + CODEC_CMI_CHFORMAT + 2, ~(SPD24SEL|0x12), 0);
917 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~(SPD32SEL|AC3_EN), 0);
918 if (s->chip_version == 33)
919 maskb(s->iobase + CODEC_CMI_CHFORMAT + 1, ~0x08, 0);
920 if (s->chip_version >= 39)
921 maskb(s->iobase + CODEC_CMI_MIXER1, ~0, CDPLAY);
922 s->status &= ~DO_AC3;
924 s->spdif_counter = 0;
927 static void set_line_as_rear(struct cm_state *s, int use_line_as_rear)
929 if (!(s->capability & CAN_LINE_AS_REAR))
931 if (use_line_as_rear) {
932 maskb(s->iobase + CODEC_CMI_MIXER1, ~0, SPK4);
933 s->status |= DO_LINE_AS_REAR;
935 maskb(s->iobase + CODEC_CMI_MIXER1, ~SPK4, 0);
936 s->status &= ~DO_LINE_AS_REAR;
940 static void set_line_as_bass(struct cm_state *s, int use_line_as_bass)
942 if (!(s->capability & CAN_LINE_AS_BASS))
944 if (use_line_as_bass) {
945 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 1, ~0, CB2LIN);
946 s->status |= DO_LINE_AS_BASS;
948 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 1, ~CB2LIN, 0);
949 s->status &= ~DO_LINE_AS_BASS;
953 static void set_mic_as_bass(struct cm_state *s, int use_mic_as_bass)
955 if (!(s->capability & CAN_MIC_AS_BASS))
957 if (use_mic_as_bass) {
958 maskb(s->iobase + CODEC_CMI_MISC, ~0, 0x04);
959 s->status |= DO_MIC_AS_BASS;
961 maskb(s->iobase + CODEC_CMI_MISC, ~0x04, 0);
962 s->status &= ~DO_MIC_AS_BASS;
966 static void set_hw_copy(struct cm_state *s, int hw_copy)
968 if (s->max_channels > 2 && hw_copy)
969 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 3, ~0, N4SPK3D);
971 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 3, ~N4SPK3D, 0);
974 static void set_ac3(struct cm_state *s, unsigned rate)
978 spin_lock_irqsave(&s->lock, flags);
979 set_spdifout_unlocked(s, rate);
980 set_ac3_unlocked(s, rate);
981 spin_unlock_irqrestore(&s->lock, flags);
984 static int trans_ac3(struct cm_state *s, void *dest, const char __user *source, int size)
988 unsigned short data16;
989 unsigned long *dst = (unsigned long *) dest;
990 unsigned short __user *src = (unsigned short __user *)source;
994 if ((err = __get_user(data16, src++)))
996 data = (unsigned long)le16_to_cpu(data16);
997 data <<= 12; // ok for 16-bit data
998 if (s->spdif_counter == 2 || s->spdif_counter == 3)
999 data |= 0x40000000; // indicate AC-3 raw data
1001 data |= 0x80000000; // parity
1002 if (s->spdif_counter == 0)
1003 data |= 3; // preamble 'M'
1004 else if (s->spdif_counter & 1)
1005 data |= 5; // odd, 'W'
1007 data |= 9; // even, 'M'
1008 *dst++ = cpu_to_le32(data);
1010 if (s->spdif_counter == 384)
1011 s->spdif_counter = 0;
1017 static void set_adc_rate_unlocked(struct cm_state *s, unsigned rate)
1019 unsigned char freq = 4;
1026 for (i = 0; i < sizeof(rate_lookup) / sizeof(rate_lookup[0]); i++) {
1027 if (rate > rate_lookup[i].lower && rate <= rate_lookup[i].upper) {
1028 rate = rate_lookup[i].rate;
1029 freq = rate_lookup[i].freq;
1034 freq <<= CM_FREQ_ADCSHIFT;
1036 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~ASFC, freq);
1039 static void set_adc_rate(struct cm_state *s, unsigned rate)
1041 unsigned long flags;
1042 unsigned char freq = 4;
1049 for (i = 0; i < sizeof(rate_lookup) / sizeof(rate_lookup[0]); i++) {
1050 if (rate > rate_lookup[i].lower && rate <= rate_lookup[i].upper) {
1051 rate = rate_lookup[i].rate;
1052 freq = rate_lookup[i].freq;
1057 freq <<= CM_FREQ_ADCSHIFT;
1059 spin_lock_irqsave(&s->lock, flags);
1060 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~ASFC, freq);
1061 spin_unlock_irqrestore(&s->lock, flags);
1064 static void set_dac_rate(struct cm_state *s, unsigned rate)
1066 unsigned long flags;
1067 unsigned char freq = 4;
1074 for (i = 0; i < sizeof(rate_lookup) / sizeof(rate_lookup[0]); i++) {
1075 if (rate > rate_lookup[i].lower && rate <= rate_lookup[i].upper) {
1076 rate = rate_lookup[i].rate;
1077 freq = rate_lookup[i].freq;
1082 freq <<= CM_FREQ_DACSHIFT;
1084 spin_lock_irqsave(&s->lock, flags);
1085 maskb(s->iobase + CODEC_CMI_FUNCTRL1 + 1, ~DSFC, freq);
1086 spin_unlock_irqrestore(&s->lock, flags);
1088 if (s->curr_channels <= 2 && spdif_out)
1089 set_spdifout(s, rate);
1090 if (s->status & DO_DUAL_DAC)
1091 set_dac1_rate(s, rate);
1094 /* --------------------------------------------------------------------- */
1095 static inline void reset_adc(struct cm_state *s)
1097 /* reset bus master */
1098 outb(s->enable | RSTADC, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1100 outb(s->enable & ~RSTADC, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1103 static inline void reset_dac(struct cm_state *s)
1105 /* reset bus master */
1106 outb(s->enable | RSTDAC, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1108 outb(s->enable & ~RSTDAC, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1109 if (s->status & DO_DUAL_DAC)
1113 static inline void pause_adc(struct cm_state *s)
1115 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~0, PAUSEADC);
1118 static inline void pause_dac(struct cm_state *s)
1120 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~0, PAUSEDAC);
1121 if (s->status & DO_DUAL_DAC)
1125 static inline void disable_adc(struct cm_state *s)
1127 /* disable channel */
1128 s->enable &= ~ENADC;
1129 outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1133 static inline void disable_dac(struct cm_state *s)
1135 /* disable channel */
1136 s->enable &= ~ENDAC;
1137 outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1139 if (s->status & DO_DUAL_DAC)
1143 static inline void enable_adc(struct cm_state *s)
1145 if (!(s->enable & ENADC)) {
1146 /* enable channel */
1148 outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1150 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~PAUSEADC, 0);
1153 static inline void enable_dac_unlocked(struct cm_state *s)
1155 if (!(s->enable & ENDAC)) {
1156 /* enable channel */
1158 outb(s->enable, s->iobase + CODEC_CMI_FUNCTRL0 + 2);
1160 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~PAUSEDAC, 0);
1162 if (s->status & DO_DUAL_DAC)
1166 static inline void stop_adc_unlocked(struct cm_state *s)
1168 if (s->enable & ENADC) {
1169 /* disable interrupt */
1170 maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~ENADCINT, 0);
1175 static inline void stop_adc(struct cm_state *s)
1177 unsigned long flags;
1179 spin_lock_irqsave(&s->lock, flags);
1180 stop_adc_unlocked(s);
1181 spin_unlock_irqrestore(&s->lock, flags);
1185 static inline void stop_dac_unlocked(struct cm_state *s)
1187 if (s->enable & ENDAC) {
1188 /* disable interrupt */
1189 maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~ENDACINT, 0);
1192 if (s->status & DO_DUAL_DAC)
1193 stop_dac1_unlocked(s);
1196 static inline void stop_dac(struct cm_state *s)
1198 unsigned long flags;
1200 spin_lock_irqsave(&s->lock, flags);
1201 stop_dac_unlocked(s);
1202 spin_unlock_irqrestore(&s->lock, flags);
1205 static inline void start_adc_unlocked(struct cm_state *s)
1207 if ((s->dma_adc.mapped || s->dma_adc.count < (signed)(s->dma_adc.dmasize - 2*s->dma_adc.fragsize))
1208 && s->dma_adc.ready) {
1209 /* enable interrupt */
1210 maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~0, ENADCINT);
1215 static void start_adc(struct cm_state *s)
1217 unsigned long flags;
1219 spin_lock_irqsave(&s->lock, flags);
1220 start_adc_unlocked(s);
1221 spin_unlock_irqrestore(&s->lock, flags);
1224 static void start_dac1_unlocked(struct cm_state *s)
1226 if ((s->dma_adc.mapped || s->dma_adc.count > 0) && s->dma_adc.ready) {
1227 /* enable interrupt */
1228 maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~0, ENADCINT);
1229 enable_dac_unlocked(s);
1233 static void start_dac_unlocked(struct cm_state *s)
1235 if ((s->dma_dac.mapped || s->dma_dac.count > 0) && s->dma_dac.ready) {
1236 /* enable interrupt */
1237 maskb(s->iobase + CODEC_CMI_INT_HLDCLR + 2, ~0, ENDACINT);
1238 enable_dac_unlocked(s);
1240 if (s->status & DO_DUAL_DAC)
1241 start_dac1_unlocked(s);
1244 static void start_dac(struct cm_state *s)
1246 unsigned long flags;
1248 spin_lock_irqsave(&s->lock, flags);
1249 start_dac_unlocked(s);
1250 spin_unlock_irqrestore(&s->lock, flags);
1253 static int prog_dmabuf(struct cm_state *s, unsigned rec);
1255 static int set_dac_channels(struct cm_state *s, int channels)
1257 unsigned long flags;
1258 static unsigned int fmmute = 0;
1260 spin_lock_irqsave(&s->lock, flags);
1262 if ((channels > 2) && (channels <= s->max_channels)
1263 && (((s->fmt >> CM_CFMT_DACSHIFT) & CM_CFMT_MASK) == (CM_CFMT_STEREO | CM_CFMT_16BIT))) {
1264 set_spdifout_unlocked(s, 0);
1265 if (s->capability & CAN_MULTI_CH_HW) {
1267 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3, ~0, NXCHG);
1269 maskb(s->iobase + CODEC_CMI_CHFORMAT + 3, ~(CHB3D5C|CHB3D), channels > 4 ? CHB3D5C : CHB3D);
1271 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 1, ~CHB3D6C, channels == 6 ? CHB3D6C : 0);
1273 maskb(s->iobase + CODEC_CMI_MISC_CTRL, ~ENCENTER, channels == 6 ? ENCENTER : 0);
1274 s->status |= DO_MULTI_CH_HW;
1275 } else if (s->capability & CAN_DUAL_DAC) {
1276 unsigned char fmtm = ~0, fmts = 0;
1279 // ENDBDAC, turn on double DAC mode
1280 // XCHGDAC, CH0 -> back, CH1->front
1281 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0, ENDBDAC|XCHGDAC);
1283 fmmute = inb(s->iobase + CODEC_CMI_MIXER1) & FMMUTE;
1284 maskb(s->iobase + CODEC_CMI_MIXER1, ~0, FMMUTE);
1285 s->status |= DO_DUAL_DAC;
1286 // prepare secondary buffer
1287 spin_unlock_irqrestore(&s->lock, flags);
1288 ret = prog_dmabuf(s, 1);
1289 if (ret) return ret;
1290 spin_lock_irqsave(&s->lock, flags);
1292 // copy the hw state
1293 fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_DACSHIFT);
1294 fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_ADCSHIFT);
1295 // the HW only support 16-bit stereo
1296 fmts |= CM_CFMT_16BIT << CM_CFMT_DACSHIFT;
1297 fmts |= CM_CFMT_16BIT << CM_CFMT_ADCSHIFT;
1298 fmts |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
1299 fmts |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
1301 set_fmt_unlocked(s, fmtm, fmts);
1302 set_adc_rate_unlocked(s, s->ratedac);
1304 // disable 4 speaker mode (analog duplicate)
1306 s->curr_channels = channels;
1308 // enable jack redirect
1309 set_line_as_rear(s, use_line_as_rear);
1311 set_line_as_bass(s, use_line_as_bass);
1312 set_mic_as_bass(s, use_mic_as_bass);
1315 if (s->status & DO_MULTI_CH_HW) {
1316 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3, ~NXCHG, 0);
1317 maskb(s->iobase + CODEC_CMI_CHFORMAT + 3, ~(CHB3D5C|CHB3D), 0);
1318 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 1, ~CHB3D6C, 0);
1319 } else if (s->status & DO_DUAL_DAC) {
1320 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~ENDBDAC, 0);
1321 maskb(s->iobase + CODEC_CMI_MIXER1, ~FMMUTE, fmmute);
1323 // enable 4 speaker mode (analog duplicate)
1324 set_hw_copy(s, hw_copy);
1325 s->status &= ~DO_MULTI_CH;
1326 s->curr_channels = s->fmt & (CM_CFMT_STEREO << CM_CFMT_DACSHIFT) ? 2 : 1;
1327 // disable jack redirect
1328 set_line_as_rear(s, hw_copy ? use_line_as_rear : 0);
1329 set_line_as_bass(s, 0);
1330 set_mic_as_bass(s, 0);
1332 spin_unlock_irqrestore(&s->lock, flags);
1333 return s->curr_channels;
1336 /* --------------------------------------------------------------------- */
1338 #define DMABUF_DEFAULTORDER (16-PAGE_SHIFT)
1339 #define DMABUF_MINORDER 1
1341 static void dealloc_dmabuf(struct cm_state *s, struct dmabuf *db)
1343 struct page *pstart, *pend;
1346 /* undo marking the pages as reserved */
1347 pend = virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
1348 for (pstart = virt_to_page(db->rawbuf); pstart <= pend; pstart++)
1349 ClearPageReserved(pstart);
1350 pci_free_consistent(s->dev, PAGE_SIZE << db->buforder, db->rawbuf, db->dmaaddr);
1353 db->mapped = db->ready = 0;
1356 /* Ch1 is used for playback, Ch0 is used for recording */
1358 static int prog_dmabuf(struct cm_state *s, unsigned rec)
1360 struct dmabuf *db = rec ? &s->dma_adc : &s->dma_dac;
1361 unsigned rate = rec ? s->rateadc : s->ratedac;
1363 unsigned bytepersec;
1365 struct page *pstart, *pend;
1367 unsigned long flags;
1372 fmt >>= CM_CFMT_ADCSHIFT;
1375 fmt >>= CM_CFMT_DACSHIFT;
1378 fmt &= CM_CFMT_MASK;
1379 db->hwptr = db->swptr = db->total_bytes = db->count = db->error = db->endcleared = 0;
1381 db->ready = db->mapped = 0;
1382 for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--)
1383 if ((db->rawbuf = pci_alloc_consistent(s->dev, PAGE_SIZE << order, &db->dmaaddr)))
1385 if (!db->rawbuf || !db->dmaaddr)
1387 db->buforder = order;
1388 /* now mark the pages as reserved; otherwise remap_pfn_range doesn't do what we want */
1389 pend = virt_to_page(db->rawbuf + (PAGE_SIZE << db->buforder) - 1);
1390 for (pstart = virt_to_page(db->rawbuf); pstart <= pend; pstart++)
1391 SetPageReserved(pstart);
1393 bytepersec = rate << sample_shift[fmt];
1394 bufs = PAGE_SIZE << db->buforder;
1395 if (db->ossfragshift) {
1396 if ((1000 << db->ossfragshift) < bytepersec)
1397 db->fragshift = ld2(bytepersec/1000);
1399 db->fragshift = db->ossfragshift;
1401 db->fragshift = ld2(bytepersec/100/(db->subdivision ? db->subdivision : 1));
1402 if (db->fragshift < 3)
1405 db->numfrag = bufs >> db->fragshift;
1406 while (db->numfrag < 4 && db->fragshift > 3) {
1408 db->numfrag = bufs >> db->fragshift;
1410 db->fragsize = 1 << db->fragshift;
1411 if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
1412 db->numfrag = db->ossmaxfrags;
1413 /* to make fragsize >= 4096 */
1414 db->fragsamples = db->fragsize >> sample_shift[fmt];
1415 db->dmasize = db->numfrag << db->fragshift;
1416 db->dmasamples = db->dmasize >> sample_shift[fmt];
1417 memset(db->rawbuf, (fmt & CM_CFMT_16BIT) ? 0 : 0x80, db->dmasize);
1418 spin_lock_irqsave(&s->lock, flags);
1420 if (s->status & DO_DUAL_DAC)
1421 set_dmadac1(s, db->dmaaddr, db->dmasize >> sample_shift[fmt]);
1423 set_dmaadc(s, db->dmaaddr, db->dmasize >> sample_shift[fmt]);
1424 /* program sample counts */
1425 set_countdac(s, db->fragsamples);
1427 set_dmadac(s, db->dmaaddr, db->dmasize >> sample_shift[fmt]);
1428 /* program sample counts */
1429 set_countdac(s, db->fragsamples);
1431 spin_unlock_irqrestore(&s->lock, flags);
1437 static inline void clear_advance(struct cm_state *s)
1439 unsigned char c = (s->fmt & (CM_CFMT_16BIT << CM_CFMT_DACSHIFT)) ? 0 : 0x80;
1440 unsigned char *buf = s->dma_dac.rawbuf;
1441 unsigned char *buf1 = s->dma_adc.rawbuf;
1442 unsigned bsize = s->dma_dac.dmasize;
1443 unsigned bptr = s->dma_dac.swptr;
1444 unsigned len = s->dma_dac.fragsize;
1446 if (bptr + len > bsize) {
1447 unsigned x = bsize - bptr;
1448 memset(buf + bptr, c, x);
1449 if (s->status & DO_DUAL_DAC)
1450 memset(buf1 + bptr, c, x);
1454 memset(buf + bptr, c, len);
1455 if (s->status & DO_DUAL_DAC)
1456 memset(buf1 + bptr, c, len);
1459 /* call with spinlock held! */
1460 static void cm_update_ptr(struct cm_state *s)
1465 /* update ADC pointer */
1466 if (s->dma_adc.ready) {
1467 if (s->status & DO_DUAL_DAC) {
1468 /* the dac part will finish for this */
1470 hwptr = get_dmaadc(s) % s->dma_adc.dmasize;
1471 diff = (s->dma_adc.dmasize + hwptr - s->dma_adc.hwptr) % s->dma_adc.dmasize;
1472 s->dma_adc.hwptr = hwptr;
1473 s->dma_adc.total_bytes += diff;
1474 s->dma_adc.count += diff;
1475 if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
1476 wake_up(&s->dma_adc.wait);
1477 if (!s->dma_adc.mapped) {
1478 if (s->dma_adc.count > (signed)(s->dma_adc.dmasize - ((3 * s->dma_adc.fragsize) >> 1))) {
1485 /* update DAC pointer */
1486 if (s->dma_dac.ready) {
1487 hwptr = get_dmadac(s) % s->dma_dac.dmasize;
1488 diff = (s->dma_dac.dmasize + hwptr - s->dma_dac.hwptr) % s->dma_dac.dmasize;
1489 s->dma_dac.hwptr = hwptr;
1490 s->dma_dac.total_bytes += diff;
1491 if (s->status & DO_DUAL_DAC) {
1492 s->dma_adc.hwptr = hwptr;
1493 s->dma_adc.total_bytes += diff;
1495 if (s->dma_dac.mapped) {
1496 s->dma_dac.count += diff;
1497 if (s->status & DO_DUAL_DAC)
1498 s->dma_adc.count += diff;
1499 if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
1500 wake_up(&s->dma_dac.wait);
1502 s->dma_dac.count -= diff;
1503 if (s->status & DO_DUAL_DAC)
1504 s->dma_adc.count -= diff;
1505 if (s->dma_dac.count <= 0) {
1508 } else if (s->dma_dac.count <= (signed)s->dma_dac.fragsize && !s->dma_dac.endcleared) {
1510 s->dma_dac.endcleared = 1;
1511 if (s->status & DO_DUAL_DAC)
1512 s->dma_adc.endcleared = 1;
1514 if (s->dma_dac.count + (signed)s->dma_dac.fragsize <= (signed)s->dma_dac.dmasize)
1515 wake_up(&s->dma_dac.wait);
1520 static irqreturn_t cm_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1522 struct cm_state *s = (struct cm_state *)dev_id;
1523 unsigned int intsrc, intstat;
1524 unsigned char mask = 0;
1526 /* fastpath out, to ease interrupt sharing */
1527 intsrc = inl(s->iobase + CODEC_CMI_INT_STATUS);
1528 if (!(intsrc & 0x80000000))
1530 spin_lock(&s->lock);
1531 intstat = inb(s->iobase + CODEC_CMI_INT_HLDCLR + 2);
1532 /* acknowledge interrupt */
1533 if (intsrc & ADCINT)
1535 if (intsrc & DACINT)
1537 outb(intstat & ~mask, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
1538 outb(intstat | mask, s->iobase + CODEC_CMI_INT_HLDCLR + 2);
1540 spin_unlock(&s->lock);
1541 #ifdef CONFIG_SOUND_CMPCI_MIDI
1542 if (intsrc & 0x00010000) { // UART interrupt
1543 if (s->midi_devc && intchk_mpu401((void *)s->midi_devc))
1544 mpuintr(irq, (void *)s->midi_devc, regs);
1546 inb(s->iomidi);// dummy read
1552 /* --------------------------------------------------------------------- */
1554 static const char invalid_magic[] = KERN_CRIT "cmpci: invalid magic value\n";
1556 #define VALIDATE_STATE(s) \
1558 if (!(s) || (s)->magic != CM_MAGIC) { \
1559 printk(invalid_magic); \
1564 /* --------------------------------------------------------------------- */
1568 #define MT_4MUTEMONO 3
1570 #define MT_5MUTEMONO 5
1572 static const struct {
1578 } mixtable[SOUND_MIXER_NRDEVICES] = {
1579 [SOUND_MIXER_CD] = { DSP_MIX_CDVOLIDX_L, DSP_MIX_CDVOLIDX_R, MT_5MUTE, 0x04, 0x06 },
1580 [SOUND_MIXER_LINE] = { DSP_MIX_LINEVOLIDX_L, DSP_MIX_LINEVOLIDX_R, MT_5MUTE, 0x10, 0x18 },
1581 [SOUND_MIXER_MIC] = { DSP_MIX_MICVOLIDX, DSP_MIX_MICVOLIDX, MT_5MUTEMONO, 0x01, 0x01 },
1582 [SOUND_MIXER_SYNTH] = { DSP_MIX_FMVOLIDX_L, DSP_MIX_FMVOLIDX_R, MT_5MUTE, 0x40, 0x00 },
1583 [SOUND_MIXER_VOLUME] = { DSP_MIX_MASTERVOLIDX_L, DSP_MIX_MASTERVOLIDX_R, MT_5MUTE, 0x00, 0x00 },
1584 [SOUND_MIXER_PCM] = { DSP_MIX_VOICEVOLIDX_L, DSP_MIX_VOICEVOLIDX_R, MT_5MUTE, 0x00, 0x00 },
1585 [SOUND_MIXER_LINE1] = { DSP_MIX_AUXVOL_L, DSP_MIX_AUXVOL_R, MT_5MUTE, 0x80, 0x60 },
1586 [SOUND_MIXER_SPEAKER]= { DSP_MIX_SPKRVOLIDX, DSP_MIX_SPKRVOLIDX, MT_5MUTEMONO, 0x00, 0x01 }
1589 static const unsigned char volidx[SOUND_MIXER_NRDEVICES] =
1591 [SOUND_MIXER_CD] = 1,
1592 [SOUND_MIXER_LINE] = 2,
1593 [SOUND_MIXER_MIC] = 3,
1594 [SOUND_MIXER_SYNTH] = 4,
1595 [SOUND_MIXER_VOLUME] = 5,
1596 [SOUND_MIXER_PCM] = 6,
1597 [SOUND_MIXER_LINE1] = 7,
1598 [SOUND_MIXER_SPEAKER]= 8
1601 static unsigned mixer_outmask(struct cm_state *s)
1603 unsigned long flags;
1606 spin_lock_irqsave(&s->lock, flags);
1607 j = rdmixer(s, DSP_MIX_OUTMIXIDX);
1608 spin_unlock_irqrestore(&s->lock, flags);
1609 for (k = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1610 if (j & mixtable[i].play)
1615 static unsigned mixer_recmask(struct cm_state *s)
1617 unsigned long flags;
1620 spin_lock_irqsave(&s->lock, flags);
1621 j = rdmixer(s, DSP_MIX_ADCMIXIDX_L);
1622 spin_unlock_irqrestore(&s->lock, flags);
1623 for (k = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1624 if (j & mixtable[i].rec)
1629 static int mixer_ioctl(struct cm_state *s, unsigned int cmd, unsigned long arg)
1631 unsigned long flags;
1633 unsigned char l, r, rl, rr;
1634 void __user *argp = (void __user *)arg;
1635 int __user *p = argp;
1638 if (cmd == SOUND_MIXER_INFO) {
1640 memset(&info, 0, sizeof(info));
1641 strlcpy(info.id, "cmpci", sizeof(info.id));
1642 strlcpy(info.name, "C-Media PCI", sizeof(info.name));
1643 info.modify_counter = s->mix.modcnt;
1644 if (copy_to_user(argp, &info, sizeof(info)))
1648 if (cmd == SOUND_OLD_MIXER_INFO) {
1649 _old_mixer_info info;
1650 memset(&info, 0, sizeof(info));
1651 strlcpy(info.id, "cmpci", sizeof(info.id));
1652 strlcpy(info.name, "C-Media cmpci", sizeof(info.name));
1653 if (copy_to_user(argp, &info, sizeof(info)))
1657 if (cmd == OSS_GETVERSION)
1658 return put_user(SOUND_VERSION, p);
1659 if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
1661 if (_SIOC_DIR(cmd) == _SIOC_READ) {
1662 switch (_IOC_NR(cmd)) {
1663 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
1664 val = mixer_recmask(s);
1665 return put_user(val, p);
1667 case SOUND_MIXER_OUTSRC: /* Arg contains a bit for each recording source */
1668 val = mixer_outmask(s);
1669 return put_user(val, p);
1671 case SOUND_MIXER_DEVMASK: /* Arg contains a bit for each supported device */
1672 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1673 if (mixtable[i].type)
1675 return put_user(val, p);
1677 case SOUND_MIXER_RECMASK: /* Arg contains a bit for each supported recording source */
1678 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1679 if (mixtable[i].rec)
1681 return put_user(val, p);
1683 case SOUND_MIXER_OUTMASK: /* Arg contains a bit for each supported recording source */
1684 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1685 if (mixtable[i].play)
1687 return put_user(val, p);
1689 case SOUND_MIXER_STEREODEVS: /* Mixer channels supporting stereo */
1690 for (val = i = 0; i < SOUND_MIXER_NRDEVICES; i++)
1691 if (mixtable[i].type && mixtable[i].type != MT_4MUTEMONO)
1693 return put_user(val, p);
1695 case SOUND_MIXER_CAPS:
1696 return put_user(0, p);
1700 if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
1704 return put_user(s->mix.vol[volidx[i]-1], p);
1707 if (_SIOC_DIR(cmd) != (_SIOC_READ|_SIOC_WRITE))
1710 switch (_IOC_NR(cmd)) {
1711 case SOUND_MIXER_RECSRC: /* Arg contains a bit for each recording source */
1712 if (get_user(val, p))
1714 i = generic_hweight32(val);
1715 for (j = i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1716 if (!(val & (1 << i)))
1718 if (!mixtable[i].rec) {
1722 j |= mixtable[i].rec;
1724 spin_lock_irqsave(&s->lock, flags);
1725 wrmixer(s, DSP_MIX_ADCMIXIDX_L, j);
1726 wrmixer(s, DSP_MIX_ADCMIXIDX_R, (j & 1) | (j>>1) | (j & 0x80));
1727 spin_unlock_irqrestore(&s->lock, flags);
1730 case SOUND_MIXER_OUTSRC: /* Arg contains a bit for each recording source */
1731 if (get_user(val, p))
1733 for (j = i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
1734 if (!(val & (1 << i)))
1736 if (!mixtable[i].play) {
1740 j |= mixtable[i].play;
1742 spin_lock_irqsave(&s->lock, flags);
1743 wrmixer(s, DSP_MIX_OUTMIXIDX, j);
1744 spin_unlock_irqrestore(&s->lock, flags);
1749 if (i >= SOUND_MIXER_NRDEVICES || !mixtable[i].type)
1751 if (get_user(val, p))
1754 r = (val >> 8) & 0xff;
1759 spin_lock_irqsave(&s->lock, flags);
1760 switch (mixtable[i].type) {
1766 frobindir(s, mixtable[i].left, 0xf0, l / 6);
1767 frobindir(s, mixtable[i].right, 0xf0, l / 6);
1771 rl = (l < 4 ? 0 : (l - 5) / 3) & 31;
1773 wrmixer(s, mixtable[i].left, rl<<3);
1774 if (i == SOUND_MIXER_MIC)
1775 maskb(s->iobase + CODEC_CMI_MIXER2, ~0x0e, rr<<1);
1779 rl = l < 4 ? 0 : (l - 5) / 3;
1780 wrmixer(s, mixtable[i].left, rl<<3);
1781 l = rdmixer(s, DSP_MIX_OUTMIXIDX) & ~mixtable[i].play;
1782 r = rl ? mixtable[i].play : 0;
1783 wrmixer(s, DSP_MIX_OUTMIXIDX, l | r);
1785 if (i == SOUND_MIXER_MIC) {
1786 if (s->chip_version >= 37) {
1788 maskb(s->iobase + CODEC_CMI_MIXER2, ~0x0e, (rr&0x07)<<1);
1789 frobindir(s, DSP_MIX_EXTENSION, ~0x01, rr>>3);
1792 maskb(s->iobase + CODEC_CMI_MIXER2, ~0x0e, rr<<1);
1798 rl = l < 4 ? 0 : (l - 5) / 3;
1799 rr = r < 4 ? 0 : (r - 5) / 3;
1800 wrmixer(s, mixtable[i].left, rl<<3);
1801 wrmixer(s, mixtable[i].right, rr<<3);
1802 l = rdmixer(s, DSP_MIX_OUTMIXIDX);
1803 l &= ~mixtable[i].play;
1804 r = (rl|rr) ? mixtable[i].play : 0;
1805 wrmixer(s, DSP_MIX_OUTMIXIDX, l | r);
1817 wrmixer(s, mixtable[i].left, rl);
1818 wrmixer(s, mixtable[i].right, rr);
1821 spin_unlock_irqrestore(&s->lock, flags);
1825 s->mix.vol[volidx[i]-1] = val;
1826 return put_user(s->mix.vol[volidx[i]-1], p);
1830 /* --------------------------------------------------------------------- */
1832 static int cm_open_mixdev(struct inode *inode, struct file *file)
1834 int minor = iminor(inode);
1835 struct list_head *list;
1838 for (list = devs.next; ; list = list->next) {
1841 s = list_entry(list, struct cm_state, devs);
1842 if (s->dev_mixer == minor)
1846 file->private_data = s;
1847 return nonseekable_open(inode, file);
1850 static int cm_release_mixdev(struct inode *inode, struct file *file)
1852 struct cm_state *s = (struct cm_state *)file->private_data;
1858 static int cm_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1860 return mixer_ioctl((struct cm_state *)file->private_data, cmd, arg);
1863 static /*const*/ struct file_operations cm_mixer_fops = {
1864 .owner = THIS_MODULE,
1865 .llseek = no_llseek,
1866 .ioctl = cm_ioctl_mixdev,
1867 .open = cm_open_mixdev,
1868 .release = cm_release_mixdev,
1872 /* --------------------------------------------------------------------- */
1874 static int drain_dac(struct cm_state *s, int nonblock)
1876 DECLARE_WAITQUEUE(wait, current);
1877 unsigned long flags;
1880 if (s->dma_dac.mapped || !s->dma_dac.ready)
1882 add_wait_queue(&s->dma_dac.wait, &wait);
1884 __set_current_state(TASK_INTERRUPTIBLE);
1885 spin_lock_irqsave(&s->lock, flags);
1886 count = s->dma_dac.count;
1887 spin_unlock_irqrestore(&s->lock, flags);
1890 if (signal_pending(current))
1893 remove_wait_queue(&s->dma_dac.wait, &wait);
1894 set_current_state(TASK_RUNNING);
1897 tmo = 3 * HZ * (count + s->dma_dac.fragsize) / 2 / s->ratedac;
1898 tmo >>= sample_shift[(s->fmt >> CM_CFMT_DACSHIFT) & CM_CFMT_MASK];
1899 if (!schedule_timeout(tmo + 1))
1900 DBG(printk(KERN_DEBUG "cmpci: dma timed out??\n");)
1902 remove_wait_queue(&s->dma_dac.wait, &wait);
1903 set_current_state(TASK_RUNNING);
1904 if (signal_pending(current))
1905 return -ERESTARTSYS;
1909 /* --------------------------------------------------------------------- */
1911 static ssize_t cm_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
1913 struct cm_state *s = (struct cm_state *)file->private_data;
1914 DECLARE_WAITQUEUE(wait, current);
1916 unsigned long flags;
1921 if (s->dma_adc.mapped)
1923 if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
1925 if (!access_ok(VERIFY_WRITE, buffer, count))
1929 add_wait_queue(&s->dma_adc.wait, &wait);
1931 spin_lock_irqsave(&s->lock, flags);
1932 swptr = s->dma_adc.swptr;
1933 cnt = s->dma_adc.dmasize-swptr;
1934 if (s->dma_adc.count < cnt)
1935 cnt = s->dma_adc.count;
1937 __set_current_state(TASK_INTERRUPTIBLE);
1938 spin_unlock_irqrestore(&s->lock, flags);
1942 if (s->dma_adc.enabled)
1944 if (file->f_flags & O_NONBLOCK) {
1949 if (!schedule_timeout(HZ)) {
1950 printk(KERN_DEBUG "cmpci: read: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1951 s->dma_adc.dmasize, s->dma_adc.fragsize, s->dma_adc.count,
1952 s->dma_adc.hwptr, s->dma_adc.swptr);
1953 spin_lock_irqsave(&s->lock, flags);
1954 stop_adc_unlocked(s);
1955 set_dmaadc(s, s->dma_adc.dmaaddr, s->dma_adc.dmasamples);
1956 /* program sample counts */
1957 set_countadc(s, s->dma_adc.fragsamples);
1958 s->dma_adc.count = s->dma_adc.hwptr = s->dma_adc.swptr = 0;
1959 spin_unlock_irqrestore(&s->lock, flags);
1961 if (signal_pending(current)) {
1968 if (s->status & DO_BIGENDIAN_R) {
1971 char __user *dst = buffer;
1972 unsigned char data[2];
1974 src = (unsigned char *) (s->dma_adc.rawbuf + swptr);
1975 // copy left/right sample at one time
1976 for (i = 0; i < cnt / 2; i++) {
1979 if ((err = __put_user(data[0], dst++))) {
1983 if ((err = __put_user(data[1], dst++))) {
1989 } else if (copy_to_user(buffer, s->dma_adc.rawbuf + swptr, cnt)) {
1994 swptr = (swptr + cnt) % s->dma_adc.dmasize;
1995 spin_lock_irqsave(&s->lock, flags);
1996 s->dma_adc.swptr = swptr;
1997 s->dma_adc.count -= cnt;
2001 if (s->dma_adc.enabled)
2002 start_adc_unlocked(s);
2003 spin_unlock_irqrestore(&s->lock, flags);
2006 remove_wait_queue(&s->dma_adc.wait, &wait);
2007 set_current_state(TASK_RUNNING);
2011 static ssize_t cm_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
2013 struct cm_state *s = (struct cm_state *)file->private_data;
2014 DECLARE_WAITQUEUE(wait, current);
2016 unsigned long flags;
2021 if (s->dma_dac.mapped)
2023 if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
2025 if (!access_ok(VERIFY_READ, buffer, count))
2027 if (s->status & DO_DUAL_DAC) {
2028 if (s->dma_adc.mapped)
2030 if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
2033 if (!access_ok(VERIFY_READ, buffer, count))
2037 add_wait_queue(&s->dma_dac.wait, &wait);
2039 spin_lock_irqsave(&s->lock, flags);
2040 if (s->dma_dac.count < 0) {
2041 s->dma_dac.count = 0;
2042 s->dma_dac.swptr = s->dma_dac.hwptr;
2044 if (s->status & DO_DUAL_DAC) {
2045 s->dma_adc.swptr = s->dma_dac.swptr;
2046 s->dma_adc.count = s->dma_dac.count;
2047 s->dma_adc.endcleared = s->dma_dac.endcleared;
2049 swptr = s->dma_dac.swptr;
2050 cnt = s->dma_dac.dmasize-swptr;
2051 if (s->status & DO_AC3_SW) {
2052 if (s->dma_dac.count + 2 * cnt > s->dma_dac.dmasize)
2053 cnt = (s->dma_dac.dmasize - s->dma_dac.count) / 2;
2055 if (s->dma_dac.count + cnt > s->dma_dac.dmasize)
2056 cnt = s->dma_dac.dmasize - s->dma_dac.count;
2059 __set_current_state(TASK_INTERRUPTIBLE);
2060 spin_unlock_irqrestore(&s->lock, flags);
2063 if ((s->status & DO_DUAL_DAC) && (cnt > count / 2))
2066 if (s->dma_dac.enabled)
2068 if (file->f_flags & O_NONBLOCK) {
2073 if (!schedule_timeout(HZ)) {
2074 printk(KERN_DEBUG "cmpci: write: chip lockup? dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
2075 s->dma_dac.dmasize, s->dma_dac.fragsize, s->dma_dac.count,
2076 s->dma_dac.hwptr, s->dma_dac.swptr);
2077 spin_lock_irqsave(&s->lock, flags);
2078 stop_dac_unlocked(s);
2079 set_dmadac(s, s->dma_dac.dmaaddr, s->dma_dac.dmasamples);
2080 /* program sample counts */
2081 set_countdac(s, s->dma_dac.fragsamples);
2082 s->dma_dac.count = s->dma_dac.hwptr = s->dma_dac.swptr = 0;
2083 if (s->status & DO_DUAL_DAC) {
2084 set_dmadac1(s, s->dma_adc.dmaaddr, s->dma_adc.dmasamples);
2085 s->dma_adc.count = s->dma_adc.hwptr = s->dma_adc.swptr = 0;
2087 spin_unlock_irqrestore(&s->lock, flags);
2089 if (signal_pending(current)) {
2096 if (s->status & DO_AC3_SW) {
2099 // clip exceeded data, caught by 033 and 037
2100 if (swptr + 2 * cnt > s->dma_dac.dmasize)
2101 cnt = (s->dma_dac.dmasize - swptr) / 2;
2102 if ((err = trans_ac3(s, s->dma_dac.rawbuf + swptr, buffer, cnt))) {
2106 swptr = (swptr + 2 * cnt) % s->dma_dac.dmasize;
2107 } else if ((s->status & DO_DUAL_DAC) && (s->status & DO_BIGENDIAN_W)) {
2109 const char __user *src = buffer;
2110 unsigned char *dst0, *dst1;
2111 unsigned char data[8];
2113 dst0 = (unsigned char *) (s->dma_dac.rawbuf + swptr);
2114 dst1 = (unsigned char *) (s->dma_adc.rawbuf + swptr);
2115 // copy left/right sample at one time
2116 for (i = 0; i < cnt / 4; i++) {
2117 if ((err = __get_user(data[0], src++))) {
2121 if ((err = __get_user(data[1], src++))) {
2125 if ((err = __get_user(data[2], src++))) {
2129 if ((err = __get_user(data[3], src++))) {
2133 if ((err = __get_user(data[4], src++))) {
2137 if ((err = __get_user(data[5], src++))) {
2141 if ((err = __get_user(data[6], src++))) {
2145 if ((err = __get_user(data[7], src++))) {
2160 swptr = (swptr + cnt) % s->dma_dac.dmasize;
2161 } else if (s->status & DO_DUAL_DAC) {
2163 unsigned long __user *src = (unsigned long __user *) buffer;
2164 unsigned long *dst0, *dst1;
2166 dst0 = (unsigned long *) (s->dma_dac.rawbuf + swptr);
2167 dst1 = (unsigned long *) (s->dma_adc.rawbuf + swptr);
2168 // copy left/right sample at one time
2169 for (i = 0; i < cnt / 4; i++) {
2170 if ((err = __get_user(*dst0++, src++))) {
2174 if ((err = __get_user(*dst1++, src++))) {
2179 swptr = (swptr + cnt) % s->dma_dac.dmasize;
2180 } else if (s->status & DO_BIGENDIAN_W) {
2182 const char __user *src = buffer;
2184 unsigned char data[2];
2186 dst = (unsigned char *) (s->dma_dac.rawbuf + swptr);
2187 // swap hi/lo bytes for each sample
2188 for (i = 0; i < cnt / 2; i++) {
2189 if ((err = __get_user(data[0], src++))) {
2193 if ((err = __get_user(data[1], src++))) {
2201 swptr = (swptr + cnt) % s->dma_dac.dmasize;
2203 if (copy_from_user(s->dma_dac.rawbuf + swptr, buffer, cnt)) {
2208 swptr = (swptr + cnt) % s->dma_dac.dmasize;
2210 spin_lock_irqsave(&s->lock, flags);
2211 s->dma_dac.swptr = swptr;
2212 s->dma_dac.count += cnt;
2213 if (s->status & DO_AC3_SW)
2214 s->dma_dac.count += cnt;
2215 s->dma_dac.endcleared = 0;
2216 spin_unlock_irqrestore(&s->lock, flags);
2220 if (s->status & DO_DUAL_DAC) {
2225 if (s->dma_dac.enabled)
2229 remove_wait_queue(&s->dma_dac.wait, &wait);
2230 set_current_state(TASK_RUNNING);
2234 static unsigned int cm_poll(struct file *file, struct poll_table_struct *wait)
2236 struct cm_state *s = (struct cm_state *)file->private_data;
2237 unsigned long flags;
2238 unsigned int mask = 0;
2241 if (file->f_mode & FMODE_WRITE) {
2242 if (!s->dma_dac.ready && prog_dmabuf(s, 0))
2244 poll_wait(file, &s->dma_dac.wait, wait);
2246 if (file->f_mode & FMODE_READ) {
2247 if (!s->dma_adc.ready && prog_dmabuf(s, 1))
2249 poll_wait(file, &s->dma_adc.wait, wait);
2251 spin_lock_irqsave(&s->lock, flags);
2253 if (file->f_mode & FMODE_READ) {
2254 if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
2255 mask |= POLLIN | POLLRDNORM;
2257 if (file->f_mode & FMODE_WRITE) {
2258 if (s->dma_dac.mapped) {
2259 if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
2260 mask |= POLLOUT | POLLWRNORM;
2262 if ((signed)s->dma_dac.dmasize >= s->dma_dac.count + (signed)s->dma_dac.fragsize)
2263 mask |= POLLOUT | POLLWRNORM;
2266 spin_unlock_irqrestore(&s->lock, flags);
2270 static int cm_mmap(struct file *file, struct vm_area_struct *vma)
2272 struct cm_state *s = (struct cm_state *)file->private_data;
2279 if (vma->vm_flags & VM_WRITE) {
2280 if ((ret = prog_dmabuf(s, 0)) != 0)
2283 } else if (vma->vm_flags & VM_READ) {
2284 if ((ret = prog_dmabuf(s, 1)) != 0)
2290 if (vma->vm_pgoff != 0)
2292 size = vma->vm_end - vma->vm_start;
2293 if (size > (PAGE_SIZE << db->buforder))
2296 if (remap_pfn_range(vma, vma->vm_start,
2297 virt_to_phys(db->rawbuf) >> PAGE_SHIFT,
2298 size, vma->vm_page_prot))
2307 #define SNDCTL_SPDIF_COPYRIGHT _SIOW('S', 0, int) // set/reset S/PDIF copy protection
2308 #define SNDCTL_SPDIF_LOOP _SIOW('S', 1, int) // set/reset S/PDIF loop
2309 #define SNDCTL_SPDIF_MONITOR _SIOW('S', 2, int) // set S/PDIF monitor
2310 #define SNDCTL_SPDIF_LEVEL _SIOW('S', 3, int) // set/reset S/PDIF out level
2311 #define SNDCTL_SPDIF_INV _SIOW('S', 4, int) // set/reset S/PDIF in inverse
2312 #define SNDCTL_SPDIF_SEL2 _SIOW('S', 5, int) // set S/PDIF in #2
2313 #define SNDCTL_SPDIF_VALID _SIOW('S', 6, int) // set S/PDIF valid
2314 #define SNDCTL_SPDIFOUT _SIOW('S', 7, int) // set S/PDIF out
2315 #define SNDCTL_SPDIFIN _SIOW('S', 8, int) // set S/PDIF out
2317 static int cm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2319 struct cm_state *s = (struct cm_state *)file->private_data;
2320 unsigned long flags;
2321 audio_buf_info abinfo;
2323 int val, mapped, ret;
2324 unsigned char fmtm, fmtd;
2325 void __user *argp = (void __user *)arg;
2326 int __user *p = argp;
2329 mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
2330 ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
2332 case OSS_GETVERSION:
2333 return put_user(SOUND_VERSION, p);
2335 case SNDCTL_DSP_SYNC:
2336 if (file->f_mode & FMODE_WRITE)
2337 return drain_dac(s, 0/*file->f_flags & O_NONBLOCK*/);
2340 case SNDCTL_DSP_SETDUPLEX:
2343 case SNDCTL_DSP_GETCAPS:
2344 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_BIND, p);
2346 case SNDCTL_DSP_RESET:
2347 if (file->f_mode & FMODE_WRITE) {
2349 synchronize_irq(s->irq);
2350 s->dma_dac.swptr = s->dma_dac.hwptr = s->dma_dac.count = s->dma_dac.total_bytes = 0;
2351 if (s->status & DO_DUAL_DAC)
2352 s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes = 0;
2354 if (file->f_mode & FMODE_READ) {
2356 synchronize_irq(s->irq);
2357 s->dma_adc.swptr = s->dma_adc.hwptr = s->dma_adc.count = s->dma_adc.total_bytes = 0;
2361 case SNDCTL_DSP_SPEED:
2362 if (get_user(val, p))
2365 if (file->f_mode & FMODE_READ) {
2366 spin_lock_irqsave(&s->lock, flags);
2367 stop_adc_unlocked(s);
2368 s->dma_adc.ready = 0;
2369 set_adc_rate_unlocked(s, val);
2370 spin_unlock_irqrestore(&s->lock, flags);
2372 if (file->f_mode & FMODE_WRITE) {
2374 s->dma_dac.ready = 0;
2375 if (s->status & DO_DUAL_DAC)
2376 s->dma_adc.ready = 0;
2377 set_dac_rate(s, val);
2380 return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
2382 case SNDCTL_DSP_STEREO:
2383 if (get_user(val, p))
2387 if (file->f_mode & FMODE_READ) {
2389 s->dma_adc.ready = 0;
2391 fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
2393 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
2395 if (file->f_mode & FMODE_WRITE) {
2397 s->dma_dac.ready = 0;
2399 fmtd |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
2401 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_DACSHIFT);
2402 if (s->status & DO_DUAL_DAC) {
2403 s->dma_adc.ready = 0;
2405 fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
2407 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
2410 set_fmt(s, fmtm, fmtd);
2413 case SNDCTL_DSP_CHANNELS:
2414 if (get_user(val, p))
2419 if (file->f_mode & FMODE_READ) {
2421 s->dma_adc.ready = 0;
2423 fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
2425 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
2427 if (file->f_mode & FMODE_WRITE) {
2429 s->dma_dac.ready = 0;
2431 fmtd |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
2433 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_DACSHIFT);
2434 if (s->status & DO_DUAL_DAC) {
2435 s->dma_adc.ready = 0;
2437 fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
2439 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
2442 set_fmt(s, fmtm, fmtd);
2443 if ((s->capability & CAN_MULTI_CH)
2444 && (file->f_mode & FMODE_WRITE)) {
2445 val = set_dac_channels(s, val);
2446 return put_user(val, p);
2449 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_STEREO << CM_CFMT_ADCSHIFT)
2450 : (CM_CFMT_STEREO << CM_CFMT_DACSHIFT))) ? 2 : 1, p);
2452 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
2453 return put_user(AFMT_S16_BE|AFMT_S16_LE|AFMT_U8|
2454 ((s->capability & CAN_AC3) ? AFMT_AC3 : 0), p);
2456 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
2457 if (get_user(val, p))
2459 if (val != AFMT_QUERY) {
2462 if (file->f_mode & FMODE_READ) {
2464 s->dma_adc.ready = 0;
2465 if (val == AFMT_S16_BE || val == AFMT_S16_LE)
2466 fmtd |= CM_CFMT_16BIT << CM_CFMT_ADCSHIFT;
2468 fmtm &= ~(CM_CFMT_16BIT << CM_CFMT_ADCSHIFT);
2469 if (val == AFMT_S16_BE)
2470 s->status |= DO_BIGENDIAN_R;
2472 s->status &= ~DO_BIGENDIAN_R;
2474 if (file->f_mode & FMODE_WRITE) {
2476 s->dma_dac.ready = 0;
2477 if (val == AFMT_S16_BE || val == AFMT_S16_LE || val == AFMT_AC3)
2478 fmtd |= CM_CFMT_16BIT << CM_CFMT_DACSHIFT;
2480 fmtm &= ~(CM_CFMT_16BIT << CM_CFMT_DACSHIFT);
2481 if (val == AFMT_AC3) {
2482 fmtd |= CM_CFMT_STEREO << CM_CFMT_DACSHIFT;
2486 if (s->status & DO_DUAL_DAC) {
2487 s->dma_adc.ready = 0;
2488 if (val == AFMT_S16_BE || val == AFMT_S16_LE)
2489 fmtd |= CM_CFMT_STEREO << CM_CFMT_ADCSHIFT;
2491 fmtm &= ~(CM_CFMT_STEREO << CM_CFMT_ADCSHIFT);
2493 if (val == AFMT_S16_BE)
2494 s->status |= DO_BIGENDIAN_W;
2496 s->status &= ~DO_BIGENDIAN_W;
2498 set_fmt(s, fmtm, fmtd);
2500 if (s->status & DO_AC3) return put_user(AFMT_AC3, p);
2501 return put_user((s->fmt & ((file->f_mode & FMODE_READ) ? (CM_CFMT_16BIT << CM_CFMT_ADCSHIFT)
2502 : (CM_CFMT_16BIT << CM_CFMT_DACSHIFT))) ? val : AFMT_U8, p);
2504 case SNDCTL_DSP_POST:
2507 case SNDCTL_DSP_GETTRIGGER:
2509 if (s->status & DO_DUAL_DAC) {
2510 if (file->f_mode & FMODE_WRITE &&
2511 (s->enable & ENDAC) &&
2512 (s->enable & ENADC))
2513 val |= PCM_ENABLE_OUTPUT;
2514 return put_user(val, p);
2516 if (file->f_mode & FMODE_READ && s->enable & ENADC)
2517 val |= PCM_ENABLE_INPUT;
2518 if (file->f_mode & FMODE_WRITE && s->enable & ENDAC)
2519 val |= PCM_ENABLE_OUTPUT;
2520 return put_user(val, p);
2522 case SNDCTL_DSP_SETTRIGGER:
2523 if (get_user(val, p))
2525 if (file->f_mode & FMODE_READ) {
2526 if (val & PCM_ENABLE_INPUT) {
2527 if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
2529 s->dma_adc.enabled = 1;
2532 s->dma_adc.enabled = 0;
2536 if (file->f_mode & FMODE_WRITE) {
2537 if (val & PCM_ENABLE_OUTPUT) {
2538 if (!s->dma_dac.ready && (ret = prog_dmabuf(s, 0)))
2540 if (s->status & DO_DUAL_DAC) {
2541 if (!s->dma_adc.ready && (ret = prog_dmabuf(s, 1)))
2544 s->dma_dac.enabled = 1;
2547 s->dma_dac.enabled = 0;
2553 case SNDCTL_DSP_GETOSPACE:
2554 if (!(file->f_mode & FMODE_WRITE))
2556 if (!(s->enable & ENDAC) && (val = prog_dmabuf(s, 0)) != 0)
2558 spin_lock_irqsave(&s->lock, flags);
2560 abinfo.fragsize = s->dma_dac.fragsize;
2561 abinfo.bytes = s->dma_dac.dmasize - s->dma_dac.count;
2562 abinfo.fragstotal = s->dma_dac.numfrag;
2563 abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
2564 spin_unlock_irqrestore(&s->lock, flags);
2565 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2567 case SNDCTL_DSP_GETISPACE:
2568 if (!(file->f_mode & FMODE_READ))
2570 if (!(s->enable & ENADC) && (val = prog_dmabuf(s, 1)) != 0)
2572 spin_lock_irqsave(&s->lock, flags);
2574 abinfo.fragsize = s->dma_adc.fragsize;
2575 abinfo.bytes = s->dma_adc.count;
2576 abinfo.fragstotal = s->dma_adc.numfrag;
2577 abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;
2578 spin_unlock_irqrestore(&s->lock, flags);
2579 return copy_to_user(argp, &abinfo, sizeof(abinfo)) ? -EFAULT : 0;
2581 case SNDCTL_DSP_NONBLOCK:
2582 file->f_flags |= O_NONBLOCK;
2585 case SNDCTL_DSP_GETODELAY:
2586 if (!(file->f_mode & FMODE_WRITE))
2588 spin_lock_irqsave(&s->lock, flags);
2590 val = s->dma_dac.count;
2591 spin_unlock_irqrestore(&s->lock, flags);
2592 return put_user(val, p);
2594 case SNDCTL_DSP_GETIPTR:
2595 if (!(file->f_mode & FMODE_READ))
2597 spin_lock_irqsave(&s->lock, flags);
2599 cinfo.bytes = s->dma_adc.total_bytes;
2600 cinfo.blocks = s->dma_adc.count >> s->dma_adc.fragshift;
2601 cinfo.ptr = s->dma_adc.hwptr;
2602 if (s->dma_adc.mapped)
2603 s->dma_adc.count &= s->dma_adc.fragsize-1;
2604 spin_unlock_irqrestore(&s->lock, flags);
2605 return copy_to_user(argp, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
2607 case SNDCTL_DSP_GETOPTR:
2608 if (!(file->f_mode & FMODE_WRITE))
2610 spin_lock_irqsave(&s->lock, flags);
2612 cinfo.bytes = s->dma_dac.total_bytes;
2613 cinfo.blocks = s->dma_dac.count >> s->dma_dac.fragshift;
2614 cinfo.ptr = s->dma_dac.hwptr;
2615 if (s->dma_dac.mapped)
2616 s->dma_dac.count &= s->dma_dac.fragsize-1;
2617 if (s->status & DO_DUAL_DAC) {
2618 if (s->dma_adc.mapped)
2619 s->dma_adc.count &= s->dma_adc.fragsize-1;
2621 spin_unlock_irqrestore(&s->lock, flags);
2622 return copy_to_user(argp, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
2624 case SNDCTL_DSP_GETBLKSIZE:
2625 if (file->f_mode & FMODE_WRITE) {
2626 if ((val = prog_dmabuf(s, 0)))
2628 if (s->status & DO_DUAL_DAC) {
2629 if ((val = prog_dmabuf(s, 1)))
2631 return put_user(2 * s->dma_dac.fragsize, p);
2633 return put_user(s->dma_dac.fragsize, p);
2635 if ((val = prog_dmabuf(s, 1)))
2637 return put_user(s->dma_adc.fragsize, p);
2639 case SNDCTL_DSP_SETFRAGMENT:
2640 if (get_user(val, p))
2642 if (file->f_mode & FMODE_READ) {
2643 s->dma_adc.ossfragshift = val & 0xffff;
2644 s->dma_adc.ossmaxfrags = (val >> 16) & 0xffff;
2645 if (s->dma_adc.ossfragshift < 4)
2646 s->dma_adc.ossfragshift = 4;
2647 if (s->dma_adc.ossfragshift > 15)
2648 s->dma_adc.ossfragshift = 15;
2649 if (s->dma_adc.ossmaxfrags < 4)
2650 s->dma_adc.ossmaxfrags = 4;
2652 if (file->f_mode & FMODE_WRITE) {
2653 s->dma_dac.ossfragshift = val & 0xffff;
2654 s->dma_dac.ossmaxfrags = (val >> 16) & 0xffff;
2655 if (s->dma_dac.ossfragshift < 4)
2656 s->dma_dac.ossfragshift = 4;
2657 if (s->dma_dac.ossfragshift > 15)
2658 s->dma_dac.ossfragshift = 15;
2659 if (s->dma_dac.ossmaxfrags < 4)
2660 s->dma_dac.ossmaxfrags = 4;
2661 if (s->status & DO_DUAL_DAC) {
2662 s->dma_adc.ossfragshift = s->dma_dac.ossfragshift;
2663 s->dma_adc.ossmaxfrags = s->dma_dac.ossmaxfrags;
2668 case SNDCTL_DSP_SUBDIVIDE:
2669 if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
2670 (file->f_mode & FMODE_WRITE && s->dma_dac.subdivision))
2672 if (get_user(val, p))
2674 if (val != 1 && val != 2 && val != 4)
2676 if (file->f_mode & FMODE_READ)
2677 s->dma_adc.subdivision = val;
2678 if (file->f_mode & FMODE_WRITE) {
2679 s->dma_dac.subdivision = val;
2680 if (s->status & DO_DUAL_DAC)
2681 s->dma_adc.subdivision = val;
2685 case SOUND_PCM_READ_RATE:
2686 return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
2688 case SOUND_PCM_READ_CHANNELS:
2689 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);
2691 case SOUND_PCM_READ_BITS:
2692 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);
2694 case SOUND_PCM_READ_FILTER:
2695 return put_user((file->f_mode & FMODE_READ) ? s->rateadc : s->ratedac, p);
2697 case SNDCTL_DSP_GETCHANNELMASK:
2698 return put_user(DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE|DSP_BIND_SPDIF, p);
2700 case SNDCTL_DSP_BIND_CHANNEL:
2701 if (get_user(val, p))
2703 if (val == DSP_BIND_QUERY) {
2704 val = DSP_BIND_FRONT;
2705 if (s->status & DO_SPDIF_OUT)
2706 val |= DSP_BIND_SPDIF;
2708 if (s->curr_channels == 4)
2709 val |= DSP_BIND_SURR;
2710 if (s->curr_channels > 4)
2711 val |= DSP_BIND_CENTER_LFE;
2714 if (file->f_mode & FMODE_READ) {
2716 s->dma_adc.ready = 0;
2717 if (val & DSP_BIND_SPDIF) {
2718 set_spdifin(s, s->rateadc);
2719 if (!(s->status & DO_SPDIF_OUT))
2720 val &= ~DSP_BIND_SPDIF;
2723 if (file->f_mode & FMODE_WRITE) {
2725 s->dma_dac.ready = 0;
2726 if (val & DSP_BIND_SPDIF) {
2727 set_spdifout(s, s->ratedac);
2728 set_dac_channels(s, s->fmt & (CM_CFMT_STEREO << CM_CFMT_DACSHIFT) ? 2 : 1);
2729 if (!(s->status & DO_SPDIF_OUT))
2730 val &= ~DSP_BIND_SPDIF;
2735 mask = val & (DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE);
2737 case DSP_BIND_FRONT:
2740 case DSP_BIND_FRONT|DSP_BIND_SURR:
2743 case DSP_BIND_FRONT|DSP_BIND_SURR|DSP_BIND_CENTER_LFE:
2747 channels = s->fmt & (CM_CFMT_STEREO << CM_CFMT_DACSHIFT) ? 2 : 1;
2750 set_dac_channels(s, channels);
2754 return put_user(val, p);
2756 case SOUND_PCM_WRITE_FILTER:
2757 case SNDCTL_DSP_MAPINBUF:
2758 case SNDCTL_DSP_MAPOUTBUF:
2759 case SNDCTL_DSP_SETSYNCRO:
2761 case SNDCTL_SPDIF_COPYRIGHT:
2762 if (get_user(val, p))
2764 set_spdif_copyright(s, val);
2766 case SNDCTL_SPDIF_LOOP:
2767 if (get_user(val, p))
2769 set_spdif_loop(s, val);
2771 case SNDCTL_SPDIF_MONITOR:
2772 if (get_user(val, p))
2774 set_spdif_monitor(s, val);
2776 case SNDCTL_SPDIF_LEVEL:
2777 if (get_user(val, p))
2779 set_spdifout_level(s, val);
2781 case SNDCTL_SPDIF_INV:
2782 if (get_user(val, p))
2784 set_spdifin_inverse(s, val);
2786 case SNDCTL_SPDIF_SEL2:
2787 if (get_user(val, p))
2789 set_spdifin_channel2(s, val);
2791 case SNDCTL_SPDIF_VALID:
2792 if (get_user(val, p))
2794 set_spdifin_valid(s, val);
2796 case SNDCTL_SPDIFOUT:
2797 if (get_user(val, p))
2799 set_spdifout(s, val ? s->ratedac : 0);
2801 case SNDCTL_SPDIFIN:
2802 if (get_user(val, p))
2804 set_spdifin(s, val ? s->rateadc : 0);
2807 return mixer_ioctl(s, cmd, arg);
2810 static int cm_open(struct inode *inode, struct file *file)
2812 int minor = iminor(inode);
2813 DECLARE_WAITQUEUE(wait, current);
2814 unsigned char fmtm = ~0, fmts = 0;
2815 struct list_head *list;
2818 for (list = devs.next; ; list = list->next) {
2821 s = list_entry(list, struct cm_state, devs);
2822 if (!((s->dev_audio ^ minor) & ~0xf))
2826 file->private_data = s;
2827 /* wait for device to become free */
2829 while (s->open_mode & file->f_mode) {
2830 if (file->f_flags & O_NONBLOCK) {
2834 add_wait_queue(&s->open_wait, &wait);
2835 __set_current_state(TASK_INTERRUPTIBLE);
2838 remove_wait_queue(&s->open_wait, &wait);
2839 set_current_state(TASK_RUNNING);
2840 if (signal_pending(current))
2841 return -ERESTARTSYS;
2844 if (file->f_mode & FMODE_READ) {
2845 s->status &= ~DO_BIGENDIAN_R;
2846 fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_ADCSHIFT);
2847 if ((minor & 0xf) == SND_DEV_DSP16)
2848 fmts |= CM_CFMT_16BIT << CM_CFMT_ADCSHIFT;
2849 s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags = s->dma_adc.subdivision = 0;
2850 s->dma_adc.enabled = 1;
2851 set_adc_rate(s, 8000);
2852 // spdif-in is turnned off by default
2855 if (file->f_mode & FMODE_WRITE) {
2856 s->status &= ~DO_BIGENDIAN_W;
2857 fmtm &= ~((CM_CFMT_STEREO | CM_CFMT_16BIT) << CM_CFMT_DACSHIFT);
2858 if ((minor & 0xf) == SND_DEV_DSP16)
2859 fmts |= CM_CFMT_16BIT << CM_CFMT_DACSHIFT;
2860 s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags = s->dma_dac.subdivision = 0;
2861 s->dma_dac.enabled = 1;
2862 set_dac_rate(s, 8000);
2863 // clear previous multichannel, spdif, ac3 state
2866 set_dac_channels(s, 1);
2868 set_fmt(s, fmtm, fmts);
2869 s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
2871 return nonseekable_open(inode, file);
2874 static int cm_release(struct inode *inode, struct file *file)
2876 struct cm_state *s = (struct cm_state *)file->private_data;
2880 if (file->f_mode & FMODE_WRITE)
2881 drain_dac(s, file->f_flags & O_NONBLOCK);
2883 if (file->f_mode & FMODE_WRITE) {
2886 dealloc_dmabuf(s, &s->dma_dac);
2887 if (s->status & DO_DUAL_DAC)
2888 dealloc_dmabuf(s, &s->dma_adc);
2890 if (s->status & DO_MULTI_CH)
2891 set_dac_channels(s, 1);
2892 if (s->status & DO_AC3)
2894 if (s->status & DO_SPDIF_OUT)
2896 /* enable SPDIF loop */
2897 set_spdif_loop(s, spdif_loop);
2898 s->status &= ~DO_BIGENDIAN_W;
2900 if (file->f_mode & FMODE_READ) {
2902 dealloc_dmabuf(s, &s->dma_adc);
2903 s->status &= ~DO_BIGENDIAN_R;
2905 s->open_mode &= ~(file->f_mode & (FMODE_READ|FMODE_WRITE));
2907 wake_up(&s->open_wait);
2912 static /*const*/ struct file_operations cm_audio_fops = {
2913 .owner = THIS_MODULE,
2914 .llseek = no_llseek,
2921 .release = cm_release,
2924 /* --------------------------------------------------------------------- */
2926 static struct initvol {
2929 } initvol[] __devinitdata = {
2930 { SOUND_MIXER_WRITE_CD, 0x4f4f },
2931 { SOUND_MIXER_WRITE_LINE, 0x4f4f },
2932 { SOUND_MIXER_WRITE_MIC, 0x4f4f },
2933 { SOUND_MIXER_WRITE_SYNTH, 0x4f4f },
2934 { SOUND_MIXER_WRITE_VOLUME, 0x4f4f },
2935 { SOUND_MIXER_WRITE_PCM, 0x4f4f }
2938 /* check chip version and capability */
2939 static int query_chip(struct cm_state *s)
2941 int ChipVersion = -1;
2942 unsigned char RegValue;
2944 // check reg 0Ch, bit 24-31
2945 RegValue = inb(s->iobase + CODEC_CMI_INT_HLDCLR + 3);
2946 if (RegValue == 0) {
2947 // check reg 08h, bit 24-28
2948 RegValue = inb(s->iobase + CODEC_CMI_CHFORMAT + 3);
2950 if (RegValue == 0) {
2952 s->max_channels = 4;
2953 s->capability |= CAN_AC3_SW;
2954 s->capability |= CAN_DUAL_DAC;
2957 s->max_channels = 4;
2958 s->capability |= CAN_AC3_HW;
2959 s->capability |= CAN_DUAL_DAC;
2962 // check reg 0Ch, bit 26
2963 if (RegValue & (1 << (26-24))) {
2965 if (RegValue & (1 << (24-24)))
2966 s->max_channels = 6;
2968 s->max_channels = 4;
2969 s->capability |= CAN_AC3_HW;
2970 s->capability |= CAN_DUAL_DAC;
2971 s->capability |= CAN_MULTI_CH_HW;
2972 s->capability |= CAN_LINE_AS_BASS;
2973 s->capability |= CAN_MIC_AS_BASS;
2975 ChipVersion = 55; // 4 or 6 channels
2976 s->max_channels = 6;
2977 s->capability |= CAN_AC3_HW;
2978 s->capability |= CAN_DUAL_DAC;
2979 s->capability |= CAN_MULTI_CH_HW;
2980 s->capability |= CAN_LINE_AS_BASS;
2981 s->capability |= CAN_MIC_AS_BASS;
2984 s->capability |= CAN_LINE_AS_REAR;
2988 #ifdef CONFIG_SOUND_CMPCI_JOYSTICK
2989 static int __devinit cm_create_gameport(struct cm_state *s, int io_port)
2991 struct gameport *gp;
2993 if (!request_region(io_port, CM_EXTENT_GAME, "cmpci GAME")) {
2994 printk(KERN_ERR "cmpci: gameport io ports 0x%#x in use\n", io_port);
2998 if (!(s->gameport = gp = gameport_allocate_port())) {
2999 printk(KERN_ERR "cmpci: can not allocate memory for gameport\n");
3000 release_region(io_port, CM_EXTENT_GAME);
3004 gameport_set_name(gp, "C-Media GP");
3005 gameport_set_phys(gp, "pci%s/gameport0", pci_name(s->dev));
3006 gp->dev.parent = &s->dev->dev;
3009 /* enable joystick */
3010 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x02);
3012 gameport_register_port(gp);
3017 static void __devexit cm_free_gameport(struct cm_state *s)
3020 int gpio = s->gameport->io;
3022 gameport_unregister_port(s->gameport);
3024 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x02, 0);
3025 release_region(gpio, CM_EXTENT_GAME);
3029 static inline int cm_create_gameport(struct cm_state *s, int io_port) { return -ENOSYS; }
3030 static inline void cm_free_gameport(struct cm_state *s) { }
3033 #define echo_option(x)\
3034 if (x) strcat(options, "" #x " ")
3036 static int __devinit cm_probe(struct pci_dev *pcidev, const struct pci_device_id *pciid)
3041 unsigned char reg_mask;
3043 struct resource *ports;
3045 unsigned short deviceid;
3048 { PCI_DEVICE_ID_CMEDIA_CM8338A, "CM8338A" },
3049 { PCI_DEVICE_ID_CMEDIA_CM8338B, "CM8338B" },
3050 { PCI_DEVICE_ID_CMEDIA_CM8738, "CM8738" },
3051 { PCI_DEVICE_ID_CMEDIA_CM8738B, "CM8738B" },
3053 char *devicename = "unknown";
3056 if ((ret = pci_enable_device(pcidev)))
3058 if (!(pci_resource_flags(pcidev, 0) & IORESOURCE_IO))
3060 if (pcidev->irq == 0)
3062 i = pci_set_dma_mask(pcidev, DMA_32BIT_MASK);
3064 printk(KERN_WARNING "cmpci: architecture does not support 32bit PCI busmaster DMA\n");
3067 s = kmalloc(sizeof(*s), GFP_KERNEL);
3069 printk(KERN_WARNING "cmpci: out of memory\n");
3072 /* search device name */
3073 for (i = 0; i < sizeof(devicetable) / sizeof(devicetable[0]); i++) {
3074 if (devicetable[i].deviceid == pcidev->device) {
3075 devicename = devicetable[i].devicename;
3079 memset(s, 0, sizeof(struct cm_state));
3080 init_waitqueue_head(&s->dma_adc.wait);
3081 init_waitqueue_head(&s->dma_dac.wait);
3082 init_waitqueue_head(&s->open_wait);
3083 init_MUTEX(&s->open_sem);
3084 spin_lock_init(&s->lock);
3085 s->magic = CM_MAGIC;
3087 s->iobase = pci_resource_start(pcidev, 0);
3090 #ifdef CONFIG_SOUND_CMPCI_MIDI
3096 s->irq = pcidev->irq;
3098 if (!request_region(s->iobase, CM_EXTENT_CODEC, "cmpci")) {
3099 printk(KERN_ERR "cmpci: io ports %#x-%#x in use\n", s->iobase, s->iobase+CM_EXTENT_CODEC-1);
3103 /* dump parameters */
3104 strcpy(options, "cmpci: ");
3105 echo_option(joystick);
3106 echo_option(spdif_inverse);
3107 echo_option(spdif_loop);
3108 echo_option(spdif_out);
3109 echo_option(use_line_as_rear);
3110 echo_option(use_line_as_bass);
3111 echo_option(use_mic_as_bass);
3112 echo_option(mic_boost);
3113 echo_option(hw_copy);
3114 printk(KERN_INFO "%s\n", options);
3116 /* initialize codec registers */
3117 outb(0, s->iobase + CODEC_CMI_INT_HLDCLR + 2); /* disable ints */
3118 outb(0, s->iobase + CODEC_CMI_FUNCTRL0 + 2); /* disable channels */
3120 wrmixer(s, DSP_MIX_DATARESETIDX, 0);
3123 if ((ret = request_irq(s->irq, cm_interrupt, SA_SHIRQ, "cmpci", s))) {
3124 printk(KERN_ERR "cmpci: irq %u in use\n", s->irq);
3127 printk(KERN_INFO "cmpci: found %s adapter at io %#x irq %u\n",
3128 devicename, s->iobase, s->irq);
3129 /* register devices */
3130 if ((s->dev_audio = register_sound_dsp(&cm_audio_fops, -1)) < 0) {
3134 if ((s->dev_mixer = register_sound_mixer(&cm_mixer_fops, -1)) < 0) {
3138 pci_set_master(pcidev); /* enable bus mastering */
3139 /* initialize the chips */
3142 /* set mixer output */
3143 frobindir(s, DSP_MIX_OUTMIXIDX, 0x1f, 0x1f);
3144 /* set mixer input */
3145 val = SOUND_MASK_LINE|SOUND_MASK_SYNTH|SOUND_MASK_CD|SOUND_MASK_MIC;
3146 mixer_ioctl(s, SOUND_MIXER_WRITE_RECSRC, (unsigned long)&val);
3147 for (i = 0; i < sizeof(initvol)/sizeof(initvol[0]); i++) {
3148 val = initvol[i].vol;
3149 mixer_ioctl(s, initvol[i].mixch, (unsigned long)&val);
3152 /* use channel 1 for playback, channel 0 for record */
3153 maskb(s->iobase + CODEC_CMI_FUNCTRL0, ~CHADC1, CHADC0);
3154 /* turn off VMIC3 - mic boost */
3156 maskb(s->iobase + CODEC_CMI_MIXER2, ~1, 0);
3158 maskb(s->iobase + CODEC_CMI_MIXER2, ~0, 1);
3159 s->deviceid = pcidev->device;
3161 if (pcidev->device == PCI_DEVICE_ID_CMEDIA_CM8738
3162 || pcidev->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
3164 /* chip version and hw capability check */
3165 s->chip_version = query_chip(s);
3166 printk(KERN_INFO "cmpci: chip version = 0%d\n", s->chip_version);
3168 /* set SPDIF-in inverse before enable SPDIF loop */
3169 set_spdifin_inverse(s, spdif_inverse);
3171 /* use SPDIF in #1 */
3172 set_spdifin_channel2(s, 0);
3174 s->chip_version = 0;
3175 /* 8338 will fall here */
3176 s->max_channels = 4;
3177 s->capability |= CAN_DUAL_DAC;
3178 s->capability |= CAN_LINE_AS_REAR;
3180 /* enable SPDIF loop */
3181 set_spdif_loop(s, spdif_loop);
3183 // enable 4 speaker mode (analog duplicate)
3184 set_hw_copy(s, hw_copy);
3187 #ifdef CONFIG_SOUND_CMPCI_FM
3189 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~8, 0);
3191 /* don't enable OPL3 if there is one */
3192 if (opl3_detect(s->iosynth, NULL)) {
3195 /* set IO based at 0x388 */
3196 switch (s->iosynth) {
3213 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3, ~0x03, reg_mask);
3216 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~0, 8);
3217 if (opl3_detect(s->iosynth, NULL))
3218 ret = opl3_init(s->iosynth, NULL, THIS_MODULE);
3220 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~8, 0);
3227 #ifdef CONFIG_SOUND_CMPCI_MIDI
3228 switch (s->iomidi) {
3245 ports = request_region(s->iomidi, 2, "mpu401");
3248 /* disable MPU-401 */
3249 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x04, 0);
3250 s->mpu_data.name = "cmpci mpu";
3251 s->mpu_data.io_base = s->iomidi;
3252 s->mpu_data.irq = -s->irq; // tell mpu401 to share irq
3253 if (probe_mpu401(&s->mpu_data, ports)) {
3254 release_region(s->iomidi, 2);
3258 maskb(s->iobase + CODEC_CMI_LEGACY_CTRL + 3, ~0x60, reg_mask);
3259 /* enable MPU-401 */
3260 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x04);
3261 /* clear all previously received interrupt */
3262 for (timeout = 900000; timeout > 0; timeout--) {
3263 if ((inb(s->iomidi + 1) && 0x80) == 0)
3268 if (!probe_mpu401(&s->mpu_data, ports)) {
3269 release_region(s->iomidi, 2);
3271 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0, 0x04);
3273 attach_mpu401(&s->mpu_data, THIS_MODULE);
3274 s->midi_devc = s->mpu_data.slots[1];
3278 /* disable joystick port */
3279 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x02, 0);
3281 cm_create_gameport(s, 0x200);
3283 /* store it in the driver field */
3284 pci_set_drvdata(pcidev, s);
3285 /* put it into driver list */
3286 list_add_tail(&s->devs, &devs);
3287 /* increment devindex */
3288 if (devindex < NR_DEVICE-1)
3293 unregister_sound_dsp(s->dev_audio);
3295 printk(KERN_ERR "cmpci: cannot register misc device\n");
3296 free_irq(s->irq, s);
3298 release_region(s->iobase, CM_EXTENT_CODEC);
3304 /* --------------------------------------------------------------------- */
3306 MODULE_AUTHOR("ChenLi Tien, cltien@cmedia.com.tw");
3307 MODULE_DESCRIPTION("CM8x38 Audio Driver");
3308 MODULE_LICENSE("GPL");
3310 static void __devexit cm_remove(struct pci_dev *dev)
3312 struct cm_state *s = pci_get_drvdata(dev);
3317 cm_free_gameport(s);
3319 #ifdef CONFIG_SOUND_CMPCI_FM
3322 maskb(s->iobase + CODEC_CMI_MISC_CTRL + 2, ~8, 0);
3325 #ifdef CONFIG_SOUND_CMPCI_MIDI
3327 unload_mpu401(&s->mpu_data);
3328 /* disable MPU-401 */
3329 maskb(s->iobase + CODEC_CMI_FUNCTRL1, ~0x04, 0);
3332 set_spdif_loop(s, 0);
3334 outb(0, s->iobase + CODEC_CMI_INT_HLDCLR + 2); /* disable ints */
3335 synchronize_irq(s->irq);
3336 outb(0, s->iobase + CODEC_CMI_FUNCTRL0 + 2); /* disable channels */
3337 free_irq(s->irq, s);
3340 wrmixer(s, DSP_MIX_DATARESETIDX, 0);
3342 release_region(s->iobase, CM_EXTENT_CODEC);
3343 unregister_sound_dsp(s->dev_audio);
3344 unregister_sound_mixer(s->dev_mixer);
3346 pci_set_drvdata(dev, NULL);
3349 static struct pci_device_id id_table[] __devinitdata = {
3350 { PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738B, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
3351 { PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
3352 { PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
3353 { PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B, PCI_ANY_ID, PCI_ANY_ID, 0, 0 },
3357 MODULE_DEVICE_TABLE(pci, id_table);
3359 static struct pci_driver cm_driver = {
3361 .id_table = id_table,
3363 .remove = __devexit_p(cm_remove)
3366 static int __init init_cmpci(void)
3368 printk(KERN_INFO "cmpci: version $Revision: 6.82 $ time " __TIME__ " " __DATE__ "\n");
3369 return pci_register_driver(&cm_driver);
3372 static void __exit cleanup_cmpci(void)
3374 printk(KERN_INFO "cmpci: unloading\n");
3375 pci_unregister_driver(&cm_driver);
3378 module_init(init_cmpci);
3379 module_exit(cleanup_cmpci);