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 #define chip_t snd_sb_csp_t
38 MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
39 MODULE_DESCRIPTION("ALSA driver for SB16 Creative Signal Processor");
40 MODULE_LICENSE("GPL");
41 MODULE_CLASSES("{sound}");
43 #ifdef SNDRV_LITTLE_ENDIAN
44 #define CSP_HDR_VALUE(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
46 #define CSP_HDR_VALUE(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
48 #define LE_SHORT(v) le16_to_cpu(v)
49 #define LE_INT(v) le32_to_cpu(v)
51 #define RIFF_HEADER CSP_HDR_VALUE('R', 'I', 'F', 'F')
52 #define CSP__HEADER CSP_HDR_VALUE('C', 'S', 'P', ' ')
53 #define LIST_HEADER CSP_HDR_VALUE('L', 'I', 'S', 'T')
54 #define FUNC_HEADER CSP_HDR_VALUE('f', 'u', 'n', 'c')
55 #define CODE_HEADER CSP_HDR_VALUE('c', 'o', 'd', 'e')
56 #define INIT_HEADER CSP_HDR_VALUE('i', 'n', 'i', 't')
57 #define MAIN_HEADER CSP_HDR_VALUE('m', 'a', 'i', 'n')
62 typedef struct riff_header {
67 typedef struct desc_header {
72 __u16 flags_16bit_8bit;
73 __u16 flags_stereo_mono;
80 static void snd_sb_csp_free(snd_hwdep_t *hw);
81 static int snd_sb_csp_open(snd_hwdep_t * hw, struct file *file);
82 static int snd_sb_csp_ioctl(snd_hwdep_t * hw, struct file *file, unsigned int cmd, unsigned long arg);
83 static int snd_sb_csp_release(snd_hwdep_t * hw, struct file *file);
85 static int csp_detect(sb_t *chip, int *version);
86 static int set_codec_parameter(sb_t *chip, unsigned char par, unsigned char val);
87 static int set_register(sb_t *chip, unsigned char reg, unsigned char val);
88 static int read_register(sb_t *chip, unsigned char reg);
89 static int set_mode_register(sb_t *chip, unsigned char mode);
90 static int get_version(sb_t *chip);
92 static int snd_sb_csp_riff_load(snd_sb_csp_t * p, snd_sb_csp_microcode_t __user * code);
93 static int snd_sb_csp_unload(snd_sb_csp_t * p);
94 static int snd_sb_csp_load_user(snd_sb_csp_t * p, const unsigned char __user *buf, int size, int load_flags);
95 static int snd_sb_csp_autoload(snd_sb_csp_t * p, int pcm_sfmt, int play_rec_mode);
96 static int snd_sb_csp_check_version(snd_sb_csp_t * p);
98 static int snd_sb_csp_use(snd_sb_csp_t * p);
99 static int snd_sb_csp_unuse(snd_sb_csp_t * p);
100 static int snd_sb_csp_start(snd_sb_csp_t * p, int sample_width, int channels);
101 static int snd_sb_csp_stop(snd_sb_csp_t * p);
102 static int snd_sb_csp_pause(snd_sb_csp_t * p);
103 static int snd_sb_csp_restart(snd_sb_csp_t * p);
105 static int snd_sb_qsound_build(snd_sb_csp_t * p);
106 static void snd_sb_qsound_destroy(snd_sb_csp_t * p);
107 static int snd_sb_csp_qsound_transfer(snd_sb_csp_t * p);
109 static int init_proc_entry(snd_sb_csp_t * p, int device);
110 static void info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer);
113 * Detect CSP chip and create a new instance
115 int snd_sb_csp_new(sb_t *chip, int device, snd_hwdep_t ** rhwdep)
124 if (csp_detect(chip, &version))
127 if ((err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw)) < 0)
130 if ((p = snd_magic_kcalloc(snd_sb_csp_t, 0, GFP_KERNEL)) == NULL) {
131 snd_device_free(chip->card, hw);
135 p->version = version;
138 p->ops.csp_use = snd_sb_csp_use;
139 p->ops.csp_unuse = snd_sb_csp_unuse;
140 p->ops.csp_autoload = snd_sb_csp_autoload;
141 p->ops.csp_start = snd_sb_csp_start;
142 p->ops.csp_stop = snd_sb_csp_stop;
143 p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;
145 init_MUTEX(&p->access_mutex);
146 sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f));
147 hw->iface = SNDRV_HWDEP_IFACE_SB16CSP;
148 hw->private_data = p;
149 hw->private_free = snd_sb_csp_free;
151 /* operators - only write/ioctl */
152 hw->ops.open = snd_sb_csp_open;
153 hw->ops.ioctl = snd_sb_csp_ioctl;
154 hw->ops.release = snd_sb_csp_release;
156 /* create a proc entry */
157 init_proc_entry(p, device);
164 * free_private for hwdep instance
166 static void snd_sb_csp_free(snd_hwdep_t *hwdep)
168 snd_sb_csp_t *p = snd_magic_cast(snd_sb_csp_t, hwdep->private_data, return);
170 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
176 /* ------------------------------ */
179 * open the device exclusively
181 static int snd_sb_csp_open(snd_hwdep_t * hw, struct file *file)
183 snd_sb_csp_t *p = snd_magic_cast(snd_sb_csp_t, hw->private_data, return -ENXIO);
184 return (snd_sb_csp_use(p));
188 * ioctl for hwdep device:
190 static int snd_sb_csp_ioctl(snd_hwdep_t * hw, struct file *file, unsigned int cmd, unsigned long arg)
192 snd_sb_csp_t *p = snd_magic_cast(snd_sb_csp_t, hw->private_data, return -ENXIO);
193 snd_sb_csp_info_t info;
194 snd_sb_csp_start_t start_info;
197 snd_assert(p != NULL, return -EINVAL);
199 if (snd_sb_csp_check_version(p))
203 /* get information */
204 case SNDRV_SB_CSP_IOCTL_INFO:
205 *info.codec_name = *p->codec_name;
206 info.func_nr = p->func_nr;
207 info.acc_format = p->acc_format;
208 info.acc_channels = p->acc_channels;
209 info.acc_width = p->acc_width;
210 info.acc_rates = p->acc_rates;
211 info.csp_mode = p->mode;
212 info.run_channels = p->run_channels;
213 info.run_width = p->run_width;
214 info.version = p->version;
215 info.state = p->running;
216 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
222 /* load CSP microcode */
223 case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
224 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
225 -EBUSY : snd_sb_csp_riff_load(p, (snd_sb_csp_microcode_t __user *) arg));
227 case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
228 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
229 -EBUSY : snd_sb_csp_unload(p));
232 /* change CSP running state */
233 case SNDRV_SB_CSP_IOCTL_START:
234 if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info)))
237 err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
239 case SNDRV_SB_CSP_IOCTL_STOP:
240 err = snd_sb_csp_stop(p);
242 case SNDRV_SB_CSP_IOCTL_PAUSE:
243 err = snd_sb_csp_pause(p);
245 case SNDRV_SB_CSP_IOCTL_RESTART:
246 err = snd_sb_csp_restart(p);
259 static int snd_sb_csp_release(snd_hwdep_t * hw, struct file *file)
261 snd_sb_csp_t *p = snd_magic_cast(snd_sb_csp_t, hw->private_data, return -ENXIO);
262 return (snd_sb_csp_unuse(p));
265 /* ------------------------------ */
270 static int snd_sb_csp_use(snd_sb_csp_t * p)
272 down(&p->access_mutex);
274 up(&p->access_mutex);
278 up(&p->access_mutex);
287 static int snd_sb_csp_unuse(snd_sb_csp_t * p)
289 down(&p->access_mutex);
291 up(&p->access_mutex);
297 * load microcode via ioctl:
298 * code is user-space pointer
300 static int snd_sb_csp_riff_load(snd_sb_csp_t * p, snd_sb_csp_microcode_t __user * mcode)
302 snd_sb_csp_mc_header_t info;
304 unsigned char __user *data_ptr;
305 unsigned char __user *data_end;
306 unsigned short func_nr = 0;
308 riff_header_t file_h, item_h, code_h;
310 desc_header_t funcdesc_h;
315 if (copy_from_user(&info, mcode, sizeof(info)))
317 data_ptr = mcode->data;
319 if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
321 if ((file_h.name != RIFF_HEADER) ||
322 (LE_INT(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
323 snd_printd("%s: Invalid RIFF header\n", __FUNCTION__);
326 data_ptr += sizeof(file_h);
327 data_end = data_ptr + LE_INT(file_h.len);
329 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
331 if (item_type != CSP__HEADER) {
332 snd_printd("%s: Invalid RIFF file type\n", __FUNCTION__);
335 data_ptr += sizeof (item_type);
337 for (; data_ptr < data_end; data_ptr += LE_INT(item_h.len)) {
338 if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
340 data_ptr += sizeof(item_h);
341 if (item_h.name != LIST_HEADER)
344 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
348 if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
350 func_nr = LE_SHORT(funcdesc_h.func_nr);
353 if (func_nr != info.func_req)
354 break; /* not required function, try next */
355 data_ptr += sizeof(item_type);
357 /* destroy QSound mixer element */
358 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
359 snd_sb_qsound_destroy(p);
361 /* Clear all flags */
365 /* load microcode blocks */
367 if (data_ptr >= data_end)
369 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
372 /* init microcode blocks */
373 if (code_h.name != INIT_HEADER)
375 data_ptr += sizeof(code_h);
376 err = snd_sb_csp_load_user(p, data_ptr, LE_INT(code_h.len),
377 SNDRV_SB_CSP_LOAD_INITBLOCK);
380 data_ptr += LE_INT(code_h.len);
382 /* main microcode block */
383 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
386 if (code_h.name != MAIN_HEADER) {
387 snd_printd("%s: Missing 'main' microcode\n", __FUNCTION__);
390 data_ptr += sizeof(code_h);
391 err = snd_sb_csp_load_user(p, data_ptr,
392 LE_INT(code_h.len), 0);
396 /* fill in codec header */
397 strlcpy(p->codec_name, info.codec_name, sizeof(p->codec_name));
398 p->func_nr = func_nr;
399 p->mode = LE_SHORT(funcdesc_h.flags_play_rec);
400 switch (LE_SHORT(funcdesc_h.VOC_type)) {
401 case 0x0001: /* QSound decoder */
402 if (LE_SHORT(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
403 if (snd_sb_qsound_build(p) == 0)
404 /* set QSound flag and clear all other mode flags */
405 p->mode = SNDRV_SB_CSP_MODE_QSOUND;
409 case 0x0006: /* A Law codec */
410 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
412 case 0x0007: /* Mu Law codec */
413 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
415 case 0x0011: /* what Creative thinks is IMA ADPCM codec */
416 case 0x0200: /* Creative ADPCM codec */
417 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
419 case 201: /* Text 2 Speech decoder */
420 /* TODO: Text2Speech handling routines */
423 case 0x0202: /* Fast Speech 8 codec */
424 case 0x0203: /* Fast Speech 10 codec */
425 p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
427 default: /* other codecs are unsupported */
428 p->acc_format = p->acc_width = p->acc_rates = 0;
430 snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
432 LE_SHORT(funcdesc_h.VOC_type));
435 p->acc_channels = LE_SHORT(funcdesc_h.flags_stereo_mono);
436 p->acc_width = LE_SHORT(funcdesc_h.flags_16bit_8bit);
437 p->acc_rates = LE_SHORT(funcdesc_h.flags_rates);
439 /* Decouple CSP from IRQ and DMAREQ lines */
440 spin_lock_irqsave(&p->chip->reg_lock, flags);
441 set_mode_register(p->chip, 0xfc);
442 set_mode_register(p->chip, 0x00);
443 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
445 /* finished loading successfully */
446 p->running = SNDRV_SB_CSP_ST_LOADED; /* set LOADED flag */
450 snd_printd("%s: Function #%d not found\n", __FUNCTION__, info.func_req);
455 * unload CSP microcode
457 static int snd_sb_csp_unload(snd_sb_csp_t * p)
459 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
461 if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
464 /* clear supported formats */
466 p->acc_channels = p->acc_width = p->acc_rates = 0;
467 /* destroy QSound mixer element */
468 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
469 snd_sb_qsound_destroy(p);
471 /* clear all flags */
478 * send command sequence to DSP
480 static inline int command_seq(sb_t *chip, const unsigned char *seq, int size)
483 for (i = 0; i < size; i++) {
484 if (!snd_sbdsp_command(chip, seq[i]))
491 * set CSP codec parameter
493 static int set_codec_parameter(sb_t *chip, unsigned char par, unsigned char val)
495 unsigned char dsp_cmd[3];
497 dsp_cmd[0] = 0x05; /* CSP set codec parameter */
498 dsp_cmd[1] = val; /* Parameter value */
499 dsp_cmd[2] = par; /* Parameter */
500 command_seq(chip, dsp_cmd, 3);
501 snd_sbdsp_command(chip, 0x03); /* DSP read? */
502 if (snd_sbdsp_get_byte(chip) != par)
510 static int set_register(sb_t *chip, unsigned char reg, unsigned char val)
512 unsigned char dsp_cmd[3];
514 dsp_cmd[0] = 0x0e; /* CSP set register */
515 dsp_cmd[1] = reg; /* CSP Register */
516 dsp_cmd[2] = val; /* value */
517 return command_seq(chip, dsp_cmd, 3);
522 * return < 0 -> error
524 static int read_register(sb_t *chip, unsigned char reg)
526 unsigned char dsp_cmd[2];
528 dsp_cmd[0] = 0x0f; /* CSP read register */
529 dsp_cmd[1] = reg; /* CSP Register */
530 command_seq(chip, dsp_cmd, 2);
531 return snd_sbdsp_get_byte(chip); /* Read DSP value */
535 * set CSP mode register
537 static int set_mode_register(sb_t *chip, unsigned char mode)
539 unsigned char dsp_cmd[2];
541 dsp_cmd[0] = 0x04; /* CSP set mode register */
542 dsp_cmd[1] = mode; /* mode */
543 return command_seq(chip, dsp_cmd, 2);
548 * return 0 if CSP exists.
550 static int csp_detect(sb_t *chip, int *version)
552 unsigned char csp_test1, csp_test2;
554 int result = -ENODEV;
556 spin_lock_irqsave(&chip->reg_lock, flags);
558 set_codec_parameter(chip, 0x00, 0x00);
559 set_mode_register(chip, 0xfc); /* 0xfc = ?? */
561 csp_test1 = read_register(chip, 0x83);
562 set_register(chip, 0x83, ~csp_test1);
563 csp_test2 = read_register(chip, 0x83);
564 if (csp_test2 != (csp_test1 ^ 0xff))
567 set_register(chip, 0x83, csp_test1);
568 csp_test2 = read_register(chip, 0x83);
569 if (csp_test2 != csp_test1)
572 set_mode_register(chip, 0x00); /* 0x00 = ? */
574 *version = get_version(chip);
575 snd_sbdsp_reset(chip); /* reset DSP after getversion! */
576 if (*version >= 0x10 && *version <= 0x1f)
577 result = 0; /* valid version id */
580 spin_unlock_irqrestore(&chip->reg_lock, flags);
585 * get CSP version number
587 static int get_version(sb_t *chip)
589 unsigned char dsp_cmd[2];
591 dsp_cmd[0] = 0x08; /* SB_DSP_!something! */
592 dsp_cmd[1] = 0x03; /* get chip version id? */
593 command_seq(chip, dsp_cmd, 2);
595 return (snd_sbdsp_get_byte(chip));
599 * check if the CSP version is valid
601 static int snd_sb_csp_check_version(snd_sb_csp_t * p)
603 if (p->version < 0x10 || p->version > 0x1f) {
604 snd_printd("%s: Invalid CSP version: 0x%x\n", __FUNCTION__, p->version);
611 * download microcode to CSP (microcode should have one "main" block).
613 static int snd_sb_csp_load(snd_sb_csp_t * p, const unsigned char *buf, int size, int load_flags)
620 spin_lock_irqsave(&p->chip->reg_lock, flags);
621 snd_sbdsp_command(p->chip, 0x01); /* CSP download command */
622 if (snd_sbdsp_get_byte(p->chip)) {
623 snd_printd("%s: Download command failed\n", __FUNCTION__);
626 /* Send CSP low byte (size - 1) */
627 snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
629 snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
630 /* send microcode sequence */
631 /* load from kernel space */
633 if (!snd_sbdsp_command(p->chip, *buf++))
636 if (snd_sbdsp_get_byte(p->chip))
639 if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
641 /* some codecs (FastSpeech) take some time to initialize */
643 snd_sbdsp_command(p->chip, 0x03);
644 status = snd_sbdsp_get_byte(p->chip);
645 if (status == 0x55 || ++i >= 10)
649 if (status != 0x55) {
650 snd_printd("%s: Microcode initialization failed\n", __FUNCTION__);
655 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
656 * Start CSP chip if no 16bit DMA channel is set - some kind
657 * of autorun or perhaps a bugfix?
659 spin_lock(&p->chip->mixer_lock);
660 status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
661 spin_unlock(&p->chip->mixer_lock);
662 if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
663 err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
664 set_codec_parameter(p->chip, 0xff, 0x00));
665 snd_sbdsp_reset(p->chip); /* really! */
668 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
669 set_mode_register(p->chip, 0x70); /* 70 = RUN */
675 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
679 static int snd_sb_csp_load_user(snd_sb_csp_t * p, const unsigned char __user *buf, int size, int load_flags)
682 unsigned char *kbuf = kmalloc(size, GFP_KERNEL);
684 if (copy_from_user(kbuf, buf, size))
687 err = snd_sb_csp_load(p, kbuf, size, load_flags);
693 #include "sb16_csp_codecs.h"
696 * autoload hardware codec if necessary
697 * return 0 if CSP is loaded and ready to run (p->running != 0)
699 static int snd_sb_csp_autoload(snd_sb_csp_t * p, int pcm_sfmt, int play_rec_mode)
704 /* if CSP is running or manually loaded then exit */
705 if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED))
708 /* autoload microcode only if requested hardware codec is not already loaded */
709 if (((1 << pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
710 p->running = SNDRV_SB_CSP_ST_AUTO;
713 case SNDRV_PCM_FORMAT_MU_LAW:
714 err = snd_sb_csp_load(p, &mulaw_main[0], sizeof(mulaw_main), 0);
715 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
716 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
718 case SNDRV_PCM_FORMAT_A_LAW:
719 err = snd_sb_csp_load(p, &alaw_main[0], sizeof(alaw_main), 0);
720 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
721 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
723 case SNDRV_PCM_FORMAT_IMA_ADPCM:
724 err = snd_sb_csp_load(p, &ima_adpcm_init[0], sizeof(ima_adpcm_init),
725 SNDRV_SB_CSP_LOAD_INITBLOCK);
728 if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
729 err = snd_sb_csp_load(p, &ima_adpcm_playback[0],
730 sizeof(ima_adpcm_playback), 0);
731 p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
733 err = snd_sb_csp_load(p, &ima_adpcm_capture[0],
734 sizeof(ima_adpcm_capture), 0);
735 p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
737 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
740 /* Decouple CSP from IRQ and DMAREQ lines */
741 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
742 spin_lock_irqsave(&p->chip->reg_lock, flags);
743 set_mode_register(p->chip, 0xfc);
744 set_mode_register(p->chip, 0x00);
745 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
746 p->running = 0; /* clear autoloaded flag */
752 p->acc_channels = p->acc_width = p->acc_rates = 0;
754 p->running = 0; /* clear autoloaded flag */
758 p->running = SNDRV_SB_CSP_ST_AUTO; /* set autoloaded flag */
759 p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT; /* only 16 bit data */
760 p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
761 p->acc_rates = SNDRV_SB_CSP_RATE_ALL; /* HW codecs accept all rates */
765 return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
771 static int snd_sb_csp_start(snd_sb_csp_t * p, int sample_width, int channels)
773 unsigned char s_type; /* sample type */
774 unsigned char mixL, mixR;
778 if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
779 snd_printd("%s: Microcode not loaded\n", __FUNCTION__);
782 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
783 snd_printd("%s: CSP already running\n", __FUNCTION__);
786 if (!(sample_width & p->acc_width)) {
787 snd_printd("%s: Unsupported PCM sample width\n", __FUNCTION__);
790 if (!(channels & p->acc_channels)) {
791 snd_printd("%s: Invalid number of channels\n", __FUNCTION__);
795 /* Mute PCM volume */
796 spin_lock_irqsave(&p->chip->mixer_lock, flags);
797 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
798 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
799 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
800 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
802 spin_lock(&p->chip->reg_lock);
803 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
804 set_mode_register(p->chip, 0x70); /* 70 = RUN */
807 if (channels == SNDRV_SB_CSP_MONO)
808 s_type = 0x11; /* 000n 000n (n = 1 if mono) */
809 if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
810 s_type |= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
812 if (set_codec_parameter(p->chip, 0x81, s_type)) {
813 snd_printd("%s: Set sample type command failed\n", __FUNCTION__);
816 if (set_codec_parameter(p->chip, 0x80, 0x00)) {
817 snd_printd("%s: Codec start command failed\n", __FUNCTION__);
820 p->run_width = sample_width;
821 p->run_channels = channels;
823 p->running |= SNDRV_SB_CSP_ST_RUNNING;
825 if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
826 set_codec_parameter(p->chip, 0xe0, 0x01);
827 /* enable QSound decoder */
828 set_codec_parameter(p->chip, 0x00, 0xff);
829 set_codec_parameter(p->chip, 0x01, 0xff);
830 p->running |= SNDRV_SB_CSP_ST_QSOUND;
831 /* set QSound startup value */
832 snd_sb_csp_qsound_transfer(p);
837 spin_unlock(&p->chip->reg_lock);
839 /* restore PCM volume */
840 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
841 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
842 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
850 static int snd_sb_csp_stop(snd_sb_csp_t * p)
853 unsigned char mixL, mixR;
856 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
859 /* Mute PCM volume */
860 spin_lock_irqsave(&p->chip->mixer_lock, flags);
861 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
862 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
863 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
864 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
866 spin_lock(&p->chip->reg_lock);
867 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
868 set_codec_parameter(p->chip, 0xe0, 0x01);
869 /* disable QSound decoder */
870 set_codec_parameter(p->chip, 0x00, 0x00);
871 set_codec_parameter(p->chip, 0x01, 0x00);
873 p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
875 result = set_mode_register(p->chip, 0xc0); /* c0 = STOP */
876 spin_unlock(&p->chip->reg_lock);
878 /* restore PCM volume */
879 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
880 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
881 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
884 p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
889 * pause CSP codec and hold DMA transfer
891 static int snd_sb_csp_pause(snd_sb_csp_t * p)
896 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
899 spin_lock_irqsave(&p->chip->reg_lock, flags);
900 result = set_codec_parameter(p->chip, 0x80, 0xff);
901 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
903 p->running |= SNDRV_SB_CSP_ST_PAUSED;
909 * restart CSP codec and resume DMA transfer
911 static int snd_sb_csp_restart(snd_sb_csp_t * p)
916 if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
919 spin_lock_irqsave(&p->chip->reg_lock, flags);
920 result = set_codec_parameter(p->chip, 0x80, 0x00);
921 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
923 p->running &= ~SNDRV_SB_CSP_ST_PAUSED;
928 /* ------------------------------ */
931 * QSound mixer control for PCM
934 static int snd_sb_qsound_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
936 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
938 uinfo->value.integer.min = 0;
939 uinfo->value.integer.max = 1;
943 static int snd_sb_qsound_switch_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
945 snd_sb_csp_t *p = snd_kcontrol_chip(kcontrol);
947 ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
951 static int snd_sb_qsound_switch_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
953 snd_sb_csp_t *p = snd_kcontrol_chip(kcontrol);
958 nval = ucontrol->value.integer.value[0] & 0x01;
959 spin_lock_irqsave(&p->q_lock, flags);
960 change = p->q_enabled != nval;
962 spin_unlock_irqrestore(&p->q_lock, flags);
966 static int snd_sb_qsound_space_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
968 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
970 uinfo->value.integer.min = 0;
971 uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
975 static int snd_sb_qsound_space_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
977 snd_sb_csp_t *p = snd_kcontrol_chip(kcontrol);
980 spin_lock_irqsave(&p->q_lock, flags);
981 ucontrol->value.integer.value[0] = p->qpos_left;
982 ucontrol->value.integer.value[1] = p->qpos_right;
983 spin_unlock_irqrestore(&p->q_lock, flags);
987 static int snd_sb_qsound_space_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
989 snd_sb_csp_t *p = snd_kcontrol_chip(kcontrol);
992 unsigned char nval1, nval2;
994 nval1 = ucontrol->value.integer.value[0];
995 if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
996 nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
997 nval2 = ucontrol->value.integer.value[1];
998 if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
999 nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1000 spin_lock_irqsave(&p->q_lock, flags);
1001 change = p->qpos_left != nval1 || p->qpos_right != nval2;
1002 p->qpos_left = nval1;
1003 p->qpos_right = nval2;
1004 p->qpos_changed = change;
1005 spin_unlock_irqrestore(&p->q_lock, flags);
1009 static snd_kcontrol_new_t snd_sb_qsound_switch = {
1010 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1011 .name = "3D Control - Switch",
1012 .info = snd_sb_qsound_switch_info,
1013 .get = snd_sb_qsound_switch_get,
1014 .put = snd_sb_qsound_switch_put
1017 static snd_kcontrol_new_t snd_sb_qsound_space = {
1018 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1019 .name = "3D Control - Space",
1020 .info = snd_sb_qsound_space_info,
1021 .get = snd_sb_qsound_space_get,
1022 .put = snd_sb_qsound_space_put
1025 static int snd_sb_qsound_build(snd_sb_csp_t * p)
1030 snd_assert(p != NULL, return -EINVAL);
1032 card = p->chip->card;
1033 p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
1034 p->qpos_changed = 0;
1036 spin_lock_init(&p->q_lock);
1038 if ((err = snd_ctl_add(card, p->qsound_switch = snd_ctl_new1(&snd_sb_qsound_switch, p))) < 0)
1040 if ((err = snd_ctl_add(card, p->qsound_space = snd_ctl_new1(&snd_sb_qsound_space, p))) < 0)
1046 snd_sb_qsound_destroy(p);
1050 static void snd_sb_qsound_destroy(snd_sb_csp_t * p)
1053 unsigned long flags;
1055 snd_assert(p != NULL, return);
1057 card = p->chip->card;
1059 down_write(&card->controls_rwsem);
1060 if (p->qsound_switch)
1061 snd_ctl_remove(card, p->qsound_switch);
1062 if (p->qsound_space)
1063 snd_ctl_remove(card, p->qsound_space);
1064 up_write(&card->controls_rwsem);
1066 /* cancel pending transfer of QSound parameters */
1067 spin_lock_irqsave (&p->q_lock, flags);
1068 p->qpos_changed = 0;
1069 spin_unlock_irqrestore (&p->q_lock, flags);
1073 * Transfer qsound parameters to CSP,
1074 * function should be called from interrupt routine
1076 static int snd_sb_csp_qsound_transfer(snd_sb_csp_t * p)
1080 spin_lock(&p->q_lock);
1081 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1082 set_codec_parameter(p->chip, 0xe0, 0x01);
1084 set_codec_parameter(p->chip, 0x00, p->qpos_left);
1085 set_codec_parameter(p->chip, 0x02, 0x00);
1087 set_codec_parameter(p->chip, 0x00, p->qpos_right);
1088 set_codec_parameter(p->chip, 0x03, 0x00);
1091 p->qpos_changed = 0;
1092 spin_unlock(&p->q_lock);
1096 /* ------------------------------ */
1101 static int init_proc_entry(snd_sb_csp_t * p, int device)
1104 snd_info_entry_t *entry;
1105 sprintf(name, "cspD%d", device);
1106 if (! snd_card_proc_new(p->chip->card, name, &entry))
1107 snd_info_set_text_ops(entry, p, 1024, info_read);
1111 static void info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
1113 snd_sb_csp_t *p = snd_magic_cast(snd_sb_csp_t, entry->private_data, return);
1115 snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
1116 snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
1117 ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
1118 ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
1119 ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
1120 if (p->running & SNDRV_SB_CSP_ST_LOADED) {
1121 snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
1122 snd_iprintf(buffer, "Sample rates: ");
1123 if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
1124 snd_iprintf(buffer, "All\n");
1126 snd_iprintf(buffer, "%s%s%s%s\n",
1127 ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
1128 ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
1129 ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
1130 ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
1132 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
1133 snd_iprintf(buffer, "QSound decoder %sabled\n",
1134 p->q_enabled ? "en" : "dis");
1136 snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
1138 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
1139 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
1140 ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
1141 ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
1142 ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
1143 ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
1146 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
1147 snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1149 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
1150 snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
1151 ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
1152 ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
1154 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1155 snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
1156 p->qpos_left, p->qpos_right);
1162 EXPORT_SYMBOL(snd_sb_csp_new);
1168 static int __init alsa_sb_csp_init(void)
1173 static void __exit alsa_sb_csp_exit(void)
1177 module_init(alsa_sb_csp_init)
1178 module_exit(alsa_sb_csp_exit)