blob: 5b064d360cb7495a6392edee68d9d4f5b5572854 [file] [log] [blame]
Alex Dewardbddf422019-08-25 10:49:16 +01001// SPDX-License-Identifier: GPL-2.0
Jeff Dikecb8fa612007-10-16 01:27:34 -07002/*
3 * Copyright (C) 2002 Steve Schmidtke
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
Al Viro37185b32012-10-08 03:27:32 +01006#include <linux/fs.h>
7#include <linux/module.h>
8#include <linux/slab.h>
9#include <linux/sound.h>
10#include <linux/soundcard.h>
11#include <linux/mutex.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080012#include <linux/uaccess.h>
Al Viro37185b32012-10-08 03:27:32 +010013#include <init.h>
14#include <os.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
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
Arnd Bergmann9a181c52010-09-11 18:38:03 +020066static DEFINE_MUTEX(hostaudio_mutex);
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* /dev/dsp file operations */
69
Al Viro4d338e12006-03-31 02:30:15 -080070static ssize_t hostaudio_read(struct file *file, char __user *buffer,
71 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Jeff Diked471c0f2007-02-10 01:44:00 -080073 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 void *kbuf;
75 int err;
76
77#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -070078 printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#endif
80
81 kbuf = kmalloc(count, GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -070082 if (kbuf == NULL)
83 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Jeff Dikea6ea4cc2007-05-06 14:51:43 -070085 err = os_read_file(state->fd, kbuf, count);
Jeff Dikecb8fa612007-10-16 01:27:34 -070086 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 goto out;
88
Jeff Dikecb8fa612007-10-16 01:27:34 -070089 if (copy_to_user(buffer, kbuf, err))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 err = -EFAULT;
91
Jeff Diked471c0f2007-02-10 01:44:00 -080092out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 kfree(kbuf);
Jeff Dikecb8fa612007-10-16 01:27:34 -070094 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Al Viro4d338e12006-03-31 02:30:15 -080097static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 size_t count, loff_t *ppos)
99{
Jeff Diked471c0f2007-02-10 01:44:00 -0800100 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 void *kbuf;
102 int err;
103
104#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700105 printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#endif
107
Al Viro1ceb3622016-01-02 14:50:51 -0500108 kbuf = memdup_user(buffer, count);
109 if (IS_ERR(kbuf))
110 return PTR_ERR(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700112 err = os_write_file(state->fd, kbuf, count);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700113 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 goto out;
115 *ppos += err;
116
117 out:
118 kfree(kbuf);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700119 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Al Viro76d2d4a2017-07-02 22:59:49 -0400122static __poll_t hostaudio_poll(struct file *file,
123 struct poll_table_struct *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700126 printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#endif
128
Yang Liea8e8962021-03-10 16:49:08 +0800129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
John Kacurd6c89d92010-05-07 17:34:28 +0200132static long hostaudio_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 unsigned int cmd, unsigned long arg)
134{
Jeff Diked471c0f2007-02-10 01:44:00 -0800135 struct hostaudio_state *state = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned long data = 0;
137 int err;
138
139#ifdef DEBUG
Jeff Dikecb8fa612007-10-16 01:27:34 -0700140 printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#endif
142 switch(cmd){
143 case SNDCTL_DSP_SPEED:
144 case SNDCTL_DSP_STEREO:
145 case SNDCTL_DSP_GETBLKSIZE:
146 case SNDCTL_DSP_CHANNELS:
147 case SNDCTL_DSP_SUBDIVIDE:
148 case SNDCTL_DSP_SETFRAGMENT:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700149 if (get_user(data, (int __user *) arg))
Johann Felix Soden484f1e22008-05-12 14:01:51 -0700150 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 break;
152 default:
153 break;
154 }
155
156 err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
157
158 switch(cmd){
159 case SNDCTL_DSP_SPEED:
160 case SNDCTL_DSP_STEREO:
161 case SNDCTL_DSP_GETBLKSIZE:
162 case SNDCTL_DSP_CHANNELS:
163 case SNDCTL_DSP_SUBDIVIDE:
164 case SNDCTL_DSP_SETFRAGMENT:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700165 if (put_user(data, (int __user *) arg))
166 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
168 default:
169 break;
170 }
171
Jeff Dikecb8fa612007-10-16 01:27:34 -0700172 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175static int hostaudio_open(struct inode *inode, struct file *file)
176{
Jeff Diked471c0f2007-02-10 01:44:00 -0800177 struct hostaudio_state *state;
178 int r = 0, w = 0;
179 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181#ifdef DEBUG
Dan Streetmanb51d23e42015-06-17 06:18:52 +0930182 kernel_param_lock(THIS_MODULE);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700183 printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
Dan Streetmanb51d23e42015-06-17 06:18:52 +0930184 kernel_param_unlock(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#endif
186
Jeff Diked471c0f2007-02-10 01:44:00 -0800187 state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700188 if (state == NULL)
189 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Jeff Dikecb8fa612007-10-16 01:27:34 -0700191 if (file->f_mode & FMODE_READ)
192 r = 1;
193 if (file->f_mode & FMODE_WRITE)
194 w = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Dan Streetmanb51d23e42015-06-17 06:18:52 +0930196 kernel_param_lock(THIS_MODULE);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200197 mutex_lock(&hostaudio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200199 mutex_unlock(&hostaudio_mutex);
Dan Streetmanb51d23e42015-06-17 06:18:52 +0930200 kernel_param_unlock(THIS_MODULE);
Arnd Bergmann90dc7632010-07-11 12:16:36 +0200201
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
John Kacurd6c89d92010-05-07 17:34:28 +0200226static long hostmixer_ioctl_mixdev(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
Dan Streetmanb51d23e42015-06-17 06:18:52 +0930257 kernel_param_lock(THIS_MODULE);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200258 mutex_lock(&hostaudio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200260 mutex_unlock(&hostaudio_mutex);
Dan Streetmanb51d23e42015-06-17 06:18:52 +0930261 kernel_param_unlock(THIS_MODULE);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700262
263 if (ret < 0) {
Dan Streetmanb51d23e42015-06-17 06:18:52 +0930264 kernel_param_lock(THIS_MODULE);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700265 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
266 "err = %d\n", dsp, -ret);
Dan Streetmanb51d23e42015-06-17 06:18:52 +0930267 kernel_param_unlock(THIS_MODULE);
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,
Arnd Bergmann2022ca02018-09-06 22:54:38 +0200299 .compat_ioctl = compat_ptr_ioctl,
Jeff Diked471c0f2007-02-10 01:44:00 -0800300 .mmap = NULL,
301 .open = hostaudio_open,
302 .release = hostaudio_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303};
304
Jeff Dike5e7672e2006-09-27 01:50:33 -0700305static const struct file_operations hostmixer_fops = {
Jeff Diked471c0f2007-02-10 01:44:00 -0800306 .owner = THIS_MODULE,
307 .llseek = no_llseek,
John Kacurd6c89d92010-05-07 17:34:28 +0200308 .unlocked_ioctl = hostmixer_ioctl_mixdev,
Jeff Diked471c0f2007-02-10 01:44:00 -0800309 .open = hostmixer_open_mixdev,
310 .release = hostmixer_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
313struct {
314 int dev_audio;
315 int dev_mixer;
316} module_data;
317
318MODULE_AUTHOR("Steve Schmidtke");
319MODULE_DESCRIPTION("UML Audio Relay");
320MODULE_LICENSE("GPL");
321
322static int __init hostaudio_init_module(void)
323{
Dan Streetmanb51d23e42015-06-17 06:18:52 +0930324 kernel_param_lock(THIS_MODULE);
Jeff Diked471c0f2007-02-10 01:44:00 -0800325 printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 dsp, mixer);
Dan Streetmanb51d23e42015-06-17 06:18:52 +0930327 kernel_param_unlock(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700330 if (module_data.dev_audio < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800331 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
332 return -ENODEV;
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700336 if (module_data.dev_mixer < 0) {
Jeff Diked471c0f2007-02-10 01:44:00 -0800337 printk(KERN_ERR "hostmixer: couldn't register mixer "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 "device!\n");
Jeff Diked471c0f2007-02-10 01:44:00 -0800339 unregister_sound_dsp(module_data.dev_audio);
340 return -ENODEV;
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Jeff Diked471c0f2007-02-10 01:44:00 -0800343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346static void __exit hostaudio_cleanup_module (void)
347{
Jeff Diked471c0f2007-02-10 01:44:00 -0800348 unregister_sound_mixer(module_data.dev_mixer);
349 unregister_sound_dsp(module_data.dev_audio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352module_init(hostaudio_init_module);
353module_exit(hostaudio_cleanup_module);