2 * Copyright (C) 2011 - 2015 UNISYS CORPORATION
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12 * NON INFRINGEMENT. See the GNU General Public License for more
17 * This driver lives in a generic guest Linux partition, and registers to
18 * receive keyboard and mouse channels from the visorbus driver. It reads
19 * inputs from such channels, and delivers it to the Linux OS in the
20 * standard way the Linux expects for input drivers.
24 #include <linux/input.h>
25 #include <linux/kernel.h>
26 #include <linux/uuid.h>
29 #include "ultrainputreport.h"
31 /* Keyboard channel {c73416d0-b0b8-44af-b304-9d2ae99f1b3d} */
32 #define VISOR_KEYBOARD_CHANNEL_GUID \
33 GUID_INIT(0xc73416d0, 0xb0b8, 0x44af, \
34 0xb3, 0x4, 0x9d, 0x2a, 0xe9, 0x9f, 0x1b, 0x3d)
35 #define VISOR_KEYBOARD_CHANNEL_GUID_STR "c73416d0-b0b8-44af-b304-9d2ae99f1b3d"
37 /* Mouse channel {addf07d4-94a9-46e2-81c3-61abcdbdbd87} */
38 #define VISOR_MOUSE_CHANNEL_GUID \
39 GUID_INIT(0xaddf07d4, 0x94a9, 0x46e2, \
40 0x81, 0xc3, 0x61, 0xab, 0xcd, 0xbd, 0xbd, 0x87)
41 #define VISOR_MOUSE_CHANNEL_GUID_STR "addf07d4-94a9-46e2-81c3-61abcdbdbd87"
43 #define PIXELS_ACROSS_DEFAULT 800
44 #define PIXELS_DOWN_DEFAULT 600
45 #define KEYCODE_TABLE_BYTES 256
47 enum visorinput_device_type {
53 * This is the private data that we store for each device.
54 * A pointer to this struct is maintained via
55 * dev_get_drvdata() / dev_set_drvdata() for each struct device.
57 struct visorinput_devdata {
58 struct visor_device *dev;
60 struct mutex lock_visor_dev;
61 struct input_dev *visorinput_dev;
63 bool interrupts_enabled;
64 /* size of following array */
65 unsigned int keycode_table_bytes;
66 /* for keyboard devices: visorkbd_keycode[] + visorkbd_ext_keycode[] */
67 unsigned char keycode_table[0];
70 static const guid_t visor_keyboard_channel_guid = VISOR_KEYBOARD_CHANNEL_GUID;
71 static const guid_t visor_mouse_channel_guid = VISOR_MOUSE_CHANNEL_GUID;
74 * Borrowed from drivers/input/keyboard/atakbd.c
75 * This maps 1-byte scancodes to keycodes.
77 static const unsigned char visorkbd_keycode[KEYCODE_TABLE_BYTES] = {
105 [26] = KEY_LEFTBRACE,
106 [27] = KEY_RIGHTBRACE,
118 [39] = KEY_SEMICOLON,
119 [40] = KEY_APOSTROPHE,
121 [42] = KEY_LEFTSHIFT,
122 [43] = KEY_BACKSLASH,
133 [54] = KEY_RIGHTSHIFT,
134 [55] = KEY_KPASTERISK,
149 [70] = KEY_SCROLLLOCK,
163 /* enables UK backslash+pipe key and FR lessthan+greaterthan key */
167 [90] = KEY_KPLEFTPAREN,
168 [91] = KEY_KPRIGHTPAREN,
169 [92] = KEY_KPASTERISK,
170 [93] = KEY_KPASTERISK,
174 [97] = KEY_RIGHTCTRL,
176 [99] = KEY_KPLEFTPAREN,
177 [100] = KEY_KPRIGHTPAREN,
186 [109] = KEY_PAGEDOWN,
194 * This maps the <xx> in extended scancodes of the form "0xE0 <xx>" into
197 static const unsigned char visorkbd_ext_keycode[KEYCODE_TABLE_BYTES] = {
198 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
199 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
200 0, 0, 0, 0, KEY_KPENTER, KEY_RIGHTCTRL, 0, 0, /* 0x18 */
201 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
202 KEY_RIGHTALT, 0, 0, 0, 0, 0, 0, 0, /* 0x28 */
203 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 */
204 KEY_RIGHTALT /* AltGr */, 0, 0, 0, 0, 0, 0, 0, /* 0x38 */
205 0, 0, 0, 0, 0, 0, 0, KEY_HOME, /* 0x40 */
206 KEY_UP, KEY_PAGEUP, 0, KEY_LEFT, 0, KEY_RIGHT, 0, KEY_END, /* 0x48 */
207 KEY_DOWN, KEY_PAGEDOWN, KEY_INSERT, KEY_DELETE, 0, 0, 0, 0, /* 0x50 */
208 0, 0, 0, 0, 0, 0, 0, 0, /* 0x58 */
209 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 */
210 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
213 static int visorinput_open(struct input_dev *visorinput_dev)
215 struct visorinput_devdata *devdata = input_get_drvdata(visorinput_dev);
218 dev_err(&visorinput_dev->dev,
219 "%s input_get_drvdata(%p) returned NULL\n",
220 __func__, visorinput_dev);
223 dev_dbg(&visorinput_dev->dev, "%s opened\n", __func__);
226 * If we're not paused, really enable interrupts.
227 * Regardless of whether we are paused, set a flag indicating
228 * interrupts should be enabled so when we resume, interrupts
229 * will really be enabled.
231 mutex_lock(&devdata->lock_visor_dev);
232 devdata->interrupts_enabled = true;
235 visorbus_enable_channel_interrupts(devdata->dev);
238 mutex_unlock(&devdata->lock_visor_dev);
242 static void visorinput_close(struct input_dev *visorinput_dev)
244 struct visorinput_devdata *devdata = input_get_drvdata(visorinput_dev);
247 dev_err(&visorinput_dev->dev,
248 "%s input_get_drvdata(%p) returned NULL\n",
249 __func__, visorinput_dev);
252 dev_dbg(&visorinput_dev->dev, "%s closed\n", __func__);
255 * If we're not paused, really disable interrupts.
256 * Regardless of whether we are paused, set a flag indicating
257 * interrupts should be disabled so when we resume we will
258 * not re-enable them.
260 mutex_lock(&devdata->lock_visor_dev);
261 devdata->interrupts_enabled = false;
264 visorbus_disable_channel_interrupts(devdata->dev);
267 mutex_unlock(&devdata->lock_visor_dev);
271 * setup_client_keyboard() initializes and returns a Linux input node that
272 * we can use to deliver keyboard inputs to Linux. We of course do this when
273 * we see keyboard inputs coming in on a keyboard channel.
275 static struct input_dev *setup_client_keyboard(void *devdata,
276 unsigned char *keycode_table)
280 struct input_dev *visorinput_dev = input_allocate_device();
285 visorinput_dev->name = "visor Keyboard";
286 visorinput_dev->phys = "visorkbd:input0";
287 visorinput_dev->id.bustype = BUS_VIRTUAL;
288 visorinput_dev->id.vendor = 0x0001;
289 visorinput_dev->id.product = 0x0001;
290 visorinput_dev->id.version = 0x0100;
292 visorinput_dev->evbit[0] = BIT_MASK(EV_KEY) |
295 visorinput_dev->ledbit[0] = BIT_MASK(LED_CAPSL) |
296 BIT_MASK(LED_SCROLLL) |
298 visorinput_dev->keycode = keycode_table;
299 /* sizeof(unsigned char) */
300 visorinput_dev->keycodesize = 1;
301 visorinput_dev->keycodemax = KEYCODE_TABLE_BYTES;
303 for (i = 1; i < visorinput_dev->keycodemax; i++)
304 set_bit(keycode_table[i], visorinput_dev->keybit);
305 for (i = 1; i < visorinput_dev->keycodemax; i++)
306 set_bit(keycode_table[i + KEYCODE_TABLE_BYTES],
307 visorinput_dev->keybit);
309 visorinput_dev->open = visorinput_open;
310 visorinput_dev->close = visorinput_close;
311 /* pre input_register! */
312 input_set_drvdata(visorinput_dev, devdata);
314 return visorinput_dev;
317 static struct input_dev *setup_client_mouse(void *devdata)
321 struct input_dev *visorinput_dev = input_allocate_device();
326 visorinput_dev->name = "visor Mouse";
327 visorinput_dev->phys = "visormou:input0";
328 visorinput_dev->id.bustype = BUS_VIRTUAL;
329 visorinput_dev->id.vendor = 0x0001;
330 visorinput_dev->id.product = 0x0002;
331 visorinput_dev->id.version = 0x0100;
333 visorinput_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
334 set_bit(BTN_LEFT, visorinput_dev->keybit);
335 set_bit(BTN_RIGHT, visorinput_dev->keybit);
336 set_bit(BTN_MIDDLE, visorinput_dev->keybit);
338 if (registered_fb[0]) {
339 fb0 = registered_fb[0];
340 xres = fb0->var.xres_virtual;
341 yres = fb0->var.yres_virtual;
343 xres = PIXELS_ACROSS_DEFAULT;
344 yres = PIXELS_DOWN_DEFAULT;
346 input_set_abs_params(visorinput_dev, ABS_X, 0, xres, 0, 0);
347 input_set_abs_params(visorinput_dev, ABS_Y, 0, yres, 0, 0);
349 visorinput_dev->open = visorinput_open;
350 visorinput_dev->close = visorinput_close;
351 /* pre input_register! */
352 input_set_drvdata(visorinput_dev, devdata);
353 input_set_capability(visorinput_dev, EV_REL, REL_WHEEL);
355 return visorinput_dev;
358 static struct visorinput_devdata *devdata_create(
359 struct visor_device *dev,
360 enum visorinput_device_type devtype)
362 struct visorinput_devdata *devdata = NULL;
363 unsigned int extra_bytes = 0;
365 if (devtype == visorinput_keyboard)
366 /* allocate room for devdata->keycode_table, filled in below */
367 extra_bytes = KEYCODE_TABLE_BYTES * 2;
368 devdata = kzalloc(sizeof(*devdata) + extra_bytes, GFP_KERNEL);
371 mutex_init(&devdata->lock_visor_dev);
372 mutex_lock(&devdata->lock_visor_dev);
376 * visorinput_open() can be called as soon as input_register_device()
377 * happens, and that will enable channel interrupts. Setting paused
378 * prevents us from getting into visorinput_channel_interrupt() prior
379 * to the device structure being totally initialized.
381 devdata->paused = true;
384 * This is an input device in a client guest partition,
385 * so we need to create whatever input nodes are necessary to
386 * deliver our inputs to the guest OS.
389 case visorinput_keyboard:
390 devdata->keycode_table_bytes = extra_bytes;
391 memcpy(devdata->keycode_table, visorkbd_keycode,
392 KEYCODE_TABLE_BYTES);
393 memcpy(devdata->keycode_table + KEYCODE_TABLE_BYTES,
394 visorkbd_ext_keycode, KEYCODE_TABLE_BYTES);
395 devdata->visorinput_dev = setup_client_keyboard
396 (devdata, devdata->keycode_table);
397 if (!devdata->visorinput_dev)
398 goto cleanups_register;
400 case visorinput_mouse:
401 devdata->visorinput_dev = setup_client_mouse(devdata);
402 if (!devdata->visorinput_dev)
403 goto cleanups_register;
406 /* No other input devices supported */
410 dev_set_drvdata(&dev->device, devdata);
411 mutex_unlock(&devdata->lock_visor_dev);
414 * Device struct is completely set up now, with the exception of
415 * visorinput_dev being registered.
416 * We need to unlock before we register the device, because this
417 * can cause an on-stack call of visorinput_open(), which would
418 * deadlock if we had the lock.
420 if (input_register_device(devdata->visorinput_dev)) {
421 input_free_device(devdata->visorinput_dev);
422 goto err_kfree_devdata;
425 mutex_lock(&devdata->lock_visor_dev);
427 * Establish calls to visorinput_channel_interrupt() if that is
428 * the desired state that we've kept track of in interrupts_enabled
429 * while the device was being created.
431 devdata->paused = false;
432 if (devdata->interrupts_enabled)
433 visorbus_enable_channel_interrupts(dev);
434 mutex_unlock(&devdata->lock_visor_dev);
439 mutex_unlock(&devdata->lock_visor_dev);
445 static int visorinput_probe(struct visor_device *dev)
448 enum visorinput_device_type devtype;
450 guid = visorchannel_get_guid(dev->visorchannel);
451 if (guid_equal(guid, &visor_mouse_channel_guid))
452 devtype = visorinput_mouse;
453 else if (guid_equal(guid, &visor_keyboard_channel_guid))
454 devtype = visorinput_keyboard;
457 visorbus_disable_channel_interrupts(dev);
458 if (!devdata_create(dev, devtype))
463 static void unregister_client_input(struct input_dev *visorinput_dev)
466 input_unregister_device(visorinput_dev);
469 static void visorinput_remove(struct visor_device *dev)
471 struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
476 mutex_lock(&devdata->lock_visor_dev);
477 visorbus_disable_channel_interrupts(dev);
480 * due to above, at this time no thread of execution will be
481 * in visorinput_channel_interrupt()
484 dev_set_drvdata(&dev->device, NULL);
485 mutex_unlock(&devdata->lock_visor_dev);
487 unregister_client_input(devdata->visorinput_dev);
492 * Make it so the current locking state of the locking key indicated by
493 * <keycode> is as indicated by <desired_state> (1=locked, 0=unlocked).
495 static void handle_locking_key(struct input_dev *visorinput_dev, int keycode,
514 if (test_bit(led, visorinput_dev->led) != desired_state) {
515 input_report_key(visorinput_dev, keycode, 1);
516 input_sync(visorinput_dev);
517 input_report_key(visorinput_dev, keycode, 0);
518 input_sync(visorinput_dev);
519 __change_bit(led, visorinput_dev->led);
524 * <scancode> is either a 1-byte scancode, or an extended 16-bit scancode
525 * with 0xE0 in the low byte and the extended scancode value in the next
528 static int scancode_to_keycode(int scancode)
531 return visorkbd_ext_keycode[(scancode >> 8) & 0xff];
533 return visorkbd_keycode[scancode];
536 static int calc_button(int x)
551 * This is used only when this driver is active as an input driver in the
552 * client guest partition. It is called periodically so we can obtain inputs
553 * from the channel, and deliver them to the guest OS.
555 static void visorinput_channel_interrupt(struct visor_device *dev)
557 struct visor_inputreport r;
558 int scancode, keycode;
559 struct input_dev *visorinput_dev;
560 int xmotion, ymotion, button;
562 struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
567 visorinput_dev = devdata->visorinput_dev;
569 while (!visorchannel_signalremove(dev->visorchannel, 0, &r)) {
570 scancode = r.activity.arg1;
571 keycode = scancode_to_keycode(scancode);
572 switch (r.activity.action) {
573 case INPUTACTION_KEY_DOWN:
574 input_report_key(visorinput_dev, keycode, 1);
575 input_sync(visorinput_dev);
577 case INPUTACTION_KEY_UP:
578 input_report_key(visorinput_dev, keycode, 0);
579 input_sync(visorinput_dev);
581 case INPUTACTION_KEY_DOWN_UP:
582 input_report_key(visorinput_dev, keycode, 1);
583 input_sync(visorinput_dev);
584 input_report_key(visorinput_dev, keycode, 0);
585 input_sync(visorinput_dev);
587 case INPUTACTION_SET_LOCKING_KEY_STATE:
588 handle_locking_key(visorinput_dev, keycode,
591 case INPUTACTION_XY_MOTION:
592 xmotion = r.activity.arg1;
593 ymotion = r.activity.arg2;
594 input_report_abs(visorinput_dev, ABS_X, xmotion);
595 input_report_abs(visorinput_dev, ABS_Y, ymotion);
596 input_sync(visorinput_dev);
598 case INPUTACTION_MOUSE_BUTTON_DOWN:
599 button = calc_button(r.activity.arg1);
602 input_report_key(visorinput_dev, button, 1);
603 input_sync(visorinput_dev);
605 case INPUTACTION_MOUSE_BUTTON_UP:
606 button = calc_button(r.activity.arg1);
609 input_report_key(visorinput_dev, button, 0);
610 input_sync(visorinput_dev);
612 case INPUTACTION_MOUSE_BUTTON_CLICK:
613 button = calc_button(r.activity.arg1);
616 input_report_key(visorinput_dev, button, 1);
617 input_sync(visorinput_dev);
618 input_report_key(visorinput_dev, button, 0);
619 input_sync(visorinput_dev);
621 case INPUTACTION_MOUSE_BUTTON_DCLICK:
622 button = calc_button(r.activity.arg1);
625 for (i = 0; i < 2; i++) {
626 input_report_key(visorinput_dev, button, 1);
627 input_sync(visorinput_dev);
628 input_report_key(visorinput_dev, button, 0);
629 input_sync(visorinput_dev);
632 case INPUTACTION_WHEEL_ROTATE_AWAY:
633 input_report_rel(visorinput_dev, REL_WHEEL, 1);
634 input_sync(visorinput_dev);
636 case INPUTACTION_WHEEL_ROTATE_TOWARD:
637 input_report_rel(visorinput_dev, REL_WHEEL, -1);
638 input_sync(visorinput_dev);
641 /* Unsupported input action */
647 static int visorinput_pause(struct visor_device *dev,
648 visorbus_state_complete_func complete_func)
651 struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
658 mutex_lock(&devdata->lock_visor_dev);
659 if (devdata->paused) {
663 if (devdata->interrupts_enabled)
664 visorbus_disable_channel_interrupts(dev);
667 * due to above, at this time no thread of execution will be
668 * in visorinput_channel_interrupt()
670 devdata->paused = true;
671 complete_func(dev, 0);
674 mutex_unlock(&devdata->lock_visor_dev);
679 static int visorinput_resume(struct visor_device *dev,
680 visorbus_state_complete_func complete_func)
683 struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
689 mutex_lock(&devdata->lock_visor_dev);
690 if (!devdata->paused) {
694 devdata->paused = false;
695 complete_func(dev, 0);
698 * Re-establish calls to visorinput_channel_interrupt() if that is
699 * the desired state that we've kept track of in interrupts_enabled
700 * while the device was paused.
702 if (devdata->interrupts_enabled)
703 visorbus_enable_channel_interrupts(dev);
707 mutex_unlock(&devdata->lock_visor_dev);
712 /* GUIDS for all channel types supported by this driver. */
713 static struct visor_channeltype_descriptor visorinput_channel_types[] = {
714 { VISOR_KEYBOARD_CHANNEL_GUID, "keyboard"},
715 { VISOR_MOUSE_CHANNEL_GUID, "mouse"},
719 static struct visor_driver visorinput_driver = {
720 .name = "visorinput",
721 .owner = THIS_MODULE,
722 .channel_types = visorinput_channel_types,
723 .probe = visorinput_probe,
724 .remove = visorinput_remove,
725 .channel_interrupt = visorinput_channel_interrupt,
726 .pause = visorinput_pause,
727 .resume = visorinput_resume,
730 module_driver(visorinput_driver, visorbus_register_visor_driver,
731 visorbus_unregister_visor_driver);
733 MODULE_DEVICE_TABLE(visorbus, visorinput_channel_types);
735 MODULE_AUTHOR("Unisys");
736 MODULE_LICENSE("GPL");
737 MODULE_DESCRIPTION("s-Par human input driver for virtual keyboard/mouse");
739 MODULE_ALIAS("visorbus:" VISOR_MOUSE_CHANNEL_GUID_STR);
740 MODULE_ALIAS("visorbus:" VISOR_KEYBOARD_CHANNEL_GUID_STR);