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>
35 #include <linux/kernel.h>
38 #include <linux/wait.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/mutex.h>
44 #include <linux/module.h>
46 #include <linux/device.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
[MAX_MEM_BLOCKS
];
59 static DEFINE_MUTEX(soundcard_mutex
);
60 int sound_nblocks
= 0;
62 /* Persistent DMA buffers */
63 #ifdef CONFIG_SOUND_DMAP
64 int sound_dmap_flag
= 1;
66 int sound_dmap_flag
= 0;
69 static char dma_alloc_map
[MAX_DMA_CHANNELS
];
71 #define DMA_MAP_UNAVAIL 0
72 #define DMA_MAP_FREE 1
73 #define DMA_MAP_BUSY 2
76 unsigned long seq_time
= 0; /* Time for /dev/sequencer */
77 extern struct class *sound_class
;
80 * Table for configurable mixer volume handling
82 static mixer_vol_table mixer_vols
[MAX_MIXER_DEV
];
83 static int num_mixer_volumes
;
85 int *load_mixer_volumes(char *name
, int *levels
, int present
)
89 for (i
= 0; i
< num_mixer_volumes
; i
++) {
90 if (strncmp(name
, mixer_vols
[i
].name
, 32) == 0) {
92 mixer_vols
[i
].num
= i
;
93 return mixer_vols
[i
].levels
;
96 if (num_mixer_volumes
>= MAX_MIXER_DEV
) {
97 printk(KERN_ERR
"Sound: Too many mixers (%s)\n", name
);
100 n
= num_mixer_volumes
++;
102 strncpy(mixer_vols
[n
].name
, name
, 32);
105 mixer_vols
[n
].num
= n
;
107 mixer_vols
[n
].num
= -1;
109 for (i
= 0; i
< 32; i
++)
110 mixer_vols
[n
].levels
[i
] = levels
[i
];
111 return mixer_vols
[n
].levels
;
113 EXPORT_SYMBOL(load_mixer_volumes
);
115 static int set_mixer_levels(void __user
* arg
)
117 /* mixer_vol_table is 174 bytes, so IMHO no reason to not allocate it on the stack */
120 if (__copy_from_user(&buf
, arg
, sizeof(buf
)))
122 load_mixer_volumes(buf
.name
, buf
.levels
, 0);
123 if (__copy_to_user(arg
, &buf
, sizeof(buf
)))
128 static int get_mixer_levels(void __user
* arg
)
132 if (__get_user(n
, (int __user
*)(&(((mixer_vol_table __user
*)arg
)->num
))))
134 if (n
< 0 || n
>= num_mixer_volumes
)
136 if (__copy_to_user(arg
, &mixer_vols
[n
], sizeof(mixer_vol_table
)))
141 /* 4K page size but our output routines use some slack for overruns */
142 #define PROC_BLOCK_SIZE (3*1024)
144 static ssize_t
sound_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
146 int dev
= iminor(file_inode(file
));
150 * The OSS drivers aren't remotely happy without this locking,
151 * and unless someone fixes them when they are about to bite the
152 * big one anyway, we might as well bandage here..
155 mutex_lock(&soundcard_mutex
);
157 switch (dev
& 0x0f) {
161 ret
= audio_read(dev
, file
, buf
, count
);
166 ret
= sequencer_read(dev
, file
, buf
, count
);
170 ret
= MIDIbuf_read(dev
, file
, buf
, count
);
172 mutex_unlock(&soundcard_mutex
);
176 static ssize_t
sound_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
178 int dev
= iminor(file_inode(file
));
181 mutex_lock(&soundcard_mutex
);
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
);
198 mutex_unlock(&soundcard_mutex
);
202 static int sound_open(struct inode
*inode
, struct file
*file
)
204 int dev
= iminor(inode
);
207 if ((dev
>= SND_NDEVS
) || (dev
< 0)) {
208 printk(KERN_ERR
"Invalid minor device %d\n", dev
);
211 mutex_lock(&soundcard_mutex
);
212 switch (dev
& 0x0f) {
215 if (dev
>= 0 && dev
< MAX_MIXER_DEV
&& mixer_devs
[dev
] == NULL
) {
216 request_module("mixer%d", dev
);
219 if (dev
&& (dev
>= num_mixers
|| mixer_devs
[dev
] == NULL
))
222 if (!try_module_get(mixer_devs
[dev
]->owner
))
230 retval
= sequencer_open(dev
, file
);
234 retval
= MIDIbuf_open(dev
, file
);
240 retval
= audio_open(dev
, file
);
244 printk(KERN_ERR
"Invalid minor device %d\n", dev
);
248 mutex_unlock(&soundcard_mutex
);
252 static int sound_release(struct inode
*inode
, struct file
*file
)
254 int dev
= iminor(inode
);
256 mutex_lock(&soundcard_mutex
);
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
);
280 mutex_unlock(&soundcard_mutex
);
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 long sound_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
332 int dev
= iminor(file_inode(file
));
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 if (cmd
== OSS_GETVERSION
)
351 return __put_user(SOUND_VERSION
, (int __user
*)p
);
353 mutex_lock(&soundcard_mutex
);
354 if (_IOC_TYPE(cmd
) == 'M' && num_mixers
> 0 && /* Mixer ioctl */
355 (dev
& 0x0f) != SND_DEV_CTL
) {
361 ret
= sound_mixer_ioctl(audio_devs
[dev
>> 4]->mixer_dev
,
365 ret
= sound_mixer_ioctl(dev
>> 4, cmd
, p
);
368 mutex_unlock(&soundcard_mutex
);
372 switch (dev
& 0x0f) {
374 if (cmd
== SOUND_MIXER_GETLEVELS
)
375 ret
= get_mixer_levels(p
);
376 else if (cmd
== SOUND_MIXER_SETLEVELS
)
377 ret
= set_mixer_levels(p
);
379 ret
= sound_mixer_ioctl(dev
>> 4, cmd
, p
);
384 ret
= sequencer_ioctl(dev
, file
, cmd
, p
);
390 ret
= audio_ioctl(dev
, file
, cmd
, p
);
394 ret
= MIDIbuf_ioctl(dev
, file
, cmd
, p
);
398 mutex_unlock(&soundcard_mutex
);
402 static unsigned int sound_poll(struct file
*file
, poll_table
* wait
)
404 struct inode
*inode
= file_inode(file
);
405 int dev
= iminor(inode
);
407 switch (dev
& 0x0f) {
410 return sequencer_poll(dev
, file
, wait
);
413 return MIDIbuf_poll(dev
, file
, wait
);
418 return DMAbuf_poll(file
, dev
>> 4, wait
);
423 static int sound_mmap(struct file
*file
, struct vm_area_struct
*vma
)
427 struct dma_buffparms
*dmap
= NULL
;
428 int dev
= iminor(file_inode(file
));
430 dev_class
= dev
& 0x0f;
433 if (dev_class
!= SND_DEV_DSP
&& dev_class
!= SND_DEV_DSP16
&& dev_class
!= SND_DEV_AUDIO
) {
434 printk(KERN_ERR
"Sound: mmap() not supported for other than audio devices\n");
437 mutex_lock(&soundcard_mutex
);
438 if (vma
->vm_flags
& VM_WRITE
) /* Map write and read/write to the output buf */
439 dmap
= audio_devs
[dev
]->dmap_out
;
440 else if (vma
->vm_flags
& VM_READ
)
441 dmap
= audio_devs
[dev
]->dmap_in
;
443 printk(KERN_ERR
"Sound: Undefined mmap() access\n");
444 mutex_unlock(&soundcard_mutex
);
449 printk(KERN_ERR
"Sound: mmap() error. dmap == NULL\n");
450 mutex_unlock(&soundcard_mutex
);
453 if (dmap
->raw_buf
== NULL
) {
454 printk(KERN_ERR
"Sound: mmap() called when raw_buf == NULL\n");
455 mutex_unlock(&soundcard_mutex
);
458 if (dmap
->mapping_flags
) {
459 printk(KERN_ERR
"Sound: mmap() called twice for the same DMA buffer\n");
460 mutex_unlock(&soundcard_mutex
);
463 if (vma
->vm_pgoff
!= 0) {
464 printk(KERN_ERR
"Sound: mmap() offset must be 0.\n");
465 mutex_unlock(&soundcard_mutex
);
468 size
= vma
->vm_end
- vma
->vm_start
;
470 if (size
!= dmap
->bytes_in_use
) {
471 printk(KERN_WARNING
"Sound: mmap() size = %ld. Should be %d\n", size
, dmap
->bytes_in_use
);
473 if (remap_pfn_range(vma
, vma
->vm_start
,
474 virt_to_phys(dmap
->raw_buf
) >> PAGE_SHIFT
,
475 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
)) {
476 mutex_unlock(&soundcard_mutex
);
480 dmap
->mapping_flags
|= DMA_MAP_MAPPED
;
482 if( audio_devs
[dev
]->d
->mmap
)
483 audio_devs
[dev
]->d
->mmap(dev
);
485 memset(dmap
->raw_buf
,
488 mutex_unlock(&soundcard_mutex
);
492 const struct file_operations oss_sound_fops
= {
493 .owner
= THIS_MODULE
,
496 .write
= sound_write
,
498 .unlocked_ioctl
= sound_ioctl
,
501 .release
= sound_release
,
505 * Create the required special subdevices
508 static int create_special_devices(void)
511 seq1
=register_sound_special(&oss_sound_fops
, 1);
514 seq2
=register_sound_special(&oss_sound_fops
, 8);
517 unregister_sound_special(1);
526 module_param(dmabuf
, int, 0444);
527 module_param(dmabug
, int, 0444);
529 /* additional minors for compatibility */
530 struct oss_minor_dev
{
531 unsigned short minor
;
532 unsigned int enabled
;
538 static int __init
oss_init(void)
545 isa_dma_bridge_buggy
= dmabug
;
548 err
= create_special_devices();
550 printk(KERN_ERR
"sound: driver already loaded/included in kernel\n");
554 /* Protecting the innocent */
555 sound_dmap_flag
= (dmabuf
> 0 ? 1 : 0);
557 for (i
= 0; i
< ARRAY_SIZE(dev_list
); i
++) {
560 unsigned short minor
= dev_list
[i
].minor
+ j
* 0x10;
561 if (!register_sound_special(&oss_sound_fops
, minor
))
562 dev_list
[i
].enabled
= (1 << j
);
563 } while (++j
< num_audiodevs
);
566 if (sound_nblocks
>= MAX_MEM_BLOCKS
- 1)
567 printk(KERN_ERR
"Sound warning: Deallocation table was too small.\n");
572 static void __exit
oss_cleanup(void)
576 for (i
= 0; i
< ARRAY_SIZE(dev_list
); i
++) {
579 if (dev_list
[i
].enabled
& (1 << j
))
580 unregister_sound_special(dev_list
[i
].minor
);
581 } while (++j
< num_audiodevs
);
584 unregister_sound_special(1);
585 unregister_sound_special(8);
591 for (i
= 0; i
< MAX_DMA_CHANNELS
; i
++)
592 if (dma_alloc_map
[i
] != DMA_MAP_UNAVAIL
) {
593 printk(KERN_ERR
"Sound: Hmm, DMA%d was left allocated - fixed\n", i
);
597 for (i
= 0; i
< sound_nblocks
; i
++)
598 vfree(sound_mem_blocks
[i
]);
602 module_init(oss_init
);
603 module_exit(oss_cleanup
);
604 MODULE_LICENSE("GPL");
605 MODULE_DESCRIPTION("OSS Sound subsystem");
606 MODULE_AUTHOR("Hannu Savolainen, et al.");
609 int sound_alloc_dma(int chn
, char *deviceID
)
613 if ((err
= request_dma(chn
, deviceID
)) != 0)
616 dma_alloc_map
[chn
] = DMA_MAP_FREE
;
620 EXPORT_SYMBOL(sound_alloc_dma
);
622 int sound_open_dma(int chn
, char *deviceID
)
624 if (!valid_dma(chn
)) {
625 printk(KERN_ERR
"sound_open_dma: Invalid DMA channel %d\n", chn
);
629 if (dma_alloc_map
[chn
] != DMA_MAP_FREE
) {
630 printk("sound_open_dma: DMA channel %d busy or not allocated (%d)\n", chn
, dma_alloc_map
[chn
]);
633 dma_alloc_map
[chn
] = DMA_MAP_BUSY
;
636 EXPORT_SYMBOL(sound_open_dma
);
638 void sound_free_dma(int chn
)
640 if (dma_alloc_map
[chn
] == DMA_MAP_UNAVAIL
) {
641 /* printk( "sound_free_dma: Bad access to DMA channel %d\n", chn); */
645 dma_alloc_map
[chn
] = DMA_MAP_UNAVAIL
;
647 EXPORT_SYMBOL(sound_free_dma
);
649 void sound_close_dma(int chn
)
651 if (dma_alloc_map
[chn
] != DMA_MAP_BUSY
) {
652 printk(KERN_ERR
"sound_close_dma: Bad access to DMA channel %d\n", chn
);
655 dma_alloc_map
[chn
] = DMA_MAP_FREE
;
657 EXPORT_SYMBOL(sound_close_dma
);
659 static void do_sequencer_timer(unsigned long dummy
)
665 static DEFINE_TIMER(seq_timer
, do_sequencer_timer
, 0, 0);
667 void request_sound_timer(int count
)
669 extern unsigned long seq_time
;
672 seq_timer
.expires
= (-count
) + jiffies
;
673 add_timer(&seq_timer
);
683 seq_timer
.expires
= (count
) + jiffies
;
684 add_timer(&seq_timer
);
687 void sound_stop_timer(void)
689 del_timer(&seq_timer
);
692 void conf_printf(char *name
, struct address_info
*hw_config
)
694 #ifndef CONFIG_SOUND_TRACEINIT
697 printk("<%s> at 0x%03x", name
, hw_config
->io_base
);
700 printk(" irq %d", (hw_config
->irq
> 0) ? hw_config
->irq
: -hw_config
->irq
);
702 if (hw_config
->dma
!= -1 || hw_config
->dma2
!= -1)
704 printk(" dma %d", hw_config
->dma
);
705 if (hw_config
->dma2
!= -1)
706 printk(",%d", hw_config
->dma2
);
711 EXPORT_SYMBOL(conf_printf
);
713 void conf_printf2(char *name
, int base
, int irq
, int dma
, int dma2
)
715 #ifndef CONFIG_SOUND_TRACEINIT
718 printk("<%s> at 0x%03x", name
, base
);
721 printk(" irq %d", (irq
> 0) ? irq
: -irq
);
723 if (dma
!= -1 || dma2
!= -1)
725 printk(" dma %d", dma
);
732 EXPORT_SYMBOL(conf_printf2
);