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