blob: b2b3c014221afd4d6b2079c6a3627f64fcfe9480 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * sound/oss/sb_audio.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Audio routines for Sound Blaster compatible cards.
5 *
6 *
7 * Copyright (C) by Hannu Savolainen 1993-1997
8 *
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.
12 *
13 * Changes
14 * Alan Cox : Formatting and clean ups
15 *
16 * Status
17 * Mostly working. Weird uart bug causing irq storms
18 *
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
23 */
24
25#include <linux/spinlock.h>
26
27#include "sound_config.h"
28
29#include "sb_mixer.h"
30#include "sb.h"
31
32#include "sb_ess.h"
33
34int sb_audio_open(int dev, int mode)
35{
36 sb_devc *devc = audio_devs[dev]->devc;
37 unsigned long flags;
38
39 if (devc == NULL)
40 {
41 printk(KERN_ERR "Sound Blaster: incomplete initialization.\n");
42 return -ENXIO;
43 }
44 if (devc->caps & SB_NO_RECORDING && mode & OPEN_READ)
45 {
46 if (mode == OPEN_READ)
47 return -EPERM;
48 }
49 spin_lock_irqsave(&devc->lock, flags);
50 if (devc->opened)
51 {
52 spin_unlock_irqrestore(&devc->lock, flags);
53 return -EBUSY;
54 }
55 if (devc->dma16 != -1 && devc->dma16 != devc->dma8 && !devc->duplex)
56 {
57 if (sound_open_dma(devc->dma16, "Sound Blaster 16 bit"))
58 {
59 spin_unlock_irqrestore(&devc->lock, flags);
60 return -EBUSY;
61 }
62 }
63 devc->opened = mode;
64 spin_unlock_irqrestore(&devc->lock, flags);
65
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);
71
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
75 */
76 if (devc->model == MDL_ESS) ess_mixer_reload (devc, SOUND_MIXER_RECLEV);
77
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. */
84
85 if (devc->submodel == SUBMDL_ALS007)
86 {
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);
93 }
94 return 0;
95}
96
97void sb_audio_close(int dev)
98{
99 sb_devc *devc = audio_devs[dev]->devc;
100
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 {
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;
110 }
111 audio_devs[dev]->dmap_out->dma = devc->dma8;
112 audio_devs[dev]->dmap_in->dma = ( devc->duplex ) ?
113 devc->dma16 : devc->dma8;
114
115 if (devc->dma16 != -1 && devc->dma16 != devc->dma8 && !devc->duplex)
116 sound_close_dma(devc->dma16);
117
118 /* For ALS007, turn DSP output back on if closing the device for read */
119
120 if ((devc->submodel == SUBMDL_ALS007) && (devc->opened & OPEN_READ))
121 {
122 sb_setmixer(devc,ALS007_OUTPUT_CTRL2,
123 sb_getmixer(devc,ALS007_OUTPUT_CTRL2) | 0x06);
124 }
125 devc->opened = 0;
126}
127
128static void sb_set_output_parms(int dev, unsigned long buf, int nr_bytes,
129 int intrflag)
130{
131 sb_devc *devc = audio_devs[dev]->devc;
132
133 if (!devc->fullduplex || devc->bits == AFMT_S16_LE)
134 {
135 devc->trg_buf = buf;
136 devc->trg_bytes = nr_bytes;
137 devc->trg_intrflag = intrflag;
138 devc->irq_mode = IMODE_OUTPUT;
139 }
140 else
141 {
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;
146 }
147}
148
149static void sb_set_input_parms(int dev, unsigned long buf, int count, int intrflag)
150{
151 sb_devc *devc = audio_devs[dev]->devc;
152
153 if (!devc->fullduplex || devc->bits != AFMT_S16_LE)
154 {
155 devc->trg_buf = buf;
156 devc->trg_bytes = count;
157 devc->trg_intrflag = intrflag;
158 devc->irq_mode = IMODE_INPUT;
159 }
160 else
161 {
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;
166 }
167}
168
169/*
170 * SB1.x compatible routines
171 */
172
173static void sb1_audio_output_block(int dev, unsigned long buf, int nr_bytes, int intrflag)
174{
175 unsigned long flags;
176 int count = nr_bytes;
177 sb_devc *devc = audio_devs[dev]->devc;
178
179 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE); */
180
181 if (audio_devs[dev]->dmap_out->dma > 3)
182 count >>= 1;
183 count--;
184
185 devc->irq_mode = IMODE_OUTPUT;
186
187 spin_lock_irqsave(&devc->lock, flags);
188 if (sb_dsp_command(devc, 0x14)) /* 8 bit DAC using DMA */
189 {
190 sb_dsp_command(devc, (unsigned char) (count & 0xff));
191 sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
192 }
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;
197}
198
199static void sb1_audio_start_input(int dev, unsigned long buf, int nr_bytes, int intrflag)
200{
201 unsigned long flags;
202 int count = nr_bytes;
203 sb_devc *devc = audio_devs[dev]->devc;
204
205 /*
206 * Start a DMA input to the buffer pointed by dmaqtail
207 */
208
209 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ); */
210
211 if (audio_devs[dev]->dmap_out->dma > 3)
212 count >>= 1;
213 count--;
214
215 devc->irq_mode = IMODE_INPUT;
216
217 spin_lock_irqsave(&devc->lock, flags);
218 if (sb_dsp_command(devc, 0x24)) /* 8 bit ADC using DMA */
219 {
220 sb_dsp_command(devc, (unsigned char) (count & 0xff));
221 sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
222 }
223 else
224 printk(KERN_ERR "Sound Blaster: unable to start ADC.\n");
225 spin_unlock_irqrestore(&devc->lock, flags);
226
227 devc->intr_active = 1;
228}
229
230static void sb1_audio_trigger(int dev, int bits)
231{
232 sb_devc *devc = audio_devs[dev]->devc;
233
234 bits &= devc->irq_mode;
235
236 if (!bits)
237 sb_dsp_command(devc, 0xd0); /* Halt DMA */
238 else
239 {
240 switch (devc->irq_mode)
241 {
242 case IMODE_INPUT:
243 sb1_audio_start_input(dev, devc->trg_buf, devc->trg_bytes,
244 devc->trg_intrflag);
245 break;
246
247 case IMODE_OUTPUT:
248 sb1_audio_output_block(dev, devc->trg_buf, devc->trg_bytes,
249 devc->trg_intrflag);
250 break;
251 }
252 }
253 devc->trigger_bits = bits;
254}
255
256static int sb1_audio_prepare_for_input(int dev, int bsize, int bcount)
257{
258 sb_devc *devc = audio_devs[dev]->devc;
259 unsigned long flags;
260
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);
266
267 devc->trigger_bits = 0;
268 return 0;
269}
270
271static int sb1_audio_prepare_for_output(int dev, int bsize, int bcount)
272{
273 sb_devc *devc = audio_devs[dev]->devc;
274 unsigned long flags;
275
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;
283}
284
285static int sb1_audio_set_speed(int dev, int speed)
286{
287 int max_speed = 23000;
288 sb_devc *devc = audio_devs[dev]->devc;
289 int tmp;
290
291 if (devc->opened & OPEN_READ)
292 max_speed = 13000;
293
294 if (speed > 0)
295 {
296 if (speed < 4000)
297 speed = 4000;
298
299 if (speed > max_speed)
300 speed = max_speed;
301
302 devc->tconst = (256 - ((1000000 + speed / 2) / speed)) & 0xff;
303 tmp = 256 - devc->tconst;
304 speed = (1000000 + tmp / 2) / tmp;
305
306 devc->speed = speed;
307 }
308 return devc->speed;
309}
310
311static short sb1_audio_set_channels(int dev, short channels)
312{
313 sb_devc *devc = audio_devs[dev]->devc;
314 return devc->channels = 1;
315}
316
317static unsigned int sb1_audio_set_bits(int dev, unsigned int bits)
318{
319 sb_devc *devc = audio_devs[dev]->devc;
320 return devc->bits = 8;
321}
322
323static void sb1_audio_halt_xfer(int dev)
324{
325 unsigned long flags;
326 sb_devc *devc = audio_devs[dev]->devc;
327
328 spin_lock_irqsave(&devc->lock, flags);
329 sb_dsp_reset(devc);
330 spin_unlock_irqrestore(&devc->lock, flags);
331}
332
333/*
334 * SB 2.0 and SB 2.01 compatible routines
335 */
336
337static void sb20_audio_output_block(int dev, unsigned long buf, int nr_bytes,
338 int intrflag)
339{
340 unsigned long flags;
341 int count = nr_bytes;
342 sb_devc *devc = audio_devs[dev]->devc;
343 unsigned char cmd;
344
345 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE); */
346
347 if (audio_devs[dev]->dmap_out->dma > 3)
348 count >>= 1;
349 count--;
350
351 devc->irq_mode = IMODE_OUTPUT;
352
353 spin_lock_irqsave(&devc->lock, flags);
354 if (sb_dsp_command(devc, 0x48)) /* DSP Block size */
355 {
356 sb_dsp_command(devc, (unsigned char) (count & 0xff));
357 sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
358
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) */
363
364 if (!sb_dsp_command(devc, cmd))
365 printk(KERN_ERR "Sound Blaster: unable to start DAC.\n");
366 }
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;
371}
372
373static void sb20_audio_start_input(int dev, unsigned long buf, int nr_bytes, int intrflag)
374{
375 unsigned long flags;
376 int count = nr_bytes;
377 sb_devc *devc = audio_devs[dev]->devc;
378 unsigned char cmd;
379
380 /*
381 * Start a DMA input to the buffer pointed by dmaqtail
382 */
383
384 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ); */
385
386 if (audio_devs[dev]->dmap_out->dma > 3)
387 count >>= 1;
388 count--;
389
390 devc->irq_mode = IMODE_INPUT;
391
392 spin_lock_irqsave(&devc->lock, flags);
393 if (sb_dsp_command(devc, 0x48)) /* DSP Block size */
394 {
395 sb_dsp_command(devc, (unsigned char) (count & 0xff));
396 sb_dsp_command(devc, (unsigned char) ((count >> 8) & 0xff));
397
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) */
402
403 if (!sb_dsp_command(devc, cmd))
404 printk(KERN_ERR "Sound Blaster: unable to start ADC.\n");
405 }
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;
410}
411
412static void sb20_audio_trigger(int dev, int bits)
413{
414 sb_devc *devc = audio_devs[dev]->devc;
415 bits &= devc->irq_mode;
416
417 if (!bits)
418 sb_dsp_command(devc, 0xd0); /* Halt DMA */
419 else
420 {
421 switch (devc->irq_mode)
422 {
423 case IMODE_INPUT:
424 sb20_audio_start_input(dev, devc->trg_buf, devc->trg_bytes,
425 devc->trg_intrflag);
426 break;
427
428 case IMODE_OUTPUT:
429 sb20_audio_output_block(dev, devc->trg_buf, devc->trg_bytes,
430 devc->trg_intrflag);
431 break;
432 }
433 }
434 devc->trigger_bits = bits;
435}
436
437/*
438 * SB2.01 specific speed setup
439 */
440
441static int sb201_audio_set_speed(int dev, int speed)
442{
443 sb_devc *devc = audio_devs[dev]->devc;
444 int tmp;
445 int s = speed * devc->channels;
446
447 if (speed > 0)
448 {
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;
458
459 devc->speed = speed;
460 }
461 return devc->speed;
462}
463
464/*
465 * SB Pro specific routines
466 */
467
468static 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;
473
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;
477
478 if (devc->model == MDL_JAZZ || devc->model == MDL_SMW)
479 if (devc->bits == AFMT_S16_LE)
480 bits = 0x04; /* 16 bit mode */
481
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);
491
492 devc->trigger_bits = 0;
493 return 0;
494}
495
496static 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;
502
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);
507
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);
512
513 if (devc->model == MDL_JAZZ || devc->model == MDL_SMW)
514 {
515 if (devc->bits == AFMT_S16_LE)
516 bits = 0x04; /* 16 bit mode */
517
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);
523 }
524 else
525 {
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);
533 }
534 devc->trigger_bits = 0;
535 return 0;
536}
537
538static int sbpro_audio_set_speed(int dev, int speed)
539{
540 sb_devc *devc = audio_devs[dev]->devc;
541
542 if (speed > 0)
543 {
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);
551 }
552 return devc->speed;
553}
554
555static short sbpro_audio_set_channels(int dev, short channels)
556{
557 sb_devc *devc = audio_devs[dev]->devc;
558
559 if (channels == 1 || channels == 2)
560 {
561 if (channels != devc->channels)
562 {
563 devc->channels = channels;
564 if (devc->model == MDL_SBPRO && devc->channels == 2)
565 sbpro_audio_set_speed(dev, devc->speed);
566 }
567 }
568 return devc->channels;
569}
570
571static int jazz16_audio_set_speed(int dev, int speed)
572{
573 sb_devc *devc = audio_devs[dev]->devc;
574
575 if (speed > 0)
576 {
577 int tmp;
Dan Carpenter94f3ec62012-08-18 18:55:15 +0300578 int s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 if (speed < 5000)
581 speed = 5000;
582 if (speed > 44100)
583 speed = 44100;
584
Dan Carpenter94f3ec62012-08-18 18:55:15 +0300585 s = speed * devc->channels;
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 devc->tconst = (256 - ((1000000 + s / 2) / s)) & 0xff;
588
589 tmp = 256 - devc->tconst;
590 speed = ((1000000 + tmp / 2) / tmp) / devc->channels;
591
592 devc->speed = speed;
593 }
594 return devc->speed;
595}
596
597/*
598 * SB16 specific routines
599 */
600
601static int sb16_audio_set_speed(int dev, int speed)
602{
603 sb_devc *devc = audio_devs[dev]->devc;
604 int max_speed = devc->submodel == SUBMDL_ALS100 ? 48000 : 44100;
605
606 if (speed > 0)
607 {
608 if (speed < 5000)
609 speed = 5000;
610
611 if (speed > max_speed)
612 speed = max_speed;
613
614 devc->speed = speed;
615 }
616 return devc->speed;
617}
618
619static unsigned int sb16_audio_set_bits(int dev, unsigned int bits)
620{
621 sb_devc *devc = audio_devs[dev]->devc;
622
623 if (bits != 0)
624 {
625 if (bits == AFMT_U8 || bits == AFMT_S16_LE)
626 devc->bits = bits;
627 else
628 devc->bits = AFMT_U8;
629 }
630
631 return devc->bits;
632}
633
634static int sb16_audio_prepare_for_input(int dev, int bsize, int bcount)
635{
636 sb_devc *devc = audio_devs[dev]->devc;
637
638 if (!devc->fullduplex)
639 {
640 audio_devs[dev]->dmap_out->dma =
641 audio_devs[dev]->dmap_in->dma =
642 devc->bits == AFMT_S16_LE ?
643 devc->dma16 : devc->dma8;
644 }
645 else if (devc->bits == AFMT_S16_LE)
646 {
647 audio_devs[dev]->dmap_out->dma = devc->dma8;
648 audio_devs[dev]->dmap_in->dma = devc->dma16;
649 }
650 else
651 {
652 audio_devs[dev]->dmap_out->dma = devc->dma16;
653 audio_devs[dev]->dmap_in->dma = devc->dma8;
654 }
655
656 devc->trigger_bits = 0;
657 return 0;
658}
659
660static int sb16_audio_prepare_for_output(int dev, int bsize, int bcount)
661{
662 sb_devc *devc = audio_devs[dev]->devc;
663
664 if (!devc->fullduplex)
665 {
666 audio_devs[dev]->dmap_out->dma =
667 audio_devs[dev]->dmap_in->dma =
668 devc->bits == AFMT_S16_LE ?
669 devc->dma16 : devc->dma8;
670 }
671 else if (devc->bits == AFMT_S16_LE)
672 {
673 audio_devs[dev]->dmap_out->dma = devc->dma8;
674 audio_devs[dev]->dmap_in->dma = devc->dma16;
675 }
676 else
677 {
678 audio_devs[dev]->dmap_out->dma = devc->dma16;
679 audio_devs[dev]->dmap_in->dma = devc->dma8;
680 }
681
682 devc->trigger_bits = 0;
683 return 0;
684}
685
686static void sb16_audio_output_block(int dev, unsigned long buf, int count,
687 int intrflag)
688{
689 unsigned long flags, cnt;
690 sb_devc *devc = audio_devs[dev]->devc;
691 unsigned long bits;
692
693 if (!devc->fullduplex || devc->bits == AFMT_S16_LE)
694 {
695 devc->irq_mode = IMODE_OUTPUT;
696 devc->intr_active = 1;
697 }
698 else
699 {
700 devc->irq_mode_16 = IMODE_OUTPUT;
701 devc->intr_active_16 = 1;
702 }
703
704 /* save value */
705 spin_lock_irqsave(&devc->lock, flags);
706 bits = devc->bits;
707 if (devc->fullduplex)
708 devc->bits = (devc->bits == AFMT_S16_LE) ?
709 AFMT_U8 : AFMT_S16_LE;
710 spin_unlock_irqrestore(&devc->lock, flags);
711
712 cnt = count;
713 if (devc->bits == AFMT_S16_LE)
714 cnt >>= 1;
715 cnt--;
716
717 spin_lock_irqsave(&devc->lock, flags);
718
719 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE); */
720
721 sb_dsp_command(devc, 0x41);
722 sb_dsp_command(devc, (unsigned char) ((devc->speed >> 8) & 0xff));
723 sb_dsp_command(devc, (unsigned char) (devc->speed & 0xff));
724
725 sb_dsp_command(devc, (devc->bits == AFMT_S16_LE ? 0xb6 : 0xc6));
726 sb_dsp_command(devc, ((devc->channels == 2 ? 0x20 : 0) +
727 (devc->bits == AFMT_S16_LE ? 0x10 : 0)));
728 sb_dsp_command(devc, (unsigned char) (cnt & 0xff));
729 sb_dsp_command(devc, (unsigned char) (cnt >> 8));
730
731 /* restore real value after all programming */
732 devc->bits = bits;
733 spin_unlock_irqrestore(&devc->lock, flags);
734}
735
736
737/*
738 * This fails on the Cyrix MediaGX. If you don't have the DMA enabled
739 * before the first sample arrives it locks up. However even if you
740 * do enable the DMA in time you just get DMA timeouts and missing
741 * interrupts and stuff, so for now I've not bothered fixing this either.
742 */
743
744static void sb16_audio_start_input(int dev, unsigned long buf, int count, int intrflag)
745{
746 unsigned long flags, cnt;
747 sb_devc *devc = audio_devs[dev]->devc;
748
749 if (!devc->fullduplex || devc->bits != AFMT_S16_LE)
750 {
751 devc->irq_mode = IMODE_INPUT;
752 devc->intr_active = 1;
753 }
754 else
755 {
756 devc->irq_mode_16 = IMODE_INPUT;
757 devc->intr_active_16 = 1;
758 }
759
760 cnt = count;
761 if (devc->bits == AFMT_S16_LE)
762 cnt >>= 1;
763 cnt--;
764
765 spin_lock_irqsave(&devc->lock, flags);
766
767 /* DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ); */
768
769 sb_dsp_command(devc, 0x42);
770 sb_dsp_command(devc, (unsigned char) ((devc->speed >> 8) & 0xff));
771 sb_dsp_command(devc, (unsigned char) (devc->speed & 0xff));
772
773 sb_dsp_command(devc, (devc->bits == AFMT_S16_LE ? 0xbe : 0xce));
774 sb_dsp_command(devc, ((devc->channels == 2 ? 0x20 : 0) +
775 (devc->bits == AFMT_S16_LE ? 0x10 : 0)));
776 sb_dsp_command(devc, (unsigned char) (cnt & 0xff));
777 sb_dsp_command(devc, (unsigned char) (cnt >> 8));
778
779 spin_unlock_irqrestore(&devc->lock, flags);
780}
781
782static void sb16_audio_trigger(int dev, int bits)
783{
784 sb_devc *devc = audio_devs[dev]->devc;
785
786 int bits_16 = bits & devc->irq_mode_16;
787 bits &= devc->irq_mode;
788
789 if (!bits && !bits_16)
790 sb_dsp_command(devc, 0xd0); /* Halt DMA */
791 else
792 {
793 if (bits)
794 {
795 switch (devc->irq_mode)
796 {
797 case IMODE_INPUT:
798 sb16_audio_start_input(dev,
799 devc->trg_buf,
800 devc->trg_bytes,
801 devc->trg_intrflag);
802 break;
803
804 case IMODE_OUTPUT:
805 sb16_audio_output_block(dev,
806 devc->trg_buf,
807 devc->trg_bytes,
808 devc->trg_intrflag);
809 break;
810 }
811 }
812 if (bits_16)
813 {
814 switch (devc->irq_mode_16)
815 {
816 case IMODE_INPUT:
817 sb16_audio_start_input(dev,
818 devc->trg_buf_16,
819 devc->trg_bytes_16,
820 devc->trg_intrflag_16);
821 break;
822
823 case IMODE_OUTPUT:
824 sb16_audio_output_block(dev,
825 devc->trg_buf_16,
826 devc->trg_bytes_16,
827 devc->trg_intrflag_16);
828 break;
829 }
830 }
831 }
832
833 devc->trigger_bits = bits | bits_16;
834}
835
836static unsigned char lbuf8[2048];
837static signed short *lbuf16 = (signed short *)lbuf8;
838#define LBUFCOPYSIZE 1024
839static void
840sb16_copy_from_user(int dev,
841 char *localbuf, int localoffs,
842 const char __user *userbuf, int useroffs,
843 int max_in, int max_out,
844 int *used, int *returned,
845 int len)
846{
847 sb_devc *devc = audio_devs[dev]->devc;
848 int i, c, p, locallen;
849 unsigned char *buf8;
850 signed short *buf16;
851
852 /* if not duplex no conversion */
853 if (!devc->fullduplex)
854 {
855 if (copy_from_user(localbuf + localoffs,
856 userbuf + useroffs, len))
857 return;
858 *used = len;
859 *returned = len;
860 }
861 else if (devc->bits == AFMT_S16_LE)
862 {
863 /* 16 -> 8 */
864 /* max_in >> 1, max number of samples in ( 16 bits ) */
865 /* max_out, max number of samples out ( 8 bits ) */
866 /* len, number of samples that will be taken ( 16 bits )*/
867 /* c, count of samples remaining in buffer ( 16 bits )*/
868 /* p, count of samples already processed ( 16 bits )*/
869 len = ( (max_in >> 1) > max_out) ? max_out : (max_in >> 1);
870 c = len;
871 p = 0;
872 buf8 = (unsigned char *)(localbuf + localoffs);
873 while (c)
874 {
875 locallen = (c >= LBUFCOPYSIZE ? LBUFCOPYSIZE : c);
876 /* << 1 in order to get 16 bit samples */
877 if (copy_from_user(lbuf16,
878 userbuf + useroffs + (p << 1),
879 locallen << 1))
880 return;
881 for (i = 0; i < locallen; i++)
882 {
883 buf8[p+i] = ~((lbuf16[i] >> 8) & 0xff) ^ 0x80;
884 }
885 c -= locallen; p += locallen;
886 }
887 /* used = ( samples * 16 bits size ) */
888 *used = max_in > ( max_out << 1) ? (max_out << 1) : max_in;
889 /* returned = ( samples * 8 bits size ) */
890 *returned = len;
891 }
892 else
893 {
894 /* 8 -> 16 */
895 /* max_in, max number of samples in ( 8 bits ) */
896 /* max_out >> 1, max number of samples out ( 16 bits ) */
897 /* len, number of samples that will be taken ( 8 bits )*/
898 /* c, count of samples remaining in buffer ( 8 bits )*/
899 /* p, count of samples already processed ( 8 bits )*/
900 len = max_in > (max_out >> 1) ? (max_out >> 1) : max_in;
901 c = len;
902 p = 0;
903 buf16 = (signed short *)(localbuf + localoffs);
904 while (c)
905 {
906 locallen = (c >= LBUFCOPYSIZE ? LBUFCOPYSIZE : c);
907 if (copy_from_user(lbuf8,
908 userbuf+useroffs + p,
909 locallen))
910 return;
911 for (i = 0; i < locallen; i++)
912 {
913 buf16[p+i] = (~lbuf8[i] ^ 0x80) << 8;
914 }
915 c -= locallen; p += locallen;
916 }
917 /* used = ( samples * 8 bits size ) */
918 *used = len;
919 /* returned = ( samples * 16 bits size ) */
920 *returned = len << 1;
921 }
922}
923
924static void
925sb16_audio_mmap(int dev)
926{
927 sb_devc *devc = audio_devs[dev]->devc;
928 devc->fullduplex = 0;
929}
930
931static struct audio_driver sb1_audio_driver = /* SB1.x */
932{
933 .owner = THIS_MODULE,
934 .open = sb_audio_open,
935 .close = sb_audio_close,
936 .output_block = sb_set_output_parms,
937 .start_input = sb_set_input_parms,
938 .prepare_for_input = sb1_audio_prepare_for_input,
939 .prepare_for_output = sb1_audio_prepare_for_output,
940 .halt_io = sb1_audio_halt_xfer,
941 .trigger = sb1_audio_trigger,
942 .set_speed = sb1_audio_set_speed,
943 .set_bits = sb1_audio_set_bits,
944 .set_channels = sb1_audio_set_channels
945};
946
947static struct audio_driver sb20_audio_driver = /* SB2.0 */
948{
949 .owner = THIS_MODULE,
950 .open = sb_audio_open,
951 .close = sb_audio_close,
952 .output_block = sb_set_output_parms,
953 .start_input = sb_set_input_parms,
954 .prepare_for_input = sb1_audio_prepare_for_input,
955 .prepare_for_output = sb1_audio_prepare_for_output,
956 .halt_io = sb1_audio_halt_xfer,
957 .trigger = sb20_audio_trigger,
958 .set_speed = sb1_audio_set_speed,
959 .set_bits = sb1_audio_set_bits,
960 .set_channels = sb1_audio_set_channels
961};
962
963static struct audio_driver sb201_audio_driver = /* SB2.01 */
964{
965 .owner = THIS_MODULE,
966 .open = sb_audio_open,
967 .close = sb_audio_close,
968 .output_block = sb_set_output_parms,
969 .start_input = sb_set_input_parms,
970 .prepare_for_input = sb1_audio_prepare_for_input,
971 .prepare_for_output = sb1_audio_prepare_for_output,
972 .halt_io = sb1_audio_halt_xfer,
973 .trigger = sb20_audio_trigger,
974 .set_speed = sb201_audio_set_speed,
975 .set_bits = sb1_audio_set_bits,
976 .set_channels = sb1_audio_set_channels
977};
978
979static struct audio_driver sbpro_audio_driver = /* SB Pro */
980{
981 .owner = THIS_MODULE,
982 .open = sb_audio_open,
983 .close = sb_audio_close,
984 .output_block = sb_set_output_parms,
985 .start_input = sb_set_input_parms,
986 .prepare_for_input = sbpro_audio_prepare_for_input,
987 .prepare_for_output = sbpro_audio_prepare_for_output,
988 .halt_io = sb1_audio_halt_xfer,
989 .trigger = sb20_audio_trigger,
990 .set_speed = sbpro_audio_set_speed,
991 .set_bits = sb1_audio_set_bits,
992 .set_channels = sbpro_audio_set_channels
993};
994
995static struct audio_driver jazz16_audio_driver = /* Jazz16 and SM Wave */
996{
997 .owner = THIS_MODULE,
998 .open = sb_audio_open,
999 .close = sb_audio_close,
1000 .output_block = sb_set_output_parms,
1001 .start_input = sb_set_input_parms,
1002 .prepare_for_input = sbpro_audio_prepare_for_input,
1003 .prepare_for_output = sbpro_audio_prepare_for_output,
1004 .halt_io = sb1_audio_halt_xfer,
1005 .trigger = sb20_audio_trigger,
1006 .set_speed = jazz16_audio_set_speed,
1007 .set_bits = sb16_audio_set_bits,
1008 .set_channels = sbpro_audio_set_channels
1009};
1010
1011static struct audio_driver sb16_audio_driver = /* SB16 */
1012{
1013 .owner = THIS_MODULE,
1014 .open = sb_audio_open,
1015 .close = sb_audio_close,
1016 .output_block = sb_set_output_parms,
1017 .start_input = sb_set_input_parms,
1018 .prepare_for_input = sb16_audio_prepare_for_input,
1019 .prepare_for_output = sb16_audio_prepare_for_output,
1020 .halt_io = sb1_audio_halt_xfer,
1021 .copy_user = sb16_copy_from_user,
1022 .trigger = sb16_audio_trigger,
1023 .set_speed = sb16_audio_set_speed,
1024 .set_bits = sb16_audio_set_bits,
1025 .set_channels = sbpro_audio_set_channels,
1026 .mmap = sb16_audio_mmap
1027};
1028
1029void sb_audio_init(sb_devc * devc, char *name, struct module *owner)
1030{
1031 int audio_flags = 0;
1032 int format_mask = AFMT_U8;
1033
1034 struct audio_driver *driver = &sb1_audio_driver;
1035
1036 switch (devc->model)
1037 {
1038 case MDL_SB1: /* SB1.0 or SB 1.5 */
1039 DDB(printk("Will use standard SB1.x driver\n"));
1040 audio_flags = DMA_HARDSTOP;
1041 break;
1042
1043 case MDL_SB2:
1044 DDB(printk("Will use SB2.0 driver\n"));
1045 audio_flags = DMA_AUTOMODE;
1046 driver = &sb20_audio_driver;
1047 break;
1048
1049 case MDL_SB201:
1050 DDB(printk("Will use SB2.01 (high speed) driver\n"));
1051 audio_flags = DMA_AUTOMODE;
1052 driver = &sb201_audio_driver;
1053 break;
1054
1055 case MDL_JAZZ:
1056 case MDL_SMW:
1057 DDB(printk("Will use Jazz16 driver\n"));
1058 audio_flags = DMA_AUTOMODE;
1059 format_mask |= AFMT_S16_LE;
1060 driver = &jazz16_audio_driver;
1061 break;
1062
1063 case MDL_ESS:
1064 DDB(printk("Will use ESS ES688/1688 driver\n"));
1065 driver = ess_audio_init (devc, &audio_flags, &format_mask);
1066 break;
1067
1068 case MDL_SB16:
1069 DDB(printk("Will use SB16 driver\n"));
1070 audio_flags = DMA_AUTOMODE;
1071 format_mask |= AFMT_S16_LE;
1072 if (devc->dma8 != devc->dma16 && devc->dma16 != -1)
1073 {
1074 audio_flags |= DMA_DUPLEX;
1075 devc->duplex = 1;
1076 }
1077 driver = &sb16_audio_driver;
1078 break;
1079
1080 default:
1081 DDB(printk("Will use SB Pro driver\n"));
1082 audio_flags = DMA_AUTOMODE;
1083 driver = &sbpro_audio_driver;
1084 }
1085
1086 if (owner)
1087 driver->owner = owner;
1088
1089 if ((devc->dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
1090 name,driver, sizeof(struct audio_driver),
1091 audio_flags, format_mask, devc,
1092 devc->dma8,
1093 devc->duplex ? devc->dma16 : devc->dma8)) < 0)
1094 {
1095 printk(KERN_ERR "Sound Blaster: unable to install audio.\n");
1096 return;
1097 }
1098 audio_devs[devc->dev]->mixer_dev = devc->my_mixerdev;
1099 audio_devs[devc->dev]->min_fragment = 5;
1100}