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