blob: 5f21aec585f61805d54cf5886ab66e4f2d61cbdd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 1999 by Uros Bizjak <uros@kss-loka.si>
3 * Takashi Iwai <tiwai@suse.de>
4 *
5 * SB16ASP/AWE32 CSP control
6 *
7 * CSP microcode loader:
8 * alsa-tools/sb16_csp/
9 *
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.
14 *
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.
19 *
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
23 *
24 */
25
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>
35
36MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
37MODULE_DESCRIPTION("ALSA driver for SB16 Creative Signal Processor");
38MODULE_LICENSE("GPL");
Clemens Ladisch7e0af292007-05-03 17:59:54 +020039#ifndef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
40MODULE_FIRMWARE("sb16/mulaw_main.csp");
41MODULE_FIRMWARE("sb16/alaw_main.csp");
42MODULE_FIRMWARE("sb16/ima_adpcm_init.csp");
43MODULE_FIRMWARE("sb16/ima_adpcm_playback.csp");
44MODULE_FIRMWARE("sb16/ima_adpcm_capture.csp");
45#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#ifdef SNDRV_LITTLE_ENDIAN
48#define CSP_HDR_VALUE(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
49#else
50#define CSP_HDR_VALUE(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
51#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define RIFF_HEADER CSP_HDR_VALUE('R', 'I', 'F', 'F')
54#define CSP__HEADER CSP_HDR_VALUE('C', 'S', 'P', ' ')
55#define LIST_HEADER CSP_HDR_VALUE('L', 'I', 'S', 'T')
56#define FUNC_HEADER CSP_HDR_VALUE('f', 'u', 'n', 'c')
57#define CODE_HEADER CSP_HDR_VALUE('c', 'o', 'd', 'e')
58#define INIT_HEADER CSP_HDR_VALUE('i', 'n', 'i', 't')
59#define MAIN_HEADER CSP_HDR_VALUE('m', 'a', 'i', 'n')
60
61/*
62 * RIFF data format
63 */
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070064struct riff_header {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 __u32 name;
66 __u32 len;
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070067};
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070069struct desc_header {
70 struct riff_header info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 __u16 func_nr;
72 __u16 VOC_type;
73 __u16 flags_play_rec;
74 __u16 flags_16bit_8bit;
75 __u16 flags_stereo_mono;
76 __u16 flags_rates;
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070077};
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * prototypes
81 */
Takashi Iwai029d64b2005-11-17 14:34:36 +010082static void snd_sb_csp_free(struct snd_hwdep *hw);
83static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file);
84static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg);
85static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Takashi Iwai029d64b2005-11-17 14:34:36 +010087static int csp_detect(struct snd_sb *chip, int *version);
88static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val);
89static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val);
90static int read_register(struct snd_sb *chip, unsigned char reg);
91static int set_mode_register(struct snd_sb *chip, unsigned char mode);
92static int get_version(struct snd_sb *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Takashi Iwai029d64b2005-11-17 14:34:36 +010094static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
95 struct snd_sb_csp_microcode __user * code);
96static int snd_sb_csp_unload(struct snd_sb_csp * p);
97static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags);
98static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode);
99static int snd_sb_csp_check_version(struct snd_sb_csp * p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Takashi Iwai029d64b2005-11-17 14:34:36 +0100101static int snd_sb_csp_use(struct snd_sb_csp * p);
102static int snd_sb_csp_unuse(struct snd_sb_csp * p);
103static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels);
104static int snd_sb_csp_stop(struct snd_sb_csp * p);
105static int snd_sb_csp_pause(struct snd_sb_csp * p);
106static int snd_sb_csp_restart(struct snd_sb_csp * p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Takashi Iwai029d64b2005-11-17 14:34:36 +0100108static int snd_sb_qsound_build(struct snd_sb_csp * p);
109static void snd_sb_qsound_destroy(struct snd_sb_csp * p);
110static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Takashi Iwai029d64b2005-11-17 14:34:36 +0100112static int init_proc_entry(struct snd_sb_csp * p, int device);
113static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/*
116 * Detect CSP chip and create a new instance
117 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100118int snd_sb_csp_new(struct snd_sb *chip, int device, struct snd_hwdep ** rhwdep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100120 struct snd_sb_csp *p;
Takashi Iwaicd0b4ac2007-12-14 12:18:52 +0100121 int uninitialized_var(version);
122 int err;
Takashi Iwai029d64b2005-11-17 14:34:36 +0100123 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 if (rhwdep)
126 *rhwdep = NULL;
127
128 if (csp_detect(chip, &version))
129 return -ENODEV;
130
131 if ((err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw)) < 0)
132 return err;
133
Takashi Iwai9e76a762005-09-09 14:21:17 +0200134 if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 snd_device_free(chip->card, hw);
136 return -ENOMEM;
137 }
138 p->chip = chip;
139 p->version = version;
140
141 /* CSP operators */
142 p->ops.csp_use = snd_sb_csp_use;
143 p->ops.csp_unuse = snd_sb_csp_unuse;
144 p->ops.csp_autoload = snd_sb_csp_autoload;
145 p->ops.csp_start = snd_sb_csp_start;
146 p->ops.csp_stop = snd_sb_csp_stop;
147 p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;
148
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100149 mutex_init(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f));
151 hw->iface = SNDRV_HWDEP_IFACE_SB16CSP;
152 hw->private_data = p;
153 hw->private_free = snd_sb_csp_free;
154
155 /* operators - only write/ioctl */
156 hw->ops.open = snd_sb_csp_open;
157 hw->ops.ioctl = snd_sb_csp_ioctl;
158 hw->ops.release = snd_sb_csp_release;
159
160 /* create a proc entry */
161 init_proc_entry(p, device);
162 if (rhwdep)
163 *rhwdep = hw;
164 return 0;
165}
166
167/*
168 * free_private for hwdep instance
169 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100170static void snd_sb_csp_free(struct snd_hwdep *hwdep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200172#ifndef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
Clemens Ladischde66d532006-11-06 09:18:34 +0100173 int i;
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200174#endif
Takashi Iwai029d64b2005-11-17 14:34:36 +0100175 struct snd_sb_csp *p = hwdep->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (p) {
177 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
178 snd_sb_csp_stop(p);
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200179#ifndef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
Clemens Ladischde66d532006-11-06 09:18:34 +0100180 for (i = 0; i < ARRAY_SIZE(p->csp_programs); ++i)
181 release_firmware(p->csp_programs[i]);
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200182#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 kfree(p);
184 }
185}
186
187/* ------------------------------ */
188
189/*
190 * open the device exclusively
191 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100192static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100194 struct snd_sb_csp *p = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return (snd_sb_csp_use(p));
196}
197
198/*
199 * ioctl for hwdep device:
200 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100201static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100203 struct snd_sb_csp *p = hw->private_data;
204 struct snd_sb_csp_info info;
205 struct snd_sb_csp_start start_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int err;
207
208 snd_assert(p != NULL, return -EINVAL);
209
210 if (snd_sb_csp_check_version(p))
211 return -ENODEV;
212
213 switch (cmd) {
214 /* get information */
215 case SNDRV_SB_CSP_IOCTL_INFO:
216 *info.codec_name = *p->codec_name;
217 info.func_nr = p->func_nr;
218 info.acc_format = p->acc_format;
219 info.acc_channels = p->acc_channels;
220 info.acc_width = p->acc_width;
221 info.acc_rates = p->acc_rates;
222 info.csp_mode = p->mode;
223 info.run_channels = p->run_channels;
224 info.run_width = p->run_width;
225 info.version = p->version;
226 info.state = p->running;
227 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
228 err = -EFAULT;
229 else
230 err = 0;
231 break;
232
233 /* load CSP microcode */
234 case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
235 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
Takashi Iwai029d64b2005-11-17 14:34:36 +0100236 -EBUSY : snd_sb_csp_riff_load(p, (struct snd_sb_csp_microcode __user *) arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 break;
238 case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
239 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
240 -EBUSY : snd_sb_csp_unload(p));
241 break;
242
243 /* change CSP running state */
244 case SNDRV_SB_CSP_IOCTL_START:
245 if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info)))
246 err = -EFAULT;
247 else
248 err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
249 break;
250 case SNDRV_SB_CSP_IOCTL_STOP:
251 err = snd_sb_csp_stop(p);
252 break;
253 case SNDRV_SB_CSP_IOCTL_PAUSE:
254 err = snd_sb_csp_pause(p);
255 break;
256 case SNDRV_SB_CSP_IOCTL_RESTART:
257 err = snd_sb_csp_restart(p);
258 break;
259 default:
260 err = -ENOTTY;
261 break;
262 }
263
264 return err;
265}
266
267/*
268 * close the device
269 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100270static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100272 struct snd_sb_csp *p = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return (snd_sb_csp_unuse(p));
274}
275
276/* ------------------------------ */
277
278/*
279 * acquire device
280 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100281static int snd_sb_csp_use(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100283 mutex_lock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (p->used) {
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100285 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return -EAGAIN;
287 }
288 p->used++;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100289 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 return 0;
292
293}
294
295/*
296 * release device
297 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100298static int snd_sb_csp_unuse(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100300 mutex_lock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 p->used--;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100302 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 return 0;
305}
306
307/*
308 * load microcode via ioctl:
309 * code is user-space pointer
310 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100311static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
312 struct snd_sb_csp_microcode __user * mcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100314 struct snd_sb_csp_mc_header info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 unsigned char __user *data_ptr;
317 unsigned char __user *data_end;
318 unsigned short func_nr = 0;
319
Alexey Dobriyandfc866e2005-09-10 00:26:35 -0700320 struct riff_header file_h, item_h, code_h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 __u32 item_type;
Alexey Dobriyandfc866e2005-09-10 00:26:35 -0700322 struct desc_header funcdesc_h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 unsigned long flags;
325 int err;
326
327 if (copy_from_user(&info, mcode, sizeof(info)))
328 return -EFAULT;
329 data_ptr = mcode->data;
330
331 if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
332 return -EFAULT;
333 if ((file_h.name != RIFF_HEADER) ||
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700334 (le32_to_cpu(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 snd_printd("%s: Invalid RIFF header\n", __FUNCTION__);
336 return -EINVAL;
337 }
338 data_ptr += sizeof(file_h);
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700339 data_end = data_ptr + le32_to_cpu(file_h.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
342 return -EFAULT;
343 if (item_type != CSP__HEADER) {
344 snd_printd("%s: Invalid RIFF file type\n", __FUNCTION__);
345 return -EINVAL;
346 }
347 data_ptr += sizeof (item_type);
348
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700349 for (; data_ptr < data_end; data_ptr += le32_to_cpu(item_h.len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
351 return -EFAULT;
352 data_ptr += sizeof(item_h);
353 if (item_h.name != LIST_HEADER)
354 continue;
355
356 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
357 return -EFAULT;
358 switch (item_type) {
359 case FUNC_HEADER:
360 if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
361 return -EFAULT;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700362 func_nr = le16_to_cpu(funcdesc_h.func_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 case CODE_HEADER:
365 if (func_nr != info.func_req)
366 break; /* not required function, try next */
367 data_ptr += sizeof(item_type);
368
369 /* destroy QSound mixer element */
370 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
371 snd_sb_qsound_destroy(p);
372 }
373 /* Clear all flags */
374 p->running = 0;
375 p->mode = 0;
376
377 /* load microcode blocks */
378 for (;;) {
379 if (data_ptr >= data_end)
380 return -EINVAL;
381 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
382 return -EFAULT;
383
384 /* init microcode blocks */
385 if (code_h.name != INIT_HEADER)
386 break;
387 data_ptr += sizeof(code_h);
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700388 err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 SNDRV_SB_CSP_LOAD_INITBLOCK);
390 if (err)
391 return err;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700392 data_ptr += le32_to_cpu(code_h.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394 /* main microcode block */
395 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
396 return -EFAULT;
397
398 if (code_h.name != MAIN_HEADER) {
399 snd_printd("%s: Missing 'main' microcode\n", __FUNCTION__);
400 return -EINVAL;
401 }
402 data_ptr += sizeof(code_h);
403 err = snd_sb_csp_load_user(p, data_ptr,
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700404 le32_to_cpu(code_h.len), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (err)
406 return err;
407
408 /* fill in codec header */
409 strlcpy(p->codec_name, info.codec_name, sizeof(p->codec_name));
410 p->func_nr = func_nr;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700411 p->mode = le16_to_cpu(funcdesc_h.flags_play_rec);
412 switch (le16_to_cpu(funcdesc_h.VOC_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 case 0x0001: /* QSound decoder */
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700414 if (le16_to_cpu(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (snd_sb_qsound_build(p) == 0)
416 /* set QSound flag and clear all other mode flags */
417 p->mode = SNDRV_SB_CSP_MODE_QSOUND;
418 }
419 p->acc_format = 0;
420 break;
421 case 0x0006: /* A Law codec */
422 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
423 break;
424 case 0x0007: /* Mu Law codec */
425 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
426 break;
427 case 0x0011: /* what Creative thinks is IMA ADPCM codec */
428 case 0x0200: /* Creative ADPCM codec */
429 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
430 break;
431 case 201: /* Text 2 Speech decoder */
432 /* TODO: Text2Speech handling routines */
433 p->acc_format = 0;
434 break;
435 case 0x0202: /* Fast Speech 8 codec */
436 case 0x0203: /* Fast Speech 10 codec */
437 p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
438 break;
439 default: /* other codecs are unsupported */
440 p->acc_format = p->acc_width = p->acc_rates = 0;
441 p->mode = 0;
442 snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
443 __FUNCTION__,
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700444 le16_to_cpu(funcdesc_h.VOC_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return -EINVAL;
446 }
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700447 p->acc_channels = le16_to_cpu(funcdesc_h.flags_stereo_mono);
448 p->acc_width = le16_to_cpu(funcdesc_h.flags_16bit_8bit);
449 p->acc_rates = le16_to_cpu(funcdesc_h.flags_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /* Decouple CSP from IRQ and DMAREQ lines */
452 spin_lock_irqsave(&p->chip->reg_lock, flags);
453 set_mode_register(p->chip, 0xfc);
454 set_mode_register(p->chip, 0x00);
455 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
456
457 /* finished loading successfully */
458 p->running = SNDRV_SB_CSP_ST_LOADED; /* set LOADED flag */
459 return 0;
460 }
461 }
462 snd_printd("%s: Function #%d not found\n", __FUNCTION__, info.func_req);
463 return -EINVAL;
464}
465
466/*
467 * unload CSP microcode
468 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100469static int snd_sb_csp_unload(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
472 return -EBUSY;
473 if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
474 return -ENXIO;
475
476 /* clear supported formats */
477 p->acc_format = 0;
478 p->acc_channels = p->acc_width = p->acc_rates = 0;
479 /* destroy QSound mixer element */
480 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
481 snd_sb_qsound_destroy(p);
482 }
483 /* clear all flags */
484 p->running = 0;
485 p->mode = 0;
486 return 0;
487}
488
489/*
490 * send command sequence to DSP
491 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100492static inline int command_seq(struct snd_sb *chip, const unsigned char *seq, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 int i;
495 for (i = 0; i < size; i++) {
496 if (!snd_sbdsp_command(chip, seq[i]))
497 return -EIO;
498 }
499 return 0;
500}
501
502/*
503 * set CSP codec parameter
504 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100505static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 unsigned char dsp_cmd[3];
508
509 dsp_cmd[0] = 0x05; /* CSP set codec parameter */
510 dsp_cmd[1] = val; /* Parameter value */
511 dsp_cmd[2] = par; /* Parameter */
512 command_seq(chip, dsp_cmd, 3);
513 snd_sbdsp_command(chip, 0x03); /* DSP read? */
514 if (snd_sbdsp_get_byte(chip) != par)
515 return -EIO;
516 return 0;
517}
518
519/*
520 * set CSP register
521 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100522static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 unsigned char dsp_cmd[3];
525
526 dsp_cmd[0] = 0x0e; /* CSP set register */
527 dsp_cmd[1] = reg; /* CSP Register */
528 dsp_cmd[2] = val; /* value */
529 return command_seq(chip, dsp_cmd, 3);
530}
531
532/*
533 * read CSP register
534 * return < 0 -> error
535 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100536static int read_register(struct snd_sb *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 unsigned char dsp_cmd[2];
539
540 dsp_cmd[0] = 0x0f; /* CSP read register */
541 dsp_cmd[1] = reg; /* CSP Register */
542 command_seq(chip, dsp_cmd, 2);
543 return snd_sbdsp_get_byte(chip); /* Read DSP value */
544}
545
546/*
547 * set CSP mode register
548 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100549static int set_mode_register(struct snd_sb *chip, unsigned char mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 unsigned char dsp_cmd[2];
552
553 dsp_cmd[0] = 0x04; /* CSP set mode register */
554 dsp_cmd[1] = mode; /* mode */
555 return command_seq(chip, dsp_cmd, 2);
556}
557
558/*
559 * Detect CSP
560 * return 0 if CSP exists.
561 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100562static int csp_detect(struct snd_sb *chip, int *version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 unsigned char csp_test1, csp_test2;
565 unsigned long flags;
566 int result = -ENODEV;
567
568 spin_lock_irqsave(&chip->reg_lock, flags);
569
570 set_codec_parameter(chip, 0x00, 0x00);
571 set_mode_register(chip, 0xfc); /* 0xfc = ?? */
572
573 csp_test1 = read_register(chip, 0x83);
574 set_register(chip, 0x83, ~csp_test1);
575 csp_test2 = read_register(chip, 0x83);
576 if (csp_test2 != (csp_test1 ^ 0xff))
577 goto __fail;
578
579 set_register(chip, 0x83, csp_test1);
580 csp_test2 = read_register(chip, 0x83);
581 if (csp_test2 != csp_test1)
582 goto __fail;
583
584 set_mode_register(chip, 0x00); /* 0x00 = ? */
585
586 *version = get_version(chip);
587 snd_sbdsp_reset(chip); /* reset DSP after getversion! */
588 if (*version >= 0x10 && *version <= 0x1f)
589 result = 0; /* valid version id */
590
591 __fail:
592 spin_unlock_irqrestore(&chip->reg_lock, flags);
593 return result;
594}
595
596/*
597 * get CSP version number
598 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100599static int get_version(struct snd_sb *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 unsigned char dsp_cmd[2];
602
603 dsp_cmd[0] = 0x08; /* SB_DSP_!something! */
604 dsp_cmd[1] = 0x03; /* get chip version id? */
605 command_seq(chip, dsp_cmd, 2);
606
607 return (snd_sbdsp_get_byte(chip));
608}
609
610/*
611 * check if the CSP version is valid
612 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100613static int snd_sb_csp_check_version(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 if (p->version < 0x10 || p->version > 0x1f) {
616 snd_printd("%s: Invalid CSP version: 0x%x\n", __FUNCTION__, p->version);
617 return 1;
618 }
619 return 0;
620}
621
622/*
623 * download microcode to CSP (microcode should have one "main" block).
624 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100625static int snd_sb_csp_load(struct snd_sb_csp * p, const unsigned char *buf, int size, int load_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 int status, i;
628 int err;
629 int result = -EIO;
630 unsigned long flags;
631
632 spin_lock_irqsave(&p->chip->reg_lock, flags);
633 snd_sbdsp_command(p->chip, 0x01); /* CSP download command */
634 if (snd_sbdsp_get_byte(p->chip)) {
635 snd_printd("%s: Download command failed\n", __FUNCTION__);
636 goto __fail;
637 }
638 /* Send CSP low byte (size - 1) */
639 snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
640 /* Send high byte */
641 snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
642 /* send microcode sequence */
643 /* load from kernel space */
644 while (size--) {
645 if (!snd_sbdsp_command(p->chip, *buf++))
646 goto __fail;
647 }
648 if (snd_sbdsp_get_byte(p->chip))
649 goto __fail;
650
651 if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
652 i = 0;
653 /* some codecs (FastSpeech) take some time to initialize */
654 while (1) {
655 snd_sbdsp_command(p->chip, 0x03);
656 status = snd_sbdsp_get_byte(p->chip);
657 if (status == 0x55 || ++i >= 10)
658 break;
659 udelay (10);
660 }
661 if (status != 0x55) {
662 snd_printd("%s: Microcode initialization failed\n", __FUNCTION__);
663 goto __fail;
664 }
665 } else {
666 /*
667 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
668 * Start CSP chip if no 16bit DMA channel is set - some kind
669 * of autorun or perhaps a bugfix?
670 */
671 spin_lock(&p->chip->mixer_lock);
672 status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
673 spin_unlock(&p->chip->mixer_lock);
674 if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
675 err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
676 set_codec_parameter(p->chip, 0xff, 0x00));
677 snd_sbdsp_reset(p->chip); /* really! */
678 if (err)
679 goto __fail;
680 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
681 set_mode_register(p->chip, 0x70); /* 70 = RUN */
682 }
683 }
684 result = 0;
685
686 __fail:
687 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
688 return result;
689}
690
Takashi Iwai029d64b2005-11-17 14:34:36 +0100691static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 int err = -ENOMEM;
694 unsigned char *kbuf = kmalloc(size, GFP_KERNEL);
695 if (kbuf) {
696 if (copy_from_user(kbuf, buf, size))
697 err = -EFAULT;
698 else
699 err = snd_sb_csp_load(p, kbuf, size, load_flags);
700 kfree(kbuf);
701 }
702 return err;
703}
704
Takashi Iwai8ad2da12007-02-26 15:55:43 +0100705#ifdef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706#include "sb16_csp_codecs.h"
707
Clemens Ladischde66d532006-11-06 09:18:34 +0100708static const struct firmware snd_sb_csp_static_programs[] = {
709 { .data = mulaw_main, .size = sizeof mulaw_main },
710 { .data = alaw_main, .size = sizeof alaw_main },
711 { .data = ima_adpcm_init, .size = sizeof ima_adpcm_init },
712 { .data = ima_adpcm_playback, .size = sizeof ima_adpcm_playback },
713 { .data = ima_adpcm_capture, .size = sizeof ima_adpcm_capture },
714};
715#endif
716
717static int snd_sb_csp_firmware_load(struct snd_sb_csp *p, int index, int flags)
718{
719 static const char *const names[] = {
720 "sb16/mulaw_main.csp",
721 "sb16/alaw_main.csp",
722 "sb16/ima_adpcm_init.csp",
723 "sb16/ima_adpcm_playback.csp",
724 "sb16/ima_adpcm_capture.csp",
725 };
726 const struct firmware *program;
Clemens Ladischde66d532006-11-06 09:18:34 +0100727
728 BUILD_BUG_ON(ARRAY_SIZE(names) != CSP_PROGRAM_COUNT);
729 program = p->csp_programs[index];
730 if (!program) {
Takashi Iwai8ad2da12007-02-26 15:55:43 +0100731#ifdef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200732 program = &snd_sb_csp_static_programs[index];
Clemens Ladischde66d532006-11-06 09:18:34 +0100733#else
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200734 int err = request_firmware(&program, names[index],
735 p->chip->card->dev);
736 if (err < 0)
Clemens Ladischde66d532006-11-06 09:18:34 +0100737 return err;
738#endif
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200739 p->csp_programs[index] = program;
Clemens Ladischde66d532006-11-06 09:18:34 +0100740 }
741 return snd_sb_csp_load(p, program->data, program->size, flags);
742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/*
745 * autoload hardware codec if necessary
746 * return 0 if CSP is loaded and ready to run (p->running != 0)
747 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100748static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 unsigned long flags;
751 int err = 0;
752
753 /* if CSP is running or manually loaded then exit */
754 if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED))
755 return -EBUSY;
756
757 /* autoload microcode only if requested hardware codec is not already loaded */
758 if (((1 << pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
759 p->running = SNDRV_SB_CSP_ST_AUTO;
760 } else {
761 switch (pcm_sfmt) {
762 case SNDRV_PCM_FORMAT_MU_LAW:
Clemens Ladischde66d532006-11-06 09:18:34 +0100763 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_MULAW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
765 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
766 break;
767 case SNDRV_PCM_FORMAT_A_LAW:
Clemens Ladischde66d532006-11-06 09:18:34 +0100768 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ALAW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
770 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
771 break;
772 case SNDRV_PCM_FORMAT_IMA_ADPCM:
Clemens Ladischde66d532006-11-06 09:18:34 +0100773 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ADPCM_INIT,
774 SNDRV_SB_CSP_LOAD_INITBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (err)
776 break;
777 if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
Clemens Ladischde66d532006-11-06 09:18:34 +0100778 err = snd_sb_csp_firmware_load
779 (p, CSP_PROGRAM_ADPCM_PLAYBACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
781 } else {
Clemens Ladischde66d532006-11-06 09:18:34 +0100782 err = snd_sb_csp_firmware_load
783 (p, CSP_PROGRAM_ADPCM_CAPTURE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
785 }
786 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
787 break;
788 default:
789 /* Decouple CSP from IRQ and DMAREQ lines */
790 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
791 spin_lock_irqsave(&p->chip->reg_lock, flags);
792 set_mode_register(p->chip, 0xfc);
793 set_mode_register(p->chip, 0x00);
794 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
795 p->running = 0; /* clear autoloaded flag */
796 }
797 return -EINVAL;
798 }
799 if (err) {
800 p->acc_format = 0;
801 p->acc_channels = p->acc_width = p->acc_rates = 0;
802
803 p->running = 0; /* clear autoloaded flag */
804 p->mode = 0;
805 return (err);
806 } else {
807 p->running = SNDRV_SB_CSP_ST_AUTO; /* set autoloaded flag */
808 p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT; /* only 16 bit data */
809 p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
810 p->acc_rates = SNDRV_SB_CSP_RATE_ALL; /* HW codecs accept all rates */
811 }
812
813 }
814 return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
815}
816
817/*
818 * start CSP
819 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100820static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 unsigned char s_type; /* sample type */
823 unsigned char mixL, mixR;
824 int result = -EIO;
825 unsigned long flags;
826
827 if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
828 snd_printd("%s: Microcode not loaded\n", __FUNCTION__);
829 return -ENXIO;
830 }
831 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
832 snd_printd("%s: CSP already running\n", __FUNCTION__);
833 return -EBUSY;
834 }
835 if (!(sample_width & p->acc_width)) {
836 snd_printd("%s: Unsupported PCM sample width\n", __FUNCTION__);
837 return -EINVAL;
838 }
839 if (!(channels & p->acc_channels)) {
840 snd_printd("%s: Invalid number of channels\n", __FUNCTION__);
841 return -EINVAL;
842 }
843
844 /* Mute PCM volume */
845 spin_lock_irqsave(&p->chip->mixer_lock, flags);
846 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
847 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
848 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
849 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
850
851 spin_lock(&p->chip->reg_lock);
852 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
853 set_mode_register(p->chip, 0x70); /* 70 = RUN */
854
855 s_type = 0x00;
856 if (channels == SNDRV_SB_CSP_MONO)
857 s_type = 0x11; /* 000n 000n (n = 1 if mono) */
858 if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
859 s_type |= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
860
861 if (set_codec_parameter(p->chip, 0x81, s_type)) {
862 snd_printd("%s: Set sample type command failed\n", __FUNCTION__);
863 goto __fail;
864 }
865 if (set_codec_parameter(p->chip, 0x80, 0x00)) {
866 snd_printd("%s: Codec start command failed\n", __FUNCTION__);
867 goto __fail;
868 }
869 p->run_width = sample_width;
870 p->run_channels = channels;
871
872 p->running |= SNDRV_SB_CSP_ST_RUNNING;
873
874 if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
875 set_codec_parameter(p->chip, 0xe0, 0x01);
876 /* enable QSound decoder */
877 set_codec_parameter(p->chip, 0x00, 0xff);
878 set_codec_parameter(p->chip, 0x01, 0xff);
879 p->running |= SNDRV_SB_CSP_ST_QSOUND;
880 /* set QSound startup value */
881 snd_sb_csp_qsound_transfer(p);
882 }
883 result = 0;
884
885 __fail:
886 spin_unlock(&p->chip->reg_lock);
887
888 /* restore PCM volume */
889 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
890 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
891 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
892
893 return result;
894}
895
896/*
897 * stop CSP
898 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100899static int snd_sb_csp_stop(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 int result;
902 unsigned char mixL, mixR;
903 unsigned long flags;
904
905 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
906 return 0;
907
908 /* Mute PCM volume */
909 spin_lock_irqsave(&p->chip->mixer_lock, flags);
910 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
911 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
912 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
913 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
914
915 spin_lock(&p->chip->reg_lock);
916 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
917 set_codec_parameter(p->chip, 0xe0, 0x01);
918 /* disable QSound decoder */
919 set_codec_parameter(p->chip, 0x00, 0x00);
920 set_codec_parameter(p->chip, 0x01, 0x00);
921
922 p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
923 }
924 result = set_mode_register(p->chip, 0xc0); /* c0 = STOP */
925 spin_unlock(&p->chip->reg_lock);
926
927 /* restore PCM volume */
928 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
929 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
930 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
931
932 if (!(result))
933 p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
934 return result;
935}
936
937/*
938 * pause CSP codec and hold DMA transfer
939 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100940static int snd_sb_csp_pause(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 int result;
943 unsigned long flags;
944
945 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
946 return -EBUSY;
947
948 spin_lock_irqsave(&p->chip->reg_lock, flags);
949 result = set_codec_parameter(p->chip, 0x80, 0xff);
950 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
951 if (!(result))
952 p->running |= SNDRV_SB_CSP_ST_PAUSED;
953
954 return result;
955}
956
957/*
958 * restart CSP codec and resume DMA transfer
959 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100960static int snd_sb_csp_restart(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 int result;
963 unsigned long flags;
964
965 if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
966 return -EBUSY;
967
968 spin_lock_irqsave(&p->chip->reg_lock, flags);
969 result = set_codec_parameter(p->chip, 0x80, 0x00);
970 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
971 if (!(result))
972 p->running &= ~SNDRV_SB_CSP_ST_PAUSED;
973
974 return result;
975}
976
977/* ------------------------------ */
978
979/*
980 * QSound mixer control for PCM
981 */
982
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200983#define snd_sb_qsound_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Takashi Iwai029d64b2005-11-17 14:34:36 +0100985static int snd_sb_qsound_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100987 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
990 return 0;
991}
992
Takashi Iwai029d64b2005-11-17 14:34:36 +0100993static int snd_sb_qsound_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100995 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 unsigned long flags;
997 int change;
998 unsigned char nval;
999
1000 nval = ucontrol->value.integer.value[0] & 0x01;
1001 spin_lock_irqsave(&p->q_lock, flags);
1002 change = p->q_enabled != nval;
1003 p->q_enabled = nval;
1004 spin_unlock_irqrestore(&p->q_lock, flags);
1005 return change;
1006}
1007
Takashi Iwai029d64b2005-11-17 14:34:36 +01001008static int snd_sb_qsound_space_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1011 uinfo->count = 2;
1012 uinfo->value.integer.min = 0;
1013 uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1014 return 0;
1015}
1016
Takashi Iwai029d64b2005-11-17 14:34:36 +01001017static int snd_sb_qsound_space_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001019 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 unsigned long flags;
1021
1022 spin_lock_irqsave(&p->q_lock, flags);
1023 ucontrol->value.integer.value[0] = p->qpos_left;
1024 ucontrol->value.integer.value[1] = p->qpos_right;
1025 spin_unlock_irqrestore(&p->q_lock, flags);
1026 return 0;
1027}
1028
Takashi Iwai029d64b2005-11-17 14:34:36 +01001029static int snd_sb_qsound_space_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001031 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 unsigned long flags;
1033 int change;
1034 unsigned char nval1, nval2;
1035
1036 nval1 = ucontrol->value.integer.value[0];
1037 if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1038 nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1039 nval2 = ucontrol->value.integer.value[1];
1040 if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1041 nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1042 spin_lock_irqsave(&p->q_lock, flags);
1043 change = p->qpos_left != nval1 || p->qpos_right != nval2;
1044 p->qpos_left = nval1;
1045 p->qpos_right = nval2;
1046 p->qpos_changed = change;
1047 spin_unlock_irqrestore(&p->q_lock, flags);
1048 return change;
1049}
1050
Takashi Iwai029d64b2005-11-17 14:34:36 +01001051static struct snd_kcontrol_new snd_sb_qsound_switch = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1053 .name = "3D Control - Switch",
1054 .info = snd_sb_qsound_switch_info,
1055 .get = snd_sb_qsound_switch_get,
1056 .put = snd_sb_qsound_switch_put
1057};
1058
Takashi Iwai029d64b2005-11-17 14:34:36 +01001059static struct snd_kcontrol_new snd_sb_qsound_space = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1061 .name = "3D Control - Space",
1062 .info = snd_sb_qsound_space_info,
1063 .get = snd_sb_qsound_space_get,
1064 .put = snd_sb_qsound_space_put
1065};
1066
Takashi Iwai029d64b2005-11-17 14:34:36 +01001067static int snd_sb_qsound_build(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001069 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 int err;
1071
1072 snd_assert(p != NULL, return -EINVAL);
1073
1074 card = p->chip->card;
1075 p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
1076 p->qpos_changed = 0;
1077
1078 spin_lock_init(&p->q_lock);
1079
1080 if ((err = snd_ctl_add(card, p->qsound_switch = snd_ctl_new1(&snd_sb_qsound_switch, p))) < 0)
1081 goto __error;
1082 if ((err = snd_ctl_add(card, p->qsound_space = snd_ctl_new1(&snd_sb_qsound_space, p))) < 0)
1083 goto __error;
1084
1085 return 0;
1086
1087 __error:
1088 snd_sb_qsound_destroy(p);
1089 return err;
1090}
1091
Takashi Iwai029d64b2005-11-17 14:34:36 +01001092static void snd_sb_qsound_destroy(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001094 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 unsigned long flags;
1096
1097 snd_assert(p != NULL, return);
1098
1099 card = p->chip->card;
1100
1101 down_write(&card->controls_rwsem);
1102 if (p->qsound_switch)
1103 snd_ctl_remove(card, p->qsound_switch);
1104 if (p->qsound_space)
1105 snd_ctl_remove(card, p->qsound_space);
1106 up_write(&card->controls_rwsem);
1107
1108 /* cancel pending transfer of QSound parameters */
1109 spin_lock_irqsave (&p->q_lock, flags);
1110 p->qpos_changed = 0;
1111 spin_unlock_irqrestore (&p->q_lock, flags);
1112}
1113
1114/*
1115 * Transfer qsound parameters to CSP,
1116 * function should be called from interrupt routine
1117 */
Takashi Iwai029d64b2005-11-17 14:34:36 +01001118static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 int err = -ENXIO;
1121
1122 spin_lock(&p->q_lock);
1123 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1124 set_codec_parameter(p->chip, 0xe0, 0x01);
1125 /* left channel */
1126 set_codec_parameter(p->chip, 0x00, p->qpos_left);
1127 set_codec_parameter(p->chip, 0x02, 0x00);
1128 /* right channel */
1129 set_codec_parameter(p->chip, 0x00, p->qpos_right);
1130 set_codec_parameter(p->chip, 0x03, 0x00);
1131 err = 0;
1132 }
1133 p->qpos_changed = 0;
1134 spin_unlock(&p->q_lock);
1135 return err;
1136}
1137
1138/* ------------------------------ */
1139
1140/*
1141 * proc interface
1142 */
Takashi Iwai029d64b2005-11-17 14:34:36 +01001143static int init_proc_entry(struct snd_sb_csp * p, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 char name[16];
Takashi Iwai029d64b2005-11-17 14:34:36 +01001146 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 sprintf(name, "cspD%d", device);
1148 if (! snd_card_proc_new(p->chip->card, name, &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001149 snd_info_set_text_ops(entry, p, info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return 0;
1151}
1152
Takashi Iwai029d64b2005-11-17 14:34:36 +01001153static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001155 struct snd_sb_csp *p = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
1158 snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
1159 ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
1160 ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
1161 ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
1162 if (p->running & SNDRV_SB_CSP_ST_LOADED) {
1163 snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
1164 snd_iprintf(buffer, "Sample rates: ");
1165 if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
1166 snd_iprintf(buffer, "All\n");
1167 } else {
1168 snd_iprintf(buffer, "%s%s%s%s\n",
1169 ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
1170 ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
1171 ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
1172 ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
1173 }
1174 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
1175 snd_iprintf(buffer, "QSound decoder %sabled\n",
1176 p->q_enabled ? "en" : "dis");
1177 } else {
1178 snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
1179 p->acc_format,
1180 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
1181 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
1182 ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
1183 ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
1184 ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
1185 ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
1186 }
1187 }
1188 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
1189 snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1190 }
1191 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
1192 snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
1193 ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
1194 ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
1195 }
1196 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1197 snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
1198 p->qpos_left, p->qpos_right);
1199 }
1200}
1201
1202/* */
1203
1204EXPORT_SYMBOL(snd_sb_csp_new);
1205
1206/*
1207 * INIT part
1208 */
1209
1210static int __init alsa_sb_csp_init(void)
1211{
1212 return 0;
1213}
1214
1215static void __exit alsa_sb_csp_exit(void)
1216{
1217}
1218
1219module_init(alsa_sb_csp_init)
1220module_exit(alsa_sb_csp_exit)