]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/hid/hid-logitech-hidpp.c
HID: hid-logitech-hidpp: Add basic support for Logitech G920
[mirror_ubuntu-artful-kernel.git] / drivers / hid / hid-logitech-hidpp.c
1 /*
2 * HIDPP protocol for Logitech Unifying receivers
3 *
4 * Copyright (c) 2011 Logitech (c)
5 * Copyright (c) 2012-2013 Google (c)
6 * Copyright (c) 2013-2014 Red Hat Inc.
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; version 2 of the License.
13 */
14
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17 #include <linux/device.h>
18 #include <linux/hid.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/sched.h>
22 #include <linux/kfifo.h>
23 #include <linux/input/mt.h>
24 #include <asm/unaligned.h>
25 #include "hid-ids.h"
26
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
29 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
30
31 static bool disable_raw_mode;
32 module_param(disable_raw_mode, bool, 0644);
33 MODULE_PARM_DESC(disable_raw_mode,
34 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
35
36 static bool disable_tap_to_click;
37 module_param(disable_tap_to_click, bool, 0644);
38 MODULE_PARM_DESC(disable_tap_to_click,
39 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
40
41 #define REPORT_ID_HIDPP_SHORT 0x10
42 #define REPORT_ID_HIDPP_LONG 0x11
43 #define REPORT_ID_HIDPP_VERY_LONG 0x12
44
45 #define HIDPP_REPORT_SHORT_LENGTH 7
46 #define HIDPP_REPORT_LONG_LENGTH 20
47 #define HIDPP_REPORT_VERY_LONG_LENGTH 64
48
49 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
50 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
51 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
52 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
53
54 /* bits 2..20 are reserved for classes */
55 #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21)
56 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
57 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
58 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
59
60 #define HIDPP_QUIRK_DELAYED_INIT (HIDPP_QUIRK_NO_HIDINPUT | \
61 HIDPP_QUIRK_CONNECT_EVENTS)
62
63 /*
64 * There are two hidpp protocols in use, the first version hidpp10 is known
65 * as register access protocol or RAP, the second version hidpp20 is known as
66 * feature access protocol or FAP
67 *
68 * Most older devices (including the Unifying usb receiver) use the RAP protocol
69 * where as most newer devices use the FAP protocol. Both protocols are
70 * compatible with the underlying transport, which could be usb, Unifiying, or
71 * bluetooth. The message lengths are defined by the hid vendor specific report
72 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
73 * the HIDPP_LONG report type (total message length 20 bytes)
74 *
75 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
76 * messages. The Unifying receiver itself responds to RAP messages (device index
77 * is 0xFF for the receiver), and all messages (short or long) with a device
78 * index between 1 and 6 are passed untouched to the corresponding paired
79 * Unifying device.
80 *
81 * The paired device can be RAP or FAP, it will receive the message untouched
82 * from the Unifiying receiver.
83 */
84
85 struct fap {
86 u8 feature_index;
87 u8 funcindex_clientid;
88 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
89 };
90
91 struct rap {
92 u8 sub_id;
93 u8 reg_address;
94 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
95 };
96
97 struct hidpp_report {
98 u8 report_id;
99 u8 device_index;
100 union {
101 struct fap fap;
102 struct rap rap;
103 u8 rawbytes[sizeof(struct fap)];
104 };
105 } __packed;
106
107 struct hidpp_device {
108 struct hid_device *hid_dev;
109 struct mutex send_mutex;
110 void *send_receive_buf;
111 char *name; /* will never be NULL and should not be freed */
112 wait_queue_head_t wait;
113 bool answer_available;
114 u8 protocol_major;
115 u8 protocol_minor;
116
117 void *private_data;
118
119 struct work_struct work;
120 struct kfifo delayed_work_fifo;
121 atomic_t connected;
122 struct input_dev *delayed_input;
123
124 unsigned long quirks;
125 };
126
127
128 /* HID++ 1.0 error codes */
129 #define HIDPP_ERROR 0x8f
130 #define HIDPP_ERROR_SUCCESS 0x00
131 #define HIDPP_ERROR_INVALID_SUBID 0x01
132 #define HIDPP_ERROR_INVALID_ADRESS 0x02
133 #define HIDPP_ERROR_INVALID_VALUE 0x03
134 #define HIDPP_ERROR_CONNECT_FAIL 0x04
135 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
136 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
137 #define HIDPP_ERROR_BUSY 0x07
138 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
139 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
140 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
141 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
142 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
143 /* HID++ 2.0 error codes */
144 #define HIDPP20_ERROR 0xff
145
146 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
147
148 static int __hidpp_send_report(struct hid_device *hdev,
149 struct hidpp_report *hidpp_report)
150 {
151 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
152 int fields_count, ret;
153
154 hidpp = hid_get_drvdata(hdev);
155
156 switch (hidpp_report->report_id) {
157 case REPORT_ID_HIDPP_SHORT:
158 fields_count = HIDPP_REPORT_SHORT_LENGTH;
159 break;
160 case REPORT_ID_HIDPP_LONG:
161 fields_count = HIDPP_REPORT_LONG_LENGTH;
162 break;
163 case REPORT_ID_HIDPP_VERY_LONG:
164 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
165 break;
166 default:
167 return -ENODEV;
168 }
169
170 /*
171 * set the device_index as the receiver, it will be overwritten by
172 * hid_hw_request if needed
173 */
174 hidpp_report->device_index = 0xff;
175
176 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
177 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
178 } else {
179 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
180 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
181 HID_REQ_SET_REPORT);
182 }
183
184 return ret == fields_count ? 0 : -1;
185 }
186
187 /**
188 * hidpp_send_message_sync() returns 0 in case of success, and something else
189 * in case of a failure.
190 * - If ' something else' is positive, that means that an error has been raised
191 * by the protocol itself.
192 * - If ' something else' is negative, that means that we had a classic error
193 * (-ENOMEM, -EPIPE, etc...)
194 */
195 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
196 struct hidpp_report *message,
197 struct hidpp_report *response)
198 {
199 int ret;
200
201 mutex_lock(&hidpp->send_mutex);
202
203 hidpp->send_receive_buf = response;
204 hidpp->answer_available = false;
205
206 /*
207 * So that we can later validate the answer when it arrives
208 * in hidpp_raw_event
209 */
210 *response = *message;
211
212 ret = __hidpp_send_report(hidpp->hid_dev, message);
213
214 if (ret) {
215 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
216 memset(response, 0, sizeof(struct hidpp_report));
217 goto exit;
218 }
219
220 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
221 5*HZ)) {
222 dbg_hid("%s:timeout waiting for response\n", __func__);
223 memset(response, 0, sizeof(struct hidpp_report));
224 ret = -ETIMEDOUT;
225 }
226
227 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
228 response->rap.sub_id == HIDPP_ERROR) {
229 ret = response->rap.params[1];
230 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
231 goto exit;
232 }
233
234 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
235 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
236 response->fap.feature_index == HIDPP20_ERROR) {
237 ret = response->fap.params[1];
238 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
239 goto exit;
240 }
241
242 exit:
243 mutex_unlock(&hidpp->send_mutex);
244 return ret;
245
246 }
247
248 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
249 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
250 struct hidpp_report *response)
251 {
252 struct hidpp_report *message;
253 int ret;
254
255 if (param_count > sizeof(message->fap.params))
256 return -EINVAL;
257
258 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
259 if (!message)
260 return -ENOMEM;
261
262 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
263 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
264 else
265 message->report_id = REPORT_ID_HIDPP_LONG;
266 message->fap.feature_index = feat_index;
267 message->fap.funcindex_clientid = funcindex_clientid;
268 memcpy(&message->fap.params, params, param_count);
269
270 ret = hidpp_send_message_sync(hidpp, message, response);
271 kfree(message);
272 return ret;
273 }
274
275 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
276 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
277 struct hidpp_report *response)
278 {
279 struct hidpp_report *message;
280 int ret, max_count;
281
282 switch (report_id) {
283 case REPORT_ID_HIDPP_SHORT:
284 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
285 break;
286 case REPORT_ID_HIDPP_LONG:
287 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
288 break;
289 case REPORT_ID_HIDPP_VERY_LONG:
290 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
291 break;
292 default:
293 return -EINVAL;
294 }
295
296 if (param_count > max_count)
297 return -EINVAL;
298
299 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
300 if (!message)
301 return -ENOMEM;
302 message->report_id = report_id;
303 message->rap.sub_id = sub_id;
304 message->rap.reg_address = reg_address;
305 memcpy(&message->rap.params, params, param_count);
306
307 ret = hidpp_send_message_sync(hidpp_dev, message, response);
308 kfree(message);
309 return ret;
310 }
311
312 static void delayed_work_cb(struct work_struct *work)
313 {
314 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
315 work);
316 hidpp_connect_event(hidpp);
317 }
318
319 static inline bool hidpp_match_answer(struct hidpp_report *question,
320 struct hidpp_report *answer)
321 {
322 return (answer->fap.feature_index == question->fap.feature_index) &&
323 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
324 }
325
326 static inline bool hidpp_match_error(struct hidpp_report *question,
327 struct hidpp_report *answer)
328 {
329 return ((answer->rap.sub_id == HIDPP_ERROR) ||
330 (answer->fap.feature_index == HIDPP20_ERROR)) &&
331 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
332 (answer->fap.params[0] == question->fap.funcindex_clientid);
333 }
334
335 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
336 {
337 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
338 (report->rap.sub_id == 0x41);
339 }
340
341 /**
342 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
343 */
344 static void hidpp_prefix_name(char **name, int name_length)
345 {
346 #define PREFIX_LENGTH 9 /* "Logitech " */
347
348 int new_length;
349 char *new_name;
350
351 if (name_length > PREFIX_LENGTH &&
352 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
353 /* The prefix has is already in the name */
354 return;
355
356 new_length = PREFIX_LENGTH + name_length;
357 new_name = kzalloc(new_length, GFP_KERNEL);
358 if (!new_name)
359 return;
360
361 snprintf(new_name, new_length, "Logitech %s", *name);
362
363 kfree(*name);
364
365 *name = new_name;
366 }
367
368 /* -------------------------------------------------------------------------- */
369 /* HIDP++ 1.0 commands */
370 /* -------------------------------------------------------------------------- */
371
372 #define HIDPP_SET_REGISTER 0x80
373 #define HIDPP_GET_REGISTER 0x81
374 #define HIDPP_SET_LONG_REGISTER 0x82
375 #define HIDPP_GET_LONG_REGISTER 0x83
376
377 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
378 #define DEVICE_NAME 0x40
379
380 static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev)
381 {
382 struct hidpp_report response;
383 int ret;
384 /* hid-logitech-dj is in charge of setting the right device index */
385 u8 params[1] = { DEVICE_NAME };
386 char *name;
387 int len;
388
389 ret = hidpp_send_rap_command_sync(hidpp_dev,
390 REPORT_ID_HIDPP_SHORT,
391 HIDPP_GET_LONG_REGISTER,
392 HIDPP_REG_PAIRING_INFORMATION,
393 params, 1, &response);
394 if (ret)
395 return NULL;
396
397 len = response.rap.params[1];
398
399 if (2 + len > sizeof(response.rap.params))
400 return NULL;
401
402 name = kzalloc(len + 1, GFP_KERNEL);
403 if (!name)
404 return NULL;
405
406 memcpy(name, &response.rap.params[2], len);
407
408 /* include the terminating '\0' */
409 hidpp_prefix_name(&name, len + 1);
410
411 return name;
412 }
413
414 /* -------------------------------------------------------------------------- */
415 /* 0x0000: Root */
416 /* -------------------------------------------------------------------------- */
417
418 #define HIDPP_PAGE_ROOT 0x0000
419 #define HIDPP_PAGE_ROOT_IDX 0x00
420
421 #define CMD_ROOT_GET_FEATURE 0x01
422 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
423
424 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
425 u8 *feature_index, u8 *feature_type)
426 {
427 struct hidpp_report response;
428 int ret;
429 u8 params[2] = { feature >> 8, feature & 0x00FF };
430
431 ret = hidpp_send_fap_command_sync(hidpp,
432 HIDPP_PAGE_ROOT_IDX,
433 CMD_ROOT_GET_FEATURE,
434 params, 2, &response);
435 if (ret)
436 return ret;
437
438 *feature_index = response.fap.params[0];
439 *feature_type = response.fap.params[1];
440
441 return ret;
442 }
443
444 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
445 {
446 struct hidpp_report response;
447 int ret;
448
449 ret = hidpp_send_fap_command_sync(hidpp,
450 HIDPP_PAGE_ROOT_IDX,
451 CMD_ROOT_GET_PROTOCOL_VERSION,
452 NULL, 0, &response);
453
454 if (ret == HIDPP_ERROR_INVALID_SUBID) {
455 hidpp->protocol_major = 1;
456 hidpp->protocol_minor = 0;
457 return 0;
458 }
459
460 /* the device might not be connected */
461 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
462 return -EIO;
463
464 if (ret > 0) {
465 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
466 __func__, ret);
467 return -EPROTO;
468 }
469 if (ret)
470 return ret;
471
472 hidpp->protocol_major = response.fap.params[0];
473 hidpp->protocol_minor = response.fap.params[1];
474
475 return ret;
476 }
477
478 static bool hidpp_is_connected(struct hidpp_device *hidpp)
479 {
480 int ret;
481
482 ret = hidpp_root_get_protocol_version(hidpp);
483 if (!ret)
484 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
485 hidpp->protocol_major, hidpp->protocol_minor);
486 return ret == 0;
487 }
488
489 /* -------------------------------------------------------------------------- */
490 /* 0x0005: GetDeviceNameType */
491 /* -------------------------------------------------------------------------- */
492
493 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
494
495 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
496 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
497 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
498
499 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
500 u8 feature_index, u8 *nameLength)
501 {
502 struct hidpp_report response;
503 int ret;
504
505 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
506 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
507
508 if (ret > 0) {
509 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
510 __func__, ret);
511 return -EPROTO;
512 }
513 if (ret)
514 return ret;
515
516 *nameLength = response.fap.params[0];
517
518 return ret;
519 }
520
521 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
522 u8 feature_index, u8 char_index, char *device_name, int len_buf)
523 {
524 struct hidpp_report response;
525 int ret, i;
526 int count;
527
528 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
529 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
530 &response);
531
532 if (ret > 0) {
533 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
534 __func__, ret);
535 return -EPROTO;
536 }
537 if (ret)
538 return ret;
539
540 switch (response.report_id) {
541 case REPORT_ID_HIDPP_VERY_LONG:
542 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
543 break;
544 case REPORT_ID_HIDPP_LONG:
545 count = HIDPP_REPORT_LONG_LENGTH - 4;
546 break;
547 case REPORT_ID_HIDPP_SHORT:
548 count = HIDPP_REPORT_SHORT_LENGTH - 4;
549 break;
550 default:
551 return -EPROTO;
552 }
553
554 if (len_buf < count)
555 count = len_buf;
556
557 for (i = 0; i < count; i++)
558 device_name[i] = response.fap.params[i];
559
560 return count;
561 }
562
563 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
564 {
565 u8 feature_type;
566 u8 feature_index;
567 u8 __name_length;
568 char *name;
569 unsigned index = 0;
570 int ret;
571
572 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
573 &feature_index, &feature_type);
574 if (ret)
575 return NULL;
576
577 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
578 &__name_length);
579 if (ret)
580 return NULL;
581
582 name = kzalloc(__name_length + 1, GFP_KERNEL);
583 if (!name)
584 return NULL;
585
586 while (index < __name_length) {
587 ret = hidpp_devicenametype_get_device_name(hidpp,
588 feature_index, index, name + index,
589 __name_length - index);
590 if (ret <= 0) {
591 kfree(name);
592 return NULL;
593 }
594 index += ret;
595 }
596
597 /* include the terminating '\0' */
598 hidpp_prefix_name(&name, __name_length + 1);
599
600 return name;
601 }
602
603 /* -------------------------------------------------------------------------- */
604 /* 0x6010: Touchpad FW items */
605 /* -------------------------------------------------------------------------- */
606
607 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
608
609 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
610
611 struct hidpp_touchpad_fw_items {
612 uint8_t presence;
613 uint8_t desired_state;
614 uint8_t state;
615 uint8_t persistent;
616 };
617
618 /**
619 * send a set state command to the device by reading the current items->state
620 * field. items is then filled with the current state.
621 */
622 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
623 u8 feature_index,
624 struct hidpp_touchpad_fw_items *items)
625 {
626 struct hidpp_report response;
627 int ret;
628 u8 *params = (u8 *)response.fap.params;
629
630 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
631 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
632
633 if (ret > 0) {
634 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
635 __func__, ret);
636 return -EPROTO;
637 }
638 if (ret)
639 return ret;
640
641 items->presence = params[0];
642 items->desired_state = params[1];
643 items->state = params[2];
644 items->persistent = params[3];
645
646 return 0;
647 }
648
649 /* -------------------------------------------------------------------------- */
650 /* 0x6100: TouchPadRawXY */
651 /* -------------------------------------------------------------------------- */
652
653 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
654
655 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
656 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
657
658 #define EVENT_TOUCHPAD_RAW_XY 0x00
659
660 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
661 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
662
663 struct hidpp_touchpad_raw_info {
664 u16 x_size;
665 u16 y_size;
666 u8 z_range;
667 u8 area_range;
668 u8 timestamp_unit;
669 u8 maxcontacts;
670 u8 origin;
671 u16 res;
672 };
673
674 struct hidpp_touchpad_raw_xy_finger {
675 u8 contact_type;
676 u8 contact_status;
677 u16 x;
678 u16 y;
679 u8 z;
680 u8 area;
681 u8 finger_id;
682 };
683
684 struct hidpp_touchpad_raw_xy {
685 u16 timestamp;
686 struct hidpp_touchpad_raw_xy_finger fingers[2];
687 u8 spurious_flag;
688 u8 end_of_frame;
689 u8 finger_count;
690 u8 button;
691 };
692
693 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
694 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
695 {
696 struct hidpp_report response;
697 int ret;
698 u8 *params = (u8 *)response.fap.params;
699
700 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
701 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
702
703 if (ret > 0) {
704 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
705 __func__, ret);
706 return -EPROTO;
707 }
708 if (ret)
709 return ret;
710
711 raw_info->x_size = get_unaligned_be16(&params[0]);
712 raw_info->y_size = get_unaligned_be16(&params[2]);
713 raw_info->z_range = params[4];
714 raw_info->area_range = params[5];
715 raw_info->maxcontacts = params[7];
716 raw_info->origin = params[8];
717 /* res is given in unit per inch */
718 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
719
720 return ret;
721 }
722
723 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
724 u8 feature_index, bool send_raw_reports,
725 bool sensor_enhanced_settings)
726 {
727 struct hidpp_report response;
728
729 /*
730 * Params:
731 * bit 0 - enable raw
732 * bit 1 - 16bit Z, no area
733 * bit 2 - enhanced sensitivity
734 * bit 3 - width, height (4 bits each) instead of area
735 * bit 4 - send raw + gestures (degrades smoothness)
736 * remaining bits - reserved
737 */
738 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
739
740 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
741 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
742 }
743
744 static void hidpp_touchpad_touch_event(u8 *data,
745 struct hidpp_touchpad_raw_xy_finger *finger)
746 {
747 u8 x_m = data[0] << 2;
748 u8 y_m = data[2] << 2;
749
750 finger->x = x_m << 6 | data[1];
751 finger->y = y_m << 6 | data[3];
752
753 finger->contact_type = data[0] >> 6;
754 finger->contact_status = data[2] >> 6;
755
756 finger->z = data[4];
757 finger->area = data[5];
758 finger->finger_id = data[6] >> 4;
759 }
760
761 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
762 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
763 {
764 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
765 raw_xy->end_of_frame = data[8] & 0x01;
766 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
767 raw_xy->finger_count = data[15] & 0x0f;
768 raw_xy->button = (data[8] >> 2) & 0x01;
769
770 if (raw_xy->finger_count) {
771 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
772 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
773 }
774 }
775
776 /* ************************************************************************** */
777 /* */
778 /* Device Support */
779 /* */
780 /* ************************************************************************** */
781
782 /* -------------------------------------------------------------------------- */
783 /* Touchpad HID++ devices */
784 /* -------------------------------------------------------------------------- */
785
786 #define WTP_MANUAL_RESOLUTION 39
787
788 struct wtp_data {
789 struct input_dev *input;
790 u16 x_size, y_size;
791 u8 finger_count;
792 u8 mt_feature_index;
793 u8 button_feature_index;
794 u8 maxcontacts;
795 bool flip_y;
796 unsigned int resolution;
797 };
798
799 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
800 struct hid_field *field, struct hid_usage *usage,
801 unsigned long **bit, int *max)
802 {
803 return -1;
804 }
805
806 static void wtp_populate_input(struct hidpp_device *hidpp,
807 struct input_dev *input_dev, bool origin_is_hid_core)
808 {
809 struct wtp_data *wd = hidpp->private_data;
810
811 __set_bit(EV_ABS, input_dev->evbit);
812 __set_bit(EV_KEY, input_dev->evbit);
813 __clear_bit(EV_REL, input_dev->evbit);
814 __clear_bit(EV_LED, input_dev->evbit);
815
816 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
817 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
818 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
819 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
820
821 /* Max pressure is not given by the devices, pick one */
822 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
823
824 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
825
826 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
827 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
828 else
829 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
830
831 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
832 INPUT_MT_DROP_UNUSED);
833
834 wd->input = input_dev;
835 }
836
837 static void wtp_touch_event(struct wtp_data *wd,
838 struct hidpp_touchpad_raw_xy_finger *touch_report)
839 {
840 int slot;
841
842 if (!touch_report->finger_id || touch_report->contact_type)
843 /* no actual data */
844 return;
845
846 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
847
848 input_mt_slot(wd->input, slot);
849 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
850 touch_report->contact_status);
851 if (touch_report->contact_status) {
852 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
853 touch_report->x);
854 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
855 wd->flip_y ? wd->y_size - touch_report->y :
856 touch_report->y);
857 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
858 touch_report->area);
859 }
860 }
861
862 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
863 struct hidpp_touchpad_raw_xy *raw)
864 {
865 struct wtp_data *wd = hidpp->private_data;
866 int i;
867
868 for (i = 0; i < 2; i++)
869 wtp_touch_event(wd, &(raw->fingers[i]));
870
871 if (raw->end_of_frame &&
872 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
873 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
874
875 if (raw->end_of_frame || raw->finger_count <= 2) {
876 input_mt_sync_frame(wd->input);
877 input_sync(wd->input);
878 }
879 }
880
881 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
882 {
883 struct wtp_data *wd = hidpp->private_data;
884 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
885 (data[7] >> 4) * (data[7] >> 4)) / 2;
886 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
887 (data[13] >> 4) * (data[13] >> 4)) / 2;
888 struct hidpp_touchpad_raw_xy raw = {
889 .timestamp = data[1],
890 .fingers = {
891 {
892 .contact_type = 0,
893 .contact_status = !!data[7],
894 .x = get_unaligned_le16(&data[3]),
895 .y = get_unaligned_le16(&data[5]),
896 .z = c1_area,
897 .area = c1_area,
898 .finger_id = data[2],
899 }, {
900 .contact_type = 0,
901 .contact_status = !!data[13],
902 .x = get_unaligned_le16(&data[9]),
903 .y = get_unaligned_le16(&data[11]),
904 .z = c2_area,
905 .area = c2_area,
906 .finger_id = data[8],
907 }
908 },
909 .finger_count = wd->maxcontacts,
910 .spurious_flag = 0,
911 .end_of_frame = (data[0] >> 7) == 0,
912 .button = data[0] & 0x01,
913 };
914
915 wtp_send_raw_xy_event(hidpp, &raw);
916
917 return 1;
918 }
919
920 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
921 {
922 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
923 struct wtp_data *wd = hidpp->private_data;
924 struct hidpp_report *report = (struct hidpp_report *)data;
925 struct hidpp_touchpad_raw_xy raw;
926
927 if (!wd || !wd->input)
928 return 1;
929
930 switch (data[0]) {
931 case 0x02:
932 if (size < 2) {
933 hid_err(hdev, "Received HID report of bad size (%d)",
934 size);
935 return 1;
936 }
937 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
938 input_event(wd->input, EV_KEY, BTN_LEFT,
939 !!(data[1] & 0x01));
940 input_event(wd->input, EV_KEY, BTN_RIGHT,
941 !!(data[1] & 0x02));
942 input_sync(wd->input);
943 return 0;
944 } else {
945 if (size < 21)
946 return 1;
947 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
948 }
949 case REPORT_ID_HIDPP_LONG:
950 /* size is already checked in hidpp_raw_event. */
951 if ((report->fap.feature_index != wd->mt_feature_index) ||
952 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
953 return 1;
954 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
955
956 wtp_send_raw_xy_event(hidpp, &raw);
957 return 0;
958 }
959
960 return 0;
961 }
962
963 static int wtp_get_config(struct hidpp_device *hidpp)
964 {
965 struct wtp_data *wd = hidpp->private_data;
966 struct hidpp_touchpad_raw_info raw_info = {0};
967 u8 feature_type;
968 int ret;
969
970 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
971 &wd->mt_feature_index, &feature_type);
972 if (ret)
973 /* means that the device is not powered up */
974 return ret;
975
976 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
977 &raw_info);
978 if (ret)
979 return ret;
980
981 wd->x_size = raw_info.x_size;
982 wd->y_size = raw_info.y_size;
983 wd->maxcontacts = raw_info.maxcontacts;
984 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
985 wd->resolution = raw_info.res;
986 if (!wd->resolution)
987 wd->resolution = WTP_MANUAL_RESOLUTION;
988
989 return 0;
990 }
991
992 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
993 {
994 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
995 struct wtp_data *wd;
996
997 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
998 GFP_KERNEL);
999 if (!wd)
1000 return -ENOMEM;
1001
1002 hidpp->private_data = wd;
1003
1004 return 0;
1005 };
1006
1007 static int wtp_connect(struct hid_device *hdev, bool connected)
1008 {
1009 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1010 struct wtp_data *wd = hidpp->private_data;
1011 int ret;
1012
1013 if (!connected)
1014 return 0;
1015
1016 if (!wd->x_size) {
1017 ret = wtp_get_config(hidpp);
1018 if (ret) {
1019 hid_err(hdev, "Can not get wtp config: %d\n", ret);
1020 return ret;
1021 }
1022 }
1023
1024 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
1025 true, true);
1026 }
1027
1028 /* ------------------------------------------------------------------------- */
1029 /* Logitech M560 devices */
1030 /* ------------------------------------------------------------------------- */
1031
1032 /*
1033 * Logitech M560 protocol overview
1034 *
1035 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
1036 * the sides buttons are pressed, it sends some keyboard keys events
1037 * instead of buttons ones.
1038 * To complicate things further, the middle button keys sequence
1039 * is different from the odd press and the even press.
1040 *
1041 * forward button -> Super_R
1042 * backward button -> Super_L+'d' (press only)
1043 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
1044 * 2nd time: left-click (press only)
1045 * NB: press-only means that when the button is pressed, the
1046 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
1047 * together sequentially; instead when the button is released, no event is
1048 * generated !
1049 *
1050 * With the command
1051 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
1052 * the mouse reacts differently:
1053 * - it never sends a keyboard key event
1054 * - for the three mouse button it sends:
1055 * middle button press 11<xx>0a 3500af00...
1056 * side 1 button (forward) press 11<xx>0a 3500b000...
1057 * side 2 button (backward) press 11<xx>0a 3500ae00...
1058 * middle/side1/side2 button release 11<xx>0a 35000000...
1059 */
1060
1061 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
1062
1063 struct m560_private_data {
1064 struct input_dev *input;
1065 };
1066
1067 /* how buttons are mapped in the report */
1068 #define M560_MOUSE_BTN_LEFT 0x01
1069 #define M560_MOUSE_BTN_RIGHT 0x02
1070 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
1071 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
1072
1073 #define M560_SUB_ID 0x0a
1074 #define M560_BUTTON_MODE_REGISTER 0x35
1075
1076 static int m560_send_config_command(struct hid_device *hdev, bool connected)
1077 {
1078 struct hidpp_report response;
1079 struct hidpp_device *hidpp_dev;
1080
1081 hidpp_dev = hid_get_drvdata(hdev);
1082
1083 if (!connected)
1084 return -ENODEV;
1085
1086 return hidpp_send_rap_command_sync(
1087 hidpp_dev,
1088 REPORT_ID_HIDPP_SHORT,
1089 M560_SUB_ID,
1090 M560_BUTTON_MODE_REGISTER,
1091 (u8 *)m560_config_parameter,
1092 sizeof(m560_config_parameter),
1093 &response
1094 );
1095 }
1096
1097 static int m560_allocate(struct hid_device *hdev)
1098 {
1099 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1100 struct m560_private_data *d;
1101
1102 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
1103 GFP_KERNEL);
1104 if (!d)
1105 return -ENOMEM;
1106
1107 hidpp->private_data = d;
1108
1109 return 0;
1110 };
1111
1112 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
1113 {
1114 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1115 struct m560_private_data *mydata = hidpp->private_data;
1116
1117 /* sanity check */
1118 if (!mydata || !mydata->input) {
1119 hid_err(hdev, "error in parameter\n");
1120 return -EINVAL;
1121 }
1122
1123 if (size < 7) {
1124 hid_err(hdev, "error in report\n");
1125 return 0;
1126 }
1127
1128 if (data[0] == REPORT_ID_HIDPP_LONG &&
1129 data[2] == M560_SUB_ID && data[6] == 0x00) {
1130 /*
1131 * m560 mouse report for middle, forward and backward button
1132 *
1133 * data[0] = 0x11
1134 * data[1] = device-id
1135 * data[2] = 0x0a
1136 * data[5] = 0xaf -> middle
1137 * 0xb0 -> forward
1138 * 0xae -> backward
1139 * 0x00 -> release all
1140 * data[6] = 0x00
1141 */
1142
1143 switch (data[5]) {
1144 case 0xaf:
1145 input_report_key(mydata->input, BTN_MIDDLE, 1);
1146 break;
1147 case 0xb0:
1148 input_report_key(mydata->input, BTN_FORWARD, 1);
1149 break;
1150 case 0xae:
1151 input_report_key(mydata->input, BTN_BACK, 1);
1152 break;
1153 case 0x00:
1154 input_report_key(mydata->input, BTN_BACK, 0);
1155 input_report_key(mydata->input, BTN_FORWARD, 0);
1156 input_report_key(mydata->input, BTN_MIDDLE, 0);
1157 break;
1158 default:
1159 hid_err(hdev, "error in report\n");
1160 return 0;
1161 }
1162 input_sync(mydata->input);
1163
1164 } else if (data[0] == 0x02) {
1165 /*
1166 * Logitech M560 mouse report
1167 *
1168 * data[0] = type (0x02)
1169 * data[1..2] = buttons
1170 * data[3..5] = xy
1171 * data[6] = wheel
1172 */
1173
1174 int v;
1175
1176 input_report_key(mydata->input, BTN_LEFT,
1177 !!(data[1] & M560_MOUSE_BTN_LEFT));
1178 input_report_key(mydata->input, BTN_RIGHT,
1179 !!(data[1] & M560_MOUSE_BTN_RIGHT));
1180
1181 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
1182 input_report_rel(mydata->input, REL_HWHEEL, -1);
1183 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
1184 input_report_rel(mydata->input, REL_HWHEEL, 1);
1185
1186 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
1187 input_report_rel(mydata->input, REL_X, v);
1188
1189 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
1190 input_report_rel(mydata->input, REL_Y, v);
1191
1192 v = hid_snto32(data[6], 8);
1193 input_report_rel(mydata->input, REL_WHEEL, v);
1194
1195 input_sync(mydata->input);
1196 }
1197
1198 return 1;
1199 }
1200
1201 static void m560_populate_input(struct hidpp_device *hidpp,
1202 struct input_dev *input_dev, bool origin_is_hid_core)
1203 {
1204 struct m560_private_data *mydata = hidpp->private_data;
1205
1206 mydata->input = input_dev;
1207
1208 __set_bit(EV_KEY, mydata->input->evbit);
1209 __set_bit(BTN_MIDDLE, mydata->input->keybit);
1210 __set_bit(BTN_RIGHT, mydata->input->keybit);
1211 __set_bit(BTN_LEFT, mydata->input->keybit);
1212 __set_bit(BTN_BACK, mydata->input->keybit);
1213 __set_bit(BTN_FORWARD, mydata->input->keybit);
1214
1215 __set_bit(EV_REL, mydata->input->evbit);
1216 __set_bit(REL_X, mydata->input->relbit);
1217 __set_bit(REL_Y, mydata->input->relbit);
1218 __set_bit(REL_WHEEL, mydata->input->relbit);
1219 __set_bit(REL_HWHEEL, mydata->input->relbit);
1220 }
1221
1222 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1223 struct hid_field *field, struct hid_usage *usage,
1224 unsigned long **bit, int *max)
1225 {
1226 return -1;
1227 }
1228
1229 /* ------------------------------------------------------------------------- */
1230 /* Logitech K400 devices */
1231 /* ------------------------------------------------------------------------- */
1232
1233 /*
1234 * The Logitech K400 keyboard has an embedded touchpad which is seen
1235 * as a mouse from the OS point of view. There is a hardware shortcut to disable
1236 * tap-to-click but the setting is not remembered accross reset, annoying some
1237 * users.
1238 *
1239 * We can toggle this feature from the host by using the feature 0x6010:
1240 * Touchpad FW items
1241 */
1242
1243 struct k400_private_data {
1244 u8 feature_index;
1245 };
1246
1247 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
1248 {
1249 struct k400_private_data *k400 = hidpp->private_data;
1250 struct hidpp_touchpad_fw_items items = {};
1251 int ret;
1252 u8 feature_type;
1253
1254 if (!k400->feature_index) {
1255 ret = hidpp_root_get_feature(hidpp,
1256 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
1257 &k400->feature_index, &feature_type);
1258 if (ret)
1259 /* means that the device is not powered up */
1260 return ret;
1261 }
1262
1263 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
1264 if (ret)
1265 return ret;
1266
1267 return 0;
1268 }
1269
1270 static int k400_allocate(struct hid_device *hdev)
1271 {
1272 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1273 struct k400_private_data *k400;
1274
1275 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
1276 GFP_KERNEL);
1277 if (!k400)
1278 return -ENOMEM;
1279
1280 hidpp->private_data = k400;
1281
1282 return 0;
1283 };
1284
1285 static int k400_connect(struct hid_device *hdev, bool connected)
1286 {
1287 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1288
1289 if (!connected)
1290 return 0;
1291
1292 if (!disable_tap_to_click)
1293 return 0;
1294
1295 return k400_disable_tap_to_click(hidpp);
1296 }
1297
1298 /* -------------------------------------------------------------------------- */
1299 /* Generic HID++ devices */
1300 /* -------------------------------------------------------------------------- */
1301
1302 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1303 struct hid_field *field, struct hid_usage *usage,
1304 unsigned long **bit, int *max)
1305 {
1306 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1307
1308 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1309 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
1310 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
1311 field->application != HID_GD_MOUSE)
1312 return m560_input_mapping(hdev, hi, field, usage, bit, max);
1313
1314 return 0;
1315 }
1316
1317 static void hidpp_populate_input(struct hidpp_device *hidpp,
1318 struct input_dev *input, bool origin_is_hid_core)
1319 {
1320 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1321 wtp_populate_input(hidpp, input, origin_is_hid_core);
1322 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
1323 m560_populate_input(hidpp, input, origin_is_hid_core);
1324 }
1325
1326 static int hidpp_input_configured(struct hid_device *hdev,
1327 struct hid_input *hidinput)
1328 {
1329 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1330 struct input_dev *input = hidinput->input;
1331
1332 hidpp_populate_input(hidpp, input, true);
1333
1334 return 0;
1335 }
1336
1337 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
1338 int size)
1339 {
1340 struct hidpp_report *question = hidpp->send_receive_buf;
1341 struct hidpp_report *answer = hidpp->send_receive_buf;
1342 struct hidpp_report *report = (struct hidpp_report *)data;
1343
1344 /*
1345 * If the mutex is locked then we have a pending answer from a
1346 * previously sent command.
1347 */
1348 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
1349 /*
1350 * Check for a correct hidpp20 answer or the corresponding
1351 * error
1352 */
1353 if (hidpp_match_answer(question, report) ||
1354 hidpp_match_error(question, report)) {
1355 *answer = *report;
1356 hidpp->answer_available = true;
1357 wake_up(&hidpp->wait);
1358 /*
1359 * This was an answer to a command that this driver sent
1360 * We return 1 to hid-core to avoid forwarding the
1361 * command upstream as it has been treated by the driver
1362 */
1363
1364 return 1;
1365 }
1366 }
1367
1368 if (unlikely(hidpp_report_is_connect_event(report))) {
1369 atomic_set(&hidpp->connected,
1370 !(report->rap.params[0] & (1 << 6)));
1371 if ((hidpp->quirks & HIDPP_QUIRK_CONNECT_EVENTS) &&
1372 (schedule_work(&hidpp->work) == 0))
1373 dbg_hid("%s: connect event already queued\n", __func__);
1374 return 1;
1375 }
1376
1377 return 0;
1378 }
1379
1380 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
1381 u8 *data, int size)
1382 {
1383 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1384 int ret = 0;
1385
1386 /* Generic HID++ processing. */
1387 switch (data[0]) {
1388 case REPORT_ID_HIDPP_VERY_LONG:
1389 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
1390 hid_err(hdev, "received hid++ report of bad size (%d)",
1391 size);
1392 return 1;
1393 }
1394 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1395 break;
1396 case REPORT_ID_HIDPP_LONG:
1397 if (size != HIDPP_REPORT_LONG_LENGTH) {
1398 hid_err(hdev, "received hid++ report of bad size (%d)",
1399 size);
1400 return 1;
1401 }
1402 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1403 break;
1404 case REPORT_ID_HIDPP_SHORT:
1405 if (size != HIDPP_REPORT_SHORT_LENGTH) {
1406 hid_err(hdev, "received hid++ report of bad size (%d)",
1407 size);
1408 return 1;
1409 }
1410 ret = hidpp_raw_hidpp_event(hidpp, data, size);
1411 break;
1412 }
1413
1414 /* If no report is available for further processing, skip calling
1415 * raw_event of subclasses. */
1416 if (ret != 0)
1417 return ret;
1418
1419 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
1420 return wtp_raw_event(hdev, data, size);
1421 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
1422 return m560_raw_event(hdev, data, size);
1423
1424 return 0;
1425 }
1426
1427 static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying)
1428 {
1429 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1430 char *name;
1431
1432 if (use_unifying)
1433 /*
1434 * the device is connected through an Unifying receiver, and
1435 * might not be already connected.
1436 * Ask the receiver for its name.
1437 */
1438 name = hidpp_get_unifying_name(hidpp);
1439 else
1440 name = hidpp_get_device_name(hidpp);
1441
1442 if (!name) {
1443 hid_err(hdev, "unable to retrieve the name of the device");
1444 } else {
1445 dbg_hid("HID++: Got name: %s\n", name);
1446 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
1447 }
1448
1449 kfree(name);
1450 }
1451
1452 static int hidpp_input_open(struct input_dev *dev)
1453 {
1454 struct hid_device *hid = input_get_drvdata(dev);
1455
1456 return hid_hw_open(hid);
1457 }
1458
1459 static void hidpp_input_close(struct input_dev *dev)
1460 {
1461 struct hid_device *hid = input_get_drvdata(dev);
1462
1463 hid_hw_close(hid);
1464 }
1465
1466 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
1467 {
1468 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
1469 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1470
1471 if (!input_dev)
1472 return NULL;
1473
1474 input_set_drvdata(input_dev, hdev);
1475 input_dev->open = hidpp_input_open;
1476 input_dev->close = hidpp_input_close;
1477
1478 input_dev->name = hidpp->name;
1479 input_dev->phys = hdev->phys;
1480 input_dev->uniq = hdev->uniq;
1481 input_dev->id.bustype = hdev->bus;
1482 input_dev->id.vendor = hdev->vendor;
1483 input_dev->id.product = hdev->product;
1484 input_dev->id.version = hdev->version;
1485 input_dev->dev.parent = &hdev->dev;
1486
1487 return input_dev;
1488 }
1489
1490 static void hidpp_connect_event(struct hidpp_device *hidpp)
1491 {
1492 struct hid_device *hdev = hidpp->hid_dev;
1493 int ret = 0;
1494 bool connected = atomic_read(&hidpp->connected);
1495 struct input_dev *input;
1496 char *name, *devm_name;
1497
1498 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1499 ret = wtp_connect(hdev, connected);
1500 if (ret)
1501 return;
1502 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
1503 ret = m560_send_config_command(hdev, connected);
1504 if (ret)
1505 return;
1506 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
1507 ret = k400_connect(hdev, connected);
1508 if (ret)
1509 return;
1510 }
1511
1512 if (!connected || hidpp->delayed_input)
1513 return;
1514
1515 /* the device is already connected, we can ask for its name and
1516 * protocol */
1517 if (!hidpp->protocol_major) {
1518 ret = !hidpp_is_connected(hidpp);
1519 if (ret) {
1520 hid_err(hdev, "Can not get the protocol version.\n");
1521 return;
1522 }
1523 hid_info(hdev, "HID++ %u.%u device connected.\n",
1524 hidpp->protocol_major, hidpp->protocol_minor);
1525 }
1526
1527 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT))
1528 /* if HID created the input nodes for us, we can stop now */
1529 return;
1530
1531 if (!hidpp->name || hidpp->name == hdev->name) {
1532 name = hidpp_get_device_name(hidpp);
1533 if (!name) {
1534 hid_err(hdev,
1535 "unable to retrieve the name of the device");
1536 return;
1537 }
1538
1539 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
1540 kfree(name);
1541 if (!devm_name)
1542 return;
1543
1544 hidpp->name = devm_name;
1545 }
1546
1547 input = hidpp_allocate_input(hdev);
1548 if (!input) {
1549 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
1550 return;
1551 }
1552
1553 hidpp_populate_input(hidpp, input, false);
1554
1555 ret = input_register_device(input);
1556 if (ret)
1557 input_free_device(input);
1558
1559 hidpp->delayed_input = input;
1560 }
1561
1562 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
1563 {
1564 struct hidpp_device *hidpp;
1565 int ret;
1566 bool connected;
1567 unsigned int connect_mask = HID_CONNECT_DEFAULT;
1568
1569 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
1570 GFP_KERNEL);
1571 if (!hidpp)
1572 return -ENOMEM;
1573
1574 hidpp->hid_dev = hdev;
1575 hidpp->name = hdev->name;
1576 hid_set_drvdata(hdev, hidpp);
1577
1578 hidpp->quirks = id->driver_data;
1579
1580 if (disable_raw_mode) {
1581 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
1582 hidpp->quirks &= ~HIDPP_QUIRK_CONNECT_EVENTS;
1583 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
1584 }
1585
1586 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
1587 ret = wtp_allocate(hdev, id);
1588 if (ret)
1589 goto allocate_fail;
1590 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
1591 ret = m560_allocate(hdev);
1592 if (ret)
1593 goto allocate_fail;
1594 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
1595 ret = k400_allocate(hdev);
1596 if (ret)
1597 goto allocate_fail;
1598 }
1599
1600 INIT_WORK(&hidpp->work, delayed_work_cb);
1601 mutex_init(&hidpp->send_mutex);
1602 init_waitqueue_head(&hidpp->wait);
1603
1604 ret = hid_parse(hdev);
1605 if (ret) {
1606 hid_err(hdev, "%s:parse failed\n", __func__);
1607 goto hid_parse_fail;
1608 }
1609
1610 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
1611 connect_mask &= ~HID_CONNECT_HIDINPUT;
1612
1613 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
1614 ret = hid_hw_start(hdev, connect_mask);
1615 if (ret) {
1616 hid_err(hdev, "hw start failed\n");
1617 goto hid_hw_start_fail;
1618 }
1619 ret = hid_hw_open(hdev);
1620 if (ret < 0) {
1621 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
1622 __func__, ret);
1623 hid_hw_stop(hdev);
1624 goto hid_hw_start_fail;
1625 }
1626 }
1627
1628
1629 /* Allow incoming packets */
1630 hid_device_io_start(hdev);
1631
1632 connected = hidpp_is_connected(hidpp);
1633 if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) {
1634 if (!connected) {
1635 ret = -ENODEV;
1636 hid_err(hdev, "Device not connected");
1637 goto hid_hw_open_failed;
1638 }
1639
1640 hid_info(hdev, "HID++ %u.%u device connected.\n",
1641 hidpp->protocol_major, hidpp->protocol_minor);
1642 }
1643
1644 hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE);
1645 atomic_set(&hidpp->connected, connected);
1646
1647 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
1648 ret = wtp_get_config(hidpp);
1649 if (ret)
1650 goto hid_hw_open_failed;
1651 }
1652
1653 /* Block incoming packets */
1654 hid_device_io_stop(hdev);
1655
1656 if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
1657 ret = hid_hw_start(hdev, connect_mask);
1658 if (ret) {
1659 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
1660 goto hid_hw_start_fail;
1661 }
1662 }
1663
1664 if (hidpp->quirks & HIDPP_QUIRK_CONNECT_EVENTS) {
1665 /* Allow incoming packets */
1666 hid_device_io_start(hdev);
1667
1668 hidpp_connect_event(hidpp);
1669 }
1670
1671 return ret;
1672
1673 hid_hw_open_failed:
1674 hid_device_io_stop(hdev);
1675 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
1676 hid_hw_close(hdev);
1677 hid_hw_stop(hdev);
1678 }
1679 hid_hw_start_fail:
1680 hid_parse_fail:
1681 cancel_work_sync(&hidpp->work);
1682 mutex_destroy(&hidpp->send_mutex);
1683 allocate_fail:
1684 hid_set_drvdata(hdev, NULL);
1685 return ret;
1686 }
1687
1688 static void hidpp_remove(struct hid_device *hdev)
1689 {
1690 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1691
1692 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)
1693 hid_hw_close(hdev);
1694 hid_hw_stop(hdev);
1695 cancel_work_sync(&hidpp->work);
1696 mutex_destroy(&hidpp->send_mutex);
1697 }
1698
1699 static const struct hid_device_id hidpp_devices[] = {
1700 { /* wireless touchpad */
1701 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1702 USB_VENDOR_ID_LOGITECH, 0x4011),
1703 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
1704 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
1705 { /* wireless touchpad T650 */
1706 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1707 USB_VENDOR_ID_LOGITECH, 0x4101),
1708 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
1709 { /* wireless touchpad T651 */
1710 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
1711 USB_DEVICE_ID_LOGITECH_T651),
1712 .driver_data = HIDPP_QUIRK_CLASS_WTP },
1713 { /* Mouse logitech M560 */
1714 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1715 USB_VENDOR_ID_LOGITECH, 0x402d),
1716 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
1717 { /* Keyboard logitech K400 */
1718 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1719 USB_VENDOR_ID_LOGITECH, 0x4024),
1720 .driver_data = HIDPP_QUIRK_CONNECT_EVENTS | HIDPP_QUIRK_CLASS_K400 },
1721
1722 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
1723 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
1724
1725 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
1726 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
1727 {}
1728 };
1729
1730 MODULE_DEVICE_TABLE(hid, hidpp_devices);
1731
1732 static struct hid_driver hidpp_driver = {
1733 .name = "logitech-hidpp-device",
1734 .id_table = hidpp_devices,
1735 .probe = hidpp_probe,
1736 .remove = hidpp_remove,
1737 .raw_event = hidpp_raw_event,
1738 .input_configured = hidpp_input_configured,
1739 .input_mapping = hidpp_input_mapping,
1740 };
1741
1742 module_hid_driver(hidpp_driver);