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