blob: 3a4b58730f5fbcf45e8a586b2005e3b4b9688eda [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
Al Viro37185b32012-10-08 03:27:32 +01006#include <linux/fs.h>
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>
13#include <init.h>
14#include <os.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
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
Al Viro1ceb3622016-01-02 14:50:51 -0500108 kbuf = memdup_user(buffer, count);
109 if (IS_ERR(kbuf))
110 return PTR_ERR(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700112 err = os_write_file(state->fd, kbuf, count);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700113 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 goto out;
115 *ppos += err;
116
117 out:
118 kfree(kbuf);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700119 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Jeff Dikecb8fa612007-10-16 01:27:34 -0700122static unsigned int hostaudio_poll(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct poll_table_struct *wait)
124{
Jeff Diked471c0f2007-02-10 01:44:00 -0800125 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700128 printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#endif
130
Jeff Dikecb8fa612007-10-16 01:27:34 -0700131 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
John Kacurd6c89d92010-05-07 17:34:28 +0200134static long hostaudio_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 unsigned int cmd, unsigned long arg)
136{
Jeff Diked471c0f2007-02-10 01:44:00 -0800137 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 unsigned long data = 0;
139 int err;
140
141#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700142 printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#endif
144 switch(cmd){
145 case SNDCTL_DSP_SPEED:
146 case SNDCTL_DSP_STEREO:
147 case SNDCTL_DSP_GETBLKSIZE:
148 case SNDCTL_DSP_CHANNELS:
149 case SNDCTL_DSP_SUBDIVIDE:
150 case SNDCTL_DSP_SETFRAGMENT:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700151 if (get_user(data, (int __user *) arg))
Johann Felix Soden484f1e22008-05-12 14:01:51 -0700152 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 break;
154 default:
155 break;
156 }
157
158 err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
159
160 switch(cmd){
161 case SNDCTL_DSP_SPEED:
162 case SNDCTL_DSP_STEREO:
163 case SNDCTL_DSP_GETBLKSIZE:
164 case SNDCTL_DSP_CHANNELS:
165 case SNDCTL_DSP_SUBDIVIDE:
166 case SNDCTL_DSP_SETFRAGMENT:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700167 if (put_user(data, (int __user *) arg))
168 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
170 default:
171 break;
172 }
173
Jeff Dikecb8fa612007-10-16 01:27:34 -0700174 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177static int hostaudio_open(struct inode *inode, struct file *file)
178{
Jeff Diked471c0f2007-02-10 01:44:00 -0800179 struct hostaudio_state *state;
180 int r = 0, w = 0;
181 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183#ifdef DEBUG
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930184 kernel_param_lock(THIS_MODULE);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700185 printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930186 kernel_param_unlock(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#endif
188
Jeff Diked471c0f2007-02-10 01:44:00 -0800189 state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700190 if (state == NULL)
191 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Jeff Dikecb8fa612007-10-16 01:27:34 -0700193 if (file->f_mode & FMODE_READ)
194 r = 1;
195 if (file->f_mode & FMODE_WRITE)
196 w = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930198 kernel_param_lock(THIS_MODULE);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200199 mutex_lock(&hostaudio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200201 mutex_unlock(&hostaudio_mutex);
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930202 kernel_param_unlock(THIS_MODULE);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200203
Jeff Dikecb8fa612007-10-16 01:27:34 -0700204 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 kfree(state);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700206 return ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 state->fd = ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800209 file->private_data = state;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213static int hostaudio_release(struct inode *inode, struct file *file)
214{
Jeff Diked471c0f2007-02-10 01:44:00 -0800215 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700218 printk(KERN_DEBUG "hostaudio: release called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#endif
Jeff Diked471c0f2007-02-10 01:44:00 -0800220 os_close_file(state->fd);
221 kfree(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Jeff Dikecb8fa612007-10-16 01:27:34 -0700223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226/* /dev/mixer file operations */
227
John Kacurd6c89d92010-05-07 17:34:28 +0200228static long hostmixer_ioctl_mixdev(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 unsigned int cmd, unsigned long arg)
230{
Jeff Diked471c0f2007-02-10 01:44:00 -0800231 struct hostmixer_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700234 printk(KERN_DEBUG "hostmixer: ioctl called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#endif
236
Jeff Dikecb8fa612007-10-16 01:27:34 -0700237 return os_ioctl_generic(state->fd, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
241{
Jeff Diked471c0f2007-02-10 01:44:00 -0800242 struct hostmixer_state *state;
243 int r = 0, w = 0;
244 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700247 printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#endif
249
Jeff Diked471c0f2007-02-10 01:44:00 -0800250 state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700251 if (state == NULL)
252 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Jeff Dikecb8fa612007-10-16 01:27:34 -0700254 if (file->f_mode & FMODE_READ)
255 r = 1;
256 if (file->f_mode & FMODE_WRITE)
257 w = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930259 kernel_param_lock(THIS_MODULE);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200260 mutex_lock(&hostaudio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200262 mutex_unlock(&hostaudio_mutex);
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930263 kernel_param_unlock(THIS_MODULE);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700264
265 if (ret < 0) {
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930266 kernel_param_lock(THIS_MODULE);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700267 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
268 "err = %d\n", dsp, -ret);
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930269 kernel_param_unlock(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 kfree(state);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700271 return ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Jeff Diked471c0f2007-02-10 01:44:00 -0800274 file->private_data = state;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278static int hostmixer_release(struct inode *inode, struct file *file)
279{
Jeff Diked471c0f2007-02-10 01:44:00 -0800280 struct hostmixer_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700283 printk(KERN_DEBUG "hostmixer: release called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#endif
285
Jeff Diked471c0f2007-02-10 01:44:00 -0800286 os_close_file(state->fd);
287 kfree(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Jeff Dikecb8fa612007-10-16 01:27:34 -0700289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/* kernel module operations */
293
Jeff Dike5e7672e2006-09-27 01:50:33 -0700294static const struct file_operations hostaudio_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800295 .owner = THIS_MODULE,
296 .llseek = no_llseek,
297 .read = hostaudio_read,
298 .write = hostaudio_write,
299 .poll = hostaudio_poll,
John Kacurd6c89d92010-05-07 17:34:28 +0200300 .unlocked_ioctl = hostaudio_ioctl,
Jeff Diked471c0f2007-02-10 01:44:00 -0800301 .mmap = NULL,
302 .open = hostaudio_open,
303 .release = hostaudio_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
Jeff Dike5e7672e2006-09-27 01:50:33 -0700306static const struct file_operations hostmixer_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800307 .owner = THIS_MODULE,
308 .llseek = no_llseek,
John Kacurd6c89d92010-05-07 17:34:28 +0200309 .unlocked_ioctl = hostmixer_ioctl_mixdev,
Jeff Diked471c0f2007-02-10 01:44:00 -0800310 .open = hostmixer_open_mixdev,
311 .release = hostmixer_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312};
313
314struct {
315 int dev_audio;
316 int dev_mixer;
317} module_data;
318
319MODULE_AUTHOR("Steve Schmidtke");
320MODULE_DESCRIPTION("UML Audio Relay");
321MODULE_LICENSE("GPL");
322
323static int __init hostaudio_init_module(void)
324{
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930325 kernel_param_lock(THIS_MODULE);
Jeff Diked471c0f2007-02-10 01:44:00 -0800326 printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 dsp, mixer);
Dan Streetmanb51d23e2015-06-17 06:18:52 +0930328 kernel_param_unlock(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700331 if (module_data.dev_audio < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800332 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
333 return -ENODEV;
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700337 if (module_data.dev_mixer < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800338 printk(KERN_ERR "hostmixer: couldn't register mixer "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 "device!\n");
Jeff Diked471c0f2007-02-10 01:44:00 -0800340 unregister_sound_dsp(module_data.dev_audio);
341 return -ENODEV;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Jeff Diked471c0f2007-02-10 01:44:00 -0800344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347static void __exit hostaudio_cleanup_module (void)
348{
Jeff Diked471c0f2007-02-10 01:44:00 -0800349 unregister_sound_mixer(module_data.dev_mixer);
350 unregister_sound_dsp(module_data.dev_audio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353module_init(hostaudio_init_module);
354module_exit(hostaudio_cleanup_module);