stmmac: fix a filter problem after resuming.
[linux/fpc-iii.git] / sound / oss / sb_audio.c
blobdc91072f4d828b027df5668df5a8e884ccb0c422
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))
105 swap(audio_devs[dev]->dmap_out, audio_devs[dev]->dmap_in);
107 audio_devs[dev]->dmap_out->dma = devc->dma8;
108 audio_devs[dev]->dmap_in->dma = ( devc->duplex ) ?
109 devc->dma16 : devc->dma8;
111 if (devc->dma16 != -1 && devc->dma16 != devc->dma8 && !devc->duplex)
112 sound_close_dma(devc->dma16);
114 /* For ALS007, turn DSP output back on if closing the device for read */
116 if ((devc->submodel == SUBMDL_ALS007) && (devc->opened & OPEN_READ))
118 sb_setmixer(devc,ALS007_OUTPUT_CTRL2,
119 sb_getmixer(devc,ALS007_OUTPUT_CTRL2) | 0x06);
121 devc->opened = 0;
124 static void sb_set_output_parms(int dev, unsigned long buf, int nr_bytes,
125 int intrflag)
127 sb_devc *devc = audio_devs[dev]->devc;
129 if (!devc->fullduplex || devc->bits == AFMT_S16_LE)
131 devc->trg_buf = buf;
132 devc->trg_bytes = nr_bytes;
133 devc->trg_intrflag = intrflag;
134 devc->irq_mode = IMODE_OUTPUT;
136 else
138 devc->trg_buf_16 = buf;
139 devc->trg_bytes_16 = nr_bytes;
140 devc->trg_intrflag_16 = intrflag;
141 devc->irq_mode_16 = IMODE_OUTPUT;
145 static void sb_set_input_parms(int dev, unsigned long buf, int count, int intrflag)
147 sb_devc *devc = audio_devs[dev]->devc;
149 if (!devc->fullduplex || devc->bits != AFMT_S16_LE)
151 devc->trg_buf = buf;
152 devc->trg_bytes = count;
153 devc->trg_intrflag = intrflag;
154 devc->irq_mode = IMODE_INPUT;
156 else
158 devc->trg_buf_16 = buf;
159 devc->trg_bytes_16 = count;
160 devc->trg_intrflag_16 = intrflag;
161 devc->irq_mode_16 = IMODE_INPUT;
166 * SB1.x compatible routines
169 static void sb1_audio_output_block(int dev, unsigned long buf, int nr_bytes, int intrflag)
171 unsigned long flags;
172 int count = nr_bytes;
173 sb_devc *devc = audio_devs[dev]->devc;
175 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE); */
177 if (audio_devs[dev]->dmap_out->dma > 3)
178 count >>= 1;
179 count--;
181 devc->irq_mode = IMODE_OUTPUT;
183 spin_lock_irqsave(&devc->lock, flags);
184 if (sb_dsp_command(devc, 0x14)) /* 8 bit DAC using DMA */
186 sb_dsp_command(devc, (unsigned char) (count & 0xff));
187 sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
189 else
190 printk(KERN_WARNING "Sound Blaster: unable to start DAC.\n");
191 spin_unlock_irqrestore(&devc->lock, flags);
192 devc->intr_active = 1;
195 static void sb1_audio_start_input(int dev, unsigned long buf, int nr_bytes, int intrflag)
197 unsigned long flags;
198 int count = nr_bytes;
199 sb_devc *devc = audio_devs[dev]->devc;
202 * Start a DMA input to the buffer pointed by dmaqtail
205 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ); */
207 if (audio_devs[dev]->dmap_out->dma > 3)
208 count >>= 1;
209 count--;
211 devc->irq_mode = IMODE_INPUT;
213 spin_lock_irqsave(&devc->lock, flags);
214 if (sb_dsp_command(devc, 0x24)) /* 8 bit ADC using DMA */
216 sb_dsp_command(devc, (unsigned char) (count & 0xff));
217 sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
219 else
220 printk(KERN_ERR "Sound Blaster: unable to start ADC.\n");
221 spin_unlock_irqrestore(&devc->lock, flags);
223 devc->intr_active = 1;
226 static void sb1_audio_trigger(int dev, int bits)
228 sb_devc *devc = audio_devs[dev]->devc;
230 bits &= devc->irq_mode;
232 if (!bits)
233 sb_dsp_command(devc, 0xd0); /* Halt DMA */
234 else
236 switch (devc->irq_mode)
238 case IMODE_INPUT:
239 sb1_audio_start_input(dev, devc->trg_buf, devc->trg_bytes,
240 devc->trg_intrflag);
241 break;
243 case IMODE_OUTPUT:
244 sb1_audio_output_block(dev, devc->trg_buf, devc->trg_bytes,
245 devc->trg_intrflag);
246 break;
249 devc->trigger_bits = bits;
252 static int sb1_audio_prepare_for_input(int dev, int bsize, int bcount)
254 sb_devc *devc = audio_devs[dev]->devc;
255 unsigned long flags;
257 spin_lock_irqsave(&devc->lock, flags);
258 if (sb_dsp_command(devc, 0x40))
259 sb_dsp_command(devc, devc->tconst);
260 sb_dsp_command(devc, DSP_CMD_SPKOFF);
261 spin_unlock_irqrestore(&devc->lock, flags);
263 devc->trigger_bits = 0;
264 return 0;
267 static int sb1_audio_prepare_for_output(int dev, int bsize, int bcount)
269 sb_devc *devc = audio_devs[dev]->devc;
270 unsigned long flags;
272 spin_lock_irqsave(&devc->lock, flags);
273 if (sb_dsp_command(devc, 0x40))
274 sb_dsp_command(devc, devc->tconst);
275 sb_dsp_command(devc, DSP_CMD_SPKON);
276 spin_unlock_irqrestore(&devc->lock, flags);
277 devc->trigger_bits = 0;
278 return 0;
281 static int sb1_audio_set_speed(int dev, int speed)
283 int max_speed = 23000;
284 sb_devc *devc = audio_devs[dev]->devc;
285 int tmp;
287 if (devc->opened & OPEN_READ)
288 max_speed = 13000;
290 if (speed > 0)
292 if (speed < 4000)
293 speed = 4000;
295 if (speed > max_speed)
296 speed = max_speed;
298 devc->tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
299 tmp = 256 - devc->tconst;
300 speed = (1000000 + tmp / 2) / tmp;
302 devc->speed = speed;
304 return devc->speed;
307 static short sb1_audio_set_channels(int dev, short channels)
309 sb_devc *devc = audio_devs[dev]->devc;
310 return devc->channels = 1;
313 static unsigned int sb1_audio_set_bits(int dev, unsigned int bits)
315 sb_devc *devc = audio_devs[dev]->devc;
316 return devc->bits = 8;
319 static void sb1_audio_halt_xfer(int dev)
321 unsigned long flags;
322 sb_devc *devc = audio_devs[dev]->devc;
324 spin_lock_irqsave(&devc->lock, flags);
325 sb_dsp_reset(devc);
326 spin_unlock_irqrestore(&devc->lock, flags);
330 * SB 2.0 and SB 2.01 compatible routines
333 static void sb20_audio_output_block(int dev, unsigned long buf, int nr_bytes,
334 int intrflag)
336 unsigned long flags;
337 int count = nr_bytes;
338 sb_devc *devc = audio_devs[dev]->devc;
339 unsigned char cmd;
341 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE); */
343 if (audio_devs[dev]->dmap_out->dma > 3)
344 count >>= 1;
345 count--;
347 devc->irq_mode = IMODE_OUTPUT;
349 spin_lock_irqsave(&devc->lock, flags);
350 if (sb_dsp_command(devc, 0x48)) /* DSP Block size */
352 sb_dsp_command(devc, (unsigned char) (count & 0xff));
353 sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
355 if (devc->speed * devc->channels <= 23000)
356 cmd = 0x1c; /* 8 bit PCM output */
357 else
358 cmd = 0x90; /* 8 bit high speed PCM output (SB2.01/Pro) */
360 if (!sb_dsp_command(devc, cmd))
361 printk(KERN_ERR "Sound Blaster: unable to start DAC.\n");
363 else
364 printk(KERN_ERR "Sound Blaster: unable to start DAC.\n");
365 spin_unlock_irqrestore(&devc->lock, flags);
366 devc->intr_active = 1;
369 static void sb20_audio_start_input(int dev, unsigned long buf, int nr_bytes, int intrflag)
371 unsigned long flags;
372 int count = nr_bytes;
373 sb_devc *devc = audio_devs[dev]->devc;
374 unsigned char cmd;
377 * Start a DMA input to the buffer pointed by dmaqtail
380 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ); */
382 if (audio_devs[dev]->dmap_out->dma > 3)
383 count >>= 1;
384 count--;
386 devc->irq_mode = IMODE_INPUT;
388 spin_lock_irqsave(&devc->lock, flags);
389 if (sb_dsp_command(devc, 0x48)) /* DSP Block size */
391 sb_dsp_command(devc, (unsigned char) (count & 0xff));
392 sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
394 if (devc->speed * devc->channels <= (devc->major == 3 ? 23000 : 13000))
395 cmd = 0x2c; /* 8 bit PCM input */
396 else
397 cmd = 0x98; /* 8 bit high speed PCM input (SB2.01/Pro) */
399 if (!sb_dsp_command(devc, cmd))
400 printk(KERN_ERR "Sound Blaster: unable to start ADC.\n");
402 else
403 printk(KERN_ERR "Sound Blaster: unable to start ADC.\n");
404 spin_unlock_irqrestore(&devc->lock, flags);
405 devc->intr_active = 1;
408 static void sb20_audio_trigger(int dev, int bits)
410 sb_devc *devc = audio_devs[dev]->devc;
411 bits &= devc->irq_mode;
413 if (!bits)
414 sb_dsp_command(devc, 0xd0); /* Halt DMA */
415 else
417 switch (devc->irq_mode)
419 case IMODE_INPUT:
420 sb20_audio_start_input(dev, devc->trg_buf, devc->trg_bytes,
421 devc->trg_intrflag);
422 break;
424 case IMODE_OUTPUT:
425 sb20_audio_output_block(dev, devc->trg_buf, devc->trg_bytes,
426 devc->trg_intrflag);
427 break;
430 devc->trigger_bits = bits;
434 * SB2.01 specific speed setup
437 static int sb201_audio_set_speed(int dev, int speed)
439 sb_devc *devc = audio_devs[dev]->devc;
440 int tmp;
441 int s;
443 if (speed > 0)
445 if (speed < 4000)
446 speed = 4000;
447 if (speed > 44100)
448 speed = 44100;
449 if (devc->opened & OPEN_READ && speed > 15000)
450 speed = 15000;
451 s = speed * devc->channels;
452 devc->tconst = (256 - ((1000000 + s / 2) / s)) & 0xff;
453 tmp = 256 - devc->tconst;
454 speed = ((1000000 + tmp / 2) / tmp) / devc->channels;
456 devc->speed = speed;
458 return devc->speed;
462 * SB Pro specific routines
465 static int sbpro_audio_prepare_for_input(int dev, int bsize, int bcount)
466 { /* For SB Pro and Jazz16 */
467 sb_devc *devc = audio_devs[dev]->devc;
468 unsigned long flags;
469 unsigned char bits = 0;
471 if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
472 audio_devs[dev]->dmap_out->dma = audio_devs[dev]->dmap_in->dma =
473 devc->bits == 16 ? devc->dma16 : devc->dma8;
475 if (devc->model == MDL_JAZZ || devc->model == MDL_SMW)
476 if (devc->bits == AFMT_S16_LE)
477 bits = 0x04; /* 16 bit mode */
479 spin_lock_irqsave(&devc->lock, flags);
480 if (sb_dsp_command(devc, 0x40))
481 sb_dsp_command(devc, devc->tconst);
482 sb_dsp_command(devc, DSP_CMD_SPKOFF);
483 if (devc->channels == 1)
484 sb_dsp_command(devc, 0xa0 | bits); /* Mono input */
485 else
486 sb_dsp_command(devc, 0xa8 | bits); /* Stereo input */
487 spin_unlock_irqrestore(&devc->lock, flags);
489 devc->trigger_bits = 0;
490 return 0;
493 static int sbpro_audio_prepare_for_output(int dev, int bsize, int bcount)
494 { /* For SB Pro and Jazz16 */
495 sb_devc *devc = audio_devs[dev]->devc;
496 unsigned long flags;
497 unsigned char tmp;
498 unsigned char bits = 0;
500 if (devc->dma16 >= 0 && devc->dma16 != devc->dma8)
501 audio_devs[dev]->dmap_out->dma = audio_devs[dev]->dmap_in->dma = devc->bits == 16 ? devc->dma16 : devc->dma8;
502 if (devc->model == MDL_SBPRO)
503 sb_mixer_set_stereo(devc, devc->channels == 2);
505 spin_lock_irqsave(&devc->lock, flags);
506 if (sb_dsp_command(devc, 0x40))
507 sb_dsp_command(devc, devc->tconst);
508 sb_dsp_command(devc, DSP_CMD_SPKON);
510 if (devc->model == MDL_JAZZ || devc->model == MDL_SMW)
512 if (devc->bits == AFMT_S16_LE)
513 bits = 0x04; /* 16 bit mode */
515 if (devc->channels == 1)
516 sb_dsp_command(devc, 0xa0 | bits); /* Mono output */
517 else
518 sb_dsp_command(devc, 0xa8 | bits); /* Stereo output */
519 spin_unlock_irqrestore(&devc->lock, flags);
521 else
523 spin_unlock_irqrestore(&devc->lock, flags);
524 tmp = sb_getmixer(devc, 0x0e);
525 if (devc->channels == 1)
526 tmp &= ~0x02;
527 else
528 tmp |= 0x02;
529 sb_setmixer(devc, 0x0e, tmp);
531 devc->trigger_bits = 0;
532 return 0;
535 static int sbpro_audio_set_speed(int dev, int speed)
537 sb_devc *devc = audio_devs[dev]->devc;
539 if (speed > 0)
541 if (speed < 4000)
542 speed = 4000;
543 if (speed > 44100)
544 speed = 44100;
545 if (devc->channels > 1 && speed > 22050)
546 speed = 22050;
547 sb201_audio_set_speed(dev, speed);
549 return devc->speed;
552 static short sbpro_audio_set_channels(int dev, short channels)
554 sb_devc *devc = audio_devs[dev]->devc;
556 if (channels == 1 || channels == 2)
558 if (channels != devc->channels)
560 devc->channels = channels;
561 if (devc->model == MDL_SBPRO && devc->channels == 2)
562 sbpro_audio_set_speed(dev, devc->speed);
565 return devc->channels;
568 static int jazz16_audio_set_speed(int dev, int speed)
570 sb_devc *devc = audio_devs[dev]->devc;
572 if (speed > 0)
574 int tmp;
575 int s;
577 if (speed < 5000)
578 speed = 5000;
579 if (speed > 44100)
580 speed = 44100;
582 s = speed * devc->channels;
584 devc->tconst = (256 - ((1000000 + s / 2) / s)) & 0xff;
586 tmp = 256 - devc->tconst;
587 speed = ((1000000 + tmp / 2) / tmp) / devc->channels;
589 devc->speed = speed;
591 return devc->speed;
595 * SB16 specific routines
598 static int sb16_audio_set_speed(int dev, int speed)
600 sb_devc *devc = audio_devs[dev]->devc;
601 int max_speed = devc->submodel == SUBMDL_ALS100 ? 48000 : 44100;
603 if (speed > 0)
605 if (speed < 5000)
606 speed = 5000;
608 if (speed > max_speed)
609 speed = max_speed;
611 devc->speed = speed;
613 return devc->speed;
616 static unsigned int sb16_audio_set_bits(int dev, unsigned int bits)
618 sb_devc *devc = audio_devs[dev]->devc;
620 if (bits != 0)
622 if (bits == AFMT_U8 || bits == AFMT_S16_LE)
623 devc->bits = bits;
624 else
625 devc->bits = AFMT_U8;
628 return devc->bits;
631 static int sb16_audio_prepare_for_input(int dev, int bsize, int bcount)
633 sb_devc *devc = audio_devs[dev]->devc;
635 if (!devc->fullduplex)
637 audio_devs[dev]->dmap_out->dma =
638 audio_devs[dev]->dmap_in->dma =
639 devc->bits == AFMT_S16_LE ?
640 devc->dma16 : devc->dma8;
642 else if (devc->bits == AFMT_S16_LE)
644 audio_devs[dev]->dmap_out->dma = devc->dma8;
645 audio_devs[dev]->dmap_in->dma = devc->dma16;
647 else
649 audio_devs[dev]->dmap_out->dma = devc->dma16;
650 audio_devs[dev]->dmap_in->dma = devc->dma8;
653 devc->trigger_bits = 0;
654 return 0;
657 static int sb16_audio_prepare_for_output(int dev, int bsize, int bcount)
659 sb_devc *devc = audio_devs[dev]->devc;
661 if (!devc->fullduplex)
663 audio_devs[dev]->dmap_out->dma =
664 audio_devs[dev]->dmap_in->dma =
665 devc->bits == AFMT_S16_LE ?
666 devc->dma16 : devc->dma8;
668 else if (devc->bits == AFMT_S16_LE)
670 audio_devs[dev]->dmap_out->dma = devc->dma8;
671 audio_devs[dev]->dmap_in->dma = devc->dma16;
673 else
675 audio_devs[dev]->dmap_out->dma = devc->dma16;
676 audio_devs[dev]->dmap_in->dma = devc->dma8;
679 devc->trigger_bits = 0;
680 return 0;
683 static void sb16_audio_output_block(int dev, unsigned long buf, int count,
684 int intrflag)
686 unsigned long flags, cnt;
687 sb_devc *devc = audio_devs[dev]->devc;
688 unsigned long bits;
690 if (!devc->fullduplex || devc->bits == AFMT_S16_LE)
692 devc->irq_mode = IMODE_OUTPUT;
693 devc->intr_active = 1;
695 else
697 devc->irq_mode_16 = IMODE_OUTPUT;
698 devc->intr_active_16 = 1;
701 /* save value */
702 spin_lock_irqsave(&devc->lock, flags);
703 bits = devc->bits;
704 if (devc->fullduplex)
705 devc->bits = (devc->bits == AFMT_S16_LE) ?
706 AFMT_U8 : AFMT_S16_LE;
707 spin_unlock_irqrestore(&devc->lock, flags);
709 cnt = count;
710 if (devc->bits == AFMT_S16_LE)
711 cnt >>= 1;
712 cnt--;
714 spin_lock_irqsave(&devc->lock, flags);
716 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE); */
718 sb_dsp_command(devc, 0x41);
719 sb_dsp_command(devc, (unsigned char) ((devc->speed >> 8) & 0xff));
720 sb_dsp_command(devc, (unsigned char) (devc->speed & 0xff));
722 sb_dsp_command(devc, (devc->bits == AFMT_S16_LE ? 0xb6 : 0xc6));
723 sb_dsp_command(devc, ((devc->channels == 2 ? 0x20 : 0) +
724 (devc->bits == AFMT_S16_LE ? 0x10 : 0)));
725 sb_dsp_command(devc, (unsigned char) (cnt & 0xff));
726 sb_dsp_command(devc, (unsigned char) (cnt >> 8));
728 /* restore real value after all programming */
729 devc->bits = bits;
730 spin_unlock_irqrestore(&devc->lock, flags);
735 * This fails on the Cyrix MediaGX. If you don't have the DMA enabled
736 * before the first sample arrives it locks up. However even if you
737 * do enable the DMA in time you just get DMA timeouts and missing
738 * interrupts and stuff, so for now I've not bothered fixing this either.
741 static void sb16_audio_start_input(int dev, unsigned long buf, int count, int intrflag)
743 unsigned long flags, cnt;
744 sb_devc *devc = audio_devs[dev]->devc;
746 if (!devc->fullduplex || devc->bits != AFMT_S16_LE)
748 devc->irq_mode = IMODE_INPUT;
749 devc->intr_active = 1;
751 else
753 devc->irq_mode_16 = IMODE_INPUT;
754 devc->intr_active_16 = 1;
757 cnt = count;
758 if (devc->bits == AFMT_S16_LE)
759 cnt >>= 1;
760 cnt--;
762 spin_lock_irqsave(&devc->lock, flags);
764 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ); */
766 sb_dsp_command(devc, 0x42);
767 sb_dsp_command(devc, (unsigned char) ((devc->speed >> 8) & 0xff));
768 sb_dsp_command(devc, (unsigned char) (devc->speed & 0xff));
770 sb_dsp_command(devc, (devc->bits == AFMT_S16_LE ? 0xbe : 0xce));
771 sb_dsp_command(devc, ((devc->channels == 2 ? 0x20 : 0) +
772 (devc->bits == AFMT_S16_LE ? 0x10 : 0)));
773 sb_dsp_command(devc, (unsigned char) (cnt & 0xff));
774 sb_dsp_command(devc, (unsigned char) (cnt >> 8));
776 spin_unlock_irqrestore(&devc->lock, flags);
779 static void sb16_audio_trigger(int dev, int bits)
781 sb_devc *devc = audio_devs[dev]->devc;
783 int bits_16 = bits & devc->irq_mode_16;
784 bits &= devc->irq_mode;
786 if (!bits && !bits_16)
787 sb_dsp_command(devc, 0xd0); /* Halt DMA */
788 else
790 if (bits)
792 switch (devc->irq_mode)
794 case IMODE_INPUT:
795 sb16_audio_start_input(dev,
796 devc->trg_buf,
797 devc->trg_bytes,
798 devc->trg_intrflag);
799 break;
801 case IMODE_OUTPUT:
802 sb16_audio_output_block(dev,
803 devc->trg_buf,
804 devc->trg_bytes,
805 devc->trg_intrflag);
806 break;
809 if (bits_16)
811 switch (devc->irq_mode_16)
813 case IMODE_INPUT:
814 sb16_audio_start_input(dev,
815 devc->trg_buf_16,
816 devc->trg_bytes_16,
817 devc->trg_intrflag_16);
818 break;
820 case IMODE_OUTPUT:
821 sb16_audio_output_block(dev,
822 devc->trg_buf_16,
823 devc->trg_bytes_16,
824 devc->trg_intrflag_16);
825 break;
830 devc->trigger_bits = bits | bits_16;
833 static unsigned char lbuf8[2048];
834 static signed short *lbuf16 = (signed short *)lbuf8;
835 #define LBUFCOPYSIZE 1024
836 static void
837 sb16_copy_from_user(int dev,
838 char *localbuf, int localoffs,
839 const char __user *userbuf, int useroffs,
840 int max_in, int max_out,
841 int *used, int *returned,
842 int len)
844 sb_devc *devc = audio_devs[dev]->devc;
845 int i, c, p, locallen;
846 unsigned char *buf8;
847 signed short *buf16;
849 /* if not duplex no conversion */
850 if (!devc->fullduplex)
852 if (copy_from_user(localbuf + localoffs,
853 userbuf + useroffs, len))
854 return;
855 *used = len;
856 *returned = len;
858 else if (devc->bits == AFMT_S16_LE)
860 /* 16 -> 8 */
861 /* max_in >> 1, max number of samples in ( 16 bits ) */
862 /* max_out, max number of samples out ( 8 bits ) */
863 /* len, number of samples that will be taken ( 16 bits )*/
864 /* c, count of samples remaining in buffer ( 16 bits )*/
865 /* p, count of samples already processed ( 16 bits )*/
866 len = ( (max_in >> 1) > max_out) ? max_out : (max_in >> 1);
867 c = len;
868 p = 0;
869 buf8 = (unsigned char *)(localbuf + localoffs);
870 while (c)
872 locallen = (c >= LBUFCOPYSIZE ? LBUFCOPYSIZE : c);
873 /* << 1 in order to get 16 bit samples */
874 if (copy_from_user(lbuf16,
875 userbuf + useroffs + (p << 1),
876 locallen << 1))
877 return;
878 for (i = 0; i < locallen; i++)
880 buf8[p+i] = ~((lbuf16[i] >> 8) & 0xff) ^ 0x80;
882 c -= locallen; p += locallen;
884 /* used = ( samples * 16 bits size ) */
885 *used = max_in > ( max_out << 1) ? (max_out << 1) : max_in;
886 /* returned = ( samples * 8 bits size ) */
887 *returned = len;
889 else
891 /* 8 -> 16 */
892 /* max_in, max number of samples in ( 8 bits ) */
893 /* max_out >> 1, max number of samples out ( 16 bits ) */
894 /* len, number of samples that will be taken ( 8 bits )*/
895 /* c, count of samples remaining in buffer ( 8 bits )*/
896 /* p, count of samples already processed ( 8 bits )*/
897 len = max_in > (max_out >> 1) ? (max_out >> 1) : max_in;
898 c = len;
899 p = 0;
900 buf16 = (signed short *)(localbuf + localoffs);
901 while (c)
903 locallen = (c >= LBUFCOPYSIZE ? LBUFCOPYSIZE : c);
904 if (copy_from_user(lbuf8,
905 userbuf+useroffs + p,
906 locallen))
907 return;
908 for (i = 0; i < locallen; i++)
910 buf16[p+i] = (~lbuf8[i] ^ 0x80) << 8;
912 c -= locallen; p += locallen;
914 /* used = ( samples * 8 bits size ) */
915 *used = len;
916 /* returned = ( samples * 16 bits size ) */
917 *returned = len << 1;
921 static void
922 sb16_audio_mmap(int dev)
924 sb_devc *devc = audio_devs[dev]->devc;
925 devc->fullduplex = 0;
928 static struct audio_driver sb1_audio_driver = /* SB1.x */
930 .owner = THIS_MODULE,
931 .open = sb_audio_open,
932 .close = sb_audio_close,
933 .output_block = sb_set_output_parms,
934 .start_input = sb_set_input_parms,
935 .prepare_for_input = sb1_audio_prepare_for_input,
936 .prepare_for_output = sb1_audio_prepare_for_output,
937 .halt_io = sb1_audio_halt_xfer,
938 .trigger = sb1_audio_trigger,
939 .set_speed = sb1_audio_set_speed,
940 .set_bits = sb1_audio_set_bits,
941 .set_channels = sb1_audio_set_channels
944 static struct audio_driver sb20_audio_driver = /* SB2.0 */
946 .owner = THIS_MODULE,
947 .open = sb_audio_open,
948 .close = sb_audio_close,
949 .output_block = sb_set_output_parms,
950 .start_input = sb_set_input_parms,
951 .prepare_for_input = sb1_audio_prepare_for_input,
952 .prepare_for_output = sb1_audio_prepare_for_output,
953 .halt_io = sb1_audio_halt_xfer,
954 .trigger = sb20_audio_trigger,
955 .set_speed = sb1_audio_set_speed,
956 .set_bits = sb1_audio_set_bits,
957 .set_channels = sb1_audio_set_channels
960 static struct audio_driver sb201_audio_driver = /* SB2.01 */
962 .owner = THIS_MODULE,
963 .open = sb_audio_open,
964 .close = sb_audio_close,
965 .output_block = sb_set_output_parms,
966 .start_input = sb_set_input_parms,
967 .prepare_for_input = sb1_audio_prepare_for_input,
968 .prepare_for_output = sb1_audio_prepare_for_output,
969 .halt_io = sb1_audio_halt_xfer,
970 .trigger = sb20_audio_trigger,
971 .set_speed = sb201_audio_set_speed,
972 .set_bits = sb1_audio_set_bits,
973 .set_channels = sb1_audio_set_channels
976 static struct audio_driver sbpro_audio_driver = /* SB Pro */
978 .owner = THIS_MODULE,
979 .open = sb_audio_open,
980 .close = sb_audio_close,
981 .output_block = sb_set_output_parms,
982 .start_input = sb_set_input_parms,
983 .prepare_for_input = sbpro_audio_prepare_for_input,
984 .prepare_for_output = sbpro_audio_prepare_for_output,
985 .halt_io = sb1_audio_halt_xfer,
986 .trigger = sb20_audio_trigger,
987 .set_speed = sbpro_audio_set_speed,
988 .set_bits = sb1_audio_set_bits,
989 .set_channels = sbpro_audio_set_channels
992 static struct audio_driver jazz16_audio_driver = /* Jazz16 and SM Wave */
994 .owner = THIS_MODULE,
995 .open = sb_audio_open,
996 .close = sb_audio_close,
997 .output_block = sb_set_output_parms,
998 .start_input = sb_set_input_parms,
999 .prepare_for_input = sbpro_audio_prepare_for_input,
1000 .prepare_for_output = sbpro_audio_prepare_for_output,
1001 .halt_io = sb1_audio_halt_xfer,
1002 .trigger = sb20_audio_trigger,
1003 .set_speed = jazz16_audio_set_speed,
1004 .set_bits = sb16_audio_set_bits,
1005 .set_channels = sbpro_audio_set_channels
1008 static struct audio_driver sb16_audio_driver = /* SB16 */
1010 .owner = THIS_MODULE,
1011 .open = sb_audio_open,
1012 .close = sb_audio_close,
1013 .output_block = sb_set_output_parms,
1014 .start_input = sb_set_input_parms,
1015 .prepare_for_input = sb16_audio_prepare_for_input,
1016 .prepare_for_output = sb16_audio_prepare_for_output,
1017 .halt_io = sb1_audio_halt_xfer,
1018 .copy_user = sb16_copy_from_user,
1019 .trigger = sb16_audio_trigger,
1020 .set_speed = sb16_audio_set_speed,
1021 .set_bits = sb16_audio_set_bits,
1022 .set_channels = sbpro_audio_set_channels,
1023 .mmap = sb16_audio_mmap
1026 void sb_audio_init(sb_devc * devc, char *name, struct module *owner)
1028 int audio_flags = 0;
1029 int format_mask = AFMT_U8;
1031 struct audio_driver *driver = &sb1_audio_driver;
1033 switch (devc->model)
1035 case MDL_SB1: /* SB1.0 or SB 1.5 */
1036 DDB(printk("Will use standard SB1.x driver\n"));
1037 audio_flags = DMA_HARDSTOP;
1038 break;
1040 case MDL_SB2:
1041 DDB(printk("Will use SB2.0 driver\n"));
1042 audio_flags = DMA_AUTOMODE;
1043 driver = &sb20_audio_driver;
1044 break;
1046 case MDL_SB201:
1047 DDB(printk("Will use SB2.01 (high speed) driver\n"));
1048 audio_flags = DMA_AUTOMODE;
1049 driver = &sb201_audio_driver;
1050 break;
1052 case MDL_JAZZ:
1053 case MDL_SMW:
1054 DDB(printk("Will use Jazz16 driver\n"));
1055 audio_flags = DMA_AUTOMODE;
1056 format_mask |= AFMT_S16_LE;
1057 driver = &jazz16_audio_driver;
1058 break;
1060 case MDL_ESS:
1061 DDB(printk("Will use ESS ES688/1688 driver\n"));
1062 driver = ess_audio_init (devc, &audio_flags, &format_mask);
1063 break;
1065 case MDL_SB16:
1066 DDB(printk("Will use SB16 driver\n"));
1067 audio_flags = DMA_AUTOMODE;
1068 format_mask |= AFMT_S16_LE;
1069 if (devc->dma8 != devc->dma16 && devc->dma16 != -1)
1071 audio_flags |= DMA_DUPLEX;
1072 devc->duplex = 1;
1074 driver = &sb16_audio_driver;
1075 break;
1077 default:
1078 DDB(printk("Will use SB Pro driver\n"));
1079 audio_flags = DMA_AUTOMODE;
1080 driver = &sbpro_audio_driver;
1083 if (owner)
1084 driver->owner = owner;
1086 if ((devc->dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
1087 name,driver, sizeof(struct audio_driver),
1088 audio_flags, format_mask, devc,
1089 devc->dma8,
1090 devc->duplex ? devc->dma16 : devc->dma8)) < 0)
1092 printk(KERN_ERR "Sound Blaster: unable to install audio.\n");
1093 return;
1095 audio_devs[devc->dev]->mixer_dev = devc->my_mixerdev;
1096 audio_devs[devc->dev]->min_fragment = 5;