1/* 2 * HIDPP protocol for Logitech Unifying receivers 3 * 4 * Copyright (c) 2011 Logitech (c) 5 * Copyright (c) 2012-2013 Google (c) 6 * Copyright (c) 2013-2014 Red Hat Inc. 7 */ 8 9/* 10 * This program is free software; you can redistribute it and/or modify it 11 * under the terms of the GNU General Public License as published by the Free 12 * Software Foundation; version 2 of the License. 13 */ 14 15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 16 17#include <linux/device.h> 18#include <linux/hid.h> 19#include <linux/module.h> 20#include <linux/slab.h> 21#include <linux/sched.h> 22#include <linux/kfifo.h> 23#include <linux/input/mt.h> 24#include <asm/unaligned.h> 25#include "hid-ids.h" 26 27MODULE_LICENSE("GPL"); 28MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); 29MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>"); 30 31static bool disable_raw_mode; 32module_param(disable_raw_mode, bool, 0644); 33MODULE_PARM_DESC(disable_raw_mode, 34 "Disable Raw mode reporting for touchpads and keep firmware gestures."); 35 36#define REPORT_ID_HIDPP_SHORT 0x10 37#define REPORT_ID_HIDPP_LONG 0x11 38 39#define HIDPP_REPORT_SHORT_LENGTH 7 40#define HIDPP_REPORT_LONG_LENGTH 20 41 42#define HIDPP_QUIRK_CLASS_WTP BIT(0) 43 44/* bits 1..20 are reserved for classes */ 45#define HIDPP_QUIRK_DELAYED_INIT BIT(21) 46#define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22) 47 48/* 49 * There are two hidpp protocols in use, the first version hidpp10 is known 50 * as register access protocol or RAP, the second version hidpp20 is known as 51 * feature access protocol or FAP 52 * 53 * Most older devices (including the Unifying usb receiver) use the RAP protocol 54 * where as most newer devices use the FAP protocol. Both protocols are 55 * compatible with the underlying transport, which could be usb, Unifiying, or 56 * bluetooth. The message lengths are defined by the hid vendor specific report 57 * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and 58 * the HIDPP_LONG report type (total message length 20 bytes) 59 * 60 * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG 61 * messages. The Unifying receiver itself responds to RAP messages (device index 62 * is 0xFF for the receiver), and all messages (short or long) with a device 63 * index between 1 and 6 are passed untouched to the corresponding paired 64 * Unifying device. 65 * 66 * The paired device can be RAP or FAP, it will receive the message untouched 67 * from the Unifiying receiver. 68 */ 69 70struct fap { 71 u8 feature_index; 72 u8 funcindex_clientid; 73 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U]; 74}; 75 76struct rap { 77 u8 sub_id; 78 u8 reg_address; 79 u8 params[HIDPP_REPORT_LONG_LENGTH - 4U]; 80}; 81 82struct hidpp_report { 83 u8 report_id; 84 u8 device_index; 85 union { 86 struct fap fap; 87 struct rap rap; 88 u8 rawbytes[sizeof(struct fap)]; 89 }; 90} __packed; 91 92struct hidpp_device { 93 struct hid_device *hid_dev; 94 struct mutex send_mutex; 95 void *send_receive_buf; 96 char *name; /* will never be NULL and should not be freed */ 97 wait_queue_head_t wait; 98 bool answer_available; 99 u8 protocol_major; 100 u8 protocol_minor; 101 102 void *private_data; 103 104 struct work_struct work; 105 struct kfifo delayed_work_fifo; 106 atomic_t connected; 107 struct input_dev *delayed_input; 108 109 unsigned long quirks; 110}; 111 112 113/* HID++ 1.0 error codes */ 114#define HIDPP_ERROR 0x8f 115#define HIDPP_ERROR_SUCCESS 0x00 116#define HIDPP_ERROR_INVALID_SUBID 0x01 117#define HIDPP_ERROR_INVALID_ADRESS 0x02 118#define HIDPP_ERROR_INVALID_VALUE 0x03 119#define HIDPP_ERROR_CONNECT_FAIL 0x04 120#define HIDPP_ERROR_TOO_MANY_DEVICES 0x05 121#define HIDPP_ERROR_ALREADY_EXISTS 0x06 122#define HIDPP_ERROR_BUSY 0x07 123#define HIDPP_ERROR_UNKNOWN_DEVICE 0x08 124#define HIDPP_ERROR_RESOURCE_ERROR 0x09 125#define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a 126#define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b 127#define HIDPP_ERROR_WRONG_PIN_CODE 0x0c 128/* HID++ 2.0 error codes */ 129#define HIDPP20_ERROR 0xff 130 131static void hidpp_connect_event(struct hidpp_device *hidpp_dev); 132 133static int __hidpp_send_report(struct hid_device *hdev, 134 struct hidpp_report *hidpp_report) 135{ 136 int fields_count, ret; 137 138 switch (hidpp_report->report_id) { 139 case REPORT_ID_HIDPP_SHORT: 140 fields_count = HIDPP_REPORT_SHORT_LENGTH; 141 break; 142 case REPORT_ID_HIDPP_LONG: 143 fields_count = HIDPP_REPORT_LONG_LENGTH; 144 break; 145 default: 146 return -ENODEV; 147 } 148 149 /* 150 * set the device_index as the receiver, it will be overwritten by 151 * hid_hw_request if needed 152 */ 153 hidpp_report->device_index = 0xff; 154 155 ret = hid_hw_raw_request(hdev, hidpp_report->report_id, 156 (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT, 157 HID_REQ_SET_REPORT); 158 159 return ret == fields_count ? 0 : -1; 160} 161 162/** 163 * hidpp_send_message_sync() returns 0 in case of success, and something else 164 * in case of a failure. 165 * - If ' something else' is positive, that means that an error has been raised 166 * by the protocol itself. 167 * - If ' something else' is negative, that means that we had a classic error 168 * (-ENOMEM, -EPIPE, etc...) 169 */ 170static int hidpp_send_message_sync(struct hidpp_device *hidpp, 171 struct hidpp_report *message, 172 struct hidpp_report *response) 173{ 174 int ret; 175 176 mutex_lock(&hidpp->send_mutex); 177 178 hidpp->send_receive_buf = response; 179 hidpp->answer_available = false; 180 181 /* 182 * So that we can later validate the answer when it arrives 183 * in hidpp_raw_event 184 */ 185 *response = *message; 186 187 ret = __hidpp_send_report(hidpp->hid_dev, message); 188 189 if (ret) { 190 dbg_hid("__hidpp_send_report returned err: %d\n", ret); 191 memset(response, 0, sizeof(struct hidpp_report)); 192 goto exit; 193 } 194 195 if (!wait_event_timeout(hidpp->wait, hidpp->answer_available, 196 5*HZ)) { 197 dbg_hid("%s:timeout waiting for response\n", __func__); 198 memset(response, 0, sizeof(struct hidpp_report)); 199 ret = -ETIMEDOUT; 200 } 201 202 if (response->report_id == REPORT_ID_HIDPP_SHORT && 203 response->rap.sub_id == HIDPP_ERROR) { 204 ret = response->rap.params[1]; 205 dbg_hid("%s:got hidpp error %02X\n", __func__, ret); 206 goto exit; 207 } 208 209 if (response->report_id == REPORT_ID_HIDPP_LONG && 210 response->fap.feature_index == HIDPP20_ERROR) { 211 ret = response->fap.params[1]; 212 dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret); 213 goto exit; 214 } 215 216exit: 217 mutex_unlock(&hidpp->send_mutex); 218 return ret; 219 220} 221 222static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp, 223 u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count, 224 struct hidpp_report *response) 225{ 226 struct hidpp_report *message; 227 int ret; 228 229 if (param_count > sizeof(message->fap.params)) 230 return -EINVAL; 231 232 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL); 233 if (!message) 234 return -ENOMEM; 235 message->report_id = REPORT_ID_HIDPP_LONG; 236 message->fap.feature_index = feat_index; 237 message->fap.funcindex_clientid = funcindex_clientid; 238 memcpy(&message->fap.params, params, param_count); 239 240 ret = hidpp_send_message_sync(hidpp, message, response); 241 kfree(message); 242 return ret; 243} 244 245static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev, 246 u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count, 247 struct hidpp_report *response) 248{ 249 struct hidpp_report *message; 250 int ret; 251 252 if ((report_id != REPORT_ID_HIDPP_SHORT) && 253 (report_id != REPORT_ID_HIDPP_LONG)) 254 return -EINVAL; 255 256 if (param_count > sizeof(message->rap.params)) 257 return -EINVAL; 258 259 message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL); 260 if (!message) 261 return -ENOMEM; 262 message->report_id = report_id; 263 message->rap.sub_id = sub_id; 264 message->rap.reg_address = reg_address; 265 memcpy(&message->rap.params, params, param_count); 266 267 ret = hidpp_send_message_sync(hidpp_dev, message, response); 268 kfree(message); 269 return ret; 270} 271 272static void delayed_work_cb(struct work_struct *work) 273{ 274 struct hidpp_device *hidpp = container_of(work, struct hidpp_device, 275 work); 276 hidpp_connect_event(hidpp); 277} 278 279static inline bool hidpp_match_answer(struct hidpp_report *question, 280 struct hidpp_report *answer) 281{ 282 return (answer->fap.feature_index == question->fap.feature_index) && 283 (answer->fap.funcindex_clientid == question->fap.funcindex_clientid); 284} 285 286static inline bool hidpp_match_error(struct hidpp_report *question, 287 struct hidpp_report *answer) 288{ 289 return ((answer->rap.sub_id == HIDPP_ERROR) || 290 (answer->fap.feature_index == HIDPP20_ERROR)) && 291 (answer->fap.funcindex_clientid == question->fap.feature_index) && 292 (answer->fap.params[0] == question->fap.funcindex_clientid); 293} 294 295static inline bool hidpp_report_is_connect_event(struct hidpp_report *report) 296{ 297 return (report->report_id == REPORT_ID_HIDPP_SHORT) && 298 (report->rap.sub_id == 0x41); 299} 300 301/** 302 * hidpp_prefix_name() prefixes the current given name with "Logitech ". 303 */ 304static void hidpp_prefix_name(char **name, int name_length) 305{ 306#define PREFIX_LENGTH 9 /* "Logitech " */ 307 308 int new_length; 309 char *new_name; 310 311 if (name_length > PREFIX_LENGTH && 312 strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0) 313 /* The prefix has is already in the name */ 314 return; 315 316 new_length = PREFIX_LENGTH + name_length; 317 new_name = kzalloc(new_length, GFP_KERNEL); 318 if (!new_name) 319 return; 320 321 snprintf(new_name, new_length, "Logitech %s", *name); 322 323 kfree(*name); 324 325 *name = new_name; 326} 327 328/* -------------------------------------------------------------------------- */ 329/* HIDP++ 1.0 commands */ 330/* -------------------------------------------------------------------------- */ 331 332#define HIDPP_SET_REGISTER 0x80 333#define HIDPP_GET_REGISTER 0x81 334#define HIDPP_SET_LONG_REGISTER 0x82 335#define HIDPP_GET_LONG_REGISTER 0x83 336 337#define HIDPP_REG_PAIRING_INFORMATION 0xB5 338#define DEVICE_NAME 0x40 339 340static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev) 341{ 342 struct hidpp_report response; 343 int ret; 344 /* hid-logitech-dj is in charge of setting the right device index */ 345 u8 params[1] = { DEVICE_NAME }; 346 char *name; 347 int len; 348 349 ret = hidpp_send_rap_command_sync(hidpp_dev, 350 REPORT_ID_HIDPP_SHORT, 351 HIDPP_GET_LONG_REGISTER, 352 HIDPP_REG_PAIRING_INFORMATION, 353 params, 1, &response); 354 if (ret) 355 return NULL; 356 357 len = response.rap.params[1]; 358 359 if (2 + len > sizeof(response.rap.params)) 360 return NULL; 361 362 name = kzalloc(len + 1, GFP_KERNEL); 363 if (!name) 364 return NULL; 365 366 memcpy(name, &response.rap.params[2], len); 367 368 /* include the terminating '\0' */ 369 hidpp_prefix_name(&name, len + 1); 370 371 return name; 372} 373 374/* -------------------------------------------------------------------------- */ 375/* 0x0000: Root */ 376/* -------------------------------------------------------------------------- */ 377 378#define HIDPP_PAGE_ROOT 0x0000 379#define HIDPP_PAGE_ROOT_IDX 0x00 380 381#define CMD_ROOT_GET_FEATURE 0x01 382#define CMD_ROOT_GET_PROTOCOL_VERSION 0x11 383 384static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature, 385 u8 *feature_index, u8 *feature_type) 386{ 387 struct hidpp_report response; 388 int ret; 389 u8 params[2] = { feature >> 8, feature & 0x00FF }; 390 391 ret = hidpp_send_fap_command_sync(hidpp, 392 HIDPP_PAGE_ROOT_IDX, 393 CMD_ROOT_GET_FEATURE, 394 params, 2, &response); 395 if (ret) 396 return ret; 397 398 *feature_index = response.fap.params[0]; 399 *feature_type = response.fap.params[1]; 400 401 return ret; 402} 403 404static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp) 405{ 406 struct hidpp_report response; 407 int ret; 408 409 ret = hidpp_send_fap_command_sync(hidpp, 410 HIDPP_PAGE_ROOT_IDX, 411 CMD_ROOT_GET_PROTOCOL_VERSION, 412 NULL, 0, &response); 413 414 if (ret == HIDPP_ERROR_INVALID_SUBID) { 415 hidpp->protocol_major = 1; 416 hidpp->protocol_minor = 0; 417 return 0; 418 } 419 420 /* the device might not be connected */ 421 if (ret == HIDPP_ERROR_RESOURCE_ERROR) 422 return -EIO; 423 424 if (ret > 0) { 425 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 426 __func__, ret); 427 return -EPROTO; 428 } 429 if (ret) 430 return ret; 431 432 hidpp->protocol_major = response.fap.params[0]; 433 hidpp->protocol_minor = response.fap.params[1]; 434 435 return ret; 436} 437 438static bool hidpp_is_connected(struct hidpp_device *hidpp) 439{ 440 int ret; 441 442 ret = hidpp_root_get_protocol_version(hidpp); 443 if (!ret) 444 hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n", 445 hidpp->protocol_major, hidpp->protocol_minor); 446 return ret == 0; 447} 448 449/* -------------------------------------------------------------------------- */ 450/* 0x0005: GetDeviceNameType */ 451/* -------------------------------------------------------------------------- */ 452 453#define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005 454 455#define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01 456#define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11 457#define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21 458 459static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp, 460 u8 feature_index, u8 *nameLength) 461{ 462 struct hidpp_report response; 463 int ret; 464 465 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 466 CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response); 467 468 if (ret > 0) { 469 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 470 __func__, ret); 471 return -EPROTO; 472 } 473 if (ret) 474 return ret; 475 476 *nameLength = response.fap.params[0]; 477 478 return ret; 479} 480 481static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp, 482 u8 feature_index, u8 char_index, char *device_name, int len_buf) 483{ 484 struct hidpp_report response; 485 int ret, i; 486 int count; 487 488 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 489 CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1, 490 &response); 491 492 if (ret > 0) { 493 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 494 __func__, ret); 495 return -EPROTO; 496 } 497 if (ret) 498 return ret; 499 500 if (response.report_id == REPORT_ID_HIDPP_LONG) 501 count = HIDPP_REPORT_LONG_LENGTH - 4; 502 else 503 count = HIDPP_REPORT_SHORT_LENGTH - 4; 504 505 if (len_buf < count) 506 count = len_buf; 507 508 for (i = 0; i < count; i++) 509 device_name[i] = response.fap.params[i]; 510 511 return count; 512} 513 514static char *hidpp_get_device_name(struct hidpp_device *hidpp) 515{ 516 u8 feature_type; 517 u8 feature_index; 518 u8 __name_length; 519 char *name; 520 unsigned index = 0; 521 int ret; 522 523 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE, 524 &feature_index, &feature_type); 525 if (ret) 526 return NULL; 527 528 ret = hidpp_devicenametype_get_count(hidpp, feature_index, 529 &__name_length); 530 if (ret) 531 return NULL; 532 533 name = kzalloc(__name_length + 1, GFP_KERNEL); 534 if (!name) 535 return NULL; 536 537 while (index < __name_length) { 538 ret = hidpp_devicenametype_get_device_name(hidpp, 539 feature_index, index, name + index, 540 __name_length - index); 541 if (ret <= 0) { 542 kfree(name); 543 return NULL; 544 } 545 index += ret; 546 } 547 548 /* include the terminating '\0' */ 549 hidpp_prefix_name(&name, __name_length + 1); 550 551 return name; 552} 553 554/* -------------------------------------------------------------------------- */ 555/* 0x6100: TouchPadRawXY */ 556/* -------------------------------------------------------------------------- */ 557 558#define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100 559 560#define CMD_TOUCHPAD_GET_RAW_INFO 0x01 561#define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21 562 563#define EVENT_TOUCHPAD_RAW_XY 0x00 564 565#define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01 566#define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03 567 568struct hidpp_touchpad_raw_info { 569 u16 x_size; 570 u16 y_size; 571 u8 z_range; 572 u8 area_range; 573 u8 timestamp_unit; 574 u8 maxcontacts; 575 u8 origin; 576 u16 res; 577}; 578 579struct hidpp_touchpad_raw_xy_finger { 580 u8 contact_type; 581 u8 contact_status; 582 u16 x; 583 u16 y; 584 u8 z; 585 u8 area; 586 u8 finger_id; 587}; 588 589struct hidpp_touchpad_raw_xy { 590 u16 timestamp; 591 struct hidpp_touchpad_raw_xy_finger fingers[2]; 592 u8 spurious_flag; 593 u8 end_of_frame; 594 u8 finger_count; 595 u8 button; 596}; 597 598static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp, 599 u8 feature_index, struct hidpp_touchpad_raw_info *raw_info) 600{ 601 struct hidpp_report response; 602 int ret; 603 u8 *params = (u8 *)response.fap.params; 604 605 ret = hidpp_send_fap_command_sync(hidpp, feature_index, 606 CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response); 607 608 if (ret > 0) { 609 hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", 610 __func__, ret); 611 return -EPROTO; 612 } 613 if (ret) 614 return ret; 615 616 raw_info->x_size = get_unaligned_be16(¶ms[0]); 617 raw_info->y_size = get_unaligned_be16(¶ms[2]); 618 raw_info->z_range = params[4]; 619 raw_info->area_range = params[5]; 620 raw_info->maxcontacts = params[7]; 621 raw_info->origin = params[8]; 622 /* res is given in unit per inch */ 623 raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51; 624 625 return ret; 626} 627 628static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev, 629 u8 feature_index, bool send_raw_reports, 630 bool sensor_enhanced_settings) 631{ 632 struct hidpp_report response; 633 634 /* 635 * Params: 636 * bit 0 - enable raw 637 * bit 1 - 16bit Z, no area 638 * bit 2 - enhanced sensitivity 639 * bit 3 - width, height (4 bits each) instead of area 640 * bit 4 - send raw + gestures (degrades smoothness) 641 * remaining bits - reserved 642 */ 643 u8 params = send_raw_reports | (sensor_enhanced_settings << 2); 644 645 return hidpp_send_fap_command_sync(hidpp_dev, feature_index, 646 CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response); 647} 648 649static void hidpp_touchpad_touch_event(u8 *data, 650 struct hidpp_touchpad_raw_xy_finger *finger) 651{ 652 u8 x_m = data[0] << 2; 653 u8 y_m = data[2] << 2; 654 655 finger->x = x_m << 6 | data[1]; 656 finger->y = y_m << 6 | data[3]; 657 658 finger->contact_type = data[0] >> 6; 659 finger->contact_status = data[2] >> 6; 660 661 finger->z = data[4]; 662 finger->area = data[5]; 663 finger->finger_id = data[6] >> 4; 664} 665 666static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev, 667 u8 *data, struct hidpp_touchpad_raw_xy *raw_xy) 668{ 669 memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy)); 670 raw_xy->end_of_frame = data[8] & 0x01; 671 raw_xy->spurious_flag = (data[8] >> 1) & 0x01; 672 raw_xy->finger_count = data[15] & 0x0f; 673 raw_xy->button = (data[8] >> 2) & 0x01; 674 675 if (raw_xy->finger_count) { 676 hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]); 677 hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]); 678 } 679} 680 681/* ************************************************************************** */ 682/* */ 683/* Device Support */ 684/* */ 685/* ************************************************************************** */ 686 687/* -------------------------------------------------------------------------- */ 688/* Touchpad HID++ devices */ 689/* -------------------------------------------------------------------------- */ 690 691#define WTP_MANUAL_RESOLUTION 39 692 693struct wtp_data { 694 struct input_dev *input; 695 u16 x_size, y_size; 696 u8 finger_count; 697 u8 mt_feature_index; 698 u8 button_feature_index; 699 u8 maxcontacts; 700 bool flip_y; 701 unsigned int resolution; 702}; 703 704static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi, 705 struct hid_field *field, struct hid_usage *usage, 706 unsigned long **bit, int *max) 707{ 708 return -1; 709} 710 711static void wtp_populate_input(struct hidpp_device *hidpp, 712 struct input_dev *input_dev, bool origin_is_hid_core) 713{ 714 struct wtp_data *wd = hidpp->private_data; 715 716 __set_bit(EV_ABS, input_dev->evbit); 717 __set_bit(EV_KEY, input_dev->evbit); 718 __clear_bit(EV_REL, input_dev->evbit); 719 __clear_bit(EV_LED, input_dev->evbit); 720 721 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0); 722 input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution); 723 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0); 724 input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution); 725 726 /* Max pressure is not given by the devices, pick one */ 727 input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0); 728 729 input_set_capability(input_dev, EV_KEY, BTN_LEFT); 730 731 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) 732 input_set_capability(input_dev, EV_KEY, BTN_RIGHT); 733 else 734 __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit); 735 736 input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER | 737 INPUT_MT_DROP_UNUSED); 738 739 wd->input = input_dev; 740} 741 742static void wtp_touch_event(struct wtp_data *wd, 743 struct hidpp_touchpad_raw_xy_finger *touch_report) 744{ 745 int slot; 746 747 if (!touch_report->finger_id || touch_report->contact_type) 748 /* no actual data */ 749 return; 750 751 slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id); 752 753 input_mt_slot(wd->input, slot); 754 input_mt_report_slot_state(wd->input, MT_TOOL_FINGER, 755 touch_report->contact_status); 756 if (touch_report->contact_status) { 757 input_event(wd->input, EV_ABS, ABS_MT_POSITION_X, 758 touch_report->x); 759 input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y, 760 wd->flip_y ? wd->y_size - touch_report->y : 761 touch_report->y); 762 input_event(wd->input, EV_ABS, ABS_MT_PRESSURE, 763 touch_report->area); 764 } 765} 766 767static void wtp_send_raw_xy_event(struct hidpp_device *hidpp, 768 struct hidpp_touchpad_raw_xy *raw) 769{ 770 struct wtp_data *wd = hidpp->private_data; 771 int i; 772 773 for (i = 0; i < 2; i++) 774 wtp_touch_event(wd, &(raw->fingers[i])); 775 776 if (raw->end_of_frame && 777 !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)) 778 input_event(wd->input, EV_KEY, BTN_LEFT, raw->button); 779 780 if (raw->end_of_frame || raw->finger_count <= 2) { 781 input_mt_sync_frame(wd->input); 782 input_sync(wd->input); 783 } 784} 785 786static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data) 787{ 788 struct wtp_data *wd = hidpp->private_data; 789 u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) + 790 (data[7] >> 4) * (data[7] >> 4)) / 2; 791 u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) + 792 (data[13] >> 4) * (data[13] >> 4)) / 2; 793 struct hidpp_touchpad_raw_xy raw = { 794 .timestamp = data[1], 795 .fingers = { 796 { 797 .contact_type = 0, 798 .contact_status = !!data[7], 799 .x = get_unaligned_le16(&data[3]), 800 .y = get_unaligned_le16(&data[5]), 801 .z = c1_area, 802 .area = c1_area, 803 .finger_id = data[2], 804 }, { 805 .contact_type = 0, 806 .contact_status = !!data[13], 807 .x = get_unaligned_le16(&data[9]), 808 .y = get_unaligned_le16(&data[11]), 809 .z = c2_area, 810 .area = c2_area, 811 .finger_id = data[8], 812 } 813 }, 814 .finger_count = wd->maxcontacts, 815 .spurious_flag = 0, 816 .end_of_frame = (data[0] >> 7) == 0, 817 .button = data[0] & 0x01, 818 }; 819 820 wtp_send_raw_xy_event(hidpp, &raw); 821 822 return 1; 823} 824 825static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size) 826{ 827 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 828 struct wtp_data *wd = hidpp->private_data; 829 struct hidpp_report *report = (struct hidpp_report *)data; 830 struct hidpp_touchpad_raw_xy raw; 831 832 if (!wd || !wd->input) 833 return 1; 834 835 switch (data[0]) { 836 case 0x02: 837 if (size < 2) { 838 hid_err(hdev, "Received HID report of bad size (%d)", 839 size); 840 return 1; 841 } 842 if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) { 843 input_event(wd->input, EV_KEY, BTN_LEFT, 844 !!(data[1] & 0x01)); 845 input_event(wd->input, EV_KEY, BTN_RIGHT, 846 !!(data[1] & 0x02)); 847 input_sync(wd->input); 848 return 0; 849 } else { 850 if (size < 21) 851 return 1; 852 return wtp_mouse_raw_xy_event(hidpp, &data[7]); 853 } 854 case REPORT_ID_HIDPP_LONG: 855 /* size is already checked in hidpp_raw_event. */ 856 if ((report->fap.feature_index != wd->mt_feature_index) || 857 (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY)) 858 return 1; 859 hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw); 860 861 wtp_send_raw_xy_event(hidpp, &raw); 862 return 0; 863 } 864 865 return 0; 866} 867 868static int wtp_get_config(struct hidpp_device *hidpp) 869{ 870 struct wtp_data *wd = hidpp->private_data; 871 struct hidpp_touchpad_raw_info raw_info = {0}; 872 u8 feature_type; 873 int ret; 874 875 ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY, 876 &wd->mt_feature_index, &feature_type); 877 if (ret) 878 /* means that the device is not powered up */ 879 return ret; 880 881 ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index, 882 &raw_info); 883 if (ret) 884 return ret; 885 886 wd->x_size = raw_info.x_size; 887 wd->y_size = raw_info.y_size; 888 wd->maxcontacts = raw_info.maxcontacts; 889 wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT; 890 wd->resolution = raw_info.res; 891 if (!wd->resolution) 892 wd->resolution = WTP_MANUAL_RESOLUTION; 893 894 return 0; 895} 896 897static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id) 898{ 899 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 900 struct wtp_data *wd; 901 902 wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data), 903 GFP_KERNEL); 904 if (!wd) 905 return -ENOMEM; 906 907 hidpp->private_data = wd; 908 909 return 0; 910}; 911 912static int wtp_connect(struct hid_device *hdev, bool connected) 913{ 914 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 915 struct wtp_data *wd = hidpp->private_data; 916 int ret; 917 918 if (!connected) 919 return 0; 920 921 if (!wd->x_size) { 922 ret = wtp_get_config(hidpp); 923 if (ret) { 924 hid_err(hdev, "Can not get wtp config: %d\n", ret); 925 return ret; 926 } 927 } 928 929 return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index, 930 true, true); 931} 932 933/* -------------------------------------------------------------------------- */ 934/* Generic HID++ devices */ 935/* -------------------------------------------------------------------------- */ 936 937static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi, 938 struct hid_field *field, struct hid_usage *usage, 939 unsigned long **bit, int *max) 940{ 941 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 942 943 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) 944 return wtp_input_mapping(hdev, hi, field, usage, bit, max); 945 946 return 0; 947} 948 949static void hidpp_populate_input(struct hidpp_device *hidpp, 950 struct input_dev *input, bool origin_is_hid_core) 951{ 952 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) 953 wtp_populate_input(hidpp, input, origin_is_hid_core); 954} 955 956static void hidpp_input_configured(struct hid_device *hdev, 957 struct hid_input *hidinput) 958{ 959 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 960 struct input_dev *input = hidinput->input; 961 962 hidpp_populate_input(hidpp, input, true); 963} 964 965static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data, 966 int size) 967{ 968 struct hidpp_report *question = hidpp->send_receive_buf; 969 struct hidpp_report *answer = hidpp->send_receive_buf; 970 struct hidpp_report *report = (struct hidpp_report *)data; 971 972 /* 973 * If the mutex is locked then we have a pending answer from a 974 * previously sent command. 975 */ 976 if (unlikely(mutex_is_locked(&hidpp->send_mutex))) { 977 /* 978 * Check for a correct hidpp20 answer or the corresponding 979 * error 980 */ 981 if (hidpp_match_answer(question, report) || 982 hidpp_match_error(question, report)) { 983 *answer = *report; 984 hidpp->answer_available = true; 985 wake_up(&hidpp->wait); 986 /* 987 * This was an answer to a command that this driver sent 988 * We return 1 to hid-core to avoid forwarding the 989 * command upstream as it has been treated by the driver 990 */ 991 992 return 1; 993 } 994 } 995 996 if (unlikely(hidpp_report_is_connect_event(report))) { 997 atomic_set(&hidpp->connected, 998 !(report->rap.params[0] & (1 << 6))); 999 if ((hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) && 1000 (schedule_work(&hidpp->work) == 0)) 1001 dbg_hid("%s: connect event already queued\n", __func__); 1002 return 1; 1003 } 1004 1005 return 0; 1006} 1007 1008static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report, 1009 u8 *data, int size) 1010{ 1011 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 1012 int ret = 0; 1013 1014 /* Generic HID++ processing. */ 1015 switch (data[0]) { 1016 case REPORT_ID_HIDPP_LONG: 1017 if (size != HIDPP_REPORT_LONG_LENGTH) { 1018 hid_err(hdev, "received hid++ report of bad size (%d)", 1019 size); 1020 return 1; 1021 } 1022 ret = hidpp_raw_hidpp_event(hidpp, data, size); 1023 break; 1024 case REPORT_ID_HIDPP_SHORT: 1025 if (size != HIDPP_REPORT_SHORT_LENGTH) { 1026 hid_err(hdev, "received hid++ report of bad size (%d)", 1027 size); 1028 return 1; 1029 } 1030 ret = hidpp_raw_hidpp_event(hidpp, data, size); 1031 break; 1032 } 1033 1034 /* If no report is available for further processing, skip calling 1035 * raw_event of subclasses. */ 1036 if (ret != 0) 1037 return ret; 1038 1039 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) 1040 return wtp_raw_event(hdev, data, size); 1041 1042 return 0; 1043} 1044 1045static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying) 1046{ 1047 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 1048 char *name; 1049 1050 if (use_unifying) 1051 /* 1052 * the device is connected through an Unifying receiver, and 1053 * might not be already connected. 1054 * Ask the receiver for its name. 1055 */ 1056 name = hidpp_get_unifying_name(hidpp); 1057 else 1058 name = hidpp_get_device_name(hidpp); 1059 1060 if (!name) 1061 hid_err(hdev, "unable to retrieve the name of the device"); 1062 else 1063 snprintf(hdev->name, sizeof(hdev->name), "%s", name); 1064 1065 kfree(name); 1066} 1067 1068static int hidpp_input_open(struct input_dev *dev) 1069{ 1070 struct hid_device *hid = input_get_drvdata(dev); 1071 1072 return hid_hw_open(hid); 1073} 1074 1075static void hidpp_input_close(struct input_dev *dev) 1076{ 1077 struct hid_device *hid = input_get_drvdata(dev); 1078 1079 hid_hw_close(hid); 1080} 1081 1082static struct input_dev *hidpp_allocate_input(struct hid_device *hdev) 1083{ 1084 struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev); 1085 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 1086 1087 if (!input_dev) 1088 return NULL; 1089 1090 input_set_drvdata(input_dev, hdev); 1091 input_dev->open = hidpp_input_open; 1092 input_dev->close = hidpp_input_close; 1093 1094 input_dev->name = hidpp->name; 1095 input_dev->phys = hdev->phys; 1096 input_dev->uniq = hdev->uniq; 1097 input_dev->id.bustype = hdev->bus; 1098 input_dev->id.vendor = hdev->vendor; 1099 input_dev->id.product = hdev->product; 1100 input_dev->id.version = hdev->version; 1101 input_dev->dev.parent = &hdev->dev; 1102 1103 return input_dev; 1104} 1105 1106static void hidpp_connect_event(struct hidpp_device *hidpp) 1107{ 1108 struct hid_device *hdev = hidpp->hid_dev; 1109 int ret = 0; 1110 bool connected = atomic_read(&hidpp->connected); 1111 struct input_dev *input; 1112 char *name, *devm_name; 1113 1114 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) { 1115 ret = wtp_connect(hdev, connected); 1116 if (ret) 1117 return; 1118 } 1119 1120 if (!connected || hidpp->delayed_input) 1121 return; 1122 1123 if (!hidpp->protocol_major) { 1124 ret = !hidpp_is_connected(hidpp); 1125 if (ret) { 1126 hid_err(hdev, "Can not get the protocol version.\n"); 1127 return; 1128 } 1129 } 1130 1131 /* the device is already connected, we can ask for its name and 1132 * protocol */ 1133 hid_info(hdev, "HID++ %u.%u device connected.\n", 1134 hidpp->protocol_major, hidpp->protocol_minor); 1135 1136 if (!hidpp->name || hidpp->name == hdev->name) { 1137 name = hidpp_get_device_name(hidpp); 1138 if (!name) { 1139 hid_err(hdev, 1140 "unable to retrieve the name of the device"); 1141 return; 1142 } 1143 1144 devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name); 1145 kfree(name); 1146 if (!devm_name) 1147 return; 1148 1149 hidpp->name = devm_name; 1150 } 1151 1152 input = hidpp_allocate_input(hdev); 1153 if (!input) { 1154 hid_err(hdev, "cannot allocate new input device: %d\n", ret); 1155 return; 1156 } 1157 1158 hidpp_populate_input(hidpp, input, false); 1159 1160 ret = input_register_device(input); 1161 if (ret) 1162 input_free_device(input); 1163 1164 hidpp->delayed_input = input; 1165} 1166 1167static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id) 1168{ 1169 struct hidpp_device *hidpp; 1170 int ret; 1171 bool connected; 1172 unsigned int connect_mask = HID_CONNECT_DEFAULT; 1173 1174 hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device), 1175 GFP_KERNEL); 1176 if (!hidpp) 1177 return -ENOMEM; 1178 1179 hidpp->hid_dev = hdev; 1180 hidpp->name = hdev->name; 1181 hid_set_drvdata(hdev, hidpp); 1182 1183 hidpp->quirks = id->driver_data; 1184 1185 if (disable_raw_mode) { 1186 hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP; 1187 hidpp->quirks &= ~HIDPP_QUIRK_DELAYED_INIT; 1188 } 1189 1190 if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) { 1191 ret = wtp_allocate(hdev, id); 1192 if (ret) 1193 goto wtp_allocate_fail; 1194 } 1195 1196 INIT_WORK(&hidpp->work, delayed_work_cb); 1197 mutex_init(&hidpp->send_mutex); 1198 init_waitqueue_head(&hidpp->wait); 1199 1200 ret = hid_parse(hdev); 1201 if (ret) { 1202 hid_err(hdev, "%s:parse failed\n", __func__); 1203 goto hid_parse_fail; 1204 } 1205 1206 /* Allow incoming packets */ 1207 hid_device_io_start(hdev); 1208 1209 connected = hidpp_is_connected(hidpp); 1210 if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) { 1211 if (!connected) { 1212 ret = -ENODEV; 1213 hid_err(hdev, "Device not connected"); 1214 hid_device_io_stop(hdev); 1215 goto hid_parse_fail; 1216 } 1217 1218 hid_info(hdev, "HID++ %u.%u device connected.\n", 1219 hidpp->protocol_major, hidpp->protocol_minor); 1220 } 1221 1222 hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE); 1223 atomic_set(&hidpp->connected, connected); 1224 1225 if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) { 1226 ret = wtp_get_config(hidpp); 1227 if (ret) 1228 goto hid_parse_fail; 1229 } 1230 1231 /* Block incoming packets */ 1232 hid_device_io_stop(hdev); 1233 1234 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) 1235 connect_mask &= ~HID_CONNECT_HIDINPUT; 1236 1237 ret = hid_hw_start(hdev, connect_mask); 1238 if (ret) { 1239 hid_err(hdev, "%s:hid_hw_start returned error\n", __func__); 1240 goto hid_hw_start_fail; 1241 } 1242 1243 if (hidpp->quirks & HIDPP_QUIRK_DELAYED_INIT) { 1244 /* Allow incoming packets */ 1245 hid_device_io_start(hdev); 1246 1247 hidpp_connect_event(hidpp); 1248 } 1249 1250 return ret; 1251 1252hid_hw_start_fail: 1253hid_parse_fail: 1254 cancel_work_sync(&hidpp->work); 1255 mutex_destroy(&hidpp->send_mutex); 1256wtp_allocate_fail: 1257 hid_set_drvdata(hdev, NULL); 1258 return ret; 1259} 1260 1261static void hidpp_remove(struct hid_device *hdev) 1262{ 1263 struct hidpp_device *hidpp = hid_get_drvdata(hdev); 1264 1265 cancel_work_sync(&hidpp->work); 1266 mutex_destroy(&hidpp->send_mutex); 1267 hid_hw_stop(hdev); 1268} 1269 1270static const struct hid_device_id hidpp_devices[] = { 1271 { /* wireless touchpad */ 1272 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, 1273 USB_VENDOR_ID_LOGITECH, 0x4011), 1274 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT | 1275 HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS }, 1276 { /* wireless touchpad T650 */ 1277 HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, 1278 USB_VENDOR_ID_LOGITECH, 0x4101), 1279 .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT }, 1280 { /* wireless touchpad T651 */ 1281 HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, 1282 USB_DEVICE_ID_LOGITECH_T651), 1283 .driver_data = HIDPP_QUIRK_CLASS_WTP }, 1284 1285 { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, 1286 USB_VENDOR_ID_LOGITECH, HID_ANY_ID)}, 1287 {} 1288}; 1289 1290MODULE_DEVICE_TABLE(hid, hidpp_devices); 1291 1292static struct hid_driver hidpp_driver = { 1293 .name = "logitech-hidpp-device", 1294 .id_table = hidpp_devices, 1295 .probe = hidpp_probe, 1296 .remove = hidpp_remove, 1297 .raw_event = hidpp_raw_event, 1298 .input_configured = hidpp_input_configured, 1299 .input_mapping = hidpp_input_mapping, 1300}; 1301 1302module_hid_driver(hidpp_driver); 1303