This source file includes following definitions.
- sb_dsp_command
- ess_write
- ess_read
- sbintr
- sb_get_irq
- sb_free_irq
- sb_reset_dsp
- dsp_speaker
- ess_speed
- dsp_speed
- dsp_set_stereo
- sb_dsp_output_block
- actually_output_block
- sb_dsp_start_input
- actually_start_input
- sb_dsp_trigger
- dsp_cleanup
- sb_dsp_prepare_for_input
- sb_dsp_prepare_for_output
- sb_dsp_halt_xfer
- verify_irq
- sb_dsp_open
- sb_dsp_close
- dsp_set_bits
- sb_dsp_ioctl
- sb_dsp_reset
- get_sb_byte
- smw_putmem
- smw_getmem
- initialize_smw
- initialize_ProSonic16
- sb_dsp_detect
- ess_init
- ess_midi_init
- Jazz16_midi_init
- Jazz16_set_dma16
- dsp_get_vers
- sb_dsp_init
- sb_dsp_unload
- sb_dsp_disable_midi
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 #include <linux/config.h>
30
31
32
33
34
35
36
37
38
39
40 #include "sound_config.h"
41
42 #if defined(CONFIG_SB)
43
44 #include "sb.h"
45 #include "sb_mixer.h"
46 #undef SB_TEST_IRQ
47
48 int sbc_base = 0;
49 static int sbc_irq = 0, sbc_dma;
50 static int open_mode = 0;
51 int Jazz16_detected = 0;
52 int AudioDrive = 0;
53 static int ess_mpu_irq = 0;
54 int sb_no_recording = 0;
55 static int dsp_count = 0;
56 static int trigger_bits;
57 static int mpu_base = 0, mpu_irq = 0;
58
59
60
61
62
63
64
65
66
67 int sb_dsp_ok = 0;
68
69
70
71
72 static int midi_disabled = 0;
73 int sb_dsp_highspeed = 0;
74 int sbc_major = 1, sbc_minor = 0;
75 static int dsp_stereo = 0;
76 static int dsp_current_speed = DSP_DEFAULT_SPEED;
77 static int sb16 = 0;
78 static int irq_verified = 0;
79
80 int sb_midi_mode = NORMAL_MIDI;
81 int sb_midi_busy = 0;
82 int sb_dsp_busy = 0;
83
84 volatile int sb_irq_mode = IMODE_NONE;
85 static volatile int irq_ok = 0;
86
87 static int dma8 = 1;
88 static int dsp_16bit = 0;
89
90
91
92
93 static int dma16 = 1;
94
95 static int dsp_set_bits (int arg);
96 static int initialize_ProSonic16 (void);
97
98
99
100
101 int sb_duplex_midi = 0;
102 static int my_dev = 0;
103
104 volatile int sb_intr_active = 0;
105
106 static int dsp_speed (int);
107 static int dsp_set_stereo (int mode);
108 static void sb_dsp_reset (int dev);
109 static void dsp_get_vers (struct address_info *hw_config);
110 int *sb_osp = NULL;
111
112 #if defined(CONFIG_MIDI) || defined(CONFIG_AUDIO)
113
114
115
116
117
118 int
119 sb_dsp_command (unsigned char val)
120 {
121 int i;
122 unsigned long limit;
123
124 limit = jiffies + HZ / 10;
125
126
127
128
129
130
131
132
133
134
135
136 for (i = 0; i < 500000 && jiffies < limit; i++)
137 {
138 if ((inb (DSP_STATUS) & 0x80) == 0)
139 {
140 outb (val, DSP_COMMAND);
141 return 1;
142 }
143 }
144
145 printk ("SoundBlaster: DSP Command(%x) Timeout.\n", val);
146 return 0;
147 }
148
149 static int
150 ess_write (unsigned char reg, unsigned char data)
151 {
152
153
154 if (!sb_dsp_command (reg))
155 return 0;
156
157 return sb_dsp_command (data);
158 }
159
160 static int
161 ess_read (unsigned char reg)
162 {
163
164
165 int i;
166
167 if (!sb_dsp_command (0xc0))
168 return -1;
169
170 if (!sb_dsp_command (reg))
171 return -1;
172
173 for (i = 1000; i; i--)
174 {
175 if (inb (DSP_DATA_AVAIL) & 0x80)
176 return inb (DSP_READ);
177 }
178
179 return -1;
180 }
181
182 void
183 sbintr (int irq, struct pt_regs *dummy)
184 {
185 int status;
186
187 if (sb16 && !AudioDrive)
188 {
189 unsigned char src = sb_getmixer (IRQ_STAT);
190
191 if (src & 3)
192 sb16_dsp_interrupt (irq);
193
194 #ifdef CONFIG_MIDI
195 if (src & 4)
196 sb16midiintr (irq);
197
198
199 #endif
200
201 if (!(src & 1))
202 return;
203
204
205 }
206
207 status = inb (DSP_DATA_AVAIL);
208
209
210 if (sb_intr_active)
211 switch (sb_irq_mode)
212 {
213 case IMODE_OUTPUT:
214 if (!AudioDrive)
215 sb_intr_active = 0;
216 DMAbuf_outputintr (my_dev, 1);
217 break;
218
219 case IMODE_INPUT:
220 if (!AudioDrive)
221 sb_intr_active = 0;
222 DMAbuf_inputintr (my_dev);
223 break;
224
225 case IMODE_INIT:
226 sb_intr_active = 0;
227 irq_ok = 1;
228 break;
229
230 case IMODE_MIDI:
231 #ifdef CONFIG_MIDI
232 sb_midi_interrupt (irq);
233 #endif
234 break;
235
236 default:
237 printk ("SoundBlaster: Unexpected interrupt\n");
238 }
239 }
240
241 int
242 sb_get_irq (void)
243 {
244 return 0;
245 }
246
247 void
248 sb_free_irq (void)
249 {
250 }
251
252 int
253 sb_reset_dsp (void)
254 {
255 int loopc;
256
257 if (AudioDrive)
258 outb (3, DSP_RESET);
259 else
260 outb (1, DSP_RESET);
261
262 tenmicrosec (sb_osp);
263 outb (0, DSP_RESET);
264 tenmicrosec (sb_osp);
265 tenmicrosec (sb_osp);
266 tenmicrosec (sb_osp);
267
268 for (loopc = 0; loopc < 1000 && !(inb (DSP_DATA_AVAIL) & 0x80); loopc++);
269
270 if (inb (DSP_READ) != 0xAA)
271 return 0;
272
273 if (AudioDrive)
274 sb_dsp_command (0xc6);
275
276 return 1;
277 }
278
279 #endif
280
281 #ifdef CONFIG_AUDIO
282
283 static void
284 dsp_speaker (char state)
285 {
286 if (state)
287 sb_dsp_command (DSP_CMD_SPKON);
288 else
289 sb_dsp_command (DSP_CMD_SPKOFF);
290 }
291
292 static int
293 ess_speed (int speed)
294 {
295 int rate;
296 unsigned char bits = 0;
297
298 if (speed < 4000)
299 speed = 4000;
300 else if (speed > 48000)
301 speed = 48000;
302
303 if (speed > 22000)
304 {
305 bits = 0x80;
306 rate = 256 - (795500 + speed / 2) / speed;
307 speed = 795500 / (256 - rate);
308 }
309 else
310 {
311 rate = 128 - (397700 + speed / 2) / speed;
312 speed = 397700 / (128 - rate);
313 }
314
315 bits |= (unsigned char) rate;
316 ess_write (0xa1, bits);
317
318 dsp_current_speed = speed;
319 return speed;
320 }
321
322 static int
323 dsp_speed (int speed)
324 {
325 unsigned char tconst;
326 unsigned long flags;
327 int max_speed = 44100;
328
329 if (AudioDrive)
330 return ess_speed (speed);
331
332 if (speed < 4000)
333 speed = 4000;
334
335
336
337
338
339 if (sbc_major < 2 ||
340 (sbc_major == 2 && sbc_minor == 0))
341 max_speed = 22050;
342
343
344
345
346 if (open_mode != OPEN_WRITE)
347 if (sbc_major < 3)
348 if (sbc_major == 2 && sbc_minor > 0)
349 max_speed = 15000;
350 else
351 max_speed = 13000;
352
353 if (speed > max_speed)
354 speed = max_speed;
355
356
357
358
359 #if !defined (SM_GAMES)
360
361
362
363 if (dsp_stereo && speed > 22050 && Jazz16_detected == 0 && AudioDrive == 0)
364 speed = 22050;
365 #endif
366
367 if ((speed > 22050) && sb_midi_busy)
368 {
369 printk ("SB Warning: High speed DSP not possible simultaneously with MIDI output\n");
370 speed = 22050;
371 }
372
373 if (dsp_stereo)
374 speed *= 2;
375
376
377
378
379
380 if (speed > 22050)
381 {
382
383
384 int tmp;
385
386 tconst = (unsigned char) ((65536 -
387 ((256000000 + speed / 2) / speed)) >> 8);
388 sb_dsp_highspeed = 1;
389
390 save_flags (flags);
391 cli ();
392 if (sb_dsp_command (0x40))
393 sb_dsp_command (tconst);
394 restore_flags (flags);
395
396 tmp = 65536 - (tconst << 8);
397 speed = (256000000 + tmp / 2) / tmp;
398 }
399 else
400 {
401 int tmp;
402
403 sb_dsp_highspeed = 0;
404 tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
405
406 save_flags (flags);
407 cli ();
408 if (sb_dsp_command (0x40))
409
410
411 sb_dsp_command (tconst);
412 restore_flags (flags);
413
414 tmp = 256 - tconst;
415 speed = (1000000 + tmp / 2) / tmp;
416 }
417
418 if (dsp_stereo)
419 speed /= 2;
420
421 dsp_current_speed = speed;
422 return speed;
423 }
424
425 static int
426 dsp_set_stereo (int mode)
427 {
428 dsp_stereo = 0;
429
430 if (sbc_major < 3 || sb16)
431 return 0;
432
433
434
435 if (mode && sb_midi_busy)
436 {
437 printk ("SB Warning: Stereo DSP not possible simultaneously with MIDI output\n");
438 return 0;
439 }
440
441 dsp_stereo = !!mode;
442 return dsp_stereo;
443 }
444
445 static unsigned long trg_buf;
446 static int trg_bytes;
447 static int trg_intrflag;
448 static int trg_restart;
449
450 static void
451 sb_dsp_output_block (int dev, unsigned long buf, int nr_bytes,
452 int intrflag, int restart_dma)
453 {
454 trg_buf = buf;
455 trg_bytes = nr_bytes;
456 trg_intrflag = intrflag;
457 trg_restart = restart_dma;
458 sb_irq_mode = IMODE_OUTPUT;
459 }
460
461 static void
462 actually_output_block (int dev, unsigned long buf, int nr_bytes,
463 int intrflag, int restart_dma)
464 {
465 unsigned long flags;
466 int count = nr_bytes;
467
468 if (!sb_irq_mode)
469 dsp_speaker (ON);
470
471 DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE);
472
473 sb_irq_mode = 0;
474
475 if (audio_devs[dev]->dmachan1 > 3)
476 count >>= 1;
477 count--;
478 dsp_count = count;
479
480 sb_irq_mode = IMODE_OUTPUT;
481
482 if (AudioDrive)
483 {
484 int c = 0xffff - nr_bytes;
485
486 ess_write (0xa4, (unsigned char) (c & 0xff));
487 ess_write (0xa5, (unsigned char) ((c >> 8) & 0xff));
488
489 ess_write (0xb8, ess_read (0xb8) | 0x01);
490 }
491 else if (sb_dsp_highspeed)
492 {
493 save_flags (flags);
494 cli ();
495 if (sb_dsp_command (0x48))
496
497
498 {
499 sb_dsp_command ((unsigned char) (dsp_count & 0xff));
500 sb_dsp_command ((unsigned char) ((dsp_count >> 8) & 0xff));
501 sb_dsp_command (0x91);
502
503
504 }
505 else
506 printk ("SB Error: Unable to start (high speed) DAC\n");
507 restore_flags (flags);
508 }
509 else
510 {
511 save_flags (flags);
512 cli ();
513 if (sb_dsp_command (0x14))
514
515
516 {
517 sb_dsp_command ((unsigned char) (dsp_count & 0xff));
518 sb_dsp_command ((unsigned char) ((dsp_count >> 8) & 0xff));
519 }
520 else
521 printk ("SB Error: Unable to start DAC\n");
522 restore_flags (flags);
523 }
524 sb_intr_active = 1;
525 }
526
527 static void
528 sb_dsp_start_input (int dev, unsigned long buf, int count, int intrflag,
529 int restart_dma)
530 {
531 if (sb_no_recording)
532 {
533 return;
534 }
535
536 trg_buf = buf;
537 trg_bytes = count;
538 trg_intrflag = intrflag;
539 trg_restart = restart_dma;
540 sb_irq_mode = IMODE_INPUT;
541 }
542
543 static void
544 actually_start_input (int dev, unsigned long buf, int nr_bytes, int intrflag,
545 int restart_dma)
546 {
547 unsigned long flags;
548 int count = nr_bytes;
549
550 if (sb_no_recording)
551 {
552 return;
553 }
554
555
556
557
558
559 if (!sb_irq_mode)
560 dsp_speaker (OFF);
561
562 DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ);
563 sb_irq_mode = 0;
564
565 if (audio_devs[dev]->dmachan1 > 3)
566 count >>= 1;
567 count--;
568 dsp_count = count;
569
570 sb_irq_mode = IMODE_INPUT;
571
572 if (AudioDrive)
573 {
574 int c = 0xffff - nr_bytes;
575
576 ess_write (0xa4, (unsigned char) (c & 0xff));
577 ess_write (0xa5, (unsigned char) ((c >> 8) & 0xff));
578
579 ess_write (0xb8, ess_read (0xb8) | 0x01);
580 }
581 else if (sb_dsp_highspeed)
582 {
583 save_flags (flags);
584 cli ();
585 if (sb_dsp_command (0x48))
586
587
588 {
589 sb_dsp_command ((unsigned char) (dsp_count & 0xff));
590 sb_dsp_command ((unsigned char) ((dsp_count >> 8) & 0xff));
591 sb_dsp_command (0x99);
592
593
594 }
595 else
596 printk ("SB Error: Unable to start (high speed) ADC\n");
597 restore_flags (flags);
598 }
599 else
600 {
601 save_flags (flags);
602 cli ();
603 if (sb_dsp_command (0x24))
604
605
606 {
607 sb_dsp_command ((unsigned char) (dsp_count & 0xff));
608 sb_dsp_command ((unsigned char) ((dsp_count >> 8) & 0xff));
609 }
610 else
611 printk ("SB Error: Unable to start ADC\n");
612 restore_flags (flags);
613 }
614
615 sb_intr_active = 1;
616 }
617
618 static void
619 sb_dsp_trigger (int dev, int bits)
620 {
621
622 if (!bits)
623 sb_dsp_command (0xd0);
624 else if (bits & sb_irq_mode)
625 {
626 switch (sb_irq_mode)
627 {
628 case IMODE_INPUT:
629 actually_start_input (my_dev, trg_buf, trg_bytes,
630 trg_intrflag, trg_restart);
631 break;
632
633 case IMODE_OUTPUT:
634 actually_output_block (my_dev, trg_buf, trg_bytes,
635 trg_intrflag, trg_restart);
636 break;
637 }
638 }
639
640 trigger_bits = bits;
641 }
642
643 static void
644 dsp_cleanup (void)
645 {
646 sb_intr_active = 0;
647 }
648
649 static int
650 sb_dsp_prepare_for_input (int dev, int bsize, int bcount)
651 {
652 if (sb_no_recording)
653 {
654 printk ("SB Error: This device doesn't support recording\n");
655 return 0;
656 }
657
658 dsp_cleanup ();
659 dsp_speaker (OFF);
660
661 if (sbc_major == 3)
662
663
664 {
665 if (AudioDrive)
666 {
667
668 ess_write (0xb8, 0x0e);
669 ess_write (0xa8, (ess_read (0xa8) & ~0x04) |
670 (2 - dsp_stereo));
671 ess_write (0xb9, 2);
672
673 if (!dsp_stereo)
674 {
675 if (dsp_16bit == 0)
676 {
677 ess_write (0xb7, 0x51);
678 ess_write (0xb7, 0xd0);
679 }
680 else
681 {
682 ess_write (0xb7, 0x71);
683 ess_write (0xb7, 0xf4);
684 }
685 }
686 else
687 {
688 if (!dsp_16bit)
689 {
690 ess_write (0xb7, 0x51);
691 ess_write (0xb7, 0x98);
692 }
693 else
694 {
695 ess_write (0xb7, 0x71);
696 ess_write (0xb7, 0xbc);
697 }
698 }
699
700 ess_write (0xb1, (ess_read (0xb1) & 0x0f) | 0x50);
701 ess_write (0xb2, (ess_read (0xb2) & 0x0f) | 0x50);
702 }
703 else
704 {
705
706
707
708
709 audio_devs[my_dev]->dmachan1 =
710 audio_devs[my_dev]->dmachan2 =
711 dsp_16bit ? dma16 : dma8;
712 if (dsp_stereo)
713 sb_dsp_command (dsp_16bit ? 0xac : 0xa8);
714 else
715 sb_dsp_command (dsp_16bit ? 0xa4 : 0xa0);
716
717 dsp_speed (dsp_current_speed);
718 }
719 }
720 trigger_bits = 0;
721 return 0;
722 }
723
724 static int
725 sb_dsp_prepare_for_output (int dev, int bsize, int bcount)
726 {
727 dsp_cleanup ();
728 dsp_speaker (OFF);
729
730 if (sbc_major == 3)
731 {
732
733 if (AudioDrive)
734 {
735
736 ess_write (0xb8, 4);
737 ess_write (0xa8, ess_read (0xa8) |
738 (2 - dsp_stereo));
739 ess_write (0xb9, 2);
740
741 if (!dsp_stereo)
742 {
743 if (dsp_16bit == 0)
744 {
745 ess_write (0xb6, 0x80);
746 ess_write (0xb7, 0x51);
747 ess_write (0xb7, 0xd0);
748 }
749 else
750 {
751 ess_write (0xb6, 0x00);
752 ess_write (0xb7, 0x71);
753 ess_write (0xb7, 0xf4);
754 }
755 }
756 else
757 {
758 if (!dsp_16bit)
759 {
760 ess_write (0xb6, 0x80);
761 ess_write (0xb7, 0x51);
762 ess_write (0xb7, 0x98);
763 }
764 else
765 {
766 ess_write (0xb6, 0x00);
767 ess_write (0xb7, 0x71);
768 ess_write (0xb7, 0xbc);
769 }
770 }
771
772 ess_write (0xb1, (ess_read (0xb1) & 0x0f) | 0x50);
773 ess_write (0xb2, (ess_read (0xb2) & 0x0f) | 0x50);
774 }
775 else
776 {
777
778
779
780 audio_devs[my_dev]->dmachan1 =
781 audio_devs[my_dev]->dmachan2 =
782 dsp_16bit ? dma16 : dma8;
783 if (Jazz16_detected != 2)
784 sb_mixer_set_stereo (dsp_stereo);
785 if (dsp_stereo)
786 sb_dsp_command (dsp_16bit ? 0xac : 0xa8);
787 else
788 sb_dsp_command (dsp_16bit ? 0xa4 : 0xa0);
789 }
790
791 }
792
793 trigger_bits = 0;
794 dsp_speaker (ON);
795 return 0;
796 }
797
798 static void
799 sb_dsp_halt_xfer (int dev)
800 {
801 }
802
803 static int
804 verify_irq (void)
805 {
806 irq_ok = 1;
807 return irq_ok;
808 }
809
810 static int
811 sb_dsp_open (int dev, int mode)
812 {
813 int retval;
814
815 if (!sb_dsp_ok)
816 {
817 printk ("SB Error: SoundBlaster board not installed\n");
818 return -ENXIO;
819 }
820
821 if (sb_no_recording && mode & OPEN_READ)
822 {
823 printk ("SB Warning: Recording not supported by this device\n");
824 }
825
826 if (sb_intr_active || (sb_midi_busy && sb_midi_mode == UART_MIDI))
827 {
828 printk ("SB: PCM not possible during MIDI input\n");
829 return -EBUSY;
830 }
831
832 if (!irq_verified)
833 {
834 verify_irq ();
835 irq_verified = 1;
836 }
837 else if (!irq_ok)
838 printk ("SB Warning: Incorrect IRQ setting %d\n",
839 sbc_irq);
840
841 retval = sb_get_irq ();
842 if (retval)
843 return retval;
844
845
846
847 audio_devs[my_dev]->dmachan1 =
848 audio_devs[my_dev]->dmachan2 =
849 dma8;
850
851
852
853 if (Jazz16_detected != 0)
854 if (dma16 != dma8)
855 {
856 if (sound_open_dma (dma16, "Jazz16 16 bit"))
857 {
858 sb_free_irq ();
859
860 return -EBUSY;
861 }
862 }
863
864 sb_irq_mode = IMODE_NONE;
865
866 sb_dsp_busy = 1;
867 open_mode = mode;
868
869 return 0;
870 }
871
872 static void
873 sb_dsp_close (int dev)
874 {
875
876
877 if (Jazz16_detected)
878 {
879 audio_devs[my_dev]->dmachan1 =
880 audio_devs[my_dev]->dmachan2 =
881 dma8;
882
883 if (dma16 != dma8)
884 sound_close_dma (dma16);
885 }
886
887
888 sb_free_irq ();
889
890 dsp_cleanup ();
891 dsp_speaker (OFF);
892 sb_dsp_busy = 0;
893 sb_dsp_highspeed = 0;
894 open_mode = 0;
895 }
896
897 static int
898 dsp_set_bits (int arg)
899 {
900 if (arg)
901 if (Jazz16_detected == 0 && AudioDrive == 0)
902 dsp_16bit = 0;
903 else
904 switch (arg)
905 {
906 case 8:
907 dsp_16bit = 0;
908 break;
909 case 16:
910 dsp_16bit = 1;
911 break;
912 default:
913 dsp_16bit = 0;
914 }
915
916 return dsp_16bit ? 16 : 8;
917 }
918
919 static int
920 sb_dsp_ioctl (int dev, unsigned int cmd, caddr_t arg, int local)
921 {
922 switch (cmd)
923 {
924 case SOUND_PCM_WRITE_RATE:
925 if (local)
926 return dsp_speed ((long) arg);
927 return snd_ioctl_return ((int *) arg, dsp_speed (get_fs_long ((long *) arg)));
928 break;
929
930 case SOUND_PCM_READ_RATE:
931 if (local)
932 return dsp_current_speed;
933 return snd_ioctl_return ((int *) arg, dsp_current_speed);
934 break;
935
936 case SOUND_PCM_WRITE_CHANNELS:
937 if (local)
938 return dsp_set_stereo ((long) arg - 1) + 1;
939 return snd_ioctl_return ((int *) arg, dsp_set_stereo (get_fs_long ((long *) arg) - 1) + 1);
940 break;
941
942 case SOUND_PCM_READ_CHANNELS:
943 if (local)
944 return dsp_stereo + 1;
945 return snd_ioctl_return ((int *) arg, dsp_stereo + 1);
946 break;
947
948 case SNDCTL_DSP_STEREO:
949 if (local)
950 return dsp_set_stereo ((long) arg);
951 return snd_ioctl_return ((int *) arg, dsp_set_stereo (get_fs_long ((long *) arg)));
952 break;
953
954
955
956
957 case SNDCTL_DSP_SETFMT:
958 if (local)
959 return dsp_set_bits ((long) arg);
960 return snd_ioctl_return ((int *) arg, dsp_set_bits (get_fs_long ((long *) arg)));
961 break;
962
963 case SOUND_PCM_READ_BITS:
964 if (local)
965 return dsp_16bit ? 16 : 8;
966 return snd_ioctl_return ((int *) arg, dsp_16bit ? 16 : 8);
967 break;
968
969 case SOUND_PCM_WRITE_FILTER:
970 case SOUND_PCM_READ_FILTER:
971 return -EINVAL;
972 break;
973
974 default:;
975 }
976
977 return -EINVAL;
978 }
979
980 static void
981 sb_dsp_reset (int dev)
982 {
983 unsigned long flags;
984
985 save_flags (flags);
986 cli ();
987
988 sb_reset_dsp ();
989 dsp_speed (dsp_current_speed);
990 dsp_cleanup ();
991
992 restore_flags (flags);
993 }
994
995 #endif
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009 unsigned int
1010 get_sb_byte (void)
1011 {
1012 int i;
1013
1014 for (i = 1000; i; i--)
1015 if (inb (DSP_DATA_AVAIL) & 0x80)
1016 {
1017 return inb (DSP_READ);
1018 }
1019
1020 return 0xffff;
1021 }
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033 static void
1034 smw_putmem (int base, int addr, unsigned char val)
1035 {
1036 unsigned long flags;
1037
1038 save_flags (flags);
1039 cli ();
1040
1041 outb (addr & 0xff, base + 1);
1042 outb (addr >> 8, base + 2);
1043 outb (val, base);
1044
1045 restore_flags (flags);
1046 }
1047
1048 static unsigned char
1049 smw_getmem (int base, int addr)
1050 {
1051 unsigned long flags;
1052 unsigned char val;
1053
1054 save_flags (flags);
1055 cli ();
1056
1057 outb (addr & 0xff, base + 1);
1058 outb (addr >> 8, base + 2);
1059 val = inb (base);
1060
1061 restore_flags (flags);
1062 return val;
1063 }
1064
1065 #ifdef SMW_MIDI0001_INCLUDED
1066 #include "smw-midi0001.h"
1067 #else
1068 unsigned char *smw_ucode = NULL;
1069 int smw_ucodeLen = 0;
1070
1071 #endif
1072
1073 static int
1074 initialize_smw (int mpu_base)
1075 {
1076
1077 int mp_base = mpu_base + 4;
1078 int i;
1079 unsigned char control;
1080
1081
1082
1083
1084
1085
1086 control = inb (mpu_base + 7);
1087 outb (control | 3, mpu_base + 7);
1088 outb ((control & 0xfe) | 2, mpu_base + 7);
1089
1090 for (i = 0; i < 300; i++)
1091 tenmicrosec (sb_osp);
1092
1093 outb (control & 0xfc, mpu_base + 7);
1094
1095
1096
1097
1098 smw_putmem (mp_base, 0, 0x00);
1099 smw_putmem (mp_base, 1, 0xff);
1100 tenmicrosec (sb_osp);
1101
1102 if (smw_getmem (mp_base, 0) != 0x00 || smw_getmem (mp_base, 1) != 0xff)
1103 {
1104 printk ("\nSM Wave: No microcontroller RAM detected (%02x, %02x)\n",
1105 smw_getmem (mp_base, 0), smw_getmem (mp_base, 1));
1106 return 0;
1107 }
1108
1109
1110
1111
1112
1113 if (smw_ucodeLen > 0)
1114 {
1115 if (smw_ucodeLen != 8192)
1116 {
1117 printk ("\nSM Wave: Invalid microcode (MIDI0001.BIN) length\n");
1118 return 1;
1119 }
1120
1121
1122
1123
1124
1125 for (i = 0; i < 8192; i++)
1126 smw_putmem (mp_base, i, smw_ucode[i]);
1127
1128
1129
1130
1131
1132 for (i = 0; i < 8192; i++)
1133 if (smw_getmem (mp_base, i) != smw_ucode[i])
1134 {
1135 printk ("SM Wave: Microcode verification failed\n");
1136 return 0;
1137 }
1138 }
1139
1140 control = 0;
1141 #ifdef SMW_SCSI_IRQ
1142
1143
1144
1145
1146
1147
1148 {
1149 static unsigned char scsi_irq_bits[] =
1150 {0, 0, 3, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0};
1151
1152 control |= scsi_irq_bits[SMW_SCSI_IRQ] << 6;
1153 }
1154 #endif
1155
1156 #ifdef SMW_OPL4_ENABLE
1157
1158
1159
1160
1161
1162
1163
1164 control |= 0x10;
1165
1166 #endif
1167
1168 outb (control | 0x03, mpu_base + 7);
1169 return 1;
1170 }
1171
1172 static int
1173 initialize_ProSonic16 (void)
1174 {
1175 int x;
1176 static unsigned char int_translat[16] =
1177 {0, 0, 2, 3, 0, 1, 0, 4, 0, 2, 5, 0, 0, 0, 0, 6}, dma_translat[8] =
1178 {0, 1, 0, 2, 0, 3, 0, 4};
1179
1180 outb (0xAF, 0x201);
1181 for (x = 0; x < 1000; ++x)
1182 tenmicrosec (sb_osp);
1183 outb (0x50, 0x201);
1184 outb ((sbc_base & 0x70) | ((mpu_base & 0x30) >> 4), 0x201);
1185
1186 if (sb_reset_dsp ())
1187 {
1188
1189
1190
1191 if (!sb_dsp_command (0xFA))
1192 return 1;
1193 if (get_sb_byte () != 0x12)
1194 return 1;
1195
1196 if (sb_dsp_command (0xFB) &&
1197 sb_dsp_command ((dma_translat[dma16] << 4) | dma_translat[dma8]) &&
1198 sb_dsp_command ((int_translat[mpu_irq] << 4) | int_translat[sbc_irq]))
1199 {
1200 Jazz16_detected = 1;
1201
1202 if (mpu_base != 0)
1203 if (initialize_smw (mpu_base))
1204 Jazz16_detected = 2;
1205
1206 sb_dsp_disable_midi ();
1207 }
1208
1209 return 1;
1210 }
1211 return 0;
1212 }
1213
1214 int
1215 sb_dsp_detect (struct address_info *hw_config)
1216 {
1217 sbc_base = hw_config->io_base;
1218 sbc_irq = hw_config->irq;
1219 sbc_dma = hw_config->dma;
1220 sb_osp = hw_config->osp;
1221
1222 if (sb_dsp_ok)
1223 return 0;
1224
1225
1226 dma8 = dma16 = hw_config->dma;
1227
1228 if (sb_reset_dsp ())
1229 dsp_get_vers (hw_config);
1230 else
1231 sbc_major = 0;
1232
1233 if (sbc_major == 3 || sbc_major == 0)
1234 if (initialize_ProSonic16 ())
1235 return 1;
1236
1237 if (!sb_reset_dsp ())
1238 return 0;
1239
1240 return 1;
1241
1242
1243 }
1244
1245 #ifdef CONFIG_AUDIO
1246 static struct audio_operations sb_dsp_operations =
1247 {
1248 "SoundBlaster",
1249 NOTHING_SPECIAL,
1250 AFMT_U8,
1251 NULL,
1252 sb_dsp_open,
1253 sb_dsp_close,
1254 sb_dsp_output_block,
1255 sb_dsp_start_input,
1256 sb_dsp_ioctl,
1257 sb_dsp_prepare_for_input,
1258 sb_dsp_prepare_for_output,
1259 sb_dsp_reset,
1260 sb_dsp_halt_xfer,
1261 NULL,
1262 NULL,
1263 NULL,
1264 NULL,
1265 sb_dsp_trigger
1266 };
1267
1268 #endif
1269
1270 static void
1271 ess_init (int ess_minor)
1272 {
1273 unsigned char cfg, irq_bits = 0, dma_bits = 0;
1274
1275 AudioDrive = 1;
1276 sb_no_recording = 1;
1277
1278 if (ess_minor >= 8)
1279 midi_disabled = 1;
1280
1281 sb_reset_dsp ();
1282
1283
1284
1285
1286
1287 cfg = 0x50;
1288
1289 switch (sbc_irq)
1290 {
1291 case 2:
1292 case 9:
1293 irq_bits = 0;
1294 break;
1295
1296 case 5:
1297 irq_bits = 1;
1298 break;
1299
1300 case 7:
1301 irq_bits = 2;
1302 break;
1303
1304 case 10:
1305 irq_bits = 3;
1306 break;
1307
1308 default:
1309 irq_bits = 0;
1310 cfg = 0x10;
1311 printk ("\nESS1688: Invalid IRQ %d\n", sbc_irq);
1312 }
1313
1314 if (!ess_write (0xb1, cfg | (irq_bits << 2)))
1315 printk ("\nESS1688: Failed to write to IRQ config register\n");
1316
1317
1318
1319
1320
1321 cfg = 0x50;
1322
1323 if (sbc_dma > 3 || sbc_dma < 0 || sbc_dma == 2)
1324 {
1325 dma_bits = 0;
1326 cfg = 0x00;
1327 printk ("\nESS1688: Invalid DMA %d\n", sbc_dma);
1328 }
1329 else
1330 {
1331 if (sbc_dma == 3)
1332 dma_bits = 3;
1333 else
1334 dma_bits = sbc_dma + 1;
1335 }
1336
1337 if (!ess_write (0xb2, cfg | (dma_bits << 2)))
1338 printk ("\nESS1688: Failed to write to DMA config register\n");
1339
1340
1341
1342
1343
1344 cfg = sb_getmixer (0x40);
1345 sb_setmixer (0x40, cfg | 0x03);
1346 }
1347
1348 #ifdef CONFIG_MIDI
1349 void
1350 ess_midi_init (struct address_info *hw_config)
1351 {
1352 unsigned char cfg, tmp;
1353
1354 cfg = sb_getmixer (0x40) & 0x03;
1355
1356 tmp = (hw_config->io_base & 0x0f0) >> 4;
1357
1358 if (tmp > 3)
1359 {
1360 sb_setmixer (0x40, cfg);
1361 return;
1362 }
1363
1364 cfg |= tmp << 3;
1365
1366 tmp = 1;
1367
1368 switch (hw_config->irq)
1369 {
1370 case 9:
1371 tmp = 0x4;
1372 break;
1373 case 5:
1374 tmp = 0x5;
1375 break;
1376 case 7:
1377 tmp = 0x6;
1378 break;
1379 case 10:
1380 tmp = 0x7;
1381 break;
1382 }
1383
1384 cfg |= tmp << 5;
1385
1386 if (tmp != 1)
1387 {
1388 ess_mpu_irq = hw_config->irq;
1389
1390 if (snd_set_irq_handler (ess_mpu_irq, sbmidiintr, "ES1688 MIDI", sb_osp) < 0)
1391 printk ("ES1688: Can't allocate IRQ%d\n", ess_mpu_irq);
1392 }
1393
1394 sb_setmixer (0x40, cfg);
1395 }
1396 #endif
1397
1398 void
1399 Jazz16_midi_init (struct address_info *hw_config)
1400 {
1401 extern void smw_mixer_init (void);
1402 extern void sb_mixer_reset (void);
1403
1404 mpu_base = hw_config->io_base;
1405 mpu_irq = hw_config->irq;
1406
1407 initialize_ProSonic16 ();
1408
1409 smw_mixer_init ();
1410 sb_mixer_reset ();
1411 }
1412
1413 void
1414 Jazz16_set_dma16 (int dma)
1415 {
1416 dma16 = dma;
1417
1418
1419
1420 if (dma16 != dma8)
1421 {
1422 if (sound_alloc_dma (dma16, "Jazz16 16 bit"))
1423 {
1424 printk ("Jazz16: Can't allocate 16 bit DMA channel\n");
1425 Jazz16_detected = 0;
1426 return;
1427 }
1428 }
1429 initialize_ProSonic16 ();
1430 }
1431
1432 static void
1433 dsp_get_vers (struct address_info *hw_config)
1434 {
1435 int i;
1436
1437 unsigned long flags;
1438
1439 save_flags (flags);
1440 cli ();
1441 sb_osp = hw_config->osp;
1442 sbc_major = sbc_minor = 0;
1443 sb_dsp_command (0xe1);
1444
1445 for (i = 100000; i; i--)
1446 {
1447 if (inb (DSP_DATA_AVAIL) & 0x80)
1448 {
1449 if (sbc_major == 0)
1450 sbc_major = inb (DSP_READ);
1451 else
1452 {
1453 sbc_minor = inb (DSP_READ);
1454 break;
1455 }
1456 }
1457 }
1458 restore_flags (flags);
1459 }
1460
1461 long
1462 sb_dsp_init (long mem_start, struct address_info *hw_config)
1463 {
1464 int i;
1465 int ess_major = 0, ess_minor = 0;
1466
1467 int mixer_type = 0;
1468
1469 dsp_get_vers (hw_config);
1470
1471 if (sbc_major == 0)
1472 {
1473 sb_reset_dsp ();
1474 dsp_get_vers (hw_config);
1475 }
1476
1477 if (sbc_major == 0)
1478 {
1479 printk ("\n\nFailed to get SB version (%x) - possible I/O conflict?\n\n",
1480 inb (DSP_DATA_AVAIL));
1481 sbc_major = 1;
1482 }
1483
1484 if (sbc_major == 2 || sbc_major == 3)
1485 sb_duplex_midi = 1;
1486
1487 if (sbc_major == 4)
1488 sb16 = 1;
1489
1490 if (sbc_major == 3 && sbc_minor == 1)
1491 {
1492
1493
1494
1495
1496
1497 sb_dsp_command (0xe7);
1498
1499
1500
1501 for (i = 1000; i; i--)
1502 {
1503 if (inb (DSP_DATA_AVAIL) & 0x80)
1504 {
1505
1506
1507 if (ess_major == 0)
1508 ess_major = inb (DSP_READ);
1509 else
1510 {
1511 ess_minor = inb (DSP_READ);
1512 break;
1513 }
1514 }
1515 }
1516 }
1517
1518 if (snd_set_irq_handler (sbc_irq, sbintr, "SoundBlaster", sb_osp) < 0)
1519 printk ("sb_dsp: Can't allocate IRQ\n");;
1520
1521 #ifdef CONFIG_AUDIO
1522 if (sbc_major >= 3)
1523 {
1524 if (Jazz16_detected)
1525 {
1526 if (Jazz16_detected == 2)
1527 sprintf (sb_dsp_operations.name, "SoundMan Wave %d.%d", sbc_major, sbc_minor);
1528 else
1529 sprintf (sb_dsp_operations.name, "MV Jazz16 %d.%d", sbc_major, sbc_minor);
1530 sb_dsp_operations.format_mask |= AFMT_S16_LE;
1531 }
1532 else
1533 #ifdef __SGNXPRO__
1534 if (mixer_type == 2)
1535 {
1536 sprintf (sb_dsp_operations.name, "Sound Galaxy NX Pro %d.%d", sbc_major, sbc_minor);
1537 }
1538 else
1539 #endif
1540
1541 if (sbc_major == 4)
1542 {
1543 sprintf (sb_dsp_operations.name, "SoundBlaster 16 %d.%d", sbc_major, sbc_minor);
1544 }
1545 else if (ess_major != 0)
1546 {
1547 if (ess_major == 0x48 && (ess_minor & 0xf0) == 0x80)
1548 sprintf (sb_dsp_operations.name, "ESS ES488 AudioDrive (rev %d)",
1549 ess_minor & 0x0f);
1550 else if (ess_major == 0x68 && (ess_minor & 0xf0) == 0x80)
1551 {
1552 sprintf (sb_dsp_operations.name,
1553 "ESS ES1688 AudioDrive (rev %d)",
1554 ess_minor & 0x0f);
1555 sb_dsp_operations.format_mask |= AFMT_S16_LE;
1556 ess_init (ess_minor);
1557 }
1558 }
1559 else
1560 {
1561 sprintf (sb_dsp_operations.name, "SoundBlaster Pro %d.%d", sbc_major, sbc_minor);
1562 }
1563 }
1564 else
1565 {
1566 sprintf (sb_dsp_operations.name, "SoundBlaster %d.%d", sbc_major, sbc_minor);
1567 }
1568
1569 conf_printf (sb_dsp_operations.name, hw_config);
1570
1571 if (sbc_major >= 3)
1572 mixer_type = sb_mixer_init (sbc_major);
1573
1574 if (!sb16)
1575 if (num_audiodevs < MAX_AUDIO_DEV)
1576 {
1577 audio_devs[my_dev = num_audiodevs++] = &sb_dsp_operations;
1578
1579 if (AudioDrive)
1580 audio_devs[my_dev]->flags |= DMA_AUTOMODE;
1581
1582 audio_devs[my_dev]->buffsize = DSP_BUFFSIZE;
1583 dma8 = audio_devs[my_dev]->dmachan1 = hw_config->dma;
1584 dma16 = audio_devs[my_dev]->dmachan2 = hw_config->dma;
1585 if (sound_alloc_dma (hw_config->dma, "SoundBlaster"))
1586 printk ("sb_dsp.c: Can't allocate DMA channel\n");
1587 }
1588 else
1589 printk ("SB: Too many DSP devices available\n");
1590 #else
1591 conf_printf ("SoundBlaster (configured without audio support)", hw_config);
1592 #endif
1593
1594 #ifdef CONFIG_MIDI
1595 if (!midi_disabled && !sb16)
1596
1597
1598
1599 sb_midi_init (sbc_major);
1600 #endif
1601
1602 sb_dsp_ok = 1;
1603 sb_reset_dsp ();
1604 return mem_start;
1605 }
1606
1607 void
1608 sb_dsp_unload (void)
1609 {
1610 sound_free_dma (dma8);
1611
1612
1613
1614 if (Jazz16_detected != 0)
1615 if (dma16 != dma8)
1616 {
1617 sound_free_dma (dma16);
1618 }
1619 snd_release_irq (sbc_irq);
1620
1621 if (AudioDrive && ess_mpu_irq)
1622 {
1623 snd_release_irq (ess_mpu_irq);
1624 }
1625 }
1626
1627 void
1628 sb_dsp_disable_midi (void)
1629 {
1630 midi_disabled = 1;
1631 }
1632
1633
1634 #endif