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>
71 #include <linux/mutex.h>
74 #include <asm/uaccess.h>
75 #include <asm/mach-au1x00/au1000.h>
76 #include <asm/mach-au1x00/au1000_dma.h>
78 /* --------------------------------------------------------------------- */
80 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
82 #undef AU1000_VERBOSE_DEBUG
84 #define AU1000_MODULE_NAME "Au1000 audio"
85 #define PFX AU1000_MODULE_NAME
88 #define dbg(format, arg...) printk(KERN_DEBUG PFX ": " format "\n" , ## arg)
90 #define dbg(format, arg...) do {} while (0)
92 #define err(format, arg...) printk(KERN_ERR PFX ": " format "\n" , ## arg)
93 #define info(format, arg...) printk(KERN_INFO PFX ": " format "\n" , ## arg)
94 #define warn(format, arg...) printk(KERN_WARNING PFX ": " format "\n" , ## arg)
98 #define POLL_COUNT 0x5000
99 #define AC97_EXT_DACS (AC97_EXTID_SDAC | AC97_EXTID_CDAC | AC97_EXTID_LDAC)
102 static int vra = 0; // 0 = no VRA, 1 = use VRA if codec supports it
103 module_param(vra, bool, 0);
104 MODULE_PARM_DESC(vra, "if 1 use VRA if codec supports it");
107 /* --------------------------------------------------------------------- */
109 struct au1000_state {
110 /* soundcore stuff */
114 /* debug /proc entry */
115 struct proc_dir_entry *ps;
116 struct proc_dir_entry *ac97_ps;
117 #endif /* AU1000_DEBUG */
119 struct ac97_codec codec;
120 unsigned codec_base_caps;// AC'97 reg 00h, "Reset Register"
121 unsigned codec_ext_caps; // AC'97 reg 28h, "Extended Audio ID"
122 int no_vra; // do not use VRA
125 struct mutex open_mutex;
128 wait_queue_head_t open_wait;
131 unsigned int dmanr; // DMA Channel number
132 unsigned sample_rate; // Hz
133 unsigned src_factor; // SRC interp/decimation (no vra)
134 unsigned sample_size; // 8 or 16
135 int num_channels; // 1 = mono, 2 = stereo, 4, 6
136 int dma_bytes_per_sample;// DMA bytes per audio sample frame
137 int user_bytes_per_sample;// User bytes per audio sample frame
138 int cnt_factor; // user-to-DMA bytes per audio
143 unsigned numfrag; // # of DMA fragments in DMA buffer
145 void *nextIn; // ptr to next-in to DMA buffer
146 void *nextOut;// ptr to next-out from DMA buffer
147 int count; // current byte count in DMA buffer
148 unsigned total_bytes; // total bytes written or read
149 unsigned error; // over/underrun
150 wait_queue_head_t wait;
151 /* redundant, but makes calculations easier */
152 unsigned fragsize; // user perception of fragment size
153 unsigned dma_fragsize; // DMA (real) fragment size
154 unsigned dmasize; // Total DMA buffer size
155 // (mult. of DMA fragsize)
160 unsigned ossfragshift;
162 unsigned subdivision;
166 /* --------------------------------------------------------------------- */
169 static inline unsigned ld2(unsigned int x)
194 /* --------------------------------------------------------------------- */
196 static void au1000_delay(int msec)
204 tmo = jiffies + (msec * HZ) / 1000;
206 tmo2 = tmo - jiffies;
209 schedule_timeout(tmo2);
214 /* --------------------------------------------------------------------- */
216 static u16 rdcodec(struct ac97_codec *codec, u8 addr)
218 struct au1000_state *s = (struct au1000_state *)codec->private_data;
224 spin_lock_irqsave(&s->lock, flags);
226 for (i = 0; i < POLL_COUNT; i++)
227 if (!(au_readl(AC97C_STATUS) & AC97C_CP))
230 err("rdcodec: codec cmd pending expired!");
232 cmd = (u32) addr & AC97C_INDEX_MASK;
233 cmd |= AC97C_READ; // read command
234 au_writel(cmd, AC97C_CMD);
236 /* now wait for the data */
237 for (i = 0; i < POLL_COUNT; i++)
238 if (!(au_readl(AC97C_STATUS) & AC97C_CP))
240 if (i == POLL_COUNT) {
241 err("rdcodec: read poll expired!");
245 data = au_readl(AC97C_CMD) & 0xffff;
247 spin_unlock_irqrestore(&s->lock, flags);
253 static void wrcodec(struct ac97_codec *codec, u8 addr, u16 data)
255 struct au1000_state *s = (struct au1000_state *)codec->private_data;
260 spin_lock_irqsave(&s->lock, flags);
262 for (i = 0; i < POLL_COUNT; i++)
263 if (!(au_readl(AC97C_STATUS) & AC97C_CP))
266 err("wrcodec: codec cmd pending expired!");
268 cmd = (u32) addr & AC97C_INDEX_MASK;
269 cmd &= ~AC97C_READ; // write command
270 cmd |= ((u32) data << AC97C_WD_BIT); // OR in the data word
271 au_writel(cmd, AC97C_CMD);
273 spin_unlock_irqrestore(&s->lock, flags);
276 static void waitcodec(struct ac97_codec *codec)
281 /* codec_wait is used to wait for a ready state after
285 // first poll the CODEC_READY tag bit
286 for (i = 0; i < POLL_COUNT; i++)
287 if (au_readl(AC97C_STATUS) & AC97C_READY)
289 if (i == POLL_COUNT) {
290 err("waitcodec: CODEC_READY poll expired!");
293 // get AC'97 powerdown control/status register
294 temp = rdcodec(codec, AC97_POWER_CONTROL);
296 // If anything is powered down, power'em up
299 wrcodec(codec, AC97_POWER_CONTROL, 0);
302 temp = rdcodec(codec, AC97_POWER_CONTROL);
305 // Check if Codec REF,ANL,DAC,ADC ready
306 if ((temp & 0x7f0f) != 0x000f)
307 err("codec reg 26 status (0x%x) not ready!!", temp);
311 /* --------------------------------------------------------------------- */
313 /* stop the ADC before calling */
314 static void set_adc_rate(struct au1000_state *s, unsigned rate)
316 struct dmabuf *adc = &s->dma_adc;
317 struct dmabuf *dac = &s->dma_dac;
318 unsigned adc_rate, dac_rate;
323 adc->src_factor = ((96000 / rate) + 1) >> 1;
324 adc->sample_rate = 48000 / adc->src_factor;
330 ac97_extstat = rdcodec(&s->codec, AC97_EXTENDED_STATUS);
332 rate = rate > 48000 ? 48000 : rate;
335 wrcodec(&s->codec, AC97_EXTENDED_STATUS,
336 ac97_extstat | AC97_EXTSTAT_VRA);
337 // now write the sample rate
338 wrcodec(&s->codec, AC97_PCM_LR_ADC_RATE, (u16) rate);
339 // read it back for actual supported rate
340 adc_rate = rdcodec(&s->codec, AC97_PCM_LR_ADC_RATE);
342 #ifdef AU1000_VERBOSE_DEBUG
343 dbg("%s: set to %d Hz", __FUNCTION__, adc_rate);
346 // some codec's don't allow unequal DAC and ADC rates, in which case
347 // writing one rate reg actually changes both.
348 dac_rate = rdcodec(&s->codec, AC97_PCM_FRONT_DAC_RATE);
349 if (dac->num_channels > 2)
350 wrcodec(&s->codec, AC97_PCM_SURR_DAC_RATE, dac_rate);
351 if (dac->num_channels > 4)
352 wrcodec(&s->codec, AC97_PCM_LFE_DAC_RATE, dac_rate);
354 adc->sample_rate = adc_rate;
355 dac->sample_rate = dac_rate;
358 /* stop the DAC before calling */
359 static void set_dac_rate(struct au1000_state *s, unsigned rate)
361 struct dmabuf *dac = &s->dma_dac;
362 struct dmabuf *adc = &s->dma_adc;
363 unsigned adc_rate, dac_rate;
368 dac->src_factor = ((96000 / rate) + 1) >> 1;
369 dac->sample_rate = 48000 / dac->src_factor;
375 ac97_extstat = rdcodec(&s->codec, AC97_EXTENDED_STATUS);
377 rate = rate > 48000 ? 48000 : rate;
380 wrcodec(&s->codec, AC97_EXTENDED_STATUS,
381 ac97_extstat | AC97_EXTSTAT_VRA);
382 // now write the sample rate
383 wrcodec(&s->codec, AC97_PCM_FRONT_DAC_RATE, (u16) rate);
384 // I don't support different sample rates for multichannel,
385 // so make these channels the same.
386 if (dac->num_channels > 2)
387 wrcodec(&s->codec, AC97_PCM_SURR_DAC_RATE, (u16) rate);
388 if (dac->num_channels > 4)
389 wrcodec(&s->codec, AC97_PCM_LFE_DAC_RATE, (u16) rate);
390 // read it back for actual supported rate
391 dac_rate = rdcodec(&s->codec, AC97_PCM_FRONT_DAC_RATE);
393 #ifdef AU1000_VERBOSE_DEBUG
394 dbg("%s: set to %d Hz", __FUNCTION__, dac_rate);
397 // some codec's don't allow unequal DAC and ADC rates, in which case
398 // writing one rate reg actually changes both.
399 adc_rate = rdcodec(&s->codec, AC97_PCM_LR_ADC_RATE);
401 dac->sample_rate = dac_rate;
402 adc->sample_rate = adc_rate;
405 static void stop_dac(struct au1000_state *s)
407 struct dmabuf *db = &s->dma_dac;
413 spin_lock_irqsave(&s->lock, flags);
415 disable_dma(db->dmanr);
419 spin_unlock_irqrestore(&s->lock, flags);
422 static void stop_adc(struct au1000_state *s)
424 struct dmabuf *db = &s->dma_adc;
430 spin_lock_irqsave(&s->lock, flags);
432 disable_dma(db->dmanr);
436 spin_unlock_irqrestore(&s->lock, flags);
440 static void set_xmit_slots(int num_channels)
442 u32 ac97_config = au_readl(AC97C_CONFIG) & ~AC97C_XMIT_SLOTS_MASK;
444 switch (num_channels) {
446 case 2: // stereo, slots 3,4
447 ac97_config |= (0x3 << AC97C_XMIT_SLOTS_BIT);
449 case 4: // stereo with surround, slots 3,4,7,8
450 ac97_config |= (0x33 << AC97C_XMIT_SLOTS_BIT);
452 case 6: // stereo with surround and center/LFE, slots 3,4,6,7,8,9
453 ac97_config |= (0x7b << AC97C_XMIT_SLOTS_BIT);
457 au_writel(ac97_config, AC97C_CONFIG);
460 static void set_recv_slots(int num_channels)
462 u32 ac97_config = au_readl(AC97C_CONFIG) & ~AC97C_RECV_SLOTS_MASK;
465 * Always enable slots 3 and 4 (stereo). Slot 6 is
466 * optional Mic ADC, which I don't support yet.
468 ac97_config |= (0x3 << AC97C_RECV_SLOTS_BIT);
470 au_writel(ac97_config, AC97C_CONFIG);
473 static void start_dac(struct au1000_state *s)
475 struct dmabuf *db = &s->dma_dac;
477 unsigned long buf1, buf2;
482 spin_lock_irqsave(&s->lock, flags);
484 au_readl(AC97C_STATUS); // read status to clear sticky bits
486 // reset Buffer 1 and 2 pointers to nextOut and nextOut+dma_fragsize
487 buf1 = virt_to_phys(db->nextOut);
488 buf2 = buf1 + db->dma_fragsize;
489 if (buf2 >= db->dmaaddr + db->dmasize)
492 set_xmit_slots(db->num_channels);
495 if (get_dma_active_buffer(db->dmanr) == 0) {
496 clear_dma_done0(db->dmanr); // clear DMA done bit
497 set_dma_addr0(db->dmanr, buf1);
498 set_dma_addr1(db->dmanr, buf2);
500 clear_dma_done1(db->dmanr); // clear DMA done bit
501 set_dma_addr1(db->dmanr, buf1);
502 set_dma_addr0(db->dmanr, buf2);
504 set_dma_count(db->dmanr, db->dma_fragsize>>1);
505 enable_dma_buffers(db->dmanr);
507 start_dma(db->dmanr);
509 #ifdef AU1000_VERBOSE_DEBUG
510 dump_au1000_dma_channel(db->dmanr);
515 spin_unlock_irqrestore(&s->lock, flags);
518 static void start_adc(struct au1000_state *s)
520 struct dmabuf *db = &s->dma_adc;
522 unsigned long buf1, buf2;
527 spin_lock_irqsave(&s->lock, flags);
529 au_readl(AC97C_STATUS); // read status to clear sticky bits
531 // reset Buffer 1 and 2 pointers to nextIn and nextIn+dma_fragsize
532 buf1 = virt_to_phys(db->nextIn);
533 buf2 = buf1 + db->dma_fragsize;
534 if (buf2 >= db->dmaaddr + db->dmasize)
537 set_recv_slots(db->num_channels);
540 if (get_dma_active_buffer(db->dmanr) == 0) {
541 clear_dma_done0(db->dmanr); // clear DMA done bit
542 set_dma_addr0(db->dmanr, buf1);
543 set_dma_addr1(db->dmanr, buf2);
545 clear_dma_done1(db->dmanr); // clear DMA done bit
546 set_dma_addr1(db->dmanr, buf1);
547 set_dma_addr0(db->dmanr, buf2);
549 set_dma_count(db->dmanr, db->dma_fragsize>>1);
550 enable_dma_buffers(db->dmanr);
552 start_dma(db->dmanr);
554 #ifdef AU1000_VERBOSE_DEBUG
555 dump_au1000_dma_channel(db->dmanr);
560 spin_unlock_irqrestore(&s->lock, flags);
563 /* --------------------------------------------------------------------- */
565 #define DMABUF_DEFAULTORDER (17-PAGE_SHIFT)
566 #define DMABUF_MINORDER 1
568 static inline void dealloc_dmabuf(struct au1000_state *s, struct dmabuf *db)
570 struct page *page, *pend;
573 /* undo marking the pages as reserved */
574 pend = virt_to_page(db->rawbuf +
575 (PAGE_SIZE << db->buforder) - 1);
576 for (page = virt_to_page(db->rawbuf); page <= pend; page++)
577 ClearPageReserved(page);
578 dma_free_noncoherent(NULL,
579 PAGE_SIZE << db->buforder,
583 db->rawbuf = db->nextIn = db->nextOut = NULL;
584 db->mapped = db->ready = 0;
587 static int prog_dmabuf(struct au1000_state *s, struct dmabuf *db)
590 unsigned user_bytes_per_sec;
592 struct page *page, *pend;
593 unsigned rate = db->sample_rate;
596 db->ready = db->mapped = 0;
597 for (order = DMABUF_DEFAULTORDER;
598 order >= DMABUF_MINORDER; order--)
599 if ((db->rawbuf = dma_alloc_noncoherent(NULL,
606 db->buforder = order;
607 /* now mark the pages as reserved;
608 otherwise remap_pfn_range doesn't do what we want */
609 pend = virt_to_page(db->rawbuf +
610 (PAGE_SIZE << db->buforder) - 1);
611 for (page = virt_to_page(db->rawbuf); page <= pend; page++)
612 SetPageReserved(page);
616 if (db->sample_size == 8)
618 if (db->num_channels == 1)
620 db->cnt_factor *= db->src_factor;
623 db->nextIn = db->nextOut = db->rawbuf;
625 db->user_bytes_per_sample = (db->sample_size>>3) * db->num_channels;
626 db->dma_bytes_per_sample = 2 * ((db->num_channels == 1) ?
627 2 : db->num_channels);
629 user_bytes_per_sec = rate * db->user_bytes_per_sample;
630 bufs = PAGE_SIZE << db->buforder;
631 if (db->ossfragshift) {
632 if ((1000 << db->ossfragshift) < user_bytes_per_sec)
633 db->fragshift = ld2(user_bytes_per_sec/1000);
635 db->fragshift = db->ossfragshift;
637 db->fragshift = ld2(user_bytes_per_sec / 100 /
638 (db->subdivision ? db->subdivision : 1));
639 if (db->fragshift < 3)
643 db->fragsize = 1 << db->fragshift;
644 db->dma_fragsize = db->fragsize * db->cnt_factor;
645 db->numfrag = bufs / db->dma_fragsize;
647 while (db->numfrag < 4 && db->fragshift > 3) {
649 db->fragsize = 1 << db->fragshift;
650 db->dma_fragsize = db->fragsize * db->cnt_factor;
651 db->numfrag = bufs / db->dma_fragsize;
654 if (db->ossmaxfrags >= 4 && db->ossmaxfrags < db->numfrag)
655 db->numfrag = db->ossmaxfrags;
657 db->dmasize = db->dma_fragsize * db->numfrag;
658 memset(db->rawbuf, 0, bufs);
660 #ifdef AU1000_VERBOSE_DEBUG
661 dbg("rate=%d, samplesize=%d, channels=%d",
662 rate, db->sample_size, db->num_channels);
663 dbg("fragsize=%d, cnt_factor=%d, dma_fragsize=%d",
664 db->fragsize, db->cnt_factor, db->dma_fragsize);
665 dbg("numfrag=%d, dmasize=%d", db->numfrag, db->dmasize);
672 static inline int prog_dmabuf_adc(struct au1000_state *s)
675 return prog_dmabuf(s, &s->dma_adc);
679 static inline int prog_dmabuf_dac(struct au1000_state *s)
682 return prog_dmabuf(s, &s->dma_dac);
686 /* hold spinlock for the following */
687 static irqreturn_t dac_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
689 struct au1000_state *s = (struct au1000_state *) dev_id;
690 struct dmabuf *dac = &s->dma_dac;
691 unsigned long newptr;
692 u32 ac97c_stat, buff_done;
694 ac97c_stat = au_readl(AC97C_STATUS);
695 #ifdef AU1000_VERBOSE_DEBUG
696 if (ac97c_stat & (AC97C_XU | AC97C_XO | AC97C_TE))
697 dbg("AC97C status = 0x%08x", ac97c_stat);
700 if ((buff_done = get_dma_buffer_done(dac->dmanr)) == 0) {
701 /* fastpath out, to ease interrupt sharing */
707 if (buff_done != (DMA_D0 | DMA_D1)) {
708 dac->nextOut += dac->dma_fragsize;
709 if (dac->nextOut >= dac->rawbuf + dac->dmasize)
710 dac->nextOut -= dac->dmasize;
712 /* update playback pointers */
713 newptr = virt_to_phys(dac->nextOut) + dac->dma_fragsize;
714 if (newptr >= dac->dmaaddr + dac->dmasize)
715 newptr -= dac->dmasize;
717 dac->count -= dac->dma_fragsize;
718 dac->total_bytes += dac->dma_fragsize;
720 if (dac->count <= 0) {
721 #ifdef AU1000_VERBOSE_DEBUG
724 spin_unlock(&s->lock);
728 dac->nextIn = dac->nextOut;
729 } else if (buff_done == DMA_D0) {
730 clear_dma_done0(dac->dmanr); // clear DMA done bit
731 set_dma_count0(dac->dmanr, dac->dma_fragsize>>1);
732 set_dma_addr0(dac->dmanr, newptr);
733 enable_dma_buffer0(dac->dmanr); // reenable
735 clear_dma_done1(dac->dmanr); // clear DMA done bit
736 set_dma_count1(dac->dmanr, dac->dma_fragsize>>1);
737 set_dma_addr1(dac->dmanr, newptr);
738 enable_dma_buffer1(dac->dmanr); // reenable
741 // both done bits set, we missed an interrupt
742 spin_unlock(&s->lock);
746 dac->nextOut += 2*dac->dma_fragsize;
747 if (dac->nextOut >= dac->rawbuf + dac->dmasize)
748 dac->nextOut -= dac->dmasize;
750 dac->count -= 2*dac->dma_fragsize;
751 dac->total_bytes += 2*dac->dma_fragsize;
753 if (dac->count > 0) {
754 spin_unlock(&s->lock);
760 /* wake up anybody listening */
761 if (waitqueue_active(&dac->wait))
764 spin_unlock(&s->lock);
770 static irqreturn_t adc_dma_interrupt(int irq, void *dev_id, struct pt_regs *regs)
772 struct au1000_state *s = (struct au1000_state *) dev_id;
773 struct dmabuf *adc = &s->dma_adc;
774 unsigned long newptr;
775 u32 ac97c_stat, buff_done;
777 ac97c_stat = au_readl(AC97C_STATUS);
778 #ifdef AU1000_VERBOSE_DEBUG
779 if (ac97c_stat & (AC97C_RU | AC97C_RO))
780 dbg("AC97C status = 0x%08x", ac97c_stat);
783 if ((buff_done = get_dma_buffer_done(adc->dmanr)) == 0) {
784 /* fastpath out, to ease interrupt sharing */
790 if (buff_done != (DMA_D0 | DMA_D1)) {
791 if (adc->count + adc->dma_fragsize > adc->dmasize) {
792 // Overrun. Stop ADC and log the error
793 spin_unlock(&s->lock);
800 adc->nextIn += adc->dma_fragsize;
801 if (adc->nextIn >= adc->rawbuf + adc->dmasize)
802 adc->nextIn -= adc->dmasize;
804 /* update capture pointers */
805 newptr = virt_to_phys(adc->nextIn) + adc->dma_fragsize;
806 if (newptr >= adc->dmaaddr + adc->dmasize)
807 newptr -= adc->dmasize;
809 adc->count += adc->dma_fragsize;
810 adc->total_bytes += adc->dma_fragsize;
812 if (buff_done == DMA_D0) {
813 clear_dma_done0(adc->dmanr); // clear DMA done bit
814 set_dma_count0(adc->dmanr, adc->dma_fragsize>>1);
815 set_dma_addr0(adc->dmanr, newptr);
816 enable_dma_buffer0(adc->dmanr); // reenable
818 clear_dma_done1(adc->dmanr); // clear DMA done bit
819 set_dma_count1(adc->dmanr, adc->dma_fragsize>>1);
820 set_dma_addr1(adc->dmanr, newptr);
821 enable_dma_buffer1(adc->dmanr); // reenable
824 // both done bits set, we missed an interrupt
825 spin_unlock(&s->lock);
829 if (adc->count + 2*adc->dma_fragsize > adc->dmasize) {
830 // Overrun. Log the error
833 spin_unlock(&s->lock);
837 adc->nextIn += 2*adc->dma_fragsize;
838 if (adc->nextIn >= adc->rawbuf + adc->dmasize)
839 adc->nextIn -= adc->dmasize;
841 adc->count += 2*adc->dma_fragsize;
842 adc->total_bytes += 2*adc->dma_fragsize;
844 spin_unlock(&s->lock);
849 /* wake up anybody listening */
850 if (waitqueue_active(&adc->wait))
853 spin_unlock(&s->lock);
858 /* --------------------------------------------------------------------- */
860 static loff_t au1000_llseek(struct file *file, loff_t offset, int origin)
866 static int au1000_open_mixdev(struct inode *inode, struct file *file)
868 file->private_data = &au1000_state;
869 return nonseekable_open(inode, file);
872 static int au1000_release_mixdev(struct inode *inode, struct file *file)
877 static int mixdev_ioctl(struct ac97_codec *codec, unsigned int cmd,
880 return codec->mixer_ioctl(codec, cmd, arg);
883 static int au1000_ioctl_mixdev(struct inode *inode, struct file *file,
884 unsigned int cmd, unsigned long arg)
886 struct au1000_state *s = (struct au1000_state *)file->private_data;
887 struct ac97_codec *codec = &s->codec;
889 return mixdev_ioctl(codec, cmd, arg);
892 static /*const */ struct file_operations au1000_mixer_fops = {
893 .owner = THIS_MODULE,
894 .llseek = au1000_llseek,
895 .ioctl = au1000_ioctl_mixdev,
896 .open = au1000_open_mixdev,
897 .release = au1000_release_mixdev,
900 /* --------------------------------------------------------------------- */
902 static int drain_dac(struct au1000_state *s, int nonblock)
907 if (s->dma_dac.mapped || !s->dma_dac.ready || s->dma_dac.stopped)
911 spin_lock_irqsave(&s->lock, flags);
912 count = s->dma_dac.count;
913 spin_unlock_irqrestore(&s->lock, flags);
916 if (signal_pending(current))
920 tmo = 1000 * count / (s->no_vra ?
921 48000 : s->dma_dac.sample_rate);
922 tmo /= s->dma_dac.dma_bytes_per_sample;
925 if (signal_pending(current))
930 /* --------------------------------------------------------------------- */
932 static inline u8 S16_TO_U8(s16 ch)
934 return (u8) (ch >> 8) + 0x80;
936 static inline s16 U8_TO_S16(u8 ch)
938 return (s16) (ch - 0x80) << 8;
942 * Translates user samples to dma buffer suitable for AC'97 DAC data:
943 * If mono, copy left channel to right channel in dma buffer.
944 * If 8 bit samples, cvt to 16-bit before writing to dma buffer.
945 * If interpolating (no VRA), duplicate every audio frame src_factor times.
947 static int translate_from_user(struct dmabuf *db,
953 int interp_bytes_per_sample;
955 int mono = (db->num_channels == 1);
957 s16 ch, dmasample[6];
959 if (db->sample_size == 16 && !mono && db->src_factor == 1) {
960 // no translation necessary, just copy
961 if (copy_from_user(dmabuf, userbuf, dmacount))
966 interp_bytes_per_sample = db->dma_bytes_per_sample * db->src_factor;
967 num_samples = dmacount / interp_bytes_per_sample;
969 for (sample = 0; sample < num_samples; sample++) {
970 if (copy_from_user(usersample, userbuf,
971 db->user_bytes_per_sample)) {
972 dbg("%s: fault", __FUNCTION__);
976 for (i = 0; i < db->num_channels; i++) {
977 if (db->sample_size == 8)
978 ch = U8_TO_S16(usersample[i]);
980 ch = *((s16 *) (&usersample[i * 2]));
983 dmasample[i + 1] = ch; // right channel
986 // duplicate every audio frame src_factor times
987 for (i = 0; i < db->src_factor; i++)
988 memcpy(dmabuf, dmasample, db->dma_bytes_per_sample);
990 userbuf += db->user_bytes_per_sample;
991 dmabuf += interp_bytes_per_sample;
994 return num_samples * interp_bytes_per_sample;
998 * Translates AC'97 ADC samples to user buffer:
999 * If mono, send only left channel to user buffer.
1000 * If 8 bit samples, cvt from 16 to 8 bit before writing to user buffer.
1001 * If decimating (no VRA), skip over src_factor audio frames.
1003 static int translate_to_user(struct dmabuf *db,
1009 int interp_bytes_per_sample;
1011 int mono = (db->num_channels == 1);
1012 char usersample[12];
1014 if (db->sample_size == 16 && !mono && db->src_factor == 1) {
1015 // no translation necessary, just copy
1016 if (copy_to_user(userbuf, dmabuf, dmacount))
1021 interp_bytes_per_sample = db->dma_bytes_per_sample * db->src_factor;
1022 num_samples = dmacount / interp_bytes_per_sample;
1024 for (sample = 0; sample < num_samples; sample++) {
1025 for (i = 0; i < db->num_channels; i++) {
1026 if (db->sample_size == 8)
1028 S16_TO_U8(*((s16 *) (&dmabuf[i * 2])));
1030 *((s16 *) (&usersample[i * 2])) =
1031 *((s16 *) (&dmabuf[i * 2]));
1034 if (copy_to_user(userbuf, usersample,
1035 db->user_bytes_per_sample)) {
1036 dbg("%s: fault", __FUNCTION__);
1040 userbuf += db->user_bytes_per_sample;
1041 dmabuf += interp_bytes_per_sample;
1044 return num_samples * interp_bytes_per_sample;
1048 * Copy audio data to/from user buffer from/to dma buffer, taking care
1049 * that we wrap when reading/writing the dma buffer. Returns actual byte
1050 * count written to or read from the dma buffer.
1052 static int copy_dmabuf_user(struct dmabuf *db, char* userbuf,
1053 int count, int to_user)
1055 char *bufptr = to_user ? db->nextOut : db->nextIn;
1056 char *bufend = db->rawbuf + db->dmasize;
1059 if (bufptr + count > bufend) {
1060 int partial = (int) (bufend - bufptr);
1062 if ((cnt = translate_to_user(db, userbuf,
1063 bufptr, partial)) < 0)
1066 if ((cnt = translate_to_user(db, userbuf + partial,
1068 count - partial)) < 0)
1072 if ((cnt = translate_from_user(db, bufptr, userbuf,
1076 if ((cnt = translate_from_user(db, db->rawbuf,
1078 count - partial)) < 0)
1084 ret = translate_to_user(db, userbuf, bufptr, count);
1086 ret = translate_from_user(db, bufptr, userbuf, count);
1093 static ssize_t au1000_read(struct file *file, char *buffer,
1094 size_t count, loff_t *ppos)
1096 struct au1000_state *s = (struct au1000_state *)file->private_data;
1097 struct dmabuf *db = &s->dma_adc;
1098 DECLARE_WAITQUEUE(wait, current);
1100 unsigned long flags;
1101 int cnt, usercnt, avail;
1105 if (!access_ok(VERIFY_WRITE, buffer, count))
1109 count *= db->cnt_factor;
1111 mutex_lock(&s->sem);
1112 add_wait_queue(&db->wait, &wait);
1115 // wait for samples in ADC dma buffer
1119 spin_lock_irqsave(&s->lock, flags);
1122 __set_current_state(TASK_INTERRUPTIBLE);
1123 spin_unlock_irqrestore(&s->lock, flags);
1125 if (file->f_flags & O_NONBLOCK) {
1130 mutex_unlock(&s->sem);
1132 if (signal_pending(current)) {
1137 mutex_lock(&s->sem);
1139 } while (avail <= 0);
1141 // copy from nextOut to user
1142 if ((cnt = copy_dmabuf_user(db, buffer,
1144 avail : count, 1)) < 0) {
1150 spin_lock_irqsave(&s->lock, flags);
1153 if (db->nextOut >= db->rawbuf + db->dmasize)
1154 db->nextOut -= db->dmasize;
1155 spin_unlock_irqrestore(&s->lock, flags);
1158 usercnt = cnt / db->cnt_factor;
1161 } // while (count > 0)
1164 mutex_unlock(&s->sem);
1166 remove_wait_queue(&db->wait, &wait);
1167 set_current_state(TASK_RUNNING);
1171 static ssize_t au1000_write(struct file *file, const char *buffer,
1172 size_t count, loff_t * ppos)
1174 struct au1000_state *s = (struct au1000_state *)file->private_data;
1175 struct dmabuf *db = &s->dma_dac;
1176 DECLARE_WAITQUEUE(wait, current);
1178 unsigned long flags;
1179 int cnt, usercnt, avail;
1181 #ifdef AU1000_VERBOSE_DEBUG
1182 dbg("write: count=%d", count);
1187 if (!access_ok(VERIFY_READ, buffer, count))
1190 count *= db->cnt_factor;
1192 mutex_lock(&s->sem);
1193 add_wait_queue(&db->wait, &wait);
1196 // wait for space in playback buffer
1198 spin_lock_irqsave(&s->lock, flags);
1199 avail = (int) db->dmasize - db->count;
1201 __set_current_state(TASK_INTERRUPTIBLE);
1202 spin_unlock_irqrestore(&s->lock, flags);
1204 if (file->f_flags & O_NONBLOCK) {
1209 mutex_unlock(&s->sem);
1211 if (signal_pending(current)) {
1216 mutex_lock(&s->sem);
1218 } while (avail <= 0);
1220 // copy from user to nextIn
1221 if ((cnt = copy_dmabuf_user(db, (char *) buffer,
1223 avail : count, 0)) < 0) {
1229 spin_lock_irqsave(&s->lock, flags);
1232 if (db->nextIn >= db->rawbuf + db->dmasize)
1233 db->nextIn -= db->dmasize;
1234 spin_unlock_irqrestore(&s->lock, flags);
1239 usercnt = cnt / db->cnt_factor;
1242 } // while (count > 0)
1245 mutex_unlock(&s->sem);
1247 remove_wait_queue(&db->wait, &wait);
1248 set_current_state(TASK_RUNNING);
1253 /* No kernel lock - we have our own spinlock */
1254 static unsigned int au1000_poll(struct file *file,
1255 struct poll_table_struct *wait)
1257 struct au1000_state *s = (struct au1000_state *)file->private_data;
1258 unsigned long flags;
1259 unsigned int mask = 0;
1261 if (file->f_mode & FMODE_WRITE) {
1262 if (!s->dma_dac.ready)
1264 poll_wait(file, &s->dma_dac.wait, wait);
1266 if (file->f_mode & FMODE_READ) {
1267 if (!s->dma_adc.ready)
1269 poll_wait(file, &s->dma_adc.wait, wait);
1272 spin_lock_irqsave(&s->lock, flags);
1274 if (file->f_mode & FMODE_READ) {
1275 if (s->dma_adc.count >= (signed)s->dma_adc.dma_fragsize)
1276 mask |= POLLIN | POLLRDNORM;
1278 if (file->f_mode & FMODE_WRITE) {
1279 if (s->dma_dac.mapped) {
1280 if (s->dma_dac.count >=
1281 (signed)s->dma_dac.dma_fragsize)
1282 mask |= POLLOUT | POLLWRNORM;
1284 if ((signed) s->dma_dac.dmasize >=
1285 s->dma_dac.count + (signed)s->dma_dac.dma_fragsize)
1286 mask |= POLLOUT | POLLWRNORM;
1289 spin_unlock_irqrestore(&s->lock, flags);
1293 static int au1000_mmap(struct file *file, struct vm_area_struct *vma)
1295 struct au1000_state *s = (struct au1000_state *)file->private_data;
1300 dbg("%s", __FUNCTION__);
1303 mutex_lock(&s->sem);
1304 if (vma->vm_flags & VM_WRITE)
1306 else if (vma->vm_flags & VM_READ)
1312 if (vma->vm_pgoff != 0) {
1316 size = vma->vm_end - vma->vm_start;
1317 if (size > (PAGE_SIZE << db->buforder)) {
1321 if (remap_pfn_range(vma, vma->vm_start, virt_to_phys(db->rawbuf),
1322 size, vma->vm_page_prot)) {
1326 vma->vm_flags &= ~VM_IO;
1329 mutex_unlock(&s->sem);
1335 #ifdef AU1000_VERBOSE_DEBUG
1336 static struct ioctl_str_t {
1340 {SNDCTL_DSP_RESET, "SNDCTL_DSP_RESET"},
1341 {SNDCTL_DSP_SYNC, "SNDCTL_DSP_SYNC"},
1342 {SNDCTL_DSP_SPEED, "SNDCTL_DSP_SPEED"},
1343 {SNDCTL_DSP_STEREO, "SNDCTL_DSP_STEREO"},
1344 {SNDCTL_DSP_GETBLKSIZE, "SNDCTL_DSP_GETBLKSIZE"},
1345 {SNDCTL_DSP_SAMPLESIZE, "SNDCTL_DSP_SAMPLESIZE"},
1346 {SNDCTL_DSP_CHANNELS, "SNDCTL_DSP_CHANNELS"},
1347 {SOUND_PCM_WRITE_CHANNELS, "SOUND_PCM_WRITE_CHANNELS"},
1348 {SOUND_PCM_WRITE_FILTER, "SOUND_PCM_WRITE_FILTER"},
1349 {SNDCTL_DSP_POST, "SNDCTL_DSP_POST"},
1350 {SNDCTL_DSP_SUBDIVIDE, "SNDCTL_DSP_SUBDIVIDE"},
1351 {SNDCTL_DSP_SETFRAGMENT, "SNDCTL_DSP_SETFRAGMENT"},
1352 {SNDCTL_DSP_GETFMTS, "SNDCTL_DSP_GETFMTS"},
1353 {SNDCTL_DSP_SETFMT, "SNDCTL_DSP_SETFMT"},
1354 {SNDCTL_DSP_GETOSPACE, "SNDCTL_DSP_GETOSPACE"},
1355 {SNDCTL_DSP_GETISPACE, "SNDCTL_DSP_GETISPACE"},
1356 {SNDCTL_DSP_NONBLOCK, "SNDCTL_DSP_NONBLOCK"},
1357 {SNDCTL_DSP_GETCAPS, "SNDCTL_DSP_GETCAPS"},
1358 {SNDCTL_DSP_GETTRIGGER, "SNDCTL_DSP_GETTRIGGER"},
1359 {SNDCTL_DSP_SETTRIGGER, "SNDCTL_DSP_SETTRIGGER"},
1360 {SNDCTL_DSP_GETIPTR, "SNDCTL_DSP_GETIPTR"},
1361 {SNDCTL_DSP_GETOPTR, "SNDCTL_DSP_GETOPTR"},
1362 {SNDCTL_DSP_MAPINBUF, "SNDCTL_DSP_MAPINBUF"},
1363 {SNDCTL_DSP_MAPOUTBUF, "SNDCTL_DSP_MAPOUTBUF"},
1364 {SNDCTL_DSP_SETSYNCRO, "SNDCTL_DSP_SETSYNCRO"},
1365 {SNDCTL_DSP_SETDUPLEX, "SNDCTL_DSP_SETDUPLEX"},
1366 {SNDCTL_DSP_GETODELAY, "SNDCTL_DSP_GETODELAY"},
1367 {SNDCTL_DSP_GETCHANNELMASK, "SNDCTL_DSP_GETCHANNELMASK"},
1368 {SNDCTL_DSP_BIND_CHANNEL, "SNDCTL_DSP_BIND_CHANNEL"},
1369 {OSS_GETVERSION, "OSS_GETVERSION"},
1370 {SOUND_PCM_READ_RATE, "SOUND_PCM_READ_RATE"},
1371 {SOUND_PCM_READ_CHANNELS, "SOUND_PCM_READ_CHANNELS"},
1372 {SOUND_PCM_READ_BITS, "SOUND_PCM_READ_BITS"},
1373 {SOUND_PCM_READ_FILTER, "SOUND_PCM_READ_FILTER"}
1377 // Need to hold a spin-lock before calling this!
1378 static int dma_count_done(struct dmabuf *db)
1383 return db->dma_fragsize - get_dma_residue(db->dmanr);
1387 static int au1000_ioctl(struct inode *inode, struct file *file,
1388 unsigned int cmd, unsigned long arg)
1390 struct au1000_state *s = (struct au1000_state *)file->private_data;
1391 unsigned long flags;
1392 audio_buf_info abinfo;
1395 int val, mapped, ret, diff;
1397 mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
1398 ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
1400 #ifdef AU1000_VERBOSE_DEBUG
1401 for (count=0; count<sizeof(ioctl_str)/sizeof(ioctl_str[0]); count++) {
1402 if (ioctl_str[count].cmd == cmd)
1405 if (count < sizeof(ioctl_str) / sizeof(ioctl_str[0]))
1406 dbg("ioctl %s, arg=0x%lx", ioctl_str[count].str, arg);
1408 dbg("ioctl 0x%x unknown, arg=0x%lx", cmd, arg);
1412 case OSS_GETVERSION:
1413 return put_user(SOUND_VERSION, (int *) arg);
1415 case SNDCTL_DSP_SYNC:
1416 if (file->f_mode & FMODE_WRITE)
1417 return drain_dac(s, file->f_flags & O_NONBLOCK);
1420 case SNDCTL_DSP_SETDUPLEX:
1423 case SNDCTL_DSP_GETCAPS:
1424 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME |
1425 DSP_CAP_TRIGGER | DSP_CAP_MMAP, (int *)arg);
1427 case SNDCTL_DSP_RESET:
1428 if (file->f_mode & FMODE_WRITE) {
1431 s->dma_dac.count = s->dma_dac.total_bytes = 0;
1432 s->dma_dac.nextIn = s->dma_dac.nextOut =
1435 if (file->f_mode & FMODE_READ) {
1438 s->dma_adc.count = s->dma_adc.total_bytes = 0;
1439 s->dma_adc.nextIn = s->dma_adc.nextOut =
1444 case SNDCTL_DSP_SPEED:
1445 if (get_user(val, (int *) arg))
1448 if (file->f_mode & FMODE_READ) {
1450 set_adc_rate(s, val);
1452 if (file->f_mode & FMODE_WRITE) {
1454 set_dac_rate(s, val);
1456 if (s->open_mode & FMODE_READ)
1457 if ((ret = prog_dmabuf_adc(s)))
1459 if (s->open_mode & FMODE_WRITE)
1460 if ((ret = prog_dmabuf_dac(s)))
1463 return put_user((file->f_mode & FMODE_READ) ?
1464 s->dma_adc.sample_rate :
1465 s->dma_dac.sample_rate,
1468 case SNDCTL_DSP_STEREO:
1469 if (get_user(val, (int *) arg))
1471 if (file->f_mode & FMODE_READ) {
1473 s->dma_adc.num_channels = val ? 2 : 1;
1474 if ((ret = prog_dmabuf_adc(s)))
1477 if (file->f_mode & FMODE_WRITE) {
1479 s->dma_dac.num_channels = val ? 2 : 1;
1480 if (s->codec_ext_caps & AC97_EXT_DACS) {
1481 // disable surround and center/lfe in AC'97
1482 u16 ext_stat = rdcodec(&s->codec,
1483 AC97_EXTENDED_STATUS);
1484 wrcodec(&s->codec, AC97_EXTENDED_STATUS,
1485 ext_stat | (AC97_EXTSTAT_PRI |
1489 if ((ret = prog_dmabuf_dac(s)))
1494 case SNDCTL_DSP_CHANNELS:
1495 if (get_user(val, (int *) arg))
1498 if (file->f_mode & FMODE_READ) {
1499 if (val < 0 || val > 2)
1502 s->dma_adc.num_channels = val;
1503 if ((ret = prog_dmabuf_adc(s)))
1506 if (file->f_mode & FMODE_WRITE) {
1515 if (!(s->codec_ext_caps &
1520 if ((s->codec_ext_caps &
1521 AC97_EXT_DACS) != AC97_EXT_DACS)
1530 (s->codec_ext_caps & AC97_EXT_DACS)) {
1531 // disable surround and center/lfe
1532 // channels in AC'97
1535 AC97_EXTENDED_STATUS);
1537 AC97_EXTENDED_STATUS,
1538 ext_stat | (AC97_EXTSTAT_PRI |
1541 } else if (val >= 4) {
1542 // enable surround, center/lfe
1543 // channels in AC'97
1546 AC97_EXTENDED_STATUS);
1547 ext_stat &= ~AC97_EXTSTAT_PRJ;
1550 ~(AC97_EXTSTAT_PRI |
1553 AC97_EXTENDED_STATUS,
1557 s->dma_dac.num_channels = val;
1558 if ((ret = prog_dmabuf_dac(s)))
1562 return put_user(val, (int *) arg);
1564 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
1565 return put_user(AFMT_S16_LE | AFMT_U8, (int *) arg);
1567 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt */
1568 if (get_user(val, (int *) arg))
1570 if (val != AFMT_QUERY) {
1571 if (file->f_mode & FMODE_READ) {
1573 if (val == AFMT_S16_LE)
1574 s->dma_adc.sample_size = 16;
1577 s->dma_adc.sample_size = 8;
1579 if ((ret = prog_dmabuf_adc(s)))
1582 if (file->f_mode & FMODE_WRITE) {
1584 if (val == AFMT_S16_LE)
1585 s->dma_dac.sample_size = 16;
1588 s->dma_dac.sample_size = 8;
1590 if ((ret = prog_dmabuf_dac(s)))
1594 if (file->f_mode & FMODE_READ)
1595 val = (s->dma_adc.sample_size == 16) ?
1596 AFMT_S16_LE : AFMT_U8;
1598 val = (s->dma_dac.sample_size == 16) ?
1599 AFMT_S16_LE : AFMT_U8;
1601 return put_user(val, (int *) arg);
1603 case SNDCTL_DSP_POST:
1606 case SNDCTL_DSP_GETTRIGGER:
1608 spin_lock_irqsave(&s->lock, flags);
1609 if (file->f_mode & FMODE_READ && !s->dma_adc.stopped)
1610 val |= PCM_ENABLE_INPUT;
1611 if (file->f_mode & FMODE_WRITE && !s->dma_dac.stopped)
1612 val |= PCM_ENABLE_OUTPUT;
1613 spin_unlock_irqrestore(&s->lock, flags);
1614 return put_user(val, (int *) arg);
1616 case SNDCTL_DSP_SETTRIGGER:
1617 if (get_user(val, (int *) arg))
1619 if (file->f_mode & FMODE_READ) {
1620 if (val & PCM_ENABLE_INPUT)
1625 if (file->f_mode & FMODE_WRITE) {
1626 if (val & PCM_ENABLE_OUTPUT)
1633 case SNDCTL_DSP_GETOSPACE:
1634 if (!(file->f_mode & FMODE_WRITE))
1636 abinfo.fragsize = s->dma_dac.fragsize;
1637 spin_lock_irqsave(&s->lock, flags);
1638 count = s->dma_dac.count;
1639 count -= dma_count_done(&s->dma_dac);
1640 spin_unlock_irqrestore(&s->lock, flags);
1643 abinfo.bytes = (s->dma_dac.dmasize - count) /
1644 s->dma_dac.cnt_factor;
1645 abinfo.fragstotal = s->dma_dac.numfrag;
1646 abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
1647 #ifdef AU1000_VERBOSE_DEBUG
1648 dbg("bytes=%d, fragments=%d", abinfo.bytes, abinfo.fragments);
1650 return copy_to_user((void *) arg, &abinfo,
1651 sizeof(abinfo)) ? -EFAULT : 0;
1653 case SNDCTL_DSP_GETISPACE:
1654 if (!(file->f_mode & FMODE_READ))
1656 abinfo.fragsize = s->dma_adc.fragsize;
1657 spin_lock_irqsave(&s->lock, flags);
1658 count = s->dma_adc.count;
1659 count += dma_count_done(&s->dma_adc);
1660 spin_unlock_irqrestore(&s->lock, flags);
1663 abinfo.bytes = count / s->dma_adc.cnt_factor;
1664 abinfo.fragstotal = s->dma_adc.numfrag;
1665 abinfo.fragments = abinfo.bytes >> s->dma_adc.fragshift;
1666 return copy_to_user((void *) arg, &abinfo,
1667 sizeof(abinfo)) ? -EFAULT : 0;
1669 case SNDCTL_DSP_NONBLOCK:
1670 file->f_flags |= O_NONBLOCK;
1673 case SNDCTL_DSP_GETODELAY:
1674 if (!(file->f_mode & FMODE_WRITE))
1676 spin_lock_irqsave(&s->lock, flags);
1677 count = s->dma_dac.count;
1678 count -= dma_count_done(&s->dma_dac);
1679 spin_unlock_irqrestore(&s->lock, flags);
1682 count /= s->dma_dac.cnt_factor;
1683 return put_user(count, (int *) arg);
1685 case SNDCTL_DSP_GETIPTR:
1686 if (!(file->f_mode & FMODE_READ))
1688 spin_lock_irqsave(&s->lock, flags);
1689 cinfo.bytes = s->dma_adc.total_bytes;
1690 count = s->dma_adc.count;
1691 if (!s->dma_adc.stopped) {
1692 diff = dma_count_done(&s->dma_adc);
1694 cinfo.bytes += diff;
1695 cinfo.ptr = virt_to_phys(s->dma_adc.nextIn) + diff -
1698 cinfo.ptr = virt_to_phys(s->dma_adc.nextIn) -
1700 if (s->dma_adc.mapped)
1701 s->dma_adc.count &= (s->dma_adc.dma_fragsize-1);
1702 spin_unlock_irqrestore(&s->lock, flags);
1705 cinfo.blocks = count >> s->dma_adc.fragshift;
1706 return copy_to_user((void *) arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
1708 case SNDCTL_DSP_GETOPTR:
1709 if (!(file->f_mode & FMODE_READ))
1711 spin_lock_irqsave(&s->lock, flags);
1712 cinfo.bytes = s->dma_dac.total_bytes;
1713 count = s->dma_dac.count;
1714 if (!s->dma_dac.stopped) {
1715 diff = dma_count_done(&s->dma_dac);
1717 cinfo.bytes += diff;
1718 cinfo.ptr = virt_to_phys(s->dma_dac.nextOut) + diff -
1721 cinfo.ptr = virt_to_phys(s->dma_dac.nextOut) -
1723 if (s->dma_dac.mapped)
1724 s->dma_dac.count &= (s->dma_dac.dma_fragsize-1);
1725 spin_unlock_irqrestore(&s->lock, flags);
1728 cinfo.blocks = count >> s->dma_dac.fragshift;
1729 return copy_to_user((void *) arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
1731 case SNDCTL_DSP_GETBLKSIZE:
1732 if (file->f_mode & FMODE_WRITE)
1733 return put_user(s->dma_dac.fragsize, (int *) arg);
1735 return put_user(s->dma_adc.fragsize, (int *) arg);
1737 case SNDCTL_DSP_SETFRAGMENT:
1738 if (get_user(val, (int *) arg))
1740 if (file->f_mode & FMODE_READ) {
1742 s->dma_adc.ossfragshift = val & 0xffff;
1743 s->dma_adc.ossmaxfrags = (val >> 16) & 0xffff;
1744 if (s->dma_adc.ossfragshift < 4)
1745 s->dma_adc.ossfragshift = 4;
1746 if (s->dma_adc.ossfragshift > 15)
1747 s->dma_adc.ossfragshift = 15;
1748 if (s->dma_adc.ossmaxfrags < 4)
1749 s->dma_adc.ossmaxfrags = 4;
1750 if ((ret = prog_dmabuf_adc(s)))
1753 if (file->f_mode & FMODE_WRITE) {
1755 s->dma_dac.ossfragshift = val & 0xffff;
1756 s->dma_dac.ossmaxfrags = (val >> 16) & 0xffff;
1757 if (s->dma_dac.ossfragshift < 4)
1758 s->dma_dac.ossfragshift = 4;
1759 if (s->dma_dac.ossfragshift > 15)
1760 s->dma_dac.ossfragshift = 15;
1761 if (s->dma_dac.ossmaxfrags < 4)
1762 s->dma_dac.ossmaxfrags = 4;
1763 if ((ret = prog_dmabuf_dac(s)))
1768 case SNDCTL_DSP_SUBDIVIDE:
1769 if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision) ||
1770 (file->f_mode & FMODE_WRITE && s->dma_dac.subdivision))
1772 if (get_user(val, (int *) arg))
1774 if (val != 1 && val != 2 && val != 4)
1776 if (file->f_mode & FMODE_READ) {
1778 s->dma_adc.subdivision = val;
1779 if ((ret = prog_dmabuf_adc(s)))
1782 if (file->f_mode & FMODE_WRITE) {
1784 s->dma_dac.subdivision = val;
1785 if ((ret = prog_dmabuf_dac(s)))
1790 case SOUND_PCM_READ_RATE:
1791 return put_user((file->f_mode & FMODE_READ) ?
1792 s->dma_adc.sample_rate :
1793 s->dma_dac.sample_rate,
1796 case SOUND_PCM_READ_CHANNELS:
1797 if (file->f_mode & FMODE_READ)
1798 return put_user(s->dma_adc.num_channels, (int *)arg);
1800 return put_user(s->dma_dac.num_channels, (int *)arg);
1802 case SOUND_PCM_READ_BITS:
1803 if (file->f_mode & FMODE_READ)
1804 return put_user(s->dma_adc.sample_size, (int *)arg);
1806 return put_user(s->dma_dac.sample_size, (int *)arg);
1808 case SOUND_PCM_WRITE_FILTER:
1809 case SNDCTL_DSP_SETSYNCRO:
1810 case SOUND_PCM_READ_FILTER:
1814 return mixdev_ioctl(&s->codec, cmd, arg);
1818 static int au1000_open(struct inode *inode, struct file *file)
1820 int minor = iminor(inode);
1821 DECLARE_WAITQUEUE(wait, current);
1822 struct au1000_state *s = &au1000_state;
1825 #ifdef AU1000_VERBOSE_DEBUG
1826 if (file->f_flags & O_NONBLOCK)
1827 dbg("%s: non-blocking", __FUNCTION__);
1829 dbg("%s: blocking", __FUNCTION__);
1832 file->private_data = s;
1833 /* wait for device to become free */
1834 mutex_lock(&s->open_mutex);
1835 while (s->open_mode & file->f_mode) {
1836 if (file->f_flags & O_NONBLOCK) {
1837 mutex_unlock(&s->open_mutex);
1840 add_wait_queue(&s->open_wait, &wait);
1841 __set_current_state(TASK_INTERRUPTIBLE);
1842 mutex_unlock(&s->open_mutex);
1844 remove_wait_queue(&s->open_wait, &wait);
1845 set_current_state(TASK_RUNNING);
1846 if (signal_pending(current))
1847 return -ERESTARTSYS;
1848 mutex_lock(&s->open_mutex);
1854 if (file->f_mode & FMODE_READ) {
1855 s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags =
1856 s->dma_adc.subdivision = s->dma_adc.total_bytes = 0;
1857 s->dma_adc.num_channels = 1;
1858 s->dma_adc.sample_size = 8;
1859 set_adc_rate(s, 8000);
1860 if ((minor & 0xf) == SND_DEV_DSP16)
1861 s->dma_adc.sample_size = 16;
1864 if (file->f_mode & FMODE_WRITE) {
1865 s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags =
1866 s->dma_dac.subdivision = s->dma_dac.total_bytes = 0;
1867 s->dma_dac.num_channels = 1;
1868 s->dma_dac.sample_size = 8;
1869 set_dac_rate(s, 8000);
1870 if ((minor & 0xf) == SND_DEV_DSP16)
1871 s->dma_dac.sample_size = 16;
1874 if (file->f_mode & FMODE_READ) {
1875 if ((ret = prog_dmabuf_adc(s)))
1878 if (file->f_mode & FMODE_WRITE) {
1879 if ((ret = prog_dmabuf_dac(s)))
1883 s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
1884 mutex_unlock(&s->open_mutex);
1885 mutex_init(&s->sem);
1886 return nonseekable_open(inode, file);
1889 static int au1000_release(struct inode *inode, struct file *file)
1891 struct au1000_state *s = (struct au1000_state *)file->private_data;
1895 if (file->f_mode & FMODE_WRITE) {
1897 drain_dac(s, file->f_flags & O_NONBLOCK);
1901 mutex_lock(&s->open_mutex);
1902 if (file->f_mode & FMODE_WRITE) {
1904 dealloc_dmabuf(s, &s->dma_dac);
1906 if (file->f_mode & FMODE_READ) {
1908 dealloc_dmabuf(s, &s->dma_adc);
1910 s->open_mode &= ((~file->f_mode) & (FMODE_READ|FMODE_WRITE));
1911 mutex_unlock(&s->open_mutex);
1912 wake_up(&s->open_wait);
1917 static /*const */ struct file_operations au1000_audio_fops = {
1918 .owner = THIS_MODULE,
1919 .llseek = au1000_llseek,
1920 .read = au1000_read,
1921 .write = au1000_write,
1922 .poll = au1000_poll,
1923 .ioctl = au1000_ioctl,
1924 .mmap = au1000_mmap,
1925 .open = au1000_open,
1926 .release = au1000_release,
1930 /* --------------------------------------------------------------------- */
1933 /* --------------------------------------------------------------------- */
1936 * for debugging purposes, we'll create a proc device that dumps the
1941 static int proc_au1000_dump(char *buf, char **start, off_t fpos,
1942 int length, int *eof, void *data)
1944 struct au1000_state *s = &au1000_state;
1947 /* print out header */
1948 len += sprintf(buf + len, "\n\t\tAU1000 Audio Debug\n\n");
1950 // print out digital controller state
1951 len += sprintf(buf + len, "AU1000 Audio Controller registers\n");
1952 len += sprintf(buf + len, "---------------------------------\n");
1953 len += sprintf (buf + len, "AC97C_CONFIG = %08x\n",
1954 au_readl(AC97C_CONFIG));
1955 len += sprintf (buf + len, "AC97C_STATUS = %08x\n",
1956 au_readl(AC97C_STATUS));
1957 len += sprintf (buf + len, "AC97C_CNTRL = %08x\n",
1958 au_readl(AC97C_CNTRL));
1960 /* print out CODEC state */
1961 len += sprintf(buf + len, "\nAC97 CODEC registers\n");
1962 len += sprintf(buf + len, "----------------------\n");
1963 for (cnt = 0; cnt <= 0x7e; cnt += 2)
1964 len += sprintf(buf + len, "reg %02x = %04x\n",
1965 cnt, rdcodec(&s->codec, cnt));
1972 *start = buf + fpos;
1973 if ((len -= fpos) > length)
1979 #endif /* AU1000_DEBUG */
1981 /* --------------------------------------------------------------------- */
1983 MODULE_AUTHOR("Monta Vista Software, stevel@mvista.com");
1984 MODULE_DESCRIPTION("Au1000 Audio Driver");
1986 /* --------------------------------------------------------------------- */
1988 static int __devinit au1000_probe(void)
1990 struct au1000_state *s = &au1000_state;
1996 memset(s, 0, sizeof(struct au1000_state));
1998 init_waitqueue_head(&s->dma_adc.wait);
1999 init_waitqueue_head(&s->dma_dac.wait);
2000 init_waitqueue_head(&s->open_wait);
2001 mutex_init(&s->open_mutex);
2002 spin_lock_init(&s->lock);
2003 s->codec.private_data = s;
2005 s->codec.codec_read = rdcodec;
2006 s->codec.codec_write = wrcodec;
2007 s->codec.codec_wait = waitcodec;
2009 if (!request_mem_region(CPHYSADDR(AC97C_CONFIG),
2010 0x14, AU1000_MODULE_NAME)) {
2011 err("AC'97 ports in use");
2014 // Allocate the DMA Channels
2015 if ((s->dma_dac.dmanr = request_au1000_dma(DMA_ID_AC97C_TX,
2018 SA_INTERRUPT, s)) < 0) {
2019 err("Can't get DAC DMA");
2022 if ((s->dma_adc.dmanr = request_au1000_dma(DMA_ID_AC97C_RX,
2025 SA_INTERRUPT, s)) < 0) {
2026 err("Can't get ADC DMA");
2030 info("DAC: DMA%d/IRQ%d, ADC: DMA%d/IRQ%d",
2031 s->dma_dac.dmanr, get_dma_done_irq(s->dma_dac.dmanr),
2032 s->dma_adc.dmanr, get_dma_done_irq(s->dma_adc.dmanr));
2034 // enable DMA coherency in read/write DMA channels
2035 set_dma_mode(s->dma_dac.dmanr,
2036 get_dma_mode(s->dma_dac.dmanr) & ~DMA_NC);
2037 set_dma_mode(s->dma_adc.dmanr,
2038 get_dma_mode(s->dma_adc.dmanr) & ~DMA_NC);
2040 /* register devices */
2042 if ((s->dev_audio = register_sound_dsp(&au1000_audio_fops, -1)) < 0)
2044 if ((s->codec.dev_mixer =
2045 register_sound_mixer(&au1000_mixer_fops, -1)) < 0)
2049 /* intialize the debug proc device */
2050 s->ps = create_proc_read_entry(AU1000_MODULE_NAME, 0, NULL,
2051 proc_au1000_dump, NULL);
2052 #endif /* AU1000_DEBUG */
2054 // configure pins for AC'97
2055 au_writel(au_readl(SYS_PINFUNC) & ~0x02, SYS_PINFUNC);
2057 // Assert reset for 10msec to the AC'97 controller, and enable clock
2058 au_writel(AC97C_RS | AC97C_CE, AC97C_CNTRL);
2060 au_writel(AC97C_CE, AC97C_CNTRL);
2061 au1000_delay(10); // wait for clock to stabilize
2063 /* cold reset the AC'97 */
2064 au_writel(AC97C_RESET, AC97C_CONFIG);
2066 au_writel(0, AC97C_CONFIG);
2067 /* need to delay around 500msec(bleech) to give
2068 some CODECs enough time to wakeup */
2071 /* warm reset the AC'97 to start the bitclk */
2072 au_writel(AC97C_SG | AC97C_SYNC, AC97C_CONFIG);
2074 au_writel(0, AC97C_CONFIG);
2077 if (!ac97_probe_codec(&s->codec))
2080 s->codec_base_caps = rdcodec(&s->codec, AC97_RESET);
2081 s->codec_ext_caps = rdcodec(&s->codec, AC97_EXTENDED_ID);
2082 info("AC'97 Base/Extended ID = %04x/%04x",
2083 s->codec_base_caps, s->codec_ext_caps);
2086 * On the Pb1000, audio playback is on the AUX_OUT
2087 * channel (which defaults to LNLVL_OUT in AC'97
2088 * rev 2.2) so make sure this channel is listed
2089 * as supported (soundcard.h calls this channel
2090 * ALTPCM). ac97_codec.c does not handle detection
2091 * of this channel correctly.
2093 s->codec.supported_mixers |= SOUND_MASK_ALTPCM;
2095 * Now set AUX_OUT's default volume.
2098 mixdev_ioctl(&s->codec, SOUND_MIXER_WRITE_ALTPCM,
2099 (unsigned long) &val);
2101 if (!(s->codec_ext_caps & AC97_EXTID_VRA)) {
2102 // codec does not support VRA
2105 // Boot option says disable VRA
2106 u16 ac97_extstat = rdcodec(&s->codec, AC97_EXTENDED_STATUS);
2107 wrcodec(&s->codec, AC97_EXTENDED_STATUS,
2108 ac97_extstat & ~AC97_EXTSTAT_VRA);
2112 info("no VRA, interpolating and decimating");
2114 /* set mic to be the recording source */
2115 val = SOUND_MASK_MIC;
2116 mixdev_ioctl(&s->codec, SOUND_MIXER_WRITE_RECSRC,
2117 (unsigned long) &val);
2120 sprintf(proc_str, "driver/%s/%d/ac97", AU1000_MODULE_NAME,
2122 s->ac97_ps = create_proc_read_entry (proc_str, 0, NULL,
2123 ac97_read_proc, &s->codec);
2126 #ifdef CONFIG_MIPS_XXS1500
2128 wrcodec(&s->codec, AC97_POWER_CONTROL,
2129 rdcodec(&s->codec, AC97_POWER_CONTROL) & ~0x8000);
2130 /* mute a number of signals which seem to be causing problems
2133 wrcodec(&s->codec, AC97_PCBEEP_VOL, 0x8000);
2134 wrcodec(&s->codec, AC97_PHONE_VOL, 0x8008);
2135 wrcodec(&s->codec, AC97_MIC_VOL, 0x8008);
2136 wrcodec(&s->codec, AC97_LINEIN_VOL, 0x8808);
2137 wrcodec(&s->codec, AC97_CD_VOL, 0x8808);
2138 wrcodec(&s->codec, AC97_VIDEO_VOL, 0x8808);
2139 wrcodec(&s->codec, AC97_AUX_VOL, 0x8808);
2140 wrcodec(&s->codec, AC97_PCMOUT_VOL, 0x0808);
2141 wrcodec(&s->codec, AC97_GENERAL_PURPOSE, 0x2000);
2147 unregister_sound_mixer(s->codec.dev_mixer);
2149 unregister_sound_dsp(s->dev_audio);
2151 free_au1000_dma(s->dma_adc.dmanr);
2153 free_au1000_dma(s->dma_dac.dmanr);
2155 release_mem_region(CPHYSADDR(AC97C_CONFIG), 0x14);
2159 static void au1000_remove(void)
2161 struct au1000_state *s = &au1000_state;
2167 remove_proc_entry(AU1000_MODULE_NAME, NULL);
2168 #endif /* AU1000_DEBUG */
2170 free_au1000_dma(s->dma_adc.dmanr);
2171 free_au1000_dma(s->dma_dac.dmanr);
2172 release_mem_region(CPHYSADDR(AC97C_CONFIG), 0x14);
2173 unregister_sound_dsp(s->dev_audio);
2174 unregister_sound_mixer(s->codec.dev_mixer);
2177 static int __init init_au1000(void)
2179 info("stevel@mvista.com, built " __TIME__ " on " __DATE__);
2180 return au1000_probe();
2183 static void __exit cleanup_au1000(void)
2189 module_init(init_au1000);
2190 module_exit(cleanup_au1000);
2192 /* --------------------------------------------------------------------- */
2196 static int __init au1000_setup(char *options)
2200 if (!options || !*options)
2203 while ((this_opt = strsep(&options, ","))) {
2206 if (!strncmp(this_opt, "vra", 3)) {
2214 __setup("au1000_audio=", au1000_setup);