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