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