2 * ite8172.c -- ITE IT8172G Sound Driver.
4 * Copyright 2001 MontaVista Software Inc.
5 * Author: MontaVista Software, Inc.
6 * stevel@mvista.com or source@mvista.com
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
16 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
19 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 675 Mass Ave, Cambridge, MA 02139, USA.
29 * Module command line parameters:
32 * /dev/dsp standard OSS /dev/dsp device
33 * /dev/mixer standard OSS /dev/mixer device
37 * 1. Much of the OSS buffer allocation, ioctl's, and mmap'ing are
38 * taken, slightly modified or not at all, from the ES1371 driver,
39 * so refer to the credits in es1371.c for those. The rest of the
40 * code (probe, open, read, write, the ISR, etc.) is new.
41 * 2. The following support is untested:
42 * * Memory mapping the audio buffers, and the ioctl controls that go
46 * 3. The following is not supported:
47 * * legacy audio mode.
48 * 4. Support for volume button interrupts is implemented but doesn't
52 * 02.08.2001 Initial release
53 * 06.22.2001 Added I2S support
55 #include <linux/version.h>
56 #include <linux/module.h>
57 #include <linux/string.h>
58 #include <linux/ioport.h>
59 #include <linux/sched.h>
60 #include <linux/delay.h>
61 #include <linux/sound.h>
62 #include <linux/slab.h>
63 #include <linux/soundcard.h>
64 #include <linux/pci.h>
65 #include <linux/init.h>
66 #include <linux/poll.h>
67 #include <linux/bitops.h>
68 #include <linux/proc_fs.h>
69 #include <linux/spinlock.h>
70 #include <linux/smp_lock.h>
71 #include <linux/ac97_codec.h>
72 #include <linux/wrapper.h>
75 #include <asm/uaccess.h>
76 #include <asm/hardirq.h>
77 #include <asm/it8172/it8172.h>
79 /* --------------------------------------------------------------------- */
81 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
83 #undef IT8172_VERBOSE_DEBUG
86 #define IT8172_MODULE_NAME "IT8172 audio"
87 #define PFX IT8172_MODULE_NAME
90 #define dbg(format, arg...) printk(KERN_DEBUG PFX ": " format "\n" , ## arg)
92 #define dbg(format, arg...) do {} while (0)
94 #define err(format, arg...) printk(KERN_ERR PFX ": " format "\n" , ## arg)
95 #define info(format, arg...) printk(KERN_INFO PFX ": " format "\n" , ## arg)
96 #define warn(format, arg...) printk(KERN_WARNING PFX ": " format "\n" , ## arg)
99 static const unsigned sample_shift[] = { 0, 1, 1, 2 };
103 * Audio Controller register bit definitions follow. See
104 * include/asm/it8172/it8172.h for register offsets.
107 /* PCM Out Volume Reg */
108 #define PCMOV_PCMOM (1<<15) /* PCM Out Mute default 1: mute */
109 #define PCMOV_PCMRCG_BIT 8 /* PCM Right channel Gain */
110 #define PCMOV_PCMRCG_MASK (0x1f<<PCMOV_PCMRCG_BIT)
111 #define PCMOV_PCMLCG_BIT 0 /* PCM Left channel gain */
112 #define PCMOV_PCMLCG_MASK 0x1f
114 /* FM Out Volume Reg */
115 #define FMOV_FMOM (1<<15) /* FM Out Mute default 1: mute */
116 #define FMOV_FMRCG_BIT 8 /* FM Right channel Gain */
117 #define FMOV_FMRCG_MASK (0x1f<<FMOV_FMRCG_BIT)
118 #define FMOV_FMLCG_BIT 0 /* FM Left channel gain */
119 #define FMOV_FMLCG_MASK 0x1f
121 /* I2S Out Volume Reg */
122 #define I2SV_I2SOM (1<<15) /* I2S Out Mute default 1: mute */
123 #define I2SV_I2SRCG_BIT 8 /* I2S Right channel Gain */
124 #define I2SV_I2SRCG_MASK (0x1f<<I2SV_I2SRCG_BIT)
125 #define I2SV_I2SLCG_BIT 0 /* I2S Left channel gain */
126 #define I2SV_I2SLCG_MASK 0x1f
128 /* Digital Recording Source Select Reg */
130 #define DRSS_MASK 0x07
131 #define DRSS_AC97_PRIM 0
135 #define DRSS_AC97_SEC 4
137 /* Playback/Capture Channel Control Registers */
138 #define CC_SM (1<<15) /* Stereo, Mone 0: mono 1: stereo */
139 #define CC_DF (1<<14) /* Data Format 0: 8 bit 1: 16 bit */
140 #define CC_FMT_BIT 14
141 #define CC_FMT_MASK (0x03<<CC_FMT_BIT)
142 #define CC_CF_BIT 12 /* Channel format (Playback only) */
143 #define CC_CF_MASK (0x03<<CC_CF_BIT)
145 #define CC_CF_4 (1<<CC_CF_BIT)
146 #define CC_CF_6 (2<<CC_CF_BIT)
147 #define CC_SR_BIT 8 /* sample Rate */
148 #define CC_SR_MASK (0x0f<<CC_SR_BIT)
150 #define CC_SR_8000 (1<<CC_SR_BIT)
151 #define CC_SR_9600 (2<<CC_SR_BIT)
152 #define CC_SR_11025 (3<<CC_SR_BIT)
153 #define CC_SR_16000 (4<<CC_SR_BIT)
154 #define CC_SR_19200 (5<<CC_SR_BIT)
155 #define CC_SR_22050 (6<<CC_SR_BIT)
156 #define CC_SR_32000 (7<<CC_SR_BIT)
157 #define CC_SR_38400 (8<<CC_SR_BIT)
158 #define CC_SR_44100 (9<<CC_SR_BIT)
159 #define CC_SR_48000 (10<<CC_SR_BIT)
160 #define CC_CSP (1<<7) /* Channel stop
161 * 0: End of Current buffer
162 * 1: Immediately stop when rec stop */
163 #define CC_CP (1<<6) /* Channel pause 0: normal, 1: pause */
164 #define CC_CA (1<<5) /* Channel Action 0: Stop , 1: start */
165 #define CC_CB2L (1<<2) /* Cur. buf. 2 xfr is last 0: No, 1: Yes */
166 #define CC_CB1L (1<<1) /* Cur. buf. 1 xfr is last 0: No, 1: Yes */
167 #define CC_DE 1 /* DFC/DFIFO Data Empty 1: empty, 0: not empty
171 /* Codec Control Reg */
172 #define CODECC_GME (1<<9) /* AC97 GPIO Mode enable */
173 #define CODECC_ATM (1<<8) /* AC97 ATE test mode 0: test 1: normal */
174 #define CODECC_WR (1<<6) /* AC97 Warn reset 1: warm reset , 0: Normal */
175 #define CODECC_CR (1<<5) /* AC97 Cold reset 1: Cold reset , 0: Normal */
178 /* I2S Control Reg */
179 #define I2SMC_SR_BIT 6 /* I2S Sampling rate
180 * 00: 48KHz, 01: 44.1 KHz, 10: 32 32 KHz */
181 #define I2SMC_SR_MASK (0x03<<I2SMC_SR_BIT)
182 #define I2SMC_SR_48000 0
183 #define I2SMC_SR_44100 (1<<I2SMC_SR_BIT)
184 #define I2SMC_SR_32000 (2<<I2SMC_SR_BIT)
185 #define I2SMC_SRSS (1<<5) /* Sample Rate Source Select 1:S/W, 0: H/W */
186 #define I2SMC_I2SF_BIT 0 /* I2S Format */
187 #define I2SMC_I2SF_MASK 0x03
188 #define I2SMC_I2SF_DAC 0
189 #define I2SMC_I2SF_ADC 2
190 #define I2SMC_I2SF_I2S 3
193 /* Volume up, Down, Mute */
194 #define VS_VMP (1<<2) /* Volume mute 1: pushed, 0: not */
195 #define VS_VDP (1<<1) /* Volume Down 1: pushed, 0: not */
196 #define VS_VUP 1 /* Volime Up 1: pushed, 0: not */
198 /* SRC, Mixer test control/DFC status reg */
199 #define SRCS_DPUSC (1<<5) /* DFC Playback underrun Status/clear */
200 #define SRCS_DCOSC (1<<4) /* DFC Capture Overrun Status/clear */
201 #define SRCS_SIS (1<<3) /* SRC input select 1: Mixer, 0: Codec I/F */
202 #define SRCS_CDIS_BIT 0 /* Codec Data Input Select */
203 #define SRCS_CDIS_MASK 0x07
204 #define SRCS_CDIS_MIXER 0
205 #define SRCS_CDIS_PCM 1
206 #define SRCS_CDIS_I2S 2
207 #define SRCS_CDIS_FM 3
208 #define SRCS_CDIS_DFC 4
211 /* Codec Index Reg command Port */
212 #define CIRCP_CID_BIT 10
213 #define CIRCP_CID_MASK (0x03<<CIRCP_CID_BIT)
214 #define CIRCP_CPS (1<<9) /* Command Port Status 0: ready, 1: busy */
215 #define CIRCP_DPVF (1<<8) /* Data Port Valid Flag 0: invalis, 1: valid */
216 #define CIRCP_RWC (1<<7) /* Read/write command */
217 #define CIRCP_CIA_BIT 0
218 #define CIRCP_CIA_MASK 0x007F /* Codec Index Address */
220 /* Test Mode Control/Test group Select Control */
222 /* General Control Reg */
223 #define GC_VDC_BIT 6 /* Volume Division Control */
224 #define GC_VDC_MASK (0x03<<GC_VDC_BIT)
225 #define GC_VDC_NONE 0
226 #define GC_VDC_DIV2 (1<<GC_VDC_BIT)
227 #define GC_VDC_DIV4 (2<<GC_VDC_BIT)
228 #define GC_SOE (1<<2) /* S/PDIF Output enable */
229 #define GC_SWR 1 /* Software warn reset */
231 /* Interrupt mask Control Reg */
232 #define IMC_VCIM (1<<6) /* Volume CNTL interrupt mask */
233 #define IMC_CCIM (1<<1) /* Capture Chan. iterrupt mask */
234 #define IMC_PCIM 1 /* Playback Chan. interrupt mask */
236 /* Interrupt status/clear reg */
237 #define ISC_VCI (1<<6) /* Volume CNTL interrupt 1: clears */
238 #define ISC_CCI (1<<1) /* Capture Chan. interrupt 1: clears */
239 #define ISC_PCI 1 /* Playback Chan. interrupt 1: clears */
242 #define POLL_COUNT 0x5000
245 /* --------------------------------------------------------------------- */
248 * Define DIGITAL1 as the I2S channel, since it is not listed in
251 #define SOUND_MIXER_I2S SOUND_MIXER_DIGITAL1
252 #define SOUND_MASK_I2S SOUND_MASK_DIGITAL1
253 #define SOUND_MIXER_READ_I2S MIXER_READ(SOUND_MIXER_I2S)
254 #define SOUND_MIXER_WRITE_I2S MIXER_WRITE(SOUND_MIXER_I2S)
256 /* --------------------------------------------------------------------- */
258 struct it8172_state {
259 /* list of it8172 devices */
260 struct list_head devs;
262 /* the corresponding pci_dev structure */
265 /* soundcore stuff */
268 /* hardware resources */
275 u8 rev; /* the chip revision */
278 int spdif_volume; /* S/PDIF output is enabled if != -1 */
279 int i2s_volume; /* current I2S out volume, in OSS format */
280 int i2s_recording;/* 1 = recording from I2S, 0 = not */
283 /* debug /proc entry */
284 struct proc_dir_entry *ps;
285 struct proc_dir_entry *ac97_ps;
286 #endif /* IT8172_DEBUG */
288 struct ac97_codec codec;
290 unsigned short pcc, capcc;
291 unsigned dacrate, adcrate;
294 struct semaphore open_sem;
296 wait_queue_head_t open_wait;
308 unsigned total_bytes;
309 unsigned error; /* over/underrun */
310 wait_queue_head_t wait;
311 /* redundant, but makes calculations easier */
314 unsigned fragsamples;
319 unsigned ossfragshift;
321 unsigned subdivision;
325 /* --------------------------------------------------------------------- */
327 static LIST_HEAD(devs);
329 /* --------------------------------------------------------------------- */
331 static inline unsigned ld2(unsigned int x)
356 /* --------------------------------------------------------------------- */
358 static void it8172_delay(int msec)
366 tmo = jiffies + (msec*HZ)/1000;
368 tmo2 = tmo - jiffies;
371 schedule_timeout(tmo2);
376 static unsigned short
377 get_compat_rate(unsigned* rate)
379 unsigned rate_out = *rate;
382 if (rate_out >= 46050) {
383 sr = CC_SR_48000; rate_out = 48000;
384 } else if (rate_out >= 41250) {
385 sr = CC_SR_44100; rate_out = 44100;
386 } else if (rate_out >= 35200) {
387 sr = CC_SR_38400; rate_out = 38400;
388 } else if (rate_out >= 27025) {
389 sr = CC_SR_32000; rate_out = 32000;
390 } else if (rate_out >= 20625) {
391 sr = CC_SR_22050; rate_out = 22050;
392 } else if (rate_out >= 17600) {
393 sr = CC_SR_19200; rate_out = 19200;
394 } else if (rate_out >= 13513) {
395 sr = CC_SR_16000; rate_out = 16000;
396 } else if (rate_out >= 10313) {
397 sr = CC_SR_11025; rate_out = 11025;
398 } else if (rate_out >= 8800) {
399 sr = CC_SR_9600; rate_out = 9600;
400 } else if (rate_out >= 6750) {
401 sr = CC_SR_8000; rate_out = 8000;
403 sr = CC_SR_5500; rate_out = 5500;
410 static void set_adc_rate(struct it8172_state *s, unsigned rate)
415 sr = get_compat_rate(&rate);
417 spin_lock_irqsave(&s->lock, flags);
418 s->capcc &= ~CC_SR_MASK;
420 outw(s->capcc, s->io+IT_AC_CAPCC);
421 spin_unlock_irqrestore(&s->lock, flags);
427 static void set_dac_rate(struct it8172_state *s, unsigned rate)
432 sr = get_compat_rate(&rate);
434 spin_lock_irqsave(&s->lock, flags);
435 s->pcc &= ~CC_SR_MASK;
437 outw(s->pcc, s->io+IT_AC_PCC);
438 spin_unlock_irqrestore(&s->lock, flags);
444 /* --------------------------------------------------------------------- */
446 static u16 rdcodec(struct ac97_codec *codec, u8 addr)
448 struct it8172_state *s = (struct it8172_state *)codec->private_data;
450 unsigned short circp, data;
453 spin_lock_irqsave(&s->lock, flags);
455 for (i = 0; i < POLL_COUNT; i++)
456 if (!(inw(s->io+IT_AC_CIRCP) & CIRCP_CPS))
459 err("rdcodec: codec ready poll expired!");
461 circp = addr & CIRCP_CIA_MASK;
462 circp |= (codec->id << CIRCP_CID_BIT);
463 circp |= CIRCP_RWC; // read command
464 outw(circp, s->io+IT_AC_CIRCP);
466 /* now wait for the data */
467 for (i = 0; i < POLL_COUNT; i++)
468 if (inw(s->io+IT_AC_CIRCP) & CIRCP_DPVF)
471 err("rdcodec: read poll expired!");
473 data = inw(s->io+IT_AC_CIRDP);
474 spin_unlock_irqrestore(&s->lock, flags);
480 static void wrcodec(struct ac97_codec *codec, u8 addr, u16 data)
482 struct it8172_state *s = (struct it8172_state *)codec->private_data;
484 unsigned short circp;
487 spin_lock_irqsave(&s->lock, flags);
489 for (i = 0; i < POLL_COUNT; i++)
490 if (!(inw(s->io+IT_AC_CIRCP) & CIRCP_CPS))
493 err("wrcodec: codec ready poll expired!");
495 circp = addr & CIRCP_CIA_MASK;
496 circp |= (codec->id << CIRCP_CID_BIT);
497 circp &= ~CIRCP_RWC; // write command
499 outw(data, s->io+IT_AC_CIRDP); // send data first
500 outw(circp, s->io+IT_AC_CIRCP);
502 spin_unlock_irqrestore(&s->lock, flags);
506 static void waitcodec(struct ac97_codec *codec)
510 /* codec_wait is used to wait for a ready state after
514 temp = rdcodec(codec, 0x26);
516 // If power down, power up
519 wrcodec(codec, 0x26, 0);
522 temp = rdcodec(codec, 0x26);
525 // Check if Codec REF,ANL,DAC,ADC ready***/
526 if ((temp & 0x3f0f) != 0x000f) {
527 err("codec reg 26 status (0x%x) not ready!!", temp);
533 /* --------------------------------------------------------------------- */
535 static inline void stop_adc(struct it8172_state *s)
537 struct dmabuf* db = &s->dma_adc;
544 spin_lock_irqsave(&s->lock, flags);
546 s->capcc &= ~(CC_CA | CC_CP | CC_CB2L | CC_CB1L);
548 outw(s->capcc, s->io+IT_AC_CAPCC);
550 // disable capture interrupt
551 imc = inb(s->io+IT_AC_IMC);
552 outb(imc | IMC_CCIM, s->io+IT_AC_IMC);
556 spin_unlock_irqrestore(&s->lock, flags);
559 static inline void stop_dac(struct it8172_state *s)
561 struct dmabuf* db = &s->dma_dac;
568 spin_lock_irqsave(&s->lock, flags);
570 s->pcc &= ~(CC_CA | CC_CP | CC_CB2L | CC_CB1L);
572 outw(s->pcc, s->io+IT_AC_PCC);
574 // disable playback interrupt
575 imc = inb(s->io+IT_AC_IMC);
576 outb(imc | IMC_PCIM, s->io+IT_AC_IMC);
580 spin_unlock_irqrestore(&s->lock, flags);
583 static void start_dac(struct it8172_state *s)
585 struct dmabuf* db = &s->dma_dac;
588 unsigned long buf1, buf2;
593 spin_lock_irqsave(&s->lock, flags);
595 // reset Buffer 1 and 2 pointers to nextOut and nextOut+fragsize
596 buf1 = virt_to_bus(db->nextOut);
597 buf2 = buf1 + db->fragsize;
598 if (buf2 >= db->dmaaddr + db->dmasize)
601 outl(buf1, s->io+IT_AC_PCB1STA);
602 outl(buf2, s->io+IT_AC_PCB2STA);
603 db->curBufPtr = IT_AC_PCB1STA;
605 // enable playback interrupt
606 imc = inb(s->io+IT_AC_IMC);
607 outb(imc & ~IMC_PCIM, s->io+IT_AC_IMC);
609 s->pcc &= ~(CC_CSP | CC_CP | CC_CB2L | CC_CB1L);
611 outw(s->pcc, s->io+IT_AC_PCC);
615 spin_unlock_irqrestore(&s->lock, flags);
618 static void start_adc(struct it8172_state *s)
620 struct dmabuf* db = &s->dma_adc;
623 unsigned long buf1, buf2;
628 spin_lock_irqsave(&s->lock, flags);
630 // reset Buffer 1 and 2 pointers to nextIn and nextIn+fragsize
631 buf1 = virt_to_bus(db->nextIn);
632 buf2 = buf1 + db->fragsize;
633 if (buf2 >= db->dmaaddr + db->dmasize)
636 outl(buf1, s->io+IT_AC_CAPB1STA);
637 outl(buf2, s->io+IT_AC_CAPB2STA);
638 db->curBufPtr = IT_AC_CAPB1STA;
640 // enable capture interrupt
641 imc = inb(s->io+IT_AC_IMC);
642 outb(imc & ~IMC_CCIM, s->io+IT_AC_IMC);
644 s->capcc &= ~(CC_CSP | CC_CP | CC_CB2L | CC_CB1L);
646 outw(s->capcc, s->io+IT_AC_CAPCC);
650 spin_unlock_irqrestore(&s->lock, flags);
653 /* --------------------------------------------------------------------- */
655 #define DMABUF_DEFAULTORDER (17-PAGE_SHIFT)
656 #define DMABUF_MINORDER 1
658 static inline void dealloc_dmabuf(struct it8172_state *s, struct dmabuf *db)
660 struct page *page, *pend;
663 /* undo marking the pages as reserved */
664 pend = virt_to_page(db->rawbuf +
665 (PAGE_SIZE << db->buforder) - 1);
666 for (page = virt_to_page(db->rawbuf); page <= pend; page++)
667 mem_map_unreserve(page);
668 pci_free_consistent(s->dev, PAGE_SIZE << db->buforder,
669 db->rawbuf, db->dmaaddr);
671 db->rawbuf = db->nextIn = db->nextOut = NULL;
672 db->mapped = db->ready = 0;
675 static int prog_dmabuf(struct it8172_state *s, struct dmabuf *db,
676 unsigned rate, unsigned fmt, unsigned reg)
681 struct page *page, *pend;
684 db->ready = db->mapped = 0;
685 for (order = DMABUF_DEFAULTORDER;
686 order >= DMABUF_MINORDER; order--)
688 pci_alloc_consistent(s->dev,
694 db->buforder = order;
695 /* now mark the pages as reserved;
696 otherwise remap_page_range doesn't do what we want */
697 pend = virt_to_page(db->rawbuf +
698 (PAGE_SIZE << db->buforder) - 1);
699 for (page = virt_to_page(db->rawbuf); page <= pend; page++)
700 mem_map_reserve(page);
704 db->nextIn = db->nextOut = db->rawbuf;
706 bytepersec = rate << sample_shift[fmt];
707 bufs = PAGE_SIZE << db->buforder;
708 if (db->ossfragshift) {
709 if ((1000 << db->ossfragshift) < bytepersec)
710 db->fragshift = ld2(bytepersec/1000);
712 db->fragshift = db->ossfragshift;
714 db->fragshift = ld2(bytepersec/100/(db->subdivision ?
715 db->subdivision : 1));
716 if (db->fragshift < 3)
719 db->numfrag = bufs >> db->fragshift;
720 while (db->numfrag < 4 && db->fragshift > 3) {
722 db->numfrag = bufs >> db->fragshift;
724 db->fragsize = 1 << db->fragshift;
725 if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
726 db->numfrag = db->ossmaxfrags;
727 db->fragsamples = db->fragsize >> sample_shift[fmt];
728 db->dmasize = db->numfrag << db->fragshift;
729 memset(db->rawbuf, (fmt & (CC_DF>>CC_FMT_BIT)) ? 0 : 0x80, bufs);
731 #ifdef IT8172_VERBOSE_DEBUG
732 dbg("rate=%d, fragsize=%d, numfrag=%d, dmasize=%d",
733 rate, db->fragsize, db->numfrag, db->dmasize);
736 // set data length register
737 outw(db->fragsize, s->io+reg+2);
743 static inline int prog_dmabuf_adc(struct it8172_state *s)
746 return prog_dmabuf(s, &s->dma_adc, s->adcrate,
747 (s->capcc & CC_FMT_MASK) >> CC_FMT_BIT,
751 static inline int prog_dmabuf_dac(struct it8172_state *s)
754 return prog_dmabuf(s, &s->dma_dac, s->dacrate,
755 (s->pcc & CC_FMT_MASK) >> CC_FMT_BIT,
760 /* hold spinlock for the following! */
762 static void it8172_interrupt(int irq, void *dev_id, struct pt_regs *regs)
764 struct it8172_state *s = (struct it8172_state *)dev_id;
765 struct dmabuf* dac = &s->dma_dac;
766 struct dmabuf* adc = &s->dma_adc;
767 unsigned char isc, vs;
768 unsigned short vol, mute;
769 unsigned long newptr;
773 isc = inb(s->io+IT_AC_ISC);
775 /* fastpath out, to ease interrupt sharing */
776 if (!(isc & (ISC_VCI | ISC_CCI | ISC_PCI))) {
777 spin_unlock(&s->lock);
781 /* clear audio interrupts first */
782 outb(isc | ISC_VCI | ISC_CCI | ISC_PCI, s->io+IT_AC_ISC);
784 /* handle volume button events (ignore if S/PDIF enabled) */
785 if ((isc & ISC_VCI) && s->spdif_volume == -1) {
786 vs = inb(s->io+IT_AC_VS);
787 outb(0, s->io+IT_AC_VS);
788 vol = inw(s->io+IT_AC_PCMOV);
789 mute = vol & PCMOV_PCMOM;
790 vol &= PCMOV_PCMLCG_MASK;
791 if ((vs & VS_VUP) && vol > 0)
793 if ((vs & VS_VDP) && vol < 0x1f)
795 vol |= (vol << PCMOV_PCMRCG_BIT);
797 vol |= (mute ^ PCMOV_PCMOM);
798 outw(vol, s->io+IT_AC_PCMOV);
801 /* update capture pointers */
803 if (adc->count > adc->dmasize - adc->fragsize) {
804 // Overrun. Stop ADC and log the error
809 newptr = virt_to_bus(adc->nextIn) + 2*adc->fragsize;
810 if (newptr >= adc->dmaaddr + adc->dmasize)
811 newptr -= adc->dmasize;
813 outl(newptr, s->io+adc->curBufPtr);
814 adc->curBufPtr = (adc->curBufPtr == IT_AC_CAPB1STA) ?
815 IT_AC_CAPB2STA : IT_AC_CAPB1STA;
817 adc->nextIn += adc->fragsize;
818 if (adc->nextIn >= adc->rawbuf + adc->dmasize)
819 adc->nextIn -= adc->dmasize;
821 adc->count += adc->fragsize;
822 adc->total_bytes += adc->fragsize;
824 /* wake up anybody listening */
825 if (waitqueue_active(&adc->wait))
826 wake_up_interruptible(&adc->wait);
830 /* update playback pointers */
832 newptr = virt_to_bus(dac->nextOut) + 2*dac->fragsize;
833 if (newptr >= dac->dmaaddr + dac->dmasize)
834 newptr -= dac->dmasize;
836 outl(newptr, s->io+dac->curBufPtr);
837 dac->curBufPtr = (dac->curBufPtr == IT_AC_PCB1STA) ?
838 IT_AC_PCB2STA : IT_AC_PCB1STA;
840 dac->nextOut += dac->fragsize;
841 if (dac->nextOut >= dac->rawbuf + dac->dmasize)
842 dac->nextOut -= dac->dmasize;
844 dac->count -= dac->fragsize;
845 dac->total_bytes += dac->fragsize;
847 /* wake up anybody listening */
848 if (waitqueue_active(&dac->wait))
849 wake_up_interruptible(&dac->wait);
855 spin_unlock(&s->lock);
858 /* --------------------------------------------------------------------- */
860 static loff_t it8172_llseek(struct file *file, loff_t offset, int origin)
866 static int it8172_open_mixdev(struct inode *inode, struct file *file)
868 int minor = MINOR(inode->i_rdev);
869 struct list_head *list;
870 struct it8172_state *s;
872 for (list = devs.next; ; list = list->next) {
875 s = list_entry(list, struct it8172_state, devs);
876 if (s->codec.dev_mixer == minor)
879 file->private_data = s;
883 static int it8172_release_mixdev(struct inode *inode, struct file *file)
890 cvt_ossvol(unsigned int gain)
900 ret = (100 - gain + 32) / 4;
901 ret = ret > 31 ? 31 : ret;
906 static int mixdev_ioctl(struct ac97_codec *codec, unsigned int cmd,
909 struct it8172_state *s = (struct it8172_state *)codec->private_data;
910 unsigned int left, right;
916 * When we are in S/PDIF mode, we want to disable any analog output so
917 * we filter the master/PCM channel volume ioctls.
919 * Also filter I2S channel, which AC'97 knows nothing about.
923 case SOUND_MIXER_WRITE_VOLUME:
924 // if not in S/PDIF mode, pass to AC'97
925 if (s->spdif_volume == -1)
928 case SOUND_MIXER_WRITE_PCM:
929 // if not in S/PDIF mode, pass to AC'97
930 if (s->spdif_volume == -1)
932 if (get_user(val, (int *)arg))
934 right = ((val >> 8) & 0xff);
940 s->spdif_volume = (right << 8) | left;
941 vol = cvt_ossvol(left);
942 vol |= (cvt_ossvol(right) << PCMOV_PCMRCG_BIT);
944 vol = PCMOV_PCMOM; // mute
945 spin_lock_irqsave(&s->lock, flags);
946 outw(vol, s->io+IT_AC_PCMOV);
947 spin_unlock_irqrestore(&s->lock, flags);
948 return put_user(s->spdif_volume, (int *)arg);
949 case SOUND_MIXER_READ_PCM:
950 // if not in S/PDIF mode, pass to AC'97
951 if (s->spdif_volume == -1)
953 return put_user(s->spdif_volume, (int *)arg);
954 case SOUND_MIXER_WRITE_I2S:
955 if (get_user(val, (int *)arg))
957 right = ((val >> 8) & 0xff);
963 s->i2s_volume = (right << 8) | left;
964 vol = cvt_ossvol(left);
965 vol |= (cvt_ossvol(right) << I2SV_I2SRCG_BIT);
967 vol = I2SV_I2SOM; // mute
968 outw(vol, s->io+IT_AC_I2SV);
969 return put_user(s->i2s_volume, (int *)arg);
970 case SOUND_MIXER_READ_I2S:
971 return put_user(s->i2s_volume, (int *)arg);
972 case SOUND_MIXER_WRITE_RECSRC:
973 if (get_user(val, (int *)arg))
975 if (val & SOUND_MASK_I2S) {
976 s->i2s_recording = 1;
977 outb(DRSS_I2S, s->io+IT_AC_DRSS);
980 s->i2s_recording = 0;
981 outb(DRSS_AC97_PRIM, s->io+IT_AC_DRSS);
982 // now let AC'97 select record source
985 case SOUND_MIXER_READ_RECSRC:
986 if (s->i2s_recording)
987 return put_user(SOUND_MASK_I2S, (int *)arg);
989 // let AC'97 report recording source
993 return codec->mixer_ioctl(codec, cmd, arg);
996 static int it8172_ioctl_mixdev(struct inode *inode, struct file *file,
997 unsigned int cmd, unsigned long arg)
999 struct it8172_state *s = (struct it8172_state *)file->private_data;
1000 struct ac97_codec *codec = &s->codec;
1002 return mixdev_ioctl(codec, cmd, arg);
1005 static /*const*/ struct file_operations it8172_mixer_fops = {
1007 llseek: it8172_llseek,
1008 ioctl: it8172_ioctl_mixdev,
1009 open: it8172_open_mixdev,
1010 release: it8172_release_mixdev,
1013 /* --------------------------------------------------------------------- */
1015 static int drain_dac(struct it8172_state *s, int nonblock)
1017 unsigned long flags;
1020 if (s->dma_dac.mapped || !s->dma_dac.ready || s->dma_dac.stopped)
1024 spin_lock_irqsave(&s->lock, flags);
1025 count = s->dma_dac.count;
1026 spin_unlock_irqrestore(&s->lock, flags);
1029 if (signal_pending(current))
1033 tmo = 1000 * count / s->dacrate;
1034 tmo >>= sample_shift[(s->pcc & CC_FMT_MASK) >> CC_FMT_BIT];
1037 if (signal_pending(current))
1038 return -ERESTARTSYS;
1042 /* --------------------------------------------------------------------- */
1046 * Copy audio data to/from user buffer from/to dma buffer, taking care
1047 * that we wrap when reading/writing the dma buffer. Returns actual byte
1048 * count written to or read from the dma buffer.
1050 static int copy_dmabuf_user(struct dmabuf *db, char* userbuf,
1051 int count, int to_user)
1053 char* bufptr = to_user ? db->nextOut : db->nextIn;
1054 char* bufend = db->rawbuf + db->dmasize;
1056 if (bufptr + count > bufend) {
1057 int partial = (int)(bufend - bufptr);
1059 if (copy_to_user(userbuf, bufptr, partial))
1061 if (copy_to_user(userbuf + partial, db->rawbuf,
1065 if (copy_from_user(bufptr, userbuf, partial))
1067 if (copy_from_user(db->rawbuf,
1074 if (copy_to_user(userbuf, bufptr, count))
1077 if (copy_from_user(bufptr, userbuf, count))
1086 static ssize_t it8172_read(struct file *file, char *buffer,
1087 size_t count, loff_t *ppos)
1089 struct it8172_state *s = (struct it8172_state *)file->private_data;
1090 struct dmabuf *db = &s->dma_adc;
1092 unsigned long flags;
1093 int cnt, remainder, avail;
1095 if (ppos != &file->f_pos)
1099 if (!access_ok(VERIFY_WRITE, buffer, count))
1104 // wait for samples in capture buffer
1106 spin_lock_irqsave(&s->lock, flags);
1110 spin_unlock_irqrestore(&s->lock, flags);
1112 if (file->f_flags & O_NONBLOCK) {
1117 interruptible_sleep_on(&db->wait);
1118 if (signal_pending(current)) {
1124 } while (avail <= 0);
1126 // copy from nextOut to user
1127 if ((cnt = copy_dmabuf_user(db, buffer, count > avail ?
1128 avail : count, 1)) < 0) {
1134 spin_lock_irqsave(&s->lock, flags);
1136 spin_unlock_irqrestore(&s->lock, flags);
1139 if (db->nextOut >= db->rawbuf + db->dmasize)
1140 db->nextOut -= db->dmasize;
1145 } // while (count > 0)
1148 * See if the dma buffer count after this read call is
1149 * aligned on a fragsize boundary. If not, read from
1150 * buffer until we reach a boundary, and let's hope this
1151 * is just the last remainder of an audio record. If not
1152 * it means the user is not reading in fragsize chunks, in
1153 * which case it's his/her fault that there are audio gaps
1156 spin_lock_irqsave(&s->lock, flags);
1157 remainder = db->count % db->fragsize;
1159 db->nextOut += remainder;
1160 if (db->nextOut >= db->rawbuf + db->dmasize)
1161 db->nextOut -= db->dmasize;
1162 db->count -= remainder;
1164 spin_unlock_irqrestore(&s->lock, flags);
1169 static ssize_t it8172_write(struct file *file, const char *buffer,
1170 size_t count, loff_t *ppos)
1172 struct it8172_state *s = (struct it8172_state *)file->private_data;
1173 struct dmabuf *db = &s->dma_dac;
1175 unsigned long flags;
1176 int cnt, remainder, avail;
1178 if (ppos != &file->f_pos)
1182 if (!access_ok(VERIFY_READ, buffer, count))
1187 // wait for space in playback buffer
1189 spin_lock_irqsave(&s->lock, flags);
1190 avail = db->dmasize - db->count;
1191 spin_unlock_irqrestore(&s->lock, flags);
1193 if (file->f_flags & O_NONBLOCK) {
1198 interruptible_sleep_on(&db->wait);
1199 if (signal_pending(current)) {
1205 } while (avail <= 0);
1208 if ((cnt = copy_dmabuf_user(db, (char*)buffer,
1210 avail : count, 0)) < 0) {
1216 spin_lock_irqsave(&s->lock, flags);
1220 spin_unlock_irqrestore(&s->lock, flags);
1223 if (db->nextIn >= db->rawbuf + db->dmasize)
1224 db->nextIn -= db->dmasize;
1229 } // while (count > 0)
1232 * See if the dma buffer count after this write call is
1233 * aligned on a fragsize boundary. If not, fill buffer
1234 * with silence to the next boundary, and let's hope this
1235 * is just the last remainder of an audio playback. If not
1236 * it means the user is not sending us fragsize chunks, in
1237 * which case it's his/her fault that there are audio gaps
1238 * in their playback.
1240 spin_lock_irqsave(&s->lock, flags);
1241 remainder = db->count % db->fragsize;
1243 int fill_cnt = db->fragsize - remainder;
1244 memset(db->nextIn, 0, fill_cnt);
1245 db->nextIn += fill_cnt;
1246 if (db->nextIn >= db->rawbuf + db->dmasize)
1247 db->nextIn -= db->dmasize;
1248 db->count += fill_cnt;
1250 spin_unlock_irqrestore(&s->lock, flags);
1255 /* No kernel lock - we have our own spinlock */
1256 static unsigned int it8172_poll(struct file *file,
1257 struct poll_table_struct *wait)
1259 struct it8172_state *s = (struct it8172_state *)file->private_data;
1260 unsigned long flags;
1261 unsigned int mask = 0;
1263 if (file->f_mode & FMODE_WRITE) {
1264 if (!s->dma_dac.ready)
1266 poll_wait(file, &s->dma_dac.wait, wait);
1268 if (file->f_mode & FMODE_READ) {
1269 if (!s->dma_adc.ready)
1271 poll_wait(file, &s->dma_adc.wait, wait);
1274 spin_lock_irqsave(&s->lock, flags);
1275 if (file->f_mode & FMODE_READ) {
1276 if (s->dma_adc.count >= (signed)s->dma_adc.fragsize)
1277 mask |= POLLIN | POLLRDNORM;
1279 if (file->f_mode & FMODE_WRITE) {
1280 if (s->dma_dac.mapped) {
1281 if (s->dma_dac.count >= (signed)s->dma_dac.fragsize)
1282 mask |= POLLOUT | POLLWRNORM;
1284 if ((signed)s->dma_dac.dmasize >=
1285 s->dma_dac.count + (signed)s->dma_dac.fragsize)
1286 mask |= POLLOUT | POLLWRNORM;
1289 spin_unlock_irqrestore(&s->lock, flags);
1293 static int it8172_mmap(struct file *file, struct vm_area_struct *vma)
1295 struct it8172_state *s = (struct it8172_state *)file->private_data;
1300 if (vma->vm_flags & VM_WRITE)
1302 else if (vma->vm_flags & VM_READ)
1308 if (vma->vm_pgoff != 0) {
1312 size = vma->vm_end - vma->vm_start;
1313 if (size > (PAGE_SIZE << db->buforder)) {
1317 if (remap_page_range(vma->vm_start, virt_to_phys(db->rawbuf),
1318 size, vma->vm_page_prot)) {
1328 #ifdef IT8172_VERBOSE_DEBUG
1329 static struct ioctl_str_t {
1333 {SNDCTL_DSP_RESET, "SNDCTL_DSP_RESET"},
1334 {SNDCTL_DSP_SYNC, "SNDCTL_DSP_SYNC"},
1335 {SNDCTL_DSP_SPEED, "SNDCTL_DSP_SPEED"},
1336 {SNDCTL_DSP_STEREO, "SNDCTL_DSP_STEREO"},
1337 {SNDCTL_DSP_GETBLKSIZE, "SNDCTL_DSP_GETBLKSIZE"},
1338 {SNDCTL_DSP_SAMPLESIZE, "SNDCTL_DSP_SAMPLESIZE"},
1339 {SNDCTL_DSP_CHANNELS, "SNDCTL_DSP_CHANNELS"},
1340 {SOUND_PCM_WRITE_CHANNELS, "SOUND_PCM_WRITE_CHANNELS"},
1341 {SOUND_PCM_WRITE_FILTER, "SOUND_PCM_WRITE_FILTER"},
1342 {SNDCTL_DSP_POST, "SNDCTL_DSP_POST"},
1343 {SNDCTL_DSP_SUBDIVIDE, "SNDCTL_DSP_SUBDIVIDE"},
1344 {SNDCTL_DSP_SETFRAGMENT, "SNDCTL_DSP_SETFRAGMENT"},
1345 {SNDCTL_DSP_GETFMTS, "SNDCTL_DSP_GETFMTS"},
1346 {SNDCTL_DSP_SETFMT, "SNDCTL_DSP_SETFMT"},
1347 {SNDCTL_DSP_GETOSPACE, "SNDCTL_DSP_GETOSPACE"},
1348 {SNDCTL_DSP_GETISPACE, "SNDCTL_DSP_GETISPACE"},
1349 {SNDCTL_DSP_NONBLOCK, "SNDCTL_DSP_NONBLOCK"},
1350 {SNDCTL_DSP_GETCAPS, "SNDCTL_DSP_GETCAPS"},
1351 {SNDCTL_DSP_GETTRIGGER, "SNDCTL_DSP_GETTRIGGER"},
1352 {SNDCTL_DSP_SETTRIGGER, "SNDCTL_DSP_SETTRIGGER"},
1353 {SNDCTL_DSP_GETIPTR, "SNDCTL_DSP_GETIPTR"},
1354 {SNDCTL_DSP_GETOPTR, "SNDCTL_DSP_GETOPTR"},
1355 {SNDCTL_DSP_MAPINBUF, "SNDCTL_DSP_MAPINBUF"},
1356 {SNDCTL_DSP_MAPOUTBUF, "SNDCTL_DSP_MAPOUTBUF"},
1357 {SNDCTL_DSP_SETSYNCRO, "SNDCTL_DSP_SETSYNCRO"},
1358 {SNDCTL_DSP_SETDUPLEX, "SNDCTL_DSP_SETDUPLEX"},
1359 {SNDCTL_DSP_GETODELAY, "SNDCTL_DSP_GETODELAY"},
1360 {SNDCTL_DSP_GETCHANNELMASK, "SNDCTL_DSP_GETCHANNELMASK"},
1361 {SNDCTL_DSP_BIND_CHANNEL, "SNDCTL_DSP_BIND_CHANNEL"},
1362 {OSS_GETVERSION, "OSS_GETVERSION"},
1363 {SOUND_PCM_READ_RATE, "SOUND_PCM_READ_RATE"},
1364 {SOUND_PCM_READ_CHANNELS, "SOUND_PCM_READ_CHANNELS"},
1365 {SOUND_PCM_READ_BITS, "SOUND_PCM_READ_BITS"},
1366 {SOUND_PCM_READ_FILTER, "SOUND_PCM_READ_FILTER"}
1370 static int it8172_ioctl(struct inode *inode, struct file *file,
1371 unsigned int cmd, unsigned long arg)
1373 struct it8172_state *s = (struct it8172_state *)file->private_data;
1374 unsigned long flags;
1375 audio_buf_info abinfo;
1378 int val, mapped, ret, diff;
1380 mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
1381 ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
1383 #ifdef IT8172_VERBOSE_DEBUG
1384 for (count=0; count<sizeof(ioctl_str)/sizeof(ioctl_str[0]); count++) {
1385 if (ioctl_str[count].cmd == cmd)
1388 if (count < sizeof(ioctl_str)/sizeof(ioctl_str[0]))
1389 dbg("ioctl %s, arg=0x%08x",
1390 ioctl_str[count].str, (unsigned int)arg);
1392 dbg("ioctl unknown, 0x%x", cmd);
1396 case OSS_GETVERSION:
1397 return put_user(SOUND_VERSION, (int *)arg);
1399 case SNDCTL_DSP_SYNC:
1400 if (file->f_mode & FMODE_WRITE)
1401 return drain_dac(s, file->f_flags & O_NONBLOCK);
1404 case SNDCTL_DSP_SETDUPLEX:
1407 case SNDCTL_DSP_GETCAPS:
1408 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME |
1409 DSP_CAP_TRIGGER | DSP_CAP_MMAP, (int *)arg);
1411 case SNDCTL_DSP_RESET:
1412 if (file->f_mode & FMODE_WRITE) {
1415 s->dma_dac.count = s->dma_dac.total_bytes = 0;
1416 s->dma_dac.nextIn = s->dma_dac.nextOut =
1419 if (file->f_mode & FMODE_READ) {
1422 s->dma_adc.count = s->dma_adc.total_bytes = 0;
1423 s->dma_adc.nextIn = s->dma_adc.nextOut =
1428 case SNDCTL_DSP_SPEED:
1429 if (get_user(val, (int *)arg))
1432 if (file->f_mode & FMODE_READ) {
1434 set_adc_rate(s, val);
1435 if ((ret = prog_dmabuf_adc(s)))
1438 if (file->f_mode & FMODE_WRITE) {
1440 set_dac_rate(s, val);
1441 if ((ret = prog_dmabuf_dac(s)))
1445 return put_user((file->f_mode & FMODE_READ) ?
1446 s->adcrate : s->dacrate, (int *)arg);
1448 case SNDCTL_DSP_STEREO:
1449 if (get_user(val, (int *)arg))
1451 if (file->f_mode & FMODE_READ) {
1457 outw(s->capcc, s->io+IT_AC_CAPCC);
1458 if ((ret = prog_dmabuf_adc(s)))
1461 if (file->f_mode & FMODE_WRITE) {
1467 outw(s->pcc, s->io+IT_AC_PCC);
1468 if ((ret = prog_dmabuf_dac(s)))
1473 case SNDCTL_DSP_CHANNELS:
1474 if (get_user(val, (int *)arg))
1477 if (file->f_mode & FMODE_READ) {
1485 outw(s->capcc, s->io+IT_AC_CAPCC);
1486 if ((ret = prog_dmabuf_adc(s)))
1489 if (file->f_mode & FMODE_WRITE) {
1499 // FIX! support multichannel???
1504 outw(s->pcc, s->io+IT_AC_PCC);
1505 if ((ret = prog_dmabuf_dac(s)))
1509 return put_user(val, (int *)arg);
1511 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
1512 return put_user(AFMT_S16_LE|AFMT_U8, (int *)arg);
1514 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
1515 if (get_user(val, (int *)arg))
1517 if (val != AFMT_QUERY) {
1518 if (file->f_mode & FMODE_READ) {
1520 if (val == AFMT_S16_LE)
1526 outw(s->capcc, s->io+IT_AC_CAPCC);
1527 if ((ret = prog_dmabuf_adc(s)))
1530 if (file->f_mode & FMODE_WRITE) {
1532 if (val == AFMT_S16_LE)
1538 outw(s->pcc, s->io+IT_AC_PCC);
1539 if ((ret = prog_dmabuf_dac(s)))
1543 if (file->f_mode & FMODE_READ)
1544 val = (s->capcc & CC_DF) ?
1545 AFMT_S16_LE : AFMT_U8;
1547 val = (s->pcc & CC_DF) ?
1548 AFMT_S16_LE : AFMT_U8;
1550 return put_user(val, (int *)arg);
1552 case SNDCTL_DSP_POST:
1555 case SNDCTL_DSP_GETTRIGGER:
1557 spin_lock_irqsave(&s->lock, flags);
1558 if (file->f_mode & FMODE_READ && !s->dma_adc.stopped)
1559 val |= PCM_ENABLE_INPUT;
1560 if (file->f_mode & FMODE_WRITE && !s->dma_dac.stopped)
1561 val |= PCM_ENABLE_OUTPUT;
1562 spin_unlock_irqrestore(&s->lock, flags);
1563 return put_user(val, (int *)arg);
1565 case SNDCTL_DSP_SETTRIGGER:
1566 if (get_user(val, (int *)arg))
1568 if (file->f_mode & FMODE_READ) {
1569 if (val & PCM_ENABLE_INPUT)
1574 if (file->f_mode & FMODE_WRITE) {
1575 if (val & PCM_ENABLE_OUTPUT)
1582 case SNDCTL_DSP_GETOSPACE:
1583 if (!(file->f_mode & FMODE_WRITE))
1585 abinfo.fragsize = s->dma_dac.fragsize;
1586 spin_lock_irqsave(&s->lock, flags);
1587 count = s->dma_dac.count;
1588 if (!s->dma_dac.stopped)
1589 count -= (s->dma_dac.fragsize -
1590 inw(s->io+IT_AC_PCDL));
1591 spin_unlock_irqrestore(&s->lock, flags);
1594 abinfo.bytes = s->dma_dac.dmasize - count;
1595 abinfo.fragstotal = s->dma_dac.numfrag;
1596 abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
1597 return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ?
1600 case SNDCTL_DSP_GETISPACE:
1601 if (!(file->f_mode & FMODE_READ))
1603 abinfo.fragsize = s->dma_adc.fragsize;
1604 spin_lock_irqsave(&s->lock, flags);
1605 count = s->dma_adc.count;
1606 if (!s->dma_adc.stopped)
1607 count += (s->dma_adc.fragsize -
1608 inw(s->io+IT_AC_CAPCDL));
1609 spin_unlock_irqrestore(&s->lock, flags);
1612 abinfo.bytes = count;
1613 abinfo.fragstotal = s->dma_adc.numfrag;
1614 abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;
1615 return copy_to_user((void *)arg, &abinfo, sizeof(abinfo)) ?
1618 case SNDCTL_DSP_NONBLOCK:
1619 file->f_flags |= O_NONBLOCK;
1622 case SNDCTL_DSP_GETODELAY:
1623 if (!(file->f_mode & FMODE_WRITE))
1625 spin_lock_irqsave(&s->lock, flags);
1626 count = s->dma_dac.count;
1627 if (!s->dma_dac.stopped)
1628 count -= (s->dma_dac.fragsize -
1629 inw(s->io+IT_AC_PCDL));
1630 spin_unlock_irqrestore(&s->lock, flags);
1633 return put_user(count, (int *)arg);
1635 case SNDCTL_DSP_GETIPTR:
1636 if (!(file->f_mode & FMODE_READ))
1638 spin_lock_irqsave(&s->lock, flags);
1639 cinfo.bytes = s->dma_adc.total_bytes;
1640 count = s->dma_adc.count;
1641 if (!s->dma_adc.stopped) {
1642 diff = s->dma_adc.fragsize - inw(s->io+IT_AC_CAPCDL);
1644 cinfo.bytes += diff;
1645 cinfo.ptr = inl(s->io+s->dma_adc.curBufPtr) -
1648 cinfo.ptr = virt_to_bus(s->dma_adc.nextIn) -
1650 if (s->dma_adc.mapped)
1651 s->dma_adc.count &= s->dma_adc.fragsize-1;
1652 spin_unlock_irqrestore(&s->lock, flags);
1655 cinfo.blocks = count >> s->dma_adc.fragshift;
1656 return copy_to_user((void *)arg, &cinfo, sizeof(cinfo));
1658 case SNDCTL_DSP_GETOPTR:
1659 if (!(file->f_mode & FMODE_READ))
1661 spin_lock_irqsave(&s->lock, flags);
1662 cinfo.bytes = s->dma_dac.total_bytes;
1663 count = s->dma_dac.count;
1664 if (!s->dma_dac.stopped) {
1665 diff = s->dma_dac.fragsize - inw(s->io+IT_AC_CAPCDL);
1667 cinfo.bytes += diff;
1668 cinfo.ptr = inl(s->io+s->dma_dac.curBufPtr) -
1671 cinfo.ptr = virt_to_bus(s->dma_dac.nextOut) -
1673 if (s->dma_dac.mapped)
1674 s->dma_dac.count &= s->dma_dac.fragsize-1;
1675 spin_unlock_irqrestore(&s->lock, flags);
1678 cinfo.blocks = count >> s->dma_dac.fragshift;
1679 return copy_to_user((void *)arg, &cinfo, sizeof(cinfo));
1681 case SNDCTL_DSP_GETBLKSIZE:
1682 if (file->f_mode & FMODE_WRITE)
1683 return put_user(s->dma_dac.fragsize, (int *)arg);
1685 return put_user(s->dma_adc.fragsize, (int *)arg);
1687 case SNDCTL_DSP_SETFRAGMENT:
1688 if (get_user(val, (int *)arg))
1690 if (file->f_mode & FMODE_READ) {
1692 s->dma_adc.ossfragshift = val & 0xffff;
1693 s->dma_adc.ossmaxfrags = (val >> 16) & 0xffff;
1694 if (s->dma_adc.ossfragshift < 4)
1695 s->dma_adc.ossfragshift = 4;
1696 if (s->dma_adc.ossfragshift > 15)
1697 s->dma_adc.ossfragshift = 15;
1698 if (s->dma_adc.ossmaxfrags < 4)
1699 s->dma_adc.ossmaxfrags = 4;
1700 if ((ret = prog_dmabuf_adc(s)))
1703 if (file->f_mode & FMODE_WRITE) {
1705 s->dma_dac.ossfragshift = val & 0xffff;
1706 s->dma_dac.ossmaxfrags = (val >> 16) & 0xffff;
1707 if (s->dma_dac.ossfragshift < 4)
1708 s->dma_dac.ossfragshift = 4;
1709 if (s->dma_dac.ossfragshift > 15)
1710 s->dma_dac.ossfragshift = 15;
1711 if (s->dma_dac.ossmaxfrags < 4)
1712 s->dma_dac.ossmaxfrags = 4;
1713 if ((ret = prog_dmabuf_dac(s)))
1718 case SNDCTL_DSP_SUBDIVIDE:
1719 if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
1720 (file->f_mode & FMODE_WRITE && s->dma_dac.subdivision))
1722 if (get_user(val, (int *)arg))
1724 if (val != 1 && val != 2 && val != 4)
1726 if (file->f_mode & FMODE_READ) {
1728 s->dma_adc.subdivision = val;
1729 if ((ret = prog_dmabuf_adc(s)))
1732 if (file->f_mode & FMODE_WRITE) {
1734 s->dma_dac.subdivision = val;
1735 if ((ret = prog_dmabuf_dac(s)))
1740 case SOUND_PCM_READ_RATE:
1741 return put_user((file->f_mode & FMODE_READ) ?
1742 s->adcrate : s->dacrate, (int *)arg);
1744 case SOUND_PCM_READ_CHANNELS:
1745 if (file->f_mode & FMODE_READ)
1746 return put_user((s->capcc & CC_SM) ? 2 : 1,
1749 return put_user((s->pcc & CC_SM) ? 2 : 1,
1752 case SOUND_PCM_READ_BITS:
1753 if (file->f_mode & FMODE_READ)
1754 return put_user((s->capcc & CC_DF) ? 16 : 8,
1757 return put_user((s->pcc & CC_DF) ? 16 : 8,
1760 case SOUND_PCM_WRITE_FILTER:
1761 case SNDCTL_DSP_SETSYNCRO:
1762 case SOUND_PCM_READ_FILTER:
1766 return mixdev_ioctl(&s->codec, cmd, arg);
1770 static int it8172_open(struct inode *inode, struct file *file)
1772 int minor = MINOR(inode->i_rdev);
1773 DECLARE_WAITQUEUE(wait, current);
1774 unsigned long flags;
1775 struct list_head *list;
1776 struct it8172_state *s;
1779 #ifdef IT8172_VERBOSE_DEBUG
1780 if (file->f_flags & O_NONBLOCK)
1781 dbg(__FUNCTION__ ": non-blocking");
1783 dbg(__FUNCTION__ ": blocking");
1786 for (list = devs.next; ; list = list->next) {
1789 s = list_entry(list, struct it8172_state, devs);
1790 if (!((s->dev_audio ^ minor) & ~0xf))
1793 file->private_data = s;
1794 /* wait for device to become free */
1796 while (s->open_mode & file->f_mode) {
1797 if (file->f_flags & O_NONBLOCK) {
1801 add_wait_queue(&s->open_wait, &wait);
1802 __set_current_state(TASK_INTERRUPTIBLE);
1805 remove_wait_queue(&s->open_wait, &wait);
1806 set_current_state(TASK_RUNNING);
1807 if (signal_pending(current))
1808 return -ERESTARTSYS;
1812 spin_lock_irqsave(&s->lock, flags);
1814 if (file->f_mode & FMODE_READ) {
1815 s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags =
1816 s->dma_adc.subdivision = s->dma_adc.total_bytes = 0;
1817 s->capcc &= ~(CC_SM | CC_DF);
1818 set_adc_rate(s, 8000);
1819 if ((minor & 0xf) == SND_DEV_DSP16)
1821 outw(s->capcc, s->io+IT_AC_CAPCC);
1822 if ((ret = prog_dmabuf_adc(s))) {
1823 spin_unlock_irqrestore(&s->lock, flags);
1827 if (file->f_mode & FMODE_WRITE) {
1828 s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags =
1829 s->dma_dac.subdivision = s->dma_dac.total_bytes = 0;
1830 s->pcc &= ~(CC_SM | CC_DF);
1831 set_dac_rate(s, 8000);
1832 if ((minor & 0xf) == SND_DEV_DSP16)
1834 outw(s->pcc, s->io+IT_AC_PCC);
1835 if ((ret = prog_dmabuf_dac(s))) {
1836 spin_unlock_irqrestore(&s->lock, flags);
1841 spin_unlock_irqrestore(&s->lock, flags);
1843 s->open_mode |= (file->f_mode & (FMODE_READ | FMODE_WRITE));
1848 static int it8172_release(struct inode *inode, struct file *file)
1850 struct it8172_state *s = (struct it8172_state *)file->private_data;
1852 #ifdef IT8172_VERBOSE_DEBUG
1856 if (file->f_mode & FMODE_WRITE)
1857 drain_dac(s, file->f_flags & O_NONBLOCK);
1859 if (file->f_mode & FMODE_WRITE) {
1861 dealloc_dmabuf(s, &s->dma_dac);
1863 if (file->f_mode & FMODE_READ) {
1865 dealloc_dmabuf(s, &s->dma_adc);
1867 s->open_mode &= ((~file->f_mode) & (FMODE_READ|FMODE_WRITE));
1869 wake_up(&s->open_wait);
1874 static /*const*/ struct file_operations it8172_audio_fops = {
1876 llseek: it8172_llseek,
1878 write: it8172_write,
1880 ioctl: it8172_ioctl,
1883 release: it8172_release,
1887 /* --------------------------------------------------------------------- */
1890 /* --------------------------------------------------------------------- */
1893 * for debugging purposes, we'll create a proc device that dumps the
1898 static int proc_it8172_dump (char *buf, char **start, off_t fpos,
1899 int length, int *eof, void *data)
1901 struct it8172_state *s;
1904 if (list_empty(&devs))
1906 s = list_entry(devs.next, struct it8172_state, devs);
1908 /* print out header */
1909 len += sprintf(buf + len, "\n\t\tIT8172 Audio Debug\n\n");
1911 // print out digital controller state
1912 len += sprintf (buf + len, "IT8172 Audio Controller registers\n");
1913 len += sprintf (buf + len, "---------------------------------\n");
1915 while (cnt < 0x72) {
1916 if (cnt == IT_AC_PCB1STA || cnt == IT_AC_PCB2STA ||
1917 cnt == IT_AC_CAPB1STA || cnt == IT_AC_CAPB2STA ||
1918 cnt == IT_AC_PFDP) {
1919 len+= sprintf (buf + len, "reg %02x = %08x\n",
1920 cnt, inl(s->io+cnt));
1923 len+= sprintf (buf + len, "reg %02x = %04x\n",
1924 cnt, inw(s->io+cnt));
1929 /* print out CODEC state */
1930 len += sprintf (buf + len, "\nAC97 CODEC registers\n");
1931 len += sprintf (buf + len, "----------------------\n");
1932 for (cnt=0; cnt <= 0x7e; cnt = cnt +2)
1933 len+= sprintf (buf + len, "reg %02x = %04x\n",
1934 cnt, rdcodec(&s->codec, cnt));
1941 *start = buf + fpos;
1942 if ((len -= fpos) > length)
1948 #endif /* IT8172_DEBUG */
1950 /* --------------------------------------------------------------------- */
1952 /* maximum number of devices; only used for command line params */
1955 static int spdif[NR_DEVICE] = { 0, };
1956 static int i2s_fmt[NR_DEVICE] = { 0, };
1958 static unsigned int devindex = 0;
1960 MODULE_PARM(spdif, "1-" __MODULE_STRING(NR_DEVICE) "i");
1961 MODULE_PARM_DESC(spdif, "if 1 the S/PDIF digital output is enabled");
1962 MODULE_PARM(i2s_fmt, "1-" __MODULE_STRING(NR_DEVICE) "i");
1963 MODULE_PARM_DESC(i2s_fmt, "the format of I2S");
1965 MODULE_AUTHOR("Monta Vista Software, stevel@mvista.com");
1966 MODULE_DESCRIPTION("IT8172 Audio Driver");
1968 /* --------------------------------------------------------------------- */
1970 static int __devinit it8172_probe(struct pci_dev *pcidev,
1971 const struct pci_device_id *pciid)
1973 struct it8172_state *s;
1975 unsigned short pcisr, vol;
1976 unsigned char legacy, imc;
1979 if (pcidev->irq == 0)
1982 if (!(s = kmalloc(sizeof(struct it8172_state), GFP_KERNEL))) {
1983 err("alloc of device struct failed");
1987 memset(s, 0, sizeof(struct it8172_state));
1988 init_waitqueue_head(&s->dma_adc.wait);
1989 init_waitqueue_head(&s->dma_dac.wait);
1990 init_waitqueue_head(&s->open_wait);
1991 init_MUTEX(&s->open_sem);
1992 spin_lock_init(&s->lock);
1994 s->io = pci_resource_start(pcidev, 0);
1995 s->irq = pcidev->irq;
1996 s->vendor = pcidev->vendor;
1997 s->device = pcidev->device;
1998 pci_read_config_byte(pcidev, PCI_REVISION_ID, &s->rev);
1999 s->codec.private_data = s;
2001 s->codec.codec_read = rdcodec;
2002 s->codec.codec_write = wrcodec;
2003 s->codec.codec_wait = waitcodec;
2005 if (!request_region(s->io, pci_resource_len(pcidev,0),
2006 IT8172_MODULE_NAME)) {
2007 err("io ports %#lx->%#lx in use",
2008 s->io, s->io + pci_resource_len(pcidev,0)-1);
2011 if (request_irq(s->irq, it8172_interrupt, SA_INTERRUPT,
2012 IT8172_MODULE_NAME, s)) {
2013 err("irq %u in use", s->irq);
2017 info("IO at %#lx, IRQ %d", s->io, s->irq);
2019 /* register devices */
2020 if ((s->dev_audio = register_sound_dsp(&it8172_audio_fops, -1)) < 0)
2022 if ((s->codec.dev_mixer =
2023 register_sound_mixer(&it8172_mixer_fops, -1)) < 0)
2027 /* intialize the debug proc device */
2028 s->ps = create_proc_read_entry(IT8172_MODULE_NAME, 0, NULL,
2029 proc_it8172_dump, NULL);
2030 #endif /* IT8172_DEBUG */
2033 * Reset the Audio device using the IT8172 PCI Reset register. This
2034 * creates an audible double click on a speaker connected to Line-out.
2036 IT_IO_READ16(IT_PM_PCISR, pcisr);
2037 pcisr |= IT_PM_PCISR_ACSR;
2038 IT_IO_WRITE16(IT_PM_PCISR, pcisr);
2039 /* wait up to 100msec for reset to complete */
2040 for (i=0; pcisr & IT_PM_PCISR_ACSR; i++) {
2044 IT_IO_READ16(IT_PM_PCISR, pcisr);
2047 err("chip reset timeout!");
2051 /* enable pci io and bus mastering */
2052 if (pci_enable_device(pcidev))
2054 pci_set_master(pcidev);
2056 /* get out of legacy mode */
2057 pci_read_config_byte (pcidev, 0x40, &legacy);
2058 pci_write_config_byte (pcidev, 0x40, legacy & ~1);
2060 s->spdif_volume = -1;
2061 /* check to see if s/pdif mode is being requested */
2062 if (spdif[devindex]) {
2063 info("enabling S/PDIF output");
2064 s->spdif_volume = 0;
2065 outb(GC_SOE, s->io+IT_AC_GC);
2067 info("disabling S/PDIF output");
2068 outb(0, s->io+IT_AC_GC);
2071 /* check to see if I2S format requested */
2072 if (i2s_fmt[devindex]) {
2073 info("setting I2S format to 0x%02x", i2s_fmt[devindex]);
2074 outb(i2s_fmt[devindex], s->io+IT_AC_I2SMC);
2076 outb(I2SMC_I2SF_I2S, s->io+IT_AC_I2SMC);
2079 /* cold reset the AC97 */
2080 outw(CODECC_CR, s->io+IT_AC_CODECC);
2082 outw(0, s->io+IT_AC_CODECC);
2083 /* need to delay around 500msec(bleech) to give
2084 some CODECs enough time to wakeup */
2087 /* AC97 warm reset to start the bitclk */
2088 outw(CODECC_WR, s->io+IT_AC_CODECC);
2090 outw(0, s->io+IT_AC_CODECC);
2093 if (!ac97_probe_codec(&s->codec))
2096 /* add I2S as allowable recording source */
2097 s->codec.record_sources |= SOUND_MASK_I2S;
2099 /* Enable Volume button interrupts */
2100 imc = inb(s->io+IT_AC_IMC);
2101 outb(imc & ~IMC_VCIM, s->io+IT_AC_IMC);
2103 /* Un-mute PCM and FM out on the controller */
2104 vol = inw(s->io+IT_AC_PCMOV);
2105 outw(vol & ~PCMOV_PCMOM, s->io+IT_AC_PCMOV);
2106 vol = inw(s->io+IT_AC_FMOV);
2107 outw(vol & ~FMOV_FMOM, s->io+IT_AC_FMOV);
2109 /* set channel defaults to 8-bit, mono, 8 Khz */
2112 set_dac_rate(s, 8000);
2113 set_adc_rate(s, 8000);
2115 /* set mic to be the recording source */
2116 val = SOUND_MASK_MIC;
2117 mixdev_ioctl(&s->codec, SOUND_MIXER_WRITE_RECSRC,
2118 (unsigned long)&val);
2120 /* mute AC'97 master and PCM when in S/PDIF mode */
2121 if (s->spdif_volume != -1) {
2123 s->codec.mixer_ioctl(&s->codec, SOUND_MIXER_WRITE_VOLUME,
2124 (unsigned long)&val);
2125 s->codec.mixer_ioctl(&s->codec, SOUND_MIXER_WRITE_PCM,
2126 (unsigned long)&val);
2130 sprintf(proc_str, "driver/%s/%d/ac97", IT8172_MODULE_NAME,
2132 s->ac97_ps = create_proc_read_entry (proc_str, 0, NULL,
2133 ac97_read_proc, &s->codec);
2136 /* store it in the driver field */
2137 pci_set_drvdata(pcidev, s);
2138 pcidev->dma_mask = 0xffffffff;
2139 /* put it into driver list */
2140 list_add_tail(&s->devs, &devs);
2141 /* increment devindex */
2142 if (devindex < NR_DEVICE-1)
2147 unregister_sound_mixer(s->codec.dev_mixer);
2149 unregister_sound_dsp(s->dev_audio);
2151 err("cannot register misc device");
2152 free_irq(s->irq, s);
2154 release_region(s->io, pci_resource_len(pcidev,0));
2160 static void __devinit it8172_remove(struct pci_dev *dev)
2162 struct it8172_state *s = pci_get_drvdata(dev);
2169 remove_proc_entry(IT8172_MODULE_NAME, NULL);
2170 #endif /* IT8172_DEBUG */
2172 free_irq(s->irq, s);
2173 release_region(s->io, pci_resource_len(dev,0));
2174 unregister_sound_dsp(s->dev_audio);
2175 unregister_sound_mixer(s->codec.dev_mixer);
2177 pci_set_drvdata(dev, NULL);
2182 static struct pci_device_id id_table[] __devinitdata = {
2183 { PCI_VENDOR_ID_ITE, PCI_DEVICE_ID_ITE_IT8172G_AUDIO, PCI_ANY_ID,
2188 MODULE_DEVICE_TABLE(pci, id_table);
2190 static struct pci_driver it8172_driver = {
2191 name: IT8172_MODULE_NAME,
2193 probe: it8172_probe,
2194 remove: it8172_remove
2197 static int __init init_it8172(void)
2199 if (!pci_present()) /* No PCI bus in this machine! */
2201 info("version v0.5 time " __TIME__ " " __DATE__);
2202 return pci_module_init(&it8172_driver);
2205 static void __exit cleanup_it8172(void)
2208 pci_unregister_driver(&it8172_driver);
2211 module_init(init_it8172);
2212 module_exit(cleanup_it8172);
2214 /* --------------------------------------------------------------------- */
2218 /* format is: it8172=[spdif],[i2s:<I2S format>] */
2220 static int __init it8172_setup(char *options)
2223 static unsigned __initdata nr_dev = 0;
2225 if (nr_dev >= NR_DEVICE)
2228 if (!options || !*options)
2231 for(this_opt=strtok(options, ",");
2232 this_opt; this_opt=strtok(NULL, ",")) {
2233 if (!strncmp(this_opt, "spdif", 5)) {
2235 } else if (!strncmp(this_opt, "i2s:", 4)) {
2236 if (!strncmp(this_opt+4, "dac", 3))
2237 i2s_fmt[nr_dev] = I2SMC_I2SF_DAC;
2238 else if (!strncmp(this_opt+4, "adc", 3))
2239 i2s_fmt[nr_dev] = I2SMC_I2SF_ADC;
2240 else if (!strncmp(this_opt+4, "i2s", 3))
2241 i2s_fmt[nr_dev] = I2SMC_I2SF_I2S;
2249 __setup("it8172=", it8172_setup);