blob: ef71e50744e982a4b5a89892370dae61f8d8cd96 [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{
Clemens Ladischde66d532006-11-06 09:18:34 +0100164 int i;
Takashi Iwai029d64b2005-11-17 14:34:36 +0100165 struct snd_sb_csp *p = hwdep->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (p) {
167 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
168 snd_sb_csp_stop(p);
Clemens Ladischde66d532006-11-06 09:18:34 +0100169 for (i = 0; i < ARRAY_SIZE(p->csp_programs); ++i)
170 release_firmware(p->csp_programs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 kfree(p);
172 }
173}
174
175/* ------------------------------ */
176
177/*
178 * open the device exclusively
179 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100180static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100182 struct snd_sb_csp *p = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return (snd_sb_csp_use(p));
184}
185
186/*
187 * ioctl for hwdep device:
188 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100189static 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 -0700190{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100191 struct snd_sb_csp *p = hw->private_data;
192 struct snd_sb_csp_info info;
193 struct snd_sb_csp_start start_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int err;
195
196 snd_assert(p != NULL, return -EINVAL);
197
198 if (snd_sb_csp_check_version(p))
199 return -ENODEV;
200
201 switch (cmd) {
202 /* get information */
203 case SNDRV_SB_CSP_IOCTL_INFO:
204 *info.codec_name = *p->codec_name;
205 info.func_nr = p->func_nr;
206 info.acc_format = p->acc_format;
207 info.acc_channels = p->acc_channels;
208 info.acc_width = p->acc_width;
209 info.acc_rates = p->acc_rates;
210 info.csp_mode = p->mode;
211 info.run_channels = p->run_channels;
212 info.run_width = p->run_width;
213 info.version = p->version;
214 info.state = p->running;
215 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
216 err = -EFAULT;
217 else
218 err = 0;
219 break;
220
221 /* load CSP microcode */
222 case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
223 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
Takashi Iwai029d64b2005-11-17 14:34:36 +0100224 -EBUSY : snd_sb_csp_riff_load(p, (struct snd_sb_csp_microcode __user *) arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 break;
226 case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
227 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
228 -EBUSY : snd_sb_csp_unload(p));
229 break;
230
231 /* change CSP running state */
232 case SNDRV_SB_CSP_IOCTL_START:
233 if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info)))
234 err = -EFAULT;
235 else
236 err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
237 break;
238 case SNDRV_SB_CSP_IOCTL_STOP:
239 err = snd_sb_csp_stop(p);
240 break;
241 case SNDRV_SB_CSP_IOCTL_PAUSE:
242 err = snd_sb_csp_pause(p);
243 break;
244 case SNDRV_SB_CSP_IOCTL_RESTART:
245 err = snd_sb_csp_restart(p);
246 break;
247 default:
248 err = -ENOTTY;
249 break;
250 }
251
252 return err;
253}
254
255/*
256 * close the device
257 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100258static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100260 struct snd_sb_csp *p = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return (snd_sb_csp_unuse(p));
262}
263
264/* ------------------------------ */
265
266/*
267 * acquire device
268 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100269static int snd_sb_csp_use(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100271 mutex_lock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (p->used) {
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100273 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return -EAGAIN;
275 }
276 p->used++;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100277 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 return 0;
280
281}
282
283/*
284 * release device
285 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100286static int snd_sb_csp_unuse(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100288 mutex_lock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 p->used--;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100290 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 return 0;
293}
294
295/*
296 * load microcode via ioctl:
297 * code is user-space pointer
298 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100299static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
300 struct snd_sb_csp_microcode __user * mcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100302 struct snd_sb_csp_mc_header info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 unsigned char __user *data_ptr;
305 unsigned char __user *data_end;
306 unsigned short func_nr = 0;
307
Alexey Dobriyandfc866e2005-09-10 00:26:35 -0700308 struct riff_header file_h, item_h, code_h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 __u32 item_type;
Alexey Dobriyandfc866e2005-09-10 00:26:35 -0700310 struct desc_header funcdesc_h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 unsigned long flags;
313 int err;
314
315 if (copy_from_user(&info, mcode, sizeof(info)))
316 return -EFAULT;
317 data_ptr = mcode->data;
318
319 if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
320 return -EFAULT;
321 if ((file_h.name != RIFF_HEADER) ||
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700322 (le32_to_cpu(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 snd_printd("%s: Invalid RIFF header\n", __FUNCTION__);
324 return -EINVAL;
325 }
326 data_ptr += sizeof(file_h);
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700327 data_end = data_ptr + le32_to_cpu(file_h.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
330 return -EFAULT;
331 if (item_type != CSP__HEADER) {
332 snd_printd("%s: Invalid RIFF file type\n", __FUNCTION__);
333 return -EINVAL;
334 }
335 data_ptr += sizeof (item_type);
336
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700337 for (; data_ptr < data_end; data_ptr += le32_to_cpu(item_h.len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
339 return -EFAULT;
340 data_ptr += sizeof(item_h);
341 if (item_h.name != LIST_HEADER)
342 continue;
343
344 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
345 return -EFAULT;
346 switch (item_type) {
347 case FUNC_HEADER:
348 if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
349 return -EFAULT;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700350 func_nr = le16_to_cpu(funcdesc_h.func_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 break;
352 case CODE_HEADER:
353 if (func_nr != info.func_req)
354 break; /* not required function, try next */
355 data_ptr += sizeof(item_type);
356
357 /* destroy QSound mixer element */
358 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
359 snd_sb_qsound_destroy(p);
360 }
361 /* Clear all flags */
362 p->running = 0;
363 p->mode = 0;
364
365 /* load microcode blocks */
366 for (;;) {
367 if (data_ptr >= data_end)
368 return -EINVAL;
369 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
370 return -EFAULT;
371
372 /* init microcode blocks */
373 if (code_h.name != INIT_HEADER)
374 break;
375 data_ptr += sizeof(code_h);
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700376 err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 SNDRV_SB_CSP_LOAD_INITBLOCK);
378 if (err)
379 return err;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700380 data_ptr += le32_to_cpu(code_h.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 /* main microcode block */
383 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
384 return -EFAULT;
385
386 if (code_h.name != MAIN_HEADER) {
387 snd_printd("%s: Missing 'main' microcode\n", __FUNCTION__);
388 return -EINVAL;
389 }
390 data_ptr += sizeof(code_h);
391 err = snd_sb_csp_load_user(p, data_ptr,
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700392 le32_to_cpu(code_h.len), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (err)
394 return err;
395
396 /* fill in codec header */
397 strlcpy(p->codec_name, info.codec_name, sizeof(p->codec_name));
398 p->func_nr = func_nr;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700399 p->mode = le16_to_cpu(funcdesc_h.flags_play_rec);
400 switch (le16_to_cpu(funcdesc_h.VOC_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 case 0x0001: /* QSound decoder */
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700402 if (le16_to_cpu(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (snd_sb_qsound_build(p) == 0)
404 /* set QSound flag and clear all other mode flags */
405 p->mode = SNDRV_SB_CSP_MODE_QSOUND;
406 }
407 p->acc_format = 0;
408 break;
409 case 0x0006: /* A Law codec */
410 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
411 break;
412 case 0x0007: /* Mu Law codec */
413 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
414 break;
415 case 0x0011: /* what Creative thinks is IMA ADPCM codec */
416 case 0x0200: /* Creative ADPCM codec */
417 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
418 break;
419 case 201: /* Text 2 Speech decoder */
420 /* TODO: Text2Speech handling routines */
421 p->acc_format = 0;
422 break;
423 case 0x0202: /* Fast Speech 8 codec */
424 case 0x0203: /* Fast Speech 10 codec */
425 p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
426 break;
427 default: /* other codecs are unsupported */
428 p->acc_format = p->acc_width = p->acc_rates = 0;
429 p->mode = 0;
430 snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
431 __FUNCTION__,
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700432 le16_to_cpu(funcdesc_h.VOC_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return -EINVAL;
434 }
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700435 p->acc_channels = le16_to_cpu(funcdesc_h.flags_stereo_mono);
436 p->acc_width = le16_to_cpu(funcdesc_h.flags_16bit_8bit);
437 p->acc_rates = le16_to_cpu(funcdesc_h.flags_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /* Decouple CSP from IRQ and DMAREQ lines */
440 spin_lock_irqsave(&p->chip->reg_lock, flags);
441 set_mode_register(p->chip, 0xfc);
442 set_mode_register(p->chip, 0x00);
443 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
444
445 /* finished loading successfully */
446 p->running = SNDRV_SB_CSP_ST_LOADED; /* set LOADED flag */
447 return 0;
448 }
449 }
450 snd_printd("%s: Function #%d not found\n", __FUNCTION__, info.func_req);
451 return -EINVAL;
452}
453
454/*
455 * unload CSP microcode
456 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100457static int snd_sb_csp_unload(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
460 return -EBUSY;
461 if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
462 return -ENXIO;
463
464 /* clear supported formats */
465 p->acc_format = 0;
466 p->acc_channels = p->acc_width = p->acc_rates = 0;
467 /* destroy QSound mixer element */
468 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
469 snd_sb_qsound_destroy(p);
470 }
471 /* clear all flags */
472 p->running = 0;
473 p->mode = 0;
474 return 0;
475}
476
477/*
478 * send command sequence to DSP
479 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100480static inline int command_seq(struct snd_sb *chip, const unsigned char *seq, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 int i;
483 for (i = 0; i < size; i++) {
484 if (!snd_sbdsp_command(chip, seq[i]))
485 return -EIO;
486 }
487 return 0;
488}
489
490/*
491 * set CSP codec parameter
492 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100493static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 unsigned char dsp_cmd[3];
496
497 dsp_cmd[0] = 0x05; /* CSP set codec parameter */
498 dsp_cmd[1] = val; /* Parameter value */
499 dsp_cmd[2] = par; /* Parameter */
500 command_seq(chip, dsp_cmd, 3);
501 snd_sbdsp_command(chip, 0x03); /* DSP read? */
502 if (snd_sbdsp_get_byte(chip) != par)
503 return -EIO;
504 return 0;
505}
506
507/*
508 * set CSP register
509 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100510static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 unsigned char dsp_cmd[3];
513
514 dsp_cmd[0] = 0x0e; /* CSP set register */
515 dsp_cmd[1] = reg; /* CSP Register */
516 dsp_cmd[2] = val; /* value */
517 return command_seq(chip, dsp_cmd, 3);
518}
519
520/*
521 * read CSP register
522 * return < 0 -> error
523 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100524static int read_register(struct snd_sb *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 unsigned char dsp_cmd[2];
527
528 dsp_cmd[0] = 0x0f; /* CSP read register */
529 dsp_cmd[1] = reg; /* CSP Register */
530 command_seq(chip, dsp_cmd, 2);
531 return snd_sbdsp_get_byte(chip); /* Read DSP value */
532}
533
534/*
535 * set CSP mode register
536 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100537static int set_mode_register(struct snd_sb *chip, unsigned char mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 unsigned char dsp_cmd[2];
540
541 dsp_cmd[0] = 0x04; /* CSP set mode register */
542 dsp_cmd[1] = mode; /* mode */
543 return command_seq(chip, dsp_cmd, 2);
544}
545
546/*
547 * Detect CSP
548 * return 0 if CSP exists.
549 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100550static int csp_detect(struct snd_sb *chip, int *version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 unsigned char csp_test1, csp_test2;
553 unsigned long flags;
554 int result = -ENODEV;
555
556 spin_lock_irqsave(&chip->reg_lock, flags);
557
558 set_codec_parameter(chip, 0x00, 0x00);
559 set_mode_register(chip, 0xfc); /* 0xfc = ?? */
560
561 csp_test1 = read_register(chip, 0x83);
562 set_register(chip, 0x83, ~csp_test1);
563 csp_test2 = read_register(chip, 0x83);
564 if (csp_test2 != (csp_test1 ^ 0xff))
565 goto __fail;
566
567 set_register(chip, 0x83, csp_test1);
568 csp_test2 = read_register(chip, 0x83);
569 if (csp_test2 != csp_test1)
570 goto __fail;
571
572 set_mode_register(chip, 0x00); /* 0x00 = ? */
573
574 *version = get_version(chip);
575 snd_sbdsp_reset(chip); /* reset DSP after getversion! */
576 if (*version >= 0x10 && *version <= 0x1f)
577 result = 0; /* valid version id */
578
579 __fail:
580 spin_unlock_irqrestore(&chip->reg_lock, flags);
581 return result;
582}
583
584/*
585 * get CSP version number
586 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100587static int get_version(struct snd_sb *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 unsigned char dsp_cmd[2];
590
591 dsp_cmd[0] = 0x08; /* SB_DSP_!something! */
592 dsp_cmd[1] = 0x03; /* get chip version id? */
593 command_seq(chip, dsp_cmd, 2);
594
595 return (snd_sbdsp_get_byte(chip));
596}
597
598/*
599 * check if the CSP version is valid
600 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100601static int snd_sb_csp_check_version(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 if (p->version < 0x10 || p->version > 0x1f) {
604 snd_printd("%s: Invalid CSP version: 0x%x\n", __FUNCTION__, p->version);
605 return 1;
606 }
607 return 0;
608}
609
610/*
611 * download microcode to CSP (microcode should have one "main" block).
612 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100613static 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 -0700614{
615 int status, i;
616 int err;
617 int result = -EIO;
618 unsigned long flags;
619
620 spin_lock_irqsave(&p->chip->reg_lock, flags);
621 snd_sbdsp_command(p->chip, 0x01); /* CSP download command */
622 if (snd_sbdsp_get_byte(p->chip)) {
623 snd_printd("%s: Download command failed\n", __FUNCTION__);
624 goto __fail;
625 }
626 /* Send CSP low byte (size - 1) */
627 snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
628 /* Send high byte */
629 snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
630 /* send microcode sequence */
631 /* load from kernel space */
632 while (size--) {
633 if (!snd_sbdsp_command(p->chip, *buf++))
634 goto __fail;
635 }
636 if (snd_sbdsp_get_byte(p->chip))
637 goto __fail;
638
639 if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
640 i = 0;
641 /* some codecs (FastSpeech) take some time to initialize */
642 while (1) {
643 snd_sbdsp_command(p->chip, 0x03);
644 status = snd_sbdsp_get_byte(p->chip);
645 if (status == 0x55 || ++i >= 10)
646 break;
647 udelay (10);
648 }
649 if (status != 0x55) {
650 snd_printd("%s: Microcode initialization failed\n", __FUNCTION__);
651 goto __fail;
652 }
653 } else {
654 /*
655 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
656 * Start CSP chip if no 16bit DMA channel is set - some kind
657 * of autorun or perhaps a bugfix?
658 */
659 spin_lock(&p->chip->mixer_lock);
660 status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
661 spin_unlock(&p->chip->mixer_lock);
662 if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
663 err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
664 set_codec_parameter(p->chip, 0xff, 0x00));
665 snd_sbdsp_reset(p->chip); /* really! */
666 if (err)
667 goto __fail;
668 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
669 set_mode_register(p->chip, 0x70); /* 70 = RUN */
670 }
671 }
672 result = 0;
673
674 __fail:
675 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
676 return result;
677}
678
Takashi Iwai029d64b2005-11-17 14:34:36 +0100679static 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 -0700680{
681 int err = -ENOMEM;
682 unsigned char *kbuf = kmalloc(size, GFP_KERNEL);
683 if (kbuf) {
684 if (copy_from_user(kbuf, buf, size))
685 err = -EFAULT;
686 else
687 err = snd_sb_csp_load(p, kbuf, size, load_flags);
688 kfree(kbuf);
689 }
690 return err;
691}
692
Takashi Iwai8ad2da12007-02-26 15:55:43 +0100693#ifdef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694#include "sb16_csp_codecs.h"
695
Clemens Ladischde66d532006-11-06 09:18:34 +0100696static const struct firmware snd_sb_csp_static_programs[] = {
697 { .data = mulaw_main, .size = sizeof mulaw_main },
698 { .data = alaw_main, .size = sizeof alaw_main },
699 { .data = ima_adpcm_init, .size = sizeof ima_adpcm_init },
700 { .data = ima_adpcm_playback, .size = sizeof ima_adpcm_playback },
701 { .data = ima_adpcm_capture, .size = sizeof ima_adpcm_capture },
702};
703#endif
704
705static int snd_sb_csp_firmware_load(struct snd_sb_csp *p, int index, int flags)
706{
707 static const char *const names[] = {
708 "sb16/mulaw_main.csp",
709 "sb16/alaw_main.csp",
710 "sb16/ima_adpcm_init.csp",
711 "sb16/ima_adpcm_playback.csp",
712 "sb16/ima_adpcm_capture.csp",
713 };
714 const struct firmware *program;
715 int err;
716
717 BUILD_BUG_ON(ARRAY_SIZE(names) != CSP_PROGRAM_COUNT);
718 program = p->csp_programs[index];
719 if (!program) {
720 err = request_firmware(&program, names[index],
721 p->chip->card->dev);
722 if (err >= 0)
723 p->csp_programs[index] = program;
724 else {
Takashi Iwai8ad2da12007-02-26 15:55:43 +0100725#ifdef CONFIG_SND_SB16_CSP_FIRMWARE_IN_KERNEL
Clemens Ladischde66d532006-11-06 09:18:34 +0100726 program = &snd_sb_csp_static_programs[index];
727#else
728 return err;
729#endif
730 }
731 }
732 return snd_sb_csp_load(p, program->data, program->size, flags);
733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/*
736 * autoload hardware codec if necessary
737 * return 0 if CSP is loaded and ready to run (p->running != 0)
738 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100739static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 unsigned long flags;
742 int err = 0;
743
744 /* if CSP is running or manually loaded then exit */
745 if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED))
746 return -EBUSY;
747
748 /* autoload microcode only if requested hardware codec is not already loaded */
749 if (((1 << pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
750 p->running = SNDRV_SB_CSP_ST_AUTO;
751 } else {
752 switch (pcm_sfmt) {
753 case SNDRV_PCM_FORMAT_MU_LAW:
Clemens Ladischde66d532006-11-06 09:18:34 +0100754 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_MULAW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
756 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
757 break;
758 case SNDRV_PCM_FORMAT_A_LAW:
Clemens Ladischde66d532006-11-06 09:18:34 +0100759 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ALAW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
761 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
762 break;
763 case SNDRV_PCM_FORMAT_IMA_ADPCM:
Clemens Ladischde66d532006-11-06 09:18:34 +0100764 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ADPCM_INIT,
765 SNDRV_SB_CSP_LOAD_INITBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (err)
767 break;
768 if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
Clemens Ladischde66d532006-11-06 09:18:34 +0100769 err = snd_sb_csp_firmware_load
770 (p, CSP_PROGRAM_ADPCM_PLAYBACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
772 } else {
Clemens Ladischde66d532006-11-06 09:18:34 +0100773 err = snd_sb_csp_firmware_load
774 (p, CSP_PROGRAM_ADPCM_CAPTURE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
776 }
777 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
778 break;
779 default:
780 /* Decouple CSP from IRQ and DMAREQ lines */
781 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
782 spin_lock_irqsave(&p->chip->reg_lock, flags);
783 set_mode_register(p->chip, 0xfc);
784 set_mode_register(p->chip, 0x00);
785 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
786 p->running = 0; /* clear autoloaded flag */
787 }
788 return -EINVAL;
789 }
790 if (err) {
791 p->acc_format = 0;
792 p->acc_channels = p->acc_width = p->acc_rates = 0;
793
794 p->running = 0; /* clear autoloaded flag */
795 p->mode = 0;
796 return (err);
797 } else {
798 p->running = SNDRV_SB_CSP_ST_AUTO; /* set autoloaded flag */
799 p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT; /* only 16 bit data */
800 p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
801 p->acc_rates = SNDRV_SB_CSP_RATE_ALL; /* HW codecs accept all rates */
802 }
803
804 }
805 return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
806}
807
808/*
809 * start CSP
810 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100811static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 unsigned char s_type; /* sample type */
814 unsigned char mixL, mixR;
815 int result = -EIO;
816 unsigned long flags;
817
818 if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
819 snd_printd("%s: Microcode not loaded\n", __FUNCTION__);
820 return -ENXIO;
821 }
822 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
823 snd_printd("%s: CSP already running\n", __FUNCTION__);
824 return -EBUSY;
825 }
826 if (!(sample_width & p->acc_width)) {
827 snd_printd("%s: Unsupported PCM sample width\n", __FUNCTION__);
828 return -EINVAL;
829 }
830 if (!(channels & p->acc_channels)) {
831 snd_printd("%s: Invalid number of channels\n", __FUNCTION__);
832 return -EINVAL;
833 }
834
835 /* Mute PCM volume */
836 spin_lock_irqsave(&p->chip->mixer_lock, flags);
837 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
838 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
839 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
840 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
841
842 spin_lock(&p->chip->reg_lock);
843 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
844 set_mode_register(p->chip, 0x70); /* 70 = RUN */
845
846 s_type = 0x00;
847 if (channels == SNDRV_SB_CSP_MONO)
848 s_type = 0x11; /* 000n 000n (n = 1 if mono) */
849 if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
850 s_type |= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
851
852 if (set_codec_parameter(p->chip, 0x81, s_type)) {
853 snd_printd("%s: Set sample type command failed\n", __FUNCTION__);
854 goto __fail;
855 }
856 if (set_codec_parameter(p->chip, 0x80, 0x00)) {
857 snd_printd("%s: Codec start command failed\n", __FUNCTION__);
858 goto __fail;
859 }
860 p->run_width = sample_width;
861 p->run_channels = channels;
862
863 p->running |= SNDRV_SB_CSP_ST_RUNNING;
864
865 if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
866 set_codec_parameter(p->chip, 0xe0, 0x01);
867 /* enable QSound decoder */
868 set_codec_parameter(p->chip, 0x00, 0xff);
869 set_codec_parameter(p->chip, 0x01, 0xff);
870 p->running |= SNDRV_SB_CSP_ST_QSOUND;
871 /* set QSound startup value */
872 snd_sb_csp_qsound_transfer(p);
873 }
874 result = 0;
875
876 __fail:
877 spin_unlock(&p->chip->reg_lock);
878
879 /* restore PCM volume */
880 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
881 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
882 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
883
884 return result;
885}
886
887/*
888 * stop CSP
889 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100890static int snd_sb_csp_stop(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 int result;
893 unsigned char mixL, mixR;
894 unsigned long flags;
895
896 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
897 return 0;
898
899 /* Mute PCM volume */
900 spin_lock_irqsave(&p->chip->mixer_lock, flags);
901 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
902 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
903 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
904 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
905
906 spin_lock(&p->chip->reg_lock);
907 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
908 set_codec_parameter(p->chip, 0xe0, 0x01);
909 /* disable QSound decoder */
910 set_codec_parameter(p->chip, 0x00, 0x00);
911 set_codec_parameter(p->chip, 0x01, 0x00);
912
913 p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
914 }
915 result = set_mode_register(p->chip, 0xc0); /* c0 = STOP */
916 spin_unlock(&p->chip->reg_lock);
917
918 /* restore PCM volume */
919 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
920 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
921 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
922
923 if (!(result))
924 p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
925 return result;
926}
927
928/*
929 * pause CSP codec and hold DMA transfer
930 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100931static int snd_sb_csp_pause(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 int result;
934 unsigned long flags;
935
936 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
937 return -EBUSY;
938
939 spin_lock_irqsave(&p->chip->reg_lock, flags);
940 result = set_codec_parameter(p->chip, 0x80, 0xff);
941 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
942 if (!(result))
943 p->running |= SNDRV_SB_CSP_ST_PAUSED;
944
945 return result;
946}
947
948/*
949 * restart CSP codec and resume DMA transfer
950 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100951static int snd_sb_csp_restart(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 int result;
954 unsigned long flags;
955
956 if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
957 return -EBUSY;
958
959 spin_lock_irqsave(&p->chip->reg_lock, flags);
960 result = set_codec_parameter(p->chip, 0x80, 0x00);
961 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
962 if (!(result))
963 p->running &= ~SNDRV_SB_CSP_ST_PAUSED;
964
965 return result;
966}
967
968/* ------------------------------ */
969
970/*
971 * QSound mixer control for PCM
972 */
973
Takashi Iwai029d64b2005-11-17 14:34:36 +0100974static int snd_sb_qsound_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
977 uinfo->count = 1;
978 uinfo->value.integer.min = 0;
979 uinfo->value.integer.max = 1;
980 return 0;
981}
982
Takashi Iwai029d64b2005-11-17 14:34:36 +0100983static int snd_sb_qsound_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100985 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
988 return 0;
989}
990
Takashi Iwai029d64b2005-11-17 14:34:36 +0100991static int snd_sb_qsound_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100993 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 unsigned long flags;
995 int change;
996 unsigned char nval;
997
998 nval = ucontrol->value.integer.value[0] & 0x01;
999 spin_lock_irqsave(&p->q_lock, flags);
1000 change = p->q_enabled != nval;
1001 p->q_enabled = nval;
1002 spin_unlock_irqrestore(&p->q_lock, flags);
1003 return change;
1004}
1005
Takashi Iwai029d64b2005-11-17 14:34:36 +01001006static int snd_sb_qsound_space_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1009 uinfo->count = 2;
1010 uinfo->value.integer.min = 0;
1011 uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1012 return 0;
1013}
1014
Takashi Iwai029d64b2005-11-17 14:34:36 +01001015static int snd_sb_qsound_space_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001017 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 unsigned long flags;
1019
1020 spin_lock_irqsave(&p->q_lock, flags);
1021 ucontrol->value.integer.value[0] = p->qpos_left;
1022 ucontrol->value.integer.value[1] = p->qpos_right;
1023 spin_unlock_irqrestore(&p->q_lock, flags);
1024 return 0;
1025}
1026
Takashi Iwai029d64b2005-11-17 14:34:36 +01001027static int snd_sb_qsound_space_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001029 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 unsigned long flags;
1031 int change;
1032 unsigned char nval1, nval2;
1033
1034 nval1 = ucontrol->value.integer.value[0];
1035 if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1036 nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1037 nval2 = ucontrol->value.integer.value[1];
1038 if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1039 nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1040 spin_lock_irqsave(&p->q_lock, flags);
1041 change = p->qpos_left != nval1 || p->qpos_right != nval2;
1042 p->qpos_left = nval1;
1043 p->qpos_right = nval2;
1044 p->qpos_changed = change;
1045 spin_unlock_irqrestore(&p->q_lock, flags);
1046 return change;
1047}
1048
Takashi Iwai029d64b2005-11-17 14:34:36 +01001049static struct snd_kcontrol_new snd_sb_qsound_switch = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1051 .name = "3D Control - Switch",
1052 .info = snd_sb_qsound_switch_info,
1053 .get = snd_sb_qsound_switch_get,
1054 .put = snd_sb_qsound_switch_put
1055};
1056
Takashi Iwai029d64b2005-11-17 14:34:36 +01001057static struct snd_kcontrol_new snd_sb_qsound_space = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1059 .name = "3D Control - Space",
1060 .info = snd_sb_qsound_space_info,
1061 .get = snd_sb_qsound_space_get,
1062 .put = snd_sb_qsound_space_put
1063};
1064
Takashi Iwai029d64b2005-11-17 14:34:36 +01001065static int snd_sb_qsound_build(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001067 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 int err;
1069
1070 snd_assert(p != NULL, return -EINVAL);
1071
1072 card = p->chip->card;
1073 p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
1074 p->qpos_changed = 0;
1075
1076 spin_lock_init(&p->q_lock);
1077
1078 if ((err = snd_ctl_add(card, p->qsound_switch = snd_ctl_new1(&snd_sb_qsound_switch, p))) < 0)
1079 goto __error;
1080 if ((err = snd_ctl_add(card, p->qsound_space = snd_ctl_new1(&snd_sb_qsound_space, p))) < 0)
1081 goto __error;
1082
1083 return 0;
1084
1085 __error:
1086 snd_sb_qsound_destroy(p);
1087 return err;
1088}
1089
Takashi Iwai029d64b2005-11-17 14:34:36 +01001090static void snd_sb_qsound_destroy(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001092 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 unsigned long flags;
1094
1095 snd_assert(p != NULL, return);
1096
1097 card = p->chip->card;
1098
1099 down_write(&card->controls_rwsem);
1100 if (p->qsound_switch)
1101 snd_ctl_remove(card, p->qsound_switch);
1102 if (p->qsound_space)
1103 snd_ctl_remove(card, p->qsound_space);
1104 up_write(&card->controls_rwsem);
1105
1106 /* cancel pending transfer of QSound parameters */
1107 spin_lock_irqsave (&p->q_lock, flags);
1108 p->qpos_changed = 0;
1109 spin_unlock_irqrestore (&p->q_lock, flags);
1110}
1111
1112/*
1113 * Transfer qsound parameters to CSP,
1114 * function should be called from interrupt routine
1115 */
Takashi Iwai029d64b2005-11-17 14:34:36 +01001116static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118 int err = -ENXIO;
1119
1120 spin_lock(&p->q_lock);
1121 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1122 set_codec_parameter(p->chip, 0xe0, 0x01);
1123 /* left channel */
1124 set_codec_parameter(p->chip, 0x00, p->qpos_left);
1125 set_codec_parameter(p->chip, 0x02, 0x00);
1126 /* right channel */
1127 set_codec_parameter(p->chip, 0x00, p->qpos_right);
1128 set_codec_parameter(p->chip, 0x03, 0x00);
1129 err = 0;
1130 }
1131 p->qpos_changed = 0;
1132 spin_unlock(&p->q_lock);
1133 return err;
1134}
1135
1136/* ------------------------------ */
1137
1138/*
1139 * proc interface
1140 */
Takashi Iwai029d64b2005-11-17 14:34:36 +01001141static int init_proc_entry(struct snd_sb_csp * p, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 char name[16];
Takashi Iwai029d64b2005-11-17 14:34:36 +01001144 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 sprintf(name, "cspD%d", device);
1146 if (! snd_card_proc_new(p->chip->card, name, &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001147 snd_info_set_text_ops(entry, p, info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return 0;
1149}
1150
Takashi Iwai029d64b2005-11-17 14:34:36 +01001151static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001153 struct snd_sb_csp *p = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
1156 snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
1157 ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
1158 ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
1159 ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
1160 if (p->running & SNDRV_SB_CSP_ST_LOADED) {
1161 snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
1162 snd_iprintf(buffer, "Sample rates: ");
1163 if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
1164 snd_iprintf(buffer, "All\n");
1165 } else {
1166 snd_iprintf(buffer, "%s%s%s%s\n",
1167 ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
1168 ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
1169 ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
1170 ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
1171 }
1172 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
1173 snd_iprintf(buffer, "QSound decoder %sabled\n",
1174 p->q_enabled ? "en" : "dis");
1175 } else {
1176 snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
1177 p->acc_format,
1178 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
1179 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
1180 ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
1181 ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
1182 ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
1183 ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
1184 }
1185 }
1186 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
1187 snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1188 }
1189 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
1190 snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
1191 ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
1192 ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
1193 }
1194 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1195 snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
1196 p->qpos_left, p->qpos_right);
1197 }
1198}
1199
1200/* */
1201
1202EXPORT_SYMBOL(snd_sb_csp_new);
1203
1204/*
1205 * INIT part
1206 */
1207
1208static int __init alsa_sb_csp_init(void)
1209{
1210 return 0;
1211}
1212
1213static void __exit alsa_sb_csp_exit(void)
1214{
1215}
1216
1217module_init(alsa_sb_csp_init)
1218module_exit(alsa_sb_csp_exit)