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
17 * Switch to grant tables together with xen-fbfront.c.
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/module.h>
23 #include <linux/input.h>
24 #include <asm/xen/hypervisor.h>
25 #include <xen/events.h>
27 #include <xen/interface/io/fbif.h>
28 #include <xen/interface/io/kbdif.h>
29 #include <xen/xenbus.h>
32 struct input_dev *kbd;
33 struct input_dev *ptr;
34 struct xenkbd_page *page;
36 struct xenbus_device *xbdev;
40 static int xenkbd_remove(struct xenbus_device *);
41 static int xenkbd_connect_backend(struct xenbus_device *, struct xenkbd_info *);
42 static void xenkbd_disconnect_backend(struct xenkbd_info *);
45 * Note: if you need to send out events, see xenfb_do_update() for how
49 static irqreturn_t input_handler(int rq, void *dev_id)
51 struct xenkbd_info *info = dev_id;
52 struct xenkbd_page *page = info->page;
56 if (prod == page->in_cons)
58 rmb(); /* ensure we see ring contents up to prod */
59 for (cons = page->in_cons; cons != prod; cons++) {
60 union xenkbd_in_event *event;
61 struct input_dev *dev;
62 event = &XENKBD_IN_RING_REF(page, cons);
65 switch (event->type) {
66 case XENKBD_TYPE_MOTION:
67 input_report_rel(dev, REL_X, event->motion.rel_x);
68 input_report_rel(dev, REL_Y, event->motion.rel_y);
69 if (event->motion.rel_z)
70 input_report_rel(dev, REL_WHEEL,
71 -event->motion.rel_z);
75 if (test_bit(event->key.keycode, info->kbd->keybit))
77 if (test_bit(event->key.keycode, info->ptr->keybit))
80 input_report_key(dev, event->key.keycode,
84 "xenkbd: unhandled keycode 0x%x\n",
88 input_report_abs(dev, ABS_X, event->pos.abs_x);
89 input_report_abs(dev, ABS_Y, event->pos.abs_y);
91 input_report_rel(dev, REL_WHEEL,
98 mb(); /* ensure we got ring contents */
100 notify_remote_via_irq(info->irq);
105 static int __devinit xenkbd_probe(struct xenbus_device *dev,
106 const struct xenbus_device_id *id)
109 struct xenkbd_info *info;
110 struct input_dev *kbd, *ptr;
112 info = kzalloc(sizeof(*info), GFP_KERNEL);
114 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
117 dev->dev.driver_data = info;
120 snprintf(info->phys, sizeof(info->phys), "xenbus/%s", dev->nodename);
122 info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
127 kbd = input_allocate_device();
130 kbd->name = "Xen Virtual Keyboard";
131 kbd->phys = info->phys;
132 kbd->id.bustype = BUS_PCI;
133 kbd->id.vendor = 0x5853;
134 kbd->id.product = 0xffff;
135 kbd->evbit[0] = BIT(EV_KEY);
136 for (i = KEY_ESC; i < KEY_UNKNOWN; i++)
137 set_bit(i, kbd->keybit);
138 for (i = KEY_OK; i < KEY_MAX; i++)
139 set_bit(i, kbd->keybit);
141 ret = input_register_device(kbd);
143 input_free_device(kbd);
144 xenbus_dev_fatal(dev, ret, "input_register_device(kbd)");
149 /* pointing device */
150 ptr = input_allocate_device();
153 ptr->name = "Xen Virtual Pointer";
154 ptr->phys = info->phys;
155 ptr->id.bustype = BUS_PCI;
156 ptr->id.vendor = 0x5853;
157 ptr->id.product = 0xfffe;
158 ptr->evbit[0] = BIT(EV_KEY) | BIT(EV_REL) | BIT(EV_ABS);
159 for (i = BTN_LEFT; i <= BTN_TASK; i++)
160 set_bit(i, ptr->keybit);
161 ptr->relbit[0] = BIT(REL_X) | BIT(REL_Y) | BIT(REL_WHEEL);
162 input_set_abs_params(ptr, ABS_X, 0, XENFB_WIDTH, 0, 0);
163 input_set_abs_params(ptr, ABS_Y, 0, XENFB_HEIGHT, 0, 0);
165 ret = input_register_device(ptr);
167 input_free_device(ptr);
168 xenbus_dev_fatal(dev, ret, "input_register_device(ptr)");
173 ret = xenkbd_connect_backend(dev, info);
181 xenbus_dev_fatal(dev, ret, "allocating device memory");
187 static int xenkbd_resume(struct xenbus_device *dev)
189 struct xenkbd_info *info = dev->dev.driver_data;
191 xenkbd_disconnect_backend(info);
192 memset(info->page, 0, PAGE_SIZE);
193 return xenkbd_connect_backend(dev, info);
196 static int xenkbd_remove(struct xenbus_device *dev)
198 struct xenkbd_info *info = dev->dev.driver_data;
200 xenkbd_disconnect_backend(info);
202 input_unregister_device(info->kbd);
204 input_unregister_device(info->ptr);
205 free_page((unsigned long)info->page);
210 static int xenkbd_connect_backend(struct xenbus_device *dev,
211 struct xenkbd_info *info)
214 struct xenbus_transaction xbt;
216 ret = xenbus_alloc_evtchn(dev, &evtchn);
219 ret = bind_evtchn_to_irqhandler(evtchn, input_handler,
220 0, dev->devicetype, info);
222 xenbus_free_evtchn(dev, evtchn);
223 xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
229 ret = xenbus_transaction_start(&xbt);
231 xenbus_dev_fatal(dev, ret, "starting transaction");
234 ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu",
235 virt_to_mfn(info->page));
238 ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
242 ret = xenbus_transaction_end(xbt, 0);
246 xenbus_dev_fatal(dev, ret, "completing transaction");
250 xenbus_switch_state(dev, XenbusStateInitialised);
254 xenbus_transaction_end(xbt, 1);
255 xenbus_dev_fatal(dev, ret, "writing xenstore");
259 static void xenkbd_disconnect_backend(struct xenkbd_info *info)
262 unbind_from_irqhandler(info->irq, info);
266 static void xenkbd_backend_changed(struct xenbus_device *dev,
267 enum xenbus_state backend_state)
269 struct xenkbd_info *info = dev->dev.driver_data;
272 switch (backend_state) {
273 case XenbusStateInitialising:
274 case XenbusStateInitialised:
275 case XenbusStateUnknown:
276 case XenbusStateClosed:
279 case XenbusStateInitWait:
281 ret = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
282 "feature-abs-pointer", "%d", &val);
286 ret = xenbus_printf(XBT_NIL, info->xbdev->nodename,
287 "request-abs-pointer", "1");
290 "xenkbd: can't request abs-pointer");
292 xenbus_switch_state(dev, XenbusStateConnected);
295 case XenbusStateConnected:
297 * Work around xenbus race condition: If backend goes
298 * through InitWait to Connected fast enough, we can
299 * get Connected twice here.
301 if (dev->state != XenbusStateConnected)
302 goto InitWait; /* no InitWait seen yet, fudge it */
304 /* Set input abs params to match backend screen res */
305 if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
306 "width", "%d", &val) > 0)
307 input_set_abs_params(info->ptr, ABS_X, 0, val, 0, 0);
309 if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
310 "height", "%d", &val) > 0)
311 input_set_abs_params(info->ptr, ABS_Y, 0, val, 0, 0);
315 case XenbusStateClosing:
316 xenbus_frontend_closed(dev);
321 static struct xenbus_device_id xenkbd_ids[] = {
326 static struct xenbus_driver xenkbd = {
328 .owner = THIS_MODULE,
330 .probe = xenkbd_probe,
331 .remove = xenkbd_remove,
332 .resume = xenkbd_resume,
333 .otherend_changed = xenkbd_backend_changed,
336 static int __init xenkbd_init(void)
341 /* Nothing to do if running in dom0. */
342 if (xen_initial_domain())
345 return xenbus_register_frontend(&xenkbd);
348 static void __exit xenkbd_cleanup(void)
350 xenbus_unregister_driver(&xenkbd);
353 module_init(xenkbd_init);
354 module_exit(xenkbd_cleanup);
356 MODULE_DESCRIPTION("Xen virtual keyboard/pointer device frontend");
357 MODULE_LICENSE("GPL");
358 MODULE_ALIAS("xen:vkbd");