2 * linux/sound/oss/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)
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>
37 #include <linux/wait.h>
38 #include <linux/slab.h>
39 #include <linux/ioport.h>
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>
48 * This ought to be moved into include/asm/dma.h
51 #define valid_dma(n) ((n) >= 0 && (n) < MAX_DMA_CHANNELS && (n) != 4)
55 * Table for permanently allocated memory (used when unloading the module)
57 void * sound_mem_blocks
[1024];
58 int sound_nblocks
= 0;
60 /* Persistent DMA buffers */
61 #ifdef CONFIG_SOUND_DMAP
62 int sound_dmap_flag
= 1;
64 int sound_dmap_flag
= 0;
67 static char dma_alloc_map
[MAX_DMA_CHANNELS
];
69 #define DMA_MAP_UNAVAIL 0
70 #define DMA_MAP_FREE 1
71 #define DMA_MAP_BUSY 2
74 unsigned long seq_time
= 0; /* Time for /dev/sequencer */
75 extern struct class *sound_class
;
78 * Table for configurable mixer volume handling
80 static mixer_vol_table mixer_vols
[MAX_MIXER_DEV
];
81 static int num_mixer_volumes
;
83 int *load_mixer_volumes(char *name
, int *levels
, int present
)
87 for (i
= 0; i
< num_mixer_volumes
; i
++) {
88 if (strcmp(name
, mixer_vols
[i
].name
) == 0) {
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
);
98 n
= num_mixer_volumes
++;
100 strcpy(mixer_vols
[n
].name
, name
);
103 mixer_vols
[n
].num
= n
;
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
;
111 EXPORT_SYMBOL(load_mixer_volumes
);
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)
548 isa_dma_bridge_buggy
= dmabug
;
551 err
= create_special_devices();
553 printk(KERN_ERR
"sound: driver already loaded/included in kernel\n");
557 /* Protecting the innocent */
558 sound_dmap_flag
= (dmabuf
> 0 ? 1 : 0);
560 for (i
= 0; i
< sizeof (dev_list
) / sizeof *dev_list
; i
++) {
561 device_create(sound_class
, NULL
,
562 MKDEV(SOUND_MAJOR
, dev_list
[i
].minor
),
563 "%s", dev_list
[i
].name
);
565 if (!dev_list
[i
].num
)
568 for (j
= 1; j
< *dev_list
[i
].num
; j
++)
569 device_create(sound_class
, NULL
,
570 MKDEV(SOUND_MAJOR
, dev_list
[i
].minor
+ (j
*0x10)),
571 "%s%d", dev_list
[i
].name
, j
);
574 if (sound_nblocks
>= 1024)
575 printk(KERN_ERR
"Sound warning: Deallocation table was too small.\n");
580 static void __exit
oss_cleanup(void)
584 for (i
= 0; i
< sizeof (dev_list
) / sizeof *dev_list
; i
++) {
585 device_destroy(sound_class
, MKDEV(SOUND_MAJOR
, dev_list
[i
].minor
));
586 if (!dev_list
[i
].num
)
588 for (j
= 1; j
< *dev_list
[i
].num
; j
++)
589 device_destroy(sound_class
, MKDEV(SOUND_MAJOR
, dev_list
[i
].minor
+ (j
*0x10)));
592 unregister_sound_special(1);
593 unregister_sound_special(8);
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
);
605 for (i
= 0; i
< sound_nblocks
; i
++)
606 vfree(sound_mem_blocks
[i
]);
610 module_init(oss_init
);
611 module_exit(oss_cleanup
);
612 MODULE_LICENSE("GPL");
613 MODULE_DESCRIPTION("OSS Sound subsystem");
614 MODULE_AUTHOR("Hannu Savolainen, et al.");
617 int sound_alloc_dma(int chn
, char *deviceID
)
621 if ((err
= request_dma(chn
, deviceID
)) != 0)
624 dma_alloc_map
[chn
] = DMA_MAP_FREE
;
628 EXPORT_SYMBOL(sound_alloc_dma
);
630 int sound_open_dma(int chn
, char *deviceID
)
632 if (!valid_dma(chn
)) {
633 printk(KERN_ERR
"sound_open_dma: Invalid DMA channel %d\n", chn
);
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
]);
641 dma_alloc_map
[chn
] = DMA_MAP_BUSY
;
644 EXPORT_SYMBOL(sound_open_dma
);
646 void sound_free_dma(int chn
)
648 if (dma_alloc_map
[chn
] == DMA_MAP_UNAVAIL
) {
649 /* printk( "sound_free_dma: Bad access to DMA channel %d\n", chn); */
653 dma_alloc_map
[chn
] = DMA_MAP_UNAVAIL
;
655 EXPORT_SYMBOL(sound_free_dma
);
657 void sound_close_dma(int chn
)
659 if (dma_alloc_map
[chn
] != DMA_MAP_BUSY
) {
660 printk(KERN_ERR
"sound_close_dma: Bad access to DMA channel %d\n", chn
);
663 dma_alloc_map
[chn
] = DMA_MAP_FREE
;
665 EXPORT_SYMBOL(sound_close_dma
);
667 static void do_sequencer_timer(unsigned long dummy
)
673 static DEFINE_TIMER(seq_timer
, do_sequencer_timer
, 0, 0);
675 void request_sound_timer(int count
)
677 extern unsigned long seq_time
;
680 seq_timer
.expires
= (-count
) + jiffies
;
681 add_timer(&seq_timer
);
691 seq_timer
.expires
= (count
) + jiffies
;
692 add_timer(&seq_timer
);
695 void sound_stop_timer(void)
697 del_timer(&seq_timer
);
700 void conf_printf(char *name
, struct address_info
*hw_config
)
702 #ifndef CONFIG_SOUND_TRACEINIT
705 printk("<%s> at 0x%03x", name
, hw_config
->io_base
);
708 printk(" irq %d", (hw_config
->irq
> 0) ? hw_config
->irq
: -hw_config
->irq
);
710 if (hw_config
->dma
!= -1 || hw_config
->dma2
!= -1)
712 printk(" dma %d", hw_config
->dma
);
713 if (hw_config
->dma2
!= -1)
714 printk(",%d", hw_config
->dma2
);
719 EXPORT_SYMBOL(conf_printf
);
721 void conf_printf2(char *name
, int base
, int irq
, int dma
, int dma2
)
723 #ifndef CONFIG_SOUND_TRACEINIT
726 printk("<%s> at 0x%03x", name
, base
);
729 printk(" irq %d", (irq
> 0) ? irq
: -irq
);
731 if (dma
!= -1 || dma2
!= -1)
733 printk(" dma %d", dma
);
740 EXPORT_SYMBOL(conf_printf2
);