3 * AD1816 lowlevel sound driver for Linux 2.6.0 and above
5 * Copyright (C) 1998-2003 by Thorsten Knabe <linux@thorsten-knabe.de>
7 * Based on the CS4232/AD1848 driver Copyright (C) by Hannu Savolainen 1993-1996
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
34 * Thorsten Knabe: Compiling with CONFIG_PNP enabled
35 * works again. It is now possible to use more than one
36 * AD1816 sound card. Sample rate now may be changed during
37 * playback/capture. printk() uses log levels everywhere.
38 * SMP fixes. DMA handling fixes.
39 * Other minor code cleanup. 2003/07/15
44 #include <linux/config.h>
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/interrupt.h>
48 #include <linux/isapnp.h>
49 #include <linux/stddef.h>
50 #include <linux/spinlock.h>
51 #include "sound_config.h"
55 #define CHECK_FOR_POWER { int timeout=100; \
56 while (timeout > 0 && (inb(devc->base)&0x80)!= 0x80) {\
60 printk(KERN_WARNING "ad1816: Check for power failed in %s line: %d\n",__FILE__,__LINE__); \
64 /* structure to hold device specific information */
67 int base; /* set in attach */
72 int opened; /* open */
78 int recmask; /* setup */
79 unsigned char format_bits;
80 int supported_devices;
81 int supported_rec_devices;
82 unsigned short levels[SOUND_MIXER_NRDEVICES];
84 struct pnp_dev *pnpdev; /* configured via pnp */
85 int dev_no; /* this is the # in audio_devs and NOT
90 static int nr_ad1816_devs;
91 static int ad1816_clockfreq = 33000;
94 /* supported audio formats */
95 static int ad_format_mask =
96 AFMT_U8 | AFMT_S16_LE | AFMT_S16_BE | AFMT_MU_LAW | AFMT_A_LAW;
98 /* array of device info structures */
99 static ad1816_info dev_info[MAX_AUDIO_DEV];
102 /* ------------------------------------------------------------------- */
104 /* functions for easier access to inderect registers */
106 static int ad_read (ad1816_info * devc, int reg)
111 outb ((unsigned char) (reg & 0x3f), devc->base+0);
112 result = inb(devc->base+2);
113 result+= inb(devc->base+3)<<8;
118 static void ad_write (ad1816_info * devc, int reg, int data)
121 outb ((unsigned char) (reg & 0xff), devc->base+0);
122 outb ((unsigned char) (data & 0xff),devc->base+2);
123 outb ((unsigned char) ((data>>8)&0xff),devc->base+3);
126 /* ------------------------------------------------------------------- */
128 /* function interface required by struct audio_driver */
130 static void ad1816_halt_input (int dev)
133 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
134 unsigned char buffer;
136 DEBUGNOISE(printk(KERN_DEBUG "ad1816: halt_input called\n"));
138 spin_lock_irqsave(&devc->lock,flags);
140 if(!isa_dma_bridge_buggy) {
141 disable_dma(audio_devs[dev]->dmap_in->dma);
144 buffer=inb(devc->base+9);
146 /* disable capture */
147 outb(buffer & ~0x01,devc->base+9);
150 if(!isa_dma_bridge_buggy) {
151 enable_dma(audio_devs[dev]->dmap_in->dma);
154 /* Clear interrupt status */
155 outb (~0x40, devc->base+1);
157 devc->audio_mode &= ~PCM_ENABLE_INPUT;
158 spin_unlock_irqrestore(&devc->lock,flags);
161 static void ad1816_halt_output (int dev)
164 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
166 unsigned char buffer;
168 DEBUGNOISE(printk(KERN_DEBUG "ad1816: halt_output called!\n"));
170 spin_lock_irqsave(&devc->lock,flags);
171 /* Mute pcm output */
172 ad_write(devc, 4, ad_read(devc,4)|0x8080);
174 if(!isa_dma_bridge_buggy) {
175 disable_dma(audio_devs[dev]->dmap_out->dma);
178 buffer=inb(devc->base+8);
180 /* disable capture */
181 outb(buffer & ~0x01,devc->base+8);
184 if(!isa_dma_bridge_buggy) {
185 enable_dma(audio_devs[dev]->dmap_out->dma);
188 /* Clear interrupt status */
189 outb ((unsigned char)~0x80, devc->base+1);
191 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
192 spin_unlock_irqrestore(&devc->lock,flags);
195 static void ad1816_output_block (int dev, unsigned long buf,
196 int count, int intrflag)
200 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
202 DEBUGNOISE(printk(KERN_DEBUG "ad1816: output_block called buf=%ld count=%d flags=%d\n",buf,count,intrflag));
206 spin_lock_irqsave(&devc->lock,flags);
208 /* set transfer count */
209 ad_write (devc, 8, cnt & 0xffff);
211 devc->audio_mode |= PCM_ENABLE_OUTPUT;
212 spin_unlock_irqrestore(&devc->lock,flags);
216 static void ad1816_start_input (int dev, unsigned long buf, int count,
221 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
223 DEBUGNOISE(printk(KERN_DEBUG "ad1816: start_input called buf=%ld count=%d flags=%d\n",buf,count,intrflag));
227 spin_lock_irqsave(&devc->lock,flags);
229 /* set transfer count */
230 ad_write (devc, 10, cnt & 0xffff);
231 devc->audio_mode |= PCM_ENABLE_INPUT;
232 spin_unlock_irqrestore(&devc->lock,flags);
235 static int ad1816_prepare_for_input (int dev, int bsize, int bcount)
239 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
240 unsigned char fmt_bits;
242 DEBUGNOISE(printk(KERN_DEBUG "ad1816: prepare_for_input called: bsize=%d bcount=%d\n",bsize,bcount));
244 spin_lock_irqsave(&devc->lock,flags);
245 fmt_bits= (devc->format_bits&0x7)<<3;
247 /* set mono/stereo mode */
248 if (devc->channels > 1) {
251 /* set Mono/Stereo in playback/capture register */
252 outb( (inb(devc->base+8) & ~0x3C)|fmt_bits, devc->base+8);
253 outb( (inb(devc->base+9) & ~0x3C)|fmt_bits, devc->base+9);
255 freq=((unsigned int)devc->speed*33000)/ad1816_clockfreq;
257 /* write playback/capture speeds */
258 ad_write (devc, 2, freq & 0xffff);
259 ad_write (devc, 3, freq & 0xffff);
261 spin_unlock_irqrestore(&devc->lock,flags);
263 ad1816_halt_input(dev);
267 static int ad1816_prepare_for_output (int dev, int bsize, int bcount)
271 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
272 unsigned char fmt_bits;
274 DEBUGNOISE(printk(KERN_DEBUG "ad1816: prepare_for_output called: bsize=%d bcount=%d\n",bsize,bcount));
276 spin_lock_irqsave(&devc->lock,flags);
278 fmt_bits= (devc->format_bits&0x7)<<3;
279 /* set mono/stereo mode */
280 if (devc->channels > 1) {
284 /* write format bits to playback/capture registers */
285 outb( (inb(devc->base+8) & ~0x3C)|fmt_bits, devc->base+8);
286 outb( (inb(devc->base+9) & ~0x3C)|fmt_bits, devc->base+9);
288 freq=((unsigned int)devc->speed*33000)/ad1816_clockfreq;
290 /* write playback/capture speeds */
291 ad_write (devc, 2, freq & 0xffff);
292 ad_write (devc, 3, freq & 0xffff);
294 spin_unlock_irqrestore(&devc->lock,flags);
296 ad1816_halt_output(dev);
301 static void ad1816_trigger (int dev, int state)
304 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
306 DEBUGNOISE(printk(KERN_DEBUG "ad1816: trigger called! (devc=%d,devc->base=%d\n", devc, devc->base));
308 /* mode may have changed */
310 spin_lock_irqsave(&devc->lock,flags);
312 /* mask out modes not specified on open call */
313 state &= devc->audio_mode;
315 /* setup soundchip to new io-mode */
316 if (state & PCM_ENABLE_INPUT) {
318 outb(inb(devc->base+9)|0x01, devc->base+9);
320 /* disable capture */
321 outb(inb(devc->base+9)&~0x01, devc->base+9);
324 if (state & PCM_ENABLE_OUTPUT) {
325 /* enable playback */
326 outb(inb(devc->base+8)|0x01, devc->base+8);
327 /* unmute pcm output */
328 ad_write(devc, 4, ad_read(devc,4)&~0x8080);
330 /* mute pcm output */
331 ad_write(devc, 4, ad_read(devc,4)|0x8080);
332 /* disable capture */
333 outb(inb(devc->base+8)&~0x01, devc->base+8);
335 spin_unlock_irqrestore(&devc->lock,flags);
339 /* halt input & output */
340 static void ad1816_halt (int dev)
342 ad1816_halt_input(dev);
343 ad1816_halt_output(dev);
346 static void ad1816_reset (int dev)
351 /* set playback speed */
352 static int ad1816_set_speed (int dev, int arg)
358 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
360 spin_lock_irqsave(&devc->lock, flags);
363 spin_unlock_irqrestore(&devc->lock, flags);
375 /* change speed during playback */
376 freq=((unsigned int)devc->speed*33000)/ad1816_clockfreq;
377 /* write playback/capture speeds */
378 ad_write (devc, 2, freq & 0xffff);
379 ad_write (devc, 3, freq & 0xffff);
382 spin_unlock_irqrestore(&devc->lock, flags);
387 static unsigned int ad1816_set_bits (int dev, unsigned int arg)
390 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
392 static struct format_tbl {
399 { AFMT_IMA_ADPCM, 0 },
408 int i, n = sizeof (format2bits) / sizeof (struct format_tbl);
410 spin_lock_irqsave(&devc->lock, flags);
411 /* return current format */
413 arg = devc->audio_format;
414 spin_unlock_irqrestore(&devc->lock, flags);
417 devc->audio_format = arg;
419 /* search matching format bits */
420 for (i = 0; i < n; i++)
421 if (format2bits[i].format == arg) {
422 devc->format_bits = format2bits[i].bits;
423 devc->audio_format = arg;
424 spin_unlock_irqrestore(&devc->lock, flags);
428 /* Still hanging here. Something must be terribly wrong */
429 devc->format_bits = 0;
430 devc->audio_format = AFMT_U8;
431 spin_unlock_irqrestore(&devc->lock, flags);
435 static short ad1816_set_channels (int dev, short arg)
437 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
439 if (arg != 1 && arg != 2)
440 return devc->channels;
442 devc->channels = arg;
447 static int ad1816_open (int dev, int mode)
449 ad1816_info *devc = NULL;
452 /* is device number valid ? */
453 if (dev < 0 || dev >= num_audiodevs)
456 /* get device info of this dev */
457 devc = (ad1816_info *) audio_devs[dev]->devc;
459 /* make check if device already open atomic */
460 spin_lock_irqsave(&devc->lock,flags);
463 spin_unlock_irqrestore(&devc->lock,flags);
467 /* mark device as open */
470 devc->audio_mode = 0;
472 devc->audio_format=AFMT_U8;
474 spin_unlock_irqrestore(&devc->lock,flags);
475 ad1816_reset(devc->dev_no); /* halt all pending output */
479 static void ad1816_close (int dev) /* close device */
482 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
484 /* halt all pending output */
485 ad1816_reset(devc->dev_no);
487 spin_lock_irqsave(&devc->lock,flags);
489 devc->audio_mode = 0;
491 devc->audio_format=AFMT_U8;
492 devc->format_bits = 0;
493 spin_unlock_irqrestore(&devc->lock,flags);
497 /* ------------------------------------------------------------------- */
499 /* Audio driver structure */
501 static struct audio_driver ad1816_audio_driver =
503 .owner = THIS_MODULE,
505 .close = ad1816_close,
506 .output_block = ad1816_output_block,
507 .start_input = ad1816_start_input,
508 .prepare_for_input = ad1816_prepare_for_input,
509 .prepare_for_output = ad1816_prepare_for_output,
510 .halt_io = ad1816_halt,
511 .halt_input = ad1816_halt_input,
512 .halt_output = ad1816_halt_output,
513 .trigger = ad1816_trigger,
514 .set_speed = ad1816_set_speed,
515 .set_bits = ad1816_set_bits,
516 .set_channels = ad1816_set_channels,
520 /* ------------------------------------------------------------------- */
522 /* Interrupt handler */
525 static irqreturn_t ad1816_interrupt (int irq, void *dev_id, struct pt_regs *dummy)
527 unsigned char status;
528 ad1816_info *devc = (ad1816_info *)dev_id;
530 if (irq < 0 || irq > 15) {
531 printk(KERN_WARNING "ad1816: Got bogus interrupt %d\n", irq);
535 spin_lock(&devc->lock);
537 /* read interrupt register */
538 status = inb (devc->base+1);
539 /* Clear all interrupt */
540 outb (~status, devc->base+1);
542 DEBUGNOISE(printk(KERN_DEBUG "ad1816: Got interrupt subclass %d\n",status));
545 DEBUGNOISE(printk(KERN_DEBUG "ad1816: interrupt: Got interrupt, but no source.\n"));
546 spin_unlock(&devc->lock);
550 if (devc->opened && (devc->audio_mode & PCM_ENABLE_INPUT) && (status&64))
551 DMAbuf_inputintr (devc->dev_no);
553 if (devc->opened && (devc->audio_mode & PCM_ENABLE_OUTPUT) && (status & 128))
554 DMAbuf_outputintr (devc->dev_no, 1);
556 spin_unlock(&devc->lock);
560 /* ------------------------------------------------------------------- */
565 unsigned int regno: 7;
566 unsigned int polarity:1; /* 0=normal, 1=reversed */
567 unsigned int bitpos:4;
568 unsigned int nbits:4;
571 static char mix_cvt[101] = {
572 0, 0, 3, 7,10,13,16,19,21,23,26,28,30,32,34,35,37,39,40,42,
573 43,45,46,47,49,50,51,52,53,55,56,57,58,59,60,61,62,63,64,65,
574 65,66,67,68,69,70,70,71,72,73,73,74,75,75,76,77,77,78,79,79,
575 80,81,81,82,82,83,84,84,85,85,86,86,87,87,88,88,89,89,90,90,
576 91,91,92,92,93,93,94,94,95,95,96,96,96,97,97,98,98,98,99,99,
580 typedef struct mixer_def mixer_ent;
583 * Most of the mixer entries work in backwards. Setting the polarity field
584 * makes them to work correctly.
586 * The channel numbering used by individual soundcards is not fixed. Some
587 * cards have assigned different meanings for the AUX1, AUX2 and LINE inputs.
588 * The current version doesn't try to compensate this.
591 #define MIX_ENT(name, reg_l, pola_l, pos_l, len_l, reg_r, pola_r, pos_r, len_r) \
592 {{reg_l, pola_l, pos_l, len_l}, {reg_r, pola_r, pos_r, len_r}}
595 mixer_ent mix_devices[SOUND_MIXER_NRDEVICES][2] = {
596 MIX_ENT(SOUND_MIXER_VOLUME, 14, 1, 8, 5, 14, 1, 0, 5),
597 MIX_ENT(SOUND_MIXER_BASS, 0, 0, 0, 0, 0, 0, 0, 0),
598 MIX_ENT(SOUND_MIXER_TREBLE, 0, 0, 0, 0, 0, 0, 0, 0),
599 MIX_ENT(SOUND_MIXER_SYNTH, 5, 1, 8, 6, 5, 1, 0, 6),
600 MIX_ENT(SOUND_MIXER_PCM, 4, 1, 8, 6, 4, 1, 0, 6),
601 MIX_ENT(SOUND_MIXER_SPEAKER, 0, 0, 0, 0, 0, 0, 0, 0),
602 MIX_ENT(SOUND_MIXER_LINE, 18, 1, 8, 5, 18, 1, 0, 5),
603 MIX_ENT(SOUND_MIXER_MIC, 19, 1, 8, 5, 19, 1, 0, 5),
604 MIX_ENT(SOUND_MIXER_CD, 15, 1, 8, 5, 15, 1, 0, 5),
605 MIX_ENT(SOUND_MIXER_IMIX, 0, 0, 0, 0, 0, 0, 0, 0),
606 MIX_ENT(SOUND_MIXER_ALTPCM, 0, 0, 0, 0, 0, 0, 0, 0),
607 MIX_ENT(SOUND_MIXER_RECLEV, 20, 0, 8, 4, 20, 0, 0, 4),
608 MIX_ENT(SOUND_MIXER_IGAIN, 0, 0, 0, 0, 0, 0, 0, 0),
609 MIX_ENT(SOUND_MIXER_OGAIN, 0, 0, 0, 0, 0, 0, 0, 0),
610 MIX_ENT(SOUND_MIXER_LINE1, 17, 1, 8, 5, 17, 1, 0, 5),
611 MIX_ENT(SOUND_MIXER_LINE2, 16, 1, 8, 5, 16, 1, 0, 5),
612 MIX_ENT(SOUND_MIXER_LINE3, 39, 0, 9, 4, 39, 1, 0, 5)
616 static unsigned short default_mixer_levels[SOUND_MIXER_NRDEVICES] =
618 0x4343, /* Master Volume */
623 0x0000, /* PC Speaker */
624 0x0000, /* Ext Line */
627 0x0000, /* Recording monitor */
629 0x0000, /* Recording level */
630 0x0000, /* Input gain */
631 0x0000, /* Output gain */
634 0x0000 /* Line3 (usually line in)*/
643 ad1816_set_recmask (ad1816_info * devc, int mask)
646 unsigned char recdev;
649 spin_lock_irqsave(&devc->lock, flags);
650 mask &= devc->supported_rec_devices;
653 /* Count selected device bits */
654 for (i = 0; i < 32; i++)
659 mask = SOUND_MASK_MIC;
660 else if (n != 1) { /* Too many devices selected */
661 /* Filter out active settings */
662 mask &= ~devc->recmask;
665 /* Count selected device bits */
666 for (i = 0; i < 32; i++)
671 mask = SOUND_MASK_MIC;
679 case SOUND_MASK_LINE:
687 case SOUND_MASK_LINE1:
691 case SOUND_MASK_LINE2:
695 case SOUND_MASK_VOLUME:
700 mask = SOUND_MASK_MIC;
706 (ad_read (devc, 20) & 0x8f8f) | recdev | (recdev<<8));
708 devc->recmask = mask;
709 spin_unlock_irqrestore(&devc->lock, flags);
714 change_bits (int *regval, int dev, int chn, int newval)
719 /* Reverse polarity*/
721 if (mix_devices[dev][chn].polarity == 1)
722 newval = 100 - newval;
724 mask = (1 << mix_devices[dev][chn].nbits) - 1;
725 shift = mix_devices[dev][chn].bitpos;
727 newval = (int) ((newval * mask) + 50) / 100;
729 *regval &= ~(mask << shift);
731 *regval |= (newval & mask) << shift;
735 ad1816_mixer_get (ad1816_info * devc, int dev)
737 DEBUGNOISE(printk(KERN_DEBUG "ad1816: mixer_get called!\n"));
739 /* range check + supported mixer check */
740 if (dev < 0 || dev >= SOUND_MIXER_NRDEVICES )
742 if (!((1 << dev) & devc->supported_devices))
745 return devc->levels[dev];
749 ad1816_mixer_set (ad1816_info * devc, int dev, int value)
751 int left = value & 0x000000ff;
752 int right = (value & 0x0000ff00) >> 8;
760 DEBUGNOISE(printk(KERN_DEBUG "ad1816: mixer_set called!\n"));
762 if (dev < 0 || dev >= SOUND_MIXER_NRDEVICES )
775 if (mix_devices[dev][RIGHT_CHN].nbits == 0)
777 retvol = left | (right << 8);
781 left = mix_cvt[left];
782 right = mix_cvt[right];
784 /* reject all mixers that are not supported */
785 if (!(devc->supported_devices & (1 << dev)))
789 if (mix_devices[dev][LEFT_CHN].nbits == 0)
791 spin_lock_irqsave(&devc->lock, flags);
793 /* keep precise volume internal */
794 devc->levels[dev] = retvol;
796 /* Set the left channel */
797 regoffs = mix_devices[dev][LEFT_CHN].regno;
798 val = ad_read (devc, regoffs);
799 change_bits (&val, dev, LEFT_CHN, left);
803 /* Mute bit masking on some registers */
804 if ( regoffs==5 || regoffs==14 || regoffs==15 ||
805 regoffs==16 || regoffs==17 || regoffs==18 ||
806 regoffs==19 || regoffs==39) {
812 ad_write (devc, regoffs, valmute); /* mute */
815 * Set the right channel
818 /* Was just a mono channel */
819 if (mix_devices[dev][RIGHT_CHN].nbits == 0) {
820 spin_unlock_irqrestore(&devc->lock, flags);
824 regoffs = mix_devices[dev][RIGHT_CHN].regno;
825 val = ad_read (devc, regoffs);
826 change_bits (&val, dev, RIGHT_CHN, right);
829 if ( regoffs==5 || regoffs==14 || regoffs==15 ||
830 regoffs==16 || regoffs==17 || regoffs==18 ||
831 regoffs==19 || regoffs==39) {
837 ad_write (devc, regoffs, valmute); /* mute */
838 spin_unlock_irqrestore(&devc->lock, flags);
842 #define MIXER_DEVICES ( SOUND_MASK_VOLUME | \
853 #define REC_DEVICES ( SOUND_MASK_LINE2 |\
862 ad1816_mixer_reset (ad1816_info * devc)
866 devc->supported_devices = MIXER_DEVICES;
868 devc->supported_rec_devices = REC_DEVICES;
870 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
871 if (devc->supported_devices & (1 << i))
872 ad1816_mixer_set (devc, i, default_mixer_levels[i]);
873 ad1816_set_recmask (devc, SOUND_MASK_MIC);
877 ad1816_mixer_ioctl (int dev, unsigned int cmd, caddr_t arg)
879 ad1816_info *devc = mixer_devs[dev]->devc;
882 DEBUGNOISE(printk(KERN_DEBUG "ad1816: mixer_ioctl called!\n"));
885 if (((cmd >> 8) & 0xff) == 'M') {
888 if (_SIOC_DIR (cmd) & _SIOC_WRITE) {
890 case SOUND_MIXER_RECSRC:
892 if (get_user(val, (int *)arg))
894 val=ad1816_set_recmask (devc, val);
895 return put_user(val, (int *)arg);
899 if (get_user(val, (int *)arg))
901 if ((val=ad1816_mixer_set (devc, cmd & 0xff, val))<0)
904 return put_user(val, (int *)arg);
908 switch (cmd & 0xff) {
910 case SOUND_MIXER_RECSRC:
912 return put_user(val, (int *)arg);
915 case SOUND_MIXER_DEVMASK:
916 val=devc->supported_devices;
917 return put_user(val, (int *)arg);
920 case SOUND_MIXER_STEREODEVS:
921 val=devc->supported_devices & ~(SOUND_MASK_SPEAKER | SOUND_MASK_IMIX);
922 return put_user(val, (int *)arg);
925 case SOUND_MIXER_RECMASK:
926 val=devc->supported_rec_devices;
927 return put_user(val, (int *)arg);
930 case SOUND_MIXER_CAPS:
931 val=SOUND_CAP_EXCL_INPUT;
932 return put_user(val, (int *)arg);
936 if ((val=ad1816_mixer_get (devc, cmd & 0xff))<0)
939 return put_user(val, (int *)arg);
947 /* ------------------------------------------------------------------- */
949 /* Mixer structure */
951 static struct mixer_operations ad1816_mixer_operations = {
952 .owner = THIS_MODULE,
954 .name = "AD1816 Mixer",
955 .ioctl = ad1816_mixer_ioctl
959 /* ------------------------------------------------------------------- */
961 /* stuff for card recognition, init and unloading PNP ...*/
964 /* check if AD1816 present at specified hw_config and register device with OS
965 * return 1 if initialization was successful, 0 otherwise
967 static int __init ad1816_init_card (struct address_info *hw_config,
970 ad1816_info *devc = NULL;
974 printk(KERN_INFO "ad1816: initializing card: io=0x%x, irq=%d, dma=%d, "
975 "dma2=%d, clockfreq=%d, options=%d isadmabug=%d "
983 isa_dma_bridge_buggy,
986 /* ad1816_info structure remaining ? */
987 if (nr_ad1816_devs >= MAX_AUDIO_DEV) {
988 printk(KERN_WARNING "ad1816: no more ad1816_info structures "
993 devc = &dev_info[nr_ad1816_devs];
994 devc->base = hw_config->io_base;
995 devc->irq = hw_config->irq;
996 devc->dma_playback=hw_config->dma;
997 devc->dma_capture=hw_config->dma2;
1000 spin_lock_init(&devc->lock);
1002 if (!request_region(devc->base, 16, "AD1816 Sound")) {
1003 printk(KERN_WARNING "ad1816: I/O port 0x%03x not free\n",
1008 printk(KERN_INFO "ad1816: Examining AD1816 at address 0x%03x.\n",
1012 /* tests for ad1816 */
1013 /* base+0: bit 1 must be set but not 255 */
1014 tmp=inb(devc->base);
1015 if ( (tmp&0x80)==0 || tmp==255 ) {
1016 printk (KERN_INFO "ad1816: Chip is not an AD1816 or chip "
1017 "is not active (Test 0)\n");
1018 goto out_release_region;
1021 /* writes to ireg 8 are copied to ireg 9 */
1022 ad_write(devc,8,12345);
1023 if (ad_read(devc,9)!=12345) {
1024 printk(KERN_INFO "ad1816: Chip is not an AD1816 (Test 1)\n");
1025 goto out_release_region;
1028 /* writes to ireg 8 are copied to ireg 9 */
1029 ad_write(devc,8,54321);
1030 if (ad_read(devc,9)!=54321) {
1031 printk(KERN_INFO "ad1816: Chip is not an AD1816 (Test 2)\n");
1032 goto out_release_region;
1035 /* writes to ireg 10 are copied to ireg 11 */
1036 ad_write(devc,10,54321);
1037 if (ad_read(devc,11)!=54321) {
1038 printk (KERN_INFO "ad1816: Chip is not an AD1816 (Test 3)\n");
1039 goto out_release_region;
1042 /* writes to ireg 10 are copied to ireg 11 */
1043 ad_write(devc,10,12345);
1044 if (ad_read(devc,11)!=12345) {
1045 printk (KERN_INFO "ad1816: Chip is not an AD1816 (Test 4)\n");
1046 goto out_release_region;
1049 /* bit in base +1 cannot be set to 1 */
1050 tmp=inb(devc->base+1);
1051 outb(0xff,devc->base+1);
1052 if (inb(devc->base+1)!=tmp) {
1053 printk(KERN_INFO "ad1816: Chip is not an AD1816 (Test 5)\n");
1054 goto out_release_region;
1057 printk(KERN_INFO "ad1816: AD1816 (version %d) successfully detected!\n",
1060 /* disable all interrupts */
1063 /* Clear pending interrupts */
1064 outb (0, devc->base+1);
1067 if (devc->irq < 0 || devc->irq > 15)
1068 goto out_release_region;
1069 if (request_irq(devc->irq, ad1816_interrupt,0,
1070 "SoundPort", devc) < 0) {
1071 printk(KERN_WARNING "ad1816: IRQ in use\n");
1072 goto out_release_region;
1076 if (sound_alloc_dma (devc->dma_playback, "Sound System")) {
1077 printk(KERN_WARNING "ad1816: Can't allocate DMA%d\n",
1078 devc->dma_playback);
1082 if ( devc->dma_capture >= 0 &&
1083 devc->dma_capture != devc->dma_playback) {
1084 if (sound_alloc_dma(devc->dma_capture,
1085 "Sound System (capture)")) {
1086 printk(KERN_WARNING "ad1816: Can't allocate DMA%d\n",
1090 devc->audio_mode=DMA_AUTOMODE|DMA_DUPLEX;
1092 printk(KERN_WARNING "ad1816: Only one DMA channel "
1093 "available/configured. No duplex operation possible\n");
1094 devc->audio_mode=DMA_AUTOMODE;
1097 conf_printf2 ("AD1816 audio driver",
1098 devc->base, devc->irq, devc->dma_playback,
1101 /* register device */
1102 if ((oss_devno = sound_install_audiodrv (AUDIO_DRIVER_VERSION,
1103 "AD1816 audio driver",
1104 &ad1816_audio_driver,
1105 sizeof (struct audio_driver),
1110 devc->dma_capture)) < 0) {
1111 printk(KERN_WARNING "ad1816: Can't install sound driver\n");
1112 goto out_free_dma_2;
1116 ad_write(devc,32,0x80f0); /* sound system mode */
1118 ad_write(devc,33,0); /* disable all audiosources for dsp */
1120 ad_write(devc,33,0x03f8); /* enable all audiosources for dsp */
1122 ad_write(devc,4,0x8080); /* default values for volumes (muted)*/
1123 ad_write(devc,5,0x8080);
1124 ad_write(devc,6,0x8080);
1125 ad_write(devc,7,0x8080);
1126 ad_write(devc,15,0x8888);
1127 ad_write(devc,16,0x8888);
1128 ad_write(devc,17,0x8888);
1129 ad_write(devc,18,0x8888);
1130 ad_write(devc,19,0xc888); /* +20db mic active */
1131 ad_write(devc,14,0x0000); /* Master volume unmuted */
1132 ad_write(devc,39,0x009f); /* 3D effect on 0% phone out muted */
1133 ad_write(devc,44,0x0080); /* everything on power, 3d enabled for d/a */
1134 outb(0x10,devc->base+8); /* set dma mode */
1135 outb(0x10,devc->base+9);
1137 /* enable capture + playback interrupt */
1138 ad_write(devc,1,0xc000);
1140 /* set mixer defaults */
1141 ad1816_mixer_reset (devc);
1143 /* register mixer */
1144 if ((audio_devs[oss_devno]->mixer_dev=sound_install_mixer(
1145 MIXER_DRIVER_VERSION,
1146 "AD1816 audio driver",
1147 &ad1816_mixer_operations,
1148 sizeof (struct mixer_operations),
1150 printk(KERN_WARNING "Can't install mixer\n");
1152 /* make ad1816_info active */
1154 printk(KERN_INFO "ad1816: card successfully installed!\n");
1156 /* error handling */
1158 if (devc->dma_capture >= 0 && devc->dma_capture != devc->dma_playback)
1159 sound_free_dma(devc->dma_capture);
1161 sound_free_dma(devc->dma_playback);
1163 free_irq(devc->irq, devc);
1165 release_region(devc->base, 16);
1170 static void __exit unload_card(ad1816_info *devc)
1175 printk("ad1816: Unloading card at address 0x%03x\n",devc->base);
1178 mixer = audio_devs[dev]->mixer_dev;
1182 sound_unload_mixerdev(mixer);
1184 /* unreg audiodev */
1185 sound_unload_audiodev(dev);
1187 /* free dma channels */
1188 if (devc->dma_capture>=0 &&
1189 devc->dma_capture != devc->dma_playback) {
1190 sound_free_dma(devc->dma_capture);
1192 sound_free_dma (devc->dma_playback);
1194 free_irq(devc->irq, devc);
1196 release_region (devc->base, 16);
1199 pnp_disable_dev(devc->pnpdev);
1200 pnp_device_detach(devc->pnpdev);
1205 printk(KERN_WARNING "ad1816: no device/card specified\n");
1208 static int __initdata io = -1;
1209 static int __initdata irq = -1;
1210 static int __initdata dma = -1;
1211 static int __initdata dma2 = -1;
1214 /* use isapnp for configuration */
1215 static int isapnp = 1;
1216 static int isapnpjump;
1217 MODULE_PARM(isapnp, "i");
1218 MODULE_PARM(isapnpjump, "i");
1221 MODULE_PARM(io,"i");
1222 MODULE_PARM(irq,"i");
1223 MODULE_PARM(dma,"i");
1224 MODULE_PARM(dma2,"i");
1225 MODULE_PARM(ad1816_clockfreq,"i");
1226 MODULE_PARM(options,"i");
1230 unsigned short card_vendor, card_device;
1231 unsigned short vendor;
1232 unsigned short function;
1233 struct ad1816_data *data;
1234 } isapnp_ad1816_list[] __initdata = {
1235 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
1236 ISAPNP_VENDOR('A','D','S'), ISAPNP_FUNCTION(0x7150),
1238 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
1239 ISAPNP_VENDOR('A','D','S'), ISAPNP_FUNCTION(0x7180),
1244 MODULE_DEVICE_TABLE(isapnp, isapnp_ad1816_list);
1247 void __init ad1816_config_pnp_card(struct pnp_card *card, unsigned short vendor,
1248 unsigned short function)
1250 struct address_info cfg;
1251 struct pnp_dev *card_dev = pnp_find_dev(card, vendor, function, NULL);
1252 if (!card_dev) return;
1253 if (pnp_device_attach(card_dev) < 0) {
1254 printk(KERN_WARNING "ad1816: Failed to attach PnP device\n");
1257 if (pnp_activate_dev(card_dev) < 0) {
1258 printk(KERN_WARNING "ad1816: Failed to activate PnP device\n");
1259 pnp_device_detach(card_dev);
1262 cfg.io_base = pnp_port_start(card_dev, 2);
1263 cfg.irq = pnp_irq(card_dev, 0);
1264 cfg.dma = pnp_irq(card_dev, 0);
1265 cfg.dma2 = pnp_irq(card_dev, 1);
1266 if (!ad1816_init_card(&cfg, card_dev)) {
1267 pnp_disable_dev(card_dev);
1268 pnp_device_detach(card_dev);
1272 void __init ad1816_config_pnp_cards(void)
1277 /* Count entries in isapnp_ad1816_list */
1278 for (nr_pnp_cfg = 0; isapnp_ad1816_list[nr_pnp_cfg].card_vendor != 0;
1280 /* Check and adjust isapnpjump */
1281 if( isapnpjump < 0 || isapnpjump >= nr_pnp_cfg) {
1283 "ad1816: Valid range for isapnpjump is 0-%d. "
1284 "Adjusted to 0.\n", nr_pnp_cfg-1);
1287 for (i = isapnpjump; isapnp_ad1816_list[i].card_vendor != 0; i++) {
1288 struct pnp_card *card = NULL;
1289 /* iterate over all pnp cards */
1290 while ((card = pnp_find_card(isapnp_ad1816_list[i].card_vendor,
1291 isapnp_ad1816_list[i].card_device, card)))
1292 ad1816_config_pnp_card(card,
1293 isapnp_ad1816_list[i].vendor,
1294 isapnp_ad1816_list[i].function);
1299 /* module initialization */
1300 static int __init init_ad1816(void)
1302 printk(KERN_INFO "ad1816: AD1816 sounddriver "
1303 "Copyright (C) 1998-2003 by Thorsten Knabe and "
1306 /* set ad1816_clockfreq if set during compilation */
1307 ad1816_clockfreq=AD1816_CLOCK;
1309 if (ad1816_clockfreq<5000 || ad1816_clockfreq>100000) {
1310 ad1816_clockfreq=33000;
1314 /* configure PnP cards */
1315 if(isapnp) ad1816_config_pnp_cards();
1317 /* configure card by module params */
1318 if (io != -1 && irq != -1 && dma != -1) {
1319 struct address_info cfg;
1324 ad1816_init_card(&cfg, NULL);
1326 if (nr_ad1816_devs <= 0)
1331 /* module cleanup */
1332 static void __exit cleanup_ad1816 (void)
1335 ad1816_info *devc = NULL;
1337 /* remove any soundcard */
1338 for (i = 0; i < nr_ad1816_devs; i++) {
1339 devc = &dev_info[i];
1343 printk(KERN_INFO "ad1816: driver unloaded!\n");
1346 module_init(init_ad1816);
1347 module_exit(cleanup_ad1816);
1350 /* kernel command line parameter evaluation */
1351 static int __init setup_ad1816(char *str)
1353 /* io, irq, dma, dma2 */
1356 str = get_options(str, ARRAY_SIZE(ints), ints);
1365 __setup("ad1816=", setup_ad1816);
1367 MODULE_LICENSE("GPL");