Merge git://git.kernel.org/pub/scm/linux/kernel/git/steve/gfs2-2.6-nmw
[pandora-kernel.git] / arch / um / drivers / hostaudio_kern.c
1 /*
2  * Copyright (C) 2002 Steve Schmidtke
3  * Licensed under the GPL
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/smp_lock.h"
12 #include "asm/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
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
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 /* /dev/dsp file operations */
67
68 static ssize_t hostaudio_read(struct file *file, char __user *buffer,
69                               size_t count, loff_t *ppos)
70 {
71         struct hostaudio_state *state = file->private_data;
72         void *kbuf;
73         int err;
74
75 #ifdef DEBUG
76         printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
77 #endif
78
79         kbuf = kmalloc(count, GFP_KERNEL);
80         if (kbuf == NULL)
81                 return -ENOMEM;
82
83         err = os_read_file(state->fd, kbuf, count);
84         if (err < 0)
85                 goto out;
86
87         if (copy_to_user(buffer, kbuf, err))
88                 err = -EFAULT;
89
90 out:
91         kfree(kbuf);
92         return err;
93 }
94
95 static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
96                                size_t count, loff_t *ppos)
97 {
98         struct hostaudio_state *state = file->private_data;
99         void *kbuf;
100         int err;
101
102 #ifdef DEBUG
103         printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
104 #endif
105
106         kbuf = kmalloc(count, GFP_KERNEL);
107         if (kbuf == NULL)
108                 return -ENOMEM;
109
110         err = -EFAULT;
111         if (copy_from_user(kbuf, buffer, count))
112                 goto out;
113
114         err = os_write_file(state->fd, kbuf, count);
115         if (err < 0)
116                 goto out;
117         *ppos += err;
118
119  out:
120         kfree(kbuf);
121         return err;
122 }
123
124 static unsigned int hostaudio_poll(struct file *file,
125                                    struct poll_table_struct *wait)
126 {
127         unsigned int mask = 0;
128
129 #ifdef DEBUG
130         printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
131 #endif
132
133         return mask;
134 }
135
136 static long hostaudio_ioctl(struct file *file,
137                            unsigned int cmd, unsigned long arg)
138 {
139         struct hostaudio_state *state = file->private_data;
140         unsigned long data = 0;
141         int err;
142
143 #ifdef DEBUG
144         printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
145 #endif
146         switch(cmd){
147         case SNDCTL_DSP_SPEED:
148         case SNDCTL_DSP_STEREO:
149         case SNDCTL_DSP_GETBLKSIZE:
150         case SNDCTL_DSP_CHANNELS:
151         case SNDCTL_DSP_SUBDIVIDE:
152         case SNDCTL_DSP_SETFRAGMENT:
153                 if (get_user(data, (int __user *) arg))
154                         return -EFAULT;
155                 break;
156         default:
157                 break;
158         }
159
160         err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
161
162         switch(cmd){
163         case SNDCTL_DSP_SPEED:
164         case SNDCTL_DSP_STEREO:
165         case SNDCTL_DSP_GETBLKSIZE:
166         case SNDCTL_DSP_CHANNELS:
167         case SNDCTL_DSP_SUBDIVIDE:
168         case SNDCTL_DSP_SETFRAGMENT:
169                 if (put_user(data, (int __user *) arg))
170                         return -EFAULT;
171                 break;
172         default:
173                 break;
174         }
175
176         return err;
177 }
178
179 static int hostaudio_open(struct inode *inode, struct file *file)
180 {
181         struct hostaudio_state *state;
182         int r = 0, w = 0;
183         int ret;
184
185 #ifdef DEBUG
186         kparam_block_sysfs_write(dsp);
187         printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
188         kparam_unblock_sysfs_write(dsp);
189 #endif
190
191         state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
192         if (state == NULL)
193                 return -ENOMEM;
194
195         if (file->f_mode & FMODE_READ)
196                 r = 1;
197         if (file->f_mode & FMODE_WRITE)
198                 w = 1;
199
200         kparam_block_sysfs_write(dsp);
201         lock_kernel();
202         ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
203         unlock_kernel();
204         kparam_unblock_sysfs_write(dsp);
205
206         if (ret < 0) {
207                 kfree(state);
208                 return ret;
209         }
210         state->fd = ret;
211         file->private_data = state;
212         return 0;
213 }
214
215 static int hostaudio_release(struct inode *inode, struct file *file)
216 {
217         struct hostaudio_state *state = file->private_data;
218
219 #ifdef DEBUG
220         printk(KERN_DEBUG "hostaudio: release called\n");
221 #endif
222         os_close_file(state->fd);
223         kfree(state);
224
225         return 0;
226 }
227
228 /* /dev/mixer file operations */
229
230 static long hostmixer_ioctl_mixdev(struct file *file,
231                                   unsigned int cmd, unsigned long arg)
232 {
233         struct hostmixer_state *state = file->private_data;
234
235 #ifdef DEBUG
236         printk(KERN_DEBUG "hostmixer: ioctl called\n");
237 #endif
238
239         return os_ioctl_generic(state->fd, cmd, arg);
240 }
241
242 static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
243 {
244         struct hostmixer_state *state;
245         int r = 0, w = 0;
246         int ret;
247
248 #ifdef DEBUG
249         printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
250 #endif
251
252         state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
253         if (state == NULL)
254                 return -ENOMEM;
255
256         if (file->f_mode & FMODE_READ)
257                 r = 1;
258         if (file->f_mode & FMODE_WRITE)
259                 w = 1;
260
261         kparam_block_sysfs_write(mixer);
262         lock_kernel();
263         ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
264         unlock_kernel();
265         kparam_unblock_sysfs_write(mixer);
266
267         if (ret < 0) {
268                 kparam_block_sysfs_write(dsp);
269                 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
270                        "err = %d\n", dsp, -ret);
271                 kparam_unblock_sysfs_write(dsp);
272                 kfree(state);
273                 return ret;
274         }
275
276         file->private_data = state;
277         return 0;
278 }
279
280 static int hostmixer_release(struct inode *inode, struct file *file)
281 {
282         struct hostmixer_state *state = file->private_data;
283
284 #ifdef DEBUG
285         printk(KERN_DEBUG "hostmixer: release called\n");
286 #endif
287
288         os_close_file(state->fd);
289         kfree(state);
290
291         return 0;
292 }
293
294 /* kernel module operations */
295
296 static const struct file_operations hostaudio_fops = {
297         .owner          = THIS_MODULE,
298         .llseek         = no_llseek,
299         .read           = hostaudio_read,
300         .write          = hostaudio_write,
301         .poll           = hostaudio_poll,
302         .unlocked_ioctl = hostaudio_ioctl,
303         .mmap           = NULL,
304         .open           = hostaudio_open,
305         .release        = hostaudio_release,
306 };
307
308 static const struct file_operations hostmixer_fops = {
309         .owner          = THIS_MODULE,
310         .llseek         = no_llseek,
311         .unlocked_ioctl = hostmixer_ioctl_mixdev,
312         .open           = hostmixer_open_mixdev,
313         .release        = hostmixer_release,
314 };
315
316 struct {
317         int dev_audio;
318         int dev_mixer;
319 } module_data;
320
321 MODULE_AUTHOR("Steve Schmidtke");
322 MODULE_DESCRIPTION("UML Audio Relay");
323 MODULE_LICENSE("GPL");
324
325 static int __init hostaudio_init_module(void)
326 {
327         __kernel_param_lock();
328         printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
329                dsp, mixer);
330         __kernel_param_unlock();
331
332         module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
333         if (module_data.dev_audio < 0) {
334                 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
335                 return -ENODEV;
336         }
337
338         module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
339         if (module_data.dev_mixer < 0) {
340                 printk(KERN_ERR "hostmixer: couldn't register mixer "
341                        "device!\n");
342                 unregister_sound_dsp(module_data.dev_audio);
343                 return -ENODEV;
344         }
345
346         return 0;
347 }
348
349 static void __exit hostaudio_cleanup_module (void)
350 {
351         unregister_sound_mixer(module_data.dev_mixer);
352         unregister_sound_dsp(module_data.dev_audio);
353 }
354
355 module_init(hostaudio_init_module);
356 module_exit(hostaudio_cleanup_module);