2 * Copyright (C) 2002 Steve Schmidtke
3 * Licensed under the GPL
7 #include "linux/module.h"
8 #include "linux/slab.h"
9 #include "linux/sound.h"
10 #include "linux/soundcard.h"
11 #include "linux/mutex.h"
12 #include "asm/uaccess.h"
16 struct hostaudio_state {
20 struct hostmixer_state {
24 #define HOSTAUDIO_DEV_DSP "/dev/sound/dsp"
25 #define HOSTAUDIO_DEV_MIXER "/dev/sound/mixer"
28 * Changed either at boot time or module load time. At boot, this is
29 * single-threaded; at module load, multiple modules would each have
30 * their own copy of these variables.
32 static char *dsp = HOSTAUDIO_DEV_DSP;
33 static char *mixer = HOSTAUDIO_DEV_MIXER;
36 " This is used to specify the host dsp device to the hostaudio driver.\n" \
37 " The default is \"" HOSTAUDIO_DEV_DSP "\".\n\n"
40 " This is used to specify the host mixer device to the hostaudio driver.\n"\
41 " The default is \"" HOSTAUDIO_DEV_MIXER "\".\n\n"
44 static int set_dsp(char *name, int *add)
50 __uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);
52 static int set_mixer(char *name, int *add)
58 __uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);
62 module_param(dsp, charp, 0644);
63 MODULE_PARM_DESC(dsp, DSP_HELP);
65 module_param(mixer, charp, 0644);
66 MODULE_PARM_DESC(mixer, MIXER_HELP);
70 static DEFINE_MUTEX(hostaudio_mutex);
72 /* /dev/dsp file operations */
74 static ssize_t hostaudio_read(struct file *file, char __user *buffer,
75 size_t count, loff_t *ppos)
77 struct hostaudio_state *state = file->private_data;
82 printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
85 kbuf = kmalloc(count, GFP_KERNEL);
89 err = os_read_file(state->fd, kbuf, count);
93 if (copy_to_user(buffer, kbuf, err))
101 static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
102 size_t count, loff_t *ppos)
104 struct hostaudio_state *state = file->private_data;
109 printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
112 kbuf = kmalloc(count, GFP_KERNEL);
117 if (copy_from_user(kbuf, buffer, count))
120 err = os_write_file(state->fd, kbuf, count);
130 static unsigned int hostaudio_poll(struct file *file,
131 struct poll_table_struct *wait)
133 unsigned int mask = 0;
136 printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
142 static long hostaudio_ioctl(struct file *file,
143 unsigned int cmd, unsigned long arg)
145 struct hostaudio_state *state = file->private_data;
146 unsigned long data = 0;
150 printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
153 case SNDCTL_DSP_SPEED:
154 case SNDCTL_DSP_STEREO:
155 case SNDCTL_DSP_GETBLKSIZE:
156 case SNDCTL_DSP_CHANNELS:
157 case SNDCTL_DSP_SUBDIVIDE:
158 case SNDCTL_DSP_SETFRAGMENT:
159 if (get_user(data, (int __user *) arg))
166 err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
169 case SNDCTL_DSP_SPEED:
170 case SNDCTL_DSP_STEREO:
171 case SNDCTL_DSP_GETBLKSIZE:
172 case SNDCTL_DSP_CHANNELS:
173 case SNDCTL_DSP_SUBDIVIDE:
174 case SNDCTL_DSP_SETFRAGMENT:
175 if (put_user(data, (int __user *) arg))
185 static int hostaudio_open(struct inode *inode, struct file *file)
187 struct hostaudio_state *state;
192 kparam_block_sysfs_write(dsp);
193 printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
194 kparam_unblock_sysfs_write(dsp);
197 state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
201 if (file->f_mode & FMODE_READ)
203 if (file->f_mode & FMODE_WRITE)
206 kparam_block_sysfs_write(dsp);
207 mutex_lock(&hostaudio_mutex);
208 ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
209 mutex_unlock(&hostaudio_mutex);
210 kparam_unblock_sysfs_write(dsp);
217 file->private_data = state;
221 static int hostaudio_release(struct inode *inode, struct file *file)
223 struct hostaudio_state *state = file->private_data;
226 printk(KERN_DEBUG "hostaudio: release called\n");
228 os_close_file(state->fd);
234 /* /dev/mixer file operations */
236 static long hostmixer_ioctl_mixdev(struct file *file,
237 unsigned int cmd, unsigned long arg)
239 struct hostmixer_state *state = file->private_data;
242 printk(KERN_DEBUG "hostmixer: ioctl called\n");
245 return os_ioctl_generic(state->fd, cmd, arg);
248 static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
250 struct hostmixer_state *state;
255 printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
258 state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
262 if (file->f_mode & FMODE_READ)
264 if (file->f_mode & FMODE_WRITE)
267 kparam_block_sysfs_write(mixer);
268 mutex_lock(&hostaudio_mutex);
269 ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
270 mutex_unlock(&hostaudio_mutex);
271 kparam_unblock_sysfs_write(mixer);
274 kparam_block_sysfs_write(dsp);
275 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
276 "err = %d\n", dsp, -ret);
277 kparam_unblock_sysfs_write(dsp);
282 file->private_data = state;
286 static int hostmixer_release(struct inode *inode, struct file *file)
288 struct hostmixer_state *state = file->private_data;
291 printk(KERN_DEBUG "hostmixer: release called\n");
294 os_close_file(state->fd);
300 /* kernel module operations */
302 static const struct file_operations hostaudio_fops = {
303 .owner = THIS_MODULE,
305 .read = hostaudio_read,
306 .write = hostaudio_write,
307 .poll = hostaudio_poll,
308 .unlocked_ioctl = hostaudio_ioctl,
310 .open = hostaudio_open,
311 .release = hostaudio_release,
314 static const struct file_operations hostmixer_fops = {
315 .owner = THIS_MODULE,
317 .unlocked_ioctl = hostmixer_ioctl_mixdev,
318 .open = hostmixer_open_mixdev,
319 .release = hostmixer_release,
327 MODULE_AUTHOR("Steve Schmidtke");
328 MODULE_DESCRIPTION("UML Audio Relay");
329 MODULE_LICENSE("GPL");
331 static int __init hostaudio_init_module(void)
333 __kernel_param_lock();
334 printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
336 __kernel_param_unlock();
338 module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
339 if (module_data.dev_audio < 0) {
340 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
344 module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
345 if (module_data.dev_mixer < 0) {
346 printk(KERN_ERR "hostmixer: couldn't register mixer "
348 unregister_sound_dsp(module_data.dev_audio);
355 static void __exit hostaudio_cleanup_module (void)
357 unregister_sound_mixer(module_data.dev_mixer);
358 unregister_sound_dsp(module_data.dev_audio);
361 module_init(hostaudio_init_module);
362 module_exit(hostaudio_cleanup_module);