sb16_csp.c 32.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 *  Copyright (c) 1999 by Uros Bizjak <uros@kss-loka.si>
 *                        Takashi Iwai <tiwai@suse.de>
 *
 *  SB16ASP/AWE32 CSP control
 *
 *  CSP microcode loader:
 *   alsa-tools/sb16_csp/ 
 *
 *   This program is free software; you can redistribute it and/or modify 
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/delay.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <sound/core.h>
#include <sound/control.h>
#include <sound/info.h>
#include <sound/sb16_csp.h>
#include <sound/initval.h>

MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
MODULE_DESCRIPTION("ALSA driver for SB16 Creative Signal Processor");
MODULE_LICENSE("GPL");
38 39 40 41 42
MODULE_FIRMWARE("sb16/mulaw_main.csp");
MODULE_FIRMWARE("sb16/alaw_main.csp");
MODULE_FIRMWARE("sb16/ima_adpcm_init.csp");
MODULE_FIRMWARE("sb16/ima_adpcm_playback.csp");
MODULE_FIRMWARE("sb16/ima_adpcm_capture.csp");
L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

#ifdef SNDRV_LITTLE_ENDIAN
#define CSP_HDR_VALUE(a,b,c,d)	((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
#else
#define CSP_HDR_VALUE(a,b,c,d)	((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
#endif

#define RIFF_HEADER	CSP_HDR_VALUE('R', 'I', 'F', 'F')
#define CSP__HEADER	CSP_HDR_VALUE('C', 'S', 'P', ' ')
#define LIST_HEADER	CSP_HDR_VALUE('L', 'I', 'S', 'T')
#define FUNC_HEADER	CSP_HDR_VALUE('f', 'u', 'n', 'c')
#define CODE_HEADER	CSP_HDR_VALUE('c', 'o', 'd', 'e')
#define INIT_HEADER	CSP_HDR_VALUE('i', 'n', 'i', 't')
#define MAIN_HEADER	CSP_HDR_VALUE('m', 'a', 'i', 'n')

/*
 * RIFF data format
 */
A
Alexey Dobriyan 已提交
61
struct riff_header {
L
Linus Torvalds 已提交
62 63
	__u32 name;
	__u32 len;
A
Alexey Dobriyan 已提交
64
};
L
Linus Torvalds 已提交
65

A
Alexey Dobriyan 已提交
66 67
struct desc_header {
	struct riff_header info;
L
Linus Torvalds 已提交
68 69 70 71 72 73
	__u16 func_nr;
	__u16 VOC_type;
	__u16 flags_play_rec;
	__u16 flags_16bit_8bit;
	__u16 flags_stereo_mono;
	__u16 flags_rates;
A
Alexey Dobriyan 已提交
74
};
L
Linus Torvalds 已提交
75 76 77 78

/*
 * prototypes
 */
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
static void snd_sb_csp_free(struct snd_hwdep *hw);
static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file);
static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg);
static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file);

static int csp_detect(struct snd_sb *chip, int *version);
static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val);
static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val);
static int read_register(struct snd_sb *chip, unsigned char reg);
static int set_mode_register(struct snd_sb *chip, unsigned char mode);
static int get_version(struct snd_sb *chip);

static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
				struct snd_sb_csp_microcode __user * code);
static int snd_sb_csp_unload(struct snd_sb_csp * p);
static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags);
static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode);
static int snd_sb_csp_check_version(struct snd_sb_csp * p);

static int snd_sb_csp_use(struct snd_sb_csp * p);
static int snd_sb_csp_unuse(struct snd_sb_csp * p);
static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels);
static int snd_sb_csp_stop(struct snd_sb_csp * p);
static int snd_sb_csp_pause(struct snd_sb_csp * p);
static int snd_sb_csp_restart(struct snd_sb_csp * p);

static int snd_sb_qsound_build(struct snd_sb_csp * p);
static void snd_sb_qsound_destroy(struct snd_sb_csp * p);
static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p);

static int init_proc_entry(struct snd_sb_csp * p, int device);
static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer);
L
Linus Torvalds 已提交
111 112 113 114

/*
 * Detect CSP chip and create a new instance
 */
115
int snd_sb_csp_new(struct snd_sb *chip, int device, struct snd_hwdep ** rhwdep)
L
Linus Torvalds 已提交
116
{
117
	struct snd_sb_csp *p;
118 119
	int uninitialized_var(version);
	int err;
120
	struct snd_hwdep *hw;
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129 130

	if (rhwdep)
		*rhwdep = NULL;

	if (csp_detect(chip, &version))
		return -ENODEV;

	if ((err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw)) < 0)
		return err;

131
	if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
L
Linus Torvalds 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145
		snd_device_free(chip->card, hw);
		return -ENOMEM;
	}
	p->chip = chip;
	p->version = version;

	/* CSP operators */
	p->ops.csp_use = snd_sb_csp_use;
	p->ops.csp_unuse = snd_sb_csp_unuse;
	p->ops.csp_autoload = snd_sb_csp_autoload;
	p->ops.csp_start = snd_sb_csp_start;
	p->ops.csp_stop = snd_sb_csp_stop;
	p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;

146
	mutex_init(&p->access_mutex);
L
Linus Torvalds 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f));
	hw->iface = SNDRV_HWDEP_IFACE_SB16CSP;
	hw->private_data = p;
	hw->private_free = snd_sb_csp_free;

	/* operators - only write/ioctl */
	hw->ops.open = snd_sb_csp_open;
	hw->ops.ioctl = snd_sb_csp_ioctl;
	hw->ops.release = snd_sb_csp_release;

	/* create a proc entry */
	init_proc_entry(p, device);
	if (rhwdep)
		*rhwdep = hw;
	return 0;
}

/*
 * free_private for hwdep instance
 */
167
static void snd_sb_csp_free(struct snd_hwdep *hwdep)
L
Linus Torvalds 已提交
168
{
169
	int i;
170
	struct snd_sb_csp *p = hwdep->private_data;
L
Linus Torvalds 已提交
171 172 173
	if (p) {
		if (p->running & SNDRV_SB_CSP_ST_RUNNING)
			snd_sb_csp_stop(p);
174 175
		for (i = 0; i < ARRAY_SIZE(p->csp_programs); ++i)
			release_firmware(p->csp_programs[i]);
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184
		kfree(p);
	}
}

/* ------------------------------ */

/*
 * open the device exclusively
 */
185
static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file)
L
Linus Torvalds 已提交
186
{
187
	struct snd_sb_csp *p = hw->private_data;
L
Linus Torvalds 已提交
188 189 190 191 192 193
	return (snd_sb_csp_use(p));
}

/*
 * ioctl for hwdep device:
 */
194
static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
195
{
196 197 198
	struct snd_sb_csp *p = hw->private_data;
	struct snd_sb_csp_info info;
	struct snd_sb_csp_start start_info;
L
Linus Torvalds 已提交
199 200
	int err;

201 202
	if (snd_BUG_ON(!p))
		return -EINVAL;
L
Linus Torvalds 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

	if (snd_sb_csp_check_version(p))
		return -ENODEV;

	switch (cmd) {
		/* get information */
	case SNDRV_SB_CSP_IOCTL_INFO:
		*info.codec_name = *p->codec_name;
		info.func_nr = p->func_nr;
		info.acc_format = p->acc_format;
		info.acc_channels = p->acc_channels;
		info.acc_width = p->acc_width;
		info.acc_rates = p->acc_rates;
		info.csp_mode = p->mode;
		info.run_channels = p->run_channels;
		info.run_width = p->run_width;
		info.version = p->version;
		info.state = p->running;
		if (copy_to_user((void __user *)arg, &info, sizeof(info)))
			err = -EFAULT;
		else
			err = 0;
		break;

		/* load CSP microcode */
	case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
		err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
230
		       -EBUSY : snd_sb_csp_riff_load(p, (struct snd_sb_csp_microcode __user *) arg));
L
Linus Torvalds 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
		break;
	case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
		err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
		       -EBUSY : snd_sb_csp_unload(p));
		break;

		/* change CSP running state */
	case SNDRV_SB_CSP_IOCTL_START:
		if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info)))
			err = -EFAULT;
		else
			err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
		break;
	case SNDRV_SB_CSP_IOCTL_STOP:
		err = snd_sb_csp_stop(p);
		break;
	case SNDRV_SB_CSP_IOCTL_PAUSE:
		err = snd_sb_csp_pause(p);
		break;
	case SNDRV_SB_CSP_IOCTL_RESTART:
		err = snd_sb_csp_restart(p);
		break;
	default:
		err = -ENOTTY;
		break;
	}

	return err;
}

/*
 * close the device
 */
264
static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file)
L
Linus Torvalds 已提交
265
{
266
	struct snd_sb_csp *p = hw->private_data;
L
Linus Torvalds 已提交
267 268 269 270 271 272 273 274
	return (snd_sb_csp_unuse(p));
}

/* ------------------------------ */

/*
 * acquire device
 */
275
static int snd_sb_csp_use(struct snd_sb_csp * p)
L
Linus Torvalds 已提交
276
{
277
	mutex_lock(&p->access_mutex);
L
Linus Torvalds 已提交
278
	if (p->used) {
279
		mutex_unlock(&p->access_mutex);
L
Linus Torvalds 已提交
280 281 282
		return -EAGAIN;
	}
	p->used++;
283
	mutex_unlock(&p->access_mutex);
L
Linus Torvalds 已提交
284 285 286 287 288 289 290 291

	return 0;

}

/*
 * release device
 */
292
static int snd_sb_csp_unuse(struct snd_sb_csp * p)
L
Linus Torvalds 已提交
293
{
294
	mutex_lock(&p->access_mutex);
L
Linus Torvalds 已提交
295
	p->used--;
296
	mutex_unlock(&p->access_mutex);
L
Linus Torvalds 已提交
297 298 299 300 301 302 303 304

	return 0;
}

/*
 * load microcode via ioctl: 
 * code is user-space pointer
 */
305 306
static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
				struct snd_sb_csp_microcode __user * mcode)
L
Linus Torvalds 已提交
307
{
308
	struct snd_sb_csp_mc_header info;
L
Linus Torvalds 已提交
309 310 311 312 313

	unsigned char __user *data_ptr;
	unsigned char __user *data_end;
	unsigned short func_nr = 0;

A
Alexey Dobriyan 已提交
314
	struct riff_header file_h, item_h, code_h;
L
Linus Torvalds 已提交
315
	__u32 item_type;
A
Alexey Dobriyan 已提交
316
	struct desc_header funcdesc_h;
L
Linus Torvalds 已提交
317 318 319 320 321 322 323 324 325 326 327

	unsigned long flags;
	int err;

	if (copy_from_user(&info, mcode, sizeof(info)))
		return -EFAULT;
	data_ptr = mcode->data;

	if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
		return -EFAULT;
	if ((file_h.name != RIFF_HEADER) ||
328
	    (le32_to_cpu(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
329
		snd_printd("%s: Invalid RIFF header\n", __func__);
L
Linus Torvalds 已提交
330 331 332
		return -EINVAL;
	}
	data_ptr += sizeof(file_h);
333
	data_end = data_ptr + le32_to_cpu(file_h.len);
L
Linus Torvalds 已提交
334 335 336 337

	if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
		return -EFAULT;
	if (item_type != CSP__HEADER) {
338
		snd_printd("%s: Invalid RIFF file type\n", __func__);
L
Linus Torvalds 已提交
339 340 341 342
		return -EINVAL;
	}
	data_ptr += sizeof (item_type);

343
	for (; data_ptr < data_end; data_ptr += le32_to_cpu(item_h.len)) {
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351 352 353 354 355
		if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
			return -EFAULT;
		data_ptr += sizeof(item_h);
		if (item_h.name != LIST_HEADER)
			continue;

		if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
			 return -EFAULT;
		switch (item_type) {
		case FUNC_HEADER:
			if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
				return -EFAULT;
356
			func_nr = le16_to_cpu(funcdesc_h.func_nr);
L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
			break;
		case CODE_HEADER:
			if (func_nr != info.func_req)
				break;	/* not required function, try next */
			data_ptr += sizeof(item_type);

			/* destroy QSound mixer element */
			if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
				snd_sb_qsound_destroy(p);
			}
			/* Clear all flags */
			p->running = 0;
			p->mode = 0;

			/* load microcode blocks */
			for (;;) {
				if (data_ptr >= data_end)
					return -EINVAL;
				if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
					return -EFAULT;

				/* init microcode blocks */
				if (code_h.name != INIT_HEADER)
					break;
				data_ptr += sizeof(code_h);
382
				err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len),
L
Linus Torvalds 已提交
383 384 385
						      SNDRV_SB_CSP_LOAD_INITBLOCK);
				if (err)
					return err;
386
				data_ptr += le32_to_cpu(code_h.len);
L
Linus Torvalds 已提交
387 388 389 390 391 392
			}
			/* main microcode block */
			if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
				return -EFAULT;

			if (code_h.name != MAIN_HEADER) {
393
				snd_printd("%s: Missing 'main' microcode\n", __func__);
L
Linus Torvalds 已提交
394 395 396 397
				return -EINVAL;
			}
			data_ptr += sizeof(code_h);
			err = snd_sb_csp_load_user(p, data_ptr,
398
						   le32_to_cpu(code_h.len), 0);
L
Linus Torvalds 已提交
399 400 401 402 403 404
			if (err)
				return err;

			/* fill in codec header */
			strlcpy(p->codec_name, info.codec_name, sizeof(p->codec_name));
			p->func_nr = func_nr;
405 406
			p->mode = le16_to_cpu(funcdesc_h.flags_play_rec);
			switch (le16_to_cpu(funcdesc_h.VOC_type)) {
L
Linus Torvalds 已提交
407
			case 0x0001:	/* QSound decoder */
408
				if (le16_to_cpu(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
L
Linus Torvalds 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
					if (snd_sb_qsound_build(p) == 0)
						/* set QSound flag and clear all other mode flags */
						p->mode = SNDRV_SB_CSP_MODE_QSOUND;
				}
				p->acc_format = 0;
				break;
			case 0x0006:	/* A Law codec */
				p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
				break;
			case 0x0007:	/* Mu Law codec */
				p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
				break;
			case 0x0011:	/* what Creative thinks is IMA ADPCM codec */
			case 0x0200:	/* Creative ADPCM codec */
				p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
				break;
			case    201:	/* Text 2 Speech decoder */
				/* TODO: Text2Speech handling routines */
				p->acc_format = 0;
				break;
			case 0x0202:	/* Fast Speech 8 codec */
			case 0x0203:	/* Fast Speech 10 codec */
				p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
				break;
			default:	/* other codecs are unsupported */
				p->acc_format = p->acc_width = p->acc_rates = 0;
				p->mode = 0;
				snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
437
					   __func__,
438
					   le16_to_cpu(funcdesc_h.VOC_type));
L
Linus Torvalds 已提交
439 440
				return -EINVAL;
			}
441 442 443
			p->acc_channels = le16_to_cpu(funcdesc_h.flags_stereo_mono);
			p->acc_width = le16_to_cpu(funcdesc_h.flags_16bit_8bit);
			p->acc_rates = le16_to_cpu(funcdesc_h.flags_rates);
L
Linus Torvalds 已提交
444 445 446 447 448 449 450 451 452 453 454 455

			/* Decouple CSP from IRQ and DMAREQ lines */
			spin_lock_irqsave(&p->chip->reg_lock, flags);
			set_mode_register(p->chip, 0xfc);
			set_mode_register(p->chip, 0x00);
			spin_unlock_irqrestore(&p->chip->reg_lock, flags);

			/* finished loading successfully */
			p->running = SNDRV_SB_CSP_ST_LOADED;	/* set LOADED flag */
			return 0;
		}
	}
456
	snd_printd("%s: Function #%d not found\n", __func__, info.func_req);
L
Linus Torvalds 已提交
457 458 459 460 461 462
	return -EINVAL;
}

/*
 * unload CSP microcode
 */
463
static int snd_sb_csp_unload(struct snd_sb_csp * p)
L
Linus Torvalds 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
{
	if (p->running & SNDRV_SB_CSP_ST_RUNNING)
		return -EBUSY;
	if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
		return -ENXIO;

	/* clear supported formats */
	p->acc_format = 0;
	p->acc_channels = p->acc_width = p->acc_rates = 0;
	/* destroy QSound mixer element */
	if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
		snd_sb_qsound_destroy(p);
	}
	/* clear all flags */
	p->running = 0;
	p->mode = 0;
	return 0;
}

/*
 * send command sequence to DSP
 */
486
static inline int command_seq(struct snd_sb *chip, const unsigned char *seq, int size)
L
Linus Torvalds 已提交
487 488 489 490 491 492 493 494 495 496 497 498
{
	int i;
	for (i = 0; i < size; i++) {
		if (!snd_sbdsp_command(chip, seq[i]))
			return -EIO;
	}
	return 0;
}

/*
 * set CSP codec parameter
 */
499
static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val)
L
Linus Torvalds 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
{
	unsigned char dsp_cmd[3];

	dsp_cmd[0] = 0x05;	/* CSP set codec parameter */
	dsp_cmd[1] = val;	/* Parameter value */
	dsp_cmd[2] = par;	/* Parameter */
	command_seq(chip, dsp_cmd, 3);
	snd_sbdsp_command(chip, 0x03);	/* DSP read? */
	if (snd_sbdsp_get_byte(chip) != par)
		return -EIO;
	return 0;
}

/*
 * set CSP register
 */
516
static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val)
L
Linus Torvalds 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529
{
	unsigned char dsp_cmd[3];

	dsp_cmd[0] = 0x0e;	/* CSP set register */
	dsp_cmd[1] = reg;	/* CSP Register */
	dsp_cmd[2] = val;	/* value */
	return command_seq(chip, dsp_cmd, 3);
}

/*
 * read CSP register
 * return < 0 -> error
 */
530
static int read_register(struct snd_sb *chip, unsigned char reg)
L
Linus Torvalds 已提交
531 532 533 534 535 536 537 538 539 540 541 542
{
	unsigned char dsp_cmd[2];

	dsp_cmd[0] = 0x0f;	/* CSP read register */
	dsp_cmd[1] = reg;	/* CSP Register */
	command_seq(chip, dsp_cmd, 2);
	return snd_sbdsp_get_byte(chip);	/* Read DSP value */
}

/*
 * set CSP mode register
 */
543
static int set_mode_register(struct snd_sb *chip, unsigned char mode)
L
Linus Torvalds 已提交
544 545 546 547 548 549 550 551 552 553 554 555
{
	unsigned char dsp_cmd[2];

	dsp_cmd[0] = 0x04;	/* CSP set mode register */
	dsp_cmd[1] = mode;	/* mode */
	return command_seq(chip, dsp_cmd, 2);
}

/*
 * Detect CSP
 * return 0 if CSP exists.
 */
556
static int csp_detect(struct snd_sb *chip, int *version)
L
Linus Torvalds 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
{
	unsigned char csp_test1, csp_test2;
	unsigned long flags;
	int result = -ENODEV;

	spin_lock_irqsave(&chip->reg_lock, flags);

	set_codec_parameter(chip, 0x00, 0x00);
	set_mode_register(chip, 0xfc);		/* 0xfc = ?? */

	csp_test1 = read_register(chip, 0x83);
	set_register(chip, 0x83, ~csp_test1);
	csp_test2 = read_register(chip, 0x83);
	if (csp_test2 != (csp_test1 ^ 0xff))
		goto __fail;

	set_register(chip, 0x83, csp_test1);
	csp_test2 = read_register(chip, 0x83);
	if (csp_test2 != csp_test1)
		goto __fail;

	set_mode_register(chip, 0x00);		/* 0x00 = ? */

	*version = get_version(chip);
	snd_sbdsp_reset(chip);	/* reset DSP after getversion! */
	if (*version >= 0x10 && *version <= 0x1f)
		result = 0;		/* valid version id */

      __fail:
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	return result;
}

/*
 * get CSP version number
 */
593
static int get_version(struct snd_sb *chip)
L
Linus Torvalds 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606
{
	unsigned char dsp_cmd[2];

	dsp_cmd[0] = 0x08;	/* SB_DSP_!something! */
	dsp_cmd[1] = 0x03;	/* get chip version id? */
	command_seq(chip, dsp_cmd, 2);

	return (snd_sbdsp_get_byte(chip));
}

/*
 * check if the CSP version is valid
 */
607
static int snd_sb_csp_check_version(struct snd_sb_csp * p)
L
Linus Torvalds 已提交
608 609
{
	if (p->version < 0x10 || p->version > 0x1f) {
610
		snd_printd("%s: Invalid CSP version: 0x%x\n", __func__, p->version);
L
Linus Torvalds 已提交
611 612 613 614 615 616 617 618
		return 1;
	}
	return 0;
}

/*
 * download microcode to CSP (microcode should have one "main" block).
 */
619
static int snd_sb_csp_load(struct snd_sb_csp * p, const unsigned char *buf, int size, int load_flags)
L
Linus Torvalds 已提交
620 621 622 623 624 625 626 627 628
{
	int status, i;
	int err;
	int result = -EIO;
	unsigned long flags;

	spin_lock_irqsave(&p->chip->reg_lock, flags);
	snd_sbdsp_command(p->chip, 0x01);	/* CSP download command */
	if (snd_sbdsp_get_byte(p->chip)) {
629
		snd_printd("%s: Download command failed\n", __func__);
L
Linus Torvalds 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
		goto __fail;
	}
	/* Send CSP low byte (size - 1) */
	snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
	/* Send high byte */
	snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
	/* send microcode sequence */
	/* load from kernel space */
	while (size--) {
		if (!snd_sbdsp_command(p->chip, *buf++))
			goto __fail;
	}
	if (snd_sbdsp_get_byte(p->chip))
		goto __fail;

	if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
		i = 0;
		/* some codecs (FastSpeech) take some time to initialize */
		while (1) {
			snd_sbdsp_command(p->chip, 0x03);
			status = snd_sbdsp_get_byte(p->chip);
			if (status == 0x55 || ++i >= 10)
				break;
			udelay (10);
		}
		if (status != 0x55) {
656
			snd_printd("%s: Microcode initialization failed\n", __func__);
L
Linus Torvalds 已提交
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
			goto __fail;
		}
	} else {
		/*
		 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
		 * Start CSP chip if no 16bit DMA channel is set - some kind
		 * of autorun or perhaps a bugfix?
		 */
		spin_lock(&p->chip->mixer_lock);
		status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
		spin_unlock(&p->chip->mixer_lock);
		if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
			err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
			       set_codec_parameter(p->chip, 0xff, 0x00));
			snd_sbdsp_reset(p->chip);		/* really! */
			if (err)
				goto __fail;
			set_mode_register(p->chip, 0xc0);	/* c0 = STOP */
			set_mode_register(p->chip, 0x70);	/* 70 = RUN */
		}
	}
	result = 0;

      __fail:
	spin_unlock_irqrestore(&p->chip->reg_lock, flags);
	return result;
}
 
685
static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags)
L
Linus Torvalds 已提交
686
{
L
Li Zefan 已提交
687 688 689 690 691 692 693 694 695 696
	int err;
	unsigned char *kbuf;

	kbuf = memdup_user(buf, size);
	if (IS_ERR(kbuf))
		return PTR_ERR(kbuf);

	err = snd_sb_csp_load(p, kbuf, size, load_flags);

	kfree(kbuf);
L
Linus Torvalds 已提交
697 698 699
	return err;
}

700 701 702 703 704 705 706 707 708 709 710 711 712 713
static int snd_sb_csp_firmware_load(struct snd_sb_csp *p, int index, int flags)
{
	static const char *const names[] = {
		"sb16/mulaw_main.csp",
		"sb16/alaw_main.csp",
		"sb16/ima_adpcm_init.csp",
		"sb16/ima_adpcm_playback.csp",
		"sb16/ima_adpcm_capture.csp",
	};
	const struct firmware *program;

	BUILD_BUG_ON(ARRAY_SIZE(names) != CSP_PROGRAM_COUNT);
	program = p->csp_programs[index];
	if (!program) {
714 715 716
		int err = request_firmware(&program, names[index],
				       p->chip->card->dev);
		if (err < 0)
717
			return err;
718
		p->csp_programs[index] = program;
719 720 721 722
	}
	return snd_sb_csp_load(p, program->data, program->size, flags);
}

L
Linus Torvalds 已提交
723 724 725 726
/*
 * autoload hardware codec if necessary
 * return 0 if CSP is loaded and ready to run (p->running != 0)
 */
727
static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode)
L
Linus Torvalds 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741
{
	unsigned long flags;
	int err = 0;

	/* if CSP is running or manually loaded then exit */
	if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED)) 
		return -EBUSY;

	/* autoload microcode only if requested hardware codec is not already loaded */
	if (((1 << pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
		p->running = SNDRV_SB_CSP_ST_AUTO;
	} else {
		switch (pcm_sfmt) {
		case SNDRV_PCM_FORMAT_MU_LAW:
742
			err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_MULAW, 0);
L
Linus Torvalds 已提交
743 744 745 746
			p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
			p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
			break;
		case SNDRV_PCM_FORMAT_A_LAW:
747
			err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ALAW, 0);
L
Linus Torvalds 已提交
748 749 750 751
			p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
			p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
			break;
		case SNDRV_PCM_FORMAT_IMA_ADPCM:
752 753
			err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ADPCM_INIT,
						       SNDRV_SB_CSP_LOAD_INITBLOCK);
L
Linus Torvalds 已提交
754 755 756
			if (err)
				break;
			if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
757 758
				err = snd_sb_csp_firmware_load
					(p, CSP_PROGRAM_ADPCM_PLAYBACK, 0);
L
Linus Torvalds 已提交
759 760
				p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
			} else {
761 762
				err = snd_sb_csp_firmware_load
					(p, CSP_PROGRAM_ADPCM_CAPTURE, 0);
L
Linus Torvalds 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
				p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
			}
			p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
			break;				  
		default:
			/* Decouple CSP from IRQ and DMAREQ lines */
			if (p->running & SNDRV_SB_CSP_ST_AUTO) {
				spin_lock_irqsave(&p->chip->reg_lock, flags);
				set_mode_register(p->chip, 0xfc);
				set_mode_register(p->chip, 0x00);
				spin_unlock_irqrestore(&p->chip->reg_lock, flags);
				p->running = 0;			/* clear autoloaded flag */
			}
			return -EINVAL;
		}
		if (err) {
			p->acc_format = 0;
			p->acc_channels = p->acc_width = p->acc_rates = 0;

			p->running = 0;				/* clear autoloaded flag */
			p->mode = 0;
			return (err);
		} else {
			p->running = SNDRV_SB_CSP_ST_AUTO;	/* set autoloaded flag */
			p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT;	/* only 16 bit data */
			p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
			p->acc_rates = SNDRV_SB_CSP_RATE_ALL;	/* HW codecs accept all rates */
		}   

	}
	return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
}

/*
 * start CSP
 */
799
static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels)
L
Linus Torvalds 已提交
800 801 802 803 804 805 806
{
	unsigned char s_type;	/* sample type */
	unsigned char mixL, mixR;
	int result = -EIO;
	unsigned long flags;

	if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
807
		snd_printd("%s: Microcode not loaded\n", __func__);
L
Linus Torvalds 已提交
808 809 810
		return -ENXIO;
	}
	if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
811
		snd_printd("%s: CSP already running\n", __func__);
L
Linus Torvalds 已提交
812 813 814
		return -EBUSY;
	}
	if (!(sample_width & p->acc_width)) {
815
		snd_printd("%s: Unsupported PCM sample width\n", __func__);
L
Linus Torvalds 已提交
816 817 818
		return -EINVAL;
	}
	if (!(channels & p->acc_channels)) {
819
		snd_printd("%s: Invalid number of channels\n", __func__);
L
Linus Torvalds 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
		return -EINVAL;
	}

	/* Mute PCM volume */
	spin_lock_irqsave(&p->chip->mixer_lock, flags);
	mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
	mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);

	spin_lock(&p->chip->reg_lock);
	set_mode_register(p->chip, 0xc0);	/* c0 = STOP */
	set_mode_register(p->chip, 0x70);	/* 70 = RUN */

	s_type = 0x00;
	if (channels == SNDRV_SB_CSP_MONO)
		s_type = 0x11;	/* 000n 000n    (n = 1 if mono) */
	if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
		s_type |= 0x22;	/* 00dX 00dX    (d = 1 if 8 bit samples) */

	if (set_codec_parameter(p->chip, 0x81, s_type)) {
841
		snd_printd("%s: Set sample type command failed\n", __func__);
L
Linus Torvalds 已提交
842 843 844
		goto __fail;
	}
	if (set_codec_parameter(p->chip, 0x80, 0x00)) {
845
		snd_printd("%s: Codec start command failed\n", __func__);
L
Linus Torvalds 已提交
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
		goto __fail;
	}
	p->run_width = sample_width;
	p->run_channels = channels;

	p->running |= SNDRV_SB_CSP_ST_RUNNING;

	if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
		set_codec_parameter(p->chip, 0xe0, 0x01);
		/* enable QSound decoder */
		set_codec_parameter(p->chip, 0x00, 0xff);
		set_codec_parameter(p->chip, 0x01, 0xff);
		p->running |= SNDRV_SB_CSP_ST_QSOUND;
		/* set QSound startup value */
		snd_sb_csp_qsound_transfer(p);
	}
	result = 0;

      __fail:
	spin_unlock(&p->chip->reg_lock);

	/* restore PCM volume */
	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
	spin_unlock_irqrestore(&p->chip->mixer_lock, flags);

	return result;
}

/*
 * stop CSP
 */
878
static int snd_sb_csp_stop(struct snd_sb_csp * p)
L
Linus Torvalds 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
{
	int result;
	unsigned char mixL, mixR;
	unsigned long flags;

	if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
		return 0;

	/* Mute PCM volume */
	spin_lock_irqsave(&p->chip->mixer_lock, flags);
	mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
	mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);

	spin_lock(&p->chip->reg_lock);
	if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
		set_codec_parameter(p->chip, 0xe0, 0x01);
		/* disable QSound decoder */
		set_codec_parameter(p->chip, 0x00, 0x00);
		set_codec_parameter(p->chip, 0x01, 0x00);

		p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
	}
	result = set_mode_register(p->chip, 0xc0);	/* c0 = STOP */
	spin_unlock(&p->chip->reg_lock);

	/* restore PCM volume */
	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
	spin_unlock_irqrestore(&p->chip->mixer_lock, flags);

	if (!(result))
		p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
	return result;
}

/*
 * pause CSP codec and hold DMA transfer
 */
919
static int snd_sb_csp_pause(struct snd_sb_csp * p)
L
Linus Torvalds 已提交
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
{
	int result;
	unsigned long flags;

	if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
		return -EBUSY;

	spin_lock_irqsave(&p->chip->reg_lock, flags);
	result = set_codec_parameter(p->chip, 0x80, 0xff);
	spin_unlock_irqrestore(&p->chip->reg_lock, flags);
	if (!(result))
		p->running |= SNDRV_SB_CSP_ST_PAUSED;

	return result;
}

/*
 * restart CSP codec and resume DMA transfer
 */
939
static int snd_sb_csp_restart(struct snd_sb_csp * p)
L
Linus Torvalds 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
{
	int result;
	unsigned long flags;

	if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
		return -EBUSY;

	spin_lock_irqsave(&p->chip->reg_lock, flags);
	result = set_codec_parameter(p->chip, 0x80, 0x00);
	spin_unlock_irqrestore(&p->chip->reg_lock, flags);
	if (!(result))
		p->running &= ~SNDRV_SB_CSP_ST_PAUSED;

	return result;
}

/* ------------------------------ */

/*
 * QSound mixer control for PCM
 */

962
#define snd_sb_qsound_switch_info	snd_ctl_boolean_mono_info
L
Linus Torvalds 已提交
963

964
static int snd_sb_qsound_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
965
{
966
	struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
967 968 969 970 971
	
	ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
	return 0;
}

972
static int snd_sb_qsound_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
973
{
974
	struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
975 976 977 978 979 980 981 982 983 984 985 986
	unsigned long flags;
	int change;
	unsigned char nval;
	
	nval = ucontrol->value.integer.value[0] & 0x01;
	spin_lock_irqsave(&p->q_lock, flags);
	change = p->q_enabled != nval;
	p->q_enabled = nval;
	spin_unlock_irqrestore(&p->q_lock, flags);
	return change;
}

987
static int snd_sb_qsound_space_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
988 989 990 991 992 993 994 995
{
	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
	return 0;
}

996
static int snd_sb_qsound_space_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
997
{
998
	struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007
	unsigned long flags;
	
	spin_lock_irqsave(&p->q_lock, flags);
	ucontrol->value.integer.value[0] = p->qpos_left;
	ucontrol->value.integer.value[1] = p->qpos_right;
	spin_unlock_irqrestore(&p->q_lock, flags);
	return 0;
}

1008
static int snd_sb_qsound_space_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1009
{
1010
	struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
	unsigned long flags;
	int change;
	unsigned char nval1, nval2;
	
	nval1 = ucontrol->value.integer.value[0];
	if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
		nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
	nval2 = ucontrol->value.integer.value[1];
	if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
		nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
	spin_lock_irqsave(&p->q_lock, flags);
	change = p->qpos_left != nval1 || p->qpos_right != nval2;
	p->qpos_left = nval1;
	p->qpos_right = nval2;
	p->qpos_changed = change;
	spin_unlock_irqrestore(&p->q_lock, flags);
	return change;
}

1030
static struct snd_kcontrol_new snd_sb_qsound_switch = {
L
Linus Torvalds 已提交
1031 1032 1033 1034 1035 1036 1037
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "3D Control - Switch",
	.info = snd_sb_qsound_switch_info,
	.get = snd_sb_qsound_switch_get,
	.put = snd_sb_qsound_switch_put
};

1038
static struct snd_kcontrol_new snd_sb_qsound_space = {
L
Linus Torvalds 已提交
1039 1040 1041 1042 1043 1044 1045
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "3D Control - Space",
	.info = snd_sb_qsound_space_info,
	.get = snd_sb_qsound_space_get,
	.put = snd_sb_qsound_space_put
};

1046
static int snd_sb_qsound_build(struct snd_sb_csp * p)
L
Linus Torvalds 已提交
1047
{
1048
	struct snd_card *card;
L
Linus Torvalds 已提交
1049 1050
	int err;

1051 1052
	if (snd_BUG_ON(!p))
		return -EINVAL;
L
Linus Torvalds 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071

	card = p->chip->card;
	p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
	p->qpos_changed = 0;

	spin_lock_init(&p->q_lock);

	if ((err = snd_ctl_add(card, p->qsound_switch = snd_ctl_new1(&snd_sb_qsound_switch, p))) < 0)
		goto __error;
	if ((err = snd_ctl_add(card, p->qsound_space = snd_ctl_new1(&snd_sb_qsound_space, p))) < 0)
		goto __error;

	return 0;

     __error:
	snd_sb_qsound_destroy(p);
	return err;
}

1072
static void snd_sb_qsound_destroy(struct snd_sb_csp * p)
L
Linus Torvalds 已提交
1073
{
1074
	struct snd_card *card;
L
Linus Torvalds 已提交
1075 1076
	unsigned long flags;

1077 1078
	if (snd_BUG_ON(!p))
		return;
L
Linus Torvalds 已提交
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098

	card = p->chip->card;	
	
	down_write(&card->controls_rwsem);
	if (p->qsound_switch)
		snd_ctl_remove(card, p->qsound_switch);
	if (p->qsound_space)
		snd_ctl_remove(card, p->qsound_space);
	up_write(&card->controls_rwsem);

	/* cancel pending transfer of QSound parameters */
	spin_lock_irqsave (&p->q_lock, flags);
	p->qpos_changed = 0;
	spin_unlock_irqrestore (&p->q_lock, flags);
}

/*
 * Transfer qsound parameters to CSP,
 * function should be called from interrupt routine
 */
1099
static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p)
L
Linus Torvalds 已提交
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
{
	int err = -ENXIO;

	spin_lock(&p->q_lock);
	if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
		set_codec_parameter(p->chip, 0xe0, 0x01);
		/* left channel */
		set_codec_parameter(p->chip, 0x00, p->qpos_left);
		set_codec_parameter(p->chip, 0x02, 0x00);
		/* right channel */
		set_codec_parameter(p->chip, 0x00, p->qpos_right);
		set_codec_parameter(p->chip, 0x03, 0x00);
		err = 0;
	}
	p->qpos_changed = 0;
	spin_unlock(&p->q_lock);
	return err;
}

/* ------------------------------ */

/*
 * proc interface
 */
1124
static int init_proc_entry(struct snd_sb_csp * p, int device)
L
Linus Torvalds 已提交
1125 1126
{
	char name[16];
1127
	struct snd_info_entry *entry;
L
Linus Torvalds 已提交
1128 1129
	sprintf(name, "cspD%d", device);
	if (! snd_card_proc_new(p->chip->card, name, &entry))
1130
		snd_info_set_text_ops(entry, p, info_read);
L
Linus Torvalds 已提交
1131 1132 1133
	return 0;
}

1134
static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
L
Linus Torvalds 已提交
1135
{
1136
	struct snd_sb_csp *p = entry->private_data;
L
Linus Torvalds 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201

	snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
	snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
		    ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
		    ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
		    ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
	if (p->running & SNDRV_SB_CSP_ST_LOADED) {
		snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
		snd_iprintf(buffer, "Sample rates: ");
		if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
			snd_iprintf(buffer, "All\n");
		} else {
			snd_iprintf(buffer, "%s%s%s%s\n",
				    ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
				    ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
				    ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
				    ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
		}
		if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
			snd_iprintf(buffer, "QSound decoder %sabled\n",
				    p->q_enabled ? "en" : "dis");
		} else {
			snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
				    p->acc_format,
				    ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
				    ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
				    ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
				    ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
				    ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
				    ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
		}
	}
	if (p->running & SNDRV_SB_CSP_ST_AUTO) {
		snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
	}
	if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
		snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
			    ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
			    ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
	}
	if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
		snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
			    p->qpos_left, p->qpos_right);
	}
}

/* */

EXPORT_SYMBOL(snd_sb_csp_new);

/*
 * INIT part
 */

static int __init alsa_sb_csp_init(void)
{
	return 0;
}

static void __exit alsa_sb_csp_exit(void)
{
}

module_init(alsa_sb_csp_init)
module_exit(alsa_sb_csp_exit)