Linux 2.6.19-rc1
[linux-2.6/next.git] / sound / oss / sb_audio.c
blob733b014ec7d1637a50954b46ff64b6bd662241e3
1 /*
2 * sound/oss/sb_audio.c
4 * Audio routines for Sound Blaster compatible cards.
7 * Copyright (C) by Hannu Savolainen 1993-1997
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
11 * for more info.
13 * Changes
14 * Alan Cox : Formatting and clean ups
16 * Status
17 * Mostly working. Weird uart bug causing irq storms
19 * Daniel J. Rodriksson: Changes to make sb16 work full duplex.
20 * Maybe other 16 bit cards in this code could behave
21 * the same.
22 * Chris Rankin: Use spinlocks instead of CLI/STI
25 #include <linux/spinlock.h>
27 #include "sound_config.h"
29 #include "sb_mixer.h"
30 #include "sb.h"
32 #include "sb_ess.h"
34 int sb_audio_open(int dev, int mode)
36 sb_devc *devc = audio_devs[dev]->devc;
37 unsigned long flags;
39 if (devc == NULL)
41 printk(KERN_ERR "Sound Blaster: incomplete initialization.\n");
42 return -ENXIO;
44 if (devc->caps & SB_NO_RECORDING && mode & OPEN_READ)
46 if (mode == OPEN_READ)
47 return -EPERM;
49 spin_lock_irqsave(&devc->lock, flags);
50 if (devc->opened)
52 spin_unlock_irqrestore(&devc->lock, flags);
53 return -EBUSY;
55 if (devc->dma16 != -1 && devc->dma16 != devc->dma8 && !devc->duplex)
57 if (sound_open_dma(devc->dma16, "Sound Blaster 16 bit"))
59 spin_unlock_irqrestore(&devc->lock, flags);
60 return -EBUSY;
63 devc->opened = mode;
64 spin_unlock_irqrestore(&devc->lock, flags);
66 devc->irq_mode = IMODE_NONE;
67 devc->irq_mode_16 = IMODE_NONE;
68 devc->fullduplex = devc->duplex &&
69 ((mode & OPEN_READ) && (mode & OPEN_WRITE));
70 sb_dsp_reset(devc);
72 /* At first glance this check isn't enough, some ESS chips might not
73 * have a RECLEV. However if they don't common_mixer_set will refuse
74 * cause devc->iomap has no register mapping for RECLEV
76 if (devc->model == MDL_ESS) ess_mixer_reload (devc, SOUND_MIXER_RECLEV);
78 /* The ALS007 seems to require that the DSP be removed from the output */
79 /* in order for recording to be activated properly. This is done by */
80 /* setting the appropriate bits of the output control register 4ch to */
81 /* zero. This code assumes that the output control registers are not */
82 /* used anywhere else and therefore the DSP bits are *always* ON for */
83 /* output and OFF for sampling. */
85 if (devc->submodel == SUBMDL_ALS007)
87 if (mode & OPEN_READ)
88 sb_setmixer(devc,ALS007_OUTPUT_CTRL2,
89 sb_getmixer(devc,ALS007_OUTPUT_CTRL2) & 0xf9);
90 else
91 sb_setmixer(devc,ALS007_OUTPUT_CTRL2,
92 sb_getmixer(devc,ALS007_OUTPUT_CTRL2) | 0x06);
94 return 0;
97 void sb_audio_close(int dev)
99 sb_devc *devc = audio_devs[dev]->devc;
101 /* fix things if mmap turned off fullduplex */
102 if(devc->duplex
103 && !devc->fullduplex
104 && (devc->opened & OPEN_READ) && (devc->opened & OPEN_WRITE))
106 struct dma_buffparms *dmap_temp;
107 dmap_temp = audio_devs[dev]->dmap_out;
108 audio_devs[dev]->dmap_out = audio_devs[dev]->dmap_in;
109 audio_devs[dev]->dmap_in = dmap_temp;
111 audio_devs[dev]->dmap_out->dma = devc->dma8;
112 audio_devs[dev]->dmap_in->dma = ( devc->duplex ) ?
113 devc->dma16 : devc->dma8;
115 if (devc->dma16 != -1 && devc->dma16 != devc->dma8 && !devc->duplex)
116 sound_close_dma(devc->dma16);
118 /* For ALS007, turn DSP output back on if closing the device for read */
120 if ((devc->submodel == SUBMDL_ALS007) && (devc->opened & OPEN_READ))
122 sb_setmixer(devc,ALS007_OUTPUT_CTRL2,
123 sb_getmixer(devc,ALS007_OUTPUT_CTRL2) | 0x06);
125 devc->opened = 0;
128 static void sb_set_output_parms(int dev, unsigned long buf, int nr_bytes,
129 int intrflag)
131 sb_devc *devc = audio_devs[dev]->devc;
133 if (!devc->fullduplex || devc->bits == AFMT_S16_LE)
135 devc->trg_buf = buf;
136 devc->trg_bytes = nr_bytes;
137 devc->trg_intrflag = intrflag;
138 devc->irq_mode = IMODE_OUTPUT;
140 else
142 devc->trg_buf_16 = buf;
143 devc->trg_bytes_16 = nr_bytes;
144 devc->trg_intrflag_16 = intrflag;
145 devc->irq_mode_16 = IMODE_OUTPUT;
149 static void sb_set_input_parms(int dev, unsigned long buf, int count, int intrflag)
151 sb_devc *devc = audio_devs[dev]->devc;
153 if (!devc->fullduplex || devc->bits != AFMT_S16_LE)
155 devc->trg_buf = buf;
156 devc->trg_bytes = count;
157 devc->trg_intrflag = intrflag;
158 devc->irq_mode = IMODE_INPUT;
160 else
162 devc->trg_buf_16 = buf;
163 devc->trg_bytes_16 = count;
164 devc->trg_intrflag_16 = intrflag;
165 devc->irq_mode_16 = IMODE_INPUT;
170 * SB1.x compatible routines
173 static void sb1_audio_output_block(int dev, unsigned long buf, int nr_bytes, int intrflag)
175 unsigned long flags;
176 int count = nr_bytes;
177 sb_devc *devc = audio_devs[dev]->devc;
179 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE); */
181 if (audio_devs[dev]->dmap_out->dma > 3)
182 count >>= 1;
183 count--;
185 devc->irq_mode = IMODE_OUTPUT;
187 spin_lock_irqsave(&devc->lock, flags);
188 if (sb_dsp_command(devc, 0x14)) /* 8 bit DAC using DMA */
190 sb_dsp_command(devc, (unsigned char) (count & 0xff));
191 sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
193 else
194 printk(KERN_WARNING "Sound Blaster: unable to start DAC.\n");
195 spin_unlock_irqrestore(&devc->lock, flags);
196 devc->intr_active = 1;
199 static void sb1_audio_start_input(int dev, unsigned long buf, int nr_bytes, int intrflag)
201 unsigned long flags;
202 int count = nr_bytes;
203 sb_devc *devc = audio_devs[dev]->devc;
206 * Start a DMA input to the buffer pointed by dmaqtail
209 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ); */
211 if (audio_devs[dev]->dmap_out->dma > 3)
212 count >>= 1;
213 count--;
215 devc->irq_mode = IMODE_INPUT;
217 spin_lock_irqsave(&devc->lock, flags);
218 if (sb_dsp_command(devc, 0x24)) /* 8 bit ADC using DMA */
220 sb_dsp_command(devc, (unsigned char) (count & 0xff));
221 sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
223 else
224 printk(KERN_ERR "Sound Blaster: unable to start ADC.\n");
225 spin_unlock_irqrestore(&devc->lock, flags);
227 devc->intr_active = 1;
230 static void sb1_audio_trigger(int dev, int bits)
232 sb_devc *devc = audio_devs[dev]->devc;
234 bits &= devc->irq_mode;
236 if (!bits)
237 sb_dsp_command(devc, 0xd0); /* Halt DMA */
238 else
240 switch (devc->irq_mode)
242 case IMODE_INPUT:
243 sb1_audio_start_input(dev, devc->trg_buf, devc->trg_bytes,
244 devc->trg_intrflag);
245 break;
247 case IMODE_OUTPUT:
248 sb1_audio_output_block(dev, devc->trg_buf, devc->trg_bytes,
249 devc->trg_intrflag);
250 break;
253 devc->trigger_bits = bits;
256 static int sb1_audio_prepare_for_input(int dev, int bsize, int bcount)
258 sb_devc *devc = audio_devs[dev]->devc;
259 unsigned long flags;
261 spin_lock_irqsave(&devc->lock, flags);
262 if (sb_dsp_command(devc, 0x40))
263 sb_dsp_command(devc, devc->tconst);
264 sb_dsp_command(devc, DSP_CMD_SPKOFF);
265 spin_unlock_irqrestore(&devc->lock, flags);
267 devc->trigger_bits = 0;
268 return 0;
271 static int sb1_audio_prepare_for_output(int dev, int bsize, int bcount)
273 sb_devc *devc = audio_devs[dev]->devc;
274 unsigned long flags;
276 spin_lock_irqsave(&devc->lock, flags);
277 if (sb_dsp_command(devc, 0x40))
278 sb_dsp_command(devc, devc->tconst);
279 sb_dsp_command(devc, DSP_CMD_SPKON);
280 spin_unlock_irqrestore(&devc->lock, flags);
281 devc->trigger_bits = 0;
282 return 0;
285 static int sb1_audio_set_speed(int dev, int speed)
287 int max_speed = 23000;
288 sb_devc *devc = audio_devs[dev]->devc;
289 int tmp;
291 if (devc->opened & OPEN_READ)
292 max_speed = 13000;
294 if (speed > 0)
296 if (speed < 4000)
297 speed = 4000;
299 if (speed > max_speed)
300 speed = max_speed;
302 devc->tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
303 tmp = 256 - devc->tconst;
304 speed = (1000000 + tmp / 2) / tmp;
306 devc->speed = speed;
308 return devc->speed;
311 static short sb1_audio_set_channels(int dev, short channels)
313 sb_devc *devc = audio_devs[dev]->devc;
314 return devc->channels = 1;
317 static unsigned int sb1_audio_set_bits(int dev, unsigned int bits)
319 sb_devc *devc = audio_devs[dev]->devc;
320 return devc->bits = 8;
323 static void sb1_audio_halt_xfer(int dev)
325 unsigned long flags;
326 sb_devc *devc = audio_devs[dev]->devc;
328 spin_lock_irqsave(&devc->lock, flags);
329 sb_dsp_reset(devc);
330 spin_unlock_irqrestore(&devc->lock, flags);
334 * SB 2.0 and SB 2.01 compatible routines
337 static void sb20_audio_output_block(int dev, unsigned long buf, int nr_bytes,
338 int intrflag)
340 unsigned long flags;
341 int count = nr_bytes;
342 sb_devc *devc = audio_devs[dev]->devc;
343 unsigned char cmd;
345 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE); */
347 if (audio_devs[dev]->dmap_out->dma > 3)
348 count >>= 1;
349 count--;
351 devc->irq_mode = IMODE_OUTPUT;
353 spin_lock_irqsave(&devc->lock, flags);
354 if (sb_dsp_command(devc, 0x48)) /* DSP Block size */
356 sb_dsp_command(devc, (unsigned char) (count & 0xff));
357 sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
359 if (devc->speed * devc->channels <= 23000)
360 cmd = 0x1c; /* 8 bit PCM output */
361 else
362 cmd = 0x90; /* 8 bit high speed PCM output (SB2.01/Pro) */
364 if (!sb_dsp_command(devc, cmd))
365 printk(KERN_ERR "Sound Blaster: unable to start DAC.\n");
367 else
368 printk(KERN_ERR "Sound Blaster: unable to start DAC.\n");
369 spin_unlock_irqrestore(&devc->lock, flags);
370 devc->intr_active = 1;
373 static void sb20_audio_start_input(int dev, unsigned long buf, int nr_bytes, int intrflag)
375 unsigned long flags;
376 int count = nr_bytes;
377 sb_devc *devc = audio_devs[dev]->devc;
378 unsigned char cmd;
381 * Start a DMA input to the buffer pointed by dmaqtail
384 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ); */
386 if (audio_devs[dev]->dmap_out->dma > 3)
387 count >>= 1;
388 count--;
390 devc->irq_mode = IMODE_INPUT;
392 spin_lock_irqsave(&devc->lock, flags);
393 if (sb_dsp_command(devc, 0x48)) /* DSP Block size */
395 sb_dsp_command(devc, (unsigned char) (count & 0xff));
396 sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
398 if (devc->speed * devc->channels <= (devc->major == 3 ? 23000 : 13000))
399 cmd = 0x2c; /* 8 bit PCM input */
400 else
401 cmd = 0x98; /* 8 bit high speed PCM input (SB2.01/Pro) */
403 if (!sb_dsp_command(devc, cmd))
404 printk(KERN_ERR "Sound Blaster: unable to start ADC.\n");
406 else
407 printk(KERN_ERR "Sound Blaster: unable to start ADC.\n");
408 spin_unlock_irqrestore(&devc->lock, flags);
409 devc->intr_active = 1;
412 static void sb20_audio_trigger(int dev, int bits)
414 sb_devc *devc = audio_devs[dev]->devc;
415 bits &= devc->irq_mode;
417 if (!bits)
418 sb_dsp_command(devc, 0xd0); /* Halt DMA */
419 else
421 switch (devc->irq_mode)
423 case IMODE_INPUT:
424 sb20_audio_start_input(dev, devc->trg_buf, devc->trg_bytes,
425 devc->trg_intrflag);
426 break;
428 case IMODE_OUTPUT:
429 sb20_audio_output_block(dev, devc->trg_buf, devc->trg_bytes,
430 devc->trg_intrflag);
431 break;
434 devc->trigger_bits = bits;
438 * SB2.01 specific speed setup
441 static int sb201_audio_set_speed(int dev, int speed)
443 sb_devc *devc = audio_devs[dev]->devc;
444 int tmp;
445 int s = speed * devc->channels;
447 if (speed > 0)
449 if (speed < 4000)
450 speed = 4000;
451 if (speed > 44100)
452 speed = 44100;
453 if (devc->opened & OPEN_READ && speed > 15000)
454 speed = 15000;
455 devc->tconst = (256 - ((1000000 + s / 2) / s)) & 0xff;
456 tmp = 256 - devc->tconst;
457 speed = ((1000000 + tmp / 2) / tmp) / devc->channels;
459 devc->speed = speed;
461 return devc->speed;
465 * SB Pro specific routines
468 static int sbpro_audio_prepare_for_input(int dev, int bsize, int bcount)
469 { /* For SB Pro and Jazz16 */
470 sb_devc *devc = audio_devs[dev]->devc;
471 unsigned long flags;
472 unsigned char bits = 0;
474 if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
475 audio_devs[dev]->dmap_out->dma = audio_devs[dev]->dmap_in->dma =
476 devc->bits == 16 ? devc->dma16 : devc->dma8;
478 if (devc->model == MDL_JAZZ || devc->model == MDL_SMW)
479 if (devc->bits == AFMT_S16_LE)
480 bits = 0x04; /* 16 bit mode */
482 spin_lock_irqsave(&devc->lock, flags);
483 if (sb_dsp_command(devc, 0x40))
484 sb_dsp_command(devc, devc->tconst);
485 sb_dsp_command(devc, DSP_CMD_SPKOFF);
486 if (devc->channels == 1)
487 sb_dsp_command(devc, 0xa0 | bits); /* Mono input */
488 else
489 sb_dsp_command(devc, 0xa8 | bits); /* Stereo input */
490 spin_unlock_irqrestore(&devc->lock, flags);
492 devc->trigger_bits = 0;
493 return 0;
496 static int sbpro_audio_prepare_for_output(int dev, int bsize, int bcount)
497 { /* For SB Pro and Jazz16 */
498 sb_devc *devc = audio_devs[dev]->devc;
499 unsigned long flags;
500 unsigned char tmp;
501 unsigned char bits = 0;
503 if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
504 audio_devs[dev]->dmap_out->dma = audio_devs[dev]->dmap_in->dma = devc->bits == 16 ? devc->dma16 : devc->dma8;
505 if (devc->model == MDL_SBPRO)
506 sb_mixer_set_stereo(devc, devc->channels == 2);
508 spin_lock_irqsave(&devc->lock, flags);
509 if (sb_dsp_command(devc, 0x40))
510 sb_dsp_command(devc, devc->tconst);
511 sb_dsp_command(devc, DSP_CMD_SPKON);
513 if (devc->model == MDL_JAZZ || devc->model == MDL_SMW)
515 if (devc->bits == AFMT_S16_LE)
516 bits = 0x04; /* 16 bit mode */
518 if (devc->channels == 1)
519 sb_dsp_command(devc, 0xa0 | bits); /* Mono output */
520 else
521 sb_dsp_command(devc, 0xa8 | bits); /* Stereo output */
522 spin_unlock_irqrestore(&devc->lock, flags);
524 else
526 spin_unlock_irqrestore(&devc->lock, flags);
527 tmp = sb_getmixer(devc, 0x0e);
528 if (devc->channels == 1)
529 tmp &= ~0x02;
530 else
531 tmp |= 0x02;
532 sb_setmixer(devc, 0x0e, tmp);
534 devc->trigger_bits = 0;
535 return 0;
538 static int sbpro_audio_set_speed(int dev, int speed)
540 sb_devc *devc = audio_devs[dev]->devc;
542 if (speed > 0)
544 if (speed < 4000)
545 speed = 4000;
546 if (speed > 44100)
547 speed = 44100;
548 if (devc->channels > 1 && speed > 22050)
549 speed = 22050;
550 sb201_audio_set_speed(dev, speed);
552 return devc->speed;
555 static short sbpro_audio_set_channels(int dev, short channels)
557 sb_devc *devc = audio_devs[dev]->devc;
559 if (channels == 1 || channels == 2)
561 if (channels != devc->channels)
563 devc->channels = channels;
564 if (devc->model == MDL_SBPRO && devc->channels == 2)
565 sbpro_audio_set_speed(dev, devc->speed);
568 return devc->channels;
571 static int jazz16_audio_set_speed(int dev, int speed)
573 sb_devc *devc = audio_devs[dev]->devc;
575 if (speed > 0)
577 int tmp;
578 int s = speed * devc->channels;
580 if (speed < 5000)
581 speed = 5000;
582 if (speed > 44100)
583 speed = 44100;
585 devc->tconst = (256 - ((1000000 + s / 2) / s)) & 0xff;
587 tmp = 256 - devc->tconst;
588 speed = ((1000000 + tmp / 2) / tmp) / devc->channels;
590 devc->speed = speed;
592 return devc->speed;
596 * SB16 specific routines
599 static int sb16_audio_set_speed(int dev, int speed)
601 sb_devc *devc = audio_devs[dev]->devc;
602 int max_speed = devc->submodel == SUBMDL_ALS100 ? 48000 : 44100;
604 if (speed > 0)
606 if (speed < 5000)
607 speed = 5000;
609 if (speed > max_speed)
610 speed = max_speed;
612 devc->speed = speed;
614 return devc->speed;
617 static unsigned int sb16_audio_set_bits(int dev, unsigned int bits)
619 sb_devc *devc = audio_devs[dev]->devc;
621 if (bits != 0)
623 if (bits == AFMT_U8 || bits == AFMT_S16_LE)
624 devc->bits = bits;
625 else
626 devc->bits = AFMT_U8;
629 return devc->bits;
632 static int sb16_audio_prepare_for_input(int dev, int bsize, int bcount)
634 sb_devc *devc = audio_devs[dev]->devc;
636 if (!devc->fullduplex)
638 audio_devs[dev]->dmap_out->dma =
639 audio_devs[dev]->dmap_in->dma =
640 devc->bits == AFMT_S16_LE ?
641 devc->dma16 : devc->dma8;
643 else if (devc->bits == AFMT_S16_LE)
645 audio_devs[dev]->dmap_out->dma = devc->dma8;
646 audio_devs[dev]->dmap_in->dma = devc->dma16;
648 else
650 audio_devs[dev]->dmap_out->dma = devc->dma16;
651 audio_devs[dev]->dmap_in->dma = devc->dma8;
654 devc->trigger_bits = 0;
655 return 0;
658 static int sb16_audio_prepare_for_output(int dev, int bsize, int bcount)
660 sb_devc *devc = audio_devs[dev]->devc;
662 if (!devc->fullduplex)
664 audio_devs[dev]->dmap_out->dma =
665 audio_devs[dev]->dmap_in->dma =
666 devc->bits == AFMT_S16_LE ?
667 devc->dma16 : devc->dma8;
669 else if (devc->bits == AFMT_S16_LE)
671 audio_devs[dev]->dmap_out->dma = devc->dma8;
672 audio_devs[dev]->dmap_in->dma = devc->dma16;
674 else
676 audio_devs[dev]->dmap_out->dma = devc->dma16;
677 audio_devs[dev]->dmap_in->dma = devc->dma8;
680 devc->trigger_bits = 0;
681 return 0;
684 static void sb16_audio_output_block(int dev, unsigned long buf, int count,
685 int intrflag)
687 unsigned long flags, cnt;
688 sb_devc *devc = audio_devs[dev]->devc;
689 unsigned long bits;
691 if (!devc->fullduplex || devc->bits == AFMT_S16_LE)
693 devc->irq_mode = IMODE_OUTPUT;
694 devc->intr_active = 1;
696 else
698 devc->irq_mode_16 = IMODE_OUTPUT;
699 devc->intr_active_16 = 1;
702 /* save value */
703 spin_lock_irqsave(&devc->lock, flags);
704 bits = devc->bits;
705 if (devc->fullduplex)
706 devc->bits = (devc->bits == AFMT_S16_LE) ?
707 AFMT_U8 : AFMT_S16_LE;
708 spin_unlock_irqrestore(&devc->lock, flags);
710 cnt = count;
711 if (devc->bits == AFMT_S16_LE)
712 cnt >>= 1;
713 cnt--;
715 spin_lock_irqsave(&devc->lock, flags);
717 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE); */
719 sb_dsp_command(devc, 0x41);
720 sb_dsp_command(devc, (unsigned char) ((devc->speed >> 8) & 0xff));
721 sb_dsp_command(devc, (unsigned char) (devc->speed & 0xff));
723 sb_dsp_command(devc, (devc->bits == AFMT_S16_LE ? 0xb6 : 0xc6));
724 sb_dsp_command(devc, ((devc->channels == 2 ? 0x20 : 0) +
725 (devc->bits == AFMT_S16_LE ? 0x10 : 0)));
726 sb_dsp_command(devc, (unsigned char) (cnt & 0xff));
727 sb_dsp_command(devc, (unsigned char) (cnt >> 8));
729 /* restore real value after all programming */
730 devc->bits = bits;
731 spin_unlock_irqrestore(&devc->lock, flags);
736 * This fails on the Cyrix MediaGX. If you don't have the DMA enabled
737 * before the first sample arrives it locks up. However even if you
738 * do enable the DMA in time you just get DMA timeouts and missing
739 * interrupts and stuff, so for now I've not bothered fixing this either.
742 static void sb16_audio_start_input(int dev, unsigned long buf, int count, int intrflag)
744 unsigned long flags, cnt;
745 sb_devc *devc = audio_devs[dev]->devc;
747 if (!devc->fullduplex || devc->bits != AFMT_S16_LE)
749 devc->irq_mode = IMODE_INPUT;
750 devc->intr_active = 1;
752 else
754 devc->irq_mode_16 = IMODE_INPUT;
755 devc->intr_active_16 = 1;
758 cnt = count;
759 if (devc->bits == AFMT_S16_LE)
760 cnt >>= 1;
761 cnt--;
763 spin_lock_irqsave(&devc->lock, flags);
765 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ); */
767 sb_dsp_command(devc, 0x42);
768 sb_dsp_command(devc, (unsigned char) ((devc->speed >> 8) & 0xff));
769 sb_dsp_command(devc, (unsigned char) (devc->speed & 0xff));
771 sb_dsp_command(devc, (devc->bits == AFMT_S16_LE ? 0xbe : 0xce));
772 sb_dsp_command(devc, ((devc->channels == 2 ? 0x20 : 0) +
773 (devc->bits == AFMT_S16_LE ? 0x10 : 0)));
774 sb_dsp_command(devc, (unsigned char) (cnt & 0xff));
775 sb_dsp_command(devc, (unsigned char) (cnt >> 8));
777 spin_unlock_irqrestore(&devc->lock, flags);
780 static void sb16_audio_trigger(int dev, int bits)
782 sb_devc *devc = audio_devs[dev]->devc;
784 int bits_16 = bits & devc->irq_mode_16;
785 bits &= devc->irq_mode;
787 if (!bits && !bits_16)
788 sb_dsp_command(devc, 0xd0); /* Halt DMA */
789 else
791 if (bits)
793 switch (devc->irq_mode)
795 case IMODE_INPUT:
796 sb16_audio_start_input(dev,
797 devc->trg_buf,
798 devc->trg_bytes,
799 devc->trg_intrflag);
800 break;
802 case IMODE_OUTPUT:
803 sb16_audio_output_block(dev,
804 devc->trg_buf,
805 devc->trg_bytes,
806 devc->trg_intrflag);
807 break;
810 if (bits_16)
812 switch (devc->irq_mode_16)
814 case IMODE_INPUT:
815 sb16_audio_start_input(dev,
816 devc->trg_buf_16,
817 devc->trg_bytes_16,
818 devc->trg_intrflag_16);
819 break;
821 case IMODE_OUTPUT:
822 sb16_audio_output_block(dev,
823 devc->trg_buf_16,
824 devc->trg_bytes_16,
825 devc->trg_intrflag_16);
826 break;
831 devc->trigger_bits = bits | bits_16;
834 static unsigned char lbuf8[2048];
835 static signed short *lbuf16 = (signed short *)lbuf8;
836 #define LBUFCOPYSIZE 1024
837 static void
838 sb16_copy_from_user(int dev,
839 char *localbuf, int localoffs,
840 const char __user *userbuf, int useroffs,
841 int max_in, int max_out,
842 int *used, int *returned,
843 int len)
845 sb_devc *devc = audio_devs[dev]->devc;
846 int i, c, p, locallen;
847 unsigned char *buf8;
848 signed short *buf16;
850 /* if not duplex no conversion */
851 if (!devc->fullduplex)
853 if (copy_from_user(localbuf + localoffs,
854 userbuf + useroffs, len))
855 return;
856 *used = len;
857 *returned = len;
859 else if (devc->bits == AFMT_S16_LE)
861 /* 16 -> 8 */
862 /* max_in >> 1, max number of samples in ( 16 bits ) */
863 /* max_out, max number of samples out ( 8 bits ) */
864 /* len, number of samples that will be taken ( 16 bits )*/
865 /* c, count of samples remaining in buffer ( 16 bits )*/
866 /* p, count of samples already processed ( 16 bits )*/
867 len = ( (max_in >> 1) > max_out) ? max_out : (max_in >> 1);
868 c = len;
869 p = 0;
870 buf8 = (unsigned char *)(localbuf + localoffs);
871 while (c)
873 locallen = (c >= LBUFCOPYSIZE ? LBUFCOPYSIZE : c);
874 /* << 1 in order to get 16 bit samples */
875 if (copy_from_user(lbuf16,
876 userbuf + useroffs + (p << 1),
877 locallen << 1))
878 return;
879 for (i = 0; i < locallen; i++)
881 buf8[p+i] = ~((lbuf16[i] >> 8) & 0xff) ^ 0x80;
883 c -= locallen; p += locallen;
885 /* used = ( samples * 16 bits size ) */
886 *used = max_in > ( max_out << 1) ? (max_out << 1) : max_in;
887 /* returned = ( samples * 8 bits size ) */
888 *returned = len;
890 else
892 /* 8 -> 16 */
893 /* max_in, max number of samples in ( 8 bits ) */
894 /* max_out >> 1, max number of samples out ( 16 bits ) */
895 /* len, number of samples that will be taken ( 8 bits )*/
896 /* c, count of samples remaining in buffer ( 8 bits )*/
897 /* p, count of samples already processed ( 8 bits )*/
898 len = max_in > (max_out >> 1) ? (max_out >> 1) : max_in;
899 c = len;
900 p = 0;
901 buf16 = (signed short *)(localbuf + localoffs);
902 while (c)
904 locallen = (c >= LBUFCOPYSIZE ? LBUFCOPYSIZE : c);
905 if (copy_from_user(lbuf8,
906 userbuf+useroffs + p,
907 locallen))
908 return;
909 for (i = 0; i < locallen; i++)
911 buf16[p+i] = (~lbuf8[i] ^ 0x80) << 8;
913 c -= locallen; p += locallen;
915 /* used = ( samples * 8 bits size ) */
916 *used = len;
917 /* returned = ( samples * 16 bits size ) */
918 *returned = len << 1;
922 static void
923 sb16_audio_mmap(int dev)
925 sb_devc *devc = audio_devs[dev]->devc;
926 devc->fullduplex = 0;
929 static struct audio_driver sb1_audio_driver = /* SB1.x */
931 .owner = THIS_MODULE,
932 .open = sb_audio_open,
933 .close = sb_audio_close,
934 .output_block = sb_set_output_parms,
935 .start_input = sb_set_input_parms,
936 .prepare_for_input = sb1_audio_prepare_for_input,
937 .prepare_for_output = sb1_audio_prepare_for_output,
938 .halt_io = sb1_audio_halt_xfer,
939 .trigger = sb1_audio_trigger,
940 .set_speed = sb1_audio_set_speed,
941 .set_bits = sb1_audio_set_bits,
942 .set_channels = sb1_audio_set_channels
945 static struct audio_driver sb20_audio_driver = /* SB2.0 */
947 .owner = THIS_MODULE,
948 .open = sb_audio_open,
949 .close = sb_audio_close,
950 .output_block = sb_set_output_parms,
951 .start_input = sb_set_input_parms,
952 .prepare_for_input = sb1_audio_prepare_for_input,
953 .prepare_for_output = sb1_audio_prepare_for_output,
954 .halt_io = sb1_audio_halt_xfer,
955 .trigger = sb20_audio_trigger,
956 .set_speed = sb1_audio_set_speed,
957 .set_bits = sb1_audio_set_bits,
958 .set_channels = sb1_audio_set_channels
961 static struct audio_driver sb201_audio_driver = /* SB2.01 */
963 .owner = THIS_MODULE,
964 .open = sb_audio_open,
965 .close = sb_audio_close,
966 .output_block = sb_set_output_parms,
967 .start_input = sb_set_input_parms,
968 .prepare_for_input = sb1_audio_prepare_for_input,
969 .prepare_for_output = sb1_audio_prepare_for_output,
970 .halt_io = sb1_audio_halt_xfer,
971 .trigger = sb20_audio_trigger,
972 .set_speed = sb201_audio_set_speed,
973 .set_bits = sb1_audio_set_bits,
974 .set_channels = sb1_audio_set_channels
977 static struct audio_driver sbpro_audio_driver = /* SB Pro */
979 .owner = THIS_MODULE,
980 .open = sb_audio_open,
981 .close = sb_audio_close,
982 .output_block = sb_set_output_parms,
983 .start_input = sb_set_input_parms,
984 .prepare_for_input = sbpro_audio_prepare_for_input,
985 .prepare_for_output = sbpro_audio_prepare_for_output,
986 .halt_io = sb1_audio_halt_xfer,
987 .trigger = sb20_audio_trigger,
988 .set_speed = sbpro_audio_set_speed,
989 .set_bits = sb1_audio_set_bits,
990 .set_channels = sbpro_audio_set_channels
993 static struct audio_driver jazz16_audio_driver = /* Jazz16 and SM Wave */
995 .owner = THIS_MODULE,
996 .open = sb_audio_open,
997 .close = sb_audio_close,
998 .output_block = sb_set_output_parms,
999 .start_input = sb_set_input_parms,
1000 .prepare_for_input = sbpro_audio_prepare_for_input,
1001 .prepare_for_output = sbpro_audio_prepare_for_output,
1002 .halt_io = sb1_audio_halt_xfer,
1003 .trigger = sb20_audio_trigger,
1004 .set_speed = jazz16_audio_set_speed,
1005 .set_bits = sb16_audio_set_bits,
1006 .set_channels = sbpro_audio_set_channels
1009 static struct audio_driver sb16_audio_driver = /* SB16 */
1011 .owner = THIS_MODULE,
1012 .open = sb_audio_open,
1013 .close = sb_audio_close,
1014 .output_block = sb_set_output_parms,
1015 .start_input = sb_set_input_parms,
1016 .prepare_for_input = sb16_audio_prepare_for_input,
1017 .prepare_for_output = sb16_audio_prepare_for_output,
1018 .halt_io = sb1_audio_halt_xfer,
1019 .copy_user = sb16_copy_from_user,
1020 .trigger = sb16_audio_trigger,
1021 .set_speed = sb16_audio_set_speed,
1022 .set_bits = sb16_audio_set_bits,
1023 .set_channels = sbpro_audio_set_channels,
1024 .mmap = sb16_audio_mmap
1027 void sb_audio_init(sb_devc * devc, char *name, struct module *owner)
1029 int audio_flags = 0;
1030 int format_mask = AFMT_U8;
1032 struct audio_driver *driver = &sb1_audio_driver;
1034 switch (devc->model)
1036 case MDL_SB1: /* SB1.0 or SB 1.5 */
1037 DDB(printk("Will use standard SB1.x driver\n"));
1038 audio_flags = DMA_HARDSTOP;
1039 break;
1041 case MDL_SB2:
1042 DDB(printk("Will use SB2.0 driver\n"));
1043 audio_flags = DMA_AUTOMODE;
1044 driver = &sb20_audio_driver;
1045 break;
1047 case MDL_SB201:
1048 DDB(printk("Will use SB2.01 (high speed) driver\n"));
1049 audio_flags = DMA_AUTOMODE;
1050 driver = &sb201_audio_driver;
1051 break;
1053 case MDL_JAZZ:
1054 case MDL_SMW:
1055 DDB(printk("Will use Jazz16 driver\n"));
1056 audio_flags = DMA_AUTOMODE;
1057 format_mask |= AFMT_S16_LE;
1058 driver = &jazz16_audio_driver;
1059 break;
1061 case MDL_ESS:
1062 DDB(printk("Will use ESS ES688/1688 driver\n"));
1063 driver = ess_audio_init (devc, &audio_flags, &format_mask);
1064 break;
1066 case MDL_SB16:
1067 DDB(printk("Will use SB16 driver\n"));
1068 audio_flags = DMA_AUTOMODE;
1069 format_mask |= AFMT_S16_LE;
1070 if (devc->dma8 != devc->dma16 && devc->dma16 != -1)
1072 audio_flags |= DMA_DUPLEX;
1073 devc->duplex = 1;
1075 driver = &sb16_audio_driver;
1076 break;
1078 default:
1079 DDB(printk("Will use SB Pro driver\n"));
1080 audio_flags = DMA_AUTOMODE;
1081 driver = &sbpro_audio_driver;
1084 if (owner)
1085 driver->owner = owner;
1087 if ((devc->dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
1088 name,driver, sizeof(struct audio_driver),
1089 audio_flags, format_mask, devc,
1090 devc->dma8,
1091 devc->duplex ? devc->dma16 : devc->dma8)) < 0)
1093 printk(KERN_ERR "Sound Blaster: unable to install audio.\n");
1094 return;
1096 audio_devs[devc->dev]->mixer_dev = devc->my_mixerdev;
1097 audio_devs[devc->dev]->min_fragment = 5;