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