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");
40 #ifdef SNDRV_LITTLE_ENDIAN
41 #define CSP_HDR_VALUE(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
43 #define CSP_HDR_VALUE(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
46 #define RIFF_HEADER CSP_HDR_VALUE('R', 'I', 'F', 'F')
47 #define CSP__HEADER CSP_HDR_VALUE('C', 'S', 'P', ' ')
48 #define LIST_HEADER CSP_HDR_VALUE('L', 'I', 'S', 'T')
49 #define FUNC_HEADER CSP_HDR_VALUE('f', 'u', 'n', 'c')
50 #define CODE_HEADER CSP_HDR_VALUE('c', 'o', 'd', 'e')
51 #define INIT_HEADER CSP_HDR_VALUE('i', 'n', 'i', 't')
52 #define MAIN_HEADER CSP_HDR_VALUE('m', 'a', 'i', 'n')
63 struct riff_header info;
67 __u16 flags_16bit_8bit;
68 __u16 flags_stereo_mono;
75 static void snd_sb_csp_free(snd_hwdep_t *hw);
76 static int snd_sb_csp_open(snd_hwdep_t * hw, struct file *file);
77 static int snd_sb_csp_ioctl(snd_hwdep_t * hw, struct file *file, unsigned int cmd, unsigned long arg);
78 static int snd_sb_csp_release(snd_hwdep_t * hw, struct file *file);
80 static int csp_detect(sb_t *chip, int *version);
81 static int set_codec_parameter(sb_t *chip, unsigned char par, unsigned char val);
82 static int set_register(sb_t *chip, unsigned char reg, unsigned char val);
83 static int read_register(sb_t *chip, unsigned char reg);
84 static int set_mode_register(sb_t *chip, unsigned char mode);
85 static int get_version(sb_t *chip);
87 static int snd_sb_csp_riff_load(snd_sb_csp_t * p, snd_sb_csp_microcode_t __user * code);
88 static int snd_sb_csp_unload(snd_sb_csp_t * p);
89 static int snd_sb_csp_load_user(snd_sb_csp_t * p, const unsigned char __user *buf, int size, int load_flags);
90 static int snd_sb_csp_autoload(snd_sb_csp_t * p, int pcm_sfmt, int play_rec_mode);
91 static int snd_sb_csp_check_version(snd_sb_csp_t * p);
93 static int snd_sb_csp_use(snd_sb_csp_t * p);
94 static int snd_sb_csp_unuse(snd_sb_csp_t * p);
95 static int snd_sb_csp_start(snd_sb_csp_t * p, int sample_width, int channels);
96 static int snd_sb_csp_stop(snd_sb_csp_t * p);
97 static int snd_sb_csp_pause(snd_sb_csp_t * p);
98 static int snd_sb_csp_restart(snd_sb_csp_t * p);
100 static int snd_sb_qsound_build(snd_sb_csp_t * p);
101 static void snd_sb_qsound_destroy(snd_sb_csp_t * p);
102 static int snd_sb_csp_qsound_transfer(snd_sb_csp_t * p);
104 static int init_proc_entry(snd_sb_csp_t * p, int device);
105 static void info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer);
108 * Detect CSP chip and create a new instance
110 int snd_sb_csp_new(sb_t *chip, int device, snd_hwdep_t ** rhwdep)
119 if (csp_detect(chip, &version))
122 if ((err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw)) < 0)
125 if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
126 snd_device_free(chip->card, hw);
130 p->version = version;
133 p->ops.csp_use = snd_sb_csp_use;
134 p->ops.csp_unuse = snd_sb_csp_unuse;
135 p->ops.csp_autoload = snd_sb_csp_autoload;
136 p->ops.csp_start = snd_sb_csp_start;
137 p->ops.csp_stop = snd_sb_csp_stop;
138 p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;
140 init_MUTEX(&p->access_mutex);
141 sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f));
142 hw->iface = SNDRV_HWDEP_IFACE_SB16CSP;
143 hw->private_data = p;
144 hw->private_free = snd_sb_csp_free;
146 /* operators - only write/ioctl */
147 hw->ops.open = snd_sb_csp_open;
148 hw->ops.ioctl = snd_sb_csp_ioctl;
149 hw->ops.release = snd_sb_csp_release;
151 /* create a proc entry */
152 init_proc_entry(p, device);
159 * free_private for hwdep instance
161 static void snd_sb_csp_free(snd_hwdep_t *hwdep)
163 snd_sb_csp_t *p = hwdep->private_data;
165 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
171 /* ------------------------------ */
174 * open the device exclusively
176 static int snd_sb_csp_open(snd_hwdep_t * hw, struct file *file)
178 snd_sb_csp_t *p = hw->private_data;
179 return (snd_sb_csp_use(p));
183 * ioctl for hwdep device:
185 static int snd_sb_csp_ioctl(snd_hwdep_t * hw, struct file *file, unsigned int cmd, unsigned long arg)
187 snd_sb_csp_t *p = hw->private_data;
188 snd_sb_csp_info_t info;
189 snd_sb_csp_start_t start_info;
192 snd_assert(p != NULL, return -EINVAL);
194 if (snd_sb_csp_check_version(p))
198 /* get information */
199 case SNDRV_SB_CSP_IOCTL_INFO:
200 *info.codec_name = *p->codec_name;
201 info.func_nr = p->func_nr;
202 info.acc_format = p->acc_format;
203 info.acc_channels = p->acc_channels;
204 info.acc_width = p->acc_width;
205 info.acc_rates = p->acc_rates;
206 info.csp_mode = p->mode;
207 info.run_channels = p->run_channels;
208 info.run_width = p->run_width;
209 info.version = p->version;
210 info.state = p->running;
211 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
217 /* load CSP microcode */
218 case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
219 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
220 -EBUSY : snd_sb_csp_riff_load(p, (snd_sb_csp_microcode_t __user *) arg));
222 case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
223 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
224 -EBUSY : snd_sb_csp_unload(p));
227 /* change CSP running state */
228 case SNDRV_SB_CSP_IOCTL_START:
229 if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info)))
232 err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
234 case SNDRV_SB_CSP_IOCTL_STOP:
235 err = snd_sb_csp_stop(p);
237 case SNDRV_SB_CSP_IOCTL_PAUSE:
238 err = snd_sb_csp_pause(p);
240 case SNDRV_SB_CSP_IOCTL_RESTART:
241 err = snd_sb_csp_restart(p);
254 static int snd_sb_csp_release(snd_hwdep_t * hw, struct file *file)
256 snd_sb_csp_t *p = hw->private_data;
257 return (snd_sb_csp_unuse(p));
260 /* ------------------------------ */
265 static int snd_sb_csp_use(snd_sb_csp_t * p)
267 down(&p->access_mutex);
269 up(&p->access_mutex);
273 up(&p->access_mutex);
282 static int snd_sb_csp_unuse(snd_sb_csp_t * p)
284 down(&p->access_mutex);
286 up(&p->access_mutex);
292 * load microcode via ioctl:
293 * code is user-space pointer
295 static int snd_sb_csp_riff_load(snd_sb_csp_t * p, snd_sb_csp_microcode_t __user * mcode)
297 snd_sb_csp_mc_header_t info;
299 unsigned char __user *data_ptr;
300 unsigned char __user *data_end;
301 unsigned short func_nr = 0;
303 struct riff_header file_h, item_h, code_h;
305 struct desc_header funcdesc_h;
310 if (copy_from_user(&info, mcode, sizeof(info)))
312 data_ptr = mcode->data;
314 if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
316 if ((file_h.name != RIFF_HEADER) ||
317 (le32_to_cpu(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
318 snd_printd("%s: Invalid RIFF header\n", __FUNCTION__);
321 data_ptr += sizeof(file_h);
322 data_end = data_ptr + le32_to_cpu(file_h.len);
324 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
326 if (item_type != CSP__HEADER) {
327 snd_printd("%s: Invalid RIFF file type\n", __FUNCTION__);
330 data_ptr += sizeof (item_type);
332 for (; data_ptr < data_end; data_ptr += le32_to_cpu(item_h.len)) {
333 if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
335 data_ptr += sizeof(item_h);
336 if (item_h.name != LIST_HEADER)
339 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
343 if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
345 func_nr = le16_to_cpu(funcdesc_h.func_nr);
348 if (func_nr != info.func_req)
349 break; /* not required function, try next */
350 data_ptr += sizeof(item_type);
352 /* destroy QSound mixer element */
353 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
354 snd_sb_qsound_destroy(p);
356 /* Clear all flags */
360 /* load microcode blocks */
362 if (data_ptr >= data_end)
364 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
367 /* init microcode blocks */
368 if (code_h.name != INIT_HEADER)
370 data_ptr += sizeof(code_h);
371 err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len),
372 SNDRV_SB_CSP_LOAD_INITBLOCK);
375 data_ptr += le32_to_cpu(code_h.len);
377 /* main microcode block */
378 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
381 if (code_h.name != MAIN_HEADER) {
382 snd_printd("%s: Missing 'main' microcode\n", __FUNCTION__);
385 data_ptr += sizeof(code_h);
386 err = snd_sb_csp_load_user(p, data_ptr,
387 le32_to_cpu(code_h.len), 0);
391 /* fill in codec header */
392 strlcpy(p->codec_name, info.codec_name, sizeof(p->codec_name));
393 p->func_nr = func_nr;
394 p->mode = le16_to_cpu(funcdesc_h.flags_play_rec);
395 switch (le16_to_cpu(funcdesc_h.VOC_type)) {
396 case 0x0001: /* QSound decoder */
397 if (le16_to_cpu(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
398 if (snd_sb_qsound_build(p) == 0)
399 /* set QSound flag and clear all other mode flags */
400 p->mode = SNDRV_SB_CSP_MODE_QSOUND;
404 case 0x0006: /* A Law codec */
405 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
407 case 0x0007: /* Mu Law codec */
408 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
410 case 0x0011: /* what Creative thinks is IMA ADPCM codec */
411 case 0x0200: /* Creative ADPCM codec */
412 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
414 case 201: /* Text 2 Speech decoder */
415 /* TODO: Text2Speech handling routines */
418 case 0x0202: /* Fast Speech 8 codec */
419 case 0x0203: /* Fast Speech 10 codec */
420 p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
422 default: /* other codecs are unsupported */
423 p->acc_format = p->acc_width = p->acc_rates = 0;
425 snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
427 le16_to_cpu(funcdesc_h.VOC_type));
430 p->acc_channels = le16_to_cpu(funcdesc_h.flags_stereo_mono);
431 p->acc_width = le16_to_cpu(funcdesc_h.flags_16bit_8bit);
432 p->acc_rates = le16_to_cpu(funcdesc_h.flags_rates);
434 /* Decouple CSP from IRQ and DMAREQ lines */
435 spin_lock_irqsave(&p->chip->reg_lock, flags);
436 set_mode_register(p->chip, 0xfc);
437 set_mode_register(p->chip, 0x00);
438 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
440 /* finished loading successfully */
441 p->running = SNDRV_SB_CSP_ST_LOADED; /* set LOADED flag */
445 snd_printd("%s: Function #%d not found\n", __FUNCTION__, info.func_req);
450 * unload CSP microcode
452 static int snd_sb_csp_unload(snd_sb_csp_t * p)
454 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
456 if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
459 /* clear supported formats */
461 p->acc_channels = p->acc_width = p->acc_rates = 0;
462 /* destroy QSound mixer element */
463 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
464 snd_sb_qsound_destroy(p);
466 /* clear all flags */
473 * send command sequence to DSP
475 static inline int command_seq(sb_t *chip, const unsigned char *seq, int size)
478 for (i = 0; i < size; i++) {
479 if (!snd_sbdsp_command(chip, seq[i]))
486 * set CSP codec parameter
488 static int set_codec_parameter(sb_t *chip, unsigned char par, unsigned char val)
490 unsigned char dsp_cmd[3];
492 dsp_cmd[0] = 0x05; /* CSP set codec parameter */
493 dsp_cmd[1] = val; /* Parameter value */
494 dsp_cmd[2] = par; /* Parameter */
495 command_seq(chip, dsp_cmd, 3);
496 snd_sbdsp_command(chip, 0x03); /* DSP read? */
497 if (snd_sbdsp_get_byte(chip) != par)
505 static int set_register(sb_t *chip, unsigned char reg, unsigned char val)
507 unsigned char dsp_cmd[3];
509 dsp_cmd[0] = 0x0e; /* CSP set register */
510 dsp_cmd[1] = reg; /* CSP Register */
511 dsp_cmd[2] = val; /* value */
512 return command_seq(chip, dsp_cmd, 3);
517 * return < 0 -> error
519 static int read_register(sb_t *chip, unsigned char reg)
521 unsigned char dsp_cmd[2];
523 dsp_cmd[0] = 0x0f; /* CSP read register */
524 dsp_cmd[1] = reg; /* CSP Register */
525 command_seq(chip, dsp_cmd, 2);
526 return snd_sbdsp_get_byte(chip); /* Read DSP value */
530 * set CSP mode register
532 static int set_mode_register(sb_t *chip, unsigned char mode)
534 unsigned char dsp_cmd[2];
536 dsp_cmd[0] = 0x04; /* CSP set mode register */
537 dsp_cmd[1] = mode; /* mode */
538 return command_seq(chip, dsp_cmd, 2);
543 * return 0 if CSP exists.
545 static int csp_detect(sb_t *chip, int *version)
547 unsigned char csp_test1, csp_test2;
549 int result = -ENODEV;
551 spin_lock_irqsave(&chip->reg_lock, flags);
553 set_codec_parameter(chip, 0x00, 0x00);
554 set_mode_register(chip, 0xfc); /* 0xfc = ?? */
556 csp_test1 = read_register(chip, 0x83);
557 set_register(chip, 0x83, ~csp_test1);
558 csp_test2 = read_register(chip, 0x83);
559 if (csp_test2 != (csp_test1 ^ 0xff))
562 set_register(chip, 0x83, csp_test1);
563 csp_test2 = read_register(chip, 0x83);
564 if (csp_test2 != csp_test1)
567 set_mode_register(chip, 0x00); /* 0x00 = ? */
569 *version = get_version(chip);
570 snd_sbdsp_reset(chip); /* reset DSP after getversion! */
571 if (*version >= 0x10 && *version <= 0x1f)
572 result = 0; /* valid version id */
575 spin_unlock_irqrestore(&chip->reg_lock, flags);
580 * get CSP version number
582 static int get_version(sb_t *chip)
584 unsigned char dsp_cmd[2];
586 dsp_cmd[0] = 0x08; /* SB_DSP_!something! */
587 dsp_cmd[1] = 0x03; /* get chip version id? */
588 command_seq(chip, dsp_cmd, 2);
590 return (snd_sbdsp_get_byte(chip));
594 * check if the CSP version is valid
596 static int snd_sb_csp_check_version(snd_sb_csp_t * p)
598 if (p->version < 0x10 || p->version > 0x1f) {
599 snd_printd("%s: Invalid CSP version: 0x%x\n", __FUNCTION__, p->version);
606 * download microcode to CSP (microcode should have one "main" block).
608 static int snd_sb_csp_load(snd_sb_csp_t * p, const unsigned char *buf, int size, int load_flags)
615 spin_lock_irqsave(&p->chip->reg_lock, flags);
616 snd_sbdsp_command(p->chip, 0x01); /* CSP download command */
617 if (snd_sbdsp_get_byte(p->chip)) {
618 snd_printd("%s: Download command failed\n", __FUNCTION__);
621 /* Send CSP low byte (size - 1) */
622 snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
624 snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
625 /* send microcode sequence */
626 /* load from kernel space */
628 if (!snd_sbdsp_command(p->chip, *buf++))
631 if (snd_sbdsp_get_byte(p->chip))
634 if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
636 /* some codecs (FastSpeech) take some time to initialize */
638 snd_sbdsp_command(p->chip, 0x03);
639 status = snd_sbdsp_get_byte(p->chip);
640 if (status == 0x55 || ++i >= 10)
644 if (status != 0x55) {
645 snd_printd("%s: Microcode initialization failed\n", __FUNCTION__);
650 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
651 * Start CSP chip if no 16bit DMA channel is set - some kind
652 * of autorun or perhaps a bugfix?
654 spin_lock(&p->chip->mixer_lock);
655 status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
656 spin_unlock(&p->chip->mixer_lock);
657 if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
658 err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
659 set_codec_parameter(p->chip, 0xff, 0x00));
660 snd_sbdsp_reset(p->chip); /* really! */
663 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
664 set_mode_register(p->chip, 0x70); /* 70 = RUN */
670 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
674 static int snd_sb_csp_load_user(snd_sb_csp_t * p, const unsigned char __user *buf, int size, int load_flags)
677 unsigned char *kbuf = kmalloc(size, GFP_KERNEL);
679 if (copy_from_user(kbuf, buf, size))
682 err = snd_sb_csp_load(p, kbuf, size, load_flags);
688 #include "sb16_csp_codecs.h"
691 * autoload hardware codec if necessary
692 * return 0 if CSP is loaded and ready to run (p->running != 0)
694 static int snd_sb_csp_autoload(snd_sb_csp_t * p, int pcm_sfmt, int play_rec_mode)
699 /* if CSP is running or manually loaded then exit */
700 if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED))
703 /* autoload microcode only if requested hardware codec is not already loaded */
704 if (((1 << pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
705 p->running = SNDRV_SB_CSP_ST_AUTO;
708 case SNDRV_PCM_FORMAT_MU_LAW:
709 err = snd_sb_csp_load(p, &mulaw_main[0], sizeof(mulaw_main), 0);
710 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
711 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
713 case SNDRV_PCM_FORMAT_A_LAW:
714 err = snd_sb_csp_load(p, &alaw_main[0], sizeof(alaw_main), 0);
715 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
716 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
718 case SNDRV_PCM_FORMAT_IMA_ADPCM:
719 err = snd_sb_csp_load(p, &ima_adpcm_init[0], sizeof(ima_adpcm_init),
720 SNDRV_SB_CSP_LOAD_INITBLOCK);
723 if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
724 err = snd_sb_csp_load(p, &ima_adpcm_playback[0],
725 sizeof(ima_adpcm_playback), 0);
726 p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
728 err = snd_sb_csp_load(p, &ima_adpcm_capture[0],
729 sizeof(ima_adpcm_capture), 0);
730 p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
732 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
735 /* Decouple CSP from IRQ and DMAREQ lines */
736 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
737 spin_lock_irqsave(&p->chip->reg_lock, flags);
738 set_mode_register(p->chip, 0xfc);
739 set_mode_register(p->chip, 0x00);
740 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
741 p->running = 0; /* clear autoloaded flag */
747 p->acc_channels = p->acc_width = p->acc_rates = 0;
749 p->running = 0; /* clear autoloaded flag */
753 p->running = SNDRV_SB_CSP_ST_AUTO; /* set autoloaded flag */
754 p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT; /* only 16 bit data */
755 p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
756 p->acc_rates = SNDRV_SB_CSP_RATE_ALL; /* HW codecs accept all rates */
760 return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
766 static int snd_sb_csp_start(snd_sb_csp_t * p, int sample_width, int channels)
768 unsigned char s_type; /* sample type */
769 unsigned char mixL, mixR;
773 if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
774 snd_printd("%s: Microcode not loaded\n", __FUNCTION__);
777 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
778 snd_printd("%s: CSP already running\n", __FUNCTION__);
781 if (!(sample_width & p->acc_width)) {
782 snd_printd("%s: Unsupported PCM sample width\n", __FUNCTION__);
785 if (!(channels & p->acc_channels)) {
786 snd_printd("%s: Invalid number of channels\n", __FUNCTION__);
790 /* Mute PCM volume */
791 spin_lock_irqsave(&p->chip->mixer_lock, flags);
792 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
793 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
794 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
795 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
797 spin_lock(&p->chip->reg_lock);
798 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
799 set_mode_register(p->chip, 0x70); /* 70 = RUN */
802 if (channels == SNDRV_SB_CSP_MONO)
803 s_type = 0x11; /* 000n 000n (n = 1 if mono) */
804 if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
805 s_type |= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
807 if (set_codec_parameter(p->chip, 0x81, s_type)) {
808 snd_printd("%s: Set sample type command failed\n", __FUNCTION__);
811 if (set_codec_parameter(p->chip, 0x80, 0x00)) {
812 snd_printd("%s: Codec start command failed\n", __FUNCTION__);
815 p->run_width = sample_width;
816 p->run_channels = channels;
818 p->running |= SNDRV_SB_CSP_ST_RUNNING;
820 if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
821 set_codec_parameter(p->chip, 0xe0, 0x01);
822 /* enable QSound decoder */
823 set_codec_parameter(p->chip, 0x00, 0xff);
824 set_codec_parameter(p->chip, 0x01, 0xff);
825 p->running |= SNDRV_SB_CSP_ST_QSOUND;
826 /* set QSound startup value */
827 snd_sb_csp_qsound_transfer(p);
832 spin_unlock(&p->chip->reg_lock);
834 /* restore PCM volume */
835 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
836 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
837 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
845 static int snd_sb_csp_stop(snd_sb_csp_t * p)
848 unsigned char mixL, mixR;
851 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
854 /* Mute PCM volume */
855 spin_lock_irqsave(&p->chip->mixer_lock, flags);
856 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
857 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
858 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
859 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
861 spin_lock(&p->chip->reg_lock);
862 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
863 set_codec_parameter(p->chip, 0xe0, 0x01);
864 /* disable QSound decoder */
865 set_codec_parameter(p->chip, 0x00, 0x00);
866 set_codec_parameter(p->chip, 0x01, 0x00);
868 p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
870 result = set_mode_register(p->chip, 0xc0); /* c0 = STOP */
871 spin_unlock(&p->chip->reg_lock);
873 /* restore PCM volume */
874 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
875 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
876 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
879 p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
884 * pause CSP codec and hold DMA transfer
886 static int snd_sb_csp_pause(snd_sb_csp_t * p)
891 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
894 spin_lock_irqsave(&p->chip->reg_lock, flags);
895 result = set_codec_parameter(p->chip, 0x80, 0xff);
896 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
898 p->running |= SNDRV_SB_CSP_ST_PAUSED;
904 * restart CSP codec and resume DMA transfer
906 static int snd_sb_csp_restart(snd_sb_csp_t * p)
911 if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
914 spin_lock_irqsave(&p->chip->reg_lock, flags);
915 result = set_codec_parameter(p->chip, 0x80, 0x00);
916 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
918 p->running &= ~SNDRV_SB_CSP_ST_PAUSED;
923 /* ------------------------------ */
926 * QSound mixer control for PCM
929 static int snd_sb_qsound_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
931 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
933 uinfo->value.integer.min = 0;
934 uinfo->value.integer.max = 1;
938 static int snd_sb_qsound_switch_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
940 snd_sb_csp_t *p = snd_kcontrol_chip(kcontrol);
942 ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
946 static int snd_sb_qsound_switch_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
948 snd_sb_csp_t *p = snd_kcontrol_chip(kcontrol);
953 nval = ucontrol->value.integer.value[0] & 0x01;
954 spin_lock_irqsave(&p->q_lock, flags);
955 change = p->q_enabled != nval;
957 spin_unlock_irqrestore(&p->q_lock, flags);
961 static int snd_sb_qsound_space_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
963 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
965 uinfo->value.integer.min = 0;
966 uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
970 static int snd_sb_qsound_space_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
972 snd_sb_csp_t *p = snd_kcontrol_chip(kcontrol);
975 spin_lock_irqsave(&p->q_lock, flags);
976 ucontrol->value.integer.value[0] = p->qpos_left;
977 ucontrol->value.integer.value[1] = p->qpos_right;
978 spin_unlock_irqrestore(&p->q_lock, flags);
982 static int snd_sb_qsound_space_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
984 snd_sb_csp_t *p = snd_kcontrol_chip(kcontrol);
987 unsigned char nval1, nval2;
989 nval1 = ucontrol->value.integer.value[0];
990 if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
991 nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
992 nval2 = ucontrol->value.integer.value[1];
993 if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
994 nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
995 spin_lock_irqsave(&p->q_lock, flags);
996 change = p->qpos_left != nval1 || p->qpos_right != nval2;
997 p->qpos_left = nval1;
998 p->qpos_right = nval2;
999 p->qpos_changed = change;
1000 spin_unlock_irqrestore(&p->q_lock, flags);
1004 static snd_kcontrol_new_t snd_sb_qsound_switch = {
1005 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1006 .name = "3D Control - Switch",
1007 .info = snd_sb_qsound_switch_info,
1008 .get = snd_sb_qsound_switch_get,
1009 .put = snd_sb_qsound_switch_put
1012 static snd_kcontrol_new_t snd_sb_qsound_space = {
1013 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1014 .name = "3D Control - Space",
1015 .info = snd_sb_qsound_space_info,
1016 .get = snd_sb_qsound_space_get,
1017 .put = snd_sb_qsound_space_put
1020 static int snd_sb_qsound_build(snd_sb_csp_t * p)
1025 snd_assert(p != NULL, return -EINVAL);
1027 card = p->chip->card;
1028 p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
1029 p->qpos_changed = 0;
1031 spin_lock_init(&p->q_lock);
1033 if ((err = snd_ctl_add(card, p->qsound_switch = snd_ctl_new1(&snd_sb_qsound_switch, p))) < 0)
1035 if ((err = snd_ctl_add(card, p->qsound_space = snd_ctl_new1(&snd_sb_qsound_space, p))) < 0)
1041 snd_sb_qsound_destroy(p);
1045 static void snd_sb_qsound_destroy(snd_sb_csp_t * p)
1048 unsigned long flags;
1050 snd_assert(p != NULL, return);
1052 card = p->chip->card;
1054 down_write(&card->controls_rwsem);
1055 if (p->qsound_switch)
1056 snd_ctl_remove(card, p->qsound_switch);
1057 if (p->qsound_space)
1058 snd_ctl_remove(card, p->qsound_space);
1059 up_write(&card->controls_rwsem);
1061 /* cancel pending transfer of QSound parameters */
1062 spin_lock_irqsave (&p->q_lock, flags);
1063 p->qpos_changed = 0;
1064 spin_unlock_irqrestore (&p->q_lock, flags);
1068 * Transfer qsound parameters to CSP,
1069 * function should be called from interrupt routine
1071 static int snd_sb_csp_qsound_transfer(snd_sb_csp_t * p)
1075 spin_lock(&p->q_lock);
1076 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1077 set_codec_parameter(p->chip, 0xe0, 0x01);
1079 set_codec_parameter(p->chip, 0x00, p->qpos_left);
1080 set_codec_parameter(p->chip, 0x02, 0x00);
1082 set_codec_parameter(p->chip, 0x00, p->qpos_right);
1083 set_codec_parameter(p->chip, 0x03, 0x00);
1086 p->qpos_changed = 0;
1087 spin_unlock(&p->q_lock);
1091 /* ------------------------------ */
1096 static int init_proc_entry(snd_sb_csp_t * p, int device)
1099 snd_info_entry_t *entry;
1100 sprintf(name, "cspD%d", device);
1101 if (! snd_card_proc_new(p->chip->card, name, &entry))
1102 snd_info_set_text_ops(entry, p, 1024, info_read);
1106 static void info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
1108 snd_sb_csp_t *p = entry->private_data;
1110 snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
1111 snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
1112 ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
1113 ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
1114 ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
1115 if (p->running & SNDRV_SB_CSP_ST_LOADED) {
1116 snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
1117 snd_iprintf(buffer, "Sample rates: ");
1118 if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
1119 snd_iprintf(buffer, "All\n");
1121 snd_iprintf(buffer, "%s%s%s%s\n",
1122 ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
1123 ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
1124 ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
1125 ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
1127 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
1128 snd_iprintf(buffer, "QSound decoder %sabled\n",
1129 p->q_enabled ? "en" : "dis");
1131 snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
1133 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
1134 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
1135 ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
1136 ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
1137 ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
1138 ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
1141 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
1142 snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1144 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
1145 snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
1146 ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
1147 ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
1149 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1150 snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
1151 p->qpos_left, p->qpos_right);
1157 EXPORT_SYMBOL(snd_sb_csp_new);
1163 static int __init alsa_sb_csp_init(void)
1168 static void __exit alsa_sb_csp_exit(void)
1172 module_init(alsa_sb_csp_init)
1173 module_exit(alsa_sb_csp_exit)