blob: 49037d074c719f8e2ef2160b1d8730af1dec2cf6 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/delay.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <sound/core.h>
30#include <sound/control.h>
31#include <sound/info.h>
32#include <sound/sb16_csp.h>
33#include <sound/initval.h>
34
35MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
36MODULE_DESCRIPTION("ALSA driver for SB16 Creative Signal Processor");
37MODULE_LICENSE("GPL");
Clemens Ladisch7e0af292007-05-03 17:59:54 +020038MODULE_FIRMWARE("sb16/mulaw_main.csp");
39MODULE_FIRMWARE("sb16/alaw_main.csp");
40MODULE_FIRMWARE("sb16/ima_adpcm_init.csp");
41MODULE_FIRMWARE("sb16/ima_adpcm_playback.csp");
42MODULE_FIRMWARE("sb16/ima_adpcm_capture.csp");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#ifdef SNDRV_LITTLE_ENDIAN
45#define CSP_HDR_VALUE(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
46#else
47#define CSP_HDR_VALUE(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
48#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define RIFF_HEADER CSP_HDR_VALUE('R', 'I', 'F', 'F')
51#define CSP__HEADER CSP_HDR_VALUE('C', 'S', 'P', ' ')
52#define LIST_HEADER CSP_HDR_VALUE('L', 'I', 'S', 'T')
53#define FUNC_HEADER CSP_HDR_VALUE('f', 'u', 'n', 'c')
54#define CODE_HEADER CSP_HDR_VALUE('c', 'o', 'd', 'e')
55#define INIT_HEADER CSP_HDR_VALUE('i', 'n', 'i', 't')
56#define MAIN_HEADER CSP_HDR_VALUE('m', 'a', 'i', 'n')
57
58/*
59 * RIFF data format
60 */
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070061struct riff_header {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 __u32 name;
63 __u32 len;
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070064};
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070066struct desc_header {
67 struct riff_header info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 __u16 func_nr;
69 __u16 VOC_type;
70 __u16 flags_play_rec;
71 __u16 flags_16bit_8bit;
72 __u16 flags_stereo_mono;
73 __u16 flags_rates;
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070074};
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/*
77 * prototypes
78 */
Takashi Iwai029d64b2005-11-17 14:34:36 +010079static void snd_sb_csp_free(struct snd_hwdep *hw);
80static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file);
81static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg);
82static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Takashi Iwai029d64b2005-11-17 14:34:36 +010084static int csp_detect(struct snd_sb *chip, int *version);
85static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val);
86static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val);
87static int read_register(struct snd_sb *chip, unsigned char reg);
88static int set_mode_register(struct snd_sb *chip, unsigned char mode);
89static int get_version(struct snd_sb *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Takashi Iwai029d64b2005-11-17 14:34:36 +010091static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
92 struct snd_sb_csp_microcode __user * code);
93static int snd_sb_csp_unload(struct snd_sb_csp * p);
94static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags);
95static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode);
96static int snd_sb_csp_check_version(struct snd_sb_csp * p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Takashi Iwai029d64b2005-11-17 14:34:36 +010098static int snd_sb_csp_use(struct snd_sb_csp * p);
99static int snd_sb_csp_unuse(struct snd_sb_csp * p);
100static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels);
101static int snd_sb_csp_stop(struct snd_sb_csp * p);
102static int snd_sb_csp_pause(struct snd_sb_csp * p);
103static int snd_sb_csp_restart(struct snd_sb_csp * p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Takashi Iwai029d64b2005-11-17 14:34:36 +0100105static int snd_sb_qsound_build(struct snd_sb_csp * p);
106static void snd_sb_qsound_destroy(struct snd_sb_csp * p);
107static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Takashi Iwai029d64b2005-11-17 14:34:36 +0100109static int init_proc_entry(struct snd_sb_csp * p, int device);
110static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/*
113 * Detect CSP chip and create a new instance
114 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100115int snd_sb_csp_new(struct snd_sb *chip, int device, struct snd_hwdep ** rhwdep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100117 struct snd_sb_csp *p;
Takashi Iwaicd0b4ac2007-12-14 12:18:52 +0100118 int uninitialized_var(version);
119 int err;
Takashi Iwai029d64b2005-11-17 14:34:36 +0100120 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (rhwdep)
123 *rhwdep = NULL;
124
125 if (csp_detect(chip, &version))
126 return -ENODEV;
127
128 if ((err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw)) < 0)
129 return err;
130
Takashi Iwai9e76a762005-09-09 14:21:17 +0200131 if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 snd_device_free(chip->card, hw);
133 return -ENOMEM;
134 }
135 p->chip = chip;
136 p->version = version;
137
138 /* CSP operators */
139 p->ops.csp_use = snd_sb_csp_use;
140 p->ops.csp_unuse = snd_sb_csp_unuse;
141 p->ops.csp_autoload = snd_sb_csp_autoload;
142 p->ops.csp_start = snd_sb_csp_start;
143 p->ops.csp_stop = snd_sb_csp_stop;
144 p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;
145
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100146 mutex_init(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f));
148 hw->iface = SNDRV_HWDEP_IFACE_SB16CSP;
149 hw->private_data = p;
150 hw->private_free = snd_sb_csp_free;
151
152 /* operators - only write/ioctl */
153 hw->ops.open = snd_sb_csp_open;
154 hw->ops.ioctl = snd_sb_csp_ioctl;
155 hw->ops.release = snd_sb_csp_release;
156
157 /* create a proc entry */
158 init_proc_entry(p, device);
159 if (rhwdep)
160 *rhwdep = hw;
161 return 0;
162}
163
164/*
165 * free_private for hwdep instance
166 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100167static void snd_sb_csp_free(struct snd_hwdep *hwdep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Clemens Ladischde66d532006-11-06 09:18:34 +0100169 int i;
Takashi Iwai029d64b2005-11-17 14:34:36 +0100170 struct snd_sb_csp *p = hwdep->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (p) {
172 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
173 snd_sb_csp_stop(p);
Clemens Ladischde66d532006-11-06 09:18:34 +0100174 for (i = 0; i < ARRAY_SIZE(p->csp_programs); ++i)
175 release_firmware(p->csp_programs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 kfree(p);
177 }
178}
179
180/* ------------------------------ */
181
182/*
183 * open the device exclusively
184 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100185static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100187 struct snd_sb_csp *p = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return (snd_sb_csp_use(p));
189}
190
191/*
192 * ioctl for hwdep device:
193 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100194static 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 -0700195{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100196 struct snd_sb_csp *p = hw->private_data;
197 struct snd_sb_csp_info info;
198 struct snd_sb_csp_start start_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int err;
200
Takashi Iwai622207d2008-08-08 17:11:45 +0200201 if (snd_BUG_ON(!p))
202 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 if (snd_sb_csp_check_version(p))
205 return -ENODEV;
206
207 switch (cmd) {
208 /* get information */
209 case SNDRV_SB_CSP_IOCTL_INFO:
210 *info.codec_name = *p->codec_name;
211 info.func_nr = p->func_nr;
212 info.acc_format = p->acc_format;
213 info.acc_channels = p->acc_channels;
214 info.acc_width = p->acc_width;
215 info.acc_rates = p->acc_rates;
216 info.csp_mode = p->mode;
217 info.run_channels = p->run_channels;
218 info.run_width = p->run_width;
219 info.version = p->version;
220 info.state = p->running;
221 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
222 err = -EFAULT;
223 else
224 err = 0;
225 break;
226
227 /* load CSP microcode */
228 case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
229 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
Takashi Iwai029d64b2005-11-17 14:34:36 +0100230 -EBUSY : snd_sb_csp_riff_load(p, (struct snd_sb_csp_microcode __user *) arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 break;
232 case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
233 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
234 -EBUSY : snd_sb_csp_unload(p));
235 break;
236
237 /* change CSP running state */
238 case SNDRV_SB_CSP_IOCTL_START:
239 if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info)))
240 err = -EFAULT;
241 else
242 err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
243 break;
244 case SNDRV_SB_CSP_IOCTL_STOP:
245 err = snd_sb_csp_stop(p);
246 break;
247 case SNDRV_SB_CSP_IOCTL_PAUSE:
248 err = snd_sb_csp_pause(p);
249 break;
250 case SNDRV_SB_CSP_IOCTL_RESTART:
251 err = snd_sb_csp_restart(p);
252 break;
253 default:
254 err = -ENOTTY;
255 break;
256 }
257
258 return err;
259}
260
261/*
262 * close the device
263 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100264static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100266 struct snd_sb_csp *p = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return (snd_sb_csp_unuse(p));
268}
269
270/* ------------------------------ */
271
272/*
273 * acquire device
274 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100275static int snd_sb_csp_use(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100277 mutex_lock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (p->used) {
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100279 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return -EAGAIN;
281 }
282 p->used++;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100283 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 return 0;
286
287}
288
289/*
290 * release device
291 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100292static int snd_sb_csp_unuse(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100294 mutex_lock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 p->used--;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100296 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 return 0;
299}
300
301/*
302 * load microcode via ioctl:
303 * code is user-space pointer
304 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100305static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
306 struct snd_sb_csp_microcode __user * mcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100308 struct snd_sb_csp_mc_header info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 unsigned char __user *data_ptr;
311 unsigned char __user *data_end;
312 unsigned short func_nr = 0;
313
Alexey Dobriyandfc866e2005-09-10 00:26:35 -0700314 struct riff_header file_h, item_h, code_h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 __u32 item_type;
Alexey Dobriyandfc866e2005-09-10 00:26:35 -0700316 struct desc_header funcdesc_h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 unsigned long flags;
319 int err;
320
321 if (copy_from_user(&info, mcode, sizeof(info)))
322 return -EFAULT;
323 data_ptr = mcode->data;
324
325 if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
326 return -EFAULT;
327 if ((file_h.name != RIFF_HEADER) ||
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700328 (le32_to_cpu(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800329 snd_printd("%s: Invalid RIFF header\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return -EINVAL;
331 }
332 data_ptr += sizeof(file_h);
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700333 data_end = data_ptr + le32_to_cpu(file_h.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
336 return -EFAULT;
337 if (item_type != CSP__HEADER) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800338 snd_printd("%s: Invalid RIFF file type\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return -EINVAL;
340 }
341 data_ptr += sizeof (item_type);
342
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700343 for (; data_ptr < data_end; data_ptr += le32_to_cpu(item_h.len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
345 return -EFAULT;
346 data_ptr += sizeof(item_h);
347 if (item_h.name != LIST_HEADER)
348 continue;
349
350 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
351 return -EFAULT;
352 switch (item_type) {
353 case FUNC_HEADER:
354 if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
355 return -EFAULT;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700356 func_nr = le16_to_cpu(funcdesc_h.func_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 break;
358 case CODE_HEADER:
359 if (func_nr != info.func_req)
360 break; /* not required function, try next */
361 data_ptr += sizeof(item_type);
362
363 /* destroy QSound mixer element */
364 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
365 snd_sb_qsound_destroy(p);
366 }
367 /* Clear all flags */
368 p->running = 0;
369 p->mode = 0;
370
371 /* load microcode blocks */
372 for (;;) {
373 if (data_ptr >= data_end)
374 return -EINVAL;
375 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
376 return -EFAULT;
377
378 /* init microcode blocks */
379 if (code_h.name != INIT_HEADER)
380 break;
381 data_ptr += sizeof(code_h);
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700382 err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 SNDRV_SB_CSP_LOAD_INITBLOCK);
384 if (err)
385 return err;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700386 data_ptr += le32_to_cpu(code_h.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 /* main microcode block */
389 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
390 return -EFAULT;
391
392 if (code_h.name != MAIN_HEADER) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800393 snd_printd("%s: Missing 'main' microcode\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return -EINVAL;
395 }
396 data_ptr += sizeof(code_h);
397 err = snd_sb_csp_load_user(p, data_ptr,
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700398 le32_to_cpu(code_h.len), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (err)
400 return err;
401
402 /* fill in codec header */
403 strlcpy(p->codec_name, info.codec_name, sizeof(p->codec_name));
404 p->func_nr = func_nr;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700405 p->mode = le16_to_cpu(funcdesc_h.flags_play_rec);
406 switch (le16_to_cpu(funcdesc_h.VOC_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 case 0x0001: /* QSound decoder */
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700408 if (le16_to_cpu(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (snd_sb_qsound_build(p) == 0)
410 /* set QSound flag and clear all other mode flags */
411 p->mode = SNDRV_SB_CSP_MODE_QSOUND;
412 }
413 p->acc_format = 0;
414 break;
415 case 0x0006: /* A Law codec */
416 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
417 break;
418 case 0x0007: /* Mu Law codec */
419 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
420 break;
421 case 0x0011: /* what Creative thinks is IMA ADPCM codec */
422 case 0x0200: /* Creative ADPCM codec */
423 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
424 break;
425 case 201: /* Text 2 Speech decoder */
426 /* TODO: Text2Speech handling routines */
427 p->acc_format = 0;
428 break;
429 case 0x0202: /* Fast Speech 8 codec */
430 case 0x0203: /* Fast Speech 10 codec */
431 p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
432 break;
433 default: /* other codecs are unsupported */
434 p->acc_format = p->acc_width = p->acc_rates = 0;
435 p->mode = 0;
436 snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800437 __func__,
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700438 le16_to_cpu(funcdesc_h.VOC_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return -EINVAL;
440 }
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700441 p->acc_channels = le16_to_cpu(funcdesc_h.flags_stereo_mono);
442 p->acc_width = le16_to_cpu(funcdesc_h.flags_16bit_8bit);
443 p->acc_rates = le16_to_cpu(funcdesc_h.flags_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 /* Decouple CSP from IRQ and DMAREQ lines */
446 spin_lock_irqsave(&p->chip->reg_lock, flags);
447 set_mode_register(p->chip, 0xfc);
448 set_mode_register(p->chip, 0x00);
449 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
450
451 /* finished loading successfully */
452 p->running = SNDRV_SB_CSP_ST_LOADED; /* set LOADED flag */
453 return 0;
454 }
455 }
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800456 snd_printd("%s: Function #%d not found\n", __func__, info.func_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return -EINVAL;
458}
459
460/*
461 * unload CSP microcode
462 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100463static int snd_sb_csp_unload(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
466 return -EBUSY;
467 if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
468 return -ENXIO;
469
470 /* clear supported formats */
471 p->acc_format = 0;
472 p->acc_channels = p->acc_width = p->acc_rates = 0;
473 /* destroy QSound mixer element */
474 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
475 snd_sb_qsound_destroy(p);
476 }
477 /* clear all flags */
478 p->running = 0;
479 p->mode = 0;
480 return 0;
481}
482
483/*
484 * send command sequence to DSP
485 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100486static inline int command_seq(struct snd_sb *chip, const unsigned char *seq, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 int i;
489 for (i = 0; i < size; i++) {
490 if (!snd_sbdsp_command(chip, seq[i]))
491 return -EIO;
492 }
493 return 0;
494}
495
496/*
497 * set CSP codec parameter
498 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100499static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 unsigned char dsp_cmd[3];
502
503 dsp_cmd[0] = 0x05; /* CSP set codec parameter */
504 dsp_cmd[1] = val; /* Parameter value */
505 dsp_cmd[2] = par; /* Parameter */
506 command_seq(chip, dsp_cmd, 3);
507 snd_sbdsp_command(chip, 0x03); /* DSP read? */
508 if (snd_sbdsp_get_byte(chip) != par)
509 return -EIO;
510 return 0;
511}
512
513/*
514 * set CSP register
515 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100516static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 unsigned char dsp_cmd[3];
519
520 dsp_cmd[0] = 0x0e; /* CSP set register */
521 dsp_cmd[1] = reg; /* CSP Register */
522 dsp_cmd[2] = val; /* value */
523 return command_seq(chip, dsp_cmd, 3);
524}
525
526/*
527 * read CSP register
528 * return < 0 -> error
529 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100530static int read_register(struct snd_sb *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 unsigned char dsp_cmd[2];
533
534 dsp_cmd[0] = 0x0f; /* CSP read register */
535 dsp_cmd[1] = reg; /* CSP Register */
536 command_seq(chip, dsp_cmd, 2);
537 return snd_sbdsp_get_byte(chip); /* Read DSP value */
538}
539
540/*
541 * set CSP mode register
542 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100543static int set_mode_register(struct snd_sb *chip, unsigned char mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 unsigned char dsp_cmd[2];
546
547 dsp_cmd[0] = 0x04; /* CSP set mode register */
548 dsp_cmd[1] = mode; /* mode */
549 return command_seq(chip, dsp_cmd, 2);
550}
551
552/*
553 * Detect CSP
554 * return 0 if CSP exists.
555 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100556static int csp_detect(struct snd_sb *chip, int *version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 unsigned char csp_test1, csp_test2;
559 unsigned long flags;
560 int result = -ENODEV;
561
562 spin_lock_irqsave(&chip->reg_lock, flags);
563
564 set_codec_parameter(chip, 0x00, 0x00);
565 set_mode_register(chip, 0xfc); /* 0xfc = ?? */
566
567 csp_test1 = read_register(chip, 0x83);
568 set_register(chip, 0x83, ~csp_test1);
569 csp_test2 = read_register(chip, 0x83);
570 if (csp_test2 != (csp_test1 ^ 0xff))
571 goto __fail;
572
573 set_register(chip, 0x83, csp_test1);
574 csp_test2 = read_register(chip, 0x83);
575 if (csp_test2 != csp_test1)
576 goto __fail;
577
578 set_mode_register(chip, 0x00); /* 0x00 = ? */
579
580 *version = get_version(chip);
581 snd_sbdsp_reset(chip); /* reset DSP after getversion! */
582 if (*version >= 0x10 && *version <= 0x1f)
583 result = 0; /* valid version id */
584
585 __fail:
586 spin_unlock_irqrestore(&chip->reg_lock, flags);
587 return result;
588}
589
590/*
591 * get CSP version number
592 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100593static int get_version(struct snd_sb *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 unsigned char dsp_cmd[2];
596
597 dsp_cmd[0] = 0x08; /* SB_DSP_!something! */
598 dsp_cmd[1] = 0x03; /* get chip version id? */
599 command_seq(chip, dsp_cmd, 2);
600
601 return (snd_sbdsp_get_byte(chip));
602}
603
604/*
605 * check if the CSP version is valid
606 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100607static int snd_sb_csp_check_version(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 if (p->version < 0x10 || p->version > 0x1f) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800610 snd_printd("%s: Invalid CSP version: 0x%x\n", __func__, p->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return 1;
612 }
613 return 0;
614}
615
616/*
617 * download microcode to CSP (microcode should have one "main" block).
618 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100619static 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 -0700620{
621 int status, i;
622 int err;
623 int result = -EIO;
624 unsigned long flags;
625
626 spin_lock_irqsave(&p->chip->reg_lock, flags);
627 snd_sbdsp_command(p->chip, 0x01); /* CSP download command */
628 if (snd_sbdsp_get_byte(p->chip)) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800629 snd_printd("%s: Download command failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 goto __fail;
631 }
632 /* Send CSP low byte (size - 1) */
633 snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
634 /* Send high byte */
635 snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
636 /* send microcode sequence */
637 /* load from kernel space */
638 while (size--) {
639 if (!snd_sbdsp_command(p->chip, *buf++))
640 goto __fail;
641 }
642 if (snd_sbdsp_get_byte(p->chip))
643 goto __fail;
644
645 if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
646 i = 0;
647 /* some codecs (FastSpeech) take some time to initialize */
648 while (1) {
649 snd_sbdsp_command(p->chip, 0x03);
650 status = snd_sbdsp_get_byte(p->chip);
651 if (status == 0x55 || ++i >= 10)
652 break;
653 udelay (10);
654 }
655 if (status != 0x55) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800656 snd_printd("%s: Microcode initialization failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto __fail;
658 }
659 } else {
660 /*
661 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
662 * Start CSP chip if no 16bit DMA channel is set - some kind
663 * of autorun or perhaps a bugfix?
664 */
665 spin_lock(&p->chip->mixer_lock);
666 status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
667 spin_unlock(&p->chip->mixer_lock);
668 if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
669 err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
670 set_codec_parameter(p->chip, 0xff, 0x00));
671 snd_sbdsp_reset(p->chip); /* really! */
672 if (err)
673 goto __fail;
674 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
675 set_mode_register(p->chip, 0x70); /* 70 = RUN */
676 }
677 }
678 result = 0;
679
680 __fail:
681 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
682 return result;
683}
684
Takashi Iwai029d64b2005-11-17 14:34:36 +0100685static 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 -0700686{
687 int err = -ENOMEM;
688 unsigned char *kbuf = kmalloc(size, GFP_KERNEL);
689 if (kbuf) {
690 if (copy_from_user(kbuf, buf, size))
691 err = -EFAULT;
692 else
693 err = snd_sb_csp_load(p, kbuf, size, load_flags);
694 kfree(kbuf);
695 }
696 return err;
697}
698
Clemens Ladischde66d532006-11-06 09:18:34 +0100699static int snd_sb_csp_firmware_load(struct snd_sb_csp *p, int index, int flags)
700{
701 static const char *const names[] = {
702 "sb16/mulaw_main.csp",
703 "sb16/alaw_main.csp",
704 "sb16/ima_adpcm_init.csp",
705 "sb16/ima_adpcm_playback.csp",
706 "sb16/ima_adpcm_capture.csp",
707 };
708 const struct firmware *program;
Clemens Ladischde66d532006-11-06 09:18:34 +0100709
710 BUILD_BUG_ON(ARRAY_SIZE(names) != CSP_PROGRAM_COUNT);
711 program = p->csp_programs[index];
712 if (!program) {
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200713 int err = request_firmware(&program, names[index],
714 p->chip->card->dev);
715 if (err < 0)
Clemens Ladischde66d532006-11-06 09:18:34 +0100716 return err;
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200717 p->csp_programs[index] = program;
Clemens Ladischde66d532006-11-06 09:18:34 +0100718 }
719 return snd_sb_csp_load(p, program->data, program->size, flags);
720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/*
723 * autoload hardware codec if necessary
724 * return 0 if CSP is loaded and ready to run (p->running != 0)
725 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100726static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 unsigned long flags;
729 int err = 0;
730
731 /* if CSP is running or manually loaded then exit */
732 if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED))
733 return -EBUSY;
734
735 /* autoload microcode only if requested hardware codec is not already loaded */
736 if (((1 << pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
737 p->running = SNDRV_SB_CSP_ST_AUTO;
738 } else {
739 switch (pcm_sfmt) {
740 case SNDRV_PCM_FORMAT_MU_LAW:
Clemens Ladischde66d532006-11-06 09:18:34 +0100741 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_MULAW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
743 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
744 break;
745 case SNDRV_PCM_FORMAT_A_LAW:
Clemens Ladischde66d532006-11-06 09:18:34 +0100746 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ALAW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
748 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
749 break;
750 case SNDRV_PCM_FORMAT_IMA_ADPCM:
Clemens Ladischde66d532006-11-06 09:18:34 +0100751 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ADPCM_INIT,
752 SNDRV_SB_CSP_LOAD_INITBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (err)
754 break;
755 if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
Clemens Ladischde66d532006-11-06 09:18:34 +0100756 err = snd_sb_csp_firmware_load
757 (p, CSP_PROGRAM_ADPCM_PLAYBACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
759 } else {
Clemens Ladischde66d532006-11-06 09:18:34 +0100760 err = snd_sb_csp_firmware_load
761 (p, CSP_PROGRAM_ADPCM_CAPTURE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
763 }
764 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
765 break;
766 default:
767 /* Decouple CSP from IRQ and DMAREQ lines */
768 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
769 spin_lock_irqsave(&p->chip->reg_lock, flags);
770 set_mode_register(p->chip, 0xfc);
771 set_mode_register(p->chip, 0x00);
772 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
773 p->running = 0; /* clear autoloaded flag */
774 }
775 return -EINVAL;
776 }
777 if (err) {
778 p->acc_format = 0;
779 p->acc_channels = p->acc_width = p->acc_rates = 0;
780
781 p->running = 0; /* clear autoloaded flag */
782 p->mode = 0;
783 return (err);
784 } else {
785 p->running = SNDRV_SB_CSP_ST_AUTO; /* set autoloaded flag */
786 p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT; /* only 16 bit data */
787 p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
788 p->acc_rates = SNDRV_SB_CSP_RATE_ALL; /* HW codecs accept all rates */
789 }
790
791 }
792 return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
793}
794
795/*
796 * start CSP
797 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100798static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 unsigned char s_type; /* sample type */
801 unsigned char mixL, mixR;
802 int result = -EIO;
803 unsigned long flags;
804
805 if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800806 snd_printd("%s: Microcode not loaded\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return -ENXIO;
808 }
809 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800810 snd_printd("%s: CSP already running\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return -EBUSY;
812 }
813 if (!(sample_width & p->acc_width)) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800814 snd_printd("%s: Unsupported PCM sample width\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return -EINVAL;
816 }
817 if (!(channels & p->acc_channels)) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800818 snd_printd("%s: Invalid number of channels\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return -EINVAL;
820 }
821
822 /* Mute PCM volume */
823 spin_lock_irqsave(&p->chip->mixer_lock, flags);
824 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
825 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
826 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
827 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
828
829 spin_lock(&p->chip->reg_lock);
830 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
831 set_mode_register(p->chip, 0x70); /* 70 = RUN */
832
833 s_type = 0x00;
834 if (channels == SNDRV_SB_CSP_MONO)
835 s_type = 0x11; /* 000n 000n (n = 1 if mono) */
836 if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
837 s_type |= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
838
839 if (set_codec_parameter(p->chip, 0x81, s_type)) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800840 snd_printd("%s: Set sample type command failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto __fail;
842 }
843 if (set_codec_parameter(p->chip, 0x80, 0x00)) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800844 snd_printd("%s: Codec start command failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 goto __fail;
846 }
847 p->run_width = sample_width;
848 p->run_channels = channels;
849
850 p->running |= SNDRV_SB_CSP_ST_RUNNING;
851
852 if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
853 set_codec_parameter(p->chip, 0xe0, 0x01);
854 /* enable QSound decoder */
855 set_codec_parameter(p->chip, 0x00, 0xff);
856 set_codec_parameter(p->chip, 0x01, 0xff);
857 p->running |= SNDRV_SB_CSP_ST_QSOUND;
858 /* set QSound startup value */
859 snd_sb_csp_qsound_transfer(p);
860 }
861 result = 0;
862
863 __fail:
864 spin_unlock(&p->chip->reg_lock);
865
866 /* restore PCM volume */
867 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
868 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
869 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
870
871 return result;
872}
873
874/*
875 * stop CSP
876 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100877static int snd_sb_csp_stop(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 int result;
880 unsigned char mixL, mixR;
881 unsigned long flags;
882
883 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
884 return 0;
885
886 /* Mute PCM volume */
887 spin_lock_irqsave(&p->chip->mixer_lock, flags);
888 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
889 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
890 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
891 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
892
893 spin_lock(&p->chip->reg_lock);
894 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
895 set_codec_parameter(p->chip, 0xe0, 0x01);
896 /* disable QSound decoder */
897 set_codec_parameter(p->chip, 0x00, 0x00);
898 set_codec_parameter(p->chip, 0x01, 0x00);
899
900 p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
901 }
902 result = set_mode_register(p->chip, 0xc0); /* c0 = STOP */
903 spin_unlock(&p->chip->reg_lock);
904
905 /* restore PCM volume */
906 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
907 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
908 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
909
910 if (!(result))
911 p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
912 return result;
913}
914
915/*
916 * pause CSP codec and hold DMA transfer
917 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100918static int snd_sb_csp_pause(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 int result;
921 unsigned long flags;
922
923 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
924 return -EBUSY;
925
926 spin_lock_irqsave(&p->chip->reg_lock, flags);
927 result = set_codec_parameter(p->chip, 0x80, 0xff);
928 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
929 if (!(result))
930 p->running |= SNDRV_SB_CSP_ST_PAUSED;
931
932 return result;
933}
934
935/*
936 * restart CSP codec and resume DMA transfer
937 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100938static int snd_sb_csp_restart(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 int result;
941 unsigned long flags;
942
943 if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
944 return -EBUSY;
945
946 spin_lock_irqsave(&p->chip->reg_lock, flags);
947 result = set_codec_parameter(p->chip, 0x80, 0x00);
948 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
949 if (!(result))
950 p->running &= ~SNDRV_SB_CSP_ST_PAUSED;
951
952 return result;
953}
954
955/* ------------------------------ */
956
957/*
958 * QSound mixer control for PCM
959 */
960
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200961#define snd_sb_qsound_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Takashi Iwai029d64b2005-11-17 14:34:36 +0100963static int snd_sb_qsound_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100965 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
968 return 0;
969}
970
Takashi Iwai029d64b2005-11-17 14:34:36 +0100971static int snd_sb_qsound_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100973 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 unsigned long flags;
975 int change;
976 unsigned char nval;
977
978 nval = ucontrol->value.integer.value[0] & 0x01;
979 spin_lock_irqsave(&p->q_lock, flags);
980 change = p->q_enabled != nval;
981 p->q_enabled = nval;
982 spin_unlock_irqrestore(&p->q_lock, flags);
983 return change;
984}
985
Takashi Iwai029d64b2005-11-17 14:34:36 +0100986static int snd_sb_qsound_space_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
989 uinfo->count = 2;
990 uinfo->value.integer.min = 0;
991 uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
992 return 0;
993}
994
Takashi Iwai029d64b2005-11-17 14:34:36 +0100995static int snd_sb_qsound_space_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100997 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 unsigned long flags;
999
1000 spin_lock_irqsave(&p->q_lock, flags);
1001 ucontrol->value.integer.value[0] = p->qpos_left;
1002 ucontrol->value.integer.value[1] = p->qpos_right;
1003 spin_unlock_irqrestore(&p->q_lock, flags);
1004 return 0;
1005}
1006
Takashi Iwai029d64b2005-11-17 14:34:36 +01001007static int snd_sb_qsound_space_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001009 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 unsigned long flags;
1011 int change;
1012 unsigned char nval1, nval2;
1013
1014 nval1 = ucontrol->value.integer.value[0];
1015 if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1016 nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1017 nval2 = ucontrol->value.integer.value[1];
1018 if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1019 nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1020 spin_lock_irqsave(&p->q_lock, flags);
1021 change = p->qpos_left != nval1 || p->qpos_right != nval2;
1022 p->qpos_left = nval1;
1023 p->qpos_right = nval2;
1024 p->qpos_changed = change;
1025 spin_unlock_irqrestore(&p->q_lock, flags);
1026 return change;
1027}
1028
Takashi Iwai029d64b2005-11-17 14:34:36 +01001029static struct snd_kcontrol_new snd_sb_qsound_switch = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1031 .name = "3D Control - Switch",
1032 .info = snd_sb_qsound_switch_info,
1033 .get = snd_sb_qsound_switch_get,
1034 .put = snd_sb_qsound_switch_put
1035};
1036
Takashi Iwai029d64b2005-11-17 14:34:36 +01001037static struct snd_kcontrol_new snd_sb_qsound_space = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1039 .name = "3D Control - Space",
1040 .info = snd_sb_qsound_space_info,
1041 .get = snd_sb_qsound_space_get,
1042 .put = snd_sb_qsound_space_put
1043};
1044
Takashi Iwai029d64b2005-11-17 14:34:36 +01001045static int snd_sb_qsound_build(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001047 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 int err;
1049
Takashi Iwai622207d2008-08-08 17:11:45 +02001050 if (snd_BUG_ON(!p))
1051 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 card = p->chip->card;
1054 p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
1055 p->qpos_changed = 0;
1056
1057 spin_lock_init(&p->q_lock);
1058
1059 if ((err = snd_ctl_add(card, p->qsound_switch = snd_ctl_new1(&snd_sb_qsound_switch, p))) < 0)
1060 goto __error;
1061 if ((err = snd_ctl_add(card, p->qsound_space = snd_ctl_new1(&snd_sb_qsound_space, p))) < 0)
1062 goto __error;
1063
1064 return 0;
1065
1066 __error:
1067 snd_sb_qsound_destroy(p);
1068 return err;
1069}
1070
Takashi Iwai029d64b2005-11-17 14:34:36 +01001071static void snd_sb_qsound_destroy(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001073 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 unsigned long flags;
1075
Takashi Iwai622207d2008-08-08 17:11:45 +02001076 if (snd_BUG_ON(!p))
1077 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 card = p->chip->card;
1080
1081 down_write(&card->controls_rwsem);
1082 if (p->qsound_switch)
1083 snd_ctl_remove(card, p->qsound_switch);
1084 if (p->qsound_space)
1085 snd_ctl_remove(card, p->qsound_space);
1086 up_write(&card->controls_rwsem);
1087
1088 /* cancel pending transfer of QSound parameters */
1089 spin_lock_irqsave (&p->q_lock, flags);
1090 p->qpos_changed = 0;
1091 spin_unlock_irqrestore (&p->q_lock, flags);
1092}
1093
1094/*
1095 * Transfer qsound parameters to CSP,
1096 * function should be called from interrupt routine
1097 */
Takashi Iwai029d64b2005-11-17 14:34:36 +01001098static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 int err = -ENXIO;
1101
1102 spin_lock(&p->q_lock);
1103 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1104 set_codec_parameter(p->chip, 0xe0, 0x01);
1105 /* left channel */
1106 set_codec_parameter(p->chip, 0x00, p->qpos_left);
1107 set_codec_parameter(p->chip, 0x02, 0x00);
1108 /* right channel */
1109 set_codec_parameter(p->chip, 0x00, p->qpos_right);
1110 set_codec_parameter(p->chip, 0x03, 0x00);
1111 err = 0;
1112 }
1113 p->qpos_changed = 0;
1114 spin_unlock(&p->q_lock);
1115 return err;
1116}
1117
1118/* ------------------------------ */
1119
1120/*
1121 * proc interface
1122 */
Takashi Iwai029d64b2005-11-17 14:34:36 +01001123static int init_proc_entry(struct snd_sb_csp * p, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 char name[16];
Takashi Iwai029d64b2005-11-17 14:34:36 +01001126 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 sprintf(name, "cspD%d", device);
1128 if (! snd_card_proc_new(p->chip->card, name, &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001129 snd_info_set_text_ops(entry, p, info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return 0;
1131}
1132
Takashi Iwai029d64b2005-11-17 14:34:36 +01001133static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001135 struct snd_sb_csp *p = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
1138 snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
1139 ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
1140 ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
1141 ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
1142 if (p->running & SNDRV_SB_CSP_ST_LOADED) {
1143 snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
1144 snd_iprintf(buffer, "Sample rates: ");
1145 if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
1146 snd_iprintf(buffer, "All\n");
1147 } else {
1148 snd_iprintf(buffer, "%s%s%s%s\n",
1149 ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
1150 ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
1151 ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
1152 ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
1153 }
1154 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
1155 snd_iprintf(buffer, "QSound decoder %sabled\n",
1156 p->q_enabled ? "en" : "dis");
1157 } else {
1158 snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
1159 p->acc_format,
1160 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
1161 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
1162 ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
1163 ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
1164 ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
1165 ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
1166 }
1167 }
1168 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
1169 snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1170 }
1171 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
1172 snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
1173 ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
1174 ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
1175 }
1176 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1177 snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
1178 p->qpos_left, p->qpos_right);
1179 }
1180}
1181
1182/* */
1183
1184EXPORT_SYMBOL(snd_sb_csp_new);
1185
1186/*
1187 * INIT part
1188 */
1189
1190static int __init alsa_sb_csp_init(void)
1191{
1192 return 0;
1193}
1194
1195static void __exit alsa_sb_csp_exit(void)
1196{
1197}
1198
1199module_init(alsa_sb_csp_init)
1200module_exit(alsa_sb_csp_exit)