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 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 int sound_open(struct inode
*inode
, struct file
*file
)
207 int dev
= minor(inode
->i_rdev
);
210 DEB(printk("sound_open(dev=%d)\n", dev
));
211 if ((dev
>= SND_NDEVS
) || (dev
< 0)) {
212 printk(KERN_ERR
"Invalid minor device %d\n", dev
);
215 switch (dev
& 0x0f) {
218 if (dev
>= 0 && dev
< MAX_MIXER_DEV
&& mixer_devs
[dev
] == NULL
) {
220 sprintf(modname
, "mixer%d", dev
);
221 request_module(modname
);
223 if (dev
&& (dev
>= num_mixers
|| mixer_devs
[dev
] == NULL
))
226 if (mixer_devs
[dev
]->owner
)
227 __MOD_INC_USE_COUNT (mixer_devs
[dev
]->owner
);
232 if ((retval
= sequencer_open(dev
, file
)) < 0)
237 if ((retval
= MIDIbuf_open(dev
, file
)) < 0)
244 if ((retval
= audio_open(dev
, file
)) < 0)
249 printk(KERN_ERR
"Invalid minor device %d\n", dev
);
256 static int sound_release(struct inode
*inode
, struct file
*file
)
258 int dev
= minor(inode
->i_rdev
);
261 DEB(printk("sound_release(dev=%d)\n", dev
));
262 switch (dev
& 0x0f) {
265 if (mixer_devs
[dev
]->owner
)
266 __MOD_DEC_USE_COUNT (mixer_devs
[dev
]->owner
);
271 sequencer_release(dev
, file
);
275 MIDIbuf_release(dev
, file
);
281 audio_release(dev
, file
);
285 printk(KERN_ERR
"Sound error: Releasing unknown device 0x%02x\n", dev
);
292 static int get_mixer_info(int dev
, caddr_t arg
)
296 strncpy(info
.id
, mixer_devs
[dev
]->id
, sizeof(info
.id
));
297 strncpy(info
.name
, mixer_devs
[dev
]->name
, sizeof(info
.name
));
298 info
.name
[sizeof(info
.name
)-1] = 0;
299 info
.modify_counter
= mixer_devs
[dev
]->modify_counter
;
300 if (__copy_to_user(arg
, &info
, sizeof(info
)))
305 static int get_old_mixer_info(int dev
, caddr_t arg
)
307 _old_mixer_info info
;
309 strncpy(info
.id
, mixer_devs
[dev
]->id
, sizeof(info
.id
));
310 strncpy(info
.name
, mixer_devs
[dev
]->name
, sizeof(info
.name
));
311 info
.name
[sizeof(info
.name
)-1] = 0;
312 if (copy_to_user(arg
, &info
, sizeof(info
)))
317 static int sound_mixer_ioctl(int mixdev
, unsigned int cmd
, caddr_t arg
)
319 if (mixdev
< 0 || mixdev
>= MAX_MIXER_DEV
)
321 /* Try to load the mixer... */
322 if (mixer_devs
[mixdev
] == NULL
) {
324 sprintf(modname
, "mixer%d", mixdev
);
325 request_module(modname
);
327 if (mixdev
>= num_mixers
|| !mixer_devs
[mixdev
])
329 if (cmd
== SOUND_MIXER_INFO
)
330 return get_mixer_info(mixdev
, arg
);
331 if (cmd
== SOUND_OLD_MIXER_INFO
)
332 return get_old_mixer_info(mixdev
, arg
);
333 if (_SIOC_DIR(cmd
) & _SIOC_WRITE
)
334 mixer_devs
[mixdev
]->modify_counter
++;
335 if (!mixer_devs
[mixdev
]->ioctl
)
337 return mixer_devs
[mixdev
]->ioctl(mixdev
, cmd
, arg
);
340 static int sound_ioctl(struct inode
*inode
, struct file
*file
,
341 unsigned int cmd
, unsigned long arg
)
343 int err
, len
= 0, dtype
;
344 int dev
= minor(inode
->i_rdev
);
346 if (_SIOC_DIR(cmd
) != _SIOC_NONE
&& _SIOC_DIR(cmd
) != 0) {
348 * Have to validate the address given by the process.
350 len
= _SIOC_SIZE(cmd
);
351 if (len
< 1 || len
> 65536 || arg
== 0)
353 if (_SIOC_DIR(cmd
) & _SIOC_WRITE
)
354 if ((err
= verify_area(VERIFY_READ
, (void *)arg
, len
)) < 0)
356 if (_SIOC_DIR(cmd
) & _SIOC_READ
)
357 if ((err
= verify_area(VERIFY_WRITE
, (void *)arg
, len
)) < 0)
360 DEB(printk("sound_ioctl(dev=%d, cmd=0x%x, arg=0x%x)\n", dev
, cmd
, arg
));
361 if (cmd
== OSS_GETVERSION
)
362 return __put_user(SOUND_VERSION
, (int *)arg
);
364 if (_IOC_TYPE(cmd
) == 'M' && num_mixers
> 0 && /* Mixer ioctl */
365 (dev
& 0x0f) != SND_DEV_CTL
) {
371 return sound_mixer_ioctl(audio_devs
[dev
>> 4]->mixer_dev
,
375 return sound_mixer_ioctl(dev
>> 4, cmd
, (caddr_t
)arg
);
378 switch (dev
& 0x0f) {
380 if (cmd
== SOUND_MIXER_GETLEVELS
)
381 return get_mixer_levels((caddr_t
)arg
);
382 if (cmd
== SOUND_MIXER_SETLEVELS
)
383 return set_mixer_levels((caddr_t
)arg
);
384 return sound_mixer_ioctl(dev
>> 4, cmd
, (caddr_t
)arg
);
388 return sequencer_ioctl(dev
, file
, cmd
, (caddr_t
)arg
);
393 return audio_ioctl(dev
, file
, cmd
, (caddr_t
)arg
);
397 return MIDIbuf_ioctl(dev
, file
, cmd
, (caddr_t
)arg
);
404 static unsigned int sound_poll(struct file
*file
, poll_table
* wait
)
406 struct inode
*inode
= file
->f_dentry
->d_inode
;
407 int dev
= minor(inode
->i_rdev
);
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
= minor(file
->f_dentry
->d_inode
->i_rdev
);
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_page_range(vma
, vma
->vm_start
, virt_to_phys(dmap
->raw_buf
),
477 vma
->vm_end
- vma
->vm_start
,
478 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 struct file_operations oss_sound_fops
= {
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
,
545 static int dmabuf
= 0;
546 static int dmabug
= 0;
548 MODULE_PARM(dmabuf
, "i");
549 MODULE_PARM(dmabug
, "i");
551 static int __init
oss_init(void)
557 /* drag in sound_syms.o */
559 extern char sound_syms_symbol
;
560 sound_syms_symbol
= 0;
565 isa_dma_bridge_buggy
= dmabug
;
568 err
= create_special_devices();
570 printk(KERN_ERR
"sound: driver already loaded/included in kernel\n");
574 /* Protecting the innocent */
575 sound_dmap_flag
= (dmabuf
> 0 ? 1 : 0);
577 for (i
= 0; i
< sizeof (dev_list
) / sizeof *dev_list
; i
++) {
578 sprintf(name_buf
, "sound/%s", dev_list
[i
].name
);
579 devfs_register (NULL
, name_buf
, DEVFS_FL_NONE
,
580 SOUND_MAJOR
, dev_list
[i
].minor
,
581 S_IFCHR
| dev_list
[i
].mode
,
582 &oss_sound_fops
, NULL
);
583 if (!dev_list
[i
].num
)
585 for (j
= 1; j
< *dev_list
[i
].num
; j
++) {
586 sprintf(name_buf
, "sound/%s%d", dev_list
[i
].name
, j
);
587 devfs_register (NULL
, name_buf
, DEVFS_FL_NONE
,
588 SOUND_MAJOR
, dev_list
[i
].minor
+ (j
* 0x10),
589 S_IFCHR
| dev_list
[i
].mode
,
590 &oss_sound_fops
, NULL
);
594 if (sound_nblocks
>= 1024)
595 printk(KERN_ERR
"Sound warning: Deallocation table was too small.\n");
600 static void __exit
oss_cleanup(void)
607 for (i
= 0; i
< sizeof (dev_list
) / sizeof *dev_list
; i
++) {
608 devfs_remove("snd/%s", dev_list
[i
].name
);
609 if (!dev_list
[i
].num
)
611 for (j
= 1; j
< *dev_list
[i
].num
; j
++)
612 devfs_remove("sound/%s%d", dev_list
[i
].name
, j
);
615 unregister_sound_special(1);
616 unregister_sound_special(8);
622 for (i
= 0; i
< MAX_DMA_CHANNELS
; i
++)
623 if (dma_alloc_map
[i
] != DMA_MAP_UNAVAIL
) {
624 printk(KERN_ERR
"Sound: Hmm, DMA%d was left allocated - fixed\n", i
);
628 for (i
= 0; i
< sound_nblocks
; i
++)
629 vfree(sound_mem_blocks
[i
]);
633 module_init(oss_init
);
634 module_exit(oss_cleanup
);
635 MODULE_LICENSE("GPL");
638 int sound_alloc_dma(int chn
, char *deviceID
)
642 if ((err
= request_dma(chn
, deviceID
)) != 0)
645 dma_alloc_map
[chn
] = DMA_MAP_FREE
;
650 int sound_open_dma(int chn
, char *deviceID
)
652 if (!valid_dma(chn
)) {
653 printk(KERN_ERR
"sound_open_dma: Invalid DMA channel %d\n", chn
);
657 if (dma_alloc_map
[chn
] != DMA_MAP_FREE
) {
658 printk("sound_open_dma: DMA channel %d busy or not allocated (%d)\n", chn
, dma_alloc_map
[chn
]);
661 dma_alloc_map
[chn
] = DMA_MAP_BUSY
;
665 void sound_free_dma(int chn
)
667 if (dma_alloc_map
[chn
] == DMA_MAP_UNAVAIL
) {
668 /* printk( "sound_free_dma: Bad access to DMA channel %d\n", chn); */
672 dma_alloc_map
[chn
] = DMA_MAP_UNAVAIL
;
675 void sound_close_dma(int chn
)
677 if (dma_alloc_map
[chn
] != DMA_MAP_BUSY
) {
678 printk(KERN_ERR
"sound_close_dma: Bad access to DMA channel %d\n", chn
);
681 dma_alloc_map
[chn
] = DMA_MAP_FREE
;
684 static void do_sequencer_timer(unsigned long dummy
)
690 static struct timer_list seq_timer
=
691 TIMER_INITIALIZER(do_sequencer_timer
, 0, 0);
693 void request_sound_timer(int count
)
695 extern unsigned long seq_time
;
698 seq_timer
.expires
= (-count
) + jiffies
;
699 add_timer(&seq_timer
);
709 seq_timer
.expires
= (count
) + jiffies
;
710 add_timer(&seq_timer
);
713 void sound_stop_timer(void)
715 del_timer(&seq_timer
);;
718 void conf_printf(char *name
, struct address_info
*hw_config
)
720 #ifndef CONFIG_SOUND_TRACEINIT
723 printk("<%s> at 0x%03x", name
, hw_config
->io_base
);
726 printk(" irq %d", (hw_config
->irq
> 0) ? hw_config
->irq
: -hw_config
->irq
);
728 if (hw_config
->dma
!= -1 || hw_config
->dma2
!= -1)
730 printk(" dma %d", hw_config
->dma
);
731 if (hw_config
->dma2
!= -1)
732 printk(",%d", hw_config
->dma2
);
738 void conf_printf2(char *name
, int base
, int irq
, int dma
, int dma2
)
740 #ifndef CONFIG_SOUND_TRACEINIT
743 printk("<%s> at 0x%03x", name
, base
);
746 printk(" irq %d", (irq
> 0) ? irq
: -irq
);
748 if (dma
!= -1 || dma2
!= -1)
750 printk(" dma %d", dma
);