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/smp_lock.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 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
;
112 EXPORT_SYMBOL(load_mixer_volumes
);
114 static int set_mixer_levels(void __user
* arg
)
116 /* mixer_vol_table is 174 bytes, so IMHO no reason to not allocate it on the stack */
119 if (__copy_from_user(&buf
, arg
, sizeof(buf
)))
121 load_mixer_volumes(buf
.name
, buf
.levels
, 0);
122 if (__copy_to_user(arg
, &buf
, sizeof(buf
)))
127 static int get_mixer_levels(void __user
* arg
)
131 if (__get_user(n
, (int __user
*)(&(((mixer_vol_table __user
*)arg
)->num
))))
133 if (n
< 0 || n
>= num_mixer_volumes
)
135 if (__copy_to_user(arg
, &mixer_vols
[n
], sizeof(mixer_vol_table
)))
140 /* 4K page size but our output routines use some slack for overruns */
141 #define PROC_BLOCK_SIZE (3*1024)
143 static ssize_t
sound_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
145 int dev
= iminor(file
->f_path
.dentry
->d_inode
);
149 * The OSS drivers aren't remotely happy without this locking,
150 * and unless someone fixes them when they are about to bite the
151 * big one anyway, we might as well bandage here..
156 DEB(printk("sound_read(dev=%d, count=%d)\n", dev
, count
));
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
);
176 static ssize_t
sound_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
178 int dev
= iminor(file
->f_path
.dentry
->d_inode
);
182 DEB(printk("sound_write(dev=%d, count=%d)\n", dev
, count
));
183 switch (dev
& 0x0f) {
186 ret
= sequencer_write(dev
, file
, buf
, count
);
192 ret
= audio_write(dev
, file
, buf
, count
);
196 ret
= MIDIbuf_write(dev
, file
, buf
, count
);
203 static int sound_open(struct inode
*inode
, struct file
*file
)
205 int dev
= iminor(inode
);
208 DEB(printk("sound_open(dev=%d)\n", dev
));
209 if ((dev
>= SND_NDEVS
) || (dev
< 0)) {
210 printk(KERN_ERR
"Invalid minor device %d\n", dev
);
213 switch (dev
& 0x0f) {
216 if (dev
>= 0 && dev
< MAX_MIXER_DEV
&& mixer_devs
[dev
] == NULL
) {
217 request_module("mixer%d", dev
);
219 if (dev
&& (dev
>= num_mixers
|| mixer_devs
[dev
] == NULL
))
222 if (!try_module_get(mixer_devs
[dev
]->owner
))
228 if ((retval
= sequencer_open(dev
, file
)) < 0)
233 if ((retval
= MIDIbuf_open(dev
, file
)) < 0)
240 if ((retval
= audio_open(dev
, file
)) < 0)
245 printk(KERN_ERR
"Invalid minor device %d\n", dev
);
252 static int sound_release(struct inode
*inode
, struct file
*file
)
254 int dev
= iminor(inode
);
257 DEB(printk("sound_release(dev=%d)\n", dev
));
258 switch (dev
& 0x0f) {
260 module_put(mixer_devs
[dev
>> 4]->owner
);
265 sequencer_release(dev
, file
);
269 MIDIbuf_release(dev
, file
);
275 audio_release(dev
, file
);
279 printk(KERN_ERR
"Sound error: Releasing unknown device 0x%02x\n", dev
);
286 static int get_mixer_info(int dev
, void __user
*arg
)
289 memset(&info
, 0, sizeof(info
));
290 strlcpy(info
.id
, mixer_devs
[dev
]->id
, sizeof(info
.id
));
291 strlcpy(info
.name
, mixer_devs
[dev
]->name
, sizeof(info
.name
));
292 info
.modify_counter
= mixer_devs
[dev
]->modify_counter
;
293 if (__copy_to_user(arg
, &info
, sizeof(info
)))
298 static int get_old_mixer_info(int dev
, void __user
*arg
)
300 _old_mixer_info info
;
301 memset(&info
, 0, sizeof(info
));
302 strlcpy(info
.id
, mixer_devs
[dev
]->id
, sizeof(info
.id
));
303 strlcpy(info
.name
, mixer_devs
[dev
]->name
, sizeof(info
.name
));
304 if (copy_to_user(arg
, &info
, sizeof(info
)))
309 static int sound_mixer_ioctl(int mixdev
, unsigned int cmd
, void __user
*arg
)
311 if (mixdev
< 0 || mixdev
>= MAX_MIXER_DEV
)
313 /* Try to load the mixer... */
314 if (mixer_devs
[mixdev
] == NULL
) {
315 request_module("mixer%d", mixdev
);
317 if (mixdev
>= num_mixers
|| !mixer_devs
[mixdev
])
319 if (cmd
== SOUND_MIXER_INFO
)
320 return get_mixer_info(mixdev
, arg
);
321 if (cmd
== SOUND_OLD_MIXER_INFO
)
322 return get_old_mixer_info(mixdev
, arg
);
323 if (_SIOC_DIR(cmd
) & _SIOC_WRITE
)
324 mixer_devs
[mixdev
]->modify_counter
++;
325 if (!mixer_devs
[mixdev
]->ioctl
)
327 return mixer_devs
[mixdev
]->ioctl(mixdev
, cmd
, arg
);
330 static long sound_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
333 int dev
= iminor(file
->f_dentry
->d_inode
);
335 void __user
*p
= (void __user
*)arg
;
337 if (_SIOC_DIR(cmd
) != _SIOC_NONE
&& _SIOC_DIR(cmd
) != 0) {
339 * Have to validate the address given by the process.
341 len
= _SIOC_SIZE(cmd
);
342 if (len
< 1 || len
> 65536 || !p
)
344 if (_SIOC_DIR(cmd
) & _SIOC_WRITE
)
345 if (!access_ok(VERIFY_READ
, p
, len
))
347 if (_SIOC_DIR(cmd
) & _SIOC_READ
)
348 if (!access_ok(VERIFY_WRITE
, p
, len
))
351 DEB(printk("sound_ioctl(dev=%d, cmd=0x%x, arg=0x%x)\n", dev
, cmd
, arg
));
352 if (cmd
== OSS_GETVERSION
)
353 return __put_user(SOUND_VERSION
, (int __user
*)p
);
356 if (_IOC_TYPE(cmd
) == 'M' && num_mixers
> 0 && /* Mixer ioctl */
357 (dev
& 0x0f) != SND_DEV_CTL
) {
363 ret
= sound_mixer_ioctl(audio_devs
[dev
>> 4]->mixer_dev
,
367 ret
= sound_mixer_ioctl(dev
>> 4, cmd
, p
);
374 switch (dev
& 0x0f) {
376 if (cmd
== SOUND_MIXER_GETLEVELS
)
377 ret
= get_mixer_levels(p
);
378 else if (cmd
== SOUND_MIXER_SETLEVELS
)
379 ret
= set_mixer_levels(p
);
381 ret
= sound_mixer_ioctl(dev
>> 4, cmd
, p
);
386 ret
= sequencer_ioctl(dev
, file
, cmd
, p
);
392 return audio_ioctl(dev
, file
, cmd
, p
);
396 return MIDIbuf_ioctl(dev
, file
, cmd
, p
);
404 static unsigned int sound_poll(struct file
*file
, poll_table
* wait
)
406 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
407 int dev
= iminor(inode
);
409 DEB(printk("sound_poll(dev=%d)\n", dev
));
410 switch (dev
& 0x0f) {
413 return sequencer_poll(dev
, file
, wait
);
416 return MIDIbuf_poll(dev
, file
, wait
);
421 return DMAbuf_poll(file
, dev
>> 4, wait
);
426 static int sound_mmap(struct file
*file
, struct vm_area_struct
*vma
)
430 struct dma_buffparms
*dmap
= NULL
;
431 int dev
= iminor(file
->f_path
.dentry
->d_inode
);
433 dev_class
= dev
& 0x0f;
436 if (dev_class
!= SND_DEV_DSP
&& dev_class
!= SND_DEV_DSP16
&& dev_class
!= SND_DEV_AUDIO
) {
437 printk(KERN_ERR
"Sound: mmap() not supported for other than audio devices\n");
441 if (vma
->vm_flags
& VM_WRITE
) /* Map write and read/write to the output buf */
442 dmap
= audio_devs
[dev
]->dmap_out
;
443 else if (vma
->vm_flags
& VM_READ
)
444 dmap
= audio_devs
[dev
]->dmap_in
;
446 printk(KERN_ERR
"Sound: Undefined mmap() access\n");
452 printk(KERN_ERR
"Sound: mmap() error. dmap == NULL\n");
456 if (dmap
->raw_buf
== NULL
) {
457 printk(KERN_ERR
"Sound: mmap() called when raw_buf == NULL\n");
461 if (dmap
->mapping_flags
) {
462 printk(KERN_ERR
"Sound: mmap() called twice for the same DMA buffer\n");
466 if (vma
->vm_pgoff
!= 0) {
467 printk(KERN_ERR
"Sound: mmap() offset must be 0.\n");
471 size
= vma
->vm_end
- vma
->vm_start
;
473 if (size
!= dmap
->bytes_in_use
) {
474 printk(KERN_WARNING
"Sound: mmap() size = %ld. Should be %d\n", size
, dmap
->bytes_in_use
);
476 if (remap_pfn_range(vma
, vma
->vm_start
,
477 virt_to_phys(dmap
->raw_buf
) >> PAGE_SHIFT
,
478 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
)) {
483 dmap
->mapping_flags
|= DMA_MAP_MAPPED
;
485 if( audio_devs
[dev
]->d
->mmap
)
486 audio_devs
[dev
]->d
->mmap(dev
);
488 memset(dmap
->raw_buf
,
495 const struct file_operations oss_sound_fops
= {
496 .owner
= THIS_MODULE
,
499 .write
= sound_write
,
501 .unlocked_ioctl
= sound_ioctl
,
504 .release
= sound_release
,
508 * Create the required special subdevices
511 static int create_special_devices(void)
514 seq1
=register_sound_special(&oss_sound_fops
, 1);
517 seq2
=register_sound_special(&oss_sound_fops
, 8);
520 unregister_sound_special(1);
526 /* These device names follow the official Linux device list,
527 * Documentation/devices.txt. Let us know if there are other
528 * common names we should support for compatibility.
529 * Only those devices not created by the generic code in sound_core.c are
532 static const struct {
533 unsigned short minor
;
537 } dev_list
[] = { /* list of minor devices */
538 /* seems to be some confusion here -- this device is not in the device list */
539 {SND_DEV_DSP16
, "dspW", S_IWUGO
| S_IRUSR
| S_IRGRP
,
541 {SND_DEV_AUDIO
, "audio", S_IWUGO
| S_IRUSR
| S_IRGRP
,
548 module_param(dmabuf
, int, 0444);
549 module_param(dmabug
, int, 0444);
551 static int __init
oss_init(void)
558 isa_dma_bridge_buggy
= dmabug
;
561 err
= create_special_devices();
563 printk(KERN_ERR
"sound: driver already loaded/included in kernel\n");
567 /* Protecting the innocent */
568 sound_dmap_flag
= (dmabuf
> 0 ? 1 : 0);
570 for (i
= 0; i
< ARRAY_SIZE(dev_list
); i
++) {
571 device_create(sound_class
, NULL
,
572 MKDEV(SOUND_MAJOR
, dev_list
[i
].minor
), NULL
,
573 "%s", dev_list
[i
].name
);
575 if (!dev_list
[i
].num
)
578 for (j
= 1; j
< *dev_list
[i
].num
; j
++)
579 device_create(sound_class
, NULL
,
581 dev_list
[i
].minor
+ (j
*0x10)),
582 NULL
, "%s%d", dev_list
[i
].name
, j
);
585 if (sound_nblocks
>= MAX_MEM_BLOCKS
- 1)
586 printk(KERN_ERR
"Sound warning: Deallocation table was too small.\n");
591 static void __exit
oss_cleanup(void)
595 for (i
= 0; i
< ARRAY_SIZE(dev_list
); i
++) {
596 device_destroy(sound_class
, MKDEV(SOUND_MAJOR
, dev_list
[i
].minor
));
597 if (!dev_list
[i
].num
)
599 for (j
= 1; j
< *dev_list
[i
].num
; j
++)
600 device_destroy(sound_class
, MKDEV(SOUND_MAJOR
, dev_list
[i
].minor
+ (j
*0x10)));
603 unregister_sound_special(1);
604 unregister_sound_special(8);
610 for (i
= 0; i
< MAX_DMA_CHANNELS
; i
++)
611 if (dma_alloc_map
[i
] != DMA_MAP_UNAVAIL
) {
612 printk(KERN_ERR
"Sound: Hmm, DMA%d was left allocated - fixed\n", i
);
616 for (i
= 0; i
< sound_nblocks
; i
++)
617 vfree(sound_mem_blocks
[i
]);
621 module_init(oss_init
);
622 module_exit(oss_cleanup
);
623 MODULE_LICENSE("GPL");
624 MODULE_DESCRIPTION("OSS Sound subsystem");
625 MODULE_AUTHOR("Hannu Savolainen, et al.");
628 int sound_alloc_dma(int chn
, char *deviceID
)
632 if ((err
= request_dma(chn
, deviceID
)) != 0)
635 dma_alloc_map
[chn
] = DMA_MAP_FREE
;
639 EXPORT_SYMBOL(sound_alloc_dma
);
641 int sound_open_dma(int chn
, char *deviceID
)
643 if (!valid_dma(chn
)) {
644 printk(KERN_ERR
"sound_open_dma: Invalid DMA channel %d\n", chn
);
648 if (dma_alloc_map
[chn
] != DMA_MAP_FREE
) {
649 printk("sound_open_dma: DMA channel %d busy or not allocated (%d)\n", chn
, dma_alloc_map
[chn
]);
652 dma_alloc_map
[chn
] = DMA_MAP_BUSY
;
655 EXPORT_SYMBOL(sound_open_dma
);
657 void sound_free_dma(int chn
)
659 if (dma_alloc_map
[chn
] == DMA_MAP_UNAVAIL
) {
660 /* printk( "sound_free_dma: Bad access to DMA channel %d\n", chn); */
664 dma_alloc_map
[chn
] = DMA_MAP_UNAVAIL
;
666 EXPORT_SYMBOL(sound_free_dma
);
668 void sound_close_dma(int chn
)
670 if (dma_alloc_map
[chn
] != DMA_MAP_BUSY
) {
671 printk(KERN_ERR
"sound_close_dma: Bad access to DMA channel %d\n", chn
);
674 dma_alloc_map
[chn
] = DMA_MAP_FREE
;
676 EXPORT_SYMBOL(sound_close_dma
);
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
);
730 EXPORT_SYMBOL(conf_printf
);
732 void conf_printf2(char *name
, int base
, int irq
, int dma
, int dma2
)
734 #ifndef CONFIG_SOUND_TRACEINIT
737 printk("<%s> at 0x%03x", name
, base
);
740 printk(" irq %d", (irq
> 0) ? irq
: -irq
);
742 if (dma
!= -1 || dma2
!= -1)
744 printk(" dma %d", dma
);
751 EXPORT_SYMBOL(conf_printf2
);