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