xref: /linux/sound/isa/opti9xx/miro.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
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 		strscpy(card->mixername, "ACI & OPTi924");
722 		break;
723 	case OPTi9XX_HW_82C929:
724 		strscpy(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 	strscpy(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 char retval = 0xff;
826 
827 	guard(spinlock_irqsave)(&chip->lock);
828 	outb(chip->password, chip->mc_base + chip->pwd_reg);
829 
830 	switch (chip->hardware) {
831 	case OPTi9XX_HW_82C924:
832 		if (reg > 7) {
833 			outb(reg, chip->mc_base + 8);
834 			outb(chip->password, chip->mc_base + chip->pwd_reg);
835 			retval = inb(chip->mc_base + 9);
836 			break;
837 		}
838 		fallthrough;
839 
840 	case OPTi9XX_HW_82C929:
841 		retval = inb(chip->mc_base + reg);
842 		break;
843 
844 	default:
845 		dev_err(chip->card->dev, "sorry, no support for %d\n", chip->hardware);
846 	}
847 
848 	return retval;
849 }
850 
851 static void snd_miro_write(struct snd_miro *chip, unsigned char reg,
852 			   unsigned char value)
853 {
854 	guard(spinlock_irqsave)(&chip->lock);
855 	outb(chip->password, chip->mc_base + chip->pwd_reg);
856 
857 	switch (chip->hardware) {
858 	case OPTi9XX_HW_82C924:
859 		if (reg > 7) {
860 			outb(reg, chip->mc_base + 8);
861 			outb(chip->password, chip->mc_base + chip->pwd_reg);
862 			outb(value, chip->mc_base + 9);
863 			break;
864 		}
865 		fallthrough;
866 
867 	case OPTi9XX_HW_82C929:
868 		outb(value, chip->mc_base + reg);
869 		break;
870 
871 	default:
872 		dev_err(chip->card->dev, "sorry, no support for %d\n", chip->hardware);
873 	}
874 }
875 
876 static inline void snd_miro_write_mask(struct snd_miro *chip,
877 		unsigned char reg, unsigned char value, unsigned char mask)
878 {
879 	unsigned char oldval = snd_miro_read(chip, reg);
880 
881 	snd_miro_write(chip, reg, (oldval & ~mask) | (value & mask));
882 }
883 
884 /*
885  *  Proc Interface
886  */
887 
888 static void snd_miro_proc_read(struct snd_info_entry * entry,
889 			       struct snd_info_buffer *buffer)
890 {
891 	struct snd_miro *miro = (struct snd_miro *) entry->private_data;
892 	struct snd_miro_aci *aci = miro->aci;
893 	char* model = "unknown";
894 
895 	/* miroSOUND PCM1 pro, early PCM12 */
896 
897 	if ((miro->hardware == OPTi9XX_HW_82C929) &&
898 	    (aci->aci_vendor == 'm') &&
899 	    (aci->aci_product == 'A')) {
900 		switch (aci->aci_version) {
901 		case 3:
902 			model = "miroSOUND PCM1 pro";
903 			break;
904 		default:
905 			model = "miroSOUND PCM1 pro / (early) PCM12";
906 			break;
907 		}
908 	}
909 
910 	/* miroSOUND PCM12, PCM12 (Rev. E), PCM12 pnp */
911 
912 	if ((miro->hardware == OPTi9XX_HW_82C924) &&
913 	    (aci->aci_vendor == 'm') &&
914 	    (aci->aci_product == 'B')) {
915 		switch (aci->aci_version) {
916 		case 4:
917 			model = "miroSOUND PCM12";
918 			break;
919 		case 176:
920 			model = "miroSOUND PCM12 (Rev. E)";
921 			break;
922 		default:
923 			model = "miroSOUND PCM12 / PCM12 pnp";
924 			break;
925 		}
926 	}
927 
928 	/* miroSOUND PCM20 radio */
929 
930 	if ((miro->hardware == OPTi9XX_HW_82C924) &&
931 	    (aci->aci_vendor == 'm') &&
932 	    (aci->aci_product == 'C')) {
933 		switch (aci->aci_version) {
934 		case 7:
935 			model = "miroSOUND PCM20 radio (Rev. E)";
936 			break;
937 		default:
938 			model = "miroSOUND PCM20 radio";
939 			break;
940 		}
941 	}
942 
943 	snd_iprintf(buffer, "\nGeneral information:\n");
944 	snd_iprintf(buffer, "  model   : %s\n", model);
945 	snd_iprintf(buffer, "  opti    : %s\n", miro->name);
946 	snd_iprintf(buffer, "  codec   : %s\n", miro->pcm->name);
947 	snd_iprintf(buffer, "  port    : 0x%lx\n", miro->wss_base);
948 	snd_iprintf(buffer, "  irq     : %d\n", miro->irq);
949 	snd_iprintf(buffer, "  dma     : %d,%d\n\n", miro->dma1, miro->dma2);
950 
951 	snd_iprintf(buffer, "MPU-401:\n");
952 	snd_iprintf(buffer, "  port    : 0x%lx\n", miro->mpu_port);
953 	snd_iprintf(buffer, "  irq     : %d\n\n", miro->mpu_irq);
954 
955 	snd_iprintf(buffer, "ACI information:\n");
956 	snd_iprintf(buffer, "  vendor  : ");
957 	switch (aci->aci_vendor) {
958 	case 'm':
959 		snd_iprintf(buffer, "Miro\n");
960 		break;
961 	default:
962 		snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_vendor);
963 		break;
964 	}
965 
966 	snd_iprintf(buffer, "  product : ");
967 	switch (aci->aci_product) {
968 	case 'A':
969 		snd_iprintf(buffer, "miroSOUND PCM1 pro / (early) PCM12\n");
970 		break;
971 	case 'B':
972 		snd_iprintf(buffer, "miroSOUND PCM12\n");
973 		break;
974 	case 'C':
975 		snd_iprintf(buffer, "miroSOUND PCM20 radio\n");
976 		break;
977 	default:
978 		snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_product);
979 		break;
980 	}
981 
982 	snd_iprintf(buffer, "  firmware: %d (0x%x)\n",
983 		    aci->aci_version, aci->aci_version);
984 	snd_iprintf(buffer, "  port    : 0x%lx-0x%lx\n",
985 		    aci->aci_port, aci->aci_port+2);
986 	snd_iprintf(buffer, "  wss     : 0x%x\n", wss);
987 	snd_iprintf(buffer, "  ide     : 0x%x\n", ide);
988 	snd_iprintf(buffer, "  solomode: 0x%x\n", aci->aci_solomode);
989 	snd_iprintf(buffer, "  amp     : 0x%x\n", aci->aci_amp);
990 	snd_iprintf(buffer, "  preamp  : 0x%x\n", aci->aci_preamp);
991 }
992 
993 static void snd_miro_proc_init(struct snd_card *card,
994 			       struct snd_miro *miro)
995 {
996 	snd_card_ro_proc_new(card, "miro", miro, snd_miro_proc_read);
997 }
998 
999 /*
1000  *  Init
1001  */
1002 
1003 static int snd_miro_configure(struct snd_miro *chip)
1004 {
1005 	unsigned char wss_base_bits;
1006 	unsigned char irq_bits;
1007 	unsigned char dma_bits;
1008 	unsigned char mpu_port_bits = 0;
1009 	unsigned char mpu_irq_bits;
1010 
1011 	snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
1012 	snd_miro_write_mask(chip, OPTi9XX_MC_REG(2), 0x20, 0x20); /* OPL4 */
1013 	snd_miro_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
1014 
1015 	switch (chip->hardware) {
1016 	case OPTi9XX_HW_82C924:
1017 		snd_miro_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
1018 		snd_miro_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
1019 		break;
1020 	case OPTi9XX_HW_82C929:
1021 		/* untested init commands for OPTi929 */
1022 		snd_miro_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
1023 		break;
1024 	default:
1025 		dev_err(chip->card->dev, "chip %d not supported\n", chip->hardware);
1026 		return -EINVAL;
1027 	}
1028 
1029 	/* PnP resource says it decodes only 10 bits of address */
1030 	switch (chip->wss_base & 0x3ff) {
1031 	case 0x130:
1032 		chip->wss_base = 0x530;
1033 		wss_base_bits = 0x00;
1034 		break;
1035 	case 0x204:
1036 		chip->wss_base = 0x604;
1037 		wss_base_bits = 0x03;
1038 		break;
1039 	case 0x280:
1040 		chip->wss_base = 0xe80;
1041 		wss_base_bits = 0x01;
1042 		break;
1043 	case 0x340:
1044 		chip->wss_base = 0xf40;
1045 		wss_base_bits = 0x02;
1046 		break;
1047 	default:
1048 		dev_err(chip->card->dev, "WSS port 0x%lx not valid\n", chip->wss_base);
1049 		goto __skip_base;
1050 	}
1051 	snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
1052 
1053 __skip_base:
1054 	switch (chip->irq) {
1055 	case 5:
1056 		irq_bits = 0x05;
1057 		break;
1058 	case 7:
1059 		irq_bits = 0x01;
1060 		break;
1061 	case 9:
1062 		irq_bits = 0x02;
1063 		break;
1064 	case 10:
1065 		irq_bits = 0x03;
1066 		break;
1067 	case 11:
1068 		irq_bits = 0x04;
1069 		break;
1070 	default:
1071 		dev_err(chip->card->dev, "WSS irq # %d not valid\n", chip->irq);
1072 		goto __skip_resources;
1073 	}
1074 
1075 	switch (chip->dma1) {
1076 	case 0:
1077 		dma_bits = 0x01;
1078 		break;
1079 	case 1:
1080 		dma_bits = 0x02;
1081 		break;
1082 	case 3:
1083 		dma_bits = 0x03;
1084 		break;
1085 	default:
1086 		dev_err(chip->card->dev, "WSS dma1 # %d not valid\n", chip->dma1);
1087 		goto __skip_resources;
1088 	}
1089 
1090 	if (chip->dma1 == chip->dma2) {
1091 		dev_err(chip->card->dev, "don't want to share dmas\n");
1092 		return -EBUSY;
1093 	}
1094 
1095 	switch (chip->dma2) {
1096 	case 0:
1097 	case 1:
1098 		break;
1099 	default:
1100 		dev_err(chip->card->dev, "WSS dma2 # %d not valid\n", chip->dma2);
1101 		goto __skip_resources;
1102 	}
1103 	dma_bits |= 0x04;
1104 
1105 	scoped_guard(spinlock_irqsave, &chip->lock) {
1106 		outb(irq_bits << 3 | dma_bits, chip->wss_base);
1107 	}
1108 
1109 __skip_resources:
1110 	if (chip->hardware > OPTi9XX_HW_82C928) {
1111 		switch (chip->mpu_port) {
1112 		case 0:
1113 		case -1:
1114 			break;
1115 		case 0x300:
1116 			mpu_port_bits = 0x03;
1117 			break;
1118 		case 0x310:
1119 			mpu_port_bits = 0x02;
1120 			break;
1121 		case 0x320:
1122 			mpu_port_bits = 0x01;
1123 			break;
1124 		case 0x330:
1125 			mpu_port_bits = 0x00;
1126 			break;
1127 		default:
1128 			dev_err(chip->card->dev, "MPU-401 port 0x%lx not valid\n",
1129 				chip->mpu_port);
1130 			goto __skip_mpu;
1131 		}
1132 
1133 		switch (chip->mpu_irq) {
1134 		case 5:
1135 			mpu_irq_bits = 0x02;
1136 			break;
1137 		case 7:
1138 			mpu_irq_bits = 0x03;
1139 			break;
1140 		case 9:
1141 			mpu_irq_bits = 0x00;
1142 			break;
1143 		case 10:
1144 			mpu_irq_bits = 0x01;
1145 			break;
1146 		default:
1147 			dev_err(chip->card->dev, "MPU-401 irq # %d not valid\n",
1148 				chip->mpu_irq);
1149 			goto __skip_mpu;
1150 		}
1151 
1152 		snd_miro_write_mask(chip, OPTi9XX_MC_REG(6),
1153 			(chip->mpu_port <= 0) ? 0x00 :
1154 				0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
1155 			0xf8);
1156 	}
1157 __skip_mpu:
1158 
1159 	return 0;
1160 }
1161 
1162 static int snd_miro_opti_check(struct snd_card *card, struct snd_miro *chip)
1163 {
1164 	unsigned char value;
1165 
1166 	chip->res_mc_base =
1167 		devm_request_region(card->dev, chip->mc_base,
1168 				    chip->mc_base_size, "OPTi9xx MC");
1169 	if (chip->res_mc_base == NULL)
1170 		return -ENOMEM;
1171 
1172 	value = snd_miro_read(chip, OPTi9XX_MC_REG(1));
1173 	if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
1174 		if (value == snd_miro_read(chip, OPTi9XX_MC_REG(1)))
1175 			return 0;
1176 
1177 	devm_release_resource(card->dev, chip->res_mc_base);
1178 	chip->res_mc_base = NULL;
1179 
1180 	return -ENODEV;
1181 }
1182 
1183 static int snd_card_miro_detect(struct snd_card *card,
1184 				struct snd_miro *chip)
1185 {
1186 	int i, err;
1187 
1188 	for (i = OPTi9XX_HW_82C929; i <= OPTi9XX_HW_82C924; i++) {
1189 
1190 		err = snd_miro_init(chip, i);
1191 		if (err < 0)
1192 			return err;
1193 
1194 		err = snd_miro_opti_check(card, chip);
1195 		if (err == 0)
1196 			return 1;
1197 	}
1198 
1199 	return -ENODEV;
1200 }
1201 
1202 static int snd_card_miro_aci_detect(struct snd_card *card,
1203 				    struct snd_miro *miro)
1204 {
1205 	unsigned char regval;
1206 	int i;
1207 	struct snd_miro_aci *aci = &aci_device;
1208 
1209 	miro->aci = aci;
1210 
1211 	aci->card = card;
1212 	mutex_init(&aci->aci_mutex);
1213 
1214 	/* get ACI port from OPTi9xx MC 4 */
1215 
1216 	regval=inb(miro->mc_base + 4);
1217 	aci->aci_port = (regval & 0x10) ? 0x344 : 0x354;
1218 
1219 	miro->res_aci_port =
1220 		devm_request_region(card->dev, aci->aci_port, 3, "miro aci");
1221 	if (miro->res_aci_port == NULL) {
1222 		dev_err(card->dev, "aci i/o area 0x%lx-0x%lx already used.\n",
1223 			aci->aci_port, aci->aci_port+2);
1224 		return -ENOMEM;
1225 	}
1226 
1227         /* force ACI into a known state */
1228 	for (i = 0; i < 3; i++)
1229 		if (snd_aci_cmd(aci, ACI_ERROR_OP, -1, -1) < 0) {
1230 			dev_err(card->dev, "can't force aci into known state.\n");
1231 			return -ENXIO;
1232 		}
1233 
1234 	aci->aci_vendor = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
1235 	aci->aci_product = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
1236 	if (aci->aci_vendor < 0 || aci->aci_product < 0) {
1237 		dev_err(card->dev, "can't read aci id on 0x%lx.\n",
1238 			aci->aci_port);
1239 		return -ENXIO;
1240 	}
1241 
1242 	aci->aci_version = snd_aci_cmd(aci, ACI_READ_VERSION, -1, -1);
1243 	if (aci->aci_version < 0) {
1244 		dev_err(card->dev, "can't read aci version on 0x%lx.\n",
1245 			aci->aci_port);
1246 		return -ENXIO;
1247 	}
1248 
1249 	if (snd_aci_cmd(aci, ACI_INIT, -1, -1) < 0 ||
1250 	    snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0 ||
1251 	    snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0) {
1252 		dev_err(card->dev, "can't initialize aci.\n");
1253 		return -ENXIO;
1254 	}
1255 
1256 	return 0;
1257 }
1258 
1259 static int snd_miro_probe(struct snd_card *card)
1260 {
1261 	int error;
1262 	struct snd_miro *miro = card->private_data;
1263 	struct snd_wss *codec;
1264 	struct snd_rawmidi *rmidi;
1265 
1266 	if (!miro->res_mc_base) {
1267 		miro->res_mc_base = devm_request_region(card->dev,
1268 							miro->mc_base,
1269 							miro->mc_base_size,
1270 							"miro (OPTi9xx MC)");
1271 		if (miro->res_mc_base == NULL) {
1272 			dev_err(card->dev, "request for OPTI9xx MC failed\n");
1273 			return -ENOMEM;
1274 		}
1275 	}
1276 
1277 	error = snd_card_miro_aci_detect(card, miro);
1278 	if (error < 0) {
1279 		dev_err(card->dev, "unable to detect aci chip\n");
1280 		return -ENODEV;
1281 	}
1282 
1283 	miro->wss_base = port;
1284 	miro->mpu_port = mpu_port;
1285 	miro->irq = irq;
1286 	miro->mpu_irq = mpu_irq;
1287 	miro->dma1 = dma1;
1288 	miro->dma2 = dma2;
1289 
1290 	/* init proc interface */
1291 	snd_miro_proc_init(card, miro);
1292 
1293 	error = snd_miro_configure(miro);
1294 	if (error)
1295 		return error;
1296 
1297 	error = snd_wss_create(card, miro->wss_base + 4, -1,
1298 			       miro->irq, miro->dma1, miro->dma2,
1299 			       WSS_HW_DETECT, 0, &codec);
1300 	if (error < 0)
1301 		return error;
1302 
1303 	error = snd_wss_pcm(codec, 0);
1304 	if (error < 0)
1305 		return error;
1306 
1307 	error = snd_wss_mixer(codec);
1308 	if (error < 0)
1309 		return error;
1310 
1311 	error = snd_wss_timer(codec, 0);
1312 	if (error < 0)
1313 		return error;
1314 
1315 	miro->pcm = codec->pcm;
1316 
1317 	error = snd_miro_mixer(card, miro);
1318 	if (error < 0)
1319 		return error;
1320 
1321 	if (miro->aci->aci_vendor == 'm') {
1322 		/* It looks like a miro sound card. */
1323 		switch (miro->aci->aci_product) {
1324 		case 'A':
1325 			sprintf(card->shortname,
1326 				"miroSOUND PCM1 pro / PCM12");
1327 			break;
1328 		case 'B':
1329 			sprintf(card->shortname,
1330 				"miroSOUND PCM12");
1331 			break;
1332 		case 'C':
1333 			sprintf(card->shortname,
1334 				"miroSOUND PCM20 radio");
1335 			break;
1336 		default:
1337 			sprintf(card->shortname,
1338 				"unknown miro");
1339 			dev_info(card->dev, "unknown miro aci id\n");
1340 			break;
1341 		}
1342 	} else {
1343 		dev_info(card->dev, "found unsupported aci card\n");
1344 		sprintf(card->shortname, "unknown Cardinal Technologies");
1345 	}
1346 
1347 	strscpy(card->driver, "miro");
1348 	scnprintf(card->longname, sizeof(card->longname),
1349 		  "%s: OPTi%s, %s at 0x%lx, irq %d, dma %d&%d",
1350 		  card->shortname, miro->name, codec->pcm->name,
1351 		  miro->wss_base + 4, miro->irq, miro->dma1, miro->dma2);
1352 
1353 	if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
1354 		rmidi = NULL;
1355 	else {
1356 		error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
1357 				mpu_port, 0, miro->mpu_irq, &rmidi);
1358 		if (error < 0)
1359 			dev_warn(card->dev, "no MPU-401 device at 0x%lx?\n",
1360 				 mpu_port);
1361 	}
1362 
1363 	if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
1364 		struct snd_opl3 *opl3 = NULL;
1365 		struct snd_opl4 *opl4;
1366 
1367 		if (snd_opl4_create(card, fm_port, fm_port - 8,
1368 				    2, &opl3, &opl4) < 0)
1369 			dev_warn(card->dev, "no OPL4 device at 0x%lx\n",
1370 				 fm_port);
1371 	}
1372 
1373 	error = snd_set_aci_init_values(miro);
1374 	if (error < 0)
1375                 return error;
1376 
1377 	return snd_card_register(card);
1378 }
1379 
1380 static int snd_miro_isa_match(struct device *devptr, unsigned int n)
1381 {
1382 #ifdef CONFIG_PNP
1383 	if (snd_miro_pnp_is_probed)
1384 		return 0;
1385 	if (isapnp)
1386 		return 0;
1387 #endif
1388 	return 1;
1389 }
1390 
1391 static int snd_miro_isa_probe(struct device *devptr, unsigned int n)
1392 {
1393 	static const long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
1394 	static const long possible_mpu_ports[] = {0x330, 0x300, 0x310, 0x320, -1};
1395 	static const int possible_irqs[] = {11, 9, 10, 7, -1};
1396 	static const int possible_mpu_irqs[] = {10, 5, 9, 7, -1};
1397 	static const int possible_dma1s[] = {3, 1, 0, -1};
1398 	static const int possible_dma2s[][2] = { {1, -1}, {0, -1}, {-1, -1},
1399 					   {0, -1} };
1400 
1401 	int error;
1402 	struct snd_miro *miro;
1403 	struct snd_card *card;
1404 
1405 	error = snd_devm_card_new(devptr, index, id, THIS_MODULE,
1406 				  sizeof(struct snd_miro), &card);
1407 	if (error < 0)
1408 		return error;
1409 
1410 	miro = card->private_data;
1411 
1412 	error = snd_card_miro_detect(card, miro);
1413 	if (error < 0) {
1414 		dev_err(card->dev, "unable to detect OPTi9xx chip\n");
1415 		return -ENODEV;
1416 	}
1417 
1418 	if (port == SNDRV_AUTO_PORT) {
1419 		port = snd_legacy_find_free_ioport(possible_ports, 4);
1420 		if (port < 0) {
1421 			dev_err(card->dev, "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 			dev_err(card->dev,
1430 				"unable to find a free MPU401 port\n");
1431 			return -EBUSY;
1432 		}
1433 	}
1434 
1435 	if (irq == SNDRV_AUTO_IRQ) {
1436 		irq = snd_legacy_find_free_irq(possible_irqs);
1437 		if (irq < 0) {
1438 			dev_err(card->dev, "unable to find a free IRQ\n");
1439 			return -EBUSY;
1440 		}
1441 	}
1442 	if (mpu_irq == SNDRV_AUTO_IRQ) {
1443 		mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs);
1444 		if (mpu_irq < 0) {
1445 			dev_err(card->dev,
1446 				"unable to find a free MPU401 IRQ\n");
1447 			return -EBUSY;
1448 		}
1449 	}
1450 	if (dma1 == SNDRV_AUTO_DMA) {
1451 		dma1 = snd_legacy_find_free_dma(possible_dma1s);
1452 		if (dma1 < 0) {
1453 			dev_err(card->dev, "unable to find a free DMA1\n");
1454 			return -EBUSY;
1455 		}
1456 	}
1457 	if (dma2 == SNDRV_AUTO_DMA) {
1458 		dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4]);
1459 		if (dma2 < 0) {
1460 			dev_err(card->dev, "unable to find a free DMA2\n");
1461 			return -EBUSY;
1462 		}
1463 	}
1464 
1465 	error = snd_miro_probe(card);
1466 	if (error < 0)
1467 		return error;
1468 
1469 	dev_set_drvdata(devptr, card);
1470 	return 0;
1471 }
1472 
1473 #define DEV_NAME "miro"
1474 
1475 static struct isa_driver snd_miro_driver = {
1476 	.match		= snd_miro_isa_match,
1477 	.probe		= snd_miro_isa_probe,
1478 	/* FIXME: suspend/resume */
1479 	.driver		= {
1480 		.name	= DEV_NAME
1481 	},
1482 };
1483 
1484 #ifdef CONFIG_PNP
1485 
1486 static int snd_card_miro_pnp(struct snd_miro *chip,
1487 			     struct pnp_card_link *card,
1488 			     const struct pnp_card_device_id *pid)
1489 {
1490 	struct pnp_dev *pdev;
1491 	int err;
1492 	struct pnp_dev *devmpu;
1493 	struct pnp_dev *devmc;
1494 
1495 	pdev = pnp_request_card_device(card, pid->devs[0].id, NULL);
1496 	if (pdev == NULL)
1497 		return -EBUSY;
1498 
1499 	devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
1500 	if (devmpu == NULL)
1501 		return -EBUSY;
1502 
1503 	devmc = pnp_request_card_device(card, pid->devs[2].id, NULL);
1504 	if (devmc == NULL)
1505 		return -EBUSY;
1506 
1507 	err = pnp_activate_dev(pdev);
1508 	if (err < 0) {
1509 		dev_err(chip->card->dev, "AUDIO pnp configure failure: %d\n", err);
1510 		return err;
1511 	}
1512 
1513 	err = pnp_activate_dev(devmc);
1514 	if (err < 0) {
1515 		dev_err(chip->card->dev, "MC pnp configure failure: %d\n",
1516 			err);
1517 		return err;
1518 	}
1519 
1520 	port = pnp_port_start(pdev, 1);
1521 	fm_port = pnp_port_start(pdev, 2) + 8;
1522 
1523 	/*
1524 	 * The MC(0) is never accessed and the miroSOUND PCM20 card does not
1525 	 * include it in the PnP resource range. OPTI93x include it.
1526 	 */
1527 	chip->mc_base = pnp_port_start(devmc, 0) - 1;
1528 	chip->mc_base_size = pnp_port_len(devmc, 0) + 1;
1529 
1530 	irq = pnp_irq(pdev, 0);
1531 	dma1 = pnp_dma(pdev, 0);
1532 	dma2 = pnp_dma(pdev, 1);
1533 
1534 	if (mpu_port > 0) {
1535 		err = pnp_activate_dev(devmpu);
1536 		if (err < 0) {
1537 			dev_err(chip->card->dev, "MPU401 pnp configure failure\n");
1538 			mpu_port = -1;
1539 			return err;
1540 		}
1541 		mpu_port = pnp_port_start(devmpu, 0);
1542 		mpu_irq = pnp_irq(devmpu, 0);
1543 	}
1544 	return 0;
1545 }
1546 
1547 static int snd_miro_pnp_probe(struct pnp_card_link *pcard,
1548 			      const struct pnp_card_device_id *pid)
1549 {
1550 	struct snd_card *card;
1551 	int err;
1552 	struct snd_miro *miro;
1553 
1554 	if (snd_miro_pnp_is_probed)
1555 		return -EBUSY;
1556 	if (!isapnp)
1557 		return -ENODEV;
1558 	err = snd_devm_card_new(&pcard->card->dev, index, id, THIS_MODULE,
1559 				sizeof(struct snd_miro), &card);
1560 	if (err < 0)
1561 		return err;
1562 
1563 	miro = card->private_data;
1564 	miro->card = card;
1565 
1566 	err = snd_card_miro_pnp(miro, pcard, pid);
1567 	if (err)
1568 		return err;
1569 
1570 	/* only miroSOUND PCM20 and PCM12 == OPTi924 */
1571 	err = snd_miro_init(miro, OPTi9XX_HW_82C924);
1572 	if (err)
1573 		return err;
1574 
1575 	err = snd_miro_opti_check(card, miro);
1576 	if (err) {
1577 		dev_err(card->dev, "OPTI chip not found\n");
1578 		return err;
1579 	}
1580 
1581 	err = snd_miro_probe(card);
1582 	if (err < 0)
1583 		return err;
1584 	pnp_set_card_drvdata(pcard, card);
1585 	snd_miro_pnp_is_probed = 1;
1586 	return 0;
1587 }
1588 
1589 static void snd_miro_pnp_remove(struct pnp_card_link *pcard)
1590 {
1591 	snd_miro_pnp_is_probed = 0;
1592 }
1593 
1594 static struct pnp_card_driver miro_pnpc_driver = {
1595 	.flags		= PNP_DRIVER_RES_DISABLE,
1596 	.name		= "miro",
1597 	.id_table	= snd_miro_pnpids,
1598 	.probe		= snd_miro_pnp_probe,
1599 	.remove		= snd_miro_pnp_remove,
1600 };
1601 #endif
1602 
1603 static int __init alsa_card_miro_init(void)
1604 {
1605 #ifdef CONFIG_PNP
1606 	pnp_register_card_driver(&miro_pnpc_driver);
1607 	if (snd_miro_pnp_is_probed)
1608 		return 0;
1609 	pnp_unregister_card_driver(&miro_pnpc_driver);
1610 #endif
1611 	return isa_register_driver(&snd_miro_driver, 1);
1612 }
1613 
1614 static void __exit alsa_card_miro_exit(void)
1615 {
1616 	if (!snd_miro_pnp_is_probed) {
1617 		isa_unregister_driver(&snd_miro_driver);
1618 		return;
1619 	}
1620 #ifdef CONFIG_PNP
1621 	pnp_unregister_card_driver(&miro_pnpc_driver);
1622 #endif
1623 }
1624 
1625 module_init(alsa_card_miro_init)
1626 module_exit(alsa_card_miro_exit)
1627