patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / pci / ice1712 / ice1724.c
1 /*
2  *   ALSA driver for VT1724 ICEnsemble ICE1724 / VIA VT1724 (Envy24HT)
3  *                   VIA VT1720 (Envy24PT)
4  *
5  *      Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
6  *                    2002 James Stafford <jstafford@ampltd.com>
7  *                    2003 Takashi Iwai <tiwai@suse.de>
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  *
23  */      
24
25 #include <sound/driver.h>
26 #include <asm/io.h>
27 #include <linux/delay.h>
28 #include <linux/interrupt.h>
29 #include <linux/init.h>
30 #include <linux/pci.h>
31 #include <linux/slab.h>
32 #include <linux/moduleparam.h>
33 #include <sound/core.h>
34 #include <sound/info.h>
35 #include <sound/mpu401.h>
36 #include <sound/initval.h>
37
38 #include <sound/asoundef.h>
39
40 #include "ice1712.h"
41 #include "envy24ht.h"
42
43 /* lowlevel routines */
44 #include "amp.h"
45 #include "revo.h"
46 #include "aureon.h"
47
48
49 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
50 MODULE_DESCRIPTION("VIA ICEnsemble ICE1724/1720 (Envy24HT/PT)");
51 MODULE_LICENSE("GPL");
52 MODULE_CLASSES("{sound}");
53 MODULE_DEVICES("{"
54                REVO_DEVICE_DESC
55                AMP_AUDIO2000_DEVICE_DESC
56                AUREON_DEVICE_DESC
57                 "{VIA,VT1720},"
58                 "{VIA,VT1724},"
59                 "{ICEnsemble,Generic ICE1724},"
60                 "{ICEnsemble,Generic Envy24HT}"
61                 "{ICEnsemble,Generic Envy24PT}}");
62
63 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
64 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
65 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;              /* Enable this card */
66 static char *model[SNDRV_CARDS];
67 static int boot_devs;
68
69 module_param_array(index, int, boot_devs, 0444);
70 MODULE_PARM_DESC(index, "Index value for ICE1724 soundcard.");
71 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
72 module_param_array(id, charp, boot_devs, 0444);
73 MODULE_PARM_DESC(id, "ID string for ICE1724 soundcard.");
74 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
75 module_param_array(enable, bool, boot_devs, 0444);
76 MODULE_PARM_DESC(enable, "Enable ICE1724 soundcard.");
77 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
78 module_param_array(model, charp, boot_devs, 0444);
79 MODULE_PARM_DESC(model, "Use the given board model.");
80
81 #ifndef PCI_VENDOR_ID_ICE
82 #define PCI_VENDOR_ID_ICE               0x1412
83 #endif
84 #ifndef PCI_DEVICE_ID_VT1724
85 #define PCI_DEVICE_ID_VT1724            0x1724
86 #endif
87
88 /* Both VT1720 and VT1724 have the same PCI IDs */
89 static struct pci_device_id snd_vt1724_ids[] = {
90         { PCI_VENDOR_ID_ICE, PCI_DEVICE_ID_VT1724, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
91         { 0, }
92 };
93
94 MODULE_DEVICE_TABLE(pci, snd_vt1724_ids);
95
96
97 static int PRO_RATE_LOCKED;
98 static int PRO_RATE_RESET = 1;
99 static unsigned int PRO_RATE_DEFAULT = 44100;
100
101 /*
102  *  Basic I/O
103  */
104  
105 /* check whether the clock mode is spdif-in */
106 static inline int is_spdif_master(ice1712_t *ice)
107 {
108         return (inb(ICEMT1724(ice, RATE)) & VT1724_SPDIF_MASTER) ? 1 : 0;
109 }
110
111 static inline int is_pro_rate_locked(ice1712_t *ice)
112 {
113         return is_spdif_master(ice) || PRO_RATE_LOCKED;
114 }
115
116 /*
117  * ac97 section
118  */
119
120 static unsigned char snd_vt1724_ac97_ready(ice1712_t *ice)
121 {
122         unsigned char old_cmd;
123         int tm;
124         for (tm = 0; tm < 0x10000; tm++) {
125                 old_cmd = inb(ICEMT1724(ice, AC97_CMD));
126                 if (old_cmd & (VT1724_AC97_WRITE | VT1724_AC97_READ))
127                         continue;
128                 if (!(old_cmd & VT1724_AC97_READY))
129                         continue;
130                 return old_cmd;
131         }
132         snd_printd(KERN_ERR "snd_vt1724_ac97_ready: timeout\n");
133         return old_cmd;
134 }
135
136 static int snd_vt1724_ac97_wait_bit(ice1712_t *ice, unsigned char bit)
137 {
138         int tm;
139         for (tm = 0; tm < 0x10000; tm++)
140                 if ((inb(ICEMT1724(ice, AC97_CMD)) & bit) == 0)
141                         return 0;
142         snd_printd(KERN_ERR "snd_vt1724_ac97_wait_bit: timeout\n");
143         return -EIO;
144 }
145
146 static void snd_vt1724_ac97_write(ac97_t *ac97,
147                                   unsigned short reg,
148                                   unsigned short val)
149 {
150         ice1712_t *ice = (ice1712_t *)ac97->private_data;
151         unsigned char old_cmd;
152
153         old_cmd = snd_vt1724_ac97_ready(ice);
154         old_cmd &= ~VT1724_AC97_ID_MASK;
155         old_cmd |= ac97->num;
156         outb(reg, ICEMT1724(ice, AC97_INDEX));
157         outw(val, ICEMT1724(ice, AC97_DATA));
158         outb(old_cmd | VT1724_AC97_WRITE, ICEMT1724(ice, AC97_CMD));
159         snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_WRITE);
160 }
161
162 static unsigned short snd_vt1724_ac97_read(ac97_t *ac97, unsigned short reg)
163 {
164         ice1712_t *ice = (ice1712_t *)ac97->private_data;
165         unsigned char old_cmd;
166
167         old_cmd = snd_vt1724_ac97_ready(ice);
168         old_cmd &= ~VT1724_AC97_ID_MASK;
169         old_cmd |= ac97->num;
170         outb(reg, ICEMT1724(ice, AC97_INDEX));
171         outb(old_cmd | VT1724_AC97_READ, ICEMT1724(ice, AC97_CMD));
172         if (snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_READ) < 0)
173                 return ~0;
174         return inw(ICEMT1724(ice, AC97_DATA));
175 }
176
177
178 /*
179  * GPIO operations
180  */
181
182 /* set gpio direction 0 = read, 1 = write */
183 static void snd_vt1724_set_gpio_dir(ice1712_t *ice, unsigned int data)
184 {
185         outl(data, ICEREG1724(ice, GPIO_DIRECTION));
186         inw(ICEREG1724(ice, GPIO_DIRECTION)); /* dummy read for pci-posting */
187 }
188
189 /* set the gpio mask (0 = writable) */
190 static void snd_vt1724_set_gpio_mask(ice1712_t *ice, unsigned int data)
191 {
192         outw(data, ICEREG1724(ice, GPIO_WRITE_MASK));
193         outb((data >> 16) & 0xff, ICEREG1724(ice, GPIO_WRITE_MASK_22));
194         inw(ICEREG1724(ice, GPIO_WRITE_MASK)); /* dummy read for pci-posting */
195 }
196
197 static void snd_vt1724_set_gpio_data(ice1712_t *ice, unsigned int data)
198 {
199         outw(data, ICEREG1724(ice, GPIO_DATA));
200         outb(data >> 16, ICEREG1724(ice, GPIO_DATA_22));
201         inw(ICEREG1724(ice, GPIO_DATA)); /* dummy read for pci-posting */
202 }
203
204 static unsigned int snd_vt1724_get_gpio_data(ice1712_t *ice)
205 {
206         unsigned int data;
207         data = (unsigned int)inb(ICEREG1724(ice, GPIO_DATA_22));
208         data = (data << 16) | inw(ICEREG1724(ice, GPIO_DATA));
209         return data;
210 }
211
212 /*
213  *  Interrupt handler
214  */
215
216 static irqreturn_t snd_vt1724_interrupt(int irq, void *dev_id, struct pt_regs *regs)
217 {
218         ice1712_t *ice = snd_magic_cast(ice1712_t, dev_id, return IRQ_NONE);
219         unsigned char status;
220         int handled = 0;
221
222         while (1) {
223                 status = inb(ICEREG1724(ice, IRQSTAT));
224                 if (status == 0)
225                         break;
226
227                 handled = 1;            
228                 /*  these should probably be separated at some point, 
229                         but as we don't currently have MPU support on the board I will leave it */
230                 if ((status & VT1724_IRQ_MPU_RX)||(status & VT1724_IRQ_MPU_TX)) {
231                         if (ice->rmidi[0])
232                                 snd_mpu401_uart_interrupt(irq, ice->rmidi[0]->private_data, regs);
233                         outb(VT1724_IRQ_MPU_RX|VT1724_IRQ_MPU_TX, ICEREG1724(ice, IRQSTAT));
234                         status &= ~(VT1724_IRQ_MPU_RX|VT1724_IRQ_MPU_TX);
235                 }
236                 if (status & VT1724_IRQ_MTPCM) {
237                         /*
238                          * Multi-track PCM
239                          * PCM assignment are:
240                          * Playback DMA0 (M/C) = playback_pro_substream
241                          * Playback DMA1 = playback_con_substream_ds[0]
242                          * Playback DMA2 = playback_con_substream_ds[1]
243                          * Playback DMA3 = playback_con_substream_ds[2]
244                          * Playback DMA4 (SPDIF) = playback_con_substream
245                          * Record DMA0 = capture_pro_substream
246                          * Record DMA1 = capture_con_substream
247                          */
248                         unsigned char mtstat = inb(ICEMT1724(ice, IRQ));
249                         if (mtstat & VT1724_MULTI_PDMA0) {
250                                 if (ice->playback_pro_substream)
251                                         snd_pcm_period_elapsed(ice->playback_pro_substream);
252                         }
253                         if (mtstat & VT1724_MULTI_RDMA0) {
254                                 if (ice->capture_pro_substream)
255                                         snd_pcm_period_elapsed(ice->capture_pro_substream);
256                         }
257                         if (mtstat & VT1724_MULTI_PDMA1) {
258                                 if (ice->playback_con_substream_ds[0])
259                                         snd_pcm_period_elapsed(ice->playback_con_substream_ds[0]);
260                         }
261                         if (mtstat & VT1724_MULTI_PDMA2) {
262                                 if (ice->playback_con_substream_ds[1])
263                                         snd_pcm_period_elapsed(ice->playback_con_substream_ds[1]);
264                         }
265                         if (mtstat & VT1724_MULTI_PDMA3) {
266                                 if (ice->playback_con_substream_ds[2])
267                                         snd_pcm_period_elapsed(ice->playback_con_substream_ds[2]);
268                         }
269                         if (mtstat & VT1724_MULTI_PDMA4) {
270                                 if (ice->playback_con_substream)
271                                         snd_pcm_period_elapsed(ice->playback_con_substream);
272                         }
273                         if (mtstat & VT1724_MULTI_RDMA1) {
274                                 if (ice->capture_con_substream)
275                                         snd_pcm_period_elapsed(ice->capture_con_substream);
276                         }
277                         /* ack anyway to avoid freeze */
278                         outb(mtstat, ICEMT1724(ice, IRQ));
279                         /* ought to really handle this properly */
280                         if (mtstat & VT1724_MULTI_FIFO_ERR) {
281                                 unsigned char fstat = inb(ICEMT1724(ice, DMA_FIFO_ERR));
282                                 outb(fstat, ICEMT1724(ice, DMA_FIFO_ERR));      
283                                 outb(VT1724_MULTI_FIFO_ERR | inb(ICEMT1724(ice, DMA_INT_MASK)), ICEMT1724(ice, DMA_INT_MASK));  
284                                 /* If I don't do this, I get machine lockup due to continual interrupts */
285                         }
286
287                 }
288         }
289         return IRQ_RETVAL(handled);
290 }
291
292 /*
293  *  PCM code - professional part (multitrack)
294  */
295
296 static unsigned int rates[] = {
297         8000, 9600, 11025, 12000, 16000, 22050, 24000,
298         32000, 44100, 48000, 64000, 88200, 96000,
299         176400, 192000,
300 };
301
302 static snd_pcm_hw_constraint_list_t hw_constraints_rates_96 = {
303         .count = ARRAY_SIZE(rates) - 2, /* up to 96000 */
304         .list = rates,
305         .mask = 0,
306 };
307
308 static snd_pcm_hw_constraint_list_t hw_constraints_rates_48 = {
309         .count = ARRAY_SIZE(rates) - 5, /* up to 48000 */
310         .list = rates,
311         .mask = 0,
312 };
313
314 static snd_pcm_hw_constraint_list_t hw_constraints_rates_192 = {
315         .count = ARRAY_SIZE(rates),
316         .list = rates,
317         .mask = 0,
318 };
319
320 static int snd_vt1724_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
321 {
322         ice1712_t *ice = snd_pcm_substream_chip(substream);
323         unsigned char what;
324         unsigned char old;
325         struct list_head *pos;
326         snd_pcm_substream_t *s;
327
328         what = 0;
329         snd_pcm_group_for_each(pos, substream) {
330                 s = snd_pcm_group_substream_entry(pos);
331                 what |= (unsigned long)(s->runtime->private_data);
332                 snd_pcm_trigger_done(s, substream);
333         }
334
335         switch (cmd) {
336         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
337         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
338                 spin_lock(&ice->reg_lock);
339                 old = inb(ICEMT1724(ice, DMA_PAUSE));
340                 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
341                         old |= what;
342                 else
343                         old &= ~what;
344                 outb(old, ICEMT1724(ice, DMA_PAUSE));
345                 spin_unlock(&ice->reg_lock);
346                 break;
347
348         case SNDRV_PCM_TRIGGER_START:
349         case SNDRV_PCM_TRIGGER_STOP:
350                 spin_lock(&ice->reg_lock);
351                 old = inb(ICEMT1724(ice, DMA_CONTROL));
352                 if (cmd == SNDRV_PCM_TRIGGER_START)
353                         old |= what;
354                 else
355                         old &= ~what;
356                 outb(old, ICEMT1724(ice, DMA_CONTROL));
357                 spin_unlock(&ice->reg_lock);
358                 break;
359
360         default:
361                 return -EINVAL;
362         }
363         return 0;
364 }
365
366 /*
367  */
368
369 #define DMA_STARTS      (VT1724_RDMA0_START|VT1724_PDMA0_START|VT1724_RDMA1_START|\
370         VT1724_PDMA1_START|VT1724_PDMA2_START|VT1724_PDMA3_START|VT1724_PDMA4_START)
371 #define DMA_PAUSES      (VT1724_RDMA0_PAUSE|VT1724_PDMA0_PAUSE|VT1724_RDMA1_PAUSE|\
372         VT1724_PDMA1_PAUSE|VT1724_PDMA2_PAUSE|VT1724_PDMA3_PAUSE|VT1724_PDMA4_PAUSE)
373
374 static void snd_vt1724_set_pro_rate(ice1712_t *ice, unsigned int rate, int force)
375 {
376         unsigned long flags;
377         unsigned char val, old;
378         unsigned int i;
379
380         spin_lock_irqsave(&ice->reg_lock, flags);
381         if ((inb(ICEMT1724(ice, DMA_CONTROL)) & DMA_STARTS) || 
382             (inb(ICEMT1724(ice, DMA_PAUSE)) & DMA_PAUSES)) {
383                 /* running? we cannot change the rate now... */
384                 spin_unlock_irqrestore(&ice->reg_lock, flags);
385                 return;
386         }
387         if (!force && is_pro_rate_locked(ice)) {
388                 spin_unlock_irqrestore(&ice->reg_lock, flags);
389                 return;
390         }
391
392         switch (rate) {
393         case 8000: val = 6; break;
394         case 9600: val = 3; break;
395         case 11025: val = 10; break;
396         case 12000: val = 2; break;
397         case 16000: val = 5; break;
398         case 22050: val = 9; break;
399         case 24000: val = 1; break;
400         case 32000: val = 4; break;
401         case 44100: val = 8; break;
402         case 48000: val = 0; break;
403         case 64000: val = 15; break;
404         case 88200: val = 11; break;
405         case 96000: val = 7; break;
406         case 176400: val = 12; break;
407         case 192000: val = 14; break;
408         default:
409                 snd_BUG();
410                 val = 0;
411                 break;
412         }
413         outb(val, ICEMT1724(ice, RATE));
414         if (rate == ice->cur_rate) {
415                 spin_unlock_irqrestore(&ice->reg_lock, flags);
416                 return;
417         }
418
419         ice->cur_rate = rate;
420
421         /* check MT02 */
422         if (ice->eeprom.data[ICE_EEP2_ACLINK] & 0x80) {
423                 val = old = inb(ICEMT1724(ice, I2S_FORMAT));
424                 if (rate > 96000)
425                         val |= VT1724_MT_I2S_MCLK_128X; /* 128x MCLK */
426                 else
427                         val &= ~VT1724_MT_I2S_MCLK_128X; /* 256x MCLK */
428                 if (val != old) {
429                         outb(val, ICEMT1724(ice, I2S_FORMAT));
430                         if (ice->eeprom.subvendor == VT1724_SUBDEVICE_REVOLUTION71) {
431                                 /* FIXME: is this revo only? */
432                                 /* assert PRST# to converters; MT05 bit 7 */
433                                 outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
434                                 spin_unlock_irqrestore(&ice->reg_lock, flags);
435                                 mdelay(5);
436                                 spin_lock_irqsave(&ice->reg_lock, flags);
437                                 /* deassert PRST# */
438                                 outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
439                         }
440                 }
441         }
442         spin_unlock_irqrestore(&ice->reg_lock, flags);
443
444         /* set up codecs */
445         for (i = 0; i < ice->akm_codecs; i++) {
446                 if (ice->akm[i].ops.set_rate_val)
447                         ice->akm[i].ops.set_rate_val(&ice->akm[i], rate);
448         }
449
450         /* set up AC97 registers if needed */
451         if (! (ice->eeprom.data[ICE_EEP2_ACLINK] & 0x80) && ice->ac97) {
452                 snd_ac97_set_rate(ice->ac97, AC97_PCM_FRONT_DAC_RATE, rate);
453                 snd_ac97_set_rate(ice->ac97, AC97_PCM_SURR_DAC_RATE, rate);
454                 snd_ac97_set_rate(ice->ac97, AC97_PCM_LFE_DAC_RATE, rate);
455                 snd_ac97_set_rate(ice->ac97, AC97_SPDIF, rate);
456                 snd_ac97_set_rate(ice->ac97, AC97_PCM_LR_ADC_RATE, rate);
457         }
458 }
459
460 static int snd_vt1724_pcm_hw_params(snd_pcm_substream_t * substream,
461                                     snd_pcm_hw_params_t * hw_params)
462 {
463         ice1712_t *ice = snd_pcm_substream_chip(substream);
464         int i, chs;
465
466         chs = params_channels(hw_params);
467         down(&ice->open_mutex);
468         /* mark surround channels */
469         if (substream == ice->playback_pro_substream) {
470                 /* PDMA0 can be multi-channel up to 8 */
471                 chs = chs / 2 - 1;
472                 for (i = 0; i < chs; i++) {
473                         if (ice->pcm_reserved[i] && ice->pcm_reserved[i] != substream) {
474                                 up(&ice->open_mutex);
475                                 return -EBUSY;
476                         }
477                         ice->pcm_reserved[i] = substream;
478                 }
479                 for (; i < 3; i++) {
480                         if (ice->pcm_reserved[i] == substream)
481                                 ice->pcm_reserved[i] = NULL;
482                 }
483         } else {
484                 for (i = 0; i < 3; i++) {
485                         /* check individual playback stream */
486                         if (ice->playback_con_substream_ds[i] == substream) {
487                                 if (ice->pcm_reserved[i] && ice->pcm_reserved[i] != substream) {
488                                         up(&ice->open_mutex);
489                                         return -EBUSY;
490                                 }
491                                 ice->pcm_reserved[i] = substream;
492                                 break;
493                         }
494                 }
495         }
496         up(&ice->open_mutex);
497         snd_vt1724_set_pro_rate(ice, params_rate(hw_params), 0);
498         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
499 }
500
501 static int snd_vt1724_pcm_hw_free(snd_pcm_substream_t * substream)
502 {
503         ice1712_t *ice = snd_pcm_substream_chip(substream);
504         int i;
505
506         down(&ice->open_mutex);
507         /* unmark surround channels */
508         for (i = 0; i < 3; i++)
509                 if (ice->pcm_reserved[i] == substream)
510                         ice->pcm_reserved[i] = NULL;
511         up(&ice->open_mutex);
512         return snd_pcm_lib_free_pages(substream);
513 }
514
515 static int snd_vt1724_playback_pro_prepare(snd_pcm_substream_t * substream)
516 {
517         ice1712_t *ice = snd_pcm_substream_chip(substream);
518         unsigned char val;
519         unsigned int size;
520
521         spin_lock(&ice->reg_lock);
522         val = (8 - substream->runtime->channels) >> 1;
523         outb(val, ICEMT1724(ice, BURST));
524
525         outl(substream->runtime->dma_addr, ICEMT1724(ice, PLAYBACK_ADDR));
526
527         size = (snd_pcm_lib_buffer_bytes(substream) >> 2) - 1;
528         // outl(size, ICEMT1724(ice, PLAYBACK_SIZE));
529         outw(size, ICEMT1724(ice, PLAYBACK_SIZE));
530         outb(size >> 16, ICEMT1724(ice, PLAYBACK_SIZE) + 2);
531         size = (snd_pcm_lib_period_bytes(substream) >> 2) - 1;
532         // outl(size, ICEMT1724(ice, PLAYBACK_COUNT));
533         outw(size, ICEMT1724(ice, PLAYBACK_COUNT));
534         outb(size >> 16, ICEMT1724(ice, PLAYBACK_COUNT) + 2);
535
536         spin_unlock(&ice->reg_lock);
537
538         // printk("pro prepare: ch = %d, addr = 0x%x, buffer = 0x%x, period = 0x%x\n", substream->runtime->channels, (unsigned int)substream->runtime->dma_addr, snd_pcm_lib_buffer_bytes(substream), snd_pcm_lib_period_bytes(substream));
539         return 0;
540 }
541
542 static snd_pcm_uframes_t snd_vt1724_playback_pro_pointer(snd_pcm_substream_t * substream)
543 {
544         ice1712_t *ice = snd_pcm_substream_chip(substream);
545         size_t ptr;
546
547         if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & VT1724_PDMA0_START))
548                 return 0;
549 #if 0 /* read PLAYBACK_ADDR */
550         ptr = inl(ICEMT1724(ice, PLAYBACK_ADDR));
551         if (ptr < substream->runtime->dma_addr) {
552                 snd_printd("ice1724: invalid negative ptr\n");
553                 return 0;
554         }
555         ptr -= substream->runtime->dma_addr;
556         ptr = bytes_to_frames(substream->runtime, ptr);
557         if (ptr >= substream->runtime->buffer_size) {
558                 snd_printd("ice1724: invalid ptr %d (size=%d)\n", (int)ptr, (int)substream->runtime->period_size);
559                 return 0;
560         }
561 #else /* read PLAYBACK_SIZE */
562         ptr = inl(ICEMT1724(ice, PLAYBACK_SIZE)) & 0xffffff;
563         ptr = (ptr + 1) << 2;
564         ptr = bytes_to_frames(substream->runtime, ptr);
565         if (ptr <= substream->runtime->buffer_size)
566                 ptr = substream->runtime->buffer_size - ptr;
567         else {
568                 snd_printd("ice1724: invalid ptr %d (size=%d)\n", (int)ptr, (int)substream->runtime->buffer_size);
569                 ptr = 0;
570         }
571 #endif
572         return ptr;
573 }
574
575 struct vt1724_pcm_reg {
576         unsigned int addr;      /* ADDR register offset */
577         unsigned int size;      /* SIZE register offset */
578         unsigned int count;     /* COUNT register offset */
579         unsigned int start;     /* start bit */
580         unsigned int pause;     /* pause bit */
581 };
582
583 static int snd_vt1724_pcm_prepare(snd_pcm_substream_t *substream, const struct vt1724_pcm_reg *reg)
584 {
585         ice1712_t *ice = snd_pcm_substream_chip(substream);
586
587         spin_lock(&ice->reg_lock);
588         outl(substream->runtime->dma_addr, ice->profi_port + reg->addr);
589         outw((snd_pcm_lib_buffer_bytes(substream) >> 2) - 1, ice->profi_port + reg->size);
590         outw((snd_pcm_lib_period_bytes(substream) >> 2) - 1, ice->profi_port + reg->count);
591         spin_unlock(&ice->reg_lock);
592         return 0;
593 }
594
595 static snd_pcm_uframes_t snd_vt1724_pcm_pointer(snd_pcm_substream_t *substream, const struct vt1724_pcm_reg *reg)
596 {
597         ice1712_t *ice = snd_pcm_substream_chip(substream);
598         size_t ptr;
599
600         if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & reg->start))
601                 return 0;
602 #if 0 /* use ADDR register */
603         ptr = inl(ice->profi_port + reg->addr);
604         ptr -= substream->runtime->dma_addr;
605         return bytes_to_frames(substream->runtime, ptr);
606 #else /* use SIZE register */
607         ptr = inw(ice->profi_port + reg->size);
608         ptr = (ptr + 1) << 2;
609         ptr = bytes_to_frames(substream->runtime, ptr);
610         if (ptr <= substream->runtime->buffer_size)
611                 ptr = substream->runtime->buffer_size - ptr;
612         else {
613                 snd_printd("ice1724: invalid ptr %d (size=%d)\n", (int)ptr, (int)substream->runtime->buffer_size);
614                 ptr = 0;
615         }
616         return ptr;
617 #endif
618 }
619
620 const static struct vt1724_pcm_reg vt1724_capture_pro_reg = {
621         .addr = VT1724_MT_CAPTURE_ADDR,
622         .size = VT1724_MT_CAPTURE_SIZE,
623         .count = VT1724_MT_CAPTURE_COUNT,
624         .start = VT1724_RDMA0_START,
625         .pause = VT1724_RDMA0_PAUSE,
626 };
627
628 static int snd_vt1724_capture_pro_prepare(snd_pcm_substream_t * substream)
629 {
630         return snd_vt1724_pcm_prepare(substream, &vt1724_capture_pro_reg);
631 }
632
633 static snd_pcm_uframes_t snd_vt1724_capture_pro_pointer(snd_pcm_substream_t * substream)
634 {
635         return snd_vt1724_pcm_pointer(substream, &vt1724_capture_pro_reg);
636 }
637
638 static snd_pcm_hardware_t snd_vt1724_playback_pro =
639 {
640         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
641                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
642                                  SNDRV_PCM_INFO_MMAP_VALID |
643                                  SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
644         .formats =              SNDRV_PCM_FMTBIT_S32_LE,
645         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
646         .rate_min =             8000,
647         .rate_max =             192000,
648         .channels_min =         2,
649         .channels_max =         8,
650         .buffer_bytes_max =     (1UL << 21),    /* 19bits dword */
651         .period_bytes_min =     8 * 4 * 2,      /* FIXME: constraints needed */
652         .period_bytes_max =     (1UL << 21),
653         .periods_min =          2,
654         .periods_max =          1024,
655 };
656
657 static snd_pcm_hardware_t snd_vt1724_spdif =
658 {
659         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
660                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
661                                  SNDRV_PCM_INFO_MMAP_VALID |
662                                  SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
663         .formats =              SNDRV_PCM_FMTBIT_S32_LE,
664         .rates =                SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000,
665         .rate_min =             32000,
666         .rate_max =             48000,
667         .channels_min =         2,
668         .channels_max =         2,
669         .buffer_bytes_max =     (1UL << 18),    /* 16bits dword */
670         .period_bytes_min =     2 * 4 * 2,
671         .period_bytes_max =     (1UL << 18),
672         .periods_min =          2,
673         .periods_max =          1024,
674 };
675
676 static snd_pcm_hardware_t snd_vt1724_2ch_stereo =
677 {
678         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
679                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
680                                  SNDRV_PCM_INFO_MMAP_VALID |
681                                  SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
682         .formats =              SNDRV_PCM_FMTBIT_S32_LE,
683         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
684         .rate_min =             8000,
685         .rate_max =             192000,
686         .channels_min =         2,
687         .channels_max =         2,
688         .buffer_bytes_max =     (1UL << 18),    /* 16bits dword */
689         .period_bytes_min =     2 * 4 * 2,
690         .period_bytes_max =     (1UL << 18),
691         .periods_min =          2,
692         .periods_max =          1024,
693 };
694
695 /*
696  * set rate constraints
697  */
698 static int set_rate_constraints(ice1712_t *ice, snd_pcm_substream_t *substream)
699 {
700         snd_pcm_runtime_t *runtime = substream->runtime;
701         if (ice->eeprom.data[ICE_EEP2_ACLINK] & 0x80) {
702                 /* I2S */
703                 if (ice->eeprom.data[ICE_EEP2_I2S] & 0x08)
704                         return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates_192);
705                 else {
706                         runtime->hw.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_96000;
707                         runtime->hw.rate_max = 96000;
708                         return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates_96);
709                 }
710         } else if (ice->ac97) {
711                 /* ACLINK */
712                 int ratec;
713                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
714                         ratec = AC97_RATES_FRONT_DAC;
715                 else
716                         ratec = AC97_RATES_ADC;
717                 runtime->hw.rates = ice->ac97->rates[ratec];
718                 runtime->hw.rate_max = 48000;
719                 if (runtime->hw.rates == SNDRV_PCM_RATE_48000) {
720                         runtime->hw.rate_min = 48000;
721                         return 0;
722                 } else {
723                         runtime->hw.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000;
724                         return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates_48);
725                 }
726         }
727         return 0;
728 }
729
730 /* multi-channel playback needs alignment 8x32bit regarless of the channels
731  * actually used
732  */
733 #define VT1724_BUFFER_ALIGN     0x20
734
735 static int snd_vt1724_playback_pro_open(snd_pcm_substream_t * substream)
736 {
737         snd_pcm_runtime_t *runtime = substream->runtime;
738         ice1712_t *ice = snd_pcm_substream_chip(substream);
739         int chs;
740
741         runtime->private_data = (void*)VT1724_PDMA0_START; /* irq/status/trigger bit */
742         ice->playback_pro_substream = substream;
743         runtime->hw = snd_vt1724_playback_pro;
744         snd_pcm_set_sync(substream);
745         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
746         set_rate_constraints(ice, substream);
747         down(&ice->open_mutex);
748         /* calculate the currently available channels */
749         for (chs = 0; chs < 3; chs++) {
750                 if (ice->pcm_reserved[chs])
751                         break;
752         }
753         chs = (chs + 1) * 2;
754         runtime->hw.channels_max = chs;
755         if (chs > 2) /* channels must be even */
756                 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
757         up(&ice->open_mutex);
758         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
759                                    VT1724_BUFFER_ALIGN);
760         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
761                                    VT1724_BUFFER_ALIGN);
762         return 0;
763 }
764
765 static int snd_vt1724_capture_pro_open(snd_pcm_substream_t * substream)
766 {
767         ice1712_t *ice = snd_pcm_substream_chip(substream);
768         snd_pcm_runtime_t *runtime = substream->runtime;
769
770         runtime->private_data = (void*)VT1724_RDMA0_START; /* irq/status/trigger bit */
771         ice->capture_pro_substream = substream;
772         runtime->hw = snd_vt1724_2ch_stereo;
773         snd_pcm_set_sync(substream);
774         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
775         set_rate_constraints(ice, substream);
776         return 0;
777 }
778
779 static int snd_vt1724_playback_pro_close(snd_pcm_substream_t * substream)
780 {
781         ice1712_t *ice = snd_pcm_substream_chip(substream);
782
783         if (PRO_RATE_RESET)
784                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
785         ice->playback_pro_substream = NULL;
786
787         return 0;
788 }
789
790 static int snd_vt1724_capture_pro_close(snd_pcm_substream_t * substream)
791 {
792         ice1712_t *ice = snd_pcm_substream_chip(substream);
793
794         if (PRO_RATE_RESET)
795                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
796         ice->capture_pro_substream = NULL;
797         return 0;
798 }
799
800 static snd_pcm_ops_t snd_vt1724_playback_pro_ops = {
801         .open =         snd_vt1724_playback_pro_open,
802         .close =        snd_vt1724_playback_pro_close,
803         .ioctl =        snd_pcm_lib_ioctl,
804         .hw_params =    snd_vt1724_pcm_hw_params,
805         .hw_free =      snd_vt1724_pcm_hw_free,
806         .prepare =      snd_vt1724_playback_pro_prepare,
807         .trigger =      snd_vt1724_pcm_trigger,
808         .pointer =      snd_vt1724_playback_pro_pointer,
809 };
810
811 static snd_pcm_ops_t snd_vt1724_capture_pro_ops = {
812         .open =         snd_vt1724_capture_pro_open,
813         .close =        snd_vt1724_capture_pro_close,
814         .ioctl =        snd_pcm_lib_ioctl,
815         .hw_params =    snd_vt1724_pcm_hw_params,
816         .hw_free =      snd_vt1724_pcm_hw_free,
817         .prepare =      snd_vt1724_capture_pro_prepare,
818         .trigger =      snd_vt1724_pcm_trigger,
819         .pointer =      snd_vt1724_capture_pro_pointer,
820 };
821
822 static int __devinit snd_vt1724_pcm_profi(ice1712_t * ice, int device)
823 {
824         snd_pcm_t *pcm;
825         int err;
826
827         err = snd_pcm_new(ice->card, "ICE1724", device, 1, 1, &pcm);
828         if (err < 0)
829                 return err;
830
831         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_vt1724_playback_pro_ops);
832         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_vt1724_capture_pro_ops);
833
834         pcm->private_data = ice;
835         pcm->info_flags = 0;
836         strcpy(pcm->name, "ICE1724");
837
838         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
839                                               snd_dma_pci_data(ice->pci), 256*1024, 256*1024);
840
841         ice->pcm_pro = pcm;
842
843         return 0;
844 }
845
846
847 /*
848  * SPDIF PCM
849  */
850
851 const static struct vt1724_pcm_reg vt1724_playback_spdif_reg = {
852         .addr = VT1724_MT_PDMA4_ADDR,
853         .size = VT1724_MT_PDMA4_SIZE,
854         .count = VT1724_MT_PDMA4_COUNT,
855         .start = VT1724_PDMA4_START,
856         .pause = VT1724_PDMA4_PAUSE,
857 };
858
859 const static struct vt1724_pcm_reg vt1724_capture_spdif_reg = {
860         .addr = VT1724_MT_RDMA1_ADDR,
861         .size = VT1724_MT_RDMA1_SIZE,
862         .count = VT1724_MT_RDMA1_COUNT,
863         .start = VT1724_RDMA1_START,
864         .pause = VT1724_RDMA1_PAUSE,
865 };
866
867 static int snd_vt1724_playback_spdif_prepare(snd_pcm_substream_t * substream)
868 {
869         return snd_vt1724_pcm_prepare(substream, &vt1724_playback_spdif_reg);
870 }
871
872 static snd_pcm_uframes_t snd_vt1724_playback_spdif_pointer(snd_pcm_substream_t * substream)
873 {
874         return snd_vt1724_pcm_pointer(substream, &vt1724_playback_spdif_reg);
875 }
876
877 static int snd_vt1724_capture_spdif_prepare(snd_pcm_substream_t * substream)
878 {
879         return snd_vt1724_pcm_prepare(substream, &vt1724_capture_spdif_reg);
880 }
881
882 static snd_pcm_uframes_t snd_vt1724_capture_spdif_pointer(snd_pcm_substream_t * substream)
883 {
884         return snd_vt1724_pcm_pointer(substream, &vt1724_capture_spdif_reg);
885 }
886
887 static int snd_vt1724_playback_spdif_open(snd_pcm_substream_t *substream)
888 {
889         ice1712_t *ice = snd_pcm_substream_chip(substream);
890         snd_pcm_runtime_t *runtime = substream->runtime;
891
892         runtime->private_data = (void*)VT1724_PDMA4_START; /* irq/status/trigger bit */
893         ice->playback_con_substream = substream;
894         if (ice->force_pdma4) {
895                 runtime->hw = snd_vt1724_2ch_stereo;
896                 set_rate_constraints(ice, substream);
897         } else
898                 runtime->hw = snd_vt1724_spdif;
899         snd_pcm_set_sync(substream);
900         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
901         return 0;
902 }
903
904 static int snd_vt1724_playback_spdif_close(snd_pcm_substream_t * substream)
905 {
906         ice1712_t *ice = snd_pcm_substream_chip(substream);
907
908         if (PRO_RATE_RESET)
909                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
910         ice->playback_con_substream = NULL;
911
912         return 0;
913 }
914
915 static int snd_vt1724_capture_spdif_open(snd_pcm_substream_t *substream)
916 {
917         ice1712_t *ice = snd_pcm_substream_chip(substream);
918         snd_pcm_runtime_t *runtime = substream->runtime;
919
920         runtime->private_data = (void*)VT1724_RDMA1_START; /* irq/status/trigger bit */
921         ice->capture_con_substream = substream;
922         if (ice->force_rdma1) {
923                 runtime->hw = snd_vt1724_2ch_stereo;
924                 set_rate_constraints(ice, substream);
925         } else
926                 runtime->hw = snd_vt1724_spdif;
927         snd_pcm_set_sync(substream);
928         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
929         return 0;
930 }
931
932 static int snd_vt1724_capture_spdif_close(snd_pcm_substream_t * substream)
933 {
934         ice1712_t *ice = snd_pcm_substream_chip(substream);
935
936         if (PRO_RATE_RESET)
937                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
938         ice->capture_con_substream = NULL;
939
940         return 0;
941 }
942
943 static snd_pcm_ops_t snd_vt1724_playback_spdif_ops = {
944         .open =         snd_vt1724_playback_spdif_open,
945         .close =        snd_vt1724_playback_spdif_close,
946         .ioctl =        snd_pcm_lib_ioctl,
947         .hw_params =    snd_vt1724_pcm_hw_params,
948         .hw_free =      snd_vt1724_pcm_hw_free,
949         .prepare =      snd_vt1724_playback_spdif_prepare,
950         .trigger =      snd_vt1724_pcm_trigger,
951         .pointer =      snd_vt1724_playback_spdif_pointer,
952 };
953
954 static snd_pcm_ops_t snd_vt1724_capture_spdif_ops = {
955         .open =         snd_vt1724_capture_spdif_open,
956         .close =        snd_vt1724_capture_spdif_close,
957         .ioctl =        snd_pcm_lib_ioctl,
958         .hw_params =    snd_vt1724_pcm_hw_params,
959         .hw_free =      snd_vt1724_pcm_hw_free,
960         .prepare =      snd_vt1724_capture_spdif_prepare,
961         .trigger =      snd_vt1724_pcm_trigger,
962         .pointer =      snd_vt1724_capture_spdif_pointer,
963 };
964
965
966 static int __devinit snd_vt1724_pcm_spdif(ice1712_t * ice, int device)
967 {
968         char *name;
969         snd_pcm_t *pcm;
970         int play, capt;
971         int err;
972
973         if (ice->force_pdma4 ||
974             (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_OUT_INT)) {
975                 play = 1;
976                 ice->has_spdif = 1;
977         } else
978                 play = 0;
979         if (ice->force_rdma1 ||
980             (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_IN)) {
981                 capt = 1;
982                 ice->has_spdif = 1;
983         } else
984                 capt = 0;
985         if (! play && ! capt)
986                 return 0; /* no spdif device */
987
988         if (ice->force_pdma4 || ice->force_rdma1)
989                 name = "ICE1724 Secondary";
990         else
991                 name = "IEC1724 IEC958";
992         err = snd_pcm_new(ice->card, name, device, play, capt, &pcm);
993         if (err < 0)
994                 return err;
995
996         if (play)
997                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
998                                 &snd_vt1724_playback_spdif_ops);
999         if (capt)
1000                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1001                                 &snd_vt1724_capture_spdif_ops);
1002
1003         pcm->private_data = ice;
1004         pcm->info_flags = 0;
1005         strcpy(pcm->name, name);
1006
1007         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1008                                               snd_dma_pci_data(ice->pci), 64*1024, 64*1024);
1009
1010         ice->pcm = pcm;
1011
1012         return 0;
1013 }
1014
1015
1016 /*
1017  * independent surround PCMs
1018  */
1019
1020 const static struct vt1724_pcm_reg vt1724_playback_dma_regs[3] = {
1021         {
1022                 .addr = VT1724_MT_PDMA1_ADDR,
1023                 .size = VT1724_MT_PDMA1_SIZE,
1024                 .count = VT1724_MT_PDMA1_COUNT,
1025                 .start = VT1724_PDMA1_START,
1026                 .pause = VT1724_PDMA1_PAUSE,
1027         },
1028         {
1029                 .addr = VT1724_MT_PDMA2_ADDR,
1030                 .size = VT1724_MT_PDMA2_SIZE,
1031                 .count = VT1724_MT_PDMA2_COUNT,
1032                 .start = VT1724_PDMA2_START,
1033                 .pause = VT1724_PDMA2_PAUSE,
1034         },
1035         {
1036                 .addr = VT1724_MT_PDMA3_ADDR,
1037                 .size = VT1724_MT_PDMA3_SIZE,
1038                 .count = VT1724_MT_PDMA3_COUNT,
1039                 .start = VT1724_PDMA3_START,
1040                 .pause = VT1724_PDMA3_PAUSE,
1041         },
1042 };
1043
1044 static int snd_vt1724_playback_indep_prepare(snd_pcm_substream_t * substream)
1045 {
1046         ice1712_t *ice = snd_pcm_substream_chip(substream);
1047         unsigned char val;
1048
1049         spin_lock(&ice->reg_lock);
1050         val = 3 - substream->number;
1051         if (inb(ICEMT1724(ice, BURST)) < val)
1052                 outb(val, ICEMT1724(ice, BURST));
1053         spin_unlock(&ice->reg_lock);
1054         return snd_vt1724_pcm_prepare(substream, &vt1724_playback_dma_regs[substream->number]);
1055 }
1056
1057 static snd_pcm_uframes_t snd_vt1724_playback_indep_pointer(snd_pcm_substream_t * substream)
1058 {
1059         return snd_vt1724_pcm_pointer(substream, &vt1724_playback_dma_regs[substream->number]);
1060 }
1061
1062 static int snd_vt1724_playback_indep_open(snd_pcm_substream_t *substream)
1063 {
1064         ice1712_t *ice = snd_pcm_substream_chip(substream);
1065         snd_pcm_runtime_t *runtime = substream->runtime;
1066
1067         down(&ice->open_mutex);
1068         /* already used by PDMA0? */
1069         if (ice->pcm_reserved[substream->number]) {
1070                 up(&ice->open_mutex);
1071                 return -EBUSY; /* FIXME: should handle blocking mode properly */
1072         }
1073         up(&ice->open_mutex);
1074         runtime->private_data = (void*)(1 << (substream->number + 4));
1075         ice->playback_con_substream_ds[substream->number] = substream;
1076         runtime->hw = snd_vt1724_2ch_stereo;
1077         snd_pcm_set_sync(substream);
1078         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1079         set_rate_constraints(ice, substream);
1080         return 0;
1081 }
1082
1083 static int snd_vt1724_playback_indep_close(snd_pcm_substream_t * substream)
1084 {
1085         ice1712_t *ice = snd_pcm_substream_chip(substream);
1086
1087         if (PRO_RATE_RESET)
1088                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
1089         ice->playback_con_substream_ds[substream->number] = NULL;
1090         ice->pcm_reserved[substream->number] = NULL;
1091
1092         return 0;
1093 }
1094
1095 static snd_pcm_ops_t snd_vt1724_playback_indep_ops = {
1096         .open =         snd_vt1724_playback_indep_open,
1097         .close =        snd_vt1724_playback_indep_close,
1098         .ioctl =        snd_pcm_lib_ioctl,
1099         .hw_params =    snd_vt1724_pcm_hw_params,
1100         .hw_free =      snd_vt1724_pcm_hw_free,
1101         .prepare =      snd_vt1724_playback_indep_prepare,
1102         .trigger =      snd_vt1724_pcm_trigger,
1103         .pointer =      snd_vt1724_playback_indep_pointer,
1104 };
1105
1106
1107 static int __devinit snd_vt1724_pcm_indep(ice1712_t * ice, int device)
1108 {
1109         snd_pcm_t *pcm;
1110         int play;
1111         int err;
1112
1113         play = ice->num_total_dacs / 2 - 1;
1114         if (play <= 0)
1115                 return 0;
1116
1117         err = snd_pcm_new(ice->card, "ICE1724 Surrounds", device, play, 0, &pcm);
1118         if (err < 0)
1119                 return err;
1120
1121         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1122                         &snd_vt1724_playback_indep_ops);
1123
1124         pcm->private_data = ice;
1125         pcm->info_flags = 0;
1126         strcpy(pcm->name, "ICE1724 Surround PCM");
1127
1128         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1129                                               snd_dma_pci_data(ice->pci), 64*1024, 64*1024);
1130
1131         ice->pcm_ds = pcm;
1132
1133         return 0;
1134 }
1135
1136
1137 /*
1138  *  Mixer section
1139  */
1140
1141 static int __devinit snd_vt1724_ac97_mixer(ice1712_t * ice)
1142 {
1143         int err;
1144
1145         if (! (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S)) {
1146                 ac97_bus_t bus, *pbus;
1147                 ac97_t ac97;
1148                 /* cold reset */
1149                 outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
1150                 mdelay(5); /* FIXME */
1151                 outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
1152
1153                 memset(&bus, 0, sizeof(bus));
1154                 bus.write = snd_vt1724_ac97_write;
1155                 bus.read = snd_vt1724_ac97_read;
1156                 if ((err = snd_ac97_bus(ice->card, &bus, &pbus)) < 0)
1157                         return err;
1158                 memset(&ac97, 0, sizeof(ac97));
1159                 ac97.private_data = ice;
1160                 if ((err = snd_ac97_mixer(pbus, &ac97, &ice->ac97)) < 0)
1161                         printk(KERN_WARNING "ice1712: cannot initialize pro ac97, skipped\n");
1162                 else
1163                         return 0;
1164         }
1165         /* I2S mixer only */
1166         strcat(ice->card->mixername, "ICE1724 - multitrack");
1167         return 0;
1168 }
1169
1170 /*
1171  *
1172  */
1173
1174 static inline unsigned int eeprom_triple(ice1712_t *ice, int idx)
1175 {
1176         return (unsigned int)ice->eeprom.data[idx] | \
1177                 ((unsigned int)ice->eeprom.data[idx + 1] << 8) | \
1178                 ((unsigned int)ice->eeprom.data[idx + 2] << 16);
1179 }
1180
1181 static void snd_vt1724_proc_read(snd_info_entry_t *entry, 
1182                                  snd_info_buffer_t * buffer)
1183 {
1184         ice1712_t *ice = snd_magic_cast(ice1712_t, entry->private_data, return);
1185         unsigned int idx;
1186
1187         snd_iprintf(buffer, "%s\n\n", ice->card->longname);
1188         snd_iprintf(buffer, "EEPROM:\n");
1189
1190         snd_iprintf(buffer, "  Subvendor        : 0x%x\n", ice->eeprom.subvendor);
1191         snd_iprintf(buffer, "  Size             : %i bytes\n", ice->eeprom.size);
1192         snd_iprintf(buffer, "  Version          : %i\n", ice->eeprom.version);
1193         snd_iprintf(buffer, "  System Config    : 0x%x\n", ice->eeprom.data[ICE_EEP2_SYSCONF]);
1194         snd_iprintf(buffer, "  ACLink           : 0x%x\n", ice->eeprom.data[ICE_EEP2_ACLINK]);
1195         snd_iprintf(buffer, "  I2S              : 0x%x\n", ice->eeprom.data[ICE_EEP2_I2S]);
1196         snd_iprintf(buffer, "  S/PDIF           : 0x%x\n", ice->eeprom.data[ICE_EEP2_SPDIF]);
1197         snd_iprintf(buffer, "  GPIO direction   : 0x%x\n", ice->eeprom.gpiodir);
1198         snd_iprintf(buffer, "  GPIO mask        : 0x%x\n", ice->eeprom.gpiomask);
1199         snd_iprintf(buffer, "  GPIO state       : 0x%x\n", ice->eeprom.gpiostate);
1200         for (idx = 0x12; idx < ice->eeprom.size; idx++)
1201                 snd_iprintf(buffer, "  Extra #%02i        : 0x%x\n", idx, ice->eeprom.data[idx]);
1202
1203         snd_iprintf(buffer, "\nRegisters:\n");
1204
1205         snd_iprintf(buffer, "  PSDOUT03 : 0x%08x\n", (unsigned)inl(ICEMT1724(ice, ROUTE_PLAYBACK)));
1206         for (idx = 0x0; idx < 0x20 ; idx++)
1207                 snd_iprintf(buffer, "  CCS%02x    : 0x%02x\n", idx, inb(ice->port+idx));
1208         for (idx = 0x0; idx < 0x30 ; idx++)
1209                 snd_iprintf(buffer, "  MT%02x     : 0x%02x\n", idx, inb(ice->profi_port+idx));
1210 }
1211
1212 static void __devinit snd_vt1724_proc_init(ice1712_t * ice)
1213 {
1214         snd_info_entry_t *entry;
1215
1216         if (! snd_card_proc_new(ice->card, "ice1724", &entry))
1217                 snd_info_set_text_ops(entry, ice, 1024, snd_vt1724_proc_read);
1218 }
1219
1220 /*
1221  *
1222  */
1223
1224 static int snd_vt1724_eeprom_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1225 {
1226         uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1227         uinfo->count = sizeof(ice1712_eeprom_t);
1228         return 0;
1229 }
1230
1231 static int snd_vt1724_eeprom_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1232 {
1233         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1234         
1235         memcpy(ucontrol->value.bytes.data, &ice->eeprom, sizeof(ice->eeprom));
1236         return 0;
1237 }
1238
1239 static snd_kcontrol_new_t snd_vt1724_eeprom __devinitdata = {
1240         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1241         .name = "ICE1724 EEPROM",
1242         .access = SNDRV_CTL_ELEM_ACCESS_READ,
1243         .info = snd_vt1724_eeprom_info,
1244         .get = snd_vt1724_eeprom_get
1245 };
1246
1247 /*
1248  */
1249 static int snd_vt1724_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1250 {
1251         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1252         uinfo->count = 1;
1253         return 0;
1254 }
1255
1256 static unsigned int encode_spdif_bits(snd_aes_iec958_t *diga)
1257 {
1258         unsigned int val;
1259
1260         val = diga->status[0] & 0x03; /* professional, non-audio */
1261         if (val & 0x01) {
1262                 /* professional */
1263                 if ((diga->status[0] & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
1264                         val |= 1U << 3;
1265                 switch (diga->status[0] & IEC958_AES0_PRO_FS) {
1266                 case IEC958_AES0_PRO_FS_44100:
1267                         break;
1268                 case IEC958_AES0_PRO_FS_32000:
1269                         val |= 3U << 12;
1270                         break;
1271                 default:
1272                         val |= 2U << 12;
1273                         break;
1274                 }
1275         } else {
1276                 /* consumer */
1277                 val |= diga->status[0] & 0x04; /* copyright */
1278                 if ((diga->status[0] & IEC958_AES0_CON_EMPHASIS)== IEC958_AES0_CON_EMPHASIS_5015)
1279                         val |= 1U << 3;
1280                 val |= (unsigned int)(diga->status[1] & 0x3f) << 4; /* category */
1281                 val |= (unsigned int)(diga->status[3] & IEC958_AES3_CON_FS) << 12; /* fs */
1282         }
1283         return val;
1284 }
1285
1286 static void decode_spdif_bits(snd_aes_iec958_t *diga, unsigned int val)
1287 {
1288         memset(diga->status, 0, sizeof(diga->status));
1289         diga->status[0] = val & 0x03; /* professional, non-audio */
1290         if (val & 0x01) {
1291                 /* professional */
1292                 if (val & (1U << 3))
1293                         diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015;
1294                 switch ((val >> 12) & 0x7) {
1295                 case 0:
1296                         break;
1297                 case 2:
1298                         diga->status[0] |= IEC958_AES0_PRO_FS_32000;
1299                         break;
1300                 default:
1301                         diga->status[0] |= IEC958_AES0_PRO_FS_48000;
1302                         break;
1303                 }
1304         } else {
1305                 /* consumer */
1306                 diga->status[0] |= val & (1U << 2); /* copyright */
1307                 if (val & (1U << 3))
1308                         diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015;
1309                 diga->status[1] |= (val >> 4) & 0x3f; /* category */
1310                 diga->status[3] |= (val >> 12) & 0x07; /* fs */
1311         }
1312 }
1313
1314 static int snd_vt1724_spdif_default_get(snd_kcontrol_t * kcontrol,
1315                                         snd_ctl_elem_value_t * ucontrol)
1316 {
1317         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1318         unsigned int val;
1319         val = inw(ICEMT1724(ice, SPDIF_CTRL));
1320         decode_spdif_bits(&ucontrol->value.iec958, val);
1321         return 0;
1322 }
1323
1324 static int snd_vt1724_spdif_default_put(snd_kcontrol_t * kcontrol,
1325                                          snd_ctl_elem_value_t * ucontrol)
1326 {
1327         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1328         unsigned int val, old;
1329         unsigned long flags;
1330
1331         val = encode_spdif_bits(&ucontrol->value.iec958);
1332         spin_lock_irqsave(&ice->reg_lock, flags);
1333         old = inw(ICEMT1724(ice, SPDIF_CTRL));
1334         if (val != old) {
1335                 unsigned char cbit, disabled;
1336                 cbit = inb(ICEREG1724(ice, SPDIF_CFG));
1337                 disabled = cbit & ~VT1724_CFG_SPDIF_OUT_EN;
1338                 if (cbit != disabled)
1339                         outb(disabled, ICEREG1724(ice, SPDIF_CFG));
1340                 outw(val, ICEMT1724(ice, SPDIF_CTRL));
1341                 if (cbit != disabled)
1342                         outb(cbit, ICEREG1724(ice, SPDIF_CFG));
1343         }
1344         spin_unlock_irqrestore(&ice->reg_lock, flags);
1345         return (val != old);
1346 }
1347
1348 static snd_kcontrol_new_t snd_vt1724_spdif_default __devinitdata =
1349 {
1350         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1351         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1352         .info =         snd_vt1724_spdif_info,
1353         .get =          snd_vt1724_spdif_default_get,
1354         .put =          snd_vt1724_spdif_default_put
1355 };
1356
1357 static int snd_vt1724_spdif_maskc_get(snd_kcontrol_t * kcontrol,
1358                                        snd_ctl_elem_value_t * ucontrol)
1359 {
1360         ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1361                                                      IEC958_AES0_PROFESSIONAL |
1362                                                      IEC958_AES0_CON_NOT_COPYRIGHT |
1363                                                      IEC958_AES0_CON_EMPHASIS;
1364         ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL |
1365                                                      IEC958_AES1_CON_CATEGORY;
1366         ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
1367         return 0;
1368 }
1369
1370 static int snd_vt1724_spdif_maskp_get(snd_kcontrol_t * kcontrol,
1371                                        snd_ctl_elem_value_t * ucontrol)
1372 {
1373         ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1374                                                      IEC958_AES0_PROFESSIONAL |
1375                                                      IEC958_AES0_PRO_FS |
1376                                                      IEC958_AES0_PRO_EMPHASIS;
1377         return 0;
1378 }
1379
1380 static snd_kcontrol_new_t snd_vt1724_spdif_maskc __devinitdata =
1381 {
1382         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1383         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1384         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1385         .info =         snd_vt1724_spdif_info,
1386         .get =          snd_vt1724_spdif_maskc_get,
1387 };
1388
1389 static snd_kcontrol_new_t snd_vt1724_spdif_maskp __devinitdata =
1390 {
1391         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1392         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1393         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1394         .info =         snd_vt1724_spdif_info,
1395         .get =          snd_vt1724_spdif_maskp_get,
1396 };
1397
1398 static int snd_vt1724_spdif_sw_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1399 {
1400         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1401         uinfo->count = 1;
1402         uinfo->value.integer.min = 0;
1403         uinfo->value.integer.max = 1;
1404         return 0;
1405 }
1406
1407 static int snd_vt1724_spdif_sw_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1408 {
1409         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1410         ucontrol->value.integer.value[0] = inb(ICEREG1724(ice, SPDIF_CFG)) & VT1724_CFG_SPDIF_OUT_EN ? 1 : 0;
1411         return 0;
1412 }
1413
1414 static int snd_vt1724_spdif_sw_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1415 {
1416         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1417         unsigned char old, val;
1418         unsigned long flags;
1419         spin_lock_irqsave(&ice->reg_lock, flags);
1420         old = val = inb(ICEREG1724(ice, SPDIF_CFG));
1421         val &= ~VT1724_CFG_SPDIF_OUT_EN;
1422         if (ucontrol->value.integer.value[0])
1423                 val |= VT1724_CFG_SPDIF_OUT_EN;
1424         if (old != val)
1425                 outb(val, ICEREG1724(ice, SPDIF_CFG));
1426         spin_unlock_irqrestore(&ice->reg_lock, flags);
1427         return old != val;
1428 }
1429
1430 static snd_kcontrol_new_t snd_vt1724_spdif_switch __devinitdata =
1431 {
1432         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1433         /* FIXME: the following conflict with IEC958 Playback Route */
1434         // .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1435         .name =         "IEC958 Output Switch",
1436         .info =         snd_vt1724_spdif_sw_info,
1437         .get =          snd_vt1724_spdif_sw_get,
1438         .put =          snd_vt1724_spdif_sw_put
1439 };
1440
1441
1442 #if 0 /* NOT USED YET */
1443 /*
1444  * GPIO access from extern
1445  */
1446
1447 int snd_vt1724_gpio_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1448 {
1449         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1450         uinfo->count = 1;
1451         uinfo->value.integer.min = 0;
1452         uinfo->value.integer.max = 1;
1453         return 0;
1454 }
1455
1456 int snd_vt1724_gpio_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1457 {
1458         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1459         int shift = kcontrol->private_value & 0xff;
1460         int invert = (kcontrol->private_value & (1<<24)) ? 1 : 0;
1461         
1462         snd_ice1712_save_gpio_status(ice);
1463         ucontrol->value.integer.value[0] = (snd_ice1712_gpio_read(ice) & (1 << shift) ? 1 : 0) ^ invert;
1464         snd_ice1712_restore_gpio_status(ice);
1465         return 0;
1466 }
1467
1468 int snd_ice1712_gpio_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1469 {
1470         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1471         int shift = kcontrol->private_value & 0xff;
1472         int invert = (kcontrol->private_value & (1<<24)) ? mask : 0;
1473         unsigned int val, nval;
1474
1475         if (kcontrol->private_value & (1 << 31))
1476                 return -EPERM;
1477         nval = (ucontrol->value.integer.value[0] ? (1 << shift) : 0) ^ invert;
1478         snd_ice1712_save_gpio_status(ice);
1479         val = snd_ice1712_gpio_read(ice);
1480         nval |= val & ~(1 << shift);
1481         if (val != nval)
1482                 snd_ice1712_gpio_write(ice, nval);
1483         snd_ice1712_restore_gpio_status(ice);
1484         return val != nval;
1485 }
1486 #endif /* NOT USED YET */
1487
1488 /*
1489  *  rate
1490  */
1491 static int snd_vt1724_pro_internal_clock_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1492 {
1493         static char *texts[] = {
1494                 "8000",         /* 0: 6 */
1495                 "9600",         /* 1: 3 */
1496                 "11025",        /* 2: 10 */
1497                 "12000",        /* 3: 2 */
1498                 "16000",        /* 4: 5 */
1499                 "22050",        /* 5: 9 */
1500                 "24000",        /* 6: 1 */
1501                 "32000",        /* 7: 4 */
1502                 "44100",        /* 8: 8 */
1503                 "48000",        /* 9: 0 */
1504                 "64000",        /* 10: 15 */
1505                 "88200",        /* 11: 11 */
1506                 "96000",        /* 12: 7 */
1507                 "176400",       /* 13: 12 */
1508                 "192000",       /* 14: 14 */
1509                 "IEC958 Input", /* 15: -- */
1510         };
1511         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1512         uinfo->count = 1;
1513         uinfo->value.enumerated.items = 16;
1514         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1515                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1516         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1517         return 0;
1518 }
1519
1520 static int snd_vt1724_pro_internal_clock_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1521 {
1522         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1523         static unsigned char xlate[16] = {
1524                 9, 6, 3, 1, 7, 4, 0, 12, 8, 5, 2, 11, 13, 255, 14, 10
1525         };
1526         unsigned char val;
1527         
1528         spin_lock_irq(&ice->reg_lock);
1529         if (is_spdif_master(ice)) {
1530                 ucontrol->value.enumerated.item[0] = 15;
1531         } else {
1532                 val = xlate[inb(ICEMT1724(ice, RATE)) & 15];
1533                 if (val == 255) {
1534                         snd_BUG();
1535                         val = 0;
1536                 }
1537                 ucontrol->value.enumerated.item[0] = val;
1538         }
1539         spin_unlock_irq(&ice->reg_lock);
1540         return 0;
1541 }
1542
1543 static int snd_vt1724_pro_internal_clock_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1544 {
1545         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1546         unsigned char oval;
1547         int change = 0;
1548
1549         spin_lock_irq(&ice->reg_lock);
1550         oval = inb(ICEMT1724(ice, RATE));
1551         if (ucontrol->value.enumerated.item[0] == 15) {
1552                 outb(oval | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
1553         } else {
1554                 PRO_RATE_DEFAULT = rates[ucontrol->value.integer.value[0] % 15];
1555                 spin_unlock_irq(&ice->reg_lock);
1556                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 1);
1557                 spin_lock_irq(&ice->reg_lock);
1558         }
1559         change = inb(ICEMT1724(ice, RATE)) != oval;
1560         spin_unlock_irq(&ice->reg_lock);
1561
1562         if ((oval & VT1724_SPDIF_MASTER) != (inb(ICEMT1724(ice, RATE)) & VT1724_SPDIF_MASTER)) {
1563                 /* notify akm chips as well */
1564                 if (is_spdif_master(ice)) {
1565                         unsigned int i;
1566                         for (i = 0; i < ice->akm_codecs; i++) {
1567                                 if (ice->akm[i].ops.set_rate_val)
1568                                         ice->akm[i].ops.set_rate_val(&ice->akm[i], 0);
1569                         }
1570                 }
1571         }
1572         return change;
1573 }
1574
1575 static snd_kcontrol_new_t snd_vt1724_pro_internal_clock __devinitdata = {
1576         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1577         .name = "Multi Track Internal Clock",
1578         .info = snd_vt1724_pro_internal_clock_info,
1579         .get = snd_vt1724_pro_internal_clock_get,
1580         .put = snd_vt1724_pro_internal_clock_put
1581 };
1582
1583 static int snd_vt1724_pro_rate_locking_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1584 {
1585         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1586         uinfo->count = 1;
1587         uinfo->value.integer.min = 0;
1588         uinfo->value.integer.max = 1;
1589         return 0;
1590 }
1591
1592 static int snd_vt1724_pro_rate_locking_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1593 {
1594         ucontrol->value.integer.value[0] = PRO_RATE_LOCKED;
1595         return 0;
1596 }
1597
1598 static int snd_vt1724_pro_rate_locking_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1599 {
1600         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1601         int change = 0, nval;
1602
1603         nval = ucontrol->value.integer.value[0] ? 1 : 0;
1604         spin_lock_irq(&ice->reg_lock);
1605         change = PRO_RATE_LOCKED != nval;
1606         PRO_RATE_LOCKED = nval;
1607         spin_unlock_irq(&ice->reg_lock);
1608         return change;
1609 }
1610
1611 static snd_kcontrol_new_t snd_vt1724_pro_rate_locking __devinitdata = {
1612         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1613         .name = "Multi Track Rate Locking",
1614         .info = snd_vt1724_pro_rate_locking_info,
1615         .get = snd_vt1724_pro_rate_locking_get,
1616         .put = snd_vt1724_pro_rate_locking_put
1617 };
1618
1619 static int snd_vt1724_pro_rate_reset_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1620 {
1621         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1622         uinfo->count = 1;
1623         uinfo->value.integer.min = 0;
1624         uinfo->value.integer.max = 1;
1625         return 0;
1626 }
1627
1628 static int snd_vt1724_pro_rate_reset_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1629 {
1630         ucontrol->value.integer.value[0] = PRO_RATE_RESET ? 1 : 0;
1631         return 0;
1632 }
1633
1634 static int snd_vt1724_pro_rate_reset_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1635 {
1636         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1637         int change = 0, nval;
1638
1639         nval = ucontrol->value.integer.value[0] ? 1 : 0;
1640         spin_lock_irq(&ice->reg_lock);
1641         change = PRO_RATE_RESET != nval;
1642         PRO_RATE_RESET = nval;
1643         spin_unlock_irq(&ice->reg_lock);
1644         return change;
1645 }
1646
1647 static snd_kcontrol_new_t snd_vt1724_pro_rate_reset __devinitdata = {
1648         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1649         .name = "Multi Track Rate Reset",
1650         .info = snd_vt1724_pro_rate_reset_info,
1651         .get = snd_vt1724_pro_rate_reset_get,
1652         .put = snd_vt1724_pro_rate_reset_put
1653 };
1654
1655
1656 /*
1657  * routing
1658  */
1659 static int snd_vt1724_pro_route_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1660 {
1661         static char *texts[] = {
1662                 "PCM Out", /* 0 */
1663                 "H/W In 0", "H/W In 1", /* 1-2 */
1664                 "IEC958 In L", "IEC958 In R", /* 3-4 */
1665         };
1666         
1667         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1668         uinfo->count = 1;
1669         uinfo->value.enumerated.items = 5;
1670         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1671                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1672         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1673         return 0;
1674 }
1675
1676 static inline int analog_route_shift(int idx)
1677 {
1678         return (idx % 2) * 12 + ((idx / 2) * 3) + 8;
1679 }
1680
1681 static inline int digital_route_shift(int idx)
1682 {
1683         return idx * 3;
1684 }
1685
1686 static int get_route_val(ice1712_t *ice, int shift)
1687 {
1688         unsigned long val;
1689         unsigned char eitem;
1690         static unsigned char xlate[8] = {
1691                 0, 255, 1, 2, 255, 255, 3, 4,
1692         };
1693
1694         val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1695         val >>= shift;
1696         val &= 7;       //we now have 3 bits per output
1697         eitem = xlate[val];
1698         if (eitem == 255) {
1699                 snd_BUG();
1700                 return 0;
1701         }
1702         return eitem;
1703 }
1704
1705 static int put_route_val(ice1712_t *ice, unsigned int val, int shift)
1706 {
1707         unsigned int old_val, nval;
1708         int change;
1709         static unsigned char xroute[8] = {
1710                 0, /* PCM */
1711                 2, /* PSDIN0 Left */
1712                 3, /* PSDIN0 Right */
1713                 6, /* SPDIN Left */
1714                 7, /* SPDIN Right */
1715         };
1716
1717         nval = xroute[val % 5];
1718         val = old_val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1719         val &= ~(0x07 << shift);
1720         val |= nval << shift;
1721         change = val != old_val;
1722         if (change)
1723                 outl(val, ICEMT1724(ice, ROUTE_PLAYBACK));
1724         return change;
1725 }
1726
1727 static int snd_vt1724_pro_route_analog_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
1728 {
1729         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1730         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1731         ucontrol->value.enumerated.item[0] = get_route_val(ice, analog_route_shift(idx));
1732         return 0;
1733 }
1734
1735 static int snd_vt1724_pro_route_analog_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
1736 {
1737         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1738         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1739         return put_route_val(ice, ucontrol->value.enumerated.item[0],
1740                              analog_route_shift(idx));
1741 }
1742
1743 static int snd_vt1724_pro_route_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
1744 {
1745         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1746         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1747         ucontrol->value.enumerated.item[0] = get_route_val(ice, digital_route_shift(idx));
1748         return 0;
1749 }
1750
1751 static int snd_vt1724_pro_route_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
1752 {
1753         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1754         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1755         return put_route_val(ice, ucontrol->value.enumerated.item[0],
1756                              digital_route_shift(idx));
1757 }
1758
1759 static snd_kcontrol_new_t snd_vt1724_mixer_pro_analog_route __devinitdata = {
1760         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1761         .name = "H/W Playback Route",
1762         .info = snd_vt1724_pro_route_info,
1763         .get = snd_vt1724_pro_route_analog_get,
1764         .put = snd_vt1724_pro_route_analog_put,
1765 };
1766
1767 static snd_kcontrol_new_t snd_vt1724_mixer_pro_spdif_route __devinitdata = {
1768         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1769         .name = "IEC958 Playback Route",
1770         .info = snd_vt1724_pro_route_info,
1771         .get = snd_vt1724_pro_route_spdif_get,
1772         .put = snd_vt1724_pro_route_spdif_put,
1773         .count = 2,
1774 };
1775
1776
1777 static int snd_vt1724_pro_peak_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1778 {
1779         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1780         uinfo->count = 22; /* FIXME: for compatibility with ice1712... */
1781         uinfo->value.integer.min = 0;
1782         uinfo->value.integer.max = 255;
1783         return 0;
1784 }
1785
1786 static int snd_vt1724_pro_peak_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1787 {
1788         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1789         int idx;
1790         
1791         spin_lock_irq(&ice->reg_lock);
1792         for (idx = 0; idx < 22; idx++) {
1793                 outb(idx, ICEMT1724(ice, MONITOR_PEAKINDEX));
1794                 ucontrol->value.integer.value[idx] = inb(ICEMT1724(ice, MONITOR_PEAKDATA));
1795         }
1796         spin_unlock_irq(&ice->reg_lock);
1797         return 0;
1798 }
1799
1800 static snd_kcontrol_new_t snd_vt1724_mixer_pro_peak __devinitdata = {
1801         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1802         .name = "Multi Track Peak",
1803         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1804         .info = snd_vt1724_pro_peak_info,
1805         .get = snd_vt1724_pro_peak_get
1806 };
1807
1808 /*
1809  *
1810  */
1811
1812 static struct snd_ice1712_card_info no_matched __devinitdata;
1813
1814 static struct snd_ice1712_card_info *card_tables[] __devinitdata = {
1815         snd_vt1724_revo_cards,
1816         snd_vt1724_amp_cards, 
1817         snd_vt1724_aureon_cards,
1818         0,
1819 };
1820
1821
1822 /*
1823  */
1824
1825 unsigned char snd_vt1724_read_i2c(ice1712_t *ice, unsigned char dev, unsigned char addr)
1826 {
1827         long t = 0x10000;
1828
1829         outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
1830         outb(dev & ~VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
1831         while (t-- > 0 && (inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_BUSY)) ;
1832         return inb(ICEREG1724(ice, I2C_DATA));
1833 }
1834
1835 void snd_vt1724_write_i2c(ice1712_t *ice, unsigned char dev, unsigned char addr, unsigned char data)
1836 {
1837         long t = 0x10000;
1838
1839         outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
1840         outb(data, ICEREG1724(ice, I2C_DATA));
1841         outb(dev | VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
1842         while (t-- > 0 && (inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_BUSY)) ;
1843 }
1844
1845 static int __devinit snd_vt1724_read_eeprom(ice1712_t *ice, const char *modelname)
1846 {
1847         const int dev = 0xa0;           /* EEPROM device address */
1848         unsigned int i, size;
1849         struct snd_ice1712_card_info **tbl, *c;
1850
1851         if (! modelname || ! *modelname) {
1852                 ice->eeprom.subvendor = 0;
1853                 if ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_EEPROM) != 0)
1854                         ice->eeprom.subvendor = (snd_vt1724_read_i2c(ice, dev, 0x00) << 0) |
1855                                 (snd_vt1724_read_i2c(ice, dev, 0x01) << 8) | 
1856                                 (snd_vt1724_read_i2c(ice, dev, 0x02) << 16) | 
1857                                 (snd_vt1724_read_i2c(ice, dev, 0x03) << 24);
1858                 if (ice->eeprom.subvendor == 0 || ice->eeprom.subvendor == (unsigned int)-1) {
1859                         /* invalid subvendor from EEPROM, try the PCI subststem ID instead */
1860                         u16 vendor, device;
1861                         pci_read_config_word(ice->pci, PCI_SUBSYSTEM_VENDOR_ID, &vendor);
1862                         pci_read_config_word(ice->pci, PCI_SUBSYSTEM_ID, &device);
1863                         ice->eeprom.subvendor = ((unsigned int)swab16(vendor) << 16) | swab16(device);
1864                         if (ice->eeprom.subvendor == 0 || ice->eeprom.subvendor == (unsigned int)-1) {
1865                                 printk(KERN_ERR "ice1724: No valid ID is found\n");
1866                                 return -ENXIO;
1867                         }
1868                 }
1869         }
1870         for (tbl = card_tables; *tbl; tbl++) {
1871                 for (c = *tbl; c->subvendor; c++) {
1872                         if (modelname && c->model && ! strcmp(modelname, c->model)) {
1873                                 printk(KERN_INFO "ice1724: Using board model %s\n", c->name);
1874                                 ice->eeprom.subvendor = c->subvendor;
1875                         } else if (c->subvendor != ice->eeprom.subvendor)
1876                                 continue;
1877                         if (! c->eeprom_size || ! c->eeprom_data)
1878                                 goto found;
1879                         /* if the EEPROM is given by the driver, use it */
1880                         snd_printdd("using the defined eeprom..\n");
1881                         ice->eeprom.version = 2;
1882                         ice->eeprom.size = c->eeprom_size + 6;
1883                         memcpy(ice->eeprom.data, c->eeprom_data, c->eeprom_size);
1884                         goto read_skipped;
1885                 }
1886         }
1887         printk(KERN_WARNING "ice1724: No matching model found for ID 0x%x\n", ice->eeprom.subvendor);
1888
1889  found:
1890         ice->eeprom.size = snd_vt1724_read_i2c(ice, dev, 0x04);
1891         if (ice->eeprom.size < 6)
1892                 ice->eeprom.size = 32;
1893         else if (ice->eeprom.size > 32) {
1894                 printk(KERN_ERR "ice1724: Invalid EEPROM (size = %i)\n", ice->eeprom.size);
1895                 return -EIO;
1896         }
1897         ice->eeprom.version = snd_vt1724_read_i2c(ice, dev, 0x05);
1898         if (ice->eeprom.version != 2)
1899                 printk(KERN_WARNING "ice1724: Invalid EEPROM version %i\n", ice->eeprom.version);
1900         size = ice->eeprom.size - 6;
1901         for (i = 0; i < size; i++)
1902                 ice->eeprom.data[i] = snd_vt1724_read_i2c(ice, dev, i + 6);
1903
1904  read_skipped:
1905         ice->eeprom.gpiomask = eeprom_triple(ice, ICE_EEP2_GPIO_MASK);
1906         ice->eeprom.gpiostate = eeprom_triple(ice, ICE_EEP2_GPIO_STATE);
1907         ice->eeprom.gpiodir = eeprom_triple(ice, ICE_EEP2_GPIO_DIR);
1908
1909         return 0;
1910 }
1911
1912
1913
1914 static int __devinit snd_vt1724_chip_init(ice1712_t *ice)
1915 {
1916         outb(VT1724_RESET , ICEREG1724(ice, CONTROL));
1917         udelay(200);
1918         outb(0, ICEREG1724(ice, CONTROL));
1919         udelay(200);
1920         outb(ice->eeprom.data[ICE_EEP2_SYSCONF], ICEREG1724(ice, SYS_CFG));
1921         outb(ice->eeprom.data[ICE_EEP2_ACLINK], ICEREG1724(ice, AC97_CFG));
1922         outb(ice->eeprom.data[ICE_EEP2_I2S], ICEREG1724(ice, I2S_FEATURES));
1923         outb(ice->eeprom.data[ICE_EEP2_SPDIF], ICEREG1724(ice, SPDIF_CFG));
1924
1925         ice->gpio.write_mask = ice->eeprom.gpiomask;
1926         ice->gpio.direction = ice->eeprom.gpiodir;
1927         snd_vt1724_set_gpio_mask(ice, ice->eeprom.gpiomask);
1928         snd_vt1724_set_gpio_dir(ice, ice->eeprom.gpiodir);
1929         snd_vt1724_set_gpio_data(ice, ice->eeprom.gpiostate);
1930
1931         outb(0, ICEREG1724(ice, POWERDOWN));
1932
1933         /* read back to check the availability of SPDIF out */
1934         ice->eeprom.data[ICE_EEP2_SPDIF] = inb(ICEREG1724(ice, SPDIF_CFG));
1935
1936         return 0;
1937 }
1938
1939 static int __devinit snd_vt1724_spdif_build_controls(ice1712_t *ice)
1940 {
1941         int err;
1942         snd_kcontrol_t *kctl;
1943
1944         snd_assert(ice->pcm != NULL, return -EIO);
1945
1946         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_spdif_route, ice));
1947         if (err < 0)
1948                 return err;
1949
1950         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_spdif_switch, ice));
1951         if (err < 0)
1952                 return err;
1953
1954         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_default, ice));
1955         if (err < 0)
1956                 return err;
1957         kctl->id.device = ice->pcm->device;
1958         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskc, ice));
1959         if (err < 0)
1960                 return err;
1961         kctl->id.device = ice->pcm->device;
1962         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskp, ice));
1963         if (err < 0)
1964                 return err;
1965         kctl->id.device = ice->pcm->device;
1966 #if 0 /* use default only */
1967         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_stream, ice));
1968         if (err < 0)
1969                 return err;
1970         kctl->id.device = ice->pcm->device;
1971         ice->spdif.stream_ctl = kctl;
1972 #endif
1973         return 0;
1974 }
1975
1976
1977 static int __devinit snd_vt1724_build_controls(ice1712_t *ice)
1978 {
1979         int err;
1980
1981         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_eeprom, ice));
1982         if (err < 0)
1983                 return err;
1984         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_internal_clock, ice));
1985         if (err < 0)
1986                 return err;
1987
1988         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_locking, ice));
1989         if (err < 0)
1990                 return err;
1991         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_reset, ice));
1992         if (err < 0)
1993                 return err;
1994
1995         if (ice->num_total_dacs > 0) {
1996                 snd_kcontrol_new_t tmp = snd_vt1724_mixer_pro_analog_route;
1997                 if (ice->vt1720)
1998                         tmp.count = 2;
1999                 else
2000                         tmp.count = ice->num_total_dacs;
2001                 err = snd_ctl_add(ice->card, snd_ctl_new1(&tmp, ice));
2002                 if (err < 0)
2003                         return err;
2004         }
2005
2006         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_peak, ice));
2007         if (err < 0)
2008                 return err;
2009
2010         return 0;
2011 }
2012
2013 static int snd_vt1724_free(ice1712_t *ice)
2014 {
2015         if (ice->res_port == NULL)
2016                 goto __hw_end;
2017         /* mask all interrupts */
2018         outb(0xff, ICEMT1724(ice, DMA_INT_MASK));
2019         outb(0xff, ICEREG1724(ice, IRQMASK));
2020         /* --- */
2021       __hw_end:
2022         if (ice->irq >= 0) {
2023                 synchronize_irq(ice->irq);
2024                 free_irq(ice->irq, (void *) ice);
2025         }
2026         if (ice->res_port) {
2027                 release_resource(ice->res_port);
2028                 kfree_nocheck(ice->res_port);
2029         }
2030         if (ice->res_profi_port) {
2031                 release_resource(ice->res_profi_port);
2032                 kfree_nocheck(ice->res_profi_port);
2033         }
2034         snd_ice1712_akm4xxx_free(ice);
2035         snd_magic_kfree(ice);
2036         return 0;
2037 }
2038
2039 static int snd_vt1724_dev_free(snd_device_t *device)
2040 {
2041         ice1712_t *ice = snd_magic_cast(ice1712_t, device->device_data, return -ENXIO);
2042         return snd_vt1724_free(ice);
2043 }
2044
2045 static int __devinit snd_vt1724_create(snd_card_t * card,
2046                                        struct pci_dev *pci,
2047                                        const char *modelname,
2048                                        ice1712_t ** r_ice1712)
2049 {
2050         ice1712_t *ice;
2051         int err;
2052         unsigned char mask;
2053         static snd_device_ops_t ops = {
2054                 .dev_free =     snd_vt1724_dev_free,
2055         };
2056
2057         *r_ice1712 = NULL;
2058
2059         /* enable PCI device */
2060         if ((err = pci_enable_device(pci)) < 0)
2061                 return err;
2062
2063         ice = snd_magic_kcalloc(ice1712_t, 0, GFP_KERNEL);
2064         if (ice == NULL)
2065                 return -ENOMEM;
2066         ice->vt1724 = 1;
2067         spin_lock_init(&ice->reg_lock);
2068         init_MUTEX(&ice->gpio_mutex);
2069         init_MUTEX(&ice->open_mutex);
2070         ice->gpio.set_mask = snd_vt1724_set_gpio_mask;
2071         ice->gpio.set_dir = snd_vt1724_set_gpio_dir;
2072         ice->gpio.set_data = snd_vt1724_set_gpio_data;
2073         ice->gpio.get_data = snd_vt1724_get_gpio_data;
2074         ice->card = card;
2075         ice->pci = pci;
2076         ice->irq = -1;
2077         ice->port = pci_resource_start(pci, 0);
2078         ice->profi_port = pci_resource_start(pci, 1);
2079         pci_set_master(pci);
2080         snd_vt1724_proc_init(ice);
2081         synchronize_irq(pci->irq);
2082
2083         if ((ice->res_port = request_region(ice->port, 32, "ICE1724 - Controller")) == NULL) {
2084                 snd_printk("unable to grab ports 0x%lx-0x%lx\n", ice->port, ice->port + 32 - 1);
2085                 snd_vt1724_free(ice);
2086                 return -EIO;
2087         }
2088
2089         if ((ice->res_profi_port = request_region(ice->profi_port, 128, "ICE1724 - Professional")) == NULL) {
2090                 snd_printk("unable to grab ports 0x%lx-0x%lx\n", ice->profi_port, ice->profi_port + 16 - 1);
2091                 snd_vt1724_free(ice);
2092                 return -EIO;
2093         }
2094                 
2095         if (request_irq(pci->irq, snd_vt1724_interrupt, SA_INTERRUPT|SA_SHIRQ, "ICE1724", (void *) ice)) {
2096                 snd_printk("unable to grab IRQ %d\n", pci->irq);
2097                 snd_vt1724_free(ice);
2098                 return -EIO;
2099         }
2100
2101         ice->irq = pci->irq;
2102
2103         if (snd_vt1724_read_eeprom(ice, modelname) < 0) {
2104                 snd_vt1724_free(ice);
2105                 return -EIO;
2106         }
2107         if (snd_vt1724_chip_init(ice) < 0) {
2108                 snd_vt1724_free(ice);
2109                 return -EIO;
2110         }
2111
2112         /* unmask used interrupts */
2113         if (! (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401))
2114                 mask = VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX;
2115         else
2116                 mask = 0;
2117         outb(mask, ICEREG1724(ice, IRQMASK));
2118         /* don't handle FIFO overrun/underruns (just yet), since they cause machine lockups */
2119         outb(VT1724_MULTI_FIFO_ERR, ICEMT1724(ice, DMA_INT_MASK));
2120
2121         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ice, &ops)) < 0) {
2122                 snd_vt1724_free(ice);
2123                 return err;
2124         }
2125
2126         snd_card_set_dev(card, &pci->dev);
2127
2128         *r_ice1712 = ice;
2129         return 0;
2130 }
2131
2132
2133 /*
2134  *
2135  * Registration
2136  *
2137  */
2138
2139 static int __devinit snd_vt1724_probe(struct pci_dev *pci,
2140                                       const struct pci_device_id *pci_id)
2141 {
2142         static int dev;
2143         snd_card_t *card;
2144         ice1712_t *ice;
2145         int pcm_dev = 0, err;
2146         struct snd_ice1712_card_info **tbl, *c;
2147
2148         if (dev >= SNDRV_CARDS)
2149                 return -ENODEV;
2150         if (!enable[dev]) {
2151                 dev++;
2152                 return -ENOENT;
2153         }
2154
2155         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2156         if (card == NULL)
2157                 return -ENOMEM;
2158
2159         strcpy(card->driver, "ICE1724");
2160         strcpy(card->shortname, "ICEnsemble ICE1724");
2161         
2162         if ((err = snd_vt1724_create(card, pci, model[dev], &ice)) < 0) {
2163                 snd_card_free(card);
2164                 return err;
2165         }
2166
2167         for (tbl = card_tables; *tbl; tbl++) {
2168                 for (c = *tbl; c->subvendor; c++) {
2169                         if (c->subvendor == ice->eeprom.subvendor) {
2170                                 strcpy(card->shortname, c->name);
2171                                 if (c->driver) /* specific driver? */
2172                                         strcpy(card->driver, c->driver);
2173                                 if (c->chip_init) {
2174                                         if ((err = c->chip_init(ice)) < 0) {
2175                                                 snd_card_free(card);
2176                                                 return err;
2177                                         }
2178                                 }
2179                                 goto __found;
2180                         }
2181                 }
2182         }
2183         c = &no_matched;
2184  __found:
2185
2186         if ((err = snd_vt1724_pcm_profi(ice, pcm_dev++)) < 0) {
2187                 snd_card_free(card);
2188                 return err;
2189         }
2190         
2191         if ((err = snd_vt1724_pcm_spdif(ice, pcm_dev++)) < 0) {
2192                 snd_card_free(card);
2193                 return err;
2194         }
2195         
2196         if ((err = snd_vt1724_pcm_indep(ice, pcm_dev++)) < 0) {
2197                 snd_card_free(card);
2198                 return err;
2199         }
2200
2201         if ((err = snd_vt1724_ac97_mixer(ice)) < 0) {
2202                 snd_card_free(card);
2203                 return err;
2204         }
2205
2206         if ((err = snd_vt1724_build_controls(ice)) < 0) {
2207                 snd_card_free(card);
2208                 return err;
2209         }
2210
2211         if (ice->pcm && ice->has_spdif) { /* has SPDIF I/O */
2212                 if ((err = snd_vt1724_spdif_build_controls(ice)) < 0) {
2213                         snd_card_free(card);
2214                         return err;
2215                 }
2216         }
2217
2218         if (c->build_controls) {
2219                 if ((err = c->build_controls(ice)) < 0) {
2220                         snd_card_free(card);
2221                         return err;
2222                 }
2223         }
2224
2225         if (! c->no_mpu401) {
2226                 if (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401) {
2227                         if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ICE1712,
2228                                                        ICEREG1724(ice, MPU_CTRL), 1,
2229                                                        ice->irq, 0,
2230                                                        &ice->rmidi[0])) < 0) {
2231                                 snd_card_free(card);
2232                                 return err;
2233                         }
2234                 }
2235         }
2236
2237         sprintf(card->longname, "%s at 0x%lx, irq %i",
2238                 card->shortname, ice->port, ice->irq);
2239
2240         if ((err = snd_card_register(card)) < 0) {
2241                 snd_card_free(card);
2242                 return err;
2243         }
2244         pci_set_drvdata(pci, card);
2245         dev++;
2246         return 0;
2247 }
2248
2249 static void __devexit snd_vt1724_remove(struct pci_dev *pci)
2250 {
2251         snd_card_free(pci_get_drvdata(pci));
2252         pci_set_drvdata(pci, NULL);
2253 }
2254
2255 static struct pci_driver driver = {
2256         .name = "ICE1724",
2257         .id_table = snd_vt1724_ids,
2258         .probe = snd_vt1724_probe,
2259         .remove = __devexit_p(snd_vt1724_remove),
2260 };
2261
2262 static int __init alsa_card_ice1724_init(void)
2263 {
2264         return pci_module_init(&driver);
2265 }
2266
2267 static void __exit alsa_card_ice1724_exit(void)
2268 {
2269         pci_unregister_driver(&driver);
2270 }
2271
2272 module_init(alsa_card_ice1724_init)
2273 module_exit(alsa_card_ice1724_exit)