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