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