2 * au1550_ac97.c -- Sound driver for Alchemy Au1550 MIPS Internet Edge
5 * Copyright 2004 Embedded Edge, LLC
8 * Mostly copied from the au1000.c driver and some from the
9 * PowerMac dbdma driver.
10 * We assume the processor can do memory coherent DMA.
12 * Ported to 2.6 by Matt Porter <mporter@kernel.crashing.org>
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
22 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * You should have received a copy of the GNU General Public License along
31 * with this program; if not, write to the Free Software Foundation, Inc.,
32 * 675 Mass Ave, Cambridge, MA 02139, USA.
38 #include <linux/module.h>
39 #include <linux/string.h>
40 #include <linux/ioport.h>
41 #include <linux/sched.h>
42 #include <linux/delay.h>
43 #include <linux/sound.h>
44 #include <linux/slab.h>
45 #include <linux/soundcard.h>
46 #include <linux/init.h>
47 #include <linux/interrupt.h>
48 #include <linux/kernel.h>
49 #include <linux/poll.h>
50 #include <linux/pci.h>
51 #include <linux/bitops.h>
52 #include <linux/spinlock.h>
53 #include <linux/smp_lock.h>
54 #include <linux/ac97_codec.h>
55 #include <linux/mutex.h>
58 #include <asm/uaccess.h>
59 #include <asm/hardirq.h>
60 #include <asm/mach-au1x00/au1xxx_psc.h>
61 #include <asm/mach-au1x00/au1xxx_dbdma.h>
62 #include <asm/mach-au1x00/au1xxx.h>
64 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
67 #define POLL_COUNT 0x50000
68 #define AC97_EXT_DACS (AC97_EXTID_SDAC | AC97_EXTID_CDAC | AC97_EXTID_LDAC)
70 /* The number of DBDMA ring descriptors to allocate. No sense making
71 * this too large....if you can't keep up with a few you aren't likely
72 * to be able to with lots of them, either.
74 #define NUM_DBDMA_DESCRIPTORS 4
76 #define err(format, arg...) printk(KERN_ERR format "\n" , ## arg)
79 * 0 = no VRA, 1 = use VRA if codec supports it
82 module_param(vra
, bool, 0);
83 MODULE_PARM_DESC(vra
, "if 1 use VRA if codec supports it");
85 static struct au1550_state
{
89 struct ac97_codec
*codec
;
90 unsigned codec_base_caps
; /* AC'97 reg 00h, "Reset Register" */
91 unsigned codec_ext_caps
; /* AC'97 reg 28h, "Extended Audio ID" */
92 int no_vra
; /* do not use VRA */
95 struct mutex open_mutex
;
98 wait_queue_head_t open_wait
;
102 unsigned sample_rate
;
104 unsigned sample_size
;
106 int dma_bytes_per_sample
;
107 int user_bytes_per_sample
;
117 unsigned total_bytes
;
119 wait_queue_head_t wait
;
121 /* redundant, but makes calculations easier */
123 unsigned dma_fragsize
;
131 unsigned ossfragshift
;
133 unsigned subdivision
;
164 au1550_delay(int msec
)
172 tmo
= jiffies
+ (msec
* HZ
) / 1000;
174 tmo2
= tmo
- jiffies
;
177 schedule_timeout(tmo2
);
182 rdcodec(struct ac97_codec
*codec
, u8 addr
)
184 struct au1550_state
*s
= (struct au1550_state
*)codec
->private_data
;
190 spin_lock_irqsave(&s
->lock
, flags
);
192 for (i
= 0; i
< POLL_COUNT
; i
++) {
193 val
= au_readl(PSC_AC97STAT
);
195 if (!(val
& PSC_AC97STAT_CP
))
199 err("rdcodec: codec cmd pending expired!");
201 cmd
= (u32
)PSC_AC97CDC_INDX(addr
);
202 cmd
|= PSC_AC97CDC_RD
; /* read command */
203 au_writel(cmd
, PSC_AC97CDC
);
206 /* now wait for the data
208 for (i
= 0; i
< POLL_COUNT
; i
++) {
209 val
= au_readl(PSC_AC97STAT
);
211 if (!(val
& PSC_AC97STAT_CP
))
214 if (i
== POLL_COUNT
) {
215 err("rdcodec: read poll expired!");
220 /* wait for command done?
222 for (i
= 0; i
< POLL_COUNT
; i
++) {
223 val
= au_readl(PSC_AC97EVNT
);
225 if (val
& PSC_AC97EVNT_CD
)
228 if (i
== POLL_COUNT
) {
229 err("rdcodec: read cmdwait expired!");
234 data
= au_readl(PSC_AC97CDC
) & 0xffff;
237 /* Clear command done event.
239 au_writel(PSC_AC97EVNT_CD
, PSC_AC97EVNT
);
243 spin_unlock_irqrestore(&s
->lock
, flags
);
250 wrcodec(struct ac97_codec
*codec
, u8 addr
, u16 data
)
252 struct au1550_state
*s
= (struct au1550_state
*)codec
->private_data
;
257 spin_lock_irqsave(&s
->lock
, flags
);
259 for (i
= 0; i
< POLL_COUNT
; i
++) {
260 val
= au_readl(PSC_AC97STAT
);
262 if (!(val
& PSC_AC97STAT_CP
))
266 err("wrcodec: codec cmd pending expired!");
268 cmd
= (u32
)PSC_AC97CDC_INDX(addr
);
270 au_writel(cmd
, PSC_AC97CDC
);
273 for (i
= 0; i
< POLL_COUNT
; i
++) {
274 val
= au_readl(PSC_AC97STAT
);
276 if (!(val
& PSC_AC97STAT_CP
))
280 err("wrcodec: codec cmd pending expired!");
282 for (i
= 0; i
< POLL_COUNT
; i
++) {
283 val
= au_readl(PSC_AC97EVNT
);
285 if (val
& PSC_AC97EVNT_CD
)
289 err("wrcodec: read cmdwait expired!");
291 /* Clear command done event.
293 au_writel(PSC_AC97EVNT_CD
, PSC_AC97EVNT
);
296 spin_unlock_irqrestore(&s
->lock
, flags
);
300 waitcodec(struct ac97_codec
*codec
)
306 /* codec_wait is used to wait for a ready state after
311 /* first poll the CODEC_READY tag bit
313 for (i
= 0; i
< POLL_COUNT
; i
++) {
314 val
= au_readl(PSC_AC97STAT
);
316 if (val
& PSC_AC97STAT_CR
)
319 if (i
== POLL_COUNT
) {
320 err("waitcodec: CODEC_READY poll expired!");
324 /* get AC'97 powerdown control/status register
326 temp
= rdcodec(codec
, AC97_POWER_CONTROL
);
328 /* If anything is powered down, power'em up
333 wrcodec(codec
, AC97_POWER_CONTROL
, 0);
338 temp
= rdcodec(codec
, AC97_POWER_CONTROL
);
341 /* Check if Codec REF,ANL,DAC,ADC ready
343 if ((temp
& 0x7f0f) != 0x000f)
344 err("codec reg 26 status (0x%x) not ready!!", temp
);
347 /* stop the ADC before calling */
349 set_adc_rate(struct au1550_state
*s
, unsigned rate
)
351 struct dmabuf
*adc
= &s
->dma_adc
;
352 struct dmabuf
*dac
= &s
->dma_dac
;
353 unsigned adc_rate
, dac_rate
;
359 adc
->src_factor
= ((96000 / rate
) + 1) >> 1;
360 adc
->sample_rate
= 48000 / adc
->src_factor
;
366 ac97_extstat
= rdcodec(s
->codec
, AC97_EXTENDED_STATUS
);
368 rate
= rate
> 48000 ? 48000 : rate
;
372 wrcodec(s
->codec
, AC97_EXTENDED_STATUS
,
373 ac97_extstat
| AC97_EXTSTAT_VRA
);
375 /* now write the sample rate
377 wrcodec(s
->codec
, AC97_PCM_LR_ADC_RATE
, (u16
) rate
);
379 /* read it back for actual supported rate
381 adc_rate
= rdcodec(s
->codec
, AC97_PCM_LR_ADC_RATE
);
383 pr_debug("set_adc_rate: set to %d Hz\n", adc_rate
);
385 /* some codec's don't allow unequal DAC and ADC rates, in which case
386 * writing one rate reg actually changes both.
388 dac_rate
= rdcodec(s
->codec
, AC97_PCM_FRONT_DAC_RATE
);
389 if (dac
->num_channels
> 2)
390 wrcodec(s
->codec
, AC97_PCM_SURR_DAC_RATE
, dac_rate
);
391 if (dac
->num_channels
> 4)
392 wrcodec(s
->codec
, AC97_PCM_LFE_DAC_RATE
, dac_rate
);
394 adc
->sample_rate
= adc_rate
;
395 dac
->sample_rate
= dac_rate
;
398 /* stop the DAC before calling */
400 set_dac_rate(struct au1550_state
*s
, unsigned rate
)
402 struct dmabuf
*dac
= &s
->dma_dac
;
403 struct dmabuf
*adc
= &s
->dma_adc
;
404 unsigned adc_rate
, dac_rate
;
410 dac
->src_factor
= ((96000 / rate
) + 1) >> 1;
411 dac
->sample_rate
= 48000 / dac
->src_factor
;
417 ac97_extstat
= rdcodec(s
->codec
, AC97_EXTENDED_STATUS
);
419 rate
= rate
> 48000 ? 48000 : rate
;
423 wrcodec(s
->codec
, AC97_EXTENDED_STATUS
,
424 ac97_extstat
| AC97_EXTSTAT_VRA
);
426 /* now write the sample rate
428 wrcodec(s
->codec
, AC97_PCM_FRONT_DAC_RATE
, (u16
) rate
);
430 /* I don't support different sample rates for multichannel,
431 * so make these channels the same.
433 if (dac
->num_channels
> 2)
434 wrcodec(s
->codec
, AC97_PCM_SURR_DAC_RATE
, (u16
) rate
);
435 if (dac
->num_channels
> 4)
436 wrcodec(s
->codec
, AC97_PCM_LFE_DAC_RATE
, (u16
) rate
);
437 /* read it back for actual supported rate
439 dac_rate
= rdcodec(s
->codec
, AC97_PCM_FRONT_DAC_RATE
);
441 pr_debug("set_dac_rate: set to %d Hz\n", dac_rate
);
443 /* some codec's don't allow unequal DAC and ADC rates, in which case
444 * writing one rate reg actually changes both.
446 adc_rate
= rdcodec(s
->codec
, AC97_PCM_LR_ADC_RATE
);
448 dac
->sample_rate
= dac_rate
;
449 adc
->sample_rate
= adc_rate
;
453 stop_dac(struct au1550_state
*s
)
455 struct dmabuf
*db
= &s
->dma_dac
;
462 spin_lock_irqsave(&s
->lock
, flags
);
464 au_writel(PSC_AC97PCR_TP
, PSC_AC97PCR
);
467 /* Wait for Transmit Busy to show disabled.
470 stat
= au_readl(PSC_AC97STAT
);
472 } while ((stat
& PSC_AC97STAT_TB
) != 0);
474 au1xxx_dbdma_reset(db
->dmanr
);
478 spin_unlock_irqrestore(&s
->lock
, flags
);
482 stop_adc(struct au1550_state
*s
)
484 struct dmabuf
*db
= &s
->dma_adc
;
491 spin_lock_irqsave(&s
->lock
, flags
);
493 au_writel(PSC_AC97PCR_RP
, PSC_AC97PCR
);
496 /* Wait for Receive Busy to show disabled.
499 stat
= au_readl(PSC_AC97STAT
);
501 } while ((stat
& PSC_AC97STAT_RB
) != 0);
503 au1xxx_dbdma_reset(db
->dmanr
);
507 spin_unlock_irqrestore(&s
->lock
, flags
);
512 set_xmit_slots(int num_channels
)
514 u32 ac97_config
, stat
;
516 ac97_config
= au_readl(PSC_AC97CFG
);
518 ac97_config
&= ~(PSC_AC97CFG_TXSLOT_MASK
| PSC_AC97CFG_DE_ENABLE
);
519 au_writel(ac97_config
, PSC_AC97CFG
);
522 switch (num_channels
) {
523 case 6: /* stereo with surround and center/LFE,
526 ac97_config
|= PSC_AC97CFG_TXSLOT_ENA(6);
527 ac97_config
|= PSC_AC97CFG_TXSLOT_ENA(9);
529 case 4: /* stereo with surround, slots 3,4,7,8 */
530 ac97_config
|= PSC_AC97CFG_TXSLOT_ENA(7);
531 ac97_config
|= PSC_AC97CFG_TXSLOT_ENA(8);
533 case 2: /* stereo, slots 3,4 */
535 ac97_config
|= PSC_AC97CFG_TXSLOT_ENA(3);
536 ac97_config
|= PSC_AC97CFG_TXSLOT_ENA(4);
539 au_writel(ac97_config
, PSC_AC97CFG
);
542 ac97_config
|= PSC_AC97CFG_DE_ENABLE
;
543 au_writel(ac97_config
, PSC_AC97CFG
);
546 /* Wait for Device ready.
549 stat
= au_readl(PSC_AC97STAT
);
551 } while ((stat
& PSC_AC97STAT_DR
) == 0);
555 set_recv_slots(int num_channels
)
557 u32 ac97_config
, stat
;
559 ac97_config
= au_readl(PSC_AC97CFG
);
561 ac97_config
&= ~(PSC_AC97CFG_RXSLOT_MASK
| PSC_AC97CFG_DE_ENABLE
);
562 au_writel(ac97_config
, PSC_AC97CFG
);
565 /* Always enable slots 3 and 4 (stereo). Slot 6 is
566 * optional Mic ADC, which we don't support yet.
568 ac97_config
|= PSC_AC97CFG_RXSLOT_ENA(3);
569 ac97_config
|= PSC_AC97CFG_RXSLOT_ENA(4);
571 au_writel(ac97_config
, PSC_AC97CFG
);
574 ac97_config
|= PSC_AC97CFG_DE_ENABLE
;
575 au_writel(ac97_config
, PSC_AC97CFG
);
578 /* Wait for Device ready.
581 stat
= au_readl(PSC_AC97STAT
);
583 } while ((stat
& PSC_AC97STAT_DR
) == 0);
586 /* Hold spinlock for both start_dac() and start_adc() calls */
588 start_dac(struct au1550_state
*s
)
590 struct dmabuf
*db
= &s
->dma_dac
;
595 set_xmit_slots(db
->num_channels
);
596 au_writel(PSC_AC97PCR_TC
, PSC_AC97PCR
);
598 au_writel(PSC_AC97PCR_TS
, PSC_AC97PCR
);
601 au1xxx_dbdma_start(db
->dmanr
);
607 start_adc(struct au1550_state
*s
)
609 struct dmabuf
*db
= &s
->dma_adc
;
615 /* Put two buffers on the ring to get things started.
617 for (i
=0; i
<2; i
++) {
618 au1xxx_dbdma_put_dest(db
->dmanr
, db
->nextIn
, db
->dma_fragsize
);
620 db
->nextIn
+= db
->dma_fragsize
;
621 if (db
->nextIn
>= db
->rawbuf
+ db
->dmasize
)
622 db
->nextIn
-= db
->dmasize
;
625 set_recv_slots(db
->num_channels
);
626 au1xxx_dbdma_start(db
->dmanr
);
627 au_writel(PSC_AC97PCR_RC
, PSC_AC97PCR
);
629 au_writel(PSC_AC97PCR_RS
, PSC_AC97PCR
);
636 prog_dmabuf(struct au1550_state
*s
, struct dmabuf
*db
)
638 unsigned user_bytes_per_sec
;
640 unsigned rate
= db
->sample_rate
;
643 db
->ready
= db
->mapped
= 0;
644 db
->buforder
= 5; /* 32 * PAGE_SIZE */
645 db
->rawbuf
= kmalloc((PAGE_SIZE
<< db
->buforder
), GFP_KERNEL
);
651 if (db
->sample_size
== 8)
653 if (db
->num_channels
== 1)
655 db
->cnt_factor
*= db
->src_factor
;
659 db
->nextIn
= db
->nextOut
= db
->rawbuf
;
661 db
->user_bytes_per_sample
= (db
->sample_size
>>3) * db
->num_channels
;
662 db
->dma_bytes_per_sample
= 2 * ((db
->num_channels
== 1) ?
663 2 : db
->num_channels
);
665 user_bytes_per_sec
= rate
* db
->user_bytes_per_sample
;
666 bufs
= PAGE_SIZE
<< db
->buforder
;
667 if (db
->ossfragshift
) {
668 if ((1000 << db
->ossfragshift
) < user_bytes_per_sec
)
669 db
->fragshift
= ld2(user_bytes_per_sec
/1000);
671 db
->fragshift
= db
->ossfragshift
;
673 db
->fragshift
= ld2(user_bytes_per_sec
/ 100 /
674 (db
->subdivision
? db
->subdivision
: 1));
675 if (db
->fragshift
< 3)
679 db
->fragsize
= 1 << db
->fragshift
;
680 db
->dma_fragsize
= db
->fragsize
* db
->cnt_factor
;
681 db
->numfrag
= bufs
/ db
->dma_fragsize
;
683 while (db
->numfrag
< 4 && db
->fragshift
> 3) {
685 db
->fragsize
= 1 << db
->fragshift
;
686 db
->dma_fragsize
= db
->fragsize
* db
->cnt_factor
;
687 db
->numfrag
= bufs
/ db
->dma_fragsize
;
690 if (db
->ossmaxfrags
>= 4 && db
->ossmaxfrags
< db
->numfrag
)
691 db
->numfrag
= db
->ossmaxfrags
;
693 db
->dmasize
= db
->dma_fragsize
* db
->numfrag
;
694 memset(db
->rawbuf
, 0, bufs
);
696 pr_debug("prog_dmabuf: rate=%d, samplesize=%d, channels=%d\n",
697 rate
, db
->sample_size
, db
->num_channels
);
698 pr_debug("prog_dmabuf: fragsize=%d, cnt_factor=%d, dma_fragsize=%d\n",
699 db
->fragsize
, db
->cnt_factor
, db
->dma_fragsize
);
700 pr_debug("prog_dmabuf: numfrag=%d, dmasize=%d\n", db
->numfrag
, db
->dmasize
);
707 prog_dmabuf_adc(struct au1550_state
*s
)
710 return prog_dmabuf(s
, &s
->dma_adc
);
715 prog_dmabuf_dac(struct au1550_state
*s
)
718 return prog_dmabuf(s
, &s
->dma_dac
);
722 static void dac_dma_interrupt(int irq
, void *dev_id
)
724 struct au1550_state
*s
= (struct au1550_state
*) dev_id
;
725 struct dmabuf
*db
= &s
->dma_dac
;
730 ac97c_stat
= au_readl(PSC_AC97STAT
);
731 if (ac97c_stat
& (AC97C_XU
| AC97C_XO
| AC97C_TE
))
732 pr_debug("AC97C status = 0x%08x\n", ac97c_stat
);
735 if (db
->count
>= db
->fragsize
) {
736 if (au1xxx_dbdma_put_source(db
->dmanr
, db
->nextOut
,
737 db
->fragsize
) == 0) {
738 err("qcount < 2 and no ring room!");
740 db
->nextOut
+= db
->fragsize
;
741 if (db
->nextOut
>= db
->rawbuf
+ db
->dmasize
)
742 db
->nextOut
-= db
->dmasize
;
743 db
->count
-= db
->fragsize
;
744 db
->total_bytes
+= db
->dma_fragsize
;
748 /* wake up anybody listening */
749 if (waitqueue_active(&db
->wait
))
752 spin_unlock(&s
->lock
);
756 static void adc_dma_interrupt(int irq
, void *dev_id
)
758 struct au1550_state
*s
= (struct au1550_state
*)dev_id
;
759 struct dmabuf
*dp
= &s
->dma_adc
;
765 /* Pull the buffer from the dma queue.
767 au1xxx_dbdma_get_dest(dp
->dmanr
, (void *)(&obuf
), &obytes
);
769 if ((dp
->count
+ obytes
) > dp
->dmasize
) {
770 /* Overrun. Stop ADC and log the error
772 spin_unlock(&s
->lock
);
779 /* Put a new empty buffer on the destination DMA.
781 au1xxx_dbdma_put_dest(dp
->dmanr
, dp
->nextIn
, dp
->dma_fragsize
);
783 dp
->nextIn
+= dp
->dma_fragsize
;
784 if (dp
->nextIn
>= dp
->rawbuf
+ dp
->dmasize
)
785 dp
->nextIn
-= dp
->dmasize
;
788 dp
->total_bytes
+= obytes
;
790 /* wake up anybody listening
792 if (waitqueue_active(&dp
->wait
))
795 spin_unlock(&s
->lock
);
799 au1550_llseek(struct file
*file
, loff_t offset
, int origin
)
806 au1550_open_mixdev(struct inode
*inode
, struct file
*file
)
808 file
->private_data
= &au1550_state
;
813 au1550_release_mixdev(struct inode
*inode
, struct file
*file
)
819 mixdev_ioctl(struct ac97_codec
*codec
, unsigned int cmd
,
822 return codec
->mixer_ioctl(codec
, cmd
, arg
);
826 au1550_ioctl_mixdev(struct inode
*inode
, struct file
*file
,
827 unsigned int cmd
, unsigned long arg
)
829 struct au1550_state
*s
= (struct au1550_state
*)file
->private_data
;
830 struct ac97_codec
*codec
= s
->codec
;
832 return mixdev_ioctl(codec
, cmd
, arg
);
835 static /*const */ struct file_operations au1550_mixer_fops
= {
837 llseek
:au1550_llseek
,
838 ioctl
:au1550_ioctl_mixdev
,
839 open
:au1550_open_mixdev
,
840 release
:au1550_release_mixdev
,
844 drain_dac(struct au1550_state
*s
, int nonblock
)
849 if (s
->dma_dac
.mapped
|| !s
->dma_dac
.ready
|| s
->dma_dac
.stopped
)
853 spin_lock_irqsave(&s
->lock
, flags
);
854 count
= s
->dma_dac
.count
;
855 spin_unlock_irqrestore(&s
->lock
, flags
);
856 if (count
<= s
->dma_dac
.fragsize
)
858 if (signal_pending(current
))
862 tmo
= 1000 * count
/ (s
->no_vra
?
863 48000 : s
->dma_dac
.sample_rate
);
864 tmo
/= s
->dma_dac
.dma_bytes_per_sample
;
867 if (signal_pending(current
))
872 static inline u8
S16_TO_U8(s16 ch
)
874 return (u8
) (ch
>> 8) + 0x80;
876 static inline s16
U8_TO_S16(u8 ch
)
878 return (s16
) (ch
- 0x80) << 8;
882 * Translates user samples to dma buffer suitable for AC'97 DAC data:
883 * If mono, copy left channel to right channel in dma buffer.
884 * If 8 bit samples, cvt to 16-bit before writing to dma buffer.
885 * If interpolating (no VRA), duplicate every audio frame src_factor times.
888 translate_from_user(struct dmabuf
*db
, char* dmabuf
, char* userbuf
,
892 int interp_bytes_per_sample
;
894 int mono
= (db
->num_channels
== 1);
896 s16 ch
, dmasample
[6];
898 if (db
->sample_size
== 16 && !mono
&& db
->src_factor
== 1) {
899 /* no translation necessary, just copy
901 if (copy_from_user(dmabuf
, userbuf
, dmacount
))
906 interp_bytes_per_sample
= db
->dma_bytes_per_sample
* db
->src_factor
;
907 num_samples
= dmacount
/ interp_bytes_per_sample
;
909 for (sample
= 0; sample
< num_samples
; sample
++) {
910 if (copy_from_user(usersample
, userbuf
,
911 db
->user_bytes_per_sample
)) {
915 for (i
= 0; i
< db
->num_channels
; i
++) {
916 if (db
->sample_size
== 8)
917 ch
= U8_TO_S16(usersample
[i
]);
919 ch
= *((s16
*) (&usersample
[i
* 2]));
922 dmasample
[i
+ 1] = ch
; /* right channel */
925 /* duplicate every audio frame src_factor times
927 for (i
= 0; i
< db
->src_factor
; i
++)
928 memcpy(dmabuf
, dmasample
, db
->dma_bytes_per_sample
);
930 userbuf
+= db
->user_bytes_per_sample
;
931 dmabuf
+= interp_bytes_per_sample
;
934 return num_samples
* interp_bytes_per_sample
;
938 * Translates AC'97 ADC samples to user buffer:
939 * If mono, send only left channel to user buffer.
940 * If 8 bit samples, cvt from 16 to 8 bit before writing to user buffer.
941 * If decimating (no VRA), skip over src_factor audio frames.
944 translate_to_user(struct dmabuf
*db
, char* userbuf
, char* dmabuf
,
948 int interp_bytes_per_sample
;
950 int mono
= (db
->num_channels
== 1);
953 if (db
->sample_size
== 16 && !mono
&& db
->src_factor
== 1) {
954 /* no translation necessary, just copy
956 if (copy_to_user(userbuf
, dmabuf
, dmacount
))
961 interp_bytes_per_sample
= db
->dma_bytes_per_sample
* db
->src_factor
;
962 num_samples
= dmacount
/ interp_bytes_per_sample
;
964 for (sample
= 0; sample
< num_samples
; sample
++) {
965 for (i
= 0; i
< db
->num_channels
; i
++) {
966 if (db
->sample_size
== 8)
968 S16_TO_U8(*((s16
*) (&dmabuf
[i
* 2])));
970 *((s16
*) (&usersample
[i
* 2])) =
971 *((s16
*) (&dmabuf
[i
* 2]));
974 if (copy_to_user(userbuf
, usersample
,
975 db
->user_bytes_per_sample
)) {
979 userbuf
+= db
->user_bytes_per_sample
;
980 dmabuf
+= interp_bytes_per_sample
;
983 return num_samples
* interp_bytes_per_sample
;
987 * Copy audio data to/from user buffer from/to dma buffer, taking care
988 * that we wrap when reading/writing the dma buffer. Returns actual byte
989 * count written to or read from the dma buffer.
992 copy_dmabuf_user(struct dmabuf
*db
, char* userbuf
, int count
, int to_user
)
994 char *bufptr
= to_user
? db
->nextOut
: db
->nextIn
;
995 char *bufend
= db
->rawbuf
+ db
->dmasize
;
998 if (bufptr
+ count
> bufend
) {
999 int partial
= (int) (bufend
- bufptr
);
1001 if ((cnt
= translate_to_user(db
, userbuf
,
1002 bufptr
, partial
)) < 0)
1005 if ((cnt
= translate_to_user(db
, userbuf
+ partial
,
1007 count
- partial
)) < 0)
1011 if ((cnt
= translate_from_user(db
, bufptr
, userbuf
,
1015 if ((cnt
= translate_from_user(db
, db
->rawbuf
,
1017 count
- partial
)) < 0)
1023 ret
= translate_to_user(db
, userbuf
, bufptr
, count
);
1025 ret
= translate_from_user(db
, bufptr
, userbuf
, count
);
1033 au1550_read(struct file
*file
, char *buffer
, size_t count
, loff_t
*ppos
)
1035 struct au1550_state
*s
= (struct au1550_state
*)file
->private_data
;
1036 struct dmabuf
*db
= &s
->dma_adc
;
1037 DECLARE_WAITQUEUE(wait
, current
);
1039 unsigned long flags
;
1040 int cnt
, usercnt
, avail
;
1044 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
1048 count
*= db
->cnt_factor
;
1050 mutex_lock(&s
->sem
);
1051 add_wait_queue(&db
->wait
, &wait
);
1054 /* wait for samples in ADC dma buffer
1057 spin_lock_irqsave(&s
->lock
, flags
);
1062 __set_current_state(TASK_INTERRUPTIBLE
);
1063 spin_unlock_irqrestore(&s
->lock
, flags
);
1065 if (file
->f_flags
& O_NONBLOCK
) {
1070 mutex_unlock(&s
->sem
);
1072 if (signal_pending(current
)) {
1077 mutex_lock(&s
->sem
);
1079 } while (avail
<= 0);
1081 /* copy from nextOut to user
1083 if ((cnt
= copy_dmabuf_user(db
, buffer
,
1085 avail
: count
, 1)) < 0) {
1091 spin_lock_irqsave(&s
->lock
, flags
);
1094 if (db
->nextOut
>= db
->rawbuf
+ db
->dmasize
)
1095 db
->nextOut
-= db
->dmasize
;
1096 spin_unlock_irqrestore(&s
->lock
, flags
);
1099 usercnt
= cnt
/ db
->cnt_factor
;
1102 } /* while (count > 0) */
1105 mutex_unlock(&s
->sem
);
1107 remove_wait_queue(&db
->wait
, &wait
);
1108 set_current_state(TASK_RUNNING
);
1113 au1550_write(struct file
*file
, const char *buffer
, size_t count
, loff_t
* ppos
)
1115 struct au1550_state
*s
= (struct au1550_state
*)file
->private_data
;
1116 struct dmabuf
*db
= &s
->dma_dac
;
1117 DECLARE_WAITQUEUE(wait
, current
);
1119 unsigned long flags
;
1120 int cnt
, usercnt
, avail
;
1122 pr_debug("write: count=%d\n", count
);
1126 if (!access_ok(VERIFY_READ
, buffer
, count
))
1129 count
*= db
->cnt_factor
;
1131 mutex_lock(&s
->sem
);
1132 add_wait_queue(&db
->wait
, &wait
);
1135 /* wait for space in playback buffer
1138 spin_lock_irqsave(&s
->lock
, flags
);
1139 avail
= (int) db
->dmasize
- db
->count
;
1141 __set_current_state(TASK_INTERRUPTIBLE
);
1142 spin_unlock_irqrestore(&s
->lock
, flags
);
1144 if (file
->f_flags
& O_NONBLOCK
) {
1149 mutex_unlock(&s
->sem
);
1151 if (signal_pending(current
)) {
1156 mutex_lock(&s
->sem
);
1158 } while (avail
<= 0);
1160 /* copy from user to nextIn
1162 if ((cnt
= copy_dmabuf_user(db
, (char *) buffer
,
1164 avail
: count
, 0)) < 0) {
1170 spin_lock_irqsave(&s
->lock
, flags
);
1173 if (db
->nextIn
>= db
->rawbuf
+ db
->dmasize
)
1174 db
->nextIn
-= db
->dmasize
;
1176 /* If the data is available, we want to keep two buffers
1177 * on the dma queue. If the queue count reaches zero,
1178 * we know the dma has stopped.
1180 while ((db
->dma_qcount
< 2) && (db
->count
>= db
->fragsize
)) {
1181 if (au1xxx_dbdma_put_source(db
->dmanr
, db
->nextOut
,
1182 db
->fragsize
) == 0) {
1183 err("qcount < 2 and no ring room!");
1185 db
->nextOut
+= db
->fragsize
;
1186 if (db
->nextOut
>= db
->rawbuf
+ db
->dmasize
)
1187 db
->nextOut
-= db
->dmasize
;
1188 db
->total_bytes
+= db
->dma_fragsize
;
1189 if (db
->dma_qcount
== 0)
1193 spin_unlock_irqrestore(&s
->lock
, flags
);
1196 usercnt
= cnt
/ db
->cnt_factor
;
1199 } /* while (count > 0) */
1202 mutex_unlock(&s
->sem
);
1204 remove_wait_queue(&db
->wait
, &wait
);
1205 set_current_state(TASK_RUNNING
);
1210 /* No kernel lock - we have our own spinlock */
1212 au1550_poll(struct file
*file
, struct poll_table_struct
*wait
)
1214 struct au1550_state
*s
= (struct au1550_state
*)file
->private_data
;
1215 unsigned long flags
;
1216 unsigned int mask
= 0;
1218 if (file
->f_mode
& FMODE_WRITE
) {
1219 if (!s
->dma_dac
.ready
)
1221 poll_wait(file
, &s
->dma_dac
.wait
, wait
);
1223 if (file
->f_mode
& FMODE_READ
) {
1224 if (!s
->dma_adc
.ready
)
1226 poll_wait(file
, &s
->dma_adc
.wait
, wait
);
1229 spin_lock_irqsave(&s
->lock
, flags
);
1231 if (file
->f_mode
& FMODE_READ
) {
1232 if (s
->dma_adc
.count
>= (signed)s
->dma_adc
.dma_fragsize
)
1233 mask
|= POLLIN
| POLLRDNORM
;
1235 if (file
->f_mode
& FMODE_WRITE
) {
1236 if (s
->dma_dac
.mapped
) {
1237 if (s
->dma_dac
.count
>=
1238 (signed)s
->dma_dac
.dma_fragsize
)
1239 mask
|= POLLOUT
| POLLWRNORM
;
1241 if ((signed) s
->dma_dac
.dmasize
>=
1242 s
->dma_dac
.count
+ (signed)s
->dma_dac
.dma_fragsize
)
1243 mask
|= POLLOUT
| POLLWRNORM
;
1246 spin_unlock_irqrestore(&s
->lock
, flags
);
1251 au1550_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1253 struct au1550_state
*s
= (struct au1550_state
*)file
->private_data
;
1259 mutex_lock(&s
->sem
);
1260 if (vma
->vm_flags
& VM_WRITE
)
1262 else if (vma
->vm_flags
& VM_READ
)
1268 if (vma
->vm_pgoff
!= 0) {
1272 size
= vma
->vm_end
- vma
->vm_start
;
1273 if (size
> (PAGE_SIZE
<< db
->buforder
)) {
1277 if (remap_pfn_range(vma
, vma
->vm_start
, page_to_pfn(virt_to_page(db
->rawbuf
)),
1278 size
, vma
->vm_page_prot
)) {
1282 vma
->vm_flags
&= ~VM_IO
;
1285 mutex_unlock(&s
->sem
);
1291 static struct ioctl_str_t
{
1295 {SNDCTL_DSP_RESET
, "SNDCTL_DSP_RESET"},
1296 {SNDCTL_DSP_SYNC
, "SNDCTL_DSP_SYNC"},
1297 {SNDCTL_DSP_SPEED
, "SNDCTL_DSP_SPEED"},
1298 {SNDCTL_DSP_STEREO
, "SNDCTL_DSP_STEREO"},
1299 {SNDCTL_DSP_GETBLKSIZE
, "SNDCTL_DSP_GETBLKSIZE"},
1300 {SNDCTL_DSP_SAMPLESIZE
, "SNDCTL_DSP_SAMPLESIZE"},
1301 {SNDCTL_DSP_CHANNELS
, "SNDCTL_DSP_CHANNELS"},
1302 {SOUND_PCM_WRITE_CHANNELS
, "SOUND_PCM_WRITE_CHANNELS"},
1303 {SOUND_PCM_WRITE_FILTER
, "SOUND_PCM_WRITE_FILTER"},
1304 {SNDCTL_DSP_POST
, "SNDCTL_DSP_POST"},
1305 {SNDCTL_DSP_SUBDIVIDE
, "SNDCTL_DSP_SUBDIVIDE"},
1306 {SNDCTL_DSP_SETFRAGMENT
, "SNDCTL_DSP_SETFRAGMENT"},
1307 {SNDCTL_DSP_GETFMTS
, "SNDCTL_DSP_GETFMTS"},
1308 {SNDCTL_DSP_SETFMT
, "SNDCTL_DSP_SETFMT"},
1309 {SNDCTL_DSP_GETOSPACE
, "SNDCTL_DSP_GETOSPACE"},
1310 {SNDCTL_DSP_GETISPACE
, "SNDCTL_DSP_GETISPACE"},
1311 {SNDCTL_DSP_NONBLOCK
, "SNDCTL_DSP_NONBLOCK"},
1312 {SNDCTL_DSP_GETCAPS
, "SNDCTL_DSP_GETCAPS"},
1313 {SNDCTL_DSP_GETTRIGGER
, "SNDCTL_DSP_GETTRIGGER"},
1314 {SNDCTL_DSP_SETTRIGGER
, "SNDCTL_DSP_SETTRIGGER"},
1315 {SNDCTL_DSP_GETIPTR
, "SNDCTL_DSP_GETIPTR"},
1316 {SNDCTL_DSP_GETOPTR
, "SNDCTL_DSP_GETOPTR"},
1317 {SNDCTL_DSP_MAPINBUF
, "SNDCTL_DSP_MAPINBUF"},
1318 {SNDCTL_DSP_MAPOUTBUF
, "SNDCTL_DSP_MAPOUTBUF"},
1319 {SNDCTL_DSP_SETSYNCRO
, "SNDCTL_DSP_SETSYNCRO"},
1320 {SNDCTL_DSP_SETDUPLEX
, "SNDCTL_DSP_SETDUPLEX"},
1321 {SNDCTL_DSP_GETODELAY
, "SNDCTL_DSP_GETODELAY"},
1322 {SNDCTL_DSP_GETCHANNELMASK
, "SNDCTL_DSP_GETCHANNELMASK"},
1323 {SNDCTL_DSP_BIND_CHANNEL
, "SNDCTL_DSP_BIND_CHANNEL"},
1324 {OSS_GETVERSION
, "OSS_GETVERSION"},
1325 {SOUND_PCM_READ_RATE
, "SOUND_PCM_READ_RATE"},
1326 {SOUND_PCM_READ_CHANNELS
, "SOUND_PCM_READ_CHANNELS"},
1327 {SOUND_PCM_READ_BITS
, "SOUND_PCM_READ_BITS"},
1328 {SOUND_PCM_READ_FILTER
, "SOUND_PCM_READ_FILTER"}
1333 dma_count_done(struct dmabuf
*db
)
1338 return db
->dma_fragsize
- au1xxx_get_dma_residue(db
->dmanr
);
1343 au1550_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
1346 struct au1550_state
*s
= (struct au1550_state
*)file
->private_data
;
1347 unsigned long flags
;
1348 audio_buf_info abinfo
;
1351 int val
, mapped
, ret
, diff
;
1353 mapped
= ((file
->f_mode
& FMODE_WRITE
) && s
->dma_dac
.mapped
) ||
1354 ((file
->f_mode
& FMODE_READ
) && s
->dma_adc
.mapped
);
1357 for (count
= 0; count
< ARRAY_SIZE(ioctl_str
); count
++) {
1358 if (ioctl_str
[count
].cmd
== cmd
)
1361 if (count
< ARRAY_SIZE(ioctl_str
))
1362 pr_debug("ioctl %s, arg=0x%lxn", ioctl_str
[count
].str
, arg
);
1364 pr_debug("ioctl 0x%x unknown, arg=0x%lx\n", cmd
, arg
);
1368 case OSS_GETVERSION
:
1369 return put_user(SOUND_VERSION
, (int *) arg
);
1371 case SNDCTL_DSP_SYNC
:
1372 if (file
->f_mode
& FMODE_WRITE
)
1373 return drain_dac(s
, file
->f_flags
& O_NONBLOCK
);
1376 case SNDCTL_DSP_SETDUPLEX
:
1379 case SNDCTL_DSP_GETCAPS
:
1380 return put_user(DSP_CAP_DUPLEX
| DSP_CAP_REALTIME
|
1381 DSP_CAP_TRIGGER
| DSP_CAP_MMAP
, (int *)arg
);
1383 case SNDCTL_DSP_RESET
:
1384 if (file
->f_mode
& FMODE_WRITE
) {
1387 s
->dma_dac
.count
= s
->dma_dac
.total_bytes
= 0;
1388 s
->dma_dac
.nextIn
= s
->dma_dac
.nextOut
=
1391 if (file
->f_mode
& FMODE_READ
) {
1394 s
->dma_adc
.count
= s
->dma_adc
.total_bytes
= 0;
1395 s
->dma_adc
.nextIn
= s
->dma_adc
.nextOut
=
1400 case SNDCTL_DSP_SPEED
:
1401 if (get_user(val
, (int *) arg
))
1404 if (file
->f_mode
& FMODE_READ
) {
1406 set_adc_rate(s
, val
);
1408 if (file
->f_mode
& FMODE_WRITE
) {
1410 set_dac_rate(s
, val
);
1412 if (s
->open_mode
& FMODE_READ
)
1413 if ((ret
= prog_dmabuf_adc(s
)))
1415 if (s
->open_mode
& FMODE_WRITE
)
1416 if ((ret
= prog_dmabuf_dac(s
)))
1419 return put_user((file
->f_mode
& FMODE_READ
) ?
1420 s
->dma_adc
.sample_rate
:
1421 s
->dma_dac
.sample_rate
,
1424 case SNDCTL_DSP_STEREO
:
1425 if (get_user(val
, (int *) arg
))
1427 if (file
->f_mode
& FMODE_READ
) {
1429 s
->dma_adc
.num_channels
= val
? 2 : 1;
1430 if ((ret
= prog_dmabuf_adc(s
)))
1433 if (file
->f_mode
& FMODE_WRITE
) {
1435 s
->dma_dac
.num_channels
= val
? 2 : 1;
1436 if (s
->codec_ext_caps
& AC97_EXT_DACS
) {
1437 /* disable surround and center/lfe in AC'97
1439 u16 ext_stat
= rdcodec(s
->codec
,
1440 AC97_EXTENDED_STATUS
);
1441 wrcodec(s
->codec
, AC97_EXTENDED_STATUS
,
1442 ext_stat
| (AC97_EXTSTAT_PRI
|
1446 if ((ret
= prog_dmabuf_dac(s
)))
1451 case SNDCTL_DSP_CHANNELS
:
1452 if (get_user(val
, (int *) arg
))
1455 if (file
->f_mode
& FMODE_READ
) {
1456 if (val
< 0 || val
> 2)
1459 s
->dma_adc
.num_channels
= val
;
1460 if ((ret
= prog_dmabuf_adc(s
)))
1463 if (file
->f_mode
& FMODE_WRITE
) {
1472 if (!(s
->codec_ext_caps
&
1477 if ((s
->codec_ext_caps
&
1478 AC97_EXT_DACS
) != AC97_EXT_DACS
)
1487 (s
->codec_ext_caps
& AC97_EXT_DACS
)) {
1488 /* disable surround and center/lfe
1493 AC97_EXTENDED_STATUS
);
1495 AC97_EXTENDED_STATUS
,
1496 ext_stat
| (AC97_EXTSTAT_PRI
|
1499 } else if (val
>= 4) {
1500 /* enable surround, center/lfe
1505 AC97_EXTENDED_STATUS
);
1506 ext_stat
&= ~AC97_EXTSTAT_PRJ
;
1509 ~(AC97_EXTSTAT_PRI
|
1512 AC97_EXTENDED_STATUS
,
1516 s
->dma_dac
.num_channels
= val
;
1517 if ((ret
= prog_dmabuf_dac(s
)))
1521 return put_user(val
, (int *) arg
);
1523 case SNDCTL_DSP_GETFMTS
: /* Returns a mask */
1524 return put_user(AFMT_S16_LE
| AFMT_U8
, (int *) arg
);
1526 case SNDCTL_DSP_SETFMT
: /* Selects ONE fmt */
1527 if (get_user(val
, (int *) arg
))
1529 if (val
!= AFMT_QUERY
) {
1530 if (file
->f_mode
& FMODE_READ
) {
1532 if (val
== AFMT_S16_LE
)
1533 s
->dma_adc
.sample_size
= 16;
1536 s
->dma_adc
.sample_size
= 8;
1538 if ((ret
= prog_dmabuf_adc(s
)))
1541 if (file
->f_mode
& FMODE_WRITE
) {
1543 if (val
== AFMT_S16_LE
)
1544 s
->dma_dac
.sample_size
= 16;
1547 s
->dma_dac
.sample_size
= 8;
1549 if ((ret
= prog_dmabuf_dac(s
)))
1553 if (file
->f_mode
& FMODE_READ
)
1554 val
= (s
->dma_adc
.sample_size
== 16) ?
1555 AFMT_S16_LE
: AFMT_U8
;
1557 val
= (s
->dma_dac
.sample_size
== 16) ?
1558 AFMT_S16_LE
: AFMT_U8
;
1560 return put_user(val
, (int *) arg
);
1562 case SNDCTL_DSP_POST
:
1565 case SNDCTL_DSP_GETTRIGGER
:
1567 spin_lock_irqsave(&s
->lock
, flags
);
1568 if (file
->f_mode
& FMODE_READ
&& !s
->dma_adc
.stopped
)
1569 val
|= PCM_ENABLE_INPUT
;
1570 if (file
->f_mode
& FMODE_WRITE
&& !s
->dma_dac
.stopped
)
1571 val
|= PCM_ENABLE_OUTPUT
;
1572 spin_unlock_irqrestore(&s
->lock
, flags
);
1573 return put_user(val
, (int *) arg
);
1575 case SNDCTL_DSP_SETTRIGGER
:
1576 if (get_user(val
, (int *) arg
))
1578 if (file
->f_mode
& FMODE_READ
) {
1579 if (val
& PCM_ENABLE_INPUT
) {
1580 spin_lock_irqsave(&s
->lock
, flags
);
1582 spin_unlock_irqrestore(&s
->lock
, flags
);
1586 if (file
->f_mode
& FMODE_WRITE
) {
1587 if (val
& PCM_ENABLE_OUTPUT
) {
1588 spin_lock_irqsave(&s
->lock
, flags
);
1590 spin_unlock_irqrestore(&s
->lock
, flags
);
1596 case SNDCTL_DSP_GETOSPACE
:
1597 if (!(file
->f_mode
& FMODE_WRITE
))
1599 abinfo
.fragsize
= s
->dma_dac
.fragsize
;
1600 spin_lock_irqsave(&s
->lock
, flags
);
1601 count
= s
->dma_dac
.count
;
1602 count
-= dma_count_done(&s
->dma_dac
);
1603 spin_unlock_irqrestore(&s
->lock
, flags
);
1606 abinfo
.bytes
= (s
->dma_dac
.dmasize
- count
) /
1607 s
->dma_dac
.cnt_factor
;
1608 abinfo
.fragstotal
= s
->dma_dac
.numfrag
;
1609 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_dac
.fragshift
;
1610 pr_debug("ioctl SNDCTL_DSP_GETOSPACE: bytes=%d, fragments=%d\n", abinfo
.bytes
, abinfo
.fragments
);
1611 return copy_to_user((void *) arg
, &abinfo
,
1612 sizeof(abinfo
)) ? -EFAULT
: 0;
1614 case SNDCTL_DSP_GETISPACE
:
1615 if (!(file
->f_mode
& FMODE_READ
))
1617 abinfo
.fragsize
= s
->dma_adc
.fragsize
;
1618 spin_lock_irqsave(&s
->lock
, flags
);
1619 count
= s
->dma_adc
.count
;
1620 count
+= dma_count_done(&s
->dma_adc
);
1621 spin_unlock_irqrestore(&s
->lock
, flags
);
1624 abinfo
.bytes
= count
/ s
->dma_adc
.cnt_factor
;
1625 abinfo
.fragstotal
= s
->dma_adc
.numfrag
;
1626 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_adc
.fragshift
;
1627 return copy_to_user((void *) arg
, &abinfo
,
1628 sizeof(abinfo
)) ? -EFAULT
: 0;
1630 case SNDCTL_DSP_NONBLOCK
:
1631 file
->f_flags
|= O_NONBLOCK
;
1634 case SNDCTL_DSP_GETODELAY
:
1635 if (!(file
->f_mode
& FMODE_WRITE
))
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 count
/= s
->dma_dac
.cnt_factor
;
1644 return put_user(count
, (int *) arg
);
1646 case SNDCTL_DSP_GETIPTR
:
1647 if (!(file
->f_mode
& FMODE_READ
))
1649 spin_lock_irqsave(&s
->lock
, flags
);
1650 cinfo
.bytes
= s
->dma_adc
.total_bytes
;
1651 count
= s
->dma_adc
.count
;
1652 if (!s
->dma_adc
.stopped
) {
1653 diff
= dma_count_done(&s
->dma_adc
);
1655 cinfo
.bytes
+= diff
;
1656 cinfo
.ptr
= virt_to_phys(s
->dma_adc
.nextIn
) + diff
-
1657 virt_to_phys(s
->dma_adc
.rawbuf
);
1659 cinfo
.ptr
= virt_to_phys(s
->dma_adc
.nextIn
) -
1660 virt_to_phys(s
->dma_adc
.rawbuf
);
1661 if (s
->dma_adc
.mapped
)
1662 s
->dma_adc
.count
&= (s
->dma_adc
.dma_fragsize
-1);
1663 spin_unlock_irqrestore(&s
->lock
, flags
);
1666 cinfo
.blocks
= count
>> s
->dma_adc
.fragshift
;
1667 return copy_to_user((void *) arg
, &cinfo
, sizeof(cinfo
));
1669 case SNDCTL_DSP_GETOPTR
:
1670 if (!(file
->f_mode
& FMODE_READ
))
1672 spin_lock_irqsave(&s
->lock
, flags
);
1673 cinfo
.bytes
= s
->dma_dac
.total_bytes
;
1674 count
= s
->dma_dac
.count
;
1675 if (!s
->dma_dac
.stopped
) {
1676 diff
= dma_count_done(&s
->dma_dac
);
1678 cinfo
.bytes
+= diff
;
1679 cinfo
.ptr
= virt_to_phys(s
->dma_dac
.nextOut
) + diff
-
1680 virt_to_phys(s
->dma_dac
.rawbuf
);
1682 cinfo
.ptr
= virt_to_phys(s
->dma_dac
.nextOut
) -
1683 virt_to_phys(s
->dma_dac
.rawbuf
);
1684 if (s
->dma_dac
.mapped
)
1685 s
->dma_dac
.count
&= (s
->dma_dac
.dma_fragsize
-1);
1686 spin_unlock_irqrestore(&s
->lock
, flags
);
1689 cinfo
.blocks
= count
>> s
->dma_dac
.fragshift
;
1690 return copy_to_user((void *) arg
, &cinfo
, sizeof(cinfo
));
1692 case SNDCTL_DSP_GETBLKSIZE
:
1693 if (file
->f_mode
& FMODE_WRITE
)
1694 return put_user(s
->dma_dac
.fragsize
, (int *) arg
);
1696 return put_user(s
->dma_adc
.fragsize
, (int *) arg
);
1698 case SNDCTL_DSP_SETFRAGMENT
:
1699 if (get_user(val
, (int *) arg
))
1701 if (file
->f_mode
& FMODE_READ
) {
1703 s
->dma_adc
.ossfragshift
= val
& 0xffff;
1704 s
->dma_adc
.ossmaxfrags
= (val
>> 16) & 0xffff;
1705 if (s
->dma_adc
.ossfragshift
< 4)
1706 s
->dma_adc
.ossfragshift
= 4;
1707 if (s
->dma_adc
.ossfragshift
> 15)
1708 s
->dma_adc
.ossfragshift
= 15;
1709 if (s
->dma_adc
.ossmaxfrags
< 4)
1710 s
->dma_adc
.ossmaxfrags
= 4;
1711 if ((ret
= prog_dmabuf_adc(s
)))
1714 if (file
->f_mode
& FMODE_WRITE
) {
1716 s
->dma_dac
.ossfragshift
= val
& 0xffff;
1717 s
->dma_dac
.ossmaxfrags
= (val
>> 16) & 0xffff;
1718 if (s
->dma_dac
.ossfragshift
< 4)
1719 s
->dma_dac
.ossfragshift
= 4;
1720 if (s
->dma_dac
.ossfragshift
> 15)
1721 s
->dma_dac
.ossfragshift
= 15;
1722 if (s
->dma_dac
.ossmaxfrags
< 4)
1723 s
->dma_dac
.ossmaxfrags
= 4;
1724 if ((ret
= prog_dmabuf_dac(s
)))
1729 case SNDCTL_DSP_SUBDIVIDE
:
1730 if ((file
->f_mode
& FMODE_READ
&& s
->dma_adc
.subdivision
) ||
1731 (file
->f_mode
& FMODE_WRITE
&& s
->dma_dac
.subdivision
))
1733 if (get_user(val
, (int *) arg
))
1735 if (val
!= 1 && val
!= 2 && val
!= 4)
1737 if (file
->f_mode
& FMODE_READ
) {
1739 s
->dma_adc
.subdivision
= val
;
1740 if ((ret
= prog_dmabuf_adc(s
)))
1743 if (file
->f_mode
& FMODE_WRITE
) {
1745 s
->dma_dac
.subdivision
= val
;
1746 if ((ret
= prog_dmabuf_dac(s
)))
1751 case SOUND_PCM_READ_RATE
:
1752 return put_user((file
->f_mode
& FMODE_READ
) ?
1753 s
->dma_adc
.sample_rate
:
1754 s
->dma_dac
.sample_rate
,
1757 case SOUND_PCM_READ_CHANNELS
:
1758 if (file
->f_mode
& FMODE_READ
)
1759 return put_user(s
->dma_adc
.num_channels
, (int *)arg
);
1761 return put_user(s
->dma_dac
.num_channels
, (int *)arg
);
1763 case SOUND_PCM_READ_BITS
:
1764 if (file
->f_mode
& FMODE_READ
)
1765 return put_user(s
->dma_adc
.sample_size
, (int *)arg
);
1767 return put_user(s
->dma_dac
.sample_size
, (int *)arg
);
1769 case SOUND_PCM_WRITE_FILTER
:
1770 case SNDCTL_DSP_SETSYNCRO
:
1771 case SOUND_PCM_READ_FILTER
:
1775 return mixdev_ioctl(s
->codec
, cmd
, arg
);
1780 au1550_open(struct inode
*inode
, struct file
*file
)
1782 int minor
= MINOR(inode
->i_rdev
);
1783 DECLARE_WAITQUEUE(wait
, current
);
1784 struct au1550_state
*s
= &au1550_state
;
1788 if (file
->f_flags
& O_NONBLOCK
)
1789 pr_debug("open: non-blocking\n");
1791 pr_debug("open: blocking\n");
1794 file
->private_data
= s
;
1795 /* wait for device to become free */
1796 mutex_lock(&s
->open_mutex
);
1797 while (s
->open_mode
& file
->f_mode
) {
1798 if (file
->f_flags
& O_NONBLOCK
) {
1799 mutex_unlock(&s
->open_mutex
);
1802 add_wait_queue(&s
->open_wait
, &wait
);
1803 __set_current_state(TASK_INTERRUPTIBLE
);
1804 mutex_unlock(&s
->open_mutex
);
1806 remove_wait_queue(&s
->open_wait
, &wait
);
1807 set_current_state(TASK_RUNNING
);
1808 if (signal_pending(current
))
1809 return -ERESTARTSYS
;
1810 mutex_lock(&s
->open_mutex
);
1816 if (file
->f_mode
& FMODE_READ
) {
1817 s
->dma_adc
.ossfragshift
= s
->dma_adc
.ossmaxfrags
=
1818 s
->dma_adc
.subdivision
= s
->dma_adc
.total_bytes
= 0;
1819 s
->dma_adc
.num_channels
= 1;
1820 s
->dma_adc
.sample_size
= 8;
1821 set_adc_rate(s
, 8000);
1822 if ((minor
& 0xf) == SND_DEV_DSP16
)
1823 s
->dma_adc
.sample_size
= 16;
1826 if (file
->f_mode
& FMODE_WRITE
) {
1827 s
->dma_dac
.ossfragshift
= s
->dma_dac
.ossmaxfrags
=
1828 s
->dma_dac
.subdivision
= s
->dma_dac
.total_bytes
= 0;
1829 s
->dma_dac
.num_channels
= 1;
1830 s
->dma_dac
.sample_size
= 8;
1831 set_dac_rate(s
, 8000);
1832 if ((minor
& 0xf) == SND_DEV_DSP16
)
1833 s
->dma_dac
.sample_size
= 16;
1836 if (file
->f_mode
& FMODE_READ
) {
1837 if ((ret
= prog_dmabuf_adc(s
)))
1840 if (file
->f_mode
& FMODE_WRITE
) {
1841 if ((ret
= prog_dmabuf_dac(s
)))
1845 s
->open_mode
|= file
->f_mode
& (FMODE_READ
| FMODE_WRITE
);
1846 mutex_unlock(&s
->open_mutex
);
1847 mutex_init(&s
->sem
);
1852 au1550_release(struct inode
*inode
, struct file
*file
)
1854 struct au1550_state
*s
= (struct au1550_state
*)file
->private_data
;
1858 if (file
->f_mode
& FMODE_WRITE
) {
1860 drain_dac(s
, file
->f_flags
& O_NONBLOCK
);
1864 mutex_lock(&s
->open_mutex
);
1865 if (file
->f_mode
& FMODE_WRITE
) {
1867 kfree(s
->dma_dac
.rawbuf
);
1868 s
->dma_dac
.rawbuf
= NULL
;
1870 if (file
->f_mode
& FMODE_READ
) {
1872 kfree(s
->dma_adc
.rawbuf
);
1873 s
->dma_adc
.rawbuf
= NULL
;
1875 s
->open_mode
&= ((~file
->f_mode
) & (FMODE_READ
|FMODE_WRITE
));
1876 mutex_unlock(&s
->open_mutex
);
1877 wake_up(&s
->open_wait
);
1882 static /*const */ struct file_operations au1550_audio_fops
= {
1884 llseek
: au1550_llseek
,
1886 write
: au1550_write
,
1888 ioctl
: au1550_ioctl
,
1891 release
: au1550_release
,
1894 MODULE_AUTHOR("Advanced Micro Devices (AMD), dan@embeddededge.com");
1895 MODULE_DESCRIPTION("Au1550 AC97 Audio Driver");
1896 MODULE_LICENSE("GPL");
1899 static int __devinit
1902 struct au1550_state
*s
= &au1550_state
;
1905 memset(s
, 0, sizeof(struct au1550_state
));
1907 init_waitqueue_head(&s
->dma_adc
.wait
);
1908 init_waitqueue_head(&s
->dma_dac
.wait
);
1909 init_waitqueue_head(&s
->open_wait
);
1910 mutex_init(&s
->open_mutex
);
1911 spin_lock_init(&s
->lock
);
1913 s
->codec
= ac97_alloc_codec();
1914 if(s
->codec
== NULL
) {
1915 err("Out of memory");
1918 s
->codec
->private_data
= s
;
1920 s
->codec
->codec_read
= rdcodec
;
1921 s
->codec
->codec_write
= wrcodec
;
1922 s
->codec
->codec_wait
= waitcodec
;
1924 if (!request_mem_region(CPHYSADDR(AC97_PSC_SEL
),
1925 0x30, "Au1550 AC97")) {
1926 err("AC'97 ports in use");
1929 /* Allocate the DMA Channels
1931 if ((s
->dma_dac
.dmanr
= au1xxx_dbdma_chan_alloc(DBDMA_MEM_CHAN
,
1932 DBDMA_AC97_TX_CHAN
, dac_dma_interrupt
, (void *)s
)) == 0) {
1933 err("Can't get DAC DMA");
1936 au1xxx_dbdma_set_devwidth(s
->dma_dac
.dmanr
, 16);
1937 if (au1xxx_dbdma_ring_alloc(s
->dma_dac
.dmanr
,
1938 NUM_DBDMA_DESCRIPTORS
) == 0) {
1939 err("Can't get DAC DMA descriptors");
1943 if ((s
->dma_adc
.dmanr
= au1xxx_dbdma_chan_alloc(DBDMA_AC97_RX_CHAN
,
1944 DBDMA_MEM_CHAN
, adc_dma_interrupt
, (void *)s
)) == 0) {
1945 err("Can't get ADC DMA");
1948 au1xxx_dbdma_set_devwidth(s
->dma_adc
.dmanr
, 16);
1949 if (au1xxx_dbdma_ring_alloc(s
->dma_adc
.dmanr
,
1950 NUM_DBDMA_DESCRIPTORS
) == 0) {
1951 err("Can't get ADC DMA descriptors");
1955 pr_info("DAC: DMA%d, ADC: DMA%d", DBDMA_AC97_TX_CHAN
, DBDMA_AC97_RX_CHAN
);
1957 /* register devices */
1959 if ((s
->dev_audio
= register_sound_dsp(&au1550_audio_fops
, -1)) < 0)
1961 if ((s
->codec
->dev_mixer
=
1962 register_sound_mixer(&au1550_mixer_fops
, -1)) < 0)
1965 /* The GPIO for the appropriate PSC was configured by the
1966 * board specific start up.
1968 * configure PSC for AC'97
1970 au_writel(0, AC97_PSC_CTRL
); /* Disable PSC */
1972 au_writel((PSC_SEL_CLK_SERCLK
| PSC_SEL_PS_AC97MODE
), AC97_PSC_SEL
);
1975 /* cold reset the AC'97
1977 au_writel(PSC_AC97RST_RST
, PSC_AC97RST
);
1980 au_writel(0, PSC_AC97RST
);
1983 /* need to delay around 500msec(bleech) to give
1984 some CODECs enough time to wakeup */
1987 /* warm reset the AC'97 to start the bitclk
1989 au_writel(PSC_AC97RST_SNC
, PSC_AC97RST
);
1992 au_writel(0, PSC_AC97RST
);
1997 au_writel(PSC_CTRL_ENABLE
, AC97_PSC_CTRL
);
2000 /* Wait for PSC ready.
2003 val
= au_readl(PSC_AC97STAT
);
2005 } while ((val
& PSC_AC97STAT_SR
) == 0);
2007 /* Configure AC97 controller.
2008 * Deep FIFO, 16-bit sample, DMA, make sure DMA matches fifo size.
2010 val
= PSC_AC97CFG_SET_LEN(16);
2011 val
|= PSC_AC97CFG_RT_FIFO8
| PSC_AC97CFG_TT_FIFO8
;
2013 /* Enable device so we can at least
2014 * talk over the AC-link.
2016 au_writel(val
, PSC_AC97CFG
);
2017 au_writel(PSC_AC97MSK_ALLMASK
, PSC_AC97MSK
);
2019 val
|= PSC_AC97CFG_DE_ENABLE
;
2020 au_writel(val
, PSC_AC97CFG
);
2023 /* Wait for Device ready.
2026 val
= au_readl(PSC_AC97STAT
);
2028 } while ((val
& PSC_AC97STAT_DR
) == 0);
2031 if (!ac97_probe_codec(s
->codec
))
2034 s
->codec_base_caps
= rdcodec(s
->codec
, AC97_RESET
);
2035 s
->codec_ext_caps
= rdcodec(s
->codec
, AC97_EXTENDED_ID
);
2036 pr_info("AC'97 Base/Extended ID = %04x/%04x",
2037 s
->codec_base_caps
, s
->codec_ext_caps
);
2039 if (!(s
->codec_ext_caps
& AC97_EXTID_VRA
)) {
2040 /* codec does not support VRA
2044 /* Boot option says disable VRA
2046 u16 ac97_extstat
= rdcodec(s
->codec
, AC97_EXTENDED_STATUS
);
2047 wrcodec(s
->codec
, AC97_EXTENDED_STATUS
,
2048 ac97_extstat
& ~AC97_EXTSTAT_VRA
);
2052 pr_info("no VRA, interpolating and decimating");
2054 /* set mic to be the recording source */
2055 val
= SOUND_MASK_MIC
;
2056 mixdev_ioctl(s
->codec
, SOUND_MIXER_WRITE_RECSRC
,
2057 (unsigned long) &val
);
2062 unregister_sound_mixer(s
->codec
->dev_mixer
);
2064 unregister_sound_dsp(s
->dev_audio
);
2066 au1xxx_dbdma_chan_free(s
->dma_adc
.dmanr
);
2068 au1xxx_dbdma_chan_free(s
->dma_dac
.dmanr
);
2070 release_mem_region(CPHYSADDR(AC97_PSC_SEL
), 0x30);
2072 ac97_release_codec(s
->codec
);
2076 static void __devinit
2079 struct au1550_state
*s
= &au1550_state
;
2084 au1xxx_dbdma_chan_free(s
->dma_adc
.dmanr
);
2085 au1xxx_dbdma_chan_free(s
->dma_dac
.dmanr
);
2086 release_mem_region(CPHYSADDR(AC97_PSC_SEL
), 0x30);
2087 unregister_sound_dsp(s
->dev_audio
);
2088 unregister_sound_mixer(s
->codec
->dev_mixer
);
2089 ac97_release_codec(s
->codec
);
2095 return au1550_probe();
2099 cleanup_au1550(void)
2104 module_init(init_au1550
);
2105 module_exit(cleanup_au1550
);
2110 au1550_setup(char *options
)
2114 if (!options
|| !*options
)
2117 while ((this_opt
= strsep(&options
, ","))) {
2120 if (!strncmp(this_opt
, "vra", 3)) {
2128 __setup("au1550_audio=", au1550_setup
);