Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 | /* * linux/kernel/chr_drv/sound/soundcard.c * * Soundcard driver for Linux */ /* * Copyright by Hannu Savolainen 1993-1996 * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. 2. * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include <linux/config.h> #include "sound_config.h" #include <linux/major.h> static int chrdev_registered = 0; static int sound_major = SOUND_MAJOR; static int is_unloading = 0; /* * Table for permanently allocated memory (used when unloading the module) */ caddr_t sound_mem_blocks[1024]; int sound_num_blocks = 0; static int soundcard_configured = 0; static struct fileinfo files[SND_NDEVS]; static char dma_alloc_map[8] = {0}; #define DMA_MAP_UNAVAIL 0 #define DMA_MAP_FREE 1 #define DMA_MAP_BUSY 2 int snd_ioctl_return (int *addr, int value) { if (value < 0) return value; put_fs_long (value, (long *) &((addr)[0])); return 0; } static int sound_read (inode_handle * inode, file_handle * file, char *buf, int count) { int dev; dev = inode_get_rdev (inode); dev = MINOR (dev); files[dev].flags = file_get_flags (file); return sound_read_sw (dev, &files[dev], buf, count); } static int sound_write (inode_handle * inode, file_handle * file, const char *buf, int count) { int dev; dev = inode_get_rdev (inode); dev = MINOR (dev); files[dev].flags = file_get_flags (file); return sound_write_sw (dev, &files[dev], buf, count); } static int sound_lseek (inode_handle * inode, file_handle * file, off_t offset, int orig) { return -EPERM; } static int sound_open (inode_handle * inode, file_handle * file) { int dev, retval; struct fileinfo tmp_file; if (is_unloading) { printk ("Sound: Driver partially removed. Can't open device\n"); return -EBUSY; } dev = inode_get_rdev (inode); dev = MINOR (dev); if (!soundcard_configured && dev != SND_DEV_CTL && dev != SND_DEV_STATUS) { printk ("SoundCard Error: The soundcard system has not been configured\n"); return -ENXIO; } tmp_file.mode = 0; tmp_file.flags = file_get_flags (file); if ((tmp_file.flags & O_ACCMODE) == O_RDWR) tmp_file.mode = OPEN_READWRITE; if ((tmp_file.flags & O_ACCMODE) == O_RDONLY) tmp_file.mode = OPEN_READ; if ((tmp_file.flags & O_ACCMODE) == O_WRONLY) tmp_file.mode = OPEN_WRITE; if ((retval = sound_open_sw (dev, &tmp_file)) < 0) return retval; #ifdef MODULE MOD_INC_USE_COUNT; #endif memcpy ((char *) &files[dev], (char *) &tmp_file, sizeof (tmp_file)); return retval; } static void sound_release (inode_handle * inode, file_handle * file) { int dev; dev = inode_get_rdev (inode); dev = MINOR (dev); files[dev].flags = file_get_flags (file); sound_release_sw (dev, &files[dev]); #ifdef MODULE MOD_DEC_USE_COUNT; #endif } static int sound_ioctl (inode_handle * inode, file_handle * file, unsigned int cmd, unsigned long arg) { int dev, err; dev = inode_get_rdev (inode); dev = MINOR (dev); files[dev].flags = file_get_flags (file); if (_IOC_DIR (cmd) != _IOC_NONE) { /* * Have to validate the address given by the process. */ int len; len = _IOC_SIZE (cmd); if (_IOC_DIR (cmd) == _IOC_WRITE) { if ((err = verify_area (VERIFY_READ, (void *) arg, len)) < 0) return err; } if (_IOC_DIR (cmd) == _IOC_READ) { if ((err = verify_area (VERIFY_WRITE, (void *) arg, len)) < 0) return err; } } err = sound_ioctl_sw (dev, &files[dev], cmd, (caddr_t) arg); return err; } static int sound_select (inode_handle * inode, file_handle * file, int sel_type, select_table_handle * wait) { int dev; dev = inode_get_rdev (inode); dev = MINOR (dev); files[dev].flags = file_get_flags (file); DEB (printk ("sound_select(dev=%d, type=0x%x)\n", dev, sel_type)); switch (dev & 0x0f) { #ifdef CONFIG_SEQUENCER case SND_DEV_SEQ: case SND_DEV_SEQ2: return sequencer_select (dev, &files[dev], sel_type, wait); break; #endif #ifdef CONFIG_MIDI case SND_DEV_MIDIN: return MIDIbuf_select (dev, &files[dev], sel_type, wait); break; #endif #ifdef CONFIG_AUDIO case SND_DEV_DSP: case SND_DEV_DSP16: case SND_DEV_AUDIO: return audio_select (dev, &files[dev], sel_type, wait); break; #endif default: return 0; } return 0; } static int sound_mmap (inode_handle * inode, file_handle * file, vm_area_handle * vma) { int dev, dev_class; unsigned long size; struct dma_buffparms *dmap = NULL; dev = inode_get_rdev (inode); dev = MINOR (dev); files[dev].flags = file_get_flags (file); dev_class = dev & 0x0f; dev >>= 4; if (dev_class != SND_DEV_DSP && dev_class != SND_DEV_DSP16 && dev_class != SND_DEV_AUDIO) { printk ("Sound: mmap() not supported for other than audio devices\n"); return -EINVAL; } if ((vma_get_flags (vma) & (VM_READ | VM_WRITE)) == (VM_READ | VM_WRITE)) { printk ("Sound: Cannot do read/write mmap()\n"); return -EINVAL; } if (vma_get_flags (vma) & VM_READ) { dmap = audio_devs[dev]->dmap_in; } else if (vma_get_flags (vma) & VM_WRITE) { dmap = audio_devs[dev]->dmap_out; } else { printk ("Sound: Undefined mmap() access\n"); return -EINVAL; } if (dmap == NULL) { printk ("Sound: mmap() error. dmap == NULL\n"); return -EIO; } if (dmap->raw_buf == NULL) { printk ("Sound: mmap() called when raw_buf == NULL\n"); return -EIO; } if (dmap->mapping_flags) { printk ("Sound: mmap() called twice for the same DMA buffer\n"); return -EIO; } if (vma_get_offset (vma) != 0) { printk ("Sound: mmap() offset must be 0.\n"); return -EINVAL; } size = vma_get_end (vma) - vma_get_start (vma); if (size != dmap->bytes_in_use) { printk ("Sound: mmap() size = %ld. Should be %d\n", size, dmap->bytes_in_use); } if (remap_page_range (vma_get_start (vma), dmap->raw_buf_phys, vma_get_end (vma) - vma_get_start (vma), vma_get_page_prot (vma))) return -EAGAIN; vma_set_inode (vma, inode); inode_inc_count (inode); dmap->mapping_flags |= DMA_MAP_MAPPED; memset (dmap->raw_buf, dmap->neutral_byte, dmap->bytes_in_use); return 0; } static struct file_operation_handle sound_fops = { sound_lseek, sound_read, sound_write, NULL, /* sound_readdir */ sound_select, sound_ioctl, sound_mmap, sound_open, sound_release }; void soundcard_init (void) { #ifndef MODULE module_register_chrdev (sound_major, "sound", &sound_fops); chrdev_registered = 1; #endif soundcard_configured = 1; sndtable_init (0); /* Initialize call tables and * detect cards */ if (sndtable_get_cardcount () == 0) return; /* No cards detected */ #ifdef CONFIG_AUDIO if (num_audiodevs) /* Audio devices present */ { DMAbuf_init (0); audio_init (0); } #endif #ifdef CONFIG_MIDI if (num_midis) MIDIbuf_init (0); #endif #ifdef CONFIG_SEQUENCER if (num_midis + num_synths) sequencer_init (0); #endif } static unsigned int irqs = 0; #ifdef MODULE static void free_all_irqs (void) { int i; for (i = 0; i < 31; i++) if (irqs & (1ul << i)) { printk ("Sound warning: IRQ%d was left allocated - fixed.\n", i); snd_release_irq (i); } irqs = 0; } char kernel_version[] = UTS_RELEASE; #endif static int debugmem = 0; /* switched off by default */ static int sound[20] = {0}; int init_module (void) { int err; int ints[21]; int i; if (connect_wrapper (WRAPPER_VERSION) < 0) { printk ("Sound: Incompatible kernel (wrapper) version\n"); return -EINVAL; } /* * "sound=" command line handling by Harald Milz. */ i = 0; while (i < 20 && sound[i]) ints[i + 1] = sound[i++]; ints[0] = i; if (i) sound_setup ("sound=", ints); err = module_register_chrdev (sound_major, "sound", &sound_fops); if (err) { printk ("sound: driver already loaded/included in kernel\n"); return err; } chrdev_registered = 1; soundcard_init (); if (sound_num_blocks >= 1024) printk ("Sound warning: Deallocation table was too small.\n"); return 0; } #ifdef MODULE void cleanup_module (void) { if (MOD_IN_USE) printk ("sound: module busy -- remove delayed\n"); else { int i; if (chrdev_registered) module_unregister_chrdev (sound_major, "sound"); #ifdef CONFIG_SEQUENCER sound_stop_timer (); #endif sound_unload_drivers (); for (i = 0; i < sound_num_blocks; i++) kfree (sound_mem_blocks[i]); free_all_irqs (); /* If something was left allocated by accident */ for (i = 0; i < 8; i++) if (dma_alloc_map[i] != DMA_MAP_UNAVAIL) { printk ("Sound: Hmm, DMA%d was left allocated - fixed\n", i); sound_free_dma (i); } } } #endif void tenmicrosec (int *osp) { int i; for (i = 0; i < 16; i++) inb (0x80); } int snd_set_irq_handler (int interrupt_level, void (*hndlr) (int, struct pt_regs *), char *name, int *osp) { int retcode; retcode = request_irq (interrupt_level, hndlr, 0 /* SA_INTERRUPT */ , name); if (retcode < 0) { printk ("Sound: IRQ%d already in use\n", interrupt_level); } else irqs |= (1ul << interrupt_level); return retcode; } void snd_release_irq (int vect) { irqs &= ~(1ul << vect); free_irq (vect); } int sound_alloc_dma (int chn, char *deviceID) { int err; if ((err = request_dma (chn, deviceID)) != 0) return err; dma_alloc_map[chn] = DMA_MAP_FREE; return 0; } int sound_open_dma (int chn, char *deviceID) { unsigned long flags; save_flags (flags); cli (); if (dma_alloc_map[chn] != DMA_MAP_FREE) { printk ("sound_open_dma: DMA channel %d busy or not allocated\n", chn); restore_flags (flags); return 1; } dma_alloc_map[chn] = DMA_MAP_BUSY; restore_flags (flags); return 0; } void sound_free_dma (int chn) { if (dma_alloc_map[chn] != DMA_MAP_FREE) { /* printk ("sound_free_dma: Bad access to DMA channel %d\n", chn); */ return; } free_dma (chn); dma_alloc_map[chn] = DMA_MAP_UNAVAIL; } void sound_close_dma (int chn) { unsigned long flags; save_flags (flags); cli (); if (dma_alloc_map[chn] != DMA_MAP_BUSY) { printk ("sound_close_dma: Bad access to DMA channel %d\n", chn); restore_flags (flags); return; } dma_alloc_map[chn] = DMA_MAP_FREE; restore_flags (flags); } #ifdef CONFIG_SEQUENCER static struct timer_list seq_timer = {NULL, NULL, 0, 0, sequencer_timer}; void request_sound_timer (int count) { extern unsigned long seq_time; if (count < 0) count = jiffies + (-count); else count += seq_time; ; { seq_timer.expires = ((count - jiffies)) + jiffies; add_timer (&seq_timer); }; } void sound_stop_timer (void) { del_timer (&seq_timer);; } #endif #ifdef CONFIG_AUDIO #ifdef KMALLOC_DMA_BROKEN fatal_error__This_version_is_not_compatible_with_this_kernel; #endif static int dma_buffsize = DSP_BUFFSIZE; int sound_alloc_dmap (int dev, struct dma_buffparms *dmap, int chan) { char *start_addr, *end_addr; int i, dma_pagesize; dmap->mapping_flags &= ~DMA_MAP_MAPPED; if (dmap->raw_buf != NULL) return 0; /* Already done */ if (dma_buffsize < 4096) dma_buffsize = 4096; if (chan < 4) dma_pagesize = 64 * 1024; else dma_pagesize = 128 * 1024; dmap->raw_buf = NULL; if (debugmem) printk ("sound: buffsize[%d] = %lu\n", dev, audio_devs[dev]->buffsize); audio_devs[dev]->buffsize = dma_buffsize; if (audio_devs[dev]->buffsize > dma_pagesize) audio_devs[dev]->buffsize = dma_pagesize; start_addr = NULL; /* * Now loop until we get a free buffer. Try to get smaller buffer if * it fails. */ while (start_addr == NULL && audio_devs[dev]->buffsize > PAGE_SIZE) { int sz, size; for (sz = 0, size = PAGE_SIZE; size < audio_devs[dev]->buffsize; sz++, size <<= 1); audio_devs[dev]->buffsize = PAGE_SIZE * (1 << sz); if ((start_addr = (char *) __get_free_pages (GFP_ATOMIC, sz, MAX_DMA_ADDRESS)) == NULL) audio_devs[dev]->buffsize /= 2; } if (start_addr == NULL) { printk ("Sound error: Couldn't allocate DMA buffer\n"); return -ENOMEM; } else { /* make some checks */ end_addr = start_addr + audio_devs[dev]->buffsize - 1; if (debugmem) printk ("sound: start 0x%lx, end 0x%lx\n", (long) start_addr, (long) end_addr); /* now check if it fits into the same dma-pagesize */ if (((long) start_addr & ~(dma_pagesize - 1)) != ((long) end_addr & ~(dma_pagesize - 1)) || end_addr >= (char *) (MAX_DMA_ADDRESS)) { printk ( "sound: kmalloc returned invalid address 0x%lx for %ld Bytes DMA-buffer\n", (long) start_addr, audio_devs[dev]->buffsize); return -EFAULT; } } dmap->raw_buf = start_addr; dmap->raw_buf_phys = virt_to_bus (start_addr); memset (dmap->raw_buf, 0x00, audio_devs[dev]->buffsize); for (i = MAP_NR (start_addr); i <= MAP_NR (end_addr); i++) { mem_map_reserve (i); } return 0; } void sound_free_dmap (int dev, struct dma_buffparms *dmap) { if (dmap->raw_buf == NULL) return; if (dmap->mapping_flags & DMA_MAP_MAPPED) return; /* Don't free mmapped buffer. Will use it next time */ { int sz, size, i; unsigned long start_addr, end_addr; for (sz = 0, size = PAGE_SIZE; size < audio_devs[dev]->buffsize; sz++, size <<= 1); start_addr = (unsigned long) dmap->raw_buf; end_addr = start_addr + audio_devs[dev]->buffsize; for (i = MAP_NR (start_addr); i <= MAP_NR (end_addr); i++) { mem_map_unreserve (i); } free_pages ((unsigned long) dmap->raw_buf, sz); } dmap->raw_buf = NULL; } int soud_map_buffer (int dev, struct dma_buffparms *dmap, buffmem_desc * info) { printk ("Entered sound_map_buffer()\n"); printk ("Exited sound_map_buffer()\n"); return -EINVAL; } void conf_printf (char *name, struct address_info *hw_config) { if (!trace_init) return; printk ("<%s> at 0x%03x", name, hw_config->io_base); if (hw_config->irq) printk (" irq %d", (hw_config->irq > 0) ? hw_config->irq : -hw_config->irq); if (hw_config->dma != -1 || hw_config->dma2 != -1) { printk (" dma %d", hw_config->dma); if (hw_config->dma2 != -1) printk (",%d", hw_config->dma2); } printk ("\n"); } void conf_printf2 (char *name, int base, int irq, int dma, int dma2) { if (!trace_init) return; printk ("<%s> at 0x%03x", name, base); if (irq) printk (" irq %d", (irq > 0) ? irq : -irq); if (dma != -1 || dma2 != -1) { printk (" dma %d", dma); if (dma2 != -1) printk (",%d", dma2); } printk ("\n"); } #endif |