1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44#include <linux/init.h>
45#include <linux/interrupt.h>
46#include <linux/module.h>
47#include <linux/stddef.h>
48#include <linux/slab.h>
49#include <linux/isapnp.h>
50#include <linux/pnp.h>
51#include <linux/spinlock.h>
52
53#include "sound_config.h"
54
55#include "ad1848.h"
56#include "ad1848_mixer.h"
57
58typedef struct
59{
60 spinlock_t lock;
61 int base;
62 int irq;
63 int dma1, dma2;
64 int dual_dma;
65 int subtype;
66 unsigned char MCE_bit;
67 unsigned char saved_regs[64];
68 int debug_flag;
69
70 int audio_flags;
71 int record_dev, playback_dev;
72
73 int xfer_count;
74 int audio_mode;
75 int open_mode;
76 int intr_active;
77 char *chip_name, *name;
78 int model;
79#define MD_1848 1
80#define MD_4231 2
81#define MD_4231A 3
82#define MD_1845 4
83#define MD_4232 5
84#define MD_C930 6
85#define MD_IWAVE 7
86#define MD_4235 8
87#define MD_1845_SSCAPE 9
88#define MD_4236 10
89#define MD_42xB 11
90#define MD_4239 12
91
92
93 int recmask;
94 int supported_devices, orig_devices;
95 int supported_rec_devices, orig_rec_devices;
96 int *levels;
97 short mixer_reroute[32];
98 int dev_no;
99 volatile unsigned long timer_ticks;
100 int timer_running;
101 int irq_ok;
102 mixer_ents *mix_devices;
103 int mixer_output_port;
104} ad1848_info;
105
106typedef struct ad1848_port_info
107{
108 int open_mode;
109 int speed;
110 unsigned char speed_bits;
111 int channels;
112 int audio_format;
113 unsigned char format_bits;
114}
115ad1848_port_info;
116
117static struct address_info cfg;
118static int nr_ad1848_devs;
119
120static bool deskpro_xl;
121static bool deskpro_m;
122static bool soundpro;
123
124#ifndef EXCLUDE_TIMERS
125static int timer_installed = -1;
126#endif
127
128static int loaded;
129
130static int ad_format_mask[13 ] =
131{
132 0,
133 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW,
134 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
135 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
136 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW,
137 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
138 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
139 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
140 AFMT_U8 | AFMT_S16_LE ,
141 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW ,
142 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
143 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM,
144 AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_S16_BE | AFMT_IMA_ADPCM
145};
146
147static ad1848_info adev_info[MAX_AUDIO_DEV];
148
149#define io_Index_Addr(d) ((d)->base)
150#define io_Indexed_Data(d) ((d)->base+1)
151#define io_Status(d) ((d)->base+2)
152#define io_Polled_IO(d) ((d)->base+3)
153
154static struct {
155 unsigned char flags;
156#define CAP_F_TIMER 0x01
157} capabilities [10 ] = {
158 {0}
159 ,{0}
160 ,{CAP_F_TIMER}
161 ,{CAP_F_TIMER}
162 ,{CAP_F_TIMER}
163 ,{CAP_F_TIMER}
164 ,{0}
165 ,{CAP_F_TIMER}
166 ,{0}
167 ,{CAP_F_TIMER}
168};
169
170#ifdef CONFIG_PNP
171static int isapnp = 1;
172static int isapnpjump;
173static bool reverse;
174
175static int audio_activated;
176#else
177static int isapnp;
178#endif
179
180
181
182static int ad1848_open(int dev, int mode);
183static void ad1848_close(int dev);
184static void ad1848_output_block(int dev, unsigned long buf, int count, int intrflag);
185static void ad1848_start_input(int dev, unsigned long buf, int count, int intrflag);
186static int ad1848_prepare_for_output(int dev, int bsize, int bcount);
187static int ad1848_prepare_for_input(int dev, int bsize, int bcount);
188static void ad1848_halt(int dev);
189static void ad1848_halt_input(int dev);
190static void ad1848_halt_output(int dev);
191static void ad1848_trigger(int dev, int bits);
192static irqreturn_t adintr(int irq, void *dev_id);
193
194#ifndef EXCLUDE_TIMERS
195static int ad1848_tmr_install(int dev);
196static void ad1848_tmr_reprogram(int dev);
197#endif
198
199static int ad_read(ad1848_info * devc, int reg)
200{
201 int x;
202 int timeout = 900000;
203
204 while (timeout > 0 && inb(devc->base) == 0x80)
205 timeout--;
206
207 if(reg < 32)
208 {
209 outb(((unsigned char) (reg & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
210 x = inb(io_Indexed_Data(devc));
211 }
212 else
213 {
214 int xreg, xra;
215
216 xreg = (reg & 0xff) - 32;
217 xra = (((xreg & 0x0f) << 4) & 0xf0) | 0x08 | ((xreg & 0x10) >> 2);
218 outb(((unsigned char) (23 & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
219 outb(((unsigned char) (xra & 0xff)), io_Indexed_Data(devc));
220 x = inb(io_Indexed_Data(devc));
221 }
222
223 return x;
224}
225
226static void ad_write(ad1848_info * devc, int reg, int data)
227{
228 int timeout = 900000;
229
230 while (timeout > 0 && inb(devc->base) == 0x80)
231 timeout--;
232
233 if(reg < 32)
234 {
235 outb(((unsigned char) (reg & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
236 outb(((unsigned char) (data & 0xff)), io_Indexed_Data(devc));
237 }
238 else
239 {
240 int xreg, xra;
241
242 xreg = (reg & 0xff) - 32;
243 xra = (((xreg & 0x0f) << 4) & 0xf0) | 0x08 | ((xreg & 0x10) >> 2);
244 outb(((unsigned char) (23 & 0xff) | devc->MCE_bit), io_Index_Addr(devc));
245 outb(((unsigned char) (xra & 0xff)), io_Indexed_Data(devc));
246 outb((unsigned char) (data & 0xff), io_Indexed_Data(devc));
247 }
248}
249
250static void wait_for_calibration(ad1848_info * devc)
251{
252 int timeout;
253
254
255
256
257
258
259
260
261 timeout = 100000;
262 while (timeout > 0 && inb(devc->base) == 0x80)
263 timeout--;
264 if (inb(devc->base) & 0x80)
265 printk(KERN_WARNING "ad1848: Auto calibration timed out(1).\n");
266
267 timeout = 100;
268 while (timeout > 0 && !(ad_read(devc, 11) & 0x20))
269 timeout--;
270 if (!(ad_read(devc, 11) & 0x20))
271 return;
272
273 timeout = 80000;
274 while (timeout > 0 && (ad_read(devc, 11) & 0x20))
275 timeout--;
276 if (ad_read(devc, 11) & 0x20)
277 if ((devc->model != MD_1845) && (devc->model != MD_1845_SSCAPE))
278 printk(KERN_WARNING "ad1848: Auto calibration timed out(3).\n");
279}
280
281static void ad_mute(ad1848_info * devc)
282{
283 int i;
284 unsigned char prev;
285
286
287
288
289
290 for (i = 6; i < 8; i++)
291 {
292 prev = devc->saved_regs[i] = ad_read(devc, i);
293 }
294
295}
296
297static void ad_unmute(ad1848_info * devc)
298{
299}
300
301static void ad_enter_MCE(ad1848_info * devc)
302{
303 int timeout = 1000;
304 unsigned short prev;
305
306 while (timeout > 0 && inb(devc->base) == 0x80)
307 timeout--;
308
309 devc->MCE_bit = 0x40;
310 prev = inb(io_Index_Addr(devc));
311 if (prev & 0x40)
312 {
313 return;
314 }
315 outb((devc->MCE_bit), io_Index_Addr(devc));
316}
317
318static void ad_leave_MCE(ad1848_info * devc)
319{
320 unsigned char prev, acal;
321 int timeout = 1000;
322
323 while (timeout > 0 && inb(devc->base) == 0x80)
324 timeout--;
325
326 acal = ad_read(devc, 9);
327
328 devc->MCE_bit = 0x00;
329 prev = inb(io_Index_Addr(devc));
330 outb((0x00), io_Index_Addr(devc));
331
332 if ((prev & 0x40) == 0)
333 {
334 return;
335 }
336 outb((0x00), io_Index_Addr(devc));
337 if (acal & 0x08)
338 wait_for_calibration(devc);
339}
340
341static int ad1848_set_recmask(ad1848_info * devc, int mask)
342{
343 unsigned char recdev;
344 int i, n;
345 unsigned long flags;
346
347 mask &= devc->supported_rec_devices;
348
349
350 for (i = 0; i < 32; i++)
351 {
352 if (devc->mixer_reroute[i] != i)
353 {
354 if (mask & (1 << i))
355 {
356 mask &= ~(1 << i);
357 mask |= (1 << devc->mixer_reroute[i]);
358 }
359 }
360 }
361
362 n = 0;
363 for (i = 0; i < 32; i++)
364 if (mask & (1 << i))
365 n++;
366
367 spin_lock_irqsave(&devc->lock,flags);
368 if (!soundpro) {
369 if (n == 0)
370 mask = SOUND_MASK_MIC;
371 else if (n != 1) {
372 mask &= ~devc->recmask;
373
374 n = 0;
375 for (i = 0; i < 32; i++)
376 if (mask & (1 << i))
377 n++;
378
379 if (n != 1)
380 mask = SOUND_MASK_MIC;
381 }
382 switch (mask) {
383 case SOUND_MASK_MIC:
384 recdev = 2;
385 break;
386
387 case SOUND_MASK_LINE:
388 case SOUND_MASK_LINE3:
389 recdev = 0;
390 break;
391
392 case SOUND_MASK_CD:
393 case SOUND_MASK_LINE1:
394 recdev = 1;
395 break;
396
397 case SOUND_MASK_IMIX:
398 recdev = 3;
399 break;
400
401 default:
402 mask = SOUND_MASK_MIC;
403 recdev = 2;
404 }
405
406 recdev <<= 6;
407 ad_write(devc, 0, (ad_read(devc, 0) & 0x3f) | recdev);
408 ad_write(devc, 1, (ad_read(devc, 1) & 0x3f) | recdev);
409 } else {
410 unsigned char val;
411 int set_rec_bit;
412 int j;
413
414 for (i = 0; i < 32; i++) {
415 if ((devc->supported_rec_devices & (1 << i)) == 0)
416 continue;
417
418 for (j = LEFT_CHN; j <= RIGHT_CHN; j++) {
419 if (devc->mix_devices[i][j].nbits == 0)
420 continue;
421
422
423
424
425
426
427 set_rec_bit = ((mask & (1 << i)) != 0) ^ devc->mix_devices[i][j].recpol;
428
429 val = ad_read(devc, devc->mix_devices[i][j].recreg);
430 val &= ~(1 << devc->mix_devices[i][j].recpos);
431 val |= (set_rec_bit << devc->mix_devices[i][j].recpos);
432 ad_write(devc, devc->mix_devices[i][j].recreg, val);
433 }
434 }
435 }
436 spin_unlock_irqrestore(&devc->lock,flags);
437
438
439 for (i = 0; i < 32; i++)
440 {
441 if (devc->mixer_reroute[i] != i)
442 {
443 if (mask & (1 << devc->mixer_reroute[i]))
444 {
445 mask &= ~(1 << devc->mixer_reroute[i]);
446 mask |= (1 << i);
447 }
448 }
449 }
450 devc->recmask = mask;
451 return mask;
452}
453
454static void oss_change_bits(ad1848_info *devc, unsigned char *regval,
455 unsigned char *muteval, int dev, int chn, int newval)
456{
457 unsigned char mask;
458 int shift;
459 int mute;
460 int mutemask;
461 int set_mute_bit;
462
463 set_mute_bit = (newval == 0) ^ devc->mix_devices[dev][chn].mutepol;
464
465 if (devc->mix_devices[dev][chn].polarity == 1)
466 newval = 100 - newval;
467
468 mask = (1 << devc->mix_devices[dev][chn].nbits) - 1;
469 shift = devc->mix_devices[dev][chn].bitpos;
470
471 if (devc->mix_devices[dev][chn].mutepos == 8)
472 {
473 mute = 0;
474 mutemask = ~0;
475 }
476 else
477 {
478 mute = (set_mute_bit << devc->mix_devices[dev][chn].mutepos);
479 mutemask = ~(1 << devc->mix_devices[dev][chn].mutepos);
480 }
481
482 newval = (int) ((newval * mask) + 50) / 100;
483 *regval &= ~(mask << shift);
484 *regval |= (newval & mask) << shift;
485
486 *muteval &= mutemask;
487 *muteval |= mute;
488}
489
490static int ad1848_mixer_get(ad1848_info * devc, int dev)
491{
492 if (!((1 << dev) & devc->supported_devices))
493 return -EINVAL;
494
495 dev = devc->mixer_reroute[dev];
496
497 return devc->levels[dev];
498}
499
500static void ad1848_mixer_set_channel(ad1848_info *devc, int dev, int value, int channel)
501{
502 int regoffs, muteregoffs;
503 unsigned char val, muteval;
504 unsigned long flags;
505
506 regoffs = devc->mix_devices[dev][channel].regno;
507 muteregoffs = devc->mix_devices[dev][channel].mutereg;
508 val = ad_read(devc, regoffs);
509
510 if (muteregoffs != regoffs) {
511 muteval = ad_read(devc, muteregoffs);
512 oss_change_bits(devc, &val, &muteval, dev, channel, value);
513 }
514 else
515 oss_change_bits(devc, &val, &val, dev, channel, value);
516
517 spin_lock_irqsave(&devc->lock,flags);
518 ad_write(devc, regoffs, val);
519 devc->saved_regs[regoffs] = val;
520 if (muteregoffs != regoffs) {
521 ad_write(devc, muteregoffs, muteval);
522 devc->saved_regs[muteregoffs] = muteval;
523 }
524 spin_unlock_irqrestore(&devc->lock,flags);
525}
526
527static int ad1848_mixer_set(ad1848_info * devc, int dev, int value)
528{
529 int left = value & 0x000000ff;
530 int right = (value & 0x0000ff00) >> 8;
531 int retvol;
532
533 if (dev > 31)
534 return -EINVAL;
535
536 if (!(devc->supported_devices & (1 << dev)))
537 return -EINVAL;
538
539 dev = devc->mixer_reroute[dev];
540
541 if (devc->mix_devices[dev][LEFT_CHN].nbits == 0)
542 return -EINVAL;
543
544 if (left > 100)
545 left = 100;
546 if (right > 100)
547 right = 100;
548
549 if (devc->mix_devices[dev][RIGHT_CHN].nbits == 0)
550 right = left;
551
552 retvol = left | (right << 8);
553
554
555 left = mix_cvt[left];
556 right = mix_cvt[right];
557
558 devc->levels[dev] = retvol;
559
560
561
562
563 ad1848_mixer_set_channel(devc, dev, left, LEFT_CHN);
564
565
566
567
568 if (devc->mix_devices[dev][RIGHT_CHN].nbits == 0)
569 goto out;
570 ad1848_mixer_set_channel(devc, dev, right, RIGHT_CHN);
571
572 out:
573 return retvol;
574}
575
576static void ad1848_mixer_reset(ad1848_info * devc)
577{
578 int i;
579 char name[32];
580 unsigned long flags;
581
582 devc->mix_devices = &(ad1848_mix_devices[0]);
583
584 sprintf(name, "%s_%d", devc->chip_name, nr_ad1848_devs);
585
586 for (i = 0; i < 32; i++)
587 devc->mixer_reroute[i] = i;
588
589 devc->supported_rec_devices = MODE1_REC_DEVICES;
590
591 switch (devc->model)
592 {
593 case MD_4231:
594 case MD_4231A:
595 case MD_1845:
596 case MD_1845_SSCAPE:
597 devc->supported_devices = MODE2_MIXER_DEVICES;
598 break;
599
600 case MD_C930:
601 devc->supported_devices = C930_MIXER_DEVICES;
602 devc->mix_devices = &(c930_mix_devices[0]);
603 break;
604
605 case MD_IWAVE:
606 devc->supported_devices = MODE3_MIXER_DEVICES;
607 devc->mix_devices = &(iwave_mix_devices[0]);
608 break;
609
610 case MD_42xB:
611 case MD_4239:
612 devc->mix_devices = &(cs42xb_mix_devices[0]);
613 devc->supported_devices = MODE3_MIXER_DEVICES;
614 break;
615 case MD_4232:
616 case MD_4235:
617 case MD_4236:
618 devc->supported_devices = MODE3_MIXER_DEVICES;
619 break;
620
621 case MD_1848:
622 if (soundpro) {
623 devc->supported_devices = SPRO_MIXER_DEVICES;
624 devc->supported_rec_devices = SPRO_REC_DEVICES;
625 devc->mix_devices = &(spro_mix_devices[0]);
626 break;
627 }
628
629 default:
630 devc->supported_devices = MODE1_MIXER_DEVICES;
631 }
632
633 devc->orig_devices = devc->supported_devices;
634 devc->orig_rec_devices = devc->supported_rec_devices;
635
636 devc->levels = load_mixer_volumes(name, default_mixer_levels, 1);
637
638 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
639 {
640 if (devc->supported_devices & (1 << i))
641 ad1848_mixer_set(devc, i, devc->levels[i]);
642 }
643
644 ad1848_set_recmask(devc, SOUND_MASK_MIC);
645
646 devc->mixer_output_port = devc->levels[31] | AUDIO_HEADPHONE | AUDIO_LINE_OUT;
647
648 spin_lock_irqsave(&devc->lock,flags);
649 if (!soundpro) {
650 if (devc->mixer_output_port & AUDIO_SPEAKER)
651 ad_write(devc, 26, ad_read(devc, 26) & ~0x40);
652 else
653 ad_write(devc, 26, ad_read(devc, 26) | 0x40);
654 } else {
655
656
657
658
659
660 ad_write(devc, 16, 0x60);
661 }
662 spin_unlock_irqrestore(&devc->lock,flags);
663}
664
665static int ad1848_mixer_ioctl(int dev, unsigned int cmd, void __user *arg)
666{
667 ad1848_info *devc = mixer_devs[dev]->devc;
668 int val;
669
670 if (cmd == SOUND_MIXER_PRIVATE1)
671 {
672 if (get_user(val, (int __user *)arg))
673 return -EFAULT;
674
675 if (val != 0xffff)
676 {
677 unsigned long flags;
678 val &= (AUDIO_SPEAKER | AUDIO_HEADPHONE | AUDIO_LINE_OUT);
679 devc->mixer_output_port = val;
680 val |= AUDIO_HEADPHONE | AUDIO_LINE_OUT;
681 devc->mixer_output_port = val;
682 spin_lock_irqsave(&devc->lock,flags);
683 if (val & AUDIO_SPEAKER)
684 ad_write(devc, 26, ad_read(devc, 26) & ~0x40);
685 else
686 ad_write(devc, 26, ad_read(devc, 26) | 0x40);
687 spin_unlock_irqrestore(&devc->lock,flags);
688 }
689 val = devc->mixer_output_port;
690 return put_user(val, (int __user *)arg);
691 }
692 if (cmd == SOUND_MIXER_PRIVATE2)
693 {
694 if (get_user(val, (int __user *)arg))
695 return -EFAULT;
696 return(ad1848_control(AD1848_MIXER_REROUTE, val));
697 }
698 if (((cmd >> 8) & 0xff) == 'M')
699 {
700 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
701 {
702 switch (cmd & 0xff)
703 {
704 case SOUND_MIXER_RECSRC:
705 if (get_user(val, (int __user *)arg))
706 return -EFAULT;
707 val = ad1848_set_recmask(devc, val);
708 break;
709
710 default:
711 if (get_user(val, (int __user *)arg))
712 return -EFAULT;
713 val = ad1848_mixer_set(devc, cmd & 0xff, val);
714 break;
715 }
716 return put_user(val, (int __user *)arg);
717 }
718 else
719 {
720 switch (cmd & 0xff)
721 {
722
723
724
725
726 case SOUND_MIXER_RECSRC:
727 val = devc->recmask;
728 break;
729
730 case SOUND_MIXER_DEVMASK:
731 val = devc->supported_devices;
732 break;
733
734 case SOUND_MIXER_STEREODEVS:
735 val = devc->supported_devices;
736 if (devc->model != MD_C930)
737 val &= ~(SOUND_MASK_SPEAKER | SOUND_MASK_IMIX);
738 break;
739
740 case SOUND_MIXER_RECMASK:
741 val = devc->supported_rec_devices;
742 break;
743
744 case SOUND_MIXER_CAPS:
745 val=SOUND_CAP_EXCL_INPUT;
746 break;
747
748 default:
749 val = ad1848_mixer_get(devc, cmd & 0xff);
750 break;
751 }
752 return put_user(val, (int __user *)arg);
753 }
754 }
755 else
756 return -EINVAL;
757}
758
759static int ad1848_set_speed(int dev, int arg)
760{
761 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
762 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
763
764
765
766
767
768
769
770
771
772 typedef struct
773 {
774 int speed;
775 unsigned char bits;
776 }
777 speed_struct;
778
779 static speed_struct speed_table[] =
780 {
781 {5510, (0 << 1) | 1},
782 {5510, (0 << 1) | 1},
783 {6620, (7 << 1) | 1},
784 {8000, (0 << 1) | 0},
785 {9600, (7 << 1) | 0},
786 {11025, (1 << 1) | 1},
787 {16000, (1 << 1) | 0},
788 {18900, (2 << 1) | 1},
789 {22050, (3 << 1) | 1},
790 {27420, (2 << 1) | 0},
791 {32000, (3 << 1) | 0},
792 {33075, (6 << 1) | 1},
793 {37800, (4 << 1) | 1},
794 {44100, (5 << 1) | 1},
795 {48000, (6 << 1) | 0}
796 };
797
798 int i, n, selected = -1;
799
800 n = sizeof(speed_table) / sizeof(speed_struct);
801
802 if (arg <= 0)
803 return portc->speed;
804
805 if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE)
806 {
807 if (arg < 4000)
808 arg = 4000;
809 if (arg > 50000)
810 arg = 50000;
811
812 portc->speed = arg;
813 portc->speed_bits = speed_table[3].bits;
814 return portc->speed;
815 }
816 if (arg < speed_table[0].speed)
817 selected = 0;
818 if (arg > speed_table[n - 1].speed)
819 selected = n - 1;
820
821 for (i = 1 ; selected == -1 && i < n; i++)
822 {
823 if (speed_table[i].speed == arg)
824 selected = i;
825 else if (speed_table[i].speed > arg)
826 {
827 int diff1, diff2;
828
829 diff1 = arg - speed_table[i - 1].speed;
830 diff2 = speed_table[i].speed - arg;
831
832 if (diff1 < diff2)
833 selected = i - 1;
834 else
835 selected = i;
836 }
837 }
838 if (selected == -1)
839 {
840 printk(KERN_WARNING "ad1848: Can't find speed???\n");
841 selected = 3;
842 }
843 portc->speed = speed_table[selected].speed;
844 portc->speed_bits = speed_table[selected].bits;
845 return portc->speed;
846}
847
848static short ad1848_set_channels(int dev, short arg)
849{
850 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
851
852 if (arg != 1 && arg != 2)
853 return portc->channels;
854
855 portc->channels = arg;
856 return arg;
857}
858
859static unsigned int ad1848_set_bits(int dev, unsigned int arg)
860{
861 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
862 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
863
864 static struct format_tbl
865 {
866 int format;
867 unsigned char bits;
868 }
869 format2bits[] =
870 {
871 {
872 0, 0
873 }
874 ,
875 {
876 AFMT_MU_LAW, 1
877 }
878 ,
879 {
880 AFMT_A_LAW, 3
881 }
882 ,
883 {
884 AFMT_IMA_ADPCM, 5
885 }
886 ,
887 {
888 AFMT_U8, 0
889 }
890 ,
891 {
892 AFMT_S16_LE, 2
893 }
894 ,
895 {
896 AFMT_S16_BE, 6
897 }
898 ,
899 {
900 AFMT_S8, 0
901 }
902 ,
903 {
904 AFMT_U16_LE, 0
905 }
906 ,
907 {
908 AFMT_U16_BE, 0
909 }
910 };
911 int i, n = sizeof(format2bits) / sizeof(struct format_tbl);
912
913 if (arg == 0)
914 return portc->audio_format;
915
916 if (!(arg & ad_format_mask[devc->model]))
917 arg = AFMT_U8;
918
919 portc->audio_format = arg;
920
921 for (i = 0; i < n; i++)
922 if (format2bits[i].format == arg)
923 {
924 if ((portc->format_bits = format2bits[i].bits) == 0)
925 return portc->audio_format = AFMT_U8;
926
927 return arg;
928 }
929
930 portc->format_bits = 0;
931 return portc->audio_format = AFMT_U8;
932}
933
934static struct audio_driver ad1848_audio_driver =
935{
936 .owner = THIS_MODULE,
937 .open = ad1848_open,
938 .close = ad1848_close,
939 .output_block = ad1848_output_block,
940 .start_input = ad1848_start_input,
941 .prepare_for_input = ad1848_prepare_for_input,
942 .prepare_for_output = ad1848_prepare_for_output,
943 .halt_io = ad1848_halt,
944 .halt_input = ad1848_halt_input,
945 .halt_output = ad1848_halt_output,
946 .trigger = ad1848_trigger,
947 .set_speed = ad1848_set_speed,
948 .set_bits = ad1848_set_bits,
949 .set_channels = ad1848_set_channels
950};
951
952static struct mixer_operations ad1848_mixer_operations =
953{
954 .owner = THIS_MODULE,
955 .id = "SOUNDPORT",
956 .name = "AD1848/CS4248/CS4231",
957 .ioctl = ad1848_mixer_ioctl
958};
959
960static int ad1848_open(int dev, int mode)
961{
962 ad1848_info *devc;
963 ad1848_port_info *portc;
964 unsigned long flags;
965
966 if (dev < 0 || dev >= num_audiodevs)
967 return -ENXIO;
968
969 devc = (ad1848_info *) audio_devs[dev]->devc;
970 portc = (ad1848_port_info *) audio_devs[dev]->portc;
971
972
973 spin_lock(&devc->lock);
974 if (portc->open_mode || (devc->open_mode & mode))
975 {
976 spin_unlock(&devc->lock);
977 return -EBUSY;
978 }
979 devc->dual_dma = 0;
980
981 if (audio_devs[dev]->flags & DMA_DUPLEX)
982 {
983 devc->dual_dma = 1;
984 }
985 devc->intr_active = 0;
986 devc->audio_mode = 0;
987 devc->open_mode |= mode;
988 portc->open_mode = mode;
989 spin_unlock(&devc->lock);
990 ad1848_trigger(dev, 0);
991
992 if (mode & OPEN_READ)
993 devc->record_dev = dev;
994 if (mode & OPEN_WRITE)
995 devc->playback_dev = dev;
996
997
998
999 spin_lock_irqsave(&devc->lock,flags);
1000 ad_mute(devc);
1001 spin_unlock_irqrestore(&devc->lock,flags);
1002
1003 return 0;
1004}
1005
1006static void ad1848_close(int dev)
1007{
1008 unsigned long flags;
1009 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1010 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1011
1012 devc->intr_active = 0;
1013 ad1848_halt(dev);
1014
1015 spin_lock_irqsave(&devc->lock,flags);
1016
1017 devc->audio_mode = 0;
1018 devc->open_mode &= ~portc->open_mode;
1019 portc->open_mode = 0;
1020
1021 ad_unmute(devc);
1022 spin_unlock_irqrestore(&devc->lock,flags);
1023}
1024
1025static void ad1848_output_block(int dev, unsigned long buf, int count, int intrflag)
1026{
1027 unsigned long flags, cnt;
1028 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1029 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1030
1031 cnt = count;
1032
1033 if (portc->audio_format == AFMT_IMA_ADPCM)
1034 {
1035 cnt /= 4;
1036 }
1037 else
1038 {
1039 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))
1040 cnt >>= 1;
1041 }
1042 if (portc->channels > 1)
1043 cnt >>= 1;
1044 cnt--;
1045
1046 if ((devc->audio_mode & PCM_ENABLE_OUTPUT) && (audio_devs[dev]->flags & DMA_AUTOMODE) &&
1047 intrflag &&
1048 cnt == devc->xfer_count)
1049 {
1050 devc->audio_mode |= PCM_ENABLE_OUTPUT;
1051 devc->intr_active = 1;
1052 return;
1053
1054
1055 }
1056 spin_lock_irqsave(&devc->lock,flags);
1057
1058 ad_write(devc, 15, (unsigned char) (cnt & 0xff));
1059 ad_write(devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
1060
1061 devc->xfer_count = cnt;
1062 devc->audio_mode |= PCM_ENABLE_OUTPUT;
1063 devc->intr_active = 1;
1064 spin_unlock_irqrestore(&devc->lock,flags);
1065}
1066
1067static void ad1848_start_input(int dev, unsigned long buf, int count, int intrflag)
1068{
1069 unsigned long flags, cnt;
1070 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1071 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1072
1073 cnt = count;
1074 if (portc->audio_format == AFMT_IMA_ADPCM)
1075 {
1076 cnt /= 4;
1077 }
1078 else
1079 {
1080 if (portc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))
1081 cnt >>= 1;
1082 }
1083 if (portc->channels > 1)
1084 cnt >>= 1;
1085 cnt--;
1086
1087 if ((devc->audio_mode & PCM_ENABLE_INPUT) && (audio_devs[dev]->flags & DMA_AUTOMODE) &&
1088 intrflag &&
1089 cnt == devc->xfer_count)
1090 {
1091 devc->audio_mode |= PCM_ENABLE_INPUT;
1092 devc->intr_active = 1;
1093 return;
1094
1095
1096 }
1097 spin_lock_irqsave(&devc->lock,flags);
1098
1099 if (devc->model == MD_1848)
1100 {
1101 ad_write(devc, 15, (unsigned char) (cnt & 0xff));
1102 ad_write(devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
1103 }
1104 else
1105 {
1106 ad_write(devc, 31, (unsigned char) (cnt & 0xff));
1107 ad_write(devc, 30, (unsigned char) ((cnt >> 8) & 0xff));
1108 }
1109
1110 ad_unmute(devc);
1111
1112 devc->xfer_count = cnt;
1113 devc->audio_mode |= PCM_ENABLE_INPUT;
1114 devc->intr_active = 1;
1115 spin_unlock_irqrestore(&devc->lock,flags);
1116}
1117
1118static int ad1848_prepare_for_output(int dev, int bsize, int bcount)
1119{
1120 int timeout;
1121 unsigned char fs, old_fs, tmp = 0;
1122 unsigned long flags;
1123 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1124 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1125
1126 ad_mute(devc);
1127
1128 spin_lock_irqsave(&devc->lock,flags);
1129 fs = portc->speed_bits | (portc->format_bits << 5);
1130
1131 if (portc->channels > 1)
1132 fs |= 0x10;
1133
1134 ad_enter_MCE(devc);
1135
1136 if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE)
1137 {
1138 fs &= 0xf0;
1139
1140 ad_write(devc, 22, (portc->speed >> 8) & 0xff);
1141 ad_write(devc, 23, portc->speed & 0xff);
1142 }
1143 old_fs = ad_read(devc, 8);
1144
1145 if (devc->model == MD_4232 || devc->model >= MD_4236)
1146 {
1147 tmp = ad_read(devc, 16);
1148 ad_write(devc, 16, tmp | 0x30);
1149 }
1150 if (devc->model == MD_IWAVE)
1151 ad_write(devc, 17, 0xc2);
1152
1153 ad_write(devc, 8, fs);
1154
1155
1156
1157
1158
1159 timeout = 0;
1160 while (timeout < 100 && inb(devc->base) != 0x80)
1161 timeout++;
1162 timeout = 0;
1163 while (timeout < 10000 && inb(devc->base) == 0x80)
1164 timeout++;
1165
1166 if (devc->model >= MD_4232)
1167 ad_write(devc, 16, tmp & ~0x30);
1168
1169 ad_leave_MCE(devc);
1170
1171
1172 spin_unlock_irqrestore(&devc->lock,flags);
1173 devc->xfer_count = 0;
1174
1175#ifndef EXCLUDE_TIMERS
1176 if (dev == timer_installed && devc->timer_running)
1177 if ((fs & 0x01) != (old_fs & 0x01))
1178 {
1179 ad1848_tmr_reprogram(dev);
1180 }
1181#endif
1182 ad1848_halt_output(dev);
1183 return 0;
1184}
1185
1186static int ad1848_prepare_for_input(int dev, int bsize, int bcount)
1187{
1188 int timeout;
1189 unsigned char fs, old_fs, tmp = 0;
1190 unsigned long flags;
1191 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1192 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1193
1194 if (devc->audio_mode)
1195 return 0;
1196
1197 spin_lock_irqsave(&devc->lock,flags);
1198 fs = portc->speed_bits | (portc->format_bits << 5);
1199
1200 if (portc->channels > 1)
1201 fs |= 0x10;
1202
1203 ad_enter_MCE(devc);
1204
1205 if ((devc->model == MD_1845) || (devc->model == MD_1845_SSCAPE))
1206 {
1207 fs &= 0xf0;
1208
1209 ad_write(devc, 22, (portc->speed >> 8) & 0xff);
1210 ad_write(devc, 23, portc->speed & 0xff);
1211 }
1212 if (devc->model == MD_4232)
1213 {
1214 tmp = ad_read(devc, 16);
1215 ad_write(devc, 16, tmp | 0x30);
1216 }
1217 if (devc->model == MD_IWAVE)
1218 ad_write(devc, 17, 0xc2);
1219
1220
1221
1222
1223
1224 if (devc->model != MD_1848)
1225 {
1226 old_fs = ad_read(devc, 28);
1227 ad_write(devc, 28, fs);
1228
1229
1230
1231
1232
1233 timeout = 0;
1234 while (timeout < 100 && inb(devc->base) != 0x80)
1235 timeout++;
1236
1237 timeout = 0;
1238 while (timeout < 10000 && inb(devc->base) == 0x80)
1239 timeout++;
1240
1241 if (devc->model != MD_1848 && devc->model != MD_1845 && devc->model != MD_1845_SSCAPE)
1242 {
1243
1244
1245
1246
1247
1248 unsigned char tmp = portc->speed_bits | (ad_read(devc, 8) & 0xf0);
1249
1250 ad_write(devc, 8, tmp);
1251
1252
1253
1254 timeout = 0;
1255 while (timeout < 100 && inb(devc->base) != 0x80)
1256 timeout++;
1257
1258 timeout = 0;
1259 while (timeout < 10000 && inb(devc->base) == 0x80)
1260 timeout++;
1261 }
1262 }
1263 else
1264 {
1265
1266 old_fs = ad_read(devc, 8);
1267 ad_write(devc, 8, fs);
1268
1269
1270
1271 timeout = 0;
1272 while (timeout < 100 && inb(devc->base) != 0x80)
1273 timeout++;
1274 timeout = 0;
1275 while (timeout < 10000 && inb(devc->base) == 0x80)
1276 timeout++;
1277 }
1278
1279 if (devc->model == MD_4232)
1280 ad_write(devc, 16, tmp & ~0x30);
1281
1282 ad_leave_MCE(devc);
1283
1284
1285 spin_unlock_irqrestore(&devc->lock,flags);
1286 devc->xfer_count = 0;
1287
1288#ifndef EXCLUDE_TIMERS
1289 if (dev == timer_installed && devc->timer_running)
1290 {
1291 if ((fs & 0x01) != (old_fs & 0x01))
1292 {
1293 ad1848_tmr_reprogram(dev);
1294 }
1295 }
1296#endif
1297 ad1848_halt_input(dev);
1298 return 0;
1299}
1300
1301static void ad1848_halt(int dev)
1302{
1303 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1304 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1305
1306 unsigned char bits = ad_read(devc, 9);
1307
1308 if (bits & 0x01 && (portc->open_mode & OPEN_WRITE))
1309 ad1848_halt_output(dev);
1310
1311 if (bits & 0x02 && (portc->open_mode & OPEN_READ))
1312 ad1848_halt_input(dev);
1313 devc->audio_mode = 0;
1314}
1315
1316static void ad1848_halt_input(int dev)
1317{
1318 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1319 unsigned long flags;
1320
1321 if (!(ad_read(devc, 9) & 0x02))
1322 return;
1323
1324 spin_lock_irqsave(&devc->lock,flags);
1325
1326 ad_mute(devc);
1327
1328 {
1329 int tmout;
1330
1331 if(!isa_dma_bridge_buggy)
1332 disable_dma(audio_devs[dev]->dmap_in->dma);
1333
1334 for (tmout = 0; tmout < 100000; tmout++)
1335 if (ad_read(devc, 11) & 0x10)
1336 break;
1337 ad_write(devc, 9, ad_read(devc, 9) & ~0x02);
1338
1339 if(!isa_dma_bridge_buggy)
1340 enable_dma(audio_devs[dev]->dmap_in->dma);
1341 devc->audio_mode &= ~PCM_ENABLE_INPUT;
1342 }
1343
1344 outb(0, io_Status(devc));
1345 outb(0, io_Status(devc));
1346
1347 devc->audio_mode &= ~PCM_ENABLE_INPUT;
1348
1349 spin_unlock_irqrestore(&devc->lock,flags);
1350}
1351
1352static void ad1848_halt_output(int dev)
1353{
1354 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1355 unsigned long flags;
1356
1357 if (!(ad_read(devc, 9) & 0x01))
1358 return;
1359
1360 spin_lock_irqsave(&devc->lock,flags);
1361
1362 ad_mute(devc);
1363 {
1364 int tmout;
1365
1366 if(!isa_dma_bridge_buggy)
1367 disable_dma(audio_devs[dev]->dmap_out->dma);
1368
1369 for (tmout = 0; tmout < 100000; tmout++)
1370 if (ad_read(devc, 11) & 0x10)
1371 break;
1372 ad_write(devc, 9, ad_read(devc, 9) & ~0x01);
1373
1374 if(!isa_dma_bridge_buggy)
1375 enable_dma(audio_devs[dev]->dmap_out->dma);
1376
1377 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
1378 }
1379
1380 outb((0), io_Status(devc));
1381 outb((0), io_Status(devc));
1382
1383 devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
1384
1385 spin_unlock_irqrestore(&devc->lock,flags);
1386}
1387
1388static void ad1848_trigger(int dev, int state)
1389{
1390 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
1391 ad1848_port_info *portc = (ad1848_port_info *) audio_devs[dev]->portc;
1392 unsigned long flags;
1393 unsigned char tmp, old;
1394
1395 spin_lock_irqsave(&devc->lock,flags);
1396 state &= devc->audio_mode;
1397
1398 tmp = old = ad_read(devc, 9);
1399
1400 if (portc->open_mode & OPEN_READ)
1401 {
1402 if (state & PCM_ENABLE_INPUT)
1403 tmp |= 0x02;
1404 else
1405 tmp &= ~0x02;
1406 }
1407 if (portc->open_mode & OPEN_WRITE)
1408 {
1409 if (state & PCM_ENABLE_OUTPUT)
1410 tmp |= 0x01;
1411 else
1412 tmp &= ~0x01;
1413 }
1414
1415 if (tmp != old)
1416 {
1417 ad_write(devc, 9, tmp);
1418 ad_unmute(devc);
1419 }
1420 spin_unlock_irqrestore(&devc->lock,flags);
1421}
1422
1423static void ad1848_init_hw(ad1848_info * devc)
1424{
1425 int i;
1426 int *init_values;
1427
1428
1429
1430
1431 static int init_values_a[] =
1432 {
1433 0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
1434 0x00, 0x0c, 0x02, 0x00, 0x8a, 0x01, 0x00, 0x00,
1435
1436
1437 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x1f, 0x40,
1438 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1439 };
1440
1441 static int init_values_b[] =
1442 {
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453 0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
1454 0x00, 0x00, 0x06, 0x00, 0xe0, 0x01, 0x00, 0x00,
1455 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x1f, 0x40,
1456 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1457 };
1458
1459
1460
1461
1462
1463 init_values = init_values_a;
1464 if(devc->model >= MD_4236)
1465 init_values = init_values_b;
1466
1467 for (i = 0; i < 16; i++)
1468 ad_write(devc, i, init_values[i]);
1469
1470
1471 ad_mute(devc);
1472 ad_unmute(devc);
1473
1474 if (devc->model > MD_1848)
1475 {
1476 if (devc->model == MD_1845_SSCAPE)
1477 ad_write(devc, 12, ad_read(devc, 12) | 0x50);
1478 else
1479 ad_write(devc, 12, ad_read(devc, 12) | 0x40);
1480
1481 if (devc->model == MD_IWAVE)
1482 ad_write(devc, 12, 0x6c);
1483
1484 if (devc->model != MD_1845_SSCAPE)
1485 for (i = 16; i < 32; i++)
1486 ad_write(devc, i, init_values[i]);
1487
1488 if (devc->model == MD_IWAVE)
1489 ad_write(devc, 16, 0x30);
1490 }
1491 if (devc->model > MD_1848)
1492 {
1493 if (devc->audio_flags & DMA_DUPLEX)
1494 ad_write(devc, 9, ad_read(devc, 9) & ~0x04);
1495 else
1496 ad_write(devc, 9, ad_read(devc, 9) | 0x04);
1497
1498 if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE)
1499 ad_write(devc, 27, ad_read(devc, 27) | 0x08);
1500
1501 if (devc->model == MD_IWAVE)
1502 {
1503 ad_write(devc, 12, 0x6c);
1504 ad_write(devc, 16, 0x30);
1505 ad_write(devc, 17, 0xc2);
1506 }
1507 }
1508 else
1509 {
1510 devc->audio_flags &= ~DMA_DUPLEX;
1511 ad_write(devc, 9, ad_read(devc, 9) | 0x04);
1512 if (soundpro)
1513 ad_write(devc, 12, ad_read(devc, 12) | 0x40);
1514 }
1515
1516 outb((0), io_Status(devc));
1517
1518
1519
1520
1521
1522 ad_enter_MCE(devc);
1523 ad_leave_MCE(devc);
1524
1525 ad1848_mixer_reset(devc);
1526}
1527
1528int ad1848_detect(struct resource *ports, int *ad_flags, int *osp)
1529{
1530 unsigned char tmp;
1531 ad1848_info *devc = &adev_info[nr_ad1848_devs];
1532 unsigned char tmp1 = 0xff, tmp2 = 0xff;
1533 int optiC930 = 0;
1534 int interwave = 0;
1535 int ad1847_flag = 0;
1536 int cs4248_flag = 0;
1537 int sscape_flag = 0;
1538 int io_base = ports->start;
1539
1540 int i;
1541
1542 DDB(printk("ad1848_detect(%x)\n", io_base));
1543
1544 if (ad_flags)
1545 {
1546 if (*ad_flags == 0x12345678)
1547 {
1548 interwave = 1;
1549 *ad_flags = 0;
1550 }
1551
1552 if (*ad_flags == 0x87654321)
1553 {
1554 sscape_flag = 1;
1555 *ad_flags = 0;
1556 }
1557
1558 if (*ad_flags == 0x12345677)
1559 {
1560 cs4248_flag = 1;
1561 *ad_flags = 0;
1562 }
1563 }
1564 if (nr_ad1848_devs >= MAX_AUDIO_DEV)
1565 {
1566 printk(KERN_ERR "ad1848 - Too many audio devices\n");
1567 return 0;
1568 }
1569 spin_lock_init(&devc->lock);
1570 devc->base = io_base;
1571 devc->irq_ok = 0;
1572 devc->timer_running = 0;
1573 devc->MCE_bit = 0x40;
1574 devc->irq = 0;
1575 devc->open_mode = 0;
1576 devc->chip_name = devc->name = "AD1848";
1577 devc->model = MD_1848;
1578 devc->levels = NULL;
1579 devc->debug_flag = 0;
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591 if (inb(devc->base) == 0xff)
1592 {
1593 DDB(printk("ad1848_detect: The base I/O address appears to be dead\n"));
1594 }
1595
1596
1597
1598
1599
1600 DDB(printk("ad1848_detect() - step 0\n"));
1601
1602 for (i = 0; i < 10000000; i++)
1603 {
1604 unsigned char x = inb(devc->base);
1605
1606 if (x == 0xff || !(x & 0x80))
1607 break;
1608 }
1609
1610 DDB(printk("ad1848_detect() - step A\n"));
1611
1612 if (inb(devc->base) == 0x80)
1613 ad_leave_MCE(devc);
1614
1615 if ((inb(devc->base) & 0x80) != 0x00)
1616 {
1617 DDB(printk("ad1848 detect error - step A (%02x)\n", (int) inb(devc->base)));
1618 return 0;
1619 }
1620
1621
1622
1623
1624
1625
1626
1627 DDB(printk("ad1848_detect() - step B\n"));
1628 ad_write(devc, 0, 0xaa);
1629 ad_write(devc, 1, 0x45);
1630
1631 if ((tmp1 = ad_read(devc, 0)) != 0xaa || (tmp2 = ad_read(devc, 1)) != 0x45)
1632 {
1633 if (tmp2 == 0x65)
1634 ad1847_flag = 1;
1635 else
1636 {
1637 DDB(printk("ad1848 detect error - step B (%x/%x)\n", tmp1, tmp2));
1638 return 0;
1639 }
1640 }
1641 DDB(printk("ad1848_detect() - step C\n"));
1642 ad_write(devc, 0, 0x45);
1643 ad_write(devc, 1, 0xaa);
1644
1645 if ((tmp1 = ad_read(devc, 0)) != 0x45 || (tmp2 = ad_read(devc, 1)) != 0xaa)
1646 {
1647 if (tmp2 == 0x8a)
1648 ad1847_flag = 1;
1649 else
1650 {
1651 DDB(printk("ad1848 detect error - step C (%x/%x)\n", tmp1, tmp2));
1652 return 0;
1653 }
1654 }
1655
1656
1657
1658
1659
1660
1661 DDB(printk("ad1848_detect() - step D\n"));
1662 tmp = ad_read(devc, 12);
1663 ad_write(devc, 12, (~tmp) & 0x0f);
1664
1665 if ((tmp & 0x0f) != ((tmp1 = ad_read(devc, 12)) & 0x0f))
1666 {
1667 DDB(printk("ad1848 detect error - step D (%x)\n", tmp1));
1668 return 0;
1669 }
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690 DDB(printk("ad1848_detect() - step F\n"));
1691 ad_write(devc, 12, 0);
1692
1693 for (i = 0; i < 16; i++)
1694 {
1695 if ((tmp1 = ad_read(devc, i)) != (tmp2 = ad_read(devc, i + 16)))
1696 {
1697 DDB(printk("ad1848 detect step F(%d/%x/%x) - OPTi chip???\n", i, tmp1, tmp2));
1698 if (!ad1847_flag)
1699 optiC930 = 1;
1700 break;
1701 }
1702 }
1703
1704
1705
1706
1707
1708
1709 DDB(printk("ad1848_detect() - step G\n"));
1710
1711 if (ad_flags && *ad_flags == 400)
1712 *ad_flags = 0;
1713 else
1714 ad_write(devc, 12, 0x40);
1715
1716
1717 if (ad_flags)
1718 *ad_flags = 0;
1719
1720 tmp1 = ad_read(devc, 12);
1721 if (tmp1 & 0x80)
1722 {
1723 if (ad_flags)
1724 *ad_flags |= AD_F_CS4248;
1725
1726 devc->chip_name = "CS4248";
1727 }
1728 if (optiC930 || (tmp1 & 0xc0) == (0x80 | 0x40))
1729 {
1730
1731
1732
1733
1734
1735
1736 DDB(printk("ad1848_detect() - step H\n"));
1737 ad_write(devc, 16, 0);
1738
1739 ad_write(devc, 0, 0x45);
1740 if ((tmp1 = ad_read(devc, 16)) != 0x45)
1741 {
1742 ad_write(devc, 0, 0xaa);
1743 if ((tmp1 = ad_read(devc, 16)) == 0xaa)
1744 {
1745 DDB(printk("ad1848 detect error - step H(%x)\n", tmp1));
1746 return 0;
1747 }
1748
1749
1750
1751
1752
1753 DDB(printk("ad1848_detect() - step I\n"));
1754 tmp1 = ad_read(devc, 25);
1755 ad_write(devc, 25, ~tmp1);
1756 if ((ad_read(devc, 25) & 0xe7) == (tmp1 & 0xe7))
1757 {
1758 int id;
1759
1760
1761
1762
1763
1764 devc->chip_name = "CS4231";
1765 devc->model = MD_4231;
1766
1767
1768
1769
1770
1771
1772
1773 id = ad_read(devc, 25);
1774 if ((id & 0xe7) == 0x80)
1775 id = ad_read(devc, 25);
1776 if ((id & 0xe7) == 0x80)
1777 id = ad_read(devc, 25);
1778 DDB(printk("ad1848_detect() - step J (%02x/%02x)\n", id, ad_read(devc, 25)));
1779
1780 if ((id & 0xe7) == 0x80) {
1781
1782
1783
1784
1785
1786
1787
1788 unsigned char tmp = ad_read(devc, 23);
1789 ad_write(devc, 23, ~tmp);
1790
1791 if (interwave)
1792 {
1793 devc->model = MD_IWAVE;
1794 devc->chip_name = "IWave";
1795 }
1796 else if (ad_read(devc, 23) != tmp)
1797 {
1798 devc->chip_name = "AD1845";
1799 devc->model = MD_1845;
1800 }
1801 else if (cs4248_flag)
1802 {
1803 if (ad_flags)
1804 *ad_flags |= AD_F_CS4248;
1805 devc->chip_name = "CS4248";
1806 devc->model = MD_1848;
1807 ad_write(devc, 12, ad_read(devc, 12) & ~0x40);
1808 }
1809 ad_write(devc, 23, tmp);
1810 }
1811 else
1812 {
1813 switch (id & 0x1f) {
1814 case 3:
1815 {
1816 int xid;
1817 ad_write(devc, 12, ad_read(devc, 12) | 0x60);
1818 ad_write(devc, 23, 0x9c);
1819 xid = inb(io_Indexed_Data(devc));
1820 ad_write(devc, 12, ad_read(devc, 12) & ~0x60);
1821 switch (xid & 0x1f)
1822 {
1823 case 0x00:
1824 devc->chip_name = "CS4237B(B)";
1825 devc->model = MD_42xB;
1826 break;
1827 case 0x08:
1828
1829 devc->chip_name = "CS4238";
1830 devc->model = MD_42xB;
1831 break;
1832 case 0x09:
1833 devc->chip_name = "CS4238B";
1834 devc->model = MD_42xB;
1835 break;
1836 case 0x0b:
1837 devc->chip_name = "CS4236B";
1838 devc->model = MD_4236;
1839 break;
1840 case 0x10:
1841 devc->chip_name = "CS4237B";
1842 devc->model = MD_42xB;
1843 break;
1844 case 0x1d:
1845 devc->chip_name = "CS4235";
1846 devc->model = MD_4235;
1847 break;
1848 case 0x1e:
1849 devc->chip_name = "CS4239";
1850 devc->model = MD_4239;
1851 break;
1852 default:
1853 printk("Chip ident is %X.\n", xid&0x1F);
1854 devc->chip_name = "CS42xx";
1855 devc->model = MD_4232;
1856 break;
1857 }
1858 }
1859 break;
1860
1861 case 2:
1862 devc->chip_name = "CS4232";
1863 devc->model = MD_4232;
1864 break;
1865
1866 case 0:
1867 if ((id & 0xe0) == 0xa0)
1868 {
1869 devc->chip_name = "CS4231A";
1870 devc->model = MD_4231A;
1871 }
1872 else
1873 {
1874 devc->chip_name = "CS4321";
1875 devc->model = MD_4231;
1876 }
1877 break;
1878
1879 default:
1880 DDB(printk("ad1848: I25 = %02x/%02x\n", ad_read(devc, 25), ad_read(devc, 25) & 0xe7));
1881 if (optiC930)
1882 {
1883 devc->chip_name = "82C930";
1884 devc->model = MD_C930;
1885 }
1886 else
1887 {
1888 devc->chip_name = "CS4231";
1889 devc->model = MD_4231;
1890 }
1891 }
1892 }
1893 }
1894 ad_write(devc, 25, tmp1);
1895
1896 DDB(printk("ad1848_detect() - step K\n"));
1897 }
1898 } else if (tmp1 == 0x0a) {
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909 for (i = 0; i < 16; i++) {
1910 if ((tmp1 = ad_read(devc, i)) != (tmp2 = ad_read(devc, i + 16))) {
1911 DDB(printk("ad1848 detect step H(%d/%x/%x) - SoundPro chip?\n", i, tmp1, tmp2));
1912 soundpro = 1;
1913 devc->chip_name = "SoundPro CMI 8330";
1914 break;
1915 }
1916 }
1917 }
1918
1919 DDB(printk("ad1848_detect() - step L\n"));
1920 if (ad_flags)
1921 {
1922 if (devc->model != MD_1848)
1923 *ad_flags |= AD_F_CS4231;
1924 }
1925 DDB(printk("ad1848_detect() - Detected OK\n"));
1926
1927 if (devc->model == MD_1848 && ad1847_flag)
1928 devc->chip_name = "AD1847";
1929
1930
1931 if (sscape_flag == 1)
1932 devc->model = MD_1845_SSCAPE;
1933
1934 return 1;
1935}
1936
1937int ad1848_init (char *name, struct resource *ports, int irq, int dma_playback,
1938 int dma_capture, int share_dma, int *osp, struct module *owner)
1939{
1940
1941
1942
1943
1944
1945
1946 int my_dev;
1947 char dev_name[100];
1948 int e;
1949
1950 ad1848_info *devc = &adev_info[nr_ad1848_devs];
1951
1952 ad1848_port_info *portc = NULL;
1953
1954 devc->irq = (irq > 0) ? irq : 0;
1955 devc->open_mode = 0;
1956 devc->timer_ticks = 0;
1957 devc->dma1 = dma_playback;
1958 devc->dma2 = dma_capture;
1959 devc->subtype = cfg.card_subtype;
1960 devc->audio_flags = DMA_AUTOMODE;
1961 devc->playback_dev = devc->record_dev = 0;
1962 if (name != NULL)
1963 devc->name = name;
1964
1965 if (name != NULL && name[0] != 0)
1966 sprintf(dev_name,
1967 "%s (%s)", name, devc->chip_name);
1968 else
1969 sprintf(dev_name,
1970 "Generic audio codec (%s)", devc->chip_name);
1971
1972 rename_region(ports, devc->name);
1973
1974 conf_printf2(dev_name, devc->base, devc->irq, dma_playback, dma_capture);
1975
1976 if (devc->model == MD_1848 || devc->model == MD_C930)
1977 devc->audio_flags |= DMA_HARDSTOP;
1978
1979 if (devc->model > MD_1848)
1980 {
1981 if (devc->dma1 == devc->dma2 || devc->dma2 == -1 || devc->dma1 == -1)
1982 devc->audio_flags &= ~DMA_DUPLEX;
1983 else
1984 devc->audio_flags |= DMA_DUPLEX;
1985 }
1986
1987 portc = kmalloc(sizeof(ad1848_port_info), GFP_KERNEL);
1988 if(portc==NULL) {
1989 release_region(devc->base, 4);
1990 return -1;
1991 }
1992
1993 if ((my_dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
1994 dev_name,
1995 &ad1848_audio_driver,
1996 sizeof(struct audio_driver),
1997 devc->audio_flags,
1998 ad_format_mask[devc->model],
1999 devc,
2000 dma_playback,
2001 dma_capture)) < 0)
2002 {
2003 release_region(devc->base, 4);
2004 kfree(portc);
2005 return -1;
2006 }
2007
2008 audio_devs[my_dev]->portc = portc;
2009 audio_devs[my_dev]->mixer_dev = -1;
2010 if (owner)
2011 audio_devs[my_dev]->d->owner = owner;
2012 memset((char *) portc, 0, sizeof(*portc));
2013
2014 nr_ad1848_devs++;
2015
2016 ad1848_init_hw(devc);
2017
2018 if (irq > 0)
2019 {
2020 devc->dev_no = my_dev;
2021 if (request_irq(devc->irq, adintr, 0, devc->name,
2022 (void *)(long)my_dev) < 0)
2023 {
2024 printk(KERN_WARNING "ad1848: Unable to allocate IRQ\n");
2025
2026 devc->irq = 0;
2027 }
2028 if (capabilities[devc->model].flags & CAP_F_TIMER)
2029 {
2030#ifndef CONFIG_SMP
2031 int x;
2032 unsigned char tmp = ad_read(devc, 16);
2033#endif
2034
2035 devc->timer_ticks = 0;
2036
2037 ad_write(devc, 21, 0x00);
2038 ad_write(devc, 20, 0x10);
2039#ifndef CONFIG_SMP
2040 ad_write(devc, 16, tmp | 0x40);
2041 for (x = 0; x < 100000 && devc->timer_ticks == 0; x++);
2042 ad_write(devc, 16, tmp & ~0x40);
2043
2044 if (devc->timer_ticks == 0)
2045 printk(KERN_WARNING "ad1848: Interrupt test failed (IRQ%d)\n", irq);
2046 else
2047 {
2048 DDB(printk("Interrupt test OK\n"));
2049 devc->irq_ok = 1;
2050 }
2051#else
2052 devc->irq_ok = 1;
2053#endif
2054 }
2055 else
2056 devc->irq_ok = 1;
2057 } else if (irq < 0)
2058 devc->dev_no = my_dev;
2059
2060#ifndef EXCLUDE_TIMERS
2061 if ((capabilities[devc->model].flags & CAP_F_TIMER) &&
2062 devc->irq_ok)
2063 ad1848_tmr_install(my_dev);
2064#endif
2065
2066 if (!share_dma)
2067 {
2068 if (sound_alloc_dma(dma_playback, devc->name))
2069 printk(KERN_WARNING "ad1848.c: Can't allocate DMA%d\n", dma_playback);
2070
2071 if (dma_capture != dma_playback)
2072 if (sound_alloc_dma(dma_capture, devc->name))
2073 printk(KERN_WARNING "ad1848.c: Can't allocate DMA%d\n", dma_capture);
2074 }
2075
2076 if ((e = sound_install_mixer(MIXER_DRIVER_VERSION,
2077 dev_name,
2078 &ad1848_mixer_operations,
2079 sizeof(struct mixer_operations),
2080 devc)) >= 0)
2081 {
2082 audio_devs[my_dev]->mixer_dev = e;
2083 if (owner)
2084 mixer_devs[e]->owner = owner;
2085 }
2086 return my_dev;
2087}
2088
2089int ad1848_control(int cmd, int arg)
2090{
2091 ad1848_info *devc;
2092 unsigned long flags;
2093
2094 if (nr_ad1848_devs < 1)
2095 return -ENODEV;
2096
2097 devc = &adev_info[nr_ad1848_devs - 1];
2098
2099 switch (cmd)
2100 {
2101 case AD1848_SET_XTAL:
2102 if (devc->model != MD_1845 && devc->model != MD_1845_SSCAPE)
2103 return -EINVAL;
2104 spin_lock_irqsave(&devc->lock,flags);
2105 ad_enter_MCE(devc);
2106 ad_write(devc, 29, (ad_read(devc, 29) & 0x1f) | (arg << 5));
2107 ad_leave_MCE(devc);
2108 spin_unlock_irqrestore(&devc->lock,flags);
2109 break;
2110
2111 case AD1848_MIXER_REROUTE:
2112 {
2113 int o = (arg >> 8) & 0xff;
2114 int n = arg & 0xff;
2115
2116 if (o < 0 || o >= SOUND_MIXER_NRDEVICES)
2117 return -EINVAL;
2118
2119 if (!(devc->supported_devices & (1 << o)) &&
2120 !(devc->supported_rec_devices & (1 << o)))
2121 return -EINVAL;
2122
2123 if (n == SOUND_MIXER_NONE)
2124 {
2125 ad1848_mixer_set(devc, o, 0);
2126 devc->supported_devices &= ~(1 << o);
2127 devc->supported_rec_devices &= ~(1 << o);
2128 break;
2129 }
2130
2131
2132 if (n < 0 || n >= SOUND_MIXER_NRDEVICES)
2133 return -EINVAL;
2134
2135 devc->mixer_reroute[n] = o;
2136 if (devc->supported_devices & (1 << o))
2137 devc->supported_devices |= (1 << n);
2138 if (devc->supported_rec_devices & (1 << o))
2139 devc->supported_rec_devices |= (1 << n);
2140
2141 devc->supported_devices &= ~(1 << o);
2142 devc->supported_rec_devices &= ~(1 << o);
2143 }
2144 break;
2145 }
2146 return 0;
2147}
2148
2149void ad1848_unload(int io_base, int irq, int dma_playback, int dma_capture, int share_dma)
2150{
2151 int i, mixer, dev = 0;
2152 ad1848_info *devc = NULL;
2153
2154 for (i = 0; devc == NULL && i < nr_ad1848_devs; i++)
2155 {
2156 if (adev_info[i].base == io_base)
2157 {
2158 devc = &adev_info[i];
2159 dev = devc->dev_no;
2160 }
2161 }
2162
2163 if (devc != NULL)
2164 {
2165 kfree(audio_devs[dev]->portc);
2166 release_region(devc->base, 4);
2167
2168 if (!share_dma)
2169 {
2170 if (devc->irq > 0)
2171 free_irq(devc->irq, (void *)(long)devc->dev_no);
2172
2173 sound_free_dma(dma_playback);
2174
2175 if (dma_playback != dma_capture)
2176 sound_free_dma(dma_capture);
2177
2178 }
2179 mixer = audio_devs[devc->dev_no]->mixer_dev;
2180 if(mixer>=0)
2181 sound_unload_mixerdev(mixer);
2182
2183 nr_ad1848_devs--;
2184 for ( ; i < nr_ad1848_devs ; i++)
2185 adev_info[i] = adev_info[i+1];
2186 }
2187 else
2188 printk(KERN_ERR "ad1848: Can't find device to be unloaded. Base=%x\n", io_base);
2189}
2190
2191static irqreturn_t adintr(int irq, void *dev_id)
2192{
2193 unsigned char status;
2194 ad1848_info *devc;
2195 int dev;
2196 int alt_stat = 0xff;
2197 unsigned char c930_stat = 0;
2198 int cnt = 0;
2199
2200 dev = (long)dev_id;
2201 devc = (ad1848_info *) audio_devs[dev]->devc;
2202
2203interrupt_again:
2204
2205 status = inb(io_Status(devc));
2206
2207 if (status == 0x80)
2208 printk(KERN_DEBUG "adintr: Why?\n");
2209 if (devc->model == MD_1848)
2210 outb((0), io_Status(devc));
2211
2212 if (status & 0x01)
2213 {
2214 if (devc->model == MD_C930)
2215 {
2216
2217 spin_lock(&devc->lock);
2218
2219
2220
2221
2222 outb(11, 0xe0e);
2223 c930_stat = inb(0xe0f);
2224 outb((~c930_stat), 0xe0f);
2225
2226 spin_unlock(&devc->lock);
2227
2228 alt_stat = (c930_stat << 2) & 0x30;
2229 }
2230 else if (devc->model != MD_1848)
2231 {
2232 spin_lock(&devc->lock);
2233 alt_stat = ad_read(devc, 24);
2234 ad_write(devc, 24, ad_read(devc, 24) & ~alt_stat);
2235 spin_unlock(&devc->lock);
2236 }
2237
2238 if ((devc->open_mode & OPEN_READ) && (devc->audio_mode & PCM_ENABLE_INPUT) && (alt_stat & 0x20))
2239 {
2240 DMAbuf_inputintr(devc->record_dev);
2241 }
2242 if ((devc->open_mode & OPEN_WRITE) && (devc->audio_mode & PCM_ENABLE_OUTPUT) &&
2243 (alt_stat & 0x10))
2244 {
2245 DMAbuf_outputintr(devc->playback_dev, 1);
2246 }
2247 if (devc->model != MD_1848 && (alt_stat & 0x40))
2248 {
2249 devc->timer_ticks++;
2250#ifndef EXCLUDE_TIMERS
2251 if (timer_installed == dev && devc->timer_running)
2252 sound_timer_interrupt();
2253#endif
2254 }
2255 }
2256
2257
2258
2259
2260
2261
2262 if (inb(io_Status(devc)) & 0x01 && cnt++ < 4)
2263 {
2264 goto interrupt_again;
2265 }
2266 return IRQ_HANDLED;
2267}
2268
2269
2270
2271
2272
2273
2274static int init_deskpro_m(struct address_info *hw_config)
2275{
2276 unsigned char tmp;
2277
2278 if ((tmp = inb(0xc44)) == 0xff)
2279 {
2280 DDB(printk("init_deskpro_m: Dead port 0xc44\n"));
2281 return 0;
2282 }
2283
2284 outb(0x10, 0xc44);
2285 outb(0x40, 0xc45);
2286 outb(0x00, 0xc46);
2287 outb(0xe8, 0xc47);
2288 outb(0x14, 0xc44);
2289 outb(0x40, 0xc45);
2290 outb(0x00, 0xc46);
2291 outb(0xe8, 0xc47);
2292 outb(0x10, 0xc44);
2293
2294 return 1;
2295}
2296
2297
2298
2299
2300
2301
2302static int init_deskpro(struct address_info *hw_config)
2303{
2304 unsigned char tmp;
2305
2306 if ((tmp = inb(0xc44)) == 0xff)
2307 {
2308 DDB(printk("init_deskpro: Dead port 0xc44\n"));
2309 return 0;
2310 }
2311 outb((tmp | 0x04), 0xc44);
2312 if (inb(0xc44) != 0x04)
2313 {
2314 DDB(printk("init_deskpro: Invalid bank1 signature in port 0xc44\n"));
2315 return 0;
2316 }
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348#ifdef DEBUGXL
2349
2350 printk("Port 0xc44 (before): ");
2351 outb((tmp & ~0x04), 0xc44);
2352 printk("%02x ", inb(0xc44));
2353 outb((tmp | 0x04), 0xc44);
2354 printk("%02x\n", inb(0xc44));
2355#endif
2356
2357
2358 tmp = 0x58;
2359
2360 switch (hw_config->io_base)
2361 {
2362 case 0x530:
2363 tmp |= 0x00;
2364 break;
2365 case 0x604:
2366 tmp |= 0x01;
2367 break;
2368 case 0xf40:
2369 tmp |= 0x02;
2370 break;
2371 case 0xe80:
2372 tmp |= 0x03;
2373 break;
2374 default:
2375 DDB(printk("init_deskpro: Invalid MSS port %x\n", hw_config->io_base));
2376 return 0;
2377 }
2378 outb((tmp & ~0x04), 0xc44);
2379
2380#ifdef DEBUGXL
2381
2382 printk("Port 0xc44 (after): ");
2383 outb((tmp & ~0x04), 0xc44);
2384 printk("%02x ", inb(0xc44));
2385 outb((tmp | 0x04), 0xc44);
2386 printk("%02x\n", inb(0xc44));
2387#endif
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399#ifdef DEBUGXL
2400
2401 printk("Port 0xc45 (before): ");
2402 outb((tmp & ~0x04), 0xc44);
2403 printk("%02x ", inb(0xc45));
2404 outb((tmp | 0x04), 0xc44);
2405 printk("%02x\n", inb(0xc45));
2406#endif
2407
2408 outb((tmp & ~0x04), 0xc44);
2409 outb((0x88), 0xc45);
2410 outb((tmp | 0x04), 0xc44);
2411 outb((0x10), 0xc45);
2412
2413#ifdef DEBUGXL
2414
2415 printk("Port 0xc45 (after): ");
2416 outb((tmp & ~0x04), 0xc44);
2417 printk("%02x ", inb(0xc45));
2418 outb((tmp | 0x04), 0xc44);
2419 printk("%02x\n", inb(0xc45));
2420#endif
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430#ifdef DEBUGXL
2431
2432 printk("Port 0xc46 (before): ");
2433 outb((tmp & ~0x04), 0xc44);
2434 printk("%02x ", inb(0xc46));
2435 outb((tmp | 0x04), 0xc44);
2436 printk("%02x\n", inb(0xc46));
2437#endif
2438
2439 outb((tmp & ~0x04), 0xc44);
2440 outb((0x03), 0xc46);
2441 outb((tmp | 0x04), 0xc44);
2442 outb((0x11), 0xc46);
2443
2444#ifdef DEBUGXL
2445
2446 printk("Port 0xc46 (after): ");
2447 outb((tmp & ~0x04), 0xc44);
2448 printk("%02x ", inb(0xc46));
2449 outb((tmp | 0x04), 0xc44);
2450 printk("%02x\n", inb(0xc46));
2451#endif
2452
2453
2454
2455
2456
2457
2458
2459
2460#ifdef DEBUGXL
2461
2462 printk("Port 0xc47 (before): ");
2463 outb((tmp & ~0x04), 0xc44);
2464 printk("%02x ", inb(0xc47));
2465 outb((tmp | 0x04), 0xc44);
2466 printk("%02x\n", inb(0xc47));
2467#endif
2468
2469 outb((tmp & ~0x04), 0xc44);
2470 outb((0x7c), 0xc47);
2471 outb((tmp | 0x04), 0xc44);
2472 outb((0x00), 0xc47);
2473
2474#ifdef DEBUGXL
2475
2476 printk("Port 0xc47 (after): ");
2477 outb((tmp & ~0x04), 0xc44);
2478 printk("%02x ", inb(0xc47));
2479 outb((tmp | 0x04), 0xc44);
2480 printk("%02x\n", inb(0xc47));
2481#endif
2482
2483
2484
2485
2486
2487#ifdef DEBUGXL
2488 printk("Port 0xc6f (before) = %02x\n", inb(0xc6f));
2489#endif
2490
2491 outb((0x80), 0xc6f);
2492
2493#ifdef DEBUGXL
2494 printk("Port 0xc6f (after) = %02x\n", inb(0xc6f));
2495#endif
2496
2497 return 1;
2498}
2499
2500int probe_ms_sound(struct address_info *hw_config, struct resource *ports)
2501{
2502 unsigned char tmp;
2503
2504 DDB(printk("Entered probe_ms_sound(%x, %d)\n", hw_config->io_base, hw_config->card_subtype));
2505
2506 if (hw_config->card_subtype == 1)
2507 {
2508
2509 return ad1848_detect(ports, NULL, hw_config->osp);
2510 }
2511
2512 if (deskpro_xl && hw_config->card_subtype == 2)
2513 {
2514 if (!init_deskpro(hw_config))
2515 return 0;
2516 }
2517
2518 if (deskpro_m)
2519 {
2520 if (!init_deskpro_m(hw_config))
2521 return 0;
2522 }
2523
2524
2525
2526
2527
2528
2529
2530 if ((tmp = inb(hw_config->io_base + 3)) == 0xff)
2531 {
2532 int ret;
2533
2534 DDB(printk("I/O address is inactive (%x)\n", tmp));
2535 if (!(ret = ad1848_detect(ports, NULL, hw_config->osp)))
2536 return 0;
2537 return 1;
2538 }
2539 DDB(printk("MSS signature = %x\n", tmp & 0x3f));
2540 if ((tmp & 0x3f) != 0x04 &&
2541 (tmp & 0x3f) != 0x0f &&
2542 (tmp & 0x3f) != 0x00)
2543 {
2544 int ret;
2545
2546 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)));
2547 DDB(printk("Trying to detect codec anyway but IRQ/DMA may not work\n"));
2548 if (!(ret = ad1848_detect(ports, NULL, hw_config->osp)))
2549 return 0;
2550
2551 hw_config->card_subtype = 1;
2552 return 1;
2553 }
2554 if ((hw_config->irq != 5) &&
2555 (hw_config->irq != 7) &&
2556 (hw_config->irq != 9) &&
2557 (hw_config->irq != 10) &&
2558 (hw_config->irq != 11) &&
2559 (hw_config->irq != 12))
2560 {
2561 printk(KERN_ERR "MSS: Bad IRQ %d\n", hw_config->irq);
2562 return 0;
2563 }
2564 if (hw_config->dma != 0 && hw_config->dma != 1 && hw_config->dma != 3)
2565 {
2566 printk(KERN_ERR "MSS: Bad DMA %d\n", hw_config->dma);
2567 return 0;
2568 }
2569
2570
2571
2572
2573 if (hw_config->dma == 0 && inb(hw_config->io_base + 3) & 0x80)
2574 {
2575 printk(KERN_ERR "MSS: Can't use DMA0 with a 8 bit card/slot\n");
2576 return 0;
2577 }
2578 if (hw_config->irq > 7 && hw_config->irq != 9 && inb(hw_config->io_base + 3) & 0x80)
2579 {
2580 printk(KERN_ERR "MSS: Can't use IRQ%d with a 8 bit card/slot\n", hw_config->irq);
2581 return 0;
2582 }
2583 return ad1848_detect(ports, NULL, hw_config->osp);
2584}
2585
2586void attach_ms_sound(struct address_info *hw_config, struct resource *ports, struct module *owner)
2587{
2588 static signed char interrupt_bits[12] =
2589 {
2590 -1, -1, -1, -1, -1, 0x00, -1, 0x08, -1, 0x10, 0x18, 0x20
2591 };
2592 signed char bits;
2593 char dma2_bit = 0;
2594
2595 static char dma_bits[4] =
2596 {
2597 1, 2, 0, 3
2598 };
2599
2600 int config_port = hw_config->io_base + 0;
2601 int version_port = hw_config->io_base + 3;
2602 int dma = hw_config->dma;
2603 int dma2 = hw_config->dma2;
2604
2605 if (hw_config->card_subtype == 1)
2606 {
2607 hw_config->slots[0] = ad1848_init("MS Sound System", ports,
2608 hw_config->irq,
2609 hw_config->dma,
2610 hw_config->dma2, 0,
2611 hw_config->osp,
2612 owner);
2613 return;
2614 }
2615
2616
2617
2618
2619 bits = interrupt_bits[hw_config->irq];
2620 if (bits == -1)
2621 {
2622 printk(KERN_ERR "MSS: Bad IRQ %d\n", hw_config->irq);
2623 release_region(ports->start, 4);
2624 release_region(ports->start - 4, 4);
2625 return;
2626 }
2627 outb((bits | 0x40), config_port);
2628 if ((inb(version_port) & 0x40) == 0)
2629 printk(KERN_ERR "[MSS: IRQ Conflict?]\n");
2630
2631
2632
2633
2634
2635 if (dma2 != -1 && dma2 != dma)
2636 {
2637 if (!((dma == 0 && dma2 == 1) ||
2638 (dma == 1 && dma2 == 0) ||
2639 (dma == 3 && dma2 == 0)))
2640 {
2641 int tmp = dma;
2642
2643 dma = dma2;
2644 dma2 = tmp;
2645 }
2646 if ((dma == 0 && dma2 == 1) ||
2647 (dma == 1 && dma2 == 0) ||
2648 (dma == 3 && dma2 == 0))
2649 {
2650 dma2_bit = 0x04;
2651 }
2652 else
2653 {
2654 printk(KERN_WARNING "MSS: Invalid capture DMA\n");
2655 dma2 = dma;
2656 }
2657 }
2658 else
2659 {
2660 dma2 = dma;
2661 }
2662
2663 hw_config->dma = dma;
2664 hw_config->dma2 = dma2;
2665
2666 outb((bits | dma_bits[dma] | dma2_bit), config_port);
2667
2668 hw_config->slots[0] = ad1848_init("MS Sound System", ports,
2669 hw_config->irq,
2670 dma, dma2, 0,
2671 hw_config->osp,
2672 THIS_MODULE);
2673}
2674
2675void unload_ms_sound(struct address_info *hw_config)
2676{
2677 ad1848_unload(hw_config->io_base + 4,
2678 hw_config->irq,
2679 hw_config->dma,
2680 hw_config->dma2, 0);
2681 sound_unload_audiodev(hw_config->slots[0]);
2682 release_region(hw_config->io_base, 4);
2683}
2684
2685#ifndef EXCLUDE_TIMERS
2686
2687
2688
2689
2690
2691static unsigned int current_interval;
2692
2693static unsigned int ad1848_tmr_start(int dev, unsigned int usecs)
2694{
2695 unsigned long flags;
2696 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
2697 unsigned long xtal_nsecs;
2698 unsigned long divider;
2699
2700 spin_lock_irqsave(&devc->lock,flags);
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713 if (devc->model == MD_1845 || devc->model == MD_1845_SSCAPE)
2714 xtal_nsecs = 10050;
2715 else if (ad_read(devc, 8) & 0x01)
2716 xtal_nsecs = 9920;
2717 else
2718 xtal_nsecs = 9969;
2719
2720 divider = (usecs * 1000 + xtal_nsecs / 2) / xtal_nsecs;
2721
2722 if (divider < 100)
2723 divider = 100;
2724
2725 if (divider > 65535)
2726 divider = 65535;
2727
2728 ad_write(devc, 21, (divider >> 8) & 0xff);
2729 ad_write(devc, 20, divider & 0xff);
2730 ad_write(devc, 16, ad_read(devc, 16) | 0x40);
2731 devc->timer_running = 1;
2732 spin_unlock_irqrestore(&devc->lock,flags);
2733
2734 return current_interval = (divider * xtal_nsecs + 500) / 1000;
2735}
2736
2737static void ad1848_tmr_reprogram(int dev)
2738{
2739
2740
2741
2742
2743
2744 ad1848_tmr_start(dev, current_interval);
2745 sound_timer_syncinterval(current_interval);
2746}
2747
2748static void ad1848_tmr_disable(int dev)
2749{
2750 unsigned long flags;
2751 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
2752
2753 spin_lock_irqsave(&devc->lock,flags);
2754 ad_write(devc, 16, ad_read(devc, 16) & ~0x40);
2755 devc->timer_running = 0;
2756 spin_unlock_irqrestore(&devc->lock,flags);
2757}
2758
2759static void ad1848_tmr_restart(int dev)
2760{
2761 unsigned long flags;
2762 ad1848_info *devc = (ad1848_info *) audio_devs[dev]->devc;
2763
2764 if (current_interval == 0)
2765 return;
2766
2767 spin_lock_irqsave(&devc->lock,flags);
2768 ad_write(devc, 16, ad_read(devc, 16) | 0x40);
2769 devc->timer_running = 1;
2770 spin_unlock_irqrestore(&devc->lock,flags);
2771}
2772
2773static struct sound_lowlev_timer ad1848_tmr =
2774{
2775 0,
2776 2,
2777 ad1848_tmr_start,
2778 ad1848_tmr_disable,
2779 ad1848_tmr_restart
2780};
2781
2782static int ad1848_tmr_install(int dev)
2783{
2784 if (timer_installed != -1)
2785 return 0;
2786
2787 timer_installed = ad1848_tmr.dev = dev;
2788 sound_timer_init(&ad1848_tmr, audio_devs[dev]->name);
2789
2790 return 1;
2791}
2792#endif
2793
2794EXPORT_SYMBOL(ad1848_detect);
2795EXPORT_SYMBOL(ad1848_init);
2796EXPORT_SYMBOL(ad1848_unload);
2797EXPORT_SYMBOL(ad1848_control);
2798EXPORT_SYMBOL(probe_ms_sound);
2799EXPORT_SYMBOL(attach_ms_sound);
2800EXPORT_SYMBOL(unload_ms_sound);
2801
2802static int __initdata io = -1;
2803static int __initdata irq = -1;
2804static int __initdata dma = -1;
2805static int __initdata dma2 = -1;
2806static int __initdata type = 0;
2807
2808module_param_hw(io, int, ioport, 0);
2809module_param_hw(irq, int, irq, 0);
2810module_param_hw(dma, int, dma, 0);
2811module_param_hw(dma2, int, dma, 0);
2812module_param(type, int, 0);
2813module_param(deskpro_xl, bool, 0);
2814module_param(deskpro_m, bool, 0);
2815module_param(soundpro, bool, 0);
2816
2817#ifdef CONFIG_PNP
2818module_param(isapnp, int, 0);
2819module_param(isapnpjump, int, 0);
2820module_param(reverse, bool, 0);
2821MODULE_PARM_DESC(isapnp, "When set to 0, Plug & Play support will be disabled");
2822MODULE_PARM_DESC(isapnpjump, "Jumps to a specific slot in the driver's PnP table. Use the source, Luke.");
2823MODULE_PARM_DESC(reverse, "When set to 1, will reverse ISAPnP search order");
2824
2825static struct pnp_dev *ad1848_dev = NULL;
2826
2827
2828static struct {
2829 char *name;
2830 unsigned short card_vendor, card_device,
2831 vendor, function;
2832 short mss_io, irq, dma, dma2;
2833 int type;
2834} ad1848_isapnp_list[] __initdata = {
2835 {"CMI 8330 SoundPRO",
2836 ISAPNP_VENDOR('C','M','I'), ISAPNP_DEVICE(0x0001),
2837 ISAPNP_VENDOR('@','@','@'), ISAPNP_FUNCTION(0x0001),
2838 0, 0, 0,-1, 0},
2839 {"CS4232 based card",
2840 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2841 ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0000),
2842 0, 0, 0, 1, 0},
2843 {"CS4232 based card",
2844 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2845 ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0100),
2846 0, 0, 0, 1, 0},
2847 {"OPL3-SA2 WSS mode",
2848 ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2849 ISAPNP_VENDOR('Y','M','H'), ISAPNP_FUNCTION(0x0021),
2850 1, 0, 0, 1, 1},
2851 {"Advanced Gravis InterWave Audio",
2852 ISAPNP_VENDOR('G','R','V'), ISAPNP_DEVICE(0x0001),
2853 ISAPNP_VENDOR('G','R','V'), ISAPNP_FUNCTION(0x0000),
2854 0, 0, 0, 1, 0},
2855 {NULL}
2856};
2857
2858#ifdef MODULE
2859static struct isapnp_device_id id_table[] = {
2860 { ISAPNP_VENDOR('C','M','I'), ISAPNP_DEVICE(0x0001),
2861 ISAPNP_VENDOR('@','@','@'), ISAPNP_FUNCTION(0x0001), 0 },
2862 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2863 ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0000), 0 },
2864 { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
2865 ISAPNP_VENDOR('C','S','C'), ISAPNP_FUNCTION(0x0100), 0 },
2866
2867
2868
2869
2870 { ISAPNP_VENDOR('G','R','V'), ISAPNP_DEVICE(0x0001),
2871 ISAPNP_VENDOR('G','R','V'), ISAPNP_FUNCTION(0x0000), 0 },
2872 {0}
2873};
2874
2875MODULE_DEVICE_TABLE(isapnp, id_table);
2876#endif
2877
2878static struct pnp_dev *activate_dev(char *devname, char *resname, struct pnp_dev *dev)
2879{
2880 int err;
2881
2882 err = pnp_device_attach(dev);
2883 if (err < 0)
2884 return(NULL);
2885
2886 if((err = pnp_activate_dev(dev)) < 0) {
2887 printk(KERN_ERR "ad1848: %s %s config failed (out of resources?)[%d]\n", devname, resname, err);
2888
2889 pnp_device_detach(dev);
2890
2891 return(NULL);
2892 }
2893 audio_activated = 1;
2894 return(dev);
2895}
2896
2897static struct pnp_dev __init *ad1848_init_generic(struct pnp_card *bus,
2898 struct address_info *hw_config, int slot)
2899{
2900
2901
2902 if((ad1848_dev = pnp_find_dev(bus, ad1848_isapnp_list[slot].vendor, ad1848_isapnp_list[slot].function, NULL)))
2903 {
2904 if((ad1848_dev = activate_dev(ad1848_isapnp_list[slot].name, "ad1848", ad1848_dev)))
2905 {
2906 hw_config->io_base = pnp_port_start(ad1848_dev, ad1848_isapnp_list[slot].mss_io);
2907 hw_config->irq = pnp_irq(ad1848_dev, ad1848_isapnp_list[slot].irq);
2908 hw_config->dma = pnp_dma(ad1848_dev, ad1848_isapnp_list[slot].dma);
2909 if(ad1848_isapnp_list[slot].dma2 != -1)
2910 hw_config->dma2 = pnp_dma(ad1848_dev, ad1848_isapnp_list[slot].dma2);
2911 else
2912 hw_config->dma2 = -1;
2913 hw_config->card_subtype = ad1848_isapnp_list[slot].type;
2914 } else
2915 return(NULL);
2916 } else
2917 return(NULL);
2918
2919 return(ad1848_dev);
2920}
2921
2922static int __init ad1848_isapnp_init(struct address_info *hw_config, struct pnp_card *bus, int slot)
2923{
2924 char *busname = bus->name[0] ? bus->name : ad1848_isapnp_list[slot].name;
2925
2926
2927
2928 if(ad1848_init_generic(bus, hw_config, slot)) {
2929
2930
2931 printk(KERN_NOTICE "ad1848: PnP reports '%s' at i/o %#x, irq %d, dma %d, %d\n",
2932 busname,
2933 hw_config->io_base, hw_config->irq, hw_config->dma,
2934 hw_config->dma2);
2935 return 1;
2936 }
2937 return 0;
2938}
2939
2940static int __init ad1848_isapnp_probe(struct address_info *hw_config)
2941{
2942 static int first = 1;
2943 int i;
2944
2945
2946 for (i = 0; ad1848_isapnp_list[i].card_vendor != 0; i++);
2947 i--;
2948
2949
2950 if( isapnpjump < 0 || isapnpjump > i) {
2951 isapnpjump = reverse ? i : 0;
2952 printk(KERN_ERR "ad1848: Valid range for isapnpjump is 0-%d. Adjusted to %d.\n", i, isapnpjump);
2953 }
2954
2955 if(!first || !reverse)
2956 i = isapnpjump;
2957 first = 0;
2958 while(ad1848_isapnp_list[i].card_vendor != 0) {
2959 static struct pnp_card *bus = NULL;
2960
2961 while ((bus = pnp_find_card(
2962 ad1848_isapnp_list[i].card_vendor,
2963 ad1848_isapnp_list[i].card_device,
2964 bus))) {
2965
2966 if(ad1848_isapnp_init(hw_config, bus, i)) {
2967 isapnpjump = i;
2968 return 0;
2969 }
2970 }
2971 i += reverse ? -1 : 1;
2972 }
2973
2974 return -ENODEV;
2975}
2976#endif
2977
2978
2979static int __init init_ad1848(void)
2980{
2981 printk(KERN_INFO "ad1848/cs4248 codec driver Copyright (C) by Hannu Savolainen 1993-1996\n");
2982
2983#ifdef CONFIG_PNP
2984 if(isapnp && (ad1848_isapnp_probe(&cfg) < 0) ) {
2985 printk(KERN_NOTICE "ad1848: No ISAPnP cards found, trying standard ones...\n");
2986 isapnp = 0;
2987 }
2988#endif
2989
2990 if(io != -1) {
2991 struct resource *ports;
2992 if( isapnp == 0 )
2993 {
2994 if(irq == -1 || dma == -1) {
2995 printk(KERN_WARNING "ad1848: must give I/O , IRQ and DMA.\n");
2996 return -EINVAL;
2997 }
2998
2999 cfg.irq = irq;
3000 cfg.io_base = io;
3001 cfg.dma = dma;
3002 cfg.dma2 = dma2;
3003 cfg.card_subtype = type;
3004 }
3005
3006 ports = request_region(io + 4, 4, "ad1848");
3007
3008 if (!ports)
3009 return -EBUSY;
3010
3011 if (!request_region(io, 4, "WSS config")) {
3012 release_region(io + 4, 4);
3013 return -EBUSY;
3014 }
3015
3016 if (!probe_ms_sound(&cfg, ports)) {
3017 release_region(io + 4, 4);
3018 release_region(io, 4);
3019 return -ENODEV;
3020 }
3021 attach_ms_sound(&cfg, ports, THIS_MODULE);
3022 loaded = 1;
3023 }
3024 return 0;
3025}
3026
3027static void __exit cleanup_ad1848(void)
3028{
3029 if(loaded)
3030 unload_ms_sound(&cfg);
3031
3032#ifdef CONFIG_PNP
3033 if(ad1848_dev){
3034 if(audio_activated)
3035 pnp_device_detach(ad1848_dev);
3036 }
3037#endif
3038}
3039
3040module_init(init_ad1848);
3041module_exit(cleanup_ad1848);
3042
3043#ifndef MODULE
3044static int __init setup_ad1848(char *str)
3045{
3046
3047 int ints[6];
3048
3049 str = get_options(str, ARRAY_SIZE(ints), ints);
3050
3051 io = ints[1];
3052 irq = ints[2];
3053 dma = ints[3];
3054 dma2 = ints[4];
3055 type = ints[5];
3056
3057 return 1;
3058}
3059
3060__setup("ad1848=", setup_ad1848);
3061#endif
3062MODULE_LICENSE("GPL");
3063