Linux Audio

Check our new training course

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