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