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))
45 #define LE_SHORT(v) le16_to_cpu(v)
46 #define LE_INT(v) le32_to_cpu(v)
48 #define RIFF_HEADER CSP_HDR_VALUE('R', 'I', 'F', 'F')
49 #define CSP__HEADER CSP_HDR_VALUE('C', 'S', 'P', ' ')
50 #define LIST_HEADER CSP_HDR_VALUE('L', 'I', 'S', 'T')
51 #define FUNC_HEADER CSP_HDR_VALUE('f', 'u', 'n', 'c')
52 #define CODE_HEADER CSP_HDR_VALUE('c', 'o', 'd', 'e')
53 #define INIT_HEADER CSP_HDR_VALUE('i', 'n', 'i', 't')
54 #define MAIN_HEADER CSP_HDR_VALUE('m', 'a', 'i', 'n')
59 typedef struct riff_header
{
64 typedef struct desc_header
{
69 __u16 flags_16bit_8bit
;
70 __u16 flags_stereo_mono
;
77 static void snd_sb_csp_free(snd_hwdep_t
*hw
);
78 static int snd_sb_csp_open(snd_hwdep_t
* hw
, struct file
*file
);
79 static int snd_sb_csp_ioctl(snd_hwdep_t
* hw
, struct file
*file
, unsigned int cmd
, unsigned long arg
);
80 static int snd_sb_csp_release(snd_hwdep_t
* hw
, struct file
*file
);
82 static int csp_detect(sb_t
*chip
, int *version
);
83 static int set_codec_parameter(sb_t
*chip
, unsigned char par
, unsigned char val
);
84 static int set_register(sb_t
*chip
, unsigned char reg
, unsigned char val
);
85 static int read_register(sb_t
*chip
, unsigned char reg
);
86 static int set_mode_register(sb_t
*chip
, unsigned char mode
);
87 static int get_version(sb_t
*chip
);
89 static int snd_sb_csp_riff_load(snd_sb_csp_t
* p
, snd_sb_csp_microcode_t __user
* code
);
90 static int snd_sb_csp_unload(snd_sb_csp_t
* p
);
91 static int snd_sb_csp_load_user(snd_sb_csp_t
* p
, const unsigned char __user
*buf
, int size
, int load_flags
);
92 static int snd_sb_csp_autoload(snd_sb_csp_t
* p
, int pcm_sfmt
, int play_rec_mode
);
93 static int snd_sb_csp_check_version(snd_sb_csp_t
* p
);
95 static int snd_sb_csp_use(snd_sb_csp_t
* p
);
96 static int snd_sb_csp_unuse(snd_sb_csp_t
* p
);
97 static int snd_sb_csp_start(snd_sb_csp_t
* p
, int sample_width
, int channels
);
98 static int snd_sb_csp_stop(snd_sb_csp_t
* p
);
99 static int snd_sb_csp_pause(snd_sb_csp_t
* p
);
100 static int snd_sb_csp_restart(snd_sb_csp_t
* p
);
102 static int snd_sb_qsound_build(snd_sb_csp_t
* p
);
103 static void snd_sb_qsound_destroy(snd_sb_csp_t
* p
);
104 static int snd_sb_csp_qsound_transfer(snd_sb_csp_t
* p
);
106 static int init_proc_entry(snd_sb_csp_t
* p
, int device
);
107 static void info_read(snd_info_entry_t
*entry
, snd_info_buffer_t
* buffer
);
110 * Detect CSP chip and create a new instance
112 int snd_sb_csp_new(sb_t
*chip
, int device
, snd_hwdep_t
** rhwdep
)
121 if (csp_detect(chip
, &version
))
124 if ((err
= snd_hwdep_new(chip
->card
, "SB16-CSP", device
, &hw
)) < 0)
127 if ((p
= kcalloc(1, sizeof(*p
), GFP_KERNEL
)) == NULL
) {
128 snd_device_free(chip
->card
, hw
);
132 p
->version
= version
;
135 p
->ops
.csp_use
= snd_sb_csp_use
;
136 p
->ops
.csp_unuse
= snd_sb_csp_unuse
;
137 p
->ops
.csp_autoload
= snd_sb_csp_autoload
;
138 p
->ops
.csp_start
= snd_sb_csp_start
;
139 p
->ops
.csp_stop
= snd_sb_csp_stop
;
140 p
->ops
.csp_qsound_transfer
= snd_sb_csp_qsound_transfer
;
142 init_MUTEX(&p
->access_mutex
);
143 sprintf(hw
->name
, "CSP v%d.%d", (version
>> 4), (version
& 0x0f));
144 hw
->iface
= SNDRV_HWDEP_IFACE_SB16CSP
;
145 hw
->private_data
= p
;
146 hw
->private_free
= snd_sb_csp_free
;
148 /* operators - only write/ioctl */
149 hw
->ops
.open
= snd_sb_csp_open
;
150 hw
->ops
.ioctl
= snd_sb_csp_ioctl
;
151 hw
->ops
.release
= snd_sb_csp_release
;
153 /* create a proc entry */
154 init_proc_entry(p
, device
);
161 * free_private for hwdep instance
163 static void snd_sb_csp_free(snd_hwdep_t
*hwdep
)
165 snd_sb_csp_t
*p
= hwdep
->private_data
;
167 if (p
->running
& SNDRV_SB_CSP_ST_RUNNING
)
173 /* ------------------------------ */
176 * open the device exclusively
178 static int snd_sb_csp_open(snd_hwdep_t
* hw
, struct file
*file
)
180 snd_sb_csp_t
*p
= hw
->private_data
;
181 return (snd_sb_csp_use(p
));
185 * ioctl for hwdep device:
187 static int snd_sb_csp_ioctl(snd_hwdep_t
* hw
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
189 snd_sb_csp_t
*p
= hw
->private_data
;
190 snd_sb_csp_info_t info
;
191 snd_sb_csp_start_t start_info
;
194 snd_assert(p
!= NULL
, return -EINVAL
);
196 if (snd_sb_csp_check_version(p
))
200 /* get information */
201 case SNDRV_SB_CSP_IOCTL_INFO
:
202 *info
.codec_name
= *p
->codec_name
;
203 info
.func_nr
= p
->func_nr
;
204 info
.acc_format
= p
->acc_format
;
205 info
.acc_channels
= p
->acc_channels
;
206 info
.acc_width
= p
->acc_width
;
207 info
.acc_rates
= p
->acc_rates
;
208 info
.csp_mode
= p
->mode
;
209 info
.run_channels
= p
->run_channels
;
210 info
.run_width
= p
->run_width
;
211 info
.version
= p
->version
;
212 info
.state
= p
->running
;
213 if (copy_to_user((void __user
*)arg
, &info
, sizeof(info
)))
219 /* load CSP microcode */
220 case SNDRV_SB_CSP_IOCTL_LOAD_CODE
:
221 err
= (p
->running
& SNDRV_SB_CSP_ST_RUNNING
?
222 -EBUSY
: snd_sb_csp_riff_load(p
, (snd_sb_csp_microcode_t __user
*) arg
));
224 case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE
:
225 err
= (p
->running
& SNDRV_SB_CSP_ST_RUNNING
?
226 -EBUSY
: snd_sb_csp_unload(p
));
229 /* change CSP running state */
230 case SNDRV_SB_CSP_IOCTL_START
:
231 if (copy_from_user(&start_info
, (void __user
*) arg
, sizeof(start_info
)))
234 err
= snd_sb_csp_start(p
, start_info
.sample_width
, start_info
.channels
);
236 case SNDRV_SB_CSP_IOCTL_STOP
:
237 err
= snd_sb_csp_stop(p
);
239 case SNDRV_SB_CSP_IOCTL_PAUSE
:
240 err
= snd_sb_csp_pause(p
);
242 case SNDRV_SB_CSP_IOCTL_RESTART
:
243 err
= snd_sb_csp_restart(p
);
256 static int snd_sb_csp_release(snd_hwdep_t
* hw
, struct file
*file
)
258 snd_sb_csp_t
*p
= hw
->private_data
;
259 return (snd_sb_csp_unuse(p
));
262 /* ------------------------------ */
267 static int snd_sb_csp_use(snd_sb_csp_t
* p
)
269 down(&p
->access_mutex
);
271 up(&p
->access_mutex
);
275 up(&p
->access_mutex
);
284 static int snd_sb_csp_unuse(snd_sb_csp_t
* p
)
286 down(&p
->access_mutex
);
288 up(&p
->access_mutex
);
294 * load microcode via ioctl:
295 * code is user-space pointer
297 static int snd_sb_csp_riff_load(snd_sb_csp_t
* p
, snd_sb_csp_microcode_t __user
* mcode
)
299 snd_sb_csp_mc_header_t info
;
301 unsigned char __user
*data_ptr
;
302 unsigned char __user
*data_end
;
303 unsigned short func_nr
= 0;
305 riff_header_t file_h
, item_h
, code_h
;
307 desc_header_t funcdesc_h
;
312 if (copy_from_user(&info
, mcode
, sizeof(info
)))
314 data_ptr
= mcode
->data
;
316 if (copy_from_user(&file_h
, data_ptr
, sizeof(file_h
)))
318 if ((file_h
.name
!= RIFF_HEADER
) ||
319 (LE_INT(file_h
.len
) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE
- sizeof(file_h
))) {
320 snd_printd("%s: Invalid RIFF header\n", __FUNCTION__
);
323 data_ptr
+= sizeof(file_h
);
324 data_end
= data_ptr
+ LE_INT(file_h
.len
);
326 if (copy_from_user(&item_type
, data_ptr
, sizeof(item_type
)))
328 if (item_type
!= CSP__HEADER
) {
329 snd_printd("%s: Invalid RIFF file type\n", __FUNCTION__
);
332 data_ptr
+= sizeof (item_type
);
334 for (; data_ptr
< data_end
; data_ptr
+= LE_INT(item_h
.len
)) {
335 if (copy_from_user(&item_h
, data_ptr
, sizeof(item_h
)))
337 data_ptr
+= sizeof(item_h
);
338 if (item_h
.name
!= LIST_HEADER
)
341 if (copy_from_user(&item_type
, data_ptr
, sizeof(item_type
)))
345 if (copy_from_user(&funcdesc_h
, data_ptr
+ sizeof(item_type
), sizeof(funcdesc_h
)))
347 func_nr
= LE_SHORT(funcdesc_h
.func_nr
);
350 if (func_nr
!= info
.func_req
)
351 break; /* not required function, try next */
352 data_ptr
+= sizeof(item_type
);
354 /* destroy QSound mixer element */
355 if (p
->mode
== SNDRV_SB_CSP_MODE_QSOUND
) {
356 snd_sb_qsound_destroy(p
);
358 /* Clear all flags */
362 /* load microcode blocks */
364 if (data_ptr
>= data_end
)
366 if (copy_from_user(&code_h
, data_ptr
, sizeof(code_h
)))
369 /* init microcode blocks */
370 if (code_h
.name
!= INIT_HEADER
)
372 data_ptr
+= sizeof(code_h
);
373 err
= snd_sb_csp_load_user(p
, data_ptr
, LE_INT(code_h
.len
),
374 SNDRV_SB_CSP_LOAD_INITBLOCK
);
377 data_ptr
+= LE_INT(code_h
.len
);
379 /* main microcode block */
380 if (copy_from_user(&code_h
, data_ptr
, sizeof(code_h
)))
383 if (code_h
.name
!= MAIN_HEADER
) {
384 snd_printd("%s: Missing 'main' microcode\n", __FUNCTION__
);
387 data_ptr
+= sizeof(code_h
);
388 err
= snd_sb_csp_load_user(p
, data_ptr
,
389 LE_INT(code_h
.len
), 0);
393 /* fill in codec header */
394 strlcpy(p
->codec_name
, info
.codec_name
, sizeof(p
->codec_name
));
395 p
->func_nr
= func_nr
;
396 p
->mode
= LE_SHORT(funcdesc_h
.flags_play_rec
);
397 switch (LE_SHORT(funcdesc_h
.VOC_type
)) {
398 case 0x0001: /* QSound decoder */
399 if (LE_SHORT(funcdesc_h
.flags_play_rec
) == SNDRV_SB_CSP_MODE_DSP_WRITE
) {
400 if (snd_sb_qsound_build(p
) == 0)
401 /* set QSound flag and clear all other mode flags */
402 p
->mode
= SNDRV_SB_CSP_MODE_QSOUND
;
406 case 0x0006: /* A Law codec */
407 p
->acc_format
= SNDRV_PCM_FMTBIT_A_LAW
;
409 case 0x0007: /* Mu Law codec */
410 p
->acc_format
= SNDRV_PCM_FMTBIT_MU_LAW
;
412 case 0x0011: /* what Creative thinks is IMA ADPCM codec */
413 case 0x0200: /* Creative ADPCM codec */
414 p
->acc_format
= SNDRV_PCM_FMTBIT_IMA_ADPCM
;
416 case 201: /* Text 2 Speech decoder */
417 /* TODO: Text2Speech handling routines */
420 case 0x0202: /* Fast Speech 8 codec */
421 case 0x0203: /* Fast Speech 10 codec */
422 p
->acc_format
= SNDRV_PCM_FMTBIT_SPECIAL
;
424 default: /* other codecs are unsupported */
425 p
->acc_format
= p
->acc_width
= p
->acc_rates
= 0;
427 snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
429 LE_SHORT(funcdesc_h
.VOC_type
));
432 p
->acc_channels
= LE_SHORT(funcdesc_h
.flags_stereo_mono
);
433 p
->acc_width
= LE_SHORT(funcdesc_h
.flags_16bit_8bit
);
434 p
->acc_rates
= LE_SHORT(funcdesc_h
.flags_rates
);
436 /* Decouple CSP from IRQ and DMAREQ lines */
437 spin_lock_irqsave(&p
->chip
->reg_lock
, flags
);
438 set_mode_register(p
->chip
, 0xfc);
439 set_mode_register(p
->chip
, 0x00);
440 spin_unlock_irqrestore(&p
->chip
->reg_lock
, flags
);
442 /* finished loading successfully */
443 p
->running
= SNDRV_SB_CSP_ST_LOADED
; /* set LOADED flag */
447 snd_printd("%s: Function #%d not found\n", __FUNCTION__
, info
.func_req
);
452 * unload CSP microcode
454 static int snd_sb_csp_unload(snd_sb_csp_t
* p
)
456 if (p
->running
& SNDRV_SB_CSP_ST_RUNNING
)
458 if (!(p
->running
& SNDRV_SB_CSP_ST_LOADED
))
461 /* clear supported formats */
463 p
->acc_channels
= p
->acc_width
= p
->acc_rates
= 0;
464 /* destroy QSound mixer element */
465 if (p
->mode
== SNDRV_SB_CSP_MODE_QSOUND
) {
466 snd_sb_qsound_destroy(p
);
468 /* clear all flags */
475 * send command sequence to DSP
477 static inline int command_seq(sb_t
*chip
, const unsigned char *seq
, int size
)
480 for (i
= 0; i
< size
; i
++) {
481 if (!snd_sbdsp_command(chip
, seq
[i
]))
488 * set CSP codec parameter
490 static int set_codec_parameter(sb_t
*chip
, unsigned char par
, unsigned char val
)
492 unsigned char dsp_cmd
[3];
494 dsp_cmd
[0] = 0x05; /* CSP set codec parameter */
495 dsp_cmd
[1] = val
; /* Parameter value */
496 dsp_cmd
[2] = par
; /* Parameter */
497 command_seq(chip
, dsp_cmd
, 3);
498 snd_sbdsp_command(chip
, 0x03); /* DSP read? */
499 if (snd_sbdsp_get_byte(chip
) != par
)
507 static int set_register(sb_t
*chip
, unsigned char reg
, unsigned char val
)
509 unsigned char dsp_cmd
[3];
511 dsp_cmd
[0] = 0x0e; /* CSP set register */
512 dsp_cmd
[1] = reg
; /* CSP Register */
513 dsp_cmd
[2] = val
; /* value */
514 return command_seq(chip
, dsp_cmd
, 3);
519 * return < 0 -> error
521 static int read_register(sb_t
*chip
, unsigned char reg
)
523 unsigned char dsp_cmd
[2];
525 dsp_cmd
[0] = 0x0f; /* CSP read register */
526 dsp_cmd
[1] = reg
; /* CSP Register */
527 command_seq(chip
, dsp_cmd
, 2);
528 return snd_sbdsp_get_byte(chip
); /* Read DSP value */
532 * set CSP mode register
534 static int set_mode_register(sb_t
*chip
, unsigned char mode
)
536 unsigned char dsp_cmd
[2];
538 dsp_cmd
[0] = 0x04; /* CSP set mode register */
539 dsp_cmd
[1] = mode
; /* mode */
540 return command_seq(chip
, dsp_cmd
, 2);
545 * return 0 if CSP exists.
547 static int csp_detect(sb_t
*chip
, int *version
)
549 unsigned char csp_test1
, csp_test2
;
551 int result
= -ENODEV
;
553 spin_lock_irqsave(&chip
->reg_lock
, flags
);
555 set_codec_parameter(chip
, 0x00, 0x00);
556 set_mode_register(chip
, 0xfc); /* 0xfc = ?? */
558 csp_test1
= read_register(chip
, 0x83);
559 set_register(chip
, 0x83, ~csp_test1
);
560 csp_test2
= read_register(chip
, 0x83);
561 if (csp_test2
!= (csp_test1
^ 0xff))
564 set_register(chip
, 0x83, csp_test1
);
565 csp_test2
= read_register(chip
, 0x83);
566 if (csp_test2
!= csp_test1
)
569 set_mode_register(chip
, 0x00); /* 0x00 = ? */
571 *version
= get_version(chip
);
572 snd_sbdsp_reset(chip
); /* reset DSP after getversion! */
573 if (*version
>= 0x10 && *version
<= 0x1f)
574 result
= 0; /* valid version id */
577 spin_unlock_irqrestore(&chip
->reg_lock
, flags
);
582 * get CSP version number
584 static int get_version(sb_t
*chip
)
586 unsigned char dsp_cmd
[2];
588 dsp_cmd
[0] = 0x08; /* SB_DSP_!something! */
589 dsp_cmd
[1] = 0x03; /* get chip version id? */
590 command_seq(chip
, dsp_cmd
, 2);
592 return (snd_sbdsp_get_byte(chip
));
596 * check if the CSP version is valid
598 static int snd_sb_csp_check_version(snd_sb_csp_t
* p
)
600 if (p
->version
< 0x10 || p
->version
> 0x1f) {
601 snd_printd("%s: Invalid CSP version: 0x%x\n", __FUNCTION__
, p
->version
);
608 * download microcode to CSP (microcode should have one "main" block).
610 static int snd_sb_csp_load(snd_sb_csp_t
* p
, const unsigned char *buf
, int size
, int load_flags
)
617 spin_lock_irqsave(&p
->chip
->reg_lock
, flags
);
618 snd_sbdsp_command(p
->chip
, 0x01); /* CSP download command */
619 if (snd_sbdsp_get_byte(p
->chip
)) {
620 snd_printd("%s: Download command failed\n", __FUNCTION__
);
623 /* Send CSP low byte (size - 1) */
624 snd_sbdsp_command(p
->chip
, (unsigned char)(size
- 1));
626 snd_sbdsp_command(p
->chip
, (unsigned char)((size
- 1) >> 8));
627 /* send microcode sequence */
628 /* load from kernel space */
630 if (!snd_sbdsp_command(p
->chip
, *buf
++))
633 if (snd_sbdsp_get_byte(p
->chip
))
636 if (load_flags
& SNDRV_SB_CSP_LOAD_INITBLOCK
) {
638 /* some codecs (FastSpeech) take some time to initialize */
640 snd_sbdsp_command(p
->chip
, 0x03);
641 status
= snd_sbdsp_get_byte(p
->chip
);
642 if (status
== 0x55 || ++i
>= 10)
646 if (status
!= 0x55) {
647 snd_printd("%s: Microcode initialization failed\n", __FUNCTION__
);
652 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
653 * Start CSP chip if no 16bit DMA channel is set - some kind
654 * of autorun or perhaps a bugfix?
656 spin_lock(&p
->chip
->mixer_lock
);
657 status
= snd_sbmixer_read(p
->chip
, SB_DSP4_DMASETUP
);
658 spin_unlock(&p
->chip
->mixer_lock
);
659 if (!(status
& (SB_DMASETUP_DMA7
| SB_DMASETUP_DMA6
| SB_DMASETUP_DMA5
))) {
660 err
= (set_codec_parameter(p
->chip
, 0xaa, 0x00) ||
661 set_codec_parameter(p
->chip
, 0xff, 0x00));
662 snd_sbdsp_reset(p
->chip
); /* really! */
665 set_mode_register(p
->chip
, 0xc0); /* c0 = STOP */
666 set_mode_register(p
->chip
, 0x70); /* 70 = RUN */
672 spin_unlock_irqrestore(&p
->chip
->reg_lock
, flags
);
676 static int snd_sb_csp_load_user(snd_sb_csp_t
* p
, const unsigned char __user
*buf
, int size
, int load_flags
)
679 unsigned char *kbuf
= kmalloc(size
, GFP_KERNEL
);
681 if (copy_from_user(kbuf
, buf
, size
))
684 err
= snd_sb_csp_load(p
, kbuf
, size
, load_flags
);
690 #include "sb16_csp_codecs.h"
693 * autoload hardware codec if necessary
694 * return 0 if CSP is loaded and ready to run (p->running != 0)
696 static int snd_sb_csp_autoload(snd_sb_csp_t
* p
, int pcm_sfmt
, int play_rec_mode
)
701 /* if CSP is running or manually loaded then exit */
702 if (p
->running
& (SNDRV_SB_CSP_ST_RUNNING
| SNDRV_SB_CSP_ST_LOADED
))
705 /* autoload microcode only if requested hardware codec is not already loaded */
706 if (((1 << pcm_sfmt
) & p
->acc_format
) && (play_rec_mode
& p
->mode
)) {
707 p
->running
= SNDRV_SB_CSP_ST_AUTO
;
710 case SNDRV_PCM_FORMAT_MU_LAW
:
711 err
= snd_sb_csp_load(p
, &mulaw_main
[0], sizeof(mulaw_main
), 0);
712 p
->acc_format
= SNDRV_PCM_FMTBIT_MU_LAW
;
713 p
->mode
= SNDRV_SB_CSP_MODE_DSP_READ
| SNDRV_SB_CSP_MODE_DSP_WRITE
;
715 case SNDRV_PCM_FORMAT_A_LAW
:
716 err
= snd_sb_csp_load(p
, &alaw_main
[0], sizeof(alaw_main
), 0);
717 p
->acc_format
= SNDRV_PCM_FMTBIT_A_LAW
;
718 p
->mode
= SNDRV_SB_CSP_MODE_DSP_READ
| SNDRV_SB_CSP_MODE_DSP_WRITE
;
720 case SNDRV_PCM_FORMAT_IMA_ADPCM
:
721 err
= snd_sb_csp_load(p
, &ima_adpcm_init
[0], sizeof(ima_adpcm_init
),
722 SNDRV_SB_CSP_LOAD_INITBLOCK
);
725 if (play_rec_mode
== SNDRV_SB_CSP_MODE_DSP_WRITE
) {
726 err
= snd_sb_csp_load(p
, &ima_adpcm_playback
[0],
727 sizeof(ima_adpcm_playback
), 0);
728 p
->mode
= SNDRV_SB_CSP_MODE_DSP_WRITE
;
730 err
= snd_sb_csp_load(p
, &ima_adpcm_capture
[0],
731 sizeof(ima_adpcm_capture
), 0);
732 p
->mode
= SNDRV_SB_CSP_MODE_DSP_READ
;
734 p
->acc_format
= SNDRV_PCM_FMTBIT_IMA_ADPCM
;
737 /* Decouple CSP from IRQ and DMAREQ lines */
738 if (p
->running
& SNDRV_SB_CSP_ST_AUTO
) {
739 spin_lock_irqsave(&p
->chip
->reg_lock
, flags
);
740 set_mode_register(p
->chip
, 0xfc);
741 set_mode_register(p
->chip
, 0x00);
742 spin_unlock_irqrestore(&p
->chip
->reg_lock
, flags
);
743 p
->running
= 0; /* clear autoloaded flag */
749 p
->acc_channels
= p
->acc_width
= p
->acc_rates
= 0;
751 p
->running
= 0; /* clear autoloaded flag */
755 p
->running
= SNDRV_SB_CSP_ST_AUTO
; /* set autoloaded flag */
756 p
->acc_width
= SNDRV_SB_CSP_SAMPLE_16BIT
; /* only 16 bit data */
757 p
->acc_channels
= SNDRV_SB_CSP_MONO
| SNDRV_SB_CSP_STEREO
;
758 p
->acc_rates
= SNDRV_SB_CSP_RATE_ALL
; /* HW codecs accept all rates */
762 return (p
->running
& SNDRV_SB_CSP_ST_AUTO
) ? 0 : -ENXIO
;
768 static int snd_sb_csp_start(snd_sb_csp_t
* p
, int sample_width
, int channels
)
770 unsigned char s_type
; /* sample type */
771 unsigned char mixL
, mixR
;
775 if (!(p
->running
& (SNDRV_SB_CSP_ST_LOADED
| SNDRV_SB_CSP_ST_AUTO
))) {
776 snd_printd("%s: Microcode not loaded\n", __FUNCTION__
);
779 if (p
->running
& SNDRV_SB_CSP_ST_RUNNING
) {
780 snd_printd("%s: CSP already running\n", __FUNCTION__
);
783 if (!(sample_width
& p
->acc_width
)) {
784 snd_printd("%s: Unsupported PCM sample width\n", __FUNCTION__
);
787 if (!(channels
& p
->acc_channels
)) {
788 snd_printd("%s: Invalid number of channels\n", __FUNCTION__
);
792 /* Mute PCM volume */
793 spin_lock_irqsave(&p
->chip
->mixer_lock
, flags
);
794 mixL
= snd_sbmixer_read(p
->chip
, SB_DSP4_PCM_DEV
);
795 mixR
= snd_sbmixer_read(p
->chip
, SB_DSP4_PCM_DEV
+ 1);
796 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
, mixL
& 0x7);
797 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
+ 1, mixR
& 0x7);
799 spin_lock(&p
->chip
->reg_lock
);
800 set_mode_register(p
->chip
, 0xc0); /* c0 = STOP */
801 set_mode_register(p
->chip
, 0x70); /* 70 = RUN */
804 if (channels
== SNDRV_SB_CSP_MONO
)
805 s_type
= 0x11; /* 000n 000n (n = 1 if mono) */
806 if (sample_width
== SNDRV_SB_CSP_SAMPLE_8BIT
)
807 s_type
|= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
809 if (set_codec_parameter(p
->chip
, 0x81, s_type
)) {
810 snd_printd("%s: Set sample type command failed\n", __FUNCTION__
);
813 if (set_codec_parameter(p
->chip
, 0x80, 0x00)) {
814 snd_printd("%s: Codec start command failed\n", __FUNCTION__
);
817 p
->run_width
= sample_width
;
818 p
->run_channels
= channels
;
820 p
->running
|= SNDRV_SB_CSP_ST_RUNNING
;
822 if (p
->mode
& SNDRV_SB_CSP_MODE_QSOUND
) {
823 set_codec_parameter(p
->chip
, 0xe0, 0x01);
824 /* enable QSound decoder */
825 set_codec_parameter(p
->chip
, 0x00, 0xff);
826 set_codec_parameter(p
->chip
, 0x01, 0xff);
827 p
->running
|= SNDRV_SB_CSP_ST_QSOUND
;
828 /* set QSound startup value */
829 snd_sb_csp_qsound_transfer(p
);
834 spin_unlock(&p
->chip
->reg_lock
);
836 /* restore PCM volume */
837 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
, mixL
);
838 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
+ 1, mixR
);
839 spin_unlock_irqrestore(&p
->chip
->mixer_lock
, flags
);
847 static int snd_sb_csp_stop(snd_sb_csp_t
* p
)
850 unsigned char mixL
, mixR
;
853 if (!(p
->running
& SNDRV_SB_CSP_ST_RUNNING
))
856 /* Mute PCM volume */
857 spin_lock_irqsave(&p
->chip
->mixer_lock
, flags
);
858 mixL
= snd_sbmixer_read(p
->chip
, SB_DSP4_PCM_DEV
);
859 mixR
= snd_sbmixer_read(p
->chip
, SB_DSP4_PCM_DEV
+ 1);
860 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
, mixL
& 0x7);
861 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
+ 1, mixR
& 0x7);
863 spin_lock(&p
->chip
->reg_lock
);
864 if (p
->running
& SNDRV_SB_CSP_ST_QSOUND
) {
865 set_codec_parameter(p
->chip
, 0xe0, 0x01);
866 /* disable QSound decoder */
867 set_codec_parameter(p
->chip
, 0x00, 0x00);
868 set_codec_parameter(p
->chip
, 0x01, 0x00);
870 p
->running
&= ~SNDRV_SB_CSP_ST_QSOUND
;
872 result
= set_mode_register(p
->chip
, 0xc0); /* c0 = STOP */
873 spin_unlock(&p
->chip
->reg_lock
);
875 /* restore PCM volume */
876 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
, mixL
);
877 snd_sbmixer_write(p
->chip
, SB_DSP4_PCM_DEV
+ 1, mixR
);
878 spin_unlock_irqrestore(&p
->chip
->mixer_lock
, flags
);
881 p
->running
&= ~(SNDRV_SB_CSP_ST_PAUSED
| SNDRV_SB_CSP_ST_RUNNING
);
886 * pause CSP codec and hold DMA transfer
888 static int snd_sb_csp_pause(snd_sb_csp_t
* p
)
893 if (!(p
->running
& SNDRV_SB_CSP_ST_RUNNING
))
896 spin_lock_irqsave(&p
->chip
->reg_lock
, flags
);
897 result
= set_codec_parameter(p
->chip
, 0x80, 0xff);
898 spin_unlock_irqrestore(&p
->chip
->reg_lock
, flags
);
900 p
->running
|= SNDRV_SB_CSP_ST_PAUSED
;
906 * restart CSP codec and resume DMA transfer
908 static int snd_sb_csp_restart(snd_sb_csp_t
* p
)
913 if (!(p
->running
& SNDRV_SB_CSP_ST_PAUSED
))
916 spin_lock_irqsave(&p
->chip
->reg_lock
, flags
);
917 result
= set_codec_parameter(p
->chip
, 0x80, 0x00);
918 spin_unlock_irqrestore(&p
->chip
->reg_lock
, flags
);
920 p
->running
&= ~SNDRV_SB_CSP_ST_PAUSED
;
925 /* ------------------------------ */
928 * QSound mixer control for PCM
931 static int snd_sb_qsound_switch_info(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
* uinfo
)
933 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
935 uinfo
->value
.integer
.min
= 0;
936 uinfo
->value
.integer
.max
= 1;
940 static int snd_sb_qsound_switch_get(snd_kcontrol_t
* kcontrol
, snd_ctl_elem_value_t
* ucontrol
)
942 snd_sb_csp_t
*p
= snd_kcontrol_chip(kcontrol
);
944 ucontrol
->value
.integer
.value
[0] = p
->q_enabled
? 1 : 0;
948 static int snd_sb_qsound_switch_put(snd_kcontrol_t
* kcontrol
, snd_ctl_elem_value_t
* ucontrol
)
950 snd_sb_csp_t
*p
= snd_kcontrol_chip(kcontrol
);
955 nval
= ucontrol
->value
.integer
.value
[0] & 0x01;
956 spin_lock_irqsave(&p
->q_lock
, flags
);
957 change
= p
->q_enabled
!= nval
;
959 spin_unlock_irqrestore(&p
->q_lock
, flags
);
963 static int snd_sb_qsound_space_info(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
* uinfo
)
965 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
967 uinfo
->value
.integer
.min
= 0;
968 uinfo
->value
.integer
.max
= SNDRV_SB_CSP_QSOUND_MAX_RIGHT
;
972 static int snd_sb_qsound_space_get(snd_kcontrol_t
* kcontrol
, snd_ctl_elem_value_t
* ucontrol
)
974 snd_sb_csp_t
*p
= snd_kcontrol_chip(kcontrol
);
977 spin_lock_irqsave(&p
->q_lock
, flags
);
978 ucontrol
->value
.integer
.value
[0] = p
->qpos_left
;
979 ucontrol
->value
.integer
.value
[1] = p
->qpos_right
;
980 spin_unlock_irqrestore(&p
->q_lock
, flags
);
984 static int snd_sb_qsound_space_put(snd_kcontrol_t
* kcontrol
, snd_ctl_elem_value_t
* ucontrol
)
986 snd_sb_csp_t
*p
= snd_kcontrol_chip(kcontrol
);
989 unsigned char nval1
, nval2
;
991 nval1
= ucontrol
->value
.integer
.value
[0];
992 if (nval1
> SNDRV_SB_CSP_QSOUND_MAX_RIGHT
)
993 nval1
= SNDRV_SB_CSP_QSOUND_MAX_RIGHT
;
994 nval2
= ucontrol
->value
.integer
.value
[1];
995 if (nval2
> SNDRV_SB_CSP_QSOUND_MAX_RIGHT
)
996 nval2
= SNDRV_SB_CSP_QSOUND_MAX_RIGHT
;
997 spin_lock_irqsave(&p
->q_lock
, flags
);
998 change
= p
->qpos_left
!= nval1
|| p
->qpos_right
!= nval2
;
999 p
->qpos_left
= nval1
;
1000 p
->qpos_right
= nval2
;
1001 p
->qpos_changed
= change
;
1002 spin_unlock_irqrestore(&p
->q_lock
, flags
);
1006 static snd_kcontrol_new_t snd_sb_qsound_switch
= {
1007 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1008 .name
= "3D Control - Switch",
1009 .info
= snd_sb_qsound_switch_info
,
1010 .get
= snd_sb_qsound_switch_get
,
1011 .put
= snd_sb_qsound_switch_put
1014 static snd_kcontrol_new_t snd_sb_qsound_space
= {
1015 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1016 .name
= "3D Control - Space",
1017 .info
= snd_sb_qsound_space_info
,
1018 .get
= snd_sb_qsound_space_get
,
1019 .put
= snd_sb_qsound_space_put
1022 static int snd_sb_qsound_build(snd_sb_csp_t
* p
)
1027 snd_assert(p
!= NULL
, return -EINVAL
);
1029 card
= p
->chip
->card
;
1030 p
->qpos_left
= p
->qpos_right
= SNDRV_SB_CSP_QSOUND_MAX_RIGHT
/ 2;
1031 p
->qpos_changed
= 0;
1033 spin_lock_init(&p
->q_lock
);
1035 if ((err
= snd_ctl_add(card
, p
->qsound_switch
= snd_ctl_new1(&snd_sb_qsound_switch
, p
))) < 0)
1037 if ((err
= snd_ctl_add(card
, p
->qsound_space
= snd_ctl_new1(&snd_sb_qsound_space
, p
))) < 0)
1043 snd_sb_qsound_destroy(p
);
1047 static void snd_sb_qsound_destroy(snd_sb_csp_t
* p
)
1050 unsigned long flags
;
1052 snd_assert(p
!= NULL
, return);
1054 card
= p
->chip
->card
;
1056 down_write(&card
->controls_rwsem
);
1057 if (p
->qsound_switch
)
1058 snd_ctl_remove(card
, p
->qsound_switch
);
1059 if (p
->qsound_space
)
1060 snd_ctl_remove(card
, p
->qsound_space
);
1061 up_write(&card
->controls_rwsem
);
1063 /* cancel pending transfer of QSound parameters */
1064 spin_lock_irqsave (&p
->q_lock
, flags
);
1065 p
->qpos_changed
= 0;
1066 spin_unlock_irqrestore (&p
->q_lock
, flags
);
1070 * Transfer qsound parameters to CSP,
1071 * function should be called from interrupt routine
1073 static int snd_sb_csp_qsound_transfer(snd_sb_csp_t
* p
)
1077 spin_lock(&p
->q_lock
);
1078 if (p
->running
& SNDRV_SB_CSP_ST_QSOUND
) {
1079 set_codec_parameter(p
->chip
, 0xe0, 0x01);
1081 set_codec_parameter(p
->chip
, 0x00, p
->qpos_left
);
1082 set_codec_parameter(p
->chip
, 0x02, 0x00);
1084 set_codec_parameter(p
->chip
, 0x00, p
->qpos_right
);
1085 set_codec_parameter(p
->chip
, 0x03, 0x00);
1088 p
->qpos_changed
= 0;
1089 spin_unlock(&p
->q_lock
);
1093 /* ------------------------------ */
1098 static int init_proc_entry(snd_sb_csp_t
* p
, int device
)
1101 snd_info_entry_t
*entry
;
1102 sprintf(name
, "cspD%d", device
);
1103 if (! snd_card_proc_new(p
->chip
->card
, name
, &entry
))
1104 snd_info_set_text_ops(entry
, p
, 1024, info_read
);
1108 static void info_read(snd_info_entry_t
*entry
, snd_info_buffer_t
* buffer
)
1110 snd_sb_csp_t
*p
= entry
->private_data
;
1112 snd_iprintf(buffer
, "Creative Signal Processor [v%d.%d]\n", (p
->version
>> 4), (p
->version
& 0x0f));
1113 snd_iprintf(buffer
, "State: %cx%c%c%c\n", ((p
->running
& SNDRV_SB_CSP_ST_QSOUND
) ? 'Q' : '-'),
1114 ((p
->running
& SNDRV_SB_CSP_ST_PAUSED
) ? 'P' : '-'),
1115 ((p
->running
& SNDRV_SB_CSP_ST_RUNNING
) ? 'R' : '-'),
1116 ((p
->running
& SNDRV_SB_CSP_ST_LOADED
) ? 'L' : '-'));
1117 if (p
->running
& SNDRV_SB_CSP_ST_LOADED
) {
1118 snd_iprintf(buffer
, "Codec: %s [func #%d]\n", p
->codec_name
, p
->func_nr
);
1119 snd_iprintf(buffer
, "Sample rates: ");
1120 if (p
->acc_rates
== SNDRV_SB_CSP_RATE_ALL
) {
1121 snd_iprintf(buffer
, "All\n");
1123 snd_iprintf(buffer
, "%s%s%s%s\n",
1124 ((p
->acc_rates
& SNDRV_SB_CSP_RATE_8000
) ? "8000Hz " : ""),
1125 ((p
->acc_rates
& SNDRV_SB_CSP_RATE_11025
) ? "11025Hz " : ""),
1126 ((p
->acc_rates
& SNDRV_SB_CSP_RATE_22050
) ? "22050Hz " : ""),
1127 ((p
->acc_rates
& SNDRV_SB_CSP_RATE_44100
) ? "44100Hz" : ""));
1129 if (p
->mode
== SNDRV_SB_CSP_MODE_QSOUND
) {
1130 snd_iprintf(buffer
, "QSound decoder %sabled\n",
1131 p
->q_enabled
? "en" : "dis");
1133 snd_iprintf(buffer
, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
1135 ((p
->acc_width
& SNDRV_SB_CSP_SAMPLE_16BIT
) ? "16bit" : "-"),
1136 ((p
->acc_width
& SNDRV_SB_CSP_SAMPLE_8BIT
) ? "8bit" : "-"),
1137 ((p
->acc_channels
& SNDRV_SB_CSP_MONO
) ? "mono" : "-"),
1138 ((p
->acc_channels
& SNDRV_SB_CSP_STEREO
) ? "stereo" : "-"),
1139 ((p
->mode
& SNDRV_SB_CSP_MODE_DSP_WRITE
) ? "playback" : "-"),
1140 ((p
->mode
& SNDRV_SB_CSP_MODE_DSP_READ
) ? "capture" : "-"));
1143 if (p
->running
& SNDRV_SB_CSP_ST_AUTO
) {
1144 snd_iprintf(buffer
, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1146 if (p
->running
& SNDRV_SB_CSP_ST_RUNNING
) {
1147 snd_iprintf(buffer
, "Processing %dbit %s PCM samples\n",
1148 ((p
->run_width
& SNDRV_SB_CSP_SAMPLE_16BIT
) ? 16 : 8),
1149 ((p
->run_channels
& SNDRV_SB_CSP_MONO
) ? "mono" : "stereo"));
1151 if (p
->running
& SNDRV_SB_CSP_ST_QSOUND
) {
1152 snd_iprintf(buffer
, "Qsound position: left = 0x%x, right = 0x%x\n",
1153 p
->qpos_left
, p
->qpos_right
);
1159 EXPORT_SYMBOL(snd_sb_csp_new
);
1165 static int __init
alsa_sb_csp_init(void)
1170 static void __exit
alsa_sb_csp_exit(void)
1174 module_init(alsa_sb_csp_init
)
1175 module_exit(alsa_sb_csp_exit
)