2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2002 Steve Schmidtke
|
|
|
|
* Licensed under the GPL
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "linux/module.h"
|
|
|
|
#include "linux/init.h"
|
|
|
|
#include "linux/slab.h"
|
|
|
|
#include "linux/fs.h"
|
|
|
|
#include "linux/sound.h"
|
|
|
|
#include "linux/soundcard.h"
|
|
|
|
#include "asm/uaccess.h"
|
|
|
|
#include "kern_util.h"
|
|
|
|
#include "init.h"
|
|
|
|
#include "os.h"
|
|
|
|
|
|
|
|
struct hostaudio_state {
|
2007-02-10 17:44:00 +08:00
|
|
|
int fd;
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct hostmixer_state {
|
2007-02-10 17:44:00 +08:00
|
|
|
int fd;
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define HOSTAUDIO_DEV_DSP "/dev/sound/dsp"
|
|
|
|
#define HOSTAUDIO_DEV_MIXER "/dev/sound/mixer"
|
|
|
|
|
2007-02-10 17:43:59 +08:00
|
|
|
/* Changed either at boot time or module load time. At boot, this is
|
|
|
|
* single-threaded; at module load, multiple modules would each have
|
|
|
|
* their own copy of these variables.
|
|
|
|
*/
|
|
|
|
static char *dsp = HOSTAUDIO_DEV_DSP;
|
|
|
|
static char *mixer = HOSTAUDIO_DEV_MIXER;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#define DSP_HELP \
|
|
|
|
" This is used to specify the host dsp device to the hostaudio driver.\n" \
|
|
|
|
" The default is \"" HOSTAUDIO_DEV_DSP "\".\n\n"
|
|
|
|
|
|
|
|
#define MIXER_HELP \
|
|
|
|
" This is used to specify the host mixer device to the hostaudio driver.\n"\
|
|
|
|
" The default is \"" HOSTAUDIO_DEV_MIXER "\".\n\n"
|
|
|
|
|
|
|
|
#ifndef MODULE
|
|
|
|
static int set_dsp(char *name, int *add)
|
|
|
|
{
|
|
|
|
dsp = name;
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
__uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);
|
|
|
|
|
|
|
|
static int set_mixer(char *name, int *add)
|
|
|
|
{
|
|
|
|
mixer = name;
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
__uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);
|
|
|
|
|
|
|
|
#else /*MODULE*/
|
|
|
|
|
2005-07-28 02:43:32 +08:00
|
|
|
module_param(dsp, charp, 0644);
|
2005-04-17 06:20:36 +08:00
|
|
|
MODULE_PARM_DESC(dsp, DSP_HELP);
|
|
|
|
|
2005-07-28 02:43:32 +08:00
|
|
|
module_param(mixer, charp, 0644);
|
2005-04-17 06:20:36 +08:00
|
|
|
MODULE_PARM_DESC(mixer, MIXER_HELP);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* /dev/dsp file operations */
|
|
|
|
|
2006-03-31 18:30:15 +08:00
|
|
|
static ssize_t hostaudio_read(struct file *file, char __user *buffer,
|
|
|
|
size_t count, loff_t *ppos)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-02-10 17:44:00 +08:00
|
|
|
struct hostaudio_state *state = file->private_data;
|
2005-04-17 06:20:36 +08:00
|
|
|
void *kbuf;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2007-02-10 17:44:00 +08:00
|
|
|
printk("hostaudio: read called, count = %d\n", count);
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
kbuf = kmalloc(count, GFP_KERNEL);
|
|
|
|
if(kbuf == NULL)
|
|
|
|
return(-ENOMEM);
|
|
|
|
|
uml: start fixing os_read_file and os_write_file
This patch starts the removal of a very old, very broken piece of code. This
stems from the problem of passing a userspace buffer into read() or write() on
the host. If that buffer had not yet been faulted in, read and write will
return -EFAULT.
To avoid this problem, the solution was to fault the buffer in before the
system call by touching the pages that hold the buffer by doing a copy-user of
a byte to each page. This is obviously bogus, but it does usually work, in tt
mode, since the kernel and process are in the same address space and userspace
addresses can be accessed directly in the kernel.
In skas mode, where the kernel and process are in separate address spaces, it
is completely bogus because the userspace address, which is invalid in the
kernel, is passed into the system call instead of the corresponding physical
address, which would be valid. Here, it appears that this code, on every host
read() or write(), tries to fault in a random process page. This doesn't seem
to cause any correctness problems, but there is a performance impact. This
patch, and the ones following, result in a 10-15% performance gain on a kernel
build.
This code can't be immediately tossed out because when it is, you can't log
in. Apparently, there is some code in the console driver which depends on
this somehow.
However, we can start removing it by switching the code which does I/O using
kernel addresses to using plain read() and write(). This patch introduces
os_read_file_k and os_write_file_k for use with kernel buffers and converts
all call locations which use obvious kernel buffers to use them. These
include I/O using buffers which are local variables which are on the stack or
kmalloc-ed. Later patches will handle the less obvious cases, followed by a
mass conversion back to the original interface.
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-07 05:51:32 +08:00
|
|
|
err = os_read_file_k(state->fd, kbuf, count);
|
2005-04-17 06:20:36 +08:00
|
|
|
if(err < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if(copy_to_user(buffer, kbuf, err))
|
|
|
|
err = -EFAULT;
|
|
|
|
|
2007-02-10 17:44:00 +08:00
|
|
|
out:
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(kbuf);
|
|
|
|
return(err);
|
|
|
|
}
|
|
|
|
|
2006-03-31 18:30:15 +08:00
|
|
|
static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
|
2005-04-17 06:20:36 +08:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
2007-02-10 17:44:00 +08:00
|
|
|
struct hostaudio_state *state = file->private_data;
|
2005-04-17 06:20:36 +08:00
|
|
|
void *kbuf;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2007-02-10 17:44:00 +08:00
|
|
|
printk("hostaudio: write called, count = %d\n", count);
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
kbuf = kmalloc(count, GFP_KERNEL);
|
|
|
|
if(kbuf == NULL)
|
|
|
|
return(-ENOMEM);
|
|
|
|
|
|
|
|
err = -EFAULT;
|
|
|
|
if(copy_from_user(kbuf, buffer, count))
|
|
|
|
goto out;
|
|
|
|
|
uml: start fixing os_read_file and os_write_file
This patch starts the removal of a very old, very broken piece of code. This
stems from the problem of passing a userspace buffer into read() or write() on
the host. If that buffer had not yet been faulted in, read and write will
return -EFAULT.
To avoid this problem, the solution was to fault the buffer in before the
system call by touching the pages that hold the buffer by doing a copy-user of
a byte to each page. This is obviously bogus, but it does usually work, in tt
mode, since the kernel and process are in the same address space and userspace
addresses can be accessed directly in the kernel.
In skas mode, where the kernel and process are in separate address spaces, it
is completely bogus because the userspace address, which is invalid in the
kernel, is passed into the system call instead of the corresponding physical
address, which would be valid. Here, it appears that this code, on every host
read() or write(), tries to fault in a random process page. This doesn't seem
to cause any correctness problems, but there is a performance impact. This
patch, and the ones following, result in a 10-15% performance gain on a kernel
build.
This code can't be immediately tossed out because when it is, you can't log
in. Apparently, there is some code in the console driver which depends on
this somehow.
However, we can start removing it by switching the code which does I/O using
kernel addresses to using plain read() and write(). This patch introduces
os_read_file_k and os_write_file_k for use with kernel buffers and converts
all call locations which use obvious kernel buffers to use them. These
include I/O using buffers which are local variables which are on the stack or
kmalloc-ed. Later patches will handle the less obvious cases, followed by a
mass conversion back to the original interface.
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-07 05:51:32 +08:00
|
|
|
err = os_write_file_k(state->fd, kbuf, count);
|
2005-04-17 06:20:36 +08:00
|
|
|
if(err < 0)
|
|
|
|
goto out;
|
|
|
|
*ppos += err;
|
|
|
|
|
|
|
|
out:
|
|
|
|
kfree(kbuf);
|
|
|
|
return(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int hostaudio_poll(struct file *file,
|
|
|
|
struct poll_table_struct *wait)
|
|
|
|
{
|
2007-02-10 17:44:00 +08:00
|
|
|
unsigned int mask = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2007-02-10 17:44:00 +08:00
|
|
|
printk("hostaudio: poll called (unimplemented)\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
|
2007-02-10 17:44:00 +08:00
|
|
|
return(mask);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int hostaudio_ioctl(struct inode *inode, struct file *file,
|
|
|
|
unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
2007-02-10 17:44:00 +08:00
|
|
|
struct hostaudio_state *state = file->private_data;
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long data = 0;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2007-02-10 17:44:00 +08:00
|
|
|
printk("hostaudio: ioctl called, cmd = %u\n", cmd);
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
switch(cmd){
|
|
|
|
case SNDCTL_DSP_SPEED:
|
|
|
|
case SNDCTL_DSP_STEREO:
|
|
|
|
case SNDCTL_DSP_GETBLKSIZE:
|
|
|
|
case SNDCTL_DSP_CHANNELS:
|
|
|
|
case SNDCTL_DSP_SUBDIVIDE:
|
|
|
|
case SNDCTL_DSP_SETFRAGMENT:
|
2006-03-31 18:30:15 +08:00
|
|
|
if(get_user(data, (int __user *) arg))
|
2005-04-17 06:20:36 +08:00
|
|
|
return(-EFAULT);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
|
|
|
|
|
|
|
|
switch(cmd){
|
|
|
|
case SNDCTL_DSP_SPEED:
|
|
|
|
case SNDCTL_DSP_STEREO:
|
|
|
|
case SNDCTL_DSP_GETBLKSIZE:
|
|
|
|
case SNDCTL_DSP_CHANNELS:
|
|
|
|
case SNDCTL_DSP_SUBDIVIDE:
|
|
|
|
case SNDCTL_DSP_SETFRAGMENT:
|
2006-03-31 18:30:15 +08:00
|
|
|
if(put_user(data, (int __user *) arg))
|
2005-04-17 06:20:36 +08:00
|
|
|
return(-EFAULT);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hostaudio_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2007-02-10 17:44:00 +08:00
|
|
|
struct hostaudio_state *state;
|
|
|
|
int r = 0, w = 0;
|
|
|
|
int ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2007-02-10 17:44:00 +08:00
|
|
|
printk("hostaudio: open called (host: %s)\n", dsp);
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
|
2007-02-10 17:44:00 +08:00
|
|
|
state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
|
|
|
|
if(state == NULL)
|
2005-04-17 06:20:36 +08:00
|
|
|
return(-ENOMEM);
|
|
|
|
|
2007-02-10 17:44:00 +08:00
|
|
|
if(file->f_mode & FMODE_READ) r = 1;
|
|
|
|
if(file->f_mode & FMODE_WRITE) w = 1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
|
2007-02-10 17:44:00 +08:00
|
|
|
if(ret < 0){
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(state);
|
|
|
|
return(ret);
|
2007-02-10 17:44:00 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
state->fd = ret;
|
2007-02-10 17:44:00 +08:00
|
|
|
file->private_data = state;
|
|
|
|
return(0);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int hostaudio_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
2007-02-10 17:44:00 +08:00
|
|
|
struct hostaudio_state *state = file->private_data;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2007-02-10 17:44:00 +08:00
|
|
|
printk("hostaudio: release called\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
2007-02-10 17:44:00 +08:00
|
|
|
os_close_file(state->fd);
|
|
|
|
kfree(state);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* /dev/mixer file operations */
|
|
|
|
|
|
|
|
static int hostmixer_ioctl_mixdev(struct inode *inode, struct file *file,
|
|
|
|
unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
2007-02-10 17:44:00 +08:00
|
|
|
struct hostmixer_state *state = file->private_data;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2007-02-10 17:44:00 +08:00
|
|
|
printk("hostmixer: ioctl called\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return(os_ioctl_generic(state->fd, cmd, arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
|
|
|
|
{
|
2007-02-10 17:44:00 +08:00
|
|
|
struct hostmixer_state *state;
|
|
|
|
int r = 0, w = 0;
|
|
|
|
int ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2007-02-10 17:44:00 +08:00
|
|
|
printk("hostmixer: open called (host: %s)\n", mixer);
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
|
2007-02-10 17:44:00 +08:00
|
|
|
state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
|
|
|
|
if(state == NULL) return(-ENOMEM);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-02-10 17:44:00 +08:00
|
|
|
if(file->f_mode & FMODE_READ) r = 1;
|
|
|
|
if(file->f_mode & FMODE_WRITE) w = 1;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
|
|
|
|
|
2007-02-10 17:44:00 +08:00
|
|
|
if(ret < 0){
|
2005-04-17 06:20:36 +08:00
|
|
|
printk("hostaudio_open_mixdev failed to open '%s', err = %d\n",
|
|
|
|
dsp, -ret);
|
|
|
|
kfree(state);
|
|
|
|
return(ret);
|
2007-02-10 17:44:00 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-02-10 17:44:00 +08:00
|
|
|
file->private_data = state;
|
|
|
|
return(0);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int hostmixer_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
2007-02-10 17:44:00 +08:00
|
|
|
struct hostmixer_state *state = file->private_data;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2007-02-10 17:44:00 +08:00
|
|
|
printk("hostmixer: release called\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
|
2007-02-10 17:44:00 +08:00
|
|
|
os_close_file(state->fd);
|
|
|
|
kfree(state);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* kernel module operations */
|
|
|
|
|
2006-09-27 16:50:33 +08:00
|
|
|
static const struct file_operations hostaudio_fops = {
|
2007-02-10 17:44:00 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.llseek = no_llseek,
|
|
|
|
.read = hostaudio_read,
|
|
|
|
.write = hostaudio_write,
|
|
|
|
.poll = hostaudio_poll,
|
|
|
|
.ioctl = hostaudio_ioctl,
|
|
|
|
.mmap = NULL,
|
|
|
|
.open = hostaudio_open,
|
|
|
|
.release = hostaudio_release,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2006-09-27 16:50:33 +08:00
|
|
|
static const struct file_operations hostmixer_fops = {
|
2007-02-10 17:44:00 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.llseek = no_llseek,
|
|
|
|
.ioctl = hostmixer_ioctl_mixdev,
|
|
|
|
.open = hostmixer_open_mixdev,
|
|
|
|
.release = hostmixer_release,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct {
|
|
|
|
int dev_audio;
|
|
|
|
int dev_mixer;
|
|
|
|
} module_data;
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Steve Schmidtke");
|
|
|
|
MODULE_DESCRIPTION("UML Audio Relay");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
|
|
static int __init hostaudio_init_module(void)
|
|
|
|
{
|
2007-02-10 17:44:00 +08:00
|
|
|
printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
dsp, mixer);
|
|
|
|
|
|
|
|
module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
|
2007-02-10 17:44:00 +08:00
|
|
|
if(module_data.dev_audio < 0){
|
|
|
|
printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
|
2007-02-10 17:44:00 +08:00
|
|
|
if(module_data.dev_mixer < 0){
|
|
|
|
printk(KERN_ERR "hostmixer: couldn't register mixer "
|
2005-04-17 06:20:36 +08:00
|
|
|
"device!\n");
|
2007-02-10 17:44:00 +08:00
|
|
|
unregister_sound_dsp(module_data.dev_audio);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-02-10 17:44:00 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit hostaudio_cleanup_module (void)
|
|
|
|
{
|
2007-02-10 17:44:00 +08:00
|
|
|
unregister_sound_mixer(module_data.dev_mixer);
|
|
|
|
unregister_sound_dsp(module_data.dev_audio);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module_init(hostaudio_init_module);
|
|
|
|
module_exit(hostaudio_cleanup_module);
|