1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 1999 by Uros Bizjak <uros@kss-loka.si>
4 * Takashi Iwai <tiwai@suse.de>
6 * SB16ASP/AWE32 CSP control
8 * CSP microcode loader:
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>
22 MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
23 MODULE_DESCRIPTION("ALSA driver for SB16 Creative Signal Processor");
24 MODULE_LICENSE("GPL");
27 #ifdef SNDRV_LITTLE_ENDIAN
28 #define CSP_HDR_VALUE(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
30 #define CSP_HDR_VALUE(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
33 #define RIFF_HEADER CSP_HDR_VALUE('R', 'I', 'F', 'F')
34 #define CSP__HEADER CSP_HDR_VALUE('C', 'S', 'P', ' ')
35 #define LIST_HEADER CSP_HDR_VALUE('L', 'I', 'S', 'T')
36 #define FUNC_HEADER CSP_HDR_VALUE('f', 'u', 'n', 'c')
37 #define CODE_HEADER CSP_HDR_VALUE('c', 'o', 'd', 'e')
38 #define INIT_HEADER CSP_HDR_VALUE('i', 'n', 'i', 't')
39 #define MAIN_HEADER CSP_HDR_VALUE('m', 'a', 'i', 'n')
50 struct riff_header info;
53 __le16 flags_play_rec;
54 __le16 flags_16bit_8bit;
55 __le16 flags_stereo_mono;
62 static void snd_sb_csp_free(struct snd_hwdep *hw);
63 static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file);
64 static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg);
65 static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file);
67 static int csp_detect(struct snd_sb *chip, int *version);
68 static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val);
69 static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val);
70 static int read_register(struct snd_sb *chip, unsigned char reg);
71 static int set_mode_register(struct snd_sb *chip, unsigned char mode);
72 static int get_version(struct snd_sb *chip);
74 static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
75 struct snd_sb_csp_microcode __user * code);
76 static int snd_sb_csp_unload(struct snd_sb_csp * p);
77 static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags);
78 static int snd_sb_csp_autoload(struct snd_sb_csp * p, snd_pcm_format_t pcm_sfmt, int play_rec_mode);
79 static int snd_sb_csp_check_version(struct snd_sb_csp * p);
81 static int snd_sb_csp_use(struct snd_sb_csp * p);
82 static int snd_sb_csp_unuse(struct snd_sb_csp * p);
83 static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels);
84 static int snd_sb_csp_stop(struct snd_sb_csp * p);
85 static int snd_sb_csp_pause(struct snd_sb_csp * p);
86 static int snd_sb_csp_restart(struct snd_sb_csp * p);
88 static int snd_sb_qsound_build(struct snd_sb_csp * p);
89 static void snd_sb_qsound_destroy(struct snd_sb_csp * p);
90 static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p);
92 static int init_proc_entry(struct snd_sb_csp * p, int device);
93 static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer);
96 * Detect CSP chip and create a new instance
98 int snd_sb_csp_new(struct snd_sb *chip, int device, struct snd_hwdep ** rhwdep)
100 struct snd_sb_csp *p;
101 int uninitialized_var(version);
103 struct snd_hwdep *hw;
108 if (csp_detect(chip, &version))
111 if ((err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw)) < 0)
114 if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
115 snd_device_free(chip->card, hw);
119 p->version = version;
122 p->ops.csp_use = snd_sb_csp_use;
123 p->ops.csp_unuse = snd_sb_csp_unuse;
124 p->ops.csp_autoload = snd_sb_csp_autoload;
125 p->ops.csp_start = snd_sb_csp_start;
126 p->ops.csp_stop = snd_sb_csp_stop;
127 p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;
129 mutex_init(&p->access_mutex);
130 sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f));
131 hw->iface = SNDRV_HWDEP_IFACE_SB16CSP;
132 hw->private_data = p;
133 hw->private_free = snd_sb_csp_free;
135 /* operators - only write/ioctl */
136 hw->ops.open = snd_sb_csp_open;
137 hw->ops.ioctl = snd_sb_csp_ioctl;
138 hw->ops.release = snd_sb_csp_release;
140 /* create a proc entry */
141 init_proc_entry(p, device);
148 * free_private for hwdep instance
150 static void snd_sb_csp_free(struct snd_hwdep *hwdep)
153 struct snd_sb_csp *p = hwdep->private_data;
155 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
157 for (i = 0; i < ARRAY_SIZE(p->csp_programs); ++i)
158 release_firmware(p->csp_programs[i]);
163 /* ------------------------------ */
166 * open the device exclusively
168 static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file)
170 struct snd_sb_csp *p = hw->private_data;
171 return (snd_sb_csp_use(p));
175 * ioctl for hwdep device:
177 static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg)
179 struct snd_sb_csp *p = hw->private_data;
180 struct snd_sb_csp_info info;
181 struct snd_sb_csp_start start_info;
187 if (snd_sb_csp_check_version(p))
191 /* get information */
192 case SNDRV_SB_CSP_IOCTL_INFO:
193 memset(&info, 0, sizeof(info));
194 *info.codec_name = *p->codec_name;
195 info.func_nr = p->func_nr;
196 info.acc_format = p->acc_format;
197 info.acc_channels = p->acc_channels;
198 info.acc_width = p->acc_width;
199 info.acc_rates = p->acc_rates;
200 info.csp_mode = p->mode;
201 info.run_channels = p->run_channels;
202 info.run_width = p->run_width;
203 info.version = p->version;
204 info.state = p->running;
205 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
211 /* load CSP microcode */
212 case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
213 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
214 -EBUSY : snd_sb_csp_riff_load(p, (struct snd_sb_csp_microcode __user *) arg));
216 case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
217 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
218 -EBUSY : snd_sb_csp_unload(p));
221 /* change CSP running state */
222 case SNDRV_SB_CSP_IOCTL_START:
223 if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info)))
226 err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
228 case SNDRV_SB_CSP_IOCTL_STOP:
229 err = snd_sb_csp_stop(p);
231 case SNDRV_SB_CSP_IOCTL_PAUSE:
232 err = snd_sb_csp_pause(p);
234 case SNDRV_SB_CSP_IOCTL_RESTART:
235 err = snd_sb_csp_restart(p);
248 static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file)
250 struct snd_sb_csp *p = hw->private_data;
251 return (snd_sb_csp_unuse(p));
254 /* ------------------------------ */
259 static int snd_sb_csp_use(struct snd_sb_csp * p)
261 mutex_lock(&p->access_mutex);
263 mutex_unlock(&p->access_mutex);
267 mutex_unlock(&p->access_mutex);
276 static int snd_sb_csp_unuse(struct snd_sb_csp * p)
278 mutex_lock(&p->access_mutex);
280 mutex_unlock(&p->access_mutex);
286 * load microcode via ioctl:
287 * code is user-space pointer
289 static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
290 struct snd_sb_csp_microcode __user * mcode)
292 struct snd_sb_csp_mc_header info;
294 unsigned char __user *data_ptr;
295 unsigned char __user *data_end;
296 unsigned short func_nr = 0;
298 struct riff_header file_h, item_h, code_h;
300 struct desc_header funcdesc_h;
305 if (copy_from_user(&info, mcode, sizeof(info)))
307 data_ptr = mcode->data;
309 if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
311 if ((le32_to_cpu(file_h.name) != RIFF_HEADER) ||
312 (le32_to_cpu(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
313 snd_printd("%s: Invalid RIFF header\n", __func__);
316 data_ptr += sizeof(file_h);
317 data_end = data_ptr + le32_to_cpu(file_h.len);
319 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
321 if (le32_to_cpu(item_type) != CSP__HEADER) {
322 snd_printd("%s: Invalid RIFF file type\n", __func__);
325 data_ptr += sizeof (item_type);
327 for (; data_ptr < data_end; data_ptr += le32_to_cpu(item_h.len)) {
328 if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
330 data_ptr += sizeof(item_h);
331 if (le32_to_cpu(item_h.name) != LIST_HEADER)
334 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
336 switch (le32_to_cpu(item_type)) {
338 if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
340 func_nr = le16_to_cpu(funcdesc_h.func_nr);
343 if (func_nr != info.func_req)
344 break; /* not required function, try next */
345 data_ptr += sizeof(item_type);
347 /* destroy QSound mixer element */
348 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
349 snd_sb_qsound_destroy(p);
351 /* Clear all flags */
355 /* load microcode blocks */
357 if (data_ptr >= data_end)
359 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
362 /* init microcode blocks */
363 if (le32_to_cpu(code_h.name) != INIT_HEADER)
365 data_ptr += sizeof(code_h);
366 err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len),
367 SNDRV_SB_CSP_LOAD_INITBLOCK);
370 data_ptr += le32_to_cpu(code_h.len);
372 /* main microcode block */
373 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
376 if (le32_to_cpu(code_h.name) != MAIN_HEADER) {
377 snd_printd("%s: Missing 'main' microcode\n", __func__);
380 data_ptr += sizeof(code_h);
381 err = snd_sb_csp_load_user(p, data_ptr,
382 le32_to_cpu(code_h.len), 0);
386 /* fill in codec header */
387 strlcpy(p->codec_name, info.codec_name, sizeof(p->codec_name));
388 p->func_nr = func_nr;
389 p->mode = le16_to_cpu(funcdesc_h.flags_play_rec);
390 switch (le16_to_cpu(funcdesc_h.VOC_type)) {
391 case 0x0001: /* QSound decoder */
392 if (le16_to_cpu(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
393 if (snd_sb_qsound_build(p) == 0)
394 /* set QSound flag and clear all other mode flags */
395 p->mode = SNDRV_SB_CSP_MODE_QSOUND;
399 case 0x0006: /* A Law codec */
400 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
402 case 0x0007: /* Mu Law codec */
403 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
405 case 0x0011: /* what Creative thinks is IMA ADPCM codec */
406 case 0x0200: /* Creative ADPCM codec */
407 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
409 case 201: /* Text 2 Speech decoder */
410 /* TODO: Text2Speech handling routines */
413 case 0x0202: /* Fast Speech 8 codec */
414 case 0x0203: /* Fast Speech 10 codec */
415 p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
417 default: /* other codecs are unsupported */
418 p->acc_format = p->acc_width = p->acc_rates = 0;
420 snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
422 le16_to_cpu(funcdesc_h.VOC_type));
425 p->acc_channels = le16_to_cpu(funcdesc_h.flags_stereo_mono);
426 p->acc_width = le16_to_cpu(funcdesc_h.flags_16bit_8bit);
427 p->acc_rates = le16_to_cpu(funcdesc_h.flags_rates);
429 /* Decouple CSP from IRQ and DMAREQ lines */
430 spin_lock_irqsave(&p->chip->reg_lock, flags);
431 set_mode_register(p->chip, 0xfc);
432 set_mode_register(p->chip, 0x00);
433 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
435 /* finished loading successfully */
436 p->running = SNDRV_SB_CSP_ST_LOADED; /* set LOADED flag */
440 snd_printd("%s: Function #%d not found\n", __func__, info.func_req);
445 * unload CSP microcode
447 static int snd_sb_csp_unload(struct snd_sb_csp * p)
449 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
451 if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
454 /* clear supported formats */
456 p->acc_channels = p->acc_width = p->acc_rates = 0;
457 /* destroy QSound mixer element */
458 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
459 snd_sb_qsound_destroy(p);
461 /* clear all flags */
468 * send command sequence to DSP
470 static inline int command_seq(struct snd_sb *chip, const unsigned char *seq, int size)
473 for (i = 0; i < size; i++) {
474 if (!snd_sbdsp_command(chip, seq[i]))
481 * set CSP codec parameter
483 static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val)
485 unsigned char dsp_cmd[3];
487 dsp_cmd[0] = 0x05; /* CSP set codec parameter */
488 dsp_cmd[1] = val; /* Parameter value */
489 dsp_cmd[2] = par; /* Parameter */
490 command_seq(chip, dsp_cmd, 3);
491 snd_sbdsp_command(chip, 0x03); /* DSP read? */
492 if (snd_sbdsp_get_byte(chip) != par)
500 static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val)
502 unsigned char dsp_cmd[3];
504 dsp_cmd[0] = 0x0e; /* CSP set register */
505 dsp_cmd[1] = reg; /* CSP Register */
506 dsp_cmd[2] = val; /* value */
507 return command_seq(chip, dsp_cmd, 3);
512 * return < 0 -> error
514 static int read_register(struct snd_sb *chip, unsigned char reg)
516 unsigned char dsp_cmd[2];
518 dsp_cmd[0] = 0x0f; /* CSP read register */
519 dsp_cmd[1] = reg; /* CSP Register */
520 command_seq(chip, dsp_cmd, 2);
521 return snd_sbdsp_get_byte(chip); /* Read DSP value */
525 * set CSP mode register
527 static int set_mode_register(struct snd_sb *chip, unsigned char mode)
529 unsigned char dsp_cmd[2];
531 dsp_cmd[0] = 0x04; /* CSP set mode register */
532 dsp_cmd[1] = mode; /* mode */
533 return command_seq(chip, dsp_cmd, 2);
538 * return 0 if CSP exists.
540 static int csp_detect(struct snd_sb *chip, int *version)
542 unsigned char csp_test1, csp_test2;
544 int result = -ENODEV;
546 spin_lock_irqsave(&chip->reg_lock, flags);
548 set_codec_parameter(chip, 0x00, 0x00);
549 set_mode_register(chip, 0xfc); /* 0xfc = ?? */
551 csp_test1 = read_register(chip, 0x83);
552 set_register(chip, 0x83, ~csp_test1);
553 csp_test2 = read_register(chip, 0x83);
554 if (csp_test2 != (csp_test1 ^ 0xff))
557 set_register(chip, 0x83, csp_test1);
558 csp_test2 = read_register(chip, 0x83);
559 if (csp_test2 != csp_test1)
562 set_mode_register(chip, 0x00); /* 0x00 = ? */
564 *version = get_version(chip);
565 snd_sbdsp_reset(chip); /* reset DSP after getversion! */
566 if (*version >= 0x10 && *version <= 0x1f)
567 result = 0; /* valid version id */
570 spin_unlock_irqrestore(&chip->reg_lock, flags);
575 * get CSP version number
577 static int get_version(struct snd_sb *chip)
579 unsigned char dsp_cmd[2];
581 dsp_cmd[0] = 0x08; /* SB_DSP_!something! */
582 dsp_cmd[1] = 0x03; /* get chip version id? */
583 command_seq(chip, dsp_cmd, 2);
585 return (snd_sbdsp_get_byte(chip));
589 * check if the CSP version is valid
591 static int snd_sb_csp_check_version(struct snd_sb_csp * p)
593 if (p->version < 0x10 || p->version > 0x1f) {
594 snd_printd("%s: Invalid CSP version: 0x%x\n", __func__, p->version);
601 * download microcode to CSP (microcode should have one "main" block).
603 static int snd_sb_csp_load(struct snd_sb_csp * p, const unsigned char *buf, int size, int load_flags)
610 spin_lock_irqsave(&p->chip->reg_lock, flags);
611 snd_sbdsp_command(p->chip, 0x01); /* CSP download command */
612 if (snd_sbdsp_get_byte(p->chip)) {
613 snd_printd("%s: Download command failed\n", __func__);
616 /* Send CSP low byte (size - 1) */
617 snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
619 snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
620 /* send microcode sequence */
621 /* load from kernel space */
623 if (!snd_sbdsp_command(p->chip, *buf++))
626 if (snd_sbdsp_get_byte(p->chip))
629 if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
631 /* some codecs (FastSpeech) take some time to initialize */
633 snd_sbdsp_command(p->chip, 0x03);
634 status = snd_sbdsp_get_byte(p->chip);
635 if (status == 0x55 || ++i >= 10)
639 if (status != 0x55) {
640 snd_printd("%s: Microcode initialization failed\n", __func__);
645 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
646 * Start CSP chip if no 16bit DMA channel is set - some kind
647 * of autorun or perhaps a bugfix?
649 spin_lock(&p->chip->mixer_lock);
650 status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
651 spin_unlock(&p->chip->mixer_lock);
652 if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
653 err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
654 set_codec_parameter(p->chip, 0xff, 0x00));
655 snd_sbdsp_reset(p->chip); /* really! */
658 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
659 set_mode_register(p->chip, 0x70); /* 70 = RUN */
665 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
669 static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags)
674 kbuf = memdup_user(buf, size);
676 return PTR_ERR(kbuf);
678 err = snd_sb_csp_load(p, kbuf, size, load_flags);
684 static int snd_sb_csp_firmware_load(struct snd_sb_csp *p, int index, int flags)
686 static const char *const names[] = {
693 const struct firmware *program;
695 BUILD_BUG_ON(ARRAY_SIZE(names) != CSP_PROGRAM_COUNT);
696 program = p->csp_programs[index];
698 int err = reject_firmware(&program, names[index],
702 p->csp_programs[index] = program;
704 return snd_sb_csp_load(p, program->data, program->size, flags);
708 * autoload hardware codec if necessary
709 * return 0 if CSP is loaded and ready to run (p->running != 0)
711 static int snd_sb_csp_autoload(struct snd_sb_csp * p, snd_pcm_format_t pcm_sfmt, int play_rec_mode)
716 /* if CSP is running or manually loaded then exit */
717 if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED))
720 /* autoload microcode only if requested hardware codec is not already loaded */
721 if (((1U << (__force int)pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
722 p->running = SNDRV_SB_CSP_ST_AUTO;
725 case SNDRV_PCM_FORMAT_MU_LAW:
726 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_MULAW, 0);
727 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
728 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
730 case SNDRV_PCM_FORMAT_A_LAW:
731 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ALAW, 0);
732 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
733 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
735 case SNDRV_PCM_FORMAT_IMA_ADPCM:
736 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ADPCM_INIT,
737 SNDRV_SB_CSP_LOAD_INITBLOCK);
740 if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
741 err = snd_sb_csp_firmware_load
742 (p, CSP_PROGRAM_ADPCM_PLAYBACK, 0);
743 p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
745 err = snd_sb_csp_firmware_load
746 (p, CSP_PROGRAM_ADPCM_CAPTURE, 0);
747 p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
749 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
752 /* Decouple CSP from IRQ and DMAREQ lines */
753 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
754 spin_lock_irqsave(&p->chip->reg_lock, flags);
755 set_mode_register(p->chip, 0xfc);
756 set_mode_register(p->chip, 0x00);
757 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
758 p->running = 0; /* clear autoloaded flag */
764 p->acc_channels = p->acc_width = p->acc_rates = 0;
766 p->running = 0; /* clear autoloaded flag */
770 p->running = SNDRV_SB_CSP_ST_AUTO; /* set autoloaded flag */
771 p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT; /* only 16 bit data */
772 p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
773 p->acc_rates = SNDRV_SB_CSP_RATE_ALL; /* HW codecs accept all rates */
777 return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
783 static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels)
785 unsigned char s_type; /* sample type */
786 unsigned char mixL, mixR;
790 if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
791 snd_printd("%s: Microcode not loaded\n", __func__);
794 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
795 snd_printd("%s: CSP already running\n", __func__);
798 if (!(sample_width & p->acc_width)) {
799 snd_printd("%s: Unsupported PCM sample width\n", __func__);
802 if (!(channels & p->acc_channels)) {
803 snd_printd("%s: Invalid number of channels\n", __func__);
807 /* Mute PCM volume */
808 spin_lock_irqsave(&p->chip->mixer_lock, flags);
809 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
810 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
811 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
812 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
814 spin_lock(&p->chip->reg_lock);
815 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
816 set_mode_register(p->chip, 0x70); /* 70 = RUN */
819 if (channels == SNDRV_SB_CSP_MONO)
820 s_type = 0x11; /* 000n 000n (n = 1 if mono) */
821 if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
822 s_type |= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
824 if (set_codec_parameter(p->chip, 0x81, s_type)) {
825 snd_printd("%s: Set sample type command failed\n", __func__);
828 if (set_codec_parameter(p->chip, 0x80, 0x00)) {
829 snd_printd("%s: Codec start command failed\n", __func__);
832 p->run_width = sample_width;
833 p->run_channels = channels;
835 p->running |= SNDRV_SB_CSP_ST_RUNNING;
837 if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
838 set_codec_parameter(p->chip, 0xe0, 0x01);
839 /* enable QSound decoder */
840 set_codec_parameter(p->chip, 0x00, 0xff);
841 set_codec_parameter(p->chip, 0x01, 0xff);
842 p->running |= SNDRV_SB_CSP_ST_QSOUND;
843 /* set QSound startup value */
844 snd_sb_csp_qsound_transfer(p);
849 spin_unlock(&p->chip->reg_lock);
851 /* restore PCM volume */
852 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
853 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
854 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
862 static int snd_sb_csp_stop(struct snd_sb_csp * p)
865 unsigned char mixL, mixR;
868 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
871 /* Mute PCM volume */
872 spin_lock_irqsave(&p->chip->mixer_lock, flags);
873 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
874 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
875 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
876 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
878 spin_lock(&p->chip->reg_lock);
879 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
880 set_codec_parameter(p->chip, 0xe0, 0x01);
881 /* disable QSound decoder */
882 set_codec_parameter(p->chip, 0x00, 0x00);
883 set_codec_parameter(p->chip, 0x01, 0x00);
885 p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
887 result = set_mode_register(p->chip, 0xc0); /* c0 = STOP */
888 spin_unlock(&p->chip->reg_lock);
890 /* restore PCM volume */
891 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
892 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
893 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
896 p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
901 * pause CSP codec and hold DMA transfer
903 static int snd_sb_csp_pause(struct snd_sb_csp * p)
908 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
911 spin_lock_irqsave(&p->chip->reg_lock, flags);
912 result = set_codec_parameter(p->chip, 0x80, 0xff);
913 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
915 p->running |= SNDRV_SB_CSP_ST_PAUSED;
921 * restart CSP codec and resume DMA transfer
923 static int snd_sb_csp_restart(struct snd_sb_csp * p)
928 if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
931 spin_lock_irqsave(&p->chip->reg_lock, flags);
932 result = set_codec_parameter(p->chip, 0x80, 0x00);
933 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
935 p->running &= ~SNDRV_SB_CSP_ST_PAUSED;
940 /* ------------------------------ */
943 * QSound mixer control for PCM
946 #define snd_sb_qsound_switch_info snd_ctl_boolean_mono_info
948 static int snd_sb_qsound_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
950 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
952 ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
956 static int snd_sb_qsound_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
958 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
963 nval = ucontrol->value.integer.value[0] & 0x01;
964 spin_lock_irqsave(&p->q_lock, flags);
965 change = p->q_enabled != nval;
967 spin_unlock_irqrestore(&p->q_lock, flags);
971 static int snd_sb_qsound_space_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
973 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
975 uinfo->value.integer.min = 0;
976 uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
980 static int snd_sb_qsound_space_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
982 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
985 spin_lock_irqsave(&p->q_lock, flags);
986 ucontrol->value.integer.value[0] = p->qpos_left;
987 ucontrol->value.integer.value[1] = p->qpos_right;
988 spin_unlock_irqrestore(&p->q_lock, flags);
992 static int snd_sb_qsound_space_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
994 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
997 unsigned char nval1, nval2;
999 nval1 = ucontrol->value.integer.value[0];
1000 if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1001 nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1002 nval2 = ucontrol->value.integer.value[1];
1003 if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1004 nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1005 spin_lock_irqsave(&p->q_lock, flags);
1006 change = p->qpos_left != nval1 || p->qpos_right != nval2;
1007 p->qpos_left = nval1;
1008 p->qpos_right = nval2;
1009 p->qpos_changed = change;
1010 spin_unlock_irqrestore(&p->q_lock, flags);
1014 static const struct snd_kcontrol_new snd_sb_qsound_switch = {
1015 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1016 .name = "3D Control - Switch",
1017 .info = snd_sb_qsound_switch_info,
1018 .get = snd_sb_qsound_switch_get,
1019 .put = snd_sb_qsound_switch_put
1022 static const struct snd_kcontrol_new snd_sb_qsound_space = {
1023 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1024 .name = "3D Control - Space",
1025 .info = snd_sb_qsound_space_info,
1026 .get = snd_sb_qsound_space_get,
1027 .put = snd_sb_qsound_space_put
1030 static int snd_sb_qsound_build(struct snd_sb_csp * p)
1032 struct snd_card *card;
1038 card = p->chip->card;
1039 p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
1040 p->qpos_changed = 0;
1042 spin_lock_init(&p->q_lock);
1044 if ((err = snd_ctl_add(card, p->qsound_switch = snd_ctl_new1(&snd_sb_qsound_switch, p))) < 0)
1046 if ((err = snd_ctl_add(card, p->qsound_space = snd_ctl_new1(&snd_sb_qsound_space, p))) < 0)
1052 snd_sb_qsound_destroy(p);
1056 static void snd_sb_qsound_destroy(struct snd_sb_csp * p)
1058 struct snd_card *card;
1059 unsigned long flags;
1064 card = p->chip->card;
1066 down_write(&card->controls_rwsem);
1067 if (p->qsound_switch)
1068 snd_ctl_remove(card, p->qsound_switch);
1069 if (p->qsound_space)
1070 snd_ctl_remove(card, p->qsound_space);
1071 up_write(&card->controls_rwsem);
1073 /* cancel pending transfer of QSound parameters */
1074 spin_lock_irqsave (&p->q_lock, flags);
1075 p->qpos_changed = 0;
1076 spin_unlock_irqrestore (&p->q_lock, flags);
1080 * Transfer qsound parameters to CSP,
1081 * function should be called from interrupt routine
1083 static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p)
1087 spin_lock(&p->q_lock);
1088 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1089 set_codec_parameter(p->chip, 0xe0, 0x01);
1091 set_codec_parameter(p->chip, 0x00, p->qpos_left);
1092 set_codec_parameter(p->chip, 0x02, 0x00);
1094 set_codec_parameter(p->chip, 0x00, p->qpos_right);
1095 set_codec_parameter(p->chip, 0x03, 0x00);
1098 p->qpos_changed = 0;
1099 spin_unlock(&p->q_lock);
1103 /* ------------------------------ */
1108 static int init_proc_entry(struct snd_sb_csp * p, int device)
1112 sprintf(name, "cspD%d", device);
1113 snd_card_ro_proc_new(p->chip->card, name, p, info_read);
1117 static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
1119 struct snd_sb_csp *p = entry->private_data;
1121 snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
1122 snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
1123 ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
1124 ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
1125 ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
1126 if (p->running & SNDRV_SB_CSP_ST_LOADED) {
1127 snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
1128 snd_iprintf(buffer, "Sample rates: ");
1129 if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
1130 snd_iprintf(buffer, "All\n");
1132 snd_iprintf(buffer, "%s%s%s%s\n",
1133 ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
1134 ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
1135 ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
1136 ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
1138 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
1139 snd_iprintf(buffer, "QSound decoder %sabled\n",
1140 p->q_enabled ? "en" : "dis");
1142 snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
1144 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
1145 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
1146 ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
1147 ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
1148 ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
1149 ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
1152 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
1153 snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1155 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
1156 snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
1157 ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
1158 ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
1160 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1161 snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
1162 p->qpos_left, p->qpos_right);
1168 EXPORT_SYMBOL(snd_sb_csp_new);