3 * AD1816 lowlevel sound driver for Linux 2.2.0 and above
5 * Copyright (C) 1998 by Thorsten Knabe <tek@rbg.informatik.tu-darmstadt.de>
7 * Based on the CS4232/AD1848 driver Copyright (C) by Hannu Savolainen 1993-1996
11 * status: experimental
15 * Oleg Drokin: Some cleanup of load/unload functions. 1998/11/24
17 * Thorsten Knabe: attach and unload rewritten,
18 * some argument checks added 1998/11/30
20 * Thorsten Knabe: Buggy isa bridge workaround added 1999/01/16
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
28 * Christoph Hellwig: Adapted to module_init/module_exit. 2000/03/03
30 * Christoph Hellwig: Added isapnp support 2000/03/15
32 * Arnaldo Carvalho de Melo: get rid of check_region 2001/10/07
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>
41 #include "sound_config.h"
48 #define CHECK_FOR_POWER { int timeout=100; \
49 while (timeout > 0 && (inb(devc->base)&0x80)!= 0x80) {\
53 printk(KERN_WARNING "ad1816: Check for power failed in %s line: %d\n",__FILE__,__LINE__); \
57 /* structure to hold device specific information */
60 int base; /* set in attach */
68 unsigned char format_bits;
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
83 static int nr_ad1816_devs;
84 static int ad1816_clockfreq = 33000;
87 /* for backward mapping of irq to sound device */
89 static volatile char irq2dev[17] = {-1, -1, -1, -1, -1, -1, -1, -1,
90 -1, -1, -1, -1, -1, -1, -1, -1, -1};
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;
97 /* array of device info structures */
98 static ad1816_info dev_info[MAX_AUDIO_DEV];
101 /* ------------------------------------------------------------------- */
103 /* functions for easier access to inderect registers */
105 static int ad_read (ad1816_info * devc, int reg)
112 save_flags (flags); /* make register access atomic */
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);
123 static void ad_write (ad1816_info * devc, int reg, int data)
129 save_flags (flags); /* make register access atomic */
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);
138 /* ------------------------------------------------------------------- */
140 /* function interface required by struct audio_driver */
142 static void ad1816_halt_input (int dev)
145 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
146 unsigned char buffer;
148 DEBUGINFO (printk("ad1816: halt_input called\n"));
153 if(!isa_dma_bridge_buggy) {
154 disable_dma(audio_devs[dev]->dmap_in->dma);
157 buffer=inb(devc->base+9);
159 /* disable capture */
160 outb(buffer & ~0x01,devc->base+9);
163 if(!isa_dma_bridge_buggy) {
164 enable_dma(audio_devs[dev]->dmap_in->dma);
167 /* Clear interrupt status */
168 outb (~0x40, devc->base+1);
170 devc->audio_mode &= ~PCM_ENABLE_INPUT;
171 restore_flags (flags);
174 static void ad1816_halt_output (int dev)
177 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
179 unsigned char buffer;
181 DEBUGINFO (printk("ad1816: halt_output called!\n"));
185 /* Mute pcm output */
186 ad_write(devc, 4, ad_read(devc,4)|0x8080);
188 if(!isa_dma_bridge_buggy) {
189 disable_dma(audio_devs[dev]->dmap_out->dma);
192 buffer=inb(devc->base+8);
194 /* disable capture */
195 outb(buffer & ~0x01,devc->base+8);
198 if(!isa_dma_bridge_buggy) {
199 enable_dma(audio_devs[dev]->dmap_out->dma);
202 /* Clear interrupt status */
203 outb ((unsigned char)~0x80, devc->base+1);
205 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
206 restore_flags (flags);
209 static void ad1816_output_block (int dev, unsigned long buf,
210 int count, int intrflag)
214 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
216 DEBUGINFO(printk("ad1816: output_block called buf=%ld count=%d flags=%d\n",buf,count,intrflag));
223 /* set transfer count */
224 ad_write (devc, 8, cnt & 0xffff);
226 devc->audio_mode |= PCM_ENABLE_OUTPUT;
227 restore_flags (flags);
231 static void ad1816_start_input (int dev, unsigned long buf, int count,
236 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
238 DEBUGINFO(printk("ad1816: start_input called buf=%ld count=%d flags=%d\n",buf,count,intrflag));
242 save_flags (flags); /* make register access atomic */
245 /* set transfer count */
246 ad_write (devc, 10, cnt & 0xffff);
248 devc->audio_mode |= PCM_ENABLE_INPUT;
249 restore_flags (flags);
252 static int ad1816_prepare_for_input (int dev, int bsize, int bcount)
256 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
257 unsigned char fmt_bits;
259 DEBUGINFO (printk ("ad1816: prepare_for_input called: bsize=%d bcount=%d\n",bsize,bcount));
264 fmt_bits= (devc->format_bits&0x7)<<3;
266 /* set mono/stereo mode */
267 if (devc->channels > 1) {
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);
275 /* If compiled into kernel, AD1816_CLOCK is defined, so use it */
277 ad1816_clockfreq=AD1816_CLOCK;
280 /* capture/playback frequency correction for soundcards
281 with clock chips != 33MHz (allowed range 5 - 100 kHz) */
283 if (ad1816_clockfreq<5000 || ad1816_clockfreq>100000) {
284 ad1816_clockfreq=33000;
287 freq=((unsigned int)devc->speed*33000)/ad1816_clockfreq;
289 /* write playback/capture speeds */
290 ad_write (devc, 2, freq & 0xffff);
291 ad_write (devc, 3, freq & 0xffff);
293 restore_flags (flags);
295 ad1816_halt_input(dev);
299 static int ad1816_prepare_for_output (int dev, int bsize, int bcount)
303 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
304 unsigned char fmt_bits;
306 DEBUGINFO (printk ("ad1816: prepare_for_output called: bsize=%d bcount=%d\n",bsize,bcount));
308 save_flags (flags); /* make register access atomic */
311 fmt_bits= (devc->format_bits&0x7)<<3;
312 /* set mono/stereo mode */
313 if (devc->channels > 1) {
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);
322 ad1816_clockfreq=AD1816_CLOCK;
325 /* capture/playback frequency correction for soundcards
326 with clock chips != 33MHz (allowed range 5 - 100 kHz)*/
328 if (ad1816_clockfreq<5000 || ad1816_clockfreq>100000) {
329 ad1816_clockfreq=33000;
332 freq=((unsigned int)devc->speed*33000)/ad1816_clockfreq;
334 /* write playback/capture speeds */
335 ad_write (devc, 2, freq & 0xffff);
336 ad_write (devc, 3, freq & 0xffff);
338 restore_flags (flags);
340 ad1816_halt_output(dev);
345 static void ad1816_trigger (int dev, int state)
348 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
350 DEBUGINFO (printk("ad1816: trigger called! (devc=%d,devc->base=%d\n", devc, devc->base));
352 /* mode may have changed */
354 save_flags (flags); /* make register access atomic */
357 /* mask out modes not specified on open call */
358 state &= devc->audio_mode;
360 /* setup soundchip to new io-mode */
361 if (state & PCM_ENABLE_INPUT) {
363 outb(inb(devc->base+9)|0x01, devc->base+9);
365 /* disable capture */
366 outb(inb(devc->base+9)&~0x01, devc->base+9);
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);
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);
380 restore_flags (flags);
384 /* halt input & output */
385 static void ad1816_halt (int dev)
387 ad1816_halt_input(dev);
388 ad1816_halt_output(dev);
391 static void ad1816_reset (int dev)
396 /* set playback speed */
397 static int ad1816_set_speed (int dev, int arg)
399 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
417 static unsigned int ad1816_set_bits (int dev, unsigned int arg)
419 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
421 static struct format_tbl {
428 { AFMT_IMA_ADPCM, 0 },
437 int i, n = sizeof (format2bits) / sizeof (struct format_tbl);
439 /* return current format */
441 return devc->audio_format;
443 devc->audio_format = arg;
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;
453 /* Still hanging here. Something must be terribly wrong */
454 devc->format_bits = 0;
455 return devc->audio_format = AFMT_U8;
458 static short ad1816_set_channels (int dev, short arg)
460 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
462 if (arg != 1 && arg != 2)
463 return devc->channels;
465 devc->channels = arg;
470 static int ad1816_open (int dev, int mode)
472 ad1816_info *devc = NULL;
475 /* is device number valid ? */
476 if (dev < 0 || dev >= num_audiodevs)
479 /* get device info of this dev */
480 devc = (ad1816_info *) audio_devs[dev]->devc;
482 /* make check if device already open atomic */
487 restore_flags (flags);
491 /* mark device as open */
494 devc->audio_mode = 0;
496 devc->audio_format=AFMT_U8;
499 ad1816_reset(devc->dev_no); /* halt all pending output */
500 restore_flags (flags);
504 static void ad1816_close (int dev) /* close device */
507 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
512 /* halt all pending output */
513 ad1816_reset(devc->dev_no);
516 devc->audio_mode = 0;
518 devc->audio_format=AFMT_U8;
519 devc->format_bits = 0;
522 restore_flags (flags);
526 /* ------------------------------------------------------------------- */
528 /* Audio driver structure */
530 static struct audio_driver ad1816_audio_driver =
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,
549 /* ------------------------------------------------------------------- */
551 /* Interrupt handler */
554 static void ad1816_interrupt (int irq, void *dev_id, struct pt_regs *dummy)
556 unsigned char status;
562 if (irq < 0 || irq > 15) {
563 printk(KERN_WARNING "ad1816: Got bogus interrupt %d\n", irq);
569 if (dev < 0 || dev >= num_audiodevs) {
570 printk(KERN_WARNING "ad1816: IRQ2AD1816-mapping failed for "
571 "irq %d device %d\n", irq,dev);
575 devc = (ad1816_info *) audio_devs[dev]->devc;
580 /* read interrupt register */
581 status = inb (devc->base+1);
582 /* Clear all interrupt */
583 outb (~status, devc->base+1);
585 DEBUGNOISE (printk("ad1816: Got interrupt subclass %d\n",status));
590 DEBUGWARN(printk ("ad1816: interrupt: Got interrupt, but no reason?\n"));
592 if (devc->opened && (devc->audio_mode & PCM_ENABLE_INPUT) && (status&64))
593 DMAbuf_inputintr (dev);
595 if (devc->opened && (devc->audio_mode & PCM_ENABLE_OUTPUT) && (status & 128))
596 DMAbuf_outputintr (dev, 1);
598 restore_flags(flags);
601 /* ------------------------------------------------------------------- */
606 unsigned int regno: 7;
607 unsigned int polarity:1; /* 0=normal, 1=reversed */
608 unsigned int bitpos:4;
609 unsigned int nbits:4;
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,
621 typedef struct mixer_def mixer_ent;
624 * Most of the mixer entries work in backwards. Setting the polarity field
625 * makes them to work correctly.
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.
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}}
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)
657 static unsigned short default_mixer_levels[SOUND_MIXER_NRDEVICES] =
659 0x4343, /* Master Volume */
664 0x0000, /* PC Speaker */
665 0x0000, /* Ext Line */
668 0x0000, /* Recording monitor */
670 0x0000, /* Recording level */
671 0x0000, /* Input gain */
672 0x0000, /* Output gain */
675 0x0000 /* Line3 (usually line in)*/
684 ad1816_set_recmask (ad1816_info * devc, int mask)
686 unsigned char recdev;
689 mask &= devc->supported_rec_devices;
692 /* Count selected device bits */
693 for (i = 0; i < 32; i++)
698 mask = SOUND_MASK_MIC;
699 else if (n != 1) { /* Too many devices selected */
700 /* Filter out active settings */
701 mask &= ~devc->recmask;
704 /* Count selected device bits */
705 for (i = 0; i < 32; i++)
710 mask = SOUND_MASK_MIC;
718 case SOUND_MASK_LINE:
726 case SOUND_MASK_LINE1:
730 case SOUND_MASK_LINE2:
734 case SOUND_MASK_VOLUME:
739 mask = SOUND_MASK_MIC;
745 (ad_read (devc, 20) & 0x8f8f) | recdev | (recdev<<8));
747 devc->recmask = mask;
752 change_bits (int *regval, int dev, int chn, int newval)
757 /* Reverse polarity*/
759 if (mix_devices[dev][chn].polarity == 1)
760 newval = 100 - newval;
762 mask = (1 << mix_devices[dev][chn].nbits) - 1;
763 shift = mix_devices[dev][chn].bitpos;
765 newval = (int) ((newval * mask) + 50) / 100;
767 *regval &= ~(mask << shift);
769 *regval |= (newval & mask) << shift;
773 ad1816_mixer_get (ad1816_info * devc, int dev)
775 DEBUGINFO(printk("ad1816: mixer_get called!\n"));
777 /* range check + supported mixer check */
778 if (dev < 0 || dev >= SOUND_MIXER_NRDEVICES )
780 if (!((1 << dev) & devc->supported_devices))
783 return devc->levels[dev];
787 ad1816_mixer_set (ad1816_info * devc, int dev, int value)
789 int left = value & 0x000000ff;
790 int right = (value & 0x0000ff00) >> 8;
797 DEBUGINFO(printk("ad1816: mixer_set called!\n"));
799 if (dev < 0 || dev >= SOUND_MIXER_NRDEVICES )
812 if (mix_devices[dev][RIGHT_CHN].nbits == 0)
814 retvol = left | (right << 8);
818 left = mix_cvt[left];
819 right = mix_cvt[right];
821 /* reject all mixers that are not supported */
822 if (!(devc->supported_devices & (1 << dev)))
826 if (mix_devices[dev][LEFT_CHN].nbits == 0)
829 /* keep precise volume internal */
830 devc->levels[dev] = retvol;
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);
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) {
848 ad_write (devc, regoffs, valmute); /* mute */
851 * Set the right channel
854 /* Was just a mono channel */
855 if (mix_devices[dev][RIGHT_CHN].nbits == 0)
858 regoffs = mix_devices[dev][RIGHT_CHN].regno;
859 val = ad_read (devc, regoffs);
860 change_bits (&val, dev, RIGHT_CHN, right);
863 if ( regoffs==5 || regoffs==14 || regoffs==15 ||
864 regoffs==16 || regoffs==17 || regoffs==18 ||
865 regoffs==19 || regoffs==39) {
871 ad_write (devc, regoffs, valmute); /* mute */
876 #define MIXER_DEVICES ( SOUND_MASK_VOLUME | \
887 #define REC_DEVICES ( SOUND_MASK_LINE2 |\
896 ad1816_mixer_reset (ad1816_info * devc)
900 devc->supported_devices = MIXER_DEVICES;
902 devc->supported_rec_devices = REC_DEVICES;
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);
911 ad1816_mixer_ioctl (int dev, unsigned int cmd, caddr_t arg)
913 ad1816_info *devc = mixer_devs[dev]->devc;
916 DEBUGINFO(printk("ad1816: mixer_ioctl called!\n"));
919 if (((cmd >> 8) & 0xff) == 'M') {
922 if (_SIOC_DIR (cmd) & _SIOC_WRITE) {
924 case SOUND_MIXER_RECSRC:
926 if (get_user(val, (int *)arg))
928 val=ad1816_set_recmask (devc, val);
929 return put_user(val, (int *)arg);
933 if (get_user(val, (int *)arg))
935 if ((val=ad1816_mixer_set (devc, cmd & 0xff, val))<0)
938 return put_user(val, (int *)arg);
942 switch (cmd & 0xff) {
944 case SOUND_MIXER_RECSRC:
946 return put_user(val, (int *)arg);
949 case SOUND_MIXER_DEVMASK:
950 val=devc->supported_devices;
951 return put_user(val, (int *)arg);
954 case SOUND_MIXER_STEREODEVS:
955 val=devc->supported_devices & ~(SOUND_MASK_SPEAKER | SOUND_MASK_IMIX);
956 return put_user(val, (int *)arg);
959 case SOUND_MIXER_RECMASK:
960 val=devc->supported_rec_devices;
961 return put_user(val, (int *)arg);
964 case SOUND_MIXER_CAPS:
965 val=SOUND_CAP_EXCL_INPUT;
966 return put_user(val, (int *)arg);
970 if ((val=ad1816_mixer_get (devc, cmd & 0xff))<0)
973 return put_user(val, (int *)arg);
981 /* ------------------------------------------------------------------- */
983 /* Mixer structure */
985 static struct mixer_operations ad1816_mixer_operations = {
988 name: "AD1816 Mixer",
989 ioctl: ad1816_mixer_ioctl
993 /* ------------------------------------------------------------------- */
995 /* stuff for card recognition, init and unloading */
998 /* replace with probe routine */
999 static int __init probe_ad1816 ( struct address_info *hw_config )
1001 ad1816_info *devc = &dev_info[nr_ad1816_devs];
1002 int io_base=hw_config->io_base;
1003 int *osp=hw_config->osp;
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",
1016 isa_dma_bridge_buggy);
1018 if (!request_region(io_base, 16, "AD1816 Sound")) {
1019 printk(KERN_WARNING "ad1816: I/O port 0x%03x not free\n",
1024 DEBUGLOG(printk ("ad1816: detect(%x)\n", io_base));
1026 if (nr_ad1816_devs >= MAX_AUDIO_DEV) {
1027 printk(KERN_WARNING "ad1816: detect error - step 0\n");
1028 goto out_release_region;
1031 devc->base = io_base;
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;
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;
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;
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;
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;
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;
1083 DEBUGLOG (printk ("ad1816: detect() - Detected OK\n"));
1084 DEBUGLOG (printk ("ad1816: AD1816 Version: %d\n",ad_read(devc,45)));
1086 /* detection was successful */
1089 release_region(io_base, 16);
1090 /* detection was NOT successful */
1095 /* allocate resources from the kernel. If any allocation fails, free
1096 all allocated resources and exit attach.
1100 static void __init attach_ad1816 (struct address_info *hw_config)
1104 ad1816_info *devc = &dev_info[nr_ad1816_devs];
1106 devc->base = hw_config->io_base;
1108 /* disable all interrupts */
1111 /* Clear pending interrupts */
1112 outb (0, devc->base+1);
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;
1122 devc->irq=hw_config->irq;
1125 if (sound_alloc_dma (hw_config->dma, "Sound System")) {
1126 printk(KERN_WARNING "ad1816: Can't allocate DMA%d\n",
1130 devc->dma_playback=hw_config->dma;
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",
1139 devc->dma_capture=hw_config->dma2;
1140 devc->audio_mode=DMA_AUTOMODE|DMA_DUPLEX;
1142 devc->dma_capture=-1;
1143 devc->audio_mode=DMA_AUTOMODE;
1146 sprintf (dev_name,"AD1816 audio driver");
1148 conf_printf2 (dev_name,
1149 devc->base, devc->irq, hw_config->dma, hw_config->dma2);
1151 /* register device */
1152 if ((my_dev = sound_install_audiodrv (AUDIO_DRIVER_VERSION,
1154 &ad1816_audio_driver,
1155 sizeof (struct audio_driver),
1160 hw_config->dma2)) < 0) {
1161 printk(KERN_WARNING "ad1816: Can't install sound driver\n");
1162 goto out_free_dma_2;
1165 /* fill rest of structure with reasonable default values */
1166 irq2dev[hw_config->irq] = devc->dev_no = my_dev;
1169 devc->osp = hw_config->osp;
1172 ad_write(devc,32,0x80f0); /* sound system mode */
1174 ad_write(devc,33,0); /* disable all audiosources for dsp */
1176 ad_write(devc,33,0x03f8); /* enable all audiosources for dsp */
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);
1193 /* enable capture + playback interrupt */
1194 ad_write(devc,1,0xc000);
1196 /* set mixer defaults */
1197 ad1816_mixer_reset (devc);
1199 /* register mixer */
1200 if ((audio_devs[my_dev]->mixer_dev=sound_install_mixer(
1201 MIXER_DRIVER_VERSION,
1203 &ad1816_mixer_operations,
1204 sizeof (struct mixer_operations),
1206 audio_devs[my_dev]->min_fragment = 0;
1210 if (devc->dma_capture >= 0)
1211 sound_free_dma(hw_config->dma2);
1213 sound_free_dma(hw_config->dma);
1215 free_irq(hw_config->irq,hw_config->osp);
1217 release_region(hw_config->io_base, 16);
1221 static void __exit unload_card(ad1816_info *devc)
1226 DEBUGLOG (printk("ad1816: Unloading card at base=%x\n",devc->base));
1229 mixer = audio_devs[dev]->mixer_dev;
1233 sound_unload_mixerdev(mixer);
1235 sound_unload_audiodev(dev);
1237 /* free dma channels */
1238 if (devc->dma_capture>=0) {
1239 sound_free_dma(devc->dma_capture);
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);
1248 DEBUGLOG (printk("ad1816: Unloading card at base=%x was successful\n",devc->base));
1251 printk(KERN_WARNING "ad1816: no device/card specified\n");
1254 static struct address_info cfg;
1256 static int __initdata io = -1;
1257 static int __initdata irq = -1;
1258 static int __initdata dma = -1;
1259 static int __initdata dma2 = -1;
1261 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
1262 struct pci_dev *ad1816_dev = NULL;
1264 static int activated = 1;
1266 static int isapnp = 1;
1267 static int isapnpjump = 0;
1269 MODULE_PARM(isapnp, "i");
1270 MODULE_PARM(isapnpjump, "i");
1273 static int isapnp = 0;
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");
1283 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
1285 static struct pci_dev *activate_dev(char *devname, char *resname, struct pci_dev *dev)
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);
1304 static struct pci_dev *ad1816_init_generic(struct pci_bus *bus, struct pci_dev *card,
1305 struct address_info *hw_config)
1307 if((ad1816_dev = isapnp_find_dev(bus, card->vendor, card->device, NULL))) {
1308 ad1816_dev->prepare(ad1816_dev);
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;
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),
1325 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
1326 ISAPNP_VENDOR('A','D','S'), ISAPNP_FUNCTION(0x7180),
1331 MODULE_DEVICE_TABLE(isapnp, isapnp_ad1816_list);
1333 static int __init ad1816_init_isapnp(struct address_info *hw_config,
1334 struct pci_bus *bus, struct pci_dev *card, int slot)
1336 char *busname = bus->name[0] ? bus->name : "Analog Devices AD1816a";
1337 struct pci_dev *idev = NULL;
1339 printk(KERN_INFO "ad1816: %s detected\n", busname);
1341 /* Initialize this baby. */
1342 if ((idev = ad1816_init_generic(bus, card, hw_config))) {
1345 printk(KERN_NOTICE "ad1816: ISAPnP reports '%s' at i/o %#x, irq %d, dma %d, %d\n",
1347 hw_config->io_base, hw_config->irq, hw_config->dma,
1351 printk(KERN_INFO "ad1816: Failed to initialize %s\n", busname);
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
1363 int __init ad1816_probe_isapnp(struct address_info *hw_config)
1367 /* Count entries in isapnp_ad1816_list */
1368 for (i = 0; isapnp_ad1816_list[i].vendor != 0; i++)
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);
1376 for (i = isapnpjump; isapnp_ad1816_list[i].vendor != 0; i++) {
1377 struct pci_dev *card = NULL;
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))
1389 static int __init init_ad1816(void)
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");
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");
1411 if (probe_ad1816(&cfg) == 0) {
1415 attach_ad1816(&cfg);
1420 static void __exit cleanup_ad1816 (void)
1423 ad1816_info *devc = NULL;
1425 /* remove any soundcard */
1426 for (i = 0; i < nr_ad1816_devs; i++) {
1427 devc = &dev_info[i];
1432 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
1435 ad1816_dev->deactivate(ad1816_dev);
1439 module_init(init_ad1816);
1440 module_exit(cleanup_ad1816);
1443 static int __init setup_ad1816(char *str)
1445 /* io, irq, dma, dma2 */
1448 str = get_options(str, ARRAY_SIZE(ints), ints);
1458 __setup("ad1816=", setup_ad1816);
1460 MODULE_LICENSE("GPL");