Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / sound / soundcard.c
blob87ec7f181421199cccab76a8bf3d69727eb94691
1 /*
2 * linux/drivers/sound/soundcard.c
4 * Sound card driver for Linux
7 * Copyright (C) by Hannu Savolainen 1993-1997
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.
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)
25 #include <linux/config.h>
27 #include "sound_config.h"
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/errno.h>
31 #include <linux/signal.h>
32 #include <linux/fcntl.h>
33 #include <linux/ctype.h>
34 #include <linux/stddef.h>
35 #include <linux/kmod.h>
36 #include <asm/dma.h>
37 #include <asm/io.h>
38 #include <asm/segment.h>
39 #include <linux/wait.h>
40 #include <linux/malloc.h>
41 #include <linux/ioport.h>
42 #include <linux/devfs_fs_kernel.h>
43 #include <linux/major.h>
44 #include <linux/delay.h>
45 #include <linux/proc_fs.h>
46 #include <linux/smp_lock.h>
49 * This ought to be moved into include/asm/dma.h
51 #ifndef valid_dma
52 #define valid_dma(n) ((n) >= 0 && (n) < MAX_DMA_CHANNELS && (n) != 4)
53 #endif
56 * Table for permanently allocated memory (used when unloading the module)
58 caddr_t sound_mem_blocks[1024];
59 int sound_nblocks = 0;
61 /* Persistent DMA buffers */
62 #ifdef CONFIG_SOUND_DMAP
63 int sound_dmap_flag = 1;
64 #else
65 int sound_dmap_flag = 0;
66 #endif
68 static char dma_alloc_map[MAX_DMA_CHANNELS] = {0};
70 #define DMA_MAP_UNAVAIL 0
71 #define DMA_MAP_FREE 1
72 #define DMA_MAP_BUSY 2
75 unsigned long seq_time = 0; /* Time for /dev/sequencer */
78 * Table for configurable mixer volume handling
80 static mixer_vol_table mixer_vols[MAX_MIXER_DEV];
81 static int num_mixer_volumes = 0;
83 int *load_mixer_volumes(char *name, int *levels, int present)
85 int i, n;
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;
94 if (num_mixer_volumes >= MAX_MIXER_DEV) {
95 printk(KERN_ERR "Sound: Too many mixers (%s)\n", name);
96 return levels;
98 n = num_mixer_volumes++;
100 strcpy(mixer_vols[n].name, name);
102 if (present)
103 mixer_vols[n].num = n;
104 else
105 mixer_vols[n].num = -1;
107 for (i = 0; i < 32; i++)
108 mixer_vols[n].levels[i] = levels[i];
109 return mixer_vols[n].levels;
112 static int set_mixer_levels(caddr_t arg)
114 /* mixer_vol_table is 174 bytes, so IMHO no reason to not allocate it on the stack */
115 mixer_vol_table buf;
117 if (__copy_from_user(&buf, arg, sizeof(buf)))
118 return -EFAULT;
119 load_mixer_volumes(buf.name, buf.levels, 0);
120 if (__copy_to_user(arg, &buf, sizeof(buf)))
121 return -EFAULT;
122 return 0;
125 static int get_mixer_levels(caddr_t arg)
127 int n;
129 if (__get_user(n, (int *)(&(((mixer_vol_table *)arg)->num))))
130 return -EFAULT;
131 if (n < 0 || n >= num_mixer_volumes)
132 return -EINVAL;
133 if (__copy_to_user(arg, &mixer_vols[n], sizeof(mixer_vol_table)))
134 return -EFAULT;
135 return 0;
138 #ifndef MIN
139 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
140 #endif
142 /* 4K page size but our output routines use some slack for overruns */
143 #define PROC_BLOCK_SIZE (3*1024)
145 static ssize_t sound_read(struct file *file, char *buf, size_t count, loff_t *ppos)
147 int dev = MINOR(file->f_dentry->d_inode->i_rdev);
148 int ret = -EINVAL;
151 * The OSS drivers aren't remotely happy without this locking,
152 * and unless someone fixes them when they are about to bite the
153 * big one anyway, we might as well bandage here..
156 lock_kernel();
158 DEB(printk("sound_read(dev=%d, count=%d)\n", dev, count));
159 switch (dev & 0x0f) {
160 case SND_DEV_DSP:
161 case SND_DEV_DSP16:
162 case SND_DEV_AUDIO:
163 ret = audio_read(dev, file, buf, count);
164 break;
166 case SND_DEV_SEQ:
167 case SND_DEV_SEQ2:
168 ret = sequencer_read(dev, file, buf, count);
169 break;
171 case SND_DEV_MIDIN:
172 ret = MIDIbuf_read(dev, file, buf, count);
174 unlock_kernel();
175 return ret;
178 static ssize_t sound_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
180 int dev = MINOR(file->f_dentry->d_inode->i_rdev);
181 int ret = -EINVAL;
183 lock_kernel();
184 DEB(printk("sound_write(dev=%d, count=%d)\n", dev, count));
185 switch (dev & 0x0f) {
186 case SND_DEV_SEQ:
187 case SND_DEV_SEQ2:
188 ret = sequencer_write(dev, file, buf, count);
189 break;
191 case SND_DEV_DSP:
192 case SND_DEV_DSP16:
193 case SND_DEV_AUDIO:
194 ret = audio_write(dev, file, buf, count);
195 break;
197 case SND_DEV_MIDIN:
198 ret = MIDIbuf_write(dev, file, buf, count);
199 break;
201 unlock_kernel();
202 return ret;
205 static long long sound_lseek(struct file *file, long long offset, int orig)
207 return -ESPIPE;
210 static int sound_open(struct inode *inode, struct file *file)
212 int dev = MINOR(inode->i_rdev);
213 int retval;
215 DEB(printk("sound_open(dev=%d)\n", dev));
216 if ((dev >= SND_NDEVS) || (dev < 0)) {
217 printk(KERN_ERR "Invalid minor device %d\n", dev);
218 return -ENXIO;
220 switch (dev & 0x0f) {
221 case SND_DEV_CTL:
222 dev >>= 4;
223 if (dev >= 0 && dev < MAX_MIXER_DEV && mixer_devs[dev] == NULL) {
224 char modname[20];
225 sprintf(modname, "mixer%d", dev);
226 request_module(modname);
228 if (dev && (dev >= num_mixers || mixer_devs[dev] == NULL))
229 return -ENXIO;
231 if (mixer_devs[dev]->owner)
232 __MOD_INC_USE_COUNT (mixer_devs[dev]->owner);
233 break;
235 case SND_DEV_SEQ:
236 case SND_DEV_SEQ2:
237 if ((retval = sequencer_open(dev, file)) < 0)
238 return retval;
239 break;
241 case SND_DEV_MIDIN:
242 if ((retval = MIDIbuf_open(dev, file)) < 0)
243 return retval;
244 break;
246 case SND_DEV_DSP:
247 case SND_DEV_DSP16:
248 case SND_DEV_AUDIO:
249 if ((retval = audio_open(dev, file)) < 0)
250 return retval;
251 break;
253 default:
254 printk(KERN_ERR "Invalid minor device %d\n", dev);
255 return -ENXIO;
258 return 0;
261 static int sound_release(struct inode *inode, struct file *file)
263 int dev = MINOR(inode->i_rdev);
265 lock_kernel();
266 DEB(printk("sound_release(dev=%d)\n", dev));
267 switch (dev & 0x0f) {
268 case SND_DEV_CTL:
269 dev >>= 4;
270 if (mixer_devs[dev]->owner)
271 __MOD_DEC_USE_COUNT (mixer_devs[dev]->owner);
272 break;
274 case SND_DEV_SEQ:
275 case SND_DEV_SEQ2:
276 sequencer_release(dev, file);
277 break;
279 case SND_DEV_MIDIN:
280 MIDIbuf_release(dev, file);
281 break;
283 case SND_DEV_DSP:
284 case SND_DEV_DSP16:
285 case SND_DEV_AUDIO:
286 audio_release(dev, file);
287 break;
289 default:
290 printk(KERN_ERR "Sound error: Releasing unknown device 0x%02x\n", dev);
292 unlock_kernel();
294 return 0;
297 static int get_mixer_info(int dev, caddr_t arg)
299 mixer_info info;
301 strncpy(info.id, mixer_devs[dev]->id, sizeof(info.id));
302 strncpy(info.name, mixer_devs[dev]->name, sizeof(info.name));
303 info.name[sizeof(info.name)-1] = 0;
304 info.modify_counter = mixer_devs[dev]->modify_counter;
305 if (__copy_to_user(arg, &info, sizeof(info)))
306 return -EFAULT;
307 return 0;
310 static int get_old_mixer_info(int dev, caddr_t arg)
312 _old_mixer_info info;
314 strncpy(info.id, mixer_devs[dev]->id, sizeof(info.id));
315 strncpy(info.name, mixer_devs[dev]->name, sizeof(info.name));
316 info.name[sizeof(info.name)-1] = 0;
317 if (copy_to_user(arg, &info, sizeof(info)))
318 return -EFAULT;
319 return 0;
322 static int sound_mixer_ioctl(int mixdev, unsigned int cmd, caddr_t arg)
324 if (mixdev < 0 || mixdev >= MAX_MIXER_DEV)
325 return -ENXIO;
326 /* Try to load the mixer... */
327 if (mixer_devs[mixdev] == NULL) {
328 char modname[20];
329 sprintf(modname, "mixer%d", mixdev);
330 request_module(modname);
332 if (mixdev >= num_mixers || !mixer_devs[mixdev])
333 return -ENXIO;
334 if (cmd == SOUND_MIXER_INFO)
335 return get_mixer_info(mixdev, arg);
336 if (cmd == SOUND_OLD_MIXER_INFO)
337 return get_old_mixer_info(mixdev, arg);
338 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
339 mixer_devs[mixdev]->modify_counter++;
340 if (!mixer_devs[mixdev]->ioctl)
341 return -EINVAL;
342 return mixer_devs[mixdev]->ioctl(mixdev, cmd, arg);
345 static int sound_ioctl(struct inode *inode, struct file *file,
346 unsigned int cmd, unsigned long arg)
348 int err, len = 0, dtype;
349 int dev = MINOR(inode->i_rdev);
351 if (_SIOC_DIR(cmd) != _SIOC_NONE && _SIOC_DIR(cmd) != 0) {
353 * Have to validate the address given by the process.
355 len = _SIOC_SIZE(cmd);
356 if (len < 1 || len > 65536 || arg == 0)
357 return -EFAULT;
358 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
359 if ((err = verify_area(VERIFY_READ, (void *)arg, len)) < 0)
360 return err;
361 if (_SIOC_DIR(cmd) & _SIOC_READ)
362 if ((err = verify_area(VERIFY_WRITE, (void *)arg, len)) < 0)
363 return err;
365 DEB(printk("sound_ioctl(dev=%d, cmd=0x%x, arg=0x%x)\n", dev, cmd, arg));
366 if (cmd == OSS_GETVERSION)
367 return __put_user(SOUND_VERSION, (int *)arg);
369 if (_IOC_TYPE(cmd) == 'M' && num_mixers > 0 && /* Mixer ioctl */
370 (dev & 0x0f) != SND_DEV_CTL) {
371 dtype = dev & 0x0f;
372 switch (dtype) {
373 case SND_DEV_DSP:
374 case SND_DEV_DSP16:
375 case SND_DEV_AUDIO:
376 return sound_mixer_ioctl(audio_devs[dev >> 4]->mixer_dev,
377 cmd, (caddr_t)arg);
379 default:
380 return sound_mixer_ioctl(dev >> 4, cmd, (caddr_t)arg);
383 switch (dev & 0x0f) {
384 case SND_DEV_CTL:
385 if (cmd == SOUND_MIXER_GETLEVELS)
386 return get_mixer_levels((caddr_t)arg);
387 if (cmd == SOUND_MIXER_SETLEVELS)
388 return set_mixer_levels((caddr_t)arg);
389 return sound_mixer_ioctl(dev >> 4, cmd, (caddr_t)arg);
391 case SND_DEV_SEQ:
392 case SND_DEV_SEQ2:
393 return sequencer_ioctl(dev, file, cmd, (caddr_t)arg);
395 case SND_DEV_DSP:
396 case SND_DEV_DSP16:
397 case SND_DEV_AUDIO:
398 return audio_ioctl(dev, file, cmd, (caddr_t)arg);
399 break;
401 case SND_DEV_MIDIN:
402 return MIDIbuf_ioctl(dev, file, cmd, (caddr_t)arg);
403 break;
406 return -EINVAL;
409 static unsigned int sound_poll(struct file *file, poll_table * wait)
411 struct inode *inode = file->f_dentry->d_inode;
412 int dev = MINOR(inode->i_rdev);
414 DEB(printk("sound_poll(dev=%d)\n", dev));
415 switch (dev & 0x0f) {
416 case SND_DEV_SEQ:
417 case SND_DEV_SEQ2:
418 return sequencer_poll(dev, file, wait);
420 case SND_DEV_MIDIN:
421 return MIDIbuf_poll(dev, file, wait);
423 case SND_DEV_DSP:
424 case SND_DEV_DSP16:
425 case SND_DEV_AUDIO:
426 return DMAbuf_poll(file, dev >> 4, wait);
428 return 0;
431 static int sound_mmap(struct file *file, struct vm_area_struct *vma)
433 int dev_class;
434 unsigned long size;
435 struct dma_buffparms *dmap = NULL;
436 int dev = MINOR(file->f_dentry->d_inode->i_rdev);
438 dev_class = dev & 0x0f;
439 dev >>= 4;
441 if (dev_class != SND_DEV_DSP && dev_class != SND_DEV_DSP16 && dev_class != SND_DEV_AUDIO) {
442 printk(KERN_ERR "Sound: mmap() not supported for other than audio devices\n");
443 return -EINVAL;
445 lock_kernel();
446 if (vma->vm_flags & VM_WRITE) /* Map write and read/write to the output buf */
447 dmap = audio_devs[dev]->dmap_out;
448 else if (vma->vm_flags & VM_READ)
449 dmap = audio_devs[dev]->dmap_in;
450 else {
451 printk(KERN_ERR "Sound: Undefined mmap() access\n");
452 unlock_kernel();
453 return -EINVAL;
456 if (dmap == NULL) {
457 printk(KERN_ERR "Sound: mmap() error. dmap == NULL\n");
458 unlock_kernel();
459 return -EIO;
461 if (dmap->raw_buf == NULL) {
462 printk(KERN_ERR "Sound: mmap() called when raw_buf == NULL\n");
463 unlock_kernel();
464 return -EIO;
466 if (dmap->mapping_flags) {
467 printk(KERN_ERR "Sound: mmap() called twice for the same DMA buffer\n");
468 unlock_kernel();
469 return -EIO;
471 if (vma->vm_pgoff != 0) {
472 printk(KERN_ERR "Sound: mmap() offset must be 0.\n");
473 unlock_kernel();
474 return -EINVAL;
476 size = vma->vm_end - vma->vm_start;
478 if (size != dmap->bytes_in_use) {
479 printk(KERN_WARNING "Sound: mmap() size = %ld. Should be %d\n", size, dmap->bytes_in_use);
481 if (remap_page_range(vma->vm_start, virt_to_phys(dmap->raw_buf),
482 vma->vm_end - vma->vm_start,
483 vma->vm_page_prot)) {
484 unlock_kernel();
485 return -EAGAIN;
488 dmap->mapping_flags |= DMA_MAP_MAPPED;
490 if( audio_devs[dev]->d->mmap)
491 audio_devs[dev]->d->mmap(dev);
493 memset(dmap->raw_buf,
494 dmap->neutral_byte,
495 dmap->bytes_in_use);
496 unlock_kernel();
497 return 0;
500 struct file_operations oss_sound_fops = {
501 owner: THIS_MODULE,
502 llseek: sound_lseek,
503 read: sound_read,
504 write: sound_write,
505 poll: sound_poll,
506 ioctl: sound_ioctl,
507 mmap: sound_mmap,
508 open: sound_open,
509 release: sound_release,
513 * Create the required special subdevices
516 static int create_special_devices(void)
518 int seq1,seq2;
519 seq1=register_sound_special(&oss_sound_fops, 1);
520 if(seq1==-1)
521 goto bad;
522 seq2=register_sound_special(&oss_sound_fops, 8);
523 if(seq2!=-1)
524 return 0;
525 unregister_sound_special(1);
526 bad:
527 return -1;
531 /* These device names follow the official Linux device list,
532 * Documentation/devices.txt. Let us know if there are other
533 * common names we should support for compatibility.
534 * Only those devices not created by the generic code in sound_core.c are
535 * registered here.
537 static const struct {
538 unsigned short minor;
539 char *name;
540 umode_t mode;
541 int *num;
542 } dev_list[] = { /* list of minor devices */
543 /* seems to be some confusion here -- this device is not in the device list */
544 {SND_DEV_DSP16, "dspW", S_IWUGO | S_IRUSR | S_IRGRP,
545 &num_audiodevs},
546 {SND_DEV_AUDIO, "audio", S_IWUGO | S_IRUSR | S_IRGRP,
547 &num_audiodevs},
550 static char *
551 soundcard_make_name(char *buf, char *name, int idx) {
552 if (idx==0)
553 sprintf(buf, "sound/%s", name);
554 else
555 sprintf(buf, "sound/%s%d", name, idx);
556 return buf;
559 /* Register/unregister audio entries */
560 static void soundcard_register_devfs (int do_register)
562 char name_buf[32];
563 int i, j, num;
565 for (i = 0; i < sizeof (dev_list) / sizeof *dev_list; i++) {
566 num = (dev_list[i].num == NULL) ? 0 : *dev_list[i].num;
567 for (j = 0; j < num || j == 0; j++) {
568 soundcard_make_name (name_buf, dev_list[i].name, j);
569 if (do_register)
570 devfs_register (NULL, name_buf, DEVFS_FL_NONE,
571 SOUND_MAJOR, dev_list[i].minor+ (j* 0x10),
572 S_IFCHR | dev_list[i].mode,
573 &oss_sound_fops, NULL);
574 else {
575 devfs_handle_t de;
577 de = devfs_find_handle (NULL, name_buf, 0, 0,
578 DEVFS_SPECIAL_CHR, 0);
579 devfs_unregister (de);
586 static int dmabuf = 0;
587 static int dmabug = 0;
589 MODULE_PARM(dmabuf, "i");
590 MODULE_PARM(dmabug, "i");
592 static int __init oss_init(void)
594 int err;
596 /* drag in sound_syms.o */
598 extern char sound_syms_symbol;
599 sound_syms_symbol = 0;
602 #ifdef CONFIG_PCI
603 if(dmabug)
604 isa_dma_bridge_buggy = dmabug;
605 #endif
607 err = create_special_devices();
608 if (err) {
609 printk(KERN_ERR "sound: driver already loaded/included in kernel\n");
610 return err;
613 /* Protecting the innocent */
614 sound_dmap_flag = (dmabuf > 0 ? 1 : 0);
616 soundcard_register_devfs(1);
618 if (sound_nblocks >= 1024)
619 printk(KERN_ERR "Sound warning: Deallocation table was too small.\n");
621 return 0;
624 static void __exit oss_cleanup(void)
626 int i;
628 if (MOD_IN_USE)
629 return;
631 soundcard_register_devfs (0);
633 unregister_sound_special(1);
634 unregister_sound_special(8);
636 sound_stop_timer();
638 sequencer_unload();
640 for (i = 0; i < MAX_DMA_CHANNELS; i++)
641 if (dma_alloc_map[i] != DMA_MAP_UNAVAIL) {
642 printk(KERN_ERR "Sound: Hmm, DMA%d was left allocated - fixed\n", i);
643 sound_free_dma(i);
646 for (i = 0; i < sound_nblocks; i++)
647 vfree(sound_mem_blocks[i]);
651 module_init(oss_init);
652 module_exit(oss_cleanup);
655 int sound_alloc_dma(int chn, char *deviceID)
657 int err;
659 if ((err = request_dma(chn, deviceID)) != 0)
660 return err;
662 dma_alloc_map[chn] = DMA_MAP_FREE;
664 return 0;
667 int sound_open_dma(int chn, char *deviceID)
669 unsigned long flags;
671 if (!valid_dma(chn)) {
672 printk(KERN_ERR "sound_open_dma: Invalid DMA channel %d\n", chn);
673 return 1;
675 save_flags(flags);
676 cli();
678 if (dma_alloc_map[chn] != DMA_MAP_FREE) {
679 printk("sound_open_dma: DMA channel %d busy or not allocated (%d)\n", chn, dma_alloc_map[chn]);
680 restore_flags(flags);
681 return 1;
683 dma_alloc_map[chn] = DMA_MAP_BUSY;
684 restore_flags(flags);
685 return 0;
688 void sound_free_dma(int chn)
690 if (dma_alloc_map[chn] == DMA_MAP_UNAVAIL) {
691 /* printk( "sound_free_dma: Bad access to DMA channel %d\n", chn); */
692 return;
694 free_dma(chn);
695 dma_alloc_map[chn] = DMA_MAP_UNAVAIL;
698 void sound_close_dma(int chn)
700 unsigned long flags;
702 save_flags(flags);
703 cli();
705 if (dma_alloc_map[chn] != DMA_MAP_BUSY) {
706 printk(KERN_ERR "sound_close_dma: Bad access to DMA channel %d\n", chn);
707 restore_flags(flags);
708 return;
710 dma_alloc_map[chn] = DMA_MAP_FREE;
711 restore_flags(flags);
714 static void do_sequencer_timer(unsigned long dummy)
716 sequencer_timer(0);
720 static struct timer_list seq_timer =
721 {function: do_sequencer_timer};
723 void request_sound_timer(int count)
725 extern unsigned long seq_time;
727 if (count < 0) {
728 seq_timer.expires = (-count) + jiffies;
729 add_timer(&seq_timer);
730 return;
732 count += seq_time;
734 count -= jiffies;
736 if (count < 1)
737 count = 1;
739 seq_timer.expires = (count) + jiffies;
740 add_timer(&seq_timer);
743 void sound_stop_timer(void)
745 del_timer(&seq_timer);;
748 void conf_printf(char *name, struct address_info *hw_config)
750 #ifndef CONFIG_SOUND_TRACEINIT
751 return;
752 #else
753 printk("<%s> at 0x%03x", name, hw_config->io_base);
755 if (hw_config->irq)
756 printk(" irq %d", (hw_config->irq > 0) ? hw_config->irq : -hw_config->irq);
758 if (hw_config->dma != -1 || hw_config->dma2 != -1)
760 printk(" dma %d", hw_config->dma);
761 if (hw_config->dma2 != -1)
762 printk(",%d", hw_config->dma2);
764 printk("\n");
765 #endif
768 void conf_printf2(char *name, int base, int irq, int dma, int dma2)
770 #ifndef CONFIG_SOUND_TRACEINIT
771 return;
772 #else
773 printk("<%s> at 0x%03x", name, base);
775 if (irq)
776 printk(" irq %d", (irq > 0) ? irq : -irq);
778 if (dma != -1 || dma2 != -1)
780 printk(" dma %d", dma);
781 if (dma2 != -1)
782 printk(",%d", dma2);
784 printk("\n");
785 #endif