ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / pci / ali5451 / ali5451.c
1 /*
2  *  Matt Wu <Matt_Wu@acersoftech.com.cn>
3  *  Apr 26, 2001
4  *  Routines for control of ALi pci audio M5451
5  *
6  *  BUGS:
7  *    --
8  *
9  *  TODO:
10  *    --
11  *
12  *   This program is free software; you can redistribute it and/or modify
13  *   it under the terms of the GNU General Public Lcodecnse as published by
14  *   the Free Software Foundation; either version 2 of the Lcodecnse, or
15  *   (at your option) any later version.
16  *
17  *   This program is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public Lcodecnse for more details.
21  *
22  *   You should have received a copy of the GNU General Public Lcodecnse
23  *   along with this program; if not, write to the Free Software
24  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  *
26  */
27
28 #define __SNDRV_OSS_COMPAT__
29
30 #include <sound/driver.h>
31 #include <asm/io.h>
32 #include <linux/delay.h>
33 #include <linux/interrupt.h>
34 #include <linux/init.h>
35 #include <linux/pci.h>
36 #include <linux/slab.h>
37 #include <sound/core.h>
38 #include <sound/pcm.h>
39 #include <sound/info.h>
40 #include <sound/ac97_codec.h>
41 #include <sound/mpu401.h>
42 #define SNDRV_GET_ID
43 #include <sound/initval.h>
44
45 MODULE_AUTHOR("Matt Wu <Matt_Wu@acersoftech.com.cn>");
46 MODULE_DESCRIPTION("ALI M5451");
47 MODULE_LICENSE("GPL");
48 MODULE_CLASSES("{sound}");
49 MODULE_DEVICES("{{ALI,M5451,pci},{ALI,M5451}}");
50
51 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
52 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
53 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
54 static int pcm_channels[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 32};
55 static int spdif[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
56
57 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
58 MODULE_PARM_DESC(index, "Index value for ALI M5451 PCI Audio.");
59 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
60 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
61 MODULE_PARM_DESC(id, "ID string for ALI M5451 PCI Audio.");
62 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
63 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
64 MODULE_PARM_DESC(enable, "Enable ALI 5451 PCI Audio.");
65 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
66 MODULE_PARM(pcm_channels, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
67 MODULE_PARM_DESC(pcm_channels, "PCM Channels");
68 MODULE_PARM_SYNTAX(pcm_channels, SNDRV_ENABLED ",default:32,allows:{{1,32}}");
69 MODULE_PARM(spdif, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
70 MODULE_PARM_DESC(spdif, "Support SPDIF I/O");
71 MODULE_PARM_SYNTAX(spdif, SNDRV_ENABLED "," SNDRV_BOOLEAN_FALSE_DESC);
72
73 /*
74  *  Debug part definitions
75  */
76
77 //#define ALI_DEBUG
78
79 #ifdef ALI_DEBUG
80 #define snd_ali_printk(format, args...) printk(format, ##args);
81 #else
82 #define snd_ali_printk(format, args...)
83 #endif
84
85 /*
86  *  Constants definition
87  */
88
89 #ifndef PCI_VENDOR_ID_ALI
90 #define PCI_VENDOR_ID_ALI       0x10b9
91 #endif
92
93 #ifndef PCI_DEVICE_ID_ALI_5451
94 #define PCI_DEVICE_ID_ALI_5451  0x5451
95 #endif
96
97 #define DEVICE_ID_ALI5451       ((PCI_VENDOR_ID_ALI<<16)|PCI_DEVICE_ID_ALI_5451)
98
99
100 #define ALI_CHANNELS            32
101
102 #define ALI_PCM_IN_CHANNEL      31
103 #define ALI_SPDIF_IN_CHANNEL    19
104 #define ALI_SPDIF_OUT_CHANNEL   15
105 #define ALI_CENTER_CHANNEL      24
106 #define ALI_LEF_CHANNEL         23
107 #define ALI_SURR_LEFT_CHANNEL   26
108 #define ALI_SURR_RIGHT_CHANNEL  25
109
110 #define SNDRV_ALI_VOICE_TYPE_PCM        01
111 #define SNDRV_ALI_VOICE_TYPE_OTH        02
112
113 #define ALI_5451_V02            0x02
114
115 /*
116  *  Direct Registers
117  */
118
119 #define ALI_LEGACY_DMAR0        0x00  // ADR0
120 #define ALI_LEGACY_DMAR4        0x04  // CNT0
121 #define ALI_LEGACY_DMAR11       0x0b  // MOD 
122 #define ALI_LEGACY_DMAR15       0x0f  // MMR 
123 #define ALI_MPUR0               0x20
124 #define ALI_MPUR1               0x21
125 #define ALI_MPUR2               0x22
126 #define ALI_MPUR3               0x23
127
128 #define ALI_AC97_WRITE          0x40
129 #define ALI_AC97_READ           0x44
130
131 #define ALI_SCTRL               0x48
132 #define   ALI_SPDIF_OUT_ENABLE          0x20
133 #define ALI_AC97_GPIO           0x4c
134 #define ALI_SPDIF_CS            0x70
135 #define ALI_SPDIF_CTRL          0x74
136 #define   ALI_SPDIF_IN_FUNC_ENABLE      0x02
137 #define   ALI_SPDIF_IN_CH_STATUS        0x40
138 #define   ALI_SPDIF_OUT_CH_STATUS       0xbf
139 #define ALI_START               0x80
140 #define ALI_STOP                0x84
141 #define ALI_CSPF                0x90
142 #define ALI_AINT                0x98
143 #define ALI_GC_CIR              0xa0
144         #define ENDLP_IE                0x00001000
145         #define MIDLP_IE                0x00002000
146 #define ALI_AINTEN              0xa4
147 #define ALI_VOLUME              0xa8
148 #define ALI_SBDELTA_DELTA_R     0xac
149 #define ALI_MISCINT             0xb0
150         #define ADDRESS_IRQ             0x00000020
151         #define TARGET_REACHED          0x00008000
152         #define MIXER_OVERFLOW          0x00000800
153         #define MIXER_UNDERFLOW         0x00000400
154 #define ALI_SBBL_SBCL           0xc0
155 #define ALI_SBCTRL_SBE2R_SBDD   0xc4
156 #define ALI_STIMER              0xc8
157 #define ALI_GLOBAL_CONTROL      0xd4
158 #define   ALI_SPDIF_OUT_SEL_PCM         0x00000400 /* bit 10 */
159 #define   ALI_SPDIF_IN_SUPPORT          0x00000800 /* bit 11 */
160 #define   ALI_SPDIF_OUT_CH_ENABLE       0x00008000 /* bit 15 */
161 #define   ALI_SPDIF_IN_CH_ENABLE        0x00080000 /* bit 19 */
162 #define   ALI_PCM_IN_ENABLE             0x80000000 /* bit 31 */
163
164 #define ALI_CSO_ALPHA_FMS       0xe0
165 #define ALI_LBA                 0xe4
166 #define ALI_ESO_DELTA           0xe8
167 #define ALI_GVSEL_PAN_VOC_CTRL_EC       0xf0
168 #define ALI_EBUF1               0xf4
169 #define ALI_EBUF2               0xf8
170
171 #define ALI_REG(codec, x) ((codec)->port + x)
172
173 typedef struct snd_stru_ali ali_t;
174 typedef struct snd_ali_stru_voice snd_ali_voice_t;
175 #define chip_t ali_t
176
177 typedef struct snd_ali_channel_control {
178         // register data
179         struct REGDATA {
180                 unsigned int start;
181                 unsigned int stop;
182                 unsigned int aint;
183                 unsigned int ainten;
184         } data;
185                 
186         // register addresses
187         struct REGS {
188                 unsigned int start;
189                 unsigned int stop;
190                 unsigned int aint;
191                 unsigned int ainten;
192                 unsigned int ac97read;
193                 unsigned int ac97write;
194         } regs;
195
196 } snd_ali_channel_control_t;
197
198 struct snd_ali_stru_voice {
199         unsigned int number;
200         int use: 1,
201             pcm: 1,
202             midi: 1,
203             mode: 1,
204             synth: 1;
205
206         /* PCM data */
207         ali_t *codec;
208         snd_pcm_substream_t *substream;
209         snd_ali_voice_t *extra;
210         
211         int running: 1;
212
213         int eso;                /* final ESO value for channel */
214         int count;              /* runtime->period_size */
215
216         /* --- */
217
218         void *private_data;
219         void (*private_free)(void *private_data);
220 };
221
222
223 typedef struct snd_stru_alidev {
224
225         snd_ali_voice_t voices[ALI_CHANNELS];   
226
227         unsigned int    chcnt;                  /* num of opened channels */
228         unsigned int    chmap;                  /* bitmap for opened channels */
229         unsigned int synthcount;
230
231 } alidev_t;
232
233
234 #ifdef CONFIG_PM
235 #define ALI_GLOBAL_REGS         56
236 #define ALI_CHANNEL_REGS        8
237 typedef struct snd_ali_image {
238         unsigned long regs[ALI_GLOBAL_REGS];
239         unsigned long channel_regs[ALI_CHANNELS][ALI_CHANNEL_REGS];
240 } ali_image_t;
241 #endif
242
243
244 struct snd_stru_ali {
245         unsigned long   irq;
246         unsigned long   port;
247         unsigned char   revision;
248
249         unsigned int hw_initialized: 1;
250         unsigned int spdif_support: 1;
251         struct resource *res_port;
252
253         struct pci_dev  *pci;
254         struct pci_dev  *pci_m1533;
255         struct pci_dev  *pci_m7101;
256
257         snd_card_t      *card;
258         snd_pcm_t       *pcm;
259         alidev_t        synth;
260         snd_ali_channel_control_t chregs;
261
262         /* S/PDIF Mask */
263         unsigned int    spdif_mask;
264
265         unsigned int spurious_irq_count;
266         unsigned int spurious_irq_max_delta;
267
268         ac97_bus_t *ac97_bus;
269         ac97_t *ac97;
270         unsigned short  ac97_ext_id;
271         unsigned short  ac97_ext_status;
272
273         spinlock_t      reg_lock;
274         spinlock_t      voice_alloc;
275
276 #ifdef CONFIG_PM
277         ali_image_t *image;
278 #endif
279 };
280
281 static struct pci_device_id snd_ali_ids[] = {
282         {0x10b9, 0x5451, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
283         {0, }
284 };
285 MODULE_DEVICE_TABLE(pci, snd_ali_ids);
286
287 static void snd_ali_clear_voices(ali_t *, unsigned int, unsigned int);
288 static unsigned short snd_ali_codec_peek(ali_t *, int, unsigned short);
289 static void snd_ali_codec_poke(ali_t *, int, unsigned short, unsigned short);
290
291 /*
292  *  Debug Part
293  */
294
295 #ifdef ALI_DEBUG
296
297 static void ali_read_regs(ali_t *codec, int channel)
298 {
299         int i,j;
300         unsigned int dwVal;
301
302         printk("channel %d registers map:\n", channel);
303         outb((unsigned char)(channel & 0x001f), ALI_REG(codec,ALI_GC_CIR));
304
305         printk("    ");
306         for(j=0;j<8;j++)
307                 printk("%2.2x       ", j*4);
308         printk("\n");
309
310         for (i=0; i<=0xf8/4;i++) {
311                 if(i%8 == 0)
312                         printk("%2.2x  ", (i*4/0x10)*0x10);
313                 dwVal = inl(ALI_REG(codec,i*4));
314                 printk("%8.8x ", dwVal);
315                 if ((i+1)%8 == 0)
316                         printk("\n");
317         }
318         printk("\n");
319 }
320 static void ali_read_cfg(unsigned int vendor, unsigned deviceid)
321 {
322         unsigned int dwVal;
323         struct pci_dev *pci_dev = NULL;
324         int i,j;
325
326
327         pci_dev = pci_find_device(vendor, deviceid, pci_dev);
328         if (pci_dev == NULL)
329                 return ;
330
331         printk("\nM%x PCI CFG\n", deviceid);
332         printk("    ");
333         for(j=0;j<8;j++)
334                 printk("%d        ",j);
335         printk("\n");
336
337         for(i=0;i<8;i++) {
338                 printk("%d   ",i);
339                 for(j=0;j<8;j++)
340                 {
341                         pci_read_config_dword(pci_dev, i*0x20+j*4, &dwVal);
342                         printk("%8.8x ", dwVal);
343                 }
344                 printk("\n");
345         }
346  }
347 static void ali_read_ac97regs(ali_t *codec, int secondary)
348 {
349         unsigned short i,j;
350         unsigned short wVal;
351
352         printk("\ncodec %d registers map:\n", secondary);
353
354         printk("    ");
355         for(j=0;j<8;j++)
356                 printk("%2.2x   ",j*2);
357         printk("\n");
358
359         for (i=0; i<64;i++) {
360                 if(i%8 == 0)
361                         printk("%2.2x  ", (i/8)*0x10);
362                 wVal = snd_ali_codec_peek(codec, secondary, i*2);
363                 printk("%4.4x ", wVal);
364                 if ((i+1)%8 == 0)
365                         printk("\n");
366         }
367         printk("\n");
368 }
369
370 #endif
371
372 /*
373  *  AC97 ACCESS
374  */
375
376 static inline unsigned int snd_ali_5451_peek(ali_t *codec,
377                                                 unsigned int port )
378 {
379         return (unsigned int)inl(ALI_REG(codec, port)); 
380 }
381
382 static inline void snd_ali_5451_poke(   ali_t *codec,
383                                         unsigned int port,
384                                         unsigned int val )
385 {
386         outl((unsigned int)val, ALI_REG(codec, port));
387 }
388
389 static int snd_ali_codec_ready( ali_t *codec,
390                                 unsigned int port,
391                                 int sched )
392 {
393         unsigned long end_time;
394         unsigned int res;
395         
396         end_time = jiffies + 10 * (HZ >> 2);
397         do {
398                 res = snd_ali_5451_peek(codec,port);
399                 if (! (res & 0x8000))
400                         return 0;
401                 if (sched) {
402                         set_current_state(TASK_UNINTERRUPTIBLE);
403                         schedule_timeout(1);
404                 }
405         } while (time_after_eq(end_time, jiffies));
406         snd_ali_5451_poke(codec, port, res & ~0x8000);
407         snd_printdd("ali_codec_ready: codec is not ready.\n ");
408         return -EIO;
409 }
410
411 static int snd_ali_stimer_ready(ali_t *codec, int sched)
412 {
413         unsigned long end_time;
414         unsigned long dwChk1,dwChk2;
415         
416         dwChk1 = snd_ali_5451_peek(codec, ALI_STIMER);
417         dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
418
419         end_time = jiffies + 10 * (HZ >> 2);
420         do {
421                 dwChk2 = snd_ali_5451_peek(codec, ALI_STIMER);
422                 if (dwChk2 != dwChk1)
423                         return 0;
424                 if (sched) {
425                         set_current_state(TASK_UNINTERRUPTIBLE);
426                         schedule_timeout(1);
427                 }
428         } while (time_after_eq(end_time, jiffies));
429         snd_printk("ali_stimer_read: stimer is not ready.\n");
430         return -EIO;
431 }
432
433 static void snd_ali_codec_poke(ali_t *codec,int secondary,
434                                      unsigned short reg,
435                                      unsigned short val)
436 {
437         unsigned int dwVal = 0;
438         unsigned int port = 0;
439
440         if (reg >= 0x80) {
441                 snd_printk("ali_codec_poke: reg(%xh) invalid.\n", reg);
442                 return;
443         }
444
445         port = codec->chregs.regs.ac97write;
446
447         if (snd_ali_codec_ready(codec, port, 0) < 0)
448                 return;
449         if (snd_ali_stimer_ready(codec, 0) < 0)
450                 return;
451
452         dwVal  = (unsigned int) (reg & 0xff);
453         dwVal |= 0x8000 | (val << 16);
454         if (secondary) dwVal |= 0x0080;
455         if (codec->revision == ALI_5451_V02) dwVal |= 0x0100;
456
457         snd_ali_5451_poke(codec,port,dwVal);
458
459         return ;
460 }
461
462 static unsigned short snd_ali_codec_peek( ali_t *codec,
463                                           int secondary,
464                                           unsigned short reg)
465 {
466         unsigned int dwVal = 0;
467         unsigned int port = 0;
468
469         if (reg >= 0x80) {
470                 snd_printk("ali_codec_peek: reg(%xh) invalid.\n", reg);
471                 return ~0;
472         }
473
474         port = codec->chregs.regs.ac97read;
475
476         if (snd_ali_codec_ready(codec, port, 0) < 0)
477                 return ~0;
478         if (snd_ali_stimer_ready(codec, 0) < 0)
479                 return ~0;
480
481         dwVal  = (unsigned int) (reg & 0xff);
482         dwVal |= 0x8000;                                /* bit 15*/
483         if (secondary) dwVal |= 0x0080;
484
485         snd_ali_5451_poke(codec, port, dwVal);
486
487         if (snd_ali_stimer_ready(codec, 0) < 0)
488                 return ~0;
489         if (snd_ali_codec_ready(codec, port, 0) < 0)
490                 return ~0;
491         
492         return (snd_ali_5451_peek(codec, port) & 0xffff0000)>>16;
493 }
494
495 static void snd_ali_codec_write(ac97_t *ac97,
496                                 unsigned short reg,
497                                 unsigned short val )
498 {
499         ali_t *codec = snd_magic_cast(ali_t, ac97->private_data, return);
500
501         snd_ali_printk("codec_write: reg=%xh data=%xh.\n", reg, val);
502         snd_ali_codec_poke(codec, 0, reg, val);
503         return ;
504 }
505
506
507 static unsigned short snd_ali_codec_read(ac97_t *ac97, unsigned short reg)
508 {
509         ali_t *codec = snd_magic_cast(ali_t, ac97->private_data, return -ENXIO);
510
511         snd_ali_printk("codec_read reg=%xh.\n", reg);
512         return (snd_ali_codec_peek(codec, 0, reg));
513 }
514
515 /*
516  *      AC97 Reset
517  */
518
519 static int snd_ali_reset_5451(ali_t *codec)
520 {
521         struct pci_dev *pci_dev = NULL;
522         unsigned short wCount, wReg;
523         unsigned int   dwVal;
524         
525         if ((pci_dev = codec->pci_m1533) != NULL) {
526                 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
527                 pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
528                 udelay(5000);
529                 pci_read_config_dword(pci_dev, 0x7c, &dwVal);
530                 pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
531                 udelay(5000);
532         }
533         
534         pci_dev = codec->pci;
535         pci_read_config_dword(pci_dev, 0x44, &dwVal);
536         pci_write_config_dword(pci_dev, 0x44, dwVal | 0x000c0000);
537         udelay(500);
538         pci_read_config_dword(pci_dev, 0x44, &dwVal);
539         pci_write_config_dword(pci_dev, 0x44, dwVal & 0xfffbffff);
540         udelay(5000);
541         
542         wCount = 200;
543         while(wCount--) {
544                 wReg = snd_ali_codec_peek(codec, 0, AC97_POWERDOWN);
545                 if((wReg & 0x000f) == 0x000f)
546                         return 0;
547                 udelay(5000);
548         }
549
550         /* non-fatal if you have a non PM capable codec */
551         /* snd_printk(KERN_WARNING "ali5451: reset time out\n"); */
552         return 0;
553 }
554
555 #ifdef CODEC_RESET
556
557 static int snd_ali_reset_codec(ali_t *codec)
558 {
559         struct pci_dev *pci_dev = NULL;
560         unsigned char bVal = 0;
561         unsigned int   dwVal;
562         unsigned short wCount, wReg;
563
564         pci_dev = codec->pci_m1533;
565         
566         pci_read_config_dword(pci_dev, 0x7c, &dwVal);
567         pci_write_config_dword(pci_dev, 0x7c, dwVal | 0x08000000);
568         udelay(5000);
569         pci_read_config_dword(pci_dev, 0x7c, &dwVal);
570         pci_write_config_dword(pci_dev, 0x7c, dwVal & 0xf7ffffff);
571         udelay(5000);
572
573         bVal = inb(ALI_REG(codec,ALI_SCTRL));
574         bVal |= 0x02;
575         outb(ALI_REG(codec,ALI_SCTRL),bVal);
576         udelay(5000);
577         bVal = inb(ALI_REG(codec,ALI_SCTRL));
578         bVal &= 0xfd;
579         outb(ALI_REG(codec,ALI_SCTRL),bVal);
580         udelay(15000);
581
582         wCount = 200;
583         while(wCount--) {
584                 wReg = snd_ali_codec_read(codec->ac97, AC97_POWERDOWN);
585                 if((wReg & 0x000f) == 0x000f)
586                         return 0;
587                 udelay(5000);
588         }
589         return -1;
590 }
591
592 #endif
593
594 /*
595  *  ALI 5451 Controller
596  */
597
598 static void snd_ali_enable_special_channel(ali_t *codec, unsigned int channel)
599 {
600         unsigned long dwVal = 0;
601
602         dwVal  = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
603         dwVal |= 1 << (channel & 0x0000001f);
604         outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
605 }
606
607 static void snd_ali_disable_special_channel(ali_t *codec, unsigned int channel)
608 {
609         unsigned long dwVal = 0;
610
611         dwVal  = inl(ALI_REG(codec,ALI_GLOBAL_CONTROL));
612         dwVal &= ~(1 << (channel & 0x0000001f));
613         outl(dwVal, ALI_REG(codec,ALI_GLOBAL_CONTROL));
614 }
615
616 static void snd_ali_enable_address_interrupt(ali_t * codec)
617 {
618         unsigned int gc;
619
620         gc  = inl(ALI_REG(codec, ALI_GC_CIR));
621         gc |= ENDLP_IE;
622         gc |= MIDLP_IE;
623         outl( gc, ALI_REG(codec, ALI_GC_CIR));
624 }
625
626 static void snd_ali_disable_address_interrupt(ali_t * codec)
627 {
628         unsigned int gc;
629
630         gc  = inl(ALI_REG(codec, ALI_GC_CIR));
631         gc &= ~ENDLP_IE;
632         gc &= ~MIDLP_IE;
633         outl(gc, ALI_REG(codec, ALI_GC_CIR));
634 }
635
636 #if 0 // not used
637 static void snd_ali_enable_voice_irq(ali_t *codec, unsigned int channel)
638 {
639         unsigned int mask;
640         snd_ali_channel_control_t *pchregs = &(codec->chregs);
641
642         snd_ali_printk("enable_voice_irq channel=%d\n",channel);
643         
644         mask = 1 << (channel & 0x1f);
645         pchregs->data.ainten  = inl(ALI_REG(codec,pchregs->regs.ainten));
646         pchregs->data.ainten |= mask;
647         outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
648 }
649 #endif
650
651 static void snd_ali_disable_voice_irq(ali_t *codec, unsigned int channel)
652 {
653         unsigned int mask;
654         snd_ali_channel_control_t *pchregs = &(codec->chregs);
655
656         snd_ali_printk("disable_voice_irq channel=%d\n",channel);
657
658         mask = 1 << (channel & 0x1f);
659         pchregs->data.ainten  = inl(ALI_REG(codec,pchregs->regs.ainten));
660         pchregs->data.ainten &= ~mask;
661         outl(pchregs->data.ainten,ALI_REG(codec,pchregs->regs.ainten));
662 }
663
664 static int snd_ali_alloc_pcm_channel(ali_t *codec, int channel)
665 {
666         unsigned int idx =  channel & 0x1f;
667
668         if (codec->synth.chcnt >= ALI_CHANNELS){
669                 snd_printk("ali_alloc_pcm_channel: no free channels.\n");
670                 return -1;
671         }
672
673         if (!(codec->synth.chmap & (1 << idx))) {
674                 codec->synth.chmap |= 1 << idx;
675                 codec->synth.chcnt++;
676                 snd_ali_printk("alloc_pcm_channel no. %d.\n",idx);
677                 return idx;
678         }
679         return -1;
680 }
681
682 static int snd_ali_find_free_channel(ali_t * codec, int rec)
683 {
684         int idx;
685         int result = -1;
686
687         snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm");
688
689         // recording
690         if (rec) {
691                 if (codec->spdif_support &&
692                     (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT))
693                         idx = ALI_SPDIF_IN_CHANNEL;
694                 else
695                         idx = ALI_PCM_IN_CHANNEL;
696
697                 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
698                         return result;
699                 } else {
700                         snd_printk("ali_find_free_channel: record channel is busy now.\n");
701                         return -1;
702                 }
703         }
704
705         //playback...
706         if (codec->spdif_support &&
707             (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)) {
708                 idx = ALI_SPDIF_OUT_CHANNEL;
709                 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0) {
710                         return result;
711                 } else {
712                         snd_printk("ali_find_free_channel: S/PDIF out channel is in busy now.\n");
713                 }
714         }
715
716         for (idx = 0; idx < ALI_CHANNELS; idx++) {
717                 if ((result = snd_ali_alloc_pcm_channel(codec,idx)) >= 0)
718                         return result;
719         }
720         snd_printk("ali_find_free_channel: no free channels.\n");
721         return -1;
722 }
723
724 static void snd_ali_free_channel_pcm(ali_t *codec, int channel)
725 {
726         unsigned int idx = channel & 0x0000001f;
727
728         snd_ali_printk("free_channel_pcm channel=%d\n",channel);
729
730         if (channel < 0 || channel >= ALI_CHANNELS)
731                 return;
732
733         if (!(codec->synth.chmap & (1 << idx))) {
734                 snd_printk("ali_free_channel_pcm: channel %d is not in use.\n",channel);
735                 return;
736         } else {
737                 codec->synth.chmap &= ~(1 << idx);
738                 codec->synth.chcnt--;
739         }
740 }
741
742 #if 0 // not used
743 static void snd_ali_start_voice(ali_t * codec, unsigned int channel)
744 {
745         unsigned int mask = 1 << (channel & 0x1f);
746         
747         snd_ali_printk("start_voice: channel=%d\n",channel);
748         outl(mask, ALI_REG(codec,codec->chregs.regs.start));
749 }
750 #endif
751
752 static void snd_ali_stop_voice(ali_t * codec, unsigned int channel)
753 {
754         unsigned int mask = 1 << (channel & 0x1f);
755
756         snd_ali_printk("stop_voice: channel=%d\n",channel);
757         outl(mask, ALI_REG(codec, codec->chregs.regs.stop));
758 }
759
760 /*
761  *    S/PDIF Part
762  */
763
764 static void snd_ali_delay(ali_t *codec,int interval)
765 {
766         unsigned long  begintimer,currenttimer;
767
768         begintimer   = inl(ALI_REG(codec, ALI_STIMER));
769         currenttimer = inl(ALI_REG(codec, ALI_STIMER));
770
771         while (currenttimer < begintimer + interval) {
772                 if(snd_ali_stimer_ready(codec, 1) < 0)
773                         break;
774                 currenttimer = inl(ALI_REG(codec,  ALI_STIMER));
775         }
776 }
777
778 static void snd_ali_detect_spdif_rate(ali_t *codec)
779 {
780         u16 wval  = 0;
781         u16 count = 0;
782         u8  bval = 0, R1 = 0, R2 = 0;
783
784         bval  = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
785         bval |= 0x1F;
786         outb(bval,ALI_REG(codec,ALI_SPDIF_CTRL + 1));
787
788         while (((R1 < 0x0B )||(R1 > 0x0E)) && (R1 != 0x12) && count <= 50000) {
789                 count ++;
790                 snd_ali_delay(codec, 6);
791                 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
792                 R1 = bval & 0x1F;
793         }
794
795         if (count > 50000) {
796                 snd_printk("ali_detect_spdif_rate: timeout!\n");
797                 return;
798         }
799
800         count = 0;
801         while (count++ <= 50000) {
802                 snd_ali_delay(codec, 6);
803                 bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1));
804                 R2 = bval & 0x1F;
805                 if (R2 != R1) R1 = R2; else break;
806         }
807
808         if (count > 50000) {
809                 snd_printk("ali_detect_spdif_rate: timeout!\n");
810                 return;
811         }
812
813         if (R2 >= 0x0b && R2 <= 0x0e) {
814                 wval  = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
815                 wval &= 0xE0F0;
816                 wval |= (u16)0x09 << 8 | (u16)0x05;
817                 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
818
819                 bval  = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
820                 outb(bval|0x02,ALI_REG(codec,ALI_SPDIF_CS + 3));
821         } else if (R2 == 0x12) {
822                 wval  = inw(ALI_REG(codec,ALI_SPDIF_CTRL + 2));
823                 wval &= 0xE0F0;
824                 wval |= (u16)0x0E << 8 | (u16)0x08;
825                 outw(wval,ALI_REG(codec,ALI_SPDIF_CTRL + 2));
826
827                 bval  = inb(ALI_REG(codec,ALI_SPDIF_CS +3)) & 0xF0;
828                 outb(bval|0x03,ALI_REG(codec,ALI_SPDIF_CS + 3));
829         }
830 }
831
832 static unsigned int snd_ali_get_spdif_in_rate(ali_t *codec)
833 {
834         u32     dwRate = 0;
835         u8      bval = 0;
836
837         bval  = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
838         bval &= 0x7F;
839         bval |= 0x40;
840         outb(bval, ALI_REG(codec,ALI_SPDIF_CTRL));
841
842         snd_ali_detect_spdif_rate(codec);
843
844         bval  = inb(ALI_REG(codec,ALI_SPDIF_CS + 3));
845         bval &= 0x0F;
846
847         if (bval == 0) dwRate = 44100;
848         if (bval == 1) dwRate = 48000;
849         if (bval == 2) dwRate = 32000;
850
851         return dwRate;
852 }
853
854 static void snd_ali_enable_spdif_in(ali_t *codec)
855 {       
856         unsigned int dwVal;
857
858         dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
859         dwVal |= ALI_SPDIF_IN_SUPPORT;
860         outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
861
862         dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
863         dwVal |= 0x02;
864         outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL));
865
866         snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);
867 }
868
869 static void snd_ali_disable_spdif_in(ali_t *codec)
870 {
871         unsigned int dwVal;
872         
873         dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL));
874         dwVal &= ~ALI_SPDIF_IN_SUPPORT;
875         outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
876         
877         snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);   
878 }
879
880
881 static void snd_ali_set_spdif_out_rate(ali_t *codec, unsigned int rate)
882 {
883         unsigned char  bVal;
884         unsigned int  dwRate = 0;
885         
886         if (rate == 32000) dwRate = 0x300;
887         if (rate == 44100) dwRate = 0;
888         if (rate == 48000) dwRate = 0x200;
889         
890         bVal  = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
891         bVal &= (unsigned char)(~(1<<6));
892         
893         bVal |= 0x80;           //select right
894         outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
895         outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2));
896         
897         bVal &= (~0x80);        //select left
898         outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL));
899         outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));
900 }
901
902 static void snd_ali_enable_spdif_out(ali_t *codec)
903 {
904         unsigned short wVal;
905         unsigned char bVal;
906
907         struct pci_dev *pci_dev = NULL;
908
909         pci_dev = codec->pci_m1533;
910         if (pci_dev == NULL)
911                 return;
912         pci_read_config_byte(pci_dev, 0x61, &bVal);
913         bVal |= 0x40;
914         pci_write_config_byte(pci_dev, 0x61, bVal);
915         pci_read_config_byte(pci_dev, 0x7d, &bVal);
916         bVal |= 0x01;
917         pci_write_config_byte(pci_dev, 0x7d, bVal);
918
919         pci_read_config_byte(pci_dev, 0x7e, &bVal);
920         bVal &= (~0x20);
921         bVal |= 0x10;
922         pci_write_config_byte(pci_dev, 0x7e, bVal);
923
924         bVal = inb(ALI_REG(codec, ALI_SCTRL));
925         outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
926
927         bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL));
928         outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL));
929    
930         {
931                 wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
932                 wVal |= ALI_SPDIF_OUT_SEL_PCM;
933                 outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
934                 snd_ali_disable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
935         }
936 }
937
938 static void snd_ali_enable_spdif_chnout(ali_t *codec)
939 {
940         unsigned short wVal = 0;
941
942         wVal  = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
943         wVal &= ~ALI_SPDIF_OUT_SEL_PCM;
944         outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
945 /*
946         wVal = inw(ALI_REG(codec, ALI_SPDIF_CS));
947         if (flag & ALI_SPDIF_OUT_NON_PCM)
948                 wVal |= 0x0002;
949         else    
950                 wVal &= (~0x0002);
951         outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));
952 */
953         snd_ali_enable_special_channel(codec,ALI_SPDIF_OUT_CHANNEL);
954 }
955
956 static void snd_ali_disable_spdif_chnout(ali_t *codec)
957 {
958         unsigned short wVal = 0;
959         wVal  = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL));
960         wVal |= ALI_SPDIF_OUT_SEL_PCM;
961         outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));
962
963         snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);
964 }
965
966 static void snd_ali_disable_spdif_out(ali_t *codec)
967 {
968         unsigned char  bVal;
969
970         bVal = inb(ALI_REG(codec, ALI_SCTRL));
971         outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL));
972
973         snd_ali_disable_spdif_chnout(codec);
974 }
975
976 static void snd_ali_update_ptr(ali_t *codec,int channel)
977 {
978         snd_ali_voice_t *pvoice = NULL;
979         snd_pcm_runtime_t *runtime;
980         snd_ali_channel_control_t *pchregs = NULL;
981         unsigned int old, mask;
982 #ifdef ALI_DEBUG
983         unsigned int temp, cspf;
984 #endif
985
986         pchregs = &(codec->chregs);
987
988         // check if interrupt occurred for channel
989         old  = pchregs->data.aint;
990         mask = ((unsigned int) 1L) << (channel & 0x1f);
991
992         if (!(old & mask))
993                 return;
994
995         pvoice = &codec->synth.voices[channel];
996         runtime = pvoice->substream->runtime;
997
998         udelay(100);
999         spin_lock(&codec->reg_lock);
1000
1001         if (pvoice->pcm && pvoice->substream) {
1002                 /* pcm interrupt */
1003 #ifdef ALI_DEBUG
1004                 outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR));
1005                 temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1006                 cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;
1007 #endif
1008                 if (pvoice->running) {
1009                         snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n",(u16)temp,cspf);
1010                         spin_unlock(&codec->reg_lock);
1011                         snd_pcm_period_elapsed(pvoice->substream);
1012                         spin_lock(&codec->reg_lock);
1013                 } else {
1014                         snd_ali_stop_voice(codec, channel);
1015                         snd_ali_disable_voice_irq(codec, channel);
1016                 }       
1017         } else if (codec->synth.voices[channel].synth) {
1018                 /* synth interrupt */
1019         } else if (codec->synth.voices[channel].midi) {
1020                 /* midi interrupt */
1021         } else {
1022                 /* unknown interrupt */
1023                 snd_ali_stop_voice(codec, channel);
1024                 snd_ali_disable_voice_irq(codec, channel);
1025         }
1026         spin_unlock(&codec->reg_lock);
1027         outl(mask,ALI_REG(codec,pchregs->regs.aint));
1028         pchregs->data.aint = old & (~mask);
1029 }
1030
1031 static void snd_ali_interrupt(ali_t * codec)
1032 {
1033         int channel;
1034         unsigned int audio_int;
1035         snd_ali_channel_control_t *pchregs = NULL;
1036         pchregs = &(codec->chregs);
1037
1038         audio_int = inl(ALI_REG(codec, ALI_MISCINT));
1039         if (audio_int & ADDRESS_IRQ) {
1040                 // get interrupt status for all channels
1041                 pchregs->data.aint = inl(ALI_REG(codec,pchregs->regs.aint));
1042                 for (channel = 0; channel < ALI_CHANNELS; channel++) {
1043                         snd_ali_update_ptr(codec, channel);
1044                 }
1045         }
1046         outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW),
1047                 ALI_REG(codec,ALI_MISCINT));
1048 }
1049
1050
1051 static irqreturn_t snd_ali_card_interrupt(int irq,
1052                                    void *dev_id,
1053                                    struct pt_regs *regs)
1054 {
1055         ali_t   *codec = snd_magic_cast(ali_t, dev_id, return IRQ_NONE);
1056
1057         if (codec == NULL)
1058                 return IRQ_NONE;
1059         snd_ali_interrupt(codec);
1060         return IRQ_HANDLED;
1061 }
1062
1063
1064 static snd_ali_voice_t *snd_ali_alloc_voice(ali_t * codec, int type, int rec)
1065 {
1066         snd_ali_voice_t *pvoice = NULL;
1067         unsigned long flags;
1068         int idx;
1069
1070         snd_ali_printk("alloc_voice: type=%d rec=%d\n",type,rec);
1071
1072         spin_lock_irqsave(&codec->voice_alloc, flags);
1073         if (type == SNDRV_ALI_VOICE_TYPE_PCM) {
1074                 idx = snd_ali_find_free_channel(codec,rec);
1075                 if(idx < 0) {
1076                         snd_printk("ali_alloc_voice: err.\n");
1077                         spin_unlock_irqrestore(&codec->voice_alloc, flags);
1078                         return NULL;
1079                 }
1080                 pvoice = &(codec->synth.voices[idx]);
1081                 pvoice->use = 1;
1082                 pvoice->pcm = 1;
1083                 pvoice->mode = rec;
1084                 spin_unlock_irqrestore(&codec->voice_alloc, flags);
1085                 return pvoice;
1086         }
1087         spin_unlock_irqrestore(&codec->voice_alloc, flags);
1088         return NULL;
1089 }
1090
1091
1092 static void snd_ali_free_voice(ali_t * codec, snd_ali_voice_t *pvoice)
1093 {
1094         unsigned long flags;
1095         void (*private_free)(void *);
1096         void *private_data;
1097
1098         snd_ali_printk("free_voice: channel=%d\n",pvoice->number);
1099         if (pvoice == NULL || !pvoice->use)
1100                 return;
1101         snd_ali_clear_voices(codec, pvoice->number, pvoice->number);
1102         spin_lock_irqsave(&codec->voice_alloc, flags);
1103         private_free = pvoice->private_free;
1104         private_data = pvoice->private_data;
1105         pvoice->private_free = NULL;
1106         pvoice->private_data = NULL;
1107         if (pvoice->pcm) {
1108                 snd_ali_free_channel_pcm(codec, pvoice->number);
1109         }
1110         pvoice->use = pvoice->pcm = pvoice->synth = 0;
1111         pvoice->substream = NULL;
1112         spin_unlock_irqrestore(&codec->voice_alloc, flags);
1113         if (private_free)
1114                 private_free(private_data);
1115 }
1116
1117
1118 static void snd_ali_clear_voices(ali_t * codec,
1119                           unsigned int v_min,
1120                           unsigned int v_max)
1121 {
1122         unsigned int i;
1123
1124         for (i = v_min; i <= v_max; i++) {
1125                 snd_ali_stop_voice(codec, i);
1126                 snd_ali_disable_voice_irq(codec, i);
1127         }
1128 }
1129
1130 static void snd_ali_write_voice_regs(ali_t * codec,
1131                          unsigned int Channel,
1132                          unsigned int LBA,
1133                          unsigned int CSO,
1134                          unsigned int ESO,
1135                          unsigned int DELTA,
1136                          unsigned int ALPHA_FMS,
1137                          unsigned int GVSEL,
1138                          unsigned int PAN,
1139                          unsigned int VOL,
1140                          unsigned int CTRL,
1141                          unsigned int EC)
1142 {
1143         unsigned int ctlcmds[4];
1144         
1145         outb((unsigned char)(Channel & 0x001f),ALI_REG(codec,ALI_GC_CIR));
1146
1147         ctlcmds[0] =  (CSO << 16) | (ALPHA_FMS & 0x0000ffff);
1148         ctlcmds[1] =  LBA;
1149         ctlcmds[2] =  (ESO << 16) | (DELTA & 0x0ffff);
1150         ctlcmds[3] =  (GVSEL << 31) |
1151                       ((PAN & 0x0000007f) << 24) |
1152                       ((VOL & 0x000000ff) << 16) |
1153                       ((CTRL & 0x0000000f) << 12) |
1154                       (EC & 0x00000fff);
1155
1156         outb(Channel, ALI_REG(codec, ALI_GC_CIR));
1157
1158         outl(ctlcmds[0], ALI_REG(codec,ALI_CSO_ALPHA_FMS));
1159         outl(ctlcmds[1], ALI_REG(codec,ALI_LBA));
1160         outl(ctlcmds[2], ALI_REG(codec,ALI_ESO_DELTA));
1161         outl(ctlcmds[3], ALI_REG(codec,ALI_GVSEL_PAN_VOC_CTRL_EC));
1162
1163         outl(0x30000000, ALI_REG(codec, ALI_EBUF1));    /* Still Mode */
1164         outl(0x30000000, ALI_REG(codec, ALI_EBUF2));    /* Still Mode */
1165 }
1166
1167 static unsigned int snd_ali_convert_rate(unsigned int rate, int rec)
1168 {
1169         unsigned int delta;
1170
1171         if (rate < 4000)  rate = 4000;
1172         if (rate > 48000) rate = 48000;
1173
1174         if (rec) {
1175                 if (rate == 44100)
1176                         delta = 0x116a;
1177                 else if (rate == 8000)
1178                         delta = 0x6000;
1179                 else if (rate == 48000)
1180                         delta = 0x1000;
1181                 else
1182                         delta = ((48000 << 12) / rate) & 0x0000ffff;
1183         } else {
1184                 if (rate == 44100)
1185                         delta = 0xeb3;
1186                 else if (rate == 8000)
1187                         delta = 0x2ab;
1188                 else if (rate == 48000)
1189                         delta = 0x1000;
1190                 else 
1191                         delta = (((rate << 12) + rate) / 48000) & 0x0000ffff;
1192         }
1193
1194         return delta;
1195 }
1196
1197 static unsigned int snd_ali_control_mode(snd_pcm_substream_t *substream)
1198 {
1199         unsigned int CTRL;
1200         snd_pcm_runtime_t *runtime = substream->runtime;
1201
1202         /* set ctrl mode
1203            CTRL default: 8-bit (unsigned) mono, loop mode enabled
1204          */
1205         CTRL = 0x00000001;
1206         if (snd_pcm_format_width(runtime->format) == 16)
1207                 CTRL |= 0x00000008;     // 16-bit data
1208         if (!snd_pcm_format_unsigned(runtime->format))
1209                 CTRL |= 0x00000002;     // signed data
1210         if (runtime->channels > 1)
1211                 CTRL |= 0x00000004;     // stereo data
1212         return CTRL;
1213 }
1214
1215 /*
1216  *  PCM part
1217  */
1218
1219 static int snd_ali_ioctl(snd_pcm_substream_t * substream,
1220                                   unsigned int cmd, void *arg)
1221 {
1222         return snd_pcm_lib_ioctl(substream, cmd, arg);
1223 }
1224
1225 static int snd_ali_trigger(snd_pcm_substream_t *substream,
1226                                int cmd)
1227                                     
1228 {
1229         ali_t *codec = snd_pcm_substream_chip(substream);
1230         struct list_head *pos;
1231         snd_pcm_substream_t *s;
1232         unsigned int what, whati, capture_flag;
1233         snd_ali_voice_t *pvoice = NULL, *evoice = NULL;
1234         unsigned int val;
1235         int do_start;
1236
1237         switch (cmd) {
1238         case SNDRV_PCM_TRIGGER_START:
1239         case SNDRV_PCM_TRIGGER_RESUME:
1240                 do_start = 1; break;
1241         case SNDRV_PCM_TRIGGER_STOP:
1242         case SNDRV_PCM_TRIGGER_SUSPEND:
1243                 do_start = 0; break;
1244         default:
1245                 return -EINVAL;
1246         }
1247
1248         what = whati = capture_flag = 0;
1249         snd_pcm_group_for_each(pos, substream) {
1250                 s = snd_pcm_group_substream_entry(pos);
1251                 if ((ali_t *) _snd_pcm_chip(s->pcm) == codec) {
1252                         pvoice = (snd_ali_voice_t *) s->runtime->private_data;
1253                         evoice = pvoice->extra;
1254                         what |= 1 << (pvoice->number & 0x1f);
1255                         if (evoice == NULL) {
1256                                 whati |= 1 << (pvoice->number & 0x1f);
1257                         } else {
1258                                 whati |= 1 << (evoice->number & 0x1f);
1259                                 what |= 1 << (evoice->number & 0x1f);
1260                         }
1261                         if (do_start) {
1262                                 pvoice->running = 1;
1263                                 if (evoice != NULL)
1264                                         evoice->running = 1;
1265                         } else {
1266                                 pvoice->running = 0;
1267                                 if (evoice != NULL)
1268                                         evoice->running = 0;
1269                         }
1270                         snd_pcm_trigger_done(s, substream);
1271                         if (pvoice->mode)
1272                                 capture_flag = 1;
1273                 }
1274         }
1275         spin_lock(&codec->reg_lock);
1276         if (! do_start) {
1277                 outl(what, ALI_REG(codec, ALI_STOP));
1278         }
1279         val = inl(ALI_REG(codec, ALI_AINTEN));
1280         if (do_start) {
1281                 val |= whati;
1282         } else {
1283                 val &= ~whati;
1284         }
1285         outl(val, ALI_REG(codec, ALI_AINTEN));
1286         if (do_start) {
1287                 outl(what, ALI_REG(codec, ALI_START));
1288         }
1289         snd_ali_printk("trigger: what=%xh whati=%xh\n",what,whati);
1290         spin_unlock(&codec->reg_lock);
1291
1292         return 0;
1293 }
1294
1295 static int snd_ali_playback_hw_params(snd_pcm_substream_t * substream,
1296                                  snd_pcm_hw_params_t * hw_params)
1297 {
1298         ali_t *codec = snd_pcm_substream_chip(substream);
1299         snd_pcm_runtime_t *runtime = substream->runtime;
1300         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1301         snd_ali_voice_t *evoice = pvoice->extra;
1302         int err;
1303         err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1304         if (err < 0) return err;
1305         
1306         /* voice management */
1307
1308         if (params_buffer_size(hw_params)/2 != params_period_size(hw_params)) {
1309                 if (evoice == NULL) {
1310                         evoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0);
1311                         if (evoice == NULL)
1312                                 return -ENOMEM;
1313                         pvoice->extra = evoice;
1314                         evoice->substream = substream;
1315                 }
1316         } else {
1317                 if (evoice != NULL) {
1318                         snd_ali_free_voice(codec, evoice);
1319                         pvoice->extra = evoice = NULL;
1320                 }
1321         }
1322
1323         return 0;
1324 }
1325
1326 static int snd_ali_playback_hw_free(snd_pcm_substream_t * substream)
1327 {
1328         ali_t *codec = snd_pcm_substream_chip(substream);
1329         snd_pcm_runtime_t *runtime = substream->runtime;
1330         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1331         snd_ali_voice_t *evoice = pvoice ? pvoice->extra : NULL;
1332
1333         snd_pcm_lib_free_pages(substream);
1334         if (evoice != NULL) {
1335                 snd_ali_free_voice(codec, evoice);
1336                 pvoice->extra = NULL;
1337         }
1338         return 0;
1339 }
1340
1341 static int snd_ali_capture_hw_params(snd_pcm_substream_t * substream,
1342                                  snd_pcm_hw_params_t * hw_params)
1343 {
1344         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1345 }
1346
1347 static int snd_ali_capture_hw_free(snd_pcm_substream_t * substream)
1348 {
1349         return snd_pcm_lib_free_pages(substream);
1350 }
1351
1352 static int snd_ali_playback_prepare(snd_pcm_substream_t * substream)
1353 {
1354         ali_t *codec = snd_pcm_substream_chip(substream);
1355         snd_pcm_runtime_t *runtime = substream->runtime;
1356         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1357         snd_ali_voice_t *evoice = pvoice->extra;
1358         unsigned long flags;
1359
1360         unsigned int LBA;
1361         unsigned int Delta;
1362         unsigned int ESO;
1363         unsigned int CTRL;
1364         unsigned int GVSEL;
1365         unsigned int PAN;
1366         unsigned int VOL;
1367         unsigned int EC;
1368         
1369         snd_ali_printk("playback_prepare ...\n");
1370
1371         spin_lock_irqsave(&codec->reg_lock, flags);     
1372         
1373         /* set Delta (rate) value */
1374         Delta = snd_ali_convert_rate(runtime->rate, 0);
1375
1376         if ((pvoice->number == ALI_SPDIF_IN_CHANNEL) || 
1377             (pvoice->number == ALI_PCM_IN_CHANNEL))
1378                 snd_ali_disable_special_channel(codec, pvoice->number);
1379         else if (codec->spdif_support &&
1380                  (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)
1381                  && (pvoice->number == ALI_SPDIF_OUT_CHANNEL)) {
1382                 snd_ali_set_spdif_out_rate(codec, runtime->rate);
1383                 Delta = 0x1000;
1384         }
1385         
1386         /* set Loop Back Address */
1387         LBA = runtime->dma_addr;
1388
1389         /* set interrupt count size */
1390         pvoice->count = runtime->period_size;
1391
1392         /* set target ESO for channel */
1393         pvoice->eso = runtime->buffer_size; 
1394
1395         snd_ali_printk("playback_prepare: eso=%xh count=%xh\n",pvoice->eso,pvoice->count);
1396
1397         /* set ESO to capture first MIDLP interrupt */
1398         ESO = pvoice->eso -1;
1399         /* set ctrl mode */
1400         CTRL = snd_ali_control_mode(substream);
1401
1402         GVSEL = 1;
1403         PAN = 0;
1404         VOL = 0;
1405         EC = 0;
1406         snd_ali_printk("playback_prepare:\n    ch=%d, Rate=%d Delta=%xh,GVSEL=%xh,PAN=%xh,CTRL=%xh\n",pvoice->number,runtime->rate,Delta,GVSEL,PAN,CTRL);
1407         snd_ali_write_voice_regs(    codec,
1408                                      pvoice->number,
1409                                      LBA,
1410                                      0, /* cso */
1411                                      ESO,
1412                                      Delta,
1413                                      0, /* alpha */
1414                                      GVSEL,
1415                                      PAN,
1416                                      VOL,
1417                                      CTRL,
1418                                      EC);
1419         if (evoice != NULL) {
1420                 evoice->count = pvoice->count;
1421                 evoice->eso = pvoice->count << 1;
1422                 ESO = evoice->eso - 1;
1423                 snd_ali_write_voice_regs(codec,
1424                                      evoice->number,
1425                                      LBA,
1426                                      0, /* cso */
1427                                      ESO,
1428                                      Delta,
1429                                      0, /* alpha */
1430                                      GVSEL,
1431                                      (unsigned int)0x7f,
1432                                      (unsigned int)0x3ff,
1433                                      CTRL,
1434                                      EC);
1435         }
1436         spin_unlock_irqrestore(&codec->reg_lock, flags);
1437         return 0;
1438 }
1439
1440
1441 static int snd_ali_capture_prepare(snd_pcm_substream_t * substream)
1442 {
1443         ali_t *codec = snd_pcm_substream_chip(substream);
1444         snd_pcm_runtime_t *runtime = substream->runtime;
1445         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1446         unsigned long flags;
1447         unsigned int LBA;
1448         unsigned int Delta;
1449         unsigned int ESO;
1450         unsigned int CTRL;
1451         unsigned int GVSEL;
1452         unsigned int PAN;
1453         unsigned int VOL;
1454         unsigned int EC;
1455         u8       bValue;
1456
1457         spin_lock_irqsave(&codec->reg_lock, flags);
1458
1459         snd_ali_printk("capture_prepare...\n");
1460
1461         snd_ali_enable_special_channel(codec,pvoice->number);
1462
1463         Delta = snd_ali_convert_rate(runtime->rate, 1);
1464
1465         // Prepare capture intr channel
1466         if (pvoice->number == ALI_SPDIF_IN_CHANNEL) {
1467
1468                 unsigned int rate;
1469                 
1470                 if (codec->revision != ALI_5451_V02) {
1471                         spin_unlock_irqrestore(&codec->reg_lock, flags);                        
1472                         return -1;
1473                 }
1474                 rate = snd_ali_get_spdif_in_rate(codec);
1475                 if (rate == 0) {
1476                         snd_printk("ali_capture_preapre: spdif rate detect err!\n");
1477                         rate = 48000;
1478                 }
1479                 bValue = inb(ALI_REG(codec,ALI_SPDIF_CTRL));
1480                 if (bValue & 0x10) {
1481                         outb(bValue,ALI_REG(codec,ALI_SPDIF_CTRL));
1482                         printk("clear SPDIF parity error flag.\n");
1483                 }
1484
1485                 if (rate != 48000)
1486                         Delta = ((rate << 12)/runtime->rate)&0x00ffff;
1487         }
1488
1489         // set target ESO for channel 
1490         pvoice->eso = runtime->buffer_size; 
1491
1492         // set interrupt count size 
1493         pvoice->count = runtime->period_size;
1494
1495         // set Loop Back Address 
1496         LBA = runtime->dma_addr;
1497
1498         // set ESO to capture first MIDLP interrupt 
1499         ESO = pvoice->eso - 1;
1500         CTRL = snd_ali_control_mode(substream);
1501         GVSEL = 0;
1502         PAN = 0x00;
1503         VOL = 0x00;
1504         EC = 0;
1505
1506         snd_ali_write_voice_regs(    codec,
1507                                      pvoice->number,
1508                                      LBA,
1509                                      0, /* cso */
1510                                      ESO,
1511                                      Delta,
1512                                      0, /* alpha */
1513                                      GVSEL,
1514                                      PAN,
1515                                      VOL,
1516                                      CTRL,
1517                                      EC);
1518
1519
1520         spin_unlock_irqrestore(&codec->reg_lock, flags);
1521
1522         return 0;
1523 }
1524
1525
1526 static snd_pcm_uframes_t snd_ali_playback_pointer(snd_pcm_substream_t *substream)
1527 {
1528         ali_t *codec = snd_pcm_substream_chip(substream);
1529         snd_pcm_runtime_t *runtime = substream->runtime;
1530         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1531         unsigned int cso;
1532         unsigned long flags;
1533
1534         spin_lock_irqsave(&codec->reg_lock, flags);
1535         if (!pvoice->running) {
1536                 spin_unlock_irqrestore(&codec->reg_lock, flags);
1537                 return 0;
1538         }
1539         outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1540         cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1541         spin_unlock_irqrestore(&codec->reg_lock, flags);
1542         snd_ali_printk("playback pointer returned cso=%xh.\n", cso);
1543
1544         return cso;
1545 }
1546
1547
1548 static snd_pcm_uframes_t snd_ali_capture_pointer(snd_pcm_substream_t *substream)
1549 {
1550         ali_t *codec = snd_pcm_substream_chip(substream);
1551         snd_pcm_runtime_t *runtime = substream->runtime;
1552         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1553         unsigned int cso;
1554         unsigned long flags;
1555
1556         spin_lock_irqsave(&codec->reg_lock, flags);
1557         if (!pvoice->running) {
1558                 spin_unlock_irqrestore(&codec->reg_lock, flags);
1559                 return 0;
1560         }
1561         outb(pvoice->number, ALI_REG(codec, ALI_GC_CIR));
1562         cso = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2));
1563         spin_unlock_irqrestore(&codec->reg_lock, flags);
1564
1565         return cso;
1566 }
1567
1568 static snd_pcm_hardware_t snd_ali_playback =
1569 {
1570         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1571                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1572                                  SNDRV_PCM_INFO_MMAP_VALID |
1573                                  SNDRV_PCM_INFO_RESUME |
1574                                  SNDRV_PCM_INFO_SYNC_START),
1575         .formats =              (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1576                                  SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1577         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1578         .rate_min =             4000,
1579         .rate_max =             48000,
1580         .channels_min =         1,
1581         .channels_max =         2,
1582         .buffer_bytes_max =     (256*1024),
1583         .period_bytes_min =     64,
1584         .period_bytes_max =     (256*1024),
1585         .periods_min =          1,
1586         .periods_max =          1024,
1587         .fifo_size =            0,
1588 };
1589
1590 /*
1591  *  Capture support device description
1592  */
1593
1594 static snd_pcm_hardware_t snd_ali_capture =
1595 {
1596         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1597                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1598                                  SNDRV_PCM_INFO_MMAP_VALID |
1599                                  SNDRV_PCM_INFO_RESUME |
1600                                  SNDRV_PCM_INFO_SYNC_START),
1601         .formats =              (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
1602                                  SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
1603         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
1604         .rate_min =             4000,
1605         .rate_max =             48000,
1606         .channels_min =         1,
1607         .channels_max =         2,
1608         .buffer_bytes_max =     (128*1024),
1609         .period_bytes_min =     64,
1610         .period_bytes_max =     (128*1024),
1611         .periods_min =          1,
1612         .periods_max =          1024,
1613         .fifo_size =            0,
1614 };
1615
1616 static void snd_ali_pcm_free_substream(snd_pcm_runtime_t *runtime)
1617 {
1618         unsigned long flags;
1619         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1620         ali_t *codec;
1621
1622         if (pvoice) {
1623                 codec = pvoice->codec;
1624                 spin_lock_irqsave(&codec->reg_lock, flags);
1625                 snd_ali_free_voice(pvoice->codec, pvoice);
1626                 spin_unlock_irqrestore(&codec->reg_lock, flags);
1627         }
1628 }
1629
1630 static int snd_ali_playback_open(snd_pcm_substream_t * substream)
1631 {
1632         ali_t *codec = snd_pcm_substream_chip(substream);
1633         snd_pcm_runtime_t *runtime = substream->runtime;
1634         snd_ali_voice_t *pvoice;
1635         unsigned long flags = 0;
1636
1637         spin_lock_irqsave(&codec->reg_lock, flags);
1638         pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 0);
1639         if (pvoice == NULL) {
1640                 spin_unlock_irqrestore(&codec->reg_lock, flags);
1641                 return -EAGAIN;
1642         }
1643         pvoice->codec = codec;
1644         spin_unlock_irqrestore(&codec->reg_lock, flags);
1645
1646         pvoice->substream = substream;
1647         runtime->private_data = pvoice;
1648         runtime->private_free = snd_ali_pcm_free_substream;
1649
1650         runtime->hw = snd_ali_playback;
1651         snd_pcm_set_sync(substream);
1652         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1653         return 0;
1654 }
1655
1656
1657 static int snd_ali_capture_open(snd_pcm_substream_t * substream)
1658 {
1659         ali_t *codec = snd_pcm_substream_chip(substream);
1660         snd_pcm_runtime_t *runtime = substream->runtime;
1661         snd_ali_voice_t *pvoice;
1662         unsigned long flags;
1663
1664         spin_lock_irqsave(&codec->reg_lock, flags);
1665         pvoice = snd_ali_alloc_voice(codec, SNDRV_ALI_VOICE_TYPE_PCM, 1);
1666         if (pvoice == NULL) {
1667                 spin_unlock_irqrestore(&codec->reg_lock, flags);
1668                 return -EAGAIN;
1669         }
1670         pvoice->codec = codec;
1671         spin_unlock_irqrestore(&codec->reg_lock, flags);
1672
1673         pvoice->substream = substream;
1674         runtime->private_data = pvoice;
1675         runtime->private_free = snd_ali_pcm_free_substream;
1676         runtime->hw = snd_ali_capture;
1677         snd_pcm_set_sync(substream);
1678         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
1679         return 0;
1680 }
1681
1682
1683 static int snd_ali_playback_close(snd_pcm_substream_t * substream)
1684 {
1685         return 0;
1686 }
1687
1688 static int snd_ali_capture_close(snd_pcm_substream_t * substream)
1689 {
1690         ali_t *codec = snd_pcm_substream_chip(substream);
1691         snd_pcm_runtime_t *runtime = substream->runtime;
1692         snd_ali_voice_t *pvoice = (snd_ali_voice_t *) runtime->private_data;
1693
1694         snd_ali_disable_special_channel(codec,pvoice->number);
1695
1696         return 0;
1697 }
1698
1699 static snd_pcm_ops_t snd_ali_playback_ops = {
1700         .open =         snd_ali_playback_open,
1701         .close =        snd_ali_playback_close,
1702         .ioctl =        snd_ali_ioctl,
1703         .hw_params =    snd_ali_playback_hw_params,
1704         .hw_free =      snd_ali_playback_hw_free,
1705         .prepare =      snd_ali_playback_prepare,
1706         .trigger =      snd_ali_trigger,
1707         .pointer =      snd_ali_playback_pointer,
1708 };
1709
1710 static snd_pcm_ops_t snd_ali_capture_ops = {
1711         .open =         snd_ali_capture_open,
1712         .close =        snd_ali_capture_close,
1713         .ioctl =        snd_ali_ioctl,
1714         .hw_params =    snd_ali_capture_hw_params,
1715         .hw_free =      snd_ali_capture_hw_free,
1716         .prepare =      snd_ali_capture_prepare,
1717         .trigger =      snd_ali_trigger,
1718         .pointer =      snd_ali_capture_pointer,
1719 };
1720
1721
1722 static void snd_ali_pcm_free(snd_pcm_t *pcm)
1723 {
1724         ali_t *codec = snd_magic_cast(ali_t, pcm->private_data, return);
1725         codec->pcm = NULL;
1726 }
1727
1728 static int __devinit snd_ali_pcm(ali_t * codec, int device, snd_pcm_t ** rpcm)
1729 {
1730         snd_pcm_t *pcm;
1731         int err;
1732
1733         if (rpcm) *rpcm = NULL;
1734         err = snd_pcm_new(codec->card, "ALI 5451", device, ALI_CHANNELS, 1, &pcm);
1735         if (err < 0) {
1736                 snd_printk("snd_ali_pcm: err called snd_pcm_new.\n");
1737                 return err;
1738         }
1739         pcm->private_data = codec;
1740         pcm->private_free = snd_ali_pcm_free;
1741         pcm->info_flags = 0;
1742         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ali_playback_ops);
1743         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ali_capture_ops);
1744
1745         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1746                                               snd_dma_pci_data(codec->pci), 64*1024, 128*1024);
1747
1748         pcm->info_flags = 0;
1749         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1750         strcpy(pcm->name, "ALI 5451");
1751         codec->pcm = pcm;
1752         if (rpcm) *rpcm = pcm;
1753         return 0;
1754 }
1755
1756 #define ALI5451_SPDIF(xname, xindex, value) \
1757 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex,\
1758 .info = snd_ali5451_spdif_info, .get = snd_ali5451_spdif_get, \
1759 .put = snd_ali5451_spdif_put, .private_value = value}
1760
1761 static int snd_ali5451_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1762 {
1763         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1764         uinfo->count = 1;
1765         uinfo->value.integer.min = 0;
1766         uinfo->value.integer.max = 1;
1767         return 0;
1768 }
1769
1770 static int snd_ali5451_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1771 {
1772         unsigned long flags;
1773         ali_t *codec = snd_magic_cast(ali_t, kcontrol->private_data, -ENXIO);
1774         unsigned int enable;
1775
1776         enable = ucontrol->value.integer.value[0] ? 1 : 0;
1777
1778         spin_lock_irqsave(&codec->reg_lock, flags);
1779         switch(kcontrol->private_value) {
1780         case 0:
1781                 enable = (codec->spdif_mask & 0x02) ? 1 : 0;
1782                 break;
1783         case 1:
1784                 enable = ((codec->spdif_mask & 0x02) && (codec->spdif_mask & 0x04)) ? 1 : 0;
1785                 break;
1786         case 2:
1787                 enable = (codec->spdif_mask & 0x01) ? 1 : 0;
1788                 break;
1789         default:
1790                 break;
1791         }
1792         ucontrol->value.integer.value[0] = enable;
1793         spin_unlock_irqrestore(&codec->reg_lock, flags);
1794         return 0;
1795 }
1796
1797 static int snd_ali5451_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1798 {
1799         unsigned long flags;
1800         ali_t *codec = snd_magic_cast(ali_t, kcontrol->private_data, -ENXIO);
1801         unsigned int change = 0, enable = 0;
1802
1803         enable = ucontrol->value.integer.value[0] ? 1 : 0;
1804
1805         spin_lock_irqsave(&codec->reg_lock, flags);
1806         switch (kcontrol->private_value) {
1807         case 0:
1808                 change = (codec->spdif_mask & 0x02) ? 1 : 0;
1809                 change = change ^ enable;
1810                 if (change) {
1811                         if (enable) {
1812                                 codec->spdif_mask |= 0x02;
1813                                 snd_ali_enable_spdif_out(codec);
1814                         } else {
1815                                 codec->spdif_mask &= ~(0x02);
1816                                 codec->spdif_mask &= ~(0x04);
1817                                 snd_ali_disable_spdif_out(codec);
1818                         }
1819                 }
1820                 break;
1821         case 1: 
1822                 change = (codec->spdif_mask & 0x04) ? 1 : 0;
1823                 change = change ^ enable;
1824                 if (change && (codec->spdif_mask & 0x02)) {
1825                         if (enable) {
1826                                 codec->spdif_mask |= 0x04;
1827                                 snd_ali_enable_spdif_chnout(codec);
1828                         } else {
1829                                 codec->spdif_mask &= ~(0x04);
1830                                 snd_ali_disable_spdif_chnout(codec);
1831                         }
1832                 }
1833                 break;
1834         case 2:
1835                 change = (codec->spdif_mask & 0x01) ? 1 : 0;
1836                 change = change ^ enable;
1837                 if (change) {
1838                         if (enable) {
1839                                 codec->spdif_mask |= 0x01;
1840                                 snd_ali_enable_spdif_in(codec);
1841                         } else {
1842                                 codec->spdif_mask &= ~(0x01);
1843                                 snd_ali_disable_spdif_in(codec);
1844                         }
1845                 }
1846                 break;
1847         default:
1848                 break;
1849         }
1850         spin_unlock_irqrestore(&codec->reg_lock, flags);
1851         
1852         return change;
1853 }
1854
1855 static snd_kcontrol_new_t snd_ali5451_mixer_spdif[] __devinitdata = {
1856         /* spdif aplayback switch */
1857         /* FIXME: "IEC958 Playback Switch" may conflict with one on ac97_codec */
1858         ALI5451_SPDIF("IEC958 Output switch", 0, 0),
1859         /* spdif out to spdif channel */
1860         ALI5451_SPDIF("IEC958 Channel Output Switch", 0, 1),
1861         /* spdif in from spdif channel */
1862         ALI5451_SPDIF(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0, 2)
1863 };
1864
1865 static void snd_ali_mixer_free_ac97_bus(ac97_bus_t *bus)
1866 {
1867         ali_t *codec = snd_magic_cast(ali_t, bus->private_data, return);
1868         codec->ac97_bus = NULL;
1869 }
1870
1871 static void snd_ali_mixer_free_ac97(ac97_t *ac97)
1872 {
1873         ali_t *codec = snd_magic_cast(ali_t, ac97->private_data, return);
1874         codec->ac97 = NULL;
1875 }
1876
1877 static int __devinit snd_ali_mixer(ali_t * codec)
1878 {
1879         ac97_bus_t bus;
1880         ac97_t ac97;
1881         unsigned int idx;
1882         int err;
1883
1884         memset(&bus, 0, sizeof(bus));
1885         bus.write = snd_ali_codec_write;
1886         bus.read = snd_ali_codec_read;
1887         bus.private_data = codec;
1888         bus.private_free = snd_ali_mixer_free_ac97_bus;
1889         if ((err = snd_ac97_bus(codec->card, &bus, &codec->ac97_bus)) < 0)
1890                 return err;
1891
1892         memset(&ac97, 0, sizeof(ac97));
1893         ac97.private_data = codec;
1894         ac97.private_free = snd_ali_mixer_free_ac97;
1895         if ((err = snd_ac97_mixer(codec->ac97_bus, &ac97, &codec->ac97)) < 0) {
1896                 snd_printk("ali mixer creating error.\n");
1897                 return err;
1898         }
1899         if (codec->spdif_support) {
1900                 for(idx = 0; idx < ARRAY_SIZE(snd_ali5451_mixer_spdif); idx++) {
1901                         err=snd_ctl_add(codec->card, snd_ctl_new1(&snd_ali5451_mixer_spdif[idx], codec));
1902                         if (err < 0) return err;
1903                 }
1904         }
1905         return 0;
1906 }
1907
1908 #ifdef CONFIG_PM
1909 static void ali_suspend(ali_t *chip)
1910 {
1911         ali_image_t *im;
1912         int i, j;
1913
1914         im = chip->image;
1915         if (! im)
1916                 return;
1917
1918         spin_lock_irq(&chip->reg_lock);
1919         
1920         im->regs[ALI_MISCINT >> 2] = inl(ALI_REG(chip, ALI_MISCINT));
1921         // im->regs[ALI_START >> 2] = inl(ALI_REG(chip, ALI_START));
1922         im->regs[ALI_STOP >> 2] = inl(ALI_REG(chip, ALI_STOP));
1923         
1924         // disable all IRQ bits
1925         outl(0, ALI_REG(chip, ALI_MISCINT));
1926         
1927         for (i = 0; i < ALI_GLOBAL_REGS; i++) { 
1928                 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP))
1929                         continue;
1930                 im->regs[i] = inl(ALI_REG(chip, i*4));
1931         }
1932         
1933         for (i = 0; i < ALI_CHANNELS; i++) {
1934                 outb(i, ALI_REG(chip, ALI_GC_CIR));
1935                 for (j = 0; j < ALI_CHANNEL_REGS; j++) 
1936                         im->channel_regs[i][j] = inl(ALI_REG(chip, j*4 + 0xe0));
1937         }
1938
1939         // stop all HW channel
1940         outl(0xffffffff, ALI_REG(chip, ALI_STOP));
1941
1942         spin_unlock_irq(&chip->reg_lock);
1943 }
1944
1945 static void ali_resume(ali_t *chip)
1946 {
1947         ali_image_t *im;
1948         int i, j;
1949
1950         im = chip->image;
1951         if (! im)
1952                 return;
1953
1954         pci_enable_device(chip->pci);
1955
1956         spin_lock_irq(&chip->reg_lock);
1957         
1958         for (i = 0; i < ALI_CHANNELS; i++) {
1959                 outb(i, ALI_REG(chip, ALI_GC_CIR));
1960                 for (j = 0; j < ALI_CHANNEL_REGS; j++) 
1961                         outl(im->channel_regs[i][j], ALI_REG(chip, j*4 + 0xe0));
1962         }
1963         
1964         for (i = 0; i < ALI_GLOBAL_REGS; i++) { 
1965                 if ((i*4 == ALI_MISCINT) || (i*4 == ALI_STOP) || (i*4 == ALI_START))
1966                         continue;
1967                 outl(im->regs[i], ALI_REG(chip, i*4));
1968         }
1969         
1970         snd_ac97_resume(chip->ac97);
1971         
1972         // start HW channel
1973         outl(im->regs[ALI_START >> 2], ALI_REG(chip, ALI_START));
1974         // restore IRQ enable bits
1975         outl(im->regs[ALI_MISCINT >> 2], ALI_REG(chip, ALI_MISCINT));
1976         
1977         spin_unlock_irq(&chip->reg_lock);
1978         return;
1979 }
1980
1981 static int snd_ali_suspend(struct pci_dev *dev, u32 state)
1982 {
1983         ali_t *chip = snd_magic_cast(ali_t, pci_get_drvdata(dev), return -ENXIO);
1984         ali_suspend(chip);
1985         return 0;
1986 }
1987 static int snd_ali_resume(struct pci_dev *dev)
1988 {
1989         ali_t *chip = snd_magic_cast(ali_t, pci_get_drvdata(dev), return -ENXIO);
1990         ali_resume(chip);
1991         return 0;
1992 }
1993 #endif /* CONFIG_PM */
1994
1995 static int snd_ali_free(ali_t * codec)
1996 {
1997         if (codec->hw_initialized)
1998                 snd_ali_disable_address_interrupt(codec);
1999         if (codec->irq >= 0) {
2000                 synchronize_irq(codec->irq);
2001                 free_irq(codec->irq, (void *)codec);
2002         }
2003         if (codec->res_port) {
2004                 release_resource(codec->res_port);
2005                 kfree_nocheck(codec->res_port);
2006         }
2007 #ifdef CONFIG_PM
2008         if (codec->image)
2009                 kfree(codec->image);
2010 #endif
2011         snd_magic_kfree(codec);
2012         return 0;
2013 }
2014
2015 static int snd_ali_chip_init(ali_t *codec)
2016 {
2017         unsigned int legacy;
2018         unsigned char temp;
2019         struct pci_dev *pci_dev = NULL;
2020
2021         snd_ali_printk("chip initializing ... \n");
2022
2023         if (snd_ali_reset_5451(codec)) {
2024                 snd_printk("ali_chip_init: reset 5451 error.\n");
2025                 return -1;
2026         }
2027
2028         if (codec->revision == ALI_5451_V02) {
2029                 pci_dev = codec->pci_m1533;
2030                 pci_read_config_byte(pci_dev, 0x59, &temp);
2031                 temp |= 0x80;
2032                 pci_write_config_byte(pci_dev, 0x59, temp);
2033         
2034                 pci_dev = codec->pci_m7101;
2035                 pci_read_config_byte(pci_dev, 0xb8, &temp);
2036                 temp |= 0x20;
2037                 pci_write_config_byte(pci_dev, 0xB8, temp);
2038         }
2039
2040         pci_read_config_dword(codec->pci, 0x44, &legacy);
2041         legacy &= 0xff00ff00;
2042         legacy |= 0x000800aa;
2043         pci_write_config_dword(codec->pci, 0x44, legacy);
2044
2045         outl(0x80000001, ALI_REG(codec, ALI_GLOBAL_CONTROL));
2046         outl(0x00000000, ALI_REG(codec, ALI_AINTEN));
2047         outl(0xffffffff, ALI_REG(codec, ALI_AINT));
2048         outl(0x00000000, ALI_REG(codec, ALI_VOLUME));
2049         outb(0x10,       ALI_REG(codec, ALI_MPUR2));
2050
2051         codec->ac97_ext_id = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_ID);
2052         codec->ac97_ext_status = snd_ali_codec_peek(codec, 0, AC97_EXTENDED_STATUS);
2053         if (codec->spdif_support) {
2054                 snd_ali_enable_spdif_out(codec);
2055                 codec->spdif_mask = 0x00000002;
2056         }
2057
2058         snd_ali_printk("chip initialize succeed.\n");
2059         return 0;
2060
2061 }
2062
2063 static int __devinit snd_ali_resources(ali_t *codec)
2064 {
2065         snd_ali_printk("resouces allocation ...\n");
2066         if ((codec->res_port = request_region(codec->port, 0x100, "ALI 5451")) == NULL) {
2067                 snd_printk("Unalbe to request io ports.\n");
2068                 return -EBUSY;
2069         }
2070
2071         if (request_irq(codec->pci->irq, snd_ali_card_interrupt, SA_INTERRUPT|SA_SHIRQ, "ALI 5451", (void *)codec)) {
2072                 snd_printk("Unable to request irq.\n");
2073                 return -EBUSY;
2074         }
2075         codec->irq = codec->pci->irq;
2076         snd_ali_printk("resouces allocated.\n");
2077         return 0;
2078 }
2079 static int snd_ali_dev_free(snd_device_t *device) 
2080 {
2081         ali_t *codec=snd_magic_cast(ali_t, device->device_data, return -ENXIO);
2082         snd_ali_free(codec);
2083         return 0;
2084 }
2085
2086 static int __devinit snd_ali_create(snd_card_t * card,
2087                                     struct pci_dev *pci,
2088                                     int pcm_streams,
2089                                     int spdif_support,
2090                                     ali_t ** r_ali)
2091 {
2092         ali_t *codec;
2093         int i, err;
2094         unsigned short cmdw = 0;
2095         struct pci_dev *pci_dev = NULL;
2096         static snd_device_ops_t ops = {
2097                 (snd_dev_free_t *)snd_ali_dev_free,
2098                 NULL,
2099                 NULL
2100         };
2101
2102         *r_ali = NULL;
2103
2104         snd_ali_printk("creating ...\n");
2105
2106         /* enable PCI device */
2107         if ((err = pci_enable_device(pci)) < 0)
2108                 return err;
2109         /* check, if we can restrict PCI DMA transfers to 31 bits */
2110         if (pci_set_dma_mask(pci, 0x7fffffff) < 0 ||
2111             pci_set_consistent_dma_mask(pci, 0x7fffffff) < 0) {
2112                 snd_printk("architecture does not support 31bit PCI busmaster DMA\n");
2113                 return -ENXIO;
2114         }
2115
2116         if ((codec = snd_magic_kcalloc(ali_t, 0, GFP_KERNEL)) == NULL)
2117                 return -ENOMEM;
2118
2119         spin_lock_init(&codec->reg_lock);
2120         spin_lock_init(&codec->voice_alloc);
2121
2122         codec->card = card;
2123         codec->pci = pci;
2124         codec->irq = -1;
2125         codec->port = pci_resource_start(pci, 0);
2126         pci_read_config_byte(pci, PCI_REVISION_ID, &codec->revision);
2127         codec->spdif_support = spdif_support;
2128
2129         if (pcm_streams < 1)
2130                 pcm_streams = 1;
2131         if (pcm_streams > 32)
2132                 pcm_streams = 32;
2133         
2134         pci_set_master(pci);
2135         pci_read_config_word(pci, PCI_COMMAND, &cmdw);
2136         if ((cmdw & PCI_COMMAND_IO) != PCI_COMMAND_IO) {
2137                 cmdw |= PCI_COMMAND_IO;
2138                 pci_write_config_word(pci, PCI_COMMAND, cmdw);
2139         }
2140         pci_set_master(pci);
2141         
2142         if (snd_ali_resources(codec)) {
2143                 snd_ali_free(codec);
2144                 return -EBUSY;
2145         }
2146
2147         synchronize_irq(pci->irq);
2148
2149         codec->synth.chmap = 0;
2150         codec->synth.chcnt = 0;
2151         codec->spdif_mask = 0;
2152         codec->synth.synthcount = 0;
2153
2154         if (codec->revision == ALI_5451_V02)
2155                 codec->chregs.regs.ac97read = ALI_AC97_WRITE;
2156         else
2157                 codec->chregs.regs.ac97read = ALI_AC97_READ;
2158         codec->chregs.regs.ac97write = ALI_AC97_WRITE;
2159
2160         codec->chregs.regs.start  = ALI_START;
2161         codec->chregs.regs.stop   = ALI_STOP;
2162         codec->chregs.regs.aint   = ALI_AINT;
2163         codec->chregs.regs.ainten = ALI_AINTEN;
2164
2165         codec->chregs.data.start  = 0x00;
2166         codec->chregs.data.stop   = 0x00;
2167         codec->chregs.data.aint   = 0x00;
2168         codec->chregs.data.ainten = 0x00;
2169
2170         /* M1533: southbridge */
2171         pci_dev = pci_find_device(0x10b9, 0x1533, NULL);
2172         codec->pci_m1533 = pci_dev;
2173         if (! codec->pci_m1533) {
2174                 snd_printk(KERN_ERR "ali5451: cannot find ALi 1533 chip.\n");
2175                 snd_ali_free(codec);
2176                 return -ENODEV;
2177         }
2178         /* M7101: power management */
2179         pci_dev = pci_find_device(0x10b9, 0x7101, NULL);
2180         codec->pci_m7101 = pci_dev;
2181         if (! codec->pci_m7101 && codec->revision == ALI_5451_V02) {
2182                 snd_printk(KERN_ERR "ali5451: cannot find ALi 7101 chip.\n");
2183                 snd_ali_free(codec);
2184                 return -ENODEV;
2185         }
2186
2187         snd_ali_printk("snd_device_new is called.\n");
2188         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, codec, &ops)) < 0) {
2189                 snd_ali_free(codec);
2190                 return err;
2191         }
2192
2193         /* initialise synth voices*/
2194         for (i = 0; i < ALI_CHANNELS; i++ ) {
2195                 codec->synth.voices[i].number = i;
2196         }
2197
2198         if ((err = snd_ali_chip_init(codec)) < 0) {
2199                 snd_printk("ali create: chip init error.\n");
2200                 return err;
2201         }
2202
2203 #ifdef CONFIG_PM
2204         codec->image = kmalloc(sizeof(*codec->image), GFP_KERNEL);
2205         if (! codec->image)
2206                 snd_printk("can't allocate apm buffer\n");
2207 #endif
2208
2209         snd_ali_enable_address_interrupt(codec);
2210         codec->hw_initialized = 1;
2211
2212         *r_ali = codec;
2213         snd_ali_printk("created.\n");
2214         return 0;
2215 }
2216
2217 static int __devinit snd_ali_probe(struct pci_dev *pci,
2218                                    const struct pci_device_id *pci_id)
2219 {
2220         static int dev;
2221         snd_card_t *card;
2222         ali_t *codec;
2223         int err;
2224
2225         snd_ali_printk("probe ...\n");
2226
2227         if (dev >= SNDRV_CARDS)
2228                 return -ENODEV;
2229         if (!enable[dev]) {
2230                 dev++;
2231                 return -ENOENT;
2232         }
2233
2234         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2235         if (card == NULL)
2236                 return -ENOMEM;
2237
2238         if ((err = snd_ali_create(card, pci, pcm_channels[dev], spdif[dev], &codec)) < 0) {
2239                 snd_card_free(card);
2240                 return err;
2241         }
2242
2243         snd_ali_printk("mixer building ...\n");
2244         if ((err = snd_ali_mixer(codec)) < 0) {
2245                 snd_card_free(card);
2246                 return err;
2247         }
2248         
2249         snd_ali_printk("pcm building ...\n");
2250         if ((err = snd_ali_pcm(codec, 0, NULL)) < 0) {
2251                 snd_card_free(card);
2252                 return err;
2253         }
2254
2255         strcpy(card->driver, "ALI5451");
2256         strcpy(card->shortname, "ALI 5451");
2257         
2258         sprintf(card->longname, "%s at 0x%lx, irq %li",
2259                 card->shortname, codec->port, codec->irq);
2260
2261         snd_ali_printk("register card.\n");
2262         if ((err = snd_card_register(card)) < 0) {
2263                 snd_card_free(card);
2264                 return err;
2265         }
2266         pci_set_drvdata(pci, codec);
2267         dev++;
2268         return 0;
2269 }
2270
2271 static void __devexit snd_ali_remove(struct pci_dev *pci)
2272 {
2273         ali_t *chip = snd_magic_cast(ali_t, pci_get_drvdata(pci), return);
2274         if (chip)
2275                 snd_card_free(chip->card);
2276         pci_set_drvdata(pci, NULL);
2277 }
2278
2279 static struct pci_driver driver = {
2280         .name = "ALI 5451",
2281         .id_table = snd_ali_ids,
2282         .probe = snd_ali_probe,
2283         .remove = __devexit_p(snd_ali_remove),
2284 #ifdef CONFIG_PM
2285         .suspend = snd_ali_suspend,
2286         .resume = snd_ali_resume,
2287 #endif
2288 };                                
2289
2290 static int __init alsa_card_ali_init(void)
2291 {
2292         int err;
2293
2294         if ((err = pci_module_init(&driver)) < 0) {
2295 #ifdef MODULE
2296                 printk(KERN_ERR "ALi pci audio not found or device busy.\n");
2297 #endif
2298                 return err;
2299         }
2300         return 0;
2301 }
2302
2303 static void __exit alsa_card_ali_exit(void)
2304 {
2305         pci_unregister_driver(&driver);
2306 }
2307
2308 module_init(alsa_card_ali_init)
2309 module_exit(alsa_card_ali_exit)
2310
2311 #ifndef MODULE
2312
2313 /* format is: snd-ali5451=enable,index,id,pcm_channels */
2314
2315 static int __init alsa_card_ali_setup(char *str)
2316 {
2317         static unsigned __initdata nr_dev = 0;
2318
2319         if (nr_dev >= SNDRV_CARDS)
2320                 return 0;
2321         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
2322                get_option(&str,&index[nr_dev]) == 2 &&
2323                get_id(&str,&id[nr_dev]) == 2 &&
2324                get_option(&str,&pcm_channels[nr_dev]) == 2);
2325         nr_dev++;
2326         return 1;
2327 }
2328
2329 __setup("snd-ali5451=", alsa_card_ali_setup);
2330
2331 #endif /* ifndef */