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