vserver 1.9.3
[linux-2.6.git] / sound / pci / ice1712 / ice1712.c
1 /*
2  *   ALSA driver for ICEnsemble ICE1712 (Envy24)
3  *
4  *      Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */      
21
22 /*
23   NOTES:
24   - spdif nonaudio consumer mode does not work (at least with my
25     Sony STR-DB830)
26 */
27
28 /*
29  * Changes:
30  *
31  *  2002.09.09  Takashi Iwai <tiwai@suse.de>
32  *      split the code to several files.  each low-level routine
33  *      is stored in the local file and called from registration
34  *      function from card_info struct.
35  *
36  *  2002.11.26  James Stafford <jstafford@ampltd.com>
37  *      Added support for VT1724 (Envy24HT)
38  *      I have left out support for 176.4 and 192 KHz for the moment. 
39  *  I also haven't done anything with the internal S/PDIF transmitter or the MPU-401
40  *
41  *  2003.02.20  Taksahi Iwai <tiwai@suse.de>
42  *      Split vt1724 part to an independent driver.
43  *      The GPIO is accessed through the callback functions now.
44  *
45  * 2004.03.31 Doug McLain <nostar@comcast.net>
46  *    Added support for Event Electronics EZ8 card to hoontech.c.
47  */
48
49
50 #include <sound/driver.h>
51 #include <asm/io.h>
52 #include <linux/delay.h>
53 #include <linux/interrupt.h>
54 #include <linux/init.h>
55 #include <linux/pci.h>
56 #include <linux/slab.h>
57 #include <linux/moduleparam.h>
58 #include <sound/core.h>
59 #include <sound/cs8427.h>
60 #include <sound/info.h>
61 #include <sound/mpu401.h>
62 #include <sound/initval.h>
63
64 #include <sound/asoundef.h>
65
66 #include "ice1712.h"
67
68 /* lowlevel routines */
69 #include "delta.h"
70 #include "ews.h"
71 #include "hoontech.h"
72
73 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
74 MODULE_DESCRIPTION("ICEnsemble ICE1712 (Envy24)");
75 MODULE_LICENSE("GPL");
76 MODULE_SUPPORTED_DEVICE("{"
77                HOONTECH_DEVICE_DESC
78                DELTA_DEVICE_DESC
79                EWS_DEVICE_DESC
80                "{ICEnsemble,Generic ICE1712},"
81                "{ICEnsemble,Generic Envy24}}");
82
83 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
84 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
85 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;              /* Enable this card */
86 static char *model[SNDRV_CARDS];
87 static int omni[SNDRV_CARDS];   /* Delta44 & 66 Omni I/O support */
88 static int cs8427_timeout[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 500}; /* CS8427 S/PDIF transciever reset timeout value in msec */
89 static int boot_devs;
90
91 module_param_array(index, int, boot_devs, 0444);
92 MODULE_PARM_DESC(index, "Index value for ICE1712 soundcard.");
93 module_param_array(id, charp, boot_devs, 0444);
94 MODULE_PARM_DESC(id, "ID string for ICE1712 soundcard.");
95 module_param_array(enable, bool, boot_devs, 0444);
96 MODULE_PARM_DESC(enable, "Enable ICE1712 soundcard.");
97 module_param_array(omni, bool, boot_devs, 0444);
98 MODULE_PARM_DESC(omni, "Enable Midiman M-Audio Delta Omni I/O support.");
99 module_param_array(cs8427_timeout, int, boot_devs, 0444);
100 MODULE_PARM_DESC(cs8427_timeout, "Define reset timeout for cs8427 chip in msec resolution.");
101 module_param_array(model, charp, boot_devs, 0444);
102 MODULE_PARM_DESC(model, "Use the given board model.");
103
104 #ifndef PCI_VENDOR_ID_ICE
105 #define PCI_VENDOR_ID_ICE               0x1412
106 #endif
107 #ifndef PCI_DEVICE_ID_ICE_1712
108 #define PCI_DEVICE_ID_ICE_1712          0x1712
109 #endif
110
111 static struct pci_device_id snd_ice1712_ids[] = {
112         { PCI_VENDOR_ID_ICE, PCI_DEVICE_ID_ICE_1712, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },   /* ICE1712 */
113         { 0, }
114 };
115
116 MODULE_DEVICE_TABLE(pci, snd_ice1712_ids);
117
118 static int snd_ice1712_build_pro_mixer(ice1712_t *ice);
119 static int snd_ice1712_build_controls(ice1712_t *ice);
120
121 static int PRO_RATE_LOCKED;
122 static int PRO_RATE_RESET = 1;
123 static unsigned int PRO_RATE_DEFAULT = 44100;
124
125 /*
126  *  Basic I/O
127  */
128  
129 /* check whether the clock mode is spdif-in */
130 static inline int is_spdif_master(ice1712_t *ice)
131 {
132         return (inb(ICEMT(ice, RATE)) & ICE1712_SPDIF_MASTER) ? 1 : 0;
133 }
134
135 static inline int is_pro_rate_locked(ice1712_t *ice)
136 {
137         return is_spdif_master(ice) || PRO_RATE_LOCKED;
138 }
139
140 static inline void snd_ice1712_ds_write(ice1712_t * ice, u8 channel, u8 addr, u32 data)
141 {
142         outb((channel << 4) | addr, ICEDS(ice, INDEX));
143         outl(data, ICEDS(ice, DATA));
144 }
145
146 static inline u32 snd_ice1712_ds_read(ice1712_t * ice, u8 channel, u8 addr)
147 {
148         outb((channel << 4) | addr, ICEDS(ice, INDEX));
149         return inl(ICEDS(ice, DATA));
150 }
151
152 static void snd_ice1712_ac97_write(ac97_t *ac97,
153                                    unsigned short reg,
154                                    unsigned short val)
155 {
156         ice1712_t *ice = (ice1712_t *)ac97->private_data;
157         int tm;
158         unsigned char old_cmd = 0;
159
160         for (tm = 0; tm < 0x10000; tm++) {
161                 old_cmd = inb(ICEREG(ice, AC97_CMD));
162                 if (old_cmd & (ICE1712_AC97_WRITE | ICE1712_AC97_READ))
163                         continue;
164                 if (!(old_cmd & ICE1712_AC97_READY))
165                         continue;
166                 break;
167         }
168         outb(reg, ICEREG(ice, AC97_INDEX));
169         outw(val, ICEREG(ice, AC97_DATA));
170         old_cmd &= ~(ICE1712_AC97_PBK_VSR | ICE1712_AC97_CAP_VSR);
171         outb(old_cmd | ICE1712_AC97_WRITE, ICEREG(ice, AC97_CMD));
172         for (tm = 0; tm < 0x10000; tm++)
173                 if ((inb(ICEREG(ice, AC97_CMD)) & ICE1712_AC97_WRITE) == 0)
174                         break;
175 }
176
177 static unsigned short snd_ice1712_ac97_read(ac97_t *ac97,
178                                             unsigned short reg)
179 {
180         ice1712_t *ice = (ice1712_t *)ac97->private_data;
181         int tm;
182         unsigned char old_cmd = 0;
183
184         for (tm = 0; tm < 0x10000; tm++) {
185                 old_cmd = inb(ICEREG(ice, AC97_CMD));
186                 if (old_cmd & (ICE1712_AC97_WRITE | ICE1712_AC97_READ))
187                         continue;
188                 if (!(old_cmd & ICE1712_AC97_READY))
189                         continue;
190                 break;
191         }
192         outb(reg, ICEREG(ice, AC97_INDEX));
193         outb(old_cmd | ICE1712_AC97_READ, ICEREG(ice, AC97_CMD));
194         for (tm = 0; tm < 0x10000; tm++)
195                 if ((inb(ICEREG(ice, AC97_CMD)) & ICE1712_AC97_READ) == 0)
196                         break;
197         if (tm >= 0x10000)              /* timeout */
198                 return ~0;
199         return inw(ICEREG(ice, AC97_DATA));
200 }
201
202 /*
203  * pro ac97 section
204  */
205
206 static void snd_ice1712_pro_ac97_write(ac97_t *ac97,
207                                        unsigned short reg,
208                                        unsigned short val)
209 {
210         ice1712_t *ice = (ice1712_t *)ac97->private_data;
211         int tm;
212         unsigned char old_cmd = 0;
213
214         for (tm = 0; tm < 0x10000; tm++) {
215                 old_cmd = inb(ICEMT(ice, AC97_CMD));
216                 if (old_cmd & (ICE1712_AC97_WRITE | ICE1712_AC97_READ))
217                         continue;
218                 if (!(old_cmd & ICE1712_AC97_READY))
219                         continue;
220                 break;
221         }
222         outb(reg, ICEMT(ice, AC97_INDEX));
223         outw(val, ICEMT(ice, AC97_DATA));
224         old_cmd &= ~(ICE1712_AC97_PBK_VSR | ICE1712_AC97_CAP_VSR);
225         outb(old_cmd | ICE1712_AC97_WRITE, ICEMT(ice, AC97_CMD));
226         for (tm = 0; tm < 0x10000; tm++)
227                 if ((inb(ICEMT(ice, AC97_CMD)) & ICE1712_AC97_WRITE) == 0)
228                         break;
229 }
230
231
232 static unsigned short snd_ice1712_pro_ac97_read(ac97_t *ac97,
233                                                 unsigned short reg)
234 {
235         ice1712_t *ice = (ice1712_t *)ac97->private_data;
236         int tm;
237         unsigned char old_cmd = 0;
238
239         for (tm = 0; tm < 0x10000; tm++) {
240                 old_cmd = inb(ICEMT(ice, AC97_CMD));
241                 if (old_cmd & (ICE1712_AC97_WRITE | ICE1712_AC97_READ))
242                         continue;
243                 if (!(old_cmd & ICE1712_AC97_READY))
244                         continue;
245                 break;
246         }
247         outb(reg, ICEMT(ice, AC97_INDEX));
248         outb(old_cmd | ICE1712_AC97_READ, ICEMT(ice, AC97_CMD));
249         for (tm = 0; tm < 0x10000; tm++)
250                 if ((inb(ICEMT(ice, AC97_CMD)) & ICE1712_AC97_READ) == 0)
251                         break;
252         if (tm >= 0x10000)              /* timeout */
253                 return ~0;
254         return inw(ICEMT(ice, AC97_DATA));
255 }
256
257 /*
258  * consumer ac97 digital mix
259  */
260 static int snd_ice1712_digmix_route_ac97_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
261 {
262         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
263         uinfo->count = 1;
264         uinfo->value.integer.min = 0;
265         uinfo->value.integer.max = 1;
266         return 0;
267 }
268
269 static int snd_ice1712_digmix_route_ac97_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
270 {
271         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
272         
273         ucontrol->value.integer.value[0] = inb(ICEMT(ice, MONITOR_ROUTECTRL)) & ICE1712_ROUTE_AC97 ? 1 : 0;
274         return 0;
275 }
276
277 static int snd_ice1712_digmix_route_ac97_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
278 {
279         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
280         unsigned char val, nval;
281         
282         spin_lock_irq(&ice->reg_lock);
283         val = inb(ICEMT(ice, MONITOR_ROUTECTRL));
284         nval = val & ~ICE1712_ROUTE_AC97;
285         if (ucontrol->value.integer.value[0]) nval |= ICE1712_ROUTE_AC97;
286         outb(nval, ICEMT(ice, MONITOR_ROUTECTRL));
287         spin_unlock_irq(&ice->reg_lock);
288         return val != nval;
289 }
290
291 static snd_kcontrol_new_t snd_ice1712_mixer_digmix_route_ac97 __devinitdata = {
292         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
293         .name = "Digital Mixer To AC97",
294         .info = snd_ice1712_digmix_route_ac97_info,
295         .get = snd_ice1712_digmix_route_ac97_get,
296         .put = snd_ice1712_digmix_route_ac97_put,
297 };
298
299
300 /*
301  * gpio operations
302  */
303 static void snd_ice1712_set_gpio_dir(ice1712_t *ice, unsigned int data)
304 {
305         snd_ice1712_write(ice, ICE1712_IREG_GPIO_DIRECTION, data);
306         inb(ICEREG(ice, DATA)); /* dummy read for pci-posting */
307 }
308
309 static void snd_ice1712_set_gpio_mask(ice1712_t *ice, unsigned int data)
310 {
311         snd_ice1712_write(ice, ICE1712_IREG_GPIO_WRITE_MASK, data);
312         inb(ICEREG(ice, DATA)); /* dummy read for pci-posting */
313 }
314
315 static unsigned int snd_ice1712_get_gpio_data(ice1712_t *ice)
316 {
317         return snd_ice1712_read(ice, ICE1712_IREG_GPIO_DATA);
318 }
319
320 static void snd_ice1712_set_gpio_data(ice1712_t *ice, unsigned int val)
321 {
322         snd_ice1712_write(ice, ICE1712_IREG_GPIO_DATA, val);
323         inb(ICEREG(ice, DATA)); /* dummy read for pci-posting */
324 }
325
326
327 /*
328  *
329  * CS8427 interface
330  *
331  */
332
333 /*
334  * change the input clock selection
335  * spdif_clock = 1 - IEC958 input, 0 - Envy24
336  */
337 static int snd_ice1712_cs8427_set_input_clock(ice1712_t *ice, int spdif_clock)
338 {
339         unsigned char reg[2] = { 0x80 | 4, 0 };   /* CS8427 auto increment | register number 4 + data */
340         unsigned char val, nval;
341         int res = 0;
342         
343         snd_i2c_lock(ice->i2c);
344         if (snd_i2c_sendbytes(ice->cs8427, reg, 1) != 1) {
345                 snd_i2c_unlock(ice->i2c);
346                 return -EIO;
347         }
348         if (snd_i2c_readbytes(ice->cs8427, &val, 1) != 1) {
349                 snd_i2c_unlock(ice->i2c);
350                 return -EIO;
351         }
352         nval = val & 0xf0;
353         if (spdif_clock)
354                 nval |= 0x01;
355         else
356                 nval |= 0x04;
357         if (val != nval) {
358                 reg[1] = nval;
359                 if (snd_i2c_sendbytes(ice->cs8427, reg, 2) != 2) {
360                         res = -EIO;
361                 } else {
362                         res++;
363                 }
364         }
365         snd_i2c_unlock(ice->i2c);
366         return res;
367 }
368
369 /*
370  * spdif callbacks
371  */
372 static void open_cs8427(ice1712_t *ice, snd_pcm_substream_t * substream)
373 {
374         snd_cs8427_iec958_active(ice->cs8427, 1);
375 }
376
377 static void close_cs8427(ice1712_t *ice, snd_pcm_substream_t * substream)
378 {
379         snd_cs8427_iec958_active(ice->cs8427, 0);
380 }
381
382 static void setup_cs8427(ice1712_t *ice, int rate)
383 {
384         snd_cs8427_iec958_pcm(ice->cs8427, rate);
385 }
386
387 /*
388  * create and initialize callbacks for cs8427 interface
389  */
390 int __devinit snd_ice1712_init_cs8427(ice1712_t *ice, int addr)
391 {
392         int err;
393
394         if ((err = snd_cs8427_create(ice->i2c, addr,
395                                      (ice->cs8427_timeout * HZ) / 1000,
396                                      &ice->cs8427)) < 0) {
397                 snd_printk("CS8427 initialization failed\n");
398                 return err;
399         }
400         ice->spdif.ops.open = open_cs8427;
401         ice->spdif.ops.close = close_cs8427;
402         ice->spdif.ops.setup_rate = setup_cs8427;
403         return 0;
404 }
405
406
407 /*
408  *  Interrupt handler
409  */
410
411 static irqreturn_t snd_ice1712_interrupt(int irq, void *dev_id, struct pt_regs *regs)
412 {
413         ice1712_t *ice = dev_id;
414         unsigned char status;
415         int handled = 0;
416
417         while (1) {
418                 status = inb(ICEREG(ice, IRQSTAT));
419                 if (status == 0)
420                         break;
421                 handled = 1;
422                 if (status & ICE1712_IRQ_MPU1) {
423                         if (ice->rmidi[0])
424                                 snd_mpu401_uart_interrupt(irq, ice->rmidi[0]->private_data, regs);
425                         outb(ICE1712_IRQ_MPU1, ICEREG(ice, IRQSTAT));
426                         status &= ~ICE1712_IRQ_MPU1;
427                 }
428                 if (status & ICE1712_IRQ_TIMER)
429                         outb(ICE1712_IRQ_TIMER, ICEREG(ice, IRQSTAT));
430                 if (status & ICE1712_IRQ_MPU2) {
431                         if (ice->rmidi[1])
432                                 snd_mpu401_uart_interrupt(irq, ice->rmidi[1]->private_data, regs);
433                         outb(ICE1712_IRQ_MPU2, ICEREG(ice, IRQSTAT));
434                         status &= ~ICE1712_IRQ_MPU2;
435                 }
436                 if (status & ICE1712_IRQ_PROPCM) {
437                         unsigned char mtstat = inb(ICEMT(ice, IRQ));
438                         if (mtstat & ICE1712_MULTI_PBKSTATUS) {
439                                 if (ice->playback_pro_substream)
440                                         snd_pcm_period_elapsed(ice->playback_pro_substream);
441                                 outb(ICE1712_MULTI_PBKSTATUS, ICEMT(ice, IRQ));
442                         }
443                         if (mtstat & ICE1712_MULTI_CAPSTATUS) {
444                                 if (ice->capture_pro_substream)
445                                         snd_pcm_period_elapsed(ice->capture_pro_substream);
446                                 outb(ICE1712_MULTI_CAPSTATUS, ICEMT(ice, IRQ));
447                         }
448                 }
449                 if (status & ICE1712_IRQ_FM)
450                         outb(ICE1712_IRQ_FM, ICEREG(ice, IRQSTAT));
451                 if (status & ICE1712_IRQ_PBKDS) {
452                         u32 idx;
453                         u16 pbkstatus;
454                         snd_pcm_substream_t *substream;
455                         pbkstatus = inw(ICEDS(ice, INTSTAT));
456                         //printk("pbkstatus = 0x%x\n", pbkstatus);
457                         for (idx = 0; idx < 6; idx++) {
458                                 if ((pbkstatus & (3 << (idx * 2))) == 0)
459                                         continue;
460                                 if ((substream = ice->playback_con_substream_ds[idx]) != NULL)
461                                         snd_pcm_period_elapsed(substream);
462                                 outw(3 << (idx * 2), ICEDS(ice, INTSTAT));
463                         }
464                         outb(ICE1712_IRQ_PBKDS, ICEREG(ice, IRQSTAT));
465                 }
466                 if (status & ICE1712_IRQ_CONCAP) {
467                         if (ice->capture_con_substream)
468                                 snd_pcm_period_elapsed(ice->capture_con_substream);
469                         outb(ICE1712_IRQ_CONCAP, ICEREG(ice, IRQSTAT));
470                 }
471                 if (status & ICE1712_IRQ_CONPBK) {
472                         if (ice->playback_con_substream)
473                                 snd_pcm_period_elapsed(ice->playback_con_substream);
474                         outb(ICE1712_IRQ_CONPBK, ICEREG(ice, IRQSTAT));
475                 }
476         }
477         return IRQ_RETVAL(handled);
478 }
479
480
481 /*
482  *  PCM part - misc
483  */
484
485 static int snd_ice1712_hw_params(snd_pcm_substream_t * substream,
486                                  snd_pcm_hw_params_t * hw_params)
487 {
488         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
489 }
490
491 static int snd_ice1712_hw_free(snd_pcm_substream_t * substream)
492 {
493         return snd_pcm_lib_free_pages(substream);
494 }
495
496 /*
497  *  PCM part - consumer I/O
498  */
499
500 static int snd_ice1712_playback_trigger(snd_pcm_substream_t * substream,
501                                         int cmd)
502 {
503         ice1712_t *ice = snd_pcm_substream_chip(substream);
504         int result = 0;
505         u32 tmp;
506         
507         spin_lock(&ice->reg_lock);
508         tmp = snd_ice1712_read(ice, ICE1712_IREG_PBK_CTRL);
509         if (cmd == SNDRV_PCM_TRIGGER_START) {
510                 tmp |= 1;
511         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
512                 tmp &= ~1;
513         } else if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH) {
514                 tmp |= 2;
515         } else if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE) {
516                 tmp &= ~2;
517         } else {
518                 result = -EINVAL;
519         }
520         snd_ice1712_write(ice, ICE1712_IREG_PBK_CTRL, tmp);
521         spin_unlock(&ice->reg_lock);
522         return result;
523 }
524
525 static int snd_ice1712_playback_ds_trigger(snd_pcm_substream_t * substream,
526                                            int cmd)
527 {
528         ice1712_t *ice = snd_pcm_substream_chip(substream);
529         int result = 0;
530         u32 tmp;
531         
532         spin_lock(&ice->reg_lock);
533         tmp = snd_ice1712_ds_read(ice, substream->number * 2, ICE1712_DSC_CONTROL);
534         if (cmd == SNDRV_PCM_TRIGGER_START) {
535                 tmp |= 1;
536         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
537                 tmp &= ~1;
538         } else if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH) {
539                 tmp |= 2;
540         } else if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE) {
541                 tmp &= ~2;
542         } else {
543                 result = -EINVAL;
544         }
545         snd_ice1712_ds_write(ice, substream->number * 2, ICE1712_DSC_CONTROL, tmp);
546         spin_unlock(&ice->reg_lock);
547         return result;
548 }
549
550 static int snd_ice1712_capture_trigger(snd_pcm_substream_t * substream,
551                                        int cmd)
552 {
553         ice1712_t *ice = snd_pcm_substream_chip(substream);
554         int result = 0;
555         u8 tmp;
556         
557         spin_lock(&ice->reg_lock);
558         tmp = snd_ice1712_read(ice, ICE1712_IREG_CAP_CTRL);
559         if (cmd == SNDRV_PCM_TRIGGER_START) {
560                 tmp |= 1;
561         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
562                 tmp &= ~1;
563         } else {
564                 result = -EINVAL;
565         }
566         snd_ice1712_write(ice, ICE1712_IREG_CAP_CTRL, tmp);
567         spin_unlock(&ice->reg_lock);
568         return result;
569 }
570
571 static int snd_ice1712_playback_prepare(snd_pcm_substream_t * substream)
572 {
573         ice1712_t *ice = snd_pcm_substream_chip(substream);
574         snd_pcm_runtime_t *runtime = substream->runtime;
575         u32 period_size, buf_size, rate, tmp;
576
577         period_size = (snd_pcm_lib_period_bytes(substream) >> 2) - 1;
578         buf_size = snd_pcm_lib_buffer_bytes(substream) - 1;
579         tmp = 0x0000;
580         if (snd_pcm_format_width(runtime->format) == 16)
581                 tmp |= 0x10;
582         if (runtime->channels == 2)
583                 tmp |= 0x08;
584         rate = (runtime->rate * 8192) / 375;
585         if (rate > 0x000fffff)
586                 rate = 0x000fffff;
587         spin_lock_irq(&ice->reg_lock);
588         outb(0, ice->ddma_port + 15);
589         outb(ICE1712_DMA_MODE_WRITE | ICE1712_DMA_AUTOINIT, ice->ddma_port + 0x0b);
590         outl(runtime->dma_addr, ice->ddma_port + 0);
591         outw(buf_size, ice->ddma_port + 4);
592         snd_ice1712_write(ice, ICE1712_IREG_PBK_RATE_LO, rate & 0xff);
593         snd_ice1712_write(ice, ICE1712_IREG_PBK_RATE_MID, (rate >> 8) & 0xff);
594         snd_ice1712_write(ice, ICE1712_IREG_PBK_RATE_HI, (rate >> 16) & 0xff);
595         snd_ice1712_write(ice, ICE1712_IREG_PBK_CTRL, tmp);
596         snd_ice1712_write(ice, ICE1712_IREG_PBK_COUNT_LO, period_size & 0xff);
597         snd_ice1712_write(ice, ICE1712_IREG_PBK_COUNT_HI, period_size >> 8);
598         snd_ice1712_write(ice, ICE1712_IREG_PBK_LEFT, 0);
599         snd_ice1712_write(ice, ICE1712_IREG_PBK_RIGHT, 0);
600         spin_unlock_irq(&ice->reg_lock);
601         return 0;
602 }
603
604 static int snd_ice1712_playback_ds_prepare(snd_pcm_substream_t * substream)
605 {
606         ice1712_t *ice = snd_pcm_substream_chip(substream);
607         snd_pcm_runtime_t *runtime = substream->runtime;
608         u32 period_size, buf_size, rate, tmp, chn;
609
610         period_size = snd_pcm_lib_period_bytes(substream) - 1;
611         buf_size = snd_pcm_lib_buffer_bytes(substream) - 1;
612         tmp = 0x0064;
613         if (snd_pcm_format_width(runtime->format) == 16)
614                 tmp &= ~0x04;
615         if (runtime->channels == 2)
616                 tmp |= 0x08;
617         rate = (runtime->rate * 8192) / 375;
618         if (rate > 0x000fffff)
619                 rate = 0x000fffff;
620         ice->playback_con_active_buf[substream->number] = 0;
621         ice->playback_con_virt_addr[substream->number] = runtime->dma_addr;
622         chn = substream->number * 2;
623         spin_lock_irq(&ice->reg_lock);
624         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_ADDR0, runtime->dma_addr);
625         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_COUNT0, period_size);
626         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_ADDR1, runtime->dma_addr + (runtime->periods > 1 ? period_size + 1 : 0));
627         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_COUNT1, period_size);
628         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_RATE, rate);
629         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_VOLUME, 0);
630         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_CONTROL, tmp);
631         if (runtime->channels == 2) {
632                 snd_ice1712_ds_write(ice, chn + 1, ICE1712_DSC_RATE, rate);
633                 snd_ice1712_ds_write(ice, chn + 1, ICE1712_DSC_VOLUME, 0);
634         }
635         spin_unlock_irq(&ice->reg_lock);
636         return 0;
637 }
638
639 static int snd_ice1712_capture_prepare(snd_pcm_substream_t * substream)
640 {
641         ice1712_t *ice = snd_pcm_substream_chip(substream);
642         snd_pcm_runtime_t *runtime = substream->runtime;
643         u32 period_size, buf_size;
644         u8 tmp;
645
646         period_size = (snd_pcm_lib_period_bytes(substream) >> 2) - 1;
647         buf_size = snd_pcm_lib_buffer_bytes(substream) - 1;
648         tmp = 0x06;
649         if (snd_pcm_format_width(runtime->format) == 16)
650                 tmp &= ~0x04;
651         if (runtime->channels == 2)
652                 tmp &= ~0x02;
653         spin_lock_irq(&ice->reg_lock);
654         outl(ice->capture_con_virt_addr = runtime->dma_addr, ICEREG(ice, CONCAP_ADDR));
655         outw(buf_size, ICEREG(ice, CONCAP_COUNT));
656         snd_ice1712_write(ice, ICE1712_IREG_CAP_COUNT_HI, period_size >> 8);
657         snd_ice1712_write(ice, ICE1712_IREG_CAP_COUNT_LO, period_size & 0xff);
658         snd_ice1712_write(ice, ICE1712_IREG_CAP_CTRL, tmp);
659         spin_unlock_irq(&ice->reg_lock);
660         snd_ac97_set_rate(ice->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
661         return 0;
662 }
663
664 static snd_pcm_uframes_t snd_ice1712_playback_pointer(snd_pcm_substream_t * substream)
665 {
666         ice1712_t *ice = snd_pcm_substream_chip(substream);
667         snd_pcm_runtime_t *runtime = substream->runtime;
668         size_t ptr;
669
670         if (!(snd_ice1712_read(ice, ICE1712_IREG_PBK_CTRL) & 1))
671                 return 0;
672         ptr = runtime->buffer_size - inw(ice->ddma_port + 4);
673         if (ptr == runtime->buffer_size)
674                 ptr = 0;
675         return bytes_to_frames(substream->runtime, ptr);
676 }
677
678 static snd_pcm_uframes_t snd_ice1712_playback_ds_pointer(snd_pcm_substream_t * substream)
679 {
680         ice1712_t *ice = snd_pcm_substream_chip(substream);
681         u8 addr;
682         size_t ptr;
683
684         if (!(snd_ice1712_ds_read(ice, substream->number * 2, ICE1712_DSC_CONTROL) & 1))
685                 return 0;
686         if (ice->playback_con_active_buf[substream->number])
687                 addr = ICE1712_DSC_ADDR1;
688         else
689                 addr = ICE1712_DSC_ADDR0;
690         ptr = snd_ice1712_ds_read(ice, substream->number * 2, addr) -
691                 ice->playback_con_virt_addr[substream->number];
692         if (ptr == substream->runtime->buffer_size)
693                 ptr = 0;
694         return bytes_to_frames(substream->runtime, ptr);
695 }
696
697 static snd_pcm_uframes_t snd_ice1712_capture_pointer(snd_pcm_substream_t * substream)
698 {
699         ice1712_t *ice = snd_pcm_substream_chip(substream);
700         size_t ptr;
701
702         if (!(snd_ice1712_read(ice, ICE1712_IREG_CAP_CTRL) & 1))
703                 return 0;
704         ptr = inl(ICEREG(ice, CONCAP_ADDR)) - ice->capture_con_virt_addr;
705         if (ptr == substream->runtime->buffer_size)
706                 ptr = 0;
707         return bytes_to_frames(substream->runtime, ptr);
708 }
709
710 static snd_pcm_hardware_t snd_ice1712_playback =
711 {
712         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
713                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
714                                  SNDRV_PCM_INFO_MMAP_VALID |
715                                  SNDRV_PCM_INFO_PAUSE),
716         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
717         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
718         .rate_min =             4000,
719         .rate_max =             48000,
720         .channels_min =         1,
721         .channels_max =         2,
722         .buffer_bytes_max =     (64*1024),
723         .period_bytes_min =     64,
724         .period_bytes_max =     (64*1024),
725         .periods_min =          1,
726         .periods_max =          1024,
727         .fifo_size =            0,
728 };
729
730 static snd_pcm_hardware_t snd_ice1712_playback_ds =
731 {
732         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
733                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
734                                  SNDRV_PCM_INFO_MMAP_VALID |
735                                  SNDRV_PCM_INFO_PAUSE),
736         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
737         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
738         .rate_min =             4000,
739         .rate_max =             48000,
740         .channels_min =         1,
741         .channels_max =         2,
742         .buffer_bytes_max =     (128*1024),
743         .period_bytes_min =     64,
744         .period_bytes_max =     (128*1024),
745         .periods_min =          2,
746         .periods_max =          2,
747         .fifo_size =            0,
748 };
749
750 static snd_pcm_hardware_t snd_ice1712_capture =
751 {
752         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
753                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
754                                  SNDRV_PCM_INFO_MMAP_VALID),
755         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
756         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
757         .rate_min =             4000,
758         .rate_max =             48000,
759         .channels_min =         1,
760         .channels_max =         2,
761         .buffer_bytes_max =     (64*1024),
762         .period_bytes_min =     64,
763         .period_bytes_max =     (64*1024),
764         .periods_min =          1,
765         .periods_max =          1024,
766         .fifo_size =            0,
767 };
768
769 static int snd_ice1712_playback_open(snd_pcm_substream_t * substream)
770 {
771         snd_pcm_runtime_t *runtime = substream->runtime;
772         ice1712_t *ice = snd_pcm_substream_chip(substream);
773
774         ice->playback_con_substream = substream;
775         runtime->hw = snd_ice1712_playback;
776         return 0;
777 }
778
779 static int snd_ice1712_playback_ds_open(snd_pcm_substream_t * substream)
780 {
781         snd_pcm_runtime_t *runtime = substream->runtime;
782         ice1712_t *ice = snd_pcm_substream_chip(substream);
783         u32 tmp;
784
785         ice->playback_con_substream_ds[substream->number] = substream;
786         runtime->hw = snd_ice1712_playback_ds;
787         spin_lock_irq(&ice->reg_lock); 
788         tmp = inw(ICEDS(ice, INTMASK)) & ~(1 << (substream->number * 2));
789         outw(tmp, ICEDS(ice, INTMASK));
790         spin_unlock_irq(&ice->reg_lock);
791         return 0;
792 }
793
794 static int snd_ice1712_capture_open(snd_pcm_substream_t * substream)
795 {
796         snd_pcm_runtime_t *runtime = substream->runtime;
797         ice1712_t *ice = snd_pcm_substream_chip(substream);
798
799         ice->capture_con_substream = substream;
800         runtime->hw = snd_ice1712_capture;
801         runtime->hw.rates = ice->ac97->rates[AC97_RATES_ADC];
802         if (!(runtime->hw.rates & SNDRV_PCM_RATE_8000))
803                 runtime->hw.rate_min = 48000;
804         return 0;
805 }
806
807 static int snd_ice1712_playback_close(snd_pcm_substream_t * substream)
808 {
809         ice1712_t *ice = snd_pcm_substream_chip(substream);
810
811         ice->playback_con_substream = NULL;
812         return 0;
813 }
814
815 static int snd_ice1712_playback_ds_close(snd_pcm_substream_t * substream)
816 {
817         ice1712_t *ice = snd_pcm_substream_chip(substream);
818         u32 tmp;
819
820         spin_lock_irq(&ice->reg_lock); 
821         tmp = inw(ICEDS(ice, INTMASK)) | (3 << (substream->number * 2));
822         outw(tmp, ICEDS(ice, INTMASK));
823         spin_unlock_irq(&ice->reg_lock);
824         ice->playback_con_substream_ds[substream->number] = NULL;
825         return 0;
826 }
827
828 static int snd_ice1712_capture_close(snd_pcm_substream_t * substream)
829 {
830         ice1712_t *ice = snd_pcm_substream_chip(substream);
831
832         ice->capture_con_substream = NULL;
833         return 0;
834 }
835
836 static snd_pcm_ops_t snd_ice1712_playback_ops = {
837         .open =         snd_ice1712_playback_open,
838         .close =        snd_ice1712_playback_close,
839         .ioctl =        snd_pcm_lib_ioctl,
840         .hw_params =    snd_ice1712_hw_params,
841         .hw_free =      snd_ice1712_hw_free,
842         .prepare =      snd_ice1712_playback_prepare,
843         .trigger =      snd_ice1712_playback_trigger,
844         .pointer =      snd_ice1712_playback_pointer,
845 };
846
847 static snd_pcm_ops_t snd_ice1712_playback_ds_ops = {
848         .open =         snd_ice1712_playback_ds_open,
849         .close =        snd_ice1712_playback_ds_close,
850         .ioctl =        snd_pcm_lib_ioctl,
851         .hw_params =    snd_ice1712_hw_params,
852         .hw_free =      snd_ice1712_hw_free,
853         .prepare =      snd_ice1712_playback_ds_prepare,
854         .trigger =      snd_ice1712_playback_ds_trigger,
855         .pointer =      snd_ice1712_playback_ds_pointer,
856 };
857
858 static snd_pcm_ops_t snd_ice1712_capture_ops = {
859         .open =         snd_ice1712_capture_open,
860         .close =        snd_ice1712_capture_close,
861         .ioctl =        snd_pcm_lib_ioctl,
862         .hw_params =    snd_ice1712_hw_params,
863         .hw_free =      snd_ice1712_hw_free,
864         .prepare =      snd_ice1712_capture_prepare,
865         .trigger =      snd_ice1712_capture_trigger,
866         .pointer =      snd_ice1712_capture_pointer,
867 };
868
869 static void snd_ice1712_pcm_free(snd_pcm_t *pcm)
870 {
871         ice1712_t *ice = pcm->private_data;
872         ice->pcm = NULL;
873         snd_pcm_lib_preallocate_free_for_all(pcm);
874 }
875
876 static int __devinit snd_ice1712_pcm(ice1712_t * ice, int device, snd_pcm_t ** rpcm)
877 {
878         snd_pcm_t *pcm;
879         int err;
880
881         if (rpcm)
882                 *rpcm = NULL;
883         err = snd_pcm_new(ice->card, "ICE1712 consumer", device, 1, 1, &pcm);
884         if (err < 0)
885                 return err;
886
887         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ice1712_playback_ops);
888         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ice1712_capture_ops);
889
890         pcm->private_data = ice;
891         pcm->private_free = snd_ice1712_pcm_free;
892         pcm->info_flags = 0;
893         strcpy(pcm->name, "ICE1712 consumer");
894         ice->pcm = pcm;
895
896         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
897                                               snd_dma_pci_data(ice->pci), 64*1024, 64*1024);
898
899         if (rpcm)
900                 *rpcm = pcm;
901
902         printk(KERN_WARNING "Consumer PCM code does not work well at the moment --jk\n");
903
904         return 0;
905 }
906
907 static void snd_ice1712_pcm_free_ds(snd_pcm_t *pcm)
908 {
909         ice1712_t *ice = pcm->private_data;
910         ice->pcm_ds = NULL;
911         snd_pcm_lib_preallocate_free_for_all(pcm);
912 }
913
914 static int __devinit snd_ice1712_pcm_ds(ice1712_t * ice, int device, snd_pcm_t ** rpcm)
915 {
916         snd_pcm_t *pcm;
917         int err;
918
919         if (rpcm)
920                 *rpcm = NULL;
921         err = snd_pcm_new(ice->card, "ICE1712 consumer (DS)", device, 6, 0, &pcm);
922         if (err < 0)
923                 return err;
924
925         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ice1712_playback_ds_ops);
926
927         pcm->private_data = ice;
928         pcm->private_free = snd_ice1712_pcm_free_ds;
929         pcm->info_flags = 0;
930         strcpy(pcm->name, "ICE1712 consumer (DS)");
931         ice->pcm_ds = pcm;
932
933         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
934                                               snd_dma_pci_data(ice->pci), 64*1024, 128*1024);
935
936         if (rpcm)
937                 *rpcm = pcm;
938
939         return 0;
940 }
941
942 /*
943  *  PCM code - professional part (multitrack)
944  */
945
946 static unsigned int rates[] = { 8000, 9600, 11025, 12000, 16000, 22050, 24000,
947                                 32000, 44100, 48000, 64000, 88200, 96000 };
948
949 static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
950         .count = ARRAY_SIZE(rates),
951         .list = rates,
952         .mask = 0,
953 };
954
955 static int snd_ice1712_pro_trigger(snd_pcm_substream_t *substream,
956                                    int cmd)
957 {
958         ice1712_t *ice = snd_pcm_substream_chip(substream);
959         switch (cmd) {
960         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
961         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
962         {
963                 unsigned int what;
964                 unsigned int old;
965                 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
966                         return -EINVAL;
967                 what = ICE1712_PLAYBACK_PAUSE;
968                 snd_pcm_trigger_done(substream, substream);
969                 spin_lock(&ice->reg_lock);
970                 old = inl(ICEMT(ice, PLAYBACK_CONTROL));
971                 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
972                         old |= what;
973                 else
974                         old &= ~what;
975                 outl(old, ICEMT(ice, PLAYBACK_CONTROL));
976                 spin_unlock(&ice->reg_lock);
977                 break;
978         }
979         case SNDRV_PCM_TRIGGER_START:
980         case SNDRV_PCM_TRIGGER_STOP:
981         {
982                 unsigned int what = 0;
983                 unsigned int old;
984                 struct list_head *pos;
985                 snd_pcm_substream_t *s;
986
987                 snd_pcm_group_for_each(pos, substream) {
988                         s = snd_pcm_group_substream_entry(pos);
989                         if (s == ice->playback_pro_substream) {
990                                 what |= ICE1712_PLAYBACK_START;
991                                 snd_pcm_trigger_done(s, substream);
992                         } else if (s == ice->capture_pro_substream) {
993                                 what |= ICE1712_CAPTURE_START_SHADOW;
994                                 snd_pcm_trigger_done(s, substream);
995                         }
996                 }
997                 spin_lock(&ice->reg_lock);
998                 old = inl(ICEMT(ice, PLAYBACK_CONTROL));
999                 if (cmd == SNDRV_PCM_TRIGGER_START)
1000                         old |= what;
1001                 else
1002                         old &= ~what;
1003                 outl(old, ICEMT(ice, PLAYBACK_CONTROL));
1004                 spin_unlock(&ice->reg_lock);
1005                 break;
1006         }
1007         default:
1008                 return -EINVAL;
1009         }
1010         return 0;
1011 }
1012
1013 /*
1014  */
1015 static void snd_ice1712_set_pro_rate(ice1712_t *ice, unsigned int rate, int force)
1016 {
1017         unsigned long flags;
1018         unsigned char val;
1019         unsigned int i;
1020
1021         spin_lock_irqsave(&ice->reg_lock, flags);
1022         if (inb(ICEMT(ice, PLAYBACK_CONTROL)) & (ICE1712_CAPTURE_START_SHADOW|
1023                                                  ICE1712_PLAYBACK_PAUSE|
1024                                                  ICE1712_PLAYBACK_START)) {
1025                 spin_unlock_irqrestore(&ice->reg_lock, flags);
1026                 return;
1027         }
1028         if (!force && is_pro_rate_locked(ice)) {
1029                 spin_unlock_irqrestore(&ice->reg_lock, flags);
1030                 return;
1031         }
1032
1033         switch (rate) {
1034         case 8000: val = 6; break;
1035         case 9600: val = 3; break;
1036         case 11025: val = 10; break;
1037         case 12000: val = 2; break;
1038         case 16000: val = 5; break;
1039         case 22050: val = 9; break;
1040         case 24000: val = 1; break;
1041         case 32000: val = 4; break;
1042         case 44100: val = 8; break;
1043         case 48000: val = 0; break;
1044         case 64000: val = 15; break;
1045         case 88200: val = 11; break;
1046         case 96000: val = 7; break;
1047         default:
1048                 snd_BUG();
1049                 val = 0;
1050                 rate = 48000;
1051                 break;
1052         }
1053         outb(val, ICEMT(ice, RATE));
1054
1055         spin_unlock_irqrestore(&ice->reg_lock, flags);
1056
1057         if (ice->gpio.set_pro_rate)
1058                 ice->gpio.set_pro_rate(ice, rate);
1059         for (i = 0; i < ice->akm_codecs; i++) {
1060                 if (ice->akm[i].ops.set_rate_val)
1061                         ice->akm[i].ops.set_rate_val(&ice->akm[i], rate);
1062         }
1063         if (ice->spdif.ops.setup_rate)
1064                 ice->spdif.ops.setup_rate(ice, rate);
1065 }
1066
1067 static int snd_ice1712_playback_pro_prepare(snd_pcm_substream_t * substream)
1068 {
1069         ice1712_t *ice = snd_pcm_substream_chip(substream);
1070
1071         ice->playback_pro_size = snd_pcm_lib_buffer_bytes(substream);
1072         spin_lock_irq(&ice->reg_lock);
1073         outl(substream->runtime->dma_addr, ICEMT(ice, PLAYBACK_ADDR));
1074         outw((ice->playback_pro_size >> 2) - 1, ICEMT(ice, PLAYBACK_SIZE));
1075         outw((snd_pcm_lib_period_bytes(substream) >> 2) - 1, ICEMT(ice, PLAYBACK_COUNT));
1076         spin_unlock_irq(&ice->reg_lock);
1077
1078         return 0;
1079 }
1080
1081 static int snd_ice1712_playback_pro_hw_params(snd_pcm_substream_t * substream,
1082                                               snd_pcm_hw_params_t * hw_params)
1083 {
1084         ice1712_t *ice = snd_pcm_substream_chip(substream);
1085
1086         snd_ice1712_set_pro_rate(ice, params_rate(hw_params), 0);
1087         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1088 }
1089
1090 static int snd_ice1712_capture_pro_prepare(snd_pcm_substream_t * substream)
1091 {
1092         ice1712_t *ice = snd_pcm_substream_chip(substream);
1093
1094         ice->capture_pro_size = snd_pcm_lib_buffer_bytes(substream);
1095         spin_lock_irq(&ice->reg_lock);
1096         outl(substream->runtime->dma_addr, ICEMT(ice, CAPTURE_ADDR));
1097         outw((ice->capture_pro_size >> 2) - 1, ICEMT(ice, CAPTURE_SIZE));
1098         outw((snd_pcm_lib_period_bytes(substream) >> 2) - 1, ICEMT(ice, CAPTURE_COUNT));
1099         spin_unlock_irq(&ice->reg_lock);
1100         return 0;
1101 }
1102
1103 static int snd_ice1712_capture_pro_hw_params(snd_pcm_substream_t * substream,
1104                                              snd_pcm_hw_params_t * hw_params)
1105 {
1106         ice1712_t *ice = snd_pcm_substream_chip(substream);
1107
1108         snd_ice1712_set_pro_rate(ice, params_rate(hw_params), 0);
1109         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1110 }
1111
1112 static snd_pcm_uframes_t snd_ice1712_playback_pro_pointer(snd_pcm_substream_t * substream)
1113 {
1114         ice1712_t *ice = snd_pcm_substream_chip(substream);
1115         size_t ptr;
1116
1117         if (!(inl(ICEMT(ice, PLAYBACK_CONTROL)) & ICE1712_PLAYBACK_START))
1118                 return 0;
1119         ptr = ice->playback_pro_size - (inw(ICEMT(ice, PLAYBACK_SIZE)) << 2);
1120         if (ptr == substream->runtime->buffer_size)
1121                 ptr = 0;
1122         return bytes_to_frames(substream->runtime, ptr);
1123 }
1124
1125 static snd_pcm_uframes_t snd_ice1712_capture_pro_pointer(snd_pcm_substream_t * substream)
1126 {
1127         ice1712_t *ice = snd_pcm_substream_chip(substream);
1128         size_t ptr;
1129
1130         if (!(inl(ICEMT(ice, PLAYBACK_CONTROL)) & ICE1712_CAPTURE_START_SHADOW))
1131                 return 0;
1132         ptr = ice->capture_pro_size - (inw(ICEMT(ice, CAPTURE_SIZE)) << 2);
1133         if (ptr == substream->runtime->buffer_size)
1134                 ptr = 0;
1135         return bytes_to_frames(substream->runtime, ptr);
1136 }
1137
1138 static snd_pcm_hardware_t snd_ice1712_playback_pro =
1139 {
1140         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1141                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1142                                  SNDRV_PCM_INFO_MMAP_VALID |
1143                                  SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
1144         .formats =              SNDRV_PCM_FMTBIT_S32_LE,
1145         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_96000,
1146         .rate_min =             4000,
1147         .rate_max =             96000,
1148         .channels_min =         10,
1149         .channels_max =         10,
1150         .buffer_bytes_max =     (256*1024),
1151         .period_bytes_min =     10 * 4 * 2,
1152         .period_bytes_max =     131040,
1153         .periods_min =          1,
1154         .periods_max =          1024,
1155         .fifo_size =            0,
1156 };
1157
1158 static snd_pcm_hardware_t snd_ice1712_capture_pro =
1159 {
1160         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1161                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1162                                  SNDRV_PCM_INFO_MMAP_VALID |
1163                                  SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
1164         .formats =              SNDRV_PCM_FMTBIT_S32_LE,
1165         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_96000,
1166         .rate_min =             4000,
1167         .rate_max =             96000,
1168         .channels_min =         12,
1169         .channels_max =         12,
1170         .buffer_bytes_max =     (256*1024),
1171         .period_bytes_min =     12 * 4 * 2,
1172         .period_bytes_max =     131040,
1173         .periods_min =          1,
1174         .periods_max =          1024,
1175         .fifo_size =            0,
1176 };
1177
1178 static int snd_ice1712_playback_pro_open(snd_pcm_substream_t * substream)
1179 {
1180         snd_pcm_runtime_t *runtime = substream->runtime;
1181         ice1712_t *ice = snd_pcm_substream_chip(substream);
1182
1183         ice->playback_pro_substream = substream;
1184         runtime->hw = snd_ice1712_playback_pro;
1185         snd_pcm_set_sync(substream);
1186         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1187         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1188
1189         if (ice->spdif.ops.open)
1190                 ice->spdif.ops.open(ice, substream);
1191
1192         return 0;
1193 }
1194
1195 static int snd_ice1712_capture_pro_open(snd_pcm_substream_t * substream)
1196 {
1197         ice1712_t *ice = snd_pcm_substream_chip(substream);
1198         snd_pcm_runtime_t *runtime = substream->runtime;
1199
1200         ice->capture_pro_substream = substream;
1201         runtime->hw = snd_ice1712_capture_pro;
1202         snd_pcm_set_sync(substream);
1203         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1204         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1205         return 0;
1206 }
1207
1208 static int snd_ice1712_playback_pro_close(snd_pcm_substream_t * substream)
1209 {
1210         ice1712_t *ice = snd_pcm_substream_chip(substream);
1211
1212         if (PRO_RATE_RESET)
1213                 snd_ice1712_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
1214         ice->playback_pro_substream = NULL;
1215         if (ice->spdif.ops.close)
1216                 ice->spdif.ops.close(ice, substream);
1217
1218         return 0;
1219 }
1220
1221 static int snd_ice1712_capture_pro_close(snd_pcm_substream_t * substream)
1222 {
1223         ice1712_t *ice = snd_pcm_substream_chip(substream);
1224
1225         if (PRO_RATE_RESET)
1226                 snd_ice1712_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
1227         ice->capture_pro_substream = NULL;
1228         return 0;
1229 }
1230
1231 static void snd_ice1712_pcm_profi_free(snd_pcm_t *pcm)
1232 {
1233         ice1712_t *ice = pcm->private_data;
1234         ice->pcm_pro = NULL;
1235         snd_pcm_lib_preallocate_free_for_all(pcm);
1236 }
1237
1238 static snd_pcm_ops_t snd_ice1712_playback_pro_ops = {
1239         .open =         snd_ice1712_playback_pro_open,
1240         .close =        snd_ice1712_playback_pro_close,
1241         .ioctl =        snd_pcm_lib_ioctl,
1242         .hw_params =    snd_ice1712_playback_pro_hw_params,
1243         .hw_free =      snd_ice1712_hw_free,
1244         .prepare =      snd_ice1712_playback_pro_prepare,
1245         .trigger =      snd_ice1712_pro_trigger,
1246         .pointer =      snd_ice1712_playback_pro_pointer,
1247 };
1248
1249 static snd_pcm_ops_t snd_ice1712_capture_pro_ops = {
1250         .open =         snd_ice1712_capture_pro_open,
1251         .close =        snd_ice1712_capture_pro_close,
1252         .ioctl =        snd_pcm_lib_ioctl,
1253         .hw_params =    snd_ice1712_capture_pro_hw_params,
1254         .hw_free =      snd_ice1712_hw_free,
1255         .prepare =      snd_ice1712_capture_pro_prepare,
1256         .trigger =      snd_ice1712_pro_trigger,
1257         .pointer =      snd_ice1712_capture_pro_pointer,
1258 };
1259
1260 static int __devinit snd_ice1712_pcm_profi(ice1712_t * ice, int device, snd_pcm_t ** rpcm)
1261 {
1262         snd_pcm_t *pcm;
1263         int err;
1264
1265         if (rpcm)
1266                 *rpcm = NULL;
1267         err = snd_pcm_new(ice->card, "ICE1712 multi", device, 1, 1, &pcm);
1268         if (err < 0)
1269                 return err;
1270
1271         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ice1712_playback_pro_ops);
1272         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ice1712_capture_pro_ops);
1273
1274         pcm->private_data = ice;
1275         pcm->private_free = snd_ice1712_pcm_profi_free;
1276         pcm->info_flags = 0;
1277         strcpy(pcm->name, "ICE1712 multi");
1278
1279         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1280                                               snd_dma_pci_data(ice->pci), 256*1024, 256*1024);
1281
1282         ice->pcm_pro = pcm;
1283         if (rpcm)
1284                 *rpcm = pcm;
1285         
1286         if (ice->cs8427) {
1287                 /* assign channels to iec958 */
1288                 err = snd_cs8427_iec958_build(ice->cs8427,
1289                                               pcm->streams[0].substream,
1290                                               pcm->streams[1].substream);
1291                 if (err < 0)
1292                         return err;
1293         }
1294
1295         if ((err = snd_ice1712_build_pro_mixer(ice)) < 0)
1296                 return err;
1297         return 0;
1298 }
1299
1300 /*
1301  *  Mixer section
1302  */
1303
1304 static void snd_ice1712_update_volume(ice1712_t *ice, int index)
1305 {
1306         unsigned int vol = ice->pro_volumes[index];
1307         unsigned short val = 0;
1308
1309         val |= (vol & 0x8000) == 0 ? (96 - (vol & 0x7f)) : 0x7f;
1310         val |= ((vol & 0x80000000) == 0 ? (96 - ((vol >> 16) & 0x7f)) : 0x7f) << 8;
1311         outb(index, ICEMT(ice, MONITOR_INDEX));
1312         outw(val, ICEMT(ice, MONITOR_VOLUME));
1313 }
1314
1315 static int snd_ice1712_pro_mixer_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1316 {
1317         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1318         uinfo->count = 2;
1319         uinfo->value.integer.min = 0;
1320         uinfo->value.integer.max = 1;
1321         return 0;
1322 }
1323
1324 static int snd_ice1712_pro_mixer_switch_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1325 {
1326         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1327         int index = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + kcontrol->private_value;
1328         
1329         spin_lock_irq(&ice->reg_lock);
1330         ucontrol->value.integer.value[0] = !((ice->pro_volumes[index] >> 15) & 1);
1331         ucontrol->value.integer.value[1] = !((ice->pro_volumes[index] >> 31) & 1);
1332         spin_unlock_irq(&ice->reg_lock);
1333         return 0;
1334 }
1335
1336 static int snd_ice1712_pro_mixer_switch_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1337 {
1338         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1339         int index = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + kcontrol->private_value;
1340         unsigned int nval, change;
1341
1342         nval = (ucontrol->value.integer.value[0] ? 0 : 0x00008000) |
1343                (ucontrol->value.integer.value[1] ? 0 : 0x80000000);
1344         spin_lock_irq(&ice->reg_lock);
1345         nval |= ice->pro_volumes[index] & ~0x80008000;
1346         change = nval != ice->pro_volumes[index];
1347         ice->pro_volumes[index] = nval;
1348         snd_ice1712_update_volume(ice, index);
1349         spin_unlock_irq(&ice->reg_lock);
1350         return change;
1351 }
1352
1353 static int snd_ice1712_pro_mixer_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1354 {
1355         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1356         uinfo->count = 2;
1357         uinfo->value.integer.min = 0;
1358         uinfo->value.integer.max = 96;
1359         return 0;
1360 }
1361
1362 static int snd_ice1712_pro_mixer_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1363 {
1364         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1365         int index = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + kcontrol->private_value;
1366         
1367         spin_lock_irq(&ice->reg_lock);
1368         ucontrol->value.integer.value[0] = (ice->pro_volumes[index] >> 0) & 127;
1369         ucontrol->value.integer.value[1] = (ice->pro_volumes[index] >> 16) & 127;
1370         spin_unlock_irq(&ice->reg_lock);
1371         return 0;
1372 }
1373
1374 static int snd_ice1712_pro_mixer_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1375 {
1376         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1377         int index = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + kcontrol->private_value;
1378         unsigned int nval, change;
1379
1380         nval = (ucontrol->value.integer.value[0] & 127) |
1381                ((ucontrol->value.integer.value[1] & 127) << 16);
1382         spin_lock_irq(&ice->reg_lock);
1383         nval |= ice->pro_volumes[index] & ~0x007f007f;
1384         change = nval != ice->pro_volumes[index];
1385         ice->pro_volumes[index] = nval;
1386         snd_ice1712_update_volume(ice, index);
1387         spin_unlock_irq(&ice->reg_lock);
1388         return change;
1389 }
1390
1391
1392 static snd_kcontrol_new_t snd_ice1712_multi_playback_ctrls[] __devinitdata = {
1393         {
1394                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1395                 .name = "Multi Playback Switch",
1396                 .info = snd_ice1712_pro_mixer_switch_info,
1397                 .get = snd_ice1712_pro_mixer_switch_get,
1398                 .put = snd_ice1712_pro_mixer_switch_put,
1399                 .private_value = 0,
1400                 .count = 10,
1401         },
1402         {
1403                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1404                 .name = "Multi Playback Volume",
1405                 .info = snd_ice1712_pro_mixer_volume_info,
1406                 .get = snd_ice1712_pro_mixer_volume_get,
1407                 .put = snd_ice1712_pro_mixer_volume_put,
1408                 .private_value = 0,
1409                 .count = 10,
1410         },
1411 };
1412
1413 static snd_kcontrol_new_t snd_ice1712_multi_capture_analog_switch __devinitdata = {
1414         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1415         .name = "H/W Multi Capture Switch",
1416         .info = snd_ice1712_pro_mixer_switch_info,
1417         .get = snd_ice1712_pro_mixer_switch_get,
1418         .put = snd_ice1712_pro_mixer_switch_put,
1419         .private_value = 10,
1420 };
1421
1422 static snd_kcontrol_new_t snd_ice1712_multi_capture_spdif_switch __devinitdata = {
1423         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1424         .name = "IEC958 Multi Capture Switch",
1425         .info = snd_ice1712_pro_mixer_switch_info,
1426         .get = snd_ice1712_pro_mixer_switch_get,
1427         .put = snd_ice1712_pro_mixer_switch_put,
1428         .private_value = 18,
1429         .count = 2,
1430 };
1431
1432 static snd_kcontrol_new_t snd_ice1712_multi_capture_analog_volume __devinitdata = {
1433         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1434         .name = "H/W Multi Capture Volume",
1435         .info = snd_ice1712_pro_mixer_volume_info,
1436         .get = snd_ice1712_pro_mixer_volume_get,
1437         .put = snd_ice1712_pro_mixer_volume_put,
1438         .private_value = 10,
1439 };
1440
1441 static snd_kcontrol_new_t snd_ice1712_multi_capture_spdif_volume __devinitdata = {
1442         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1443         .name = "IEC958 Multi Capture Volume",
1444         .info = snd_ice1712_pro_mixer_volume_info,
1445         .get = snd_ice1712_pro_mixer_volume_get,
1446         .put = snd_ice1712_pro_mixer_volume_put,
1447         .private_value = 18,
1448         .count = 2,
1449 };
1450
1451 static int __devinit snd_ice1712_build_pro_mixer(ice1712_t *ice)
1452 {
1453         snd_card_t * card = ice->card;
1454         unsigned int idx;
1455         int err;
1456
1457         /* multi-channel mixer */
1458         for (idx = 0; idx < ARRAY_SIZE(snd_ice1712_multi_playback_ctrls); idx++) {
1459                 err = snd_ctl_add(card, snd_ctl_new1(&snd_ice1712_multi_playback_ctrls[idx], ice));
1460                 if (err < 0)
1461                         return err;
1462         }
1463         
1464         if (ice->num_total_adcs > 0) {
1465                 snd_kcontrol_new_t tmp = snd_ice1712_multi_capture_analog_switch;
1466                 tmp.count = ice->num_total_adcs;
1467                 err = snd_ctl_add(card, snd_ctl_new1(&tmp, ice));
1468                 if (err < 0)
1469                         return err;
1470         }
1471
1472         err = snd_ctl_add(card, snd_ctl_new1(&snd_ice1712_multi_capture_spdif_switch, ice));
1473         if (err < 0)
1474                 return err;
1475
1476         if (ice->num_total_adcs > 0) {
1477                 snd_kcontrol_new_t tmp = snd_ice1712_multi_capture_analog_volume;
1478                 tmp.count = ice->num_total_adcs;
1479                 err = snd_ctl_add(card, snd_ctl_new1(&tmp, ice));
1480                 if (err < 0)
1481                         return err;
1482         }
1483
1484         err = snd_ctl_add(card, snd_ctl_new1(&snd_ice1712_multi_capture_spdif_volume, ice));
1485         if (err < 0)
1486                 return err;
1487
1488         /* initialize volumes */
1489         for (idx = 0; idx < 10; idx++) {
1490                 ice->pro_volumes[idx] = 0x80008000;     /* mute */
1491                 snd_ice1712_update_volume(ice, idx);
1492         }
1493         for (idx = 10; idx < 10 + ice->num_total_adcs; idx++) {
1494                 ice->pro_volumes[idx] = 0x80008000;     /* mute */
1495                 snd_ice1712_update_volume(ice, idx);
1496         }
1497         for (idx = 18; idx < 20; idx++) {
1498                 ice->pro_volumes[idx] = 0x80008000;     /* mute */
1499                 snd_ice1712_update_volume(ice, idx);
1500         }
1501         return 0;
1502 }
1503
1504 static void snd_ice1712_mixer_free_ac97(ac97_t *ac97)
1505 {
1506         ice1712_t *ice = ac97->private_data;
1507         ice->ac97 = NULL;
1508 }
1509
1510 static int __devinit snd_ice1712_ac97_mixer(ice1712_t * ice)
1511 {
1512         int err, bus_num = 0;
1513         ac97_template_t ac97;
1514         ac97_bus_t *pbus;
1515         static ac97_bus_ops_t con_ops = {
1516                 .write = snd_ice1712_ac97_write,
1517                 .read = snd_ice1712_ac97_read,
1518         };
1519         static ac97_bus_ops_t pro_ops = {
1520                 .write = snd_ice1712_pro_ac97_write,
1521                 .read = snd_ice1712_pro_ac97_read,
1522         };
1523
1524         if (ice_has_con_ac97(ice)) {
1525                 if ((err = snd_ac97_bus(ice->card, bus_num++, &con_ops, NULL, &pbus)) < 0)
1526                         return err;
1527                 memset(&ac97, 0, sizeof(ac97));
1528                 ac97.private_data = ice;
1529                 ac97.private_free = snd_ice1712_mixer_free_ac97;
1530                 if ((err = snd_ac97_mixer(pbus, &ac97, &ice->ac97)) < 0)
1531                         printk(KERN_WARNING "ice1712: cannot initialize ac97 for consumer, skipped\n");
1532                 else {
1533                         if ((err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_mixer_digmix_route_ac97, ice))) < 0)
1534                                 return err;
1535                         return 0;
1536                 }
1537         }
1538
1539         if (! (ice->eeprom.data[ICE_EEP1_ACLINK] & ICE1712_CFG_PRO_I2S)) {
1540                 if ((err = snd_ac97_bus(ice->card, bus_num, &pro_ops, NULL, &pbus)) < 0)
1541                         return err;
1542                 memset(&ac97, 0, sizeof(ac97));
1543                 ac97.private_data = ice;
1544                 ac97.private_free = snd_ice1712_mixer_free_ac97;
1545                 if ((err = snd_ac97_mixer(pbus, &ac97, &ice->ac97)) < 0)
1546                         printk(KERN_WARNING "ice1712: cannot initialize pro ac97, skipped\n");
1547                 else
1548                         return 0;
1549         }
1550         /* I2S mixer only */
1551         strcat(ice->card->mixername, "ICE1712 - multitrack");
1552         return 0;
1553 }
1554
1555 /*
1556  *
1557  */
1558
1559 static inline unsigned int eeprom_double(ice1712_t *ice, int idx)
1560 {
1561         return (unsigned int)ice->eeprom.data[idx] | ((unsigned int)ice->eeprom.data[idx + 1] << 8);
1562 }
1563
1564 static void snd_ice1712_proc_read(snd_info_entry_t *entry, 
1565                                   snd_info_buffer_t * buffer)
1566 {
1567         ice1712_t *ice = entry->private_data;
1568         unsigned int idx;
1569
1570         snd_iprintf(buffer, "%s\n\n", ice->card->longname);
1571         snd_iprintf(buffer, "EEPROM:\n");
1572
1573         snd_iprintf(buffer, "  Subvendor        : 0x%x\n", ice->eeprom.subvendor);
1574         snd_iprintf(buffer, "  Size             : %i bytes\n", ice->eeprom.size);
1575         snd_iprintf(buffer, "  Version          : %i\n", ice->eeprom.version);
1576         snd_iprintf(buffer, "  Codec            : 0x%x\n", ice->eeprom.data[ICE_EEP1_CODEC]);
1577         snd_iprintf(buffer, "  ACLink           : 0x%x\n", ice->eeprom.data[ICE_EEP1_ACLINK]);
1578         snd_iprintf(buffer, "  I2S ID           : 0x%x\n", ice->eeprom.data[ICE_EEP1_I2SID]);
1579         snd_iprintf(buffer, "  S/PDIF           : 0x%x\n", ice->eeprom.data[ICE_EEP1_SPDIF]);
1580         snd_iprintf(buffer, "  GPIO mask        : 0x%x\n", ice->eeprom.gpiomask);
1581         snd_iprintf(buffer, "  GPIO state       : 0x%x\n", ice->eeprom.gpiostate);
1582         snd_iprintf(buffer, "  GPIO direction   : 0x%x\n", ice->eeprom.gpiodir);
1583         snd_iprintf(buffer, "  AC'97 main       : 0x%x\n", eeprom_double(ice, ICE_EEP1_AC97_MAIN_LO));
1584         snd_iprintf(buffer, "  AC'97 pcm        : 0x%x\n", eeprom_double(ice, ICE_EEP1_AC97_PCM_LO));
1585         snd_iprintf(buffer, "  AC'97 record     : 0x%x\n", eeprom_double(ice, ICE_EEP1_AC97_REC_LO));
1586         snd_iprintf(buffer, "  AC'97 record src : 0x%x\n", ice->eeprom.data[ICE_EEP1_AC97_RECSRC]);
1587         for (idx = 0; idx < 4; idx++)
1588                 snd_iprintf(buffer, "  DAC ID #%i        : 0x%x\n", idx, ice->eeprom.data[ICE_EEP1_DAC_ID + idx]);
1589         for (idx = 0; idx < 4; idx++)
1590                 snd_iprintf(buffer, "  ADC ID #%i        : 0x%x\n", idx, ice->eeprom.data[ICE_EEP1_ADC_ID + idx]);
1591         for (idx = 0x1c; idx < ice->eeprom.size; idx++)
1592                 snd_iprintf(buffer, "  Extra #%02i        : 0x%x\n", idx, ice->eeprom.data[idx]);
1593
1594         snd_iprintf(buffer, "\nRegisters:\n");
1595         snd_iprintf(buffer, "  PSDOUT03         : 0x%04x\n", (unsigned)inw(ICEMT(ice, ROUTE_PSDOUT03)));
1596         snd_iprintf(buffer, "  CAPTURE          : 0x%08x\n", inl(ICEMT(ice, ROUTE_CAPTURE)));
1597         snd_iprintf(buffer, "  SPDOUT           : 0x%04x\n", (unsigned)inw(ICEMT(ice, ROUTE_SPDOUT)));
1598         snd_iprintf(buffer, "  RATE             : 0x%02x\n", (unsigned)inb(ICEMT(ice, RATE)));
1599 }
1600
1601 static void __devinit snd_ice1712_proc_init(ice1712_t * ice)
1602 {
1603         snd_info_entry_t *entry;
1604
1605         if (! snd_card_proc_new(ice->card, "ice1712", &entry))
1606                 snd_info_set_text_ops(entry, ice, 1024, snd_ice1712_proc_read);
1607 }
1608
1609 /*
1610  *
1611  */
1612
1613 static int snd_ice1712_eeprom_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1614 {
1615         uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1616         uinfo->count = sizeof(ice1712_eeprom_t);
1617         return 0;
1618 }
1619
1620 static int snd_ice1712_eeprom_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1621 {
1622         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1623         
1624         memcpy(ucontrol->value.bytes.data, &ice->eeprom, sizeof(ice->eeprom));
1625         return 0;
1626 }
1627
1628 static snd_kcontrol_new_t snd_ice1712_eeprom __devinitdata = {
1629         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1630         .name = "ICE1712 EEPROM",
1631         .access = SNDRV_CTL_ELEM_ACCESS_READ,
1632         .info = snd_ice1712_eeprom_info,
1633         .get = snd_ice1712_eeprom_get
1634 };
1635
1636 /*
1637  */
1638 static int snd_ice1712_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1639 {
1640         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1641         uinfo->count = 1;
1642         return 0;
1643 }
1644
1645 static int snd_ice1712_spdif_default_get(snd_kcontrol_t * kcontrol,
1646                                          snd_ctl_elem_value_t * ucontrol)
1647 {
1648         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1649         if (ice->spdif.ops.default_get)
1650                 ice->spdif.ops.default_get(ice, ucontrol); 
1651         return 0;
1652 }
1653
1654 static int snd_ice1712_spdif_default_put(snd_kcontrol_t * kcontrol,
1655                                          snd_ctl_elem_value_t * ucontrol)
1656 {
1657         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1658         if (ice->spdif.ops.default_put)
1659                 return ice->spdif.ops.default_put(ice, ucontrol);
1660         return 0;
1661 }
1662
1663 static snd_kcontrol_new_t snd_ice1712_spdif_default __devinitdata =
1664 {
1665         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1666         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1667         .info =         snd_ice1712_spdif_info,
1668         .get =          snd_ice1712_spdif_default_get,
1669         .put =          snd_ice1712_spdif_default_put
1670 };
1671
1672 static int snd_ice1712_spdif_maskc_get(snd_kcontrol_t * kcontrol,
1673                                        snd_ctl_elem_value_t * ucontrol)
1674 {
1675         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1676         if (ice->spdif.ops.default_get) {
1677                 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1678                                                      IEC958_AES0_PROFESSIONAL |
1679                                                      IEC958_AES0_CON_NOT_COPYRIGHT |
1680                                                      IEC958_AES0_CON_EMPHASIS;
1681                 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL |
1682                                                      IEC958_AES1_CON_CATEGORY;
1683                 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
1684         } else {
1685                 ucontrol->value.iec958.status[0] = 0xff;
1686                 ucontrol->value.iec958.status[1] = 0xff;
1687                 ucontrol->value.iec958.status[2] = 0xff;
1688                 ucontrol->value.iec958.status[3] = 0xff;
1689                 ucontrol->value.iec958.status[4] = 0xff;
1690         }
1691         return 0;
1692 }
1693
1694 static int snd_ice1712_spdif_maskp_get(snd_kcontrol_t * kcontrol,
1695                                        snd_ctl_elem_value_t * ucontrol)
1696 {
1697         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1698         if (ice->spdif.ops.default_get) {
1699                 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1700                                                      IEC958_AES0_PROFESSIONAL |
1701                                                      IEC958_AES0_PRO_FS |
1702                                                      IEC958_AES0_PRO_EMPHASIS;
1703                 ucontrol->value.iec958.status[1] = IEC958_AES1_PRO_MODE;
1704         } else {
1705                 ucontrol->value.iec958.status[0] = 0xff;
1706                 ucontrol->value.iec958.status[1] = 0xff;
1707                 ucontrol->value.iec958.status[2] = 0xff;
1708                 ucontrol->value.iec958.status[3] = 0xff;
1709                 ucontrol->value.iec958.status[4] = 0xff;
1710         }
1711         return 0;
1712 }
1713
1714 static snd_kcontrol_new_t snd_ice1712_spdif_maskc __devinitdata =
1715 {
1716         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1717         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1718         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1719         .info =         snd_ice1712_spdif_info,
1720         .get =          snd_ice1712_spdif_maskc_get,
1721 };
1722
1723 static snd_kcontrol_new_t snd_ice1712_spdif_maskp __devinitdata =
1724 {
1725         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1726         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1727         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1728         .info =         snd_ice1712_spdif_info,
1729         .get =          snd_ice1712_spdif_maskp_get,
1730 };
1731
1732 static int snd_ice1712_spdif_stream_get(snd_kcontrol_t * kcontrol,
1733                                         snd_ctl_elem_value_t * ucontrol)
1734 {
1735         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1736         if (ice->spdif.ops.stream_get)
1737                 ice->spdif.ops.stream_get(ice, ucontrol);
1738         return 0;
1739 }
1740
1741 static int snd_ice1712_spdif_stream_put(snd_kcontrol_t * kcontrol,
1742                                         snd_ctl_elem_value_t * ucontrol)
1743 {
1744         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1745         if (ice->spdif.ops.stream_put)
1746                 return ice->spdif.ops.stream_put(ice, ucontrol);
1747         return 0;
1748 }
1749
1750 static snd_kcontrol_new_t snd_ice1712_spdif_stream __devinitdata =
1751 {
1752         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1753         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1754         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1755         .info =         snd_ice1712_spdif_info,
1756         .get =          snd_ice1712_spdif_stream_get,
1757         .put =          snd_ice1712_spdif_stream_put
1758 };
1759
1760 int snd_ice1712_gpio_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1761 {
1762         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1763         uinfo->count = 1;
1764         uinfo->value.integer.min = 0;
1765         uinfo->value.integer.max = 1;
1766         return 0;
1767 }
1768
1769 int snd_ice1712_gpio_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1770 {
1771         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1772         unsigned char mask = kcontrol->private_value & 0xff;
1773         int invert = (kcontrol->private_value & (1<<24)) ? 1 : 0;
1774         
1775         snd_ice1712_save_gpio_status(ice);
1776         ucontrol->value.integer.value[0] = (snd_ice1712_gpio_read(ice) & mask ? 1 : 0) ^ invert;
1777         snd_ice1712_restore_gpio_status(ice);
1778         return 0;
1779 }
1780
1781 int snd_ice1712_gpio_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1782 {
1783         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1784         unsigned char mask = kcontrol->private_value & 0xff;
1785         int invert = (kcontrol->private_value & (1<<24)) ? mask : 0;
1786         unsigned int val, nval;
1787
1788         if (kcontrol->private_value & (1 << 31))
1789                 return -EPERM;
1790         nval = (ucontrol->value.integer.value[0] ? mask : 0) ^ invert;
1791         snd_ice1712_save_gpio_status(ice);
1792         val = snd_ice1712_gpio_read(ice);
1793         nval |= val & ~mask;
1794         if (val != nval)
1795                 snd_ice1712_gpio_write(ice, nval);
1796         snd_ice1712_restore_gpio_status(ice);
1797         return val != nval;
1798 }
1799
1800 /*
1801  *  rate
1802  */
1803 static int snd_ice1712_pro_internal_clock_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1804 {
1805         static char *texts[] = {
1806                 "8000",         /* 0: 6 */
1807                 "9600",         /* 1: 3 */
1808                 "11025",        /* 2: 10 */
1809                 "12000",        /* 3: 2 */
1810                 "16000",        /* 4: 5 */
1811                 "22050",        /* 5: 9 */
1812                 "24000",        /* 6: 1 */
1813                 "32000",        /* 7: 4 */
1814                 "44100",        /* 8: 8 */
1815                 "48000",        /* 9: 0 */
1816                 "64000",        /* 10: 15 */
1817                 "88200",        /* 11: 11 */
1818                 "96000",        /* 12: 7 */
1819                 "IEC958 Input", /* 13: -- */
1820         };
1821         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1822         uinfo->count = 1;
1823         uinfo->value.enumerated.items = 14;
1824         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1825                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1826         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1827         return 0;
1828 }
1829
1830 static int snd_ice1712_pro_internal_clock_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1831 {
1832         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1833         static unsigned char xlate[16] = {
1834                 9, 6, 3, 1, 7, 4, 0, 12, 8, 5, 2, 11, 255, 255, 255, 10
1835         };
1836         unsigned char val;
1837         
1838         spin_lock_irq(&ice->reg_lock);
1839         if (is_spdif_master(ice)) {
1840                 ucontrol->value.enumerated.item[0] = 13;
1841         } else {
1842                 val = xlate[inb(ICEMT(ice, RATE)) & 15];
1843                 if (val == 255) {
1844                         snd_BUG();
1845                         val = 0;
1846                 }
1847                 ucontrol->value.enumerated.item[0] = val;
1848         }
1849         spin_unlock_irq(&ice->reg_lock);
1850         return 0;
1851 }
1852
1853 static int snd_ice1712_pro_internal_clock_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1854 {
1855         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1856         static unsigned int xrate[13] = {
1857                 8000, 9600, 11025, 12000, 1600, 22050, 24000,
1858                 32000, 44100, 48000, 64000, 88200, 96000
1859         };
1860         unsigned char oval;
1861         int change = 0;
1862
1863         spin_lock_irq(&ice->reg_lock);
1864         oval = inb(ICEMT(ice, RATE));
1865         if (ucontrol->value.enumerated.item[0] == 13) {
1866                 outb(oval | ICE1712_SPDIF_MASTER, ICEMT(ice, RATE));
1867         } else {
1868                 PRO_RATE_DEFAULT = xrate[ucontrol->value.integer.value[0] % 13];
1869                 spin_unlock_irq(&ice->reg_lock);
1870                 snd_ice1712_set_pro_rate(ice, PRO_RATE_DEFAULT, 1);
1871                 spin_lock_irq(&ice->reg_lock);
1872         }
1873         change = inb(ICEMT(ice, RATE)) != oval;
1874         spin_unlock_irq(&ice->reg_lock);
1875
1876         if ((oval & ICE1712_SPDIF_MASTER) != (inb(ICEMT(ice, RATE)) & ICE1712_SPDIF_MASTER)) {
1877                 /* change CS8427 clock source too */
1878                 if (ice->cs8427) {
1879                         snd_ice1712_cs8427_set_input_clock(ice, is_spdif_master(ice));
1880                 }
1881                 /* notify ak4524 chip as well */
1882                 if (is_spdif_master(ice)) {
1883                         unsigned int i;
1884                         for (i = 0; i < ice->akm_codecs; i++) {
1885                                 if (ice->akm[i].ops.set_rate_val)
1886                                         ice->akm[i].ops.set_rate_val(&ice->akm[i], 0);
1887                         }
1888                 }
1889         }
1890
1891         return change;
1892 }
1893
1894 static snd_kcontrol_new_t snd_ice1712_pro_internal_clock __devinitdata = {
1895         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1896         .name = "Multi Track Internal Clock",
1897         .info = snd_ice1712_pro_internal_clock_info,
1898         .get = snd_ice1712_pro_internal_clock_get,
1899         .put = snd_ice1712_pro_internal_clock_put
1900 };
1901
1902 static int snd_ice1712_pro_internal_clock_default_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1903 {
1904         static char *texts[] = {
1905                 "8000",         /* 0: 6 */
1906                 "9600",         /* 1: 3 */
1907                 "11025",        /* 2: 10 */
1908                 "12000",        /* 3: 2 */
1909                 "16000",        /* 4: 5 */
1910                 "22050",        /* 5: 9 */
1911                 "24000",        /* 6: 1 */
1912                 "32000",        /* 7: 4 */
1913                 "44100",        /* 8: 8 */
1914                 "48000",        /* 9: 0 */
1915                 "64000",        /* 10: 15 */
1916                 "88200",        /* 11: 11 */
1917                 "96000",        /* 12: 7 */
1918                 // "IEC958 Input",      /* 13: -- */
1919         };
1920         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1921         uinfo->count = 1;
1922         uinfo->value.enumerated.items = 13;
1923         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1924                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1925         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1926         return 0;
1927 }
1928
1929 static int snd_ice1712_pro_internal_clock_default_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1930 {
1931         int val;
1932         static unsigned int xrate[13] = {
1933                 8000, 9600, 11025, 12000, 1600, 22050, 24000,
1934                 32000, 44100, 48000, 64000, 88200, 96000
1935         };
1936
1937         for (val = 0; val < 13; val++) {
1938                 if (xrate[val] == PRO_RATE_DEFAULT)
1939                         break;
1940         }
1941
1942         ucontrol->value.enumerated.item[0] = val;
1943         return 0;
1944 }
1945
1946 static int snd_ice1712_pro_internal_clock_default_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1947 {
1948         static unsigned int xrate[13] = {
1949                 8000, 9600, 11025, 12000, 1600, 22050, 24000,
1950                 32000, 44100, 48000, 64000, 88200, 96000
1951         };
1952         unsigned char oval;
1953         int change = 0;
1954
1955         oval = PRO_RATE_DEFAULT;
1956         PRO_RATE_DEFAULT = xrate[ucontrol->value.integer.value[0] % 13];
1957         change = PRO_RATE_DEFAULT != oval;
1958
1959         return change;
1960 }
1961
1962 static snd_kcontrol_new_t snd_ice1712_pro_internal_clock_default __devinitdata = {
1963         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1964         .name = "Multi Track Internal Clock Default",
1965         .info = snd_ice1712_pro_internal_clock_default_info,
1966         .get = snd_ice1712_pro_internal_clock_default_get,
1967         .put = snd_ice1712_pro_internal_clock_default_put
1968 };
1969
1970 static int snd_ice1712_pro_rate_locking_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1971 {
1972         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1973         uinfo->count = 1;
1974         uinfo->value.integer.min = 0;
1975         uinfo->value.integer.max = 1;
1976         return 0;
1977 }
1978
1979 static int snd_ice1712_pro_rate_locking_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1980 {
1981         ucontrol->value.integer.value[0] = PRO_RATE_LOCKED;
1982         return 0;
1983 }
1984
1985 static int snd_ice1712_pro_rate_locking_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1986 {
1987         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1988         int change = 0, nval;
1989
1990         nval = ucontrol->value.integer.value[0] ? 1 : 0;
1991         spin_lock_irq(&ice->reg_lock);
1992         change = PRO_RATE_LOCKED != nval;
1993         PRO_RATE_LOCKED = nval;
1994         spin_unlock_irq(&ice->reg_lock);
1995         return change;
1996 }
1997
1998 static snd_kcontrol_new_t snd_ice1712_pro_rate_locking __devinitdata = {
1999         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2000         .name = "Multi Track Rate Locking",
2001         .info = snd_ice1712_pro_rate_locking_info,
2002         .get = snd_ice1712_pro_rate_locking_get,
2003         .put = snd_ice1712_pro_rate_locking_put
2004 };
2005
2006 static int snd_ice1712_pro_rate_reset_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
2007 {
2008         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2009         uinfo->count = 1;
2010         uinfo->value.integer.min = 0;
2011         uinfo->value.integer.max = 1;
2012         return 0;
2013 }
2014
2015 static int snd_ice1712_pro_rate_reset_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2016 {
2017         ucontrol->value.integer.value[0] = PRO_RATE_RESET;
2018         return 0;
2019 }
2020
2021 static int snd_ice1712_pro_rate_reset_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2022 {
2023         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2024         int change = 0, nval;
2025
2026         nval = ucontrol->value.integer.value[0] ? 1 : 0;
2027         spin_lock_irq(&ice->reg_lock);
2028         change = PRO_RATE_RESET != nval;
2029         PRO_RATE_RESET = nval;
2030         spin_unlock_irq(&ice->reg_lock);
2031         return change;
2032 }
2033
2034 static snd_kcontrol_new_t snd_ice1712_pro_rate_reset __devinitdata = {
2035         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2036         .name = "Multi Track Rate Reset",
2037         .info = snd_ice1712_pro_rate_reset_info,
2038         .get = snd_ice1712_pro_rate_reset_get,
2039         .put = snd_ice1712_pro_rate_reset_put
2040 };
2041
2042 /*
2043  * routing
2044  */
2045 static int snd_ice1712_pro_route_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
2046 {
2047         static char *texts[] = {
2048                 "PCM Out", /* 0 */
2049                 "H/W In 0", "H/W In 1", "H/W In 2", "H/W In 3", /* 1-4 */
2050                 "H/W In 4", "H/W In 5", "H/W In 6", "H/W In 7", /* 5-8 */
2051                 "IEC958 In L", "IEC958 In R", /* 9-10 */
2052                 "Digital Mixer", /* 11 - optional */
2053         };
2054         
2055         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2056         uinfo->count = 1;
2057         uinfo->value.enumerated.items = snd_ctl_get_ioffidx(kcontrol, &uinfo->id) < 2 ? 12 : 11;
2058         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2059                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2060         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2061         return 0;
2062 }
2063
2064 static int snd_ice1712_pro_route_analog_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
2065 {
2066         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2067         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2068         unsigned int val, cval;
2069
2070         spin_lock_irq(&ice->reg_lock);
2071         val = inw(ICEMT(ice, ROUTE_PSDOUT03));
2072         cval = inl(ICEMT(ice, ROUTE_CAPTURE));
2073         spin_unlock_irq(&ice->reg_lock);
2074
2075         val >>= ((idx % 2) * 8) + ((idx / 2) * 2);
2076         val &= 3;
2077         cval >>= ((idx / 2) * 8) + ((idx % 2) * 4);
2078         if (val == 1 && idx < 2)
2079                 ucontrol->value.enumerated.item[0] = 11;
2080         else if (val == 2)
2081                 ucontrol->value.enumerated.item[0] = (cval & 7) + 1;
2082         else if (val == 3)
2083                 ucontrol->value.enumerated.item[0] = ((cval >> 3) & 1) + 9;
2084         else
2085                 ucontrol->value.enumerated.item[0] = 0;
2086         return 0;
2087 }
2088
2089 static int snd_ice1712_pro_route_analog_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
2090 {
2091         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2092         int change, shift;
2093         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2094         unsigned int val, old_val, nval;
2095         
2096         /* update PSDOUT */
2097         if (ucontrol->value.enumerated.item[0] >= 11)
2098                 nval = idx < 2 ? 1 : 0; /* dig mixer (or pcm) */
2099         else if (ucontrol->value.enumerated.item[0] >= 9)
2100                 nval = 3; /* spdif in */
2101         else if (ucontrol->value.enumerated.item[0] >= 1)
2102                 nval = 2; /* analog in */
2103         else
2104                 nval = 0; /* pcm */
2105         shift = ((idx % 2) * 8) + ((idx / 2) * 2);
2106         spin_lock_irq(&ice->reg_lock);
2107         val = old_val = inw(ICEMT(ice, ROUTE_PSDOUT03));
2108         val &= ~(0x03 << shift);
2109         val |= nval << shift;
2110         change = val != old_val;
2111         if (change)
2112                 outw(val, ICEMT(ice, ROUTE_PSDOUT03));
2113         spin_unlock_irq(&ice->reg_lock);
2114         if (nval < 2) /* dig mixer of pcm */
2115                 return change;
2116
2117         /* update CAPTURE */
2118         spin_lock_irq(&ice->reg_lock);
2119         val = old_val = inl(ICEMT(ice, ROUTE_CAPTURE));
2120         shift = ((idx / 2) * 8) + ((idx % 2) * 4);
2121         if (nval == 2) { /* analog in */
2122                 nval = ucontrol->value.enumerated.item[0] - 1;
2123                 val &= ~(0x07 << shift);
2124                 val |= nval << shift;
2125         } else { /* spdif in */
2126                 nval = (ucontrol->value.enumerated.item[0] - 9) << 3;
2127                 val &= ~(0x08 << shift);
2128                 val |= nval << shift;
2129         }
2130         if (val != old_val) {
2131                 change = 1;
2132                 outl(val, ICEMT(ice, ROUTE_CAPTURE));
2133         }
2134         spin_unlock_irq(&ice->reg_lock);
2135         return change;
2136 }
2137
2138 static int snd_ice1712_pro_route_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
2139 {
2140         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2141         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2142         unsigned int val, cval;
2143         val = inw(ICEMT(ice, ROUTE_SPDOUT));
2144         cval = (val >> (idx * 4 + 8)) & 0x0f;
2145         val = (val >> (idx * 2)) & 0x03;
2146         if (val == 1)
2147                 ucontrol->value.enumerated.item[0] = 11;
2148         else if (val == 2)
2149                 ucontrol->value.enumerated.item[0] = (cval & 7) + 1;
2150         else if (val == 3)
2151                 ucontrol->value.enumerated.item[0] = ((cval >> 3) & 1) + 9;
2152         else
2153                 ucontrol->value.enumerated.item[0] = 0;
2154         return 0;
2155 }
2156
2157 static int snd_ice1712_pro_route_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
2158 {
2159         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2160         int change, shift;
2161         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2162         unsigned int val, old_val, nval;
2163         
2164         /* update SPDOUT */
2165         spin_lock_irq(&ice->reg_lock);
2166         val = old_val = inw(ICEMT(ice, ROUTE_SPDOUT));
2167         if (ucontrol->value.enumerated.item[0] >= 11)
2168                 nval = 1;
2169         else if (ucontrol->value.enumerated.item[0] >= 9)
2170                 nval = 3;
2171         else if (ucontrol->value.enumerated.item[0] >= 1)
2172                 nval = 2;
2173         else
2174                 nval = 0;
2175         shift = idx * 2;
2176         val &= ~(0x03 << shift);
2177         val |= nval << shift;
2178         shift = idx * 4 + 8;
2179         if (nval == 2) {
2180                 nval = ucontrol->value.enumerated.item[0] - 1;
2181                 val &= ~(0x07 << shift);
2182                 val |= nval << shift;
2183         } else if (nval == 3) {
2184                 nval = (ucontrol->value.enumerated.item[0] - 9) << 3;
2185                 val &= ~(0x08 << shift);
2186                 val |= nval << shift;
2187         }
2188         change = val != old_val;
2189         if (change)
2190                 outw(val, ICEMT(ice, ROUTE_SPDOUT));
2191         spin_unlock_irq(&ice->reg_lock);
2192         return change;
2193 }
2194
2195 static snd_kcontrol_new_t snd_ice1712_mixer_pro_analog_route __devinitdata = {
2196         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2197         .name = "H/W Playback Route",
2198         .info = snd_ice1712_pro_route_info,
2199         .get = snd_ice1712_pro_route_analog_get,
2200         .put = snd_ice1712_pro_route_analog_put,
2201 };
2202
2203 static snd_kcontrol_new_t snd_ice1712_mixer_pro_spdif_route __devinitdata = {
2204         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2205         .name = "IEC958 Playback Route",
2206         .info = snd_ice1712_pro_route_info,
2207         .get = snd_ice1712_pro_route_spdif_get,
2208         .put = snd_ice1712_pro_route_spdif_put,
2209         .count = 2,
2210 };
2211
2212
2213 static int snd_ice1712_pro_volume_rate_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
2214 {
2215         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2216         uinfo->count = 1;
2217         uinfo->value.integer.min = 0;
2218         uinfo->value.integer.max = 255;
2219         return 0;
2220 }
2221
2222 static int snd_ice1712_pro_volume_rate_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2223 {
2224         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2225         
2226         ucontrol->value.integer.value[0] = inb(ICEMT(ice, MONITOR_RATE));
2227         return 0;
2228 }
2229
2230 static int snd_ice1712_pro_volume_rate_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2231 {
2232         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2233         int change;
2234
2235         spin_lock_irq(&ice->reg_lock);
2236         change = inb(ICEMT(ice, MONITOR_RATE)) != ucontrol->value.integer.value[0];
2237         outb(ucontrol->value.integer.value[0], ICEMT(ice, MONITOR_RATE));
2238         spin_unlock_irq(&ice->reg_lock);
2239         return change;
2240 }
2241
2242 static snd_kcontrol_new_t snd_ice1712_mixer_pro_volume_rate __devinitdata = {
2243         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2244         .name = "Multi Track Volume Rate",
2245         .info = snd_ice1712_pro_volume_rate_info,
2246         .get = snd_ice1712_pro_volume_rate_get,
2247         .put = snd_ice1712_pro_volume_rate_put
2248 };
2249
2250 static int snd_ice1712_pro_peak_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
2251 {
2252         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2253         uinfo->count = 22;
2254         uinfo->value.integer.min = 0;
2255         uinfo->value.integer.max = 255;
2256         return 0;
2257 }
2258
2259 static int snd_ice1712_pro_peak_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2260 {
2261         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2262         int idx;
2263         
2264         spin_lock_irq(&ice->reg_lock);
2265         for (idx = 0; idx < 22; idx++) {
2266                 outb(idx, ICEMT(ice, MONITOR_PEAKINDEX));
2267                 ucontrol->value.integer.value[idx] = inb(ICEMT(ice, MONITOR_PEAKDATA));
2268         }
2269         spin_unlock_irq(&ice->reg_lock);
2270         return 0;
2271 }
2272
2273 static snd_kcontrol_new_t snd_ice1712_mixer_pro_peak __devinitdata = {
2274         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2275         .name = "Multi Track Peak",
2276         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2277         .info = snd_ice1712_pro_peak_info,
2278         .get = snd_ice1712_pro_peak_get
2279 };
2280
2281 /*
2282  *
2283  */
2284
2285 /*
2286  * list of available boards
2287  */
2288 static struct snd_ice1712_card_info *card_tables[] __devinitdata = {
2289         snd_ice1712_hoontech_cards,
2290         snd_ice1712_delta_cards,
2291         snd_ice1712_ews_cards,
2292         NULL,
2293 };
2294
2295 static unsigned char __devinit snd_ice1712_read_i2c(ice1712_t *ice,
2296                                                  unsigned char dev,
2297                                                  unsigned char addr)
2298 {
2299         long t = 0x10000;
2300
2301         outb(addr, ICEREG(ice, I2C_BYTE_ADDR));
2302         outb(dev & ~ICE1712_I2C_WRITE, ICEREG(ice, I2C_DEV_ADDR));
2303         while (t-- > 0 && (inb(ICEREG(ice, I2C_CTRL)) & ICE1712_I2C_BUSY)) ;
2304         return inb(ICEREG(ice, I2C_DATA));
2305 }
2306
2307 static int __devinit snd_ice1712_read_eeprom(ice1712_t *ice, const char *modelname)
2308 {
2309         int dev = 0xa0;         /* EEPROM device address */
2310         unsigned int i, size;
2311
2312         if ((inb(ICEREG(ice, I2C_CTRL)) & ICE1712_I2C_EEPROM) == 0) {
2313                 snd_printk("ICE1712 has not detected EEPROM\n");
2314                 return -EIO;
2315         }
2316         if (modelname && *modelname) {
2317                 struct snd_ice1712_card_info **tbl, *c;
2318                 for (tbl = card_tables; *tbl; tbl++) {
2319                         for (c = *tbl; c->subvendor; c++) {
2320                                 if (c->model && !strcmp(modelname, c->model)) {
2321                                         /* use the given subvendor */
2322                                         printk(KERN_INFO "ice1712: Using board model %s\n", c->name);
2323                                         ice->eeprom.subvendor = c->subvendor;
2324                                         break;
2325                                 }
2326                         }
2327                 }
2328         } else
2329                 ice->eeprom.subvendor = (snd_ice1712_read_i2c(ice, dev, 0x00) << 0) |
2330                         (snd_ice1712_read_i2c(ice, dev, 0x01) << 8) | 
2331                         (snd_ice1712_read_i2c(ice, dev, 0x02) << 16) | 
2332                         (snd_ice1712_read_i2c(ice, dev, 0x03) << 24);
2333         ice->eeprom.size = snd_ice1712_read_i2c(ice, dev, 0x04);
2334         if (ice->eeprom.size < 6)
2335                 ice->eeprom.size = 32; /* FIXME: any cards without the correct size? */
2336         else if (ice->eeprom.size > 32) {
2337                 snd_printk("invalid EEPROM (size = %i)\n", ice->eeprom.size);
2338                 return -EIO;
2339         }
2340         ice->eeprom.version = snd_ice1712_read_i2c(ice, dev, 0x05);
2341         if (ice->eeprom.version != 1) {
2342                 snd_printk("invalid EEPROM version %i\n", ice->eeprom.version);
2343                 /* return -EIO; */
2344         }
2345         size = ice->eeprom.size - 6;
2346         for (i = 0; i < size; i++)
2347                 ice->eeprom.data[i] = snd_ice1712_read_i2c(ice, dev, i + 6);
2348
2349         ice->eeprom.gpiomask = ice->eeprom.data[ICE_EEP1_GPIO_MASK];
2350         ice->eeprom.gpiostate = ice->eeprom.data[ICE_EEP1_GPIO_STATE];
2351         ice->eeprom.gpiodir = ice->eeprom.data[ICE_EEP1_GPIO_DIR];
2352
2353         return 0;
2354 }
2355
2356
2357
2358 static int __devinit snd_ice1712_chip_init(ice1712_t *ice)
2359 {
2360         outb(ICE1712_RESET | ICE1712_NATIVE, ICEREG(ice, CONTROL));
2361         udelay(200);
2362         outb(ICE1712_NATIVE, ICEREG(ice, CONTROL));
2363         udelay(200);
2364         pci_write_config_byte(ice->pci, 0x60, ice->eeprom.data[ICE_EEP1_CODEC]);
2365         pci_write_config_byte(ice->pci, 0x61, ice->eeprom.data[ICE_EEP1_ACLINK]);
2366         pci_write_config_byte(ice->pci, 0x62, ice->eeprom.data[ICE_EEP1_I2SID]);
2367         pci_write_config_byte(ice->pci, 0x63, ice->eeprom.data[ICE_EEP1_SPDIF]);
2368         if (ice->eeprom.subvendor != ICE1712_SUBDEVICE_STDSP24) {
2369                 ice->gpio.write_mask = ice->eeprom.gpiomask;
2370                 ice->gpio.direction = ice->eeprom.gpiodir;
2371                 snd_ice1712_write(ice, ICE1712_IREG_GPIO_WRITE_MASK, ice->eeprom.gpiomask);
2372                 snd_ice1712_write(ice, ICE1712_IREG_GPIO_DIRECTION, ice->eeprom.gpiodir);
2373                 snd_ice1712_write(ice, ICE1712_IREG_GPIO_DATA, ice->eeprom.gpiostate);
2374         } else {
2375                 ice->gpio.write_mask = 0xc0;
2376                 ice->gpio.direction = 0xff;
2377                 snd_ice1712_write(ice, ICE1712_IREG_GPIO_WRITE_MASK, 0xc0);
2378                 snd_ice1712_write(ice, ICE1712_IREG_GPIO_DIRECTION, 0xff);
2379                 snd_ice1712_write(ice, ICE1712_IREG_GPIO_DATA, ICE1712_STDSP24_CLOCK_BIT);
2380         }
2381         snd_ice1712_write(ice, ICE1712_IREG_PRO_POWERDOWN, 0);
2382         if (!(ice->eeprom.data[ICE_EEP1_CODEC] & ICE1712_CFG_NO_CON_AC97)) {
2383                 outb(ICE1712_AC97_WARM, ICEREG(ice, AC97_CMD));
2384                 udelay(100);
2385                 outb(0, ICEREG(ice, AC97_CMD));
2386                 udelay(200);
2387                 snd_ice1712_write(ice, ICE1712_IREG_CONSUMER_POWERDOWN, 0);
2388         }
2389
2390         return 0;
2391 }
2392
2393 int __devinit snd_ice1712_spdif_build_controls(ice1712_t *ice)
2394 {
2395         int err;
2396         snd_kcontrol_t *kctl;
2397
2398         snd_assert(ice->pcm_pro != NULL, return -EIO);
2399         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_ice1712_spdif_default, ice));
2400         if (err < 0)
2401                 return err;
2402         kctl->id.device = ice->pcm_pro->device;
2403         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_ice1712_spdif_maskc, ice));
2404         if (err < 0)
2405                 return err;
2406         kctl->id.device = ice->pcm_pro->device;
2407         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_ice1712_spdif_maskp, ice));
2408         if (err < 0)
2409                 return err;
2410         kctl->id.device = ice->pcm_pro->device;
2411         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_ice1712_spdif_stream, ice));
2412         if (err < 0)
2413                 return err;
2414         kctl->id.device = ice->pcm_pro->device;
2415         ice->spdif.stream_ctl = kctl;
2416         return 0;
2417 }
2418
2419
2420 static int __devinit snd_ice1712_build_controls(ice1712_t *ice)
2421 {
2422         int err;
2423
2424         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_eeprom, ice));
2425         if (err < 0)
2426                 return err;
2427         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_pro_internal_clock, ice));
2428         if (err < 0)
2429                 return err;
2430         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_pro_internal_clock_default, ice));
2431         if (err < 0)
2432                 return err;
2433
2434         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_pro_rate_locking, ice));
2435         if (err < 0)
2436                 return err;
2437         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_pro_rate_reset, ice));
2438         if (err < 0)
2439                 return err;
2440
2441         if (ice->num_total_dacs > 0) {
2442                 snd_kcontrol_new_t tmp = snd_ice1712_mixer_pro_analog_route;
2443                 tmp.count = ice->num_total_dacs;
2444                 err = snd_ctl_add(ice->card, snd_ctl_new1(&tmp, ice));
2445                 if (err < 0)
2446                         return err;
2447         }
2448
2449         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_mixer_pro_spdif_route, ice));
2450         if (err < 0)
2451                 return err;
2452
2453         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_mixer_pro_volume_rate, ice));
2454         if (err < 0)
2455                 return err;
2456         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_mixer_pro_peak, ice));
2457         if (err < 0)
2458                 return err;
2459
2460         return 0;
2461 }
2462
2463 static int snd_ice1712_free(ice1712_t *ice)
2464 {
2465         if (! ice->port)
2466                 goto __hw_end;
2467         /* mask all interrupts */
2468         outb(0xc0, ICEMT(ice, IRQ));
2469         outb(0xff, ICEREG(ice, IRQMASK));
2470         /* --- */
2471       __hw_end:
2472         if (ice->irq >= 0) {
2473                 synchronize_irq(ice->irq);
2474                 free_irq(ice->irq, (void *) ice);
2475         }
2476         if (ice->port)
2477                 pci_release_regions(ice->pci);
2478         snd_ice1712_akm4xxx_free(ice);
2479         kfree(ice);
2480         return 0;
2481 }
2482
2483 static int snd_ice1712_dev_free(snd_device_t *device)
2484 {
2485         ice1712_t *ice = device->device_data;
2486         return snd_ice1712_free(ice);
2487 }
2488
2489 static int __devinit snd_ice1712_create(snd_card_t * card,
2490                                         struct pci_dev *pci,
2491                                         const char *modelname,
2492                                         int omni,
2493                                         int cs8427_timeout,
2494                                         ice1712_t ** r_ice1712)
2495 {
2496         ice1712_t *ice;
2497         int err;
2498         static snd_device_ops_t ops = {
2499                 .dev_free =     snd_ice1712_dev_free,
2500         };
2501
2502         *r_ice1712 = NULL;
2503
2504         /* enable PCI device */
2505         if ((err = pci_enable_device(pci)) < 0)
2506                 return err;
2507         /* check, if we can restrict PCI DMA transfers to 28 bits */
2508         if (pci_set_dma_mask(pci, 0x0fffffff) < 0 ||
2509             pci_set_consistent_dma_mask(pci, 0x0fffffff) < 0) {
2510                 snd_printk("architecture does not support 28bit PCI busmaster DMA\n");
2511                 return -ENXIO;
2512         }
2513
2514         ice = kcalloc(1, sizeof(*ice), GFP_KERNEL);
2515         if (ice == NULL)
2516                 return -ENOMEM;
2517         ice->omni = omni ? 1 : 0;
2518         if (cs8427_timeout < 1)
2519                 cs8427_timeout = 1;
2520         else if (cs8427_timeout > 1000)
2521                 cs8427_timeout = 1000;
2522         ice->cs8427_timeout = cs8427_timeout;
2523         spin_lock_init(&ice->reg_lock);
2524         init_MUTEX(&ice->gpio_mutex);
2525         init_MUTEX(&ice->open_mutex);
2526         ice->gpio.set_mask = snd_ice1712_set_gpio_mask;
2527         ice->gpio.set_dir = snd_ice1712_set_gpio_dir;
2528         ice->gpio.set_data = snd_ice1712_set_gpio_data;
2529         ice->gpio.get_data = snd_ice1712_get_gpio_data;
2530
2531         ice->spdif.cs8403_bits =
2532                 ice->spdif.cs8403_stream_bits = (0x01 | /* consumer format */
2533                                                  0x10 | /* no emphasis */
2534                                                  0x20); /* PCM encoder/decoder */
2535         ice->card = card;
2536         ice->pci = pci;
2537         ice->irq = -1;
2538         pci_set_master(pci);
2539         pci_write_config_word(ice->pci, 0x40, 0x807f);
2540         pci_write_config_word(ice->pci, 0x42, 0x0006);
2541         snd_ice1712_proc_init(ice);
2542         synchronize_irq(pci->irq);
2543
2544         if ((err = pci_request_regions(pci, "ICE1712")) < 0) {
2545                 kfree(ice);
2546                 return err;
2547         }
2548         ice->port = pci_resource_start(pci, 0);
2549         ice->ddma_port = pci_resource_start(pci, 1);
2550         ice->dmapath_port = pci_resource_start(pci, 2);
2551         ice->profi_port = pci_resource_start(pci, 3);
2552
2553         if (request_irq(pci->irq, snd_ice1712_interrupt, SA_INTERRUPT|SA_SHIRQ, "ICE1712", (void *) ice)) {
2554                 snd_printk("unable to grab IRQ %d\n", pci->irq);
2555                 snd_ice1712_free(ice);
2556                 return -EIO;
2557         }
2558         
2559         ice->irq = pci->irq;
2560
2561         if (snd_ice1712_read_eeprom(ice, modelname) < 0) {
2562                 snd_ice1712_free(ice);
2563                 return -EIO;
2564         }
2565         if (snd_ice1712_chip_init(ice) < 0) {
2566                 snd_ice1712_free(ice);
2567                 return -EIO;
2568         }
2569
2570         /* unmask used interrupts */
2571         outb((ice->eeprom.data[ICE_EEP1_CODEC] & ICE1712_CFG_2xMPU401) == 0 ? ICE1712_IRQ_MPU2 : 0 |
2572              (ice->eeprom.data[ICE_EEP1_CODEC] & ICE1712_CFG_NO_CON_AC97) ? ICE1712_IRQ_PBKDS | ICE1712_IRQ_CONCAP | ICE1712_IRQ_CONPBK : 0,
2573              ICEREG(ice, IRQMASK));
2574         outb(0x00, ICEMT(ice, IRQ));
2575
2576         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ice, &ops)) < 0) {
2577                 snd_ice1712_free(ice);
2578                 return err;
2579         }
2580
2581         snd_card_set_dev(card, &pci->dev);
2582
2583         *r_ice1712 = ice;
2584         return 0;
2585 }
2586
2587
2588 /*
2589  *
2590  * Registration
2591  *
2592  */
2593
2594 static struct snd_ice1712_card_info no_matched __devinitdata;
2595
2596 static int __devinit snd_ice1712_probe(struct pci_dev *pci,
2597                                        const struct pci_device_id *pci_id)
2598 {
2599         static int dev;
2600         snd_card_t *card;
2601         ice1712_t *ice;
2602         int pcm_dev = 0, err;
2603         struct snd_ice1712_card_info **tbl, *c;
2604
2605         if (dev >= SNDRV_CARDS)
2606                 return -ENODEV;
2607         if (!enable[dev]) {
2608                 dev++;
2609                 return -ENOENT;
2610         }
2611
2612         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2613         if (card == NULL)
2614                 return -ENOMEM;
2615
2616         strcpy(card->driver, "ICE1712");
2617         strcpy(card->shortname, "ICEnsemble ICE1712");
2618         
2619         if ((err = snd_ice1712_create(card, pci, model[dev], omni[dev], cs8427_timeout[dev], &ice)) < 0) {
2620                 snd_card_free(card);
2621                 return err;
2622         }
2623
2624         for (tbl = card_tables; *tbl; tbl++) {
2625                 for (c = *tbl; c->subvendor; c++) {
2626                         if (c->subvendor == ice->eeprom.subvendor) {
2627                                 strcpy(card->shortname, c->name);
2628                                 if (c->driver) /* specific driver? */
2629                                         strcpy(card->driver, c->driver);
2630                                 if (c->chip_init) {
2631                                         if ((err = c->chip_init(ice)) < 0) {
2632                                                 snd_card_free(card);
2633                                                 return err;
2634                                         }
2635                                 }
2636                                 goto __found;
2637                         }
2638                 }
2639         }
2640         c = &no_matched;
2641  __found:
2642
2643         if ((err = snd_ice1712_pcm_profi(ice, pcm_dev++, NULL)) < 0) {
2644                 snd_card_free(card);
2645                 return err;
2646         }
2647         
2648         if (ice_has_con_ac97(ice))
2649                 if ((err = snd_ice1712_pcm(ice, pcm_dev++, NULL)) < 0) {
2650                         snd_card_free(card);
2651                         return err;
2652                 }
2653
2654         if ((err = snd_ice1712_ac97_mixer(ice)) < 0) {
2655                 snd_card_free(card);
2656                 return err;
2657         }
2658
2659         if ((err = snd_ice1712_build_controls(ice)) < 0) {
2660                 snd_card_free(card);
2661                 return err;
2662         }
2663
2664         if (c->build_controls) {
2665                 if ((err = c->build_controls(ice)) < 0) {
2666                         snd_card_free(card);
2667                         return err;
2668                 }
2669         }
2670
2671         if (ice_has_con_ac97(ice))
2672                 if ((err = snd_ice1712_pcm_ds(ice, pcm_dev++, NULL)) < 0) {
2673                         snd_card_free(card);
2674                         return err;
2675                 }
2676
2677         if (! c->no_mpu401) {
2678                 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ICE1712,
2679                                                ICEREG(ice, MPU1_CTRL), 1,
2680                                                ice->irq, 0,
2681                                                &ice->rmidi[0])) < 0) {
2682                         snd_card_free(card);
2683                         return err;
2684                 }
2685
2686                 if (ice->eeprom.data[ICE_EEP1_CODEC] & ICE1712_CFG_2xMPU401)
2687                         if ((err = snd_mpu401_uart_new(card, 1, MPU401_HW_ICE1712,
2688                                                        ICEREG(ice, MPU2_CTRL), 1,
2689                                                        ice->irq, 0,
2690                                                        &ice->rmidi[1])) < 0) {
2691                                 snd_card_free(card);
2692                                 return err;
2693                         }
2694         }
2695
2696         sprintf(card->longname, "%s at 0x%lx, irq %i",
2697                 card->shortname, ice->port, ice->irq);
2698
2699         if ((err = snd_card_register(card)) < 0) {
2700                 snd_card_free(card);
2701                 return err;
2702         }
2703         pci_set_drvdata(pci, card);
2704         dev++;
2705         return 0;
2706 }
2707
2708 static void __devexit snd_ice1712_remove(struct pci_dev *pci)
2709 {
2710         snd_card_free(pci_get_drvdata(pci));
2711         pci_set_drvdata(pci, NULL);
2712 }
2713
2714 static struct pci_driver driver = {
2715         .name = "ICE1712",
2716         .id_table = snd_ice1712_ids,
2717         .probe = snd_ice1712_probe,
2718         .remove = __devexit_p(snd_ice1712_remove),
2719 };
2720
2721 static int __init alsa_card_ice1712_init(void)
2722 {
2723         return pci_module_init(&driver);
2724 }
2725
2726 static void __exit alsa_card_ice1712_exit(void)
2727 {
2728         pci_unregister_driver(&driver);
2729 }
2730
2731 module_init(alsa_card_ice1712_init)
2732 module_exit(alsa_card_ice1712_exit)