blob: ff1b22b69e9c73fb56f842ba367c814c423508af [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"
11#include "asm/uaccess.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "init.h"
13#include "os.h"
14
15struct hostaudio_state {
Jeff Diked471c0f2007-02-10 01:44:00 -080016 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017};
18
19struct hostmixer_state {
Jeff Diked471c0f2007-02-10 01:44:00 -080020 int fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021};
22
23#define HOSTAUDIO_DEV_DSP "/dev/sound/dsp"
24#define HOSTAUDIO_DEV_MIXER "/dev/sound/mixer"
25
Jeff Dikecb8fa612007-10-16 01:27:34 -070026/*
27 * Changed either at boot time or module load time. At boot, this is
Jeff Dikeb612e472007-02-10 01:43:59 -080028 * single-threaded; at module load, multiple modules would each have
29 * their own copy of these variables.
30 */
31static char *dsp = HOSTAUDIO_DEV_DSP;
32static char *mixer = HOSTAUDIO_DEV_MIXER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#define DSP_HELP \
35" This is used to specify the host dsp device to the hostaudio driver.\n" \
36" The default is \"" HOSTAUDIO_DEV_DSP "\".\n\n"
37
38#define MIXER_HELP \
39" This is used to specify the host mixer device to the hostaudio driver.\n"\
40" The default is \"" HOSTAUDIO_DEV_MIXER "\".\n\n"
41
42#ifndef MODULE
43static int set_dsp(char *name, int *add)
44{
45 dsp = name;
Jeff Dikecb8fa612007-10-16 01:27:34 -070046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
49__uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);
50
51static int set_mixer(char *name, int *add)
52{
53 mixer = name;
Jeff Dikecb8fa612007-10-16 01:27:34 -070054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
57__uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);
58
59#else /*MODULE*/
60
Dominik Hackl20a64f12005-07-27 11:43:32 -070061module_param(dsp, charp, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062MODULE_PARM_DESC(dsp, DSP_HELP);
63
Dominik Hackl20a64f12005-07-27 11:43:32 -070064module_param(mixer, charp, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065MODULE_PARM_DESC(mixer, MIXER_HELP);
66
67#endif
68
69/* /dev/dsp file operations */
70
Al Viro4d338e12006-03-31 02:30:15 -080071static ssize_t hostaudio_read(struct file *file, char __user *buffer,
72 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Jeff Diked471c0f2007-02-10 01:44:00 -080074 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 void *kbuf;
76 int err;
77
78#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -070079 printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#endif
81
82 kbuf = kmalloc(count, GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -070083 if (kbuf == NULL)
84 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Jeff Dikea6ea4cc2007-05-06 14:51:43 -070086 err = os_read_file(state->fd, kbuf, count);
Jeff Dikecb8fa612007-10-16 01:27:34 -070087 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 goto out;
89
Jeff Dikecb8fa612007-10-16 01:27:34 -070090 if (copy_to_user(buffer, kbuf, err))
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 err = -EFAULT;
92
Jeff Diked471c0f2007-02-10 01:44:00 -080093out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 kfree(kbuf);
Jeff Dikecb8fa612007-10-16 01:27:34 -070095 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Al Viro4d338e12006-03-31 02:30:15 -080098static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 size_t count, loff_t *ppos)
100{
Jeff Diked471c0f2007-02-10 01:44:00 -0800101 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 void *kbuf;
103 int err;
104
105#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700106 printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#endif
108
109 kbuf = kmalloc(count, GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700110 if (kbuf == NULL)
111 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 err = -EFAULT;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700114 if (copy_from_user(kbuf, buffer, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 goto out;
116
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700117 err = os_write_file(state->fd, kbuf, count);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700118 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 goto out;
120 *ppos += err;
121
122 out:
123 kfree(kbuf);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700124 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Jeff Dikecb8fa612007-10-16 01:27:34 -0700127static unsigned int hostaudio_poll(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 struct poll_table_struct *wait)
129{
Jeff Diked471c0f2007-02-10 01:44:00 -0800130 unsigned int mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700133 printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#endif
135
Jeff Dikecb8fa612007-10-16 01:27:34 -0700136 return mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Jeff Dikecb8fa612007-10-16 01:27:34 -0700139static int hostaudio_ioctl(struct inode *inode, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 unsigned int cmd, unsigned long arg)
141{
Jeff Diked471c0f2007-02-10 01:44:00 -0800142 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 unsigned long data = 0;
144 int err;
145
146#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700147 printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#endif
149 switch(cmd){
150 case SNDCTL_DSP_SPEED:
151 case SNDCTL_DSP_STEREO:
152 case SNDCTL_DSP_GETBLKSIZE:
153 case SNDCTL_DSP_CHANNELS:
154 case SNDCTL_DSP_SUBDIVIDE:
155 case SNDCTL_DSP_SETFRAGMENT:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700156 if (get_user(data, (int __user *) arg))
157 return EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 break;
159 default:
160 break;
161 }
162
163 err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
164
165 switch(cmd){
166 case SNDCTL_DSP_SPEED:
167 case SNDCTL_DSP_STEREO:
168 case SNDCTL_DSP_GETBLKSIZE:
169 case SNDCTL_DSP_CHANNELS:
170 case SNDCTL_DSP_SUBDIVIDE:
171 case SNDCTL_DSP_SETFRAGMENT:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700172 if (put_user(data, (int __user *) arg))
173 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 break;
175 default:
176 break;
177 }
178
Jeff Dikecb8fa612007-10-16 01:27:34 -0700179 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182static int hostaudio_open(struct inode *inode, struct file *file)
183{
Jeff Diked471c0f2007-02-10 01:44:00 -0800184 struct hostaudio_state *state;
185 int r = 0, w = 0;
186 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700189 printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#endif
191
Jeff Diked471c0f2007-02-10 01:44:00 -0800192 state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700193 if (state == NULL)
194 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Jeff Dikecb8fa612007-10-16 01:27:34 -0700196 if (file->f_mode & FMODE_READ)
197 r = 1;
198 if (file->f_mode & FMODE_WRITE)
199 w = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700202 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 kfree(state);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700204 return ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 state->fd = ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800207 file->private_data = state;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211static int hostaudio_release(struct inode *inode, struct file *file)
212{
Jeff Diked471c0f2007-02-10 01:44:00 -0800213 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700216 printk(KERN_DEBUG "hostaudio: release called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#endif
Jeff Diked471c0f2007-02-10 01:44:00 -0800218 os_close_file(state->fd);
219 kfree(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Jeff Dikecb8fa612007-10-16 01:27:34 -0700221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/* /dev/mixer file operations */
225
Jeff Dikecb8fa612007-10-16 01:27:34 -0700226static int hostmixer_ioctl_mixdev(struct inode *inode, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 unsigned int cmd, unsigned long arg)
228{
Jeff Diked471c0f2007-02-10 01:44:00 -0800229 struct hostmixer_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700232 printk(KERN_DEBUG "hostmixer: ioctl called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#endif
234
Jeff Dikecb8fa612007-10-16 01:27:34 -0700235 return os_ioctl_generic(state->fd, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
239{
Jeff Diked471c0f2007-02-10 01:44:00 -0800240 struct hostmixer_state *state;
241 int r = 0, w = 0;
242 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700245 printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#endif
247
Jeff Diked471c0f2007-02-10 01:44:00 -0800248 state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700249 if (state == NULL)
250 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Jeff Dikecb8fa612007-10-16 01:27:34 -0700252 if (file->f_mode & FMODE_READ)
253 r = 1;
254 if (file->f_mode & FMODE_WRITE)
255 w = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700258
259 if (ret < 0) {
260 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
261 "err = %d\n", dsp, -ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 kfree(state);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700263 return ret;
Jeff Diked471c0f2007-02-10 01:44:00 -0800264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Jeff Diked471c0f2007-02-10 01:44:00 -0800266 file->private_data = state;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270static int hostmixer_release(struct inode *inode, struct file *file)
271{
Jeff Diked471c0f2007-02-10 01:44:00 -0800272 struct hostmixer_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700275 printk(KERN_DEBUG "hostmixer: release called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#endif
277
Jeff Diked471c0f2007-02-10 01:44:00 -0800278 os_close_file(state->fd);
279 kfree(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Jeff Dikecb8fa612007-10-16 01:27:34 -0700281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/* kernel module operations */
285
Jeff Dike5e7672e2006-09-27 01:50:33 -0700286static const struct file_operations hostaudio_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800287 .owner = THIS_MODULE,
288 .llseek = no_llseek,
289 .read = hostaudio_read,
290 .write = hostaudio_write,
291 .poll = hostaudio_poll,
292 .ioctl = hostaudio_ioctl,
293 .mmap = NULL,
294 .open = hostaudio_open,
295 .release = hostaudio_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296};
297
Jeff Dike5e7672e2006-09-27 01:50:33 -0700298static const struct file_operations hostmixer_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800299 .owner = THIS_MODULE,
300 .llseek = no_llseek,
301 .ioctl = hostmixer_ioctl_mixdev,
302 .open = hostmixer_open_mixdev,
303 .release = hostmixer_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
306struct {
307 int dev_audio;
308 int dev_mixer;
309} module_data;
310
311MODULE_AUTHOR("Steve Schmidtke");
312MODULE_DESCRIPTION("UML Audio Relay");
313MODULE_LICENSE("GPL");
314
315static int __init hostaudio_init_module(void)
316{
Jeff Diked471c0f2007-02-10 01:44:00 -0800317 printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 dsp, mixer);
319
320 module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700321 if (module_data.dev_audio < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800322 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
323 return -ENODEV;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700327 if (module_data.dev_mixer < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800328 printk(KERN_ERR "hostmixer: couldn't register mixer "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 "device!\n");
Jeff Diked471c0f2007-02-10 01:44:00 -0800330 unregister_sound_dsp(module_data.dev_audio);
331 return -ENODEV;
332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Jeff Diked471c0f2007-02-10 01:44:00 -0800334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337static void __exit hostaudio_cleanup_module (void)
338{
Jeff Diked471c0f2007-02-10 01:44:00 -0800339 unregister_sound_mixer(module_data.dev_mixer);
340 unregister_sound_dsp(module_data.dev_audio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343module_init(hostaudio_init_module);
344module_exit(hostaudio_cleanup_module);