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 <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>
36 MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
37 MODULE_DESCRIPTION("ALSA driver for SB16 Creative Signal Processor");
38 MODULE_LICENSE("GPL");
41 #ifdef SNDRV_LITTLE_ENDIAN
42 #define CSP_HDR_VALUE(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
44 #define CSP_HDR_VALUE(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
47 #define RIFF_HEADER CSP_HDR_VALUE('R', 'I', 'F', 'F')
48 #define CSP__HEADER CSP_HDR_VALUE('C', 'S', 'P', ' ')
49 #define LIST_HEADER CSP_HDR_VALUE('L', 'I', 'S', 'T')
50 #define FUNC_HEADER CSP_HDR_VALUE('f', 'u', 'n', 'c')
51 #define CODE_HEADER CSP_HDR_VALUE('c', 'o', 'd', 'e')
52 #define INIT_HEADER CSP_HDR_VALUE('i', 'n', 'i', 't')
53 #define MAIN_HEADER CSP_HDR_VALUE('m', 'a', 'i', 'n')
64 struct riff_header info;
67 __le16 flags_play_rec;
68 __le16 flags_16bit_8bit;
69 __le16 flags_stereo_mono;
76 static void snd_sb_csp_free(struct snd_hwdep *hw);
77 static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file);
78 static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg);
79 static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file);
81 static int csp_detect(struct snd_sb *chip, int *version);
82 static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val);
83 static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val);
84 static int read_register(struct snd_sb *chip, unsigned char reg);
85 static int set_mode_register(struct snd_sb *chip, unsigned char mode);
86 static int get_version(struct snd_sb *chip);
88 static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
89 struct snd_sb_csp_microcode __user * code);
90 static int snd_sb_csp_unload(struct snd_sb_csp * p);
91 static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags);
92 static int snd_sb_csp_autoload(struct snd_sb_csp * p, snd_pcm_format_t pcm_sfmt, int play_rec_mode);
93 static int snd_sb_csp_check_version(struct snd_sb_csp * p);
95 static int snd_sb_csp_use(struct snd_sb_csp * p);
96 static int snd_sb_csp_unuse(struct snd_sb_csp * p);
97 static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels);
98 static int snd_sb_csp_stop(struct snd_sb_csp * p);
99 static int snd_sb_csp_pause(struct snd_sb_csp * p);
100 static int snd_sb_csp_restart(struct snd_sb_csp * p);
102 static int snd_sb_qsound_build(struct snd_sb_csp * p);
103 static void snd_sb_qsound_destroy(struct snd_sb_csp * p);
104 static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p);
106 static int init_proc_entry(struct snd_sb_csp * p, int device);
107 static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer);
110 * Detect CSP chip and create a new instance
112 int snd_sb_csp_new(struct snd_sb *chip, int device, struct snd_hwdep ** rhwdep)
114 struct snd_sb_csp *p;
115 int uninitialized_var(version);
117 struct snd_hwdep *hw;
122 if (csp_detect(chip, &version))
125 if ((err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw)) < 0)
128 if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
129 snd_device_free(chip->card, hw);
133 p->version = version;
136 p->ops.csp_use = snd_sb_csp_use;
137 p->ops.csp_unuse = snd_sb_csp_unuse;
138 p->ops.csp_autoload = snd_sb_csp_autoload;
139 p->ops.csp_start = snd_sb_csp_start;
140 p->ops.csp_stop = snd_sb_csp_stop;
141 p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;
143 mutex_init(&p->access_mutex);
144 sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f));
145 hw->iface = SNDRV_HWDEP_IFACE_SB16CSP;
146 hw->private_data = p;
147 hw->private_free = snd_sb_csp_free;
149 /* operators - only write/ioctl */
150 hw->ops.open = snd_sb_csp_open;
151 hw->ops.ioctl = snd_sb_csp_ioctl;
152 hw->ops.release = snd_sb_csp_release;
154 /* create a proc entry */
155 init_proc_entry(p, device);
162 * free_private for hwdep instance
164 static void snd_sb_csp_free(struct snd_hwdep *hwdep)
167 struct snd_sb_csp *p = hwdep->private_data;
169 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
171 for (i = 0; i < ARRAY_SIZE(p->csp_programs); ++i)
172 release_firmware(p->csp_programs[i]);
177 /* ------------------------------ */
180 * open the device exclusively
182 static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file)
184 struct snd_sb_csp *p = hw->private_data;
185 return (snd_sb_csp_use(p));
189 * ioctl for hwdep device:
191 static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg)
193 struct snd_sb_csp *p = hw->private_data;
194 struct snd_sb_csp_info info;
195 struct snd_sb_csp_start start_info;
201 if (snd_sb_csp_check_version(p))
205 /* get information */
206 case SNDRV_SB_CSP_IOCTL_INFO:
207 memset(&info, 0, sizeof(info));
208 *info.codec_name = *p->codec_name;
209 info.func_nr = p->func_nr;
210 info.acc_format = p->acc_format;
211 info.acc_channels = p->acc_channels;
212 info.acc_width = p->acc_width;
213 info.acc_rates = p->acc_rates;
214 info.csp_mode = p->mode;
215 info.run_channels = p->run_channels;
216 info.run_width = p->run_width;
217 info.version = p->version;
218 info.state = p->running;
219 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
225 /* load CSP microcode */
226 case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
227 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
228 -EBUSY : snd_sb_csp_riff_load(p, (struct snd_sb_csp_microcode __user *) arg));
230 case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
231 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
232 -EBUSY : snd_sb_csp_unload(p));
235 /* change CSP running state */
236 case SNDRV_SB_CSP_IOCTL_START:
237 if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info)))
240 err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
242 case SNDRV_SB_CSP_IOCTL_STOP:
243 err = snd_sb_csp_stop(p);
245 case SNDRV_SB_CSP_IOCTL_PAUSE:
246 err = snd_sb_csp_pause(p);
248 case SNDRV_SB_CSP_IOCTL_RESTART:
249 err = snd_sb_csp_restart(p);
262 static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file)
264 struct snd_sb_csp *p = hw->private_data;
265 return (snd_sb_csp_unuse(p));
268 /* ------------------------------ */
273 static int snd_sb_csp_use(struct snd_sb_csp * p)
275 mutex_lock(&p->access_mutex);
277 mutex_unlock(&p->access_mutex);
281 mutex_unlock(&p->access_mutex);
290 static int snd_sb_csp_unuse(struct snd_sb_csp * p)
292 mutex_lock(&p->access_mutex);
294 mutex_unlock(&p->access_mutex);
300 * load microcode via ioctl:
301 * code is user-space pointer
303 static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
304 struct snd_sb_csp_microcode __user * mcode)
306 struct snd_sb_csp_mc_header info;
308 unsigned char __user *data_ptr;
309 unsigned char __user *data_end;
310 unsigned short func_nr = 0;
312 struct riff_header file_h, item_h, code_h;
314 struct desc_header funcdesc_h;
319 if (copy_from_user(&info, mcode, sizeof(info)))
321 data_ptr = mcode->data;
323 if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
325 if ((le32_to_cpu(file_h.name) != RIFF_HEADER) ||
326 (le32_to_cpu(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
327 snd_printd("%s: Invalid RIFF header\n", __func__);
330 data_ptr += sizeof(file_h);
331 data_end = data_ptr + le32_to_cpu(file_h.len);
333 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
335 if (le32_to_cpu(item_type) != CSP__HEADER) {
336 snd_printd("%s: Invalid RIFF file type\n", __func__);
339 data_ptr += sizeof (item_type);
341 for (; data_ptr < data_end; data_ptr += le32_to_cpu(item_h.len)) {
342 if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
344 data_ptr += sizeof(item_h);
345 if (le32_to_cpu(item_h.name) != LIST_HEADER)
348 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
350 switch (le32_to_cpu(item_type)) {
352 if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
354 func_nr = le16_to_cpu(funcdesc_h.func_nr);
357 if (func_nr != info.func_req)
358 break; /* not required function, try next */
359 data_ptr += sizeof(item_type);
361 /* destroy QSound mixer element */
362 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
363 snd_sb_qsound_destroy(p);
365 /* Clear all flags */
369 /* load microcode blocks */
371 if (data_ptr >= data_end)
373 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
376 /* init microcode blocks */
377 if (le32_to_cpu(code_h.name) != INIT_HEADER)
379 data_ptr += sizeof(code_h);
380 err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len),
381 SNDRV_SB_CSP_LOAD_INITBLOCK);
384 data_ptr += le32_to_cpu(code_h.len);
386 /* main microcode block */
387 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
390 if (le32_to_cpu(code_h.name) != MAIN_HEADER) {
391 snd_printd("%s: Missing 'main' microcode\n", __func__);
394 data_ptr += sizeof(code_h);
395 err = snd_sb_csp_load_user(p, data_ptr,
396 le32_to_cpu(code_h.len), 0);
400 /* fill in codec header */
401 strlcpy(p->codec_name, info.codec_name, sizeof(p->codec_name));
402 p->func_nr = func_nr;
403 p->mode = le16_to_cpu(funcdesc_h.flags_play_rec);
404 switch (le16_to_cpu(funcdesc_h.VOC_type)) {
405 case 0x0001: /* QSound decoder */
406 if (le16_to_cpu(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
407 if (snd_sb_qsound_build(p) == 0)
408 /* set QSound flag and clear all other mode flags */
409 p->mode = SNDRV_SB_CSP_MODE_QSOUND;
413 case 0x0006: /* A Law codec */
414 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
416 case 0x0007: /* Mu Law codec */
417 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
419 case 0x0011: /* what Creative thinks is IMA ADPCM codec */
420 case 0x0200: /* Creative ADPCM codec */
421 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
423 case 201: /* Text 2 Speech decoder */
424 /* TODO: Text2Speech handling routines */
427 case 0x0202: /* Fast Speech 8 codec */
428 case 0x0203: /* Fast Speech 10 codec */
429 p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
431 default: /* other codecs are unsupported */
432 p->acc_format = p->acc_width = p->acc_rates = 0;
434 snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
436 le16_to_cpu(funcdesc_h.VOC_type));
439 p->acc_channels = le16_to_cpu(funcdesc_h.flags_stereo_mono);
440 p->acc_width = le16_to_cpu(funcdesc_h.flags_16bit_8bit);
441 p->acc_rates = le16_to_cpu(funcdesc_h.flags_rates);
443 /* Decouple CSP from IRQ and DMAREQ lines */
444 spin_lock_irqsave(&p->chip->reg_lock, flags);
445 set_mode_register(p->chip, 0xfc);
446 set_mode_register(p->chip, 0x00);
447 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
449 /* finished loading successfully */
450 p->running = SNDRV_SB_CSP_ST_LOADED; /* set LOADED flag */
454 snd_printd("%s: Function #%d not found\n", __func__, info.func_req);
459 * unload CSP microcode
461 static int snd_sb_csp_unload(struct snd_sb_csp * p)
463 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
465 if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
468 /* clear supported formats */
470 p->acc_channels = p->acc_width = p->acc_rates = 0;
471 /* destroy QSound mixer element */
472 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
473 snd_sb_qsound_destroy(p);
475 /* clear all flags */
482 * send command sequence to DSP
484 static inline int command_seq(struct snd_sb *chip, const unsigned char *seq, int size)
487 for (i = 0; i < size; i++) {
488 if (!snd_sbdsp_command(chip, seq[i]))
495 * set CSP codec parameter
497 static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val)
499 unsigned char dsp_cmd[3];
501 dsp_cmd[0] = 0x05; /* CSP set codec parameter */
502 dsp_cmd[1] = val; /* Parameter value */
503 dsp_cmd[2] = par; /* Parameter */
504 command_seq(chip, dsp_cmd, 3);
505 snd_sbdsp_command(chip, 0x03); /* DSP read? */
506 if (snd_sbdsp_get_byte(chip) != par)
514 static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val)
516 unsigned char dsp_cmd[3];
518 dsp_cmd[0] = 0x0e; /* CSP set register */
519 dsp_cmd[1] = reg; /* CSP Register */
520 dsp_cmd[2] = val; /* value */
521 return command_seq(chip, dsp_cmd, 3);
526 * return < 0 -> error
528 static int read_register(struct snd_sb *chip, unsigned char reg)
530 unsigned char dsp_cmd[2];
532 dsp_cmd[0] = 0x0f; /* CSP read register */
533 dsp_cmd[1] = reg; /* CSP Register */
534 command_seq(chip, dsp_cmd, 2);
535 return snd_sbdsp_get_byte(chip); /* Read DSP value */
539 * set CSP mode register
541 static int set_mode_register(struct snd_sb *chip, unsigned char mode)
543 unsigned char dsp_cmd[2];
545 dsp_cmd[0] = 0x04; /* CSP set mode register */
546 dsp_cmd[1] = mode; /* mode */
547 return command_seq(chip, dsp_cmd, 2);
552 * return 0 if CSP exists.
554 static int csp_detect(struct snd_sb *chip, int *version)
556 unsigned char csp_test1, csp_test2;
558 int result = -ENODEV;
560 spin_lock_irqsave(&chip->reg_lock, flags);
562 set_codec_parameter(chip, 0x00, 0x00);
563 set_mode_register(chip, 0xfc); /* 0xfc = ?? */
565 csp_test1 = read_register(chip, 0x83);
566 set_register(chip, 0x83, ~csp_test1);
567 csp_test2 = read_register(chip, 0x83);
568 if (csp_test2 != (csp_test1 ^ 0xff))
571 set_register(chip, 0x83, csp_test1);
572 csp_test2 = read_register(chip, 0x83);
573 if (csp_test2 != csp_test1)
576 set_mode_register(chip, 0x00); /* 0x00 = ? */
578 *version = get_version(chip);
579 snd_sbdsp_reset(chip); /* reset DSP after getversion! */
580 if (*version >= 0x10 && *version <= 0x1f)
581 result = 0; /* valid version id */
584 spin_unlock_irqrestore(&chip->reg_lock, flags);
589 * get CSP version number
591 static int get_version(struct snd_sb *chip)
593 unsigned char dsp_cmd[2];
595 dsp_cmd[0] = 0x08; /* SB_DSP_!something! */
596 dsp_cmd[1] = 0x03; /* get chip version id? */
597 command_seq(chip, dsp_cmd, 2);
599 return (snd_sbdsp_get_byte(chip));
603 * check if the CSP version is valid
605 static int snd_sb_csp_check_version(struct snd_sb_csp * p)
607 if (p->version < 0x10 || p->version > 0x1f) {
608 snd_printd("%s: Invalid CSP version: 0x%x\n", __func__, p->version);
615 * download microcode to CSP (microcode should have one "main" block).
617 static int snd_sb_csp_load(struct snd_sb_csp * p, const unsigned char *buf, int size, int load_flags)
624 spin_lock_irqsave(&p->chip->reg_lock, flags);
625 snd_sbdsp_command(p->chip, 0x01); /* CSP download command */
626 if (snd_sbdsp_get_byte(p->chip)) {
627 snd_printd("%s: Download command failed\n", __func__);
630 /* Send CSP low byte (size - 1) */
631 snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
633 snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
634 /* send microcode sequence */
635 /* load from kernel space */
637 if (!snd_sbdsp_command(p->chip, *buf++))
640 if (snd_sbdsp_get_byte(p->chip))
643 if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
645 /* some codecs (FastSpeech) take some time to initialize */
647 snd_sbdsp_command(p->chip, 0x03);
648 status = snd_sbdsp_get_byte(p->chip);
649 if (status == 0x55 || ++i >= 10)
653 if (status != 0x55) {
654 snd_printd("%s: Microcode initialization failed\n", __func__);
659 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
660 * Start CSP chip if no 16bit DMA channel is set - some kind
661 * of autorun or perhaps a bugfix?
663 spin_lock(&p->chip->mixer_lock);
664 status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
665 spin_unlock(&p->chip->mixer_lock);
666 if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
667 err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
668 set_codec_parameter(p->chip, 0xff, 0x00));
669 snd_sbdsp_reset(p->chip); /* really! */
672 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
673 set_mode_register(p->chip, 0x70); /* 70 = RUN */
679 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
683 static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags)
688 kbuf = memdup_user(buf, size);
690 return PTR_ERR(kbuf);
692 err = snd_sb_csp_load(p, kbuf, size, load_flags);
698 static int snd_sb_csp_firmware_load(struct snd_sb_csp *p, int index, int flags)
700 static const char *const names[] = {
707 const struct firmware *program;
709 BUILD_BUG_ON(ARRAY_SIZE(names) != CSP_PROGRAM_COUNT);
710 program = p->csp_programs[index];
712 int err = reject_firmware(&program, names[index],
716 p->csp_programs[index] = program;
718 return snd_sb_csp_load(p, program->data, program->size, flags);
722 * autoload hardware codec if necessary
723 * return 0 if CSP is loaded and ready to run (p->running != 0)
725 static int snd_sb_csp_autoload(struct snd_sb_csp * p, snd_pcm_format_t pcm_sfmt, int play_rec_mode)
730 /* if CSP is running or manually loaded then exit */
731 if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED))
734 /* autoload microcode only if requested hardware codec is not already loaded */
735 if (((1U << (__force int)pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
736 p->running = SNDRV_SB_CSP_ST_AUTO;
739 case SNDRV_PCM_FORMAT_MU_LAW:
740 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_MULAW, 0);
741 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
742 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
744 case SNDRV_PCM_FORMAT_A_LAW:
745 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ALAW, 0);
746 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
747 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
749 case SNDRV_PCM_FORMAT_IMA_ADPCM:
750 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ADPCM_INIT,
751 SNDRV_SB_CSP_LOAD_INITBLOCK);
754 if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
755 err = snd_sb_csp_firmware_load
756 (p, CSP_PROGRAM_ADPCM_PLAYBACK, 0);
757 p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
759 err = snd_sb_csp_firmware_load
760 (p, CSP_PROGRAM_ADPCM_CAPTURE, 0);
761 p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
763 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
766 /* Decouple CSP from IRQ and DMAREQ lines */
767 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
768 spin_lock_irqsave(&p->chip->reg_lock, flags);
769 set_mode_register(p->chip, 0xfc);
770 set_mode_register(p->chip, 0x00);
771 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
772 p->running = 0; /* clear autoloaded flag */
778 p->acc_channels = p->acc_width = p->acc_rates = 0;
780 p->running = 0; /* clear autoloaded flag */
784 p->running = SNDRV_SB_CSP_ST_AUTO; /* set autoloaded flag */
785 p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT; /* only 16 bit data */
786 p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
787 p->acc_rates = SNDRV_SB_CSP_RATE_ALL; /* HW codecs accept all rates */
791 return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
797 static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels)
799 unsigned char s_type; /* sample type */
800 unsigned char mixL, mixR;
804 if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
805 snd_printd("%s: Microcode not loaded\n", __func__);
808 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
809 snd_printd("%s: CSP already running\n", __func__);
812 if (!(sample_width & p->acc_width)) {
813 snd_printd("%s: Unsupported PCM sample width\n", __func__);
816 if (!(channels & p->acc_channels)) {
817 snd_printd("%s: Invalid number of channels\n", __func__);
821 /* Mute PCM volume */
822 spin_lock_irqsave(&p->chip->mixer_lock, flags);
823 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
824 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
825 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
826 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
827 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
829 spin_lock(&p->chip->reg_lock);
830 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
831 set_mode_register(p->chip, 0x70); /* 70 = RUN */
834 if (channels == SNDRV_SB_CSP_MONO)
835 s_type = 0x11; /* 000n 000n (n = 1 if mono) */
836 if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
837 s_type |= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
839 if (set_codec_parameter(p->chip, 0x81, s_type)) {
840 snd_printd("%s: Set sample type command failed\n", __func__);
843 if (set_codec_parameter(p->chip, 0x80, 0x00)) {
844 snd_printd("%s: Codec start command failed\n", __func__);
847 p->run_width = sample_width;
848 p->run_channels = channels;
850 p->running |= SNDRV_SB_CSP_ST_RUNNING;
852 if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
853 set_codec_parameter(p->chip, 0xe0, 0x01);
854 /* enable QSound decoder */
855 set_codec_parameter(p->chip, 0x00, 0xff);
856 set_codec_parameter(p->chip, 0x01, 0xff);
857 p->running |= SNDRV_SB_CSP_ST_QSOUND;
858 /* set QSound startup value */
859 snd_sb_csp_qsound_transfer(p);
864 spin_unlock(&p->chip->reg_lock);
866 /* restore PCM volume */
867 spin_lock_irqsave(&p->chip->mixer_lock, flags);
868 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
869 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
870 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
878 static int snd_sb_csp_stop(struct snd_sb_csp * p)
881 unsigned char mixL, mixR;
884 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
887 /* Mute PCM volume */
888 spin_lock_irqsave(&p->chip->mixer_lock, flags);
889 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
890 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
891 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
892 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
893 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
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);
902 p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
904 result = set_mode_register(p->chip, 0xc0); /* c0 = STOP */
905 spin_unlock(&p->chip->reg_lock);
907 /* restore PCM volume */
908 spin_lock_irqsave(&p->chip->mixer_lock, flags);
909 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
910 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
911 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
914 p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
919 * pause CSP codec and hold DMA transfer
921 static int snd_sb_csp_pause(struct snd_sb_csp * p)
926 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
929 spin_lock_irqsave(&p->chip->reg_lock, flags);
930 result = set_codec_parameter(p->chip, 0x80, 0xff);
931 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
933 p->running |= SNDRV_SB_CSP_ST_PAUSED;
939 * restart CSP codec and resume DMA transfer
941 static int snd_sb_csp_restart(struct snd_sb_csp * p)
946 if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
949 spin_lock_irqsave(&p->chip->reg_lock, flags);
950 result = set_codec_parameter(p->chip, 0x80, 0x00);
951 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
953 p->running &= ~SNDRV_SB_CSP_ST_PAUSED;
958 /* ------------------------------ */
961 * QSound mixer control for PCM
964 #define snd_sb_qsound_switch_info snd_ctl_boolean_mono_info
966 static int snd_sb_qsound_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
968 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
970 ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
974 static int snd_sb_qsound_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
976 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
981 nval = ucontrol->value.integer.value[0] & 0x01;
982 spin_lock_irqsave(&p->q_lock, flags);
983 change = p->q_enabled != nval;
985 spin_unlock_irqrestore(&p->q_lock, flags);
989 static int snd_sb_qsound_space_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
991 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
993 uinfo->value.integer.min = 0;
994 uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
998 static int snd_sb_qsound_space_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1000 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
1001 unsigned long flags;
1003 spin_lock_irqsave(&p->q_lock, flags);
1004 ucontrol->value.integer.value[0] = p->qpos_left;
1005 ucontrol->value.integer.value[1] = p->qpos_right;
1006 spin_unlock_irqrestore(&p->q_lock, flags);
1010 static int snd_sb_qsound_space_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1012 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
1013 unsigned long flags;
1015 unsigned char nval1, nval2;
1017 nval1 = ucontrol->value.integer.value[0];
1018 if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1019 nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1020 nval2 = ucontrol->value.integer.value[1];
1021 if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1022 nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1023 spin_lock_irqsave(&p->q_lock, flags);
1024 change = p->qpos_left != nval1 || p->qpos_right != nval2;
1025 p->qpos_left = nval1;
1026 p->qpos_right = nval2;
1027 p->qpos_changed = change;
1028 spin_unlock_irqrestore(&p->q_lock, flags);
1032 static const struct snd_kcontrol_new snd_sb_qsound_switch = {
1033 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1034 .name = "3D Control - Switch",
1035 .info = snd_sb_qsound_switch_info,
1036 .get = snd_sb_qsound_switch_get,
1037 .put = snd_sb_qsound_switch_put
1040 static const struct snd_kcontrol_new snd_sb_qsound_space = {
1041 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1042 .name = "3D Control - Space",
1043 .info = snd_sb_qsound_space_info,
1044 .get = snd_sb_qsound_space_get,
1045 .put = snd_sb_qsound_space_put
1048 static int snd_sb_qsound_build(struct snd_sb_csp * p)
1050 struct snd_card *card;
1056 card = p->chip->card;
1057 p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
1058 p->qpos_changed = 0;
1060 spin_lock_init(&p->q_lock);
1062 if ((err = snd_ctl_add(card, p->qsound_switch = snd_ctl_new1(&snd_sb_qsound_switch, p))) < 0) {
1063 p->qsound_switch = NULL;
1066 if ((err = snd_ctl_add(card, p->qsound_space = snd_ctl_new1(&snd_sb_qsound_space, p))) < 0) {
1067 p->qsound_space = NULL;
1074 snd_sb_qsound_destroy(p);
1078 static void snd_sb_qsound_destroy(struct snd_sb_csp * p)
1080 struct snd_card *card;
1081 unsigned long flags;
1086 card = p->chip->card;
1088 down_write(&card->controls_rwsem);
1089 if (p->qsound_switch) {
1090 snd_ctl_remove(card, p->qsound_switch);
1091 p->qsound_switch = NULL;
1093 if (p->qsound_space) {
1094 snd_ctl_remove(card, p->qsound_space);
1095 p->qsound_space = NULL;
1097 up_write(&card->controls_rwsem);
1099 /* cancel pending transfer of QSound parameters */
1100 spin_lock_irqsave (&p->q_lock, flags);
1101 p->qpos_changed = 0;
1102 spin_unlock_irqrestore (&p->q_lock, flags);
1106 * Transfer qsound parameters to CSP,
1107 * function should be called from interrupt routine
1109 static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p)
1113 spin_lock(&p->q_lock);
1114 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1115 set_codec_parameter(p->chip, 0xe0, 0x01);
1117 set_codec_parameter(p->chip, 0x00, p->qpos_left);
1118 set_codec_parameter(p->chip, 0x02, 0x00);
1120 set_codec_parameter(p->chip, 0x00, p->qpos_right);
1121 set_codec_parameter(p->chip, 0x03, 0x00);
1124 p->qpos_changed = 0;
1125 spin_unlock(&p->q_lock);
1129 /* ------------------------------ */
1134 static int init_proc_entry(struct snd_sb_csp * p, int device)
1137 struct snd_info_entry *entry;
1138 sprintf(name, "cspD%d", device);
1139 if (! snd_card_proc_new(p->chip->card, name, &entry))
1140 snd_info_set_text_ops(entry, p, info_read);
1144 static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
1146 struct snd_sb_csp *p = entry->private_data;
1148 snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
1149 snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
1150 ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
1151 ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
1152 ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
1153 if (p->running & SNDRV_SB_CSP_ST_LOADED) {
1154 snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
1155 snd_iprintf(buffer, "Sample rates: ");
1156 if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
1157 snd_iprintf(buffer, "All\n");
1159 snd_iprintf(buffer, "%s%s%s%s\n",
1160 ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
1161 ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
1162 ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
1163 ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
1165 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
1166 snd_iprintf(buffer, "QSound decoder %sabled\n",
1167 p->q_enabled ? "en" : "dis");
1169 snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
1171 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
1172 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
1173 ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
1174 ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
1175 ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
1176 ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
1179 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
1180 snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1182 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
1183 snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
1184 ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
1185 ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
1187 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1188 snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
1189 p->qpos_left, p->qpos_right);
1195 EXPORT_SYMBOL(snd_sb_csp_new);