Linux-libre 4.9.189-gnu
[librecmc/linux-libre.git] / drivers / staging / unisys / visorinput / visorinput.c
1 /* visorinput.c
2  *
3  * Copyright (C) 2011 - 2015 UNISYS CORPORATION
4  * All rights reserved.
5  *
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.
9  *
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
14  * details.
15  */
16
17 /*
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.
22  */
23
24 #include <linux/buffer_head.h>
25 #include <linux/fb.h>
26 #include <linux/fs.h>
27 #include <linux/input.h>
28 #include <linux/uaccess.h>
29 #include <linux/kernel.h>
30 #include <linux/uuid.h>
31
32 #include "visorbus.h"
33 #include "ultrainputreport.h"
34
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"
40
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"
47
48 #define PIXELS_ACROSS_DEFAULT   800
49 #define PIXELS_DOWN_DEFAULT     600
50 #define KEYCODE_TABLE_BYTES     256
51
52 enum visorinput_device_type {
53         visorinput_keyboard,
54         visorinput_mouse,
55 };
56
57 /*
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.
61  */
62 struct visorinput_devdata {
63         struct visor_device *dev;
64         struct mutex lock_visor_dev; /* lock for dev */
65         struct input_dev *visorinput_dev;
66         bool paused;
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];
71 };
72
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;
77
78 /*
79  * Borrowed from drivers/input/keyboard/atakbd.c
80  * This maps 1-byte scancodes to keycodes.
81  */
82 static const unsigned char visorkbd_keycode[KEYCODE_TABLE_BYTES] = {
83         /* American layout */
84         [0] = KEY_GRAVE,
85         [1] = KEY_ESC,
86         [2] = KEY_1,
87         [3] = KEY_2,
88         [4] = KEY_3,
89         [5] = KEY_4,
90         [6] = KEY_5,
91         [7] = KEY_6,
92         [8] = KEY_7,
93         [9] = KEY_8,
94         [10] = KEY_9,
95         [11] = KEY_0,
96         [12] = KEY_MINUS,
97         [13] = KEY_EQUAL,
98         [14] = KEY_BACKSPACE,
99         [15] = KEY_TAB,
100         [16] = KEY_Q,
101         [17] = KEY_W,
102         [18] = KEY_E,
103         [19] = KEY_R,
104         [20] = KEY_T,
105         [21] = KEY_Y,
106         [22] = KEY_U,
107         [23] = KEY_I,
108         [24] = KEY_O,
109         [25] = KEY_P,
110         [26] = KEY_LEFTBRACE,
111         [27] = KEY_RIGHTBRACE,
112         [28] = KEY_ENTER,
113         [29] = KEY_LEFTCTRL,
114         [30] = KEY_A,
115         [31] = KEY_S,
116         [32] = KEY_D,
117         [33] = KEY_F,
118         [34] = KEY_G,
119         [35] = KEY_H,
120         [36] = KEY_J,
121         [37] = KEY_K,
122         [38] = KEY_L,
123         [39] = KEY_SEMICOLON,
124         [40] = KEY_APOSTROPHE,
125         [41] = KEY_GRAVE,
126         [42] = KEY_LEFTSHIFT,
127         [43] = KEY_BACKSLASH,
128         [44] = KEY_Z,
129         [45] = KEY_X,
130         [46] = KEY_C,
131         [47] = KEY_V,
132         [48] = KEY_B,
133         [49] = KEY_N,
134         [50] = KEY_M,
135         [51] = KEY_COMMA,
136         [52] = KEY_DOT,
137         [53] = KEY_SLASH,
138         [54] = KEY_RIGHTSHIFT,
139         [55] = KEY_KPASTERISK,
140         [56] = KEY_LEFTALT,
141         [57] = KEY_SPACE,
142         [58] = KEY_CAPSLOCK,
143         [59] = KEY_F1,
144         [60] = KEY_F2,
145         [61] = KEY_F3,
146         [62] = KEY_F4,
147         [63] = KEY_F5,
148         [64] = KEY_F6,
149         [65] = KEY_F7,
150         [66] = KEY_F8,
151         [67] = KEY_F9,
152         [68] = KEY_F10,
153         [69] = KEY_NUMLOCK,
154         [70] = KEY_SCROLLLOCK,
155         [71] = KEY_KP7,
156         [72] = KEY_KP8,
157         [73] = KEY_KP9,
158         [74] = KEY_KPMINUS,
159         [75] = KEY_KP4,
160         [76] = KEY_KP5,
161         [77] = KEY_KP6,
162         [78] = KEY_KPPLUS,
163         [79] = KEY_KP1,
164         [80] = KEY_KP2,
165         [81] = KEY_KP3,
166         [82] = KEY_KP0,
167         [83] = KEY_KPDOT,
168         [86] = KEY_102ND, /* enables UK backslash+pipe key,
169                            * and FR lessthan+greaterthan key
170                            */
171         [87] = KEY_F11,
172         [88] = KEY_F12,
173         [90] = KEY_KPLEFTPAREN,
174         [91] = KEY_KPRIGHTPAREN,
175         [92] = KEY_KPASTERISK,
176         [93] = KEY_KPASTERISK,
177         [94] = KEY_KPPLUS,
178         [95] = KEY_HELP,
179         [96] = KEY_KPENTER,
180         [97] = KEY_RIGHTCTRL,
181         [98] = KEY_KPSLASH,
182         [99] = KEY_KPLEFTPAREN,
183         [100] = KEY_KPRIGHTPAREN,
184         [101] = KEY_KPSLASH,
185         [102] = KEY_HOME,
186         [103] = KEY_UP,
187         [104] = KEY_PAGEUP,
188         [105] = KEY_LEFT,
189         [106] = KEY_RIGHT,
190         [107] = KEY_END,
191         [108] = KEY_DOWN,
192         [109] = KEY_PAGEDOWN,
193         [110] = KEY_INSERT,
194         [111] = KEY_DELETE,
195         [112] = KEY_MACRO,
196         [113] = KEY_MUTE
197 };
198
199 /*
200  * This maps the <xx> in extended scancodes of the form "0xE0 <xx>" into
201  * keycodes.
202  */
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 */
217 };
218
219 static int visorinput_open(struct input_dev *visorinput_dev)
220 {
221         struct visorinput_devdata *devdata = input_get_drvdata(visorinput_dev);
222
223         if (!devdata) {
224                 dev_err(&visorinput_dev->dev,
225                         "%s input_get_drvdata(%p) returned NULL\n",
226                         __func__, visorinput_dev);
227                 return -EINVAL;
228         }
229         dev_dbg(&visorinput_dev->dev, "%s opened\n", __func__);
230
231         /*
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.
236          */
237         mutex_lock(&devdata->lock_visor_dev);
238         devdata->interrupts_enabled = true;
239         if (devdata->paused)
240                 goto out_unlock;
241         visorbus_enable_channel_interrupts(devdata->dev);
242
243 out_unlock:
244         mutex_unlock(&devdata->lock_visor_dev);
245         return 0;
246 }
247
248 static void visorinput_close(struct input_dev *visorinput_dev)
249 {
250         struct visorinput_devdata *devdata = input_get_drvdata(visorinput_dev);
251
252         if (!devdata) {
253                 dev_err(&visorinput_dev->dev,
254                         "%s input_get_drvdata(%p) returned NULL\n",
255                         __func__, visorinput_dev);
256                 return;
257         }
258         dev_dbg(&visorinput_dev->dev, "%s closed\n", __func__);
259
260         /*
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.
265          */
266
267         mutex_lock(&devdata->lock_visor_dev);
268         devdata->interrupts_enabled = false;
269         if (devdata->paused)
270                 goto out_unlock;
271         visorbus_disable_channel_interrupts(devdata->dev);
272
273 out_unlock:
274         mutex_unlock(&devdata->lock_visor_dev);
275 }
276
277 /*
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.
281  */
282 static struct input_dev *
283 setup_client_keyboard(void *devdata,  /* opaque on purpose */
284                       unsigned char *keycode_table)
285
286 {
287         int i;
288         struct input_dev *visorinput_dev;
289
290         visorinput_dev = input_allocate_device();
291         if (!visorinput_dev)
292                 return NULL;
293
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;
300
301         visorinput_dev->evbit[0] = BIT_MASK(EV_KEY) |
302                                    BIT_MASK(EV_REP) |
303                                    BIT_MASK(EV_LED);
304         visorinput_dev->ledbit[0] = BIT_MASK(LED_CAPSL) |
305                                     BIT_MASK(LED_SCROLLL) |
306                                     BIT_MASK(LED_NUML);
307         visorinput_dev->keycode = keycode_table;
308         visorinput_dev->keycodesize = 1; /* sizeof(unsigned char) */
309         visorinput_dev->keycodemax = KEYCODE_TABLE_BYTES;
310
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);
316
317         visorinput_dev->open = visorinput_open;
318         visorinput_dev->close = visorinput_close;
319         input_set_drvdata(visorinput_dev, devdata); /* pre input_register! */
320
321         return visorinput_dev;
322 }
323
324 static struct input_dev *
325 setup_client_mouse(void *devdata /* opaque on purpose */)
326 {
327         struct input_dev *visorinput_dev = NULL;
328         int xres, yres;
329         struct fb_info *fb0;
330
331         visorinput_dev = input_allocate_device();
332         if (!visorinput_dev)
333                 return NULL;
334
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;
341
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);
346
347         if (registered_fb[0]) {
348                 fb0 = registered_fb[0];
349                 xres = fb0->var.xres_virtual;
350                 yres = fb0->var.yres_virtual;
351         } else {
352                 xres = PIXELS_ACROSS_DEFAULT;
353                 yres = PIXELS_DOWN_DEFAULT;
354         }
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);
357
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);
362
363         return visorinput_dev;
364 }
365
366 static struct visorinput_devdata *
367 devdata_create(struct visor_device *dev, enum visorinput_device_type devtype)
368 {
369         struct visorinput_devdata *devdata = NULL;
370         unsigned int extra_bytes = 0;
371
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);
376         if (!devdata)
377                 return NULL;
378         mutex_init(&devdata->lock_visor_dev);
379         mutex_lock(&devdata->lock_visor_dev);
380         devdata->dev = dev;
381
382         /*
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.
387          */
388         devdata->paused = true;
389
390         /*
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.
394          */
395         switch (devtype) {
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;
406                 break;
407         case visorinput_mouse:
408                 devdata->visorinput_dev = setup_client_mouse(devdata);
409                 if (!devdata->visorinput_dev)
410                         goto cleanups_register;
411                 break;
412         }
413
414         dev_set_drvdata(&dev->device, devdata);
415         mutex_unlock(&devdata->lock_visor_dev);
416
417         /*
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.
423          */
424         if (input_register_device(devdata->visorinput_dev)) {
425                 input_free_device(devdata->visorinput_dev);
426                 goto err_kfree_devdata;
427         }
428
429         mutex_lock(&devdata->lock_visor_dev);
430         /*
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.
434          */
435         devdata->paused = false;
436         if (devdata->interrupts_enabled)
437                 visorbus_enable_channel_interrupts(dev);
438         mutex_unlock(&devdata->lock_visor_dev);
439
440         return devdata;
441
442 cleanups_register:
443         mutex_unlock(&devdata->lock_visor_dev);
444 err_kfree_devdata:
445         kfree(devdata);
446         return NULL;
447 }
448
449 static int
450 visorinput_probe(struct visor_device *dev)
451 {
452         uuid_le guid;
453         enum visorinput_device_type devtype;
454
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;
460         else
461                 return -ENODEV;
462         visorbus_disable_channel_interrupts(dev);
463         if (!devdata_create(dev, devtype))
464                 return -ENOMEM;
465         return 0;
466 }
467
468 static void
469 unregister_client_input(struct input_dev *visorinput_dev)
470 {
471         if (visorinput_dev)
472                 input_unregister_device(visorinput_dev);
473 }
474
475 static void
476 visorinput_remove(struct visor_device *dev)
477 {
478         struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
479
480         if (!devdata)
481                 return;
482
483         mutex_lock(&devdata->lock_visor_dev);
484         visorbus_disable_channel_interrupts(dev);
485
486         /*
487          * due to above, at this time no thread of execution will be
488          * in visorinput_channel_interrupt()
489          */
490
491         dev_set_drvdata(&dev->device, NULL);
492         mutex_unlock(&devdata->lock_visor_dev);
493
494         unregister_client_input(devdata->visorinput_dev);
495         kfree(devdata);
496 }
497
498 /*
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).
501  */
502 static void
503 handle_locking_key(struct input_dev *visorinput_dev,
504                    int keycode, int desired_state)
505 {
506         int led;
507
508         switch (keycode) {
509         case KEY_CAPSLOCK:
510                 led = LED_CAPSL;
511                 break;
512         case KEY_SCROLLLOCK:
513                 led = LED_SCROLLL;
514                 break;
515         case KEY_NUMLOCK:
516                 led = LED_NUML;
517                 break;
518         default:
519                 led = -1;
520                 return;
521         }
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);
528         }
529 }
530
531 /*
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
534  * higher byte.
535  */
536 static int
537 scancode_to_keycode(int scancode)
538 {
539         int keycode;
540
541         if (scancode > 0xff)
542                 keycode = visorkbd_ext_keycode[(scancode >> 8) & 0xff];
543         else
544                 keycode = visorkbd_keycode[scancode];
545         return keycode;
546 }
547
548 static int
549 calc_button(int x)
550 {
551         switch (x) {
552         case 1:
553                 return BTN_LEFT;
554         case 2:
555                 return BTN_MIDDLE;
556         case 3:
557                 return BTN_RIGHT;
558         default:
559                 return -EINVAL;
560         }
561 }
562
563 /*
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.
567  */
568 static void
569 visorinput_channel_interrupt(struct visor_device *dev)
570 {
571         struct ultra_inputreport r;
572         int scancode, keycode;
573         struct input_dev *visorinput_dev;
574         int xmotion, ymotion, button;
575         int i;
576
577         struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
578
579         if (!devdata)
580                 return;
581
582         visorinput_dev = devdata->visorinput_dev;
583
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);
591                         break;
592                 case inputaction_key_up:
593                         input_report_key(visorinput_dev, keycode, 0);
594                         input_sync(visorinput_dev);
595                         break;
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);
601                         break;
602                 case inputaction_set_locking_key_state:
603                         handle_locking_key(visorinput_dev, keycode,
604                                            r.activity.arg2);
605                         break;
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);
612                         break;
613                 case inputaction_mouse_button_down:
614                         button = calc_button(r.activity.arg1);
615                         if (button < 0)
616                                 break;
617                         input_report_key(visorinput_dev, button, 1);
618                         input_sync(visorinput_dev);
619                         break;
620                 case inputaction_mouse_button_up:
621                         button = calc_button(r.activity.arg1);
622                         if (button < 0)
623                                 break;
624                         input_report_key(visorinput_dev, button, 0);
625                         input_sync(visorinput_dev);
626                         break;
627                 case inputaction_mouse_button_click:
628                         button = calc_button(r.activity.arg1);
629                         if (button < 0)
630                                 break;
631                         input_report_key(visorinput_dev, button, 1);
632
633                         input_sync(visorinput_dev);
634                         input_report_key(visorinput_dev, button, 0);
635                         input_sync(visorinput_dev);
636                         break;
637                 case inputaction_mouse_button_dclick:
638                         button = calc_button(r.activity.arg1);
639                         if (button < 0)
640                                 break;
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);
646                         }
647                         break;
648                 case inputaction_wheel_rotate_away:
649                         input_report_rel(visorinput_dev, REL_WHEEL, 1);
650                         input_sync(visorinput_dev);
651                         break;
652                 case inputaction_wheel_rotate_toward:
653                         input_report_rel(visorinput_dev, REL_WHEEL, -1);
654                         input_sync(visorinput_dev);
655                         break;
656                 }
657         }
658 }
659
660 static int
661 visorinput_pause(struct visor_device *dev,
662                  visorbus_state_complete_func complete_func)
663 {
664         int rc;
665         struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
666
667         if (!devdata) {
668                 rc = -ENODEV;
669                 goto out;
670         }
671
672         mutex_lock(&devdata->lock_visor_dev);
673         if (devdata->paused) {
674                 rc = -EBUSY;
675                 goto out_locked;
676         }
677         if (devdata->interrupts_enabled)
678                 visorbus_disable_channel_interrupts(dev);
679
680         /*
681          * due to above, at this time no thread of execution will be
682          * in visorinput_channel_interrupt()
683          */
684
685         devdata->paused = true;
686         complete_func(dev, 0);
687         rc = 0;
688 out_locked:
689         mutex_unlock(&devdata->lock_visor_dev);
690 out:
691         return rc;
692 }
693
694 static int
695 visorinput_resume(struct visor_device *dev,
696                   visorbus_state_complete_func complete_func)
697 {
698         int rc;
699         struct visorinput_devdata *devdata = dev_get_drvdata(&dev->device);
700
701         if (!devdata) {
702                 rc = -ENODEV;
703                 goto out;
704         }
705         mutex_lock(&devdata->lock_visor_dev);
706         if (!devdata->paused) {
707                 rc = -EBUSY;
708                 goto out_locked;
709         }
710         devdata->paused = false;
711         complete_func(dev, 0);
712
713         /*
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.
717          */
718         if (devdata->interrupts_enabled)
719                 visorbus_enable_channel_interrupts(dev);
720
721         rc = 0;
722 out_locked:
723         mutex_unlock(&devdata->lock_visor_dev);
724 out:
725         return rc;
726 }
727
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 }
733 };
734
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,
744 };
745
746 static int
747 visorinput_init(void)
748 {
749         return visorbus_register_visor_driver(&visorinput_driver);
750 }
751
752 static void
753 visorinput_cleanup(void)
754 {
755         visorbus_unregister_visor_driver(&visorinput_driver);
756 }
757
758 module_init(visorinput_init);
759 module_exit(visorinput_cleanup);
760
761 MODULE_DEVICE_TABLE(visorbus, visorinput_channel_types);
762
763 MODULE_AUTHOR("Unisys");
764 MODULE_LICENSE("GPL");
765 MODULE_DESCRIPTION("s-Par human input driver for virtual keyboard/mouse");
766
767 MODULE_ALIAS("visorbus:" SPAR_MOUSE_CHANNEL_PROTOCOL_UUID_STR);
768 MODULE_ALIAS("visorbus:" SPAR_KEYBOARD_CHANNEL_PROTOCOL_UUID_STR);