1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2002 Steve Schmidtke
4 */
5
6 #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>
12 #include <linux/uaccess.h>
13 #include <init.h>
14 #include <os.h>
15
16 struct hostaudio_state {
17 int fd;
18 };
19
20 struct hostmixer_state {
21 int fd;
22 };
23
24 #define HOSTAUDIO_DEV_DSP "/dev/sound/dsp"
25 #define HOSTAUDIO_DEV_MIXER "/dev/sound/mixer"
26
27 /*
28 * Changed either at boot time or module load time. At boot, this is
29 * single-threaded; at module load, multiple modules would each have
30 * their own copy of these variables.
31 */
32 static char *dsp = HOSTAUDIO_DEV_DSP;
33 static char *mixer = HOSTAUDIO_DEV_MIXER;
34
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 module_param(dsp, charp, 0644);
44 MODULE_PARM_DESC(dsp, DSP_HELP);
45 module_param(mixer, charp, 0644);
46 MODULE_PARM_DESC(mixer, MIXER_HELP);
47
48 #ifndef MODULE
set_dsp(char * name,int * add)49 static int set_dsp(char *name, int *add)
50 {
51 dsp = name;
52 return 0;
53 }
54
55 __uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);
56
set_mixer(char * name,int * add)57 static int set_mixer(char *name, int *add)
58 {
59 mixer = name;
60 return 0;
61 }
62
63 __uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);
64 #endif
65
66 static DEFINE_MUTEX(hostaudio_mutex);
67
68 /* /dev/dsp file operations */
69
hostaudio_read(struct file * file,char __user * buffer,size_t count,loff_t * ppos)70 static ssize_t hostaudio_read(struct file *file, char __user *buffer,
71 size_t count, loff_t *ppos)
72 {
73 struct hostaudio_state *state = file->private_data;
74 void *kbuf;
75 int err;
76
77 #ifdef DEBUG
78 printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
79 #endif
80
81 kbuf = kmalloc(count, GFP_KERNEL);
82 if (kbuf == NULL)
83 return -ENOMEM;
84
85 err = os_read_file(state->fd, kbuf, count);
86 if (err < 0)
87 goto out;
88
89 if (copy_to_user(buffer, kbuf, err))
90 err = -EFAULT;
91
92 out:
93 kfree(kbuf);
94 return err;
95 }
96
hostaudio_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)97 static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
98 size_t count, loff_t *ppos)
99 {
100 struct hostaudio_state *state = file->private_data;
101 void *kbuf;
102 int err;
103
104 #ifdef DEBUG
105 printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
106 #endif
107
108 kbuf = memdup_user(buffer, count);
109 if (IS_ERR(kbuf))
110 return PTR_ERR(kbuf);
111
112 err = os_write_file(state->fd, kbuf, count);
113 if (err < 0)
114 goto out;
115 *ppos += err;
116
117 out:
118 kfree(kbuf);
119 return err;
120 }
121
hostaudio_poll(struct file * file,struct poll_table_struct * wait)122 static __poll_t hostaudio_poll(struct file *file,
123 struct poll_table_struct *wait)
124 {
125 #ifdef DEBUG
126 printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
127 #endif
128
129 return 0;
130 }
131
hostaudio_ioctl(struct file * file,unsigned int cmd,unsigned long arg)132 static long hostaudio_ioctl(struct file *file,
133 unsigned int cmd, unsigned long arg)
134 {
135 struct hostaudio_state *state = file->private_data;
136 unsigned long data = 0;
137 int err;
138
139 #ifdef DEBUG
140 printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
141 #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:
149 if (get_user(data, (int __user *) arg))
150 return -EFAULT;
151 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:
165 if (put_user(data, (int __user *) arg))
166 return -EFAULT;
167 break;
168 default:
169 break;
170 }
171
172 return err;
173 }
174
hostaudio_open(struct inode * inode,struct file * file)175 static int hostaudio_open(struct inode *inode, struct file *file)
176 {
177 struct hostaudio_state *state;
178 int r = 0, w = 0;
179 int ret;
180
181 #ifdef DEBUG
182 kernel_param_lock(THIS_MODULE);
183 printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
184 kernel_param_unlock(THIS_MODULE);
185 #endif
186
187 state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
188 if (state == NULL)
189 return -ENOMEM;
190
191 if (file->f_mode & FMODE_READ)
192 r = 1;
193 if (file->f_mode & FMODE_WRITE)
194 w = 1;
195
196 kernel_param_lock(THIS_MODULE);
197 mutex_lock(&hostaudio_mutex);
198 ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
199 mutex_unlock(&hostaudio_mutex);
200 kernel_param_unlock(THIS_MODULE);
201
202 if (ret < 0) {
203 kfree(state);
204 return ret;
205 }
206 state->fd = ret;
207 file->private_data = state;
208 return 0;
209 }
210
hostaudio_release(struct inode * inode,struct file * file)211 static int hostaudio_release(struct inode *inode, struct file *file)
212 {
213 struct hostaudio_state *state = file->private_data;
214
215 #ifdef DEBUG
216 printk(KERN_DEBUG "hostaudio: release called\n");
217 #endif
218 os_close_file(state->fd);
219 kfree(state);
220
221 return 0;
222 }
223
224 /* /dev/mixer file operations */
225
hostmixer_ioctl_mixdev(struct file * file,unsigned int cmd,unsigned long arg)226 static long hostmixer_ioctl_mixdev(struct file *file,
227 unsigned int cmd, unsigned long arg)
228 {
229 struct hostmixer_state *state = file->private_data;
230
231 #ifdef DEBUG
232 printk(KERN_DEBUG "hostmixer: ioctl called\n");
233 #endif
234
235 return os_ioctl_generic(state->fd, cmd, arg);
236 }
237
hostmixer_open_mixdev(struct inode * inode,struct file * file)238 static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
239 {
240 struct hostmixer_state *state;
241 int r = 0, w = 0;
242 int ret;
243
244 #ifdef DEBUG
245 printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
246 #endif
247
248 state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
249 if (state == NULL)
250 return -ENOMEM;
251
252 if (file->f_mode & FMODE_READ)
253 r = 1;
254 if (file->f_mode & FMODE_WRITE)
255 w = 1;
256
257 kernel_param_lock(THIS_MODULE);
258 mutex_lock(&hostaudio_mutex);
259 ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
260 mutex_unlock(&hostaudio_mutex);
261 kernel_param_unlock(THIS_MODULE);
262
263 if (ret < 0) {
264 kernel_param_lock(THIS_MODULE);
265 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
266 "err = %d\n", dsp, -ret);
267 kernel_param_unlock(THIS_MODULE);
268 kfree(state);
269 return ret;
270 }
271
272 file->private_data = state;
273 return 0;
274 }
275
hostmixer_release(struct inode * inode,struct file * file)276 static int hostmixer_release(struct inode *inode, struct file *file)
277 {
278 struct hostmixer_state *state = file->private_data;
279
280 #ifdef DEBUG
281 printk(KERN_DEBUG "hostmixer: release called\n");
282 #endif
283
284 os_close_file(state->fd);
285 kfree(state);
286
287 return 0;
288 }
289
290 /* kernel module operations */
291
292 static const struct file_operations hostaudio_fops = {
293 .owner = THIS_MODULE,
294 .llseek = no_llseek,
295 .read = hostaudio_read,
296 .write = hostaudio_write,
297 .poll = hostaudio_poll,
298 .unlocked_ioctl = hostaudio_ioctl,
299 .compat_ioctl = compat_ptr_ioctl,
300 .mmap = NULL,
301 .open = hostaudio_open,
302 .release = hostaudio_release,
303 };
304
305 static const struct file_operations hostmixer_fops = {
306 .owner = THIS_MODULE,
307 .llseek = no_llseek,
308 .unlocked_ioctl = hostmixer_ioctl_mixdev,
309 .open = hostmixer_open_mixdev,
310 .release = hostmixer_release,
311 };
312
313 static struct {
314 int dev_audio;
315 int dev_mixer;
316 } module_data;
317
318 MODULE_AUTHOR("Steve Schmidtke");
319 MODULE_DESCRIPTION("UML Audio Relay");
320 MODULE_LICENSE("GPL");
321
hostaudio_init_module(void)322 static int __init hostaudio_init_module(void)
323 {
324 kernel_param_lock(THIS_MODULE);
325 printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
326 dsp, mixer);
327 kernel_param_unlock(THIS_MODULE);
328
329 module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
330 if (module_data.dev_audio < 0) {
331 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
332 return -ENODEV;
333 }
334
335 module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
336 if (module_data.dev_mixer < 0) {
337 printk(KERN_ERR "hostmixer: couldn't register mixer "
338 "device!\n");
339 unregister_sound_dsp(module_data.dev_audio);
340 return -ENODEV;
341 }
342
343 return 0;
344 }
345
hostaudio_cleanup_module(void)346 static void __exit hostaudio_cleanup_module (void)
347 {
348 unregister_sound_mixer(module_data.dev_mixer);
349 unregister_sound_dsp(module_data.dev_audio);
350 }
351
352 module_init(hostaudio_init_module);
353 module_exit(hostaudio_cleanup_module);
354