xref: /OK3568_Linux_fs/kernel/drivers/staging/unisys/visorinput/visorinput.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2011 - 2015 UNISYS CORPORATION
4  * All rights reserved.
5  */
6 
7 /*
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.
12  */
13 
14 #include <linux/fb.h>
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>
20 
21 /* These defines identify mouse and keyboard activity which is specified by the
22  * firmware to the host using the cmsimpleinput protocol.  @ingroup coretypes
23  */
24 /* only motion; arg1=x, arg2=y */
25 #define INPUTACTION_XY_MOTION 1
26 
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
32 
33 /* arg1: wheel rotation away from/toward user */
34 #define INPUTACTION_WHEEL_ROTATE_AWAY 6
35 #define INPUTACTION_WHEEL_ROTATE_TOWARD 7
36 
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'.
41  */
42 #define INPUTACTION_KEY_DOWN 64
43 #define INPUTACTION_KEY_UP 65
44 #define INPUTACTION_KEY_DOWN_UP 67
45 
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)
49  */
50 #define INPUTACTION_SET_LOCKING_KEY_STATE 66
51 
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"
57 
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"
63 
64 #define PIXELS_ACROSS_DEFAULT 1024
65 #define PIXELS_DOWN_DEFAULT   768
66 #define KEYCODE_TABLE_BYTES   256
67 
68 struct visor_inputactivity {
69 	u16 action;
70 	u16 arg1;
71 	u16 arg2;
72 	u16 arg3;
73 } __packed;
74 
75 struct visor_inputreport {
76 	u64 seq_no;
77 	struct visor_inputactivity activity;
78 } __packed;
79 
80 /* header of keyboard/mouse channels */
81 struct visor_input_channel_data {
82 	u32 n_input_reports;
83 	union {
84 		struct {
85 			u16 x_res;
86 			u16 y_res;
87 		} mouse;
88 		struct {
89 			u32 flags;
90 		} keyboard;
91 	};
92 } __packed;
93 
94 enum visorinput_dev_type {
95 	visorinput_keyboard,
96 	visorinput_mouse,
97 };
98 
99 /*
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
102  * struct device.
103  */
104 struct visorinput_devdata {
105 	struct visor_device *dev;
106 	/* lock for dev */
107 	struct mutex lock_visor_dev;
108 	struct input_dev *visorinput_dev;
109 	bool paused;
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[];
115 };
116 
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;
119 
120 /*
121  * Borrowed from drivers/input/keyboard/atakbd.c
122  * This maps 1-byte scancodes to keycodes.
123  */
124 static const unsigned char visorkbd_keycode[KEYCODE_TABLE_BYTES] = {
125 	/* American layout */
126 	[0] = KEY_GRAVE,
127 	[1] = KEY_ESC,
128 	[2] = KEY_1,
129 	[3] = KEY_2,
130 	[4] = KEY_3,
131 	[5] = KEY_4,
132 	[6] = KEY_5,
133 	[7] = KEY_6,
134 	[8] = KEY_7,
135 	[9] = KEY_8,
136 	[10] = KEY_9,
137 	[11] = KEY_0,
138 	[12] = KEY_MINUS,
139 	[13] = KEY_EQUAL,
140 	[14] = KEY_BACKSPACE,
141 	[15] = KEY_TAB,
142 	[16] = KEY_Q,
143 	[17] = KEY_W,
144 	[18] = KEY_E,
145 	[19] = KEY_R,
146 	[20] = KEY_T,
147 	[21] = KEY_Y,
148 	[22] = KEY_U,
149 	[23] = KEY_I,
150 	[24] = KEY_O,
151 	[25] = KEY_P,
152 	[26] = KEY_LEFTBRACE,
153 	[27] = KEY_RIGHTBRACE,
154 	[28] = KEY_ENTER,
155 	[29] = KEY_LEFTCTRL,
156 	[30] = KEY_A,
157 	[31] = KEY_S,
158 	[32] = KEY_D,
159 	[33] = KEY_F,
160 	[34] = KEY_G,
161 	[35] = KEY_H,
162 	[36] = KEY_J,
163 	[37] = KEY_K,
164 	[38] = KEY_L,
165 	[39] = KEY_SEMICOLON,
166 	[40] = KEY_APOSTROPHE,
167 	[41] = KEY_GRAVE,
168 	[42] = KEY_LEFTSHIFT,
169 	[43] = KEY_BACKSLASH,
170 	[44] = KEY_Z,
171 	[45] = KEY_X,
172 	[46] = KEY_C,
173 	[47] = KEY_V,
174 	[48] = KEY_B,
175 	[49] = KEY_N,
176 	[50] = KEY_M,
177 	[51] = KEY_COMMA,
178 	[52] = KEY_DOT,
179 	[53] = KEY_SLASH,
180 	[54] = KEY_RIGHTSHIFT,
181 	[55] = KEY_KPASTERISK,
182 	[56] = KEY_LEFTALT,
183 	[57] = KEY_SPACE,
184 	[58] = KEY_CAPSLOCK,
185 	[59] = KEY_F1,
186 	[60] = KEY_F2,
187 	[61] = KEY_F3,
188 	[62] = KEY_F4,
189 	[63] = KEY_F5,
190 	[64] = KEY_F6,
191 	[65] = KEY_F7,
192 	[66] = KEY_F8,
193 	[67] = KEY_F9,
194 	[68] = KEY_F10,
195 	[69] = KEY_NUMLOCK,
196 	[70] = KEY_SCROLLLOCK,
197 	[71] = KEY_KP7,
198 	[72] = KEY_KP8,
199 	[73] = KEY_KP9,
200 	[74] = KEY_KPMINUS,
201 	[75] = KEY_KP4,
202 	[76] = KEY_KP5,
203 	[77] = KEY_KP6,
204 	[78] = KEY_KPPLUS,
205 	[79] = KEY_KP1,
206 	[80] = KEY_KP2,
207 	[81] = KEY_KP3,
208 	[82] = KEY_KP0,
209 	[83] = KEY_KPDOT,
210 	/* enables UK backslash+pipe key and FR lessthan+greaterthan key */
211 	[86] = KEY_102ND,
212 	[87] = KEY_F11,
213 	[88] = KEY_F12,
214 	[90] = KEY_KPLEFTPAREN,
215 	[91] = KEY_KPRIGHTPAREN,
216 	[92] = KEY_KPASTERISK,
217 	[93] = KEY_KPASTERISK,
218 	[94] = KEY_KPPLUS,
219 	[95] = KEY_HELP,
220 	[96] = KEY_KPENTER,
221 	[97] = KEY_RIGHTCTRL,
222 	[98] = KEY_KPSLASH,
223 	[99] = KEY_KPLEFTPAREN,
224 	[100] = KEY_KPRIGHTPAREN,
225 	[101] = KEY_KPSLASH,
226 	[102] = KEY_HOME,
227 	[103] = KEY_UP,
228 	[104] = KEY_PAGEUP,
229 	[105] = KEY_LEFT,
230 	[106] = KEY_RIGHT,
231 	[107] = KEY_END,
232 	[108] = KEY_DOWN,
233 	[109] = KEY_PAGEDOWN,
234 	[110] = KEY_INSERT,
235 	[111] = KEY_DELETE,
236 	[112] = KEY_MACRO,
237 	[113] = KEY_MUTE
238 };
239 
240 /*
241  * This maps the <xx> in extended scancodes of the form "0xE0 <xx>" into
242  * keycodes.
243  */
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 */
258 };
259 
visorinput_open(struct input_dev * visorinput_dev)260 static int visorinput_open(struct input_dev *visorinput_dev)
261 {
262 	struct visorinput_devdata *devdata = input_get_drvdata(visorinput_dev);
263 
264 	if (!devdata) {
265 		dev_err(&visorinput_dev->dev,
266 			"%s input_get_drvdata(%p) returned NULL\n",
267 			__func__, visorinput_dev);
268 		return -EINVAL;
269 	}
270 	dev_dbg(&visorinput_dev->dev, "%s opened\n", __func__);
271 
272 	/*
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.
276 	 */
277 	mutex_lock(&devdata->lock_visor_dev);
278 	devdata->interrupts_enabled = true;
279 	if (devdata->paused)
280 		goto out_unlock;
281 	visorbus_enable_channel_interrupts(devdata->dev);
282 
283 out_unlock:
284 	mutex_unlock(&devdata->lock_visor_dev);
285 	return 0;
286 }
287 
visorinput_close(struct input_dev * visorinput_dev)288 static void visorinput_close(struct input_dev *visorinput_dev)
289 {
290 	struct visorinput_devdata *devdata = input_get_drvdata(visorinput_dev);
291 
292 	if (!devdata) {
293 		dev_err(&visorinput_dev->dev,
294 			"%s input_get_drvdata(%p) returned NULL\n",
295 			__func__, visorinput_dev);
296 		return;
297 	}
298 	dev_dbg(&visorinput_dev->dev, "%s closed\n", __func__);
299 
300 	/*
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.
304 	 */
305 	mutex_lock(&devdata->lock_visor_dev);
306 	devdata->interrupts_enabled = false;
307 	if (devdata->paused)
308 		goto out_unlock;
309 	visorbus_disable_channel_interrupts(devdata->dev);
310 
311 out_unlock:
312 	mutex_unlock(&devdata->lock_visor_dev);
313 }
314 
315 /*
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.
319  */
setup_client_keyboard(void * devdata,unsigned char * keycode_table)320 static struct input_dev *setup_client_keyboard(void *devdata,
321 					       unsigned char *keycode_table)
322 
323 {
324 	int i;
325 	struct input_dev *visorinput_dev = input_allocate_device();
326 
327 	if (!visorinput_dev)
328 		return NULL;
329 
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;
336 
337 	visorinput_dev->evbit[0] = BIT_MASK(EV_KEY) |
338 				   BIT_MASK(EV_REP) |
339 				   BIT_MASK(EV_LED);
340 	visorinput_dev->ledbit[0] = BIT_MASK(LED_CAPSL) |
341 				    BIT_MASK(LED_SCROLLL) |
342 				    BIT_MASK(LED_NUML);
343 	visorinput_dev->keycode = keycode_table;
344 	/* sizeof(unsigned char) */
345 	visorinput_dev->keycodesize = 1;
346 	visorinput_dev->keycodemax = KEYCODE_TABLE_BYTES;
347 
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);
353 
354 	visorinput_dev->open = visorinput_open;
355 	visorinput_dev->close = visorinput_close;
356 	/* pre input_register! */
357 	input_set_drvdata(visorinput_dev, devdata);
358 
359 	return visorinput_dev;
360 }
361 
setup_client_mouse(void * devdata,unsigned int xres,unsigned int yres)362 static struct input_dev *setup_client_mouse(void *devdata, unsigned int xres,
363 					    unsigned int yres)
364 {
365 	struct input_dev *visorinput_dev = input_allocate_device();
366 
367 	if (!visorinput_dev)
368 		return NULL;
369 
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;
376 
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);
381 
382 	if (xres == 0)
383 		xres = PIXELS_ACROSS_DEFAULT;
384 	if (yres == 0)
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);
388 
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);
394 
395 	return visorinput_dev;
396 }
397 
devdata_create(struct visor_device * dev,enum visorinput_dev_type dtype)398 static struct visorinput_devdata *devdata_create(struct visor_device *dev,
399 						 enum visorinput_dev_type dtype)
400 {
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;
405 
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);
410 	if (!devdata)
411 		return NULL;
412 	mutex_init(&devdata->lock_visor_dev);
413 	mutex_lock(&devdata->lock_visor_dev);
414 	devdata->dev = dev;
415 
416 	/*
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.
421 	 */
422 	devdata->paused = true;
423 
424 	/*
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
427 	 * the guest OS.
428 	 */
429 	switch (dtype) {
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;
440 		break;
441 	case visorinput_mouse:
442 		size = sizeof(struct visor_input_channel_data);
443 		err = visorbus_read_channel(dev, sizeof(struct channel_header),
444 					    &data, size);
445 		if (err)
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,
450 							     yres);
451 		if (!devdata->visorinput_dev)
452 			goto cleanups_register;
453 		break;
454 	default:
455 		/* No other input devices supported */
456 		break;
457 	}
458 
459 	dev_set_drvdata(&dev->device, devdata);
460 	mutex_unlock(&devdata->lock_visor_dev);
461 
462 	/*
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.
467 	 */
468 	if (input_register_device(devdata->visorinput_dev)) {
469 		input_free_device(devdata->visorinput_dev);
470 		goto err_kfree_devdata;
471 	}
472 
473 	mutex_lock(&devdata->lock_visor_dev);
474 	/*
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.
478 	 */
479 	devdata->paused = false;
480 	if (devdata->interrupts_enabled)
481 		visorbus_enable_channel_interrupts(dev);
482 	mutex_unlock(&devdata->lock_visor_dev);
483 
484 	return devdata;
485 
486 cleanups_register:
487 	mutex_unlock(&devdata->lock_visor_dev);
488 err_kfree_devdata:
489 	kfree(devdata);
490 	return NULL;
491 }
492 
visorinput_probe(struct visor_device * dev)493 static int visorinput_probe(struct visor_device *dev)
494 {
495 	const guid_t *guid;
496 	enum visorinput_dev_type dtype;
497 
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;
503 	else
504 		return -ENODEV;
505 	visorbus_disable_channel_interrupts(dev);
506 	if (!devdata_create(dev, dtype))
507 		return -ENOMEM;
508 	return 0;
509 }
510 
unregister_client_input(struct input_dev * visorinput_dev)511 static void unregister_client_input(struct input_dev *visorinput_dev)
512 {
513 	if (visorinput_dev)
514 		input_unregister_device(visorinput_dev);
515 }
516 
visorinput_remove(struct visor_device * dev)517 static void visorinput_remove(struct visor_device *dev)
518 {
519 	struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
520 
521 	if (!devdata)
522 		return;
523 
524 	mutex_lock(&devdata->lock_visor_dev);
525 	visorbus_disable_channel_interrupts(dev);
526 
527 	/*
528 	 * due to above, at this time no thread of execution will be in
529 	 * visorinput_channel_interrupt()
530 	 */
531 
532 	dev_set_drvdata(&dev->device, NULL);
533 	mutex_unlock(&devdata->lock_visor_dev);
534 
535 	unregister_client_input(devdata->visorinput_dev);
536 	kfree(devdata);
537 }
538 
539 /*
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).
542  */
handle_locking_key(struct input_dev * visorinput_dev,int keycode,int desired_state)543 static void handle_locking_key(struct input_dev *visorinput_dev, int keycode,
544 			       int desired_state)
545 {
546 	int led;
547 
548 	switch (keycode) {
549 	case KEY_CAPSLOCK:
550 		led = LED_CAPSL;
551 		break;
552 	case KEY_SCROLLLOCK:
553 		led = LED_SCROLLL;
554 		break;
555 	case KEY_NUMLOCK:
556 		led = LED_NUML;
557 		break;
558 	default:
559 		led = -1;
560 		return;
561 	}
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);
568 	}
569 }
570 
571 /*
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.
574  */
scancode_to_keycode(int scancode)575 static int scancode_to_keycode(int scancode)
576 {
577 	if (scancode > 0xff)
578 		return visorkbd_ext_keycode[(scancode >> 8) & 0xff];
579 
580 	return visorkbd_keycode[scancode];
581 }
582 
calc_button(int x)583 static int calc_button(int x)
584 {
585 	switch (x) {
586 	case 1:
587 		return BTN_LEFT;
588 	case 2:
589 		return BTN_MIDDLE;
590 	case 3:
591 		return BTN_RIGHT;
592 	default:
593 		return -EINVAL;
594 	}
595 }
596 
597 /*
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.
601  */
visorinput_channel_interrupt(struct visor_device * dev)602 static void visorinput_channel_interrupt(struct visor_device *dev)
603 {
604 	struct visor_inputreport r;
605 	int scancode, keycode;
606 	struct input_dev *visorinput_dev;
607 	int xmotion, ymotion, button;
608 	int i;
609 	struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
610 
611 	if (!devdata)
612 		return;
613 
614 	visorinput_dev = devdata->visorinput_dev;
615 
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);
623 			break;
624 		case INPUTACTION_KEY_UP:
625 			input_report_key(visorinput_dev, keycode, 0);
626 			input_sync(visorinput_dev);
627 			break;
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);
633 			break;
634 		case INPUTACTION_SET_LOCKING_KEY_STATE:
635 			handle_locking_key(visorinput_dev, keycode,
636 					   r.activity.arg2);
637 			break;
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);
644 			break;
645 		case INPUTACTION_MOUSE_BUTTON_DOWN:
646 			button = calc_button(r.activity.arg1);
647 			if (button < 0)
648 				break;
649 			input_report_key(visorinput_dev, button, 1);
650 			input_sync(visorinput_dev);
651 			break;
652 		case INPUTACTION_MOUSE_BUTTON_UP:
653 			button = calc_button(r.activity.arg1);
654 			if (button < 0)
655 				break;
656 			input_report_key(visorinput_dev, button, 0);
657 			input_sync(visorinput_dev);
658 			break;
659 		case INPUTACTION_MOUSE_BUTTON_CLICK:
660 			button = calc_button(r.activity.arg1);
661 			if (button < 0)
662 				break;
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);
667 			break;
668 		case INPUTACTION_MOUSE_BUTTON_DCLICK:
669 			button = calc_button(r.activity.arg1);
670 			if (button < 0)
671 				break;
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);
677 			}
678 			break;
679 		case INPUTACTION_WHEEL_ROTATE_AWAY:
680 			input_report_rel(visorinput_dev, REL_WHEEL, 1);
681 			input_sync(visorinput_dev);
682 			break;
683 		case INPUTACTION_WHEEL_ROTATE_TOWARD:
684 			input_report_rel(visorinput_dev, REL_WHEEL, -1);
685 			input_sync(visorinput_dev);
686 			break;
687 		default:
688 			/* Unsupported input action */
689 			break;
690 		}
691 	}
692 }
693 
visorinput_pause(struct visor_device * dev,visorbus_state_complete_func complete_func)694 static int visorinput_pause(struct visor_device *dev,
695 			    visorbus_state_complete_func complete_func)
696 {
697 	int rc;
698 	struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
699 
700 	if (!devdata) {
701 		rc = -ENODEV;
702 		goto out;
703 	}
704 
705 	mutex_lock(&devdata->lock_visor_dev);
706 	if (devdata->paused) {
707 		rc = -EBUSY;
708 		goto out_locked;
709 	}
710 	if (devdata->interrupts_enabled)
711 		visorbus_disable_channel_interrupts(dev);
712 
713 	/*
714 	 * due to above, at this time no thread of execution will be in
715 	 * visorinput_channel_interrupt()
716 	 */
717 	devdata->paused = true;
718 	complete_func(dev, 0);
719 	rc = 0;
720 out_locked:
721 	mutex_unlock(&devdata->lock_visor_dev);
722 out:
723 	return rc;
724 }
725 
visorinput_resume(struct visor_device * dev,visorbus_state_complete_func complete_func)726 static int visorinput_resume(struct visor_device *dev,
727 			     visorbus_state_complete_func complete_func)
728 {
729 	int rc;
730 	struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
731 
732 	if (!devdata) {
733 		rc = -ENODEV;
734 		goto out;
735 	}
736 	mutex_lock(&devdata->lock_visor_dev);
737 	if (!devdata->paused) {
738 		rc = -EBUSY;
739 		goto out_locked;
740 	}
741 	devdata->paused = false;
742 	complete_func(dev, 0);
743 
744 	/*
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.
748 	 */
749 	if (devdata->interrupts_enabled)
750 		visorbus_enable_channel_interrupts(dev);
751 
752 	rc = 0;
753 out_locked:
754 	mutex_unlock(&devdata->lock_visor_dev);
755 out:
756 	return rc;
757 }
758 
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 },
764 	{}
765 };
766 
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,
776 };
777 
778 module_driver(visorinput_driver, visorbus_register_visor_driver,
779 	      visorbus_unregister_visor_driver);
780 
781 MODULE_DEVICE_TABLE(visorbus, visorinput_channel_types);
782 
783 MODULE_AUTHOR("Unisys");
784 MODULE_LICENSE("GPL");
785 MODULE_DESCRIPTION("s-Par human input driver for virtual keyboard/mouse");
786 
787 MODULE_ALIAS("visorbus:" VISOR_MOUSE_CHANNEL_GUID_STR);
788 MODULE_ALIAS("visorbus:" VISOR_KEYBOARD_CHANNEL_GUID_STR);
789