f5da4283d1d052bc60a7100f24c7dd4eceb7d760
[linux-2.6.git] / drivers / xen / evtchn / evtchn.c
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  */
30
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>
49
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
56
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
61
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;
68
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 };
73
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;
77
78 void evtchn_device_upcall(int port)
79 {
80     struct per_user_data *u;
81
82     spin_lock(&port_user_lock);
83
84     mask_evtchn(port);
85     clear_evtchn(port);
86
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     }
103
104     spin_unlock(&port_user_lock);
105 }
106
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;
114
115     add_wait_queue(&u->evtchn_wait, &wait);
116
117     count &= ~1; /* even number of bytes */
118
119     if ( count == 0 )
120     {
121         rc = 0;
122         goto out;
123     }
124
125     if ( count > PAGE_SIZE )
126         count = PAGE_SIZE;
127
128     for ( ; ; )
129     {
130         set_current_state(TASK_INTERRUPTIBLE);
131
132         if ( (c = u->ring_cons) != (p = u->ring_prod) )
133             break;
134
135         if ( u->ring_overflow )
136         {
137             rc = -EFBIG;
138             goto out;
139         }
140
141         if ( file->f_flags & O_NONBLOCK )
142         {
143             rc = -EAGAIN;
144             goto out;
145         }
146
147         if ( signal_pending(current) )
148         {
149             rc = -ERESTARTSYS;
150             goto out;
151         }
152
153         schedule();
154     }
155
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     }
167
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     }
178
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     }
185
186     u->ring_cons += (bytes1 + bytes2) / sizeof(u16);
187
188     rc = bytes1 + bytes2;
189
190  out:
191     __set_current_state(TASK_RUNNING);
192     remove_wait_queue(&u->evtchn_wait, &wait);
193     return rc;
194 }
195
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;
202
203     if ( kbuf == NULL )
204         return -ENOMEM;
205
206     count &= ~1; /* even number of bytes */
207
208     if ( count == 0 )
209     {
210         rc = 0;
211         goto out;
212     }
213
214     if ( count > PAGE_SIZE )
215         count = PAGE_SIZE;
216
217     if ( copy_from_user(kbuf, buf, count) != 0 )
218     {
219         rc = -EFAULT;
220         goto out;
221     }
222
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);
228
229     rc = count;
230
231  out:
232     free_page((unsigned long)kbuf);
233     return rc;
234 }
235
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;
241
242     spin_lock_irq(&port_user_lock);
243     
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;
250
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;
266
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;
282
283     default:
284         rc = -ENOSYS;
285         break;
286     }
287
288     spin_unlock_irq(&port_user_lock);   
289
290     return rc;
291 }
292
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;
297
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 }
305
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 }
311
312 static int evtchn_open(struct inode *inode, struct file *filp)
313 {
314     struct per_user_data *u;
315
316     if ( (u = kmalloc(sizeof(*u), GFP_KERNEL)) == NULL )
317         return -ENOMEM;
318
319     memset(u, 0, sizeof(*u));
320     init_waitqueue_head(&u->evtchn_wait);
321
322     if ( (u->ring = (u16 *)__get_free_page(GFP_KERNEL)) == NULL )
323     {
324         kfree(u);
325         return -ENOMEM;
326     }
327
328     filp->private_data = u;
329
330     return 0;
331 }
332
333 static int evtchn_release(struct inode *inode, struct file *filp)
334 {
335     int i;
336     struct per_user_data *u = filp->private_data;
337
338     spin_lock_irq(&port_user_lock);
339
340     free_page((unsigned long)u->ring);
341
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     }
350
351     spin_unlock_irq(&port_user_lock);
352
353     return 0;
354 }
355
356 static struct file_operations evtchn_fops = {
357     owner:    THIS_MODULE,
358     read:     evtchn_read,
359     write:    evtchn_write,
360     ioctl:    evtchn_ioctl,
361     poll:     evtchn_poll,
362     fasync:   evtchn_fasync,
363     open:     evtchn_open,
364     release:  evtchn_release
365 };
366
367 static struct miscdevice evtchn_miscdev = {
368     .minor        = EVTCHN_MINOR,
369     .name         = "evtchn",
370     .fops         = &evtchn_fops,
371 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
372     .devfs_name   = "misc/evtchn",
373 #endif
374 };
375
376 static int __init evtchn_init(void)
377 {
378 #ifdef OLD_DEVFS
379     devfs_handle_t symlink_handle;
380     int            pos;
381     char           link_dest[64];
382 #endif
383     int err;
384
385     spin_lock_init(&port_user_lock);
386     memset(port_user, 0, sizeof(port_user));
387
388     /* (DEVFS) create '/dev/misc/evtchn'. */
389     err = misc_register(&evtchn_miscdev);
390     if ( err != 0 )
391     {
392         printk(KERN_ALERT "Could not register /dev/misc/evtchn\n");
393         return err;
394     }
395
396 #ifdef OLD_DEVFS
397     /* (DEVFS) create directory '/dev/xen'. */
398     xen_dev_dir = devfs_mk_dir(NULL, "xen", NULL);
399
400     /* (DEVFS) &link_dest[pos] == '../misc/evtchn'. */
401     pos = devfs_generate_path(evtchn_miscdev.devfs_handle, 
402                               &link_dest[3], 
403                               sizeof(link_dest) - 3);
404     if ( pos >= 0 )
405         strncpy(&link_dest[pos], "../", 3);
406
407     /* (DEVFS) symlink '/dev/xen/evtchn' -> '../misc/evtchn'. */
408     (void)devfs_mk_symlink(xen_dev_dir, 
409                            "evtchn", 
410                            DEVFS_FL_DEFAULT, 
411                            &link_dest[pos],
412                            &symlink_handle, 
413                            NULL);
414
415     /* (DEVFS) automatically destroy the symlink with its destination. */
416     devfs_auto_unregister(evtchn_miscdev.devfs_handle, symlink_handle);
417 #endif
418
419     printk("Event-channel device installed.\n");
420
421     return 0;
422 }
423
424 static void evtchn_cleanup(void)
425 {
426     misc_deregister(&evtchn_miscdev);
427 }
428
429 module_init(evtchn_init);
430 module_exit(evtchn_cleanup);