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