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