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");
39 MODULE_FIRMWARE("sb16/mulaw_main.csp");
40 MODULE_FIRMWARE("sb16/alaw_main.csp");
41 MODULE_FIRMWARE("sb16/ima_adpcm_init.csp");
42 MODULE_FIRMWARE("sb16/ima_adpcm_playback.csp");
43 MODULE_FIRMWARE("sb16/ima_adpcm_capture.csp");
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')
68 struct riff_header info
;
72 __u16 flags_16bit_8bit
;
73 __u16 flags_stereo_mono
;
80 static void snd_sb_csp_free(struct snd_hwdep
*hw
);
81 static int snd_sb_csp_open(struct snd_hwdep
* hw
, struct file
*file
);
82 static int snd_sb_csp_ioctl(struct snd_hwdep
* hw
, struct file
*file
, unsigned int cmd
, unsigned long arg
);
83 static int snd_sb_csp_release(struct snd_hwdep
* hw
, struct file
*file
);
85 static int csp_detect(struct snd_sb
*chip
, int *version
);
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
);
90 static int get_version(struct snd_sb
*chip
);
92 static int snd_sb_csp_riff_load(struct snd_sb_csp
* p
,
93 struct snd_sb_csp_microcode __user
* code
);
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
);
101 static int snd_sb_csp_start(struct snd_sb_csp
* p
, int sample_width
, int channels
);
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
);
111 static void info_read(struct snd_info_entry
*entry
, struct snd_info_buffer
*buffer
);
114 * Detect CSP chip and create a new instance
116 int snd_sb_csp_new(struct snd_sb
*chip
, int device
, struct snd_hwdep
** rhwdep
)
118 struct snd_sb_csp
*p
;
119 int uninitialized_var(version
);
121 struct snd_hwdep
*hw
;
126 if (csp_detect(chip
, &version
))
129 if ((err
= snd_hwdep_new(chip
->card
, "SB16-CSP", device
, &hw
)) < 0)
132 if ((p
= kzalloc(sizeof(*p
), GFP_KERNEL
)) == NULL
) {
133 snd_device_free(chip
->card
, hw
);
137 p
->version
= 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
;
143 p
->ops
.csp_start
= snd_sb_csp_start
;
144 p
->ops
.csp_stop
= snd_sb_csp_stop
;
145 p
->ops
.csp_qsound_transfer
= snd_sb_csp_qsound_transfer
;
147 mutex_init(&p
->access_mutex
);
148 sprintf(hw
->name
, "CSP v%d.%d", (version
>> 4), (version
& 0x0f));
149 hw
->iface
= SNDRV_HWDEP_IFACE_SB16CSP
;
150 hw
->private_data
= p
;
151 hw
->private_free
= snd_sb_csp_free
;
153 /* operators - only write/ioctl */
154 hw
->ops
.open
= snd_sb_csp_open
;
155 hw
->ops
.ioctl
= snd_sb_csp_ioctl
;
156 hw
->ops
.release
= snd_sb_csp_release
;
158 /* create a proc entry */
159 init_proc_entry(p
, device
);
166 * free_private for hwdep instance
168 static void snd_sb_csp_free(struct snd_hwdep
*hwdep
)
171 struct snd_sb_csp
*p
= hwdep
->private_data
;
173 if (p
->running
& SNDRV_SB_CSP_ST_RUNNING
)
175 for (i
= 0; i
< ARRAY_SIZE(p
->csp_programs
); ++i
)
176 release_firmware(p
->csp_programs
[i
]);
181 /* ------------------------------ */
184 * open the device exclusively
186 static int snd_sb_csp_open(struct snd_hwdep
* hw
, struct file
*file
)
188 struct snd_sb_csp
*p
= hw
->private_data
;
189 return (snd_sb_csp_use(p
));
193 * ioctl for hwdep device:
195 static int snd_sb_csp_ioctl(struct snd_hwdep
* hw
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
197 struct snd_sb_csp
*p
= hw
->private_data
;
198 struct snd_sb_csp_info info
;
199 struct snd_sb_csp_start start_info
;
205 if (snd_sb_csp_check_version(p
))
209 /* get information */
210 case SNDRV_SB_CSP_IOCTL_INFO
:
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
;
222 if (copy_to_user((void __user
*)arg
, &info
, sizeof(info
)))
228 /* load CSP microcode */
229 case SNDRV_SB_CSP_IOCTL_LOAD_CODE
:
230 err
= (p
->running
& SNDRV_SB_CSP_ST_RUNNING
?
231 -EBUSY
: snd_sb_csp_riff_load(p
, (struct snd_sb_csp_microcode __user
*) arg
));
233 case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE
:
234 err
= (p
->running
& SNDRV_SB_CSP_ST_RUNNING
?
235 -EBUSY
: snd_sb_csp_unload(p
));
238 /* change CSP running state */
239 case SNDRV_SB_CSP_IOCTL_START
:
240 if (copy_from_user(&start_info
, (void __user
*) arg
, sizeof(start_info
)))
243 err
= snd_sb_csp_start(p
, start_info
.sample_width
, start_info
.channels
);
245 case SNDRV_SB_CSP_IOCTL_STOP
:
246 err
= snd_sb_csp_stop(p
);
248 case SNDRV_SB_CSP_IOCTL_PAUSE
:
249 err
= snd_sb_csp_pause(p
);
251 case SNDRV_SB_CSP_IOCTL_RESTART
:
252 err
= snd_sb_csp_restart(p
);
265 static int snd_sb_csp_release(struct snd_hwdep
* hw
, struct file
*file
)
267 struct snd_sb_csp
*p
= hw
->private_data
;
268 return (snd_sb_csp_unuse(p
));
271 /* ------------------------------ */
276 static int snd_sb_csp_use(struct snd_sb_csp
* p
)
278 mutex_lock(&p
->access_mutex
);
280 mutex_unlock(&p
->access_mutex
);
284 mutex_unlock(&p
->access_mutex
);
293 static int snd_sb_csp_unuse(struct snd_sb_csp
* p
)
295 mutex_lock(&p
->access_mutex
);
297 mutex_unlock(&p
->access_mutex
);
303 * load microcode via ioctl:
304 * code is user-space pointer
306 static int snd_sb_csp_riff_load(struct snd_sb_csp
* p
,
307 struct snd_sb_csp_microcode __user
* mcode
)
309 struct snd_sb_csp_mc_header info
;
311 unsigned char __user
*data_ptr
;
312 unsigned char __user
*data_end
;
313 unsigned short func_nr
= 0;
315 struct riff_header file_h
, item_h
, code_h
;
317 struct desc_header funcdesc_h
;
322 if (copy_from_user(&info
, mcode
, sizeof(info
)))
324 data_ptr
= mcode
->data
;
326 if (copy_from_user(&file_h
, data_ptr
, sizeof(file_h
)))
328 if ((file_h
.name
!= RIFF_HEADER
) ||
329 (le32_to_cpu(file_h
.len
) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE
- sizeof(file_h
))) {
330 snd_printd("%s: Invalid RIFF header\n", __func__
);
333 data_ptr
+= sizeof(file_h
);
334 data_end
= data_ptr
+ le32_to_cpu(file_h
.len
);
336 if (copy_from_user(&item_type
, data_ptr
, sizeof(item_type
)))
338 if (item_type
!= CSP__HEADER
) {
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
)) {
345 if (copy_from_user(&item_h
, data_ptr
, sizeof(item_h
)))
347 data_ptr
+= sizeof(item_h
);
348 if (item_h
.name
!= LIST_HEADER
)
351 if (copy_from_user(&item_type
, data_ptr
, sizeof(item_type
)))
355 if (copy_from_user(&funcdesc_h
, data_ptr
+ sizeof(item_type
), sizeof(funcdesc_h
)))
357 func_nr
= le16_to_cpu(funcdesc_h
.func_nr
);
360 if (func_nr
!= info
.func_req
)
361 break; /* not required function, try next */
362 data_ptr
+= sizeof(item_type
);
364 /* destroy QSound mixer element */
365 if (p
->mode
== SNDRV_SB_CSP_MODE_QSOUND
) {
366 snd_sb_qsound_destroy(p
);
368 /* Clear all flags */
372 /* load microcode blocks */
374 if (data_ptr
>= data_end
)
376 if (copy_from_user(&code_h
, data_ptr
, sizeof(code_h
)))
379 /* init microcode blocks */
380 if (code_h
.name
!= INIT_HEADER
)
382 data_ptr
+= sizeof(code_h
);
383 err
= snd_sb_csp_load_user(p
, data_ptr
, le32_to_cpu(code_h
.len
),
384 SNDRV_SB_CSP_LOAD_INITBLOCK
);
387 data_ptr
+= le32_to_cpu(code_h
.len
);
389 /* main microcode block */
390 if (copy_from_user(&code_h
, data_ptr
, sizeof(code_h
)))
393 if (code_h
.name
!= MAIN_HEADER
) {
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
,
399 le32_to_cpu(code_h
.len
), 0);
403 /* fill in codec header */
404 strlcpy(p
->codec_name
, info
.codec_name
, sizeof(p
->codec_name
));
405 p
->func_nr
= func_nr
;
406 p
->mode
= le16_to_cpu(funcdesc_h
.flags_play_rec
);
407 switch (le16_to_cpu(funcdesc_h
.VOC_type
)) {
408 case 0x0001: /* QSound decoder */
409 if (le16_to_cpu(funcdesc_h
.flags_play_rec
) == SNDRV_SB_CSP_MODE_DSP_WRITE
) {
410 if (snd_sb_qsound_build(p
) == 0)
411 /* set QSound flag and clear all other mode flags */
412 p
->mode
= SNDRV_SB_CSP_MODE_QSOUND
;
416 case 0x0006: /* A Law codec */
417 p
->acc_format
= SNDRV_PCM_FMTBIT_A_LAW
;
419 case 0x0007: /* Mu Law codec */
420 p
->acc_format
= SNDRV_PCM_FMTBIT_MU_LAW
;
422 case 0x0011: /* what Creative thinks is IMA ADPCM codec */
423 case 0x0200: /* Creative ADPCM codec */
424 p
->acc_format
= SNDRV_PCM_FMTBIT_IMA_ADPCM
;
426 case 201: /* Text 2 Speech decoder */
427 /* TODO: Text2Speech handling routines */
430 case 0x0202: /* Fast Speech 8 codec */
431 case 0x0203: /* Fast Speech 10 codec */
432 p
->acc_format
= SNDRV_PCM_FMTBIT_SPECIAL
;
434 default: /* other codecs are unsupported */
435 p
->acc_format
= p
->acc_width
= p
->acc_rates
= 0;
437 snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
439 le16_to_cpu(funcdesc_h
.VOC_type
));
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
);
446 /* Decouple CSP from IRQ and DMAREQ lines */
447 spin_lock_irqsave(&p
->chip
->reg_lock
, flags
);
448 set_mode_register(p
->chip
, 0xfc);
449 set_mode_register(p
->chip
, 0x00);
450 spin_unlock_irqrestore(&p
->chip
->reg_lock
, flags
);
452 /* finished loading successfully */
453 p
->running
= SNDRV_SB_CSP_ST_LOADED
; /* set LOADED flag */
457 snd_printd("%s: Function #%d not found\n", __func__
, info
.func_req
);
462 * unload CSP microcode
464 static int snd_sb_csp_unload(struct snd_sb_csp
* p
)
466 if (p
->running
& SNDRV_SB_CSP_ST_RUNNING
)
468 if (!(p
->running
& SNDRV_SB_CSP_ST_LOADED
))
471 /* clear supported formats */
473 p
->acc_channels
= p
->acc_width
= p
->acc_rates
= 0;
474 /* destroy QSound mixer element */
475 if (p
->mode
== SNDRV_SB_CSP_MODE_QSOUND
) {
476 snd_sb_qsound_destroy(p
);
478 /* clear all flags */
485 * send command sequence to DSP
487 static inline int command_seq(struct snd_sb
*chip
, const unsigned char *seq
, int size
)
490 for (i
= 0; i
< size
; i
++) {
491 if (!snd_sbdsp_command(chip
, seq
[i
]))
498 * set CSP codec parameter
500 static int set_codec_parameter(struct snd_sb
*chip
, unsigned char par
, unsigned char val
)
502 unsigned char dsp_cmd
[3];
504 dsp_cmd
[0] = 0x05; /* CSP set codec parameter */
505 dsp_cmd
[1] = val
; /* Parameter value */
506 dsp_cmd
[2] = par
; /* Parameter */
507 command_seq(chip
, dsp_cmd
, 3);
508 snd_sbdsp_command(chip
, 0x03); /* DSP read? */
509 if (snd_sbdsp_get_byte(chip
) != par
)
517 static int set_register(struct snd_sb
*chip
, unsigned char reg
, unsigned char val
)
519 unsigned char dsp_cmd
[3];
521 dsp_cmd
[0] = 0x0e; /* CSP set register */
522 dsp_cmd
[1] = reg
; /* CSP Register */
523 dsp_cmd
[2] = val
; /* value */
524 return command_seq(chip
, dsp_cmd
, 3);
529 * return < 0 -> error
531 static int read_register(struct snd_sb
*chip
, unsigned char reg
)
533 unsigned char dsp_cmd
[2];
535 dsp_cmd
[0] = 0x0f; /* CSP read register */
536 dsp_cmd
[1] = reg
; /* CSP Register */
537 command_seq(chip
, dsp_cmd
, 2);
538 return snd_sbdsp_get_byte(chip
); /* Read DSP value */
542 * set CSP mode register
544 static int set_mode_register(struct snd_sb
*chip
, unsigned char mode
)
546 unsigned char dsp_cmd
[2];
548 dsp_cmd
[0] = 0x04; /* CSP set mode register */
549 dsp_cmd
[1] = mode
; /* mode */
550 return command_seq(chip
, dsp_cmd
, 2);
555 * return 0 if CSP exists.
557 static int csp_detect(struct snd_sb
*chip
, int *version
)
559 unsigned char csp_test1
, csp_test2
;
561 int result
= -ENODEV
;
563 spin_lock_irqsave(&chip
->reg_lock
, flags
);
565 set_codec_parameter(chip
, 0x00, 0x00);
566 set_mode_register(chip
, 0xfc); /* 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); /* 0x00 = ? */
581 *version
= get_version(chip
);
582 snd_sbdsp_reset(chip
); /* reset DSP after getversion! */
583 if (*version
>= 0x10 && *version
<= 0x1f)
584 result
= 0; /* valid version id */
587 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
592 * get CSP version number
594 static int get_version(struct snd_sb
*chip
)
596 unsigned char dsp_cmd
[2];
598 dsp_cmd
[0] = 0x08; /* SB_DSP_!something! */
599 dsp_cmd
[1] = 0x03; /* get chip version id? */
600 command_seq(chip
, dsp_cmd
, 2);
602 return (snd_sbdsp_get_byte(chip
));
606 * check if the CSP version is valid
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
);
618 * download microcode to CSP (microcode should have one "main" block).
620 static int snd_sb_csp_load(struct snd_sb_csp
* p
, const unsigned char *buf
, int size
, int load_flags
)
627 spin_lock_irqsave(&p
->chip
->reg_lock
, flags
);
628 snd_sbdsp_command(p
->chip
, 0x01); /* CSP download command */
629 if (snd_sbdsp_get_byte(p
->chip
)) {
630 snd_printd("%s: Download command failed\n", __func__
);
633 /* Send CSP low byte (size - 1) */
634 snd_sbdsp_command(p
->chip
, (unsigned char)(size
- 1));
636 snd_sbdsp_command(p
->chip
, (unsigned char)((size
- 1) >> 8));
637 /* send microcode sequence */
638 /* load from kernel space */
640 if (!snd_sbdsp_command(p
->chip
, *buf
++))
643 if (snd_sbdsp_get_byte(p
->chip
))
646 if (load_flags
& SNDRV_SB_CSP_LOAD_INITBLOCK
) {
648 /* some codecs (FastSpeech) take some time to initialize */
650 snd_sbdsp_command(p
->chip
, 0x03);
651 status
= snd_sbdsp_get_byte(p
->chip
);
652 if (status
== 0x55 || ++i
>= 10)
656 if (status
!= 0x55) {
657 snd_printd("%s: Microcode initialization failed\n", __func__
);
662 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
663 * Start CSP chip if no 16bit DMA channel is set - some kind
664 * of autorun or perhaps a bugfix?
666 spin_lock(&p
->chip
->mixer_lock
);
667 status
= snd_sbmixer_read(p
->chip
, SB_DSP4_DMASETUP
);
668 spin_unlock(&p
->chip
->mixer_lock
);
669 if (!(status
& (SB_DMASETUP_DMA7
| SB_DMASETUP_DMA6
| SB_DMASETUP_DMA5
))) {
670 err
= (set_codec_parameter(p
->chip
, 0xaa, 0x00) ||
671 set_codec_parameter(p
->chip
, 0xff, 0x00));
672 snd_sbdsp_reset(p
->chip
); /* really! */
675 set_mode_register(p
->chip
, 0xc0); /* c0 = STOP */
676 set_mode_register(p
->chip
, 0x70); /* 70 = RUN */
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
)
691 kbuf
= memdup_user(buf
, size
);
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",
710 const struct firmware
*program
;
712 BUILD_BUG_ON(ARRAY_SIZE(names
) != CSP_PROGRAM_COUNT
);
713 program
= p
->csp_programs
[index
];
715 int err
= request_firmware(&program
, names
[index
],
719 p
->csp_programs
[index
] = program
;
721 return snd_sb_csp_load(p
, program
->data
, program
->size
, flags
);
725 * autoload hardware codec if necessary
726 * return 0 if CSP is loaded and ready to run (p->running != 0)
728 static int snd_sb_csp_autoload(struct snd_sb_csp
* p
, int pcm_sfmt
, int play_rec_mode
)
733 /* if CSP is running or manually loaded then exit */
734 if (p
->running
& (SNDRV_SB_CSP_ST_RUNNING
| SNDRV_SB_CSP_ST_LOADED
))
737 /* autoload microcode only if requested hardware codec is not already loaded */
738 if (((1 << pcm_sfmt
) & p
->acc_format
) && (play_rec_mode
& p
->mode
)) {
739 p
->running
= SNDRV_SB_CSP_ST_AUTO
;
742 case SNDRV_PCM_FORMAT_MU_LAW
:
743 err
= snd_sb_csp_firmware_load(p
, CSP_PROGRAM_MULAW
, 0);
744 p
->acc_format
= SNDRV_PCM_FMTBIT_MU_LAW
;
745 p
->mode
= SNDRV_SB_CSP_MODE_DSP_READ
| SNDRV_SB_CSP_MODE_DSP_WRITE
;
747 case SNDRV_PCM_FORMAT_A_LAW
:
748 err
= snd_sb_csp_firmware_load(p
, CSP_PROGRAM_ALAW
, 0);
749 p
->acc_format
= SNDRV_PCM_FMTBIT_A_LAW
;
750 p
->mode
= SNDRV_SB_CSP_MODE_DSP_READ
| SNDRV_SB_CSP_MODE_DSP_WRITE
;
752 case SNDRV_PCM_FORMAT_IMA_ADPCM
:
753 err
= snd_sb_csp_firmware_load(p
, CSP_PROGRAM_ADPCM_INIT
,
754 SNDRV_SB_CSP_LOAD_INITBLOCK
);
757 if (play_rec_mode
== SNDRV_SB_CSP_MODE_DSP_WRITE
) {
758 err
= snd_sb_csp_firmware_load
759 (p
, CSP_PROGRAM_ADPCM_PLAYBACK
, 0);
760 p
->mode
= SNDRV_SB_CSP_MODE_DSP_WRITE
;
762 err
= snd_sb_csp_firmware_load
763 (p
, CSP_PROGRAM_ADPCM_CAPTURE
, 0);
764 p
->mode
= SNDRV_SB_CSP_MODE_DSP_READ
;
766 p
->acc_format
= SNDRV_PCM_FMTBIT_IMA_ADPCM
;
769 /* Decouple CSP from IRQ and DMAREQ lines */
770 if (p
->running
& SNDRV_SB_CSP_ST_AUTO
) {
771 spin_lock_irqsave(&p
->chip
->reg_lock
, flags
);
772 set_mode_register(p
->chip
, 0xfc);
773 set_mode_register(p
->chip
, 0x00);
774 spin_unlock_irqrestore(&p
->chip
->reg_lock
, flags
);
775 p
->running
= 0; /* clear autoloaded flag */
781 p
->acc_channels
= p
->acc_width
= p
->acc_rates
= 0;
783 p
->running
= 0; /* clear autoloaded flag */
787 p
->running
= SNDRV_SB_CSP_ST_AUTO
; /* set autoloaded flag */
788 p
->acc_width
= SNDRV_SB_CSP_SAMPLE_16BIT
; /* only 16 bit data */
789 p
->acc_channels
= SNDRV_SB_CSP_MONO
| SNDRV_SB_CSP_STEREO
;
790 p
->acc_rates
= SNDRV_SB_CSP_RATE_ALL
; /* HW codecs accept all rates */
794 return (p
->running
& SNDRV_SB_CSP_ST_AUTO
) ? 0 : -ENXIO
;
800 static int snd_sb_csp_start(struct snd_sb_csp
* p
, int sample_width
, int channels
)
802 unsigned char s_type
; /* sample type */
803 unsigned char mixL
, mixR
;
807 if (!(p
->running
& (SNDRV_SB_CSP_ST_LOADED
| SNDRV_SB_CSP_ST_AUTO
))) {
808 snd_printd("%s: Microcode not loaded\n", __func__
);
811 if (p
->running
& SNDRV_SB_CSP_ST_RUNNING
) {
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__
);
824 /* Mute PCM volume */
825 spin_lock_irqsave(&p
->chip
->mixer_lock
, flags
);
826 mixL
= snd_sbmixer_read(p
->chip
, SB_DSP4_PCM_DEV
);
827 mixR
= snd_sbmixer_read(p
->chip
, SB_DSP4_PCM_DEV
+ 1);
828 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
, mixL
& 0x7);
829 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
+ 1, mixR
& 0x7);
831 spin_lock(&p
->chip
->reg_lock
);
832 set_mode_register(p
->chip
, 0xc0); /* c0 = STOP */
833 set_mode_register(p
->chip
, 0x70); /* 70 = RUN */
836 if (channels
== SNDRV_SB_CSP_MONO
)
837 s_type
= 0x11; /* 000n 000n (n = 1 if mono) */
838 if (sample_width
== SNDRV_SB_CSP_SAMPLE_8BIT
)
839 s_type
|= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
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
;
850 p
->run_channels
= channels
;
852 p
->running
|= SNDRV_SB_CSP_ST_RUNNING
;
854 if (p
->mode
& SNDRV_SB_CSP_MODE_QSOUND
) {
855 set_codec_parameter(p
->chip
, 0xe0, 0x01);
856 /* enable QSound decoder */
857 set_codec_parameter(p
->chip
, 0x00, 0xff);
858 set_codec_parameter(p
->chip
, 0x01, 0xff);
859 p
->running
|= SNDRV_SB_CSP_ST_QSOUND
;
860 /* set QSound startup value */
861 snd_sb_csp_qsound_transfer(p
);
866 spin_unlock(&p
->chip
->reg_lock
);
868 /* restore PCM volume */
869 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
, mixL
);
870 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
+ 1, mixR
);
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
;
885 if (!(p
->running
& SNDRV_SB_CSP_ST_RUNNING
))
888 /* Mute PCM volume */
889 spin_lock_irqsave(&p
->chip
->mixer_lock
, flags
);
890 mixL
= snd_sbmixer_read(p
->chip
, SB_DSP4_PCM_DEV
);
891 mixR
= snd_sbmixer_read(p
->chip
, SB_DSP4_PCM_DEV
+ 1);
892 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
, mixL
& 0x7);
893 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
+ 1, mixR
& 0x7);
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 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
, mixL
);
909 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
+ 1, mixR
);
910 spin_unlock_irqrestore(&p
->chip
->mixer_lock
, flags
);
913 p
->running
&= ~(SNDRV_SB_CSP_ST_PAUSED
| SNDRV_SB_CSP_ST_RUNNING
);
918 * pause CSP codec and hold DMA transfer
920 static int snd_sb_csp_pause(struct snd_sb_csp
* p
)
925 if (!(p
->running
& SNDRV_SB_CSP_ST_RUNNING
))
928 spin_lock_irqsave(&p
->chip
->reg_lock
, flags
);
929 result
= set_codec_parameter(p
->chip
, 0x80, 0xff);
930 spin_unlock_irqrestore(&p
->chip
->reg_lock
, flags
);
932 p
->running
|= SNDRV_SB_CSP_ST_PAUSED
;
938 * restart CSP codec and resume DMA transfer
940 static int snd_sb_csp_restart(struct snd_sb_csp
* p
)
945 if (!(p
->running
& SNDRV_SB_CSP_ST_PAUSED
))
948 spin_lock_irqsave(&p
->chip
->reg_lock
, flags
);
949 result
= set_codec_parameter(p
->chip
, 0x80, 0x00);
950 spin_unlock_irqrestore(&p
->chip
->reg_lock
, flags
);
952 p
->running
&= ~SNDRV_SB_CSP_ST_PAUSED
;
957 /* ------------------------------ */
960 * QSound mixer control for PCM
963 #define snd_sb_qsound_switch_info snd_ctl_boolean_mono_info
965 static int snd_sb_qsound_switch_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
967 struct snd_sb_csp
*p
= snd_kcontrol_chip(kcontrol
);
969 ucontrol
->value
.integer
.value
[0] = p
->q_enabled
? 1 : 0;
973 static int snd_sb_qsound_switch_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
975 struct snd_sb_csp
*p
= snd_kcontrol_chip(kcontrol
);
980 nval
= ucontrol
->value
.integer
.value
[0] & 0x01;
981 spin_lock_irqsave(&p
->q_lock
, flags
);
982 change
= p
->q_enabled
!= nval
;
984 spin_unlock_irqrestore(&p
->q_lock
, flags
);
988 static int snd_sb_qsound_space_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
990 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
992 uinfo
->value
.integer
.min
= 0;
993 uinfo
->value
.integer
.max
= SNDRV_SB_CSP_QSOUND_MAX_RIGHT
;
997 static int snd_sb_qsound_space_get(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
999 struct snd_sb_csp
*p
= snd_kcontrol_chip(kcontrol
);
1000 unsigned long flags
;
1002 spin_lock_irqsave(&p
->q_lock
, 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
);
1009 static int snd_sb_qsound_space_put(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
1011 struct snd_sb_csp
*p
= snd_kcontrol_chip(kcontrol
);
1012 unsigned long flags
;
1014 unsigned char nval1
, nval2
;
1016 nval1
= ucontrol
->value
.integer
.value
[0];
1017 if (nval1
> SNDRV_SB_CSP_QSOUND_MAX_RIGHT
)
1018 nval1
= SNDRV_SB_CSP_QSOUND_MAX_RIGHT
;
1019 nval2
= ucontrol
->value
.integer
.value
[1];
1020 if (nval2
> SNDRV_SB_CSP_QSOUND_MAX_RIGHT
)
1021 nval2
= SNDRV_SB_CSP_QSOUND_MAX_RIGHT
;
1022 spin_lock_irqsave(&p
->q_lock
, flags
);
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
);
1031 static struct snd_kcontrol_new snd_sb_qsound_switch
= {
1032 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1033 .name
= "3D Control - Switch",
1034 .info
= snd_sb_qsound_switch_info
,
1035 .get
= snd_sb_qsound_switch_get
,
1036 .put
= snd_sb_qsound_switch_put
1039 static struct snd_kcontrol_new snd_sb_qsound_space
= {
1040 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
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
)
1049 struct snd_card
*card
;
1055 card
= p
->chip
->card
;
1056 p
->qpos_left
= p
->qpos_right
= SNDRV_SB_CSP_QSOUND_MAX_RIGHT
/ 2;
1057 p
->qpos_changed
= 0;
1059 spin_lock_init(&p
->q_lock
);
1061 if ((err
= snd_ctl_add(card
, p
->qsound_switch
= snd_ctl_new1(&snd_sb_qsound_switch
, p
))) < 0)
1063 if ((err
= snd_ctl_add(card
, p
->qsound_space
= snd_ctl_new1(&snd_sb_qsound_space
, p
))) < 0)
1069 snd_sb_qsound_destroy(p
);
1073 static void snd_sb_qsound_destroy(struct snd_sb_csp
* p
)
1075 struct snd_card
*card
;
1076 unsigned long flags
;
1081 card
= p
->chip
->card
;
1083 down_write(&card
->controls_rwsem
);
1084 if (p
->qsound_switch
)
1085 snd_ctl_remove(card
, p
->qsound_switch
);
1086 if (p
->qsound_space
)
1087 snd_ctl_remove(card
, p
->qsound_space
);
1088 up_write(&card
->controls_rwsem
);
1090 /* cancel pending transfer of QSound parameters */
1091 spin_lock_irqsave (&p
->q_lock
, flags
);
1092 p
->qpos_changed
= 0;
1093 spin_unlock_irqrestore (&p
->q_lock
, flags
);
1097 * Transfer qsound parameters to CSP,
1098 * function should be called from interrupt routine
1100 static int snd_sb_csp_qsound_transfer(struct snd_sb_csp
* p
)
1104 spin_lock(&p
->q_lock
);
1105 if (p
->running
& SNDRV_SB_CSP_ST_QSOUND
) {
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
);
1120 /* ------------------------------ */
1125 static int init_proc_entry(struct snd_sb_csp
* p
, int device
)
1128 struct snd_info_entry
*entry
;
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
);
1135 static void info_read(struct snd_info_entry
*entry
, struct snd_info_buffer
*buffer
)
1137 struct snd_sb_csp
*p
= entry
->private_data
;
1139 snd_iprintf(buffer
, "Creative Signal Processor [v%d.%d]\n", (p
->version
>> 4), (p
->version
& 0x0f));
1140 snd_iprintf(buffer
, "State: %cx%c%c%c\n", ((p
->running
& SNDRV_SB_CSP_ST_QSOUND
) ? 'Q' : '-'),
1141 ((p
->running
& SNDRV_SB_CSP_ST_PAUSED
) ? 'P' : '-'),
1142 ((p
->running
& SNDRV_SB_CSP_ST_RUNNING
) ? 'R' : '-'),
1143 ((p
->running
& SNDRV_SB_CSP_ST_LOADED
) ? 'L' : '-'));
1144 if (p
->running
& SNDRV_SB_CSP_ST_LOADED
) {
1145 snd_iprintf(buffer
, "Codec: %s [func #%d]\n", p
->codec_name
, p
->func_nr
);
1146 snd_iprintf(buffer
, "Sample rates: ");
1147 if (p
->acc_rates
== SNDRV_SB_CSP_RATE_ALL
) {
1148 snd_iprintf(buffer
, "All\n");
1150 snd_iprintf(buffer
, "%s%s%s%s\n",
1151 ((p
->acc_rates
& SNDRV_SB_CSP_RATE_8000
) ? "8000Hz " : ""),
1152 ((p
->acc_rates
& SNDRV_SB_CSP_RATE_11025
) ? "11025Hz " : ""),
1153 ((p
->acc_rates
& SNDRV_SB_CSP_RATE_22050
) ? "22050Hz " : ""),
1154 ((p
->acc_rates
& SNDRV_SB_CSP_RATE_44100
) ? "44100Hz" : ""));
1156 if (p
->mode
== SNDRV_SB_CSP_MODE_QSOUND
) {
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",
1162 ((p
->acc_width
& SNDRV_SB_CSP_SAMPLE_16BIT
) ? "16bit" : "-"),
1163 ((p
->acc_width
& SNDRV_SB_CSP_SAMPLE_8BIT
) ? "8bit" : "-"),
1164 ((p
->acc_channels
& SNDRV_SB_CSP_MONO
) ? "mono" : "-"),
1165 ((p
->acc_channels
& SNDRV_SB_CSP_STEREO
) ? "stereo" : "-"),
1166 ((p
->mode
& SNDRV_SB_CSP_MODE_DSP_WRITE
) ? "playback" : "-"),
1167 ((p
->mode
& SNDRV_SB_CSP_MODE_DSP_READ
) ? "capture" : "-"));
1170 if (p
->running
& SNDRV_SB_CSP_ST_AUTO
) {
1171 snd_iprintf(buffer
, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1173 if (p
->running
& SNDRV_SB_CSP_ST_RUNNING
) {
1174 snd_iprintf(buffer
, "Processing %dbit %s PCM samples\n",
1175 ((p
->run_width
& SNDRV_SB_CSP_SAMPLE_16BIT
) ? 16 : 8),
1176 ((p
->run_channels
& SNDRV_SB_CSP_MONO
) ? "mono" : "stereo"));
1178 if (p
->running
& SNDRV_SB_CSP_ST_QSOUND
) {
1179 snd_iprintf(buffer
, "Qsound position: left = 0x%x, right = 0x%x\n",
1180 p
->qpos_left
, p
->qpos_right
);
1186 EXPORT_SYMBOL(snd_sb_csp_new
);
1192 static int __init
alsa_sb_csp_init(void)
1197 static void __exit
alsa_sb_csp_exit(void)
1201 module_init(alsa_sb_csp_init
)
1202 module_exit(alsa_sb_csp_exit
)