1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2011 - 2015 UNISYS CORPORATION
8 * This driver lives in a generic guest Linux partition, and registers to
9 * receive keyboard and mouse channels from the visorbus driver. It reads
10 * inputs from such channels, and delivers it to the Linux OS in the
11 * standard way the Linux expects for input drivers.
15 #include <linux/input.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/uuid.h>
19 #include <linux/visorbus.h>
21 /* These defines identify mouse and keyboard activity which is specified by the
22 * firmware to the host using the cmsimpleinput protocol. @ingroup coretypes
24 /* only motion; arg1=x, arg2=y */
25 #define INPUTACTION_XY_MOTION 1
27 /* arg1: 1=left,2=center,3=right */
28 #define INPUTACTION_MOUSE_BUTTON_DOWN 2
29 #define INPUTACTION_MOUSE_BUTTON_UP 3
30 #define INPUTACTION_MOUSE_BUTTON_CLICK 4
31 #define INPUTACTION_MOUSE_BUTTON_DCLICK 5
33 /* arg1: wheel rotation away from/toward user */
34 #define INPUTACTION_WHEEL_ROTATE_AWAY 6
35 #define INPUTACTION_WHEEL_ROTATE_TOWARD 7
37 /* arg1: scancode, as follows: If arg1 <= 0xff, it's a 1-byte scancode and arg1
38 * is that scancode. If arg1 > 0xff, it's a 2-byte scanecode, with the 1st
39 * byte in the low 8 bits, and the 2nd byte in the high 8 bits.
40 * E.g., the right ALT key would appear as x'38e0'.
42 #define INPUTACTION_KEY_DOWN 64
43 #define INPUTACTION_KEY_UP 65
44 #define INPUTACTION_KEY_DOWN_UP 67
46 /* arg1: scancode (in same format as inputaction_keyDown); MUST refer to one of
47 * the locking keys, like capslock, numlock, or scrolllock.
48 * arg2: 1 iff locking key should be in the LOCKED position (e.g., light is ON)
50 #define INPUTACTION_SET_LOCKING_KEY_STATE 66
52 /* Keyboard channel {c73416d0-b0b8-44af-b304-9d2ae99f1b3d} */
53 #define VISOR_KEYBOARD_CHANNEL_GUID \
54 GUID_INIT(0xc73416d0, 0xb0b8, 0x44af, \
55 0xb3, 0x4, 0x9d, 0x2a, 0xe9, 0x9f, 0x1b, 0x3d)
56 #define VISOR_KEYBOARD_CHANNEL_GUID_STR "c73416d0-b0b8-44af-b304-9d2ae99f1b3d"
58 /* Mouse channel {addf07d4-94a9-46e2-81c3-61abcdbdbd87} */
59 #define VISOR_MOUSE_CHANNEL_GUID \
60 GUID_INIT(0xaddf07d4, 0x94a9, 0x46e2, \
61 0x81, 0xc3, 0x61, 0xab, 0xcd, 0xbd, 0xbd, 0x87)
62 #define VISOR_MOUSE_CHANNEL_GUID_STR "addf07d4-94a9-46e2-81c3-61abcdbdbd87"
64 #define PIXELS_ACROSS_DEFAULT 1024
65 #define PIXELS_DOWN_DEFAULT 768
66 #define KEYCODE_TABLE_BYTES 256
68 struct visor_inputactivity {
75 struct visor_inputreport {
77 struct visor_inputactivity activity;
80 /* header of keyboard/mouse channels */
81 struct visor_input_channel_data {
94 enum visorinput_dev_type {
100 * This is the private data that we store for each device. A pointer to this
101 * struct is maintained via dev_get_drvdata() / dev_set_drvdata() for each
104 struct visorinput_devdata {
105 struct visor_device *dev;
107 struct mutex lock_visor_dev;
108 struct input_dev *visorinput_dev;
110 bool interrupts_enabled;
111 /* size of following array */
112 unsigned int keycode_table_bytes;
113 /* for keyboard devices: visorkbd_keycode[] + visorkbd_ext_keycode[] */
114 unsigned char keycode_table[0];
117 static const guid_t visor_keyboard_channel_guid = VISOR_KEYBOARD_CHANNEL_GUID;
118 static const guid_t visor_mouse_channel_guid = VISOR_MOUSE_CHANNEL_GUID;
121 * Borrowed from drivers/input/keyboard/atakbd.c
122 * This maps 1-byte scancodes to keycodes.
124 static const unsigned char visorkbd_keycode[KEYCODE_TABLE_BYTES] = {
125 /* American layout */
140 [14] = KEY_BACKSPACE,
152 [26] = KEY_LEFTBRACE,
153 [27] = KEY_RIGHTBRACE,
165 [39] = KEY_SEMICOLON,
166 [40] = KEY_APOSTROPHE,
168 [42] = KEY_LEFTSHIFT,
169 [43] = KEY_BACKSLASH,
180 [54] = KEY_RIGHTSHIFT,
181 [55] = KEY_KPASTERISK,
196 [70] = KEY_SCROLLLOCK,
210 /* enables UK backslash+pipe key and FR lessthan+greaterthan key */
214 [90] = KEY_KPLEFTPAREN,
215 [91] = KEY_KPRIGHTPAREN,
216 [92] = KEY_KPASTERISK,
217 [93] = KEY_KPASTERISK,
221 [97] = KEY_RIGHTCTRL,
223 [99] = KEY_KPLEFTPAREN,
224 [100] = KEY_KPRIGHTPAREN,
233 [109] = KEY_PAGEDOWN,
241 * This maps the <xx> in extended scancodes of the form "0xE0 <xx>" into
244 static const unsigned char visorkbd_ext_keycode[KEYCODE_TABLE_BYTES] = {
245 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
246 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
247 0, 0, 0, 0, KEY_KPENTER, KEY_RIGHTCTRL, 0, 0, /* 0x18 */
248 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
249 KEY_RIGHTALT, 0, 0, 0, 0, 0, 0, 0, /* 0x28 */
250 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 */
251 KEY_RIGHTALT /* AltGr */, 0, 0, 0, 0, 0, 0, 0, /* 0x38 */
252 0, 0, 0, 0, 0, 0, 0, KEY_HOME, /* 0x40 */
253 KEY_UP, KEY_PAGEUP, 0, KEY_LEFT, 0, KEY_RIGHT, 0, KEY_END, /* 0x48 */
254 KEY_DOWN, KEY_PAGEDOWN, KEY_INSERT, KEY_DELETE, 0, 0, 0, 0, /* 0x50 */
255 0, 0, 0, 0, 0, 0, 0, 0, /* 0x58 */
256 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 */
257 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
260 static int visorinput_open(struct input_dev *visorinput_dev)
262 struct visorinput_devdata *devdata = input_get_drvdata(visorinput_dev);
265 dev_err(&visorinput_dev->dev,
266 "%s input_get_drvdata(%p) returned NULL\n",
267 __func__, visorinput_dev);
270 dev_dbg(&visorinput_dev->dev, "%s opened\n", __func__);
273 * If we're not paused, really enable interrupts. Regardless of whether
274 * we are paused, set a flag indicating interrupts should be enabled so
275 * when we resume, interrupts will really be enabled.
277 mutex_lock(&devdata->lock_visor_dev);
278 devdata->interrupts_enabled = true;
281 visorbus_enable_channel_interrupts(devdata->dev);
284 mutex_unlock(&devdata->lock_visor_dev);
288 static void visorinput_close(struct input_dev *visorinput_dev)
290 struct visorinput_devdata *devdata = input_get_drvdata(visorinput_dev);
293 dev_err(&visorinput_dev->dev,
294 "%s input_get_drvdata(%p) returned NULL\n",
295 __func__, visorinput_dev);
298 dev_dbg(&visorinput_dev->dev, "%s closed\n", __func__);
301 * If we're not paused, really disable interrupts. Regardless of
302 * whether we are paused, set a flag indicating interrupts should be
303 * disabled so when we resume we will not re-enable them.
305 mutex_lock(&devdata->lock_visor_dev);
306 devdata->interrupts_enabled = false;
309 visorbus_disable_channel_interrupts(devdata->dev);
312 mutex_unlock(&devdata->lock_visor_dev);
316 * setup_client_keyboard() initializes and returns a Linux input node that we
317 * can use to deliver keyboard inputs to Linux. We of course do this when we
318 * see keyboard inputs coming in on a keyboard channel.
320 static struct input_dev *setup_client_keyboard(void *devdata,
321 unsigned char *keycode_table)
325 struct input_dev *visorinput_dev = input_allocate_device();
330 visorinput_dev->name = "visor Keyboard";
331 visorinput_dev->phys = "visorkbd:input0";
332 visorinput_dev->id.bustype = BUS_VIRTUAL;
333 visorinput_dev->id.vendor = 0x0001;
334 visorinput_dev->id.product = 0x0001;
335 visorinput_dev->id.version = 0x0100;
337 visorinput_dev->evbit[0] = BIT_MASK(EV_KEY) |
340 visorinput_dev->ledbit[0] = BIT_MASK(LED_CAPSL) |
341 BIT_MASK(LED_SCROLLL) |
343 visorinput_dev->keycode = keycode_table;
344 /* sizeof(unsigned char) */
345 visorinput_dev->keycodesize = 1;
346 visorinput_dev->keycodemax = KEYCODE_TABLE_BYTES;
348 for (i = 1; i < visorinput_dev->keycodemax; i++)
349 set_bit(keycode_table[i], visorinput_dev->keybit);
350 for (i = 1; i < visorinput_dev->keycodemax; i++)
351 set_bit(keycode_table[i + KEYCODE_TABLE_BYTES],
352 visorinput_dev->keybit);
354 visorinput_dev->open = visorinput_open;
355 visorinput_dev->close = visorinput_close;
356 /* pre input_register! */
357 input_set_drvdata(visorinput_dev, devdata);
359 return visorinput_dev;
362 static struct input_dev *setup_client_mouse(void *devdata, unsigned int xres,
365 struct input_dev *visorinput_dev = input_allocate_device();
370 visorinput_dev->name = "visor Mouse";
371 visorinput_dev->phys = "visormou:input0";
372 visorinput_dev->id.bustype = BUS_VIRTUAL;
373 visorinput_dev->id.vendor = 0x0001;
374 visorinput_dev->id.product = 0x0002;
375 visorinput_dev->id.version = 0x0100;
377 visorinput_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
378 set_bit(BTN_LEFT, visorinput_dev->keybit);
379 set_bit(BTN_RIGHT, visorinput_dev->keybit);
380 set_bit(BTN_MIDDLE, visorinput_dev->keybit);
383 xres = PIXELS_ACROSS_DEFAULT;
385 yres = PIXELS_DOWN_DEFAULT;
386 input_set_abs_params(visorinput_dev, ABS_X, 0, xres, 0, 0);
387 input_set_abs_params(visorinput_dev, ABS_Y, 0, yres, 0, 0);
389 visorinput_dev->open = visorinput_open;
390 visorinput_dev->close = visorinput_close;
391 /* pre input_register! */
392 input_set_drvdata(visorinput_dev, devdata);
393 input_set_capability(visorinput_dev, EV_REL, REL_WHEEL);
395 return visorinput_dev;
398 static struct visorinput_devdata *devdata_create(struct visor_device *dev,
399 enum visorinput_dev_type dtype)
401 struct visorinput_devdata *devdata = NULL;
402 unsigned int extra_bytes = 0;
403 unsigned int size, xres, yres, err;
404 struct visor_input_channel_data data;
406 if (dtype == visorinput_keyboard)
407 /* allocate room for devdata->keycode_table, filled in below */
408 extra_bytes = KEYCODE_TABLE_BYTES * 2;
409 devdata = kzalloc(sizeof(*devdata) + extra_bytes, GFP_KERNEL);
412 mutex_init(&devdata->lock_visor_dev);
413 mutex_lock(&devdata->lock_visor_dev);
417 * visorinput_open() can be called as soon as input_register_device()
418 * happens, and that will enable channel interrupts. Setting paused
419 * prevents us from getting into visorinput_channel_interrupt() prior
420 * to the device structure being totally initialized.
422 devdata->paused = true;
425 * This is an input device in a client guest partition, so we need to
426 * create whatever input nodes are necessary to deliver our inputs to
430 case visorinput_keyboard:
431 devdata->keycode_table_bytes = extra_bytes;
432 memcpy(devdata->keycode_table, visorkbd_keycode,
433 KEYCODE_TABLE_BYTES);
434 memcpy(devdata->keycode_table + KEYCODE_TABLE_BYTES,
435 visorkbd_ext_keycode, KEYCODE_TABLE_BYTES);
436 devdata->visorinput_dev = setup_client_keyboard
437 (devdata, devdata->keycode_table);
438 if (!devdata->visorinput_dev)
439 goto cleanups_register;
441 case visorinput_mouse:
442 size = sizeof(struct visor_input_channel_data);
443 err = visorbus_read_channel(dev, sizeof(struct channel_header),
446 goto cleanups_register;
447 xres = data.mouse.x_res;
448 yres = data.mouse.y_res;
449 devdata->visorinput_dev = setup_client_mouse(devdata, xres,
451 if (!devdata->visorinput_dev)
452 goto cleanups_register;
455 /* No other input devices supported */
459 dev_set_drvdata(&dev->device, devdata);
460 mutex_unlock(&devdata->lock_visor_dev);
463 * Device struct is completely set up now, with the exception of
464 * visorinput_dev being registered. We need to unlock before we
465 * register the device, because this can cause an on-stack call of
466 * visorinput_open(), which would deadlock if we had the lock.
468 if (input_register_device(devdata->visorinput_dev)) {
469 input_free_device(devdata->visorinput_dev);
470 goto err_kfree_devdata;
473 mutex_lock(&devdata->lock_visor_dev);
475 * Establish calls to visorinput_channel_interrupt() if that is the
476 * desired state that we've kept track of in interrupts_enabled while
477 * the device was being created.
479 devdata->paused = false;
480 if (devdata->interrupts_enabled)
481 visorbus_enable_channel_interrupts(dev);
482 mutex_unlock(&devdata->lock_visor_dev);
487 mutex_unlock(&devdata->lock_visor_dev);
493 static int visorinput_probe(struct visor_device *dev)
496 enum visorinput_dev_type dtype;
498 guid = visorchannel_get_guid(dev->visorchannel);
499 if (guid_equal(guid, &visor_mouse_channel_guid))
500 dtype = visorinput_mouse;
501 else if (guid_equal(guid, &visor_keyboard_channel_guid))
502 dtype = visorinput_keyboard;
505 visorbus_disable_channel_interrupts(dev);
506 if (!devdata_create(dev, dtype))
511 static void unregister_client_input(struct input_dev *visorinput_dev)
514 input_unregister_device(visorinput_dev);
517 static void visorinput_remove(struct visor_device *dev)
519 struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
524 mutex_lock(&devdata->lock_visor_dev);
525 visorbus_disable_channel_interrupts(dev);
528 * due to above, at this time no thread of execution will be in
529 * visorinput_channel_interrupt()
532 dev_set_drvdata(&dev->device, NULL);
533 mutex_unlock(&devdata->lock_visor_dev);
535 unregister_client_input(devdata->visorinput_dev);
540 * Make it so the current locking state of the locking key indicated by
541 * <keycode> is as indicated by <desired_state> (1=locked, 0=unlocked).
543 static void handle_locking_key(struct input_dev *visorinput_dev, int keycode,
562 if (test_bit(led, visorinput_dev->led) != desired_state) {
563 input_report_key(visorinput_dev, keycode, 1);
564 input_sync(visorinput_dev);
565 input_report_key(visorinput_dev, keycode, 0);
566 input_sync(visorinput_dev);
567 __change_bit(led, visorinput_dev->led);
572 * <scancode> is either a 1-byte scancode, or an extended 16-bit scancode with
573 * 0xE0 in the low byte and the extended scancode value in the next higher byte.
575 static int scancode_to_keycode(int scancode)
578 return visorkbd_ext_keycode[(scancode >> 8) & 0xff];
580 return visorkbd_keycode[scancode];
583 static int calc_button(int x)
598 * This is used only when this driver is active as an input driver in the
599 * client guest partition. It is called periodically so we can obtain inputs
600 * from the channel, and deliver them to the guest OS.
602 static void visorinput_channel_interrupt(struct visor_device *dev)
604 struct visor_inputreport r;
605 int scancode, keycode;
606 struct input_dev *visorinput_dev;
607 int xmotion, ymotion, button;
609 struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
614 visorinput_dev = devdata->visorinput_dev;
616 while (!visorchannel_signalremove(dev->visorchannel, 0, &r)) {
617 scancode = r.activity.arg1;
618 keycode = scancode_to_keycode(scancode);
619 switch (r.activity.action) {
620 case INPUTACTION_KEY_DOWN:
621 input_report_key(visorinput_dev, keycode, 1);
622 input_sync(visorinput_dev);
624 case INPUTACTION_KEY_UP:
625 input_report_key(visorinput_dev, keycode, 0);
626 input_sync(visorinput_dev);
628 case INPUTACTION_KEY_DOWN_UP:
629 input_report_key(visorinput_dev, keycode, 1);
630 input_sync(visorinput_dev);
631 input_report_key(visorinput_dev, keycode, 0);
632 input_sync(visorinput_dev);
634 case INPUTACTION_SET_LOCKING_KEY_STATE:
635 handle_locking_key(visorinput_dev, keycode,
638 case INPUTACTION_XY_MOTION:
639 xmotion = r.activity.arg1;
640 ymotion = r.activity.arg2;
641 input_report_abs(visorinput_dev, ABS_X, xmotion);
642 input_report_abs(visorinput_dev, ABS_Y, ymotion);
643 input_sync(visorinput_dev);
645 case INPUTACTION_MOUSE_BUTTON_DOWN:
646 button = calc_button(r.activity.arg1);
649 input_report_key(visorinput_dev, button, 1);
650 input_sync(visorinput_dev);
652 case INPUTACTION_MOUSE_BUTTON_UP:
653 button = calc_button(r.activity.arg1);
656 input_report_key(visorinput_dev, button, 0);
657 input_sync(visorinput_dev);
659 case INPUTACTION_MOUSE_BUTTON_CLICK:
660 button = calc_button(r.activity.arg1);
663 input_report_key(visorinput_dev, button, 1);
664 input_sync(visorinput_dev);
665 input_report_key(visorinput_dev, button, 0);
666 input_sync(visorinput_dev);
668 case INPUTACTION_MOUSE_BUTTON_DCLICK:
669 button = calc_button(r.activity.arg1);
672 for (i = 0; i < 2; i++) {
673 input_report_key(visorinput_dev, button, 1);
674 input_sync(visorinput_dev);
675 input_report_key(visorinput_dev, button, 0);
676 input_sync(visorinput_dev);
679 case INPUTACTION_WHEEL_ROTATE_AWAY:
680 input_report_rel(visorinput_dev, REL_WHEEL, 1);
681 input_sync(visorinput_dev);
683 case INPUTACTION_WHEEL_ROTATE_TOWARD:
684 input_report_rel(visorinput_dev, REL_WHEEL, -1);
685 input_sync(visorinput_dev);
688 /* Unsupported input action */
694 static int visorinput_pause(struct visor_device *dev,
695 visorbus_state_complete_func complete_func)
698 struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
705 mutex_lock(&devdata->lock_visor_dev);
706 if (devdata->paused) {
710 if (devdata->interrupts_enabled)
711 visorbus_disable_channel_interrupts(dev);
714 * due to above, at this time no thread of execution will be in
715 * visorinput_channel_interrupt()
717 devdata->paused = true;
718 complete_func(dev, 0);
721 mutex_unlock(&devdata->lock_visor_dev);
726 static int visorinput_resume(struct visor_device *dev,
727 visorbus_state_complete_func complete_func)
730 struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
736 mutex_lock(&devdata->lock_visor_dev);
737 if (!devdata->paused) {
741 devdata->paused = false;
742 complete_func(dev, 0);
745 * Re-establish calls to visorinput_channel_interrupt() if that is the
746 * desired state that we've kept track of in interrupts_enabled while
747 * the device was paused.
749 if (devdata->interrupts_enabled)
750 visorbus_enable_channel_interrupts(dev);
754 mutex_unlock(&devdata->lock_visor_dev);
759 /* GUIDS for all channel types supported by this driver. */
760 static struct visor_channeltype_descriptor visorinput_channel_types[] = {
761 { VISOR_KEYBOARD_CHANNEL_GUID, "keyboard",
762 sizeof(struct channel_header), 0 },
763 { VISOR_MOUSE_CHANNEL_GUID, "mouse", sizeof(struct channel_header), 0 },
767 static struct visor_driver visorinput_driver = {
768 .name = "visorinput",
769 .owner = THIS_MODULE,
770 .channel_types = visorinput_channel_types,
771 .probe = visorinput_probe,
772 .remove = visorinput_remove,
773 .channel_interrupt = visorinput_channel_interrupt,
774 .pause = visorinput_pause,
775 .resume = visorinput_resume,
778 module_driver(visorinput_driver, visorbus_register_visor_driver,
779 visorbus_unregister_visor_driver);
781 MODULE_DEVICE_TABLE(visorbus, visorinput_channel_types);
783 MODULE_AUTHOR("Unisys");
784 MODULE_LICENSE("GPL");
785 MODULE_DESCRIPTION("s-Par human input driver for virtual keyboard/mouse");
787 MODULE_ALIAS("visorbus:" VISOR_MOUSE_CHANNEL_GUID_STR);
788 MODULE_ALIAS("visorbus:" VISOR_KEYBOARD_CHANNEL_GUID_STR);