GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / staging / unisys / visorinput / visorinput.c
1 /*
2  * Copyright (C) 2011 - 2015 UNISYS CORPORATION
3  * All rights reserved.
4  *
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.
8  *
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
13  * details.
14  */
15
16 /*
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.
21  */
22
23 #include <linux/fb.h>
24 #include <linux/input.h>
25 #include <linux/kernel.h>
26 #include <linux/uuid.h>
27
28 #include "visorbus.h"
29 #include "ultrainputreport.h"
30
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"
36
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"
42
43 #define PIXELS_ACROSS_DEFAULT 800
44 #define PIXELS_DOWN_DEFAULT   600
45 #define KEYCODE_TABLE_BYTES   256
46
47 enum visorinput_device_type {
48         visorinput_keyboard,
49         visorinput_mouse,
50 };
51
52 /*
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.
56  */
57 struct visorinput_devdata {
58         struct visor_device *dev;
59         /* lock for dev */
60         struct mutex lock_visor_dev;
61         struct input_dev *visorinput_dev;
62         bool paused;
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];
68 };
69
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;
72
73 /*
74  * Borrowed from drivers/input/keyboard/atakbd.c
75  * This maps 1-byte scancodes to keycodes.
76  */
77 static const unsigned char visorkbd_keycode[KEYCODE_TABLE_BYTES] = {
78         /* American layout */
79         [0] = KEY_GRAVE,
80         [1] = KEY_ESC,
81         [2] = KEY_1,
82         [3] = KEY_2,
83         [4] = KEY_3,
84         [5] = KEY_4,
85         [6] = KEY_5,
86         [7] = KEY_6,
87         [8] = KEY_7,
88         [9] = KEY_8,
89         [10] = KEY_9,
90         [11] = KEY_0,
91         [12] = KEY_MINUS,
92         [13] = KEY_EQUAL,
93         [14] = KEY_BACKSPACE,
94         [15] = KEY_TAB,
95         [16] = KEY_Q,
96         [17] = KEY_W,
97         [18] = KEY_E,
98         [19] = KEY_R,
99         [20] = KEY_T,
100         [21] = KEY_Y,
101         [22] = KEY_U,
102         [23] = KEY_I,
103         [24] = KEY_O,
104         [25] = KEY_P,
105         [26] = KEY_LEFTBRACE,
106         [27] = KEY_RIGHTBRACE,
107         [28] = KEY_ENTER,
108         [29] = KEY_LEFTCTRL,
109         [30] = KEY_A,
110         [31] = KEY_S,
111         [32] = KEY_D,
112         [33] = KEY_F,
113         [34] = KEY_G,
114         [35] = KEY_H,
115         [36] = KEY_J,
116         [37] = KEY_K,
117         [38] = KEY_L,
118         [39] = KEY_SEMICOLON,
119         [40] = KEY_APOSTROPHE,
120         [41] = KEY_GRAVE,
121         [42] = KEY_LEFTSHIFT,
122         [43] = KEY_BACKSLASH,
123         [44] = KEY_Z,
124         [45] = KEY_X,
125         [46] = KEY_C,
126         [47] = KEY_V,
127         [48] = KEY_B,
128         [49] = KEY_N,
129         [50] = KEY_M,
130         [51] = KEY_COMMA,
131         [52] = KEY_DOT,
132         [53] = KEY_SLASH,
133         [54] = KEY_RIGHTSHIFT,
134         [55] = KEY_KPASTERISK,
135         [56] = KEY_LEFTALT,
136         [57] = KEY_SPACE,
137         [58] = KEY_CAPSLOCK,
138         [59] = KEY_F1,
139         [60] = KEY_F2,
140         [61] = KEY_F3,
141         [62] = KEY_F4,
142         [63] = KEY_F5,
143         [64] = KEY_F6,
144         [65] = KEY_F7,
145         [66] = KEY_F8,
146         [67] = KEY_F9,
147         [68] = KEY_F10,
148         [69] = KEY_NUMLOCK,
149         [70] = KEY_SCROLLLOCK,
150         [71] = KEY_KP7,
151         [72] = KEY_KP8,
152         [73] = KEY_KP9,
153         [74] = KEY_KPMINUS,
154         [75] = KEY_KP4,
155         [76] = KEY_KP5,
156         [77] = KEY_KP6,
157         [78] = KEY_KPPLUS,
158         [79] = KEY_KP1,
159         [80] = KEY_KP2,
160         [81] = KEY_KP3,
161         [82] = KEY_KP0,
162         [83] = KEY_KPDOT,
163         /* enables UK backslash+pipe key and FR lessthan+greaterthan key */
164         [86] = KEY_102ND,
165         [87] = KEY_F11,
166         [88] = KEY_F12,
167         [90] = KEY_KPLEFTPAREN,
168         [91] = KEY_KPRIGHTPAREN,
169         [92] = KEY_KPASTERISK,
170         [93] = KEY_KPASTERISK,
171         [94] = KEY_KPPLUS,
172         [95] = KEY_HELP,
173         [96] = KEY_KPENTER,
174         [97] = KEY_RIGHTCTRL,
175         [98] = KEY_KPSLASH,
176         [99] = KEY_KPLEFTPAREN,
177         [100] = KEY_KPRIGHTPAREN,
178         [101] = KEY_KPSLASH,
179         [102] = KEY_HOME,
180         [103] = KEY_UP,
181         [104] = KEY_PAGEUP,
182         [105] = KEY_LEFT,
183         [106] = KEY_RIGHT,
184         [107] = KEY_END,
185         [108] = KEY_DOWN,
186         [109] = KEY_PAGEDOWN,
187         [110] = KEY_INSERT,
188         [111] = KEY_DELETE,
189         [112] = KEY_MACRO,
190         [113] = KEY_MUTE
191 };
192
193 /*
194  * This maps the <xx> in extended scancodes of the form "0xE0 <xx>" into
195  * keycodes.
196  */
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 */
211 };
212
213 static int visorinput_open(struct input_dev *visorinput_dev)
214 {
215         struct visorinput_devdata *devdata = input_get_drvdata(visorinput_dev);
216
217         if (!devdata) {
218                 dev_err(&visorinput_dev->dev,
219                         "%s input_get_drvdata(%p) returned NULL\n",
220                         __func__, visorinput_dev);
221                 return -EINVAL;
222         }
223         dev_dbg(&visorinput_dev->dev, "%s opened\n", __func__);
224
225         /*
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.
230          */
231         mutex_lock(&devdata->lock_visor_dev);
232         devdata->interrupts_enabled = true;
233         if (devdata->paused)
234                 goto out_unlock;
235         visorbus_enable_channel_interrupts(devdata->dev);
236
237 out_unlock:
238         mutex_unlock(&devdata->lock_visor_dev);
239         return 0;
240 }
241
242 static void visorinput_close(struct input_dev *visorinput_dev)
243 {
244         struct visorinput_devdata *devdata = input_get_drvdata(visorinput_dev);
245
246         if (!devdata) {
247                 dev_err(&visorinput_dev->dev,
248                         "%s input_get_drvdata(%p) returned NULL\n",
249                         __func__, visorinput_dev);
250                 return;
251         }
252         dev_dbg(&visorinput_dev->dev, "%s closed\n", __func__);
253
254         /*
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.
259          */
260         mutex_lock(&devdata->lock_visor_dev);
261         devdata->interrupts_enabled = false;
262         if (devdata->paused)
263                 goto out_unlock;
264         visorbus_disable_channel_interrupts(devdata->dev);
265
266 out_unlock:
267         mutex_unlock(&devdata->lock_visor_dev);
268 }
269
270 /*
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.
274  */
275 static struct input_dev *setup_client_keyboard(void *devdata,
276                                                unsigned char *keycode_table)
277
278 {
279         int i;
280         struct input_dev *visorinput_dev = input_allocate_device();
281
282         if (!visorinput_dev)
283                 return NULL;
284
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;
291
292         visorinput_dev->evbit[0] = BIT_MASK(EV_KEY) |
293                                    BIT_MASK(EV_REP) |
294                                    BIT_MASK(EV_LED);
295         visorinput_dev->ledbit[0] = BIT_MASK(LED_CAPSL) |
296                                     BIT_MASK(LED_SCROLLL) |
297                                     BIT_MASK(LED_NUML);
298         visorinput_dev->keycode = keycode_table;
299         /* sizeof(unsigned char) */
300         visorinput_dev->keycodesize = 1;
301         visorinput_dev->keycodemax = KEYCODE_TABLE_BYTES;
302
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);
308
309         visorinput_dev->open = visorinput_open;
310         visorinput_dev->close = visorinput_close;
311         /* pre input_register! */
312         input_set_drvdata(visorinput_dev, devdata);
313
314         return visorinput_dev;
315 }
316
317 static struct input_dev *setup_client_mouse(void *devdata)
318 {
319         int xres, yres;
320         struct fb_info *fb0;
321         struct input_dev *visorinput_dev = input_allocate_device();
322
323         if (!visorinput_dev)
324                 return NULL;
325
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;
332
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);
337
338         if (registered_fb[0]) {
339                 fb0 = registered_fb[0];
340                 xres = fb0->var.xres_virtual;
341                 yres = fb0->var.yres_virtual;
342         } else {
343                 xres = PIXELS_ACROSS_DEFAULT;
344                 yres = PIXELS_DOWN_DEFAULT;
345         }
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);
348
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);
354
355         return visorinput_dev;
356 }
357
358 static struct visorinput_devdata *devdata_create(
359                                         struct visor_device *dev,
360                                         enum visorinput_device_type devtype)
361 {
362         struct visorinput_devdata *devdata = NULL;
363         unsigned int extra_bytes = 0;
364
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);
369         if (!devdata)
370                 return NULL;
371         mutex_init(&devdata->lock_visor_dev);
372         mutex_lock(&devdata->lock_visor_dev);
373         devdata->dev = dev;
374
375         /*
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.
380          */
381         devdata->paused = true;
382
383         /*
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.
387          */
388         switch (devtype) {
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;
399                 break;
400         case visorinput_mouse:
401                 devdata->visorinput_dev = setup_client_mouse(devdata);
402                 if (!devdata->visorinput_dev)
403                         goto cleanups_register;
404                 break;
405         default:
406                 /* No other input devices supported */
407                 break;
408         }
409
410         dev_set_drvdata(&dev->device, devdata);
411         mutex_unlock(&devdata->lock_visor_dev);
412
413         /*
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.
419          */
420         if (input_register_device(devdata->visorinput_dev)) {
421                 input_free_device(devdata->visorinput_dev);
422                 goto err_kfree_devdata;
423         }
424
425         mutex_lock(&devdata->lock_visor_dev);
426         /*
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.
430          */
431         devdata->paused = false;
432         if (devdata->interrupts_enabled)
433                 visorbus_enable_channel_interrupts(dev);
434         mutex_unlock(&devdata->lock_visor_dev);
435
436         return devdata;
437
438 cleanups_register:
439         mutex_unlock(&devdata->lock_visor_dev);
440 err_kfree_devdata:
441         kfree(devdata);
442         return NULL;
443 }
444
445 static int visorinput_probe(struct visor_device *dev)
446 {
447         const guid_t *guid;
448         enum visorinput_device_type devtype;
449
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;
455         else
456                 return -ENODEV;
457         visorbus_disable_channel_interrupts(dev);
458         if (!devdata_create(dev, devtype))
459                 return -ENOMEM;
460         return 0;
461 }
462
463 static void unregister_client_input(struct input_dev *visorinput_dev)
464 {
465         if (visorinput_dev)
466                 input_unregister_device(visorinput_dev);
467 }
468
469 static void visorinput_remove(struct visor_device *dev)
470 {
471         struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
472
473         if (!devdata)
474                 return;
475
476         mutex_lock(&devdata->lock_visor_dev);
477         visorbus_disable_channel_interrupts(dev);
478
479         /*
480          * due to above, at this time no thread of execution will be
481          * in visorinput_channel_interrupt()
482          */
483
484         dev_set_drvdata(&dev->device, NULL);
485         mutex_unlock(&devdata->lock_visor_dev);
486
487         unregister_client_input(devdata->visorinput_dev);
488         kfree(devdata);
489 }
490
491 /*
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).
494  */
495 static void handle_locking_key(struct input_dev *visorinput_dev, int keycode,
496                                int desired_state)
497 {
498         int led;
499
500         switch (keycode) {
501         case KEY_CAPSLOCK:
502                 led = LED_CAPSL;
503                 break;
504         case KEY_SCROLLLOCK:
505                 led = LED_SCROLLL;
506                 break;
507         case KEY_NUMLOCK:
508                 led = LED_NUML;
509                 break;
510         default:
511                 led = -1;
512                 return;
513         }
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);
520         }
521 }
522
523 /*
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
526  * higher byte.
527  */
528 static int scancode_to_keycode(int scancode)
529 {
530         if (scancode > 0xff)
531                 return visorkbd_ext_keycode[(scancode >> 8) & 0xff];
532
533         return visorkbd_keycode[scancode];
534 }
535
536 static int calc_button(int x)
537 {
538         switch (x) {
539         case 1:
540                 return BTN_LEFT;
541         case 2:
542                 return BTN_MIDDLE;
543         case 3:
544                 return BTN_RIGHT;
545         default:
546                 return -EINVAL;
547         }
548 }
549
550 /*
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.
554  */
555 static void visorinput_channel_interrupt(struct visor_device *dev)
556 {
557         struct visor_inputreport r;
558         int scancode, keycode;
559         struct input_dev *visorinput_dev;
560         int xmotion, ymotion, button;
561         int i;
562         struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
563
564         if (!devdata)
565                 return;
566
567         visorinput_dev = devdata->visorinput_dev;
568
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);
576                         break;
577                 case INPUTACTION_KEY_UP:
578                         input_report_key(visorinput_dev, keycode, 0);
579                         input_sync(visorinput_dev);
580                         break;
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);
586                         break;
587                 case INPUTACTION_SET_LOCKING_KEY_STATE:
588                         handle_locking_key(visorinput_dev, keycode,
589                                            r.activity.arg2);
590                         break;
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);
597                         break;
598                 case INPUTACTION_MOUSE_BUTTON_DOWN:
599                         button = calc_button(r.activity.arg1);
600                         if (button < 0)
601                                 break;
602                         input_report_key(visorinput_dev, button, 1);
603                         input_sync(visorinput_dev);
604                         break;
605                 case INPUTACTION_MOUSE_BUTTON_UP:
606                         button = calc_button(r.activity.arg1);
607                         if (button < 0)
608                                 break;
609                         input_report_key(visorinput_dev, button, 0);
610                         input_sync(visorinput_dev);
611                         break;
612                 case INPUTACTION_MOUSE_BUTTON_CLICK:
613                         button = calc_button(r.activity.arg1);
614                         if (button < 0)
615                                 break;
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);
620                         break;
621                 case INPUTACTION_MOUSE_BUTTON_DCLICK:
622                         button = calc_button(r.activity.arg1);
623                         if (button < 0)
624                                 break;
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);
630                         }
631                         break;
632                 case INPUTACTION_WHEEL_ROTATE_AWAY:
633                         input_report_rel(visorinput_dev, REL_WHEEL, 1);
634                         input_sync(visorinput_dev);
635                         break;
636                 case INPUTACTION_WHEEL_ROTATE_TOWARD:
637                         input_report_rel(visorinput_dev, REL_WHEEL, -1);
638                         input_sync(visorinput_dev);
639                         break;
640                 default:
641                         /* Unsupported input action */
642                         break;
643                 }
644         }
645 }
646
647 static int visorinput_pause(struct visor_device *dev,
648                             visorbus_state_complete_func complete_func)
649 {
650         int rc;
651         struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
652
653         if (!devdata) {
654                 rc = -ENODEV;
655                 goto out;
656         }
657
658         mutex_lock(&devdata->lock_visor_dev);
659         if (devdata->paused) {
660                 rc = -EBUSY;
661                 goto out_locked;
662         }
663         if (devdata->interrupts_enabled)
664                 visorbus_disable_channel_interrupts(dev);
665
666         /*
667          * due to above, at this time no thread of execution will be
668          * in visorinput_channel_interrupt()
669          */
670         devdata->paused = true;
671         complete_func(dev, 0);
672         rc = 0;
673 out_locked:
674         mutex_unlock(&devdata->lock_visor_dev);
675 out:
676         return rc;
677 }
678
679 static int visorinput_resume(struct visor_device *dev,
680                              visorbus_state_complete_func complete_func)
681 {
682         int rc;
683         struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
684
685         if (!devdata) {
686                 rc = -ENODEV;
687                 goto out;
688         }
689         mutex_lock(&devdata->lock_visor_dev);
690         if (!devdata->paused) {
691                 rc = -EBUSY;
692                 goto out_locked;
693         }
694         devdata->paused = false;
695         complete_func(dev, 0);
696
697         /*
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.
701          */
702         if (devdata->interrupts_enabled)
703                 visorbus_enable_channel_interrupts(dev);
704
705         rc = 0;
706 out_locked:
707         mutex_unlock(&devdata->lock_visor_dev);
708 out:
709         return rc;
710 }
711
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"},
716         {}
717 };
718
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,
728 };
729
730 module_driver(visorinput_driver, visorbus_register_visor_driver,
731               visorbus_unregister_visor_driver);
732
733 MODULE_DEVICE_TABLE(visorbus, visorinput_channel_types);
734
735 MODULE_AUTHOR("Unisys");
736 MODULE_LICENSE("GPL");
737 MODULE_DESCRIPTION("s-Par human input driver for virtual keyboard/mouse");
738
739 MODULE_ALIAS("visorbus:" VISOR_MOUSE_CHANNEL_GUID_STR);
740 MODULE_ALIAS("visorbus:" VISOR_KEYBOARD_CHANNEL_GUID_STR);