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