1 /***********************************************************************
2 * Copyright 2001 MontaVista Software Inc.
3 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
5 * drivers/sound/nec_vrc5477.c
6 * AC97 sound dirver for NEC Vrc5477 chip (an integrated,
7 * multi-function controller chip for MIPS CPUs)
9 * VRA support Copyright 2001 Bradley D. LaRonde <brad@ltc.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 ***********************************************************************
19 * This code is derived from ite8172.c, which is written by Steve Longerbeam.
22 * Currently we only support the following capabilities:
23 * . mono output to PCM L/R (line out).
24 * . stereo output to PCM L/R (line out).
25 * . mono input from PCM L (line in).
26 * . stereo output from PCM (line in).
27 * . sampling rate at 48k or variable sampling rate
28 * . support /dev/dsp, /dev/mixer devices, standard OSS devices.
29 * . only support 16-bit PCM format (hardware limit, no software
31 * . support duplex, but no trigger or realtime.
33 * Specifically the following are not supported:
34 * . app-set frag size.
35 * . mmap'ed buffer access
39 * Original comments from ite8172.c file.
46 * 1. Much of the OSS buffer allocation, ioctl's, and mmap'ing are
47 * taken, slightly modified or not at all, from the ES1371 driver,
48 * so refer to the credits in es1371.c for those. The rest of the
49 * code (probe, open, read, write, the ISR, etc.) is new.
50 * 2. The following support is untested:
51 * * Memory mapping the audio buffers, and the ioctl controls that go
54 * 3. The following is not supported:
56 * * legacy audio mode.
57 * 4. Support for volume button interrupts is implemented but doesn't
61 * 02.08.2001 0.1 Initial release
64 #include <linux/module.h>
65 #include <linux/string.h>
66 #include <linux/kernel.h>
67 #include <linux/ioport.h>
68 #include <linux/sched.h>
69 #include <linux/delay.h>
70 #include <linux/sound.h>
71 #include <linux/slab.h>
72 #include <linux/soundcard.h>
73 #include <linux/pci.h>
74 #include <linux/init.h>
75 #include <linux/poll.h>
76 #include <linux/bitops.h>
77 #include <linux/proc_fs.h>
78 #include <linux/spinlock.h>
79 #include <linux/smp_lock.h>
80 #include <linux/ac97_codec.h>
83 #include <asm/uaccess.h>
85 /* -------------------debug macros -------------------------------------- */
86 /* #undef VRC5477_AC97_DEBUG */
87 #define VRC5477_AC97_DEBUG
89 #undef VRC5477_AC97_VERBOSE_DEBUG
90 /* #define VRC5477_AC97_VERBOSE_DEBUG */
92 #if defined(VRC5477_AC97_VERBOSE_DEBUG)
93 #define VRC5477_AC97_DEBUG
96 #if defined(VRC5477_AC97_DEBUG)
97 #define ASSERT(x) if (!(x)) { \
98 panic("assertion failed at %s:%d: %s\n", __FILE__, __LINE__, #x); }
101 #endif /* VRC5477_AC97_DEBUG */
103 #if defined(VRC5477_AC97_VERBOSE_DEBUG)
104 static u16 inTicket
; /* check sync between intr & write */
105 static u16 outTicket
;
108 /* --------------------------------------------------------------------- */
110 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
112 static const unsigned sample_shift
[] = { 0, 1, 1, 2 };
114 #define VRC5477_INT_CLR 0x0
115 #define VRC5477_INT_STATUS 0x0
116 #define VRC5477_CODEC_WR 0x4
117 #define VRC5477_CODEC_RD 0x8
118 #define VRC5477_CTRL 0x18
119 #define VRC5477_ACLINK_CTRL 0x1c
120 #define VRC5477_INT_MASK 0x24
122 #define VRC5477_DAC1_CTRL 0x30
123 #define VRC5477_DAC1L 0x34
124 #define VRC5477_DAC1_BADDR 0x38
125 #define VRC5477_DAC2_CTRL 0x3c
126 #define VRC5477_DAC2L 0x40
127 #define VRC5477_DAC2_BADDR 0x44
128 #define VRC5477_DAC3_CTRL 0x48
129 #define VRC5477_DAC3L 0x4c
130 #define VRC5477_DAC3_BADDR 0x50
132 #define VRC5477_ADC1_CTRL 0x54
133 #define VRC5477_ADC1L 0x58
134 #define VRC5477_ADC1_BADDR 0x5c
135 #define VRC5477_ADC2_CTRL 0x60
136 #define VRC5477_ADC2L 0x64
137 #define VRC5477_ADC2_BADDR 0x68
138 #define VRC5477_ADC3_CTRL 0x6c
139 #define VRC5477_ADC3L 0x70
140 #define VRC5477_ADC3_BADDR 0x74
142 #define VRC5477_CODEC_WR_RWC (1 << 23)
144 #define VRC5477_CODEC_RD_RRDYA (1 << 31)
145 #define VRC5477_CODEC_RD_RRDYD (1 << 30)
147 #define VRC5477_ACLINK_CTRL_RST_ON (1 << 15)
148 #define VRC5477_ACLINK_CTRL_RST_TIME 0x7f
149 #define VRC5477_ACLINK_CTRL_SYNC_ON (1 << 30)
150 #define VRC5477_ACLINK_CTRL_CK_STOP_ON (1 << 31)
152 #define VRC5477_CTRL_DAC2ENB (1 << 15)
153 #define VRC5477_CTRL_ADC2ENB (1 << 14)
154 #define VRC5477_CTRL_DAC1ENB (1 << 13)
155 #define VRC5477_CTRL_ADC1ENB (1 << 12)
157 #define VRC5477_INT_MASK_NMASK (1 << 31)
158 #define VRC5477_INT_MASK_DAC1END (1 << 5)
159 #define VRC5477_INT_MASK_DAC2END (1 << 4)
160 #define VRC5477_INT_MASK_DAC3END (1 << 3)
161 #define VRC5477_INT_MASK_ADC1END (1 << 2)
162 #define VRC5477_INT_MASK_ADC2END (1 << 1)
163 #define VRC5477_INT_MASK_ADC3END (1 << 0)
165 #define VRC5477_DMA_ACTIVATION (1 << 31)
166 #define VRC5477_DMA_WIP (1 << 30)
169 #define VRC5477_AC97_MODULE_NAME "NEC_Vrc5477_audio"
170 #define PFX VRC5477_AC97_MODULE_NAME ": "
172 /* --------------------------------------------------------------------- */
174 struct vrc5477_ac97_state
{
175 /* list of vrc5477_ac97 devices */
176 struct list_head devs
;
178 /* the corresponding pci_dev structure */
181 /* soundcore stuff */
184 /* hardware resources */
188 #ifdef VRC5477_AC97_DEBUG
189 /* debug /proc entry */
190 struct proc_dir_entry
*ps
;
191 struct proc_dir_entry
*ac97_ps
;
192 #endif /* VRC5477_AC97_DEBUG */
194 struct ac97_codec
*codec
;
196 unsigned dacChannels
, adcChannels
;
197 unsigned short dacRate
, adcRate
;
198 unsigned short extended_status
;
201 struct semaphore open_sem
;
203 wait_queue_head_t open_wait
;
207 dma_addr_t lbufDma
, rbufDma
;
211 unsigned fragSize
; /* redundant */
212 unsigned fragTotalSize
; /* = numFrag * fragSize(real) */
216 unsigned error
; /* over/underrun */
217 wait_queue_head_t wait
;
223 #define WORK_BUF_SIZE 2048
227 } workBuf
[WORK_BUF_SIZE
/4];
230 /* --------------------------------------------------------------------- */
232 static LIST_HEAD(devs
);
234 /* --------------------------------------------------------------------- */
236 static inline unsigned ld2(unsigned int x
)
261 /* --------------------------------------------------------------------- */
263 static u16
rdcodec(struct ac97_codec
*codec
, u8 addr
)
265 struct vrc5477_ac97_state
*s
=
266 (struct vrc5477_ac97_state
*)codec
->private_data
;
270 spin_lock_irqsave(&s
->lock
, flags
);
272 /* wait until we can access codec registers */
273 while (inl(s
->io
+ VRC5477_CODEC_WR
) & 0x80000000);
275 /* write the address and "read" command to codec */
277 outl((addr
<< 16) | VRC5477_CODEC_WR_RWC
, s
->io
+ VRC5477_CODEC_WR
);
279 /* get the return result */
280 udelay(100); /* workaround hardware bug */
281 while ( (result
= inl(s
->io
+ VRC5477_CODEC_RD
)) &
282 (VRC5477_CODEC_RD_RRDYA
| VRC5477_CODEC_RD_RRDYD
) ) {
283 /* we get either addr or data, or both */
284 if (result
& VRC5477_CODEC_RD_RRDYA
) {
285 ASSERT(addr
== ((result
>> 16) & 0x7f) );
287 if (result
& VRC5477_CODEC_RD_RRDYD
) {
292 spin_unlock_irqrestore(&s
->lock
, flags
);
294 return result
& 0xffff;
298 static void wrcodec(struct ac97_codec
*codec
, u8 addr
, u16 data
)
300 struct vrc5477_ac97_state
*s
=
301 (struct vrc5477_ac97_state
*)codec
->private_data
;
304 spin_lock_irqsave(&s
->lock
, flags
);
306 /* wait until we can access codec registers */
307 while (inl(s
->io
+ VRC5477_CODEC_WR
) & 0x80000000);
309 /* write the address and value to codec */
310 outl((addr
<< 16) | data
, s
->io
+ VRC5477_CODEC_WR
);
312 spin_unlock_irqrestore(&s
->lock
, flags
);
316 static void waitcodec(struct ac97_codec
*codec
)
318 struct vrc5477_ac97_state
*s
=
319 (struct vrc5477_ac97_state
*)codec
->private_data
;
321 /* wait until we can access codec registers */
322 while (inl(s
->io
+ VRC5477_CODEC_WR
) & 0x80000000);
325 static int ac97_codec_not_present(struct ac97_codec
*codec
)
327 struct vrc5477_ac97_state
*s
=
328 (struct vrc5477_ac97_state
*)codec
->private_data
;
330 unsigned short count
= 0xffff;
332 spin_lock_irqsave(&s
->lock
, flags
);
334 /* wait until we can access codec registers */
336 if (!(inl(s
->io
+ VRC5477_CODEC_WR
) & 0x80000000))
341 spin_unlock_irqrestore(&s
->lock
, flags
);
345 /* write 0 to reset */
346 outl((AC97_RESET
<< 16) | 0, s
->io
+ VRC5477_CODEC_WR
);
348 /* test whether we get a response from ac97 chip */
351 if (!(inl(s
->io
+ VRC5477_CODEC_WR
) & 0x80000000))
356 spin_unlock_irqrestore(&s
->lock
, flags
);
359 spin_unlock_irqrestore(&s
->lock
, flags
);
363 /* --------------------------------------------------------------------- */
365 static void vrc5477_ac97_delay(int msec
)
373 tmo
= jiffies
+ (msec
*HZ
)/1000;
375 tmo2
= tmo
- jiffies
;
378 schedule_timeout(tmo2
);
383 static void set_adc_rate(struct vrc5477_ac97_state
*s
, unsigned rate
)
385 wrcodec(s
->codec
, AC97_PCM_LR_ADC_RATE
, rate
);
390 static void set_dac_rate(struct vrc5477_ac97_state
*s
, unsigned rate
)
392 if(s
->extended_status
& AC97_EXTSTAT_VRA
) {
393 wrcodec(s
->codec
, AC97_PCM_FRONT_DAC_RATE
, rate
);
394 s
->dacRate
= rdcodec(s
->codec
, AC97_PCM_FRONT_DAC_RATE
);
398 static int ac97_codec_not_present(struct ac97_codec
*codec
)
400 struct vrc5477_ac97_state
*s
=
401 (struct vrc5477_ac97_state
*)codec
->private_data
;
403 unsigned short count
= 0xffff;
405 spin_lock_irqsave(&s
->lock
, flags
);
407 /* wait until we can access codec registers */
409 if (!(inl(s
->io
+ VRC5477_CODEC_WR
) & 0x80000000))
414 spin_unlock_irqrestore(&s
->lock
, flags
);
418 /* write 0 to reset */
419 outl((AC97_RESET
<< 16) | 0, s
->io
+ VRC5477_CODEC_WR
);
421 /* test whether we get a response from ac97 chip */
424 if (!(inl(s
->io
+ VRC5477_CODEC_WR
) & 0x80000000))
429 spin_unlock_irqrestore(&s
->lock
, flags
);
432 spin_unlock_irqrestore(&s
->lock
, flags
);
436 /* --------------------------------------------------------------------- */
439 stop_dac(struct vrc5477_ac97_state
*s
)
441 struct dmabuf
* db
= &s
->dma_dac
;
445 spin_lock_irqsave(&s
->lock
, flags
);
448 spin_unlock_irqrestore(&s
->lock
, flags
);
452 /* deactivate the dma */
453 outl(0, s
->io
+ VRC5477_DAC1_CTRL
);
454 outl(0, s
->io
+ VRC5477_DAC2_CTRL
);
456 /* wait for DAM completely stop */
457 while (inl(s
->io
+ VRC5477_DAC1_CTRL
) & VRC5477_DMA_WIP
);
458 while (inl(s
->io
+ VRC5477_DAC2_CTRL
) & VRC5477_DMA_WIP
);
460 /* disable dac slots in aclink */
461 temp
= inl(s
->io
+ VRC5477_CTRL
);
462 temp
&= ~ (VRC5477_CTRL_DAC1ENB
| VRC5477_CTRL_DAC2ENB
);
463 outl (temp
, s
->io
+ VRC5477_CTRL
);
465 /* disable interrupts */
466 temp
= inl(s
->io
+ VRC5477_INT_MASK
);
467 temp
&= ~ (VRC5477_INT_MASK_DAC1END
| VRC5477_INT_MASK_DAC2END
);
468 outl (temp
, s
->io
+ VRC5477_INT_MASK
);
470 /* clear pending ones */
471 outl(VRC5477_INT_MASK_DAC1END
| VRC5477_INT_MASK_DAC2END
,
472 s
->io
+ VRC5477_INT_CLR
);
476 spin_unlock_irqrestore(&s
->lock
, flags
);
479 static void start_dac(struct vrc5477_ac97_state
*s
)
481 struct dmabuf
* db
= &s
->dma_dac
;
486 spin_lock_irqsave(&s
->lock
, flags
);
489 spin_unlock_irqrestore(&s
->lock
, flags
);
493 /* we should have some data to do the DMA trasnfer */
494 ASSERT(db
->count
>= db
->fragSize
);
496 /* clear pending fales interrupts */
497 outl(VRC5477_INT_MASK_DAC1END
| VRC5477_INT_MASK_DAC2END
,
498 s
->io
+ VRC5477_INT_CLR
);
500 /* enable interrupts */
501 temp
= inl(s
->io
+ VRC5477_INT_MASK
);
502 temp
|= VRC5477_INT_MASK_DAC1END
| VRC5477_INT_MASK_DAC2END
;
503 outl(temp
, s
->io
+ VRC5477_INT_MASK
);
505 /* setup dma base addr */
506 outl(db
->lbufDma
+ db
->nextOut
, s
->io
+ VRC5477_DAC1_BADDR
);
507 if (s
->dacChannels
== 1) {
508 outl(db
->lbufDma
+ db
->nextOut
, s
->io
+ VRC5477_DAC2_BADDR
);
510 outl(db
->rbufDma
+ db
->nextOut
, s
->io
+ VRC5477_DAC2_BADDR
);
513 /* set dma length, in the unit of 0x10 bytes */
514 dmaLength
= db
->fragSize
>> 4;
515 outl(dmaLength
, s
->io
+ VRC5477_DAC1L
);
516 outl(dmaLength
, s
->io
+ VRC5477_DAC2L
);
519 outl(VRC5477_DMA_ACTIVATION
, s
->io
+ VRC5477_DAC1_CTRL
);
520 outl(VRC5477_DMA_ACTIVATION
, s
->io
+ VRC5477_DAC2_CTRL
);
522 /* enable dac slots - we should hear the music now! */
523 temp
= inl(s
->io
+ VRC5477_CTRL
);
524 temp
|= (VRC5477_CTRL_DAC1ENB
| VRC5477_CTRL_DAC2ENB
);
525 outl (temp
, s
->io
+ VRC5477_CTRL
);
527 /* it is time to setup next dma transfer */
528 ASSERT(inl(s
->io
+ VRC5477_DAC1_CTRL
) & VRC5477_DMA_WIP
);
529 ASSERT(inl(s
->io
+ VRC5477_DAC2_CTRL
) & VRC5477_DMA_WIP
);
531 temp
= db
->nextOut
+ db
->fragSize
;
532 if (temp
>= db
->fragTotalSize
) {
533 ASSERT(temp
== db
->fragTotalSize
);
537 outl(db
->lbufDma
+ temp
, s
->io
+ VRC5477_DAC1_BADDR
);
538 if (s
->dacChannels
== 1) {
539 outl(db
->lbufDma
+ temp
, s
->io
+ VRC5477_DAC2_BADDR
);
541 outl(db
->rbufDma
+ temp
, s
->io
+ VRC5477_DAC2_BADDR
);
546 #if defined(VRC5477_AC97_VERBOSE_DEBUG)
547 outTicket
= *(u16
*)(db
->lbuf
+db
->nextOut
);
548 if (db
->count
> db
->fragSize
) {
549 ASSERT((u16
)(outTicket
+1) == *(u16
*)(db
->lbuf
+temp
));
553 spin_unlock_irqrestore(&s
->lock
, flags
);
556 static inline void stop_adc(struct vrc5477_ac97_state
*s
)
558 struct dmabuf
* db
= &s
->dma_adc
;
562 spin_lock_irqsave(&s
->lock
, flags
);
565 spin_unlock_irqrestore(&s
->lock
, flags
);
569 /* deactivate the dma */
570 outl(0, s
->io
+ VRC5477_ADC1_CTRL
);
571 outl(0, s
->io
+ VRC5477_ADC2_CTRL
);
573 /* disable adc slots in aclink */
574 temp
= inl(s
->io
+ VRC5477_CTRL
);
575 temp
&= ~ (VRC5477_CTRL_ADC1ENB
| VRC5477_CTRL_ADC2ENB
);
576 outl (temp
, s
->io
+ VRC5477_CTRL
);
578 /* disable interrupts */
579 temp
= inl(s
->io
+ VRC5477_INT_MASK
);
580 temp
&= ~ (VRC5477_INT_MASK_ADC1END
| VRC5477_INT_MASK_ADC2END
);
581 outl (temp
, s
->io
+ VRC5477_INT_MASK
);
583 /* clear pending ones */
584 outl(VRC5477_INT_MASK_ADC1END
| VRC5477_INT_MASK_ADC2END
,
585 s
->io
+ VRC5477_INT_CLR
);
589 spin_unlock_irqrestore(&s
->lock
, flags
);
592 static void start_adc(struct vrc5477_ac97_state
*s
)
594 struct dmabuf
* db
= &s
->dma_adc
;
599 spin_lock_irqsave(&s
->lock
, flags
);
602 spin_unlock_irqrestore(&s
->lock
, flags
);
606 /* we should at least have some free space in the buffer */
607 ASSERT(db
->count
< db
->fragTotalSize
- db
->fragSize
* 2);
609 /* clear pending ones */
610 outl(VRC5477_INT_MASK_ADC1END
| VRC5477_INT_MASK_ADC2END
,
611 s
->io
+ VRC5477_INT_CLR
);
613 /* enable interrupts */
614 temp
= inl(s
->io
+ VRC5477_INT_MASK
);
615 temp
|= VRC5477_INT_MASK_ADC1END
| VRC5477_INT_MASK_ADC2END
;
616 outl(temp
, s
->io
+ VRC5477_INT_MASK
);
618 /* setup dma base addr */
619 outl(db
->lbufDma
+ db
->nextIn
, s
->io
+ VRC5477_ADC1_BADDR
);
620 outl(db
->rbufDma
+ db
->nextIn
, s
->io
+ VRC5477_ADC2_BADDR
);
622 /* setup dma length */
623 dmaLength
= db
->fragSize
>> 4;
624 outl(dmaLength
, s
->io
+ VRC5477_ADC1L
);
625 outl(dmaLength
, s
->io
+ VRC5477_ADC2L
);
628 outl(VRC5477_DMA_ACTIVATION
, s
->io
+ VRC5477_ADC1_CTRL
);
629 outl(VRC5477_DMA_ACTIVATION
, s
->io
+ VRC5477_ADC2_CTRL
);
631 /* enable adc slots */
632 temp
= inl(s
->io
+ VRC5477_CTRL
);
633 temp
|= (VRC5477_CTRL_ADC1ENB
| VRC5477_CTRL_ADC2ENB
);
634 outl (temp
, s
->io
+ VRC5477_CTRL
);
636 /* it is time to setup next dma transfer */
637 temp
= db
->nextIn
+ db
->fragSize
;
638 if (temp
>= db
->fragTotalSize
) {
639 ASSERT(temp
== db
->fragTotalSize
);
642 outl(db
->lbufDma
+ temp
, s
->io
+ VRC5477_ADC1_BADDR
);
643 outl(db
->rbufDma
+ temp
, s
->io
+ VRC5477_ADC2_BADDR
);
647 spin_unlock_irqrestore(&s
->lock
, flags
);
650 /* --------------------------------------------------------------------- */
652 #define DMABUF_DEFAULTORDER (16-PAGE_SHIFT)
653 #define DMABUF_MINORDER 1
655 static inline void dealloc_dmabuf(struct vrc5477_ac97_state
*s
,
660 pci_free_consistent(s
->dev
, PAGE_SIZE
<< db
->bufOrder
,
661 db
->lbuf
, db
->lbufDma
);
662 pci_free_consistent(s
->dev
, PAGE_SIZE
<< db
->bufOrder
,
663 db
->rbuf
, db
->rbufDma
);
664 db
->lbuf
= db
->rbuf
= NULL
;
666 db
->nextIn
= db
->nextOut
= 0;
670 static int prog_dmabuf(struct vrc5477_ac97_state
*s
,
681 for (order
= DMABUF_DEFAULTORDER
;
682 order
>= DMABUF_MINORDER
;
684 db
->lbuf
= pci_alloc_consistent(s
->dev
,
687 db
->rbuf
= pci_alloc_consistent(s
->dev
,
690 if (db
->lbuf
&& db
->rbuf
) break;
693 pci_free_consistent(s
->dev
,
704 db
->bufOrder
= order
;
708 db
->nextIn
= db
->nextOut
= 0;
710 bufsize
= PAGE_SIZE
<< db
->bufOrder
;
711 db
->fragShift
= ld2(rate
* 2 / 100);
712 if (db
->fragShift
< 4) db
->fragShift
= 4;
714 db
->numFrag
= bufsize
>> db
->fragShift
;
715 while (db
->numFrag
< 4 && db
->fragShift
> 4) {
717 db
->numFrag
= bufsize
>> db
->fragShift
;
719 db
->fragSize
= 1 << db
->fragShift
;
720 db
->fragTotalSize
= db
->numFrag
<< db
->fragShift
;
721 memset(db
->lbuf
, 0, db
->fragTotalSize
);
722 memset(db
->rbuf
, 0, db
->fragTotalSize
);
729 static inline int prog_dmabuf_adc(struct vrc5477_ac97_state
*s
)
732 return prog_dmabuf(s
, &s
->dma_adc
, s
->adcRate
);
735 static inline int prog_dmabuf_dac(struct vrc5477_ac97_state
*s
)
738 return prog_dmabuf(s
, &s
->dma_dac
, s
->dacRate
);
742 /* --------------------------------------------------------------------- */
743 /* hold spinlock for the following! */
745 static inline void vrc5477_ac97_adc_interrupt(struct vrc5477_ac97_state
*s
)
747 struct dmabuf
* adc
= &s
->dma_adc
;
750 /* we need two frags avaiable because one is already being used
751 * and the other will be used when next interrupt happens.
753 if (adc
->count
>= adc
->fragTotalSize
- adc
->fragSize
) {
756 printk(KERN_INFO PFX
"adc overrun\n");
760 /* set the base addr for next DMA transfer */
761 temp
= adc
->nextIn
+ 2*adc
->fragSize
;
762 if (temp
>= adc
->fragTotalSize
) {
763 ASSERT( (temp
== adc
->fragTotalSize
) ||
764 (temp
== adc
->fragTotalSize
+ adc
->fragSize
) );
765 temp
-= adc
->fragTotalSize
;
767 outl(adc
->lbufDma
+ temp
, s
->io
+ VRC5477_ADC1_BADDR
);
768 outl(adc
->rbufDma
+ temp
, s
->io
+ VRC5477_ADC2_BADDR
);
771 adc
->nextIn
+= adc
->fragSize
;
772 if (adc
->nextIn
>= adc
->fragTotalSize
) {
773 ASSERT(adc
->nextIn
== adc
->fragTotalSize
);
778 adc
->count
+= adc
->fragSize
;
780 /* wake up anybody listening */
781 if (waitqueue_active(&adc
->wait
)) {
782 wake_up_interruptible(&adc
->wait
);
786 static inline void vrc5477_ac97_dac_interrupt(struct vrc5477_ac97_state
*s
)
788 struct dmabuf
* dac
= &s
->dma_dac
;
791 /* next DMA transfer should already started */
792 // ASSERT(inl(s->io + VRC5477_DAC1_CTRL) & VRC5477_DMA_WIP);
793 // ASSERT(inl(s->io + VRC5477_DAC2_CTRL) & VRC5477_DMA_WIP);
795 /* let us set for next next DMA transfer */
796 temp
= dac
->nextOut
+ dac
->fragSize
*2;
797 if (temp
>= dac
->fragTotalSize
) {
798 ASSERT( (temp
== dac
->fragTotalSize
) ||
799 (temp
== dac
->fragTotalSize
+ dac
->fragSize
) );
800 temp
-= dac
->fragTotalSize
;
802 outl(dac
->lbufDma
+ temp
, s
->io
+ VRC5477_DAC1_BADDR
);
803 if (s
->dacChannels
== 1) {
804 outl(dac
->lbufDma
+ temp
, s
->io
+ VRC5477_DAC2_BADDR
);
806 outl(dac
->rbufDma
+ temp
, s
->io
+ VRC5477_DAC2_BADDR
);
809 #if defined(VRC5477_AC97_VERBOSE_DEBUG)
810 if (*(u16
*)(dac
->lbuf
+ dac
->nextOut
) != outTicket
) {
811 printk("assert fail: - %d vs %d\n",
812 *(u16
*)(dac
->lbuf
+ dac
->nextOut
),
818 /* adjust nextOut pointer */
819 dac
->nextOut
+= dac
->fragSize
;
820 if (dac
->nextOut
>= dac
->fragTotalSize
) {
821 ASSERT(dac
->nextOut
== dac
->fragTotalSize
);
826 dac
->count
-= dac
->fragSize
;
827 if (dac
->count
<=0 ) {
828 /* buffer under run */
830 dac
->nextIn
= dac
->nextOut
;
834 #if defined(VRC5477_AC97_VERBOSE_DEBUG)
837 ASSERT(*(u16
*)(dac
->lbuf
+ dac
->nextOut
) == outTicket
);
841 /* we cannot have both under run and someone is waiting on us */
842 ASSERT(! (waitqueue_active(&dac
->wait
) && (dac
->count
<= 0)) );
844 /* wake up anybody listening */
845 if (waitqueue_active(&dac
->wait
))
846 wake_up_interruptible(&dac
->wait
);
849 static irqreturn_t
vrc5477_ac97_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
851 struct vrc5477_ac97_state
*s
= (struct vrc5477_ac97_state
*)dev_id
;
853 u32 adcInterrupts
, dacInterrupts
;
857 /* get irqStatus and clear the detected ones */
858 irqStatus
= inl(s
->io
+ VRC5477_INT_STATUS
);
859 outl(irqStatus
, s
->io
+ VRC5477_INT_CLR
);
861 /* let us see what we get */
862 dacInterrupts
= VRC5477_INT_MASK_DAC1END
| VRC5477_INT_MASK_DAC2END
;
863 adcInterrupts
= VRC5477_INT_MASK_ADC1END
| VRC5477_INT_MASK_ADC2END
;
864 if (irqStatus
& dacInterrupts
) {
865 /* we should get both interrupts, but just in case ... */
866 if (irqStatus
& VRC5477_INT_MASK_DAC1END
) {
867 vrc5477_ac97_dac_interrupt(s
);
869 if ( (irqStatus
& dacInterrupts
) != dacInterrupts
) {
870 printk(KERN_WARNING
"vrc5477_ac97 : dac interrupts not in sync!!!\n");
874 } else if (irqStatus
& adcInterrupts
) {
875 /* we should get both interrupts, but just in case ... */
876 if(irqStatus
& VRC5477_INT_MASK_ADC1END
) {
877 vrc5477_ac97_adc_interrupt(s
);
879 if ( (irqStatus
& adcInterrupts
) != adcInterrupts
) {
880 printk(KERN_WARNING
"vrc5477_ac97 : adc interrupts not in sync!!!\n");
886 spin_unlock(&s
->lock
);
890 /* --------------------------------------------------------------------- */
892 static int vrc5477_ac97_open_mixdev(struct inode
*inode
, struct file
*file
)
894 int minor
= iminor(inode
);
895 struct list_head
*list
;
896 struct vrc5477_ac97_state
*s
;
898 for (list
= devs
.next
; ; list
= list
->next
) {
901 s
= list_entry(list
, struct vrc5477_ac97_state
, devs
);
902 if (s
->codec
->dev_mixer
== minor
)
905 file
->private_data
= s
;
906 return nonseekable_open(inode
, file
);
909 static int vrc5477_ac97_release_mixdev(struct inode
*inode
, struct file
*file
)
915 static int mixdev_ioctl(struct ac97_codec
*codec
, unsigned int cmd
,
918 return codec
->mixer_ioctl(codec
, cmd
, arg
);
921 static int vrc5477_ac97_ioctl_mixdev(struct inode
*inode
, struct file
*file
,
922 unsigned int cmd
, unsigned long arg
)
924 struct vrc5477_ac97_state
*s
=
925 (struct vrc5477_ac97_state
*)file
->private_data
;
926 struct ac97_codec
*codec
= s
->codec
;
928 return mixdev_ioctl(codec
, cmd
, arg
);
931 static /*const*/ struct file_operations vrc5477_ac97_mixer_fops
= {
932 .owner
= THIS_MODULE
,
934 .ioctl
= vrc5477_ac97_ioctl_mixdev
,
935 .open
= vrc5477_ac97_open_mixdev
,
936 .release
= vrc5477_ac97_release_mixdev
,
939 /* --------------------------------------------------------------------- */
941 static int drain_dac(struct vrc5477_ac97_state
*s
, int nonblock
)
946 if (!s
->dma_dac
.ready
)
950 spin_lock_irqsave(&s
->lock
, flags
);
951 count
= s
->dma_dac
.count
;
952 spin_unlock_irqrestore(&s
->lock
, flags
);
955 if (signal_pending(current
))
959 tmo
= 1000 * count
/ s
->dacRate
/ 2;
960 vrc5477_ac97_delay(tmo
);
962 if (signal_pending(current
))
967 /* --------------------------------------------------------------------- */
970 copy_two_channel_adc_to_user(struct vrc5477_ac97_state
*s
,
974 struct dmabuf
*db
= &s
->dma_adc
;
975 int bufStart
= db
->nextOut
;
976 for (; copyCount
> 0; ) {
978 int count
= copyCount
;
979 if (count
> WORK_BUF_SIZE
/2) count
= WORK_BUF_SIZE
/2;
980 for (i
=0; i
< count
/2; i
++) {
981 s
->workBuf
[i
].lchannel
=
982 *(u16
*)(db
->lbuf
+ bufStart
+ i
*2);
983 s
->workBuf
[i
].rchannel
=
984 *(u16
*)(db
->rbuf
+ bufStart
+ i
*2);
986 if (copy_to_user(buffer
, s
->workBuf
, count
*2)) {
992 ASSERT(bufStart
<= db
->fragTotalSize
);
998 /* return the total bytes that is copied */
1000 copy_adc_to_user(struct vrc5477_ac97_state
*s
,
1005 struct dmabuf
*db
= &s
->dma_adc
;
1007 int copyFragCount
=0;
1008 int totalCopyCount
= 0;
1009 int totalCopyFragCount
= 0;
1010 unsigned long flags
;
1012 /* adjust count to signel channel byte count */
1013 count
>>= s
->adcChannels
- 1;
1015 /* we may have to "copy" twice as ring buffer wraps around */
1016 for (; (avail
> 0) && (count
> 0); ) {
1017 /* determine max possible copy count for single channel */
1019 if (copyCount
> avail
) {
1022 if (copyCount
+ db
->nextOut
> db
->fragTotalSize
) {
1023 copyCount
= db
->fragTotalSize
- db
->nextOut
;
1024 ASSERT((copyCount
% db
->fragSize
) == 0);
1027 copyFragCount
= (copyCount
-1) >> db
->fragShift
;
1028 copyFragCount
= (copyFragCount
+1) << db
->fragShift
;
1029 ASSERT(copyFragCount
>= copyCount
);
1031 /* we copy differently based on adc channels */
1032 if (s
->adcChannels
== 1) {
1033 if (copy_to_user(buffer
,
1034 db
->lbuf
+ db
->nextOut
,
1038 /* *sigh* we have to mix two streams into one */
1039 if (copy_two_channel_adc_to_user(s
, buffer
, copyCount
))
1044 totalCopyCount
+= copyCount
;
1045 avail
-= copyFragCount
;
1046 totalCopyFragCount
+= copyFragCount
;
1048 buffer
+= copyCount
<< (s
->adcChannels
-1);
1050 db
->nextOut
+= copyFragCount
;
1051 if (db
->nextOut
>= db
->fragTotalSize
) {
1052 ASSERT(db
->nextOut
== db
->fragTotalSize
);
1056 ASSERT((copyFragCount
% db
->fragSize
) == 0);
1057 ASSERT( (count
== 0) || (copyCount
== copyFragCount
));
1060 spin_lock_irqsave(&s
->lock
, flags
);
1061 db
->count
-= totalCopyFragCount
;
1062 spin_unlock_irqrestore(&s
->lock
, flags
);
1064 return totalCopyCount
<< (s
->adcChannels
-1);
1068 vrc5477_ac97_read(struct file
*file
,
1073 struct vrc5477_ac97_state
*s
=
1074 (struct vrc5477_ac97_state
*)file
->private_data
;
1075 struct dmabuf
*db
= &s
->dma_adc
;
1077 unsigned long flags
;
1081 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
1087 // wait for samples in capture buffer
1089 spin_lock_irqsave(&s
->lock
, flags
);
1093 spin_unlock_irqrestore(&s
->lock
, flags
);
1095 if (file
->f_flags
& O_NONBLOCK
) {
1100 interruptible_sleep_on(&db
->wait
);
1101 if (signal_pending(current
)) {
1107 } while (avail
<= 0);
1109 ASSERT( (avail
% db
->fragSize
) == 0);
1110 copyCount
= copy_adc_to_user(s
, buffer
, count
, avail
);
1111 if (copyCount
<=0 ) {
1112 if (!ret
) ret
= -EFAULT
;
1117 buffer
+= copyCount
;
1119 } // while (count > 0)
1125 copy_two_channel_dac_from_user(struct vrc5477_ac97_state
*s
,
1129 struct dmabuf
*db
= &s
->dma_dac
;
1130 int bufStart
= db
->nextIn
;
1134 for (; copyCount
> 0; ) {
1136 int count
= copyCount
;
1137 if (count
> WORK_BUF_SIZE
/2) count
= WORK_BUF_SIZE
/2;
1138 if (copy_from_user(s
->workBuf
, buffer
, count
*2)) {
1141 for (i
=0; i
< count
/2; i
++) {
1142 *(u16
*)(db
->lbuf
+ bufStart
+ i
*2) =
1143 s
->workBuf
[i
].lchannel
;
1144 *(u16
*)(db
->rbuf
+ bufStart
+ i
*2) =
1145 s
->workBuf
[i
].rchannel
;
1150 ASSERT(bufStart
<= db
->fragTotalSize
);
1157 /* return the total bytes that is copied */
1159 copy_dac_from_user(struct vrc5477_ac97_state
*s
,
1164 struct dmabuf
*db
= &s
->dma_dac
;
1166 int copyFragCount
=0;
1167 int totalCopyCount
= 0;
1168 int totalCopyFragCount
= 0;
1169 unsigned long flags
;
1170 #if defined(VRC5477_AC97_VERBOSE_DEBUG)
1174 /* adjust count to signel channel byte count */
1175 count
>>= s
->dacChannels
- 1;
1177 /* we may have to "copy" twice as ring buffer wraps around */
1178 for (; (avail
> 0) && (count
> 0); ) {
1179 /* determine max possible copy count for single channel */
1181 if (copyCount
> avail
) {
1184 if (copyCount
+ db
->nextIn
> db
->fragTotalSize
) {
1185 copyCount
= db
->fragTotalSize
- db
->nextIn
;
1186 ASSERT(copyCount
> 0);
1189 copyFragCount
= copyCount
;
1190 ASSERT(copyFragCount
>= copyCount
);
1192 /* we copy differently based on the number channels */
1193 if (s
->dacChannels
== 1) {
1194 if (copy_from_user(db
->lbuf
+ db
->nextIn
,
1198 /* fill gaps with 0 */
1199 memset(db
->lbuf
+ db
->nextIn
+ copyCount
,
1201 copyFragCount
- copyCount
);
1203 /* we have demux the stream into two separate ones */
1204 if (copy_two_channel_dac_from_user(s
, buffer
, copyCount
))
1206 /* fill gaps with 0 */
1207 memset(db
->lbuf
+ db
->nextIn
+ copyCount
,
1209 copyFragCount
- copyCount
);
1210 memset(db
->rbuf
+ db
->nextIn
+ copyCount
,
1212 copyFragCount
- copyCount
);
1215 #if defined(VRC5477_AC97_VERBOSE_DEBUG)
1216 for (i
=0; i
< copyFragCount
; i
+= db
->fragSize
) {
1217 *(u16
*)(db
->lbuf
+ db
->nextIn
+ i
) = inTicket
++;
1222 totalCopyCount
+= copyCount
;
1223 avail
-= copyFragCount
;
1224 totalCopyFragCount
+= copyFragCount
;
1226 buffer
+= copyCount
<< (s
->dacChannels
- 1);
1228 db
->nextIn
+= copyFragCount
;
1229 if (db
->nextIn
>= db
->fragTotalSize
) {
1230 ASSERT(db
->nextIn
== db
->fragTotalSize
);
1234 ASSERT( (count
== 0) || (copyCount
== copyFragCount
));
1237 spin_lock_irqsave(&s
->lock
, flags
);
1238 db
->count
+= totalCopyFragCount
;
1243 /* nextIn should not be equal to nextOut unless we are full */
1244 ASSERT( ( (db
->count
== db
->fragTotalSize
) &&
1245 (db
->nextIn
== db
->nextOut
) ) ||
1246 ( (db
->count
< db
->fragTotalSize
) &&
1247 (db
->nextIn
!= db
->nextOut
) ) );
1249 spin_unlock_irqrestore(&s
->lock
, flags
);
1251 return totalCopyCount
<< (s
->dacChannels
-1);
1255 static ssize_t
vrc5477_ac97_write(struct file
*file
, const char *buffer
,
1256 size_t count
, loff_t
*ppos
)
1258 struct vrc5477_ac97_state
*s
=
1259 (struct vrc5477_ac97_state
*)file
->private_data
;
1260 struct dmabuf
*db
= &s
->dma_dac
;
1262 unsigned long flags
;
1263 int copyCount
, avail
;
1265 if (!access_ok(VERIFY_READ
, buffer
, count
))
1270 // wait for space in playback buffer
1272 spin_lock_irqsave(&s
->lock
, flags
);
1273 avail
= db
->fragTotalSize
- db
->count
;
1274 spin_unlock_irqrestore(&s
->lock
, flags
);
1276 if (file
->f_flags
& O_NONBLOCK
) {
1281 interruptible_sleep_on(&db
->wait
);
1282 if (signal_pending(current
)) {
1288 } while (avail
<= 0);
1290 copyCount
= copy_dac_from_user(s
, buffer
, count
, avail
);
1291 if (copyCount
< 0) {
1292 if (!ret
) ret
= -EFAULT
;
1297 buffer
+= copyCount
;
1299 } // while (count > 0)
1304 /* No kernel lock - we have our own spinlock */
1305 static unsigned int vrc5477_ac97_poll(struct file
*file
,
1306 struct poll_table_struct
*wait
)
1308 struct vrc5477_ac97_state
*s
= (struct vrc5477_ac97_state
*)file
->private_data
;
1309 unsigned long flags
;
1310 unsigned int mask
= 0;
1312 if (file
->f_mode
& FMODE_WRITE
)
1313 poll_wait(file
, &s
->dma_dac
.wait
, wait
);
1314 if (file
->f_mode
& FMODE_READ
)
1315 poll_wait(file
, &s
->dma_adc
.wait
, wait
);
1316 spin_lock_irqsave(&s
->lock
, flags
);
1317 if (file
->f_mode
& FMODE_READ
) {
1318 if (s
->dma_adc
.count
>= (signed)s
->dma_adc
.fragSize
)
1319 mask
|= POLLIN
| POLLRDNORM
;
1321 if (file
->f_mode
& FMODE_WRITE
) {
1322 if ((signed)s
->dma_dac
.fragTotalSize
>=
1323 s
->dma_dac
.count
+ (signed)s
->dma_dac
.fragSize
)
1324 mask
|= POLLOUT
| POLLWRNORM
;
1326 spin_unlock_irqrestore(&s
->lock
, flags
);
1330 #ifdef VRC5477_AC97_DEBUG
1331 static struct ioctl_str_t
{
1335 {SNDCTL_DSP_RESET
, "SNDCTL_DSP_RESET"},
1336 {SNDCTL_DSP_SYNC
, "SNDCTL_DSP_SYNC"},
1337 {SNDCTL_DSP_SPEED
, "SNDCTL_DSP_SPEED"},
1338 {SNDCTL_DSP_STEREO
, "SNDCTL_DSP_STEREO"},
1339 {SNDCTL_DSP_GETBLKSIZE
, "SNDCTL_DSP_GETBLKSIZE"},
1340 {SNDCTL_DSP_SETFMT
, "SNDCTL_DSP_SETFMT"},
1341 {SNDCTL_DSP_SAMPLESIZE
, "SNDCTL_DSP_SAMPLESIZE"},
1342 {SNDCTL_DSP_CHANNELS
, "SNDCTL_DSP_CHANNELS"},
1343 {SOUND_PCM_WRITE_CHANNELS
, "SOUND_PCM_WRITE_CHANNELS"},
1344 {SOUND_PCM_WRITE_FILTER
, "SOUND_PCM_WRITE_FILTER"},
1345 {SNDCTL_DSP_POST
, "SNDCTL_DSP_POST"},
1346 {SNDCTL_DSP_SUBDIVIDE
, "SNDCTL_DSP_SUBDIVIDE"},
1347 {SNDCTL_DSP_SETFRAGMENT
, "SNDCTL_DSP_SETFRAGMENT"},
1348 {SNDCTL_DSP_GETFMTS
, "SNDCTL_DSP_GETFMTS"},
1349 {SNDCTL_DSP_GETOSPACE
, "SNDCTL_DSP_GETOSPACE"},
1350 {SNDCTL_DSP_GETISPACE
, "SNDCTL_DSP_GETISPACE"},
1351 {SNDCTL_DSP_NONBLOCK
, "SNDCTL_DSP_NONBLOCK"},
1352 {SNDCTL_DSP_GETCAPS
, "SNDCTL_DSP_GETCAPS"},
1353 {SNDCTL_DSP_GETTRIGGER
, "SNDCTL_DSP_GETTRIGGER"},
1354 {SNDCTL_DSP_SETTRIGGER
, "SNDCTL_DSP_SETTRIGGER"},
1355 {SNDCTL_DSP_GETIPTR
, "SNDCTL_DSP_GETIPTR"},
1356 {SNDCTL_DSP_GETOPTR
, "SNDCTL_DSP_GETOPTR"},
1357 {SNDCTL_DSP_MAPINBUF
, "SNDCTL_DSP_MAPINBUF"},
1358 {SNDCTL_DSP_MAPOUTBUF
, "SNDCTL_DSP_MAPOUTBUF"},
1359 {SNDCTL_DSP_SETSYNCRO
, "SNDCTL_DSP_SETSYNCRO"},
1360 {SNDCTL_DSP_SETDUPLEX
, "SNDCTL_DSP_SETDUPLEX"},
1361 {SNDCTL_DSP_GETODELAY
, "SNDCTL_DSP_GETODELAY"},
1362 {SNDCTL_DSP_GETCHANNELMASK
, "SNDCTL_DSP_GETCHANNELMASK"},
1363 {SNDCTL_DSP_BIND_CHANNEL
, "SNDCTL_DSP_BIND_CHANNEL"},
1364 {OSS_GETVERSION
, "OSS_GETVERSION"},
1365 {SOUND_PCM_READ_RATE
, "SOUND_PCM_READ_RATE"},
1366 {SOUND_PCM_READ_CHANNELS
, "SOUND_PCM_READ_CHANNELS"},
1367 {SOUND_PCM_READ_BITS
, "SOUND_PCM_READ_BITS"},
1368 {SOUND_PCM_READ_FILTER
, "SOUND_PCM_READ_FILTER"}
1372 static int vrc5477_ac97_ioctl(struct inode
*inode
, struct file
*file
,
1373 unsigned int cmd
, unsigned long arg
)
1375 struct vrc5477_ac97_state
*s
= (struct vrc5477_ac97_state
*)file
->private_data
;
1376 unsigned long flags
;
1377 audio_buf_info abinfo
;
1381 #ifdef VRC5477_AC97_DEBUG
1382 for (count
=0; count
<sizeof(ioctl_str
)/sizeof(ioctl_str
[0]); count
++) {
1383 if (ioctl_str
[count
].cmd
== cmd
)
1386 if (count
< sizeof(ioctl_str
)/sizeof(ioctl_str
[0]))
1387 printk(KERN_INFO PFX
"ioctl %s\n", ioctl_str
[count
].str
);
1389 printk(KERN_INFO PFX
"ioctl unknown, 0x%x\n", cmd
);
1393 case OSS_GETVERSION
:
1394 return put_user(SOUND_VERSION
, (int *)arg
);
1396 case SNDCTL_DSP_SYNC
:
1397 if (file
->f_mode
& FMODE_WRITE
)
1398 return drain_dac(s
, file
->f_flags
& O_NONBLOCK
);
1401 case SNDCTL_DSP_SETDUPLEX
:
1404 case SNDCTL_DSP_GETCAPS
:
1405 return put_user(DSP_CAP_DUPLEX
, (int *)arg
);
1407 case SNDCTL_DSP_RESET
:
1408 if (file
->f_mode
& FMODE_WRITE
) {
1410 synchronize_irq(s
->irq
);
1411 s
->dma_dac
.count
= 0;
1412 s
->dma_dac
.nextIn
= s
->dma_dac
.nextOut
= 0;
1414 if (file
->f_mode
& FMODE_READ
) {
1416 synchronize_irq(s
->irq
);
1417 s
->dma_adc
.count
= 0;
1418 s
->dma_adc
.nextIn
= s
->dma_adc
.nextOut
= 0;
1422 case SNDCTL_DSP_SPEED
:
1423 if (get_user(val
, (int *)arg
))
1426 if (file
->f_mode
& FMODE_READ
) {
1428 set_adc_rate(s
, val
);
1429 if ((ret
= prog_dmabuf_adc(s
)))
1432 if (file
->f_mode
& FMODE_WRITE
) {
1434 set_dac_rate(s
, val
);
1435 if ((ret
= prog_dmabuf_dac(s
)))
1439 return put_user((file
->f_mode
& FMODE_READ
) ?
1440 s
->adcRate
: s
->dacRate
, (int *)arg
);
1442 case SNDCTL_DSP_STEREO
:
1443 if (get_user(val
, (int *)arg
))
1445 if (file
->f_mode
& FMODE_READ
) {
1451 if ((ret
= prog_dmabuf_adc(s
)))
1454 if (file
->f_mode
& FMODE_WRITE
) {
1460 if ((ret
= prog_dmabuf_dac(s
)))
1465 case SNDCTL_DSP_CHANNELS
:
1466 if (get_user(val
, (int *)arg
))
1469 if ( (val
!= 1) && (val
!= 2)) val
= 2;
1471 if (file
->f_mode
& FMODE_READ
) {
1473 s
->dacChannels
= val
;
1474 if ((ret
= prog_dmabuf_adc(s
)))
1477 if (file
->f_mode
& FMODE_WRITE
) {
1479 s
->dacChannels
= val
;
1480 if ((ret
= prog_dmabuf_dac(s
)))
1484 return put_user(val
, (int *)arg
);
1486 case SNDCTL_DSP_GETFMTS
: /* Returns a mask */
1487 return put_user(AFMT_S16_LE
, (int *)arg
);
1489 case SNDCTL_DSP_SETFMT
: /* Selects ONE fmt*/
1490 if (get_user(val
, (int *)arg
))
1492 if (val
!= AFMT_QUERY
) {
1493 if (val
!= AFMT_S16_LE
) return -EINVAL
;
1494 if (file
->f_mode
& FMODE_READ
) {
1496 if ((ret
= prog_dmabuf_adc(s
)))
1499 if (file
->f_mode
& FMODE_WRITE
) {
1501 if ((ret
= prog_dmabuf_dac(s
)))
1507 return put_user(val
, (int *)arg
);
1509 case SNDCTL_DSP_POST
:
1512 case SNDCTL_DSP_GETTRIGGER
:
1513 case SNDCTL_DSP_SETTRIGGER
:
1517 case SNDCTL_DSP_GETOSPACE
:
1518 if (!(file
->f_mode
& FMODE_WRITE
))
1520 abinfo
.fragsize
= s
->dma_dac
.fragSize
<< (s
->dacChannels
-1);
1521 spin_lock_irqsave(&s
->lock
, flags
);
1522 count
= s
->dma_dac
.count
;
1523 spin_unlock_irqrestore(&s
->lock
, flags
);
1524 abinfo
.bytes
= (s
->dma_dac
.fragTotalSize
- count
) <<
1526 abinfo
.fragstotal
= s
->dma_dac
.numFrag
;
1527 abinfo
.fragments
= abinfo
.bytes
>> s
->dma_dac
.fragShift
>>
1529 return copy_to_user((void *)arg
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1531 case SNDCTL_DSP_GETISPACE
:
1532 if (!(file
->f_mode
& FMODE_READ
))
1534 abinfo
.fragsize
= s
->dma_adc
.fragSize
<< (s
->adcChannels
-1);
1535 spin_lock_irqsave(&s
->lock
, flags
);
1536 count
= s
->dma_adc
.count
;
1537 spin_unlock_irqrestore(&s
->lock
, flags
);
1540 abinfo
.bytes
= count
<< (s
->adcChannels
-1);
1541 abinfo
.fragstotal
= s
->dma_adc
.numFrag
;
1542 abinfo
.fragments
= (abinfo
.bytes
>> s
->dma_adc
.fragShift
) >>
1544 return copy_to_user((void *)arg
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1546 case SNDCTL_DSP_NONBLOCK
:
1547 file
->f_flags
|= O_NONBLOCK
;
1550 case SNDCTL_DSP_GETODELAY
:
1551 if (!(file
->f_mode
& FMODE_WRITE
))
1553 spin_lock_irqsave(&s
->lock
, flags
);
1554 count
= s
->dma_dac
.count
;
1555 spin_unlock_irqrestore(&s
->lock
, flags
);
1556 return put_user(count
, (int *)arg
);
1558 case SNDCTL_DSP_GETIPTR
:
1559 case SNDCTL_DSP_GETOPTR
:
1560 /* we cannot get DMA ptr */
1563 case SNDCTL_DSP_GETBLKSIZE
:
1564 if (file
->f_mode
& FMODE_WRITE
)
1565 return put_user(s
->dma_dac
.fragSize
<< (s
->dacChannels
-1), (int *)arg
);
1567 return put_user(s
->dma_adc
.fragSize
<< (s
->adcChannels
-1), (int *)arg
);
1569 case SNDCTL_DSP_SETFRAGMENT
:
1570 /* we ignore fragment size request */
1573 case SNDCTL_DSP_SUBDIVIDE
:
1574 /* what is this for? [jsun] */
1577 case SOUND_PCM_READ_RATE
:
1578 return put_user((file
->f_mode
& FMODE_READ
) ?
1579 s
->adcRate
: s
->dacRate
, (int *)arg
);
1581 case SOUND_PCM_READ_CHANNELS
:
1582 if (file
->f_mode
& FMODE_READ
)
1583 return put_user(s
->adcChannels
, (int *)arg
);
1585 return put_user(s
->dacChannels
? 2 : 1, (int *)arg
);
1587 case SOUND_PCM_READ_BITS
:
1588 return put_user(16, (int *)arg
);
1590 case SOUND_PCM_WRITE_FILTER
:
1591 case SNDCTL_DSP_SETSYNCRO
:
1592 case SOUND_PCM_READ_FILTER
:
1596 return mixdev_ioctl(s
->codec
, cmd
, arg
);
1600 static int vrc5477_ac97_open(struct inode
*inode
, struct file
*file
)
1602 int minor
= iminor(inode
);
1603 DECLARE_WAITQUEUE(wait
, current
);
1604 unsigned long flags
;
1605 struct list_head
*list
;
1606 struct vrc5477_ac97_state
*s
;
1609 nonseekable_open(inode
, file
);
1610 for (list
= devs
.next
; ; list
= list
->next
) {
1613 s
= list_entry(list
, struct vrc5477_ac97_state
, devs
);
1614 if (!((s
->dev_audio
^ minor
) & ~0xf))
1617 file
->private_data
= s
;
1619 /* wait for device to become free */
1621 while (s
->open_mode
& file
->f_mode
) {
1623 if (file
->f_flags
& O_NONBLOCK
) {
1627 add_wait_queue(&s
->open_wait
, &wait
);
1628 __set_current_state(TASK_INTERRUPTIBLE
);
1631 remove_wait_queue(&s
->open_wait
, &wait
);
1632 set_current_state(TASK_RUNNING
);
1633 if (signal_pending(current
))
1634 return -ERESTARTSYS
;
1638 spin_lock_irqsave(&s
->lock
, flags
);
1640 if (file
->f_mode
& FMODE_READ
) {
1641 /* set default settings */
1642 set_adc_rate(s
, 48000);
1645 ret
= prog_dmabuf_adc(s
);
1646 if (ret
) goto bailout
;
1648 if (file
->f_mode
& FMODE_WRITE
) {
1649 /* set default settings */
1650 set_dac_rate(s
, 48000);
1653 ret
= prog_dmabuf_dac(s
);
1654 if (ret
) goto bailout
;
1657 s
->open_mode
|= file
->f_mode
& (FMODE_READ
| FMODE_WRITE
);
1660 spin_unlock_irqrestore(&s
->lock
, flags
);
1666 static int vrc5477_ac97_release(struct inode
*inode
, struct file
*file
)
1668 struct vrc5477_ac97_state
*s
=
1669 (struct vrc5477_ac97_state
*)file
->private_data
;
1672 if (file
->f_mode
& FMODE_WRITE
)
1673 drain_dac(s
, file
->f_flags
& O_NONBLOCK
);
1675 if (file
->f_mode
& FMODE_WRITE
) {
1677 dealloc_dmabuf(s
, &s
->dma_dac
);
1679 if (file
->f_mode
& FMODE_READ
) {
1681 dealloc_dmabuf(s
, &s
->dma_adc
);
1683 s
->open_mode
&= (~file
->f_mode
) & (FMODE_READ
|FMODE_WRITE
);
1685 wake_up(&s
->open_wait
);
1690 static /*const*/ struct file_operations vrc5477_ac97_audio_fops
= {
1691 .owner
= THIS_MODULE
,
1692 .llseek
= no_llseek
,
1693 .read
= vrc5477_ac97_read
,
1694 .write
= vrc5477_ac97_write
,
1695 .poll
= vrc5477_ac97_poll
,
1696 .ioctl
= vrc5477_ac97_ioctl
,
1697 // .mmap = vrc5477_ac97_mmap,
1698 .open
= vrc5477_ac97_open
,
1699 .release
= vrc5477_ac97_release
,
1703 /* --------------------------------------------------------------------- */
1706 /* --------------------------------------------------------------------- */
1709 * for debugging purposes, we'll create a proc device that dumps the
1713 #ifdef VRC5477_AC97_DEBUG
1716 const char *regname
;
1718 } vrc5477_ac97_regs
[] = {
1719 {"VRC5477_INT_STATUS", VRC5477_INT_STATUS
},
1720 {"VRC5477_CODEC_WR", VRC5477_CODEC_WR
},
1721 {"VRC5477_CODEC_RD", VRC5477_CODEC_RD
},
1722 {"VRC5477_CTRL", VRC5477_CTRL
},
1723 {"VRC5477_ACLINK_CTRL", VRC5477_ACLINK_CTRL
},
1724 {"VRC5477_INT_MASK", VRC5477_INT_MASK
},
1725 {"VRC5477_DAC1_CTRL", VRC5477_DAC1_CTRL
},
1726 {"VRC5477_DAC1L", VRC5477_DAC1L
},
1727 {"VRC5477_DAC1_BADDR", VRC5477_DAC1_BADDR
},
1728 {"VRC5477_DAC2_CTRL", VRC5477_DAC2_CTRL
},
1729 {"VRC5477_DAC2L", VRC5477_DAC2L
},
1730 {"VRC5477_DAC2_BADDR", VRC5477_DAC2_BADDR
},
1731 {"VRC5477_DAC3_CTRL", VRC5477_DAC3_CTRL
},
1732 {"VRC5477_DAC3L", VRC5477_DAC3L
},
1733 {"VRC5477_DAC3_BADDR", VRC5477_DAC3_BADDR
},
1734 {"VRC5477_ADC1_CTRL", VRC5477_ADC1_CTRL
},
1735 {"VRC5477_ADC1L", VRC5477_ADC1L
},
1736 {"VRC5477_ADC1_BADDR", VRC5477_ADC1_BADDR
},
1737 {"VRC5477_ADC2_CTRL", VRC5477_ADC2_CTRL
},
1738 {"VRC5477_ADC2L", VRC5477_ADC2L
},
1739 {"VRC5477_ADC2_BADDR", VRC5477_ADC2_BADDR
},
1740 {"VRC5477_ADC3_CTRL", VRC5477_ADC3_CTRL
},
1741 {"VRC5477_ADC3L", VRC5477_ADC3L
},
1742 {"VRC5477_ADC3_BADDR", VRC5477_ADC3_BADDR
},
1746 static int proc_vrc5477_ac97_dump (char *buf
, char **start
, off_t fpos
,
1747 int length
, int *eof
, void *data
)
1749 struct vrc5477_ac97_state
*s
;
1752 if (list_empty(&devs
))
1754 s
= list_entry(devs
.next
, struct vrc5477_ac97_state
, devs
);
1756 /* print out header */
1757 len
+= sprintf(buf
+ len
, "\n\t\tVrc5477 Audio Debug\n\n");
1759 // print out digital controller state
1760 len
+= sprintf (buf
+ len
, "NEC Vrc5477 Audio Controller registers\n");
1761 len
+= sprintf (buf
+ len
, "---------------------------------\n");
1762 for (cnt
=0; vrc5477_ac97_regs
[cnt
].regname
!= NULL
; cnt
++) {
1763 len
+= sprintf (buf
+ len
, "%-20s = %08x\n",
1764 vrc5477_ac97_regs
[cnt
].regname
,
1765 inl(s
->io
+ vrc5477_ac97_regs
[cnt
].regaddr
));
1768 /* print out driver state */
1769 len
+= sprintf (buf
+ len
, "NEC Vrc5477 Audio driver states\n");
1770 len
+= sprintf (buf
+ len
, "---------------------------------\n");
1771 len
+= sprintf (buf
+ len
, "dacChannels = %d\n", s
->dacChannels
);
1772 len
+= sprintf (buf
+ len
, "adcChannels = %d\n", s
->adcChannels
);
1773 len
+= sprintf (buf
+ len
, "dacRate = %d\n", s
->dacRate
);
1774 len
+= sprintf (buf
+ len
, "adcRate = %d\n", s
->adcRate
);
1776 len
+= sprintf (buf
+ len
, "dma_dac is %s ready\n",
1777 s
->dma_dac
.ready
? "" : "not");
1778 if (s
->dma_dac
.ready
) {
1779 len
+= sprintf (buf
+ len
, "dma_dac is %s stopped.\n",
1780 s
->dma_dac
.stopped
? "" : "not");
1781 len
+= sprintf (buf
+ len
, "dma_dac.fragSize = %x\n",
1782 s
->dma_dac
.fragSize
);
1783 len
+= sprintf (buf
+ len
, "dma_dac.fragShift = %x\n",
1784 s
->dma_dac
.fragShift
);
1785 len
+= sprintf (buf
+ len
, "dma_dac.numFrag = %x\n",
1786 s
->dma_dac
.numFrag
);
1787 len
+= sprintf (buf
+ len
, "dma_dac.fragTotalSize = %x\n",
1788 s
->dma_dac
.fragTotalSize
);
1789 len
+= sprintf (buf
+ len
, "dma_dac.nextIn = %x\n",
1791 len
+= sprintf (buf
+ len
, "dma_dac.nextOut = %x\n",
1792 s
->dma_dac
.nextOut
);
1793 len
+= sprintf (buf
+ len
, "dma_dac.count = %x\n",
1797 len
+= sprintf (buf
+ len
, "dma_adc is %s ready\n",
1798 s
->dma_adc
.ready
? "" : "not");
1799 if (s
->dma_adc
.ready
) {
1800 len
+= sprintf (buf
+ len
, "dma_adc is %s stopped.\n",
1801 s
->dma_adc
.stopped
? "" : "not");
1802 len
+= sprintf (buf
+ len
, "dma_adc.fragSize = %x\n",
1803 s
->dma_adc
.fragSize
);
1804 len
+= sprintf (buf
+ len
, "dma_adc.fragShift = %x\n",
1805 s
->dma_adc
.fragShift
);
1806 len
+= sprintf (buf
+ len
, "dma_adc.numFrag = %x\n",
1807 s
->dma_adc
.numFrag
);
1808 len
+= sprintf (buf
+ len
, "dma_adc.fragTotalSize = %x\n",
1809 s
->dma_adc
.fragTotalSize
);
1810 len
+= sprintf (buf
+ len
, "dma_adc.nextIn = %x\n",
1812 len
+= sprintf (buf
+ len
, "dma_adc.nextOut = %x\n",
1813 s
->dma_adc
.nextOut
);
1814 len
+= sprintf (buf
+ len
, "dma_adc.count = %x\n",
1818 /* print out CODEC state */
1819 len
+= sprintf (buf
+ len
, "\nAC97 CODEC registers\n");
1820 len
+= sprintf (buf
+ len
, "----------------------\n");
1821 for (cnt
=0; cnt
<= 0x7e; cnt
= cnt
+2)
1822 len
+= sprintf (buf
+ len
, "reg %02x = %04x\n",
1823 cnt
, rdcodec(s
->codec
, cnt
));
1830 *start
= buf
+ fpos
;
1831 if ((len
-= fpos
) > length
)
1837 #endif /* VRC5477_AC97_DEBUG */
1839 /* --------------------------------------------------------------------- */
1841 /* maximum number of devices; only used for command line params */
1844 static unsigned int devindex
;
1846 MODULE_AUTHOR("Monta Vista Software, jsun@mvista.com or jsun@junsun.net");
1847 MODULE_DESCRIPTION("NEC Vrc5477 audio (AC97) Driver");
1848 MODULE_LICENSE("GPL");
1850 static int __devinit
vrc5477_ac97_probe(struct pci_dev
*pcidev
,
1851 const struct pci_device_id
*pciid
)
1853 struct vrc5477_ac97_state
*s
;
1854 #ifdef VRC5477_AC97_DEBUG
1858 if (pcidev
->irq
== 0)
1861 if (!(s
= kmalloc(sizeof(struct vrc5477_ac97_state
), GFP_KERNEL
))) {
1862 printk(KERN_ERR PFX
"alloc of device struct failed\n");
1865 memset(s
, 0, sizeof(struct vrc5477_ac97_state
));
1867 init_waitqueue_head(&s
->dma_adc
.wait
);
1868 init_waitqueue_head(&s
->dma_dac
.wait
);
1869 init_waitqueue_head(&s
->open_wait
);
1870 init_MUTEX(&s
->open_sem
);
1871 spin_lock_init(&s
->lock
);
1874 s
->io
= pci_resource_start(pcidev
, 0);
1875 s
->irq
= pcidev
->irq
;
1877 s
->codec
= ac97_alloc_codec();
1879 s
->codec
->private_data
= s
;
1881 s
->codec
->codec_read
= rdcodec
;
1882 s
->codec
->codec_write
= wrcodec
;
1883 s
->codec
->codec_wait
= waitcodec
;
1885 /* setting some other default values such as
1886 * adcChannels, adcRate is done in open() so that
1887 * no persistent state across file opens.
1890 /* test if get response from ac97, if not return */
1891 if (ac97_codec_not_present(s
->codec
)) {
1892 printk(KERN_ERR PFX
"no ac97 codec\n");
1897 /* test if get response from ac97, if not return */
1898 if (ac97_codec_not_present(&(s
->codec
))) {
1899 printk(KERN_ERR PFX
"no ac97 codec\n");
1904 if (!request_region(s
->io
, pci_resource_len(pcidev
,0),
1905 VRC5477_AC97_MODULE_NAME
)) {
1906 printk(KERN_ERR PFX
"io ports %#lx->%#lx in use\n",
1907 s
->io
, s
->io
+ pci_resource_len(pcidev
,0)-1);
1910 if (request_irq(s
->irq
, vrc5477_ac97_interrupt
, SA_INTERRUPT
,
1911 VRC5477_AC97_MODULE_NAME
, s
)) {
1912 printk(KERN_ERR PFX
"irq %u in use\n", s
->irq
);
1916 printk(KERN_INFO PFX
"IO at %#lx, IRQ %d\n", s
->io
, s
->irq
);
1918 /* register devices */
1919 if ((s
->dev_audio
= register_sound_dsp(&vrc5477_ac97_audio_fops
, -1)) < 0)
1921 if ((s
->codec
->dev_mixer
=
1922 register_sound_mixer(&vrc5477_ac97_mixer_fops
, -1)) < 0)
1925 #ifdef VRC5477_AC97_DEBUG
1926 /* initialize the debug proc device */
1927 s
->ps
= create_proc_read_entry(VRC5477_AC97_MODULE_NAME
, 0, NULL
,
1928 proc_vrc5477_ac97_dump
, NULL
);
1929 #endif /* VRC5477_AC97_DEBUG */
1931 /* enable pci io and bus mastering */
1932 if (pci_enable_device(pcidev
))
1934 pci_set_master(pcidev
);
1936 /* cold reset the AC97 */
1937 outl(VRC5477_ACLINK_CTRL_RST_ON
| VRC5477_ACLINK_CTRL_RST_TIME
,
1938 s
->io
+ VRC5477_ACLINK_CTRL
);
1939 while (inl(s
->io
+ VRC5477_ACLINK_CTRL
) & VRC5477_ACLINK_CTRL_RST_ON
);
1942 if (!ac97_probe_codec(s
->codec
))
1945 #ifdef VRC5477_AC97_DEBUG
1946 sprintf(proc_str
, "driver/%s/%d/ac97",
1947 VRC5477_AC97_MODULE_NAME
, s
->codec
->id
);
1948 s
->ac97_ps
= create_proc_read_entry (proc_str
, 0, NULL
,
1949 ac97_read_proc
, s
->codec
);
1950 /* TODO : why this proc file does not show up? */
1953 /* Try to enable variable rate audio mode. */
1954 wrcodec(s
->codec
, AC97_EXTENDED_STATUS
,
1955 rdcodec(s
->codec
, AC97_EXTENDED_STATUS
) | AC97_EXTSTAT_VRA
);
1956 /* Did we enable it? */
1957 if(rdcodec(s
->codec
, AC97_EXTENDED_STATUS
) & AC97_EXTSTAT_VRA
)
1958 s
->extended_status
|= AC97_EXTSTAT_VRA
;
1961 printk(KERN_INFO PFX
"VRA mode not enabled; rate fixed at %d.",
1965 /* let us get the default volumne louder */
1966 wrcodec(s
->codec
, 0x2, 0x1010); /* master volume, middle */
1967 wrcodec(s
->codec
, 0xc, 0x10); /* phone volume, middle */
1968 // wrcodec(s->codec, 0xe, 0x10); /* misc volume, middle */
1969 wrcodec(s
->codec
, 0x10, 0x8000); /* line-in 2 line-out disable */
1970 wrcodec(s
->codec
, 0x18, 0x0707); /* PCM out (line out) middle */
1973 /* by default we select line in the input */
1974 wrcodec(s
->codec
, 0x1a, 0x0404);
1975 wrcodec(s
->codec
, 0x1c, 0x0f0f);
1976 wrcodec(s
->codec
, 0x1e, 0x07);
1978 /* enable the master interrupt but disable all others */
1979 outl(VRC5477_INT_MASK_NMASK
, s
->io
+ VRC5477_INT_MASK
);
1981 /* store it in the driver field */
1982 pci_set_drvdata(pcidev
, s
);
1983 pcidev
->dma_mask
= 0xffffffff;
1984 /* put it into driver list */
1985 list_add_tail(&s
->devs
, &devs
);
1986 /* increment devindex */
1987 if (devindex
< NR_DEVICE
-1)
1992 unregister_sound_mixer(s
->codec
->dev_mixer
);
1994 unregister_sound_dsp(s
->dev_audio
);
1996 printk(KERN_ERR PFX
"cannot register misc device\n");
1997 free_irq(s
->irq
, s
);
1999 release_region(s
->io
, pci_resource_len(pcidev
,0));
2001 ac97_release_codec(codec
);
2006 static void __devexit
vrc5477_ac97_remove(struct pci_dev
*dev
)
2008 struct vrc5477_ac97_state
*s
= pci_get_drvdata(dev
);
2014 #ifdef VRC5477_AC97_DEBUG
2016 remove_proc_entry(VRC5477_AC97_MODULE_NAME
, NULL
);
2017 #endif /* VRC5477_AC97_DEBUG */
2020 free_irq(s
->irq
, s
);
2021 release_region(s
->io
, pci_resource_len(dev
,0));
2022 unregister_sound_dsp(s
->dev_audio
);
2023 unregister_sound_mixer(s
->codec
->dev_mixer
);
2024 ac97_release_codec(s
->codec
);
2026 pci_set_drvdata(dev
, NULL
);
2030 static struct pci_device_id id_table
[] = {
2031 { PCI_VENDOR_ID_NEC
, PCI_DEVICE_ID_NEC_VRC5477_AC97
,
2032 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0 },
2036 MODULE_DEVICE_TABLE(pci
, id_table
);
2038 static struct pci_driver vrc5477_ac97_driver
= {
2039 .name
= VRC5477_AC97_MODULE_NAME
,
2040 .id_table
= id_table
,
2041 .probe
= vrc5477_ac97_probe
,
2042 .remove
= __devexit_p(vrc5477_ac97_remove
)
2045 static int __init
init_vrc5477_ac97(void)
2047 printk("Vrc5477 AC97 driver: version v0.2 time " __TIME__
" " __DATE__
" by Jun Sun\n");
2048 return pci_register_driver(&vrc5477_ac97_driver
);
2051 static void __exit
cleanup_vrc5477_ac97(void)
2053 printk(KERN_INFO PFX
"unloading\n");
2054 pci_unregister_driver(&vrc5477_ac97_driver
);
2057 module_init(init_vrc5477_ac97
);
2058 module_exit(cleanup_vrc5477_ac97
);