Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * linux/sound/oss/soundcard.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Sound card driver for Linux |
| 5 | * |
| 6 | * |
| 7 | * Copyright (C) by Hannu Savolainen 1993-1997 |
| 8 | * |
| 9 | * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL) |
| 10 | * Version 2 (June 1991). See the "COPYING" file distributed with this software |
| 11 | * for more info. |
| 12 | * |
| 13 | * |
| 14 | * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed) |
| 15 | * integrated sound_switch.c |
| 16 | * Stefan Reinauer : integrated /proc/sound (equals to /dev/sndstat, |
| 17 | * which should disappear in the near future) |
| 18 | * Eric Dumas : devfs support (22-Jan-98) <dumas@linux.eu.org> with |
| 19 | * fixups by C. Scott Ananian <cananian@alumni.princeton.edu> |
| 20 | * Richard Gooch : moved common (non OSS-specific) devices to sound_core.c |
| 21 | * Rob Riggs : Added persistent DMA buffers support (1998/10/17) |
| 22 | * Christoph Hellwig : Some cleanup work (2000/03/01) |
| 23 | */ |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include "sound_config.h" |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/types.h> |
| 29 | #include <linux/errno.h> |
| 30 | #include <linux/signal.h> |
| 31 | #include <linux/fcntl.h> |
| 32 | #include <linux/ctype.h> |
| 33 | #include <linux/stddef.h> |
| 34 | #include <linux/kmod.h> |
| 35 | #include <asm/dma.h> |
| 36 | #include <asm/io.h> |
| 37 | #include <linux/wait.h> |
| 38 | #include <linux/slab.h> |
| 39 | #include <linux/ioport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/major.h> |
| 41 | #include <linux/delay.h> |
| 42 | #include <linux/proc_fs.h> |
| 43 | #include <linux/smp_lock.h> |
| 44 | #include <linux/module.h> |
Al Viro | f23f6e0 | 2006-10-20 15:17:02 -0400 | [diff] [blame] | 45 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * This ought to be moved into include/asm/dma.h |
| 49 | */ |
| 50 | #ifndef valid_dma |
| 51 | #define valid_dma(n) ((n) >= 0 && (n) < MAX_DMA_CHANNELS && (n) != 4) |
| 52 | #endif |
| 53 | |
| 54 | /* |
| 55 | * Table for permanently allocated memory (used when unloading the module) |
| 56 | */ |
| 57 | void * sound_mem_blocks[1024]; |
| 58 | int sound_nblocks = 0; |
| 59 | |
| 60 | /* Persistent DMA buffers */ |
| 61 | #ifdef CONFIG_SOUND_DMAP |
| 62 | int sound_dmap_flag = 1; |
| 63 | #else |
| 64 | int sound_dmap_flag = 0; |
| 65 | #endif |
| 66 | |
| 67 | static char dma_alloc_map[MAX_DMA_CHANNELS]; |
| 68 | |
| 69 | #define DMA_MAP_UNAVAIL 0 |
| 70 | #define DMA_MAP_FREE 1 |
| 71 | #define DMA_MAP_BUSY 2 |
| 72 | |
| 73 | |
| 74 | unsigned long seq_time = 0; /* Time for /dev/sequencer */ |
gregkh@suse.de | 619e666 | 2005-03-23 09:51:41 -0800 | [diff] [blame] | 75 | extern struct class *sound_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Table for configurable mixer volume handling |
| 79 | */ |
| 80 | static mixer_vol_table mixer_vols[MAX_MIXER_DEV]; |
| 81 | static int num_mixer_volumes; |
| 82 | |
| 83 | int *load_mixer_volumes(char *name, int *levels, int present) |
| 84 | { |
| 85 | int i, n; |
| 86 | |
| 87 | for (i = 0; i < num_mixer_volumes; i++) { |
| 88 | if (strcmp(name, mixer_vols[i].name) == 0) { |
| 89 | if (present) |
| 90 | mixer_vols[i].num = i; |
| 91 | return mixer_vols[i].levels; |
| 92 | } |
| 93 | } |
| 94 | if (num_mixer_volumes >= MAX_MIXER_DEV) { |
| 95 | printk(KERN_ERR "Sound: Too many mixers (%s)\n", name); |
| 96 | return levels; |
| 97 | } |
| 98 | n = num_mixer_volumes++; |
| 99 | |
| 100 | strcpy(mixer_vols[n].name, name); |
| 101 | |
| 102 | if (present) |
| 103 | mixer_vols[n].num = n; |
| 104 | else |
| 105 | mixer_vols[n].num = -1; |
| 106 | |
| 107 | for (i = 0; i < 32; i++) |
| 108 | mixer_vols[n].levels[i] = levels[i]; |
| 109 | return mixer_vols[n].levels; |
| 110 | } |
Adrian Bunk | ece7f77 | 2006-10-04 02:17:31 -0700 | [diff] [blame] | 111 | EXPORT_SYMBOL(load_mixer_volumes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | static int set_mixer_levels(void __user * arg) |
| 114 | { |
| 115 | /* mixer_vol_table is 174 bytes, so IMHO no reason to not allocate it on the stack */ |
| 116 | mixer_vol_table buf; |
| 117 | |
| 118 | if (__copy_from_user(&buf, arg, sizeof(buf))) |
| 119 | return -EFAULT; |
| 120 | load_mixer_volumes(buf.name, buf.levels, 0); |
| 121 | if (__copy_to_user(arg, &buf, sizeof(buf))) |
| 122 | return -EFAULT; |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int get_mixer_levels(void __user * arg) |
| 127 | { |
| 128 | int n; |
| 129 | |
| 130 | if (__get_user(n, (int __user *)(&(((mixer_vol_table __user *)arg)->num)))) |
| 131 | return -EFAULT; |
| 132 | if (n < 0 || n >= num_mixer_volumes) |
| 133 | return -EINVAL; |
| 134 | if (__copy_to_user(arg, &mixer_vols[n], sizeof(mixer_vol_table))) |
| 135 | return -EFAULT; |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | /* 4K page size but our output routines use some slack for overruns */ |
| 140 | #define PROC_BLOCK_SIZE (3*1024) |
| 141 | |
| 142 | static ssize_t sound_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 143 | { |
Josef Sipek | 7bc5632 | 2006-12-08 02:37:40 -0800 | [diff] [blame] | 144 | int dev = iminor(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | int ret = -EINVAL; |
| 146 | |
| 147 | /* |
| 148 | * The OSS drivers aren't remotely happy without this locking, |
| 149 | * and unless someone fixes them when they are about to bite the |
| 150 | * big one anyway, we might as well bandage here.. |
| 151 | */ |
| 152 | |
| 153 | lock_kernel(); |
| 154 | |
| 155 | DEB(printk("sound_read(dev=%d, count=%d)\n", dev, count)); |
| 156 | switch (dev & 0x0f) { |
| 157 | case SND_DEV_DSP: |
| 158 | case SND_DEV_DSP16: |
| 159 | case SND_DEV_AUDIO: |
| 160 | ret = audio_read(dev, file, buf, count); |
| 161 | break; |
| 162 | |
| 163 | case SND_DEV_SEQ: |
| 164 | case SND_DEV_SEQ2: |
| 165 | ret = sequencer_read(dev, file, buf, count); |
| 166 | break; |
| 167 | |
| 168 | case SND_DEV_MIDIN: |
| 169 | ret = MIDIbuf_read(dev, file, buf, count); |
| 170 | } |
| 171 | unlock_kernel(); |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | static ssize_t sound_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 176 | { |
Josef Sipek | 7bc5632 | 2006-12-08 02:37:40 -0800 | [diff] [blame] | 177 | int dev = iminor(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | int ret = -EINVAL; |
| 179 | |
| 180 | lock_kernel(); |
| 181 | DEB(printk("sound_write(dev=%d, count=%d)\n", dev, count)); |
| 182 | switch (dev & 0x0f) { |
| 183 | case SND_DEV_SEQ: |
| 184 | case SND_DEV_SEQ2: |
| 185 | ret = sequencer_write(dev, file, buf, count); |
| 186 | break; |
| 187 | |
| 188 | case SND_DEV_DSP: |
| 189 | case SND_DEV_DSP16: |
| 190 | case SND_DEV_AUDIO: |
| 191 | ret = audio_write(dev, file, buf, count); |
| 192 | break; |
| 193 | |
| 194 | case SND_DEV_MIDIN: |
| 195 | ret = MIDIbuf_write(dev, file, buf, count); |
| 196 | break; |
| 197 | } |
| 198 | unlock_kernel(); |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | static int sound_open(struct inode *inode, struct file *file) |
| 203 | { |
| 204 | int dev = iminor(inode); |
| 205 | int retval; |
| 206 | |
| 207 | DEB(printk("sound_open(dev=%d)\n", dev)); |
| 208 | if ((dev >= SND_NDEVS) || (dev < 0)) { |
| 209 | printk(KERN_ERR "Invalid minor device %d\n", dev); |
| 210 | return -ENXIO; |
| 211 | } |
| 212 | switch (dev & 0x0f) { |
| 213 | case SND_DEV_CTL: |
| 214 | dev >>= 4; |
| 215 | if (dev >= 0 && dev < MAX_MIXER_DEV && mixer_devs[dev] == NULL) { |
| 216 | request_module("mixer%d", dev); |
| 217 | } |
| 218 | if (dev && (dev >= num_mixers || mixer_devs[dev] == NULL)) |
| 219 | return -ENXIO; |
| 220 | |
| 221 | if (!try_module_get(mixer_devs[dev]->owner)) |
| 222 | return -ENXIO; |
| 223 | break; |
| 224 | |
| 225 | case SND_DEV_SEQ: |
| 226 | case SND_DEV_SEQ2: |
| 227 | if ((retval = sequencer_open(dev, file)) < 0) |
| 228 | return retval; |
| 229 | break; |
| 230 | |
| 231 | case SND_DEV_MIDIN: |
| 232 | if ((retval = MIDIbuf_open(dev, file)) < 0) |
| 233 | return retval; |
| 234 | break; |
| 235 | |
| 236 | case SND_DEV_DSP: |
| 237 | case SND_DEV_DSP16: |
| 238 | case SND_DEV_AUDIO: |
| 239 | if ((retval = audio_open(dev, file)) < 0) |
| 240 | return retval; |
| 241 | break; |
| 242 | |
| 243 | default: |
| 244 | printk(KERN_ERR "Invalid minor device %d\n", dev); |
| 245 | return -ENXIO; |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static int sound_release(struct inode *inode, struct file *file) |
| 252 | { |
| 253 | int dev = iminor(inode); |
| 254 | |
| 255 | lock_kernel(); |
| 256 | DEB(printk("sound_release(dev=%d)\n", dev)); |
| 257 | switch (dev & 0x0f) { |
| 258 | case SND_DEV_CTL: |
| 259 | module_put(mixer_devs[dev >> 4]->owner); |
| 260 | break; |
| 261 | |
| 262 | case SND_DEV_SEQ: |
| 263 | case SND_DEV_SEQ2: |
| 264 | sequencer_release(dev, file); |
| 265 | break; |
| 266 | |
| 267 | case SND_DEV_MIDIN: |
| 268 | MIDIbuf_release(dev, file); |
| 269 | break; |
| 270 | |
| 271 | case SND_DEV_DSP: |
| 272 | case SND_DEV_DSP16: |
| 273 | case SND_DEV_AUDIO: |
| 274 | audio_release(dev, file); |
| 275 | break; |
| 276 | |
| 277 | default: |
| 278 | printk(KERN_ERR "Sound error: Releasing unknown device 0x%02x\n", dev); |
| 279 | } |
| 280 | unlock_kernel(); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int get_mixer_info(int dev, void __user *arg) |
| 286 | { |
| 287 | mixer_info info; |
| 288 | memset(&info, 0, sizeof(info)); |
| 289 | strlcpy(info.id, mixer_devs[dev]->id, sizeof(info.id)); |
| 290 | strlcpy(info.name, mixer_devs[dev]->name, sizeof(info.name)); |
| 291 | info.modify_counter = mixer_devs[dev]->modify_counter; |
| 292 | if (__copy_to_user(arg, &info, sizeof(info))) |
| 293 | return -EFAULT; |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int get_old_mixer_info(int dev, void __user *arg) |
| 298 | { |
| 299 | _old_mixer_info info; |
| 300 | memset(&info, 0, sizeof(info)); |
| 301 | strlcpy(info.id, mixer_devs[dev]->id, sizeof(info.id)); |
| 302 | strlcpy(info.name, mixer_devs[dev]->name, sizeof(info.name)); |
| 303 | if (copy_to_user(arg, &info, sizeof(info))) |
| 304 | return -EFAULT; |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int sound_mixer_ioctl(int mixdev, unsigned int cmd, void __user *arg) |
| 309 | { |
| 310 | if (mixdev < 0 || mixdev >= MAX_MIXER_DEV) |
| 311 | return -ENXIO; |
| 312 | /* Try to load the mixer... */ |
| 313 | if (mixer_devs[mixdev] == NULL) { |
| 314 | request_module("mixer%d", mixdev); |
| 315 | } |
| 316 | if (mixdev >= num_mixers || !mixer_devs[mixdev]) |
| 317 | return -ENXIO; |
| 318 | if (cmd == SOUND_MIXER_INFO) |
| 319 | return get_mixer_info(mixdev, arg); |
| 320 | if (cmd == SOUND_OLD_MIXER_INFO) |
| 321 | return get_old_mixer_info(mixdev, arg); |
| 322 | if (_SIOC_DIR(cmd) & _SIOC_WRITE) |
| 323 | mixer_devs[mixdev]->modify_counter++; |
| 324 | if (!mixer_devs[mixdev]->ioctl) |
| 325 | return -EINVAL; |
| 326 | return mixer_devs[mixdev]->ioctl(mixdev, cmd, arg); |
| 327 | } |
| 328 | |
| 329 | static int sound_ioctl(struct inode *inode, struct file *file, |
| 330 | unsigned int cmd, unsigned long arg) |
| 331 | { |
| 332 | int len = 0, dtype; |
| 333 | int dev = iminor(inode); |
| 334 | void __user *p = (void __user *)arg; |
| 335 | |
| 336 | if (_SIOC_DIR(cmd) != _SIOC_NONE && _SIOC_DIR(cmd) != 0) { |
| 337 | /* |
| 338 | * Have to validate the address given by the process. |
| 339 | */ |
| 340 | len = _SIOC_SIZE(cmd); |
| 341 | if (len < 1 || len > 65536 || !p) |
| 342 | return -EFAULT; |
| 343 | if (_SIOC_DIR(cmd) & _SIOC_WRITE) |
| 344 | if (!access_ok(VERIFY_READ, p, len)) |
| 345 | return -EFAULT; |
| 346 | if (_SIOC_DIR(cmd) & _SIOC_READ) |
| 347 | if (!access_ok(VERIFY_WRITE, p, len)) |
| 348 | return -EFAULT; |
| 349 | } |
| 350 | DEB(printk("sound_ioctl(dev=%d, cmd=0x%x, arg=0x%x)\n", dev, cmd, arg)); |
| 351 | if (cmd == OSS_GETVERSION) |
| 352 | return __put_user(SOUND_VERSION, (int __user *)p); |
| 353 | |
| 354 | if (_IOC_TYPE(cmd) == 'M' && num_mixers > 0 && /* Mixer ioctl */ |
| 355 | (dev & 0x0f) != SND_DEV_CTL) { |
| 356 | dtype = dev & 0x0f; |
| 357 | switch (dtype) { |
| 358 | case SND_DEV_DSP: |
| 359 | case SND_DEV_DSP16: |
| 360 | case SND_DEV_AUDIO: |
| 361 | return sound_mixer_ioctl(audio_devs[dev >> 4]->mixer_dev, |
| 362 | cmd, p); |
| 363 | |
| 364 | default: |
| 365 | return sound_mixer_ioctl(dev >> 4, cmd, p); |
| 366 | } |
| 367 | } |
| 368 | switch (dev & 0x0f) { |
| 369 | case SND_DEV_CTL: |
| 370 | if (cmd == SOUND_MIXER_GETLEVELS) |
| 371 | return get_mixer_levels(p); |
| 372 | if (cmd == SOUND_MIXER_SETLEVELS) |
| 373 | return set_mixer_levels(p); |
| 374 | return sound_mixer_ioctl(dev >> 4, cmd, p); |
| 375 | |
| 376 | case SND_DEV_SEQ: |
| 377 | case SND_DEV_SEQ2: |
| 378 | return sequencer_ioctl(dev, file, cmd, p); |
| 379 | |
| 380 | case SND_DEV_DSP: |
| 381 | case SND_DEV_DSP16: |
| 382 | case SND_DEV_AUDIO: |
| 383 | return audio_ioctl(dev, file, cmd, p); |
| 384 | break; |
| 385 | |
| 386 | case SND_DEV_MIDIN: |
| 387 | return MIDIbuf_ioctl(dev, file, cmd, p); |
| 388 | break; |
| 389 | |
| 390 | } |
| 391 | return -EINVAL; |
| 392 | } |
| 393 | |
| 394 | static unsigned int sound_poll(struct file *file, poll_table * wait) |
| 395 | { |
Josef Sipek | 7bc5632 | 2006-12-08 02:37:40 -0800 | [diff] [blame] | 396 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | int dev = iminor(inode); |
| 398 | |
| 399 | DEB(printk("sound_poll(dev=%d)\n", dev)); |
| 400 | switch (dev & 0x0f) { |
| 401 | case SND_DEV_SEQ: |
| 402 | case SND_DEV_SEQ2: |
| 403 | return sequencer_poll(dev, file, wait); |
| 404 | |
| 405 | case SND_DEV_MIDIN: |
| 406 | return MIDIbuf_poll(dev, file, wait); |
| 407 | |
| 408 | case SND_DEV_DSP: |
| 409 | case SND_DEV_DSP16: |
| 410 | case SND_DEV_AUDIO: |
| 411 | return DMAbuf_poll(file, dev >> 4, wait); |
| 412 | } |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static int sound_mmap(struct file *file, struct vm_area_struct *vma) |
| 417 | { |
| 418 | int dev_class; |
| 419 | unsigned long size; |
| 420 | struct dma_buffparms *dmap = NULL; |
Josef Sipek | 7bc5632 | 2006-12-08 02:37:40 -0800 | [diff] [blame] | 421 | int dev = iminor(file->f_path.dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
| 423 | dev_class = dev & 0x0f; |
| 424 | dev >>= 4; |
| 425 | |
| 426 | if (dev_class != SND_DEV_DSP && dev_class != SND_DEV_DSP16 && dev_class != SND_DEV_AUDIO) { |
| 427 | printk(KERN_ERR "Sound: mmap() not supported for other than audio devices\n"); |
| 428 | return -EINVAL; |
| 429 | } |
| 430 | lock_kernel(); |
| 431 | if (vma->vm_flags & VM_WRITE) /* Map write and read/write to the output buf */ |
| 432 | dmap = audio_devs[dev]->dmap_out; |
| 433 | else if (vma->vm_flags & VM_READ) |
| 434 | dmap = audio_devs[dev]->dmap_in; |
| 435 | else { |
| 436 | printk(KERN_ERR "Sound: Undefined mmap() access\n"); |
| 437 | unlock_kernel(); |
| 438 | return -EINVAL; |
| 439 | } |
| 440 | |
| 441 | if (dmap == NULL) { |
| 442 | printk(KERN_ERR "Sound: mmap() error. dmap == NULL\n"); |
| 443 | unlock_kernel(); |
| 444 | return -EIO; |
| 445 | } |
| 446 | if (dmap->raw_buf == NULL) { |
| 447 | printk(KERN_ERR "Sound: mmap() called when raw_buf == NULL\n"); |
| 448 | unlock_kernel(); |
| 449 | return -EIO; |
| 450 | } |
| 451 | if (dmap->mapping_flags) { |
| 452 | printk(KERN_ERR "Sound: mmap() called twice for the same DMA buffer\n"); |
| 453 | unlock_kernel(); |
| 454 | return -EIO; |
| 455 | } |
| 456 | if (vma->vm_pgoff != 0) { |
| 457 | printk(KERN_ERR "Sound: mmap() offset must be 0.\n"); |
| 458 | unlock_kernel(); |
| 459 | return -EINVAL; |
| 460 | } |
| 461 | size = vma->vm_end - vma->vm_start; |
| 462 | |
| 463 | if (size != dmap->bytes_in_use) { |
| 464 | printk(KERN_WARNING "Sound: mmap() size = %ld. Should be %d\n", size, dmap->bytes_in_use); |
| 465 | } |
| 466 | if (remap_pfn_range(vma, vma->vm_start, |
| 467 | virt_to_phys(dmap->raw_buf) >> PAGE_SHIFT, |
| 468 | vma->vm_end - vma->vm_start, vma->vm_page_prot)) { |
| 469 | unlock_kernel(); |
| 470 | return -EAGAIN; |
| 471 | } |
| 472 | |
| 473 | dmap->mapping_flags |= DMA_MAP_MAPPED; |
| 474 | |
| 475 | if( audio_devs[dev]->d->mmap) |
| 476 | audio_devs[dev]->d->mmap(dev); |
| 477 | |
| 478 | memset(dmap->raw_buf, |
| 479 | dmap->neutral_byte, |
| 480 | dmap->bytes_in_use); |
| 481 | unlock_kernel(); |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | struct file_operations oss_sound_fops = { |
| 486 | .owner = THIS_MODULE, |
| 487 | .llseek = no_llseek, |
| 488 | .read = sound_read, |
| 489 | .write = sound_write, |
| 490 | .poll = sound_poll, |
| 491 | .ioctl = sound_ioctl, |
| 492 | .mmap = sound_mmap, |
| 493 | .open = sound_open, |
| 494 | .release = sound_release, |
| 495 | }; |
| 496 | |
| 497 | /* |
| 498 | * Create the required special subdevices |
| 499 | */ |
| 500 | |
| 501 | static int create_special_devices(void) |
| 502 | { |
| 503 | int seq1,seq2; |
| 504 | seq1=register_sound_special(&oss_sound_fops, 1); |
| 505 | if(seq1==-1) |
| 506 | goto bad; |
| 507 | seq2=register_sound_special(&oss_sound_fops, 8); |
| 508 | if(seq2!=-1) |
| 509 | return 0; |
| 510 | unregister_sound_special(1); |
| 511 | bad: |
| 512 | return -1; |
| 513 | } |
| 514 | |
| 515 | |
| 516 | /* These device names follow the official Linux device list, |
| 517 | * Documentation/devices.txt. Let us know if there are other |
| 518 | * common names we should support for compatibility. |
| 519 | * Only those devices not created by the generic code in sound_core.c are |
| 520 | * registered here. |
| 521 | */ |
| 522 | static const struct { |
| 523 | unsigned short minor; |
| 524 | char *name; |
| 525 | umode_t mode; |
| 526 | int *num; |
| 527 | } dev_list[] = { /* list of minor devices */ |
| 528 | /* seems to be some confusion here -- this device is not in the device list */ |
| 529 | {SND_DEV_DSP16, "dspW", S_IWUGO | S_IRUSR | S_IRGRP, |
| 530 | &num_audiodevs}, |
| 531 | {SND_DEV_AUDIO, "audio", S_IWUGO | S_IRUSR | S_IRGRP, |
| 532 | &num_audiodevs}, |
| 533 | }; |
| 534 | |
| 535 | static int dmabuf; |
| 536 | static int dmabug; |
| 537 | |
| 538 | module_param(dmabuf, int, 0444); |
| 539 | module_param(dmabug, int, 0444); |
| 540 | |
| 541 | static int __init oss_init(void) |
| 542 | { |
| 543 | int err; |
| 544 | int i, j; |
| 545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | #ifdef CONFIG_PCI |
| 547 | if(dmabug) |
| 548 | isa_dma_bridge_buggy = dmabug; |
| 549 | #endif |
| 550 | |
| 551 | err = create_special_devices(); |
| 552 | if (err) { |
| 553 | printk(KERN_ERR "sound: driver already loaded/included in kernel\n"); |
| 554 | return err; |
| 555 | } |
| 556 | |
| 557 | /* Protecting the innocent */ |
| 558 | sound_dmap_flag = (dmabuf > 0 ? 1 : 0); |
| 559 | |
| 560 | for (i = 0; i < sizeof (dev_list) / sizeof *dev_list; i++) { |
Greg Kroah-Hartman | d80f19f | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 561 | device_create(sound_class, NULL, |
| 562 | MKDEV(SOUND_MAJOR, dev_list[i].minor), |
| 563 | "%s", dev_list[i].name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
| 565 | if (!dev_list[i].num) |
| 566 | continue; |
| 567 | |
Greg Kroah-Hartman | 0936f26 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 568 | for (j = 1; j < *dev_list[i].num; j++) |
Greg Kroah-Hartman | d80f19f | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 569 | device_create(sound_class, NULL, |
| 570 | MKDEV(SOUND_MAJOR, dev_list[i].minor + (j*0x10)), |
| 571 | "%s%d", dev_list[i].name, j); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | if (sound_nblocks >= 1024) |
| 575 | printk(KERN_ERR "Sound warning: Deallocation table was too small.\n"); |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static void __exit oss_cleanup(void) |
| 581 | { |
| 582 | int i, j; |
| 583 | |
| 584 | for (i = 0; i < sizeof (dev_list) / sizeof *dev_list; i++) { |
Greg Kroah-Hartman | d80f19f | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 585 | device_destroy(sound_class, MKDEV(SOUND_MAJOR, dev_list[i].minor)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | if (!dev_list[i].num) |
| 587 | continue; |
Greg Kroah-Hartman | 0936f26 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 588 | for (j = 1; j < *dev_list[i].num; j++) |
Greg Kroah-Hartman | d80f19f | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 589 | device_destroy(sound_class, MKDEV(SOUND_MAJOR, dev_list[i].minor + (j*0x10))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | unregister_sound_special(1); |
| 593 | unregister_sound_special(8); |
| 594 | |
| 595 | sound_stop_timer(); |
| 596 | |
| 597 | sequencer_unload(); |
| 598 | |
| 599 | for (i = 0; i < MAX_DMA_CHANNELS; i++) |
| 600 | if (dma_alloc_map[i] != DMA_MAP_UNAVAIL) { |
| 601 | printk(KERN_ERR "Sound: Hmm, DMA%d was left allocated - fixed\n", i); |
| 602 | sound_free_dma(i); |
| 603 | } |
| 604 | |
| 605 | for (i = 0; i < sound_nblocks; i++) |
| 606 | vfree(sound_mem_blocks[i]); |
| 607 | |
| 608 | } |
| 609 | |
| 610 | module_init(oss_init); |
| 611 | module_exit(oss_cleanup); |
| 612 | MODULE_LICENSE("GPL"); |
Adrian Bunk | ece7f77 | 2006-10-04 02:17:31 -0700 | [diff] [blame] | 613 | MODULE_DESCRIPTION("OSS Sound subsystem"); |
| 614 | MODULE_AUTHOR("Hannu Savolainen, et al."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
| 616 | |
| 617 | int sound_alloc_dma(int chn, char *deviceID) |
| 618 | { |
| 619 | int err; |
| 620 | |
| 621 | if ((err = request_dma(chn, deviceID)) != 0) |
| 622 | return err; |
| 623 | |
| 624 | dma_alloc_map[chn] = DMA_MAP_FREE; |
| 625 | |
| 626 | return 0; |
| 627 | } |
Adrian Bunk | ece7f77 | 2006-10-04 02:17:31 -0700 | [diff] [blame] | 628 | EXPORT_SYMBOL(sound_alloc_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
| 630 | int sound_open_dma(int chn, char *deviceID) |
| 631 | { |
| 632 | if (!valid_dma(chn)) { |
| 633 | printk(KERN_ERR "sound_open_dma: Invalid DMA channel %d\n", chn); |
| 634 | return 1; |
| 635 | } |
| 636 | |
| 637 | if (dma_alloc_map[chn] != DMA_MAP_FREE) { |
| 638 | printk("sound_open_dma: DMA channel %d busy or not allocated (%d)\n", chn, dma_alloc_map[chn]); |
| 639 | return 1; |
| 640 | } |
| 641 | dma_alloc_map[chn] = DMA_MAP_BUSY; |
| 642 | return 0; |
| 643 | } |
Adrian Bunk | ece7f77 | 2006-10-04 02:17:31 -0700 | [diff] [blame] | 644 | EXPORT_SYMBOL(sound_open_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
| 646 | void sound_free_dma(int chn) |
| 647 | { |
| 648 | if (dma_alloc_map[chn] == DMA_MAP_UNAVAIL) { |
| 649 | /* printk( "sound_free_dma: Bad access to DMA channel %d\n", chn); */ |
| 650 | return; |
| 651 | } |
| 652 | free_dma(chn); |
| 653 | dma_alloc_map[chn] = DMA_MAP_UNAVAIL; |
| 654 | } |
Adrian Bunk | ece7f77 | 2006-10-04 02:17:31 -0700 | [diff] [blame] | 655 | EXPORT_SYMBOL(sound_free_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
| 657 | void sound_close_dma(int chn) |
| 658 | { |
| 659 | if (dma_alloc_map[chn] != DMA_MAP_BUSY) { |
| 660 | printk(KERN_ERR "sound_close_dma: Bad access to DMA channel %d\n", chn); |
| 661 | return; |
| 662 | } |
| 663 | dma_alloc_map[chn] = DMA_MAP_FREE; |
| 664 | } |
Adrian Bunk | ece7f77 | 2006-10-04 02:17:31 -0700 | [diff] [blame] | 665 | EXPORT_SYMBOL(sound_close_dma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
| 667 | static void do_sequencer_timer(unsigned long dummy) |
| 668 | { |
| 669 | sequencer_timer(0); |
| 670 | } |
| 671 | |
| 672 | |
Ingo Molnar | 8d06afa | 2005-09-09 13:10:40 -0700 | [diff] [blame] | 673 | static DEFINE_TIMER(seq_timer, do_sequencer_timer, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
| 675 | void request_sound_timer(int count) |
| 676 | { |
| 677 | extern unsigned long seq_time; |
| 678 | |
| 679 | if (count < 0) { |
| 680 | seq_timer.expires = (-count) + jiffies; |
| 681 | add_timer(&seq_timer); |
| 682 | return; |
| 683 | } |
| 684 | count += seq_time; |
| 685 | |
| 686 | count -= jiffies; |
| 687 | |
| 688 | if (count < 1) |
| 689 | count = 1; |
| 690 | |
| 691 | seq_timer.expires = (count) + jiffies; |
| 692 | add_timer(&seq_timer); |
| 693 | } |
| 694 | |
| 695 | void sound_stop_timer(void) |
| 696 | { |
| 697 | del_timer(&seq_timer); |
| 698 | } |
| 699 | |
| 700 | void conf_printf(char *name, struct address_info *hw_config) |
| 701 | { |
| 702 | #ifndef CONFIG_SOUND_TRACEINIT |
| 703 | return; |
| 704 | #else |
| 705 | printk("<%s> at 0x%03x", name, hw_config->io_base); |
| 706 | |
| 707 | if (hw_config->irq) |
| 708 | printk(" irq %d", (hw_config->irq > 0) ? hw_config->irq : -hw_config->irq); |
| 709 | |
| 710 | if (hw_config->dma != -1 || hw_config->dma2 != -1) |
| 711 | { |
| 712 | printk(" dma %d", hw_config->dma); |
| 713 | if (hw_config->dma2 != -1) |
| 714 | printk(",%d", hw_config->dma2); |
| 715 | } |
| 716 | printk("\n"); |
| 717 | #endif |
| 718 | } |
Adrian Bunk | ece7f77 | 2006-10-04 02:17:31 -0700 | [diff] [blame] | 719 | EXPORT_SYMBOL(conf_printf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
| 721 | void conf_printf2(char *name, int base, int irq, int dma, int dma2) |
| 722 | { |
| 723 | #ifndef CONFIG_SOUND_TRACEINIT |
| 724 | return; |
| 725 | #else |
| 726 | printk("<%s> at 0x%03x", name, base); |
| 727 | |
| 728 | if (irq) |
| 729 | printk(" irq %d", (irq > 0) ? irq : -irq); |
| 730 | |
| 731 | if (dma != -1 || dma2 != -1) |
| 732 | { |
| 733 | printk(" dma %d", dma); |
| 734 | if (dma2 != -1) |
| 735 | printk(",%d", dma2); |
| 736 | } |
| 737 | printk("\n"); |
| 738 | #endif |
| 739 | } |
Adrian Bunk | ece7f77 | 2006-10-04 02:17:31 -0700 | [diff] [blame] | 740 | EXPORT_SYMBOL(conf_printf2); |
| 741 | |