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