blob: f9f6a4e205901ee603d56d0167fb3c6e0807e868 [file] [log] [blame]
Jeff Dikecb8fa612007-10-16 01:27:34 -07001/*
2 * Copyright (C) 2002 Steve Schmidtke
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "linux/fs.h"
Jeff Dikecb8fa612007-10-16 01:27:34 -07007#include "linux/module.h"
8#include "linux/slab.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "linux/sound.h"
10#include "linux/soundcard.h"
Arnd Bergmann9a181c52010-09-11 18:38:03 +020011#include "linux/mutex.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "asm/uaccess.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "init.h"
14#include "os.h"
15
16struct hostaudio_state {
Jeff Diked471c0f2007-02-10 01:44:00 -080017 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018};
19
20struct hostmixer_state {
Jeff Diked471c0f2007-02-10 01:44:00 -080021 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022};
23
24#define HOSTAUDIO_DEV_DSP "/dev/sound/dsp"
25#define HOSTAUDIO_DEV_MIXER "/dev/sound/mixer"
26
Jeff Dikecb8fa612007-10-16 01:27:34 -070027/*
28 * Changed either at boot time or module load time. At boot, this is
Jeff Dikeb612e472007-02-10 01:43:59 -080029 * single-threaded; at module load, multiple modules would each have
30 * their own copy of these variables.
31 */
32static char *dsp = HOSTAUDIO_DEV_DSP;
33static char *mixer = HOSTAUDIO_DEV_MIXER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#define DSP_HELP \
36" This is used to specify the host dsp device to the hostaudio driver.\n" \
37" The default is \"" HOSTAUDIO_DEV_DSP "\".\n\n"
38
39#define MIXER_HELP \
40" This is used to specify the host mixer device to the hostaudio driver.\n"\
41" The default is \"" HOSTAUDIO_DEV_MIXER "\".\n\n"
42
FUJITA Tomonorie3c6cf62010-10-15 14:34:13 -070043module_param(dsp, charp, 0644);
44MODULE_PARM_DESC(dsp, DSP_HELP);
45module_param(mixer, charp, 0644);
46MODULE_PARM_DESC(mixer, MIXER_HELP);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#ifndef MODULE
49static int set_dsp(char *name, int *add)
50{
51 dsp = name;
Jeff Dikecb8fa612007-10-16 01:27:34 -070052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
55__uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);
56
57static int set_mixer(char *name, int *add)
58{
59 mixer = name;
Jeff Dikecb8fa612007-10-16 01:27:34 -070060 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63__uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#endif
65
Arnd Bergmann9a181c52010-09-11 18:38:03 +020066static DEFINE_MUTEX(hostaudio_mutex);
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* /dev/dsp file operations */
69
Al Viro4d338e12006-03-31 02:30:15 -080070static ssize_t hostaudio_read(struct file *file, char __user *buffer,
71 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Jeff Diked471c0f2007-02-10 01:44:00 -080073 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 void *kbuf;
75 int err;
76
77#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -070078 printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#endif
80
81 kbuf = kmalloc(count, GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -070082 if (kbuf == NULL)
83 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Jeff Dikea6ea4cc2007-05-06 14:51:43 -070085 err = os_read_file(state->fd, kbuf, count);
Jeff Dikecb8fa612007-10-16 01:27:34 -070086 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 goto out;
88
Jeff Dikecb8fa612007-10-16 01:27:34 -070089 if (copy_to_user(buffer, kbuf, err))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 err = -EFAULT;
91
Jeff Diked471c0f2007-02-10 01:44:00 -080092out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 kfree(kbuf);
Jeff Dikecb8fa612007-10-16 01:27:34 -070094 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Al Viro4d338e12006-03-31 02:30:15 -080097static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 size_t count, loff_t *ppos)
99{
Jeff Diked471c0f2007-02-10 01:44:00 -0800100 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 void *kbuf;
102 int err;
103
104#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700105 printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#endif
107
108 kbuf = kmalloc(count, GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700109 if (kbuf == NULL)
110 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 err = -EFAULT;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700113 if (copy_from_user(kbuf, buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 goto out;
115
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700116 err = os_write_file(state->fd, kbuf, count);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700117 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 goto out;
119 *ppos += err;
120
121 out:
122 kfree(kbuf);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700123 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Jeff Dikecb8fa612007-10-16 01:27:34 -0700126static unsigned int hostaudio_poll(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 struct poll_table_struct *wait)
128{
Jeff Diked471c0f2007-02-10 01:44:00 -0800129 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700132 printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#endif
134
Jeff Dikecb8fa612007-10-16 01:27:34 -0700135 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
John Kacurd6c89d92010-05-07 17:34:28 +0200138static long hostaudio_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 unsigned int cmd, unsigned long arg)
140{
Jeff Diked471c0f2007-02-10 01:44:00 -0800141 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 unsigned long data = 0;
143 int err;
144
145#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700146 printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#endif
148 switch(cmd){
149 case SNDCTL_DSP_SPEED:
150 case SNDCTL_DSP_STEREO:
151 case SNDCTL_DSP_GETBLKSIZE:
152 case SNDCTL_DSP_CHANNELS:
153 case SNDCTL_DSP_SUBDIVIDE:
154 case SNDCTL_DSP_SETFRAGMENT:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700155 if (get_user(data, (int __user *) arg))
Johann Felix Soden484f1e22008-05-12 14:01:51 -0700156 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 break;
158 default:
159 break;
160 }
161
162 err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
163
164 switch(cmd){
165 case SNDCTL_DSP_SPEED:
166 case SNDCTL_DSP_STEREO:
167 case SNDCTL_DSP_GETBLKSIZE:
168 case SNDCTL_DSP_CHANNELS:
169 case SNDCTL_DSP_SUBDIVIDE:
170 case SNDCTL_DSP_SETFRAGMENT:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700171 if (put_user(data, (int __user *) arg))
172 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 break;
174 default:
175 break;
176 }
177
Jeff Dikecb8fa612007-10-16 01:27:34 -0700178 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181static int hostaudio_open(struct inode *inode, struct file *file)
182{
Jeff Diked471c0f2007-02-10 01:44:00 -0800183 struct hostaudio_state *state;
184 int r = 0, w = 0;
185 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187#ifdef DEBUG
Rusty Russelld6d1b652010-08-11 23:04:27 -0600188 kparam_block_sysfs_write(dsp);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700189 printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600190 kparam_unblock_sysfs_write(dsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#endif
192
Jeff Diked471c0f2007-02-10 01:44:00 -0800193 state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700194 if (state == NULL)
195 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Jeff Dikecb8fa612007-10-16 01:27:34 -0700197 if (file->f_mode & FMODE_READ)
198 r = 1;
199 if (file->f_mode & FMODE_WRITE)
200 w = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Rusty Russelld6d1b652010-08-11 23:04:27 -0600202 kparam_block_sysfs_write(dsp);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200203 mutex_lock(&hostaudio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200205 mutex_unlock(&hostaudio_mutex);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600206 kparam_unblock_sysfs_write(dsp);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200207
Jeff Dikecb8fa612007-10-16 01:27:34 -0700208 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 kfree(state);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700210 return ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 state->fd = ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800213 file->private_data = state;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217static int hostaudio_release(struct inode *inode, struct file *file)
218{
Jeff Diked471c0f2007-02-10 01:44:00 -0800219 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700222 printk(KERN_DEBUG "hostaudio: release called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#endif
Jeff Diked471c0f2007-02-10 01:44:00 -0800224 os_close_file(state->fd);
225 kfree(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Jeff Dikecb8fa612007-10-16 01:27:34 -0700227 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230/* /dev/mixer file operations */
231
John Kacurd6c89d92010-05-07 17:34:28 +0200232static long hostmixer_ioctl_mixdev(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 unsigned int cmd, unsigned long arg)
234{
Jeff Diked471c0f2007-02-10 01:44:00 -0800235 struct hostmixer_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700238 printk(KERN_DEBUG "hostmixer: ioctl called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239#endif
240
Jeff Dikecb8fa612007-10-16 01:27:34 -0700241 return os_ioctl_generic(state->fd, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
245{
Jeff Diked471c0f2007-02-10 01:44:00 -0800246 struct hostmixer_state *state;
247 int r = 0, w = 0;
248 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700251 printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#endif
253
Jeff Diked471c0f2007-02-10 01:44:00 -0800254 state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700255 if (state == NULL)
256 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Jeff Dikecb8fa612007-10-16 01:27:34 -0700258 if (file->f_mode & FMODE_READ)
259 r = 1;
260 if (file->f_mode & FMODE_WRITE)
261 w = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Rusty Russelld6d1b652010-08-11 23:04:27 -0600263 kparam_block_sysfs_write(mixer);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200264 mutex_lock(&hostaudio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200266 mutex_unlock(&hostaudio_mutex);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600267 kparam_unblock_sysfs_write(mixer);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700268
269 if (ret < 0) {
Rusty Russelld6d1b652010-08-11 23:04:27 -0600270 kparam_block_sysfs_write(dsp);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700271 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
272 "err = %d\n", dsp, -ret);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600273 kparam_unblock_sysfs_write(dsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 kfree(state);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700275 return ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Jeff Diked471c0f2007-02-10 01:44:00 -0800278 file->private_data = state;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static int hostmixer_release(struct inode *inode, struct file *file)
283{
Jeff Diked471c0f2007-02-10 01:44:00 -0800284 struct hostmixer_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700287 printk(KERN_DEBUG "hostmixer: release called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288#endif
289
Jeff Diked471c0f2007-02-10 01:44:00 -0800290 os_close_file(state->fd);
291 kfree(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Jeff Dikecb8fa612007-10-16 01:27:34 -0700293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/* kernel module operations */
297
Jeff Dike5e7672e2006-09-27 01:50:33 -0700298static const struct file_operations hostaudio_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800299 .owner = THIS_MODULE,
300 .llseek = no_llseek,
301 .read = hostaudio_read,
302 .write = hostaudio_write,
303 .poll = hostaudio_poll,
John Kacurd6c89d92010-05-07 17:34:28 +0200304 .unlocked_ioctl = hostaudio_ioctl,
Jeff Diked471c0f2007-02-10 01:44:00 -0800305 .mmap = NULL,
306 .open = hostaudio_open,
307 .release = hostaudio_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308};
309
Jeff Dike5e7672e2006-09-27 01:50:33 -0700310static const struct file_operations hostmixer_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800311 .owner = THIS_MODULE,
312 .llseek = no_llseek,
John Kacurd6c89d92010-05-07 17:34:28 +0200313 .unlocked_ioctl = hostmixer_ioctl_mixdev,
Jeff Diked471c0f2007-02-10 01:44:00 -0800314 .open = hostmixer_open_mixdev,
315 .release = hostmixer_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316};
317
318struct {
319 int dev_audio;
320 int dev_mixer;
321} module_data;
322
323MODULE_AUTHOR("Steve Schmidtke");
324MODULE_DESCRIPTION("UML Audio Relay");
325MODULE_LICENSE("GPL");
326
327static int __init hostaudio_init_module(void)
328{
Rusty Russelld6d1b652010-08-11 23:04:27 -0600329 __kernel_param_lock();
Jeff Diked471c0f2007-02-10 01:44:00 -0800330 printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 dsp, mixer);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600332 __kernel_param_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700335 if (module_data.dev_audio < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800336 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
337 return -ENODEV;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700341 if (module_data.dev_mixer < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800342 printk(KERN_ERR "hostmixer: couldn't register mixer "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 "device!\n");
Jeff Diked471c0f2007-02-10 01:44:00 -0800344 unregister_sound_dsp(module_data.dev_audio);
345 return -ENODEV;
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Jeff Diked471c0f2007-02-10 01:44:00 -0800348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351static void __exit hostaudio_cleanup_module (void)
352{
Jeff Diked471c0f2007-02-10 01:44:00 -0800353 unregister_sound_mixer(module_data.dev_mixer);
354 unregister_sound_dsp(module_data.dev_audio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357module_init(hostaudio_init_module);
358module_exit(hostaudio_cleanup_module);