ALSA: opti-miro: add PnP detection
[firefly-linux-kernel-4.4.55.git] / sound / isa / opti9xx / miro.c
1 /*
2  *   ALSA soundcard driver for Miro miroSOUND PCM1 pro
3  *                                  miroSOUND PCM12
4  *                                  miroSOUND PCM20 Radio
5  *
6  *   Copyright (C) 2004-2005 Martin Langer <martin-langer@gmx.de>
7  *
8  *   Based on OSS ACI and ALSA OPTi9xx drivers
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU General Public License as published by
12  *   the Free Software Foundation; either version 2 of the License, or
13  *   (at your option) any later version.
14  *
15  *   This program is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with this program; if not, write to the Free Software
22  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24
25 #include <linux/init.h>
26 #include <linux/err.h>
27 #include <linux/isa.h>
28 #include <linux/pnp.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/ioport.h>
32 #include <linux/moduleparam.h>
33 #include <asm/io.h>
34 #include <asm/dma.h>
35 #include <sound/core.h>
36 #include <sound/wss.h>
37 #include <sound/mpu401.h>
38 #include <sound/opl4.h>
39 #include <sound/control.h>
40 #include <sound/info.h>
41 #define SNDRV_LEGACY_FIND_FREE_IRQ
42 #define SNDRV_LEGACY_FIND_FREE_DMA
43 #include <sound/initval.h>
44 #include <sound/aci.h>
45
46 MODULE_AUTHOR("Martin Langer <martin-langer@gmx.de>");
47 MODULE_LICENSE("GPL");
48 MODULE_DESCRIPTION("Miro miroSOUND PCM1 pro, PCM12, PCM20 Radio");
49 MODULE_SUPPORTED_DEVICE("{{Miro,miroSOUND PCM1 pro}, "
50                         "{Miro,miroSOUND PCM12}, "
51                         "{Miro,miroSOUND PCM20 Radio}}");
52
53 static int index = SNDRV_DEFAULT_IDX1;          /* Index 0-MAX */
54 static char *id = SNDRV_DEFAULT_STR1;           /* ID for this card */
55 static long port = SNDRV_DEFAULT_PORT1;         /* 0x530,0xe80,0xf40,0x604 */
56 static long mpu_port = SNDRV_DEFAULT_PORT1;     /* 0x300,0x310,0x320,0x330 */
57 static long fm_port = SNDRV_DEFAULT_PORT1;      /* 0x388 */
58 static int irq = SNDRV_DEFAULT_IRQ1;            /* 5,7,9,10,11 */
59 static int mpu_irq = SNDRV_DEFAULT_IRQ1;        /* 5,7,9,10 */
60 static int dma1 = SNDRV_DEFAULT_DMA1;           /* 0,1,3 */
61 static int dma2 = SNDRV_DEFAULT_DMA1;           /* 0,1,3 */
62 static int wss;
63 static int ide;
64 #ifdef CONFIG_PNP
65 static int isapnp = 1;                          /* Enable ISA PnP detection */
66 #endif
67
68 module_param(index, int, 0444);
69 MODULE_PARM_DESC(index, "Index value for miro soundcard.");
70 module_param(id, charp, 0444);
71 MODULE_PARM_DESC(id, "ID string for miro soundcard.");
72 module_param(port, long, 0444);
73 MODULE_PARM_DESC(port, "WSS port # for miro driver.");
74 module_param(mpu_port, long, 0444);
75 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for miro driver.");
76 module_param(fm_port, long, 0444);
77 MODULE_PARM_DESC(fm_port, "FM Port # for miro driver.");
78 module_param(irq, int, 0444);
79 MODULE_PARM_DESC(irq, "WSS irq # for miro driver.");
80 module_param(mpu_irq, int, 0444);
81 MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for miro driver.");
82 module_param(dma1, int, 0444);
83 MODULE_PARM_DESC(dma1, "1st dma # for miro driver.");
84 module_param(dma2, int, 0444);
85 MODULE_PARM_DESC(dma2, "2nd dma # for miro driver.");
86 module_param(wss, int, 0444);
87 MODULE_PARM_DESC(wss, "wss mode");
88 module_param(ide, int, 0444);
89 MODULE_PARM_DESC(ide, "enable ide port");
90 #ifdef CONFIG_PNP
91 module_param(isapnp, bool, 0444);
92 MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
93 #endif
94
95 #define OPTi9XX_HW_DETECT       0
96 #define OPTi9XX_HW_82C928       1
97 #define OPTi9XX_HW_82C929       2
98 #define OPTi9XX_HW_82C924       3
99 #define OPTi9XX_HW_82C925       4
100 #define OPTi9XX_HW_82C930       5
101 #define OPTi9XX_HW_82C931       6
102 #define OPTi9XX_HW_82C933       7
103 #define OPTi9XX_HW_LAST         OPTi9XX_HW_82C933
104
105 #define OPTi9XX_MC_REG(n)       n
106
107 struct snd_miro {
108         unsigned short hardware;
109         unsigned char password;
110         char name[7];
111
112         struct resource *res_mc_base;
113         struct resource *res_aci_port;
114
115         unsigned long mc_base;
116         unsigned long mc_base_size;
117         unsigned long pwd_reg;
118
119         spinlock_t lock;
120         struct snd_pcm *pcm;
121
122         long wss_base;
123         int irq;
124         int dma1;
125         int dma2;
126
127         long mpu_port;
128         int mpu_irq;
129
130         struct snd_miro_aci *aci;
131 };
132
133 static struct snd_miro_aci aci_device;
134
135 static char * snd_opti9xx_names[] = {
136         "unkown",
137         "82C928", "82C929",
138         "82C924", "82C925",
139         "82C930", "82C931", "82C933"
140 };
141
142 static int snd_miro_pnp_is_probed;
143
144 #ifdef CONFIG_PNP
145
146 static struct pnp_card_device_id snd_miro_pnpids[] = {
147         /* PCM20 and PCM12 in PnP mode */
148         { .id = "MIR0924",
149           .devs = { { "MIR0000" }, { "MIR0002" }, { "MIR0005" } }, },
150         { .id = "" }
151 };
152
153 MODULE_DEVICE_TABLE(pnp_card, snd_miro_pnpids);
154
155 #endif  /* CONFIG_PNP */
156
157 /* 
158  *  ACI control
159  */
160
161 static int aci_busy_wait(struct snd_miro_aci *aci)
162 {
163         long timeout;
164         unsigned char byte;
165
166         for (timeout = 1; timeout <= ACI_MINTIME + 30; timeout++) {
167                 byte = inb(aci->aci_port + ACI_REG_BUSY);
168                 if ((byte & 1) == 0) {
169                         if (timeout >= ACI_MINTIME)
170                                 snd_printd("aci ready in round %ld.\n",
171                                            timeout-ACI_MINTIME);
172                         return byte;
173                 }
174                 if (timeout >= ACI_MINTIME) {
175                         long out=10*HZ;
176                         switch (timeout-ACI_MINTIME) {
177                         case 0 ... 9:
178                                 out /= 10;
179                         case 10 ... 19:
180                                 out /= 10;
181                         case 20 ... 30:
182                                 out /= 10;
183                         default:
184                                 set_current_state(TASK_UNINTERRUPTIBLE);
185                                 schedule_timeout(out);
186                                 break;
187                         }
188                 }
189         }
190         snd_printk(KERN_ERR "aci_busy_wait() time out\n");
191         return -EBUSY;
192 }
193
194 static inline int aci_write(struct snd_miro_aci *aci, unsigned char byte)
195 {
196         if (aci_busy_wait(aci) >= 0) {
197                 outb(byte, aci->aci_port + ACI_REG_COMMAND);
198                 return 0;
199         } else {
200                 snd_printk(KERN_ERR "aci busy, aci_write(0x%x) stopped.\n", byte);
201                 return -EBUSY;
202         }
203 }
204
205 static inline int aci_read(struct snd_miro_aci *aci)
206 {
207         unsigned char byte;
208
209         if (aci_busy_wait(aci) >= 0) {
210                 byte = inb(aci->aci_port + ACI_REG_STATUS);
211                 return byte;
212         } else {
213                 snd_printk(KERN_ERR "aci busy, aci_read() stopped.\n");
214                 return -EBUSY;
215         }
216 }
217
218 int snd_aci_cmd(struct snd_miro_aci *aci, int write1, int write2, int write3)
219 {
220         int write[] = {write1, write2, write3};
221         int value, i;
222
223         if (mutex_lock_interruptible(&aci->aci_mutex))
224                 return -EINTR;
225
226         for (i=0; i<3; i++) {
227                 if (write[i]< 0 || write[i] > 255)
228                         break;
229                 else {
230                         value = aci_write(aci, write[i]);
231                         if (value < 0)
232                                 goto out;
233                 }
234         }
235
236         value = aci_read(aci);
237
238 out:    mutex_unlock(&aci->aci_mutex);
239         return value;
240 }
241 EXPORT_SYMBOL(snd_aci_cmd);
242
243 static int aci_getvalue(struct snd_miro_aci *aci, unsigned char index)
244 {
245         return snd_aci_cmd(aci, ACI_STATUS, index, -1);
246 }
247
248 static int aci_setvalue(struct snd_miro_aci *aci, unsigned char index,
249                         int value)
250 {
251         return snd_aci_cmd(aci, index, value, -1);
252 }
253
254 struct snd_miro_aci *snd_aci_get_aci(void)
255 {
256         if (aci_device.aci_port == 0)
257                 return NULL;
258         return &aci_device;
259 }
260 EXPORT_SYMBOL(snd_aci_get_aci);
261
262 /*
263  *  MIXER part
264  */
265
266 #define snd_miro_info_capture   snd_ctl_boolean_mono_info
267
268 static int snd_miro_get_capture(struct snd_kcontrol *kcontrol,
269                                 struct snd_ctl_elem_value *ucontrol)
270 {
271         struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
272         int value;
273
274         value = aci_getvalue(miro->aci, ACI_S_GENERAL);
275         if (value < 0) {
276                 snd_printk(KERN_ERR "snd_miro_get_capture() failed: %d\n",
277                            value);
278                 return value;
279         }
280
281         ucontrol->value.integer.value[0] = value & 0x20;
282
283         return 0;
284 }
285
286 static int snd_miro_put_capture(struct snd_kcontrol *kcontrol,
287                                 struct snd_ctl_elem_value *ucontrol)
288 {
289         struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
290         int change, value, error;
291
292         value = !(ucontrol->value.integer.value[0]);
293
294         error = aci_setvalue(miro->aci, ACI_SET_SOLOMODE, value);
295         if (error < 0) {
296                 snd_printk(KERN_ERR "snd_miro_put_capture() failed: %d\n",
297                            error);
298                 return error;
299         }
300
301         change = (value != miro->aci->aci_solomode);
302         miro->aci->aci_solomode = value;
303         
304         return change;
305 }
306
307 static int snd_miro_info_preamp(struct snd_kcontrol *kcontrol,
308                                 struct snd_ctl_elem_info *uinfo)
309 {
310         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
311         uinfo->count = 1;
312         uinfo->value.integer.min = 0;
313         uinfo->value.integer.max = 3;
314
315         return 0;
316 }
317
318 static int snd_miro_get_preamp(struct snd_kcontrol *kcontrol,
319                                struct snd_ctl_elem_value *ucontrol)
320 {
321         struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
322         int value;
323
324         if (miro->aci->aci_version <= 176) {
325
326                 /* 
327                    OSS says it's not readable with versions < 176.
328                    But it doesn't work on my card,
329                    which is a PCM12 with aci_version = 176.
330                 */
331
332                 ucontrol->value.integer.value[0] = miro->aci->aci_preamp;
333                 return 0;
334         }
335
336         value = aci_getvalue(miro->aci, ACI_GET_PREAMP);
337         if (value < 0) {
338                 snd_printk(KERN_ERR "snd_miro_get_preamp() failed: %d\n",
339                            value);
340                 return value;
341         }
342         
343         ucontrol->value.integer.value[0] = value;
344
345         return 0;
346 }
347
348 static int snd_miro_put_preamp(struct snd_kcontrol *kcontrol,
349                                struct snd_ctl_elem_value *ucontrol)
350 {
351         struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
352         int error, value, change;
353
354         value = ucontrol->value.integer.value[0];
355
356         error = aci_setvalue(miro->aci, ACI_SET_PREAMP, value);
357         if (error < 0) {
358                 snd_printk(KERN_ERR "snd_miro_put_preamp() failed: %d\n",
359                            error);
360                 return error;
361         }
362
363         change = (value != miro->aci->aci_preamp);
364         miro->aci->aci_preamp = value;
365
366         return change;
367 }
368
369 #define snd_miro_info_amp       snd_ctl_boolean_mono_info
370
371 static int snd_miro_get_amp(struct snd_kcontrol *kcontrol,
372                             struct snd_ctl_elem_value *ucontrol)
373 {
374         struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
375         ucontrol->value.integer.value[0] = miro->aci->aci_amp;
376
377         return 0;
378 }
379
380 static int snd_miro_put_amp(struct snd_kcontrol *kcontrol,
381                             struct snd_ctl_elem_value *ucontrol)
382 {
383         struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
384         int error, value, change;
385
386         value = ucontrol->value.integer.value[0];
387
388         error = aci_setvalue(miro->aci, ACI_SET_POWERAMP, value);
389         if (error < 0) {
390                 snd_printk(KERN_ERR "snd_miro_put_amp() to %d failed: %d\n", value, error);
391                 return error;
392         }
393
394         change = (value != miro->aci->aci_amp);
395         miro->aci->aci_amp = value;
396
397         return change;
398 }
399
400 #define MIRO_DOUBLE(ctl_name, ctl_index, get_right_reg, set_right_reg) \
401 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
402   .name = ctl_name, \
403   .index = ctl_index, \
404   .info = snd_miro_info_double, \
405   .get = snd_miro_get_double, \
406   .put = snd_miro_put_double, \
407   .private_value = get_right_reg | (set_right_reg << 8) \
408 }
409
410 static int snd_miro_info_double(struct snd_kcontrol *kcontrol, 
411                                 struct snd_ctl_elem_info *uinfo)
412 {
413         int reg = kcontrol->private_value & 0xff;
414
415         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
416         uinfo->count = 2;
417
418         if ((reg >= ACI_GET_EQ1) && (reg <= ACI_GET_EQ7)) {
419
420                 /* equalizer elements */
421
422                 uinfo->value.integer.min = - 0x7f;
423                 uinfo->value.integer.max = 0x7f;
424         } else {
425
426                 /* non-equalizer elements */
427
428                 uinfo->value.integer.min = 0;
429                 uinfo->value.integer.max = 0x20;
430         }
431
432         return 0;
433 }
434
435 static int snd_miro_get_double(struct snd_kcontrol *kcontrol, 
436                                struct snd_ctl_elem_value *uinfo)
437 {
438         struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
439         int left_val, right_val;
440
441         int right_reg = kcontrol->private_value & 0xff;
442         int left_reg = right_reg + 1;
443
444         right_val = aci_getvalue(miro->aci, right_reg);
445         if (right_val < 0) {
446                 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", right_reg, right_val);
447                 return right_val;
448         }
449
450         left_val = aci_getvalue(miro->aci, left_reg);
451         if (left_val < 0) {
452                 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", left_reg, left_val);
453                 return left_val;
454         }
455
456         if ((right_reg >= ACI_GET_EQ1) && (right_reg <= ACI_GET_EQ7)) {
457
458                 /* equalizer elements */
459
460                 if (left_val < 0x80) {
461                         uinfo->value.integer.value[0] = left_val;
462                 } else {
463                         uinfo->value.integer.value[0] = 0x80 - left_val;
464                 }
465
466                 if (right_val < 0x80) {
467                         uinfo->value.integer.value[1] = right_val;
468                 } else {
469                         uinfo->value.integer.value[1] = 0x80 - right_val;
470                 }
471
472         } else {
473
474                 /* non-equalizer elements */
475
476                 uinfo->value.integer.value[0] = 0x20 - left_val;
477                 uinfo->value.integer.value[1] = 0x20 - right_val;
478         }
479
480         return 0;
481 }
482
483 static int snd_miro_put_double(struct snd_kcontrol *kcontrol, 
484                                struct snd_ctl_elem_value *ucontrol)
485 {
486         struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
487         struct snd_miro_aci *aci = miro->aci;
488         int left, right, left_old, right_old;
489         int setreg_left, setreg_right, getreg_left, getreg_right;
490         int change, error;
491
492         left = ucontrol->value.integer.value[0];
493         right = ucontrol->value.integer.value[1];
494
495         setreg_right = (kcontrol->private_value >> 8) & 0xff;
496         setreg_left = setreg_right + 8;
497         if (setreg_right == ACI_SET_MASTER)
498                 setreg_left -= 7;
499
500         getreg_right = kcontrol->private_value & 0xff;
501         getreg_left = getreg_right + 1;
502
503         left_old = aci_getvalue(aci, getreg_left);
504         if (left_old < 0) {
505                 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_left, left_old);
506                 return left_old;
507         }
508
509         right_old = aci_getvalue(aci, getreg_right);
510         if (right_old < 0) {
511                 snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_right, right_old);
512                 return right_old;
513         }
514
515         if ((getreg_right >= ACI_GET_EQ1) && (getreg_right <= ACI_GET_EQ7)) {
516
517                 /* equalizer elements */
518
519                 if (left < -0x7f || left > 0x7f ||
520                     right < -0x7f || right > 0x7f)
521                         return -EINVAL;
522
523                 if (left_old > 0x80) 
524                         left_old = 0x80 - left_old;
525                 if (right_old > 0x80) 
526                         right_old = 0x80 - right_old;
527
528                 if (left >= 0) {
529                         error = aci_setvalue(aci, setreg_left, left);
530                         if (error < 0) {
531                                 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
532                                            left, error);
533                                 return error;
534                         }
535                 } else {
536                         error = aci_setvalue(aci, setreg_left, 0x80 - left);
537                         if (error < 0) {
538                                 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
539                                            0x80 - left, error);
540                                 return error;
541                         }
542                 }
543
544                 if (right >= 0) {
545                         error = aci_setvalue(aci, setreg_right, right);
546                         if (error < 0) {
547                                 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
548                                            right, error);
549                                 return error;
550                         }
551                 } else {
552                         error = aci_setvalue(aci, setreg_right, 0x80 - right);
553                         if (error < 0) {
554                                 snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
555                                            0x80 - right, error);
556                                 return error;
557                         }
558                 }
559
560         } else {
561
562                 /* non-equalizer elements */
563
564                 if (left < 0 || left > 0x20 ||
565                     right < 0 || right > 0x20)
566                         return -EINVAL;
567
568                 left_old = 0x20 - left_old;
569                 right_old = 0x20 - right_old;
570
571                 error = aci_setvalue(aci, setreg_left, 0x20 - left);
572                 if (error < 0) {
573                         snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
574                                    0x20 - left, error);
575                         return error;
576                 }
577                 error = aci_setvalue(aci, setreg_right, 0x20 - right);
578                 if (error < 0) {
579                         snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
580                                    0x20 - right, error);
581                         return error;
582                 }
583         }
584
585         change = (left != left_old) || (right != right_old);
586
587         return change;
588 }
589
590 static struct snd_kcontrol_new snd_miro_controls[] __devinitdata = {
591 MIRO_DOUBLE("Master Playback Volume", 0, ACI_GET_MASTER, ACI_SET_MASTER),
592 MIRO_DOUBLE("Mic Playback Volume", 1, ACI_GET_MIC, ACI_SET_MIC),
593 MIRO_DOUBLE("Line Playback Volume", 1, ACI_GET_LINE, ACI_SET_LINE),
594 MIRO_DOUBLE("CD Playback Volume", 0, ACI_GET_CD, ACI_SET_CD),
595 MIRO_DOUBLE("Synth Playback Volume", 0, ACI_GET_SYNTH, ACI_SET_SYNTH),
596 MIRO_DOUBLE("PCM Playback Volume", 1, ACI_GET_PCM, ACI_SET_PCM),
597 MIRO_DOUBLE("Aux Playback Volume", 2, ACI_GET_LINE2, ACI_SET_LINE2),
598 };
599
600 /* Equalizer with seven bands (only PCM20) 
601    from -12dB up to +12dB on each band */
602 static struct snd_kcontrol_new snd_miro_eq_controls[] __devinitdata = {
603 MIRO_DOUBLE("Tone Control - 28 Hz", 0, ACI_GET_EQ1, ACI_SET_EQ1),
604 MIRO_DOUBLE("Tone Control - 160 Hz", 0, ACI_GET_EQ2, ACI_SET_EQ2),
605 MIRO_DOUBLE("Tone Control - 400 Hz", 0, ACI_GET_EQ3, ACI_SET_EQ3),
606 MIRO_DOUBLE("Tone Control - 1 kHz", 0, ACI_GET_EQ4, ACI_SET_EQ4),
607 MIRO_DOUBLE("Tone Control - 2.5 kHz", 0, ACI_GET_EQ5, ACI_SET_EQ5),
608 MIRO_DOUBLE("Tone Control - 6.3 kHz", 0, ACI_GET_EQ6, ACI_SET_EQ6),
609 MIRO_DOUBLE("Tone Control - 16 kHz", 0, ACI_GET_EQ7, ACI_SET_EQ7),
610 };
611
612 static struct snd_kcontrol_new snd_miro_radio_control[] __devinitdata = {
613 MIRO_DOUBLE("Radio Playback Volume", 0, ACI_GET_LINE1, ACI_SET_LINE1),
614 };
615
616 static struct snd_kcontrol_new snd_miro_line_control[] __devinitdata = {
617 MIRO_DOUBLE("Line Playback Volume", 2, ACI_GET_LINE1, ACI_SET_LINE1),
618 };
619
620 static struct snd_kcontrol_new snd_miro_preamp_control[] __devinitdata = {
621 {
622         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
623         .name = "Mic Boost",
624         .index = 1,
625         .info = snd_miro_info_preamp,
626         .get = snd_miro_get_preamp,
627         .put = snd_miro_put_preamp,
628 }};
629
630 static struct snd_kcontrol_new snd_miro_amp_control[] __devinitdata = {
631 {
632         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
633         .name = "Line Boost",
634         .index = 0,
635         .info = snd_miro_info_amp,
636         .get = snd_miro_get_amp,
637         .put = snd_miro_put_amp,
638 }};
639
640 static struct snd_kcontrol_new snd_miro_capture_control[] __devinitdata = {
641 {
642         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
643         .name = "PCM Capture Switch",
644         .index = 0,
645         .info = snd_miro_info_capture,
646         .get = snd_miro_get_capture,
647         .put = snd_miro_put_capture,
648 }};
649
650 static unsigned char aci_init_values[][2] __devinitdata = {
651         { ACI_SET_MUTE, 0x00 },
652         { ACI_SET_POWERAMP, 0x00 },
653         { ACI_SET_PREAMP, 0x00 },
654         { ACI_SET_SOLOMODE, 0x00 },
655         { ACI_SET_MIC + 0, 0x20 },
656         { ACI_SET_MIC + 8, 0x20 },
657         { ACI_SET_LINE + 0, 0x20 },
658         { ACI_SET_LINE + 8, 0x20 },
659         { ACI_SET_CD + 0, 0x20 },
660         { ACI_SET_CD + 8, 0x20 },
661         { ACI_SET_PCM + 0, 0x20 },
662         { ACI_SET_PCM + 8, 0x20 },
663         { ACI_SET_LINE1 + 0, 0x20 },
664         { ACI_SET_LINE1 + 8, 0x20 },
665         { ACI_SET_LINE2 + 0, 0x20 },
666         { ACI_SET_LINE2 + 8, 0x20 },
667         { ACI_SET_SYNTH + 0, 0x20 },
668         { ACI_SET_SYNTH + 8, 0x20 },
669         { ACI_SET_MASTER + 0, 0x20 },
670         { ACI_SET_MASTER + 1, 0x20 },
671 };
672
673 static int __devinit snd_set_aci_init_values(struct snd_miro *miro)
674 {
675         int idx, error;
676         struct snd_miro_aci *aci = miro->aci;
677
678         /* enable WSS on PCM1 */
679
680         if ((aci->aci_product == 'A') && wss) {
681                 error = aci_setvalue(aci, ACI_SET_WSS, wss);
682                 if (error < 0) {
683                         snd_printk(KERN_ERR "enabling WSS mode failed\n");
684                         return error;
685                 }
686         }
687
688         /* enable IDE port */
689
690         if (ide) {
691                 error = aci_setvalue(aci, ACI_SET_IDE, ide);
692                 if (error < 0) {
693                         snd_printk(KERN_ERR "enabling IDE port failed\n");
694                         return error;
695                 }
696         }
697
698         /* set common aci values */
699
700         for (idx = 0; idx < ARRAY_SIZE(aci_init_values); idx++) {
701                 error = aci_setvalue(aci, aci_init_values[idx][0],
702                                      aci_init_values[idx][1]);
703                 if (error < 0) {
704                         snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n", 
705                                    aci_init_values[idx][0], error);
706                         return error;
707                 }
708         }
709         aci->aci_amp = 0;
710         aci->aci_preamp = 0;
711         aci->aci_solomode = 1;
712
713         return 0;
714 }
715
716 static int __devinit snd_miro_mixer(struct snd_card *card,
717                                     struct snd_miro *miro)
718 {
719         unsigned int idx;
720         int err;
721
722         if (snd_BUG_ON(!miro || !card))
723                 return -EINVAL;
724
725         switch (miro->hardware) {
726         case OPTi9XX_HW_82C924:
727                 strcpy(card->mixername, "ACI & OPTi924");
728                 break;
729         case OPTi9XX_HW_82C929:
730                 strcpy(card->mixername, "ACI & OPTi929");
731                 break;
732         default:
733                 snd_BUG();
734                 break;
735         }
736
737         for (idx = 0; idx < ARRAY_SIZE(snd_miro_controls); idx++) {
738                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_controls[idx], miro))) < 0)
739                         return err;
740         }
741
742         if ((miro->aci->aci_product == 'A') ||
743             (miro->aci->aci_product == 'B')) {
744                 /* PCM1/PCM12 with power-amp and Line 2 */
745                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_line_control[0], miro))) < 0)
746                         return err;
747                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_amp_control[0], miro))) < 0)
748                         return err;
749         }
750
751         if ((miro->aci->aci_product == 'B') ||
752             (miro->aci->aci_product == 'C')) {
753                 /* PCM12/PCM20 with mic-preamp */
754                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_preamp_control[0], miro))) < 0)
755                         return err;
756                 if (miro->aci->aci_version >= 176)
757                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_capture_control[0], miro))) < 0)
758                                 return err;
759         }
760
761         if (miro->aci->aci_product == 'C') {
762                 /* PCM20 with radio and 7 band equalizer */
763                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_radio_control[0], miro))) < 0)
764                         return err;
765                 for (idx = 0; idx < ARRAY_SIZE(snd_miro_eq_controls); idx++) {
766                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_eq_controls[idx], miro))) < 0)
767                                 return err;
768                 }
769         }
770
771         return 0;
772 }
773
774 static long snd_legacy_find_free_ioport(long *port_table, long size)
775 {
776         while (*port_table != -1) {
777                 struct resource *res;
778                 if ((res = request_region(*port_table, size, 
779                                           "ALSA test")) != NULL) {
780                         release_and_free_resource(res);
781                         return *port_table;
782                 }
783                 port_table++;
784         }
785         return -1;
786 }
787
788 static int __devinit snd_miro_init(struct snd_miro *chip,
789                                    unsigned short hardware)
790 {
791         static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
792
793         chip->hardware = hardware;
794         strcpy(chip->name, snd_opti9xx_names[hardware]);
795
796         chip->mc_base_size = opti9xx_mc_size[hardware];  
797
798         spin_lock_init(&chip->lock);
799
800         chip->wss_base = -1;
801         chip->irq = -1;
802         chip->dma1 = -1;
803         chip->dma2 = -1;
804         chip->mpu_port = -1;
805         chip->mpu_irq = -1;
806
807         chip->pwd_reg = 3;
808
809 #ifdef CONFIG_PNP
810         if (isapnp && chip->mc_base)
811                 /* PnP resource gives the least 10 bits */
812                 chip->mc_base |= 0xc00;
813         else
814 #endif
815                 chip->mc_base = 0xf8c;
816
817         switch (hardware) {
818         case OPTi9XX_HW_82C929:
819                 chip->password = 0xe3;
820                 break;
821
822         case OPTi9XX_HW_82C924:
823                 chip->password = 0xe5;
824                 break;
825
826         default:
827                 snd_printk(KERN_ERR "sorry, no support for %d\n", hardware);
828                 return -ENODEV;
829         }
830
831         return 0;
832 }
833
834 static unsigned char snd_miro_read(struct snd_miro *chip,
835                                    unsigned char reg)
836 {
837         unsigned long flags;
838         unsigned char retval = 0xff;
839
840         spin_lock_irqsave(&chip->lock, flags);
841         outb(chip->password, chip->mc_base + chip->pwd_reg);
842
843         switch (chip->hardware) {
844         case OPTi9XX_HW_82C924:
845                 if (reg > 7) {
846                         outb(reg, chip->mc_base + 8);
847                         outb(chip->password, chip->mc_base + chip->pwd_reg);
848                         retval = inb(chip->mc_base + 9);
849                         break;
850                 }
851
852         case OPTi9XX_HW_82C929:
853                 retval = inb(chip->mc_base + reg);
854                 break;
855
856         default:
857                 snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
858         }
859
860         spin_unlock_irqrestore(&chip->lock, flags);
861         return retval;
862 }
863
864 static void snd_miro_write(struct snd_miro *chip, unsigned char reg,
865                            unsigned char value)
866 {
867         unsigned long flags;
868
869         spin_lock_irqsave(&chip->lock, flags);
870         outb(chip->password, chip->mc_base + chip->pwd_reg);
871
872         switch (chip->hardware) {
873         case OPTi9XX_HW_82C924:
874                 if (reg > 7) {
875                         outb(reg, chip->mc_base + 8);
876                         outb(chip->password, chip->mc_base + chip->pwd_reg);
877                         outb(value, chip->mc_base + 9);
878                         break;
879                 }
880
881         case OPTi9XX_HW_82C929:
882                 outb(value, chip->mc_base + reg);
883                 break;
884
885         default:
886                 snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
887         }
888
889         spin_unlock_irqrestore(&chip->lock, flags);
890 }
891
892
893 #define snd_miro_write_mask(chip, reg, value, mask)     \
894         snd_miro_write(chip, reg,                       \
895                 (snd_miro_read(chip, reg) & ~(mask)) | ((value) & (mask)))
896
897 /*
898  *  Proc Interface
899  */
900
901 static void snd_miro_proc_read(struct snd_info_entry * entry, 
902                                struct snd_info_buffer *buffer)
903 {
904         struct snd_miro *miro = (struct snd_miro *) entry->private_data;
905         struct snd_miro_aci *aci = miro->aci;
906         char* model = "unknown";
907
908         /* miroSOUND PCM1 pro, early PCM12 */
909
910         if ((miro->hardware == OPTi9XX_HW_82C929) &&
911             (aci->aci_vendor == 'm') &&
912             (aci->aci_product == 'A')) {
913                 switch (aci->aci_version) {
914                 case 3:
915                         model = "miroSOUND PCM1 pro";
916                         break;
917                 default:
918                         model = "miroSOUND PCM1 pro / (early) PCM12";
919                         break;
920                 }
921         }
922
923         /* miroSOUND PCM12, PCM12 (Rev. E), PCM12 pnp */
924
925         if ((miro->hardware == OPTi9XX_HW_82C924) &&
926             (aci->aci_vendor == 'm') &&
927             (aci->aci_product == 'B')) {
928                 switch (aci->aci_version) {
929                 case 4:
930                         model = "miroSOUND PCM12";
931                         break;
932                 case 176:
933                         model = "miroSOUND PCM12 (Rev. E)";
934                         break;
935                 default:
936                         model = "miroSOUND PCM12 / PCM12 pnp";
937                         break;
938                 }
939         }
940
941         /* miroSOUND PCM20 radio */
942
943         if ((miro->hardware == OPTi9XX_HW_82C924) &&
944             (aci->aci_vendor == 'm') &&
945             (aci->aci_product == 'C')) {
946                 switch (aci->aci_version) {
947                 case 7:
948                         model = "miroSOUND PCM20 radio (Rev. E)";
949                         break;
950                 default:
951                         model = "miroSOUND PCM20 radio";
952                         break;
953                 }
954         }
955
956         snd_iprintf(buffer, "\nGeneral information:\n");
957         snd_iprintf(buffer, "  model   : %s\n", model);
958         snd_iprintf(buffer, "  opti    : %s\n", miro->name);
959         snd_iprintf(buffer, "  codec   : %s\n", miro->pcm->name);
960         snd_iprintf(buffer, "  port    : 0x%lx\n", miro->wss_base);
961         snd_iprintf(buffer, "  irq     : %d\n", miro->irq);
962         snd_iprintf(buffer, "  dma     : %d,%d\n\n", miro->dma1, miro->dma2);
963
964         snd_iprintf(buffer, "MPU-401:\n");
965         snd_iprintf(buffer, "  port    : 0x%lx\n", miro->mpu_port);
966         snd_iprintf(buffer, "  irq     : %d\n\n", miro->mpu_irq);
967
968         snd_iprintf(buffer, "ACI information:\n");
969         snd_iprintf(buffer, "  vendor  : ");
970         switch (aci->aci_vendor) {
971         case 'm':
972                 snd_iprintf(buffer, "Miro\n");
973                 break;
974         default:
975                 snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_vendor);
976                 break;
977         }
978
979         snd_iprintf(buffer, "  product : ");
980         switch (aci->aci_product) {
981         case 'A':
982                 snd_iprintf(buffer, "miroSOUND PCM1 pro / (early) PCM12\n");
983                 break;
984         case 'B':
985                 snd_iprintf(buffer, "miroSOUND PCM12\n");
986                 break;
987         case 'C':
988                 snd_iprintf(buffer, "miroSOUND PCM20 radio\n");
989                 break;
990         default:
991                 snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_product);
992                 break;
993         }
994
995         snd_iprintf(buffer, "  firmware: %d (0x%x)\n",
996                     aci->aci_version, aci->aci_version);
997         snd_iprintf(buffer, "  port    : 0x%lx-0x%lx\n", 
998                     aci->aci_port, aci->aci_port+2);
999         snd_iprintf(buffer, "  wss     : 0x%x\n", wss);
1000         snd_iprintf(buffer, "  ide     : 0x%x\n", ide);
1001         snd_iprintf(buffer, "  solomode: 0x%x\n", aci->aci_solomode);
1002         snd_iprintf(buffer, "  amp     : 0x%x\n", aci->aci_amp);
1003         snd_iprintf(buffer, "  preamp  : 0x%x\n", aci->aci_preamp);
1004 }
1005
1006 static void __devinit snd_miro_proc_init(struct snd_card *card,
1007                                          struct snd_miro *miro)
1008 {
1009         struct snd_info_entry *entry;
1010
1011         if (!snd_card_proc_new(card, "miro", &entry))
1012                 snd_info_set_text_ops(entry, miro, snd_miro_proc_read);
1013 }
1014
1015 /*
1016  *  Init
1017  */
1018
1019 static int __devinit snd_miro_configure(struct snd_miro *chip)
1020 {
1021         unsigned char wss_base_bits;
1022         unsigned char irq_bits;
1023         unsigned char dma_bits;
1024         unsigned char mpu_port_bits = 0;
1025         unsigned char mpu_irq_bits;
1026         unsigned long flags;
1027
1028         snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
1029         snd_miro_write_mask(chip, OPTi9XX_MC_REG(2), 0x20, 0x20); /* OPL4 */
1030         snd_miro_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
1031
1032         switch (chip->hardware) {
1033         case OPTi9XX_HW_82C924:
1034                 snd_miro_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
1035                 snd_miro_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
1036                 break;
1037         case OPTi9XX_HW_82C929:
1038                 /* untested init commands for OPTi929 */
1039                 snd_miro_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
1040                 break;
1041         default:
1042                 snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
1043                 return -EINVAL;
1044         }
1045
1046         /* PnP resource says it decodes only 10 bits of address */
1047         switch (chip->wss_base & 0x3ff) {
1048         case 0x130:
1049                 chip->wss_base = 0x530;
1050                 wss_base_bits = 0x00;
1051                 break;
1052         case 0x204:
1053                 chip->wss_base = 0x604;
1054                 wss_base_bits = 0x03;
1055                 break;
1056         case 0x280:
1057                 chip->wss_base = 0xe80;
1058                 wss_base_bits = 0x01;
1059                 break;
1060         case 0x340:
1061                 chip->wss_base = 0xf40;
1062                 wss_base_bits = 0x02;
1063                 break;
1064         default:
1065                 snd_printk(KERN_ERR "WSS port 0x%lx not valid\n", chip->wss_base);
1066                 goto __skip_base;
1067         }
1068         snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
1069
1070 __skip_base:
1071         switch (chip->irq) {
1072         case 5:
1073                 irq_bits = 0x05;
1074                 break;
1075         case 7:
1076                 irq_bits = 0x01;
1077                 break;
1078         case 9:
1079                 irq_bits = 0x02;
1080                 break;
1081         case 10:
1082                 irq_bits = 0x03;
1083                 break;
1084         case 11:
1085                 irq_bits = 0x04;
1086                 break;
1087         default:
1088                 snd_printk(KERN_ERR "WSS irq # %d not valid\n", chip->irq);
1089                 goto __skip_resources;
1090         }
1091
1092         switch (chip->dma1) {
1093         case 0:
1094                 dma_bits = 0x01;
1095                 break;
1096         case 1:
1097                 dma_bits = 0x02;
1098                 break;
1099         case 3:
1100                 dma_bits = 0x03;
1101                 break;
1102         default:
1103                 snd_printk(KERN_ERR "WSS dma1 # %d not valid\n", chip->dma1);
1104                 goto __skip_resources;
1105         }
1106
1107         if (chip->dma1 == chip->dma2) {
1108                 snd_printk(KERN_ERR "don't want to share dmas\n");
1109                 return -EBUSY;
1110         }
1111
1112         switch (chip->dma2) {
1113         case 0:
1114         case 1:
1115                 break;
1116         default:
1117                 snd_printk(KERN_ERR "WSS dma2 # %d not valid\n", chip->dma2);
1118                 goto __skip_resources;
1119         }
1120         dma_bits |= 0x04;
1121
1122         spin_lock_irqsave(&chip->lock, flags);
1123         outb(irq_bits << 3 | dma_bits, chip->wss_base);
1124         spin_unlock_irqrestore(&chip->lock, flags);
1125
1126 __skip_resources:
1127         if (chip->hardware > OPTi9XX_HW_82C928) {
1128                 switch (chip->mpu_port) {
1129                 case 0:
1130                 case -1:
1131                         break;
1132                 case 0x300:
1133                         mpu_port_bits = 0x03;
1134                         break;
1135                 case 0x310:
1136                         mpu_port_bits = 0x02;
1137                         break;
1138                 case 0x320:
1139                         mpu_port_bits = 0x01;
1140                         break;
1141                 case 0x330:
1142                         mpu_port_bits = 0x00;
1143                         break;
1144                 default:
1145                         snd_printk(KERN_ERR "MPU-401 port 0x%lx not valid\n",
1146                                    chip->mpu_port);
1147                         goto __skip_mpu;
1148                 }
1149
1150                 switch (chip->mpu_irq) {
1151                 case 5:
1152                         mpu_irq_bits = 0x02;
1153                         break;
1154                 case 7:
1155                         mpu_irq_bits = 0x03;
1156                         break;
1157                 case 9:
1158                         mpu_irq_bits = 0x00;
1159                         break;
1160                 case 10:
1161                         mpu_irq_bits = 0x01;
1162                         break;
1163                 default:
1164                         snd_printk(KERN_ERR "MPU-401 irq # %d not valid\n",
1165                                    chip->mpu_irq);
1166                         goto __skip_mpu;
1167                 }
1168
1169                 snd_miro_write_mask(chip, OPTi9XX_MC_REG(6),
1170                         (chip->mpu_port <= 0) ? 0x00 :
1171                                 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
1172                         0xf8);
1173         }
1174 __skip_mpu:
1175
1176         return 0;
1177 }
1178
1179 static int __devinit snd_miro_opti_check(struct snd_miro *chip)
1180 {
1181         unsigned char value;
1182
1183         chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
1184                                            "OPTi9xx MC");
1185         if (chip->res_mc_base == NULL)
1186                 return -ENOMEM;
1187
1188         value = snd_miro_read(chip, OPTi9XX_MC_REG(1));
1189         if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
1190                 if (value == snd_miro_read(chip, OPTi9XX_MC_REG(1)))
1191                         return 0;
1192
1193         release_and_free_resource(chip->res_mc_base);
1194         chip->res_mc_base = NULL;
1195
1196         return -ENODEV;
1197 }
1198
1199 static int __devinit snd_card_miro_detect(struct snd_card *card,
1200                                           struct snd_miro *chip)
1201 {
1202         int i, err;
1203
1204         for (i = OPTi9XX_HW_82C929; i <= OPTi9XX_HW_82C924; i++) {
1205
1206                 if ((err = snd_miro_init(chip, i)) < 0)
1207                         return err;
1208
1209                 err = snd_miro_opti_check(chip);
1210                 if (err == 0)
1211                         return 1;
1212         }
1213
1214         return -ENODEV;
1215 }
1216
1217 static int __devinit snd_card_miro_aci_detect(struct snd_card *card,
1218                                               struct snd_miro *miro)
1219 {
1220         unsigned char regval;
1221         int i;
1222         struct snd_miro_aci *aci = &aci_device;
1223
1224         miro->aci = aci;
1225
1226         mutex_init(&aci->aci_mutex);
1227
1228         /* get ACI port from OPTi9xx MC 4 */
1229
1230         regval=inb(miro->mc_base + 4);
1231         aci->aci_port = (regval & 0x10) ? 0x344 : 0x354;
1232
1233         miro->res_aci_port = request_region(aci->aci_port, 3, "miro aci");
1234         if (miro->res_aci_port == NULL) {
1235                 snd_printk(KERN_ERR "aci i/o area 0x%lx-0x%lx already used.\n", 
1236                            aci->aci_port, aci->aci_port+2);
1237                 return -ENOMEM;
1238         }
1239
1240         /* force ACI into a known state */
1241         for (i = 0; i < 3; i++)
1242                 if (snd_aci_cmd(aci, ACI_ERROR_OP, -1, -1) < 0) {
1243                         snd_printk(KERN_ERR "can't force aci into known state.\n");
1244                         return -ENXIO;
1245                 }
1246
1247         aci->aci_vendor = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
1248         aci->aci_product = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
1249         if (aci->aci_vendor < 0 || aci->aci_product < 0) {
1250                 snd_printk(KERN_ERR "can't read aci id on 0x%lx.\n",
1251                            aci->aci_port);
1252                 return -ENXIO;
1253         }
1254
1255         aci->aci_version = snd_aci_cmd(aci, ACI_READ_VERSION, -1, -1);
1256         if (aci->aci_version < 0) {
1257                 snd_printk(KERN_ERR "can't read aci version on 0x%lx.\n", 
1258                            aci->aci_port);
1259                 return -ENXIO;
1260         }
1261
1262         if (snd_aci_cmd(aci, ACI_INIT, -1, -1) < 0 ||
1263             snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0 ||
1264             snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0) {
1265                 snd_printk(KERN_ERR "can't initialize aci.\n"); 
1266                 return -ENXIO;
1267         }
1268
1269         return 0;
1270 }
1271
1272 static void snd_card_miro_free(struct snd_card *card)
1273 {
1274         struct snd_miro *miro = card->private_data;
1275
1276         release_and_free_resource(miro->res_aci_port);
1277         if (miro->aci)
1278                 miro->aci->aci_port = 0;
1279         release_and_free_resource(miro->res_mc_base);
1280 }
1281
1282 static int __devinit snd_miro_probe(struct snd_card *card)
1283 {
1284         int error;
1285         struct snd_miro *miro = card->private_data;
1286         struct snd_wss *codec;
1287         struct snd_timer *timer;
1288         struct snd_pcm *pcm;
1289         struct snd_rawmidi *rmidi;
1290
1291         if (!miro->res_mc_base) {
1292                 miro->res_mc_base = request_region(miro->mc_base,
1293                                                 miro->mc_base_size,
1294                                                 "miro (OPTi9xx MC)");
1295                 if (miro->res_mc_base == NULL) {
1296                         snd_printk(KERN_ERR "request for OPTI9xx MC failed\n");
1297                         return -ENOMEM;
1298                 }
1299         }
1300
1301         error = snd_card_miro_aci_detect(card, miro);
1302         if (error < 0) {
1303                 snd_card_free(card);
1304                 snd_printk(KERN_ERR "unable to detect aci chip\n");
1305                 return -ENODEV;
1306         }
1307
1308         miro->wss_base = port;
1309         miro->mpu_port = mpu_port;
1310         miro->irq = irq;
1311         miro->mpu_irq = mpu_irq;
1312         miro->dma1 = dma1;
1313         miro->dma2 = dma2;
1314
1315         /* init proc interface */
1316         snd_miro_proc_init(card, miro);
1317
1318         error = snd_miro_configure(miro);
1319         if (error)
1320                 return error;
1321
1322         error = snd_wss_create(card, miro->wss_base + 4, -1,
1323                                miro->irq, miro->dma1, miro->dma2,
1324                                WSS_HW_DETECT, 0, &codec);
1325         if (error < 0)
1326                 return error;
1327
1328         error = snd_wss_pcm(codec, 0, &pcm);
1329         if (error < 0)
1330                 return error;
1331
1332         error = snd_wss_mixer(codec);
1333         if (error < 0)
1334                 return error;
1335
1336         error = snd_wss_timer(codec, 0, &timer);
1337         if (error < 0)
1338                 return error;
1339
1340         miro->pcm = pcm;
1341
1342         error = snd_miro_mixer(card, miro);
1343         if (error < 0)
1344                 return error;
1345
1346         if (miro->aci->aci_vendor == 'm') {
1347                 /* It looks like a miro sound card. */
1348                 switch (miro->aci->aci_product) {
1349                 case 'A':
1350                         sprintf(card->shortname, 
1351                                 "miroSOUND PCM1 pro / PCM12");
1352                         break;
1353                 case 'B':
1354                         sprintf(card->shortname, 
1355                                 "miroSOUND PCM12");
1356                         break;
1357                 case 'C':
1358                         sprintf(card->shortname, 
1359                                 "miroSOUND PCM20 radio");
1360                         break;
1361                 default:
1362                         sprintf(card->shortname, 
1363                                 "unknown miro");
1364                         snd_printk(KERN_INFO "unknown miro aci id\n");
1365                         break;
1366                 }
1367         } else {
1368                 snd_printk(KERN_INFO "found unsupported aci card\n");
1369                 sprintf(card->shortname, "unknown Cardinal Technologies");
1370         }
1371
1372         strcpy(card->driver, "miro");
1373         sprintf(card->longname, "%s: OPTi%s, %s at 0x%lx, irq %d, dma %d&%d",
1374                 card->shortname, miro->name, pcm->name, miro->wss_base + 4,
1375                 miro->irq, miro->dma1, miro->dma2);
1376
1377         if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
1378                 rmidi = NULL;
1379         else {
1380                 error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
1381                                 mpu_port, 0, miro->mpu_irq, IRQF_DISABLED,
1382                                 &rmidi);
1383                 if (error < 0)
1384                         snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
1385                                    mpu_port);
1386         }
1387
1388         if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
1389                 struct snd_opl3 *opl3 = NULL;
1390                 struct snd_opl4 *opl4;
1391
1392                 if (snd_opl4_create(card, fm_port, fm_port - 8,
1393                                     2, &opl3, &opl4) < 0)
1394                         snd_printk(KERN_WARNING "no OPL4 device at 0x%lx\n",
1395                                    fm_port);
1396         }
1397
1398         error = snd_set_aci_init_values(miro);
1399         if (error < 0)
1400                 return error;
1401
1402         return snd_card_register(card);
1403 }
1404
1405 static int __devinit snd_miro_isa_match(struct device *devptr, unsigned int n)
1406 {
1407 #ifdef CONFIG_PNP
1408         if (snd_miro_pnp_is_probed)
1409                 return 0;
1410         if (isapnp)
1411                 return 0;
1412 #endif
1413         return 1;
1414 }
1415
1416 static int __devinit snd_miro_isa_probe(struct device *devptr, unsigned int n)
1417 {
1418         static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
1419         static long possible_mpu_ports[] = {0x330, 0x300, 0x310, 0x320, -1};
1420         static int possible_irqs[] = {11, 9, 10, 7, -1};
1421         static int possible_mpu_irqs[] = {10, 5, 9, 7, -1};
1422         static int possible_dma1s[] = {3, 1, 0, -1};
1423         static int possible_dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1},
1424                                            {0, -1} };
1425
1426         int error;
1427         struct snd_miro *miro;
1428         struct snd_card *card;
1429
1430         error = snd_card_create(index, id, THIS_MODULE,
1431                                 sizeof(struct snd_miro), &card);
1432         if (error < 0)
1433                 return error;
1434
1435         card->private_free = snd_card_miro_free;
1436         miro = card->private_data;
1437
1438         error = snd_card_miro_detect(card, miro);
1439         if (error < 0) {
1440                 snd_card_free(card);
1441                 snd_printk(KERN_ERR "unable to detect OPTi9xx chip\n");
1442                 return -ENODEV;
1443         }
1444
1445         if (port == SNDRV_AUTO_PORT) {
1446                 port = snd_legacy_find_free_ioport(possible_ports, 4);
1447                 if (port < 0) {
1448                         snd_card_free(card);
1449                         snd_printk(KERN_ERR "unable to find a free WSS port\n");
1450                         return -EBUSY;
1451                 }
1452         }
1453
1454         if (mpu_port == SNDRV_AUTO_PORT) {
1455                 mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2);
1456                 if (mpu_port < 0) {
1457                         snd_card_free(card);
1458                         snd_printk(KERN_ERR
1459                                    "unable to find a free MPU401 port\n");
1460                         return -EBUSY;
1461                 }
1462         }
1463
1464         if (irq == SNDRV_AUTO_IRQ) {
1465                 irq = snd_legacy_find_free_irq(possible_irqs);
1466                 if (irq < 0) {
1467                         snd_card_free(card);
1468                         snd_printk(KERN_ERR "unable to find a free IRQ\n");
1469                         return -EBUSY;
1470                 }
1471         }
1472         if (mpu_irq == SNDRV_AUTO_IRQ) {
1473                 mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs);
1474                 if (mpu_irq < 0) {
1475                         snd_card_free(card);
1476                         snd_printk(KERN_ERR
1477                                    "unable to find a free MPU401 IRQ\n");
1478                         return -EBUSY;
1479                 }
1480         }
1481         if (dma1 == SNDRV_AUTO_DMA) {
1482                 dma1 = snd_legacy_find_free_dma(possible_dma1s);
1483                 if (dma1 < 0) {
1484                         snd_card_free(card);
1485                         snd_printk(KERN_ERR "unable to find a free DMA1\n");
1486                         return -EBUSY;
1487                 }
1488         }
1489         if (dma2 == SNDRV_AUTO_DMA) {
1490                 dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4]);
1491                 if (dma2 < 0) {
1492                         snd_card_free(card);
1493                         snd_printk(KERN_ERR "unable to find a free DMA2\n");
1494                         return -EBUSY;
1495                 }
1496         }
1497
1498         snd_card_set_dev(card, devptr);
1499
1500         error = snd_miro_probe(card);
1501         if (error < 0) {
1502                 snd_card_free(card);
1503                 return error;
1504         }
1505
1506         dev_set_drvdata(devptr, card);
1507         return 0;
1508 }
1509
1510 static int __devexit snd_miro_isa_remove(struct device *devptr,
1511                                          unsigned int dev)
1512 {
1513         snd_card_free(dev_get_drvdata(devptr));
1514         dev_set_drvdata(devptr, NULL);
1515         return 0;
1516 }
1517
1518 #define DEV_NAME "miro"
1519
1520 static struct isa_driver snd_miro_driver = {
1521         .match          = snd_miro_isa_match,
1522         .probe          = snd_miro_isa_probe,
1523         .remove         = __devexit_p(snd_miro_isa_remove),
1524         /* FIXME: suspend/resume */
1525         .driver         = {
1526                 .name   = DEV_NAME
1527         },
1528 };
1529
1530 #ifdef CONFIG_PNP
1531
1532 static int __devinit snd_card_miro_pnp(struct snd_miro *chip,
1533                                         struct pnp_card_link *card,
1534                                         const struct pnp_card_device_id *pid)
1535 {
1536         struct pnp_dev *pdev;
1537         int err;
1538         struct pnp_dev *devmpu;
1539         struct pnp_dev *devmc;
1540
1541         pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
1542         if (pdev == NULL)
1543                 return -EBUSY;
1544
1545         devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
1546         if (devmpu == NULL)
1547                 return -EBUSY;
1548
1549         devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
1550         if (devmc == NULL)
1551                 return -EBUSY;
1552
1553         err = pnp_activate_dev(pdev);
1554         if (err < 0) {
1555                 snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
1556                 return err;
1557         }
1558
1559         err = pnp_activate_dev(devmc);
1560         if (err < 0) {
1561                 snd_printk(KERN_ERR "OPL syntg pnp configure failure: %d\n",
1562                                     err);
1563                 return err;
1564         }
1565
1566         port = pnp_port_start(pdev, 1);
1567         fm_port = pnp_port_start(pdev, 2) + 8;
1568
1569         /*
1570          * The MC(0) is never accessed and the miroSOUND PCM20 card does not
1571          * include it in the PnP resource range. OPTI93x include it.
1572          */
1573         chip->mc_base = pnp_port_start(devmc, 0) - 1;
1574         chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
1575
1576         irq = pnp_irq(pdev, 0);
1577         dma1 = pnp_dma(pdev, 0);
1578         dma2 = pnp_dma(pdev, 1);
1579
1580         if (mpu_port > 0) {
1581                 err = pnp_activate_dev(devmpu);
1582                 if (err < 0) {
1583                         snd_printk(KERN_ERR "MPU401 pnp configure failure\n");
1584                         mpu_port = -1;
1585                         return err;
1586                 }
1587                 mpu_port = pnp_port_start(devmpu, 0);
1588                 mpu_irq = pnp_irq(devmpu, 0);
1589         }
1590         return 0;
1591 }
1592
1593 static int __devinit snd_miro_pnp_probe(struct pnp_card_link *pcard,
1594                                         const struct pnp_card_device_id *pid)
1595 {
1596         struct snd_card *card;
1597         int err;
1598         struct snd_miro *miro;
1599
1600         if (snd_miro_pnp_is_probed)
1601                 return -EBUSY;
1602         if (!isapnp)
1603                 return -ENODEV;
1604         err = snd_card_create(index, id, THIS_MODULE,
1605                                 sizeof(struct snd_miro), &card);
1606         if (err < 0)
1607                 return err;
1608
1609         card->private_free = snd_card_miro_free;
1610         miro = card->private_data;
1611
1612         err = snd_card_miro_pnp(miro, pcard, pid);
1613         if (err) {
1614                 snd_card_free(card);
1615                 return err;
1616         }
1617
1618         /* only miroSOUND PCM20 and PCM12 == OPTi924 */
1619         err = snd_miro_init(miro, OPTi9XX_HW_82C924);
1620         if (err) {
1621                 snd_card_free(card);
1622                 return err;
1623         }
1624
1625         err = snd_miro_opti_check(miro);
1626         if (err) {
1627                 snd_printk(KERN_ERR "OPTI chip not found\n");
1628                 snd_card_free(card);
1629                 return err;
1630         }
1631
1632         snd_card_set_dev(card, &pcard->card->dev);
1633         err = snd_miro_probe(card);
1634         if (err < 0) {
1635                 snd_card_free(card);
1636                 return err;
1637         }
1638         pnp_set_card_drvdata(pcard, card);
1639         snd_miro_pnp_is_probed = 1;
1640         return 0;
1641 }
1642
1643 static void __devexit snd_miro_pnp_remove(struct pnp_card_link * pcard)
1644 {
1645         snd_card_free(pnp_get_card_drvdata(pcard));
1646         pnp_set_card_drvdata(pcard, NULL);
1647         snd_miro_pnp_is_probed = 0;
1648 }
1649
1650 static struct pnp_card_driver miro_pnpc_driver = {
1651         .flags          = PNP_DRIVER_RES_DISABLE,
1652         .name           = "miro",
1653         .id_table       = snd_miro_pnpids,
1654         .probe          = snd_miro_pnp_probe,
1655         .remove         = __devexit_p(snd_miro_pnp_remove),
1656 };
1657 #endif
1658
1659 static int __init alsa_card_miro_init(void)
1660 {
1661 #ifdef CONFIG_PNP
1662         pnp_register_card_driver(&miro_pnpc_driver);
1663         if (snd_miro_pnp_is_probed)
1664                 return 0;
1665         pnp_unregister_card_driver(&miro_pnpc_driver);
1666 #endif
1667         return isa_register_driver(&snd_miro_driver, 1);
1668 }
1669
1670 static void __exit alsa_card_miro_exit(void)
1671 {
1672         if (!snd_miro_pnp_is_probed) {
1673                 isa_unregister_driver(&snd_miro_driver);
1674                 return;
1675         }
1676 #ifdef CONFIG_PNP
1677         pnp_unregister_card_driver(&miro_pnpc_driver);
1678 #endif
1679 }
1680
1681 module_init(alsa_card_miro_init)
1682 module_exit(alsa_card_miro_exit)