28 #include <linux/slab.h>
29 #include <linux/module.h>
45 #ifdef SNDRV_LITTLE_ENDIAN
46 #define CSP_HDR_VALUE(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
48 #define CSP_HDR_VALUE(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
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')
86 static int set_codec_parameter(
struct snd_sb *
chip,
unsigned char par,
unsigned char val);
87 static int set_register(
struct snd_sb *
chip,
unsigned char reg,
unsigned char val);
88 static int read_register(
struct snd_sb *
chip,
unsigned char reg);
89 static int set_mode_register(
struct snd_sb *
chip,
unsigned char mode);
92 static int snd_sb_csp_riff_load(
struct snd_sb_csp *
p,
94 static int snd_sb_csp_unload(
struct snd_sb_csp *
p);
95 static int snd_sb_csp_load_user(
struct snd_sb_csp *
p,
const unsigned char __user *
buf,
int size,
int load_flags);
96 static int snd_sb_csp_autoload(
struct snd_sb_csp *
p,
int pcm_sfmt,
int play_rec_mode);
97 static int snd_sb_csp_check_version(
struct snd_sb_csp *
p);
99 static int snd_sb_csp_use(
struct snd_sb_csp *
p);
100 static int snd_sb_csp_unuse(
struct snd_sb_csp *
p);
102 static int snd_sb_csp_stop(
struct snd_sb_csp *
p);
103 static int snd_sb_csp_pause(
struct snd_sb_csp *
p);
104 static int snd_sb_csp_restart(
struct snd_sb_csp *
p);
106 static int snd_sb_qsound_build(
struct snd_sb_csp *
p);
107 static void snd_sb_qsound_destroy(
struct snd_sb_csp *
p);
108 static int snd_sb_csp_qsound_transfer(
struct snd_sb_csp *
p);
110 static int init_proc_entry(
struct snd_sb_csp *
p,
int device);
118 struct snd_sb_csp *
p;
126 if (csp_detect(chip, &
version))
140 p->ops.csp_use = snd_sb_csp_use;
141 p->ops.csp_unuse = snd_sb_csp_unuse;
142 p->ops.csp_autoload = snd_sb_csp_autoload;
144 p->ops.csp_stop = snd_sb_csp_stop;
145 p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;
154 hw->
ops.open = snd_sb_csp_open;
155 hw->
ops.ioctl = snd_sb_csp_ioctl;
156 hw->
ops.release = snd_sb_csp_release;
159 init_proc_entry(p, device);
168 static void snd_sb_csp_free(
struct snd_hwdep *hwdep)
189 return (snd_sb_csp_use(p));
195 static int snd_sb_csp_ioctl(
struct snd_hwdep * hw,
struct file *file,
unsigned int cmd,
unsigned long arg)
205 if (snd_sb_csp_check_version(p))
211 *
info.codec_name = *p->codec_name;
212 info.func_nr = p->func_nr;
213 info.acc_format = p->acc_format;
214 info.acc_channels = p->acc_channels;
215 info.acc_width = p->acc_width;
216 info.acc_rates = p->acc_rates;
217 info.csp_mode = p->mode;
218 info.run_channels = p->run_channels;
219 info.run_width = p->run_width;
220 info.version = p->version;
221 info.state = p->running;
235 -
EBUSY : snd_sb_csp_unload(p));
246 err = snd_sb_csp_stop(p);
249 err = snd_sb_csp_pause(p);
252 err = snd_sb_csp_restart(p);
265 static int snd_sb_csp_release(
struct snd_hwdep * hw,
struct file *file)
268 return (snd_sb_csp_unuse(p));
276 static int snd_sb_csp_use(
struct snd_sb_csp * p)
293 static int snd_sb_csp_unuse(
struct snd_sb_csp * p)
306 static int snd_sb_csp_riff_load(
struct snd_sb_csp * p,
311 unsigned char __user *data_ptr;
312 unsigned char __user *data_end;
313 unsigned short func_nr = 0;
324 data_ptr = mcode->data;
330 snd_printd(
"%s: Invalid RIFF header\n", __func__);
333 data_ptr +=
sizeof(file_h);
339 snd_printd(
"%s: Invalid RIFF file type\n", __func__);
342 data_ptr +=
sizeof (item_type);
344 for (; data_ptr < data_end; data_ptr +=
le32_to_cpu(item_h.len)) {
347 data_ptr +=
sizeof(item_h);
355 if (
copy_from_user(&funcdesc_h, data_ptr +
sizeof(item_type),
sizeof(funcdesc_h)))
360 if (func_nr !=
info.func_req)
362 data_ptr +=
sizeof(item_type);
366 snd_sb_qsound_destroy(p);
374 if (data_ptr >= data_end)
382 data_ptr +=
sizeof(code_h);
383 err = snd_sb_csp_load_user(p, data_ptr,
le32_to_cpu(code_h.len),
394 snd_printd(
"%s: Missing 'main' microcode\n", __func__);
397 data_ptr +=
sizeof(code_h);
398 err = snd_sb_csp_load_user(p, data_ptr,
404 strlcpy(p->codec_name,
info.codec_name,
sizeof(p->codec_name));
410 if (snd_sb_qsound_build(p) == 0)
435 p->acc_format = p->acc_width = p->acc_rates = 0;
437 snd_printd(
"%s: Unsupported CSP codec type: 0x%04x\n",
442 p->acc_channels =
le16_to_cpu(funcdesc_h.flags_stereo_mono);
443 p->acc_width =
le16_to_cpu(funcdesc_h.flags_16bit_8bit);
444 p->acc_rates =
le16_to_cpu(funcdesc_h.flags_rates);
448 set_mode_register(p->chip, 0xfc);
449 set_mode_register(p->chip, 0x00);
450 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
457 snd_printd(
"%s: Function #%d not found\n", __func__,
info.func_req);
464 static int snd_sb_csp_unload(
struct snd_sb_csp * p)
473 p->acc_channels = p->acc_width = p->acc_rates = 0;
476 snd_sb_qsound_destroy(p);
487 static inline int command_seq(
struct snd_sb *
chip,
const unsigned char *seq,
int size)
490 for (i = 0; i <
size; i++) {
500 static int set_codec_parameter(
struct snd_sb *chip,
unsigned char par,
unsigned char val)
507 command_seq(chip, dsp_cmd, 3);
517 static int set_register(
struct snd_sb *chip,
unsigned char reg,
unsigned char val)
519 unsigned char dsp_cmd[3];
524 return command_seq(chip, dsp_cmd, 3);
531 static int read_register(
struct snd_sb *chip,
unsigned char reg)
533 unsigned char dsp_cmd[2];
537 command_seq(chip, dsp_cmd, 2);
544 static int set_mode_register(
struct snd_sb *chip,
unsigned char mode)
546 unsigned char dsp_cmd[2];
550 return command_seq(chip, dsp_cmd, 2);
559 unsigned char csp_test1, csp_test2;
565 set_codec_parameter(chip, 0x00, 0x00);
566 set_mode_register(chip, 0xfc);
568 csp_test1 = read_register(chip, 0x83);
569 set_register(chip, 0x83, ~csp_test1);
570 csp_test2 = read_register(chip, 0x83);
571 if (csp_test2 != (csp_test1 ^ 0xff))
574 set_register(chip, 0x83, csp_test1);
575 csp_test2 = read_register(chip, 0x83);
576 if (csp_test2 != csp_test1)
579 set_mode_register(chip, 0x00);
581 *version = get_version(chip);
583 if (*version >= 0x10 && *version <= 0x1f)
587 spin_unlock_irqrestore(&chip->
reg_lock, flags);
594 static int get_version(
struct snd_sb *chip)
596 unsigned char dsp_cmd[2];
600 command_seq(chip, dsp_cmd, 2);
608 static int snd_sb_csp_check_version(
struct snd_sb_csp * p)
610 if (p->version < 0x10 || p->version > 0x1f) {
611 snd_printd(
"%s: Invalid CSP version: 0x%x\n", __func__, p->version);
620 static int snd_sb_csp_load(
struct snd_sb_csp * p,
const unsigned char *
buf,
int size,
int load_flags)
630 snd_printd(
"%s: Download command failed\n", __func__);
652 if (status == 0x55 || ++i >= 10)
656 if (status != 0x55) {
657 snd_printd(
"%s: Microcode initialization failed\n", __func__);
666 spin_lock(&p->chip->mixer_lock);
668 spin_unlock(&p->chip->mixer_lock);
670 err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
671 set_codec_parameter(p->chip, 0xff, 0x00));
675 set_mode_register(p->chip, 0xc0);
676 set_mode_register(p->chip, 0x70);
682 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
686 static int snd_sb_csp_load_user(
struct snd_sb_csp * p,
const unsigned char __user *buf,
int size,
int load_flags)
693 return PTR_ERR(kbuf);
695 err = snd_sb_csp_load(p, kbuf, size, load_flags);
701 static int snd_sb_csp_firmware_load(
struct snd_sb_csp *p,
int index,
int flags)
703 static const char *
const names[] = {
704 "sb16/mulaw_main.csp",
705 "sb16/alaw_main.csp",
706 "sb16/ima_adpcm_init.csp",
707 "sb16/ima_adpcm_playback.csp",
708 "sb16/ima_adpcm_capture.csp",
713 program = p->csp_programs[
index];
719 p->csp_programs[
index] = program;
721 return snd_sb_csp_load(p, program->
data, program->
size, flags);
728 static int snd_sb_csp_autoload(
struct snd_sb_csp * p,
int pcm_sfmt,
int play_rec_mode)
738 if (((1 << pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
743 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_MULAW, 0);
748 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ALAW, 0);
753 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ADPCM_INIT,
754 SNDRV_SB_CSP_LOAD_INITBLOCK);
758 err = snd_sb_csp_firmware_load
759 (p, CSP_PROGRAM_ADPCM_PLAYBACK, 0);
762 err = snd_sb_csp_firmware_load
763 (p, CSP_PROGRAM_ADPCM_CAPTURE, 0);
772 set_mode_register(p->chip, 0xfc);
773 set_mode_register(p->chip, 0x00);
774 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
781 p->acc_channels = p->acc_width = p->acc_rates = 0;
802 unsigned char s_type;
803 unsigned char mixL, mixR;
808 snd_printd(
"%s: Microcode not loaded\n", __func__);
812 snd_printd(
"%s: CSP already running\n", __func__);
815 if (!(sample_width & p->acc_width)) {
816 snd_printd(
"%s: Unsupported PCM sample width\n", __func__);
819 if (!(channels & p->acc_channels)) {
820 snd_printd(
"%s: Invalid number of channels\n", __func__);
831 spin_lock(&p->chip->reg_lock);
832 set_mode_register(p->chip, 0xc0);
833 set_mode_register(p->chip, 0x70);
841 if (set_codec_parameter(p->chip, 0x81, s_type)) {
842 snd_printd(
"%s: Set sample type command failed\n", __func__);
845 if (set_codec_parameter(p->chip, 0x80, 0x00)) {
846 snd_printd(
"%s: Codec start command failed\n", __func__);
849 p->run_width = sample_width;
855 set_codec_parameter(p->chip, 0xe0, 0x01);
857 set_codec_parameter(p->chip, 0x00, 0xff);
858 set_codec_parameter(p->chip, 0x01, 0xff);
861 snd_sb_csp_qsound_transfer(p);
866 spin_unlock(&p->chip->reg_lock);
871 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
879 static int snd_sb_csp_stop(
struct snd_sb_csp * p)
882 unsigned char mixL, mixR;
895 spin_lock(&p->chip->reg_lock);
897 set_codec_parameter(p->chip, 0xe0, 0x01);
899 set_codec_parameter(p->chip, 0x00, 0x00);
900 set_codec_parameter(p->chip, 0x01, 0x00);
904 result = set_mode_register(p->chip, 0xc0);
905 spin_unlock(&p->chip->reg_lock);
910 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
920 static int snd_sb_csp_pause(
struct snd_sb_csp * p)
929 result = set_codec_parameter(p->chip, 0x80, 0xff);
930 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
940 static int snd_sb_csp_restart(
struct snd_sb_csp * p)
949 result = set_codec_parameter(p->chip, 0x80, 0x00);
950 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
963 #define snd_sb_qsound_switch_info snd_ctl_boolean_mono_info
969 ucontrol->
value.integer.value[0] = p->q_enabled ? 1 : 0;
980 nval = ucontrol->
value.integer.value[0] & 0x01;
982 change = p->q_enabled != nval;
984 spin_unlock_irqrestore(&p->q_lock, flags);
1000 unsigned long flags;
1003 ucontrol->
value.integer.value[0] = p->qpos_left;
1004 ucontrol->
value.integer.value[1] = p->qpos_right;
1005 spin_unlock_irqrestore(&p->q_lock, flags);
1012 unsigned long flags;
1014 unsigned char nval1, nval2;
1016 nval1 = ucontrol->
value.integer.value[0];
1019 nval2 = ucontrol->
value.integer.value[1];
1023 change = p->qpos_left != nval1 || p->qpos_right != nval2;
1024 p->qpos_left = nval1;
1025 p->qpos_right = nval2;
1026 p->qpos_changed = change;
1027 spin_unlock_irqrestore(&p->q_lock, flags);
1033 .name =
"3D Control - Switch",
1035 .get = snd_sb_qsound_switch_get,
1036 .put = snd_sb_qsound_switch_put
1041 .name =
"3D Control - Space",
1042 .info = snd_sb_qsound_space_info,
1043 .get = snd_sb_qsound_space_get,
1044 .put = snd_sb_qsound_space_put
1047 static int snd_sb_qsound_build(
struct snd_sb_csp * p)
1055 card = p->chip->card;
1057 p->qpos_changed = 0;
1069 snd_sb_qsound_destroy(p);
1073 static void snd_sb_qsound_destroy(
struct snd_sb_csp * p)
1076 unsigned long flags;
1081 card = p->chip->card;
1084 if (p->qsound_switch)
1086 if (p->qsound_space)
1092 p->qpos_changed = 0;
1093 spin_unlock_irqrestore (&p->q_lock, flags);
1100 static int snd_sb_csp_qsound_transfer(
struct snd_sb_csp * p)
1104 spin_lock(&p->q_lock);
1106 set_codec_parameter(p->chip, 0xe0, 0x01);
1108 set_codec_parameter(p->chip, 0x00, p->qpos_left);
1109 set_codec_parameter(p->chip, 0x02, 0x00);
1111 set_codec_parameter(p->chip, 0x00, p->qpos_right);
1112 set_codec_parameter(p->chip, 0x03, 0x00);
1115 p->qpos_changed = 0;
1116 spin_unlock(&p->q_lock);
1125 static int init_proc_entry(
struct snd_sb_csp * p,
int device)
1129 sprintf(name,
"cspD%d", device);
1130 if (! snd_card_proc_new(p->chip->card, name, &entry))
1131 snd_info_set_text_ops(entry, p, info_read);
1139 snd_iprintf(buffer,
"Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
1145 snd_iprintf(buffer,
"Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
1146 snd_iprintf(buffer,
"Sample rates: ");
1148 snd_iprintf(buffer,
"All\n");
1150 snd_iprintf(buffer,
"%s%s%s%s\n",
1157 snd_iprintf(buffer,
"QSound decoder %sabled\n",
1158 p->q_enabled ?
"en" :
"dis");
1160 snd_iprintf(buffer,
"PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
1171 snd_iprintf(buffer,
"Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1174 snd_iprintf(buffer,
"Processing %dbit %s PCM samples\n",
1179 snd_iprintf(buffer,
"Qsound position: left = 0x%x, right = 0x%x\n",
1180 p->qpos_left, p->qpos_right);
1192 static int __init alsa_sb_csp_init(
void)
1197 static void __exit alsa_sb_csp_exit(
void)