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