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