2 * linux/drivers/input/keyboard/xenkbd.c -- Xen para-virtual input device
4 * Copyright (C) 2005 Anthony Liguori <aliguori@us.ibm.com>
5 * Copyright (C) 2006 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 xenfb.c.
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/module.h>
23 #include <linux/input.h>
24 #include <asm/hypervisor.h>
25 #include <xen/evtchn.h>
26 #include <xen/interface/io/fbif.h>
27 #include <xen/interface/io/kbdif.h>
28 #include <xen/xenbus.h>
32 struct input_dev *dev;
33 struct xenkbd_page *page;
36 struct xenbus_device *xbdev;
39 static int xenkbd_remove(struct xenbus_device *);
40 static int xenkbd_connect_backend(struct xenbus_device *, struct xenkbd_info *);
41 static void xenkbd_disconnect_backend(struct xenkbd_info *);
44 * Note: if you need to send out events, see xenfb_do_update() for how
48 static irqreturn_t input_handler(int rq, void *dev_id, struct pt_regs *regs)
50 struct xenkbd_info *info = dev_id;
51 struct xenkbd_page *page = info->page;
55 if (prod == page->out_cons)
57 rmb(); /* ensure we see ring contents up to prod */
58 for (cons = page->in_cons; cons != prod; cons++) {
59 union xenkbd_in_event *event;
60 event = &XENKBD_IN_RING_REF(page, cons);
62 switch (event->type) {
63 case XENKBD_TYPE_MOTION:
64 input_report_rel(info->dev, REL_X, event->motion.rel_x);
65 input_report_rel(info->dev, REL_Y, event->motion.rel_y);
68 input_report_key(info->dev, event->key.keycode, event->key.pressed);
71 input_report_abs(info->dev, ABS_X, event->pos.abs_x);
72 input_report_abs(info->dev, ABS_Y, event->pos.abs_y);
76 input_sync(info->dev);
77 mb(); /* ensure we got ring contents */
79 notify_remote_via_evtchn(info->evtchn);
84 int __devinit xenkbd_probe(struct xenbus_device *dev,
85 const struct xenbus_device_id *id)
88 struct xenkbd_info *info;
89 struct input_dev *input_dev;
91 info = kzalloc(sizeof(*info), GFP_KERNEL);
93 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
96 dev->dev.driver_data = info;
99 info->page = (void *)__get_free_page(GFP_KERNEL);
102 info->page->in_cons = info->page->in_prod = 0;
103 info->page->out_cons = info->page->out_prod = 0;
105 input_dev = input_allocate_device();
109 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL) | BIT(EV_ABS);
110 input_dev->keybit[LONG(BTN_MOUSE)]
111 = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
112 /* TODO additional buttons */
113 input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
115 /* FIXME not sure this is quite right */
116 for (i = 0; i < 256; i++)
117 set_bit(i, input_dev->keybit);
119 input_dev->name = "Xen Virtual Keyboard/Mouse";
121 input_set_abs_params(input_dev, ABS_X, 0, XENFB_WIDTH, 0, 0);
122 input_set_abs_params(input_dev, ABS_Y, 0, XENFB_HEIGHT, 0, 0);
124 ret = input_register_device(input_dev);
126 input_free_device(input_dev);
127 xenbus_dev_fatal(dev, ret, "input_register_device");
130 info->dev = input_dev;
132 ret = xenkbd_connect_backend(dev, info);
140 xenbus_dev_fatal(dev, ret, "allocating device memory");
146 static int xenkbd_resume(struct xenbus_device *dev)
148 struct xenkbd_info *info = dev->dev.driver_data;
150 xenkbd_disconnect_backend(info);
151 return xenkbd_connect_backend(dev, info);
154 static int xenkbd_remove(struct xenbus_device *dev)
156 struct xenkbd_info *info = dev->dev.driver_data;
158 xenkbd_disconnect_backend(info);
159 input_unregister_device(info->dev);
160 free_page((unsigned long)info->page);
165 static int xenkbd_connect_backend(struct xenbus_device *dev,
166 struct xenkbd_info *info)
169 struct xenbus_transaction xbt;
171 ret = xenbus_alloc_evtchn(dev, &info->evtchn);
174 ret = bind_evtchn_to_irqhandler(info->evtchn, input_handler, 0,
177 xenbus_free_evtchn(dev, info->evtchn);
178 xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
184 ret = xenbus_transaction_start(&xbt);
186 xenbus_dev_fatal(dev, ret, "starting transaction");
189 ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu",
190 virt_to_mfn(info->page));
193 ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
197 ret = xenbus_transaction_end(xbt, 0);
201 xenbus_dev_fatal(dev, ret, "completing transaction");
205 xenbus_switch_state(dev, XenbusStateInitialised);
209 xenbus_transaction_end(xbt, 1);
210 xenbus_dev_fatal(dev, ret, "writing xenstore");
214 static void xenkbd_disconnect_backend(struct xenkbd_info *info)
217 unbind_from_irqhandler(info->irq, info);
221 static void xenkbd_backend_changed(struct xenbus_device *dev,
222 enum xenbus_state backend_state)
224 struct xenkbd_info *info = dev->dev.driver_data;
227 switch (backend_state) {
228 case XenbusStateInitialising:
229 case XenbusStateInitialised:
230 case XenbusStateUnknown:
231 case XenbusStateClosed:
234 case XenbusStateInitWait:
236 ret = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
237 "feature-abs-pointer", "%d", &val);
241 ret = xenbus_printf(XBT_NIL, info->xbdev->nodename,
242 "request-abs-pointer", "1");
246 xenbus_switch_state(dev, XenbusStateConnected);
249 case XenbusStateConnected:
251 * Work around xenbus race condition: If backend goes
252 * through InitWait to Connected fast enough, we can
253 * get Connected twice here.
255 if (dev->state != XenbusStateConnected)
256 goto InitWait; /* no InitWait seen yet, fudge it */
259 case XenbusStateClosing:
260 xenbus_frontend_closed(dev);
265 static struct xenbus_device_id xenkbd_ids[] = {
270 static struct xenbus_driver xenkbd = {
272 .owner = THIS_MODULE,
274 .probe = xenkbd_probe,
275 .remove = xenkbd_remove,
276 .resume = xenkbd_resume,
277 .otherend_changed = xenkbd_backend_changed,
280 static int __init xenkbd_init(void)
282 if (!is_running_on_xen())
285 /* Nothing to do if running in dom0. */
286 if (is_initial_xendomain())
289 return xenbus_register_frontend(&xenkbd);
292 static void __exit xenkbd_cleanup(void)
294 return xenbus_unregister_driver(&xenkbd);
297 module_init(xenkbd_init);
298 module_exit(xenkbd_cleanup);
300 MODULE_LICENSE("GPL");