This source file includes following definitions.
- sb_dsp_command
- sbintr
- sb_get_irq
- sb_free_irq
- sb_reset_dsp
- dsp_speaker
- dsp_speed
- dsp_set_stereo
- sb_dsp_output_block
- sb_dsp_start_input
- dsp_cleanup
- sb_dsp_prepare_for_input
- sb_dsp_prepare_for_output
- sb_dsp_halt_xfer
- verify_irq
- sb_dsp_open
- sb_dsp_close
- sb_dsp_ioctl
- sb_dsp_reset
- sb_dsp_detect
- sb_dsp_init
- 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
30 #include "sound_config.h"
31
32 #if defined(CONFIGURE_SOUNDCARD) && !defined(EXCLUDE_SB)
33
34 #include "sb.h"
35 #include "sb_mixer.h"
36 #undef SB_TEST_IRQ
37
38 int sbc_base = 0;
39 static int sbc_irq = 0;
40
41
42
43
44
45
46
47
48
49 int sb_dsp_ok = 0;
50 static int midi_disabled = 0;
51 int sb_dsp_highspeed = 0;
52 static int major=1, minor=0;
53 static int dsp_stereo = 0;
54 static int dsp_current_speed = DSP_DEFAULT_SPEED;
55 static int sb16 = 0;
56 static int irq_verified = 0;
57
58 int sb_midi_mode = NORMAL_MIDI;
59 int sb_midi_busy = 0;
60 int sb_dsp_busy = 0;
61
62 volatile int sb_irq_mode = IMODE_NONE;
63
64 static volatile int irq_ok = 0;
65
66 int sb_dsp_model = 1;
67 int sb_duplex_midi = 0;
68 static int my_dev = 0;
69
70 volatile int sb_intr_active = 0;
71
72 static int dsp_speed (int);
73 static int dsp_set_stereo (int mode);
74 int sb_dsp_command (unsigned char val);
75
76 #if !defined(EXCLUDE_MIDI) || !defined(EXCLUDE_AUDIO)
77
78
79
80 int
81 sb_dsp_command (unsigned char val)
82 {
83 int i, limit;
84
85 limit = GET_TIME () + 10;
86
87
88
89
90
91
92
93
94
95 for (i = 0; i < 500000 && GET_TIME () < limit; i++)
96 {
97 if ((INB (DSP_STATUS) & 0x80) == 0)
98 {
99 OUTB (val, DSP_COMMAND);
100 return 1;
101 }
102 }
103
104 printk ("SoundBlaster: DSP Command(%x) Timeout.\n", val);
105 printk ("IRQ conflict???\n");
106 return 0;
107 }
108
109 void
110 sbintr (int unit)
111 {
112 int status, data;
113
114 #ifndef EXCLUDE_SBPRO
115 if (sb16)
116 {
117 unsigned char src = sb_getmixer (IRQ_STAT);
118
119 #ifndef EXCLUDE_SB16
120 if (src & 3) sb16_dsp_interrupt(unit);
121
122 #ifndef EXCLUDE_MIDI
123 if (src & 4) sb16midiintr (unit);
124 #endif
125
126 #endif
127
128 if (!(src & 1))
129 return;
130 }
131 #endif
132
133 status = INB (DSP_DATA_AVAIL);
134
135 if (sb_intr_active)
136 switch (sb_irq_mode)
137 {
138 case IMODE_OUTPUT:
139 sb_intr_active = 0;
140 DMAbuf_outputintr (my_dev, 1);
141 break;
142
143 case IMODE_INPUT:
144 sb_intr_active = 0;
145 DMAbuf_inputintr (my_dev);
146
147 break;
148
149 case IMODE_INIT:
150 sb_intr_active = 0;
151 irq_ok = 1;
152 break;
153
154 case IMODE_MIDI:
155 printk ("+");
156 data = INB (DSP_READ);
157 printk ("%x", data);
158
159 break;
160
161 default:
162 printk ("SoundBlaster: Unexpected interrupt\n");
163 }
164 }
165
166 static int sb_irq_usecount = 0;
167
168 int
169 sb_get_irq(void)
170 {
171 int ok;
172
173 if (!sb_irq_usecount)
174 if ((ok=snd_set_irq_handler(sbc_irq, sbintr))<0) return ok;
175
176 sb_irq_usecount++;
177
178 return 0;
179 }
180
181 void
182 sb_free_irq(void)
183 {
184 if (!sb_irq_usecount) return;
185
186 sb_irq_usecount--;
187
188 if (!sb_irq_usecount) snd_release_irq(sbc_irq);
189 }
190
191 int
192 sb_reset_dsp (void)
193 {
194 int loopc;
195
196 OUTB (1, DSP_RESET);
197 tenmicrosec ();
198 OUTB (0, DSP_RESET);
199 tenmicrosec ();
200 tenmicrosec ();
201 tenmicrosec ();
202
203 for (loopc = 0; loopc < 1000 && !(INB (DSP_DATA_AVAIL) & 0x80); loopc++);
204
205
206 if (INB (DSP_READ) != 0xAA)
207 return 0;
208
209 return 1;
210 }
211
212 #endif
213
214 #ifndef EXCLUDE_AUDIO
215
216 static void
217 dsp_speaker (char state)
218 {
219 if (state)
220 sb_dsp_command (DSP_CMD_SPKON);
221 else
222 sb_dsp_command (DSP_CMD_SPKOFF);
223 }
224
225 static int
226 dsp_speed (int speed)
227 {
228 unsigned char tconst;
229 unsigned long flags;
230
231 if (speed < 4000)
232 speed = 4000;
233
234 if (speed > 44100)
235 speed = 44100;
236
237 if (sb_dsp_model == 1 && speed > 22050)
238 speed = 22050;
239
240
241
242 if (dsp_stereo && speed > 22050)
243 speed = 22050;
244
245
246 if ((speed > 22050) && sb_midi_busy)
247 {
248 printk ("SB Warning: High speed DSP not possible simultaneously with MIDI output\n");
249 speed = 22050;
250 }
251
252 if (dsp_stereo)
253 speed *= 2;
254
255
256
257 if (speed > 22050)
258 {
259 int tmp;
260
261 tconst = (unsigned char) ((65536 -
262 ((256000000+speed/2) / speed)) >> 8);
263 sb_dsp_highspeed = 1;
264
265 DISABLE_INTR (flags);
266 if (sb_dsp_command (0x40))
267 sb_dsp_command (tconst);
268 RESTORE_INTR (flags);
269
270 tmp = 65536 - (tconst << 8);
271 speed = (256000000+tmp/2) / tmp;
272 }
273 else
274 {
275 int tmp;
276
277 sb_dsp_highspeed = 0;
278 tconst = (256 - ((1000000+speed/2) / speed)) & 0xff;
279
280 DISABLE_INTR (flags);
281 if (sb_dsp_command (0x40))
282 sb_dsp_command (tconst);
283 RESTORE_INTR (flags);
284
285 tmp = 256 - tconst;
286 speed = (1000000+tmp/2) / tmp;
287 }
288
289 if (dsp_stereo)
290 speed /= 2;
291
292 dsp_current_speed = speed;
293 return speed;
294 }
295
296 static int
297 dsp_set_stereo (int mode)
298 {
299 dsp_stereo = 0;
300
301 #ifdef EXCLUDE_SBPRO
302 return 0;
303 #else
304 if (sb_dsp_model == 1 || sb16)
305 return 0;
306
307 if (mode && sb_midi_busy)
308 {
309 printk ("SB Warning: Stereo DSP not possible simultaneously with MIDI output\n");
310 return 0;
311 }
312
313 dsp_stereo = !!mode;
314 return dsp_stereo;
315 #endif
316 }
317
318 static void
319 sb_dsp_output_block (int dev, unsigned long buf, int count,
320 int intrflag, int restart_dma)
321 {
322 unsigned long flags;
323
324 if (!sb_irq_mode)
325 dsp_speaker (ON);
326
327 sb_irq_mode = IMODE_OUTPUT;
328 DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE);
329
330 if (sound_dsp_dmachan[dev] > 3)
331 count >>= 1;
332 count--;
333
334 if (sb_dsp_highspeed)
335 {
336 DISABLE_INTR (flags);
337 if (sb_dsp_command (0x48))
338 {
339 sb_dsp_command ((unsigned char)(count & 0xff));
340 sb_dsp_command ((unsigned char)((count >> 8) & 0xff));
341 sb_dsp_command (0x91);
342 }
343 else
344 printk ("SB Error: Unable to start (high speed) DAC\n");
345 RESTORE_INTR (flags);
346 }
347 else
348 {
349 DISABLE_INTR (flags);
350 if (sb_dsp_command (0x14))
351 {
352 sb_dsp_command ((unsigned char)(count & 0xff));
353 sb_dsp_command ((unsigned char)((count >> 8) & 0xff));
354 }
355 else
356 printk ("SB Error: Unable to start DAC\n");
357 RESTORE_INTR (flags);
358 }
359 sb_intr_active = 1;
360 }
361
362 static void
363 sb_dsp_start_input (int dev, unsigned long buf, int count, int intrflag,
364 int restart_dma)
365 {
366
367
368 unsigned long flags;
369
370 if (!sb_irq_mode)
371 dsp_speaker (OFF);
372
373 sb_irq_mode = IMODE_INPUT;
374 DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ);
375
376 if (sound_dsp_dmachan[dev] > 3)
377 count >>= 1;
378 count--;
379
380 if (sb_dsp_highspeed)
381 {
382 DISABLE_INTR (flags);
383 if (sb_dsp_command (0x48))
384 {
385 sb_dsp_command ((unsigned char)(count & 0xff));
386 sb_dsp_command ((unsigned char)((count >> 8) & 0xff));
387 sb_dsp_command (0x99);
388 }
389 else
390 printk ("SB Error: Unable to start (high speed) ADC\n");
391 RESTORE_INTR (flags);
392 }
393 else
394 {
395 DISABLE_INTR (flags);
396 if (sb_dsp_command (0x24))
397 {
398 sb_dsp_command ((unsigned char)(count & 0xff));
399 sb_dsp_command ((unsigned char)((count >> 8) & 0xff));
400 }
401 else
402 printk ("SB Error: Unable to start ADC\n");
403 RESTORE_INTR (flags);
404 }
405
406 sb_intr_active = 1;
407 }
408
409 static void
410 dsp_cleanup (void)
411 {
412 sb_intr_active = 0;
413 }
414
415 static int
416 sb_dsp_prepare_for_input (int dev, int bsize, int bcount)
417 {
418 dsp_cleanup ();
419 dsp_speaker (OFF);
420
421 if (major == 3)
422 {
423 if (dsp_stereo)
424 sb_dsp_command(0xa8);
425 else
426 sb_dsp_command(0xa0);
427
428 dsp_speed (dsp_current_speed);
429
430 }
431 return 0;
432 }
433
434 static int
435 sb_dsp_prepare_for_output (int dev, int bsize, int bcount)
436 {
437 dsp_cleanup ();
438 dsp_speaker (ON);
439
440 #ifndef EXCLUDE_SBPRO
441 if (major == 3)
442 {
443 sb_mixer_set_stereo(dsp_stereo);
444 dsp_speed (dsp_current_speed);
445
446 }
447 #endif
448 return 0;
449 }
450
451 static void
452 sb_dsp_halt_xfer (int dev)
453 {
454 }
455
456 static int
457 verify_irq (void)
458 {
459 #if 0
460 DEFINE_WAIT_QUEUE(testq, testf);
461
462 irq_ok = 0;
463
464 if (sb_get_irq () == -1)
465 {
466 printk ("*** SB Error: Irq %d already in use\n", sbc_irq);
467 return 0;
468 }
469
470
471 sb_irq_mode = IMODE_INIT;
472
473 sb_dsp_command (0xf2);
474
475 DO_SLEEP(testq, testf, HZ / 5);
476
477 sb_free_irq();
478
479 if (!irq_ok)
480 {
481 printk ("SB Warning: IRQ%d test not passed!", sbc_irq);
482 irq_ok = 1;
483 }
484 #else
485 irq_ok = 1;
486 #endif
487 return irq_ok;
488 }
489
490 static int
491 sb_dsp_open (int dev, int mode)
492 {
493 int retval;
494
495 if (!sb_dsp_ok)
496 {
497 printk ("SB Error: SoundBlaster board not installed\n");
498 return RET_ERROR (ENXIO);
499 }
500
501 if (sb_intr_active || (sb_midi_busy && sb_midi_mode == UART_MIDI))
502 {
503 printk ("SB: PCM not possible during MIDI input\n");
504 return RET_ERROR (EBUSY);
505 }
506
507 if (!irq_verified)
508 {
509 verify_irq();
510 irq_verified = 1;
511 }
512 else
513 if (!irq_ok)
514 printk("SB Warning: Incorrect IRQ setting %d\n",
515 sbc_irq);
516
517 retval = sb_get_irq ();
518 if (retval)
519 return retval;
520
521 if (!DMAbuf_open_dma (dev))
522 {
523 sb_free_irq ();
524 printk ("SB: DMA Busy\n");
525 return RET_ERROR (EBUSY);
526 }
527
528 sb_irq_mode = IMODE_NONE;
529
530 sb_dsp_busy = 1;
531
532 return 0;
533 }
534
535 static void
536 sb_dsp_close (int dev)
537 {
538 DMAbuf_close_dma (dev);
539 sb_free_irq ();
540 dsp_cleanup ();
541 dsp_speaker (OFF);
542 sb_dsp_busy = 0;
543 sb_dsp_highspeed = 0;
544 }
545
546 static int
547 sb_dsp_ioctl (int dev, unsigned int cmd, unsigned int arg, int local)
548 {
549 switch (cmd)
550 {
551 case SOUND_PCM_WRITE_RATE:
552 if (local)
553 return dsp_speed (arg);
554 return IOCTL_OUT (arg, dsp_speed (IOCTL_IN (arg)));
555 break;
556
557 case SOUND_PCM_READ_RATE:
558 if (local)
559 return dsp_current_speed;
560 return IOCTL_OUT (arg, dsp_current_speed);
561 break;
562
563 case SOUND_PCM_WRITE_CHANNELS:
564 if (local)
565 return dsp_set_stereo (arg - 1) + 1;
566 return IOCTL_OUT (arg, dsp_set_stereo (IOCTL_IN (arg) - 1) + 1);
567 break;
568
569 case SOUND_PCM_READ_CHANNELS:
570 if (local)
571 return dsp_stereo + 1;
572 return IOCTL_OUT (arg, dsp_stereo + 1);
573 break;
574
575 case SNDCTL_DSP_STEREO:
576 if (local)
577 return dsp_set_stereo (arg);
578 return IOCTL_OUT (arg, dsp_set_stereo (IOCTL_IN (arg)));
579 break;
580
581 case SOUND_PCM_WRITE_BITS:
582 case SOUND_PCM_READ_BITS:
583 if (local)
584 return 8;
585 return IOCTL_OUT (arg, 8);
586 break;
587
588 case SOUND_PCM_WRITE_FILTER:
589 case SOUND_PCM_READ_FILTER:
590 return RET_ERROR (EINVAL);
591 break;
592
593 default:
594 return RET_ERROR (EINVAL);
595 }
596
597 return RET_ERROR (EINVAL);
598 }
599
600 static void
601 sb_dsp_reset (int dev)
602 {
603 unsigned long flags;
604
605 DISABLE_INTR (flags);
606
607 sb_reset_dsp ();
608 dsp_cleanup ();
609
610 RESTORE_INTR (flags);
611 }
612
613 #endif
614
615 int
616 sb_dsp_detect (struct address_info *hw_config)
617 {
618 sbc_base = hw_config->io_base;
619 sbc_irq = hw_config->irq;
620
621 if (sb_dsp_ok)
622 return 0;
623
624 if (!sb_reset_dsp ())
625 return 0;
626
627 return 1;
628 }
629
630 #ifndef EXCLUDE_AUDIO
631 static struct audio_operations sb_dsp_operations =
632 {
633 "SoundBlaster",
634 sb_dsp_open,
635 sb_dsp_close,
636 sb_dsp_output_block,
637 sb_dsp_start_input,
638 sb_dsp_ioctl,
639 sb_dsp_prepare_for_input,
640 sb_dsp_prepare_for_output,
641 sb_dsp_reset,
642 sb_dsp_halt_xfer,
643 NULL,
644 NULL
645 };
646
647 #endif
648
649 long
650 sb_dsp_init (long mem_start, struct address_info *hw_config)
651 {
652 int i;
653
654 major = minor = 0;
655 sb_dsp_command (0xe1);
656
657 for (i = 1000; i; i--)
658 {
659 if (INB (DSP_DATA_AVAIL) & 0x80)
660 {
661 if (major == 0)
662 major = INB (DSP_READ);
663 else
664 {
665 minor = INB (DSP_READ);
666 break;
667 }
668 }
669 }
670
671 if (major == 2 || major == 3)
672 sb_duplex_midi = 1;
673
674 if (major == 4)
675 sb16 = 1;
676
677 if (major >= 3)
678 sb_dsp_model = 2;
679
680 #ifndef EXCLUDE_SBPRO
681 if (major >= 3)
682 sb_mixer_init(major);
683 #endif
684
685 #ifndef EXCLUDE_YM3812
686 if (major > 3 || (major == 3 && minor > 0))
687 {
688 enable_opl3_mode (OPL3_LEFT, OPL3_RIGHT, OPL3_BOTH);
689 }
690 #endif
691
692 if (major >= 3)
693 {
694 #ifndef SCO
695 sprintf (sb_dsp_operations.name, "SoundBlaster Pro %d.%d", major, minor);
696 #endif
697 }
698 else
699 {
700 #ifndef SCO
701 sprintf (sb_dsp_operations.name, "SoundBlaster %d.%d", major, minor);
702 #endif
703 }
704
705 printk (" <%s>", sb_dsp_operations.name);
706
707 #ifndef EXCLUDE_AUDIO
708 # if !defined(EXCLUDE_SB16) && !defined(EXCLUDE_SBPRO)
709 if (!sb16)
710 # endif
711 if (num_dspdevs < MAX_DSP_DEV)
712 {
713 dsp_devs[my_dev = num_dspdevs++] = &sb_dsp_operations;
714 sound_buffcounts[my_dev] = DSP_BUFFCOUNT;
715 sound_buffsizes[my_dev] = DSP_BUFFSIZE;
716 sound_dsp_dmachan[my_dev] = hw_config->dma;
717 sound_dma_automode[my_dev] = 0;
718 }
719 else
720 printk ("SB: Too many DSP devices available\n");
721 #endif
722
723 #ifndef EXCLUDE_MIDI
724 if (!midi_disabled && !sb16)
725
726 sb_midi_init(major);
727 #endif
728
729 sb_dsp_ok = 1;
730 return mem_start;
731 }
732
733 void
734 sb_dsp_disable_midi (void)
735 {
736 midi_disabled = 1;
737 }
738
739 #endif