Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / sound / ad1816.c
blobbe1aec5a583e5cb9722d37dec3575ef8a33bdaf6
1 /*
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
10 * version: 1.3.1
11 * status: experimental
12 * date: 1999/4/18
14 * Changes:
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
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>
39 #include "sound_config.h"
41 #define DEBUGNOISE(x)
42 #define DEBUGINFO(x)
43 #define DEBUGLOG(x)
44 #define DEBUGWARN(x)
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 } \
55 /* structure to hold device specific information */
56 typedef struct
58 int base; /* set in attach */
59 int irq;
60 int dma_playback;
61 int dma_capture;
63 int speed; /* open */
64 int channels;
65 int audio_format;
66 unsigned char format_bits;
67 int audio_mode;
68 int opened;
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;
79 } ad1816_info;
81 static int nr_ad1816_devs = 0;
82 static int ad1816_clockfreq=33000;
83 static int options=0;
85 /* for backward mapping of irq to sound device */
87 static volatile char irq2dev[17] = {-1, -1, -1, -1, -1, -1, -1, -1,
88 -1, -1, -1, -1, -1, -1, -1, -1, -1};
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;
95 /* array of device info structures */
96 static ad1816_info dev_info[MAX_AUDIO_DEV];
99 /* ------------------------------------------------------------------- */
101 /* functions for easier access to inderect registers */
103 static int ad_read (ad1816_info * devc, int reg)
105 unsigned long flags;
106 int result;
108 CHECK_FOR_POWER;
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);
117 return (result);
121 static void ad_write (ad1816_info * devc, int reg, int data)
123 unsigned long flags;
125 CHECK_FOR_POWER;
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);
136 /* ------------------------------------------------------------------- */
138 /* function interface required by struct audio_driver */
140 static void ad1816_halt_input (int dev)
142 unsigned long flags;
143 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
144 unsigned char buffer;
146 DEBUGINFO (printk("ad1816: halt_input called\n"));
148 save_flags (flags);
149 cli ();
151 if(!isa_dma_bridge_buggy) {
152 disable_dma(audio_devs[dev]->dmap_in->dma);
155 buffer=inb(devc->base+9);
156 if (buffer & 0x01) {
157 /* disable capture */
158 outb(buffer & ~0x01,devc->base+9);
161 if(!isa_dma_bridge_buggy) {
162 enable_dma(audio_devs[dev]->dmap_in->dma);
165 /* Clear interrupt status */
166 outb (~0x40, devc->base+1);
168 devc->audio_mode &= ~PCM_ENABLE_INPUT;
169 restore_flags (flags);
172 static void ad1816_halt_output (int dev)
174 unsigned long flags;
175 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
177 unsigned char buffer;
179 DEBUGINFO (printk("ad1816: halt_output called!\n"));
181 save_flags (flags);
182 cli ();
183 /* Mute pcm output */
184 ad_write(devc, 4, ad_read(devc,4)|0x8080);
186 if(!isa_dma_bridge_buggy) {
187 disable_dma(audio_devs[dev]->dmap_out->dma);
190 buffer=inb(devc->base+8);
191 if (buffer & 0x01) {
192 /* disable capture */
193 outb(buffer & ~0x01,devc->base+8);
196 if(!isa_dma_bridge_buggy) {
197 enable_dma(audio_devs[dev]->dmap_out->dma);
200 /* Clear interrupt status */
201 outb ((unsigned char)~0x80, devc->base+1);
203 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
204 restore_flags (flags);
207 static void ad1816_output_block (int dev, unsigned long buf,
208 int count, int intrflag)
210 unsigned long flags;
211 unsigned long cnt;
212 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
214 DEBUGINFO(printk("ad1816: output_block called buf=%ld count=%d flags=%d\n",buf,count,intrflag));
216 cnt = count/4 - 1;
218 save_flags (flags);
219 cli ();
221 /* set transfer count */
222 ad_write (devc, 8, cnt & 0xffff);
224 devc->audio_mode |= PCM_ENABLE_OUTPUT;
225 restore_flags (flags);
229 static void ad1816_start_input (int dev, unsigned long buf, int count,
230 int intrflag)
232 unsigned long flags;
233 unsigned long cnt;
234 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
236 DEBUGINFO(printk("ad1816: start_input called buf=%ld count=%d flags=%d\n",buf,count,intrflag));
238 cnt = count/4 - 1;
240 save_flags (flags); /* make register access atomic */
241 cli ();
243 /* set transfer count */
244 ad_write (devc, 10, cnt & 0xffff);
246 devc->audio_mode |= PCM_ENABLE_INPUT;
247 restore_flags (flags);
250 static int ad1816_prepare_for_input (int dev, int bsize, int bcount)
252 unsigned long flags;
253 unsigned int freq;
254 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
255 unsigned char fmt_bits;
257 DEBUGINFO (printk ("ad1816: prepare_for_input called: bsize=%d bcount=%d\n",bsize,bcount));
259 save_flags (flags);
260 cli ();
262 fmt_bits= (devc->format_bits&0x7)<<3;
264 /* set mono/stereo mode */
265 if (devc->channels > 1) {
266 fmt_bits |=0x4;
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);
273 /* If compiled into kernel, AD1816_CLOCK is defined, so use it */
274 #ifdef AD1816_CLOCK
275 ad1816_clockfreq=AD1816_CLOCK;
276 #endif
278 /* capture/playback frequency correction for soundcards
279 with clock chips != 33MHz (allowed range 5 - 100 kHz) */
281 if (ad1816_clockfreq<5000 || ad1816_clockfreq>100000) {
282 ad1816_clockfreq=33000;
285 freq=((unsigned int)devc->speed*33000)/ad1816_clockfreq;
287 /* write playback/capture speeds */
288 ad_write (devc, 2, freq & 0xffff);
289 ad_write (devc, 3, freq & 0xffff);
291 restore_flags (flags);
293 ad1816_halt_input(dev);
294 return 0;
297 static int ad1816_prepare_for_output (int dev, int bsize, int bcount)
299 unsigned long flags;
300 unsigned int freq;
301 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
302 unsigned char fmt_bits;
304 DEBUGINFO (printk ("ad1816: prepare_for_output called: bsize=%d bcount=%d\n",bsize,bcount));
306 save_flags (flags); /* make register access atomic */
307 cli ();
309 fmt_bits= (devc->format_bits&0x7)<<3;
310 /* set mono/stereo mode */
311 if (devc->channels > 1) {
312 fmt_bits |=0x4;
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);
319 #ifdef AD1816_CLOCK
320 ad1816_clockfreq=AD1816_CLOCK;
321 #endif
323 /* capture/playback frequency correction for soundcards
324 with clock chips != 33MHz (allowed range 5 - 100 kHz)*/
326 if (ad1816_clockfreq<5000 || ad1816_clockfreq>100000) {
327 ad1816_clockfreq=33000;
330 freq=((unsigned int)devc->speed*33000)/ad1816_clockfreq;
332 /* write playback/capture speeds */
333 ad_write (devc, 2, freq & 0xffff);
334 ad_write (devc, 3, freq & 0xffff);
336 restore_flags (flags);
338 ad1816_halt_output(dev);
339 return 0;
343 static void ad1816_trigger (int dev, int state)
345 unsigned long flags;
346 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
348 DEBUGINFO (printk("ad1816: trigger called! (devc=%d,devc->base=%d\n", devc, devc->base));
350 /* mode may have changed */
352 save_flags (flags); /* make register access atomic */
353 cli ();
355 /* mask out modes not specified on open call */
356 state &= devc->audio_mode;
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);
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);
378 restore_flags (flags);
382 /* halt input & output */
383 static void ad1816_halt (int dev)
385 ad1816_halt_input(dev);
386 ad1816_halt_output(dev);
389 static void ad1816_reset (int dev)
391 ad1816_halt (dev);
394 /* set playback speed */
395 static int ad1816_set_speed (int dev, int arg)
397 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
399 if (arg == 0) {
400 return devc->speed;
402 /* range checking */
403 if (arg < 4000) {
404 arg = 4000;
406 if (arg > 55000) {
407 arg = 55000;
410 devc->speed = arg;
411 return devc->speed;
415 static unsigned int ad1816_set_bits (int dev, unsigned int arg)
417 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
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 }
435 int i, n = sizeof (format2bits) / sizeof (struct format_tbl);
437 /* return current format */
438 if (arg == 0)
439 return devc->audio_format;
441 devc->audio_format = arg;
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;
451 /* Still hanging here. Something must be terribly wrong */
452 devc->format_bits = 0;
453 return devc->audio_format = AFMT_U8;
456 static short ad1816_set_channels (int dev, short arg)
458 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
460 if (arg != 1 && arg != 2)
461 return devc->channels;
463 devc->channels = arg;
464 return arg;
467 /* open device */
468 static int ad1816_open (int dev, int mode)
470 ad1816_info *devc = NULL;
471 unsigned long flags;
473 /* is device number valid ? */
474 if (dev < 0 || dev >= num_audiodevs)
475 return -(ENXIO);
477 /* get device info of this dev */
478 devc = (ad1816_info *) audio_devs[dev]->devc;
480 /* make check if device already open atomic */
481 save_flags (flags);
482 cli ();
484 if (devc->opened) {
485 restore_flags (flags);
486 return -(EBUSY);
489 /* mark device as open */
490 devc->opened = 1;
492 devc->audio_mode = 0;
493 devc->speed = 8000;
494 devc->audio_format=AFMT_U8;
495 devc->channels=1;
497 ad1816_reset(devc->dev_no); /* halt all pending output */
498 restore_flags (flags);
499 return 0;
502 static void ad1816_close (int dev) /* close device */
504 unsigned long flags;
505 ad1816_info *devc = (ad1816_info *) audio_devs[dev]->devc;
507 save_flags (flags);
508 cli ();
510 /* halt all pending output */
511 ad1816_reset(devc->dev_no);
513 devc->opened = 0;
514 devc->audio_mode = 0;
515 devc->speed = 8000;
516 devc->audio_format=AFMT_U8;
517 devc->format_bits = 0;
520 restore_flags (flags);
524 /* ------------------------------------------------------------------- */
526 /* Audio driver structure */
528 static struct audio_driver ad1816_audio_driver =
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,
547 /* ------------------------------------------------------------------- */
549 /* Interrupt handler */
552 static void ad1816_interrupt (int irq, void *dev_id, struct pt_regs *dummy)
554 unsigned char status;
555 ad1816_info *devc;
556 int dev;
557 unsigned long flags;
560 if (irq < 0 || irq > 15) {
561 printk ("ad1816: Got bogus interrupt %d\n", irq);
562 return;
565 dev = irq2dev[irq];
567 if (dev < 0 || dev >= num_audiodevs) {
568 printk ("ad1816: IRQ2AD1816-mapping failed for irq %d device %d\n", irq,dev);
569 return;
572 devc = (ad1816_info *) audio_devs[dev]->devc;
574 save_flags(flags);
575 cli();
577 /* read interrupt register */
578 status = inb (devc->base+1);
579 /* Clear all interrupt */
580 outb (~status, devc->base+1);
582 DEBUGNOISE (printk("ad1816: Got interrupt subclass %d\n",status));
584 devc->irq_ok=1;
586 if (status == 0)
587 DEBUGWARN(printk ("ad1816: interrupt: Got interrupt, but no reason?\n"));
589 if (devc->opened && (devc->audio_mode & PCM_ENABLE_INPUT) && (status&64))
590 DMAbuf_inputintr (dev);
592 if (devc->opened && (devc->audio_mode & PCM_ENABLE_OUTPUT) && (status & 128))
593 DMAbuf_outputintr (dev, 1);
595 restore_flags(flags);
598 /* ------------------------------------------------------------------- */
600 /* Mixer stuff */
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;
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,
618 typedef struct mixer_def mixer_ent;
621 * Most of the mixer entries work in backwards. Setting the polarity field
622 * makes them to work correctly.
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.
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}}
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)
654 static unsigned short default_mixer_levels[SOUND_MIXER_NRDEVICES] =
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)*/
675 #define LEFT_CHN 0
676 #define RIGHT_CHN 1
680 static int
681 ad1816_set_recmask (ad1816_info * devc, int mask)
683 unsigned char recdev;
684 int i, n;
686 mask &= devc->supported_rec_devices;
688 n = 0;
689 /* Count selected device bits */
690 for (i = 0; i < 32; i++)
691 if (mask & (1 << i))
692 n++;
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;
700 n = 0;
701 /* Count selected device bits */
702 for (i = 0; i < 32; i++)
703 if (mask & (1 << i))
704 n++;
706 if (n != 1)
707 mask = SOUND_MASK_MIC;
710 switch (mask) {
711 case SOUND_MASK_MIC:
712 recdev = 5;
713 break;
715 case SOUND_MASK_LINE:
716 recdev = 0;
717 break;
719 case SOUND_MASK_CD:
720 recdev = 2;
721 break;
723 case SOUND_MASK_LINE1:
724 recdev = 4;
725 break;
727 case SOUND_MASK_LINE2:
728 recdev = 3;
729 break;
731 case SOUND_MASK_VOLUME:
732 recdev = 1;
733 break;
735 default:
736 mask = SOUND_MASK_MIC;
737 recdev = 5;
740 recdev <<= 4;
741 ad_write (devc, 20,
742 (ad_read (devc, 20) & 0x8f8f) | recdev | (recdev<<8));
744 devc->recmask = mask;
745 return mask;
748 static void
749 change_bits (int *regval, int dev, int chn, int newval)
751 unsigned char mask;
752 int shift;
754 /* Reverse polarity*/
756 if (mix_devices[dev][chn].polarity == 1)
757 newval = 100 - newval;
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;
769 static int
770 ad1816_mixer_get (ad1816_info * devc, int dev)
772 DEBUGINFO(printk("ad1816: mixer_get called!\n"));
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);
780 return devc->levels[dev];
783 static int
784 ad1816_mixer_set (ad1816_info * devc, int dev, int value)
786 int left = value & 0x000000ff;
787 int right = (value & 0x0000ff00) >> 8;
788 int retvol;
790 int regoffs;
791 int val;
792 int valmute;
794 DEBUGINFO(printk("ad1816: mixer_set called!\n"));
796 if (dev < 0 || dev >= SOUND_MIXER_NRDEVICES )
797 return -(EINVAL);
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;
808 /* Mono control */
809 if (mix_devices[dev][RIGHT_CHN].nbits == 0)
810 right = left;
811 retvol = left | (right << 8);
813 /* Scale it */
815 left = mix_cvt[left];
816 right = mix_cvt[right];
818 /* reject all mixers that are not supported */
819 if (!(devc->supported_devices & (1 << dev)))
820 return -(EINVAL);
822 /* sanity check */
823 if (mix_devices[dev][LEFT_CHN].nbits == 0)
824 return -(EINVAL);
826 /* keep precise volume internal */
827 devc->levels[dev] = retvol;
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);
834 valmute=val;
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;
845 ad_write (devc, regoffs, valmute); /* mute */
848 * Set the right channel
851 /* Was just a mono channel */
852 if (mix_devices[dev][RIGHT_CHN].nbits == 0)
853 return retvol;
855 regoffs = mix_devices[dev][RIGHT_CHN].regno;
856 val = ad_read (devc, regoffs);
857 change_bits (&val, dev, RIGHT_CHN, right);
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;
868 ad_write (devc, regoffs, valmute); /* mute */
870 return retvol;
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 \
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 \
892 static void
893 ad1816_mixer_reset (ad1816_info * devc)
895 int i;
897 devc->supported_devices = MIXER_DEVICES;
899 devc->supported_rec_devices = REC_DEVICES;
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);
907 static int
908 ad1816_mixer_ioctl (int dev, unsigned int cmd, caddr_t arg)
910 ad1816_info *devc = mixer_devs[dev]->devc;
911 int val;
913 DEBUGINFO(printk("ad1816: mixer_ioctl called!\n"));
915 /* Mixer ioctl */
916 if (((cmd >> 8) & 0xff) == 'M') {
918 /* set ioctl */
919 if (_SIOC_DIR (cmd) & _SIOC_WRITE) {
920 switch (cmd & 0xff){
921 case SOUND_MIXER_RECSRC:
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;
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);
937 } else {
938 /* read ioctl */
939 switch (cmd & 0xff) {
941 case SOUND_MIXER_RECSRC:
942 val=devc->recmask;
943 return put_user(val, (int *)arg);
944 break;
946 case SOUND_MIXER_DEVMASK:
947 val=devc->supported_devices;
948 return put_user(val, (int *)arg);
949 break;
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;
956 case SOUND_MIXER_RECMASK:
957 val=devc->supported_rec_devices;
958 return put_user(val, (int *)arg);
959 break;
961 case SOUND_MIXER_CAPS:
962 val=SOUND_CAP_EXCL_INPUT;
963 return put_user(val, (int *)arg);
964 break;
966 default:
967 if ((val=ad1816_mixer_get (devc, cmd & 0xff))<0)
968 return val;
969 else
970 return put_user(val, (int *)arg);
973 } else
974 /* not for mixer */
975 return -(EINVAL);
978 /* ------------------------------------------------------------------- */
980 /* Mixer structure */
982 static struct mixer_operations ad1816_mixer_operations = {
983 owner: THIS_MODULE,
984 id: "AD1816",
985 name: "AD1816 Mixer",
986 ioctl: ad1816_mixer_ioctl
990 /* ------------------------------------------------------------------- */
992 /* stuff for card recognition, init and unloading */
995 /* replace with probe routine */
996 static int __init probe_ad1816 ( struct address_info *hw_config )
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;
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);
1013 if (check_region (io_base, 16)) {
1014 printk ("ad1816: I/O port 0x%03x not free\n", io_base);
1015 return 0;
1018 DEBUGLOG(printk ("ad1816: detect(%x)\n", io_base));
1020 if (nr_ad1816_devs >= MAX_AUDIO_DEV) {
1021 printk ("ad1816: detect error - step 0\n");
1022 return 0;
1025 devc->base = io_base;
1026 devc->irq_ok = 0;
1027 devc->irq = 0;
1028 devc->opened = 0;
1029 devc->osp = osp;
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);
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);
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);
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);
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);
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);
1077 DEBUGLOG (printk ("ad1816: detect() - Detected OK\n"));
1078 DEBUGLOG (printk ("ad1816: AD1816 Version: %d\n",ad_read(devc,45)));
1080 /* detection was successful */
1081 return 1;
1085 /* allocate resources from the kernel. If any allocation fails, free
1086 all allocated resources and exit attach.
1090 static void __init attach_ad1816 (struct address_info *hw_config)
1092 int my_dev;
1093 char dev_name[100];
1094 ad1816_info *devc = &dev_info[nr_ad1816_devs];
1097 /* allocate i/o ports */
1098 request_region (hw_config->io_base, 16, "AD1816 Sound");
1099 devc->base = hw_config->io_base;
1101 /* disable all interrupts */
1102 ad_write(devc,1,0);
1104 /* Clear pending interrupts */
1105 outb (0, devc->base+1);
1107 /* allocate irq */
1108 if (hw_config->irq < 0 || hw_config->irq > 15) {
1109 release_region(hw_config->io_base, 16);
1110 return;
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;
1119 devc->irq=hw_config->irq;
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;
1128 devc->dma_playback=hw_config->dma;
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;
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;
1145 sprintf (dev_name,"AD1816 audio driver");
1147 conf_printf2 (dev_name,
1148 devc->base, devc->irq, hw_config->dma, hw_config->dma2);
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);
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;
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++;
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 */
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);
1199 /* enable capture + playback interrupt */
1200 ad_write(devc,1,0xc000);
1202 /* set mixer defaults */
1203 ad1816_mixer_reset (devc);
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;
1216 static void __exit unload_card(ad1816_info *devc)
1218 int mixer, dev = 0;
1220 if (devc != NULL) {
1221 DEBUGLOG (printk("ad1816: Unloading card at base=%x\n",devc->base));
1223 dev = devc->dev_no;
1224 mixer = audio_devs[dev]->mixer_dev;
1226 /* unreg mixer*/
1227 if(mixer>=0) {
1228 sound_unload_mixerdev(mixer);
1230 sound_unload_audiodev(dev);
1232 /* free dma channels */
1233 if (devc->dma_capture>=0) {
1234 sound_free_dma(devc->dma_capture);
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);
1243 DEBUGLOG (printk("ad1816: Unloading card at base=%x was successful\n",devc->base));
1245 } else {
1246 printk ("ad1816: no device/card specified\n");
1250 static struct address_info cfg;
1252 static int __initdata io = -1;
1253 static int __initdata irq = -1;
1254 static int __initdata dma = -1;
1255 static int __initdata dma2 = -1;
1257 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
1258 struct pci_dev *ad1816_dev = NULL;
1260 static int activated = 1;
1262 static int isapnp = 1;
1263 static int isapnpjump = 0;
1265 MODULE_PARM(isapnp, "i");
1266 MODULE_PARM(isapnpjump, "i");
1268 #else
1269 static int isapnp = 0;
1270 #endif
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");
1279 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
1281 static struct pci_dev *activate_dev(char *devname, char *resname, struct pci_dev *dev)
1283 int err;
1285 if(dev->active) {
1286 activated = 0;
1287 return(dev);
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);
1297 return(dev);
1300 static struct pci_dev *ad1816_init_generic(struct pci_bus *bus, struct pci_dev *card,
1301 struct address_info *hw_config)
1303 if((ad1816_dev = isapnp_find_dev(bus, card->vendor, card->device, NULL))) {
1304 ad1816_dev->prepare(ad1816_dev);
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;
1314 return(ad1816_dev);
1317 static struct {
1318 unsigned short vendor;
1319 unsigned short function;
1320 struct pci_dev * (*initfunc)(struct pci_bus*, struct pci_dev *, struct address_info *);
1321 char *name;
1322 } isapnp_ad1816_list[] __initdata = {
1323 {ISAPNP_VENDOR('A','D','S'), ISAPNP_FUNCTION(0x7150), &ad1816_init_generic, "Analog Devices 1815" },
1324 {ISAPNP_VENDOR('A','D','S'), ISAPNP_FUNCTION(0x7180), &ad1816_init_generic, "Analog Devices 1816A" },
1328 static int __init ad1816_init_isapnp(struct address_info *hw_config,
1329 struct pci_bus *bus, struct pci_dev *card, int slot)
1331 struct pci_dev *idev = NULL;
1333 /* You missed the init func? That's bad. */
1334 if(isapnp_ad1816_list[slot].initfunc) {
1335 char *busname = bus->name[0] ? bus->name : isapnp_ad1816_list[slot].name;
1337 printk(KERN_INFO "ad1816: %s detected\n", busname);
1339 /* Initialize this baby. */
1340 if((idev = isapnp_ad1816_list[slot].initfunc(bus, card, hw_config))) {
1341 /* We got it. */
1343 printk(KERN_NOTICE "ad1816: ISAPnP reports '%s' at i/o %#x, irq %d, dma %d, %d\n",
1344 busname,
1345 hw_config->io_base, hw_config->irq, hw_config->dma,
1346 hw_config->dma2);
1347 return 1;
1348 } else
1349 printk(KERN_INFO "ad1816: Failed to initialize %s\n", busname);
1350 } else
1351 printk(KERN_ERR "ad1816: Bad entry in ad1816.c PnP table\n");
1353 return 0;
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)
1365 int i;
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);
1373 isapnpjump = 0;
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))
1382 return 0;
1385 return -ENODEV;
1387 #endif
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");
1395 isapnp = 0;
1397 #endif
1399 if( isapnp == 0) {
1400 cfg.io_base = io;
1401 cfg.irq = irq;
1402 cfg.dma = dma;
1403 cfg.dma2 = dma2;
1406 if (cfg.io_base == -1 || cfg.irq == -1 || cfg.dma == -1 || cfg.dma2 == -1) {
1407 printk(KERN_INFO "ad1816: dma, dma2, irq and io must be set.\n");
1408 return -EINVAL;
1411 if (probe_ad1816(&cfg) == 0) {
1412 return -ENODEV;
1415 attach_ad1816(&cfg);
1417 return 0;
1420 static void __exit cleanup_ad1816 (void)
1422 int i;
1423 ad1816_info *devc = NULL;
1425 /* remove any soundcard */
1426 for (i = 0; i < nr_ad1816_devs; i++) {
1427 devc = &dev_info[i];
1428 unload_card(devc);
1430 nr_ad1816_devs=0;
1432 #if defined CONFIG_ISAPNP || defined CONFIG_ISAPNP_MODULE
1433 if(activated)
1434 if(ad1816_dev)
1435 ad1816_dev->deactivate(ad1816_dev);
1436 #endif
1439 module_init(init_ad1816);
1440 module_exit(cleanup_ad1816);
1442 #ifndef MODULE
1443 static int __init setup_ad1816(char *str)
1445 /* io, irq, dma, dma2 */
1446 int ints[5];
1448 str = get_options(str, ARRAY_SIZE(ints), ints);
1450 io = ints[1];
1451 irq = ints[2];
1452 dma = ints[3];
1453 dma2 = ints[4];
1455 return 1;
1458 __setup("ad1816=", setup_ad1816);
1459 #endif