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 <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
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 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;
65 int sound_dmap_flag
= 0;
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
)
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
;
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 */
117 if (__copy_from_user(&buf
, arg
, sizeof(buf
)))
119 load_mixer_volumes(buf
.name
, buf
.levels
, 0);
120 if (__copy_to_user(arg
, &buf
, sizeof(buf
)))
125 static int get_mixer_levels(caddr_t arg
)
129 if (__get_user(n
, (int *)(&(((mixer_vol_table
*)arg
)->num
))))
131 if (n
< 0 || n
>= num_mixer_volumes
)
133 if (__copy_to_user(arg
, &mixer_vols
[n
], sizeof(mixer_vol_table
)))
139 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
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
);
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..
158 DEB(printk("sound_read(dev=%d, count=%d)\n", dev
, count
));
159 switch (dev
& 0x0f) {
163 ret
= audio_read(dev
, file
, buf
, count
);
168 ret
= sequencer_read(dev
, file
, buf
, count
);
172 ret
= MIDIbuf_read(dev
, file
, buf
, count
);
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
);
184 DEB(printk("sound_write(dev=%d, count=%d)\n", dev
, count
));
185 switch (dev
& 0x0f) {
188 ret
= sequencer_write(dev
, file
, buf
, count
);
194 ret
= audio_write(dev
, file
, buf
, count
);
198 ret
= MIDIbuf_write(dev
, file
, buf
, count
);
205 static long long sound_lseek(struct file
*file
, long long offset
, int orig
)
210 static int sound_open(struct inode
*inode
, struct file
*file
)
212 int dev
= MINOR(inode
->i_rdev
);
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
);
220 switch (dev
& 0x0f) {
223 if (dev
>= 0 && dev
< MAX_MIXER_DEV
&& mixer_devs
[dev
] == NULL
) {
225 sprintf(modname
, "mixer%d", dev
);
226 request_module(modname
);
228 if (dev
&& (dev
>= num_mixers
|| mixer_devs
[dev
] == NULL
))
231 if (mixer_devs
[dev
]->owner
)
232 __MOD_INC_USE_COUNT (mixer_devs
[dev
]->owner
);
237 if ((retval
= sequencer_open(dev
, file
)) < 0)
242 if ((retval
= MIDIbuf_open(dev
, file
)) < 0)
249 if ((retval
= audio_open(dev
, file
)) < 0)
254 printk(KERN_ERR
"Invalid minor device %d\n", dev
);
261 static int sound_release(struct inode
*inode
, struct file
*file
)
263 int dev
= MINOR(inode
->i_rdev
);
266 DEB(printk("sound_release(dev=%d)\n", dev
));
267 switch (dev
& 0x0f) {
270 if (mixer_devs
[dev
]->owner
)
271 __MOD_DEC_USE_COUNT (mixer_devs
[dev
]->owner
);
276 sequencer_release(dev
, file
);
280 MIDIbuf_release(dev
, file
);
286 audio_release(dev
, file
);
290 printk(KERN_ERR
"Sound error: Releasing unknown device 0x%02x\n", dev
);
297 static int get_mixer_info(int dev
, caddr_t arg
)
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
)))
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
)))
322 static int sound_mixer_ioctl(int mixdev
, unsigned int cmd
, caddr_t arg
)
324 if (mixdev
< 0 || mixdev
>= MAX_MIXER_DEV
)
326 /* Try to load the mixer... */
327 if (mixer_devs
[mixdev
] == NULL
) {
329 sprintf(modname
, "mixer%d", mixdev
);
330 request_module(modname
);
332 if (mixdev
>= num_mixers
|| !mixer_devs
[mixdev
])
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
)
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)
358 if (_SIOC_DIR(cmd
) & _SIOC_WRITE
)
359 if ((err
= verify_area(VERIFY_READ
, (void *)arg
, len
)) < 0)
361 if (_SIOC_DIR(cmd
) & _SIOC_READ
)
362 if ((err
= verify_area(VERIFY_WRITE
, (void *)arg
, len
)) < 0)
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
) {
376 return sound_mixer_ioctl(audio_devs
[dev
>> 4]->mixer_dev
,
380 return sound_mixer_ioctl(dev
>> 4, cmd
, (caddr_t
)arg
);
383 switch (dev
& 0x0f) {
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
);
393 return sequencer_ioctl(dev
, file
, cmd
, (caddr_t
)arg
);
398 return audio_ioctl(dev
, file
, cmd
, (caddr_t
)arg
);
402 return MIDIbuf_ioctl(dev
, file
, cmd
, (caddr_t
)arg
);
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) {
418 return sequencer_poll(dev
, file
, wait
);
421 return MIDIbuf_poll(dev
, file
, wait
);
426 return DMAbuf_poll(file
, dev
>> 4, wait
);
431 static int sound_mmap(struct file
*file
, struct vm_area_struct
*vma
)
435 struct dma_buffparms
*dmap
= NULL
;
436 int dev
= MINOR(file
->f_dentry
->d_inode
->i_rdev
);
438 dev_class
= dev
& 0x0f;
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");
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
;
451 printk(KERN_ERR
"Sound: Undefined mmap() access\n");
457 printk(KERN_ERR
"Sound: mmap() error. dmap == NULL\n");
461 if (dmap
->raw_buf
== NULL
) {
462 printk(KERN_ERR
"Sound: mmap() called when raw_buf == NULL\n");
466 if (dmap
->mapping_flags
) {
467 printk(KERN_ERR
"Sound: mmap() called twice for the same DMA buffer\n");
471 if (vma
->vm_pgoff
!= 0) {
472 printk(KERN_ERR
"Sound: mmap() offset must be 0.\n");
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
)) {
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
,
500 struct file_operations oss_sound_fops
= {
509 release
: sound_release
,
513 * Create the required special subdevices
516 static int create_special_devices(void)
519 seq1
=register_sound_special(&oss_sound_fops
, 1);
522 seq2
=register_sound_special(&oss_sound_fops
, 8);
525 unregister_sound_special(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
537 static const struct {
538 unsigned short minor
;
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
,
546 {SND_DEV_AUDIO
, "audio", S_IWUGO
| S_IRUSR
| S_IRGRP
,
551 soundcard_make_name(char *buf
, char *name
, int idx
) {
553 sprintf(buf
, "sound/%s", name
);
555 sprintf(buf
, "sound/%s%d", name
, idx
);
559 /* Register/unregister audio entries */
560 static void soundcard_register_devfs (int do_register
)
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
);
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
);
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)
596 /* drag in sound_syms.o */
598 extern char sound_syms_symbol
;
599 sound_syms_symbol
= 0;
604 isa_dma_bridge_buggy
= dmabug
;
607 err
= create_special_devices();
609 printk(KERN_ERR
"sound: driver already loaded/included in kernel\n");
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");
624 static void __exit
oss_cleanup(void)
631 soundcard_register_devfs (0);
633 unregister_sound_special(1);
634 unregister_sound_special(8);
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
);
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
)
659 if ((err
= request_dma(chn
, deviceID
)) != 0)
662 dma_alloc_map
[chn
] = DMA_MAP_FREE
;
667 int sound_open_dma(int chn
, char *deviceID
)
671 if (!valid_dma(chn
)) {
672 printk(KERN_ERR
"sound_open_dma: Invalid DMA channel %d\n", chn
);
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
);
683 dma_alloc_map
[chn
] = DMA_MAP_BUSY
;
684 restore_flags(flags
);
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); */
695 dma_alloc_map
[chn
] = DMA_MAP_UNAVAIL
;
698 void sound_close_dma(int chn
)
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
);
710 dma_alloc_map
[chn
] = DMA_MAP_FREE
;
711 restore_flags(flags
);
714 static void do_sequencer_timer(unsigned long dummy
)
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
;
728 seq_timer
.expires
= (-count
) + jiffies
;
729 add_timer(&seq_timer
);
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
753 printk("<%s> at 0x%03x", name
, hw_config
->io_base
);
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
);
768 void conf_printf2(char *name
, int base
, int irq
, int dma
, int dma2
)
770 #ifndef CONFIG_SOUND_TRACEINIT
773 printk("<%s> at 0x%03x", name
, base
);
776 printk(" irq %d", (irq
> 0) ? irq
: -irq
);
778 if (dma
!= -1 || dma2
!= -1)
780 printk(" dma %d", dma
);