2 * OSS driver for Linux 2.[46].x for
7 * Tvia/IGST CyberPro 5050
9 * Driver: Alan Cox <alan@redhat.com>
12 * Low level code: <audio@tridentmicro.com> from ALSA
13 * Framework: Thomas Sailer <sailer@ife.ee.ethz.ch>
14 * Extended by: Zach Brown <zab@redhat.com>
17 * Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
18 * Ollie Lho <ollie@sis.com.tw> SiS 7018 Audio Core Support
19 * Ching-Ling Lee <cling-li@ali.com.tw> ALi 5451 Audio Core Support
20 * Matt Wu <mattwu@acersoftech.com.cn> ALi 5451 Audio Core Support
21 * Peter Wächtler <pwaechtler@loewe-komp.de> CyberPro5050 support
22 * Muli Ben-Yehuda <mulix@mulix.org>
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
41 * January 3 2004 Eugene Teo <eugeneteo@eugeneteo.net>
42 * minor cleanup to use pr_debug instead of TRDBG since it is already
43 * defined in linux/kernel.h.
45 * December 29 2003 Muli Ben-Yehuda <mulix@mulix.org>
46 * major cleanup for 2.6, fix a few error patch buglets
47 * with returning without properly cleaning up first,
48 * get rid of lock_kernel().
50 * Sept 10 2002 Pascal Schmidt <der.eremit@email.de>
51 * added support for ALi 5451 joystick port
53 * Sept 05 2002 Alan Cox <alan@redhat.com>
54 * adapt to new pci joystick attachment interface
56 * July 24 2002 Muli Ben-Yehuda <mulix@actcom.co.il>
57 * patch from Eric Lemar (via Ian Soboroff): in suspend and resume,
58 * fix wrong cast from pci_dev* to struct trident_card*.
60 * July 19 2002 Muli Ben-Yehuda <mulix@actcom.co.il>
61 * rewrite the DMA buffer allocation/deallcoation functions, to make it
62 * modular and fix a bug where we would call free_pages on memory
63 * obtained with pci_alloc_consistent. Also remove unnecessary #ifdef
64 * CONFIG_PROC_FS and various other cleanups.
66 * July 19 2002 Muli Ben-Yehuda <mulix@actcom.co.il>
67 * made several printk(KERN_NOTICE...) into TRDBG(...), to avoid spamming
68 * my syslog with hundreds of messages.
70 * July 16 2002 Muli Ben-Yehuda <mulix@actcom.co.il>
71 * Cleaned up Lei Hu's 0.4.10 driver to conform to Documentation/CodingStyle
72 * and the coding style used in the rest of the file.
74 * June 23 2002 Muli Ben-Yehuda <mulix@actcom.co.il>
75 * add a missing unlock_set_fmt, remove a superflous lock/unlock pair
76 * with nothing in between.
78 * June 21 2002 Muli Ben-Yehuda <mulix@actcom.co.il>
79 * use a debug macro instead of #ifdef CONFIG_DEBUG, trim to 80 columns
80 * per line, use 'do {} while (0)' in statement macros.
82 * June 6 2002 Lei Hu <Lei_hu@ali.com.tw>
83 * rewrite the part to read/write registers of audio codec for Ali5451
85 * January 2 2002 Vojtech Pavlik <vojtech@ucw.cz> added gameport
86 * support to avoid resource conflict with pcigame.c
88 * October 8 2001 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
89 * use set_current_state, properly release resources on failure in
90 * trident_probe, get rid of check_region
92 * August 10 2001 Peter Wächtler <pwaechtler@loewe-komp.de>
93 * added support for Tvia (formerly Integraphics/IGST) CyberPro5050
94 * this chip is often found in settop boxes (combined video+audio)
96 * Switch to static inline not extern inline (gcc 3)
99 * 0.14.9 crashed on rmmod due to a timer/bh left running. Simplified
100 * the existing logic (the BH doesn't help as ac97 is lock_irqsave)
101 * and used del_timer_sync to clean up
102 * Fixed a problem where the ALi change broke my generic card
104 * Jul 10 2001 Matt Wu
105 * Add H/W Volume Control
107 * July 7 2001 Alan Cox
108 * Moved Matt Wu's ac97 register cache into the card structure
110 * Apr 30 2001 Matt Wu
111 * Set EBUF1 and EBUF2 to still mode
112 * Add dc97/ac97 reset function
113 * Fix power management: ali_restore_regs
115 * Mar 09 2001 Matt Wu
116 * Add cache for ac97 access
118 * Feb 06 2001 Matt Wu
119 * Fix ac97 initialization
120 * Fix bug: an extra tail will be played when playing
121 * Jan 05 2001 Matt Wu
122 * Implement multi-channels and S/PDIF in support for ALi 1535+
124 * Nov 1 2000 Ching-Ling Lee
125 * Fix the bug of memory leak when switching 5.1-channels to 2 channels.
126 * Add lock protection into dynamic changing format of data.
127 * Oct 18 2000 Ching-Ling Lee
128 * 5.1-channels support for ALi
129 * June 28 2000 Ching-Ling Lee
130 * S/PDIF out/in(playback/record) support for ALi 1535+, using /proc to be selected by user
131 * Simple Power Management support for ALi
132 * v0.14.5 May 23 2000 Ollie Lho
133 * Misc bug fix from the Net
134 * v0.14.4 May 20 2000 Aaron Holtzman
135 * Fix kfree'd memory access in release
136 * Fix race in open while looking for a free virtual channel slot
137 * remove open_wait wq (which appears to be unused)
138 * v0.14.3 May 10 2000 Ollie Lho
139 * fixed a small bug in trident_update_ptr, xmms 1.0.1 no longer uses 100% CPU
140 * v0.14.2 Mar 29 2000 Ching-Ling Lee
141 * Add clear to silence advance in trident_update_ptr
142 * fix invalid data of the end of the sound
143 * v0.14.1 Mar 24 2000 Ching-Ling Lee
144 * ALi 5451 support added, playback and recording O.K.
145 * ALi 5451 originally developed and structured based on sonicvibes, and
146 * suggested to merge into this file by Alan Cox.
147 * v0.14 Mar 15 2000 Ollie Lho
148 * 5.1 channel output support with channel binding. What's the Matrix ?
149 * v0.13.1 Mar 10 2000 Ollie Lho
150 * few minor bugs on dual codec support, needs more testing
151 * v0.13 Mar 03 2000 Ollie Lho
152 * new pci_* for 2.4 kernel, back ported to 2.2
153 * v0.12 Feb 23 2000 Ollie Lho
154 * Preliminary Recording support
155 * v0.11.2 Feb 19 2000 Ollie Lho
156 * removed incomplete full-dulplex support
157 * v0.11.1 Jan 28 2000 Ollie Lho
158 * small bug in setting sample rate for 4d-nx (reported by Aaron)
159 * v0.11 Jan 27 2000 Ollie Lho
160 * DMA bug, scheduler latency, second try
161 * v0.10 Jan 24 2000 Ollie Lho
162 * DMA bug fixed, found kernel scheduling problem
163 * v0.09 Jan 20 2000 Ollie Lho
164 * Clean up of channel register access routine (prepare for channel binding)
165 * v0.08 Jan 14 2000 Ollie Lho
166 * Isolation of AC97 codec code
167 * v0.07 Jan 13 2000 Ollie Lho
168 * Get rid of ugly old low level access routines (e.g. CHRegs.lp****)
169 * v0.06 Jan 11 2000 Ollie Lho
170 * Preliminary support for dual (more ?) AC97 codecs
171 * v0.05 Jan 08 2000 Luca Montecchiani <m.luca@iname.com>
172 * adapt to 2.3.x new __setup/__init call
173 * v0.04 Dec 31 1999 Ollie Lho
174 * Multiple Open, using Middle Loop Interrupt to smooth playback
175 * v0.03 Dec 24 1999 Ollie Lho
176 * mem leak in prog_dmabuf and dealloc_dmabuf removed
177 * v0.02 Dec 15 1999 Ollie Lho
178 * SiS 7018 support added, playback O.K.
179 * v0.01 Alan Cox et. al.
180 * Initial Release in kernel 2.3.30, does not work
183 * Clean up of low level channel register access code. (done)
184 * Fix the bug on dma buffer management in update_ptr, read/write, drain_dac (done)
185 * Dual AC97 codecs support (done)
186 * Recording support (done)
188 * "Channel Binding" ioctl extension (done)
189 * new pci device driver interface for 2.4 kernel (done)
191 * Lock order (high->low)
192 * lock - hardware lock
193 * open_sem - guard opens
194 * sem - guard dmabuf, write re-entry etc
197 #include <linux/config.h>
198 #include <linux/module.h>
199 #include <linux/string.h>
200 #include <linux/ctype.h>
201 #include <linux/ioport.h>
202 #include <linux/sched.h>
203 #include <linux/delay.h>
204 #include <linux/sound.h>
205 #include <linux/slab.h>
206 #include <linux/soundcard.h>
207 #include <linux/pci.h>
208 #include <linux/init.h>
209 #include <linux/poll.h>
210 #include <linux/spinlock.h>
211 #include <linux/smp_lock.h>
212 #include <linux/ac97_codec.h>
213 #include <linux/bitops.h>
214 #include <linux/proc_fs.h>
215 #include <linux/interrupt.h>
216 #include <linux/pm.h>
217 #include <linux/gameport.h>
218 #include <linux/kernel.h>
219 #include <asm/uaccess.h>
220 #include <asm/hardirq.h>
224 #if defined(CONFIG_ALPHA_NAUTILUS) || defined(CONFIG_ALPHA_GENERIC)
225 #include <asm/hwrpb.h>
230 #define DRIVER_VERSION "0.14.10j-2.6"
232 /* magic numbers to protect our data structures */
233 #define TRIDENT_CARD_MAGIC 0x5072696E /* "Prin" */
234 #define TRIDENT_STATE_MAGIC 0x63657373 /* "cess" */
236 #define TRIDENT_DMA_MASK 0x3fffffff /* DMA buffer mask for pci_alloc_consist */
237 #define ALI_DMA_MASK 0x7fffffff /* ALI Tridents have 31-bit DMA. Wow. */
241 /* maximum number of AC97 codecs connected, AC97 2.0 defined 4, but 7018 and 4D-NX only
242 have 2 SDATA_IN lines (currently) */
245 /* minor number of /dev/swmodem (temporary, experimental) */
246 #define SND_DEV_SWMODEM 7
248 static const unsigned ali_multi_channels_5_1[] = {
249 /*ALI_SURR_LEFT_CHANNEL, ALI_SURR_RIGHT_CHANNEL, */
252 ALI_SURR_LEFT_CHANNEL,
253 ALI_SURR_RIGHT_CHANNEL
256 static const unsigned sample_size[] = { 1, 2, 2, 4 };
257 static const unsigned sample_shift[] = { 0, 1, 1, 2 };
259 static const char invalid_magic[] = KERN_CRIT "trident: invalid magic value in %s\n";
269 static char *card_names[] = {
272 "SiS 7018 PCI Audio",
273 "ALi Audio Accelerator",
274 "Tvia/IGST CyberPro 5050"
277 static struct pci_device_id trident_pci_tbl[] = {
278 {PCI_VENDOR_ID_TRIDENT, PCI_DEVICE_ID_TRIDENT_4DWAVE_DX,
279 PCI_ANY_ID, PCI_ANY_ID, 0, 0, TRIDENT_4D_DX},
280 {PCI_VENDOR_ID_TRIDENT, PCI_DEVICE_ID_TRIDENT_4DWAVE_NX,
281 PCI_ANY_ID, PCI_ANY_ID, 0, 0, TRIDENT_4D_NX},
282 {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_7018,
283 PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_7018},
284 {PCI_VENDOR_ID_ALI, PCI_DEVICE_ID_ALI_5451,
285 PCI_ANY_ID, PCI_ANY_ID, 0, 0, ALI_5451},
286 {PCI_VENDOR_ID_INTERG, PCI_DEVICE_ID_INTERG_5050,
287 PCI_ANY_ID, PCI_ANY_ID, 0, 0, CYBER5050},
291 MODULE_DEVICE_TABLE(pci, trident_pci_tbl);
293 /* "software" or virtual channel, an instance of opened /dev/dsp */
294 struct trident_state {
296 struct trident_card *card; /* Card info */
301 /* virtual channel number */
305 /* wave sample stuff */
307 unsigned char fmt, enable;
309 /* hardware channel */
310 struct trident_channel *channel;
312 /* OSS buffer management stuff */
314 dma_addr_t dma_handle;
319 /* our buffer acts like a circular ring */
320 unsigned hwptr; /* where dma last started, updated by update_ptr */
321 unsigned swptr; /* where driver last clear/filled, updated by read/write */
322 int count; /* bytes to be comsumed or been generated by dma machine */
323 unsigned total_bytes; /* total bytes dmaed by hardware */
325 unsigned error; /* number of over/underruns */
326 /* put process on wait queue when no more space in buffer */
327 wait_queue_head_t wait;
329 /* redundant, but makes calculations easier */
332 unsigned fragsamples;
337 unsigned endcleared:1;
338 unsigned update_flag;
339 unsigned ossfragshift;
341 unsigned subdivision;
346 struct trident_state *other_states[4];
347 int multi_channels_adjust_count;
349 unsigned long fmt_flag;
350 /* Guard against mmap/write/read races */
351 struct semaphore sem;
355 /* hardware channels */
356 struct trident_channel {
357 int num; /* channel number */
358 u32 lba; /* Loop Begine Address, where dma buffer starts */
359 u32 eso; /* End Sample Offset, wehre dma buffer ends */
360 /* (in the unit of samples) */
361 u32 delta; /* delta value, sample rate / 48k for playback, */
362 /* 48k/sample rate for recording */
363 u16 attribute; /* control where PCM data go and come */
365 u32 control; /* signed/unsigned, 8/16 bits, mono/stereo */
368 struct trident_pcm_bank_address {
375 static struct trident_pcm_bank_address bank_a_addrs = {
382 static struct trident_pcm_bank_address bank_b_addrs = {
389 struct trident_pcm_bank {
390 /* register addresses to control bank operations */
391 struct trident_pcm_bank_address *addresses;
392 /* each bank has 32 channels */
393 u32 bitmap; /* channel allocation bitmap */
394 struct trident_channel channels[32];
397 struct trident_card {
400 /* We keep trident cards in a linked list */
401 struct trident_card *next;
403 /* single open lock mechanism, only used for recording */
404 struct semaphore open_sem;
406 /* The trident has a certain amount of cross channel interaction
407 so we use a single per card lock */
410 /* PCI device stuff */
411 struct pci_dev *pci_dev;
415 /* soundcore stuff */
418 /* structures for abstraction of hardware facilities, codecs, */
419 /* banks and channels */
420 struct ac97_codec *ac97_codec[NR_AC97];
421 struct trident_pcm_bank banks[NR_BANKS];
422 struct trident_state *states[NR_HW_CH];
424 /* hardware resources */
425 unsigned long iobase;
428 /* Function support */
429 struct trident_channel *(*alloc_pcm_channel) (struct trident_card *);
430 struct trident_channel *(*alloc_rec_pcm_channel) (struct trident_card *);
431 void (*free_pcm_channel) (struct trident_card *, unsigned int chan);
432 void (*address_interrupt) (struct trident_card *);
434 /* Added by Matt Wu 01-05-2001 for spdif in */
435 int multi_channel_use_count;
436 int rec_channel_use_count;
437 u16 mixer_regs[64][NR_AC97]; /* Made card local by Alan */
438 int mixer_regs_ready;
440 /* Added for hardware volume control */
442 struct timer_list timer;
444 /* Game port support */
445 struct gameport gameport;
453 /* table to map from CHANNELMASK to channel attribute for SiS 7018 */
454 static u16 mask2attr[] = {
455 PCM_LR, PCM_LR, SURR_LR, CENTER_LFE,
456 HSET, MIC, MODEM_LINE1, MODEM_LINE2,
460 /* table to map from channel attribute to CHANNELMASK for SiS 7018 */
461 static int attr2mask[] = {
462 DSP_BIND_MODEM1, DSP_BIND_MODEM2, DSP_BIND_FRONT, DSP_BIND_HANDSET,
463 DSP_BIND_I2S, DSP_BIND_CENTER_LFE, DSP_BIND_SURR, DSP_BIND_SPDIF
466 /* Added by Matt Wu 01-05-2001 for spdif in */
467 static int ali_close_multi_channels(void);
468 static void ali_delay(struct trident_card *card, int interval);
469 static void ali_detect_spdif_rate(struct trident_card *card);
471 static void ali_ac97_write(struct ac97_codec *codec, u8 reg, u16 val);
472 static u16 ali_ac97_read(struct ac97_codec *codec, u8 reg);
474 static struct trident_card *devs;
476 static void trident_ac97_set(struct ac97_codec *codec, u8 reg, u16 val);
477 static u16 trident_ac97_get(struct ac97_codec *codec, u8 reg);
479 static int trident_open_mixdev(struct inode *inode, struct file *file);
480 static int trident_ioctl_mixdev(struct inode *inode, struct file *file,
481 unsigned int cmd, unsigned long arg);
483 static void ali_ac97_set(struct trident_card *card, int secondary, u8 reg, u16 val);
484 static u16 ali_ac97_get(struct trident_card *card, int secondary, u8 reg);
485 static void ali_set_spdif_out_rate(struct trident_card *card, unsigned int rate);
486 static void ali_enable_special_channel(struct trident_state *stat);
487 static struct trident_channel *ali_alloc_rec_pcm_channel(struct trident_card *card);
488 static struct trident_channel *ali_alloc_pcm_channel(struct trident_card *card);
489 static void ali_restore_regs(struct trident_card *card);
490 static void ali_save_regs(struct trident_card *card);
491 static int trident_suspend(struct pci_dev *dev, u32 unused);
492 static int trident_resume(struct pci_dev *dev);
493 static void ali_free_pcm_channel(struct trident_card *card, unsigned int channel);
494 static int ali_setup_multi_channels(struct trident_card *card, int chan_nums);
495 static unsigned int ali_get_spdif_in_rate(struct trident_card *card);
496 static void ali_setup_spdif_in(struct trident_card *card);
497 static void ali_disable_spdif_in(struct trident_card *card);
498 static void ali_disable_special_channel(struct trident_card *card, int ch);
499 static void ali_setup_spdif_out(struct trident_card *card, int flag);
500 static int ali_write_5_1(struct trident_state *state, const char *buffer,
501 int cnt_for_multi_channel, unsigned int *copy_count,
502 unsigned int *state_cnt);
503 static int ali_allocate_other_states_resources(struct trident_state *state,
505 static void ali_free_other_states_resources(struct trident_state *state);
507 /* save registers for ALi Power Management */
508 static struct ali_saved_registers {
509 unsigned long global_regs[ALI_GLOBAL_REGS];
510 unsigned long channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
511 unsigned mixer_regs[ALI_MIXER_REGS];
514 #define seek_offset(dma_ptr, buffer, cnt, offset, copy_count) do { \
515 (dma_ptr) += (offset); \
516 (buffer) += (offset); \
518 (copy_count) += (offset); \
521 static inline int lock_set_fmt(struct trident_state* state)
523 if (test_and_set_bit(0, &state->fmt_flag))
529 static inline void unlock_set_fmt(struct trident_state* state)
531 clear_bit(0, &state->fmt_flag);
535 trident_enable_loop_interrupts(struct trident_card *card)
539 global_control = inl(TRID_REG(card, T4D_LFO_GC_CIR));
541 switch (card->pci_id) {
542 case PCI_DEVICE_ID_SI_7018:
543 global_control |= (ENDLP_IE | MIDLP_IE | BANK_B_EN);
545 case PCI_DEVICE_ID_ALI_5451:
546 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX:
547 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX:
548 case PCI_DEVICE_ID_INTERG_5050:
549 global_control |= (ENDLP_IE | MIDLP_IE);
555 outl(global_control, TRID_REG(card, T4D_LFO_GC_CIR));
557 pr_debug("trident: Enable Loop Interrupts, globctl = 0x%08X\n",
558 inl(TRID_REG(card, T4D_LFO_GC_CIR)));
564 trident_disable_loop_interrupts(struct trident_card *card)
568 global_control = inl(TRID_REG(card, T4D_LFO_GC_CIR));
569 global_control &= ~(ENDLP_IE | MIDLP_IE);
570 outl(global_control, TRID_REG(card, T4D_LFO_GC_CIR));
572 pr_debug("trident: Disabled Loop Interrupts, globctl = 0x%08X\n",
579 trident_enable_voice_irq(struct trident_card *card, unsigned int channel)
581 unsigned int mask = 1 << (channel & 0x1f);
582 struct trident_pcm_bank *bank = &card->banks[channel >> 5];
583 u32 reg, addr = bank->addresses->aint_en;
585 reg = inl(TRID_REG(card, addr));
587 outl(reg, TRID_REG(card, addr));
590 reg = inl(TRID_REG(card, addr));
591 pr_debug("trident: enabled IRQ on channel %d, %s = 0x%08x(addr:%X)\n",
592 channel, addr == T4D_AINTEN_B ? "AINTEN_B" : "AINTEN_A",
598 trident_disable_voice_irq(struct trident_card *card, unsigned int channel)
600 unsigned int mask = 1 << (channel & 0x1f);
601 struct trident_pcm_bank *bank = &card->banks[channel >> 5];
602 u32 reg, addr = bank->addresses->aint_en;
604 reg = inl(TRID_REG(card, addr));
606 outl(reg, TRID_REG(card, addr));
608 /* Ack the channel in case the interrupt was set before we disable it. */
609 outl(mask, TRID_REG(card, bank->addresses->aint));
612 reg = inl(TRID_REG(card, addr));
613 pr_debug("trident: disabled IRQ on channel %d, %s = 0x%08x(addr:%X)\n",
614 channel, addr == T4D_AINTEN_B ? "AINTEN_B" : "AINTEN_A",
620 trident_start_voice(struct trident_card *card, unsigned int channel)
622 unsigned int mask = 1 << (channel & 0x1f);
623 struct trident_pcm_bank *bank = &card->banks[channel >> 5];
624 u32 addr = bank->addresses->start;
630 outl(mask, TRID_REG(card, addr));
633 reg = inl(TRID_REG(card, addr));
634 pr_debug("trident: start voice on channel %d, %s = 0x%08x(addr:%X)\n",
635 channel, addr == T4D_START_B ? "START_B" : "START_A",
641 trident_stop_voice(struct trident_card *card, unsigned int channel)
643 unsigned int mask = 1 << (channel & 0x1f);
644 struct trident_pcm_bank *bank = &card->banks[channel >> 5];
645 u32 addr = bank->addresses->stop;
651 outl(mask, TRID_REG(card, addr));
654 reg = inl(TRID_REG(card, addr));
655 pr_debug("trident: stop voice on channel %d, %s = 0x%08x(addr:%X)\n",
656 channel, addr == T4D_STOP_B ? "STOP_B" : "STOP_A",
662 trident_get_interrupt_mask(struct trident_card *card, unsigned int channel)
664 struct trident_pcm_bank *bank = &card->banks[channel];
665 u32 addr = bank->addresses->aint;
666 return inl(TRID_REG(card, addr));
670 trident_check_channel_interrupt(struct trident_card *card, unsigned int channel)
672 unsigned int mask = 1 << (channel & 0x1f);
673 u32 reg = trident_get_interrupt_mask(card, channel >> 5);
677 pr_debug("trident: channel %d has interrupt, %s = 0x%08x\n",
678 channel, reg == T4D_AINT_B ? "AINT_B" : "AINT_A",
681 return (reg & mask) ? 1 : 0;
685 trident_ack_channel_interrupt(struct trident_card *card, unsigned int channel)
687 unsigned int mask = 1 << (channel & 0x1f);
688 struct trident_pcm_bank *bank = &card->banks[channel >> 5];
689 u32 reg, addr = bank->addresses->aint;
691 reg = inl(TRID_REG(card, addr));
693 outl(reg, TRID_REG(card, addr));
696 reg = inl(TRID_REG(card, T4D_AINT_B));
697 pr_debug("trident: Ack channel %d interrupt, AINT_B = 0x%08x\n",
702 static struct trident_channel *
703 trident_alloc_pcm_channel(struct trident_card *card)
705 struct trident_pcm_bank *bank;
708 bank = &card->banks[BANK_B];
710 for (idx = 31; idx >= 0; idx--) {
711 if (!(bank->bitmap & (1 << idx))) {
712 struct trident_channel *channel = &bank->channels[idx];
713 bank->bitmap |= 1 << idx;
714 channel->num = idx + 32;
719 /* no more free channels available */
720 printk(KERN_ERR "trident: no more channels available on Bank B.\n");
725 trident_free_pcm_channel(struct trident_card *card, unsigned int channel)
730 if (channel < 31 || channel > 63)
733 if (card->pci_id == PCI_DEVICE_ID_TRIDENT_4DWAVE_DX ||
734 card->pci_id == PCI_DEVICE_ID_TRIDENT_4DWAVE_NX) {
735 b = inb(TRID_REG(card, T4D_REC_CH));
736 if ((b & ~0x80) == channel)
737 outb(0x0, TRID_REG(card, T4D_REC_CH));
741 channel = channel & 0x1f;
743 card->banks[bank].bitmap &= ~(1 << (channel));
746 static struct trident_channel *
747 cyber_alloc_pcm_channel(struct trident_card *card)
749 struct trident_pcm_bank *bank;
752 /* The cyberpro 5050 has only 32 voices and one bank */
753 /* .. at least they are not documented (if you want to call that
754 * crap documentation), perhaps broken ? */
756 bank = &card->banks[BANK_A];
758 for (idx = 31; idx >= 0; idx--) {
759 if (!(bank->bitmap & (1 << idx))) {
760 struct trident_channel *channel = &bank->channels[idx];
761 bank->bitmap |= 1 << idx;
767 /* no more free channels available */
768 printk(KERN_ERR "cyberpro5050: no more channels available on Bank A.\n");
773 cyber_free_pcm_channel(struct trident_card *card, unsigned int channel)
777 card->banks[BANK_A].bitmap &= ~(1 << (channel));
781 cyber_outidx(int port, int idx, int data)
784 outb(data, port + 1);
788 cyber_inidx(int port, int idx)
791 return inb(port + 1);
795 cyber_init_ritual(struct trident_card *card)
797 /* some black magic, taken from SDK samples */
798 /* remove this and nothing will work */
804 * Keep interrupts off for the configure - we don't want to
805 * clash with another cyberpro config event
808 spin_lock_irqsave(&card->lock, flags);
809 portDat = cyber_inidx(CYBER_PORT_AUDIO, CYBER_IDX_AUDIO_ENABLE);
810 /* enable, if it was disabled */
811 if ((portDat & CYBER_BMSK_AUENZ) != CYBER_BMSK_AUENZ_ENABLE) {
812 printk(KERN_INFO "cyberpro5050: enabling audio controller\n");
813 cyber_outidx(CYBER_PORT_AUDIO, CYBER_IDX_AUDIO_ENABLE,
814 portDat | CYBER_BMSK_AUENZ_ENABLE);
815 /* check again if hardware is enabled now */
816 portDat = cyber_inidx(CYBER_PORT_AUDIO, CYBER_IDX_AUDIO_ENABLE);
818 if ((portDat & CYBER_BMSK_AUENZ) != CYBER_BMSK_AUENZ_ENABLE) {
819 printk(KERN_ERR "cyberpro5050: initAudioAccess: no success\n");
822 cyber_outidx(CYBER_PORT_AUDIO, CYBER_IDX_IRQ_ENABLE,
823 CYBER_BMSK_AUDIO_INT_ENABLE);
824 cyber_outidx(CYBER_PORT_AUDIO, 0xbf, 0x01);
825 cyber_outidx(CYBER_PORT_AUDIO, 0xba, 0x20);
826 cyber_outidx(CYBER_PORT_AUDIO, 0xbb, 0x08);
827 cyber_outidx(CYBER_PORT_AUDIO, 0xbf, 0x02);
828 cyber_outidx(CYBER_PORT_AUDIO, 0xb3, 0x06);
829 cyber_outidx(CYBER_PORT_AUDIO, 0xbf, 0x00);
831 spin_unlock_irqrestore(&card->lock, flags);
835 /* called with spin lock held */
838 trident_load_channel_registers(struct trident_card *card, u32 * data,
839 unsigned int channel)
846 /* select hardware channel to write */
847 outb(channel, TRID_REG(card, T4D_LFO_GC_CIR));
849 /* Output the channel registers, but don't write register
850 three to an ALI chip. */
851 for (i = 0; i < CHANNEL_REGS; i++) {
852 if (i == 3 && card->pci_id == PCI_DEVICE_ID_ALI_5451)
854 outl(data[i], TRID_REG(card, CHANNEL_START + 4 * i));
856 if (card->pci_id == PCI_DEVICE_ID_ALI_5451 ||
857 card->pci_id == PCI_DEVICE_ID_INTERG_5050) {
858 outl(ALI_EMOD_Still, TRID_REG(card, ALI_EBUF1));
859 outl(ALI_EMOD_Still, TRID_REG(card, ALI_EBUF2));
864 /* called with spin lock held */
866 trident_write_voice_regs(struct trident_state *state)
868 unsigned int data[CHANNEL_REGS + 1];
869 struct trident_channel *channel;
871 channel = state->dmabuf.channel;
873 data[1] = channel->lba;
874 data[4] = channel->control;
876 switch (state->card->pci_id) {
877 case PCI_DEVICE_ID_ALI_5451:
878 data[0] = 0; /* Current Sample Offset */
879 data[2] = (channel->eso << 16) | (channel->delta & 0xffff);
882 case PCI_DEVICE_ID_SI_7018:
883 case PCI_DEVICE_ID_INTERG_5050:
884 data[0] = 0; /* Current Sample Offset */
885 data[2] = (channel->eso << 16) | (channel->delta & 0xffff);
886 data[3] = (channel->attribute << 16) | (channel->fm_vol & 0xffff);
888 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX:
889 data[0] = 0; /* Current Sample Offset */
890 data[2] = (channel->eso << 16) | (channel->delta & 0xffff);
891 data[3] = channel->fm_vol & 0xffff;
893 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX:
894 data[0] = (channel->delta << 24);
895 data[2] = ((channel->delta << 16) & 0xff000000) |
896 (channel->eso & 0x00ffffff);
897 data[3] = channel->fm_vol & 0xffff;
903 return trident_load_channel_registers(state->card, data, channel->num);
907 compute_rate_play(u32 rate)
910 /* We special case 44100 and 8000 since rounding with the equation
911 does not give us an accurate enough value. For 11025 and 22050
912 the equation gives us the best answer. All other frequencies will
913 also use the equation. JDW */
916 else if (rate == 8000)
918 else if (rate == 48000)
921 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
926 compute_rate_rec(u32 rate)
932 else if (rate == 8000)
934 else if (rate == 48000)
937 delta = ((48000 << 12) / rate) & 0x0000ffff;
942 /* set playback sample rate */
944 trident_set_dac_rate(struct trident_state *state, unsigned int rate)
946 struct dmabuf *dmabuf = &state->dmabuf;
954 dmabuf->channel->delta = compute_rate_play(rate);
956 trident_write_voice_regs(state);
958 pr_debug("trident: called trident_set_dac_rate : rate = %d\n", rate);
963 /* set recording sample rate */
965 trident_set_adc_rate(struct trident_state *state, unsigned int rate)
967 struct dmabuf *dmabuf = &state->dmabuf;
975 dmabuf->channel->delta = compute_rate_rec(rate);
977 trident_write_voice_regs(state);
979 pr_debug("trident: called trident_set_adc_rate : rate = %d\n", rate);
984 /* prepare channel attributes for playback */
986 trident_play_setup(struct trident_state *state)
988 struct dmabuf *dmabuf = &state->dmabuf;
989 struct trident_channel *channel = dmabuf->channel;
991 channel->lba = dmabuf->dma_handle;
992 channel->delta = compute_rate_play(dmabuf->rate);
994 channel->eso = dmabuf->dmasize >> sample_shift[dmabuf->fmt];
997 if (state->card->pci_id != PCI_DEVICE_ID_SI_7018) {
998 channel->attribute = 0;
999 if (state->card->pci_id == PCI_DEVICE_ID_ALI_5451) {
1000 if ((channel->num == ALI_SPDIF_IN_CHANNEL) ||
1001 (channel->num == ALI_PCM_IN_CHANNEL))
1002 ali_disable_special_channel(state->card, channel->num);
1003 else if ((inl(TRID_REG(state->card, ALI_GLOBAL_CONTROL))
1004 & ALI_SPDIF_OUT_CH_ENABLE)
1005 && (channel->num == ALI_SPDIF_OUT_CHANNEL)) {
1006 ali_set_spdif_out_rate(state->card,
1007 state->dmabuf.rate);
1008 state->dmabuf.channel->delta = 0x1000;
1013 channel->fm_vol = 0x0;
1015 channel->control = CHANNEL_LOOP;
1016 if (dmabuf->fmt & TRIDENT_FMT_16BIT) {
1018 channel->control |= CHANNEL_16BITS;
1020 channel->control |= CHANNEL_SIGNED;
1022 if (dmabuf->fmt & TRIDENT_FMT_STEREO)
1024 channel->control |= CHANNEL_STEREO;
1026 pr_debug("trident: trident_play_setup, LBA = 0x%08x, Delta = 0x%08x, "
1027 "ESO = 0x%08x, Control = 0x%08x\n", channel->lba,
1028 channel->delta, channel->eso, channel->control);
1030 trident_write_voice_regs(state);
1033 /* prepare channel attributes for recording */
1035 trident_rec_setup(struct trident_state *state)
1040 struct trident_card *card = state->card;
1041 struct dmabuf *dmabuf = &state->dmabuf;
1042 struct trident_channel *channel = dmabuf->channel;
1045 /* Enable AC-97 ADC (capture) */
1046 switch (card->pci_id) {
1047 case PCI_DEVICE_ID_ALI_5451:
1048 ali_enable_special_channel(state);
1050 case PCI_DEVICE_ID_SI_7018:
1051 /* for 7018, the ac97 is always in playback/record (duplex) mode */
1053 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX:
1054 w = inb(TRID_REG(card, DX_ACR2_AC97_COM_STAT));
1055 outb(w | 0x48, TRID_REG(card, DX_ACR2_AC97_COM_STAT));
1056 /* enable and set record channel */
1057 outb(0x80 | channel->num, TRID_REG(card, T4D_REC_CH));
1059 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX:
1060 w = inw(TRID_REG(card, T4D_MISCINT));
1061 outw(w | 0x1000, TRID_REG(card, T4D_MISCINT));
1062 /* enable and set record channel */
1063 outb(0x80 | channel->num, TRID_REG(card, T4D_REC_CH));
1065 case PCI_DEVICE_ID_INTERG_5050:
1066 /* don't know yet, using special channel 22 in GC1(0xd4)? */
1072 channel->lba = dmabuf->dma_handle;
1073 channel->delta = compute_rate_rec(dmabuf->rate);
1074 if ((card->pci_id == PCI_DEVICE_ID_ALI_5451) &&
1075 (channel->num == ALI_SPDIF_IN_CHANNEL)) {
1076 rate = ali_get_spdif_in_rate(card);
1078 printk(KERN_WARNING "trident: ALi 5451 "
1079 "S/PDIF input setup error!\n");
1082 bval = inb(TRID_REG(card, ALI_SPDIF_CTRL));
1084 outb(bval, TRID_REG(card, ALI_SPDIF_CTRL));
1085 printk(KERN_WARNING "trident: cleared ALi "
1086 "5451 S/PDIF parity error flag.\n");
1090 channel->delta = ((rate << 12) / dmabuf->rate) & 0x0000ffff;
1093 channel->eso = dmabuf->dmasize >> sample_shift[dmabuf->fmt];
1096 if (state->card->pci_id != PCI_DEVICE_ID_SI_7018) {
1097 channel->attribute = 0;
1100 channel->fm_vol = 0x0;
1102 channel->control = CHANNEL_LOOP;
1103 if (dmabuf->fmt & TRIDENT_FMT_16BIT) {
1105 channel->control |= CHANNEL_16BITS;
1107 channel->control |= CHANNEL_SIGNED;
1109 if (dmabuf->fmt & TRIDENT_FMT_STEREO)
1111 channel->control |= CHANNEL_STEREO;
1113 pr_debug("trident: trident_rec_setup, LBA = 0x%08x, Delat = 0x%08x, "
1114 "ESO = 0x%08x, Control = 0x%08x\n", channel->lba,
1115 channel->delta, channel->eso, channel->control);
1117 trident_write_voice_regs(state);
1120 /* get current playback/recording dma buffer pointer (byte offset from LBA),
1121 called with spinlock held! */
1122 static inline unsigned
1123 trident_get_dma_addr(struct trident_state *state)
1125 struct dmabuf *dmabuf = &state->dmabuf;
1128 if (!dmabuf->enable)
1131 outb(dmabuf->channel->num, TRID_REG(state->card, T4D_LFO_GC_CIR));
1133 switch (state->card->pci_id) {
1134 case PCI_DEVICE_ID_ALI_5451:
1135 case PCI_DEVICE_ID_SI_7018:
1136 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX:
1137 case PCI_DEVICE_ID_INTERG_5050:
1138 /* 16 bits ESO, CSO for 7018 and DX */
1139 cso = inw(TRID_REG(state->card, CH_DX_CSO_ALPHA_FMS + 2));
1141 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX:
1142 /* 24 bits ESO, CSO for NX */
1143 cso = inl(TRID_REG(state->card, CH_NX_DELTA_CSO)) & 0x00ffffff;
1149 pr_debug("trident: trident_get_dma_addr: chip reported channel: %d, "
1150 "cso = 0x%04x\n", dmabuf->channel->num, cso);
1152 /* ESO and CSO are in units of Samples, convert to byte offset */
1153 cso <<= sample_shift[dmabuf->fmt];
1155 return (cso % dmabuf->dmasize);
1158 /* Stop recording (lock held) */
1160 __stop_adc(struct trident_state *state)
1162 struct dmabuf *dmabuf = &state->dmabuf;
1163 unsigned int chan_num = dmabuf->channel->num;
1164 struct trident_card *card = state->card;
1166 dmabuf->enable &= ~ADC_RUNNING;
1167 trident_stop_voice(card, chan_num);
1168 trident_disable_voice_irq(card, chan_num);
1172 stop_adc(struct trident_state *state)
1174 struct trident_card *card = state->card;
1175 unsigned long flags;
1177 spin_lock_irqsave(&card->lock, flags);
1179 spin_unlock_irqrestore(&card->lock, flags);
1183 start_adc(struct trident_state *state)
1185 struct dmabuf *dmabuf = &state->dmabuf;
1186 unsigned int chan_num = dmabuf->channel->num;
1187 struct trident_card *card = state->card;
1188 unsigned long flags;
1190 spin_lock_irqsave(&card->lock, flags);
1191 if ((dmabuf->mapped ||
1192 dmabuf->count < (signed) dmabuf->dmasize) &&
1194 dmabuf->enable |= ADC_RUNNING;
1195 trident_enable_voice_irq(card, chan_num);
1196 trident_start_voice(card, chan_num);
1198 spin_unlock_irqrestore(&card->lock, flags);
1201 /* stop playback (lock held) */
1203 __stop_dac(struct trident_state *state)
1205 struct dmabuf *dmabuf = &state->dmabuf;
1206 unsigned int chan_num = dmabuf->channel->num;
1207 struct trident_card *card = state->card;
1209 dmabuf->enable &= ~DAC_RUNNING;
1210 trident_stop_voice(card, chan_num);
1211 if (state->chans_num == 6) {
1212 trident_stop_voice(card, state->other_states[0]->
1213 dmabuf.channel->num);
1214 trident_stop_voice(card, state->other_states[1]->
1215 dmabuf.channel->num);
1216 trident_stop_voice(card, state->other_states[2]->
1217 dmabuf.channel->num);
1218 trident_stop_voice(card, state->other_states[3]->
1219 dmabuf.channel->num);
1221 trident_disable_voice_irq(card, chan_num);
1225 stop_dac(struct trident_state *state)
1227 struct trident_card *card = state->card;
1228 unsigned long flags;
1230 spin_lock_irqsave(&card->lock, flags);
1232 spin_unlock_irqrestore(&card->lock, flags);
1236 start_dac(struct trident_state *state)
1238 struct dmabuf *dmabuf = &state->dmabuf;
1239 unsigned int chan_num = dmabuf->channel->num;
1240 struct trident_card *card = state->card;
1241 unsigned long flags;
1243 spin_lock_irqsave(&card->lock, flags);
1244 if ((dmabuf->mapped || dmabuf->count > 0) && dmabuf->ready) {
1245 dmabuf->enable |= DAC_RUNNING;
1246 trident_enable_voice_irq(card, chan_num);
1247 trident_start_voice(card, chan_num);
1248 if (state->chans_num == 6) {
1249 trident_start_voice(card, state->other_states[0]->
1250 dmabuf.channel->num);
1251 trident_start_voice(card, state->other_states[1]->
1252 dmabuf.channel->num);
1253 trident_start_voice(card, state->other_states[2]->
1254 dmabuf.channel->num);
1255 trident_start_voice(card, state->other_states[3]->
1256 dmabuf.channel->num);
1259 spin_unlock_irqrestore(&card->lock, flags);
1262 #define DMABUF_DEFAULTORDER (15-PAGE_SHIFT)
1263 #define DMABUF_MINORDER 1
1265 /* alloc a DMA buffer of with a buffer of this order */
1267 alloc_dmabuf(struct dmabuf *dmabuf, struct pci_dev *pci_dev, int order)
1269 void *rawbuf = NULL;
1270 struct page *page, *pend;
1272 if (!(rawbuf = pci_alloc_consistent(pci_dev, PAGE_SIZE << order,
1273 &dmabuf->dma_handle)))
1276 pr_debug("trident: allocated %ld (order = %d) bytes at %p\n",
1277 PAGE_SIZE << order, order, rawbuf);
1279 dmabuf->ready = dmabuf->mapped = 0;
1280 dmabuf->rawbuf = rawbuf;
1281 dmabuf->buforder = order;
1283 /* now mark the pages as reserved; otherwise */
1284 /* remap_page_range doesn't do what we want */
1285 pend = virt_to_page(rawbuf + (PAGE_SIZE << order) - 1);
1286 for (page = virt_to_page(rawbuf); page <= pend; page++)
1287 SetPageReserved(page);
1292 /* allocate the main DMA buffer, playback and recording buffer should be */
1293 /* allocated separately */
1295 alloc_main_dmabuf(struct trident_state *state)
1297 struct dmabuf *dmabuf = &state->dmabuf;
1301 /* alloc as big a chunk as we can, FIXME: is this necessary ?? */
1302 for (order = DMABUF_DEFAULTORDER; order >= DMABUF_MINORDER; order--) {
1303 if (!(ret = alloc_dmabuf(dmabuf, state->card->pci_dev, order)))
1305 /* else try again */
1310 /* deallocate a DMA buffer */
1312 dealloc_dmabuf(struct dmabuf *dmabuf, struct pci_dev *pci_dev)
1314 struct page *page, *pend;
1316 if (dmabuf->rawbuf) {
1317 /* undo marking the pages as reserved */
1318 pend = virt_to_page(dmabuf->rawbuf + (PAGE_SIZE << dmabuf->buforder) - 1);
1319 for (page = virt_to_page(dmabuf->rawbuf); page <= pend; page++)
1320 ClearPageReserved(page);
1321 pci_free_consistent(pci_dev, PAGE_SIZE << dmabuf->buforder,
1322 dmabuf->rawbuf, dmabuf->dma_handle);
1323 dmabuf->rawbuf = NULL;
1325 dmabuf->mapped = dmabuf->ready = 0;
1329 prog_dmabuf(struct trident_state *state, enum dmabuf_mode rec)
1331 struct dmabuf *dmabuf = &state->dmabuf;
1332 unsigned bytepersec;
1333 struct trident_state *s = state;
1334 unsigned bufsize, dma_nums;
1335 unsigned long flags;
1338 if ((ret = lock_set_fmt(state)) < 0)
1341 if (state->chans_num == 6)
1346 for (i = 0; i < dma_nums; i++) {
1348 s = state->other_states[i - 1];
1349 dmabuf = &s->dmabuf;
1350 dmabuf->fmt = state->dmabuf.fmt;
1351 dmabuf->rate = state->dmabuf.rate;
1354 spin_lock_irqsave(&s->card->lock, flags);
1355 dmabuf->hwptr = dmabuf->swptr = dmabuf->total_bytes = 0;
1356 dmabuf->count = dmabuf->error = 0;
1357 spin_unlock_irqrestore(&s->card->lock, flags);
1359 /* allocate DMA buffer if not allocated yet */
1360 if (!dmabuf->rawbuf) {
1362 if ((ret = alloc_main_dmabuf(state))) {
1363 unlock_set_fmt(state);
1368 order = state->dmabuf.buforder - 1;
1369 if (order >= DMABUF_MINORDER) {
1370 ret = alloc_dmabuf(dmabuf,
1371 state->card->pci_dev,
1375 /* release the main DMA buffer */
1376 dealloc_dmabuf(&state->dmabuf, state->card->pci_dev);
1377 /* release the auxiliary DMA buffers */
1378 for (i -= 2; i >= 0; i--)
1379 dealloc_dmabuf(&state->other_states[i]->dmabuf,
1380 state->card->pci_dev);
1381 unlock_set_fmt(state);
1386 /* FIXME: figure out all this OSS fragment stuff */
1387 bytepersec = dmabuf->rate << sample_shift[dmabuf->fmt];
1388 bufsize = PAGE_SIZE << dmabuf->buforder;
1389 if (dmabuf->ossfragshift) {
1390 if ((1000 << dmabuf->ossfragshift) < bytepersec)
1391 dmabuf->fragshift = ld2(bytepersec / 1000);
1393 dmabuf->fragshift = dmabuf->ossfragshift;
1395 /* lets hand out reasonable big ass buffers by default */
1396 dmabuf->fragshift = (dmabuf->buforder + PAGE_SHIFT - 2);
1398 dmabuf->numfrag = bufsize >> dmabuf->fragshift;
1399 while (dmabuf->numfrag < 4 && dmabuf->fragshift > 3) {
1400 dmabuf->fragshift--;
1401 dmabuf->numfrag = bufsize >> dmabuf->fragshift;
1403 dmabuf->fragsize = 1 << dmabuf->fragshift;
1404 if (dmabuf->ossmaxfrags >= 4 && dmabuf->ossmaxfrags < dmabuf->numfrag)
1405 dmabuf->numfrag = dmabuf->ossmaxfrags;
1406 dmabuf->fragsamples = dmabuf->fragsize >> sample_shift[dmabuf->fmt];
1407 dmabuf->dmasize = dmabuf->numfrag << dmabuf->fragshift;
1409 memset(dmabuf->rawbuf, (dmabuf->fmt & TRIDENT_FMT_16BIT) ? 0 : 0x80,
1412 spin_lock_irqsave(&s->card->lock, flags);
1413 if (rec == DM_RECORD)
1414 trident_rec_setup(s);
1415 else /* DM_PLAYBACK */
1416 trident_play_setup(s);
1418 spin_unlock_irqrestore(&s->card->lock, flags);
1420 /* set the ready flag for the dma buffer */
1423 pr_debug("trident: prog_dmabuf(%d), sample rate = %d, "
1424 "format = %d, numfrag = %d, fragsize = %d "
1425 "dmasize = %d\n", dmabuf->channel->num,
1426 dmabuf->rate, dmabuf->fmt, dmabuf->numfrag,
1427 dmabuf->fragsize, dmabuf->dmasize);
1429 unlock_set_fmt(state);
1434 static inline int prog_dmabuf_record(struct trident_state* state)
1436 return prog_dmabuf(state, DM_RECORD);
1439 static inline int prog_dmabuf_playback(struct trident_state* state)
1441 return prog_dmabuf(state, DM_PLAYBACK);
1444 /* we are doing quantum mechanics here, the buffer can only be empty, half or full filled i.e.
1445 |------------|------------| or |xxxxxxxxxxxx|------------| or |xxxxxxxxxxxx|xxxxxxxxxxxx|
1446 but we almost always get this
1447 |xxxxxx------|------------| or |xxxxxxxxxxxx|xxxxx-------|
1448 so we have to clear the tail space to "silence"
1449 |xxxxxx000000|------------| or |xxxxxxxxxxxx|xxxxxx000000|
1452 trident_clear_tail(struct trident_state *state)
1454 struct dmabuf *dmabuf = &state->dmabuf;
1456 unsigned char silence = (dmabuf->fmt & TRIDENT_FMT_16BIT) ? 0 : 0x80;
1458 unsigned long flags;
1460 spin_lock_irqsave(&state->card->lock, flags);
1461 swptr = dmabuf->swptr;
1462 spin_unlock_irqrestore(&state->card->lock, flags);
1464 if (swptr == 0 || swptr == dmabuf->dmasize / 2 ||
1465 swptr == dmabuf->dmasize)
1468 if (swptr < dmabuf->dmasize / 2)
1469 len = dmabuf->dmasize / 2 - swptr;
1471 len = dmabuf->dmasize - swptr;
1473 memset(dmabuf->rawbuf + swptr, silence, len);
1474 if (state->card->pci_id != PCI_DEVICE_ID_ALI_5451) {
1475 spin_lock_irqsave(&state->card->lock, flags);
1476 dmabuf->swptr += len;
1477 dmabuf->count += len;
1478 spin_unlock_irqrestore(&state->card->lock, flags);
1481 /* restart the dma machine in case it is halted */
1486 drain_dac(struct trident_state *state, int nonblock)
1488 DECLARE_WAITQUEUE(wait, current);
1489 struct dmabuf *dmabuf = &state->dmabuf;
1490 unsigned long flags;
1493 unsigned long diff = 0;
1495 if (dmabuf->mapped || !dmabuf->ready)
1498 add_wait_queue(&dmabuf->wait, &wait);
1500 /* It seems that we have to set the current state to TASK_INTERRUPTIBLE
1501 every time to make the process really go to sleep */
1502 set_current_state(TASK_INTERRUPTIBLE);
1504 spin_lock_irqsave(&state->card->lock, flags);
1505 count = dmabuf->count;
1506 spin_unlock_irqrestore(&state->card->lock, flags);
1511 if (signal_pending(current))
1515 remove_wait_queue(&dmabuf->wait, &wait);
1516 set_current_state(TASK_RUNNING);
1520 /* No matter how much data is left in the buffer, we have to wait until
1521 CSO == ESO/2 or CSO == ESO when address engine interrupts */
1522 if (state->card->pci_id == PCI_DEVICE_ID_ALI_5451 ||
1523 state->card->pci_id == PCI_DEVICE_ID_INTERG_5050) {
1524 diff = dmabuf->swptr - trident_get_dma_addr(state) + dmabuf->dmasize;
1525 diff = diff % (dmabuf->dmasize);
1526 tmo = (diff * HZ) / dmabuf->rate;
1528 tmo = (dmabuf->dmasize * HZ) / dmabuf->rate;
1530 tmo >>= sample_shift[dmabuf->fmt];
1531 if (!schedule_timeout(tmo ? tmo : 1) && tmo) {
1535 remove_wait_queue(&dmabuf->wait, &wait);
1536 set_current_state(TASK_RUNNING);
1537 if (signal_pending(current))
1538 return -ERESTARTSYS;
1543 /* update buffer manangement pointers, especially, */
1544 /* dmabuf->count and dmabuf->hwptr */
1546 trident_update_ptr(struct trident_state *state)
1548 struct dmabuf *dmabuf = &state->dmabuf;
1549 unsigned hwptr, swptr;
1552 unsigned char silence;
1553 unsigned half_dmasize;
1555 /* update hardware pointer */
1556 hwptr = trident_get_dma_addr(state);
1557 diff = (dmabuf->dmasize + hwptr - dmabuf->hwptr) % dmabuf->dmasize;
1558 dmabuf->hwptr = hwptr;
1559 dmabuf->total_bytes += diff;
1561 /* error handling and process wake up for ADC */
1562 if (dmabuf->enable == ADC_RUNNING) {
1563 if (dmabuf->mapped) {
1564 dmabuf->count -= diff;
1565 if (dmabuf->count >= (signed) dmabuf->fragsize)
1566 wake_up(&dmabuf->wait);
1568 dmabuf->count += diff;
1570 if (dmabuf->count < 0 ||
1571 dmabuf->count > dmabuf->dmasize) {
1572 /* buffer underrun or buffer overrun, */
1573 /* we have no way to recover it here, just */
1574 /* stop the machine and let the process */
1575 /* force hwptr and swptr to sync */
1579 if (dmabuf->count < (signed) dmabuf->dmasize / 2)
1580 wake_up(&dmabuf->wait);
1584 /* error handling and process wake up for DAC */
1585 if (dmabuf->enable == DAC_RUNNING) {
1586 if (dmabuf->mapped) {
1587 dmabuf->count += diff;
1588 if (dmabuf->count >= (signed) dmabuf->fragsize)
1589 wake_up(&dmabuf->wait);
1591 dmabuf->count -= diff;
1593 if (dmabuf->count < 0 ||
1594 dmabuf->count > dmabuf->dmasize) {
1595 /* buffer underrun or buffer overrun, we have no way to recover
1596 it here, just stop the machine and let the process force hwptr
1597 and swptr to sync */
1600 } else if (!dmabuf->endcleared) {
1601 swptr = dmabuf->swptr;
1602 silence = (dmabuf->fmt & TRIDENT_FMT_16BIT ? 0 : 0x80);
1603 if (dmabuf->update_flag & ALI_ADDRESS_INT_UPDATE) {
1604 /* We must clear end data of 1/2 dmabuf if needed.
1605 According to 1/2 algorithm of Address Engine Interrupt,
1606 check the validation of the data of half dmasize. */
1607 half_dmasize = dmabuf->dmasize / 2;
1608 if ((diff = hwptr - half_dmasize) < 0)
1610 if ((dmabuf->count + diff) < half_dmasize) {
1611 //there is invalid data in the end of half buffer
1612 if ((clear_cnt = half_dmasize - swptr) < 0)
1613 clear_cnt += half_dmasize;
1614 //clear the invalid data
1615 memset(dmabuf->rawbuf + swptr, silence, clear_cnt);
1616 if (state->chans_num == 6) {
1617 clear_cnt = clear_cnt / 2;
1619 memset(state->other_states[0]->dmabuf.rawbuf + swptr,
1620 silence, clear_cnt);
1621 memset(state->other_states[1]->dmabuf.rawbuf + swptr,
1622 silence, clear_cnt);
1623 memset(state->other_states[2]->dmabuf.rawbuf + swptr,
1624 silence, clear_cnt);
1625 memset(state->other_states[3]->dmabuf.rawbuf + swptr,
1626 silence, clear_cnt);
1628 dmabuf->endcleared = 1;
1630 } else if (dmabuf->count < (signed) dmabuf->fragsize) {
1631 clear_cnt = dmabuf->fragsize;
1632 if ((swptr + clear_cnt) > dmabuf->dmasize)
1633 clear_cnt = dmabuf->dmasize - swptr;
1634 memset(dmabuf->rawbuf + swptr, silence, clear_cnt);
1635 if (state->chans_num == 6) {
1636 clear_cnt = clear_cnt / 2;
1638 memset(state->other_states[0]->dmabuf.rawbuf + swptr,
1639 silence, clear_cnt);
1640 memset(state->other_states[1]->dmabuf.rawbuf + swptr,
1641 silence, clear_cnt);
1642 memset(state->other_states[2]->dmabuf.rawbuf + swptr,
1643 silence, clear_cnt);
1644 memset(state->other_states[3]->dmabuf.rawbuf + swptr,
1645 silence, clear_cnt);
1647 dmabuf->endcleared = 1;
1650 /* trident_update_ptr is called by interrupt handler or by process via
1651 ioctl/poll, we only wake up the waiting process when we have more
1652 than 1/2 buffer free (always true for interrupt handler) */
1653 if (dmabuf->count < (signed) dmabuf->dmasize / 2)
1654 wake_up(&dmabuf->wait);
1657 dmabuf->update_flag &= ~ALI_ADDRESS_INT_UPDATE;
1661 trident_address_interrupt(struct trident_card *card)
1664 struct trident_state *state;
1665 unsigned int channel;
1667 /* Update the pointers for all channels we are running. */
1668 /* FIXME: should read interrupt status only once */
1669 for (i = 0; i < NR_HW_CH; i++) {
1671 if (trident_check_channel_interrupt(card, channel)) {
1672 trident_ack_channel_interrupt(card, channel);
1673 if ((state = card->states[i]) != NULL) {
1674 trident_update_ptr(state);
1676 printk(KERN_WARNING "trident: spurious channel "
1677 "irq %d.\n", channel);
1678 trident_stop_voice(card, channel);
1679 trident_disable_voice_irq(card, channel);
1686 ali_hwvol_control(struct trident_card *card, int opt)
1688 u16 dwTemp, volume[2], mute, diff, *pVol[2];
1690 dwTemp = ali_ac97_read(card->ac97_codec[0], 0x02);
1691 mute = dwTemp & 0x8000;
1692 volume[0] = dwTemp & 0x001f;
1693 volume[1] = (dwTemp & 0x1f00) >> 8;
1694 if (volume[0] < volume[1]) {
1695 pVol[0] = &volume[0];
1696 pVol[1] = &volume[1];
1698 pVol[1] = &volume[0];
1699 pVol[0] = &volume[1];
1701 diff = *(pVol[1]) - *(pVol[0]);
1703 if (opt == 1) { // MUTE
1705 ali_ac97_write(card->ac97_codec[0],
1707 } else if (opt == 2) { // Down
1710 if (*(pVol[1]) < 0x001f) {
1712 *(pVol[0]) = *(pVol[1]) - diff;
1715 dwTemp |= (volume[0]) | (volume[1] << 8);
1716 ali_ac97_write(card->ac97_codec[0], 0x02, dwTemp);
1717 card->ac97_codec[0]->mixer_state[0] = ((32 - volume[0]) * 25 / 8) |
1718 (((32 - volume[1]) * 25 / 8) << 8);
1719 } else if (opt == 4) { // Up
1722 if (*(pVol[0]) > 0) {
1724 *(pVol[1]) = *(pVol[0]) + diff;
1727 dwTemp |= (volume[0]) | (volume[1] << 8);
1728 ali_ac97_write(card->ac97_codec[0], 0x02, dwTemp);
1729 card->ac97_codec[0]->mixer_state[0] = ((32 - volume[0]) * 25 / 8) |
1730 (((32 - volume[1]) * 25 / 8) << 8);
1732 /* Nothing needs doing */
1737 * Re-enable reporting of vol change after 0.1 seconds
1741 ali_timeout(unsigned long ptr)
1743 struct trident_card *card = (struct trident_card *) ptr;
1746 /* Enable GPIO IRQ (MISCINT bit 18h) */
1747 temp = inw(TRID_REG(card, T4D_MISCINT + 2));
1749 outw(temp, TRID_REG(card, T4D_MISCINT + 2));
1753 * Set up the timer to clear the vol change notification
1757 ali_set_timer(struct trident_card *card)
1759 /* Add Timer Routine to Enable GPIO IRQ */
1760 del_timer(&card->timer); /* Never queue twice */
1761 card->timer.function = ali_timeout;
1762 card->timer.data = (unsigned long) card;
1763 card->timer.expires = jiffies + HZ / 10;
1764 add_timer(&card->timer);
1768 * Process a GPIO event
1772 ali_queue_task(struct trident_card *card, int opt)
1776 /* Disable GPIO IRQ (MISCINT bit 18h) */
1777 temp = inw(TRID_REG(card, T4D_MISCINT + 2));
1778 temp &= (u16) (~0x0004);
1779 outw(temp, TRID_REG(card, T4D_MISCINT + 2));
1781 /* Adjust the volume */
1782 ali_hwvol_control(card, opt);
1784 /* Set the timer for 1/10th sec */
1785 ali_set_timer(card);
1789 cyber_address_interrupt(struct trident_card *card)
1792 struct trident_state *state;
1793 unsigned int channel;
1795 /* Update the pointers for all channels we are running. */
1796 /* FIXED: read interrupt status only once */
1797 irq_status = inl(TRID_REG(card, T4D_AINT_A));
1799 pr_debug("cyber_address_interrupt: irq_status 0x%X\n", irq_status);
1801 for (i = 0; i < NR_HW_CH; i++) {
1803 if (irq_status & (1 << channel)) {
1804 /* clear bit by writing a 1, zeroes are ignored */
1805 outl((1 << channel), TRID_REG(card, T4D_AINT_A));
1807 pr_debug("cyber_interrupt: channel %d\n", channel);
1809 if ((state = card->states[i]) != NULL) {
1810 trident_update_ptr(state);
1812 printk(KERN_WARNING "cyber5050: spurious "
1813 "channel irq %d.\n", channel);
1814 trident_stop_voice(card, channel);
1815 trident_disable_voice_irq(card, channel);
1822 trident_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1824 struct trident_card *card = (struct trident_card *) dev_id;
1828 spin_lock(&card->lock);
1829 event = inl(TRID_REG(card, T4D_MISCINT));
1831 pr_debug("trident: trident_interrupt called, MISCINT = 0x%08x\n",
1834 if (event & ADDRESS_IRQ) {
1835 card->address_interrupt(card);
1838 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
1839 /* GPIO IRQ (H/W Volume Control) */
1840 event = inl(TRID_REG(card, T4D_MISCINT));
1841 if (event & (1 << 25)) {
1842 gpio = inl(TRID_REG(card, ALI_GPIO));
1843 if (!timer_pending(&card->timer))
1844 ali_queue_task(card, gpio & 0x07);
1846 event = inl(TRID_REG(card, T4D_MISCINT));
1847 outl(event | (ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
1848 TRID_REG(card, T4D_MISCINT));
1849 spin_unlock(&card->lock);
1853 /* manually clear interrupt status, bad hardware design, blame T^2 */
1854 outl((ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
1855 TRID_REG(card, T4D_MISCINT));
1856 spin_unlock(&card->lock);
1860 /* in this loop, dmabuf.count signifies the amount of data that is waiting */
1861 /* to be copied to the user's buffer. it is filled by the dma machine and */
1862 /* drained by this loop. */
1864 trident_read(struct file *file, char *buffer, size_t count, loff_t * ppos)
1866 struct trident_state *state = (struct trident_state *)file->private_data;
1867 struct dmabuf *dmabuf = &state->dmabuf;
1869 unsigned long flags;
1873 pr_debug("trident: trident_read called, count = %d\n", count);
1875 VALIDATE_STATE(state);
1876 if (ppos != &file->f_pos)
1881 if (!access_ok(VERIFY_WRITE, buffer, count))
1885 if (!dmabuf->ready && (ret = prog_dmabuf_record(state)))
1889 spin_lock_irqsave(&state->card->lock, flags);
1890 if (dmabuf->count > (signed) dmabuf->dmasize) {
1891 /* buffer overrun, we are recovering from */
1892 /* sleep_on_timeout, resync hwptr and swptr, */
1893 /* make process flush the buffer */
1894 dmabuf->count = dmabuf->dmasize;
1895 dmabuf->swptr = dmabuf->hwptr;
1897 swptr = dmabuf->swptr;
1898 cnt = dmabuf->dmasize - swptr;
1899 if (dmabuf->count < cnt)
1900 cnt = dmabuf->count;
1901 spin_unlock_irqrestore(&state->card->lock, flags);
1907 /* buffer is empty, start the dma machine and */
1908 /* wait for data to be recorded */
1910 if (file->f_flags & O_NONBLOCK) {
1917 /* No matter how much space left in the buffer, */
1918 /* we have to wait until CSO == ESO/2 or CSO == ESO */
1919 /* when address engine interrupts */
1920 tmo = (dmabuf->dmasize * HZ) / (dmabuf->rate * 2);
1921 tmo >>= sample_shift[dmabuf->fmt];
1922 /* There are two situations when sleep_on_timeout returns, one is when
1923 the interrupt is serviced correctly and the process is waked up by
1924 ISR ON TIME. Another is when timeout is expired, which means that
1925 either interrupt is NOT serviced correctly (pending interrupt) or it
1926 is TOO LATE for the process to be scheduled to run (scheduler latency)
1927 which results in a (potential) buffer overrun. And worse, there is
1928 NOTHING we can do to prevent it. */
1929 if (!interruptible_sleep_on_timeout(&dmabuf->wait, tmo)) {
1930 pr_debug(KERN_ERR "trident: recording schedule timeout, "
1931 "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1932 dmabuf->dmasize, dmabuf->fragsize, dmabuf->count,
1933 dmabuf->hwptr, dmabuf->swptr);
1935 /* a buffer overrun, we delay the recovery until next time the
1936 while loop begin and we REALLY have space to record */
1938 if (signal_pending(current)) {
1944 if (dmabuf->mapped) {
1952 if (copy_to_user(buffer, dmabuf->rawbuf + swptr, cnt)) {
1958 swptr = (swptr + cnt) % dmabuf->dmasize;
1960 spin_lock_irqsave(&state->card->lock, flags);
1961 dmabuf->swptr = swptr;
1962 dmabuf->count -= cnt;
1963 spin_unlock_irqrestore(&state->card->lock, flags);
1975 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
1976 the soundcard. it is drained by the dma machine and filled by this loop. */
1979 trident_write(struct file *file, const char *buffer, size_t count, loff_t * ppos)
1981 struct trident_state *state = (struct trident_state *)file->private_data;
1982 struct dmabuf *dmabuf = &state->dmabuf;
1984 unsigned long flags;
1987 unsigned int state_cnt;
1988 unsigned int copy_count;
1989 int lret; /* for lock_set_fmt */
1991 pr_debug("trident: trident_write called, count = %d\n", count);
1993 VALIDATE_STATE(state);
1994 if (ppos != &file->f_pos)
1998 * Guard against an mmap or ioctl while writing
2003 if (dmabuf->mapped) {
2007 if (!dmabuf->ready && (ret = prog_dmabuf_playback(state)))
2010 if (!access_ok(VERIFY_READ, buffer, count)) {
2018 spin_lock_irqsave(&state->card->lock, flags);
2019 if (dmabuf->count < 0) {
2020 /* buffer underrun, we are recovering from */
2021 /* sleep_on_timeout, resync hwptr and swptr */
2023 dmabuf->swptr = dmabuf->hwptr;
2025 swptr = dmabuf->swptr;
2026 cnt = dmabuf->dmasize - swptr;
2027 if (dmabuf->count + cnt > dmabuf->dmasize)
2028 cnt = dmabuf->dmasize - dmabuf->count;
2029 spin_unlock_irqrestore(&state->card->lock, flags);
2035 /* buffer is full, start the dma machine and */
2036 /* wait for data to be played */
2038 if (file->f_flags & O_NONBLOCK) {
2043 /* No matter how much data left in the buffer, */
2044 /* we have to wait until CSO == ESO/2 or CSO == ESO */
2045 /* when address engine interrupts */
2046 lock_set_fmt(state);
2047 tmo = (dmabuf->dmasize * HZ) / (dmabuf->rate * 2);
2048 tmo >>= sample_shift[dmabuf->fmt];
2049 unlock_set_fmt(state);
2052 /* There are two situations when sleep_on_timeout */
2053 /* returns, one is when the interrupt is serviced */
2054 /* correctly and the process is waked up by ISR */
2055 /* ON TIME. Another is when timeout is expired, which */
2056 /* means that either interrupt is NOT serviced */
2057 /* correctly (pending interrupt) or it is TOO LATE */
2058 /* for the process to be scheduled to run */
2059 /* (scheduler latency) which results in a (potential) */
2060 /* buffer underrun. And worse, there is NOTHING we */
2061 /* can do to prevent it. */
2062 if (!interruptible_sleep_on_timeout(&dmabuf->wait, tmo)) {
2063 pr_debug(KERN_ERR "trident: playback schedule "
2064 "timeout, dmasz %u fragsz %u count %i "
2065 "hwptr %u swptr %u\n", dmabuf->dmasize,
2066 dmabuf->fragsize, dmabuf->count,
2067 dmabuf->hwptr, dmabuf->swptr);
2069 /* a buffer underrun, we delay the recovery */
2070 /* until next time the while loop begin and */
2071 /* we REALLY have data to play */
2073 if (signal_pending(current)) {
2079 if (dmabuf->mapped) {
2086 if ((lret = lock_set_fmt(state)) < 0) {
2091 if (state->chans_num == 6) {
2094 if (ali_write_5_1(state, buffer, cnt, ©_count,
2095 &state_cnt) == -EFAULT) {
2097 swptr = (swptr + state_cnt) % dmabuf->dmasize;
2098 spin_lock_irqsave(&state->card->lock, flags);
2099 dmabuf->swptr = swptr;
2100 dmabuf->count += state_cnt;
2101 dmabuf->endcleared = 0;
2102 spin_unlock_irqrestore(&state->card->lock, flags);
2107 unlock_set_fmt(state);
2111 if (copy_from_user(dmabuf->rawbuf + swptr,
2115 unlock_set_fmt(state);
2120 unlock_set_fmt(state);
2122 swptr = (swptr + state_cnt) % dmabuf->dmasize;
2124 spin_lock_irqsave(&state->card->lock, flags);
2125 dmabuf->swptr = swptr;
2126 dmabuf->count += state_cnt;
2127 dmabuf->endcleared = 0;
2128 spin_unlock_irqrestore(&state->card->lock, flags);
2141 /* No kernel lock - we have our own spinlock */
2143 trident_poll(struct file *file, struct poll_table_struct *wait)
2145 struct trident_state *state = (struct trident_state *)file->private_data;
2146 struct dmabuf *dmabuf = &state->dmabuf;
2147 unsigned long flags;
2148 unsigned int mask = 0;
2150 VALIDATE_STATE(state);
2153 * Guard against a parallel poll and write causing multiple
2154 * prog_dmabuf events
2159 if (file->f_mode & FMODE_WRITE) {
2160 if (!dmabuf->ready && prog_dmabuf_playback(state)) {
2164 poll_wait(file, &dmabuf->wait, wait);
2166 if (file->f_mode & FMODE_READ) {
2167 if (!dmabuf->ready && prog_dmabuf_record(state)) {
2171 poll_wait(file, &dmabuf->wait, wait);
2176 spin_lock_irqsave(&state->card->lock, flags);
2177 trident_update_ptr(state);
2178 if (file->f_mode & FMODE_READ) {
2179 if (dmabuf->count >= (signed) dmabuf->fragsize)
2180 mask |= POLLIN | POLLRDNORM;
2182 if (file->f_mode & FMODE_WRITE) {
2183 if (dmabuf->mapped) {
2184 if (dmabuf->count >= (signed) dmabuf->fragsize)
2185 mask |= POLLOUT | POLLWRNORM;
2187 if ((signed) dmabuf->dmasize >= dmabuf->count +
2188 (signed) dmabuf->fragsize)
2189 mask |= POLLOUT | POLLWRNORM;
2192 spin_unlock_irqrestore(&state->card->lock, flags);
2198 trident_mmap(struct file *file, struct vm_area_struct *vma)
2200 struct trident_state *state = (struct trident_state *)file->private_data;
2201 struct dmabuf *dmabuf = &state->dmabuf;
2205 VALIDATE_STATE(state);
2208 * Lock against poll read write or mmap creating buffers. Also lock
2209 * a read or write against an mmap.
2214 if (vma->vm_flags & VM_WRITE) {
2215 if ((ret = prog_dmabuf_playback(state)) != 0)
2217 } else if (vma->vm_flags & VM_READ) {
2218 if ((ret = prog_dmabuf_record(state)) != 0)
2224 if (vma->vm_pgoff != 0)
2226 size = vma->vm_end - vma->vm_start;
2227 if (size > (PAGE_SIZE << dmabuf->buforder))
2230 if (remap_page_range(vma, vma->vm_start, virt_to_phys(dmabuf->rawbuf),
2231 size, vma->vm_page_prot))
2241 trident_ioctl(struct inode *inode, struct file *file,
2242 unsigned int cmd, unsigned long arg)
2244 struct trident_state *state = (struct trident_state *)file->private_data;
2245 struct dmabuf *dmabuf = &state->dmabuf;
2246 unsigned long flags;
2247 audio_buf_info abinfo;
2249 int val, mapped, ret = 0;
2251 struct trident_card *card = state->card;
2253 VALIDATE_STATE(state);
2256 mapped = ((file->f_mode & (FMODE_WRITE | FMODE_READ)) && dmabuf->mapped);
2258 pr_debug("trident: trident_ioctl, command = %2d, arg = 0x%08x\n",
2259 _IOC_NR(cmd), arg ? *(int *) arg : 0);
2262 case OSS_GETVERSION:
2263 ret = put_user(SOUND_VERSION, (int *) arg);
2266 case SNDCTL_DSP_RESET:
2267 /* FIXME: spin_lock ? */
2268 if (file->f_mode & FMODE_WRITE) {
2270 synchronize_irq(card->irq);
2272 dmabuf->swptr = dmabuf->hwptr = 0;
2273 dmabuf->count = dmabuf->total_bytes = 0;
2275 if (file->f_mode & FMODE_READ) {
2277 synchronize_irq(card->irq);
2279 dmabuf->swptr = dmabuf->hwptr = 0;
2280 dmabuf->count = dmabuf->total_bytes = 0;
2284 case SNDCTL_DSP_SYNC:
2285 if (file->f_mode & FMODE_WRITE)
2286 ret = drain_dac(state, file->f_flags & O_NONBLOCK);
2289 case SNDCTL_DSP_SPEED: /* set smaple rate */
2290 if (get_user(val, (int *) arg)) {
2295 if (file->f_mode & FMODE_WRITE) {
2298 spin_lock_irqsave(&state->card->lock, flags);
2299 trident_set_dac_rate(state, val);
2300 spin_unlock_irqrestore(&state->card->lock, flags);
2302 if (file->f_mode & FMODE_READ) {
2305 spin_lock_irqsave(&state->card->lock, flags);
2306 trident_set_adc_rate(state, val);
2307 spin_unlock_irqrestore(&state->card->lock, flags);
2310 ret = put_user(dmabuf->rate, (int *) arg);
2313 case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
2314 if (get_user(val, (int *) arg)) {
2318 if ((ret = lock_set_fmt(state)) < 0)
2321 if (file->f_mode & FMODE_WRITE) {
2325 dmabuf->fmt |= TRIDENT_FMT_STEREO;
2327 dmabuf->fmt &= ~TRIDENT_FMT_STEREO;
2329 if (file->f_mode & FMODE_READ) {
2333 dmabuf->fmt |= TRIDENT_FMT_STEREO;
2335 dmabuf->fmt &= ~TRIDENT_FMT_STEREO;
2337 unlock_set_fmt(state);
2340 case SNDCTL_DSP_GETBLKSIZE:
2341 if (file->f_mode & FMODE_WRITE) {
2342 if ((val = prog_dmabuf_playback(state)))
2345 ret = put_user(dmabuf->fragsize, (int *) arg);
2348 if (file->f_mode & FMODE_READ) {
2349 if ((val = prog_dmabuf_record(state)))
2352 ret = put_user(dmabuf->fragsize, (int *) arg);
2355 /* neither READ nor WRITE? is this even possible? */
2360 case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format */
2361 ret = put_user(AFMT_S16_LE | AFMT_U16_LE | AFMT_S8 |
2362 AFMT_U8, (int *) arg);
2365 case SNDCTL_DSP_SETFMT: /* Select sample format */
2366 if (get_user(val, (int *) arg)) {
2370 if ((ret = lock_set_fmt(state)) < 0)
2373 if (val != AFMT_QUERY) {
2374 if (file->f_mode & FMODE_WRITE) {
2377 if (val == AFMT_S16_LE)
2378 dmabuf->fmt |= TRIDENT_FMT_16BIT;
2380 dmabuf->fmt &= ~TRIDENT_FMT_16BIT;
2382 if (file->f_mode & FMODE_READ) {
2385 if (val == AFMT_S16_LE)
2386 dmabuf->fmt |= TRIDENT_FMT_16BIT;
2388 dmabuf->fmt &= ~TRIDENT_FMT_16BIT;
2391 unlock_set_fmt(state);
2392 ret = put_user((dmabuf->fmt & TRIDENT_FMT_16BIT) ? AFMT_S16_LE :
2393 AFMT_U8, (int *) arg);
2396 case SNDCTL_DSP_CHANNELS:
2397 if (get_user(val, (int *) arg)) {
2402 if ((ret = lock_set_fmt(state)) < 0)
2405 if (file->f_mode & FMODE_WRITE) {
2409 //prevent from memory leak
2410 if ((state->chans_num > 2) && (state->chans_num != val)) {
2411 ali_free_other_states_resources(state);
2412 state->chans_num = 1;
2417 dmabuf->fmt |= TRIDENT_FMT_STEREO;
2418 if ((val == 6) && (state->card->pci_id == PCI_DEVICE_ID_ALI_5451)) {
2419 if (card->rec_channel_use_count > 0) {
2420 printk(KERN_ERR "trident: Record is "
2421 "working on the card!\n");
2423 unlock_set_fmt(state);
2427 ret = ali_setup_multi_channels(state->card, 6);
2429 unlock_set_fmt(state);
2432 down(&state->card->open_sem);
2433 ret = ali_allocate_other_states_resources(state, 6);
2435 up(&state->card->open_sem);
2436 unlock_set_fmt(state);
2439 state->card->multi_channel_use_count++;
2440 up(&state->card->open_sem);
2442 val = 2; /*yield to 2-channels */
2444 dmabuf->fmt &= ~TRIDENT_FMT_STEREO;
2445 state->chans_num = val;
2447 if (file->f_mode & FMODE_READ) {
2451 if (!((file->f_mode & FMODE_WRITE) &&
2454 dmabuf->fmt |= TRIDENT_FMT_STEREO;
2456 dmabuf->fmt &= ~TRIDENT_FMT_STEREO;
2457 state->chans_num = val;
2459 unlock_set_fmt(state);
2461 ret = put_user(val, (int *) arg);
2464 case SNDCTL_DSP_POST:
2465 /* Cause the working fragment to be output */
2468 case SNDCTL_DSP_SUBDIVIDE:
2469 if (dmabuf->subdivision) {
2473 if (get_user(val, (int *) arg)) {
2477 if (val != 1 && val != 2 && val != 4) {
2481 dmabuf->subdivision = val;
2484 case SNDCTL_DSP_SETFRAGMENT:
2485 if (get_user(val, (int *) arg)) {
2490 dmabuf->ossfragshift = val & 0xffff;
2491 dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2492 if (dmabuf->ossfragshift < 4)
2493 dmabuf->ossfragshift = 4;
2494 if (dmabuf->ossfragshift > 15)
2495 dmabuf->ossfragshift = 15;
2496 if (dmabuf->ossmaxfrags < 4)
2497 dmabuf->ossmaxfrags = 4;
2501 case SNDCTL_DSP_GETOSPACE:
2502 if (!(file->f_mode & FMODE_WRITE)) {
2506 if (!dmabuf->ready && (val = prog_dmabuf_playback(state)) != 0) {
2510 spin_lock_irqsave(&state->card->lock, flags);
2511 trident_update_ptr(state);
2512 abinfo.fragsize = dmabuf->fragsize;
2513 abinfo.bytes = dmabuf->dmasize - dmabuf->count;
2514 abinfo.fragstotal = dmabuf->numfrag;
2515 abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
2516 spin_unlock_irqrestore(&state->card->lock, flags);
2517 ret = copy_to_user((void *) arg, &abinfo, sizeof (abinfo)) ?
2521 case SNDCTL_DSP_GETISPACE:
2522 if (!(file->f_mode & FMODE_READ)) {
2526 if (!dmabuf->ready && (val = prog_dmabuf_record(state)) != 0) {
2530 spin_lock_irqsave(&state->card->lock, flags);
2531 trident_update_ptr(state);
2532 abinfo.fragsize = dmabuf->fragsize;
2533 abinfo.bytes = dmabuf->count;
2534 abinfo.fragstotal = dmabuf->numfrag;
2535 abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
2536 spin_unlock_irqrestore(&state->card->lock, flags);
2537 ret = copy_to_user((void *) arg, &abinfo, sizeof (abinfo)) ?
2541 case SNDCTL_DSP_NONBLOCK:
2542 file->f_flags |= O_NONBLOCK;
2545 case SNDCTL_DSP_GETCAPS:
2546 ret = put_user(DSP_CAP_REALTIME | DSP_CAP_TRIGGER |
2547 DSP_CAP_MMAP | DSP_CAP_BIND, (int *) arg);
2550 case SNDCTL_DSP_GETTRIGGER:
2552 if ((file->f_mode & FMODE_READ) && dmabuf->enable)
2553 val |= PCM_ENABLE_INPUT;
2554 if ((file->f_mode & FMODE_WRITE) && dmabuf->enable)
2555 val |= PCM_ENABLE_OUTPUT;
2556 ret = put_user(val, (int *) arg);
2559 case SNDCTL_DSP_SETTRIGGER:
2560 if (get_user(val, (int *) arg)) {
2564 if (file->f_mode & FMODE_READ) {
2565 if (val & PCM_ENABLE_INPUT) {
2566 if (!dmabuf->ready &&
2567 (ret = prog_dmabuf_record(state)))
2573 if (file->f_mode & FMODE_WRITE) {
2574 if (val & PCM_ENABLE_OUTPUT) {
2575 if (!dmabuf->ready &&
2576 (ret = prog_dmabuf_playback(state)))
2584 case SNDCTL_DSP_GETIPTR:
2585 if (!(file->f_mode & FMODE_READ)) {
2589 if (!dmabuf->ready && (val = prog_dmabuf_record(state))
2594 spin_lock_irqsave(&state->card->lock, flags);
2595 trident_update_ptr(state);
2596 cinfo.bytes = dmabuf->total_bytes;
2597 cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
2598 cinfo.ptr = dmabuf->hwptr;
2600 dmabuf->count &= dmabuf->fragsize - 1;
2601 spin_unlock_irqrestore(&state->card->lock, flags);
2602 ret = copy_to_user((void *) arg, &cinfo, sizeof (cinfo)) ?
2606 case SNDCTL_DSP_GETOPTR:
2607 if (!(file->f_mode & FMODE_WRITE)) {
2611 if (!dmabuf->ready && (val = prog_dmabuf_playback(state))
2617 spin_lock_irqsave(&state->card->lock, flags);
2618 trident_update_ptr(state);
2619 cinfo.bytes = dmabuf->total_bytes;
2620 cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
2621 cinfo.ptr = dmabuf->hwptr;
2623 dmabuf->count &= dmabuf->fragsize - 1;
2624 spin_unlock_irqrestore(&state->card->lock, flags);
2625 ret = copy_to_user((void *) arg, &cinfo, sizeof (cinfo)) ?
2629 case SNDCTL_DSP_SETDUPLEX:
2633 case SNDCTL_DSP_GETODELAY:
2634 if (!(file->f_mode & FMODE_WRITE)) {
2638 if (!dmabuf->ready && (val = prog_dmabuf_playback(state)) != 0) {
2642 spin_lock_irqsave(&state->card->lock, flags);
2643 trident_update_ptr(state);
2644 val = dmabuf->count;
2645 spin_unlock_irqrestore(&state->card->lock, flags);
2646 ret = put_user(val, (int *) arg);
2649 case SOUND_PCM_READ_RATE:
2650 ret = put_user(dmabuf->rate, (int *) arg);
2653 case SOUND_PCM_READ_CHANNELS:
2654 ret = put_user((dmabuf->fmt & TRIDENT_FMT_STEREO) ? 2 : 1,
2658 case SOUND_PCM_READ_BITS:
2659 ret = put_user((dmabuf->fmt & TRIDENT_FMT_16BIT) ? AFMT_S16_LE :
2660 AFMT_U8, (int *) arg);
2663 case SNDCTL_DSP_GETCHANNELMASK:
2664 ret = put_user(DSP_BIND_FRONT | DSP_BIND_SURR |
2665 DSP_BIND_CENTER_LFE, (int *) arg);
2668 case SNDCTL_DSP_BIND_CHANNEL:
2669 if (state->card->pci_id != PCI_DEVICE_ID_SI_7018) {
2674 if (get_user(val, (int *) arg)) {
2678 if (val == DSP_BIND_QUERY) {
2679 val = dmabuf->channel->attribute | 0x3c00;
2680 val = attr2mask[val >> 8];
2683 if (file->f_mode & FMODE_READ)
2684 dmabuf->channel->attribute = (CHANNEL_REC |
2686 if (file->f_mode & FMODE_WRITE)
2687 dmabuf->channel->attribute = (CHANNEL_SPC_PB |
2689 dmabuf->channel->attribute |= mask2attr[ffs(val)];
2691 ret = put_user(val, (int *) arg);
2694 case SNDCTL_DSP_MAPINBUF:
2695 case SNDCTL_DSP_MAPOUTBUF:
2696 case SNDCTL_DSP_SETSYNCRO:
2697 case SOUND_PCM_WRITE_FILTER:
2698 case SOUND_PCM_READ_FILTER:
2708 trident_open(struct inode *inode, struct file *file)
2711 int minor = iminor(inode);
2712 struct trident_card *card = devs;
2713 struct trident_state *state = NULL;
2714 struct dmabuf *dmabuf = NULL;
2716 /* Added by Matt Wu 01-05-2001 */
2717 /* TODO: there's some redundacy here wrt the check below */
2718 /* for multi_use_count > 0. Should we return -EBUSY or find */
2719 /* a different card? for now, don't break current behaviour */
2721 if (file->f_mode & FMODE_READ) {
2722 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
2723 if (card->multi_channel_use_count > 0)
2728 /* find an available virtual channel (instance of /dev/dsp) */
2729 while (card != NULL) {
2730 down(&card->open_sem);
2731 if (file->f_mode & FMODE_READ) {
2732 /* Skip opens on cards that are in 6 channel mode */
2733 if (card->multi_channel_use_count > 0) {
2734 up(&card->open_sem);
2739 for (i = 0; i < NR_HW_CH; i++) {
2740 if (card->states[i] == NULL) {
2741 state = card->states[i] = kmalloc(sizeof(*state), GFP_KERNEL);
2742 if (state == NULL) {
2743 up(&card->open_sem);
2746 memset(state, 0, sizeof(*state));
2747 init_MUTEX(&state->sem);
2748 dmabuf = &state->dmabuf;
2752 up(&card->open_sem);
2755 /* no more virtual channel avaiable */
2760 /* found a free virtual channel, allocate hardware channels */
2761 if (file->f_mode & FMODE_READ)
2762 dmabuf->channel = card->alloc_rec_pcm_channel(card);
2764 dmabuf->channel = card->alloc_pcm_channel(card);
2766 if (dmabuf->channel == NULL) {
2767 kfree(card->states[i]);
2768 card->states[i] = NULL;
2772 /* initialize the virtual channel */
2775 state->magic = TRIDENT_STATE_MAGIC;
2776 init_waitqueue_head(&dmabuf->wait);
2777 file->private_data = state;
2779 /* set default sample format. According to OSS Programmer's */
2780 /* Guide /dev/dsp should be default to unsigned 8-bits, mono, */
2781 /* with sample rate 8kHz and /dev/dspW will accept 16-bits sample */
2782 if (file->f_mode & FMODE_WRITE) {
2783 dmabuf->fmt &= ~TRIDENT_FMT_MASK;
2784 if ((minor & 0x0f) == SND_DEV_DSP16)
2785 dmabuf->fmt |= TRIDENT_FMT_16BIT;
2786 dmabuf->ossfragshift = 0;
2787 dmabuf->ossmaxfrags = 0;
2788 dmabuf->subdivision = 0;
2789 if (card->pci_id == PCI_DEVICE_ID_SI_7018) {
2790 /* set default channel attribute to normal playback */
2791 dmabuf->channel->attribute = CHANNEL_PB;
2793 trident_set_dac_rate(state, 8000);
2796 if (file->f_mode & FMODE_READ) {
2797 /* FIXME: Trident 4d can only record in signed 16-bits stereo, */
2798 /* 48kHz sample, to be dealed with in trident_set_adc_rate() ?? */
2799 dmabuf->fmt &= ~TRIDENT_FMT_MASK;
2800 if ((minor & 0x0f) == SND_DEV_DSP16)
2801 dmabuf->fmt |= TRIDENT_FMT_16BIT;
2802 dmabuf->ossfragshift = 0;
2803 dmabuf->ossmaxfrags = 0;
2804 dmabuf->subdivision = 0;
2805 if (card->pci_id == PCI_DEVICE_ID_SI_7018) {
2806 /* set default channel attribute to 0x8a80, record from
2807 PCM L/R FIFO and mono = (left + right + 1)/2 */
2808 dmabuf->channel->attribute = (CHANNEL_REC | PCM_LR |
2811 trident_set_adc_rate(state, 8000);
2813 /* Added by Matt Wu 01-05-2001 */
2814 if (card->pci_id == PCI_DEVICE_ID_ALI_5451)
2815 card->rec_channel_use_count++;
2818 state->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
2819 up(&card->open_sem);
2821 pr_debug("trident: open virtual channel %d, hard channel %d\n",
2822 state->virt, dmabuf->channel->num);
2828 trident_release(struct inode *inode, struct file *file)
2830 struct trident_state *state = (struct trident_state *)file->private_data;
2831 struct trident_card *card;
2832 struct dmabuf *dmabuf;
2834 VALIDATE_STATE(state);
2837 dmabuf = &state->dmabuf;
2839 if (file->f_mode & FMODE_WRITE) {
2840 trident_clear_tail(state);
2841 drain_dac(state, file->f_flags & O_NONBLOCK);
2844 pr_debug("trident: closing virtual channel %d, hard channel %d\n",
2845 state->virt, dmabuf->channel->num);
2847 /* stop DMA state machine and free DMA buffers/channels */
2848 down(&card->open_sem);
2850 if (file->f_mode & FMODE_WRITE) {
2852 dealloc_dmabuf(&state->dmabuf, state->card->pci_dev);
2853 state->card->free_pcm_channel(state->card, dmabuf->channel->num);
2855 /* Added by Matt Wu */
2856 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
2857 if (state->chans_num > 2) {
2858 if (card->multi_channel_use_count-- < 0)
2859 card->multi_channel_use_count = 0;
2860 if (card->multi_channel_use_count == 0)
2861 ali_close_multi_channels();
2862 ali_free_other_states_resources(state);
2866 if (file->f_mode & FMODE_READ) {
2868 dealloc_dmabuf(&state->dmabuf, state->card->pci_dev);
2869 state->card->free_pcm_channel(state->card, dmabuf->channel->num);
2871 /* Added by Matt Wu */
2872 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
2873 if (card->rec_channel_use_count-- < 0)
2874 card->rec_channel_use_count = 0;
2878 card->states[state->virt] = NULL;
2881 /* we're covered by the open_sem */
2882 up(&card->open_sem);
2887 static /*const */ struct file_operations trident_audio_fops = {
2888 .owner = THIS_MODULE,
2889 .llseek = no_llseek,
2890 .read = trident_read,
2891 .write = trident_write,
2892 .poll = trident_poll,
2893 .ioctl = trident_ioctl,
2894 .mmap = trident_mmap,
2895 .open = trident_open,
2896 .release = trident_release,
2899 /* trident specific AC97 functions */
2900 /* Write AC97 codec registers */
2902 trident_ac97_set(struct ac97_codec *codec, u8 reg, u16 val)
2904 struct trident_card *card = (struct trident_card *)codec->private_data;
2905 unsigned int address, mask, busy;
2906 unsigned short count = 0xffff;
2907 unsigned long flags;
2910 data = ((u32) val) << 16;
2912 switch (card->pci_id) {
2914 case PCI_DEVICE_ID_SI_7018:
2915 address = SI_AC97_WRITE;
2916 mask = SI_AC97_BUSY_WRITE | SI_AC97_AUDIO_BUSY;
2918 mask |= SI_AC97_SECONDARY;
2919 busy = SI_AC97_BUSY_WRITE;
2921 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX:
2922 address = DX_ACR0_AC97_W;
2923 mask = busy = DX_AC97_BUSY_WRITE;
2925 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX:
2926 address = NX_ACR1_AC97_W;
2927 mask = NX_AC97_BUSY_WRITE;
2929 mask |= NX_AC97_WRITE_SECONDARY;
2930 busy = NX_AC97_BUSY_WRITE;
2932 case PCI_DEVICE_ID_INTERG_5050:
2933 address = SI_AC97_WRITE;
2934 mask = busy = SI_AC97_BUSY_WRITE;
2936 mask |= SI_AC97_SECONDARY;
2940 spin_lock_irqsave(&card->lock, flags);
2942 if ((inw(TRID_REG(card, address)) & busy) == 0)
2946 data |= (mask | (reg & AC97_REG_ADDR));
2949 printk(KERN_ERR "trident: AC97 CODEC write timed out.\n");
2950 spin_unlock_irqrestore(&card->lock, flags);
2954 outl(data, TRID_REG(card, address));
2955 spin_unlock_irqrestore(&card->lock, flags);
2958 /* Read AC97 codec registers */
2960 trident_ac97_get(struct ac97_codec *codec, u8 reg)
2962 struct trident_card *card = (struct trident_card *)codec->private_data;
2963 unsigned int address, mask, busy;
2964 unsigned short count = 0xffff;
2965 unsigned long flags;
2968 switch (card->pci_id) {
2970 case PCI_DEVICE_ID_SI_7018:
2971 address = SI_AC97_READ;
2972 mask = SI_AC97_BUSY_READ | SI_AC97_AUDIO_BUSY;
2974 mask |= SI_AC97_SECONDARY;
2975 busy = SI_AC97_BUSY_READ;
2977 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX:
2978 address = DX_ACR1_AC97_R;
2979 mask = busy = DX_AC97_BUSY_READ;
2981 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX:
2983 address = NX_ACR3_AC97_R_SECONDARY;
2985 address = NX_ACR2_AC97_R_PRIMARY;
2986 mask = NX_AC97_BUSY_READ;
2987 busy = NX_AC97_BUSY_READ | NX_AC97_BUSY_DATA;
2989 case PCI_DEVICE_ID_INTERG_5050:
2990 address = SI_AC97_READ;
2991 mask = busy = SI_AC97_BUSY_READ;
2993 mask |= SI_AC97_SECONDARY;
2997 data = (mask | (reg & AC97_REG_ADDR));
2999 spin_lock_irqsave(&card->lock, flags);
3000 outl(data, TRID_REG(card, address));
3002 data = inl(TRID_REG(card, address));
3003 if ((data & busy) == 0)
3006 spin_unlock_irqrestore(&card->lock, flags);
3009 printk(KERN_ERR "trident: AC97 CODEC read timed out.\n");
3012 return ((u16) (data >> 16));
3015 /* rewrite ac97 read and write mixer register by hulei for ALI*/
3017 acquirecodecaccess(struct trident_card *card)
3019 u16 wsemamask = 0x6000; /* bit 14..13 */
3025 wcontrol = inw(TRID_REG(card, ALI_AC97_WRITE));
3026 wsemabits = wcontrol & wsemamask;
3028 if (wsemabits == 0x4000)
3029 return 1; /* 0x4000 is audio ,then success */
3032 if (wsemabits == 0) {
3034 outl(((u32) (wcontrol & 0x1eff) | 0x00004000),
3035 TRID_REG(card, ALI_AC97_WRITE));
3041 pr_debug("accesscodecsemaphore: try unlock\n");
3049 releasecodecaccess(struct trident_card *card)
3051 unsigned long wcontrol;
3052 wcontrol = inl(TRID_REG(card, ALI_AC97_WRITE));
3053 outl((wcontrol & 0xffff1eff), TRID_REG(card, ALI_AC97_WRITE));
3057 waitforstimertick(struct trident_card *card)
3059 unsigned long chk1, chk2;
3060 unsigned int wcount = 0xffff;
3061 chk1 = inl(TRID_REG(card, ALI_STIMER));
3064 chk2 = inl(TRID_REG(card, ALI_STIMER));
3065 if ((wcount > 0) && chk1 != chk2)
3074 /* Read AC97 codec registers for ALi*/
3076 ali_ac97_get(struct trident_card *card, int secondary, u8 reg)
3078 unsigned int address, mask;
3079 unsigned int ncount;
3080 unsigned long aud_reg;
3083 unsigned long flags;
3088 address = ALI_AC97_READ;
3089 if (card->revision == ALI_5451_V02) {
3090 address = ALI_AC97_WRITE;
3092 mask = ALI_AC97_READ_ACTION | ALI_AC97_AUDIO_BUSY;
3094 mask |= ALI_AC97_SECONDARY;
3096 spin_lock_irqsave(&card->lock, flags);
3098 if (!acquirecodecaccess(card))
3099 printk(KERN_ERR "access codec fail\n");
3101 wcontrol = inw(TRID_REG(card, ALI_AC97_WRITE));
3103 wcontrol |= (0x8000 | reg);
3104 outw(wcontrol, TRID_REG(card, ALI_AC97_WRITE));
3106 data = (mask | (reg & AC97_REG_ADDR));
3108 if (!waitforstimertick(card)) {
3109 printk(KERN_ERR "ali_ac97_read: BIT_CLOCK is dead\n");
3118 if ((inw(TRID_REG(card, ALI_AC97_WRITE)) & ALI_AC97_BUSY_READ)
3123 if (ncount-- == 1) {
3124 pr_debug("ali_ac97_read :try clear busy flag\n");
3125 aud_reg = inl(TRID_REG(card, ALI_AC97_WRITE));
3126 outl((aud_reg & 0xffff7fff),
3127 TRID_REG(card, ALI_AC97_WRITE));
3132 data = inl(TRID_REG(card, address));
3134 spin_unlock_irqrestore(&card->lock, flags);
3136 return ((u16) (data >> 16));
3139 releasecodecaccess(card);
3140 spin_unlock_irqrestore(&card->lock, flags);
3141 printk(KERN_ERR "ali_ac97_read: AC97 CODEC read timed out.\n");
3145 /* Write AC97 codec registers for hulei*/
3147 ali_ac97_set(struct trident_card *card, int secondary, u8 reg, u16 val)
3149 unsigned int address, mask;
3150 unsigned int ncount;
3153 unsigned long flags;
3155 data = ((u32) val) << 16;
3160 address = ALI_AC97_WRITE;
3161 mask = ALI_AC97_WRITE_ACTION | ALI_AC97_AUDIO_BUSY;
3163 mask |= ALI_AC97_SECONDARY;
3164 if (card->revision == ALI_5451_V02)
3165 mask |= ALI_AC97_WRITE_MIXER_REGISTER;
3167 spin_lock_irqsave(&card->lock, flags);
3168 if (!acquirecodecaccess(card))
3169 printk(KERN_ERR "ali_ac97_write: access codec fail\n");
3171 wcontrol = inw(TRID_REG(card, ALI_AC97_WRITE));
3173 wcontrol |= (0x8100 | reg); /* bit 8=1: (ali1535 )reserved/ */
3174 /* ali1535+ write */
3175 outl((data | wcontrol), TRID_REG(card, ALI_AC97_WRITE));
3177 if (!waitforstimertick(card)) {
3178 printk(KERN_ERR "BIT_CLOCK is dead\n");
3184 wcontrol = inw(TRID_REG(card, ALI_AC97_WRITE));
3185 if (!(wcontrol & 0x8000))
3189 if (ncount-- == 1) {
3190 pr_debug("ali_ac97_set :try clear busy flag!!\n");
3191 outw(wcontrol & 0x7fff,
3192 TRID_REG(card, ALI_AC97_WRITE));
3198 releasecodecaccess(card);
3199 spin_unlock_irqrestore(&card->lock, flags);
3204 ali_enable_special_channel(struct trident_state *stat)
3206 struct trident_card *card = stat->card;
3207 unsigned long s_channels;
3209 s_channels = inl(TRID_REG(card, ALI_GLOBAL_CONTROL));
3210 s_channels |= (1 << stat->dmabuf.channel->num);
3211 outl(s_channels, TRID_REG(card, ALI_GLOBAL_CONTROL));
3215 ali_ac97_read(struct ac97_codec *codec, u8 reg)
3219 struct trident_card *card = NULL;
3221 /* Added by Matt Wu */
3225 card = (struct trident_card *) codec->private_data;
3227 if (!card->mixer_regs_ready)
3228 return ali_ac97_get(card, codec->id, reg);
3231 * FIXME: need to stop this caching some registers
3238 data = card->mixer_regs[reg / 2][id];
3243 ali_ac97_write(struct ac97_codec *codec, u8 reg, u16 val)
3246 struct trident_card *card;
3248 /* Added by Matt Wu */
3252 card = (struct trident_card *) codec->private_data;
3254 if (!card->mixer_regs_ready) {
3255 ali_ac97_set(card, codec->id, reg, val);
3264 card->mixer_regs[reg / 2][id] = val;
3265 ali_ac97_set(card, codec->id, reg, val);
3269 flag: ALI_SPDIF_OUT_TO_SPDIF_OUT
3270 ALI_PCM_TO_SPDIF_OUT
3274 ali_setup_spdif_out(struct trident_card *card, int flag)
3276 unsigned long spdif;
3280 struct pci_dev *pci_dev = NULL;
3282 pci_dev = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533,
3284 if (pci_dev == NULL)
3286 pci_read_config_byte(pci_dev, 0x61, &temp);
3288 pci_write_config_byte(pci_dev, 0x61, temp);
3289 pci_read_config_byte(pci_dev, 0x7d, &temp);
3291 pci_write_config_byte(pci_dev, 0x7d, temp);
3292 pci_read_config_byte(pci_dev, 0x7e, &temp);
3295 pci_write_config_byte(pci_dev, 0x7e, temp);
3297 ch = inb(TRID_REG(card, ALI_SCTRL));
3298 outb(ch | ALI_SPDIF_OUT_ENABLE, TRID_REG(card, ALI_SCTRL));
3299 ch = inb(TRID_REG(card, ALI_SPDIF_CTRL));
3300 outb(ch & ALI_SPDIF_OUT_CH_STATUS, TRID_REG(card, ALI_SPDIF_CTRL));
3302 if (flag & ALI_SPDIF_OUT_TO_SPDIF_OUT) {
3303 spdif = inw(TRID_REG(card, ALI_GLOBAL_CONTROL));
3304 spdif |= ALI_SPDIF_OUT_CH_ENABLE;
3305 spdif &= ALI_SPDIF_OUT_SEL_SPDIF;
3306 outw(spdif, TRID_REG(card, ALI_GLOBAL_CONTROL));
3307 spdif = inw(TRID_REG(card, ALI_SPDIF_CS));
3308 if (flag & ALI_SPDIF_OUT_NON_PCM)
3312 outw(spdif, TRID_REG(card, ALI_SPDIF_CS));
3314 spdif = inw(TRID_REG(card, ALI_GLOBAL_CONTROL));
3315 spdif |= ALI_SPDIF_OUT_SEL_PCM;
3316 outw(spdif, TRID_REG(card, ALI_GLOBAL_CONTROL));
3321 ali_disable_special_channel(struct trident_card *card, int ch)
3325 sc = inl(TRID_REG(card, ALI_GLOBAL_CONTROL));
3327 outl(sc, TRID_REG(card, ALI_GLOBAL_CONTROL));
3331 ali_disable_spdif_in(struct trident_card *card)
3333 unsigned long spdif;
3335 spdif = inl(TRID_REG(card, ALI_GLOBAL_CONTROL));
3336 spdif &= (~ALI_SPDIF_IN_SUPPORT);
3337 outl(spdif, TRID_REG(card, ALI_GLOBAL_CONTROL));
3339 ali_disable_special_channel(card, ALI_SPDIF_IN_CHANNEL);
3343 ali_setup_spdif_in(struct trident_card *card)
3345 unsigned long spdif;
3347 //Set SPDIF IN Supported
3348 spdif = inl(TRID_REG(card, ALI_GLOBAL_CONTROL));
3349 spdif |= ALI_SPDIF_IN_SUPPORT;
3350 outl(spdif, TRID_REG(card, ALI_GLOBAL_CONTROL));
3353 spdif = inl(TRID_REG(card, ALI_GLOBAL_CONTROL));
3354 spdif |= ALI_SPDIF_IN_CH_ENABLE;
3355 outl(spdif, TRID_REG(card, ALI_GLOBAL_CONTROL));
3357 spdif = inb(TRID_REG(card, ALI_SPDIF_CTRL));
3358 spdif |= ALI_SPDIF_IN_CH_STATUS;
3359 outb(spdif, TRID_REG(card, ALI_SPDIF_CTRL));
3361 spdif = inb(TRID_REG(card, ALI_SPDIF_CTRL));
3362 spdif |= ALI_SPDIF_IN_FUNC_ENABLE;
3363 outb(spdif, TRID_REG(card, ALI_SPDIF_CTRL));
3368 ali_delay(struct trident_card *card, int interval)
3370 unsigned long begintimer, currenttimer;
3372 begintimer = inl(TRID_REG(card, ALI_STIMER));
3373 currenttimer = inl(TRID_REG(card, ALI_STIMER));
3375 while (currenttimer < begintimer + interval)
3376 currenttimer = inl(TRID_REG(card, ALI_STIMER));
3380 ali_detect_spdif_rate(struct trident_card *card)
3384 u8 bval = 0, R1 = 0, R2 = 0;
3386 bval = inb(TRID_REG(card, ALI_SPDIF_CTRL));
3388 outb(bval, TRID_REG(card, ALI_SPDIF_CTRL));
3390 bval = inb(TRID_REG(card, ALI_SPDIF_CTRL + 1));
3392 outb(bval, TRID_REG(card, ALI_SPDIF_CTRL + 1));
3394 while (((R1 < 0x0B) || (R1 > 0x0E)) && (R1 != 0x12) &&
3400 bval = inb(TRID_REG(card, ALI_SPDIF_CTRL + 1));
3404 if (count > 50000) {
3405 printk(KERN_WARNING "trident: Error in "
3406 "ali_detect_spdif_rate!\n");
3412 while (count <= 50000) {
3417 bval = inb(TRID_REG(card, ALI_SPDIF_CTRL + 1));
3426 if (count > 50000) {
3427 printk(KERN_WARNING "trident: Error in "
3428 "ali_detect_spdif_rate!\n");
3437 wval = inw(TRID_REG(card, ALI_SPDIF_CTRL + 2));
3439 wval |= (u16) 0x09 << 8 | (u16) 0x05;
3440 outw(wval, TRID_REG(card, ALI_SPDIF_CTRL + 2));
3442 bval = inb(TRID_REG(card, ALI_SPDIF_CS + 3)) & 0xF0;
3443 outb(bval | 0x02, TRID_REG(card, ALI_SPDIF_CS + 3));
3447 wval = inw(TRID_REG(card, ALI_SPDIF_CTRL + 2));
3449 wval |= (u16) 0x0E << 8 | (u16) 0x08;
3450 outw(wval, TRID_REG(card, ALI_SPDIF_CTRL + 2));
3452 bval = inb(TRID_REG(card, ALI_SPDIF_CS + 3)) & 0xF0;
3453 outb(bval | 0x03, TRID_REG(card, ALI_SPDIF_CS + 3));
3463 ali_get_spdif_in_rate(struct trident_card *card)
3468 ali_detect_spdif_rate(card);
3470 bval = inb(TRID_REG(card, ALI_SPDIF_CTRL));
3473 outb(bval, TRID_REG(card, ALI_SPDIF_CTRL));
3475 bval = inb(TRID_REG(card, ALI_SPDIF_CS + 3));
3498 ali_close_multi_channels(void)
3501 struct pci_dev *pci_dev = NULL;
3503 pci_dev = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533,
3505 if (pci_dev == NULL)
3507 pci_read_config_byte(pci_dev, 0x59, &temp);
3509 pci_write_config_byte(pci_dev, 0x59, temp);
3511 pci_dev = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101,
3513 if (pci_dev == NULL)
3516 pci_read_config_byte(pci_dev, 0xB8, &temp);
3518 pci_write_config_byte(pci_dev, 0xB8, temp);
3524 ali_setup_multi_channels(struct trident_card *card, int chan_nums)
3526 unsigned long dwValue;
3528 struct pci_dev *pci_dev = NULL;
3530 pci_dev = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533,
3532 if (pci_dev == NULL)
3534 pci_read_config_byte(pci_dev, 0x59, &temp);
3536 pci_write_config_byte(pci_dev, 0x59, temp);
3538 pci_dev = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101,
3540 if (pci_dev == NULL)
3542 pci_read_config_byte(pci_dev, (int) 0xB8, &temp);
3544 pci_write_config_byte(pci_dev, (int) 0xB8, (u8) temp);
3545 if (chan_nums == 6) {
3546 dwValue = inl(TRID_REG(card, ALI_SCTRL)) | 0x000f0000;
3547 outl(dwValue, TRID_REG(card, ALI_SCTRL));
3549 dwValue = inl(TRID_REG(card, ALI_SCTRL));
3550 if (dwValue & 0x2000000) {
3551 ali_ac97_write(card->ac97_codec[0], 0x02, 8080);
3552 ali_ac97_write(card->ac97_codec[0], 0x36, 0);
3553 ali_ac97_write(card->ac97_codec[0], 0x38, 0);
3555 * On a board with a single codec you won't get the
3556 * surround. On other boards configure it.
3558 if (card->ac97_codec[1] != NULL) {
3559 ali_ac97_write(card->ac97_codec[1], 0x36, 0);
3560 ali_ac97_write(card->ac97_codec[1], 0x38, 0);
3561 ali_ac97_write(card->ac97_codec[1], 0x02, 0x0606);
3562 ali_ac97_write(card->ac97_codec[1], 0x18, 0x0303);
3563 ali_ac97_write(card->ac97_codec[1], 0x74, 0x3);
3572 ali_free_pcm_channel(struct trident_card *card, unsigned int channel)
3579 bank = channel >> 5;
3580 channel = channel & 0x1f;
3582 card->banks[bank].bitmap &= ~(1 << (channel));
3586 ali_allocate_other_states_resources(struct trident_state *state, int chan_nums)
3588 struct trident_card *card = state->card;
3589 struct trident_state *s;
3590 int i, state_count = 0;
3591 struct trident_pcm_bank *bank;
3592 struct trident_channel *channel;
3595 bank = &card->banks[BANK_A];
3600 for (i = 0; (i < ALI_CHANNELS) && (state_count != 4); i++) {
3601 if (card->states[i])
3604 num = ali_multi_channels_5_1[state_count];
3605 if (!(bank->bitmap & (1 << num))) {
3606 bank->bitmap |= 1 << num;
3607 channel = &bank->channels[num];
3611 for (; state_count >= 0; state_count--) {
3612 kfree(state->other_states[state_count]);
3613 num = ali_multi_channels_5_1[state_count];
3614 ali_free_pcm_channel(card, num);
3618 s = card->states[i] = kmalloc(sizeof(*state), GFP_KERNEL);
3620 num = ali_multi_channels_5_1[state_count];
3621 ali_free_pcm_channel(card, num);
3623 for (; state_count >= 0; state_count--) {
3624 num = ali_multi_channels_5_1[state_count];
3625 ali_free_pcm_channel(card, num);
3626 kfree(state->other_states[state_count]);
3630 memset(s, 0, sizeof(*state));
3632 s->dmabuf.channel = channel;
3633 s->dmabuf.ossfragshift = s->dmabuf.ossmaxfrags =
3634 s->dmabuf.subdivision = 0;
3635 init_waitqueue_head(&s->dmabuf.wait);
3636 s->magic = card->magic;
3639 ali_enable_special_channel(s);
3640 state->other_states[state_count++] = s;
3643 if (state_count != 4) {
3645 for (; state_count >= 0; state_count--) {
3646 kfree(state->other_states[state_count]);
3647 num = ali_multi_channels_5_1[state_count];
3648 ali_free_pcm_channel(card, num);
3656 ali_save_regs(struct trident_card *card)
3658 unsigned long flags;
3661 spin_lock_irqsave(&card->lock, flags);
3663 ali_registers.global_regs[0x2c] = inl(TRID_REG(card, T4D_MISCINT));
3664 //ali_registers.global_regs[0x20] = inl(TRID_REG(card,T4D_START_A));
3665 ali_registers.global_regs[0x21] = inl(TRID_REG(card, T4D_STOP_A));
3667 //disable all IRQ bits
3668 outl(ALI_DISABLE_ALL_IRQ, TRID_REG(card, T4D_MISCINT));
3670 for (i = 1; i < ALI_MIXER_REGS; i++)
3671 ali_registers.mixer_regs[i] = ali_ac97_read(card->ac97_codec[0],
3674 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
3675 if ((i * 4 == T4D_MISCINT) || (i * 4 == T4D_STOP_A))
3677 ali_registers.global_regs[i] = inl(TRID_REG(card, i * 4));
3680 for (i = 0; i < ALI_CHANNELS; i++) {
3681 outb(i, TRID_REG(card, T4D_LFO_GC_CIR));
3682 for (j = 0; j < ALI_CHANNEL_REGS; j++)
3683 ali_registers.channel_regs[i][j] = inl(TRID_REG(card,
3687 //Stop all HW channel
3688 outl(ALI_STOP_ALL_CHANNELS, TRID_REG(card, T4D_STOP_A));
3690 spin_unlock_irqrestore(&card->lock, flags);
3694 ali_restore_regs(struct trident_card *card)
3696 unsigned long flags;
3699 spin_lock_irqsave(&card->lock, flags);
3701 for (i = 1; i < ALI_MIXER_REGS; i++)
3702 ali_ac97_write(card->ac97_codec[0], i * 2,
3703 ali_registers.mixer_regs[i]);
3705 for (i = 0; i < ALI_CHANNELS; i++) {
3706 outb(i, TRID_REG(card, T4D_LFO_GC_CIR));
3707 for (j = 0; j < ALI_CHANNEL_REGS; j++)
3708 outl(ali_registers.channel_regs[i][j],
3709 TRID_REG(card, j * 4 + 0xe0));
3712 for (i = 0; i < ALI_GLOBAL_REGS; i++) {
3713 if ((i * 4 == T4D_MISCINT) || (i * 4 == T4D_STOP_A) ||
3714 (i * 4 == T4D_START_A))
3716 outl(ali_registers.global_regs[i], TRID_REG(card, i * 4));
3720 outl(ali_registers.global_regs[0x20], TRID_REG(card, T4D_START_A));
3721 //restore IRQ enable bits
3722 outl(ali_registers.global_regs[0x2c], TRID_REG(card, T4D_MISCINT));
3724 spin_unlock_irqrestore(&card->lock, flags);
3728 trident_suspend(struct pci_dev *dev, u32 unused)
3730 struct trident_card *card = pci_get_drvdata(dev);
3732 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
3733 ali_save_regs(card);
3739 trident_resume(struct pci_dev *dev)
3741 struct trident_card *card = pci_get_drvdata(dev);
3743 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
3744 ali_restore_regs(card);
3749 static struct trident_channel *
3750 ali_alloc_pcm_channel(struct trident_card *card)
3752 struct trident_pcm_bank *bank;
3755 bank = &card->banks[BANK_A];
3757 if (inl(TRID_REG(card, ALI_GLOBAL_CONTROL)) &
3758 (ALI_SPDIF_OUT_CH_ENABLE)) {
3759 idx = ALI_SPDIF_OUT_CHANNEL;
3760 if (!(bank->bitmap & (1 << idx))) {
3761 struct trident_channel *channel = &bank->channels[idx];
3762 bank->bitmap |= 1 << idx;
3768 for (idx = ALI_PCM_OUT_CHANNEL_FIRST; idx <= ALI_PCM_OUT_CHANNEL_LAST;
3770 if (!(bank->bitmap & (1 << idx))) {
3771 struct trident_channel *channel = &bank->channels[idx];
3772 bank->bitmap |= 1 << idx;
3778 /* no more free channels avaliable */
3780 printk(KERN_ERR "ali: no more channels available on Bank A.\n");
3785 static struct trident_channel *
3786 ali_alloc_rec_pcm_channel(struct trident_card *card)
3788 struct trident_pcm_bank *bank;
3791 if (inl(TRID_REG(card, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT)
3792 idx = ALI_SPDIF_IN_CHANNEL;
3794 idx = ALI_PCM_IN_CHANNEL;
3796 bank = &card->banks[BANK_A];
3798 if (!(bank->bitmap & (1 << idx))) {
3799 struct trident_channel *channel = &bank->channels[idx];
3800 bank->bitmap |= 1 << idx;
3805 /* no free recordable channels avaliable */
3807 printk(KERN_ERR "ali: no recordable channels available on Bank A.\n");
3813 ali_set_spdif_out_rate(struct trident_card *card, unsigned int rate)
3815 unsigned char ch_st_sel;
3816 unsigned short status_rate;
3823 status_rate = 0x300;
3827 status_rate = 0x200;
3831 /* select spdif_out */
3832 ch_st_sel = inb(TRID_REG(card, ALI_SPDIF_CTRL)) & ALI_SPDIF_OUT_CH_STATUS;
3834 ch_st_sel |= 0x80; /* select right */
3835 outb(ch_st_sel, TRID_REG(card, ALI_SPDIF_CTRL));
3836 outb(status_rate | 0x20, TRID_REG(card, ALI_SPDIF_CS + 2));
3838 ch_st_sel &= (~0x80); /* select left */
3839 outb(ch_st_sel, TRID_REG(card, ALI_SPDIF_CTRL));
3840 outw(status_rate | 0x10, TRID_REG(card, ALI_SPDIF_CS + 2));
3844 ali_address_interrupt(struct trident_card *card)
3847 struct trident_state *state;
3848 u32 mask, channel_mask;
3850 mask = trident_get_interrupt_mask(card, 0);
3851 for (i = 0; i < NR_HW_CH; i++) {
3852 if ((state = card->states[i]) == NULL)
3854 channel = state->dmabuf.channel->num;
3855 if ((channel_mask = 1 << channel) & mask) {
3856 mask &= ~channel_mask;
3857 trident_ack_channel_interrupt(card, channel);
3859 state->dmabuf.update_flag |= ALI_ADDRESS_INT_UPDATE;
3860 trident_update_ptr(state);
3864 for (i = 0; i < NR_HW_CH; i++) {
3865 if (mask & (1 << i)) {
3866 printk("ali: spurious channel irq %d.\n", i);
3867 trident_ack_channel_interrupt(card, i);
3868 trident_stop_voice(card, i);
3869 trident_disable_voice_irq(card, i);
3875 /* Updating the values of counters of other_states' DMAs without lock
3876 protection is no harm because all DMAs of multi-channels and interrupt
3877 depend on a master state's DMA, and changing the counters of the master
3878 state DMA is protected by a spinlock.
3881 ali_write_5_1(struct trident_state *state, const char *buf,
3882 int cnt_for_multi_channel, unsigned int *copy_count,
3883 unsigned int *state_cnt)
3886 struct dmabuf *dmabuf = &state->dmabuf;
3887 struct dmabuf *dmabuf_temp;
3888 const char *buffer = buf;
3889 unsigned swptr, other_dma_nums, sample_s;
3890 unsigned int i, loop;
3893 sample_s = sample_size[dmabuf->fmt] >> 1;
3894 swptr = dmabuf->swptr;
3896 if ((i = state->multi_channels_adjust_count) > 0) {
3898 if (copy_from_user(dmabuf->rawbuf + swptr,
3901 seek_offset(swptr, buffer, cnt_for_multi_channel,
3902 sample_s, *copy_count);
3904 (*state_cnt) += sample_s;
3905 state->multi_channels_adjust_count++;
3907 i = i - (state->chans_num - other_dma_nums);
3908 for (; (i < other_dma_nums) && (cnt_for_multi_channel > 0); i++) {
3909 dmabuf_temp = &state->other_states[i]->dmabuf;
3910 if (copy_from_user(dmabuf_temp->rawbuf + dmabuf_temp->swptr,
3913 seek_offset(dmabuf_temp->swptr, buffer, cnt_for_multi_channel,
3914 sample_s, *copy_count);
3916 if (cnt_for_multi_channel == 0)
3917 state->multi_channels_adjust_count += i;
3919 if (cnt_for_multi_channel > 0) {
3920 loop = cnt_for_multi_channel / (state->chans_num * sample_s);
3921 for (i = 0; i < loop; i++) {
3922 if (copy_from_user(dmabuf->rawbuf + swptr, buffer,
3925 seek_offset(swptr, buffer, cnt_for_multi_channel,
3926 sample_s * 2, *copy_count);
3927 (*state_cnt) += (sample_s * 2);
3929 dmabuf_temp = &state->other_states[0]->dmabuf;
3930 if (copy_from_user(dmabuf_temp->rawbuf + dmabuf_temp->swptr,
3933 seek_offset(dmabuf_temp->swptr, buffer, cnt_for_multi_channel,
3934 sample_s, *copy_count);
3936 dmabuf_temp = &state->other_states[1]->dmabuf;
3937 if (copy_from_user(dmabuf_temp->rawbuf + dmabuf_temp->swptr,
3940 seek_offset(dmabuf_temp->swptr, buffer, cnt_for_multi_channel,
3941 sample_s, *copy_count);
3943 dmabuf_temp = &state->other_states[2]->dmabuf;
3944 if (copy_from_user(dmabuf_temp->rawbuf + dmabuf_temp->swptr,
3947 seek_offset(dmabuf_temp->swptr, buffer, cnt_for_multi_channel,
3948 sample_s, *copy_count);
3950 dmabuf_temp = &state->other_states[3]->dmabuf;
3951 if (copy_from_user(dmabuf_temp->rawbuf + dmabuf_temp->swptr,
3954 seek_offset(dmabuf_temp->swptr, buffer, cnt_for_multi_channel,
3955 sample_s, *copy_count);
3958 if (cnt_for_multi_channel > 0) {
3959 state->multi_channels_adjust_count = cnt_for_multi_channel / sample_s;
3961 if (copy_from_user(dmabuf->rawbuf + swptr, buffer, sample_s))
3963 seek_offset(swptr, buffer, cnt_for_multi_channel,
3964 sample_s, *copy_count);
3965 (*state_cnt) += sample_s;
3967 if (cnt_for_multi_channel > 0) {
3968 if (copy_from_user(dmabuf->rawbuf + swptr,
3971 seek_offset(swptr, buffer, cnt_for_multi_channel,
3972 sample_s, *copy_count);
3973 (*state_cnt) += sample_s;
3975 if (cnt_for_multi_channel > 0) {
3976 int diff = state->chans_num - other_dma_nums;
3977 loop = state->multi_channels_adjust_count - diff;
3978 for (i = 0; i < loop; i++) {
3979 dmabuf_temp = &state->other_states[i]->dmabuf;
3980 if (copy_from_user(dmabuf_temp->rawbuf +
3984 seek_offset(dmabuf_temp->swptr, buffer,
3985 cnt_for_multi_channel,
3986 sample_s, *copy_count);
3991 state->multi_channels_adjust_count = 0;
3993 for (i = 0; i < other_dma_nums; i++) {
3994 dmabuf_temp = &state->other_states[i]->dmabuf;
3995 dmabuf_temp->swptr = dmabuf_temp->swptr % dmabuf_temp->dmasize;
4001 ali_free_other_states_resources(struct trident_state *state)
4004 struct trident_card *card = state->card;
4005 struct trident_state *s;
4006 unsigned other_states_count;
4008 other_states_count = state->chans_num - 2; /* except PCM L/R channels */
4009 for (i = 0; i < other_states_count; i++) {
4010 s = state->other_states[i];
4011 dealloc_dmabuf(&s->dmabuf, card->pci_dev);
4012 ali_disable_special_channel(s->card, s->dmabuf.channel->num);
4013 state->card->free_pcm_channel(s->card, s->dmabuf.channel->num);
4014 card->states[s->virt] = NULL;
4019 struct proc_dir_entry *res;
4021 ali_write_proc(struct file *file, const char *buffer, unsigned long count, void *data)
4023 struct trident_card *card = (struct trident_card *) data;
4024 unsigned long flags;
4031 if (get_user(c, buffer))
4034 spin_lock_irqsave(&card->lock, flags);
4037 ali_setup_spdif_out(card, ALI_PCM_TO_SPDIF_OUT);
4038 ali_disable_special_channel(card, ALI_SPDIF_OUT_CHANNEL);
4041 ali_setup_spdif_out(card, ALI_SPDIF_OUT_TO_SPDIF_OUT |
4045 ali_setup_spdif_out(card, ALI_SPDIF_OUT_TO_SPDIF_OUT |
4046 ALI_SPDIF_OUT_NON_PCM);
4049 ali_disable_spdif_in(card); //default
4052 ali_setup_spdif_in(card);
4055 spin_unlock_irqrestore(&card->lock, flags);
4060 /* OSS /dev/mixer file operation methods */
4062 trident_open_mixdev(struct inode *inode, struct file *file)
4065 int minor = iminor(inode);
4066 struct trident_card *card = devs;
4068 for (card = devs; card != NULL; card = card->next)
4069 for (i = 0; i < NR_AC97; i++)
4070 if (card->ac97_codec[i] != NULL &&
4071 card->ac97_codec[i]->dev_mixer == minor)
4078 file->private_data = card->ac97_codec[i];
4084 trident_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd,
4087 struct ac97_codec *codec = (struct ac97_codec *) file->private_data;
4089 return codec->mixer_ioctl(codec, cmd, arg);
4092 static /*const */ struct file_operations trident_mixer_fops = {
4093 .owner = THIS_MODULE,
4094 .llseek = no_llseek,
4095 .ioctl = trident_ioctl_mixdev,
4096 .open = trident_open_mixdev,
4100 ali_reset_5451(struct trident_card *card)
4102 struct pci_dev *pci_dev = NULL;
4104 unsigned short wCount, wReg;
4106 pci_dev = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533,
4108 if (pci_dev == NULL)
4111 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
4112 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
4114 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
4115 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
4118 pci_dev = card->pci_dev;
4119 if (pci_dev == NULL)
4122 pci_read_config_dword(pci_dev, 0x44, &dwVal);
4123 pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
4125 pci_read_config_dword(pci_dev, 0x44, &dwVal);
4126 pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
4129 /* TODO: recognize if we have a PM capable codec and only do this */
4130 /* if the codec is PM capable */
4133 wReg = ali_ac97_get(card, 0, AC97_POWER_CONTROL);
4134 if ((wReg & 0x000f) == 0x000f)
4138 /* This is non fatal if you have a non PM capable codec.. */
4142 /* AC97 codec initialisation. */
4143 static int __devinit
4144 trident_ac97_init(struct trident_card *card)
4147 unsigned long ready_2nd = 0;
4148 struct ac97_codec *codec;
4151 /* initialize controller side of AC link, and find out if secondary codes
4153 switch (card->pci_id) {
4154 case PCI_DEVICE_ID_ALI_5451:
4155 if (ali_reset_5451(card)) {
4156 printk(KERN_ERR "trident_ac97_init: error "
4157 "resetting 5451.\n");
4160 outl(0x80000001, TRID_REG(card, ALI_GLOBAL_CONTROL));
4161 outl(0x00000000, TRID_REG(card, T4D_AINTEN_A));
4162 outl(0xffffffff, TRID_REG(card, T4D_AINT_A));
4163 outl(0x00000000, TRID_REG(card, T4D_MUSICVOL_WAVEVOL));
4164 outb(0x10, TRID_REG(card, ALI_MPUR2));
4165 ready_2nd = inl(TRID_REG(card, ALI_SCTRL));
4166 ready_2nd &= 0x3fff;
4167 outl(ready_2nd | PCMOUT | 0x8000, TRID_REG(card, ALI_SCTRL));
4168 ready_2nd = inl(TRID_REG(card, ALI_SCTRL));
4169 ready_2nd &= SI_AC97_SECONDARY_READY;
4170 if (card->revision < ALI_5451_V02)
4173 case PCI_DEVICE_ID_SI_7018:
4174 /* disable AC97 GPIO interrupt */
4175 outl(0x00, TRID_REG(card, SI_AC97_GPIO));
4176 /* when power up the AC link is in cold reset mode so stop it */
4177 outl(PCMOUT | SURROUT | CENTEROUT | LFEOUT | SECONDARY_ID,
4178 TRID_REG(card, SI_SERIAL_INTF_CTRL));
4179 /* it take a long time to recover from a cold reset */
4180 /* (especially when you have more than one codec) */
4182 ready_2nd = inl(TRID_REG(card, SI_SERIAL_INTF_CTRL));
4183 ready_2nd &= SI_AC97_SECONDARY_READY;
4185 case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX:
4187 outl(DX_AC97_PLAYBACK, TRID_REG(card, DX_ACR2_AC97_COM_STAT));
4189 case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX:
4190 /* enable AC97 Output Slot 3,4 (PCM Left/Right Playback) */
4191 outl(NX_AC97_PCM_OUTPUT, TRID_REG(card, NX_ACR0_AC97_COM_STAT));
4192 ready_2nd = inl(TRID_REG(card, NX_ACR0_AC97_COM_STAT));
4193 ready_2nd &= NX_AC97_SECONDARY_READY;
4195 case PCI_DEVICE_ID_INTERG_5050:
4196 /* disable AC97 GPIO interrupt */
4197 outl(0x00, TRID_REG(card, SI_AC97_GPIO));
4198 /* when power up, the AC link is in cold reset mode, so stop it */
4199 outl(PCMOUT | SURROUT | CENTEROUT | LFEOUT,
4200 TRID_REG(card, SI_SERIAL_INTF_CTRL));
4201 /* it take a long time to recover from a cold reset (especially */
4202 /* when you have more than one codec) */
4204 ready_2nd = inl(TRID_REG(card, SI_SERIAL_INTF_CTRL));
4205 ready_2nd &= SI_AC97_SECONDARY_READY;
4209 for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
4210 if ((codec = ac97_alloc_codec()) == NULL)
4213 /* initialize some basic codec information, other fields */
4214 /* will be filled in ac97_probe_codec */
4215 codec->private_data = card;
4216 codec->id = num_ac97;
4218 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
4219 codec->codec_read = ali_ac97_read;
4220 codec->codec_write = ali_ac97_write;
4222 codec->codec_read = trident_ac97_get;
4223 codec->codec_write = trident_ac97_set;
4226 if (ac97_probe_codec(codec) == 0)
4229 codec->dev_mixer = register_sound_mixer(&trident_mixer_fops, -1);
4230 if (codec->dev_mixer < 0) {
4231 printk(KERN_ERR "trident: couldn't register mixer!\n");
4232 ac97_release_codec(codec);
4236 card->ac97_codec[num_ac97] = codec;
4238 /* if there is no secondary codec at all, don't probe any more */
4243 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
4244 for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
4245 if (card->ac97_codec[num_ac97] == NULL)
4247 for (i = 0; i < 64; i++) {
4248 u16 reg = ali_ac97_get(card, num_ac97, i * 2);
4249 card->mixer_regs[i][num_ac97] = reg;
4253 return num_ac97 + 1;
4256 /* Gameport functions for the cards ADC gameport */
4258 static unsigned char
4259 trident_game_read(struct gameport *gameport)
4261 struct trident_card *card = gameport->driver;
4262 return inb(TRID_REG(card, T4D_GAME_LEG));
4266 trident_game_trigger(struct gameport *gameport)
4268 struct trident_card *card = gameport->driver;
4269 outb(0xff, TRID_REG(card, T4D_GAME_LEG));
4273 trident_game_cooked_read(struct gameport *gameport, int *axes, int *buttons)
4275 struct trident_card *card = gameport->driver;
4278 *buttons = (~inb(TRID_REG(card, T4D_GAME_LEG)) >> 4) & 0xf;
4280 for (i = 0; i < 4; i++) {
4281 axes[i] = inw(TRID_REG(card, T4D_GAME_AXD) + i * sizeof (u16));
4282 if (axes[i] == 0xffff)
4290 trident_game_open(struct gameport *gameport, int mode)
4292 struct trident_card *card = gameport->driver;
4295 case GAMEPORT_MODE_COOKED:
4296 outb(0x80, TRID_REG(card, T4D_GAME_CR));
4299 case GAMEPORT_MODE_RAW:
4300 outb(0x00, TRID_REG(card, T4D_GAME_CR));
4309 /* install the driver, we do not allocate hardware channel nor DMA buffer */
4310 /* now, they are defered until "ACCESS" time (in prog_dmabuf called by */
4311 /* open/read/write/ioctl/mmap) */
4312 static int __devinit
4313 trident_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
4315 unsigned long iobase;
4316 struct trident_card *card;
4321 struct pci_dev *pci_dev_m1533 = NULL;
4325 if (pci_enable_device(pci_dev))
4328 if (pci_dev->device == PCI_DEVICE_ID_ALI_5451)
4329 dma_mask = ALI_DMA_MASK;
4331 dma_mask = TRIDENT_DMA_MASK;
4332 if (pci_set_dma_mask(pci_dev, dma_mask)) {
4333 printk(KERN_ERR "trident: architecture does not support"
4334 " %s PCI busmaster DMA\n",
4335 pci_dev->device == PCI_DEVICE_ID_ALI_5451 ?
4336 "32-bit" : "30-bit");
4339 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
4341 if (pci_id->device == PCI_DEVICE_ID_INTERG_5050)
4342 iobase = pci_resource_start(pci_dev, 1);
4344 iobase = pci_resource_start(pci_dev, 0);
4346 if (!request_region(iobase, 256, card_names[pci_id->driver_data])) {
4347 printk(KERN_ERR "trident: can't allocate I/O space at "
4348 "0x%4.4lx\n", iobase);
4353 if ((card = kmalloc(sizeof(*card), GFP_KERNEL)) == NULL) {
4354 printk(KERN_ERR "trident: out of memory\n");
4355 goto out_release_region;
4357 memset(card, 0, sizeof (*card));
4359 init_timer(&card->timer);
4360 card->iobase = iobase;
4361 card->pci_dev = pci_dev;
4362 card->pci_id = pci_id->device;
4363 card->revision = revision;
4364 card->irq = pci_dev->irq;
4366 card->magic = TRIDENT_CARD_MAGIC;
4367 card->banks[BANK_A].addresses = &bank_a_addrs;
4368 card->banks[BANK_A].bitmap = 0UL;
4369 card->banks[BANK_B].addresses = &bank_b_addrs;
4370 card->banks[BANK_B].bitmap = 0UL;
4372 card->gameport.driver = card;
4373 card->gameport.fuzz = 64;
4374 card->gameport.read = trident_game_read;
4375 card->gameport.trigger = trident_game_trigger;
4376 card->gameport.cooked_read = trident_game_cooked_read;
4377 card->gameport.open = trident_game_open;
4379 init_MUTEX(&card->open_sem);
4380 spin_lock_init(&card->lock);
4381 init_timer(&card->timer);
4385 pci_set_master(pci_dev);
4387 printk(KERN_INFO "trident: %s found at IO 0x%04lx, IRQ %d\n",
4388 card_names[pci_id->driver_data], card->iobase, card->irq);
4390 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
4391 /* ALi channel Management */
4392 card->alloc_pcm_channel = ali_alloc_pcm_channel;
4393 card->alloc_rec_pcm_channel = ali_alloc_rec_pcm_channel;
4394 card->free_pcm_channel = ali_free_pcm_channel;
4396 card->address_interrupt = ali_address_interrupt;
4398 /* Added by Matt Wu 01-05-2001 for spdif in */
4399 card->multi_channel_use_count = 0;
4400 card->rec_channel_use_count = 0;
4402 /* ALi SPDIF OUT function */
4403 if (card->revision == ALI_5451_V02) {
4404 ali_setup_spdif_out(card, ALI_PCM_TO_SPDIF_OUT);
4405 res = create_proc_entry("ALi5451", 0, NULL);
4407 res->write_proc = ali_write_proc;
4412 /* Add H/W Volume Control By Matt Wu Jul. 06, 2001 */
4414 pci_dev_m1533 = pci_find_device(PCI_VENDOR_ID_AL,
4415 PCI_DEVICE_ID_AL_M1533,
4418 if (pci_dev_m1533 == NULL)
4420 pci_read_config_byte(pci_dev_m1533, 0x63, &bits);
4421 if (bits & (1 << 5))
4423 if (card->hwvolctl) {
4424 /* Clear m1533 pci cfg 78h bit 30 to zero, which makes
4425 GPIO11/12/13 work as ACGP_UP/DOWN/MUTE. */
4426 pci_read_config_byte(pci_dev_m1533, 0x7b, &bits);
4427 bits &= 0xbf; /*clear bit 6 */
4428 pci_write_config_byte(pci_dev_m1533, 0x7b, bits);
4430 } else if (card->pci_id == PCI_DEVICE_ID_INTERG_5050) {
4431 card->alloc_pcm_channel = cyber_alloc_pcm_channel;
4432 card->alloc_rec_pcm_channel = cyber_alloc_pcm_channel;
4433 card->free_pcm_channel = cyber_free_pcm_channel;
4434 card->address_interrupt = cyber_address_interrupt;
4435 cyber_init_ritual(card);
4437 card->alloc_pcm_channel = trident_alloc_pcm_channel;
4438 card->alloc_rec_pcm_channel = trident_alloc_pcm_channel;
4439 card->free_pcm_channel = trident_free_pcm_channel;
4440 card->address_interrupt = trident_address_interrupt;
4445 if (request_irq(card->irq, &trident_interrupt, SA_SHIRQ,
4446 card_names[pci_id->driver_data], card)) {
4447 printk(KERN_ERR "trident: unable to allocate irq %d\n",
4451 /* register /dev/dsp */
4452 if ((card->dev_audio = register_sound_dsp(&trident_audio_fops, -1)) < 0) {
4453 printk(KERN_ERR "trident: couldn't register DSP device!\n");
4456 card->mixer_regs_ready = 0;
4457 /* initialize AC97 codec and register /dev/mixer */
4458 if (trident_ac97_init(card) <= 0) {
4459 /* unregister audio devices */
4460 for (i = 0; i < NR_AC97; i++) {
4461 if (card->ac97_codec[i] != NULL) {
4462 struct ac97_codec* codec = card->ac97_codec[i];
4463 unregister_sound_mixer(codec->dev_mixer);
4464 ac97_release_codec(codec);
4467 goto out_unregister_sound_dsp;
4469 card->mixer_regs_ready = 1;
4470 outl(0x00, TRID_REG(card, T4D_MUSICVOL_WAVEVOL));
4472 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
4473 /* Add H/W Volume Control By Matt Wu Jul. 06, 2001 */
4474 if (card->hwvolctl) {
4475 /* Enable GPIO IRQ (MISCINT bit 18h) */
4476 temp = inw(TRID_REG(card, T4D_MISCINT + 2));
4478 outw(temp, TRID_REG(card, T4D_MISCINT + 2));
4480 /* Enable H/W Volume Control GLOVAL CONTROL bit 0 */
4481 temp = inw(TRID_REG(card, ALI_GLOBAL_CONTROL));
4483 outw(temp, TRID_REG(card, ALI_GLOBAL_CONTROL));
4486 if (card->revision == ALI_5451_V02)
4487 ali_close_multi_channels();
4488 /* edited by HMSEO for GT sound */
4489 #if defined(CONFIG_ALPHA_NAUTILUS) || defined(CONFIG_ALPHA_GENERIC)
4492 extern struct hwrpb_struct *hwrpb;
4494 if ((hwrpb->sys_type) == 201) {
4495 printk(KERN_INFO "trident: Running on Alpha system "
4497 ac97_data = ali_ac97_get(card, 0, AC97_POWER_CONTROL);
4498 ali_ac97_set(card, 0, AC97_POWER_CONTROL,
4499 ac97_data | ALI_EAPD_POWER_DOWN);
4502 #endif /* CONFIG_ALPHA_NAUTILUS || CONFIG_ALPHA_GENERIC */
4503 /* edited by HMSEO for GT sound */
4506 pci_set_drvdata(pci_dev, card);
4508 /* Enable Address Engine Interrupts */
4509 trident_enable_loop_interrupts(card);
4511 /* Register gameport */
4512 gameport_register_port(&card->gameport);
4517 out_unregister_sound_dsp:
4518 unregister_sound_dsp(card->dev_audio);
4520 free_irq(card->irq, card);
4523 remove_proc_entry("ALi5451", NULL);
4529 release_region(iobase, 256);
4533 static void __devexit
4534 trident_remove(struct pci_dev *pci_dev)
4537 struct trident_card *card = pci_get_drvdata(pci_dev);
4540 * Kill running timers before unload. We can't have them
4541 * going off after rmmod!
4544 del_timer_sync(&card->timer);
4546 /* ALi S/PDIF and Power Management */
4547 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
4548 ali_setup_spdif_out(card, ALI_PCM_TO_SPDIF_OUT);
4549 ali_disable_special_channel(card, ALI_SPDIF_OUT_CHANNEL);
4550 ali_disable_spdif_in(card);
4551 remove_proc_entry("ALi5451", NULL);
4554 /* Unregister gameport */
4555 gameport_unregister_port(&card->gameport);
4557 /* Kill interrupts, and SP/DIF */
4558 trident_disable_loop_interrupts(card);
4560 /* free hardware resources */
4561 free_irq(card->irq, card);
4562 release_region(card->iobase, 256);
4564 /* unregister audio devices */
4565 for (i = 0; i < NR_AC97; i++)
4566 if (card->ac97_codec[i] != NULL) {
4567 unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
4568 ac97_release_codec(card->ac97_codec[i]);
4570 unregister_sound_dsp(card->dev_audio);
4574 pci_set_drvdata(pci_dev, NULL);
4577 MODULE_AUTHOR("Alan Cox, Aaron Holtzman, Ollie Lho, Ching Ling Lee, Muli Ben-Yehuda");
4578 MODULE_DESCRIPTION("Trident 4DWave/SiS 7018/ALi 5451 and Tvia/IGST CyberPro5050 PCI "
4580 MODULE_LICENSE("GPL");
4582 #define TRIDENT_MODULE_NAME "trident"
4584 static struct pci_driver trident_pci_driver = {
4585 .name = TRIDENT_MODULE_NAME,
4586 .id_table = trident_pci_tbl,
4587 .probe = trident_probe,
4588 .remove = __devexit_p(trident_remove),
4589 .suspend = trident_suspend,
4590 .resume = trident_resume
4594 trident_init_module(void)
4596 printk(KERN_INFO "Trident 4DWave/SiS 7018/ALi 5451,Tvia CyberPro "
4597 "5050 PCI Audio, version " DRIVER_VERSION ", " __TIME__ " "
4600 if (!pci_register_driver(&trident_pci_driver)) {
4601 pci_unregister_driver(&trident_pci_driver);
4608 trident_cleanup_module(void)
4610 pci_unregister_driver(&trident_pci_driver);
4613 module_init(trident_init_module);
4614 module_exit(trident_cleanup_module);