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