cs4232.c 12.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*
 * Copyright (C) by Hannu Savolainen 1993-1997
 *
 *	cs4232.c
 *
 * The low level driver for Crystal CS4232 based cards. The CS4232 is
 * a PnP compatible chip which contains a CS4231A codec, SB emulation,
 * a MPU401 compatible MIDI port, joystick and synthesizer and IDE CD-ROM 
 * interfaces. This is just a temporary driver until full PnP support
 * gets implemented. Just the WSS codec, FM synth and the MIDI ports are
 * supported. Other interfaces are left uninitialized.
 *
 * ifdef ...WAVEFRONT...
 * 
 *   Support is provided for initializing the WaveFront synth
 *   interface as well, which is logical device #4. Note that if
 *   you have a Tropez+ card, you probably don't need to setup
 *   the CS4232-supported MIDI interface, since it corresponds to
 *   the internal 26-pin header that's hard to access. Using this
 *   requires an additional IRQ, a resource none too plentiful in
 *   this environment. Just don't set module parameters mpuio and
 *   mpuirq, and the MIDI port will be left uninitialized. You can
 *   still use the ICS2115 hosted MIDI interface which corresponds
 *   to the 9-pin D connector on the back of the card.
 *
 * endif  ...WAVEFRONT...
 *
 * Supported chips are:
 *      CS4232
 *      CS4236
 *      CS4236B
 *
 * Note: You will need a PnP config setup to initialise some CS4232 boards
 * anyway.
 *
 * Changes
 *	Alan Cox		Modularisation, Basic cleanups.
 *      Paul Barton-Davis	Separated MPU configuration, added
 *                                       Tropez+ (WaveFront) support
 *	Christoph Hellwig	Adapted to module_init/module_exit,
 * 					simple cleanups
 * 	Arnaldo C. de Melo	got rid of attach_uart401
 *	Bartlomiej Zolnierkiewicz
 *				Added some __init/__initdata/__exit
Linus Torvalds's avatar
Linus Torvalds committed
45
 *	Marcus Meissner		Added ISA PnP support.
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48
 */

#include <linux/config.h>
Linus Torvalds's avatar
Linus Torvalds committed
49
#include <linux/isapnp.h>
Linus Torvalds's avatar
Linus Torvalds committed
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
#include <linux/module.h>
#include <linux/init.h>

#include "sound_config.h"

#include "cs4232.h"
#include "ad1848.h"
#include "mpu401.h"

#define KEY_PORT	0x279	/* Same as LPT1 status port */
#define CSN_NUM		0x99	/* Just a random number */

static void CS_OUT(unsigned char a)
{
	outb(a, KEY_PORT);
}

#define CS_OUT2(a, b)		{CS_OUT(a);CS_OUT(b);}
#define CS_OUT3(a, b, c)	{CS_OUT(a);CS_OUT(b);CS_OUT(c);}

static int mpu_base = 0, mpu_irq = 0;
static int synth_base = 0, synth_irq = 0;
static int mpu_detected = 0;

int __init probe_cs4232_mpu(struct address_info *hw_config)
{
	/*
	 *	Just write down the config values.
	 */

	mpu_base = hw_config->io_base;
	mpu_irq = hw_config->irq;

	return 1;
}

static unsigned char crystal_key[] __initdata =	/* A 32 byte magic key sequence */
{
	0x96, 0x35, 0x9a, 0xcd, 0xe6, 0xf3, 0x79, 0xbc,
	0x5e, 0xaf, 0x57, 0x2b, 0x15, 0x8a, 0xc5, 0xe2,
	0xf1, 0xf8, 0x7c, 0x3e, 0x9f, 0x4f, 0x27, 0x13,
	0x09, 0x84, 0x42, 0xa1, 0xd0, 0x68, 0x34, 0x1a
};

static void sleep(unsigned howlong)
{
	current->state = TASK_INTERRUPTIBLE;
	schedule_timeout(howlong);
}

Linus Torvalds's avatar
Linus Torvalds committed
100
int __init probe_cs4232(struct address_info *hw_config, int isapnp_configured)
Linus Torvalds's avatar
Linus Torvalds committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114
{
	int i, n;
	int base = hw_config->io_base, irq = hw_config->irq;
	int dma1 = hw_config->dma, dma2 = hw_config->dma2;

	/*
	 * Verify that the I/O port range is free.
	 */

	if (check_region(base, 4))
	{
		printk(KERN_ERR "cs4232.c: I/O port 0x%03x not free\n", base);
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
115
	if (ad1848_detect(hw_config->io_base, NULL, hw_config->osp)) {
Linus Torvalds's avatar
Linus Torvalds committed
116
		return 1;	/* The card is already active */
Linus Torvalds's avatar
Linus Torvalds committed
117 118 119 120 121
	}
	if (isapnp_configured) {
		printk(KERN_ERR "cs4232.c: ISA PnP configured, but not detected?\n");
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

	/*
	 * This version of the driver doesn't use the PnP method when configuring
	 * the card but a simplified method defined by Crystal. This means that
	 * just one CS4232 compatible device can exist on the system. Also this
	 * method conflicts with possible PnP support in the OS. For this reason 
	 * driver is just a temporary kludge.
	 *
	 * Also the Cirrus/Crystal method doesnt always work. Try ISA PnP first ;)
	 */

	/*
	 * Repeat initialization few times since it doesn't always succeed in
	 * first time.
	 */

	for (n = 0; n < 4; n++)
	{	
		/*
		 *	Wake up the card by sending a 32 byte Crystal key to the key port.
		 */
		
		for (i = 0; i < 32; i++)
			CS_OUT(crystal_key[i]);

		sleep(HZ / 10);

		/*
		 *	Now set the CSN (Card Select Number).
		 */

		CS_OUT2(0x06, CSN_NUM);

		/*
		 *	Then set some config bytes. First logical device 0 
		 */

		CS_OUT2(0x15, 0x00);	/* Select logical device 0 (WSS/SB/FM) */
		CS_OUT3(0x47, (base >> 8) & 0xff, base & 0xff);	/* WSS base */

		if (check_region(0x388, 4))	/* Not free */
			CS_OUT3(0x48, 0x00, 0x00)	/* FM base off */
		else
			CS_OUT3(0x48, 0x03, 0x88);	/* FM base 0x388 */

		CS_OUT3(0x42, 0x00, 0x00);	/* SB base off */
		CS_OUT2(0x22, irq);		/* SB+WSS IRQ */
		CS_OUT2(0x2a, dma1);		/* SB+WSS DMA */

		if (dma2 != -1)
			CS_OUT2(0x25, dma2)	/* WSS DMA2 */
		else
			CS_OUT2(0x25, 4);	/* No WSS DMA2 */

		CS_OUT2(0x33, 0x01);	/* Activate logical dev 0 */

		sleep(HZ / 10);

		/*
		 * Initialize logical device 3 (MPU)
		 */

		if (mpu_base != 0 && mpu_irq != 0)
		{
			CS_OUT2(0x15, 0x03);	/* Select logical device 3 (MPU) */
			CS_OUT3(0x47, (mpu_base >> 8) & 0xff, mpu_base & 0xff);	/* MPU base */
			CS_OUT2(0x22, mpu_irq);	/* MPU IRQ */
			CS_OUT2(0x33, 0x01);	/* Activate logical dev 3 */
		}

		if(synth_base != 0)
		{
		    CS_OUT2 (0x15, 0x04);	        /* logical device 4 (WaveFront) */
		    CS_OUT3 (0x47, (synth_base >> 8) & 0xff,
			     synth_base & 0xff);	/* base */
		    CS_OUT2 (0x22, synth_irq);     	/* IRQ */
		    CS_OUT2 (0x33, 0x01);	        /* Activate logical dev 4 */
		}

		/*
		 * Finally activate the chip
		 */
		
		CS_OUT(0x79);

		sleep(HZ / 5);

		/*
		 * Then try to detect the codec part of the chip
		 */

		if (ad1848_detect(hw_config->io_base, NULL, hw_config->osp))
			return 1;
		
		sleep(HZ);
	}
	return 0;
}

void __init attach_cs4232(struct address_info *hw_config)
{
	int base = hw_config->io_base,
		irq = hw_config->irq,
		dma1 = hw_config->dma,
		dma2 = hw_config->dma2;

	if (base == -1 || irq == -1 || dma1 == -1) {
		printk(KERN_ERR "cs4232: dma, irq and io must be set.\n");
		return;
	}

	if (dma2 == -1)
		dma2 = dma1;

	hw_config->slots[0] = ad1848_init("Crystal audio controller", base,
					  irq,
					  dma1,		/* Playback DMA */
					  dma2,		/* Capture DMA */
					  0,
					  hw_config->osp,
					  THIS_MODULE);

	if (hw_config->slots[0] != -1 &&
		audio_devs[hw_config->slots[0]]->mixer_dev!=-1)
	{	
		/* Assume the mixer map is as suggested in the CS4232 databook */
		AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_LINE);
		AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_CD);
		AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_SYNTH);		/* FM synth */
	}
	if (mpu_base != 0 && mpu_irq != 0)
	{
		static struct address_info hw_config2 = {
			0
		};		/* Ensure it's initialized */

		hw_config2.io_base = mpu_base;
		hw_config2.irq = mpu_irq;
		hw_config2.dma = -1;
		hw_config2.dma2 = -1;
		hw_config2.always_detect = 0;
		hw_config2.name = NULL;
		hw_config2.driver_use_1 = 0;
		hw_config2.driver_use_2 = 0;
		hw_config2.card_subtype = 0;

		if (probe_uart401(&hw_config2, THIS_MODULE))
		{
			mpu_detected = 1;
		}
		else
		{
			mpu_base = mpu_irq = 0;
		}
		hw_config->slots[1] = hw_config2.slots[1];
	}
}

280
static void __exit unload_cs4232(struct address_info *hw_config)
Linus Torvalds's avatar
Linus Torvalds committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
{
	int base = hw_config->io_base, irq = hw_config->irq;
	int dma1 = hw_config->dma, dma2 = hw_config->dma2;

	if (dma2 == -1)
		dma2 = dma1;

	ad1848_unload(base,
		      irq,
		      dma1,	/* Playback DMA */
		      dma2,	/* Capture DMA */
		      0);

	sound_unload_audiodev(hw_config->slots[0]);
	if (mpu_base != 0 && mpu_irq != 0 && mpu_detected)
	{
		static struct address_info hw_config2 =
		{
			0
		};		/* Ensure it's initialized */

		hw_config2.io_base = mpu_base;
		hw_config2.irq = mpu_irq;
		hw_config2.dma = -1;
		hw_config2.dma2 = -1;
		hw_config2.always_detect = 0;
		hw_config2.name = NULL;
		hw_config2.driver_use_1 = 0;
		hw_config2.driver_use_2 = 0;
		hw_config2.card_subtype = 0;
		hw_config2.slots[1] = hw_config->slots[1];

		unload_uart401(&hw_config2);
	}
}

static struct address_info cfg;
static struct address_info cfg_mpu;

static int __initdata io	= -1;
static int __initdata irq	= -1;
static int __initdata dma	= -1;
static int __initdata dma2	= -1;
static int __initdata mpuio	= -1;
static int __initdata mpuirq	= -1;
static int __initdata synthio	= -1;
static int __initdata synthirq	= -1;
Linus Torvalds's avatar
Linus Torvalds committed
328
static int __initdata isapnp	= 1;
Linus Torvalds's avatar
Linus Torvalds committed
329

Linus Torvalds's avatar
Linus Torvalds committed
330 331
MODULE_DESCRIPTION("CS4232 based soundcard driver"); 
MODULE_AUTHOR("Hannu Savolainen, Paul Barton-Davis"); 
Linus Torvalds's avatar
Linus Torvalds committed
332 333
MODULE_LICENSE("GPL");

Linus Torvalds's avatar
Linus Torvalds committed
334
MODULE_PARM(io,"i");
Linus Torvalds's avatar
Linus Torvalds committed
335
MODULE_PARM_DESC(io,"base I/O port for AD1848");
Linus Torvalds's avatar
Linus Torvalds committed
336
MODULE_PARM(irq,"i");
Linus Torvalds's avatar
Linus Torvalds committed
337
MODULE_PARM_DESC(irq,"IRQ for AD1848 chip");
Linus Torvalds's avatar
Linus Torvalds committed
338
MODULE_PARM(dma,"i");
Linus Torvalds's avatar
Linus Torvalds committed
339
MODULE_PARM_DESC(dma,"8 bit DMA for AD1848 chip");
Linus Torvalds's avatar
Linus Torvalds committed
340
MODULE_PARM(dma2,"i");
Linus Torvalds's avatar
Linus Torvalds committed
341
MODULE_PARM_DESC(dma2,"16 bit DMA for AD1848 chip");
Linus Torvalds's avatar
Linus Torvalds committed
342
MODULE_PARM(mpuio,"i");
Linus Torvalds's avatar
Linus Torvalds committed
343
MODULE_PARM_DESC(mpuio,"MPU 401 base address");
Linus Torvalds's avatar
Linus Torvalds committed
344
MODULE_PARM(mpuirq,"i");
Linus Torvalds's avatar
Linus Torvalds committed
345
MODULE_PARM_DESC(mpuirq,"MPU 401 IRQ");
Linus Torvalds's avatar
Linus Torvalds committed
346
MODULE_PARM(synthio,"i");
Linus Torvalds's avatar
Linus Torvalds committed
347
MODULE_PARM_DESC(synthio,"Maui WaveTable base I/O port");
Linus Torvalds's avatar
Linus Torvalds committed
348
MODULE_PARM(synthirq,"i");
Linus Torvalds's avatar
Linus Torvalds committed
349 350 351
MODULE_PARM_DESC(synthirq,"Maui WaveTable IRQ");
MODULE_PARM(isapnp,"i");
MODULE_PARM_DESC(isapnp,"Enable ISAPnP probing (default 1)");
Linus Torvalds's avatar
Linus Torvalds committed
352 353 354 355 356 357

/*
 *	Install a CS4232 based card. Need to have ad1848 and mpu401
 *	loaded ready.
 */

Linus Torvalds's avatar
Linus Torvalds committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 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
/* All cs4232 based cards have the main ad1848 card either as CSC0000 or
 * CSC0100. */
struct isapnp_device_id isapnp_cs4232_list[] __initdata = {
	{       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
		ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0100),
		0 },
	{       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
		ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0000),
		0 },
	/* Guillemot Turtlebeach something appears to be cs4232 compatible
	 * (untested) */
	{       ISAPNP_VENDOR('C','S','C'), ISAPNP_ANY_ID,
		ISAPNP_VENDOR('G','I','M'), ISAPNP_FUNCTION(0x0100),
		0 },
	{0}
};

MODULE_DEVICE_TABLE(isapnp, isapnp_cs4232_list);

int cs4232_isapnp_probe(struct pci_dev *dev, const struct isapnp_device_id *id)
{
	int ret;
	struct address_info *isapnpcfg;

	isapnpcfg=(struct address_info*)kmalloc(sizeof(*isapnpcfg),GFP_KERNEL);
	if (!isapnpcfg) 
		return -ENOMEM;
	/*
	 * If device is active, assume configured with /proc/isapnp
	 * and use anyway. Any other way to check this?
	 */
	ret = dev->prepare(dev);
	if(ret && ret != -EBUSY) {
		printk(KERN_ERR "cs4232: ISA PnP found device that could not be autoconfigured.\n");
		kfree(isapnpcfg);
		return -ENODEV;
	}
	if(ret != -EBUSY) {
		if(dev->activate(dev) < 0) {
			printk(KERN_WARNING "cs4232: ISA PnP activate failed\n");
			kfree(isapnpcfg);
			return -ENODEV;
		}
	} /* else subfunction is already activated */

	isapnpcfg->irq		= dev->irq_resource[0].start;
	isapnpcfg->dma		= dev->dma_resource[0].start;
	isapnpcfg->dma2		= dev->dma_resource[1].start;
	isapnpcfg->io_base	= dev->resource[0].start;
	if (probe_cs4232(isapnpcfg,TRUE) == 0) {
		printk(KERN_ERR "cs4232: ISA PnP card found, but not detected?\n");
		return -ENODEV;
	}
	attach_cs4232(isapnpcfg);
	pci_set_drvdata(dev,isapnpcfg);
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
416 417 418 419
static int __init init_cs4232(void)
{
#ifdef CONFIG_SOUND_WAVEFRONT_MODULE
	if(synthio == -1)
Linus Torvalds's avatar
Linus Torvalds committed
420
		printk(KERN_INFO "cs4232: set synthio and synthirq to use the wavefront facilities.\n");
Linus Torvalds's avatar
Linus Torvalds committed
421 422 423 424 425 426 427 428
	else {
		synth_base = synthio;
		synth_irq =  synthirq;	
	}
#else
	if(synthio != -1)
		printk(KERN_WARNING "cs4232: wavefront support not enabled in this driver.\n");
#endif
Linus Torvalds's avatar
Linus Torvalds committed
429 430 431 432 433 434 435
	cfg.irq = -1;

	if (isapnp &&
	    (isapnp_probe_devs(isapnp_cs4232_list, cs4232_isapnp_probe) > 0)
	)
		return 0;

Linus Torvalds's avatar
Linus Torvalds committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
	if(io==-1||irq==-1||dma==-1)
	{
		printk(KERN_ERR "cs4232: Must set io, irq and dma.\n");
		return -ENODEV;
	}

	cfg.io_base = io;
	cfg.irq = irq;
	cfg.dma = dma;
	cfg.dma2 = dma2;

	cfg_mpu.io_base = -1;
	cfg_mpu.irq = -1;

	if (mpuio != -1 && mpuirq != -1) {
		cfg_mpu.io_base = mpuio;
		cfg_mpu.irq = mpuirq;
		probe_cs4232_mpu(&cfg_mpu); /* Bug always returns 0 not OK -- AC */
	}

Linus Torvalds's avatar
Linus Torvalds committed
456
	if (probe_cs4232(&cfg,FALSE) == 0)
Linus Torvalds's avatar
Linus Torvalds committed
457 458 459 460 461 462
		return -ENODEV;
	attach_cs4232(&cfg);
	
	return 0;
}

463 464
static int __exit cs4232_isapnp_remove(struct pci_dev *dev,
			const struct isapnp_device_id *id)
Linus Torvalds's avatar
Linus Torvalds committed
465 466
{
	struct address_info *cfg = (struct address_info*)pci_get_drvdata(dev);
467 468
	if (cfg)
		unload_cs4232(cfg);
Linus Torvalds's avatar
Linus Torvalds committed
469 470 471 472 473
	pci_set_drvdata(dev,NULL);
	dev->deactivate(dev);
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
474 475
static void __exit cleanup_cs4232(void)
{
Linus Torvalds's avatar
Linus Torvalds committed
476 477 478
	isapnp_probe_devs(isapnp_cs4232_list, cs4232_isapnp_remove);
        if (cfg.irq != -1)
		unload_cs4232(&cfg); /* Unloads global MPU as well, if needed */
Linus Torvalds's avatar
Linus Torvalds committed
479 480 481 482 483 484 485 486 487 488
}

module_init(init_cs4232);
module_exit(cleanup_cs4232);

#ifndef MODULE
static int __init setup_cs4232(char *str)
{
	/* io, irq, dma, dma2 mpuio, mpuirq*/
	int ints[7];
Linus Torvalds's avatar
Linus Torvalds committed
489 490 491 492

	/* If we have isapnp cards, no need for options */
	if (isapnp_probe_devs(isapnp_cs4232_list, cs4232_isapnp_probe) > 0)
		return 1;
Linus Torvalds's avatar
Linus Torvalds committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
	
	str = get_options(str, ARRAY_SIZE(ints), ints);
	
	io	= ints[1];
	irq	= ints[2];
	dma	= ints[3];
	dma2	= ints[4];
	mpuio	= ints[5];
	mpuirq	= ints[6];

	return 1;
}

__setup("cs4232=", setup_cs4232);
#endif