1 /*
2  * sound/ad1848.c
3  *
4  * The low level driver for the AD1848/CS4248 codec chip which
5  * is used for example in the MS Sound System.
6  *
7  * The CS4231 which is used in the GUS MAX and some other cards is
8  * upwards compatible with AD1848 and this driver is able to drive it.
9  *
10  * CS4231A and AD1845 are upward compatible with CS4231. However
11  * the new features of these chips are different.
12  *
13  * CS4232 is a PnP audio chip which contains a CS4231A (and SB, MPU).
14  * CS4232A is an improved version of CS4232.
15  *
16  *
17  *
18  * Copyright (C) by Hannu Savolainen 1993-1997
19  *
20  * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
21  * Version 2 (June 1991). See the "COPYING" file distributed with this software
22  * for more info.
23  *
24  *
25  * Thomas Sailer	: ioctl code reworked (vmalloc/vfree removed)
26  *			  general sleep/wakeup clean up.
27  * Alan Cox		: reformatted. Fixed SMP bugs. Moved to kernel alloc/free
28  *		          of irqs. Use dev_id.
29  * Christoph Hellwig	: adapted to module_init/module_exit
30  * Aki Laukkanen	: added power management support
31  * Arnaldo C. de Melo	: added missing restore_flags in ad1848_resume
32  * Miguel Freitas       : added ISA PnP support
33  * Alan Cox		: Added CS4236->4239 identification
34  * Daniel T. Cobra	: Alernate config/mixer for later chips
35  * Alan Cox		: Merged chip idents and config code
36  * Zwane Mwaikambo	: Fix ISA PnP scan
37  *
38  * TODO
39  *		APM save restore assist code on IBM thinkpad
40  *
41  * Status:
42  *		Tested. Believed fully functional.
43  */
44 
45 #include <linux/config.h>
46 #include <linux/init.h>
47 #include <linux/module.h>
48 #include <linux/stddef.h>
49 #include <linux/pm.h>
50 #include <linux/isapnp.h>
51 
52 #define DEB(x)
53 #define DEB1(x)
54 #include "sound_config.h"
55 
56 #include "ad1848.h"
57 #include "ad1848_mixer.h"
58 
59 typedef struct
60 {
61 	int             base;
62 	int             irq;
63 	int             dma1, dma2;
64 	int             dual_dma;	/* 1, when two DMA channels allocated */
65 	int 		subtype;
66 	unsigned char   MCE_bit;
67 	unsigned char   saved_regs[64];	/* Includes extended register space */
68 	int             debug_flag;
69 
70 	int             audio_flags;
71 	int             record_dev, playback_dev;
72 
73 	int             xfer_count;
74 	int             audio_mode;
75 	int             open_mode;
76 	int             intr_active;
77 	char           *chip_name, *name;
78 	int             model;
79 #define MD_1848		1
80 #define MD_4231		2
81 #define MD_4231A	3
82 #define MD_1845		4
83 #define MD_4232		5
84 #define MD_C930		6
85 #define MD_IWAVE	7
86 #define MD_4235         8 /* Crystal Audio CS4235  */
87 #define MD_1845_SSCAPE  9 /* Ensoniq Soundscape PNP*/
88 #define MD_4236		10 /* 4236 and higher */
89 #define MD_42xB		11 /* CS 42xB */
90 #define MD_4239		12 /* CS4239 */
91 
92 	/* Mixer parameters */
93 	int             recmask;
94 	int             supported_devices, orig_devices;
95 	int             supported_rec_devices, orig_rec_devices;
96 	int            *levels;
97 	short           mixer_reroute[32];
98 	int             dev_no;
99 	volatile unsigned long timer_ticks;
100 	int             timer_running;
101 	int             irq_ok;
102 	mixer_ents     *mix_devices;
103 	int             mixer_output_port;
104 
105 	/* Power management */
106 	struct		pm_dev *pmdev;
107 } ad1848_info;
108 
109 typedef struct ad1848_port_info
110 {
111 	int             open_mode;
112 	int             speed;
113 	unsigned char   speed_bits;
114 	int             channels;
115 	int             audio_format;
116 	unsigned char   format_bits;
117 }
118 ad1848_port_info;
119 
120 static struct address_info cfg;
121 static int nr_ad1848_devs;
122 
123 int deskpro_xl;
124 int deskpro_m;
125 int soundpro;
126 
127 static volatile signed char irq2dev[17] = {
128 	-1, -1, -1, -1, -1, -1, -1, -1,
129 	-1, -1, -1, -1, -1, -1, -1, -1, -1
130 };
131 
132 #ifndef EXCLUDE_TIMERS
133 static int timer_installed = -1;
134 #endif
135 
136 static int loaded;
137 
138 static int ad_format_mask[13 /*devc->model */ ] =
139 {
140 	0,
141 	AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW,
142 	AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
143 	AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
144 	AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW,	/* AD1845 */
145 	AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
146 	AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
147 	AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
148 	AFMT_U8 | AFMT_S16_LE /* CS4235 */,
149 	AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW	/* Ensoniq Soundscape*/,
150 	AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
151 	AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
152 	AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM
153 };
154 
155 static ad1848_info adev_info[MAX_AUDIO_DEV];
156 
157 #define io_Index_Addr(d)	((d)->base)
158 #define io_Indexed_Data(d)	((d)->base+1)
159 #define io_Status(d)		((d)->base+2)
160 #define io_Polled_IO(d)		((d)->base+3)
161 
162 static struct {
163      unsigned char flags;
164 #define CAP_F_TIMER 0x01
165 } capabilities [10 /*devc->model */ ] = {
166      {0}
167     ,{0}           /* MD_1848  */
168     ,{CAP_F_TIMER} /* MD_4231  */
169     ,{CAP_F_TIMER} /* MD_4231A */
170     ,{CAP_F_TIMER} /* MD_1845  */
171     ,{CAP_F_TIMER} /* MD_4232  */
172     ,{0}           /* MD_C930  */
173     ,{CAP_F_TIMER} /* MD_IWAVE */
174     ,{0}           /* MD_4235  */
175     ,{CAP_F_TIMER} /* MD_1845_SSCAPE */
176 };
177 
178 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
179 static int isapnp	= 1;
180 static int isapnpjump	= 0;
181 static int reverse	= 0;
182 
183 static int audio_activated = 0;
184 #else
185 static int isapnp	= 0;
186 #endif
187 
188 
189 
190 static int      ad1848_open(int dev, int mode);
191 static void     ad1848_close(int dev);
192 static void     ad1848_output_block(int dev, unsigned long buf, int count, int intrflag);
193 static void     ad1848_start_input(int dev, unsigned long buf, int count, int intrflag);
194 static int      ad1848_prepare_for_output(int dev, int bsize, int bcount);
195 static int      ad1848_prepare_for_input(int dev, int bsize, int bcount);
196 static void     ad1848_halt(int dev);
197 static void     ad1848_halt_input(int dev);
198 static void     ad1848_halt_output(int dev);
199 static void     ad1848_trigger(int dev, int bits);
200 static int	ad1848_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data);
201 
202 #ifndef EXCLUDE_TIMERS
203 static int ad1848_tmr_install(int dev);
204 static void ad1848_tmr_reprogram(int dev);
205 #endif
206 
ad_read(ad1848_info * devc,int reg)207 static int ad_read(ad1848_info * devc, int reg)
208 {
209 	unsigned long flags;
210 	int x;
211 	int timeout = 900000;
212 
213 	while (timeout > 0 && inb(devc->base) == 0x80)	/*Are we initializing */
214 		timeout--;
215 
216 	save_flags(flags);
217 	cli();
218 
219 	if(reg < 32)
220 	{
221 		outb(((unsigned char) (reg & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
222 		x = inb(io_Indexed_Data(devc));
223 	}
224 	else
225 	{
226 		int xreg, xra;
227 
228 		xreg = (reg & 0xff) - 32;
229 		xra = (((xreg & 0x0f) << 4) & 0xf0) | 0x08 | ((xreg & 0x10) >> 2);
230 		outb(((unsigned char) (23 & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
231 		outb(((unsigned char) (xra & 0xff)), io_Indexed_Data(devc));
232 		x = inb(io_Indexed_Data(devc));
233 	}
234 	restore_flags(flags);
235 
236 	return x;
237 }
238 
ad_write(ad1848_info * devc,int reg,int data)239 static void ad_write(ad1848_info * devc, int reg, int data)
240 {
241 	unsigned long flags;
242 	int timeout = 900000;
243 
244 	while (timeout > 0 && inb(devc->base) == 0x80)	/* Are we initializing */
245 		timeout--;
246 
247 	save_flags(flags);
248 	cli();
249 
250 	if(reg < 32)
251 	{
252 		outb(((unsigned char) (reg & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
253 		outb(((unsigned char) (data & 0xff)), io_Indexed_Data(devc));
254 	}
255 	else
256 	{
257 		int xreg, xra;
258 
259 		xreg = (reg & 0xff) - 32;
260 		xra = (((xreg & 0x0f) << 4) & 0xf0) | 0x08 | ((xreg & 0x10) >> 2);
261 		outb(((unsigned char) (23 & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
262 		outb(((unsigned char) (xra & 0xff)), io_Indexed_Data(devc));
263 		outb((unsigned char) (data & 0xff), io_Indexed_Data(devc));
264 	}
265 	restore_flags(flags);
266 }
267 
wait_for_calibration(ad1848_info * devc)268 static void wait_for_calibration(ad1848_info * devc)
269 {
270 	int timeout = 0;
271 
272 	/*
273 	 * Wait until the auto calibration process has finished.
274 	 *
275 	 * 1)       Wait until the chip becomes ready (reads don't return 0x80).
276 	 * 2)       Wait until the ACI bit of I11 gets on and then off.
277 	 */
278 
279 	timeout = 100000;
280 	while (timeout > 0 && inb(devc->base) == 0x80)
281 		timeout--;
282 	if (inb(devc->base) & 0x80)
283 		printk(KERN_WARNING "ad1848: Auto calibration timed out(1).\n");
284 
285 	timeout = 100;
286 	while (timeout > 0 && !(ad_read(devc, 11) & 0x20))
287 		timeout--;
288 	if (!(ad_read(devc, 11) & 0x20))
289 		return;
290 
291 	timeout = 80000;
292 	while (timeout > 0 && (ad_read(devc, 11) & 0x20))
293 		timeout--;
294 	if (ad_read(devc, 11) & 0x20)
295 		if ( (devc->model != MD_1845) || (devc->model != MD_1845_SSCAPE))
296 			printk(KERN_WARNING "ad1848: Auto calibration timed out(3).\n");
297 }
298 
ad_mute(ad1848_info * devc)299 static void ad_mute(ad1848_info * devc)
300 {
301 	int i;
302 	unsigned char prev;
303 
304 	/*
305 	 * Save old register settings and mute output channels
306 	 */
307 
308 	for (i = 6; i < 8; i++)
309 	{
310 		prev = devc->saved_regs[i] = ad_read(devc, i);
311 	}
312 
313 }
314 
ad_unmute(ad1848_info * devc)315 static void ad_unmute(ad1848_info * devc)
316 {
317 }
318 
ad_enter_MCE(ad1848_info * devc)319 static void ad_enter_MCE(ad1848_info * devc)
320 {
321 	unsigned long flags;
322 	int timeout = 1000;
323 	unsigned short prev;
324 
325 	while (timeout > 0 && inb(devc->base) == 0x80)	/*Are we initializing */
326 		timeout--;
327 
328 	save_flags(flags);
329 	cli();
330 
331 	devc->MCE_bit = 0x40;
332 	prev = inb(io_Index_Addr(devc));
333 	if (prev & 0x40)
334 	{
335 		restore_flags(flags);
336 		return;
337 	}
338 	outb((devc->MCE_bit), io_Index_Addr(devc));
339 	restore_flags(flags);
340 }
341 
ad_leave_MCE(ad1848_info * devc)342 static void ad_leave_MCE(ad1848_info * devc)
343 {
344 	unsigned long flags;
345 	unsigned char prev, acal;
346 	int timeout = 1000;
347 
348 	while (timeout > 0 && inb(devc->base) == 0x80)	/*Are we initializing */
349 		timeout--;
350 
351 	save_flags(flags);
352 	cli();
353 
354 	acal = ad_read(devc, 9);
355 
356 	devc->MCE_bit = 0x00;
357 	prev = inb(io_Index_Addr(devc));
358 	outb((0x00), io_Index_Addr(devc));	/* Clear the MCE bit */
359 
360 	if ((prev & 0x40) == 0)	/* Not in MCE mode */
361 	{
362 		restore_flags(flags);
363 		return;
364 	}
365 	outb((0x00), io_Index_Addr(devc));	/* Clear the MCE bit */
366 	if (acal & 0x08)	/* Auto calibration is enabled */
367 		wait_for_calibration(devc);
368 	restore_flags(flags);
369 }
370 
ad1848_set_recmask(ad1848_info * devc,int mask)371 static int ad1848_set_recmask(ad1848_info * devc, int mask)
372 {
373 	unsigned char   recdev;
374 	int             i, n;
375 
376 	mask &= devc->supported_rec_devices;
377 
378 	/* Rename the mixer bits if necessary */
379 	for (i = 0; i < 32; i++)
380 	{
381 		if (devc->mixer_reroute[i] != i)
382 		{
383 			if (mask & (1 << i))
384 			{
385 				mask &= ~(1 << i);
386 				mask |= (1 << devc->mixer_reroute[i]);
387 			}
388 		}
389 	}
390 
391 	n = 0;
392 	for (i = 0; i < 32; i++)	/* Count selected device bits */
393 		if (mask & (1 << i))
394 			n++;
395 
396 	if (!soundpro) {
397 		if (n == 0)
398 			mask = SOUND_MASK_MIC;
399 		else if (n != 1) {	/* Too many devices selected */
400 			mask &= ~devc->recmask;	/* Filter out active settings */
401 
402 			n = 0;
403 			for (i = 0; i < 32; i++)	/* Count selected device bits */
404 				if (mask & (1 << i))
405 					n++;
406 
407 			if (n != 1)
408 				mask = SOUND_MASK_MIC;
409 		}
410 		switch (mask) {
411 		case SOUND_MASK_MIC:
412 			recdev = 2;
413 			break;
414 
415 		case SOUND_MASK_LINE:
416 		case SOUND_MASK_LINE3:
417 			recdev = 0;
418 			break;
419 
420 		case SOUND_MASK_CD:
421 		case SOUND_MASK_LINE1:
422 			recdev = 1;
423 			break;
424 
425 		case SOUND_MASK_IMIX:
426 			recdev = 3;
427 			break;
428 
429 		default:
430 			mask = SOUND_MASK_MIC;
431 			recdev = 2;
432 		}
433 
434 		recdev <<= 6;
435 		ad_write(devc, 0, (ad_read(devc, 0) & 0x3f) | recdev);
436 		ad_write(devc, 1, (ad_read(devc, 1) & 0x3f) | recdev);
437 	} else { /* soundpro */
438 		unsigned char val;
439 		int set_rec_bit;
440 		int j;
441 
442 		for (i = 0; i < 32; i++) {	/* For each bit */
443 			if ((devc->supported_rec_devices & (1 << i)) == 0)
444 				continue;	/* Device not supported */
445 
446 			for (j = LEFT_CHN; j <= RIGHT_CHN; j++) {
447 				if (devc->mix_devices[i][j].nbits == 0) /* Inexistent channel */
448 					continue;
449 
450 				/*
451 				 * This is tricky:
452 				 * set_rec_bit becomes 1 if the corresponding bit in mask is set
453 				 * then it gets flipped if the polarity is inverse
454 				 */
455 				set_rec_bit = ((mask & (1 << i)) != 0) ^ devc->mix_devices[i][j].recpol;
456 
457 				val = ad_read(devc, devc->mix_devices[i][j].recreg);
458 				val &= ~(1 << devc->mix_devices[i][j].recpos);
459 				val |= (set_rec_bit << devc->mix_devices[i][j].recpos);
460 				ad_write(devc, devc->mix_devices[i][j].recreg, val);
461 			}
462 		}
463 	}
464 
465 	/* Rename the mixer bits back if necessary */
466 	for (i = 0; i < 32; i++)
467 	{
468 		if (devc->mixer_reroute[i] != i)
469 		{
470 			if (mask & (1 << devc->mixer_reroute[i]))
471 			{
472 				mask &= ~(1 << devc->mixer_reroute[i]);
473 				mask |= (1 << i);
474 			}
475 		}
476 	}
477 	devc->recmask = mask;
478 	return mask;
479 }
480 
change_bits(ad1848_info * devc,unsigned char * regval,unsigned char * muteval,int dev,int chn,int newval)481 static void change_bits(ad1848_info * devc, unsigned char *regval,
482 			unsigned char *muteval, int dev, int chn, int newval)
483 {
484 	unsigned char mask;
485 	int shift;
486 	int mute;
487 	int mutemask;
488 	int set_mute_bit;
489 
490 	set_mute_bit = (newval == 0) ^ devc->mix_devices[dev][chn].mutepol;
491 
492 	if (devc->mix_devices[dev][chn].polarity == 1)	/* Reverse */
493 		newval = 100 - newval;
494 
495 	mask = (1 << devc->mix_devices[dev][chn].nbits) - 1;
496 	shift = devc->mix_devices[dev][chn].bitpos;
497 
498 	if (devc->mix_devices[dev][chn].mutepos == 8)
499 	{			/* if there is no mute bit */
500 		mute = 0;	/* No mute bit; do nothing special */
501 		mutemask = ~0;	/* No mute bit; do nothing special */
502 	}
503 	else
504 	{
505 		mute = (set_mute_bit << devc->mix_devices[dev][chn].mutepos);
506 		mutemask = ~(1 << devc->mix_devices[dev][chn].mutepos);
507 	}
508 
509 	newval = (int) ((newval * mask) + 50) / 100;	/* Scale it */
510 	*regval &= ~(mask << shift);			/* Clear bits */
511 	*regval |= (newval & mask) << shift;		/* Set new value */
512 
513 	*muteval &= mutemask;
514 	*muteval |= mute;
515 }
516 
ad1848_mixer_get(ad1848_info * devc,int dev)517 static int ad1848_mixer_get(ad1848_info * devc, int dev)
518 {
519 	if (!((1 << dev) & devc->supported_devices))
520 		return -EINVAL;
521 
522 	dev = devc->mixer_reroute[dev];
523 
524 	return devc->levels[dev];
525 }
526 
ad1848_mixer_set_channel(ad1848_info * devc,int dev,int value,int channel)527 static void ad1848_mixer_set_channel(ad1848_info *devc, int dev, int value, int channel)
528 {
529 	int regoffs, muteregoffs;
530 	unsigned char val, muteval;
531 
532 	regoffs = devc->mix_devices[dev][channel].regno;
533 	muteregoffs = devc->mix_devices[dev][channel].mutereg;
534 	val = ad_read(devc, regoffs);
535 
536 	if (muteregoffs != regoffs) {
537 		muteval = ad_read(devc, muteregoffs);
538 		change_bits(devc, &val, &muteval, dev, channel, value);
539 	}
540 	else
541 		change_bits(devc, &val, &val, dev, channel, value);
542 
543 	ad_write(devc, regoffs, val);
544 	devc->saved_regs[regoffs] = val;
545 	if (muteregoffs != regoffs) {
546 		ad_write(devc, muteregoffs, muteval);
547 		devc->saved_regs[muteregoffs] = muteval;
548 	}
549 }
550 
ad1848_mixer_set(ad1848_info * devc,int dev,int value)551 static int ad1848_mixer_set(ad1848_info * devc, int dev, int value)
552 {
553 	int left = value & 0x000000ff;
554 	int right = (value & 0x0000ff00) >> 8;
555 	int retvol;
556 
557 	if (dev > 31)
558 		return -EINVAL;
559 
560 	if (!(devc->supported_devices & (1 << dev)))
561 		return -EINVAL;
562 
563 	dev = devc->mixer_reroute[dev];
564 
565 	if (devc->mix_devices[dev][LEFT_CHN].nbits == 0)
566 		return -EINVAL;
567 
568 	if (left > 100)
569 		left = 100;
570 	if (right > 100)
571 		right = 100;
572 
573 	if (devc->mix_devices[dev][RIGHT_CHN].nbits == 0)	/* Mono control */
574 		right = left;
575 
576 	retvol = left | (right << 8);
577 
578 	/* Scale volumes */
579 	left = mix_cvt[left];
580 	right = mix_cvt[right];
581 
582 	devc->levels[dev] = retvol;
583 
584 	/*
585 	 * Set the left channel
586 	 */
587 	ad1848_mixer_set_channel(devc, dev, left, LEFT_CHN);
588 
589 	/*
590 	 * Set the right channel
591 	 */
592 	if (devc->mix_devices[dev][RIGHT_CHN].nbits == 0)
593 		goto out;
594 	ad1848_mixer_set_channel(devc, dev, right, RIGHT_CHN);
595 
596  out:
597 	return retvol;
598 }
599 
ad1848_mixer_reset(ad1848_info * devc)600 static void ad1848_mixer_reset(ad1848_info * devc)
601 {
602 	int i;
603 	char name[32];
604 
605 	devc->mix_devices = &(ad1848_mix_devices[0]);
606 
607 	sprintf(name, "%s_%d", devc->chip_name, nr_ad1848_devs);
608 
609 	for (i = 0; i < 32; i++)
610 		devc->mixer_reroute[i] = i;
611 
612 	devc->supported_rec_devices = MODE1_REC_DEVICES;
613 
614 	switch (devc->model)
615 	{
616 		case MD_4231:
617 		case MD_4231A:
618 		case MD_1845:
619 		case MD_1845_SSCAPE:
620 			devc->supported_devices = MODE2_MIXER_DEVICES;
621 			break;
622 
623 		case MD_C930:
624 			devc->supported_devices = C930_MIXER_DEVICES;
625 			devc->mix_devices = &(c930_mix_devices[0]);
626 			break;
627 
628 		case MD_IWAVE:
629 			devc->supported_devices = MODE3_MIXER_DEVICES;
630 			devc->mix_devices = &(iwave_mix_devices[0]);
631 			break;
632 
633 		case MD_42xB:
634 		case MD_4239:
635 			devc->mix_devices = &(cs42xb_mix_devices[0]);
636 			devc->supported_devices = MODE3_MIXER_DEVICES;
637 			break;
638 		case MD_4232:
639 		case MD_4235:
640 		case MD_4236:
641 			devc->supported_devices = MODE3_MIXER_DEVICES;
642 			break;
643 
644 		case MD_1848:
645 			if (soundpro) {
646 				devc->supported_devices = SPRO_MIXER_DEVICES;
647 				devc->supported_rec_devices = SPRO_REC_DEVICES;
648 				devc->mix_devices = &(spro_mix_devices[0]);
649 				break;
650 			}
651 
652 		default:
653 			devc->supported_devices = MODE1_MIXER_DEVICES;
654 	}
655 
656 	devc->orig_devices = devc->supported_devices;
657 	devc->orig_rec_devices = devc->supported_rec_devices;
658 
659 	devc->levels = load_mixer_volumes(name, default_mixer_levels, 1);
660 
661 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
662 	{
663 		if (devc->supported_devices & (1 << i))
664 			ad1848_mixer_set(devc, i, devc->levels[i]);
665 	}
666 
667 	ad1848_set_recmask(devc, SOUND_MASK_MIC);
668 
669 	devc->mixer_output_port = devc->levels[31] | AUDIO_HEADPHONE | AUDIO_LINE_OUT;
670 
671 	if (!soundpro) {
672 		if (devc->mixer_output_port & AUDIO_SPEAKER)
673 			ad_write(devc, 26, ad_read(devc, 26) & ~0x40);	/* Unmute mono out */
674 		else
675 			ad_write(devc, 26, ad_read(devc, 26) | 0x40);	/* Mute mono out */
676 	} else {
677 		/*
678 		 * From the "wouldn't it be nice if the mixer API had (better)
679 		 * support for custom stuff" category
680 		 */
681 		/* Enable surround mode and SB16 mixer */
682 		ad_write(devc, 16, 0x60);
683 	}
684 }
685 
ad1848_mixer_ioctl(int dev,unsigned int cmd,caddr_t arg)686 static int ad1848_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
687 {
688 	ad1848_info *devc = mixer_devs[dev]->devc;
689 	int val;
690 
691 	if (cmd == SOUND_MIXER_PRIVATE1)
692 	{
693 		if (get_user(val, (int *)arg))
694 			return -EFAULT;
695 
696 		if (val != 0xffff)
697 		{
698 			val &= (AUDIO_SPEAKER | AUDIO_HEADPHONE | AUDIO_LINE_OUT);
699 			devc->mixer_output_port = val;
700 			val |= AUDIO_HEADPHONE | AUDIO_LINE_OUT;	/* Always on */
701 			devc->mixer_output_port = val;
702 			if (val & AUDIO_SPEAKER)
703 				ad_write(devc, 26, ad_read(devc, 26) & ~0x40);	/* Unmute mono out */
704 			else
705 				ad_write(devc, 26, ad_read(devc, 26) | 0x40);		/* Mute mono out */
706 		}
707 		val = devc->mixer_output_port;
708 		return put_user(val, (int *)arg);
709 	}
710 	if (cmd == SOUND_MIXER_PRIVATE2)
711 	{
712 		if (get_user(val, (int *)arg))
713 			return -EFAULT;
714 		return(ad1848_control(AD1848_MIXER_REROUTE, val));
715 	}
716 	if (((cmd >> 8) & 0xff) == 'M')
717 	{
718 		if (_SIOC_DIR(cmd) & _SIOC_WRITE)
719 		{
720 			switch (cmd & 0xff)
721 			{
722 				case SOUND_MIXER_RECSRC:
723 					if (get_user(val, (int *)arg))
724 						return -EFAULT;
725 					val = ad1848_set_recmask(devc, val);
726 					break;
727 
728 				default:
729 					if (get_user(val, (int *)arg))
730 					return -EFAULT;
731 					val = ad1848_mixer_set(devc, cmd & 0xff, val);
732 					break;
733 			}
734 			return put_user(val, (int *)arg);
735 		}
736 		else
737 		{
738 			switch (cmd & 0xff)
739 			{
740 				/*
741 				 * Return parameters
742 				 */
743 
744 				case SOUND_MIXER_RECSRC:
745 					val = devc->recmask;
746 					break;
747 
748 				case SOUND_MIXER_DEVMASK:
749 					val = devc->supported_devices;
750 					break;
751 
752 				case SOUND_MIXER_STEREODEVS:
753 					val = devc->supported_devices;
754 					if (devc->model != MD_C930)
755 						val &= ~(SOUND_MASK_SPEAKER | SOUND_MASK_IMIX);
756 					break;
757 
758 				case SOUND_MIXER_RECMASK:
759 					val = devc->supported_rec_devices;
760 					break;
761 
762 				case SOUND_MIXER_CAPS:
763 					val=SOUND_CAP_EXCL_INPUT;
764 					break;
765 
766 				default:
767 					val = ad1848_mixer_get(devc, cmd & 0xff);
768 					break;
769 			}
770 			return put_user(val, (int *)arg);
771 		}
772 	}
773 	else
774 		return -EINVAL;
775 }
776 
ad1848_set_speed(int dev,int arg)777 static int ad1848_set_speed(int dev, int arg)
778 {
779 	ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
780 	ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
781 
782 	/*
783 	 * The sampling speed is encoded in the least significant nibble of I8. The
784 	 * LSB selects the clock source (0=24.576 MHz, 1=16.9344 MHz) and other
785 	 * three bits select the divisor (indirectly):
786 	 *
787 	 * The available speeds are in the following table. Keep the speeds in
788 	 * the increasing order.
789 	 */
790 	typedef struct
791 	{
792 		int             speed;
793 		unsigned char   bits;
794 	}
795 	speed_struct;
796 
797 	static speed_struct speed_table[] =
798 	{
799 		{5510, (0 << 1) | 1},
800 		{5510, (0 << 1) | 1},
801 		{6620, (7 << 1) | 1},
802 		{8000, (0 << 1) | 0},
803 		{9600, (7 << 1) | 0},
804 		{11025, (1 << 1) | 1},
805 		{16000, (1 << 1) | 0},
806 		{18900, (2 << 1) | 1},
807 		{22050, (3 << 1) | 1},
808 		{27420, (2 << 1) | 0},
809 		{32000, (3 << 1) | 0},
810 		{33075, (6 << 1) | 1},
811 		{37800, (4 << 1) | 1},
812 		{44100, (5 << 1) | 1},
813 		{48000, (6 << 1) | 0}
814 	};
815 
816 	int i, n, selected = -1;
817 
818 	n = sizeof(speed_table) / sizeof(speed_struct);
819 
820 	if (arg <= 0)
821 		return portc->speed;
822 
823 	if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE)	/* AD1845 has different timer than others */
824 	{
825 		if (arg < 4000)
826 			arg = 4000;
827 		if (arg > 50000)
828 			arg = 50000;
829 
830 		portc->speed = arg;
831 		portc->speed_bits = speed_table[3].bits;
832 		return portc->speed;
833 	}
834 	if (arg < speed_table[0].speed)
835 		selected = 0;
836 	if (arg > speed_table[n - 1].speed)
837 		selected = n - 1;
838 
839 	for (i = 1 /*really */ ; selected == -1 && i < n; i++)
840 	{
841 		if (speed_table[i].speed == arg)
842 			selected = i;
843 		else if (speed_table[i].speed > arg)
844 		{
845 			int diff1, diff2;
846 
847 			diff1 = arg - speed_table[i - 1].speed;
848 			diff2 = speed_table[i].speed - arg;
849 
850 			if (diff1 < diff2)
851 				selected = i - 1;
852 			else
853 				selected = i;
854 		}
855 	}
856 	if (selected == -1)
857 	{
858 		printk(KERN_WARNING "ad1848: Can't find speed???\n");
859 		selected = 3;
860 	}
861 	portc->speed = speed_table[selected].speed;
862 	portc->speed_bits = speed_table[selected].bits;
863 	return portc->speed;
864 }
865 
ad1848_set_channels(int dev,short arg)866 static short ad1848_set_channels(int dev, short arg)
867 {
868 	ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
869 
870 	if (arg != 1 && arg != 2)
871 		return portc->channels;
872 
873 	portc->channels = arg;
874 	return arg;
875 }
876 
ad1848_set_bits(int dev,unsigned int arg)877 static unsigned int ad1848_set_bits(int dev, unsigned int arg)
878 {
879 	ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
880 	ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
881 
882 	static struct format_tbl
883 	{
884 		  int             format;
885 		  unsigned char   bits;
886 	}
887 	format2bits[] =
888 	{
889 		{
890 			0, 0
891 		}
892 		,
893 		{
894 			AFMT_MU_LAW, 1
895 		}
896 		,
897 		{
898 			AFMT_A_LAW, 3
899 		}
900 		,
901 		{
902 			AFMT_IMA_ADPCM, 5
903 		}
904 		,
905 		{
906 			AFMT_U8, 0
907 		}
908 		,
909 		{
910 			AFMT_S16_LE, 2
911 		}
912 		,
913 		{
914 			AFMT_S16_BE, 6
915 		}
916 		,
917 		{
918 			AFMT_S8, 0
919 		}
920 		,
921 		{
922 			AFMT_U16_LE, 0
923 		}
924 		,
925 		{
926 			AFMT_U16_BE, 0
927 		}
928 	};
929 	int i, n = sizeof(format2bits) / sizeof(struct format_tbl);
930 
931 	if (arg == 0)
932 		return portc->audio_format;
933 
934 	if (!(arg & ad_format_mask[devc->model]))
935 		arg = AFMT_U8;
936 
937 	portc->audio_format = arg;
938 
939 	for (i = 0; i < n; i++)
940 		if (format2bits[i].format == arg)
941 		{
942 			if ((portc->format_bits = format2bits[i].bits) == 0)
943 				return portc->audio_format = AFMT_U8;		/* Was not supported */
944 
945 			return arg;
946 		}
947 	/* Still hanging here. Something must be terribly wrong */
948 	portc->format_bits = 0;
949 	return portc->audio_format = AFMT_U8;
950 }
951 
952 static struct audio_driver ad1848_audio_driver =
953 {
954 	owner:		THIS_MODULE,
955 	open:		ad1848_open,
956 	close:		ad1848_close,
957 	output_block:	ad1848_output_block,
958 	start_input:	ad1848_start_input,
959 	prepare_for_input:	ad1848_prepare_for_input,
960 	prepare_for_output:	ad1848_prepare_for_output,
961 	halt_io:	ad1848_halt,
962 	halt_input:	ad1848_halt_input,
963 	halt_output:	ad1848_halt_output,
964 	trigger:	ad1848_trigger,
965 	set_speed:	ad1848_set_speed,
966 	set_bits:	ad1848_set_bits,
967 	set_channels:	ad1848_set_channels
968 };
969 
970 static struct mixer_operations ad1848_mixer_operations =
971 {
972 	owner:	THIS_MODULE,
973 	id:	"SOUNDPORT",
974 	name:	"AD1848/CS4248/CS4231",
975 	ioctl:	ad1848_mixer_ioctl
976 };
977 
ad1848_open(int dev,int mode)978 static int ad1848_open(int dev, int mode)
979 {
980 	ad1848_info    *devc = NULL;
981 	ad1848_port_info *portc;
982 	unsigned long   flags;
983 
984 	if (dev < 0 || dev >= num_audiodevs)
985 		return -ENXIO;
986 
987 	devc = (ad1848_info *) audio_devs[dev]->devc;
988 	portc = (ad1848_port_info *) audio_devs[dev]->portc;
989 
990 	save_flags(flags);
991 	cli();
992 	if (portc->open_mode || (devc->open_mode & mode))
993 	{
994 		restore_flags(flags);
995 		return -EBUSY;
996 	}
997 	devc->dual_dma = 0;
998 
999 	if (audio_devs[dev]->flags & DMA_DUPLEX)
1000 	{
1001 		devc->dual_dma = 1;
1002 	}
1003 	devc->intr_active = 0;
1004 	devc->audio_mode = 0;
1005 	devc->open_mode |= mode;
1006 	portc->open_mode = mode;
1007 	ad1848_trigger(dev, 0);
1008 
1009 	if (mode & OPEN_READ)
1010 		devc->record_dev = dev;
1011 	if (mode & OPEN_WRITE)
1012 		devc->playback_dev = dev;
1013 	restore_flags(flags);
1014 /*
1015  * Mute output until the playback really starts. This decreases clicking (hope so).
1016  */
1017 	ad_mute(devc);
1018 
1019 	return 0;
1020 }
1021 
ad1848_close(int dev)1022 static void ad1848_close(int dev)
1023 {
1024 	unsigned long   flags;
1025 	ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1026 	ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1027 
1028 	DEB(printk("ad1848_close(void)\n"));
1029 
1030 	save_flags(flags);
1031 	cli();
1032 
1033 	devc->intr_active = 0;
1034 	ad1848_halt(dev);
1035 
1036 	devc->audio_mode = 0;
1037 	devc->open_mode &= ~portc->open_mode;
1038 	portc->open_mode = 0;
1039 
1040 	ad_unmute(devc);
1041 	restore_flags(flags);
1042 }
1043 
ad1848_output_block(int dev,unsigned long buf,int count,int intrflag)1044 static void ad1848_output_block(int dev, unsigned long buf, int count, int intrflag)
1045 {
1046 	unsigned long   flags, cnt;
1047 	ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1048 	ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1049 
1050 	cnt = count;
1051 
1052 	if (portc->audio_format == AFMT_IMA_ADPCM)
1053 	{
1054 		cnt /= 4;
1055 	}
1056 	else
1057 	{
1058 		if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))	/* 16 bit data */
1059 			cnt >>= 1;
1060 	}
1061 	if (portc->channels > 1)
1062 		cnt >>= 1;
1063 	cnt--;
1064 
1065 	if ((devc->audio_mode & PCM_ENABLE_OUTPUT) && (audio_devs[dev]->flags & DMA_AUTOMODE) &&
1066 	    intrflag &&
1067 	    cnt == devc->xfer_count)
1068 	{
1069 		devc->audio_mode |= PCM_ENABLE_OUTPUT;
1070 		devc->intr_active = 1;
1071 		return;	/*
1072 			 * Auto DMA mode on. No need to react
1073 			 */
1074 	}
1075 	save_flags(flags);
1076 	cli();
1077 
1078 	ad_write(devc, 15, (unsigned char) (cnt & 0xff));
1079 	ad_write(devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
1080 
1081 	devc->xfer_count = cnt;
1082 	devc->audio_mode |= PCM_ENABLE_OUTPUT;
1083 	devc->intr_active = 1;
1084 	restore_flags(flags);
1085 }
1086 
ad1848_start_input(int dev,unsigned long buf,int count,int intrflag)1087 static void ad1848_start_input(int dev, unsigned long buf, int count, int intrflag)
1088 {
1089 	unsigned long   flags, cnt;
1090 	ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1091 	ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1092 
1093 	cnt = count;
1094 	if (portc->audio_format == AFMT_IMA_ADPCM)
1095 	{
1096 		cnt /= 4;
1097 	}
1098 	else
1099 	{
1100 		if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))	/* 16 bit data */
1101 			cnt >>= 1;
1102 	}
1103 	if (portc->channels > 1)
1104 		cnt >>= 1;
1105 	cnt--;
1106 
1107 	if ((devc->audio_mode & PCM_ENABLE_INPUT) && (audio_devs[dev]->flags & DMA_AUTOMODE) &&
1108 		intrflag &&
1109 		cnt == devc->xfer_count)
1110 	{
1111 		devc->audio_mode |= PCM_ENABLE_INPUT;
1112 		devc->intr_active = 1;
1113 		return;	/*
1114 			 * Auto DMA mode on. No need to react
1115 			 */
1116 	}
1117 	save_flags(flags);
1118 	cli();
1119 
1120 	if (devc->model == MD_1848)
1121 	{
1122 		  ad_write(devc, 15, (unsigned char) (cnt & 0xff));
1123 		  ad_write(devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
1124 	}
1125 	else
1126 	{
1127 		  ad_write(devc, 31, (unsigned char) (cnt & 0xff));
1128 		  ad_write(devc, 30, (unsigned char) ((cnt >> 8) & 0xff));
1129 	}
1130 
1131 	ad_unmute(devc);
1132 
1133 	devc->xfer_count = cnt;
1134 	devc->audio_mode |= PCM_ENABLE_INPUT;
1135 	devc->intr_active = 1;
1136 	restore_flags(flags);
1137 }
1138 
ad1848_prepare_for_output(int dev,int bsize,int bcount)1139 static int ad1848_prepare_for_output(int dev, int bsize, int bcount)
1140 {
1141 	int             timeout;
1142 	unsigned char   fs, old_fs, tmp = 0;
1143 	unsigned long   flags;
1144 	ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1145 	ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1146 
1147 	ad_mute(devc);
1148 
1149 	save_flags(flags);
1150 	cli();
1151 	fs = portc->speed_bits | (portc->format_bits << 5);
1152 
1153 	if (portc->channels > 1)
1154 		fs |= 0x10;
1155 
1156 	ad_enter_MCE(devc);	/* Enables changes to the format select reg */
1157 
1158 	if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE) /* Use alternate speed select registers */
1159 	{
1160 		fs &= 0xf0;	/* Mask off the rate select bits */
1161 
1162 		ad_write(devc, 22, (portc->speed >> 8) & 0xff);	/* Speed MSB */
1163 		ad_write(devc, 23, portc->speed & 0xff);	/* Speed LSB */
1164 	}
1165 	old_fs = ad_read(devc, 8);
1166 
1167 	if (devc->model == MD_4232 || devc->model >= MD_4236)
1168 	{
1169 		tmp = ad_read(devc, 16);
1170 		ad_write(devc, 16, tmp | 0x30);
1171 	}
1172 	if (devc->model == MD_IWAVE)
1173 		ad_write(devc, 17, 0xc2);	/* Disable variable frequency select */
1174 
1175 	ad_write(devc, 8, fs);
1176 
1177 	/*
1178 	 * Write to I8 starts resynchronization. Wait until it completes.
1179 	 */
1180 
1181 	timeout = 0;
1182 	while (timeout < 100 && inb(devc->base) != 0x80)
1183 		timeout++;
1184 	timeout = 0;
1185 	while (timeout < 10000 && inb(devc->base) == 0x80)
1186 		timeout++;
1187 
1188 	if (devc->model >= MD_4232)
1189 		ad_write(devc, 16, tmp & ~0x30);
1190 
1191 	ad_leave_MCE(devc);	/*
1192 				 * Starts the calibration process.
1193 				 */
1194 	restore_flags(flags);
1195 	devc->xfer_count = 0;
1196 
1197 #ifndef EXCLUDE_TIMERS
1198 	if (dev == timer_installed && devc->timer_running)
1199 		if ((fs & 0x01) != (old_fs & 0x01))
1200 		{
1201 			ad1848_tmr_reprogram(dev);
1202 		}
1203 #endif
1204 	ad1848_halt_output(dev);
1205 	return 0;
1206 }
1207 
ad1848_prepare_for_input(int dev,int bsize,int bcount)1208 static int ad1848_prepare_for_input(int dev, int bsize, int bcount)
1209 {
1210 	int timeout;
1211 	unsigned char fs, old_fs, tmp = 0;
1212 	unsigned long flags;
1213 	ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1214 	ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1215 
1216 	if (devc->audio_mode)
1217 		return 0;
1218 
1219 	save_flags(flags);
1220 	cli();
1221 	fs = portc->speed_bits | (portc->format_bits << 5);
1222 
1223 	if (portc->channels > 1)
1224 		fs |= 0x10;
1225 
1226 	ad_enter_MCE(devc);	/* Enables changes to the format select reg */
1227 
1228 	if ((devc->model == MD_1845) || (devc->model == MD_1845_SSCAPE))	/* Use alternate speed select registers */
1229 	{
1230 		fs &= 0xf0;	/* Mask off the rate select bits */
1231 
1232 		ad_write(devc, 22, (portc->speed >> 8) & 0xff);	/* Speed MSB */
1233 		ad_write(devc, 23, portc->speed & 0xff);	/* Speed LSB */
1234 	}
1235 	if (devc->model == MD_4232)
1236 	{
1237 		tmp = ad_read(devc, 16);
1238 		ad_write(devc, 16, tmp | 0x30);
1239 	}
1240 	if (devc->model == MD_IWAVE)
1241 		ad_write(devc, 17, 0xc2);	/* Disable variable frequency select */
1242 
1243 	/*
1244 	 * If mode >= 2 (CS4231), set I28. It's the capture format register.
1245 	 */
1246 
1247 	if (devc->model != MD_1848)
1248 	{
1249 		old_fs = ad_read(devc, 28);
1250 		ad_write(devc, 28, fs);
1251 
1252 		/*
1253 		 * Write to I28 starts resynchronization. Wait until it completes.
1254 		 */
1255 
1256 		timeout = 0;
1257 		while (timeout < 100 && inb(devc->base) != 0x80)
1258 			timeout++;
1259 
1260 		timeout = 0;
1261 		while (timeout < 10000 && inb(devc->base) == 0x80)
1262 			timeout++;
1263 
1264 		if (devc->model != MD_1848 && devc->model != MD_1845 && devc->model != MD_1845_SSCAPE)
1265 		{
1266 			/*
1267 			 * CS4231 compatible devices don't have separate sampling rate selection
1268 			 * register for recording an playback. The I8 register is shared so we have to
1269 			 * set the speed encoding bits of it too.
1270 			 */
1271 			unsigned char   tmp = portc->speed_bits | (ad_read(devc, 8) & 0xf0);
1272 
1273 			ad_write(devc, 8, tmp);
1274 			/*
1275 			 * Write to I8 starts resynchronization. Wait until it completes.
1276 			 */
1277 			timeout = 0;
1278 			while (timeout < 100 && inb(devc->base) != 0x80)
1279 				timeout++;
1280 
1281 			timeout = 0;
1282 			while (timeout < 10000 && inb(devc->base) == 0x80)
1283 				timeout++;
1284 		}
1285 	}
1286 	else
1287 	{			/* For AD1848 set I8. */
1288 
1289 		old_fs = ad_read(devc, 8);
1290 		ad_write(devc, 8, fs);
1291 		/*
1292 		 * Write to I8 starts resynchronization. Wait until it completes.
1293 		 */
1294 		timeout = 0;
1295 		while (timeout < 100 && inb(devc->base) != 0x80)
1296 			timeout++;
1297 		timeout = 0;
1298 		while (timeout < 10000 && inb(devc->base) == 0x80)
1299 			timeout++;
1300 	}
1301 
1302 	if (devc->model == MD_4232)
1303 		ad_write(devc, 16, tmp & ~0x30);
1304 
1305 	ad_leave_MCE(devc);	/*
1306 				 * Starts the calibration process.
1307 				 */
1308 	restore_flags(flags);
1309 	devc->xfer_count = 0;
1310 
1311 #ifndef EXCLUDE_TIMERS
1312 	if (dev == timer_installed && devc->timer_running)
1313 	{
1314 		if ((fs & 0x01) != (old_fs & 0x01))
1315 		{
1316 			ad1848_tmr_reprogram(dev);
1317 		}
1318 	}
1319 #endif
1320 	ad1848_halt_input(dev);
1321 	return 0;
1322 }
1323 
ad1848_halt(int dev)1324 static void ad1848_halt(int dev)
1325 {
1326 	ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1327 	ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1328 
1329 	unsigned char   bits = ad_read(devc, 9);
1330 
1331 	if (bits & 0x01 && (portc->open_mode & OPEN_WRITE))
1332 		ad1848_halt_output(dev);
1333 
1334 	if (bits & 0x02 && (portc->open_mode & OPEN_READ))
1335 		ad1848_halt_input(dev);
1336 	devc->audio_mode = 0;
1337 }
1338 
ad1848_halt_input(int dev)1339 static void ad1848_halt_input(int dev)
1340 {
1341 	ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1342 	unsigned long   flags;
1343 
1344 	if (!(ad_read(devc, 9) & 0x02))
1345 		return;		/* Capture not enabled */
1346 
1347 	save_flags(flags);
1348 	cli();
1349 
1350 	ad_mute(devc);
1351 
1352 	{
1353 		int             tmout;
1354 
1355 		if(!isa_dma_bridge_buggy)
1356 		        disable_dma(audio_devs[dev]->dmap_in->dma);
1357 
1358 		for (tmout = 0; tmout < 100000; tmout++)
1359 			if (ad_read(devc, 11) & 0x10)
1360 				break;
1361 		ad_write(devc, 9, ad_read(devc, 9) & ~0x02);	/* Stop capture */
1362 
1363 		if(!isa_dma_bridge_buggy)
1364 		        enable_dma(audio_devs[dev]->dmap_in->dma);
1365 		devc->audio_mode &= ~PCM_ENABLE_INPUT;
1366 	}
1367 
1368 	outb(0, io_Status(devc));	/* Clear interrupt status */
1369 	outb(0, io_Status(devc));	/* Clear interrupt status */
1370 
1371 	devc->audio_mode &= ~PCM_ENABLE_INPUT;
1372 
1373 	restore_flags(flags);
1374 }
1375 
ad1848_halt_output(int dev)1376 static void ad1848_halt_output(int dev)
1377 {
1378 	ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1379 	unsigned long flags;
1380 
1381 	if (!(ad_read(devc, 9) & 0x01))
1382 		return;		/* Playback not enabled */
1383 
1384 	save_flags(flags);
1385 	cli();
1386 
1387 	ad_mute(devc);
1388 	{
1389 		int             tmout;
1390 
1391 		if(!isa_dma_bridge_buggy)
1392 		        disable_dma(audio_devs[dev]->dmap_out->dma);
1393 
1394 		for (tmout = 0; tmout < 100000; tmout++)
1395 			if (ad_read(devc, 11) & 0x10)
1396 				break;
1397 		ad_write(devc, 9, ad_read(devc, 9) & ~0x01);	/* Stop playback */
1398 
1399 		if(!isa_dma_bridge_buggy)
1400 		       enable_dma(audio_devs[dev]->dmap_out->dma);
1401 
1402 		devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
1403 	}
1404 
1405 	outb((0), io_Status(devc));	/* Clear interrupt status */
1406 	outb((0), io_Status(devc));	/* Clear interrupt status */
1407 
1408 	devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
1409 
1410 	restore_flags(flags);
1411 }
1412 
ad1848_trigger(int dev,int state)1413 static void ad1848_trigger(int dev, int state)
1414 {
1415 	ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1416 	ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1417 	unsigned long   flags;
1418 	unsigned char   tmp, old;
1419 
1420 	save_flags(flags);
1421 	cli();
1422 	state &= devc->audio_mode;
1423 
1424 	tmp = old = ad_read(devc, 9);
1425 
1426 	if (portc->open_mode & OPEN_READ)
1427 	{
1428 		  if (state & PCM_ENABLE_INPUT)
1429 			  tmp |= 0x02;
1430 		  else
1431 			  tmp &= ~0x02;
1432 	}
1433 	if (portc->open_mode & OPEN_WRITE)
1434 	{
1435 		if (state & PCM_ENABLE_OUTPUT)
1436 			tmp |= 0x01;
1437 		else
1438 			tmp &= ~0x01;
1439 	}
1440 	/* ad_mute(devc); */
1441 	if (tmp != old)
1442 	{
1443 		  ad_write(devc, 9, tmp);
1444 		  ad_unmute(devc);
1445 	}
1446 	restore_flags(flags);
1447 }
1448 
ad1848_init_hw(ad1848_info * devc)1449 static void ad1848_init_hw(ad1848_info * devc)
1450 {
1451 	int i;
1452 	int *init_values;
1453 
1454 	/*
1455 	 * Initial values for the indirect registers of CS4248/AD1848.
1456 	 */
1457 	static int      init_values_a[] =
1458 	{
1459 		0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
1460 		0x00, 0x0c, 0x02, 0x00, 0x8a, 0x01, 0x00, 0x00,
1461 
1462 	/* Positions 16 to 31 just for CS4231/2 and ad1845 */
1463 		0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x1f, 0x40,
1464 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1465 	};
1466 
1467 	static int      init_values_b[] =
1468 	{
1469 		/*
1470 		   Values for the newer chips
1471 		   Some of the register initialization values were changed. In
1472 		   order to get rid of the click that preceded PCM playback,
1473 		   calibration was disabled on the 10th byte. On that same byte,
1474 		   dual DMA was enabled; on the 11th byte, ADC dithering was
1475 		   enabled, since that is theoretically desirable; on the 13th
1476 		   byte, Mode 3 was selected, to enable access to extended
1477 		   registers.
1478 		 */
1479 		0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
1480 		0x00, 0x00, 0x06, 0x00, 0xe0, 0x01, 0x00, 0x00,
1481  		0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x1f, 0x40,
1482  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1483 	};
1484 
1485 	/*
1486 	 *	Select initialisation data
1487 	 */
1488 
1489 	init_values = init_values_a;
1490 	if(devc->model >= MD_4236)
1491 		init_values = init_values_b;
1492 
1493 	for (i = 0; i < 16; i++)
1494 		ad_write(devc, i, init_values[i]);
1495 
1496 
1497 	ad_mute(devc);		/* Initialize some variables */
1498 	ad_unmute(devc);	/* Leave it unmuted now */
1499 
1500 	if (devc->model > MD_1848)
1501 	{
1502 		if (devc->model == MD_1845_SSCAPE)
1503 			ad_write(devc, 12, ad_read(devc, 12) | 0x50);
1504 		else
1505 			ad_write(devc, 12, ad_read(devc, 12) | 0x40);		/* Mode2 = enabled */
1506 
1507 		if (devc->model == MD_IWAVE)
1508 			ad_write(devc, 12, 0x6c);	/* Select codec mode 3 */
1509 
1510 		if (devc->model != MD_1845_SSCAPE)
1511 			for (i = 16; i < 32; i++)
1512 				ad_write(devc, i, init_values[i]);
1513 
1514 		if (devc->model == MD_IWAVE)
1515 			ad_write(devc, 16, 0x30);	/* Playback and capture counters enabled */
1516 	}
1517 	if (devc->model > MD_1848)
1518 	{
1519 		if (devc->audio_flags & DMA_DUPLEX)
1520 			ad_write(devc, 9, ad_read(devc, 9) & ~0x04);	/* Dual DMA mode */
1521 		else
1522 			ad_write(devc, 9, ad_read(devc, 9) | 0x04);	/* Single DMA mode */
1523 
1524 		if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE)
1525 			ad_write(devc, 27, ad_read(devc, 27) | 0x08);		/* Alternate freq select enabled */
1526 
1527 		if (devc->model == MD_IWAVE)
1528 		{		/* Some magic Interwave specific initialization */
1529 			ad_write(devc, 12, 0x6c);	/* Select codec mode 3 */
1530 			ad_write(devc, 16, 0x30);	/* Playback and capture counters enabled */
1531 			ad_write(devc, 17, 0xc2);	/* Alternate feature enable */
1532 		}
1533 	}
1534 	else
1535 	{
1536 		  devc->audio_flags &= ~DMA_DUPLEX;
1537 		  ad_write(devc, 9, ad_read(devc, 9) | 0x04);	/* Single DMA mode */
1538 		  if (soundpro)
1539 			  ad_write(devc, 12, ad_read(devc, 12) | 0x40);	/* Mode2 = enabled */
1540 	}
1541 
1542 	outb((0), io_Status(devc));	/* Clear pending interrupts */
1543 
1544 	/*
1545 	 * Toggle the MCE bit. It completes the initialization phase.
1546 	 */
1547 
1548 	ad_enter_MCE(devc);	/* In case the bit was off */
1549 	ad_leave_MCE(devc);
1550 
1551 	ad1848_mixer_reset(devc);
1552 }
1553 
ad1848_detect(int io_base,int * ad_flags,int * osp)1554 int ad1848_detect(int io_base, int *ad_flags, int *osp)
1555 {
1556 	unsigned char tmp;
1557 	ad1848_info *devc = &adev_info[nr_ad1848_devs];
1558 	unsigned char tmp1 = 0xff, tmp2 = 0xff;
1559 	int optiC930 = 0;	/* OPTi 82C930 flag */
1560 	int interwave = 0;
1561 	int ad1847_flag = 0;
1562 	int cs4248_flag = 0;
1563 	int sscape_flag = 0;
1564 
1565 	int i;
1566 
1567 	DDB(printk("ad1848_detect(%x)\n", io_base));
1568 
1569 	if (ad_flags)
1570 	{
1571 		if (*ad_flags == 0x12345678)
1572 		{
1573 			interwave = 1;
1574 			*ad_flags = 0;
1575 		}
1576 
1577 		if (*ad_flags == 0x87654321)
1578 		{
1579 			sscape_flag = 1;
1580 			*ad_flags = 0;
1581 		}
1582 
1583 		if (*ad_flags == 0x12345677)
1584 		{
1585 		    cs4248_flag = 1;
1586 		    *ad_flags = 0;
1587 		}
1588 	}
1589 	if (nr_ad1848_devs >= MAX_AUDIO_DEV)
1590 	{
1591 		printk(KERN_ERR "ad1848 - Too many audio devices\n");
1592 		return 0;
1593 	}
1594 	if (check_region(io_base, 4))
1595 	{
1596 		printk(KERN_ERR "ad1848.c: Port %x not free.\n", io_base);
1597 		return 0;
1598 	}
1599 	devc->base = io_base;
1600 	devc->irq_ok = 0;
1601 	devc->timer_running = 0;
1602 	devc->MCE_bit = 0x40;
1603 	devc->irq = 0;
1604 	devc->open_mode = 0;
1605 	devc->chip_name = devc->name = "AD1848";
1606 	devc->model = MD_1848;	/* AD1848 or CS4248 */
1607 	devc->levels = NULL;
1608 	devc->debug_flag = 0;
1609 
1610 	/*
1611 	 * Check that the I/O address is in use.
1612 	 *
1613 	 * The bit 0x80 of the base I/O port is known to be 0 after the
1614 	 * chip has performed its power on initialization. Just assume
1615 	 * this has happened before the OS is starting.
1616 	 *
1617 	 * If the I/O address is unused, it typically returns 0xff.
1618 	 */
1619 
1620 	if (inb(devc->base) == 0xff)
1621 	{
1622 		DDB(printk("ad1848_detect: The base I/O address appears to be dead\n"));
1623 	}
1624 
1625 	/*
1626 	 * Wait for the device to stop initialization
1627 	 */
1628 
1629 	DDB(printk("ad1848_detect() - step 0\n"));
1630 
1631 	for (i = 0; i < 10000000; i++)
1632 	{
1633 		unsigned char   x = inb(devc->base);
1634 
1635 		if (x == 0xff || !(x & 0x80))
1636 			break;
1637 	}
1638 
1639 	DDB(printk("ad1848_detect() - step A\n"));
1640 
1641 	if (inb(devc->base) == 0x80)	/* Not ready. Let's wait */
1642 		ad_leave_MCE(devc);
1643 
1644 	if ((inb(devc->base) & 0x80) != 0x00)	/* Not a AD1848 */
1645 	{
1646 		DDB(printk("ad1848 detect error - step A (%02x)\n", (int) inb(devc->base)));
1647 		return 0;
1648 	}
1649 
1650 	/*
1651 	 * Test if it's possible to change contents of the indirect registers.
1652 	 * Registers 0 and 1 are ADC volume registers. The bit 0x10 is read only
1653 	 * so try to avoid using it.
1654 	 */
1655 
1656 	DDB(printk("ad1848_detect() - step B\n"));
1657 	ad_write(devc, 0, 0xaa);
1658 	ad_write(devc, 1, 0x45);	/* 0x55 with bit 0x10 clear */
1659 
1660 	if ((tmp1 = ad_read(devc, 0)) != 0xaa || (tmp2 = ad_read(devc, 1)) != 0x45)
1661 	{
1662 		if (tmp2 == 0x65)	/* AD1847 has couple of bits hardcoded to 1 */
1663 			ad1847_flag = 1;
1664 		else
1665 		{
1666 			DDB(printk("ad1848 detect error - step B (%x/%x)\n", tmp1, tmp2));
1667 			return 0;
1668 		}
1669 	}
1670 	DDB(printk("ad1848_detect() - step C\n"));
1671 	ad_write(devc, 0, 0x45);
1672 	ad_write(devc, 1, 0xaa);
1673 
1674 	if ((tmp1 = ad_read(devc, 0)) != 0x45 || (tmp2 = ad_read(devc, 1)) != 0xaa)
1675 	{
1676 		if (tmp2 == 0x8a)	/* AD1847 has few bits hardcoded to 1 */
1677 			ad1847_flag = 1;
1678 		else
1679 		{
1680 			DDB(printk("ad1848 detect error - step C (%x/%x)\n", tmp1, tmp2));
1681 			return 0;
1682 		}
1683 	}
1684 
1685 	/*
1686 	 * The indirect register I12 has some read only bits. Let's
1687 	 * try to change them.
1688 	 */
1689 
1690 	DDB(printk("ad1848_detect() - step D\n"));
1691 	tmp = ad_read(devc, 12);
1692 	ad_write(devc, 12, (~tmp) & 0x0f);
1693 
1694 	if ((tmp & 0x0f) != ((tmp1 = ad_read(devc, 12)) & 0x0f))
1695 	{
1696 		DDB(printk("ad1848 detect error - step D (%x)\n", tmp1));
1697 		return 0;
1698 	}
1699 
1700 	/*
1701 	 * NOTE! Last 4 bits of the reg I12 tell the chip revision.
1702 	 *   0x01=RevB and 0x0A=RevC.
1703 	 */
1704 
1705 	/*
1706 	 * The original AD1848/CS4248 has just 15 indirect registers. This means
1707 	 * that I0 and I16 should return the same value (etc.).
1708 	 * However this doesn't work with CS4248. Actually it seems to be impossible
1709 	 * to detect if the chip is a CS4231 or CS4248.
1710 	 * Ensure that the Mode2 enable bit of I12 is 0. Otherwise this test fails
1711 	 * with CS4231.
1712 	 */
1713 
1714 	/*
1715 	 * OPTi 82C930 has mode2 control bit in another place. This test will fail
1716 	 * with it. Accept this situation as a possible indication of this chip.
1717 	 */
1718 
1719 	DDB(printk("ad1848_detect() - step F\n"));
1720 	ad_write(devc, 12, 0);	/* Mode2=disabled */
1721 
1722 	for (i = 0; i < 16; i++)
1723 	{
1724 		if ((tmp1 = ad_read(devc, i)) != (tmp2 = ad_read(devc, i + 16)))
1725 		{
1726 			DDB(printk("ad1848 detect step F(%d/%x/%x) - OPTi chip???\n", i, tmp1, tmp2));
1727 			if (!ad1847_flag)
1728 				optiC930 = 1;
1729 			break;
1730 		}
1731 	}
1732 
1733 	/*
1734 	 * Try to switch the chip to mode2 (CS4231) by setting the MODE2 bit (0x40).
1735 	 * The bit 0x80 is always 1 in CS4248 and CS4231.
1736 	 */
1737 
1738 	DDB(printk("ad1848_detect() - step G\n"));
1739 
1740 	if (ad_flags && *ad_flags == 400)
1741 		*ad_flags = 0;
1742 	else
1743 		ad_write(devc, 12, 0x40);	/* Set mode2, clear 0x80 */
1744 
1745 
1746 	if (ad_flags)
1747 		*ad_flags = 0;
1748 
1749 	tmp1 = ad_read(devc, 12);
1750 	if (tmp1 & 0x80)
1751 	{
1752 		if (ad_flags)
1753 			*ad_flags |= AD_F_CS4248;
1754 
1755 		devc->chip_name = "CS4248";	/* Our best knowledge just now */
1756 	}
1757 	if (optiC930 || (tmp1 & 0xc0) == (0x80 | 0x40))
1758 	{
1759 		/*
1760 		 *      CS4231 detected - is it?
1761 		 *
1762 		 *      Verify that setting I0 doesn't change I16.
1763 		 */
1764 
1765 		DDB(printk("ad1848_detect() - step H\n"));
1766 		ad_write(devc, 16, 0);	/* Set I16 to known value */
1767 
1768 		ad_write(devc, 0, 0x45);
1769 		if ((tmp1 = ad_read(devc, 16)) != 0x45)	/* No change -> CS4231? */
1770 		{
1771 			ad_write(devc, 0, 0xaa);
1772 			if ((tmp1 = ad_read(devc, 16)) == 0xaa)	/* Rotten bits? */
1773 			{
1774 				DDB(printk("ad1848 detect error - step H(%x)\n", tmp1));
1775 				return 0;
1776 			}
1777 
1778 			/*
1779 			 * Verify that some bits of I25 are read only.
1780 			 */
1781 
1782 			DDB(printk("ad1848_detect() - step I\n"));
1783 			tmp1 = ad_read(devc, 25);	/* Original bits */
1784 			ad_write(devc, 25, ~tmp1);	/* Invert all bits */
1785 			if ((ad_read(devc, 25) & 0xe7) == (tmp1 & 0xe7))
1786 			{
1787 				int id;
1788 
1789 				/*
1790 				 *      It's at least CS4231
1791 				 */
1792 
1793 				devc->chip_name = "CS4231";
1794 				devc->model = MD_4231;
1795 
1796 				/*
1797 				 * It could be an AD1845 or CS4231A as well.
1798 				 * CS4231 and AD1845 report the same revision info in I25
1799 				 * while the CS4231A reports different.
1800 				 */
1801 
1802 				id = ad_read(devc, 25);
1803 				if ((id & 0xe7) == 0x80)	/* Device busy??? */
1804 					id = ad_read(devc, 25);
1805 				if ((id & 0xe7) == 0x80)	/* Device still busy??? */
1806 					id = ad_read(devc, 25);
1807 				DDB(printk("ad1848_detect() - step J (%02x/%02x)\n", id, ad_read(devc, 25)));
1808 
1809                                 if ((id & 0xe7) == 0x80) {
1810 					/*
1811 					 * It must be a CS4231 or AD1845. The register I23 of
1812 					 * CS4231 is undefined and it appears to be read only.
1813 					 * AD1845 uses I23 for setting sample rate. Assume
1814 					 * the chip is AD1845 if I23 is changeable.
1815 					 */
1816 
1817 					unsigned char   tmp = ad_read(devc, 23);
1818 					ad_write(devc, 23, ~tmp);
1819 
1820 					if (interwave)
1821 					{
1822 						devc->model = MD_IWAVE;
1823 						devc->chip_name = "IWave";
1824 					}
1825 					else if (ad_read(devc, 23) != tmp)	/* AD1845 ? */
1826 					{
1827 						devc->chip_name = "AD1845";
1828 						devc->model = MD_1845;
1829 					}
1830 					else if (cs4248_flag)
1831 					{
1832 						if (ad_flags)
1833 							  *ad_flags |= AD_F_CS4248;
1834 						devc->chip_name = "CS4248";
1835 						devc->model = MD_1848;
1836 						ad_write(devc, 12, ad_read(devc, 12) & ~0x40);	/* Mode2 off */
1837 					}
1838 					ad_write(devc, 23, tmp);	/* Restore */
1839 				}
1840 				else
1841 				{
1842 					switch (id & 0x1f) {
1843 					case 3: /* CS4236/CS4235/CS42xB/CS4239 */
1844 						{
1845 							int xid;
1846 							ad_write(devc, 12, ad_read(devc, 12) | 0x60); /* switch to mode 3 */
1847 							ad_write(devc, 23, 0x9c); /* select extended register 25 */
1848 							xid = inb(io_Indexed_Data(devc));
1849 							ad_write(devc, 12, ad_read(devc, 12) & ~0x60); /* back to mode 0 */
1850 							switch (xid & 0x1f)
1851 							{
1852 								case 0x00:
1853 									devc->chip_name = "CS4237B(B)";
1854 									devc->model = MD_42xB;
1855 									break;
1856 								case 0x08:
1857 									/* Seems to be a 4238 ?? */
1858 									devc->chip_name = "CS4238";
1859 									devc->model = MD_42xB;
1860 									break;
1861 								case 0x09:
1862 									devc->chip_name = "CS4238B";
1863 									devc->model = MD_42xB;
1864 									break;
1865 								case 0x0b:
1866 									devc->chip_name = "CS4236B";
1867 									devc->model = MD_4236;
1868 									break;
1869 								case 0x10:
1870 									devc->chip_name = "CS4237B";
1871 									devc->model = MD_42xB;
1872 									break;
1873 								case 0x1d:
1874 									devc->chip_name = "CS4235";
1875 									devc->model = MD_4235;
1876 									break;
1877 								case 0x1e:
1878 									devc->chip_name = "CS4239";
1879 									devc->model = MD_4239;
1880 									break;
1881 								default:
1882 									printk("Chip ident is %X.\n", xid&0x1F);
1883 									devc->chip_name = "CS42xx";
1884 									devc->model = MD_4232;
1885 									break;
1886 							}
1887 						}
1888 						break;
1889 
1890 					case 2: /* CS4232/CS4232A */
1891 						devc->chip_name = "CS4232";
1892 						devc->model = MD_4232;
1893 						break;
1894 
1895 					case 0:
1896 						if ((id & 0xe0) == 0xa0)
1897 						{
1898 							devc->chip_name = "CS4231A";
1899 							devc->model = MD_4231A;
1900 						}
1901 						else
1902 						{
1903 							devc->chip_name = "CS4321";
1904 							devc->model = MD_4231;
1905 						}
1906 						break;
1907 
1908 					default: /* maybe */
1909 						DDB(printk("ad1848: I25 = %02x/%02x\n", ad_read(devc, 25), ad_read(devc, 25) & 0xe7));
1910                                                 if (optiC930)
1911                                                 {
1912                                                         devc->chip_name = "82C930";
1913                                                         devc->model = MD_C930;
1914                                                 }
1915 						else
1916 						{
1917 							devc->chip_name = "CS4231";
1918 							devc->model = MD_4231;
1919 						}
1920 					}
1921 				}
1922 			}
1923 			ad_write(devc, 25, tmp1);	/* Restore bits */
1924 
1925 			DDB(printk("ad1848_detect() - step K\n"));
1926 		}
1927 	} else if (tmp1 == 0x0a) {
1928 		/*
1929 		 * Is it perhaps a SoundPro CMI8330?
1930 		 * If so, then we should be able to change indirect registers
1931 		 * greater than I15 after activating MODE2, even though reading
1932 		 * back I12 does not show it.
1933 		 */
1934 
1935 		/*
1936 		 * Let's try comparing register values
1937 		 */
1938 		for (i = 0; i < 16; i++) {
1939 			if ((tmp1 = ad_read(devc, i)) != (tmp2 = ad_read(devc, i + 16))) {
1940 				DDB(printk("ad1848 detect step H(%d/%x/%x) - SoundPro chip?\n", i, tmp1, tmp2));
1941 				soundpro = 1;
1942 				devc->chip_name = "SoundPro CMI 8330";
1943 				break;
1944 			}
1945 		}
1946 	}
1947 
1948 	DDB(printk("ad1848_detect() - step L\n"));
1949 	if (ad_flags)
1950 	{
1951 		  if (devc->model != MD_1848)
1952 			  *ad_flags |= AD_F_CS4231;
1953 	}
1954 	DDB(printk("ad1848_detect() - Detected OK\n"));
1955 
1956 	if (devc->model == MD_1848 && ad1847_flag)
1957 		devc->chip_name = "AD1847";
1958 
1959 
1960 	if (sscape_flag == 1)
1961 		devc->model = MD_1845_SSCAPE;
1962 
1963 	return 1;
1964 }
1965 
ad1848_init(char * name,int io_base,int irq,int dma_playback,int dma_capture,int share_dma,int * osp,struct module * owner)1966 int ad1848_init (char *name, int io_base, int irq, int dma_playback,
1967 		int dma_capture, int share_dma, int *osp, struct module *owner)
1968 {
1969 	/*
1970 	 * NOTE! If irq < 0, there is another driver which has allocated the IRQ
1971 	 *   so that this driver doesn't need to allocate/deallocate it.
1972 	 *   The actually used IRQ is ABS(irq).
1973 	 */
1974 
1975 	int my_dev;
1976 	char dev_name[100];
1977 	int e;
1978 
1979 	ad1848_info  *devc = &adev_info[nr_ad1848_devs];
1980 
1981 	ad1848_port_info *portc = NULL;
1982 
1983 	devc->irq = (irq > 0) ? irq : 0;
1984 	devc->open_mode = 0;
1985 	devc->timer_ticks = 0;
1986 	devc->dma1 = dma_playback;
1987 	devc->dma2 = dma_capture;
1988 	devc->subtype = cfg.card_subtype;
1989 	devc->audio_flags = DMA_AUTOMODE;
1990 	devc->playback_dev = devc->record_dev = 0;
1991 	if (name != NULL)
1992 		devc->name = name;
1993 
1994 	if (name != NULL && name[0] != 0)
1995 		sprintf(dev_name,
1996 			"%s (%s)", name, devc->chip_name);
1997 	else
1998 		sprintf(dev_name,
1999 			"Generic audio codec (%s)", devc->chip_name);
2000 
2001 	request_region(devc->base, 4, devc->name);
2002 
2003 	conf_printf2(dev_name, devc->base, devc->irq, dma_playback, dma_capture);
2004 
2005 	if (devc->model == MD_1848 || devc->model == MD_C930)
2006 		devc->audio_flags |= DMA_HARDSTOP;
2007 
2008 	if (devc->model > MD_1848)
2009 	{
2010 		if (devc->dma1 == devc->dma2 || devc->dma2 == -1 || devc->dma1 == -1)
2011 			devc->audio_flags &= ~DMA_DUPLEX;
2012 		else
2013 			devc->audio_flags |= DMA_DUPLEX;
2014 	}
2015 
2016 	portc = (ad1848_port_info *) kmalloc(sizeof(ad1848_port_info), GFP_KERNEL);
2017 	if(portc==NULL)
2018 		return -1;
2019 
2020 	if ((my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
2021 					     dev_name,
2022 					     &ad1848_audio_driver,
2023 					     sizeof(struct audio_driver),
2024 					     devc->audio_flags,
2025 					     ad_format_mask[devc->model],
2026 					     devc,
2027 					     dma_playback,
2028 					     dma_capture)) < 0)
2029 	{
2030 		kfree(portc);
2031 		portc=NULL;
2032 		return -1;
2033 	}
2034 
2035 	audio_devs[my_dev]->portc = portc;
2036 	audio_devs[my_dev]->mixer_dev = -1;
2037 	if (owner)
2038 		audio_devs[my_dev]->d->owner = owner;
2039 	memset((char *) portc, 0, sizeof(*portc));
2040 
2041 	nr_ad1848_devs++;
2042 
2043 	devc->pmdev = pm_register(PM_ISA_DEV, my_dev, ad1848_pm_callback);
2044 	if (devc->pmdev)
2045 		devc->pmdev->data = devc;
2046 
2047 	ad1848_init_hw(devc);
2048 
2049 	if (irq > 0)
2050 	{
2051 		devc->dev_no = my_dev;
2052 		if (request_irq(devc->irq, adintr, 0, devc->name, (void *)my_dev) < 0)
2053 		{
2054 			printk(KERN_WARNING "ad1848: Unable to allocate IRQ\n");
2055 			/* Don't free it either then.. */
2056 			devc->irq = 0;
2057 		}
2058 		if (capabilities[devc->model].flags & CAP_F_TIMER)
2059 		{
2060 #ifndef CONFIG_SMP
2061 			int x;
2062 			unsigned char tmp = ad_read(devc, 16);
2063 #endif
2064 
2065 			devc->timer_ticks = 0;
2066 
2067 			ad_write(devc, 21, 0x00);	/* Timer MSB */
2068 			ad_write(devc, 20, 0x10);	/* Timer LSB */
2069 #ifndef CONFIG_SMP
2070 			ad_write(devc, 16, tmp | 0x40);	/* Enable timer */
2071 			for (x = 0; x < 100000 && devc->timer_ticks == 0; x++);
2072 			ad_write(devc, 16, tmp & ~0x40);	/* Disable timer */
2073 
2074 			if (devc->timer_ticks == 0)
2075 				printk(KERN_WARNING "ad1848: Interrupt test failed (IRQ%d)\n", irq);
2076 			else
2077 			{
2078 				DDB(printk("Interrupt test OK\n"));
2079 				devc->irq_ok = 1;
2080 			}
2081 #else
2082 			devc->irq_ok = 1;
2083 #endif
2084 		}
2085 		else
2086 			devc->irq_ok = 1;	/* Couldn't test. assume it's OK */
2087 	} else if (irq < 0)
2088 		irq2dev[-irq] = devc->dev_no = my_dev;
2089 
2090 #ifndef EXCLUDE_TIMERS
2091 	if ((capabilities[devc->model].flags & CAP_F_TIMER) &&
2092 	    devc->irq_ok)
2093 		ad1848_tmr_install(my_dev);
2094 #endif
2095 
2096 	if (!share_dma)
2097 	{
2098 		if (sound_alloc_dma(dma_playback, devc->name))
2099 			printk(KERN_WARNING "ad1848.c: Can't allocate DMA%d\n", dma_playback);
2100 
2101 		if (dma_capture != dma_playback)
2102 			if (sound_alloc_dma(dma_capture, devc->name))
2103 				printk(KERN_WARNING "ad1848.c: Can't allocate DMA%d\n", dma_capture);
2104 	}
2105 
2106 	if ((e = sound_install_mixer(MIXER_DRIVER_VERSION,
2107 				     dev_name,
2108 				     &ad1848_mixer_operations,
2109 				     sizeof(struct mixer_operations),
2110 				     devc)) >= 0)
2111 	{
2112 		audio_devs[my_dev]->mixer_dev = e;
2113 		if (owner)
2114 			mixer_devs[e]->owner = owner;
2115 	}
2116 	return my_dev;
2117 }
2118 
ad1848_control(int cmd,int arg)2119 int ad1848_control(int cmd, int arg)
2120 {
2121 	ad1848_info *devc;
2122 
2123 	if (nr_ad1848_devs < 1)
2124 		return -ENODEV;
2125 
2126 	devc = &adev_info[nr_ad1848_devs - 1];
2127 
2128 	switch (cmd)
2129 	{
2130 		case AD1848_SET_XTAL:	/* Change clock frequency of AD1845 (only ) */
2131 			if (devc->model != MD_1845 || devc->model != MD_1845_SSCAPE)
2132 				return -EINVAL;
2133 			ad_enter_MCE(devc);
2134 			ad_write(devc, 29, (ad_read(devc, 29) & 0x1f) | (arg << 5));
2135 			ad_leave_MCE(devc);
2136 			break;
2137 
2138 		case AD1848_MIXER_REROUTE:
2139 		{
2140 			int o = (arg >> 8) & 0xff;
2141 			int n = arg & 0xff;
2142 
2143 			if (o < 0 || o >= SOUND_MIXER_NRDEVICES)
2144 				return -EINVAL;
2145 
2146 			if (!(devc->supported_devices & (1 << o)) &&
2147 			    !(devc->supported_rec_devices & (1 << o)))
2148 				return -EINVAL;
2149 
2150 			if (n == SOUND_MIXER_NONE)
2151 			{	/* Just hide this control */
2152 				ad1848_mixer_set(devc, o, 0);	/* Shut up it */
2153 				devc->supported_devices &= ~(1 << o);
2154 				devc->supported_rec_devices &= ~(1 << o);
2155 				break;
2156 			}
2157 
2158 			/* Make the mixer control identified by o to appear as n */
2159 			if (n < 0 || n >= SOUND_MIXER_NRDEVICES)
2160 				return -EINVAL;
2161 
2162 			devc->mixer_reroute[n] = o;	/* Rename the control */
2163 			if (devc->supported_devices & (1 << o))
2164 				devc->supported_devices |= (1 << n);
2165 			if (devc->supported_rec_devices & (1 << o))
2166 				devc->supported_rec_devices |= (1 << n);
2167 
2168 			devc->supported_devices &= ~(1 << o);
2169 			devc->supported_rec_devices &= ~(1 << o);
2170 		}
2171 		break;
2172 	}
2173 	return 0;
2174 }
2175 
ad1848_unload(int io_base,int irq,int dma_playback,int dma_capture,int share_dma)2176 void ad1848_unload(int io_base, int irq, int dma_playback, int dma_capture, int share_dma)
2177 {
2178 	int i, mixer, dev = 0;
2179 	ad1848_info *devc = NULL;
2180 
2181 	for (i = 0; devc == NULL && i < nr_ad1848_devs; i++)
2182 	{
2183 		if (adev_info[i].base == io_base)
2184 		{
2185 			devc = &adev_info[i];
2186 			dev = devc->dev_no;
2187 		}
2188 	}
2189 
2190 	if (devc != NULL)
2191 	{
2192 		if(audio_devs[dev]->portc!=NULL)
2193 			kfree(audio_devs[dev]->portc);
2194 		release_region(devc->base, 4);
2195 
2196 		if (!share_dma)
2197 		{
2198 			if (devc->irq > 0) /* There is no point in freeing irq, if it wasn't allocated */
2199 				free_irq(devc->irq, (void *)devc->dev_no);
2200 
2201 			sound_free_dma(dma_playback);
2202 
2203 			if (dma_playback != dma_capture)
2204 				sound_free_dma(dma_capture);
2205 
2206 		}
2207 		mixer = audio_devs[devc->dev_no]->mixer_dev;
2208 		if(mixer>=0)
2209 			sound_unload_mixerdev(mixer);
2210 
2211 		if (devc->pmdev)
2212 			pm_unregister(devc->pmdev);
2213 
2214 		nr_ad1848_devs--;
2215 		for ( ; i < nr_ad1848_devs ; i++)
2216 			adev_info[i] = adev_info[i+1];
2217 	}
2218 	else
2219 		printk(KERN_ERR "ad1848: Can't find device to be unloaded. Base=%x\n", io_base);
2220 }
2221 
adintr(int irq,void * dev_id,struct pt_regs * dummy)2222 void adintr(int irq, void *dev_id, struct pt_regs *dummy)
2223 {
2224 	unsigned char status;
2225 	ad1848_info *devc;
2226 	int dev;
2227 	int alt_stat = 0xff;
2228 	unsigned char c930_stat = 0;
2229 	int cnt = 0;
2230 
2231 	dev = (int)dev_id;
2232 	devc = (ad1848_info *) audio_devs[dev]->devc;
2233 
2234 interrupt_again:		/* Jump back here if int status doesn't reset */
2235 
2236 	status = inb(io_Status(devc));
2237 
2238 	if (status == 0x80)
2239 		printk(KERN_DEBUG "adintr: Why?\n");
2240 	if (devc->model == MD_1848)
2241 		outb((0), io_Status(devc));	/* Clear interrupt status */
2242 
2243 	if (status & 0x01)
2244 	{
2245 		if (devc->model == MD_C930)
2246 		{		/* 82C930 has interrupt status register in MAD16 register MC11 */
2247 			unsigned long   flags;
2248 
2249 			save_flags(flags);
2250 			cli();
2251 
2252 			/* 0xe0e is C930 address port
2253 			 * 0xe0f is C930 data port
2254 			 */
2255 			outb(11, 0xe0e);
2256 			c930_stat = inb(0xe0f);
2257 			outb((~c930_stat), 0xe0f);
2258 
2259 			restore_flags(flags);
2260 
2261 			alt_stat = (c930_stat << 2) & 0x30;
2262 		}
2263 		else if (devc->model != MD_1848)
2264 		{
2265 			alt_stat = ad_read(devc, 24);
2266 			ad_write(devc, 24, ad_read(devc, 24) & ~alt_stat);	/* Selective ack */
2267 		}
2268 
2269 		if ((devc->open_mode & OPEN_READ) && (devc->audio_mode & PCM_ENABLE_INPUT) && (alt_stat & 0x20))
2270 		{
2271 			DMAbuf_inputintr(devc->record_dev);
2272 		}
2273 		if ((devc->open_mode & OPEN_WRITE) && (devc->audio_mode & PCM_ENABLE_OUTPUT) &&
2274 		      (alt_stat & 0x10))
2275 		{
2276 			DMAbuf_outputintr(devc->playback_dev, 1);
2277 		}
2278 		if (devc->model != MD_1848 && (alt_stat & 0x40))	/* Timer interrupt */
2279 		{
2280 			devc->timer_ticks++;
2281 #ifndef EXCLUDE_TIMERS
2282 			if (timer_installed == dev && devc->timer_running)
2283 				sound_timer_interrupt();
2284 #endif
2285 		}
2286 	}
2287 /*
2288  * Sometimes playback or capture interrupts occur while a timer interrupt
2289  * is being handled. The interrupt will not be retriggered if we don't
2290  * handle it now. Check if an interrupt is still pending and restart
2291  * the handler in this case.
2292  */
2293 	if (inb(io_Status(devc)) & 0x01 && cnt++ < 4)
2294 	{
2295 		  goto interrupt_again;
2296 	}
2297 }
2298 
2299 /*
2300  *	Experimental initialization sequence for the integrated sound system
2301  *	of the Compaq Deskpro M.
2302  */
2303 
init_deskpro_m(struct address_info * hw_config)2304 static int init_deskpro_m(struct address_info *hw_config)
2305 {
2306 	unsigned char   tmp;
2307 
2308 	if ((tmp = inb(0xc44)) == 0xff)
2309 	{
2310 		DDB(printk("init_deskpro_m: Dead port 0xc44\n"));
2311 		return 0;
2312 	}
2313 
2314 	outb(0x10, 0xc44);
2315 	outb(0x40, 0xc45);
2316 	outb(0x00, 0xc46);
2317 	outb(0xe8, 0xc47);
2318 	outb(0x14, 0xc44);
2319 	outb(0x40, 0xc45);
2320 	outb(0x00, 0xc46);
2321 	outb(0xe8, 0xc47);
2322 	outb(0x10, 0xc44);
2323 
2324 	return 1;
2325 }
2326 
2327 /*
2328  *	Experimental initialization sequence for the integrated sound system
2329  *	of Compaq Deskpro XL.
2330  */
2331 
init_deskpro(struct address_info * hw_config)2332 static int init_deskpro(struct address_info *hw_config)
2333 {
2334 	unsigned char   tmp;
2335 
2336 	if ((tmp = inb(0xc44)) == 0xff)
2337 	{
2338 		DDB(printk("init_deskpro: Dead port 0xc44\n"));
2339 		return 0;
2340 	}
2341 	outb((tmp | 0x04), 0xc44);	/* Select bank 1 */
2342 	if (inb(0xc44) != 0x04)
2343 	{
2344 		DDB(printk("init_deskpro: Invalid bank1 signature in port 0xc44\n"));
2345 		return 0;
2346 	}
2347 	/*
2348 	 * OK. It looks like a Deskpro so let's proceed.
2349 	 */
2350 
2351 	/*
2352 	 * I/O port 0xc44 Audio configuration register.
2353 	 *
2354 	 * bits 0xc0:   Audio revision bits
2355 	 *              0x00 = Compaq Business Audio
2356 	 *              0x40 = MS Sound System Compatible (reset default)
2357 	 *              0x80 = Reserved
2358 	 *              0xc0 = Reserved
2359 	 * bit 0x20:    No Wait State Enable
2360 	 *              0x00 = Disabled (reset default, DMA mode)
2361 	 *              0x20 = Enabled (programmed I/O mode)
2362 	 * bit 0x10:    MS Sound System Decode Enable
2363 	 *              0x00 = Decoding disabled (reset default)
2364 	 *              0x10 = Decoding enabled
2365 	 * bit 0x08:    FM Synthesis Decode Enable
2366 	 *              0x00 = Decoding Disabled (reset default)
2367 	 *              0x08 = Decoding enabled
2368 	 * bit 0x04     Bank select
2369 	 *              0x00 = Bank 0
2370 	 *              0x04 = Bank 1
2371 	 * bits 0x03    MSS Base address
2372 	 *              0x00 = 0x530 (reset default)
2373 	 *              0x01 = 0x604
2374 	 *              0x02 = 0xf40
2375 	 *              0x03 = 0xe80
2376 	 */
2377 
2378 #ifdef DEBUGXL
2379 	/* Debug printing */
2380 	printk("Port 0xc44 (before): ");
2381 	outb((tmp & ~0x04), 0xc44);
2382 	printk("%02x ", inb(0xc44));
2383 	outb((tmp | 0x04), 0xc44);
2384 	printk("%02x\n", inb(0xc44));
2385 #endif
2386 
2387 	/* Set bank 1 of the register */
2388 	tmp = 0x58;		/* MSS Mode, MSS&FM decode enabled */
2389 
2390 	switch (hw_config->io_base)
2391 	{
2392 		case 0x530:
2393 			tmp |= 0x00;
2394 			break;
2395 		case 0x604:
2396 			tmp |= 0x01;
2397 			break;
2398 		case 0xf40:
2399 			tmp |= 0x02;
2400 			break;
2401 		case 0xe80:
2402 			tmp |= 0x03;
2403 			break;
2404 		default:
2405 			DDB(printk("init_deskpro: Invalid MSS port %x\n", hw_config->io_base));
2406 			return 0;
2407 	}
2408 	outb((tmp & ~0x04), 0xc44);	/* Write to bank=0 */
2409 
2410 #ifdef DEBUGXL
2411 	/* Debug printing */
2412 	printk("Port 0xc44 (after): ");
2413 	outb((tmp & ~0x04), 0xc44);	/* Select bank=0 */
2414 	printk("%02x ", inb(0xc44));
2415 	outb((tmp | 0x04), 0xc44);	/* Select bank=1 */
2416 	printk("%02x\n", inb(0xc44));
2417 #endif
2418 
2419 	/*
2420 	 * I/O port 0xc45 FM Address Decode/MSS ID Register.
2421 	 *
2422 	 * bank=0, bits 0xfe:   FM synthesis Decode Compare bits 7:1 (default=0x88)
2423 	 * bank=0, bit 0x01:    SBIC Power Control Bit
2424 	 *                      0x00 = Powered up
2425 	 *                      0x01 = Powered down
2426 	 * bank=1, bits 0xfc:   MSS ID (default=0x40)
2427 	 */
2428 
2429 #ifdef DEBUGXL
2430 	/* Debug printing */
2431 	printk("Port 0xc45 (before): ");
2432 	outb((tmp & ~0x04), 0xc44);	/* Select bank=0 */
2433 	printk("%02x ", inb(0xc45));
2434 	outb((tmp | 0x04), 0xc44);	/* Select bank=1 */
2435 	printk("%02x\n", inb(0xc45));
2436 #endif
2437 
2438 	outb((tmp & ~0x04), 0xc44);	/* Select bank=0 */
2439 	outb((0x88), 0xc45);	/* FM base 7:0 = 0x88 */
2440 	outb((tmp | 0x04), 0xc44);	/* Select bank=1 */
2441 	outb((0x10), 0xc45);	/* MSS ID = 0x10 (MSS port returns 0x04) */
2442 
2443 #ifdef DEBUGXL
2444 	/* Debug printing */
2445 	printk("Port 0xc45 (after): ");
2446 	outb((tmp & ~0x04), 0xc44);	/* Select bank=0 */
2447 	printk("%02x ", inb(0xc45));
2448 	outb((tmp | 0x04), 0xc44);	/* Select bank=1 */
2449 	printk("%02x\n", inb(0xc45));
2450 #endif
2451 
2452 
2453 	/*
2454 	 * I/O port 0xc46 FM Address Decode/Address ASIC Revision Register.
2455 	 *
2456 	 * bank=0, bits 0xff:   FM synthesis Decode Compare bits 15:8 (default=0x03)
2457 	 * bank=1, bits 0xff:   Audio addressing ASIC id
2458 	 */
2459 
2460 #ifdef DEBUGXL
2461 	/* Debug printing */
2462 	printk("Port 0xc46 (before): ");
2463 	outb((tmp & ~0x04), 0xc44);	/* Select bank=0 */
2464 	printk("%02x ", inb(0xc46));
2465 	outb((tmp | 0x04), 0xc44);	/* Select bank=1 */
2466 	printk("%02x\n", inb(0xc46));
2467 #endif
2468 
2469 	outb((tmp & ~0x04), 0xc44);	/* Select bank=0 */
2470 	outb((0x03), 0xc46);	/* FM base 15:8 = 0x03 */
2471 	outb((tmp | 0x04), 0xc44);	/* Select bank=1 */
2472 	outb((0x11), 0xc46);	/* ASIC ID = 0x11 */
2473 
2474 #ifdef DEBUGXL
2475 	/* Debug printing */
2476 	printk("Port 0xc46 (after): ");
2477 	outb((tmp & ~0x04), 0xc44);	/* Select bank=0 */
2478 	printk("%02x ", inb(0xc46));
2479 	outb((tmp | 0x04), 0xc44);	/* Select bank=1 */
2480 	printk("%02x\n", inb(0xc46));
2481 #endif
2482 
2483 	/*
2484 	 * I/O port 0xc47 FM Address Decode Register.
2485 	 *
2486 	 * bank=0, bits 0xff:   Decode enable selection for various FM address bits
2487 	 * bank=1, bits 0xff:   Reserved
2488 	 */
2489 
2490 #ifdef DEBUGXL
2491 	/* Debug printing */
2492 	printk("Port 0xc47 (before): ");
2493 	outb((tmp & ~0x04), 0xc44);	/* Select bank=0 */
2494 	printk("%02x ", inb(0xc47));
2495 	outb((tmp | 0x04), 0xc44);	/* Select bank=1 */
2496 	printk("%02x\n", inb(0xc47));
2497 #endif
2498 
2499 	outb((tmp & ~0x04), 0xc44);	/* Select bank=0 */
2500 	outb((0x7c), 0xc47);	/* FM decode enable bits = 0x7c */
2501 	outb((tmp | 0x04), 0xc44);	/* Select bank=1 */
2502 	outb((0x00), 0xc47);	/* Reserved bank1 = 0x00 */
2503 
2504 #ifdef DEBUGXL
2505 	/* Debug printing */
2506 	printk("Port 0xc47 (after): ");
2507 	outb((tmp & ~0x04), 0xc44);	/* Select bank=0 */
2508 	printk("%02x ", inb(0xc47));
2509 	outb((tmp | 0x04), 0xc44);	/* Select bank=1 */
2510 	printk("%02x\n", inb(0xc47));
2511 #endif
2512 
2513 	/*
2514 	 * I/O port 0xc6f = Audio Disable Function Register
2515 	 */
2516 
2517 #ifdef DEBUGXL
2518 	printk("Port 0xc6f (before) = %02x\n", inb(0xc6f));
2519 #endif
2520 
2521 	outb((0x80), 0xc6f);
2522 
2523 #ifdef DEBUGXL
2524 	printk("Port 0xc6f (after) = %02x\n", inb(0xc6f));
2525 #endif
2526 
2527 	return 1;
2528 }
2529 
probe_ms_sound(struct address_info * hw_config)2530 int probe_ms_sound(struct address_info *hw_config)
2531 {
2532 	unsigned char   tmp;
2533 
2534 	DDB(printk("Entered probe_ms_sound(%x, %d)\n", hw_config->io_base, hw_config->card_subtype));
2535 
2536 	if (check_region(hw_config->io_base, 8))
2537 	{
2538 		printk(KERN_ERR "MSS: I/O port conflict\n");
2539 		return 0;
2540 	}
2541 	if (hw_config->card_subtype == 1)	/* Has no IRQ/DMA registers */
2542 	{
2543 		/* check_opl3(0x388, hw_config); */
2544 		return ad1848_detect(hw_config->io_base + 4, NULL, hw_config->osp);
2545 	}
2546 
2547 	if (deskpro_xl && hw_config->card_subtype == 2)	/* Compaq Deskpro XL */
2548 	{
2549 		if (!init_deskpro(hw_config))
2550 			return 0;
2551 	}
2552 
2553 	if (deskpro_m)	/* Compaq Deskpro M */
2554 	{
2555 		if (!init_deskpro_m(hw_config))
2556 			return 0;
2557 	}
2558 
2559 	/*
2560 	   * Check if the IO port returns valid signature. The original MS Sound
2561 	   * system returns 0x04 while some cards (AudioTrix Pro for example)
2562 	   * return 0x00 or 0x0f.
2563 	 */
2564 
2565 	if ((tmp = inb(hw_config->io_base + 3)) == 0xff)	/* Bus float */
2566 	{
2567 		  int             ret;
2568 
2569 		  DDB(printk("I/O address is inactive (%x)\n", tmp));
2570 		  if (!(ret = ad1848_detect(hw_config->io_base + 4, NULL, hw_config->osp)))
2571 			  return 0;
2572 		  return 1;
2573 	}
2574 	DDB(printk("MSS signature = %x\n", tmp & 0x3f));
2575 	if ((tmp & 0x3f) != 0x04 &&
2576 	    (tmp & 0x3f) != 0x0f &&
2577 	    (tmp & 0x3f) != 0x00)
2578 	{
2579 		int ret;
2580 
2581 		MDB(printk(KERN_ERR "No MSS signature detected on port 0x%x (0x%x)\n", hw_config->io_base, (int) inb(hw_config->io_base + 3)));
2582 		DDB(printk("Trying to detect codec anyway but IRQ/DMA may not work\n"));
2583 		if (!(ret = ad1848_detect(hw_config->io_base + 4, NULL, hw_config->osp)))
2584 			return 0;
2585 
2586 		hw_config->card_subtype = 1;
2587 		return 1;
2588 	}
2589 	if ((hw_config->irq != 5)  &&
2590 	    (hw_config->irq != 7)  &&
2591 	    (hw_config->irq != 9)  &&
2592 	    (hw_config->irq != 10) &&
2593 	    (hw_config->irq != 11) &&
2594 	    (hw_config->irq != 12))
2595 	{
2596 		printk(KERN_ERR "MSS: Bad IRQ %d\n", hw_config->irq);
2597 		return 0;
2598 	}
2599 	if (hw_config->dma != 0 && hw_config->dma != 1 && hw_config->dma != 3)
2600 	{
2601 		  printk(KERN_ERR "MSS: Bad DMA %d\n", hw_config->dma);
2602 		  return 0;
2603 	}
2604 	/*
2605 	 * Check that DMA0 is not in use with a 8 bit board.
2606 	 */
2607 
2608 	if (hw_config->dma == 0 && inb(hw_config->io_base + 3) & 0x80)
2609 	{
2610 		printk(KERN_ERR "MSS: Can't use DMA0 with a 8 bit card/slot\n");
2611 		return 0;
2612 	}
2613 	if (hw_config->irq > 7 && hw_config->irq != 9 && inb(hw_config->io_base + 3) & 0x80)
2614 	{
2615 		printk(KERN_ERR "MSS: Can't use IRQ%d with a 8 bit card/slot\n", hw_config->irq);
2616 		return 0;
2617 	}
2618 	return ad1848_detect(hw_config->io_base + 4, NULL, hw_config->osp);
2619 }
2620 
attach_ms_sound(struct address_info * hw_config,struct module * owner)2621 void attach_ms_sound(struct address_info *hw_config, struct module *owner)
2622 {
2623 	static signed char interrupt_bits[12] =
2624 	{
2625 		-1, -1, -1, -1, -1, 0x00, -1, 0x08, -1, 0x10, 0x18, 0x20
2626 	};
2627 	signed char     bits;
2628 	char            dma2_bit = 0;
2629 
2630 	static char     dma_bits[4] =
2631 	{
2632 		1, 2, 0, 3
2633 	};
2634 
2635 	int config_port = hw_config->io_base + 0;
2636 	int version_port = hw_config->io_base + 3;
2637 	int dma = hw_config->dma;
2638 	int dma2 = hw_config->dma2;
2639 
2640 	if (hw_config->card_subtype == 1)	/* Has no IRQ/DMA registers */
2641 	{
2642 		hw_config->slots[0] = ad1848_init("MS Sound System", hw_config->io_base + 4,
2643 						    hw_config->irq,
2644 						    hw_config->dma,
2645 						    hw_config->dma2, 0,
2646 						    hw_config->osp,
2647 						    owner);
2648 		request_region(hw_config->io_base, 4, "WSS config");
2649 		return;
2650 	}
2651 	/*
2652 	 * Set the IRQ and DMA addresses.
2653 	 */
2654 
2655 	bits = interrupt_bits[hw_config->irq];
2656 	if (bits == -1)
2657 	{
2658 		printk(KERN_ERR "MSS: Bad IRQ %d\n", hw_config->irq);
2659 		return;
2660 	}
2661 	outb((bits | 0x40), config_port);
2662 	if ((inb(version_port) & 0x40) == 0)
2663 		printk(KERN_ERR "[MSS: IRQ Conflict?]\n");
2664 
2665 /*
2666  * Handle the capture DMA channel
2667  */
2668 
2669 	if (dma2 != -1 && dma2 != dma)
2670 	{
2671 		if (!((dma == 0 && dma2 == 1) ||
2672 			(dma == 1 && dma2 == 0) ||
2673 			(dma == 3 && dma2 == 0)))
2674 		{	/* Unsupported combination. Try to swap channels */
2675 			int tmp = dma;
2676 
2677 			dma = dma2;
2678 			dma2 = tmp;
2679 		}
2680 		if ((dma == 0 && dma2 == 1) ||
2681 			(dma == 1 && dma2 == 0) ||
2682 			(dma == 3 && dma2 == 0))
2683 		{
2684 			dma2_bit = 0x04;	/* Enable capture DMA */
2685 		}
2686 		else
2687 		{
2688 			printk(KERN_WARNING "MSS: Invalid capture DMA\n");
2689 			dma2 = dma;
2690 		}
2691 	}
2692 	else
2693 	{
2694 		dma2 = dma;
2695 	}
2696 
2697 	hw_config->dma = dma;
2698 	hw_config->dma2 = dma2;
2699 
2700 	outb((bits | dma_bits[dma] | dma2_bit), config_port);	/* Write IRQ+DMA setup */
2701 
2702 	hw_config->slots[0] = ad1848_init("MS Sound System", hw_config->io_base + 4,
2703 					  hw_config->irq,
2704 					  dma, dma2, 0,
2705 					  hw_config->osp,
2706 					  THIS_MODULE);
2707 	request_region(hw_config->io_base, 4, "WSS config");
2708 }
2709 
unload_ms_sound(struct address_info * hw_config)2710 void unload_ms_sound(struct address_info *hw_config)
2711 {
2712 	ad1848_unload(hw_config->io_base + 4,
2713 		      hw_config->irq,
2714 		      hw_config->dma,
2715 		      hw_config->dma2, 0);
2716 	sound_unload_audiodev(hw_config->slots[0]);
2717 	release_region(hw_config->io_base, 4);
2718 }
2719 
2720 #ifndef EXCLUDE_TIMERS
2721 
2722 /*
2723  * Timer stuff (for /dev/music).
2724  */
2725 
2726 static unsigned int current_interval = 0;
2727 
ad1848_tmr_start(int dev,unsigned int usecs)2728 static unsigned int ad1848_tmr_start(int dev, unsigned int usecs)
2729 {
2730 	unsigned long   flags;
2731 	ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
2732 	unsigned long   xtal_nsecs;	/* nanoseconds per xtal oscillator tick */
2733 	unsigned long   divider;
2734 
2735 	save_flags(flags);
2736 	cli();
2737 
2738 	/*
2739 	 * Length of the timer interval (in nanoseconds) depends on the
2740 	 * selected crystal oscillator. Check this from bit 0x01 of I8.
2741 	 *
2742 	 * AD1845 has just one oscillator which has cycle time of 10.050 us
2743 	 * (when a 24.576 MHz xtal oscillator is used).
2744 	 *
2745 	 * Convert requested interval to nanoseconds before computing
2746 	 * the timer divider.
2747 	 */
2748 
2749 	if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE)
2750 		xtal_nsecs = 10050;
2751 	else if (ad_read(devc, 8) & 0x01)
2752 		xtal_nsecs = 9920;
2753 	else
2754 		xtal_nsecs = 9969;
2755 
2756 	divider = (usecs * 1000 + xtal_nsecs / 2) / xtal_nsecs;
2757 
2758 	if (divider < 100)	/* Don't allow shorter intervals than about 1ms */
2759 		divider = 100;
2760 
2761 	if (divider > 65535)	/* Overflow check */
2762 		divider = 65535;
2763 
2764 	ad_write(devc, 21, (divider >> 8) & 0xff);	/* Set upper bits */
2765 	ad_write(devc, 20, divider & 0xff);	/* Set lower bits */
2766 	ad_write(devc, 16, ad_read(devc, 16) | 0x40);	/* Start the timer */
2767 	devc->timer_running = 1;
2768 	restore_flags(flags);
2769 
2770 	return current_interval = (divider * xtal_nsecs + 500) / 1000;
2771 }
2772 
ad1848_tmr_reprogram(int dev)2773 static void ad1848_tmr_reprogram(int dev)
2774 {
2775 	/*
2776 	 *    Audio driver has changed sampling rate so that a different xtal
2777 	 *      oscillator was selected. We have to reprogram the timer rate.
2778 	 */
2779 
2780 	ad1848_tmr_start(dev, current_interval);
2781 	sound_timer_syncinterval(current_interval);
2782 }
2783 
ad1848_tmr_disable(int dev)2784 static void ad1848_tmr_disable(int dev)
2785 {
2786 	unsigned long   flags;
2787 	ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
2788 
2789 	save_flags(flags);
2790 	cli();
2791 	ad_write(devc, 16, ad_read(devc, 16) & ~0x40);
2792 	devc->timer_running = 0;
2793 	restore_flags(flags);
2794 }
2795 
ad1848_tmr_restart(int dev)2796 static void ad1848_tmr_restart(int dev)
2797 {
2798 	unsigned long   flags;
2799 	ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
2800 
2801 	if (current_interval == 0)
2802 		return;
2803 
2804 	save_flags(flags);
2805 	cli();
2806 	ad_write(devc, 16, ad_read(devc, 16) | 0x40);
2807 	devc->timer_running = 1;
2808 	restore_flags(flags);
2809 }
2810 
2811 static struct sound_lowlev_timer ad1848_tmr =
2812 {
2813 	0,
2814 	2,
2815 	ad1848_tmr_start,
2816 	ad1848_tmr_disable,
2817 	ad1848_tmr_restart
2818 };
2819 
ad1848_tmr_install(int dev)2820 static int ad1848_tmr_install(int dev)
2821 {
2822 	if (timer_installed != -1)
2823 		return 0;	/* Don't install another timer */
2824 
2825 	timer_installed = ad1848_tmr.dev = dev;
2826 	sound_timer_init(&ad1848_tmr, audio_devs[dev]->name);
2827 
2828 	return 1;
2829 }
2830 #endif /* EXCLUDE_TIMERS */
2831 
ad1848_suspend(ad1848_info * devc)2832 static int ad1848_suspend(ad1848_info *devc)
2833 {
2834 	unsigned long flags;
2835 
2836 	save_flags(flags);
2837 	cli();
2838 
2839 	ad_mute(devc);
2840 
2841 	restore_flags(flags);
2842 	return 0;
2843 }
2844 
ad1848_resume(ad1848_info * devc)2845 static int ad1848_resume(ad1848_info *devc)
2846 {
2847 	unsigned long flags;
2848 	int mixer_levels[32], i;
2849 
2850 	save_flags(flags);
2851 	cli();
2852 
2853 	/* Thinkpad is a bit more of PITA than normal. The BIOS tends to
2854 	   restore it in a different config to the one we use.  Need to
2855 	   fix this somehow */
2856 
2857 	/* store old mixer levels */
2858 	memcpy(mixer_levels, devc->levels, sizeof (mixer_levels));
2859 	ad1848_init_hw(devc);
2860 
2861 	/* restore mixer levels */
2862 	for (i = 0; i < 32; i++)
2863 		ad1848_mixer_set(devc, devc->dev_no, mixer_levels[i]);
2864 
2865 	if (!devc->subtype) {
2866 		static signed char interrupt_bits[12] = { -1, -1, -1, -1, -1, 0x00, -1, 0x08, -1, 0x10, 0x18, 0x20 };
2867 		static char dma_bits[4] = { 1, 2, 0, 3 };
2868 
2869 		signed char bits;
2870 		char dma2_bit = 0;
2871 
2872 		int config_port = devc->base + 0;
2873 
2874 		bits = interrupt_bits[devc->irq];
2875 		if (bits == -1) {
2876 			printk(KERN_ERR "MSS: Bad IRQ %d\n", devc->irq);
2877 			restore_flags(flags);
2878 			return -1;
2879 		}
2880 
2881 		outb((bits | 0x40), config_port);
2882 
2883 		if (devc->dma2 != -1 && devc->dma2 != devc->dma1)
2884 			if ( (devc->dma1 == 0 && devc->dma2 == 1) ||
2885 			     (devc->dma1 == 1 && devc->dma2 == 0) ||
2886 			     (devc->dma1 == 3 && devc->dma2 == 0))
2887 				dma2_bit = 0x04;
2888 
2889 		outb((bits | dma_bits[devc->dma1] | dma2_bit), config_port);
2890 	}
2891 
2892 	restore_flags(flags);
2893       	return 0;
2894 }
2895 
ad1848_pm_callback(struct pm_dev * dev,pm_request_t rqst,void * data)2896 static int ad1848_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data)
2897 {
2898 	ad1848_info *devc = dev->data;
2899 	if (devc) {
2900 		DEB(printk("ad1848: pm event received: 0x%x\n", rqst));
2901 
2902 		switch (rqst) {
2903 		case PM_SUSPEND:
2904 			ad1848_suspend(devc);
2905 			break;
2906 		case PM_RESUME:
2907 			ad1848_resume(devc);
2908 			break;
2909 		}
2910 	}
2911 	return 0;
2912 }
2913 
2914 
2915 EXPORT_SYMBOL(ad1848_detect);
2916 EXPORT_SYMBOL(ad1848_init);
2917 EXPORT_SYMBOL(ad1848_unload);
2918 EXPORT_SYMBOL(ad1848_control);
2919 EXPORT_SYMBOL(adintr);
2920 EXPORT_SYMBOL(probe_ms_sound);
2921 EXPORT_SYMBOL(attach_ms_sound);
2922 EXPORT_SYMBOL(unload_ms_sound);
2923 
2924 static int __initdata io = -1;
2925 static int __initdata irq = -1;
2926 static int __initdata dma = -1;
2927 static int __initdata dma2 = -1;
2928 static int __initdata type = 0;
2929 
2930 MODULE_PARM(io, "i");                   /* I/O for a raw AD1848 card */
2931 MODULE_PARM(irq, "i");                  /* IRQ to use */
2932 MODULE_PARM(dma, "i");                  /* First DMA channel */
2933 MODULE_PARM(dma2, "i");                 /* Second DMA channel */
2934 MODULE_PARM(type, "i");                 /* Card type */
2935 MODULE_PARM(deskpro_xl, "i");           /* Special magic for Deskpro XL boxen */
2936 MODULE_PARM(deskpro_m, "i");            /* Special magic for Deskpro M box */
2937 MODULE_PARM(soundpro, "i");             /* More special magic for SoundPro chips */
2938 
2939 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
2940 MODULE_PARM(isapnp,	"i");
2941 MODULE_PARM(isapnpjump,	"i");
2942 MODULE_PARM(reverse,	"i");
2943 MODULE_PARM_DESC(isapnp,	"When set to 0, Plug & Play support will be disabled");
2944 MODULE_PARM_DESC(isapnpjump,	"Jumps to a specific slot in the driver's PnP table. Use the source, Luke.");
2945 MODULE_PARM_DESC(reverse,	"When set to 1, will reverse ISAPnP search order");
2946 
2947 struct pci_dev	*ad1848_dev  = NULL;
2948 
2949 /* Please add new entries at the end of the table */
2950 static struct {
2951 	char *name;
2952 	unsigned short	card_vendor, card_device,
2953 			vendor, function;
2954 	short mss_io, irq, dma, dma2;   /* index into isapnp table */
2955         int type;
2956 } ad1848_isapnp_list[] __initdata = {
2957 	{"CMI 8330 SoundPRO",
2958 		ISAPNP_VENDOR('C','M','I'), ISAPNP_DEVICE(0x0001),
2959 		ISAPNP_VENDOR('@','@','@'), ISAPNP_FUNCTION(0x0001),
2960 		0, 0, 0,-1, 0},
2961         {"CS4232 based card",
2962                 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2963 		ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0000),
2964 		0, 0, 0, 1, 0},
2965         {"CS4232 based card",
2966                 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2967 		ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0100),
2968 		0, 0, 0, 1, 0},
2969         {"OPL3-SA2 WSS mode",
2970         	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2971 		ISAPNP_VENDOR('Y','M','H'), ISAPNP_FUNCTION(0x0021),
2972                 1, 0, 0, 1, 1},
2973 	{"AZT1008",
2974 		ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2975 		ISAPNP_VENDOR('A','Z','T'), ISAPNP_FUNCTION(0x0001),
2976 		1, 0, 0, 1, 1},
2977 	{0}
2978 };
2979 
2980 static struct isapnp_device_id id_table[] __devinitdata = {
2981 	{	ISAPNP_VENDOR('C','M','I'), ISAPNP_DEVICE(0x0001),
2982 		ISAPNP_VENDOR('@','@','@'), ISAPNP_FUNCTION(0x0001), 0 },
2983         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2984 		ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0000), 0 },
2985         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2986 		ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0100), 0 },
2987         {       ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2988 		ISAPNP_VENDOR('Y','M','H'), ISAPNP_FUNCTION(0x0021), 0 },
2989 	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2990 		ISAPNP_VENDOR('A','Z','T'), ISAPNP_FUNCTION(0x0001), 0 },
2991 	{0}
2992 };
2993 
2994 MODULE_DEVICE_TABLE(isapnp, id_table);
2995 
activate_dev(char * devname,char * resname,struct pci_dev * dev)2996 static struct pci_dev *activate_dev(char *devname, char *resname, struct pci_dev *dev)
2997 {
2998 	int err;
2999 
3000 	/* Device already active? Let's use it */
3001 	if(dev->active)
3002 		return(dev);
3003 
3004 	if((err = dev->activate(dev)) < 0) {
3005 		printk(KERN_ERR "ad1848: %s %s config failed (out of resources?)[%d]\n", devname, resname, err);
3006 
3007 		dev->deactivate(dev);
3008 
3009 		return(NULL);
3010 	}
3011 	return(dev);
3012 }
3013 
ad1848_init_generic(struct pci_dev * dev,struct address_info * hw_config,int slot)3014 static struct pci_dev *ad1848_init_generic(struct pci_dev *dev, struct address_info *hw_config, int slot)
3015 {
3016 	/* Configure Audio device, point ad1848_dev to device found */
3017 	if((ad1848_dev = dev))
3018 	{
3019 		int ret;
3020 		ret = ad1848_dev->prepare(ad1848_dev);
3021 		/* If device is active, assume configured with /proc/isapnp
3022 		 * and use anyway. Some other way to check this? */
3023 		if(ret && ret != -EBUSY) {
3024 			printk(KERN_ERR "ad1848: ISAPnP found device that could not be autoconfigured.\n");
3025 			return(NULL);
3026 		}
3027 		if(ret == -EBUSY)
3028 			audio_activated = 1;
3029 
3030 		if((ad1848_dev = activate_dev(ad1848_isapnp_list[slot].name, "ad1848", ad1848_dev)))
3031 		{
3032 			hw_config->io_base 	= ad1848_dev->resource[ad1848_isapnp_list[slot].mss_io].start;
3033 			hw_config->irq 		= ad1848_dev->irq_resource[ad1848_isapnp_list[slot].irq].start;
3034 			hw_config->dma 		= ad1848_dev->dma_resource[ad1848_isapnp_list[slot].dma].start;
3035 			if(ad1848_isapnp_list[slot].dma2 != -1)
3036 				hw_config->dma2 = ad1848_dev->dma_resource[ad1848_isapnp_list[slot].dma2].start;
3037 			else
3038 				hw_config->dma2 = -1;
3039                         hw_config->card_subtype = ad1848_isapnp_list[slot].type;
3040 		} else
3041 			return(NULL);
3042 	} else
3043 		return(NULL);
3044 
3045 	return(ad1848_dev);
3046 }
3047 
ad1848_isapnp_init(struct address_info * hw_config,struct pci_dev * dev,int slot)3048 static int __init ad1848_isapnp_init(struct address_info *hw_config, struct pci_dev *dev, int slot)
3049 {
3050 	char *devname = dev->name[0] ? dev->name : ad1848_isapnp_list[slot].name;
3051 
3052 	printk(KERN_INFO "ad1848: %s detected\n", devname);
3053 
3054 	/* Initialize this baby. */
3055 
3056 	if(ad1848_init_generic(dev, hw_config, slot)) {
3057 		/* We got it. */
3058 
3059 		printk(KERN_NOTICE "ad1848: ISAPnP reports '%s' at i/o %#x, irq %d, dma %d, %d\n",
3060 		       devname,
3061 		       hw_config->io_base, hw_config->irq, hw_config->dma,
3062 		       hw_config->dma2);
3063 		return 1;
3064 	}
3065 	else
3066 		printk(KERN_INFO "ad1848: Failed to initialize %s\n", devname);
3067 
3068 	return 0;
3069 }
3070 
ad1848_isapnp_probe(struct address_info * hw_config)3071 static int __init ad1848_isapnp_probe(struct address_info *hw_config)
3072 {
3073 	static int first = 1;
3074 	int i;
3075 
3076 	/* Count entries in sb_isapnp_list */
3077 	for (i = 0; ad1848_isapnp_list[i].card_vendor != 0; i++);
3078 	i--;
3079 
3080 	/* Check and adjust isapnpjump */
3081 	if( isapnpjump < 0 || isapnpjump > i) {
3082 		isapnpjump = reverse ? i : 0;
3083 		printk(KERN_ERR "ad1848: Valid range for isapnpjump is 0-%d. Adjusted to %d.\n", i, isapnpjump);
3084 	}
3085 
3086 	if(!first || !reverse)
3087 		i = isapnpjump;
3088 	first = 0;
3089 	while(ad1848_isapnp_list[i].card_vendor != 0) {
3090 		static struct pci_dev *dev = NULL;
3091 
3092 		while ((dev = isapnp_find_dev(NULL,
3093 				ad1848_isapnp_list[i].vendor,
3094 				ad1848_isapnp_list[i].function,
3095 				NULL))) {
3096 
3097 			if(ad1848_isapnp_init(hw_config, dev, i)) {
3098 				isapnpjump = i; /* start next search from here */
3099 				return 0;
3100 			}
3101 		}
3102 		i += reverse ? -1 : 1;
3103 	}
3104 
3105 	return -ENODEV;
3106 }
3107 #endif
3108 
3109 
init_ad1848(void)3110 static int __init init_ad1848(void)
3111 {
3112 	printk(KERN_INFO "ad1848/cs4248 codec driver Copyright (C) by Hannu Savolainen 1993-1996\n");
3113 
3114 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
3115 	if(isapnp && (ad1848_isapnp_probe(&cfg) < 0) ) {
3116 		printk(KERN_NOTICE "ad1848: No ISAPnP cards found, trying standard ones...\n");
3117 		isapnp = 0;
3118 	}
3119 #endif
3120 
3121 	if(io != -1) {
3122 	        if( isapnp == 0 )
3123 	        {
3124 			if(irq == -1 || dma == -1) {
3125 				printk(KERN_WARNING "ad1848: must give I/O , IRQ and DMA.\n");
3126 				return -EINVAL;
3127 			}
3128 
3129 			cfg.irq = irq;
3130 			cfg.io_base = io;
3131 			cfg.dma = dma;
3132 			cfg.dma2 = dma2;
3133 			cfg.card_subtype = type;
3134 	        }
3135 
3136 		if(!probe_ms_sound(&cfg))
3137 			return -ENODEV;
3138 		attach_ms_sound(&cfg, THIS_MODULE);
3139 		loaded = 1;
3140 	}
3141 	return 0;
3142 }
3143 
cleanup_ad1848(void)3144 static void __exit cleanup_ad1848(void)
3145 {
3146 	if(loaded)
3147 		unload_ms_sound(&cfg);
3148 
3149 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
3150 	if(audio_activated)
3151 		if(ad1848_dev)
3152 			ad1848_dev->deactivate(ad1848_dev);
3153 #endif
3154 }
3155 
3156 module_init(init_ad1848);
3157 module_exit(cleanup_ad1848);
3158 
3159 #ifndef MODULE
setup_ad1848(char * str)3160 static int __init setup_ad1848(char *str)
3161 {
3162         /* io, irq, dma, dma2, type */
3163 	int ints[6];
3164 
3165 	str = get_options(str, ARRAY_SIZE(ints), ints);
3166 
3167 	io	= ints[1];
3168 	irq	= ints[2];
3169 	dma	= ints[3];
3170 	dma2	= ints[4];
3171 	type	= ints[5];
3172 
3173 	return 1;
3174 }
3175 
3176 __setup("ad1848=", setup_ad1848);
3177 #endif
3178 MODULE_LICENSE("GPL");
3179