3 * Copyright (C) 2011 - 2015 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
18 * This driver lives in a generic guest Linux partition, and registers to
19 * receive keyboard and mouse channels from the visorbus driver. It reads
20 * inputs from such channels, and delivers it to the Linux OS in the
21 * standard way the Linux expects for input drivers.
24 #include <linux/buffer_head.h>
27 #include <linux/input.h>
28 #include <linux/uaccess.h>
29 #include <linux/kernel.h>
30 #include <linux/uuid.h>
33 #include "ultrainputreport.h"
35 /* Keyboard channel {c73416d0-b0b8-44af-b304-9d2ae99f1b3d} */
36 #define SPAR_KEYBOARD_CHANNEL_PROTOCOL_UUID \
37 UUID_LE(0xc73416d0, 0xb0b8, 0x44af, \
38 0xb3, 0x4, 0x9d, 0x2a, 0xe9, 0x9f, 0x1b, 0x3d)
39 #define SPAR_KEYBOARD_CHANNEL_PROTOCOL_UUID_STR "c73416d0-b0b8-44af-b304-9d2ae99f1b3d"
41 /* Mouse channel {addf07d4-94a9-46e2-81c3-61abcdbdbd87} */
42 #define SPAR_MOUSE_CHANNEL_PROTOCOL_UUID \
43 UUID_LE(0xaddf07d4, 0x94a9, 0x46e2, \
44 0x81, 0xc3, 0x61, 0xab, 0xcd, 0xbd, 0xbd, 0x87)
45 #define SPAR_MOUSE_CHANNEL_PROTOCOL_UUID_STR \
46 "addf07d4-94a9-46e2-81c3-61abcdbdbd87"
48 #define PIXELS_ACROSS_DEFAULT 800
49 #define PIXELS_DOWN_DEFAULT 600
50 #define KEYCODE_TABLE_BYTES 256
52 enum visorinput_device_type {
58 * This is the private data that we store for each device.
59 * A pointer to this struct is maintained via
60 * dev_get_drvdata() / dev_set_drvdata() for each struct device.
62 struct visorinput_devdata {
63 struct visor_device *dev;
64 struct mutex lock_visor_dev; /* lock for dev */
65 struct input_dev *visorinput_dev;
67 bool interrupts_enabled;
68 unsigned int keycode_table_bytes; /* size of following array */
69 /* for keyboard devices: visorkbd_keycode[] + visorkbd_ext_keycode[] */
70 unsigned char keycode_table[0];
73 static const uuid_le spar_keyboard_channel_protocol_uuid =
74 SPAR_KEYBOARD_CHANNEL_PROTOCOL_UUID;
75 static const uuid_le spar_mouse_channel_protocol_uuid =
76 SPAR_MOUSE_CHANNEL_PROTOCOL_UUID;
79 * Borrowed from drivers/input/keyboard/atakbd.c
80 * This maps 1-byte scancodes to keycodes.
82 static const unsigned char visorkbd_keycode[KEYCODE_TABLE_BYTES] = {
110 [26] = KEY_LEFTBRACE,
111 [27] = KEY_RIGHTBRACE,
123 [39] = KEY_SEMICOLON,
124 [40] = KEY_APOSTROPHE,
126 [42] = KEY_LEFTSHIFT,
127 [43] = KEY_BACKSLASH,
138 [54] = KEY_RIGHTSHIFT,
139 [55] = KEY_KPASTERISK,
154 [70] = KEY_SCROLLLOCK,
168 [86] = KEY_102ND, /* enables UK backslash+pipe key,
169 * and FR lessthan+greaterthan key
173 [90] = KEY_KPLEFTPAREN,
174 [91] = KEY_KPRIGHTPAREN,
175 [92] = KEY_KPASTERISK,
176 [93] = KEY_KPASTERISK,
180 [97] = KEY_RIGHTCTRL,
182 [99] = KEY_KPLEFTPAREN,
183 [100] = KEY_KPRIGHTPAREN,
192 [109] = KEY_PAGEDOWN,
200 * This maps the <xx> in extended scancodes of the form "0xE0 <xx>" into
203 static const unsigned char visorkbd_ext_keycode[KEYCODE_TABLE_BYTES] = {
204 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
205 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
206 0, 0, 0, 0, KEY_KPENTER, KEY_RIGHTCTRL, 0, 0, /* 0x18 */
207 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
208 KEY_RIGHTALT, 0, 0, 0, 0, 0, 0, 0, /* 0x28 */
209 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 */
210 KEY_RIGHTALT /* AltGr */, 0, 0, 0, 0, 0, 0, 0, /* 0x38 */
211 0, 0, 0, 0, 0, 0, 0, KEY_HOME, /* 0x40 */
212 KEY_UP, KEY_PAGEUP, 0, KEY_LEFT, 0, KEY_RIGHT, 0, KEY_END, /* 0x48 */
213 KEY_DOWN, KEY_PAGEDOWN, KEY_INSERT, KEY_DELETE, 0, 0, 0, 0, /* 0x50 */
214 0, 0, 0, 0, 0, 0, 0, 0, /* 0x58 */
215 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 */
216 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
219 static int visorinput_open(struct input_dev *visorinput_dev)
221 struct visorinput_devdata *devdata = input_get_drvdata(visorinput_dev);
224 dev_err(&visorinput_dev->dev,
225 "%s input_get_drvdata(%p) returned NULL\n",
226 __func__, visorinput_dev);
229 dev_dbg(&visorinput_dev->dev, "%s opened\n", __func__);
232 * If we're not paused, really enable interrupts.
233 * Regardless of whether we are paused, set a flag indicating
234 * interrupts should be enabled so when we resume, interrupts
235 * will really be enabled.
237 mutex_lock(&devdata->lock_visor_dev);
238 devdata->interrupts_enabled = true;
241 visorbus_enable_channel_interrupts(devdata->dev);
244 mutex_unlock(&devdata->lock_visor_dev);
248 static void visorinput_close(struct input_dev *visorinput_dev)
250 struct visorinput_devdata *devdata = input_get_drvdata(visorinput_dev);
253 dev_err(&visorinput_dev->dev,
254 "%s input_get_drvdata(%p) returned NULL\n",
255 __func__, visorinput_dev);
258 dev_dbg(&visorinput_dev->dev, "%s closed\n", __func__);
261 * If we're not paused, really disable interrupts.
262 * Regardless of whether we are paused, set a flag indicating
263 * interrupts should be disabled so when we resume we will
264 * not re-enable them.
267 mutex_lock(&devdata->lock_visor_dev);
268 devdata->interrupts_enabled = false;
271 visorbus_disable_channel_interrupts(devdata->dev);
274 mutex_unlock(&devdata->lock_visor_dev);
278 * setup_client_keyboard() initializes and returns a Linux input node that
279 * we can use to deliver keyboard inputs to Linux. We of course do this when
280 * we see keyboard inputs coming in on a keyboard channel.
282 static struct input_dev *
283 setup_client_keyboard(void *devdata, /* opaque on purpose */
284 unsigned char *keycode_table)
288 struct input_dev *visorinput_dev;
290 visorinput_dev = input_allocate_device();
294 visorinput_dev->name = "visor Keyboard";
295 visorinput_dev->phys = "visorkbd:input0";
296 visorinput_dev->id.bustype = BUS_VIRTUAL;
297 visorinput_dev->id.vendor = 0x0001;
298 visorinput_dev->id.product = 0x0001;
299 visorinput_dev->id.version = 0x0100;
301 visorinput_dev->evbit[0] = BIT_MASK(EV_KEY) |
304 visorinput_dev->ledbit[0] = BIT_MASK(LED_CAPSL) |
305 BIT_MASK(LED_SCROLLL) |
307 visorinput_dev->keycode = keycode_table;
308 visorinput_dev->keycodesize = 1; /* sizeof(unsigned char) */
309 visorinput_dev->keycodemax = KEYCODE_TABLE_BYTES;
311 for (i = 1; i < visorinput_dev->keycodemax; i++)
312 set_bit(keycode_table[i], visorinput_dev->keybit);
313 for (i = 1; i < visorinput_dev->keycodemax; i++)
314 set_bit(keycode_table[i + KEYCODE_TABLE_BYTES],
315 visorinput_dev->keybit);
317 visorinput_dev->open = visorinput_open;
318 visorinput_dev->close = visorinput_close;
319 input_set_drvdata(visorinput_dev, devdata); /* pre input_register! */
321 return visorinput_dev;
324 static struct input_dev *
325 setup_client_mouse(void *devdata /* opaque on purpose */)
327 struct input_dev *visorinput_dev = NULL;
331 visorinput_dev = input_allocate_device();
335 visorinput_dev->name = "visor Mouse";
336 visorinput_dev->phys = "visormou:input0";
337 visorinput_dev->id.bustype = BUS_VIRTUAL;
338 visorinput_dev->id.vendor = 0x0001;
339 visorinput_dev->id.product = 0x0002;
340 visorinput_dev->id.version = 0x0100;
342 visorinput_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
343 set_bit(BTN_LEFT, visorinput_dev->keybit);
344 set_bit(BTN_RIGHT, visorinput_dev->keybit);
345 set_bit(BTN_MIDDLE, visorinput_dev->keybit);
347 if (registered_fb[0]) {
348 fb0 = registered_fb[0];
349 xres = fb0->var.xres_virtual;
350 yres = fb0->var.yres_virtual;
352 xres = PIXELS_ACROSS_DEFAULT;
353 yres = PIXELS_DOWN_DEFAULT;
355 input_set_abs_params(visorinput_dev, ABS_X, 0, xres, 0, 0);
356 input_set_abs_params(visorinput_dev, ABS_Y, 0, yres, 0, 0);
358 visorinput_dev->open = visorinput_open;
359 visorinput_dev->close = visorinput_close;
360 input_set_drvdata(visorinput_dev, devdata); /* pre input_register! */
361 input_set_capability(visorinput_dev, EV_REL, REL_WHEEL);
363 return visorinput_dev;
366 static struct visorinput_devdata *
367 devdata_create(struct visor_device *dev, enum visorinput_device_type devtype)
369 struct visorinput_devdata *devdata = NULL;
370 unsigned int extra_bytes = 0;
372 if (devtype == visorinput_keyboard)
373 /* allocate room for devdata->keycode_table, filled in below */
374 extra_bytes = KEYCODE_TABLE_BYTES * 2;
375 devdata = kzalloc(sizeof(*devdata) + extra_bytes, GFP_KERNEL);
378 mutex_init(&devdata->lock_visor_dev);
379 mutex_lock(&devdata->lock_visor_dev);
383 * visorinput_open() can be called as soon as input_register_device()
384 * happens, and that will enable channel interrupts. Setting paused
385 * prevents us from getting into visorinput_channel_interrupt() prior
386 * to the device structure being totally initialized.
388 devdata->paused = true;
391 * This is an input device in a client guest partition,
392 * so we need to create whatever input nodes are necessary to
393 * deliver our inputs to the guest OS.
396 case visorinput_keyboard:
397 devdata->keycode_table_bytes = extra_bytes;
398 memcpy(devdata->keycode_table, visorkbd_keycode,
399 KEYCODE_TABLE_BYTES);
400 memcpy(devdata->keycode_table + KEYCODE_TABLE_BYTES,
401 visorkbd_ext_keycode, KEYCODE_TABLE_BYTES);
402 devdata->visorinput_dev = setup_client_keyboard
403 (devdata, devdata->keycode_table);
404 if (!devdata->visorinput_dev)
405 goto cleanups_register;
407 case visorinput_mouse:
408 devdata->visorinput_dev = setup_client_mouse(devdata);
409 if (!devdata->visorinput_dev)
410 goto cleanups_register;
414 dev_set_drvdata(&dev->device, devdata);
415 mutex_unlock(&devdata->lock_visor_dev);
418 * Device struct is completely set up now, with the exception of
419 * visorinput_dev being registered.
420 * We need to unlock before we register the device, because this
421 * can cause an on-stack call of visorinput_open(), which would
422 * deadlock if we had the lock.
424 if (input_register_device(devdata->visorinput_dev)) {
425 input_free_device(devdata->visorinput_dev);
426 goto err_kfree_devdata;
429 mutex_lock(&devdata->lock_visor_dev);
431 * Establish calls to visorinput_channel_interrupt() if that is
432 * the desired state that we've kept track of in interrupts_enabled
433 * while the device was being created.
435 devdata->paused = false;
436 if (devdata->interrupts_enabled)
437 visorbus_enable_channel_interrupts(dev);
438 mutex_unlock(&devdata->lock_visor_dev);
443 mutex_unlock(&devdata->lock_visor_dev);
450 visorinput_probe(struct visor_device *dev)
453 enum visorinput_device_type devtype;
455 guid = visorchannel_get_uuid(dev->visorchannel);
456 if (uuid_le_cmp(guid, spar_mouse_channel_protocol_uuid) == 0)
457 devtype = visorinput_mouse;
458 else if (uuid_le_cmp(guid, spar_keyboard_channel_protocol_uuid) == 0)
459 devtype = visorinput_keyboard;
462 visorbus_disable_channel_interrupts(dev);
463 if (!devdata_create(dev, devtype))
469 unregister_client_input(struct input_dev *visorinput_dev)
472 input_unregister_device(visorinput_dev);
476 visorinput_remove(struct visor_device *dev)
478 struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
483 mutex_lock(&devdata->lock_visor_dev);
484 visorbus_disable_channel_interrupts(dev);
487 * due to above, at this time no thread of execution will be
488 * in visorinput_channel_interrupt()
491 dev_set_drvdata(&dev->device, NULL);
492 mutex_unlock(&devdata->lock_visor_dev);
494 unregister_client_input(devdata->visorinput_dev);
499 * Make it so the current locking state of the locking key indicated by
500 * <keycode> is as indicated by <desired_state> (1=locked, 0=unlocked).
503 handle_locking_key(struct input_dev *visorinput_dev,
504 int keycode, int desired_state)
522 if (test_bit(led, visorinput_dev->led) != desired_state) {
523 input_report_key(visorinput_dev, keycode, 1);
524 input_sync(visorinput_dev);
525 input_report_key(visorinput_dev, keycode, 0);
526 input_sync(visorinput_dev);
527 __change_bit(led, visorinput_dev->led);
532 * <scancode> is either a 1-byte scancode, or an extended 16-bit scancode
533 * with 0xE0 in the low byte and the extended scancode value in the next
537 scancode_to_keycode(int scancode)
542 keycode = visorkbd_ext_keycode[(scancode >> 8) & 0xff];
544 keycode = visorkbd_keycode[scancode];
564 * This is used only when this driver is active as an input driver in the
565 * client guest partition. It is called periodically so we can obtain inputs
566 * from the channel, and deliver them to the guest OS.
569 visorinput_channel_interrupt(struct visor_device *dev)
571 struct ultra_inputreport r;
572 int scancode, keycode;
573 struct input_dev *visorinput_dev;
574 int xmotion, ymotion, button;
577 struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
582 visorinput_dev = devdata->visorinput_dev;
584 while (!visorchannel_signalremove(dev->visorchannel, 0, &r)) {
585 scancode = r.activity.arg1;
586 keycode = scancode_to_keycode(scancode);
587 switch (r.activity.action) {
588 case inputaction_key_down:
589 input_report_key(visorinput_dev, keycode, 1);
590 input_sync(visorinput_dev);
592 case inputaction_key_up:
593 input_report_key(visorinput_dev, keycode, 0);
594 input_sync(visorinput_dev);
596 case inputaction_key_down_up:
597 input_report_key(visorinput_dev, keycode, 1);
598 input_sync(visorinput_dev);
599 input_report_key(visorinput_dev, keycode, 0);
600 input_sync(visorinput_dev);
602 case inputaction_set_locking_key_state:
603 handle_locking_key(visorinput_dev, keycode,
606 case inputaction_xy_motion:
607 xmotion = r.activity.arg1;
608 ymotion = r.activity.arg2;
609 input_report_abs(visorinput_dev, ABS_X, xmotion);
610 input_report_abs(visorinput_dev, ABS_Y, ymotion);
611 input_sync(visorinput_dev);
613 case inputaction_mouse_button_down:
614 button = calc_button(r.activity.arg1);
617 input_report_key(visorinput_dev, button, 1);
618 input_sync(visorinput_dev);
620 case inputaction_mouse_button_up:
621 button = calc_button(r.activity.arg1);
624 input_report_key(visorinput_dev, button, 0);
625 input_sync(visorinput_dev);
627 case inputaction_mouse_button_click:
628 button = calc_button(r.activity.arg1);
631 input_report_key(visorinput_dev, button, 1);
633 input_sync(visorinput_dev);
634 input_report_key(visorinput_dev, button, 0);
635 input_sync(visorinput_dev);
637 case inputaction_mouse_button_dclick:
638 button = calc_button(r.activity.arg1);
641 for (i = 0; i < 2; i++) {
642 input_report_key(visorinput_dev, button, 1);
643 input_sync(visorinput_dev);
644 input_report_key(visorinput_dev, button, 0);
645 input_sync(visorinput_dev);
648 case inputaction_wheel_rotate_away:
649 input_report_rel(visorinput_dev, REL_WHEEL, 1);
650 input_sync(visorinput_dev);
652 case inputaction_wheel_rotate_toward:
653 input_report_rel(visorinput_dev, REL_WHEEL, -1);
654 input_sync(visorinput_dev);
661 visorinput_pause(struct visor_device *dev,
662 visorbus_state_complete_func complete_func)
665 struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
672 mutex_lock(&devdata->lock_visor_dev);
673 if (devdata->paused) {
677 if (devdata->interrupts_enabled)
678 visorbus_disable_channel_interrupts(dev);
681 * due to above, at this time no thread of execution will be
682 * in visorinput_channel_interrupt()
685 devdata->paused = true;
686 complete_func(dev, 0);
689 mutex_unlock(&devdata->lock_visor_dev);
695 visorinput_resume(struct visor_device *dev,
696 visorbus_state_complete_func complete_func)
699 struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
705 mutex_lock(&devdata->lock_visor_dev);
706 if (!devdata->paused) {
710 devdata->paused = false;
711 complete_func(dev, 0);
714 * Re-establish calls to visorinput_channel_interrupt() if that is
715 * the desired state that we've kept track of in interrupts_enabled
716 * while the device was paused.
718 if (devdata->interrupts_enabled)
719 visorbus_enable_channel_interrupts(dev);
723 mutex_unlock(&devdata->lock_visor_dev);
728 /* GUIDS for all channel types supported by this driver. */
729 static struct visor_channeltype_descriptor visorinput_channel_types[] = {
730 { SPAR_KEYBOARD_CHANNEL_PROTOCOL_UUID, "keyboard"},
731 { SPAR_MOUSE_CHANNEL_PROTOCOL_UUID, "mouse"},
732 { NULL_UUID_LE, NULL }
735 static struct visor_driver visorinput_driver = {
736 .name = "visorinput",
737 .owner = THIS_MODULE,
738 .channel_types = visorinput_channel_types,
739 .probe = visorinput_probe,
740 .remove = visorinput_remove,
741 .channel_interrupt = visorinput_channel_interrupt,
742 .pause = visorinput_pause,
743 .resume = visorinput_resume,
747 visorinput_init(void)
749 return visorbus_register_visor_driver(&visorinput_driver);
753 visorinput_cleanup(void)
755 visorbus_unregister_visor_driver(&visorinput_driver);
758 module_init(visorinput_init);
759 module_exit(visorinput_cleanup);
761 MODULE_DEVICE_TABLE(visorbus, visorinput_channel_types);
763 MODULE_AUTHOR("Unisys");
764 MODULE_LICENSE("GPL");
765 MODULE_DESCRIPTION("s-Par human input driver for virtual keyboard/mouse");
767 MODULE_ALIAS("visorbus:" SPAR_MOUSE_CHANNEL_PROTOCOL_UUID_STR);
768 MODULE_ALIAS("visorbus:" SPAR_KEYBOARD_CHANNEL_PROTOCOL_UUID_STR);