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 <linux/slab.h>
26 #include <asm/xen/hypervisor.h>
29 #include <xen/events.h>
31 #include <xen/interface/io/fbif.h>
32 #include <xen/interface/io/kbdif.h>
33 #include <xen/xenbus.h>
36 struct input_dev
*kbd
;
37 struct input_dev
*ptr
;
38 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
,
88 "xenkbd: unhandled keycode 0x%x\n",
92 input_report_abs(dev
, ABS_X
, event
->pos
.abs_x
);
93 input_report_abs(dev
, ABS_Y
, event
->pos
.abs_y
);
95 input_report_rel(dev
, REL_WHEEL
,
102 mb(); /* ensure we got ring contents */
103 page
->in_cons
= cons
;
104 notify_remote_via_irq(info
->irq
);
109 static int __devinit
xenkbd_probe(struct xenbus_device
*dev
,
110 const struct xenbus_device_id
*id
)
113 struct xenkbd_info
*info
;
114 struct input_dev
*kbd
, *ptr
;
116 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
118 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
121 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
);
131 kbd
= input_allocate_device();
134 kbd
->name
= "Xen Virtual Keyboard";
135 kbd
->phys
= info
->phys
;
136 kbd
->id
.bustype
= BUS_PCI
;
137 kbd
->id
.vendor
= 0x5853;
138 kbd
->id
.product
= 0xffff;
139 kbd
->evbit
[0] = BIT(EV_KEY
);
140 for (i
= KEY_ESC
; i
< KEY_UNKNOWN
; i
++)
141 set_bit(i
, kbd
->keybit
);
142 for (i
= KEY_OK
; i
< KEY_MAX
; i
++)
143 set_bit(i
, kbd
->keybit
);
145 ret
= input_register_device(kbd
);
147 input_free_device(kbd
);
148 xenbus_dev_fatal(dev
, ret
, "input_register_device(kbd)");
153 /* pointing device */
154 ptr
= input_allocate_device();
157 ptr
->name
= "Xen Virtual Pointer";
158 ptr
->phys
= info
->phys
;
159 ptr
->id
.bustype
= BUS_PCI
;
160 ptr
->id
.vendor
= 0x5853;
161 ptr
->id
.product
= 0xfffe;
162 ptr
->evbit
[0] = BIT(EV_KEY
) | BIT(EV_REL
) | BIT(EV_ABS
);
163 for (i
= BTN_LEFT
; i
<= BTN_TASK
; i
++)
164 set_bit(i
, ptr
->keybit
);
165 ptr
->relbit
[0] = BIT(REL_X
) | BIT(REL_Y
) | BIT(REL_WHEEL
);
166 input_set_abs_params(ptr
, ABS_X
, 0, XENFB_WIDTH
, 0, 0);
167 input_set_abs_params(ptr
, ABS_Y
, 0, XENFB_HEIGHT
, 0, 0);
169 ret
= input_register_device(ptr
);
171 input_free_device(ptr
);
172 xenbus_dev_fatal(dev
, ret
, "input_register_device(ptr)");
177 ret
= xenkbd_connect_backend(dev
, info
);
185 xenbus_dev_fatal(dev
, ret
, "allocating device memory");
191 static int xenkbd_resume(struct xenbus_device
*dev
)
193 struct xenkbd_info
*info
= dev_get_drvdata(&dev
->dev
);
195 xenkbd_disconnect_backend(info
);
196 memset(info
->page
, 0, PAGE_SIZE
);
197 return xenkbd_connect_backend(dev
, info
);
200 static int xenkbd_remove(struct xenbus_device
*dev
)
202 struct xenkbd_info
*info
= dev_get_drvdata(&dev
->dev
);
204 xenkbd_disconnect_backend(info
);
206 input_unregister_device(info
->kbd
);
208 input_unregister_device(info
->ptr
);
209 free_page((unsigned long)info
->page
);
214 static int xenkbd_connect_backend(struct xenbus_device
*dev
,
215 struct xenkbd_info
*info
)
218 struct xenbus_transaction xbt
;
220 ret
= xenbus_alloc_evtchn(dev
, &evtchn
);
223 ret
= bind_evtchn_to_irqhandler(evtchn
, input_handler
,
224 0, dev
->devicetype
, info
);
226 xenbus_free_evtchn(dev
, evtchn
);
227 xenbus_dev_fatal(dev
, ret
, "bind_evtchn_to_irqhandler");
233 ret
= xenbus_transaction_start(&xbt
);
235 xenbus_dev_fatal(dev
, ret
, "starting transaction");
238 ret
= xenbus_printf(xbt
, dev
->nodename
, "page-ref", "%lu",
239 virt_to_mfn(info
->page
));
242 ret
= xenbus_printf(xbt
, dev
->nodename
, "event-channel", "%u",
246 ret
= xenbus_transaction_end(xbt
, 0);
250 xenbus_dev_fatal(dev
, ret
, "completing transaction");
254 xenbus_switch_state(dev
, XenbusStateInitialised
);
258 xenbus_transaction_end(xbt
, 1);
259 xenbus_dev_fatal(dev
, ret
, "writing xenstore");
263 static void xenkbd_disconnect_backend(struct xenkbd_info
*info
)
266 unbind_from_irqhandler(info
->irq
, info
);
270 static void xenkbd_backend_changed(struct xenbus_device
*dev
,
271 enum xenbus_state backend_state
)
273 struct xenkbd_info
*info
= dev_get_drvdata(&dev
->dev
);
276 switch (backend_state
) {
277 case XenbusStateInitialising
:
278 case XenbusStateInitialised
:
279 case XenbusStateUnknown
:
280 case XenbusStateClosed
:
283 case XenbusStateInitWait
:
285 ret
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
286 "feature-abs-pointer", "%d", &val
);
290 ret
= xenbus_printf(XBT_NIL
, info
->xbdev
->nodename
,
291 "request-abs-pointer", "1");
294 "xenkbd: can't request abs-pointer");
296 xenbus_switch_state(dev
, XenbusStateConnected
);
299 case XenbusStateConnected
:
301 * Work around xenbus race condition: If backend goes
302 * through InitWait to Connected fast enough, we can
303 * get Connected twice here.
305 if (dev
->state
!= XenbusStateConnected
)
306 goto InitWait
; /* no InitWait seen yet, fudge it */
308 /* Set input abs params to match backend screen res */
309 if (xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
310 "width", "%d", &val
) > 0)
311 input_set_abs_params(info
->ptr
, ABS_X
, 0, val
, 0, 0);
313 if (xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
314 "height", "%d", &val
) > 0)
315 input_set_abs_params(info
->ptr
, ABS_Y
, 0, val
, 0, 0);
319 case XenbusStateClosing
:
320 xenbus_frontend_closed(dev
);
325 static const struct xenbus_device_id xenkbd_ids
[] = {
330 static struct xenbus_driver xenkbd_driver
= {
332 .owner
= THIS_MODULE
,
334 .probe
= xenkbd_probe
,
335 .remove
= xenkbd_remove
,
336 .resume
= xenkbd_resume
,
337 .otherend_changed
= xenkbd_backend_changed
,
340 static int __init
xenkbd_init(void)
345 /* Nothing to do if running in dom0. */
346 if (xen_initial_domain())
349 return xenbus_register_frontend(&xenkbd_driver
);
352 static void __exit
xenkbd_cleanup(void)
354 xenbus_unregister_driver(&xenkbd_driver
);
357 module_init(xenkbd_init
);
358 module_exit(xenkbd_cleanup
);
360 MODULE_DESCRIPTION("Xen virtual keyboard/pointer device frontend");
361 MODULE_LICENSE("GPL");
362 MODULE_ALIAS("xen:vkbd");