md: remove needless setting of thread->timeout in raid10_quiesce
[linux-2.6.git] / drivers / input / xen-kbdfront.c
1 /*
2  * Xen para-virtual input device
3  *
4  * Copyright (C) 2005 Anthony Liguori <aliguori@us.ibm.com>
5  * Copyright (C) 2006-2008 Red Hat, Inc., Markus Armbruster <armbru@redhat.com>
6  *
7  *  Based on linux/drivers/input/mouse/sermouse.c
8  *
9  *  This file is subject to the terms and conditions of the GNU General Public
10  *  License. See the file COPYING in the main directory of this archive for
11  *  more details.
12  */
13
14 /*
15  * TODO:
16  *
17  * Switch to grant tables together with xen-fbfront.c.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/module.h>
23 #include <linux/input.h>
24
25 #include <asm/xen/hypervisor.h>
26
27 #include <xen/xen.h>
28 #include <xen/events.h>
29 #include <xen/page.h>
30 #include <xen/interface/io/fbif.h>
31 #include <xen/interface/io/kbdif.h>
32 #include <xen/xenbus.h>
33
34 struct xenkbd_info {
35         struct input_dev *kbd;
36         struct input_dev *ptr;
37         struct xenkbd_page *page;
38         int irq;
39         struct xenbus_device *xbdev;
40         char phys[32];
41 };
42
43 static int xenkbd_remove(struct xenbus_device *);
44 static int xenkbd_connect_backend(struct xenbus_device *, struct xenkbd_info *);
45 static void xenkbd_disconnect_backend(struct xenkbd_info *);
46
47 /*
48  * Note: if you need to send out events, see xenfb_do_update() for how
49  * to do that.
50  */
51
52 static irqreturn_t input_handler(int rq, void *dev_id)
53 {
54         struct xenkbd_info *info = dev_id;
55         struct xenkbd_page *page = info->page;
56         __u32 cons, prod;
57
58         prod = page->in_prod;
59         if (prod == page->in_cons)
60                 return IRQ_HANDLED;
61         rmb();                  /* ensure we see ring contents up to prod */
62         for (cons = page->in_cons; cons != prod; cons++) {
63                 union xenkbd_in_event *event;
64                 struct input_dev *dev;
65                 event = &XENKBD_IN_RING_REF(page, cons);
66
67                 dev = info->ptr;
68                 switch (event->type) {
69                 case XENKBD_TYPE_MOTION:
70                         input_report_rel(dev, REL_X, event->motion.rel_x);
71                         input_report_rel(dev, REL_Y, event->motion.rel_y);
72                         if (event->motion.rel_z)
73                                 input_report_rel(dev, REL_WHEEL,
74                                                  -event->motion.rel_z);
75                         break;
76                 case XENKBD_TYPE_KEY:
77                         dev = NULL;
78                         if (test_bit(event->key.keycode, info->kbd->keybit))
79                                 dev = info->kbd;
80                         if (test_bit(event->key.keycode, info->ptr->keybit))
81                                 dev = info->ptr;
82                         if (dev)
83                                 input_report_key(dev, event->key.keycode,
84                                                  event->key.pressed);
85                         else
86                                 printk(KERN_WARNING
87                                        "xenkbd: unhandled keycode 0x%x\n",
88                                        event->key.keycode);
89                         break;
90                 case XENKBD_TYPE_POS:
91                         input_report_abs(dev, ABS_X, event->pos.abs_x);
92                         input_report_abs(dev, ABS_Y, event->pos.abs_y);
93                         if (event->pos.rel_z)
94                                 input_report_rel(dev, REL_WHEEL,
95                                                  -event->pos.rel_z);
96                         break;
97                 }
98                 if (dev)
99                         input_sync(dev);
100         }
101         mb();                   /* ensure we got ring contents */
102         page->in_cons = cons;
103         notify_remote_via_irq(info->irq);
104
105         return IRQ_HANDLED;
106 }
107
108 static int __devinit xenkbd_probe(struct xenbus_device *dev,
109                                   const struct xenbus_device_id *id)
110 {
111         int ret, i;
112         struct xenkbd_info *info;
113         struct input_dev *kbd, *ptr;
114
115         info = kzalloc(sizeof(*info), GFP_KERNEL);
116         if (!info) {
117                 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
118                 return -ENOMEM;
119         }
120         dev_set_drvdata(&dev->dev, info);
121         info->xbdev = dev;
122         info->irq = -1;
123         snprintf(info->phys, sizeof(info->phys), "xenbus/%s", dev->nodename);
124
125         info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
126         if (!info->page)
127                 goto error_nomem;
128
129         /* keyboard */
130         kbd = input_allocate_device();
131         if (!kbd)
132                 goto error_nomem;
133         kbd->name = "Xen Virtual Keyboard";
134         kbd->phys = info->phys;
135         kbd->id.bustype = BUS_PCI;
136         kbd->id.vendor = 0x5853;
137         kbd->id.product = 0xffff;
138         kbd->evbit[0] = BIT(EV_KEY);
139         for (i = KEY_ESC; i < KEY_UNKNOWN; i++)
140                 set_bit(i, kbd->keybit);
141         for (i = KEY_OK; i < KEY_MAX; i++)
142                 set_bit(i, kbd->keybit);
143
144         ret = input_register_device(kbd);
145         if (ret) {
146                 input_free_device(kbd);
147                 xenbus_dev_fatal(dev, ret, "input_register_device(kbd)");
148                 goto error;
149         }
150         info->kbd = kbd;
151
152         /* pointing device */
153         ptr = input_allocate_device();
154         if (!ptr)
155                 goto error_nomem;
156         ptr->name = "Xen Virtual Pointer";
157         ptr->phys = info->phys;
158         ptr->id.bustype = BUS_PCI;
159         ptr->id.vendor = 0x5853;
160         ptr->id.product = 0xfffe;
161         ptr->evbit[0] = BIT(EV_KEY) | BIT(EV_REL) | BIT(EV_ABS);
162         for (i = BTN_LEFT; i <= BTN_TASK; i++)
163                 set_bit(i, ptr->keybit);
164         ptr->relbit[0] = BIT(REL_X) | BIT(REL_Y) | BIT(REL_WHEEL);
165         input_set_abs_params(ptr, ABS_X, 0, XENFB_WIDTH, 0, 0);
166         input_set_abs_params(ptr, ABS_Y, 0, XENFB_HEIGHT, 0, 0);
167
168         ret = input_register_device(ptr);
169         if (ret) {
170                 input_free_device(ptr);
171                 xenbus_dev_fatal(dev, ret, "input_register_device(ptr)");
172                 goto error;
173         }
174         info->ptr = ptr;
175
176         ret = xenkbd_connect_backend(dev, info);
177         if (ret < 0)
178                 goto error;
179
180         return 0;
181
182  error_nomem:
183         ret = -ENOMEM;
184         xenbus_dev_fatal(dev, ret, "allocating device memory");
185  error:
186         xenkbd_remove(dev);
187         return ret;
188 }
189
190 static int xenkbd_resume(struct xenbus_device *dev)
191 {
192         struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
193
194         xenkbd_disconnect_backend(info);
195         memset(info->page, 0, PAGE_SIZE);
196         return xenkbd_connect_backend(dev, info);
197 }
198
199 static int xenkbd_remove(struct xenbus_device *dev)
200 {
201         struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
202
203         xenkbd_disconnect_backend(info);
204         if (info->kbd)
205                 input_unregister_device(info->kbd);
206         if (info->ptr)
207                 input_unregister_device(info->ptr);
208         free_page((unsigned long)info->page);
209         kfree(info);
210         return 0;
211 }
212
213 static int xenkbd_connect_backend(struct xenbus_device *dev,
214                                   struct xenkbd_info *info)
215 {
216         int ret, evtchn;
217         struct xenbus_transaction xbt;
218
219         ret = xenbus_alloc_evtchn(dev, &evtchn);
220         if (ret)
221                 return ret;
222         ret = bind_evtchn_to_irqhandler(evtchn, input_handler,
223                                         0, dev->devicetype, info);
224         if (ret < 0) {
225                 xenbus_free_evtchn(dev, evtchn);
226                 xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
227                 return ret;
228         }
229         info->irq = ret;
230
231  again:
232         ret = xenbus_transaction_start(&xbt);
233         if (ret) {
234                 xenbus_dev_fatal(dev, ret, "starting transaction");
235                 return ret;
236         }
237         ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu",
238                             virt_to_mfn(info->page));
239         if (ret)
240                 goto error_xenbus;
241         ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
242                             evtchn);
243         if (ret)
244                 goto error_xenbus;
245         ret = xenbus_transaction_end(xbt, 0);
246         if (ret) {
247                 if (ret == -EAGAIN)
248                         goto again;
249                 xenbus_dev_fatal(dev, ret, "completing transaction");
250                 return ret;
251         }
252
253         xenbus_switch_state(dev, XenbusStateInitialised);
254         return 0;
255
256  error_xenbus:
257         xenbus_transaction_end(xbt, 1);
258         xenbus_dev_fatal(dev, ret, "writing xenstore");
259         return ret;
260 }
261
262 static void xenkbd_disconnect_backend(struct xenkbd_info *info)
263 {
264         if (info->irq >= 0)
265                 unbind_from_irqhandler(info->irq, info);
266         info->irq = -1;
267 }
268
269 static void xenkbd_backend_changed(struct xenbus_device *dev,
270                                    enum xenbus_state backend_state)
271 {
272         struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
273         int ret, val;
274
275         switch (backend_state) {
276         case XenbusStateInitialising:
277         case XenbusStateInitialised:
278         case XenbusStateUnknown:
279         case XenbusStateClosed:
280                 break;
281
282         case XenbusStateInitWait:
283 InitWait:
284                 ret = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
285                                    "feature-abs-pointer", "%d", &val);
286                 if (ret < 0)
287                         val = 0;
288                 if (val) {
289                         ret = xenbus_printf(XBT_NIL, info->xbdev->nodename,
290                                             "request-abs-pointer", "1");
291                         if (ret)
292                                 printk(KERN_WARNING
293                                        "xenkbd: can't request abs-pointer");
294                 }
295                 xenbus_switch_state(dev, XenbusStateConnected);
296                 break;
297
298         case XenbusStateConnected:
299                 /*
300                  * Work around xenbus race condition: If backend goes
301                  * through InitWait to Connected fast enough, we can
302                  * get Connected twice here.
303                  */
304                 if (dev->state != XenbusStateConnected)
305                         goto InitWait; /* no InitWait seen yet, fudge it */
306
307                 /* Set input abs params to match backend screen res */
308                 if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
309                                  "width", "%d", &val) > 0)
310                         input_set_abs_params(info->ptr, ABS_X, 0, val, 0, 0);
311
312                 if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
313                                  "height", "%d", &val) > 0)
314                         input_set_abs_params(info->ptr, ABS_Y, 0, val, 0, 0);
315
316                 break;
317
318         case XenbusStateClosing:
319                 xenbus_frontend_closed(dev);
320                 break;
321         }
322 }
323
324 static struct xenbus_device_id xenkbd_ids[] = {
325         { "vkbd" },
326         { "" }
327 };
328
329 static struct xenbus_driver xenkbd_driver = {
330         .name = "vkbd",
331         .owner = THIS_MODULE,
332         .ids = xenkbd_ids,
333         .probe = xenkbd_probe,
334         .remove = xenkbd_remove,
335         .resume = xenkbd_resume,
336         .otherend_changed = xenkbd_backend_changed,
337 };
338
339 static int __init xenkbd_init(void)
340 {
341         if (!xen_domain())
342                 return -ENODEV;
343
344         /* Nothing to do if running in dom0. */
345         if (xen_initial_domain())
346                 return -ENODEV;
347
348         return xenbus_register_frontend(&xenkbd_driver);
349 }
350
351 static void __exit xenkbd_cleanup(void)
352 {
353         xenbus_unregister_driver(&xenkbd_driver);
354 }
355
356 module_init(xenkbd_init);
357 module_exit(xenkbd_cleanup);
358
359 MODULE_DESCRIPTION("Xen virtual keyboard/pointer device frontend");
360 MODULE_LICENSE("GPL");
361 MODULE_ALIAS("xen:vkbd");