1 | /*
|
---|
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 |
|
---|
36 | MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
|
---|
37 | MODULE_DESCRIPTION("ALSA driver for SB16 Creative Signal Processor");
|
---|
38 | MODULE_LICENSE("GPL");
|
---|
39 | MODULE_CLASSES("{sound}");
|
---|
40 |
|
---|
41 | #ifdef SNDRV_LITTLE_ENDIAN
|
---|
42 | #define CSP_HDR_VALUE(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))
|
---|
43 | #else
|
---|
44 | #define CSP_HDR_VALUE(a,b,c,d) ((d) | ((c)<<8) | ((b)<<16) | ((a)<<24))
|
---|
45 | #endif
|
---|
46 | #define LE_SHORT(v) le16_to_cpu(v)
|
---|
47 | #define LE_INT(v) le32_to_cpu(v)
|
---|
48 |
|
---|
49 | #define RIFF_HEADER CSP_HDR_VALUE('R', 'I', 'F', 'F')
|
---|
50 | #define CSP__HEADER CSP_HDR_VALUE('C', 'S', 'P', ' ')
|
---|
51 | #define LIST_HEADER CSP_HDR_VALUE('L', 'I', 'S', 'T')
|
---|
52 | #define FUNC_HEADER CSP_HDR_VALUE('f', 'u', 'n', 'c')
|
---|
53 | #define CODE_HEADER CSP_HDR_VALUE('c', 'o', 'd', 'e')
|
---|
54 | #define INIT_HEADER CSP_HDR_VALUE('i', 'n', 'i', 't')
|
---|
55 | #define MAIN_HEADER CSP_HDR_VALUE('m', 'a', 'i', 'n')
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * RIFF data format
|
---|
59 | */
|
---|
60 | typedef struct riff_header {
|
---|
61 | __u32 name;
|
---|
62 | __u32 len;
|
---|
63 | } riff_header_t;
|
---|
64 |
|
---|
65 | typedef struct desc_header {
|
---|
66 | riff_header_t info;
|
---|
67 | __u16 func_nr;
|
---|
68 | __u16 VOC_type;
|
---|
69 | __u16 flags_play_rec;
|
---|
70 | __u16 flags_16bit_8bit;
|
---|
71 | __u16 flags_stereo_mono;
|
---|
72 | __u16 flags_rates;
|
---|
73 | } desc_header_t;
|
---|
74 |
|
---|
75 | /*
|
---|
76 | * prototypes
|
---|
77 | */
|
---|
78 | static void snd_sb_csp_free(snd_hwdep_t *hw);
|
---|
79 | static int snd_sb_csp_open(snd_hwdep_t * hw, struct file *file);
|
---|
80 | static int snd_sb_csp_ioctl(snd_hwdep_t * hw, struct file *file, unsigned int cmd, unsigned long arg);
|
---|
81 | static int snd_sb_csp_release(snd_hwdep_t * hw, struct file *file);
|
---|
82 |
|
---|
83 | static int csp_detect(sb_t *chip, int *version);
|
---|
84 | static int set_codec_parameter(sb_t *chip, unsigned char par, unsigned char val);
|
---|
85 | static int set_register(sb_t *chip, unsigned char reg, unsigned char val);
|
---|
86 | static int read_register(sb_t *chip, unsigned char reg);
|
---|
87 | static int set_mode_register(sb_t *chip, unsigned char mode);
|
---|
88 | static int get_version(sb_t *chip);
|
---|
89 |
|
---|
90 | static int snd_sb_csp_riff_load(snd_sb_csp_t * p, snd_sb_csp_microcode_t * code);
|
---|
91 | static int snd_sb_csp_unload(snd_sb_csp_t * p);
|
---|
92 | static int snd_sb_csp_load(snd_sb_csp_t * p, const unsigned char *buf, int size, int load_flags);
|
---|
93 | static int snd_sb_csp_autoload(snd_sb_csp_t * p, int pcm_sfmt, int play_rec_mode);
|
---|
94 | static int snd_sb_csp_check_version(snd_sb_csp_t * p);
|
---|
95 |
|
---|
96 | static int snd_sb_csp_use(snd_sb_csp_t * p);
|
---|
97 | static int snd_sb_csp_unuse(snd_sb_csp_t * p);
|
---|
98 | static int snd_sb_csp_start(snd_sb_csp_t * p, int sample_width, int channels);
|
---|
99 | static int snd_sb_csp_stop(snd_sb_csp_t * p);
|
---|
100 | static int snd_sb_csp_pause(snd_sb_csp_t * p);
|
---|
101 | static int snd_sb_csp_restart(snd_sb_csp_t * p);
|
---|
102 |
|
---|
103 | static int snd_sb_qsound_build(snd_sb_csp_t * p);
|
---|
104 | static void snd_sb_qsound_destroy(snd_sb_csp_t * p);
|
---|
105 | static int snd_sb_csp_qsound_transfer(snd_sb_csp_t * p);
|
---|
106 |
|
---|
107 | static int init_proc_entry(snd_sb_csp_t * p, int device);
|
---|
108 | static void info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer);
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Detect CSP chip and create a new instance
|
---|
112 | */
|
---|
113 | int snd_sb_csp_new(sb_t *chip, int device, snd_hwdep_t ** rhwdep)
|
---|
114 | {
|
---|
115 | snd_sb_csp_t *p;
|
---|
116 | int version, err;
|
---|
117 | snd_hwdep_t *hw;
|
---|
118 |
|
---|
119 | if (rhwdep)
|
---|
120 | *rhwdep = NULL;
|
---|
121 |
|
---|
122 | if (csp_detect(chip, &version))
|
---|
123 | return -ENODEV;
|
---|
124 |
|
---|
125 | if ((err = snd_hwdep_new(chip->card, "SB16-CSP", device, &hw)) < 0)
|
---|
126 | return err;
|
---|
127 |
|
---|
128 | if ((p = kcalloc(1, sizeof(*p), GFP_KERNEL)) == NULL) {
|
---|
129 | snd_device_free(chip->card, hw);
|
---|
130 | return -ENOMEM;
|
---|
131 | }
|
---|
132 | p->chip = chip;
|
---|
133 | p->version = version;
|
---|
134 |
|
---|
135 | /* CSP operators */
|
---|
136 | p->ops.csp_use = snd_sb_csp_use;
|
---|
137 | p->ops.csp_unuse = snd_sb_csp_unuse;
|
---|
138 | p->ops.csp_autoload = snd_sb_csp_autoload;
|
---|
139 | p->ops.csp_start = snd_sb_csp_start;
|
---|
140 | p->ops.csp_stop = snd_sb_csp_stop;
|
---|
141 | p->ops.csp_qsound_transfer = snd_sb_csp_qsound_transfer;
|
---|
142 |
|
---|
143 | init_MUTEX(&p->access_mutex);
|
---|
144 | sprintf(hw->name, "CSP v%d.%d", (version >> 4), (version & 0x0f));
|
---|
145 | hw->iface = SNDRV_HWDEP_IFACE_SB16CSP;
|
---|
146 | hw->private_data = p;
|
---|
147 | hw->private_free = snd_sb_csp_free;
|
---|
148 |
|
---|
149 | /* operators - only write/ioctl */
|
---|
150 | hw->ops.open = snd_sb_csp_open;
|
---|
151 | hw->ops.ioctl = snd_sb_csp_ioctl;
|
---|
152 | hw->ops.release = snd_sb_csp_release;
|
---|
153 |
|
---|
154 | /* create a proc entry */
|
---|
155 | init_proc_entry(p, device);
|
---|
156 | if (rhwdep)
|
---|
157 | *rhwdep = hw;
|
---|
158 | return 0;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * free_private for hwdep instance
|
---|
163 | */
|
---|
164 | static void snd_sb_csp_free(snd_hwdep_t *hwdep)
|
---|
165 | {
|
---|
166 | snd_sb_csp_t *p = hwdep->private_data;
|
---|
167 | if (p) {
|
---|
168 | if (p->running & SNDRV_SB_CSP_ST_RUNNING)
|
---|
169 | snd_sb_csp_stop(p);
|
---|
170 | kfree(p);
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | /* ------------------------------ */
|
---|
175 |
|
---|
176 | /*
|
---|
177 | * open the device exclusively
|
---|
178 | */
|
---|
179 | static int snd_sb_csp_open(snd_hwdep_t * hw, struct file *file)
|
---|
180 | {
|
---|
181 | snd_sb_csp_t *p = hw->private_data;
|
---|
182 | return (snd_sb_csp_use(p));
|
---|
183 | }
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * ioctl for hwdep device:
|
---|
187 | */
|
---|
188 | static int snd_sb_csp_ioctl(snd_hwdep_t * hw, struct file *file, unsigned int cmd, unsigned long arg)
|
---|
189 | {
|
---|
190 | snd_sb_csp_t *p = hw->private_data;
|
---|
191 | snd_sb_csp_info_t info;
|
---|
192 | snd_sb_csp_start_t start_info;
|
---|
193 | int err;
|
---|
194 |
|
---|
195 | snd_assert(p != NULL, return -EINVAL);
|
---|
196 |
|
---|
197 | if (snd_sb_csp_check_version(p))
|
---|
198 | return -ENODEV;
|
---|
199 |
|
---|
200 | switch (cmd) {
|
---|
201 | /* get information */
|
---|
202 | case SNDRV_SB_CSP_IOCTL_INFO:
|
---|
203 | *info.codec_name = *p->codec_name;
|
---|
204 | info.func_nr = p->func_nr;
|
---|
205 | info.acc_format = p->acc_format;
|
---|
206 | info.acc_channels = p->acc_channels;
|
---|
207 | info.acc_width = p->acc_width;
|
---|
208 | info.acc_rates = p->acc_rates;
|
---|
209 | info.csp_mode = p->mode;
|
---|
210 | info.run_channels = p->run_channels;
|
---|
211 | info.run_width = p->run_width;
|
---|
212 | info.version = p->version;
|
---|
213 | info.state = p->running;
|
---|
214 | if (copy_to_user((void *) arg, &info, sizeof(info)))
|
---|
215 | err = -EFAULT;
|
---|
216 | else
|
---|
217 | err = 0;
|
---|
218 | break;
|
---|
219 |
|
---|
220 | /* load CSP microcode */
|
---|
221 | case SNDRV_SB_CSP_IOCTL_LOAD_CODE:
|
---|
222 | err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
|
---|
223 | -EBUSY : snd_sb_csp_riff_load(p, (snd_sb_csp_microcode_t *) arg));
|
---|
224 | break;
|
---|
225 | case SNDRV_SB_CSP_IOCTL_UNLOAD_CODE:
|
---|
226 | err = (p->running & SNDRV_SB_CSP_ST_RUNNING ?
|
---|
227 | -EBUSY : snd_sb_csp_unload(p));
|
---|
228 | break;
|
---|
229 |
|
---|
230 | /* change CSP running state */
|
---|
231 | case SNDRV_SB_CSP_IOCTL_START:
|
---|
232 | if (copy_from_user(&start_info, (void *) arg, sizeof(start_info)))
|
---|
233 | err = -EFAULT;
|
---|
234 | else
|
---|
235 | err = snd_sb_csp_start(p, start_info.sample_width, start_info.channels);
|
---|
236 | break;
|
---|
237 | case SNDRV_SB_CSP_IOCTL_STOP:
|
---|
238 | err = snd_sb_csp_stop(p);
|
---|
239 | break;
|
---|
240 | case SNDRV_SB_CSP_IOCTL_PAUSE:
|
---|
241 | err = snd_sb_csp_pause(p);
|
---|
242 | break;
|
---|
243 | case SNDRV_SB_CSP_IOCTL_RESTART:
|
---|
244 | err = snd_sb_csp_restart(p);
|
---|
245 | break;
|
---|
246 | default:
|
---|
247 | err = -ENOTTY;
|
---|
248 | break;
|
---|
249 | }
|
---|
250 |
|
---|
251 | return err;
|
---|
252 | }
|
---|
253 |
|
---|
254 | /*
|
---|
255 | * close the device
|
---|
256 | */
|
---|
257 | static int snd_sb_csp_release(snd_hwdep_t * hw, struct file *file)
|
---|
258 | {
|
---|
259 | snd_sb_csp_t *p = hw->private_data;
|
---|
260 | return (snd_sb_csp_unuse(p));
|
---|
261 | }
|
---|
262 |
|
---|
263 | /* ------------------------------ */
|
---|
264 |
|
---|
265 | /*
|
---|
266 | * acquire device
|
---|
267 | */
|
---|
268 | static int snd_sb_csp_use(snd_sb_csp_t * p)
|
---|
269 | {
|
---|
270 | down(&p->access_mutex);
|
---|
271 | if (p->used) {
|
---|
272 | up(&p->access_mutex);
|
---|
273 | return -EAGAIN;
|
---|
274 | }
|
---|
275 | p->used++;
|
---|
276 | up(&p->access_mutex);
|
---|
277 |
|
---|
278 | return 0;
|
---|
279 |
|
---|
280 | }
|
---|
281 |
|
---|
282 | /*
|
---|
283 | * release device
|
---|
284 | */
|
---|
285 | static int snd_sb_csp_unuse(snd_sb_csp_t * p)
|
---|
286 | {
|
---|
287 | down(&p->access_mutex);
|
---|
288 | p->used--;
|
---|
289 | up(&p->access_mutex);
|
---|
290 |
|
---|
291 | return 0;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /*
|
---|
295 | * load microcode via ioctl:
|
---|
296 | * code is user-space pointer
|
---|
297 | */
|
---|
298 | static int snd_sb_csp_riff_load(snd_sb_csp_t * p, snd_sb_csp_microcode_t * mcode)
|
---|
299 | {
|
---|
300 | snd_sb_csp_mc_header_t info;
|
---|
301 |
|
---|
302 | unsigned char *data_ptr, *data_end;
|
---|
303 | unsigned short func_nr = 0;
|
---|
304 |
|
---|
305 | riff_header_t file_h, item_h, code_h;
|
---|
306 | __u32 item_type;
|
---|
307 | desc_header_t funcdesc_h;
|
---|
308 |
|
---|
309 | unsigned long flags;
|
---|
310 | int err;
|
---|
311 |
|
---|
312 | if (copy_from_user(&info, mcode, sizeof(info)))
|
---|
313 | return -EFAULT;
|
---|
314 | data_ptr = mcode->data;
|
---|
315 |
|
---|
316 | if (copy_from_user(&file_h, data_ptr, sizeof(file_h)))
|
---|
317 | return -EFAULT;
|
---|
318 | if ((file_h.name != RIFF_HEADER) ||
|
---|
319 | (LE_INT(file_h.len) >= SNDRV_SB_CSP_MAX_MICROCODE_FILE_SIZE - sizeof(file_h))) {
|
---|
320 | snd_printd("%s: Invalid RIFF header\n", __FUNCTION__);
|
---|
321 | return -EINVAL;
|
---|
322 | }
|
---|
323 | data_ptr += sizeof(file_h);
|
---|
324 | data_end = data_ptr + LE_INT(file_h.len);
|
---|
325 |
|
---|
326 | if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
|
---|
327 | return -EFAULT;
|
---|
328 | if (item_type != CSP__HEADER) {
|
---|
329 | snd_printd("%s: Invalid RIFF file type\n", __FUNCTION__);
|
---|
330 | return -EINVAL;
|
---|
331 | }
|
---|
332 | data_ptr += sizeof (item_type);
|
---|
333 |
|
---|
334 | for (; data_ptr < data_end; data_ptr += LE_INT(item_h.len)) {
|
---|
335 | if (copy_from_user(&item_h, data_ptr, sizeof(item_h)))
|
---|
336 | return -EFAULT;
|
---|
337 | data_ptr += sizeof(item_h);
|
---|
338 | if (item_h.name != LIST_HEADER)
|
---|
339 | continue;
|
---|
340 |
|
---|
341 | if (copy_from_user(&item_type, data_ptr, sizeof(item_type)))
|
---|
342 | return -EFAULT;
|
---|
343 | switch (item_type) {
|
---|
344 | case FUNC_HEADER:
|
---|
345 | if (copy_from_user(&funcdesc_h, data_ptr + sizeof(item_type), sizeof(funcdesc_h)))
|
---|
346 | return -EFAULT;
|
---|
347 | func_nr = LE_SHORT(funcdesc_h.func_nr);
|
---|
348 | break;
|
---|
349 | case CODE_HEADER:
|
---|
350 | if (func_nr != info.func_req)
|
---|
351 | break; /* not required function, try next */
|
---|
352 | data_ptr += sizeof(item_type);
|
---|
353 |
|
---|
354 | /* destroy QSound mixer element */
|
---|
355 | if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
|
---|
356 | snd_sb_qsound_destroy(p);
|
---|
357 | }
|
---|
358 | /* Clear all flags */
|
---|
359 | p->running = 0;
|
---|
360 | p->mode = 0;
|
---|
361 |
|
---|
362 | /* load microcode blocks */
|
---|
363 | for (;;) {
|
---|
364 | if (data_ptr >= data_end)
|
---|
365 | return -EINVAL;
|
---|
366 | if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
|
---|
367 | return -EFAULT;
|
---|
368 |
|
---|
369 | /* init microcode blocks */
|
---|
370 | if (code_h.name != INIT_HEADER)
|
---|
371 | break;
|
---|
372 | data_ptr += sizeof(code_h);
|
---|
373 | err = snd_sb_csp_load(p, data_ptr, LE_INT(code_h.len),
|
---|
374 | SNDRV_SB_CSP_LOAD_INITBLOCK | SNDRV_SB_CSP_LOAD_FROMUSER);
|
---|
375 | if (err)
|
---|
376 | return err;
|
---|
377 | data_ptr += LE_INT(code_h.len);
|
---|
378 | }
|
---|
379 | /* main microcode block */
|
---|
380 | if (copy_from_user(&code_h, data_ptr, sizeof(code_h)))
|
---|
381 | return -EFAULT;
|
---|
382 |
|
---|
383 | if (code_h.name != MAIN_HEADER) {
|
---|
384 | snd_printd("%s: Missing 'main' microcode\n", __FUNCTION__);
|
---|
385 | return -EINVAL;
|
---|
386 | }
|
---|
387 | data_ptr += sizeof(code_h);
|
---|
388 | err = snd_sb_csp_load(p, data_ptr, LE_INT(code_h.len),
|
---|
389 | SNDRV_SB_CSP_LOAD_FROMUSER);
|
---|
390 | if (err)
|
---|
391 | return err;
|
---|
392 |
|
---|
393 | /* fill in codec header */
|
---|
394 | strncpy(p->codec_name, info.codec_name, sizeof(p->codec_name) - 1);
|
---|
395 | p->codec_name[sizeof(p->codec_name) - 1] = 0;
|
---|
396 | p->func_nr = func_nr;
|
---|
397 | p->mode = LE_SHORT(funcdesc_h.flags_play_rec);
|
---|
398 | switch (LE_SHORT(funcdesc_h.VOC_type)) {
|
---|
399 | case 0x0001: /* QSound decoder */
|
---|
400 | if (LE_SHORT(funcdesc_h.flags_play_rec) == SNDRV_SB_CSP_MODE_DSP_WRITE) {
|
---|
401 | if (snd_sb_qsound_build(p) == 0)
|
---|
402 | /* set QSound flag and clear all other mode flags */
|
---|
403 | p->mode = SNDRV_SB_CSP_MODE_QSOUND;
|
---|
404 | }
|
---|
405 | p->acc_format = 0;
|
---|
406 | break;
|
---|
407 | case 0x0006: /* A Law codec */
|
---|
408 | p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
|
---|
409 | break;
|
---|
410 | case 0x0007: /* Mu Law codec */
|
---|
411 | p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
|
---|
412 | break;
|
---|
413 | case 0x0011: /* what Creative thinks is IMA ADPCM codec */
|
---|
414 | case 0x0200: /* Creative ADPCM codec */
|
---|
415 | p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
|
---|
416 | break;
|
---|
417 | case 201: /* Text 2 Speech decoder */
|
---|
418 | /* TODO: Text2Speech handling routines */
|
---|
419 | p->acc_format = 0;
|
---|
420 | break;
|
---|
421 | case 0x0202: /* Fast Speech 8 codec */
|
---|
422 | case 0x0203: /* Fast Speech 10 codec */
|
---|
423 | p->acc_format = SNDRV_PCM_FMTBIT_SPECIAL;
|
---|
424 | break;
|
---|
425 | default: /* other codecs are unsupported */
|
---|
426 | p->acc_format = p->acc_width = p->acc_rates = 0;
|
---|
427 | p->mode = 0;
|
---|
428 | snd_printd("%s: Unsupported CSP codec type: 0x%04x\n",
|
---|
429 | __FUNCTION__,
|
---|
430 | LE_SHORT(funcdesc_h.VOC_type));
|
---|
431 | return -EINVAL;
|
---|
432 | }
|
---|
433 | p->acc_channels = LE_SHORT(funcdesc_h.flags_stereo_mono);
|
---|
434 | p->acc_width = LE_SHORT(funcdesc_h.flags_16bit_8bit);
|
---|
435 | p->acc_rates = LE_SHORT(funcdesc_h.flags_rates);
|
---|
436 |
|
---|
437 | /* Decouple CSP from IRQ and DMAREQ lines */
|
---|
438 | spin_lock_irqsave(&p->chip->reg_lock, flags);
|
---|
439 | set_mode_register(p->chip, 0xfc);
|
---|
440 | set_mode_register(p->chip, 0x00);
|
---|
441 | spin_unlock_irqrestore(&p->chip->reg_lock, flags);
|
---|
442 |
|
---|
443 | /* finished loading successfully */
|
---|
444 | p->running = SNDRV_SB_CSP_ST_LOADED; /* set LOADED flag */
|
---|
445 | return 0;
|
---|
446 | }
|
---|
447 | }
|
---|
448 | snd_printd("%s: Function #%d not found\n", __FUNCTION__, info.func_req);
|
---|
449 | return -EINVAL;
|
---|
450 | }
|
---|
451 |
|
---|
452 | /*
|
---|
453 | * unload CSP microcode
|
---|
454 | */
|
---|
455 | static int snd_sb_csp_unload(snd_sb_csp_t * p)
|
---|
456 | {
|
---|
457 | if (p->running & SNDRV_SB_CSP_ST_RUNNING)
|
---|
458 | return -EBUSY;
|
---|
459 | if (!(p->running & SNDRV_SB_CSP_ST_LOADED))
|
---|
460 | return -ENXIO;
|
---|
461 |
|
---|
462 | /* clear supported formats */
|
---|
463 | p->acc_format = 0;
|
---|
464 | p->acc_channels = p->acc_width = p->acc_rates = 0;
|
---|
465 | /* destroy QSound mixer element */
|
---|
466 | if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
|
---|
467 | snd_sb_qsound_destroy(p);
|
---|
468 | }
|
---|
469 | /* clear all flags */
|
---|
470 | p->running = 0;
|
---|
471 | p->mode = 0;
|
---|
472 | return 0;
|
---|
473 | }
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * send command sequence to DSP
|
---|
477 | */
|
---|
478 | static inline int command_seq(sb_t *chip, const unsigned char *seq, int size)
|
---|
479 | {
|
---|
480 | int i;
|
---|
481 | for (i = 0; i < size; i++) {
|
---|
482 | if (!snd_sbdsp_command(chip, seq[i]))
|
---|
483 | return -EIO;
|
---|
484 | }
|
---|
485 | return 0;
|
---|
486 | }
|
---|
487 |
|
---|
488 | /*
|
---|
489 | * set CSP codec parameter
|
---|
490 | */
|
---|
491 | static int set_codec_parameter(sb_t *chip, unsigned char par, unsigned char val)
|
---|
492 | {
|
---|
493 | unsigned char dsp_cmd[3];
|
---|
494 |
|
---|
495 | dsp_cmd[0] = 0x05; /* CSP set codec parameter */
|
---|
496 | dsp_cmd[1] = val; /* Parameter value */
|
---|
497 | dsp_cmd[2] = par; /* Parameter */
|
---|
498 | command_seq(chip, dsp_cmd, 3);
|
---|
499 | snd_sbdsp_command(chip, 0x03); /* DSP read? */
|
---|
500 | if (snd_sbdsp_get_byte(chip) != par)
|
---|
501 | return -EIO;
|
---|
502 | return 0;
|
---|
503 | }
|
---|
504 |
|
---|
505 | /*
|
---|
506 | * set CSP register
|
---|
507 | */
|
---|
508 | static int set_register(sb_t *chip, unsigned char reg, unsigned char val)
|
---|
509 | {
|
---|
510 | unsigned char dsp_cmd[3];
|
---|
511 |
|
---|
512 | dsp_cmd[0] = 0x0e; /* CSP set register */
|
---|
513 | dsp_cmd[1] = reg; /* CSP Register */
|
---|
514 | dsp_cmd[2] = val; /* value */
|
---|
515 | return command_seq(chip, dsp_cmd, 3);
|
---|
516 | }
|
---|
517 |
|
---|
518 | /*
|
---|
519 | * read CSP register
|
---|
520 | * return < 0 -> error
|
---|
521 | */
|
---|
522 | static int read_register(sb_t *chip, unsigned char reg)
|
---|
523 | {
|
---|
524 | unsigned char dsp_cmd[2];
|
---|
525 |
|
---|
526 | dsp_cmd[0] = 0x0f; /* CSP read register */
|
---|
527 | dsp_cmd[1] = reg; /* CSP Register */
|
---|
528 | command_seq(chip, dsp_cmd, 2);
|
---|
529 | return snd_sbdsp_get_byte(chip); /* Read DSP value */
|
---|
530 | }
|
---|
531 |
|
---|
532 | /*
|
---|
533 | * set CSP mode register
|
---|
534 | */
|
---|
535 | static int set_mode_register(sb_t *chip, unsigned char mode)
|
---|
536 | {
|
---|
537 | unsigned char dsp_cmd[2];
|
---|
538 |
|
---|
539 | dsp_cmd[0] = 0x04; /* CSP set mode register */
|
---|
540 | dsp_cmd[1] = mode; /* mode */
|
---|
541 | return command_seq(chip, dsp_cmd, 2);
|
---|
542 | }
|
---|
543 |
|
---|
544 | /*
|
---|
545 | * Detect CSP
|
---|
546 | * return 0 if CSP exists.
|
---|
547 | */
|
---|
548 | static int csp_detect(sb_t *chip, int *version)
|
---|
549 | {
|
---|
550 | unsigned char csp_test1, csp_test2;
|
---|
551 | unsigned long flags;
|
---|
552 | int result = -ENODEV;
|
---|
553 |
|
---|
554 | spin_lock_irqsave(&chip->reg_lock, flags);
|
---|
555 |
|
---|
556 | set_codec_parameter(chip, 0x00, 0x00);
|
---|
557 | set_mode_register(chip, 0xfc); /* 0xfc = ?? */
|
---|
558 |
|
---|
559 | csp_test1 = read_register(chip, 0x83);
|
---|
560 | set_register(chip, 0x83, ~csp_test1);
|
---|
561 | csp_test2 = read_register(chip, 0x83);
|
---|
562 | if (csp_test2 != (csp_test1 ^ 0xff))
|
---|
563 | goto __fail;
|
---|
564 |
|
---|
565 | set_register(chip, 0x83, csp_test1);
|
---|
566 | csp_test2 = read_register(chip, 0x83);
|
---|
567 | if (csp_test2 != csp_test1)
|
---|
568 | goto __fail;
|
---|
569 |
|
---|
570 | set_mode_register(chip, 0x00); /* 0x00 = ? */
|
---|
571 |
|
---|
572 | *version = get_version(chip);
|
---|
573 | snd_sbdsp_reset(chip); /* reset DSP after getversion! */
|
---|
574 | if (*version >= 0x10 && *version <= 0x1f)
|
---|
575 | result = 0; /* valid version id */
|
---|
576 |
|
---|
577 | __fail:
|
---|
578 | spin_unlock_irqrestore(&chip->reg_lock, flags);
|
---|
579 | return result;
|
---|
580 | }
|
---|
581 |
|
---|
582 | /*
|
---|
583 | * get CSP version number
|
---|
584 | */
|
---|
585 | static int get_version(sb_t *chip)
|
---|
586 | {
|
---|
587 | unsigned char dsp_cmd[2];
|
---|
588 |
|
---|
589 | dsp_cmd[0] = 0x08; /* SB_DSP_!something! */
|
---|
590 | dsp_cmd[1] = 0x03; /* get chip version id? */
|
---|
591 | command_seq(chip, dsp_cmd, 2);
|
---|
592 |
|
---|
593 | return (snd_sbdsp_get_byte(chip));
|
---|
594 | }
|
---|
595 |
|
---|
596 | /*
|
---|
597 | * check if the CSP version is valid
|
---|
598 | */
|
---|
599 | static int snd_sb_csp_check_version(snd_sb_csp_t * p)
|
---|
600 | {
|
---|
601 | if (p->version < 0x10 || p->version > 0x1f) {
|
---|
602 | snd_printd("%s: Invalid CSP version: 0x%x\n", __FUNCTION__, p->version);
|
---|
603 | return 1;
|
---|
604 | }
|
---|
605 | return 0;
|
---|
606 | }
|
---|
607 |
|
---|
608 | /*
|
---|
609 | * download microcode to CSP (microcode should have one "main" block).
|
---|
610 | */
|
---|
611 | static int snd_sb_csp_load(snd_sb_csp_t * p, const unsigned char *buf, int size, int load_flags)
|
---|
612 | {
|
---|
613 | int status, i;
|
---|
614 | int err;
|
---|
615 | int result = -EIO;
|
---|
616 | unsigned long flags;
|
---|
617 |
|
---|
618 | spin_lock_irqsave(&p->chip->reg_lock, flags);
|
---|
619 | snd_sbdsp_command(p->chip, 0x01); /* CSP download command */
|
---|
620 | if (snd_sbdsp_get_byte(p->chip)) {
|
---|
621 | snd_printd("%s: Download command failed\n", __FUNCTION__);
|
---|
622 | goto __fail;
|
---|
623 | }
|
---|
624 | /* Send CSP low byte (size - 1) */
|
---|
625 | snd_sbdsp_command(p->chip, (unsigned char)(size - 1));
|
---|
626 | /* Send high byte */
|
---|
627 | snd_sbdsp_command(p->chip, (unsigned char)((size - 1) >> 8));
|
---|
628 | /* send microcode sequence */
|
---|
629 | if (load_flags & SNDRV_SB_CSP_LOAD_FROMUSER) {
|
---|
630 | /* copy microcode from user space */
|
---|
631 | unsigned char *kbuf, *_kbuf;
|
---|
632 | _kbuf = kbuf = kmalloc (size, GFP_KERNEL);
|
---|
633 | if (copy_from_user(kbuf, buf, size)) {
|
---|
634 | result = -EFAULT;
|
---|
635 | kfree (_kbuf);
|
---|
636 | goto __fail;
|
---|
637 | }
|
---|
638 | while (size--) {
|
---|
639 | if (!snd_sbdsp_command(p->chip, *kbuf++)) {
|
---|
640 | kfree (_kbuf);
|
---|
641 | goto __fail;
|
---|
642 | }
|
---|
643 | }
|
---|
644 | kfree (_kbuf);
|
---|
645 | } else {
|
---|
646 | /* load from kernel space */
|
---|
647 | while (size--) {
|
---|
648 | if (!snd_sbdsp_command(p->chip, *buf++))
|
---|
649 | goto __fail;
|
---|
650 | }
|
---|
651 | }
|
---|
652 | if (snd_sbdsp_get_byte(p->chip))
|
---|
653 | goto __fail;
|
---|
654 |
|
---|
655 | if (load_flags & SNDRV_SB_CSP_LOAD_INITBLOCK) {
|
---|
656 | i = 0;
|
---|
657 | /* some codecs (FastSpeech) take some time to initialize */
|
---|
658 | while (1) {
|
---|
659 | snd_sbdsp_command(p->chip, 0x03);
|
---|
660 | status = snd_sbdsp_get_byte(p->chip);
|
---|
661 | if (status == 0x55 || ++i >= 10)
|
---|
662 | break;
|
---|
663 | udelay (10);
|
---|
664 | }
|
---|
665 | if (status != 0x55) {
|
---|
666 | snd_printd("%s: Microcode initialization failed\n", __FUNCTION__);
|
---|
667 | goto __fail;
|
---|
668 | }
|
---|
669 | } else {
|
---|
670 | /*
|
---|
671 | * Read mixer register SB_DSP4_DMASETUP after loading 'main' code.
|
---|
672 | * Start CSP chip if no 16bit DMA channel is set - some kind
|
---|
673 | * of autorun or perhaps a bugfix?
|
---|
674 | */
|
---|
675 | spin_lock(&p->chip->mixer_lock);
|
---|
676 | status = snd_sbmixer_read(p->chip, SB_DSP4_DMASETUP);
|
---|
677 | spin_unlock(&p->chip->mixer_lock);
|
---|
678 | if (!(status & (SB_DMASETUP_DMA7 | SB_DMASETUP_DMA6 | SB_DMASETUP_DMA5))) {
|
---|
679 | err = (set_codec_parameter(p->chip, 0xaa, 0x00) ||
|
---|
680 | set_codec_parameter(p->chip, 0xff, 0x00));
|
---|
681 | snd_sbdsp_reset(p->chip); /* really! */
|
---|
682 | if (err)
|
---|
683 | goto __fail;
|
---|
684 | set_mode_register(p->chip, 0xc0); /* c0 = STOP */
|
---|
685 | set_mode_register(p->chip, 0x70); /* 70 = RUN */
|
---|
686 | }
|
---|
687 | }
|
---|
688 | result = 0;
|
---|
689 |
|
---|
690 | __fail:
|
---|
691 | spin_unlock_irqrestore(&p->chip->reg_lock, flags);
|
---|
692 | return result;
|
---|
693 | }
|
---|
694 |
|
---|
695 | #include "sb16_csp_codecs.h"
|
---|
696 |
|
---|
697 | /*
|
---|
698 | * autoload hardware codec if necessary
|
---|
699 | * return 0 if CSP is loaded and ready to run (p->running != 0)
|
---|
700 | */
|
---|
701 | static int snd_sb_csp_autoload(snd_sb_csp_t * p, int pcm_sfmt, int play_rec_mode)
|
---|
702 | {
|
---|
703 | unsigned long flags;
|
---|
704 | int err = 0;
|
---|
705 |
|
---|
706 | /* if CSP is running or manually loaded then exit */
|
---|
707 | if (p->running & (SNDRV_SB_CSP_ST_RUNNING | SNDRV_SB_CSP_ST_LOADED))
|
---|
708 | return -EBUSY;
|
---|
709 |
|
---|
710 | /* autoload microcode only if requested hardware codec is not already loaded */
|
---|
711 | if (((1 << pcm_sfmt) & p->acc_format) && (play_rec_mode & p->mode)) {
|
---|
712 | p->running = SNDRV_SB_CSP_ST_AUTO;
|
---|
713 | } else {
|
---|
714 | switch (pcm_sfmt) {
|
---|
715 | case SNDRV_PCM_FORMAT_MU_LAW:
|
---|
716 | err = snd_sb_csp_load(p, &mulaw_main[0], sizeof(mulaw_main), 0);
|
---|
717 | p->acc_format = SNDRV_PCM_FMTBIT_MU_LAW;
|
---|
718 | p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
|
---|
719 | break;
|
---|
720 | case SNDRV_PCM_FORMAT_A_LAW:
|
---|
721 | err = snd_sb_csp_load(p, &alaw_main[0], sizeof(alaw_main), 0);
|
---|
722 | p->acc_format = SNDRV_PCM_FMTBIT_A_LAW;
|
---|
723 | p->mode = SNDRV_SB_CSP_MODE_DSP_READ | SNDRV_SB_CSP_MODE_DSP_WRITE;
|
---|
724 | break;
|
---|
725 | case SNDRV_PCM_FORMAT_IMA_ADPCM:
|
---|
726 | err = snd_sb_csp_load(p, &ima_adpcm_init[0], sizeof(ima_adpcm_init),
|
---|
727 | SNDRV_SB_CSP_LOAD_INITBLOCK);
|
---|
728 | if (err)
|
---|
729 | break;
|
---|
730 | if (play_rec_mode == SNDRV_SB_CSP_MODE_DSP_WRITE) {
|
---|
731 | err = snd_sb_csp_load(p, &ima_adpcm_playback[0],
|
---|
732 | sizeof(ima_adpcm_playback), 0);
|
---|
733 | p->mode = SNDRV_SB_CSP_MODE_DSP_WRITE;
|
---|
734 | } else {
|
---|
735 | err = snd_sb_csp_load(p, &ima_adpcm_capture[0],
|
---|
736 | sizeof(ima_adpcm_capture), 0);
|
---|
737 | p->mode = SNDRV_SB_CSP_MODE_DSP_READ;
|
---|
738 | }
|
---|
739 | p->acc_format = SNDRV_PCM_FMTBIT_IMA_ADPCM;
|
---|
740 | break;
|
---|
741 | default:
|
---|
742 | /* Decouple CSP from IRQ and DMAREQ lines */
|
---|
743 | if (p->running & SNDRV_SB_CSP_ST_AUTO) {
|
---|
744 | spin_lock_irqsave(&p->chip->reg_lock, flags);
|
---|
745 | set_mode_register(p->chip, 0xfc);
|
---|
746 | set_mode_register(p->chip, 0x00);
|
---|
747 | spin_unlock_irqrestore(&p->chip->reg_lock, flags);
|
---|
748 | p->running = 0; /* clear autoloaded flag */
|
---|
749 | }
|
---|
750 | return -EINVAL;
|
---|
751 | }
|
---|
752 | if (err) {
|
---|
753 | p->acc_format = 0;
|
---|
754 | p->acc_channels = p->acc_width = p->acc_rates = 0;
|
---|
755 |
|
---|
756 | p->running = 0; /* clear autoloaded flag */
|
---|
757 | p->mode = 0;
|
---|
758 | return (err);
|
---|
759 | } else {
|
---|
760 | p->running = SNDRV_SB_CSP_ST_AUTO; /* set autoloaded flag */
|
---|
761 | p->acc_width = SNDRV_SB_CSP_SAMPLE_16BIT; /* only 16 bit data */
|
---|
762 | p->acc_channels = SNDRV_SB_CSP_MONO | SNDRV_SB_CSP_STEREO;
|
---|
763 | p->acc_rates = SNDRV_SB_CSP_RATE_ALL; /* HW codecs accept all rates */
|
---|
764 | }
|
---|
765 |
|
---|
766 | }
|
---|
767 | return (p->running & SNDRV_SB_CSP_ST_AUTO) ? 0 : -ENXIO;
|
---|
768 | }
|
---|
769 |
|
---|
770 | /*
|
---|
771 | * start CSP
|
---|
772 | */
|
---|
773 | static int snd_sb_csp_start(snd_sb_csp_t * p, int sample_width, int channels)
|
---|
774 | {
|
---|
775 | unsigned char s_type; /* sample type */
|
---|
776 | unsigned char mixL, mixR;
|
---|
777 | int result = -EIO;
|
---|
778 | unsigned long flags;
|
---|
779 |
|
---|
780 | if (!(p->running & (SNDRV_SB_CSP_ST_LOADED | SNDRV_SB_CSP_ST_AUTO))) {
|
---|
781 | snd_printd("%s: Microcode not loaded\n", __FUNCTION__);
|
---|
782 | return -ENXIO;
|
---|
783 | }
|
---|
784 | if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
|
---|
785 | snd_printd("%s: CSP already running\n", __FUNCTION__);
|
---|
786 | return -EBUSY;
|
---|
787 | }
|
---|
788 | if (!(sample_width & p->acc_width)) {
|
---|
789 | snd_printd("%s: Unsupported PCM sample width\n", __FUNCTION__);
|
---|
790 | return -EINVAL;
|
---|
791 | }
|
---|
792 | if (!(channels & p->acc_channels)) {
|
---|
793 | snd_printd("%s: Invalid number of channels\n", __FUNCTION__);
|
---|
794 | return -EINVAL;
|
---|
795 | }
|
---|
796 |
|
---|
797 | /* Mute PCM volume */
|
---|
798 | spin_lock_irqsave(&p->chip->mixer_lock, flags);
|
---|
799 | mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
|
---|
800 | mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
|
---|
801 | snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
|
---|
802 | snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
|
---|
803 |
|
---|
804 | spin_lock(&p->chip->reg_lock);
|
---|
805 | set_mode_register(p->chip, 0xc0); /* c0 = STOP */
|
---|
806 | set_mode_register(p->chip, 0x70); /* 70 = RUN */
|
---|
807 |
|
---|
808 | s_type = 0x00;
|
---|
809 | if (channels == SNDRV_SB_CSP_MONO)
|
---|
810 | s_type = 0x11; /* 000n 000n (n = 1 if mono) */
|
---|
811 | if (sample_width == SNDRV_SB_CSP_SAMPLE_8BIT)
|
---|
812 | s_type |= 0x22; /* 00dX 00dX (d = 1 if 8 bit samples) */
|
---|
813 |
|
---|
814 | if (set_codec_parameter(p->chip, 0x81, s_type)) {
|
---|
815 | snd_printd("%s: Set sample type command failed\n", __FUNCTION__);
|
---|
816 | goto __fail;
|
---|
817 | }
|
---|
818 | if (set_codec_parameter(p->chip, 0x80, 0x00)) {
|
---|
819 | snd_printd("%s: Codec start command failed\n", __FUNCTION__);
|
---|
820 | goto __fail;
|
---|
821 | }
|
---|
822 | p->run_width = sample_width;
|
---|
823 | p->run_channels = channels;
|
---|
824 |
|
---|
825 | p->running |= SNDRV_SB_CSP_ST_RUNNING;
|
---|
826 |
|
---|
827 | if (p->mode & SNDRV_SB_CSP_MODE_QSOUND) {
|
---|
828 | set_codec_parameter(p->chip, 0xe0, 0x01);
|
---|
829 | /* enable QSound decoder */
|
---|
830 | set_codec_parameter(p->chip, 0x00, 0xff);
|
---|
831 | set_codec_parameter(p->chip, 0x01, 0xff);
|
---|
832 | p->running |= SNDRV_SB_CSP_ST_QSOUND;
|
---|
833 | /* set QSound startup value */
|
---|
834 | snd_sb_csp_qsound_transfer(p);
|
---|
835 | }
|
---|
836 | result = 0;
|
---|
837 |
|
---|
838 | __fail:
|
---|
839 | spin_unlock(&p->chip->reg_lock);
|
---|
840 |
|
---|
841 | /* restore PCM volume */
|
---|
842 | snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
|
---|
843 | snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
|
---|
844 | spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
|
---|
845 |
|
---|
846 | return result;
|
---|
847 | }
|
---|
848 |
|
---|
849 | /*
|
---|
850 | * stop CSP
|
---|
851 | */
|
---|
852 | static int snd_sb_csp_stop(snd_sb_csp_t * p)
|
---|
853 | {
|
---|
854 | int result;
|
---|
855 | unsigned char mixL, mixR;
|
---|
856 | unsigned long flags;
|
---|
857 |
|
---|
858 | if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
|
---|
859 | return 0;
|
---|
860 |
|
---|
861 | /* Mute PCM volume */
|
---|
862 | spin_lock_irqsave(&p->chip->mixer_lock, flags);
|
---|
863 | mixL = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV);
|
---|
864 | mixR = snd_sbmixer_read(p->chip, SB_DSP4_PCM_DEV + 1);
|
---|
865 | snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL & 0x7);
|
---|
866 | snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR & 0x7);
|
---|
867 |
|
---|
868 | spin_lock(&p->chip->reg_lock);
|
---|
869 | if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
|
---|
870 | set_codec_parameter(p->chip, 0xe0, 0x01);
|
---|
871 | /* disable QSound decoder */
|
---|
872 | set_codec_parameter(p->chip, 0x00, 0x00);
|
---|
873 | set_codec_parameter(p->chip, 0x01, 0x00);
|
---|
874 |
|
---|
875 | p->running &= ~SNDRV_SB_CSP_ST_QSOUND;
|
---|
876 | }
|
---|
877 | result = set_mode_register(p->chip, 0xc0); /* c0 = STOP */
|
---|
878 | spin_unlock(&p->chip->reg_lock);
|
---|
879 |
|
---|
880 | /* restore PCM volume */
|
---|
881 | snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV, mixL);
|
---|
882 | snd_sbmixer_write(p->chip, SB_DSP4_PCM_DEV + 1, mixR);
|
---|
883 | spin_unlock_irqrestore(&p->chip->mixer_lock, flags);
|
---|
884 |
|
---|
885 | if (!(result))
|
---|
886 | p->running &= ~(SNDRV_SB_CSP_ST_PAUSED | SNDRV_SB_CSP_ST_RUNNING);
|
---|
887 | return result;
|
---|
888 | }
|
---|
889 |
|
---|
890 | /*
|
---|
891 | * pause CSP codec and hold DMA transfer
|
---|
892 | */
|
---|
893 | static int snd_sb_csp_pause(snd_sb_csp_t * p)
|
---|
894 | {
|
---|
895 | int result;
|
---|
896 | unsigned long flags;
|
---|
897 |
|
---|
898 | if (!(p->running & SNDRV_SB_CSP_ST_RUNNING))
|
---|
899 | return -EBUSY;
|
---|
900 |
|
---|
901 | spin_lock_irqsave(&p->chip->reg_lock, flags);
|
---|
902 | result = set_codec_parameter(p->chip, 0x80, 0xff);
|
---|
903 | spin_unlock_irqrestore(&p->chip->reg_lock, flags);
|
---|
904 | if (!(result))
|
---|
905 | p->running |= SNDRV_SB_CSP_ST_PAUSED;
|
---|
906 |
|
---|
907 | return result;
|
---|
908 | }
|
---|
909 |
|
---|
910 | /*
|
---|
911 | * restart CSP codec and resume DMA transfer
|
---|
912 | */
|
---|
913 | static int snd_sb_csp_restart(snd_sb_csp_t * p)
|
---|
914 | {
|
---|
915 | int result;
|
---|
916 | unsigned long flags;
|
---|
917 |
|
---|
918 | if (!(p->running & SNDRV_SB_CSP_ST_PAUSED))
|
---|
919 | return -EBUSY;
|
---|
920 |
|
---|
921 | spin_lock_irqsave(&p->chip->reg_lock, flags);
|
---|
922 | result = set_codec_parameter(p->chip, 0x80, 0x00);
|
---|
923 | spin_unlock_irqrestore(&p->chip->reg_lock, flags);
|
---|
924 | if (!(result))
|
---|
925 | p->running &= ~SNDRV_SB_CSP_ST_PAUSED;
|
---|
926 |
|
---|
927 | return result;
|
---|
928 | }
|
---|
929 |
|
---|
930 | /* ------------------------------ */
|
---|
931 |
|
---|
932 | /*
|
---|
933 | * QSound mixer control for PCM
|
---|
934 | */
|
---|
935 |
|
---|
936 | static int snd_sb_qsound_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
|
---|
937 | {
|
---|
938 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
|
---|
939 | uinfo->count = 1;
|
---|
940 | uinfo->value.integer.min = 0;
|
---|
941 | uinfo->value.integer.max = 1;
|
---|
942 | return 0;
|
---|
943 | }
|
---|
944 |
|
---|
945 | static int snd_sb_qsound_switch_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
|
---|
946 | {
|
---|
947 | snd_sb_csp_t *p = snd_kcontrol_chip(kcontrol);
|
---|
948 |
|
---|
949 | ucontrol->value.integer.value[0] = p->q_enabled ? 1 : 0;
|
---|
950 | return 0;
|
---|
951 | }
|
---|
952 |
|
---|
953 | static int snd_sb_qsound_switch_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
|
---|
954 | {
|
---|
955 | snd_sb_csp_t *p = snd_kcontrol_chip(kcontrol);
|
---|
956 | unsigned long flags;
|
---|
957 | int change;
|
---|
958 | unsigned char nval;
|
---|
959 |
|
---|
960 | nval = ucontrol->value.integer.value[0] & 0x01;
|
---|
961 | spin_lock_irqsave(&p->q_lock, flags);
|
---|
962 | change = p->q_enabled != nval;
|
---|
963 | p->q_enabled = nval;
|
---|
964 | spin_unlock_irqrestore(&p->q_lock, flags);
|
---|
965 | return change;
|
---|
966 | }
|
---|
967 |
|
---|
968 | static int snd_sb_qsound_space_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
|
---|
969 | {
|
---|
970 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
|
---|
971 | uinfo->count = 2;
|
---|
972 | uinfo->value.integer.min = 0;
|
---|
973 | uinfo->value.integer.max = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
|
---|
974 | return 0;
|
---|
975 | }
|
---|
976 |
|
---|
977 | static int snd_sb_qsound_space_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
|
---|
978 | {
|
---|
979 | snd_sb_csp_t *p = snd_kcontrol_chip(kcontrol);
|
---|
980 | unsigned long flags;
|
---|
981 |
|
---|
982 | spin_lock_irqsave(&p->q_lock, flags);
|
---|
983 | ucontrol->value.integer.value[0] = p->qpos_left;
|
---|
984 | ucontrol->value.integer.value[1] = p->qpos_right;
|
---|
985 | spin_unlock_irqrestore(&p->q_lock, flags);
|
---|
986 | return 0;
|
---|
987 | }
|
---|
988 |
|
---|
989 | static int snd_sb_qsound_space_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
|
---|
990 | {
|
---|
991 | snd_sb_csp_t *p = snd_kcontrol_chip(kcontrol);
|
---|
992 | unsigned long flags;
|
---|
993 | int change;
|
---|
994 | unsigned char nval1, nval2;
|
---|
995 |
|
---|
996 | nval1 = ucontrol->value.integer.value[0];
|
---|
997 | if (nval1 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
|
---|
998 | nval1 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
|
---|
999 | nval2 = ucontrol->value.integer.value[1];
|
---|
1000 | if (nval2 > SNDRV_SB_CSP_QSOUND_MAX_RIGHT)
|
---|
1001 | nval2 = SNDRV_SB_CSP_QSOUND_MAX_RIGHT;
|
---|
1002 | spin_lock_irqsave(&p->q_lock, flags);
|
---|
1003 | change = p->qpos_left != nval1 || p->qpos_right != nval2;
|
---|
1004 | p->qpos_left = nval1;
|
---|
1005 | p->qpos_right = nval2;
|
---|
1006 | p->qpos_changed = change;
|
---|
1007 | spin_unlock_irqrestore(&p->q_lock, flags);
|
---|
1008 | return change;
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | static snd_kcontrol_new_t snd_sb_qsound_switch = {
|
---|
1012 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
|
---|
1013 | .name = "3D Control - Switch",
|
---|
1014 | .info = snd_sb_qsound_switch_info,
|
---|
1015 | .get = snd_sb_qsound_switch_get,
|
---|
1016 | .put = snd_sb_qsound_switch_put
|
---|
1017 | };
|
---|
1018 |
|
---|
1019 | static snd_kcontrol_new_t snd_sb_qsound_space = {
|
---|
1020 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
|
---|
1021 | .name = "3D Control - Space",
|
---|
1022 | .info = snd_sb_qsound_space_info,
|
---|
1023 | .get = snd_sb_qsound_space_get,
|
---|
1024 | .put = snd_sb_qsound_space_put
|
---|
1025 | };
|
---|
1026 |
|
---|
1027 | static int snd_sb_qsound_build(snd_sb_csp_t * p)
|
---|
1028 | {
|
---|
1029 | snd_card_t * card;
|
---|
1030 | int err;
|
---|
1031 |
|
---|
1032 | snd_assert(p != NULL, return -EINVAL);
|
---|
1033 |
|
---|
1034 | card = p->chip->card;
|
---|
1035 | p->qpos_left = p->qpos_right = SNDRV_SB_CSP_QSOUND_MAX_RIGHT / 2;
|
---|
1036 | p->qpos_changed = 0;
|
---|
1037 |
|
---|
1038 | spin_lock_init(&p->q_lock);
|
---|
1039 |
|
---|
1040 | if ((err = snd_ctl_add(card, p->qsound_switch = snd_ctl_new1(&snd_sb_qsound_switch, p))) < 0)
|
---|
1041 | goto __error;
|
---|
1042 | if ((err = snd_ctl_add(card, p->qsound_space = snd_ctl_new1(&snd_sb_qsound_space, p))) < 0)
|
---|
1043 | goto __error;
|
---|
1044 |
|
---|
1045 | return 0;
|
---|
1046 |
|
---|
1047 | __error:
|
---|
1048 | snd_sb_qsound_destroy(p);
|
---|
1049 | return err;
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | static void snd_sb_qsound_destroy(snd_sb_csp_t * p)
|
---|
1053 | {
|
---|
1054 | snd_card_t * card;
|
---|
1055 | unsigned long flags;
|
---|
1056 |
|
---|
1057 | snd_assert(p != NULL, return);
|
---|
1058 |
|
---|
1059 | card = p->chip->card;
|
---|
1060 |
|
---|
1061 | if (p->qsound_switch)
|
---|
1062 | snd_ctl_remove(card, p->qsound_switch);
|
---|
1063 | if (p->qsound_space)
|
---|
1064 | snd_ctl_remove(card, p->qsound_space);
|
---|
1065 |
|
---|
1066 | /* cancel pending transfer of QSound parameters */
|
---|
1067 | spin_lock_irqsave (&p->q_lock, flags);
|
---|
1068 | p->qpos_changed = 0;
|
---|
1069 | spin_unlock_irqrestore (&p->q_lock, flags);
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | /*
|
---|
1073 | * Transfer qsound parameters to CSP,
|
---|
1074 | * function should be called from interrupt routine
|
---|
1075 | */
|
---|
1076 | static int snd_sb_csp_qsound_transfer(snd_sb_csp_t * p)
|
---|
1077 | {
|
---|
1078 | int err = -ENXIO;
|
---|
1079 |
|
---|
1080 | spin_lock(&p->q_lock);
|
---|
1081 | if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
|
---|
1082 | set_codec_parameter(p->chip, 0xe0, 0x01);
|
---|
1083 | /* left channel */
|
---|
1084 | set_codec_parameter(p->chip, 0x00, p->qpos_left);
|
---|
1085 | set_codec_parameter(p->chip, 0x02, 0x00);
|
---|
1086 | /* right channel */
|
---|
1087 | set_codec_parameter(p->chip, 0x00, p->qpos_right);
|
---|
1088 | set_codec_parameter(p->chip, 0x03, 0x00);
|
---|
1089 | err = 0;
|
---|
1090 | }
|
---|
1091 | p->qpos_changed = 0;
|
---|
1092 | spin_unlock(&p->q_lock);
|
---|
1093 | return err;
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | /* ------------------------------ */
|
---|
1097 |
|
---|
1098 | /*
|
---|
1099 | * proc interface
|
---|
1100 | */
|
---|
1101 | static int init_proc_entry(snd_sb_csp_t * p, int device)
|
---|
1102 | {
|
---|
1103 | char name[16];
|
---|
1104 | snd_info_entry_t *entry;
|
---|
1105 | sprintf(name, "cspD%d", device);
|
---|
1106 | if (! snd_card_proc_new(p->chip->card, name, &entry))
|
---|
1107 | snd_info_set_text_ops(entry, p, info_read);
|
---|
1108 | return 0;
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | static void info_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
|
---|
1112 | {
|
---|
1113 | snd_sb_csp_t *p = entry->private_data;
|
---|
1114 |
|
---|
1115 | snd_iprintf(buffer, "Creative Signal Processor [v%d.%d]\n", (p->version >> 4), (p->version & 0x0f));
|
---|
1116 | snd_iprintf(buffer, "State: %cx%c%c%c\n", ((p->running & SNDRV_SB_CSP_ST_QSOUND) ? 'Q' : '-'),
|
---|
1117 | ((p->running & SNDRV_SB_CSP_ST_PAUSED) ? 'P' : '-'),
|
---|
1118 | ((p->running & SNDRV_SB_CSP_ST_RUNNING) ? 'R' : '-'),
|
---|
1119 | ((p->running & SNDRV_SB_CSP_ST_LOADED) ? 'L' : '-'));
|
---|
1120 | if (p->running & SNDRV_SB_CSP_ST_LOADED) {
|
---|
1121 | snd_iprintf(buffer, "Codec: %s [func #%d]\n", p->codec_name, p->func_nr);
|
---|
1122 | snd_iprintf(buffer, "Sample rates: ");
|
---|
1123 | if (p->acc_rates == SNDRV_SB_CSP_RATE_ALL) {
|
---|
1124 | snd_iprintf(buffer, "All\n");
|
---|
1125 | } else {
|
---|
1126 | snd_iprintf(buffer, "%s%s%s%s\n",
|
---|
1127 | ((p->acc_rates & SNDRV_SB_CSP_RATE_8000) ? "8000Hz " : ""),
|
---|
1128 | ((p->acc_rates & SNDRV_SB_CSP_RATE_11025) ? "11025Hz " : ""),
|
---|
1129 | ((p->acc_rates & SNDRV_SB_CSP_RATE_22050) ? "22050Hz " : ""),
|
---|
1130 | ((p->acc_rates & SNDRV_SB_CSP_RATE_44100) ? "44100Hz" : ""));
|
---|
1131 | }
|
---|
1132 | if (p->mode == SNDRV_SB_CSP_MODE_QSOUND) {
|
---|
1133 | snd_iprintf(buffer, "QSound decoder %sabled\n",
|
---|
1134 | p->q_enabled ? "en" : "dis");
|
---|
1135 | } else {
|
---|
1136 | snd_iprintf(buffer, "PCM format ID: 0x%x (%s/%s) [%s/%s] [%s/%s]\n",
|
---|
1137 | p->acc_format,
|
---|
1138 | ((p->acc_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? "16bit" : "-"),
|
---|
1139 | ((p->acc_width & SNDRV_SB_CSP_SAMPLE_8BIT) ? "8bit" : "-"),
|
---|
1140 | ((p->acc_channels & SNDRV_SB_CSP_MONO) ? "mono" : "-"),
|
---|
1141 | ((p->acc_channels & SNDRV_SB_CSP_STEREO) ? "stereo" : "-"),
|
---|
1142 | ((p->mode & SNDRV_SB_CSP_MODE_DSP_WRITE) ? "playback" : "-"),
|
---|
1143 | ((p->mode & SNDRV_SB_CSP_MODE_DSP_READ) ? "capture" : "-"));
|
---|
1144 | }
|
---|
1145 | }
|
---|
1146 | if (p->running & SNDRV_SB_CSP_ST_AUTO) {
|
---|
1147 | snd_iprintf(buffer, "Autoloaded Mu-Law, A-Law or Ima-ADPCM hardware codec\n");
|
---|
1148 | }
|
---|
1149 | if (p->running & SNDRV_SB_CSP_ST_RUNNING) {
|
---|
1150 | snd_iprintf(buffer, "Processing %dbit %s PCM samples\n",
|
---|
1151 | ((p->run_width & SNDRV_SB_CSP_SAMPLE_16BIT) ? 16 : 8),
|
---|
1152 | ((p->run_channels & SNDRV_SB_CSP_MONO) ? "mono" : "stereo"));
|
---|
1153 | }
|
---|
1154 | if (p->running & SNDRV_SB_CSP_ST_QSOUND) {
|
---|
1155 | snd_iprintf(buffer, "Qsound position: left = 0x%x, right = 0x%x\n",
|
---|
1156 | p->qpos_left, p->qpos_right);
|
---|
1157 | }
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | /* */
|
---|
1161 |
|
---|
1162 | EXPORT_SYMBOL(snd_sb_csp_new);
|
---|
1163 |
|
---|
1164 | /*
|
---|
1165 | * INIT part
|
---|
1166 | */
|
---|
1167 |
|
---|
1168 | static int __init alsa_sb_csp_init(void)
|
---|
1169 | {
|
---|
1170 | return 0;
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | static void __exit alsa_sb_csp_exit(void)
|
---|
1174 | {
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | module_init(alsa_sb_csp_init)
|
---|
1178 | module_exit(alsa_sb_csp_exit)
|
---|