drivers/net/hamradio: Eliminate a NULL pointer dereference
[linux-2.6/next.git] / arch / um / drivers / hostaudio_kern.c
blob368219cc2366d666ca92e0ac38497f111d888501
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 "asm/uaccess.h"
12 #include "init.h"
13 #include "os.h"
15 struct hostaudio_state {
16 int fd;
19 struct hostmixer_state {
20 int fd;
23 #define HOSTAUDIO_DEV_DSP "/dev/sound/dsp"
24 #define HOSTAUDIO_DEV_MIXER "/dev/sound/mixer"
27 * Changed either at boot time or module load time. At boot, this is
28 * single-threaded; at module load, multiple modules would each have
29 * their own copy of these variables.
31 static char *dsp = HOSTAUDIO_DEV_DSP;
32 static char *mixer = HOSTAUDIO_DEV_MIXER;
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"
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"
42 #ifndef MODULE
43 static int set_dsp(char *name, int *add)
45 dsp = name;
46 return 0;
49 __uml_setup("dsp=", set_dsp, "dsp=<dsp device>\n" DSP_HELP);
51 static int set_mixer(char *name, int *add)
53 mixer = name;
54 return 0;
57 __uml_setup("mixer=", set_mixer, "mixer=<mixer device>\n" MIXER_HELP);
59 #else /*MODULE*/
61 module_param(dsp, charp, 0644);
62 MODULE_PARM_DESC(dsp, DSP_HELP);
64 module_param(mixer, charp, 0644);
65 MODULE_PARM_DESC(mixer, MIXER_HELP);
67 #endif
69 /* /dev/dsp file operations */
71 static ssize_t hostaudio_read(struct file *file, char __user *buffer,
72 size_t count, loff_t *ppos)
74 struct hostaudio_state *state = file->private_data;
75 void *kbuf;
76 int err;
78 #ifdef DEBUG
79 printk(KERN_DEBUG "hostaudio: read called, count = %d\n", count);
80 #endif
82 kbuf = kmalloc(count, GFP_KERNEL);
83 if (kbuf == NULL)
84 return -ENOMEM;
86 err = os_read_file(state->fd, kbuf, count);
87 if (err < 0)
88 goto out;
90 if (copy_to_user(buffer, kbuf, err))
91 err = -EFAULT;
93 out:
94 kfree(kbuf);
95 return err;
98 static ssize_t hostaudio_write(struct file *file, const char __user *buffer,
99 size_t count, loff_t *ppos)
101 struct hostaudio_state *state = file->private_data;
102 void *kbuf;
103 int err;
105 #ifdef DEBUG
106 printk(KERN_DEBUG "hostaudio: write called, count = %d\n", count);
107 #endif
109 kbuf = kmalloc(count, GFP_KERNEL);
110 if (kbuf == NULL)
111 return -ENOMEM;
113 err = -EFAULT;
114 if (copy_from_user(kbuf, buffer, count))
115 goto out;
117 err = os_write_file(state->fd, kbuf, count);
118 if (err < 0)
119 goto out;
120 *ppos += err;
122 out:
123 kfree(kbuf);
124 return err;
127 static unsigned int hostaudio_poll(struct file *file,
128 struct poll_table_struct *wait)
130 unsigned int mask = 0;
132 #ifdef DEBUG
133 printk(KERN_DEBUG "hostaudio: poll called (unimplemented)\n");
134 #endif
136 return mask;
139 static int hostaudio_ioctl(struct inode *inode, struct file *file,
140 unsigned int cmd, unsigned long arg)
142 struct hostaudio_state *state = file->private_data;
143 unsigned long data = 0;
144 int err;
146 #ifdef DEBUG
147 printk(KERN_DEBUG "hostaudio: ioctl called, cmd = %u\n", cmd);
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:
156 if (get_user(data, (int __user *) arg))
157 return -EFAULT;
158 break;
159 default:
160 break;
163 err = os_ioctl_generic(state->fd, cmd, (unsigned long) &data);
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:
172 if (put_user(data, (int __user *) arg))
173 return -EFAULT;
174 break;
175 default:
176 break;
179 return err;
182 static int hostaudio_open(struct inode *inode, struct file *file)
184 struct hostaudio_state *state;
185 int r = 0, w = 0;
186 int ret;
188 #ifdef DEBUG
189 printk(KERN_DEBUG "hostaudio: open called (host: %s)\n", dsp);
190 #endif
192 state = kmalloc(sizeof(struct hostaudio_state), GFP_KERNEL);
193 if (state == NULL)
194 return -ENOMEM;
196 if (file->f_mode & FMODE_READ)
197 r = 1;
198 if (file->f_mode & FMODE_WRITE)
199 w = 1;
201 ret = os_open_file(dsp, of_set_rw(OPENFLAGS(), r, w), 0);
202 if (ret < 0) {
203 kfree(state);
204 return ret;
206 state->fd = ret;
207 file->private_data = state;
208 return 0;
211 static int hostaudio_release(struct inode *inode, struct file *file)
213 struct hostaudio_state *state = file->private_data;
215 #ifdef DEBUG
216 printk(KERN_DEBUG "hostaudio: release called\n");
217 #endif
218 os_close_file(state->fd);
219 kfree(state);
221 return 0;
224 /* /dev/mixer file operations */
226 static int hostmixer_ioctl_mixdev(struct inode *inode, struct file *file,
227 unsigned int cmd, unsigned long arg)
229 struct hostmixer_state *state = file->private_data;
231 #ifdef DEBUG
232 printk(KERN_DEBUG "hostmixer: ioctl called\n");
233 #endif
235 return os_ioctl_generic(state->fd, cmd, arg);
238 static int hostmixer_open_mixdev(struct inode *inode, struct file *file)
240 struct hostmixer_state *state;
241 int r = 0, w = 0;
242 int ret;
244 #ifdef DEBUG
245 printk(KERN_DEBUG "hostmixer: open called (host: %s)\n", mixer);
246 #endif
248 state = kmalloc(sizeof(struct hostmixer_state), GFP_KERNEL);
249 if (state == NULL)
250 return -ENOMEM;
252 if (file->f_mode & FMODE_READ)
253 r = 1;
254 if (file->f_mode & FMODE_WRITE)
255 w = 1;
257 ret = os_open_file(mixer, of_set_rw(OPENFLAGS(), r, w), 0);
259 if (ret < 0) {
260 printk(KERN_ERR "hostaudio_open_mixdev failed to open '%s', "
261 "err = %d\n", dsp, -ret);
262 kfree(state);
263 return ret;
266 file->private_data = state;
267 return 0;
270 static int hostmixer_release(struct inode *inode, struct file *file)
272 struct hostmixer_state *state = file->private_data;
274 #ifdef DEBUG
275 printk(KERN_DEBUG "hostmixer: release called\n");
276 #endif
278 os_close_file(state->fd);
279 kfree(state);
281 return 0;
284 /* kernel module operations */
286 static const struct file_operations hostaudio_fops = {
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,
298 static const struct file_operations hostmixer_fops = {
299 .owner = THIS_MODULE,
300 .llseek = no_llseek,
301 .ioctl = hostmixer_ioctl_mixdev,
302 .open = hostmixer_open_mixdev,
303 .release = hostmixer_release,
306 struct {
307 int dev_audio;
308 int dev_mixer;
309 } module_data;
311 MODULE_AUTHOR("Steve Schmidtke");
312 MODULE_DESCRIPTION("UML Audio Relay");
313 MODULE_LICENSE("GPL");
315 static int __init hostaudio_init_module(void)
317 printk(KERN_INFO "UML Audio Relay (host dsp = %s, host mixer = %s)\n",
318 dsp, mixer);
320 module_data.dev_audio = register_sound_dsp(&hostaudio_fops, -1);
321 if (module_data.dev_audio < 0) {
322 printk(KERN_ERR "hostaudio: couldn't register DSP device!\n");
323 return -ENODEV;
326 module_data.dev_mixer = register_sound_mixer(&hostmixer_fops, -1);
327 if (module_data.dev_mixer < 0) {
328 printk(KERN_ERR "hostmixer: couldn't register mixer "
329 "device!\n");
330 unregister_sound_dsp(module_data.dev_audio);
331 return -ENODEV;
334 return 0;
337 static void __exit hostaudio_cleanup_module (void)
339 unregister_sound_mixer(module_data.dev_mixer);
340 unregister_sound_dsp(module_data.dev_audio);
343 module_init(hostaudio_init_module);
344 module_exit(hostaudio_cleanup_module);