1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * HIDPP protocol for Logitech receivers 4 * 5 * Copyright (c) 2011 Logitech (c) 6 * Copyright (c) 2012-2013 Google (c) 7 * Copyright (c) 2013-2014 Red Hat Inc. 8 */ 9 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/device.h> 14 #include <linux/input.h> 15 #include <linux/usb.h> 16 #include <linux/hid.h> 17 #include <linux/module.h> 18 #include <linux/slab.h> 19 #include <linux/sched.h> 20 #include <linux/sched/clock.h> 21 #include <linux/kfifo.h> 22 #include <linux/input/mt.h> 23 #include <linux/workqueue.h> 24 #include <linux/atomic.h> 25 #include <linux/fixp-arith.h> 26 #include <linux/unaligned.h> 27 #include "usbhid/usbhid.h" 28 #include "hid-ids.h" 29 30 MODULE_DESCRIPTION("Support for Logitech devices relying on the HID++ specification"); 31 MODULE_LICENSE("GPL"); 32 MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); 33 MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>"); 34 MODULE_AUTHOR("Bastien Nocera <hadess@hadess.net>"); 35 36 static bool disable_tap_to_click; 37 module_param(disable_tap_to_click, bool, 0644); 38 MODULE_PARM_DESC(disable_tap_to_click, 39 "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently)."); 40 41 /* Define a non-zero software ID to identify our own requests */ 42 #define LINUX_KERNEL_SW_ID 0x01 43 44 #define REPORT_ID_HIDPP_SHORT 0x10 45 #define REPORT_ID_HIDPP_LONG 0x11 46 #define REPORT_ID_HIDPP_VERY_LONG 0x12 47 48 #define HIDPP_REPORT_SHORT_LENGTH 7 49 #define HIDPP_REPORT_LONG_LENGTH 20 50 #define HIDPP_REPORT_VERY_LONG_MAX_LENGTH 64 51 52 #define HIDPP_REPORT_SHORT_SUPPORTED BIT(0) 53 #define HIDPP_REPORT_LONG_SUPPORTED BIT(1) 54 #define HIDPP_REPORT_VERY_LONG_SUPPORTED BIT(2) 55 56 #define HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS 0x03 57 #define HIDPP_SUB_ID_ROLLER 0x05 58 #define HIDPP_SUB_ID_MOUSE_EXTRA_BTNS 0x06 59 #define HIDPP_SUB_ID_USER_IFACE_EVENT 0x08 60 #define HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST BIT(5) 61 62 #define HIDPP_QUIRK_CLASS_WTP BIT(0) 63 #define HIDPP_QUIRK_CLASS_M560 BIT(1) 64 #define HIDPP_QUIRK_CLASS_K400 BIT(2) 65 #define HIDPP_QUIRK_CLASS_G920 BIT(3) 66 #define HIDPP_QUIRK_CLASS_K750 BIT(4) 67 68 /* bits 2..20 are reserved for classes */ 69 /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */ 70 #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22) 71 #define HIDPP_QUIRK_DELAYED_INIT BIT(23) 72 #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24) 73 #define HIDPP_QUIRK_HIDPP_WHEELS BIT(25) 74 #define HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS BIT(26) 75 #define HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS BIT(27) 76 #define HIDPP_QUIRK_HI_RES_SCROLL_1P0 BIT(28) 77 #define HIDPP_QUIRK_WIRELESS_STATUS BIT(29) 78 #define HIDPP_QUIRK_RESET_HI_RES_SCROLL BIT(30) 79 #define HIDPP_QUIRK_HIDPP_REPROG_CONTROLS_BTNS BIT(31) 80 81 /* These are just aliases for now */ 82 #define HIDPP_QUIRK_KBD_SCROLL_WHEEL HIDPP_QUIRK_HIDPP_WHEELS 83 #define HIDPP_QUIRK_KBD_ZOOM_WHEEL HIDPP_QUIRK_HIDPP_WHEELS 84 85 /* Convenience constant to check for any high-res support. */ 86 #define HIDPP_CAPABILITY_HI_RES_SCROLL (HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL | \ 87 HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL | \ 88 HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL) 89 90 #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0) 91 #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1) 92 #define HIDPP_CAPABILITY_BATTERY_MILEAGE BIT(2) 93 #define HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS BIT(3) 94 #define HIDPP_CAPABILITY_BATTERY_VOLTAGE BIT(4) 95 #define HIDPP_CAPABILITY_BATTERY_PERCENTAGE BIT(5) 96 #define HIDPP_CAPABILITY_UNIFIED_BATTERY BIT(6) 97 #define HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL BIT(7) 98 #define HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL BIT(8) 99 #define HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL BIT(9) 100 #define HIDPP_CAPABILITY_ADC_MEASUREMENT BIT(10) 101 102 #define lg_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c)) 103 104 /* 105 * There are two hidpp protocols in use, the first version hidpp10 is known 106 * as register access protocol or RAP, the second version hidpp20 is known as 107 * feature access protocol or FAP 108 * 109 * Most older devices (including the Unifying usb receiver) use the RAP protocol 110 * where as most newer devices use the FAP protocol. Both protocols are 111 * compatible with the underlying transport, which could be usb, Unifiying, or 112 * bluetooth. The message lengths are defined by the hid vendor specific report 113 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and 114 * the HIDPP_LONG report type (total message length 20 bytes) 115 * 116 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG 117 * messages. The Unifying receiver itself responds to RAP messages (device index 118 * is 0xFF for the receiver), and all messages (short or long) with a device 119 * index between 1 and 6 are passed untouched to the corresponding paired 120 * Unifying device. 121 * 122 * The paired device can be RAP or FAP, it will receive the message untouched 123 * from the Unifiying receiver. 124 */ 125 126 struct fap { 127 u8 feature_index; 128 u8 funcindex_clientid; 129 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U]; 130 }; 131 132 struct rap { 133 u8 sub_id; 134 u8 reg_address; 135 u8 params[HIDPP_REPORT_VERY_LONG_MAX_LENGTH - 4U]; 136 }; 137 138 struct hidpp_report { 139 u8 report_id; 140 u8 device_index; 141 union { 142 struct fap fap; 143 struct rap rap; 144 u8 rawbytes[sizeof(struct fap)]; 145 }; 146 } __packed; 147 148 struct hidpp_battery { 149 u8 feature_index; 150 u8 solar_feature_index; 151 u8 voltage_feature_index; 152 u8 adc_measurement_feature_index; 153 struct power_supply_desc desc; 154 struct power_supply *ps; 155 char name[64]; 156 int status; 157 int capacity; 158 int level; 159 int voltage; 160 int charge_type; 161 bool online; 162 u8 supported_levels_1004; 163 }; 164 165 /** 166 * struct hidpp_scroll_counter - Utility class for processing high-resolution 167 * scroll events. 168 * @wheel_multiplier: the scalar multiplier to be applied to each wheel event 169 * @remainder: counts the number of high-resolution units moved since the last 170 * low-resolution event (REL_WHEEL or REL_HWHEEL) was sent. Should 171 * only be used by class methods. 172 * @direction: direction of last movement (1 or -1) 173 * @last_time: last event time, used to reset remainder after inactivity 174 */ 175 struct hidpp_scroll_counter { 176 int wheel_multiplier; 177 int remainder; 178 int direction; 179 unsigned long long last_time; 180 }; 181 182 struct hidpp_reprog_control_mapping; 183 184 struct hidpp_device { 185 struct hid_device *hid_dev; 186 struct input_dev *input; 187 struct mutex send_mutex; 188 void *send_receive_buf; 189 char *name; /* will never be NULL and should not be freed */ 190 wait_queue_head_t wait; 191 int very_long_report_length; 192 bool answer_available; 193 u8 protocol_major; 194 u8 protocol_minor; 195 196 void *private_data; 197 198 struct work_struct work; 199 struct work_struct reset_hi_res_work; 200 struct kfifo delayed_work_fifo; 201 struct input_dev *delayed_input; 202 203 unsigned long quirks; 204 unsigned long capabilities; 205 u8 supported_reports; 206 207 struct hidpp_battery battery; 208 struct hidpp_scroll_counter vertical_wheel_counter; 209 210 u8 wireless_feature_index; 211 u8 reprog_controls_feature_index; 212 const struct hidpp_reprog_control_mapping *reprog_controls; 213 214 int hires_wheel_multiplier; 215 u8 hires_wheel_feature_index; 216 217 bool connected_once; 218 }; 219 220 /* HID++ 1.0 error codes */ 221 #define HIDPP_ERROR 0x8f 222 #define HIDPP_ERROR_SUCCESS 0x00 223 #define HIDPP_ERROR_INVALID_SUBID 0x01 224 #define HIDPP_ERROR_INVALID_ADRESS 0x02 225 #define HIDPP_ERROR_INVALID_VALUE 0x03 226 #define HIDPP_ERROR_CONNECT_FAIL 0x04 227 #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05 228 #define HIDPP_ERROR_ALREADY_EXISTS 0x06 229 #define HIDPP_ERROR_BUSY 0x07 230 #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08 231 #define HIDPP_ERROR_RESOURCE_ERROR 0x09 232 #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a 233 #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b 234 #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c 235 /* HID++ 2.0 error codes */ 236 #define HIDPP20_ERROR_NO_ERROR 0x00 237 #define HIDPP20_ERROR_UNKNOWN 0x01 238 #define HIDPP20_ERROR_INVALID_ARGS 0x02 239 #define HIDPP20_ERROR_OUT_OF_RANGE 0x03 240 #define HIDPP20_ERROR_HW_ERROR 0x04 241 #define HIDPP20_ERROR_NOT_ALLOWED 0x05 242 #define HIDPP20_ERROR_INVALID_FEATURE_INDEX 0x06 243 #define HIDPP20_ERROR_INVALID_FUNCTION_ID 0x07 244 #define HIDPP20_ERROR_BUSY 0x08 245 #define HIDPP20_ERROR_UNSUPPORTED 0x09 246 #define HIDPP20_ERROR 0xff 247 248 static int __hidpp_send_report(struct hid_device *hdev, 249 struct hidpp_report *hidpp_report) 250 { 251 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 252 int fields_count, ret; 253 254 switch (hidpp_report->report_id) { 255 case REPORT_ID_HIDPP_SHORT: 256 fields_count = HIDPP_REPORT_SHORT_LENGTH; 257 break; 258 case REPORT_ID_HIDPP_LONG: 259 fields_count = HIDPP_REPORT_LONG_LENGTH; 260 break; 261 case REPORT_ID_HIDPP_VERY_LONG: 262 fields_count = hidpp->very_long_report_length; 263 break; 264 default: 265 return -ENODEV; 266 } 267 268 /* 269 * set the device_index as the receiver, it will be overwritten by 270 * hid_hw_request if needed 271 */ 272 hidpp_report->device_index = 0xff; 273 274 if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) { 275 ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count); 276 } else { 277 ret = hid_hw_raw_request(hdev, hidpp_report->report_id, 278 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT, 279 HID_REQ_SET_REPORT); 280 } 281 282 return ret == fields_count ? 0 : -1; 283 } 284 285 /* 286 * Effectively send the message to the device, waiting for its answer. 287 * 288 * Must be called with hidpp->send_mutex locked 289 * 290 * Same return protocol than hidpp_send_message_sync(): 291 * - success on 0 292 * - negative error means transport error 293 * - positive value means protocol error 294 */ 295 static int __do_hidpp_send_message_sync(struct hidpp_device *hidpp, 296 struct hidpp_report *message, 297 struct hidpp_report *response) 298 { 299 int ret; 300 301 __must_hold(&hidpp->send_mutex); 302 303 hidpp->send_receive_buf = response; 304 hidpp->answer_available = false; 305 306 /* 307 * So that we can later validate the answer when it arrives 308 * in hidpp_raw_event 309 */ 310 *response = *message; 311 312 ret = __hidpp_send_report(hidpp->hid_dev, message); 313 if (ret) { 314 dbg_hid("__hidpp_send_report returned err: %d\n", ret); 315 memset(response, 0, sizeof(struct hidpp_report)); 316 goto out; 317 } 318 319 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available, 320 5*HZ)) { 321 dbg_hid("%s:timeout waiting for response\n", __func__); 322 memset(response, 0, sizeof(struct hidpp_report)); 323 ret = -ETIMEDOUT; 324 goto out; 325 } 326 327 if (response->report_id == REPORT_ID_HIDPP_SHORT && 328 response->rap.sub_id == HIDPP_ERROR) { 329 ret = response->rap.params[1]; 330 dbg_hid("%s:got hidpp error %02X\n", __func__, ret); 331 goto out; 332 } 333 334 if ((response->report_id == REPORT_ID_HIDPP_LONG || 335 response->report_id == REPORT_ID_HIDPP_VERY_LONG) && 336 response->fap.feature_index == HIDPP20_ERROR) { 337 ret = response->fap.params[1]; 338 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret); 339 goto out; 340 } 341 342 ret = 0; 343 344 out: 345 hidpp->send_receive_buf = NULL; 346 return ret; 347 } 348 349 /* 350 * hidpp_send_message_sync() returns 0 in case of success, and something else 351 * in case of a failure. 352 * 353 * See __do_hidpp_send_message_sync() for a detailed explanation of the returned 354 * value. 355 */ 356 static int hidpp_send_message_sync(struct hidpp_device *hidpp, 357 struct hidpp_report *message, 358 struct hidpp_report *response) 359 { 360 int ret; 361 int max_retries = 3; 362 363 mutex_lock(&hidpp->send_mutex); 364 365 do { 366 ret = __do_hidpp_send_message_sync(hidpp, message, response); 367 if (response->report_id == REPORT_ID_HIDPP_SHORT && 368 ret != HIDPP_ERROR_BUSY) 369 break; 370 if ((response->report_id == REPORT_ID_HIDPP_LONG || 371 response->report_id == REPORT_ID_HIDPP_VERY_LONG) && 372 ret != HIDPP20_ERROR_BUSY) 373 break; 374 375 dbg_hid("%s:got busy hidpp error %02X, retrying\n", __func__, ret); 376 } while (--max_retries); 377 378 mutex_unlock(&hidpp->send_mutex); 379 return ret; 380 381 } 382 383 /* 384 * hidpp_send_fap_command_sync() returns 0 in case of success, and something else 385 * in case of a failure. 386 * 387 * See __do_hidpp_send_message_sync() for a detailed explanation of the returned 388 * value. 389 */ 390 static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp, 391 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count, 392 struct hidpp_report *response) 393 { 394 struct hidpp_report *message; 395 int ret; 396 397 if (param_count > sizeof(message->fap.params)) { 398 hid_dbg(hidpp->hid_dev, 399 "Invalid number of parameters passed to command (%d != %llu)\n", 400 param_count, 401 (unsigned long long) sizeof(message->fap.params)); 402 return -EINVAL; 403 } 404 405 message = kzalloc_obj(struct hidpp_report); 406 if (!message) 407 return -ENOMEM; 408 409 if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4)) 410 message->report_id = REPORT_ID_HIDPP_VERY_LONG; 411 else 412 message->report_id = REPORT_ID_HIDPP_LONG; 413 message->fap.feature_index = feat_index; 414 message->fap.funcindex_clientid = funcindex_clientid | LINUX_KERNEL_SW_ID; 415 memcpy(&message->fap.params, params, param_count); 416 417 ret = hidpp_send_message_sync(hidpp, message, response); 418 kfree(message); 419 return ret; 420 } 421 422 /* 423 * hidpp_send_rap_command_sync() returns 0 in case of success, and something else 424 * in case of a failure. 425 * 426 * See __do_hidpp_send_message_sync() for a detailed explanation of the returned 427 * value. 428 */ 429 static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev, 430 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count, 431 struct hidpp_report *response) 432 { 433 struct hidpp_report *message; 434 int ret, max_count; 435 436 /* Send as long report if short reports are not supported. */ 437 if (report_id == REPORT_ID_HIDPP_SHORT && 438 !(hidpp_dev->supported_reports & HIDPP_REPORT_SHORT_SUPPORTED)) 439 report_id = REPORT_ID_HIDPP_LONG; 440 441 switch (report_id) { 442 case REPORT_ID_HIDPP_SHORT: 443 max_count = HIDPP_REPORT_SHORT_LENGTH - 4; 444 break; 445 case REPORT_ID_HIDPP_LONG: 446 max_count = HIDPP_REPORT_LONG_LENGTH - 4; 447 break; 448 case REPORT_ID_HIDPP_VERY_LONG: 449 max_count = hidpp_dev->very_long_report_length - 4; 450 break; 451 default: 452 return -EINVAL; 453 } 454 455 if (param_count > max_count) 456 return -EINVAL; 457 458 message = kzalloc_obj(struct hidpp_report); 459 if (!message) 460 return -ENOMEM; 461 message->report_id = report_id; 462 message->rap.sub_id = sub_id; 463 message->rap.reg_address = reg_address; 464 memcpy(&message->rap.params, params, param_count); 465 466 ret = hidpp_send_message_sync(hidpp_dev, message, response); 467 kfree(message); 468 return ret; 469 } 470 471 static inline bool hidpp_match_answer(struct hidpp_report *question, 472 struct hidpp_report *answer) 473 { 474 return (answer->fap.feature_index == question->fap.feature_index) && 475 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid); 476 } 477 478 static inline bool hidpp_match_error(struct hidpp_report *question, 479 struct hidpp_report *answer) 480 { 481 return ((answer->rap.sub_id == HIDPP_ERROR) || 482 (answer->fap.feature_index == HIDPP20_ERROR)) && 483 (answer->fap.funcindex_clientid == question->fap.feature_index) && 484 (answer->fap.params[0] == question->fap.funcindex_clientid); 485 } 486 487 static inline bool hidpp_report_is_connect_event(struct hidpp_device *hidpp, 488 struct hidpp_report *report) 489 { 490 return (hidpp->wireless_feature_index && 491 (report->fap.feature_index == hidpp->wireless_feature_index)) || 492 ((report->report_id == REPORT_ID_HIDPP_SHORT) && 493 (report->rap.sub_id == 0x41)); 494 } 495 496 /* 497 * hidpp_prefix_name() prefixes the current given name with "Logitech ". 498 */ 499 static void hidpp_prefix_name(char **name, int name_length) 500 { 501 #define PREFIX_LENGTH 9 /* "Logitech " */ 502 503 int new_length; 504 char *new_name; 505 506 if (name_length > PREFIX_LENGTH && 507 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0) 508 /* The prefix has is already in the name */ 509 return; 510 511 new_length = PREFIX_LENGTH + name_length; 512 new_name = kzalloc(new_length, GFP_KERNEL); 513 if (!new_name) 514 return; 515 516 snprintf(new_name, new_length, "Logitech %s", *name); 517 518 kfree(*name); 519 520 *name = new_name; 521 } 522 523 /* 524 * Updates the USB wireless_status based on whether the headset 525 * is turned on and reachable. 526 */ 527 static void hidpp_update_usb_wireless_status(struct hidpp_device *hidpp) 528 { 529 struct hid_device *hdev = hidpp->hid_dev; 530 struct usb_interface *intf; 531 532 if (!(hidpp->quirks & HIDPP_QUIRK_WIRELESS_STATUS)) 533 return; 534 if (!hid_is_usb(hdev)) 535 return; 536 537 intf = to_usb_interface(hdev->dev.parent); 538 usb_set_wireless_status(intf, hidpp->battery.online ? 539 USB_WIRELESS_STATUS_CONNECTED : 540 USB_WIRELESS_STATUS_DISCONNECTED); 541 } 542 543 /** 544 * hidpp_scroll_counter_handle_scroll() - Send high- and low-resolution scroll 545 * events given a high-resolution wheel 546 * movement. 547 * @input_dev: Pointer to the input device 548 * @counter: a hid_scroll_counter struct describing the wheel. 549 * @hi_res_value: the movement of the wheel, in the mouse's high-resolution 550 * units. 551 * 552 * Given a high-resolution movement, this function converts the movement into 553 * fractions of 120 and emits high-resolution scroll events for the input 554 * device. It also uses the multiplier from &struct hid_scroll_counter to 555 * emit low-resolution scroll events when appropriate for 556 * backwards-compatibility with userspace input libraries. 557 */ 558 static void hidpp_scroll_counter_handle_scroll(struct input_dev *input_dev, 559 struct hidpp_scroll_counter *counter, 560 int hi_res_value) 561 { 562 int low_res_value, remainder, direction; 563 unsigned long long now, previous; 564 565 hi_res_value = hi_res_value * 120/counter->wheel_multiplier; 566 input_report_rel(input_dev, REL_WHEEL_HI_RES, hi_res_value); 567 568 remainder = counter->remainder; 569 direction = hi_res_value > 0 ? 1 : -1; 570 571 now = sched_clock(); 572 previous = counter->last_time; 573 counter->last_time = now; 574 /* 575 * Reset the remainder after a period of inactivity or when the 576 * direction changes. This prevents the REL_WHEEL emulation point 577 * from sliding for devices that don't always provide the same 578 * number of movements per detent. 579 */ 580 if (now - previous > 1000000000 || direction != counter->direction) 581 remainder = 0; 582 583 counter->direction = direction; 584 remainder += hi_res_value; 585 586 /* Some wheels will rest 7/8ths of a detent from the previous detent 587 * after slow movement, so we want the threshold for low-res events to 588 * be in the middle between two detents (e.g. after 4/8ths) as 589 * opposed to on the detents themselves (8/8ths). 590 */ 591 if (abs(remainder) >= 60) { 592 /* Add (or subtract) 1 because we want to trigger when the wheel 593 * is half-way to the next detent (i.e. scroll 1 detent after a 594 * 1/2 detent movement, 2 detents after a 1 1/2 detent movement, 595 * etc.). 596 */ 597 low_res_value = remainder / 120; 598 if (low_res_value == 0) 599 low_res_value = (hi_res_value > 0 ? 1 : -1); 600 input_report_rel(input_dev, REL_WHEEL, low_res_value); 601 remainder -= low_res_value * 120; 602 } 603 counter->remainder = remainder; 604 } 605 606 /* -------------------------------------------------------------------------- */ 607 /* HIDP++ 1.0 commands */ 608 /* -------------------------------------------------------------------------- */ 609 610 #define HIDPP_SET_REGISTER 0x80 611 #define HIDPP_GET_REGISTER 0x81 612 #define HIDPP_SET_LONG_REGISTER 0x82 613 #define HIDPP_GET_LONG_REGISTER 0x83 614 615 /** 616 * hidpp10_set_register - Modify a HID++ 1.0 register. 617 * @hidpp_dev: the device to set the register on. 618 * @register_address: the address of the register to modify. 619 * @byte: the byte of the register to modify. Should be less than 3. 620 * @mask: mask of the bits to modify 621 * @value: new values for the bits in mask 622 * Return: 0 if successful, otherwise a negative error code. 623 */ 624 static int hidpp10_set_register(struct hidpp_device *hidpp_dev, 625 u8 register_address, u8 byte, u8 mask, u8 value) 626 { 627 struct hidpp_report response; 628 int ret; 629 u8 params[3] = { 0 }; 630 631 ret = hidpp_send_rap_command_sync(hidpp_dev, 632 REPORT_ID_HIDPP_SHORT, 633 HIDPP_GET_REGISTER, 634 register_address, 635 NULL, 0, &response); 636 if (ret) 637 return ret; 638 639 memcpy(params, response.rap.params, 3); 640 641 params[byte] &= ~mask; 642 params[byte] |= value & mask; 643 644 return hidpp_send_rap_command_sync(hidpp_dev, 645 REPORT_ID_HIDPP_SHORT, 646 HIDPP_SET_REGISTER, 647 register_address, 648 params, 3, &response); 649 } 650 651 #define HIDPP_REG_ENABLE_REPORTS 0x00 652 #define HIDPP_ENABLE_CONSUMER_REPORT BIT(0) 653 #define HIDPP_ENABLE_WHEEL_REPORT BIT(2) 654 #define HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT BIT(3) 655 #define HIDPP_ENABLE_BAT_REPORT BIT(4) 656 #define HIDPP_ENABLE_HWHEEL_REPORT BIT(5) 657 658 static int hidpp10_enable_battery_reporting(struct hidpp_device *hidpp_dev) 659 { 660 return hidpp10_set_register(hidpp_dev, HIDPP_REG_ENABLE_REPORTS, 0, 661 HIDPP_ENABLE_BAT_REPORT, HIDPP_ENABLE_BAT_REPORT); 662 } 663 664 #define HIDPP_REG_FEATURES 0x01 665 #define HIDPP_ENABLE_SPECIAL_BUTTON_FUNC BIT(1) 666 #define HIDPP_ENABLE_FAST_SCROLL BIT(6) 667 668 /* On HID++ 1.0 devices, high-res scroll was called "scrolling acceleration". */ 669 static int hidpp10_enable_scrolling_acceleration(struct hidpp_device *hidpp_dev) 670 { 671 return hidpp10_set_register(hidpp_dev, HIDPP_REG_FEATURES, 0, 672 HIDPP_ENABLE_FAST_SCROLL, HIDPP_ENABLE_FAST_SCROLL); 673 } 674 675 #define HIDPP_REG_BATTERY_STATUS 0x07 676 677 static int hidpp10_battery_status_map_level(u8 param) 678 { 679 int level; 680 681 switch (param) { 682 case 1 ... 2: 683 level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 684 break; 685 case 3 ... 4: 686 level = POWER_SUPPLY_CAPACITY_LEVEL_LOW; 687 break; 688 case 5 ... 6: 689 level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 690 break; 691 case 7: 692 level = POWER_SUPPLY_CAPACITY_LEVEL_HIGH; 693 break; 694 default: 695 level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; 696 } 697 698 return level; 699 } 700 701 static int hidpp10_battery_status_map_status(u8 param) 702 { 703 int status; 704 705 switch (param) { 706 case 0x00: 707 /* discharging (in use) */ 708 status = POWER_SUPPLY_STATUS_DISCHARGING; 709 break; 710 case 0x21: /* (standard) charging */ 711 case 0x24: /* fast charging */ 712 case 0x25: /* slow charging */ 713 status = POWER_SUPPLY_STATUS_CHARGING; 714 break; 715 case 0x26: /* topping charge */ 716 case 0x22: /* charge complete */ 717 status = POWER_SUPPLY_STATUS_FULL; 718 break; 719 case 0x20: /* unknown */ 720 status = POWER_SUPPLY_STATUS_UNKNOWN; 721 break; 722 /* 723 * 0x01...0x1F = reserved (not charging) 724 * 0x23 = charging error 725 * 0x27..0xff = reserved 726 */ 727 default: 728 status = POWER_SUPPLY_STATUS_NOT_CHARGING; 729 break; 730 } 731 732 return status; 733 } 734 735 static int hidpp10_query_battery_status(struct hidpp_device *hidpp) 736 { 737 struct hidpp_report response; 738 int ret, status; 739 740 ret = hidpp_send_rap_command_sync(hidpp, 741 REPORT_ID_HIDPP_SHORT, 742 HIDPP_GET_REGISTER, 743 HIDPP_REG_BATTERY_STATUS, 744 NULL, 0, &response); 745 if (ret) 746 return ret; 747 748 hidpp->battery.level = 749 hidpp10_battery_status_map_level(response.rap.params[0]); 750 status = hidpp10_battery_status_map_status(response.rap.params[1]); 751 hidpp->battery.status = status; 752 /* the capacity is only available when discharging or full */ 753 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING || 754 status == POWER_SUPPLY_STATUS_FULL; 755 756 return 0; 757 } 758 759 #define HIDPP_REG_BATTERY_MILEAGE 0x0D 760 761 static int hidpp10_battery_mileage_map_status(u8 param) 762 { 763 int status; 764 765 switch (param >> 6) { 766 case 0x00: 767 /* discharging (in use) */ 768 status = POWER_SUPPLY_STATUS_DISCHARGING; 769 break; 770 case 0x01: /* charging */ 771 status = POWER_SUPPLY_STATUS_CHARGING; 772 break; 773 case 0x02: /* charge complete */ 774 status = POWER_SUPPLY_STATUS_FULL; 775 break; 776 /* 777 * 0x03 = charging error 778 */ 779 default: 780 status = POWER_SUPPLY_STATUS_NOT_CHARGING; 781 break; 782 } 783 784 return status; 785 } 786 787 static int hidpp10_query_battery_mileage(struct hidpp_device *hidpp) 788 { 789 struct hidpp_report response; 790 int ret, status; 791 792 ret = hidpp_send_rap_command_sync(hidpp, 793 REPORT_ID_HIDPP_SHORT, 794 HIDPP_GET_REGISTER, 795 HIDPP_REG_BATTERY_MILEAGE, 796 NULL, 0, &response); 797 if (ret) 798 return ret; 799 800 hidpp->battery.capacity = response.rap.params[0]; 801 status = hidpp10_battery_mileage_map_status(response.rap.params[2]); 802 hidpp->battery.status = status; 803 /* the capacity is only available when discharging or full */ 804 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING || 805 status == POWER_SUPPLY_STATUS_FULL; 806 807 return 0; 808 } 809 810 static int hidpp10_battery_event(struct hidpp_device *hidpp, u8 *data, int size) 811 { 812 struct hidpp_report *report = (struct hidpp_report *)data; 813 int status, capacity, level; 814 bool changed; 815 816 if (report->report_id != REPORT_ID_HIDPP_SHORT) 817 return 0; 818 819 switch (report->rap.sub_id) { 820 case HIDPP_REG_BATTERY_STATUS: 821 capacity = hidpp->battery.capacity; 822 level = hidpp10_battery_status_map_level(report->rawbytes[1]); 823 status = hidpp10_battery_status_map_status(report->rawbytes[2]); 824 break; 825 case HIDPP_REG_BATTERY_MILEAGE: 826 capacity = report->rap.params[0]; 827 level = hidpp->battery.level; 828 status = hidpp10_battery_mileage_map_status(report->rawbytes[3]); 829 break; 830 default: 831 return 0; 832 } 833 834 changed = capacity != hidpp->battery.capacity || 835 level != hidpp->battery.level || 836 status != hidpp->battery.status; 837 838 /* the capacity is only available when discharging or full */ 839 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING || 840 status == POWER_SUPPLY_STATUS_FULL; 841 842 if (changed) { 843 hidpp->battery.level = level; 844 hidpp->battery.status = status; 845 if (hidpp->battery.ps) 846 power_supply_changed(hidpp->battery.ps); 847 } 848 849 return 0; 850 } 851 852 #define HIDPP_REG_PAIRING_INFORMATION 0xB5 853 #define HIDPP_EXTENDED_PAIRING 0x30 854 #define HIDPP_DEVICE_NAME 0x40 855 856 static char *hidpp_unifying_get_name(struct hidpp_device *hidpp_dev) 857 { 858 struct hidpp_report response; 859 int ret; 860 u8 params[1] = { HIDPP_DEVICE_NAME }; 861 char *name; 862 int len; 863 864 ret = hidpp_send_rap_command_sync(hidpp_dev, 865 REPORT_ID_HIDPP_SHORT, 866 HIDPP_GET_LONG_REGISTER, 867 HIDPP_REG_PAIRING_INFORMATION, 868 params, 1, &response); 869 if (ret) 870 return NULL; 871 872 len = response.rap.params[1]; 873 874 if (2 + len > sizeof(response.rap.params)) 875 return NULL; 876 877 if (len < 4) /* logitech devices are usually at least Xddd */ 878 return NULL; 879 880 name = kzalloc(len + 1, GFP_KERNEL); 881 if (!name) 882 return NULL; 883 884 memcpy(name, &response.rap.params[2], len); 885 886 /* include the terminating '\0' */ 887 hidpp_prefix_name(&name, len + 1); 888 889 return name; 890 } 891 892 static int hidpp_unifying_get_serial(struct hidpp_device *hidpp, u32 *serial) 893 { 894 struct hidpp_report response; 895 int ret; 896 u8 params[1] = { HIDPP_EXTENDED_PAIRING }; 897 898 ret = hidpp_send_rap_command_sync(hidpp, 899 REPORT_ID_HIDPP_SHORT, 900 HIDPP_GET_LONG_REGISTER, 901 HIDPP_REG_PAIRING_INFORMATION, 902 params, 1, &response); 903 if (ret) 904 return ret; 905 906 /* 907 * We don't care about LE or BE, we will output it as a string 908 * with %4phD, so we need to keep the order. 909 */ 910 *serial = *((u32 *)&response.rap.params[1]); 911 return 0; 912 } 913 914 static int hidpp_unifying_init(struct hidpp_device *hidpp) 915 { 916 struct hid_device *hdev = hidpp->hid_dev; 917 const char *name; 918 u32 serial; 919 int ret; 920 921 ret = hidpp_unifying_get_serial(hidpp, &serial); 922 if (ret) 923 return ret; 924 925 snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial); 926 dbg_hid("HID++ Unifying: Got serial: %s\n", hdev->uniq); 927 928 name = hidpp_unifying_get_name(hidpp); 929 if (!name) 930 return -EIO; 931 932 snprintf(hdev->name, sizeof(hdev->name), "%s", name); 933 dbg_hid("HID++ Unifying: Got name: %s\n", name); 934 935 kfree(name); 936 return 0; 937 } 938 939 /* -------------------------------------------------------------------------- */ 940 /* 0x0000: Root */ 941 /* -------------------------------------------------------------------------- */ 942 943 #define HIDPP_PAGE_ROOT 0x0000 944 #define HIDPP_PAGE_ROOT_IDX 0x00 945 946 #define CMD_ROOT_GET_FEATURE 0x00 947 #define CMD_ROOT_GET_PROTOCOL_VERSION 0x10 948 949 static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature, 950 u8 *feature_index) 951 { 952 struct hidpp_report response; 953 int ret; 954 u8 params[2] = { feature >> 8, feature & 0x00FF }; 955 956 ret = hidpp_send_fap_command_sync(hidpp, 957 HIDPP_PAGE_ROOT_IDX, 958 CMD_ROOT_GET_FEATURE, 959 params, 2, &response); 960 if (ret) 961 return ret; 962 963 if (response.fap.params[0] == 0) 964 return -ENOENT; 965 966 *feature_index = response.fap.params[0]; 967 968 return ret; 969 } 970 971 static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp) 972 { 973 const u8 ping_byte = 0x5a; 974 u8 ping_data[3] = { 0, 0, ping_byte }; 975 struct hidpp_report response; 976 int ret; 977 978 ret = hidpp_send_rap_command_sync(hidpp, 979 REPORT_ID_HIDPP_SHORT, 980 HIDPP_PAGE_ROOT_IDX, 981 CMD_ROOT_GET_PROTOCOL_VERSION | LINUX_KERNEL_SW_ID, 982 ping_data, sizeof(ping_data), &response); 983 984 if (ret == HIDPP_ERROR_INVALID_SUBID) { 985 hidpp->protocol_major = 1; 986 hidpp->protocol_minor = 0; 987 goto print_version; 988 } 989 990 /* the device might not be connected */ 991 if (ret == HIDPP_ERROR_CONNECT_FAIL || 992 ret == HIDPP_ERROR_RESOURCE_ERROR || 993 ret == HIDPP_ERROR_UNKNOWN_DEVICE) 994 return -EIO; 995 996 if (ret > 0) { 997 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 998 __func__, ret); 999 return -EPROTO; 1000 } 1001 if (ret) 1002 return ret; 1003 1004 if (response.rap.params[2] != ping_byte) { 1005 hid_err(hidpp->hid_dev, "%s: ping mismatch 0x%02x != 0x%02x\n", 1006 __func__, response.rap.params[2], ping_byte); 1007 return -EPROTO; 1008 } 1009 1010 hidpp->protocol_major = response.rap.params[0]; 1011 hidpp->protocol_minor = response.rap.params[1]; 1012 1013 print_version: 1014 if (!hidpp->connected_once) { 1015 hid_info(hidpp->hid_dev, "HID++ %u.%u device connected.\n", 1016 hidpp->protocol_major, hidpp->protocol_minor); 1017 hidpp->connected_once = true; 1018 } else 1019 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n", 1020 hidpp->protocol_major, hidpp->protocol_minor); 1021 return 0; 1022 } 1023 1024 /* -------------------------------------------------------------------------- */ 1025 /* 0x0003: Device Information */ 1026 /* -------------------------------------------------------------------------- */ 1027 1028 #define HIDPP_PAGE_DEVICE_INFORMATION 0x0003 1029 1030 #define CMD_GET_DEVICE_INFO 0x00 1031 1032 static int hidpp_get_serial(struct hidpp_device *hidpp, u32 *serial) 1033 { 1034 struct hidpp_report response; 1035 u8 feature_index; 1036 int ret; 1037 1038 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_DEVICE_INFORMATION, 1039 &feature_index); 1040 if (ret) 1041 return ret; 1042 1043 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 1044 CMD_GET_DEVICE_INFO, 1045 NULL, 0, &response); 1046 if (ret) 1047 return ret; 1048 1049 /* See hidpp_unifying_get_serial() */ 1050 *serial = *((u32 *)&response.rap.params[1]); 1051 return 0; 1052 } 1053 1054 static int hidpp_serial_init(struct hidpp_device *hidpp) 1055 { 1056 struct hid_device *hdev = hidpp->hid_dev; 1057 u32 serial; 1058 int ret; 1059 1060 ret = hidpp_get_serial(hidpp, &serial); 1061 if (ret) 1062 return ret; 1063 1064 snprintf(hdev->uniq, sizeof(hdev->uniq), "%4phD", &serial); 1065 dbg_hid("HID++ DeviceInformation: Got serial: %s\n", hdev->uniq); 1066 1067 return 0; 1068 } 1069 1070 /* -------------------------------------------------------------------------- */ 1071 /* 0x0005: GetDeviceNameType */ 1072 /* -------------------------------------------------------------------------- */ 1073 1074 #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005 1075 1076 #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x00 1077 #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x10 1078 #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x20 1079 1080 static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp, 1081 u8 feature_index, u8 *nameLength) 1082 { 1083 struct hidpp_report response; 1084 int ret; 1085 1086 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 1087 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response); 1088 1089 if (ret > 0) { 1090 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 1091 __func__, ret); 1092 return -EPROTO; 1093 } 1094 if (ret) 1095 return ret; 1096 1097 *nameLength = response.fap.params[0]; 1098 1099 return ret; 1100 } 1101 1102 static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp, 1103 u8 feature_index, u8 char_index, char *device_name, int len_buf) 1104 { 1105 struct hidpp_report response; 1106 int ret, i; 1107 int count; 1108 1109 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 1110 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1, 1111 &response); 1112 1113 if (ret > 0) { 1114 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 1115 __func__, ret); 1116 return -EPROTO; 1117 } 1118 if (ret) 1119 return ret; 1120 1121 switch (response.report_id) { 1122 case REPORT_ID_HIDPP_VERY_LONG: 1123 count = hidpp->very_long_report_length - 4; 1124 break; 1125 case REPORT_ID_HIDPP_LONG: 1126 count = HIDPP_REPORT_LONG_LENGTH - 4; 1127 break; 1128 case REPORT_ID_HIDPP_SHORT: 1129 count = HIDPP_REPORT_SHORT_LENGTH - 4; 1130 break; 1131 default: 1132 return -EPROTO; 1133 } 1134 1135 if (len_buf < count) 1136 count = len_buf; 1137 1138 for (i = 0; i < count; i++) 1139 device_name[i] = response.fap.params[i]; 1140 1141 return count; 1142 } 1143 1144 static char *hidpp_get_device_name(struct hidpp_device *hidpp) 1145 { 1146 u8 feature_index; 1147 u8 __name_length; 1148 char *name; 1149 unsigned index = 0; 1150 int ret; 1151 1152 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE, 1153 &feature_index); 1154 if (ret) 1155 return NULL; 1156 1157 ret = hidpp_devicenametype_get_count(hidpp, feature_index, 1158 &__name_length); 1159 if (ret) 1160 return NULL; 1161 1162 name = kzalloc(__name_length + 1, GFP_KERNEL); 1163 if (!name) 1164 return NULL; 1165 1166 while (index < __name_length) { 1167 ret = hidpp_devicenametype_get_device_name(hidpp, 1168 feature_index, index, name + index, 1169 __name_length - index); 1170 if (ret <= 0) { 1171 kfree(name); 1172 return NULL; 1173 } 1174 index += ret; 1175 } 1176 1177 /* include the terminating '\0' */ 1178 hidpp_prefix_name(&name, __name_length + 1); 1179 1180 return name; 1181 } 1182 1183 /* -------------------------------------------------------------------------- */ 1184 /* 0x1000: Battery level status */ 1185 /* -------------------------------------------------------------------------- */ 1186 1187 #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000 1188 1189 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00 1190 #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10 1191 1192 #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00 1193 1194 #define FLAG_BATTERY_LEVEL_DISABLE_OSD BIT(0) 1195 #define FLAG_BATTERY_LEVEL_MILEAGE BIT(1) 1196 #define FLAG_BATTERY_LEVEL_RECHARGEABLE BIT(2) 1197 1198 static int hidpp_map_battery_level(int capacity) 1199 { 1200 if (capacity < 11) 1201 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 1202 /* 1203 * The spec says this should be < 31 but some devices report 30 1204 * with brand new batteries and Windows reports 30 as "Good". 1205 */ 1206 else if (capacity < 30) 1207 return POWER_SUPPLY_CAPACITY_LEVEL_LOW; 1208 else if (capacity < 81) 1209 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 1210 return POWER_SUPPLY_CAPACITY_LEVEL_FULL; 1211 } 1212 1213 static int hidpp20_batterylevel_map_status_capacity(u8 data[3], int *capacity, 1214 int *next_capacity, 1215 int *level) 1216 { 1217 int status; 1218 1219 *capacity = data[0]; 1220 *next_capacity = data[1]; 1221 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; 1222 1223 /* When discharging, we can rely on the device reported capacity. 1224 * For all other states the device reports 0 (unknown). 1225 */ 1226 switch (data[2]) { 1227 case 0: /* discharging (in use) */ 1228 status = POWER_SUPPLY_STATUS_DISCHARGING; 1229 *level = hidpp_map_battery_level(*capacity); 1230 break; 1231 case 1: /* recharging */ 1232 status = POWER_SUPPLY_STATUS_CHARGING; 1233 break; 1234 case 2: /* charge in final stage */ 1235 status = POWER_SUPPLY_STATUS_CHARGING; 1236 break; 1237 case 3: /* charge complete */ 1238 status = POWER_SUPPLY_STATUS_FULL; 1239 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; 1240 *capacity = 100; 1241 break; 1242 case 4: /* recharging below optimal speed */ 1243 status = POWER_SUPPLY_STATUS_CHARGING; 1244 break; 1245 /* 5 = invalid battery type 1246 6 = thermal error 1247 7 = other charging error */ 1248 default: 1249 status = POWER_SUPPLY_STATUS_NOT_CHARGING; 1250 break; 1251 } 1252 1253 return status; 1254 } 1255 1256 static int hidpp20_batterylevel_get_battery_capacity(struct hidpp_device *hidpp, 1257 u8 feature_index, 1258 int *status, 1259 int *capacity, 1260 int *next_capacity, 1261 int *level) 1262 { 1263 struct hidpp_report response; 1264 int ret; 1265 u8 *params = (u8 *)response.fap.params; 1266 1267 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 1268 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS, 1269 NULL, 0, &response); 1270 /* Ignore these intermittent errors */ 1271 if (ret == HIDPP_ERROR_RESOURCE_ERROR) 1272 return -EIO; 1273 if (ret > 0) { 1274 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 1275 __func__, ret); 1276 return -EPROTO; 1277 } 1278 if (ret) 1279 return ret; 1280 1281 *status = hidpp20_batterylevel_map_status_capacity(params, capacity, 1282 next_capacity, 1283 level); 1284 1285 return 0; 1286 } 1287 1288 static int hidpp20_batterylevel_get_battery_info(struct hidpp_device *hidpp, 1289 u8 feature_index) 1290 { 1291 struct hidpp_report response; 1292 int ret; 1293 u8 *params = (u8 *)response.fap.params; 1294 unsigned int level_count, flags; 1295 1296 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 1297 CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY, 1298 NULL, 0, &response); 1299 if (ret > 0) { 1300 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 1301 __func__, ret); 1302 return -EPROTO; 1303 } 1304 if (ret) 1305 return ret; 1306 1307 level_count = params[0]; 1308 flags = params[1]; 1309 1310 if (level_count < 10 || !(flags & FLAG_BATTERY_LEVEL_MILEAGE)) 1311 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS; 1312 else 1313 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE; 1314 1315 return 0; 1316 } 1317 1318 static int hidpp20_query_battery_info_1000(struct hidpp_device *hidpp) 1319 { 1320 int ret; 1321 int status, capacity, next_capacity, level; 1322 1323 if (hidpp->battery.feature_index == 0xff) { 1324 ret = hidpp_root_get_feature(hidpp, 1325 HIDPP_PAGE_BATTERY_LEVEL_STATUS, 1326 &hidpp->battery.feature_index); 1327 if (ret) 1328 return ret; 1329 } 1330 1331 ret = hidpp20_batterylevel_get_battery_capacity(hidpp, 1332 hidpp->battery.feature_index, 1333 &status, &capacity, 1334 &next_capacity, &level); 1335 if (ret) 1336 return ret; 1337 1338 ret = hidpp20_batterylevel_get_battery_info(hidpp, 1339 hidpp->battery.feature_index); 1340 if (ret) 1341 return ret; 1342 1343 hidpp->battery.status = status; 1344 hidpp->battery.capacity = capacity; 1345 hidpp->battery.level = level; 1346 /* the capacity is only available when discharging or full */ 1347 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING || 1348 status == POWER_SUPPLY_STATUS_FULL; 1349 1350 return 0; 1351 } 1352 1353 static int hidpp20_battery_event_1000(struct hidpp_device *hidpp, 1354 u8 *data, int size) 1355 { 1356 struct hidpp_report *report = (struct hidpp_report *)data; 1357 int status, capacity, next_capacity, level; 1358 bool changed; 1359 1360 if (report->fap.feature_index != hidpp->battery.feature_index || 1361 report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST) 1362 return 0; 1363 1364 status = hidpp20_batterylevel_map_status_capacity(report->fap.params, 1365 &capacity, 1366 &next_capacity, 1367 &level); 1368 1369 /* the capacity is only available when discharging or full */ 1370 hidpp->battery.online = status == POWER_SUPPLY_STATUS_DISCHARGING || 1371 status == POWER_SUPPLY_STATUS_FULL; 1372 1373 changed = capacity != hidpp->battery.capacity || 1374 level != hidpp->battery.level || 1375 status != hidpp->battery.status; 1376 1377 if (changed) { 1378 hidpp->battery.level = level; 1379 hidpp->battery.capacity = capacity; 1380 hidpp->battery.status = status; 1381 if (hidpp->battery.ps) 1382 power_supply_changed(hidpp->battery.ps); 1383 } 1384 1385 return 0; 1386 } 1387 1388 /* -------------------------------------------------------------------------- */ 1389 /* 0x1001: Battery voltage */ 1390 /* -------------------------------------------------------------------------- */ 1391 1392 #define HIDPP_PAGE_BATTERY_VOLTAGE 0x1001 1393 1394 #define CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE 0x00 1395 1396 #define EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST 0x00 1397 1398 static int hidpp20_battery_map_status_voltage(u8 data[3], int *voltage, 1399 int *level, int *charge_type) 1400 { 1401 int status; 1402 1403 long flags = (long) data[2]; 1404 *level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; 1405 1406 if (flags & 0x80) 1407 switch (flags & 0x07) { 1408 case 0: 1409 status = POWER_SUPPLY_STATUS_CHARGING; 1410 break; 1411 case 1: 1412 status = POWER_SUPPLY_STATUS_FULL; 1413 *level = POWER_SUPPLY_CAPACITY_LEVEL_FULL; 1414 break; 1415 case 2: 1416 status = POWER_SUPPLY_STATUS_NOT_CHARGING; 1417 break; 1418 default: 1419 status = POWER_SUPPLY_STATUS_UNKNOWN; 1420 break; 1421 } 1422 else 1423 status = POWER_SUPPLY_STATUS_DISCHARGING; 1424 1425 *charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD; 1426 if (test_bit(3, &flags)) { 1427 *charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST; 1428 } 1429 if (test_bit(4, &flags)) { 1430 *charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; 1431 } 1432 if (test_bit(5, &flags)) { 1433 *level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 1434 } 1435 1436 *voltage = get_unaligned_be16(data); 1437 1438 return status; 1439 } 1440 1441 static int hidpp20_battery_get_battery_voltage(struct hidpp_device *hidpp, 1442 u8 feature_index, 1443 int *status, int *voltage, 1444 int *level, int *charge_type) 1445 { 1446 struct hidpp_report response; 1447 int ret; 1448 u8 *params = (u8 *)response.fap.params; 1449 1450 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 1451 CMD_BATTERY_VOLTAGE_GET_BATTERY_VOLTAGE, 1452 NULL, 0, &response); 1453 1454 if (ret > 0) { 1455 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 1456 __func__, ret); 1457 return -EPROTO; 1458 } 1459 if (ret) 1460 return ret; 1461 1462 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_VOLTAGE; 1463 1464 *status = hidpp20_battery_map_status_voltage(params, voltage, 1465 level, charge_type); 1466 1467 return 0; 1468 } 1469 1470 static int hidpp20_map_battery_capacity(struct hid_device *hid_dev, int voltage) 1471 { 1472 /* NB: This voltage curve doesn't necessarily map perfectly to all 1473 * devices that implement the BATTERY_VOLTAGE feature. This is because 1474 * there are a few devices that use different battery technology. 1475 */ 1476 1477 static const int voltages[100] = { 1478 4186, 4156, 4143, 4133, 4122, 4113, 4103, 4094, 4086, 4075, 1479 4067, 4059, 4051, 4043, 4035, 4027, 4019, 4011, 4003, 3997, 1480 3989, 3983, 3976, 3969, 3961, 3955, 3949, 3942, 3935, 3929, 1481 3922, 3916, 3909, 3902, 3896, 3890, 3883, 3877, 3870, 3865, 1482 3859, 3853, 3848, 3842, 3837, 3833, 3828, 3824, 3819, 3815, 1483 3811, 3808, 3804, 3800, 3797, 3793, 3790, 3787, 3784, 3781, 1484 3778, 3775, 3772, 3770, 3767, 3764, 3762, 3759, 3757, 3754, 1485 3751, 3748, 3744, 3741, 3737, 3734, 3730, 3726, 3724, 3720, 1486 3717, 3714, 3710, 3706, 3702, 3697, 3693, 3688, 3683, 3677, 1487 3671, 3666, 3662, 3658, 3654, 3646, 3633, 3612, 3579, 3537 1488 }; 1489 1490 int i; 1491 1492 if (unlikely(voltage < 3500 || voltage >= 5000)) 1493 hid_warn_once(hid_dev, 1494 "%s: possibly using the wrong voltage curve\n", 1495 __func__); 1496 1497 for (i = 0; i < ARRAY_SIZE(voltages); i++) { 1498 if (voltage >= voltages[i]) 1499 return ARRAY_SIZE(voltages) - i; 1500 } 1501 1502 return 0; 1503 } 1504 1505 static int hidpp20_query_battery_voltage_info(struct hidpp_device *hidpp) 1506 { 1507 int ret; 1508 int status, voltage, level, charge_type; 1509 1510 if (hidpp->battery.voltage_feature_index == 0xff) { 1511 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_BATTERY_VOLTAGE, 1512 &hidpp->battery.voltage_feature_index); 1513 if (ret) 1514 return ret; 1515 } 1516 1517 ret = hidpp20_battery_get_battery_voltage(hidpp, 1518 hidpp->battery.voltage_feature_index, 1519 &status, &voltage, &level, &charge_type); 1520 1521 if (ret) 1522 return ret; 1523 1524 hidpp->battery.status = status; 1525 hidpp->battery.voltage = voltage; 1526 hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev, 1527 voltage); 1528 hidpp->battery.level = level; 1529 hidpp->battery.charge_type = charge_type; 1530 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING; 1531 1532 return 0; 1533 } 1534 1535 static int hidpp20_battery_voltage_event(struct hidpp_device *hidpp, 1536 u8 *data, int size) 1537 { 1538 struct hidpp_report *report = (struct hidpp_report *)data; 1539 int status, voltage, level, charge_type; 1540 1541 if (report->fap.feature_index != hidpp->battery.voltage_feature_index || 1542 report->fap.funcindex_clientid != EVENT_BATTERY_VOLTAGE_STATUS_BROADCAST) 1543 return 0; 1544 1545 status = hidpp20_battery_map_status_voltage(report->fap.params, &voltage, 1546 &level, &charge_type); 1547 1548 hidpp->battery.online = status != POWER_SUPPLY_STATUS_NOT_CHARGING; 1549 1550 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) { 1551 hidpp->battery.voltage = voltage; 1552 hidpp->battery.capacity = hidpp20_map_battery_capacity(hidpp->hid_dev, 1553 voltage); 1554 hidpp->battery.status = status; 1555 hidpp->battery.level = level; 1556 hidpp->battery.charge_type = charge_type; 1557 if (hidpp->battery.ps) 1558 power_supply_changed(hidpp->battery.ps); 1559 } 1560 return 0; 1561 } 1562 1563 /* -------------------------------------------------------------------------- */ 1564 /* 0x1004: Unified battery */ 1565 /* -------------------------------------------------------------------------- */ 1566 1567 #define HIDPP_PAGE_UNIFIED_BATTERY 0x1004 1568 1569 #define CMD_UNIFIED_BATTERY_GET_CAPABILITIES 0x00 1570 #define CMD_UNIFIED_BATTERY_GET_STATUS 0x10 1571 1572 #define EVENT_UNIFIED_BATTERY_STATUS_EVENT 0x00 1573 1574 #define FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL BIT(0) 1575 #define FLAG_UNIFIED_BATTERY_LEVEL_LOW BIT(1) 1576 #define FLAG_UNIFIED_BATTERY_LEVEL_GOOD BIT(2) 1577 #define FLAG_UNIFIED_BATTERY_LEVEL_FULL BIT(3) 1578 1579 #define FLAG_UNIFIED_BATTERY_FLAGS_RECHARGEABLE BIT(0) 1580 #define FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE BIT(1) 1581 1582 static int hidpp20_unifiedbattery_get_capabilities(struct hidpp_device *hidpp, 1583 u8 feature_index) 1584 { 1585 struct hidpp_report response; 1586 int ret; 1587 u8 *params = (u8 *)response.fap.params; 1588 1589 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS || 1590 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) { 1591 /* we have already set the device capabilities, so let's skip */ 1592 return 0; 1593 } 1594 1595 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 1596 CMD_UNIFIED_BATTERY_GET_CAPABILITIES, 1597 NULL, 0, &response); 1598 /* Ignore these intermittent errors */ 1599 if (ret == HIDPP_ERROR_RESOURCE_ERROR) 1600 return -EIO; 1601 if (ret > 0) { 1602 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 1603 __func__, ret); 1604 return -EPROTO; 1605 } 1606 if (ret) 1607 return ret; 1608 1609 /* 1610 * If the device supports state of charge (battery percentage) we won't 1611 * export the battery level information. there are 4 possible battery 1612 * levels and they all are optional, this means that the device might 1613 * not support any of them, we are just better off with the battery 1614 * percentage. 1615 */ 1616 if (params[1] & FLAG_UNIFIED_BATTERY_FLAGS_STATE_OF_CHARGE) { 1617 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_PERCENTAGE; 1618 hidpp->battery.supported_levels_1004 = 0; 1619 } else { 1620 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS; 1621 hidpp->battery.supported_levels_1004 = params[0]; 1622 } 1623 1624 return 0; 1625 } 1626 1627 static int hidpp20_unifiedbattery_map_status(struct hidpp_device *hidpp, 1628 u8 charging_status, 1629 u8 external_power_status) 1630 { 1631 int status; 1632 1633 switch (charging_status) { 1634 case 0: /* discharging */ 1635 status = POWER_SUPPLY_STATUS_DISCHARGING; 1636 break; 1637 case 1: /* charging */ 1638 case 2: /* charging slow */ 1639 status = POWER_SUPPLY_STATUS_CHARGING; 1640 break; 1641 case 3: /* complete */ 1642 status = POWER_SUPPLY_STATUS_FULL; 1643 break; 1644 case 4: /* error */ 1645 status = POWER_SUPPLY_STATUS_NOT_CHARGING; 1646 hid_info(hidpp->hid_dev, "%s: charging error", 1647 hidpp->name); 1648 break; 1649 default: 1650 status = POWER_SUPPLY_STATUS_NOT_CHARGING; 1651 break; 1652 } 1653 1654 return status; 1655 } 1656 1657 static int hidpp20_unifiedbattery_map_level(struct hidpp_device *hidpp, 1658 u8 battery_level) 1659 { 1660 /* cler unsupported level bits */ 1661 battery_level &= hidpp->battery.supported_levels_1004; 1662 1663 if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_FULL) 1664 return POWER_SUPPLY_CAPACITY_LEVEL_FULL; 1665 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_GOOD) 1666 return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 1667 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_LOW) 1668 return POWER_SUPPLY_CAPACITY_LEVEL_LOW; 1669 else if (battery_level & FLAG_UNIFIED_BATTERY_LEVEL_CRITICAL) 1670 return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; 1671 1672 return POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; 1673 } 1674 1675 static int hidpp20_unifiedbattery_get_status(struct hidpp_device *hidpp, 1676 u8 feature_index, 1677 u8 *state_of_charge, 1678 int *status, 1679 int *level) 1680 { 1681 struct hidpp_report response; 1682 int ret; 1683 u8 *params = (u8 *)response.fap.params; 1684 1685 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 1686 CMD_UNIFIED_BATTERY_GET_STATUS, 1687 NULL, 0, &response); 1688 /* Ignore these intermittent errors */ 1689 if (ret == HIDPP_ERROR_RESOURCE_ERROR) 1690 return -EIO; 1691 if (ret > 0) { 1692 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 1693 __func__, ret); 1694 return -EPROTO; 1695 } 1696 if (ret) 1697 return ret; 1698 1699 *state_of_charge = params[0]; 1700 *status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]); 1701 *level = hidpp20_unifiedbattery_map_level(hidpp, params[1]); 1702 1703 return 0; 1704 } 1705 1706 static int hidpp20_query_battery_info_1004(struct hidpp_device *hidpp) 1707 { 1708 int ret; 1709 u8 state_of_charge; 1710 int status, level; 1711 1712 if (hidpp->battery.feature_index == 0xff) { 1713 ret = hidpp_root_get_feature(hidpp, 1714 HIDPP_PAGE_UNIFIED_BATTERY, 1715 &hidpp->battery.feature_index); 1716 if (ret) 1717 return ret; 1718 } 1719 1720 ret = hidpp20_unifiedbattery_get_capabilities(hidpp, 1721 hidpp->battery.feature_index); 1722 if (ret) 1723 return ret; 1724 1725 ret = hidpp20_unifiedbattery_get_status(hidpp, 1726 hidpp->battery.feature_index, 1727 &state_of_charge, 1728 &status, 1729 &level); 1730 if (ret) 1731 return ret; 1732 1733 hidpp->capabilities |= HIDPP_CAPABILITY_UNIFIED_BATTERY; 1734 hidpp->battery.capacity = state_of_charge; 1735 hidpp->battery.status = status; 1736 hidpp->battery.level = level; 1737 hidpp->battery.online = true; 1738 1739 return 0; 1740 } 1741 1742 static int hidpp20_battery_event_1004(struct hidpp_device *hidpp, 1743 u8 *data, int size) 1744 { 1745 struct hidpp_report *report = (struct hidpp_report *)data; 1746 u8 *params = (u8 *)report->fap.params; 1747 int state_of_charge, status, level; 1748 bool changed; 1749 1750 if (report->fap.feature_index != hidpp->battery.feature_index || 1751 report->fap.funcindex_clientid != EVENT_UNIFIED_BATTERY_STATUS_EVENT) 1752 return 0; 1753 1754 state_of_charge = params[0]; 1755 status = hidpp20_unifiedbattery_map_status(hidpp, params[2], params[3]); 1756 level = hidpp20_unifiedbattery_map_level(hidpp, params[1]); 1757 1758 changed = status != hidpp->battery.status || 1759 (state_of_charge != hidpp->battery.capacity && 1760 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE) || 1761 (level != hidpp->battery.level && 1762 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS); 1763 1764 if (changed) { 1765 hidpp->battery.capacity = state_of_charge; 1766 hidpp->battery.status = status; 1767 hidpp->battery.level = level; 1768 if (hidpp->battery.ps) 1769 power_supply_changed(hidpp->battery.ps); 1770 } 1771 1772 return 0; 1773 } 1774 1775 /* -------------------------------------------------------------------------- */ 1776 /* Battery feature helpers */ 1777 /* -------------------------------------------------------------------------- */ 1778 1779 static enum power_supply_property hidpp_battery_props[] = { 1780 POWER_SUPPLY_PROP_ONLINE, 1781 POWER_SUPPLY_PROP_STATUS, 1782 POWER_SUPPLY_PROP_SCOPE, 1783 POWER_SUPPLY_PROP_MODEL_NAME, 1784 POWER_SUPPLY_PROP_MANUFACTURER, 1785 POWER_SUPPLY_PROP_SERIAL_NUMBER, 1786 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY, */ 1787 0, /* placeholder for POWER_SUPPLY_PROP_CAPACITY_LEVEL, */ 1788 0, /* placeholder for POWER_SUPPLY_PROP_VOLTAGE_NOW, */ 1789 }; 1790 1791 static int hidpp_battery_get_property(struct power_supply *psy, 1792 enum power_supply_property psp, 1793 union power_supply_propval *val) 1794 { 1795 struct hidpp_device *hidpp = power_supply_get_drvdata(psy); 1796 int ret = 0; 1797 1798 switch(psp) { 1799 case POWER_SUPPLY_PROP_STATUS: 1800 val->intval = hidpp->battery.status; 1801 break; 1802 case POWER_SUPPLY_PROP_CAPACITY: 1803 val->intval = hidpp->battery.capacity; 1804 break; 1805 case POWER_SUPPLY_PROP_CAPACITY_LEVEL: 1806 val->intval = hidpp->battery.level; 1807 break; 1808 case POWER_SUPPLY_PROP_SCOPE: 1809 val->intval = POWER_SUPPLY_SCOPE_DEVICE; 1810 break; 1811 case POWER_SUPPLY_PROP_ONLINE: 1812 val->intval = hidpp->battery.online; 1813 break; 1814 case POWER_SUPPLY_PROP_MODEL_NAME: 1815 if (!strncmp(hidpp->name, "Logitech ", 9)) 1816 val->strval = hidpp->name + 9; 1817 else 1818 val->strval = hidpp->name; 1819 break; 1820 case POWER_SUPPLY_PROP_MANUFACTURER: 1821 val->strval = "Logitech"; 1822 break; 1823 case POWER_SUPPLY_PROP_SERIAL_NUMBER: 1824 val->strval = hidpp->hid_dev->uniq; 1825 break; 1826 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 1827 /* hardware reports voltage in mV. sysfs expects uV */ 1828 val->intval = hidpp->battery.voltage * 1000; 1829 break; 1830 case POWER_SUPPLY_PROP_CHARGE_TYPE: 1831 val->intval = hidpp->battery.charge_type; 1832 break; 1833 default: 1834 ret = -EINVAL; 1835 break; 1836 } 1837 1838 return ret; 1839 } 1840 1841 /* -------------------------------------------------------------------------- */ 1842 /* 0x1d4b: Wireless device status */ 1843 /* -------------------------------------------------------------------------- */ 1844 #define HIDPP_PAGE_WIRELESS_DEVICE_STATUS 0x1d4b 1845 1846 static int hidpp_get_wireless_feature_index(struct hidpp_device *hidpp, u8 *feature_index) 1847 { 1848 return hidpp_root_get_feature(hidpp, 1849 HIDPP_PAGE_WIRELESS_DEVICE_STATUS, 1850 feature_index); 1851 } 1852 1853 /* -------------------------------------------------------------------------- */ 1854 /* 0x1f20: ADC measurement */ 1855 /* -------------------------------------------------------------------------- */ 1856 1857 #define HIDPP_PAGE_ADC_MEASUREMENT 0x1f20 1858 1859 #define CMD_ADC_MEASUREMENT_GET_ADC_MEASUREMENT 0x00 1860 1861 #define EVENT_ADC_MEASUREMENT_STATUS_BROADCAST 0x00 1862 1863 static int hidpp20_map_adc_measurement_1f20_capacity(struct hid_device *hid_dev, int voltage) 1864 { 1865 /* NB: This voltage curve doesn't necessarily map perfectly to all 1866 * devices that implement the ADC_MEASUREMENT feature. This is because 1867 * there are a few devices that use different battery technology. 1868 * 1869 * Adapted from: 1870 * https://github.com/Sapd/HeadsetControl/blob/acd972be0468e039b93aae81221f20a54d2d60f7/src/devices/logitech_g633_g933_935.c#L44-L52 1871 */ 1872 static const int voltages[100] = { 1873 4030, 4024, 4018, 4011, 4003, 3994, 3985, 3975, 3963, 3951, 1874 3937, 3922, 3907, 3893, 3880, 3868, 3857, 3846, 3837, 3828, 1875 3820, 3812, 3805, 3798, 3791, 3785, 3779, 3773, 3768, 3762, 1876 3757, 3752, 3747, 3742, 3738, 3733, 3729, 3724, 3720, 3716, 1877 3712, 3708, 3704, 3700, 3696, 3692, 3688, 3685, 3681, 3677, 1878 3674, 3670, 3667, 3663, 3660, 3657, 3653, 3650, 3646, 3643, 1879 3640, 3637, 3633, 3630, 3627, 3624, 3620, 3617, 3614, 3611, 1880 3608, 3604, 3601, 3598, 3595, 3592, 3589, 3585, 3582, 3579, 1881 3576, 3573, 3569, 3566, 3563, 3560, 3556, 3553, 3550, 3546, 1882 3543, 3539, 3536, 3532, 3529, 3525, 3499, 3466, 3433, 3399, 1883 }; 1884 1885 int i; 1886 1887 if (voltage == 0) 1888 return 0; 1889 1890 if (unlikely(voltage < 3400 || voltage >= 5000)) 1891 hid_warn_once(hid_dev, 1892 "%s: possibly using the wrong voltage curve\n", 1893 __func__); 1894 1895 for (i = 0; i < ARRAY_SIZE(voltages); i++) { 1896 if (voltage >= voltages[i]) 1897 return ARRAY_SIZE(voltages) - i; 1898 } 1899 1900 return 0; 1901 } 1902 1903 static int hidpp20_map_adc_measurement_1f20(u8 data[3], int *voltage) 1904 { 1905 int status; 1906 u8 flags; 1907 1908 flags = data[2]; 1909 1910 switch (flags) { 1911 case 0x01: 1912 status = POWER_SUPPLY_STATUS_DISCHARGING; 1913 break; 1914 case 0x03: 1915 status = POWER_SUPPLY_STATUS_CHARGING; 1916 break; 1917 case 0x07: 1918 status = POWER_SUPPLY_STATUS_FULL; 1919 break; 1920 case 0x0F: 1921 default: 1922 status = POWER_SUPPLY_STATUS_UNKNOWN; 1923 break; 1924 } 1925 1926 *voltage = get_unaligned_be16(data); 1927 1928 dbg_hid("Parsed 1f20 data as flag 0x%02x voltage %dmV\n", 1929 flags, *voltage); 1930 1931 return status; 1932 } 1933 1934 /* Return value is whether the device is online */ 1935 static bool hidpp20_get_adc_measurement_1f20(struct hidpp_device *hidpp, 1936 u8 feature_index, 1937 int *status, int *voltage) 1938 { 1939 struct hidpp_report response; 1940 int ret; 1941 u8 *params = (u8 *)response.fap.params; 1942 1943 *status = POWER_SUPPLY_STATUS_UNKNOWN; 1944 *voltage = 0; 1945 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 1946 CMD_ADC_MEASUREMENT_GET_ADC_MEASUREMENT, 1947 NULL, 0, &response); 1948 1949 if (ret > 0) { 1950 hid_dbg(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 1951 __func__, ret); 1952 return false; 1953 } 1954 1955 *status = hidpp20_map_adc_measurement_1f20(params, voltage); 1956 return true; 1957 } 1958 1959 static int hidpp20_query_adc_measurement_info_1f20(struct hidpp_device *hidpp) 1960 { 1961 if (hidpp->battery.adc_measurement_feature_index == 0xff) { 1962 int ret; 1963 1964 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_ADC_MEASUREMENT, 1965 &hidpp->battery.adc_measurement_feature_index); 1966 if (ret) 1967 return ret; 1968 1969 hidpp->capabilities |= HIDPP_CAPABILITY_ADC_MEASUREMENT; 1970 } 1971 1972 hidpp->battery.online = hidpp20_get_adc_measurement_1f20(hidpp, 1973 hidpp->battery.adc_measurement_feature_index, 1974 &hidpp->battery.status, 1975 &hidpp->battery.voltage); 1976 hidpp->battery.capacity = hidpp20_map_adc_measurement_1f20_capacity(hidpp->hid_dev, 1977 hidpp->battery.voltage); 1978 hidpp_update_usb_wireless_status(hidpp); 1979 1980 return 0; 1981 } 1982 1983 static int hidpp20_adc_measurement_event_1f20(struct hidpp_device *hidpp, 1984 u8 *data, int size) 1985 { 1986 struct hidpp_report *report = (struct hidpp_report *)data; 1987 int status, voltage; 1988 1989 if (report->fap.feature_index != hidpp->battery.adc_measurement_feature_index || 1990 report->fap.funcindex_clientid != EVENT_ADC_MEASUREMENT_STATUS_BROADCAST) 1991 return 0; 1992 1993 status = hidpp20_map_adc_measurement_1f20(report->fap.params, &voltage); 1994 1995 hidpp->battery.online = status != POWER_SUPPLY_STATUS_UNKNOWN; 1996 1997 if (voltage != hidpp->battery.voltage || status != hidpp->battery.status) { 1998 hidpp->battery.status = status; 1999 hidpp->battery.voltage = voltage; 2000 hidpp->battery.capacity = hidpp20_map_adc_measurement_1f20_capacity(hidpp->hid_dev, voltage); 2001 if (hidpp->battery.ps) 2002 power_supply_changed(hidpp->battery.ps); 2003 hidpp_update_usb_wireless_status(hidpp); 2004 } 2005 return 0; 2006 } 2007 2008 /* -------------------------------------------------------------------------- */ 2009 /* 0x2120: Hi-resolution scrolling */ 2010 /* -------------------------------------------------------------------------- */ 2011 2012 #define HIDPP_PAGE_HI_RESOLUTION_SCROLLING 0x2120 2013 2014 #define CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE 0x10 2015 2016 static int hidpp_hrs_set_highres_scrolling_mode(struct hidpp_device *hidpp, 2017 bool enabled, u8 *multiplier) 2018 { 2019 u8 feature_index; 2020 int ret; 2021 u8 params[1]; 2022 struct hidpp_report response; 2023 2024 ret = hidpp_root_get_feature(hidpp, 2025 HIDPP_PAGE_HI_RESOLUTION_SCROLLING, 2026 &feature_index); 2027 if (ret) 2028 return ret; 2029 2030 params[0] = enabled ? BIT(0) : 0; 2031 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 2032 CMD_HI_RESOLUTION_SCROLLING_SET_HIGHRES_SCROLLING_MODE, 2033 params, sizeof(params), &response); 2034 if (ret) 2035 return ret; 2036 *multiplier = response.fap.params[1]; 2037 return 0; 2038 } 2039 2040 /* -------------------------------------------------------------------------- */ 2041 /* 0x2121: HiRes Wheel */ 2042 /* -------------------------------------------------------------------------- */ 2043 2044 #define HIDPP_PAGE_HIRES_WHEEL 0x2121 2045 2046 #define CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY 0x00 2047 #define CMD_HIRES_WHEEL_SET_WHEEL_MODE 0x20 2048 2049 static int hidpp_hrw_get_wheel_capability(struct hidpp_device *hidpp, 2050 u8 *multiplier) 2051 { 2052 u8 feature_index; 2053 int ret; 2054 struct hidpp_report response; 2055 2056 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL, 2057 &feature_index); 2058 if (ret) 2059 goto return_default; 2060 2061 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 2062 CMD_HIRES_WHEEL_GET_WHEEL_CAPABILITY, 2063 NULL, 0, &response); 2064 if (ret) 2065 goto return_default; 2066 2067 *multiplier = response.fap.params[0]; 2068 return 0; 2069 return_default: 2070 hid_warn(hidpp->hid_dev, 2071 "Couldn't get wheel multiplier (error %d)\n", ret); 2072 return ret; 2073 } 2074 2075 static int hidpp_hrw_set_wheel_mode(struct hidpp_device *hidpp, bool invert, 2076 bool high_resolution, bool use_hidpp) 2077 { 2078 u8 feature_index; 2079 int ret; 2080 u8 params[1]; 2081 struct hidpp_report response; 2082 2083 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL, 2084 &feature_index); 2085 if (ret) 2086 return ret; 2087 2088 params[0] = (invert ? BIT(2) : 0) | 2089 (high_resolution ? BIT(1) : 0) | 2090 (use_hidpp ? BIT(0) : 0); 2091 2092 return hidpp_send_fap_command_sync(hidpp, feature_index, 2093 CMD_HIRES_WHEEL_SET_WHEEL_MODE, 2094 params, sizeof(params), &response); 2095 } 2096 2097 /* -------------------------------------------------------------------------- */ 2098 /* 0x4301: Solar Keyboard */ 2099 /* -------------------------------------------------------------------------- */ 2100 2101 #define HIDPP_PAGE_SOLAR_KEYBOARD 0x4301 2102 2103 #define CMD_SOLAR_SET_LIGHT_MEASURE 0x00 2104 2105 #define EVENT_SOLAR_BATTERY_BROADCAST 0x00 2106 #define EVENT_SOLAR_BATTERY_LIGHT_MEASURE 0x10 2107 #define EVENT_SOLAR_CHECK_LIGHT_BUTTON 0x20 2108 2109 static int hidpp_solar_request_battery_event(struct hidpp_device *hidpp) 2110 { 2111 struct hidpp_report response; 2112 u8 params[2] = { 1, 1 }; 2113 int ret; 2114 2115 if (hidpp->battery.feature_index == 0xff) { 2116 ret = hidpp_root_get_feature(hidpp, 2117 HIDPP_PAGE_SOLAR_KEYBOARD, 2118 &hidpp->battery.solar_feature_index); 2119 if (ret) 2120 return ret; 2121 } 2122 2123 ret = hidpp_send_fap_command_sync(hidpp, 2124 hidpp->battery.solar_feature_index, 2125 CMD_SOLAR_SET_LIGHT_MEASURE, 2126 params, 2, &response); 2127 if (ret > 0) { 2128 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 2129 __func__, ret); 2130 return -EPROTO; 2131 } 2132 if (ret) 2133 return ret; 2134 2135 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE; 2136 2137 return 0; 2138 } 2139 2140 static int hidpp_solar_battery_event(struct hidpp_device *hidpp, 2141 u8 *data, int size) 2142 { 2143 struct hidpp_report *report = (struct hidpp_report *)data; 2144 int capacity, lux, status; 2145 u8 function; 2146 2147 function = report->fap.funcindex_clientid; 2148 2149 2150 if (report->fap.feature_index != hidpp->battery.solar_feature_index || 2151 !(function == EVENT_SOLAR_BATTERY_BROADCAST || 2152 function == EVENT_SOLAR_BATTERY_LIGHT_MEASURE || 2153 function == EVENT_SOLAR_CHECK_LIGHT_BUTTON)) 2154 return 0; 2155 2156 capacity = report->fap.params[0]; 2157 2158 switch (function) { 2159 case EVENT_SOLAR_BATTERY_LIGHT_MEASURE: 2160 lux = (report->fap.params[1] << 8) | report->fap.params[2]; 2161 if (lux > 200) 2162 status = POWER_SUPPLY_STATUS_CHARGING; 2163 else 2164 status = POWER_SUPPLY_STATUS_DISCHARGING; 2165 break; 2166 case EVENT_SOLAR_CHECK_LIGHT_BUTTON: 2167 default: 2168 if (capacity < hidpp->battery.capacity) 2169 status = POWER_SUPPLY_STATUS_DISCHARGING; 2170 else 2171 status = POWER_SUPPLY_STATUS_CHARGING; 2172 2173 } 2174 2175 if (capacity == 100) 2176 status = POWER_SUPPLY_STATUS_FULL; 2177 2178 hidpp->battery.online = true; 2179 if (capacity != hidpp->battery.capacity || 2180 status != hidpp->battery.status) { 2181 hidpp->battery.capacity = capacity; 2182 hidpp->battery.status = status; 2183 if (hidpp->battery.ps) 2184 power_supply_changed(hidpp->battery.ps); 2185 } 2186 2187 return 0; 2188 } 2189 2190 /* -------------------------------------------------------------------------- */ 2191 /* 0x6010: Touchpad FW items */ 2192 /* -------------------------------------------------------------------------- */ 2193 2194 #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010 2195 2196 #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10 2197 2198 struct hidpp_touchpad_fw_items { 2199 uint8_t presence; 2200 uint8_t desired_state; 2201 uint8_t state; 2202 uint8_t persistent; 2203 }; 2204 2205 /* 2206 * send a set state command to the device by reading the current items->state 2207 * field. items is then filled with the current state. 2208 */ 2209 static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp, 2210 u8 feature_index, 2211 struct hidpp_touchpad_fw_items *items) 2212 { 2213 struct hidpp_report response; 2214 int ret; 2215 u8 *params = (u8 *)response.fap.params; 2216 2217 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 2218 CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response); 2219 2220 if (ret > 0) { 2221 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 2222 __func__, ret); 2223 return -EPROTO; 2224 } 2225 if (ret) 2226 return ret; 2227 2228 items->presence = params[0]; 2229 items->desired_state = params[1]; 2230 items->state = params[2]; 2231 items->persistent = params[3]; 2232 2233 return 0; 2234 } 2235 2236 /* -------------------------------------------------------------------------- */ 2237 /* 0x6100: TouchPadRawXY */ 2238 /* -------------------------------------------------------------------------- */ 2239 2240 #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100 2241 2242 #define CMD_TOUCHPAD_GET_RAW_INFO 0x00 2243 #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x20 2244 2245 #define EVENT_TOUCHPAD_RAW_XY 0x00 2246 2247 #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01 2248 #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03 2249 2250 struct hidpp_touchpad_raw_info { 2251 u16 x_size; 2252 u16 y_size; 2253 u8 z_range; 2254 u8 area_range; 2255 u8 timestamp_unit; 2256 u8 maxcontacts; 2257 u8 origin; 2258 u16 res; 2259 }; 2260 2261 struct hidpp_touchpad_raw_xy_finger { 2262 u8 contact_type; 2263 u8 contact_status; 2264 u16 x; 2265 u16 y; 2266 u8 z; 2267 u8 area; 2268 u8 finger_id; 2269 }; 2270 2271 struct hidpp_touchpad_raw_xy { 2272 u16 timestamp; 2273 struct hidpp_touchpad_raw_xy_finger fingers[2]; 2274 u8 spurious_flag; 2275 u8 end_of_frame; 2276 u8 finger_count; 2277 u8 button; 2278 }; 2279 2280 static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp, 2281 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info) 2282 { 2283 struct hidpp_report response; 2284 int ret; 2285 u8 *params = (u8 *)response.fap.params; 2286 2287 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 2288 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response); 2289 2290 if (ret > 0) { 2291 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 2292 __func__, ret); 2293 return -EPROTO; 2294 } 2295 if (ret) 2296 return ret; 2297 2298 raw_info->x_size = get_unaligned_be16(¶ms[0]); 2299 raw_info->y_size = get_unaligned_be16(¶ms[2]); 2300 raw_info->z_range = params[4]; 2301 raw_info->area_range = params[5]; 2302 raw_info->maxcontacts = params[7]; 2303 raw_info->origin = params[8]; 2304 /* res is given in unit per inch */ 2305 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51; 2306 2307 return ret; 2308 } 2309 2310 static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev, 2311 u8 feature_index, bool send_raw_reports, 2312 bool sensor_enhanced_settings) 2313 { 2314 struct hidpp_report response; 2315 2316 /* 2317 * Params: 2318 * bit 0 - enable raw 2319 * bit 1 - 16bit Z, no area 2320 * bit 2 - enhanced sensitivity 2321 * bit 3 - width, height (4 bits each) instead of area 2322 * bit 4 - send raw + gestures (degrades smoothness) 2323 * remaining bits - reserved 2324 */ 2325 u8 params = send_raw_reports | (sensor_enhanced_settings << 2); 2326 2327 return hidpp_send_fap_command_sync(hidpp_dev, feature_index, 2328 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response); 2329 } 2330 2331 static void hidpp_touchpad_touch_event(u8 *data, 2332 struct hidpp_touchpad_raw_xy_finger *finger) 2333 { 2334 u8 x_m = data[0] << 2; 2335 u8 y_m = data[2] << 2; 2336 2337 finger->x = x_m << 6 | data[1]; 2338 finger->y = y_m << 6 | data[3]; 2339 2340 finger->contact_type = data[0] >> 6; 2341 finger->contact_status = data[2] >> 6; 2342 2343 finger->z = data[4]; 2344 finger->area = data[5]; 2345 finger->finger_id = data[6] >> 4; 2346 } 2347 2348 static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev, 2349 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy) 2350 { 2351 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy)); 2352 raw_xy->end_of_frame = data[8] & 0x01; 2353 raw_xy->spurious_flag = (data[8] >> 1) & 0x01; 2354 raw_xy->finger_count = data[15] & 0x0f; 2355 raw_xy->button = (data[8] >> 2) & 0x01; 2356 2357 if (raw_xy->finger_count) { 2358 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]); 2359 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]); 2360 } 2361 } 2362 2363 /* -------------------------------------------------------------------------- */ 2364 /* 0x8123: Force feedback support */ 2365 /* -------------------------------------------------------------------------- */ 2366 2367 #define HIDPP_FF_GET_INFO 0x01 2368 #define HIDPP_FF_RESET_ALL 0x11 2369 #define HIDPP_FF_DOWNLOAD_EFFECT 0x21 2370 #define HIDPP_FF_SET_EFFECT_STATE 0x31 2371 #define HIDPP_FF_DESTROY_EFFECT 0x41 2372 #define HIDPP_FF_GET_APERTURE 0x51 2373 #define HIDPP_FF_SET_APERTURE 0x61 2374 #define HIDPP_FF_GET_GLOBAL_GAINS 0x71 2375 #define HIDPP_FF_SET_GLOBAL_GAINS 0x81 2376 2377 #define HIDPP_FF_EFFECT_STATE_GET 0x00 2378 #define HIDPP_FF_EFFECT_STATE_STOP 0x01 2379 #define HIDPP_FF_EFFECT_STATE_PLAY 0x02 2380 #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03 2381 2382 #define HIDPP_FF_EFFECT_CONSTANT 0x00 2383 #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01 2384 #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02 2385 #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03 2386 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04 2387 #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05 2388 #define HIDPP_FF_EFFECT_SPRING 0x06 2389 #define HIDPP_FF_EFFECT_DAMPER 0x07 2390 #define HIDPP_FF_EFFECT_FRICTION 0x08 2391 #define HIDPP_FF_EFFECT_INERTIA 0x09 2392 #define HIDPP_FF_EFFECT_RAMP 0x0A 2393 2394 #define HIDPP_FF_EFFECT_AUTOSTART 0x80 2395 2396 #define HIDPP_FF_EFFECTID_NONE -1 2397 #define HIDPP_FF_EFFECTID_AUTOCENTER -2 2398 #define HIDPP_AUTOCENTER_PARAMS_LENGTH 18 2399 2400 #define HIDPP_FF_MAX_PARAMS 20 2401 #define HIDPP_FF_RESERVED_SLOTS 1 2402 2403 struct hidpp_ff_private_data { 2404 struct hidpp_device *hidpp; 2405 u8 feature_index; 2406 u8 version; 2407 u16 gain; 2408 s16 range; 2409 u8 slot_autocenter; 2410 u8 num_effects; 2411 int *effect_ids; 2412 struct workqueue_struct *wq; 2413 atomic_t workqueue_size; 2414 }; 2415 2416 struct hidpp_ff_work_data { 2417 struct work_struct work; 2418 struct hidpp_ff_private_data *data; 2419 int effect_id; 2420 u8 command; 2421 u8 params[HIDPP_FF_MAX_PARAMS]; 2422 u8 size; 2423 }; 2424 2425 static const signed short hidpp_ff_effects[] = { 2426 FF_CONSTANT, 2427 FF_PERIODIC, 2428 FF_SINE, 2429 FF_SQUARE, 2430 FF_SAW_UP, 2431 FF_SAW_DOWN, 2432 FF_TRIANGLE, 2433 FF_SPRING, 2434 FF_DAMPER, 2435 FF_AUTOCENTER, 2436 FF_GAIN, 2437 -1 2438 }; 2439 2440 static const signed short hidpp_ff_effects_v2[] = { 2441 FF_RAMP, 2442 FF_FRICTION, 2443 FF_INERTIA, 2444 -1 2445 }; 2446 2447 static const u8 HIDPP_FF_CONDITION_CMDS[] = { 2448 HIDPP_FF_EFFECT_SPRING, 2449 HIDPP_FF_EFFECT_FRICTION, 2450 HIDPP_FF_EFFECT_DAMPER, 2451 HIDPP_FF_EFFECT_INERTIA 2452 }; 2453 2454 static const char *HIDPP_FF_CONDITION_NAMES[] = { 2455 "spring", 2456 "friction", 2457 "damper", 2458 "inertia" 2459 }; 2460 2461 2462 static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id) 2463 { 2464 int i; 2465 2466 for (i = 0; i < data->num_effects; i++) 2467 if (data->effect_ids[i] == effect_id) 2468 return i+1; 2469 2470 return 0; 2471 } 2472 2473 static void hidpp_ff_work_handler(struct work_struct *w) 2474 { 2475 struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work); 2476 struct hidpp_ff_private_data *data = wd->data; 2477 struct hidpp_report response; 2478 u8 slot; 2479 int ret; 2480 2481 /* add slot number if needed */ 2482 switch (wd->effect_id) { 2483 case HIDPP_FF_EFFECTID_AUTOCENTER: 2484 wd->params[0] = data->slot_autocenter; 2485 break; 2486 case HIDPP_FF_EFFECTID_NONE: 2487 /* leave slot as zero */ 2488 break; 2489 default: 2490 /* find current slot for effect */ 2491 wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id); 2492 break; 2493 } 2494 2495 /* send command and wait for reply */ 2496 ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index, 2497 wd->command, wd->params, wd->size, &response); 2498 2499 if (ret) { 2500 hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n"); 2501 goto out; 2502 } 2503 2504 /* parse return data */ 2505 switch (wd->command) { 2506 case HIDPP_FF_DOWNLOAD_EFFECT: 2507 slot = response.fap.params[0]; 2508 if (slot > 0 && slot <= data->num_effects) { 2509 if (wd->effect_id >= 0) 2510 /* regular effect uploaded */ 2511 data->effect_ids[slot-1] = wd->effect_id; 2512 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER) 2513 /* autocenter spring uploaded */ 2514 data->slot_autocenter = slot; 2515 } 2516 break; 2517 case HIDPP_FF_DESTROY_EFFECT: 2518 slot = wd->params[0]; 2519 if (slot > 0 && slot <= data->num_effects) { 2520 if (wd->effect_id >= 0) 2521 /* regular effect destroyed */ 2522 data->effect_ids[slot-1] = -1; 2523 else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER) 2524 /* autocenter spring destroyed */ 2525 data->slot_autocenter = 0; 2526 } 2527 break; 2528 case HIDPP_FF_SET_GLOBAL_GAINS: 2529 data->gain = (wd->params[0] << 8) + wd->params[1]; 2530 break; 2531 case HIDPP_FF_SET_APERTURE: 2532 data->range = (wd->params[0] << 8) + wd->params[1]; 2533 break; 2534 default: 2535 /* no action needed */ 2536 break; 2537 } 2538 2539 out: 2540 atomic_dec(&data->workqueue_size); 2541 kfree(wd); 2542 } 2543 2544 static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size) 2545 { 2546 struct hidpp_ff_work_data *wd = kzalloc_obj(*wd); 2547 int s; 2548 2549 if (!wd) 2550 return -ENOMEM; 2551 2552 INIT_WORK(&wd->work, hidpp_ff_work_handler); 2553 2554 wd->data = data; 2555 wd->effect_id = effect_id; 2556 wd->command = command; 2557 wd->size = size; 2558 memcpy(wd->params, params, size); 2559 2560 s = atomic_inc_return(&data->workqueue_size); 2561 queue_work(data->wq, &wd->work); 2562 2563 /* warn about excessive queue size */ 2564 if (s >= 20 && s % 20 == 0) 2565 hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s); 2566 2567 return 0; 2568 } 2569 2570 static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old) 2571 { 2572 struct hidpp_ff_private_data *data = dev->ff->private; 2573 u8 params[20]; 2574 u8 size; 2575 int force; 2576 2577 /* set common parameters */ 2578 params[2] = effect->replay.length >> 8; 2579 params[3] = effect->replay.length & 255; 2580 params[4] = effect->replay.delay >> 8; 2581 params[5] = effect->replay.delay & 255; 2582 2583 switch (effect->type) { 2584 case FF_CONSTANT: 2585 force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15; 2586 params[1] = HIDPP_FF_EFFECT_CONSTANT; 2587 params[6] = force >> 8; 2588 params[7] = force & 255; 2589 params[8] = effect->u.constant.envelope.attack_level >> 7; 2590 params[9] = effect->u.constant.envelope.attack_length >> 8; 2591 params[10] = effect->u.constant.envelope.attack_length & 255; 2592 params[11] = effect->u.constant.envelope.fade_level >> 7; 2593 params[12] = effect->u.constant.envelope.fade_length >> 8; 2594 params[13] = effect->u.constant.envelope.fade_length & 255; 2595 size = 14; 2596 dbg_hid("Uploading constant force level=%d in dir %d = %d\n", 2597 effect->u.constant.level, 2598 effect->direction, force); 2599 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n", 2600 effect->u.constant.envelope.attack_level, 2601 effect->u.constant.envelope.attack_length, 2602 effect->u.constant.envelope.fade_level, 2603 effect->u.constant.envelope.fade_length); 2604 break; 2605 case FF_PERIODIC: 2606 { 2607 switch (effect->u.periodic.waveform) { 2608 case FF_SINE: 2609 params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE; 2610 break; 2611 case FF_SQUARE: 2612 params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE; 2613 break; 2614 case FF_SAW_UP: 2615 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP; 2616 break; 2617 case FF_SAW_DOWN: 2618 params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN; 2619 break; 2620 case FF_TRIANGLE: 2621 params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE; 2622 break; 2623 default: 2624 hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform); 2625 return -EINVAL; 2626 } 2627 force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15; 2628 params[6] = effect->u.periodic.magnitude >> 8; 2629 params[7] = effect->u.periodic.magnitude & 255; 2630 params[8] = effect->u.periodic.offset >> 8; 2631 params[9] = effect->u.periodic.offset & 255; 2632 params[10] = effect->u.periodic.period >> 8; 2633 params[11] = effect->u.periodic.period & 255; 2634 params[12] = effect->u.periodic.phase >> 8; 2635 params[13] = effect->u.periodic.phase & 255; 2636 params[14] = effect->u.periodic.envelope.attack_level >> 7; 2637 params[15] = effect->u.periodic.envelope.attack_length >> 8; 2638 params[16] = effect->u.periodic.envelope.attack_length & 255; 2639 params[17] = effect->u.periodic.envelope.fade_level >> 7; 2640 params[18] = effect->u.periodic.envelope.fade_length >> 8; 2641 params[19] = effect->u.periodic.envelope.fade_length & 255; 2642 size = 20; 2643 dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n", 2644 effect->u.periodic.magnitude, effect->direction, 2645 effect->u.periodic.offset, 2646 effect->u.periodic.period, 2647 effect->u.periodic.phase); 2648 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n", 2649 effect->u.periodic.envelope.attack_level, 2650 effect->u.periodic.envelope.attack_length, 2651 effect->u.periodic.envelope.fade_level, 2652 effect->u.periodic.envelope.fade_length); 2653 break; 2654 } 2655 case FF_RAMP: 2656 params[1] = HIDPP_FF_EFFECT_RAMP; 2657 force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15; 2658 params[6] = force >> 8; 2659 params[7] = force & 255; 2660 force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15; 2661 params[8] = force >> 8; 2662 params[9] = force & 255; 2663 params[10] = effect->u.ramp.envelope.attack_level >> 7; 2664 params[11] = effect->u.ramp.envelope.attack_length >> 8; 2665 params[12] = effect->u.ramp.envelope.attack_length & 255; 2666 params[13] = effect->u.ramp.envelope.fade_level >> 7; 2667 params[14] = effect->u.ramp.envelope.fade_length >> 8; 2668 params[15] = effect->u.ramp.envelope.fade_length & 255; 2669 size = 16; 2670 dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n", 2671 effect->u.ramp.start_level, 2672 effect->u.ramp.end_level, 2673 effect->direction, force); 2674 dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n", 2675 effect->u.ramp.envelope.attack_level, 2676 effect->u.ramp.envelope.attack_length, 2677 effect->u.ramp.envelope.fade_level, 2678 effect->u.ramp.envelope.fade_length); 2679 break; 2680 case FF_FRICTION: 2681 case FF_INERTIA: 2682 case FF_SPRING: 2683 case FF_DAMPER: 2684 params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING]; 2685 params[6] = effect->u.condition[0].left_saturation >> 9; 2686 params[7] = (effect->u.condition[0].left_saturation >> 1) & 255; 2687 params[8] = effect->u.condition[0].left_coeff >> 8; 2688 params[9] = effect->u.condition[0].left_coeff & 255; 2689 params[10] = effect->u.condition[0].deadband >> 9; 2690 params[11] = (effect->u.condition[0].deadband >> 1) & 255; 2691 params[12] = effect->u.condition[0].center >> 8; 2692 params[13] = effect->u.condition[0].center & 255; 2693 params[14] = effect->u.condition[0].right_coeff >> 8; 2694 params[15] = effect->u.condition[0].right_coeff & 255; 2695 params[16] = effect->u.condition[0].right_saturation >> 9; 2696 params[17] = (effect->u.condition[0].right_saturation >> 1) & 255; 2697 size = 18; 2698 dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n", 2699 HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING], 2700 effect->u.condition[0].left_coeff, 2701 effect->u.condition[0].left_saturation, 2702 effect->u.condition[0].right_coeff, 2703 effect->u.condition[0].right_saturation); 2704 dbg_hid(" deadband=%d, center=%d\n", 2705 effect->u.condition[0].deadband, 2706 effect->u.condition[0].center); 2707 break; 2708 default: 2709 hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type); 2710 return -EINVAL; 2711 } 2712 2713 return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size); 2714 } 2715 2716 static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value) 2717 { 2718 struct hidpp_ff_private_data *data = dev->ff->private; 2719 u8 params[2]; 2720 2721 params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP; 2722 2723 dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id); 2724 2725 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params)); 2726 } 2727 2728 static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id) 2729 { 2730 struct hidpp_ff_private_data *data = dev->ff->private; 2731 u8 slot = 0; 2732 2733 dbg_hid("Erasing effect %d.\n", effect_id); 2734 2735 return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1); 2736 } 2737 2738 static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude) 2739 { 2740 struct hidpp_ff_private_data *data = dev->ff->private; 2741 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH]; 2742 2743 dbg_hid("Setting autocenter to %d.\n", magnitude); 2744 2745 /* start a standard spring effect */ 2746 params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART; 2747 /* zero delay and duration */ 2748 params[2] = params[3] = params[4] = params[5] = 0; 2749 /* set coeff to 25% of saturation */ 2750 params[8] = params[14] = magnitude >> 11; 2751 params[9] = params[15] = (magnitude >> 3) & 255; 2752 params[6] = params[16] = magnitude >> 9; 2753 params[7] = params[17] = (magnitude >> 1) & 255; 2754 /* zero deadband and center */ 2755 params[10] = params[11] = params[12] = params[13] = 0; 2756 2757 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params)); 2758 } 2759 2760 static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain) 2761 { 2762 struct hidpp_ff_private_data *data = dev->ff->private; 2763 u8 params[4]; 2764 2765 dbg_hid("Setting gain to %d.\n", gain); 2766 2767 params[0] = gain >> 8; 2768 params[1] = gain & 255; 2769 params[2] = 0; /* no boost */ 2770 params[3] = 0; 2771 2772 hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params)); 2773 } 2774 2775 static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf) 2776 { 2777 struct hid_device *hid = to_hid_device(dev); 2778 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 2779 struct input_dev *idev = hidinput->input; 2780 struct hidpp_ff_private_data *data = idev->ff->private; 2781 2782 return scnprintf(buf, PAGE_SIZE, "%u\n", data->range); 2783 } 2784 2785 static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 2786 { 2787 struct hid_device *hid = to_hid_device(dev); 2788 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 2789 struct input_dev *idev = hidinput->input; 2790 struct hidpp_ff_private_data *data = idev->ff->private; 2791 u8 params[2]; 2792 int range = simple_strtoul(buf, NULL, 10); 2793 2794 range = clamp(range, 180, 900); 2795 2796 params[0] = range >> 8; 2797 params[1] = range & 0x00FF; 2798 2799 hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params)); 2800 2801 return count; 2802 } 2803 2804 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store); 2805 2806 static void hidpp_ff_destroy(struct ff_device *ff) 2807 { 2808 struct hidpp_ff_private_data *data = ff->private; 2809 struct hid_device *hid = data->hidpp->hid_dev; 2810 2811 hid_info(hid, "Unloading HID++ force feedback.\n"); 2812 2813 device_remove_file(&hid->dev, &dev_attr_range); 2814 destroy_workqueue(data->wq); 2815 kfree(data->effect_ids); 2816 } 2817 2818 static int hidpp_ff_init(struct hidpp_device *hidpp, 2819 struct hidpp_ff_private_data *data) 2820 { 2821 struct hid_device *hid = hidpp->hid_dev; 2822 struct hid_input *hidinput; 2823 struct input_dev *dev; 2824 struct usb_device_descriptor *udesc; 2825 u16 bcdDevice; 2826 struct ff_device *ff; 2827 int error, j, num_slots = data->num_effects; 2828 u8 version; 2829 2830 if (!hid_is_usb(hid)) { 2831 hid_err(hid, "device is not USB\n"); 2832 return -ENODEV; 2833 } 2834 2835 if (list_empty(&hid->inputs)) { 2836 hid_err(hid, "no inputs found\n"); 2837 return -ENODEV; 2838 } 2839 hidinput = list_entry(hid->inputs.next, struct hid_input, list); 2840 dev = hidinput->input; 2841 2842 if (!dev) { 2843 hid_err(hid, "Struct input_dev not set!\n"); 2844 return -EINVAL; 2845 } 2846 2847 /* Get firmware release */ 2848 udesc = &(hid_to_usb_dev(hid)->descriptor); 2849 bcdDevice = le16_to_cpu(udesc->bcdDevice); 2850 version = bcdDevice & 255; 2851 2852 /* Set supported force feedback capabilities */ 2853 for (j = 0; hidpp_ff_effects[j] >= 0; j++) 2854 set_bit(hidpp_ff_effects[j], dev->ffbit); 2855 if (version > 1) 2856 for (j = 0; hidpp_ff_effects_v2[j] >= 0; j++) 2857 set_bit(hidpp_ff_effects_v2[j], dev->ffbit); 2858 2859 error = input_ff_create(dev, num_slots); 2860 2861 if (error) { 2862 hid_err(dev, "Failed to create FF device!\n"); 2863 return error; 2864 } 2865 /* 2866 * Create a copy of passed data, so we can transfer memory 2867 * ownership to FF core 2868 */ 2869 data = kmemdup(data, sizeof(*data), GFP_KERNEL); 2870 if (!data) { 2871 error = -ENOMEM; 2872 goto err_destroy_ff; 2873 } 2874 data->effect_ids = kzalloc_objs(int, num_slots); 2875 if (!data->effect_ids) { 2876 error = -ENOMEM; 2877 goto err_free_data; 2878 } 2879 data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue"); 2880 if (!data->wq) { 2881 error = -ENOMEM; 2882 goto err_free_effect_ids; 2883 } 2884 2885 data->hidpp = hidpp; 2886 data->version = version; 2887 for (j = 0; j < num_slots; j++) 2888 data->effect_ids[j] = -1; 2889 2890 ff = dev->ff; 2891 ff->private = data; 2892 2893 ff->upload = hidpp_ff_upload_effect; 2894 ff->erase = hidpp_ff_erase_effect; 2895 ff->playback = hidpp_ff_playback; 2896 ff->set_gain = hidpp_ff_set_gain; 2897 ff->set_autocenter = hidpp_ff_set_autocenter; 2898 ff->destroy = hidpp_ff_destroy; 2899 2900 /* Create sysfs interface */ 2901 error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range); 2902 if (error) 2903 hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error); 2904 2905 /* init the hardware command queue */ 2906 atomic_set(&data->workqueue_size, 0); 2907 2908 hid_info(hid, "Force feedback support loaded (firmware release %d).\n", 2909 version); 2910 2911 return 0; 2912 2913 err_free_effect_ids: 2914 kfree(data->effect_ids); 2915 err_free_data: 2916 kfree(data); 2917 err_destroy_ff: 2918 input_ff_destroy(dev); 2919 return error; 2920 } 2921 2922 /* ************************************************************************** */ 2923 /* */ 2924 /* Device Support */ 2925 /* */ 2926 /* ************************************************************************** */ 2927 2928 /* -------------------------------------------------------------------------- */ 2929 /* Touchpad HID++ devices */ 2930 /* -------------------------------------------------------------------------- */ 2931 2932 #define WTP_MANUAL_RESOLUTION 39 2933 2934 struct wtp_data { 2935 u16 x_size, y_size; 2936 u8 finger_count; 2937 u8 mt_feature_index; 2938 u8 button_feature_index; 2939 u8 maxcontacts; 2940 bool flip_y; 2941 unsigned int resolution; 2942 }; 2943 2944 static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi, 2945 struct hid_field *field, struct hid_usage *usage, 2946 unsigned long **bit, int *max) 2947 { 2948 return -1; 2949 } 2950 2951 static void wtp_populate_input(struct hidpp_device *hidpp, 2952 struct input_dev *input_dev) 2953 { 2954 struct wtp_data *wd = hidpp->private_data; 2955 2956 __set_bit(EV_ABS, input_dev->evbit); 2957 __set_bit(EV_KEY, input_dev->evbit); 2958 __clear_bit(EV_REL, input_dev->evbit); 2959 __clear_bit(EV_LED, input_dev->evbit); 2960 2961 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0); 2962 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution); 2963 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0); 2964 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution); 2965 2966 /* Max pressure is not given by the devices, pick one */ 2967 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0); 2968 2969 input_set_capability(input_dev, EV_KEY, BTN_LEFT); 2970 2971 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) 2972 input_set_capability(input_dev, EV_KEY, BTN_RIGHT); 2973 else 2974 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit); 2975 2976 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER | 2977 INPUT_MT_DROP_UNUSED); 2978 } 2979 2980 static void wtp_touch_event(struct hidpp_device *hidpp, 2981 struct hidpp_touchpad_raw_xy_finger *touch_report) 2982 { 2983 struct wtp_data *wd = hidpp->private_data; 2984 int slot; 2985 2986 if (!touch_report->finger_id || touch_report->contact_type) 2987 /* no actual data */ 2988 return; 2989 2990 slot = input_mt_get_slot_by_key(hidpp->input, touch_report->finger_id); 2991 2992 input_mt_slot(hidpp->input, slot); 2993 input_mt_report_slot_state(hidpp->input, MT_TOOL_FINGER, 2994 touch_report->contact_status); 2995 if (touch_report->contact_status) { 2996 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_X, 2997 touch_report->x); 2998 input_event(hidpp->input, EV_ABS, ABS_MT_POSITION_Y, 2999 wd->flip_y ? wd->y_size - touch_report->y : 3000 touch_report->y); 3001 input_event(hidpp->input, EV_ABS, ABS_MT_PRESSURE, 3002 touch_report->area); 3003 } 3004 } 3005 3006 static void wtp_send_raw_xy_event(struct hidpp_device *hidpp, 3007 struct hidpp_touchpad_raw_xy *raw) 3008 { 3009 int i; 3010 3011 for (i = 0; i < 2; i++) 3012 wtp_touch_event(hidpp, &(raw->fingers[i])); 3013 3014 if (raw->end_of_frame && 3015 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)) 3016 input_event(hidpp->input, EV_KEY, BTN_LEFT, raw->button); 3017 3018 if (raw->end_of_frame || raw->finger_count <= 2) { 3019 input_mt_sync_frame(hidpp->input); 3020 input_sync(hidpp->input); 3021 } 3022 } 3023 3024 static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data) 3025 { 3026 struct wtp_data *wd = hidpp->private_data; 3027 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) + 3028 (data[7] >> 4) * (data[7] >> 4)) / 2; 3029 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) + 3030 (data[13] >> 4) * (data[13] >> 4)) / 2; 3031 struct hidpp_touchpad_raw_xy raw = { 3032 .timestamp = data[1], 3033 .fingers = { 3034 { 3035 .contact_type = 0, 3036 .contact_status = !!data[7], 3037 .x = get_unaligned_le16(&data[3]), 3038 .y = get_unaligned_le16(&data[5]), 3039 .z = c1_area, 3040 .area = c1_area, 3041 .finger_id = data[2], 3042 }, { 3043 .contact_type = 0, 3044 .contact_status = !!data[13], 3045 .x = get_unaligned_le16(&data[9]), 3046 .y = get_unaligned_le16(&data[11]), 3047 .z = c2_area, 3048 .area = c2_area, 3049 .finger_id = data[8], 3050 } 3051 }, 3052 .finger_count = wd->maxcontacts, 3053 .spurious_flag = 0, 3054 .end_of_frame = (data[0] >> 7) == 0, 3055 .button = data[0] & 0x01, 3056 }; 3057 3058 wtp_send_raw_xy_event(hidpp, &raw); 3059 3060 return 1; 3061 } 3062 3063 static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size) 3064 { 3065 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 3066 struct wtp_data *wd = hidpp->private_data; 3067 struct hidpp_report *report = (struct hidpp_report *)data; 3068 struct hidpp_touchpad_raw_xy raw; 3069 3070 if (!wd || !hidpp->input) 3071 return 1; 3072 3073 switch (data[0]) { 3074 case 0x02: 3075 if (size < 2) { 3076 hid_err(hdev, "Received HID report of bad size (%d)", 3077 size); 3078 return 1; 3079 } 3080 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) { 3081 input_event(hidpp->input, EV_KEY, BTN_LEFT, 3082 !!(data[1] & 0x01)); 3083 input_event(hidpp->input, EV_KEY, BTN_RIGHT, 3084 !!(data[1] & 0x02)); 3085 input_sync(hidpp->input); 3086 return 0; 3087 } else { 3088 if (size < 21) 3089 return 1; 3090 return wtp_mouse_raw_xy_event(hidpp, &data[7]); 3091 } 3092 case REPORT_ID_HIDPP_LONG: 3093 /* size is already checked in hidpp_raw_event. */ 3094 if ((report->fap.feature_index != wd->mt_feature_index) || 3095 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY)) 3096 return 1; 3097 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw); 3098 3099 wtp_send_raw_xy_event(hidpp, &raw); 3100 return 0; 3101 } 3102 3103 return 0; 3104 } 3105 3106 static int wtp_get_config(struct hidpp_device *hidpp) 3107 { 3108 struct wtp_data *wd = hidpp->private_data; 3109 struct hidpp_touchpad_raw_info raw_info = {0}; 3110 int ret; 3111 3112 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY, 3113 &wd->mt_feature_index); 3114 if (ret) 3115 /* means that the device is not powered up */ 3116 return ret; 3117 3118 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index, 3119 &raw_info); 3120 if (ret) 3121 return ret; 3122 3123 wd->x_size = raw_info.x_size; 3124 wd->y_size = raw_info.y_size; 3125 wd->maxcontacts = raw_info.maxcontacts; 3126 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT; 3127 wd->resolution = raw_info.res; 3128 if (!wd->resolution) 3129 wd->resolution = WTP_MANUAL_RESOLUTION; 3130 3131 return 0; 3132 } 3133 3134 static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id) 3135 { 3136 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 3137 struct wtp_data *wd; 3138 3139 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data), 3140 GFP_KERNEL); 3141 if (!wd) 3142 return -ENOMEM; 3143 3144 hidpp->private_data = wd; 3145 3146 return 0; 3147 }; 3148 3149 static int wtp_connect(struct hid_device *hdev) 3150 { 3151 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 3152 struct wtp_data *wd = hidpp->private_data; 3153 int ret; 3154 3155 if (!wd->x_size) { 3156 ret = wtp_get_config(hidpp); 3157 if (ret) { 3158 hid_err(hdev, "Can not get wtp config: %d\n", ret); 3159 return ret; 3160 } 3161 } 3162 3163 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index, 3164 true, true); 3165 } 3166 3167 /* ------------------------------------------------------------------------- */ 3168 /* Logitech M560 devices */ 3169 /* ------------------------------------------------------------------------- */ 3170 3171 /* 3172 * Logitech M560 protocol overview 3173 * 3174 * The Logitech M560 mouse, is designed for windows 8. When the middle and/or 3175 * the sides buttons are pressed, it sends some keyboard keys events 3176 * instead of buttons ones. 3177 * To complicate things further, the middle button keys sequence 3178 * is different from the odd press and the even press. 3179 * 3180 * forward button -> Super_R 3181 * backward button -> Super_L+'d' (press only) 3182 * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only) 3183 * 2nd time: left-click (press only) 3184 * NB: press-only means that when the button is pressed, the 3185 * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated 3186 * together sequentially; instead when the button is released, no event is 3187 * generated ! 3188 * 3189 * With the command 3190 * 10<xx>0a 3500af03 (where <xx> is the mouse id), 3191 * the mouse reacts differently: 3192 * - it never sends a keyboard key event 3193 * - for the three mouse button it sends: 3194 * middle button press 11<xx>0a 3500af00... 3195 * side 1 button (forward) press 11<xx>0a 3500b000... 3196 * side 2 button (backward) press 11<xx>0a 3500ae00... 3197 * middle/side1/side2 button release 11<xx>0a 35000000... 3198 */ 3199 3200 static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03}; 3201 3202 /* how buttons are mapped in the report */ 3203 #define M560_MOUSE_BTN_LEFT 0x01 3204 #define M560_MOUSE_BTN_RIGHT 0x02 3205 #define M560_MOUSE_BTN_WHEEL_LEFT 0x08 3206 #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10 3207 3208 #define M560_SUB_ID 0x0a 3209 #define M560_BUTTON_MODE_REGISTER 0x35 3210 3211 static int m560_send_config_command(struct hid_device *hdev) 3212 { 3213 struct hidpp_report response; 3214 struct hidpp_device *hidpp_dev; 3215 3216 hidpp_dev = hid_get_drvdata(hdev); 3217 3218 return hidpp_send_rap_command_sync( 3219 hidpp_dev, 3220 REPORT_ID_HIDPP_SHORT, 3221 M560_SUB_ID, 3222 M560_BUTTON_MODE_REGISTER, 3223 (u8 *)m560_config_parameter, 3224 sizeof(m560_config_parameter), 3225 &response 3226 ); 3227 } 3228 3229 static int m560_raw_event(struct hid_device *hdev, u8 *data, int size) 3230 { 3231 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 3232 3233 /* sanity check */ 3234 if (!hidpp->input) { 3235 hid_err(hdev, "error in parameter\n"); 3236 return -EINVAL; 3237 } 3238 3239 if (size < 7) { 3240 hid_err(hdev, "error in report\n"); 3241 return 0; 3242 } 3243 3244 if (data[0] == REPORT_ID_HIDPP_LONG && 3245 data[2] == M560_SUB_ID && data[6] == 0x00) { 3246 /* 3247 * m560 mouse report for middle, forward and backward button 3248 * 3249 * data[0] = 0x11 3250 * data[1] = device-id 3251 * data[2] = 0x0a 3252 * data[5] = 0xaf -> middle 3253 * 0xb0 -> forward 3254 * 0xae -> backward 3255 * 0x00 -> release all 3256 * data[6] = 0x00 3257 */ 3258 3259 switch (data[5]) { 3260 case 0xaf: 3261 input_report_key(hidpp->input, BTN_MIDDLE, 1); 3262 break; 3263 case 0xb0: 3264 input_report_key(hidpp->input, BTN_FORWARD, 1); 3265 break; 3266 case 0xae: 3267 input_report_key(hidpp->input, BTN_BACK, 1); 3268 break; 3269 case 0x00: 3270 input_report_key(hidpp->input, BTN_BACK, 0); 3271 input_report_key(hidpp->input, BTN_FORWARD, 0); 3272 input_report_key(hidpp->input, BTN_MIDDLE, 0); 3273 break; 3274 default: 3275 hid_err(hdev, "error in report\n"); 3276 return 0; 3277 } 3278 input_sync(hidpp->input); 3279 3280 } else if (data[0] == 0x02) { 3281 /* 3282 * Logitech M560 mouse report 3283 * 3284 * data[0] = type (0x02) 3285 * data[1..2] = buttons 3286 * data[3..5] = xy 3287 * data[6] = wheel 3288 */ 3289 3290 int v; 3291 3292 input_report_key(hidpp->input, BTN_LEFT, 3293 !!(data[1] & M560_MOUSE_BTN_LEFT)); 3294 input_report_key(hidpp->input, BTN_RIGHT, 3295 !!(data[1] & M560_MOUSE_BTN_RIGHT)); 3296 3297 if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) { 3298 input_report_rel(hidpp->input, REL_HWHEEL, -1); 3299 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, 3300 -120); 3301 } else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) { 3302 input_report_rel(hidpp->input, REL_HWHEEL, 1); 3303 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, 3304 120); 3305 } 3306 3307 v = sign_extend32(hid_field_extract(hdev, data + 3, 0, 12), 11); 3308 input_report_rel(hidpp->input, REL_X, v); 3309 3310 v = sign_extend32(hid_field_extract(hdev, data + 3, 12, 12), 11); 3311 input_report_rel(hidpp->input, REL_Y, v); 3312 3313 v = sign_extend32(data[6], 7); 3314 if (v != 0) 3315 hidpp_scroll_counter_handle_scroll(hidpp->input, 3316 &hidpp->vertical_wheel_counter, v); 3317 3318 input_sync(hidpp->input); 3319 } 3320 3321 return 1; 3322 } 3323 3324 static void m560_populate_input(struct hidpp_device *hidpp, 3325 struct input_dev *input_dev) 3326 { 3327 __set_bit(EV_KEY, input_dev->evbit); 3328 __set_bit(BTN_MIDDLE, input_dev->keybit); 3329 __set_bit(BTN_RIGHT, input_dev->keybit); 3330 __set_bit(BTN_LEFT, input_dev->keybit); 3331 __set_bit(BTN_BACK, input_dev->keybit); 3332 __set_bit(BTN_FORWARD, input_dev->keybit); 3333 3334 __set_bit(EV_REL, input_dev->evbit); 3335 __set_bit(REL_X, input_dev->relbit); 3336 __set_bit(REL_Y, input_dev->relbit); 3337 __set_bit(REL_WHEEL, input_dev->relbit); 3338 __set_bit(REL_HWHEEL, input_dev->relbit); 3339 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit); 3340 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit); 3341 } 3342 3343 static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi, 3344 struct hid_field *field, struct hid_usage *usage, 3345 unsigned long **bit, int *max) 3346 { 3347 return -1; 3348 } 3349 3350 /* ------------------------------------------------------------------------- */ 3351 /* Logitech K400 devices */ 3352 /* ------------------------------------------------------------------------- */ 3353 3354 /* 3355 * The Logitech K400 keyboard has an embedded touchpad which is seen 3356 * as a mouse from the OS point of view. There is a hardware shortcut to disable 3357 * tap-to-click but the setting is not remembered accross reset, annoying some 3358 * users. 3359 * 3360 * We can toggle this feature from the host by using the feature 0x6010: 3361 * Touchpad FW items 3362 */ 3363 3364 struct k400_private_data { 3365 u8 feature_index; 3366 }; 3367 3368 static int k400_disable_tap_to_click(struct hidpp_device *hidpp) 3369 { 3370 struct k400_private_data *k400 = hidpp->private_data; 3371 struct hidpp_touchpad_fw_items items = {}; 3372 int ret; 3373 3374 if (!k400->feature_index) { 3375 ret = hidpp_root_get_feature(hidpp, 3376 HIDPP_PAGE_TOUCHPAD_FW_ITEMS, 3377 &k400->feature_index); 3378 if (ret) 3379 /* means that the device is not powered up */ 3380 return ret; 3381 } 3382 3383 ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items); 3384 if (ret) 3385 return ret; 3386 3387 return 0; 3388 } 3389 3390 static int k400_allocate(struct hid_device *hdev) 3391 { 3392 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 3393 struct k400_private_data *k400; 3394 3395 k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data), 3396 GFP_KERNEL); 3397 if (!k400) 3398 return -ENOMEM; 3399 3400 hidpp->private_data = k400; 3401 3402 return 0; 3403 }; 3404 3405 static int k400_connect(struct hid_device *hdev) 3406 { 3407 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 3408 3409 if (!disable_tap_to_click) 3410 return 0; 3411 3412 return k400_disable_tap_to_click(hidpp); 3413 } 3414 3415 /* ------------------------------------------------------------------------- */ 3416 /* Logitech G920 Driving Force Racing Wheel for Xbox One */ 3417 /* ------------------------------------------------------------------------- */ 3418 3419 #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123 3420 3421 static int g920_ff_set_autocenter(struct hidpp_device *hidpp, 3422 struct hidpp_ff_private_data *data) 3423 { 3424 struct hidpp_report response; 3425 u8 params[HIDPP_AUTOCENTER_PARAMS_LENGTH] = { 3426 [1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART, 3427 }; 3428 int ret; 3429 3430 /* initialize with zero autocenter to get wheel in usable state */ 3431 3432 dbg_hid("Setting autocenter to 0.\n"); 3433 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index, 3434 HIDPP_FF_DOWNLOAD_EFFECT, 3435 params, ARRAY_SIZE(params), 3436 &response); 3437 if (ret) 3438 hid_warn(hidpp->hid_dev, "Failed to autocenter device!\n"); 3439 else 3440 data->slot_autocenter = response.fap.params[0]; 3441 3442 return ret; 3443 } 3444 3445 static int g920_get_config(struct hidpp_device *hidpp, 3446 struct hidpp_ff_private_data *data) 3447 { 3448 struct hidpp_report response; 3449 int ret; 3450 3451 memset(data, 0, sizeof(*data)); 3452 3453 /* Find feature and store for later use */ 3454 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK, 3455 &data->feature_index); 3456 if (ret) 3457 return ret; 3458 3459 /* Read number of slots available in device */ 3460 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index, 3461 HIDPP_FF_GET_INFO, 3462 NULL, 0, 3463 &response); 3464 if (ret) { 3465 if (ret < 0) 3466 return ret; 3467 hid_err(hidpp->hid_dev, 3468 "%s: received protocol error 0x%02x\n", __func__, ret); 3469 return -EPROTO; 3470 } 3471 3472 data->num_effects = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS; 3473 3474 /* reset all forces */ 3475 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index, 3476 HIDPP_FF_RESET_ALL, 3477 NULL, 0, 3478 &response); 3479 if (ret) 3480 hid_warn(hidpp->hid_dev, "Failed to reset all forces!\n"); 3481 3482 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index, 3483 HIDPP_FF_GET_APERTURE, 3484 NULL, 0, 3485 &response); 3486 if (ret) { 3487 hid_warn(hidpp->hid_dev, 3488 "Failed to read range from device!\n"); 3489 } 3490 data->range = ret ? 3491 900 : get_unaligned_be16(&response.fap.params[0]); 3492 3493 /* Read the current gain values */ 3494 ret = hidpp_send_fap_command_sync(hidpp, data->feature_index, 3495 HIDPP_FF_GET_GLOBAL_GAINS, 3496 NULL, 0, 3497 &response); 3498 if (ret) 3499 hid_warn(hidpp->hid_dev, 3500 "Failed to read gain values from device!\n"); 3501 data->gain = ret ? 3502 0xffff : get_unaligned_be16(&response.fap.params[0]); 3503 3504 /* ignore boost value at response.fap.params[2] */ 3505 3506 return g920_ff_set_autocenter(hidpp, data); 3507 } 3508 3509 /* -------------------------------------------------------------------------- */ 3510 /* Logitech Dinovo Mini keyboard with builtin touchpad */ 3511 /* -------------------------------------------------------------------------- */ 3512 #define DINOVO_MINI_PRODUCT_ID 0xb30c 3513 3514 static int lg_dinovo_input_mapping(struct hid_device *hdev, struct hid_input *hi, 3515 struct hid_field *field, struct hid_usage *usage, 3516 unsigned long **bit, int *max) 3517 { 3518 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_LOGIVENDOR) 3519 return 0; 3520 3521 switch (usage->hid & HID_USAGE) { 3522 case 0x00d: lg_map_key_clear(KEY_MEDIA); break; 3523 default: 3524 return 0; 3525 } 3526 return 1; 3527 } 3528 3529 /* -------------------------------------------------------------------------- */ 3530 /* HID++1.0 devices which use HID++ reports for their wheels */ 3531 /* -------------------------------------------------------------------------- */ 3532 static int hidpp10_wheel_connect(struct hidpp_device *hidpp) 3533 { 3534 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0, 3535 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT, 3536 HIDPP_ENABLE_WHEEL_REPORT | HIDPP_ENABLE_HWHEEL_REPORT); 3537 } 3538 3539 static int hidpp10_wheel_raw_event(struct hidpp_device *hidpp, 3540 u8 *data, int size) 3541 { 3542 s8 value, hvalue; 3543 3544 if (!hidpp->input) 3545 return -EINVAL; 3546 3547 if (size < 7) 3548 return 0; 3549 3550 if (data[0] != REPORT_ID_HIDPP_SHORT || data[2] != HIDPP_SUB_ID_ROLLER) 3551 return 0; 3552 3553 value = data[3]; 3554 hvalue = data[4]; 3555 3556 input_report_rel(hidpp->input, REL_WHEEL, value); 3557 input_report_rel(hidpp->input, REL_WHEEL_HI_RES, value * 120); 3558 input_report_rel(hidpp->input, REL_HWHEEL, hvalue); 3559 input_report_rel(hidpp->input, REL_HWHEEL_HI_RES, hvalue * 120); 3560 input_sync(hidpp->input); 3561 3562 return 1; 3563 } 3564 3565 static void hidpp10_wheel_populate_input(struct hidpp_device *hidpp, 3566 struct input_dev *input_dev) 3567 { 3568 __set_bit(EV_REL, input_dev->evbit); 3569 __set_bit(REL_WHEEL, input_dev->relbit); 3570 __set_bit(REL_WHEEL_HI_RES, input_dev->relbit); 3571 __set_bit(REL_HWHEEL, input_dev->relbit); 3572 __set_bit(REL_HWHEEL_HI_RES, input_dev->relbit); 3573 } 3574 3575 /* -------------------------------------------------------------------------- */ 3576 /* HID++1.0 mice which use HID++ reports for extra mouse buttons */ 3577 /* -------------------------------------------------------------------------- */ 3578 static int hidpp10_extra_mouse_buttons_connect(struct hidpp_device *hidpp) 3579 { 3580 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0, 3581 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT, 3582 HIDPP_ENABLE_MOUSE_EXTRA_BTN_REPORT); 3583 } 3584 3585 static int hidpp10_extra_mouse_buttons_raw_event(struct hidpp_device *hidpp, 3586 u8 *data, int size) 3587 { 3588 int i; 3589 3590 if (!hidpp->input) 3591 return -EINVAL; 3592 3593 if (size < 7) 3594 return 0; 3595 3596 if (data[0] != REPORT_ID_HIDPP_SHORT || 3597 data[2] != HIDPP_SUB_ID_MOUSE_EXTRA_BTNS) 3598 return 0; 3599 3600 /* 3601 * Buttons are either delivered through the regular mouse report *or* 3602 * through the extra buttons report. At least for button 6 how it is 3603 * delivered differs per receiver firmware version. Even receivers with 3604 * the same usb-id show different behavior, so we handle both cases. 3605 */ 3606 for (i = 0; i < 8; i++) 3607 input_report_key(hidpp->input, BTN_MOUSE + i, 3608 (data[3] & (1 << i))); 3609 3610 /* Some mice report events on button 9+, use BTN_MISC */ 3611 for (i = 0; i < 8; i++) 3612 input_report_key(hidpp->input, BTN_MISC + i, 3613 (data[4] & (1 << i))); 3614 3615 input_sync(hidpp->input); 3616 return 1; 3617 } 3618 3619 /* -------------------------------------------------------------------------- */ 3620 /* HID++2.0 reprogrammable controls */ 3621 /* -------------------------------------------------------------------------- */ 3622 3623 #define HIDPP_PAGE_REPROG_CONTROLS_V4 0x1b04 3624 3625 #define HIDPP_REPROG_CONTROLS_GET_COUNT 0x00 3626 #define HIDPP_REPROG_CONTROLS_GET_CID_INFO 0x10 3627 #define HIDPP_REPROG_CONTROLS_SET_CONTROL_REPORTING 0x30 3628 3629 #define HIDPP_REPROG_CONTROLS_FLAG_MOUSE BIT(0) 3630 #define HIDPP_REPROG_CONTROLS_FLAG_DIVERT BIT(5) 3631 3632 #define HIDPP_REPROG_CONTROLS_TEMPORARY_DIVERTED BIT(0) 3633 #define HIDPP_REPROG_CONTROLS_CHANGE_TEMPORARY_DIVERT BIT(1) 3634 3635 #define HIDPP_REPROG_CONTROLS_EVENT_DIVERTED 0x00 3636 3637 #define HIDPP_REPROG_CONTROL_BACK 0x0053 3638 #define HIDPP_REPROG_CONTROL_FORWARD 0x0056 3639 3640 #define HIDPP_PRODUCT_SIGNATURE_M650 0xb02a 3641 3642 struct hidpp_reprog_control_mapping { 3643 u16 control; 3644 u16 code; 3645 }; 3646 3647 static const struct hidpp_reprog_control_mapping m650_reprog_control_mappings[] = { 3648 { HIDPP_REPROG_CONTROL_BACK, BTN_BACK }, 3649 { HIDPP_REPROG_CONTROL_FORWARD, BTN_FORWARD }, 3650 { } 3651 }; 3652 3653 static const struct hidpp_reprog_control_mapping * 3654 hidpp20_reprog_controls_get_mappings(struct hidpp_device *hidpp) 3655 { 3656 switch (hidpp->hid_dev->product) { 3657 case HIDPP_PRODUCT_SIGNATURE_M650: 3658 return m650_reprog_control_mappings; 3659 } 3660 3661 return NULL; 3662 } 3663 3664 static int hidpp20_reprog_controls_get_count(struct hidpp_device *hidpp) 3665 { 3666 struct hidpp_report response; 3667 u8 feature_index = hidpp->reprog_controls_feature_index; 3668 u8 cmd = HIDPP_REPROG_CONTROLS_GET_COUNT; 3669 int ret; 3670 3671 ret = hidpp_send_fap_command_sync(hidpp, feature_index, cmd, NULL, 0, 3672 &response); 3673 if (ret > 0) 3674 return -EPROTO; 3675 if (ret) 3676 return ret; 3677 3678 return response.fap.params[0]; 3679 } 3680 3681 static int hidpp20_reprog_controls_get_cid_info(struct hidpp_device *hidpp, 3682 u8 index, u16 *control, 3683 u8 *flags) 3684 { 3685 struct hidpp_report response; 3686 u8 feature_index = hidpp->reprog_controls_feature_index; 3687 u8 cmd = HIDPP_REPROG_CONTROLS_GET_CID_INFO; 3688 int ret; 3689 3690 ret = hidpp_send_fap_command_sync(hidpp, feature_index, cmd, &index, 3691 sizeof(index), &response); 3692 if (ret > 0) 3693 return -EPROTO; 3694 if (ret) 3695 return ret; 3696 3697 *control = get_unaligned_be16(&response.fap.params[0]); 3698 *flags = response.fap.params[4]; 3699 3700 return 0; 3701 } 3702 3703 static bool hidpp20_reprog_controls_find_control(struct hidpp_device *hidpp, 3704 u16 control) 3705 { 3706 int count, ret; 3707 u16 cid; 3708 u8 flags; 3709 int i; 3710 3711 count = hidpp20_reprog_controls_get_count(hidpp); 3712 if (count < 0) 3713 return false; 3714 3715 for (i = 0; i < count; i++) { 3716 ret = hidpp20_reprog_controls_get_cid_info(hidpp, i, &cid, 3717 &flags); 3718 if (ret) 3719 return false; 3720 3721 if (cid == control) 3722 return (flags & HIDPP_REPROG_CONTROLS_FLAG_MOUSE) && 3723 (flags & HIDPP_REPROG_CONTROLS_FLAG_DIVERT); 3724 } 3725 3726 return false; 3727 } 3728 3729 static int hidpp20_reprog_controls_set_control_reporting(struct hidpp_device *hidpp, 3730 u16 control, u8 flags) 3731 { 3732 struct hidpp_report response; 3733 u8 params[5]; 3734 3735 put_unaligned_be16(control, ¶ms[0]); 3736 params[2] = flags; 3737 put_unaligned_be16(control, ¶ms[3]); 3738 3739 return hidpp_send_fap_command_sync(hidpp, 3740 hidpp->reprog_controls_feature_index, 3741 HIDPP_REPROG_CONTROLS_SET_CONTROL_REPORTING, 3742 params, sizeof(params), &response); 3743 } 3744 3745 static void hidpp20_reprog_controls_connect(struct hidpp_device *hidpp) 3746 { 3747 const struct hidpp_reprog_control_mapping *mapping; 3748 u8 flags = HIDPP_REPROG_CONTROLS_TEMPORARY_DIVERTED | 3749 HIDPP_REPROG_CONTROLS_CHANGE_TEMPORARY_DIVERT; 3750 3751 if (!(hidpp->quirks & HIDPP_QUIRK_HIDPP_REPROG_CONTROLS_BTNS)) 3752 return; 3753 3754 if (!hidpp->reprog_controls) 3755 return; 3756 3757 if (hidpp_root_get_feature(hidpp, HIDPP_PAGE_REPROG_CONTROLS_V4, 3758 &hidpp->reprog_controls_feature_index)) 3759 return; 3760 3761 for (mapping = hidpp->reprog_controls; mapping->control; mapping++) { 3762 if (!hidpp20_reprog_controls_find_control(hidpp, mapping->control)) 3763 continue; 3764 3765 hidpp20_reprog_controls_set_control_reporting(hidpp, 3766 mapping->control, 3767 flags); 3768 } 3769 } 3770 3771 static int hidpp20_reprog_controls_raw_event(struct hidpp_device *hidpp, 3772 u8 *data, int size) 3773 { 3774 const struct hidpp_reprog_control_mapping *mapping; 3775 struct hidpp_report *report = (struct hidpp_report *)data; 3776 u16 controls[4]; 3777 bool pressed; 3778 unsigned int i, j; 3779 3780 if (!(hidpp->quirks & HIDPP_QUIRK_HIDPP_REPROG_CONTROLS_BTNS) || 3781 !hidpp->input || 3782 !hidpp->reprog_controls || 3783 hidpp->reprog_controls_feature_index == 0xff) 3784 return 0; 3785 3786 if (size < HIDPP_REPORT_LONG_LENGTH || 3787 report->fap.feature_index != hidpp->reprog_controls_feature_index || 3788 report->fap.funcindex_clientid != HIDPP_REPROG_CONTROLS_EVENT_DIVERTED) 3789 return 0; 3790 3791 for (i = 0; i < ARRAY_SIZE(controls); i++) 3792 controls[i] = get_unaligned_be16(&report->fap.params[i * 2]); 3793 3794 for (mapping = hidpp->reprog_controls; mapping->control; mapping++) { 3795 pressed = false; 3796 3797 for (j = 0; j < ARRAY_SIZE(controls); j++) { 3798 if (controls[j] == mapping->control) { 3799 pressed = true; 3800 break; 3801 } 3802 } 3803 3804 input_report_key(hidpp->input, mapping->code, pressed); 3805 } 3806 3807 input_sync(hidpp->input); 3808 3809 return 1; 3810 } 3811 3812 static void hidpp20_reprog_controls_populate_input(struct hidpp_device *hidpp, 3813 struct input_dev *input_dev) 3814 { 3815 const struct hidpp_reprog_control_mapping *mapping; 3816 3817 if (!hidpp->reprog_controls) 3818 return; 3819 3820 for (mapping = hidpp->reprog_controls; mapping->control; mapping++) 3821 input_set_capability(input_dev, EV_KEY, mapping->code); 3822 } 3823 3824 static void hidpp10_extra_mouse_buttons_populate_input( 3825 struct hidpp_device *hidpp, struct input_dev *input_dev) 3826 { 3827 /* BTN_MOUSE - BTN_MOUSE+7 are set already by the descriptor */ 3828 __set_bit(BTN_0, input_dev->keybit); 3829 __set_bit(BTN_1, input_dev->keybit); 3830 __set_bit(BTN_2, input_dev->keybit); 3831 __set_bit(BTN_3, input_dev->keybit); 3832 __set_bit(BTN_4, input_dev->keybit); 3833 __set_bit(BTN_5, input_dev->keybit); 3834 __set_bit(BTN_6, input_dev->keybit); 3835 __set_bit(BTN_7, input_dev->keybit); 3836 } 3837 3838 /* -------------------------------------------------------------------------- */ 3839 /* HID++1.0 kbds which only report 0x10xx consumer usages through sub-id 0x03 */ 3840 /* -------------------------------------------------------------------------- */ 3841 3842 /* Find the consumer-page input report desc and change Maximums to 0x107f */ 3843 static u8 *hidpp10_consumer_keys_report_fixup(struct hidpp_device *hidpp, 3844 u8 *_rdesc, unsigned int *rsize) 3845 { 3846 /* Note 0 terminated so we can use strnstr to search for this. */ 3847 static const char consumer_rdesc_start[] = { 3848 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */ 3849 0x09, 0x01, /* USAGE (Consumer Control) */ 3850 0xA1, 0x01, /* COLLECTION (Application) */ 3851 0x85, 0x03, /* REPORT_ID = 3 */ 3852 0x75, 0x10, /* REPORT_SIZE (16) */ 3853 0x95, 0x02, /* REPORT_COUNT (2) */ 3854 0x15, 0x01, /* LOGICAL_MIN (1) */ 3855 0x26, 0x00 /* LOGICAL_MAX (... */ 3856 }; 3857 char *consumer_rdesc, *rdesc = (char *)_rdesc; 3858 unsigned int size; 3859 3860 consumer_rdesc = strnstr(rdesc, consumer_rdesc_start, *rsize); 3861 size = *rsize - (consumer_rdesc - rdesc); 3862 if (consumer_rdesc && size >= 25) { 3863 consumer_rdesc[15] = 0x7f; 3864 consumer_rdesc[16] = 0x10; 3865 consumer_rdesc[20] = 0x7f; 3866 consumer_rdesc[21] = 0x10; 3867 } 3868 return _rdesc; 3869 } 3870 3871 static int hidpp10_consumer_keys_connect(struct hidpp_device *hidpp) 3872 { 3873 return hidpp10_set_register(hidpp, HIDPP_REG_ENABLE_REPORTS, 0, 3874 HIDPP_ENABLE_CONSUMER_REPORT, 3875 HIDPP_ENABLE_CONSUMER_REPORT); 3876 } 3877 3878 static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp, 3879 u8 *data, int size) 3880 { 3881 u8 consumer_report[5]; 3882 3883 if (size < 7) 3884 return 0; 3885 3886 if (data[0] != REPORT_ID_HIDPP_SHORT || 3887 data[2] != HIDPP_SUB_ID_CONSUMER_VENDOR_KEYS) 3888 return 0; 3889 3890 /* 3891 * Build a normal consumer report (3) out of the data, this detour 3892 * is necessary to get some keyboards to report their 0x10xx usages. 3893 */ 3894 consumer_report[0] = 0x03; 3895 memcpy(&consumer_report[1], &data[3], 4); 3896 /* We are called from atomic context */ 3897 hid_report_raw_event(hidpp->hid_dev, HID_INPUT_REPORT, 3898 consumer_report, sizeof(consumer_report), 5, 1); 3899 3900 return 1; 3901 } 3902 3903 /* -------------------------------------------------------------------------- */ 3904 /* High-resolution scroll wheels */ 3905 /* -------------------------------------------------------------------------- */ 3906 3907 static int hi_res_scroll_enable(struct hidpp_device *hidpp) 3908 { 3909 int ret; 3910 u8 multiplier = 1; 3911 3912 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL) { 3913 ret = hidpp_hrw_set_wheel_mode(hidpp, false, true, false); 3914 if (ret == 0) 3915 ret = hidpp_hrw_get_wheel_capability(hidpp, &multiplier); 3916 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL) { 3917 ret = hidpp_hrs_set_highres_scrolling_mode(hidpp, true, 3918 &multiplier); 3919 } else /* if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL) */ { 3920 ret = hidpp10_enable_scrolling_acceleration(hidpp); 3921 multiplier = 8; 3922 } 3923 if (ret) { 3924 hid_dbg(hidpp->hid_dev, 3925 "Could not enable hi-res scrolling: %d\n", ret); 3926 return ret; 3927 } 3928 3929 if (multiplier == 0) { 3930 hid_dbg(hidpp->hid_dev, 3931 "Invalid multiplier 0 from device, setting it to 1\n"); 3932 multiplier = 1; 3933 } 3934 3935 hidpp->hires_wheel_multiplier = multiplier; 3936 hidpp->vertical_wheel_counter.wheel_multiplier = multiplier; 3937 hid_dbg(hidpp->hid_dev, "wheel multiplier = %d\n", multiplier); 3938 return 0; 3939 } 3940 3941 static int hidpp_initialize_hires_scroll(struct hidpp_device *hidpp) 3942 { 3943 int ret; 3944 unsigned long capabilities; 3945 3946 hidpp->hires_wheel_feature_index = 0xff; 3947 capabilities = hidpp->capabilities; 3948 3949 if (hidpp->protocol_major >= 2) { 3950 u8 feature_index; 3951 3952 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HIRES_WHEEL, 3953 &feature_index); 3954 if (!ret) { 3955 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL; 3956 hidpp->hires_wheel_feature_index = feature_index; 3957 hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scroll wheel\n"); 3958 return 0; 3959 } 3960 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_HI_RESOLUTION_SCROLLING, 3961 &feature_index); 3962 if (!ret) { 3963 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_HI_RES_SCROLL; 3964 hid_dbg(hidpp->hid_dev, "Detected HID++ 2.0 hi-res scrolling\n"); 3965 } 3966 } else { 3967 /* We cannot detect fast scrolling support on HID++ 1.0 devices */ 3968 if (hidpp->quirks & HIDPP_QUIRK_HI_RES_SCROLL_1P0) { 3969 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_FAST_SCROLL; 3970 hid_dbg(hidpp->hid_dev, "Detected HID++ 1.0 fast scroll\n"); 3971 } 3972 } 3973 3974 if (hidpp->capabilities == capabilities) 3975 hid_dbg(hidpp->hid_dev, "Did not detect HID++ hi-res scrolling hardware support\n"); 3976 return 0; 3977 } 3978 3979 static int hidpp20_hires_wheel_raw_event(struct hidpp_device *hidpp, 3980 u8 *data, int size) 3981 { 3982 if (hidpp->hires_wheel_feature_index == 0xff) 3983 return 0; 3984 3985 if (size < 5) 3986 return 0; 3987 3988 if (data[0] != REPORT_ID_HIDPP_LONG || 3989 data[2] != hidpp->hires_wheel_feature_index) 3990 return 0; 3991 3992 if ((data[3] & 0xf0) == CMD_HIRES_WHEEL_SET_WHEEL_MODE) { 3993 u8 mode = data[4]; 3994 bool hires = (mode & 0x02) != 0; 3995 int new_multiplier = (hires && hidpp->hires_wheel_multiplier > 0) 3996 ? hidpp->hires_wheel_multiplier : 1; 3997 hidpp->vertical_wheel_counter.wheel_multiplier = new_multiplier; 3998 return 1; 3999 } 4000 4001 return 0; 4002 } 4003 4004 /* -------------------------------------------------------------------------- */ 4005 /* Generic HID++ devices */ 4006 /* -------------------------------------------------------------------------- */ 4007 4008 static const u8 *hidpp_report_fixup(struct hid_device *hdev, u8 *rdesc, 4009 unsigned int *rsize) 4010 { 4011 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 4012 4013 if (!hidpp) 4014 return rdesc; 4015 4016 /* For 27 MHz keyboards the quirk gets set after hid_parse. */ 4017 if (hdev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE || 4018 (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS)) 4019 rdesc = hidpp10_consumer_keys_report_fixup(hidpp, rdesc, rsize); 4020 4021 return rdesc; 4022 } 4023 4024 static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi, 4025 struct hid_field *field, struct hid_usage *usage, 4026 unsigned long **bit, int *max) 4027 { 4028 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 4029 4030 if (!hidpp) 4031 return 0; 4032 4033 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) 4034 return wtp_input_mapping(hdev, hi, field, usage, bit, max); 4035 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 && 4036 field->application != HID_GD_MOUSE) 4037 return m560_input_mapping(hdev, hi, field, usage, bit, max); 4038 4039 if (hdev->product == DINOVO_MINI_PRODUCT_ID) 4040 return lg_dinovo_input_mapping(hdev, hi, field, usage, bit, max); 4041 4042 return 0; 4043 } 4044 4045 static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi, 4046 struct hid_field *field, struct hid_usage *usage, 4047 unsigned long **bit, int *max) 4048 { 4049 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 4050 4051 if (!hidpp) 4052 return 0; 4053 4054 /* Ensure that Logitech G920 is not given a default fuzz/flat value */ 4055 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) { 4056 if (usage->type == EV_ABS && (usage->code == ABS_X || 4057 usage->code == ABS_Y || usage->code == ABS_Z || 4058 usage->code == ABS_RZ)) { 4059 field->application = HID_GD_MULTIAXIS; 4060 } 4061 } 4062 4063 return 0; 4064 } 4065 4066 4067 static void hidpp_populate_input(struct hidpp_device *hidpp, 4068 struct input_dev *input) 4069 { 4070 hidpp->input = input; 4071 4072 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) 4073 wtp_populate_input(hidpp, input); 4074 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) 4075 m560_populate_input(hidpp, input); 4076 4077 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) 4078 hidpp10_wheel_populate_input(hidpp, input); 4079 4080 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) 4081 hidpp10_extra_mouse_buttons_populate_input(hidpp, input); 4082 4083 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_REPROG_CONTROLS_BTNS) 4084 hidpp20_reprog_controls_populate_input(hidpp, input); 4085 } 4086 4087 static int hidpp_input_configured(struct hid_device *hdev, struct hid_input *hidinput) 4088 { 4089 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 4090 struct input_dev *input = hidinput->input; 4091 int ret; 4092 4093 if (!hidpp) 4094 return 0; 4095 4096 if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) { 4097 struct hidpp_ff_private_data data; 4098 4099 if (!list_is_first(&hidinput->list, &hdev->inputs)) 4100 return 0; 4101 4102 ret = g920_get_config(hidpp, &data); 4103 if (!ret) 4104 ret = hidpp_ff_init(hidpp, &data); 4105 4106 if (ret) { 4107 hid_warn(hidpp->hid_dev, 4108 "Unable to initialize force feedback support, errno %d\n", 4109 ret); 4110 } 4111 } 4112 4113 hidpp_populate_input(hidpp, input); 4114 4115 return 0; 4116 } 4117 4118 static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data, 4119 int size) 4120 { 4121 struct hidpp_report *question, *answer; 4122 struct hidpp_report *report = (struct hidpp_report *)data; 4123 int ret; 4124 int last_online; 4125 4126 /* 4127 * If the mutex is locked then we have a pending answer from a 4128 * previously sent command. 4129 */ 4130 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) { 4131 question = hidpp->send_receive_buf; 4132 answer = hidpp->send_receive_buf; 4133 4134 if (!question) 4135 return 0; 4136 4137 /* 4138 * Check for a correct hidpp20 answer or the corresponding 4139 * error 4140 */ 4141 if (hidpp_match_answer(question, report) || 4142 hidpp_match_error(question, report)) { 4143 *answer = *report; 4144 hidpp->answer_available = true; 4145 wake_up(&hidpp->wait); 4146 /* 4147 * This was an answer to a command that this driver sent 4148 * We return 1 to hid-core to avoid forwarding the 4149 * command upstream as it has been treated by the driver 4150 */ 4151 4152 return 1; 4153 } 4154 } 4155 4156 if (unlikely(hidpp_report_is_connect_event(hidpp, report))) { 4157 if (schedule_work(&hidpp->work) == 0) 4158 dbg_hid("%s: connect event already queued\n", __func__); 4159 return 1; 4160 } 4161 4162 if (hidpp->hid_dev->group == HID_GROUP_LOGITECH_27MHZ_DEVICE && 4163 data[0] == REPORT_ID_HIDPP_SHORT && 4164 data[2] == HIDPP_SUB_ID_USER_IFACE_EVENT && 4165 (data[3] & HIDPP_USER_IFACE_EVENT_ENCRYPTION_KEY_LOST)) { 4166 dev_err_ratelimited(&hidpp->hid_dev->dev, 4167 "Error the keyboard's wireless encryption key has been lost, your keyboard will not work unless you re-configure encryption.\n"); 4168 dev_err_ratelimited(&hidpp->hid_dev->dev, 4169 "See: https://gitlab.freedesktop.org/jwrdegoede/logitech-27mhz-keyboard-encryption-setup/\n"); 4170 } 4171 4172 last_online = hidpp->battery.online; 4173 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) { 4174 ret = hidpp20_battery_event_1000(hidpp, data, size); 4175 if (ret != 0) 4176 return ret; 4177 ret = hidpp20_battery_event_1004(hidpp, data, size); 4178 if (ret != 0) 4179 return ret; 4180 ret = hidpp_solar_battery_event(hidpp, data, size); 4181 if (ret != 0) 4182 return ret; 4183 ret = hidpp20_battery_voltage_event(hidpp, data, size); 4184 if (ret != 0) 4185 return ret; 4186 ret = hidpp20_adc_measurement_event_1f20(hidpp, data, size); 4187 if (ret != 0) 4188 return ret; 4189 } 4190 4191 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) { 4192 ret = hidpp10_battery_event(hidpp, data, size); 4193 if (ret != 0) 4194 return ret; 4195 } 4196 4197 if (hidpp->quirks & HIDPP_QUIRK_RESET_HI_RES_SCROLL) { 4198 if (last_online == 0 && hidpp->battery.online == 1) 4199 schedule_work(&hidpp->reset_hi_res_work); 4200 } 4201 4202 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) { 4203 ret = hidpp10_wheel_raw_event(hidpp, data, size); 4204 if (ret != 0) 4205 return ret; 4206 } 4207 4208 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) { 4209 ret = hidpp10_extra_mouse_buttons_raw_event(hidpp, data, size); 4210 if (ret != 0) 4211 return ret; 4212 } 4213 4214 ret = hidpp20_reprog_controls_raw_event(hidpp, data, size); 4215 if (ret != 0) 4216 return ret; 4217 4218 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) { 4219 ret = hidpp10_consumer_keys_raw_event(hidpp, data, size); 4220 if (ret != 0) 4221 return ret; 4222 } 4223 4224 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_HI_RES_WHEEL) { 4225 ret = hidpp20_hires_wheel_raw_event(hidpp, data, size); 4226 if (ret != 0) 4227 return ret; 4228 } 4229 4230 return 0; 4231 } 4232 4233 static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report, 4234 u8 *data, int size) 4235 { 4236 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 4237 int ret = 0; 4238 4239 if (!hidpp) 4240 return 0; 4241 4242 /* Generic HID++ processing. */ 4243 switch (data[0]) { 4244 case REPORT_ID_HIDPP_VERY_LONG: 4245 if (size != hidpp->very_long_report_length) { 4246 hid_err(hdev, "received hid++ report of bad size (%d)", 4247 size); 4248 return 1; 4249 } 4250 ret = hidpp_raw_hidpp_event(hidpp, data, size); 4251 break; 4252 case REPORT_ID_HIDPP_LONG: 4253 if (size != HIDPP_REPORT_LONG_LENGTH) { 4254 hid_err(hdev, "received hid++ report of bad size (%d)", 4255 size); 4256 return 1; 4257 } 4258 ret = hidpp_raw_hidpp_event(hidpp, data, size); 4259 break; 4260 case REPORT_ID_HIDPP_SHORT: 4261 if (size != HIDPP_REPORT_SHORT_LENGTH) { 4262 hid_err(hdev, "received hid++ report of bad size (%d)", 4263 size); 4264 return 1; 4265 } 4266 ret = hidpp_raw_hidpp_event(hidpp, data, size); 4267 break; 4268 } 4269 4270 /* If no report is available for further processing, skip calling 4271 * raw_event of subclasses. */ 4272 if (ret != 0) 4273 return ret; 4274 4275 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) 4276 return wtp_raw_event(hdev, data, size); 4277 else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) 4278 return m560_raw_event(hdev, data, size); 4279 4280 return 0; 4281 } 4282 4283 static int hidpp_event(struct hid_device *hdev, struct hid_field *field, 4284 struct hid_usage *usage, __s32 value) 4285 { 4286 /* This function will only be called for scroll events, due to the 4287 * restriction imposed in hidpp_usages. 4288 */ 4289 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 4290 struct hidpp_scroll_counter *counter; 4291 4292 if (!hidpp) 4293 return 0; 4294 4295 counter = &hidpp->vertical_wheel_counter; 4296 /* A scroll event may occur before the multiplier has been retrieved or 4297 * the input device set, or high-res scroll enabling may fail. In such 4298 * cases we must return early (falling back to default behaviour) to 4299 * avoid a crash in hidpp_scroll_counter_handle_scroll. 4300 */ 4301 if (!(hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL) 4302 || value == 0 || hidpp->input == NULL 4303 || counter->wheel_multiplier == 0) 4304 return 0; 4305 4306 hidpp_scroll_counter_handle_scroll(hidpp->input, counter, value); 4307 return 1; 4308 } 4309 4310 static int hidpp_initialize_battery(struct hidpp_device *hidpp) 4311 { 4312 static atomic_t battery_no = ATOMIC_INIT(0); 4313 struct power_supply_config cfg = { .drv_data = hidpp }; 4314 struct power_supply_desc *desc = &hidpp->battery.desc; 4315 enum power_supply_property *battery_props; 4316 struct hidpp_battery *battery; 4317 unsigned int num_battery_props; 4318 unsigned long n; 4319 int ret; 4320 4321 if (hidpp->battery.ps) 4322 return 0; 4323 4324 hidpp->battery.feature_index = 0xff; 4325 hidpp->battery.solar_feature_index = 0xff; 4326 hidpp->battery.voltage_feature_index = 0xff; 4327 hidpp->battery.adc_measurement_feature_index = 0xff; 4328 4329 if (hidpp->protocol_major >= 2) { 4330 if (hidpp->quirks & HIDPP_QUIRK_CLASS_K750) 4331 ret = hidpp_solar_request_battery_event(hidpp); 4332 else { 4333 /* we only support one battery feature right now, so let's 4334 first check the ones that support battery level first 4335 and leave voltage for last */ 4336 ret = hidpp20_query_battery_info_1000(hidpp); 4337 if (ret) 4338 ret = hidpp20_query_battery_info_1004(hidpp); 4339 if (ret) 4340 ret = hidpp20_query_battery_voltage_info(hidpp); 4341 if (ret) 4342 ret = hidpp20_query_adc_measurement_info_1f20(hidpp); 4343 } 4344 4345 if (ret) 4346 return ret; 4347 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY; 4348 } else { 4349 ret = hidpp10_query_battery_status(hidpp); 4350 if (ret) { 4351 ret = hidpp10_query_battery_mileage(hidpp); 4352 if (ret) 4353 return -ENOENT; 4354 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_MILEAGE; 4355 } else { 4356 hidpp->capabilities |= HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS; 4357 } 4358 hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP10_BATTERY; 4359 } 4360 4361 battery_props = devm_kmemdup(&hidpp->hid_dev->dev, 4362 hidpp_battery_props, 4363 sizeof(hidpp_battery_props), 4364 GFP_KERNEL); 4365 if (!battery_props) 4366 return -ENOMEM; 4367 4368 num_battery_props = ARRAY_SIZE(hidpp_battery_props) - 3; 4369 4370 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE || 4371 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_PERCENTAGE || 4372 hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE || 4373 hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT) 4374 battery_props[num_battery_props++] = 4375 POWER_SUPPLY_PROP_CAPACITY; 4376 4377 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_LEVEL_STATUS) 4378 battery_props[num_battery_props++] = 4379 POWER_SUPPLY_PROP_CAPACITY_LEVEL; 4380 4381 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE || 4382 hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT) 4383 battery_props[num_battery_props++] = 4384 POWER_SUPPLY_PROP_VOLTAGE_NOW; 4385 4386 battery = &hidpp->battery; 4387 4388 n = atomic_inc_return(&battery_no) - 1; 4389 desc->properties = battery_props; 4390 desc->num_properties = num_battery_props; 4391 desc->get_property = hidpp_battery_get_property; 4392 sprintf(battery->name, "hidpp_battery_%ld", n); 4393 desc->name = battery->name; 4394 desc->type = POWER_SUPPLY_TYPE_BATTERY; 4395 desc->use_for_apm = 0; 4396 4397 battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev, 4398 &battery->desc, 4399 &cfg); 4400 if (IS_ERR(battery->ps)) 4401 return PTR_ERR(battery->ps); 4402 4403 power_supply_powers(battery->ps, &hidpp->hid_dev->dev); 4404 4405 return ret; 4406 } 4407 4408 static bool hidpp_is_bolt_child(struct hid_device *hdev) 4409 { 4410 struct device *parent = hdev->dev.parent; 4411 struct hid_device *receiver_hdev; 4412 4413 if (!parent) 4414 return false; 4415 4416 receiver_hdev = to_hid_device(parent); 4417 return receiver_hdev->vendor == USB_VENDOR_ID_LOGITECH && 4418 receiver_hdev->product == USB_DEVICE_ID_LOGITECH_BOLT_RECEIVER; 4419 } 4420 4421 static int hidpp_bolt_init(struct hidpp_device *hidpp) 4422 { 4423 struct hid_device *hdev = hidpp->hid_dev; 4424 char *name; 4425 int ret; 4426 4427 ret = hidpp_serial_init(hidpp); 4428 if (ret) 4429 return ret; 4430 4431 name = hidpp_get_device_name(hidpp); 4432 if (!name) 4433 return -EIO; 4434 4435 snprintf(hdev->name, sizeof(hdev->name), "%s", name); 4436 dbg_hid("HID++ Bolt: Got name: %s\n", name); 4437 4438 kfree(name); 4439 return 0; 4440 } 4441 4442 static int hidpp_receiver_init(struct hidpp_device *hidpp) 4443 { 4444 if (hidpp_is_bolt_child(hidpp->hid_dev)) 4445 return hidpp_bolt_init(hidpp); 4446 4447 return hidpp_unifying_init(hidpp); 4448 } 4449 4450 /* Get name + serial for USB and Bluetooth HID++ devices */ 4451 static void hidpp_non_receiver_init(struct hidpp_device *hidpp) 4452 { 4453 struct hid_device *hdev = hidpp->hid_dev; 4454 char *name; 4455 4456 /* Bluetooth devices already have their serialnr set */ 4457 if (hid_is_usb(hdev)) 4458 hidpp_serial_init(hidpp); 4459 4460 name = hidpp_get_device_name(hidpp); 4461 if (name) { 4462 dbg_hid("HID++: Got name: %s\n", name); 4463 snprintf(hdev->name, sizeof(hdev->name), "%s", name); 4464 kfree(name); 4465 } 4466 } 4467 4468 static int hidpp_input_open(struct input_dev *dev) 4469 { 4470 struct hid_device *hid = input_get_drvdata(dev); 4471 4472 return hid_hw_open(hid); 4473 } 4474 4475 static void hidpp_input_close(struct input_dev *dev) 4476 { 4477 struct hid_device *hid = input_get_drvdata(dev); 4478 4479 hid_hw_close(hid); 4480 } 4481 4482 static struct input_dev *hidpp_allocate_input(struct hid_device *hdev) 4483 { 4484 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev); 4485 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 4486 4487 if (!input_dev) 4488 return NULL; 4489 4490 input_set_drvdata(input_dev, hdev); 4491 input_dev->open = hidpp_input_open; 4492 input_dev->close = hidpp_input_close; 4493 4494 input_dev->name = hidpp->name; 4495 input_dev->phys = hdev->phys; 4496 input_dev->uniq = hdev->uniq; 4497 input_dev->id.bustype = hdev->bus; 4498 input_dev->id.vendor = hdev->vendor; 4499 input_dev->id.product = hdev->product; 4500 input_dev->id.version = hdev->version; 4501 input_dev->dev.parent = &hdev->dev; 4502 4503 return input_dev; 4504 } 4505 4506 static void hidpp_connect_event(struct work_struct *work) 4507 { 4508 struct hidpp_device *hidpp = container_of(work, struct hidpp_device, work); 4509 struct hid_device *hdev = hidpp->hid_dev; 4510 struct input_dev *input; 4511 char *name, *devm_name; 4512 int ret; 4513 4514 /* Get device version to check if it is connected */ 4515 ret = hidpp_root_get_protocol_version(hidpp); 4516 if (ret) { 4517 hid_dbg(hidpp->hid_dev, "Disconnected\n"); 4518 if (hidpp->battery.ps) { 4519 hidpp->battery.online = false; 4520 hidpp->battery.status = POWER_SUPPLY_STATUS_UNKNOWN; 4521 hidpp->battery.level = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; 4522 power_supply_changed(hidpp->battery.ps); 4523 } 4524 return; 4525 } 4526 4527 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) { 4528 ret = wtp_connect(hdev); 4529 if (ret) 4530 return; 4531 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) { 4532 ret = m560_send_config_command(hdev); 4533 if (ret) 4534 return; 4535 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) { 4536 ret = k400_connect(hdev); 4537 if (ret) 4538 return; 4539 } 4540 4541 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_WHEELS) { 4542 ret = hidpp10_wheel_connect(hidpp); 4543 if (ret) 4544 return; 4545 } 4546 4547 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS) { 4548 ret = hidpp10_extra_mouse_buttons_connect(hidpp); 4549 if (ret) 4550 return; 4551 } 4552 4553 hidpp20_reprog_controls_connect(hidpp); 4554 4555 if (hidpp->quirks & HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS) { 4556 ret = hidpp10_consumer_keys_connect(hidpp); 4557 if (ret) 4558 return; 4559 } 4560 4561 if (hidpp->protocol_major >= 2) { 4562 u8 feature_index; 4563 4564 if (!hidpp_get_wireless_feature_index(hidpp, &feature_index)) 4565 hidpp->wireless_feature_index = feature_index; 4566 } 4567 4568 if (hidpp->name == hdev->name && hidpp->protocol_major >= 2) { 4569 name = hidpp_get_device_name(hidpp); 4570 if (name) { 4571 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, 4572 "%s", name); 4573 kfree(name); 4574 if (!devm_name) 4575 return; 4576 4577 hidpp->name = devm_name; 4578 } 4579 } 4580 4581 hidpp_initialize_battery(hidpp); 4582 if (!hid_is_usb(hidpp->hid_dev)) 4583 hidpp_initialize_hires_scroll(hidpp); 4584 4585 /* forward current battery state */ 4586 if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP10_BATTERY) { 4587 hidpp10_enable_battery_reporting(hidpp); 4588 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_MILEAGE) 4589 hidpp10_query_battery_mileage(hidpp); 4590 else 4591 hidpp10_query_battery_status(hidpp); 4592 } else if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) { 4593 if (hidpp->capabilities & HIDPP_CAPABILITY_BATTERY_VOLTAGE) 4594 hidpp20_query_battery_voltage_info(hidpp); 4595 else if (hidpp->capabilities & HIDPP_CAPABILITY_UNIFIED_BATTERY) 4596 hidpp20_query_battery_info_1004(hidpp); 4597 else if (hidpp->capabilities & HIDPP_CAPABILITY_ADC_MEASUREMENT) 4598 hidpp20_query_adc_measurement_info_1f20(hidpp); 4599 else 4600 hidpp20_query_battery_info_1000(hidpp); 4601 } 4602 if (hidpp->battery.ps) 4603 power_supply_changed(hidpp->battery.ps); 4604 4605 if (hidpp->capabilities & HIDPP_CAPABILITY_HI_RES_SCROLL) 4606 hi_res_scroll_enable(hidpp); 4607 4608 if (!(hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) || hidpp->delayed_input) 4609 /* if the input nodes are already created, we can stop now */ 4610 return; 4611 4612 input = hidpp_allocate_input(hdev); 4613 if (!input) { 4614 hid_err(hdev, "cannot allocate new input device: %d\n", ret); 4615 return; 4616 } 4617 4618 hidpp_populate_input(hidpp, input); 4619 4620 ret = input_register_device(input); 4621 if (ret) { 4622 hidpp->input = NULL; 4623 input_free_device(input); 4624 return; 4625 } 4626 4627 hidpp->delayed_input = input; 4628 } 4629 4630 static void hidpp_reset_hi_res_handler(struct work_struct *work) 4631 { 4632 struct hidpp_device *hidpp = container_of(work, struct hidpp_device, reset_hi_res_work); 4633 4634 hi_res_scroll_enable(hidpp); 4635 } 4636 4637 static DEVICE_ATTR(builtin_power_supply, 0000, NULL, NULL); 4638 4639 static struct attribute *sysfs_attrs[] = { 4640 &dev_attr_builtin_power_supply.attr, 4641 NULL 4642 }; 4643 4644 static const struct attribute_group ps_attribute_group = { 4645 .attrs = sysfs_attrs 4646 }; 4647 4648 static int hidpp_get_report_length(struct hid_device *hdev, int id) 4649 { 4650 struct hid_report_enum *re; 4651 struct hid_report *report; 4652 4653 re = &(hdev->report_enum[HID_OUTPUT_REPORT]); 4654 report = re->report_id_hash[id]; 4655 if (!report || !report->maxfield) 4656 return 0; 4657 4658 return report->field[0]->report_count + 1; 4659 } 4660 4661 static u8 hidpp_validate_device(struct hid_device *hdev) 4662 { 4663 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 4664 int id, report_length; 4665 u8 supported_reports = 0; 4666 4667 id = REPORT_ID_HIDPP_SHORT; 4668 report_length = hidpp_get_report_length(hdev, id); 4669 if (report_length) { 4670 if (report_length < HIDPP_REPORT_SHORT_LENGTH) 4671 goto bad_device; 4672 4673 supported_reports |= HIDPP_REPORT_SHORT_SUPPORTED; 4674 } 4675 4676 id = REPORT_ID_HIDPP_LONG; 4677 report_length = hidpp_get_report_length(hdev, id); 4678 if (report_length) { 4679 if (report_length < HIDPP_REPORT_LONG_LENGTH) 4680 goto bad_device; 4681 4682 supported_reports |= HIDPP_REPORT_LONG_SUPPORTED; 4683 } 4684 4685 id = REPORT_ID_HIDPP_VERY_LONG; 4686 report_length = hidpp_get_report_length(hdev, id); 4687 if (report_length) { 4688 if (report_length < HIDPP_REPORT_LONG_LENGTH || 4689 report_length > HIDPP_REPORT_VERY_LONG_MAX_LENGTH) 4690 goto bad_device; 4691 4692 supported_reports |= HIDPP_REPORT_VERY_LONG_SUPPORTED; 4693 hidpp->very_long_report_length = report_length; 4694 } 4695 4696 return supported_reports; 4697 4698 bad_device: 4699 hid_warn(hdev, "not enough values in hidpp report %d\n", id); 4700 return false; 4701 } 4702 4703 static bool hidpp_application_equals(struct hid_device *hdev, 4704 unsigned int application) 4705 { 4706 struct list_head *report_list; 4707 struct hid_report *report; 4708 4709 report_list = &hdev->report_enum[HID_INPUT_REPORT].report_list; 4710 report = list_first_entry_or_null(report_list, struct hid_report, list); 4711 return report && report->application == application; 4712 } 4713 4714 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id) 4715 { 4716 struct hidpp_device *hidpp; 4717 int ret; 4718 unsigned int connect_mask = HID_CONNECT_DEFAULT; 4719 4720 /* report_fixup needs drvdata to be set before we call hid_parse */ 4721 hidpp = devm_kzalloc(&hdev->dev, sizeof(*hidpp), GFP_KERNEL); 4722 if (!hidpp) 4723 return -ENOMEM; 4724 4725 hidpp->hid_dev = hdev; 4726 hidpp->name = hdev->name; 4727 hidpp->quirks = id->driver_data; 4728 hidpp->reprog_controls_feature_index = 0xff; 4729 hidpp->reprog_controls = hidpp20_reprog_controls_get_mappings(hidpp); 4730 hid_set_drvdata(hdev, hidpp); 4731 4732 ret = hid_parse(hdev); 4733 if (ret) { 4734 hid_err(hdev, "%s:parse failed\n", __func__); 4735 return ret; 4736 } 4737 4738 /* 4739 * Make sure the device is HID++ capable, otherwise treat as generic HID 4740 */ 4741 hidpp->supported_reports = hidpp_validate_device(hdev); 4742 4743 if (!hidpp->supported_reports) { 4744 hid_set_drvdata(hdev, NULL); 4745 devm_kfree(&hdev->dev, hidpp); 4746 return hid_hw_start(hdev, HID_CONNECT_DEFAULT); 4747 } 4748 4749 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE && 4750 hidpp_application_equals(hdev, HID_GD_MOUSE)) 4751 hidpp->quirks |= HIDPP_QUIRK_HIDPP_WHEELS | 4752 HIDPP_QUIRK_HIDPP_EXTRA_MOUSE_BTNS; 4753 4754 if (id->group == HID_GROUP_LOGITECH_27MHZ_DEVICE && 4755 hidpp_application_equals(hdev, HID_GD_KEYBOARD)) 4756 hidpp->quirks |= HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS; 4757 4758 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) { 4759 ret = wtp_allocate(hdev, id); 4760 if (ret) 4761 return ret; 4762 } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) { 4763 ret = k400_allocate(hdev); 4764 if (ret) 4765 return ret; 4766 } 4767 4768 INIT_WORK(&hidpp->work, hidpp_connect_event); 4769 INIT_WORK(&hidpp->reset_hi_res_work, hidpp_reset_hi_res_handler); 4770 mutex_init(&hidpp->send_mutex); 4771 init_waitqueue_head(&hidpp->wait); 4772 4773 /* indicates we are handling the battery properties in the kernel */ 4774 ret = sysfs_create_group(&hdev->dev.kobj, &ps_attribute_group); 4775 if (ret) 4776 hid_warn(hdev, "Cannot allocate sysfs group for %s\n", 4777 hdev->name); 4778 4779 /* 4780 * First call hid_hw_start(hdev, 0) to allow IO without connecting any 4781 * hid subdrivers (hid-input, hidraw). This allows retrieving the dev's 4782 * name and serial number and store these in hdev->name and hdev->uniq, 4783 * before the hid-input and hidraw drivers expose these to userspace. 4784 */ 4785 ret = hid_hw_start(hdev, 0); 4786 if (ret) { 4787 hid_err(hdev, "hw start failed\n"); 4788 goto hid_hw_start_fail; 4789 } 4790 4791 ret = hid_hw_open(hdev); 4792 if (ret < 0) { 4793 dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n", 4794 __func__, ret); 4795 goto hid_hw_open_fail; 4796 } 4797 4798 /* Allow incoming packets */ 4799 hid_device_io_start(hdev); 4800 4801 /* Get name + serial, store in hdev->name + hdev->uniq */ 4802 if (id->group == HID_GROUP_LOGITECH_DJ_DEVICE) 4803 hidpp_receiver_init(hidpp); 4804 else 4805 hidpp_non_receiver_init(hidpp); 4806 4807 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) 4808 connect_mask &= ~HID_CONNECT_HIDINPUT; 4809 4810 /* Now export the actual inputs and hidraw nodes to the world */ 4811 hid_device_io_stop(hdev); 4812 ret = hid_connect(hdev, connect_mask); 4813 if (ret) { 4814 hid_err(hdev, "%s:hid_connect returned error %d\n", __func__, ret); 4815 goto hid_hw_init_fail; 4816 } 4817 4818 /* Check for connected devices now that incoming packets will not be disabled again */ 4819 hid_device_io_start(hdev); 4820 schedule_work(&hidpp->work); 4821 flush_work(&hidpp->work); 4822 4823 /* 4824 * This relies on logi_dj_ll_close() being a no-op so that DJ connection 4825 * events will still be received. 4826 */ 4827 hid_hw_close(hdev); 4828 return ret; 4829 4830 hid_hw_init_fail: 4831 hid_hw_close(hdev); 4832 hid_hw_open_fail: 4833 hid_hw_stop(hdev); 4834 hid_hw_start_fail: 4835 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group); 4836 cancel_work_sync(&hidpp->work); 4837 mutex_destroy(&hidpp->send_mutex); 4838 return ret; 4839 } 4840 4841 static void hidpp_remove(struct hid_device *hdev) 4842 { 4843 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 4844 4845 if (!hidpp) 4846 return hid_hw_stop(hdev); 4847 4848 sysfs_remove_group(&hdev->dev.kobj, &ps_attribute_group); 4849 4850 hid_hw_stop(hdev); 4851 cancel_work_sync(&hidpp->work); 4852 cancel_work_sync(&hidpp->reset_hi_res_work); 4853 mutex_destroy(&hidpp->send_mutex); 4854 } 4855 4856 #define LDJ_DEVICE(product) \ 4857 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, \ 4858 USB_VENDOR_ID_LOGITECH, (product)) 4859 4860 #define L27MHZ_DEVICE(product) \ 4861 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_27MHZ_DEVICE, \ 4862 USB_VENDOR_ID_LOGITECH, (product)) 4863 4864 static const struct hid_device_id hidpp_devices[] = { 4865 { /* wireless touchpad */ 4866 LDJ_DEVICE(0x4011), 4867 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT | 4868 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS }, 4869 { /* wireless touchpad T650 */ 4870 LDJ_DEVICE(0x4101), 4871 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT }, 4872 { /* wireless touchpad T651 */ 4873 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 4874 USB_DEVICE_ID_LOGITECH_T651), 4875 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT }, 4876 { /* Mouse Logitech Anywhere MX */ 4877 LDJ_DEVICE(0x1017), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 }, 4878 { /* Mouse logitech M560 */ 4879 LDJ_DEVICE(0x402d), 4880 .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 }, 4881 { /* Mouse Logitech M705 (firmware RQM17) */ 4882 LDJ_DEVICE(0x101b), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 }, 4883 { /* Mouse Logitech Performance MX */ 4884 LDJ_DEVICE(0x101a), .driver_data = HIDPP_QUIRK_HI_RES_SCROLL_1P0 }, 4885 { /* Keyboard logitech K400 */ 4886 LDJ_DEVICE(0x4024), 4887 .driver_data = HIDPP_QUIRK_CLASS_K400 }, 4888 { /* Solar Keyboard Logitech K750 */ 4889 LDJ_DEVICE(0x4002), 4890 .driver_data = HIDPP_QUIRK_CLASS_K750 }, 4891 { /* Keyboard MX5000 (Bluetooth-receiver in HID proxy mode) */ 4892 LDJ_DEVICE(0xb305), 4893 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS }, 4894 { /* Dinovo Edge (Bluetooth-receiver in HID proxy mode) */ 4895 LDJ_DEVICE(0xb309), 4896 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS }, 4897 { /* Keyboard MX5500 (Bluetooth-receiver in HID proxy mode) */ 4898 LDJ_DEVICE(0xb30b), 4899 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS }, 4900 { /* Logitech G502 Lightspeed Wireless Gaming Mouse */ 4901 LDJ_DEVICE(0x407f), 4902 .driver_data = HIDPP_QUIRK_RESET_HI_RES_SCROLL }, 4903 4904 { LDJ_DEVICE(HID_ANY_ID) }, 4905 4906 { /* Keyboard LX501 (Y-RR53) */ 4907 L27MHZ_DEVICE(0x0049), 4908 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL }, 4909 { /* Keyboard MX3000 (Y-RAM74) */ 4910 L27MHZ_DEVICE(0x0057), 4911 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL }, 4912 { /* Keyboard MX3200 (Y-RAV80) */ 4913 L27MHZ_DEVICE(0x005c), 4914 .driver_data = HIDPP_QUIRK_KBD_ZOOM_WHEEL }, 4915 { /* S510 Media Remote */ 4916 L27MHZ_DEVICE(0x00fe), 4917 .driver_data = HIDPP_QUIRK_KBD_SCROLL_WHEEL }, 4918 4919 { L27MHZ_DEVICE(HID_ANY_ID) }, 4920 4921 { /* Logitech G403 Wireless Gaming Mouse over USB */ 4922 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC082) }, 4923 { /* Logitech G502 Lightspeed Wireless Gaming Mouse over USB */ 4924 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC08D) }, 4925 { /* Logitech G502 X Plus Wireless Gaming Mouse over USB */ 4926 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC095) }, 4927 { /* Logitech G703 Gaming Mouse over USB */ 4928 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC087) }, 4929 { /* Logitech G703 Hero Gaming Mouse over USB */ 4930 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC090) }, 4931 { /* Logitech G900 Gaming Mouse over USB */ 4932 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC081) }, 4933 { /* Logitech G903 Gaming Mouse over USB */ 4934 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC086) }, 4935 { /* Logitech G Pro Gaming Mouse over USB */ 4936 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC088) }, 4937 { /* MX Vertical over USB */ 4938 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC08A) }, 4939 { /* Logitech G903 Hero Gaming Mouse over USB */ 4940 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC091) }, 4941 { /* Logitech G915 TKL Keyboard over USB */ 4942 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC343) }, 4943 { /* Logitech G920 Wheel over USB */ 4944 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL), 4945 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS}, 4946 { /* Logitech G923 Wheel (Xbox version) over USB */ 4947 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G923_XBOX_WHEEL), 4948 .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS }, 4949 { /* Logitech G Pro X Superlight Gaming Mouse over USB */ 4950 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC094) }, 4951 { /* Logitech G Pro X Superlight 2 Gaming Mouse over USB */ 4952 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xC09b) }, 4953 { /* Logitech G PRO 2 LIGHTSPEED Wireless Mouse over USB */ 4954 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0xc09a) }, 4955 4956 { /* G935 Gaming Headset */ 4957 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, 0x0a87), 4958 .driver_data = HIDPP_QUIRK_WIRELESS_STATUS }, 4959 4960 { /* MX5000 keyboard over Bluetooth */ 4961 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb305), 4962 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS }, 4963 { /* Dinovo Edge keyboard over Bluetooth */ 4964 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb309), 4965 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS }, 4966 { /* MX5500 keyboard over Bluetooth */ 4967 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb30b), 4968 .driver_data = HIDPP_QUIRK_HIDPP_CONSUMER_VENDOR_KEYS }, 4969 { /* Logitech G915 TKL keyboard over Bluetooth */ 4970 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb35f) }, 4971 { /* M-RCQ142 V470 Cordless Laser Mouse over Bluetooth */ 4972 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb008) }, 4973 { /* MX Master mouse over Bluetooth */ 4974 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb012) }, 4975 { /* M720 Triathlon mouse over Bluetooth */ 4976 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb015) }, 4977 { /* MX Master 2S mouse over Bluetooth */ 4978 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb019) }, 4979 { /* MX Ergo trackball over Bluetooth */ 4980 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01d) }, 4981 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb01e) }, 4982 { /* MX Vertical mouse over Bluetooth */ 4983 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb020) }, 4984 { /* Signature M650 over Bluetooth */ 4985 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 4986 HIDPP_PRODUCT_SIGNATURE_M650), 4987 .driver_data = HIDPP_QUIRK_HIDPP_REPROG_CONTROLS_BTNS }, 4988 { /* MX Master 3 mouse over Bluetooth */ 4989 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb023) }, 4990 { /* MX Anywhere 3 mouse over Bluetooth */ 4991 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb025) }, 4992 { /* MX Master 3S mouse over Bluetooth */ 4993 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb034) }, 4994 { /* MX Anywhere 3S mouse over Bluetooth */ 4995 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb037) }, 4996 { /* MX Anywhere 3SB mouse over Bluetooth */ 4997 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb038) }, 4998 { /* Slim Solar+ K980 Keyboard over Bluetooth */ 4999 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb391) }, 5000 { /* MX Master 4 mouse over Bluetooth */ 5001 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb042) }, 5002 { /* Logitech Signature K650 over Bluetooth */ 5003 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb36f) }, 5004 { /* Logitech Signature K650 B2B over Bluetooth */ 5005 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb370) }, 5006 { /* Logitech Pebble Keys 2 K380S over Bluetooth */ 5007 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb377) }, 5008 { /* Logitech Casa Pop-Up Desk over Bluetooth */ 5009 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb371) }, 5010 { /* Logitech Casa Pop-Up Desk B2B over Bluetooth */ 5011 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb374) }, 5012 { /* Logitech Wave Keys over Bluetooth */ 5013 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb383) }, 5014 { /* Logitech Wave Keys B2B over Bluetooth */ 5015 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb384) }, 5016 { /* Logitech Signature Slim K950 over Bluetooth */ 5017 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb386) }, 5018 { /* Logitech Signature Slim K950 B2B over Bluetooth */ 5019 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb388) }, 5020 { /* Logitech MX Keys S over Bluetooth */ 5021 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb378) }, 5022 { /* Logitech MX Keys S B2B over Bluetooth */ 5023 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb380) }, 5024 { /* Logitech Keys-To-Go 2 over Bluetooth */ 5025 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb38c) }, 5026 { /* Logitech Pop Icon Keys over Bluetooth */ 5027 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb38f) }, 5028 { /* Logitech MX Keys Mini over Bluetooth */ 5029 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb369) }, 5030 { /* Logitech MX Keys Mini B2B over Bluetooth */ 5031 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb36e) }, 5032 { /* Logitech Signature Slim Solar+ K980 B2B over Bluetooth */ 5033 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb394) }, 5034 { /* Logitech Bluetooth Keyboard K250/K251 over Bluetooth */ 5035 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb397) }, 5036 { /* Logitech Signature Comfort K880 over Bluetooth */ 5037 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb39c) }, 5038 { /* Logitech Signature Comfort K880 B2B over Bluetooth */ 5039 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 0xb39d) }, 5040 {} 5041 }; 5042 5043 MODULE_DEVICE_TABLE(hid, hidpp_devices); 5044 5045 static const struct hid_usage_id hidpp_usages[] = { 5046 { HID_GD_WHEEL, EV_REL, REL_WHEEL_HI_RES }, 5047 { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1} 5048 }; 5049 5050 static struct hid_driver hidpp_driver = { 5051 .name = "logitech-hidpp-device", 5052 .id_table = hidpp_devices, 5053 .report_fixup = hidpp_report_fixup, 5054 .probe = hidpp_probe, 5055 .remove = hidpp_remove, 5056 .raw_event = hidpp_raw_event, 5057 .usage_table = hidpp_usages, 5058 .event = hidpp_event, 5059 .input_configured = hidpp_input_configured, 5060 .input_mapping = hidpp_input_mapping, 5061 .input_mapped = hidpp_input_mapped, 5062 }; 5063 5064 module_hid_driver(hidpp_driver); 5065