vserver 1.9.3
[linux-2.6.git] / sound / oss / trident.c
1 /*
2  *      OSS driver for Linux 2.[46].x for
3  *
4  *      Trident 4D-Wave
5  *      SiS 7018
6  *      ALi 5451
7  *      Tvia/IGST CyberPro 5050
8  *
9  *      Driver: Alan Cox <alan@redhat.com>
10  *
11  *  Built from:
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>  
15  *
16  *  Hacked up by:
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>
23  *
24  *
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.
29  *
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.
34  *
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.
38  *
39  *  History
40  *  v0.14.10j
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.
44  *  v0.14.10i
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().
49  *  v0.14.10h
50  *      Sept 10 2002 Pascal Schmidt <der.eremit@email.de>
51  *      added support for ALi 5451 joystick port
52  *  v0.14.10g
53  *      Sept 05 2002 Alan Cox <alan@redhat.com>
54  *      adapt to new pci joystick attachment interface
55  *  v0.14.10f
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*. 
59  *  v0.14.10e
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.
65  *  v0.14.10d
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. 
69  *  v0.14.10c
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. 
73  *  v0.14.10b
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. 
77  *  v0.14.10a
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. 
81  *  v0.14.10
82  *      June 6 2002 Lei Hu <Lei_hu@ali.com.tw>
83  *      rewrite the part to read/write registers of audio codec for Ali5451 
84  *  v0.14.9e
85  *      January 2 2002 Vojtech Pavlik <vojtech@ucw.cz> added gameport
86  *      support to avoid resource conflict with pcigame.c
87  *  v0.14.9d
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
91  *  v0.14.9c
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)
95  *  v0.14.9b
96  *      Switch to static inline not extern inline (gcc 3)
97  *  v0.14.9a
98  *      Aug 6 2001 Alan Cox
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
103  *  v0.14.9
104  *      Jul 10 2001 Matt Wu
105  *      Add H/W Volume Control
106  *  v0.14.8a
107  *      July 7 2001 Alan Cox
108  *      Moved Matt Wu's ac97 register cache into the card structure
109  *  v0.14.8
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
114  *  unreleased 
115  *      Mar 09 2001 Matt Wu
116  *      Add cache for ac97 access
117  *  v0.14.7
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+
123  *  v0.14.6 
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
181  * 
182  *  ToDo
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)
187  *      Mmap support
188  *      "Channel Binding" ioctl extension (done)
189  *      new pci device driver interface for 2.4 kernel (done)
190  *
191  *      Lock order (high->low)
192  *              lock    -       hardware lock
193  *              open_sem -      guard opens
194  *              sem     -       guard dmabuf, write re-entry etc
195  */
196
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/io.h>
221 #include <asm/dma.h>
222
223 #if defined(CONFIG_ALPHA_NAUTILUS) || defined(CONFIG_ALPHA_GENERIC)
224 #include <asm/hwrpb.h>
225 #endif
226
227 #include "trident.h"
228
229 #define DRIVER_VERSION "0.14.10j-2.6"
230
231 /* magic numbers to protect our data structures */
232 #define TRIDENT_CARD_MAGIC      0x5072696E      /* "Prin" */
233 #define TRIDENT_STATE_MAGIC     0x63657373      /* "cess" */
234
235 #define TRIDENT_DMA_MASK        0x3fffffff      /* DMA buffer mask for pci_alloc_consist */
236 #define ALI_DMA_MASK            0x7fffffff      /* ALI Tridents have 31-bit DMA. Wow. */
237
238 #define NR_HW_CH                32
239
240 /* maximum number of AC97 codecs connected, AC97 2.0 defined 4, but 7018 and 4D-NX only
241    have 2 SDATA_IN lines (currently) */
242 #define NR_AC97         2
243
244 /* minor number of /dev/swmodem (temporary, experimental) */
245 #define SND_DEV_SWMODEM 7
246
247 static const unsigned ali_multi_channels_5_1[] = {
248         /*ALI_SURR_LEFT_CHANNEL, ALI_SURR_RIGHT_CHANNEL, */
249         ALI_CENTER_CHANNEL,
250         ALI_LEF_CHANNEL,
251         ALI_SURR_LEFT_CHANNEL,
252         ALI_SURR_RIGHT_CHANNEL
253 };
254
255 static const unsigned sample_size[] = { 1, 2, 2, 4 };
256 static const unsigned sample_shift[] = { 0, 1, 1, 2 };
257
258 static const char invalid_magic[] = KERN_CRIT "trident: invalid magic value in %s\n";
259
260 enum {
261         TRIDENT_4D_DX = 0,
262         TRIDENT_4D_NX,
263         SIS_7018,
264         ALI_5451,
265         CYBER5050
266 };
267
268 static char *card_names[] = {
269         "Trident 4DWave DX",
270         "Trident 4DWave NX",
271         "SiS 7018 PCI Audio",
272         "ALi Audio Accelerator",
273         "Tvia/IGST CyberPro 5050"
274 };
275
276 static struct pci_device_id trident_pci_tbl[] = {
277         {PCI_VENDOR_ID_TRIDENT, PCI_DEVICE_ID_TRIDENT_4DWAVE_DX,
278          PCI_ANY_ID, PCI_ANY_ID, 0, 0, TRIDENT_4D_DX},
279         {PCI_VENDOR_ID_TRIDENT, PCI_DEVICE_ID_TRIDENT_4DWAVE_NX,
280          PCI_ANY_ID, PCI_ANY_ID, 0, 0, TRIDENT_4D_NX},
281         {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_7018,
282          PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_7018},
283         {PCI_VENDOR_ID_ALI, PCI_DEVICE_ID_ALI_5451,
284          PCI_ANY_ID, PCI_ANY_ID, 0, 0, ALI_5451},
285         {PCI_VENDOR_ID_INTERG, PCI_DEVICE_ID_INTERG_5050,
286          PCI_ANY_ID, PCI_ANY_ID, 0, 0, CYBER5050},
287         {0,}
288 };
289
290 MODULE_DEVICE_TABLE(pci, trident_pci_tbl);
291
292 /* "software" or virtual channel, an instance of opened /dev/dsp */
293 struct trident_state {
294         unsigned int magic;
295         struct trident_card *card;      /* Card info */
296
297         /* file mode */
298         mode_t open_mode;
299
300         /* virtual channel number */
301         int virt;
302
303         struct dmabuf {
304                 /* wave sample stuff */
305                 unsigned int rate;
306                 unsigned char fmt, enable;
307
308                 /* hardware channel */
309                 struct trident_channel *channel;
310
311                 /* OSS buffer management stuff */
312                 void *rawbuf;
313                 dma_addr_t dma_handle;
314                 unsigned buforder;
315                 unsigned numfrag;
316                 unsigned fragshift;
317
318                 /* our buffer acts like a circular ring */
319                 unsigned hwptr; /* where dma last started, updated by update_ptr */
320                 unsigned swptr; /* where driver last clear/filled, updated by read/write */
321                 int count;      /* bytes to be comsumed or been generated by dma machine */
322                 unsigned total_bytes;   /* total bytes dmaed by hardware */
323
324                 unsigned error; /* number of over/underruns */
325                 /* put process on wait queue when no more space in buffer */
326                 wait_queue_head_t wait; 
327
328                 /* redundant, but makes calculations easier */
329                 unsigned fragsize;
330                 unsigned dmasize;
331                 unsigned fragsamples;
332
333                 /* OSS stuff */
334                 unsigned mapped:1;
335                 unsigned ready:1;
336                 unsigned endcleared:1;
337                 unsigned update_flag;
338                 unsigned ossfragshift;
339                 int ossmaxfrags;
340                 unsigned subdivision;
341
342         } dmabuf;
343
344         /* 5.1 channels */
345         struct trident_state *other_states[4];
346         int multi_channels_adjust_count;
347         unsigned chans_num;
348         unsigned long fmt_flag;
349         /* Guard against mmap/write/read races */
350         struct semaphore sem;
351
352 };
353
354 /* hardware channels */
355 struct trident_channel {
356         int num; /* channel number */
357         u32 lba; /* Loop Begine Address, where dma buffer starts */
358         u32 eso; /* End Sample Offset, wehre dma buffer ends */ 
359                  /* (in the unit of samples) */
360         u32 delta; /* delta value, sample rate / 48k for playback, */
361                    /* 48k/sample rate for recording */
362         u16 attribute; /* control where PCM data go and come  */
363         u16 fm_vol;
364         u32 control; /* signed/unsigned, 8/16 bits, mono/stereo */
365 };
366
367 struct trident_pcm_bank_address {
368         u32 start;
369         u32 stop;
370         u32 aint;
371         u32 aint_en;
372 };
373
374 static struct trident_pcm_bank_address bank_a_addrs = {
375         T4D_START_A,
376         T4D_STOP_A,
377         T4D_AINT_A,
378         T4D_AINTEN_A
379 };
380
381 static struct trident_pcm_bank_address bank_b_addrs = {
382         T4D_START_B,
383         T4D_STOP_B,
384         T4D_AINT_B,
385         T4D_AINTEN_B
386 };
387
388 struct trident_pcm_bank {
389         /* register addresses to control bank operations */
390         struct trident_pcm_bank_address *addresses;
391         /* each bank has 32 channels */
392         u32 bitmap;             /* channel allocation bitmap */
393         struct trident_channel channels[32];
394 };
395
396 struct trident_card {
397         unsigned int magic;
398
399         /* We keep trident cards in a linked list */
400         struct trident_card *next;
401
402         /* single open lock mechanism, only used for recording */
403         struct semaphore open_sem;
404
405         /* The trident has a certain amount of cross channel interaction
406            so we use a single per card lock */
407         spinlock_t lock;
408
409         /* PCI device stuff */
410         struct pci_dev *pci_dev;
411         u16 pci_id;
412         u8 revision;
413
414         /* soundcore stuff */
415         int dev_audio;
416
417         /* structures for abstraction of hardware facilities, codecs, */ 
418         /* banks and channels */
419         struct ac97_codec *ac97_codec[NR_AC97];
420         struct trident_pcm_bank banks[NR_BANKS];
421         struct trident_state *states[NR_HW_CH];
422
423         /* hardware resources */
424         unsigned long iobase;
425         u32 irq;
426
427         /* Function support */
428         struct trident_channel *(*alloc_pcm_channel) (struct trident_card *);
429         struct trident_channel *(*alloc_rec_pcm_channel) (struct trident_card *);
430         void (*free_pcm_channel) (struct trident_card *, unsigned int chan);
431         void (*address_interrupt) (struct trident_card *);
432
433         /* Added by Matt Wu 01-05-2001 for spdif in */
434         int multi_channel_use_count;
435         int rec_channel_use_count;
436         u16 mixer_regs[64][NR_AC97];    /* Made card local by Alan */
437         int mixer_regs_ready;
438
439         /* Added for hardware volume control */
440         int hwvolctl;
441         struct timer_list timer;
442
443         /* Game port support */
444         struct gameport gameport;
445 };
446
447 enum dmabuf_mode {
448         DM_PLAYBACK = 0,
449         DM_RECORD
450 };
451
452 /* table to map from CHANNELMASK to channel attribute for SiS 7018 */
453 static u16 mask2attr[] = {
454         PCM_LR, PCM_LR, SURR_LR, CENTER_LFE,
455         HSET, MIC, MODEM_LINE1, MODEM_LINE2,
456         I2S_LR, SPDIF_LR
457 };
458
459 /* table to map from channel attribute to CHANNELMASK for SiS 7018 */
460 static int attr2mask[] = {
461         DSP_BIND_MODEM1, DSP_BIND_MODEM2, DSP_BIND_FRONT, DSP_BIND_HANDSET,
462         DSP_BIND_I2S, DSP_BIND_CENTER_LFE, DSP_BIND_SURR, DSP_BIND_SPDIF
463 };
464
465 /* Added by Matt Wu 01-05-2001 for spdif in */
466 static int ali_close_multi_channels(void);
467 static void ali_delay(struct trident_card *card, int interval);
468 static void ali_detect_spdif_rate(struct trident_card *card);
469
470 static void ali_ac97_write(struct ac97_codec *codec, u8 reg, u16 val);
471 static u16 ali_ac97_read(struct ac97_codec *codec, u8 reg);
472
473 static struct trident_card *devs;
474
475 static void trident_ac97_set(struct ac97_codec *codec, u8 reg, u16 val);
476 static u16 trident_ac97_get(struct ac97_codec *codec, u8 reg);
477
478 static int trident_open_mixdev(struct inode *inode, struct file *file);
479 static int trident_ioctl_mixdev(struct inode *inode, struct file *file, 
480                                 unsigned int cmd, unsigned long arg);
481
482 static void ali_ac97_set(struct trident_card *card, int secondary, u8 reg, u16 val);
483 static u16 ali_ac97_get(struct trident_card *card, int secondary, u8 reg);
484 static void ali_set_spdif_out_rate(struct trident_card *card, unsigned int rate);
485 static void ali_enable_special_channel(struct trident_state *stat);
486 static struct trident_channel *ali_alloc_rec_pcm_channel(struct trident_card *card);
487 static struct trident_channel *ali_alloc_pcm_channel(struct trident_card *card);
488 static void ali_restore_regs(struct trident_card *card);
489 static void ali_save_regs(struct trident_card *card);
490 static int trident_suspend(struct pci_dev *dev, u32 unused);
491 static int trident_resume(struct pci_dev *dev);
492 static void ali_free_pcm_channel(struct trident_card *card, unsigned int channel);
493 static int ali_setup_multi_channels(struct trident_card *card, int chan_nums);
494 static unsigned int ali_get_spdif_in_rate(struct trident_card *card);
495 static void ali_setup_spdif_in(struct trident_card *card);
496 static void ali_disable_spdif_in(struct trident_card *card);
497 static void ali_disable_special_channel(struct trident_card *card, int ch);
498 static void ali_setup_spdif_out(struct trident_card *card, int flag);
499 static int ali_write_5_1(struct trident_state *state,
500                          const char __user *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, 
504                                                int chan_nums);
505 static void ali_free_other_states_resources(struct trident_state *state);
506
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];
512 } ali_registers;
513
514 #define seek_offset(dma_ptr, buffer, cnt, offset, copy_count)   do { \
515         (dma_ptr) += (offset);    \
516         (buffer) += (offset);     \
517         (cnt) -= (offset);        \
518         (copy_count) += (offset); \
519 } while (0)
520
521 static inline int lock_set_fmt(struct trident_state* state)
522 {
523         if (test_and_set_bit(0, &state->fmt_flag))
524                 return -EFAULT;
525
526         return 0;
527 }
528
529 static inline void unlock_set_fmt(struct trident_state* state)
530 {
531         clear_bit(0, &state->fmt_flag);
532 }
533
534 static int
535 trident_enable_loop_interrupts(struct trident_card *card)
536 {
537         u32 global_control;
538
539         global_control = inl(TRID_REG(card, T4D_LFO_GC_CIR));
540
541         switch (card->pci_id) {
542         case PCI_DEVICE_ID_SI_7018:
543                 global_control |= (ENDLP_IE | MIDLP_IE | BANK_B_EN);
544                 break;
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);
550                 break;
551         default:
552                 return 0;
553         }
554
555         outl(global_control, TRID_REG(card, T4D_LFO_GC_CIR));
556
557         pr_debug("trident: Enable Loop Interrupts, globctl = 0x%08X\n",
558                  inl(TRID_REG(card, T4D_LFO_GC_CIR)));
559
560         return 1;
561 }
562
563 static int
564 trident_disable_loop_interrupts(struct trident_card *card)
565 {
566         u32 global_control;
567
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));
571
572         pr_debug("trident: Disabled Loop Interrupts, globctl = 0x%08X\n",
573                  global_control);
574
575         return 1;
576 }
577
578 static void
579 trident_enable_voice_irq(struct trident_card *card, unsigned int channel)
580 {
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;
584
585         reg = inl(TRID_REG(card, addr));
586         reg |= mask;
587         outl(reg, TRID_REG(card, addr));
588
589 #ifdef DEBUG
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",
593                  reg, addr);
594 #endif /* DEBUG */
595 }
596
597 static void
598 trident_disable_voice_irq(struct trident_card *card, unsigned int channel)
599 {
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;
603
604         reg = inl(TRID_REG(card, addr));
605         reg &= ~mask;
606         outl(reg, TRID_REG(card, addr));
607
608         /* Ack the channel in case the interrupt was set before we disable it. */
609         outl(mask, TRID_REG(card, bank->addresses->aint));
610
611 #ifdef DEBUG
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",
615                  reg, addr);
616 #endif /* DEBUG */
617 }
618
619 static void
620 trident_start_voice(struct trident_card *card, unsigned int channel)
621 {
622         unsigned int mask = 1 << (channel & 0x1f);
623         struct trident_pcm_bank *bank = &card->banks[channel >> 5];
624         u32 addr = bank->addresses->start;
625
626 #ifdef DEBUG
627         u32 reg;
628 #endif /* DEBUG */
629
630         outl(mask, TRID_REG(card, addr));
631
632 #ifdef DEBUG
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",
636                  reg, addr);
637 #endif /* DEBUG */
638 }
639
640 static void
641 trident_stop_voice(struct trident_card *card, unsigned int channel)
642 {
643         unsigned int mask = 1 << (channel & 0x1f);
644         struct trident_pcm_bank *bank = &card->banks[channel >> 5];
645         u32 addr = bank->addresses->stop;
646
647 #ifdef DEBUG
648         u32 reg;
649 #endif /* DEBUG */
650
651         outl(mask, TRID_REG(card, addr));
652
653 #ifdef DEBUG
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",
657                  reg, addr);
658 #endif /* DEBUG */
659 }
660
661 static u32
662 trident_get_interrupt_mask(struct trident_card *card, unsigned int channel)
663 {
664         struct trident_pcm_bank *bank = &card->banks[channel];
665         u32 addr = bank->addresses->aint;
666         return inl(TRID_REG(card, addr));
667 }
668
669 static int
670 trident_check_channel_interrupt(struct trident_card *card, unsigned int channel)
671 {
672         unsigned int mask = 1 << (channel & 0x1f);
673         u32 reg = trident_get_interrupt_mask(card, channel >> 5);
674
675 #ifdef DEBUG
676         if (reg & mask)
677                 pr_debug("trident: channel %d has interrupt, %s = 0x%08x\n",
678                          channel, reg == T4D_AINT_B ? "AINT_B" : "AINT_A",
679                          reg);
680 #endif /* DEBUG */
681         return (reg & mask) ? 1 : 0;
682 }
683
684 static void
685 trident_ack_channel_interrupt(struct trident_card *card, unsigned int channel)
686 {
687         unsigned int mask = 1 << (channel & 0x1f);
688         struct trident_pcm_bank *bank = &card->banks[channel >> 5];
689         u32 reg, addr = bank->addresses->aint;
690
691         reg = inl(TRID_REG(card, addr));
692         reg &= mask;
693         outl(reg, TRID_REG(card, addr));
694
695 #ifdef DEBUG
696         reg = inl(TRID_REG(card, T4D_AINT_B));
697         pr_debug("trident: Ack channel %d interrupt, AINT_B = 0x%08x\n",
698                  channel, reg);
699 #endif /* DEBUG */
700 }
701
702 static struct trident_channel *
703 trident_alloc_pcm_channel(struct trident_card *card)
704 {
705         struct trident_pcm_bank *bank;
706         int idx;
707
708         bank = &card->banks[BANK_B];
709
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;
715                         return channel;
716                 }
717         }
718
719         /* no more free channels available */
720         printk(KERN_ERR "trident: no more channels available on Bank B.\n");
721         return NULL;
722 }
723
724 static void
725 trident_free_pcm_channel(struct trident_card *card, unsigned int channel)
726 {
727         int bank;
728         unsigned char b;
729
730         if (channel < 31 || channel > 63)
731                 return;
732
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));
738         }
739
740         bank = channel >> 5;
741         channel = channel & 0x1f;
742
743         card->banks[bank].bitmap &= ~(1 << (channel));
744 }
745
746 static struct trident_channel *
747 cyber_alloc_pcm_channel(struct trident_card *card)
748 {
749         struct trident_pcm_bank *bank;
750         int idx;
751
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 ? */
755
756         bank = &card->banks[BANK_A];
757
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;
762                         channel->num = idx;
763                         return channel;
764                 }
765         }
766
767         /* no more free channels available */
768         printk(KERN_ERR "cyberpro5050: no more channels available on Bank A.\n");
769         return NULL;
770 }
771
772 static void
773 cyber_free_pcm_channel(struct trident_card *card, unsigned int channel)
774 {
775         if (channel > 31)
776                 return;
777         card->banks[BANK_A].bitmap &= ~(1 << (channel));
778 }
779
780 static inline void
781 cyber_outidx(int port, int idx, int data)
782 {
783         outb(idx, port);
784         outb(data, port + 1);
785 }
786
787 static inline int
788 cyber_inidx(int port, int idx)
789 {
790         outb(idx, port);
791         return inb(port + 1);
792 }
793
794 static int
795 cyber_init_ritual(struct trident_card *card)
796 {
797         /* some black magic, taken from SDK samples */
798         /* remove this and nothing will work */
799         int portDat;
800         int ret = 0;
801         unsigned long flags;
802
803         /*
804          *      Keep interrupts off for the configure - we don't want to
805          *      clash with another cyberpro config event
806          */
807
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);
817         }
818         if ((portDat & CYBER_BMSK_AUENZ) != CYBER_BMSK_AUENZ_ENABLE) {
819                 printk(KERN_ERR "cyberpro5050: initAudioAccess: no success\n");
820                 ret = -1;
821         } else {
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);
830         }
831         spin_unlock_irqrestore(&card->lock, flags);
832         return ret;
833 }
834
835 /*  called with spin lock held */
836
837 static int
838 trident_load_channel_registers(struct trident_card *card, u32 * data, 
839                                unsigned int channel)
840 {
841         int i;
842
843         if (channel > 63)
844                 return 0;
845
846         /* select hardware channel to write */
847         outb(channel, TRID_REG(card, T4D_LFO_GC_CIR));
848
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)
853                         continue;
854                 outl(data[i], TRID_REG(card, CHANNEL_START + 4 * i));
855         }
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));
860         }
861         return 1;
862 }
863
864 /* called with spin lock held */
865 static int
866 trident_write_voice_regs(struct trident_state *state)
867 {
868         unsigned int data[CHANNEL_REGS + 1];
869         struct trident_channel *channel;
870
871         channel = state->dmabuf.channel;
872
873         data[1] = channel->lba;
874         data[4] = channel->control;
875
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);
880                 data[3] = 0;
881                 break;
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);
887                 break;
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;
892                 break;
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;
898                 break;
899         default:
900                 return 0;
901         }
902
903         return trident_load_channel_registers(state->card, data, channel->num);
904 }
905
906 static int
907 compute_rate_play(u32 rate)
908 {
909         int delta;
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 */
914         if (rate == 44100)
915                 delta = 0xeb3;
916         else if (rate == 8000)
917                 delta = 0x2ab;
918         else if (rate == 48000)
919                 delta = 0x1000;
920         else
921                 delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
922         return delta;
923 }
924
925 static int
926 compute_rate_rec(u32 rate)
927 {
928         int delta;
929
930         if (rate == 44100)
931                 delta = 0x116a;
932         else if (rate == 8000)
933                 delta = 0x6000;
934         else if (rate == 48000)
935                 delta = 0x1000;
936         else
937                 delta = ((48000 << 12) / rate) & 0x0000ffff;
938
939         return delta;
940 }
941
942 /* set playback sample rate */
943 static unsigned int
944 trident_set_dac_rate(struct trident_state *state, unsigned int rate)
945 {
946         struct dmabuf *dmabuf = &state->dmabuf;
947
948         if (rate > 48000)
949                 rate = 48000;
950         if (rate < 4000)
951                 rate = 4000;
952
953         dmabuf->rate = rate;
954         dmabuf->channel->delta = compute_rate_play(rate);
955
956         trident_write_voice_regs(state);
957
958         pr_debug("trident: called trident_set_dac_rate : rate = %d\n", rate);
959
960         return rate;
961 }
962
963 /* set recording sample rate */
964 static unsigned int
965 trident_set_adc_rate(struct trident_state *state, unsigned int rate)
966 {
967         struct dmabuf *dmabuf = &state->dmabuf;
968
969         if (rate > 48000)
970                 rate = 48000;
971         if (rate < 4000)
972                 rate = 4000;
973
974         dmabuf->rate = rate;
975         dmabuf->channel->delta = compute_rate_rec(rate);
976
977         trident_write_voice_regs(state);
978
979         pr_debug("trident: called trident_set_adc_rate : rate = %d\n", rate);
980
981         return rate;
982 }
983
984 /* prepare channel attributes for playback */
985 static void
986 trident_play_setup(struct trident_state *state)
987 {
988         struct dmabuf *dmabuf = &state->dmabuf;
989         struct trident_channel *channel = dmabuf->channel;
990
991         channel->lba = dmabuf->dma_handle;
992         channel->delta = compute_rate_play(dmabuf->rate);
993
994         channel->eso = dmabuf->dmasize >> sample_shift[dmabuf->fmt];
995         channel->eso -= 1;
996
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;
1009                         }
1010                 }
1011         }
1012
1013         channel->fm_vol = 0x0;
1014
1015         channel->control = CHANNEL_LOOP;
1016         if (dmabuf->fmt & TRIDENT_FMT_16BIT) {
1017                 /* 16-bits */
1018                 channel->control |= CHANNEL_16BITS;
1019                 /* signed */
1020                 channel->control |= CHANNEL_SIGNED;
1021         }
1022         if (dmabuf->fmt & TRIDENT_FMT_STEREO)
1023                 /* stereo */
1024                 channel->control |= CHANNEL_STEREO;
1025
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);
1029
1030         trident_write_voice_regs(state);
1031 }
1032
1033 /* prepare channel attributes for recording */
1034 static void
1035 trident_rec_setup(struct trident_state *state)
1036 {
1037         u16 w;
1038         u8 bval;
1039
1040         struct trident_card *card = state->card;
1041         struct dmabuf *dmabuf = &state->dmabuf;
1042         struct trident_channel *channel = dmabuf->channel;
1043         unsigned int rate;
1044
1045         /* Enable AC-97 ADC (capture) */
1046         switch (card->pci_id) {
1047         case PCI_DEVICE_ID_ALI_5451:
1048                 ali_enable_special_channel(state);
1049                 break;
1050         case PCI_DEVICE_ID_SI_7018:
1051                 /* for 7018, the ac97 is always in playback/record (duplex) mode */
1052                 break;
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));
1058                 break;
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));
1064                 break;
1065         case PCI_DEVICE_ID_INTERG_5050:
1066                 /* don't know yet, using special channel 22 in GC1(0xd4)? */
1067                 break;
1068         default:
1069                 return;
1070         }
1071
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);
1077                 if (rate == 0) {
1078                         printk(KERN_WARNING "trident: ALi 5451 "
1079                                "S/PDIF input setup error!\n");
1080                         rate = 48000;
1081                 }
1082                 bval = inb(TRID_REG(card, ALI_SPDIF_CTRL));
1083                 if (bval & 0x10) {
1084                         outb(bval, TRID_REG(card, ALI_SPDIF_CTRL));
1085                         printk(KERN_WARNING "trident: cleared ALi "
1086                                "5451 S/PDIF parity error flag.\n");
1087                 }
1088
1089                 if (rate != 48000)
1090                         channel->delta = ((rate << 12) / dmabuf->rate) & 0x0000ffff;
1091         }
1092
1093         channel->eso = dmabuf->dmasize >> sample_shift[dmabuf->fmt];
1094         channel->eso -= 1;
1095
1096         if (state->card->pci_id != PCI_DEVICE_ID_SI_7018) {
1097                 channel->attribute = 0;
1098         }
1099
1100         channel->fm_vol = 0x0;
1101
1102         channel->control = CHANNEL_LOOP;
1103         if (dmabuf->fmt & TRIDENT_FMT_16BIT) {
1104                 /* 16-bits */
1105                 channel->control |= CHANNEL_16BITS;
1106                 /* signed */
1107                 channel->control |= CHANNEL_SIGNED;
1108         }
1109         if (dmabuf->fmt & TRIDENT_FMT_STEREO)
1110                 /* stereo */
1111                 channel->control |= CHANNEL_STEREO;
1112
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);
1116
1117         trident_write_voice_regs(state);
1118 }
1119
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)
1124 {
1125         struct dmabuf *dmabuf = &state->dmabuf;
1126         u32 cso;
1127
1128         if (!dmabuf->enable)
1129                 return 0;
1130
1131         outb(dmabuf->channel->num, TRID_REG(state->card, T4D_LFO_GC_CIR));
1132
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));
1140                 break;
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;
1144                 break;
1145         default:
1146                 return 0;
1147         }
1148
1149         pr_debug("trident: trident_get_dma_addr: chip reported channel: %d, "
1150                  "cso = 0x%04x\n", dmabuf->channel->num, cso);
1151
1152         /* ESO and CSO are in units of Samples, convert to byte offset */
1153         cso <<= sample_shift[dmabuf->fmt];
1154
1155         return (cso % dmabuf->dmasize);
1156 }
1157
1158 /* Stop recording (lock held) */
1159 static inline void
1160 __stop_adc(struct trident_state *state)
1161 {
1162         struct dmabuf *dmabuf = &state->dmabuf;
1163         unsigned int chan_num = dmabuf->channel->num;
1164         struct trident_card *card = state->card;
1165
1166         dmabuf->enable &= ~ADC_RUNNING;
1167         trident_stop_voice(card, chan_num);
1168         trident_disable_voice_irq(card, chan_num);
1169 }
1170
1171 static void
1172 stop_adc(struct trident_state *state)
1173 {
1174         struct trident_card *card = state->card;
1175         unsigned long flags;
1176
1177         spin_lock_irqsave(&card->lock, flags);
1178         __stop_adc(state);
1179         spin_unlock_irqrestore(&card->lock, flags);
1180 }
1181
1182 static void
1183 start_adc(struct trident_state *state)
1184 {
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;
1189
1190         spin_lock_irqsave(&card->lock, flags);
1191         if ((dmabuf->mapped || 
1192              dmabuf->count < (signed) dmabuf->dmasize) && 
1193             dmabuf->ready) {
1194                 dmabuf->enable |= ADC_RUNNING;
1195                 trident_enable_voice_irq(card, chan_num);
1196                 trident_start_voice(card, chan_num);
1197         }
1198         spin_unlock_irqrestore(&card->lock, flags);
1199 }
1200
1201 /* stop playback (lock held) */
1202 static inline void
1203 __stop_dac(struct trident_state *state)
1204 {
1205         struct dmabuf *dmabuf = &state->dmabuf;
1206         unsigned int chan_num = dmabuf->channel->num;
1207         struct trident_card *card = state->card;
1208
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);
1220         }
1221         trident_disable_voice_irq(card, chan_num);
1222 }
1223
1224 static void
1225 stop_dac(struct trident_state *state)
1226 {
1227         struct trident_card *card = state->card;
1228         unsigned long flags;
1229
1230         spin_lock_irqsave(&card->lock, flags);
1231         __stop_dac(state);
1232         spin_unlock_irqrestore(&card->lock, flags);
1233 }
1234
1235 static void
1236 start_dac(struct trident_state *state)
1237 {
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;
1242
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);
1257                 }
1258         }
1259         spin_unlock_irqrestore(&card->lock, flags);
1260 }
1261
1262 #define DMABUF_DEFAULTORDER (15-PAGE_SHIFT)
1263 #define DMABUF_MINORDER 1
1264
1265 /* alloc a DMA buffer of with a buffer of this order */
1266 static int
1267 alloc_dmabuf(struct dmabuf *dmabuf, struct pci_dev *pci_dev, int order)
1268 {
1269         void *rawbuf = NULL;
1270         struct page *page, *pend;
1271
1272         if (!(rawbuf = pci_alloc_consistent(pci_dev, PAGE_SIZE << order, 
1273                                             &dmabuf->dma_handle)))
1274                 return -ENOMEM;
1275
1276         pr_debug("trident: allocated %ld (order = %d) bytes at %p\n",
1277                  PAGE_SIZE << order, order, rawbuf);
1278
1279         dmabuf->ready = dmabuf->mapped = 0;
1280         dmabuf->rawbuf = rawbuf;
1281         dmabuf->buforder = order;
1282
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);
1288
1289         return 0;
1290 }
1291
1292 /* allocate the main DMA buffer, playback and recording buffer should be */
1293 /* allocated separately */
1294 static int
1295 alloc_main_dmabuf(struct trident_state *state)
1296 {
1297         struct dmabuf *dmabuf = &state->dmabuf;
1298         int order;
1299         int ret = -ENOMEM;
1300
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)))
1304                         return 0;
1305                 /* else try again */
1306         }
1307         return ret;
1308 }
1309
1310 /* deallocate a DMA buffer */
1311 static void
1312 dealloc_dmabuf(struct dmabuf *dmabuf, struct pci_dev *pci_dev)
1313 {
1314         struct page *page, *pend;
1315
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;
1324         }
1325         dmabuf->mapped = dmabuf->ready = 0;
1326 }
1327
1328 static int
1329 prog_dmabuf(struct trident_state *state, enum dmabuf_mode rec)
1330 {
1331         struct dmabuf *dmabuf = &state->dmabuf;
1332         unsigned bytepersec;
1333         struct trident_state *s = state;
1334         unsigned bufsize, dma_nums;
1335         unsigned long flags;
1336         int ret, i, order;
1337
1338         if ((ret = lock_set_fmt(state)) < 0)
1339                 return ret;
1340
1341         if (state->chans_num == 6)
1342                 dma_nums = 5;
1343         else
1344                 dma_nums = 1;
1345
1346         for (i = 0; i < dma_nums; i++) {
1347                 if (i > 0) {
1348                         s = state->other_states[i - 1];
1349                         dmabuf = &s->dmabuf;
1350                         dmabuf->fmt = state->dmabuf.fmt;
1351                         dmabuf->rate = state->dmabuf.rate;
1352                 }
1353
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);
1358
1359                 /* allocate DMA buffer if not allocated yet */
1360                 if (!dmabuf->rawbuf) {
1361                         if (i == 0) {
1362                                 if ((ret = alloc_main_dmabuf(state))) {
1363                                         unlock_set_fmt(state);
1364                                         return ret;
1365                                 }
1366                         } else {
1367                                 ret = -ENOMEM;
1368                                 order = state->dmabuf.buforder - 1;
1369                                 if (order >= DMABUF_MINORDER) {
1370                                         ret = alloc_dmabuf(dmabuf,
1371                                                            state->card->pci_dev,
1372                                                            order);
1373                                 }
1374                                 if (ret) {
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);
1382                                         return ret;
1383                                 }
1384                         }
1385                 }
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);
1392                         else
1393                                 dmabuf->fragshift = dmabuf->ossfragshift;
1394                 } else {
1395                         /* lets hand out reasonable big ass buffers by default */
1396                         dmabuf->fragshift = (dmabuf->buforder + PAGE_SHIFT - 2);
1397                 }
1398                 dmabuf->numfrag = bufsize >> dmabuf->fragshift;
1399                 while (dmabuf->numfrag < 4 && dmabuf->fragshift > 3) {
1400                         dmabuf->fragshift--;
1401                         dmabuf->numfrag = bufsize >> dmabuf->fragshift;
1402                 }
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;
1408
1409                 memset(dmabuf->rawbuf, (dmabuf->fmt & TRIDENT_FMT_16BIT) ? 0 : 0x80, 
1410                        dmabuf->dmasize);
1411
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);
1417
1418                 spin_unlock_irqrestore(&s->card->lock, flags);
1419
1420                 /* set the ready flag for the dma buffer */
1421                 dmabuf->ready = 1;
1422
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);
1428         }
1429         unlock_set_fmt(state);
1430         return 0;
1431 }
1432
1433
1434 static inline int prog_dmabuf_record(struct trident_state* state)
1435 {
1436         return prog_dmabuf(state, DM_RECORD);
1437 }
1438
1439 static inline int prog_dmabuf_playback(struct trident_state* state)
1440 {
1441         return prog_dmabuf(state, DM_PLAYBACK);
1442 }
1443
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|
1450 */
1451 static void
1452 trident_clear_tail(struct trident_state *state)
1453 {
1454         struct dmabuf *dmabuf = &state->dmabuf;
1455         unsigned swptr;
1456         unsigned char silence = (dmabuf->fmt & TRIDENT_FMT_16BIT) ? 0 : 0x80;
1457         unsigned int len;
1458         unsigned long flags;
1459
1460         spin_lock_irqsave(&state->card->lock, flags);
1461         swptr = dmabuf->swptr;
1462         spin_unlock_irqrestore(&state->card->lock, flags);
1463
1464         if (swptr == 0 || swptr == dmabuf->dmasize / 2 || 
1465             swptr == dmabuf->dmasize)
1466                 return;
1467
1468         if (swptr < dmabuf->dmasize / 2)
1469                 len = dmabuf->dmasize / 2 - swptr;
1470         else
1471                 len = dmabuf->dmasize - swptr;
1472
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);
1479         }
1480
1481         /* restart the dma machine in case it is halted */
1482         start_dac(state);
1483 }
1484
1485 static int
1486 drain_dac(struct trident_state *state, int nonblock)
1487 {
1488         DECLARE_WAITQUEUE(wait, current);
1489         struct dmabuf *dmabuf = &state->dmabuf;
1490         unsigned long flags;
1491         unsigned long tmo;
1492         int count;
1493         unsigned long diff = 0;
1494
1495         if (dmabuf->mapped || !dmabuf->ready)
1496                 return 0;
1497
1498         add_wait_queue(&dmabuf->wait, &wait);
1499         for (;;) {
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);
1503
1504                 spin_lock_irqsave(&state->card->lock, flags);
1505                 count = dmabuf->count;
1506                 spin_unlock_irqrestore(&state->card->lock, flags);
1507
1508                 if (count <= 0)
1509                         break;
1510
1511                 if (signal_pending(current))
1512                         break;
1513
1514                 if (nonblock) {
1515                         remove_wait_queue(&dmabuf->wait, &wait);
1516                         set_current_state(TASK_RUNNING);
1517                         return -EBUSY;
1518                 }
1519
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;
1527                 } else {
1528                         tmo = (dmabuf->dmasize * HZ) / dmabuf->rate;
1529                 }
1530                 tmo >>= sample_shift[dmabuf->fmt];
1531                 if (!schedule_timeout(tmo ? tmo : 1) && tmo) {
1532                         break;
1533                 }
1534         }
1535         remove_wait_queue(&dmabuf->wait, &wait);
1536         set_current_state(TASK_RUNNING);
1537         if (signal_pending(current))
1538                 return -ERESTARTSYS;
1539
1540         return 0;
1541 }
1542
1543 /* update buffer manangement pointers, especially, */ 
1544 /* dmabuf->count and dmabuf->hwptr */
1545 static void
1546 trident_update_ptr(struct trident_state *state)
1547 {
1548         struct dmabuf *dmabuf = &state->dmabuf;
1549         unsigned hwptr, swptr;
1550         int clear_cnt = 0;
1551         int diff;
1552         unsigned char silence;
1553         unsigned half_dmasize;
1554
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;
1560
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);
1567                 } else {
1568                         dmabuf->count += diff;
1569
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 */
1576                                 __stop_adc(state);
1577                                 dmabuf->error++;
1578                         }
1579                         if (dmabuf->count < (signed) dmabuf->dmasize / 2)
1580                                 wake_up(&dmabuf->wait);
1581                 }
1582         }
1583
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);
1590                 } else {
1591                         dmabuf->count -= diff;
1592
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 */
1598                                 __stop_dac(state);
1599                                 dmabuf->error++;
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)
1609                                                 diff = hwptr;
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;
1618                                                         swptr = swptr / 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);
1627                                                 }
1628                                                 dmabuf->endcleared = 1;
1629                                         }
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;
1637                                                 swptr = swptr / 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);
1646                                         }
1647                                         dmabuf->endcleared = 1;
1648                                 }
1649                         }
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);
1655                 }
1656         }
1657         dmabuf->update_flag &= ~ALI_ADDRESS_INT_UPDATE;
1658 }
1659
1660 static void
1661 trident_address_interrupt(struct trident_card *card)
1662 {
1663         int i;
1664         struct trident_state *state;
1665         unsigned int channel;
1666
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++) {
1670                 channel = 63 - 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);
1675                         } else {
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);
1680                         }
1681                 }
1682         }
1683 }
1684
1685 static void
1686 ali_hwvol_control(struct trident_card *card, int opt)
1687 {
1688         u16 dwTemp, volume[2], mute, diff, *pVol[2];
1689
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];
1697         } else {
1698                 pVol[1] = &volume[0];
1699                 pVol[0] = &volume[1];
1700         }
1701         diff = *(pVol[1]) - *(pVol[0]);
1702
1703         if (opt == 1) {         // MUTE
1704                 dwTemp ^= 0x8000;
1705                 ali_ac97_write(card->ac97_codec[0], 
1706                                0x02, dwTemp);
1707         } else if (opt == 2) {  // Down
1708                 if (mute)
1709                         return;
1710                 if (*(pVol[1]) < 0x001f) {
1711                         (*pVol[1])++;
1712                         *(pVol[0]) = *(pVol[1]) - diff;
1713                 }
1714                 dwTemp &= 0xe0e0;
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
1720                 if (mute)
1721                         return;
1722                 if (*(pVol[0]) > 0) {
1723                         (*pVol[0])--;
1724                         *(pVol[1]) = *(pVol[0]) + diff;
1725                 }
1726                 dwTemp &= 0xe0e0;
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);
1731         } else {
1732                 /* Nothing needs doing */
1733         }
1734 }
1735
1736 /*
1737  *      Re-enable reporting of vol change after 0.1 seconds
1738  */
1739
1740 static void
1741 ali_timeout(unsigned long ptr)
1742 {
1743         struct trident_card *card = (struct trident_card *) ptr;
1744         u16 temp = 0;
1745
1746         /* Enable GPIO IRQ (MISCINT bit 18h) */
1747         temp = inw(TRID_REG(card, T4D_MISCINT + 2));
1748         temp |= 0x0004;
1749         outw(temp, TRID_REG(card, T4D_MISCINT + 2));
1750 }
1751
1752 /*
1753  *      Set up the timer to clear the vol change notification
1754  */
1755
1756 static void
1757 ali_set_timer(struct trident_card *card)
1758 {
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);
1765 }
1766
1767 /*
1768  *      Process a GPIO event
1769  */
1770
1771 static void
1772 ali_queue_task(struct trident_card *card, int opt)
1773 {
1774         u16 temp;
1775
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));
1780
1781         /* Adjust the volume */
1782         ali_hwvol_control(card, opt);
1783
1784         /* Set the timer for 1/10th sec */
1785         ali_set_timer(card);
1786 }
1787
1788 static void
1789 cyber_address_interrupt(struct trident_card *card)
1790 {
1791         int i, irq_status;
1792         struct trident_state *state;
1793         unsigned int channel;
1794
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));
1798
1799         pr_debug("cyber_address_interrupt: irq_status 0x%X\n", irq_status);
1800
1801         for (i = 0; i < NR_HW_CH; i++) {
1802                 channel = 31 - 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));
1806
1807                         pr_debug("cyber_interrupt: channel %d\n", channel);
1808
1809                         if ((state = card->states[i]) != NULL) {
1810                                 trident_update_ptr(state);
1811                         } else {
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);
1816                         }
1817                 }
1818         }
1819 }
1820
1821 static irqreturn_t
1822 trident_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1823 {
1824         struct trident_card *card = (struct trident_card *) dev_id;
1825         u32 event;
1826         u32 gpio;
1827
1828         spin_lock(&card->lock);
1829         event = inl(TRID_REG(card, T4D_MISCINT));
1830
1831         pr_debug("trident: trident_interrupt called, MISCINT = 0x%08x\n",
1832                  event);
1833
1834         if (event & ADDRESS_IRQ) {
1835                 card->address_interrupt(card);
1836         }
1837
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);
1845                 }
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);
1850                 return IRQ_HANDLED;
1851         }
1852
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);
1857         return IRQ_HANDLED;
1858 }
1859
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. */
1863 static ssize_t
1864 trident_read(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
1865 {
1866         struct trident_state *state = (struct trident_state *)file->private_data;
1867         struct dmabuf *dmabuf = &state->dmabuf;
1868         ssize_t ret = 0;
1869         unsigned long flags;
1870         unsigned swptr;
1871         int cnt;
1872
1873         pr_debug("trident: trident_read called, count = %d\n", count);
1874
1875         VALIDATE_STATE(state);
1876
1877         if (dmabuf->mapped)
1878                 return -ENXIO;
1879         if (!access_ok(VERIFY_WRITE, buffer, count))
1880                 return -EFAULT;
1881
1882         down(&state->sem);
1883         if (!dmabuf->ready && (ret = prog_dmabuf_record(state)))
1884                 goto out;
1885
1886         while (count > 0) {
1887                 spin_lock_irqsave(&state->card->lock, flags);
1888                 if (dmabuf->count > (signed) dmabuf->dmasize) {
1889                         /* buffer overrun, we are recovering from */ 
1890                         /* sleep_on_timeout, resync hwptr and swptr, */ 
1891                         /* make process flush the buffer */
1892                         dmabuf->count = dmabuf->dmasize;
1893                         dmabuf->swptr = dmabuf->hwptr;
1894                 }
1895                 swptr = dmabuf->swptr;
1896                 cnt = dmabuf->dmasize - swptr;
1897                 if (dmabuf->count < cnt)
1898                         cnt = dmabuf->count;
1899                 spin_unlock_irqrestore(&state->card->lock, flags);
1900
1901                 if (cnt > count)
1902                         cnt = count;
1903                 if (cnt <= 0) {
1904                         unsigned long tmo;
1905                         /* buffer is empty, start the dma machine and */ 
1906                         /* wait for data to be recorded */
1907                         start_adc(state);
1908                         if (file->f_flags & O_NONBLOCK) {
1909                                 if (!ret)
1910                                         ret = -EAGAIN;
1911                                 goto out;
1912                         }
1913
1914                         up(&state->sem);
1915                         /* No matter how much space left in the buffer, */ 
1916                         /* we have to wait until CSO == ESO/2 or CSO == ESO */ 
1917                         /* when address engine interrupts */
1918                         tmo = (dmabuf->dmasize * HZ) / (dmabuf->rate * 2);
1919                         tmo >>= sample_shift[dmabuf->fmt];
1920                         /* There are two situations when sleep_on_timeout returns, one is when
1921                            the interrupt is serviced correctly and the process is waked up by
1922                            ISR ON TIME. Another is when timeout is expired, which means that
1923                            either interrupt is NOT serviced correctly (pending interrupt) or it
1924                            is TOO LATE for the process to be scheduled to run (scheduler latency)
1925                            which results in a (potential) buffer overrun. And worse, there is
1926                            NOTHING we can do to prevent it. */
1927                         if (!interruptible_sleep_on_timeout(&dmabuf->wait, tmo)) {
1928                                 pr_debug(KERN_ERR "trident: recording schedule timeout, "
1929                                          "dmasz %u fragsz %u count %i hwptr %u swptr %u\n",
1930                                          dmabuf->dmasize, dmabuf->fragsize, dmabuf->count,
1931                                          dmabuf->hwptr, dmabuf->swptr);
1932
1933                                 /* a buffer overrun, we delay the recovery until next time the
1934                                    while loop begin and we REALLY have space to record */
1935                         }
1936                         if (signal_pending(current)) {
1937                                 if (!ret)
1938                                         ret = -ERESTARTSYS;
1939                                 goto out;
1940                         }
1941                         down(&state->sem);
1942                         if (dmabuf->mapped) {
1943                                 if (!ret)
1944                                         ret = -ENXIO;
1945                                 goto out;
1946                         }
1947                         continue;
1948                 }
1949
1950                 if (copy_to_user(buffer, dmabuf->rawbuf + swptr, cnt)) {
1951                         if (!ret)
1952                                 ret = -EFAULT;
1953                         goto out;
1954                 }
1955
1956                 swptr = (swptr + cnt) % dmabuf->dmasize;
1957
1958                 spin_lock_irqsave(&state->card->lock, flags);
1959                 dmabuf->swptr = swptr;
1960                 dmabuf->count -= cnt;
1961                 spin_unlock_irqrestore(&state->card->lock, flags);
1962
1963                 count -= cnt;
1964                 buffer += cnt;
1965                 ret += cnt;
1966                 start_adc(state);
1967         }
1968 out:
1969         up(&state->sem);
1970         return ret;
1971 }
1972
1973 /* in this loop, dmabuf.count signifies the amount of data that is waiting to be dma to
1974    the soundcard.  it is drained by the dma machine and filled by this loop. */
1975
1976 static ssize_t
1977 trident_write(struct file *file, const char __user *buffer, size_t count, loff_t * ppos)
1978 {
1979         struct trident_state *state = (struct trident_state *)file->private_data;
1980         struct dmabuf *dmabuf = &state->dmabuf;
1981         ssize_t ret;
1982         unsigned long flags;
1983         unsigned swptr;
1984         int cnt;
1985         unsigned int state_cnt;
1986         unsigned int copy_count;
1987         int lret; /* for lock_set_fmt */
1988
1989         pr_debug("trident: trident_write called, count = %d\n", count);
1990
1991         VALIDATE_STATE(state);
1992
1993         /*
1994          *      Guard against an mmap or ioctl while writing
1995          */
1996
1997         down(&state->sem);
1998
1999         if (dmabuf->mapped) {
2000                 ret = -ENXIO;
2001                 goto out;
2002         }
2003         if (!dmabuf->ready && (ret = prog_dmabuf_playback(state)))
2004                 goto out;
2005
2006         if (!access_ok(VERIFY_READ, buffer, count)) {
2007                 ret = -EFAULT;
2008                 goto out;
2009         }
2010
2011         ret = 0;
2012
2013         while (count > 0) {
2014                 spin_lock_irqsave(&state->card->lock, flags);
2015                 if (dmabuf->count < 0) {
2016                         /* buffer underrun, we are recovering from */ 
2017                         /* sleep_on_timeout, resync hwptr and swptr */
2018                         dmabuf->count = 0;
2019                         dmabuf->swptr = dmabuf->hwptr;
2020                 }
2021                 swptr = dmabuf->swptr;
2022                 cnt = dmabuf->dmasize - swptr;
2023                 if (dmabuf->count + cnt > dmabuf->dmasize)
2024                         cnt = dmabuf->dmasize - dmabuf->count;
2025                 spin_unlock_irqrestore(&state->card->lock, flags);
2026
2027                 if (cnt > count)
2028                         cnt = count;
2029                 if (cnt <= 0) {
2030                         unsigned long tmo;
2031                         /* buffer is full, start the dma machine and */ 
2032                         /* wait for data to be played */
2033                         start_dac(state);
2034                         if (file->f_flags & O_NONBLOCK) {
2035                                 if (!ret)
2036                                         ret = -EAGAIN;
2037                                 goto out;
2038                         }
2039                         /* No matter how much data left in the buffer, */ 
2040                         /* we have to wait until CSO == ESO/2 or CSO == ESO */ 
2041                         /* when address engine interrupts */
2042                         lock_set_fmt(state);
2043                         tmo = (dmabuf->dmasize * HZ) / (dmabuf->rate * 2);
2044                         tmo >>= sample_shift[dmabuf->fmt];
2045                         unlock_set_fmt(state);
2046                         up(&state->sem);
2047
2048                         /* There are two situations when sleep_on_timeout */ 
2049                         /* returns, one is when the interrupt is serviced */ 
2050                         /* correctly and the process is waked up by ISR */ 
2051                         /* ON TIME. Another is when timeout is expired, which */ 
2052                         /* means that either interrupt is NOT serviced */ 
2053                         /* correctly (pending interrupt) or it is TOO LATE */ 
2054                         /* for the process to be scheduled to run */ 
2055                         /* (scheduler latency) which results in a (potential) */ 
2056                         /* buffer underrun. And worse, there is NOTHING we */ 
2057                         /* can do to prevent it. */
2058                         if (!interruptible_sleep_on_timeout(&dmabuf->wait, tmo)) {
2059                                 pr_debug(KERN_ERR "trident: playback schedule "
2060                                          "timeout, dmasz %u fragsz %u count %i "
2061                                          "hwptr %u swptr %u\n", dmabuf->dmasize,
2062                                          dmabuf->fragsize, dmabuf->count,
2063                                          dmabuf->hwptr, dmabuf->swptr);
2064
2065                                 /* a buffer underrun, we delay the recovery */ 
2066                                 /* until next time the while loop begin and */ 
2067                                 /* we REALLY have data to play */
2068                         }
2069                         if (signal_pending(current)) {
2070                                 if (!ret)
2071                                         ret = -ERESTARTSYS;
2072                                 goto out_nolock;
2073                         }
2074                         down(&state->sem);
2075                         if (dmabuf->mapped) {
2076                                 if (!ret)
2077                                         ret = -ENXIO;
2078                                 goto out;
2079                         }
2080                         continue;
2081                 }
2082                 if ((lret = lock_set_fmt(state)) < 0) {
2083                         ret = lret;
2084                         goto out;
2085                 }
2086
2087                 if (state->chans_num == 6) {
2088                         copy_count = 0;
2089                         state_cnt = 0;
2090                         if (ali_write_5_1(state, buffer, cnt, &copy_count, 
2091                                           &state_cnt) == -EFAULT) {
2092                                 if (state_cnt) {
2093                                         swptr = (swptr + state_cnt) % dmabuf->dmasize;
2094                                         spin_lock_irqsave(&state->card->lock, flags);
2095                                         dmabuf->swptr = swptr;
2096                                         dmabuf->count += state_cnt;
2097                                         dmabuf->endcleared = 0;
2098                                         spin_unlock_irqrestore(&state->card->lock, flags);
2099                                 }
2100                                 ret += copy_count;
2101                                 if (!ret)
2102                                         ret = -EFAULT;
2103                                 unlock_set_fmt(state);
2104                                 goto out;
2105                         }
2106                 } else {
2107                         if (copy_from_user(dmabuf->rawbuf + swptr, 
2108                                            buffer, cnt)) {
2109                                 if (!ret)
2110                                         ret = -EFAULT;
2111                                 unlock_set_fmt(state);
2112                                 goto out;
2113                         }
2114                         state_cnt = cnt;
2115                 }
2116                 unlock_set_fmt(state);
2117
2118                 swptr = (swptr + state_cnt) % dmabuf->dmasize;
2119
2120                 spin_lock_irqsave(&state->card->lock, flags);
2121                 dmabuf->swptr = swptr;
2122                 dmabuf->count += state_cnt;
2123                 dmabuf->endcleared = 0;
2124                 spin_unlock_irqrestore(&state->card->lock, flags);
2125
2126                 count -= cnt;
2127                 buffer += cnt;
2128                 ret += cnt;
2129                 start_dac(state);
2130         }
2131 out:
2132         up(&state->sem);
2133 out_nolock:
2134         return ret;
2135 }
2136
2137 /* No kernel lock - we have our own spinlock */
2138 static unsigned int
2139 trident_poll(struct file *file, struct poll_table_struct *wait)
2140 {
2141         struct trident_state *state = (struct trident_state *)file->private_data;
2142         struct dmabuf *dmabuf = &state->dmabuf;
2143         unsigned long flags;
2144         unsigned int mask = 0;
2145
2146         VALIDATE_STATE(state);
2147
2148         /*
2149          *      Guard against a parallel poll and write causing multiple
2150          *      prog_dmabuf events
2151          */
2152
2153         down(&state->sem);
2154
2155         if (file->f_mode & FMODE_WRITE) {
2156                 if (!dmabuf->ready && prog_dmabuf_playback(state)) {
2157                         up(&state->sem);
2158                         return 0;
2159                 }
2160                 poll_wait(file, &dmabuf->wait, wait);
2161         }
2162         if (file->f_mode & FMODE_READ) {
2163                 if (!dmabuf->ready && prog_dmabuf_record(state)) {
2164                         up(&state->sem);
2165                         return 0;
2166                 }
2167                 poll_wait(file, &dmabuf->wait, wait);
2168         }
2169
2170         up(&state->sem);
2171
2172         spin_lock_irqsave(&state->card->lock, flags);
2173         trident_update_ptr(state);
2174         if (file->f_mode & FMODE_READ) {
2175                 if (dmabuf->count >= (signed) dmabuf->fragsize)
2176                         mask |= POLLIN | POLLRDNORM;
2177         }
2178         if (file->f_mode & FMODE_WRITE) {
2179                 if (dmabuf->mapped) {
2180                         if (dmabuf->count >= (signed) dmabuf->fragsize)
2181                                 mask |= POLLOUT | POLLWRNORM;
2182                 } else {
2183                         if ((signed) dmabuf->dmasize >= dmabuf->count + 
2184                             (signed) dmabuf->fragsize)
2185                                 mask |= POLLOUT | POLLWRNORM;
2186                 }
2187         }
2188         spin_unlock_irqrestore(&state->card->lock, flags);
2189
2190         return mask;
2191 }
2192
2193 static int
2194 trident_mmap(struct file *file, struct vm_area_struct *vma)
2195 {
2196         struct trident_state *state = (struct trident_state *)file->private_data;
2197         struct dmabuf *dmabuf = &state->dmabuf;
2198         int ret = -EINVAL;
2199         unsigned long size;
2200
2201         VALIDATE_STATE(state);
2202
2203         /*
2204          *      Lock against poll read write or mmap creating buffers. Also lock
2205          *      a read or write against an mmap.
2206          */
2207
2208         down(&state->sem);
2209
2210         if (vma->vm_flags & VM_WRITE) {
2211                 if ((ret = prog_dmabuf_playback(state)) != 0)
2212                         goto out;
2213         } else if (vma->vm_flags & VM_READ) {
2214                 if ((ret = prog_dmabuf_record(state)) != 0)
2215                         goto out;
2216         } else
2217                 goto out;
2218
2219         ret = -EINVAL;
2220         if (vma->vm_pgoff != 0)
2221                 goto out;
2222         size = vma->vm_end - vma->vm_start;
2223         if (size > (PAGE_SIZE << dmabuf->buforder))
2224                 goto out;
2225         ret = -EAGAIN;
2226         if (remap_page_range(vma, vma->vm_start, virt_to_phys(dmabuf->rawbuf), 
2227                              size, vma->vm_page_prot))
2228                 goto out;
2229         dmabuf->mapped = 1;
2230         ret = 0;
2231 out:
2232         up(&state->sem);
2233         return ret;
2234 }
2235
2236 static int
2237 trident_ioctl(struct inode *inode, struct file *file, 
2238               unsigned int cmd, unsigned long arg)
2239 {
2240         struct trident_state *state = (struct trident_state *)file->private_data;
2241         struct dmabuf *dmabuf = &state->dmabuf;
2242         unsigned long flags;
2243         audio_buf_info abinfo;
2244         count_info cinfo;
2245         int val, mapped, ret = 0;
2246         struct trident_card *card = state->card;
2247         void __user *argp = (void __user *)arg;
2248         int __user *p = argp;
2249
2250         VALIDATE_STATE(state);
2251
2252
2253         mapped = ((file->f_mode & (FMODE_WRITE | FMODE_READ)) && dmabuf->mapped);
2254
2255         pr_debug("trident: trident_ioctl, command = %2d, arg = 0x%08x\n",
2256                  _IOC_NR(cmd), arg ? *p : 0);
2257
2258         switch (cmd) {
2259         case OSS_GETVERSION:
2260                 ret = put_user(SOUND_VERSION, p);
2261                 break;
2262
2263         case SNDCTL_DSP_RESET:
2264                 /* FIXME: spin_lock ? */
2265                 if (file->f_mode & FMODE_WRITE) {
2266                         stop_dac(state);
2267                         synchronize_irq(card->irq);
2268                         dmabuf->ready = 0;
2269                         dmabuf->swptr = dmabuf->hwptr = 0;
2270                         dmabuf->count = dmabuf->total_bytes = 0;
2271                 }
2272                 if (file->f_mode & FMODE_READ) {
2273                         stop_adc(state);
2274                         synchronize_irq(card->irq);
2275                         dmabuf->ready = 0;
2276                         dmabuf->swptr = dmabuf->hwptr = 0;
2277                         dmabuf->count = dmabuf->total_bytes = 0;
2278                 }
2279                 break;
2280
2281         case SNDCTL_DSP_SYNC:
2282                 if (file->f_mode & FMODE_WRITE)
2283                         ret = drain_dac(state, file->f_flags & O_NONBLOCK);
2284                 break;
2285
2286         case SNDCTL_DSP_SPEED:  /* set smaple rate */
2287                 if (get_user(val, p)) {
2288                         ret = -EFAULT;
2289                         break;
2290                 }
2291                 if (val >= 0) {
2292                         if (file->f_mode & FMODE_WRITE) {
2293                                 stop_dac(state);
2294                                 dmabuf->ready = 0;
2295                                 spin_lock_irqsave(&state->card->lock, flags);
2296                                 trident_set_dac_rate(state, val);
2297                                 spin_unlock_irqrestore(&state->card->lock, flags);
2298                         }
2299                         if (file->f_mode & FMODE_READ) {
2300                                 stop_adc(state);
2301                                 dmabuf->ready = 0;
2302                                 spin_lock_irqsave(&state->card->lock, flags);
2303                                 trident_set_adc_rate(state, val);
2304                                 spin_unlock_irqrestore(&state->card->lock, flags);
2305                         }
2306                 }
2307                 ret = put_user(dmabuf->rate, p);
2308                 break;
2309
2310         case SNDCTL_DSP_STEREO: /* set stereo or mono channel */
2311                 if (get_user(val, p)) {
2312                         ret = -EFAULT;
2313                         break;
2314                 }
2315                 if ((ret = lock_set_fmt(state)) < 0)
2316                         return ret;
2317
2318                 if (file->f_mode & FMODE_WRITE) {
2319                         stop_dac(state);
2320                         dmabuf->ready = 0;
2321                         if (val)
2322                                 dmabuf->fmt |= TRIDENT_FMT_STEREO;
2323                         else
2324                                 dmabuf->fmt &= ~TRIDENT_FMT_STEREO;
2325                 }
2326                 if (file->f_mode & FMODE_READ) {
2327                         stop_adc(state);
2328                         dmabuf->ready = 0;
2329                         if (val)
2330                                 dmabuf->fmt |= TRIDENT_FMT_STEREO;
2331                         else
2332                                 dmabuf->fmt &= ~TRIDENT_FMT_STEREO;
2333                 }
2334                 unlock_set_fmt(state);
2335                 break;
2336
2337         case SNDCTL_DSP_GETBLKSIZE:
2338                 if (file->f_mode & FMODE_WRITE) {
2339                         if ((val = prog_dmabuf_playback(state)))
2340                                 ret = val;
2341                         else
2342                                 ret = put_user(dmabuf->fragsize, p);
2343                         break;
2344                 }
2345                 if (file->f_mode & FMODE_READ) {
2346                         if ((val = prog_dmabuf_record(state)))
2347                                 ret = val;
2348                         else
2349                                 ret = put_user(dmabuf->fragsize, p);
2350                         break;
2351                 }
2352                 /* neither READ nor WRITE? is this even possible? */
2353                 ret = -EINVAL;
2354                 break;
2355
2356
2357         case SNDCTL_DSP_GETFMTS: /* Returns a mask of supported sample format */
2358                 ret = put_user(AFMT_S16_LE | AFMT_U16_LE | AFMT_S8 | 
2359                                AFMT_U8, p);
2360                 break;
2361
2362         case SNDCTL_DSP_SETFMT: /* Select sample format */
2363                 if (get_user(val, p)) {
2364                         ret = -EFAULT;
2365                         break;
2366                 }
2367                 if ((ret = lock_set_fmt(state)) < 0)
2368                         return ret;
2369
2370                 if (val != AFMT_QUERY) {
2371                         if (file->f_mode & FMODE_WRITE) {
2372                                 stop_dac(state);
2373                                 dmabuf->ready = 0;
2374                                 if (val == AFMT_S16_LE)
2375                                         dmabuf->fmt |= TRIDENT_FMT_16BIT;
2376                                 else
2377                                         dmabuf->fmt &= ~TRIDENT_FMT_16BIT;
2378                         }
2379                         if (file->f_mode & FMODE_READ) {
2380                                 stop_adc(state);
2381                                 dmabuf->ready = 0;
2382                                 if (val == AFMT_S16_LE)
2383                                         dmabuf->fmt |= TRIDENT_FMT_16BIT;
2384                                 else
2385                                         dmabuf->fmt &= ~TRIDENT_FMT_16BIT;
2386                         }
2387                 }
2388                 unlock_set_fmt(state);
2389                 ret = put_user((dmabuf->fmt & TRIDENT_FMT_16BIT) ? AFMT_S16_LE : 
2390                                AFMT_U8, p);
2391                 break;
2392
2393         case SNDCTL_DSP_CHANNELS:
2394                 if (get_user(val, p)) {
2395                         ret = -EFAULT;
2396                         break;
2397                 }
2398                 if (val != 0) {
2399                         if ((ret = lock_set_fmt(state)) < 0)
2400                                 return ret;
2401
2402                         if (file->f_mode & FMODE_WRITE) {
2403                                 stop_dac(state);
2404                                 dmabuf->ready = 0;
2405
2406                                 //prevent from memory leak
2407                                 if ((state->chans_num > 2) && (state->chans_num != val)) {
2408                                         ali_free_other_states_resources(state);
2409                                         state->chans_num = 1;
2410                                 }
2411
2412                                 if (val >= 2) {
2413
2414                                         dmabuf->fmt |= TRIDENT_FMT_STEREO;
2415                                         if ((val == 6) && (state->card->pci_id == PCI_DEVICE_ID_ALI_5451)) {
2416                                                 if (card->rec_channel_use_count > 0) {
2417                                                         printk(KERN_ERR "trident: Record is "
2418                                                                "working on the card!\n");
2419                                                         ret = -EBUSY;
2420                                                         unlock_set_fmt(state);
2421                                                         break;
2422                                                 }
2423
2424                                                 ret = ali_setup_multi_channels(state->card, 6);
2425                                                 if (ret < 0) {
2426                                                         unlock_set_fmt(state);
2427                                                         break;
2428                                                 }
2429                                                 down(&state->card->open_sem);
2430                                                 ret = ali_allocate_other_states_resources(state, 6);
2431                                                 if (ret < 0) {
2432                                                         up(&state->card->open_sem);
2433                                                         unlock_set_fmt(state);
2434                                                         break;
2435                                                 }
2436                                                 state->card->multi_channel_use_count++;
2437                                                 up(&state->card->open_sem);
2438                                         } else
2439                                                 val = 2;        /*yield to 2-channels */
2440                                 } else
2441                                         dmabuf->fmt &= ~TRIDENT_FMT_STEREO;
2442                                 state->chans_num = val;
2443                         }
2444                         if (file->f_mode & FMODE_READ) {
2445                                 stop_adc(state);
2446                                 dmabuf->ready = 0;
2447                                 if (val >= 2) {
2448                                         if (!((file->f_mode & FMODE_WRITE) && 
2449                                               (val == 6)))
2450                                                 val = 2;
2451                                         dmabuf->fmt |= TRIDENT_FMT_STEREO;
2452                                 } else
2453                                         dmabuf->fmt &= ~TRIDENT_FMT_STEREO;
2454                                 state->chans_num = val;
2455                         }
2456                         unlock_set_fmt(state);
2457                 }
2458                 ret = put_user(val, p);
2459                 break;
2460
2461         case SNDCTL_DSP_POST:
2462                 /* Cause the working fragment to be output */
2463                 break;
2464
2465         case SNDCTL_DSP_SUBDIVIDE:
2466                 if (dmabuf->subdivision) {
2467                         ret = -EINVAL;
2468                         break;
2469                 }
2470                 if (get_user(val, p)) {
2471                         ret = -EFAULT;
2472                         break;
2473                 }
2474                 if (val != 1 && val != 2 && val != 4) {
2475                         ret = -EINVAL;
2476                         break;
2477                 }
2478                 dmabuf->subdivision = val;
2479                 break;
2480
2481         case SNDCTL_DSP_SETFRAGMENT:
2482                 if (get_user(val, p)) {
2483                         ret = -EFAULT;
2484                         break;
2485                 }
2486
2487                 dmabuf->ossfragshift = val & 0xffff;
2488                 dmabuf->ossmaxfrags = (val >> 16) & 0xffff;
2489                 if (dmabuf->ossfragshift < 4)
2490                         dmabuf->ossfragshift = 4;
2491                 if (dmabuf->ossfragshift > 15)
2492                         dmabuf->ossfragshift = 15;
2493                 if (dmabuf->ossmaxfrags < 4)
2494                         dmabuf->ossmaxfrags = 4;
2495
2496                 break;
2497
2498         case SNDCTL_DSP_GETOSPACE:
2499                 if (!(file->f_mode & FMODE_WRITE)) {
2500                         ret = -EINVAL;
2501                         break;
2502                 }
2503                 if (!dmabuf->ready && (val = prog_dmabuf_playback(state)) != 0) {
2504                         ret = val;
2505                         break;
2506                 }
2507                 spin_lock_irqsave(&state->card->lock, flags);
2508                 trident_update_ptr(state);
2509                 abinfo.fragsize = dmabuf->fragsize;
2510                 abinfo.bytes = dmabuf->dmasize - dmabuf->count;
2511                 abinfo.fragstotal = dmabuf->numfrag;
2512                 abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
2513                 spin_unlock_irqrestore(&state->card->lock, flags);
2514                 ret = copy_to_user(argp, &abinfo, sizeof (abinfo)) ? 
2515                         -EFAULT : 0;
2516                 break;
2517
2518         case SNDCTL_DSP_GETISPACE:
2519                 if (!(file->f_mode & FMODE_READ)) {
2520                         ret = -EINVAL;
2521                         break;
2522                 }
2523                 if (!dmabuf->ready && (val = prog_dmabuf_record(state)) != 0) {
2524                         ret = val;
2525                         break;
2526                 }
2527                 spin_lock_irqsave(&state->card->lock, flags);
2528                 trident_update_ptr(state);
2529                 abinfo.fragsize = dmabuf->fragsize;
2530                 abinfo.bytes = dmabuf->count;
2531                 abinfo.fragstotal = dmabuf->numfrag;
2532                 abinfo.fragments = abinfo.bytes >> dmabuf->fragshift;
2533                 spin_unlock_irqrestore(&state->card->lock, flags);
2534                 ret = copy_to_user(argp, &abinfo, sizeof (abinfo)) ? 
2535                         -EFAULT : 0;
2536                 break;
2537
2538         case SNDCTL_DSP_NONBLOCK:
2539                 file->f_flags |= O_NONBLOCK;
2540                 break;
2541
2542         case SNDCTL_DSP_GETCAPS:
2543                 ret = put_user(DSP_CAP_REALTIME | DSP_CAP_TRIGGER | 
2544                                DSP_CAP_MMAP | DSP_CAP_BIND, p);
2545                 break;
2546
2547         case SNDCTL_DSP_GETTRIGGER:
2548                 val = 0;
2549                 if ((file->f_mode & FMODE_READ) && dmabuf->enable)
2550                         val |= PCM_ENABLE_INPUT;
2551                 if ((file->f_mode & FMODE_WRITE) && dmabuf->enable)
2552                         val |= PCM_ENABLE_OUTPUT;
2553                 ret = put_user(val, p);
2554                 break;
2555
2556         case SNDCTL_DSP_SETTRIGGER:
2557                 if (get_user(val, p)) {
2558                         ret = -EFAULT;
2559                         break;
2560                 }
2561                 if (file->f_mode & FMODE_READ) {
2562                         if (val & PCM_ENABLE_INPUT) {
2563                                 if (!dmabuf->ready && 
2564                                     (ret = prog_dmabuf_record(state)))
2565                                         break;
2566                                 start_adc(state);
2567                         } else
2568                                 stop_adc(state);
2569                 }
2570                 if (file->f_mode & FMODE_WRITE) {
2571                         if (val & PCM_ENABLE_OUTPUT) {
2572                                 if (!dmabuf->ready && 
2573                                     (ret = prog_dmabuf_playback(state)))
2574                                         break;
2575                                 start_dac(state);
2576                         } else
2577                                 stop_dac(state);
2578                 }
2579                 break;
2580
2581         case SNDCTL_DSP_GETIPTR:
2582                 if (!(file->f_mode & FMODE_READ)) {
2583                         ret = -EINVAL;
2584                         break;
2585                 }
2586                 if (!dmabuf->ready && (val = prog_dmabuf_record(state))
2587                     != 0) {
2588                         ret = val;
2589                         break;
2590                 }
2591                 spin_lock_irqsave(&state->card->lock, flags);
2592                 trident_update_ptr(state);
2593                 cinfo.bytes = dmabuf->total_bytes;
2594                 cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
2595                 cinfo.ptr = dmabuf->hwptr;
2596                 if (dmabuf->mapped)
2597                         dmabuf->count &= dmabuf->fragsize - 1;
2598                 spin_unlock_irqrestore(&state->card->lock, flags);
2599                 ret = copy_to_user(argp, &cinfo, sizeof (cinfo)) ? 
2600                         -EFAULT : 0;
2601                 break;
2602
2603         case SNDCTL_DSP_GETOPTR:
2604                 if (!(file->f_mode & FMODE_WRITE)) {
2605                         ret = -EINVAL;
2606                         break;
2607                 }
2608                 if (!dmabuf->ready && (val = prog_dmabuf_playback(state))
2609                     != 0) {
2610                         ret = val;
2611                         break;
2612                 }
2613
2614                 spin_lock_irqsave(&state->card->lock, flags);
2615                 trident_update_ptr(state);
2616                 cinfo.bytes = dmabuf->total_bytes;
2617                 cinfo.blocks = dmabuf->count >> dmabuf->fragshift;
2618                 cinfo.ptr = dmabuf->hwptr;
2619                 if (dmabuf->mapped)
2620                         dmabuf->count &= dmabuf->fragsize - 1;
2621                 spin_unlock_irqrestore(&state->card->lock, flags);
2622                 ret = copy_to_user(argp, &cinfo, sizeof (cinfo)) ? 
2623                         -EFAULT : 0;
2624                 break;
2625
2626         case SNDCTL_DSP_SETDUPLEX:
2627                 ret = -EINVAL;
2628                 break;
2629
2630         case SNDCTL_DSP_GETODELAY:
2631                 if (!(file->f_mode & FMODE_WRITE)) {
2632                         ret = -EINVAL;
2633                         break;
2634                 }
2635                 if (!dmabuf->ready && (val = prog_dmabuf_playback(state)) != 0) {
2636                         ret = val;
2637                         break;
2638                 }
2639                 spin_lock_irqsave(&state->card->lock, flags);
2640                 trident_update_ptr(state);
2641                 val = dmabuf->count;
2642                 spin_unlock_irqrestore(&state->card->lock, flags);
2643                 ret = put_user(val, p);
2644                 break;
2645
2646         case SOUND_PCM_READ_RATE:
2647                 ret = put_user(dmabuf->rate, p);
2648                 break;
2649
2650         case SOUND_PCM_READ_CHANNELS:
2651                 ret = put_user((dmabuf->fmt & TRIDENT_FMT_STEREO) ? 2 : 1, 
2652                                p);
2653                 break;
2654
2655         case SOUND_PCM_READ_BITS:
2656                 ret = put_user((dmabuf->fmt & TRIDENT_FMT_16BIT) ? AFMT_S16_LE : 
2657                                AFMT_U8, p);
2658                 break;
2659
2660         case SNDCTL_DSP_GETCHANNELMASK:
2661                 ret = put_user(DSP_BIND_FRONT | DSP_BIND_SURR | 
2662                                DSP_BIND_CENTER_LFE,  p);
2663                 break;
2664
2665         case SNDCTL_DSP_BIND_CHANNEL:
2666                 if (state->card->pci_id != PCI_DEVICE_ID_SI_7018) {
2667                         ret = -EINVAL;
2668                         break;
2669                 }
2670
2671                 if (get_user(val, p)) {
2672                         ret = -EFAULT;
2673                         break;
2674                 }
2675                 if (val == DSP_BIND_QUERY) {
2676                         val = dmabuf->channel->attribute | 0x3c00;
2677                         val = attr2mask[val >> 8];
2678                 } else {
2679                         dmabuf->ready = 0;
2680                         if (file->f_mode & FMODE_READ)
2681                                 dmabuf->channel->attribute = (CHANNEL_REC | 
2682                                                               SRC_ENABLE);
2683                         if (file->f_mode & FMODE_WRITE)
2684                                 dmabuf->channel->attribute = (CHANNEL_SPC_PB | 
2685                                                               SRC_ENABLE);
2686                         dmabuf->channel->attribute |= mask2attr[ffs(val)];
2687                 }
2688                 ret = put_user(val, p);
2689                 break;
2690
2691         case SNDCTL_DSP_MAPINBUF:
2692         case SNDCTL_DSP_MAPOUTBUF:
2693         case SNDCTL_DSP_SETSYNCRO:
2694         case SOUND_PCM_WRITE_FILTER:
2695         case SOUND_PCM_READ_FILTER:
2696         default:
2697                 ret = -EINVAL;
2698                 break;
2699
2700         }
2701         return ret;
2702 }
2703
2704 static int
2705 trident_open(struct inode *inode, struct file *file)
2706 {
2707         int i = 0;
2708         int minor = iminor(inode);
2709         struct trident_card *card = devs;
2710         struct trident_state *state = NULL;
2711         struct dmabuf *dmabuf = NULL;
2712
2713         /* Added by Matt Wu 01-05-2001 */
2714         /* TODO: there's some redundacy here wrt the check below */
2715         /* for multi_use_count > 0. Should we return -EBUSY or find */
2716         /* a different card? for now, don't break current behaviour */
2717         /* -- mulix */
2718         if (file->f_mode & FMODE_READ) {
2719                 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
2720                         if (card->multi_channel_use_count > 0)
2721                                 return -EBUSY;
2722                 }
2723         }
2724
2725         /* find an available virtual channel (instance of /dev/dsp) */
2726         while (card != NULL) {
2727                 down(&card->open_sem);
2728                 if (file->f_mode & FMODE_READ) {
2729                         /* Skip opens on cards that are in 6 channel mode */
2730                         if (card->multi_channel_use_count > 0) {
2731                                 up(&card->open_sem);
2732                                 card = card->next;
2733                                 continue;
2734                         }
2735                 }
2736                 for (i = 0; i < NR_HW_CH; i++) {
2737                         if (card->states[i] == NULL) {
2738                                 state = card->states[i] = kmalloc(sizeof(*state), GFP_KERNEL);
2739                                 if (state == NULL) {
2740                                         up(&card->open_sem);
2741                                         return -ENOMEM;
2742                                 }
2743                                 memset(state, 0, sizeof(*state));
2744                                 init_MUTEX(&state->sem);
2745                                 dmabuf = &state->dmabuf;
2746                                 goto found_virt;
2747                         }
2748                 }
2749                 up(&card->open_sem);
2750                 card = card->next;
2751         }
2752         /* no more virtual channel avaiable */
2753         if (!state) {
2754                 return -ENODEV;
2755         }
2756       found_virt:
2757         /* found a free virtual channel, allocate hardware channels */
2758         if (file->f_mode & FMODE_READ)
2759                 dmabuf->channel = card->alloc_rec_pcm_channel(card);
2760         else
2761                 dmabuf->channel = card->alloc_pcm_channel(card);
2762
2763         if (dmabuf->channel == NULL) {
2764                 kfree(card->states[i]);
2765                 card->states[i] = NULL;
2766                 return -ENODEV;
2767         }
2768
2769         /* initialize the virtual channel */
2770         state->virt = i;
2771         state->card = card;
2772         state->magic = TRIDENT_STATE_MAGIC;
2773         init_waitqueue_head(&dmabuf->wait);
2774         file->private_data = state;
2775
2776         /* set default sample format. According to OSS Programmer's */ 
2777         /* Guide  /dev/dsp should be default to unsigned 8-bits, mono, */ 
2778         /* with sample rate 8kHz and /dev/dspW will accept 16-bits sample */
2779         if (file->f_mode & FMODE_WRITE) {
2780                 dmabuf->fmt &= ~TRIDENT_FMT_MASK;
2781                 if ((minor & 0x0f) == SND_DEV_DSP16)
2782                         dmabuf->fmt |= TRIDENT_FMT_16BIT;
2783                 dmabuf->ossfragshift = 0;
2784                 dmabuf->ossmaxfrags = 0;
2785                 dmabuf->subdivision = 0;
2786                 if (card->pci_id == PCI_DEVICE_ID_SI_7018) {
2787                         /* set default channel attribute to normal playback */
2788                         dmabuf->channel->attribute = CHANNEL_PB;
2789                 }
2790                 trident_set_dac_rate(state, 8000);
2791         }
2792
2793         if (file->f_mode & FMODE_READ) {
2794                 /* FIXME: Trident 4d can only record in signed 16-bits stereo, */ 
2795                 /* 48kHz sample, to be dealed with in trident_set_adc_rate() ?? */
2796                 dmabuf->fmt &= ~TRIDENT_FMT_MASK;
2797                 if ((minor & 0x0f) == SND_DEV_DSP16)
2798                         dmabuf->fmt |= TRIDENT_FMT_16BIT;
2799                 dmabuf->ossfragshift = 0;
2800                 dmabuf->ossmaxfrags = 0;
2801                 dmabuf->subdivision = 0;
2802                 if (card->pci_id == PCI_DEVICE_ID_SI_7018) {
2803                         /* set default channel attribute to 0x8a80, record from
2804                            PCM L/R FIFO and mono = (left + right + 1)/2 */
2805                         dmabuf->channel->attribute = (CHANNEL_REC | PCM_LR | 
2806                                                       MONO_MIX);
2807                 }
2808                 trident_set_adc_rate(state, 8000);
2809
2810                 /* Added by Matt Wu 01-05-2001 */
2811                 if (card->pci_id == PCI_DEVICE_ID_ALI_5451)
2812                         card->rec_channel_use_count++;
2813         }
2814
2815         state->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
2816         up(&card->open_sem);
2817
2818         pr_debug("trident: open virtual channel %d, hard channel %d\n",
2819                  state->virt, dmabuf->channel->num);
2820
2821         return nonseekable_open(inode, file);
2822 }
2823
2824 static int
2825 trident_release(struct inode *inode, struct file *file)
2826 {
2827         struct trident_state *state = (struct trident_state *)file->private_data;
2828         struct trident_card *card;
2829         struct dmabuf *dmabuf;
2830
2831         VALIDATE_STATE(state);
2832
2833         card = state->card;
2834         dmabuf = &state->dmabuf;
2835
2836         if (file->f_mode & FMODE_WRITE) {
2837                 trident_clear_tail(state);
2838                 drain_dac(state, file->f_flags & O_NONBLOCK);
2839         }
2840
2841         pr_debug("trident: closing virtual channel %d, hard channel %d\n",
2842                  state->virt, dmabuf->channel->num);
2843
2844         /* stop DMA state machine and free DMA buffers/channels */
2845         down(&card->open_sem);
2846
2847         if (file->f_mode & FMODE_WRITE) {
2848                 stop_dac(state);
2849                 dealloc_dmabuf(&state->dmabuf, state->card->pci_dev);
2850                 state->card->free_pcm_channel(state->card, dmabuf->channel->num);
2851
2852                 /* Added by Matt Wu */
2853                 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
2854                         if (state->chans_num > 2) {
2855                                 if (card->multi_channel_use_count-- < 0)
2856                                         card->multi_channel_use_count = 0;
2857                                 if (card->multi_channel_use_count == 0)
2858                                         ali_close_multi_channels();
2859                                 ali_free_other_states_resources(state);
2860                         }
2861                 }
2862         }
2863         if (file->f_mode & FMODE_READ) {
2864                 stop_adc(state);
2865                 dealloc_dmabuf(&state->dmabuf, state->card->pci_dev);
2866                 state->card->free_pcm_channel(state->card, dmabuf->channel->num);
2867
2868                 /* Added by Matt Wu */
2869                 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
2870                         if (card->rec_channel_use_count-- < 0)
2871                                 card->rec_channel_use_count = 0;
2872                 }
2873         }
2874
2875         card->states[state->virt] = NULL;
2876         kfree(state);
2877
2878         /* we're covered by the open_sem */
2879         up(&card->open_sem);
2880
2881         return 0;
2882 }
2883
2884 static /*const */ struct file_operations trident_audio_fops = {
2885         .owner = THIS_MODULE,
2886         .llseek = no_llseek,
2887         .read = trident_read,
2888         .write = trident_write,
2889         .poll = trident_poll,
2890         .ioctl = trident_ioctl,
2891         .mmap = trident_mmap,
2892         .open = trident_open,
2893         .release = trident_release,
2894 };
2895
2896 /* trident specific AC97 functions */
2897 /* Write AC97 codec registers */
2898 static void
2899 trident_ac97_set(struct ac97_codec *codec, u8 reg, u16 val)
2900 {
2901         struct trident_card *card = (struct trident_card *)codec->private_data;
2902         unsigned int address, mask, busy;
2903         unsigned short count = 0xffff;
2904         unsigned long flags;
2905         u32 data;
2906
2907         data = ((u32) val) << 16;
2908
2909         switch (card->pci_id) {
2910         default:
2911         case PCI_DEVICE_ID_SI_7018:
2912                 address = SI_AC97_WRITE;
2913                 mask = SI_AC97_BUSY_WRITE | SI_AC97_AUDIO_BUSY;
2914                 if (codec->id)
2915                         mask |= SI_AC97_SECONDARY;
2916                 busy = SI_AC97_BUSY_WRITE;
2917                 break;
2918         case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX:
2919                 address = DX_ACR0_AC97_W;
2920                 mask = busy = DX_AC97_BUSY_WRITE;
2921                 break;
2922         case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX:
2923                 address = NX_ACR1_AC97_W;
2924                 mask = NX_AC97_BUSY_WRITE;
2925                 if (codec->id)
2926                         mask |= NX_AC97_WRITE_SECONDARY;
2927                 busy = NX_AC97_BUSY_WRITE;
2928                 break;
2929         case PCI_DEVICE_ID_INTERG_5050:
2930                 address = SI_AC97_WRITE;
2931                 mask = busy = SI_AC97_BUSY_WRITE;
2932                 if (codec->id)
2933                         mask |= SI_AC97_SECONDARY;
2934                 break;
2935         }
2936
2937         spin_lock_irqsave(&card->lock, flags);
2938         do {
2939                 if ((inw(TRID_REG(card, address)) & busy) == 0)
2940                         break;
2941         } while (count--);
2942
2943         data |= (mask | (reg & AC97_REG_ADDR));
2944
2945         if (count == 0) {
2946                 printk(KERN_ERR "trident: AC97 CODEC write timed out.\n");
2947                 spin_unlock_irqrestore(&card->lock, flags);
2948                 return;
2949         }
2950
2951         outl(data, TRID_REG(card, address));
2952         spin_unlock_irqrestore(&card->lock, flags);
2953 }
2954
2955 /* Read AC97 codec registers */
2956 static u16
2957 trident_ac97_get(struct ac97_codec *codec, u8 reg)
2958 {
2959         struct trident_card *card = (struct trident_card *)codec->private_data;
2960         unsigned int address, mask, busy;
2961         unsigned short count = 0xffff;
2962         unsigned long flags;
2963         u32 data;
2964
2965         switch (card->pci_id) {
2966         default:
2967         case PCI_DEVICE_ID_SI_7018:
2968                 address = SI_AC97_READ;
2969                 mask = SI_AC97_BUSY_READ | SI_AC97_AUDIO_BUSY;
2970                 if (codec->id)
2971                         mask |= SI_AC97_SECONDARY;
2972                 busy = SI_AC97_BUSY_READ;
2973                 break;
2974         case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX:
2975                 address = DX_ACR1_AC97_R;
2976                 mask = busy = DX_AC97_BUSY_READ;
2977                 break;
2978         case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX:
2979                 if (codec->id)
2980                         address = NX_ACR3_AC97_R_SECONDARY;
2981                 else
2982                         address = NX_ACR2_AC97_R_PRIMARY;
2983                 mask = NX_AC97_BUSY_READ;
2984                 busy = NX_AC97_BUSY_READ | NX_AC97_BUSY_DATA;
2985                 break;
2986         case PCI_DEVICE_ID_INTERG_5050:
2987                 address = SI_AC97_READ;
2988                 mask = busy = SI_AC97_BUSY_READ;
2989                 if (codec->id)
2990                         mask |= SI_AC97_SECONDARY;
2991                 break;
2992         }
2993
2994         data = (mask | (reg & AC97_REG_ADDR));
2995
2996         spin_lock_irqsave(&card->lock, flags);
2997         outl(data, TRID_REG(card, address));
2998         do {
2999                 data = inl(TRID_REG(card, address));
3000                 if ((data & busy) == 0)
3001                         break;
3002         } while (count--);
3003         spin_unlock_irqrestore(&card->lock, flags);
3004
3005         if (count == 0) {
3006                 printk(KERN_ERR "trident: AC97 CODEC read timed out.\n");
3007                 data = 0;
3008         }
3009         return ((u16) (data >> 16));
3010 }
3011
3012 /* rewrite ac97 read and write mixer register by hulei for ALI*/
3013 static int
3014 acquirecodecaccess(struct trident_card *card)
3015 {
3016         u16 wsemamask = 0x6000; /* bit 14..13 */
3017         u16 wsemabits;
3018         u16 wcontrol;
3019         int block = 0;
3020         int ncount = 25;
3021         while (1) {
3022                 wcontrol = inw(TRID_REG(card, ALI_AC97_WRITE));
3023                 wsemabits = wcontrol & wsemamask;
3024
3025                 if (wsemabits == 0x4000)
3026                         return 1;       /* 0x4000 is audio ,then success */
3027                 if (ncount-- < 0)
3028                         break;
3029                 if (wsemabits == 0) {
3030                       unlock:
3031                         outl(((u32) (wcontrol & 0x1eff) | 0x00004000), 
3032                              TRID_REG(card, ALI_AC97_WRITE));
3033                         continue;
3034                 }
3035                 udelay(20);
3036         }
3037         if (!block) {
3038                 pr_debug("accesscodecsemaphore: try unlock\n");
3039                 block = 1;
3040                 goto unlock;
3041         }
3042         return 0;
3043 }
3044
3045 static void
3046 releasecodecaccess(struct trident_card *card)
3047 {
3048         unsigned long wcontrol;
3049         wcontrol = inl(TRID_REG(card, ALI_AC97_WRITE));
3050         outl((wcontrol & 0xffff1eff), TRID_REG(card, ALI_AC97_WRITE));
3051 }
3052
3053 static int
3054 waitforstimertick(struct trident_card *card)
3055 {
3056         unsigned long chk1, chk2;
3057         unsigned int wcount = 0xffff;
3058         chk1 = inl(TRID_REG(card, ALI_STIMER));
3059
3060         while (1) {
3061                 chk2 = inl(TRID_REG(card, ALI_STIMER));
3062                 if ((wcount > 0) && chk1 != chk2)
3063                         return 1;
3064                 if (wcount <= 0)
3065                         break;
3066                 udelay(50);
3067         }
3068         return 0;
3069 }
3070
3071 /* Read AC97 codec registers for ALi*/
3072 static u16
3073 ali_ac97_get(struct trident_card *card, int secondary, u8 reg)
3074 {
3075         unsigned int address, mask;
3076         unsigned int ncount;
3077         unsigned long aud_reg;
3078         u32 data;
3079         u16 wcontrol;
3080         unsigned long flags;
3081
3082         if (!card)
3083                 BUG();
3084
3085         address = ALI_AC97_READ;
3086         if (card->revision == ALI_5451_V02) {
3087                 address = ALI_AC97_WRITE;
3088         }
3089         mask = ALI_AC97_READ_ACTION | ALI_AC97_AUDIO_BUSY;
3090         if (secondary)
3091                 mask |= ALI_AC97_SECONDARY;
3092
3093         spin_lock_irqsave(&card->lock, flags);
3094
3095         if (!acquirecodecaccess(card))
3096                 printk(KERN_ERR "access codec fail\n");
3097
3098         wcontrol = inw(TRID_REG(card, ALI_AC97_WRITE));
3099         wcontrol &= 0xfe00;
3100         wcontrol |= (0x8000 | reg);
3101         outw(wcontrol, TRID_REG(card, ALI_AC97_WRITE));
3102
3103         data = (mask | (reg & AC97_REG_ADDR));
3104
3105         if (!waitforstimertick(card)) {
3106                 printk(KERN_ERR "ali_ac97_read: BIT_CLOCK is dead\n");
3107                 goto releasecodec;
3108         }
3109
3110         udelay(20);
3111
3112         ncount = 10;
3113
3114         while (1) {
3115                 if ((inw(TRID_REG(card, ALI_AC97_WRITE)) & ALI_AC97_BUSY_READ) 
3116                     != 0)
3117                         break;
3118                 if (ncount <= 0)
3119                         break;
3120                 if (ncount-- == 1) {
3121                         pr_debug("ali_ac97_read :try clear busy flag\n");
3122                         aud_reg = inl(TRID_REG(card, ALI_AC97_WRITE));
3123                         outl((aud_reg & 0xffff7fff), 
3124                              TRID_REG(card, ALI_AC97_WRITE));
3125                 }
3126                 udelay(10);
3127         }
3128
3129         data = inl(TRID_REG(card, address));
3130
3131         spin_unlock_irqrestore(&card->lock, flags);
3132
3133         return ((u16) (data >> 16));
3134
3135       releasecodec:
3136         releasecodecaccess(card);
3137         spin_unlock_irqrestore(&card->lock, flags);
3138         printk(KERN_ERR "ali_ac97_read: AC97 CODEC read timed out.\n");
3139         return 0;
3140 }
3141
3142 /* Write AC97 codec registers for hulei*/
3143 static void
3144 ali_ac97_set(struct trident_card *card, int secondary, u8 reg, u16 val)
3145 {
3146         unsigned int address, mask;
3147         unsigned int ncount;
3148         u32 data;
3149         u16 wcontrol;
3150         unsigned long flags;
3151
3152         data = ((u32) val) << 16;
3153
3154         if (!card)
3155                 BUG();
3156
3157         address = ALI_AC97_WRITE;
3158         mask = ALI_AC97_WRITE_ACTION | ALI_AC97_AUDIO_BUSY;
3159         if (secondary)
3160                 mask |= ALI_AC97_SECONDARY;
3161         if (card->revision == ALI_5451_V02)
3162                 mask |= ALI_AC97_WRITE_MIXER_REGISTER;
3163
3164         spin_lock_irqsave(&card->lock, flags);
3165         if (!acquirecodecaccess(card))
3166                 printk(KERN_ERR "ali_ac97_write: access codec fail\n");
3167
3168         wcontrol = inw(TRID_REG(card, ALI_AC97_WRITE));
3169         wcontrol &= 0xff00;
3170         wcontrol |= (0x8100 | reg); /* bit 8=1: (ali1535 )reserved/ */ 
3171                                     /* ali1535+ write */
3172         outl((data | wcontrol), TRID_REG(card, ALI_AC97_WRITE));
3173
3174         if (!waitforstimertick(card)) {
3175                 printk(KERN_ERR "BIT_CLOCK is dead\n");
3176                 goto releasecodec;
3177         }
3178
3179         ncount = 10;
3180         while (1) {
3181                 wcontrol = inw(TRID_REG(card, ALI_AC97_WRITE));
3182                 if (!(wcontrol & 0x8000))
3183                         break;
3184                 if (ncount <= 0)
3185                         break;
3186                 if (ncount-- == 1) {
3187                         pr_debug("ali_ac97_set :try clear busy flag!!\n");
3188                         outw(wcontrol & 0x7fff, 
3189                              TRID_REG(card, ALI_AC97_WRITE));
3190                 }
3191                 udelay(10);
3192         }
3193
3194       releasecodec:
3195         releasecodecaccess(card);
3196         spin_unlock_irqrestore(&card->lock, flags);
3197         return;
3198 }
3199
3200 static void
3201 ali_enable_special_channel(struct trident_state *stat)
3202 {
3203         struct trident_card *card = stat->card;
3204         unsigned long s_channels;
3205
3206         s_channels = inl(TRID_REG(card, ALI_GLOBAL_CONTROL));
3207         s_channels |= (1 << stat->dmabuf.channel->num);
3208         outl(s_channels, TRID_REG(card, ALI_GLOBAL_CONTROL));
3209 }
3210
3211 static u16
3212 ali_ac97_read(struct ac97_codec *codec, u8 reg)
3213 {
3214         int id;
3215         u16 data;
3216         struct trident_card *card = NULL;
3217
3218         /* Added by Matt Wu */
3219         if (!codec)
3220                 BUG();
3221
3222         card = (struct trident_card *) codec->private_data;
3223
3224         if (!card->mixer_regs_ready)
3225                 return ali_ac97_get(card, codec->id, reg);
3226
3227         /*
3228          *      FIXME: need to stop this caching some registers
3229          */
3230         if (codec->id)
3231                 id = 1;
3232         else
3233                 id = 0;
3234
3235         data = card->mixer_regs[reg / 2][id];
3236         return data;
3237 }
3238
3239 static void
3240 ali_ac97_write(struct ac97_codec *codec, u8 reg, u16 val)
3241 {
3242         int id;
3243         struct trident_card *card;
3244
3245         /*  Added by Matt Wu */
3246         if (!codec)
3247                 BUG();
3248
3249         card = (struct trident_card *) codec->private_data;
3250
3251         if (!card->mixer_regs_ready) {
3252                 ali_ac97_set(card, codec->id, reg, val);
3253                 return;
3254         }
3255
3256         if (codec->id)
3257                 id = 1;
3258         else
3259                 id = 0;
3260
3261         card->mixer_regs[reg / 2][id] = val;
3262         ali_ac97_set(card, codec->id, reg, val);
3263 }
3264
3265 /*
3266 flag:   ALI_SPDIF_OUT_TO_SPDIF_OUT
3267         ALI_PCM_TO_SPDIF_OUT
3268 */
3269
3270 static void
3271 ali_setup_spdif_out(struct trident_card *card, int flag)
3272 {
3273         unsigned long spdif;
3274         unsigned char ch;
3275
3276         char temp;
3277         struct pci_dev *pci_dev = NULL;
3278
3279         pci_dev = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, 
3280                                   pci_dev);
3281         if (pci_dev == NULL)
3282                 return;
3283         pci_read_config_byte(pci_dev, 0x61, &temp);
3284         temp |= 0x40;
3285         pci_write_config_byte(pci_dev, 0x61, temp);
3286         pci_read_config_byte(pci_dev, 0x7d, &temp);
3287         temp |= 0x01;
3288         pci_write_config_byte(pci_dev, 0x7d, temp);
3289         pci_read_config_byte(pci_dev, 0x7e, &temp);
3290         temp &= (~0x20);
3291         temp |= 0x10;
3292         pci_write_config_byte(pci_dev, 0x7e, temp);
3293
3294         ch = inb(TRID_REG(card, ALI_SCTRL));
3295         outb(ch | ALI_SPDIF_OUT_ENABLE, TRID_REG(card, ALI_SCTRL));
3296         ch = inb(TRID_REG(card, ALI_SPDIF_CTRL));
3297         outb(ch & ALI_SPDIF_OUT_CH_STATUS, TRID_REG(card, ALI_SPDIF_CTRL));
3298
3299         if (flag & ALI_SPDIF_OUT_TO_SPDIF_OUT) {
3300                 spdif = inw(TRID_REG(card, ALI_GLOBAL_CONTROL));
3301                 spdif |= ALI_SPDIF_OUT_CH_ENABLE;
3302                 spdif &= ALI_SPDIF_OUT_SEL_SPDIF;
3303                 outw(spdif, TRID_REG(card, ALI_GLOBAL_CONTROL));
3304                 spdif = inw(TRID_REG(card, ALI_SPDIF_CS));
3305                 if (flag & ALI_SPDIF_OUT_NON_PCM)
3306                         spdif |= 0x0002;
3307                 else
3308                         spdif &= (~0x0002);
3309                 outw(spdif, TRID_REG(card, ALI_SPDIF_CS));
3310         } else {
3311                 spdif = inw(TRID_REG(card, ALI_GLOBAL_CONTROL));
3312                 spdif |= ALI_SPDIF_OUT_SEL_PCM;
3313                 outw(spdif, TRID_REG(card, ALI_GLOBAL_CONTROL));
3314         }
3315 }
3316
3317 static void
3318 ali_disable_special_channel(struct trident_card *card, int ch)
3319 {
3320         unsigned long sc;
3321
3322         sc = inl(TRID_REG(card, ALI_GLOBAL_CONTROL));
3323         sc &= ~(1 << ch);
3324         outl(sc, TRID_REG(card, ALI_GLOBAL_CONTROL));
3325 }
3326
3327 static void
3328 ali_disable_spdif_in(struct trident_card *card)
3329 {
3330         unsigned long spdif;
3331
3332         spdif = inl(TRID_REG(card, ALI_GLOBAL_CONTROL));
3333         spdif &= (~ALI_SPDIF_IN_SUPPORT);
3334         outl(spdif, TRID_REG(card, ALI_GLOBAL_CONTROL));
3335
3336         ali_disable_special_channel(card, ALI_SPDIF_IN_CHANNEL);
3337 }
3338
3339 static void
3340 ali_setup_spdif_in(struct trident_card *card)
3341 {
3342         unsigned long spdif;
3343
3344         //Set SPDIF IN Supported
3345         spdif = inl(TRID_REG(card, ALI_GLOBAL_CONTROL));
3346         spdif |= ALI_SPDIF_IN_SUPPORT;
3347         outl(spdif, TRID_REG(card, ALI_GLOBAL_CONTROL));
3348
3349         //Set SPDIF IN Rec
3350         spdif = inl(TRID_REG(card, ALI_GLOBAL_CONTROL));
3351         spdif |= ALI_SPDIF_IN_CH_ENABLE;
3352         outl(spdif, TRID_REG(card, ALI_GLOBAL_CONTROL));
3353
3354         spdif = inb(TRID_REG(card, ALI_SPDIF_CTRL));
3355         spdif |= ALI_SPDIF_IN_CH_STATUS;
3356         outb(spdif, TRID_REG(card, ALI_SPDIF_CTRL));
3357 /*
3358         spdif = inb(TRID_REG(card, ALI_SPDIF_CTRL));
3359         spdif |= ALI_SPDIF_IN_FUNC_ENABLE;
3360         outb(spdif, TRID_REG(card, ALI_SPDIF_CTRL));
3361 */
3362 }
3363
3364 static void
3365 ali_delay(struct trident_card *card, int interval)
3366 {
3367         unsigned long begintimer, currenttimer;
3368
3369         begintimer = inl(TRID_REG(card, ALI_STIMER));
3370         currenttimer = inl(TRID_REG(card, ALI_STIMER));
3371
3372         while (currenttimer < begintimer + interval)
3373                 currenttimer = inl(TRID_REG(card, ALI_STIMER));
3374 }
3375
3376 static void
3377 ali_detect_spdif_rate(struct trident_card *card)
3378 {
3379         u16 wval = 0;
3380         u16 count = 0;
3381         u8 bval = 0, R1 = 0, R2 = 0;
3382
3383         bval = inb(TRID_REG(card, ALI_SPDIF_CTRL));
3384         bval |= 0x02;
3385         outb(bval, TRID_REG(card, ALI_SPDIF_CTRL));
3386
3387         bval = inb(TRID_REG(card, ALI_SPDIF_CTRL + 1));
3388         bval |= 0x1F;
3389         outb(bval, TRID_REG(card, ALI_SPDIF_CTRL + 1));
3390
3391         while (((R1 < 0x0B) || (R1 > 0x0E)) && (R1 != 0x12) && 
3392                count <= 50000) {
3393                 count++;
3394
3395                 ali_delay(card, 6);
3396
3397                 bval = inb(TRID_REG(card, ALI_SPDIF_CTRL + 1));
3398                 R1 = bval & 0x1F;
3399         }
3400
3401         if (count > 50000) {
3402                 printk(KERN_WARNING "trident: Error in "
3403                        "ali_detect_spdif_rate!\n");
3404                 return;
3405         }
3406
3407         count = 0;
3408
3409         while (count <= 50000) {
3410                 count++;
3411
3412                 ali_delay(card, 6);
3413
3414                 bval = inb(TRID_REG(card, ALI_SPDIF_CTRL + 1));
3415                 R2 = bval & 0x1F;
3416
3417                 if (R2 != R1)
3418                         R1 = R2;
3419                 else
3420                         break;
3421         }
3422
3423         if (count > 50000) {
3424                 printk(KERN_WARNING "trident: Error in "
3425                        "ali_detect_spdif_rate!\n");
3426                 return;
3427         }
3428
3429         switch (R2) {
3430         case 0x0b:
3431         case 0x0c:
3432         case 0x0d:
3433         case 0x0e:
3434                 wval = inw(TRID_REG(card, ALI_SPDIF_CTRL + 2));
3435                 wval &= 0xE0F0;
3436                 wval |= (u16) 0x09 << 8 | (u16) 0x05;
3437                 outw(wval, TRID_REG(card, ALI_SPDIF_CTRL + 2));
3438
3439                 bval = inb(TRID_REG(card, ALI_SPDIF_CS + 3)) & 0xF0;
3440                 outb(bval | 0x02, TRID_REG(card, ALI_SPDIF_CS + 3));
3441                 break;
3442
3443         case 0x12:
3444                 wval = inw(TRID_REG(card, ALI_SPDIF_CTRL + 2));
3445                 wval &= 0xE0F0;
3446                 wval |= (u16) 0x0E << 8 | (u16) 0x08;
3447                 outw(wval, TRID_REG(card, ALI_SPDIF_CTRL + 2));
3448
3449                 bval = inb(TRID_REG(card, ALI_SPDIF_CS + 3)) & 0xF0;
3450                 outb(bval | 0x03, TRID_REG(card, ALI_SPDIF_CS + 3));
3451                 break;
3452
3453         default:
3454                 break;
3455         }
3456
3457 }
3458
3459 static unsigned int
3460 ali_get_spdif_in_rate(struct trident_card *card)
3461 {
3462         u32 dwRate = 0;
3463         u8 bval = 0;
3464
3465         ali_detect_spdif_rate(card);
3466
3467         bval = inb(TRID_REG(card, ALI_SPDIF_CTRL));
3468         bval &= 0x7F;
3469         bval |= 0x40;
3470         outb(bval, TRID_REG(card, ALI_SPDIF_CTRL));
3471
3472         bval = inb(TRID_REG(card, ALI_SPDIF_CS + 3));
3473         bval &= 0x0F;
3474
3475         switch (bval) {
3476         case 0:
3477                 dwRate = 44100;
3478                 break;
3479         case 1:
3480                 dwRate = 48000;
3481                 break;
3482         case 2:
3483                 dwRate = 32000;
3484                 break;
3485         default:
3486                 // Error occurs
3487                 break;
3488         }
3489
3490         return dwRate;
3491
3492 }
3493
3494 static int
3495 ali_close_multi_channels(void)
3496 {
3497         char temp = 0;
3498         struct pci_dev *pci_dev = NULL;
3499
3500         pci_dev = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, 
3501                                   pci_dev);
3502         if (pci_dev == NULL)
3503                 return -1;
3504         pci_read_config_byte(pci_dev, 0x59, &temp);
3505         temp &= ~0x80;
3506         pci_write_config_byte(pci_dev, 0x59, temp);
3507
3508         pci_dev = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101, 
3509                                   pci_dev);
3510         if (pci_dev == NULL)
3511                 return -1;
3512
3513         pci_read_config_byte(pci_dev, 0xB8, &temp);
3514         temp &= ~0x20;
3515         pci_write_config_byte(pci_dev, 0xB8, temp);
3516
3517         return 0;
3518 }
3519
3520 static int
3521 ali_setup_multi_channels(struct trident_card *card, int chan_nums)
3522 {
3523         unsigned long dwValue;
3524         char temp = 0;
3525         struct pci_dev *pci_dev = NULL;
3526
3527         pci_dev = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, 
3528                                   pci_dev);
3529         if (pci_dev == NULL)
3530                 return -1;
3531         pci_read_config_byte(pci_dev, 0x59, &temp);
3532         temp |= 0x80;
3533         pci_write_config_byte(pci_dev, 0x59, temp);
3534
3535         pci_dev = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101, 
3536                                   pci_dev);
3537         if (pci_dev == NULL)
3538                 return -1;
3539         pci_read_config_byte(pci_dev, (int) 0xB8, &temp);
3540         temp |= 0x20;
3541         pci_write_config_byte(pci_dev, (int) 0xB8, (u8) temp);
3542         if (chan_nums == 6) {
3543                 dwValue = inl(TRID_REG(card, ALI_SCTRL)) | 0x000f0000;
3544                 outl(dwValue, TRID_REG(card, ALI_SCTRL));
3545                 mdelay(4);
3546                 dwValue = inl(TRID_REG(card, ALI_SCTRL));
3547                 if (dwValue & 0x2000000) {
3548                         ali_ac97_write(card->ac97_codec[0], 0x02, 8080);
3549                         ali_ac97_write(card->ac97_codec[0], 0x36, 0);
3550                         ali_ac97_write(card->ac97_codec[0], 0x38, 0);
3551                         /*
3552                          *      On a board with a single codec you won't get the
3553                          *      surround. On other boards configure it.
3554                          */
3555                         if (card->ac97_codec[1] != NULL) {
3556                                 ali_ac97_write(card->ac97_codec[1], 0x36, 0);
3557                                 ali_ac97_write(card->ac97_codec[1], 0x38, 0);
3558                                 ali_ac97_write(card->ac97_codec[1], 0x02, 0x0606);
3559                                 ali_ac97_write(card->ac97_codec[1], 0x18, 0x0303);
3560                                 ali_ac97_write(card->ac97_codec[1], 0x74, 0x3);
3561                         }
3562                         return 1;
3563                 }
3564         }
3565         return -EINVAL;
3566 }
3567
3568 static void
3569 ali_free_pcm_channel(struct trident_card *card, unsigned int channel)
3570 {
3571         int bank;
3572
3573         if (channel > 31)
3574                 return;
3575
3576         bank = channel >> 5;
3577         channel = channel & 0x1f;
3578
3579         card->banks[bank].bitmap &= ~(1 << (channel));
3580 }
3581
3582 static int
3583 ali_allocate_other_states_resources(struct trident_state *state, int chan_nums)
3584 {
3585         struct trident_card *card = state->card;
3586         struct trident_state *s;
3587         int i, state_count = 0;
3588         struct trident_pcm_bank *bank;
3589         struct trident_channel *channel;
3590         unsigned long num;
3591
3592         bank = &card->banks[BANK_A];
3593
3594         if (chan_nums != 6)
3595                 return 0;
3596
3597         for (i = 0; (i < ALI_CHANNELS) && (state_count != 4); i++) {
3598                 if (card->states[i])
3599                         continue;
3600
3601                 num = ali_multi_channels_5_1[state_count];
3602                 if (!(bank->bitmap & (1 << num))) {
3603                         bank->bitmap |= 1 << num;
3604                         channel = &bank->channels[num];
3605                         channel->num = num;
3606                 } else {
3607                         state_count--;
3608                         for (; state_count >= 0; state_count--) {
3609                                 kfree(state->other_states[state_count]);
3610                                 num = ali_multi_channels_5_1[state_count];
3611                                         ali_free_pcm_channel(card, num);
3612                         }
3613                         return -EBUSY;
3614                 }
3615                 s = card->states[i] = kmalloc(sizeof(*state), GFP_KERNEL);
3616                 if (!s) {
3617                         num = ali_multi_channels_5_1[state_count];
3618                         ali_free_pcm_channel(card, num);
3619                         state_count--;
3620                         for (; state_count >= 0; state_count--) {
3621                                 num = ali_multi_channels_5_1[state_count];
3622                                 ali_free_pcm_channel(card, num);
3623                                 kfree(state->other_states[state_count]);
3624                         }
3625                         return -ENOMEM;
3626                 }
3627                 memset(s, 0, sizeof(*state));
3628
3629                 s->dmabuf.channel = channel;
3630                 s->dmabuf.ossfragshift = s->dmabuf.ossmaxfrags =
3631                         s->dmabuf.subdivision = 0;
3632                 init_waitqueue_head(&s->dmabuf.wait);
3633                 s->magic = card->magic;
3634                 s->card = card;
3635                 s->virt = i;
3636                 ali_enable_special_channel(s);
3637                 state->other_states[state_count++] = s;
3638         }
3639
3640         if (state_count != 4) {
3641                 state_count--;
3642                 for (; state_count >= 0; state_count--) {
3643                         kfree(state->other_states[state_count]);
3644                         num = ali_multi_channels_5_1[state_count];
3645                         ali_free_pcm_channel(card, num);
3646                 }
3647                 return -EBUSY;
3648         }
3649         return 0;
3650 }
3651
3652 static void
3653 ali_save_regs(struct trident_card *card)
3654 {
3655         unsigned long flags;
3656         int i, j;
3657
3658         spin_lock_irqsave(&card->lock, flags);
3659
3660         ali_registers.global_regs[0x2c] = inl(TRID_REG(card, T4D_MISCINT));
3661         //ali_registers.global_regs[0x20] = inl(TRID_REG(card,T4D_START_A));    
3662         ali_registers.global_regs[0x21] = inl(TRID_REG(card, T4D_STOP_A));
3663
3664         //disable all IRQ bits
3665         outl(ALI_DISABLE_ALL_IRQ, TRID_REG(card, T4D_MISCINT));
3666
3667         for (i = 1; i < ALI_MIXER_REGS; i++)
3668                 ali_registers.mixer_regs[i] = ali_ac97_read(card->ac97_codec[0], 
3669                                                             i * 2);
3670
3671         for (i = 0; i < ALI_GLOBAL_REGS; i++) {
3672                 if ((i * 4 == T4D_MISCINT) || (i * 4 == T4D_STOP_A))
3673                         continue;
3674                 ali_registers.global_regs[i] = inl(TRID_REG(card, i * 4));
3675         }
3676
3677         for (i = 0; i < ALI_CHANNELS; i++) {
3678                 outb(i, TRID_REG(card, T4D_LFO_GC_CIR));
3679                 for (j = 0; j < ALI_CHANNEL_REGS; j++)
3680                         ali_registers.channel_regs[i][j] = inl(TRID_REG(card, 
3681                                                                         j * 4 + 0xe0));
3682         }
3683
3684         //Stop all HW channel
3685         outl(ALI_STOP_ALL_CHANNELS, TRID_REG(card, T4D_STOP_A));
3686
3687         spin_unlock_irqrestore(&card->lock, flags);
3688 }
3689
3690 static void
3691 ali_restore_regs(struct trident_card *card)
3692 {
3693         unsigned long flags;
3694         int i, j;
3695
3696         spin_lock_irqsave(&card->lock, flags);
3697
3698         for (i = 1; i < ALI_MIXER_REGS; i++)
3699                 ali_ac97_write(card->ac97_codec[0], i * 2, 
3700                                ali_registers.mixer_regs[i]);
3701
3702         for (i = 0; i < ALI_CHANNELS; i++) {
3703                 outb(i, TRID_REG(card, T4D_LFO_GC_CIR));
3704                 for (j = 0; j < ALI_CHANNEL_REGS; j++)
3705                         outl(ali_registers.channel_regs[i][j], 
3706                              TRID_REG(card, j * 4 + 0xe0));
3707         }
3708
3709         for (i = 0; i < ALI_GLOBAL_REGS; i++) {
3710                 if ((i * 4 == T4D_MISCINT) || (i * 4 == T4D_STOP_A) || 
3711                     (i * 4 == T4D_START_A))
3712                         continue;
3713                 outl(ali_registers.global_regs[i], TRID_REG(card, i * 4));
3714         }
3715
3716         //start HW channel
3717         outl(ali_registers.global_regs[0x20], TRID_REG(card, T4D_START_A));
3718         //restore IRQ enable bits
3719         outl(ali_registers.global_regs[0x2c], TRID_REG(card, T4D_MISCINT));
3720
3721         spin_unlock_irqrestore(&card->lock, flags);
3722 }
3723
3724 static int
3725 trident_suspend(struct pci_dev *dev, u32 unused)
3726 {
3727         struct trident_card *card = pci_get_drvdata(dev);
3728
3729         if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
3730                 ali_save_regs(card);
3731         }
3732         return 0;
3733 }
3734
3735 static int
3736 trident_resume(struct pci_dev *dev)
3737 {
3738         struct trident_card *card = pci_get_drvdata(dev);
3739
3740         if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
3741                 ali_restore_regs(card);
3742         }
3743         return 0;
3744 }
3745
3746 static struct trident_channel *
3747 ali_alloc_pcm_channel(struct trident_card *card)
3748 {
3749         struct trident_pcm_bank *bank;
3750         int idx;
3751
3752         bank = &card->banks[BANK_A];
3753
3754         if (inl(TRID_REG(card, ALI_GLOBAL_CONTROL)) & 
3755             (ALI_SPDIF_OUT_CH_ENABLE)) {
3756                 idx = ALI_SPDIF_OUT_CHANNEL;
3757                 if (!(bank->bitmap & (1 << idx))) {
3758                         struct trident_channel *channel = &bank->channels[idx];
3759                         bank->bitmap |= 1 << idx;
3760                         channel->num = idx;
3761                         return channel;
3762                 }
3763         }
3764
3765         for (idx = ALI_PCM_OUT_CHANNEL_FIRST; idx <= ALI_PCM_OUT_CHANNEL_LAST; 
3766              idx++) {
3767                 if (!(bank->bitmap & (1 << idx))) {
3768                         struct trident_channel *channel = &bank->channels[idx];
3769                         bank->bitmap |= 1 << idx;
3770                         channel->num = idx;
3771                         return channel;
3772                 }
3773         }
3774
3775         /* no more free channels avaliable */
3776 #if 0 
3777         printk(KERN_ERR "ali: no more channels available on Bank A.\n");
3778 #endif /* 0 */ 
3779         return NULL;
3780 }
3781
3782 static struct trident_channel *
3783 ali_alloc_rec_pcm_channel(struct trident_card *card)
3784 {
3785         struct trident_pcm_bank *bank;
3786         int idx;
3787
3788         if (inl(TRID_REG(card, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT)
3789                 idx = ALI_SPDIF_IN_CHANNEL;
3790         else
3791                 idx = ALI_PCM_IN_CHANNEL;
3792
3793         bank = &card->banks[BANK_A];
3794
3795         if (!(bank->bitmap & (1 << idx))) {
3796                 struct trident_channel *channel = &bank->channels[idx];
3797                 bank->bitmap |= 1 << idx;
3798                 channel->num = idx;
3799                 return channel;
3800         }
3801
3802         /* no free recordable channels avaliable */
3803 #if 0 
3804         printk(KERN_ERR "ali: no recordable channels available on Bank A.\n");
3805 #endif /* 0 */ 
3806         return NULL;
3807 }
3808
3809 static void
3810 ali_set_spdif_out_rate(struct trident_card *card, unsigned int rate)
3811 {
3812         unsigned char ch_st_sel;
3813         unsigned short status_rate;
3814
3815         switch (rate) {
3816         case 44100:
3817                 status_rate = 0;
3818                 break;
3819         case 32000:
3820                 status_rate = 0x300;
3821                 break;
3822         case 48000:
3823         default:
3824                 status_rate = 0x200;
3825                 break;
3826         }
3827
3828         /* select spdif_out */ 
3829         ch_st_sel = inb(TRID_REG(card, ALI_SPDIF_CTRL)) & ALI_SPDIF_OUT_CH_STATUS;
3830
3831         ch_st_sel |= 0x80;      /* select right */ 
3832         outb(ch_st_sel, TRID_REG(card, ALI_SPDIF_CTRL));
3833         outb(status_rate | 0x20, TRID_REG(card, ALI_SPDIF_CS + 2));
3834
3835         ch_st_sel &= (~0x80);   /* select left */ 
3836         outb(ch_st_sel, TRID_REG(card, ALI_SPDIF_CTRL));
3837         outw(status_rate | 0x10, TRID_REG(card, ALI_SPDIF_CS + 2));
3838 }
3839
3840 static void
3841 ali_address_interrupt(struct trident_card *card)
3842 {
3843         int i, channel;
3844         struct trident_state *state;
3845         u32 mask, channel_mask;
3846
3847         mask = trident_get_interrupt_mask(card, 0);
3848         for (i = 0; i < NR_HW_CH; i++) {
3849                 if ((state = card->states[i]) == NULL)
3850                         continue;
3851                 channel = state->dmabuf.channel->num;
3852                 if ((channel_mask = 1 << channel) & mask) {
3853                         mask &= ~channel_mask;
3854                         trident_ack_channel_interrupt(card, channel);
3855                         udelay(100);
3856                         state->dmabuf.update_flag |= ALI_ADDRESS_INT_UPDATE;
3857                         trident_update_ptr(state);
3858                 }
3859         }
3860         if (mask) {
3861                 for (i = 0; i < NR_HW_CH; i++) {
3862                         if (mask & (1 << i)) {
3863                                 printk("ali: spurious channel irq %d.\n", i);
3864                                 trident_ack_channel_interrupt(card, i);
3865                                 trident_stop_voice(card, i);
3866                                 trident_disable_voice_irq(card, i);
3867                         }
3868                 }
3869         }
3870 }
3871
3872 /* Updating the values of counters of other_states' DMAs without lock 
3873 protection is no harm because all DMAs of multi-channels and interrupt
3874 depend on a master state's DMA, and changing the counters of the master
3875 state DMA is protected by a spinlock.
3876 */
3877 static int
3878 ali_write_5_1(struct trident_state *state, const char __user *buf, 
3879               int cnt_for_multi_channel, unsigned int *copy_count, 
3880               unsigned int *state_cnt)
3881 {
3882
3883         struct dmabuf *dmabuf = &state->dmabuf;
3884         struct dmabuf *dmabuf_temp;
3885         const char __user *buffer = buf;
3886         unsigned swptr, other_dma_nums, sample_s;
3887         unsigned int i, loop;
3888
3889         other_dma_nums = 4;
3890         sample_s = sample_size[dmabuf->fmt] >> 1;
3891         swptr = dmabuf->swptr;
3892
3893         if ((i = state->multi_channels_adjust_count) > 0) {
3894                 if (i == 1) {
3895                         if (copy_from_user(dmabuf->rawbuf + swptr, 
3896                                            buffer, sample_s))
3897                                 return -EFAULT;
3898                         seek_offset(swptr, buffer, cnt_for_multi_channel, 
3899                                     sample_s, *copy_count);
3900                         i--;
3901                         (*state_cnt) += sample_s;
3902                         state->multi_channels_adjust_count++;
3903                 } else
3904                         i = i - (state->chans_num - other_dma_nums);
3905                 for (; (i < other_dma_nums) && (cnt_for_multi_channel > 0); i++) {
3906                         dmabuf_temp = &state->other_states[i]->dmabuf;
3907                         if (copy_from_user(dmabuf_temp->rawbuf + dmabuf_temp->swptr, 
3908                                            buffer, sample_s))
3909                                 return -EFAULT;
3910                         seek_offset(dmabuf_temp->swptr, buffer, cnt_for_multi_channel, 
3911                                     sample_s, *copy_count);
3912                 }
3913                 if (cnt_for_multi_channel == 0)
3914                         state->multi_channels_adjust_count += i;
3915         }
3916         if (cnt_for_multi_channel > 0) {
3917                 loop = cnt_for_multi_channel / (state->chans_num * sample_s);
3918                 for (i = 0; i < loop; i++) {
3919                         if (copy_from_user(dmabuf->rawbuf + swptr, buffer, 
3920                                            sample_s * 2))
3921                                 return -EFAULT;
3922                         seek_offset(swptr, buffer, cnt_for_multi_channel, 
3923                                     sample_s * 2, *copy_count);
3924                         (*state_cnt) += (sample_s * 2);
3925
3926                         dmabuf_temp = &state->other_states[0]->dmabuf;
3927                         if (copy_from_user(dmabuf_temp->rawbuf + dmabuf_temp->swptr, 
3928                                            buffer, sample_s))
3929                                 return -EFAULT;
3930                         seek_offset(dmabuf_temp->swptr, buffer, cnt_for_multi_channel, 
3931                                     sample_s, *copy_count);
3932
3933                         dmabuf_temp = &state->other_states[1]->dmabuf;
3934                         if (copy_from_user(dmabuf_temp->rawbuf + dmabuf_temp->swptr, 
3935                                            buffer, sample_s))
3936                                 return -EFAULT;
3937                         seek_offset(dmabuf_temp->swptr, buffer, cnt_for_multi_channel, 
3938                                     sample_s, *copy_count);
3939
3940                         dmabuf_temp = &state->other_states[2]->dmabuf;
3941                         if (copy_from_user(dmabuf_temp->rawbuf + dmabuf_temp->swptr, 
3942                                            buffer, sample_s))
3943                                 return -EFAULT;
3944                         seek_offset(dmabuf_temp->swptr, buffer, cnt_for_multi_channel, 
3945                                     sample_s, *copy_count);
3946
3947                         dmabuf_temp = &state->other_states[3]->dmabuf;
3948                         if (copy_from_user(dmabuf_temp->rawbuf + dmabuf_temp->swptr, 
3949                                            buffer, sample_s))
3950                                 return -EFAULT;
3951                         seek_offset(dmabuf_temp->swptr, buffer, cnt_for_multi_channel, 
3952                                     sample_s, *copy_count);
3953                 }
3954
3955                 if (cnt_for_multi_channel > 0) {
3956                         state->multi_channels_adjust_count = cnt_for_multi_channel / sample_s;
3957
3958                         if (copy_from_user(dmabuf->rawbuf + swptr, buffer, sample_s))
3959                                 return -EFAULT;
3960                         seek_offset(swptr, buffer, cnt_for_multi_channel, 
3961                                     sample_s, *copy_count);
3962                         (*state_cnt) += sample_s;
3963
3964                         if (cnt_for_multi_channel > 0) {
3965                                 if (copy_from_user(dmabuf->rawbuf + swptr, 
3966                                                    buffer, sample_s))
3967                                         return -EFAULT;
3968                                 seek_offset(swptr, buffer, cnt_for_multi_channel, 
3969                                             sample_s, *copy_count);
3970                                 (*state_cnt) += sample_s;
3971
3972                                 if (cnt_for_multi_channel > 0) {
3973                                         int diff = state->chans_num - other_dma_nums;
3974                                         loop = state->multi_channels_adjust_count - diff;
3975                                         for (i = 0; i < loop; i++) {
3976                                                 dmabuf_temp = &state->other_states[i]->dmabuf;
3977                                                 if (copy_from_user(dmabuf_temp->rawbuf + 
3978                                                                    dmabuf_temp->swptr, 
3979                                                                    buffer, sample_s))
3980                                                         return -EFAULT;
3981                                                 seek_offset(dmabuf_temp->swptr, buffer, 
3982                                                             cnt_for_multi_channel, 
3983                                                             sample_s, *copy_count);
3984                                         }
3985                                 }
3986                         }
3987                 } else
3988                         state->multi_channels_adjust_count = 0;
3989         }
3990         for (i = 0; i < other_dma_nums; i++) {
3991                 dmabuf_temp = &state->other_states[i]->dmabuf;
3992                 dmabuf_temp->swptr = dmabuf_temp->swptr % dmabuf_temp->dmasize;
3993         }
3994         return *state_cnt;
3995 }
3996
3997 static void
3998 ali_free_other_states_resources(struct trident_state *state)
3999 {
4000         int i;
4001         struct trident_card *card = state->card;
4002         struct trident_state *s;
4003         unsigned other_states_count;
4004
4005         other_states_count = state->chans_num - 2; /* except PCM L/R channels */
4006         for (i = 0; i < other_states_count; i++) {
4007                 s = state->other_states[i];
4008                 dealloc_dmabuf(&s->dmabuf, card->pci_dev);
4009                 ali_disable_special_channel(s->card, s->dmabuf.channel->num);
4010                 state->card->free_pcm_channel(s->card, s->dmabuf.channel->num);
4011                 card->states[s->virt] = NULL;
4012                 kfree(s);
4013         }
4014 }
4015
4016 struct proc_dir_entry *res;
4017 static int
4018 ali_write_proc(struct file *file, const char __user *buffer, unsigned long count, void *data)
4019 {
4020         struct trident_card *card = (struct trident_card *) data;
4021         unsigned long flags;
4022         char c;
4023
4024         if (count < 0)
4025                 return -EINVAL;
4026         if (count == 0)
4027                 return 0;
4028         if (get_user(c, buffer))
4029                 return -EFAULT;
4030
4031         spin_lock_irqsave(&card->lock, flags);
4032         switch (c) {
4033         case '0':
4034                 ali_setup_spdif_out(card, ALI_PCM_TO_SPDIF_OUT);
4035                 ali_disable_special_channel(card, ALI_SPDIF_OUT_CHANNEL);
4036                 break;
4037         case '1':
4038                 ali_setup_spdif_out(card, ALI_SPDIF_OUT_TO_SPDIF_OUT | 
4039                                     ALI_SPDIF_OUT_PCM);
4040                 break;
4041         case '2':
4042                 ali_setup_spdif_out(card, ALI_SPDIF_OUT_TO_SPDIF_OUT | 
4043                                     ALI_SPDIF_OUT_NON_PCM);
4044                 break;
4045         case '3':
4046                 ali_disable_spdif_in(card);     //default
4047                 break;
4048         case '4':
4049                 ali_setup_spdif_in(card);
4050                 break;
4051         }
4052         spin_unlock_irqrestore(&card->lock, flags);
4053
4054         return count;
4055 }
4056
4057 /* OSS /dev/mixer file operation methods */
4058 static int
4059 trident_open_mixdev(struct inode *inode, struct file *file)
4060 {
4061         int i = 0;
4062         int minor = iminor(inode);
4063         struct trident_card *card = devs;
4064
4065         for (card = devs; card != NULL; card = card->next)
4066                 for (i = 0; i < NR_AC97; i++)
4067                         if (card->ac97_codec[i] != NULL && 
4068                             card->ac97_codec[i]->dev_mixer == minor)
4069                                 goto match;
4070
4071         if (!card) {
4072                 return -ENODEV;
4073         }
4074       match:
4075         file->private_data = card->ac97_codec[i];
4076
4077         return nonseekable_open(inode, file);
4078 }
4079
4080 static int
4081 trident_ioctl_mixdev(struct inode *inode, struct file *file, unsigned int cmd, 
4082                      unsigned long arg)
4083 {
4084         struct ac97_codec *codec = (struct ac97_codec *) file->private_data;
4085
4086         return codec->mixer_ioctl(codec, cmd, arg);
4087 }
4088
4089 static /*const */ struct file_operations trident_mixer_fops = {
4090         .owner = THIS_MODULE,
4091         .llseek = no_llseek,
4092         .ioctl = trident_ioctl_mixdev,
4093         .open = trident_open_mixdev,
4094 };
4095
4096 static int
4097 ali_reset_5451(struct trident_card *card)
4098 {
4099         struct pci_dev *pci_dev = NULL;
4100         unsigned int dwVal;
4101         unsigned short wCount, wReg;
4102
4103         pci_dev = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, 
4104                                   pci_dev);
4105         if (pci_dev == NULL)
4106                 return -1;
4107
4108         pci_read_config_dword(pci_dev, 0x7c, &dwVal);
4109         pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
4110         udelay(5000);
4111         pci_read_config_dword(pci_dev, 0x7c, &dwVal);
4112         pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
4113         udelay(5000);
4114
4115         pci_dev = card->pci_dev;
4116         if (pci_dev == NULL)
4117                 return -1;
4118
4119         pci_read_config_dword(pci_dev, 0x44, &dwVal);
4120         pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
4121         udelay(500);
4122         pci_read_config_dword(pci_dev, 0x44, &dwVal);
4123         pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
4124         udelay(5000);
4125
4126         /* TODO: recognize if we have a PM capable codec and only do this */
4127         /* if the codec is PM capable */
4128         wCount = 2000;
4129         while (wCount--) {
4130                 wReg = ali_ac97_get(card, 0, AC97_POWER_CONTROL);
4131                 if ((wReg & 0x000f) == 0x000f)
4132                         return 0;
4133                 udelay(5000);
4134         }
4135         /* This is non fatal if you have a non PM capable codec.. */
4136         return 0;
4137 }
4138
4139 /* AC97 codec initialisation. */
4140 static int __devinit
4141 trident_ac97_init(struct trident_card *card)
4142 {
4143         int num_ac97 = 0;
4144         unsigned long ready_2nd = 0;
4145         struct ac97_codec *codec;
4146         int i = 0;
4147
4148         /* initialize controller side of AC link, and find out if secondary codes
4149            really exist */
4150         switch (card->pci_id) {
4151         case PCI_DEVICE_ID_ALI_5451:
4152                 if (ali_reset_5451(card)) {
4153                         printk(KERN_ERR "trident_ac97_init: error "
4154                                "resetting 5451.\n");
4155                         return -1;
4156                 }
4157                 outl(0x80000001, TRID_REG(card, ALI_GLOBAL_CONTROL));
4158                 outl(0x00000000, TRID_REG(card, T4D_AINTEN_A));
4159                 outl(0xffffffff, TRID_REG(card, T4D_AINT_A));
4160                 outl(0x00000000, TRID_REG(card, T4D_MUSICVOL_WAVEVOL));
4161                 outb(0x10, TRID_REG(card, ALI_MPUR2));
4162                 ready_2nd = inl(TRID_REG(card, ALI_SCTRL));
4163                 ready_2nd &= 0x3fff;
4164                 outl(ready_2nd | PCMOUT | 0x8000, TRID_REG(card, ALI_SCTRL));
4165                 ready_2nd = inl(TRID_REG(card, ALI_SCTRL));
4166                 ready_2nd &= SI_AC97_SECONDARY_READY;
4167                 if (card->revision < ALI_5451_V02)
4168                         ready_2nd = 0;
4169                 break;
4170         case PCI_DEVICE_ID_SI_7018:
4171                 /* disable AC97 GPIO interrupt */
4172                 outl(0x00, TRID_REG(card, SI_AC97_GPIO));
4173                 /* when power up the AC link is in cold reset mode so stop it */
4174                 outl(PCMOUT | SURROUT | CENTEROUT | LFEOUT | SECONDARY_ID, 
4175                      TRID_REG(card, SI_SERIAL_INTF_CTRL));
4176                 /* it take a long time to recover from a cold reset */ 
4177                 /* (especially when you have more than one codec) */
4178                 udelay(2000);
4179                 ready_2nd = inl(TRID_REG(card, SI_SERIAL_INTF_CTRL));
4180                 ready_2nd &= SI_AC97_SECONDARY_READY;
4181                 break;
4182         case PCI_DEVICE_ID_TRIDENT_4DWAVE_DX:
4183                 /* playback on */
4184                 outl(DX_AC97_PLAYBACK, TRID_REG(card, DX_ACR2_AC97_COM_STAT));
4185                 break;
4186         case PCI_DEVICE_ID_TRIDENT_4DWAVE_NX:
4187                 /* enable AC97 Output Slot 3,4 (PCM Left/Right Playback) */
4188                 outl(NX_AC97_PCM_OUTPUT, TRID_REG(card, NX_ACR0_AC97_COM_STAT));
4189                 ready_2nd = inl(TRID_REG(card, NX_ACR0_AC97_COM_STAT));
4190                 ready_2nd &= NX_AC97_SECONDARY_READY;
4191                 break;
4192         case PCI_DEVICE_ID_INTERG_5050:
4193                 /* disable AC97 GPIO interrupt */
4194                 outl(0x00, TRID_REG(card, SI_AC97_GPIO));
4195                 /* when power up, the AC link is in cold reset mode, so stop it */
4196                 outl(PCMOUT | SURROUT | CENTEROUT | LFEOUT, 
4197                      TRID_REG(card, SI_SERIAL_INTF_CTRL));
4198                 /* it take a long time to recover from a cold reset (especially */ 
4199                 /* when you have more than one codec) */
4200                 udelay(2000);
4201                 ready_2nd = inl(TRID_REG(card, SI_SERIAL_INTF_CTRL));
4202                 ready_2nd &= SI_AC97_SECONDARY_READY;
4203                 break;
4204         }
4205
4206         for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
4207                 if ((codec = ac97_alloc_codec()) == NULL)
4208                         return -ENOMEM;
4209
4210                 /* initialize some basic codec information, other fields */ 
4211                 /* will be filled in ac97_probe_codec */
4212                 codec->private_data = card;
4213                 codec->id = num_ac97;
4214
4215                 if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
4216                         codec->codec_read = ali_ac97_read;
4217                         codec->codec_write = ali_ac97_write;
4218                 } else {
4219                         codec->codec_read = trident_ac97_get;
4220                         codec->codec_write = trident_ac97_set;
4221                 }
4222
4223                 if (ac97_probe_codec(codec) == 0)
4224                         break;
4225
4226                 codec->dev_mixer = register_sound_mixer(&trident_mixer_fops, -1);
4227                 if (codec->dev_mixer < 0) {
4228                         printk(KERN_ERR "trident: couldn't register mixer!\n");
4229                         ac97_release_codec(codec);
4230                         break;
4231                 }
4232
4233                 card->ac97_codec[num_ac97] = codec;
4234
4235                 /* if there is no secondary codec at all, don't probe any more */
4236                 if (!ready_2nd)
4237                         break;
4238         }
4239
4240         if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
4241                 for (num_ac97 = 0; num_ac97 < NR_AC97; num_ac97++) {
4242                         if (card->ac97_codec[num_ac97] == NULL)
4243                                 break;
4244                         for (i = 0; i < 64; i++) {
4245                                 u16 reg = ali_ac97_get(card, num_ac97, i * 2);
4246                                 card->mixer_regs[i][num_ac97] = reg;
4247                         }
4248                 }
4249         }
4250         return num_ac97 + 1;
4251 }
4252
4253 /* Gameport functions for the cards ADC gameport */
4254
4255 static unsigned char
4256 trident_game_read(struct gameport *gameport)
4257 {
4258         struct trident_card *card = gameport->driver;
4259         return inb(TRID_REG(card, T4D_GAME_LEG));
4260 }
4261
4262 static void
4263 trident_game_trigger(struct gameport *gameport)
4264 {
4265         struct trident_card *card = gameport->driver;
4266         outb(0xff, TRID_REG(card, T4D_GAME_LEG));
4267 }
4268
4269 static int
4270 trident_game_cooked_read(struct gameport *gameport, int *axes, int *buttons)
4271 {
4272         struct trident_card *card = gameport->driver;
4273         int i;
4274
4275         *buttons = (~inb(TRID_REG(card, T4D_GAME_LEG)) >> 4) & 0xf;
4276
4277         for (i = 0; i < 4; i++) {
4278                 axes[i] = inw(TRID_REG(card, T4D_GAME_AXD) + i * sizeof (u16));
4279                 if (axes[i] == 0xffff)
4280                         axes[i] = -1;
4281         }
4282
4283         return 0;
4284 }
4285
4286 static int
4287 trident_game_open(struct gameport *gameport, int mode)
4288 {
4289         struct trident_card *card = gameport->driver;
4290
4291         switch (mode) {
4292         case GAMEPORT_MODE_COOKED:
4293                 outb(0x80, TRID_REG(card, T4D_GAME_CR));
4294                 msleep(20);
4295                 return 0;
4296         case GAMEPORT_MODE_RAW:
4297                 outb(0x00, TRID_REG(card, T4D_GAME_CR));
4298                 return 0;
4299         default:
4300                 return -1;
4301         }
4302
4303         return 0;
4304 }
4305
4306 /* install the driver, we do not allocate hardware channel nor DMA buffer */ 
4307 /* now, they are defered until "ACCESS" time (in prog_dmabuf called by */ 
4308 /* open/read/write/ioctl/mmap) */
4309 static int __devinit
4310 trident_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
4311 {
4312         unsigned long iobase;
4313         struct trident_card *card;
4314         u8 bits;
4315         u8 revision;
4316         int i = 0;
4317         u16 temp;
4318         struct pci_dev *pci_dev_m1533 = NULL;
4319         int rc = -ENODEV;
4320         u64 dma_mask;
4321
4322         if (pci_enable_device(pci_dev))
4323                 goto out;
4324
4325         if (pci_dev->device == PCI_DEVICE_ID_ALI_5451)
4326                 dma_mask = ALI_DMA_MASK;
4327         else
4328                 dma_mask = TRIDENT_DMA_MASK;
4329         if (pci_set_dma_mask(pci_dev, dma_mask)) {
4330                 printk(KERN_ERR "trident: architecture does not support" 
4331                        " %s PCI busmaster DMA\n", 
4332                        pci_dev->device == PCI_DEVICE_ID_ALI_5451 ? 
4333                        "32-bit" : "30-bit");
4334                 goto out;
4335         }
4336         pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
4337
4338         if (pci_id->device == PCI_DEVICE_ID_INTERG_5050)
4339                 iobase = pci_resource_start(pci_dev, 1);
4340         else
4341                 iobase = pci_resource_start(pci_dev, 0);
4342
4343         if (!request_region(iobase, 256, card_names[pci_id->driver_data])) {
4344                 printk(KERN_ERR "trident: can't allocate I/O space at "
4345                        "0x%4.4lx\n", iobase);
4346                 goto out;
4347         }
4348
4349         rc = -ENOMEM;
4350         if ((card = kmalloc(sizeof(*card), GFP_KERNEL)) == NULL) {
4351                 printk(KERN_ERR "trident: out of memory\n");
4352                 goto out_release_region;
4353         }
4354         memset(card, 0, sizeof (*card));
4355
4356         init_timer(&card->timer);
4357         card->iobase = iobase;
4358         card->pci_dev = pci_dev;
4359         card->pci_id = pci_id->device;
4360         card->revision = revision;
4361         card->irq = pci_dev->irq;
4362         card->next = devs;
4363         card->magic = TRIDENT_CARD_MAGIC;
4364         card->banks[BANK_A].addresses = &bank_a_addrs;
4365         card->banks[BANK_A].bitmap = 0UL;
4366         card->banks[BANK_B].addresses = &bank_b_addrs;
4367         card->banks[BANK_B].bitmap = 0UL;
4368
4369         card->gameport.driver = card;
4370         card->gameport.fuzz = 64;
4371         card->gameport.read = trident_game_read;
4372         card->gameport.trigger = trident_game_trigger;
4373         card->gameport.cooked_read = trident_game_cooked_read;
4374         card->gameport.open = trident_game_open;
4375
4376         init_MUTEX(&card->open_sem);
4377         spin_lock_init(&card->lock);
4378         init_timer(&card->timer);
4379
4380         devs = card;
4381
4382         pci_set_master(pci_dev);
4383
4384         printk(KERN_INFO "trident: %s found at IO 0x%04lx, IRQ %d\n", 
4385                card_names[pci_id->driver_data], card->iobase, card->irq);
4386
4387         if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
4388                 /* ALi channel Management */
4389                 card->alloc_pcm_channel = ali_alloc_pcm_channel;
4390                 card->alloc_rec_pcm_channel = ali_alloc_rec_pcm_channel;
4391                 card->free_pcm_channel = ali_free_pcm_channel;
4392
4393                 card->address_interrupt = ali_address_interrupt;
4394
4395                 /* Added by Matt Wu 01-05-2001 for spdif in */
4396                 card->multi_channel_use_count = 0;
4397                 card->rec_channel_use_count = 0;
4398
4399                 /* ALi SPDIF OUT function */
4400                 if (card->revision == ALI_5451_V02) {
4401                         ali_setup_spdif_out(card, ALI_PCM_TO_SPDIF_OUT);
4402                         res = create_proc_entry("ALi5451", 0, NULL);
4403                         if (res) {
4404                                 res->write_proc = ali_write_proc;
4405                                 res->data = card;
4406                         }
4407                 }
4408
4409                 /* Add H/W Volume Control By Matt Wu Jul. 06, 2001 */
4410                 card->hwvolctl = 0;
4411                 pci_dev_m1533 = pci_find_device(PCI_VENDOR_ID_AL, 
4412                                                 PCI_DEVICE_ID_AL_M1533, 
4413                                                 pci_dev_m1533);
4414                 rc = -ENODEV;
4415                 if (pci_dev_m1533 == NULL)
4416                         goto out_proc_fs;
4417                 pci_read_config_byte(pci_dev_m1533, 0x63, &bits);
4418                 if (bits & (1 << 5))
4419                         card->hwvolctl = 1;
4420                 if (card->hwvolctl) {
4421                         /* Clear m1533 pci cfg 78h bit 30 to zero, which makes
4422                            GPIO11/12/13 work as ACGP_UP/DOWN/MUTE. */
4423                         pci_read_config_byte(pci_dev_m1533, 0x7b, &bits);
4424                         bits &= 0xbf;   /*clear bit 6 */
4425                         pci_write_config_byte(pci_dev_m1533, 0x7b, bits);
4426                 }
4427         } else if (card->pci_id == PCI_DEVICE_ID_INTERG_5050) {
4428                 card->alloc_pcm_channel = cyber_alloc_pcm_channel;
4429                 card->alloc_rec_pcm_channel = cyber_alloc_pcm_channel;
4430                 card->free_pcm_channel = cyber_free_pcm_channel;
4431                 card->address_interrupt = cyber_address_interrupt;
4432                 cyber_init_ritual(card);
4433         } else {
4434                 card->alloc_pcm_channel = trident_alloc_pcm_channel;
4435                 card->alloc_rec_pcm_channel = trident_alloc_pcm_channel;
4436                 card->free_pcm_channel = trident_free_pcm_channel;
4437                 card->address_interrupt = trident_address_interrupt;
4438         }
4439
4440         /* claim our irq */
4441         rc = -ENODEV;
4442         if (request_irq(card->irq, &trident_interrupt, SA_SHIRQ, 
4443                         card_names[pci_id->driver_data], card)) {
4444                 printk(KERN_ERR "trident: unable to allocate irq %d\n", 
4445                        card->irq);
4446                 goto out_proc_fs;
4447         }
4448         /* register /dev/dsp */
4449         if ((card->dev_audio = register_sound_dsp(&trident_audio_fops, -1)) < 0) {
4450                 printk(KERN_ERR "trident: couldn't register DSP device!\n");
4451                 goto out_free_irq;
4452         }
4453         card->mixer_regs_ready = 0;
4454         /* initialize AC97 codec and register /dev/mixer */
4455         if (trident_ac97_init(card) <= 0) {
4456                 /* unregister audio devices */
4457                 for (i = 0; i < NR_AC97; i++) {
4458                         if (card->ac97_codec[i] != NULL) {
4459                                 struct ac97_codec* codec = card->ac97_codec[i];
4460                                 unregister_sound_mixer(codec->dev_mixer);
4461                                 ac97_release_codec(codec);
4462                         }
4463                 }
4464                 goto out_unregister_sound_dsp;
4465         }
4466         card->mixer_regs_ready = 1;
4467         outl(0x00, TRID_REG(card, T4D_MUSICVOL_WAVEVOL));
4468
4469         if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
4470                 /* Add H/W Volume Control By Matt Wu Jul. 06, 2001 */
4471                 if (card->hwvolctl) {
4472                         /* Enable GPIO IRQ (MISCINT bit 18h) */
4473                         temp = inw(TRID_REG(card, T4D_MISCINT + 2));
4474                         temp |= 0x0004;
4475                         outw(temp, TRID_REG(card, T4D_MISCINT + 2));
4476
4477                         /* Enable H/W Volume Control GLOVAL CONTROL bit 0 */
4478                         temp = inw(TRID_REG(card, ALI_GLOBAL_CONTROL));
4479                         temp |= 0x0001;
4480                         outw(temp, TRID_REG(card, ALI_GLOBAL_CONTROL));
4481
4482                 }
4483                 if (card->revision == ALI_5451_V02)
4484                         ali_close_multi_channels();
4485                 /* edited by HMSEO for GT sound */
4486 #if defined(CONFIG_ALPHA_NAUTILUS) || defined(CONFIG_ALPHA_GENERIC)
4487                 {
4488                         u16 ac97_data;
4489                         extern struct hwrpb_struct *hwrpb;
4490
4491                         if ((hwrpb->sys_type) == 201) {
4492                                 printk(KERN_INFO "trident: Running on Alpha system "
4493                                        "type Nautilus\n");
4494                                 ac97_data = ali_ac97_get(card, 0, AC97_POWER_CONTROL);
4495                                 ali_ac97_set(card, 0, AC97_POWER_CONTROL, 
4496                                              ac97_data | ALI_EAPD_POWER_DOWN);
4497                         }
4498                 }
4499 #endif                          /* CONFIG_ALPHA_NAUTILUS || CONFIG_ALPHA_GENERIC */
4500                 /* edited by HMSEO for GT sound */
4501         }
4502         rc = 0;
4503         pci_set_drvdata(pci_dev, card);
4504
4505         /* Enable Address Engine Interrupts */
4506         trident_enable_loop_interrupts(card);
4507
4508         /* Register gameport */
4509         gameport_register_port(&card->gameport);
4510
4511 out:
4512         return rc;
4513
4514 out_unregister_sound_dsp:
4515         unregister_sound_dsp(card->dev_audio);
4516 out_free_irq:
4517         free_irq(card->irq, card);
4518 out_proc_fs:
4519         if (res) {
4520                 remove_proc_entry("ALi5451", NULL);
4521                 res = NULL;
4522         }
4523         kfree(card);
4524         devs = NULL;
4525 out_release_region:
4526         release_region(iobase, 256);
4527         return rc; 
4528 }
4529
4530 static void __devexit
4531 trident_remove(struct pci_dev *pci_dev)
4532 {
4533         int i;
4534         struct trident_card *card = pci_get_drvdata(pci_dev);
4535
4536         /*
4537          *      Kill running timers before unload. We can't have them
4538          *      going off after rmmod!
4539          */
4540         if (card->hwvolctl)
4541                 del_timer_sync(&card->timer);
4542
4543         /* ALi S/PDIF and Power Management */
4544         if (card->pci_id == PCI_DEVICE_ID_ALI_5451) {
4545                 ali_setup_spdif_out(card, ALI_PCM_TO_SPDIF_OUT);
4546                 ali_disable_special_channel(card, ALI_SPDIF_OUT_CHANNEL);
4547                 ali_disable_spdif_in(card);
4548                 remove_proc_entry("ALi5451", NULL);
4549         }
4550
4551         /* Unregister gameport */
4552         gameport_unregister_port(&card->gameport);
4553
4554         /* Kill interrupts, and SP/DIF */
4555         trident_disable_loop_interrupts(card);
4556
4557         /* free hardware resources */
4558         free_irq(card->irq, card);
4559         release_region(card->iobase, 256);
4560
4561         /* unregister audio devices */
4562         for (i = 0; i < NR_AC97; i++)
4563                 if (card->ac97_codec[i] != NULL) {
4564                         unregister_sound_mixer(card->ac97_codec[i]->dev_mixer);
4565                         ac97_release_codec(card->ac97_codec[i]);
4566                 }
4567         unregister_sound_dsp(card->dev_audio);
4568
4569         kfree(card);
4570
4571         pci_set_drvdata(pci_dev, NULL);
4572 }
4573
4574 MODULE_AUTHOR("Alan Cox, Aaron Holtzman, Ollie Lho, Ching Ling Lee, Muli Ben-Yehuda");
4575 MODULE_DESCRIPTION("Trident 4DWave/SiS 7018/ALi 5451 and Tvia/IGST CyberPro5050 PCI "
4576                    "Audio Driver");
4577 MODULE_LICENSE("GPL");
4578
4579 #define TRIDENT_MODULE_NAME "trident"
4580
4581 static struct pci_driver trident_pci_driver = {
4582         .name = TRIDENT_MODULE_NAME,
4583         .id_table = trident_pci_tbl,
4584         .probe = trident_probe,
4585         .remove = __devexit_p(trident_remove),
4586         .suspend = trident_suspend,
4587         .resume = trident_resume
4588 };
4589
4590 static int __init
4591 trident_init_module(void)
4592 {
4593         printk(KERN_INFO "Trident 4DWave/SiS 7018/ALi 5451,Tvia CyberPro " 
4594                "5050 PCI Audio, version " DRIVER_VERSION ", " __TIME__ " " 
4595                __DATE__ "\n");
4596
4597         if (!pci_register_driver(&trident_pci_driver)) {
4598                 pci_unregister_driver(&trident_pci_driver);
4599                 return -ENODEV;
4600         }
4601         return 0;
4602 }
4603
4604 static void __exit
4605 trident_cleanup_module(void)
4606 {
4607         pci_unregister_driver(&trident_pci_driver);
4608 }
4609
4610 module_init(trident_init_module);
4611 module_exit(trident_cleanup_module);