miro.c 35.8 KB
Newer Older
M
Martin Langer 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 *   ALSA soundcard driver for Miro miroSOUND PCM1 pro
 *                                  miroSOUND PCM12
 *                                  miroSOUND PCM20 Radio
 *
 *   Copyright (C) 2004-2005 Martin Langer <martin-langer@gmx.de>
 *
 *   Based on OSS ACI and ALSA OPTi9xx drivers
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <linux/init.h>
#include <linux/err.h>
27
#include <linux/isa.h>
M
Martin Langer 已提交
28 29 30 31 32 33 34
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <linux/moduleparam.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <sound/core.h>
35
#include <sound/wss.h>
M
Martin Langer 已提交
36 37 38 39 40 41 42
#include <sound/mpu401.h>
#include <sound/opl4.h>
#include <sound/control.h>
#include <sound/info.h>
#define SNDRV_LEGACY_FIND_FREE_IRQ
#define SNDRV_LEGACY_FIND_FREE_DMA
#include <sound/initval.h>
43
#include <sound/aci.h>
M
Martin Langer 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

MODULE_AUTHOR("Martin Langer <martin-langer@gmx.de>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Miro miroSOUND PCM1 pro, PCM12, PCM20 Radio");
MODULE_SUPPORTED_DEVICE("{{Miro,miroSOUND PCM1 pro}, "
			"{Miro,miroSOUND PCM12}, "
			"{Miro,miroSOUND PCM20 Radio}}");

static int index = SNDRV_DEFAULT_IDX1;		/* Index 0-MAX */
static char *id = SNDRV_DEFAULT_STR1;		/* ID for this card */
static long port = SNDRV_DEFAULT_PORT1; 	/* 0x530,0xe80,0xf40,0x604 */
static long mpu_port = SNDRV_DEFAULT_PORT1;	/* 0x300,0x310,0x320,0x330 */
static long fm_port = SNDRV_DEFAULT_PORT1;	/* 0x388 */
static int irq = SNDRV_DEFAULT_IRQ1;		/* 5,7,9,10,11 */
static int mpu_irq = SNDRV_DEFAULT_IRQ1;	/* 5,7,9,10 */
static int dma1 = SNDRV_DEFAULT_DMA1;		/* 0,1,3 */
static int dma2 = SNDRV_DEFAULT_DMA1;		/* 0,1,3 */
static int wss;
static int ide;

module_param(index, int, 0444);
MODULE_PARM_DESC(index, "Index value for miro soundcard.");
module_param(id, charp, 0444);
MODULE_PARM_DESC(id, "ID string for miro soundcard.");
module_param(port, long, 0444);
MODULE_PARM_DESC(port, "WSS port # for miro driver.");
module_param(mpu_port, long, 0444);
MODULE_PARM_DESC(mpu_port, "MPU-401 port # for miro driver.");
module_param(fm_port, long, 0444);
MODULE_PARM_DESC(fm_port, "FM Port # for miro driver.");
module_param(irq, int, 0444);
MODULE_PARM_DESC(irq, "WSS irq # for miro driver.");
module_param(mpu_irq, int, 0444);
MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for miro driver.");
module_param(dma1, int, 0444);
MODULE_PARM_DESC(dma1, "1st dma # for miro driver.");
module_param(dma2, int, 0444);
MODULE_PARM_DESC(dma2, "2nd dma # for miro driver.");
module_param(wss, int, 0444);
MODULE_PARM_DESC(wss, "wss mode");
module_param(ide, int, 0444);
MODULE_PARM_DESC(ide, "enable ide port");

#define OPTi9XX_HW_DETECT	0
#define OPTi9XX_HW_82C928	1
#define OPTi9XX_HW_82C929	2
#define OPTi9XX_HW_82C924	3
#define OPTi9XX_HW_82C925	4
#define OPTi9XX_HW_82C930	5
#define OPTi9XX_HW_82C931	6
#define OPTi9XX_HW_82C933	7
#define OPTi9XX_HW_LAST		OPTi9XX_HW_82C933

#define OPTi9XX_MC_REG(n)	n

struct snd_miro {
	unsigned short hardware;
	unsigned char password;
	char name[7];

	struct resource *res_mc_base;
	struct resource *res_aci_port;

	unsigned long mc_base;
	unsigned long mc_base_size;
	unsigned long pwd_reg;

	spinlock_t lock;
	struct snd_pcm *pcm;

	long wss_base;
	int irq;
	int dma1;
	int dma2;

	long mpu_port;
	int mpu_irq;

122
	struct snd_miro_aci *aci;
M
Martin Langer 已提交
123 124
};

125 126
static struct snd_miro_aci aci_device;

M
Martin Langer 已提交
127 128 129 130 131 132 133 134 135 136 137
static char * snd_opti9xx_names[] = {
	"unkown",
	"82C928", "82C929",
	"82C924", "82C925",
	"82C930", "82C931", "82C933"
};

/* 
 *  ACI control
 */

138
static int aci_busy_wait(struct snd_miro_aci *aci)
M
Martin Langer 已提交
139 140 141 142
{
	long timeout;
	unsigned char byte;

143 144 145
	for (timeout = 1; timeout <= ACI_MINTIME + 30; timeout++) {
		byte = inb(aci->aci_port + ACI_REG_BUSY);
		if ((byte & 1) == 0) {
M
Martin Langer 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
			if (timeout >= ACI_MINTIME)
				snd_printd("aci ready in round %ld.\n",
					   timeout-ACI_MINTIME);
			return byte;
		}
		if (timeout >= ACI_MINTIME) {
			long out=10*HZ;
			switch (timeout-ACI_MINTIME) {
			case 0 ... 9:
				out /= 10;
			case 10 ... 19:
				out /= 10;
			case 20 ... 30:
				out /= 10;
			default:
				set_current_state(TASK_UNINTERRUPTIBLE);
				schedule_timeout(out);
				break;
			}
		}
	}
	snd_printk(KERN_ERR "aci_busy_wait() time out\n");
	return -EBUSY;
}

171
static inline int aci_write(struct snd_miro_aci *aci, unsigned char byte)
M
Martin Langer 已提交
172
{
173 174
	if (aci_busy_wait(aci) >= 0) {
		outb(byte, aci->aci_port + ACI_REG_COMMAND);
M
Martin Langer 已提交
175 176 177 178 179 180 181
		return 0;
	} else {
		snd_printk(KERN_ERR "aci busy, aci_write(0x%x) stopped.\n", byte);
		return -EBUSY;
	}
}

182
static inline int aci_read(struct snd_miro_aci *aci)
M
Martin Langer 已提交
183 184 185
{
	unsigned char byte;

186 187
	if (aci_busy_wait(aci) >= 0) {
		byte = inb(aci->aci_port + ACI_REG_STATUS);
M
Martin Langer 已提交
188 189 190 191 192 193 194
		return byte;
	} else {
		snd_printk(KERN_ERR "aci busy, aci_read() stopped.\n");
		return -EBUSY;
	}
}

195
int snd_aci_cmd(struct snd_miro_aci *aci, int write1, int write2, int write3)
M
Martin Langer 已提交
196 197 198 199
{
	int write[] = {write1, write2, write3};
	int value, i;

200
	if (mutex_lock_interruptible(&aci->aci_mutex))
M
Martin Langer 已提交
201 202 203 204 205 206
		return -EINTR;

	for (i=0; i<3; i++) {
		if (write[i]< 0 || write[i] > 255)
			break;
		else {
207
			value = aci_write(aci, write[i]);
M
Martin Langer 已提交
208 209 210 211 212
			if (value < 0)
				goto out;
		}
	}

213
	value = aci_read(aci);
M
Martin Langer 已提交
214

215
out:	mutex_unlock(&aci->aci_mutex);
M
Martin Langer 已提交
216 217
	return value;
}
218 219 220 221 222 223
EXPORT_SYMBOL(snd_aci_cmd);

static int aci_getvalue(struct snd_miro_aci *aci, unsigned char index)
{
	return snd_aci_cmd(aci, ACI_STATUS, index, -1);
}
M
Martin Langer 已提交
224

225 226
static int aci_setvalue(struct snd_miro_aci *aci, unsigned char index,
			int value)
M
Martin Langer 已提交
227
{
228
	return snd_aci_cmd(aci, index, value, -1);
M
Martin Langer 已提交
229 230
}

231
struct snd_miro_aci *snd_aci_get_aci(void)
M
Martin Langer 已提交
232
{
233 234 235
	if (aci_device.aci_port == 0)
		return NULL;
	return &aci_device;
M
Martin Langer 已提交
236
}
237
EXPORT_SYMBOL(snd_aci_get_aci);
M
Martin Langer 已提交
238 239 240 241 242

/*
 *  MIXER part
 */

243
#define snd_miro_info_capture	snd_ctl_boolean_mono_info
M
Martin Langer 已提交
244 245 246 247 248 249 250

static int snd_miro_get_capture(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
	struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
	int value;

251 252 253 254
	value = aci_getvalue(miro->aci, ACI_S_GENERAL);
	if (value < 0) {
		snd_printk(KERN_ERR "snd_miro_get_capture() failed: %d\n",
			   value);
M
Martin Langer 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
		return value;
	}

	ucontrol->value.integer.value[0] = value & 0x20;

	return 0;
}

static int snd_miro_put_capture(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
	struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
	int change, value, error;

	value = !(ucontrol->value.integer.value[0]);

271 272 273 274
	error = aci_setvalue(miro->aci, ACI_SET_SOLOMODE, value);
	if (error < 0) {
		snd_printk(KERN_ERR "snd_miro_put_capture() failed: %d\n",
			   error);
M
Martin Langer 已提交
275 276 277
		return error;
	}

278 279
	change = (value != miro->aci->aci_solomode);
	miro->aci->aci_solomode = value;
M
Martin Langer 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	
	return change;
}

static int snd_miro_info_preamp(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_info *uinfo)
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = 3;

	return 0;
}

static int snd_miro_get_preamp(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
{
	struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
	int value;

301
	if (miro->aci->aci_version <= 176) {
M
Martin Langer 已提交
302 303 304 305 306 307 308

		/* 
		   OSS says it's not readable with versions < 176.
		   But it doesn't work on my card,
		   which is a PCM12 with aci_version = 176.
		*/

309
		ucontrol->value.integer.value[0] = miro->aci->aci_preamp;
M
Martin Langer 已提交
310 311 312
		return 0;
	}

313 314 315 316
	value = aci_getvalue(miro->aci, ACI_GET_PREAMP);
	if (value < 0) {
		snd_printk(KERN_ERR "snd_miro_get_preamp() failed: %d\n",
			   value);
M
Martin Langer 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
		return value;
	}
	
	ucontrol->value.integer.value[0] = value;

	return 0;
}

static int snd_miro_put_preamp(struct snd_kcontrol *kcontrol,
			       struct snd_ctl_elem_value *ucontrol)
{
	struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
	int error, value, change;

	value = ucontrol->value.integer.value[0];

333 334 335 336
	error = aci_setvalue(miro->aci, ACI_SET_PREAMP, value);
	if (error < 0) {
		snd_printk(KERN_ERR "snd_miro_put_preamp() failed: %d\n",
			   error);
M
Martin Langer 已提交
337 338 339
		return error;
	}

340 341
	change = (value != miro->aci->aci_preamp);
	miro->aci->aci_preamp = value;
M
Martin Langer 已提交
342 343 344 345

	return change;
}

346
#define snd_miro_info_amp	snd_ctl_boolean_mono_info
M
Martin Langer 已提交
347 348 349 350 351

static int snd_miro_get_amp(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_value *ucontrol)
{
	struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
352
	ucontrol->value.integer.value[0] = miro->aci->aci_amp;
M
Martin Langer 已提交
353 354 355 356 357 358 359 360 361 362 363 364

	return 0;
}

static int snd_miro_put_amp(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_value *ucontrol)
{
	struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
	int error, value, change;

	value = ucontrol->value.integer.value[0];

365 366
	error = aci_setvalue(miro->aci, ACI_SET_POWERAMP, value);
	if (error < 0) {
M
Martin Langer 已提交
367 368 369 370
		snd_printk(KERN_ERR "snd_miro_put_amp() to %d failed: %d\n", value, error);
		return error;
	}

371 372
	change = (value != miro->aci->aci_amp);
	miro->aci->aci_amp = value;
M
Martin Langer 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420

	return change;
}

#define MIRO_DOUBLE(ctl_name, ctl_index, get_right_reg, set_right_reg) \
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  .name = ctl_name, \
  .index = ctl_index, \
  .info = snd_miro_info_double, \
  .get = snd_miro_get_double, \
  .put = snd_miro_put_double, \
  .private_value = get_right_reg | (set_right_reg << 8) \
}

static int snd_miro_info_double(struct snd_kcontrol *kcontrol, 
				struct snd_ctl_elem_info *uinfo)
{
	int reg = kcontrol->private_value & 0xff;

	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 2;

	if ((reg >= ACI_GET_EQ1) && (reg <= ACI_GET_EQ7)) {

		/* equalizer elements */

		uinfo->value.integer.min = - 0x7f;
		uinfo->value.integer.max = 0x7f;
	} else {

		/* non-equalizer elements */

		uinfo->value.integer.min = 0;
		uinfo->value.integer.max = 0x20;
	}

	return 0;
}

static int snd_miro_get_double(struct snd_kcontrol *kcontrol, 
			       struct snd_ctl_elem_value *uinfo)
{
	struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
	int left_val, right_val;

	int right_reg = kcontrol->private_value & 0xff;
	int left_reg = right_reg + 1;

421 422
	right_val = aci_getvalue(miro->aci, right_reg);
	if (right_val < 0) {
M
Martin Langer 已提交
423 424 425 426
		snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", right_reg, right_val);
		return right_val;
	}

427 428
	left_val = aci_getvalue(miro->aci, left_reg);
	if (left_val < 0) {
M
Martin Langer 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
		snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", left_reg, left_val);
		return left_val;
	}

	if ((right_reg >= ACI_GET_EQ1) && (right_reg <= ACI_GET_EQ7)) {

		/* equalizer elements */

		if (left_val < 0x80) {
			uinfo->value.integer.value[0] = left_val;
		} else {
			uinfo->value.integer.value[0] = 0x80 - left_val;
		}

		if (right_val < 0x80) {
			uinfo->value.integer.value[1] = right_val;
		} else {
			uinfo->value.integer.value[1] = 0x80 - right_val;
		}

	} else {

		/* non-equalizer elements */

		uinfo->value.integer.value[0] = 0x20 - left_val;
		uinfo->value.integer.value[1] = 0x20 - right_val;
	}

	return 0;
}

static int snd_miro_put_double(struct snd_kcontrol *kcontrol, 
			       struct snd_ctl_elem_value *ucontrol)
{
	struct snd_miro *miro = snd_kcontrol_chip(kcontrol);
464
	struct snd_miro_aci *aci = miro->aci;
M
Martin Langer 已提交
465 466 467 468 469 470 471 472
	int left, right, left_old, right_old;
	int setreg_left, setreg_right, getreg_left, getreg_right;
	int change, error;

	left = ucontrol->value.integer.value[0];
	right = ucontrol->value.integer.value[1];

	setreg_right = (kcontrol->private_value >> 8) & 0xff;
473 474 475
	setreg_left = setreg_right + 8;
	if (setreg_right == ACI_SET_MASTER)
		setreg_left -= 7;
M
Martin Langer 已提交
476 477 478 479

	getreg_right = kcontrol->private_value & 0xff;
	getreg_left = getreg_right + 1;

480 481
	left_old = aci_getvalue(aci, getreg_left);
	if (left_old < 0) {
M
Martin Langer 已提交
482 483 484 485
		snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_left, left_old);
		return left_old;
	}

486 487
	right_old = aci_getvalue(aci, getreg_right);
	if (right_old < 0) {
M
Martin Langer 已提交
488 489 490 491 492 493 494 495
		snd_printk(KERN_ERR "aci_getvalue(%d) failed: %d\n", getreg_right, right_old);
		return right_old;
	}

	if ((getreg_right >= ACI_GET_EQ1) && (getreg_right <= ACI_GET_EQ7)) {

		/* equalizer elements */

496 497 498 499
		if (left < -0x7f || left > 0x7f ||
		    right < -0x7f || right > 0x7f)
			return -EINVAL;

M
Martin Langer 已提交
500 501 502 503 504 505
		if (left_old > 0x80) 
			left_old = 0x80 - left_old;
		if (right_old > 0x80) 
			right_old = 0x80 - right_old;

		if (left >= 0) {
506 507
			error = aci_setvalue(aci, setreg_left, left);
			if (error < 0) {
M
Martin Langer 已提交
508 509 510 511 512
				snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
					   left, error);
				return error;
			}
		} else {
513 514
			error = aci_setvalue(aci, setreg_left, 0x80 - left);
			if (error < 0) {
M
Martin Langer 已提交
515 516 517 518 519 520 521
				snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
					   0x80 - left, error);
				return error;
			}
		}

		if (right >= 0) {
522 523
			error = aci_setvalue(aci, setreg_right, right);
			if (error < 0) {
M
Martin Langer 已提交
524 525 526 527 528
				snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
					   right, error);
				return error;
			}
		} else {
529 530
			error = aci_setvalue(aci, setreg_right, 0x80 - right);
			if (error < 0) {
M
Martin Langer 已提交
531 532 533 534 535 536 537 538 539 540
				snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
					   0x80 - right, error);
				return error;
			}
		}

	} else {

		/* non-equalizer elements */

541 542 543 544
		if (left < 0 || left > 0x20 ||
		    right < 0 || right > 0x20)
			return -EINVAL;

M
Martin Langer 已提交
545 546 547
		left_old = 0x20 - left_old;
		right_old = 0x20 - right_old;

548 549
		error = aci_setvalue(aci, setreg_left, 0x20 - left);
		if (error < 0) {
M
Martin Langer 已提交
550 551 552 553
			snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
				   0x20 - left, error);
			return error;
		}
554 555
		error = aci_setvalue(aci, setreg_right, 0x20 - right);
		if (error < 0) {
M
Martin Langer 已提交
556 557 558 559 560 561 562 563 564 565 566
			snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n",
				   0x20 - right, error);
			return error;
		}
	}

	change = (left != left_old) || (right != right_old);

	return change;
}

567
static struct snd_kcontrol_new snd_miro_controls[] __devinitdata = {
M
Martin Langer 已提交
568 569 570 571 572 573 574 575 576 577 578
MIRO_DOUBLE("Master Playback Volume", 0, ACI_GET_MASTER, ACI_SET_MASTER),
MIRO_DOUBLE("Mic Playback Volume", 1, ACI_GET_MIC, ACI_SET_MIC),
MIRO_DOUBLE("Line Playback Volume", 1, ACI_GET_LINE, ACI_SET_LINE),
MIRO_DOUBLE("CD Playback Volume", 0, ACI_GET_CD, ACI_SET_CD),
MIRO_DOUBLE("Synth Playback Volume", 0, ACI_GET_SYNTH, ACI_SET_SYNTH),
MIRO_DOUBLE("PCM Playback Volume", 1, ACI_GET_PCM, ACI_SET_PCM),
MIRO_DOUBLE("Aux Playback Volume", 2, ACI_GET_LINE2, ACI_SET_LINE2),
};

/* Equalizer with seven bands (only PCM20) 
   from -12dB up to +12dB on each band */
579
static struct snd_kcontrol_new snd_miro_eq_controls[] __devinitdata = {
M
Martin Langer 已提交
580 581 582 583 584 585 586 587 588
MIRO_DOUBLE("Tone Control - 28 Hz", 0, ACI_GET_EQ1, ACI_SET_EQ1),
MIRO_DOUBLE("Tone Control - 160 Hz", 0, ACI_GET_EQ2, ACI_SET_EQ2),
MIRO_DOUBLE("Tone Control - 400 Hz", 0, ACI_GET_EQ3, ACI_SET_EQ3),
MIRO_DOUBLE("Tone Control - 1 kHz", 0, ACI_GET_EQ4, ACI_SET_EQ4),
MIRO_DOUBLE("Tone Control - 2.5 kHz", 0, ACI_GET_EQ5, ACI_SET_EQ5),
MIRO_DOUBLE("Tone Control - 6.3 kHz", 0, ACI_GET_EQ6, ACI_SET_EQ6),
MIRO_DOUBLE("Tone Control - 16 kHz", 0, ACI_GET_EQ7, ACI_SET_EQ7),
};

589
static struct snd_kcontrol_new snd_miro_radio_control[] __devinitdata = {
M
Martin Langer 已提交
590 591 592
MIRO_DOUBLE("Radio Playback Volume", 0, ACI_GET_LINE1, ACI_SET_LINE1),
};

593
static struct snd_kcontrol_new snd_miro_line_control[] __devinitdata = {
M
Martin Langer 已提交
594 595 596
MIRO_DOUBLE("Line Playback Volume", 2, ACI_GET_LINE1, ACI_SET_LINE1),
};

597
static struct snd_kcontrol_new snd_miro_preamp_control[] __devinitdata = {
M
Martin Langer 已提交
598 599 600 601 602 603 604 605 606
{
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Mic Boost",
	.index = 1,
	.info = snd_miro_info_preamp,
	.get = snd_miro_get_preamp,
	.put = snd_miro_put_preamp,
}};

607
static struct snd_kcontrol_new snd_miro_amp_control[] __devinitdata = {
M
Martin Langer 已提交
608 609 610 611 612 613 614 615 616
{
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Line Boost",
	.index = 0,
	.info = snd_miro_info_amp,
	.get = snd_miro_get_amp,
	.put = snd_miro_put_amp,
}};

617
static struct snd_kcontrol_new snd_miro_capture_control[] __devinitdata = {
M
Martin Langer 已提交
618 619 620 621 622 623 624 625 626
{
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "PCM Capture Switch",
	.index = 0,
	.info = snd_miro_info_capture,
	.get = snd_miro_get_capture,
	.put = snd_miro_put_capture,
}};

627
static unsigned char aci_init_values[][2] __devinitdata = {
M
Martin Langer 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
	{ ACI_SET_MUTE, 0x00 },
	{ ACI_SET_POWERAMP, 0x00 },
	{ ACI_SET_PREAMP, 0x00 },
	{ ACI_SET_SOLOMODE, 0x00 },
	{ ACI_SET_MIC + 0, 0x20 },
	{ ACI_SET_MIC + 8, 0x20 },
	{ ACI_SET_LINE + 0, 0x20 },
	{ ACI_SET_LINE + 8, 0x20 },
	{ ACI_SET_CD + 0, 0x20 },
	{ ACI_SET_CD + 8, 0x20 },
	{ ACI_SET_PCM + 0, 0x20 },
	{ ACI_SET_PCM + 8, 0x20 },
	{ ACI_SET_LINE1 + 0, 0x20 },
	{ ACI_SET_LINE1 + 8, 0x20 },
	{ ACI_SET_LINE2 + 0, 0x20 },
	{ ACI_SET_LINE2 + 8, 0x20 },
	{ ACI_SET_SYNTH + 0, 0x20 },
	{ ACI_SET_SYNTH + 8, 0x20 },
	{ ACI_SET_MASTER + 0, 0x20 },
	{ ACI_SET_MASTER + 1, 0x20 },
};

650
static int __devinit snd_set_aci_init_values(struct snd_miro *miro)
M
Martin Langer 已提交
651 652
{
	int idx, error;
653
	struct snd_miro_aci *aci = miro->aci;
M
Martin Langer 已提交
654 655 656

	/* enable WSS on PCM1 */

657 658 659
	if ((aci->aci_product == 'A') && wss) {
		error = aci_setvalue(aci, ACI_SET_WSS, wss);
		if (error < 0) {
M
Martin Langer 已提交
660 661 662 663 664 665 666 667
			snd_printk(KERN_ERR "enabling WSS mode failed\n");
			return error;
		}
	}

	/* enable IDE port */

	if (ide) {
668 669
		error = aci_setvalue(aci, ACI_SET_IDE, ide);
		if (error < 0) {
M
Martin Langer 已提交
670 671 672 673 674 675 676
			snd_printk(KERN_ERR "enabling IDE port failed\n");
			return error;
		}
	}

	/* set common aci values */

677 678 679 680
	for (idx = 0; idx < ARRAY_SIZE(aci_init_values); idx++) {
		error = aci_setvalue(aci, aci_init_values[idx][0],
				     aci_init_values[idx][1]);
		if (error < 0) {
M
Martin Langer 已提交
681 682 683 684
			snd_printk(KERN_ERR "aci_setvalue(%d) failed: %d\n", 
				   aci_init_values[idx][0], error);
                        return error;
                }
685 686 687 688
	}
	aci->aci_amp = 0;
	aci->aci_preamp = 0;
	aci->aci_solomode = 1;
M
Martin Langer 已提交
689 690 691 692

	return 0;
}

693 694
static int __devinit snd_miro_mixer(struct snd_card *card,
				    struct snd_miro *miro)
M
Martin Langer 已提交
695 696 697 698
{
	unsigned int idx;
	int err;

699
	if (snd_BUG_ON(!miro || !card))
700
		return -EINVAL;
M
Martin Langer 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718

	switch (miro->hardware) {
	case OPTi9XX_HW_82C924:
		strcpy(card->mixername, "ACI & OPTi924");
		break;
	case OPTi9XX_HW_82C929:
		strcpy(card->mixername, "ACI & OPTi929");
		break;
	default:
		snd_BUG();
		break;
	}

	for (idx = 0; idx < ARRAY_SIZE(snd_miro_controls); idx++) {
		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_controls[idx], miro))) < 0)
			return err;
	}

719 720
	if ((miro->aci->aci_product == 'A') ||
	    (miro->aci->aci_product == 'B')) {
M
Martin Langer 已提交
721 722 723 724 725 726 727
		/* PCM1/PCM12 with power-amp and Line 2 */
		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_line_control[0], miro))) < 0)
			return err;
		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_amp_control[0], miro))) < 0)
			return err;
	}

728 729
	if ((miro->aci->aci_product == 'B') ||
	    (miro->aci->aci_product == 'C')) {
M
Martin Langer 已提交
730 731 732
		/* PCM12/PCM20 with mic-preamp */
		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_preamp_control[0], miro))) < 0)
			return err;
733
		if (miro->aci->aci_version >= 176)
M
Martin Langer 已提交
734 735 736 737
			if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_capture_control[0], miro))) < 0)
				return err;
	}

738
	if (miro->aci->aci_product == 'C') {
M
Martin Langer 已提交
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
		/* PCM20 with radio and 7 band equalizer */
		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_radio_control[0], miro))) < 0)
			return err;
		for (idx = 0; idx < ARRAY_SIZE(snd_miro_eq_controls); idx++) {
			if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_miro_eq_controls[idx], miro))) < 0)
				return err;
		}
	}

	return 0;
}

static long snd_legacy_find_free_ioport(long *port_table, long size)
{
	while (*port_table != -1) {
		struct resource *res;
		if ((res = request_region(*port_table, size, 
					  "ALSA test")) != NULL) {
757
			release_and_free_resource(res);
M
Martin Langer 已提交
758 759 760 761 762 763 764
			return *port_table;
		}
		port_table++;
	}
	return -1;
}

765 766
static int __devinit snd_miro_init(struct snd_miro *chip,
				   unsigned short hardware)
M
Martin Langer 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
{
	static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};

	chip->hardware = hardware;
	strcpy(chip->name, snd_opti9xx_names[hardware]);

	chip->mc_base_size = opti9xx_mc_size[hardware];  

	spin_lock_init(&chip->lock);

	chip->wss_base = -1;
	chip->irq = -1;
	chip->dma1 = -1;
	chip->dma2 = -1;
	chip->mpu_port = -1;
	chip->mpu_irq = -1;

	switch (hardware) {
	case OPTi9XX_HW_82C929:
		chip->mc_base = 0xf8c;
		chip->password = 0xe3;
		chip->pwd_reg = 3;
		break;

	case OPTi9XX_HW_82C924:
		chip->mc_base = 0xf8c;
		chip->password = 0xe5;
		chip->pwd_reg = 3;
		break;

	default:
		snd_printk(KERN_ERR "sorry, no support for %d\n", hardware);
		return -ENODEV;
	}

	return 0;
}

static unsigned char snd_miro_read(struct snd_miro *chip,
				   unsigned char reg)
{
	unsigned long flags;
	unsigned char retval = 0xff;

	spin_lock_irqsave(&chip->lock, flags);
	outb(chip->password, chip->mc_base + chip->pwd_reg);

	switch (chip->hardware) {
	case OPTi9XX_HW_82C924:
		if (reg > 7) {
			outb(reg, chip->mc_base + 8);
			outb(chip->password, chip->mc_base + chip->pwd_reg);
			retval = inb(chip->mc_base + 9);
			break;
		}

	case OPTi9XX_HW_82C929:
		retval = inb(chip->mc_base + reg);
		break;

	default:
		snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
	}

	spin_unlock_irqrestore(&chip->lock, flags);
	return retval;
}

static void snd_miro_write(struct snd_miro *chip, unsigned char reg,
			   unsigned char value)
{
	unsigned long flags;

	spin_lock_irqsave(&chip->lock, flags);
	outb(chip->password, chip->mc_base + chip->pwd_reg);

	switch (chip->hardware) {
	case OPTi9XX_HW_82C924:
		if (reg > 7) {
			outb(reg, chip->mc_base + 8);
			outb(chip->password, chip->mc_base + chip->pwd_reg);
			outb(value, chip->mc_base + 9);
			break;
		}

	case OPTi9XX_HW_82C929:
		outb(value, chip->mc_base + reg);
		break;

	default:
		snd_printk(KERN_ERR "sorry, no support for %d\n", chip->hardware);
	}

	spin_unlock_irqrestore(&chip->lock, flags);
}


#define snd_miro_write_mask(chip, reg, value, mask)	\
	snd_miro_write(chip, reg,			\
		(snd_miro_read(chip, reg) & ~(mask)) | ((value) & (mask)))

/*
 *  Proc Interface
 */

static void snd_miro_proc_read(struct snd_info_entry * entry, 
			       struct snd_info_buffer *buffer)
{
	struct snd_miro *miro = (struct snd_miro *) entry->private_data;
876
	struct snd_miro_aci *aci = miro->aci;
M
Martin Langer 已提交
877 878 879 880 881
	char* model = "unknown";

	/* miroSOUND PCM1 pro, early PCM12 */

	if ((miro->hardware == OPTi9XX_HW_82C929) &&
882 883 884
	    (aci->aci_vendor == 'm') &&
	    (aci->aci_product == 'A')) {
		switch (aci->aci_version) {
M
Martin Langer 已提交
885 886 887 888 889 890 891 892 893 894 895 896
		case 3:
			model = "miroSOUND PCM1 pro";
			break;
		default:
			model = "miroSOUND PCM1 pro / (early) PCM12";
			break;
		}
	}

	/* miroSOUND PCM12, PCM12 (Rev. E), PCM12 pnp */

	if ((miro->hardware == OPTi9XX_HW_82C924) &&
897 898 899
	    (aci->aci_vendor == 'm') &&
	    (aci->aci_product == 'B')) {
		switch (aci->aci_version) {
M
Martin Langer 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
		case 4:
			model = "miroSOUND PCM12";
			break;
		case 176:
			model = "miroSOUND PCM12 (Rev. E)";
			break;
		default:
			model = "miroSOUND PCM12 / PCM12 pnp";
			break;
		}
	}

	/* miroSOUND PCM20 radio */

	if ((miro->hardware == OPTi9XX_HW_82C924) &&
915 916 917
	    (aci->aci_vendor == 'm') &&
	    (aci->aci_product == 'C')) {
		switch (aci->aci_version) {
M
Martin Langer 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
		case 7:
			model = "miroSOUND PCM20 radio (Rev. E)";
			break;
		default:
			model = "miroSOUND PCM20 radio";
			break;
		}
	}

	snd_iprintf(buffer, "\nGeneral information:\n");
	snd_iprintf(buffer, "  model   : %s\n", model);
	snd_iprintf(buffer, "  opti    : %s\n", miro->name);
	snd_iprintf(buffer, "  codec   : %s\n", miro->pcm->name);
	snd_iprintf(buffer, "  port    : 0x%lx\n", miro->wss_base);
	snd_iprintf(buffer, "  irq     : %d\n", miro->irq);
	snd_iprintf(buffer, "  dma     : %d,%d\n\n", miro->dma1, miro->dma2);

	snd_iprintf(buffer, "MPU-401:\n");
	snd_iprintf(buffer, "  port    : 0x%lx\n", miro->mpu_port);
	snd_iprintf(buffer, "  irq     : %d\n\n", miro->mpu_irq);

	snd_iprintf(buffer, "ACI information:\n");
	snd_iprintf(buffer, "  vendor  : ");
941
	switch (aci->aci_vendor) {
M
Martin Langer 已提交
942 943 944 945
	case 'm':
		snd_iprintf(buffer, "Miro\n");
		break;
	default:
946
		snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_vendor);
M
Martin Langer 已提交
947 948 949 950
		break;
	}

	snd_iprintf(buffer, "  product : ");
951
	switch (aci->aci_product) {
M
Martin Langer 已提交
952 953 954 955 956 957 958 959 960 961
	case 'A':
		snd_iprintf(buffer, "miroSOUND PCM1 pro / (early) PCM12\n");
		break;
	case 'B':
		snd_iprintf(buffer, "miroSOUND PCM12\n");
		break;
	case 'C':
		snd_iprintf(buffer, "miroSOUND PCM20 radio\n");
		break;
	default:
962
		snd_iprintf(buffer, "unknown (0x%x)\n", aci->aci_product);
M
Martin Langer 已提交
963 964 965 966
		break;
	}

	snd_iprintf(buffer, "  firmware: %d (0x%x)\n",
967
		    aci->aci_version, aci->aci_version);
M
Martin Langer 已提交
968
	snd_iprintf(buffer, "  port    : 0x%lx-0x%lx\n", 
969
		    aci->aci_port, aci->aci_port+2);
M
Martin Langer 已提交
970 971
	snd_iprintf(buffer, "  wss     : 0x%x\n", wss);
	snd_iprintf(buffer, "  ide     : 0x%x\n", ide);
972 973 974
	snd_iprintf(buffer, "  solomode: 0x%x\n", aci->aci_solomode);
	snd_iprintf(buffer, "  amp     : 0x%x\n", aci->aci_amp);
	snd_iprintf(buffer, "  preamp  : 0x%x\n", aci->aci_preamp);
M
Martin Langer 已提交
975 976
}

977 978
static void __devinit snd_miro_proc_init(struct snd_card *card,
					 struct snd_miro *miro)
M
Martin Langer 已提交
979 980 981
{
	struct snd_info_entry *entry;

982
	if (!snd_card_proc_new(card, "miro", &entry))
983
		snd_info_set_text_ops(entry, miro, snd_miro_proc_read);
M
Martin Langer 已提交
984 985 986 987 988 989
}

/*
 *  Init
 */

990
static int __devinit snd_miro_configure(struct snd_miro *chip)
M
Martin Langer 已提交
991 992 993 994 995 996 997 998
{
	unsigned char wss_base_bits;
	unsigned char irq_bits;
	unsigned char dma_bits;
	unsigned char mpu_port_bits = 0;
	unsigned char mpu_irq_bits;
	unsigned long flags;

999 1000 1001 1002
	snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
	snd_miro_write_mask(chip, OPTi9XX_MC_REG(2), 0x20, 0x20); /* OPL4 */
	snd_miro_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);

M
Martin Langer 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	switch (chip->hardware) {
	case OPTi9XX_HW_82C924:
		snd_miro_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
		snd_miro_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
		break;
	case OPTi9XX_HW_82C929:
		/* untested init commands for OPTi929 */
		snd_miro_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
		break;
	default:
		snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
		return -EINVAL;
	}

	switch (chip->wss_base) {
	case 0x530:
		wss_base_bits = 0x00;
		break;
	case 0x604:
		wss_base_bits = 0x03;
		break;
	case 0xe80:
		wss_base_bits = 0x01;
		break;
	case 0xf40:
		wss_base_bits = 0x02;
		break;
	default:
		snd_printk(KERN_ERR "WSS port 0x%lx not valid\n", chip->wss_base);
		goto __skip_base;
	}
	snd_miro_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);

__skip_base:
	switch (chip->irq) {
	case 5:
		irq_bits = 0x05;
		break;
	case 7:
		irq_bits = 0x01;
		break;
	case 9:
		irq_bits = 0x02;
		break;
	case 10:
		irq_bits = 0x03;
		break;
	case 11:
		irq_bits = 0x04;
		break;
	default:
		snd_printk(KERN_ERR "WSS irq # %d not valid\n", chip->irq);
		goto __skip_resources;
	}

	switch (chip->dma1) {
	case 0:
		dma_bits = 0x01;
		break;
	case 1:
		dma_bits = 0x02;
		break;
	case 3:
		dma_bits = 0x03;
		break;
	default:
		snd_printk(KERN_ERR "WSS dma1 # %d not valid\n", chip->dma1);
		goto __skip_resources;
	}

	if (chip->dma1 == chip->dma2) {
		snd_printk(KERN_ERR "don't want to share dmas\n");
		return -EBUSY;
	}

	switch (chip->dma2) {
	case 0:
	case 1:
		break;
	default:
		snd_printk(KERN_ERR "WSS dma2 # %d not valid\n", chip->dma2);
		goto __skip_resources;
	}
	dma_bits |= 0x04;

	spin_lock_irqsave(&chip->lock, flags);
	outb(irq_bits << 3 | dma_bits, chip->wss_base);
	spin_unlock_irqrestore(&chip->lock, flags);

__skip_resources:
	if (chip->hardware > OPTi9XX_HW_82C928) {
		switch (chip->mpu_port) {
		case 0:
		case -1:
			break;
		case 0x300:
			mpu_port_bits = 0x03;
			break;
		case 0x310:
			mpu_port_bits = 0x02;
			break;
		case 0x320:
			mpu_port_bits = 0x01;
			break;
		case 0x330:
			mpu_port_bits = 0x00;
			break;
		default:
			snd_printk(KERN_ERR "MPU-401 port 0x%lx not valid\n",
				   chip->mpu_port);
			goto __skip_mpu;
		}

		switch (chip->mpu_irq) {
		case 5:
			mpu_irq_bits = 0x02;
			break;
		case 7:
			mpu_irq_bits = 0x03;
			break;
		case 9:
			mpu_irq_bits = 0x00;
			break;
		case 10:
			mpu_irq_bits = 0x01;
			break;
		default:
			snd_printk(KERN_ERR "MPU-401 irq # %d not valid\n",
				   chip->mpu_irq);
			goto __skip_mpu;
		}

		snd_miro_write_mask(chip, OPTi9XX_MC_REG(6),
			(chip->mpu_port <= 0) ? 0x00 :
				0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
			0xf8);
	}
__skip_mpu:

	return 0;
}

1145 1146
static int __devinit snd_card_miro_detect(struct snd_card *card,
					  struct snd_miro *chip)
M
Martin Langer 已提交
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
{
	int i, err;
	unsigned char value;

	for (i = OPTi9XX_HW_82C929; i <= OPTi9XX_HW_82C924; i++) {

		if ((err = snd_miro_init(chip, i)) < 0)
			return err;

		if ((chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size, "OPTi9xx MC")) == NULL)
			continue;

		value = snd_miro_read(chip, OPTi9XX_MC_REG(1));
		if ((value != 0xff) && (value != inb(chip->mc_base + 1)))
			if (value == snd_miro_read(chip, OPTi9XX_MC_REG(1)))
				return 1;

1164
		release_and_free_resource(chip->res_mc_base);
M
Martin Langer 已提交
1165 1166 1167 1168 1169 1170 1171
		chip->res_mc_base = NULL;

	}

	return -ENODEV;
}

1172
static int __devinit snd_card_miro_aci_detect(struct snd_card *card,
1173
					      struct snd_miro *miro)
M
Martin Langer 已提交
1174 1175 1176
{
	unsigned char regval;
	int i;
1177 1178 1179
	struct snd_miro_aci *aci = &aci_device;

	miro->aci = aci;
M
Martin Langer 已提交
1180

1181
	mutex_init(&aci->aci_mutex);
M
Martin Langer 已提交
1182 1183 1184 1185

	/* get ACI port from OPTi9xx MC 4 */

	regval=inb(miro->mc_base + 4);
1186
	aci->aci_port = (regval & 0x10) ? 0x344 : 0x354;
M
Martin Langer 已提交
1187

1188 1189
	miro->res_aci_port = request_region(aci->aci_port, 3, "miro aci");
	if (miro->res_aci_port == NULL) {
M
Martin Langer 已提交
1190
		snd_printk(KERN_ERR "aci i/o area 0x%lx-0x%lx already used.\n", 
1191
			   aci->aci_port, aci->aci_port+2);
M
Martin Langer 已提交
1192 1193 1194 1195 1196
		return -ENOMEM;
	}

        /* force ACI into a known state */
	for (i = 0; i < 3; i++)
1197
		if (snd_aci_cmd(aci, ACI_ERROR_OP, -1, -1) < 0) {
M
Martin Langer 已提交
1198 1199 1200 1201
			snd_printk(KERN_ERR "can't force aci into known state.\n");
			return -ENXIO;
		}

1202 1203 1204 1205 1206
	aci->aci_vendor = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
	aci->aci_product = snd_aci_cmd(aci, ACI_READ_IDCODE, -1, -1);
	if (aci->aci_vendor < 0 || aci->aci_product < 0) {
		snd_printk(KERN_ERR "can't read aci id on 0x%lx.\n",
			   aci->aci_port);
M
Martin Langer 已提交
1207 1208 1209
		return -ENXIO;
	}

1210 1211
	aci->aci_version = snd_aci_cmd(aci, ACI_READ_VERSION, -1, -1);
	if (aci->aci_version < 0) {
M
Martin Langer 已提交
1212
		snd_printk(KERN_ERR "can't read aci version on 0x%lx.\n", 
1213
			   aci->aci_port);
M
Martin Langer 已提交
1214 1215 1216
		return -ENXIO;
	}

1217 1218 1219
	if (snd_aci_cmd(aci, ACI_INIT, -1, -1) < 0 ||
	    snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0 ||
	    snd_aci_cmd(aci, ACI_ERROR_OP, ACI_ERROR_OP, ACI_ERROR_OP) < 0) {
M
Martin Langer 已提交
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
		snd_printk(KERN_ERR "can't initialize aci.\n"); 
		return -ENXIO;
	}

	return 0;
}

static void snd_card_miro_free(struct snd_card *card)
{
	struct snd_miro *miro = card->private_data;
        
	release_and_free_resource(miro->res_aci_port);
1232 1233
	if (miro->aci)
		miro->aci->aci_port = 0;
M
Martin Langer 已提交
1234 1235 1236
	release_and_free_resource(miro->res_mc_base);
}

1237 1238 1239 1240 1241 1242
static int __devinit snd_miro_match(struct device *devptr, unsigned int n)
{
	return 1;
}

static int __devinit snd_miro_probe(struct device *devptr, unsigned int n)
M
Martin Langer 已提交
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
{
	static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
	static long possible_mpu_ports[] = {0x330, 0x300, 0x310, 0x320, -1};
	static int possible_irqs[] = {11, 9, 10, 7, -1};
	static int possible_mpu_irqs[] = {10, 5, 9, 7, -1};
	static int possible_dma1s[] = {3, 1, 0, -1};
	static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};

	int error;
	struct snd_miro *miro;
1253
	struct snd_wss *codec;
M
Martin Langer 已提交
1254 1255 1256 1257 1258
	struct snd_timer *timer;
	struct snd_card *card;
	struct snd_pcm *pcm;
	struct snd_rawmidi *rmidi;

1259 1260 1261 1262
	error = snd_card_create(index, id, THIS_MODULE,
				sizeof(struct snd_miro), &card);
	if (error < 0)
		return error;
M
Martin Langer 已提交
1263 1264 1265

	card->private_free = snd_card_miro_free;
	miro = card->private_data;
1266 1267 1268 1269 1270 1271 1272

	error = snd_card_miro_detect(card, miro);
	if (error < 0) {
		snd_card_free(card);
		snd_printk(KERN_ERR "unable to detect OPTi9xx chip\n");
		return -ENODEV;
	}
M
Martin Langer 已提交
1273 1274 1275 1276 1277 1278 1279 1280

	if ((error = snd_card_miro_aci_detect(card, miro)) < 0) {
		snd_card_free(card);
		snd_printk(KERN_ERR "unable to detect aci chip\n");
		return -ENODEV;
	}

	/* init proc interface */
1281
	snd_miro_proc_init(card, miro);
M
Martin Langer 已提交
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305


	if (! miro->res_mc_base &&
	    (miro->res_mc_base = request_region(miro->mc_base, miro->mc_base_size,
						"miro (OPTi9xx MC)")) == NULL) {
		snd_card_free(card);
		snd_printk(KERN_ERR "request for OPTI9xx MC failed\n");
		return -ENOMEM;
	}

	miro->wss_base = port;
	miro->irq = irq;
	miro->mpu_irq = mpu_irq;
	miro->dma1 = dma1;
	miro->dma2 = dma2;

	if (miro->wss_base == SNDRV_AUTO_PORT) {
		if ((miro->wss_base = snd_legacy_find_free_ioport(possible_ports, 4)) < 0) {
			snd_card_free(card);
			snd_printk(KERN_ERR "unable to find a free WSS port\n");
			return -EBUSY;
		}
	}

1306 1307 1308
	if (mpu_port == SNDRV_AUTO_PORT) {
		mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2);
		if (mpu_port < 0) {
M
Martin Langer 已提交
1309 1310
			snd_card_free(card);
			snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
1311
			return -EBUSY;
M
Martin Langer 已提交
1312 1313
		}
	}
1314 1315
	miro->mpu_port = mpu_port;

M
Martin Langer 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
	if (miro->irq == SNDRV_AUTO_IRQ) {
		if ((miro->irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
			snd_card_free(card);
			snd_printk(KERN_ERR "unable to find a free IRQ\n");
			return -EBUSY;
		}
	}
	if (miro->mpu_irq == SNDRV_AUTO_IRQ) {
		if ((miro->mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
			snd_card_free(card);
			snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
			return -EBUSY;
		}
	}
	if (miro->dma1 == SNDRV_AUTO_DMA) {
		if ((miro->dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
			snd_card_free(card);
			snd_printk(KERN_ERR "unable to find a free DMA1\n");
			return -EBUSY;
		}
	}
	if (miro->dma2 == SNDRV_AUTO_DMA) {
		if ((miro->dma2 = snd_legacy_find_free_dma(possible_dma2s[miro->dma1 % 4])) < 0) {
			snd_card_free(card);
			snd_printk(KERN_ERR "unable to find a free DMA2\n");
			return -EBUSY;
		}
	}

1345 1346
	error = snd_miro_configure(miro);
	if (error) {
M
Martin Langer 已提交
1347 1348 1349 1350
		snd_card_free(card);
		return error;
	}

1351 1352 1353 1354
	error = snd_wss_create(card, miro->wss_base + 4, -1,
				miro->irq, miro->dma1, miro->dma2,
				WSS_HW_AD1845, 0, &codec);
	if (error < 0) {
M
Martin Langer 已提交
1355 1356 1357 1358
		snd_card_free(card);
		return error;
	}

1359 1360
	error = snd_wss_pcm(codec, 0, &pcm);
	if (error < 0)  {
M
Martin Langer 已提交
1361 1362 1363
		snd_card_free(card);
		return error;
	}
1364 1365
	error = snd_wss_mixer(codec);
	if (error < 0) {
M
Martin Langer 已提交
1366 1367 1368
		snd_card_free(card);
		return error;
	}
1369 1370
	error = snd_wss_timer(codec, 0, &timer);
	if (error < 0) {
M
Martin Langer 已提交
1371 1372 1373 1374 1375 1376
		snd_card_free(card);
		return error;
	}

	miro->pcm = pcm;

1377 1378
	error = snd_miro_mixer(card, miro);
	if (error < 0) {
M
Martin Langer 已提交
1379 1380 1381 1382
		snd_card_free(card);
		return error;
	}

1383
	if (miro->aci->aci_vendor == 'm') {
M
Martin Langer 已提交
1384
		/* It looks like a miro sound card. */
1385
		switch (miro->aci->aci_product) {
M
Martin Langer 已提交
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
		case 'A':
			sprintf(card->shortname, 
				"miroSOUND PCM1 pro / PCM12");
			break;
		case 'B':
			sprintf(card->shortname, 
				"miroSOUND PCM12");
			break;
		case 'C':
			sprintf(card->shortname, 
				"miroSOUND PCM20 radio");
			break;
		default:
			sprintf(card->shortname, 
				"unknown miro");
			snd_printk(KERN_INFO "unknown miro aci id\n");
			break;
		}
	} else {
		snd_printk(KERN_INFO "found unsupported aci card\n");
		sprintf(card->shortname, "unknown Cardinal Technologies");
	}

	strcpy(card->driver, "miro");
	sprintf(card->longname, "%s: OPTi%s, %s at 0x%lx, irq %d, dma %d&%d",
		card->shortname, miro->name, pcm->name, miro->wss_base + 4,
		miro->irq, miro->dma1, miro->dma2);

1414
	if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
M
Martin Langer 已提交
1415
		rmidi = NULL;
1416 1417 1418 1419 1420 1421 1422 1423
	else {
		error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
				mpu_port, 0, miro->mpu_irq, IRQF_DISABLED,
				&rmidi);
		if (error < 0)
			snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
				   mpu_port);
	}
M
Martin Langer 已提交
1424

1425
	if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
M
Martin Langer 已提交
1426 1427
		struct snd_opl3 *opl3 = NULL;
		struct snd_opl4 *opl4;
1428
		if (snd_opl4_create(card, fm_port, fm_port - 8,
M
Martin Langer 已提交
1429
				    2, &opl3, &opl4) < 0)
1430 1431
			snd_printk(KERN_WARNING "no OPL4 device at 0x%lx\n",
				   fm_port);
M
Martin Langer 已提交
1432 1433 1434 1435 1436 1437 1438
	}

	if ((error = snd_set_aci_init_values(miro)) < 0) {
		snd_card_free(card);
                return error;
	}

1439
	snd_card_set_dev(card, devptr);
M
Martin Langer 已提交
1440 1441 1442 1443 1444 1445

	if ((error = snd_card_register(card))) {
		snd_card_free(card);
		return error;
	}

1446
	dev_set_drvdata(devptr, card);
M
Martin Langer 已提交
1447 1448 1449
	return 0;
}

1450
static int __devexit snd_miro_remove(struct device *devptr, unsigned int dev)
M
Martin Langer 已提交
1451
{
1452 1453
	snd_card_free(dev_get_drvdata(devptr));
	dev_set_drvdata(devptr, NULL);
M
Martin Langer 已提交
1454 1455 1456
	return 0;
}

1457 1458
#define DEV_NAME "miro"

1459 1460
static struct isa_driver snd_miro_driver = {
	.match		= snd_miro_match,
M
Martin Langer 已提交
1461 1462 1463 1464
	.probe		= snd_miro_probe,
	.remove		= __devexit_p(snd_miro_remove),
	/* FIXME: suspend/resume */
	.driver		= {
1465
		.name	= DEV_NAME
M
Martin Langer 已提交
1466 1467 1468 1469 1470
	},
};

static int __init alsa_card_miro_init(void)
{
1471
	return isa_register_driver(&snd_miro_driver, 1);
M
Martin Langer 已提交
1472 1473 1474 1475
}

static void __exit alsa_card_miro_exit(void)
{
1476
	isa_unregister_driver(&snd_miro_driver);
M
Martin Langer 已提交
1477 1478 1479 1480
}

module_init(alsa_card_miro_init)
module_exit(alsa_card_miro_exit)