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