blob: 48da2276683d275ece81027f44d3906b347546b5 [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040029#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
31#include <sound/control.h>
32#include <sound/info.h>
33#include <sound/sb16_csp.h>
34#include <sound/initval.h>
35
36MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
37MODULE_DESCRIPTION("ALSA driver for SB16 Creative Signal Processor");
38MODULE_LICENSE("GPL");
Clemens Ladisch7e0af292007-05-03 17:59:54 +020039MODULE_FIRMWARE("sb16/mulaw_main.csp");
40MODULE_FIRMWARE("sb16/alaw_main.csp");
41MODULE_FIRMWARE("sb16/ima_adpcm_init.csp");
42MODULE_FIRMWARE("sb16/ima_adpcm_playback.csp");
43MODULE_FIRMWARE("sb16/ima_adpcm_capture.csp");
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#ifdef SNDRV_LITTLE_ENDIAN
46#define CSP_HDR_VALUE(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
47#else
48#define CSP_HDR_VALUE(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
49#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define RIFF_HEADER CSP_HDR_VALUE('R', 'I', 'F', 'F')
52#define CSP__HEADER CSP_HDR_VALUE('C', 'S', 'P', ' ')
53#define LIST_HEADER CSP_HDR_VALUE('L', 'I', 'S', 'T')
54#define FUNC_HEADER CSP_HDR_VALUE('f', 'u', 'n', 'c')
55#define CODE_HEADER CSP_HDR_VALUE('c', 'o', 'd', 'e')
56#define INIT_HEADER CSP_HDR_VALUE('i', 'n', 'i', 't')
57#define MAIN_HEADER CSP_HDR_VALUE('m', 'a', 'i', 'n')
58
59/*
60 * RIFF data format
61 */
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070062struct riff_header {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 __u32 name;
64 __u32 len;
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070065};
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070067struct desc_header {
68 struct riff_header info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 __u16 func_nr;
70 __u16 VOC_type;
71 __u16 flags_play_rec;
72 __u16 flags_16bit_8bit;
73 __u16 flags_stereo_mono;
74 __u16 flags_rates;
Alexey Dobriyandfc866e2005-09-10 00:26:35 -070075};
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * prototypes
79 */
Takashi Iwai029d64b2005-11-17 14:34:36 +010080static void snd_sb_csp_free(struct snd_hwdep *hw);
81static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file);
82static int snd_sb_csp_ioctl(struct snd_hwdep * hw, struct file *file, unsigned int cmd, unsigned long arg);
83static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Takashi Iwai029d64b2005-11-17 14:34:36 +010085static int csp_detect(struct snd_sb *chip, int *version);
86static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val);
87static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val);
88static int read_register(struct snd_sb *chip, unsigned char reg);
89static int set_mode_register(struct snd_sb *chip, unsigned char mode);
90static int get_version(struct snd_sb *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Takashi Iwai029d64b2005-11-17 14:34:36 +010092static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
93 struct snd_sb_csp_microcode __user * code);
94static int snd_sb_csp_unload(struct snd_sb_csp * p);
95static int snd_sb_csp_load_user(struct snd_sb_csp * p, const unsigned char __user *buf, int size, int load_flags);
96static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode);
97static int snd_sb_csp_check_version(struct snd_sb_csp * p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Takashi Iwai029d64b2005-11-17 14:34:36 +010099static int snd_sb_csp_use(struct snd_sb_csp * p);
100static int snd_sb_csp_unuse(struct snd_sb_csp * p);
101static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels);
102static int snd_sb_csp_stop(struct snd_sb_csp * p);
103static int snd_sb_csp_pause(struct snd_sb_csp * p);
104static int snd_sb_csp_restart(struct snd_sb_csp * p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Takashi Iwai029d64b2005-11-17 14:34:36 +0100106static int snd_sb_qsound_build(struct snd_sb_csp * p);
107static void snd_sb_qsound_destroy(struct snd_sb_csp * p);
108static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Takashi Iwai029d64b2005-11-17 14:34:36 +0100110static int init_proc_entry(struct snd_sb_csp * p, int device);
111static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/*
114 * Detect CSP chip and create a new instance
115 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100116int snd_sb_csp_new(struct snd_sb *chip, int device, struct snd_hwdep ** rhwdep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100118 struct snd_sb_csp *p;
Takashi Iwaicd0b4ac2007-12-14 12:18:52 +0100119 int uninitialized_var(version);
120 int err;
Takashi Iwai029d64b2005-11-17 14:34:36 +0100121 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 if (rhwdep)
124 *rhwdep = NULL;
125
126 if (csp_detect(chip, &version))
127 return -ENODEV;
128
129 if ((err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw)) < 0)
130 return err;
131
Takashi Iwai9e76a762005-09-09 14:21:17 +0200132 if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 snd_device_free(chip->card, hw);
134 return -ENOMEM;
135 }
136 p->chip = chip;
137 p->version = version;
138
139 /* CSP operators */
140 p->ops.csp_use = snd_sb_csp_use;
141 p->ops.csp_unuse = snd_sb_csp_unuse;
142 p->ops.csp_autoload = snd_sb_csp_autoload;
143 p->ops.csp_start = snd_sb_csp_start;
144 p->ops.csp_stop = snd_sb_csp_stop;
145 p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;
146
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100147 mutex_init(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f));
149 hw->iface = SNDRV_HWDEP_IFACE_SB16CSP;
150 hw->private_data = p;
151 hw->private_free = snd_sb_csp_free;
152
153 /* operators - only write/ioctl */
154 hw->ops.open = snd_sb_csp_open;
155 hw->ops.ioctl = snd_sb_csp_ioctl;
156 hw->ops.release = snd_sb_csp_release;
157
158 /* create a proc entry */
159 init_proc_entry(p, device);
160 if (rhwdep)
161 *rhwdep = hw;
162 return 0;
163}
164
165/*
166 * free_private for hwdep instance
167 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100168static void snd_sb_csp_free(struct snd_hwdep *hwdep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Clemens Ladischde66d532006-11-06 09:18:34 +0100170 int i;
Takashi Iwai029d64b2005-11-17 14:34:36 +0100171 struct snd_sb_csp *p = hwdep->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (p) {
173 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
174 snd_sb_csp_stop(p);
Clemens Ladischde66d532006-11-06 09:18:34 +0100175 for (i = 0; i < ARRAY_SIZE(p->csp_programs); ++i)
176 release_firmware(p->csp_programs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 kfree(p);
178 }
179}
180
181/* ------------------------------ */
182
183/*
184 * open the device exclusively
185 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100186static int snd_sb_csp_open(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100188 struct snd_sb_csp *p = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return (snd_sb_csp_use(p));
190}
191
192/*
193 * ioctl for hwdep device:
194 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100195static 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 -0700196{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100197 struct snd_sb_csp *p = hw->private_data;
198 struct snd_sb_csp_info info;
199 struct snd_sb_csp_start start_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int err;
201
Takashi Iwai622207d2008-08-08 17:11:45 +0200202 if (snd_BUG_ON(!p))
203 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if (snd_sb_csp_check_version(p))
206 return -ENODEV;
207
208 switch (cmd) {
209 /* get information */
210 case SNDRV_SB_CSP_IOCTL_INFO:
Dan Carpenterbffbbc02013-11-07 11:09:54 +0300211 memset(&info, 0, sizeof(info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *info.codec_name = *p->codec_name;
213 info.func_nr = p->func_nr;
214 info.acc_format = p->acc_format;
215 info.acc_channels = p->acc_channels;
216 info.acc_width = p->acc_width;
217 info.acc_rates = p->acc_rates;
218 info.csp_mode = p->mode;
219 info.run_channels = p->run_channels;
220 info.run_width = p->run_width;
221 info.version = p->version;
222 info.state = p->running;
223 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
224 err = -EFAULT;
225 else
226 err = 0;
227 break;
228
229 /* load CSP microcode */
230 case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
231 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
Takashi Iwai029d64b2005-11-17 14:34:36 +0100232 -EBUSY : snd_sb_csp_riff_load(p, (struct snd_sb_csp_microcode __user *) arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 break;
234 case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
235 err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
236 -EBUSY : snd_sb_csp_unload(p));
237 break;
238
239 /* change CSP running state */
240 case SNDRV_SB_CSP_IOCTL_START:
241 if (copy_from_user(&start_info, (void __user *) arg, sizeof(start_info)))
242 err = -EFAULT;
243 else
244 err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
245 break;
246 case SNDRV_SB_CSP_IOCTL_STOP:
247 err = snd_sb_csp_stop(p);
248 break;
249 case SNDRV_SB_CSP_IOCTL_PAUSE:
250 err = snd_sb_csp_pause(p);
251 break;
252 case SNDRV_SB_CSP_IOCTL_RESTART:
253 err = snd_sb_csp_restart(p);
254 break;
255 default:
256 err = -ENOTTY;
257 break;
258 }
259
260 return err;
261}
262
263/*
264 * close the device
265 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100266static int snd_sb_csp_release(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100268 struct snd_sb_csp *p = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return (snd_sb_csp_unuse(p));
270}
271
272/* ------------------------------ */
273
274/*
275 * acquire device
276 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100277static int snd_sb_csp_use(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100279 mutex_lock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (p->used) {
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100281 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return -EAGAIN;
283 }
284 p->used++;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100285 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 return 0;
288
289}
290
291/*
292 * release device
293 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100294static int snd_sb_csp_unuse(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100296 mutex_lock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 p->used--;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100298 mutex_unlock(&p->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 return 0;
301}
302
303/*
304 * load microcode via ioctl:
305 * code is user-space pointer
306 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100307static int snd_sb_csp_riff_load(struct snd_sb_csp * p,
308 struct snd_sb_csp_microcode __user * mcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100310 struct snd_sb_csp_mc_header info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 unsigned char __user *data_ptr;
313 unsigned char __user *data_end;
314 unsigned short func_nr = 0;
315
Alexey Dobriyandfc866e2005-09-10 00:26:35 -0700316 struct riff_header file_h, item_h, code_h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 __u32 item_type;
Alexey Dobriyandfc866e2005-09-10 00:26:35 -0700318 struct desc_header funcdesc_h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 unsigned long flags;
321 int err;
322
323 if (copy_from_user(&info, mcode, sizeof(info)))
324 return -EFAULT;
325 data_ptr = mcode->data;
326
327 if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
328 return -EFAULT;
329 if ((file_h.name != RIFF_HEADER) ||
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700330 (le32_to_cpu(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800331 snd_printd("%s: Invalid RIFF header\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return -EINVAL;
333 }
334 data_ptr += sizeof(file_h);
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700335 data_end = data_ptr + le32_to_cpu(file_h.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
338 return -EFAULT;
339 if (item_type != CSP__HEADER) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800340 snd_printd("%s: Invalid RIFF file type\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -EINVAL;
342 }
343 data_ptr += sizeof (item_type);
344
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700345 for (; data_ptr < data_end; data_ptr += le32_to_cpu(item_h.len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
347 return -EFAULT;
348 data_ptr += sizeof(item_h);
349 if (item_h.name != LIST_HEADER)
350 continue;
351
352 if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
353 return -EFAULT;
354 switch (item_type) {
355 case FUNC_HEADER:
356 if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
357 return -EFAULT;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700358 func_nr = le16_to_cpu(funcdesc_h.func_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 break;
360 case CODE_HEADER:
361 if (func_nr != info.func_req)
362 break; /* not required function, try next */
363 data_ptr += sizeof(item_type);
364
365 /* destroy QSound mixer element */
366 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
367 snd_sb_qsound_destroy(p);
368 }
369 /* Clear all flags */
370 p->running = 0;
371 p->mode = 0;
372
373 /* load microcode blocks */
374 for (;;) {
375 if (data_ptr >= data_end)
376 return -EINVAL;
377 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
378 return -EFAULT;
379
380 /* init microcode blocks */
381 if (code_h.name != INIT_HEADER)
382 break;
383 data_ptr += sizeof(code_h);
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700384 err = snd_sb_csp_load_user(p, data_ptr, le32_to_cpu(code_h.len),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 SNDRV_SB_CSP_LOAD_INITBLOCK);
386 if (err)
387 return err;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700388 data_ptr += le32_to_cpu(code_h.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390 /* main microcode block */
391 if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
392 return -EFAULT;
393
394 if (code_h.name != MAIN_HEADER) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800395 snd_printd("%s: Missing 'main' microcode\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return -EINVAL;
397 }
398 data_ptr += sizeof(code_h);
399 err = snd_sb_csp_load_user(p, data_ptr,
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700400 le32_to_cpu(code_h.len), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (err)
402 return err;
403
404 /* fill in codec header */
405 strlcpy(p->codec_name, info.codec_name, sizeof(p->codec_name));
406 p->func_nr = func_nr;
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700407 p->mode = le16_to_cpu(funcdesc_h.flags_play_rec);
408 switch (le16_to_cpu(funcdesc_h.VOC_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 case 0x0001: /* QSound decoder */
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700410 if (le16_to_cpu(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (snd_sb_qsound_build(p) == 0)
412 /* set QSound flag and clear all other mode flags */
413 p->mode = SNDRV_SB_CSP_MODE_QSOUND;
414 }
415 p->acc_format = 0;
416 break;
417 case 0x0006: /* A Law codec */
418 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
419 break;
420 case 0x0007: /* Mu Law codec */
421 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
422 break;
423 case 0x0011: /* what Creative thinks is IMA ADPCM codec */
424 case 0x0200: /* Creative ADPCM codec */
425 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
426 break;
427 case 201: /* Text 2 Speech decoder */
428 /* TODO: Text2Speech handling routines */
429 p->acc_format = 0;
430 break;
431 case 0x0202: /* Fast Speech 8 codec */
432 case 0x0203: /* Fast Speech 10 codec */
433 p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
434 break;
435 default: /* other codecs are unsupported */
436 p->acc_format = p->acc_width = p->acc_rates = 0;
437 p->mode = 0;
438 snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800439 __func__,
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700440 le16_to_cpu(funcdesc_h.VOC_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return -EINVAL;
442 }
Alexey Dobriyan31bbf8f2005-09-10 00:26:34 -0700443 p->acc_channels = le16_to_cpu(funcdesc_h.flags_stereo_mono);
444 p->acc_width = le16_to_cpu(funcdesc_h.flags_16bit_8bit);
445 p->acc_rates = le16_to_cpu(funcdesc_h.flags_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /* Decouple CSP from IRQ and DMAREQ lines */
448 spin_lock_irqsave(&p->chip->reg_lock, flags);
449 set_mode_register(p->chip, 0xfc);
450 set_mode_register(p->chip, 0x00);
451 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
452
453 /* finished loading successfully */
454 p->running = SNDRV_SB_CSP_ST_LOADED; /* set LOADED flag */
455 return 0;
456 }
457 }
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800458 snd_printd("%s: Function #%d not found\n", __func__, info.func_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return -EINVAL;
460}
461
462/*
463 * unload CSP microcode
464 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100465static int snd_sb_csp_unload(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 if (p->running & SNDRV_SB_CSP_ST_RUNNING)
468 return -EBUSY;
469 if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
470 return -ENXIO;
471
472 /* clear supported formats */
473 p->acc_format = 0;
474 p->acc_channels = p->acc_width = p->acc_rates = 0;
475 /* destroy QSound mixer element */
476 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
477 snd_sb_qsound_destroy(p);
478 }
479 /* clear all flags */
480 p->running = 0;
481 p->mode = 0;
482 return 0;
483}
484
485/*
486 * send command sequence to DSP
487 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100488static inline int command_seq(struct snd_sb *chip, const unsigned char *seq, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 int i;
491 for (i = 0; i < size; i++) {
492 if (!snd_sbdsp_command(chip, seq[i]))
493 return -EIO;
494 }
495 return 0;
496}
497
498/*
499 * set CSP codec parameter
500 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100501static int set_codec_parameter(struct snd_sb *chip, unsigned char par, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 unsigned char dsp_cmd[3];
504
505 dsp_cmd[0] = 0x05; /* CSP set codec parameter */
506 dsp_cmd[1] = val; /* Parameter value */
507 dsp_cmd[2] = par; /* Parameter */
508 command_seq(chip, dsp_cmd, 3);
509 snd_sbdsp_command(chip, 0x03); /* DSP read? */
510 if (snd_sbdsp_get_byte(chip) != par)
511 return -EIO;
512 return 0;
513}
514
515/*
516 * set CSP register
517 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100518static int set_register(struct snd_sb *chip, unsigned char reg, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 unsigned char dsp_cmd[3];
521
522 dsp_cmd[0] = 0x0e; /* CSP set register */
523 dsp_cmd[1] = reg; /* CSP Register */
524 dsp_cmd[2] = val; /* value */
525 return command_seq(chip, dsp_cmd, 3);
526}
527
528/*
529 * read CSP register
530 * return < 0 -> error
531 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100532static int read_register(struct snd_sb *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 unsigned char dsp_cmd[2];
535
536 dsp_cmd[0] = 0x0f; /* CSP read register */
537 dsp_cmd[1] = reg; /* CSP Register */
538 command_seq(chip, dsp_cmd, 2);
539 return snd_sbdsp_get_byte(chip); /* Read DSP value */
540}
541
542/*
543 * set CSP mode register
544 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100545static int set_mode_register(struct snd_sb *chip, unsigned char mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 unsigned char dsp_cmd[2];
548
549 dsp_cmd[0] = 0x04; /* CSP set mode register */
550 dsp_cmd[1] = mode; /* mode */
551 return command_seq(chip, dsp_cmd, 2);
552}
553
554/*
555 * Detect CSP
556 * return 0 if CSP exists.
557 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100558static int csp_detect(struct snd_sb *chip, int *version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 unsigned char csp_test1, csp_test2;
561 unsigned long flags;
562 int result = -ENODEV;
563
564 spin_lock_irqsave(&chip->reg_lock, flags);
565
566 set_codec_parameter(chip, 0x00, 0x00);
567 set_mode_register(chip, 0xfc); /* 0xfc = ?? */
568
569 csp_test1 = read_register(chip, 0x83);
570 set_register(chip, 0x83, ~csp_test1);
571 csp_test2 = read_register(chip, 0x83);
572 if (csp_test2 != (csp_test1 ^ 0xff))
573 goto __fail;
574
575 set_register(chip, 0x83, csp_test1);
576 csp_test2 = read_register(chip, 0x83);
577 if (csp_test2 != csp_test1)
578 goto __fail;
579
580 set_mode_register(chip, 0x00); /* 0x00 = ? */
581
582 *version = get_version(chip);
583 snd_sbdsp_reset(chip); /* reset DSP after getversion! */
584 if (*version >= 0x10 && *version <= 0x1f)
585 result = 0; /* valid version id */
586
587 __fail:
588 spin_unlock_irqrestore(&chip->reg_lock, flags);
589 return result;
590}
591
592/*
593 * get CSP version number
594 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100595static int get_version(struct snd_sb *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 unsigned char dsp_cmd[2];
598
599 dsp_cmd[0] = 0x08; /* SB_DSP_!something! */
600 dsp_cmd[1] = 0x03; /* get chip version id? */
601 command_seq(chip, dsp_cmd, 2);
602
603 return (snd_sbdsp_get_byte(chip));
604}
605
606/*
607 * check if the CSP version is valid
608 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100609static int snd_sb_csp_check_version(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 if (p->version < 0x10 || p->version > 0x1f) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800612 snd_printd("%s: Invalid CSP version: 0x%x\n", __func__, p->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return 1;
614 }
615 return 0;
616}
617
618/*
619 * download microcode to CSP (microcode should have one "main" block).
620 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100621static 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 -0700622{
623 int status, i;
624 int err;
625 int result = -EIO;
626 unsigned long flags;
627
628 spin_lock_irqsave(&p->chip->reg_lock, flags);
629 snd_sbdsp_command(p->chip, 0x01); /* CSP download command */
630 if (snd_sbdsp_get_byte(p->chip)) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800631 snd_printd("%s: Download command failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 goto __fail;
633 }
634 /* Send CSP low byte (size - 1) */
635 snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
636 /* Send high byte */
637 snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
638 /* send microcode sequence */
639 /* load from kernel space */
640 while (size--) {
641 if (!snd_sbdsp_command(p->chip, *buf++))
642 goto __fail;
643 }
644 if (snd_sbdsp_get_byte(p->chip))
645 goto __fail;
646
647 if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
648 i = 0;
649 /* some codecs (FastSpeech) take some time to initialize */
650 while (1) {
651 snd_sbdsp_command(p->chip, 0x03);
652 status = snd_sbdsp_get_byte(p->chip);
653 if (status == 0x55 || ++i >= 10)
654 break;
655 udelay (10);
656 }
657 if (status != 0x55) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800658 snd_printd("%s: Microcode initialization failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 goto __fail;
660 }
661 } else {
662 /*
663 * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
664 * Start CSP chip if no 16bit DMA channel is set - some kind
665 * of autorun or perhaps a bugfix?
666 */
667 spin_lock(&p->chip->mixer_lock);
668 status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
669 spin_unlock(&p->chip->mixer_lock);
670 if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
671 err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
672 set_codec_parameter(p->chip, 0xff, 0x00));
673 snd_sbdsp_reset(p->chip); /* really! */
674 if (err)
675 goto __fail;
676 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
677 set_mode_register(p->chip, 0x70); /* 70 = RUN */
678 }
679 }
680 result = 0;
681
682 __fail:
683 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
684 return result;
685}
686
Takashi Iwai029d64b2005-11-17 14:34:36 +0100687static 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 -0700688{
Li Zefan68425ad2009-04-10 09:43:36 +0800689 int err;
690 unsigned char *kbuf;
691
692 kbuf = memdup_user(buf, size);
693 if (IS_ERR(kbuf))
694 return PTR_ERR(kbuf);
695
696 err = snd_sb_csp_load(p, kbuf, size, load_flags);
697
698 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return err;
700}
701
Clemens Ladischde66d532006-11-06 09:18:34 +0100702static int snd_sb_csp_firmware_load(struct snd_sb_csp *p, int index, int flags)
703{
704 static const char *const names[] = {
705 "sb16/mulaw_main.csp",
706 "sb16/alaw_main.csp",
707 "sb16/ima_adpcm_init.csp",
708 "sb16/ima_adpcm_playback.csp",
709 "sb16/ima_adpcm_capture.csp",
710 };
711 const struct firmware *program;
Clemens Ladischde66d532006-11-06 09:18:34 +0100712
713 BUILD_BUG_ON(ARRAY_SIZE(names) != CSP_PROGRAM_COUNT);
714 program = p->csp_programs[index];
715 if (!program) {
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200716 int err = request_firmware(&program, names[index],
717 p->chip->card->dev);
718 if (err < 0)
Clemens Ladischde66d532006-11-06 09:18:34 +0100719 return err;
Takashi Iwaib7dd2b32007-04-26 14:13:44 +0200720 p->csp_programs[index] = program;
Clemens Ladischde66d532006-11-06 09:18:34 +0100721 }
722 return snd_sb_csp_load(p, program->data, program->size, flags);
723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725/*
726 * autoload hardware codec if necessary
727 * return 0 if CSP is loaded and ready to run (p->running != 0)
728 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100729static int snd_sb_csp_autoload(struct snd_sb_csp * p, int pcm_sfmt, int play_rec_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 unsigned long flags;
732 int err = 0;
733
734 /* if CSP is running or manually loaded then exit */
735 if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED))
736 return -EBUSY;
737
738 /* autoload microcode only if requested hardware codec is not already loaded */
739 if (((1 << pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
740 p->running = SNDRV_SB_CSP_ST_AUTO;
741 } else {
742 switch (pcm_sfmt) {
743 case SNDRV_PCM_FORMAT_MU_LAW:
Clemens Ladischde66d532006-11-06 09:18:34 +0100744 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_MULAW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
746 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
747 break;
748 case SNDRV_PCM_FORMAT_A_LAW:
Clemens Ladischde66d532006-11-06 09:18:34 +0100749 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ALAW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
751 p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
752 break;
753 case SNDRV_PCM_FORMAT_IMA_ADPCM:
Clemens Ladischde66d532006-11-06 09:18:34 +0100754 err = snd_sb_csp_firmware_load(p, CSP_PROGRAM_ADPCM_INIT,
755 SNDRV_SB_CSP_LOAD_INITBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (err)
757 break;
758 if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
Clemens Ladischde66d532006-11-06 09:18:34 +0100759 err = snd_sb_csp_firmware_load
760 (p, CSP_PROGRAM_ADPCM_PLAYBACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
762 } else {
Clemens Ladischde66d532006-11-06 09:18:34 +0100763 err = snd_sb_csp_firmware_load
764 (p, CSP_PROGRAM_ADPCM_CAPTURE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
766 }
767 p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
768 break;
769 default:
770 /* Decouple CSP from IRQ and DMAREQ lines */
771 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
772 spin_lock_irqsave(&p->chip->reg_lock, flags);
773 set_mode_register(p->chip, 0xfc);
774 set_mode_register(p->chip, 0x00);
775 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
776 p->running = 0; /* clear autoloaded flag */
777 }
778 return -EINVAL;
779 }
780 if (err) {
781 p->acc_format = 0;
782 p->acc_channels = p->acc_width = p->acc_rates = 0;
783
784 p->running = 0; /* clear autoloaded flag */
785 p->mode = 0;
786 return (err);
787 } else {
788 p->running = SNDRV_SB_CSP_ST_AUTO; /* set autoloaded flag */
789 p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT; /* only 16 bit data */
790 p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
791 p->acc_rates = SNDRV_SB_CSP_RATE_ALL; /* HW codecs accept all rates */
792 }
793
794 }
795 return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
796}
797
798/*
799 * start CSP
800 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100801static int snd_sb_csp_start(struct snd_sb_csp * p, int sample_width, int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 unsigned char s_type; /* sample type */
804 unsigned char mixL, mixR;
805 int result = -EIO;
806 unsigned long flags;
807
808 if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800809 snd_printd("%s: Microcode not loaded\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -ENXIO;
811 }
812 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800813 snd_printd("%s: CSP already running\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return -EBUSY;
815 }
816 if (!(sample_width & p->acc_width)) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800817 snd_printd("%s: Unsupported PCM sample width\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return -EINVAL;
819 }
820 if (!(channels & p->acc_channels)) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800821 snd_printd("%s: Invalid number of channels\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return -EINVAL;
823 }
824
825 /* Mute PCM volume */
826 spin_lock_irqsave(&p->chip->mixer_lock, flags);
827 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
828 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
829 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
830 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
831
832 spin_lock(&p->chip->reg_lock);
833 set_mode_register(p->chip, 0xc0); /* c0 = STOP */
834 set_mode_register(p->chip, 0x70); /* 70 = RUN */
835
836 s_type = 0x00;
837 if (channels == SNDRV_SB_CSP_MONO)
838 s_type = 0x11; /* 000n 000n (n = 1 if mono) */
839 if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
840 s_type |= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
841
842 if (set_codec_parameter(p->chip, 0x81, s_type)) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800843 snd_printd("%s: Set sample type command failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 goto __fail;
845 }
846 if (set_codec_parameter(p->chip, 0x80, 0x00)) {
Harvey Harrison9bf8e7d2008-03-03 15:32:18 -0800847 snd_printd("%s: Codec start command failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 goto __fail;
849 }
850 p->run_width = sample_width;
851 p->run_channels = channels;
852
853 p->running |= SNDRV_SB_CSP_ST_RUNNING;
854
855 if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
856 set_codec_parameter(p->chip, 0xe0, 0x01);
857 /* enable QSound decoder */
858 set_codec_parameter(p->chip, 0x00, 0xff);
859 set_codec_parameter(p->chip, 0x01, 0xff);
860 p->running |= SNDRV_SB_CSP_ST_QSOUND;
861 /* set QSound startup value */
862 snd_sb_csp_qsound_transfer(p);
863 }
864 result = 0;
865
866 __fail:
867 spin_unlock(&p->chip->reg_lock);
868
869 /* restore PCM volume */
870 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
871 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
872 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
873
874 return result;
875}
876
877/*
878 * stop CSP
879 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100880static int snd_sb_csp_stop(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 int result;
883 unsigned char mixL, mixR;
884 unsigned long flags;
885
886 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
887 return 0;
888
889 /* Mute PCM volume */
890 spin_lock_irqsave(&p->chip->mixer_lock, flags);
891 mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
892 mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
893 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
894 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
895
896 spin_lock(&p->chip->reg_lock);
897 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
898 set_codec_parameter(p->chip, 0xe0, 0x01);
899 /* disable QSound decoder */
900 set_codec_parameter(p->chip, 0x00, 0x00);
901 set_codec_parameter(p->chip, 0x01, 0x00);
902
903 p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
904 }
905 result = set_mode_register(p->chip, 0xc0); /* c0 = STOP */
906 spin_unlock(&p->chip->reg_lock);
907
908 /* restore PCM volume */
909 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
910 snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
911 spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
912
913 if (!(result))
914 p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
915 return result;
916}
917
918/*
919 * pause CSP codec and hold DMA transfer
920 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100921static int snd_sb_csp_pause(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 int result;
924 unsigned long flags;
925
926 if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
927 return -EBUSY;
928
929 spin_lock_irqsave(&p->chip->reg_lock, flags);
930 result = set_codec_parameter(p->chip, 0x80, 0xff);
931 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
932 if (!(result))
933 p->running |= SNDRV_SB_CSP_ST_PAUSED;
934
935 return result;
936}
937
938/*
939 * restart CSP codec and resume DMA transfer
940 */
Takashi Iwai029d64b2005-11-17 14:34:36 +0100941static int snd_sb_csp_restart(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 int result;
944 unsigned long flags;
945
946 if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
947 return -EBUSY;
948
949 spin_lock_irqsave(&p->chip->reg_lock, flags);
950 result = set_codec_parameter(p->chip, 0x80, 0x00);
951 spin_unlock_irqrestore(&p->chip->reg_lock, flags);
952 if (!(result))
953 p->running &= ~SNDRV_SB_CSP_ST_PAUSED;
954
955 return result;
956}
957
958/* ------------------------------ */
959
960/*
961 * QSound mixer control for PCM
962 */
963
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200964#define snd_sb_qsound_switch_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Takashi Iwai029d64b2005-11-17 14:34:36 +0100966static int snd_sb_qsound_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100968 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
971 return 0;
972}
973
Takashi Iwai029d64b2005-11-17 14:34:36 +0100974static int snd_sb_qsound_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Takashi Iwai029d64b2005-11-17 14:34:36 +0100976 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 unsigned long flags;
978 int change;
979 unsigned char nval;
980
981 nval = ucontrol->value.integer.value[0] & 0x01;
982 spin_lock_irqsave(&p->q_lock, flags);
983 change = p->q_enabled != nval;
984 p->q_enabled = nval;
985 spin_unlock_irqrestore(&p->q_lock, flags);
986 return change;
987}
988
Takashi Iwai029d64b2005-11-17 14:34:36 +0100989static int snd_sb_qsound_space_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
992 uinfo->count = 2;
993 uinfo->value.integer.min = 0;
994 uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
995 return 0;
996}
997
Takashi Iwai029d64b2005-11-17 14:34:36 +0100998static int snd_sb_qsound_space_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001000 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 unsigned long flags;
1002
1003 spin_lock_irqsave(&p->q_lock, flags);
1004 ucontrol->value.integer.value[0] = p->qpos_left;
1005 ucontrol->value.integer.value[1] = p->qpos_right;
1006 spin_unlock_irqrestore(&p->q_lock, flags);
1007 return 0;
1008}
1009
Takashi Iwai029d64b2005-11-17 14:34:36 +01001010static int snd_sb_qsound_space_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001012 struct snd_sb_csp *p = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 unsigned long flags;
1014 int change;
1015 unsigned char nval1, nval2;
1016
1017 nval1 = ucontrol->value.integer.value[0];
1018 if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1019 nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1020 nval2 = ucontrol->value.integer.value[1];
1021 if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
1022 nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
1023 spin_lock_irqsave(&p->q_lock, flags);
1024 change = p->qpos_left != nval1 || p->qpos_right != nval2;
1025 p->qpos_left = nval1;
1026 p->qpos_right = nval2;
1027 p->qpos_changed = change;
1028 spin_unlock_irqrestore(&p->q_lock, flags);
1029 return change;
1030}
1031
Takashi Iwai029d64b2005-11-17 14:34:36 +01001032static struct snd_kcontrol_new snd_sb_qsound_switch = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1034 .name = "3D Control - Switch",
1035 .info = snd_sb_qsound_switch_info,
1036 .get = snd_sb_qsound_switch_get,
1037 .put = snd_sb_qsound_switch_put
1038};
1039
Takashi Iwai029d64b2005-11-17 14:34:36 +01001040static struct snd_kcontrol_new snd_sb_qsound_space = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1042 .name = "3D Control - Space",
1043 .info = snd_sb_qsound_space_info,
1044 .get = snd_sb_qsound_space_get,
1045 .put = snd_sb_qsound_space_put
1046};
1047
Takashi Iwai029d64b2005-11-17 14:34:36 +01001048static int snd_sb_qsound_build(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001050 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 int err;
1052
Takashi Iwai622207d2008-08-08 17:11:45 +02001053 if (snd_BUG_ON(!p))
1054 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 card = p->chip->card;
1057 p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
1058 p->qpos_changed = 0;
1059
1060 spin_lock_init(&p->q_lock);
1061
1062 if ((err = snd_ctl_add(card, p->qsound_switch = snd_ctl_new1(&snd_sb_qsound_switch, p))) < 0)
1063 goto __error;
1064 if ((err = snd_ctl_add(card, p->qsound_space = snd_ctl_new1(&snd_sb_qsound_space, p))) < 0)
1065 goto __error;
1066
1067 return 0;
1068
1069 __error:
1070 snd_sb_qsound_destroy(p);
1071 return err;
1072}
1073
Takashi Iwai029d64b2005-11-17 14:34:36 +01001074static void snd_sb_qsound_destroy(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001076 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 unsigned long flags;
1078
Takashi Iwai622207d2008-08-08 17:11:45 +02001079 if (snd_BUG_ON(!p))
1080 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 card = p->chip->card;
1083
1084 down_write(&card->controls_rwsem);
1085 if (p->qsound_switch)
1086 snd_ctl_remove(card, p->qsound_switch);
1087 if (p->qsound_space)
1088 snd_ctl_remove(card, p->qsound_space);
1089 up_write(&card->controls_rwsem);
1090
1091 /* cancel pending transfer of QSound parameters */
1092 spin_lock_irqsave (&p->q_lock, flags);
1093 p->qpos_changed = 0;
1094 spin_unlock_irqrestore (&p->q_lock, flags);
1095}
1096
1097/*
1098 * Transfer qsound parameters to CSP,
1099 * function should be called from interrupt routine
1100 */
Takashi Iwai029d64b2005-11-17 14:34:36 +01001101static int snd_sb_csp_qsound_transfer(struct snd_sb_csp * p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 int err = -ENXIO;
1104
1105 spin_lock(&p->q_lock);
1106 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1107 set_codec_parameter(p->chip, 0xe0, 0x01);
1108 /* left channel */
1109 set_codec_parameter(p->chip, 0x00, p->qpos_left);
1110 set_codec_parameter(p->chip, 0x02, 0x00);
1111 /* right channel */
1112 set_codec_parameter(p->chip, 0x00, p->qpos_right);
1113 set_codec_parameter(p->chip, 0x03, 0x00);
1114 err = 0;
1115 }
1116 p->qpos_changed = 0;
1117 spin_unlock(&p->q_lock);
1118 return err;
1119}
1120
1121/* ------------------------------ */
1122
1123/*
1124 * proc interface
1125 */
Takashi Iwai029d64b2005-11-17 14:34:36 +01001126static int init_proc_entry(struct snd_sb_csp * p, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 char name[16];
Takashi Iwai029d64b2005-11-17 14:34:36 +01001129 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 sprintf(name, "cspD%d", device);
1131 if (! snd_card_proc_new(p->chip->card, name, &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001132 snd_info_set_text_ops(entry, p, info_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return 0;
1134}
1135
Takashi Iwai029d64b2005-11-17 14:34:36 +01001136static void info_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Takashi Iwai029d64b2005-11-17 14:34:36 +01001138 struct snd_sb_csp *p = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
1141 snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
1142 ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
1143 ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
1144 ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
1145 if (p->running & SNDRV_SB_CSP_ST_LOADED) {
1146 snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
1147 snd_iprintf(buffer, "Sample rates: ");
1148 if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
1149 snd_iprintf(buffer, "All\n");
1150 } else {
1151 snd_iprintf(buffer, "%s%s%s%s\n",
1152 ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
1153 ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
1154 ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
1155 ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
1156 }
1157 if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
1158 snd_iprintf(buffer, "QSound decoder %sabled\n",
1159 p->q_enabled ? "en" : "dis");
1160 } else {
1161 snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
1162 p->acc_format,
1163 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
1164 ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
1165 ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
1166 ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
1167 ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
1168 ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
1169 }
1170 }
1171 if (p->running & SNDRV_SB_CSP_ST_AUTO) {
1172 snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
1173 }
1174 if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
1175 snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
1176 ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
1177 ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
1178 }
1179 if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
1180 snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
1181 p->qpos_left, p->qpos_right);
1182 }
1183}
1184
1185/* */
1186
1187EXPORT_SYMBOL(snd_sb_csp_new);
1188
1189/*
1190 * INIT part
1191 */
1192
1193static int __init alsa_sb_csp_init(void)
1194{
1195 return 0;
1196}
1197
1198static void __exit alsa_sb_csp_exit(void)
1199{
1200}
1201
1202module_init(alsa_sb_csp_init)
1203module_exit(alsa_sb_csp_exit)