]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - drivers/hid/hid-logitech-hidpp.c
Merge tag 'for-5.2-rc2-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[thirdparty/kernel/stable.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/sched/clock.h>
25 #include <linux/kfifo.h>
26 #include <linux/input/mt.h>
27 #include <linux/workqueue.h>
28 #include <linux/atomic.h>
29 #include <linux/fixp-arith.h>
30 #include <asm/unaligned.h>
31 #include "usbhid/usbhid.h"
32 #include "hid-ids.h"
33
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
36 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>");
37
38 static bool disable_raw_mode;
39 module_param(disable_raw_mode, bool, 0644);
40 MODULE_PARM_DESC(disable_raw_mode,
41 "Disable Raw mode reporting for touchpads and keep firmware gestures.");
42
43 static bool disable_tap_to_click;
44 module_param(disable_tap_to_click, bool, 0644);
45 MODULE_PARM_DESC(disable_tap_to_click,
46 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently).");
47
48 #define REPORT_ID_HIDPP_SHORT 0x10
49 #define REPORT_ID_HIDPP_LONG 0x11
50 #define REPORT_ID_HIDPP_VERY_LONG 0x12
51
52 #define HIDPP_REPORT_SHORT_LENGTH 7
53 #define HIDPP_REPORT_LONG_LENGTH 20
54 #define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64
55
56 #define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03
57 #define HIDPP_SUB_ID_ROLLER 0x05
58 #define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06
59
60 #define HIDPP_QUIRK_CLASS_WTP BIT(0)
61 #define HIDPP_QUIRK_CLASS_M560 BIT(1)
62 #define HIDPP_QUIRK_CLASS_K400 BIT(2)
63 #define HIDPP_QUIRK_CLASS_G920 BIT(3)
64 #define HIDPP_QUIRK_CLASS_K750 BIT(4)
65
66 /* bits 2..20 are reserved for classes */
67 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */
68 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22)
69 #define HIDPP_QUIRK_NO_HIDINPUT BIT(23)
70 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24)
71 #define HIDPP_QUIRK_UNIFYING BIT(25)
72 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(26)
73 #define HIDPP_QUIRK_HI_RES_SCROLL_X2120 BIT(27)
74 #define HIDPP_QUIRK_HI_RES_SCROLL_X2121 BIT(28)
75 #define HIDPP_QUIRK_HIDPP_WHEELS BIT(29)
76 #define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(30)
77 #define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(31)
78
79 /* These are just aliases for now */
80 #define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
81 #define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS
82
83 /* Convenience constant to check for any high-res support. */
84 #define HIDPP_QUIRK_HI_RES_SCROLL (HIDPP_QUIRK_HI_RES_SCROLL_1P0 | \
85 HIDPP_QUIRK_HI_RES_SCROLL_X2120 | \
86 HIDPP_QUIRK_HI_RES_SCROLL_X2121)
87
88 #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT
89
90 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0)
91 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1)
92 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2)
93 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3)
94
95 /*
96 * There are two hidpp protocols in use, the first version hidpp10 is known
97 * as register access protocol or RAP, the second version hidpp20 is known as
98 * feature access protocol or FAP
99 *
100 * Most older devices (including the Unifying usb receiver) use the RAP protocol
101 * where as most newer devices use the FAP protocol. Both protocols are
102 * compatible with the underlying transport, which could be usb, Unifiying, or
103 * bluetooth. The message lengths are defined by the hid vendor specific report
104 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and
105 * the HIDPP_LONG report type (total message length 20 bytes)
106 *
107 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG
108 * messages. The Unifying receiver itself responds to RAP messages (device index
109 * is 0xFF for the receiver), and all messages (short or long) with a device
110 * index between 1 and 6 are passed untouched to the corresponding paired
111 * Unifying device.
112 *
113 * The paired device can be RAP or FAP, it will receive the message untouched
114 * from the Unifiying receiver.
115 */
116
117 struct fap {
118 u8 feature_index;
119 u8 funcindex_clientid;
120 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
121 };
122
123 struct rap {
124 u8 sub_id;
125 u8 reg_address;
126 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U];
127 };
128
129 struct hidpp_report {
130 u8 report_id;
131 u8 device_index;
132 union {
133 struct fap fap;
134 struct rap rap;
135 u8 rawbytes[sizeof(struct fap)];
136 };
137 } __packed;
138
139 struct hidpp_battery {
140 u8 feature_index;
141 u8 solar_feature_index;
142 struct power_supply_desc desc;
143 struct power_supply *ps;
144 char name[64];
145 int status;
146 int capacity;
147 int level;
148 bool online;
149 };
150
151 /**
152 * struct hidpp_scroll_counter - Utility class for processing high-resolution
153 * scroll events.
154 * @dev: the input device for which events should be reported.
155 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event
156 * @remainder: counts the number of high-resolution units moved since the last
157 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should
158 * only be used by class methods.
159 * @direction: direction of last movement (1 or -1)
160 * @last_time: last event time, used to reset remainder after inactivity
161 */
162 struct hidpp_scroll_counter {
163 int wheel_multiplier;
164 int remainder;
165 int direction;
166 unsigned long long last_time;
167 };
168
169 struct hidpp_device {
170 struct hid_device *hid_dev;
171 struct input_dev *input;
172 struct mutex send_mutex;
173 void *send_receive_buf;
174 char *name; /* will never be NULL and should not be freed */
175 wait_queue_head_t wait;
176 int very_long_report_length;
177 bool answer_available;
178 u8 protocol_major;
179 u8 protocol_minor;
180
181 void *private_data;
182
183 struct work_struct work;
184 struct kfifo delayed_work_fifo;
185 atomic_t connected;
186 struct input_dev *delayed_input;
187
188 unsigned long quirks;
189 unsigned long capabilities;
190
191 struct hidpp_battery battery;
192 struct hidpp_scroll_counter vertical_wheel_counter;
193 };
194
195 /* HID++ 1.0 error codes */
196 #define HIDPP_ERROR 0x8f
197 #define HIDPP_ERROR_SUCCESS 0x00
198 #define HIDPP_ERROR_INVALID_SUBID 0x01
199 #define HIDPP_ERROR_INVALID_ADRESS 0x02
200 #define HIDPP_ERROR_INVALID_VALUE 0x03
201 #define HIDPP_ERROR_CONNECT_FAIL 0x04
202 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05
203 #define HIDPP_ERROR_ALREADY_EXISTS 0x06
204 #define HIDPP_ERROR_BUSY 0x07
205 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08
206 #define HIDPP_ERROR_RESOURCE_ERROR 0x09
207 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a
208 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b
209 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c
210 /* HID++ 2.0 error codes */
211 #define HIDPP20_ERROR 0xff
212
213 static void hidpp_connect_event(struct hidpp_device *hidpp_dev);
214
215 static int __hidpp_send_report(struct hid_device *hdev,
216 struct hidpp_report *hidpp_report)
217 {
218 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
219 int fields_count, ret;
220
221 switch (hidpp_report->report_id) {
222 case REPORT_ID_HIDPP_SHORT:
223 fields_count = HIDPP_REPORT_SHORT_LENGTH;
224 break;
225 case REPORT_ID_HIDPP_LONG:
226 fields_count = HIDPP_REPORT_LONG_LENGTH;
227 break;
228 case REPORT_ID_HIDPP_VERY_LONG:
229 fields_count = hidpp->very_long_report_length;
230 break;
231 default:
232 return -ENODEV;
233 }
234
235 /*
236 * set the device_index as the receiver, it will be overwritten by
237 * hid_hw_request if needed
238 */
239 hidpp_report->device_index = 0xff;
240
241 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) {
242 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count);
243 } else {
244 ret = hid_hw_raw_request(hdev, hidpp_report->report_id,
245 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT,
246 HID_REQ_SET_REPORT);
247 }
248
249 return ret == fields_count ? 0 : -1;
250 }
251
252 /**
253 * hidpp_send_message_sync() returns 0 in case of success, and something else
254 * in case of a failure.
255 * - If ' something else' is positive, that means that an error has been raised
256 * by the protocol itself.
257 * - If ' something else' is negative, that means that we had a classic error
258 * (-ENOMEM, -EPIPE, etc...)
259 */
260 static int hidpp_send_message_sync(struct hidpp_device *hidpp,
261 struct hidpp_report *message,
262 struct hidpp_report *response)
263 {
264 int ret;
265
266 mutex_lock(&hidpp->send_mutex);
267
268 hidpp->send_receive_buf = response;
269 hidpp->answer_available = false;
270
271 /*
272 * So that we can later validate the answer when it arrives
273 * in hidpp_raw_event
274 */
275 *response = *message;
276
277 ret = __hidpp_send_report(hidpp->hid_dev, message);
278
279 if (ret) {
280 dbg_hid("__hidpp_send_report returned err: %d\n", ret);
281 memset(response, 0, sizeof(struct hidpp_report));
282 goto exit;
283 }
284
285 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available,
286 5*HZ)) {
287 dbg_hid("%s:timeout waiting for response\n", __func__);
288 memset(response, 0, sizeof(struct hidpp_report));
289 ret = -ETIMEDOUT;
290 }
291
292 if (response->report_id == REPORT_ID_HIDPP_SHORT &&
293 response->rap.sub_id == HIDPP_ERROR) {
294 ret = response->rap.params[1];
295 dbg_hid("%s:got hidpp error %02X\n", __func__, ret);
296 goto exit;
297 }
298
299 if ((response->report_id == REPORT_ID_HIDPP_LONG ||
300 response->report_id == REPORT_ID_HIDPP_VERY_LONG) &&
301 response->fap.feature_index == HIDPP20_ERROR) {
302 ret = response->fap.params[1];
303 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret);
304 goto exit;
305 }
306
307 exit:
308 mutex_unlock(&hidpp->send_mutex);
309 return ret;
310
311 }
312
313 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp,
314 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count,
315 struct hidpp_report *response)
316 {
317 struct hidpp_report *message;
318 int ret;
319
320 if (param_count > sizeof(message->fap.params))
321 return -EINVAL;
322
323 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
324 if (!message)
325 return -ENOMEM;
326
327 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4))
328 message->report_id = REPORT_ID_HIDPP_VERY_LONG;
329 else
330 message->report_id = REPORT_ID_HIDPP_LONG;
331 message->fap.feature_index = feat_index;
332 message->fap.funcindex_clientid = funcindex_clientid;
333 memcpy(&message->fap.params, params, param_count);
334
335 ret = hidpp_send_message_sync(hidpp, message, response);
336 kfree(message);
337 return ret;
338 }
339
340 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev,
341 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count,
342 struct hidpp_report *response)
343 {
344 struct hidpp_report *message;
345 int ret, max_count;
346
347 switch (report_id) {
348 case REPORT_ID_HIDPP_SHORT:
349 max_count = HIDPP_REPORT_SHORT_LENGTH - 4;
350 break;
351 case REPORT_ID_HIDPP_LONG:
352 max_count = HIDPP_REPORT_LONG_LENGTH - 4;
353 break;
354 case REPORT_ID_HIDPP_VERY_LONG:
355 max_count = hidpp_dev->very_long_report_length - 4;
356 break;
357 default:
358 return -EINVAL;
359 }
360
361 if (param_count > max_count)
362 return -EINVAL;
363
364 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL);
365 if (!message)
366 return -ENOMEM;
367 message->report_id = report_id;
368 message->rap.sub_id = sub_id;
369 message->rap.reg_address = reg_address;
370 memcpy(&message->rap.params, params, param_count);
371
372 ret = hidpp_send_message_sync(hidpp_dev, message, response);
373 kfree(message);
374 return ret;
375 }
376
377 static void delayed_work_cb(struct work_struct *work)
378 {
379 struct hidpp_device *hidpp = container_of(work, struct hidpp_device,
380 work);
381 hidpp_connect_event(hidpp);
382 }
383
384 static inline bool hidpp_match_answer(struct hidpp_report *question,
385 struct hidpp_report *answer)
386 {
387 return (answer->fap.feature_index == question->fap.feature_index) &&
388 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid);
389 }
390
391 static inline bool hidpp_match_error(struct hidpp_report *question,
392 struct hidpp_report *answer)
393 {
394 return ((answer->rap.sub_id == HIDPP_ERROR) ||
395 (answer->fap.feature_index == HIDPP20_ERROR)) &&
396 (answer->fap.funcindex_clientid == question->fap.feature_index) &&
397 (answer->fap.params[0] == question->fap.funcindex_clientid);
398 }
399
400 static inline bool hidpp_report_is_connect_event(struct hidpp_report *report)
401 {
402 return (report->report_id == REPORT_ID_HIDPP_SHORT) &&
403 (report->rap.sub_id == 0x41);
404 }
405
406 /**
407 * hidpp_prefix_name() prefixes the current given name with "Logitech ".
408 */
409 static void hidpp_prefix_name(char **name, int name_length)
410 {
411 #define PREFIX_LENGTH 9 /* "Logitech " */
412
413 int new_length;
414 char *new_name;
415
416 if (name_length > PREFIX_LENGTH &&
417 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0)
418 /* The prefix has is already in the name */
419 return;
420
421 new_length = PREFIX_LENGTH + name_length;
422 new_name = kzalloc(new_length, GFP_KERNEL);
423 if (!new_name)
424 return;
425
426 snprintf(new_name, new_length, "Logitech %s", *name);
427
428 kfree(*name);
429
430 *name = new_name;
431 }
432
433 /**
434 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll
435 * events given a high-resolution wheel
436 * movement.
437 * @counter: a hid_scroll_counter struct describing the wheel.
438 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution
439 * units.
440 *
441 * Given a high-resolution movement, this function converts the movement into
442 * fractions of 120 and emits high-resolution scroll events for the input
443 * device. It also uses the multiplier from &struct hid_scroll_counter to
444 * emit low-resolution scroll events when appropriate for
445 * backwards-compatibility with userspace input libraries.
446 */
447 static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev,
448 struct hidpp_scroll_counter *counter,
449 int hi_res_value)
450 {
451 int low_res_value, remainder, direction;
452 unsigned long long now, previous;
453
454 hi_res_value = hi_res_value * 120/counter->wheel_multiplier;
455 input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value);
456
457 remainder = counter->remainder;
458 direction = hi_res_value > 0 ? 1 : -1;
459
460 now = sched_clock();
461 previous = counter->last_time;
462 counter->last_time = now;
463 /*
464 * Reset the remainder after a period of inactivity or when the
465 * direction changes. This prevents the REL_WHEEL emulation point
466 * from sliding for devices that don't always provide the same
467 * number of movements per detent.
468 */
469 if (now - previous > 1000000000 || direction != counter->direction)
470 remainder = 0;
471
472 counter->direction = direction;
473 remainder += hi_res_value;
474
475 /* Some wheels will rest 7/8ths of a detent from the previous detent
476 * after slow movement, so we want the threshold for low-res events to
477 * be in the middle between two detents (e.g. after 4/8ths) as
478 * opposed to on the detents themselves (8/8ths).
479 */
480 if (abs(remainder) >= 60) {
481 /* Add (or subtract) 1 because we want to trigger when the wheel
482 * is half-way to the next detent (i.e. scroll 1 detent after a
483 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement,
484 * etc.).
485 */
486 low_res_value = remainder / 120;
487 if (low_res_value == 0)
488 low_res_value = (hi_res_value > 0 ? 1 : -1);
489 input_report_rel(input_dev, REL_WHEEL, low_res_value);
490 remainder -= low_res_value * 120;
491 }
492 counter->remainder = remainder;
493 }
494
495 /* -------------------------------------------------------------------------- */
496 /* HIDP++ 1.0 commands */
497 /* -------------------------------------------------------------------------- */
498
499 #define HIDPP_SET_REGISTER 0x80
500 #define HIDPP_GET_REGISTER 0x81
501 #define HIDPP_SET_LONG_REGISTER 0x82
502 #define HIDPP_GET_LONG_REGISTER 0x83
503
504 /**
505 * hidpp10_set_register - Modify a HID++ 1.0 register.
506 * @hidpp_dev: the device to set the register on.
507 * @register_address: the address of the register to modify.
508 * @byte: the byte of the register to modify. Should be less than 3.
509 * @mask: mask of the bits to modify
510 * @value: new values for the bits in mask
511 * Return: 0 if successful, otherwise a negative error code.
512 */
513 static int hidpp10_set_register(struct hidpp_device *hidpp_dev,
514 u8 register_address, u8 byte, u8 mask, u8 value)
515 {
516 struct hidpp_report response;
517 int ret;
518 u8 params[3] = { 0 };
519
520 ret = hidpp_send_rap_command_sync(hidpp_dev,
521 REPORT_ID_HIDPP_SHORT,
522 HIDPP_GET_REGISTER,
523 register_address,
524 NULL, 0, &response);
525 if (ret)
526 return ret;
527
528 memcpy(params, response.rap.params, 3);
529
530 params[byte] &= ~mask;
531 params[byte] |= value & mask;
532
533 return hidpp_send_rap_command_sync(hidpp_dev,
534 REPORT_ID_HIDPP_SHORT,
535 HIDPP_SET_REGISTER,
536 register_address,
537 params, 3, &response);
538 }
539
540 #define HIDPP_REG_ENABLE_REPORTS 0x00
541 #define HIDPP_ENABLE_CONSUMER_REPORT BIT(0)
542 #define HIDPP_ENABLE_WHEEL_REPORT BIT(2)
543 #define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3)
544 #define HIDPP_ENABLE_BAT_REPORT BIT(4)
545 #define HIDPP_ENABLE_HWHEEL_REPORT BIT(5)
546
547 static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev)
548 {
549 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0,
550 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT);
551 }
552
553 #define HIDPP_REG_FEATURES 0x01
554 #define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1)
555 #define HIDPP_ENABLE_FAST_SCROLL BIT(6)
556
557 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */
558 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev)
559 {
560 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0,
561 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL);
562 }
563
564 #define HIDPP_REG_BATTERY_STATUS 0x07
565
566 static int hidpp10_battery_status_map_level(u8 param)
567 {
568 int level;
569
570 switch (param) {
571 case 1 ... 2:
572 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
573 break;
574 case 3 ... 4:
575 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
576 break;
577 case 5 ... 6:
578 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
579 break;
580 case 7:
581 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
582 break;
583 default:
584 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
585 }
586
587 return level;
588 }
589
590 static int hidpp10_battery_status_map_status(u8 param)
591 {
592 int status;
593
594 switch (param) {
595 case 0x00:
596 /* discharging (in use) */
597 status = POWER_SUPPLY_STATUS_DISCHARGING;
598 break;
599 case 0x21: /* (standard) charging */
600 case 0x24: /* fast charging */
601 case 0x25: /* slow charging */
602 status = POWER_SUPPLY_STATUS_CHARGING;
603 break;
604 case 0x26: /* topping charge */
605 case 0x22: /* charge complete */
606 status = POWER_SUPPLY_STATUS_FULL;
607 break;
608 case 0x20: /* unknown */
609 status = POWER_SUPPLY_STATUS_UNKNOWN;
610 break;
611 /*
612 * 0x01...0x1F = reserved (not charging)
613 * 0x23 = charging error
614 * 0x27..0xff = reserved
615 */
616 default:
617 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
618 break;
619 }
620
621 return status;
622 }
623
624 static int hidpp10_query_battery_status(struct hidpp_device *hidpp)
625 {
626 struct hidpp_report response;
627 int ret, status;
628
629 ret = hidpp_send_rap_command_sync(hidpp,
630 REPORT_ID_HIDPP_SHORT,
631 HIDPP_GET_REGISTER,
632 HIDPP_REG_BATTERY_STATUS,
633 NULL, 0, &response);
634 if (ret)
635 return ret;
636
637 hidpp->battery.level =
638 hidpp10_battery_status_map_level(response.rap.params[0]);
639 status = hidpp10_battery_status_map_status(response.rap.params[1]);
640 hidpp->battery.status = status;
641 /* the capacity is only available when discharging or full */
642 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
643 status == POWER_SUPPLY_STATUS_FULL;
644
645 return 0;
646 }
647
648 #define HIDPP_REG_BATTERY_MILEAGE 0x0D
649
650 static int hidpp10_battery_mileage_map_status(u8 param)
651 {
652 int status;
653
654 switch (param >> 6) {
655 case 0x00:
656 /* discharging (in use) */
657 status = POWER_SUPPLY_STATUS_DISCHARGING;
658 break;
659 case 0x01: /* charging */
660 status = POWER_SUPPLY_STATUS_CHARGING;
661 break;
662 case 0x02: /* charge complete */
663 status = POWER_SUPPLY_STATUS_FULL;
664 break;
665 /*
666 * 0x03 = charging error
667 */
668 default:
669 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
670 break;
671 }
672
673 return status;
674 }
675
676 static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp)
677 {
678 struct hidpp_report response;
679 int ret, status;
680
681 ret = hidpp_send_rap_command_sync(hidpp,
682 REPORT_ID_HIDPP_SHORT,
683 HIDPP_GET_REGISTER,
684 HIDPP_REG_BATTERY_MILEAGE,
685 NULL, 0, &response);
686 if (ret)
687 return ret;
688
689 hidpp->battery.capacity = response.rap.params[0];
690 status = hidpp10_battery_mileage_map_status(response.rap.params[2]);
691 hidpp->battery.status = status;
692 /* the capacity is only available when discharging or full */
693 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
694 status == POWER_SUPPLY_STATUS_FULL;
695
696 return 0;
697 }
698
699 static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size)
700 {
701 struct hidpp_report *report = (struct hidpp_report *)data;
702 int status, capacity, level;
703 bool changed;
704
705 if (report->report_id != REPORT_ID_HIDPP_SHORT)
706 return 0;
707
708 switch (report->rap.sub_id) {
709 case HIDPP_REG_BATTERY_STATUS:
710 capacity = hidpp->battery.capacity;
711 level = hidpp10_battery_status_map_level(report->rawbytes[1]);
712 status = hidpp10_battery_status_map_status(report->rawbytes[2]);
713 break;
714 case HIDPP_REG_BATTERY_MILEAGE:
715 capacity = report->rap.params[0];
716 level = hidpp->battery.level;
717 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]);
718 break;
719 default:
720 return 0;
721 }
722
723 changed = capacity != hidpp->battery.capacity ||
724 level != hidpp->battery.level ||
725 status != hidpp->battery.status;
726
727 /* the capacity is only available when discharging or full */
728 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
729 status == POWER_SUPPLY_STATUS_FULL;
730
731 if (changed) {
732 hidpp->battery.level = level;
733 hidpp->battery.status = status;
734 if (hidpp->battery.ps)
735 power_supply_changed(hidpp->battery.ps);
736 }
737
738 return 0;
739 }
740
741 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
742 #define HIDPP_EXTENDED_PAIRING 0x30
743 #define HIDPP_DEVICE_NAME 0x40
744
745 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev)
746 {
747 struct hidpp_report response;
748 int ret;
749 u8 params[1] = { HIDPP_DEVICE_NAME };
750 char *name;
751 int len;
752
753 ret = hidpp_send_rap_command_sync(hidpp_dev,
754 REPORT_ID_HIDPP_SHORT,
755 HIDPP_GET_LONG_REGISTER,
756 HIDPP_REG_PAIRING_INFORMATION,
757 params, 1, &response);
758 if (ret)
759 return NULL;
760
761 len = response.rap.params[1];
762
763 if (2 + len > sizeof(response.rap.params))
764 return NULL;
765
766 if (len < 4) /* logitech devices are usually at least Xddd */
767 return NULL;
768
769 name = kzalloc(len + 1, GFP_KERNEL);
770 if (!name)
771 return NULL;
772
773 memcpy(name, &response.rap.params[2], len);
774
775 /* include the terminating '\0' */
776 hidpp_prefix_name(&name, len + 1);
777
778 return name;
779 }
780
781 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial)
782 {
783 struct hidpp_report response;
784 int ret;
785 u8 params[1] = { HIDPP_EXTENDED_PAIRING };
786
787 ret = hidpp_send_rap_command_sync(hidpp,
788 REPORT_ID_HIDPP_SHORT,
789 HIDPP_GET_LONG_REGISTER,
790 HIDPP_REG_PAIRING_INFORMATION,
791 params, 1, &response);
792 if (ret)
793 return ret;
794
795 /*
796 * We don't care about LE or BE, we will output it as a string
797 * with %4phD, so we need to keep the order.
798 */
799 *serial = *((u32 *)&response.rap.params[1]);
800 return 0;
801 }
802
803 static int hidpp_unifying_init(struct hidpp_device *hidpp)
804 {
805 struct hid_device *hdev = hidpp->hid_dev;
806 const char *name;
807 u32 serial;
808 int ret;
809
810 ret = hidpp_unifying_get_serial(hidpp, &serial);
811 if (ret)
812 return ret;
813
814 snprintf(hdev->uniq, sizeof(hdev->uniq), "%04x-%4phD",
815 hdev->product, &serial);
816 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq);
817
818 name = hidpp_unifying_get_name(hidpp);
819 if (!name)
820 return -EIO;
821
822 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
823 dbg_hid("HID++ Unifying: Got name: %s\n", name);
824
825 kfree(name);
826 return 0;
827 }
828
829 /* -------------------------------------------------------------------------- */
830 /* 0x0000: Root */
831 /* -------------------------------------------------------------------------- */
832
833 #define HIDPP_PAGE_ROOT 0x0000
834 #define HIDPP_PAGE_ROOT_IDX 0x00
835
836 #define CMD_ROOT_GET_FEATURE 0x01
837 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11
838
839 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature,
840 u8 *feature_index, u8 *feature_type)
841 {
842 struct hidpp_report response;
843 int ret;
844 u8 params[2] = { feature >> 8, feature & 0x00FF };
845
846 ret = hidpp_send_fap_command_sync(hidpp,
847 HIDPP_PAGE_ROOT_IDX,
848 CMD_ROOT_GET_FEATURE,
849 params, 2, &response);
850 if (ret)
851 return ret;
852
853 if (response.fap.params[0] == 0)
854 return -ENOENT;
855
856 *feature_index = response.fap.params[0];
857 *feature_type = response.fap.params[1];
858
859 return ret;
860 }
861
862 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp)
863 {
864 const u8 ping_byte = 0x5a;
865 u8 ping_data[3] = { 0, 0, ping_byte };
866 struct hidpp_report response;
867 int ret;
868
869 ret = hidpp_send_rap_command_sync(hidpp,
870 REPORT_ID_HIDPP_SHORT,
871 HIDPP_PAGE_ROOT_IDX,
872 CMD_ROOT_GET_PROTOCOL_VERSION,
873 ping_data, sizeof(ping_data), &response);
874
875 if (ret == HIDPP_ERROR_INVALID_SUBID) {
876 hidpp->protocol_major = 1;
877 hidpp->protocol_minor = 0;
878 goto print_version;
879 }
880
881 /* the device might not be connected */
882 if (ret == HIDPP_ERROR_RESOURCE_ERROR)
883 return -EIO;
884
885 if (ret > 0) {
886 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
887 __func__, ret);
888 return -EPROTO;
889 }
890 if (ret)
891 return ret;
892
893 if (response.rap.params[2] != ping_byte) {
894 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n",
895 __func__, response.rap.params[2], ping_byte);
896 return -EPROTO;
897 }
898
899 hidpp->protocol_major = response.rap.params[0];
900 hidpp->protocol_minor = response.rap.params[1];
901
902 print_version:
903 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n",
904 hidpp->protocol_major, hidpp->protocol_minor);
905 return 0;
906 }
907
908 /* -------------------------------------------------------------------------- */
909 /* 0x0005: GetDeviceNameType */
910 /* -------------------------------------------------------------------------- */
911
912 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005
913
914 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01
915 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11
916 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21
917
918 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp,
919 u8 feature_index, u8 *nameLength)
920 {
921 struct hidpp_report response;
922 int ret;
923
924 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
925 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response);
926
927 if (ret > 0) {
928 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
929 __func__, ret);
930 return -EPROTO;
931 }
932 if (ret)
933 return ret;
934
935 *nameLength = response.fap.params[0];
936
937 return ret;
938 }
939
940 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp,
941 u8 feature_index, u8 char_index, char *device_name, int len_buf)
942 {
943 struct hidpp_report response;
944 int ret, i;
945 int count;
946
947 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
948 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1,
949 &response);
950
951 if (ret > 0) {
952 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
953 __func__, ret);
954 return -EPROTO;
955 }
956 if (ret)
957 return ret;
958
959 switch (response.report_id) {
960 case REPORT_ID_HIDPP_VERY_LONG:
961 count = hidpp->very_long_report_length - 4;
962 break;
963 case REPORT_ID_HIDPP_LONG:
964 count = HIDPP_REPORT_LONG_LENGTH - 4;
965 break;
966 case REPORT_ID_HIDPP_SHORT:
967 count = HIDPP_REPORT_SHORT_LENGTH - 4;
968 break;
969 default:
970 return -EPROTO;
971 }
972
973 if (len_buf < count)
974 count = len_buf;
975
976 for (i = 0; i < count; i++)
977 device_name[i] = response.fap.params[i];
978
979 return count;
980 }
981
982 static char *hidpp_get_device_name(struct hidpp_device *hidpp)
983 {
984 u8 feature_type;
985 u8 feature_index;
986 u8 __name_length;
987 char *name;
988 unsigned index = 0;
989 int ret;
990
991 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE,
992 &feature_index, &feature_type);
993 if (ret)
994 return NULL;
995
996 ret = hidpp_devicenametype_get_count(hidpp, feature_index,
997 &__name_length);
998 if (ret)
999 return NULL;
1000
1001 name = kzalloc(__name_length + 1, GFP_KERNEL);
1002 if (!name)
1003 return NULL;
1004
1005 while (index < __name_length) {
1006 ret = hidpp_devicenametype_get_device_name(hidpp,
1007 feature_index, index, name + index,
1008 __name_length - index);
1009 if (ret <= 0) {
1010 kfree(name);
1011 return NULL;
1012 }
1013 index += ret;
1014 }
1015
1016 /* include the terminating '\0' */
1017 hidpp_prefix_name(&name, __name_length + 1);
1018
1019 return name;
1020 }
1021
1022 /* -------------------------------------------------------------------------- */
1023 /* 0x1000: Battery level status */
1024 /* -------------------------------------------------------------------------- */
1025
1026 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000
1027
1028 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00
1029 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10
1030
1031 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00
1032
1033 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0)
1034 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1)
1035 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2)
1036
1037 static int hidpp_map_battery_level(int capacity)
1038 {
1039 if (capacity < 11)
1040 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
1041 /*
1042 * The spec says this should be < 31 but some devices report 30
1043 * with brand new batteries and Windows reports 30 as "Good".
1044 */
1045 else if (capacity < 30)
1046 return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
1047 else if (capacity < 81)
1048 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
1049 return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1050 }
1051
1052 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity,
1053 int *next_capacity,
1054 int *level)
1055 {
1056 int status;
1057
1058 *capacity = data[0];
1059 *next_capacity = data[1];
1060 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
1061
1062 /* When discharging, we can rely on the device reported capacity.
1063 * For all other states the device reports 0 (unknown).
1064 */
1065 switch (data[2]) {
1066 case 0: /* discharging (in use) */
1067 status = POWER_SUPPLY_STATUS_DISCHARGING;
1068 *level = hidpp_map_battery_level(*capacity);
1069 break;
1070 case 1: /* recharging */
1071 status = POWER_SUPPLY_STATUS_CHARGING;
1072 break;
1073 case 2: /* charge in final stage */
1074 status = POWER_SUPPLY_STATUS_CHARGING;
1075 break;
1076 case 3: /* charge complete */
1077 status = POWER_SUPPLY_STATUS_FULL;
1078 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
1079 *capacity = 100;
1080 break;
1081 case 4: /* recharging below optimal speed */
1082 status = POWER_SUPPLY_STATUS_CHARGING;
1083 break;
1084 /* 5 = invalid battery type
1085 6 = thermal error
1086 7 = other charging error */
1087 default:
1088 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1089 break;
1090 }
1091
1092 return status;
1093 }
1094
1095 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp,
1096 u8 feature_index,
1097 int *status,
1098 int *capacity,
1099 int *next_capacity,
1100 int *level)
1101 {
1102 struct hidpp_report response;
1103 int ret;
1104 u8 *params = (u8 *)response.fap.params;
1105
1106 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1107 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS,
1108 NULL, 0, &response);
1109 if (ret > 0) {
1110 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1111 __func__, ret);
1112 return -EPROTO;
1113 }
1114 if (ret)
1115 return ret;
1116
1117 *status = hidpp20_batterylevel_map_status_capacity(params, capacity,
1118 next_capacity,
1119 level);
1120
1121 return 0;
1122 }
1123
1124 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp,
1125 u8 feature_index)
1126 {
1127 struct hidpp_report response;
1128 int ret;
1129 u8 *params = (u8 *)response.fap.params;
1130 unsigned int level_count, flags;
1131
1132 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1133 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY,
1134 NULL, 0, &response);
1135 if (ret > 0) {
1136 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1137 __func__, ret);
1138 return -EPROTO;
1139 }
1140 if (ret)
1141 return ret;
1142
1143 level_count = params[0];
1144 flags = params[1];
1145
1146 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE))
1147 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
1148 else
1149 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1150
1151 return 0;
1152 }
1153
1154 static int hidpp20_query_battery_info(struct hidpp_device *hidpp)
1155 {
1156 u8 feature_type;
1157 int ret;
1158 int status, capacity, next_capacity, level;
1159
1160 if (hidpp->battery.feature_index == 0xff) {
1161 ret = hidpp_root_get_feature(hidpp,
1162 HIDPP_PAGE_BATTERY_LEVEL_STATUS,
1163 &hidpp->battery.feature_index,
1164 &feature_type);
1165 if (ret)
1166 return ret;
1167 }
1168
1169 ret = hidpp20_batterylevel_get_battery_capacity(hidpp,
1170 hidpp->battery.feature_index,
1171 &status, &capacity,
1172 &next_capacity, &level);
1173 if (ret)
1174 return ret;
1175
1176 ret = hidpp20_batterylevel_get_battery_info(hidpp,
1177 hidpp->battery.feature_index);
1178 if (ret)
1179 return ret;
1180
1181 hidpp->battery.status = status;
1182 hidpp->battery.capacity = capacity;
1183 hidpp->battery.level = level;
1184 /* the capacity is only available when discharging or full */
1185 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1186 status == POWER_SUPPLY_STATUS_FULL;
1187
1188 return 0;
1189 }
1190
1191 static int hidpp20_battery_event(struct hidpp_device *hidpp,
1192 u8 *data, int size)
1193 {
1194 struct hidpp_report *report = (struct hidpp_report *)data;
1195 int status, capacity, next_capacity, level;
1196 bool changed;
1197
1198 if (report->fap.feature_index != hidpp->battery.feature_index ||
1199 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST)
1200 return 0;
1201
1202 status = hidpp20_batterylevel_map_status_capacity(report->fap.params,
1203 &capacity,
1204 &next_capacity,
1205 &level);
1206
1207 /* the capacity is only available when discharging or full */
1208 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING ||
1209 status == POWER_SUPPLY_STATUS_FULL;
1210
1211 changed = capacity != hidpp->battery.capacity ||
1212 level != hidpp->battery.level ||
1213 status != hidpp->battery.status;
1214
1215 if (changed) {
1216 hidpp->battery.level = level;
1217 hidpp->battery.capacity = capacity;
1218 hidpp->battery.status = status;
1219 if (hidpp->battery.ps)
1220 power_supply_changed(hidpp->battery.ps);
1221 }
1222
1223 return 0;
1224 }
1225
1226 static enum power_supply_property hidpp_battery_props[] = {
1227 POWER_SUPPLY_PROP_ONLINE,
1228 POWER_SUPPLY_PROP_STATUS,
1229 POWER_SUPPLY_PROP_SCOPE,
1230 POWER_SUPPLY_PROP_MODEL_NAME,
1231 POWER_SUPPLY_PROP_MANUFACTURER,
1232 POWER_SUPPLY_PROP_SERIAL_NUMBER,
1233 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */
1234 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */
1235 };
1236
1237 static int hidpp_battery_get_property(struct power_supply *psy,
1238 enum power_supply_property psp,
1239 union power_supply_propval *val)
1240 {
1241 struct hidpp_device *hidpp = power_supply_get_drvdata(psy);
1242 int ret = 0;
1243
1244 switch(psp) {
1245 case POWER_SUPPLY_PROP_STATUS:
1246 val->intval = hidpp->battery.status;
1247 break;
1248 case POWER_SUPPLY_PROP_CAPACITY:
1249 val->intval = hidpp->battery.capacity;
1250 break;
1251 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
1252 val->intval = hidpp->battery.level;
1253 break;
1254 case POWER_SUPPLY_PROP_SCOPE:
1255 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1256 break;
1257 case POWER_SUPPLY_PROP_ONLINE:
1258 val->intval = hidpp->battery.online;
1259 break;
1260 case POWER_SUPPLY_PROP_MODEL_NAME:
1261 if (!strncmp(hidpp->name, "Logitech ", 9))
1262 val->strval = hidpp->name + 9;
1263 else
1264 val->strval = hidpp->name;
1265 break;
1266 case POWER_SUPPLY_PROP_MANUFACTURER:
1267 val->strval = "Logitech";
1268 break;
1269 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
1270 val->strval = hidpp->hid_dev->uniq;
1271 break;
1272 default:
1273 ret = -EINVAL;
1274 break;
1275 }
1276
1277 return ret;
1278 }
1279
1280 /* -------------------------------------------------------------------------- */
1281 /* 0x2120: Hi-resolution scrolling */
1282 /* -------------------------------------------------------------------------- */
1283
1284 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120
1285
1286 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10
1287
1288 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp,
1289 bool enabled, u8 *multiplier)
1290 {
1291 u8 feature_index;
1292 u8 feature_type;
1293 int ret;
1294 u8 params[1];
1295 struct hidpp_report response;
1296
1297 ret = hidpp_root_get_feature(hidpp,
1298 HIDPP_PAGE_HI_RESOLUTION_SCROLLING,
1299 &feature_index,
1300 &feature_type);
1301 if (ret)
1302 return ret;
1303
1304 params[0] = enabled ? BIT(0) : 0;
1305 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1306 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE,
1307 params, sizeof(params), &response);
1308 if (ret)
1309 return ret;
1310 *multiplier = response.fap.params[1];
1311 return 0;
1312 }
1313
1314 /* -------------------------------------------------------------------------- */
1315 /* 0x2121: HiRes Wheel */
1316 /* -------------------------------------------------------------------------- */
1317
1318 #define HIDPP_PAGE_HIRES_WHEEL 0x2121
1319
1320 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00
1321 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20
1322
1323 static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp,
1324 u8 *multiplier)
1325 {
1326 u8 feature_index;
1327 u8 feature_type;
1328 int ret;
1329 struct hidpp_report response;
1330
1331 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1332 &feature_index, &feature_type);
1333 if (ret)
1334 goto return_default;
1335
1336 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1337 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY,
1338 NULL, 0, &response);
1339 if (ret)
1340 goto return_default;
1341
1342 *multiplier = response.fap.params[0];
1343 return 0;
1344 return_default:
1345 hid_warn(hidpp->hid_dev,
1346 "Couldn't get wheel multiplier (error %d)\n", ret);
1347 return ret;
1348 }
1349
1350 static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert,
1351 bool high_resolution, bool use_hidpp)
1352 {
1353 u8 feature_index;
1354 u8 feature_type;
1355 int ret;
1356 u8 params[1];
1357 struct hidpp_report response;
1358
1359 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL,
1360 &feature_index, &feature_type);
1361 if (ret)
1362 return ret;
1363
1364 params[0] = (invert ? BIT(2) : 0) |
1365 (high_resolution ? BIT(1) : 0) |
1366 (use_hidpp ? BIT(0) : 0);
1367
1368 return hidpp_send_fap_command_sync(hidpp, feature_index,
1369 CMD_HIRES_WHEEL_SET_WHEEL_MODE,
1370 params, sizeof(params), &response);
1371 }
1372
1373 /* -------------------------------------------------------------------------- */
1374 /* 0x4301: Solar Keyboard */
1375 /* -------------------------------------------------------------------------- */
1376
1377 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301
1378
1379 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00
1380
1381 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00
1382 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10
1383 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20
1384
1385 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp)
1386 {
1387 struct hidpp_report response;
1388 u8 params[2] = { 1, 1 };
1389 u8 feature_type;
1390 int ret;
1391
1392 if (hidpp->battery.feature_index == 0xff) {
1393 ret = hidpp_root_get_feature(hidpp,
1394 HIDPP_PAGE_SOLAR_KEYBOARD,
1395 &hidpp->battery.solar_feature_index,
1396 &feature_type);
1397 if (ret)
1398 return ret;
1399 }
1400
1401 ret = hidpp_send_fap_command_sync(hidpp,
1402 hidpp->battery.solar_feature_index,
1403 CMD_SOLAR_SET_LIGHT_MEASURE,
1404 params, 2, &response);
1405 if (ret > 0) {
1406 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1407 __func__, ret);
1408 return -EPROTO;
1409 }
1410 if (ret)
1411 return ret;
1412
1413 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
1414
1415 return 0;
1416 }
1417
1418 static int hidpp_solar_battery_event(struct hidpp_device *hidpp,
1419 u8 *data, int size)
1420 {
1421 struct hidpp_report *report = (struct hidpp_report *)data;
1422 int capacity, lux, status;
1423 u8 function;
1424
1425 function = report->fap.funcindex_clientid;
1426
1427
1428 if (report->fap.feature_index != hidpp->battery.solar_feature_index ||
1429 !(function == EVENT_SOLAR_BATTERY_BROADCAST ||
1430 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE ||
1431 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON))
1432 return 0;
1433
1434 capacity = report->fap.params[0];
1435
1436 switch (function) {
1437 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE:
1438 lux = (report->fap.params[1] << 8) | report->fap.params[2];
1439 if (lux > 200)
1440 status = POWER_SUPPLY_STATUS_CHARGING;
1441 else
1442 status = POWER_SUPPLY_STATUS_DISCHARGING;
1443 break;
1444 case EVENT_SOLAR_CHECK_LIGHT_BUTTON:
1445 default:
1446 if (capacity < hidpp->battery.capacity)
1447 status = POWER_SUPPLY_STATUS_DISCHARGING;
1448 else
1449 status = POWER_SUPPLY_STATUS_CHARGING;
1450
1451 }
1452
1453 if (capacity == 100)
1454 status = POWER_SUPPLY_STATUS_FULL;
1455
1456 hidpp->battery.online = true;
1457 if (capacity != hidpp->battery.capacity ||
1458 status != hidpp->battery.status) {
1459 hidpp->battery.capacity = capacity;
1460 hidpp->battery.status = status;
1461 if (hidpp->battery.ps)
1462 power_supply_changed(hidpp->battery.ps);
1463 }
1464
1465 return 0;
1466 }
1467
1468 /* -------------------------------------------------------------------------- */
1469 /* 0x6010: Touchpad FW items */
1470 /* -------------------------------------------------------------------------- */
1471
1472 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010
1473
1474 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10
1475
1476 struct hidpp_touchpad_fw_items {
1477 uint8_t presence;
1478 uint8_t desired_state;
1479 uint8_t state;
1480 uint8_t persistent;
1481 };
1482
1483 /**
1484 * send a set state command to the device by reading the current items->state
1485 * field. items is then filled with the current state.
1486 */
1487 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp,
1488 u8 feature_index,
1489 struct hidpp_touchpad_fw_items *items)
1490 {
1491 struct hidpp_report response;
1492 int ret;
1493 u8 *params = (u8 *)response.fap.params;
1494
1495 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1496 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response);
1497
1498 if (ret > 0) {
1499 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1500 __func__, ret);
1501 return -EPROTO;
1502 }
1503 if (ret)
1504 return ret;
1505
1506 items->presence = params[0];
1507 items->desired_state = params[1];
1508 items->state = params[2];
1509 items->persistent = params[3];
1510
1511 return 0;
1512 }
1513
1514 /* -------------------------------------------------------------------------- */
1515 /* 0x6100: TouchPadRawXY */
1516 /* -------------------------------------------------------------------------- */
1517
1518 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100
1519
1520 #define CMD_TOUCHPAD_GET_RAW_INFO 0x01
1521 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21
1522
1523 #define EVENT_TOUCHPAD_RAW_XY 0x00
1524
1525 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01
1526 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03
1527
1528 struct hidpp_touchpad_raw_info {
1529 u16 x_size;
1530 u16 y_size;
1531 u8 z_range;
1532 u8 area_range;
1533 u8 timestamp_unit;
1534 u8 maxcontacts;
1535 u8 origin;
1536 u16 res;
1537 };
1538
1539 struct hidpp_touchpad_raw_xy_finger {
1540 u8 contact_type;
1541 u8 contact_status;
1542 u16 x;
1543 u16 y;
1544 u8 z;
1545 u8 area;
1546 u8 finger_id;
1547 };
1548
1549 struct hidpp_touchpad_raw_xy {
1550 u16 timestamp;
1551 struct hidpp_touchpad_raw_xy_finger fingers[2];
1552 u8 spurious_flag;
1553 u8 end_of_frame;
1554 u8 finger_count;
1555 u8 button;
1556 };
1557
1558 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp,
1559 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info)
1560 {
1561 struct hidpp_report response;
1562 int ret;
1563 u8 *params = (u8 *)response.fap.params;
1564
1565 ret = hidpp_send_fap_command_sync(hidpp, feature_index,
1566 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response);
1567
1568 if (ret > 0) {
1569 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
1570 __func__, ret);
1571 return -EPROTO;
1572 }
1573 if (ret)
1574 return ret;
1575
1576 raw_info->x_size = get_unaligned_be16(&params[0]);
1577 raw_info->y_size = get_unaligned_be16(&params[2]);
1578 raw_info->z_range = params[4];
1579 raw_info->area_range = params[5];
1580 raw_info->maxcontacts = params[7];
1581 raw_info->origin = params[8];
1582 /* res is given in unit per inch */
1583 raw_info->res = get_unaligned_be16(&params[13]) * 2 / 51;
1584
1585 return ret;
1586 }
1587
1588 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev,
1589 u8 feature_index, bool send_raw_reports,
1590 bool sensor_enhanced_settings)
1591 {
1592 struct hidpp_report response;
1593
1594 /*
1595 * Params:
1596 * bit 0 - enable raw
1597 * bit 1 - 16bit Z, no area
1598 * bit 2 - enhanced sensitivity
1599 * bit 3 - width, height (4 bits each) instead of area
1600 * bit 4 - send raw + gestures (degrades smoothness)
1601 * remaining bits - reserved
1602 */
1603 u8 params = send_raw_reports | (sensor_enhanced_settings << 2);
1604
1605 return hidpp_send_fap_command_sync(hidpp_dev, feature_index,
1606 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, &params, 1, &response);
1607 }
1608
1609 static void hidpp_touchpad_touch_event(u8 *data,
1610 struct hidpp_touchpad_raw_xy_finger *finger)
1611 {
1612 u8 x_m = data[0] << 2;
1613 u8 y_m = data[2] << 2;
1614
1615 finger->x = x_m << 6 | data[1];
1616 finger->y = y_m << 6 | data[3];
1617
1618 finger->contact_type = data[0] >> 6;
1619 finger->contact_status = data[2] >> 6;
1620
1621 finger->z = data[4];
1622 finger->area = data[5];
1623 finger->finger_id = data[6] >> 4;
1624 }
1625
1626 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev,
1627 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy)
1628 {
1629 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy));
1630 raw_xy->end_of_frame = data[8] & 0x01;
1631 raw_xy->spurious_flag = (data[8] >> 1) & 0x01;
1632 raw_xy->finger_count = data[15] & 0x0f;
1633 raw_xy->button = (data[8] >> 2) & 0x01;
1634
1635 if (raw_xy->finger_count) {
1636 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]);
1637 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]);
1638 }
1639 }
1640
1641 /* -------------------------------------------------------------------------- */
1642 /* 0x8123: Force feedback support */
1643 /* -------------------------------------------------------------------------- */
1644
1645 #define HIDPP_FF_GET_INFO 0x01
1646 #define HIDPP_FF_RESET_ALL 0x11
1647 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21
1648 #define HIDPP_FF_SET_EFFECT_STATE 0x31
1649 #define HIDPP_FF_DESTROY_EFFECT 0x41
1650 #define HIDPP_FF_GET_APERTURE 0x51
1651 #define HIDPP_FF_SET_APERTURE 0x61
1652 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71
1653 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81
1654
1655 #define HIDPP_FF_EFFECT_STATE_GET 0x00
1656 #define HIDPP_FF_EFFECT_STATE_STOP 0x01
1657 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02
1658 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03
1659
1660 #define HIDPP_FF_EFFECT_CONSTANT 0x00
1661 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01
1662 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02
1663 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03
1664 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04
1665 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05
1666 #define HIDPP_FF_EFFECT_SPRING 0x06
1667 #define HIDPP_FF_EFFECT_DAMPER 0x07
1668 #define HIDPP_FF_EFFECT_FRICTION 0x08
1669 #define HIDPP_FF_EFFECT_INERTIA 0x09
1670 #define HIDPP_FF_EFFECT_RAMP 0x0A
1671
1672 #define HIDPP_FF_EFFECT_AUTOSTART 0x80
1673
1674 #define HIDPP_FF_EFFECTID_NONE -1
1675 #define HIDPP_FF_EFFECTID_AUTOCENTER -2
1676
1677 #define HIDPP_FF_MAX_PARAMS 20
1678 #define HIDPP_FF_RESERVED_SLOTS 1
1679
1680 struct hidpp_ff_private_data {
1681 struct hidpp_device *hidpp;
1682 u8 feature_index;
1683 u8 version;
1684 u16 gain;
1685 s16 range;
1686 u8 slot_autocenter;
1687 u8 num_effects;
1688 int *effect_ids;
1689 struct workqueue_struct *wq;
1690 atomic_t workqueue_size;
1691 };
1692
1693 struct hidpp_ff_work_data {
1694 struct work_struct work;
1695 struct hidpp_ff_private_data *data;
1696 int effect_id;
1697 u8 command;
1698 u8 params[HIDPP_FF_MAX_PARAMS];
1699 u8 size;
1700 };
1701
1702 static const signed short hidpp_ff_effects[] = {
1703 FF_CONSTANT,
1704 FF_PERIODIC,
1705 FF_SINE,
1706 FF_SQUARE,
1707 FF_SAW_UP,
1708 FF_SAW_DOWN,
1709 FF_TRIANGLE,
1710 FF_SPRING,
1711 FF_DAMPER,
1712 FF_AUTOCENTER,
1713 FF_GAIN,
1714 -1
1715 };
1716
1717 static const signed short hidpp_ff_effects_v2[] = {
1718 FF_RAMP,
1719 FF_FRICTION,
1720 FF_INERTIA,
1721 -1
1722 };
1723
1724 static const u8 HIDPP_FF_CONDITION_CMDS[] = {
1725 HIDPP_FF_EFFECT_SPRING,
1726 HIDPP_FF_EFFECT_FRICTION,
1727 HIDPP_FF_EFFECT_DAMPER,
1728 HIDPP_FF_EFFECT_INERTIA
1729 };
1730
1731 static const char *HIDPP_FF_CONDITION_NAMES[] = {
1732 "spring",
1733 "friction",
1734 "damper",
1735 "inertia"
1736 };
1737
1738
1739 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id)
1740 {
1741 int i;
1742
1743 for (i = 0; i < data->num_effects; i++)
1744 if (data->effect_ids[i] == effect_id)
1745 return i+1;
1746
1747 return 0;
1748 }
1749
1750 static void hidpp_ff_work_handler(struct work_struct *w)
1751 {
1752 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work);
1753 struct hidpp_ff_private_data *data = wd->data;
1754 struct hidpp_report response;
1755 u8 slot;
1756 int ret;
1757
1758 /* add slot number if needed */
1759 switch (wd->effect_id) {
1760 case HIDPP_FF_EFFECTID_AUTOCENTER:
1761 wd->params[0] = data->slot_autocenter;
1762 break;
1763 case HIDPP_FF_EFFECTID_NONE:
1764 /* leave slot as zero */
1765 break;
1766 default:
1767 /* find current slot for effect */
1768 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id);
1769 break;
1770 }
1771
1772 /* send command and wait for reply */
1773 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index,
1774 wd->command, wd->params, wd->size, &response);
1775
1776 if (ret) {
1777 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n");
1778 goto out;
1779 }
1780
1781 /* parse return data */
1782 switch (wd->command) {
1783 case HIDPP_FF_DOWNLOAD_EFFECT:
1784 slot = response.fap.params[0];
1785 if (slot > 0 && slot <= data->num_effects) {
1786 if (wd->effect_id >= 0)
1787 /* regular effect uploaded */
1788 data->effect_ids[slot-1] = wd->effect_id;
1789 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1790 /* autocenter spring uploaded */
1791 data->slot_autocenter = slot;
1792 }
1793 break;
1794 case HIDPP_FF_DESTROY_EFFECT:
1795 if (wd->effect_id >= 0)
1796 /* regular effect destroyed */
1797 data->effect_ids[wd->params[0]-1] = -1;
1798 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER)
1799 /* autocenter spring destoyed */
1800 data->slot_autocenter = 0;
1801 break;
1802 case HIDPP_FF_SET_GLOBAL_GAINS:
1803 data->gain = (wd->params[0] << 8) + wd->params[1];
1804 break;
1805 case HIDPP_FF_SET_APERTURE:
1806 data->range = (wd->params[0] << 8) + wd->params[1];
1807 break;
1808 default:
1809 /* no action needed */
1810 break;
1811 }
1812
1813 out:
1814 atomic_dec(&data->workqueue_size);
1815 kfree(wd);
1816 }
1817
1818 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size)
1819 {
1820 struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL);
1821 int s;
1822
1823 if (!wd)
1824 return -ENOMEM;
1825
1826 INIT_WORK(&wd->work, hidpp_ff_work_handler);
1827
1828 wd->data = data;
1829 wd->effect_id = effect_id;
1830 wd->command = command;
1831 wd->size = size;
1832 memcpy(wd->params, params, size);
1833
1834 atomic_inc(&data->workqueue_size);
1835 queue_work(data->wq, &wd->work);
1836
1837 /* warn about excessive queue size */
1838 s = atomic_read(&data->workqueue_size);
1839 if (s >= 20 && s % 20 == 0)
1840 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s);
1841
1842 return 0;
1843 }
1844
1845 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
1846 {
1847 struct hidpp_ff_private_data *data = dev->ff->private;
1848 u8 params[20];
1849 u8 size;
1850 int force;
1851
1852 /* set common parameters */
1853 params[2] = effect->replay.length >> 8;
1854 params[3] = effect->replay.length & 255;
1855 params[4] = effect->replay.delay >> 8;
1856 params[5] = effect->replay.delay & 255;
1857
1858 switch (effect->type) {
1859 case FF_CONSTANT:
1860 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1861 params[1] = HIDPP_FF_EFFECT_CONSTANT;
1862 params[6] = force >> 8;
1863 params[7] = force & 255;
1864 params[8] = effect->u.constant.envelope.attack_level >> 7;
1865 params[9] = effect->u.constant.envelope.attack_length >> 8;
1866 params[10] = effect->u.constant.envelope.attack_length & 255;
1867 params[11] = effect->u.constant.envelope.fade_level >> 7;
1868 params[12] = effect->u.constant.envelope.fade_length >> 8;
1869 params[13] = effect->u.constant.envelope.fade_length & 255;
1870 size = 14;
1871 dbg_hid("Uploading constant force level=%d in dir %d = %d\n",
1872 effect->u.constant.level,
1873 effect->direction, force);
1874 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1875 effect->u.constant.envelope.attack_level,
1876 effect->u.constant.envelope.attack_length,
1877 effect->u.constant.envelope.fade_level,
1878 effect->u.constant.envelope.fade_length);
1879 break;
1880 case FF_PERIODIC:
1881 {
1882 switch (effect->u.periodic.waveform) {
1883 case FF_SINE:
1884 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE;
1885 break;
1886 case FF_SQUARE:
1887 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE;
1888 break;
1889 case FF_SAW_UP:
1890 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP;
1891 break;
1892 case FF_SAW_DOWN:
1893 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN;
1894 break;
1895 case FF_TRIANGLE:
1896 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE;
1897 break;
1898 default:
1899 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform);
1900 return -EINVAL;
1901 }
1902 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1903 params[6] = effect->u.periodic.magnitude >> 8;
1904 params[7] = effect->u.periodic.magnitude & 255;
1905 params[8] = effect->u.periodic.offset >> 8;
1906 params[9] = effect->u.periodic.offset & 255;
1907 params[10] = effect->u.periodic.period >> 8;
1908 params[11] = effect->u.periodic.period & 255;
1909 params[12] = effect->u.periodic.phase >> 8;
1910 params[13] = effect->u.periodic.phase & 255;
1911 params[14] = effect->u.periodic.envelope.attack_level >> 7;
1912 params[15] = effect->u.periodic.envelope.attack_length >> 8;
1913 params[16] = effect->u.periodic.envelope.attack_length & 255;
1914 params[17] = effect->u.periodic.envelope.fade_level >> 7;
1915 params[18] = effect->u.periodic.envelope.fade_length >> 8;
1916 params[19] = effect->u.periodic.envelope.fade_length & 255;
1917 size = 20;
1918 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n",
1919 effect->u.periodic.magnitude, effect->direction,
1920 effect->u.periodic.offset,
1921 effect->u.periodic.period,
1922 effect->u.periodic.phase);
1923 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1924 effect->u.periodic.envelope.attack_level,
1925 effect->u.periodic.envelope.attack_length,
1926 effect->u.periodic.envelope.fade_level,
1927 effect->u.periodic.envelope.fade_length);
1928 break;
1929 }
1930 case FF_RAMP:
1931 params[1] = HIDPP_FF_EFFECT_RAMP;
1932 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1933 params[6] = force >> 8;
1934 params[7] = force & 255;
1935 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15;
1936 params[8] = force >> 8;
1937 params[9] = force & 255;
1938 params[10] = effect->u.ramp.envelope.attack_level >> 7;
1939 params[11] = effect->u.ramp.envelope.attack_length >> 8;
1940 params[12] = effect->u.ramp.envelope.attack_length & 255;
1941 params[13] = effect->u.ramp.envelope.fade_level >> 7;
1942 params[14] = effect->u.ramp.envelope.fade_length >> 8;
1943 params[15] = effect->u.ramp.envelope.fade_length & 255;
1944 size = 16;
1945 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n",
1946 effect->u.ramp.start_level,
1947 effect->u.ramp.end_level,
1948 effect->direction, force);
1949 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n",
1950 effect->u.ramp.envelope.attack_level,
1951 effect->u.ramp.envelope.attack_length,
1952 effect->u.ramp.envelope.fade_level,
1953 effect->u.ramp.envelope.fade_length);
1954 break;
1955 case FF_FRICTION:
1956 case FF_INERTIA:
1957 case FF_SPRING:
1958 case FF_DAMPER:
1959 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING];
1960 params[6] = effect->u.condition[0].left_saturation >> 9;
1961 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255;
1962 params[8] = effect->u.condition[0].left_coeff >> 8;
1963 params[9] = effect->u.condition[0].left_coeff & 255;
1964 params[10] = effect->u.condition[0].deadband >> 9;
1965 params[11] = (effect->u.condition[0].deadband >> 1) & 255;
1966 params[12] = effect->u.condition[0].center >> 8;
1967 params[13] = effect->u.condition[0].center & 255;
1968 params[14] = effect->u.condition[0].right_coeff >> 8;
1969 params[15] = effect->u.condition[0].right_coeff & 255;
1970 params[16] = effect->u.condition[0].right_saturation >> 9;
1971 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255;
1972 size = 18;
1973 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n",
1974 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING],
1975 effect->u.condition[0].left_coeff,
1976 effect->u.condition[0].left_saturation,
1977 effect->u.condition[0].right_coeff,
1978 effect->u.condition[0].right_saturation);
1979 dbg_hid(" deadband=%d, center=%d\n",
1980 effect->u.condition[0].deadband,
1981 effect->u.condition[0].center);
1982 break;
1983 default:
1984 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type);
1985 return -EINVAL;
1986 }
1987
1988 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size);
1989 }
1990
1991 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value)
1992 {
1993 struct hidpp_ff_private_data *data = dev->ff->private;
1994 u8 params[2];
1995
1996 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP;
1997
1998 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id);
1999
2000 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params));
2001 }
2002
2003 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id)
2004 {
2005 struct hidpp_ff_private_data *data = dev->ff->private;
2006 u8 slot = 0;
2007
2008 dbg_hid("Erasing effect %d.\n", effect_id);
2009
2010 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1);
2011 }
2012
2013 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude)
2014 {
2015 struct hidpp_ff_private_data *data = dev->ff->private;
2016 u8 params[18];
2017
2018 dbg_hid("Setting autocenter to %d.\n", magnitude);
2019
2020 /* start a standard spring effect */
2021 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART;
2022 /* zero delay and duration */
2023 params[2] = params[3] = params[4] = params[5] = 0;
2024 /* set coeff to 25% of saturation */
2025 params[8] = params[14] = magnitude >> 11;
2026 params[9] = params[15] = (magnitude >> 3) & 255;
2027 params[6] = params[16] = magnitude >> 9;
2028 params[7] = params[17] = (magnitude >> 1) & 255;
2029 /* zero deadband and center */
2030 params[10] = params[11] = params[12] = params[13] = 0;
2031
2032 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params));
2033 }
2034
2035 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain)
2036 {
2037 struct hidpp_ff_private_data *data = dev->ff->private;
2038 u8 params[4];
2039
2040 dbg_hid("Setting gain to %d.\n", gain);
2041
2042 params[0] = gain >> 8;
2043 params[1] = gain & 255;
2044 params[2] = 0; /* no boost */
2045 params[3] = 0;
2046
2047 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params));
2048 }
2049
2050 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
2051 {
2052 struct hid_device *hid = to_hid_device(dev);
2053 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2054 struct input_dev *idev = hidinput->input;
2055 struct hidpp_ff_private_data *data = idev->ff->private;
2056
2057 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range);
2058 }
2059
2060 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2061 {
2062 struct hid_device *hid = to_hid_device(dev);
2063 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2064 struct input_dev *idev = hidinput->input;
2065 struct hidpp_ff_private_data *data = idev->ff->private;
2066 u8 params[2];
2067 int range = simple_strtoul(buf, NULL, 10);
2068
2069 range = clamp(range, 180, 900);
2070
2071 params[0] = range >> 8;
2072 params[1] = range & 0x00FF;
2073
2074 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params));
2075
2076 return count;
2077 }
2078
2079 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store);
2080
2081 static void hidpp_ff_destroy(struct ff_device *ff)
2082 {
2083 struct hidpp_ff_private_data *data = ff->private;
2084
2085 kfree(data->effect_ids);
2086 }
2087
2088 static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
2089 {
2090 struct hid_device *hid = hidpp->hid_dev;
2091 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2092 struct input_dev *dev = hidinput->input;
2093 const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
2094 const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
2095 struct ff_device *ff;
2096 struct hidpp_report response;
2097 struct hidpp_ff_private_data *data;
2098 int error, j, num_slots;
2099 u8 version;
2100
2101 if (!dev) {
2102 hid_err(hid, "Struct input_dev not set!\n");
2103 return -EINVAL;
2104 }
2105
2106 /* Get firmware release */
2107 version = bcdDevice & 255;
2108
2109 /* Set supported force feedback capabilities */
2110 for (j = 0; hidpp_ff_effects[j] >= 0; j++)
2111 set_bit(hidpp_ff_effects[j], dev->ffbit);
2112 if (version > 1)
2113 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++)
2114 set_bit(hidpp_ff_effects_v2[j], dev->ffbit);
2115
2116 /* Read number of slots available in device */
2117 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2118 HIDPP_FF_GET_INFO, NULL, 0, &response);
2119 if (error) {
2120 if (error < 0)
2121 return error;
2122 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n",
2123 __func__, error);
2124 return -EPROTO;
2125 }
2126
2127 num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS;
2128
2129 error = input_ff_create(dev, num_slots);
2130
2131 if (error) {
2132 hid_err(dev, "Failed to create FF device!\n");
2133 return error;
2134 }
2135
2136 data = kzalloc(sizeof(*data), GFP_KERNEL);
2137 if (!data)
2138 return -ENOMEM;
2139 data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL);
2140 if (!data->effect_ids) {
2141 kfree(data);
2142 return -ENOMEM;
2143 }
2144 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue");
2145 if (!data->wq) {
2146 kfree(data->effect_ids);
2147 kfree(data);
2148 return -ENOMEM;
2149 }
2150
2151 data->hidpp = hidpp;
2152 data->feature_index = feature_index;
2153 data->version = version;
2154 data->slot_autocenter = 0;
2155 data->num_effects = num_slots;
2156 for (j = 0; j < num_slots; j++)
2157 data->effect_ids[j] = -1;
2158
2159 ff = dev->ff;
2160 ff->private = data;
2161
2162 ff->upload = hidpp_ff_upload_effect;
2163 ff->erase = hidpp_ff_erase_effect;
2164 ff->playback = hidpp_ff_playback;
2165 ff->set_gain = hidpp_ff_set_gain;
2166 ff->set_autocenter = hidpp_ff_set_autocenter;
2167 ff->destroy = hidpp_ff_destroy;
2168
2169
2170 /* reset all forces */
2171 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2172 HIDPP_FF_RESET_ALL, NULL, 0, &response);
2173
2174 /* Read current Range */
2175 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2176 HIDPP_FF_GET_APERTURE, NULL, 0, &response);
2177 if (error)
2178 hid_warn(hidpp->hid_dev, "Failed to read range from device!\n");
2179 data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]);
2180
2181 /* Create sysfs interface */
2182 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range);
2183 if (error)
2184 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error);
2185
2186 /* Read the current gain values */
2187 error = hidpp_send_fap_command_sync(hidpp, feature_index,
2188 HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response);
2189 if (error)
2190 hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n");
2191 data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]);
2192 /* ignore boost value at response.fap.params[2] */
2193
2194 /* init the hardware command queue */
2195 atomic_set(&data->workqueue_size, 0);
2196
2197 /* initialize with zero autocenter to get wheel in usable state */
2198 hidpp_ff_set_autocenter(dev, 0);
2199
2200 hid_info(hid, "Force feedback support loaded (firmware release %d).\n",
2201 version);
2202
2203 return 0;
2204 }
2205
2206 static int hidpp_ff_deinit(struct hid_device *hid)
2207 {
2208 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
2209 struct input_dev *dev = hidinput->input;
2210 struct hidpp_ff_private_data *data;
2211
2212 if (!dev) {
2213 hid_err(hid, "Struct input_dev not found!\n");
2214 return -EINVAL;
2215 }
2216
2217 hid_info(hid, "Unloading HID++ force feedback.\n");
2218 data = dev->ff->private;
2219 if (!data) {
2220 hid_err(hid, "Private data not found!\n");
2221 return -EINVAL;
2222 }
2223
2224 destroy_workqueue(data->wq);
2225 device_remove_file(&hid->dev, &dev_attr_range);
2226
2227 return 0;
2228 }
2229
2230
2231 /* ************************************************************************** */
2232 /* */
2233 /* Device Support */
2234 /* */
2235 /* ************************************************************************** */
2236
2237 /* -------------------------------------------------------------------------- */
2238 /* Touchpad HID++ devices */
2239 /* -------------------------------------------------------------------------- */
2240
2241 #define WTP_MANUAL_RESOLUTION 39
2242
2243 struct wtp_data {
2244 u16 x_size, y_size;
2245 u8 finger_count;
2246 u8 mt_feature_index;
2247 u8 button_feature_index;
2248 u8 maxcontacts;
2249 bool flip_y;
2250 unsigned int resolution;
2251 };
2252
2253 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2254 struct hid_field *field, struct hid_usage *usage,
2255 unsigned long **bit, int *max)
2256 {
2257 return -1;
2258 }
2259
2260 static void wtp_populate_input(struct hidpp_device *hidpp,
2261 struct input_dev *input_dev)
2262 {
2263 struct wtp_data *wd = hidpp->private_data;
2264
2265 __set_bit(EV_ABS, input_dev->evbit);
2266 __set_bit(EV_KEY, input_dev->evbit);
2267 __clear_bit(EV_REL, input_dev->evbit);
2268 __clear_bit(EV_LED, input_dev->evbit);
2269
2270 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0);
2271 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution);
2272 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0);
2273 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution);
2274
2275 /* Max pressure is not given by the devices, pick one */
2276 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0);
2277
2278 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
2279
2280 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)
2281 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
2282 else
2283 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit);
2284
2285 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER |
2286 INPUT_MT_DROP_UNUSED);
2287 }
2288
2289 static void wtp_touch_event(struct hidpp_device *hidpp,
2290 struct hidpp_touchpad_raw_xy_finger *touch_report)
2291 {
2292 struct wtp_data *wd = hidpp->private_data;
2293 int slot;
2294
2295 if (!touch_report->finger_id || touch_report->contact_type)
2296 /* no actual data */
2297 return;
2298
2299 slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id);
2300
2301 input_mt_slot(hidpp->input, slot);
2302 input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER,
2303 touch_report->contact_status);
2304 if (touch_report->contact_status) {
2305 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X,
2306 touch_report->x);
2307 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y,
2308 wd->flip_y ? wd->y_size - touch_report->y :
2309 touch_report->y);
2310 input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE,
2311 touch_report->area);
2312 }
2313 }
2314
2315 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp,
2316 struct hidpp_touchpad_raw_xy *raw)
2317 {
2318 int i;
2319
2320 for (i = 0; i < 2; i++)
2321 wtp_touch_event(hidpp, &(raw->fingers[i]));
2322
2323 if (raw->end_of_frame &&
2324 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS))
2325 input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button);
2326
2327 if (raw->end_of_frame || raw->finger_count <= 2) {
2328 input_mt_sync_frame(hidpp->input);
2329 input_sync(hidpp->input);
2330 }
2331 }
2332
2333 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data)
2334 {
2335 struct wtp_data *wd = hidpp->private_data;
2336 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) +
2337 (data[7] >> 4) * (data[7] >> 4)) / 2;
2338 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) +
2339 (data[13] >> 4) * (data[13] >> 4)) / 2;
2340 struct hidpp_touchpad_raw_xy raw = {
2341 .timestamp = data[1],
2342 .fingers = {
2343 {
2344 .contact_type = 0,
2345 .contact_status = !!data[7],
2346 .x = get_unaligned_le16(&data[3]),
2347 .y = get_unaligned_le16(&data[5]),
2348 .z = c1_area,
2349 .area = c1_area,
2350 .finger_id = data[2],
2351 }, {
2352 .contact_type = 0,
2353 .contact_status = !!data[13],
2354 .x = get_unaligned_le16(&data[9]),
2355 .y = get_unaligned_le16(&data[11]),
2356 .z = c2_area,
2357 .area = c2_area,
2358 .finger_id = data[8],
2359 }
2360 },
2361 .finger_count = wd->maxcontacts,
2362 .spurious_flag = 0,
2363 .end_of_frame = (data[0] >> 7) == 0,
2364 .button = data[0] & 0x01,
2365 };
2366
2367 wtp_send_raw_xy_event(hidpp, &raw);
2368
2369 return 1;
2370 }
2371
2372 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size)
2373 {
2374 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2375 struct wtp_data *wd = hidpp->private_data;
2376 struct hidpp_report *report = (struct hidpp_report *)data;
2377 struct hidpp_touchpad_raw_xy raw;
2378
2379 if (!wd || !hidpp->input)
2380 return 1;
2381
2382 switch (data[0]) {
2383 case 0x02:
2384 if (size < 2) {
2385 hid_err(hdev, "Received HID report of bad size (%d)",
2386 size);
2387 return 1;
2388 }
2389 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) {
2390 input_event(hidpp->input, EV_KEY, BTN_LEFT,
2391 !!(data[1] & 0x01));
2392 input_event(hidpp->input, EV_KEY, BTN_RIGHT,
2393 !!(data[1] & 0x02));
2394 input_sync(hidpp->input);
2395 return 0;
2396 } else {
2397 if (size < 21)
2398 return 1;
2399 return wtp_mouse_raw_xy_event(hidpp, &data[7]);
2400 }
2401 case REPORT_ID_HIDPP_LONG:
2402 /* size is already checked in hidpp_raw_event. */
2403 if ((report->fap.feature_index != wd->mt_feature_index) ||
2404 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY))
2405 return 1;
2406 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw);
2407
2408 wtp_send_raw_xy_event(hidpp, &raw);
2409 return 0;
2410 }
2411
2412 return 0;
2413 }
2414
2415 static int wtp_get_config(struct hidpp_device *hidpp)
2416 {
2417 struct wtp_data *wd = hidpp->private_data;
2418 struct hidpp_touchpad_raw_info raw_info = {0};
2419 u8 feature_type;
2420 int ret;
2421
2422 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY,
2423 &wd->mt_feature_index, &feature_type);
2424 if (ret)
2425 /* means that the device is not powered up */
2426 return ret;
2427
2428 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index,
2429 &raw_info);
2430 if (ret)
2431 return ret;
2432
2433 wd->x_size = raw_info.x_size;
2434 wd->y_size = raw_info.y_size;
2435 wd->maxcontacts = raw_info.maxcontacts;
2436 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT;
2437 wd->resolution = raw_info.res;
2438 if (!wd->resolution)
2439 wd->resolution = WTP_MANUAL_RESOLUTION;
2440
2441 return 0;
2442 }
2443
2444 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id)
2445 {
2446 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2447 struct wtp_data *wd;
2448
2449 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data),
2450 GFP_KERNEL);
2451 if (!wd)
2452 return -ENOMEM;
2453
2454 hidpp->private_data = wd;
2455
2456 return 0;
2457 };
2458
2459 static int wtp_connect(struct hid_device *hdev, bool connected)
2460 {
2461 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2462 struct wtp_data *wd = hidpp->private_data;
2463 int ret;
2464
2465 if (!wd->x_size) {
2466 ret = wtp_get_config(hidpp);
2467 if (ret) {
2468 hid_err(hdev, "Can not get wtp config: %d\n", ret);
2469 return ret;
2470 }
2471 }
2472
2473 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index,
2474 true, true);
2475 }
2476
2477 /* ------------------------------------------------------------------------- */
2478 /* Logitech M560 devices */
2479 /* ------------------------------------------------------------------------- */
2480
2481 /*
2482 * Logitech M560 protocol overview
2483 *
2484 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or
2485 * the sides buttons are pressed, it sends some keyboard keys events
2486 * instead of buttons ones.
2487 * To complicate things further, the middle button keys sequence
2488 * is different from the odd press and the even press.
2489 *
2490 * forward button -> Super_R
2491 * backward button -> Super_L+'d' (press only)
2492 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only)
2493 * 2nd time: left-click (press only)
2494 * NB: press-only means that when the button is pressed, the
2495 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated
2496 * together sequentially; instead when the button is released, no event is
2497 * generated !
2498 *
2499 * With the command
2500 * 10<xx>0a 3500af03 (where <xx> is the mouse id),
2501 * the mouse reacts differently:
2502 * - it never sends a keyboard key event
2503 * - for the three mouse button it sends:
2504 * middle button press 11<xx>0a 3500af00...
2505 * side 1 button (forward) press 11<xx>0a 3500b000...
2506 * side 2 button (backward) press 11<xx>0a 3500ae00...
2507 * middle/side1/side2 button release 11<xx>0a 35000000...
2508 */
2509
2510 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03};
2511
2512 /* how buttons are mapped in the report */
2513 #define M560_MOUSE_BTN_LEFT 0x01
2514 #define M560_MOUSE_BTN_RIGHT 0x02
2515 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08
2516 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10
2517
2518 #define M560_SUB_ID 0x0a
2519 #define M560_BUTTON_MODE_REGISTER 0x35
2520
2521 static int m560_send_config_command(struct hid_device *hdev, bool connected)
2522 {
2523 struct hidpp_report response;
2524 struct hidpp_device *hidpp_dev;
2525
2526 hidpp_dev = hid_get_drvdata(hdev);
2527
2528 return hidpp_send_rap_command_sync(
2529 hidpp_dev,
2530 REPORT_ID_HIDPP_SHORT,
2531 M560_SUB_ID,
2532 M560_BUTTON_MODE_REGISTER,
2533 (u8 *)m560_config_parameter,
2534 sizeof(m560_config_parameter),
2535 &response
2536 );
2537 }
2538
2539 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size)
2540 {
2541 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2542
2543 /* sanity check */
2544 if (!hidpp->input) {
2545 hid_err(hdev, "error in parameter\n");
2546 return -EINVAL;
2547 }
2548
2549 if (size < 7) {
2550 hid_err(hdev, "error in report\n");
2551 return 0;
2552 }
2553
2554 if (data[0] == REPORT_ID_HIDPP_LONG &&
2555 data[2] == M560_SUB_ID && data[6] == 0x00) {
2556 /*
2557 * m560 mouse report for middle, forward and backward button
2558 *
2559 * data[0] = 0x11
2560 * data[1] = device-id
2561 * data[2] = 0x0a
2562 * data[5] = 0xaf -> middle
2563 * 0xb0 -> forward
2564 * 0xae -> backward
2565 * 0x00 -> release all
2566 * data[6] = 0x00
2567 */
2568
2569 switch (data[5]) {
2570 case 0xaf:
2571 input_report_key(hidpp->input, BTN_MIDDLE, 1);
2572 break;
2573 case 0xb0:
2574 input_report_key(hidpp->input, BTN_FORWARD, 1);
2575 break;
2576 case 0xae:
2577 input_report_key(hidpp->input, BTN_BACK, 1);
2578 break;
2579 case 0x00:
2580 input_report_key(hidpp->input, BTN_BACK, 0);
2581 input_report_key(hidpp->input, BTN_FORWARD, 0);
2582 input_report_key(hidpp->input, BTN_MIDDLE, 0);
2583 break;
2584 default:
2585 hid_err(hdev, "error in report\n");
2586 return 0;
2587 }
2588 input_sync(hidpp->input);
2589
2590 } else if (data[0] == 0x02) {
2591 /*
2592 * Logitech M560 mouse report
2593 *
2594 * data[0] = type (0x02)
2595 * data[1..2] = buttons
2596 * data[3..5] = xy
2597 * data[6] = wheel
2598 */
2599
2600 int v;
2601
2602 input_report_key(hidpp->input, BTN_LEFT,
2603 !!(data[1] & M560_MOUSE_BTN_LEFT));
2604 input_report_key(hidpp->input, BTN_RIGHT,
2605 !!(data[1] & M560_MOUSE_BTN_RIGHT));
2606
2607 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) {
2608 input_report_rel(hidpp->input, REL_HWHEEL, -1);
2609 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
2610 -120);
2611 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) {
2612 input_report_rel(hidpp->input, REL_HWHEEL, 1);
2613 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES,
2614 120);
2615 }
2616
2617 v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12);
2618 input_report_rel(hidpp->input, REL_X, v);
2619
2620 v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12);
2621 input_report_rel(hidpp->input, REL_Y, v);
2622
2623 v = hid_snto32(data[6], 8);
2624 if (v != 0)
2625 hidpp_scroll_counter_handle_scroll(hidpp->input,
2626 &hidpp->vertical_wheel_counter, v);
2627
2628 input_sync(hidpp->input);
2629 }
2630
2631 return 1;
2632 }
2633
2634 static void m560_populate_input(struct hidpp_device *hidpp,
2635 struct input_dev *input_dev)
2636 {
2637 __set_bit(EV_KEY, input_dev->evbit);
2638 __set_bit(BTN_MIDDLE, input_dev->keybit);
2639 __set_bit(BTN_RIGHT, input_dev->keybit);
2640 __set_bit(BTN_LEFT, input_dev->keybit);
2641 __set_bit(BTN_BACK, input_dev->keybit);
2642 __set_bit(BTN_FORWARD, input_dev->keybit);
2643
2644 __set_bit(EV_REL, input_dev->evbit);
2645 __set_bit(REL_X, input_dev->relbit);
2646 __set_bit(REL_Y, input_dev->relbit);
2647 __set_bit(REL_WHEEL, input_dev->relbit);
2648 __set_bit(REL_HWHEEL, input_dev->relbit);
2649 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
2650 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
2651 }
2652
2653 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2654 struct hid_field *field, struct hid_usage *usage,
2655 unsigned long **bit, int *max)
2656 {
2657 return -1;
2658 }
2659
2660 /* ------------------------------------------------------------------------- */
2661 /* Logitech K400 devices */
2662 /* ------------------------------------------------------------------------- */
2663
2664 /*
2665 * The Logitech K400 keyboard has an embedded touchpad which is seen
2666 * as a mouse from the OS point of view. There is a hardware shortcut to disable
2667 * tap-to-click but the setting is not remembered accross reset, annoying some
2668 * users.
2669 *
2670 * We can toggle this feature from the host by using the feature 0x6010:
2671 * Touchpad FW items
2672 */
2673
2674 struct k400_private_data {
2675 u8 feature_index;
2676 };
2677
2678 static int k400_disable_tap_to_click(struct hidpp_device *hidpp)
2679 {
2680 struct k400_private_data *k400 = hidpp->private_data;
2681 struct hidpp_touchpad_fw_items items = {};
2682 int ret;
2683 u8 feature_type;
2684
2685 if (!k400->feature_index) {
2686 ret = hidpp_root_get_feature(hidpp,
2687 HIDPP_PAGE_TOUCHPAD_FW_ITEMS,
2688 &k400->feature_index, &feature_type);
2689 if (ret)
2690 /* means that the device is not powered up */
2691 return ret;
2692 }
2693
2694 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items);
2695 if (ret)
2696 return ret;
2697
2698 return 0;
2699 }
2700
2701 static int k400_allocate(struct hid_device *hdev)
2702 {
2703 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2704 struct k400_private_data *k400;
2705
2706 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data),
2707 GFP_KERNEL);
2708 if (!k400)
2709 return -ENOMEM;
2710
2711 hidpp->private_data = k400;
2712
2713 return 0;
2714 };
2715
2716 static int k400_connect(struct hid_device *hdev, bool connected)
2717 {
2718 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2719
2720 if (!disable_tap_to_click)
2721 return 0;
2722
2723 return k400_disable_tap_to_click(hidpp);
2724 }
2725
2726 /* ------------------------------------------------------------------------- */
2727 /* Logitech G920 Driving Force Racing Wheel for Xbox One */
2728 /* ------------------------------------------------------------------------- */
2729
2730 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123
2731
2732 static int g920_get_config(struct hidpp_device *hidpp)
2733 {
2734 u8 feature_type;
2735 u8 feature_index;
2736 int ret;
2737
2738 /* Find feature and store for later use */
2739 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK,
2740 &feature_index, &feature_type);
2741 if (ret)
2742 return ret;
2743
2744 ret = hidpp_ff_init(hidpp, feature_index);
2745 if (ret)
2746 hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n",
2747 ret);
2748
2749 return 0;
2750 }
2751
2752 /* -------------------------------------------------------------------------- */
2753 /* HID++1.0 devices which use HID++ reports for their wheels */
2754 /* -------------------------------------------------------------------------- */
2755 static int hidpp10_wheel_connect(struct hidpp_device *hidpp)
2756 {
2757 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
2758 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT,
2759 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT);
2760 }
2761
2762 static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp,
2763 u8 *data, int size)
2764 {
2765 s8 value, hvalue;
2766
2767 if (!hidpp->input)
2768 return -EINVAL;
2769
2770 if (size < 7)
2771 return 0;
2772
2773 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER)
2774 return 0;
2775
2776 value = data[3];
2777 hvalue = data[4];
2778
2779 input_report_rel(hidpp->input, REL_WHEEL, value);
2780 input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120);
2781 input_report_rel(hidpp->input, REL_HWHEEL, hvalue);
2782 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120);
2783 input_sync(hidpp->input);
2784
2785 return 1;
2786 }
2787
2788 static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp,
2789 struct input_dev *input_dev)
2790 {
2791 __set_bit(EV_REL, input_dev->evbit);
2792 __set_bit(REL_WHEEL, input_dev->relbit);
2793 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit);
2794 __set_bit(REL_HWHEEL, input_dev->relbit);
2795 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit);
2796 }
2797
2798 /* -------------------------------------------------------------------------- */
2799 /* HID++1.0 mice which use HID++ reports for extra mouse buttons */
2800 /* -------------------------------------------------------------------------- */
2801 static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp)
2802 {
2803 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
2804 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT,
2805 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT);
2806 }
2807
2808 static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp,
2809 u8 *data, int size)
2810 {
2811 int i;
2812
2813 if (!hidpp->input)
2814 return -EINVAL;
2815
2816 if (size < 7)
2817 return 0;
2818
2819 if (data[0] != REPORT_ID_HIDPP_SHORT ||
2820 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS)
2821 return 0;
2822
2823 /*
2824 * Buttons are either delivered through the regular mouse report *or*
2825 * through the extra buttons report. At least for button 6 how it is
2826 * delivered differs per receiver firmware version. Even receivers with
2827 * the same usb-id show different behavior, so we handle both cases.
2828 */
2829 for (i = 0; i < 8; i++)
2830 input_report_key(hidpp->input, BTN_MOUSE + i,
2831 (data[3] & (1 << i)));
2832
2833 /* Some mice report events on button 9+, use BTN_MISC */
2834 for (i = 0; i < 8; i++)
2835 input_report_key(hidpp->input, BTN_MISC + i,
2836 (data[4] & (1 << i)));
2837
2838 input_sync(hidpp->input);
2839 return 1;
2840 }
2841
2842 static void hidpp10_extra_mouse_buttons_populate_input(
2843 struct hidpp_device *hidpp, struct input_dev *input_dev)
2844 {
2845 /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */
2846 __set_bit(BTN_0, input_dev->keybit);
2847 __set_bit(BTN_1, input_dev->keybit);
2848 __set_bit(BTN_2, input_dev->keybit);
2849 __set_bit(BTN_3, input_dev->keybit);
2850 __set_bit(BTN_4, input_dev->keybit);
2851 __set_bit(BTN_5, input_dev->keybit);
2852 __set_bit(BTN_6, input_dev->keybit);
2853 __set_bit(BTN_7, input_dev->keybit);
2854 }
2855
2856 /* -------------------------------------------------------------------------- */
2857 /* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */
2858 /* -------------------------------------------------------------------------- */
2859
2860 /* Find the consumer-page input report desc and change Maximums to 0x107f */
2861 static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp,
2862 u8 *_rdesc, unsigned int *rsize)
2863 {
2864 /* Note 0 terminated so we can use strnstr to search for this. */
2865 const char consumer_rdesc_start[] = {
2866 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
2867 0x09, 0x01, /* USAGE (Consumer Control) */
2868 0xA1, 0x01, /* COLLECTION (Application) */
2869 0x85, 0x03, /* REPORT_ID = 3 */
2870 0x75, 0x10, /* REPORT_SIZE (16) */
2871 0x95, 0x02, /* REPORT_COUNT (2) */
2872 0x15, 0x01, /* LOGICAL_MIN (1) */
2873 0x26, 0x00 /* LOGICAL_MAX (... */
2874 };
2875 char *consumer_rdesc, *rdesc = (char *)_rdesc;
2876 unsigned int size;
2877
2878 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize);
2879 size = *rsize - (consumer_rdesc - rdesc);
2880 if (consumer_rdesc && size >= 25) {
2881 consumer_rdesc[15] = 0x7f;
2882 consumer_rdesc[16] = 0x10;
2883 consumer_rdesc[20] = 0x7f;
2884 consumer_rdesc[21] = 0x10;
2885 }
2886 return _rdesc;
2887 }
2888
2889 static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp)
2890 {
2891 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0,
2892 HIDPP_ENABLE_CONSUMER_REPORT,
2893 HIDPP_ENABLE_CONSUMER_REPORT);
2894 }
2895
2896 static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,
2897 u8 *data, int size)
2898 {
2899 u8 consumer_report[5];
2900
2901 if (size < 7)
2902 return 0;
2903
2904 if (data[0] != REPORT_ID_HIDPP_SHORT ||
2905 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS)
2906 return 0;
2907
2908 /*
2909 * Build a normal consumer report (3) out of the data, this detour
2910 * is necessary to get some keyboards to report their 0x10xx usages.
2911 */
2912 consumer_report[0] = 0x03;
2913 memcpy(&consumer_report[1], &data[3], 4);
2914 /* We are called from atomic context */
2915 hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT,
2916 consumer_report, 5, 1);
2917
2918 return 1;
2919 }
2920
2921 /* -------------------------------------------------------------------------- */
2922 /* High-resolution scroll wheels */
2923 /* -------------------------------------------------------------------------- */
2924
2925 static int hi_res_scroll_enable(struct hidpp_device *hidpp)
2926 {
2927 int ret;
2928 u8 multiplier = 1;
2929
2930 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2121) {
2931 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false);
2932 if (ret == 0)
2933 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier);
2934 } else if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_X2120) {
2935 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true,
2936 &multiplier);
2937 } else /* if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) */ {
2938 ret = hidpp10_enable_scrolling_acceleration(hidpp);
2939 multiplier = 8;
2940 }
2941 if (ret)
2942 return ret;
2943
2944 if (multiplier == 0)
2945 multiplier = 1;
2946
2947 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier;
2948 hid_info(hidpp->hid_dev, "multiplier = %d\n", multiplier);
2949 return 0;
2950 }
2951
2952 /* -------------------------------------------------------------------------- */
2953 /* Generic HID++ devices */
2954 /* -------------------------------------------------------------------------- */
2955
2956 static u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc,
2957 unsigned int *rsize)
2958 {
2959 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2960
2961 if (!hidpp)
2962 return rdesc;
2963
2964 /* For 27 MHz keyboards the quirk gets set after hid_parse. */
2965 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE ||
2966 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS))
2967 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize);
2968
2969 return rdesc;
2970 }
2971
2972 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi,
2973 struct hid_field *field, struct hid_usage *usage,
2974 unsigned long **bit, int *max)
2975 {
2976 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2977
2978 if (!hidpp)
2979 return 0;
2980
2981 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
2982 return wtp_input_mapping(hdev, hi, field, usage, bit, max);
2983 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 &&
2984 field->application != HID_GD_MOUSE)
2985 return m560_input_mapping(hdev, hi, field, usage, bit, max);
2986
2987 return 0;
2988 }
2989
2990 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
2991 struct hid_field *field, struct hid_usage *usage,
2992 unsigned long **bit, int *max)
2993 {
2994 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
2995
2996 if (!hidpp)
2997 return 0;
2998
2999 /* Ensure that Logitech G920 is not given a default fuzz/flat value */
3000 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) {
3001 if (usage->type == EV_ABS && (usage->code == ABS_X ||
3002 usage->code == ABS_Y || usage->code == ABS_Z ||
3003 usage->code == ABS_RZ)) {
3004 field->application = HID_GD_MULTIAXIS;
3005 }
3006 }
3007
3008 return 0;
3009 }
3010
3011
3012 static void hidpp_populate_input(struct hidpp_device *hidpp,
3013 struct input_dev *input)
3014 {
3015 hidpp->input = input;
3016
3017 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3018 wtp_populate_input(hidpp, input);
3019 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3020 m560_populate_input(hidpp, input);
3021
3022 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS)
3023 hidpp10_wheel_populate_input(hidpp, input);
3024
3025 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS)
3026 hidpp10_extra_mouse_buttons_populate_input(hidpp, input);
3027 }
3028
3029 static int hidpp_input_configured(struct hid_device *hdev,
3030 struct hid_input *hidinput)
3031 {
3032 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3033 struct input_dev *input = hidinput->input;
3034
3035 if (!hidpp)
3036 return 0;
3037
3038 hidpp_populate_input(hidpp, input);
3039
3040 return 0;
3041 }
3042
3043 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data,
3044 int size)
3045 {
3046 struct hidpp_report *question = hidpp->send_receive_buf;
3047 struct hidpp_report *answer = hidpp->send_receive_buf;
3048 struct hidpp_report *report = (struct hidpp_report *)data;
3049 int ret;
3050
3051 /*
3052 * If the mutex is locked then we have a pending answer from a
3053 * previously sent command.
3054 */
3055 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) {
3056 /*
3057 * Check for a correct hidpp20 answer or the corresponding
3058 * error
3059 */
3060 if (hidpp_match_answer(question, report) ||
3061 hidpp_match_error(question, report)) {
3062 *answer = *report;
3063 hidpp->answer_available = true;
3064 wake_up(&hidpp->wait);
3065 /*
3066 * This was an answer to a command that this driver sent
3067 * We return 1 to hid-core to avoid forwarding the
3068 * command upstream as it has been treated by the driver
3069 */
3070
3071 return 1;
3072 }
3073 }
3074
3075 if (unlikely(hidpp_report_is_connect_event(report))) {
3076 atomic_set(&hidpp->connected,
3077 !(report->rap.params[0] & (1 << 6)));
3078 if (schedule_work(&hidpp->work) == 0)
3079 dbg_hid("%s: connect event already queued\n", __func__);
3080 return 1;
3081 }
3082
3083 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3084 ret = hidpp20_battery_event(hidpp, data, size);
3085 if (ret != 0)
3086 return ret;
3087 ret = hidpp_solar_battery_event(hidpp, data, size);
3088 if (ret != 0)
3089 return ret;
3090 }
3091
3092 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3093 ret = hidpp10_battery_event(hidpp, data, size);
3094 if (ret != 0)
3095 return ret;
3096 }
3097
3098 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3099 ret = hidpp10_wheel_raw_event(hidpp, data, size);
3100 if (ret != 0)
3101 return ret;
3102 }
3103
3104 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3105 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size);
3106 if (ret != 0)
3107 return ret;
3108 }
3109
3110 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3111 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size);
3112 if (ret != 0)
3113 return ret;
3114 }
3115
3116 return 0;
3117 }
3118
3119 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report,
3120 u8 *data, int size)
3121 {
3122 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3123 int ret = 0;
3124
3125 if (!hidpp)
3126 return 0;
3127
3128 /* Generic HID++ processing. */
3129 switch (data[0]) {
3130 case REPORT_ID_HIDPP_VERY_LONG:
3131 if (size != hidpp->very_long_report_length) {
3132 hid_err(hdev, "received hid++ report of bad size (%d)",
3133 size);
3134 return 1;
3135 }
3136 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3137 break;
3138 case REPORT_ID_HIDPP_LONG:
3139 if (size != HIDPP_REPORT_LONG_LENGTH) {
3140 hid_err(hdev, "received hid++ report of bad size (%d)",
3141 size);
3142 return 1;
3143 }
3144 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3145 break;
3146 case REPORT_ID_HIDPP_SHORT:
3147 if (size != HIDPP_REPORT_SHORT_LENGTH) {
3148 hid_err(hdev, "received hid++ report of bad size (%d)",
3149 size);
3150 return 1;
3151 }
3152 ret = hidpp_raw_hidpp_event(hidpp, data, size);
3153 break;
3154 }
3155
3156 /* If no report is available for further processing, skip calling
3157 * raw_event of subclasses. */
3158 if (ret != 0)
3159 return ret;
3160
3161 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)
3162 return wtp_raw_event(hdev, data, size);
3163 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560)
3164 return m560_raw_event(hdev, data, size);
3165
3166 return 0;
3167 }
3168
3169 static int hidpp_event(struct hid_device *hdev, struct hid_field *field,
3170 struct hid_usage *usage, __s32 value)
3171 {
3172 /* This function will only be called for scroll events, due to the
3173 * restriction imposed in hidpp_usages.
3174 */
3175 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3176 struct hidpp_scroll_counter *counter;
3177
3178 if (!hidpp)
3179 return 0;
3180
3181 counter = &hidpp->vertical_wheel_counter;
3182 /* A scroll event may occur before the multiplier has been retrieved or
3183 * the input device set, or high-res scroll enabling may fail. In such
3184 * cases we must return early (falling back to default behaviour) to
3185 * avoid a crash in hidpp_scroll_counter_handle_scroll.
3186 */
3187 if (!(hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL) || value == 0
3188 || hidpp->input == NULL || counter->wheel_multiplier == 0)
3189 return 0;
3190
3191 hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value);
3192 return 1;
3193 }
3194
3195 static int hidpp_initialize_battery(struct hidpp_device *hidpp)
3196 {
3197 static atomic_t battery_no = ATOMIC_INIT(0);
3198 struct power_supply_config cfg = { .drv_data = hidpp };
3199 struct power_supply_desc *desc = &hidpp->battery.desc;
3200 enum power_supply_property *battery_props;
3201 struct hidpp_battery *battery;
3202 unsigned int num_battery_props;
3203 unsigned long n;
3204 int ret;
3205
3206 if (hidpp->battery.ps)
3207 return 0;
3208
3209 hidpp->battery.feature_index = 0xff;
3210 hidpp->battery.solar_feature_index = 0xff;
3211
3212 if (hidpp->protocol_major >= 2) {
3213 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750)
3214 ret = hidpp_solar_request_battery_event(hidpp);
3215 else
3216 ret = hidpp20_query_battery_info(hidpp);
3217
3218 if (ret)
3219 return ret;
3220 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY;
3221 } else {
3222 ret = hidpp10_query_battery_status(hidpp);
3223 if (ret) {
3224 ret = hidpp10_query_battery_mileage(hidpp);
3225 if (ret)
3226 return -ENOENT;
3227 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE;
3228 } else {
3229 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS;
3230 }
3231 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY;
3232 }
3233
3234 battery_props = devm_kmemdup(&hidpp->hid_dev->dev,
3235 hidpp_battery_props,
3236 sizeof(hidpp_battery_props),
3237 GFP_KERNEL);
3238 if (!battery_props)
3239 return -ENOMEM;
3240
3241 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 2;
3242
3243 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3244 battery_props[num_battery_props++] =
3245 POWER_SUPPLY_PROP_CAPACITY;
3246
3247 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS)
3248 battery_props[num_battery_props++] =
3249 POWER_SUPPLY_PROP_CAPACITY_LEVEL;
3250
3251 battery = &hidpp->battery;
3252
3253 n = atomic_inc_return(&battery_no) - 1;
3254 desc->properties = battery_props;
3255 desc->num_properties = num_battery_props;
3256 desc->get_property = hidpp_battery_get_property;
3257 sprintf(battery->name, "hidpp_battery_%ld", n);
3258 desc->name = battery->name;
3259 desc->type = POWER_SUPPLY_TYPE_BATTERY;
3260 desc->use_for_apm = 0;
3261
3262 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev,
3263 &battery->desc,
3264 &cfg);
3265 if (IS_ERR(battery->ps))
3266 return PTR_ERR(battery->ps);
3267
3268 power_supply_powers(battery->ps, &hidpp->hid_dev->dev);
3269
3270 return ret;
3271 }
3272
3273 static void hidpp_overwrite_name(struct hid_device *hdev)
3274 {
3275 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3276 char *name;
3277
3278 if (hidpp->protocol_major < 2)
3279 return;
3280
3281 name = hidpp_get_device_name(hidpp);
3282
3283 if (!name) {
3284 hid_err(hdev, "unable to retrieve the name of the device");
3285 } else {
3286 dbg_hid("HID++: Got name: %s\n", name);
3287 snprintf(hdev->name, sizeof(hdev->name), "%s", name);
3288 }
3289
3290 kfree(name);
3291 }
3292
3293 static int hidpp_input_open(struct input_dev *dev)
3294 {
3295 struct hid_device *hid = input_get_drvdata(dev);
3296
3297 return hid_hw_open(hid);
3298 }
3299
3300 static void hidpp_input_close(struct input_dev *dev)
3301 {
3302 struct hid_device *hid = input_get_drvdata(dev);
3303
3304 hid_hw_close(hid);
3305 }
3306
3307 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev)
3308 {
3309 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev);
3310 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3311
3312 if (!input_dev)
3313 return NULL;
3314
3315 input_set_drvdata(input_dev, hdev);
3316 input_dev->open = hidpp_input_open;
3317 input_dev->close = hidpp_input_close;
3318
3319 input_dev->name = hidpp->name;
3320 input_dev->phys = hdev->phys;
3321 input_dev->uniq = hdev->uniq;
3322 input_dev->id.bustype = hdev->bus;
3323 input_dev->id.vendor = hdev->vendor;
3324 input_dev->id.product = hdev->product;
3325 input_dev->id.version = hdev->version;
3326 input_dev->dev.parent = &hdev->dev;
3327
3328 return input_dev;
3329 }
3330
3331 static void hidpp_connect_event(struct hidpp_device *hidpp)
3332 {
3333 struct hid_device *hdev = hidpp->hid_dev;
3334 int ret = 0;
3335 bool connected = atomic_read(&hidpp->connected);
3336 struct input_dev *input;
3337 char *name, *devm_name;
3338
3339 if (!connected) {
3340 if (hidpp->battery.ps) {
3341 hidpp->battery.online = false;
3342 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN;
3343 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
3344 power_supply_changed(hidpp->battery.ps);
3345 }
3346 return;
3347 }
3348
3349 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3350 ret = wtp_connect(hdev, connected);
3351 if (ret)
3352 return;
3353 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) {
3354 ret = m560_send_config_command(hdev, connected);
3355 if (ret)
3356 return;
3357 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3358 ret = k400_connect(hdev, connected);
3359 if (ret)
3360 return;
3361 }
3362
3363 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) {
3364 ret = hidpp10_wheel_connect(hidpp);
3365 if (ret)
3366 return;
3367 }
3368
3369 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) {
3370 ret = hidpp10_extra_mouse_buttons_connect(hidpp);
3371 if (ret)
3372 return;
3373 }
3374
3375 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) {
3376 ret = hidpp10_consumer_keys_connect(hidpp);
3377 if (ret)
3378 return;
3379 }
3380
3381 /* the device is already connected, we can ask for its name and
3382 * protocol */
3383 if (!hidpp->protocol_major) {
3384 ret = hidpp_root_get_protocol_version(hidpp);
3385 if (ret) {
3386 hid_err(hdev, "Can not get the protocol version.\n");
3387 return;
3388 }
3389 }
3390
3391 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) {
3392 name = hidpp_get_device_name(hidpp);
3393 if (name) {
3394 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
3395 "%s", name);
3396 kfree(name);
3397 if (!devm_name)
3398 return;
3399
3400 hidpp->name = devm_name;
3401 }
3402 }
3403
3404 hidpp_initialize_battery(hidpp);
3405
3406 /* forward current battery state */
3407 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) {
3408 hidpp10_enable_battery_reporting(hidpp);
3409 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE)
3410 hidpp10_query_battery_mileage(hidpp);
3411 else
3412 hidpp10_query_battery_status(hidpp);
3413 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) {
3414 hidpp20_query_battery_info(hidpp);
3415 }
3416 if (hidpp->battery.ps)
3417 power_supply_changed(hidpp->battery.ps);
3418
3419 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL)
3420 hi_res_scroll_enable(hidpp);
3421
3422 if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) || hidpp->delayed_input)
3423 /* if the input nodes are already created, we can stop now */
3424 return;
3425
3426 input = hidpp_allocate_input(hdev);
3427 if (!input) {
3428 hid_err(hdev, "cannot allocate new input device: %d\n", ret);
3429 return;
3430 }
3431
3432 hidpp_populate_input(hidpp, input);
3433
3434 ret = input_register_device(input);
3435 if (ret)
3436 input_free_device(input);
3437
3438 hidpp->delayed_input = input;
3439 }
3440
3441 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL);
3442
3443 static struct attribute *sysfs_attrs[] = {
3444 &dev_attr_builtin_power_supply.attr,
3445 NULL
3446 };
3447
3448 static const struct attribute_group ps_attribute_group = {
3449 .attrs = sysfs_attrs
3450 };
3451
3452 static int hidpp_get_report_length(struct hid_device *hdev, int id)
3453 {
3454 struct hid_report_enum *re;
3455 struct hid_report *report;
3456
3457 re = &(hdev->report_enum[HID_OUTPUT_REPORT]);
3458 report = re->report_id_hash[id];
3459 if (!report)
3460 return 0;
3461
3462 return report->field[0]->report_count + 1;
3463 }
3464
3465 static bool hidpp_validate_report(struct hid_device *hdev, int id,
3466 int expected_length, bool optional)
3467 {
3468 int report_length;
3469
3470 if (id >= HID_MAX_IDS || id < 0) {
3471 hid_err(hdev, "invalid HID report id %u\n", id);
3472 return false;
3473 }
3474
3475 report_length = hidpp_get_report_length(hdev, id);
3476 if (!report_length)
3477 return optional;
3478
3479 if (report_length < expected_length) {
3480 hid_warn(hdev, "not enough values in hidpp report %d\n", id);
3481 return false;
3482 }
3483
3484 return true;
3485 }
3486
3487 static bool hidpp_validate_device(struct hid_device *hdev)
3488 {
3489 return hidpp_validate_report(hdev, REPORT_ID_HIDPP_SHORT,
3490 HIDPP_REPORT_SHORT_LENGTH, false) &&
3491 hidpp_validate_report(hdev, REPORT_ID_HIDPP_LONG,
3492 HIDPP_REPORT_LONG_LENGTH, true);
3493 }
3494
3495 static bool hidpp_application_equals(struct hid_device *hdev,
3496 unsigned int application)
3497 {
3498 struct list_head *report_list;
3499 struct hid_report *report;
3500
3501 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list;
3502 report = list_first_entry_or_null(report_list, struct hid_report, list);
3503 return report && report->application == application;
3504 }
3505
3506 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
3507 {
3508 struct hidpp_device *hidpp;
3509 int ret;
3510 bool connected;
3511 unsigned int connect_mask = HID_CONNECT_DEFAULT;
3512
3513 /* report_fixup needs drvdata to be set before we call hid_parse */
3514 hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL);
3515 if (!hidpp)
3516 return -ENOMEM;
3517
3518 hidpp->hid_dev = hdev;
3519 hidpp->name = hdev->name;
3520 hidpp->quirks = id->driver_data;
3521 hid_set_drvdata(hdev, hidpp);
3522
3523 ret = hid_parse(hdev);
3524 if (ret) {
3525 hid_err(hdev, "%s:parse failed\n", __func__);
3526 return ret;
3527 }
3528
3529 /*
3530 * Make sure the device is HID++ capable, otherwise treat as generic HID
3531 */
3532 if (!hidpp_validate_device(hdev)) {
3533 hid_set_drvdata(hdev, NULL);
3534 devm_kfree(&hdev->dev, hidpp);
3535 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
3536 }
3537
3538 hidpp->very_long_report_length =
3539 hidpp_get_report_length(hdev, REPORT_ID_HIDPP_VERY_LONG);
3540 if (hidpp->very_long_report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH)
3541 hidpp->very_long_report_length = HIDPP_REPORT_VERY_LONG_MAX_LENGTH;
3542
3543 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE)
3544 hidpp->quirks |= HIDPP_QUIRK_UNIFYING;
3545
3546 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3547 hidpp_application_equals(hdev, HID_GD_MOUSE))
3548 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS |
3549 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS;
3550
3551 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE &&
3552 hidpp_application_equals(hdev, HID_GD_KEYBOARD))
3553 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS;
3554
3555 if (disable_raw_mode) {
3556 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP;
3557 hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT;
3558 }
3559
3560 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) {
3561 ret = wtp_allocate(hdev, id);
3562 if (ret)
3563 return ret;
3564 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) {
3565 ret = k400_allocate(hdev);
3566 if (ret)
3567 return ret;
3568 }
3569
3570 INIT_WORK(&hidpp->work, delayed_work_cb);
3571 mutex_init(&hidpp->send_mutex);
3572 init_waitqueue_head(&hidpp->wait);
3573
3574 /* indicates we are handling the battery properties in the kernel */
3575 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group);
3576 if (ret)
3577 hid_warn(hdev, "Cannot allocate sysfs group for %s\n",
3578 hdev->name);
3579
3580 /*
3581 * Plain USB connections need to actually call start and open
3582 * on the transport driver to allow incoming data.
3583 */
3584 ret = hid_hw_start(hdev, 0);
3585 if (ret) {
3586 hid_err(hdev, "hw start failed\n");
3587 goto hid_hw_start_fail;
3588 }
3589
3590 ret = hid_hw_open(hdev);
3591 if (ret < 0) {
3592 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n",
3593 __func__, ret);
3594 hid_hw_stop(hdev);
3595 goto hid_hw_open_fail;
3596 }
3597
3598 /* Allow incoming packets */
3599 hid_device_io_start(hdev);
3600
3601 if (hidpp->quirks & HIDPP_QUIRK_UNIFYING)
3602 hidpp_unifying_init(hidpp);
3603
3604 connected = hidpp_root_get_protocol_version(hidpp) == 0;
3605 atomic_set(&hidpp->connected, connected);
3606 if (!(hidpp->quirks & HIDPP_QUIRK_UNIFYING)) {
3607 if (!connected) {
3608 ret = -ENODEV;
3609 hid_err(hdev, "Device not connected");
3610 goto hid_hw_init_fail;
3611 }
3612
3613 hidpp_overwrite_name(hdev);
3614 }
3615
3616 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) {
3617 ret = wtp_get_config(hidpp);
3618 if (ret)
3619 goto hid_hw_init_fail;
3620 } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) {
3621 ret = g920_get_config(hidpp);
3622 if (ret)
3623 goto hid_hw_init_fail;
3624 }
3625
3626 hidpp_connect_event(hidpp);
3627
3628 /* Reset the HID node state */
3629 hid_device_io_stop(hdev);
3630 hid_hw_close(hdev);
3631 hid_hw_stop(hdev);
3632
3633 if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)
3634 connect_mask &= ~HID_CONNECT_HIDINPUT;
3635
3636 /* Now export the actual inputs and hidraw nodes to the world */
3637 ret = hid_hw_start(hdev, connect_mask);
3638 if (ret) {
3639 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__);
3640 goto hid_hw_start_fail;
3641 }
3642
3643 return ret;
3644
3645 hid_hw_init_fail:
3646 hid_hw_close(hdev);
3647 hid_hw_open_fail:
3648 hid_hw_stop(hdev);
3649 hid_hw_start_fail:
3650 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3651 cancel_work_sync(&hidpp->work);
3652 mutex_destroy(&hidpp->send_mutex);
3653 return ret;
3654 }
3655
3656 static void hidpp_remove(struct hid_device *hdev)
3657 {
3658 struct hidpp_device *hidpp = hid_get_drvdata(hdev);
3659
3660 if (!hidpp)
3661 return hid_hw_stop(hdev);
3662
3663 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group);
3664
3665 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)
3666 hidpp_ff_deinit(hdev);
3667
3668 hid_hw_stop(hdev);
3669 cancel_work_sync(&hidpp->work);
3670 mutex_destroy(&hidpp->send_mutex);
3671 }
3672
3673 #define LDJ_DEVICE(product) \
3674 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \
3675 USB_VENDOR_ID_LOGITECH, (product))
3676
3677 #define L27MHZ_DEVICE(product) \
3678 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \
3679 USB_VENDOR_ID_LOGITECH, (product))
3680
3681 static const struct hid_device_id hidpp_devices[] = {
3682 { /* wireless touchpad */
3683 LDJ_DEVICE(0x4011),
3684 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT |
3685 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS },
3686 { /* wireless touchpad T650 */
3687 LDJ_DEVICE(0x4101),
3688 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT },
3689 { /* wireless touchpad T651 */
3690 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH,
3691 USB_DEVICE_ID_LOGITECH_T651),
3692 .driver_data = HIDPP_QUIRK_CLASS_WTP },
3693 { /* Mouse Logitech Anywhere MX */
3694 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3695 { /* Mouse Logitech Cube */
3696 LDJ_DEVICE(0x4010), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3697 { /* Mouse Logitech M335 */
3698 LDJ_DEVICE(0x4050), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3699 { /* Mouse Logitech M515 */
3700 LDJ_DEVICE(0x4007), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3701 { /* Mouse logitech M560 */
3702 LDJ_DEVICE(0x402d),
3703 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560
3704 | HIDPP_QUIRK_HI_RES_SCROLL_X2120 },
3705 { /* Mouse Logitech M705 (firmware RQM17) */
3706 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3707 { /* Mouse Logitech M705 (firmware RQM67) */
3708 LDJ_DEVICE(0x406d), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3709 { /* Mouse Logitech M720 */
3710 LDJ_DEVICE(0x405e), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3711 { /* Mouse Logitech MX Anywhere 2 */
3712 LDJ_DEVICE(0x404a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3713 { LDJ_DEVICE(0xb013), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3714 { LDJ_DEVICE(0xb018), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3715 { LDJ_DEVICE(0xb01f), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3716 { /* Mouse Logitech MX Anywhere 2S */
3717 LDJ_DEVICE(0x406a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3718 { /* Mouse Logitech MX Master */
3719 LDJ_DEVICE(0x4041), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3720 { LDJ_DEVICE(0x4060), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3721 { LDJ_DEVICE(0x4071), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3722 { /* Mouse Logitech MX Master 2S */
3723 LDJ_DEVICE(0x4069), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_X2121 },
3724 { /* Mouse Logitech Performance MX */
3725 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 },
3726 { /* Keyboard logitech K400 */
3727 LDJ_DEVICE(0x4024),
3728 .driver_data = HIDPP_QUIRK_CLASS_K400 },
3729 { /* Solar Keyboard Logitech K750 */
3730 LDJ_DEVICE(0x4002),
3731 .driver_data = HIDPP_QUIRK_CLASS_K750 },
3732 { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */
3733 LDJ_DEVICE(0xb305),
3734 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
3735
3736 { LDJ_DEVICE(HID_ANY_ID) },
3737
3738 { /* Keyboard LX501 (Y-RR53) */
3739 L27MHZ_DEVICE(0x0049),
3740 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
3741 { /* Keyboard MX3000 (Y-RAM74) */
3742 L27MHZ_DEVICE(0x0057),
3743 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL },
3744 { /* Keyboard MX3200 (Y-RAV80) */
3745 L27MHZ_DEVICE(0x005c),
3746 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL },
3747
3748 { L27MHZ_DEVICE(HID_ANY_ID) },
3749
3750 { /* Logitech G403 Gaming Mouse over USB */
3751 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) },
3752 { /* Logitech G700 Gaming Mouse over USB */
3753 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC06B) },
3754 { /* Logitech G900 Gaming Mouse over USB */
3755 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) },
3756 { /* Logitech G920 Wheel over USB */
3757 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL),
3758 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS},
3759
3760 { /* MX5000 keyboard over Bluetooth */
3761 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305),
3762 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS },
3763 {}
3764 };
3765
3766 MODULE_DEVICE_TABLE(hid, hidpp_devices);
3767
3768 static const struct hid_usage_id hidpp_usages[] = {
3769 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES },
3770 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
3771 };
3772
3773 static struct hid_driver hidpp_driver = {
3774 .name = "logitech-hidpp-device",
3775 .id_table = hidpp_devices,
3776 .report_fixup = hidpp_report_fixup,
3777 .probe = hidpp_probe,
3778 .remove = hidpp_remove,
3779 .raw_event = hidpp_raw_event,
3780 .usage_table = hidpp_usages,
3781 .event = hidpp_event,
3782 .input_configured = hidpp_input_configured,
3783 .input_mapping = hidpp_input_mapping,
3784 .input_mapped = hidpp_input_mapped,
3785 };
3786
3787 module_hid_driver(hidpp_driver);