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 #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>
31 #include <xen/events.h>
33 #include <xen/interface/io/fbif.h>
34 #include <xen/interface/io/kbdif.h>
35 #include <xen/xenbus.h>
38 struct input_dev
*kbd
;
39 struct input_dev
*ptr
;
40 struct xenkbd_page
*page
;
42 struct xenbus_device
*xbdev
;
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
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
;
62 if (prod
== page
->in_cons
)
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
);
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
);
81 if (test_bit(event
->key
.keycode
, info
->kbd
->keybit
))
83 if (test_bit(event
->key
.keycode
, info
->ptr
->keybit
))
86 input_report_key(dev
, event
->key
.keycode
,
89 pr_warning("unhandled keycode 0x%x\n",
93 input_report_abs(dev
, ABS_X
, event
->pos
.abs_x
);
94 input_report_abs(dev
, ABS_Y
, event
->pos
.abs_y
);
96 input_report_rel(dev
, REL_WHEEL
,
103 mb(); /* ensure we got ring contents */
104 page
->in_cons
= cons
;
105 notify_remote_via_irq(info
->irq
);
110 static int __devinit
xenkbd_probe(struct xenbus_device
*dev
,
111 const struct xenbus_device_id
*id
)
114 struct xenkbd_info
*info
;
115 struct input_dev
*kbd
, *ptr
;
117 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
119 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
122 dev_set_drvdata(&dev
->dev
, info
);
125 snprintf(info
->phys
, sizeof(info
->phys
), "xenbus/%s", dev
->nodename
);
127 info
->page
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
131 if (xenbus_scanf(XBT_NIL
, dev
->otherend
, "feature-abs-pointer", "%d", &abs
) < 0)
134 xenbus_printf(XBT_NIL
, dev
->nodename
, "request-abs-pointer", "1");
137 kbd
= input_allocate_device();
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
);
154 input_free_device(kbd
);
155 xenbus_dev_fatal(dev
, ret
, "input_register_device(kbd)");
160 /* pointing device */
161 ptr
= input_allocate_device();
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;
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);
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
);
186 input_free_device(ptr
);
187 xenbus_dev_fatal(dev
, ret
, "input_register_device(ptr)");
192 ret
= xenkbd_connect_backend(dev
, info
);
200 xenbus_dev_fatal(dev
, ret
, "allocating device memory");
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
);
221 input_unregister_device(info
->kbd
);
223 input_unregister_device(info
->ptr
);
224 free_page((unsigned long)info
->page
);
229 static int xenkbd_connect_backend(struct xenbus_device
*dev
,
230 struct xenkbd_info
*info
)
233 struct xenbus_transaction xbt
;
235 ret
= xenbus_alloc_evtchn(dev
, &evtchn
);
238 ret
= bind_evtchn_to_irqhandler(evtchn
, input_handler
,
239 0, dev
->devicetype
, info
);
241 xenbus_free_evtchn(dev
, evtchn
);
242 xenbus_dev_fatal(dev
, ret
, "bind_evtchn_to_irqhandler");
248 ret
= xenbus_transaction_start(&xbt
);
250 xenbus_dev_fatal(dev
, ret
, "starting transaction");
253 ret
= xenbus_printf(xbt
, dev
->nodename
, "page-ref", "%lu",
254 virt_to_mfn(info
->page
));
257 ret
= xenbus_printf(xbt
, dev
->nodename
, "event-channel", "%u",
261 ret
= xenbus_transaction_end(xbt
, 0);
265 xenbus_dev_fatal(dev
, ret
, "completing transaction");
269 xenbus_switch_state(dev
, XenbusStateInitialised
);
273 xenbus_transaction_end(xbt
, 1);
274 xenbus_dev_fatal(dev
, ret
, "writing xenstore");
278 static void xenkbd_disconnect_backend(struct xenkbd_info
*info
)
281 unbind_from_irqhandler(info
->irq
, info
);
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
);
291 switch (backend_state
) {
292 case XenbusStateInitialising
:
293 case XenbusStateInitialised
:
294 case XenbusStateReconfiguring
:
295 case XenbusStateReconfigured
:
296 case XenbusStateUnknown
:
297 case XenbusStateClosed
:
300 case XenbusStateInitWait
:
302 ret
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
303 "feature-abs-pointer", "%d", &val
);
307 ret
= xenbus_printf(XBT_NIL
, info
->xbdev
->nodename
,
308 "request-abs-pointer", "1");
310 pr_warning("can't request abs-pointer\n");
312 xenbus_switch_state(dev
, XenbusStateConnected
);
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);
335 case XenbusStateClosing
:
336 xenbus_frontend_closed(dev
);
341 static const struct xenbus_device_id xenkbd_ids
[] = {
346 static struct xenbus_driver xenkbd_driver
= {
348 .owner
= THIS_MODULE
,
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())
361 /* Nothing to do if running in dom0. */
362 if (xen_initial_domain())
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");