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>
25 #include <asm/xen/hypervisor.h>
28 #include <xen/events.h>
30 #include <xen/interface/io/fbif.h>
31 #include <xen/interface/io/kbdif.h>
32 #include <xen/xenbus.h>
35 struct input_dev
*kbd
;
36 struct input_dev
*ptr
;
37 struct xenkbd_page
*page
;
39 struct xenbus_device
*xbdev
;
43 static int xenkbd_remove(struct xenbus_device
*);
44 static int xenkbd_connect_backend(struct xenbus_device
*, struct xenkbd_info
*);
45 static void xenkbd_disconnect_backend(struct xenkbd_info
*);
48 * Note: if you need to send out events, see xenfb_do_update() for how
52 static irqreturn_t
input_handler(int rq
, void *dev_id
)
54 struct xenkbd_info
*info
= dev_id
;
55 struct xenkbd_page
*page
= info
->page
;
59 if (prod
== page
->in_cons
)
61 rmb(); /* ensure we see ring contents up to prod */
62 for (cons
= page
->in_cons
; cons
!= prod
; cons
++) {
63 union xenkbd_in_event
*event
;
64 struct input_dev
*dev
;
65 event
= &XENKBD_IN_RING_REF(page
, cons
);
68 switch (event
->type
) {
69 case XENKBD_TYPE_MOTION
:
70 input_report_rel(dev
, REL_X
, event
->motion
.rel_x
);
71 input_report_rel(dev
, REL_Y
, event
->motion
.rel_y
);
72 if (event
->motion
.rel_z
)
73 input_report_rel(dev
, REL_WHEEL
,
74 -event
->motion
.rel_z
);
78 if (test_bit(event
->key
.keycode
, info
->kbd
->keybit
))
80 if (test_bit(event
->key
.keycode
, info
->ptr
->keybit
))
83 input_report_key(dev
, event
->key
.keycode
,
87 "xenkbd: 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 __devinit
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
);
123 snprintf(info
->phys
, sizeof(info
->phys
), "xenbus/%s", dev
->nodename
);
125 info
->page
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
130 kbd
= input_allocate_device();
133 kbd
->name
= "Xen Virtual Keyboard";
134 kbd
->phys
= info
->phys
;
135 kbd
->id
.bustype
= BUS_PCI
;
136 kbd
->id
.vendor
= 0x5853;
137 kbd
->id
.product
= 0xffff;
138 kbd
->evbit
[0] = BIT(EV_KEY
);
139 for (i
= KEY_ESC
; i
< KEY_UNKNOWN
; i
++)
140 set_bit(i
, kbd
->keybit
);
141 for (i
= KEY_OK
; i
< KEY_MAX
; i
++)
142 set_bit(i
, kbd
->keybit
);
144 ret
= input_register_device(kbd
);
146 input_free_device(kbd
);
147 xenbus_dev_fatal(dev
, ret
, "input_register_device(kbd)");
152 /* pointing device */
153 ptr
= input_allocate_device();
156 ptr
->name
= "Xen Virtual Pointer";
157 ptr
->phys
= info
->phys
;
158 ptr
->id
.bustype
= BUS_PCI
;
159 ptr
->id
.vendor
= 0x5853;
160 ptr
->id
.product
= 0xfffe;
161 ptr
->evbit
[0] = BIT(EV_KEY
) | BIT(EV_REL
) | BIT(EV_ABS
);
162 for (i
= BTN_LEFT
; i
<= BTN_TASK
; i
++)
163 set_bit(i
, ptr
->keybit
);
164 ptr
->relbit
[0] = BIT(REL_X
) | BIT(REL_Y
) | BIT(REL_WHEEL
);
165 input_set_abs_params(ptr
, ABS_X
, 0, XENFB_WIDTH
, 0, 0);
166 input_set_abs_params(ptr
, ABS_Y
, 0, XENFB_HEIGHT
, 0, 0);
168 ret
= input_register_device(ptr
);
170 input_free_device(ptr
);
171 xenbus_dev_fatal(dev
, ret
, "input_register_device(ptr)");
176 ret
= xenkbd_connect_backend(dev
, info
);
184 xenbus_dev_fatal(dev
, ret
, "allocating device memory");
190 static int xenkbd_resume(struct xenbus_device
*dev
)
192 struct xenkbd_info
*info
= dev_get_drvdata(&dev
->dev
);
194 xenkbd_disconnect_backend(info
);
195 memset(info
->page
, 0, PAGE_SIZE
);
196 return xenkbd_connect_backend(dev
, info
);
199 static int xenkbd_remove(struct xenbus_device
*dev
)
201 struct xenkbd_info
*info
= dev_get_drvdata(&dev
->dev
);
203 xenkbd_disconnect_backend(info
);
205 input_unregister_device(info
->kbd
);
207 input_unregister_device(info
->ptr
);
208 free_page((unsigned long)info
->page
);
213 static int xenkbd_connect_backend(struct xenbus_device
*dev
,
214 struct xenkbd_info
*info
)
217 struct xenbus_transaction xbt
;
219 ret
= xenbus_alloc_evtchn(dev
, &evtchn
);
222 ret
= bind_evtchn_to_irqhandler(evtchn
, input_handler
,
223 0, dev
->devicetype
, info
);
225 xenbus_free_evtchn(dev
, evtchn
);
226 xenbus_dev_fatal(dev
, ret
, "bind_evtchn_to_irqhandler");
232 ret
= xenbus_transaction_start(&xbt
);
234 xenbus_dev_fatal(dev
, ret
, "starting transaction");
237 ret
= xenbus_printf(xbt
, dev
->nodename
, "page-ref", "%lu",
238 virt_to_mfn(info
->page
));
241 ret
= xenbus_printf(xbt
, dev
->nodename
, "event-channel", "%u",
245 ret
= xenbus_transaction_end(xbt
, 0);
249 xenbus_dev_fatal(dev
, ret
, "completing transaction");
253 xenbus_switch_state(dev
, XenbusStateInitialised
);
257 xenbus_transaction_end(xbt
, 1);
258 xenbus_dev_fatal(dev
, ret
, "writing xenstore");
262 static void xenkbd_disconnect_backend(struct xenkbd_info
*info
)
265 unbind_from_irqhandler(info
->irq
, info
);
269 static void xenkbd_backend_changed(struct xenbus_device
*dev
,
270 enum xenbus_state backend_state
)
272 struct xenkbd_info
*info
= dev_get_drvdata(&dev
->dev
);
275 switch (backend_state
) {
276 case XenbusStateInitialising
:
277 case XenbusStateInitialised
:
278 case XenbusStateUnknown
:
279 case XenbusStateClosed
:
282 case XenbusStateInitWait
:
284 ret
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
285 "feature-abs-pointer", "%d", &val
);
289 ret
= xenbus_printf(XBT_NIL
, info
->xbdev
->nodename
,
290 "request-abs-pointer", "1");
293 "xenkbd: can't request abs-pointer");
295 xenbus_switch_state(dev
, XenbusStateConnected
);
298 case XenbusStateConnected
:
300 * Work around xenbus race condition: If backend goes
301 * through InitWait to Connected fast enough, we can
302 * get Connected twice here.
304 if (dev
->state
!= XenbusStateConnected
)
305 goto InitWait
; /* no InitWait seen yet, fudge it */
307 /* Set input abs params to match backend screen res */
308 if (xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
309 "width", "%d", &val
) > 0)
310 input_set_abs_params(info
->ptr
, ABS_X
, 0, val
, 0, 0);
312 if (xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
313 "height", "%d", &val
) > 0)
314 input_set_abs_params(info
->ptr
, ABS_Y
, 0, val
, 0, 0);
318 case XenbusStateClosing
:
319 xenbus_frontend_closed(dev
);
324 static const struct xenbus_device_id xenkbd_ids
[] = {
329 static struct xenbus_driver xenkbd_driver
= {
331 .owner
= THIS_MODULE
,
333 .probe
= xenkbd_probe
,
334 .remove
= xenkbd_remove
,
335 .resume
= xenkbd_resume
,
336 .otherend_changed
= xenkbd_backend_changed
,
339 static int __init
xenkbd_init(void)
344 /* Nothing to do if running in dom0. */
345 if (xen_initial_domain())
348 return xenbus_register_frontend(&xenkbd_driver
);
351 static void __exit
xenkbd_cleanup(void)
353 xenbus_unregister_driver(&xenkbd_driver
);
356 module_init(xenkbd_init
);
357 module_exit(xenkbd_cleanup
);
359 MODULE_DESCRIPTION("Xen virtual keyboard/pointer device frontend");
360 MODULE_LICENSE("GPL");
361 MODULE_ALIAS("xen:vkbd");