]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/hid/hid-logitech-hidpp.c
HID: logitech-hidpp: notify battery on connect
[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/input.h>
19 #include <linux/usb.h>
20 #include <linux/hid.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/kfifo.h>
25 #include <linux/input/mt.h>
26 #include <linux/workqueue.h>
27 #include <linux/atomic.h>
28 #include <linux/fixp-arith.h>
29 #include <asm/unaligned.h>
30 #include "usbhid/usbhid.h"
31 #include "hid-ids.h"
32
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
35 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
36
37 static bool disable_raw_mode;
38 module_param(disable_raw_mode, bool, 0644);
39 MODULE_PARM_DESC(disable_raw_mode,
40 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
41
42 static bool disable_tap_to_click;
43 module_param(disable_tap_to_click, bool, 0644);
44 MODULE_PARM_DESC(disable_tap_to_click,
45 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
46
47 #define REPORT_ID_HIDPP_SHORT 0x10
48 #define REPORT_ID_HIDPP_LONG 0x11
49 #define REPORT_ID_HIDPP_VERY_LONG 0x12
50
51 #define HIDPP_REPORT_SHORT_LENGTH 7
52 #define HIDPP_REPORT_LONG_LENGTH 20
53 #define HIDPP_REPORT_VERY_LONG_LENGTH 64
54
55 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
56 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
57 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
58 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
59
60 /* bits 2..20 are reserved for classes */
61 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
62 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
63 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
64 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
65 #define HIDPP_QUIRK_UNIFYING BIT(25)
66
67 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
68
69 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
70 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
71
72 /*
73 * There are two hidpp protocols in use, the first version hidpp10 is known
74 * as register access protocol or RAP, the second version hidpp20 is known as
75 * feature access protocol or FAP
76 *
77 * Most older devices (including the Unifying usb receiver) use the RAP protocol
78 * where as most newer devices use the FAP protocol. Both protocols are
79 * compatible with the underlying transport, which could be usb, Unifiying, or
80 * bluetooth. The message lengths are defined by the hid vendor specific report
81 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
82 * the HIDPP_LONG report type (total message length 20 bytes)
83 *
84 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
85 * messages. The Unifying receiver itself responds to RAP messages (device index
86 * is 0xFF for the receiver), and all messages (short or long) with a device
87 * index between 1 and 6 are passed untouched to the corresponding paired
88 * Unifying device.
89 *
90 * The paired device can be RAP or FAP, it will receive the message untouched
91 * from the Unifiying receiver.
92 */
93
94 struct fap {
95 u8 feature_index;
96 u8 funcindex_clientid;
97 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
98 };
99
100 struct rap {
101 u8 sub_id;
102 u8 reg_address;
103 u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U];
104 };
105
106 struct hidpp_report {
107 u8 report_id;
108 u8 device_index;
109 union {
110 struct fap fap;
111 struct rap rap;
112 u8 rawbytes[sizeof(struct fap)];
113 };
114 } __packed;
115
116 struct hidpp_battery {
117 u8 feature_index;
118 struct power_supply_desc desc;
119 struct power_supply *ps;
120 char name[64];
121 int status;
122 int level;
123 };
124
125 struct hidpp_device {
126 struct hid_device *hid_dev;
127 struct mutex send_mutex;
128 void *send_receive_buf;
129 char *name; /* will never be NULL and should not be freed */
130 wait_queue_head_t wait;
131 bool answer_available;
132 u8 protocol_major;
133 u8 protocol_minor;
134
135 void *private_data;
136
137 struct work_struct work;
138 struct kfifo delayed_work_fifo;
139 atomic_t connected;
140 struct input_dev *delayed_input;
141
142 unsigned long quirks;
143 unsigned long capabilities;
144
145 struct hidpp_battery battery;
146 };
147
148 /* HID++ 1.0 error codes */
149 #define HIDPP_ERROR 0x8f
150 #define HIDPP_ERROR_SUCCESS 0x00
151 #define HIDPP_ERROR_INVALID_SUBID 0x01
152 #define HIDPP_ERROR_INVALID_ADRESS 0x02
153 #define HIDPP_ERROR_INVALID_VALUE 0x03
154 #define HIDPP_ERROR_CONNECT_FAIL 0x04
155 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
156 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
157 #define HIDPP_ERROR_BUSY 0x07
158 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
159 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
160 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
161 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
162 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
163 /* HID++ 2.0 error codes */
164 #define HIDPP20_ERROR 0xff
165
166 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
167
168 static int __hidpp_send_report(struct hid_device *hdev,
169 struct hidpp_report *hidpp_report)
170 {
171 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
172 int fields_count, ret;
173
174 hidpp = hid_get_drvdata(hdev);
175
176 switch (hidpp_report->report_id) {
177 case REPORT_ID_HIDPP_SHORT:
178 fields_count = HIDPP_REPORT_SHORT_LENGTH;
179 break;
180 case REPORT_ID_HIDPP_LONG:
181 fields_count = HIDPP_REPORT_LONG_LENGTH;
182 break;
183 case REPORT_ID_HIDPP_VERY_LONG:
184 fields_count = HIDPP_REPORT_VERY_LONG_LENGTH;
185 break;
186 default:
187 return -ENODEV;
188 }
189
190 /*
191 * set the device_index as the receiver, it will be overwritten by
192 * hid_hw_request if needed
193 */
194 hidpp_report->device_index = 0xff;
195
196 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
197 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
198 } else {
199 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
200 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
201 HID_REQ_SET_REPORT);
202 }
203
204 return ret == fields_count ? 0 : -1;
205 }
206
207 /**
208 * hidpp_send_message_sync() returns 0 in case of success, and something else
209 * in case of a failure.
210 * - If ' something else' is positive, that means that an error has been raised
211 * by the protocol itself.
212 * - If ' something else' is negative, that means that we had a classic error
213 * (-ENOMEM, -EPIPE, etc...)
214 */
215 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
216 struct hidpp_report *message,
217 struct hidpp_report *response)
218 {
219 int ret;
220
221 mutex_lock(&hidpp->send_mutex);
222
223 hidpp->send_receive_buf = response;
224 hidpp->answer_available = false;
225
226 /*
227 * So that we can later validate the answer when it arrives
228 * in hidpp_raw_event
229 */
230 *response = *message;
231
232 ret = __hidpp_send_report(hidpp->hid_dev, message);
233
234 if (ret) {
235 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
236 memset(response, 0, sizeof(struct hidpp_report));
237 goto exit;
238 }
239
240 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
241 5*HZ)) {
242 dbg_hid("%s:timeout waiting for response\n", __func__);
243 memset(response, 0, sizeof(struct hidpp_report));
244 ret = -ETIMEDOUT;
245 }
246
247 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
248 response->rap.sub_id == HIDPP_ERROR) {
249 ret = response->rap.params[1];
250 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
251 goto exit;
252 }
253
254 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
255 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
256 response->fap.feature_index == HIDPP20_ERROR) {
257 ret = response->fap.params[1];
258 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
259 goto exit;
260 }
261
262 exit:
263 mutex_unlock(&hidpp->send_mutex);
264 return ret;
265
266 }
267
268 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
269 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
270 struct hidpp_report *response)
271 {
272 struct hidpp_report *message;
273 int ret;
274
275 if (param_count > sizeof(message->fap.params))
276 return -EINVAL;
277
278 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
279 if (!message)
280 return -ENOMEM;
281
282 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
283 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
284 else
285 message->report_id = REPORT_ID_HIDPP_LONG;
286 message->fap.feature_index = feat_index;
287 message->fap.funcindex_clientid = funcindex_clientid;
288 memcpy(&message->fap.params, params, param_count);
289
290 ret = hidpp_send_message_sync(hidpp, message, response);
291 kfree(message);
292 return ret;
293 }
294
295 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
296 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
297 struct hidpp_report *response)
298 {
299 struct hidpp_report *message;
300 int ret, max_count;
301
302 switch (report_id) {
303 case REPORT_ID_HIDPP_SHORT:
304 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
305 break;
306 case REPORT_ID_HIDPP_LONG:
307 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
308 break;
309 case REPORT_ID_HIDPP_VERY_LONG:
310 max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
311 break;
312 default:
313 return -EINVAL;
314 }
315
316 if (param_count > max_count)
317 return -EINVAL;
318
319 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
320 if (!message)
321 return -ENOMEM;
322 message->report_id = report_id;
323 message->rap.sub_id = sub_id;
324 message->rap.reg_address = reg_address;
325 memcpy(&message->rap.params, params, param_count);
326
327 ret = hidpp_send_message_sync(hidpp_dev, message, response);
328 kfree(message);
329 return ret;
330 }
331
332 static void delayed_work_cb(struct work_struct *work)
333 {
334 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
335 work);
336 hidpp_connect_event(hidpp);
337 }
338
339 static inline bool hidpp_match_answer(struct hidpp_report *question,
340 struct hidpp_report *answer)
341 {
342 return (answer->fap.feature_index == question->fap.feature_index) &&
343 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
344 }
345
346 static inline bool hidpp_match_error(struct hidpp_report *question,
347 struct hidpp_report *answer)
348 {
349 return ((answer->rap.sub_id == HIDPP_ERROR) ||
350 (answer->fap.feature_index == HIDPP20_ERROR)) &&
351 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
352 (answer->fap.params[0] == question->fap.funcindex_clientid);
353 }
354
355 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
356 {
357 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
358 (report->rap.sub_id == 0x41);
359 }
360
361 /**
362 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
363 */
364 static void hidpp_prefix_name(char **name, int name_length)
365 {
366 #define PREFIX_LENGTH 9 /* "Logitech " */
367
368 int new_length;
369 char *new_name;
370
371 if (name_length > PREFIX_LENGTH &&
372 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
373 /* The prefix has is already in the name */
374 return;
375
376 new_length = PREFIX_LENGTH + name_length;
377 new_name = kzalloc(new_length, GFP_KERNEL);
378 if (!new_name)
379 return;
380
381 snprintf(new_name, new_length, "Logitech %s", *name);
382
383 kfree(*name);
384
385 *name = new_name;
386 }
387
388 /* -------------------------------------------------------------------------- */
389 /* HIDP++ 1.0 commands */
390 /* -------------------------------------------------------------------------- */
391
392 #define HIDPP_SET_REGISTER 0x80
393 #define HIDPP_GET_REGISTER 0x81
394 #define HIDPP_SET_LONG_REGISTER 0x82
395 #define HIDPP_GET_LONG_REGISTER 0x83
396
397 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
398 #define HIDPP_EXTENDED_PAIRING 0x30
399 #define HIDPP_DEVICE_NAME 0x40
400
401 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
402 {
403 struct hidpp_report response;
404 int ret;
405 u8 params[1] = { HIDPP_DEVICE_NAME };
406 char *name;
407 int len;
408
409 ret = hidpp_send_rap_command_sync(hidpp_dev,
410 REPORT_ID_HIDPP_SHORT,
411 HIDPP_GET_LONG_REGISTER,
412 HIDPP_REG_PAIRING_INFORMATION,
413 params, 1, &response);
414 if (ret)
415 return NULL;
416
417 len = response.rap.params[1];
418
419 if (2 + len > sizeof(response.rap.params))
420 return NULL;
421
422 name = kzalloc(len + 1, GFP_KERNEL);
423 if (!name)
424 return NULL;
425
426 memcpy(name, &response.rap.params[2], len);
427
428 /* include the terminating '\0' */
429 hidpp_prefix_name(&name, len + 1);
430
431 return name;
432 }
433
434 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
435 {
436 struct hidpp_report response;
437 int ret;
438 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
439
440 ret = hidpp_send_rap_command_sync(hidpp,
441 REPORT_ID_HIDPP_SHORT,
442 HIDPP_GET_LONG_REGISTER,
443 HIDPP_REG_PAIRING_INFORMATION,
444 params, 1, &response);
445 if (ret)
446 return ret;
447
448 /*
449 * We don't care about LE or BE, we will output it as a string
450 * with %4phD, so we need to keep the order.
451 */
452 *serial = *((u32 *)&response.rap.params[1]);
453 return 0;
454 }
455
456 static int hidpp_unifying_init(struct hidpp_device *hidpp)
457 {
458 struct hid_device *hdev = hidpp->hid_dev;
459 const char *name;
460 u32 serial;
461 int ret;
462
463 ret = hidpp_unifying_get_serial(hidpp, &serial);
464 if (ret)
465 return ret;
466
467 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
468 hdev->product, &serial);
469 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
470
471 name = hidpp_unifying_get_name(hidpp);
472 if (!name)
473 return -EIO;
474
475 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
476 dbg_hid("HID++ Unifying: Got name: %s\n", name);
477
478 kfree(name);
479 return 0;
480 }
481
482 /* -------------------------------------------------------------------------- */
483 /* 0x0000: Root */
484 /* -------------------------------------------------------------------------- */
485
486 #define HIDPP_PAGE_ROOT 0x0000
487 #define HIDPP_PAGE_ROOT_IDX 0x00
488
489 #define CMD_ROOT_GET_FEATURE 0x01
490 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
491
492 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
493 u8 *feature_index, u8 *feature_type)
494 {
495 struct hidpp_report response;
496 int ret;
497 u8 params[2] = { feature >> 8, feature & 0x00FF };
498
499 ret = hidpp_send_fap_command_sync(hidpp,
500 HIDPP_PAGE_ROOT_IDX,
501 CMD_ROOT_GET_FEATURE,
502 params, 2, &response);
503 if (ret)
504 return ret;
505
506 if (response.fap.params[0] == 0)
507 return -ENOENT;
508
509 *feature_index = response.fap.params[0];
510 *feature_type = response.fap.params[1];
511
512 return ret;
513 }
514
515 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
516 {
517 struct hidpp_report response;
518 int ret;
519
520 ret = hidpp_send_fap_command_sync(hidpp,
521 HIDPP_PAGE_ROOT_IDX,
522 CMD_ROOT_GET_PROTOCOL_VERSION,
523 NULL, 0, &response);
524
525 if (ret == HIDPP_ERROR_INVALID_SUBID) {
526 hidpp->protocol_major = 1;
527 hidpp->protocol_minor = 0;
528 return 0;
529 }
530
531 /* the device might not be connected */
532 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
533 return -EIO;
534
535 if (ret > 0) {
536 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
537 __func__, ret);
538 return -EPROTO;
539 }
540 if (ret)
541 return ret;
542
543 hidpp->protocol_major = response.fap.params[0];
544 hidpp->protocol_minor = response.fap.params[1];
545
546 return ret;
547 }
548
549 static bool hidpp_is_connected(struct hidpp_device *hidpp)
550 {
551 int ret;
552
553 ret = hidpp_root_get_protocol_version(hidpp);
554 if (!ret)
555 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
556 hidpp->protocol_major, hidpp->protocol_minor);
557 return ret == 0;
558 }
559
560 /* -------------------------------------------------------------------------- */
561 /* 0x0005: GetDeviceNameType */
562 /* -------------------------------------------------------------------------- */
563
564 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
565
566 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
567 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
568 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
569
570 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
571 u8 feature_index, u8 *nameLength)
572 {
573 struct hidpp_report response;
574 int ret;
575
576 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
577 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
578
579 if (ret > 0) {
580 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
581 __func__, ret);
582 return -EPROTO;
583 }
584 if (ret)
585 return ret;
586
587 *nameLength = response.fap.params[0];
588
589 return ret;
590 }
591
592 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
593 u8 feature_index, u8 char_index, char *device_name, int len_buf)
594 {
595 struct hidpp_report response;
596 int ret, i;
597 int count;
598
599 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
600 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
601 &response);
602
603 if (ret > 0) {
604 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
605 __func__, ret);
606 return -EPROTO;
607 }
608 if (ret)
609 return ret;
610
611 switch (response.report_id) {
612 case REPORT_ID_HIDPP_VERY_LONG:
613 count = HIDPP_REPORT_VERY_LONG_LENGTH - 4;
614 break;
615 case REPORT_ID_HIDPP_LONG:
616 count = HIDPP_REPORT_LONG_LENGTH - 4;
617 break;
618 case REPORT_ID_HIDPP_SHORT:
619 count = HIDPP_REPORT_SHORT_LENGTH - 4;
620 break;
621 default:
622 return -EPROTO;
623 }
624
625 if (len_buf < count)
626 count = len_buf;
627
628 for (i = 0; i < count; i++)
629 device_name[i] = response.fap.params[i];
630
631 return count;
632 }
633
634 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
635 {
636 u8 feature_type;
637 u8 feature_index;
638 u8 __name_length;
639 char *name;
640 unsigned index = 0;
641 int ret;
642
643 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
644 &feature_index, &feature_type);
645 if (ret)
646 return NULL;
647
648 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
649 &__name_length);
650 if (ret)
651 return NULL;
652
653 name = kzalloc(__name_length + 1, GFP_KERNEL);
654 if (!name)
655 return NULL;
656
657 while (index < __name_length) {
658 ret = hidpp_devicenametype_get_device_name(hidpp,
659 feature_index, index, name + index,
660 __name_length - index);
661 if (ret <= 0) {
662 kfree(name);
663 return NULL;
664 }
665 index += ret;
666 }
667
668 /* include the terminating '\0' */
669 hidpp_prefix_name(&name, __name_length + 1);
670
671 return name;
672 }
673
674 /* -------------------------------------------------------------------------- */
675 /* 0x1000: Battery level status */
676 /* -------------------------------------------------------------------------- */
677
678 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
679
680 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
681 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
682
683 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
684
685 static int hidpp20_batterylevel_map_status_level(u8 data[3], int *level,
686 int *next_level)
687 {
688 int status;
689 int level_override;
690
691 *level = data[0];
692 *next_level = data[1];
693
694 /* When discharging, we can rely on the device reported level.
695 * For all other states the device reports level 0 (unknown). Make up
696 * a number instead
697 */
698 switch (data[2]) {
699 case 0: /* discharging (in use) */
700 status = POWER_SUPPLY_STATUS_DISCHARGING;
701 level_override = 0;
702 break;
703 case 1: /* recharging */
704 status = POWER_SUPPLY_STATUS_CHARGING;
705 level_override = 80;
706 break;
707 case 2: /* charge in final stage */
708 status = POWER_SUPPLY_STATUS_CHARGING;
709 level_override = 90;
710 break;
711 case 3: /* charge complete */
712 status = POWER_SUPPLY_STATUS_FULL;
713 level_override = 100;
714 break;
715 case 4: /* recharging below optimal speed */
716 status = POWER_SUPPLY_STATUS_CHARGING;
717 level_override = 50;
718 break;
719 /* 5 = invalid battery type
720 6 = thermal error
721 7 = other charging error */
722 default:
723 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
724 level_override = 0;
725 break;
726 }
727
728 if (level_override != 0 && *level == 0)
729 *level = level_override;
730
731 return status;
732 }
733
734 static int hidpp20_batterylevel_get_battery_level(struct hidpp_device *hidpp,
735 u8 feature_index,
736 int *status,
737 int *level,
738 int *next_level)
739 {
740 struct hidpp_report response;
741 int ret;
742 u8 *params = (u8 *)response.fap.params;
743
744 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
745 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
746 NULL, 0, &response);
747 if (ret > 0) {
748 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
749 __func__, ret);
750 return -EPROTO;
751 }
752 if (ret)
753 return ret;
754
755 *status = hidpp20_batterylevel_map_status_level(params, level,
756 next_level);
757
758 return 0;
759 }
760
761 static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
762 {
763 u8 feature_type;
764 int ret;
765 int status, level, next_level;
766
767 if (hidpp->battery.feature_index == 0) {
768 ret = hidpp_root_get_feature(hidpp,
769 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
770 &hidpp->battery.feature_index,
771 &feature_type);
772 if (ret)
773 return ret;
774 }
775
776 ret = hidpp20_batterylevel_get_battery_level(hidpp,
777 hidpp->battery.feature_index,
778 &status, &level, &next_level);
779 if (ret)
780 return ret;
781
782 hidpp->battery.status = status;
783 hidpp->battery.level = level;
784
785 return 0;
786 }
787
788 static int hidpp20_battery_event(struct hidpp_device *hidpp,
789 u8 *data, int size)
790 {
791 struct hidpp_report *report = (struct hidpp_report *)data;
792 int status, level, next_level;
793 bool changed;
794
795 if (report->fap.feature_index != hidpp->battery.feature_index ||
796 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
797 return 0;
798
799 status = hidpp20_batterylevel_map_status_level(report->fap.params,
800 &level, &next_level);
801
802 changed = level != hidpp->battery.level ||
803 status != hidpp->battery.status;
804
805 if (changed) {
806 hidpp->battery.level = level;
807 hidpp->battery.status = status;
808 if (hidpp->battery.ps)
809 power_supply_changed(hidpp->battery.ps);
810 }
811
812 return 0;
813 }
814
815 static enum power_supply_property hidpp_battery_props[] = {
816 POWER_SUPPLY_PROP_STATUS,
817 POWER_SUPPLY_PROP_CAPACITY,
818 POWER_SUPPLY_PROP_SCOPE,
819 POWER_SUPPLY_PROP_MODEL_NAME,
820 POWER_SUPPLY_PROP_MANUFACTURER,
821 POWER_SUPPLY_PROP_SERIAL_NUMBER,
822 };
823
824 static int hidpp_battery_get_property(struct power_supply *psy,
825 enum power_supply_property psp,
826 union power_supply_propval *val)
827 {
828 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
829 int ret = 0;
830
831 switch(psp) {
832 case POWER_SUPPLY_PROP_STATUS:
833 val->intval = hidpp->battery.status;
834 break;
835 case POWER_SUPPLY_PROP_CAPACITY:
836 val->intval = hidpp->battery.level;
837 break;
838 case POWER_SUPPLY_PROP_SCOPE:
839 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
840 break;
841 case POWER_SUPPLY_PROP_MODEL_NAME:
842 if (!strncmp(hidpp->name, "Logitech ", 9))
843 val->strval = hidpp->name + 9;
844 else
845 val->strval = hidpp->name;
846 break;
847 case POWER_SUPPLY_PROP_MANUFACTURER:
848 val->strval = "Logitech";
849 break;
850 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
851 val->strval = hidpp->hid_dev->uniq;
852 break;
853 default:
854 ret = -EINVAL;
855 break;
856 }
857
858 return ret;
859 }
860
861 /* -------------------------------------------------------------------------- */
862 /* 0x6010: Touchpad FW items */
863 /* -------------------------------------------------------------------------- */
864
865 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
866
867 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
868
869 struct hidpp_touchpad_fw_items {
870 uint8_t presence;
871 uint8_t desired_state;
872 uint8_t state;
873 uint8_t persistent;
874 };
875
876 /**
877 * send a set state command to the device by reading the current items->state
878 * field. items is then filled with the current state.
879 */
880 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
881 u8 feature_index,
882 struct hidpp_touchpad_fw_items *items)
883 {
884 struct hidpp_report response;
885 int ret;
886 u8 *params = (u8 *)response.fap.params;
887
888 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
889 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
890
891 if (ret > 0) {
892 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
893 __func__, ret);
894 return -EPROTO;
895 }
896 if (ret)
897 return ret;
898
899 items->presence = params[0];
900 items->desired_state = params[1];
901 items->state = params[2];
902 items->persistent = params[3];
903
904 return 0;
905 }
906
907 /* -------------------------------------------------------------------------- */
908 /* 0x6100: TouchPadRawXY */
909 /* -------------------------------------------------------------------------- */
910
911 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
912
913 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
914 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
915
916 #define EVENT_TOUCHPAD_RAW_XY 0x00
917
918 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
919 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
920
921 struct hidpp_touchpad_raw_info {
922 u16 x_size;
923 u16 y_size;
924 u8 z_range;
925 u8 area_range;
926 u8 timestamp_unit;
927 u8 maxcontacts;
928 u8 origin;
929 u16 res;
930 };
931
932 struct hidpp_touchpad_raw_xy_finger {
933 u8 contact_type;
934 u8 contact_status;
935 u16 x;
936 u16 y;
937 u8 z;
938 u8 area;
939 u8 finger_id;
940 };
941
942 struct hidpp_touchpad_raw_xy {
943 u16 timestamp;
944 struct hidpp_touchpad_raw_xy_finger fingers[2];
945 u8 spurious_flag;
946 u8 end_of_frame;
947 u8 finger_count;
948 u8 button;
949 };
950
951 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
952 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
953 {
954 struct hidpp_report response;
955 int ret;
956 u8 *params = (u8 *)response.fap.params;
957
958 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
959 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
960
961 if (ret > 0) {
962 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
963 __func__, ret);
964 return -EPROTO;
965 }
966 if (ret)
967 return ret;
968
969 raw_info->x_size = get_unaligned_be16(&params[0]);
970 raw_info->y_size = get_unaligned_be16(&params[2]);
971 raw_info->z_range = params[4];
972 raw_info->area_range = params[5];
973 raw_info->maxcontacts = params[7];
974 raw_info->origin = params[8];
975 /* res is given in unit per inch */
976 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
977
978 return ret;
979 }
980
981 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
982 u8 feature_index, bool send_raw_reports,
983 bool sensor_enhanced_settings)
984 {
985 struct hidpp_report response;
986
987 /*
988 * Params:
989 * bit 0 - enable raw
990 * bit 1 - 16bit Z, no area
991 * bit 2 - enhanced sensitivity
992 * bit 3 - width, height (4 bits each) instead of area
993 * bit 4 - send raw + gestures (degrades smoothness)
994 * remaining bits - reserved
995 */
996 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
997
998 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
999 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
1000 }
1001
1002 static void hidpp_touchpad_touch_event(u8 *data,
1003 struct hidpp_touchpad_raw_xy_finger *finger)
1004 {
1005 u8 x_m = data[0] << 2;
1006 u8 y_m = data[2] << 2;
1007
1008 finger->x = x_m << 6 | data[1];
1009 finger->y = y_m << 6 | data[3];
1010
1011 finger->contact_type = data[0] >> 6;
1012 finger->contact_status = data[2] >> 6;
1013
1014 finger->z = data[4];
1015 finger->area = data[5];
1016 finger->finger_id = data[6] >> 4;
1017 }
1018
1019 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1020 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1021 {
1022 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1023 raw_xy->end_of_frame = data[8] & 0x01;
1024 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1025 raw_xy->finger_count = data[15] & 0x0f;
1026 raw_xy->button = (data[8] >> 2) & 0x01;
1027
1028 if (raw_xy->finger_count) {
1029 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1030 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1031 }
1032 }
1033
1034 /* -------------------------------------------------------------------------- */
1035 /* 0x8123: Force feedback support */
1036 /* -------------------------------------------------------------------------- */
1037
1038 #define HIDPP_FF_GET_INFO 0x01
1039 #define HIDPP_FF_RESET_ALL 0x11
1040 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1041 #define HIDPP_FF_SET_EFFECT_STATE 0x31
1042 #define HIDPP_FF_DESTROY_EFFECT 0x41
1043 #define HIDPP_FF_GET_APERTURE 0x51
1044 #define HIDPP_FF_SET_APERTURE 0x61
1045 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1046 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1047
1048 #define HIDPP_FF_EFFECT_STATE_GET 0x00
1049 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
1050 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1051 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1052
1053 #define HIDPP_FF_EFFECT_CONSTANT 0x00
1054 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1055 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1056 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1057 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1058 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1059 #define HIDPP_FF_EFFECT_SPRING 0x06
1060 #define HIDPP_FF_EFFECT_DAMPER 0x07
1061 #define HIDPP_FF_EFFECT_FRICTION 0x08
1062 #define HIDPP_FF_EFFECT_INERTIA 0x09
1063 #define HIDPP_FF_EFFECT_RAMP 0x0A
1064
1065 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
1066
1067 #define HIDPP_FF_EFFECTID_NONE -1
1068 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
1069
1070 #define HIDPP_FF_MAX_PARAMS 20
1071 #define HIDPP_FF_RESERVED_SLOTS 1
1072
1073 struct hidpp_ff_private_data {
1074 struct hidpp_device *hidpp;
1075 u8 feature_index;
1076 u8 version;
1077 u16 gain;
1078 s16 range;
1079 u8 slot_autocenter;
1080 u8 num_effects;
1081 int *effect_ids;
1082 struct workqueue_struct *wq;
1083 atomic_t workqueue_size;
1084 };
1085
1086 struct hidpp_ff_work_data {
1087 struct work_struct work;
1088 struct hidpp_ff_private_data *data;
1089 int effect_id;
1090 u8 command;
1091 u8 params[HIDPP_FF_MAX_PARAMS];
1092 u8 size;
1093 };
1094
1095 static const signed short hiddpp_ff_effects[] = {
1096 FF_CONSTANT,
1097 FF_PERIODIC,
1098 FF_SINE,
1099 FF_SQUARE,
1100 FF_SAW_UP,
1101 FF_SAW_DOWN,
1102 FF_TRIANGLE,
1103 FF_SPRING,
1104 FF_DAMPER,
1105 FF_AUTOCENTER,
1106 FF_GAIN,
1107 -1
1108 };
1109
1110 static const signed short hiddpp_ff_effects_v2[] = {
1111 FF_RAMP,
1112 FF_FRICTION,
1113 FF_INERTIA,
1114 -1
1115 };
1116
1117 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1118 HIDPP_FF_EFFECT_SPRING,
1119 HIDPP_FF_EFFECT_FRICTION,
1120 HIDPP_FF_EFFECT_DAMPER,
1121 HIDPP_FF_EFFECT_INERTIA
1122 };
1123
1124 static const char *HIDPP_FF_CONDITION_NAMES[] = {
1125 "spring",
1126 "friction",
1127 "damper",
1128 "inertia"
1129 };
1130
1131
1132 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1133 {
1134 int i;
1135
1136 for (i = 0; i < data->num_effects; i++)
1137 if (data->effect_ids[i] == effect_id)
1138 return i+1;
1139
1140 return 0;
1141 }
1142
1143 static void hidpp_ff_work_handler(struct work_struct *w)
1144 {
1145 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1146 struct hidpp_ff_private_data *data = wd->data;
1147 struct hidpp_report response;
1148 u8 slot;
1149 int ret;
1150
1151 /* add slot number if needed */
1152 switch (wd->effect_id) {
1153 case HIDPP_FF_EFFECTID_AUTOCENTER:
1154 wd->params[0] = data->slot_autocenter;
1155 break;
1156 case HIDPP_FF_EFFECTID_NONE:
1157 /* leave slot as zero */
1158 break;
1159 default:
1160 /* find current slot for effect */
1161 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1162 break;
1163 }
1164
1165 /* send command and wait for reply */
1166 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1167 wd->command, wd->params, wd->size, &response);
1168
1169 if (ret) {
1170 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1171 goto out;
1172 }
1173
1174 /* parse return data */
1175 switch (wd->command) {
1176 case HIDPP_FF_DOWNLOAD_EFFECT:
1177 slot = response.fap.params[0];
1178 if (slot > 0 && slot <= data->num_effects) {
1179 if (wd->effect_id >= 0)
1180 /* regular effect uploaded */
1181 data->effect_ids[slot-1] = wd->effect_id;
1182 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1183 /* autocenter spring uploaded */
1184 data->slot_autocenter = slot;
1185 }
1186 break;
1187 case HIDPP_FF_DESTROY_EFFECT:
1188 if (wd->effect_id >= 0)
1189 /* regular effect destroyed */
1190 data->effect_ids[wd->params[0]-1] = -1;
1191 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1192 /* autocenter spring destoyed */
1193 data->slot_autocenter = 0;
1194 break;
1195 case HIDPP_FF_SET_GLOBAL_GAINS:
1196 data->gain = (wd->params[0] << 8) + wd->params[1];
1197 break;
1198 case HIDPP_FF_SET_APERTURE:
1199 data->range = (wd->params[0] << 8) + wd->params[1];
1200 break;
1201 default:
1202 /* no action needed */
1203 break;
1204 }
1205
1206 out:
1207 atomic_dec(&data->workqueue_size);
1208 kfree(wd);
1209 }
1210
1211 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1212 {
1213 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1214 int s;
1215
1216 if (!wd)
1217 return -ENOMEM;
1218
1219 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1220
1221 wd->data = data;
1222 wd->effect_id = effect_id;
1223 wd->command = command;
1224 wd->size = size;
1225 memcpy(wd->params, params, size);
1226
1227 atomic_inc(&data->workqueue_size);
1228 queue_work(data->wq, &wd->work);
1229
1230 /* warn about excessive queue size */
1231 s = atomic_read(&data->workqueue_size);
1232 if (s >= 20 && s % 20 == 0)
1233 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1234
1235 return 0;
1236 }
1237
1238 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1239 {
1240 struct hidpp_ff_private_data *data = dev->ff->private;
1241 u8 params[20];
1242 u8 size;
1243 int force;
1244
1245 /* set common parameters */
1246 params[2] = effect->replay.length >> 8;
1247 params[3] = effect->replay.length & 255;
1248 params[4] = effect->replay.delay >> 8;
1249 params[5] = effect->replay.delay & 255;
1250
1251 switch (effect->type) {
1252 case FF_CONSTANT:
1253 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1254 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1255 params[6] = force >> 8;
1256 params[7] = force & 255;
1257 params[8] = effect->u.constant.envelope.attack_level >> 7;
1258 params[9] = effect->u.constant.envelope.attack_length >> 8;
1259 params[10] = effect->u.constant.envelope.attack_length & 255;
1260 params[11] = effect->u.constant.envelope.fade_level >> 7;
1261 params[12] = effect->u.constant.envelope.fade_length >> 8;
1262 params[13] = effect->u.constant.envelope.fade_length & 255;
1263 size = 14;
1264 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1265 effect->u.constant.level,
1266 effect->direction, force);
1267 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1268 effect->u.constant.envelope.attack_level,
1269 effect->u.constant.envelope.attack_length,
1270 effect->u.constant.envelope.fade_level,
1271 effect->u.constant.envelope.fade_length);
1272 break;
1273 case FF_PERIODIC:
1274 {
1275 switch (effect->u.periodic.waveform) {
1276 case FF_SINE:
1277 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1278 break;
1279 case FF_SQUARE:
1280 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1281 break;
1282 case FF_SAW_UP:
1283 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1284 break;
1285 case FF_SAW_DOWN:
1286 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1287 break;
1288 case FF_TRIANGLE:
1289 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1290 break;
1291 default:
1292 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1293 return -EINVAL;
1294 }
1295 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1296 params[6] = effect->u.periodic.magnitude >> 8;
1297 params[7] = effect->u.periodic.magnitude & 255;
1298 params[8] = effect->u.periodic.offset >> 8;
1299 params[9] = effect->u.periodic.offset & 255;
1300 params[10] = effect->u.periodic.period >> 8;
1301 params[11] = effect->u.periodic.period & 255;
1302 params[12] = effect->u.periodic.phase >> 8;
1303 params[13] = effect->u.periodic.phase & 255;
1304 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1305 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1306 params[16] = effect->u.periodic.envelope.attack_length & 255;
1307 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1308 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1309 params[19] = effect->u.periodic.envelope.fade_length & 255;
1310 size = 20;
1311 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1312 effect->u.periodic.magnitude, effect->direction,
1313 effect->u.periodic.offset,
1314 effect->u.periodic.period,
1315 effect->u.periodic.phase);
1316 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1317 effect->u.periodic.envelope.attack_level,
1318 effect->u.periodic.envelope.attack_length,
1319 effect->u.periodic.envelope.fade_level,
1320 effect->u.periodic.envelope.fade_length);
1321 break;
1322 }
1323 case FF_RAMP:
1324 params[1] = HIDPP_FF_EFFECT_RAMP;
1325 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1326 params[6] = force >> 8;
1327 params[7] = force & 255;
1328 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1329 params[8] = force >> 8;
1330 params[9] = force & 255;
1331 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1332 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1333 params[12] = effect->u.ramp.envelope.attack_length & 255;
1334 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1335 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1336 params[15] = effect->u.ramp.envelope.fade_length & 255;
1337 size = 16;
1338 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1339 effect->u.ramp.start_level,
1340 effect->u.ramp.end_level,
1341 effect->direction, force);
1342 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1343 effect->u.ramp.envelope.attack_level,
1344 effect->u.ramp.envelope.attack_length,
1345 effect->u.ramp.envelope.fade_level,
1346 effect->u.ramp.envelope.fade_length);
1347 break;
1348 case FF_FRICTION:
1349 case FF_INERTIA:
1350 case FF_SPRING:
1351 case FF_DAMPER:
1352 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1353 params[6] = effect->u.condition[0].left_saturation >> 9;
1354 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1355 params[8] = effect->u.condition[0].left_coeff >> 8;
1356 params[9] = effect->u.condition[0].left_coeff & 255;
1357 params[10] = effect->u.condition[0].deadband >> 9;
1358 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1359 params[12] = effect->u.condition[0].center >> 8;
1360 params[13] = effect->u.condition[0].center & 255;
1361 params[14] = effect->u.condition[0].right_coeff >> 8;
1362 params[15] = effect->u.condition[0].right_coeff & 255;
1363 params[16] = effect->u.condition[0].right_saturation >> 9;
1364 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1365 size = 18;
1366 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1367 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1368 effect->u.condition[0].left_coeff,
1369 effect->u.condition[0].left_saturation,
1370 effect->u.condition[0].right_coeff,
1371 effect->u.condition[0].right_saturation);
1372 dbg_hid(" deadband=%d, center=%d\n",
1373 effect->u.condition[0].deadband,
1374 effect->u.condition[0].center);
1375 break;
1376 default:
1377 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1378 return -EINVAL;
1379 }
1380
1381 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1382 }
1383
1384 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1385 {
1386 struct hidpp_ff_private_data *data = dev->ff->private;
1387 u8 params[2];
1388
1389 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1390
1391 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1392
1393 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
1394 }
1395
1396 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
1397 {
1398 struct hidpp_ff_private_data *data = dev->ff->private;
1399 u8 slot = 0;
1400
1401 dbg_hid("Erasing effect %d.\n", effect_id);
1402
1403 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
1404 }
1405
1406 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
1407 {
1408 struct hidpp_ff_private_data *data = dev->ff->private;
1409 u8 params[18];
1410
1411 dbg_hid("Setting autocenter to %d.\n", magnitude);
1412
1413 /* start a standard spring effect */
1414 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
1415 /* zero delay and duration */
1416 params[2] = params[3] = params[4] = params[5] = 0;
1417 /* set coeff to 25% of saturation */
1418 params[8] = params[14] = magnitude >> 11;
1419 params[9] = params[15] = (magnitude >> 3) & 255;
1420 params[6] = params[16] = magnitude >> 9;
1421 params[7] = params[17] = (magnitude >> 1) & 255;
1422 /* zero deadband and center */
1423 params[10] = params[11] = params[12] = params[13] = 0;
1424
1425 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
1426 }
1427
1428 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
1429 {
1430 struct hidpp_ff_private_data *data = dev->ff->private;
1431 u8 params[4];
1432
1433 dbg_hid("Setting gain to %d.\n", gain);
1434
1435 params[0] = gain >> 8;
1436 params[1] = gain & 255;
1437 params[2] = 0; /* no boost */
1438 params[3] = 0;
1439
1440 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
1441 }
1442
1443 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
1444 {
1445 struct hid_device *hid = to_hid_device(dev);
1446 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1447 struct input_dev *idev = hidinput->input;
1448 struct hidpp_ff_private_data *data = idev->ff->private;
1449
1450 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
1451 }
1452
1453 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1454 {
1455 struct hid_device *hid = to_hid_device(dev);
1456 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1457 struct input_dev *idev = hidinput->input;
1458 struct hidpp_ff_private_data *data = idev->ff->private;
1459 u8 params[2];
1460 int range = simple_strtoul(buf, NULL, 10);
1461
1462 range = clamp(range, 180, 900);
1463
1464 params[0] = range >> 8;
1465 params[1] = range & 0x00FF;
1466
1467 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
1468
1469 return count;
1470 }
1471
1472 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
1473
1474 static void hidpp_ff_destroy(struct ff_device *ff)
1475 {
1476 struct hidpp_ff_private_data *data = ff->private;
1477
1478 kfree(data->effect_ids);
1479 }
1480
1481 static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
1482 {
1483 struct hid_device *hid = hidpp->hid_dev;
1484 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1485 struct input_dev *dev = hidinput->input;
1486 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1487 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1488 struct ff_device *ff;
1489 struct hidpp_report response;
1490 struct hidpp_ff_private_data *data;
1491 int error, j, num_slots;
1492 u8 version;
1493
1494 if (!dev) {
1495 hid_err(hid, "Struct input_dev not set!\n");
1496 return -EINVAL;
1497 }
1498
1499 /* Get firmware release */
1500 version = bcdDevice & 255;
1501
1502 /* Set supported force feedback capabilities */
1503 for (j = 0; hiddpp_ff_effects[j] >= 0; j++)
1504 set_bit(hiddpp_ff_effects[j], dev->ffbit);
1505 if (version > 1)
1506 for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++)
1507 set_bit(hiddpp_ff_effects_v2[j], dev->ffbit);
1508
1509 /* Read number of slots available in device */
1510 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1511 HIDPP_FF_GET_INFO, NULL, 0, &response);
1512 if (error) {
1513 if (error < 0)
1514 return error;
1515 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1516 __func__, error);
1517 return -EPROTO;
1518 }
1519
1520 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
1521
1522 error = input_ff_create(dev, num_slots);
1523
1524 if (error) {
1525 hid_err(dev, "Failed to create FF device!\n");
1526 return error;
1527 }
1528
1529 data = kzalloc(sizeof(*data), GFP_KERNEL);
1530 if (!data)
1531 return -ENOMEM;
1532 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
1533 if (!data->effect_ids) {
1534 kfree(data);
1535 return -ENOMEM;
1536 }
1537 data->hidpp = hidpp;
1538 data->feature_index = feature_index;
1539 data->version = version;
1540 data->slot_autocenter = 0;
1541 data->num_effects = num_slots;
1542 for (j = 0; j < num_slots; j++)
1543 data->effect_ids[j] = -1;
1544
1545 ff = dev->ff;
1546 ff->private = data;
1547
1548 ff->upload = hidpp_ff_upload_effect;
1549 ff->erase = hidpp_ff_erase_effect;
1550 ff->playback = hidpp_ff_playback;
1551 ff->set_gain = hidpp_ff_set_gain;
1552 ff->set_autocenter = hidpp_ff_set_autocenter;
1553 ff->destroy = hidpp_ff_destroy;
1554
1555
1556 /* reset all forces */
1557 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1558 HIDPP_FF_RESET_ALL, NULL, 0, &response);
1559
1560 /* Read current Range */
1561 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1562 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
1563 if (error)
1564 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
1565 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
1566
1567 /* Create sysfs interface */
1568 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
1569 if (error)
1570 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
1571
1572 /* Read the current gain values */
1573 error = hidpp_send_fap_command_sync(hidpp, feature_index,
1574 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
1575 if (error)
1576 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
1577 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
1578 /* ignore boost value at response.fap.params[2] */
1579
1580 /* init the hardware command queue */
1581 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
1582 atomic_set(&data->workqueue_size, 0);
1583
1584 /* initialize with zero autocenter to get wheel in usable state */
1585 hidpp_ff_set_autocenter(dev, 0);
1586
1587 hid_info(hid, "Force feeback support loaded (firmware release %d).\n", version);
1588
1589 return 0;
1590 }
1591
1592 static int hidpp_ff_deinit(struct hid_device *hid)
1593 {
1594 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1595 struct input_dev *dev = hidinput->input;
1596 struct hidpp_ff_private_data *data;
1597
1598 if (!dev) {
1599 hid_err(hid, "Struct input_dev not found!\n");
1600 return -EINVAL;
1601 }
1602
1603 hid_info(hid, "Unloading HID++ force feedback.\n");
1604 data = dev->ff->private;
1605 if (!data) {
1606 hid_err(hid, "Private data not found!\n");
1607 return -EINVAL;
1608 }
1609
1610 destroy_workqueue(data->wq);
1611 device_remove_file(&hid->dev, &dev_attr_range);
1612
1613 return 0;
1614 }
1615
1616
1617 /* ************************************************************************** */
1618 /* */
1619 /* Device Support */
1620 /* */
1621 /* ************************************************************************** */
1622
1623 /* -------------------------------------------------------------------------- */
1624 /* Touchpad HID++ devices */
1625 /* -------------------------------------------------------------------------- */
1626
1627 #define WTP_MANUAL_RESOLUTION 39
1628
1629 struct wtp_data {
1630 struct input_dev *input;
1631 u16 x_size, y_size;
1632 u8 finger_count;
1633 u8 mt_feature_index;
1634 u8 button_feature_index;
1635 u8 maxcontacts;
1636 bool flip_y;
1637 unsigned int resolution;
1638 };
1639
1640 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
1641 struct hid_field *field, struct hid_usage *usage,
1642 unsigned long **bit, int *max)
1643 {
1644 return -1;
1645 }
1646
1647 static void wtp_populate_input(struct hidpp_device *hidpp,
1648 struct input_dev *input_dev, bool origin_is_hid_core)
1649 {
1650 struct wtp_data *wd = hidpp->private_data;
1651
1652 __set_bit(EV_ABS, input_dev->evbit);
1653 __set_bit(EV_KEY, input_dev->evbit);
1654 __clear_bit(EV_REL, input_dev->evbit);
1655 __clear_bit(EV_LED, input_dev->evbit);
1656
1657 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
1658 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
1659 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
1660 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
1661
1662 /* Max pressure is not given by the devices, pick one */
1663 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
1664
1665 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
1666
1667 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
1668 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
1669 else
1670 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
1671
1672 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
1673 INPUT_MT_DROP_UNUSED);
1674
1675 wd->input = input_dev;
1676 }
1677
1678 static void wtp_touch_event(struct wtp_data *wd,
1679 struct hidpp_touchpad_raw_xy_finger *touch_report)
1680 {
1681 int slot;
1682
1683 if (!touch_report->finger_id || touch_report->contact_type)
1684 /* no actual data */
1685 return;
1686
1687 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id);
1688
1689 input_mt_slot(wd->input, slot);
1690 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER,
1691 touch_report->contact_status);
1692 if (touch_report->contact_status) {
1693 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X,
1694 touch_report->x);
1695 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y,
1696 wd->flip_y ? wd->y_size - touch_report->y :
1697 touch_report->y);
1698 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE,
1699 touch_report->area);
1700 }
1701 }
1702
1703 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
1704 struct hidpp_touchpad_raw_xy *raw)
1705 {
1706 struct wtp_data *wd = hidpp->private_data;
1707 int i;
1708
1709 for (i = 0; i < 2; i++)
1710 wtp_touch_event(wd, &(raw->fingers[i]));
1711
1712 if (raw->end_of_frame &&
1713 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
1714 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button);
1715
1716 if (raw->end_of_frame || raw->finger_count <= 2) {
1717 input_mt_sync_frame(wd->input);
1718 input_sync(wd->input);
1719 }
1720 }
1721
1722 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
1723 {
1724 struct wtp_data *wd = hidpp->private_data;
1725 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
1726 (data[7] >> 4) * (data[7] >> 4)) / 2;
1727 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
1728 (data[13] >> 4) * (data[13] >> 4)) / 2;
1729 struct hidpp_touchpad_raw_xy raw = {
1730 .timestamp = data[1],
1731 .fingers = {
1732 {
1733 .contact_type = 0,
1734 .contact_status = !!data[7],
1735 .x = get_unaligned_le16(&data[3]),
1736 .y = get_unaligned_le16(&data[5]),
1737 .z = c1_area,
1738 .area = c1_area,
1739 .finger_id = data[2],
1740 }, {
1741 .contact_type = 0,
1742 .contact_status = !!data[13],
1743 .x = get_unaligned_le16(&data[9]),
1744 .y = get_unaligned_le16(&data[11]),
1745 .z = c2_area,
1746 .area = c2_area,
1747 .finger_id = data[8],
1748 }
1749 },
1750 .finger_count = wd->maxcontacts,
1751 .spurious_flag = 0,
1752 .end_of_frame = (data[0] >> 7) == 0,
1753 .button = data[0] & 0x01,
1754 };
1755
1756 wtp_send_raw_xy_event(hidpp, &raw);
1757
1758 return 1;
1759 }
1760
1761 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
1762 {
1763 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1764 struct wtp_data *wd = hidpp->private_data;
1765 struct hidpp_report *report = (struct hidpp_report *)data;
1766 struct hidpp_touchpad_raw_xy raw;
1767
1768 if (!wd || !wd->input)
1769 return 1;
1770
1771 switch (data[0]) {
1772 case 0x02:
1773 if (size < 2) {
1774 hid_err(hdev, "Received HID report of bad size (%d)",
1775 size);
1776 return 1;
1777 }
1778 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
1779 input_event(wd->input, EV_KEY, BTN_LEFT,
1780 !!(data[1] & 0x01));
1781 input_event(wd->input, EV_KEY, BTN_RIGHT,
1782 !!(data[1] & 0x02));
1783 input_sync(wd->input);
1784 return 0;
1785 } else {
1786 if (size < 21)
1787 return 1;
1788 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
1789 }
1790 case REPORT_ID_HIDPP_LONG:
1791 /* size is already checked in hidpp_raw_event. */
1792 if ((report->fap.feature_index != wd->mt_feature_index) ||
1793 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
1794 return 1;
1795 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
1796
1797 wtp_send_raw_xy_event(hidpp, &raw);
1798 return 0;
1799 }
1800
1801 return 0;
1802 }
1803
1804 static int wtp_get_config(struct hidpp_device *hidpp)
1805 {
1806 struct wtp_data *wd = hidpp->private_data;
1807 struct hidpp_touchpad_raw_info raw_info = {0};
1808 u8 feature_type;
1809 int ret;
1810
1811 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
1812 &wd->mt_feature_index, &feature_type);
1813 if (ret)
1814 /* means that the device is not powered up */
1815 return ret;
1816
1817 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
1818 &raw_info);
1819 if (ret)
1820 return ret;
1821
1822 wd->x_size = raw_info.x_size;
1823 wd->y_size = raw_info.y_size;
1824 wd->maxcontacts = raw_info.maxcontacts;
1825 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
1826 wd->resolution = raw_info.res;
1827 if (!wd->resolution)
1828 wd->resolution = WTP_MANUAL_RESOLUTION;
1829
1830 return 0;
1831 }
1832
1833 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
1834 {
1835 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1836 struct wtp_data *wd;
1837
1838 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
1839 GFP_KERNEL);
1840 if (!wd)
1841 return -ENOMEM;
1842
1843 hidpp->private_data = wd;
1844
1845 return 0;
1846 };
1847
1848 static int wtp_connect(struct hid_device *hdev, bool connected)
1849 {
1850 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1851 struct wtp_data *wd = hidpp->private_data;
1852 int ret;
1853
1854 if (!wd->x_size) {
1855 ret = wtp_get_config(hidpp);
1856 if (ret) {
1857 hid_err(hdev, "Can not get wtp config: %d\n", ret);
1858 return ret;
1859 }
1860 }
1861
1862 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
1863 true, true);
1864 }
1865
1866 /* ------------------------------------------------------------------------- */
1867 /* Logitech M560 devices */
1868 /* ------------------------------------------------------------------------- */
1869
1870 /*
1871 * Logitech M560 protocol overview
1872 *
1873 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
1874 * the sides buttons are pressed, it sends some keyboard keys events
1875 * instead of buttons ones.
1876 * To complicate things further, the middle button keys sequence
1877 * is different from the odd press and the even press.
1878 *
1879 * forward button -> Super_R
1880 * backward button -> Super_L+'d' (press only)
1881 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
1882 * 2nd time: left-click (press only)
1883 * NB: press-only means that when the button is pressed, the
1884 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
1885 * together sequentially; instead when the button is released, no event is
1886 * generated !
1887 *
1888 * With the command
1889 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
1890 * the mouse reacts differently:
1891 * - it never sends a keyboard key event
1892 * - for the three mouse button it sends:
1893 * middle button press 11<xx>0a 3500af00...
1894 * side 1 button (forward) press 11<xx>0a 3500b000...
1895 * side 2 button (backward) press 11<xx>0a 3500ae00...
1896 * middle/side1/side2 button release 11<xx>0a 35000000...
1897 */
1898
1899 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
1900
1901 struct m560_private_data {
1902 struct input_dev *input;
1903 };
1904
1905 /* how buttons are mapped in the report */
1906 #define M560_MOUSE_BTN_LEFT 0x01
1907 #define M560_MOUSE_BTN_RIGHT 0x02
1908 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
1909 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
1910
1911 #define M560_SUB_ID 0x0a
1912 #define M560_BUTTON_MODE_REGISTER 0x35
1913
1914 static int m560_send_config_command(struct hid_device *hdev, bool connected)
1915 {
1916 struct hidpp_report response;
1917 struct hidpp_device *hidpp_dev;
1918
1919 hidpp_dev = hid_get_drvdata(hdev);
1920
1921 return hidpp_send_rap_command_sync(
1922 hidpp_dev,
1923 REPORT_ID_HIDPP_SHORT,
1924 M560_SUB_ID,
1925 M560_BUTTON_MODE_REGISTER,
1926 (u8 *)m560_config_parameter,
1927 sizeof(m560_config_parameter),
1928 &response
1929 );
1930 }
1931
1932 static int m560_allocate(struct hid_device *hdev)
1933 {
1934 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1935 struct m560_private_data *d;
1936
1937 d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data),
1938 GFP_KERNEL);
1939 if (!d)
1940 return -ENOMEM;
1941
1942 hidpp->private_data = d;
1943
1944 return 0;
1945 };
1946
1947 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
1948 {
1949 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
1950 struct m560_private_data *mydata = hidpp->private_data;
1951
1952 /* sanity check */
1953 if (!mydata || !mydata->input) {
1954 hid_err(hdev, "error in parameter\n");
1955 return -EINVAL;
1956 }
1957
1958 if (size < 7) {
1959 hid_err(hdev, "error in report\n");
1960 return 0;
1961 }
1962
1963 if (data[0] == REPORT_ID_HIDPP_LONG &&
1964 data[2] == M560_SUB_ID && data[6] == 0x00) {
1965 /*
1966 * m560 mouse report for middle, forward and backward button
1967 *
1968 * data[0] = 0x11
1969 * data[1] = device-id
1970 * data[2] = 0x0a
1971 * data[5] = 0xaf -> middle
1972 * 0xb0 -> forward
1973 * 0xae -> backward
1974 * 0x00 -> release all
1975 * data[6] = 0x00
1976 */
1977
1978 switch (data[5]) {
1979 case 0xaf:
1980 input_report_key(mydata->input, BTN_MIDDLE, 1);
1981 break;
1982 case 0xb0:
1983 input_report_key(mydata->input, BTN_FORWARD, 1);
1984 break;
1985 case 0xae:
1986 input_report_key(mydata->input, BTN_BACK, 1);
1987 break;
1988 case 0x00:
1989 input_report_key(mydata->input, BTN_BACK, 0);
1990 input_report_key(mydata->input, BTN_FORWARD, 0);
1991 input_report_key(mydata->input, BTN_MIDDLE, 0);
1992 break;
1993 default:
1994 hid_err(hdev, "error in report\n");
1995 return 0;
1996 }
1997 input_sync(mydata->input);
1998
1999 } else if (data[0] == 0x02) {
2000 /*
2001 * Logitech M560 mouse report
2002 *
2003 * data[0] = type (0x02)
2004 * data[1..2] = buttons
2005 * data[3..5] = xy
2006 * data[6] = wheel
2007 */
2008
2009 int v;
2010
2011 input_report_key(mydata->input, BTN_LEFT,
2012 !!(data[1] & M560_MOUSE_BTN_LEFT));
2013 input_report_key(mydata->input, BTN_RIGHT,
2014 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2015
2016 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT)
2017 input_report_rel(mydata->input, REL_HWHEEL, -1);
2018 else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT)
2019 input_report_rel(mydata->input, REL_HWHEEL, 1);
2020
2021 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2022 input_report_rel(mydata->input, REL_X, v);
2023
2024 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2025 input_report_rel(mydata->input, REL_Y, v);
2026
2027 v = hid_snto32(data[6], 8);
2028 input_report_rel(mydata->input, REL_WHEEL, v);
2029
2030 input_sync(mydata->input);
2031 }
2032
2033 return 1;
2034 }
2035
2036 static void m560_populate_input(struct hidpp_device *hidpp,
2037 struct input_dev *input_dev, bool origin_is_hid_core)
2038 {
2039 struct m560_private_data *mydata = hidpp->private_data;
2040
2041 mydata->input = input_dev;
2042
2043 __set_bit(EV_KEY, mydata->input->evbit);
2044 __set_bit(BTN_MIDDLE, mydata->input->keybit);
2045 __set_bit(BTN_RIGHT, mydata->input->keybit);
2046 __set_bit(BTN_LEFT, mydata->input->keybit);
2047 __set_bit(BTN_BACK, mydata->input->keybit);
2048 __set_bit(BTN_FORWARD, mydata->input->keybit);
2049
2050 __set_bit(EV_REL, mydata->input->evbit);
2051 __set_bit(REL_X, mydata->input->relbit);
2052 __set_bit(REL_Y, mydata->input->relbit);
2053 __set_bit(REL_WHEEL, mydata->input->relbit);
2054 __set_bit(REL_HWHEEL, mydata->input->relbit);
2055 }
2056
2057 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2058 struct hid_field *field, struct hid_usage *usage,
2059 unsigned long **bit, int *max)
2060 {
2061 return -1;
2062 }
2063
2064 /* ------------------------------------------------------------------------- */
2065 /* Logitech K400 devices */
2066 /* ------------------------------------------------------------------------- */
2067
2068 /*
2069 * The Logitech K400 keyboard has an embedded touchpad which is seen
2070 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2071 * tap-to-click but the setting is not remembered accross reset, annoying some
2072 * users.
2073 *
2074 * We can toggle this feature from the host by using the feature 0x6010:
2075 * Touchpad FW items
2076 */
2077
2078 struct k400_private_data {
2079 u8 feature_index;
2080 };
2081
2082 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2083 {
2084 struct k400_private_data *k400 = hidpp->private_data;
2085 struct hidpp_touchpad_fw_items items = {};
2086 int ret;
2087 u8 feature_type;
2088
2089 if (!k400->feature_index) {
2090 ret = hidpp_root_get_feature(hidpp,
2091 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2092 &k400->feature_index, &feature_type);
2093 if (ret)
2094 /* means that the device is not powered up */
2095 return ret;
2096 }
2097
2098 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2099 if (ret)
2100 return ret;
2101
2102 return 0;
2103 }
2104
2105 static int k400_allocate(struct hid_device *hdev)
2106 {
2107 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2108 struct k400_private_data *k400;
2109
2110 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2111 GFP_KERNEL);
2112 if (!k400)
2113 return -ENOMEM;
2114
2115 hidpp->private_data = k400;
2116
2117 return 0;
2118 };
2119
2120 static int k400_connect(struct hid_device *hdev, bool connected)
2121 {
2122 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2123
2124 if (!disable_tap_to_click)
2125 return 0;
2126
2127 return k400_disable_tap_to_click(hidpp);
2128 }
2129
2130 /* ------------------------------------------------------------------------- */
2131 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
2132 /* ------------------------------------------------------------------------- */
2133
2134 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2135
2136 static int g920_get_config(struct hidpp_device *hidpp)
2137 {
2138 u8 feature_type;
2139 u8 feature_index;
2140 int ret;
2141
2142 /* Find feature and store for later use */
2143 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2144 &feature_index, &feature_type);
2145 if (ret)
2146 return ret;
2147
2148 ret = hidpp_ff_init(hidpp, feature_index);
2149 if (ret)
2150 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2151 ret);
2152
2153 return 0;
2154 }
2155
2156 /* -------------------------------------------------------------------------- */
2157 /* Generic HID++ devices */
2158 /* -------------------------------------------------------------------------- */
2159
2160 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2161 struct hid_field *field, struct hid_usage *usage,
2162 unsigned long **bit, int *max)
2163 {
2164 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2165
2166 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2167 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
2168 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2169 field->application != HID_GD_MOUSE)
2170 return m560_input_mapping(hdev, hi, field, usage, bit, max);
2171
2172 return 0;
2173 }
2174
2175 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2176 struct hid_field *field, struct hid_usage *usage,
2177 unsigned long **bit, int *max)
2178 {
2179 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2180
2181 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
2182 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2183 if (usage->type == EV_ABS && (usage->code == ABS_X ||
2184 usage->code == ABS_Y || usage->code == ABS_Z ||
2185 usage->code == ABS_RZ)) {
2186 field->application = HID_GD_MULTIAXIS;
2187 }
2188 }
2189
2190 return 0;
2191 }
2192
2193
2194 static void hidpp_populate_input(struct hidpp_device *hidpp,
2195 struct input_dev *input, bool origin_is_hid_core)
2196 {
2197 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2198 wtp_populate_input(hidpp, input, origin_is_hid_core);
2199 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2200 m560_populate_input(hidpp, input, origin_is_hid_core);
2201 }
2202
2203 static int hidpp_input_configured(struct hid_device *hdev,
2204 struct hid_input *hidinput)
2205 {
2206 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2207 struct input_dev *input = hidinput->input;
2208
2209 hidpp_populate_input(hidpp, input, true);
2210
2211 return 0;
2212 }
2213
2214 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
2215 int size)
2216 {
2217 struct hidpp_report *question = hidpp->send_receive_buf;
2218 struct hidpp_report *answer = hidpp->send_receive_buf;
2219 struct hidpp_report *report = (struct hidpp_report *)data;
2220 int ret;
2221
2222 /*
2223 * If the mutex is locked then we have a pending answer from a
2224 * previously sent command.
2225 */
2226 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
2227 /*
2228 * Check for a correct hidpp20 answer or the corresponding
2229 * error
2230 */
2231 if (hidpp_match_answer(question, report) ||
2232 hidpp_match_error(question, report)) {
2233 *answer = *report;
2234 hidpp->answer_available = true;
2235 wake_up(&hidpp->wait);
2236 /*
2237 * This was an answer to a command that this driver sent
2238 * We return 1 to hid-core to avoid forwarding the
2239 * command upstream as it has been treated by the driver
2240 */
2241
2242 return 1;
2243 }
2244 }
2245
2246 if (unlikely(hidpp_report_is_connect_event(report))) {
2247 atomic_set(&hidpp->connected,
2248 !(report->rap.params[0] & (1 << 6)));
2249 if (schedule_work(&hidpp->work) == 0)
2250 dbg_hid("%s: connect event already queued\n", __func__);
2251 return 1;
2252 }
2253
2254 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2255 ret = hidpp20_battery_event(hidpp, data, size);
2256 if (ret != 0)
2257 return ret;
2258 }
2259
2260 return 0;
2261 }
2262
2263 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
2264 u8 *data, int size)
2265 {
2266 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2267 int ret = 0;
2268
2269 /* Generic HID++ processing. */
2270 switch (data[0]) {
2271 case REPORT_ID_HIDPP_VERY_LONG:
2272 if (size != HIDPP_REPORT_VERY_LONG_LENGTH) {
2273 hid_err(hdev, "received hid++ report of bad size (%d)",
2274 size);
2275 return 1;
2276 }
2277 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2278 break;
2279 case REPORT_ID_HIDPP_LONG:
2280 if (size != HIDPP_REPORT_LONG_LENGTH) {
2281 hid_err(hdev, "received hid++ report of bad size (%d)",
2282 size);
2283 return 1;
2284 }
2285 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2286 break;
2287 case REPORT_ID_HIDPP_SHORT:
2288 if (size != HIDPP_REPORT_SHORT_LENGTH) {
2289 hid_err(hdev, "received hid++ report of bad size (%d)",
2290 size);
2291 return 1;
2292 }
2293 ret = hidpp_raw_hidpp_event(hidpp, data, size);
2294 break;
2295 }
2296
2297 /* If no report is available for further processing, skip calling
2298 * raw_event of subclasses. */
2299 if (ret != 0)
2300 return ret;
2301
2302 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2303 return wtp_raw_event(hdev, data, size);
2304 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
2305 return m560_raw_event(hdev, data, size);
2306
2307 return 0;
2308 }
2309
2310 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
2311 {
2312 static atomic_t battery_no = ATOMIC_INIT(0);
2313 struct power_supply_config cfg = { .drv_data = hidpp };
2314 struct power_supply_desc *desc = &hidpp->battery.desc;
2315 struct hidpp_battery *battery;
2316 unsigned long n;
2317 int ret;
2318
2319 if (hidpp->battery.ps)
2320 return 0;
2321
2322 if (hidpp->protocol_major >= 2) {
2323 ret = hidpp20_query_battery_info(hidpp);
2324 if (ret)
2325 return ret;
2326 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
2327 } else {
2328 return -ENOENT;
2329 }
2330
2331 battery = &hidpp->battery;
2332
2333 n = atomic_inc_return(&battery_no) - 1;
2334 desc->properties = hidpp_battery_props;
2335 desc->num_properties = ARRAY_SIZE(hidpp_battery_props);
2336 desc->get_property = hidpp_battery_get_property;
2337 sprintf(battery->name, "hidpp_battery_%ld", n);
2338 desc->name = battery->name;
2339 desc->type = POWER_SUPPLY_TYPE_BATTERY;
2340 desc->use_for_apm = 0;
2341
2342 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
2343 &battery->desc,
2344 &cfg);
2345 if (IS_ERR(battery->ps))
2346 return PTR_ERR(battery->ps);
2347
2348 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
2349
2350 return ret;
2351 }
2352
2353 static void hidpp_overwrite_name(struct hid_device *hdev)
2354 {
2355 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2356 char *name;
2357
2358 if (hidpp->protocol_major < 2)
2359 return;
2360
2361 name = hidpp_get_device_name(hidpp);
2362
2363 if (!name) {
2364 hid_err(hdev, "unable to retrieve the name of the device");
2365 } else {
2366 dbg_hid("HID++: Got name: %s\n", name);
2367 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
2368 }
2369
2370 kfree(name);
2371 }
2372
2373 static int hidpp_input_open(struct input_dev *dev)
2374 {
2375 struct hid_device *hid = input_get_drvdata(dev);
2376
2377 return hid_hw_open(hid);
2378 }
2379
2380 static void hidpp_input_close(struct input_dev *dev)
2381 {
2382 struct hid_device *hid = input_get_drvdata(dev);
2383
2384 hid_hw_close(hid);
2385 }
2386
2387 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
2388 {
2389 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
2390 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2391
2392 if (!input_dev)
2393 return NULL;
2394
2395 input_set_drvdata(input_dev, hdev);
2396 input_dev->open = hidpp_input_open;
2397 input_dev->close = hidpp_input_close;
2398
2399 input_dev->name = hidpp->name;
2400 input_dev->phys = hdev->phys;
2401 input_dev->uniq = hdev->uniq;
2402 input_dev->id.bustype = hdev->bus;
2403 input_dev->id.vendor = hdev->vendor;
2404 input_dev->id.product = hdev->product;
2405 input_dev->id.version = hdev->version;
2406 input_dev->dev.parent = &hdev->dev;
2407
2408 return input_dev;
2409 }
2410
2411 static void hidpp_connect_event(struct hidpp_device *hidpp)
2412 {
2413 struct hid_device *hdev = hidpp->hid_dev;
2414 int ret = 0;
2415 bool connected = atomic_read(&hidpp->connected);
2416 struct input_dev *input;
2417 char *name, *devm_name;
2418
2419 if (!connected)
2420 return;
2421
2422 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2423 ret = wtp_connect(hdev, connected);
2424 if (ret)
2425 return;
2426 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2427 ret = m560_send_config_command(hdev, connected);
2428 if (ret)
2429 return;
2430 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2431 ret = k400_connect(hdev, connected);
2432 if (ret)
2433 return;
2434 }
2435
2436 /* the device is already connected, we can ask for its name and
2437 * protocol */
2438 if (!hidpp->protocol_major) {
2439 ret = !hidpp_is_connected(hidpp);
2440 if (ret) {
2441 hid_err(hdev, "Can not get the protocol version.\n");
2442 return;
2443 }
2444 hid_info(hdev, "HID++ %u.%u device connected.\n",
2445 hidpp->protocol_major, hidpp->protocol_minor);
2446 }
2447
2448 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
2449 name = hidpp_get_device_name(hidpp);
2450 if (!name) {
2451 hid_err(hdev,
2452 "unable to retrieve the name of the device");
2453 return;
2454 }
2455
2456 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name);
2457 kfree(name);
2458 if (!devm_name)
2459 return;
2460
2461 hidpp->name = devm_name;
2462 }
2463
2464 hidpp_initialize_battery(hidpp);
2465
2466 /* forward current battery state */
2467 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
2468 hidpp20_query_battery_info(hidpp);
2469 if (hidpp->battery.ps)
2470 power_supply_changed(hidpp->battery.ps);
2471 }
2472
2473 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
2474 /* if the input nodes are already created, we can stop now */
2475 return;
2476
2477 input = hidpp_allocate_input(hdev);
2478 if (!input) {
2479 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
2480 return;
2481 }
2482
2483 hidpp_populate_input(hidpp, input, false);
2484
2485 ret = input_register_device(input);
2486 if (ret)
2487 input_free_device(input);
2488
2489 hidpp->delayed_input = input;
2490 }
2491
2492 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
2493 {
2494 struct hidpp_device *hidpp;
2495 int ret;
2496 bool connected;
2497 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2498
2499 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device),
2500 GFP_KERNEL);
2501 if (!hidpp)
2502 return -ENOMEM;
2503
2504 hidpp->hid_dev = hdev;
2505 hidpp->name = hdev->name;
2506 hid_set_drvdata(hdev, hidpp);
2507
2508 hidpp->quirks = id->driver_data;
2509
2510 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
2511 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
2512
2513 if (disable_raw_mode) {
2514 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
2515 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
2516 }
2517
2518 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
2519 ret = wtp_allocate(hdev, id);
2520 if (ret)
2521 goto allocate_fail;
2522 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
2523 ret = m560_allocate(hdev);
2524 if (ret)
2525 goto allocate_fail;
2526 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
2527 ret = k400_allocate(hdev);
2528 if (ret)
2529 goto allocate_fail;
2530 }
2531
2532 INIT_WORK(&hidpp->work, delayed_work_cb);
2533 mutex_init(&hidpp->send_mutex);
2534 init_waitqueue_head(&hidpp->wait);
2535
2536 ret = hid_parse(hdev);
2537 if (ret) {
2538 hid_err(hdev, "%s:parse failed\n", __func__);
2539 goto hid_parse_fail;
2540 }
2541
2542 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
2543 connect_mask &= ~HID_CONNECT_HIDINPUT;
2544
2545 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2546 ret = hid_hw_start(hdev, connect_mask);
2547 if (ret) {
2548 hid_err(hdev, "hw start failed\n");
2549 goto hid_hw_start_fail;
2550 }
2551 ret = hid_hw_open(hdev);
2552 if (ret < 0) {
2553 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
2554 __func__, ret);
2555 hid_hw_stop(hdev);
2556 goto hid_hw_start_fail;
2557 }
2558 }
2559
2560
2561 /* Allow incoming packets */
2562 hid_device_io_start(hdev);
2563
2564 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
2565 hidpp_unifying_init(hidpp);
2566
2567 connected = hidpp_is_connected(hidpp);
2568 atomic_set(&hidpp->connected, connected);
2569 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
2570 if (!connected) {
2571 ret = -ENODEV;
2572 hid_err(hdev, "Device not connected");
2573 goto hid_hw_open_failed;
2574 }
2575
2576 hid_info(hdev, "HID++ %u.%u device connected.\n",
2577 hidpp->protocol_major, hidpp->protocol_minor);
2578
2579 hidpp_overwrite_name(hdev);
2580 }
2581
2582 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
2583 ret = wtp_get_config(hidpp);
2584 if (ret)
2585 goto hid_hw_open_failed;
2586 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2587 ret = g920_get_config(hidpp);
2588 if (ret)
2589 goto hid_hw_open_failed;
2590 }
2591
2592 /* Block incoming packets */
2593 hid_device_io_stop(hdev);
2594
2595 if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
2596 ret = hid_hw_start(hdev, connect_mask);
2597 if (ret) {
2598 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
2599 goto hid_hw_start_fail;
2600 }
2601 }
2602
2603 /* Allow incoming packets */
2604 hid_device_io_start(hdev);
2605
2606 hidpp_connect_event(hidpp);
2607
2608 return ret;
2609
2610 hid_hw_open_failed:
2611 hid_device_io_stop(hdev);
2612 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2613 hid_hw_close(hdev);
2614 hid_hw_stop(hdev);
2615 }
2616 hid_hw_start_fail:
2617 hid_parse_fail:
2618 cancel_work_sync(&hidpp->work);
2619 mutex_destroy(&hidpp->send_mutex);
2620 allocate_fail:
2621 hid_set_drvdata(hdev, NULL);
2622 return ret;
2623 }
2624
2625 static void hidpp_remove(struct hid_device *hdev)
2626 {
2627 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2628
2629 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
2630 hidpp_ff_deinit(hdev);
2631 hid_hw_close(hdev);
2632 }
2633 hid_hw_stop(hdev);
2634 cancel_work_sync(&hidpp->work);
2635 mutex_destroy(&hidpp->send_mutex);
2636 }
2637
2638 static const struct hid_device_id hidpp_devices[] = {
2639 { /* wireless touchpad */
2640 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2641 USB_VENDOR_ID_LOGITECH, 0x4011),
2642 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
2643 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
2644 { /* wireless touchpad T650 */
2645 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2646 USB_VENDOR_ID_LOGITECH, 0x4101),
2647 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
2648 { /* wireless touchpad T651 */
2649 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
2650 USB_DEVICE_ID_LOGITECH_T651),
2651 .driver_data = HIDPP_QUIRK_CLASS_WTP },
2652 { /* Mouse logitech M560 */
2653 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2654 USB_VENDOR_ID_LOGITECH, 0x402d),
2655 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 },
2656 { /* Keyboard logitech K400 */
2657 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2658 USB_VENDOR_ID_LOGITECH, 0x4024),
2659 .driver_data = HIDPP_QUIRK_CLASS_K400 },
2660
2661 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE,
2662 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)},
2663
2664 { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
2665 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
2666 {}
2667 };
2668
2669 MODULE_DEVICE_TABLE(hid, hidpp_devices);
2670
2671 static struct hid_driver hidpp_driver = {
2672 .name = "logitech-hidpp-device",
2673 .id_table = hidpp_devices,
2674 .probe = hidpp_probe,
2675 .remove = hidpp_remove,
2676 .raw_event = hidpp_raw_event,
2677 .input_configured = hidpp_input_configured,
2678 .input_mapping = hidpp_input_mapping,
2679 .input_mapped = hidpp_input_mapped,
2680 };
2681
2682 module_hid_driver(hidpp_driver);