1 /*
2  * sound/sb_audio.c
3  *
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 
sb_audio_open(int dev,int mode)34 int 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 
sb_audio_close(int dev)97 void 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 
sb_set_output_parms(int dev,unsigned long buf,int nr_bytes,int intrflag)128 static 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 
sb_set_input_parms(int dev,unsigned long buf,int count,int intrflag)149 static 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 
sb1_audio_output_block(int dev,unsigned long buf,int nr_bytes,int intrflag)173 static 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 
sb1_audio_start_input(int dev,unsigned long buf,int nr_bytes,int intrflag)199 static 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 
sb1_audio_trigger(int dev,int bits)230 static 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 
sb1_audio_prepare_for_input(int dev,int bsize,int bcount)256 static 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 
sb1_audio_prepare_for_output(int dev,int bsize,int bcount)271 static 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 
sb1_audio_set_speed(int dev,int speed)285 static 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 
sb1_audio_set_channels(int dev,short channels)311 static 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 
sb1_audio_set_bits(int dev,unsigned int bits)317 static 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 
sb1_audio_halt_xfer(int dev)323 static 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 
sb20_audio_output_block(int dev,unsigned long buf,int nr_bytes,int intrflag)337 static 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 
sb20_audio_start_input(int dev,unsigned long buf,int nr_bytes,int intrflag)373 static 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 
sb20_audio_trigger(int dev,int bits)412 static 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 
sb201_audio_set_speed(int dev,int speed)441 static 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 
sbpro_audio_prepare_for_input(int dev,int bsize,int bcount)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;
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 
sbpro_audio_prepare_for_output(int dev,int bsize,int bcount)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;
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 	}
523 	else
524 	{
525 		tmp = sb_getmixer(devc, 0x0e);
526 		if (devc->channels == 1)
527 			tmp &= ~0x02;
528 		else
529 			tmp |= 0x02;
530 		sb_setmixer(devc, 0x0e, tmp);
531 	}
532 	spin_unlock_irqrestore(&devc->lock, flags);
533 	devc->trigger_bits = 0;
534 	return 0;
535 }
536 
sbpro_audio_set_speed(int dev,int speed)537 static int sbpro_audio_set_speed(int dev, int speed)
538 {
539 	sb_devc *devc = audio_devs[dev]->devc;
540 
541 	if (speed > 0)
542 	{
543 		if (speed < 4000)
544 			speed = 4000;
545 		if (speed > 44100)
546 			speed = 44100;
547 		if (devc->channels > 1 && speed > 22050)
548 			speed = 22050;
549 		sb201_audio_set_speed(dev, speed);
550 	}
551 	return devc->speed;
552 }
553 
sbpro_audio_set_channels(int dev,short channels)554 static short sbpro_audio_set_channels(int dev, short channels)
555 {
556 	sb_devc *devc = audio_devs[dev]->devc;
557 
558 	if (channels == 1 || channels == 2)
559 	{
560 		if (channels != devc->channels)
561 		{
562 			devc->channels = channels;
563 			if (devc->model == MDL_SBPRO && devc->channels == 2)
564 				sbpro_audio_set_speed(dev, devc->speed);
565 		}
566 	}
567 	return devc->channels;
568 }
569 
jazz16_audio_set_speed(int dev,int speed)570 static int jazz16_audio_set_speed(int dev, int speed)
571 {
572 	sb_devc *devc = audio_devs[dev]->devc;
573 
574 	if (speed > 0)
575 	{
576 		int tmp;
577 		int s = speed * devc->channels;
578 
579 		if (speed < 5000)
580 			speed = 5000;
581 		if (speed > 44100)
582 			speed = 44100;
583 
584 		devc->tconst = (256 - ((1000000 + s / 2) / s)) & 0xff;
585 
586 		tmp = 256 - devc->tconst;
587 		speed = ((1000000 + tmp / 2) / tmp) / devc->channels;
588 
589 		devc->speed = speed;
590 	}
591 	return devc->speed;
592 }
593 
594 /*
595  * SB16 specific routines
596  */
597 
sb16_audio_set_speed(int dev,int speed)598 static int sb16_audio_set_speed(int dev, int speed)
599 {
600 	sb_devc *devc = audio_devs[dev]->devc;
601 	int	max_speed = devc->submodel == SUBMDL_ALS100 ? 48000 : 44100;
602 
603 	if (speed > 0)
604 	{
605 		if (speed < 5000)
606 			speed = 5000;
607 
608 		if (speed > max_speed)
609 			speed = max_speed;
610 
611 		devc->speed = speed;
612 	}
613 	return devc->speed;
614 }
615 
sb16_audio_set_bits(int dev,unsigned int bits)616 static unsigned int sb16_audio_set_bits(int dev, unsigned int bits)
617 {
618 	sb_devc *devc = audio_devs[dev]->devc;
619 
620 	if (bits != 0)
621 	{
622 		if (bits == AFMT_U8 || bits == AFMT_S16_LE)
623 			devc->bits = bits;
624 		else
625 			devc->bits = AFMT_U8;
626 	}
627 
628 	return devc->bits;
629 }
630 
sb16_audio_prepare_for_input(int dev,int bsize,int bcount)631 static int sb16_audio_prepare_for_input(int dev, int bsize, int bcount)
632 {
633 	sb_devc *devc = audio_devs[dev]->devc;
634 
635 	if (!devc->fullduplex)
636 	{
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;
641 	}
642 	else if (devc->bits == AFMT_S16_LE)
643 	{
644 		audio_devs[dev]->dmap_out->dma = devc->dma8;
645 		audio_devs[dev]->dmap_in->dma = devc->dma16;
646 	}
647 	else
648 	{
649 		audio_devs[dev]->dmap_out->dma = devc->dma16;
650 		audio_devs[dev]->dmap_in->dma = devc->dma8;
651 	}
652 
653 	devc->trigger_bits = 0;
654 	return 0;
655 }
656 
sb16_audio_prepare_for_output(int dev,int bsize,int bcount)657 static int sb16_audio_prepare_for_output(int dev, int bsize, int bcount)
658 {
659 	sb_devc *devc = audio_devs[dev]->devc;
660 
661 	if (!devc->fullduplex)
662 	{
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;
667 	}
668 	else if (devc->bits == AFMT_S16_LE)
669 	{
670 		audio_devs[dev]->dmap_out->dma = devc->dma8;
671 		audio_devs[dev]->dmap_in->dma = devc->dma16;
672 	}
673 	else
674 	{
675 		audio_devs[dev]->dmap_out->dma = devc->dma16;
676 		audio_devs[dev]->dmap_in->dma = devc->dma8;
677 	}
678 
679 	devc->trigger_bits = 0;
680 	return 0;
681 }
682 
sb16_audio_output_block(int dev,unsigned long buf,int count,int intrflag)683 static void sb16_audio_output_block(int dev, unsigned long buf, int count,
684 			int intrflag)
685 {
686 	unsigned long   flags, cnt;
687 	sb_devc        *devc = audio_devs[dev]->devc;
688 	unsigned long   bits;
689 
690 	if (!devc->fullduplex || devc->bits == AFMT_S16_LE)
691 	{
692 		devc->irq_mode = IMODE_OUTPUT;
693 		devc->intr_active = 1;
694 	}
695 	else
696 	{
697 		devc->irq_mode_16 = IMODE_OUTPUT;
698 		devc->intr_active_16 = 1;
699 	}
700 
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);
708 
709 	cnt = count;
710 	if (devc->bits == AFMT_S16_LE)
711 		cnt >>= 1;
712 	cnt--;
713 
714 	spin_lock_irqsave(&devc->lock, flags);
715 
716 	/* DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE); */
717 
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));
721 
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));
727 
728 	/* restore real value after all programming */
729 	devc->bits = bits;
730 	spin_unlock_irqrestore(&devc->lock, flags);
731 }
732 
733 
734 /*
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.
739  */
740 
sb16_audio_start_input(int dev,unsigned long buf,int count,int intrflag)741 static void sb16_audio_start_input(int dev, unsigned long buf, int count, int intrflag)
742 {
743 	unsigned long   flags, cnt;
744 	sb_devc        *devc = audio_devs[dev]->devc;
745 
746 	if (!devc->fullduplex || devc->bits != AFMT_S16_LE)
747 	{
748 		devc->irq_mode = IMODE_INPUT;
749 		devc->intr_active = 1;
750 	}
751 	else
752 	{
753 		devc->irq_mode_16 = IMODE_INPUT;
754 		devc->intr_active_16 = 1;
755 	}
756 
757 	cnt = count;
758 	if (devc->bits == AFMT_S16_LE)
759 		cnt >>= 1;
760 	cnt--;
761 
762 	spin_lock_irqsave(&devc->lock, flags);
763 
764 	/* DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ); */
765 
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));
769 
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));
775 
776 	spin_unlock_irqrestore(&devc->lock, flags);
777 }
778 
sb16_audio_trigger(int dev,int bits)779 static void sb16_audio_trigger(int dev, int bits)
780 {
781 	sb_devc *devc = audio_devs[dev]->devc;
782 
783 	int bits_16 = bits & devc->irq_mode_16;
784 	bits &= devc->irq_mode;
785 
786 	if (!bits && !bits_16)
787 		sb_dsp_command(devc, 0xd0);	/* Halt DMA */
788 	else
789 	{
790 		if (bits)
791 		{
792 			switch (devc->irq_mode)
793 			{
794 				case IMODE_INPUT:
795 					sb16_audio_start_input(dev,
796 							devc->trg_buf,
797 							devc->trg_bytes,
798 							devc->trg_intrflag);
799 					break;
800 
801 				case IMODE_OUTPUT:
802 					sb16_audio_output_block(dev,
803 							devc->trg_buf,
804 							devc->trg_bytes,
805 							devc->trg_intrflag);
806 					break;
807 			}
808 		}
809 		if (bits_16)
810 		{
811 			switch (devc->irq_mode_16)
812 			{
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;
819 
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;
826 			}
827 		}
828 	}
829 
830 	devc->trigger_bits = bits | bits_16;
831 }
832 
833 static unsigned char lbuf8[2048];
834 static signed short *lbuf16 = (signed short *)lbuf8;
835 #define LBUFCOPYSIZE 1024
836 static void
sb16_copy_from_user(int dev,char * localbuf,int localoffs,const char * userbuf,int useroffs,int max_in,int max_out,int * used,int * returned,int len)837 sb16_copy_from_user(int dev,
838 		char *localbuf, int localoffs,
839 		const char *userbuf, int useroffs,
840 		int max_in, int max_out,
841 		int *used, int *returned,
842 		int len)
843 {
844 	sb_devc       *devc = audio_devs[dev]->devc;
845 	int           i, c, p, locallen;
846 	unsigned char *buf8;
847 	signed short  *buf16;
848 
849 	/* if not duplex no conversion */
850 	if (!devc->fullduplex)
851 	{
852 		copy_from_user (localbuf + localoffs, userbuf + useroffs, len);
853 		*used = len;
854 		*returned = len;
855 	}
856 	else if (devc->bits == AFMT_S16_LE)
857 	{
858 		/* 16 -> 8 */
859 		/* max_in >> 1, max number of samples in ( 16 bits ) */
860 		/* max_out, max number of samples out ( 8 bits ) */
861 		/* len, number of samples that will be taken ( 16 bits )*/
862 		/* c, count of samples remaining in buffer ( 16 bits )*/
863 		/* p, count of samples already processed ( 16 bits )*/
864 		len = ( (max_in >> 1) > max_out) ? max_out : (max_in >> 1);
865 		c = len;
866 		p = 0;
867 		buf8 = (unsigned char *)(localbuf + localoffs);
868 		while (c)
869 		{
870 			locallen = (c >= LBUFCOPYSIZE ? LBUFCOPYSIZE : c);
871 			/* << 1 in order to get 16 bit samples */
872 			copy_from_user (lbuf16,
873 					userbuf+useroffs + (p << 1),
874 					locallen << 1);
875 			for (i = 0; i < locallen; i++)
876 			{
877 				buf8[p+i] = ~((lbuf16[i] >> 8) & 0xff) ^ 0x80;
878 			}
879 			c -= locallen; p += locallen;
880 		}
881 		/* used = ( samples * 16 bits size ) */
882 		*used =  max_in  > ( max_out << 1) ? (max_out << 1) : max_in;
883 		/* returned = ( samples * 8 bits size ) */
884 		*returned = len;
885 	}
886 	else
887 	{
888 		/* 8 -> 16 */
889 		/* max_in, max number of samples in ( 8 bits ) */
890 		/* max_out >> 1, max number of samples out ( 16 bits ) */
891 		/* len, number of samples that will be taken ( 8 bits )*/
892 		/* c, count of samples remaining in buffer ( 8 bits )*/
893 		/* p, count of samples already processed ( 8 bits )*/
894 		len = max_in > (max_out >> 1) ? (max_out >> 1) : max_in;
895 		c = len;
896 		p = 0;
897 		buf16 = (signed short *)(localbuf + localoffs);
898 		while (c)
899 		{
900 			locallen = (c >= LBUFCOPYSIZE ? LBUFCOPYSIZE : c);
901 			copy_from_user (lbuf8,
902 					userbuf+useroffs + p,
903 					locallen);
904 			for (i = 0; i < locallen; i++)
905 			{
906 				buf16[p+i] = (~lbuf8[i] ^ 0x80) << 8;
907 			}
908 	      		c -= locallen; p += locallen;
909 		}
910 		/* used = ( samples * 8 bits size ) */
911 		*used = len;
912 		/* returned = ( samples * 16 bits size ) */
913 		*returned = len << 1;
914 	}
915 }
916 
917 static void
sb16_audio_mmap(int dev)918 sb16_audio_mmap(int dev)
919 {
920 	sb_devc       *devc = audio_devs[dev]->devc;
921 	devc->fullduplex = 0;
922 }
923 
924 static struct audio_driver sb1_audio_driver =	/* SB1.x */
925 {
926 	owner:		THIS_MODULE,
927 	open:		sb_audio_open,
928 	close:		sb_audio_close,
929 	output_block:	sb_set_output_parms,
930 	start_input:	sb_set_input_parms,
931 	prepare_for_input:	sb1_audio_prepare_for_input,
932 	prepare_for_output:	sb1_audio_prepare_for_output,
933 	halt_io:	sb1_audio_halt_xfer,
934 	trigger:	sb1_audio_trigger,
935 	set_speed:	sb1_audio_set_speed,
936 	set_bits:	sb1_audio_set_bits,
937 	set_channels:	sb1_audio_set_channels
938 };
939 
940 static struct audio_driver sb20_audio_driver =	/* SB2.0 */
941 {
942 	owner:		THIS_MODULE,
943 	open:		sb_audio_open,
944 	close:		sb_audio_close,
945 	output_block:	sb_set_output_parms,
946 	start_input:	sb_set_input_parms,
947 	prepare_for_input:	sb1_audio_prepare_for_input,
948 	prepare_for_output:	sb1_audio_prepare_for_output,
949 	halt_io:	sb1_audio_halt_xfer,
950 	trigger:	sb20_audio_trigger,
951 	set_speed:	sb1_audio_set_speed,
952 	set_bits:	sb1_audio_set_bits,
953 	set_channels:	sb1_audio_set_channels
954 };
955 
956 static struct audio_driver sb201_audio_driver =		/* SB2.01 */
957 {
958 	owner:		THIS_MODULE,
959 	open:		sb_audio_open,
960 	close:		sb_audio_close,
961 	output_block:	sb_set_output_parms,
962 	start_input:	sb_set_input_parms,
963 	prepare_for_input:	sb1_audio_prepare_for_input,
964 	prepare_for_output:	sb1_audio_prepare_for_output,
965 	halt_io:	sb1_audio_halt_xfer,
966 	trigger:	sb20_audio_trigger,
967 	set_speed:	sb201_audio_set_speed,
968 	set_bits:	sb1_audio_set_bits,
969 	set_channels:	sb1_audio_set_channels
970 };
971 
972 static struct audio_driver sbpro_audio_driver =		/* SB Pro */
973 {
974 	owner:		THIS_MODULE,
975 	open:		sb_audio_open,
976 	close:		sb_audio_close,
977 	output_block:	sb_set_output_parms,
978 	start_input:	sb_set_input_parms,
979 	prepare_for_input:	sbpro_audio_prepare_for_input,
980 	prepare_for_output:	sbpro_audio_prepare_for_output,
981 	halt_io:	sb1_audio_halt_xfer,
982 	trigger:	sb20_audio_trigger,
983 	set_speed:	sbpro_audio_set_speed,
984 	set_bits:	sb1_audio_set_bits,
985 	set_channels:	sbpro_audio_set_channels
986 };
987 
988 static struct audio_driver jazz16_audio_driver =	/* Jazz16 and SM Wave */
989 {
990 	owner:		THIS_MODULE,
991 	open:		sb_audio_open,
992 	close:		sb_audio_close,
993 	output_block:	sb_set_output_parms,
994 	start_input:	sb_set_input_parms,
995 	prepare_for_input:	sbpro_audio_prepare_for_input,
996 	prepare_for_output:	sbpro_audio_prepare_for_output,
997 	halt_io:	sb1_audio_halt_xfer,
998 	trigger:	sb20_audio_trigger,
999 	set_speed:	jazz16_audio_set_speed,
1000 	set_bits:	sb16_audio_set_bits,
1001 	set_channels:	sbpro_audio_set_channels
1002 };
1003 
1004 static struct audio_driver sb16_audio_driver =	/* SB16 */
1005 {
1006 	owner:		THIS_MODULE,
1007 	open:		sb_audio_open,
1008 	close:		sb_audio_close,
1009 	output_block:	sb_set_output_parms,
1010 	start_input:	sb_set_input_parms,
1011 	prepare_for_input:	sb16_audio_prepare_for_input,
1012 	prepare_for_output:	sb16_audio_prepare_for_output,
1013 	halt_io:	sb1_audio_halt_xfer,
1014 	copy_user:	sb16_copy_from_user,
1015 	trigger:	sb16_audio_trigger,
1016 	set_speed:	sb16_audio_set_speed,
1017 	set_bits:	sb16_audio_set_bits,
1018 	set_channels:	sbpro_audio_set_channels,
1019 	mmap:		sb16_audio_mmap
1020 };
1021 
sb_audio_init(sb_devc * devc,char * name,struct module * owner)1022 void sb_audio_init(sb_devc * devc, char *name, struct module *owner)
1023 {
1024 	int audio_flags = 0;
1025 	int format_mask = AFMT_U8;
1026 
1027 	struct audio_driver *driver = &sb1_audio_driver;
1028 
1029 	switch (devc->model)
1030 	{
1031 		case MDL_SB1:	/* SB1.0 or SB 1.5 */
1032 			DDB(printk("Will use standard SB1.x driver\n"));
1033 			audio_flags = DMA_HARDSTOP;
1034 			break;
1035 
1036 		case MDL_SB2:
1037 			DDB(printk("Will use SB2.0 driver\n"));
1038 			audio_flags = DMA_AUTOMODE;
1039 			driver = &sb20_audio_driver;
1040 			break;
1041 
1042 		case MDL_SB201:
1043 			DDB(printk("Will use SB2.01 (high speed) driver\n"));
1044 			audio_flags = DMA_AUTOMODE;
1045 			driver = &sb201_audio_driver;
1046 			break;
1047 
1048 		case MDL_JAZZ:
1049 		case MDL_SMW:
1050 			DDB(printk("Will use Jazz16 driver\n"));
1051 			audio_flags = DMA_AUTOMODE;
1052 			format_mask |= AFMT_S16_LE;
1053 			driver = &jazz16_audio_driver;
1054 			break;
1055 
1056 		case MDL_ESS:
1057 			DDB(printk("Will use ESS ES688/1688 driver\n"));
1058 			driver = ess_audio_init (devc, &audio_flags, &format_mask);
1059 			break;
1060 
1061 		case MDL_SB16:
1062 			DDB(printk("Will use SB16 driver\n"));
1063 			audio_flags = DMA_AUTOMODE;
1064 			format_mask |= AFMT_S16_LE;
1065 			if (devc->dma8 != devc->dma16 && devc->dma16 != -1)
1066 			{
1067 				audio_flags |= DMA_DUPLEX;
1068 				devc->duplex = 1;
1069 			}
1070 			driver = &sb16_audio_driver;
1071 			break;
1072 
1073 		default:
1074 			DDB(printk("Will use SB Pro driver\n"));
1075 			audio_flags = DMA_AUTOMODE;
1076 			driver = &sbpro_audio_driver;
1077 	}
1078 
1079 	if (owner)
1080 			driver->owner = owner;
1081 
1082 	if ((devc->dev = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
1083 				name,driver, sizeof(struct audio_driver),
1084 				audio_flags, format_mask, devc,
1085 				devc->dma8,
1086 				devc->duplex ? devc->dma16 : devc->dma8)) < 0)
1087 	{
1088 		  printk(KERN_ERR "Sound Blaster:  unable to install audio.\n");
1089 		  return;
1090 	}
1091 	audio_devs[devc->dev]->mixer_dev = devc->my_mixerdev;
1092 	audio_devs[devc->dev]->min_fragment = 5;
1093 }
1094