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