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