File: /usr/src/linux/drivers/sound/ad1816.c

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