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
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/module.h>
19 #include <linux/input.h>
20 #include <linux/slab.h>
22 #include <asm/xen/hypervisor.h>
25 #include <xen/events.h>
27 #include <xen/grant_table.h>
28 #include <xen/interface/grant_table.h>
29 #include <xen/interface/io/fbif.h>
30 #include <xen/interface/io/kbdif.h>
31 #include <xen/xenbus.h>
32 #include <xen/platform_pci.h>
35 struct input_dev
*kbd
;
36 struct input_dev
*ptr
;
37 struct xenkbd_page
*page
;
40 struct xenbus_device
*xbdev
;
44 static int xenkbd_remove(struct xenbus_device
*);
45 static int xenkbd_connect_backend(struct xenbus_device
*, struct xenkbd_info
*);
46 static void xenkbd_disconnect_backend(struct xenkbd_info
*);
49 * Note: if you need to send out events, see xenfb_do_update() for how
53 static irqreturn_t
input_handler(int rq
, void *dev_id
)
55 struct xenkbd_info
*info
= dev_id
;
56 struct xenkbd_page
*page
= info
->page
;
60 if (prod
== page
->in_cons
)
62 rmb(); /* ensure we see ring contents up to prod */
63 for (cons
= page
->in_cons
; cons
!= prod
; cons
++) {
64 union xenkbd_in_event
*event
;
65 struct input_dev
*dev
;
66 event
= &XENKBD_IN_RING_REF(page
, cons
);
69 switch (event
->type
) {
70 case XENKBD_TYPE_MOTION
:
71 input_report_rel(dev
, REL_X
, event
->motion
.rel_x
);
72 input_report_rel(dev
, REL_Y
, event
->motion
.rel_y
);
73 if (event
->motion
.rel_z
)
74 input_report_rel(dev
, REL_WHEEL
,
75 -event
->motion
.rel_z
);
79 if (test_bit(event
->key
.keycode
, info
->kbd
->keybit
))
81 if (test_bit(event
->key
.keycode
, info
->ptr
->keybit
))
84 input_report_key(dev
, event
->key
.keycode
,
87 pr_warning("unhandled keycode 0x%x\n",
91 input_report_abs(dev
, ABS_X
, event
->pos
.abs_x
);
92 input_report_abs(dev
, ABS_Y
, event
->pos
.abs_y
);
94 input_report_rel(dev
, REL_WHEEL
,
101 mb(); /* ensure we got ring contents */
102 page
->in_cons
= cons
;
103 notify_remote_via_irq(info
->irq
);
108 static int xenkbd_probe(struct xenbus_device
*dev
,
109 const struct xenbus_device_id
*id
)
112 struct xenkbd_info
*info
;
113 struct input_dev
*kbd
, *ptr
;
115 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
117 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
120 dev_set_drvdata(&dev
->dev
, info
);
124 snprintf(info
->phys
, sizeof(info
->phys
), "xenbus/%s", dev
->nodename
);
126 info
->page
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
130 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "feature-abs-pointer", "%d", &abs
) < 0)
133 xenbus_printf(XBT_NIL
, dev
->nodename
, "request-abs-pointer", "1");
136 kbd
= input_allocate_device();
139 kbd
->name
= "Xen Virtual Keyboard";
140 kbd
->phys
= info
->phys
;
141 kbd
->id
.bustype
= BUS_PCI
;
142 kbd
->id
.vendor
= 0x5853;
143 kbd
->id
.product
= 0xffff;
145 __set_bit(EV_KEY
, kbd
->evbit
);
146 for (i
= KEY_ESC
; i
< KEY_UNKNOWN
; i
++)
147 __set_bit(i
, kbd
->keybit
);
148 for (i
= KEY_OK
; i
< KEY_MAX
; i
++)
149 __set_bit(i
, kbd
->keybit
);
151 ret
= input_register_device(kbd
);
153 input_free_device(kbd
);
154 xenbus_dev_fatal(dev
, ret
, "input_register_device(kbd)");
159 /* pointing device */
160 ptr
= input_allocate_device();
163 ptr
->name
= "Xen Virtual Pointer";
164 ptr
->phys
= info
->phys
;
165 ptr
->id
.bustype
= BUS_PCI
;
166 ptr
->id
.vendor
= 0x5853;
167 ptr
->id
.product
= 0xfffe;
170 __set_bit(EV_ABS
, ptr
->evbit
);
171 input_set_abs_params(ptr
, ABS_X
, 0, XENFB_WIDTH
, 0, 0);
172 input_set_abs_params(ptr
, ABS_Y
, 0, XENFB_HEIGHT
, 0, 0);
174 input_set_capability(ptr
, EV_REL
, REL_X
);
175 input_set_capability(ptr
, EV_REL
, REL_Y
);
177 input_set_capability(ptr
, EV_REL
, REL_WHEEL
);
179 __set_bit(EV_KEY
, ptr
->evbit
);
180 for (i
= BTN_LEFT
; i
<= BTN_TASK
; i
++)
181 __set_bit(i
, ptr
->keybit
);
183 ret
= input_register_device(ptr
);
185 input_free_device(ptr
);
186 xenbus_dev_fatal(dev
, ret
, "input_register_device(ptr)");
191 ret
= xenkbd_connect_backend(dev
, info
);
199 xenbus_dev_fatal(dev
, ret
, "allocating device memory");
205 static int xenkbd_resume(struct xenbus_device
*dev
)
207 struct xenkbd_info
*info
= dev_get_drvdata(&dev
->dev
);
209 xenkbd_disconnect_backend(info
);
210 memset(info
->page
, 0, PAGE_SIZE
);
211 return xenkbd_connect_backend(dev
, info
);
214 static int xenkbd_remove(struct xenbus_device
*dev
)
216 struct xenkbd_info
*info
= dev_get_drvdata(&dev
->dev
);
218 xenkbd_disconnect_backend(info
);
220 input_unregister_device(info
->kbd
);
222 input_unregister_device(info
->ptr
);
223 free_page((unsigned long)info
->page
);
228 static int xenkbd_connect_backend(struct xenbus_device
*dev
,
229 struct xenkbd_info
*info
)
232 struct xenbus_transaction xbt
;
234 ret
= gnttab_grant_foreign_access(dev
->otherend_id
,
235 virt_to_mfn(info
->page
), 0);
240 ret
= xenbus_alloc_evtchn(dev
, &evtchn
);
243 ret
= bind_evtchn_to_irqhandler(evtchn
, input_handler
,
244 0, dev
->devicetype
, info
);
246 xenbus_dev_fatal(dev
, ret
, "bind_evtchn_to_irqhandler");
252 ret
= xenbus_transaction_start(&xbt
);
254 xenbus_dev_fatal(dev
, ret
, "starting transaction");
257 ret
= xenbus_printf(xbt
, dev
->nodename
, "page-ref", "%lu",
258 virt_to_mfn(info
->page
));
261 ret
= xenbus_printf(xbt
, dev
->nodename
, "page-gref", "%u", info
->gref
);
264 ret
= xenbus_printf(xbt
, dev
->nodename
, "event-channel", "%u",
268 ret
= xenbus_transaction_end(xbt
, 0);
272 xenbus_dev_fatal(dev
, ret
, "completing transaction");
276 xenbus_switch_state(dev
, XenbusStateInitialised
);
280 xenbus_transaction_end(xbt
, 1);
281 xenbus_dev_fatal(dev
, ret
, "writing xenstore");
283 unbind_from_irqhandler(info
->irq
, info
);
286 xenbus_free_evtchn(dev
, evtchn
);
288 gnttab_end_foreign_access_ref(info
->gref
, 0);
293 static void xenkbd_disconnect_backend(struct xenkbd_info
*info
)
296 unbind_from_irqhandler(info
->irq
, info
);
299 gnttab_end_foreign_access_ref(info
->gref
, 0);
303 static void xenkbd_backend_changed(struct xenbus_device
*dev
,
304 enum xenbus_state backend_state
)
306 struct xenkbd_info
*info
= dev_get_drvdata(&dev
->dev
);
309 switch (backend_state
) {
310 case XenbusStateInitialising
:
311 case XenbusStateInitialised
:
312 case XenbusStateReconfiguring
:
313 case XenbusStateReconfigured
:
314 case XenbusStateUnknown
:
317 case XenbusStateInitWait
:
319 ret
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
320 "feature-abs-pointer", "%d", &val
);
324 ret
= xenbus_printf(XBT_NIL
, info
->xbdev
->nodename
,
325 "request-abs-pointer", "1");
327 pr_warning("xenkbd: can't request abs-pointer");
330 xenbus_switch_state(dev
, XenbusStateConnected
);
333 case XenbusStateConnected
:
335 * Work around xenbus race condition: If backend goes
336 * through InitWait to Connected fast enough, we can
337 * get Connected twice here.
339 if (dev
->state
!= XenbusStateConnected
)
340 goto InitWait
; /* no InitWait seen yet, fudge it */
342 /* Set input abs params to match backend screen res */
343 if (xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
344 "width", "%d", &val
) > 0)
345 input_set_abs_params(info
->ptr
, ABS_X
, 0, val
, 0, 0);
347 if (xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
348 "height", "%d", &val
) > 0)
349 input_set_abs_params(info
->ptr
, ABS_Y
, 0, val
, 0, 0);
353 case XenbusStateClosed
:
354 if (dev
->state
== XenbusStateClosed
)
356 /* Missed the backend's CLOSING state -- fallthrough */
357 case XenbusStateClosing
:
358 xenbus_frontend_closed(dev
);
363 static const struct xenbus_device_id xenkbd_ids
[] = {
368 static DEFINE_XENBUS_DRIVER(xenkbd
, ,
369 .probe
= xenkbd_probe
,
370 .remove
= xenkbd_remove
,
371 .resume
= xenkbd_resume
,
372 .otherend_changed
= xenkbd_backend_changed
,
375 static int __init
xenkbd_init(void)
380 /* Nothing to do if running in dom0. */
381 if (xen_initial_domain())
384 if (!xen_has_pv_devices())
387 return xenbus_register_frontend(&xenkbd_driver
);
390 static void __exit
xenkbd_cleanup(void)
392 xenbus_unregister_driver(&xenkbd_driver
);
395 module_init(xenkbd_init
);
396 module_exit(xenkbd_cleanup
);
398 MODULE_DESCRIPTION("Xen virtual keyboard/pointer device frontend");
399 MODULE_LICENSE("GPL");
400 MODULE_ALIAS("xen:vkbd");