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