Merge master.kernel.org:/pub/scm/linux/kernel/git/perex/alsa-current
[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|SNDRV_PCM_RATE_48000,
679         .rate_min =             32000,
680         .rate_max =             48000,
681         .channels_min =         2,
682         .channels_max =         2,
683         .buffer_bytes_max =     (1UL << 18),    /* 16bits dword */
684         .period_bytes_min =     2 * 4 * 2,
685         .period_bytes_max =     (1UL << 18),
686         .periods_min =          2,
687         .periods_max =          1024,
688 };
689
690 static snd_pcm_hardware_t snd_vt1724_2ch_stereo =
691 {
692         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
693                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
694                                  SNDRV_PCM_INFO_MMAP_VALID |
695                                  SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
696         .formats =              SNDRV_PCM_FMTBIT_S32_LE,
697         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
698         .rate_min =             8000,
699         .rate_max =             192000,
700         .channels_min =         2,
701         .channels_max =         2,
702         .buffer_bytes_max =     (1UL << 18),    /* 16bits dword */
703         .period_bytes_min =     2 * 4 * 2,
704         .period_bytes_max =     (1UL << 18),
705         .periods_min =          2,
706         .periods_max =          1024,
707 };
708
709 /*
710  * set rate constraints
711  */
712 static int set_rate_constraints(ice1712_t *ice, snd_pcm_substream_t *substream)
713 {
714         snd_pcm_runtime_t *runtime = substream->runtime;
715         if (ice->hw_rates) {
716                 /* hardware specific */
717                 runtime->hw.rate_min = ice->hw_rates->list[0];
718                 runtime->hw.rate_max = ice->hw_rates->list[ice->hw_rates->count - 1];
719                 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
720                 return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, ice->hw_rates);
721         }
722         if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
723                 /* I2S */
724                 /* VT1720 doesn't support more than 96kHz */
725                 if ((ice->eeprom.data[ICE_EEP2_I2S] & 0x08) && !ice->vt1720)
726                         return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates_192);
727                 else {
728                         runtime->hw.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_96000;
729                         runtime->hw.rate_max = 96000;
730                         return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates_96);
731                 }
732         } else if (ice->ac97) {
733                 /* ACLINK */
734                 runtime->hw.rate_max = 48000;
735                 runtime->hw.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000;
736                 return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates_48);
737         }
738         return 0;
739 }
740
741 /* multi-channel playback needs alignment 8x32bit regardless of the channels
742  * actually used
743  */
744 #define VT1724_BUFFER_ALIGN     0x20
745
746 static int snd_vt1724_playback_pro_open(snd_pcm_substream_t * substream)
747 {
748         snd_pcm_runtime_t *runtime = substream->runtime;
749         ice1712_t *ice = snd_pcm_substream_chip(substream);
750         int chs;
751
752         runtime->private_data = &vt1724_playback_pro_reg;
753         ice->playback_pro_substream = substream;
754         runtime->hw = snd_vt1724_playback_pro;
755         snd_pcm_set_sync(substream);
756         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
757         set_rate_constraints(ice, substream);
758         down(&ice->open_mutex);
759         /* calculate the currently available channels */
760         for (chs = 0; chs < 3; chs++) {
761                 if (ice->pcm_reserved[chs])
762                         break;
763         }
764         chs = (chs + 1) * 2;
765         runtime->hw.channels_max = chs;
766         if (chs > 2) /* channels must be even */
767                 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
768         up(&ice->open_mutex);
769         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
770                                    VT1724_BUFFER_ALIGN);
771         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
772                                    VT1724_BUFFER_ALIGN);
773         return 0;
774 }
775
776 static int snd_vt1724_capture_pro_open(snd_pcm_substream_t * substream)
777 {
778         ice1712_t *ice = snd_pcm_substream_chip(substream);
779         snd_pcm_runtime_t *runtime = substream->runtime;
780
781         runtime->private_data = &vt1724_capture_pro_reg;
782         ice->capture_pro_substream = substream;
783         runtime->hw = snd_vt1724_2ch_stereo;
784         snd_pcm_set_sync(substream);
785         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
786         set_rate_constraints(ice, substream);
787         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
788                                    VT1724_BUFFER_ALIGN);
789         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
790                                    VT1724_BUFFER_ALIGN);
791         return 0;
792 }
793
794 static int snd_vt1724_playback_pro_close(snd_pcm_substream_t * substream)
795 {
796         ice1712_t *ice = snd_pcm_substream_chip(substream);
797
798         if (PRO_RATE_RESET)
799                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
800         ice->playback_pro_substream = NULL;
801
802         return 0;
803 }
804
805 static int snd_vt1724_capture_pro_close(snd_pcm_substream_t * substream)
806 {
807         ice1712_t *ice = snd_pcm_substream_chip(substream);
808
809         if (PRO_RATE_RESET)
810                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
811         ice->capture_pro_substream = NULL;
812         return 0;
813 }
814
815 static snd_pcm_ops_t snd_vt1724_playback_pro_ops = {
816         .open =         snd_vt1724_playback_pro_open,
817         .close =        snd_vt1724_playback_pro_close,
818         .ioctl =        snd_pcm_lib_ioctl,
819         .hw_params =    snd_vt1724_pcm_hw_params,
820         .hw_free =      snd_vt1724_pcm_hw_free,
821         .prepare =      snd_vt1724_playback_pro_prepare,
822         .trigger =      snd_vt1724_pcm_trigger,
823         .pointer =      snd_vt1724_playback_pro_pointer,
824 };
825
826 static snd_pcm_ops_t snd_vt1724_capture_pro_ops = {
827         .open =         snd_vt1724_capture_pro_open,
828         .close =        snd_vt1724_capture_pro_close,
829         .ioctl =        snd_pcm_lib_ioctl,
830         .hw_params =    snd_vt1724_pcm_hw_params,
831         .hw_free =      snd_vt1724_pcm_hw_free,
832         .prepare =      snd_vt1724_pcm_prepare,
833         .trigger =      snd_vt1724_pcm_trigger,
834         .pointer =      snd_vt1724_pcm_pointer,
835 };
836
837 static int __devinit snd_vt1724_pcm_profi(ice1712_t * ice, int device)
838 {
839         snd_pcm_t *pcm;
840         int err;
841
842         err = snd_pcm_new(ice->card, "ICE1724", device, 1, 1, &pcm);
843         if (err < 0)
844                 return err;
845
846         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_vt1724_playback_pro_ops);
847         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_vt1724_capture_pro_ops);
848
849         pcm->private_data = ice;
850         pcm->info_flags = 0;
851         strcpy(pcm->name, "ICE1724");
852
853         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
854                                               snd_dma_pci_data(ice->pci), 256*1024, 256*1024);
855
856         ice->pcm_pro = pcm;
857
858         return 0;
859 }
860
861
862 /*
863  * SPDIF PCM
864  */
865
866 static struct vt1724_pcm_reg vt1724_playback_spdif_reg = {
867         .addr = VT1724_MT_PDMA4_ADDR,
868         .size = VT1724_MT_PDMA4_SIZE,
869         .count = VT1724_MT_PDMA4_COUNT,
870         .start = VT1724_PDMA4_START,
871 };
872
873 static struct vt1724_pcm_reg vt1724_capture_spdif_reg = {
874         .addr = VT1724_MT_RDMA1_ADDR,
875         .size = VT1724_MT_RDMA1_SIZE,
876         .count = VT1724_MT_RDMA1_COUNT,
877         .start = VT1724_RDMA1_START,
878 };
879
880 /* update spdif control bits; call with reg_lock */
881 static void update_spdif_bits(ice1712_t *ice, unsigned int val)
882 {
883         unsigned char cbit, disabled;
884
885         cbit = inb(ICEREG1724(ice, SPDIF_CFG));
886         disabled = cbit & ~VT1724_CFG_SPDIF_OUT_EN;
887         if (cbit != disabled)
888                 outb(disabled, ICEREG1724(ice, SPDIF_CFG));
889         outw(val, ICEMT1724(ice, SPDIF_CTRL));
890         if (cbit != disabled)
891                 outb(cbit, ICEREG1724(ice, SPDIF_CFG));
892         outw(val, ICEMT1724(ice, SPDIF_CTRL));
893 }
894
895 /* update SPDIF control bits according to the given rate */
896 static void update_spdif_rate(ice1712_t *ice, unsigned int rate)
897 {
898         unsigned int val, nval;
899         unsigned long flags;
900
901         spin_lock_irqsave(&ice->reg_lock, flags);
902         nval = val = inw(ICEMT1724(ice, SPDIF_CTRL));
903         nval &= ~(7 << 12);
904         switch (rate) {
905         case 44100: break;
906         case 48000: nval |= 2 << 12; break;
907         case 32000: nval |= 3 << 12; break;
908         }
909         if (val != nval)
910                 update_spdif_bits(ice, nval);
911         spin_unlock_irqrestore(&ice->reg_lock, flags);
912 }
913
914 static int snd_vt1724_playback_spdif_prepare(snd_pcm_substream_t * substream)
915 {
916         ice1712_t *ice = snd_pcm_substream_chip(substream);
917         if (! ice->force_pdma4)
918                 update_spdif_rate(ice, substream->runtime->rate);
919         return snd_vt1724_pcm_prepare(substream);
920 }
921
922 static int snd_vt1724_playback_spdif_open(snd_pcm_substream_t *substream)
923 {
924         ice1712_t *ice = snd_pcm_substream_chip(substream);
925         snd_pcm_runtime_t *runtime = substream->runtime;
926
927         runtime->private_data = &vt1724_playback_spdif_reg;
928         ice->playback_con_substream = substream;
929         if (ice->force_pdma4) {
930                 runtime->hw = snd_vt1724_2ch_stereo;
931                 set_rate_constraints(ice, substream);
932         } else
933                 runtime->hw = snd_vt1724_spdif;
934         snd_pcm_set_sync(substream);
935         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
936         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
937                                    VT1724_BUFFER_ALIGN);
938         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
939                                    VT1724_BUFFER_ALIGN);
940         return 0;
941 }
942
943 static int snd_vt1724_playback_spdif_close(snd_pcm_substream_t * substream)
944 {
945         ice1712_t *ice = snd_pcm_substream_chip(substream);
946
947         if (PRO_RATE_RESET)
948                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
949         ice->playback_con_substream = NULL;
950
951         return 0;
952 }
953
954 static int snd_vt1724_capture_spdif_open(snd_pcm_substream_t *substream)
955 {
956         ice1712_t *ice = snd_pcm_substream_chip(substream);
957         snd_pcm_runtime_t *runtime = substream->runtime;
958
959         runtime->private_data = &vt1724_capture_spdif_reg;
960         ice->capture_con_substream = substream;
961         if (ice->force_rdma1) {
962                 runtime->hw = snd_vt1724_2ch_stereo;
963                 set_rate_constraints(ice, substream);
964         } else
965                 runtime->hw = snd_vt1724_spdif;
966         snd_pcm_set_sync(substream);
967         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
968         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
969                                    VT1724_BUFFER_ALIGN);
970         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
971                                    VT1724_BUFFER_ALIGN);
972         return 0;
973 }
974
975 static int snd_vt1724_capture_spdif_close(snd_pcm_substream_t * substream)
976 {
977         ice1712_t *ice = snd_pcm_substream_chip(substream);
978
979         if (PRO_RATE_RESET)
980                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
981         ice->capture_con_substream = NULL;
982
983         return 0;
984 }
985
986 static snd_pcm_ops_t snd_vt1724_playback_spdif_ops = {
987         .open =         snd_vt1724_playback_spdif_open,
988         .close =        snd_vt1724_playback_spdif_close,
989         .ioctl =        snd_pcm_lib_ioctl,
990         .hw_params =    snd_vt1724_pcm_hw_params,
991         .hw_free =      snd_vt1724_pcm_hw_free,
992         .prepare =      snd_vt1724_playback_spdif_prepare,
993         .trigger =      snd_vt1724_pcm_trigger,
994         .pointer =      snd_vt1724_pcm_pointer,
995 };
996
997 static snd_pcm_ops_t snd_vt1724_capture_spdif_ops = {
998         .open =         snd_vt1724_capture_spdif_open,
999         .close =        snd_vt1724_capture_spdif_close,
1000         .ioctl =        snd_pcm_lib_ioctl,
1001         .hw_params =    snd_vt1724_pcm_hw_params,
1002         .hw_free =      snd_vt1724_pcm_hw_free,
1003         .prepare =      snd_vt1724_pcm_prepare,
1004         .trigger =      snd_vt1724_pcm_trigger,
1005         .pointer =      snd_vt1724_pcm_pointer,
1006 };
1007
1008
1009 static int __devinit snd_vt1724_pcm_spdif(ice1712_t * ice, int device)
1010 {
1011         char *name;
1012         snd_pcm_t *pcm;
1013         int play, capt;
1014         int err;
1015
1016         if (ice->force_pdma4 ||
1017             (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_OUT_INT)) {
1018                 play = 1;
1019                 ice->has_spdif = 1;
1020         } else
1021                 play = 0;
1022         if (ice->force_rdma1 ||
1023             (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_IN)) {
1024                 capt = 1;
1025                 ice->has_spdif = 1;
1026         } else
1027                 capt = 0;
1028         if (! play && ! capt)
1029                 return 0; /* no spdif device */
1030
1031         if (ice->force_pdma4 || ice->force_rdma1)
1032                 name = "ICE1724 Secondary";
1033         else
1034                 name = "IEC1724 IEC958";
1035         err = snd_pcm_new(ice->card, name, device, play, capt, &pcm);
1036         if (err < 0)
1037                 return err;
1038
1039         if (play)
1040                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1041                                 &snd_vt1724_playback_spdif_ops);
1042         if (capt)
1043                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1044                                 &snd_vt1724_capture_spdif_ops);
1045
1046         pcm->private_data = ice;
1047         pcm->info_flags = 0;
1048         strcpy(pcm->name, name);
1049
1050         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1051                                               snd_dma_pci_data(ice->pci), 64*1024, 64*1024);
1052
1053         ice->pcm = pcm;
1054
1055         return 0;
1056 }
1057
1058
1059 /*
1060  * independent surround PCMs
1061  */
1062
1063 static struct vt1724_pcm_reg vt1724_playback_dma_regs[3] = {
1064         {
1065                 .addr = VT1724_MT_PDMA1_ADDR,
1066                 .size = VT1724_MT_PDMA1_SIZE,
1067                 .count = VT1724_MT_PDMA1_COUNT,
1068                 .start = VT1724_PDMA1_START,
1069         },
1070         {
1071                 .addr = VT1724_MT_PDMA2_ADDR,
1072                 .size = VT1724_MT_PDMA2_SIZE,
1073                 .count = VT1724_MT_PDMA2_COUNT,
1074                 .start = VT1724_PDMA2_START,
1075         },
1076         {
1077                 .addr = VT1724_MT_PDMA3_ADDR,
1078                 .size = VT1724_MT_PDMA3_SIZE,
1079                 .count = VT1724_MT_PDMA3_COUNT,
1080                 .start = VT1724_PDMA3_START,
1081         },
1082 };
1083
1084 static int snd_vt1724_playback_indep_prepare(snd_pcm_substream_t * substream)
1085 {
1086         ice1712_t *ice = snd_pcm_substream_chip(substream);
1087         unsigned char val;
1088
1089         spin_lock_irq(&ice->reg_lock);
1090         val = 3 - substream->number;
1091         if (inb(ICEMT1724(ice, BURST)) < val)
1092                 outb(val, ICEMT1724(ice, BURST));
1093         spin_unlock_irq(&ice->reg_lock);
1094         return snd_vt1724_pcm_prepare(substream);
1095 }
1096
1097 static int snd_vt1724_playback_indep_open(snd_pcm_substream_t *substream)
1098 {
1099         ice1712_t *ice = snd_pcm_substream_chip(substream);
1100         snd_pcm_runtime_t *runtime = substream->runtime;
1101
1102         down(&ice->open_mutex);
1103         /* already used by PDMA0? */
1104         if (ice->pcm_reserved[substream->number]) {
1105                 up(&ice->open_mutex);
1106                 return -EBUSY; /* FIXME: should handle blocking mode properly */
1107         }
1108         up(&ice->open_mutex);
1109         runtime->private_data = &vt1724_playback_dma_regs[substream->number];
1110         ice->playback_con_substream_ds[substream->number] = substream;
1111         runtime->hw = snd_vt1724_2ch_stereo;
1112         snd_pcm_set_sync(substream);
1113         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1114         set_rate_constraints(ice, substream);
1115         return 0;
1116 }
1117
1118 static int snd_vt1724_playback_indep_close(snd_pcm_substream_t * substream)
1119 {
1120         ice1712_t *ice = snd_pcm_substream_chip(substream);
1121
1122         if (PRO_RATE_RESET)
1123                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
1124         ice->playback_con_substream_ds[substream->number] = NULL;
1125         ice->pcm_reserved[substream->number] = NULL;
1126
1127         return 0;
1128 }
1129
1130 static snd_pcm_ops_t snd_vt1724_playback_indep_ops = {
1131         .open =         snd_vt1724_playback_indep_open,
1132         .close =        snd_vt1724_playback_indep_close,
1133         .ioctl =        snd_pcm_lib_ioctl,
1134         .hw_params =    snd_vt1724_pcm_hw_params,
1135         .hw_free =      snd_vt1724_pcm_hw_free,
1136         .prepare =      snd_vt1724_playback_indep_prepare,
1137         .trigger =      snd_vt1724_pcm_trigger,
1138         .pointer =      snd_vt1724_pcm_pointer,
1139 };
1140
1141
1142 static int __devinit snd_vt1724_pcm_indep(ice1712_t * ice, int device)
1143 {
1144         snd_pcm_t *pcm;
1145         int play;
1146         int err;
1147
1148         play = ice->num_total_dacs / 2 - 1;
1149         if (play <= 0)
1150                 return 0;
1151
1152         err = snd_pcm_new(ice->card, "ICE1724 Surrounds", device, play, 0, &pcm);
1153         if (err < 0)
1154                 return err;
1155
1156         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1157                         &snd_vt1724_playback_indep_ops);
1158
1159         pcm->private_data = ice;
1160         pcm->info_flags = 0;
1161         strcpy(pcm->name, "ICE1724 Surround PCM");
1162
1163         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1164                                               snd_dma_pci_data(ice->pci), 64*1024, 64*1024);
1165
1166         ice->pcm_ds = pcm;
1167
1168         return 0;
1169 }
1170
1171
1172 /*
1173  *  Mixer section
1174  */
1175
1176 static int __devinit snd_vt1724_ac97_mixer(ice1712_t * ice)
1177 {
1178         int err;
1179
1180         if (! (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S)) {
1181                 ac97_bus_t *pbus;
1182                 ac97_template_t ac97;
1183                 static ac97_bus_ops_t ops = {
1184                         .write = snd_vt1724_ac97_write,
1185                         .read = snd_vt1724_ac97_read,
1186                 };
1187
1188                 /* cold reset */
1189                 outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
1190                 mdelay(5); /* FIXME */
1191                 outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
1192
1193                 if ((err = snd_ac97_bus(ice->card, 0, &ops, NULL, &pbus)) < 0)
1194                         return err;
1195                 memset(&ac97, 0, sizeof(ac97));
1196                 ac97.private_data = ice;
1197                 if ((err = snd_ac97_mixer(pbus, &ac97, &ice->ac97)) < 0)
1198                         printk(KERN_WARNING "ice1712: cannot initialize pro ac97, skipped\n");
1199                 else
1200                         return 0;
1201         }
1202         /* I2S mixer only */
1203         strcat(ice->card->mixername, "ICE1724 - multitrack");
1204         return 0;
1205 }
1206
1207 /*
1208  *
1209  */
1210
1211 static inline unsigned int eeprom_triple(ice1712_t *ice, int idx)
1212 {
1213         return (unsigned int)ice->eeprom.data[idx] | \
1214                 ((unsigned int)ice->eeprom.data[idx + 1] << 8) | \
1215                 ((unsigned int)ice->eeprom.data[idx + 2] << 16);
1216 }
1217
1218 static void snd_vt1724_proc_read(snd_info_entry_t *entry, 
1219                                  snd_info_buffer_t * buffer)
1220 {
1221         ice1712_t *ice = entry->private_data;
1222         unsigned int idx;
1223
1224         snd_iprintf(buffer, "%s\n\n", ice->card->longname);
1225         snd_iprintf(buffer, "EEPROM:\n");
1226
1227         snd_iprintf(buffer, "  Subvendor        : 0x%x\n", ice->eeprom.subvendor);
1228         snd_iprintf(buffer, "  Size             : %i bytes\n", ice->eeprom.size);
1229         snd_iprintf(buffer, "  Version          : %i\n", ice->eeprom.version);
1230         snd_iprintf(buffer, "  System Config    : 0x%x\n", ice->eeprom.data[ICE_EEP2_SYSCONF]);
1231         snd_iprintf(buffer, "  ACLink           : 0x%x\n", ice->eeprom.data[ICE_EEP2_ACLINK]);
1232         snd_iprintf(buffer, "  I2S              : 0x%x\n", ice->eeprom.data[ICE_EEP2_I2S]);
1233         snd_iprintf(buffer, "  S/PDIF           : 0x%x\n", ice->eeprom.data[ICE_EEP2_SPDIF]);
1234         snd_iprintf(buffer, "  GPIO direction   : 0x%x\n", ice->eeprom.gpiodir);
1235         snd_iprintf(buffer, "  GPIO mask        : 0x%x\n", ice->eeprom.gpiomask);
1236         snd_iprintf(buffer, "  GPIO state       : 0x%x\n", ice->eeprom.gpiostate);
1237         for (idx = 0x12; idx < ice->eeprom.size; idx++)
1238                 snd_iprintf(buffer, "  Extra #%02i        : 0x%x\n", idx, ice->eeprom.data[idx]);
1239
1240         snd_iprintf(buffer, "\nRegisters:\n");
1241
1242         snd_iprintf(buffer, "  PSDOUT03 : 0x%08x\n", (unsigned)inl(ICEMT1724(ice, ROUTE_PLAYBACK)));
1243         for (idx = 0x0; idx < 0x20 ; idx++)
1244                 snd_iprintf(buffer, "  CCS%02x    : 0x%02x\n", idx, inb(ice->port+idx));
1245         for (idx = 0x0; idx < 0x30 ; idx++)
1246                 snd_iprintf(buffer, "  MT%02x     : 0x%02x\n", idx, inb(ice->profi_port+idx));
1247 }
1248
1249 static void __devinit snd_vt1724_proc_init(ice1712_t * ice)
1250 {
1251         snd_info_entry_t *entry;
1252
1253         if (! snd_card_proc_new(ice->card, "ice1724", &entry))
1254                 snd_info_set_text_ops(entry, ice, 1024, snd_vt1724_proc_read);
1255 }
1256
1257 /*
1258  *
1259  */
1260
1261 static int snd_vt1724_eeprom_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1262 {
1263         uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1264         uinfo->count = sizeof(ice1712_eeprom_t);
1265         return 0;
1266 }
1267
1268 static int snd_vt1724_eeprom_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1269 {
1270         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1271         
1272         memcpy(ucontrol->value.bytes.data, &ice->eeprom, sizeof(ice->eeprom));
1273         return 0;
1274 }
1275
1276 static snd_kcontrol_new_t snd_vt1724_eeprom __devinitdata = {
1277         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1278         .name = "ICE1724 EEPROM",
1279         .access = SNDRV_CTL_ELEM_ACCESS_READ,
1280         .info = snd_vt1724_eeprom_info,
1281         .get = snd_vt1724_eeprom_get
1282 };
1283
1284 /*
1285  */
1286 static int snd_vt1724_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1287 {
1288         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1289         uinfo->count = 1;
1290         return 0;
1291 }
1292
1293 static unsigned int encode_spdif_bits(snd_aes_iec958_t *diga)
1294 {
1295         unsigned int val;
1296
1297         val = diga->status[0] & 0x03; /* professional, non-audio */
1298         if (val & 0x01) {
1299                 /* professional */
1300                 if ((diga->status[0] & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
1301                         val |= 1U << 3;
1302                 switch (diga->status[0] & IEC958_AES0_PRO_FS) {
1303                 case IEC958_AES0_PRO_FS_44100:
1304                         break;
1305                 case IEC958_AES0_PRO_FS_32000:
1306                         val |= 3U << 12;
1307                         break;
1308                 default:
1309                         val |= 2U << 12;
1310                         break;
1311                 }
1312         } else {
1313                 /* consumer */
1314                 val |= diga->status[1] & 0x04; /* copyright */
1315                 if ((diga->status[0] & IEC958_AES0_CON_EMPHASIS)== IEC958_AES0_CON_EMPHASIS_5015)
1316                         val |= 1U << 3;
1317                 val |= (unsigned int)(diga->status[1] & 0x3f) << 4; /* category */
1318                 val |= (unsigned int)(diga->status[3] & IEC958_AES3_CON_FS) << 12; /* fs */
1319         }
1320         return val;
1321 }
1322
1323 static void decode_spdif_bits(snd_aes_iec958_t *diga, unsigned int val)
1324 {
1325         memset(diga->status, 0, sizeof(diga->status));
1326         diga->status[0] = val & 0x03; /* professional, non-audio */
1327         if (val & 0x01) {
1328                 /* professional */
1329                 if (val & (1U << 3))
1330                         diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015;
1331                 switch ((val >> 12) & 0x7) {
1332                 case 0:
1333                         break;
1334                 case 2:
1335                         diga->status[0] |= IEC958_AES0_PRO_FS_32000;
1336                         break;
1337                 default:
1338                         diga->status[0] |= IEC958_AES0_PRO_FS_48000;
1339                         break;
1340                 }
1341         } else {
1342                 /* consumer */
1343                 diga->status[0] |= val & (1U << 2); /* copyright */
1344                 if (val & (1U << 3))
1345                         diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015;
1346                 diga->status[1] |= (val >> 4) & 0x3f; /* category */
1347                 diga->status[3] |= (val >> 12) & 0x07; /* fs */
1348         }
1349 }
1350
1351 static int snd_vt1724_spdif_default_get(snd_kcontrol_t * kcontrol,
1352                                         snd_ctl_elem_value_t * ucontrol)
1353 {
1354         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1355         unsigned int val;
1356         val = inw(ICEMT1724(ice, SPDIF_CTRL));
1357         decode_spdif_bits(&ucontrol->value.iec958, val);
1358         return 0;
1359 }
1360
1361 static int snd_vt1724_spdif_default_put(snd_kcontrol_t * kcontrol,
1362                                          snd_ctl_elem_value_t * ucontrol)
1363 {
1364         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1365         unsigned int val, old;
1366
1367         val = encode_spdif_bits(&ucontrol->value.iec958);
1368         spin_lock_irq(&ice->reg_lock);
1369         old = inw(ICEMT1724(ice, SPDIF_CTRL));
1370         if (val != old)
1371                 update_spdif_bits(ice, val);
1372         spin_unlock_irq(&ice->reg_lock);
1373         return (val != old);
1374 }
1375
1376 static snd_kcontrol_new_t snd_vt1724_spdif_default __devinitdata =
1377 {
1378         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1379         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1380         .info =         snd_vt1724_spdif_info,
1381         .get =          snd_vt1724_spdif_default_get,
1382         .put =          snd_vt1724_spdif_default_put
1383 };
1384
1385 static int snd_vt1724_spdif_maskc_get(snd_kcontrol_t * kcontrol,
1386                                        snd_ctl_elem_value_t * ucontrol)
1387 {
1388         ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1389                                                      IEC958_AES0_PROFESSIONAL |
1390                                                      IEC958_AES0_CON_NOT_COPYRIGHT |
1391                                                      IEC958_AES0_CON_EMPHASIS;
1392         ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL |
1393                                                      IEC958_AES1_CON_CATEGORY;
1394         ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
1395         return 0;
1396 }
1397
1398 static int snd_vt1724_spdif_maskp_get(snd_kcontrol_t * kcontrol,
1399                                        snd_ctl_elem_value_t * ucontrol)
1400 {
1401         ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1402                                                      IEC958_AES0_PROFESSIONAL |
1403                                                      IEC958_AES0_PRO_FS |
1404                                                      IEC958_AES0_PRO_EMPHASIS;
1405         return 0;
1406 }
1407
1408 static snd_kcontrol_new_t snd_vt1724_spdif_maskc __devinitdata =
1409 {
1410         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1411         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1412         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1413         .info =         snd_vt1724_spdif_info,
1414         .get =          snd_vt1724_spdif_maskc_get,
1415 };
1416
1417 static snd_kcontrol_new_t snd_vt1724_spdif_maskp __devinitdata =
1418 {
1419         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1420         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1421         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1422         .info =         snd_vt1724_spdif_info,
1423         .get =          snd_vt1724_spdif_maskp_get,
1424 };
1425
1426 static int snd_vt1724_spdif_sw_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1427 {
1428         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1429         uinfo->count = 1;
1430         uinfo->value.integer.min = 0;
1431         uinfo->value.integer.max = 1;
1432         return 0;
1433 }
1434
1435 static int snd_vt1724_spdif_sw_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1436 {
1437         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1438         ucontrol->value.integer.value[0] = inb(ICEREG1724(ice, SPDIF_CFG)) & VT1724_CFG_SPDIF_OUT_EN ? 1 : 0;
1439         return 0;
1440 }
1441
1442 static int snd_vt1724_spdif_sw_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1443 {
1444         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1445         unsigned char old, val;
1446
1447         spin_lock_irq(&ice->reg_lock);
1448         old = val = inb(ICEREG1724(ice, SPDIF_CFG));
1449         val &= ~VT1724_CFG_SPDIF_OUT_EN;
1450         if (ucontrol->value.integer.value[0])
1451                 val |= VT1724_CFG_SPDIF_OUT_EN;
1452         if (old != val)
1453                 outb(val, ICEREG1724(ice, SPDIF_CFG));
1454         spin_unlock_irq(&ice->reg_lock);
1455         return old != val;
1456 }
1457
1458 static snd_kcontrol_new_t snd_vt1724_spdif_switch __devinitdata =
1459 {
1460         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1461         /* FIXME: the following conflict with IEC958 Playback Route */
1462         // .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1463         .name =         SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
1464         .info =         snd_vt1724_spdif_sw_info,
1465         .get =          snd_vt1724_spdif_sw_get,
1466         .put =          snd_vt1724_spdif_sw_put
1467 };
1468
1469
1470 #if 0 /* NOT USED YET */
1471 /*
1472  * GPIO access from extern
1473  */
1474
1475 int snd_vt1724_gpio_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1476 {
1477         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1478         uinfo->count = 1;
1479         uinfo->value.integer.min = 0;
1480         uinfo->value.integer.max = 1;
1481         return 0;
1482 }
1483
1484 int snd_vt1724_gpio_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1485 {
1486         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1487         int shift = kcontrol->private_value & 0xff;
1488         int invert = (kcontrol->private_value & (1<<24)) ? 1 : 0;
1489         
1490         snd_ice1712_save_gpio_status(ice);
1491         ucontrol->value.integer.value[0] = (snd_ice1712_gpio_read(ice) & (1 << shift) ? 1 : 0) ^ invert;
1492         snd_ice1712_restore_gpio_status(ice);
1493         return 0;
1494 }
1495
1496 int snd_ice1712_gpio_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1497 {
1498         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1499         int shift = kcontrol->private_value & 0xff;
1500         int invert = (kcontrol->private_value & (1<<24)) ? mask : 0;
1501         unsigned int val, nval;
1502
1503         if (kcontrol->private_value & (1 << 31))
1504                 return -EPERM;
1505         nval = (ucontrol->value.integer.value[0] ? (1 << shift) : 0) ^ invert;
1506         snd_ice1712_save_gpio_status(ice);
1507         val = snd_ice1712_gpio_read(ice);
1508         nval |= val & ~(1 << shift);
1509         if (val != nval)
1510                 snd_ice1712_gpio_write(ice, nval);
1511         snd_ice1712_restore_gpio_status(ice);
1512         return val != nval;
1513 }
1514 #endif /* NOT USED YET */
1515
1516 /*
1517  *  rate
1518  */
1519 static int snd_vt1724_pro_internal_clock_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1520 {
1521         static char *texts_1724[] = {
1522                 "8000",         /* 0: 6 */
1523                 "9600",         /* 1: 3 */
1524                 "11025",        /* 2: 10 */
1525                 "12000",        /* 3: 2 */
1526                 "16000",        /* 4: 5 */
1527                 "22050",        /* 5: 9 */
1528                 "24000",        /* 6: 1 */
1529                 "32000",        /* 7: 4 */
1530                 "44100",        /* 8: 8 */
1531                 "48000",        /* 9: 0 */
1532                 "64000",        /* 10: 15 */
1533                 "88200",        /* 11: 11 */
1534                 "96000",        /* 12: 7 */
1535                 "176400",       /* 13: 12 */
1536                 "192000",       /* 14: 14 */
1537                 "IEC958 Input", /* 15: -- */
1538         };
1539         static char *texts_1720[] = {
1540                 "8000",         /* 0: 6 */
1541                 "9600",         /* 1: 3 */
1542                 "11025",        /* 2: 10 */
1543                 "12000",        /* 3: 2 */
1544                 "16000",        /* 4: 5 */
1545                 "22050",        /* 5: 9 */
1546                 "24000",        /* 6: 1 */
1547                 "32000",        /* 7: 4 */
1548                 "44100",        /* 8: 8 */
1549                 "48000",        /* 9: 0 */
1550                 "64000",        /* 10: 15 */
1551                 "88200",        /* 11: 11 */
1552                 "96000",        /* 12: 7 */
1553                 "IEC958 Input", /* 13: -- */
1554         };
1555         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1556
1557         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1558         uinfo->count = 1;
1559         uinfo->value.enumerated.items = ice->vt1720 ? 14 : 16;
1560         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1561                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1562         strcpy(uinfo->value.enumerated.name,
1563                ice->vt1720 ? texts_1720[uinfo->value.enumerated.item] :
1564                texts_1724[uinfo->value.enumerated.item]);
1565         return 0;
1566 }
1567
1568 static int snd_vt1724_pro_internal_clock_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1569 {
1570         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1571         static unsigned char xlate[16] = {
1572                 9, 6, 3, 1, 7, 4, 0, 12, 8, 5, 2, 11, 13, 255, 14, 10
1573         };
1574         unsigned char val;
1575         
1576         spin_lock_irq(&ice->reg_lock);
1577         if (is_spdif_master(ice)) {
1578                 ucontrol->value.enumerated.item[0] = ice->vt1720 ? 13 : 15;
1579         } else {
1580                 val = xlate[inb(ICEMT1724(ice, RATE)) & 15];
1581                 if (val == 255) {
1582                         snd_BUG();
1583                         val = 0;
1584                 }
1585                 ucontrol->value.enumerated.item[0] = val;
1586         }
1587         spin_unlock_irq(&ice->reg_lock);
1588         return 0;
1589 }
1590
1591 static int snd_vt1724_pro_internal_clock_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1592 {
1593         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1594         unsigned char oval;
1595         int rate;
1596         int change = 0;
1597         int spdif = ice->vt1720 ? 13 : 15;
1598
1599         spin_lock_irq(&ice->reg_lock);
1600         oval = inb(ICEMT1724(ice, RATE));
1601         if (ucontrol->value.enumerated.item[0] == spdif) {
1602                 outb(oval | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
1603         } else {
1604                 rate = rates[ucontrol->value.integer.value[0] % 15];
1605                 if (rate <= get_max_rate(ice)) {
1606                         PRO_RATE_DEFAULT = rate;
1607                         spin_unlock_irq(&ice->reg_lock);
1608                         snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 1);
1609                         spin_lock_irq(&ice->reg_lock);
1610                 }
1611         }
1612         change = inb(ICEMT1724(ice, RATE)) != oval;
1613         spin_unlock_irq(&ice->reg_lock);
1614
1615         if ((oval & VT1724_SPDIF_MASTER) != (inb(ICEMT1724(ice, RATE)) & VT1724_SPDIF_MASTER)) {
1616                 /* notify akm chips as well */
1617                 if (is_spdif_master(ice)) {
1618                         unsigned int i;
1619                         for (i = 0; i < ice->akm_codecs; i++) {
1620                                 if (ice->akm[i].ops.set_rate_val)
1621                                         ice->akm[i].ops.set_rate_val(&ice->akm[i], 0);
1622                         }
1623                 }
1624         }
1625         return change;
1626 }
1627
1628 static snd_kcontrol_new_t snd_vt1724_pro_internal_clock __devinitdata = {
1629         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1630         .name = "Multi Track Internal Clock",
1631         .info = snd_vt1724_pro_internal_clock_info,
1632         .get = snd_vt1724_pro_internal_clock_get,
1633         .put = snd_vt1724_pro_internal_clock_put
1634 };
1635
1636 static int snd_vt1724_pro_rate_locking_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1637 {
1638         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1639         uinfo->count = 1;
1640         uinfo->value.integer.min = 0;
1641         uinfo->value.integer.max = 1;
1642         return 0;
1643 }
1644
1645 static int snd_vt1724_pro_rate_locking_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1646 {
1647         ucontrol->value.integer.value[0] = PRO_RATE_LOCKED;
1648         return 0;
1649 }
1650
1651 static int snd_vt1724_pro_rate_locking_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1652 {
1653         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1654         int change = 0, nval;
1655
1656         nval = ucontrol->value.integer.value[0] ? 1 : 0;
1657         spin_lock_irq(&ice->reg_lock);
1658         change = PRO_RATE_LOCKED != nval;
1659         PRO_RATE_LOCKED = nval;
1660         spin_unlock_irq(&ice->reg_lock);
1661         return change;
1662 }
1663
1664 static snd_kcontrol_new_t snd_vt1724_pro_rate_locking __devinitdata = {
1665         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1666         .name = "Multi Track Rate Locking",
1667         .info = snd_vt1724_pro_rate_locking_info,
1668         .get = snd_vt1724_pro_rate_locking_get,
1669         .put = snd_vt1724_pro_rate_locking_put
1670 };
1671
1672 static int snd_vt1724_pro_rate_reset_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1673 {
1674         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1675         uinfo->count = 1;
1676         uinfo->value.integer.min = 0;
1677         uinfo->value.integer.max = 1;
1678         return 0;
1679 }
1680
1681 static int snd_vt1724_pro_rate_reset_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1682 {
1683         ucontrol->value.integer.value[0] = PRO_RATE_RESET ? 1 : 0;
1684         return 0;
1685 }
1686
1687 static int snd_vt1724_pro_rate_reset_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1688 {
1689         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1690         int change = 0, nval;
1691
1692         nval = ucontrol->value.integer.value[0] ? 1 : 0;
1693         spin_lock_irq(&ice->reg_lock);
1694         change = PRO_RATE_RESET != nval;
1695         PRO_RATE_RESET = nval;
1696         spin_unlock_irq(&ice->reg_lock);
1697         return change;
1698 }
1699
1700 static snd_kcontrol_new_t snd_vt1724_pro_rate_reset __devinitdata = {
1701         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1702         .name = "Multi Track Rate Reset",
1703         .info = snd_vt1724_pro_rate_reset_info,
1704         .get = snd_vt1724_pro_rate_reset_get,
1705         .put = snd_vt1724_pro_rate_reset_put
1706 };
1707
1708
1709 /*
1710  * routing
1711  */
1712 static int snd_vt1724_pro_route_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1713 {
1714         static char *texts[] = {
1715                 "PCM Out", /* 0 */
1716                 "H/W In 0", "H/W In 1", /* 1-2 */
1717                 "IEC958 In L", "IEC958 In R", /* 3-4 */
1718         };
1719         
1720         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1721         uinfo->count = 1;
1722         uinfo->value.enumerated.items = 5;
1723         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1724                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1725         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1726         return 0;
1727 }
1728
1729 static inline int analog_route_shift(int idx)
1730 {
1731         return (idx % 2) * 12 + ((idx / 2) * 3) + 8;
1732 }
1733
1734 static inline int digital_route_shift(int idx)
1735 {
1736         return idx * 3;
1737 }
1738
1739 static int get_route_val(ice1712_t *ice, int shift)
1740 {
1741         unsigned long val;
1742         unsigned char eitem;
1743         static unsigned char xlate[8] = {
1744                 0, 255, 1, 2, 255, 255, 3, 4,
1745         };
1746
1747         val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1748         val >>= shift;
1749         val &= 7;       //we now have 3 bits per output
1750         eitem = xlate[val];
1751         if (eitem == 255) {
1752                 snd_BUG();
1753                 return 0;
1754         }
1755         return eitem;
1756 }
1757
1758 static int put_route_val(ice1712_t *ice, unsigned int val, int shift)
1759 {
1760         unsigned int old_val, nval;
1761         int change;
1762         static unsigned char xroute[8] = {
1763                 0, /* PCM */
1764                 2, /* PSDIN0 Left */
1765                 3, /* PSDIN0 Right */
1766                 6, /* SPDIN Left */
1767                 7, /* SPDIN Right */
1768         };
1769
1770         nval = xroute[val % 5];
1771         val = old_val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1772         val &= ~(0x07 << shift);
1773         val |= nval << shift;
1774         change = val != old_val;
1775         if (change)
1776                 outl(val, ICEMT1724(ice, ROUTE_PLAYBACK));
1777         return change;
1778 }
1779
1780 static int snd_vt1724_pro_route_analog_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
1781 {
1782         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1783         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1784         ucontrol->value.enumerated.item[0] = get_route_val(ice, analog_route_shift(idx));
1785         return 0;
1786 }
1787
1788 static int snd_vt1724_pro_route_analog_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
1789 {
1790         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1791         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1792         return put_route_val(ice, ucontrol->value.enumerated.item[0],
1793                              analog_route_shift(idx));
1794 }
1795
1796 static int snd_vt1724_pro_route_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
1797 {
1798         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1799         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1800         ucontrol->value.enumerated.item[0] = get_route_val(ice, digital_route_shift(idx));
1801         return 0;
1802 }
1803
1804 static int snd_vt1724_pro_route_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
1805 {
1806         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1807         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1808         return put_route_val(ice, ucontrol->value.enumerated.item[0],
1809                              digital_route_shift(idx));
1810 }
1811
1812 static snd_kcontrol_new_t snd_vt1724_mixer_pro_analog_route __devinitdata = {
1813         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1814         .name = "H/W Playback Route",
1815         .info = snd_vt1724_pro_route_info,
1816         .get = snd_vt1724_pro_route_analog_get,
1817         .put = snd_vt1724_pro_route_analog_put,
1818 };
1819
1820 static snd_kcontrol_new_t snd_vt1724_mixer_pro_spdif_route __devinitdata = {
1821         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1822         .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Route",
1823         .info = snd_vt1724_pro_route_info,
1824         .get = snd_vt1724_pro_route_spdif_get,
1825         .put = snd_vt1724_pro_route_spdif_put,
1826         .count = 2,
1827 };
1828
1829
1830 static int snd_vt1724_pro_peak_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1831 {
1832         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1833         uinfo->count = 22; /* FIXME: for compatibility with ice1712... */
1834         uinfo->value.integer.min = 0;
1835         uinfo->value.integer.max = 255;
1836         return 0;
1837 }
1838
1839 static int snd_vt1724_pro_peak_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1840 {
1841         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1842         int idx;
1843         
1844         spin_lock_irq(&ice->reg_lock);
1845         for (idx = 0; idx < 22; idx++) {
1846                 outb(idx, ICEMT1724(ice, MONITOR_PEAKINDEX));
1847                 ucontrol->value.integer.value[idx] = inb(ICEMT1724(ice, MONITOR_PEAKDATA));
1848         }
1849         spin_unlock_irq(&ice->reg_lock);
1850         return 0;
1851 }
1852
1853 static snd_kcontrol_new_t snd_vt1724_mixer_pro_peak __devinitdata = {
1854         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1855         .name = "Multi Track Peak",
1856         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1857         .info = snd_vt1724_pro_peak_info,
1858         .get = snd_vt1724_pro_peak_get
1859 };
1860
1861 /*
1862  *
1863  */
1864
1865 static struct snd_ice1712_card_info no_matched __devinitdata;
1866
1867 static struct snd_ice1712_card_info *card_tables[] __devinitdata = {
1868         snd_vt1724_revo_cards,
1869         snd_vt1724_amp_cards, 
1870         snd_vt1724_aureon_cards,
1871         snd_vt1720_mobo_cards,
1872         snd_vt1720_pontis_cards,
1873         snd_vt1724_prodigy192_cards,
1874         snd_vt1724_juli_cards,
1875         snd_vt1724_phase_cards,
1876         NULL,
1877 };
1878
1879
1880 /*
1881  */
1882
1883 static void wait_i2c_busy(ice1712_t *ice)
1884 {
1885         int t = 0x10000;
1886         while ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_BUSY) && t--)
1887                 ;
1888         if (t == -1)
1889                 printk(KERN_ERR "ice1724: i2c busy timeout\n");
1890 }
1891
1892 unsigned char snd_vt1724_read_i2c(ice1712_t *ice, unsigned char dev, unsigned char addr)
1893 {
1894         unsigned char val;
1895
1896         down(&ice->i2c_mutex);
1897         outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
1898         outb(dev & ~VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
1899         wait_i2c_busy(ice);
1900         val = inb(ICEREG1724(ice, I2C_DATA));
1901         up(&ice->i2c_mutex);
1902         //printk("i2c_read: [0x%x,0x%x] = 0x%x\n", dev, addr, val);
1903         return val;
1904 }
1905
1906 void snd_vt1724_write_i2c(ice1712_t *ice, unsigned char dev, unsigned char addr, unsigned char data)
1907 {
1908         down(&ice->i2c_mutex);
1909         wait_i2c_busy(ice);
1910         //printk("i2c_write: [0x%x,0x%x] = 0x%x\n", dev, addr, data);
1911         outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
1912         outb(data, ICEREG1724(ice, I2C_DATA));
1913         outb(dev | VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
1914         wait_i2c_busy(ice);
1915         up(&ice->i2c_mutex);
1916 }
1917
1918 static int __devinit snd_vt1724_read_eeprom(ice1712_t *ice, const char *modelname)
1919 {
1920         const int dev = 0xa0;           /* EEPROM device address */
1921         unsigned int i, size;
1922         struct snd_ice1712_card_info **tbl, *c;
1923
1924         if (! modelname || ! *modelname) {
1925                 ice->eeprom.subvendor = 0;
1926                 if ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_EEPROM) != 0)
1927                         ice->eeprom.subvendor =
1928                                 (snd_vt1724_read_i2c(ice, dev, 0x00) << 0) |
1929                                 (snd_vt1724_read_i2c(ice, dev, 0x01) << 8) | 
1930                                 (snd_vt1724_read_i2c(ice, dev, 0x02) << 16) | 
1931                                 (snd_vt1724_read_i2c(ice, dev, 0x03) << 24);
1932                 if (ice->eeprom.subvendor == 0 || ice->eeprom.subvendor == (unsigned int)-1) {
1933                         /* invalid subvendor from EEPROM, try the PCI subststem ID instead */
1934                         u16 vendor, device;
1935                         pci_read_config_word(ice->pci, PCI_SUBSYSTEM_VENDOR_ID, &vendor);
1936                         pci_read_config_word(ice->pci, PCI_SUBSYSTEM_ID, &device);
1937                         ice->eeprom.subvendor = ((unsigned int)swab16(vendor) << 16) | swab16(device);
1938                         if (ice->eeprom.subvendor == 0 || ice->eeprom.subvendor == (unsigned int)-1) {
1939                                 printk(KERN_ERR "ice1724: No valid ID is found\n");
1940                                 return -ENXIO;
1941                         }
1942                 }
1943         }
1944         for (tbl = card_tables; *tbl; tbl++) {
1945                 for (c = *tbl; c->subvendor; c++) {
1946                         if (modelname && c->model && ! strcmp(modelname, c->model)) {
1947                                 printk(KERN_INFO "ice1724: Using board model %s\n", c->name);
1948                                 ice->eeprom.subvendor = c->subvendor;
1949                         } else if (c->subvendor != ice->eeprom.subvendor)
1950                                 continue;
1951                         if (! c->eeprom_size || ! c->eeprom_data)
1952                                 goto found;
1953                         /* if the EEPROM is given by the driver, use it */
1954                         snd_printdd("using the defined eeprom..\n");
1955                         ice->eeprom.version = 2;
1956                         ice->eeprom.size = c->eeprom_size + 6;
1957                         memcpy(ice->eeprom.data, c->eeprom_data, c->eeprom_size);
1958                         goto read_skipped;
1959                 }
1960         }
1961         printk(KERN_WARNING "ice1724: No matching model found for ID 0x%x\n", ice->eeprom.subvendor);
1962
1963  found:
1964         ice->eeprom.size = snd_vt1724_read_i2c(ice, dev, 0x04);
1965         if (ice->eeprom.size < 6)
1966                 ice->eeprom.size = 32;
1967         else if (ice->eeprom.size > 32) {
1968                 printk(KERN_ERR "ice1724: Invalid EEPROM (size = %i)\n", ice->eeprom.size);
1969                 return -EIO;
1970         }
1971         ice->eeprom.version = snd_vt1724_read_i2c(ice, dev, 0x05);
1972         if (ice->eeprom.version != 2)
1973                 printk(KERN_WARNING "ice1724: Invalid EEPROM version %i\n", ice->eeprom.version);
1974         size = ice->eeprom.size - 6;
1975         for (i = 0; i < size; i++)
1976                 ice->eeprom.data[i] = snd_vt1724_read_i2c(ice, dev, i + 6);
1977
1978  read_skipped:
1979         ice->eeprom.gpiomask = eeprom_triple(ice, ICE_EEP2_GPIO_MASK);
1980         ice->eeprom.gpiostate = eeprom_triple(ice, ICE_EEP2_GPIO_STATE);
1981         ice->eeprom.gpiodir = eeprom_triple(ice, ICE_EEP2_GPIO_DIR);
1982
1983         return 0;
1984 }
1985
1986
1987
1988 static int __devinit snd_vt1724_chip_init(ice1712_t *ice)
1989 {
1990         outb(VT1724_RESET , ICEREG1724(ice, CONTROL));
1991         udelay(200);
1992         outb(0, ICEREG1724(ice, CONTROL));
1993         udelay(200);
1994         outb(ice->eeprom.data[ICE_EEP2_SYSCONF], ICEREG1724(ice, SYS_CFG));
1995         outb(ice->eeprom.data[ICE_EEP2_ACLINK], ICEREG1724(ice, AC97_CFG));
1996         outb(ice->eeprom.data[ICE_EEP2_I2S], ICEREG1724(ice, I2S_FEATURES));
1997         outb(ice->eeprom.data[ICE_EEP2_SPDIF], ICEREG1724(ice, SPDIF_CFG));
1998
1999         ice->gpio.write_mask = ice->eeprom.gpiomask;
2000         ice->gpio.direction = ice->eeprom.gpiodir;
2001         snd_vt1724_set_gpio_mask(ice, ice->eeprom.gpiomask);
2002         snd_vt1724_set_gpio_dir(ice, ice->eeprom.gpiodir);
2003         snd_vt1724_set_gpio_data(ice, ice->eeprom.gpiostate);
2004
2005         outb(0, ICEREG1724(ice, POWERDOWN));
2006
2007         return 0;
2008 }
2009
2010 static int __devinit snd_vt1724_spdif_build_controls(ice1712_t *ice)
2011 {
2012         int err;
2013         snd_kcontrol_t *kctl;
2014
2015         snd_assert(ice->pcm != NULL, return -EIO);
2016
2017         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_spdif_route, ice));
2018         if (err < 0)
2019                 return err;
2020
2021         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_spdif_switch, ice));
2022         if (err < 0)
2023                 return err;
2024
2025         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_default, ice));
2026         if (err < 0)
2027                 return err;
2028         kctl->id.device = ice->pcm->device;
2029         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskc, ice));
2030         if (err < 0)
2031                 return err;
2032         kctl->id.device = ice->pcm->device;
2033         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskp, ice));
2034         if (err < 0)
2035                 return err;
2036         kctl->id.device = ice->pcm->device;
2037 #if 0 /* use default only */
2038         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_stream, ice));
2039         if (err < 0)
2040                 return err;
2041         kctl->id.device = ice->pcm->device;
2042         ice->spdif.stream_ctl = kctl;
2043 #endif
2044         return 0;
2045 }
2046
2047
2048 static int __devinit snd_vt1724_build_controls(ice1712_t *ice)
2049 {
2050         int err;
2051
2052         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_eeprom, ice));
2053         if (err < 0)
2054                 return err;
2055         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_internal_clock, ice));
2056         if (err < 0)
2057                 return err;
2058
2059         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_locking, ice));
2060         if (err < 0)
2061                 return err;
2062         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_reset, ice));
2063         if (err < 0)
2064                 return err;
2065
2066         if (ice->num_total_dacs > 0) {
2067                 snd_kcontrol_new_t tmp = snd_vt1724_mixer_pro_analog_route;
2068                 tmp.count = ice->num_total_dacs;
2069                 if (ice->vt1720 && tmp.count > 2)
2070                         tmp.count = 2;
2071                 err = snd_ctl_add(ice->card, snd_ctl_new1(&tmp, ice));
2072                 if (err < 0)
2073                         return err;
2074         }
2075
2076         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_peak, ice));
2077         if (err < 0)
2078                 return err;
2079
2080         return 0;
2081 }
2082
2083 static int snd_vt1724_free(ice1712_t *ice)
2084 {
2085         if (! ice->port)
2086                 goto __hw_end;
2087         /* mask all interrupts */
2088         outb(0xff, ICEMT1724(ice, DMA_INT_MASK));
2089         outb(0xff, ICEREG1724(ice, IRQMASK));
2090         /* --- */
2091       __hw_end:
2092         if (ice->irq >= 0) {
2093                 synchronize_irq(ice->irq);
2094                 free_irq(ice->irq, (void *) ice);
2095         }
2096         pci_release_regions(ice->pci);
2097         snd_ice1712_akm4xxx_free(ice);
2098         pci_disable_device(ice->pci);
2099         kfree(ice);
2100         return 0;
2101 }
2102
2103 static int snd_vt1724_dev_free(snd_device_t *device)
2104 {
2105         ice1712_t *ice = device->device_data;
2106         return snd_vt1724_free(ice);
2107 }
2108
2109 static int __devinit snd_vt1724_create(snd_card_t * card,
2110                                        struct pci_dev *pci,
2111                                        const char *modelname,
2112                                        ice1712_t ** r_ice1712)
2113 {
2114         ice1712_t *ice;
2115         int err;
2116         unsigned char mask;
2117         static snd_device_ops_t ops = {
2118                 .dev_free =     snd_vt1724_dev_free,
2119         };
2120
2121         *r_ice1712 = NULL;
2122
2123         /* enable PCI device */
2124         if ((err = pci_enable_device(pci)) < 0)
2125                 return err;
2126
2127         ice = kzalloc(sizeof(*ice), GFP_KERNEL);
2128         if (ice == NULL) {
2129                 pci_disable_device(pci);
2130                 return -ENOMEM;
2131         }
2132         ice->vt1724 = 1;
2133         spin_lock_init(&ice->reg_lock);
2134         init_MUTEX(&ice->gpio_mutex);
2135         init_MUTEX(&ice->open_mutex);
2136         init_MUTEX(&ice->i2c_mutex);
2137         ice->gpio.set_mask = snd_vt1724_set_gpio_mask;
2138         ice->gpio.set_dir = snd_vt1724_set_gpio_dir;
2139         ice->gpio.set_data = snd_vt1724_set_gpio_data;
2140         ice->gpio.get_data = snd_vt1724_get_gpio_data;
2141         ice->card = card;
2142         ice->pci = pci;
2143         ice->irq = -1;
2144         pci_set_master(pci);
2145         snd_vt1724_proc_init(ice);
2146         synchronize_irq(pci->irq);
2147
2148         if ((err = pci_request_regions(pci, "ICE1724")) < 0) {
2149                 kfree(ice);
2150                 pci_disable_device(pci);
2151                 return err;
2152         }
2153         ice->port = pci_resource_start(pci, 0);
2154         ice->profi_port = pci_resource_start(pci, 1);
2155
2156         if (request_irq(pci->irq, snd_vt1724_interrupt, SA_INTERRUPT|SA_SHIRQ, "ICE1724", (void *) ice)) {
2157                 snd_printk("unable to grab IRQ %d\n", pci->irq);
2158                 snd_vt1724_free(ice);
2159                 return -EIO;
2160         }
2161
2162         ice->irq = pci->irq;
2163
2164         if (snd_vt1724_read_eeprom(ice, modelname) < 0) {
2165                 snd_vt1724_free(ice);
2166                 return -EIO;
2167         }
2168         if (snd_vt1724_chip_init(ice) < 0) {
2169                 snd_vt1724_free(ice);
2170                 return -EIO;
2171         }
2172
2173         /* unmask used interrupts */
2174         if (! (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401))
2175                 mask = VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX;
2176         else
2177                 mask = 0;
2178         outb(mask, ICEREG1724(ice, IRQMASK));
2179         /* don't handle FIFO overrun/underruns (just yet), since they cause machine lockups */
2180         outb(VT1724_MULTI_FIFO_ERR, ICEMT1724(ice, DMA_INT_MASK));
2181
2182         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ice, &ops)) < 0) {
2183                 snd_vt1724_free(ice);
2184                 return err;
2185         }
2186
2187         snd_card_set_dev(card, &pci->dev);
2188
2189         *r_ice1712 = ice;
2190         return 0;
2191 }
2192
2193
2194 /*
2195  *
2196  * Registration
2197  *
2198  */
2199
2200 static int __devinit snd_vt1724_probe(struct pci_dev *pci,
2201                                       const struct pci_device_id *pci_id)
2202 {
2203         static int dev;
2204         snd_card_t *card;
2205         ice1712_t *ice;
2206         int pcm_dev = 0, err;
2207         struct snd_ice1712_card_info **tbl, *c;
2208
2209         if (dev >= SNDRV_CARDS)
2210                 return -ENODEV;
2211         if (!enable[dev]) {
2212                 dev++;
2213                 return -ENOENT;
2214         }
2215
2216         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2217         if (card == NULL)
2218                 return -ENOMEM;
2219
2220         strcpy(card->driver, "ICE1724");
2221         strcpy(card->shortname, "ICEnsemble ICE1724");
2222         
2223         if ((err = snd_vt1724_create(card, pci, model[dev], &ice)) < 0) {
2224                 snd_card_free(card);
2225                 return err;
2226         }
2227
2228         for (tbl = card_tables; *tbl; tbl++) {
2229                 for (c = *tbl; c->subvendor; c++) {
2230                         if (c->subvendor == ice->eeprom.subvendor) {
2231                                 strcpy(card->shortname, c->name);
2232                                 if (c->driver) /* specific driver? */
2233                                         strcpy(card->driver, c->driver);
2234                                 if (c->chip_init) {
2235                                         if ((err = c->chip_init(ice)) < 0) {
2236                                                 snd_card_free(card);
2237                                                 return err;
2238                                         }
2239                                 }
2240                                 goto __found;
2241                         }
2242                 }
2243         }
2244         c = &no_matched;
2245  __found:
2246
2247         if ((err = snd_vt1724_pcm_profi(ice, pcm_dev++)) < 0) {
2248                 snd_card_free(card);
2249                 return err;
2250         }
2251         
2252         if ((err = snd_vt1724_pcm_spdif(ice, pcm_dev++)) < 0) {
2253                 snd_card_free(card);
2254                 return err;
2255         }
2256         
2257         if ((err = snd_vt1724_pcm_indep(ice, pcm_dev++)) < 0) {
2258                 snd_card_free(card);
2259                 return err;
2260         }
2261
2262         if ((err = snd_vt1724_ac97_mixer(ice)) < 0) {
2263                 snd_card_free(card);
2264                 return err;
2265         }
2266
2267         if ((err = snd_vt1724_build_controls(ice)) < 0) {
2268                 snd_card_free(card);
2269                 return err;
2270         }
2271
2272         if (ice->pcm && ice->has_spdif) { /* has SPDIF I/O */
2273                 if ((err = snd_vt1724_spdif_build_controls(ice)) < 0) {
2274                         snd_card_free(card);
2275                         return err;
2276                 }
2277         }
2278
2279         if (c->build_controls) {
2280                 if ((err = c->build_controls(ice)) < 0) {
2281                         snd_card_free(card);
2282                         return err;
2283                 }
2284         }
2285
2286         if (! c->no_mpu401) {
2287                 if (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401) {
2288                         if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ICE1712,
2289                                                        ICEREG1724(ice, MPU_CTRL), 1,
2290                                                        ice->irq, 0,
2291                                                        &ice->rmidi[0])) < 0) {
2292                                 snd_card_free(card);
2293                                 return err;
2294                         }
2295                 }
2296         }
2297
2298         sprintf(card->longname, "%s at 0x%lx, irq %i",
2299                 card->shortname, ice->port, ice->irq);
2300
2301         if ((err = snd_card_register(card)) < 0) {
2302                 snd_card_free(card);
2303                 return err;
2304         }
2305         pci_set_drvdata(pci, card);
2306         dev++;
2307         return 0;
2308 }
2309
2310 static void __devexit snd_vt1724_remove(struct pci_dev *pci)
2311 {
2312         snd_card_free(pci_get_drvdata(pci));
2313         pci_set_drvdata(pci, NULL);
2314 }
2315
2316 static struct pci_driver driver = {
2317         .name = "ICE1724",
2318         .owner = THIS_MODULE,
2319         .id_table = snd_vt1724_ids,
2320         .probe = snd_vt1724_probe,
2321         .remove = __devexit_p(snd_vt1724_remove),
2322 };
2323
2324 static int __init alsa_card_ice1724_init(void)
2325 {
2326         return pci_register_driver(&driver);
2327 }
2328
2329 static void __exit alsa_card_ice1724_exit(void)
2330 {
2331         pci_unregister_driver(&driver);
2332 }
2333
2334 module_init(alsa_card_ice1724_init)
2335 module_exit(alsa_card_ice1724_exit)