1 /*
2  *
3  * AD1816 lowlevel sound driver for Linux 2.2.0 and above
4  *
5  * Copyright (C) 1998 by Thorsten Knabe <tek@rbg.informatik.tu-darmstadt.de>
6  *
7  * Based on the CS4232/AD1848 driver Copyright (C) by Hannu Savolainen 1993-1996
8  *
9  *
10  * version: 1.3.1
11  * status: experimental
12  * date: 1999/4/18
13  *
14  * Changes:
15  *	Oleg Drokin: Some cleanup of load/unload functions.	1998/11/24
16  *
17  *	Thorsten Knabe: attach and unload rewritten,
18  *	some argument checks added				1998/11/30
19  *
20  *	Thorsten Knabe: Buggy isa bridge workaround added	1999/01/16
21  *
22  *	David Moews/Thorsten Knabe: Introduced options
23  *	parameter. Added slightly modified patch from
24  *	David Moews to disable dsp audio sources by setting
25  *	bit 0 of options parameter. This seems to be
26  *	required by some Aztech/Newcom SC-16 cards.		1999/04/18
27  *
28  *	Christoph Hellwig: Adapted to module_init/module_exit.	2000/03/03
29  *
30  *	Christoph Hellwig: Added isapnp support			2000/03/15
31  *
32  *	Arnaldo Carvalho de Melo: get rid of check_region	2001/10/07
33  */
34 
35 #include <linux/config.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/isapnp.h>
39 #include <linux/stddef.h>
40 
41 #include "sound_config.h"
42 
43 #define DEBUGNOISE(x)
44 #define DEBUGINFO(x)
45 #define DEBUGLOG(x)
46 #define DEBUGWARN(x)
47 
48 #define CHECK_FOR_POWER { int timeout=100; \
49   while (timeout > 0 && (inb(devc->base)&0x80)!= 0x80) {\
50           timeout--; \
51   } \
52   if (timeout==0) {\
53           printk(KERN_WARNING "ad1816: Check for power failed in %s line: %d\n",__FILE__,__LINE__); \
54   } \
55 }
56 
57 /* structure to hold device specific information */
58 typedef struct
59 {
60         int            base;          /* set in attach */
61 	int            irq;
62 	int            dma_playback;
63         int            dma_capture;
64 
65         int            speed;         /* open */
66 	int            channels;
67 	int            audio_format;
68 	unsigned char  format_bits;
69         int            audio_mode;
70 	int            opened;
71 
72         int            recmask;        /* setup */
73 	int            supported_devices;
74 	int            supported_rec_devices;
75 	unsigned short levels[SOUND_MIXER_NRDEVICES];
76         int            dev_no;   /* this is the # in audio_devs and NOT
77 				    in ad1816_info */
78 	int            irq_ok;
79 	int            *osp;
80 
81 } ad1816_info;
82 
83 static int nr_ad1816_devs;
84 static int ad1816_clockfreq = 33000;
85 static int options;
86 
87 /* for backward mapping of irq to sound device */
88 
89 static volatile char irq2dev[17] = {-1, -1, -1, -1, -1, -1, -1, -1,
90 				    -1, -1, -1, -1, -1, -1, -1, -1, -1};
91 
92 
93 /* supported audio formats */
94 static int  ad_format_mask =
95 AFMT_U8 | AFMT_S16_LE | AFMT_S16_BE | AFMT_MU_LAW | AFMT_A_LAW;
96 
97 /* array of device info structures */
98 static ad1816_info dev_info[MAX_AUDIO_DEV];
99 
100 
101 /* ------------------------------------------------------------------- */
102 
103 /* functions for easier access to inderect registers */
104 
ad_read(ad1816_info * devc,int reg)105 static int ad_read (ad1816_info * devc, int reg)
106 {
107 	unsigned long   flags;
108 	int result;
109 
110 	CHECK_FOR_POWER;
111 
112 	save_flags (flags); /* make register access atomic */
113 	cli ();
114 	outb ((unsigned char) (reg & 0x3f), devc->base+0);
115 	result = inb(devc->base+2);
116 	result+= inb(devc->base+3)<<8;
117 	restore_flags (flags);
118 
119 	return (result);
120 }
121 
122 
ad_write(ad1816_info * devc,int reg,int data)123 static void ad_write (ad1816_info * devc, int reg, int data)
124 {
125 	unsigned long flags;
126 
127 	CHECK_FOR_POWER;
128 
129 	save_flags (flags); /* make register access atomic */
130 	cli ();
131 	outb ((unsigned char) (reg & 0xff), devc->base+0);
132 	outb ((unsigned char) (data & 0xff),devc->base+2);
133 	outb ((unsigned char) ((data>>8)&0xff),devc->base+3);
134 	restore_flags (flags);
135 
136 }
137 
138 /* ------------------------------------------------------------------- */
139 
140 /* function interface required by struct audio_driver */
141 
ad1816_halt_input(int dev)142 static void ad1816_halt_input (int dev)
143 {
144 	unsigned long flags;
145 	ad1816_info    *devc = (ad1816_info *) audio_devs[dev]->devc;
146 	unsigned char buffer;
147 
148 	DEBUGINFO (printk("ad1816: halt_input called\n"));
149 
150 	save_flags (flags);
151 	cli ();
152 
153 	if(!isa_dma_bridge_buggy) {
154 	        disable_dma(audio_devs[dev]->dmap_in->dma);
155 	}
156 
157 	buffer=inb(devc->base+9);
158 	if (buffer & 0x01) {
159 		/* disable capture */
160 		outb(buffer & ~0x01,devc->base+9);
161 	}
162 
163 	if(!isa_dma_bridge_buggy) {
164 	        enable_dma(audio_devs[dev]->dmap_in->dma);
165 	}
166 
167 	/* Clear interrupt status */
168 	outb (~0x40, devc->base+1);
169 
170 	devc->audio_mode &= ~PCM_ENABLE_INPUT;
171 	restore_flags (flags);
172 }
173 
ad1816_halt_output(int dev)174 static void ad1816_halt_output (int dev)
175 {
176 	unsigned long  flags;
177 	ad1816_info    *devc = (ad1816_info *) audio_devs[dev]->devc;
178 
179 	unsigned char buffer;
180 
181 	DEBUGINFO (printk("ad1816: halt_output called!\n"));
182 
183 	save_flags (flags);
184 	cli ();
185 	/* Mute pcm output */
186 	ad_write(devc, 4, ad_read(devc,4)|0x8080);
187 
188 	if(!isa_dma_bridge_buggy) {
189 	        disable_dma(audio_devs[dev]->dmap_out->dma);
190 	}
191 
192 	buffer=inb(devc->base+8);
193 	if (buffer & 0x01) {
194 		/* disable capture */
195 		outb(buffer & ~0x01,devc->base+8);
196 	}
197 
198 	if(!isa_dma_bridge_buggy) {
199 	        enable_dma(audio_devs[dev]->dmap_out->dma);
200 	}
201 
202 	/* Clear interrupt status */
203 	outb ((unsigned char)~0x80, devc->base+1);
204 
205 	devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
206 	restore_flags (flags);
207 }
208 
ad1816_output_block(int dev,unsigned long buf,int count,int intrflag)209 static void ad1816_output_block (int dev, unsigned long buf,
210 				 int count, int intrflag)
211 {
212 	unsigned long flags;
213 	unsigned long cnt;
214 	ad1816_info    *devc = (ad1816_info *) audio_devs[dev]->devc;
215 
216 	DEBUGINFO(printk("ad1816: output_block called buf=%ld count=%d flags=%d\n",buf,count,intrflag));
217 
218 	cnt = count/4 - 1;
219 
220 	save_flags (flags);
221 	cli ();
222 
223 	/* set transfer count */
224 	ad_write (devc, 8, cnt & 0xffff);
225 
226 	devc->audio_mode |= PCM_ENABLE_OUTPUT;
227 	restore_flags (flags);
228 }
229 
230 
ad1816_start_input(int dev,unsigned long buf,int count,int intrflag)231 static void ad1816_start_input (int dev, unsigned long buf, int count,
232 				int intrflag)
233 {
234 	unsigned long flags;
235 	unsigned long  cnt;
236 	ad1816_info    *devc = (ad1816_info *) audio_devs[dev]->devc;
237 
238 	DEBUGINFO(printk("ad1816: start_input called buf=%ld count=%d flags=%d\n",buf,count,intrflag));
239 
240 	cnt = count/4 - 1;
241 
242 	save_flags (flags); /* make register access atomic */
243 	cli ();
244 
245 	/* set transfer count */
246 	ad_write (devc, 10, cnt & 0xffff);
247 
248 	devc->audio_mode |= PCM_ENABLE_INPUT;
249 	restore_flags (flags);
250 }
251 
ad1816_prepare_for_input(int dev,int bsize,int bcount)252 static int ad1816_prepare_for_input (int dev, int bsize, int bcount)
253 {
254 	unsigned long flags;
255 	unsigned int freq;
256 	ad1816_info    *devc = (ad1816_info *) audio_devs[dev]->devc;
257 	unsigned char fmt_bits;
258 
259 	DEBUGINFO (printk ("ad1816: prepare_for_input called: bsize=%d bcount=%d\n",bsize,bcount));
260 
261 	save_flags (flags);
262 	cli ();
263 
264 	fmt_bits= (devc->format_bits&0x7)<<3;
265 
266 	/* set mono/stereo mode */
267 	if (devc->channels > 1) {
268 		fmt_bits |=0x4;
269 	}
270 
271 	/* set Mono/Stereo in playback/capture register */
272 	outb( (inb(devc->base+8) & ~0x3C)|fmt_bits, devc->base+8);
273 	outb( (inb(devc->base+9) & ~0x3C)|fmt_bits, devc->base+9);
274 
275 	/* If compiled into kernel, AD1816_CLOCK is defined, so use it */
276 #ifdef AD1816_CLOCK
277 	ad1816_clockfreq=AD1816_CLOCK;
278 #endif
279 
280 	/* capture/playback frequency correction for soundcards
281 	   with clock chips != 33MHz (allowed range 5 - 100 kHz) */
282 
283 	if (ad1816_clockfreq<5000 || ad1816_clockfreq>100000) {
284 		ad1816_clockfreq=33000;
285 	}
286 
287 	freq=((unsigned int)devc->speed*33000)/ad1816_clockfreq;
288 
289 	/* write playback/capture speeds */
290 	ad_write (devc, 2, freq & 0xffff);
291 	ad_write (devc, 3, freq & 0xffff);
292 
293 	restore_flags (flags);
294 
295 	ad1816_halt_input(dev);
296 	return 0;
297 }
298 
ad1816_prepare_for_output(int dev,int bsize,int bcount)299 static int ad1816_prepare_for_output (int dev, int bsize, int bcount)
300 {
301 	unsigned long flags;
302 	unsigned int freq;
303 	ad1816_info    *devc = (ad1816_info *) audio_devs[dev]->devc;
304 	unsigned char fmt_bits;
305 
306 	DEBUGINFO (printk ("ad1816: prepare_for_output called: bsize=%d bcount=%d\n",bsize,bcount));
307 
308 	save_flags (flags); /* make register access atomic */
309 	cli ();
310 
311 	fmt_bits= (devc->format_bits&0x7)<<3;
312 	/* set mono/stereo mode */
313 	if (devc->channels > 1) {
314 		fmt_bits |=0x4;
315 	}
316 
317 	/* write format bits to playback/capture registers */
318 	outb( (inb(devc->base+8) & ~0x3C)|fmt_bits, devc->base+8);
319 	outb( (inb(devc->base+9) & ~0x3C)|fmt_bits, devc->base+9);
320 
321 #ifdef AD1816_CLOCK
322 	ad1816_clockfreq=AD1816_CLOCK;
323 #endif
324 
325 	/* capture/playback frequency correction for soundcards
326 	   with clock chips != 33MHz (allowed range 5 - 100 kHz)*/
327 
328 	if (ad1816_clockfreq<5000 || ad1816_clockfreq>100000) {
329 		ad1816_clockfreq=33000;
330 	}
331 
332 	freq=((unsigned int)devc->speed*33000)/ad1816_clockfreq;
333 
334 	/* write playback/capture speeds */
335 	ad_write (devc, 2, freq & 0xffff);
336 	ad_write (devc, 3, freq & 0xffff);
337 
338 	restore_flags (flags);
339 
340 	ad1816_halt_output(dev);
341 	return 0;
342 
343 }
344 
ad1816_trigger(int dev,int state)345 static void ad1816_trigger (int dev, int state)
346 {
347 	unsigned long flags;
348 	ad1816_info    *devc = (ad1816_info *) audio_devs[dev]->devc;
349 
350 	DEBUGINFO (printk("ad1816: trigger called! (devc=%d,devc->base=%d\n", devc, devc->base));
351 
352 	/* mode may have changed */
353 
354 	save_flags (flags); /* make register access atomic */
355 	cli ();
356 
357 	/* mask out modes not specified on open call */
358 	state &= devc->audio_mode;
359 
360 	/* setup soundchip to new io-mode */
361 	if (state & PCM_ENABLE_INPUT) {
362 		/* enable capture */
363 		outb(inb(devc->base+9)|0x01, devc->base+9);
364 	} else {
365 		/* disable capture */
366 		outb(inb(devc->base+9)&~0x01, devc->base+9);
367 	}
368 
369 	if (state & PCM_ENABLE_OUTPUT) {
370 		/* enable playback */
371 		outb(inb(devc->base+8)|0x01, devc->base+8);
372 		/* unmute pcm output */
373 		ad_write(devc, 4, ad_read(devc,4)&~0x8080);
374 	} else {
375 		/* mute pcm output */
376 		ad_write(devc, 4, ad_read(devc,4)|0x8080);
377 		/* disable capture */
378 		outb(inb(devc->base+8)&~0x01, devc->base+8);
379 	}
380 	restore_flags (flags);
381 }
382 
383 
384 /* halt input & output */
ad1816_halt(int dev)385 static void ad1816_halt (int dev)
386 {
387 	ad1816_halt_input(dev);
388 	ad1816_halt_output(dev);
389 }
390 
ad1816_reset(int dev)391 static void ad1816_reset (int dev)
392 {
393 	ad1816_halt (dev);
394 }
395 
396 /* set playback speed */
ad1816_set_speed(int dev,int arg)397 static int ad1816_set_speed (int dev, int arg)
398 {
399 	ad1816_info    *devc = (ad1816_info *) audio_devs[dev]->devc;
400 
401 	if (arg == 0) {
402 		return devc->speed;
403 	}
404 	/* range checking */
405 	if (arg < 4000) {
406 		arg = 4000;
407 	}
408 	if (arg > 55000) {
409 		arg = 55000;
410 	}
411 
412 	devc->speed = arg;
413 	return devc->speed;
414 
415 }
416 
ad1816_set_bits(int dev,unsigned int arg)417 static unsigned int ad1816_set_bits (int dev, unsigned int arg)
418 {
419 	ad1816_info    *devc = (ad1816_info *) audio_devs[dev]->devc;
420 
421 	static struct format_tbl {
422 		int             format;
423 		unsigned char   bits;
424 	} format2bits[] = {
425 		{ 0, 0 },
426 		{ AFMT_MU_LAW, 1 },
427 		{ AFMT_A_LAW, 3 },
428 		{ AFMT_IMA_ADPCM, 0 },
429 		{ AFMT_U8, 0 },
430 		{ AFMT_S16_LE, 2 },
431 		{ AFMT_S16_BE, 6 },
432 		{ AFMT_S8, 0 },
433 		{ AFMT_U16_LE, 0 },
434 		{ AFMT_U16_BE, 0 }
435   	};
436 
437 	int  i, n = sizeof (format2bits) / sizeof (struct format_tbl);
438 
439 	/* return current format */
440 	if (arg == 0)
441 		return devc->audio_format;
442 
443 	devc->audio_format = arg;
444 
445 	/* search matching format bits */
446 	for (i = 0; i < n; i++)
447 		if (format2bits[i].format == arg) {
448 			devc->format_bits = format2bits[i].bits;
449 			devc->audio_format = arg;
450 			return arg;
451 		}
452 
453 	/* Still hanging here. Something must be terribly wrong */
454 	devc->format_bits = 0;
455 	return devc->audio_format = AFMT_U8;
456 }
457 
ad1816_set_channels(int dev,short arg)458 static short ad1816_set_channels (int dev, short arg)
459 {
460 	ad1816_info    *devc = (ad1816_info *) audio_devs[dev]->devc;
461 
462 	if (arg != 1 && arg != 2)
463 		return devc->channels;
464 
465 	devc->channels = arg;
466 	return arg;
467 }
468 
469 /* open device */
ad1816_open(int dev,int mode)470 static int ad1816_open (int dev, int mode)
471 {
472 	ad1816_info    *devc = NULL;
473 	unsigned long   flags;
474 
475 	/* is device number valid ? */
476 	if (dev < 0 || dev >= num_audiodevs)
477 		return -(ENXIO);
478 
479 	/* get device info of this dev */
480 	devc = (ad1816_info *) audio_devs[dev]->devc;
481 
482 	/* make check if device already open atomic */
483 	save_flags (flags);
484 	cli ();
485 
486 	if (devc->opened) {
487 		restore_flags (flags);
488 		return -(EBUSY);
489 	}
490 
491 	/* mark device as open */
492 	devc->opened = 1;
493 
494 	devc->audio_mode = 0;
495 	devc->speed = 8000;
496 	devc->audio_format=AFMT_U8;
497 	devc->channels=1;
498 
499 	ad1816_reset(devc->dev_no); /* halt all pending output */
500 	restore_flags (flags);
501 	return 0;
502 }
503 
ad1816_close(int dev)504 static void ad1816_close (int dev) /* close device */
505 {
506 	unsigned long flags;
507 	ad1816_info    *devc = (ad1816_info *) audio_devs[dev]->devc;
508 
509 	save_flags (flags);
510 	cli ();
511 
512 	/* halt all pending output */
513 	ad1816_reset(devc->dev_no);
514 
515 	devc->opened = 0;
516 	devc->audio_mode = 0;
517 	devc->speed = 8000;
518 	devc->audio_format=AFMT_U8;
519 	devc->format_bits = 0;
520 
521 
522 	restore_flags (flags);
523 }
524 
525 
526 /* ------------------------------------------------------------------- */
527 
528 /* Audio driver structure */
529 
530 static struct audio_driver ad1816_audio_driver =
531 {
532 	owner:		THIS_MODULE,
533 	open:		ad1816_open,
534 	close:		ad1816_close,
535 	output_block:	ad1816_output_block,
536 	start_input:	ad1816_start_input,
537 	prepare_for_input:	ad1816_prepare_for_input,
538 	prepare_for_output:	ad1816_prepare_for_output,
539 	halt_io:		ad1816_halt,
540 	halt_input:	ad1816_halt_input,
541 	halt_output:	ad1816_halt_output,
542 	trigger:	ad1816_trigger,
543 	set_speed:	ad1816_set_speed,
544 	set_bits:	ad1816_set_bits,
545 	set_channels:	ad1816_set_channels,
546 };
547 
548 
549 /* ------------------------------------------------------------------- */
550 
551 /* Interrupt handler */
552 
553 
ad1816_interrupt(int irq,void * dev_id,struct pt_regs * dummy)554 static void ad1816_interrupt (int irq, void *dev_id, struct pt_regs *dummy)
555 {
556 	unsigned char	status;
557 	ad1816_info	*devc;
558 	int		dev;
559 	unsigned long	flags;
560 
561 
562 	if (irq < 0 || irq > 15) {
563 	        printk(KERN_WARNING "ad1816: Got bogus interrupt %d\n", irq);
564 		return;
565 	}
566 
567 	dev = irq2dev[irq];
568 
569 	if (dev < 0 || dev >= num_audiodevs) {
570 	        printk(KERN_WARNING "ad1816: IRQ2AD1816-mapping failed for "
571 				    "irq %d device %d\n", irq,dev);
572 		return;
573 	}
574 
575 	devc = (ad1816_info *) audio_devs[dev]->devc;
576 
577 	save_flags(flags);
578 	cli();
579 
580 	/* read interrupt register */
581 	status = inb (devc->base+1);
582 	/* Clear all interrupt  */
583 	outb (~status, devc->base+1);
584 
585 	DEBUGNOISE (printk("ad1816: Got interrupt subclass %d\n",status));
586 
587 	devc->irq_ok=1;
588 
589 	if (status == 0)
590 		DEBUGWARN(printk ("ad1816: interrupt: Got interrupt, but no reason?\n"));
591 
592 	if (devc->opened && (devc->audio_mode & PCM_ENABLE_INPUT) && (status&64))
593 		DMAbuf_inputintr (dev);
594 
595 	if (devc->opened && (devc->audio_mode & PCM_ENABLE_OUTPUT) && (status & 128))
596 		DMAbuf_outputintr (dev, 1);
597 
598 	restore_flags(flags);
599 }
600 
601 /* ------------------------------------------------------------------- */
602 
603 /* Mixer stuff */
604 
605 struct mixer_def {
606 	unsigned int regno: 7;
607 	unsigned int polarity:1;	/* 0=normal, 1=reversed */
608 	unsigned int bitpos:4;
609 	unsigned int nbits:4;
610 };
611 
612 static char mix_cvt[101] = {
613 	 0, 0, 3, 7,10,13,16,19,21,23,26,28,30,32,34,35,37,39,40,42,
614 	43,45,46,47,49,50,51,52,53,55,56,57,58,59,60,61,62,63,64,65,
615 	65,66,67,68,69,70,70,71,72,73,73,74,75,75,76,77,77,78,79,79,
616 	80,81,81,82,82,83,84,84,85,85,86,86,87,87,88,88,89,89,90,90,
617 	91,91,92,92,93,93,94,94,95,95,96,96,96,97,97,98,98,98,99,99,
618 	100
619 };
620 
621 typedef struct mixer_def mixer_ent;
622 
623 /*
624  * Most of the mixer entries work in backwards. Setting the polarity field
625  * makes them to work correctly.
626  *
627  * The channel numbering used by individual soundcards is not fixed. Some
628  * cards have assigned different meanings for the AUX1, AUX2 and LINE inputs.
629  * The current version doesn't try to compensate this.
630  */
631 
632 #define MIX_ENT(name, reg_l, pola_l, pos_l, len_l, reg_r, pola_r, pos_r, len_r)	\
633   {{reg_l, pola_l, pos_l, len_l}, {reg_r, pola_r, pos_r, len_r}}
634 
635 
636 mixer_ent mix_devices[SOUND_MIXER_NRDEVICES][2] = {
637 MIX_ENT(SOUND_MIXER_VOLUME,	14, 1, 8, 5,	14, 1, 0, 5),
638 MIX_ENT(SOUND_MIXER_BASS,	 0, 0, 0, 0,	 0, 0, 0, 0),
639 MIX_ENT(SOUND_MIXER_TREBLE,	 0, 0, 0, 0,	 0, 0, 0, 0),
640 MIX_ENT(SOUND_MIXER_SYNTH,	 5, 1, 8, 6,	 5, 1, 0, 6),
641 MIX_ENT(SOUND_MIXER_PCM,	 4, 1, 8, 6,	 4, 1, 0, 6),
642 MIX_ENT(SOUND_MIXER_SPEAKER,	 0, 0, 0, 0,	 0, 0, 0, 0),
643 MIX_ENT(SOUND_MIXER_LINE,	18, 1, 8, 5,	18, 1, 0, 5),
644 MIX_ENT(SOUND_MIXER_MIC,	19, 1, 8, 5,	19, 1, 0, 5),
645 MIX_ENT(SOUND_MIXER_CD,	 	15, 1, 8, 5,	15, 1, 0, 5),
646 MIX_ENT(SOUND_MIXER_IMIX,	 0, 0, 0, 0,	 0, 0, 0, 0),
647 MIX_ENT(SOUND_MIXER_ALTPCM,	 0, 0, 0, 0,	 0, 0, 0, 0),
648 MIX_ENT(SOUND_MIXER_RECLEV,	20, 0, 8, 4,	20, 0, 0, 4),
649 MIX_ENT(SOUND_MIXER_IGAIN,	 0, 0, 0, 0,	 0, 0, 0, 0),
650 MIX_ENT(SOUND_MIXER_OGAIN,	 0, 0, 0, 0,	 0, 0, 0, 0),
651 MIX_ENT(SOUND_MIXER_LINE1, 	17, 1, 8, 5,	17, 1, 0, 5),
652 MIX_ENT(SOUND_MIXER_LINE2,	16, 1, 8, 5,	16, 1, 0, 5),
653 MIX_ENT(SOUND_MIXER_LINE3,      39, 0, 9, 4,    39, 1, 0, 5)
654 };
655 
656 
657 static unsigned short default_mixer_levels[SOUND_MIXER_NRDEVICES] =
658 {
659 	0x4343,		/* Master Volume */
660 	0x3232,		/* Bass */
661 	0x3232,		/* Treble */
662 	0x0000,		/* FM */
663 	0x4343,		/* PCM */
664 	0x0000,		/* PC Speaker */
665 	0x0000,		/* Ext Line */
666 	0x0000,		/* Mic */
667 	0x0000,		/* CD */
668 	0x0000,		/* Recording monitor */
669 	0x0000,		/* SB PCM */
670 	0x0000,		/* Recording level */
671 	0x0000,		/* Input gain */
672 	0x0000,		/* Output gain */
673 	0x0000,		/* Line1 */
674 	0x0000,		/* Line2 */
675 	0x0000		/* Line3 (usually line in)*/
676 };
677 
678 #define LEFT_CHN	0
679 #define RIGHT_CHN	1
680 
681 
682 
683 static int
ad1816_set_recmask(ad1816_info * devc,int mask)684 ad1816_set_recmask (ad1816_info * devc, int mask)
685 {
686 	unsigned char   recdev;
687 	int             i, n;
688 
689 	mask &= devc->supported_rec_devices;
690 
691 	n = 0;
692 	/* Count selected device bits */
693 	for (i = 0; i < 32; i++)
694 		if (mask & (1 << i))
695 			n++;
696 
697 	if (n == 0)
698 		mask = SOUND_MASK_MIC;
699 	else if (n != 1) { /* Too many devices selected */
700 		/* Filter out active settings */
701 		mask &= ~devc->recmask;
702 
703 		n = 0;
704 		/* Count selected device bits */
705 		for (i = 0; i < 32; i++)
706 			if (mask & (1 << i))
707 				n++;
708 
709 		if (n != 1)
710 			mask = SOUND_MASK_MIC;
711 	}
712 
713 	switch (mask) {
714 	case SOUND_MASK_MIC:
715 		recdev = 5;
716 		break;
717 
718 	case SOUND_MASK_LINE:
719 		recdev = 0;
720 		break;
721 
722 	case SOUND_MASK_CD:
723 		recdev = 2;
724 		break;
725 
726 	case SOUND_MASK_LINE1:
727 		recdev = 4;
728 		break;
729 
730 	case SOUND_MASK_LINE2:
731 		recdev = 3;
732 		break;
733 
734 	case SOUND_MASK_VOLUME:
735 		recdev = 1;
736 		break;
737 
738 	default:
739 		mask = SOUND_MASK_MIC;
740 		recdev = 5;
741 	}
742 
743 	recdev <<= 4;
744 	ad_write (devc, 20,
745 		  (ad_read (devc, 20) & 0x8f8f) | recdev | (recdev<<8));
746 
747 	devc->recmask = mask;
748 	return mask;
749 }
750 
751 static void
change_bits(int * regval,int dev,int chn,int newval)752 change_bits (int *regval, int dev, int chn, int newval)
753 {
754 	unsigned char   mask;
755 	int             shift;
756 
757 	/* Reverse polarity*/
758 
759 	if (mix_devices[dev][chn].polarity == 1)
760 		newval = 100 - newval;
761 
762 	mask = (1 << mix_devices[dev][chn].nbits) - 1;
763 	shift = mix_devices[dev][chn].bitpos;
764 	/* Scale it */
765 	newval = (int) ((newval * mask) + 50) / 100;
766 	/* Clear bits */
767 	*regval &= ~(mask << shift);
768 	/* Set new value */
769 	*regval |= (newval & mask) << shift;
770 }
771 
772 static int
ad1816_mixer_get(ad1816_info * devc,int dev)773 ad1816_mixer_get (ad1816_info * devc, int dev)
774 {
775 	DEBUGINFO(printk("ad1816: mixer_get called!\n"));
776 
777 	/* range check + supported mixer check */
778 	if (dev < 0 || dev >= SOUND_MIXER_NRDEVICES )
779 	        return (-(EINVAL));
780 	if (!((1 << dev) & devc->supported_devices))
781 		return -(EINVAL);
782 
783 	return devc->levels[dev];
784 }
785 
786 static int
ad1816_mixer_set(ad1816_info * devc,int dev,int value)787 ad1816_mixer_set (ad1816_info * devc, int dev, int value)
788 {
789 	int   left = value & 0x000000ff;
790 	int   right = (value & 0x0000ff00) >> 8;
791 	int   retvol;
792 
793 	int   regoffs;
794 	int   val;
795 	int   valmute;
796 
797 	DEBUGINFO(printk("ad1816: mixer_set called!\n"));
798 
799 	if (dev < 0 || dev >= SOUND_MIXER_NRDEVICES )
800 		return -(EINVAL);
801 
802 	if (left > 100)
803 		left = 100;
804 	if (left < 0)
805 		left = 0;
806 	if (right > 100)
807 		right = 100;
808 	if (right < 0)
809 		right = 0;
810 
811 	/* Mono control */
812 	if (mix_devices[dev][RIGHT_CHN].nbits == 0)
813 		right = left;
814 	retvol = left | (right << 8);
815 
816 	/* Scale it */
817 
818 	left = mix_cvt[left];
819 	right = mix_cvt[right];
820 
821 	/* reject all mixers that are not supported */
822 	if (!(devc->supported_devices & (1 << dev)))
823 		return -(EINVAL);
824 
825 	/* sanity check */
826 	if (mix_devices[dev][LEFT_CHN].nbits == 0)
827 		return -(EINVAL);
828 
829 	/* keep precise volume internal */
830 	devc->levels[dev] = retvol;
831 
832 	/* Set the left channel */
833 	regoffs = mix_devices[dev][LEFT_CHN].regno;
834 	val = ad_read (devc, regoffs);
835 	change_bits (&val, dev, LEFT_CHN, left);
836 
837 	valmute=val;
838 
839 	/* Mute bit masking on some registers */
840 	if ( regoffs==5 || regoffs==14 || regoffs==15 ||
841 	     regoffs==16 || regoffs==17 || regoffs==18 ||
842 	     regoffs==19 || regoffs==39) {
843 		if (left==0)
844 			valmute |= 0x8000;
845 		else
846 			valmute &= ~0x8000;
847 	}
848 	ad_write (devc, regoffs, valmute); /* mute */
849 
850 	/*
851 	 * Set the right channel
852 	 */
853 
854 	/* Was just a mono channel */
855 	if (mix_devices[dev][RIGHT_CHN].nbits == 0)
856 		return retvol;
857 
858 	regoffs = mix_devices[dev][RIGHT_CHN].regno;
859 	val = ad_read (devc, regoffs);
860 	change_bits (&val, dev, RIGHT_CHN, right);
861 
862 	valmute=val;
863 	if ( regoffs==5 || regoffs==14 || regoffs==15 ||
864 	     regoffs==16 || regoffs==17 || regoffs==18 ||
865 	     regoffs==19 || regoffs==39) {
866 		if (right==0)
867 			valmute |= 0x80;
868 		else
869 			valmute &= ~0x80;
870 	}
871 	ad_write (devc, regoffs, valmute); /* mute */
872 
873        	return retvol;
874 }
875 
876 #define MIXER_DEVICES ( SOUND_MASK_VOLUME | \
877 			SOUND_MASK_SYNTH | \
878 			SOUND_MASK_PCM | \
879 			SOUND_MASK_LINE | \
880 			SOUND_MASK_LINE1 | \
881 			SOUND_MASK_LINE2 | \
882 			SOUND_MASK_LINE3 | \
883 			SOUND_MASK_MIC | \
884 			SOUND_MASK_CD | \
885 			SOUND_MASK_RECLEV  \
886 			)
887 #define REC_DEVICES ( SOUND_MASK_LINE2 |\
888 		      SOUND_MASK_LINE |\
889 		      SOUND_MASK_LINE1 |\
890 		      SOUND_MASK_MIC |\
891 		      SOUND_MASK_CD |\
892 		      SOUND_MASK_VOLUME \
893 		      )
894 
895 static void
ad1816_mixer_reset(ad1816_info * devc)896 ad1816_mixer_reset (ad1816_info * devc)
897 {
898 	int  i;
899 
900 	devc->supported_devices = MIXER_DEVICES;
901 
902 	devc->supported_rec_devices = REC_DEVICES;
903 
904 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
905 		if (devc->supported_devices & (1 << i))
906 			ad1816_mixer_set (devc, i, default_mixer_levels[i]);
907 	ad1816_set_recmask (devc, SOUND_MASK_MIC);
908 }
909 
910 static int
ad1816_mixer_ioctl(int dev,unsigned int cmd,caddr_t arg)911 ad1816_mixer_ioctl (int dev, unsigned int cmd, caddr_t arg)
912 {
913 	ad1816_info    *devc = mixer_devs[dev]->devc;
914 	int val;
915 
916 	DEBUGINFO(printk("ad1816: mixer_ioctl called!\n"));
917 
918 	/* Mixer ioctl */
919 	if (((cmd >> 8) & 0xff) == 'M') {
920 
921 		/* set ioctl */
922 		if (_SIOC_DIR (cmd) & _SIOC_WRITE) {
923 			switch (cmd & 0xff){
924 			case SOUND_MIXER_RECSRC:
925 
926 				if (get_user(val, (int *)arg))
927 					return -EFAULT;
928 				val=ad1816_set_recmask (devc, val);
929 				return put_user(val, (int *)arg);
930 				break;
931 
932 			default:
933 				if (get_user(val, (int *)arg))
934 					return -EFAULT;
935 				if ((val=ad1816_mixer_set (devc, cmd & 0xff, val))<0)
936 				        return val;
937 				else
938 				        return put_user(val, (int *)arg);
939 			}
940 		} else {
941 			/* read ioctl */
942 			switch (cmd & 0xff) {
943 
944 			case SOUND_MIXER_RECSRC:
945 				val=devc->recmask;
946 				return put_user(val, (int *)arg);
947 				break;
948 
949 			case SOUND_MIXER_DEVMASK:
950 				val=devc->supported_devices;
951 				return put_user(val, (int *)arg);
952 				break;
953 
954 			case SOUND_MIXER_STEREODEVS:
955 				val=devc->supported_devices & ~(SOUND_MASK_SPEAKER | SOUND_MASK_IMIX);
956 				return put_user(val, (int *)arg);
957 				break;
958 
959 			case SOUND_MIXER_RECMASK:
960 				val=devc->supported_rec_devices;
961 				return put_user(val, (int *)arg);
962 				break;
963 
964 			case SOUND_MIXER_CAPS:
965 				val=SOUND_CAP_EXCL_INPUT;
966 				return put_user(val, (int *)arg);
967 				break;
968 
969 			default:
970 			        if ((val=ad1816_mixer_get (devc, cmd & 0xff))<0)
971 				        return val;
972 				else
973 				        return put_user(val, (int *)arg);
974 			}
975 		}
976 	} else
977 		/* not for mixer */
978 		return -(EINVAL);
979 }
980 
981 /* ------------------------------------------------------------------- */
982 
983 /* Mixer structure */
984 
985 static struct mixer_operations ad1816_mixer_operations = {
986 	owner:	THIS_MODULE,
987 	id:	"AD1816",
988 	name:	"AD1816 Mixer",
989 	ioctl:	ad1816_mixer_ioctl
990 };
991 
992 
993 /* ------------------------------------------------------------------- */
994 
995 /* stuff for card recognition, init and unloading */
996 
997 
998 /* replace with probe routine */
probe_ad1816(struct address_info * hw_config)999 static int __init probe_ad1816 ( struct address_info *hw_config )
1000 {
1001 	ad1816_info    *devc = &dev_info[nr_ad1816_devs];
1002 	int io_base=hw_config->io_base;
1003 	int *osp=hw_config->osp;
1004 	int tmp;
1005 
1006 	printk(KERN_INFO "ad1816: AD1816 sounddriver "
1007 			 "Copyright (C) 1998 by Thorsten Knabe\n");
1008 	printk(KERN_INFO "ad1816: io=0x%x, irq=%d, dma=%d, dma2=%d, "
1009 			 "clockfreq=%d, options=%d isadmabug=%d\n",
1010 	       hw_config->io_base,
1011 	       hw_config->irq,
1012 	       hw_config->dma,
1013 	       hw_config->dma2,
1014 	       ad1816_clockfreq,
1015 	       options,
1016 	       isa_dma_bridge_buggy);
1017 
1018 	if (!request_region(io_base, 16, "AD1816 Sound")) {
1019 		printk(KERN_WARNING "ad1816: I/O port 0x%03x not free\n",
1020 				    io_base);
1021 		goto err;
1022 	}
1023 
1024 	DEBUGLOG(printk ("ad1816: detect(%x)\n", io_base));
1025 
1026 	if (nr_ad1816_devs >= MAX_AUDIO_DEV) {
1027 		printk(KERN_WARNING "ad1816: detect error - step 0\n");
1028 		goto out_release_region;
1029 	}
1030 
1031 	devc->base = io_base;
1032 	devc->irq_ok = 0;
1033 	devc->irq = 0;
1034 	devc->opened = 0;
1035 	devc->osp = osp;
1036 
1037 	/* base+0: bit 1 must be set but not 255 */
1038 	tmp=inb(devc->base);
1039 	if ( (tmp&0x80)==0 || tmp==255 ) {
1040 		DEBUGLOG (printk ("ad1816: Chip is not an AD1816 or chip is not active (Test 0)\n"));
1041 		goto out_release_region;
1042 	}
1043 
1044 
1045 	/* writes to ireg 8 are copied to ireg 9 */
1046 	ad_write(devc,8,12345);
1047 	if (ad_read(devc,9)!=12345) {
1048 		DEBUGLOG (printk ("ad1816: Chip is not an AD1816 (Test 1)\n"));
1049 		goto out_release_region;
1050 	}
1051 
1052 	/* writes to ireg 8 are copied to ireg 9 */
1053 	ad_write(devc,8,54321);
1054 	if (ad_read(devc,9)!=54321) {
1055 		DEBUGLOG (printk ("ad1816: Chip is not an AD1816 (Test 2)\n"));
1056 		goto out_release_region;
1057 	}
1058 
1059 
1060 	/* writes to ireg 10 are copied to ireg 11 */
1061 	ad_write(devc,10,54321);
1062 	if (ad_read(devc,11)!=54321) {
1063 		DEBUGLOG (printk ("ad1816: Chip is not an AD1816 (Test 3)\n"));
1064 		goto out_release_region;
1065 	}
1066 
1067 	/* writes to ireg 10 are copied to ireg 11 */
1068 	ad_write(devc,10,12345);
1069 	if (ad_read(devc,11)!=12345) {
1070 		DEBUGLOG (printk ("ad1816: Chip is not an AD1816 (Test 4)\n"));
1071 		goto out_release_region;
1072 	}
1073 
1074 	/* bit in base +1 cannot be set to 1 */
1075 	tmp=inb(devc->base+1);
1076 	outb(0xff,devc->base+1);
1077 	if (inb(devc->base+1)!=tmp) {
1078 		DEBUGLOG (printk ("ad1816: Chip is not an AD1816 (Test 5)\n"));
1079 		goto out_release_region;
1080 	}
1081 
1082 
1083 	DEBUGLOG (printk ("ad1816: detect() - Detected OK\n"));
1084 	DEBUGLOG (printk ("ad1816: AD1816 Version: %d\n",ad_read(devc,45)));
1085 
1086 	/* detection was successful */
1087 	return 1;
1088 out_release_region:
1089 	release_region(io_base, 16);
1090 	/* detection was NOT successful */
1091 err:	return 0;
1092 }
1093 
1094 
1095 /* allocate resources from the kernel. If any allocation fails, free
1096    all allocated resources and exit attach.
1097 
1098  */
1099 
attach_ad1816(struct address_info * hw_config)1100 static void __init attach_ad1816 (struct address_info *hw_config)
1101 {
1102 	int             my_dev;
1103 	char            dev_name[100];
1104 	ad1816_info    *devc = &dev_info[nr_ad1816_devs];
1105 
1106 	devc->base = hw_config->io_base;
1107 
1108 	/* disable all interrupts */
1109 	ad_write(devc,1,0);
1110 
1111 	/* Clear pending interrupts */
1112 	outb (0, devc->base+1);
1113 
1114 	/* allocate irq */
1115 	if (hw_config->irq < 0 || hw_config->irq > 15)
1116 		goto out_release_region;
1117 	if (request_irq(hw_config->irq, ad1816_interrupt,0,
1118 			"SoundPort", hw_config->osp) < 0)	{
1119 	        printk(KERN_WARNING "ad1816: IRQ in use\n");
1120 		goto out_release_region;
1121 	}
1122 	devc->irq=hw_config->irq;
1123 
1124 	/* DMA stuff */
1125 	if (sound_alloc_dma (hw_config->dma, "Sound System")) {
1126 		printk(KERN_WARNING "ad1816: Can't allocate DMA%d\n",
1127 				    hw_config->dma);
1128 		goto out_free_irq;
1129 	}
1130 	devc->dma_playback=hw_config->dma;
1131 
1132 	if ( hw_config->dma2 != -1 && hw_config->dma2 != hw_config->dma) {
1133 		if (sound_alloc_dma(hw_config->dma2,
1134 				    "Sound System (capture)")) {
1135 			printk(KERN_WARNING "ad1816: Can't allocate DMA%d\n",
1136 					    hw_config->dma2);
1137 			goto out_free_dma;
1138 		}
1139 		devc->dma_capture=hw_config->dma2;
1140 		devc->audio_mode=DMA_AUTOMODE|DMA_DUPLEX;
1141 	} else {
1142 	        devc->dma_capture=-1;
1143 		devc->audio_mode=DMA_AUTOMODE;
1144 	}
1145 
1146 	sprintf (dev_name,"AD1816 audio driver");
1147 
1148 	conf_printf2 (dev_name,
1149 		      devc->base, devc->irq, hw_config->dma, hw_config->dma2);
1150 
1151 	/* register device */
1152 	if ((my_dev = sound_install_audiodrv (AUDIO_DRIVER_VERSION,
1153 					      dev_name,
1154 					      &ad1816_audio_driver,
1155 					      sizeof (struct audio_driver),
1156 					      devc->audio_mode,
1157 					      ad_format_mask,
1158 					      devc,
1159 					      hw_config->dma,
1160 					      hw_config->dma2)) < 0) {
1161 		printk(KERN_WARNING "ad1816: Can't install sound driver\n");
1162 		goto out_free_dma_2;
1163 	}
1164 
1165 	/* fill rest of structure with reasonable default values */
1166 	irq2dev[hw_config->irq] = devc->dev_no = my_dev;
1167 	devc->opened = 0;
1168 	devc->irq_ok = 0;
1169 	devc->osp = hw_config->osp;
1170 	nr_ad1816_devs++;
1171 
1172 	ad_write(devc,32,0x80f0); /* sound system mode */
1173 	if (options&1) {
1174 	        ad_write(devc,33,0); /* disable all audiosources for dsp */
1175 	} else {
1176 	        ad_write(devc,33,0x03f8); /* enable all audiosources for dsp */
1177 	}
1178 	ad_write(devc,4,0x8080);  /* default values for volumes (muted)*/
1179 	ad_write(devc,5,0x8080);
1180 	ad_write(devc,6,0x8080);
1181 	ad_write(devc,7,0x8080);
1182 	ad_write(devc,15,0x8888);
1183 	ad_write(devc,16,0x8888);
1184 	ad_write(devc,17,0x8888);
1185 	ad_write(devc,18,0x8888);
1186 	ad_write(devc,19,0xc888); /* +20db mic active */
1187 	ad_write(devc,14,0x0000); /* Master volume unmuted */
1188 	ad_write(devc,39,0x009f); /* 3D effect on 0% phone out muted */
1189 	ad_write(devc,44,0x0080); /* everything on power, 3d enabled for d/a */
1190 	outb(0x10,devc->base+8); /* set dma mode */
1191 	outb(0x10,devc->base+9);
1192 
1193 	/* enable capture + playback interrupt */
1194 	ad_write(devc,1,0xc000);
1195 
1196 	/* set mixer defaults */
1197 	ad1816_mixer_reset (devc);
1198 
1199 	/* register mixer */
1200 	if ((audio_devs[my_dev]->mixer_dev=sound_install_mixer(
1201 				       MIXER_DRIVER_VERSION,
1202 				       dev_name,
1203 				       &ad1816_mixer_operations,
1204 				       sizeof (struct mixer_operations),
1205 				       devc)) >= 0) {
1206 		audio_devs[my_dev]->min_fragment = 0;
1207 	}
1208 out:	return;
1209 out_free_dma_2:
1210 	if (devc->dma_capture >= 0)
1211 	        sound_free_dma(hw_config->dma2);
1212 out_free_dma:
1213 	sound_free_dma(hw_config->dma);
1214 out_free_irq:
1215 	free_irq(hw_config->irq,hw_config->osp);
1216 out_release_region:
1217 	release_region(hw_config->io_base, 16);
1218 	goto out;
1219 }
1220 
unload_card(ad1816_info * devc)1221 static void __exit unload_card(ad1816_info *devc)
1222 {
1223 	int  mixer, dev = 0;
1224 
1225 	if (devc != NULL) {
1226 		DEBUGLOG (printk("ad1816: Unloading card at base=%x\n",devc->base));
1227 
1228 		dev = devc->dev_no;
1229 		mixer = audio_devs[dev]->mixer_dev;
1230 
1231 		/* unreg mixer*/
1232 		if(mixer>=0) {
1233 			sound_unload_mixerdev(mixer);
1234 		}
1235 		sound_unload_audiodev(dev);
1236 
1237 		/* free dma channels */
1238 		if (devc->dma_capture>=0) {
1239 			sound_free_dma(devc->dma_capture);
1240 		}
1241 
1242 		/* card wont get added if resources could not be allocated
1243 		   thus we need not ckeck if allocation was successful */
1244 		sound_free_dma (devc->dma_playback);
1245 		free_irq(devc->irq, devc->osp);
1246 		release_region (devc->base, 16);
1247 
1248 		DEBUGLOG (printk("ad1816: Unloading card at base=%x was successful\n",devc->base));
1249 
1250 	} else
1251 		printk(KERN_WARNING "ad1816: no device/card specified\n");
1252 }
1253 
1254 static struct address_info cfg;
1255 
1256 static int __initdata io = -1;
1257 static int __initdata irq = -1;
1258 static int __initdata dma = -1;
1259 static int __initdata dma2 = -1;
1260 
1261 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
1262 struct pci_dev	*ad1816_dev  = NULL;
1263 
1264 static int activated	= 1;
1265 
1266 static int isapnp	= 1;
1267 static int isapnpjump	= 0;
1268 
1269 MODULE_PARM(isapnp, "i");
1270 MODULE_PARM(isapnpjump, "i");
1271 
1272 #else
1273 static int isapnp = 0;
1274 #endif
1275 
1276 MODULE_PARM(io,"i");
1277 MODULE_PARM(irq,"i");
1278 MODULE_PARM(dma,"i");
1279 MODULE_PARM(dma2,"i");
1280 MODULE_PARM(ad1816_clockfreq,"i");
1281 MODULE_PARM(options,"i");
1282 
1283 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
1284 
activate_dev(char * devname,char * resname,struct pci_dev * dev)1285 static struct pci_dev *activate_dev(char *devname, char *resname, struct pci_dev *dev)
1286 {
1287 	int err;
1288 
1289 	if(dev->active) {
1290 		activated = 0;
1291 		return(dev);
1292 	}
1293 
1294 	if((err = dev->activate(dev)) < 0) {
1295 		printk(KERN_ERR "ad1816: %s %s config failed (out of resources?)[%d]\n",
1296 			devname, resname, err);
1297 		dev->deactivate(dev);
1298 		return(NULL);
1299 	}
1300 
1301 	return(dev);
1302 }
1303 
ad1816_init_generic(struct pci_bus * bus,struct pci_dev * card,struct address_info * hw_config)1304 static struct pci_dev *ad1816_init_generic(struct pci_bus *bus, struct pci_dev *card,
1305 	struct address_info *hw_config)
1306 {
1307 	if((ad1816_dev = isapnp_find_dev(bus, card->vendor, card->device, NULL))) {
1308 		ad1816_dev->prepare(ad1816_dev);
1309 
1310 		if((ad1816_dev = activate_dev("Analog Devices 1816(A)", "ad1816", ad1816_dev))) {
1311 			hw_config->io_base	= ad1816_dev->resource[2].start;
1312 			hw_config->irq		= ad1816_dev->irq_resource[0].start;
1313 			hw_config->dma		= ad1816_dev->dma_resource[0].start;
1314 			hw_config->dma2		= ad1816_dev->dma_resource[1].start;
1315 		}
1316 	}
1317 
1318 	return(ad1816_dev);
1319 }
1320 
1321 struct isapnp_device_id isapnp_ad1816_list[] __initdata = {
1322 	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
1323 		ISAPNP_VENDOR('A','D','S'), ISAPNP_FUNCTION(0x7150),
1324 		0 },
1325 	{	ISAPNP_ANY_ID, ISAPNP_ANY_ID,
1326 		ISAPNP_VENDOR('A','D','S'), ISAPNP_FUNCTION(0x7180),
1327 		0 },
1328 	{0}
1329 };
1330 
1331 MODULE_DEVICE_TABLE(isapnp, isapnp_ad1816_list);
1332 
ad1816_init_isapnp(struct address_info * hw_config,struct pci_bus * bus,struct pci_dev * card,int slot)1333 static int __init ad1816_init_isapnp(struct address_info *hw_config,
1334 	struct pci_bus *bus, struct pci_dev *card, int slot)
1335 {
1336 	char *busname = bus->name[0] ? bus->name : "Analog Devices AD1816a";
1337 	struct pci_dev *idev = NULL;
1338 
1339 	printk(KERN_INFO "ad1816: %s detected\n", busname);
1340 
1341 	/* Initialize this baby. */
1342 	if ((idev = ad1816_init_generic(bus, card, hw_config))) {
1343 		/* We got it. */
1344 
1345 		printk(KERN_NOTICE "ad1816: ISAPnP reports '%s' at i/o %#x, irq %d, dma %d, %d\n",
1346 			busname,
1347 			hw_config->io_base, hw_config->irq, hw_config->dma,
1348 			hw_config->dma2);
1349 		return 1;
1350 	} else
1351 		printk(KERN_INFO "ad1816: Failed to initialize %s\n", busname);
1352 
1353 	return 0;
1354 }
1355 
1356 /*
1357  * Actually this routine will detect and configure only the first card with successful
1358  * initialization. isapnpjump could be used to jump to a specific entry.
1359  * Please always add entries at the end of the array.
1360  * Should this be fixed? - azummo
1361  */
1362 
ad1816_probe_isapnp(struct address_info * hw_config)1363 int __init ad1816_probe_isapnp(struct address_info *hw_config)
1364 {
1365 	int i;
1366 
1367 	/* Count entries in isapnp_ad1816_list */
1368 	for (i = 0; isapnp_ad1816_list[i].vendor != 0; i++)
1369 		;
1370 	/* Check and adjust isapnpjump */
1371 	if( isapnpjump < 0 || isapnpjump > ( i - 1 ) ) {
1372 		printk(KERN_ERR "ad1816: Valid range for isapnpjump is 0-%d. Adjusted to 0.\n", i-1);
1373 		isapnpjump = 0;
1374 	}
1375 
1376 	 for (i = isapnpjump; isapnp_ad1816_list[i].vendor != 0; i++) {
1377 	 	struct pci_dev *card = NULL;
1378 
1379 		while ((card = isapnp_find_dev(NULL, isapnp_ad1816_list[i].vendor,
1380 		  isapnp_ad1816_list[i].function, card)))
1381 			if(ad1816_init_isapnp(hw_config, card->bus, card, i))
1382 				return 0;
1383 	}
1384 
1385 	return -ENODEV;
1386 }
1387 #endif
1388 
init_ad1816(void)1389 static int __init init_ad1816(void)
1390 {
1391 
1392 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
1393 	if(isapnp && (ad1816_probe_isapnp(&cfg) < 0) ) {
1394 		printk(KERN_NOTICE "ad1816: No ISAPnP cards found, trying standard ones...\n");
1395 		isapnp = 0;
1396 	}
1397 #endif
1398 
1399 	if( isapnp == 0) {
1400 		cfg.io_base	= io;
1401 		cfg.irq		= irq;
1402 		cfg.dma		= dma;
1403 		cfg.dma2	= dma2;
1404 	}
1405 
1406 	if (cfg.io_base == -1 || cfg.irq == -1 || cfg.dma == -1 || cfg.dma2 == -1) {
1407 		printk(KERN_INFO "ad1816: dma, dma2, irq and io must be set.\n");
1408 		return -EINVAL;
1409 	}
1410 
1411 	if (probe_ad1816(&cfg) == 0) {
1412 		return -ENODEV;
1413 	}
1414 
1415 	attach_ad1816(&cfg);
1416 
1417 	return 0;
1418 }
1419 
cleanup_ad1816(void)1420 static void __exit cleanup_ad1816 (void)
1421 {
1422 	int          i;
1423 	ad1816_info  *devc = NULL;
1424 
1425 	/* remove any soundcard */
1426 	for (i = 0;  i < nr_ad1816_devs; i++) {
1427 		devc = &dev_info[i];
1428 		unload_card(devc);
1429 	}
1430 	nr_ad1816_devs=0;
1431 
1432 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
1433 	if(activated)
1434 		if(ad1816_dev)
1435 			ad1816_dev->deactivate(ad1816_dev);
1436 #endif
1437 }
1438 
1439 module_init(init_ad1816);
1440 module_exit(cleanup_ad1816);
1441 
1442 #ifndef MODULE
setup_ad1816(char * str)1443 static int __init setup_ad1816(char *str)
1444 {
1445 	/* io, irq, dma, dma2 */
1446 	int ints[5];
1447 
1448 	str = get_options(str, ARRAY_SIZE(ints), ints);
1449 
1450 	io	= ints[1];
1451 	irq	= ints[2];
1452 	dma	= ints[3];
1453 	dma2	= ints[4];
1454 
1455 	return 1;
1456 }
1457 
1458 __setup("ad1816=", setup_ad1816);
1459 #endif
1460 MODULE_LICENSE("GPL");
1461