1 /******************************************************************************
4 * Xenolinux driver for receiving and demuxing event-channel signals.
6 * Copyright (c) 2004, K A Fraser
7 * Multi-process extensions Copyright (c) 2004, Steven Smith
9 * This file may be distributed separately from the Linux kernel, or
10 * incorporated into other software packages, subject to the following license:
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:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
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
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>
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>
54 #include <linux/gfp.h>
58 /* NB. This must be shared amongst drivers if more things go in /dev/xen */
59 static devfs_handle_t xen_dev_dir;
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))
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;
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)
80 struct per_user_data *u;
82 spin_lock(&port_user_lock);
87 if ( (u = port_user[port]) != NULL )
89 if ( (u->ring_prod - u->ring_cons) < EVTCHN_RING_SIZE )
91 u->ring[EVTCHN_RING_MASK(u->ring_prod)] = (u16)port;
92 if ( u->ring_cons == u->ring_prod++ )
94 wake_up_interruptible(&u->evtchn_wait);
95 kill_fasync(&u->evtchn_async_queue, SIGIO, POLL_IN);
100 u->ring_overflow = 1;
104 spin_unlock(&port_user_lock);
107 static ssize_t evtchn_read(struct file *file, char *buf,
108 size_t count, loff_t *ppos)
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 */
125 if ( count > PAGE_SIZE )
130 set_current_state(TASK_INTERRUPTIBLE);
132 if ( (c = u->ring_cons) != (p = u->ring_prod) )
135 if ( u->ring_overflow )
141 if ( file->f_flags & O_NONBLOCK )
147 if ( signal_pending(current) )
156 /* Byte lengths of two chunks. Chunk split (if any) is at ring wrap. */
157 if ( ((c ^ p) & EVTCHN_RING_SIZE) != 0 )
159 bytes1 = (EVTCHN_RING_SIZE - EVTCHN_RING_MASK(c)) * sizeof(u16);
160 bytes2 = EVTCHN_RING_MASK(p) * sizeof(u16);
164 bytes1 = (p - c) * sizeof(u16);
168 /* Truncate chunks according to caller's maximum byte count. */
169 if ( bytes1 > count )
174 else if ( (bytes1 + bytes2) > count )
176 bytes2 = count - bytes1;
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)) )
186 u->ring_cons += (bytes1 + bytes2) / sizeof(u16);
188 rc = bytes1 + bytes2;
191 __set_current_state(TASK_RUNNING);
192 remove_wait_queue(&u->evtchn_wait, &wait);
196 static ssize_t evtchn_write(struct file *file, const char *buf,
197 size_t count, loff_t *ppos)
200 u16 *kbuf = (u16 *)__get_free_page(GFP_KERNEL);
201 struct per_user_data *u = file->private_data;
206 count &= ~1; /* even number of bytes */
214 if ( count > PAGE_SIZE )
217 if ( copy_from_user(kbuf, buf, count) != 0 )
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);
232 free_page((unsigned long)kbuf);
236 static int evtchn_ioctl(struct inode *inode, struct file *file,
237 unsigned int cmd, unsigned long arg)
240 struct per_user_data *u = file->private_data;
242 spin_lock_irq(&port_user_lock);
247 /* Initialise the ring to empty. Clear errors. */
248 u->ring_cons = u->ring_prod = u->ring_overflow = 0;
252 if ( arg >= NR_EVENT_CHANNELS )
256 else if ( port_user[arg] != NULL )
268 if ( arg >= NR_EVENT_CHANNELS )
272 else if ( port_user[arg] != u )
278 port_user[arg] = NULL;
288 spin_unlock_irq(&port_user_lock);
293 static unsigned int evtchn_poll(struct file *file, poll_table *wait)
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 )
306 static int evtchn_fasync(int fd, struct file *filp, int on)
308 struct per_user_data *u = filp->private_data;
309 return fasync_helper(fd, filp, on, &u->evtchn_async_queue);
312 static int evtchn_open(struct inode *inode, struct file *filp)
314 struct per_user_data *u;
316 if ( (u = kmalloc(sizeof(*u), GFP_KERNEL)) == NULL )
319 memset(u, 0, sizeof(*u));
320 init_waitqueue_head(&u->evtchn_wait);
322 if ( (u->ring = (u16 *)__get_free_page(GFP_KERNEL)) == NULL )
328 filp->private_data = u;
333 static int evtchn_release(struct inode *inode, struct file *filp)
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++ )
344 if ( port_user[i] == u )
351 spin_unlock_irq(&port_user_lock);
356 static struct file_operations evtchn_fops = {
362 fasync: evtchn_fasync,
364 release: evtchn_release
367 static struct miscdevice evtchn_miscdev = {
368 .minor = EVTCHN_MINOR,
370 .fops = &evtchn_fops,
371 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
372 .devfs_name = "misc/evtchn",
376 static int __init evtchn_init(void)
379 devfs_handle_t symlink_handle;
385 spin_lock_init(&port_user_lock);
386 memset(port_user, 0, sizeof(port_user));
388 /* (DEVFS) create '/dev/misc/evtchn'. */
389 err = misc_register(&evtchn_miscdev);
392 printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
397 /* (DEVFS) create directory '/dev/xen'. */
398 xen_dev_dir = devfs_mk_dir(NULL, "xen", NULL);
400 /* (DEVFS) &link_dest[pos] == '../misc/evtchn'. */
401 pos = devfs_generate_path(evtchn_miscdev.devfs_handle,
403 sizeof(link_dest) - 3);
405 strncpy(&link_dest[pos], "../", 3);
407 /* (DEVFS) symlink '/dev/xen/evtchn' -> '../misc/evtchn'. */
408 (void)devfs_mk_symlink(xen_dev_dir,
415 /* (DEVFS) automatically destroy the symlink with its destination. */
416 devfs_auto_unregister(evtchn_miscdev.devfs_handle, symlink_handle);
419 printk("Event-channel device installed.\n");
424 static void evtchn_cleanup(void)
426 misc_deregister(&evtchn_miscdev);
429 module_init(evtchn_init);
430 module_exit(evtchn_cleanup);