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
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>
38 #include <linux/wait.h>
39 #include <linux/slab.h>
40 #include <linux/ioport.h>
41 #include <linux/devfs_fs_kernel.h>
42 #include <linux/major.h>
43 #include <linux/delay.h>
44 #include <linux/proc_fs.h>
45 #include <linux/smp_lock.h>
46 #include <linux/module.h>
49 * This ought to be moved into include/asm/dma.h
52 #define valid_dma(n) ((n) >= 0 && (n) < MAX_DMA_CHANNELS && (n) != 4)
56 * Table for permanently allocated memory (used when unloading the module)
58 void * sound_mem_blocks
[1024];
59 int sound_nblocks
= 0;
61 /* Persistent DMA buffers */
62 #ifdef CONFIG_SOUND_DMAP
63 int sound_dmap_flag
= 1;
65 int sound_dmap_flag
= 0;
68 static char dma_alloc_map
[MAX_DMA_CHANNELS
];
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 */
76 extern struct class *sound_class
;
79 * Table for configurable mixer volume handling
81 static mixer_vol_table mixer_vols
[MAX_MIXER_DEV
];
82 static int num_mixer_volumes
;
84 int *load_mixer_volumes(char *name
, int *levels
, int present
)
88 for (i
= 0; i
< num_mixer_volumes
; i
++) {
89 if (strcmp(name
, mixer_vols
[i
].name
) == 0) {
91 mixer_vols
[i
].num
= i
;
92 return mixer_vols
[i
].levels
;
95 if (num_mixer_volumes
>= MAX_MIXER_DEV
) {
96 printk(KERN_ERR
"Sound: Too many mixers (%s)\n", name
);
99 n
= num_mixer_volumes
++;
101 strcpy(mixer_vols
[n
].name
, name
);
104 mixer_vols
[n
].num
= n
;
106 mixer_vols
[n
].num
= -1;
108 for (i
= 0; i
< 32; i
++)
109 mixer_vols
[n
].levels
[i
] = levels
[i
];
110 return mixer_vols
[n
].levels
;
113 static int set_mixer_levels(void __user
* arg
)
115 /* mixer_vol_table is 174 bytes, so IMHO no reason to not allocate it on the stack */
118 if (__copy_from_user(&buf
, arg
, sizeof(buf
)))
120 load_mixer_volumes(buf
.name
, buf
.levels
, 0);
121 if (__copy_to_user(arg
, &buf
, sizeof(buf
)))
126 static int get_mixer_levels(void __user
* arg
)
130 if (__get_user(n
, (int __user
*)(&(((mixer_vol_table __user
*)arg
)->num
))))
132 if (n
< 0 || n
>= num_mixer_volumes
)
134 if (__copy_to_user(arg
, &mixer_vols
[n
], sizeof(mixer_vol_table
)))
139 /* 4K page size but our output routines use some slack for overruns */
140 #define PROC_BLOCK_SIZE (3*1024)
142 static ssize_t
sound_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
144 int dev
= iminor(file
->f_dentry
->d_inode
);
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..
155 DEB(printk("sound_read(dev=%d, count=%d)\n", dev
, count
));
156 switch (dev
& 0x0f) {
160 ret
= audio_read(dev
, file
, buf
, count
);
165 ret
= sequencer_read(dev
, file
, buf
, count
);
169 ret
= MIDIbuf_read(dev
, file
, buf
, count
);
175 static ssize_t
sound_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
177 int dev
= iminor(file
->f_dentry
->d_inode
);
181 DEB(printk("sound_write(dev=%d, count=%d)\n", dev
, count
));
182 switch (dev
& 0x0f) {
185 ret
= sequencer_write(dev
, file
, buf
, count
);
191 ret
= audio_write(dev
, file
, buf
, count
);
195 ret
= MIDIbuf_write(dev
, file
, buf
, count
);
202 static int sound_open(struct inode
*inode
, struct file
*file
)
204 int dev
= iminor(inode
);
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
);
212 switch (dev
& 0x0f) {
215 if (dev
>= 0 && dev
< MAX_MIXER_DEV
&& mixer_devs
[dev
] == NULL
) {
216 request_module("mixer%d", dev
);
218 if (dev
&& (dev
>= num_mixers
|| mixer_devs
[dev
] == NULL
))
221 if (!try_module_get(mixer_devs
[dev
]->owner
))
227 if ((retval
= sequencer_open(dev
, file
)) < 0)
232 if ((retval
= MIDIbuf_open(dev
, file
)) < 0)
239 if ((retval
= audio_open(dev
, file
)) < 0)
244 printk(KERN_ERR
"Invalid minor device %d\n", dev
);
251 static int sound_release(struct inode
*inode
, struct file
*file
)
253 int dev
= iminor(inode
);
256 DEB(printk("sound_release(dev=%d)\n", dev
));
257 switch (dev
& 0x0f) {
259 module_put(mixer_devs
[dev
>> 4]->owner
);
264 sequencer_release(dev
, file
);
268 MIDIbuf_release(dev
, file
);
274 audio_release(dev
, file
);
278 printk(KERN_ERR
"Sound error: Releasing unknown device 0x%02x\n", dev
);
285 static int get_mixer_info(int dev
, void __user
*arg
)
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
)))
297 static int get_old_mixer_info(int dev
, void __user
*arg
)
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
)))
308 static int sound_mixer_ioctl(int mixdev
, unsigned int cmd
, void __user
*arg
)
310 if (mixdev
< 0 || mixdev
>= MAX_MIXER_DEV
)
312 /* Try to load the mixer... */
313 if (mixer_devs
[mixdev
] == NULL
) {
314 request_module("mixer%d", mixdev
);
316 if (mixdev
>= num_mixers
|| !mixer_devs
[mixdev
])
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
)
326 return mixer_devs
[mixdev
]->ioctl(mixdev
, cmd
, arg
);
329 static int sound_ioctl(struct inode
*inode
, struct file
*file
,
330 unsigned int cmd
, unsigned long arg
)
333 int dev
= iminor(inode
);
334 void __user
*p
= (void __user
*)arg
;
336 if (_SIOC_DIR(cmd
) != _SIOC_NONE
&& _SIOC_DIR(cmd
) != 0) {
338 * Have to validate the address given by the process.
340 len
= _SIOC_SIZE(cmd
);
341 if (len
< 1 || len
> 65536 || !p
)
343 if (_SIOC_DIR(cmd
) & _SIOC_WRITE
)
344 if (!access_ok(VERIFY_READ
, p
, len
))
346 if (_SIOC_DIR(cmd
) & _SIOC_READ
)
347 if (!access_ok(VERIFY_WRITE
, p
, len
))
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
);
354 if (_IOC_TYPE(cmd
) == 'M' && num_mixers
> 0 && /* Mixer ioctl */
355 (dev
& 0x0f) != SND_DEV_CTL
) {
361 return sound_mixer_ioctl(audio_devs
[dev
>> 4]->mixer_dev
,
365 return sound_mixer_ioctl(dev
>> 4, cmd
, p
);
368 switch (dev
& 0x0f) {
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
);
378 return sequencer_ioctl(dev
, file
, cmd
, p
);
383 return audio_ioctl(dev
, file
, cmd
, p
);
387 return MIDIbuf_ioctl(dev
, file
, cmd
, p
);
394 static unsigned int sound_poll(struct file
*file
, poll_table
* wait
)
396 struct inode
*inode
= file
->f_dentry
->d_inode
;
397 int dev
= iminor(inode
);
399 DEB(printk("sound_poll(dev=%d)\n", dev
));
400 switch (dev
& 0x0f) {
403 return sequencer_poll(dev
, file
, wait
);
406 return MIDIbuf_poll(dev
, file
, wait
);
411 return DMAbuf_poll(file
, dev
>> 4, wait
);
416 static int sound_mmap(struct file
*file
, struct vm_area_struct
*vma
)
420 struct dma_buffparms
*dmap
= NULL
;
421 int dev
= iminor(file
->f_dentry
->d_inode
);
423 dev_class
= dev
& 0x0f;
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");
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
;
436 printk(KERN_ERR
"Sound: Undefined mmap() access\n");
442 printk(KERN_ERR
"Sound: mmap() error. dmap == NULL\n");
446 if (dmap
->raw_buf
== NULL
) {
447 printk(KERN_ERR
"Sound: mmap() called when raw_buf == NULL\n");
451 if (dmap
->mapping_flags
) {
452 printk(KERN_ERR
"Sound: mmap() called twice for the same DMA buffer\n");
456 if (vma
->vm_pgoff
!= 0) {
457 printk(KERN_ERR
"Sound: mmap() offset must be 0.\n");
461 size
= vma
->vm_end
- vma
->vm_start
;
463 if (size
!= dmap
->bytes_in_use
) {
464 printk(KERN_WARNING
"Sound: mmap() size = %ld. Should be %d\n", size
, dmap
->bytes_in_use
);
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
)) {
473 dmap
->mapping_flags
|= DMA_MAP_MAPPED
;
475 if( audio_devs
[dev
]->d
->mmap
)
476 audio_devs
[dev
]->d
->mmap(dev
);
478 memset(dmap
->raw_buf
,
485 struct file_operations oss_sound_fops
= {
486 .owner
= THIS_MODULE
,
489 .write
= sound_write
,
491 .ioctl
= sound_ioctl
,
494 .release
= sound_release
,
498 * Create the required special subdevices
501 static int create_special_devices(void)
504 seq1
=register_sound_special(&oss_sound_fops
, 1);
507 seq2
=register_sound_special(&oss_sound_fops
, 8);
510 unregister_sound_special(1);
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
522 static const struct {
523 unsigned short minor
;
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
,
531 {SND_DEV_AUDIO
, "audio", S_IWUGO
| S_IRUSR
| S_IRGRP
,
538 module_param(dmabuf
, int, 0444);
539 module_param(dmabug
, int, 0444);
541 static int __init
oss_init(void)
546 /* drag in sound_syms.o */
548 extern char sound_syms_symbol
;
549 sound_syms_symbol
= 0;
554 isa_dma_bridge_buggy
= dmabug
;
557 err
= create_special_devices();
559 printk(KERN_ERR
"sound: driver already loaded/included in kernel\n");
563 /* Protecting the innocent */
564 sound_dmap_flag
= (dmabuf
> 0 ? 1 : 0);
566 for (i
= 0; i
< sizeof (dev_list
) / sizeof *dev_list
; i
++) {
567 devfs_mk_cdev(MKDEV(SOUND_MAJOR
, dev_list
[i
].minor
),
568 S_IFCHR
| dev_list
[i
].mode
,
569 "sound/%s", dev_list
[i
].name
);
570 class_device_create(sound_class
,
571 MKDEV(SOUND_MAJOR
, dev_list
[i
].minor
),
572 NULL
, "%s", dev_list
[i
].name
);
574 if (!dev_list
[i
].num
)
577 for (j
= 1; j
< *dev_list
[i
].num
; j
++) {
578 devfs_mk_cdev(MKDEV(SOUND_MAJOR
,
579 dev_list
[i
].minor
+ (j
*0x10)),
580 S_IFCHR
| dev_list
[i
].mode
,
581 "sound/%s%d", dev_list
[i
].name
, j
);
582 class_device_create(sound_class
,
583 MKDEV(SOUND_MAJOR
, dev_list
[i
].minor
+ (j
*0x10)),
584 NULL
, "%s%d", dev_list
[i
].name
, j
);
588 if (sound_nblocks
>= 1024)
589 printk(KERN_ERR
"Sound warning: Deallocation table was too small.\n");
594 static void __exit
oss_cleanup(void)
598 for (i
= 0; i
< sizeof (dev_list
) / sizeof *dev_list
; i
++) {
599 devfs_remove("sound/%s", dev_list
[i
].name
);
600 class_device_destroy(sound_class
, MKDEV(SOUND_MAJOR
, dev_list
[i
].minor
));
601 if (!dev_list
[i
].num
)
603 for (j
= 1; j
< *dev_list
[i
].num
; j
++) {
604 devfs_remove("sound/%s%d", dev_list
[i
].name
, j
);
605 class_device_destroy(sound_class
, MKDEV(SOUND_MAJOR
, dev_list
[i
].minor
+ (j
*0x10)));
609 unregister_sound_special(1);
610 unregister_sound_special(8);
616 for (i
= 0; i
< MAX_DMA_CHANNELS
; i
++)
617 if (dma_alloc_map
[i
] != DMA_MAP_UNAVAIL
) {
618 printk(KERN_ERR
"Sound: Hmm, DMA%d was left allocated - fixed\n", i
);
622 for (i
= 0; i
< sound_nblocks
; i
++)
623 vfree(sound_mem_blocks
[i
]);
627 module_init(oss_init
);
628 module_exit(oss_cleanup
);
629 MODULE_LICENSE("GPL");
632 int sound_alloc_dma(int chn
, char *deviceID
)
636 if ((err
= request_dma(chn
, deviceID
)) != 0)
639 dma_alloc_map
[chn
] = DMA_MAP_FREE
;
644 int sound_open_dma(int chn
, char *deviceID
)
646 if (!valid_dma(chn
)) {
647 printk(KERN_ERR
"sound_open_dma: Invalid DMA channel %d\n", chn
);
651 if (dma_alloc_map
[chn
] != DMA_MAP_FREE
) {
652 printk("sound_open_dma: DMA channel %d busy or not allocated (%d)\n", chn
, dma_alloc_map
[chn
]);
655 dma_alloc_map
[chn
] = DMA_MAP_BUSY
;
659 void sound_free_dma(int chn
)
661 if (dma_alloc_map
[chn
] == DMA_MAP_UNAVAIL
) {
662 /* printk( "sound_free_dma: Bad access to DMA channel %d\n", chn); */
666 dma_alloc_map
[chn
] = DMA_MAP_UNAVAIL
;
669 void sound_close_dma(int chn
)
671 if (dma_alloc_map
[chn
] != DMA_MAP_BUSY
) {
672 printk(KERN_ERR
"sound_close_dma: Bad access to DMA channel %d\n", chn
);
675 dma_alloc_map
[chn
] = DMA_MAP_FREE
;
678 static void do_sequencer_timer(unsigned long dummy
)
684 static DEFINE_TIMER(seq_timer
, do_sequencer_timer
, 0, 0);
686 void request_sound_timer(int count
)
688 extern unsigned long seq_time
;
691 seq_timer
.expires
= (-count
) + jiffies
;
692 add_timer(&seq_timer
);
702 seq_timer
.expires
= (count
) + jiffies
;
703 add_timer(&seq_timer
);
706 void sound_stop_timer(void)
708 del_timer(&seq_timer
);
711 void conf_printf(char *name
, struct address_info
*hw_config
)
713 #ifndef CONFIG_SOUND_TRACEINIT
716 printk("<%s> at 0x%03x", name
, hw_config
->io_base
);
719 printk(" irq %d", (hw_config
->irq
> 0) ? hw_config
->irq
: -hw_config
->irq
);
721 if (hw_config
->dma
!= -1 || hw_config
->dma2
!= -1)
723 printk(" dma %d", hw_config
->dma
);
724 if (hw_config
->dma2
!= -1)
725 printk(",%d", hw_config
->dma2
);
731 void conf_printf2(char *name
, int base
, int irq
, int dma
, int dma2
)
733 #ifndef CONFIG_SOUND_TRACEINIT
736 printk("<%s> at 0x%03x", name
, base
);
739 printk(" irq %d", (irq
> 0) ? irq
: -irq
);
741 if (dma
!= -1 || dma2
!= -1)
743 printk(" dma %d", dma
);