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