Merge branch 'x86-asm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / hid / hid-kye.c
1 /*
2  *  HID driver for Kye/Genius devices not fully compliant with HID standard
3  *
4  *  Copyright (c) 2009 Jiri Kosina
5  *  Copyright (c) 2009 Tomas Hanak
6  *  Copyright (c) 2012 Nikolai Kondrashov
7  */
8
9 /*
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 2 of the License, or (at your option)
13  * any later version.
14  */
15
16 #include <linux/device.h>
17 #include <linux/hid.h>
18 #include <linux/module.h>
19
20 #include "hid-ids.h"
21
22 /*
23  * See EasyPen i405X description, device and HID report descriptors at
24  * http://sf.net/apps/mediawiki/digimend/?title=KYE_EasyPen_i405X
25  */
26
27 /* Original EasyPen i405X report descriptor size */
28 #define EASYPEN_I405X_RDESC_ORIG_SIZE   476
29
30 /* Fixed EasyPen i405X report descriptor */
31 static __u8 easypen_i405x_rdesc_fixed[] = {
32         0x06, 0x00, 0xFF, /*  Usage Page (FF00h),             */
33         0x09, 0x01,       /*  Usage (01h),                    */
34         0xA1, 0x01,       /*  Collection (Application),       */
35         0x85, 0x05,       /*    Report ID (5),                */
36         0x09, 0x01,       /*    Usage (01h),                  */
37         0x15, 0x80,       /*    Logical Minimum (-128),       */
38         0x25, 0x7F,       /*    Logical Maximum (127),        */
39         0x75, 0x08,       /*    Report Size (8),              */
40         0x95, 0x07,       /*    Report Count (7),             */
41         0xB1, 0x02,       /*    Feature (Variable),           */
42         0xC0,             /*  End Collection,                 */
43         0x05, 0x0D,       /*  Usage Page (Digitizer),         */
44         0x09, 0x02,       /*  Usage (Pen),                    */
45         0xA1, 0x01,       /*  Collection (Application),       */
46         0x85, 0x10,       /*    Report ID (16),               */
47         0x09, 0x20,       /*    Usage (Stylus),               */
48         0xA0,             /*    Collection (Physical),        */
49         0x14,             /*      Logical Minimum (0),        */
50         0x25, 0x01,       /*      Logical Maximum (1),        */
51         0x75, 0x01,       /*      Report Size (1),            */
52         0x09, 0x42,       /*      Usage (Tip Switch),         */
53         0x09, 0x44,       /*      Usage (Barrel Switch),      */
54         0x09, 0x46,       /*      Usage (Tablet Pick),        */
55         0x95, 0x03,       /*      Report Count (3),           */
56         0x81, 0x02,       /*      Input (Variable),           */
57         0x95, 0x04,       /*      Report Count (4),           */
58         0x81, 0x03,       /*      Input (Constant, Variable), */
59         0x09, 0x32,       /*      Usage (In Range),           */
60         0x95, 0x01,       /*      Report Count (1),           */
61         0x81, 0x02,       /*      Input (Variable),           */
62         0x75, 0x10,       /*      Report Size (16),           */
63         0x95, 0x01,       /*      Report Count (1),           */
64         0xA4,             /*      Push,                       */
65         0x05, 0x01,       /*      Usage Page (Desktop),       */
66         0x55, 0xFD,       /*      Unit Exponent (-3),         */
67         0x65, 0x13,       /*      Unit (Inch),                */
68         0x34,             /*      Physical Minimum (0),       */
69         0x09, 0x30,       /*      Usage (X),                  */
70         0x46, 0x7C, 0x15, /*      Physical Maximum (5500),    */
71         0x26, 0x00, 0x37, /*      Logical Maximum (14080),    */
72         0x81, 0x02,       /*      Input (Variable),           */
73         0x09, 0x31,       /*      Usage (Y),                  */
74         0x46, 0xA0, 0x0F, /*      Physical Maximum (4000),    */
75         0x26, 0x00, 0x28, /*      Logical Maximum (10240),    */
76         0x81, 0x02,       /*      Input (Variable),           */
77         0xB4,             /*      Pop,                        */
78         0x09, 0x30,       /*      Usage (Tip Pressure),       */
79         0x26, 0xFF, 0x03, /*      Logical Maximum (1023),     */
80         0x81, 0x02,       /*      Input (Variable),           */
81         0xC0,             /*    End Collection,               */
82         0xC0              /*  End Collection                  */
83 };
84
85 /*
86  * See MousePen i608X description, device and HID report descriptors at
87  * http://sf.net/apps/mediawiki/digimend/?title=KYE_MousePen_i608X
88  */
89
90 /* Original MousePen i608X report descriptor size */
91 #define MOUSEPEN_I608X_RDESC_ORIG_SIZE  476
92
93 /* Fixed MousePen i608X report descriptor */
94 static __u8 mousepen_i608x_rdesc_fixed[] = {
95         0x06, 0x00, 0xFF, /*  Usage Page (FF00h),             */
96         0x09, 0x01,       /*  Usage (01h),                    */
97         0xA1, 0x01,       /*  Collection (Application),       */
98         0x85, 0x05,       /*    Report ID (5),                */
99         0x09, 0x01,       /*    Usage (01h),                  */
100         0x15, 0x80,       /*    Logical Minimum (-128),       */
101         0x25, 0x7F,       /*    Logical Maximum (127),        */
102         0x75, 0x08,       /*    Report Size (8),              */
103         0x95, 0x07,       /*    Report Count (7),             */
104         0xB1, 0x02,       /*    Feature (Variable),           */
105         0xC0,             /*  End Collection,                 */
106         0x05, 0x0D,       /*  Usage Page (Digitizer),         */
107         0x09, 0x02,       /*  Usage (Pen),                    */
108         0xA1, 0x01,       /*  Collection (Application),       */
109         0x85, 0x10,       /*    Report ID (16),               */
110         0x09, 0x20,       /*    Usage (Stylus),               */
111         0xA0,             /*    Collection (Physical),        */
112         0x14,             /*      Logical Minimum (0),        */
113         0x25, 0x01,       /*      Logical Maximum (1),        */
114         0x75, 0x01,       /*      Report Size (1),            */
115         0x09, 0x42,       /*      Usage (Tip Switch),         */
116         0x09, 0x44,       /*      Usage (Barrel Switch),      */
117         0x09, 0x46,       /*      Usage (Tablet Pick),        */
118         0x95, 0x03,       /*      Report Count (3),           */
119         0x81, 0x02,       /*      Input (Variable),           */
120         0x95, 0x04,       /*      Report Count (4),           */
121         0x81, 0x03,       /*      Input (Constant, Variable), */
122         0x09, 0x32,       /*      Usage (In Range),           */
123         0x95, 0x01,       /*      Report Count (1),           */
124         0x81, 0x02,       /*      Input (Variable),           */
125         0x75, 0x10,       /*      Report Size (16),           */
126         0x95, 0x01,       /*      Report Count (1),           */
127         0xA4,             /*      Push,                       */
128         0x05, 0x01,       /*      Usage Page (Desktop),       */
129         0x55, 0xFD,       /*      Unit Exponent (-3),         */
130         0x65, 0x13,       /*      Unit (Inch),                */
131         0x34,             /*      Physical Minimum (0),       */
132         0x09, 0x30,       /*      Usage (X),                  */
133         0x46, 0x40, 0x1F, /*      Physical Maximum (8000),    */
134         0x26, 0x00, 0x50, /*      Logical Maximum (20480),    */
135         0x81, 0x02,       /*      Input (Variable),           */
136         0x09, 0x31,       /*      Usage (Y),                  */
137         0x46, 0x70, 0x17, /*      Physical Maximum (6000),    */
138         0x26, 0x00, 0x3C, /*      Logical Maximum (15360),    */
139         0x81, 0x02,       /*      Input (Variable),           */
140         0xB4,             /*      Pop,                        */
141         0x09, 0x30,       /*      Usage (Tip Pressure),       */
142         0x26, 0xFF, 0x03, /*      Logical Maximum (1023),     */
143         0x81, 0x02,       /*      Input (Variable),           */
144         0xC0,             /*    End Collection,               */
145         0xC0,             /*  End Collection,                 */
146         0x05, 0x01,       /*  Usage Page (Desktop),           */
147         0x09, 0x02,       /*  Usage (Mouse),                  */
148         0xA1, 0x01,       /*  Collection (Application),       */
149         0x85, 0x11,       /*    Report ID (17),               */
150         0x09, 0x01,       /*    Usage (Pointer),              */
151         0xA0,             /*    Collection (Physical),        */
152         0x14,             /*      Logical Minimum (0),        */
153         0xA4,             /*      Push,                       */
154         0x05, 0x09,       /*      Usage Page (Button),        */
155         0x75, 0x01,       /*      Report Size (1),            */
156         0x19, 0x01,       /*      Usage Minimum (01h),        */
157         0x29, 0x03,       /*      Usage Maximum (03h),        */
158         0x25, 0x01,       /*      Logical Maximum (1),        */
159         0x95, 0x03,       /*      Report Count (3),           */
160         0x81, 0x02,       /*      Input (Variable),           */
161         0x95, 0x05,       /*      Report Count (5),           */
162         0x81, 0x01,       /*      Input (Constant),           */
163         0xB4,             /*      Pop,                        */
164         0x95, 0x01,       /*      Report Count (1),           */
165         0xA4,             /*      Push,                       */
166         0x55, 0xFD,       /*      Unit Exponent (-3),         */
167         0x65, 0x13,       /*      Unit (Inch),                */
168         0x34,             /*      Physical Minimum (0),       */
169         0x75, 0x10,       /*      Report Size (16),           */
170         0x09, 0x30,       /*      Usage (X),                  */
171         0x46, 0x40, 0x1F, /*      Physical Maximum (8000),    */
172         0x26, 0x00, 0x50, /*      Logical Maximum (20480),    */
173         0x81, 0x02,       /*      Input (Variable),           */
174         0x09, 0x31,       /*      Usage (Y),                  */
175         0x46, 0x70, 0x17, /*      Physical Maximum (6000),    */
176         0x26, 0x00, 0x3C, /*      Logical Maximum (15360),    */
177         0x81, 0x02,       /*      Input (Variable),           */
178         0xB4,             /*      Pop,                        */
179         0x75, 0x08,       /*      Report Size (8),            */
180         0x09, 0x38,       /*      Usage (Wheel),              */
181         0x15, 0xFF,       /*      Logical Minimum (-1),       */
182         0x25, 0x01,       /*      Logical Maximum (1),        */
183         0x81, 0x06,       /*      Input (Variable, Relative), */
184         0x81, 0x01,       /*      Input (Constant),           */
185         0xC0,             /*    End Collection,               */
186         0xC0              /*  End Collection                  */
187 };
188
189 /*
190  * See EasyPen M610X description, device and HID report descriptors at
191  * http://sf.net/apps/mediawiki/digimend/?title=KYE_EasyPen_M610X
192  */
193
194 /* Original EasyPen M610X report descriptor size */
195 #define EASYPEN_M610X_RDESC_ORIG_SIZE   476
196
197 /* Fixed EasyPen M610X report descriptor */
198 static __u8 easypen_m610x_rdesc_fixed[] = {
199         0x06, 0x00, 0xFF,             /*  Usage Page (FF00h),             */
200         0x09, 0x01,                   /*  Usage (01h),                    */
201         0xA1, 0x01,                   /*  Collection (Application),       */
202         0x85, 0x05,                   /*    Report ID (5),                */
203         0x09, 0x01,                   /*    Usage (01h),                  */
204         0x15, 0x80,                   /*    Logical Minimum (-128),       */
205         0x25, 0x7F,                   /*    Logical Maximum (127),        */
206         0x75, 0x08,                   /*    Report Size (8),              */
207         0x95, 0x07,                   /*    Report Count (7),             */
208         0xB1, 0x02,                   /*    Feature (Variable),           */
209         0xC0,                         /*  End Collection,                 */
210         0x05, 0x0D,                   /*  Usage Page (Digitizer),         */
211         0x09, 0x02,                   /*  Usage (Pen),                    */
212         0xA1, 0x01,                   /*  Collection (Application),       */
213         0x85, 0x10,                   /*    Report ID (16),               */
214         0x09, 0x20,                   /*    Usage (Stylus),               */
215         0xA0,                         /*    Collection (Physical),        */
216         0x14,                         /*      Logical Minimum (0),        */
217         0x25, 0x01,                   /*      Logical Maximum (1),        */
218         0x75, 0x01,                   /*      Report Size (1),            */
219         0x09, 0x42,                   /*      Usage (Tip Switch),         */
220         0x09, 0x44,                   /*      Usage (Barrel Switch),      */
221         0x09, 0x46,                   /*      Usage (Tablet Pick),        */
222         0x95, 0x03,                   /*      Report Count (3),           */
223         0x81, 0x02,                   /*      Input (Variable),           */
224         0x95, 0x04,                   /*      Report Count (4),           */
225         0x81, 0x03,                   /*      Input (Constant, Variable), */
226         0x09, 0x32,                   /*      Usage (In Range),           */
227         0x95, 0x01,                   /*      Report Count (1),           */
228         0x81, 0x02,                   /*      Input (Variable),           */
229         0x75, 0x10,                   /*      Report Size (16),           */
230         0x95, 0x01,                   /*      Report Count (1),           */
231         0xA4,                         /*      Push,                       */
232         0x05, 0x01,                   /*      Usage Page (Desktop),       */
233         0x55, 0xFD,                   /*      Unit Exponent (-3),         */
234         0x65, 0x13,                   /*      Unit (Inch),                */
235         0x34,                         /*      Physical Minimum (0),       */
236         0x09, 0x30,                   /*      Usage (X),                  */
237         0x46, 0x10, 0x27,             /*      Physical Maximum (10000),   */
238         0x27, 0x00, 0xA0, 0x00, 0x00, /*      Logical Maximum (40960),    */
239         0x81, 0x02,                   /*      Input (Variable),           */
240         0x09, 0x31,                   /*      Usage (Y),                  */
241         0x46, 0x6A, 0x18,             /*      Physical Maximum (6250),    */
242         0x26, 0x00, 0x64,             /*      Logical Maximum (25600),    */
243         0x81, 0x02,                   /*      Input (Variable),           */
244         0xB4,                         /*      Pop,                        */
245         0x09, 0x30,                   /*      Usage (Tip Pressure),       */
246         0x26, 0xFF, 0x03,             /*      Logical Maximum (1023),     */
247         0x81, 0x02,                   /*      Input (Variable),           */
248         0xC0,                         /*    End Collection,               */
249         0xC0,                         /*  End Collection,                 */
250         0x05, 0x0C,                   /*  Usage Page (Consumer),          */
251         0x09, 0x01,                   /*  Usage (Consumer Control),       */
252         0xA1, 0x01,                   /*  Collection (Application),       */
253         0x85, 0x12,                   /*    Report ID (18),               */
254         0x14,                         /*    Logical Minimum (0),          */
255         0x25, 0x01,                   /*    Logical Maximum (1),          */
256         0x75, 0x01,                   /*    Report Size (1),              */
257         0x95, 0x04,                   /*    Report Count (4),             */
258         0x0A, 0x1A, 0x02,             /*    Usage (AC Undo),              */
259         0x0A, 0x79, 0x02,             /*    Usage (AC Redo Or Repeat),    */
260         0x0A, 0x2D, 0x02,             /*    Usage (AC Zoom In),           */
261         0x0A, 0x2E, 0x02,             /*    Usage (AC Zoom Out),          */
262         0x81, 0x02,                   /*    Input (Variable),             */
263         0x95, 0x01,                   /*    Report Count (1),             */
264         0x75, 0x14,                   /*    Report Size (20),             */
265         0x81, 0x03,                   /*    Input (Constant, Variable),   */
266         0x75, 0x20,                   /*    Report Size (32),             */
267         0x81, 0x03,                   /*    Input (Constant, Variable),   */
268         0xC0                          /*  End Collection                  */
269 };
270
271 static __u8 *kye_consumer_control_fixup(struct hid_device *hdev, __u8 *rdesc,
272                 unsigned int *rsize, int offset, const char *device_name) {
273         /*
274          * the fixup that need to be done:
275          *   - change Usage Maximum in the Comsumer Control
276          *     (report ID 3) to a reasonable value
277          */
278         if (*rsize >= offset + 31 &&
279             /* Usage Page (Consumer Devices) */
280             rdesc[offset] == 0x05 && rdesc[offset + 1] == 0x0c &&
281             /* Usage (Consumer Control) */
282             rdesc[offset + 2] == 0x09 && rdesc[offset + 3] == 0x01 &&
283             /*   Usage Maximum > 12287 */
284             rdesc[offset + 10] == 0x2a && rdesc[offset + 12] > 0x2f) {
285                 hid_info(hdev, "fixing up %s report descriptor\n", device_name);
286                 rdesc[offset + 12] = 0x2f;
287         }
288         return rdesc;
289 }
290
291 static __u8 *kye_report_fixup(struct hid_device *hdev, __u8 *rdesc,
292                 unsigned int *rsize)
293 {
294         switch (hdev->product) {
295         case USB_DEVICE_ID_KYE_ERGO_525V:
296                 /* the fixups that need to be done:
297                  *   - change led usage page to button for extra buttons
298                  *   - report size 8 count 1 must be size 1 count 8 for button
299                  *     bitfield
300                  *   - change the button usage range to 4-7 for the extra
301                  *     buttons
302                  */
303                 if (*rsize >= 74 &&
304                         rdesc[61] == 0x05 && rdesc[62] == 0x08 &&
305                         rdesc[63] == 0x19 && rdesc[64] == 0x08 &&
306                         rdesc[65] == 0x29 && rdesc[66] == 0x0f &&
307                         rdesc[71] == 0x75 && rdesc[72] == 0x08 &&
308                         rdesc[73] == 0x95 && rdesc[74] == 0x01) {
309                         hid_info(hdev,
310                                  "fixing up Kye/Genius Ergo Mouse "
311                                  "report descriptor\n");
312                         rdesc[62] = 0x09;
313                         rdesc[64] = 0x04;
314                         rdesc[66] = 0x07;
315                         rdesc[72] = 0x01;
316                         rdesc[74] = 0x08;
317                 }
318                 break;
319         case USB_DEVICE_ID_KYE_EASYPEN_I405X:
320                 if (*rsize == EASYPEN_I405X_RDESC_ORIG_SIZE) {
321                         rdesc = easypen_i405x_rdesc_fixed;
322                         *rsize = sizeof(easypen_i405x_rdesc_fixed);
323                 }
324                 break;
325         case USB_DEVICE_ID_KYE_MOUSEPEN_I608X:
326                 if (*rsize == MOUSEPEN_I608X_RDESC_ORIG_SIZE) {
327                         rdesc = mousepen_i608x_rdesc_fixed;
328                         *rsize = sizeof(mousepen_i608x_rdesc_fixed);
329                 }
330                 break;
331         case USB_DEVICE_ID_KYE_EASYPEN_M610X:
332                 if (*rsize == EASYPEN_M610X_RDESC_ORIG_SIZE) {
333                         rdesc = easypen_m610x_rdesc_fixed;
334                         *rsize = sizeof(easypen_m610x_rdesc_fixed);
335                 }
336                 break;
337         case USB_DEVICE_ID_GENIUS_GILA_GAMING_MOUSE:
338                 rdesc = kye_consumer_control_fixup(hdev, rdesc, rsize, 104,
339                                         "Genius Gila Gaming Mouse");
340                 break;
341         case USB_DEVICE_ID_GENIUS_GX_IMPERATOR:
342                 rdesc = kye_consumer_control_fixup(hdev, rdesc, rsize, 83,
343                                         "Genius Gx Imperator Keyboard");
344                 break;
345         case USB_DEVICE_ID_GENIUS_MANTICORE:
346                 rdesc = kye_consumer_control_fixup(hdev, rdesc, rsize, 104,
347                                         "Genius Manticore Keyboard");
348                 break;
349         }
350         return rdesc;
351 }
352
353 /**
354  * Enable fully-functional tablet mode by setting a special feature report.
355  *
356  * @hdev:       HID device
357  *
358  * The specific report ID and data were discovered by sniffing the
359  * Windows driver traffic.
360  */
361 static int kye_tablet_enable(struct hid_device *hdev)
362 {
363         struct list_head *list;
364         struct list_head *head;
365         struct hid_report *report;
366         __s32 *value;
367
368         list = &hdev->report_enum[HID_FEATURE_REPORT].report_list;
369         list_for_each(head, list) {
370                 report = list_entry(head, struct hid_report, list);
371                 if (report->id == 5)
372                         break;
373         }
374
375         if (head == list) {
376                 hid_err(hdev, "tablet-enabling feature report not found\n");
377                 return -ENODEV;
378         }
379
380         if (report->maxfield < 1 || report->field[0]->report_count < 7) {
381                 hid_err(hdev, "invalid tablet-enabling feature report\n");
382                 return -ENODEV;
383         }
384
385         value = report->field[0]->value;
386
387         value[0] = 0x12;
388         value[1] = 0x10;
389         value[2] = 0x11;
390         value[3] = 0x12;
391         value[4] = 0x00;
392         value[5] = 0x00;
393         value[6] = 0x00;
394         hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
395
396         return 0;
397 }
398
399 static int kye_probe(struct hid_device *hdev, const struct hid_device_id *id)
400 {
401         int ret;
402
403         ret = hid_parse(hdev);
404         if (ret) {
405                 hid_err(hdev, "parse failed\n");
406                 goto err;
407         }
408
409         ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
410         if (ret) {
411                 hid_err(hdev, "hw start failed\n");
412                 goto err;
413         }
414
415         switch (id->product) {
416         case USB_DEVICE_ID_KYE_EASYPEN_I405X:
417         case USB_DEVICE_ID_KYE_MOUSEPEN_I608X:
418         case USB_DEVICE_ID_KYE_EASYPEN_M610X:
419                 ret = kye_tablet_enable(hdev);
420                 if (ret) {
421                         hid_err(hdev, "tablet enabling failed\n");
422                         goto enabling_err;
423                 }
424                 break;
425         case USB_DEVICE_ID_GENIUS_MANTICORE:
426                 /*
427                  * The manticore keyboard needs to have all the interfaces
428                  * opened at least once to be fully functional.
429                  */
430                 if (hid_hw_open(hdev))
431                         hid_hw_close(hdev);
432                 break;
433         }
434
435         return 0;
436 enabling_err:
437         hid_hw_stop(hdev);
438 err:
439         return ret;
440 }
441
442 static const struct hid_device_id kye_devices[] = {
443         { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) },
444         { HID_USB_DEVICE(USB_VENDOR_ID_KYE,
445                                 USB_DEVICE_ID_KYE_EASYPEN_I405X) },
446         { HID_USB_DEVICE(USB_VENDOR_ID_KYE,
447                                 USB_DEVICE_ID_KYE_MOUSEPEN_I608X) },
448         { HID_USB_DEVICE(USB_VENDOR_ID_KYE,
449                                 USB_DEVICE_ID_KYE_EASYPEN_M610X) },
450         { HID_USB_DEVICE(USB_VENDOR_ID_KYE,
451                                 USB_DEVICE_ID_GENIUS_GILA_GAMING_MOUSE) },
452         { HID_USB_DEVICE(USB_VENDOR_ID_KYE,
453                                 USB_DEVICE_ID_GENIUS_GX_IMPERATOR) },
454         { HID_USB_DEVICE(USB_VENDOR_ID_KYE,
455                                 USB_DEVICE_ID_GENIUS_MANTICORE) },
456         { }
457 };
458 MODULE_DEVICE_TABLE(hid, kye_devices);
459
460 static struct hid_driver kye_driver = {
461         .name = "kye",
462         .id_table = kye_devices,
463         .probe = kye_probe,
464         .report_fixup = kye_report_fixup,
465 };
466 module_hid_driver(kye_driver);
467
468 MODULE_LICENSE("GPL");