xref: /linux/sound/isa/sb/sb16_csp.c (revision 2c8d2a510c15c003749e43ac2b8e1bc79a7a00d6)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 1999 by Uros Bizjak <uros@kss-loka.si>
4  *                        Takashi Iwai <tiwai@suse.de>
5  *
6  *  SB16ASP/AWE32 CSP control
7  *
8  *  CSP microcode loader:
9  *   alsa-tools/sb16_csp/
10  */
11 
12 #include <linux/delay.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/string_choices.h>
17 #include <sound/core.h>
18 #include <sound/control.h>
19 #include <sound/info.h>
20 #include <sound/sb16_csp.h>
21 #include <sound/initval.h>
22 
23 MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
24 MODULE_DESCRIPTION("ALSA driver for SB16 Creative Signal Processor");
25 MODULE_LICENSE("GPL");
26 MODULE_FIRMWARE("sb16/mulaw_main.csp");
27 MODULE_FIRMWARE("sb16/alaw_main.csp");
28 MODULE_FIRMWARE("sb16/ima_adpcm_init.csp");
29 MODULE_FIRMWARE("sb16/ima_adpcm_playback.csp");
30 MODULE_FIRMWARE("sb16/ima_adpcm_capture.csp");
31 
32 #ifdef SNDRV_LITTLE_ENDIAN
33 #define CSP_HDR_VALUE(a,b,c,d)	((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
34 #else
35 #define CSP_HDR_VALUE(a,b,c,d)	((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
36 #endif
37 
38 #define RIFF_HEADER	CSP_HDR_VALUE('R', 'I', 'F', 'F')
39 #define CSP__HEADER	CSP_HDR_VALUE('C', 'S', 'P', ' ')
40 #define LIST_HEADER	CSP_HDR_VALUE('L', 'I', 'S', 'T')
41 #define FUNC_HEADER	CSP_HDR_VALUE('f', 'u', 'n', 'c')
42 #define CODE_HEADER	CSP_HDR_VALUE('c', 'o', 'd', 'e')
43 #define INIT_HEADER	CSP_HDR_VALUE('i', 'n', 'i', 't')
44 #define MAIN_HEADER	CSP_HDR_VALUE('m', 'a', 'i', 'n')
45 
46 /*
47  * RIFF data format
48  */
49 struct riff_header {
50 	__le32 name;
51 	__le32 len;
52 };
53 
54 struct desc_header {
55 	struct riff_header info;
56 	__le16 func_nr;
57 	__le16 VOC_type;
58 	__le16 flags_play_rec;
59 	__le16 flags_16bit_8bit;
60 	__le16 flags_stereo_mono;
61 	__le16 flags_rates;
62 };
63 
64 /*
65  * prototypes
66  */
67 static void snd_sb_csp_free(struct snd_hwdep *hw);
68 static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file);
69 static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg);
70 static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file);
71 
72 static int csp_detect(struct snd_sb *chip, int *version);
73 static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val);
74 static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val);
75 static int read_register(struct snd_sb *chip, unsigned char reg);
76 static int set_mode_register(struct snd_sb *chip, unsigned char mode);
77 static int get_version(struct snd_sb *chip);
78 
79 static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
80 				struct snd_sb_csp_microcode __user * code);
81 static int snd_sb_csp_unload(struct snd_sb_csp * p);
82 static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags);
83 static int snd_sb_csp_autoload(struct snd_sb_csp * p, snd_pcm_format_t pcm_sfmt, int play_rec_mode);
84 static int snd_sb_csp_check_version(struct snd_sb_csp * p);
85 
86 static int snd_sb_csp_use(struct snd_sb_csp * p);
87 static int snd_sb_csp_unuse(struct snd_sb_csp * p);
88 static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels);
89 static int snd_sb_csp_stop(struct snd_sb_csp * p);
90 static int snd_sb_csp_pause(struct snd_sb_csp * p);
91 static int snd_sb_csp_restart(struct snd_sb_csp * p);
92 
93 static int snd_sb_qsound_build(struct snd_sb_csp * p);
94 static void snd_sb_qsound_destroy(struct snd_sb_csp * p);
95 static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p);
96 
97 static int init_proc_entry(struct snd_sb_csp * p, int device);
98 static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer);
99 
100 /*
101  * Detect CSP chip and create a new instance
102  */
103 int snd_sb_csp_new(struct snd_sb *chip, int device, struct snd_hwdep ** rhwdep)
104 {
105 	struct snd_sb_csp *p;
106 	int version;
107 	int err;
108 	struct snd_hwdep *hw;
109 
110 	if (rhwdep)
111 		*rhwdep = NULL;
112 
113 	if (csp_detect(chip, &version))
114 		return -ENODEV;
115 
116 	err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw);
117 	if (err < 0)
118 		return err;
119 
120 	p = kzalloc(sizeof(*p), GFP_KERNEL);
121 	if (!p) {
122 		snd_device_free(chip->card, hw);
123 		return -ENOMEM;
124 	}
125 	p->chip = chip;
126 	p->version = version;
127 
128 	/* CSP operators */
129 	p->ops.csp_use = snd_sb_csp_use;
130 	p->ops.csp_unuse = snd_sb_csp_unuse;
131 	p->ops.csp_autoload = snd_sb_csp_autoload;
132 	p->ops.csp_start = snd_sb_csp_start;
133 	p->ops.csp_stop = snd_sb_csp_stop;
134 	p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;
135 
136 	mutex_init(&p->access_mutex);
137 	sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f));
138 	hw->iface = SNDRV_HWDEP_IFACE_SB16CSP;
139 	hw->private_data = p;
140 	hw->private_free = snd_sb_csp_free;
141 
142 	/* operators - only write/ioctl */
143 	hw->ops.open = snd_sb_csp_open;
144 	hw->ops.ioctl = snd_sb_csp_ioctl;
145 	hw->ops.release = snd_sb_csp_release;
146 
147 	/* create a proc entry */
148 	init_proc_entry(p, device);
149 	if (rhwdep)
150 		*rhwdep = hw;
151 	return 0;
152 }
153 
154 /*
155  * free_private for hwdep instance
156  */
157 static void snd_sb_csp_free(struct snd_hwdep *hwdep)
158 {
159 	int i;
160 	struct snd_sb_csp *p = hwdep->private_data;
161 	if (p) {
162 		if (p->running & SNDRV_SB_CSP_ST_RUNNING)
163 			snd_sb_csp_stop(p);
164 		for (i = 0; i < ARRAY_SIZE(p->csp_programs); ++i)
165 			release_firmware(p->csp_programs[i]);
166 		kfree(p);
167 	}
168 }
169 
170 /* ------------------------------ */
171 
172 /*
173  * open the device exclusively
174  */
175 static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file)
176 {
177 	struct snd_sb_csp *p = hw->private_data;
178 	return (snd_sb_csp_use(p));
179 }
180 
181 /*
182  * ioctl for hwdep device:
183  */
184 static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg)
185 {
186 	struct snd_sb_csp *p = hw->private_data;
187 	struct snd_sb_csp_info info;
188 	struct snd_sb_csp_start start_info;
189 	int err;
190 
191 	if (snd_BUG_ON(!p))
192 		return -EINVAL;
193 
194 	if (snd_sb_csp_check_version(p))
195 		return -ENODEV;
196 
197 	switch (cmd) {
198 		/* get information */
199 	case SNDRV_SB_CSP_IOCTL_INFO:
200 		memset(&info, 0, sizeof(info));
201 		*info.codec_name = *p->codec_name;
202 		info.func_nr = p->func_nr;
203 		info.acc_format = p->acc_format;
204 		info.acc_channels = p->acc_channels;
205 		info.acc_width = p->acc_width;
206 		info.acc_rates = p->acc_rates;
207 		info.csp_mode = p->mode;
208 		info.run_channels = p->run_channels;
209 		info.run_width = p->run_width;
210 		info.version = p->version;
211 		info.state = p->running;
212 		if (copy_to_user((void __user *)arg, &info, sizeof(info)))
213 			err = -EFAULT;
214 		else
215 			err = 0;
216 		break;
217 
218 		/* load CSP microcode */
219 	case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
220 		err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
221 		       -EBUSY : snd_sb_csp_riff_load(p, (struct snd_sb_csp_microcode __user *) arg));
222 		break;
223 	case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
224 		err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
225 		       -EBUSY : snd_sb_csp_unload(p));
226 		break;
227 
228 		/* change CSP running state */
229 	case SNDRV_SB_CSP_IOCTL_START:
230 		if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info)))
231 			err = -EFAULT;
232 		else
233 			err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
234 		break;
235 	case SNDRV_SB_CSP_IOCTL_STOP:
236 		err = snd_sb_csp_stop(p);
237 		break;
238 	case SNDRV_SB_CSP_IOCTL_PAUSE:
239 		err = snd_sb_csp_pause(p);
240 		break;
241 	case SNDRV_SB_CSP_IOCTL_RESTART:
242 		err = snd_sb_csp_restart(p);
243 		break;
244 	default:
245 		err = -ENOTTY;
246 		break;
247 	}
248 
249 	return err;
250 }
251 
252 /*
253  * close the device
254  */
255 static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file)
256 {
257 	struct snd_sb_csp *p = hw->private_data;
258 	return (snd_sb_csp_unuse(p));
259 }
260 
261 /* ------------------------------ */
262 
263 /*
264  * acquire device
265  */
266 static int snd_sb_csp_use(struct snd_sb_csp * p)
267 {
268 	mutex_lock(&p->access_mutex);
269 	if (p->used) {
270 		mutex_unlock(&p->access_mutex);
271 		return -EAGAIN;
272 	}
273 	p->used++;
274 	mutex_unlock(&p->access_mutex);
275 
276 	return 0;
277 
278 }
279 
280 /*
281  * release device
282  */
283 static int snd_sb_csp_unuse(struct snd_sb_csp * p)
284 {
285 	mutex_lock(&p->access_mutex);
286 	p->used--;
287 	mutex_unlock(&p->access_mutex);
288 
289 	return 0;
290 }
291 
292 /*
293  * load microcode via ioctl:
294  * code is user-space pointer
295  */
296 static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
297 				struct snd_sb_csp_microcode __user * mcode)
298 {
299 	struct snd_sb_csp_mc_header info;
300 	struct device *dev = p->chip->card->dev;
301 
302 	unsigned char __user *data_ptr;
303 	unsigned char __user *data_end;
304 	unsigned short func_nr = 0;
305 
306 	struct riff_header file_h, item_h, code_h;
307 	__le32 item_type;
308 	struct desc_header funcdesc_h;
309 
310 	unsigned long flags;
311 	int err;
312 
313 	if (copy_from_user(&info, mcode, sizeof(info)))
314 		return -EFAULT;
315 	data_ptr = mcode->data;
316 
317 	if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
318 		return -EFAULT;
319 	if ((le32_to_cpu(file_h.name) != RIFF_HEADER) ||
320 	    (le32_to_cpu(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
321 		dev_dbg(dev, "%s: Invalid RIFF header\n", __func__);
322 		return -EINVAL;
323 	}
324 	data_ptr += sizeof(file_h);
325 	data_end = data_ptr + le32_to_cpu(file_h.len);
326 
327 	if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
328 		return -EFAULT;
329 	if (le32_to_cpu(item_type) != CSP__HEADER) {
330 		dev_dbg(dev, "%s: Invalid RIFF file type\n", __func__);
331 		return -EINVAL;
332 	}
333 	data_ptr += sizeof (item_type);
334 
335 	for (; data_ptr < data_end; data_ptr += le32_to_cpu(item_h.len)) {
336 		if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
337 			return -EFAULT;
338 		data_ptr += sizeof(item_h);
339 		if (le32_to_cpu(item_h.name) != LIST_HEADER)
340 			continue;
341 
342 		if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
343 			 return -EFAULT;
344 		switch (le32_to_cpu(item_type)) {
345 		case FUNC_HEADER:
346 			if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
347 				return -EFAULT;
348 			func_nr = le16_to_cpu(funcdesc_h.func_nr);
349 			break;
350 		case CODE_HEADER:
351 			if (func_nr != info.func_req)
352 				break;	/* not required function, try next */
353 			data_ptr += sizeof(item_type);
354 
355 			/* destroy QSound mixer element */
356 			if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
357 				snd_sb_qsound_destroy(p);
358 			}
359 			/* Clear all flags */
360 			p->running = 0;
361 			p->mode = 0;
362 
363 			/* load microcode blocks */
364 			for (;;) {
365 				if (data_ptr >= data_end)
366 					return -EINVAL;
367 				if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
368 					return -EFAULT;
369 
370 				/* init microcode blocks */
371 				if (le32_to_cpu(code_h.name) != INIT_HEADER)
372 					break;
373 				data_ptr += sizeof(code_h);
374 				err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len),
375 						      SNDRV_SB_CSP_LOAD_INITBLOCK);
376 				if (err)
377 					return err;
378 				data_ptr += le32_to_cpu(code_h.len);
379 			}
380 			/* main microcode block */
381 			if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
382 				return -EFAULT;
383 
384 			if (le32_to_cpu(code_h.name) != MAIN_HEADER) {
385 				dev_dbg(dev, "%s: Missing 'main' microcode\n", __func__);
386 				return -EINVAL;
387 			}
388 			data_ptr += sizeof(code_h);
389 			err = snd_sb_csp_load_user(p, data_ptr,
390 						   le32_to_cpu(code_h.len), 0);
391 			if (err)
392 				return err;
393 
394 			/* fill in codec header */
395 			strscpy(p->codec_name, info.codec_name, sizeof(p->codec_name));
396 			p->func_nr = func_nr;
397 			p->mode = le16_to_cpu(funcdesc_h.flags_play_rec);
398 			switch (le16_to_cpu(funcdesc_h.VOC_type)) {
399 			case 0x0001:	/* QSound decoder */
400 				if (le16_to_cpu(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
401 					if (snd_sb_qsound_build(p) == 0)
402 						/* set QSound flag and clear all other mode flags */
403 						p->mode = SNDRV_SB_CSP_MODE_QSOUND;
404 				}
405 				p->acc_format = 0;
406 				break;
407 			case 0x0006:	/* A Law codec */
408 				p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
409 				break;
410 			case 0x0007:	/* Mu Law codec */
411 				p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
412 				break;
413 			case 0x0011:	/* what Creative thinks is IMA ADPCM codec */
414 			case 0x0200:	/* Creative ADPCM codec */
415 				p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
416 				break;
417 			case    201:	/* Text 2 Speech decoder */
418 				/* TODO: Text2Speech handling routines */
419 				p->acc_format = 0;
420 				break;
421 			case 0x0202:	/* Fast Speech 8 codec */
422 			case 0x0203:	/* Fast Speech 10 codec */
423 				p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
424 				break;
425 			default:	/* other codecs are unsupported */
426 				p->acc_format = p->acc_width = p->acc_rates = 0;
427 				p->mode = 0;
428 				dev_dbg(dev, "%s: Unsupported CSP codec type: 0x%04x\n",
429 					__func__,
430 					le16_to_cpu(funcdesc_h.VOC_type));
431 				return -EINVAL;
432 			}
433 			p->acc_channels = le16_to_cpu(funcdesc_h.flags_stereo_mono);
434 			p->acc_width = le16_to_cpu(funcdesc_h.flags_16bit_8bit);
435 			p->acc_rates = le16_to_cpu(funcdesc_h.flags_rates);
436 
437 			/* Decouple CSP from IRQ and DMAREQ lines */
438 			spin_lock_irqsave(&p->chip->reg_lock, flags);
439 			set_mode_register(p->chip, 0xfc);
440 			set_mode_register(p->chip, 0x00);
441 			spin_unlock_irqrestore(&p->chip->reg_lock, flags);
442 
443 			/* finished loading successfully */
444 			p->running = SNDRV_SB_CSP_ST_LOADED;	/* set LOADED flag */
445 			return 0;
446 		}
447 	}
448 	dev_dbg(dev, "%s: Function #%d not found\n", __func__, info.func_req);
449 	return -EINVAL;
450 }
451 
452 /*
453  * unload CSP microcode
454  */
455 static int snd_sb_csp_unload(struct snd_sb_csp * p)
456 {
457 	if (p->running & SNDRV_SB_CSP_ST_RUNNING)
458 		return -EBUSY;
459 	if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
460 		return -ENXIO;
461 
462 	/* clear supported formats */
463 	p->acc_format = 0;
464 	p->acc_channels = p->acc_width = p->acc_rates = 0;
465 	/* destroy QSound mixer element */
466 	if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
467 		snd_sb_qsound_destroy(p);
468 	}
469 	/* clear all flags */
470 	p->running = 0;
471 	p->mode = 0;
472 	return 0;
473 }
474 
475 /*
476  * send command sequence to DSP
477  */
478 static inline int command_seq(struct snd_sb *chip, const unsigned char *seq, int size)
479 {
480 	int i;
481 	for (i = 0; i < size; i++) {
482 		if (!snd_sbdsp_command(chip, seq[i]))
483 			return -EIO;
484 	}
485 	return 0;
486 }
487 
488 /*
489  * set CSP codec parameter
490  */
491 static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val)
492 {
493 	unsigned char dsp_cmd[3];
494 
495 	dsp_cmd[0] = 0x05;	/* CSP set codec parameter */
496 	dsp_cmd[1] = val;	/* Parameter value */
497 	dsp_cmd[2] = par;	/* Parameter */
498 	command_seq(chip, dsp_cmd, 3);
499 	snd_sbdsp_command(chip, 0x03);	/* DSP read? */
500 	if (snd_sbdsp_get_byte(chip) != par)
501 		return -EIO;
502 	return 0;
503 }
504 
505 /*
506  * set CSP register
507  */
508 static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val)
509 {
510 	unsigned char dsp_cmd[3];
511 
512 	dsp_cmd[0] = 0x0e;	/* CSP set register */
513 	dsp_cmd[1] = reg;	/* CSP Register */
514 	dsp_cmd[2] = val;	/* value */
515 	return command_seq(chip, dsp_cmd, 3);
516 }
517 
518 /*
519  * read CSP register
520  * return < 0 -> error
521  */
522 static int read_register(struct snd_sb *chip, unsigned char reg)
523 {
524 	unsigned char dsp_cmd[2];
525 
526 	dsp_cmd[0] = 0x0f;	/* CSP read register */
527 	dsp_cmd[1] = reg;	/* CSP Register */
528 	command_seq(chip, dsp_cmd, 2);
529 	return snd_sbdsp_get_byte(chip);	/* Read DSP value */
530 }
531 
532 /*
533  * set CSP mode register
534  */
535 static int set_mode_register(struct snd_sb *chip, unsigned char mode)
536 {
537 	unsigned char dsp_cmd[2];
538 
539 	dsp_cmd[0] = 0x04;	/* CSP set mode register */
540 	dsp_cmd[1] = mode;	/* mode */
541 	return command_seq(chip, dsp_cmd, 2);
542 }
543 
544 /*
545  * Detect CSP
546  * return 0 if CSP exists.
547  */
548 static int csp_detect(struct snd_sb *chip, int *version)
549 {
550 	unsigned char csp_test1, csp_test2;
551 	unsigned long flags;
552 	int result = -ENODEV;
553 
554 	spin_lock_irqsave(&chip->reg_lock, flags);
555 
556 	set_codec_parameter(chip, 0x00, 0x00);
557 	set_mode_register(chip, 0xfc);		/* 0xfc = ?? */
558 
559 	csp_test1 = read_register(chip, 0x83);
560 	set_register(chip, 0x83, ~csp_test1);
561 	csp_test2 = read_register(chip, 0x83);
562 	if (csp_test2 != (csp_test1 ^ 0xff))
563 		goto __fail;
564 
565 	set_register(chip, 0x83, csp_test1);
566 	csp_test2 = read_register(chip, 0x83);
567 	if (csp_test2 != csp_test1)
568 		goto __fail;
569 
570 	set_mode_register(chip, 0x00);		/* 0x00 = ? */
571 
572 	*version = get_version(chip);
573 	snd_sbdsp_reset(chip);	/* reset DSP after getversion! */
574 	if (*version >= 0x10 && *version <= 0x1f)
575 		result = 0;		/* valid version id */
576 
577       __fail:
578 	spin_unlock_irqrestore(&chip->reg_lock, flags);
579 	return result;
580 }
581 
582 /*
583  * get CSP version number
584  */
585 static int get_version(struct snd_sb *chip)
586 {
587 	unsigned char dsp_cmd[2];
588 
589 	dsp_cmd[0] = 0x08;	/* SB_DSP_!something! */
590 	dsp_cmd[1] = 0x03;	/* get chip version id? */
591 	command_seq(chip, dsp_cmd, 2);
592 
593 	return (snd_sbdsp_get_byte(chip));
594 }
595 
596 /*
597  * check if the CSP version is valid
598  */
599 static int snd_sb_csp_check_version(struct snd_sb_csp * p)
600 {
601 	if (p->version < 0x10 || p->version > 0x1f) {
602 		dev_dbg(p->chip->card->dev,
603 			"%s: Invalid CSP version: 0x%x\n",
604 			__func__, p->version);
605 		return 1;
606 	}
607 	return 0;
608 }
609 
610 /*
611  * download microcode to CSP (microcode should have one "main" block).
612  */
613 static int snd_sb_csp_load(struct snd_sb_csp * p, const unsigned char *buf, int size, int load_flags)
614 {
615 	int status, i;
616 	int err;
617 	int result = -EIO;
618 	unsigned long flags;
619 
620 	spin_lock_irqsave(&p->chip->reg_lock, flags);
621 	snd_sbdsp_command(p->chip, 0x01);	/* CSP download command */
622 	if (snd_sbdsp_get_byte(p->chip)) {
623 		dev_dbg(p->chip->card->dev, "%s: Download command failed\n", __func__);
624 		goto __fail;
625 	}
626 	/* Send CSP low byte (size - 1) */
627 	snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
628 	/* Send high byte */
629 	snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
630 	/* send microcode sequence */
631 	/* load from kernel space */
632 	while (size--) {
633 		if (!snd_sbdsp_command(p->chip, *buf++))
634 			goto __fail;
635 	}
636 	if (snd_sbdsp_get_byte(p->chip))
637 		goto __fail;
638 
639 	if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
640 		i = 0;
641 		/* some codecs (FastSpeech) take some time to initialize */
642 		while (1) {
643 			snd_sbdsp_command(p->chip, 0x03);
644 			status = snd_sbdsp_get_byte(p->chip);
645 			if (status == 0x55 || ++i >= 10)
646 				break;
647 			udelay (10);
648 		}
649 		if (status != 0x55) {
650 			dev_dbg(p->chip->card->dev,
651 				"%s: Microcode initialization failed\n",
652 				__func__);
653 			goto __fail;
654 		}
655 	} else {
656 		/*
657 		 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
658 		 * Start CSP chip if no 16bit DMA channel is set - some kind
659 		 * of autorun or perhaps a bugfix?
660 		 */
661 		spin_lock(&p->chip->mixer_lock);
662 		status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
663 		spin_unlock(&p->chip->mixer_lock);
664 		if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
665 			err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
666 			       set_codec_parameter(p->chip, 0xff, 0x00));
667 			snd_sbdsp_reset(p->chip);		/* really! */
668 			if (err)
669 				goto __fail;
670 			set_mode_register(p->chip, 0xc0);	/* c0 = STOP */
671 			set_mode_register(p->chip, 0x70);	/* 70 = RUN */
672 		}
673 	}
674 	result = 0;
675 
676       __fail:
677 	spin_unlock_irqrestore(&p->chip->reg_lock, flags);
678 	return result;
679 }
680 
681 static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags)
682 {
683 	int err;
684 	unsigned char *kbuf;
685 
686 	kbuf = memdup_user(buf, size);
687 	if (IS_ERR(kbuf))
688 		return PTR_ERR(kbuf);
689 
690 	err = snd_sb_csp_load(p, kbuf, size, load_flags);
691 
692 	kfree(kbuf);
693 	return err;
694 }
695 
696 static int snd_sb_csp_firmware_load(struct snd_sb_csp *p, int index, int flags)
697 {
698 	static const char *const names[] = {
699 		"sb16/mulaw_main.csp",
700 		"sb16/alaw_main.csp",
701 		"sb16/ima_adpcm_init.csp",
702 		"sb16/ima_adpcm_playback.csp",
703 		"sb16/ima_adpcm_capture.csp",
704 	};
705 	const struct firmware *program;
706 
707 	BUILD_BUG_ON(ARRAY_SIZE(names) != CSP_PROGRAM_COUNT);
708 	program = p->csp_programs[index];
709 	if (!program) {
710 		int err = request_firmware(&program, names[index],
711 				       p->chip->card->dev);
712 		if (err < 0)
713 			return err;
714 		p->csp_programs[index] = program;
715 	}
716 	return snd_sb_csp_load(p, program->data, program->size, flags);
717 }
718 
719 /*
720  * autoload hardware codec if necessary
721  * return 0 if CSP is loaded and ready to run (p->running != 0)
722  */
723 static int snd_sb_csp_autoload(struct snd_sb_csp * p, snd_pcm_format_t pcm_sfmt, int play_rec_mode)
724 {
725 	unsigned long flags;
726 	int err = 0;
727 
728 	/* if CSP is running or manually loaded then exit */
729 	if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED))
730 		return -EBUSY;
731 
732 	/* autoload microcode only if requested hardware codec is not already loaded */
733 	if (((1U << (__force int)pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
734 		p->running = SNDRV_SB_CSP_ST_AUTO;
735 	} else {
736 		switch (pcm_sfmt) {
737 		case SNDRV_PCM_FORMAT_MU_LAW:
738 			err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_MULAW, 0);
739 			p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
740 			p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
741 			break;
742 		case SNDRV_PCM_FORMAT_A_LAW:
743 			err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ALAW, 0);
744 			p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
745 			p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
746 			break;
747 		case SNDRV_PCM_FORMAT_IMA_ADPCM:
748 			err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ADPCM_INIT,
749 						       SNDRV_SB_CSP_LOAD_INITBLOCK);
750 			if (err)
751 				break;
752 			if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
753 				err = snd_sb_csp_firmware_load
754 					(p, CSP_PROGRAM_ADPCM_PLAYBACK, 0);
755 				p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
756 			} else {
757 				err = snd_sb_csp_firmware_load
758 					(p, CSP_PROGRAM_ADPCM_CAPTURE, 0);
759 				p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
760 			}
761 			p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
762 			break;
763 		default:
764 			/* Decouple CSP from IRQ and DMAREQ lines */
765 			if (p->running & SNDRV_SB_CSP_ST_AUTO) {
766 				spin_lock_irqsave(&p->chip->reg_lock, flags);
767 				set_mode_register(p->chip, 0xfc);
768 				set_mode_register(p->chip, 0x00);
769 				spin_unlock_irqrestore(&p->chip->reg_lock, flags);
770 				p->running = 0;			/* clear autoloaded flag */
771 			}
772 			return -EINVAL;
773 		}
774 		if (err) {
775 			p->acc_format = 0;
776 			p->acc_channels = p->acc_width = p->acc_rates = 0;
777 
778 			p->running = 0;				/* clear autoloaded flag */
779 			p->mode = 0;
780 			return (err);
781 		} else {
782 			p->running = SNDRV_SB_CSP_ST_AUTO;	/* set autoloaded flag */
783 			p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT;	/* only 16 bit data */
784 			p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
785 			p->acc_rates = SNDRV_SB_CSP_RATE_ALL;	/* HW codecs accept all rates */
786 		}
787 
788 	}
789 	return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
790 }
791 
792 /*
793  * start CSP
794  */
795 static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels)
796 {
797 	struct device *dev = p->chip->card->dev;
798 	unsigned char s_type;	/* sample type */
799 	unsigned char mixL, mixR;
800 	int result = -EIO;
801 	unsigned long flags;
802 
803 	if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
804 		dev_dbg(dev, "%s: Microcode not loaded\n", __func__);
805 		return -ENXIO;
806 	}
807 	if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
808 		dev_dbg(dev, "%s: CSP already running\n", __func__);
809 		return -EBUSY;
810 	}
811 	if (!(sample_width & p->acc_width)) {
812 		dev_dbg(dev, "%s: Unsupported PCM sample width\n", __func__);
813 		return -EINVAL;
814 	}
815 	if (!(channels & p->acc_channels)) {
816 		dev_dbg(dev, "%s: Invalid number of channels\n", __func__);
817 		return -EINVAL;
818 	}
819 
820 	/* Mute PCM volume */
821 	spin_lock_irqsave(&p->chip->mixer_lock, flags);
822 	mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
823 	mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
824 	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
825 	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
826 	spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
827 
828 	spin_lock(&p->chip->reg_lock);
829 	set_mode_register(p->chip, 0xc0);	/* c0 = STOP */
830 	set_mode_register(p->chip, 0x70);	/* 70 = RUN */
831 
832 	s_type = 0x00;
833 	if (channels == SNDRV_SB_CSP_MONO)
834 		s_type = 0x11;	/* 000n 000n    (n = 1 if mono) */
835 	if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
836 		s_type |= 0x22;	/* 00dX 00dX    (d = 1 if 8 bit samples) */
837 
838 	if (set_codec_parameter(p->chip, 0x81, s_type)) {
839 		dev_dbg(dev, "%s: Set sample type command failed\n", __func__);
840 		goto __fail;
841 	}
842 	if (set_codec_parameter(p->chip, 0x80, 0x00)) {
843 		dev_dbg(dev, "%s: Codec start command failed\n", __func__);
844 		goto __fail;
845 	}
846 	p->run_width = sample_width;
847 	p->run_channels = channels;
848 
849 	p->running |= SNDRV_SB_CSP_ST_RUNNING;
850 
851 	if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
852 		set_codec_parameter(p->chip, 0xe0, 0x01);
853 		/* enable QSound decoder */
854 		set_codec_parameter(p->chip, 0x00, 0xff);
855 		set_codec_parameter(p->chip, 0x01, 0xff);
856 		p->running |= SNDRV_SB_CSP_ST_QSOUND;
857 		/* set QSound startup value */
858 		snd_sb_csp_qsound_transfer(p);
859 	}
860 	result = 0;
861 
862       __fail:
863 	spin_unlock(&p->chip->reg_lock);
864 
865 	/* restore PCM volume */
866 	spin_lock_irqsave(&p->chip->mixer_lock, flags);
867 	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
868 	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
869 	spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
870 
871 	return result;
872 }
873 
874 /*
875  * stop CSP
876  */
877 static int snd_sb_csp_stop(struct snd_sb_csp * p)
878 {
879 	int result;
880 	unsigned char mixL, mixR;
881 	unsigned long flags;
882 
883 	if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
884 		return 0;
885 
886 	/* Mute PCM volume */
887 	spin_lock_irqsave(&p->chip->mixer_lock, flags);
888 	mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
889 	mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
890 	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
891 	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
892 	spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
893 
894 	spin_lock(&p->chip->reg_lock);
895 	if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
896 		set_codec_parameter(p->chip, 0xe0, 0x01);
897 		/* disable QSound decoder */
898 		set_codec_parameter(p->chip, 0x00, 0x00);
899 		set_codec_parameter(p->chip, 0x01, 0x00);
900 
901 		p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
902 	}
903 	result = set_mode_register(p->chip, 0xc0);	/* c0 = STOP */
904 	spin_unlock(&p->chip->reg_lock);
905 
906 	/* restore PCM volume */
907 	spin_lock_irqsave(&p->chip->mixer_lock, flags);
908 	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
909 	snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
910 	spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
911 
912 	if (!(result))
913 		p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
914 	return result;
915 }
916 
917 /*
918  * pause CSP codec and hold DMA transfer
919  */
920 static int snd_sb_csp_pause(struct snd_sb_csp * p)
921 {
922 	int result;
923 	unsigned long flags;
924 
925 	if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
926 		return -EBUSY;
927 
928 	spin_lock_irqsave(&p->chip->reg_lock, flags);
929 	result = set_codec_parameter(p->chip, 0x80, 0xff);
930 	spin_unlock_irqrestore(&p->chip->reg_lock, flags);
931 	if (!(result))
932 		p->running |= SNDRV_SB_CSP_ST_PAUSED;
933 
934 	return result;
935 }
936 
937 /*
938  * restart CSP codec and resume DMA transfer
939  */
940 static int snd_sb_csp_restart(struct snd_sb_csp * p)
941 {
942 	int result;
943 	unsigned long flags;
944 
945 	if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
946 		return -EBUSY;
947 
948 	spin_lock_irqsave(&p->chip->reg_lock, flags);
949 	result = set_codec_parameter(p->chip, 0x80, 0x00);
950 	spin_unlock_irqrestore(&p->chip->reg_lock, flags);
951 	if (!(result))
952 		p->running &= ~SNDRV_SB_CSP_ST_PAUSED;
953 
954 	return result;
955 }
956 
957 /* ------------------------------ */
958 
959 /*
960  * QSound mixer control for PCM
961  */
962 
963 #define snd_sb_qsound_switch_info	snd_ctl_boolean_mono_info
964 
965 static int snd_sb_qsound_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
966 {
967 	struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
968 
969 	ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
970 	return 0;
971 }
972 
973 static int snd_sb_qsound_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
974 {
975 	struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
976 	unsigned long flags;
977 	int change;
978 	unsigned char nval;
979 
980 	nval = ucontrol->value.integer.value[0] & 0x01;
981 	spin_lock_irqsave(&p->q_lock, flags);
982 	change = p->q_enabled != nval;
983 	p->q_enabled = nval;
984 	spin_unlock_irqrestore(&p->q_lock, flags);
985 	return change;
986 }
987 
988 static int snd_sb_qsound_space_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
989 {
990 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
991 	uinfo->count = 2;
992 	uinfo->value.integer.min = 0;
993 	uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
994 	return 0;
995 }
996 
997 static int snd_sb_qsound_space_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
998 {
999 	struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
1000 	unsigned long flags;
1001 
1002 	spin_lock_irqsave(&p->q_lock, flags);
1003 	ucontrol->value.integer.value[0] = p->qpos_left;
1004 	ucontrol->value.integer.value[1] = p->qpos_right;
1005 	spin_unlock_irqrestore(&p->q_lock, flags);
1006 	return 0;
1007 }
1008 
1009 static int snd_sb_qsound_space_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1010 {
1011 	struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
1012 	unsigned long flags;
1013 	int change;
1014 	unsigned char nval1, nval2;
1015 
1016 	nval1 = ucontrol->value.integer.value[0];
1017 	if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1018 		nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1019 	nval2 = ucontrol->value.integer.value[1];
1020 	if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1021 		nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1022 	spin_lock_irqsave(&p->q_lock, flags);
1023 	change = p->qpos_left != nval1 || p->qpos_right != nval2;
1024 	p->qpos_left = nval1;
1025 	p->qpos_right = nval2;
1026 	p->qpos_changed = change;
1027 	spin_unlock_irqrestore(&p->q_lock, flags);
1028 	return change;
1029 }
1030 
1031 static const struct snd_kcontrol_new snd_sb_qsound_switch = {
1032 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1033 	.name = "3D Control - Switch",
1034 	.info = snd_sb_qsound_switch_info,
1035 	.get = snd_sb_qsound_switch_get,
1036 	.put = snd_sb_qsound_switch_put
1037 };
1038 
1039 static const struct snd_kcontrol_new snd_sb_qsound_space = {
1040 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1041 	.name = "3D Control - Space",
1042 	.info = snd_sb_qsound_space_info,
1043 	.get = snd_sb_qsound_space_get,
1044 	.put = snd_sb_qsound_space_put
1045 };
1046 
1047 static int snd_sb_qsound_build(struct snd_sb_csp * p)
1048 {
1049 	struct snd_card *card;
1050 	struct snd_kcontrol *kctl;
1051 	int err;
1052 
1053 	if (snd_BUG_ON(!p))
1054 		return -EINVAL;
1055 
1056 	card = p->chip->card;
1057 	p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
1058 	p->qpos_changed = 0;
1059 
1060 	spin_lock_init(&p->q_lock);
1061 
1062 	kctl = snd_ctl_new1(&snd_sb_qsound_switch, p);
1063 	err = snd_ctl_add(card, kctl);
1064 	if (err < 0)
1065 		goto __error;
1066 	p->qsound_switch = kctl;
1067 	kctl = snd_ctl_new1(&snd_sb_qsound_space, p);
1068 	err = snd_ctl_add(card, kctl);
1069 	if (err < 0)
1070 		goto __error;
1071 	p->qsound_space = kctl;
1072 
1073 	return 0;
1074 
1075      __error:
1076 	snd_sb_qsound_destroy(p);
1077 	return err;
1078 }
1079 
1080 static void snd_sb_qsound_destroy(struct snd_sb_csp * p)
1081 {
1082 	struct snd_card *card;
1083 	unsigned long flags;
1084 
1085 	if (snd_BUG_ON(!p))
1086 		return;
1087 
1088 	card = p->chip->card;
1089 
1090 	snd_ctl_remove(card, p->qsound_switch);
1091 	p->qsound_switch = NULL;
1092 	snd_ctl_remove(card, p->qsound_space);
1093 	p->qsound_space = NULL;
1094 
1095 	/* cancel pending transfer of QSound parameters */
1096 	spin_lock_irqsave (&p->q_lock, flags);
1097 	p->qpos_changed = 0;
1098 	spin_unlock_irqrestore (&p->q_lock, flags);
1099 }
1100 
1101 /*
1102  * Transfer qsound parameters to CSP,
1103  * function should be called from interrupt routine
1104  */
1105 static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p)
1106 {
1107 	int err = -ENXIO;
1108 
1109 	spin_lock(&p->q_lock);
1110 	if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1111 		set_codec_parameter(p->chip, 0xe0, 0x01);
1112 		/* left channel */
1113 		set_codec_parameter(p->chip, 0x00, p->qpos_left);
1114 		set_codec_parameter(p->chip, 0x02, 0x00);
1115 		/* right channel */
1116 		set_codec_parameter(p->chip, 0x00, p->qpos_right);
1117 		set_codec_parameter(p->chip, 0x03, 0x00);
1118 		err = 0;
1119 	}
1120 	p->qpos_changed = 0;
1121 	spin_unlock(&p->q_lock);
1122 	return err;
1123 }
1124 
1125 /* ------------------------------ */
1126 
1127 /*
1128  * proc interface
1129  */
1130 static int init_proc_entry(struct snd_sb_csp * p, int device)
1131 {
1132 	char name[16];
1133 
1134 	sprintf(name, "cspD%d", device);
1135 	snd_card_ro_proc_new(p->chip->card, name, p, info_read);
1136 	return 0;
1137 }
1138 
1139 static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
1140 {
1141 	struct snd_sb_csp *p = entry->private_data;
1142 
1143 	snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
1144 	snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
1145 		    ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
1146 		    ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
1147 		    ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
1148 	if (p->running & SNDRV_SB_CSP_ST_LOADED) {
1149 		snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
1150 		snd_iprintf(buffer, "Sample rates: ");
1151 		if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
1152 			snd_iprintf(buffer, "All\n");
1153 		} else {
1154 			snd_iprintf(buffer, "%s%s%s%s\n",
1155 				    ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
1156 				    ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
1157 				    ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
1158 				    ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
1159 		}
1160 		if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
1161 			snd_iprintf(buffer, "QSound decoder %s\n",
1162 				    str_enabled_disabled(p->q_enabled));
1163 		} else {
1164 			snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
1165 				    p->acc_format,
1166 				    ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
1167 				    ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
1168 				    ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
1169 				    ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
1170 				    ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
1171 				    ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
1172 		}
1173 	}
1174 	if (p->running & SNDRV_SB_CSP_ST_AUTO) {
1175 		snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1176 	}
1177 	if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
1178 		snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
1179 			    ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
1180 			    ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
1181 	}
1182 	if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1183 		snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
1184 			    p->qpos_left, p->qpos_right);
1185 	}
1186 }
1187 
1188 /* */
1189 
1190 EXPORT_SYMBOL(snd_sb_csp_new);
1191