blob: 63c740a85b4cca0ed9333091593885cbdfface2e [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 Bergmann90dc7632010-07-11 12:16:36 +020011#include "linux/smp_lock.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
66/* /dev/dsp file operations */
67
Al Viro4d338e12006-03-31 02:30:15 -080068static ssize_t hostaudio_read(struct file *file, char __user *buffer,
69 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Jeff Diked471c0f2007-02-10 01:44:00 -080071 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 void *kbuf;
73 int err;
74
75#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -070076 printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#endif
78
79 kbuf = kmalloc(count, GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -070080 if (kbuf == NULL)
81 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Jeff Dikea6ea4cc2007-05-06 14:51:43 -070083 err = os_read_file(state->fd, kbuf, count);
Jeff Dikecb8fa612007-10-16 01:27:34 -070084 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 goto out;
86
Jeff Dikecb8fa612007-10-16 01:27:34 -070087 if (copy_to_user(buffer, kbuf, err))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 err = -EFAULT;
89
Jeff Diked471c0f2007-02-10 01:44:00 -080090out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 kfree(kbuf);
Jeff Dikecb8fa612007-10-16 01:27:34 -070092 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Al Viro4d338e12006-03-31 02:30:15 -080095static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 size_t count, loff_t *ppos)
97{
Jeff Diked471c0f2007-02-10 01:44:00 -080098 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 void *kbuf;
100 int err;
101
102#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700103 printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#endif
105
106 kbuf = kmalloc(count, GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700107 if (kbuf == NULL)
108 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 err = -EFAULT;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700111 if (copy_from_user(kbuf, buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 goto out;
113
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700114 err = os_write_file(state->fd, kbuf, count);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700115 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 goto out;
117 *ppos += err;
118
119 out:
120 kfree(kbuf);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700121 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Jeff Dikecb8fa612007-10-16 01:27:34 -0700124static unsigned int hostaudio_poll(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 struct poll_table_struct *wait)
126{
Jeff Diked471c0f2007-02-10 01:44:00 -0800127 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700130 printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#endif
132
Jeff Dikecb8fa612007-10-16 01:27:34 -0700133 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
John Kacurd6c89d92010-05-07 17:34:28 +0200136static long hostaudio_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 unsigned int cmd, unsigned long arg)
138{
Jeff Diked471c0f2007-02-10 01:44:00 -0800139 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 unsigned long data = 0;
141 int err;
142
143#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700144 printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#endif
146 switch(cmd){
147 case SNDCTL_DSP_SPEED:
148 case SNDCTL_DSP_STEREO:
149 case SNDCTL_DSP_GETBLKSIZE:
150 case SNDCTL_DSP_CHANNELS:
151 case SNDCTL_DSP_SUBDIVIDE:
152 case SNDCTL_DSP_SETFRAGMENT:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700153 if (get_user(data, (int __user *) arg))
Johann Felix Soden484f1e22008-05-12 14:01:51 -0700154 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 break;
156 default:
157 break;
158 }
159
160 err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
161
162 switch(cmd){
163 case SNDCTL_DSP_SPEED:
164 case SNDCTL_DSP_STEREO:
165 case SNDCTL_DSP_GETBLKSIZE:
166 case SNDCTL_DSP_CHANNELS:
167 case SNDCTL_DSP_SUBDIVIDE:
168 case SNDCTL_DSP_SETFRAGMENT:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700169 if (put_user(data, (int __user *) arg))
170 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 break;
172 default:
173 break;
174 }
175
Jeff Dikecb8fa612007-10-16 01:27:34 -0700176 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179static int hostaudio_open(struct inode *inode, struct file *file)
180{
Jeff Diked471c0f2007-02-10 01:44:00 -0800181 struct hostaudio_state *state;
182 int r = 0, w = 0;
183 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185#ifdef DEBUG
Rusty Russelld6d1b652010-08-11 23:04:27 -0600186 kparam_block_sysfs_write(dsp);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700187 printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600188 kparam_unblock_sysfs_write(dsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#endif
190
Jeff Diked471c0f2007-02-10 01:44:00 -0800191 state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700192 if (state == NULL)
193 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Jeff Dikecb8fa612007-10-16 01:27:34 -0700195 if (file->f_mode & FMODE_READ)
196 r = 1;
197 if (file->f_mode & FMODE_WRITE)
198 w = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Rusty Russelld6d1b652010-08-11 23:04:27 -0600200 kparam_block_sysfs_write(dsp);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200201 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200203 unlock_kernel();
Rusty Russelld6d1b652010-08-11 23:04:27 -0600204 kparam_unblock_sysfs_write(dsp);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200205
Jeff Dikecb8fa612007-10-16 01:27:34 -0700206 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 kfree(state);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700208 return ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 state->fd = ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800211 file->private_data = state;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215static int hostaudio_release(struct inode *inode, struct file *file)
216{
Jeff Diked471c0f2007-02-10 01:44:00 -0800217 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700220 printk(KERN_DEBUG "hostaudio: release called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#endif
Jeff Diked471c0f2007-02-10 01:44:00 -0800222 os_close_file(state->fd);
223 kfree(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Jeff Dikecb8fa612007-10-16 01:27:34 -0700225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228/* /dev/mixer file operations */
229
John Kacurd6c89d92010-05-07 17:34:28 +0200230static long hostmixer_ioctl_mixdev(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 unsigned int cmd, unsigned long arg)
232{
Jeff Diked471c0f2007-02-10 01:44:00 -0800233 struct hostmixer_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700236 printk(KERN_DEBUG "hostmixer: ioctl called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237#endif
238
Jeff Dikecb8fa612007-10-16 01:27:34 -0700239 return os_ioctl_generic(state->fd, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
243{
Jeff Diked471c0f2007-02-10 01:44:00 -0800244 struct hostmixer_state *state;
245 int r = 0, w = 0;
246 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700249 printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#endif
251
Jeff Diked471c0f2007-02-10 01:44:00 -0800252 state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700253 if (state == NULL)
254 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Jeff Dikecb8fa612007-10-16 01:27:34 -0700256 if (file->f_mode & FMODE_READ)
257 r = 1;
258 if (file->f_mode & FMODE_WRITE)
259 w = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Rusty Russelld6d1b652010-08-11 23:04:27 -0600261 kparam_block_sysfs_write(mixer);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200262 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200264 unlock_kernel();
Rusty Russelld6d1b652010-08-11 23:04:27 -0600265 kparam_unblock_sysfs_write(mixer);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700266
267 if (ret < 0) {
Rusty Russelld6d1b652010-08-11 23:04:27 -0600268 kparam_block_sysfs_write(dsp);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700269 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
270 "err = %d\n", dsp, -ret);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600271 kparam_unblock_sysfs_write(dsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 kfree(state);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700273 return ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Jeff Diked471c0f2007-02-10 01:44:00 -0800276 file->private_data = state;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280static int hostmixer_release(struct inode *inode, struct file *file)
281{
Jeff Diked471c0f2007-02-10 01:44:00 -0800282 struct hostmixer_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700285 printk(KERN_DEBUG "hostmixer: release called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286#endif
287
Jeff Diked471c0f2007-02-10 01:44:00 -0800288 os_close_file(state->fd);
289 kfree(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Jeff Dikecb8fa612007-10-16 01:27:34 -0700291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/* kernel module operations */
295
Jeff Dike5e7672e2006-09-27 01:50:33 -0700296static const struct file_operations hostaudio_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800297 .owner = THIS_MODULE,
298 .llseek = no_llseek,
299 .read = hostaudio_read,
300 .write = hostaudio_write,
301 .poll = hostaudio_poll,
John Kacurd6c89d92010-05-07 17:34:28 +0200302 .unlocked_ioctl = hostaudio_ioctl,
Jeff Diked471c0f2007-02-10 01:44:00 -0800303 .mmap = NULL,
304 .open = hostaudio_open,
305 .release = hostaudio_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306};
307
Jeff Dike5e7672e2006-09-27 01:50:33 -0700308static const struct file_operations hostmixer_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800309 .owner = THIS_MODULE,
310 .llseek = no_llseek,
John Kacurd6c89d92010-05-07 17:34:28 +0200311 .unlocked_ioctl = hostmixer_ioctl_mixdev,
Jeff Diked471c0f2007-02-10 01:44:00 -0800312 .open = hostmixer_open_mixdev,
313 .release = hostmixer_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314};
315
316struct {
317 int dev_audio;
318 int dev_mixer;
319} module_data;
320
321MODULE_AUTHOR("Steve Schmidtke");
322MODULE_DESCRIPTION("UML Audio Relay");
323MODULE_LICENSE("GPL");
324
325static int __init hostaudio_init_module(void)
326{
Rusty Russelld6d1b652010-08-11 23:04:27 -0600327 __kernel_param_lock();
Jeff Diked471c0f2007-02-10 01:44:00 -0800328 printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 dsp, mixer);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600330 __kernel_param_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700333 if (module_data.dev_audio < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800334 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
335 return -ENODEV;
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700339 if (module_data.dev_mixer < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800340 printk(KERN_ERR "hostmixer: couldn't register mixer "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 "device!\n");
Jeff Diked471c0f2007-02-10 01:44:00 -0800342 unregister_sound_dsp(module_data.dev_audio);
343 return -ENODEV;
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Jeff Diked471c0f2007-02-10 01:44:00 -0800346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349static void __exit hostaudio_cleanup_module (void)
350{
Jeff Diked471c0f2007-02-10 01:44:00 -0800351 unregister_sound_mixer(module_data.dev_mixer);
352 unregister_sound_dsp(module_data.dev_audio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355module_init(hostaudio_init_module);
356module_exit(hostaudio_cleanup_module);