3 * Trident 4D-Wave/SiS 7018/ALi 5451 OSS driver for Linux 2.2.x
5 * Driver: Alan Cox <alan@redhat.com>
8 * Low level code: <audio@tridentmicro.com> from ALSA
9 * Framework: Thomas Sailer <sailer@ife.ee.ethz.ch>
10 * Extended by: Zach Brown <zab@redhat.com>
13 * Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
14 * Ollie Lho <ollie@sis.com.tw> SiS 7018 Audio Core Support
15 * Ching Ling Lee <cling-li@ali.com.tw> ALi 5451 Audio Core Support
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 * v0.14.5 May 23 2000 Ollie Lho
34 * Misc bug fix from the Net
35 * v0.14.4 May 20 2000 Aaron Holtzman
36 * Fix kfree'd memory access in release
37 * Fix race in open while looking for a free virtual channel slot
38 * remove open_wait wq (which appears to be unused)
39 * v0.14.3 May 10 2000 Ollie Lho
40 * fixed a small bug in trident_update_ptr, xmms 1.0.1 no longer uses 100% CPU
41 * v0.14.2 Mar 29 2000 Ching Ling Lee
42 * Add clear to silence advance in trident_update_ptr
43 * fix invalid data of the end of the sound
44 * v0.14.1 Mar 24 2000 Ching Ling Lee
45 * ALi 5451 support added, playback and recording O.K.
46 * ALi 5451 originally developed and structured based on sonicvibes, and
47 * suggested to merge into this file by Alan Cox.
48 * v0.14 Mar 15 2000 Ollie Lho
49 * 5.1 channel output support with channel binding. What's the Matrix ?
50 * v0.13.1 Mar 10 2000 Ollie Lho
51 * few minor bugs on dual codec support, needs more testing
52 * v0.13 Mar 03 2000 Ollie Lho
53 * new pci_* for 2.4 kernel, back ported to 2.2
54 * v0.12 Feb 23 2000 Ollie Lho
55 * Preliminary Recording support
56 * v0.11.2 Feb 19 2000 Ollie Lho
57 * removed incomplete full-dulplex support
58 * v0.11.1 Jan 28 2000 Ollie Lho
59 * small bug in setting sample rate for 4d-nx (reported by Aaron)
60 * v0.11 Jan 27 2000 Ollie Lho
61 * DMA bug, scheduler latency, second try
62 * v0.10 Jan 24 2000 Ollie Lho
63 * DMA bug fixed, found kernel scheduling problem
64 * v0.09 Jan 20 2000 Ollie Lho
65 * Clean up of channel register access routine (prepare for channel binding)
66 * v0.08 Jan 14 2000 Ollie Lho
67 * Isolation of AC97 codec code
68 * v0.07 Jan 13 2000 Ollie Lho
69 * Get rid of ugly old low level access routines (e.g. CHRegs.lp****)
70 * v0.06 Jan 11 2000 Ollie Lho
71 * Preliminary support for dual (more ?) AC97 codecs
72 * v0.05 Jan 08 2000 Luca Montecchiani <m.luca@iname.com>
73 * adapt to 2.3.x new __setup/__init call
74 * v0.04 Dec 31 1999 Ollie Lho
75 * Multiple Open, using Middle Loop Interrupt to smooth playback
76 * v0.03 Dec 24 1999 Ollie Lho
77 * mem leak in prog_dmabuf and dealloc_dmabuf removed
78 * v0.02 Dec 15 1999 Ollie Lho
79 * SiS 7018 support added, playback O.K.
80 * v0.01 Alan Cox et. al.
81 * Initial Release in kernel 2.3.30, does not work
84 * Clean up of low level channel register access code. (done)
85 * Fix the bug on dma buffer management in update_ptr, read/write, drain_dac (done)
86 * Dual AC97 codecs support (done)
87 * Recording support (done)
89 * "Channel Binding" ioctl extension (done)
90 * new pci device driver interface for 2.4 kernel (done)
93 #include <linux/config.h>
94 #include <linux/module.h>
95 #include <linux/version.h>
96 #include <linux/string.h>
97 #include <linux/ctype.h>
98 #include <linux/ioport.h>
99 #include <linux/sched.h>
100 #include <linux/delay.h>
101 #include <linux/sound.h>
102 #include <linux/malloc.h>
103 #include <linux/soundcard.h>
104 #include <linux/pci.h>
107 #include <linux/init.h>
108 #include <linux/poll.h>
109 #include <linux/spinlock.h>
110 #include <linux/smp_lock.h>
111 #include <linux/ac97_codec.h>
112 #include <linux/wrapper.h>
113 #include <asm/uaccess.h>
114 #include <asm/hardirq.h>
115 #include <linux/bitops.h>
119 #define DRIVER_VERSION "0.14.5"
121 /* magic numbers to protect our data structures */
122 #define TRIDENT_CARD_MAGIC 0x5072696E /* "Prin" */
123 #define TRIDENT_STATE_MAGIC 0x63657373 /* "cess" */
125 #define TRIDENT_DMA_MASK 0x3fffffff /* DMA buffer mask for pci_alloc_consist */
129 /* maxinum nuber of AC97 codecs connected, AC97 2.0 defined 4, but 7018 and 4D-NX only
130 have 2 SDATA_IN lines (currently) */
133 /* minor number of /dev/swmodem (temporary, experimental) */
134 #define SND_DEV_SWMODEM 7
136 static const unsigned sample_size
[] = { 1, 2, 2, 4 };
137 static const unsigned sample_shift
[] = { 0, 1, 1, 2 };
139 static const char invalid_magic
[] = KERN_CRIT
"trident: invalid magic value in %s\n";
148 static char * card_names
[] = {
151 "SiS 7018 PCI Audio",
152 "ALi Audio Accelerator"
155 static struct pci_device_id trident_pci_tbl
[] __devinitdata
= {
156 {PCI_VENDOR_ID_TRIDENT
, PCI_DEVICE_ID_TRIDENT_4DWAVE_DX
,
157 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, TRIDENT_4D_DX
},
158 {PCI_VENDOR_ID_TRIDENT
, PCI_DEVICE_ID_TRIDENT_4DWAVE_NX
,
159 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, TRIDENT_4D_NX
},
160 {PCI_VENDOR_ID_SI
, PCI_DEVICE_ID_SI_7018
,
161 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, SIS_7018
},
162 {PCI_VENDOR_ID_ALI
, PCI_DEVICE_ID_ALI_5451
,
163 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, ALI_5451
},
167 MODULE_DEVICE_TABLE (pci
, trident_pci_tbl
);
169 /* "software" or virtual channel, an instance of opened /dev/dsp */
170 struct trident_state
{
172 struct trident_card
*card
; /* Card info */
177 /* virtual channel number */
181 /* wave sample stuff */
183 unsigned char fmt
, enable
;
185 /* hardware channel */
186 struct trident_channel
*channel
;
188 /* OSS buffer management stuff */
190 dma_addr_t dma_handle
;
195 /* our buffer acts like a circular ring */
196 unsigned hwptr
; /* where dma last started, updated by update_ptr */
197 unsigned swptr
; /* where driver last clear/filled, updated by read/write */
198 int count
; /* bytes to be comsumed or been generated by dma machine */
199 unsigned total_bytes
; /* total bytes dmaed by hardware */
201 unsigned error
; /* number of over/underruns */
202 wait_queue_head_t wait
; /* put process on wait queue when no more space in buffer */
204 /* redundant, but makes calculations easier */
207 unsigned fragsamples
;
212 unsigned endcleared
:1;
213 unsigned update_flag
;
214 unsigned ossfragshift
;
216 unsigned subdivision
;
220 /* hardware channels */
221 struct trident_channel
{
222 int num
; /* channel number */
223 u32 lba
; /* Loop Begine Address, where dma buffer starts */
224 u32 eso
; /* End Sample Offset, wehre dma buffer ends (in the unit of samples) */
225 u32 delta
; /* delta value, sample rate / 48k for playback, 48k/sample rate for recording */
226 u16 attribute
; /* control where PCM data go and come */
228 u32 control
; /* signed/unsigned, 8/16 bits, mono/stereo */
231 struct trident_pcm_bank_address
{
237 static struct trident_pcm_bank_address bank_a_addrs
=
244 static struct trident_pcm_bank_address bank_b_addrs
=
251 struct trident_pcm_bank
{
252 /* register addresses to control bank operations */
253 struct trident_pcm_bank_address
*addresses
;
254 /* each bank has 32 channels */
255 u32 bitmap
; /* channel allocation bitmap */
256 struct trident_channel channels
[32];
259 struct trident_card
{
262 /* We keep trident cards in a linked list */
263 struct trident_card
*next
;
265 /* single open lock mechanism, only used for recording */
266 struct semaphore open_sem
;
268 /* The trident has a certain amount of cross channel interaction
269 so we use a single per card lock */
272 /* PCI device stuff */
273 struct pci_dev
* pci_dev
;
277 /* soundcore stuff */
280 /* structures for abstraction of hardware facilities, codecs, banks and channels*/
281 struct ac97_codec
*ac97_codec
[NR_AC97
];
282 struct trident_pcm_bank banks
[NR_BANKS
];
283 struct trident_state
*states
[NR_HW_CH
];
285 /* hardware resources */
286 unsigned long iobase
;
289 /* Function support */
290 struct trident_channel
*(*alloc_pcm_channel
)(struct trident_card
*);
291 struct trident_channel
*(*alloc_rec_pcm_channel
)(struct trident_card
*);
292 void (*free_pcm_channel
)(struct trident_card
*, unsigned int chan
);
293 void (*address_interrupt
)(struct trident_card
*);
296 /* table to map from CHANNELMASK to channel attribute for SiS 7018 */
297 static u16 mask2attr
[] =
299 PCM_LR
, PCM_LR
, SURR_LR
, CENTER_LFE
,
300 HSET
, MIC
, MODEM_LINE1
, MODEM_LINE2
,
303 /* table to map from channel attribute to CHANNELMASK for SiS 7018 */
304 static int attr2mask
[] = {
305 DSP_BIND_MODEM1
, DSP_BIND_MODEM2
, DSP_BIND_FRONT
, DSP_BIND_HANDSET
,
306 DSP_BIND_I2S
, DSP_BIND_CENTER_LFE
, DSP_BIND_SURR
, DSP_BIND_SPDIF
309 static struct trident_card
*devs
= NULL
;
311 static void ali_ac97_set(struct ac97_codec
*codec
, u8 reg
, u16 val
);
312 static u16
ali_ac97_get(struct ac97_codec
*codec
, u8 reg
);
314 static void trident_ac97_set(struct ac97_codec
*codec
, u8 reg
, u16 val
);
315 static u16
trident_ac97_get(struct ac97_codec
*codec
, u8 reg
);
317 static int trident_open_mixdev(struct inode
*inode
, struct file
*file
);
318 static int trident_ioctl_mixdev(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
320 static loff_t
trident_llseek(struct file
*file
, loff_t offset
, int origin
);
322 static int trident_enable_loop_interrupts(struct trident_card
* card
)
326 global_control
= inl(TRID_REG(card
, T4D_LFO_GC_CIR
));
328 switch (card
->pci_id
)
330 case PCI_DEVICE_ID_SI_7018
:
331 global_control
|= (ENDLP_IE
| MIDLP_IE
| BANK_B_EN
);
333 case PCI_DEVICE_ID_ALI_5451
:
334 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX
:
335 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX
:
336 global_control
|= (ENDLP_IE
| MIDLP_IE
);
342 outl(global_control
, TRID_REG(card
, T4D_LFO_GC_CIR
));
345 printk("trident: Enable Loop Interrupts, globctl = 0x%08X\n",
351 static int trident_disable_loop_interrupts(struct trident_card
* card
)
355 global_control
= inl(TRID_REG(card
, T4D_LFO_GC_CIR
));
356 global_control
&= ~(ENDLP_IE
| MIDLP_IE
);
357 outl(global_control
, TRID_REG(card
, T4D_LFO_GC_CIR
));
360 printk("trident: Disabled Loop Interrupts, globctl = 0x%08X\n",
366 static void trident_enable_voice_irq(struct trident_card
* card
, unsigned int channel
)
368 unsigned int mask
= 1 << (channel
& 0x1f);
369 struct trident_pcm_bank
*bank
= &card
->banks
[channel
>> 5];
370 u32 reg
, addr
= bank
->addresses
->aint_en
;
372 reg
= inl(TRID_REG(card
, addr
));
374 outl(reg
, TRID_REG(card
, addr
));
377 reg
= inl(TRID_REG(card
, T4D_AINTEN_B
));
378 printk("trident: enabled IRQ on channel %d, AINTEN_B = 0x%08x\n",
383 static void trident_disable_voice_irq(struct trident_card
* card
, unsigned int channel
)
385 unsigned int mask
= 1 << (channel
& 0x1f);
386 struct trident_pcm_bank
*bank
= &card
->banks
[channel
>> 5];
387 u32 reg
, addr
= bank
->addresses
->aint_en
;
389 reg
= inl(TRID_REG(card
, addr
));
391 outl(reg
, TRID_REG(card
, addr
));
393 /* Ack the channel in case the interrupt was set before we disable it. */
394 outl(mask
, TRID_REG(card
, bank
->addresses
->aint
));
397 reg
= inl(TRID_REG(card
, T4D_AINTEN_B
));
398 printk("trident: disabled IRQ on channel %d, AINTEN_B = 0x%08x\n",
403 static void trident_start_voice(struct trident_card
* card
, unsigned int channel
)
405 unsigned int mask
= 1 << (channel
& 0x1f);
406 struct trident_pcm_bank
*bank
= &card
->banks
[channel
>> 5];
407 u32 addr
= bank
->addresses
->start
;
413 outl(mask
, TRID_REG(card
, addr
));
416 reg
= inl(TRID_REG(card
, T4D_START_B
));
417 printk("trident: start voice on channel %d, START_B = 0x%08x\n",
422 static void trident_stop_voice(struct trident_card
* card
, unsigned int channel
)
424 unsigned int mask
= 1 << (channel
& 0x1f);
425 struct trident_pcm_bank
*bank
= &card
->banks
[channel
>> 5];
426 u32 addr
= bank
->addresses
->stop
;
432 outl(mask
, TRID_REG(card
, addr
));
435 reg
= inl(TRID_REG(card
, T4D_STOP_B
));
436 printk("trident: stop voice on channel %d, STOP_B = 0x%08x\n",
441 static u32
trident_get_interrupt_mask (struct trident_card
* card
, unsigned int channel
)
443 struct trident_pcm_bank
*bank
= &card
->banks
[channel
];
444 u32 addr
= bank
->addresses
->aint
;
445 return inl(TRID_REG(card
, addr
));
448 static int trident_check_channel_interrupt(struct trident_card
* card
, unsigned int channel
)
450 unsigned int mask
= 1 << (channel
& 0x1f);
451 u32 reg
= trident_get_interrupt_mask (card
, channel
>> 5);
455 printk("trident: channel %d has interrupt, AINT_B = 0x%08x\n",
458 return (reg
& mask
) ? TRUE
: FALSE
;
461 static void trident_ack_channel_interrupt(struct trident_card
* card
, unsigned int channel
)
463 unsigned int mask
= 1 << (channel
& 0x1f);
464 struct trident_pcm_bank
*bank
= &card
->banks
[channel
>> 5];
465 u32 reg
, addr
= bank
->addresses
->aint
;
467 reg
= inl(TRID_REG(card
, addr
));
469 outl(reg
, TRID_REG(card
, addr
));
472 reg
= inl(TRID_REG(card
, T4D_AINT_B
));
473 printk("trident: Ack channel %d interrupt, AINT_B = 0x%08x\n",
478 static struct trident_channel
* trident_alloc_pcm_channel(struct trident_card
*card
)
480 struct trident_pcm_bank
*bank
;
483 bank
= &card
->banks
[BANK_B
];
485 for (idx
= 31; idx
>= 0; idx
--) {
486 if (!(bank
->bitmap
& (1 << idx
))) {
487 struct trident_channel
*channel
= &bank
->channels
[idx
];
488 bank
->bitmap
|= 1 << idx
;
489 channel
->num
= idx
+ 32;
494 /* no more free channels avaliable */
495 printk(KERN_ERR
"trident: no more channels available on Bank B.\n");
499 static struct trident_channel
*ali_alloc_pcm_channel(struct trident_card
*card
)
501 struct trident_pcm_bank
*bank
;
504 bank
= &card
->banks
[BANK_A
];
506 for (idx
= ALI_PCM_OUT_CHANNEL_FIRST
; idx
<= ALI_PCM_OUT_CHANNEL_LAST
; idx
++) {
507 if (!(bank
->bitmap
& (1 << idx
))) {
508 struct trident_channel
*channel
= &bank
->channels
[idx
];
509 bank
->bitmap
|= 1 << idx
;
515 /* no more free channels avaliable */
516 printk(KERN_ERR
"trident: no more channels available on Bank B.\n");
520 static struct trident_channel
*ali_alloc_rec_pcm_channel(struct trident_card
*card
)
522 struct trident_pcm_bank
*bank
;
523 int idx
= ALI_PCM_IN_CHANNEL
;
525 bank
= &card
->banks
[BANK_A
];
527 if (!(bank
->bitmap
& (1 << idx
))) {
528 struct trident_channel
*channel
= &bank
->channels
[idx
];
529 bank
->bitmap
|= 1 << idx
;
537 static void trident_free_pcm_channel(struct trident_card
*card
, unsigned int channel
)
541 if (channel
< 31 || channel
> 63)
545 channel
= channel
& 0x1f;
547 card
->banks
[bank
].bitmap
&= ~(1 << (channel
));
550 static void ali_free_pcm_channel(struct trident_card
*card
, unsigned int channel
)
558 channel
= channel
& 0x1f;
560 card
->banks
[bank
].bitmap
&= ~(1 << (channel
));
564 /* called with spin lock held */
566 static int trident_load_channel_registers(struct trident_card
*card
, u32
*data
, unsigned int channel
)
573 /* select hardware channel to write */
574 outb(channel
, TRID_REG(card
, T4D_LFO_GC_CIR
));
576 /* Output the channel registers, but don't write register
577 three to an ALI chip. */
578 for (i
= 0; i
< CHANNEL_REGS
; i
++) {
579 if (i
== 3 && card
->pci_id
== PCI_DEVICE_ID_ALI_5451
)
581 outl(data
[i
], TRID_REG(card
, CHANNEL_START
+ 4*i
));
586 /* called with spin lock held */
587 static int trident_write_voice_regs(struct trident_state
*state
)
589 unsigned int data
[CHANNEL_REGS
+ 1];
590 struct trident_channel
*channel
;
592 channel
= state
->dmabuf
.channel
;
594 data
[1] = channel
->lba
;
595 data
[4] = channel
->control
;
597 switch (state
->card
->pci_id
)
599 case PCI_DEVICE_ID_ALI_5451
:
600 data
[0] = 0; /* Current Sample Offset */
601 data
[2] = (channel
->eso
<< 16) | (channel
->delta
& 0xffff);
604 case PCI_DEVICE_ID_SI_7018
:
605 data
[0] = 0; /* Current Sample Offset */
606 data
[2] = (channel
->eso
<< 16) | (channel
->delta
& 0xffff);
607 data
[3] = (channel
->attribute
<< 16) | (channel
->fm_vol
& 0xffff);
609 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX
:
610 data
[0] = 0; /* Current Sample Offset */
611 data
[2] = (channel
->eso
<< 16) | (channel
->delta
& 0xffff);
612 data
[3] = channel
->fm_vol
& 0xffff;
614 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX
:
615 data
[0] = (channel
->delta
<< 24);
616 data
[2] = ((channel
->delta
<< 16) & 0xff000000) | (channel
->eso
& 0x00ffffff);
617 data
[3] = channel
->fm_vol
& 0xffff;
623 return trident_load_channel_registers(state
->card
, data
, channel
->num
);
626 static int compute_rate_play(u32 rate
)
629 /* We special case 44100 and 8000 since rounding with the equation
630 does not give us an accurate enough value. For 11025 and 22050
631 the equation gives us the best answer. All other frequencies will
632 also use the equation. JDW */
635 else if (rate
== 8000)
637 else if (rate
== 48000)
640 delta
= (((rate
<< 12) + rate
) / 48000) & 0x0000ffff;
644 static int compute_rate_rec(u32 rate
)
650 else if (rate
== 8000)
652 else if (rate
== 48000)
655 delta
= ((48000 << 12) / rate
) & 0x0000ffff;
659 /* set playback sample rate */
660 static unsigned int trident_set_dac_rate(struct trident_state
* state
, unsigned int rate
)
662 struct dmabuf
*dmabuf
= &state
->dmabuf
;
670 dmabuf
->channel
->delta
= compute_rate_play(rate
);
672 trident_write_voice_regs(state
);
675 printk("trident: called trident_set_dac_rate : rate = %d\n", rate
);
681 /* set recording sample rate */
682 static unsigned int trident_set_adc_rate(struct trident_state
* state
, unsigned int rate
)
684 struct dmabuf
*dmabuf
= &state
->dmabuf
;
692 dmabuf
->channel
->delta
= compute_rate_rec(rate
);
694 trident_write_voice_regs(state
);
697 printk("trident: called trident_set_adc_rate : rate = %d\n", rate
);
702 /* prepare channel attributes for playback */
703 static void trident_play_setup(struct trident_state
*state
)
705 struct dmabuf
*dmabuf
= &state
->dmabuf
;
706 struct trident_channel
*channel
= dmabuf
->channel
;
708 channel
->lba
= virt_to_bus(dmabuf
->rawbuf
);
709 channel
->delta
= compute_rate_play(dmabuf
->rate
);
711 channel
->eso
= dmabuf
->dmasize
>> sample_shift
[dmabuf
->fmt
];
714 if (state
->card
->pci_id
!= PCI_DEVICE_ID_SI_7018
) {
715 channel
->attribute
= 0;
718 channel
->fm_vol
= 0x0;
720 channel
->control
= CHANNEL_LOOP
;
721 if (dmabuf
->fmt
& TRIDENT_FMT_16BIT
) {
723 channel
->control
|= CHANNEL_16BITS
;
725 channel
->control
|= CHANNEL_SIGNED
;
727 if (dmabuf
->fmt
& TRIDENT_FMT_STEREO
)
729 channel
->control
|= CHANNEL_STEREO
;
731 printk("trident: trident_play_setup, LBA = 0x%08x, "
732 "Delat = 0x%08x, ESO = 0x%08x, Control = 0x%08x\n",
733 channel
->lba
, channel
->delta
, channel
->eso
, channel
->control
);
735 trident_write_voice_regs(state
);
738 /* prepare channel attributes for recording */
739 static void trident_rec_setup(struct trident_state
*state
)
742 struct trident_card
*card
= state
->card
;
743 struct dmabuf
*dmabuf
= &state
->dmabuf
;
744 struct trident_channel
*channel
= dmabuf
->channel
;
746 /* Enable AC-97 ADC (capture) */
747 switch (card
->pci_id
)
749 case PCI_DEVICE_ID_ALI_5451
:
750 case PCI_DEVICE_ID_SI_7018
:
751 /* for 7018, the ac97 is always in playback/record (duplex) mode */
753 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX
:
754 w
= inb(TRID_REG(card
, DX_ACR2_AC97_COM_STAT
));
755 outb(w
| 0x48, TRID_REG(card
, DX_ACR2_AC97_COM_STAT
));
756 /* enable and set record channel */
757 outb(0x80 | channel
->num
, TRID_REG(card
, T4D_REC_CH
));
759 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX
:
760 w
= inw(TRID_REG(card
, T4D_MISCINT
));
761 outw(w
| 0x1000, TRID_REG(card
, T4D_MISCINT
));
762 /* enable and set record channel */
763 outb(0x80 | channel
->num
, TRID_REG(card
, T4D_REC_CH
));
769 channel
->lba
= virt_to_bus(dmabuf
->rawbuf
);
770 channel
->delta
= compute_rate_rec(dmabuf
->rate
);
772 channel
->eso
= dmabuf
->dmasize
>> sample_shift
[dmabuf
->fmt
];
775 if (state
->card
->pci_id
!= PCI_DEVICE_ID_SI_7018
) {
776 channel
->attribute
= 0;
779 channel
->fm_vol
= 0x0;
781 channel
->control
= CHANNEL_LOOP
;
782 if (dmabuf
->fmt
& TRIDENT_FMT_16BIT
) {
784 channel
->control
|= CHANNEL_16BITS
;
786 channel
->control
|= CHANNEL_SIGNED
;
788 if (dmabuf
->fmt
& TRIDENT_FMT_STEREO
)
790 channel
->control
|= CHANNEL_STEREO
;
792 printk("trident: trident_rec_setup, LBA = 0x%08x, "
793 "Delat = 0x%08x, ESO = 0x%08x, Control = 0x%08x\n",
794 channel
->lba
, channel
->delta
, channel
->eso
, channel
->control
);
796 trident_write_voice_regs(state
);
799 /* get current playback/recording dma buffer pointer (byte offset from LBA),
800 called with spinlock held! */
801 extern __inline__
unsigned trident_get_dma_addr(struct trident_state
*state
)
803 struct dmabuf
*dmabuf
= &state
->dmabuf
;
809 outb(dmabuf
->channel
->num
, TRID_REG(state
->card
, T4D_LFO_GC_CIR
));
811 switch (state
->card
->pci_id
)
813 case PCI_DEVICE_ID_ALI_5451
:
814 case PCI_DEVICE_ID_SI_7018
:
815 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX
:
816 /* 16 bits ESO, CSO for 7018 and DX */
817 cso
= inw(TRID_REG(state
->card
, CH_DX_CSO_ALPHA_FMS
+ 2));
819 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX
:
820 /* 24 bits ESO, CSO for NX */
821 cso
= inl(TRID_REG(state
->card
, CH_NX_DELTA_CSO
)) & 0x00ffffff;
828 printk("trident: trident_get_dma_addr: chip reported channel: %d, "
830 dmabuf
->channel
->num
, cso
);
832 /* ESO and CSO are in units of Samples, convert to byte offset */
833 cso
<<= sample_shift
[dmabuf
->fmt
];
835 return (cso
% dmabuf
->dmasize
);
838 /* Stop recording (lock held) */
839 extern __inline__
void __stop_adc(struct trident_state
*state
)
841 struct dmabuf
*dmabuf
= &state
->dmabuf
;
842 unsigned int chan_num
= dmabuf
->channel
->num
;
843 struct trident_card
*card
= state
->card
;
845 dmabuf
->enable
&= ~ADC_RUNNING
;
846 trident_stop_voice(card
, chan_num
);
847 trident_disable_voice_irq(card
, chan_num
);
850 static void stop_adc(struct trident_state
*state
)
852 struct trident_card
*card
= state
->card
;
855 spin_lock_irqsave(&card
->lock
, flags
);
857 spin_unlock_irqrestore(&card
->lock
, flags
);
860 static void start_adc(struct trident_state
*state
)
862 struct dmabuf
*dmabuf
= &state
->dmabuf
;
863 unsigned int chan_num
= dmabuf
->channel
->num
;
864 struct trident_card
*card
= state
->card
;
867 spin_lock_irqsave(&card
->lock
, flags
);
868 if ((dmabuf
->mapped
|| dmabuf
->count
< (signed)dmabuf
->dmasize
) && dmabuf
->ready
) {
869 dmabuf
->enable
|= ADC_RUNNING
;
870 trident_enable_voice_irq(card
, chan_num
);
871 trident_start_voice(card
, chan_num
);
873 spin_unlock_irqrestore(&card
->lock
, flags
);
876 /* stop playback (lock held) */
877 extern __inline__
void __stop_dac(struct trident_state
*state
)
879 struct dmabuf
*dmabuf
= &state
->dmabuf
;
880 unsigned int chan_num
= dmabuf
->channel
->num
;
881 struct trident_card
*card
= state
->card
;
883 dmabuf
->enable
&= ~DAC_RUNNING
;
884 trident_stop_voice(card
, chan_num
);
885 trident_disable_voice_irq(card
, chan_num
);
888 static void stop_dac(struct trident_state
*state
)
890 struct trident_card
*card
= state
->card
;
893 spin_lock_irqsave(&card
->lock
, flags
);
895 spin_unlock_irqrestore(&card
->lock
, flags
);
898 static void start_dac(struct trident_state
*state
)
900 struct dmabuf
*dmabuf
= &state
->dmabuf
;
901 unsigned int chan_num
= dmabuf
->channel
->num
;
902 struct trident_card
*card
= state
->card
;
905 spin_lock_irqsave(&card
->lock
, flags
);
906 if ((dmabuf
->mapped
|| dmabuf
->count
> 0) && dmabuf
->ready
) {
907 dmabuf
->enable
|= DAC_RUNNING
;
908 trident_enable_voice_irq(card
, chan_num
);
909 trident_start_voice(card
, chan_num
);
911 spin_unlock_irqrestore(&card
->lock
, flags
);
914 #define DMABUF_DEFAULTORDER (15-PAGE_SHIFT)
915 #define DMABUF_MINORDER 1
917 /* allocate DMA buffer, playback and recording buffer should be allocated seperately */
918 static int alloc_dmabuf(struct trident_state
*state
)
920 struct dmabuf
*dmabuf
= &state
->dmabuf
;
923 struct page
*page
, *pend
;
925 /* alloc as big a chunk as we can, FIXME: is this necessary ?? */
926 for (order
= DMABUF_DEFAULTORDER
; order
>= DMABUF_MINORDER
; order
--)
927 if ((rawbuf
= pci_alloc_consistent(state
->card
->pci_dev
,
929 &dmabuf
->dma_handle
)))
935 printk("trident: allocated %ld (order = %d) bytes at %p\n",
936 PAGE_SIZE
<< order
, order
, rawbuf
);
939 dmabuf
->ready
= dmabuf
->mapped
= 0;
940 dmabuf
->rawbuf
= rawbuf
;
941 dmabuf
->buforder
= order
;
943 /* now mark the pages as reserved; otherwise remap_page_range doesn't do what we want */
944 pend
= virt_to_page(rawbuf
+ (PAGE_SIZE
<< order
) - 1);
945 for (page
= virt_to_page(rawbuf
); page
<= pend
; page
++)
946 mem_map_reserve(page
);
951 /* free DMA buffer */
952 static void dealloc_dmabuf(struct trident_state
*state
)
954 struct dmabuf
*dmabuf
= &state
->dmabuf
;
955 struct page
*page
, *pend
;
957 if (dmabuf
->rawbuf
) {
958 /* undo marking the pages as reserved */
959 pend
= virt_to_page(dmabuf
->rawbuf
+ (PAGE_SIZE
<< dmabuf
->buforder
) - 1);
960 for (page
= virt_to_page(dmabuf
->rawbuf
); page
<= pend
; page
++)
961 mem_map_unreserve(page
);
962 pci_free_consistent(state
->card
->pci_dev
, PAGE_SIZE
<< dmabuf
->buforder
,
963 dmabuf
->rawbuf
, dmabuf
->dma_handle
);
965 dmabuf
->rawbuf
= NULL
;
966 dmabuf
->mapped
= dmabuf
->ready
= 0;
969 static int prog_dmabuf(struct trident_state
*state
, unsigned rec
)
971 struct dmabuf
*dmabuf
= &state
->dmabuf
;
977 spin_lock_irqsave(&state
->card
->lock
, flags
);
978 dmabuf
->hwptr
= dmabuf
->swptr
= dmabuf
->total_bytes
= 0;
979 dmabuf
->count
= dmabuf
->error
= 0;
980 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
982 /* allocate DMA buffer if not allocated yet */
984 if ((ret
= alloc_dmabuf(state
)))
987 /* FIXME: figure out all this OSS fragment stuff */
988 bytepersec
= dmabuf
->rate
<< sample_shift
[dmabuf
->fmt
];
989 bufsize
= PAGE_SIZE
<< dmabuf
->buforder
;
990 if (dmabuf
->ossfragshift
) {
991 if ((1000 << dmabuf
->ossfragshift
) < bytepersec
)
992 dmabuf
->fragshift
= ld2(bytepersec
/1000);
994 dmabuf
->fragshift
= dmabuf
->ossfragshift
;
996 /* lets hand out reasonable big ass buffers by default */
997 dmabuf
->fragshift
= (dmabuf
->buforder
+ PAGE_SHIFT
-2);
999 dmabuf
->numfrag
= bufsize
>> dmabuf
->fragshift
;
1000 while (dmabuf
->numfrag
< 4 && dmabuf
->fragshift
> 3) {
1001 dmabuf
->fragshift
--;
1002 dmabuf
->numfrag
= bufsize
>> dmabuf
->fragshift
;
1004 dmabuf
->fragsize
= 1 << dmabuf
->fragshift
;
1005 if (dmabuf
->ossmaxfrags
>= 4 && dmabuf
->ossmaxfrags
< dmabuf
->numfrag
)
1006 dmabuf
->numfrag
= dmabuf
->ossmaxfrags
;
1007 dmabuf
->fragsamples
= dmabuf
->fragsize
>> sample_shift
[dmabuf
->fmt
];
1008 dmabuf
->dmasize
= dmabuf
->numfrag
<< dmabuf
->fragshift
;
1010 memset(dmabuf
->rawbuf
, (dmabuf
->fmt
& TRIDENT_FMT_16BIT
) ? 0 : 0x80,
1013 spin_lock_irqsave(&state
->card
->lock
, flags
);
1015 trident_rec_setup(state
);
1017 trident_play_setup(state
);
1019 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1021 /* set the ready flag for the dma buffer */
1025 printk("trident: prog_dmabuf, sample rate = %d, format = %d, numfrag = %d, "
1026 "fragsize = %d dmasize = %d\n",
1027 dmabuf
->rate
, dmabuf
->fmt
, dmabuf
->numfrag
,
1028 dmabuf
->fragsize
, dmabuf
->dmasize
);
1034 /* we are doing quantum mechanics here, the buffer can only be empty, half or full filled i.e.
1035 |------------|------------| or |xxxxxxxxxxxx|------------| or |xxxxxxxxxxxx|xxxxxxxxxxxx|
1036 but we almost always get this
1037 |xxxxxx------|------------| or |xxxxxxxxxxxx|xxxxx-------|
1038 so we have to clear the tail space to "silence"
1039 |xxxxxx000000|------------| or |xxxxxxxxxxxx|xxxxxx000000|
1041 static void trident_clear_tail(struct trident_state
*state
)
1043 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1045 unsigned char silence
= (dmabuf
->fmt
& TRIDENT_FMT_16BIT
) ? 0 : 0x80;
1047 unsigned long flags
;
1049 spin_lock_irqsave(&state
->card
->lock
, flags
);
1050 swptr
= dmabuf
->swptr
;
1051 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1053 if (swptr
== 0 || swptr
== dmabuf
->dmasize
/ 2 || swptr
== dmabuf
->dmasize
)
1056 if (swptr
< dmabuf
->dmasize
/2)
1057 len
= dmabuf
->dmasize
/2 - swptr
;
1059 len
= dmabuf
->dmasize
- swptr
;
1061 memset(dmabuf
->rawbuf
+ swptr
, silence
, len
);
1063 spin_lock_irqsave(&state
->card
->lock
, flags
);
1064 dmabuf
->swptr
+= len
;
1065 dmabuf
->count
+= len
;
1066 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1068 /* restart the dma machine in case it is halted */
1072 static int drain_dac(struct trident_state
*state
, int nonblock
)
1074 DECLARE_WAITQUEUE(wait
, current
);
1075 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1076 unsigned long flags
;
1080 if (dmabuf
->mapped
|| !dmabuf
->ready
)
1083 add_wait_queue(&dmabuf
->wait
, &wait
);
1085 /* It seems that we have to set the current state to TASK_INTERRUPTIBLE
1086 every time to make the process really go to sleep */
1087 current
->state
= TASK_INTERRUPTIBLE
;
1089 spin_lock_irqsave(&state
->card
->lock
, flags
);
1090 count
= dmabuf
->count
;
1091 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1096 if (signal_pending(current
))
1100 remove_wait_queue(&dmabuf
->wait
, &wait
);
1101 current
->state
= TASK_RUNNING
;
1105 /* No matter how much data left in the buffer, we have to wait untill
1106 CSO == ESO/2 or CSO == ESO when address engine interrupts */
1107 tmo
= (dmabuf
->dmasize
* HZ
) / dmabuf
->rate
;
1108 tmo
>>= sample_shift
[dmabuf
->fmt
];
1109 if (!schedule_timeout(tmo
? tmo
: 1) && tmo
){
1110 printk(KERN_ERR
"trident: drain_dac, dma timeout?\n");
1114 remove_wait_queue(&dmabuf
->wait
, &wait
);
1115 current
->state
= TASK_RUNNING
;
1116 if (signal_pending(current
))
1117 return -ERESTARTSYS
;
1122 /* update buffer manangement pointers, especially, dmabuf->count and dmabuf->hwptr */
1123 static void trident_update_ptr(struct trident_state
*state
)
1125 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1126 unsigned hwptr
, swptr
;
1129 unsigned char silence
;
1130 unsigned half_dmasize
;
1132 /* update hardware pointer */
1133 hwptr
= trident_get_dma_addr(state
);
1134 diff
= (dmabuf
->dmasize
+ hwptr
- dmabuf
->hwptr
) % dmabuf
->dmasize
;
1135 dmabuf
->hwptr
= hwptr
;
1136 dmabuf
->total_bytes
+= diff
;
1138 /* error handling and process wake up for ADC */
1139 if (dmabuf
->enable
== ADC_RUNNING
) {
1140 if (dmabuf
->mapped
) {
1141 dmabuf
->count
-= diff
;
1142 if (dmabuf
->count
>= (signed)dmabuf
->fragsize
)
1143 wake_up(&dmabuf
->wait
);
1145 dmabuf
->count
+= diff
;
1147 if (dmabuf
->count
< 0 || dmabuf
->count
> dmabuf
->dmasize
) {
1148 /* buffer underrun or buffer overrun, we have no way to recover
1149 it here, just stop the machine and let the process force hwptr
1150 and swptr to sync */
1154 else if (!dmabuf
->endcleared
) {
1155 swptr
= dmabuf
->swptr
;
1156 silence
= (dmabuf
->fmt
& TRIDENT_FMT_16BIT
? 0 : 0x80);
1157 if (dmabuf
->update_flag
& ALI_ADDRESS_INT_UPDATE
) {
1158 /* We must clear end data of 1/2 dmabuf if needed.
1159 According to 1/2 algorithm of Address Engine Interrupt,
1160 check the validation of the data of half dmasize. */
1161 half_dmasize
= dmabuf
->dmasize
/ 2;
1162 if ((diff
= hwptr
- half_dmasize
) < 0 )
1164 if ((dmabuf
->count
+ diff
) < half_dmasize
) {
1165 //there is invalid data in the end of half buffer
1166 if ((clear_cnt
= half_dmasize
- swptr
) < 0)
1167 clear_cnt
+= half_dmasize
;
1168 //clear the invalid data
1169 memset (dmabuf
->rawbuf
+ swptr
,
1170 silence
, clear_cnt
);
1172 dmabuf
->endcleared
= 1;
1174 } else if (dmabuf
->count
< (signed) dmabuf
->fragsize
) {
1175 clear_cnt
= dmabuf
->fragsize
;
1176 if ((swptr
+ clear_cnt
) > dmabuf
->dmasize
)
1177 clear_cnt
= dmabuf
->dmasize
- swptr
;
1178 memset (dmabuf
->rawbuf
+ swptr
, silence
, clear_cnt
);
1179 dmabuf
->endcleared
= 1;
1182 /* trident_update_ptr is called by interrupt handler or by process via
1183 ioctl/poll, we only wake up the waiting process when we have more
1184 than 1/2 buffer of data to process (always true for interrupt handler) */
1185 if (dmabuf
->count
> (signed)dmabuf
->dmasize
/2)
1186 wake_up(&dmabuf
->wait
);
1190 /* error handling and process wake up for DAC */
1191 if (dmabuf
->enable
== DAC_RUNNING
) {
1192 if (dmabuf
->mapped
) {
1193 dmabuf
->count
+= diff
;
1194 if (dmabuf
->count
>= (signed)dmabuf
->fragsize
)
1195 wake_up(&dmabuf
->wait
);
1197 dmabuf
->count
-= diff
;
1199 if (dmabuf
->count
< 0 || dmabuf
->count
> dmabuf
->dmasize
) {
1200 /* buffer underrun or buffer overrun, we have no way to recover
1201 it here, just stop the machine and let the process force hwptr
1202 and swptr to sync */
1206 /* trident_update_ptr is called by interrupt handler or by process via
1207 ioctl/poll, we only wake up the waiting process when we have more
1208 than 1/2 buffer free (always true for interrupt handler) */
1209 if (dmabuf
->count
< (signed)dmabuf
->dmasize
/2)
1210 wake_up(&dmabuf
->wait
);
1213 dmabuf
->update_flag
&= ~ALI_ADDRESS_INT_UPDATE
;
1216 static void trident_address_interrupt(struct trident_card
*card
)
1219 struct trident_state
*state
;
1221 /* Update the pointers for all channels we are running. */
1222 /* FIXME: should read interrupt status only once */
1223 for (i
= 0; i
< NR_HW_CH
; i
++) {
1224 if (trident_check_channel_interrupt(card
, 63 - i
)) {
1225 trident_ack_channel_interrupt(card
, 63 - i
);
1226 if ((state
= card
->states
[i
]) != NULL
) {
1227 trident_update_ptr(state
);
1229 printk("trident: spurious channel irq %d.\n",
1231 trident_stop_voice(card
, 63 - i
);
1232 trident_disable_voice_irq(card
, 63 - i
);
1238 static void ali_address_interrupt(struct trident_card
*card
)
1241 u32 mask
= trident_get_interrupt_mask (card
, BANK_A
);
1244 /* Sanity check to make sure that every state has a channel
1249 for (i
= 0; i
< NR_HW_CH
; i
++) {
1250 if (card
->banks
[BANK_A
].bitmap
& (1<<i
))
1253 if (card
->states
[i
]) {
1254 u32 bit
= 1 << card
->states
[i
]->dmabuf
.channel
->num
;
1256 printk (KERN_ERR
"trident: channel allocated to two states\n");
1263 printk (KERN_ERR
"trident: number of states != number of channels\n");
1266 for (i
= 0; mask
&& i
< NR_HW_CH
; i
++) {
1267 struct trident_state
*state
= card
->states
[i
];
1271 trident_ack_channel_interrupt(card
, state
->dmabuf
.channel
->num
);
1272 mask
&= ~ (1<<state
->dmabuf
.channel
->num
);
1273 state
->dmabuf
.update_flag
|= ALI_ADDRESS_INT_UPDATE
;
1274 trident_update_ptr(state
);
1278 for (i
= 0; i
< NR_HW_CH
; i
++)
1279 if (mask
& (1<<i
)) {
1280 printk("ali: spurious channel irq %d.\n", i
);
1281 trident_stop_voice(card
, i
);
1282 trident_disable_voice_irq(card
, i
);
1287 static void trident_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
1289 struct trident_card
*card
= (struct trident_card
*)dev_id
;
1292 spin_lock(&card
->lock
);
1293 event
= inl(TRID_REG(card
, T4D_MISCINT
));
1296 printk("trident: trident_interrupt called, MISCINT = 0x%08x\n", event
);
1299 if (event
& ADDRESS_IRQ
) {
1300 card
->address_interrupt(card
);
1303 /* manually clear interrupt status, bad hardware design, blame T^2 */
1304 outl((ST_TARGET_REACHED
| MIXER_OVERFLOW
| MIXER_UNDERFLOW
),
1305 TRID_REG(card
, T4D_MISCINT
));
1306 spin_unlock(&card
->lock
);
1309 static loff_t
trident_llseek(struct file
*file
, loff_t offset
, int origin
)
1314 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be copied to
1315 the user's buffer. it is filled by the dma machine and drained by this loop. */
1316 static ssize_t
trident_read(struct file
*file
, char *buffer
, size_t count
, loff_t
*ppos
)
1318 struct trident_state
*state
= (struct trident_state
*)file
->private_data
;
1319 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1321 unsigned long flags
;
1326 printk("trident: trident_read called, count = %d\n", count
);
1329 VALIDATE_STATE(state
);
1330 if (ppos
!= &file
->f_pos
)
1334 if (!dmabuf
->ready
&& (ret
= prog_dmabuf(state
, 1)))
1336 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
1340 if (state
->card
->pci_id
== PCI_DEVICE_ID_ALI_5451
)
1341 outl(inl(TRID_REG (state
->card
, ALI_GLOBAL_CONTROL
)) | ALI_PCM_IN_ENABLE
,
1342 TRID_REG (state
->card
, ALI_GLOBAL_CONTROL
));
1345 spin_lock_irqsave(&state
->card
->lock
, flags
);
1346 if (dmabuf
->count
> (signed) dmabuf
->dmasize
) {
1347 /* buffer overrun, we are recovering from sleep_on_timeout,
1348 resync hwptr and swptr, make process flush the buffer */
1349 dmabuf
->count
= dmabuf
->dmasize
;
1350 dmabuf
->swptr
= dmabuf
->hwptr
;
1352 swptr
= dmabuf
->swptr
;
1353 cnt
= dmabuf
->dmasize
- swptr
;
1354 if (dmabuf
->count
< cnt
)
1355 cnt
= dmabuf
->count
;
1356 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1362 /* buffer is empty, start the dma machine and wait for data to be
1365 if (file
->f_flags
& O_NONBLOCK
) {
1366 if (!ret
) ret
= -EAGAIN
;
1369 /* No matter how much space left in the buffer, we have to wait untill
1370 CSO == ESO/2 or CSO == ESO when address engine interrupts */
1371 tmo
= (dmabuf
->dmasize
* HZ
) / (dmabuf
->rate
* 2);
1372 tmo
>>= sample_shift
[dmabuf
->fmt
];
1373 /* There are two situations when sleep_on_timeout returns, one is when
1374 the interrupt is serviced correctly and the process is waked up by
1375 ISR ON TIME. Another is when timeout is expired, which means that
1376 either interrupt is NOT serviced correctly (pending interrupt) or it
1377 is TOO LATE for the process to be scheduled to run (scheduler latency)
1378 which results in a (potential) buffer overrun. And worse, there is
1379 NOTHING we can do to prevent it. */
1380 if (!interruptible_sleep_on_timeout(&dmabuf
->wait
, tmo
)) {
1382 printk(KERN_ERR
"trident: recording schedule timeout, "
1383 "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1384 dmabuf
->dmasize
, dmabuf
->fragsize
, dmabuf
->count
,
1385 dmabuf
->hwptr
, dmabuf
->swptr
);
1387 /* a buffer overrun, we delay the recovery untill next time the
1388 while loop begin and we REALLY have space to record */
1390 if (signal_pending(current
)) {
1391 ret
= ret
? ret
: -ERESTARTSYS
;
1397 if (copy_to_user(buffer
, dmabuf
->rawbuf
+ swptr
, cnt
)) {
1398 if (!ret
) ret
= -EFAULT
;
1402 swptr
= (swptr
+ cnt
) % dmabuf
->dmasize
;
1404 spin_lock_irqsave(&state
->card
->lock
, flags
);
1405 dmabuf
->swptr
= swptr
;
1406 dmabuf
->count
-= cnt
;
1407 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1417 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
1418 the soundcard. it is drained by the dma machine and filled by this loop. */
1419 static ssize_t
trident_write(struct file
*file
, const char *buffer
, size_t count
, loff_t
*ppos
)
1421 struct trident_state
*state
= (struct trident_state
*)file
->private_data
;
1422 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1424 unsigned long flags
;
1429 printk("trident: trident_write called, count = %d\n", count
);
1432 VALIDATE_STATE(state
);
1433 if (ppos
!= &file
->f_pos
)
1437 if (!dmabuf
->ready
&& (ret
= prog_dmabuf(state
, 0)))
1439 if (!access_ok(VERIFY_READ
, buffer
, count
))
1443 if (state
->card
->pci_id
== PCI_DEVICE_ID_ALI_5451
)
1444 if (dmabuf
->channel
->num
== ALI_PCM_IN_CHANNEL
)
1445 outl ( inl (TRID_REG (state
->card
, ALI_GLOBAL_CONTROL
)) &
1446 ALI_PCM_IN_DISABLE
, TRID_REG (state
->card
, ALI_GLOBAL_CONTROL
));
1449 spin_lock_irqsave(&state
->card
->lock
, flags
);
1450 if (dmabuf
->count
< 0) {
1451 /* buffer underrun, we are recovering from sleep_on_timeout,
1452 resync hwptr and swptr */
1454 dmabuf
->swptr
= dmabuf
->hwptr
;
1456 swptr
= dmabuf
->swptr
;
1457 cnt
= dmabuf
->dmasize
- swptr
;
1458 if (dmabuf
->count
+ cnt
> dmabuf
->dmasize
)
1459 cnt
= dmabuf
->dmasize
- dmabuf
->count
;
1460 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1466 /* buffer is full, start the dma machine and wait for data to be
1469 if (file
->f_flags
& O_NONBLOCK
) {
1470 if (!ret
) ret
= -EAGAIN
;
1473 /* No matter how much data left in the buffer, we have to wait untill
1474 CSO == ESO/2 or CSO == ESO when address engine interrupts */
1475 tmo
= (dmabuf
->dmasize
* HZ
) / (dmabuf
->rate
* 2);
1476 tmo
>>= sample_shift
[dmabuf
->fmt
];
1477 /* There are two situations when sleep_on_timeout returns, one is when
1478 the interrupt is serviced correctly and the process is waked up by
1479 ISR ON TIME. Another is when timeout is expired, which means that
1480 either interrupt is NOT serviced correctly (pending interrupt) or it
1481 is TOO LATE for the process to be scheduled to run (scheduler latency)
1482 which results in a (potential) buffer underrun. And worse, there is
1483 NOTHING we can do to prevent it. */
1484 if (!interruptible_sleep_on_timeout(&dmabuf
->wait
, tmo
)) {
1486 printk(KERN_ERR
"trident: playback schedule timeout, "
1487 "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1488 dmabuf
->dmasize
, dmabuf
->fragsize
, dmabuf
->count
,
1489 dmabuf
->hwptr
, dmabuf
->swptr
);
1491 /* a buffer underrun, we delay the recovery untill next time the
1492 while loop begin and we REALLY have data to play */
1494 if (signal_pending(current
)) {
1495 if (!ret
) ret
= -ERESTARTSYS
;
1500 if (copy_from_user(dmabuf
->rawbuf
+ swptr
, buffer
, cnt
)) {
1501 if (!ret
) ret
= -EFAULT
;
1505 swptr
= (swptr
+ cnt
) % dmabuf
->dmasize
;
1507 spin_lock_irqsave(&state
->card
->lock
, flags
);
1508 dmabuf
->swptr
= swptr
;
1509 dmabuf
->count
+= cnt
;
1510 dmabuf
->endcleared
= 0;
1511 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1521 /* No kernel lock - we have our own spinlock */
1522 static unsigned int trident_poll(struct file
*file
, struct poll_table_struct
*wait
)
1524 struct trident_state
*state
= (struct trident_state
*)file
->private_data
;
1525 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1526 unsigned long flags
;
1527 unsigned int mask
= 0;
1529 VALIDATE_STATE(state
);
1530 if (file
->f_mode
& FMODE_WRITE
)
1531 poll_wait(file
, &dmabuf
->wait
, wait
);
1532 if (file
->f_mode
& FMODE_READ
)
1533 poll_wait(file
, &dmabuf
->wait
, wait
);
1535 spin_lock_irqsave(&state
->card
->lock
, flags
);
1536 trident_update_ptr(state
);
1537 if (file
->f_mode
& FMODE_READ
) {
1538 if (dmabuf
->count
>= (signed)dmabuf
->fragsize
)
1539 mask
|= POLLIN
| POLLRDNORM
;
1541 if (file
->f_mode
& FMODE_WRITE
) {
1542 if (dmabuf
->mapped
) {
1543 if (dmabuf
->count
>= (signed)dmabuf
->fragsize
)
1544 mask
|= POLLOUT
| POLLWRNORM
;
1546 if ((signed)dmabuf
->dmasize
>= dmabuf
->count
+ (signed)dmabuf
->fragsize
)
1547 mask
|= POLLOUT
| POLLWRNORM
;
1550 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1555 static int trident_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1557 struct trident_state
*state
= (struct trident_state
*)file
->private_data
;
1558 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1562 VALIDATE_STATE(state
);
1564 if (vma
->vm_flags
& VM_WRITE
) {
1565 if ((ret
= prog_dmabuf(state
, 0)) != 0)
1567 } else if (vma
->vm_flags
& VM_READ
) {
1568 if ((ret
= prog_dmabuf(state
, 1)) != 0)
1574 if (vma
->vm_pgoff
!= 0)
1576 size
= vma
->vm_end
- vma
->vm_start
;
1577 if (size
> (PAGE_SIZE
<< dmabuf
->buforder
))
1580 if (remap_page_range(vma
->vm_start
, virt_to_phys(dmabuf
->rawbuf
),
1581 size
, vma
->vm_page_prot
))
1590 static int trident_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1592 struct trident_state
*state
= (struct trident_state
*)file
->private_data
;
1593 struct dmabuf
*dmabuf
= &state
->dmabuf
;
1594 unsigned long flags
;
1595 audio_buf_info abinfo
;
1597 int val
, mapped
, ret
;
1599 VALIDATE_STATE(state
);
1600 mapped
= ((file
->f_mode
& FMODE_WRITE
) && dmabuf
->mapped
) ||
1601 ((file
->f_mode
& FMODE_READ
) && dmabuf
->mapped
);
1603 printk("trident: trident_ioctl, command = %2d, arg = 0x%08x\n",
1604 _IOC_NR(cmd
), arg
? *(int *)arg
: 0);
1609 case OSS_GETVERSION
:
1610 return put_user(SOUND_VERSION
, (int *)arg
);
1612 case SNDCTL_DSP_RESET
:
1613 /* FIXME: spin_lock ? */
1614 if (file
->f_mode
& FMODE_WRITE
) {
1618 dmabuf
->swptr
= dmabuf
->hwptr
= 0;
1619 dmabuf
->count
= dmabuf
->total_bytes
= 0;
1621 if (file
->f_mode
& FMODE_READ
) {
1625 dmabuf
->swptr
= dmabuf
->hwptr
= 0;
1626 dmabuf
->count
= dmabuf
->total_bytes
= 0;
1630 case SNDCTL_DSP_SYNC
:
1631 if (file
->f_mode
& FMODE_WRITE
)
1632 return drain_dac(state
, file
->f_flags
& O_NONBLOCK
);
1635 case SNDCTL_DSP_SPEED
: /* set smaple rate */
1636 if (get_user(val
, (int *)arg
))
1639 if (file
->f_mode
& FMODE_WRITE
) {
1642 spin_lock_irqsave(&state
->card
->lock
, flags
);
1643 trident_set_dac_rate(state
, val
);
1644 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1646 if (file
->f_mode
& FMODE_READ
) {
1649 spin_lock_irqsave(&state
->card
->lock
, flags
);
1650 trident_set_adc_rate(state
, val
);
1651 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1654 return put_user(dmabuf
->rate
, (int *)arg
);
1656 case SNDCTL_DSP_STEREO
: /* set stereo or mono channel */
1657 if (get_user(val
, (int *)arg
))
1659 if (file
->f_mode
& FMODE_WRITE
) {
1663 dmabuf
->fmt
|= TRIDENT_FMT_STEREO
;
1665 dmabuf
->fmt
&= ~TRIDENT_FMT_STEREO
;
1667 if (file
->f_mode
& FMODE_READ
) {
1671 dmabuf
->fmt
|= TRIDENT_FMT_STEREO
;
1673 dmabuf
->fmt
&= ~TRIDENT_FMT_STEREO
;
1677 case SNDCTL_DSP_GETBLKSIZE
:
1678 if (file
->f_mode
& FMODE_WRITE
) {
1679 if ((val
= prog_dmabuf(state
, 0)))
1681 return put_user(dmabuf
->fragsize
, (int *)arg
);
1683 if (file
->f_mode
& FMODE_READ
) {
1684 if ((val
= prog_dmabuf(state
, 1)))
1686 return put_user(dmabuf
->fragsize
, (int *)arg
);
1689 case SNDCTL_DSP_GETFMTS
: /* Returns a mask of supported sample format*/
1690 return put_user(AFMT_S16_LE
|AFMT_U16_LE
|AFMT_S8
|AFMT_U8
, (int *)arg
);
1692 case SNDCTL_DSP_SETFMT
: /* Select sample format */
1693 if (get_user(val
, (int *)arg
))
1695 if (val
!= AFMT_QUERY
) {
1696 if (file
->f_mode
& FMODE_WRITE
) {
1699 if (val
== AFMT_S16_LE
)
1700 dmabuf
->fmt
|= TRIDENT_FMT_16BIT
;
1702 dmabuf
->fmt
&= ~TRIDENT_FMT_16BIT
;
1704 if (file
->f_mode
& FMODE_READ
) {
1707 if (val
== AFMT_S16_LE
)
1708 dmabuf
->fmt
|= TRIDENT_FMT_16BIT
;
1710 dmabuf
->fmt
&= ~TRIDENT_FMT_16BIT
;
1713 return put_user((dmabuf
->fmt
& TRIDENT_FMT_16BIT
) ?
1714 AFMT_S16_LE
: AFMT_U8
, (int *)arg
);
1716 case SNDCTL_DSP_CHANNELS
:
1717 if (get_user(val
, (int *)arg
))
1720 if (file
->f_mode
& FMODE_WRITE
) {
1724 dmabuf
->fmt
|= TRIDENT_FMT_STEREO
;
1726 dmabuf
->fmt
&= ~TRIDENT_FMT_STEREO
;
1728 if (file
->f_mode
& FMODE_READ
) {
1732 dmabuf
->fmt
|= TRIDENT_FMT_STEREO
;
1734 dmabuf
->fmt
&= ~TRIDENT_FMT_STEREO
;
1737 return put_user((dmabuf
->fmt
& TRIDENT_FMT_STEREO
) ? 2 : 1,
1740 case SNDCTL_DSP_POST
:
1741 /* FIXME: the same as RESET ?? */
1744 case SNDCTL_DSP_SUBDIVIDE
:
1745 if (dmabuf
->subdivision
)
1747 if (get_user(val
, (int *)arg
))
1749 if (val
!= 1 && val
!= 2 && val
!= 4)
1751 dmabuf
->subdivision
= val
;
1754 case SNDCTL_DSP_SETFRAGMENT
:
1755 if (get_user(val
, (int *)arg
))
1758 dmabuf
->ossfragshift
= val
& 0xffff;
1759 dmabuf
->ossmaxfrags
= (val
>> 16) & 0xffff;
1760 if (dmabuf
->ossfragshift
< 4)
1761 dmabuf
->ossfragshift
= 4;
1762 if (dmabuf
->ossfragshift
> 15)
1763 dmabuf
->ossfragshift
= 15;
1764 if (dmabuf
->ossmaxfrags
< 4)
1765 dmabuf
->ossmaxfrags
= 4;
1769 case SNDCTL_DSP_GETOSPACE
:
1770 if (!(file
->f_mode
& FMODE_WRITE
))
1772 if (!dmabuf
->enable
&& (val
= prog_dmabuf(state
, 0)) != 0)
1774 spin_lock_irqsave(&state
->card
->lock
, flags
);
1775 trident_update_ptr(state
);
1776 abinfo
.fragsize
= dmabuf
->fragsize
;
1777 abinfo
.bytes
= dmabuf
->dmasize
- dmabuf
->count
;
1778 abinfo
.fragstotal
= dmabuf
->numfrag
;
1779 abinfo
.fragments
= abinfo
.bytes
>> dmabuf
->fragshift
;
1780 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1781 return copy_to_user((void *)arg
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1783 case SNDCTL_DSP_GETISPACE
:
1784 if (!(file
->f_mode
& FMODE_READ
))
1786 if (!dmabuf
->enable
&& (val
= prog_dmabuf(state
, 1)) != 0)
1788 spin_lock_irqsave(&state
->card
->lock
, flags
);
1789 trident_update_ptr(state
);
1790 abinfo
.fragsize
= dmabuf
->fragsize
;
1791 abinfo
.bytes
= dmabuf
->count
;
1792 abinfo
.fragstotal
= dmabuf
->numfrag
;
1793 abinfo
.fragments
= abinfo
.bytes
>> dmabuf
->fragshift
;
1794 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1795 return copy_to_user((void *)arg
, &abinfo
, sizeof(abinfo
)) ? -EFAULT
: 0;
1797 case SNDCTL_DSP_NONBLOCK
:
1798 file
->f_flags
|= O_NONBLOCK
;
1801 case SNDCTL_DSP_GETCAPS
:
1802 return put_user(DSP_CAP_REALTIME
|DSP_CAP_TRIGGER
|DSP_CAP_MMAP
|DSP_CAP_BIND
,
1805 case SNDCTL_DSP_GETTRIGGER
:
1807 if (file
->f_mode
& FMODE_READ
&& dmabuf
->enable
)
1808 val
|= PCM_ENABLE_INPUT
;
1809 if (file
->f_mode
& FMODE_WRITE
&& dmabuf
->enable
)
1810 val
|= PCM_ENABLE_OUTPUT
;
1811 return put_user(val
, (int *)arg
);
1813 case SNDCTL_DSP_SETTRIGGER
:
1814 if (get_user(val
, (int *)arg
))
1816 if (file
->f_mode
& FMODE_READ
) {
1817 if (val
& PCM_ENABLE_INPUT
) {
1818 if (!dmabuf
->ready
&& (ret
= prog_dmabuf(state
, 1)))
1824 if (file
->f_mode
& FMODE_WRITE
) {
1825 if (val
& PCM_ENABLE_OUTPUT
) {
1826 if (!dmabuf
->ready
&& (ret
= prog_dmabuf(state
, 0)))
1834 case SNDCTL_DSP_GETIPTR
:
1835 if (!(file
->f_mode
& FMODE_READ
))
1837 spin_lock_irqsave(&state
->card
->lock
, flags
);
1838 trident_update_ptr(state
);
1839 cinfo
.bytes
= dmabuf
->total_bytes
;
1840 cinfo
.blocks
= dmabuf
->count
>> dmabuf
->fragshift
;
1841 cinfo
.ptr
= dmabuf
->hwptr
;
1843 dmabuf
->count
&= dmabuf
->fragsize
-1;
1844 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1845 return copy_to_user((void *)arg
, &cinfo
, sizeof(cinfo
));
1847 case SNDCTL_DSP_GETOPTR
:
1848 if (!(file
->f_mode
& FMODE_WRITE
))
1850 spin_lock_irqsave(&state
->card
->lock
, flags
);
1851 trident_update_ptr(state
);
1852 cinfo
.bytes
= dmabuf
->total_bytes
;
1853 cinfo
.blocks
= dmabuf
->count
>> dmabuf
->fragshift
;
1854 cinfo
.ptr
= dmabuf
->hwptr
;
1856 dmabuf
->count
&= dmabuf
->fragsize
-1;
1857 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1858 return copy_to_user((void *)arg
, &cinfo
, sizeof(cinfo
));
1860 case SNDCTL_DSP_SETDUPLEX
:
1863 case SNDCTL_DSP_GETODELAY
:
1864 if (!(file
->f_mode
& FMODE_WRITE
))
1866 spin_lock_irqsave(&state
->card
->lock
, flags
);
1867 trident_update_ptr(state
);
1868 val
= dmabuf
->count
;
1869 spin_unlock_irqrestore(&state
->card
->lock
, flags
);
1870 return put_user(val
, (int *)arg
);
1872 case SOUND_PCM_READ_RATE
:
1873 return put_user(dmabuf
->rate
, (int *)arg
);
1875 case SOUND_PCM_READ_CHANNELS
:
1876 return put_user((dmabuf
->fmt
& TRIDENT_FMT_STEREO
) ? 2 : 1,
1879 case SOUND_PCM_READ_BITS
:
1880 return put_user((dmabuf
->fmt
& TRIDENT_FMT_16BIT
) ?
1881 AFMT_S16_LE
: AFMT_U8
, (int *)arg
);
1883 case SNDCTL_DSP_GETCHANNELMASK
:
1884 return put_user(DSP_BIND_FRONT
|DSP_BIND_SURR
|DSP_BIND_CENTER_LFE
,
1887 case SNDCTL_DSP_BIND_CHANNEL
:
1888 if (state
->card
->pci_id
!= PCI_DEVICE_ID_SI_7018
)
1891 if (get_user(val
, (int *)arg
))
1893 if (val
== DSP_BIND_QUERY
) {
1894 val
= dmabuf
->channel
->attribute
| 0x3c00;
1895 val
= attr2mask
[val
>> 8];
1898 if (file
->f_mode
& FMODE_READ
)
1899 dmabuf
->channel
->attribute
= (CHANNEL_REC
|SRC_ENABLE
);
1900 if (file
->f_mode
& FMODE_WRITE
)
1901 dmabuf
->channel
->attribute
= (CHANNEL_SPC_PB
|SRC_ENABLE
);
1902 dmabuf
->channel
->attribute
|= mask2attr
[ffs(val
)];
1904 return put_user(val
, (int *)arg
);
1906 case SNDCTL_DSP_MAPINBUF
:
1907 case SNDCTL_DSP_MAPOUTBUF
:
1908 case SNDCTL_DSP_SETSYNCRO
:
1909 case SOUND_PCM_WRITE_FILTER
:
1910 case SOUND_PCM_READ_FILTER
:
1917 static int trident_open(struct inode
*inode
, struct file
*file
)
1920 int minor
= MINOR(inode
->i_rdev
);
1921 struct trident_card
*card
= devs
;
1922 struct trident_state
*state
= NULL
;
1923 struct dmabuf
*dmabuf
= NULL
;
1925 /* find an avaiable virtual channel (instance of /dev/dsp) */
1926 while (card
!= NULL
) {
1927 down(&card
->open_sem
);
1928 for (i
= 0; i
< NR_HW_CH
; i
++) {
1929 if (card
->states
[i
] == NULL
) {
1930 state
= card
->states
[i
] = (struct trident_state
*)
1931 kmalloc(sizeof(struct trident_state
), GFP_KERNEL
);
1932 if (state
== NULL
) {
1935 memset(state
, 0, sizeof(struct trident_state
));
1936 dmabuf
= &state
->dmabuf
;
1940 up(&card
->open_sem
);
1943 /* no more virtual channel avaiable */
1948 /* found a free virtual channel, allocate hardware channels */
1949 if(file
->f_mode
& FMODE_READ
)
1950 dmabuf
->channel
= card
->alloc_rec_pcm_channel(card
);
1952 dmabuf
->channel
= card
->alloc_pcm_channel(card
);
1954 if (dmabuf
->channel
== NULL
) {
1955 kfree (card
->states
[i
]);
1956 card
->states
[i
] = NULL
;
1960 /* initialize the virtual channel */
1963 state
->magic
= TRIDENT_STATE_MAGIC
;
1964 init_waitqueue_head(&dmabuf
->wait
);
1965 file
->private_data
= state
;
1967 /* set default sample format. According to OSS Programmer's Guide /dev/dsp
1968 should be default to unsigned 8-bits, mono, with sample rate 8kHz and
1969 /dev/dspW will accept 16-bits sample */
1970 if (file
->f_mode
& FMODE_WRITE
) {
1971 dmabuf
->fmt
&= ~TRIDENT_FMT_MASK
;
1972 if ((minor
& 0x0f) == SND_DEV_DSP16
)
1973 dmabuf
->fmt
|= TRIDENT_FMT_16BIT
;
1974 dmabuf
->ossfragshift
= 0;
1975 dmabuf
->ossmaxfrags
= 0;
1976 dmabuf
->subdivision
= 0;
1977 if (card
->pci_id
== PCI_DEVICE_ID_SI_7018
) {
1978 /* set default channel attribute to normal playback */
1979 dmabuf
->channel
->attribute
= CHANNEL_PB
;
1981 trident_set_dac_rate(state
, 8000);
1984 if (file
->f_mode
& FMODE_READ
) {
1985 if (card
->pci_id
== PCI_DEVICE_ID_ALI_5451
) {
1986 card
->states
[ALI_PCM_IN_CHANNEL
] = state
;
1987 card
->states
[i
] = NULL
;
1988 state
->virt
= ALI_PCM_IN_CHANNEL
;
1990 /* FIXME: Trident 4d can only record in signed 16-bits stereo, 48kHz sample,
1991 to be dealed with in trident_set_adc_rate() ?? */
1992 dmabuf
->fmt
&= ~TRIDENT_FMT_MASK
;
1993 if ((minor
& 0x0f) == SND_DEV_DSP16
)
1994 dmabuf
->fmt
|= TRIDENT_FMT_16BIT
;
1995 dmabuf
->ossfragshift
= 0;
1996 dmabuf
->ossmaxfrags
= 0;
1997 dmabuf
->subdivision
= 0;
1998 if (card
->pci_id
== PCI_DEVICE_ID_SI_7018
) {
1999 /* set default channel attribute to 0x8a80, record from
2000 PCM L/R FIFO and mono = (left + right + 1)/2*/
2001 dmabuf
->channel
->attribute
=
2002 (CHANNEL_REC
|PCM_LR
|MONO_MIX
);
2004 trident_set_adc_rate(state
, 8000);
2007 state
->open_mode
|= file
->f_mode
& (FMODE_READ
| FMODE_WRITE
);
2008 up(&card
->open_sem
);
2011 printk(KERN_ERR
"trident: open virtual channel %d, hard channel %d\n",
2012 state
->virt
, dmabuf
->channel
->num
);
2018 static int trident_release(struct inode
*inode
, struct file
*file
)
2020 struct trident_state
*state
= (struct trident_state
*)file
->private_data
;
2021 struct trident_card
*card
;
2022 struct dmabuf
*dmabuf
;
2026 dmabuf
= &state
->dmabuf
;
2027 VALIDATE_STATE(state
);
2029 if (file
->f_mode
& FMODE_WRITE
) {
2030 trident_clear_tail(state
);
2031 drain_dac(state
, file
->f_flags
& O_NONBLOCK
);
2034 /* stop DMA state machine and free DMA buffers/channels */
2035 down(&card
->open_sem
);
2037 if (file
->f_mode
& FMODE_WRITE
) {
2039 dealloc_dmabuf(state
);
2040 state
->card
->free_pcm_channel(state
->card
, dmabuf
->channel
->num
);
2042 if (file
->f_mode
& FMODE_READ
) {
2044 dealloc_dmabuf(state
);
2045 state
->card
->free_pcm_channel(state
->card
, dmabuf
->channel
->num
);
2048 card
->states
[state
->virt
] = NULL
;
2051 /* we're covered by the open_sem */
2052 up(&card
->open_sem
);
2058 static /*const*/ struct file_operations trident_audio_fops
= {
2060 llseek
: trident_llseek
,
2062 write
: trident_write
,
2064 ioctl
: trident_ioctl
,
2067 release
: trident_release
,
2070 /* trident specific AC97 functions */
2071 /* Write AC97 codec registers */
2072 static void trident_ac97_set(struct ac97_codec
*codec
, u8 reg
, u16 val
)
2074 struct trident_card
*card
= (struct trident_card
*)codec
->private_data
;
2075 unsigned int address
, mask
, busy
;
2076 unsigned short count
= 0xffff;
2077 unsigned long flags
;
2080 data
= ((u32
) val
) << 16;
2082 switch (card
->pci_id
)
2085 case PCI_DEVICE_ID_SI_7018
:
2086 address
= SI_AC97_WRITE
;
2087 mask
= SI_AC97_BUSY_WRITE
| SI_AC97_AUDIO_BUSY
;
2089 mask
|= SI_AC97_SECONDARY
;
2090 busy
= SI_AC97_BUSY_WRITE
;
2092 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX
:
2093 address
= DX_ACR0_AC97_W
;
2094 mask
= busy
= DX_AC97_BUSY_WRITE
;
2096 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX
:
2097 address
= NX_ACR1_AC97_W
;
2098 mask
= NX_AC97_BUSY_WRITE
;
2100 mask
|= NX_AC97_WRITE_SECONDARY
;
2101 busy
= NX_AC97_BUSY_WRITE
;
2105 spin_lock_irqsave(&card
->lock
, flags
);
2107 if ((inw(TRID_REG(card
, address
)) & busy
) == 0)
2112 data
|= (mask
| (reg
& AC97_REG_ADDR
));
2115 printk(KERN_ERR
"trident: AC97 CODEC write timed out.\n");
2116 spin_unlock_irqrestore(&card
->lock
, flags
);
2120 outl(data
, TRID_REG(card
, address
));
2121 spin_unlock_irqrestore(&card
->lock
, flags
);
2124 /* Read AC97 codec registers */
2125 static u16
trident_ac97_get(struct ac97_codec
*codec
, u8 reg
)
2127 struct trident_card
*card
= (struct trident_card
*)codec
->private_data
;
2128 unsigned int address
, mask
, busy
;
2129 unsigned short count
= 0xffff;
2130 unsigned long flags
;
2133 switch (card
->pci_id
)
2136 case PCI_DEVICE_ID_SI_7018
:
2137 address
= SI_AC97_READ
;
2138 mask
= SI_AC97_BUSY_READ
| SI_AC97_AUDIO_BUSY
;
2140 mask
|= SI_AC97_SECONDARY
;
2141 busy
= SI_AC97_BUSY_READ
;
2143 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX
:
2144 address
= DX_ACR1_AC97_R
;
2145 mask
= busy
= DX_AC97_BUSY_READ
;
2147 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX
:
2149 address
= NX_ACR3_AC97_R_SECONDARY
;
2151 address
= NX_ACR2_AC97_R_PRIMARY
;
2152 mask
= NX_AC97_BUSY_READ
;
2153 busy
= NX_AC97_BUSY_READ
| NX_AC97_BUSY_DATA
;
2157 data
= (mask
| (reg
& AC97_REG_ADDR
));
2159 spin_lock_irqsave(&card
->lock
, flags
);
2160 outl(data
, TRID_REG(card
, address
));
2162 data
= inl(TRID_REG(card
, address
));
2163 if ((data
& busy
) == 0)
2166 spin_unlock_irqrestore(&card
->lock
, flags
);
2169 printk(KERN_ERR
"trident: AC97 CODEC read timed out.\n");
2172 return ((u16
) (data
>> 16));
2175 /* Write AC97 codec registers for ALi*/
2176 static void ali_ac97_set(struct ac97_codec
*codec
, u8 reg
, u16 val
)
2178 struct trident_card
*card
= (struct trident_card
*)codec
->private_data
;
2179 unsigned int address
, mask
;
2180 unsigned int wCount1
= 0xffff;
2181 unsigned int wCount2
= 0xffff;
2182 unsigned long chk1
, chk2
;
2183 unsigned long flags
;
2186 data
= ((u32
) val
) << 16;
2188 address
= ALI_AC97_WRITE
;
2189 mask
= ALI_AC97_WRITE_ACTION
| ALI_AC97_AUDIO_BUSY
;
2191 mask
|= ALI_AC97_SECONDARY
;
2192 if (card
->revision
== 0x02)
2193 mask
|= ALI_AC97_WRITE_MIXER_REGISTER
;
2195 spin_lock_irqsave(&card
->lock
, flags
);
2197 if ((inw(TRID_REG(card
, address
)) & ALI_AC97_BUSY_WRITE
) == 0) {
2198 data
|= (mask
| (reg
& AC97_REG_ADDR
));
2200 chk1
= inl(TRID_REG(card
, ALI_STIMER
));
2201 chk2
= inl(TRID_REG(card
, ALI_STIMER
));
2202 while (wCount2
-- && (chk1
== chk2
))
2203 chk2
= inl(TRID_REG(card
, ALI_STIMER
));
2205 spin_unlock_irqrestore(&card
->lock
, flags
);
2208 outl(data
, TRID_REG(card
, address
)); //write!
2209 spin_unlock_irqrestore(&card
->lock
, flags
);
2212 inw(TRID_REG(card
, address
)); //wait a read cycle
2215 printk(KERN_ERR
"ali: AC97 CODEC write timed out.\n");
2216 spin_unlock_irqrestore(&card
->lock
, flags
);
2220 /* Read AC97 codec registers for ALi*/
2221 static u16
ali_ac97_get(struct ac97_codec
*codec
, u8 reg
)
2223 struct trident_card
*card
= (struct trident_card
*)codec
->private_data
;
2224 unsigned int address
, mask
;
2225 unsigned int wCount1
= 0xffff;
2226 unsigned int wCount2
= 0xffff;
2227 unsigned long chk1
, chk2
;
2228 unsigned long flags
;
2231 address
= ALI_AC97_READ
;
2232 if (card
->revision
== 0x02) {
2233 address
= ALI_AC97_WRITE
;
2234 mask
&= ALI_AC97_READ_MIXER_REGISTER
;
2236 mask
= ALI_AC97_READ_ACTION
| ALI_AC97_AUDIO_BUSY
;
2238 mask
|= ALI_AC97_SECONDARY
;
2240 spin_lock_irqsave(&card
->lock
, flags
);
2241 data
= (mask
| (reg
& AC97_REG_ADDR
));
2243 if ((inw(TRID_REG(card
, address
)) & ALI_AC97_BUSY_READ
) == 0) {
2244 chk1
= inl(TRID_REG(card
, ALI_STIMER
));
2245 chk2
= inl(TRID_REG(card
, ALI_STIMER
));
2246 while (wCount2
-- && (chk1
== chk2
))
2247 chk2
= inl(TRID_REG(card
, ALI_STIMER
));
2249 printk(KERN_ERR
"ali: AC97 CODEC read timed out.\n");
2250 spin_unlock_irqrestore(&card
->lock
, flags
);
2253 outl(data
, TRID_REG(card
, address
)); //read!
2256 if ((inw(TRID_REG(card
, address
)) & ALI_AC97_BUSY_READ
) == 0) {
2257 data
= inl(TRID_REG(card
, address
));
2258 spin_unlock_irqrestore(&card
->lock
, flags
);
2259 return ((u16
) (data
>> 16));
2263 inw(TRID_REG(card
, address
)); //wait a read cycle
2265 spin_unlock_irqrestore(&card
->lock
, flags
);
2266 printk(KERN_ERR
"ali: AC97 CODEC read timed out.\n");
2270 /* OSS /dev/mixer file operation methods */
2271 static int trident_open_mixdev(struct inode
*inode
, struct file
*file
)
2274 int minor
= MINOR(inode
->i_rdev
);
2275 struct trident_card
*card
= devs
;
2277 for (card
= devs
; card
!= NULL
; card
= card
->next
)
2278 for (i
= 0; i
< NR_AC97
; i
++)
2279 if (card
->ac97_codec
[i
] != NULL
&&
2280 card
->ac97_codec
[i
]->dev_mixer
== minor
)
2287 file
->private_data
= card
->ac97_codec
[i
];
2293 static int trident_ioctl_mixdev(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
2296 struct ac97_codec
*codec
= (struct ac97_codec
*)file
->private_data
;
2298 return codec
->mixer_ioctl(codec
, cmd
, arg
);
2301 static /*const*/ struct file_operations trident_mixer_fops
= {
2303 llseek
: trident_llseek
,
2304 ioctl
: trident_ioctl_mixdev
,
2305 open
: trident_open_mixdev
,
2308 /* AC97 codec initialisation. */
2309 static int __init
trident_ac97_init(struct trident_card
*card
)
2313 struct ac97_codec
*codec
;
2315 /* initialize controller side of AC link, and find out if secondary codes
2317 switch (card
->pci_id
)
2319 case PCI_DEVICE_ID_ALI_5451
:
2320 outl(PCMOUT
|SECONDARY_ID
, TRID_REG(card
, SI_SERIAL_INTF_CTRL
));
2321 ready_2nd
= inl(TRID_REG(card
, SI_SERIAL_INTF_CTRL
));
2322 ready_2nd
&= SI_AC97_SECONDARY_READY
;
2324 case PCI_DEVICE_ID_SI_7018
:
2325 /* disable AC97 GPIO interrupt */
2326 outl(0x00, TRID_REG(card
, SI_AC97_GPIO
));
2327 /* when power up the AC link is in cold reset mode so stop it */
2328 outl(PCMOUT
|SURROUT
|CENTEROUT
|LFEOUT
|SECONDARY_ID
,
2329 TRID_REG(card
, SI_SERIAL_INTF_CTRL
));
2330 /* it take a long time to recover from a cold reset (especially when you have
2331 more than one codec) */
2333 ready_2nd
= inl(TRID_REG(card
, SI_SERIAL_INTF_CTRL
));
2334 ready_2nd
&= SI_AC97_SECONDARY_READY
;
2336 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX
:
2338 outl(DX_AC97_PLAYBACK
, TRID_REG(card
, DX_ACR2_AC97_COM_STAT
));
2340 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX
:
2341 /* enable AC97 Output Slot 3,4 (PCM Left/Right Playback) */
2342 outl(NX_AC97_PCM_OUTPUT
, TRID_REG(card
, NX_ACR0_AC97_COM_STAT
));
2343 ready_2nd
= inl(TRID_REG(card
, NX_ACR0_AC97_COM_STAT
));
2344 ready_2nd
&= NX_AC97_SECONDARY_READY
;
2348 for (num_ac97
= 0; num_ac97
< NR_AC97
; num_ac97
++) {
2349 if ((codec
= kmalloc(sizeof(struct ac97_codec
), GFP_KERNEL
)) == NULL
)
2351 memset(codec
, 0, sizeof(struct ac97_codec
));
2353 /* initialize some basic codec information, other fields will be filled
2354 in ac97_probe_codec */
2355 codec
->private_data
= card
;
2356 codec
->id
= num_ac97
;
2358 if (card
->pci_id
== PCI_DEVICE_ID_ALI_5451
) {
2359 codec
->codec_read
= ali_ac97_get
;
2360 codec
->codec_write
= ali_ac97_set
;
2363 codec
->codec_read
= trident_ac97_get
;
2364 codec
->codec_write
= trident_ac97_set
;
2367 if (ac97_probe_codec(codec
) == 0)
2370 if ((codec
->dev_mixer
= register_sound_mixer(&trident_mixer_fops
, -1)) < 0) {
2371 printk(KERN_ERR
"trident: couldn't register mixer!\n");
2376 card
->ac97_codec
[num_ac97
] = codec
;
2378 /* if there is no secondary codec at all, don't probe any more */
2386 /* install the driver, we do not allocate hardware channel nor DMA buffer now, they are defered
2387 untill "ACCESS" time (in prog_dmabuf called by open/read/write/ioctl/mmap) */
2388 static int __init
trident_probe(struct pci_dev
*pci_dev
, const struct pci_device_id
*pci_id
)
2390 unsigned long iobase
;
2391 struct trident_card
*card
;
2394 if (!pci_dma_supported(pci_dev
, TRIDENT_DMA_MASK
)) {
2395 printk(KERN_ERR
"trident: architecture does not support"
2396 " 30bit PCI busmaster DMA\n");
2399 pci_read_config_byte(pci_dev
, PCI_CLASS_REVISION
, &revision
);
2401 iobase
= pci_resource_start(pci_dev
, 0);
2402 if (check_region(iobase
, 256)) {
2403 printk(KERN_ERR
"trident: can't allocate I/O space at 0x%4.4lx\n",
2408 if (pci_enable_device(pci_dev
))
2411 if ((card
= kmalloc(sizeof(struct trident_card
), GFP_KERNEL
)) == NULL
) {
2412 printk(KERN_ERR
"trident: out of memory\n");
2415 memset(card
, 0, sizeof(*card
));
2417 card
->iobase
= iobase
;
2418 card
->pci_dev
= pci_dev
;
2419 card
->pci_id
= pci_id
->device
;
2420 card
->revision
= revision
;
2421 card
->irq
= pci_dev
->irq
;
2423 card
->magic
= TRIDENT_CARD_MAGIC
;
2424 card
->banks
[BANK_A
].addresses
= &bank_a_addrs
;
2425 card
->banks
[BANK_A
].bitmap
= 0UL;
2426 card
->banks
[BANK_B
].addresses
= &bank_b_addrs
;
2427 card
->banks
[BANK_B
].bitmap
= 0UL;
2428 init_MUTEX(&card
->open_sem
);
2429 spin_lock_init(&card
->lock
);
2432 pci_set_master(pci_dev
);
2434 printk(KERN_INFO
"trident: %s found at IO 0x%04lx, IRQ %d\n",
2435 card_names
[pci_id
->driver_data
], card
->iobase
, card
->irq
);
2437 if(card
->pci_id
== PCI_DEVICE_ID_ALI_5451
) {
2438 card
->alloc_pcm_channel
= ali_alloc_pcm_channel
;
2439 card
->alloc_rec_pcm_channel
= ali_alloc_rec_pcm_channel
;
2440 card
->free_pcm_channel
= ali_free_pcm_channel
;
2441 card
->address_interrupt
= ali_address_interrupt
;
2444 card
->alloc_pcm_channel
= trident_alloc_pcm_channel
;
2445 card
->alloc_rec_pcm_channel
= trident_alloc_pcm_channel
;
2446 card
->free_pcm_channel
= trident_free_pcm_channel
;
2447 card
->address_interrupt
= trident_address_interrupt
;
2450 /* claim our iospace and irq */
2451 request_region(card
->iobase
, 256, card_names
[pci_id
->driver_data
]);
2452 if (request_irq(card
->irq
, &trident_interrupt
, SA_SHIRQ
,
2453 card_names
[pci_id
->driver_data
], card
)) {
2454 printk(KERN_ERR
"trident: unable to allocate irq %d\n", card
->irq
);
2455 release_region(card
->iobase
, 256);
2459 /* register /dev/dsp */
2460 if ((card
->dev_audio
= register_sound_dsp(&trident_audio_fops
, -1)) < 0) {
2461 printk(KERN_ERR
"trident: couldn't register DSP device!\n");
2462 release_region(iobase
, 256);
2463 free_irq(card
->irq
, card
);
2467 /* initilize AC97 codec and register /dev/mixer */
2468 if (trident_ac97_init(card
) <= 0) {
2469 unregister_sound_dsp(card
->dev_audio
);
2470 release_region(iobase
, 256);
2471 free_irq(card
->irq
, card
);
2475 outl(0x00, TRID_REG(card
, T4D_MUSICVOL_WAVEVOL
));
2477 if (card
->pci_id
== PCI_DEVICE_ID_ALI_5451
) {
2478 /* edited by HMSEO for GT sound */
2479 #ifdef CONFIG_ALPHA_NAUTILUS
2480 u16 ac97_data
= trident_ac97_get (card
->ac97_codec
[0], AC97_POWER_CONTROL
);
2481 trident_ac97_set (card
->ac97_codec
[0], AC97_POWER_CONTROL
,
2482 ac97_data
| ALI_EAPD_POWER_DOWN
);
2484 /* edited by HMSEO for GT sound*/
2487 pci_dev
->driver_data
= card
;
2488 pci_dev
->dma_mask
= TRIDENT_DMA_MASK
;
2490 /* Enable Address Engine Interrupts */
2491 trident_enable_loop_interrupts(card
);
2496 static void __exit
trident_remove(struct pci_dev
*pci_dev
)
2499 struct trident_card
*card
= pci_dev
->driver_data
;
2501 /* Kill interrupts, and SP/DIF */
2502 trident_disable_loop_interrupts(card
);
2504 /* free hardware resources */
2505 free_irq(card
->irq
, card
);
2506 release_region(card
->iobase
, 256);
2508 /* unregister audio devices */
2509 for (i
= 0; i
< NR_AC97
; i
++)
2510 if (card
->ac97_codec
[i
] != NULL
) {
2511 unregister_sound_mixer(card
->ac97_codec
[i
]->dev_mixer
);
2512 kfree (card
->ac97_codec
[i
]);
2514 unregister_sound_dsp(card
->dev_audio
);
2519 MODULE_AUTHOR("Alan Cox, Aaron Holtzman, Ollie Lho, Ching Ling Lee");
2520 MODULE_DESCRIPTION("Trident 4DWave/SiS 7018/ALi 5451 PCI Audio Driver");
2522 #define TRIDENT_MODULE_NAME "trident"
2524 static struct pci_driver trident_pci_driver
= {
2525 name
: TRIDENT_MODULE_NAME
,
2526 id_table
: trident_pci_tbl
,
2527 probe
: trident_probe
,
2528 remove
: trident_remove
,
2531 static int __init
trident_init_module (void)
2533 if (!pci_present()) /* No PCI bus in this machine! */
2536 printk(KERN_INFO
"Trident 4DWave/SiS 7018/ALi 5451 PCI Audio, version "
2537 DRIVER_VERSION
", " __TIME__
" " __DATE__
"\n");
2539 if (!pci_register_driver(&trident_pci_driver
)) {
2540 pci_unregister_driver(&trident_pci_driver
);
2546 static void __exit
trident_cleanup_module (void)
2548 pci_unregister_driver(&trident_pci_driver
);
2551 module_init(trident_init_module
);
2552 module_exit(trident_cleanup_module
);