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