blob: d92ff6d602972a3865e456c7ac6f6d67a3179901 [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
43#ifndef MODULE
44static int set_dsp(char *name, int *add)
45{
46 dsp = name;
Jeff Dikecb8fa612007-10-16 01:27:34 -070047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
50__uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);
51
52static int set_mixer(char *name, int *add)
53{
54 mixer = name;
Jeff Dikecb8fa612007-10-16 01:27:34 -070055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
58__uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);
59
60#else /*MODULE*/
61
Dominik Hackl20a64f12005-07-27 11:43:32 -070062module_param(dsp, charp, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063MODULE_PARM_DESC(dsp, DSP_HELP);
64
Dominik Hackl20a64f12005-07-27 11:43:32 -070065module_param(mixer, charp, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066MODULE_PARM_DESC(mixer, MIXER_HELP);
67
68#endif
69
Arnd Bergmann9a181c52010-09-11 18:38:03 +020070static DEFINE_MUTEX(hostaudio_mutex);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* /dev/dsp file operations */
73
Al Viro4d338e12006-03-31 02:30:15 -080074static ssize_t hostaudio_read(struct file *file, char __user *buffer,
75 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Jeff Diked471c0f2007-02-10 01:44:00 -080077 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 void *kbuf;
79 int err;
80
81#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -070082 printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#endif
84
85 kbuf = kmalloc(count, GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -070086 if (kbuf == NULL)
87 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Jeff Dikea6ea4cc2007-05-06 14:51:43 -070089 err = os_read_file(state->fd, kbuf, count);
Jeff Dikecb8fa612007-10-16 01:27:34 -070090 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 goto out;
92
Jeff Dikecb8fa612007-10-16 01:27:34 -070093 if (copy_to_user(buffer, kbuf, err))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 err = -EFAULT;
95
Jeff Diked471c0f2007-02-10 01:44:00 -080096out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 kfree(kbuf);
Jeff Dikecb8fa612007-10-16 01:27:34 -070098 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Al Viro4d338e12006-03-31 02:30:15 -0800101static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 size_t count, loff_t *ppos)
103{
Jeff Diked471c0f2007-02-10 01:44:00 -0800104 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 void *kbuf;
106 int err;
107
108#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700109 printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#endif
111
112 kbuf = kmalloc(count, GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700113 if (kbuf == NULL)
114 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 err = -EFAULT;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700117 if (copy_from_user(kbuf, buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 goto out;
119
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700120 err = os_write_file(state->fd, kbuf, count);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700121 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 goto out;
123 *ppos += err;
124
125 out:
126 kfree(kbuf);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700127 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Jeff Dikecb8fa612007-10-16 01:27:34 -0700130static unsigned int hostaudio_poll(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct poll_table_struct *wait)
132{
Jeff Diked471c0f2007-02-10 01:44:00 -0800133 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700136 printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#endif
138
Jeff Dikecb8fa612007-10-16 01:27:34 -0700139 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
John Kacurd6c89d92010-05-07 17:34:28 +0200142static long hostaudio_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 unsigned int cmd, unsigned long arg)
144{
Jeff Diked471c0f2007-02-10 01:44:00 -0800145 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 unsigned long data = 0;
147 int err;
148
149#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700150 printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#endif
152 switch(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:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700159 if (get_user(data, (int __user *) arg))
Johann Felix Soden484f1e22008-05-12 14:01:51 -0700160 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 break;
162 default:
163 break;
164 }
165
166 err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
167
168 switch(cmd){
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:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700175 if (put_user(data, (int __user *) arg))
176 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 break;
178 default:
179 break;
180 }
181
Jeff Dikecb8fa612007-10-16 01:27:34 -0700182 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185static int hostaudio_open(struct inode *inode, struct file *file)
186{
Jeff Diked471c0f2007-02-10 01:44:00 -0800187 struct hostaudio_state *state;
188 int r = 0, w = 0;
189 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191#ifdef DEBUG
Rusty Russelld6d1b652010-08-11 23:04:27 -0600192 kparam_block_sysfs_write(dsp);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700193 printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600194 kparam_unblock_sysfs_write(dsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#endif
196
Jeff Diked471c0f2007-02-10 01:44:00 -0800197 state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700198 if (state == NULL)
199 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Jeff Dikecb8fa612007-10-16 01:27:34 -0700201 if (file->f_mode & FMODE_READ)
202 r = 1;
203 if (file->f_mode & FMODE_WRITE)
204 w = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Rusty Russelld6d1b652010-08-11 23:04:27 -0600206 kparam_block_sysfs_write(dsp);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200207 mutex_lock(&hostaudio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200209 mutex_unlock(&hostaudio_mutex);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600210 kparam_unblock_sysfs_write(dsp);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200211
Jeff Dikecb8fa612007-10-16 01:27:34 -0700212 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 kfree(state);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700214 return ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 state->fd = ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800217 file->private_data = state;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221static int hostaudio_release(struct inode *inode, struct file *file)
222{
Jeff Diked471c0f2007-02-10 01:44:00 -0800223 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700226 printk(KERN_DEBUG "hostaudio: release called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#endif
Jeff Diked471c0f2007-02-10 01:44:00 -0800228 os_close_file(state->fd);
229 kfree(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Jeff Dikecb8fa612007-10-16 01:27:34 -0700231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234/* /dev/mixer file operations */
235
John Kacurd6c89d92010-05-07 17:34:28 +0200236static long hostmixer_ioctl_mixdev(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 unsigned int cmd, unsigned long arg)
238{
Jeff Diked471c0f2007-02-10 01:44:00 -0800239 struct hostmixer_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700242 printk(KERN_DEBUG "hostmixer: ioctl called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#endif
244
Jeff Dikecb8fa612007-10-16 01:27:34 -0700245 return os_ioctl_generic(state->fd, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
249{
Jeff Diked471c0f2007-02-10 01:44:00 -0800250 struct hostmixer_state *state;
251 int r = 0, w = 0;
252 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700255 printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256#endif
257
Jeff Diked471c0f2007-02-10 01:44:00 -0800258 state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700259 if (state == NULL)
260 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Jeff Dikecb8fa612007-10-16 01:27:34 -0700262 if (file->f_mode & FMODE_READ)
263 r = 1;
264 if (file->f_mode & FMODE_WRITE)
265 w = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Rusty Russelld6d1b652010-08-11 23:04:27 -0600267 kparam_block_sysfs_write(mixer);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200268 mutex_lock(&hostaudio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200270 mutex_unlock(&hostaudio_mutex);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600271 kparam_unblock_sysfs_write(mixer);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700272
273 if (ret < 0) {
Rusty Russelld6d1b652010-08-11 23:04:27 -0600274 kparam_block_sysfs_write(dsp);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700275 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
276 "err = %d\n", dsp, -ret);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600277 kparam_unblock_sysfs_write(dsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 kfree(state);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700279 return ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Jeff Diked471c0f2007-02-10 01:44:00 -0800282 file->private_data = state;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286static int hostmixer_release(struct inode *inode, struct file *file)
287{
Jeff Diked471c0f2007-02-10 01:44:00 -0800288 struct hostmixer_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700291 printk(KERN_DEBUG "hostmixer: release called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292#endif
293
Jeff Diked471c0f2007-02-10 01:44:00 -0800294 os_close_file(state->fd);
295 kfree(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Jeff Dikecb8fa612007-10-16 01:27:34 -0700297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/* kernel module operations */
301
Jeff Dike5e7672e2006-09-27 01:50:33 -0700302static const struct file_operations hostaudio_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800303 .owner = THIS_MODULE,
304 .llseek = no_llseek,
305 .read = hostaudio_read,
306 .write = hostaudio_write,
307 .poll = hostaudio_poll,
John Kacurd6c89d92010-05-07 17:34:28 +0200308 .unlocked_ioctl = hostaudio_ioctl,
Jeff Diked471c0f2007-02-10 01:44:00 -0800309 .mmap = NULL,
310 .open = hostaudio_open,
311 .release = hostaudio_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312};
313
Jeff Dike5e7672e2006-09-27 01:50:33 -0700314static const struct file_operations hostmixer_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800315 .owner = THIS_MODULE,
316 .llseek = no_llseek,
John Kacurd6c89d92010-05-07 17:34:28 +0200317 .unlocked_ioctl = hostmixer_ioctl_mixdev,
Jeff Diked471c0f2007-02-10 01:44:00 -0800318 .open = hostmixer_open_mixdev,
319 .release = hostmixer_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320};
321
322struct {
323 int dev_audio;
324 int dev_mixer;
325} module_data;
326
327MODULE_AUTHOR("Steve Schmidtke");
328MODULE_DESCRIPTION("UML Audio Relay");
329MODULE_LICENSE("GPL");
330
331static int __init hostaudio_init_module(void)
332{
Rusty Russelld6d1b652010-08-11 23:04:27 -0600333 __kernel_param_lock();
Jeff Diked471c0f2007-02-10 01:44:00 -0800334 printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 dsp, mixer);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600336 __kernel_param_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700339 if (module_data.dev_audio < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800340 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
341 return -ENODEV;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700345 if (module_data.dev_mixer < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800346 printk(KERN_ERR "hostmixer: couldn't register mixer "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 "device!\n");
Jeff Diked471c0f2007-02-10 01:44:00 -0800348 unregister_sound_dsp(module_data.dev_audio);
349 return -ENODEV;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Jeff Diked471c0f2007-02-10 01:44:00 -0800352 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355static void __exit hostaudio_cleanup_module (void)
356{
Jeff Diked471c0f2007-02-10 01:44:00 -0800357 unregister_sound_mixer(module_data.dev_mixer);
358 unregister_sound_dsp(module_data.dev_audio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361module_init(hostaudio_init_module);
362module_exit(hostaudio_cleanup_module);