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