blob: 0c46e398cd8f313d89a3ff07187916aa6021b93f [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
Rusty Russelld6d1b652010-08-11 23:04:27 -0600190 kparam_block_sysfs_write(dsp);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700191 printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600192 kparam_unblock_sysfs_write(dsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#endif
194
Jeff Diked471c0f2007-02-10 01:44:00 -0800195 state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700196 if (state == NULL)
197 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Jeff Dikecb8fa612007-10-16 01:27:34 -0700199 if (file->f_mode & FMODE_READ)
200 r = 1;
201 if (file->f_mode & FMODE_WRITE)
202 w = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Rusty Russelld6d1b652010-08-11 23:04:27 -0600204 kparam_block_sysfs_write(dsp);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200205 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200207 unlock_kernel();
Rusty Russelld6d1b652010-08-11 23:04:27 -0600208 kparam_unblock_sysfs_write(dsp);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200209
Jeff Dikecb8fa612007-10-16 01:27:34 -0700210 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 kfree(state);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700212 return ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 state->fd = ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800215 file->private_data = state;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219static int hostaudio_release(struct inode *inode, struct file *file)
220{
Jeff Diked471c0f2007-02-10 01:44:00 -0800221 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700224 printk(KERN_DEBUG "hostaudio: release called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#endif
Jeff Diked471c0f2007-02-10 01:44:00 -0800226 os_close_file(state->fd);
227 kfree(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Jeff Dikecb8fa612007-10-16 01:27:34 -0700229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232/* /dev/mixer file operations */
233
John Kacurd6c89d92010-05-07 17:34:28 +0200234static long hostmixer_ioctl_mixdev(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 unsigned int cmd, unsigned long arg)
236{
Jeff Diked471c0f2007-02-10 01:44:00 -0800237 struct hostmixer_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700240 printk(KERN_DEBUG "hostmixer: ioctl called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241#endif
242
Jeff Dikecb8fa612007-10-16 01:27:34 -0700243 return os_ioctl_generic(state->fd, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
247{
Jeff Diked471c0f2007-02-10 01:44:00 -0800248 struct hostmixer_state *state;
249 int r = 0, w = 0;
250 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700253 printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254#endif
255
Jeff Diked471c0f2007-02-10 01:44:00 -0800256 state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700257 if (state == NULL)
258 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Jeff Dikecb8fa612007-10-16 01:27:34 -0700260 if (file->f_mode & FMODE_READ)
261 r = 1;
262 if (file->f_mode & FMODE_WRITE)
263 w = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Rusty Russelld6d1b652010-08-11 23:04:27 -0600265 kparam_block_sysfs_write(mixer);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200266 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200268 unlock_kernel();
Rusty Russelld6d1b652010-08-11 23:04:27 -0600269 kparam_unblock_sysfs_write(mixer);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700270
271 if (ret < 0) {
Rusty Russelld6d1b652010-08-11 23:04:27 -0600272 kparam_block_sysfs_write(dsp);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700273 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
274 "err = %d\n", dsp, -ret);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600275 kparam_unblock_sysfs_write(dsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 kfree(state);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700277 return ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Jeff Diked471c0f2007-02-10 01:44:00 -0800280 file->private_data = state;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284static int hostmixer_release(struct inode *inode, struct file *file)
285{
Jeff Diked471c0f2007-02-10 01:44:00 -0800286 struct hostmixer_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700289 printk(KERN_DEBUG "hostmixer: release called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290#endif
291
Jeff Diked471c0f2007-02-10 01:44:00 -0800292 os_close_file(state->fd);
293 kfree(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Jeff Dikecb8fa612007-10-16 01:27:34 -0700295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/* kernel module operations */
299
Jeff Dike5e7672e2006-09-27 01:50:33 -0700300static const struct file_operations hostaudio_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800301 .owner = THIS_MODULE,
302 .llseek = no_llseek,
303 .read = hostaudio_read,
304 .write = hostaudio_write,
305 .poll = hostaudio_poll,
John Kacurd6c89d92010-05-07 17:34:28 +0200306 .unlocked_ioctl = hostaudio_ioctl,
Jeff Diked471c0f2007-02-10 01:44:00 -0800307 .mmap = NULL,
308 .open = hostaudio_open,
309 .release = hostaudio_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
Jeff Dike5e7672e2006-09-27 01:50:33 -0700312static const struct file_operations hostmixer_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800313 .owner = THIS_MODULE,
314 .llseek = no_llseek,
John Kacurd6c89d92010-05-07 17:34:28 +0200315 .unlocked_ioctl = hostmixer_ioctl_mixdev,
Jeff Diked471c0f2007-02-10 01:44:00 -0800316 .open = hostmixer_open_mixdev,
317 .release = hostmixer_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318};
319
320struct {
321 int dev_audio;
322 int dev_mixer;
323} module_data;
324
325MODULE_AUTHOR("Steve Schmidtke");
326MODULE_DESCRIPTION("UML Audio Relay");
327MODULE_LICENSE("GPL");
328
329static int __init hostaudio_init_module(void)
330{
Rusty Russelld6d1b652010-08-11 23:04:27 -0600331 __kernel_param_lock();
Jeff Diked471c0f2007-02-10 01:44:00 -0800332 printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 dsp, mixer);
Rusty Russelld6d1b652010-08-11 23:04:27 -0600334 __kernel_param_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700337 if (module_data.dev_audio < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800338 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
339 return -ENODEV;
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700343 if (module_data.dev_mixer < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800344 printk(KERN_ERR "hostmixer: couldn't register mixer "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 "device!\n");
Jeff Diked471c0f2007-02-10 01:44:00 -0800346 unregister_sound_dsp(module_data.dev_audio);
347 return -ENODEV;
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Jeff Diked471c0f2007-02-10 01:44:00 -0800350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353static void __exit hostaudio_cleanup_module (void)
354{
Jeff Diked471c0f2007-02-10 01:44:00 -0800355 unregister_sound_mixer(module_data.dev_mixer);
356 unregister_sound_dsp(module_data.dev_audio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359module_init(hostaudio_init_module);
360module_exit(hostaudio_cleanup_module);