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