blob: 68142df76608319a666bf455713e54c9c683bb88 [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
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
70/* /dev/dsp file operations */
71
Al Viro4d338e12006-03-31 02:30:15 -080072static ssize_t hostaudio_read(struct file *file, char __user *buffer,
73 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Jeff Diked471c0f2007-02-10 01:44:00 -080075 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 void *kbuf;
77 int err;
78
79#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -070080 printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#endif
82
83 kbuf = kmalloc(count, GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -070084 if (kbuf == NULL)
85 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Jeff Dikea6ea4cc2007-05-06 14:51:43 -070087 err = os_read_file(state->fd, kbuf, count);
Jeff Dikecb8fa612007-10-16 01:27:34 -070088 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 goto out;
90
Jeff Dikecb8fa612007-10-16 01:27:34 -070091 if (copy_to_user(buffer, kbuf, err))
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 err = -EFAULT;
93
Jeff Diked471c0f2007-02-10 01:44:00 -080094out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 kfree(kbuf);
Jeff Dikecb8fa612007-10-16 01:27:34 -070096 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Al Viro4d338e12006-03-31 02:30:15 -080099static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 size_t count, loff_t *ppos)
101{
Jeff Diked471c0f2007-02-10 01:44:00 -0800102 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 void *kbuf;
104 int err;
105
106#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700107 printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#endif
109
110 kbuf = kmalloc(count, GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700111 if (kbuf == NULL)
112 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 err = -EFAULT;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700115 if (copy_from_user(kbuf, buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 goto out;
117
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700118 err = os_write_file(state->fd, kbuf, count);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700119 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 goto out;
121 *ppos += err;
122
123 out:
124 kfree(kbuf);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700125 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Jeff Dikecb8fa612007-10-16 01:27:34 -0700128static unsigned int hostaudio_poll(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct poll_table_struct *wait)
130{
Jeff Diked471c0f2007-02-10 01:44:00 -0800131 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700134 printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#endif
136
Jeff Dikecb8fa612007-10-16 01:27:34 -0700137 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
John Kacurd6c89d92010-05-07 17:34:28 +0200140static long hostaudio_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 unsigned int cmd, unsigned long arg)
142{
Jeff Diked471c0f2007-02-10 01:44:00 -0800143 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 unsigned long data = 0;
145 int err;
146
147#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700148 printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#endif
150 switch(cmd){
151 case SNDCTL_DSP_SPEED:
152 case SNDCTL_DSP_STEREO:
153 case SNDCTL_DSP_GETBLKSIZE:
154 case SNDCTL_DSP_CHANNELS:
155 case SNDCTL_DSP_SUBDIVIDE:
156 case SNDCTL_DSP_SETFRAGMENT:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700157 if (get_user(data, (int __user *) arg))
Johann Felix Soden484f1e22008-05-12 14:01:51 -0700158 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 break;
160 default:
161 break;
162 }
163
164 err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
165
166 switch(cmd){
167 case SNDCTL_DSP_SPEED:
168 case SNDCTL_DSP_STEREO:
169 case SNDCTL_DSP_GETBLKSIZE:
170 case SNDCTL_DSP_CHANNELS:
171 case SNDCTL_DSP_SUBDIVIDE:
172 case SNDCTL_DSP_SETFRAGMENT:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700173 if (put_user(data, (int __user *) arg))
174 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 break;
176 default:
177 break;
178 }
179
Jeff Dikecb8fa612007-10-16 01:27:34 -0700180 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183static int hostaudio_open(struct inode *inode, struct file *file)
184{
Jeff Diked471c0f2007-02-10 01:44:00 -0800185 struct hostaudio_state *state;
186 int r = 0, w = 0;
187 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700190 printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", 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
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200202 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200204 unlock_kernel();
205
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
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200261 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200263 unlock_kernel();
Jeff Dikecb8fa612007-10-16 01:27:34 -0700264
265 if (ret < 0) {
266 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
267 "err = %d\n", dsp, -ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 kfree(state);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700269 return ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Jeff Diked471c0f2007-02-10 01:44:00 -0800272 file->private_data = state;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276static int hostmixer_release(struct inode *inode, struct file *file)
277{
Jeff Diked471c0f2007-02-10 01:44:00 -0800278 struct hostmixer_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700281 printk(KERN_DEBUG "hostmixer: release called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282#endif
283
Jeff Diked471c0f2007-02-10 01:44:00 -0800284 os_close_file(state->fd);
285 kfree(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Jeff Dikecb8fa612007-10-16 01:27:34 -0700287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/* kernel module operations */
291
Jeff Dike5e7672e2006-09-27 01:50:33 -0700292static const struct file_operations hostaudio_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800293 .owner = THIS_MODULE,
294 .llseek = no_llseek,
295 .read = hostaudio_read,
296 .write = hostaudio_write,
297 .poll = hostaudio_poll,
John Kacurd6c89d92010-05-07 17:34:28 +0200298 .unlocked_ioctl = hostaudio_ioctl,
Jeff Diked471c0f2007-02-10 01:44:00 -0800299 .mmap = NULL,
300 .open = hostaudio_open,
301 .release = hostaudio_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
303
Jeff Dike5e7672e2006-09-27 01:50:33 -0700304static const struct file_operations hostmixer_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800305 .owner = THIS_MODULE,
306 .llseek = no_llseek,
John Kacurd6c89d92010-05-07 17:34:28 +0200307 .unlocked_ioctl = hostmixer_ioctl_mixdev,
Jeff Diked471c0f2007-02-10 01:44:00 -0800308 .open = hostmixer_open_mixdev,
309 .release = hostmixer_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
312struct {
313 int dev_audio;
314 int dev_mixer;
315} module_data;
316
317MODULE_AUTHOR("Steve Schmidtke");
318MODULE_DESCRIPTION("UML Audio Relay");
319MODULE_LICENSE("GPL");
320
321static int __init hostaudio_init_module(void)
322{
Jeff Diked471c0f2007-02-10 01:44:00 -0800323 printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 dsp, mixer);
325
326 module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700327 if (module_data.dev_audio < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800328 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
329 return -ENODEV;
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700333 if (module_data.dev_mixer < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800334 printk(KERN_ERR "hostmixer: couldn't register mixer "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 "device!\n");
Jeff Diked471c0f2007-02-10 01:44:00 -0800336 unregister_sound_dsp(module_data.dev_audio);
337 return -ENODEV;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Jeff Diked471c0f2007-02-10 01:44:00 -0800340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343static void __exit hostaudio_cleanup_module (void)
344{
Jeff Diked471c0f2007-02-10 01:44:00 -0800345 unregister_sound_mixer(module_data.dev_mixer);
346 unregister_sound_dsp(module_data.dev_audio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349module_init(hostaudio_init_module);
350module_exit(hostaudio_cleanup_module);