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