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