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 <asm/xen/hypervisor.h>
25 #include <xen/events.h>
27 #include <xen/interface/io/fbif.h>
28 #include <xen/interface/io/kbdif.h>
29 #include <xen/xenbus.h>
32 struct input_dev
*kbd
;
33 struct input_dev
*ptr
;
34 struct xenkbd_page
*page
;
36 struct xenbus_device
*xbdev
;
40 static int xenkbd_remove(struct xenbus_device
*);
41 static int xenkbd_connect_backend(struct xenbus_device
*, struct xenkbd_info
*);
42 static void xenkbd_disconnect_backend(struct xenkbd_info
*);
45 * Note: if you need to send out events, see xenfb_do_update() for how
49 static irqreturn_t
input_handler(int rq
, void *dev_id
)
51 struct xenkbd_info
*info
= dev_id
;
52 struct xenkbd_page
*page
= info
->page
;
56 if (prod
== page
->in_cons
)
58 rmb(); /* ensure we see ring contents up to prod */
59 for (cons
= page
->in_cons
; cons
!= prod
; cons
++) {
60 union xenkbd_in_event
*event
;
61 struct input_dev
*dev
;
62 event
= &XENKBD_IN_RING_REF(page
, cons
);
65 switch (event
->type
) {
66 case XENKBD_TYPE_MOTION
:
67 input_report_rel(dev
, REL_X
, event
->motion
.rel_x
);
68 input_report_rel(dev
, REL_Y
, event
->motion
.rel_y
);
72 if (test_bit(event
->key
.keycode
, info
->kbd
->keybit
))
74 if (test_bit(event
->key
.keycode
, info
->ptr
->keybit
))
77 input_report_key(dev
, event
->key
.keycode
,
81 "xenkbd: unhandled keycode 0x%x\n",
85 input_report_abs(dev
, ABS_X
, event
->pos
.abs_x
);
86 input_report_abs(dev
, ABS_Y
, event
->pos
.abs_y
);
92 mb(); /* ensure we got ring contents */
94 notify_remote_via_irq(info
->irq
);
99 static int __devinit
xenkbd_probe(struct xenbus_device
*dev
,
100 const struct xenbus_device_id
*id
)
103 struct xenkbd_info
*info
;
104 struct input_dev
*kbd
, *ptr
;
106 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
108 xenbus_dev_fatal(dev
, -ENOMEM
, "allocating info structure");
111 dev
->dev
.driver_data
= info
;
114 snprintf(info
->phys
, sizeof(info
->phys
), "xenbus/%s", dev
->nodename
);
116 info
->page
= (void *)__get_free_page(GFP_KERNEL
| __GFP_ZERO
);
121 kbd
= input_allocate_device();
124 kbd
->name
= "Xen Virtual Keyboard";
125 kbd
->phys
= info
->phys
;
126 kbd
->id
.bustype
= BUS_PCI
;
127 kbd
->id
.vendor
= 0x5853;
128 kbd
->id
.product
= 0xffff;
129 kbd
->evbit
[0] = BIT(EV_KEY
);
130 for (i
= KEY_ESC
; i
< KEY_UNKNOWN
; i
++)
131 set_bit(i
, kbd
->keybit
);
132 for (i
= KEY_OK
; i
< KEY_MAX
; i
++)
133 set_bit(i
, kbd
->keybit
);
135 ret
= input_register_device(kbd
);
137 input_free_device(kbd
);
138 xenbus_dev_fatal(dev
, ret
, "input_register_device(kbd)");
143 /* pointing device */
144 ptr
= input_allocate_device();
147 ptr
->name
= "Xen Virtual Pointer";
148 ptr
->phys
= info
->phys
;
149 ptr
->id
.bustype
= BUS_PCI
;
150 ptr
->id
.vendor
= 0x5853;
151 ptr
->id
.product
= 0xfffe;
152 ptr
->evbit
[0] = BIT(EV_KEY
) | BIT(EV_REL
) | BIT(EV_ABS
);
153 for (i
= BTN_LEFT
; i
<= BTN_TASK
; i
++)
154 set_bit(i
, ptr
->keybit
);
155 ptr
->relbit
[0] = BIT(REL_X
) | BIT(REL_Y
);
156 input_set_abs_params(ptr
, ABS_X
, 0, XENFB_WIDTH
, 0, 0);
157 input_set_abs_params(ptr
, ABS_Y
, 0, XENFB_HEIGHT
, 0, 0);
159 ret
= input_register_device(ptr
);
161 input_free_device(ptr
);
162 xenbus_dev_fatal(dev
, ret
, "input_register_device(ptr)");
167 ret
= xenkbd_connect_backend(dev
, info
);
175 xenbus_dev_fatal(dev
, ret
, "allocating device memory");
181 static int xenkbd_resume(struct xenbus_device
*dev
)
183 struct xenkbd_info
*info
= dev
->dev
.driver_data
;
185 xenkbd_disconnect_backend(info
);
186 memset(info
->page
, 0, PAGE_SIZE
);
187 return xenkbd_connect_backend(dev
, info
);
190 static int xenkbd_remove(struct xenbus_device
*dev
)
192 struct xenkbd_info
*info
= dev
->dev
.driver_data
;
194 xenkbd_disconnect_backend(info
);
196 input_unregister_device(info
->kbd
);
198 input_unregister_device(info
->ptr
);
199 free_page((unsigned long)info
->page
);
204 static int xenkbd_connect_backend(struct xenbus_device
*dev
,
205 struct xenkbd_info
*info
)
208 struct xenbus_transaction xbt
;
210 ret
= xenbus_alloc_evtchn(dev
, &evtchn
);
213 ret
= bind_evtchn_to_irqhandler(evtchn
, input_handler
,
214 0, dev
->devicetype
, info
);
216 xenbus_free_evtchn(dev
, evtchn
);
217 xenbus_dev_fatal(dev
, ret
, "bind_evtchn_to_irqhandler");
223 ret
= xenbus_transaction_start(&xbt
);
225 xenbus_dev_fatal(dev
, ret
, "starting transaction");
228 ret
= xenbus_printf(xbt
, dev
->nodename
, "page-ref", "%lu",
229 virt_to_mfn(info
->page
));
232 ret
= xenbus_printf(xbt
, dev
->nodename
, "event-channel", "%u",
236 ret
= xenbus_transaction_end(xbt
, 0);
240 xenbus_dev_fatal(dev
, ret
, "completing transaction");
244 xenbus_switch_state(dev
, XenbusStateInitialised
);
248 xenbus_transaction_end(xbt
, 1);
249 xenbus_dev_fatal(dev
, ret
, "writing xenstore");
253 static void xenkbd_disconnect_backend(struct xenkbd_info
*info
)
256 unbind_from_irqhandler(info
->irq
, info
);
260 static void xenkbd_backend_changed(struct xenbus_device
*dev
,
261 enum xenbus_state backend_state
)
263 struct xenkbd_info
*info
= dev
->dev
.driver_data
;
266 switch (backend_state
) {
267 case XenbusStateInitialising
:
268 case XenbusStateInitialised
:
269 case XenbusStateUnknown
:
270 case XenbusStateClosed
:
273 case XenbusStateInitWait
:
275 ret
= xenbus_scanf(XBT_NIL
, info
->xbdev
->otherend
,
276 "feature-abs-pointer", "%d", &val
);
280 ret
= xenbus_printf(XBT_NIL
, info
->xbdev
->nodename
,
281 "request-abs-pointer", "1");
284 "xenkbd: can't request abs-pointer");
286 xenbus_switch_state(dev
, XenbusStateConnected
);
289 case XenbusStateConnected
:
291 * Work around xenbus race condition: If backend goes
292 * through InitWait to Connected fast enough, we can
293 * get Connected twice here.
295 if (dev
->state
!= XenbusStateConnected
)
296 goto InitWait
; /* no InitWait seen yet, fudge it */
299 case XenbusStateClosing
:
300 xenbus_frontend_closed(dev
);
305 static struct xenbus_device_id xenkbd_ids
[] = {
310 static struct xenbus_driver xenkbd
= {
312 .owner
= THIS_MODULE
,
314 .probe
= xenkbd_probe
,
315 .remove
= xenkbd_remove
,
316 .resume
= xenkbd_resume
,
317 .otherend_changed
= xenkbd_backend_changed
,
320 static int __init
xenkbd_init(void)
322 if (!is_running_on_xen())
325 /* Nothing to do if running in dom0. */
326 if (is_initial_xendomain())
329 return xenbus_register_frontend(&xenkbd
);
332 static void __exit
xenkbd_cleanup(void)
334 xenbus_unregister_driver(&xenkbd
);
337 module_init(xenkbd_init
);
338 module_exit(xenkbd_cleanup
);
340 MODULE_LICENSE("GPL");