Pull one more egcs 1.1.2 workaround.
[linux-2.6/linux-mips.git] / sound / oss / ad1848.c
blobb0b848f220b5c0b356683081b27ed453fbc2a807
1 /*
2 * sound/ad1848.c
4 * The low level driver for the AD1848/CS4248 codec chip which
5 * is used for example in the MS Sound System.
7 * The CS4231 which is used in the GUS MAX and some other cards is
8 * upwards compatible with AD1848 and this driver is able to drive it.
10 * CS4231A and AD1845 are upward compatible with CS4231. However
11 * the new features of these chips are different.
13 * CS4232 is a PnP audio chip which contains a CS4231A (and SB, MPU).
14 * CS4232A is an improved version of CS4232.
18 * Copyright (C) by Hannu Savolainen 1993-1997
20 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
21 * Version 2 (June 1991). See the "COPYING" file distributed with this software
22 * for more info.
25 * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
26 * general sleep/wakeup clean up.
27 * Alan Cox : reformatted. Fixed SMP bugs. Moved to kernel alloc/free
28 * of irqs. Use dev_id.
29 * Christoph Hellwig : adapted to module_init/module_exit
30 * Aki Laukkanen : added power management support
31 * Arnaldo C. de Melo : added missing restore_flags in ad1848_resume
32 * Miguel Freitas : added ISA PnP support
33 * Alan Cox : Added CS4236->4239 identification
34 * Daniel T. Cobra : Alernate config/mixer for later chips
35 * Alan Cox : Merged chip idents and config code
37 * TODO
38 * APM save restore assist code on IBM thinkpad
40 * Status:
41 * Tested. Believed fully functional.
44 #include <linux/config.h>
45 #include <linux/init.h>
46 #include <linux/module.h>
47 #include <linux/stddef.h>
48 #include <linux/pm.h>
49 #include <linux/isapnp.h>
50 #include <linux/pnp.h>
51 #include <linux/spinlock.h>
53 #define DEB(x)
54 #define DEB1(x)
55 #include "sound_config.h"
57 #include "ad1848.h"
58 #include "ad1848_mixer.h"
60 typedef struct
62 spinlock_t lock;
63 int base;
64 int irq;
65 int dma1, dma2;
66 int dual_dma; /* 1, when two DMA channels allocated */
67 int subtype;
68 unsigned char MCE_bit;
69 unsigned char saved_regs[64]; /* Includes extended register space */
70 int debug_flag;
72 int audio_flags;
73 int record_dev, playback_dev;
75 int xfer_count;
76 int audio_mode;
77 int open_mode;
78 int intr_active;
79 char *chip_name, *name;
80 int model;
81 #define MD_1848 1
82 #define MD_4231 2
83 #define MD_4231A 3
84 #define MD_1845 4
85 #define MD_4232 5
86 #define MD_C930 6
87 #define MD_IWAVE 7
88 #define MD_4235 8 /* Crystal Audio CS4235 */
89 #define MD_1845_SSCAPE 9 /* Ensoniq Soundscape PNP*/
90 #define MD_4236 10 /* 4236 and higher */
91 #define MD_42xB 11 /* CS 42xB */
92 #define MD_4239 12 /* CS4239 */
94 /* Mixer parameters */
95 int recmask;
96 int supported_devices, orig_devices;
97 int supported_rec_devices, orig_rec_devices;
98 int *levels;
99 short mixer_reroute[32];
100 int dev_no;
101 volatile unsigned long timer_ticks;
102 int timer_running;
103 int irq_ok;
104 mixer_ents *mix_devices;
105 int mixer_output_port;
107 /* Power management */
108 struct pm_dev *pmdev;
109 } ad1848_info;
111 typedef struct ad1848_port_info
113 int open_mode;
114 int speed;
115 unsigned char speed_bits;
116 int channels;
117 int audio_format;
118 unsigned char format_bits;
120 ad1848_port_info;
122 static struct address_info cfg;
123 static int nr_ad1848_devs;
125 int deskpro_xl;
126 int deskpro_m;
127 int soundpro;
129 static volatile signed char irq2dev[17] = {
130 -1, -1, -1, -1, -1, -1, -1, -1,
131 -1, -1, -1, -1, -1, -1, -1, -1, -1
134 #ifndef EXCLUDE_TIMERS
135 static int timer_installed = -1;
136 #endif
138 static int loaded;
140 static int ad_format_mask[13 /*devc->model */ ] =
143 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW,
144 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
145 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
146 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW, /* AD1845 */
147 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
148 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
149 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
150 AFMT_U8 | AFMT_S16_LE /* CS4235 */,
151 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW /* Ensoniq Soundscape*/,
152 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
153 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
154 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM
157 static ad1848_info adev_info[MAX_AUDIO_DEV];
159 #define io_Index_Addr(d) ((d)->base)
160 #define io_Indexed_Data(d) ((d)->base+1)
161 #define io_Status(d) ((d)->base+2)
162 #define io_Polled_IO(d) ((d)->base+3)
164 static struct {
165 unsigned char flags;
166 #define CAP_F_TIMER 0x01
167 } capabilities [10 /*devc->model */ ] = {
169 ,{0} /* MD_1848 */
170 ,{CAP_F_TIMER} /* MD_4231 */
171 ,{CAP_F_TIMER} /* MD_4231A */
172 ,{CAP_F_TIMER} /* MD_1845 */
173 ,{CAP_F_TIMER} /* MD_4232 */
174 ,{0} /* MD_C930 */
175 ,{CAP_F_TIMER} /* MD_IWAVE */
176 ,{0} /* MD_4235 */
177 ,{CAP_F_TIMER} /* MD_1845_SSCAPE */
180 #ifdef CONFIG_PNP
181 static int isapnp = 1;
182 static int isapnpjump = 0;
183 static int reverse = 0;
185 static int audio_activated = 0;
186 #else
187 static int isapnp = 0;
188 #endif
192 static int ad1848_open(int dev, int mode);
193 static void ad1848_close(int dev);
194 static void ad1848_output_block(int dev, unsigned long buf, int count, int intrflag);
195 static void ad1848_start_input(int dev, unsigned long buf, int count, int intrflag);
196 static int ad1848_prepare_for_output(int dev, int bsize, int bcount);
197 static int ad1848_prepare_for_input(int dev, int bsize, int bcount);
198 static void ad1848_halt(int dev);
199 static void ad1848_halt_input(int dev);
200 static void ad1848_halt_output(int dev);
201 static void ad1848_trigger(int dev, int bits);
202 static int ad1848_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data);
204 #ifndef EXCLUDE_TIMERS
205 static int ad1848_tmr_install(int dev);
206 static void ad1848_tmr_reprogram(int dev);
207 #endif
209 static int ad_read(ad1848_info * devc, int reg)
211 unsigned long flags;
212 int x;
213 int timeout = 900000;
215 while (timeout > 0 && inb(devc->base) == 0x80) /*Are we initializing */
216 timeout--;
218 spin_lock_irqsave(&devc->lock,flags);
220 if(reg < 32)
222 outb(((unsigned char) (reg & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
223 x = inb(io_Indexed_Data(devc));
225 else
227 int xreg, xra;
229 xreg = (reg & 0xff) - 32;
230 xra = (((xreg & 0x0f) << 4) & 0xf0) | 0x08 | ((xreg & 0x10) >> 2);
231 outb(((unsigned char) (23 & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
232 outb(((unsigned char) (xra & 0xff)), io_Indexed_Data(devc));
233 x = inb(io_Indexed_Data(devc));
235 spin_unlock_irqrestore(&devc->lock,flags);
237 return x;
240 static void ad_write(ad1848_info * devc, int reg, int data)
242 unsigned long flags;
243 int timeout = 900000;
245 while (timeout > 0 && inb(devc->base) == 0x80) /* Are we initializing */
246 timeout--;
248 spin_lock_irqsave(&devc->lock,flags);
250 if(reg < 32)
252 outb(((unsigned char) (reg & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
253 outb(((unsigned char) (data & 0xff)), io_Indexed_Data(devc));
255 else
257 int xreg, xra;
259 xreg = (reg & 0xff) - 32;
260 xra = (((xreg & 0x0f) << 4) & 0xf0) | 0x08 | ((xreg & 0x10) >> 2);
261 outb(((unsigned char) (23 & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
262 outb(((unsigned char) (xra & 0xff)), io_Indexed_Data(devc));
263 outb((unsigned char) (data & 0xff), io_Indexed_Data(devc));
265 spin_unlock_irqrestore(&devc->lock,flags);
268 static void wait_for_calibration(ad1848_info * devc)
270 int timeout = 0;
273 * Wait until the auto calibration process has finished.
275 * 1) Wait until the chip becomes ready (reads don't return 0x80).
276 * 2) Wait until the ACI bit of I11 gets on and then off.
279 timeout = 100000;
280 while (timeout > 0 && inb(devc->base) == 0x80)
281 timeout--;
282 if (inb(devc->base) & 0x80)
283 printk(KERN_WARNING "ad1848: Auto calibration timed out(1).\n");
285 timeout = 100;
286 while (timeout > 0 && !(ad_read(devc, 11) & 0x20))
287 timeout--;
288 if (!(ad_read(devc, 11) & 0x20))
289 return;
291 timeout = 80000;
292 while (timeout > 0 && (ad_read(devc, 11) & 0x20))
293 timeout--;
294 if (ad_read(devc, 11) & 0x20)
295 if ( (devc->model != MD_1845) || (devc->model != MD_1845_SSCAPE))
296 printk(KERN_WARNING "ad1848: Auto calibration timed out(3).\n");
299 static void ad_mute(ad1848_info * devc)
301 int i;
302 unsigned char prev;
305 * Save old register settings and mute output channels
308 for (i = 6; i < 8; i++)
310 prev = devc->saved_regs[i] = ad_read(devc, i);
315 static void ad_unmute(ad1848_info * devc)
319 static void ad_enter_MCE(ad1848_info * devc)
321 unsigned long flags;
322 int timeout = 1000;
323 unsigned short prev;
325 while (timeout > 0 && inb(devc->base) == 0x80) /*Are we initializing */
326 timeout--;
328 spin_lock_irqsave(&devc->lock,flags);
330 devc->MCE_bit = 0x40;
331 prev = inb(io_Index_Addr(devc));
332 if (prev & 0x40)
334 spin_unlock_irqrestore(&devc->lock,flags);
335 return;
337 outb((devc->MCE_bit), io_Index_Addr(devc));
338 spin_unlock_irqrestore(&devc->lock,flags);
341 static void ad_leave_MCE(ad1848_info * devc)
343 unsigned long flags;
344 unsigned char prev, acal;
345 int timeout = 1000;
347 while (timeout > 0 && inb(devc->base) == 0x80) /*Are we initializing */
348 timeout--;
350 spin_lock_irqsave(&devc->lock,flags);
352 acal = ad_read(devc, 9);
354 devc->MCE_bit = 0x00;
355 prev = inb(io_Index_Addr(devc));
356 outb((0x00), io_Index_Addr(devc)); /* Clear the MCE bit */
358 if ((prev & 0x40) == 0) /* Not in MCE mode */
360 spin_unlock_irqrestore(&devc->lock,flags);
361 return;
363 outb((0x00), io_Index_Addr(devc)); /* Clear the MCE bit */
364 if (acal & 0x08) /* Auto calibration is enabled */
365 wait_for_calibration(devc);
366 spin_unlock_irqrestore(&devc->lock,flags);
369 static int ad1848_set_recmask(ad1848_info * devc, int mask)
371 unsigned char recdev;
372 int i, n;
374 mask &= devc->supported_rec_devices;
376 /* Rename the mixer bits if necessary */
377 for (i = 0; i < 32; i++)
379 if (devc->mixer_reroute[i] != i)
381 if (mask & (1 << i))
383 mask &= ~(1 << i);
384 mask |= (1 << devc->mixer_reroute[i]);
389 n = 0;
390 for (i = 0; i < 32; i++) /* Count selected device bits */
391 if (mask & (1 << i))
392 n++;
394 if (!soundpro) {
395 if (n == 0)
396 mask = SOUND_MASK_MIC;
397 else if (n != 1) { /* Too many devices selected */
398 mask &= ~devc->recmask; /* Filter out active settings */
400 n = 0;
401 for (i = 0; i < 32; i++) /* Count selected device bits */
402 if (mask & (1 << i))
403 n++;
405 if (n != 1)
406 mask = SOUND_MASK_MIC;
408 switch (mask) {
409 case SOUND_MASK_MIC:
410 recdev = 2;
411 break;
413 case SOUND_MASK_LINE:
414 case SOUND_MASK_LINE3:
415 recdev = 0;
416 break;
418 case SOUND_MASK_CD:
419 case SOUND_MASK_LINE1:
420 recdev = 1;
421 break;
423 case SOUND_MASK_IMIX:
424 recdev = 3;
425 break;
427 default:
428 mask = SOUND_MASK_MIC;
429 recdev = 2;
432 recdev <<= 6;
433 ad_write(devc, 0, (ad_read(devc, 0) & 0x3f) | recdev);
434 ad_write(devc, 1, (ad_read(devc, 1) & 0x3f) | recdev);
435 } else { /* soundpro */
436 unsigned char val;
437 int set_rec_bit;
438 int j;
440 for (i = 0; i < 32; i++) { /* For each bit */
441 if ((devc->supported_rec_devices & (1 << i)) == 0)
442 continue; /* Device not supported */
444 for (j = LEFT_CHN; j <= RIGHT_CHN; j++) {
445 if (devc->mix_devices[i][j].nbits == 0) /* Inexistent channel */
446 continue;
449 * This is tricky:
450 * set_rec_bit becomes 1 if the corresponding bit in mask is set
451 * then it gets flipped if the polarity is inverse
453 set_rec_bit = ((mask & (1 << i)) != 0) ^ devc->mix_devices[i][j].recpol;
455 val = ad_read(devc, devc->mix_devices[i][j].recreg);
456 val &= ~(1 << devc->mix_devices[i][j].recpos);
457 val |= (set_rec_bit << devc->mix_devices[i][j].recpos);
458 ad_write(devc, devc->mix_devices[i][j].recreg, val);
463 /* Rename the mixer bits back if necessary */
464 for (i = 0; i < 32; i++)
466 if (devc->mixer_reroute[i] != i)
468 if (mask & (1 << devc->mixer_reroute[i]))
470 mask &= ~(1 << devc->mixer_reroute[i]);
471 mask |= (1 << i);
475 devc->recmask = mask;
476 return mask;
479 static void change_bits(ad1848_info * devc, unsigned char *regval,
480 unsigned char *muteval, int dev, int chn, int newval)
482 unsigned char mask;
483 int shift;
484 int mute;
485 int mutemask;
486 int set_mute_bit;
488 set_mute_bit = (newval == 0) ^ devc->mix_devices[dev][chn].mutepol;
490 if (devc->mix_devices[dev][chn].polarity == 1) /* Reverse */
491 newval = 100 - newval;
493 mask = (1 << devc->mix_devices[dev][chn].nbits) - 1;
494 shift = devc->mix_devices[dev][chn].bitpos;
496 if (devc->mix_devices[dev][chn].mutepos == 8)
497 { /* if there is no mute bit */
498 mute = 0; /* No mute bit; do nothing special */
499 mutemask = ~0; /* No mute bit; do nothing special */
501 else
503 mute = (set_mute_bit << devc->mix_devices[dev][chn].mutepos);
504 mutemask = ~(1 << devc->mix_devices[dev][chn].mutepos);
507 newval = (int) ((newval * mask) + 50) / 100; /* Scale it */
508 *regval &= ~(mask << shift); /* Clear bits */
509 *regval |= (newval & mask) << shift; /* Set new value */
511 *muteval &= mutemask;
512 *muteval |= mute;
515 static int ad1848_mixer_get(ad1848_info * devc, int dev)
517 if (!((1 << dev) & devc->supported_devices))
518 return -EINVAL;
520 dev = devc->mixer_reroute[dev];
522 return devc->levels[dev];
525 static void ad1848_mixer_set_channel(ad1848_info *devc, int dev, int value, int channel)
527 int regoffs, muteregoffs;
528 unsigned char val, muteval;
530 regoffs = devc->mix_devices[dev][channel].regno;
531 muteregoffs = devc->mix_devices[dev][channel].mutereg;
532 val = ad_read(devc, regoffs);
534 if (muteregoffs != regoffs) {
535 muteval = ad_read(devc, muteregoffs);
536 change_bits(devc, &val, &muteval, dev, channel, value);
538 else
539 change_bits(devc, &val, &val, dev, channel, value);
541 ad_write(devc, regoffs, val);
542 devc->saved_regs[regoffs] = val;
543 if (muteregoffs != regoffs) {
544 ad_write(devc, muteregoffs, muteval);
545 devc->saved_regs[muteregoffs] = muteval;
549 static int ad1848_mixer_set(ad1848_info * devc, int dev, int value)
551 int left = value & 0x000000ff;
552 int right = (value & 0x0000ff00) >> 8;
553 int retvol;
555 if (dev > 31)
556 return -EINVAL;
558 if (!(devc->supported_devices & (1 << dev)))
559 return -EINVAL;
561 dev = devc->mixer_reroute[dev];
563 if (devc->mix_devices[dev][LEFT_CHN].nbits == 0)
564 return -EINVAL;
566 if (left > 100)
567 left = 100;
568 if (right > 100)
569 right = 100;
571 if (devc->mix_devices[dev][RIGHT_CHN].nbits == 0) /* Mono control */
572 right = left;
574 retvol = left | (right << 8);
576 /* Scale volumes */
577 left = mix_cvt[left];
578 right = mix_cvt[right];
580 devc->levels[dev] = retvol;
583 * Set the left channel
585 ad1848_mixer_set_channel(devc, dev, left, LEFT_CHN);
588 * Set the right channel
590 if (devc->mix_devices[dev][RIGHT_CHN].nbits == 0)
591 goto out;
592 ad1848_mixer_set_channel(devc, dev, right, RIGHT_CHN);
594 out:
595 return retvol;
598 static void ad1848_mixer_reset(ad1848_info * devc)
600 int i;
601 char name[32];
603 devc->mix_devices = &(ad1848_mix_devices[0]);
605 sprintf(name, "%s_%d", devc->chip_name, nr_ad1848_devs);
607 for (i = 0; i < 32; i++)
608 devc->mixer_reroute[i] = i;
610 devc->supported_rec_devices = MODE1_REC_DEVICES;
612 switch (devc->model)
614 case MD_4231:
615 case MD_4231A:
616 case MD_1845:
617 case MD_1845_SSCAPE:
618 devc->supported_devices = MODE2_MIXER_DEVICES;
619 break;
621 case MD_C930:
622 devc->supported_devices = C930_MIXER_DEVICES;
623 devc->mix_devices = &(c930_mix_devices[0]);
624 break;
626 case MD_IWAVE:
627 devc->supported_devices = MODE3_MIXER_DEVICES;
628 devc->mix_devices = &(iwave_mix_devices[0]);
629 break;
631 case MD_42xB:
632 case MD_4239:
633 devc->mix_devices = &(cs42xb_mix_devices[0]);
634 devc->supported_devices = MODE3_MIXER_DEVICES;
635 break;
636 case MD_4232:
637 case MD_4236:
638 devc->supported_devices = MODE3_MIXER_DEVICES;
639 break;
641 case MD_1848:
642 if (soundpro) {
643 devc->supported_devices = SPRO_MIXER_DEVICES;
644 devc->supported_rec_devices = SPRO_REC_DEVICES;
645 devc->mix_devices = &(spro_mix_devices[0]);
646 break;
649 default:
650 devc->supported_devices = MODE1_MIXER_DEVICES;
653 devc->orig_devices = devc->supported_devices;
654 devc->orig_rec_devices = devc->supported_rec_devices;
656 devc->levels = load_mixer_volumes(name, default_mixer_levels, 1);
658 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
660 if (devc->supported_devices & (1 << i))
661 ad1848_mixer_set(devc, i, devc->levels[i]);
664 ad1848_set_recmask(devc, SOUND_MASK_MIC);
666 devc->mixer_output_port = devc->levels[31] | AUDIO_HEADPHONE | AUDIO_LINE_OUT;
668 if (!soundpro) {
669 if (devc->mixer_output_port & AUDIO_SPEAKER)
670 ad_write(devc, 26, ad_read(devc, 26) & ~0x40); /* Unmute mono out */
671 else
672 ad_write(devc, 26, ad_read(devc, 26) | 0x40); /* Mute mono out */
673 } else {
675 * From the "wouldn't it be nice if the mixer API had (better)
676 * support for custom stuff" category
678 /* Enable surround mode and SB16 mixer */
679 ad_write(devc, 16, 0x60);
683 static int ad1848_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
685 ad1848_info *devc = mixer_devs[dev]->devc;
686 int val;
688 if (cmd == SOUND_MIXER_PRIVATE1)
690 if (get_user(val, (int *)arg))
691 return -EFAULT;
693 if (val != 0xffff)
695 val &= (AUDIO_SPEAKER | AUDIO_HEADPHONE | AUDIO_LINE_OUT);
696 devc->mixer_output_port = val;
697 val |= AUDIO_HEADPHONE | AUDIO_LINE_OUT; /* Always on */
698 devc->mixer_output_port = val;
699 if (val & AUDIO_SPEAKER)
700 ad_write(devc, 26, ad_read(devc, 26) & ~0x40); /* Unmute mono out */
701 else
702 ad_write(devc, 26, ad_read(devc, 26) | 0x40); /* Mute mono out */
704 val = devc->mixer_output_port;
705 return put_user(val, (int *)arg);
707 if (cmd == SOUND_MIXER_PRIVATE2)
709 if (get_user(val, (int *)arg))
710 return -EFAULT;
711 return(ad1848_control(AD1848_MIXER_REROUTE, val));
713 if (((cmd >> 8) & 0xff) == 'M')
715 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
717 switch (cmd & 0xff)
719 case SOUND_MIXER_RECSRC:
720 if (get_user(val, (int *)arg))
721 return -EFAULT;
722 val = ad1848_set_recmask(devc, val);
723 break;
725 default:
726 if (get_user(val, (int *)arg))
727 return -EFAULT;
728 val = ad1848_mixer_set(devc, cmd & 0xff, val);
729 break;
731 return put_user(val, (int *)arg);
733 else
735 switch (cmd & 0xff)
738 * Return parameters
741 case SOUND_MIXER_RECSRC:
742 val = devc->recmask;
743 break;
745 case SOUND_MIXER_DEVMASK:
746 val = devc->supported_devices;
747 break;
749 case SOUND_MIXER_STEREODEVS:
750 val = devc->supported_devices;
751 if (devc->model != MD_C930)
752 val &= ~(SOUND_MASK_SPEAKER | SOUND_MASK_IMIX);
753 break;
755 case SOUND_MIXER_RECMASK:
756 val = devc->supported_rec_devices;
757 break;
759 case SOUND_MIXER_CAPS:
760 val=SOUND_CAP_EXCL_INPUT;
761 break;
763 default:
764 val = ad1848_mixer_get(devc, cmd & 0xff);
765 break;
767 return put_user(val, (int *)arg);
770 else
771 return -EINVAL;
774 static int ad1848_set_speed(int dev, int arg)
776 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
777 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
780 * The sampling speed is encoded in the least significant nibble of I8. The
781 * LSB selects the clock source (0=24.576 MHz, 1=16.9344 MHz) and other
782 * three bits select the divisor (indirectly):
784 * The available speeds are in the following table. Keep the speeds in
785 * the increasing order.
787 typedef struct
789 int speed;
790 unsigned char bits;
792 speed_struct;
794 static speed_struct speed_table[] =
796 {5510, (0 << 1) | 1},
797 {5510, (0 << 1) | 1},
798 {6620, (7 << 1) | 1},
799 {8000, (0 << 1) | 0},
800 {9600, (7 << 1) | 0},
801 {11025, (1 << 1) | 1},
802 {16000, (1 << 1) | 0},
803 {18900, (2 << 1) | 1},
804 {22050, (3 << 1) | 1},
805 {27420, (2 << 1) | 0},
806 {32000, (3 << 1) | 0},
807 {33075, (6 << 1) | 1},
808 {37800, (4 << 1) | 1},
809 {44100, (5 << 1) | 1},
810 {48000, (6 << 1) | 0}
813 int i, n, selected = -1;
815 n = sizeof(speed_table) / sizeof(speed_struct);
817 if (arg <= 0)
818 return portc->speed;
820 if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE) /* AD1845 has different timer than others */
822 if (arg < 4000)
823 arg = 4000;
824 if (arg > 50000)
825 arg = 50000;
827 portc->speed = arg;
828 portc->speed_bits = speed_table[3].bits;
829 return portc->speed;
831 if (arg < speed_table[0].speed)
832 selected = 0;
833 if (arg > speed_table[n - 1].speed)
834 selected = n - 1;
836 for (i = 1 /*really */ ; selected == -1 && i < n; i++)
838 if (speed_table[i].speed == arg)
839 selected = i;
840 else if (speed_table[i].speed > arg)
842 int diff1, diff2;
844 diff1 = arg - speed_table[i - 1].speed;
845 diff2 = speed_table[i].speed - arg;
847 if (diff1 < diff2)
848 selected = i - 1;
849 else
850 selected = i;
853 if (selected == -1)
855 printk(KERN_WARNING "ad1848: Can't find speed???\n");
856 selected = 3;
858 portc->speed = speed_table[selected].speed;
859 portc->speed_bits = speed_table[selected].bits;
860 return portc->speed;
863 static short ad1848_set_channels(int dev, short arg)
865 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
867 if (arg != 1 && arg != 2)
868 return portc->channels;
870 portc->channels = arg;
871 return arg;
874 static unsigned int ad1848_set_bits(int dev, unsigned int arg)
876 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
877 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
879 static struct format_tbl
881 int format;
882 unsigned char bits;
884 format2bits[] =
887 0, 0
891 AFMT_MU_LAW, 1
895 AFMT_A_LAW, 3
899 AFMT_IMA_ADPCM, 5
903 AFMT_U8, 0
907 AFMT_S16_LE, 2
911 AFMT_S16_BE, 6
915 AFMT_S8, 0
919 AFMT_U16_LE, 0
923 AFMT_U16_BE, 0
926 int i, n = sizeof(format2bits) / sizeof(struct format_tbl);
928 if (arg == 0)
929 return portc->audio_format;
931 if (!(arg & ad_format_mask[devc->model]))
932 arg = AFMT_U8;
934 portc->audio_format = arg;
936 for (i = 0; i < n; i++)
937 if (format2bits[i].format == arg)
939 if ((portc->format_bits = format2bits[i].bits) == 0)
940 return portc->audio_format = AFMT_U8; /* Was not supported */
942 return arg;
944 /* Still hanging here. Something must be terribly wrong */
945 portc->format_bits = 0;
946 return portc->audio_format = AFMT_U8;
949 static struct audio_driver ad1848_audio_driver =
951 owner: THIS_MODULE,
952 open: ad1848_open,
953 close: ad1848_close,
954 output_block: ad1848_output_block,
955 start_input: ad1848_start_input,
956 prepare_for_input: ad1848_prepare_for_input,
957 prepare_for_output: ad1848_prepare_for_output,
958 halt_io: ad1848_halt,
959 halt_input: ad1848_halt_input,
960 halt_output: ad1848_halt_output,
961 trigger: ad1848_trigger,
962 set_speed: ad1848_set_speed,
963 set_bits: ad1848_set_bits,
964 set_channels: ad1848_set_channels
967 static struct mixer_operations ad1848_mixer_operations =
969 owner: THIS_MODULE,
970 id: "SOUNDPORT",
971 name: "AD1848/CS4248/CS4231",
972 ioctl: ad1848_mixer_ioctl
975 static int ad1848_open(int dev, int mode)
977 ad1848_info *devc;
978 ad1848_port_info *portc;
979 unsigned long flags;
981 if (dev < 0 || dev >= num_audiodevs)
982 return -ENXIO;
984 devc = (ad1848_info *) audio_devs[dev]->devc;
985 portc = (ad1848_port_info *) audio_devs[dev]->portc;
987 spin_lock_irqsave(&devc->lock,flags);
988 if (portc->open_mode || (devc->open_mode & mode))
990 spin_unlock_irqrestore(&devc->lock,flags);
991 return -EBUSY;
993 devc->dual_dma = 0;
995 if (audio_devs[dev]->flags & DMA_DUPLEX)
997 devc->dual_dma = 1;
999 devc->intr_active = 0;
1000 devc->audio_mode = 0;
1001 devc->open_mode |= mode;
1002 portc->open_mode = mode;
1003 ad1848_trigger(dev, 0);
1005 if (mode & OPEN_READ)
1006 devc->record_dev = dev;
1007 if (mode & OPEN_WRITE)
1008 devc->playback_dev = dev;
1009 spin_unlock_irqrestore(&devc->lock,flags);
1011 * Mute output until the playback really starts. This decreases clicking (hope so).
1013 ad_mute(devc);
1015 return 0;
1018 static void ad1848_close(int dev)
1020 unsigned long flags;
1021 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1022 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1024 DEB(printk("ad1848_close(void)\n"));
1026 spin_lock_irqsave(&devc->lock,flags);
1028 devc->intr_active = 0;
1029 ad1848_halt(dev);
1031 devc->audio_mode = 0;
1032 devc->open_mode &= ~portc->open_mode;
1033 portc->open_mode = 0;
1035 ad_unmute(devc);
1036 spin_unlock_irqrestore(&devc->lock,flags);
1039 static void ad1848_output_block(int dev, unsigned long buf, int count, int intrflag)
1041 unsigned long flags, cnt;
1042 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1043 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1045 cnt = count;
1047 if (portc->audio_format == AFMT_IMA_ADPCM)
1049 cnt /= 4;
1051 else
1053 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE)) /* 16 bit data */
1054 cnt >>= 1;
1056 if (portc->channels > 1)
1057 cnt >>= 1;
1058 cnt--;
1060 if ((devc->audio_mode & PCM_ENABLE_OUTPUT) && (audio_devs[dev]->flags & DMA_AUTOMODE) &&
1061 intrflag &&
1062 cnt == devc->xfer_count)
1064 devc->audio_mode |= PCM_ENABLE_OUTPUT;
1065 devc->intr_active = 1;
1066 return; /*
1067 * Auto DMA mode on. No need to react
1070 spin_lock_irqsave(&devc->lock,flags);
1072 ad_write(devc, 15, (unsigned char) (cnt & 0xff));
1073 ad_write(devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
1075 devc->xfer_count = cnt;
1076 devc->audio_mode |= PCM_ENABLE_OUTPUT;
1077 devc->intr_active = 1;
1078 spin_unlock_irqrestore(&devc->lock,flags);
1081 static void ad1848_start_input(int dev, unsigned long buf, int count, int intrflag)
1083 unsigned long flags, cnt;
1084 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1085 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1087 cnt = count;
1088 if (portc->audio_format == AFMT_IMA_ADPCM)
1090 cnt /= 4;
1092 else
1094 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE)) /* 16 bit data */
1095 cnt >>= 1;
1097 if (portc->channels > 1)
1098 cnt >>= 1;
1099 cnt--;
1101 if ((devc->audio_mode & PCM_ENABLE_INPUT) && (audio_devs[dev]->flags & DMA_AUTOMODE) &&
1102 intrflag &&
1103 cnt == devc->xfer_count)
1105 devc->audio_mode |= PCM_ENABLE_INPUT;
1106 devc->intr_active = 1;
1107 return; /*
1108 * Auto DMA mode on. No need to react
1111 spin_lock_irqsave(&devc->lock,flags);
1113 if (devc->model == MD_1848)
1115 ad_write(devc, 15, (unsigned char) (cnt & 0xff));
1116 ad_write(devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
1118 else
1120 ad_write(devc, 31, (unsigned char) (cnt & 0xff));
1121 ad_write(devc, 30, (unsigned char) ((cnt >> 8) & 0xff));
1124 ad_unmute(devc);
1126 devc->xfer_count = cnt;
1127 devc->audio_mode |= PCM_ENABLE_INPUT;
1128 devc->intr_active = 1;
1129 spin_unlock_irqrestore(&devc->lock,flags);
1132 static int ad1848_prepare_for_output(int dev, int bsize, int bcount)
1134 int timeout;
1135 unsigned char fs, old_fs, tmp = 0;
1136 unsigned long flags;
1137 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1138 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1140 ad_mute(devc);
1142 spin_lock_irqsave(&devc->lock,flags);
1143 fs = portc->speed_bits | (portc->format_bits << 5);
1145 if (portc->channels > 1)
1146 fs |= 0x10;
1148 ad_enter_MCE(devc); /* Enables changes to the format select reg */
1150 if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE) /* Use alternate speed select registers */
1152 fs &= 0xf0; /* Mask off the rate select bits */
1154 ad_write(devc, 22, (portc->speed >> 8) & 0xff); /* Speed MSB */
1155 ad_write(devc, 23, portc->speed & 0xff); /* Speed LSB */
1157 old_fs = ad_read(devc, 8);
1159 if (devc->model == MD_4232 || devc->model >= MD_4236)
1161 tmp = ad_read(devc, 16);
1162 ad_write(devc, 16, tmp | 0x30);
1164 if (devc->model == MD_IWAVE)
1165 ad_write(devc, 17, 0xc2); /* Disable variable frequency select */
1167 ad_write(devc, 8, fs);
1170 * Write to I8 starts resynchronization. Wait until it completes.
1173 timeout = 0;
1174 while (timeout < 100 && inb(devc->base) != 0x80)
1175 timeout++;
1176 timeout = 0;
1177 while (timeout < 10000 && inb(devc->base) == 0x80)
1178 timeout++;
1180 if (devc->model >= MD_4232)
1181 ad_write(devc, 16, tmp & ~0x30);
1183 ad_leave_MCE(devc); /*
1184 * Starts the calibration process.
1186 spin_unlock_irqrestore(&devc->lock,flags);
1187 devc->xfer_count = 0;
1189 #ifndef EXCLUDE_TIMERS
1190 if (dev == timer_installed && devc->timer_running)
1191 if ((fs & 0x01) != (old_fs & 0x01))
1193 ad1848_tmr_reprogram(dev);
1195 #endif
1196 ad1848_halt_output(dev);
1197 return 0;
1200 static int ad1848_prepare_for_input(int dev, int bsize, int bcount)
1202 int timeout;
1203 unsigned char fs, old_fs, tmp = 0;
1204 unsigned long flags;
1205 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1206 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1208 if (devc->audio_mode)
1209 return 0;
1211 spin_lock_irqsave(&devc->lock,flags);
1212 fs = portc->speed_bits | (portc->format_bits << 5);
1214 if (portc->channels > 1)
1215 fs |= 0x10;
1217 ad_enter_MCE(devc); /* Enables changes to the format select reg */
1219 if ((devc->model == MD_1845) || (devc->model == MD_1845_SSCAPE)) /* Use alternate speed select registers */
1221 fs &= 0xf0; /* Mask off the rate select bits */
1223 ad_write(devc, 22, (portc->speed >> 8) & 0xff); /* Speed MSB */
1224 ad_write(devc, 23, portc->speed & 0xff); /* Speed LSB */
1226 if (devc->model == MD_4232)
1228 tmp = ad_read(devc, 16);
1229 ad_write(devc, 16, tmp | 0x30);
1231 if (devc->model == MD_IWAVE)
1232 ad_write(devc, 17, 0xc2); /* Disable variable frequency select */
1235 * If mode >= 2 (CS4231), set I28. It's the capture format register.
1238 if (devc->model != MD_1848)
1240 old_fs = ad_read(devc, 28);
1241 ad_write(devc, 28, fs);
1244 * Write to I28 starts resynchronization. Wait until it completes.
1247 timeout = 0;
1248 while (timeout < 100 && inb(devc->base) != 0x80)
1249 timeout++;
1251 timeout = 0;
1252 while (timeout < 10000 && inb(devc->base) == 0x80)
1253 timeout++;
1255 if (devc->model != MD_1848 && devc->model != MD_1845 && devc->model != MD_1845_SSCAPE)
1258 * CS4231 compatible devices don't have separate sampling rate selection
1259 * register for recording an playback. The I8 register is shared so we have to
1260 * set the speed encoding bits of it too.
1262 unsigned char tmp = portc->speed_bits | (ad_read(devc, 8) & 0xf0);
1264 ad_write(devc, 8, tmp);
1266 * Write to I8 starts resynchronization. Wait until it completes.
1268 timeout = 0;
1269 while (timeout < 100 && inb(devc->base) != 0x80)
1270 timeout++;
1272 timeout = 0;
1273 while (timeout < 10000 && inb(devc->base) == 0x80)
1274 timeout++;
1277 else
1278 { /* For AD1848 set I8. */
1280 old_fs = ad_read(devc, 8);
1281 ad_write(devc, 8, fs);
1283 * Write to I8 starts resynchronization. Wait until it completes.
1285 timeout = 0;
1286 while (timeout < 100 && inb(devc->base) != 0x80)
1287 timeout++;
1288 timeout = 0;
1289 while (timeout < 10000 && inb(devc->base) == 0x80)
1290 timeout++;
1293 if (devc->model == MD_4232)
1294 ad_write(devc, 16, tmp & ~0x30);
1296 ad_leave_MCE(devc); /*
1297 * Starts the calibration process.
1299 spin_unlock_irqrestore(&devc->lock,flags);
1300 devc->xfer_count = 0;
1302 #ifndef EXCLUDE_TIMERS
1303 if (dev == timer_installed && devc->timer_running)
1305 if ((fs & 0x01) != (old_fs & 0x01))
1307 ad1848_tmr_reprogram(dev);
1310 #endif
1311 ad1848_halt_input(dev);
1312 return 0;
1315 static void ad1848_halt(int dev)
1317 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1318 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1320 unsigned char bits = ad_read(devc, 9);
1322 if (bits & 0x01 && (portc->open_mode & OPEN_WRITE))
1323 ad1848_halt_output(dev);
1325 if (bits & 0x02 && (portc->open_mode & OPEN_READ))
1326 ad1848_halt_input(dev);
1327 devc->audio_mode = 0;
1330 static void ad1848_halt_input(int dev)
1332 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1333 unsigned long flags;
1335 if (!(ad_read(devc, 9) & 0x02))
1336 return; /* Capture not enabled */
1338 spin_lock_irqsave(&devc->lock,flags);
1340 ad_mute(devc);
1343 int tmout;
1345 if(!isa_dma_bridge_buggy)
1346 disable_dma(audio_devs[dev]->dmap_in->dma);
1348 for (tmout = 0; tmout < 100000; tmout++)
1349 if (ad_read(devc, 11) & 0x10)
1350 break;
1351 ad_write(devc, 9, ad_read(devc, 9) & ~0x02); /* Stop capture */
1353 if(!isa_dma_bridge_buggy)
1354 enable_dma(audio_devs[dev]->dmap_in->dma);
1355 devc->audio_mode &= ~PCM_ENABLE_INPUT;
1358 outb(0, io_Status(devc)); /* Clear interrupt status */
1359 outb(0, io_Status(devc)); /* Clear interrupt status */
1361 devc->audio_mode &= ~PCM_ENABLE_INPUT;
1363 spin_unlock_irqrestore(&devc->lock,flags);
1366 static void ad1848_halt_output(int dev)
1368 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1369 unsigned long flags;
1371 if (!(ad_read(devc, 9) & 0x01))
1372 return; /* Playback not enabled */
1374 spin_lock_irqsave(&devc->lock,flags);
1376 ad_mute(devc);
1378 int tmout;
1380 if(!isa_dma_bridge_buggy)
1381 disable_dma(audio_devs[dev]->dmap_out->dma);
1383 for (tmout = 0; tmout < 100000; tmout++)
1384 if (ad_read(devc, 11) & 0x10)
1385 break;
1386 ad_write(devc, 9, ad_read(devc, 9) & ~0x01); /* Stop playback */
1388 if(!isa_dma_bridge_buggy)
1389 enable_dma(audio_devs[dev]->dmap_out->dma);
1391 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
1394 outb((0), io_Status(devc)); /* Clear interrupt status */
1395 outb((0), io_Status(devc)); /* Clear interrupt status */
1397 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
1399 spin_unlock_irqrestore(&devc->lock,flags);
1402 static void ad1848_trigger(int dev, int state)
1404 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1405 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1406 unsigned long flags;
1407 unsigned char tmp, old;
1409 spin_lock_irqsave(&devc->lock,flags);
1410 state &= devc->audio_mode;
1412 tmp = old = ad_read(devc, 9);
1414 if (portc->open_mode & OPEN_READ)
1416 if (state & PCM_ENABLE_INPUT)
1417 tmp |= 0x02;
1418 else
1419 tmp &= ~0x02;
1421 if (portc->open_mode & OPEN_WRITE)
1423 if (state & PCM_ENABLE_OUTPUT)
1424 tmp |= 0x01;
1425 else
1426 tmp &= ~0x01;
1428 /* ad_mute(devc); */
1429 if (tmp != old)
1431 ad_write(devc, 9, tmp);
1432 ad_unmute(devc);
1434 spin_unlock_irqrestore(&devc->lock,flags);
1437 static void ad1848_init_hw(ad1848_info * devc)
1439 int i;
1440 int *init_values;
1443 * Initial values for the indirect registers of CS4248/AD1848.
1445 static int init_values_a[] =
1447 0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
1448 0x00, 0x0c, 0x02, 0x00, 0x8a, 0x01, 0x00, 0x00,
1450 /* Positions 16 to 31 just for CS4231/2 and ad1845 */
1451 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x1f, 0x40,
1452 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1455 static int init_values_b[] =
1458 Values for the newer chips
1459 Some of the register initialization values were changed. In
1460 order to get rid of the click that preceded PCM playback,
1461 calibration was disabled on the 10th byte. On that same byte,
1462 dual DMA was enabled; on the 11th byte, ADC dithering was
1463 enabled, since that is theoretically desirable; on the 13th
1464 byte, Mode 3 was selected, to enable access to extended
1465 registers.
1467 0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
1468 0x00, 0x00, 0x06, 0x00, 0xe0, 0x01, 0x00, 0x00,
1469 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x1f, 0x40,
1470 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1474 * Select initialisation data
1477 init_values = init_values_a;
1478 if(devc->model >= MD_4236)
1479 init_values = init_values_b;
1481 for (i = 0; i < 16; i++)
1482 ad_write(devc, i, init_values[i]);
1485 ad_mute(devc); /* Initialize some variables */
1486 ad_unmute(devc); /* Leave it unmuted now */
1488 if (devc->model > MD_1848)
1490 if (devc->model == MD_1845_SSCAPE)
1491 ad_write(devc, 12, ad_read(devc, 12) | 0x50);
1492 else
1493 ad_write(devc, 12, ad_read(devc, 12) | 0x40); /* Mode2 = enabled */
1495 if (devc->model == MD_IWAVE)
1496 ad_write(devc, 12, 0x6c); /* Select codec mode 3 */
1498 if (devc->model != MD_1845_SSCAPE)
1499 for (i = 16; i < 32; i++)
1500 ad_write(devc, i, init_values[i]);
1502 if (devc->model == MD_IWAVE)
1503 ad_write(devc, 16, 0x30); /* Playback and capture counters enabled */
1505 if (devc->model > MD_1848)
1507 if (devc->audio_flags & DMA_DUPLEX)
1508 ad_write(devc, 9, ad_read(devc, 9) & ~0x04); /* Dual DMA mode */
1509 else
1510 ad_write(devc, 9, ad_read(devc, 9) | 0x04); /* Single DMA mode */
1512 if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE)
1513 ad_write(devc, 27, ad_read(devc, 27) | 0x08); /* Alternate freq select enabled */
1515 if (devc->model == MD_IWAVE)
1516 { /* Some magic Interwave specific initialization */
1517 ad_write(devc, 12, 0x6c); /* Select codec mode 3 */
1518 ad_write(devc, 16, 0x30); /* Playback and capture counters enabled */
1519 ad_write(devc, 17, 0xc2); /* Alternate feature enable */
1522 else
1524 devc->audio_flags &= ~DMA_DUPLEX;
1525 ad_write(devc, 9, ad_read(devc, 9) | 0x04); /* Single DMA mode */
1526 if (soundpro)
1527 ad_write(devc, 12, ad_read(devc, 12) | 0x40); /* Mode2 = enabled */
1530 outb((0), io_Status(devc)); /* Clear pending interrupts */
1533 * Toggle the MCE bit. It completes the initialization phase.
1536 ad_enter_MCE(devc); /* In case the bit was off */
1537 ad_leave_MCE(devc);
1539 ad1848_mixer_reset(devc);
1542 int ad1848_detect(int io_base, int *ad_flags, int *osp)
1544 unsigned char tmp;
1545 ad1848_info *devc = &adev_info[nr_ad1848_devs];
1546 unsigned char tmp1 = 0xff, tmp2 = 0xff;
1547 int optiC930 = 0; /* OPTi 82C930 flag */
1548 int interwave = 0;
1549 int ad1847_flag = 0;
1550 int cs4248_flag = 0;
1551 int sscape_flag = 0;
1553 int i;
1555 DDB(printk("ad1848_detect(%x)\n", io_base));
1557 if (ad_flags)
1559 if (*ad_flags == 0x12345678)
1561 interwave = 1;
1562 *ad_flags = 0;
1565 if (*ad_flags == 0x87654321)
1567 sscape_flag = 1;
1568 *ad_flags = 0;
1571 if (*ad_flags == 0x12345677)
1573 cs4248_flag = 1;
1574 *ad_flags = 0;
1577 if (nr_ad1848_devs >= MAX_AUDIO_DEV)
1579 printk(KERN_ERR "ad1848 - Too many audio devices\n");
1580 return 0;
1582 if (check_region(io_base, 4))
1584 printk(KERN_ERR "ad1848.c: Port %x not free.\n", io_base);
1585 return 0;
1587 devc->base = io_base;
1588 devc->irq_ok = 0;
1589 devc->timer_running = 0;
1590 devc->MCE_bit = 0x40;
1591 devc->irq = 0;
1592 devc->open_mode = 0;
1593 devc->chip_name = devc->name = "AD1848";
1594 devc->model = MD_1848; /* AD1848 or CS4248 */
1595 devc->levels = NULL;
1596 devc->debug_flag = 0;
1599 * Check that the I/O address is in use.
1601 * The bit 0x80 of the base I/O port is known to be 0 after the
1602 * chip has performed its power on initialization. Just assume
1603 * this has happened before the OS is starting.
1605 * If the I/O address is unused, it typically returns 0xff.
1608 if (inb(devc->base) == 0xff)
1610 DDB(printk("ad1848_detect: The base I/O address appears to be dead\n"));
1614 * Wait for the device to stop initialization
1617 DDB(printk("ad1848_detect() - step 0\n"));
1619 for (i = 0; i < 10000000; i++)
1621 unsigned char x = inb(devc->base);
1623 if (x == 0xff || !(x & 0x80))
1624 break;
1627 DDB(printk("ad1848_detect() - step A\n"));
1629 if (inb(devc->base) == 0x80) /* Not ready. Let's wait */
1630 ad_leave_MCE(devc);
1632 if ((inb(devc->base) & 0x80) != 0x00) /* Not a AD1848 */
1634 DDB(printk("ad1848 detect error - step A (%02x)\n", (int) inb(devc->base)));
1635 return 0;
1639 * Test if it's possible to change contents of the indirect registers.
1640 * Registers 0 and 1 are ADC volume registers. The bit 0x10 is read only
1641 * so try to avoid using it.
1644 DDB(printk("ad1848_detect() - step B\n"));
1645 ad_write(devc, 0, 0xaa);
1646 ad_write(devc, 1, 0x45); /* 0x55 with bit 0x10 clear */
1648 if ((tmp1 = ad_read(devc, 0)) != 0xaa || (tmp2 = ad_read(devc, 1)) != 0x45)
1650 if (tmp2 == 0x65) /* AD1847 has couple of bits hardcoded to 1 */
1651 ad1847_flag = 1;
1652 else
1654 DDB(printk("ad1848 detect error - step B (%x/%x)\n", tmp1, tmp2));
1655 return 0;
1658 DDB(printk("ad1848_detect() - step C\n"));
1659 ad_write(devc, 0, 0x45);
1660 ad_write(devc, 1, 0xaa);
1662 if ((tmp1 = ad_read(devc, 0)) != 0x45 || (tmp2 = ad_read(devc, 1)) != 0xaa)
1664 if (tmp2 == 0x8a) /* AD1847 has few bits hardcoded to 1 */
1665 ad1847_flag = 1;
1666 else
1668 DDB(printk("ad1848 detect error - step C (%x/%x)\n", tmp1, tmp2));
1669 return 0;
1674 * The indirect register I12 has some read only bits. Let's
1675 * try to change them.
1678 DDB(printk("ad1848_detect() - step D\n"));
1679 tmp = ad_read(devc, 12);
1680 ad_write(devc, 12, (~tmp) & 0x0f);
1682 if ((tmp & 0x0f) != ((tmp1 = ad_read(devc, 12)) & 0x0f))
1684 DDB(printk("ad1848 detect error - step D (%x)\n", tmp1));
1685 return 0;
1689 * NOTE! Last 4 bits of the reg I12 tell the chip revision.
1690 * 0x01=RevB and 0x0A=RevC.
1694 * The original AD1848/CS4248 has just 15 indirect registers. This means
1695 * that I0 and I16 should return the same value (etc.).
1696 * However this doesn't work with CS4248. Actually it seems to be impossible
1697 * to detect if the chip is a CS4231 or CS4248.
1698 * Ensure that the Mode2 enable bit of I12 is 0. Otherwise this test fails
1699 * with CS4231.
1703 * OPTi 82C930 has mode2 control bit in another place. This test will fail
1704 * with it. Accept this situation as a possible indication of this chip.
1707 DDB(printk("ad1848_detect() - step F\n"));
1708 ad_write(devc, 12, 0); /* Mode2=disabled */
1710 for (i = 0; i < 16; i++)
1712 if ((tmp1 = ad_read(devc, i)) != (tmp2 = ad_read(devc, i + 16)))
1714 DDB(printk("ad1848 detect step F(%d/%x/%x) - OPTi chip???\n", i, tmp1, tmp2));
1715 if (!ad1847_flag)
1716 optiC930 = 1;
1717 break;
1722 * Try to switch the chip to mode2 (CS4231) by setting the MODE2 bit (0x40).
1723 * The bit 0x80 is always 1 in CS4248 and CS4231.
1726 DDB(printk("ad1848_detect() - step G\n"));
1728 if (ad_flags && *ad_flags == 400)
1729 *ad_flags = 0;
1730 else
1731 ad_write(devc, 12, 0x40); /* Set mode2, clear 0x80 */
1734 if (ad_flags)
1735 *ad_flags = 0;
1737 tmp1 = ad_read(devc, 12);
1738 if (tmp1 & 0x80)
1740 if (ad_flags)
1741 *ad_flags |= AD_F_CS4248;
1743 devc->chip_name = "CS4248"; /* Our best knowledge just now */
1745 if (optiC930 || (tmp1 & 0xc0) == (0x80 | 0x40))
1748 * CS4231 detected - is it?
1750 * Verify that setting I0 doesn't change I16.
1753 DDB(printk("ad1848_detect() - step H\n"));
1754 ad_write(devc, 16, 0); /* Set I16 to known value */
1756 ad_write(devc, 0, 0x45);
1757 if ((tmp1 = ad_read(devc, 16)) != 0x45) /* No change -> CS4231? */
1759 ad_write(devc, 0, 0xaa);
1760 if ((tmp1 = ad_read(devc, 16)) == 0xaa) /* Rotten bits? */
1762 DDB(printk("ad1848 detect error - step H(%x)\n", tmp1));
1763 return 0;
1767 * Verify that some bits of I25 are read only.
1770 DDB(printk("ad1848_detect() - step I\n"));
1771 tmp1 = ad_read(devc, 25); /* Original bits */
1772 ad_write(devc, 25, ~tmp1); /* Invert all bits */
1773 if ((ad_read(devc, 25) & 0xe7) == (tmp1 & 0xe7))
1775 int id;
1778 * It's at least CS4231
1781 devc->chip_name = "CS4231";
1782 devc->model = MD_4231;
1785 * It could be an AD1845 or CS4231A as well.
1786 * CS4231 and AD1845 report the same revision info in I25
1787 * while the CS4231A reports different.
1790 id = ad_read(devc, 25);
1791 if ((id & 0xe7) == 0x80) /* Device busy??? */
1792 id = ad_read(devc, 25);
1793 if ((id & 0xe7) == 0x80) /* Device still busy??? */
1794 id = ad_read(devc, 25);
1795 DDB(printk("ad1848_detect() - step J (%02x/%02x)\n", id, ad_read(devc, 25)));
1797 if ((id & 0xe7) == 0x80) {
1799 * It must be a CS4231 or AD1845. The register I23 of
1800 * CS4231 is undefined and it appears to be read only.
1801 * AD1845 uses I23 for setting sample rate. Assume
1802 * the chip is AD1845 if I23 is changeable.
1805 unsigned char tmp = ad_read(devc, 23);
1806 ad_write(devc, 23, ~tmp);
1808 if (interwave)
1810 devc->model = MD_IWAVE;
1811 devc->chip_name = "IWave";
1813 else if (ad_read(devc, 23) != tmp) /* AD1845 ? */
1815 devc->chip_name = "AD1845";
1816 devc->model = MD_1845;
1818 else if (cs4248_flag)
1820 if (ad_flags)
1821 *ad_flags |= AD_F_CS4248;
1822 devc->chip_name = "CS4248";
1823 devc->model = MD_1848;
1824 ad_write(devc, 12, ad_read(devc, 12) & ~0x40); /* Mode2 off */
1826 ad_write(devc, 23, tmp); /* Restore */
1828 else
1830 switch (id & 0x1f) {
1831 case 3: /* CS4236/CS4235/CS42xB/CS4239 */
1833 int xid;
1834 ad_write(devc, 12, ad_read(devc, 12) | 0x60); /* switch to mode 3 */
1835 ad_write(devc, 23, 0x9c); /* select extended register 25 */
1836 xid = inb(io_Indexed_Data(devc));
1837 ad_write(devc, 12, ad_read(devc, 12) & ~0x60); /* back to mode 0 */
1838 switch (xid & 0x1f)
1840 case 0x00:
1841 devc->chip_name = "CS4237B(B)";
1842 devc->model = MD_42xB;
1843 break;
1844 case 0x08:
1845 /* Seems to be a 4238 ?? */
1846 devc->chip_name = "CS4238";
1847 devc->model = MD_42xB;
1848 break;
1849 case 0x09:
1850 devc->chip_name = "CS4238B";
1851 devc->model = MD_42xB;
1852 break;
1853 case 0x0b:
1854 devc->chip_name = "CS4236B";
1855 devc->model = MD_4236;
1856 break;
1857 case 0x10:
1858 devc->chip_name = "CS4237B";
1859 devc->model = MD_42xB;
1860 break;
1861 case 0x1d:
1862 devc->chip_name = "CS4235";
1863 devc->model = MD_4235;
1864 break;
1865 case 0x1e:
1866 devc->chip_name = "CS4239";
1867 devc->model = MD_4239;
1868 break;
1869 default:
1870 printk("Chip ident is %X.\n", xid&0x1F);
1871 devc->chip_name = "CS42xx";
1872 devc->model = MD_4232;
1873 break;
1876 break;
1878 case 2: /* CS4232/CS4232A */
1879 devc->chip_name = "CS4232";
1880 devc->model = MD_4232;
1881 break;
1883 case 0:
1884 if ((id & 0xe0) == 0xa0)
1886 devc->chip_name = "CS4231A";
1887 devc->model = MD_4231A;
1889 else
1891 devc->chip_name = "CS4321";
1892 devc->model = MD_4231;
1894 break;
1896 default: /* maybe */
1897 DDB(printk("ad1848: I25 = %02x/%02x\n", ad_read(devc, 25), ad_read(devc, 25) & 0xe7));
1898 if (optiC930)
1900 devc->chip_name = "82C930";
1901 devc->model = MD_C930;
1903 else
1905 devc->chip_name = "CS4231";
1906 devc->model = MD_4231;
1911 ad_write(devc, 25, tmp1); /* Restore bits */
1913 DDB(printk("ad1848_detect() - step K\n"));
1915 } else if (tmp1 == 0x0a) {
1917 * Is it perhaps a SoundPro CMI8330?
1918 * If so, then we should be able to change indirect registers
1919 * greater than I15 after activating MODE2, even though reading
1920 * back I12 does not show it.
1924 * Let's try comparing register values
1926 for (i = 0; i < 16; i++) {
1927 if ((tmp1 = ad_read(devc, i)) != (tmp2 = ad_read(devc, i + 16))) {
1928 DDB(printk("ad1848 detect step H(%d/%x/%x) - SoundPro chip?\n", i, tmp1, tmp2));
1929 soundpro = 1;
1930 devc->chip_name = "SoundPro CMI 8330";
1931 break;
1936 DDB(printk("ad1848_detect() - step L\n"));
1937 if (ad_flags)
1939 if (devc->model != MD_1848)
1940 *ad_flags |= AD_F_CS4231;
1942 DDB(printk("ad1848_detect() - Detected OK\n"));
1944 if (devc->model == MD_1848 && ad1847_flag)
1945 devc->chip_name = "AD1847";
1948 if (sscape_flag == 1)
1949 devc->model = MD_1845_SSCAPE;
1951 return 1;
1954 int ad1848_init (char *name, int io_base, int irq, int dma_playback,
1955 int dma_capture, int share_dma, int *osp, struct module *owner)
1958 * NOTE! If irq < 0, there is another driver which has allocated the IRQ
1959 * so that this driver doesn't need to allocate/deallocate it.
1960 * The actually used IRQ is ABS(irq).
1963 int my_dev;
1964 char dev_name[100];
1965 int e;
1967 ad1848_info *devc = &adev_info[nr_ad1848_devs];
1969 ad1848_port_info *portc = NULL;
1971 spin_lock_init(&devc->lock);
1972 devc->irq = (irq > 0) ? irq : 0;
1973 devc->open_mode = 0;
1974 devc->timer_ticks = 0;
1975 devc->dma1 = dma_playback;
1976 devc->dma2 = dma_capture;
1977 devc->subtype = cfg.card_subtype;
1978 devc->audio_flags = DMA_AUTOMODE;
1979 devc->playback_dev = devc->record_dev = 0;
1980 if (name != NULL)
1981 devc->name = name;
1983 if (name != NULL && name[0] != 0)
1984 sprintf(dev_name,
1985 "%s (%s)", name, devc->chip_name);
1986 else
1987 sprintf(dev_name,
1988 "Generic audio codec (%s)", devc->chip_name);
1990 if (!request_region(devc->base, 4, devc->name))
1991 return -1;
1993 conf_printf2(dev_name, devc->base, devc->irq, dma_playback, dma_capture);
1995 if (devc->model == MD_1848 || devc->model == MD_C930)
1996 devc->audio_flags |= DMA_HARDSTOP;
1998 if (devc->model > MD_1848)
2000 if (devc->dma1 == devc->dma2 || devc->dma2 == -1 || devc->dma1 == -1)
2001 devc->audio_flags &= ~DMA_DUPLEX;
2002 else
2003 devc->audio_flags |= DMA_DUPLEX;
2006 portc = (ad1848_port_info *) kmalloc(sizeof(ad1848_port_info), GFP_KERNEL);
2007 if(portc==NULL) {
2008 release_region(devc->base, 4);
2009 return -1;
2012 if ((my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
2013 dev_name,
2014 &ad1848_audio_driver,
2015 sizeof(struct audio_driver),
2016 devc->audio_flags,
2017 ad_format_mask[devc->model],
2018 devc,
2019 dma_playback,
2020 dma_capture)) < 0)
2022 release_region(devc->base, 4);
2023 kfree(portc);
2024 return -1;
2027 audio_devs[my_dev]->portc = portc;
2028 audio_devs[my_dev]->mixer_dev = -1;
2029 if (owner)
2030 audio_devs[my_dev]->d->owner = owner;
2031 memset((char *) portc, 0, sizeof(*portc));
2033 nr_ad1848_devs++;
2035 devc->pmdev = pm_register(PM_ISA_DEV, my_dev, ad1848_pm_callback);
2036 if (devc->pmdev)
2037 devc->pmdev->data = devc;
2039 ad1848_init_hw(devc);
2041 if (irq > 0)
2043 devc->dev_no = my_dev;
2044 if (request_irq(devc->irq, adintr, 0, devc->name, (void *)my_dev) < 0)
2046 printk(KERN_WARNING "ad1848: Unable to allocate IRQ\n");
2047 /* Don't free it either then.. */
2048 devc->irq = 0;
2050 if (capabilities[devc->model].flags & CAP_F_TIMER)
2052 #ifndef CONFIG_SMP
2053 int x;
2054 unsigned char tmp = ad_read(devc, 16);
2055 #endif
2057 devc->timer_ticks = 0;
2059 ad_write(devc, 21, 0x00); /* Timer MSB */
2060 ad_write(devc, 20, 0x10); /* Timer LSB */
2061 #ifndef CONFIG_SMP
2062 ad_write(devc, 16, tmp | 0x40); /* Enable timer */
2063 for (x = 0; x < 100000 && devc->timer_ticks == 0; x++);
2064 ad_write(devc, 16, tmp & ~0x40); /* Disable timer */
2066 if (devc->timer_ticks == 0)
2067 printk(KERN_WARNING "ad1848: Interrupt test failed (IRQ%d)\n", irq);
2068 else
2070 DDB(printk("Interrupt test OK\n"));
2071 devc->irq_ok = 1;
2073 #else
2074 devc->irq_ok = 1;
2075 #endif
2077 else
2078 devc->irq_ok = 1; /* Couldn't test. assume it's OK */
2079 } else if (irq < 0)
2080 irq2dev[-irq] = devc->dev_no = my_dev;
2082 #ifndef EXCLUDE_TIMERS
2083 if ((capabilities[devc->model].flags & CAP_F_TIMER) &&
2084 devc->irq_ok)
2085 ad1848_tmr_install(my_dev);
2086 #endif
2088 if (!share_dma)
2090 if (sound_alloc_dma(dma_playback, devc->name))
2091 printk(KERN_WARNING "ad1848.c: Can't allocate DMA%d\n", dma_playback);
2093 if (dma_capture != dma_playback)
2094 if (sound_alloc_dma(dma_capture, devc->name))
2095 printk(KERN_WARNING "ad1848.c: Can't allocate DMA%d\n", dma_capture);
2098 if ((e = sound_install_mixer(MIXER_DRIVER_VERSION,
2099 dev_name,
2100 &ad1848_mixer_operations,
2101 sizeof(struct mixer_operations),
2102 devc)) >= 0)
2104 audio_devs[my_dev]->mixer_dev = e;
2105 if (owner)
2106 mixer_devs[e]->owner = owner;
2108 return my_dev;
2111 int ad1848_control(int cmd, int arg)
2113 ad1848_info *devc;
2115 if (nr_ad1848_devs < 1)
2116 return -ENODEV;
2118 devc = &adev_info[nr_ad1848_devs - 1];
2120 switch (cmd)
2122 case AD1848_SET_XTAL: /* Change clock frequency of AD1845 (only ) */
2123 if (devc->model != MD_1845 || devc->model != MD_1845_SSCAPE)
2124 return -EINVAL;
2125 ad_enter_MCE(devc);
2126 ad_write(devc, 29, (ad_read(devc, 29) & 0x1f) | (arg << 5));
2127 ad_leave_MCE(devc);
2128 break;
2130 case AD1848_MIXER_REROUTE:
2132 int o = (arg >> 8) & 0xff;
2133 int n = arg & 0xff;
2135 if (o < 0 || o >= SOUND_MIXER_NRDEVICES)
2136 return -EINVAL;
2138 if (!(devc->supported_devices & (1 << o)) &&
2139 !(devc->supported_rec_devices & (1 << o)))
2140 return -EINVAL;
2142 if (n == SOUND_MIXER_NONE)
2143 { /* Just hide this control */
2144 ad1848_mixer_set(devc, o, 0); /* Shut up it */
2145 devc->supported_devices &= ~(1 << o);
2146 devc->supported_rec_devices &= ~(1 << o);
2147 break;
2150 /* Make the mixer control identified by o to appear as n */
2151 if (n < 0 || n >= SOUND_MIXER_NRDEVICES)
2152 return -EINVAL;
2154 devc->mixer_reroute[n] = o; /* Rename the control */
2155 if (devc->supported_devices & (1 << o))
2156 devc->supported_devices |= (1 << n);
2157 if (devc->supported_rec_devices & (1 << o))
2158 devc->supported_rec_devices |= (1 << n);
2160 devc->supported_devices &= ~(1 << o);
2161 devc->supported_rec_devices &= ~(1 << o);
2163 break;
2165 return 0;
2168 void ad1848_unload(int io_base, int irq, int dma_playback, int dma_capture, int share_dma)
2170 int i, mixer, dev = 0;
2171 ad1848_info *devc = NULL;
2173 for (i = 0; devc == NULL && i < nr_ad1848_devs; i++)
2175 if (adev_info[i].base == io_base)
2177 devc = &adev_info[i];
2178 dev = devc->dev_no;
2182 if (devc != NULL)
2184 if(audio_devs[dev]->portc!=NULL)
2185 kfree(audio_devs[dev]->portc);
2186 release_region(devc->base, 4);
2188 if (!share_dma)
2190 if (devc->irq > 0) /* There is no point in freeing irq, if it wasn't allocated */
2191 free_irq(devc->irq, (void *)devc->dev_no);
2193 sound_free_dma(dma_playback);
2195 if (dma_playback != dma_capture)
2196 sound_free_dma(dma_capture);
2199 mixer = audio_devs[devc->dev_no]->mixer_dev;
2200 if(mixer>=0)
2201 sound_unload_mixerdev(mixer);
2203 if (devc->pmdev)
2204 pm_unregister(devc->pmdev);
2206 nr_ad1848_devs--;
2207 for ( ; i < nr_ad1848_devs ; i++)
2208 adev_info[i] = adev_info[i+1];
2210 else
2211 printk(KERN_ERR "ad1848: Can't find device to be unloaded. Base=%x\n", io_base);
2214 void adintr(int irq, void *dev_id, struct pt_regs *dummy)
2216 unsigned char status;
2217 ad1848_info *devc;
2218 int dev;
2219 int alt_stat = 0xff;
2220 unsigned char c930_stat = 0;
2221 int cnt = 0;
2223 dev = (int)dev_id;
2224 devc = (ad1848_info *) audio_devs[dev]->devc;
2226 interrupt_again: /* Jump back here if int status doesn't reset */
2228 status = inb(io_Status(devc));
2230 if (status == 0x80)
2231 printk(KERN_DEBUG "adintr: Why?\n");
2232 if (devc->model == MD_1848)
2233 outb((0), io_Status(devc)); /* Clear interrupt status */
2235 if (status & 0x01)
2237 if (devc->model == MD_C930)
2238 { /* 82C930 has interrupt status register in MAD16 register MC11 */
2240 spin_lock(&devc->lock);
2242 /* 0xe0e is C930 address port
2243 * 0xe0f is C930 data port
2245 outb(11, 0xe0e);
2246 c930_stat = inb(0xe0f);
2247 outb((~c930_stat), 0xe0f);
2249 spin_unlock(&devc->lock);
2251 alt_stat = (c930_stat << 2) & 0x30;
2253 else if (devc->model != MD_1848)
2255 alt_stat = ad_read(devc, 24);
2256 ad_write(devc, 24, ad_read(devc, 24) & ~alt_stat); /* Selective ack */
2259 if ((devc->open_mode & OPEN_READ) && (devc->audio_mode & PCM_ENABLE_INPUT) && (alt_stat & 0x20))
2261 DMAbuf_inputintr(devc->record_dev);
2263 if ((devc->open_mode & OPEN_WRITE) && (devc->audio_mode & PCM_ENABLE_OUTPUT) &&
2264 (alt_stat & 0x10))
2266 DMAbuf_outputintr(devc->playback_dev, 1);
2268 if (devc->model != MD_1848 && (alt_stat & 0x40)) /* Timer interrupt */
2270 devc->timer_ticks++;
2271 #ifndef EXCLUDE_TIMERS
2272 if (timer_installed == dev && devc->timer_running)
2273 sound_timer_interrupt();
2274 #endif
2278 * Sometimes playback or capture interrupts occur while a timer interrupt
2279 * is being handled. The interrupt will not be retriggered if we don't
2280 * handle it now. Check if an interrupt is still pending and restart
2281 * the handler in this case.
2283 if (inb(io_Status(devc)) & 0x01 && cnt++ < 4)
2285 goto interrupt_again;
2290 * Experimental initialization sequence for the integrated sound system
2291 * of the Compaq Deskpro M.
2294 static int init_deskpro_m(struct address_info *hw_config)
2296 unsigned char tmp;
2298 if ((tmp = inb(0xc44)) == 0xff)
2300 DDB(printk("init_deskpro_m: Dead port 0xc44\n"));
2301 return 0;
2304 outb(0x10, 0xc44);
2305 outb(0x40, 0xc45);
2306 outb(0x00, 0xc46);
2307 outb(0xe8, 0xc47);
2308 outb(0x14, 0xc44);
2309 outb(0x40, 0xc45);
2310 outb(0x00, 0xc46);
2311 outb(0xe8, 0xc47);
2312 outb(0x10, 0xc44);
2314 return 1;
2318 * Experimental initialization sequence for the integrated sound system
2319 * of Compaq Deskpro XL.
2322 static int init_deskpro(struct address_info *hw_config)
2324 unsigned char tmp;
2326 if ((tmp = inb(0xc44)) == 0xff)
2328 DDB(printk("init_deskpro: Dead port 0xc44\n"));
2329 return 0;
2331 outb((tmp | 0x04), 0xc44); /* Select bank 1 */
2332 if (inb(0xc44) != 0x04)
2334 DDB(printk("init_deskpro: Invalid bank1 signature in port 0xc44\n"));
2335 return 0;
2338 * OK. It looks like a Deskpro so let's proceed.
2342 * I/O port 0xc44 Audio configuration register.
2344 * bits 0xc0: Audio revision bits
2345 * 0x00 = Compaq Business Audio
2346 * 0x40 = MS Sound System Compatible (reset default)
2347 * 0x80 = Reserved
2348 * 0xc0 = Reserved
2349 * bit 0x20: No Wait State Enable
2350 * 0x00 = Disabled (reset default, DMA mode)
2351 * 0x20 = Enabled (programmed I/O mode)
2352 * bit 0x10: MS Sound System Decode Enable
2353 * 0x00 = Decoding disabled (reset default)
2354 * 0x10 = Decoding enabled
2355 * bit 0x08: FM Synthesis Decode Enable
2356 * 0x00 = Decoding Disabled (reset default)
2357 * 0x08 = Decoding enabled
2358 * bit 0x04 Bank select
2359 * 0x00 = Bank 0
2360 * 0x04 = Bank 1
2361 * bits 0x03 MSS Base address
2362 * 0x00 = 0x530 (reset default)
2363 * 0x01 = 0x604
2364 * 0x02 = 0xf40
2365 * 0x03 = 0xe80
2368 #ifdef DEBUGXL
2369 /* Debug printing */
2370 printk("Port 0xc44 (before): ");
2371 outb((tmp & ~0x04), 0xc44);
2372 printk("%02x ", inb(0xc44));
2373 outb((tmp | 0x04), 0xc44);
2374 printk("%02x\n", inb(0xc44));
2375 #endif
2377 /* Set bank 1 of the register */
2378 tmp = 0x58; /* MSS Mode, MSS&FM decode enabled */
2380 switch (hw_config->io_base)
2382 case 0x530:
2383 tmp |= 0x00;
2384 break;
2385 case 0x604:
2386 tmp |= 0x01;
2387 break;
2388 case 0xf40:
2389 tmp |= 0x02;
2390 break;
2391 case 0xe80:
2392 tmp |= 0x03;
2393 break;
2394 default:
2395 DDB(printk("init_deskpro: Invalid MSS port %x\n", hw_config->io_base));
2396 return 0;
2398 outb((tmp & ~0x04), 0xc44); /* Write to bank=0 */
2400 #ifdef DEBUGXL
2401 /* Debug printing */
2402 printk("Port 0xc44 (after): ");
2403 outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
2404 printk("%02x ", inb(0xc44));
2405 outb((tmp | 0x04), 0xc44); /* Select bank=1 */
2406 printk("%02x\n", inb(0xc44));
2407 #endif
2410 * I/O port 0xc45 FM Address Decode/MSS ID Register.
2412 * bank=0, bits 0xfe: FM synthesis Decode Compare bits 7:1 (default=0x88)
2413 * bank=0, bit 0x01: SBIC Power Control Bit
2414 * 0x00 = Powered up
2415 * 0x01 = Powered down
2416 * bank=1, bits 0xfc: MSS ID (default=0x40)
2419 #ifdef DEBUGXL
2420 /* Debug printing */
2421 printk("Port 0xc45 (before): ");
2422 outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
2423 printk("%02x ", inb(0xc45));
2424 outb((tmp | 0x04), 0xc44); /* Select bank=1 */
2425 printk("%02x\n", inb(0xc45));
2426 #endif
2428 outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
2429 outb((0x88), 0xc45); /* FM base 7:0 = 0x88 */
2430 outb((tmp | 0x04), 0xc44); /* Select bank=1 */
2431 outb((0x10), 0xc45); /* MSS ID = 0x10 (MSS port returns 0x04) */
2433 #ifdef DEBUGXL
2434 /* Debug printing */
2435 printk("Port 0xc45 (after): ");
2436 outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
2437 printk("%02x ", inb(0xc45));
2438 outb((tmp | 0x04), 0xc44); /* Select bank=1 */
2439 printk("%02x\n", inb(0xc45));
2440 #endif
2444 * I/O port 0xc46 FM Address Decode/Address ASIC Revision Register.
2446 * bank=0, bits 0xff: FM synthesis Decode Compare bits 15:8 (default=0x03)
2447 * bank=1, bits 0xff: Audio addressing ASIC id
2450 #ifdef DEBUGXL
2451 /* Debug printing */
2452 printk("Port 0xc46 (before): ");
2453 outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
2454 printk("%02x ", inb(0xc46));
2455 outb((tmp | 0x04), 0xc44); /* Select bank=1 */
2456 printk("%02x\n", inb(0xc46));
2457 #endif
2459 outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
2460 outb((0x03), 0xc46); /* FM base 15:8 = 0x03 */
2461 outb((tmp | 0x04), 0xc44); /* Select bank=1 */
2462 outb((0x11), 0xc46); /* ASIC ID = 0x11 */
2464 #ifdef DEBUGXL
2465 /* Debug printing */
2466 printk("Port 0xc46 (after): ");
2467 outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
2468 printk("%02x ", inb(0xc46));
2469 outb((tmp | 0x04), 0xc44); /* Select bank=1 */
2470 printk("%02x\n", inb(0xc46));
2471 #endif
2474 * I/O port 0xc47 FM Address Decode Register.
2476 * bank=0, bits 0xff: Decode enable selection for various FM address bits
2477 * bank=1, bits 0xff: Reserved
2480 #ifdef DEBUGXL
2481 /* Debug printing */
2482 printk("Port 0xc47 (before): ");
2483 outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
2484 printk("%02x ", inb(0xc47));
2485 outb((tmp | 0x04), 0xc44); /* Select bank=1 */
2486 printk("%02x\n", inb(0xc47));
2487 #endif
2489 outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
2490 outb((0x7c), 0xc47); /* FM decode enable bits = 0x7c */
2491 outb((tmp | 0x04), 0xc44); /* Select bank=1 */
2492 outb((0x00), 0xc47); /* Reserved bank1 = 0x00 */
2494 #ifdef DEBUGXL
2495 /* Debug printing */
2496 printk("Port 0xc47 (after): ");
2497 outb((tmp & ~0x04), 0xc44); /* Select bank=0 */
2498 printk("%02x ", inb(0xc47));
2499 outb((tmp | 0x04), 0xc44); /* Select bank=1 */
2500 printk("%02x\n", inb(0xc47));
2501 #endif
2504 * I/O port 0xc6f = Audio Disable Function Register
2507 #ifdef DEBUGXL
2508 printk("Port 0xc6f (before) = %02x\n", inb(0xc6f));
2509 #endif
2511 outb((0x80), 0xc6f);
2513 #ifdef DEBUGXL
2514 printk("Port 0xc6f (after) = %02x\n", inb(0xc6f));
2515 #endif
2517 return 1;
2520 int probe_ms_sound(struct address_info *hw_config)
2522 unsigned char tmp;
2524 DDB(printk("Entered probe_ms_sound(%x, %d)\n", hw_config->io_base, hw_config->card_subtype));
2526 if (check_region(hw_config->io_base, 8))
2528 printk(KERN_ERR "MSS: I/O port conflict\n");
2529 return 0;
2531 if (hw_config->card_subtype == 1) /* Has no IRQ/DMA registers */
2533 /* check_opl3(0x388, hw_config); */
2534 return ad1848_detect(hw_config->io_base + 4, NULL, hw_config->osp);
2537 if (deskpro_xl && hw_config->card_subtype == 2) /* Compaq Deskpro XL */
2539 if (!init_deskpro(hw_config))
2540 return 0;
2543 if (deskpro_m) /* Compaq Deskpro M */
2545 if (!init_deskpro_m(hw_config))
2546 return 0;
2550 * Check if the IO port returns valid signature. The original MS Sound
2551 * system returns 0x04 while some cards (AudioTrix Pro for example)
2552 * return 0x00 or 0x0f.
2555 if ((tmp = inb(hw_config->io_base + 3)) == 0xff) /* Bus float */
2557 int ret;
2559 DDB(printk("I/O address is inactive (%x)\n", tmp));
2560 if (!(ret = ad1848_detect(hw_config->io_base + 4, NULL, hw_config->osp)))
2561 return 0;
2562 return 1;
2564 DDB(printk("MSS signature = %x\n", tmp & 0x3f));
2565 if ((tmp & 0x3f) != 0x04 &&
2566 (tmp & 0x3f) != 0x0f &&
2567 (tmp & 0x3f) != 0x00)
2569 int ret;
2571 MDB(printk(KERN_ERR "No MSS signature detected on port 0x%x (0x%x)\n", hw_config->io_base, (int) inb(hw_config->io_base + 3)));
2572 DDB(printk("Trying to detect codec anyway but IRQ/DMA may not work\n"));
2573 if (!(ret = ad1848_detect(hw_config->io_base + 4, NULL, hw_config->osp)))
2574 return 0;
2576 hw_config->card_subtype = 1;
2577 return 1;
2579 if ((hw_config->irq != 5) &&
2580 (hw_config->irq != 7) &&
2581 (hw_config->irq != 9) &&
2582 (hw_config->irq != 10) &&
2583 (hw_config->irq != 11) &&
2584 (hw_config->irq != 12))
2586 printk(KERN_ERR "MSS: Bad IRQ %d\n", hw_config->irq);
2587 return 0;
2589 if (hw_config->dma != 0 && hw_config->dma != 1 && hw_config->dma != 3)
2591 printk(KERN_ERR "MSS: Bad DMA %d\n", hw_config->dma);
2592 return 0;
2595 * Check that DMA0 is not in use with a 8 bit board.
2598 if (hw_config->dma == 0 && inb(hw_config->io_base + 3) & 0x80)
2600 printk(KERN_ERR "MSS: Can't use DMA0 with a 8 bit card/slot\n");
2601 return 0;
2603 if (hw_config->irq > 7 && hw_config->irq != 9 && inb(hw_config->io_base + 3) & 0x80)
2605 printk(KERN_ERR "MSS: Can't use IRQ%d with a 8 bit card/slot\n", hw_config->irq);
2606 return 0;
2608 return ad1848_detect(hw_config->io_base + 4, NULL, hw_config->osp);
2611 void attach_ms_sound(struct address_info *hw_config, struct module *owner)
2613 static signed char interrupt_bits[12] =
2615 -1, -1, -1, -1, -1, 0x00, -1, 0x08, -1, 0x10, 0x18, 0x20
2617 signed char bits;
2618 char dma2_bit = 0;
2620 static char dma_bits[4] =
2622 1, 2, 0, 3
2625 int config_port = hw_config->io_base + 0;
2626 int version_port = hw_config->io_base + 3;
2627 int dma = hw_config->dma;
2628 int dma2 = hw_config->dma2;
2630 if (hw_config->card_subtype == 1) /* Has no IRQ/DMA registers */
2632 hw_config->slots[0] = ad1848_init("MS Sound System", hw_config->io_base + 4,
2633 hw_config->irq,
2634 hw_config->dma,
2635 hw_config->dma2, 0,
2636 hw_config->osp,
2637 owner);
2638 request_region(hw_config->io_base, 4, "WSS config");
2639 return;
2642 * Set the IRQ and DMA addresses.
2645 bits = interrupt_bits[hw_config->irq];
2646 if (bits == -1)
2648 printk(KERN_ERR "MSS: Bad IRQ %d\n", hw_config->irq);
2649 return;
2651 outb((bits | 0x40), config_port);
2652 if ((inb(version_port) & 0x40) == 0)
2653 printk(KERN_ERR "[MSS: IRQ Conflict?]\n");
2656 * Handle the capture DMA channel
2659 if (dma2 != -1 && dma2 != dma)
2661 if (!((dma == 0 && dma2 == 1) ||
2662 (dma == 1 && dma2 == 0) ||
2663 (dma == 3 && dma2 == 0)))
2664 { /* Unsupported combination. Try to swap channels */
2665 int tmp = dma;
2667 dma = dma2;
2668 dma2 = tmp;
2670 if ((dma == 0 && dma2 == 1) ||
2671 (dma == 1 && dma2 == 0) ||
2672 (dma == 3 && dma2 == 0))
2674 dma2_bit = 0x04; /* Enable capture DMA */
2676 else
2678 printk(KERN_WARNING "MSS: Invalid capture DMA\n");
2679 dma2 = dma;
2682 else
2684 dma2 = dma;
2687 hw_config->dma = dma;
2688 hw_config->dma2 = dma2;
2690 outb((bits | dma_bits[dma] | dma2_bit), config_port); /* Write IRQ+DMA setup */
2692 hw_config->slots[0] = ad1848_init("MS Sound System", hw_config->io_base + 4,
2693 hw_config->irq,
2694 dma, dma2, 0,
2695 hw_config->osp,
2696 THIS_MODULE);
2697 request_region(hw_config->io_base, 4, "WSS config");
2700 void unload_ms_sound(struct address_info *hw_config)
2702 ad1848_unload(hw_config->io_base + 4,
2703 hw_config->irq,
2704 hw_config->dma,
2705 hw_config->dma2, 0);
2706 sound_unload_audiodev(hw_config->slots[0]);
2707 release_region(hw_config->io_base, 4);
2710 #ifndef EXCLUDE_TIMERS
2713 * Timer stuff (for /dev/music).
2716 static unsigned int current_interval = 0;
2718 static unsigned int ad1848_tmr_start(int dev, unsigned int usecs)
2720 unsigned long flags;
2721 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
2722 unsigned long xtal_nsecs; /* nanoseconds per xtal oscillator tick */
2723 unsigned long divider;
2725 spin_lock_irqsave(&devc->lock,flags);
2728 * Length of the timer interval (in nanoseconds) depends on the
2729 * selected crystal oscillator. Check this from bit 0x01 of I8.
2731 * AD1845 has just one oscillator which has cycle time of 10.050 us
2732 * (when a 24.576 MHz xtal oscillator is used).
2734 * Convert requested interval to nanoseconds before computing
2735 * the timer divider.
2738 if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE)
2739 xtal_nsecs = 10050;
2740 else if (ad_read(devc, 8) & 0x01)
2741 xtal_nsecs = 9920;
2742 else
2743 xtal_nsecs = 9969;
2745 divider = (usecs * 1000 + xtal_nsecs / 2) / xtal_nsecs;
2747 if (divider < 100) /* Don't allow shorter intervals than about 1ms */
2748 divider = 100;
2750 if (divider > 65535) /* Overflow check */
2751 divider = 65535;
2753 ad_write(devc, 21, (divider >> 8) & 0xff); /* Set upper bits */
2754 ad_write(devc, 20, divider & 0xff); /* Set lower bits */
2755 ad_write(devc, 16, ad_read(devc, 16) | 0x40); /* Start the timer */
2756 devc->timer_running = 1;
2757 spin_unlock_irqrestore(&devc->lock,flags);
2759 return current_interval = (divider * xtal_nsecs + 500) / 1000;
2762 static void ad1848_tmr_reprogram(int dev)
2765 * Audio driver has changed sampling rate so that a different xtal
2766 * oscillator was selected. We have to reprogram the timer rate.
2769 ad1848_tmr_start(dev, current_interval);
2770 sound_timer_syncinterval(current_interval);
2773 static void ad1848_tmr_disable(int dev)
2775 unsigned long flags;
2776 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
2778 spin_lock_irqsave(&devc->lock,flags);
2779 ad_write(devc, 16, ad_read(devc, 16) & ~0x40);
2780 devc->timer_running = 0;
2781 spin_unlock_irqrestore(&devc->lock,flags);
2784 static void ad1848_tmr_restart(int dev)
2786 unsigned long flags;
2787 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
2789 if (current_interval == 0)
2790 return;
2792 spin_lock_irqsave(&devc->lock,flags);
2793 ad_write(devc, 16, ad_read(devc, 16) | 0x40);
2794 devc->timer_running = 1;
2795 spin_unlock_irqrestore(&devc->lock,flags);
2798 static struct sound_lowlev_timer ad1848_tmr =
2802 ad1848_tmr_start,
2803 ad1848_tmr_disable,
2804 ad1848_tmr_restart
2807 static int ad1848_tmr_install(int dev)
2809 if (timer_installed != -1)
2810 return 0; /* Don't install another timer */
2812 timer_installed = ad1848_tmr.dev = dev;
2813 sound_timer_init(&ad1848_tmr, audio_devs[dev]->name);
2815 return 1;
2817 #endif /* EXCLUDE_TIMERS */
2819 static int ad1848_suspend(ad1848_info *devc)
2821 unsigned long flags;
2823 spin_lock_irqsave(&devc->lock,flags);
2825 ad_mute(devc);
2827 spin_unlock_irqrestore(&devc->lock,flags);
2828 return 0;
2831 static int ad1848_resume(ad1848_info *devc)
2833 unsigned long flags;
2834 int mixer_levels[32], i;
2836 spin_lock_irqsave(&devc->lock,flags);
2838 /* Thinkpad is a bit more of PITA than normal. The BIOS tends to
2839 restore it in a different config to the one we use. Need to
2840 fix this somehow */
2842 /* store old mixer levels */
2843 memcpy(mixer_levels, devc->levels, sizeof (mixer_levels));
2844 ad1848_init_hw(devc);
2846 /* restore mixer levels */
2847 for (i = 0; i < 32; i++)
2848 ad1848_mixer_set(devc, devc->dev_no, mixer_levels[i]);
2850 if (!devc->subtype) {
2851 static signed char interrupt_bits[12] = { -1, -1, -1, -1, -1, 0x00, -1, 0x08, -1, 0x10, 0x18, 0x20 };
2852 static char dma_bits[4] = { 1, 2, 0, 3 };
2854 signed char bits;
2855 char dma2_bit = 0;
2857 int config_port = devc->base + 0;
2859 bits = interrupt_bits[devc->irq];
2860 if (bits == -1) {
2861 printk(KERN_ERR "MSS: Bad IRQ %d\n", devc->irq);
2862 spin_unlock_irqrestore(&devc->lock,flags);
2863 return -1;
2866 outb((bits | 0x40), config_port);
2868 if (devc->dma2 != -1 && devc->dma2 != devc->dma1)
2869 if ( (devc->dma1 == 0 && devc->dma2 == 1) ||
2870 (devc->dma1 == 1 && devc->dma2 == 0) ||
2871 (devc->dma1 == 3 && devc->dma2 == 0))
2872 dma2_bit = 0x04;
2874 outb((bits | dma_bits[devc->dma1] | dma2_bit), config_port);
2877 spin_unlock_irqrestore(&devc->lock,flags);
2878 return 0;
2881 static int ad1848_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data)
2883 ad1848_info *devc = dev->data;
2884 if (devc) {
2885 DEB(printk("ad1848: pm event received: 0x%x\n", rqst));
2887 switch (rqst) {
2888 case PM_SUSPEND:
2889 ad1848_suspend(devc);
2890 break;
2891 case PM_RESUME:
2892 ad1848_resume(devc);
2893 break;
2896 return 0;
2900 EXPORT_SYMBOL(ad1848_detect);
2901 EXPORT_SYMBOL(ad1848_init);
2902 EXPORT_SYMBOL(ad1848_unload);
2903 EXPORT_SYMBOL(ad1848_control);
2904 EXPORT_SYMBOL(adintr);
2905 EXPORT_SYMBOL(probe_ms_sound);
2906 EXPORT_SYMBOL(attach_ms_sound);
2907 EXPORT_SYMBOL(unload_ms_sound);
2909 static int __initdata io = -1;
2910 static int __initdata irq = -1;
2911 static int __initdata dma = -1;
2912 static int __initdata dma2 = -1;
2913 static int __initdata type = 0;
2915 MODULE_PARM(io, "i"); /* I/O for a raw AD1848 card */
2916 MODULE_PARM(irq, "i"); /* IRQ to use */
2917 MODULE_PARM(dma, "i"); /* First DMA channel */
2918 MODULE_PARM(dma2, "i"); /* Second DMA channel */
2919 MODULE_PARM(type, "i"); /* Card type */
2920 MODULE_PARM(deskpro_xl, "i"); /* Special magic for Deskpro XL boxen */
2921 MODULE_PARM(deskpro_m, "i"); /* Special magic for Deskpro M box */
2922 MODULE_PARM(soundpro, "i"); /* More special magic for SoundPro chips */
2924 #ifdef CONFIG_PNP
2925 MODULE_PARM(isapnp, "i");
2926 MODULE_PARM(isapnpjump, "i");
2927 MODULE_PARM(reverse, "i");
2928 MODULE_PARM_DESC(isapnp, "When set to 0, Plug & Play support will be disabled");
2929 MODULE_PARM_DESC(isapnpjump, "Jumps to a specific slot in the driver's PnP table. Use the source, Luke.");
2930 MODULE_PARM_DESC(reverse, "When set to 1, will reverse ISAPnP search order");
2932 struct pnp_dev *ad1848_dev = NULL;
2934 /* Please add new entries at the end of the table */
2935 static struct {
2936 char *name;
2937 unsigned short card_vendor, card_device,
2938 vendor, function;
2939 short mss_io, irq, dma, dma2; /* index into isapnp table */
2940 int type;
2941 } ad1848_isapnp_list[] __initdata = {
2942 {"CMI 8330 SoundPRO",
2943 ISAPNP_VENDOR('C','M','I'), ISAPNP_DEVICE(0x0001),
2944 ISAPNP_VENDOR('@','@','@'), ISAPNP_FUNCTION(0x0001),
2945 0, 0, 0,-1, 0},
2946 {"CS4232 based card",
2947 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2948 ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0000),
2949 0, 0, 0, 1, 0},
2950 {"CS4232 based card",
2951 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2952 ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0100),
2953 0, 0, 0, 1, 0},
2954 {"OPL3-SA2 WSS mode",
2955 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2956 ISAPNP_VENDOR('Y','M','H'), ISAPNP_FUNCTION(0x0021),
2957 1, 0, 0, 1, 1},
2958 {"Advanced Gravis InterWave Audio",
2959 ISAPNP_VENDOR('G','R','V'), ISAPNP_DEVICE(0x0001),
2960 ISAPNP_VENDOR('G','R','V'), ISAPNP_FUNCTION(0x0000),
2961 0, 0, 0, 1, 0},
2965 static struct isapnp_device_id id_table[] __devinitdata = {
2966 { ISAPNP_VENDOR('C','M','I'), ISAPNP_DEVICE(0x0001),
2967 ISAPNP_VENDOR('@','@','@'), ISAPNP_FUNCTION(0x0001), 0 },
2968 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2969 ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0000), 0 },
2970 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2971 ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0100), 0 },
2972 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2973 ISAPNP_VENDOR('Y','M','H'), ISAPNP_FUNCTION(0x0021), 0 },
2974 { ISAPNP_VENDOR('G','R','V'), ISAPNP_DEVICE(0x0001),
2975 ISAPNP_VENDOR('G','R','V'), ISAPNP_FUNCTION(0x0000), 0 },
2979 MODULE_DEVICE_TABLE(isapnp, id_table);
2981 static struct pnp_dev *activate_dev(char *devname, char *resname, struct pnp_dev *dev)
2983 int err;
2985 /* Device already active? Let's use it */
2986 if(dev->active)
2987 return(dev);
2989 if((err = pnp_activate_dev(dev)) < 0) {
2990 printk(KERN_ERR "ad1848: %s %s config failed (out of resources?)[%d]\n", devname, resname, err);
2992 pnp_disable_dev(dev);
2994 return(NULL);
2996 audio_activated = 1;
2997 return(dev);
3000 static struct pnp_dev *ad1848_init_generic(struct pnp_card *bus, struct address_info *hw_config, int slot)
3003 /* Configure Audio device */
3004 if((ad1848_dev = pnp_find_dev(bus, ad1848_isapnp_list[slot].vendor, ad1848_isapnp_list[slot].function, NULL)))
3006 if((ad1848_dev = activate_dev(ad1848_isapnp_list[slot].name, "ad1848", ad1848_dev)))
3008 get_device(&ad1848_dev->dev);
3009 hw_config->io_base = ad1848_dev->resource[ad1848_isapnp_list[slot].mss_io].start;
3010 hw_config->irq = ad1848_dev->irq_resource[ad1848_isapnp_list[slot].irq].start;
3011 hw_config->dma = ad1848_dev->dma_resource[ad1848_isapnp_list[slot].dma].start;
3012 if(ad1848_isapnp_list[slot].dma2 != -1)
3013 hw_config->dma2 = ad1848_dev->dma_resource[ad1848_isapnp_list[slot].dma2].start;
3014 else
3015 hw_config->dma2 = -1;
3016 hw_config->card_subtype = ad1848_isapnp_list[slot].type;
3017 } else
3018 return(NULL);
3019 } else
3020 return(NULL);
3022 return(ad1848_dev);
3025 static int __init ad1848_isapnp_init(struct address_info *hw_config, struct pnp_card *bus, int slot)
3027 char *busname = bus->name[0] ? bus->name : ad1848_isapnp_list[slot].name;
3029 /* Initialize this baby. */
3031 if(ad1848_init_generic(bus, hw_config, slot)) {
3032 /* We got it. */
3034 printk(KERN_NOTICE "ad1848: ISAPnP reports '%s' at i/o %#x, irq %d, dma %d, %d\n",
3035 busname,
3036 hw_config->io_base, hw_config->irq, hw_config->dma,
3037 hw_config->dma2);
3038 return 1;
3040 return 0;
3043 static int __init ad1848_isapnp_probe(struct address_info *hw_config)
3045 static int first = 1;
3046 int i;
3048 /* Count entries in sb_isapnp_list */
3049 for (i = 0; ad1848_isapnp_list[i].card_vendor != 0; i++);
3050 i--;
3052 /* Check and adjust isapnpjump */
3053 if( isapnpjump < 0 || isapnpjump > i) {
3054 isapnpjump = reverse ? i : 0;
3055 printk(KERN_ERR "ad1848: Valid range for isapnpjump is 0-%d. Adjusted to %d.\n", i, isapnpjump);
3058 if(!first || !reverse)
3059 i = isapnpjump;
3060 first = 0;
3061 while(ad1848_isapnp_list[i].card_vendor != 0) {
3062 static struct pnp_card *bus = NULL;
3064 while ((bus = pnp_find_card(
3065 ad1848_isapnp_list[i].card_vendor,
3066 ad1848_isapnp_list[i].card_device,
3067 bus))) {
3069 if(ad1848_isapnp_init(hw_config, bus, i)) {
3070 isapnpjump = i; /* start next search from here */
3071 return 0;
3074 i += reverse ? -1 : 1;
3077 return -ENODEV;
3079 #endif
3082 static int __init init_ad1848(void)
3084 printk(KERN_INFO "ad1848/cs4248 codec driver Copyright (C) by Hannu Savolainen 1993-1996\n");
3086 #ifdef CONFIG_PNP
3087 if(isapnp && (ad1848_isapnp_probe(&cfg) < 0) ) {
3088 printk(KERN_NOTICE "ad1848: No ISAPnP cards found, trying standard ones...\n");
3089 isapnp = 0;
3091 #endif
3093 if(io != -1) {
3094 if( isapnp == 0 )
3096 if(irq == -1 || dma == -1) {
3097 printk(KERN_WARNING "ad1848: must give I/O , IRQ and DMA.\n");
3098 return -EINVAL;
3101 cfg.irq = irq;
3102 cfg.io_base = io;
3103 cfg.dma = dma;
3104 cfg.dma2 = dma2;
3105 cfg.card_subtype = type;
3108 if(!probe_ms_sound(&cfg))
3109 return -ENODEV;
3110 attach_ms_sound(&cfg, THIS_MODULE);
3111 loaded = 1;
3113 return 0;
3116 static void __exit cleanup_ad1848(void)
3118 if(loaded)
3119 unload_ms_sound(&cfg);
3121 #ifdef CONFIG_PNP
3122 if(ad1848_dev){
3123 if(audio_activated)
3124 pnp_disable_dev(ad1848_dev);
3125 put_device(&ad1848_dev->dev);
3127 #endif
3130 module_init(init_ad1848);
3131 module_exit(cleanup_ad1848);
3133 #ifndef MODULE
3134 static int __init setup_ad1848(char *str)
3136 /* io, irq, dma, dma2, type */
3137 int ints[6];
3139 str = get_options(str, ARRAY_SIZE(ints), ints);
3141 io = ints[1];
3142 irq = ints[2];
3143 dma = ints[3];
3144 dma2 = ints[4];
3145 type = ints[5];
3147 return 1;
3150 __setup("ad1848=", setup_ad1848);
3151 #endif
3152 MODULE_LICENSE("GPL");