ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/evtchn/evtchn.c @ 6617:c7e4e2fc4f4a

Free struct file file_private on release.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Sat Sep 03 16:32:53 2005 +0000 (2005-09-03)
parents dd668f7527cb
children f27205ea60ef
line source
1 /******************************************************************************
2 * evtchn.c
3 *
4 * Xenolinux driver for receiving and demuxing event-channel signals.
5 *
6 * Copyright (c) 2004, K A Fraser
7 * Multi-process extensions Copyright (c) 2004, Steven Smith
8 *
9 * This file may be distributed separately from the Linux kernel, or
10 * incorporated into other software packages, subject to the following license:
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 * IN THE SOFTWARE.
29 */
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/errno.h>
38 #include <linux/fs.h>
39 #include <linux/errno.h>
40 #include <linux/miscdevice.h>
41 #include <linux/major.h>
42 #include <linux/proc_fs.h>
43 #include <linux/stat.h>
44 #include <linux/poll.h>
45 #include <linux/irq.h>
46 #include <linux/init.h>
47 #define XEN_EVTCHN_MASK_OPS
48 #include <asm-xen/evtchn.h>
50 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
51 #include <linux/devfs_fs_kernel.h>
52 #define OLD_DEVFS
53 #else
54 #include <linux/gfp.h>
55 #endif
57 #ifdef OLD_DEVFS
58 /* NB. This must be shared amongst drivers if more things go in /dev/xen */
59 static devfs_handle_t xen_dev_dir;
60 #endif
62 struct per_user_data {
63 /* Notification ring, accessed via /dev/xen/evtchn. */
64 # define EVTCHN_RING_SIZE 2048 /* 2048 16-bit entries */
65 # define EVTCHN_RING_MASK(_i) ((_i)&(EVTCHN_RING_SIZE-1))
66 u16 *ring;
67 unsigned int ring_cons, ring_prod, ring_overflow;
69 /* Processes wait on this queue when ring is empty. */
70 wait_queue_head_t evtchn_wait;
71 struct fasync_struct *evtchn_async_queue;
72 };
74 /* Who's bound to each port? */
75 static struct per_user_data *port_user[NR_EVENT_CHANNELS];
76 static spinlock_t port_user_lock;
78 void evtchn_device_upcall(int port)
79 {
80 struct per_user_data *u;
82 spin_lock(&port_user_lock);
84 mask_evtchn(port);
85 clear_evtchn(port);
87 if ( (u = port_user[port]) != NULL )
88 {
89 if ( (u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE )
90 {
91 u->ring[EVTCHN_RING_MASK(u->ring_prod)] = (u16)port;
92 if ( u->ring_cons == u->ring_prod++ )
93 {
94 wake_up_interruptible(&u->evtchn_wait);
95 kill_fasync(&u->evtchn_async_queue, SIGIO, POLL_IN);
96 }
97 }
98 else
99 {
100 u->ring_overflow = 1;
101 }
102 }
104 spin_unlock(&port_user_lock);
105 }
107 static ssize_t evtchn_read(struct file *file, char *buf,
108 size_t count, loff_t *ppos)
109 {
110 int rc;
111 unsigned int c, p, bytes1 = 0, bytes2 = 0;
112 DECLARE_WAITQUEUE(wait, current);
113 struct per_user_data *u = file->private_data;
115 add_wait_queue(&u->evtchn_wait, &wait);
117 count &= ~1; /* even number of bytes */
119 if ( count == 0 )
120 {
121 rc = 0;
122 goto out;
123 }
125 if ( count > PAGE_SIZE )
126 count = PAGE_SIZE;
128 for ( ; ; )
129 {
130 set_current_state(TASK_INTERRUPTIBLE);
132 if ( (c = u->ring_cons) != (p = u->ring_prod) )
133 break;
135 if ( u->ring_overflow )
136 {
137 rc = -EFBIG;
138 goto out;
139 }
141 if ( file->f_flags & O_NONBLOCK )
142 {
143 rc = -EAGAIN;
144 goto out;
145 }
147 if ( signal_pending(current) )
148 {
149 rc = -ERESTARTSYS;
150 goto out;
151 }
153 schedule();
154 }
156 /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
157 if ( ((c ^ p) & EVTCHN_RING_SIZE) != 0 )
158 {
159 bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) * sizeof(u16);
160 bytes2 = EVTCHN_RING_MASK(p) * sizeof(u16);
161 }
162 else
163 {
164 bytes1 = (p - c) * sizeof(u16);
165 bytes2 = 0;
166 }
168 /* Truncate chunks according to caller's maximum byte count. */
169 if ( bytes1 > count )
170 {
171 bytes1 = count;
172 bytes2 = 0;
173 }
174 else if ( (bytes1 + bytes2) > count )
175 {
176 bytes2 = count - bytes1;
177 }
179 if ( copy_to_user(buf, &u->ring[EVTCHN_RING_MASK(c)], bytes1) ||
180 ((bytes2 != 0) && copy_to_user(&buf[bytes1], &u->ring[0], bytes2)) )
181 {
182 rc = -EFAULT;
183 goto out;
184 }
186 u->ring_cons += (bytes1 + bytes2) / sizeof(u16);
188 rc = bytes1 + bytes2;
190 out:
191 __set_current_state(TASK_RUNNING);
192 remove_wait_queue(&u->evtchn_wait, &wait);
193 return rc;
194 }
196 static ssize_t evtchn_write(struct file *file, const char *buf,
197 size_t count, loff_t *ppos)
198 {
199 int rc, i;
200 u16 *kbuf = (u16 *)__get_free_page(GFP_KERNEL);
201 struct per_user_data *u = file->private_data;
203 if ( kbuf == NULL )
204 return -ENOMEM;
206 count &= ~1; /* even number of bytes */
208 if ( count == 0 )
209 {
210 rc = 0;
211 goto out;
212 }
214 if ( count > PAGE_SIZE )
215 count = PAGE_SIZE;
217 if ( copy_from_user(kbuf, buf, count) != 0 )
218 {
219 rc = -EFAULT;
220 goto out;
221 }
223 spin_lock_irq(&port_user_lock);
224 for ( i = 0; i < (count/2); i++ )
225 if ( (kbuf[i] < NR_EVENT_CHANNELS) && (port_user[kbuf[i]] == u) )
226 unmask_evtchn(kbuf[i]);
227 spin_unlock_irq(&port_user_lock);
229 rc = count;
231 out:
232 free_page((unsigned long)kbuf);
233 return rc;
234 }
236 static int evtchn_ioctl(struct inode *inode, struct file *file,
237 unsigned int cmd, unsigned long arg)
238 {
239 int rc = 0;
240 struct per_user_data *u = file->private_data;
242 spin_lock_irq(&port_user_lock);
244 switch ( cmd )
245 {
246 case EVTCHN_RESET:
247 /* Initialise the ring to empty. Clear errors. */
248 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
249 break;
251 case EVTCHN_BIND:
252 if ( arg >= NR_EVENT_CHANNELS )
253 {
254 rc = -EINVAL;
255 }
256 else if ( port_user[arg] != NULL )
257 {
258 rc = -EISCONN;
259 }
260 else
261 {
262 port_user[arg] = u;
263 unmask_evtchn(arg);
264 }
265 break;
267 case EVTCHN_UNBIND:
268 if ( arg >= NR_EVENT_CHANNELS )
269 {
270 rc = -EINVAL;
271 }
272 else if ( port_user[arg] != u )
273 {
274 rc = -ENOTCONN;
275 }
276 else
277 {
278 port_user[arg] = NULL;
279 mask_evtchn(arg);
280 }
281 break;
283 default:
284 rc = -ENOSYS;
285 break;
286 }
288 spin_unlock_irq(&port_user_lock);
290 return rc;
291 }
293 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
294 {
295 unsigned int mask = POLLOUT | POLLWRNORM;
296 struct per_user_data *u = file->private_data;
298 poll_wait(file, &u->evtchn_wait, wait);
299 if ( u->ring_cons != u->ring_prod )
300 mask |= POLLIN | POLLRDNORM;
301 if ( u->ring_overflow )
302 mask = POLLERR;
303 return mask;
304 }
306 static int evtchn_fasync(int fd, struct file *filp, int on)
307 {
308 struct per_user_data *u = filp->private_data;
309 return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
310 }
312 static int evtchn_open(struct inode *inode, struct file *filp)
313 {
314 struct per_user_data *u;
316 if ( (u = kmalloc(sizeof(*u), GFP_KERNEL)) == NULL )
317 return -ENOMEM;
319 memset(u, 0, sizeof(*u));
320 init_waitqueue_head(&u->evtchn_wait);
322 if ( (u->ring = (u16 *)__get_free_page(GFP_KERNEL)) == NULL )
323 {
324 kfree(u);
325 return -ENOMEM;
326 }
328 filp->private_data = u;
330 return 0;
331 }
333 static int evtchn_release(struct inode *inode, struct file *filp)
334 {
335 int i;
336 struct per_user_data *u = filp->private_data;
338 spin_lock_irq(&port_user_lock);
340 free_page((unsigned long)u->ring);
342 for ( i = 0; i < NR_EVENT_CHANNELS; i++ )
343 {
344 if ( port_user[i] == u )
345 {
346 port_user[i] = NULL;
347 mask_evtchn(i);
348 }
349 }
351 spin_unlock_irq(&port_user_lock);
353 kfree(u);
355 return 0;
356 }
358 static struct file_operations evtchn_fops = {
359 owner: THIS_MODULE,
360 read: evtchn_read,
361 write: evtchn_write,
362 ioctl: evtchn_ioctl,
363 poll: evtchn_poll,
364 fasync: evtchn_fasync,
365 open: evtchn_open,
366 release: evtchn_release
367 };
369 static struct miscdevice evtchn_miscdev = {
370 .minor = EVTCHN_MINOR,
371 .name = "evtchn",
372 .fops = &evtchn_fops,
373 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
374 .devfs_name = "misc/evtchn",
375 #endif
376 };
378 static int __init evtchn_init(void)
379 {
380 #ifdef OLD_DEVFS
381 devfs_handle_t symlink_handle;
382 int pos;
383 char link_dest[64];
384 #endif
385 int err;
387 spin_lock_init(&port_user_lock);
388 memset(port_user, 0, sizeof(port_user));
390 /* (DEVFS) create '/dev/misc/evtchn'. */
391 err = misc_register(&evtchn_miscdev);
392 if ( err != 0 )
393 {
394 printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
395 return err;
396 }
398 #ifdef OLD_DEVFS
399 /* (DEVFS) create directory '/dev/xen'. */
400 xen_dev_dir = devfs_mk_dir(NULL, "xen", NULL);
402 /* (DEVFS) &link_dest[pos] == '../misc/evtchn'. */
403 pos = devfs_generate_path(evtchn_miscdev.devfs_handle,
404 &link_dest[3],
405 sizeof(link_dest) - 3);
406 if ( pos >= 0 )
407 strncpy(&link_dest[pos], "../", 3);
409 /* (DEVFS) symlink '/dev/xen/evtchn' -> '../misc/evtchn'. */
410 (void)devfs_mk_symlink(xen_dev_dir,
411 "evtchn",
412 DEVFS_FL_DEFAULT,
413 &link_dest[pos],
414 &symlink_handle,
415 NULL);
417 /* (DEVFS) automatically destroy the symlink with its destination. */
418 devfs_auto_unregister(evtchn_miscdev.devfs_handle, symlink_handle);
419 #endif
421 printk("Event-channel device installed.\n");
423 return 0;
424 }
426 static void evtchn_cleanup(void)
427 {
428 misc_deregister(&evtchn_miscdev);
429 }
431 module_init(evtchn_init);
432 module_exit(evtchn_cleanup);