blob: 9703c68e4e0803e5c7fe0259b7ec7cff8d46604b [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");
39
40#ifdef SNDRV_LITTLE_ENDIAN
41#define CSP_HDR_VALUE(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
42#else
43#define CSP_HDR_VALUE(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
44#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define RIFF_HEADER CSP_HDR_VALUE('R', 'I', 'F', 'F')
47#define CSP__HEADER CSP_HDR_VALUE('C', 'S', 'P', ' ')
48#define LIST_HEADER CSP_HDR_VALUE('L', 'I', 'S', 'T')
49#define FUNC_HEADER CSP_HDR_VALUE('f', 'u', 'n', 'c')
50#define CODE_HEADER CSP_HDR_VALUE('c', 'o', 'd', 'e')
51#define INIT_HEADER CSP_HDR_VALUE('i', 'n', 'i', 't')
52#define MAIN_HEADER CSP_HDR_VALUE('m', 'a', 'i', 'n')
53
54/*
55 * RIFF data format
56 */
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070057struct riff_header {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 __u32 name;
59 __u32 len;
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070060};
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070062struct desc_header {
63 struct riff_header info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 __u16 func_nr;
65 __u16 VOC_type;
66 __u16 flags_play_rec;
67 __u16 flags_16bit_8bit;
68 __u16 flags_stereo_mono;
69 __u16 flags_rates;
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070070};
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72/*
73 * prototypes
74 */
Takashi Iwai029d64b2005-11-17 14:34:36 +010075static void snd_sb_csp_free(struct snd_hwdep *hw);
76static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file);
77static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg);
78static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Takashi Iwai029d64b2005-11-17 14:34:36 +010080static int csp_detect(struct snd_sb *chip, int *version);
81static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val);
82static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val);
83static int read_register(struct snd_sb *chip, unsigned char reg);
84static int set_mode_register(struct snd_sb *chip, unsigned char mode);
85static int get_version(struct snd_sb *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Takashi Iwai029d64b2005-11-17 14:34:36 +010087static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
88 struct snd_sb_csp_microcode __user * code);
89static int snd_sb_csp_unload(struct snd_sb_csp * p);
90static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags);
91static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode);
92static int snd_sb_csp_check_version(struct snd_sb_csp * p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Takashi Iwai029d64b2005-11-17 14:34:36 +010094static int snd_sb_csp_use(struct snd_sb_csp * p);
95static int snd_sb_csp_unuse(struct snd_sb_csp * p);
96static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels);
97static int snd_sb_csp_stop(struct snd_sb_csp * p);
98static int snd_sb_csp_pause(struct snd_sb_csp * p);
99static int snd_sb_csp_restart(struct snd_sb_csp * p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Takashi Iwai029d64b2005-11-17 14:34:36 +0100101static int snd_sb_qsound_build(struct snd_sb_csp * p);
102static void snd_sb_qsound_destroy(struct snd_sb_csp * p);
103static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Takashi Iwai029d64b2005-11-17 14:34:36 +0100105static int init_proc_entry(struct snd_sb_csp * p, int device);
106static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
109 * Detect CSP chip and create a new instance
110 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100111int snd_sb_csp_new(struct snd_sb *chip, int device, struct snd_hwdep ** rhwdep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100113 struct snd_sb_csp *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int version, err;
Takashi Iwai029d64b2005-11-17 14:34:36 +0100115 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 if (rhwdep)
118 *rhwdep = NULL;
119
120 if (csp_detect(chip, &version))
121 return -ENODEV;
122
123 if ((err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw)) < 0)
124 return err;
125
Takashi Iwai9e76a762005-09-09 14:21:17 +0200126 if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 snd_device_free(chip->card, hw);
128 return -ENOMEM;
129 }
130 p->chip = chip;
131 p->version = version;
132
133 /* CSP operators */
134 p->ops.csp_use = snd_sb_csp_use;
135 p->ops.csp_unuse = snd_sb_csp_unuse;
136 p->ops.csp_autoload = snd_sb_csp_autoload;
137 p->ops.csp_start = snd_sb_csp_start;
138 p->ops.csp_stop = snd_sb_csp_stop;
139 p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;
140
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100141 mutex_init(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f));
143 hw->iface = SNDRV_HWDEP_IFACE_SB16CSP;
144 hw->private_data = p;
145 hw->private_free = snd_sb_csp_free;
146
147 /* operators - only write/ioctl */
148 hw->ops.open = snd_sb_csp_open;
149 hw->ops.ioctl = snd_sb_csp_ioctl;
150 hw->ops.release = snd_sb_csp_release;
151
152 /* create a proc entry */
153 init_proc_entry(p, device);
154 if (rhwdep)
155 *rhwdep = hw;
156 return 0;
157}
158
159/*
160 * free_private for hwdep instance
161 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100162static void snd_sb_csp_free(struct snd_hwdep *hwdep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100164 struct snd_sb_csp *p = hwdep->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (p) {
166 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
167 snd_sb_csp_stop(p);
168 kfree(p);
169 }
170}
171
172/* ------------------------------ */
173
174/*
175 * open the device exclusively
176 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100177static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100179 struct snd_sb_csp *p = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return (snd_sb_csp_use(p));
181}
182
183/*
184 * ioctl for hwdep device:
185 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100186static 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 -0700187{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100188 struct snd_sb_csp *p = hw->private_data;
189 struct snd_sb_csp_info info;
190 struct snd_sb_csp_start start_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int err;
192
193 snd_assert(p != NULL, return -EINVAL);
194
195 if (snd_sb_csp_check_version(p))
196 return -ENODEV;
197
198 switch (cmd) {
199 /* get information */
200 case SNDRV_SB_CSP_IOCTL_INFO:
201 *info.codec_name = *p->codec_name;
202 info.func_nr = p->func_nr;
203 info.acc_format = p->acc_format;
204 info.acc_channels = p->acc_channels;
205 info.acc_width = p->acc_width;
206 info.acc_rates = p->acc_rates;
207 info.csp_mode = p->mode;
208 info.run_channels = p->run_channels;
209 info.run_width = p->run_width;
210 info.version = p->version;
211 info.state = p->running;
212 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
213 err = -EFAULT;
214 else
215 err = 0;
216 break;
217
218 /* load CSP microcode */
219 case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
220 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
Takashi Iwai029d64b2005-11-17 14:34:36 +0100221 -EBUSY : snd_sb_csp_riff_load(p, (struct snd_sb_csp_microcode __user *) arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 break;
223 case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
224 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
225 -EBUSY : snd_sb_csp_unload(p));
226 break;
227
228 /* change CSP running state */
229 case SNDRV_SB_CSP_IOCTL_START:
230 if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info)))
231 err = -EFAULT;
232 else
233 err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
234 break;
235 case SNDRV_SB_CSP_IOCTL_STOP:
236 err = snd_sb_csp_stop(p);
237 break;
238 case SNDRV_SB_CSP_IOCTL_PAUSE:
239 err = snd_sb_csp_pause(p);
240 break;
241 case SNDRV_SB_CSP_IOCTL_RESTART:
242 err = snd_sb_csp_restart(p);
243 break;
244 default:
245 err = -ENOTTY;
246 break;
247 }
248
249 return err;
250}
251
252/*
253 * close the device
254 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100255static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100257 struct snd_sb_csp *p = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return (snd_sb_csp_unuse(p));
259}
260
261/* ------------------------------ */
262
263/*
264 * acquire device
265 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100266static int snd_sb_csp_use(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100268 mutex_lock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (p->used) {
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100270 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return -EAGAIN;
272 }
273 p->used++;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100274 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 return 0;
277
278}
279
280/*
281 * release device
282 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100283static int snd_sb_csp_unuse(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100285 mutex_lock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 p->used--;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100287 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 return 0;
290}
291
292/*
293 * load microcode via ioctl:
294 * code is user-space pointer
295 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100296static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
297 struct snd_sb_csp_microcode __user * mcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100299 struct snd_sb_csp_mc_header info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 unsigned char __user *data_ptr;
302 unsigned char __user *data_end;
303 unsigned short func_nr = 0;
304
Alexey Dobriyandfc866e2005-09-10 00:26:35 -0700305 struct riff_header file_h, item_h, code_h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 __u32 item_type;
Alexey Dobriyandfc866e2005-09-10 00:26:35 -0700307 struct desc_header funcdesc_h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 unsigned long flags;
310 int err;
311
312 if (copy_from_user(&info, mcode, sizeof(info)))
313 return -EFAULT;
314 data_ptr = mcode->data;
315
316 if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
317 return -EFAULT;
318 if ((file_h.name != RIFF_HEADER) ||
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700319 (le32_to_cpu(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 snd_printd("%s: Invalid RIFF header\n", __FUNCTION__);
321 return -EINVAL;
322 }
323 data_ptr += sizeof(file_h);
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700324 data_end = data_ptr + le32_to_cpu(file_h.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
327 return -EFAULT;
328 if (item_type != CSP__HEADER) {
329 snd_printd("%s: Invalid RIFF file type\n", __FUNCTION__);
330 return -EINVAL;
331 }
332 data_ptr += sizeof (item_type);
333
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700334 for (; data_ptr < data_end; data_ptr += le32_to_cpu(item_h.len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
336 return -EFAULT;
337 data_ptr += sizeof(item_h);
338 if (item_h.name != LIST_HEADER)
339 continue;
340
341 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
342 return -EFAULT;
343 switch (item_type) {
344 case FUNC_HEADER:
345 if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
346 return -EFAULT;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700347 func_nr = le16_to_cpu(funcdesc_h.func_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 break;
349 case CODE_HEADER:
350 if (func_nr != info.func_req)
351 break; /* not required function, try next */
352 data_ptr += sizeof(item_type);
353
354 /* destroy QSound mixer element */
355 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
356 snd_sb_qsound_destroy(p);
357 }
358 /* Clear all flags */
359 p->running = 0;
360 p->mode = 0;
361
362 /* load microcode blocks */
363 for (;;) {
364 if (data_ptr >= data_end)
365 return -EINVAL;
366 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
367 return -EFAULT;
368
369 /* init microcode blocks */
370 if (code_h.name != INIT_HEADER)
371 break;
372 data_ptr += sizeof(code_h);
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700373 err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 SNDRV_SB_CSP_LOAD_INITBLOCK);
375 if (err)
376 return err;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700377 data_ptr += le32_to_cpu(code_h.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379 /* main microcode block */
380 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
381 return -EFAULT;
382
383 if (code_h.name != MAIN_HEADER) {
384 snd_printd("%s: Missing 'main' microcode\n", __FUNCTION__);
385 return -EINVAL;
386 }
387 data_ptr += sizeof(code_h);
388 err = snd_sb_csp_load_user(p, data_ptr,
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700389 le32_to_cpu(code_h.len), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (err)
391 return err;
392
393 /* fill in codec header */
394 strlcpy(p->codec_name, info.codec_name, sizeof(p->codec_name));
395 p->func_nr = func_nr;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700396 p->mode = le16_to_cpu(funcdesc_h.flags_play_rec);
397 switch (le16_to_cpu(funcdesc_h.VOC_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 case 0x0001: /* QSound decoder */
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700399 if (le16_to_cpu(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (snd_sb_qsound_build(p) == 0)
401 /* set QSound flag and clear all other mode flags */
402 p->mode = SNDRV_SB_CSP_MODE_QSOUND;
403 }
404 p->acc_format = 0;
405 break;
406 case 0x0006: /* A Law codec */
407 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
408 break;
409 case 0x0007: /* Mu Law codec */
410 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
411 break;
412 case 0x0011: /* what Creative thinks is IMA ADPCM codec */
413 case 0x0200: /* Creative ADPCM codec */
414 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
415 break;
416 case 201: /* Text 2 Speech decoder */
417 /* TODO: Text2Speech handling routines */
418 p->acc_format = 0;
419 break;
420 case 0x0202: /* Fast Speech 8 codec */
421 case 0x0203: /* Fast Speech 10 codec */
422 p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
423 break;
424 default: /* other codecs are unsupported */
425 p->acc_format = p->acc_width = p->acc_rates = 0;
426 p->mode = 0;
427 snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
428 __FUNCTION__,
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700429 le16_to_cpu(funcdesc_h.VOC_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return -EINVAL;
431 }
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700432 p->acc_channels = le16_to_cpu(funcdesc_h.flags_stereo_mono);
433 p->acc_width = le16_to_cpu(funcdesc_h.flags_16bit_8bit);
434 p->acc_rates = le16_to_cpu(funcdesc_h.flags_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /* Decouple CSP from IRQ and DMAREQ lines */
437 spin_lock_irqsave(&p->chip->reg_lock, flags);
438 set_mode_register(p->chip, 0xfc);
439 set_mode_register(p->chip, 0x00);
440 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
441
442 /* finished loading successfully */
443 p->running = SNDRV_SB_CSP_ST_LOADED; /* set LOADED flag */
444 return 0;
445 }
446 }
447 snd_printd("%s: Function #%d not found\n", __FUNCTION__, info.func_req);
448 return -EINVAL;
449}
450
451/*
452 * unload CSP microcode
453 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100454static int snd_sb_csp_unload(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
457 return -EBUSY;
458 if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
459 return -ENXIO;
460
461 /* clear supported formats */
462 p->acc_format = 0;
463 p->acc_channels = p->acc_width = p->acc_rates = 0;
464 /* destroy QSound mixer element */
465 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
466 snd_sb_qsound_destroy(p);
467 }
468 /* clear all flags */
469 p->running = 0;
470 p->mode = 0;
471 return 0;
472}
473
474/*
475 * send command sequence to DSP
476 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100477static inline int command_seq(struct snd_sb *chip, const unsigned char *seq, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 int i;
480 for (i = 0; i < size; i++) {
481 if (!snd_sbdsp_command(chip, seq[i]))
482 return -EIO;
483 }
484 return 0;
485}
486
487/*
488 * set CSP codec parameter
489 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100490static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 unsigned char dsp_cmd[3];
493
494 dsp_cmd[0] = 0x05; /* CSP set codec parameter */
495 dsp_cmd[1] = val; /* Parameter value */
496 dsp_cmd[2] = par; /* Parameter */
497 command_seq(chip, dsp_cmd, 3);
498 snd_sbdsp_command(chip, 0x03); /* DSP read? */
499 if (snd_sbdsp_get_byte(chip) != par)
500 return -EIO;
501 return 0;
502}
503
504/*
505 * set CSP register
506 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100507static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 unsigned char dsp_cmd[3];
510
511 dsp_cmd[0] = 0x0e; /* CSP set register */
512 dsp_cmd[1] = reg; /* CSP Register */
513 dsp_cmd[2] = val; /* value */
514 return command_seq(chip, dsp_cmd, 3);
515}
516
517/*
518 * read CSP register
519 * return < 0 -> error
520 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100521static int read_register(struct snd_sb *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 unsigned char dsp_cmd[2];
524
525 dsp_cmd[0] = 0x0f; /* CSP read register */
526 dsp_cmd[1] = reg; /* CSP Register */
527 command_seq(chip, dsp_cmd, 2);
528 return snd_sbdsp_get_byte(chip); /* Read DSP value */
529}
530
531/*
532 * set CSP mode register
533 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100534static int set_mode_register(struct snd_sb *chip, unsigned char mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 unsigned char dsp_cmd[2];
537
538 dsp_cmd[0] = 0x04; /* CSP set mode register */
539 dsp_cmd[1] = mode; /* mode */
540 return command_seq(chip, dsp_cmd, 2);
541}
542
543/*
544 * Detect CSP
545 * return 0 if CSP exists.
546 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100547static int csp_detect(struct snd_sb *chip, int *version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 unsigned char csp_test1, csp_test2;
550 unsigned long flags;
551 int result = -ENODEV;
552
553 spin_lock_irqsave(&chip->reg_lock, flags);
554
555 set_codec_parameter(chip, 0x00, 0x00);
556 set_mode_register(chip, 0xfc); /* 0xfc = ?? */
557
558 csp_test1 = read_register(chip, 0x83);
559 set_register(chip, 0x83, ~csp_test1);
560 csp_test2 = read_register(chip, 0x83);
561 if (csp_test2 != (csp_test1 ^ 0xff))
562 goto __fail;
563
564 set_register(chip, 0x83, csp_test1);
565 csp_test2 = read_register(chip, 0x83);
566 if (csp_test2 != csp_test1)
567 goto __fail;
568
569 set_mode_register(chip, 0x00); /* 0x00 = ? */
570
571 *version = get_version(chip);
572 snd_sbdsp_reset(chip); /* reset DSP after getversion! */
573 if (*version >= 0x10 && *version <= 0x1f)
574 result = 0; /* valid version id */
575
576 __fail:
577 spin_unlock_irqrestore(&chip->reg_lock, flags);
578 return result;
579}
580
581/*
582 * get CSP version number
583 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100584static int get_version(struct snd_sb *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 unsigned char dsp_cmd[2];
587
588 dsp_cmd[0] = 0x08; /* SB_DSP_!something! */
589 dsp_cmd[1] = 0x03; /* get chip version id? */
590 command_seq(chip, dsp_cmd, 2);
591
592 return (snd_sbdsp_get_byte(chip));
593}
594
595/*
596 * check if the CSP version is valid
597 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100598static int snd_sb_csp_check_version(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 if (p->version < 0x10 || p->version > 0x1f) {
601 snd_printd("%s: Invalid CSP version: 0x%x\n", __FUNCTION__, p->version);
602 return 1;
603 }
604 return 0;
605}
606
607/*
608 * download microcode to CSP (microcode should have one "main" block).
609 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100610static 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 -0700611{
612 int status, i;
613 int err;
614 int result = -EIO;
615 unsigned long flags;
616
617 spin_lock_irqsave(&p->chip->reg_lock, flags);
618 snd_sbdsp_command(p->chip, 0x01); /* CSP download command */
619 if (snd_sbdsp_get_byte(p->chip)) {
620 snd_printd("%s: Download command failed\n", __FUNCTION__);
621 goto __fail;
622 }
623 /* Send CSP low byte (size - 1) */
624 snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
625 /* Send high byte */
626 snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
627 /* send microcode sequence */
628 /* load from kernel space */
629 while (size--) {
630 if (!snd_sbdsp_command(p->chip, *buf++))
631 goto __fail;
632 }
633 if (snd_sbdsp_get_byte(p->chip))
634 goto __fail;
635
636 if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
637 i = 0;
638 /* some codecs (FastSpeech) take some time to initialize */
639 while (1) {
640 snd_sbdsp_command(p->chip, 0x03);
641 status = snd_sbdsp_get_byte(p->chip);
642 if (status == 0x55 || ++i >= 10)
643 break;
644 udelay (10);
645 }
646 if (status != 0x55) {
647 snd_printd("%s: Microcode initialization failed\n", __FUNCTION__);
648 goto __fail;
649 }
650 } else {
651 /*
652 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
653 * Start CSP chip if no 16bit DMA channel is set - some kind
654 * of autorun or perhaps a bugfix?
655 */
656 spin_lock(&p->chip->mixer_lock);
657 status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
658 spin_unlock(&p->chip->mixer_lock);
659 if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
660 err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
661 set_codec_parameter(p->chip, 0xff, 0x00));
662 snd_sbdsp_reset(p->chip); /* really! */
663 if (err)
664 goto __fail;
665 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
666 set_mode_register(p->chip, 0x70); /* 70 = RUN */
667 }
668 }
669 result = 0;
670
671 __fail:
672 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
673 return result;
674}
675
Takashi Iwai029d64b2005-11-17 14:34:36 +0100676static 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 -0700677{
678 int err = -ENOMEM;
679 unsigned char *kbuf = kmalloc(size, GFP_KERNEL);
680 if (kbuf) {
681 if (copy_from_user(kbuf, buf, size))
682 err = -EFAULT;
683 else
684 err = snd_sb_csp_load(p, kbuf, size, load_flags);
685 kfree(kbuf);
686 }
687 return err;
688}
689
690#include "sb16_csp_codecs.h"
691
692/*
693 * autoload hardware codec if necessary
694 * return 0 if CSP is loaded and ready to run (p->running != 0)
695 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100696static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 unsigned long flags;
699 int err = 0;
700
701 /* if CSP is running or manually loaded then exit */
702 if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED))
703 return -EBUSY;
704
705 /* autoload microcode only if requested hardware codec is not already loaded */
706 if (((1 << pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
707 p->running = SNDRV_SB_CSP_ST_AUTO;
708 } else {
709 switch (pcm_sfmt) {
710 case SNDRV_PCM_FORMAT_MU_LAW:
711 err = snd_sb_csp_load(p, &mulaw_main[0], sizeof(mulaw_main), 0);
712 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
713 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
714 break;
715 case SNDRV_PCM_FORMAT_A_LAW:
716 err = snd_sb_csp_load(p, &alaw_main[0], sizeof(alaw_main), 0);
717 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
718 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
719 break;
720 case SNDRV_PCM_FORMAT_IMA_ADPCM:
721 err = snd_sb_csp_load(p, &ima_adpcm_init[0], sizeof(ima_adpcm_init),
722 SNDRV_SB_CSP_LOAD_INITBLOCK);
723 if (err)
724 break;
725 if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
726 err = snd_sb_csp_load(p, &ima_adpcm_playback[0],
727 sizeof(ima_adpcm_playback), 0);
728 p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
729 } else {
730 err = snd_sb_csp_load(p, &ima_adpcm_capture[0],
731 sizeof(ima_adpcm_capture), 0);
732 p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
733 }
734 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
735 break;
736 default:
737 /* Decouple CSP from IRQ and DMAREQ lines */
738 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
739 spin_lock_irqsave(&p->chip->reg_lock, flags);
740 set_mode_register(p->chip, 0xfc);
741 set_mode_register(p->chip, 0x00);
742 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
743 p->running = 0; /* clear autoloaded flag */
744 }
745 return -EINVAL;
746 }
747 if (err) {
748 p->acc_format = 0;
749 p->acc_channels = p->acc_width = p->acc_rates = 0;
750
751 p->running = 0; /* clear autoloaded flag */
752 p->mode = 0;
753 return (err);
754 } else {
755 p->running = SNDRV_SB_CSP_ST_AUTO; /* set autoloaded flag */
756 p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT; /* only 16 bit data */
757 p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
758 p->acc_rates = SNDRV_SB_CSP_RATE_ALL; /* HW codecs accept all rates */
759 }
760
761 }
762 return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
763}
764
765/*
766 * start CSP
767 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100768static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 unsigned char s_type; /* sample type */
771 unsigned char mixL, mixR;
772 int result = -EIO;
773 unsigned long flags;
774
775 if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
776 snd_printd("%s: Microcode not loaded\n", __FUNCTION__);
777 return -ENXIO;
778 }
779 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
780 snd_printd("%s: CSP already running\n", __FUNCTION__);
781 return -EBUSY;
782 }
783 if (!(sample_width & p->acc_width)) {
784 snd_printd("%s: Unsupported PCM sample width\n", __FUNCTION__);
785 return -EINVAL;
786 }
787 if (!(channels & p->acc_channels)) {
788 snd_printd("%s: Invalid number of channels\n", __FUNCTION__);
789 return -EINVAL;
790 }
791
792 /* Mute PCM volume */
793 spin_lock_irqsave(&p->chip->mixer_lock, flags);
794 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
795 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
796 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
797 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
798
799 spin_lock(&p->chip->reg_lock);
800 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
801 set_mode_register(p->chip, 0x70); /* 70 = RUN */
802
803 s_type = 0x00;
804 if (channels == SNDRV_SB_CSP_MONO)
805 s_type = 0x11; /* 000n 000n (n = 1 if mono) */
806 if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
807 s_type |= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
808
809 if (set_codec_parameter(p->chip, 0x81, s_type)) {
810 snd_printd("%s: Set sample type command failed\n", __FUNCTION__);
811 goto __fail;
812 }
813 if (set_codec_parameter(p->chip, 0x80, 0x00)) {
814 snd_printd("%s: Codec start command failed\n", __FUNCTION__);
815 goto __fail;
816 }
817 p->run_width = sample_width;
818 p->run_channels = channels;
819
820 p->running |= SNDRV_SB_CSP_ST_RUNNING;
821
822 if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
823 set_codec_parameter(p->chip, 0xe0, 0x01);
824 /* enable QSound decoder */
825 set_codec_parameter(p->chip, 0x00, 0xff);
826 set_codec_parameter(p->chip, 0x01, 0xff);
827 p->running |= SNDRV_SB_CSP_ST_QSOUND;
828 /* set QSound startup value */
829 snd_sb_csp_qsound_transfer(p);
830 }
831 result = 0;
832
833 __fail:
834 spin_unlock(&p->chip->reg_lock);
835
836 /* restore PCM volume */
837 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
838 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
839 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
840
841 return result;
842}
843
844/*
845 * stop CSP
846 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100847static int snd_sb_csp_stop(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 int result;
850 unsigned char mixL, mixR;
851 unsigned long flags;
852
853 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
854 return 0;
855
856 /* Mute PCM volume */
857 spin_lock_irqsave(&p->chip->mixer_lock, flags);
858 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
859 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
860 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
861 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
862
863 spin_lock(&p->chip->reg_lock);
864 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
865 set_codec_parameter(p->chip, 0xe0, 0x01);
866 /* disable QSound decoder */
867 set_codec_parameter(p->chip, 0x00, 0x00);
868 set_codec_parameter(p->chip, 0x01, 0x00);
869
870 p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
871 }
872 result = set_mode_register(p->chip, 0xc0); /* c0 = STOP */
873 spin_unlock(&p->chip->reg_lock);
874
875 /* restore PCM volume */
876 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
877 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
878 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
879
880 if (!(result))
881 p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
882 return result;
883}
884
885/*
886 * pause CSP codec and hold DMA transfer
887 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100888static int snd_sb_csp_pause(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 int result;
891 unsigned long flags;
892
893 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
894 return -EBUSY;
895
896 spin_lock_irqsave(&p->chip->reg_lock, flags);
897 result = set_codec_parameter(p->chip, 0x80, 0xff);
898 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
899 if (!(result))
900 p->running |= SNDRV_SB_CSP_ST_PAUSED;
901
902 return result;
903}
904
905/*
906 * restart CSP codec and resume DMA transfer
907 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100908static int snd_sb_csp_restart(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 int result;
911 unsigned long flags;
912
913 if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
914 return -EBUSY;
915
916 spin_lock_irqsave(&p->chip->reg_lock, flags);
917 result = set_codec_parameter(p->chip, 0x80, 0x00);
918 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
919 if (!(result))
920 p->running &= ~SNDRV_SB_CSP_ST_PAUSED;
921
922 return result;
923}
924
925/* ------------------------------ */
926
927/*
928 * QSound mixer control for PCM
929 */
930
Takashi Iwai029d64b2005-11-17 14:34:36 +0100931static int snd_sb_qsound_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
934 uinfo->count = 1;
935 uinfo->value.integer.min = 0;
936 uinfo->value.integer.max = 1;
937 return 0;
938}
939
Takashi Iwai029d64b2005-11-17 14:34:36 +0100940static int snd_sb_qsound_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100942 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
945 return 0;
946}
947
Takashi Iwai029d64b2005-11-17 14:34:36 +0100948static int snd_sb_qsound_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100950 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 unsigned long flags;
952 int change;
953 unsigned char nval;
954
955 nval = ucontrol->value.integer.value[0] & 0x01;
956 spin_lock_irqsave(&p->q_lock, flags);
957 change = p->q_enabled != nval;
958 p->q_enabled = nval;
959 spin_unlock_irqrestore(&p->q_lock, flags);
960 return change;
961}
962
Takashi Iwai029d64b2005-11-17 14:34:36 +0100963static int snd_sb_qsound_space_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
966 uinfo->count = 2;
967 uinfo->value.integer.min = 0;
968 uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
969 return 0;
970}
971
Takashi Iwai029d64b2005-11-17 14:34:36 +0100972static int snd_sb_qsound_space_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100974 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 unsigned long flags;
976
977 spin_lock_irqsave(&p->q_lock, flags);
978 ucontrol->value.integer.value[0] = p->qpos_left;
979 ucontrol->value.integer.value[1] = p->qpos_right;
980 spin_unlock_irqrestore(&p->q_lock, flags);
981 return 0;
982}
983
Takashi Iwai029d64b2005-11-17 14:34:36 +0100984static int snd_sb_qsound_space_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100986 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 unsigned long flags;
988 int change;
989 unsigned char nval1, nval2;
990
991 nval1 = ucontrol->value.integer.value[0];
992 if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
993 nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
994 nval2 = ucontrol->value.integer.value[1];
995 if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
996 nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
997 spin_lock_irqsave(&p->q_lock, flags);
998 change = p->qpos_left != nval1 || p->qpos_right != nval2;
999 p->qpos_left = nval1;
1000 p->qpos_right = nval2;
1001 p->qpos_changed = change;
1002 spin_unlock_irqrestore(&p->q_lock, flags);
1003 return change;
1004}
1005
Takashi Iwai029d64b2005-11-17 14:34:36 +01001006static struct snd_kcontrol_new snd_sb_qsound_switch = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1008 .name = "3D Control - Switch",
1009 .info = snd_sb_qsound_switch_info,
1010 .get = snd_sb_qsound_switch_get,
1011 .put = snd_sb_qsound_switch_put
1012};
1013
Takashi Iwai029d64b2005-11-17 14:34:36 +01001014static struct snd_kcontrol_new snd_sb_qsound_space = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1016 .name = "3D Control - Space",
1017 .info = snd_sb_qsound_space_info,
1018 .get = snd_sb_qsound_space_get,
1019 .put = snd_sb_qsound_space_put
1020};
1021
Takashi Iwai029d64b2005-11-17 14:34:36 +01001022static int snd_sb_qsound_build(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001024 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 int err;
1026
1027 snd_assert(p != NULL, return -EINVAL);
1028
1029 card = p->chip->card;
1030 p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
1031 p->qpos_changed = 0;
1032
1033 spin_lock_init(&p->q_lock);
1034
1035 if ((err = snd_ctl_add(card, p->qsound_switch = snd_ctl_new1(&snd_sb_qsound_switch, p))) < 0)
1036 goto __error;
1037 if ((err = snd_ctl_add(card, p->qsound_space = snd_ctl_new1(&snd_sb_qsound_space, p))) < 0)
1038 goto __error;
1039
1040 return 0;
1041
1042 __error:
1043 snd_sb_qsound_destroy(p);
1044 return err;
1045}
1046
Takashi Iwai029d64b2005-11-17 14:34:36 +01001047static void snd_sb_qsound_destroy(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001049 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 unsigned long flags;
1051
1052 snd_assert(p != NULL, return);
1053
1054 card = p->chip->card;
1055
1056 down_write(&card->controls_rwsem);
1057 if (p->qsound_switch)
1058 snd_ctl_remove(card, p->qsound_switch);
1059 if (p->qsound_space)
1060 snd_ctl_remove(card, p->qsound_space);
1061 up_write(&card->controls_rwsem);
1062
1063 /* cancel pending transfer of QSound parameters */
1064 spin_lock_irqsave (&p->q_lock, flags);
1065 p->qpos_changed = 0;
1066 spin_unlock_irqrestore (&p->q_lock, flags);
1067}
1068
1069/*
1070 * Transfer qsound parameters to CSP,
1071 * function should be called from interrupt routine
1072 */
Takashi Iwai029d64b2005-11-17 14:34:36 +01001073static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 int err = -ENXIO;
1076
1077 spin_lock(&p->q_lock);
1078 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1079 set_codec_parameter(p->chip, 0xe0, 0x01);
1080 /* left channel */
1081 set_codec_parameter(p->chip, 0x00, p->qpos_left);
1082 set_codec_parameter(p->chip, 0x02, 0x00);
1083 /* right channel */
1084 set_codec_parameter(p->chip, 0x00, p->qpos_right);
1085 set_codec_parameter(p->chip, 0x03, 0x00);
1086 err = 0;
1087 }
1088 p->qpos_changed = 0;
1089 spin_unlock(&p->q_lock);
1090 return err;
1091}
1092
1093/* ------------------------------ */
1094
1095/*
1096 * proc interface
1097 */
Takashi Iwai029d64b2005-11-17 14:34:36 +01001098static int init_proc_entry(struct snd_sb_csp * p, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 char name[16];
Takashi Iwai029d64b2005-11-17 14:34:36 +01001101 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 sprintf(name, "cspD%d", device);
1103 if (! snd_card_proc_new(p->chip->card, name, &entry))
1104 snd_info_set_text_ops(entry, p, 1024, info_read);
1105 return 0;
1106}
1107
Takashi Iwai029d64b2005-11-17 14:34:36 +01001108static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001110 struct snd_sb_csp *p = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
1113 snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
1114 ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
1115 ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
1116 ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
1117 if (p->running & SNDRV_SB_CSP_ST_LOADED) {
1118 snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
1119 snd_iprintf(buffer, "Sample rates: ");
1120 if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
1121 snd_iprintf(buffer, "All\n");
1122 } else {
1123 snd_iprintf(buffer, "%s%s%s%s\n",
1124 ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
1125 ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
1126 ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
1127 ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
1128 }
1129 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
1130 snd_iprintf(buffer, "QSound decoder %sabled\n",
1131 p->q_enabled ? "en" : "dis");
1132 } else {
1133 snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
1134 p->acc_format,
1135 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
1136 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
1137 ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
1138 ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
1139 ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
1140 ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
1141 }
1142 }
1143 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
1144 snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1145 }
1146 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
1147 snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
1148 ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
1149 ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
1150 }
1151 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1152 snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
1153 p->qpos_left, p->qpos_right);
1154 }
1155}
1156
1157/* */
1158
1159EXPORT_SYMBOL(snd_sb_csp_new);
1160
1161/*
1162 * INIT part
1163 */
1164
1165static int __init alsa_sb_csp_init(void)
1166{
1167 return 0;
1168}
1169
1170static void __exit alsa_sb_csp_exit(void)
1171{
1172}
1173
1174module_init(alsa_sb_csp_init)
1175module_exit(alsa_sb_csp_exit)