Loading...
1/*
2 * Xen para-virtual input device
3 *
4 * Copyright (C) 2005 Anthony Liguori <aliguori@us.ibm.com>
5 * Copyright (C) 2006-2008 Red Hat, Inc., Markus Armbruster <armbru@redhat.com>
6 *
7 * Based on linux/drivers/input/mouse/sermouse.c
8 *
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
11 * more details.
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/module.h>
19#include <linux/input.h>
20#include <linux/input/mt.h>
21#include <linux/slab.h>
22
23#include <asm/xen/hypervisor.h>
24
25#include <xen/xen.h>
26#include <xen/events.h>
27#include <xen/page.h>
28#include <xen/grant_table.h>
29#include <xen/interface/grant_table.h>
30#include <xen/interface/io/fbif.h>
31#include <xen/interface/io/kbdif.h>
32#include <xen/xenbus.h>
33#include <xen/platform_pci.h>
34
35struct xenkbd_info {
36 struct input_dev *kbd;
37 struct input_dev *ptr;
38 struct input_dev *mtouch;
39 struct xenkbd_page *page;
40 int gref;
41 int irq;
42 struct xenbus_device *xbdev;
43 char phys[32];
44 /* current MT slot/contact ID we are injecting events in */
45 int mtouch_cur_contact_id;
46};
47
48enum { KPARAM_X, KPARAM_Y, KPARAM_CNT };
49static int ptr_size[KPARAM_CNT] = { XENFB_WIDTH, XENFB_HEIGHT };
50module_param_array(ptr_size, int, NULL, 0444);
51MODULE_PARM_DESC(ptr_size,
52 "Pointing device width, height in pixels (default 800,600)");
53
54static void xenkbd_remove(struct xenbus_device *);
55static int xenkbd_connect_backend(struct xenbus_device *, struct xenkbd_info *);
56static void xenkbd_disconnect_backend(struct xenkbd_info *);
57
58/*
59 * Note: if you need to send out events, see xenfb_do_update() for how
60 * to do that.
61 */
62
63static void xenkbd_handle_motion_event(struct xenkbd_info *info,
64 struct xenkbd_motion *motion)
65{
66 if (unlikely(!info->ptr))
67 return;
68
69 input_report_rel(info->ptr, REL_X, motion->rel_x);
70 input_report_rel(info->ptr, REL_Y, motion->rel_y);
71 if (motion->rel_z)
72 input_report_rel(info->ptr, REL_WHEEL, -motion->rel_z);
73 input_sync(info->ptr);
74}
75
76static void xenkbd_handle_position_event(struct xenkbd_info *info,
77 struct xenkbd_position *pos)
78{
79 if (unlikely(!info->ptr))
80 return;
81
82 input_report_abs(info->ptr, ABS_X, pos->abs_x);
83 input_report_abs(info->ptr, ABS_Y, pos->abs_y);
84 if (pos->rel_z)
85 input_report_rel(info->ptr, REL_WHEEL, -pos->rel_z);
86 input_sync(info->ptr);
87}
88
89static void xenkbd_handle_key_event(struct xenkbd_info *info,
90 struct xenkbd_key *key)
91{
92 struct input_dev *dev;
93 int value = key->pressed;
94
95 if (test_bit(key->keycode, info->ptr->keybit)) {
96 dev = info->ptr;
97 } else if (test_bit(key->keycode, info->kbd->keybit)) {
98 dev = info->kbd;
99 if (key->pressed && test_bit(key->keycode, info->kbd->key))
100 value = 2; /* Mark as autorepeat */
101 } else {
102 pr_warn("unhandled keycode 0x%x\n", key->keycode);
103 return;
104 }
105
106 if (unlikely(!dev))
107 return;
108
109 input_event(dev, EV_KEY, key->keycode, value);
110 input_sync(dev);
111}
112
113static void xenkbd_handle_mt_event(struct xenkbd_info *info,
114 struct xenkbd_mtouch *mtouch)
115{
116 if (unlikely(!info->mtouch))
117 return;
118
119 if (mtouch->contact_id != info->mtouch_cur_contact_id) {
120 info->mtouch_cur_contact_id = mtouch->contact_id;
121 input_mt_slot(info->mtouch, mtouch->contact_id);
122 }
123
124 switch (mtouch->event_type) {
125 case XENKBD_MT_EV_DOWN:
126 input_mt_report_slot_state(info->mtouch, MT_TOOL_FINGER, true);
127 fallthrough;
128
129 case XENKBD_MT_EV_MOTION:
130 input_report_abs(info->mtouch, ABS_MT_POSITION_X,
131 mtouch->u.pos.abs_x);
132 input_report_abs(info->mtouch, ABS_MT_POSITION_Y,
133 mtouch->u.pos.abs_y);
134 break;
135
136 case XENKBD_MT_EV_SHAPE:
137 input_report_abs(info->mtouch, ABS_MT_TOUCH_MAJOR,
138 mtouch->u.shape.major);
139 input_report_abs(info->mtouch, ABS_MT_TOUCH_MINOR,
140 mtouch->u.shape.minor);
141 break;
142
143 case XENKBD_MT_EV_ORIENT:
144 input_report_abs(info->mtouch, ABS_MT_ORIENTATION,
145 mtouch->u.orientation);
146 break;
147
148 case XENKBD_MT_EV_UP:
149 input_mt_report_slot_inactive(info->mtouch);
150 break;
151
152 case XENKBD_MT_EV_SYN:
153 input_mt_sync_frame(info->mtouch);
154 input_sync(info->mtouch);
155 break;
156 }
157}
158
159static void xenkbd_handle_event(struct xenkbd_info *info,
160 union xenkbd_in_event *event)
161{
162 switch (event->type) {
163 case XENKBD_TYPE_MOTION:
164 xenkbd_handle_motion_event(info, &event->motion);
165 break;
166
167 case XENKBD_TYPE_KEY:
168 xenkbd_handle_key_event(info, &event->key);
169 break;
170
171 case XENKBD_TYPE_POS:
172 xenkbd_handle_position_event(info, &event->pos);
173 break;
174
175 case XENKBD_TYPE_MTOUCH:
176 xenkbd_handle_mt_event(info, &event->mtouch);
177 break;
178 }
179}
180
181static irqreturn_t input_handler(int rq, void *dev_id)
182{
183 struct xenkbd_info *info = dev_id;
184 struct xenkbd_page *page = info->page;
185 __u32 cons, prod;
186
187 prod = page->in_prod;
188 if (prod == page->in_cons)
189 return IRQ_HANDLED;
190 rmb(); /* ensure we see ring contents up to prod */
191 for (cons = page->in_cons; cons != prod; cons++)
192 xenkbd_handle_event(info, &XENKBD_IN_RING_REF(page, cons));
193 mb(); /* ensure we got ring contents */
194 page->in_cons = cons;
195 notify_remote_via_irq(info->irq);
196
197 return IRQ_HANDLED;
198}
199
200static int xenkbd_probe(struct xenbus_device *dev,
201 const struct xenbus_device_id *id)
202{
203 int ret, i;
204 bool with_mtouch, with_kbd, with_ptr;
205 struct xenkbd_info *info;
206 struct input_dev *kbd, *ptr, *mtouch;
207
208 info = kzalloc(sizeof(*info), GFP_KERNEL);
209 if (!info) {
210 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
211 return -ENOMEM;
212 }
213 dev_set_drvdata(&dev->dev, info);
214 info->xbdev = dev;
215 info->irq = -1;
216 info->gref = -1;
217 snprintf(info->phys, sizeof(info->phys), "xenbus/%s", dev->nodename);
218
219 info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
220 if (!info->page)
221 goto error_nomem;
222
223 /*
224 * The below are reverse logic, e.g. if the feature is set, then
225 * do not expose the corresponding virtual device.
226 */
227 with_kbd = !xenbus_read_unsigned(dev->otherend,
228 XENKBD_FIELD_FEAT_DSBL_KEYBRD, 0);
229
230 with_ptr = !xenbus_read_unsigned(dev->otherend,
231 XENKBD_FIELD_FEAT_DSBL_POINTER, 0);
232
233 /* Direct logic: if set, then create multi-touch device. */
234 with_mtouch = xenbus_read_unsigned(dev->otherend,
235 XENKBD_FIELD_FEAT_MTOUCH, 0);
236 if (with_mtouch) {
237 ret = xenbus_write(XBT_NIL, dev->nodename,
238 XENKBD_FIELD_REQ_MTOUCH, "1");
239 if (ret) {
240 pr_warn("xenkbd: can't request multi-touch");
241 with_mtouch = 0;
242 }
243 }
244
245 /* keyboard */
246 if (with_kbd) {
247 kbd = input_allocate_device();
248 if (!kbd)
249 goto error_nomem;
250 kbd->name = "Xen Virtual Keyboard";
251 kbd->phys = info->phys;
252 kbd->id.bustype = BUS_PCI;
253 kbd->id.vendor = 0x5853;
254 kbd->id.product = 0xffff;
255
256 __set_bit(EV_KEY, kbd->evbit);
257 for (i = KEY_ESC; i < KEY_UNKNOWN; i++)
258 __set_bit(i, kbd->keybit);
259 for (i = KEY_OK; i < KEY_MAX; i++)
260 __set_bit(i, kbd->keybit);
261
262 ret = input_register_device(kbd);
263 if (ret) {
264 input_free_device(kbd);
265 xenbus_dev_fatal(dev, ret,
266 "input_register_device(kbd)");
267 goto error;
268 }
269 info->kbd = kbd;
270 }
271
272 /* pointing device */
273 if (with_ptr) {
274 unsigned int abs;
275
276 /* Set input abs params to match backend screen res */
277 abs = xenbus_read_unsigned(dev->otherend,
278 XENKBD_FIELD_FEAT_ABS_POINTER, 0);
279 ptr_size[KPARAM_X] = xenbus_read_unsigned(dev->otherend,
280 XENKBD_FIELD_WIDTH,
281 ptr_size[KPARAM_X]);
282 ptr_size[KPARAM_Y] = xenbus_read_unsigned(dev->otherend,
283 XENKBD_FIELD_HEIGHT,
284 ptr_size[KPARAM_Y]);
285 if (abs) {
286 ret = xenbus_write(XBT_NIL, dev->nodename,
287 XENKBD_FIELD_REQ_ABS_POINTER, "1");
288 if (ret) {
289 pr_warn("xenkbd: can't request abs-pointer\n");
290 abs = 0;
291 }
292 }
293
294 ptr = input_allocate_device();
295 if (!ptr)
296 goto error_nomem;
297 ptr->name = "Xen Virtual Pointer";
298 ptr->phys = info->phys;
299 ptr->id.bustype = BUS_PCI;
300 ptr->id.vendor = 0x5853;
301 ptr->id.product = 0xfffe;
302
303 if (abs) {
304 __set_bit(EV_ABS, ptr->evbit);
305 input_set_abs_params(ptr, ABS_X, 0,
306 ptr_size[KPARAM_X], 0, 0);
307 input_set_abs_params(ptr, ABS_Y, 0,
308 ptr_size[KPARAM_Y], 0, 0);
309 } else {
310 input_set_capability(ptr, EV_REL, REL_X);
311 input_set_capability(ptr, EV_REL, REL_Y);
312 }
313 input_set_capability(ptr, EV_REL, REL_WHEEL);
314
315 __set_bit(EV_KEY, ptr->evbit);
316 for (i = BTN_LEFT; i <= BTN_TASK; i++)
317 __set_bit(i, ptr->keybit);
318
319 ret = input_register_device(ptr);
320 if (ret) {
321 input_free_device(ptr);
322 xenbus_dev_fatal(dev, ret,
323 "input_register_device(ptr)");
324 goto error;
325 }
326 info->ptr = ptr;
327 }
328
329 /* multi-touch device */
330 if (with_mtouch) {
331 int num_cont, width, height;
332
333 mtouch = input_allocate_device();
334 if (!mtouch)
335 goto error_nomem;
336
337 num_cont = xenbus_read_unsigned(info->xbdev->otherend,
338 XENKBD_FIELD_MT_NUM_CONTACTS,
339 1);
340 width = xenbus_read_unsigned(info->xbdev->otherend,
341 XENKBD_FIELD_MT_WIDTH,
342 XENFB_WIDTH);
343 height = xenbus_read_unsigned(info->xbdev->otherend,
344 XENKBD_FIELD_MT_HEIGHT,
345 XENFB_HEIGHT);
346
347 mtouch->name = "Xen Virtual Multi-touch";
348 mtouch->phys = info->phys;
349 mtouch->id.bustype = BUS_PCI;
350 mtouch->id.vendor = 0x5853;
351 mtouch->id.product = 0xfffd;
352
353 input_set_abs_params(mtouch, ABS_MT_TOUCH_MAJOR,
354 0, 255, 0, 0);
355 input_set_abs_params(mtouch, ABS_MT_POSITION_X,
356 0, width, 0, 0);
357 input_set_abs_params(mtouch, ABS_MT_POSITION_Y,
358 0, height, 0, 0);
359
360 ret = input_mt_init_slots(mtouch, num_cont, INPUT_MT_DIRECT);
361 if (ret) {
362 input_free_device(mtouch);
363 xenbus_dev_fatal(info->xbdev, ret,
364 "input_mt_init_slots");
365 goto error;
366 }
367
368 ret = input_register_device(mtouch);
369 if (ret) {
370 input_free_device(mtouch);
371 xenbus_dev_fatal(info->xbdev, ret,
372 "input_register_device(mtouch)");
373 goto error;
374 }
375 info->mtouch_cur_contact_id = -1;
376 info->mtouch = mtouch;
377 }
378
379 if (!(with_kbd || with_ptr || with_mtouch)) {
380 ret = -ENXIO;
381 goto error;
382 }
383
384 ret = xenkbd_connect_backend(dev, info);
385 if (ret < 0)
386 goto error;
387
388 return 0;
389
390 error_nomem:
391 ret = -ENOMEM;
392 xenbus_dev_fatal(dev, ret, "allocating device memory");
393 error:
394 xenkbd_remove(dev);
395 return ret;
396}
397
398static int xenkbd_resume(struct xenbus_device *dev)
399{
400 struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
401
402 xenkbd_disconnect_backend(info);
403 memset(info->page, 0, PAGE_SIZE);
404 return xenkbd_connect_backend(dev, info);
405}
406
407static void xenkbd_remove(struct xenbus_device *dev)
408{
409 struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
410
411 xenkbd_disconnect_backend(info);
412 if (info->kbd)
413 input_unregister_device(info->kbd);
414 if (info->ptr)
415 input_unregister_device(info->ptr);
416 if (info->mtouch)
417 input_unregister_device(info->mtouch);
418 free_page((unsigned long)info->page);
419 kfree(info);
420}
421
422static int xenkbd_connect_backend(struct xenbus_device *dev,
423 struct xenkbd_info *info)
424{
425 int ret, evtchn;
426 struct xenbus_transaction xbt;
427
428 ret = gnttab_grant_foreign_access(dev->otherend_id,
429 virt_to_gfn(info->page), 0);
430 if (ret < 0)
431 return ret;
432 info->gref = ret;
433
434 ret = xenbus_alloc_evtchn(dev, &evtchn);
435 if (ret)
436 goto error_grant;
437 ret = bind_evtchn_to_irqhandler(evtchn, input_handler,
438 0, dev->devicetype, info);
439 if (ret < 0) {
440 xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
441 goto error_evtchan;
442 }
443 info->irq = ret;
444
445 again:
446 ret = xenbus_transaction_start(&xbt);
447 if (ret) {
448 xenbus_dev_fatal(dev, ret, "starting transaction");
449 goto error_irqh;
450 }
451 ret = xenbus_printf(xbt, dev->nodename, XENKBD_FIELD_RING_REF, "%lu",
452 virt_to_gfn(info->page));
453 if (ret)
454 goto error_xenbus;
455 ret = xenbus_printf(xbt, dev->nodename, XENKBD_FIELD_RING_GREF,
456 "%u", info->gref);
457 if (ret)
458 goto error_xenbus;
459 ret = xenbus_printf(xbt, dev->nodename, XENKBD_FIELD_EVT_CHANNEL, "%u",
460 evtchn);
461 if (ret)
462 goto error_xenbus;
463 ret = xenbus_transaction_end(xbt, 0);
464 if (ret) {
465 if (ret == -EAGAIN)
466 goto again;
467 xenbus_dev_fatal(dev, ret, "completing transaction");
468 goto error_irqh;
469 }
470
471 xenbus_switch_state(dev, XenbusStateInitialised);
472 return 0;
473
474 error_xenbus:
475 xenbus_transaction_end(xbt, 1);
476 xenbus_dev_fatal(dev, ret, "writing xenstore");
477 error_irqh:
478 unbind_from_irqhandler(info->irq, info);
479 info->irq = -1;
480 error_evtchan:
481 xenbus_free_evtchn(dev, evtchn);
482 error_grant:
483 gnttab_end_foreign_access(info->gref, NULL);
484 info->gref = -1;
485 return ret;
486}
487
488static void xenkbd_disconnect_backend(struct xenkbd_info *info)
489{
490 if (info->irq >= 0)
491 unbind_from_irqhandler(info->irq, info);
492 info->irq = -1;
493 if (info->gref >= 0)
494 gnttab_end_foreign_access(info->gref, NULL);
495 info->gref = -1;
496}
497
498static void xenkbd_backend_changed(struct xenbus_device *dev,
499 enum xenbus_state backend_state)
500{
501 switch (backend_state) {
502 case XenbusStateInitialising:
503 case XenbusStateInitialised:
504 case XenbusStateReconfiguring:
505 case XenbusStateReconfigured:
506 case XenbusStateUnknown:
507 break;
508
509 case XenbusStateInitWait:
510 xenbus_switch_state(dev, XenbusStateConnected);
511 break;
512
513 case XenbusStateConnected:
514 /*
515 * Work around xenbus race condition: If backend goes
516 * through InitWait to Connected fast enough, we can
517 * get Connected twice here.
518 */
519 if (dev->state != XenbusStateConnected)
520 xenbus_switch_state(dev, XenbusStateConnected);
521 break;
522
523 case XenbusStateClosed:
524 if (dev->state == XenbusStateClosed)
525 break;
526 fallthrough; /* Missed the backend's CLOSING state */
527 case XenbusStateClosing:
528 xenbus_frontend_closed(dev);
529 break;
530 }
531}
532
533static const struct xenbus_device_id xenkbd_ids[] = {
534 { XENKBD_DRIVER_NAME },
535 { "" }
536};
537
538static struct xenbus_driver xenkbd_driver = {
539 .ids = xenkbd_ids,
540 .probe = xenkbd_probe,
541 .remove = xenkbd_remove,
542 .resume = xenkbd_resume,
543 .otherend_changed = xenkbd_backend_changed,
544 .not_essential = true,
545};
546
547static int __init xenkbd_init(void)
548{
549 if (!xen_domain())
550 return -ENODEV;
551
552 /* Nothing to do if running in dom0. */
553 if (xen_initial_domain())
554 return -ENODEV;
555
556 if (!xen_has_pv_devices())
557 return -ENODEV;
558
559 return xenbus_register_frontend(&xenkbd_driver);
560}
561
562static void __exit xenkbd_cleanup(void)
563{
564 xenbus_unregister_driver(&xenkbd_driver);
565}
566
567module_init(xenkbd_init);
568module_exit(xenkbd_cleanup);
569
570MODULE_DESCRIPTION("Xen virtual keyboard/pointer device frontend");
571MODULE_LICENSE("GPL");
572MODULE_ALIAS("xen:" XENKBD_DRIVER_NAME);
1/*
2 * Xen para-virtual input device
3 *
4 * Copyright (C) 2005 Anthony Liguori <aliguori@us.ibm.com>
5 * Copyright (C) 2006-2008 Red Hat, Inc., Markus Armbruster <armbru@redhat.com>
6 *
7 * Based on linux/drivers/input/mouse/sermouse.c
8 *
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
11 * more details.
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
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>
21
22#include <asm/xen/hypervisor.h>
23
24#include <xen/xen.h>
25#include <xen/events.h>
26#include <xen/page.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>
33
34struct xenkbd_info {
35 struct input_dev *kbd;
36 struct input_dev *ptr;
37 struct xenkbd_page *page;
38 int gref;
39 int irq;
40 struct xenbus_device *xbdev;
41 char phys[32];
42};
43
44static int xenkbd_remove(struct xenbus_device *);
45static int xenkbd_connect_backend(struct xenbus_device *, struct xenkbd_info *);
46static void xenkbd_disconnect_backend(struct xenkbd_info *);
47
48/*
49 * Note: if you need to send out events, see xenfb_do_update() for how
50 * to do that.
51 */
52
53static irqreturn_t input_handler(int rq, void *dev_id)
54{
55 struct xenkbd_info *info = dev_id;
56 struct xenkbd_page *page = info->page;
57 __u32 cons, prod;
58
59 prod = page->in_prod;
60 if (prod == page->in_cons)
61 return IRQ_HANDLED;
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);
67
68 dev = info->ptr;
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);
76 break;
77 case XENKBD_TYPE_KEY:
78 dev = NULL;
79 if (test_bit(event->key.keycode, info->kbd->keybit))
80 dev = info->kbd;
81 if (test_bit(event->key.keycode, info->ptr->keybit))
82 dev = info->ptr;
83 if (dev)
84 input_report_key(dev, event->key.keycode,
85 event->key.pressed);
86 else
87 pr_warning("unhandled keycode 0x%x\n",
88 event->key.keycode);
89 break;
90 case XENKBD_TYPE_POS:
91 input_report_abs(dev, ABS_X, event->pos.abs_x);
92 input_report_abs(dev, ABS_Y, event->pos.abs_y);
93 if (event->pos.rel_z)
94 input_report_rel(dev, REL_WHEEL,
95 -event->pos.rel_z);
96 break;
97 }
98 if (dev)
99 input_sync(dev);
100 }
101 mb(); /* ensure we got ring contents */
102 page->in_cons = cons;
103 notify_remote_via_irq(info->irq);
104
105 return IRQ_HANDLED;
106}
107
108static int xenkbd_probe(struct xenbus_device *dev,
109 const struct xenbus_device_id *id)
110{
111 int ret, i, abs;
112 struct xenkbd_info *info;
113 struct input_dev *kbd, *ptr;
114
115 info = kzalloc(sizeof(*info), GFP_KERNEL);
116 if (!info) {
117 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
118 return -ENOMEM;
119 }
120 dev_set_drvdata(&dev->dev, info);
121 info->xbdev = dev;
122 info->irq = -1;
123 info->gref = -1;
124 snprintf(info->phys, sizeof(info->phys), "xenbus/%s", dev->nodename);
125
126 info->page = (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
127 if (!info->page)
128 goto error_nomem;
129
130 if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-abs-pointer", "%d", &abs) < 0)
131 abs = 0;
132 if (abs) {
133 ret = xenbus_printf(XBT_NIL, dev->nodename,
134 "request-abs-pointer", "1");
135 if (ret) {
136 pr_warning("xenkbd: can't request abs-pointer");
137 abs = 0;
138 }
139 }
140
141 /* keyboard */
142 kbd = input_allocate_device();
143 if (!kbd)
144 goto error_nomem;
145 kbd->name = "Xen Virtual Keyboard";
146 kbd->phys = info->phys;
147 kbd->id.bustype = BUS_PCI;
148 kbd->id.vendor = 0x5853;
149 kbd->id.product = 0xffff;
150
151 __set_bit(EV_KEY, kbd->evbit);
152 for (i = KEY_ESC; i < KEY_UNKNOWN; i++)
153 __set_bit(i, kbd->keybit);
154 for (i = KEY_OK; i < KEY_MAX; i++)
155 __set_bit(i, kbd->keybit);
156
157 ret = input_register_device(kbd);
158 if (ret) {
159 input_free_device(kbd);
160 xenbus_dev_fatal(dev, ret, "input_register_device(kbd)");
161 goto error;
162 }
163 info->kbd = kbd;
164
165 /* pointing device */
166 ptr = input_allocate_device();
167 if (!ptr)
168 goto error_nomem;
169 ptr->name = "Xen Virtual Pointer";
170 ptr->phys = info->phys;
171 ptr->id.bustype = BUS_PCI;
172 ptr->id.vendor = 0x5853;
173 ptr->id.product = 0xfffe;
174
175 if (abs) {
176 __set_bit(EV_ABS, ptr->evbit);
177 input_set_abs_params(ptr, ABS_X, 0, XENFB_WIDTH, 0, 0);
178 input_set_abs_params(ptr, ABS_Y, 0, XENFB_HEIGHT, 0, 0);
179 } else {
180 input_set_capability(ptr, EV_REL, REL_X);
181 input_set_capability(ptr, EV_REL, REL_Y);
182 }
183 input_set_capability(ptr, EV_REL, REL_WHEEL);
184
185 __set_bit(EV_KEY, ptr->evbit);
186 for (i = BTN_LEFT; i <= BTN_TASK; i++)
187 __set_bit(i, ptr->keybit);
188
189 ret = input_register_device(ptr);
190 if (ret) {
191 input_free_device(ptr);
192 xenbus_dev_fatal(dev, ret, "input_register_device(ptr)");
193 goto error;
194 }
195 info->ptr = ptr;
196
197 ret = xenkbd_connect_backend(dev, info);
198 if (ret < 0)
199 goto error;
200
201 return 0;
202
203 error_nomem:
204 ret = -ENOMEM;
205 xenbus_dev_fatal(dev, ret, "allocating device memory");
206 error:
207 xenkbd_remove(dev);
208 return ret;
209}
210
211static int xenkbd_resume(struct xenbus_device *dev)
212{
213 struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
214
215 xenkbd_disconnect_backend(info);
216 memset(info->page, 0, PAGE_SIZE);
217 return xenkbd_connect_backend(dev, info);
218}
219
220static int xenkbd_remove(struct xenbus_device *dev)
221{
222 struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
223
224 xenkbd_disconnect_backend(info);
225 if (info->kbd)
226 input_unregister_device(info->kbd);
227 if (info->ptr)
228 input_unregister_device(info->ptr);
229 free_page((unsigned long)info->page);
230 kfree(info);
231 return 0;
232}
233
234static int xenkbd_connect_backend(struct xenbus_device *dev,
235 struct xenkbd_info *info)
236{
237 int ret, evtchn;
238 struct xenbus_transaction xbt;
239
240 ret = gnttab_grant_foreign_access(dev->otherend_id,
241 virt_to_gfn(info->page), 0);
242 if (ret < 0)
243 return ret;
244 info->gref = ret;
245
246 ret = xenbus_alloc_evtchn(dev, &evtchn);
247 if (ret)
248 goto error_grant;
249 ret = bind_evtchn_to_irqhandler(evtchn, input_handler,
250 0, dev->devicetype, info);
251 if (ret < 0) {
252 xenbus_dev_fatal(dev, ret, "bind_evtchn_to_irqhandler");
253 goto error_evtchan;
254 }
255 info->irq = ret;
256
257 again:
258 ret = xenbus_transaction_start(&xbt);
259 if (ret) {
260 xenbus_dev_fatal(dev, ret, "starting transaction");
261 goto error_irqh;
262 }
263 ret = xenbus_printf(xbt, dev->nodename, "page-ref", "%lu",
264 virt_to_gfn(info->page));
265 if (ret)
266 goto error_xenbus;
267 ret = xenbus_printf(xbt, dev->nodename, "page-gref", "%u", info->gref);
268 if (ret)
269 goto error_xenbus;
270 ret = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
271 evtchn);
272 if (ret)
273 goto error_xenbus;
274 ret = xenbus_transaction_end(xbt, 0);
275 if (ret) {
276 if (ret == -EAGAIN)
277 goto again;
278 xenbus_dev_fatal(dev, ret, "completing transaction");
279 goto error_irqh;
280 }
281
282 xenbus_switch_state(dev, XenbusStateInitialised);
283 return 0;
284
285 error_xenbus:
286 xenbus_transaction_end(xbt, 1);
287 xenbus_dev_fatal(dev, ret, "writing xenstore");
288 error_irqh:
289 unbind_from_irqhandler(info->irq, info);
290 info->irq = -1;
291 error_evtchan:
292 xenbus_free_evtchn(dev, evtchn);
293 error_grant:
294 gnttab_end_foreign_access(info->gref, 0, 0UL);
295 info->gref = -1;
296 return ret;
297}
298
299static void xenkbd_disconnect_backend(struct xenkbd_info *info)
300{
301 if (info->irq >= 0)
302 unbind_from_irqhandler(info->irq, info);
303 info->irq = -1;
304 if (info->gref >= 0)
305 gnttab_end_foreign_access(info->gref, 0, 0UL);
306 info->gref = -1;
307}
308
309static void xenkbd_backend_changed(struct xenbus_device *dev,
310 enum xenbus_state backend_state)
311{
312 struct xenkbd_info *info = dev_get_drvdata(&dev->dev);
313 int ret, val;
314
315 switch (backend_state) {
316 case XenbusStateInitialising:
317 case XenbusStateInitialised:
318 case XenbusStateReconfiguring:
319 case XenbusStateReconfigured:
320 case XenbusStateUnknown:
321 break;
322
323 case XenbusStateInitWait:
324InitWait:
325 ret = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
326 "feature-abs-pointer", "%d", &val);
327 if (ret < 0)
328 val = 0;
329 if (val) {
330 ret = xenbus_printf(XBT_NIL, info->xbdev->nodename,
331 "request-abs-pointer", "1");
332 if (ret)
333 pr_warning("xenkbd: can't request abs-pointer");
334 }
335
336 xenbus_switch_state(dev, XenbusStateConnected);
337 break;
338
339 case XenbusStateConnected:
340 /*
341 * Work around xenbus race condition: If backend goes
342 * through InitWait to Connected fast enough, we can
343 * get Connected twice here.
344 */
345 if (dev->state != XenbusStateConnected)
346 goto InitWait; /* no InitWait seen yet, fudge it */
347
348 /* Set input abs params to match backend screen res */
349 if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
350 "width", "%d", &val) > 0)
351 input_set_abs_params(info->ptr, ABS_X, 0, val, 0, 0);
352
353 if (xenbus_scanf(XBT_NIL, info->xbdev->otherend,
354 "height", "%d", &val) > 0)
355 input_set_abs_params(info->ptr, ABS_Y, 0, val, 0, 0);
356
357 break;
358
359 case XenbusStateClosed:
360 if (dev->state == XenbusStateClosed)
361 break;
362 /* Missed the backend's CLOSING state -- fallthrough */
363 case XenbusStateClosing:
364 xenbus_frontend_closed(dev);
365 break;
366 }
367}
368
369static const struct xenbus_device_id xenkbd_ids[] = {
370 { "vkbd" },
371 { "" }
372};
373
374static struct xenbus_driver xenkbd_driver = {
375 .ids = xenkbd_ids,
376 .probe = xenkbd_probe,
377 .remove = xenkbd_remove,
378 .resume = xenkbd_resume,
379 .otherend_changed = xenkbd_backend_changed,
380};
381
382static int __init xenkbd_init(void)
383{
384 if (!xen_domain())
385 return -ENODEV;
386
387 /* Nothing to do if running in dom0. */
388 if (xen_initial_domain())
389 return -ENODEV;
390
391 if (!xen_has_pv_devices())
392 return -ENODEV;
393
394 return xenbus_register_frontend(&xenkbd_driver);
395}
396
397static void __exit xenkbd_cleanup(void)
398{
399 xenbus_unregister_driver(&xenkbd_driver);
400}
401
402module_init(xenkbd_init);
403module_exit(xenkbd_cleanup);
404
405MODULE_DESCRIPTION("Xen virtual keyboard/pointer device frontend");
406MODULE_LICENSE("GPL");
407MODULE_ALIAS("xen:vkbd");