Deleted Added
sdiff udiff text old ( 29651 ) new ( 30869 )
full compact
1/*
2 * sound/sb_dsp.c
3 *
4 * driver for the SoundBlaster and clones.
5 *
6 * Copyright 1997 Luigi Rizzo.
7 *
8 * Derived from files in the Voxware 3.5 distribution,
9 * Copyright by Hannu Savolainen 1994, under the same copyright
10 * conditions.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
26 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37/*
38 * use this as a template file for board-specific drivers.
39 * The next two lines (and the final #endif) are in all drivers:
40 */
41
42#include <i386/isa/snd/sound.h>
43#if NPCM > 0
44
45/*
46 * Begin with the board-specific include files...
47 */
48
49#define __SB_MIXER_C__ /* XXX warning... */
50#include <i386/isa/snd/sbcard.h>
51
52/*
53 * then prototypes of functions which go in the snddev_info
54 * (usually static, unless they are shared by other modules)...
55 */
56
57static int sb_probe(struct isa_device *dev);
58static int sb_attach(struct isa_device *dev);
59
60static d_open_t sb_dsp_open;
61static d_close_t sb_dsp_close;
62static d_ioctl_t sb_dsp_ioctl;
63static irq_proc_t sbintr;
64static snd_callback_t sb_callback;
65
66/*
67 * and prototypes for other private functions defined in this module.
68 */
69
70static void sb_dsp_init(snddev_info *d, struct isa_device *dev);
71static void sb_mix_init(snddev_info *d);
72static int sb_mixer_set(snddev_info *d, int dev, int value);
73static int dsp_speed(snddev_info *d);
74static void sb_mixer_reset(snddev_info *d);
75
76u_int sb_get_byte(int io_base);
77
78/*
79 * Then put here the descriptors for the various boards supported
80 * by this module, properly initialized.
81 */
82
83snddev_info sb_op_desc = {
84 "basic soundblaster",
85
86 SNDCARD_SB,
87 sb_probe,
88 sb_attach,
89
90 sb_dsp_open,
91 sb_dsp_close /* sb_close */,
92 NULL /* use generic sndread */,
93 NULL /* use generic sndwrite */,
94 sb_dsp_ioctl,
95 sndpoll,
96
97 sbintr,
98 sb_callback,
99
100 DSP_BUFFSIZE, /* bufsize */
101
102 AFMT_STEREO | AFMT_U8, /* audio format */
103
104} ;
105
106/*
107 * Then the file continues with the body of all functions
108 * directly referenced in the descriptor.
109 */
110
111/*
112 * the probe routine for the SoundBlaster only consists in
113 * resetting the dsp and testing if it is there.
114 * Version detection etc. will be done at attach time.
115 *
116 * Remember, ISA probe routines are supposed to return the
117 * size of io space used.
118 */
119
120static int
121sb_probe(struct isa_device *dev)
122{
123 bzero(&pcm_info[dev->id_unit], sizeof(pcm_info[dev->id_unit]) );
124 if (dev->id_iobase == -1) {
125 dev->id_iobase = 0x220;
126 printf("sb_probe: no address supplied, try defaults (0x220,0x240)\n");
127 if (snd_conflict(dev->id_iobase))
128 dev->id_iobase = 0x240;
129 }
130 if (snd_conflict(dev->id_iobase))
131 return 0 ;
132
133 if (sb_reset_dsp(dev->id_iobase))
134 return 16 ; /* the SB uses 16 registers... */
135 else
136 return 0;
137}
138
139static int
140sb_attach(struct isa_device *dev)
141{
142 snddev_info *d = &pcm_info[dev->id_unit] ;
143
144 dev->id_alive = 16 ; /* number of io ports */
145 /* should be already set but just in case... */
146 sb_dsp_init(d, dev);
147 return 0 ;
148}
149
150/*
151 * here are the main routines from the switches.
152 */
153
154static int
155sb_dsp_open(dev_t dev, int flags, int mode, struct proc * p)
156{
157 snddev_info *d;
158 int unit ;
159
160 dev = minor(dev);
161 unit = dev >> 4 ;
162 d = &pcm_info[unit] ;
163
164 DEB(printf("<%s>%d : open\n", d->name, unit));
165
166 if (d->flags & SND_F_BUSY) {
167 printf("<%s>%d open: device busy\n", d->name, unit);
168 return EBUSY ;
169 }
170
171 d->wsel.si_pid = 0;
172 d->wsel.si_flags = 0;
173
174 d->rsel.si_pid = 0;
175 d->rsel.si_flags = 0;
176
177 d->flags = 0 ;
178 d->bd_flags &= ~BD_F_HISPEED ;
179
180 switch ( dev & 0xf ) {
181 case SND_DEV_DSP16 :
182 if ((d->audio_fmt & AFMT_S16_LE) == 0) {
183 printf("sorry, 16-bit not supported on SB %d.%02d\n",
184 (d->bd_id >>8) & 0xff, d->bd_id & 0xff);
185 return ENXIO;
186 }
187 d->play_fmt = d->rec_fmt = AFMT_S16_LE ;
188 break;
189 case SND_DEV_AUDIO :
190 d->play_fmt = d->rec_fmt = AFMT_MU_LAW ;
191 break ;
192 case SND_DEV_DSP :
193 d->play_fmt = d->rec_fmt = AFMT_U8 ;
194 break ;
195 }
196
197 d->flags |= SND_F_BUSY ;
198 d->play_speed = d->rec_speed = DSP_DEFAULT_SPEED ;
199
200 if (flags & O_NONBLOCK)
201 d->flags |= SND_F_NBIO ;
202
203 sb_reset_dsp(d->io_base);
204 ask_init(d);
205
206 return 0;
207}
208
209static int
210sb_dsp_close(dev_t dev, int flags, int mode, struct proc * p)
211{
212 int unit;
213 snddev_info *d;
214 u_long s;
215
216 dev = minor(dev);
217 unit = dev >> 4 ;
218 d = &pcm_info[unit] ;
219
220 s = spltty();
221 d->flags |= SND_F_CLOSING ;
222 splx(s);
223 snd_flush(d);
224
225 sb_cmd(d->io_base, DSP_CMD_SPKOFF ); /* XXX useless ? */
226
227 d->flags = 0 ;
228 return 0 ;
229}
230
231static int
232sb_dsp_ioctl(dev_t dev, int cmd, caddr_t arg, int mode, struct proc * p)
233{
234 int unit;
235 snddev_info *d;
236
237 dev = minor(dev);
238 unit = dev >> 4 ;
239 d = &pcm_info[unit] ;
240
241 /*
242 * handle mixer calls first. Reads are in the default handler,
243 * so do not bother about them.
244 */
245 if ( (cmd & MIXER_WRITE(0)) == MIXER_WRITE(0) )
246 return sb_mixer_set(d, cmd & 0xff, *(int *)arg) ;
247
248 /*
249 * for the remaining functions, use the default handler.
250 */
251
252 return ENOSYS ;
253}
254
255static void
256sbintr(int unit)
257{
258 snddev_info *d = &pcm_info[unit];
259 int reason = 3, c=1, io_base = d->io_base;
260
261 DEB(printf("got sbintr for unit %d, flags 0x%08lx\n", unit, d->flags));
262
263 /*
264 * SB < 4.0 is half duplex and has only 1 bit for int source,
265 * so we fake it. SB 4.x (SB16) has the int source in a separate
266 * register.
267 */
268again:
269 if (d->bd_flags & BD_F_SB16) {
270 c = sb_getmixer(io_base, IRQ_STAT);
271 /* this tells us if the source is 8-bit or 16-bit dma. We
272 * have to check the io channel to map it to read or write...
273 */
274 reason = 0 ;
275 if ( c & 1 ) { /* 8-bit dma */
276 if (d->dma1 < 4)
277 reason |= 1;
278 if (d->dma2 < 4)
279 reason |= 2;
280 }
281 if ( c & 2 ) { /* 16-bit dma */
282 if (d->dma1 >= 4)
283 reason |= 1;
284 if (d->dma2 >= 4)
285 reason |= 2;
286 }
287 }
288 /* XXX previous location of ack... */
289 DEB(printf("sbintr, flags 0x%08lx reason %d\n", d->flags, reason));
290 if ( d->dbuf_out.dl && (reason & 1) )
291 dsp_wrintr(d);
292 if ( d->dbuf_in.dl && (reason & 2) )
293 dsp_rdintr(d);
294
295 if ( c & 2 )
296 inb(DSP_DATA_AVL16); /* 16-bit int ack */
297 if (c & 1)
298 inb(DSP_DATA_AVAIL); /* 8-bit int ack */
299
300 /*
301 * the sb16 might have multiple sources etc.
302 */
303 if (d->bd_flags & BD_F_SB16 && (c & 3) )
304 goto again;
305}
306
307/*
308 * device-specific function called back from the dma module.
309 * The reason of the callback is the second argument.
310 * NOTE: during operations, some ioctl can be done to change
311 * settings (e.g. speed, channels, format), and the default
312 * ioctl handler will just record the change and set the
313 * flag SND_F_INIT. The callback routine is in charge of applying
314 * the changes at the next convenient time (typically, at the
315 * start of operations). For full duplex devices, in some cases the
316 * init requires both channels to be idle.
317 */
318static int
319sb_callback(snddev_info *d, int reason)
320{
321 int rd = reason & SND_CB_RD ;
322 int l = (rd) ? d->dbuf_in.dl : d->dbuf_out.dl ;
323
324 switch (reason & SND_CB_REASON_MASK) {
325 case SND_CB_INIT : /* called with int enabled and no pending io */
326 dsp_speed(d);
327 snd_set_blocksize(d);
328 if (d->play_fmt & AFMT_MU_LAW)
329 d->flags |= SND_F_XLAT8 ;
330 else
331 d->flags &= ~SND_F_XLAT8 ;
332 reset_dbuf(& (d->dbuf_in), SND_CHAN_RD );
333 reset_dbuf(& (d->dbuf_out), SND_CHAN_WR );
334 return 1;
335 break;
336
337 case SND_CB_START : /* called with int disabled */
338 if (d->bd_flags & BD_F_SB16) {
339 /* the SB16 can do full duplex using one 16-bit channel
340 * and one 8-bit channel. It needs to be programmed to
341 * use split format though.
342 * We use the following algorithm:
343 * 1. check which direction(s) are active;
344 * 2. check if we should swap dma channels
345 * 3. check if we can do the swap.
346 */
347 int swap = 1 ; /* default... */
348
349 if (rd) {
350 if (d->flags & SND_F_WRITING || d->dbuf_out.dl)
351 swap = 0;
352 if (d->rec_fmt == AFMT_S16_LE && d->dma2 >=4)
353 swap = 0;
354 if (d->rec_fmt != AFMT_S16_LE && d->dma2 <4)
355 swap = 0;
356 } else {
357 if (d->flags & SND_F_READING || d->dbuf_in.dl)
358 swap = 0;
359 if (d->play_fmt == AFMT_S16_LE && d->dma1 >=4)
360 swap = 0;
361 if (d->play_fmt != AFMT_S16_LE && d->dma1 <4)
362 swap = 0;
363 }
364
365 if (swap) {
366 int c = d->dma2 ;
367 d->dma2 = d->dma1;
368 d->dma1 = c ;
369 reset_dbuf(& (d->dbuf_in), SND_CHAN_RD );
370 reset_dbuf(& (d->dbuf_out), SND_CHAN_WR );
371 DEB(printf("START dma chan: play %d, rec %d\n",
372 d->dma1, d->dma2));
373 }
374 }
375 if (!rd)
376 sb_cmd(d->io_base, DSP_CMD_SPKON);
377
378 if (d->bd_flags & BD_F_SB16) {
379 u_char c, c1 ;
380
381 if (rd) {
382 c = ((d->dma2 > 3) ? DSP_DMA16 : DSP_DMA8) |
383 DSP_F16_AUTO |
384 DSP_F16_FIFO_ON | DSP_F16_ADC ;
385 c1 = (d->rec_fmt == AFMT_U8) ? 0 : DSP_F16_SIGNED ;
386 if (d->rec_fmt == AFMT_MU_LAW) c1 = 0 ;
387 if (d->rec_fmt == AFMT_S16_LE)
388 l /= 2 ;
389 } else {
390 c = ((d->dma1 > 3) ? DSP_DMA16 : DSP_DMA8) |
391 DSP_F16_AUTO |
392 DSP_F16_FIFO_ON | DSP_F16_DAC ;
393 c1 = (d->play_fmt == AFMT_U8) ? 0 : DSP_F16_SIGNED ;
394 if (d->play_fmt == AFMT_MU_LAW) c1 = 0 ;
395 if (d->play_fmt == AFMT_S16_LE)
396 l /= 2 ;
397 }
398
399 if (d->flags & SND_F_STEREO)
400 c1 |= DSP_F16_STEREO ;
401
402 sb_cmd(d->io_base, c );
403 sb_cmd3(d->io_base, c1 , l - 1) ;
404 } else {
405 /* code for the SB2 and SB3 */
406 u_char c ;
407 if (d->bd_flags & BD_F_HISPEED)
408 c = (rd) ? DSP_CMD_HSADC_AUTO : DSP_CMD_HSDAC_AUTO ;
409 else
410 c = (rd) ? DSP_CMD_ADC8_AUTO : DSP_CMD_DAC8_AUTO ;
411 sb_cmd3(d->io_base, c , l - 1) ;
412 }
413 break;
414
415 case SND_CB_ABORT : /* XXX */
416 case SND_CB_STOP :
417 {
418 int cmd = DSP_CMD_DMAPAUSE_8 ; /* default: halt 8 bit chan */
419 if (d->bd_flags & BD_F_SB16) {
420 if ( (rd && d->dbuf_in.chan>4) || (!rd && d->dbuf_out.chan>4) )
421 cmd = DSP_CMD_DMAPAUSE_16 ;
422 }
423 if (d->bd_flags & BD_F_HISPEED) {
424 sb_reset_dsp(d->io_base);
425 d->flags |= SND_F_INIT ;
426 } else {
427 sb_cmd(d->io_base, cmd); /* pause dma. */
428 /*
429 * This seems to have the side effect of blocking the other
430 * side as well so I have to re-enable it :(
431 */
432 if ( (rd && d->dbuf_out.dl) ||
433 (!rd && d->dbuf_in.dl) )
434 sb_cmd(d->io_base, cmd == DSP_CMD_DMAPAUSE_8 ?
435 0xd6 : 0xd4); /* continue other dma */
436 }
437 }
438 DEB( sb_cmd(d->io_base, DSP_CMD_SPKOFF) ); /* speaker off */
439 break ;
440
441 }
442 return 0 ;
443}
444
445/*
446 * The second part of the file contains all functions specific to
447 * the board and (usually) not exported to other modules.
448 */
449
450int
451sb_reset_dsp(int io_base)
452{
453 int loopc;
454
455 outb(DSP_RESET, 1);
456 DELAY(100);
457 outb(DSP_RESET, 0);
458 for (loopc = 0; loopc<100 && !(inb(DSP_DATA_AVAIL) & 0x80); loopc++)
459 DELAY(30);
460
461 if (inb(DSP_READ) != 0xAA) {
462 DEB(printf("sb_reset_dsp 0x%x failed\n", io_base));
463 return 0; /* Sorry */
464 }
465 return 1;
466}
467
468/*
469 * only used in sb_attach from here.
470 */
471
472static void
473sb_dsp_init(snddev_info *d, struct isa_device *dev)
474{
475 int i, x;
476 char *fmt = NULL ;
477 int io_base = dev->id_iobase ;
478
479 d->bd_id = 0 ;
480
481 sb_reset_dsp(io_base);
482 sb_cmd(io_base, DSP_CMD_GETVER); /* Get version */
483
484 for (i = 10000; i; i--) { /* perhaps wait longer on a fast machine ? */
485 if (inb(DSP_DATA_AVAIL) & 0x80) { /* wait for Data Ready */
486 if ( (d->bd_id & 0xff00) == 0)
487 d->bd_id = inb(DSP_READ) << 8; /* major */
488 else {
489 d->bd_id |= inb(DSP_READ); /* minor */
490 break;
491 }
492 } else
493 DELAY(20);
494 }
495
496 /*
497 * now do various initializations depending on board id.
498 */
499
500 fmt = "SoundBlaster %d.%d" ; /* default */
501
502 switch ( d->bd_id >> 8 ) {
503 case 0 :
504 printf("\n\nFailed to get SB version (%x) - possible I/O conflict\n\n",
505 inb(DSP_DATA_AVAIL));
506 d->bd_id = 0x100;
507 case 1 : /* old sound blaster has nothing... */
508 break ;
509
510 case 2 :
511 d->dma2 = d->dma1 ; /* half duplex */
512 d->bd_flags |= BD_F_DUP_MIDI ;
513
514 if (d->bd_id == 0x200)
515 break ; /* no mixer on the 2.0 */
516 d->bd_flags &= ~BD_F_MIX_MASK ;
517 d->bd_flags |= BD_F_MIX_CT1335 ;
518
519 break ;
520 case 4 :
521 fmt = "SoundBlaster 16 %d.%d";
522 d->audio_fmt |= AFMT_FULLDUPLEX | AFMT_WEIRD | AFMT_S8 | AFMT_S16_LE;
523 d->bd_flags |= BD_F_SB16;
524 d->bd_flags &= ~BD_F_MIX_MASK ;
525 d->bd_flags |= BD_F_MIX_CT1745 ;
526
527 /* soft irq/dma configuration */
528 x = -1 ;
529 if (d->irq == 5) x = 2;
530 else if (d->irq == 7) x = 4;
531 else if (d->irq == 9) x = 1;
532 else if (d->irq == 10) x = 8;
533 if (x == -1)
534 printf("<%s>%d: bad irq %d (only 5,7,9,10 allowed)\n",
535 d->name, dev->id_unit, d->irq);
536 else
537 sb_setmixer(io_base, IRQ_NR, x);
538
539 sb_setmixer(io_base, DMA_NR, (1 << d->dma1) | (1 << d->dma2));
540 break ;
541
542 case 3 :
543 d->dma2 = d->dma1 ; /* half duplex */
544 fmt = "SoundBlaster Pro %d.%d";
545 d->bd_flags |= BD_F_DUP_MIDI ;
546 d->bd_flags &= ~BD_F_MIX_MASK ;
547 d->bd_flags |= BD_F_MIX_CT1345 ;
548 if (d->bd_id == 0x301) {
549 int ess_major = 0, ess_minor = 0;
550
551 /*
552 * Try to detect ESS chips.
553 */
554
555 sb_cmd(io_base, DSP_CMD_GETID); /* Return ident. bytes. */
556
557 for (i = 1000; i; i--) {
558 if (inb(DSP_DATA_AVAIL) & 0x80) { /* wait for Data Ready */
559 if (ess_major == 0)
560 ess_major = inb(DSP_READ);
561 else {
562 ess_minor = inb(DSP_READ);
563 break;
564 }
565 }
566 }
567
568 if (ess_major == 0x48 && (ess_minor & 0xf0) == 0x80)
569 printf("Hmm... Could this be an ESS488 based card (rev %d)\n",
570 ess_minor & 0x0f);
571 else if (ess_major == 0x68 && (ess_minor & 0xf0) == 0x80)
572 printf("Hmm... Could this be an ESS688 based card (rev %d)\n",
573 ess_minor & 0x0f);
574 }
575
576 if (d->bd_flags & BD_F_JAZZ16) {
577 if (d->bd_flags & BD_F_JAZZ16_2)
578 fmt = "SoundMan Wave %d.%d";
579 else
580 fmt = "MV Jazz16 %d.%d";
581 d->audio_fmt |= AFMT_S16_LE; /* 16 bits */
582 }
583 }
584
585 sprintf(d->name, fmt, (d->bd_id >> 8) &0xff, d->bd_id & 0xff);
586
587 sb_mix_init(d);
588}
589
590static void
591sb_mix_init(snddev_info *d)
592{
593 switch (d->bd_flags & BD_F_MIX_MASK) {
594 case BD_F_MIX_CT1345 : /* SB 3.0 has 1345 mixer */
595
596 d->mix_devs = SBPRO_MIXER_DEVICES ;
597 d->mix_rec_devs = SBPRO_RECORDING_DEVICES ;
598 d->mix_recsrc = SOUND_MASK_MIC ;
599
600 sb_setmixer(d->io_base, 0, 1 ); /* reset mixer */
601 sb_setmixer(d->io_base, MIC_VOL , 0x6 ); /* mic volume max */
602 sb_setmixer(d->io_base, RECORD_SRC , 0x0 ); /* mic source */
603 sb_setmixer(d->io_base, FM_VOL , 0x0 ); /* no midi */
604 break ;
605
606 case BD_F_MIX_CT1745 : /* SB16 mixer ... */
607
608 d->mix_devs = SB16_MIXER_DEVICES ;
609 d->mix_rec_devs = SB16_RECORDING_DEVICES ;
610 d->mix_recsrc = SOUND_MASK_MIC ;
611 }
612 sb_mixer_reset(d);
613}
614
615/*
616 * Common code for the midi and pcm functions
617 */
618
619int
620sb_cmd(int io_base, u_char val)
621{
622 int i;
623
624 for (i = 0; i < 1000 ; i++) {
625 if ((inb(DSP_STATUS) & 0x80) == 0) {
626 outb(DSP_COMMAND, val);
627 return 1;
628 }
629 if (i > 10)
630 DELAY (i > 100 ? 1000 : 10 );
631 }
632
633 printf("SoundBlaster: DSP Command(0x%02x) timeout. IRQ conflict ?\n", val);
634 return 0;
635}
636
637int
638sb_cmd3(int io_base, u_char cmd, int val)
639{
640 if (sb_cmd(io_base, cmd)) {
641 sb_cmd(io_base, val & 0xff );
642 sb_cmd(io_base, (val>>8) & 0xff );
643 return 1 ;
644 } else
645 return 0;
646}
647
648int
649sb_cmd2(int io_base, u_char cmd, int val)
650{
651 if (sb_cmd(io_base, cmd)) {
652 sb_cmd(io_base, val & 0xff );
653 return 1 ;
654 } else
655 return 0;
656}
657
658void
659sb_setmixer(int io_base, u_int port, u_int value)
660{
661 u_long flags;
662
663 flags = spltty();
664 outb(MIXER_ADDR, (u_char) (port & 0xff)); /* Select register */
665 DELAY(10);
666 outb(MIXER_DATA, (u_char) (value & 0xff));
667 DELAY(10);
668 splx(flags);
669}
670
671u_int
672sb_get_byte(int io_base)
673{
674 int i;
675
676 for (i = 1000; i; i--)
677 if (inb(DSP_DATA_AVAIL) & 0x80)
678 return inb(DSP_READ);
679 else
680 DELAY(20);
681 return 0xffff;
682}
683
684int
685sb_getmixer(int io_base, u_int port)
686{
687 int val;
688 u_long flags;
689
690 flags = spltty();
691 outb(MIXER_ADDR, (u_char) (port & 0xff)); /* Select register */
692 DELAY(10);
693 val = inb(MIXER_DATA);
694 DELAY(10);
695 splx(flags);
696
697 return val;
698}
699
700
701/*
702 * various utility functions for the DSP
703 */
704
705/*
706 * dsp_speed updates the speed setting from the descriptor. make sure
707 * it is called at spltty().
708 * Besides, it takes care of stereo setting.
709 */
710static int
711dsp_speed(snddev_info *d)
712{
713 u_char tconst;
714 u_long flags;
715 int max_speed = 44100, speed = d->play_speed ;
716
717 if (d->bd_flags & BD_F_SB16) {
718 RANGE (speed, 5000, 45000);
719 d->play_speed = d->rec_speed = speed ;
720 sb_cmd(d->io_base, 0x41);
721 sb_cmd(d->io_base, d->play_speed >> 8 );
722 sb_cmd(d->io_base, d->play_speed & 0xff );
723 sb_cmd(d->io_base, 0x42);
724 sb_cmd(d->io_base, d->rec_speed >> 8 );
725 sb_cmd(d->io_base, d->rec_speed & 0xff );
726 return speed ;
727 }
728 /*
729 * only some models can do stereo, and only if not
730 * simultaneously using midi.
731 */
732 if ( (d->bd_id & 0xff00) < 0x300 || d->bd_flags & BD_F_MIDIBUSY)
733 d->flags &= ~SND_F_STEREO;
734
735 /*
736 * here enforce speed limitations.
737 */
738 if (d->bd_id <= 0x200)
739 max_speed = 22050; /* max 22050 on SB 1.X */
740
741 /*
742 * SB models earlier than SB Pro have low limit for the
743 * input rate. Note that this is only for input, but since
744 * we do not support separate values for rec & play....
745 */
746 if (d->bd_id <= 0x200)
747 max_speed = 13000;
748 else if (d->bd_id < 0x300)
749 max_speed = 15000;
750
751 RANGE(speed, 4000, max_speed);
752
753 /*
754 * Logitech SoundMan Games and Jazz16 cards can support 44.1kHz
755 * stereo
756 */
757#if !defined (SM_GAMES)
758 /*
759 * Max. stereo speed is 22050
760 */
761 if (d->flags & SND_F_STEREO && speed > 22050 && !(d->bd_flags & BD_F_JAZZ16))
762 speed = 22050;
763#endif
764
765 if (d->flags & SND_F_STEREO)
766 speed *= 2;
767
768 /*
769 * Now the speed should be valid. Compute the value to be
770 * programmed into the board.
771 *
772 * XXX stereo init is still missing...
773 */
774
775 if (speed > 22050) { /* High speed mode on 2.01/3.xx */
776 int tmp;
777
778 tconst = (u_char) ((65536 - ((256000000 + speed / 2) / speed)) >> 8) ;
779 d->bd_flags |= BD_F_HISPEED ;
780
781 flags = spltty();
782 sb_cmd2(d->io_base, DSP_CMD_TCONST, tconst);
783 splx(flags);
784
785 tmp = 65536 - (tconst << 8);
786 speed = (256000000 + tmp / 2) / tmp;
787 } else {
788 int tmp;
789
790 d->bd_flags &= ~BD_F_HISPEED ;
791 tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
792
793 flags = spltty();
794 sb_cmd2(d->io_base, DSP_CMD_TCONST, tconst);
795 splx(flags);
796
797 tmp = 256 - tconst;
798 speed = (1000000 + tmp / 2) / tmp;
799 }
800
801 if (d->flags & SND_F_STEREO)
802 speed /= 2;
803
804 d->play_speed = d->rec_speed = speed;
805 return speed;
806}
807
808/*
809 * mixer support, originally in sb_mixer.c
810 */
811
812static void
813sb_set_recsrc(snddev_info *d, int mask)
814{
815 u_char recdev ;
816
817 mask &= d->mix_rec_devs;
818 switch (d->bd_flags & BD_F_MIX_MASK) {
819 case BD_F_MIX_CT1345 :
820 if (mask == SOUND_MASK_LINE)
821 recdev = 6 ;
822 else if (mask == SOUND_MASK_CD)
823 recdev = 2 ;
824 else { /* default: mic */
825 mask = SOUND_MASK_MIC ;
826 recdev = 0 ;
827 }
828 sb_setmixer(d->io_base, RECORD_SRC,
829 recdev | (sb_getmixer(d->io_base, RECORD_SRC) & ~7 ));
830 break ;
831 case BD_F_MIX_CT1745 : /* sb16 */
832 if (mask == 0)
833 mask = SOUND_MASK_MIC ; /* XXX For compatibility. Bug ? */
834 recdev = 0 ;
835 if (mask & SOUND_MASK_MIC)
836 recdev |= 1 ;
837 if (mask & SOUND_MASK_CD)
838 recdev |= 6 ; /* l+r cd */
839 if (mask & SOUND_MASK_LINE)
840 recdev |= 0x18 ; /* l+r line */
841 if (mask & SOUND_MASK_SYNTH)
842 recdev |= 0x60 ; /* l+r midi */
843 sb_setmixer(d->io_base, SB16_IMASK_L, recdev);
844 sb_setmixer(d->io_base, SB16_IMASK_R, recdev);
845 /*
846 * since the same volume controls apply to the input and
847 * output sections, the best approach to have a consistent
848 * behaviour among cards would be to disable the output path
849 * on devices which are used to record.
850 * However, since users like to have feedback, we only disable
851 * the mike -- permanently.
852 */
853 sb_setmixer(d->io_base, SB16_OMASK, 0x1f & ~1);
854 break ;
855 }
856 d->mix_recsrc = mask;
857}
858
859static void
860sb_mixer_reset(snddev_info *d)
861{
862 int i;
863
864 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
865 sb_mixer_set(d, i, levels[i]);
866 if (d->bd_flags & BD_F_SB16) {
867 sb_setmixer(d->io_base, 0x3c, 0x1f); /* make all output active */
868 sb_setmixer(d->io_base, 0x3d, 0); /* make all inputs-l off */
869 sb_setmixer(d->io_base, 0x3e, 0); /* make all inputs-r off */
870 }
871 sb_set_recsrc(d, SOUND_MASK_MIC);
872}
873
874static int
875sb_mixer_set(snddev_info *d, int dev, int value)
876{
877 int left = value & 0x000000ff;
878 int right = (value & 0x0000ff00) >> 8;
879 int regoffs;
880 u_char val;
881 mixer_tab *iomap;
882
883#ifdef JAZZ16
884 if (d->bd_flags & BD_F_JAZZ16 && d->bd_flags & BD_F_JAZZ16_2)
885 return smw_mixer_set(dev, value);
886#endif
887
888 if (dev == SOUND_MIXER_RECSRC) {
889 sb_set_recsrc(d, value);
890 return 0 ;
891 }
892 if (left > 100)
893 left = 100;
894 if (right > 100)
895 right = 100;
896
897 if (dev > 31)
898 return EINVAL ;
899
900 if (!(d->mix_devs & (1 << dev))) /* Not supported */
901 return EINVAL;
902
903 switch ( d->bd_flags & BD_F_MIX_MASK ) {
904 default:
905 /* mixer unknown, fail... */
906 return EINVAL ;/* XXX change this */
907 case BD_F_MIX_CT1345 :
908 iomap = &sbpro_mix ;
909 break;
910 case BD_F_MIX_CT1745 :
911 iomap = &sb16_mix ;
912 break;
913 /* XXX how about the SG NX Pro, iomap = sgnxpro_mix */
914 }
915 regoffs = (*iomap)[dev][LEFT_CHN].regno;
916 if (regoffs == 0)
917 return EINVAL;
918
919 val = sb_getmixer(d->io_base, regoffs);
920
921 change_bits(iomap, &val, dev, LEFT_CHN, left);
922
923 d->mix_levels[dev] = left | (left << 8);
924
925 if ((*iomap)[dev][RIGHT_CHN].regno != regoffs) { /* Change register */
926 sb_setmixer(d->io_base, regoffs, val); /* Save the old one */
927 regoffs = (*iomap)[dev][RIGHT_CHN].regno;
928
929 if (regoffs == 0)
930 return 0 ; /* Just left channel present */
931
932 val = sb_getmixer(d->io_base, regoffs); /* Read the new one */
933 }
934 change_bits(iomap, &val, dev, RIGHT_CHN, right);
935
936 sb_setmixer(d->io_base, regoffs, val);
937
938 d->mix_levels[dev] = left | (right << 8);
939 return 0 ; /* ok */
940}
941
942/*
943 * now support for some PnP boards.
944 */
945
946#if NPNP > 0
947static char *opti925_probe(u_long csn, u_long vend_id);
948static void opti925_attach(u_long csn, u_long vend_id, char *name,
949 struct isa_device *dev);
950
951static struct pnp_device opti925 = {
952 "opti925",
953 opti925_probe,
954 opti925_attach,
955 &nsnd, /* use this for all sound cards */
956 &tty_imask /* imask */
957};
958DATA_SET (pnpdevice_set, opti925);
959
960static char *
961opti925_probe(u_long csn, u_long vend_id)
962{
963 if (vend_id == 0x2509143e) {
964 struct pnp_cinfo d ;
965 read_pnp_parms ( &d , 1 ) ;
966 if (d.enable == 0) {
967 printf("This is an OPTi925, but LDN 1 is disabled\n");
968 return NULL;
969 }
970 return "OPTi925" ;
971 }
972 return NULL ;
973}
974
975static void
976opti925_attach(u_long csn, u_long vend_id, char *name,
977 struct isa_device *dev)
978{
979 struct pnp_cinfo d ;
980 snddev_info tmp_d ; /* patched copy of the basic snddev_info */
981 int the_irq = 0 ;
982
983 tmp_d = sb_op_desc;
984 snddev_last_probed = &tmp_d;
985
986 read_pnp_parms ( &d , 3 ); /* disable LDN 3 */
987 the_irq = d.irq[0];
988 d.port[0] = 0 ;
989 d.enable = 0 ;
990 write_pnp_parms ( &d , 3 );
991
992 read_pnp_parms ( &d , 2 ); /* disable LDN 2 */
993 d.port[0] = 0 ;
994 d.enable = 0 ;
995 write_pnp_parms ( &d , 2 );
996
997 read_pnp_parms ( &d , 1 ) ;
998 d.irq[0] = the_irq ;
999 dev->id_iobase = d.port[0];
1000 write_pnp_parms ( &d , 1 );
1001 enable_pnp_card();
1002
1003 tmp_d.conf_base = d.port[3];
1004
1005 dev->id_drq = d.drq[0] ; /* primary dma */
1006 dev->id_irq = (1 << d.irq[0] ) ;
1007 dev->id_intr = pcmintr ;
1008 dev->id_flags = DV_F_DUAL_DMA | (d.drq[1] ) ;
1009
1010 snddev_last_probed->probe(dev); /* not really necessary but doesn't harm */
1011
1012 pcmattach(dev);
1013
1014}
1015
1016/*
1017 * A driver for some SB16pnp and compatibles...
1018 *
1019 * Avance Asound 100 -- 0x01009305
1020 * xxx -- 0x2b008c0e
1021 *
1022 */
1023
1024static char *sb16pnp_probe(u_long csn, u_long vend_id);
1025static void sb16pnp_attach(u_long csn, u_long vend_id, char *name,
1026 struct isa_device *dev);
1027
1028static struct pnp_device sb16pnp = {
1029 "SB16pnp",
1030 sb16pnp_probe,
1031 sb16pnp_attach,
1032 &nsnd, /* use this for all sound cards */
1033 &tty_imask /* imask */
1034};
1035DATA_SET (pnpdevice_set, sb16pnp);
1036
1037static char *
1038sb16pnp_probe(u_long csn, u_long vend_id)
1039{
1040 char *s = NULL ;
1041
1042 /*
1043 * The SB16/AWExx cards seem to differ in the fourth byte of
1044 * the vendor id, so I have just masked it for the time being...
1045 * Reported values are:
1046 * SB16 Value PnP: 0x2b008c0e
1047 * SB AWE64 PnP: 0x39008c0e 0x9d008c0e 0xc3008c0e
1048 */
1049 if ( (vend_id & 0xffffff) == (0x9d008c0e & 0xffffff) )
1050 s = "SB16 PnP";
1051 else if (vend_id == 0x01009305)
1052 s = "Avance Asound 100" ;
1053 if (s) {
1054 struct pnp_cinfo d;
1055 read_pnp_parms(&d, 0);
1056 if (d.enable == 0) {
1057 printf("This is a %s, but LDN 0 is disabled\n", s);
1058 return NULL ;
1059 }
1060 return s ;
1061 }
1062 return NULL ;
1063}
1064
1065static void
1066sb16pnp_attach(u_long csn, u_long vend_id, char *name,
1067 struct isa_device *dev)
1068{
1069 struct pnp_cinfo d ;
1070 snddev_info tmp_d ; /* patched copy of the basic snddev_info */
1071
1072 tmp_d = sb_op_desc;
1073 snddev_last_probed = &tmp_d;
1074
1075 read_pnp_parms ( &d , 0 ) ;
1076 d.port[1] = 0 ; /* only the first address is used */
1077 dev->id_iobase = d.port[0];
1078 write_pnp_parms ( &d , 0 );
1079 enable_pnp_card();
1080
1081 dev->id_drq = d.drq[0] ; /* primary dma */
1082 dev->id_irq = (1 << d.irq[0] ) ;
1083 dev->id_intr = pcmintr ;
1084 dev->id_flags = DV_F_DUAL_DMA | (d.drq[1] ) ;
1085
1086 pcm_info[dev->id_unit] = tmp_d;
1087 snddev_last_probed->probe(dev); /* not really necessary but doesn't harm */
1088
1089 pcmattach(dev);
1090}
1091#endif /* NPNP */
1092
1093#endif