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