workqueue: fix deadlock in worker_maybe_bind_and_lock()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / xen-kbdfront.c
blobc35ab940f1f00b3eb84a34b88889754af67077a1
1 /*
2 * Xen para-virtual input device
4 * Copyright (C) 2005 Anthony Liguori <aliguori@us.ibm.com>
5 * Copyright (C) 2006-2008 Red Hat, Inc., Markus Armbruster <armbru@redhat.com>
7 * Based on linux/drivers/input/mouse/sermouse.c
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.
15 * TODO:
17 * Switch to grant tables together with xen-fbfront.c.
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/kernel.h>
23 #include <linux/errno.h>
24 #include <linux/module.h>
25 #include <linux/input.h>
26 #include <linux/slab.h>
28 #include <asm/xen/hypervisor.h>
30 #include <xen/xen.h>
31 #include <xen/events.h>
32 #include <xen/page.h>
33 #include <xen/interface/io/fbif.h>
34 #include <xen/interface/io/kbdif.h>
35 #include <xen/xenbus.h>
37 struct xenkbd_info {
38 struct input_dev *kbd;
39 struct input_dev *ptr;
40 struct xenkbd_page *page;
41 int irq;
42 struct xenbus_device *xbdev;
43 char phys[32];
46 static int xenkbd_remove(struct xenbus_device *);
47 static int xenkbd_connect_backend(struct xenbus_device *, struct xenkbd_info *);
48 static void xenkbd_disconnect_backend(struct xenkbd_info *);
51 * Note: if you need to send out events, see xenfb_do_update() for how
52 * to do that.
55 static irqreturn_t input_handler(int rq, void *dev_id)
57 struct xenkbd_info *info = dev_id;
58 struct xenkbd_page *page = info->page;
59 __u32 cons, prod;
61 prod = page->in_prod;
62 if (prod == page->in_cons)
63 return IRQ_HANDLED;
64 rmb(); /* ensure we see ring contents up to prod */
65 for (cons = page->in_cons; cons != prod; cons++) {
66 union xenkbd_in_event *event;
67 struct input_dev *dev;
68 event = &XENKBD_IN_RING_REF(page, cons);
70 dev = info->ptr;
71 switch (event->type) {
72 case XENKBD_TYPE_MOTION:
73 input_report_rel(dev, REL_X, event->motion.rel_x);
74 input_report_rel(dev, REL_Y, event->motion.rel_y);
75 if (event->motion.rel_z)
76 input_report_rel(dev, REL_WHEEL,
77 -event->motion.rel_z);
78 break;
79 case XENKBD_TYPE_KEY:
80 dev = NULL;
81 if (test_bit(event->key.keycode, info->kbd->keybit))
82 dev = info->kbd;
83 if (test_bit(event->key.keycode, info->ptr->keybit))
84 dev = info->ptr;
85 if (dev)
86 input_report_key(dev, event->key.keycode,
87 event->key.pressed);
88 else
89 pr_warning("unhandled keycode 0x%x\n",
90 event->key.keycode);
91 break;
92 case XENKBD_TYPE_POS:
93 input_report_abs(dev, ABS_X, event->pos.abs_x);
94 input_report_abs(dev, ABS_Y, event->pos.abs_y);
95 if (event->pos.rel_z)
96 input_report_rel(dev, REL_WHEEL,
97 -event->pos.rel_z);
98 break;
100 if (dev)
101 input_sync(dev);
103 mb(); /* ensure we got ring contents */
104 page->in_cons = cons;
105 notify_remote_via_irq(info->irq);
107 return IRQ_HANDLED;
110 static int __devinit xenkbd_probe(struct xenbus_device *dev,
111 const struct xenbus_device_id *id)
113 int ret, i, abs;
114 struct xenkbd_info *info;
115 struct input_dev *kbd, *ptr;
117 info = kzalloc(sizeof(*info), GFP_KERNEL);
118 if (!info) {
119 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
120 return -ENOMEM;
122 dev_set_drvdata(&dev->dev, info);
123 info->xbdev = dev;
124 info->irq = -1;
125 snprintf(info->phys, sizeof(info->phys), "xenbus/%s", dev->nodename);
127 info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
128 if (!info->page)
129 goto error_nomem;
131 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-abs-pointer", "%d", &abs) < 0)
132 abs = 0;
133 if (abs)
134 xenbus_printf(XBT_NIL, dev->nodename, "request-abs-pointer", "1");
136 /* keyboard */
137 kbd = input_allocate_device();
138 if (!kbd)
139 goto error_nomem;
140 kbd->name = "Xen Virtual Keyboard";
141 kbd->phys = info->phys;
142 kbd->id.bustype = BUS_PCI;
143 kbd->id.vendor = 0x5853;
144 kbd->id.product = 0xffff;
146 __set_bit(EV_KEY, kbd->evbit);
147 for (i = KEY_ESC; i < KEY_UNKNOWN; i++)
148 __set_bit(i, kbd->keybit);
149 for (i = KEY_OK; i < KEY_MAX; i++)
150 __set_bit(i, kbd->keybit);
152 ret = input_register_device(kbd);
153 if (ret) {
154 input_free_device(kbd);
155 xenbus_dev_fatal(dev, ret, "input_register_device(kbd)");
156 goto error;
158 info->kbd = kbd;
160 /* pointing device */
161 ptr = input_allocate_device();
162 if (!ptr)
163 goto error_nomem;
164 ptr->name = "Xen Virtual Pointer";
165 ptr->phys = info->phys;
166 ptr->id.bustype = BUS_PCI;
167 ptr->id.vendor = 0x5853;
168 ptr->id.product = 0xfffe;
170 if (abs) {
171 __set_bit(EV_ABS, ptr->evbit);
172 input_set_abs_params(ptr, ABS_X, 0, XENFB_WIDTH, 0, 0);
173 input_set_abs_params(ptr, ABS_Y, 0, XENFB_HEIGHT, 0, 0);
174 } else {
175 input_set_capability(ptr, EV_REL, REL_X);
176 input_set_capability(ptr, EV_REL, REL_Y);
178 input_set_capability(ptr, EV_REL, REL_WHEEL);
180 __set_bit(EV_KEY, ptr->evbit);
181 for (i = BTN_LEFT; i <= BTN_TASK; i++)
182 __set_bit(i, ptr->keybit);
184 ret = input_register_device(ptr);
185 if (ret) {
186 input_free_device(ptr);
187 xenbus_dev_fatal(dev, ret, "input_register_device(ptr)");
188 goto error;
190 info->ptr = ptr;
192 ret = xenkbd_connect_backend(dev, info);
193 if (ret < 0)
194 goto error;
196 return 0;
198 error_nomem:
199 ret = -ENOMEM;
200 xenbus_dev_fatal(dev, ret, "allocating device memory");
201 error:
202 xenkbd_remove(dev);
203 return ret;
206 static int xenkbd_resume(struct xenbus_device *dev)
208 struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
210 xenkbd_disconnect_backend(info);
211 memset(info->page, 0, PAGE_SIZE);
212 return xenkbd_connect_backend(dev, info);
215 static int xenkbd_remove(struct xenbus_device *dev)
217 struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
219 xenkbd_disconnect_backend(info);
220 if (info->kbd)
221 input_unregister_device(info->kbd);
222 if (info->ptr)
223 input_unregister_device(info->ptr);
224 free_page((unsigned long)info->page);
225 kfree(info);
226 return 0;
229 static int xenkbd_connect_backend(struct xenbus_device *dev,
230 struct xenkbd_info *info)
232 int ret, evtchn;
233 struct xenbus_transaction xbt;
235 ret = xenbus_alloc_evtchn(dev, &evtchn);
236 if (ret)
237 return ret;
238 ret = bind_evtchn_to_irqhandler(evtchn, input_handler,
239 0, dev->devicetype, info);
240 if (ret < 0) {
241 xenbus_free_evtchn(dev, evtchn);
242 xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
243 return ret;
245 info->irq = ret;
247 again:
248 ret = xenbus_transaction_start(&xbt);
249 if (ret) {
250 xenbus_dev_fatal(dev, ret, "starting transaction");
251 return ret;
253 ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu",
254 virt_to_mfn(info->page));
255 if (ret)
256 goto error_xenbus;
257 ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
258 evtchn);
259 if (ret)
260 goto error_xenbus;
261 ret = xenbus_transaction_end(xbt, 0);
262 if (ret) {
263 if (ret == -EAGAIN)
264 goto again;
265 xenbus_dev_fatal(dev, ret, "completing transaction");
266 return ret;
269 xenbus_switch_state(dev, XenbusStateInitialised);
270 return 0;
272 error_xenbus:
273 xenbus_transaction_end(xbt, 1);
274 xenbus_dev_fatal(dev, ret, "writing xenstore");
275 return ret;
278 static void xenkbd_disconnect_backend(struct xenkbd_info *info)
280 if (info->irq >= 0)
281 unbind_from_irqhandler(info->irq, info);
282 info->irq = -1;
285 static void xenkbd_backend_changed(struct xenbus_device *dev,
286 enum xenbus_state backend_state)
288 struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
289 int ret, val;
291 switch (backend_state) {
292 case XenbusStateInitialising:
293 case XenbusStateInitialised:
294 case XenbusStateReconfiguring:
295 case XenbusStateReconfigured:
296 case XenbusStateUnknown:
297 case XenbusStateClosed:
298 break;
300 case XenbusStateInitWait:
301 InitWait:
302 ret = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
303 "feature-abs-pointer", "%d", &val);
304 if (ret < 0)
305 val = 0;
306 if (val) {
307 ret = xenbus_printf(XBT_NIL, info->xbdev->nodename,
308 "request-abs-pointer", "1");
309 if (ret)
310 pr_warning("can't request abs-pointer\n");
312 xenbus_switch_state(dev, XenbusStateConnected);
313 break;
315 case XenbusStateConnected:
317 * Work around xenbus race condition: If backend goes
318 * through InitWait to Connected fast enough, we can
319 * get Connected twice here.
321 if (dev->state != XenbusStateConnected)
322 goto InitWait; /* no InitWait seen yet, fudge it */
324 /* Set input abs params to match backend screen res */
325 if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
326 "width", "%d", &val) > 0)
327 input_set_abs_params(info->ptr, ABS_X, 0, val, 0, 0);
329 if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
330 "height", "%d", &val) > 0)
331 input_set_abs_params(info->ptr, ABS_Y, 0, val, 0, 0);
333 break;
335 case XenbusStateClosing:
336 xenbus_frontend_closed(dev);
337 break;
341 static const struct xenbus_device_id xenkbd_ids[] = {
342 { "vkbd" },
343 { "" }
346 static struct xenbus_driver xenkbd_driver = {
347 .name = "vkbd",
348 .owner = THIS_MODULE,
349 .ids = xenkbd_ids,
350 .probe = xenkbd_probe,
351 .remove = xenkbd_remove,
352 .resume = xenkbd_resume,
353 .otherend_changed = xenkbd_backend_changed,
356 static int __init xenkbd_init(void)
358 if (!xen_pv_domain())
359 return -ENODEV;
361 /* Nothing to do if running in dom0. */
362 if (xen_initial_domain())
363 return -ENODEV;
365 return xenbus_register_frontend(&xenkbd_driver);
368 static void __exit xenkbd_cleanup(void)
370 xenbus_unregister_driver(&xenkbd_driver);
373 module_init(xenkbd_init);
374 module_exit(xenkbd_cleanup);
376 MODULE_DESCRIPTION("Xen virtual keyboard/pointer device frontend");
377 MODULE_LICENSE("GPL");
378 MODULE_ALIAS("xen:vkbd");