2 * au1000.c -- Sound driver for Alchemy Au1000 MIPS Internet Edge
5 * Copyright 2001 MontaVista Software Inc.
6 * Author: MontaVista Software, Inc.
7 * stevel@mvista.com or source@mvista.com
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
20 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 675 Mass Ave, Cambridge, MA 02139, USA.
30 * Module command line parameters:
33 * /dev/dsp standard OSS /dev/dsp device
34 * /dev/mixer standard OSS /dev/mixer device
38 * 1. Much of the OSS buffer allocation, ioctl's, and mmap'ing are
39 * taken, slightly modified or not at all, from the ES1371 driver,
40 * so refer to the credits in es1371.c for those. The rest of the
41 * code (probe, open, read, write, the ISR, etc.) is new.
44 * 06.27.2001 Initial version
45 * 03.20.2002 Added mutex locks around read/write methods, to prevent
46 * simultaneous access on SMP or preemptible kernels. Also
47 * removed the counter/pointer fragment aligning at the end
48 * of read/write methods [stevel].
49 * 03.21.2002 Add support for coherent DMA on the audio read/write DMA
53 #include <linux/module.h>
54 #include <linux/string.h>
55 #include <linux/ioport.h>
56 #include <linux/sched.h>
57 #include <linux/delay.h>
58 #include <linux/sound.h>
59 #include <linux/slab.h>
60 #include <linux/soundcard.h>
61 #include <linux/init.h>
62 #include <linux/page-flags.h>
63 #include <linux/poll.h>
64 #include <linux/pci.h>
65 #include <linux/bitops.h>
66 #include <linux/proc_fs.h>
67 #include <linux/spinlock.h>
68 #include <linux/smp_lock.h>
69 #include <linux/ac97_codec.h>
70 #include <linux/interrupt.h>
72 #include <asm/uaccess.h>
73 #include <asm/mach-au1x00/au1000.h>
74 #include <asm/mach-au1x00/au1000_dma.h>
76 /* --------------------------------------------------------------------- */
78 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
80 #undef AU1000_VERBOSE_DEBUG
82 #define AU1000_MODULE_NAME "Au1000 audio"
83 #define PFX AU1000_MODULE_NAME
86 #define dbg(format, arg...) printk(KERN_DEBUG PFX ": " format "\n" , ## arg)
88 #define dbg(format, arg...) do {} while (0)
90 #define err(format, arg...) printk(KERN_ERR PFX ": " format "\n" , ## arg)
91 #define info(format, arg...) printk(KERN_INFO PFX ": " format "\n" , ## arg)
92 #define warn(format, arg...) printk(KERN_WARNING PFX ": " format "\n" , ## arg)
96 #define POLL_COUNT 0x5000
97 #define AC97_EXT_DACS (AC97_EXTID_SDAC | AC97_EXTID_CDAC | AC97_EXTID_LDAC)
100 static int vra = 0; // 0 = no VRA, 1 = use VRA if codec supports it
101 MODULE_PARM(vra, "i");
102 MODULE_PARM_DESC(vra, "if 1 use VRA if codec supports it");
105 /* --------------------------------------------------------------------- */
107 struct au1000_state {
108 /* soundcore stuff */
112 /* debug /proc entry */
113 struct proc_dir_entry *ps;
114 struct proc_dir_entry *ac97_ps;
115 #endif /* AU1000_DEBUG */
117 struct ac97_codec codec;
118 unsigned codec_base_caps;// AC'97 reg 00h, "Reset Register"
119 unsigned codec_ext_caps; // AC'97 reg 28h, "Extended Audio ID"
120 int no_vra; // do not use VRA
123 struct semaphore open_sem;
124 struct semaphore sem;
126 wait_queue_head_t open_wait;
129 unsigned int dmanr; // DMA Channel number
130 unsigned sample_rate; // Hz
131 unsigned src_factor; // SRC interp/decimation (no vra)
132 unsigned sample_size; // 8 or 16
133 int num_channels; // 1 = mono, 2 = stereo, 4, 6
134 int dma_bytes_per_sample;// DMA bytes per audio sample frame
135 int user_bytes_per_sample;// User bytes per audio sample frame
136 int cnt_factor; // user-to-DMA bytes per audio
141 unsigned numfrag; // # of DMA fragments in DMA buffer
143 void *nextIn; // ptr to next-in to DMA buffer
144 void *nextOut;// ptr to next-out from DMA buffer
145 int count; // current byte count in DMA buffer
146 unsigned total_bytes; // total bytes written or read
147 unsigned error; // over/underrun
148 wait_queue_head_t wait;
149 /* redundant, but makes calculations easier */
150 unsigned fragsize; // user perception of fragment size
151 unsigned dma_fragsize; // DMA (real) fragment size
152 unsigned dmasize; // Total DMA buffer size
153 // (mult. of DMA fragsize)
158 unsigned ossfragshift;
160 unsigned subdivision;
164 /* --------------------------------------------------------------------- */
167 static inline unsigned ld2(unsigned int x)
192 /* --------------------------------------------------------------------- */
194 static void au1000_delay(int msec)
202 tmo = jiffies + (msec * HZ) / 1000;
204 tmo2 = tmo - jiffies;
207 schedule_timeout(tmo2);
212 /* --------------------------------------------------------------------- */
214 static u16 rdcodec(struct ac97_codec *codec, u8 addr)
216 struct au1000_state *s = (struct au1000_state *)codec->private_data;
222 spin_lock_irqsave(&s->lock, flags);
224 for (i = 0; i < POLL_COUNT; i++)
225 if (!(au_readl(AC97C_STATUS) & AC97C_CP))
228 err("rdcodec: codec cmd pending expired!");
230 cmd = (u32) addr & AC97C_INDEX_MASK;
231 cmd |= AC97C_READ; // read command
232 au_writel(cmd, AC97C_CMD);
234 /* now wait for the data */
235 for (i = 0; i < POLL_COUNT; i++)
236 if (!(au_readl(AC97C_STATUS) & AC97C_CP))
238 if (i == POLL_COUNT) {
239 err("rdcodec: read poll expired!");
243 data = au_readl(AC97C_CMD) & 0xffff;
245 spin_unlock_irqrestore(&s->lock, flags);
251 static void wrcodec(struct ac97_codec *codec, u8 addr, u16 data)
253 struct au1000_state *s = (struct au1000_state *)codec->private_data;
258 spin_lock_irqsave(&s->lock, flags);
260 for (i = 0; i < POLL_COUNT; i++)
261 if (!(au_readl(AC97C_STATUS) & AC97C_CP))
264 err("wrcodec: codec cmd pending expired!");
266 cmd = (u32) addr & AC97C_INDEX_MASK;
267 cmd &= ~AC97C_READ; // write command
268 cmd |= ((u32) data << AC97C_WD_BIT); // OR in the data word
269 au_writel(cmd, AC97C_CMD);
271 spin_unlock_irqrestore(&s->lock, flags);
274 static void waitcodec(struct ac97_codec *codec)
279 /* codec_wait is used to wait for a ready state after
283 // first poll the CODEC_READY tag bit
284 for (i = 0; i < POLL_COUNT; i++)
285 if (au_readl(AC97C_STATUS) & AC97C_READY)
287 if (i == POLL_COUNT) {
288 err("waitcodec: CODEC_READY poll expired!");
291 // get AC'97 powerdown control/status register
292 temp = rdcodec(codec, AC97_POWER_CONTROL);
294 // If anything is powered down, power'em up
297 wrcodec(codec, AC97_POWER_CONTROL, 0);
300 temp = rdcodec(codec, AC97_POWER_CONTROL);
303 // Check if Codec REF,ANL,DAC,ADC ready
304 if ((temp & 0x7f0f) != 0x000f)
305 err("codec reg 26 status (0x%x) not ready!!", temp);
309 /* --------------------------------------------------------------------- */
311 /* stop the ADC before calling */
312 static void set_adc_rate(struct au1000_state *s, unsigned rate)
314 struct dmabuf *adc = &s->dma_adc;
315 struct dmabuf *dac = &s->dma_dac;
316 unsigned adc_rate, dac_rate;
321 adc->src_factor = ((96000 / rate) + 1) >> 1;
322 adc->sample_rate = 48000 / adc->src_factor;
328 ac97_extstat = rdcodec(&s->codec, AC97_EXTENDED_STATUS);
330 rate = rate > 48000 ? 48000 : rate;
333 wrcodec(&s->codec, AC97_EXTENDED_STATUS,
334 ac97_extstat | AC97_EXTSTAT_VRA);
335 // now write the sample rate
336 wrcodec(&s->codec, AC97_PCM_LR_ADC_RATE, (u16) rate);
337 // read it back for actual supported rate
338 adc_rate = rdcodec(&s->codec, AC97_PCM_LR_ADC_RATE);
340 #ifdef AU1000_VERBOSE_DEBUG
341 dbg("%s: set to %d Hz", __FUNCTION__, adc_rate);
344 // some codec's don't allow unequal DAC and ADC rates, in which case
345 // writing one rate reg actually changes both.
346 dac_rate = rdcodec(&s->codec, AC97_PCM_FRONT_DAC_RATE);
347 if (dac->num_channels > 2)
348 wrcodec(&s->codec, AC97_PCM_SURR_DAC_RATE, dac_rate);
349 if (dac->num_channels > 4)
350 wrcodec(&s->codec, AC97_PCM_LFE_DAC_RATE, dac_rate);
352 adc->sample_rate = adc_rate;
353 dac->sample_rate = dac_rate;
356 /* stop the DAC before calling */
357 static void set_dac_rate(struct au1000_state *s, unsigned rate)
359 struct dmabuf *dac = &s->dma_dac;
360 struct dmabuf *adc = &s->dma_adc;
361 unsigned adc_rate, dac_rate;
366 dac->src_factor = ((96000 / rate) + 1) >> 1;
367 dac->sample_rate = 48000 / dac->src_factor;
373 ac97_extstat = rdcodec(&s->codec, AC97_EXTENDED_STATUS);
375 rate = rate > 48000 ? 48000 : rate;
378 wrcodec(&s->codec, AC97_EXTENDED_STATUS,
379 ac97_extstat | AC97_EXTSTAT_VRA);
380 // now write the sample rate
381 wrcodec(&s->codec, AC97_PCM_FRONT_DAC_RATE, (u16) rate);
382 // I don't support different sample rates for multichannel,
383 // so make these channels the same.
384 if (dac->num_channels > 2)
385 wrcodec(&s->codec, AC97_PCM_SURR_DAC_RATE, (u16) rate);
386 if (dac->num_channels > 4)
387 wrcodec(&s->codec, AC97_PCM_LFE_DAC_RATE, (u16) rate);
388 // read it back for actual supported rate
389 dac_rate = rdcodec(&s->codec, AC97_PCM_FRONT_DAC_RATE);
391 #ifdef AU1000_VERBOSE_DEBUG
392 dbg("%s: set to %d Hz", __FUNCTION__, dac_rate);
395 // some codec's don't allow unequal DAC and ADC rates, in which case
396 // writing one rate reg actually changes both.
397 adc_rate = rdcodec(&s->codec, AC97_PCM_LR_ADC_RATE);
399 dac->sample_rate = dac_rate;
400 adc->sample_rate = adc_rate;
403 static void stop_dac(struct au1000_state *s)
405 struct dmabuf *db = &s->dma_dac;
411 spin_lock_irqsave(&s->lock, flags);
413 disable_dma(db->dmanr);
417 spin_unlock_irqrestore(&s->lock, flags);
420 static void stop_adc(struct au1000_state *s)
422 struct dmabuf *db = &s->dma_adc;
428 spin_lock_irqsave(&s->lock, flags);
430 disable_dma(db->dmanr);
434 spin_unlock_irqrestore(&s->lock, flags);
438 static void set_xmit_slots(int num_channels)
440 u32 ac97_config = au_readl(AC97C_CONFIG) & ~AC97C_XMIT_SLOTS_MASK;
442 switch (num_channels) {
444 case 2: // stereo, slots 3,4
445 ac97_config |= (0x3 << AC97C_XMIT_SLOTS_BIT);
447 case 4: // stereo with surround, slots 3,4,7,8
448 ac97_config |= (0x33 << AC97C_XMIT_SLOTS_BIT);
450 case 6: // stereo with surround and center/LFE, slots 3,4,6,7,8,9
451 ac97_config |= (0x7b << AC97C_XMIT_SLOTS_BIT);
455 au_writel(ac97_config, AC97C_CONFIG);
458 static void set_recv_slots(int num_channels)
460 u32 ac97_config = au_readl(AC97C_CONFIG) & ~AC97C_RECV_SLOTS_MASK;
463 * Always enable slots 3 and 4 (stereo). Slot 6 is
464 * optional Mic ADC, which I don't support yet.
466 ac97_config |= (0x3 << AC97C_RECV_SLOTS_BIT);
468 au_writel(ac97_config, AC97C_CONFIG);
471 static void start_dac(struct au1000_state *s)
473 struct dmabuf *db = &s->dma_dac;
475 unsigned long buf1, buf2;
480 spin_lock_irqsave(&s->lock, flags);
482 au_readl(AC97C_STATUS); // read status to clear sticky bits
484 // reset Buffer 1 and 2 pointers to nextOut and nextOut+dma_fragsize
485 buf1 = virt_to_phys(db->nextOut);
486 buf2 = buf1 + db->dma_fragsize;
487 if (buf2 >= db->dmaaddr + db->dmasize)
490 set_xmit_slots(db->num_channels);
493 if (get_dma_active_buffer(db->dmanr) == 0) {
494 clear_dma_done0(db->dmanr); // clear DMA done bit
495 set_dma_addr0(db->dmanr, buf1);
496 set_dma_addr1(db->dmanr, buf2);
498 clear_dma_done1(db->dmanr); // clear DMA done bit
499 set_dma_addr1(db->dmanr, buf1);
500 set_dma_addr0(db->dmanr, buf2);
502 set_dma_count(db->dmanr, db->dma_fragsize>>1);
503 enable_dma_buffers(db->dmanr);
505 start_dma(db->dmanr);
507 #ifdef AU1000_VERBOSE_DEBUG
508 dump_au1000_dma_channel(db->dmanr);
513 spin_unlock_irqrestore(&s->lock, flags);
516 static void start_adc(struct au1000_state *s)
518 struct dmabuf *db = &s->dma_adc;
520 unsigned long buf1, buf2;
525 spin_lock_irqsave(&s->lock, flags);
527 au_readl(AC97C_STATUS); // read status to clear sticky bits
529 // reset Buffer 1 and 2 pointers to nextIn and nextIn+dma_fragsize
530 buf1 = virt_to_phys(db->nextIn);
531 buf2 = buf1 + db->dma_fragsize;
532 if (buf2 >= db->dmaaddr + db->dmasize)
535 set_recv_slots(db->num_channels);
538 if (get_dma_active_buffer(db->dmanr) == 0) {
539 clear_dma_done0(db->dmanr); // clear DMA done bit
540 set_dma_addr0(db->dmanr, buf1);
541 set_dma_addr1(db->dmanr, buf2);
543 clear_dma_done1(db->dmanr); // clear DMA done bit
544 set_dma_addr1(db->dmanr, buf1);
545 set_dma_addr0(db->dmanr, buf2);
547 set_dma_count(db->dmanr, db->dma_fragsize>>1);
548 enable_dma_buffers(db->dmanr);
550 start_dma(db->dmanr);
552 #ifdef AU1000_VERBOSE_DEBUG
553 dump_au1000_dma_channel(db->dmanr);
558 spin_unlock_irqrestore(&s->lock, flags);
561 /* --------------------------------------------------------------------- */
563 #define DMABUF_DEFAULTORDER (17-PAGE_SHIFT)
564 #define DMABUF_MINORDER 1
566 static inline void dealloc_dmabuf(struct au1000_state *s, struct dmabuf *db)
568 struct page *page, *pend;
571 /* undo marking the pages as reserved */
572 pend = virt_to_page(db->rawbuf +
573 (PAGE_SIZE << db->buforder) - 1);
574 for (page = virt_to_page(db->rawbuf); page <= pend; page++)
575 ClearPageReserved(page);
576 dma_free_noncoherent(NULL,
577 PAGE_SIZE << db->buforder,
581 db->rawbuf = db->nextIn = db->nextOut = NULL;
582 db->mapped = db->ready = 0;
585 static int prog_dmabuf(struct au1000_state *s, struct dmabuf *db)
588 unsigned user_bytes_per_sec;
590 struct page *page, *pend;
591 unsigned rate = db->sample_rate;
594 db->ready = db->mapped = 0;
595 for (order = DMABUF_DEFAULTORDER;
596 order >= DMABUF_MINORDER; order--)
597 if ((db->rawbuf = dma_alloc_noncoherent(NULL,
604 db->buforder = order;
605 /* now mark the pages as reserved;
606 otherwise remap_pfn_range doesn't do what we want */
607 pend = virt_to_page(db->rawbuf +
608 (PAGE_SIZE << db->buforder) - 1);
609 for (page = virt_to_page(db->rawbuf); page <= pend; page++)
610 SetPageReserved(page);
614 if (db->sample_size == 8)
616 if (db->num_channels == 1)
618 db->cnt_factor *= db->src_factor;
621 db->nextIn = db->nextOut = db->rawbuf;
623 db->user_bytes_per_sample = (db->sample_size>>3) * db->num_channels;
624 db->dma_bytes_per_sample = 2 * ((db->num_channels == 1) ?
625 2 : db->num_channels);
627 user_bytes_per_sec = rate * db->user_bytes_per_sample;
628 bufs = PAGE_SIZE << db->buforder;
629 if (db->ossfragshift) {
630 if ((1000 << db->ossfragshift) < user_bytes_per_sec)
631 db->fragshift = ld2(user_bytes_per_sec/1000);
633 db->fragshift = db->ossfragshift;
635 db->fragshift = ld2(user_bytes_per_sec / 100 /
636 (db->subdivision ? db->subdivision : 1));
637 if (db->fragshift < 3)
641 db->fragsize = 1 << db->fragshift;
642 db->dma_fragsize = db->fragsize * db->cnt_factor;
643 db->numfrag = bufs / db->dma_fragsize;
645 while (db->numfrag < 4 && db->fragshift > 3) {
647 db->fragsize = 1 << db->fragshift;
648 db->dma_fragsize = db->fragsize * db->cnt_factor;
649 db->numfrag = bufs / db->dma_fragsize;
652 if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
653 db->numfrag = db->ossmaxfrags;
655 db->dmasize = db->dma_fragsize * db->numfrag;
656 memset(db->rawbuf, 0, bufs);
658 #ifdef AU1000_VERBOSE_DEBUG
659 dbg("rate=%d, samplesize=%d, channels=%d",
660 rate, db->sample_size, db->num_channels);
661 dbg("fragsize=%d, cnt_factor=%d, dma_fragsize=%d",
662 db->fragsize, db->cnt_factor, db->dma_fragsize);
663 dbg("numfrag=%d, dmasize=%d", db->numfrag, db->dmasize);
670 static inline int prog_dmabuf_adc(struct au1000_state *s)
673 return prog_dmabuf(s, &s->dma_adc);
677 static inline int prog_dmabuf_dac(struct au1000_state *s)
680 return prog_dmabuf(s, &s->dma_dac);
684 /* hold spinlock for the following */
685 static irqreturn_t dac_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
687 struct au1000_state *s = (struct au1000_state *) dev_id;
688 struct dmabuf *dac = &s->dma_dac;
689 unsigned long newptr;
690 u32 ac97c_stat, buff_done;
692 ac97c_stat = au_readl(AC97C_STATUS);
693 #ifdef AU1000_VERBOSE_DEBUG
694 if (ac97c_stat & (AC97C_XU | AC97C_XO | AC97C_TE))
695 dbg("AC97C status = 0x%08x", ac97c_stat);
698 if ((buff_done = get_dma_buffer_done(dac->dmanr)) == 0) {
699 /* fastpath out, to ease interrupt sharing */
705 if (buff_done != (DMA_D0 | DMA_D1)) {
706 dac->nextOut += dac->dma_fragsize;
707 if (dac->nextOut >= dac->rawbuf + dac->dmasize)
708 dac->nextOut -= dac->dmasize;
710 /* update playback pointers */
711 newptr = virt_to_phys(dac->nextOut) + dac->dma_fragsize;
712 if (newptr >= dac->dmaaddr + dac->dmasize)
713 newptr -= dac->dmasize;
715 dac->count -= dac->dma_fragsize;
716 dac->total_bytes += dac->dma_fragsize;
718 if (dac->count <= 0) {
719 #ifdef AU1000_VERBOSE_DEBUG
722 spin_unlock(&s->lock);
726 dac->nextIn = dac->nextOut;
727 } else if (buff_done == DMA_D0) {
728 clear_dma_done0(dac->dmanr); // clear DMA done bit
729 set_dma_count0(dac->dmanr, dac->dma_fragsize>>1);
730 set_dma_addr0(dac->dmanr, newptr);
731 enable_dma_buffer0(dac->dmanr); // reenable
733 clear_dma_done1(dac->dmanr); // clear DMA done bit
734 set_dma_count1(dac->dmanr, dac->dma_fragsize>>1);
735 set_dma_addr1(dac->dmanr, newptr);
736 enable_dma_buffer1(dac->dmanr); // reenable
739 // both done bits set, we missed an interrupt
740 spin_unlock(&s->lock);
744 dac->nextOut += 2*dac->dma_fragsize;
745 if (dac->nextOut >= dac->rawbuf + dac->dmasize)
746 dac->nextOut -= dac->dmasize;
748 dac->count -= 2*dac->dma_fragsize;
749 dac->total_bytes += 2*dac->dma_fragsize;
751 if (dac->count > 0) {
752 spin_unlock(&s->lock);
758 /* wake up anybody listening */
759 if (waitqueue_active(&dac->wait))
762 spin_unlock(&s->lock);
768 static irqreturn_t adc_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
770 struct au1000_state *s = (struct au1000_state *) dev_id;
771 struct dmabuf *adc = &s->dma_adc;
772 unsigned long newptr;
773 u32 ac97c_stat, buff_done;
775 ac97c_stat = au_readl(AC97C_STATUS);
776 #ifdef AU1000_VERBOSE_DEBUG
777 if (ac97c_stat & (AC97C_RU | AC97C_RO))
778 dbg("AC97C status = 0x%08x", ac97c_stat);
781 if ((buff_done = get_dma_buffer_done(adc->dmanr)) == 0) {
782 /* fastpath out, to ease interrupt sharing */
788 if (buff_done != (DMA_D0 | DMA_D1)) {
789 if (adc->count + adc->dma_fragsize > adc->dmasize) {
790 // Overrun. Stop ADC and log the error
791 spin_unlock(&s->lock);
798 adc->nextIn += adc->dma_fragsize;
799 if (adc->nextIn >= adc->rawbuf + adc->dmasize)
800 adc->nextIn -= adc->dmasize;
802 /* update capture pointers */
803 newptr = virt_to_phys(adc->nextIn) + adc->dma_fragsize;
804 if (newptr >= adc->dmaaddr + adc->dmasize)
805 newptr -= adc->dmasize;
807 adc->count += adc->dma_fragsize;
808 adc->total_bytes += adc->dma_fragsize;
810 if (buff_done == DMA_D0) {
811 clear_dma_done0(adc->dmanr); // clear DMA done bit
812 set_dma_count0(adc->dmanr, adc->dma_fragsize>>1);
813 set_dma_addr0(adc->dmanr, newptr);
814 enable_dma_buffer0(adc->dmanr); // reenable
816 clear_dma_done1(adc->dmanr); // clear DMA done bit
817 set_dma_count1(adc->dmanr, adc->dma_fragsize>>1);
818 set_dma_addr1(adc->dmanr, newptr);
819 enable_dma_buffer1(adc->dmanr); // reenable
822 // both done bits set, we missed an interrupt
823 spin_unlock(&s->lock);
827 if (adc->count + 2*adc->dma_fragsize > adc->dmasize) {
828 // Overrun. Log the error
831 spin_unlock(&s->lock);
835 adc->nextIn += 2*adc->dma_fragsize;
836 if (adc->nextIn >= adc->rawbuf + adc->dmasize)
837 adc->nextIn -= adc->dmasize;
839 adc->count += 2*adc->dma_fragsize;
840 adc->total_bytes += 2*adc->dma_fragsize;
842 spin_unlock(&s->lock);
847 /* wake up anybody listening */
848 if (waitqueue_active(&adc->wait))
851 spin_unlock(&s->lock);
856 /* --------------------------------------------------------------------- */
858 static loff_t au1000_llseek(struct file *file, loff_t offset, int origin)
864 static int au1000_open_mixdev(struct inode *inode, struct file *file)
866 file->private_data = &au1000_state;
867 return nonseekable_open(inode, file);
870 static int au1000_release_mixdev(struct inode *inode, struct file *file)
875 static int mixdev_ioctl(struct ac97_codec *codec, unsigned int cmd,
878 return codec->mixer_ioctl(codec, cmd, arg);
881 static int au1000_ioctl_mixdev(struct inode *inode, struct file *file,
882 unsigned int cmd, unsigned long arg)
884 struct au1000_state *s = (struct au1000_state *)file->private_data;
885 struct ac97_codec *codec = &s->codec;
887 return mixdev_ioctl(codec, cmd, arg);
890 static /*const */ struct file_operations au1000_mixer_fops = {
891 .owner = THIS_MODULE,
892 .llseek = au1000_llseek,
893 .ioctl = au1000_ioctl_mixdev,
894 .open = au1000_open_mixdev,
895 .release = au1000_release_mixdev,
898 /* --------------------------------------------------------------------- */
900 static int drain_dac(struct au1000_state *s, int nonblock)
905 if (s->dma_dac.mapped || !s->dma_dac.ready || s->dma_dac.stopped)
909 spin_lock_irqsave(&s->lock, flags);
910 count = s->dma_dac.count;
911 spin_unlock_irqrestore(&s->lock, flags);
914 if (signal_pending(current))
918 tmo = 1000 * count / (s->no_vra ?
919 48000 : s->dma_dac.sample_rate);
920 tmo /= s->dma_dac.dma_bytes_per_sample;
923 if (signal_pending(current))
928 /* --------------------------------------------------------------------- */
930 static inline u8 S16_TO_U8(s16 ch)
932 return (u8) (ch >> 8) + 0x80;
934 static inline s16 U8_TO_S16(u8 ch)
936 return (s16) (ch - 0x80) << 8;
940 * Translates user samples to dma buffer suitable for AC'97 DAC data:
941 * If mono, copy left channel to right channel in dma buffer.
942 * If 8 bit samples, cvt to 16-bit before writing to dma buffer.
943 * If interpolating (no VRA), duplicate every audio frame src_factor times.
945 static int translate_from_user(struct dmabuf *db,
951 int interp_bytes_per_sample;
953 int mono = (db->num_channels == 1);
955 s16 ch, dmasample[6];
957 if (db->sample_size == 16 && !mono && db->src_factor == 1) {
958 // no translation necessary, just copy
959 if (copy_from_user(dmabuf, userbuf, dmacount))
964 interp_bytes_per_sample = db->dma_bytes_per_sample * db->src_factor;
965 num_samples = dmacount / interp_bytes_per_sample;
967 for (sample = 0; sample < num_samples; sample++) {
968 if (copy_from_user(usersample, userbuf,
969 db->user_bytes_per_sample)) {
970 dbg("%s: fault", __FUNCTION__);
974 for (i = 0; i < db->num_channels; i++) {
975 if (db->sample_size == 8)
976 ch = U8_TO_S16(usersample[i]);
978 ch = *((s16 *) (&usersample[i * 2]));
981 dmasample[i + 1] = ch; // right channel
984 // duplicate every audio frame src_factor times
985 for (i = 0; i < db->src_factor; i++)
986 memcpy(dmabuf, dmasample, db->dma_bytes_per_sample);
988 userbuf += db->user_bytes_per_sample;
989 dmabuf += interp_bytes_per_sample;
992 return num_samples * interp_bytes_per_sample;
996 * Translates AC'97 ADC samples to user buffer:
997 * If mono, send only left channel to user buffer.
998 * If 8 bit samples, cvt from 16 to 8 bit before writing to user buffer.
999 * If decimating (no VRA), skip over src_factor audio frames.
1001 static int translate_to_user(struct dmabuf *db,
1007 int interp_bytes_per_sample;
1009 int mono = (db->num_channels == 1);
1010 char usersample[12];
1012 if (db->sample_size == 16 && !mono && db->src_factor == 1) {
1013 // no translation necessary, just copy
1014 if (copy_to_user(userbuf, dmabuf, dmacount))
1019 interp_bytes_per_sample = db->dma_bytes_per_sample * db->src_factor;
1020 num_samples = dmacount / interp_bytes_per_sample;
1022 for (sample = 0; sample < num_samples; sample++) {
1023 for (i = 0; i < db->num_channels; i++) {
1024 if (db->sample_size == 8)
1026 S16_TO_U8(*((s16 *) (&dmabuf[i * 2])));
1028 *((s16 *) (&usersample[i * 2])) =
1029 *((s16 *) (&dmabuf[i * 2]));
1032 if (copy_to_user(userbuf, usersample,
1033 db->user_bytes_per_sample)) {
1034 dbg("%s: fault", __FUNCTION__);
1038 userbuf += db->user_bytes_per_sample;
1039 dmabuf += interp_bytes_per_sample;
1042 return num_samples * interp_bytes_per_sample;
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;
1057 if (bufptr + count > bufend) {
1058 int partial = (int) (bufend - bufptr);
1060 if ((cnt = translate_to_user(db, userbuf,
1061 bufptr, partial)) < 0)
1064 if ((cnt = translate_to_user(db, userbuf + partial,
1066 count - partial)) < 0)
1070 if ((cnt = translate_from_user(db, bufptr, userbuf,
1074 if ((cnt = translate_from_user(db, db->rawbuf,
1076 count - partial)) < 0)
1082 ret = translate_to_user(db, userbuf, bufptr, count);
1084 ret = translate_from_user(db, bufptr, userbuf, count);
1091 static ssize_t au1000_read(struct file *file, char *buffer,
1092 size_t count, loff_t *ppos)
1094 struct au1000_state *s = (struct au1000_state *)file->private_data;
1095 struct dmabuf *db = &s->dma_adc;
1096 DECLARE_WAITQUEUE(wait, current);
1098 unsigned long flags;
1099 int cnt, usercnt, avail;
1103 if (!access_ok(VERIFY_WRITE, buffer, count))
1107 count *= db->cnt_factor;
1110 add_wait_queue(&db->wait, &wait);
1113 // wait for samples in ADC dma buffer
1117 spin_lock_irqsave(&s->lock, flags);
1120 __set_current_state(TASK_INTERRUPTIBLE);
1121 spin_unlock_irqrestore(&s->lock, flags);
1123 if (file->f_flags & O_NONBLOCK) {
1130 if (signal_pending(current)) {
1137 } while (avail <= 0);
1139 // copy from nextOut to user
1140 if ((cnt = copy_dmabuf_user(db, buffer,
1142 avail : count, 1)) < 0) {
1148 spin_lock_irqsave(&s->lock, flags);
1151 if (db->nextOut >= db->rawbuf + db->dmasize)
1152 db->nextOut -= db->dmasize;
1153 spin_unlock_irqrestore(&s->lock, flags);
1156 usercnt = cnt / db->cnt_factor;
1159 } // while (count > 0)
1164 remove_wait_queue(&db->wait, &wait);
1165 set_current_state(TASK_RUNNING);
1169 static ssize_t au1000_write(struct file *file, const char *buffer,
1170 size_t count, loff_t * ppos)
1172 struct au1000_state *s = (struct au1000_state *)file->private_data;
1173 struct dmabuf *db = &s->dma_dac;
1174 DECLARE_WAITQUEUE(wait, current);
1176 unsigned long flags;
1177 int cnt, usercnt, avail;
1179 #ifdef AU1000_VERBOSE_DEBUG
1180 dbg("write: count=%d", count);
1185 if (!access_ok(VERIFY_READ, buffer, count))
1188 count *= db->cnt_factor;
1191 add_wait_queue(&db->wait, &wait);
1194 // wait for space in playback buffer
1196 spin_lock_irqsave(&s->lock, flags);
1197 avail = (int) db->dmasize - db->count;
1199 __set_current_state(TASK_INTERRUPTIBLE);
1200 spin_unlock_irqrestore(&s->lock, flags);
1202 if (file->f_flags & O_NONBLOCK) {
1209 if (signal_pending(current)) {
1216 } while (avail <= 0);
1218 // copy from user to nextIn
1219 if ((cnt = copy_dmabuf_user(db, (char *) buffer,
1221 avail : count, 0)) < 0) {
1227 spin_lock_irqsave(&s->lock, flags);
1230 if (db->nextIn >= db->rawbuf + db->dmasize)
1231 db->nextIn -= db->dmasize;
1232 spin_unlock_irqrestore(&s->lock, flags);
1237 usercnt = cnt / db->cnt_factor;
1240 } // while (count > 0)
1245 remove_wait_queue(&db->wait, &wait);
1246 set_current_state(TASK_RUNNING);
1251 /* No kernel lock - we have our own spinlock */
1252 static unsigned int au1000_poll(struct file *file,
1253 struct poll_table_struct *wait)
1255 struct au1000_state *s = (struct au1000_state *)file->private_data;
1256 unsigned long flags;
1257 unsigned int mask = 0;
1259 if (file->f_mode & FMODE_WRITE) {
1260 if (!s->dma_dac.ready)
1262 poll_wait(file, &s->dma_dac.wait, wait);
1264 if (file->f_mode & FMODE_READ) {
1265 if (!s->dma_adc.ready)
1267 poll_wait(file, &s->dma_adc.wait, wait);
1270 spin_lock_irqsave(&s->lock, flags);
1272 if (file->f_mode & FMODE_READ) {
1273 if (s->dma_adc.count >= (signed)s->dma_adc.dma_fragsize)
1274 mask |= POLLIN | POLLRDNORM;
1276 if (file->f_mode & FMODE_WRITE) {
1277 if (s->dma_dac.mapped) {
1278 if (s->dma_dac.count >=
1279 (signed)s->dma_dac.dma_fragsize)
1280 mask |= POLLOUT | POLLWRNORM;
1282 if ((signed) s->dma_dac.dmasize >=
1283 s->dma_dac.count + (signed)s->dma_dac.dma_fragsize)
1284 mask |= POLLOUT | POLLWRNORM;
1287 spin_unlock_irqrestore(&s->lock, flags);
1291 static int au1000_mmap(struct file *file, struct vm_area_struct *vma)
1293 struct au1000_state *s = (struct au1000_state *)file->private_data;
1298 dbg("%s", __FUNCTION__);
1302 if (vma->vm_flags & VM_WRITE)
1304 else if (vma->vm_flags & VM_READ)
1310 if (vma->vm_pgoff != 0) {
1314 size = vma->vm_end - vma->vm_start;
1315 if (size > (PAGE_SIZE << db->buforder)) {
1319 if (remap_pfn_range(vma, vma->vm_start, virt_to_phys(db->rawbuf),
1320 size, vma->vm_page_prot)) {
1324 vma->vm_flags &= ~VM_IO;
1333 #ifdef AU1000_VERBOSE_DEBUG
1334 static struct ioctl_str_t {
1338 {SNDCTL_DSP_RESET, "SNDCTL_DSP_RESET"},
1339 {SNDCTL_DSP_SYNC, "SNDCTL_DSP_SYNC"},
1340 {SNDCTL_DSP_SPEED, "SNDCTL_DSP_SPEED"},
1341 {SNDCTL_DSP_STEREO, "SNDCTL_DSP_STEREO"},
1342 {SNDCTL_DSP_GETBLKSIZE, "SNDCTL_DSP_GETBLKSIZE"},
1343 {SNDCTL_DSP_SAMPLESIZE, "SNDCTL_DSP_SAMPLESIZE"},
1344 {SNDCTL_DSP_CHANNELS, "SNDCTL_DSP_CHANNELS"},
1345 {SOUND_PCM_WRITE_CHANNELS, "SOUND_PCM_WRITE_CHANNELS"},
1346 {SOUND_PCM_WRITE_FILTER, "SOUND_PCM_WRITE_FILTER"},
1347 {SNDCTL_DSP_POST, "SNDCTL_DSP_POST"},
1348 {SNDCTL_DSP_SUBDIVIDE, "SNDCTL_DSP_SUBDIVIDE"},
1349 {SNDCTL_DSP_SETFRAGMENT, "SNDCTL_DSP_SETFRAGMENT"},
1350 {SNDCTL_DSP_GETFMTS, "SNDCTL_DSP_GETFMTS"},
1351 {SNDCTL_DSP_SETFMT, "SNDCTL_DSP_SETFMT"},
1352 {SNDCTL_DSP_GETOSPACE, "SNDCTL_DSP_GETOSPACE"},
1353 {SNDCTL_DSP_GETISPACE, "SNDCTL_DSP_GETISPACE"},
1354 {SNDCTL_DSP_NONBLOCK, "SNDCTL_DSP_NONBLOCK"},
1355 {SNDCTL_DSP_GETCAPS, "SNDCTL_DSP_GETCAPS"},
1356 {SNDCTL_DSP_GETTRIGGER, "SNDCTL_DSP_GETTRIGGER"},
1357 {SNDCTL_DSP_SETTRIGGER, "SNDCTL_DSP_SETTRIGGER"},
1358 {SNDCTL_DSP_GETIPTR, "SNDCTL_DSP_GETIPTR"},
1359 {SNDCTL_DSP_GETOPTR, "SNDCTL_DSP_GETOPTR"},
1360 {SNDCTL_DSP_MAPINBUF, "SNDCTL_DSP_MAPINBUF"},
1361 {SNDCTL_DSP_MAPOUTBUF, "SNDCTL_DSP_MAPOUTBUF"},
1362 {SNDCTL_DSP_SETSYNCRO, "SNDCTL_DSP_SETSYNCRO"},
1363 {SNDCTL_DSP_SETDUPLEX, "SNDCTL_DSP_SETDUPLEX"},
1364 {SNDCTL_DSP_GETODELAY, "SNDCTL_DSP_GETODELAY"},
1365 {SNDCTL_DSP_GETCHANNELMASK, "SNDCTL_DSP_GETCHANNELMASK"},
1366 {SNDCTL_DSP_BIND_CHANNEL, "SNDCTL_DSP_BIND_CHANNEL"},
1367 {OSS_GETVERSION, "OSS_GETVERSION"},
1368 {SOUND_PCM_READ_RATE, "SOUND_PCM_READ_RATE"},
1369 {SOUND_PCM_READ_CHANNELS, "SOUND_PCM_READ_CHANNELS"},
1370 {SOUND_PCM_READ_BITS, "SOUND_PCM_READ_BITS"},
1371 {SOUND_PCM_READ_FILTER, "SOUND_PCM_READ_FILTER"}
1375 // Need to hold a spin-lock before calling this!
1376 static int dma_count_done(struct dmabuf *db)
1381 return db->dma_fragsize - get_dma_residue(db->dmanr);
1385 static int au1000_ioctl(struct inode *inode, struct file *file,
1386 unsigned int cmd, unsigned long arg)
1388 struct au1000_state *s = (struct au1000_state *)file->private_data;
1389 unsigned long flags;
1390 audio_buf_info abinfo;
1393 int val, mapped, ret, diff;
1395 mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
1396 ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
1398 #ifdef AU1000_VERBOSE_DEBUG
1399 for (count=0; count<sizeof(ioctl_str)/sizeof(ioctl_str[0]); count++) {
1400 if (ioctl_str[count].cmd == cmd)
1403 if (count < sizeof(ioctl_str) / sizeof(ioctl_str[0]))
1404 dbg("ioctl %s, arg=0x%lx", ioctl_str[count].str, arg);
1406 dbg("ioctl 0x%x unknown, arg=0x%lx", cmd, arg);
1410 case OSS_GETVERSION:
1411 return put_user(SOUND_VERSION, (int *) arg);
1413 case SNDCTL_DSP_SYNC:
1414 if (file->f_mode & FMODE_WRITE)
1415 return drain_dac(s, file->f_flags & O_NONBLOCK);
1418 case SNDCTL_DSP_SETDUPLEX:
1421 case SNDCTL_DSP_GETCAPS:
1422 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME |
1423 DSP_CAP_TRIGGER | DSP_CAP_MMAP, (int *)arg);
1425 case SNDCTL_DSP_RESET:
1426 if (file->f_mode & FMODE_WRITE) {
1429 s->dma_dac.count = s->dma_dac.total_bytes = 0;
1430 s->dma_dac.nextIn = s->dma_dac.nextOut =
1433 if (file->f_mode & FMODE_READ) {
1436 s->dma_adc.count = s->dma_adc.total_bytes = 0;
1437 s->dma_adc.nextIn = s->dma_adc.nextOut =
1442 case SNDCTL_DSP_SPEED:
1443 if (get_user(val, (int *) arg))
1446 if (file->f_mode & FMODE_READ) {
1448 set_adc_rate(s, val);
1450 if (file->f_mode & FMODE_WRITE) {
1452 set_dac_rate(s, val);
1454 if (s->open_mode & FMODE_READ)
1455 if ((ret = prog_dmabuf_adc(s)))
1457 if (s->open_mode & FMODE_WRITE)
1458 if ((ret = prog_dmabuf_dac(s)))
1461 return put_user((file->f_mode & FMODE_READ) ?
1462 s->dma_adc.sample_rate :
1463 s->dma_dac.sample_rate,
1466 case SNDCTL_DSP_STEREO:
1467 if (get_user(val, (int *) arg))
1469 if (file->f_mode & FMODE_READ) {
1471 s->dma_adc.num_channels = val ? 2 : 1;
1472 if ((ret = prog_dmabuf_adc(s)))
1475 if (file->f_mode & FMODE_WRITE) {
1477 s->dma_dac.num_channels = val ? 2 : 1;
1478 if (s->codec_ext_caps & AC97_EXT_DACS) {
1479 // disable surround and center/lfe in AC'97
1480 u16 ext_stat = rdcodec(&s->codec,
1481 AC97_EXTENDED_STATUS);
1482 wrcodec(&s->codec, AC97_EXTENDED_STATUS,
1483 ext_stat | (AC97_EXTSTAT_PRI |
1487 if ((ret = prog_dmabuf_dac(s)))
1492 case SNDCTL_DSP_CHANNELS:
1493 if (get_user(val, (int *) arg))
1496 if (file->f_mode & FMODE_READ) {
1497 if (val < 0 || val > 2)
1500 s->dma_adc.num_channels = val;
1501 if ((ret = prog_dmabuf_adc(s)))
1504 if (file->f_mode & FMODE_WRITE) {
1513 if (!(s->codec_ext_caps &
1518 if ((s->codec_ext_caps &
1519 AC97_EXT_DACS) != AC97_EXT_DACS)
1528 (s->codec_ext_caps & AC97_EXT_DACS)) {
1529 // disable surround and center/lfe
1530 // channels in AC'97
1533 AC97_EXTENDED_STATUS);
1535 AC97_EXTENDED_STATUS,
1536 ext_stat | (AC97_EXTSTAT_PRI |
1539 } else if (val >= 4) {
1540 // enable surround, center/lfe
1541 // channels in AC'97
1544 AC97_EXTENDED_STATUS);
1545 ext_stat &= ~AC97_EXTSTAT_PRJ;
1548 ~(AC97_EXTSTAT_PRI |
1551 AC97_EXTENDED_STATUS,
1555 s->dma_dac.num_channels = val;
1556 if ((ret = prog_dmabuf_dac(s)))
1560 return put_user(val, (int *) arg);
1562 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
1563 return put_user(AFMT_S16_LE | AFMT_U8, (int *) arg);
1565 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt */
1566 if (get_user(val, (int *) arg))
1568 if (val != AFMT_QUERY) {
1569 if (file->f_mode & FMODE_READ) {
1571 if (val == AFMT_S16_LE)
1572 s->dma_adc.sample_size = 16;
1575 s->dma_adc.sample_size = 8;
1577 if ((ret = prog_dmabuf_adc(s)))
1580 if (file->f_mode & FMODE_WRITE) {
1582 if (val == AFMT_S16_LE)
1583 s->dma_dac.sample_size = 16;
1586 s->dma_dac.sample_size = 8;
1588 if ((ret = prog_dmabuf_dac(s)))
1592 if (file->f_mode & FMODE_READ)
1593 val = (s->dma_adc.sample_size == 16) ?
1594 AFMT_S16_LE : AFMT_U8;
1596 val = (s->dma_dac.sample_size == 16) ?
1597 AFMT_S16_LE : AFMT_U8;
1599 return put_user(val, (int *) arg);
1601 case SNDCTL_DSP_POST:
1604 case SNDCTL_DSP_GETTRIGGER:
1606 spin_lock_irqsave(&s->lock, flags);
1607 if (file->f_mode & FMODE_READ && !s->dma_adc.stopped)
1608 val |= PCM_ENABLE_INPUT;
1609 if (file->f_mode & FMODE_WRITE && !s->dma_dac.stopped)
1610 val |= PCM_ENABLE_OUTPUT;
1611 spin_unlock_irqrestore(&s->lock, flags);
1612 return put_user(val, (int *) arg);
1614 case SNDCTL_DSP_SETTRIGGER:
1615 if (get_user(val, (int *) arg))
1617 if (file->f_mode & FMODE_READ) {
1618 if (val & PCM_ENABLE_INPUT)
1623 if (file->f_mode & FMODE_WRITE) {
1624 if (val & PCM_ENABLE_OUTPUT)
1631 case SNDCTL_DSP_GETOSPACE:
1632 if (!(file->f_mode & FMODE_WRITE))
1634 abinfo.fragsize = s->dma_dac.fragsize;
1635 spin_lock_irqsave(&s->lock, flags);
1636 count = s->dma_dac.count;
1637 count -= dma_count_done(&s->dma_dac);
1638 spin_unlock_irqrestore(&s->lock, flags);
1641 abinfo.bytes = (s->dma_dac.dmasize - count) /
1642 s->dma_dac.cnt_factor;
1643 abinfo.fragstotal = s->dma_dac.numfrag;
1644 abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
1645 #ifdef AU1000_VERBOSE_DEBUG
1646 dbg("bytes=%d, fragments=%d", abinfo.bytes, abinfo.fragments);
1648 return copy_to_user((void *) arg, &abinfo,
1649 sizeof(abinfo)) ? -EFAULT : 0;
1651 case SNDCTL_DSP_GETISPACE:
1652 if (!(file->f_mode & FMODE_READ))
1654 abinfo.fragsize = s->dma_adc.fragsize;
1655 spin_lock_irqsave(&s->lock, flags);
1656 count = s->dma_adc.count;
1657 count += dma_count_done(&s->dma_adc);
1658 spin_unlock_irqrestore(&s->lock, flags);
1661 abinfo.bytes = count / s->dma_adc.cnt_factor;
1662 abinfo.fragstotal = s->dma_adc.numfrag;
1663 abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;
1664 return copy_to_user((void *) arg, &abinfo,
1665 sizeof(abinfo)) ? -EFAULT : 0;
1667 case SNDCTL_DSP_NONBLOCK:
1668 file->f_flags |= O_NONBLOCK;
1671 case SNDCTL_DSP_GETODELAY:
1672 if (!(file->f_mode & FMODE_WRITE))
1674 spin_lock_irqsave(&s->lock, flags);
1675 count = s->dma_dac.count;
1676 count -= dma_count_done(&s->dma_dac);
1677 spin_unlock_irqrestore(&s->lock, flags);
1680 count /= s->dma_dac.cnt_factor;
1681 return put_user(count, (int *) arg);
1683 case SNDCTL_DSP_GETIPTR:
1684 if (!(file->f_mode & FMODE_READ))
1686 spin_lock_irqsave(&s->lock, flags);
1687 cinfo.bytes = s->dma_adc.total_bytes;
1688 count = s->dma_adc.count;
1689 if (!s->dma_adc.stopped) {
1690 diff = dma_count_done(&s->dma_adc);
1692 cinfo.bytes += diff;
1693 cinfo.ptr = virt_to_phys(s->dma_adc.nextIn) + diff -
1696 cinfo.ptr = virt_to_phys(s->dma_adc.nextIn) -
1698 if (s->dma_adc.mapped)
1699 s->dma_adc.count &= (s->dma_adc.dma_fragsize-1);
1700 spin_unlock_irqrestore(&s->lock, flags);
1703 cinfo.blocks = count >> s->dma_adc.fragshift;
1704 return copy_to_user((void *) arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
1706 case SNDCTL_DSP_GETOPTR:
1707 if (!(file->f_mode & FMODE_READ))
1709 spin_lock_irqsave(&s->lock, flags);
1710 cinfo.bytes = s->dma_dac.total_bytes;
1711 count = s->dma_dac.count;
1712 if (!s->dma_dac.stopped) {
1713 diff = dma_count_done(&s->dma_dac);
1715 cinfo.bytes += diff;
1716 cinfo.ptr = virt_to_phys(s->dma_dac.nextOut) + diff -
1719 cinfo.ptr = virt_to_phys(s->dma_dac.nextOut) -
1721 if (s->dma_dac.mapped)
1722 s->dma_dac.count &= (s->dma_dac.dma_fragsize-1);
1723 spin_unlock_irqrestore(&s->lock, flags);
1726 cinfo.blocks = count >> s->dma_dac.fragshift;
1727 return copy_to_user((void *) arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
1729 case SNDCTL_DSP_GETBLKSIZE:
1730 if (file->f_mode & FMODE_WRITE)
1731 return put_user(s->dma_dac.fragsize, (int *) arg);
1733 return put_user(s->dma_adc.fragsize, (int *) arg);
1735 case SNDCTL_DSP_SETFRAGMENT:
1736 if (get_user(val, (int *) arg))
1738 if (file->f_mode & FMODE_READ) {
1740 s->dma_adc.ossfragshift = val & 0xffff;
1741 s->dma_adc.ossmaxfrags = (val >> 16) & 0xffff;
1742 if (s->dma_adc.ossfragshift < 4)
1743 s->dma_adc.ossfragshift = 4;
1744 if (s->dma_adc.ossfragshift > 15)
1745 s->dma_adc.ossfragshift = 15;
1746 if (s->dma_adc.ossmaxfrags < 4)
1747 s->dma_adc.ossmaxfrags = 4;
1748 if ((ret = prog_dmabuf_adc(s)))
1751 if (file->f_mode & FMODE_WRITE) {
1753 s->dma_dac.ossfragshift = val & 0xffff;
1754 s->dma_dac.ossmaxfrags = (val >> 16) & 0xffff;
1755 if (s->dma_dac.ossfragshift < 4)
1756 s->dma_dac.ossfragshift = 4;
1757 if (s->dma_dac.ossfragshift > 15)
1758 s->dma_dac.ossfragshift = 15;
1759 if (s->dma_dac.ossmaxfrags < 4)
1760 s->dma_dac.ossmaxfrags = 4;
1761 if ((ret = prog_dmabuf_dac(s)))
1766 case SNDCTL_DSP_SUBDIVIDE:
1767 if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
1768 (file->f_mode & FMODE_WRITE && s->dma_dac.subdivision))
1770 if (get_user(val, (int *) arg))
1772 if (val != 1 && val != 2 && val != 4)
1774 if (file->f_mode & FMODE_READ) {
1776 s->dma_adc.subdivision = val;
1777 if ((ret = prog_dmabuf_adc(s)))
1780 if (file->f_mode & FMODE_WRITE) {
1782 s->dma_dac.subdivision = val;
1783 if ((ret = prog_dmabuf_dac(s)))
1788 case SOUND_PCM_READ_RATE:
1789 return put_user((file->f_mode & FMODE_READ) ?
1790 s->dma_adc.sample_rate :
1791 s->dma_dac.sample_rate,
1794 case SOUND_PCM_READ_CHANNELS:
1795 if (file->f_mode & FMODE_READ)
1796 return put_user(s->dma_adc.num_channels, (int *)arg);
1798 return put_user(s->dma_dac.num_channels, (int *)arg);
1800 case SOUND_PCM_READ_BITS:
1801 if (file->f_mode & FMODE_READ)
1802 return put_user(s->dma_adc.sample_size, (int *)arg);
1804 return put_user(s->dma_dac.sample_size, (int *)arg);
1806 case SOUND_PCM_WRITE_FILTER:
1807 case SNDCTL_DSP_SETSYNCRO:
1808 case SOUND_PCM_READ_FILTER:
1812 return mixdev_ioctl(&s->codec, cmd, arg);
1816 static int au1000_open(struct inode *inode, struct file *file)
1818 int minor = iminor(inode);
1819 DECLARE_WAITQUEUE(wait, current);
1820 struct au1000_state *s = &au1000_state;
1823 #ifdef AU1000_VERBOSE_DEBUG
1824 if (file->f_flags & O_NONBLOCK)
1825 dbg("%s: non-blocking", __FUNCTION__);
1827 dbg("%s: blocking", __FUNCTION__);
1830 file->private_data = s;
1831 /* wait for device to become free */
1833 while (s->open_mode & file->f_mode) {
1834 if (file->f_flags & O_NONBLOCK) {
1838 add_wait_queue(&s->open_wait, &wait);
1839 __set_current_state(TASK_INTERRUPTIBLE);
1842 remove_wait_queue(&s->open_wait, &wait);
1843 set_current_state(TASK_RUNNING);
1844 if (signal_pending(current))
1845 return -ERESTARTSYS;
1852 if (file->f_mode & FMODE_READ) {
1853 s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags =
1854 s->dma_adc.subdivision = s->dma_adc.total_bytes = 0;
1855 s->dma_adc.num_channels = 1;
1856 s->dma_adc.sample_size = 8;
1857 set_adc_rate(s, 8000);
1858 if ((minor & 0xf) == SND_DEV_DSP16)
1859 s->dma_adc.sample_size = 16;
1862 if (file->f_mode & FMODE_WRITE) {
1863 s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags =
1864 s->dma_dac.subdivision = s->dma_dac.total_bytes = 0;
1865 s->dma_dac.num_channels = 1;
1866 s->dma_dac.sample_size = 8;
1867 set_dac_rate(s, 8000);
1868 if ((minor & 0xf) == SND_DEV_DSP16)
1869 s->dma_dac.sample_size = 16;
1872 if (file->f_mode & FMODE_READ) {
1873 if ((ret = prog_dmabuf_adc(s)))
1876 if (file->f_mode & FMODE_WRITE) {
1877 if ((ret = prog_dmabuf_dac(s)))
1881 s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
1883 init_MUTEX(&s->sem);
1884 return nonseekable_open(inode, file);
1887 static int au1000_release(struct inode *inode, struct file *file)
1889 struct au1000_state *s = (struct au1000_state *)file->private_data;
1893 if (file->f_mode & FMODE_WRITE) {
1895 drain_dac(s, file->f_flags & O_NONBLOCK);
1900 if (file->f_mode & FMODE_WRITE) {
1902 dealloc_dmabuf(s, &s->dma_dac);
1904 if (file->f_mode & FMODE_READ) {
1906 dealloc_dmabuf(s, &s->dma_adc);
1908 s->open_mode &= ((~file->f_mode) & (FMODE_READ|FMODE_WRITE));
1910 wake_up(&s->open_wait);
1915 static /*const */ struct file_operations au1000_audio_fops = {
1916 .owner = THIS_MODULE,
1917 .llseek = au1000_llseek,
1918 .read = au1000_read,
1919 .write = au1000_write,
1920 .poll = au1000_poll,
1921 .ioctl = au1000_ioctl,
1922 .mmap = au1000_mmap,
1923 .open = au1000_open,
1924 .release = au1000_release,
1928 /* --------------------------------------------------------------------- */
1931 /* --------------------------------------------------------------------- */
1934 * for debugging purposes, we'll create a proc device that dumps the
1939 static int proc_au1000_dump(char *buf, char **start, off_t fpos,
1940 int length, int *eof, void *data)
1942 struct au1000_state *s = &au1000_state;
1945 /* print out header */
1946 len += sprintf(buf + len, "\n\t\tAU1000 Audio Debug\n\n");
1948 // print out digital controller state
1949 len += sprintf(buf + len, "AU1000 Audio Controller registers\n");
1950 len += sprintf(buf + len, "---------------------------------\n");
1951 len += sprintf (buf + len, "AC97C_CONFIG = %08x\n",
1952 au_readl(AC97C_CONFIG));
1953 len += sprintf (buf + len, "AC97C_STATUS = %08x\n",
1954 au_readl(AC97C_STATUS));
1955 len += sprintf (buf + len, "AC97C_CNTRL = %08x\n",
1956 au_readl(AC97C_CNTRL));
1958 /* print out CODEC state */
1959 len += sprintf(buf + len, "\nAC97 CODEC registers\n");
1960 len += sprintf(buf + len, "----------------------\n");
1961 for (cnt = 0; cnt <= 0x7e; cnt += 2)
1962 len += sprintf(buf + len, "reg %02x = %04x\n",
1963 cnt, rdcodec(&s->codec, cnt));
1970 *start = buf + fpos;
1971 if ((len -= fpos) > length)
1977 #endif /* AU1000_DEBUG */
1979 /* --------------------------------------------------------------------- */
1981 MODULE_AUTHOR("Monta Vista Software, stevel@mvista.com");
1982 MODULE_DESCRIPTION("Au1000 Audio Driver");
1984 /* --------------------------------------------------------------------- */
1986 static int __devinit au1000_probe(void)
1988 struct au1000_state *s = &au1000_state;
1994 memset(s, 0, sizeof(struct au1000_state));
1996 init_waitqueue_head(&s->dma_adc.wait);
1997 init_waitqueue_head(&s->dma_dac.wait);
1998 init_waitqueue_head(&s->open_wait);
1999 init_MUTEX(&s->open_sem);
2000 spin_lock_init(&s->lock);
2001 s->codec.private_data = s;
2003 s->codec.codec_read = rdcodec;
2004 s->codec.codec_write = wrcodec;
2005 s->codec.codec_wait = waitcodec;
2007 if (!request_mem_region(CPHYSADDR(AC97C_CONFIG),
2008 0x14, AU1000_MODULE_NAME)) {
2009 err("AC'97 ports in use");
2012 // Allocate the DMA Channels
2013 if ((s->dma_dac.dmanr = request_au1000_dma(DMA_ID_AC97C_TX,
2016 SA_INTERRUPT, s)) < 0) {
2017 err("Can't get DAC DMA");
2020 if ((s->dma_adc.dmanr = request_au1000_dma(DMA_ID_AC97C_RX,
2023 SA_INTERRUPT, s)) < 0) {
2024 err("Can't get ADC DMA");
2028 info("DAC: DMA%d/IRQ%d, ADC: DMA%d/IRQ%d",
2029 s->dma_dac.dmanr, get_dma_done_irq(s->dma_dac.dmanr),
2030 s->dma_adc.dmanr, get_dma_done_irq(s->dma_adc.dmanr));
2032 // enable DMA coherency in read/write DMA channels
2033 set_dma_mode(s->dma_dac.dmanr,
2034 get_dma_mode(s->dma_dac.dmanr) & ~DMA_NC);
2035 set_dma_mode(s->dma_adc.dmanr,
2036 get_dma_mode(s->dma_adc.dmanr) & ~DMA_NC);
2038 /* register devices */
2040 if ((s->dev_audio = register_sound_dsp(&au1000_audio_fops, -1)) < 0)
2042 if ((s->codec.dev_mixer =
2043 register_sound_mixer(&au1000_mixer_fops, -1)) < 0)
2047 /* intialize the debug proc device */
2048 s->ps = create_proc_read_entry(AU1000_MODULE_NAME, 0, NULL,
2049 proc_au1000_dump, NULL);
2050 #endif /* AU1000_DEBUG */
2052 // configure pins for AC'97
2053 au_writel(au_readl(SYS_PINFUNC) & ~0x02, SYS_PINFUNC);
2055 // Assert reset for 10msec to the AC'97 controller, and enable clock
2056 au_writel(AC97C_RS | AC97C_CE, AC97C_CNTRL);
2058 au_writel(AC97C_CE, AC97C_CNTRL);
2059 au1000_delay(10); // wait for clock to stabilize
2061 /* cold reset the AC'97 */
2062 au_writel(AC97C_RESET, AC97C_CONFIG);
2064 au_writel(0, AC97C_CONFIG);
2065 /* need to delay around 500msec(bleech) to give
2066 some CODECs enough time to wakeup */
2069 /* warm reset the AC'97 to start the bitclk */
2070 au_writel(AC97C_SG | AC97C_SYNC, AC97C_CONFIG);
2072 au_writel(0, AC97C_CONFIG);
2075 if (!ac97_probe_codec(&s->codec))
2078 s->codec_base_caps = rdcodec(&s->codec, AC97_RESET);
2079 s->codec_ext_caps = rdcodec(&s->codec, AC97_EXTENDED_ID);
2080 info("AC'97 Base/Extended ID = %04x/%04x",
2081 s->codec_base_caps, s->codec_ext_caps);
2084 * On the Pb1000, audio playback is on the AUX_OUT
2085 * channel (which defaults to LNLVL_OUT in AC'97
2086 * rev 2.2) so make sure this channel is listed
2087 * as supported (soundcard.h calls this channel
2088 * ALTPCM). ac97_codec.c does not handle detection
2089 * of this channel correctly.
2091 s->codec.supported_mixers |= SOUND_MASK_ALTPCM;
2093 * Now set AUX_OUT's default volume.
2096 mixdev_ioctl(&s->codec, SOUND_MIXER_WRITE_ALTPCM,
2097 (unsigned long) &val);
2099 if (!(s->codec_ext_caps & AC97_EXTID_VRA)) {
2100 // codec does not support VRA
2103 // Boot option says disable VRA
2104 u16 ac97_extstat = rdcodec(&s->codec, AC97_EXTENDED_STATUS);
2105 wrcodec(&s->codec, AC97_EXTENDED_STATUS,
2106 ac97_extstat & ~AC97_EXTSTAT_VRA);
2110 info("no VRA, interpolating and decimating");
2112 /* set mic to be the recording source */
2113 val = SOUND_MASK_MIC;
2114 mixdev_ioctl(&s->codec, SOUND_MIXER_WRITE_RECSRC,
2115 (unsigned long) &val);
2118 sprintf(proc_str, "driver/%s/%d/ac97", AU1000_MODULE_NAME,
2120 s->ac97_ps = create_proc_read_entry (proc_str, 0, NULL,
2121 ac97_read_proc, &s->codec);
2124 #ifdef CONFIG_MIPS_XXS1500
2126 wrcodec(&s->codec, AC97_POWER_CONTROL,
2127 rdcodec(&s->codec, AC97_POWER_CONTROL) & ~0x8000);
2128 /* mute a number of signals which seem to be causing problems
2131 wrcodec(&s->codec, AC97_PCBEEP_VOL, 0x8000);
2132 wrcodec(&s->codec, AC97_PHONE_VOL, 0x8008);
2133 wrcodec(&s->codec, AC97_MIC_VOL, 0x8008);
2134 wrcodec(&s->codec, AC97_LINEIN_VOL, 0x8808);
2135 wrcodec(&s->codec, AC97_CD_VOL, 0x8808);
2136 wrcodec(&s->codec, AC97_VIDEO_VOL, 0x8808);
2137 wrcodec(&s->codec, AC97_AUX_VOL, 0x8808);
2138 wrcodec(&s->codec, AC97_PCMOUT_VOL, 0x0808);
2139 wrcodec(&s->codec, AC97_GENERAL_PURPOSE, 0x2000);
2145 unregister_sound_mixer(s->codec.dev_mixer);
2147 unregister_sound_dsp(s->dev_audio);
2149 free_au1000_dma(s->dma_adc.dmanr);
2151 free_au1000_dma(s->dma_dac.dmanr);
2153 release_mem_region(CPHYSADDR(AC97C_CONFIG), 0x14);
2157 static void au1000_remove(void)
2159 struct au1000_state *s = &au1000_state;
2165 remove_proc_entry(AU1000_MODULE_NAME, NULL);
2166 #endif /* AU1000_DEBUG */
2168 free_au1000_dma(s->dma_adc.dmanr);
2169 free_au1000_dma(s->dma_dac.dmanr);
2170 release_mem_region(CPHYSADDR(AC97C_CONFIG), 0x14);
2171 unregister_sound_dsp(s->dev_audio);
2172 unregister_sound_mixer(s->codec.dev_mixer);
2175 static int __init init_au1000(void)
2177 info("stevel@mvista.com, built " __TIME__ " on " __DATE__);
2178 return au1000_probe();
2181 static void __exit cleanup_au1000(void)
2187 module_init(init_au1000);
2188 module_exit(cleanup_au1000);
2190 /* --------------------------------------------------------------------- */
2194 static int __init au1000_setup(char *options)
2198 if (!options || !*options)
2201 while ((this_opt = strsep(&options, ","))) {
2204 if (!strncmp(this_opt, "vra", 3)) {
2212 __setup("au1000_audio=", au1000_setup);