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