ARM: mmp: fix potential NULL dereference
[linux/fpc-iii.git] / arch / um / drivers / hostaudio_kern.c
blobf9f6a4e205901ee603d56d0167fb3c6e0807e868
1 /*
2 * Copyright (C) 2002 Steve Schmidtke
3 * Licensed under the GPL
4 */
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"
16 struct hostaudio_state {
17 int fd;
20 struct hostmixer_state {
21 int fd;
24 #define HOSTAUDIO_DEV_DSP "/dev/sound/dsp"
25 #define HOSTAUDIO_DEV_MIXER "/dev/sound/mixer"
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.
32 static char *dsp = HOSTAUDIO_DEV_DSP;
33 static char *mixer = HOSTAUDIO_DEV_MIXER;
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"
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"
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);
48 #ifndef MODULE
49 static int set_dsp(char *name, int *add)
51 dsp = name;
52 return 0;
55 __uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);
57 static int set_mixer(char *name, int *add)
59 mixer = name;
60 return 0;
63 __uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);
64 #endif
66 static DEFINE_MUTEX(hostaudio_mutex);
68 /* /dev/dsp file operations */
70 static ssize_t hostaudio_read(struct file *file, char __user *buffer,
71 size_t count, loff_t *ppos)
73 struct hostaudio_state *state = file->private_data;
74 void *kbuf;
75 int err;
77 #ifdef DEBUG
78 printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
79 #endif
81 kbuf = kmalloc(count, GFP_KERNEL);
82 if (kbuf == NULL)
83 return -ENOMEM;
85 err = os_read_file(state->fd, kbuf, count);
86 if (err < 0)
87 goto out;
89 if (copy_to_user(buffer, kbuf, err))
90 err = -EFAULT;
92 out:
93 kfree(kbuf);
94 return err;
97 static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
98 size_t count, loff_t *ppos)
100 struct hostaudio_state *state = file->private_data;
101 void *kbuf;
102 int err;
104 #ifdef DEBUG
105 printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
106 #endif
108 kbuf = kmalloc(count, GFP_KERNEL);
109 if (kbuf == NULL)
110 return -ENOMEM;
112 err = -EFAULT;
113 if (copy_from_user(kbuf, buffer, count))
114 goto out;
116 err = os_write_file(state->fd, kbuf, count);
117 if (err < 0)
118 goto out;
119 *ppos += err;
121 out:
122 kfree(kbuf);
123 return err;
126 static unsigned int hostaudio_poll(struct file *file,
127 struct poll_table_struct *wait)
129 unsigned int mask = 0;
131 #ifdef DEBUG
132 printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
133 #endif
135 return mask;
138 static long hostaudio_ioctl(struct file *file,
139 unsigned int cmd, unsigned long arg)
141 struct hostaudio_state *state = file->private_data;
142 unsigned long data = 0;
143 int err;
145 #ifdef DEBUG
146 printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
147 #endif
148 switch(cmd){
149 case SNDCTL_DSP_SPEED:
150 case SNDCTL_DSP_STEREO:
151 case SNDCTL_DSP_GETBLKSIZE:
152 case SNDCTL_DSP_CHANNELS:
153 case SNDCTL_DSP_SUBDIVIDE:
154 case SNDCTL_DSP_SETFRAGMENT:
155 if (get_user(data, (int __user *) arg))
156 return -EFAULT;
157 break;
158 default:
159 break;
162 err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
164 switch(cmd){
165 case SNDCTL_DSP_SPEED:
166 case SNDCTL_DSP_STEREO:
167 case SNDCTL_DSP_GETBLKSIZE:
168 case SNDCTL_DSP_CHANNELS:
169 case SNDCTL_DSP_SUBDIVIDE:
170 case SNDCTL_DSP_SETFRAGMENT:
171 if (put_user(data, (int __user *) arg))
172 return -EFAULT;
173 break;
174 default:
175 break;
178 return err;
181 static int hostaudio_open(struct inode *inode, struct file *file)
183 struct hostaudio_state *state;
184 int r = 0, w = 0;
185 int ret;
187 #ifdef DEBUG
188 kparam_block_sysfs_write(dsp);
189 printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
190 kparam_unblock_sysfs_write(dsp);
191 #endif
193 state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
194 if (state == NULL)
195 return -ENOMEM;
197 if (file->f_mode & FMODE_READ)
198 r = 1;
199 if (file->f_mode & FMODE_WRITE)
200 w = 1;
202 kparam_block_sysfs_write(dsp);
203 mutex_lock(&hostaudio_mutex);
204 ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
205 mutex_unlock(&hostaudio_mutex);
206 kparam_unblock_sysfs_write(dsp);
208 if (ret < 0) {
209 kfree(state);
210 return ret;
212 state->fd = ret;
213 file->private_data = state;
214 return 0;
217 static int hostaudio_release(struct inode *inode, struct file *file)
219 struct hostaudio_state *state = file->private_data;
221 #ifdef DEBUG
222 printk(KERN_DEBUG "hostaudio: release called\n");
223 #endif
224 os_close_file(state->fd);
225 kfree(state);
227 return 0;
230 /* /dev/mixer file operations */
232 static long hostmixer_ioctl_mixdev(struct file *file,
233 unsigned int cmd, unsigned long arg)
235 struct hostmixer_state *state = file->private_data;
237 #ifdef DEBUG
238 printk(KERN_DEBUG "hostmixer: ioctl called\n");
239 #endif
241 return os_ioctl_generic(state->fd, cmd, arg);
244 static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
246 struct hostmixer_state *state;
247 int r = 0, w = 0;
248 int ret;
250 #ifdef DEBUG
251 printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
252 #endif
254 state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
255 if (state == NULL)
256 return -ENOMEM;
258 if (file->f_mode & FMODE_READ)
259 r = 1;
260 if (file->f_mode & FMODE_WRITE)
261 w = 1;
263 kparam_block_sysfs_write(mixer);
264 mutex_lock(&hostaudio_mutex);
265 ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
266 mutex_unlock(&hostaudio_mutex);
267 kparam_unblock_sysfs_write(mixer);
269 if (ret < 0) {
270 kparam_block_sysfs_write(dsp);
271 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
272 "err = %d\n", dsp, -ret);
273 kparam_unblock_sysfs_write(dsp);
274 kfree(state);
275 return ret;
278 file->private_data = state;
279 return 0;
282 static int hostmixer_release(struct inode *inode, struct file *file)
284 struct hostmixer_state *state = file->private_data;
286 #ifdef DEBUG
287 printk(KERN_DEBUG "hostmixer: release called\n");
288 #endif
290 os_close_file(state->fd);
291 kfree(state);
293 return 0;
296 /* kernel module operations */
298 static const struct file_operations hostaudio_fops = {
299 .owner = THIS_MODULE,
300 .llseek = no_llseek,
301 .read = hostaudio_read,
302 .write = hostaudio_write,
303 .poll = hostaudio_poll,
304 .unlocked_ioctl = hostaudio_ioctl,
305 .mmap = NULL,
306 .open = hostaudio_open,
307 .release = hostaudio_release,
310 static const struct file_operations hostmixer_fops = {
311 .owner = THIS_MODULE,
312 .llseek = no_llseek,
313 .unlocked_ioctl = hostmixer_ioctl_mixdev,
314 .open = hostmixer_open_mixdev,
315 .release = hostmixer_release,
318 struct {
319 int dev_audio;
320 int dev_mixer;
321 } module_data;
323 MODULE_AUTHOR("Steve Schmidtke");
324 MODULE_DESCRIPTION("UML Audio Relay");
325 MODULE_LICENSE("GPL");
327 static int __init hostaudio_init_module(void)
329 __kernel_param_lock();
330 printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
331 dsp, mixer);
332 __kernel_param_unlock();
334 module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
335 if (module_data.dev_audio < 0) {
336 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
337 return -ENODEV;
340 module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
341 if (module_data.dev_mixer < 0) {
342 printk(KERN_ERR "hostmixer: couldn't register mixer "
343 "device!\n");
344 unregister_sound_dsp(module_data.dev_audio);
345 return -ENODEV;
348 return 0;
351 static void __exit hostaudio_cleanup_module (void)
353 unregister_sound_mixer(module_data.dev_mixer);
354 unregister_sound_dsp(module_data.dev_audio);
357 module_init(hostaudio_init_module);
358 module_exit(hostaudio_cleanup_module);