2 * Copyright (c) 1999 by Uros Bizjak <uros@kss-loka.si>
3 * Takashi Iwai <tiwai@suse.de>
5 * SB16ASP/AWE32 CSP control
7 * CSP microcode loader:
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.
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.
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
26 #include <sound/driver.h>
27 #include <linux/delay.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/sb16_csp.h>
34 #include <sound/initval.h>
36 MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
37 MODULE_DESCRIPTION("ALSA driver for SB16 Creative Signal Processor");
38 MODULE_LICENSE("GPL");
39 #ifndef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
40 MODULE_FIRMWARE("sb16/mulaw_main.csp");
41 MODULE_FIRMWARE("sb16/alaw_main.csp");
42 MODULE_FIRMWARE("sb16/ima_adpcm_init.csp");
43 MODULE_FIRMWARE("sb16/ima_adpcm_playback.csp");
44 MODULE_FIRMWARE("sb16/ima_adpcm_capture.csp");
47 #ifdef SNDRV_LITTLE_ENDIAN
48 #define CSP_HDR_VALUE(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
50 #define CSP_HDR_VALUE(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
53 #define RIFF_HEADER CSP_HDR_VALUE('R', 'I', 'F', 'F')
54 #define CSP__HEADER CSP_HDR_VALUE('C', 'S', 'P', ' ')
55 #define LIST_HEADER CSP_HDR_VALUE('L', 'I', 'S', 'T')
56 #define FUNC_HEADER CSP_HDR_VALUE('f', 'u', 'n', 'c')
57 #define CODE_HEADER CSP_HDR_VALUE('c', 'o', 'd', 'e')
58 #define INIT_HEADER CSP_HDR_VALUE('i', 'n', 'i', 't')
59 #define MAIN_HEADER CSP_HDR_VALUE('m', 'a', 'i', 'n')
70 struct riff_header info;
74 __u16 flags_16bit_8bit;
75 __u16 flags_stereo_mono;
82 static void snd_sb_csp_free(struct snd_hwdep *hw);
83 static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file);
84 static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg);
85 static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file);
87 static int csp_detect(struct snd_sb *chip, int *version);
88 static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val);
89 static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val);
90 static int read_register(struct snd_sb *chip, unsigned char reg);
91 static int set_mode_register(struct snd_sb *chip, unsigned char mode);
92 static int get_version(struct snd_sb *chip);
94 static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
95 struct snd_sb_csp_microcode __user * code);
96 static int snd_sb_csp_unload(struct snd_sb_csp * p);
97 static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags);
98 static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode);
99 static int snd_sb_csp_check_version(struct snd_sb_csp * p);
101 static int snd_sb_csp_use(struct snd_sb_csp * p);
102 static int snd_sb_csp_unuse(struct snd_sb_csp * p);
103 static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels);
104 static int snd_sb_csp_stop(struct snd_sb_csp * p);
105 static int snd_sb_csp_pause(struct snd_sb_csp * p);
106 static int snd_sb_csp_restart(struct snd_sb_csp * p);
108 static int snd_sb_qsound_build(struct snd_sb_csp * p);
109 static void snd_sb_qsound_destroy(struct snd_sb_csp * p);
110 static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p);
112 static int init_proc_entry(struct snd_sb_csp * p, int device);
113 static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer);
116 * Detect CSP chip and create a new instance
118 int snd_sb_csp_new(struct snd_sb *chip, int device, struct snd_hwdep ** rhwdep)
120 struct snd_sb_csp *p;
122 struct snd_hwdep *hw;
127 if (csp_detect(chip, &version))
130 if ((err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw)) < 0)
133 if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
134 snd_device_free(chip->card, hw);
138 p->version = version;
141 p->ops.csp_use = snd_sb_csp_use;
142 p->ops.csp_unuse = snd_sb_csp_unuse;
143 p->ops.csp_autoload = snd_sb_csp_autoload;
144 p->ops.csp_start = snd_sb_csp_start;
145 p->ops.csp_stop = snd_sb_csp_stop;
146 p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;
148 mutex_init(&p->access_mutex);
149 sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f));
150 hw->iface = SNDRV_HWDEP_IFACE_SB16CSP;
151 hw->private_data = p;
152 hw->private_free = snd_sb_csp_free;
154 /* operators - only write/ioctl */
155 hw->ops.open = snd_sb_csp_open;
156 hw->ops.ioctl = snd_sb_csp_ioctl;
157 hw->ops.release = snd_sb_csp_release;
159 /* create a proc entry */
160 init_proc_entry(p, device);
167 * free_private for hwdep instance
169 static void snd_sb_csp_free(struct snd_hwdep *hwdep)
171 #ifndef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
174 struct snd_sb_csp *p = hwdep->private_data;
176 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
178 #ifndef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
179 for (i = 0; i < ARRAY_SIZE(p->csp_programs); ++i)
180 release_firmware(p->csp_programs[i]);
186 /* ------------------------------ */
189 * open the device exclusively
191 static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file)
193 struct snd_sb_csp *p = hw->private_data;
194 return (snd_sb_csp_use(p));
198 * ioctl for hwdep device:
200 static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg)
202 struct snd_sb_csp *p = hw->private_data;
203 struct snd_sb_csp_info info;
204 struct snd_sb_csp_start start_info;
207 snd_assert(p != NULL, return -EINVAL);
209 if (snd_sb_csp_check_version(p))
213 /* get information */
214 case SNDRV_SB_CSP_IOCTL_INFO:
215 *info.codec_name = *p->codec_name;
216 info.func_nr = p->func_nr;
217 info.acc_format = p->acc_format;
218 info.acc_channels = p->acc_channels;
219 info.acc_width = p->acc_width;
220 info.acc_rates = p->acc_rates;
221 info.csp_mode = p->mode;
222 info.run_channels = p->run_channels;
223 info.run_width = p->run_width;
224 info.version = p->version;
225 info.state = p->running;
226 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
232 /* load CSP microcode */
233 case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
234 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
235 -EBUSY : snd_sb_csp_riff_load(p, (struct snd_sb_csp_microcode __user *) arg));
237 case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
238 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
239 -EBUSY : snd_sb_csp_unload(p));
242 /* change CSP running state */
243 case SNDRV_SB_CSP_IOCTL_START:
244 if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info)))
247 err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
249 case SNDRV_SB_CSP_IOCTL_STOP:
250 err = snd_sb_csp_stop(p);
252 case SNDRV_SB_CSP_IOCTL_PAUSE:
253 err = snd_sb_csp_pause(p);
255 case SNDRV_SB_CSP_IOCTL_RESTART:
256 err = snd_sb_csp_restart(p);
269 static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file)
271 struct snd_sb_csp *p = hw->private_data;
272 return (snd_sb_csp_unuse(p));
275 /* ------------------------------ */
280 static int snd_sb_csp_use(struct snd_sb_csp * p)
282 mutex_lock(&p->access_mutex);
284 mutex_unlock(&p->access_mutex);
288 mutex_unlock(&p->access_mutex);
297 static int snd_sb_csp_unuse(struct snd_sb_csp * p)
299 mutex_lock(&p->access_mutex);
301 mutex_unlock(&p->access_mutex);
307 * load microcode via ioctl:
308 * code is user-space pointer
310 static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
311 struct snd_sb_csp_microcode __user * mcode)
313 struct snd_sb_csp_mc_header info;
315 unsigned char __user *data_ptr;
316 unsigned char __user *data_end;
317 unsigned short func_nr = 0;
319 struct riff_header file_h, item_h, code_h;
321 struct desc_header funcdesc_h;
326 if (copy_from_user(&info, mcode, sizeof(info)))
328 data_ptr = mcode->data;
330 if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
332 if ((file_h.name != RIFF_HEADER) ||
333 (le32_to_cpu(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
334 snd_printd("%s: Invalid RIFF header\n", __FUNCTION__);
337 data_ptr += sizeof(file_h);
338 data_end = data_ptr + le32_to_cpu(file_h.len);
340 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
342 if (item_type != CSP__HEADER) {
343 snd_printd("%s: Invalid RIFF file type\n", __FUNCTION__);
346 data_ptr += sizeof (item_type);
348 for (; data_ptr < data_end; data_ptr += le32_to_cpu(item_h.len)) {
349 if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
351 data_ptr += sizeof(item_h);
352 if (item_h.name != LIST_HEADER)
355 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
359 if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
361 func_nr = le16_to_cpu(funcdesc_h.func_nr);
364 if (func_nr != info.func_req)
365 break; /* not required function, try next */
366 data_ptr += sizeof(item_type);
368 /* destroy QSound mixer element */
369 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
370 snd_sb_qsound_destroy(p);
372 /* Clear all flags */
376 /* load microcode blocks */
378 if (data_ptr >= data_end)
380 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
383 /* init microcode blocks */
384 if (code_h.name != INIT_HEADER)
386 data_ptr += sizeof(code_h);
387 err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len),
388 SNDRV_SB_CSP_LOAD_INITBLOCK);
391 data_ptr += le32_to_cpu(code_h.len);
393 /* main microcode block */
394 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
397 if (code_h.name != MAIN_HEADER) {
398 snd_printd("%s: Missing 'main' microcode\n", __FUNCTION__);
401 data_ptr += sizeof(code_h);
402 err = snd_sb_csp_load_user(p, data_ptr,
403 le32_to_cpu(code_h.len), 0);
407 /* fill in codec header */
408 strlcpy(p->codec_name, info.codec_name, sizeof(p->codec_name));
409 p->func_nr = func_nr;
410 p->mode = le16_to_cpu(funcdesc_h.flags_play_rec);
411 switch (le16_to_cpu(funcdesc_h.VOC_type)) {
412 case 0x0001: /* QSound decoder */
413 if (le16_to_cpu(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
414 if (snd_sb_qsound_build(p) == 0)
415 /* set QSound flag and clear all other mode flags */
416 p->mode = SNDRV_SB_CSP_MODE_QSOUND;
420 case 0x0006: /* A Law codec */
421 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
423 case 0x0007: /* Mu Law codec */
424 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
426 case 0x0011: /* what Creative thinks is IMA ADPCM codec */
427 case 0x0200: /* Creative ADPCM codec */
428 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
430 case 201: /* Text 2 Speech decoder */
431 /* TODO: Text2Speech handling routines */
434 case 0x0202: /* Fast Speech 8 codec */
435 case 0x0203: /* Fast Speech 10 codec */
436 p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
438 default: /* other codecs are unsupported */
439 p->acc_format = p->acc_width = p->acc_rates = 0;
441 snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
443 le16_to_cpu(funcdesc_h.VOC_type));
446 p->acc_channels = le16_to_cpu(funcdesc_h.flags_stereo_mono);
447 p->acc_width = le16_to_cpu(funcdesc_h.flags_16bit_8bit);
448 p->acc_rates = le16_to_cpu(funcdesc_h.flags_rates);
450 /* Decouple CSP from IRQ and DMAREQ lines */
451 spin_lock_irqsave(&p->chip->reg_lock, flags);
452 set_mode_register(p->chip, 0xfc);
453 set_mode_register(p->chip, 0x00);
454 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
456 /* finished loading successfully */
457 p->running = SNDRV_SB_CSP_ST_LOADED; /* set LOADED flag */
461 snd_printd("%s: Function #%d not found\n", __FUNCTION__, info.func_req);
466 * unload CSP microcode
468 static int snd_sb_csp_unload(struct snd_sb_csp * p)
470 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
472 if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
475 /* clear supported formats */
477 p->acc_channels = p->acc_width = p->acc_rates = 0;
478 /* destroy QSound mixer element */
479 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
480 snd_sb_qsound_destroy(p);
482 /* clear all flags */
489 * send command sequence to DSP
491 static inline int command_seq(struct snd_sb *chip, const unsigned char *seq, int size)
494 for (i = 0; i < size; i++) {
495 if (!snd_sbdsp_command(chip, seq[i]))
502 * set CSP codec parameter
504 static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val)
506 unsigned char dsp_cmd[3];
508 dsp_cmd[0] = 0x05; /* CSP set codec parameter */
509 dsp_cmd[1] = val; /* Parameter value */
510 dsp_cmd[2] = par; /* Parameter */
511 command_seq(chip, dsp_cmd, 3);
512 snd_sbdsp_command(chip, 0x03); /* DSP read? */
513 if (snd_sbdsp_get_byte(chip) != par)
521 static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val)
523 unsigned char dsp_cmd[3];
525 dsp_cmd[0] = 0x0e; /* CSP set register */
526 dsp_cmd[1] = reg; /* CSP Register */
527 dsp_cmd[2] = val; /* value */
528 return command_seq(chip, dsp_cmd, 3);
533 * return < 0 -> error
535 static int read_register(struct snd_sb *chip, unsigned char reg)
537 unsigned char dsp_cmd[2];
539 dsp_cmd[0] = 0x0f; /* CSP read register */
540 dsp_cmd[1] = reg; /* CSP Register */
541 command_seq(chip, dsp_cmd, 2);
542 return snd_sbdsp_get_byte(chip); /* Read DSP value */
546 * set CSP mode register
548 static int set_mode_register(struct snd_sb *chip, unsigned char mode)
550 unsigned char dsp_cmd[2];
552 dsp_cmd[0] = 0x04; /* CSP set mode register */
553 dsp_cmd[1] = mode; /* mode */
554 return command_seq(chip, dsp_cmd, 2);
559 * return 0 if CSP exists.
561 static int csp_detect(struct snd_sb *chip, int *version)
563 unsigned char csp_test1, csp_test2;
565 int result = -ENODEV;
567 spin_lock_irqsave(&chip->reg_lock, flags);
569 set_codec_parameter(chip, 0x00, 0x00);
570 set_mode_register(chip, 0xfc); /* 0xfc = ?? */
572 csp_test1 = read_register(chip, 0x83);
573 set_register(chip, 0x83, ~csp_test1);
574 csp_test2 = read_register(chip, 0x83);
575 if (csp_test2 != (csp_test1 ^ 0xff))
578 set_register(chip, 0x83, csp_test1);
579 csp_test2 = read_register(chip, 0x83);
580 if (csp_test2 != csp_test1)
583 set_mode_register(chip, 0x00); /* 0x00 = ? */
585 *version = get_version(chip);
586 snd_sbdsp_reset(chip); /* reset DSP after getversion! */
587 if (*version >= 0x10 && *version <= 0x1f)
588 result = 0; /* valid version id */
591 spin_unlock_irqrestore(&chip->reg_lock, flags);
596 * get CSP version number
598 static int get_version(struct snd_sb *chip)
600 unsigned char dsp_cmd[2];
602 dsp_cmd[0] = 0x08; /* SB_DSP_!something! */
603 dsp_cmd[1] = 0x03; /* get chip version id? */
604 command_seq(chip, dsp_cmd, 2);
606 return (snd_sbdsp_get_byte(chip));
610 * check if the CSP version is valid
612 static int snd_sb_csp_check_version(struct snd_sb_csp * p)
614 if (p->version < 0x10 || p->version > 0x1f) {
615 snd_printd("%s: Invalid CSP version: 0x%x\n", __FUNCTION__, p->version);
622 * download microcode to CSP (microcode should have one "main" block).
624 static int snd_sb_csp_load(struct snd_sb_csp * p, const unsigned char *buf, int size, int load_flags)
631 spin_lock_irqsave(&p->chip->reg_lock, flags);
632 snd_sbdsp_command(p->chip, 0x01); /* CSP download command */
633 if (snd_sbdsp_get_byte(p->chip)) {
634 snd_printd("%s: Download command failed\n", __FUNCTION__);
637 /* Send CSP low byte (size - 1) */
638 snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
640 snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
641 /* send microcode sequence */
642 /* load from kernel space */
644 if (!snd_sbdsp_command(p->chip, *buf++))
647 if (snd_sbdsp_get_byte(p->chip))
650 if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
652 /* some codecs (FastSpeech) take some time to initialize */
654 snd_sbdsp_command(p->chip, 0x03);
655 status = snd_sbdsp_get_byte(p->chip);
656 if (status == 0x55 || ++i >= 10)
660 if (status != 0x55) {
661 snd_printd("%s: Microcode initialization failed\n", __FUNCTION__);
666 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
667 * Start CSP chip if no 16bit DMA channel is set - some kind
668 * of autorun or perhaps a bugfix?
670 spin_lock(&p->chip->mixer_lock);
671 status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
672 spin_unlock(&p->chip->mixer_lock);
673 if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
674 err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
675 set_codec_parameter(p->chip, 0xff, 0x00));
676 snd_sbdsp_reset(p->chip); /* really! */
679 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
680 set_mode_register(p->chip, 0x70); /* 70 = RUN */
686 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
690 static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags)
693 unsigned char *kbuf = kmalloc(size, GFP_KERNEL);
695 if (copy_from_user(kbuf, buf, size))
698 err = snd_sb_csp_load(p, kbuf, size, load_flags);
704 #ifdef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
705 #include "sb16_csp_codecs.h"
707 static const struct firmware snd_sb_csp_static_programs[] = {
708 { .data = mulaw_main, .size = sizeof mulaw_main },
709 { .data = alaw_main, .size = sizeof alaw_main },
710 { .data = ima_adpcm_init, .size = sizeof ima_adpcm_init },
711 { .data = ima_adpcm_playback, .size = sizeof ima_adpcm_playback },
712 { .data = ima_adpcm_capture, .size = sizeof ima_adpcm_capture },
716 static int snd_sb_csp_firmware_load(struct snd_sb_csp *p, int index, int flags)
718 static const char *const names[] = {
719 "sb16/mulaw_main.csp",
720 "sb16/alaw_main.csp",
721 "sb16/ima_adpcm_init.csp",
722 "sb16/ima_adpcm_playback.csp",
723 "sb16/ima_adpcm_capture.csp",
725 const struct firmware *program;
727 BUILD_BUG_ON(ARRAY_SIZE(names) != CSP_PROGRAM_COUNT);
728 program = p->csp_programs[index];
730 #ifdef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
731 program = &snd_sb_csp_static_programs[index];
733 int err = request_firmware(&program, names[index],
738 p->csp_programs[index] = program;
740 return snd_sb_csp_load(p, program->data, program->size, flags);
744 * autoload hardware codec if necessary
745 * return 0 if CSP is loaded and ready to run (p->running != 0)
747 static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode)
752 /* if CSP is running or manually loaded then exit */
753 if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED))
756 /* autoload microcode only if requested hardware codec is not already loaded */
757 if (((1 << pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
758 p->running = SNDRV_SB_CSP_ST_AUTO;
761 case SNDRV_PCM_FORMAT_MU_LAW:
762 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_MULAW, 0);
763 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
764 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
766 case SNDRV_PCM_FORMAT_A_LAW:
767 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ALAW, 0);
768 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
769 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
771 case SNDRV_PCM_FORMAT_IMA_ADPCM:
772 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ADPCM_INIT,
773 SNDRV_SB_CSP_LOAD_INITBLOCK);
776 if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
777 err = snd_sb_csp_firmware_load
778 (p, CSP_PROGRAM_ADPCM_PLAYBACK, 0);
779 p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
781 err = snd_sb_csp_firmware_load
782 (p, CSP_PROGRAM_ADPCM_CAPTURE, 0);
783 p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
785 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
788 /* Decouple CSP from IRQ and DMAREQ lines */
789 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
790 spin_lock_irqsave(&p->chip->reg_lock, flags);
791 set_mode_register(p->chip, 0xfc);
792 set_mode_register(p->chip, 0x00);
793 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
794 p->running = 0; /* clear autoloaded flag */
800 p->acc_channels = p->acc_width = p->acc_rates = 0;
802 p->running = 0; /* clear autoloaded flag */
806 p->running = SNDRV_SB_CSP_ST_AUTO; /* set autoloaded flag */
807 p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT; /* only 16 bit data */
808 p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
809 p->acc_rates = SNDRV_SB_CSP_RATE_ALL; /* HW codecs accept all rates */
813 return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
819 static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels)
821 unsigned char s_type; /* sample type */
822 unsigned char mixL, mixR;
826 if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
827 snd_printd("%s: Microcode not loaded\n", __FUNCTION__);
830 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
831 snd_printd("%s: CSP already running\n", __FUNCTION__);
834 if (!(sample_width & p->acc_width)) {
835 snd_printd("%s: Unsupported PCM sample width\n", __FUNCTION__);
838 if (!(channels & p->acc_channels)) {
839 snd_printd("%s: Invalid number of channels\n", __FUNCTION__);
843 /* Mute PCM volume */
844 spin_lock_irqsave(&p->chip->mixer_lock, flags);
845 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
846 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
847 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
848 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
850 spin_lock(&p->chip->reg_lock);
851 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
852 set_mode_register(p->chip, 0x70); /* 70 = RUN */
855 if (channels == SNDRV_SB_CSP_MONO)
856 s_type = 0x11; /* 000n 000n (n = 1 if mono) */
857 if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
858 s_type |= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
860 if (set_codec_parameter(p->chip, 0x81, s_type)) {
861 snd_printd("%s: Set sample type command failed\n", __FUNCTION__);
864 if (set_codec_parameter(p->chip, 0x80, 0x00)) {
865 snd_printd("%s: Codec start command failed\n", __FUNCTION__);
868 p->run_width = sample_width;
869 p->run_channels = channels;
871 p->running |= SNDRV_SB_CSP_ST_RUNNING;
873 if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
874 set_codec_parameter(p->chip, 0xe0, 0x01);
875 /* enable QSound decoder */
876 set_codec_parameter(p->chip, 0x00, 0xff);
877 set_codec_parameter(p->chip, 0x01, 0xff);
878 p->running |= SNDRV_SB_CSP_ST_QSOUND;
879 /* set QSound startup value */
880 snd_sb_csp_qsound_transfer(p);
885 spin_unlock(&p->chip->reg_lock);
887 /* restore PCM volume */
888 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
889 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
890 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
898 static int snd_sb_csp_stop(struct snd_sb_csp * p)
901 unsigned char mixL, mixR;
904 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
907 /* Mute PCM volume */
908 spin_lock_irqsave(&p->chip->mixer_lock, flags);
909 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
910 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
911 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
912 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
914 spin_lock(&p->chip->reg_lock);
915 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
916 set_codec_parameter(p->chip, 0xe0, 0x01);
917 /* disable QSound decoder */
918 set_codec_parameter(p->chip, 0x00, 0x00);
919 set_codec_parameter(p->chip, 0x01, 0x00);
921 p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
923 result = set_mode_register(p->chip, 0xc0); /* c0 = STOP */
924 spin_unlock(&p->chip->reg_lock);
926 /* restore PCM volume */
927 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
928 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
929 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
932 p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
937 * pause CSP codec and hold DMA transfer
939 static int snd_sb_csp_pause(struct snd_sb_csp * p)
944 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
947 spin_lock_irqsave(&p->chip->reg_lock, flags);
948 result = set_codec_parameter(p->chip, 0x80, 0xff);
949 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
951 p->running |= SNDRV_SB_CSP_ST_PAUSED;
957 * restart CSP codec and resume DMA transfer
959 static int snd_sb_csp_restart(struct snd_sb_csp * p)
964 if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
967 spin_lock_irqsave(&p->chip->reg_lock, flags);
968 result = set_codec_parameter(p->chip, 0x80, 0x00);
969 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
971 p->running &= ~SNDRV_SB_CSP_ST_PAUSED;
976 /* ------------------------------ */
979 * QSound mixer control for PCM
982 static int snd_sb_qsound_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
984 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
986 uinfo->value.integer.min = 0;
987 uinfo->value.integer.max = 1;
991 static int snd_sb_qsound_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
993 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
995 ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
999 static int snd_sb_qsound_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1001 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
1002 unsigned long flags;
1006 nval = ucontrol->value.integer.value[0] & 0x01;
1007 spin_lock_irqsave(&p->q_lock, flags);
1008 change = p->q_enabled != nval;
1009 p->q_enabled = nval;
1010 spin_unlock_irqrestore(&p->q_lock, flags);
1014 static int snd_sb_qsound_space_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1016 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1018 uinfo->value.integer.min = 0;
1019 uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1023 static int snd_sb_qsound_space_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1025 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
1026 unsigned long flags;
1028 spin_lock_irqsave(&p->q_lock, flags);
1029 ucontrol->value.integer.value[0] = p->qpos_left;
1030 ucontrol->value.integer.value[1] = p->qpos_right;
1031 spin_unlock_irqrestore(&p->q_lock, flags);
1035 static int snd_sb_qsound_space_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1037 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
1038 unsigned long flags;
1040 unsigned char nval1, nval2;
1042 nval1 = ucontrol->value.integer.value[0];
1043 if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1044 nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1045 nval2 = ucontrol->value.integer.value[1];
1046 if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1047 nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1048 spin_lock_irqsave(&p->q_lock, flags);
1049 change = p->qpos_left != nval1 || p->qpos_right != nval2;
1050 p->qpos_left = nval1;
1051 p->qpos_right = nval2;
1052 p->qpos_changed = change;
1053 spin_unlock_irqrestore(&p->q_lock, flags);
1057 static struct snd_kcontrol_new snd_sb_qsound_switch = {
1058 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1059 .name = "3D Control - Switch",
1060 .info = snd_sb_qsound_switch_info,
1061 .get = snd_sb_qsound_switch_get,
1062 .put = snd_sb_qsound_switch_put
1065 static struct snd_kcontrol_new snd_sb_qsound_space = {
1066 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1067 .name = "3D Control - Space",
1068 .info = snd_sb_qsound_space_info,
1069 .get = snd_sb_qsound_space_get,
1070 .put = snd_sb_qsound_space_put
1073 static int snd_sb_qsound_build(struct snd_sb_csp * p)
1075 struct snd_card *card;
1078 snd_assert(p != NULL, return -EINVAL);
1080 card = p->chip->card;
1081 p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
1082 p->qpos_changed = 0;
1084 spin_lock_init(&p->q_lock);
1086 if ((err = snd_ctl_add(card, p->qsound_switch = snd_ctl_new1(&snd_sb_qsound_switch, p))) < 0)
1088 if ((err = snd_ctl_add(card, p->qsound_space = snd_ctl_new1(&snd_sb_qsound_space, p))) < 0)
1094 snd_sb_qsound_destroy(p);
1098 static void snd_sb_qsound_destroy(struct snd_sb_csp * p)
1100 struct snd_card *card;
1101 unsigned long flags;
1103 snd_assert(p != NULL, return);
1105 card = p->chip->card;
1107 down_write(&card->controls_rwsem);
1108 if (p->qsound_switch)
1109 snd_ctl_remove(card, p->qsound_switch);
1110 if (p->qsound_space)
1111 snd_ctl_remove(card, p->qsound_space);
1112 up_write(&card->controls_rwsem);
1114 /* cancel pending transfer of QSound parameters */
1115 spin_lock_irqsave (&p->q_lock, flags);
1116 p->qpos_changed = 0;
1117 spin_unlock_irqrestore (&p->q_lock, flags);
1121 * Transfer qsound parameters to CSP,
1122 * function should be called from interrupt routine
1124 static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p)
1128 spin_lock(&p->q_lock);
1129 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1130 set_codec_parameter(p->chip, 0xe0, 0x01);
1132 set_codec_parameter(p->chip, 0x00, p->qpos_left);
1133 set_codec_parameter(p->chip, 0x02, 0x00);
1135 set_codec_parameter(p->chip, 0x00, p->qpos_right);
1136 set_codec_parameter(p->chip, 0x03, 0x00);
1139 p->qpos_changed = 0;
1140 spin_unlock(&p->q_lock);
1144 /* ------------------------------ */
1149 static int init_proc_entry(struct snd_sb_csp * p, int device)
1152 struct snd_info_entry *entry;
1153 sprintf(name, "cspD%d", device);
1154 if (! snd_card_proc_new(p->chip->card, name, &entry))
1155 snd_info_set_text_ops(entry, p, info_read);
1159 static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
1161 struct snd_sb_csp *p = entry->private_data;
1163 snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
1164 snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
1165 ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
1166 ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
1167 ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
1168 if (p->running & SNDRV_SB_CSP_ST_LOADED) {
1169 snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
1170 snd_iprintf(buffer, "Sample rates: ");
1171 if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
1172 snd_iprintf(buffer, "All\n");
1174 snd_iprintf(buffer, "%s%s%s%s\n",
1175 ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
1176 ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
1177 ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
1178 ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
1180 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
1181 snd_iprintf(buffer, "QSound decoder %sabled\n",
1182 p->q_enabled ? "en" : "dis");
1184 snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
1186 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
1187 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
1188 ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
1189 ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
1190 ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
1191 ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
1194 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
1195 snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1197 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
1198 snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
1199 ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
1200 ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
1202 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1203 snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
1204 p->qpos_left, p->qpos_right);
1210 EXPORT_SYMBOL(snd_sb_csp_new);
1216 static int __init alsa_sb_csp_init(void)
1221 static void __exit alsa_sb_csp_exit(void)
1225 module_init(alsa_sb_csp_init)
1226 module_exit(alsa_sb_csp_exit)