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> |
Edwin Velds | ff21a63 | 2016-01-11 00:25:15 +0100 | [diff] [blame] | 18 | #include <linux/input.h> |
| 19 | #include <linux/usb.h> |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 20 | #include <linux/hid.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/kfifo.h> |
| 25 | #include <linux/input/mt.h> |
Edwin Velds | ff21a63 | 2016-01-11 00:25:15 +0100 | [diff] [blame] | 26 | #include <linux/workqueue.h> |
| 27 | #include <linux/atomic.h> |
| 28 | #include <linux/fixp-arith.h> |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 29 | #include <asm/unaligned.h> |
Edwin Velds | ff21a63 | 2016-01-11 00:25:15 +0100 | [diff] [blame] | 30 | #include "usbhid/usbhid.h" |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 31 | #include "hid-ids.h" |
| 32 | |
| 33 | MODULE_LICENSE("GPL"); |
| 34 | MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>"); |
| 35 | MODULE_AUTHOR("Nestor Lopez Casado <nlopezcasad@logitech.com>"); |
| 36 | |
Benjamin Tissoires | 9188dba | 2015-03-26 12:41:57 -0400 | [diff] [blame] | 37 | static bool disable_raw_mode; |
| 38 | module_param(disable_raw_mode, bool, 0644); |
| 39 | MODULE_PARM_DESC(disable_raw_mode, |
| 40 | "Disable Raw mode reporting for touchpads and keep firmware gestures."); |
| 41 | |
Benjamin Tissoires | 90cdd98 | 2015-09-03 09:08:30 -0400 | [diff] [blame] | 42 | static bool disable_tap_to_click; |
| 43 | module_param(disable_tap_to_click, bool, 0644); |
| 44 | MODULE_PARM_DESC(disable_tap_to_click, |
| 45 | "Disable Tap-To-Click mode reporting for touchpads (only on the K400 currently)."); |
| 46 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 47 | #define REPORT_ID_HIDPP_SHORT 0x10 |
| 48 | #define REPORT_ID_HIDPP_LONG 0x11 |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 49 | #define REPORT_ID_HIDPP_VERY_LONG 0x12 |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 50 | |
| 51 | #define HIDPP_REPORT_SHORT_LENGTH 7 |
| 52 | #define HIDPP_REPORT_LONG_LENGTH 20 |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 53 | #define HIDPP_REPORT_VERY_LONG_LENGTH 64 |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 54 | |
| 55 | #define HIDPP_QUIRK_CLASS_WTP BIT(0) |
Goffredo Baroncelli | 8a09b4f | 2015-05-30 11:00:27 +0200 | [diff] [blame] | 56 | #define HIDPP_QUIRK_CLASS_M560 BIT(1) |
Benjamin Tissoires | 90cdd98 | 2015-09-03 09:08:30 -0400 | [diff] [blame] | 57 | #define HIDPP_QUIRK_CLASS_K400 BIT(2) |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 58 | #define HIDPP_QUIRK_CLASS_G920 BIT(3) |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 59 | |
Goffredo Baroncelli | 8a09b4f | 2015-05-30 11:00:27 +0200 | [diff] [blame] | 60 | /* bits 2..20 are reserved for classes */ |
Benjamin Tissoires | 6bd4e65 | 2016-06-29 19:28:02 +1000 | [diff] [blame] | 61 | /* #define HIDPP_QUIRK_CONNECT_EVENTS BIT(21) disabled */ |
Benjamin Tissoires | 57ac86c | 2014-09-30 13:18:34 -0400 | [diff] [blame] | 62 | #define HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS BIT(22) |
Benjamin Tissoires | 580a7e8 | 2015-09-03 09:08:29 -0400 | [diff] [blame] | 63 | #define HIDPP_QUIRK_NO_HIDINPUT BIT(23) |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 64 | #define HIDPP_QUIRK_FORCE_OUTPUT_REPORTS BIT(24) |
Benjamin Tissoires | 580a7e8 | 2015-09-03 09:08:29 -0400 | [diff] [blame] | 65 | |
Benjamin Tissoires | 6bd4e65 | 2016-06-29 19:28:02 +1000 | [diff] [blame] | 66 | #define HIDPP_QUIRK_DELAYED_INIT HIDPP_QUIRK_NO_HIDINPUT |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 67 | |
Benjamin Tissoires | 206d7c6 | 2017-03-27 16:59:25 +0200 | [diff] [blame^] | 68 | #define HIDPP_CAPABILITY_HIDPP10_BATTERY BIT(0) |
| 69 | #define HIDPP_CAPABILITY_HIDPP20_BATTERY BIT(1) |
| 70 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 71 | /* |
| 72 | * There are two hidpp protocols in use, the first version hidpp10 is known |
| 73 | * as register access protocol or RAP, the second version hidpp20 is known as |
| 74 | * feature access protocol or FAP |
| 75 | * |
| 76 | * Most older devices (including the Unifying usb receiver) use the RAP protocol |
| 77 | * where as most newer devices use the FAP protocol. Both protocols are |
| 78 | * compatible with the underlying transport, which could be usb, Unifiying, or |
| 79 | * bluetooth. The message lengths are defined by the hid vendor specific report |
| 80 | * descriptor for the HIDPP_SHORT report type (total message lenth 7 bytes) and |
| 81 | * the HIDPP_LONG report type (total message length 20 bytes) |
| 82 | * |
| 83 | * The RAP protocol uses both report types, whereas the FAP only uses HIDPP_LONG |
| 84 | * messages. The Unifying receiver itself responds to RAP messages (device index |
| 85 | * is 0xFF for the receiver), and all messages (short or long) with a device |
| 86 | * index between 1 and 6 are passed untouched to the corresponding paired |
| 87 | * Unifying device. |
| 88 | * |
| 89 | * The paired device can be RAP or FAP, it will receive the message untouched |
| 90 | * from the Unifiying receiver. |
| 91 | */ |
| 92 | |
| 93 | struct fap { |
| 94 | u8 feature_index; |
| 95 | u8 funcindex_clientid; |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 96 | u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U]; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | struct rap { |
| 100 | u8 sub_id; |
| 101 | u8 reg_address; |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 102 | u8 params[HIDPP_REPORT_VERY_LONG_LENGTH - 4U]; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | struct hidpp_report { |
| 106 | u8 report_id; |
| 107 | u8 device_index; |
| 108 | union { |
| 109 | struct fap fap; |
| 110 | struct rap rap; |
| 111 | u8 rawbytes[sizeof(struct fap)]; |
| 112 | }; |
| 113 | } __packed; |
| 114 | |
Peter Hutterer | 5a2b190 | 2016-06-29 19:28:01 +1000 | [diff] [blame] | 115 | struct hidpp_battery { |
| 116 | u8 feature_index; |
| 117 | struct power_supply_desc desc; |
| 118 | struct power_supply *ps; |
| 119 | char name[64]; |
| 120 | int status; |
| 121 | int level; |
| 122 | }; |
| 123 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 124 | struct hidpp_device { |
| 125 | struct hid_device *hid_dev; |
| 126 | struct mutex send_mutex; |
| 127 | void *send_receive_buf; |
Benjamin Tissoires | 005b3f5 | 2015-01-08 14:37:12 -0500 | [diff] [blame] | 128 | char *name; /* will never be NULL and should not be freed */ |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 129 | wait_queue_head_t wait; |
| 130 | bool answer_available; |
| 131 | u8 protocol_major; |
| 132 | u8 protocol_minor; |
| 133 | |
| 134 | void *private_data; |
| 135 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 136 | struct work_struct work; |
| 137 | struct kfifo delayed_work_fifo; |
| 138 | atomic_t connected; |
| 139 | struct input_dev *delayed_input; |
| 140 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 141 | unsigned long quirks; |
Benjamin Tissoires | 206d7c6 | 2017-03-27 16:59:25 +0200 | [diff] [blame^] | 142 | unsigned long capabilities; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 143 | |
Peter Hutterer | 5a2b190 | 2016-06-29 19:28:01 +1000 | [diff] [blame] | 144 | struct hidpp_battery battery; |
| 145 | }; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 146 | |
Peter Wu | f677bb1 | 2014-12-16 01:50:14 +0100 | [diff] [blame] | 147 | /* HID++ 1.0 error codes */ |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 148 | #define HIDPP_ERROR 0x8f |
| 149 | #define HIDPP_ERROR_SUCCESS 0x00 |
| 150 | #define HIDPP_ERROR_INVALID_SUBID 0x01 |
| 151 | #define HIDPP_ERROR_INVALID_ADRESS 0x02 |
| 152 | #define HIDPP_ERROR_INVALID_VALUE 0x03 |
| 153 | #define HIDPP_ERROR_CONNECT_FAIL 0x04 |
| 154 | #define HIDPP_ERROR_TOO_MANY_DEVICES 0x05 |
| 155 | #define HIDPP_ERROR_ALREADY_EXISTS 0x06 |
| 156 | #define HIDPP_ERROR_BUSY 0x07 |
| 157 | #define HIDPP_ERROR_UNKNOWN_DEVICE 0x08 |
| 158 | #define HIDPP_ERROR_RESOURCE_ERROR 0x09 |
| 159 | #define HIDPP_ERROR_REQUEST_UNAVAILABLE 0x0a |
| 160 | #define HIDPP_ERROR_INVALID_PARAM_VALUE 0x0b |
| 161 | #define HIDPP_ERROR_WRONG_PIN_CODE 0x0c |
Peter Wu | f677bb1 | 2014-12-16 01:50:14 +0100 | [diff] [blame] | 162 | /* HID++ 2.0 error codes */ |
| 163 | #define HIDPP20_ERROR 0xff |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 164 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 165 | static void hidpp_connect_event(struct hidpp_device *hidpp_dev); |
| 166 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 167 | static int __hidpp_send_report(struct hid_device *hdev, |
| 168 | struct hidpp_report *hidpp_report) |
| 169 | { |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 170 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 171 | int fields_count, ret; |
| 172 | |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 173 | hidpp = hid_get_drvdata(hdev); |
| 174 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 175 | switch (hidpp_report->report_id) { |
| 176 | case REPORT_ID_HIDPP_SHORT: |
| 177 | fields_count = HIDPP_REPORT_SHORT_LENGTH; |
| 178 | break; |
| 179 | case REPORT_ID_HIDPP_LONG: |
| 180 | fields_count = HIDPP_REPORT_LONG_LENGTH; |
| 181 | break; |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 182 | case REPORT_ID_HIDPP_VERY_LONG: |
| 183 | fields_count = HIDPP_REPORT_VERY_LONG_LENGTH; |
| 184 | break; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 185 | default: |
| 186 | return -ENODEV; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * set the device_index as the receiver, it will be overwritten by |
| 191 | * hid_hw_request if needed |
| 192 | */ |
| 193 | hidpp_report->device_index = 0xff; |
| 194 | |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 195 | if (hidpp->quirks & HIDPP_QUIRK_FORCE_OUTPUT_REPORTS) { |
| 196 | ret = hid_hw_output_report(hdev, (u8 *)hidpp_report, fields_count); |
| 197 | } else { |
| 198 | ret = hid_hw_raw_request(hdev, hidpp_report->report_id, |
| 199 | (u8 *)hidpp_report, fields_count, HID_OUTPUT_REPORT, |
| 200 | HID_REQ_SET_REPORT); |
| 201 | } |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 202 | |
| 203 | return ret == fields_count ? 0 : -1; |
| 204 | } |
| 205 | |
Benjamin Tissoires | 8c9952b | 2014-11-03 16:09:58 -0500 | [diff] [blame] | 206 | /** |
| 207 | * hidpp_send_message_sync() returns 0 in case of success, and something else |
| 208 | * in case of a failure. |
| 209 | * - If ' something else' is positive, that means that an error has been raised |
| 210 | * by the protocol itself. |
| 211 | * - If ' something else' is negative, that means that we had a classic error |
| 212 | * (-ENOMEM, -EPIPE, etc...) |
| 213 | */ |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 214 | static int hidpp_send_message_sync(struct hidpp_device *hidpp, |
| 215 | struct hidpp_report *message, |
| 216 | struct hidpp_report *response) |
| 217 | { |
| 218 | int ret; |
| 219 | |
| 220 | mutex_lock(&hidpp->send_mutex); |
| 221 | |
| 222 | hidpp->send_receive_buf = response; |
| 223 | hidpp->answer_available = false; |
| 224 | |
| 225 | /* |
| 226 | * So that we can later validate the answer when it arrives |
| 227 | * in hidpp_raw_event |
| 228 | */ |
| 229 | *response = *message; |
| 230 | |
| 231 | ret = __hidpp_send_report(hidpp->hid_dev, message); |
| 232 | |
| 233 | if (ret) { |
| 234 | dbg_hid("__hidpp_send_report returned err: %d\n", ret); |
| 235 | memset(response, 0, sizeof(struct hidpp_report)); |
| 236 | goto exit; |
| 237 | } |
| 238 | |
| 239 | if (!wait_event_timeout(hidpp->wait, hidpp->answer_available, |
| 240 | 5*HZ)) { |
| 241 | dbg_hid("%s:timeout waiting for response\n", __func__); |
| 242 | memset(response, 0, sizeof(struct hidpp_report)); |
| 243 | ret = -ETIMEDOUT; |
| 244 | } |
| 245 | |
| 246 | if (response->report_id == REPORT_ID_HIDPP_SHORT && |
Peter Wu | f677bb1 | 2014-12-16 01:50:14 +0100 | [diff] [blame] | 247 | response->rap.sub_id == HIDPP_ERROR) { |
| 248 | ret = response->rap.params[1]; |
| 249 | dbg_hid("%s:got hidpp error %02X\n", __func__, ret); |
| 250 | goto exit; |
| 251 | } |
| 252 | |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 253 | if ((response->report_id == REPORT_ID_HIDPP_LONG || |
| 254 | response->report_id == REPORT_ID_HIDPP_VERY_LONG) && |
| 255 | response->fap.feature_index == HIDPP20_ERROR) { |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 256 | ret = response->fap.params[1]; |
Peter Wu | f677bb1 | 2014-12-16 01:50:14 +0100 | [diff] [blame] | 257 | dbg_hid("%s:got hidpp 2.0 error %02X\n", __func__, ret); |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 258 | goto exit; |
| 259 | } |
| 260 | |
| 261 | exit: |
| 262 | mutex_unlock(&hidpp->send_mutex); |
| 263 | return ret; |
| 264 | |
| 265 | } |
| 266 | |
| 267 | static int hidpp_send_fap_command_sync(struct hidpp_device *hidpp, |
| 268 | u8 feat_index, u8 funcindex_clientid, u8 *params, int param_count, |
| 269 | struct hidpp_report *response) |
| 270 | { |
Dan Carpenter | 3e7830c | 2014-10-31 12:14:39 +0300 | [diff] [blame] | 271 | struct hidpp_report *message; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 272 | int ret; |
| 273 | |
| 274 | if (param_count > sizeof(message->fap.params)) |
| 275 | return -EINVAL; |
| 276 | |
Dan Carpenter | 3e7830c | 2014-10-31 12:14:39 +0300 | [diff] [blame] | 277 | message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL); |
| 278 | if (!message) |
| 279 | return -ENOMEM; |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 280 | |
| 281 | if (param_count > (HIDPP_REPORT_LONG_LENGTH - 4)) |
| 282 | message->report_id = REPORT_ID_HIDPP_VERY_LONG; |
| 283 | else |
| 284 | message->report_id = REPORT_ID_HIDPP_LONG; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 285 | message->fap.feature_index = feat_index; |
| 286 | message->fap.funcindex_clientid = funcindex_clientid; |
| 287 | memcpy(&message->fap.params, params, param_count); |
| 288 | |
| 289 | ret = hidpp_send_message_sync(hidpp, message, response); |
| 290 | kfree(message); |
| 291 | return ret; |
| 292 | } |
| 293 | |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 294 | static int hidpp_send_rap_command_sync(struct hidpp_device *hidpp_dev, |
| 295 | u8 report_id, u8 sub_id, u8 reg_address, u8 *params, int param_count, |
| 296 | struct hidpp_report *response) |
| 297 | { |
Dan Carpenter | 3e7830c | 2014-10-31 12:14:39 +0300 | [diff] [blame] | 298 | struct hidpp_report *message; |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 299 | int ret, max_count; |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 300 | |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 301 | switch (report_id) { |
| 302 | case REPORT_ID_HIDPP_SHORT: |
| 303 | max_count = HIDPP_REPORT_SHORT_LENGTH - 4; |
| 304 | break; |
| 305 | case REPORT_ID_HIDPP_LONG: |
| 306 | max_count = HIDPP_REPORT_LONG_LENGTH - 4; |
| 307 | break; |
| 308 | case REPORT_ID_HIDPP_VERY_LONG: |
| 309 | max_count = HIDPP_REPORT_VERY_LONG_LENGTH - 4; |
| 310 | break; |
| 311 | default: |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 312 | return -EINVAL; |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 313 | } |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 314 | |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 315 | if (param_count > max_count) |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 316 | return -EINVAL; |
| 317 | |
Dan Carpenter | 3e7830c | 2014-10-31 12:14:39 +0300 | [diff] [blame] | 318 | message = kzalloc(sizeof(struct hidpp_report), GFP_KERNEL); |
| 319 | if (!message) |
| 320 | return -ENOMEM; |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 321 | message->report_id = report_id; |
| 322 | message->rap.sub_id = sub_id; |
| 323 | message->rap.reg_address = reg_address; |
| 324 | memcpy(&message->rap.params, params, param_count); |
| 325 | |
| 326 | ret = hidpp_send_message_sync(hidpp_dev, message, response); |
| 327 | kfree(message); |
| 328 | return ret; |
| 329 | } |
| 330 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 331 | static void delayed_work_cb(struct work_struct *work) |
| 332 | { |
| 333 | struct hidpp_device *hidpp = container_of(work, struct hidpp_device, |
| 334 | work); |
| 335 | hidpp_connect_event(hidpp); |
| 336 | } |
| 337 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 338 | static inline bool hidpp_match_answer(struct hidpp_report *question, |
| 339 | struct hidpp_report *answer) |
| 340 | { |
| 341 | return (answer->fap.feature_index == question->fap.feature_index) && |
| 342 | (answer->fap.funcindex_clientid == question->fap.funcindex_clientid); |
| 343 | } |
| 344 | |
| 345 | static inline bool hidpp_match_error(struct hidpp_report *question, |
| 346 | struct hidpp_report *answer) |
| 347 | { |
Peter Wu | f677bb1 | 2014-12-16 01:50:14 +0100 | [diff] [blame] | 348 | return ((answer->rap.sub_id == HIDPP_ERROR) || |
| 349 | (answer->fap.feature_index == HIDPP20_ERROR)) && |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 350 | (answer->fap.funcindex_clientid == question->fap.feature_index) && |
| 351 | (answer->fap.params[0] == question->fap.funcindex_clientid); |
| 352 | } |
| 353 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 354 | static inline bool hidpp_report_is_connect_event(struct hidpp_report *report) |
| 355 | { |
| 356 | return (report->report_id == REPORT_ID_HIDPP_SHORT) && |
| 357 | (report->rap.sub_id == 0x41); |
| 358 | } |
| 359 | |
Benjamin Tissoires | a0e625f | 2014-12-11 17:39:59 -0500 | [diff] [blame] | 360 | /** |
| 361 | * hidpp_prefix_name() prefixes the current given name with "Logitech ". |
| 362 | */ |
| 363 | static void hidpp_prefix_name(char **name, int name_length) |
| 364 | { |
| 365 | #define PREFIX_LENGTH 9 /* "Logitech " */ |
| 366 | |
| 367 | int new_length; |
| 368 | char *new_name; |
| 369 | |
| 370 | if (name_length > PREFIX_LENGTH && |
| 371 | strncmp(*name, "Logitech ", PREFIX_LENGTH) == 0) |
| 372 | /* The prefix has is already in the name */ |
| 373 | return; |
| 374 | |
| 375 | new_length = PREFIX_LENGTH + name_length; |
| 376 | new_name = kzalloc(new_length, GFP_KERNEL); |
| 377 | if (!new_name) |
| 378 | return; |
| 379 | |
| 380 | snprintf(new_name, new_length, "Logitech %s", *name); |
| 381 | |
| 382 | kfree(*name); |
| 383 | |
| 384 | *name = new_name; |
| 385 | } |
| 386 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 387 | /* -------------------------------------------------------------------------- */ |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 388 | /* HIDP++ 1.0 commands */ |
| 389 | /* -------------------------------------------------------------------------- */ |
| 390 | |
| 391 | #define HIDPP_SET_REGISTER 0x80 |
| 392 | #define HIDPP_GET_REGISTER 0x81 |
| 393 | #define HIDPP_SET_LONG_REGISTER 0x82 |
| 394 | #define HIDPP_GET_LONG_REGISTER 0x83 |
| 395 | |
| 396 | #define HIDPP_REG_PAIRING_INFORMATION 0xB5 |
| 397 | #define DEVICE_NAME 0x40 |
| 398 | |
| 399 | static char *hidpp_get_unifying_name(struct hidpp_device *hidpp_dev) |
| 400 | { |
| 401 | struct hidpp_report response; |
| 402 | int ret; |
| 403 | /* hid-logitech-dj is in charge of setting the right device index */ |
| 404 | u8 params[1] = { DEVICE_NAME }; |
| 405 | char *name; |
| 406 | int len; |
| 407 | |
| 408 | ret = hidpp_send_rap_command_sync(hidpp_dev, |
| 409 | REPORT_ID_HIDPP_SHORT, |
| 410 | HIDPP_GET_LONG_REGISTER, |
| 411 | HIDPP_REG_PAIRING_INFORMATION, |
| 412 | params, 1, &response); |
| 413 | if (ret) |
| 414 | return NULL; |
| 415 | |
| 416 | len = response.rap.params[1]; |
| 417 | |
Peter Wu | 3a034a7 | 2014-12-11 13:51:19 +0100 | [diff] [blame] | 418 | if (2 + len > sizeof(response.rap.params)) |
| 419 | return NULL; |
| 420 | |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 421 | name = kzalloc(len + 1, GFP_KERNEL); |
| 422 | if (!name) |
| 423 | return NULL; |
| 424 | |
| 425 | memcpy(name, &response.rap.params[2], len); |
Benjamin Tissoires | a0e625f | 2014-12-11 17:39:59 -0500 | [diff] [blame] | 426 | |
| 427 | /* include the terminating '\0' */ |
| 428 | hidpp_prefix_name(&name, len + 1); |
| 429 | |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 430 | return name; |
| 431 | } |
| 432 | |
| 433 | /* -------------------------------------------------------------------------- */ |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 434 | /* 0x0000: Root */ |
| 435 | /* -------------------------------------------------------------------------- */ |
| 436 | |
| 437 | #define HIDPP_PAGE_ROOT 0x0000 |
| 438 | #define HIDPP_PAGE_ROOT_IDX 0x00 |
| 439 | |
| 440 | #define CMD_ROOT_GET_FEATURE 0x01 |
| 441 | #define CMD_ROOT_GET_PROTOCOL_VERSION 0x11 |
| 442 | |
| 443 | static int hidpp_root_get_feature(struct hidpp_device *hidpp, u16 feature, |
| 444 | u8 *feature_index, u8 *feature_type) |
| 445 | { |
| 446 | struct hidpp_report response; |
| 447 | int ret; |
| 448 | u8 params[2] = { feature >> 8, feature & 0x00FF }; |
| 449 | |
| 450 | ret = hidpp_send_fap_command_sync(hidpp, |
| 451 | HIDPP_PAGE_ROOT_IDX, |
| 452 | CMD_ROOT_GET_FEATURE, |
| 453 | params, 2, &response); |
| 454 | if (ret) |
| 455 | return ret; |
| 456 | |
| 457 | *feature_index = response.fap.params[0]; |
| 458 | *feature_type = response.fap.params[1]; |
| 459 | |
| 460 | return ret; |
| 461 | } |
| 462 | |
| 463 | static int hidpp_root_get_protocol_version(struct hidpp_device *hidpp) |
| 464 | { |
| 465 | struct hidpp_report response; |
| 466 | int ret; |
| 467 | |
| 468 | ret = hidpp_send_fap_command_sync(hidpp, |
| 469 | HIDPP_PAGE_ROOT_IDX, |
| 470 | CMD_ROOT_GET_PROTOCOL_VERSION, |
| 471 | NULL, 0, &response); |
| 472 | |
Benjamin Tissoires | 552f12e | 2014-11-03 16:09:59 -0500 | [diff] [blame] | 473 | if (ret == HIDPP_ERROR_INVALID_SUBID) { |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 474 | hidpp->protocol_major = 1; |
| 475 | hidpp->protocol_minor = 0; |
| 476 | return 0; |
| 477 | } |
| 478 | |
Benjamin Tissoires | 552f12e | 2014-11-03 16:09:59 -0500 | [diff] [blame] | 479 | /* the device might not be connected */ |
| 480 | if (ret == HIDPP_ERROR_RESOURCE_ERROR) |
| 481 | return -EIO; |
| 482 | |
Benjamin Tissoires | 8c9952b | 2014-11-03 16:09:58 -0500 | [diff] [blame] | 483 | if (ret > 0) { |
| 484 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", |
| 485 | __func__, ret); |
| 486 | return -EPROTO; |
| 487 | } |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 488 | if (ret) |
Benjamin Tissoires | 8c9952b | 2014-11-03 16:09:58 -0500 | [diff] [blame] | 489 | return ret; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 490 | |
| 491 | hidpp->protocol_major = response.fap.params[0]; |
| 492 | hidpp->protocol_minor = response.fap.params[1]; |
| 493 | |
| 494 | return ret; |
| 495 | } |
| 496 | |
| 497 | static bool hidpp_is_connected(struct hidpp_device *hidpp) |
| 498 | { |
| 499 | int ret; |
| 500 | |
| 501 | ret = hidpp_root_get_protocol_version(hidpp); |
| 502 | if (!ret) |
| 503 | hid_dbg(hidpp->hid_dev, "HID++ %u.%u device connected.\n", |
| 504 | hidpp->protocol_major, hidpp->protocol_minor); |
| 505 | return ret == 0; |
| 506 | } |
| 507 | |
| 508 | /* -------------------------------------------------------------------------- */ |
| 509 | /* 0x0005: GetDeviceNameType */ |
| 510 | /* -------------------------------------------------------------------------- */ |
| 511 | |
| 512 | #define HIDPP_PAGE_GET_DEVICE_NAME_TYPE 0x0005 |
| 513 | |
| 514 | #define CMD_GET_DEVICE_NAME_TYPE_GET_COUNT 0x01 |
| 515 | #define CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME 0x11 |
| 516 | #define CMD_GET_DEVICE_NAME_TYPE_GET_TYPE 0x21 |
| 517 | |
| 518 | static int hidpp_devicenametype_get_count(struct hidpp_device *hidpp, |
| 519 | u8 feature_index, u8 *nameLength) |
| 520 | { |
| 521 | struct hidpp_report response; |
| 522 | int ret; |
| 523 | |
| 524 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, |
| 525 | CMD_GET_DEVICE_NAME_TYPE_GET_COUNT, NULL, 0, &response); |
| 526 | |
Benjamin Tissoires | 8c9952b | 2014-11-03 16:09:58 -0500 | [diff] [blame] | 527 | if (ret > 0) { |
| 528 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", |
| 529 | __func__, ret); |
| 530 | return -EPROTO; |
| 531 | } |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 532 | if (ret) |
Benjamin Tissoires | 8c9952b | 2014-11-03 16:09:58 -0500 | [diff] [blame] | 533 | return ret; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 534 | |
| 535 | *nameLength = response.fap.params[0]; |
| 536 | |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | static int hidpp_devicenametype_get_device_name(struct hidpp_device *hidpp, |
| 541 | u8 feature_index, u8 char_index, char *device_name, int len_buf) |
| 542 | { |
| 543 | struct hidpp_report response; |
| 544 | int ret, i; |
| 545 | int count; |
| 546 | |
| 547 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, |
| 548 | CMD_GET_DEVICE_NAME_TYPE_GET_DEVICE_NAME, &char_index, 1, |
| 549 | &response); |
| 550 | |
Benjamin Tissoires | 8c9952b | 2014-11-03 16:09:58 -0500 | [diff] [blame] | 551 | if (ret > 0) { |
| 552 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", |
| 553 | __func__, ret); |
| 554 | return -EPROTO; |
| 555 | } |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 556 | if (ret) |
Benjamin Tissoires | 8c9952b | 2014-11-03 16:09:58 -0500 | [diff] [blame] | 557 | return ret; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 558 | |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 559 | switch (response.report_id) { |
| 560 | case REPORT_ID_HIDPP_VERY_LONG: |
| 561 | count = HIDPP_REPORT_VERY_LONG_LENGTH - 4; |
| 562 | break; |
| 563 | case REPORT_ID_HIDPP_LONG: |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 564 | count = HIDPP_REPORT_LONG_LENGTH - 4; |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 565 | break; |
| 566 | case REPORT_ID_HIDPP_SHORT: |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 567 | count = HIDPP_REPORT_SHORT_LENGTH - 4; |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 568 | break; |
| 569 | default: |
| 570 | return -EPROTO; |
| 571 | } |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 572 | |
| 573 | if (len_buf < count) |
| 574 | count = len_buf; |
| 575 | |
| 576 | for (i = 0; i < count; i++) |
| 577 | device_name[i] = response.fap.params[i]; |
| 578 | |
| 579 | return count; |
| 580 | } |
| 581 | |
Peter Wu | 02cc097 | 2014-12-11 13:51:17 +0100 | [diff] [blame] | 582 | static char *hidpp_get_device_name(struct hidpp_device *hidpp) |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 583 | { |
| 584 | u8 feature_type; |
| 585 | u8 feature_index; |
| 586 | u8 __name_length; |
| 587 | char *name; |
| 588 | unsigned index = 0; |
| 589 | int ret; |
| 590 | |
| 591 | ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_GET_DEVICE_NAME_TYPE, |
| 592 | &feature_index, &feature_type); |
| 593 | if (ret) |
Peter Wu | 02cc097 | 2014-12-11 13:51:17 +0100 | [diff] [blame] | 594 | return NULL; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 595 | |
| 596 | ret = hidpp_devicenametype_get_count(hidpp, feature_index, |
| 597 | &__name_length); |
| 598 | if (ret) |
Peter Wu | 02cc097 | 2014-12-11 13:51:17 +0100 | [diff] [blame] | 599 | return NULL; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 600 | |
| 601 | name = kzalloc(__name_length + 1, GFP_KERNEL); |
| 602 | if (!name) |
Peter Wu | 02cc097 | 2014-12-11 13:51:17 +0100 | [diff] [blame] | 603 | return NULL; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 604 | |
Peter Wu | 1430ee7 | 2014-12-11 13:51:18 +0100 | [diff] [blame] | 605 | while (index < __name_length) { |
| 606 | ret = hidpp_devicenametype_get_device_name(hidpp, |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 607 | feature_index, index, name + index, |
| 608 | __name_length - index); |
Peter Wu | 1430ee7 | 2014-12-11 13:51:18 +0100 | [diff] [blame] | 609 | if (ret <= 0) { |
| 610 | kfree(name); |
| 611 | return NULL; |
| 612 | } |
| 613 | index += ret; |
| 614 | } |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 615 | |
Benjamin Tissoires | a0e625f | 2014-12-11 17:39:59 -0500 | [diff] [blame] | 616 | /* include the terminating '\0' */ |
| 617 | hidpp_prefix_name(&name, __name_length + 1); |
| 618 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 619 | return name; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* -------------------------------------------------------------------------- */ |
Peter Hutterer | 5a2b190 | 2016-06-29 19:28:01 +1000 | [diff] [blame] | 623 | /* 0x1000: Battery level status */ |
| 624 | /* -------------------------------------------------------------------------- */ |
| 625 | |
| 626 | #define HIDPP_PAGE_BATTERY_LEVEL_STATUS 0x1000 |
| 627 | |
| 628 | #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS 0x00 |
| 629 | #define CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_CAPABILITY 0x10 |
| 630 | |
| 631 | #define EVENT_BATTERY_LEVEL_STATUS_BROADCAST 0x00 |
| 632 | |
| 633 | static int hidpp20_batterylevel_map_status_level(u8 data[3], int *level, |
| 634 | int *next_level) |
| 635 | { |
| 636 | int status; |
| 637 | int level_override; |
| 638 | |
| 639 | *level = data[0]; |
| 640 | *next_level = data[1]; |
| 641 | |
| 642 | /* When discharging, we can rely on the device reported level. |
| 643 | * For all other states the device reports level 0 (unknown). Make up |
| 644 | * a number instead |
| 645 | */ |
| 646 | switch (data[2]) { |
| 647 | case 0: /* discharging (in use) */ |
| 648 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 649 | level_override = 0; |
| 650 | break; |
| 651 | case 1: /* recharging */ |
| 652 | status = POWER_SUPPLY_STATUS_CHARGING; |
| 653 | level_override = 80; |
| 654 | break; |
| 655 | case 2: /* charge in final stage */ |
| 656 | status = POWER_SUPPLY_STATUS_CHARGING; |
| 657 | level_override = 90; |
| 658 | break; |
| 659 | case 3: /* charge complete */ |
| 660 | status = POWER_SUPPLY_STATUS_FULL; |
| 661 | level_override = 100; |
| 662 | break; |
| 663 | case 4: /* recharging below optimal speed */ |
| 664 | status = POWER_SUPPLY_STATUS_CHARGING; |
| 665 | level_override = 50; |
| 666 | break; |
| 667 | /* 5 = invalid battery type |
| 668 | 6 = thermal error |
| 669 | 7 = other charging error */ |
| 670 | default: |
| 671 | status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 672 | level_override = 0; |
| 673 | break; |
| 674 | } |
| 675 | |
| 676 | if (level_override != 0 && *level == 0) |
| 677 | *level = level_override; |
| 678 | |
| 679 | return status; |
| 680 | } |
| 681 | |
| 682 | static int hidpp20_batterylevel_get_battery_level(struct hidpp_device *hidpp, |
| 683 | u8 feature_index, |
| 684 | int *status, |
| 685 | int *level, |
| 686 | int *next_level) |
| 687 | { |
| 688 | struct hidpp_report response; |
| 689 | int ret; |
| 690 | u8 *params = (u8 *)response.fap.params; |
| 691 | |
| 692 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, |
| 693 | CMD_BATTERY_LEVEL_STATUS_GET_BATTERY_LEVEL_STATUS, |
| 694 | NULL, 0, &response); |
| 695 | if (ret > 0) { |
| 696 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", |
| 697 | __func__, ret); |
| 698 | return -EPROTO; |
| 699 | } |
| 700 | if (ret) |
| 701 | return ret; |
| 702 | |
| 703 | *status = hidpp20_batterylevel_map_status_level(params, level, |
| 704 | next_level); |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | static int hidpp20_query_battery_info(struct hidpp_device *hidpp) |
| 710 | { |
| 711 | u8 feature_type; |
| 712 | int ret; |
| 713 | int status, level, next_level; |
| 714 | |
| 715 | if (hidpp->battery.feature_index == 0) { |
| 716 | ret = hidpp_root_get_feature(hidpp, |
| 717 | HIDPP_PAGE_BATTERY_LEVEL_STATUS, |
| 718 | &hidpp->battery.feature_index, |
| 719 | &feature_type); |
| 720 | if (ret) |
| 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | ret = hidpp20_batterylevel_get_battery_level(hidpp, |
| 725 | hidpp->battery.feature_index, |
| 726 | &status, &level, &next_level); |
| 727 | if (ret) |
| 728 | return ret; |
| 729 | |
| 730 | hidpp->battery.status = status; |
| 731 | hidpp->battery.level = level; |
| 732 | |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | static int hidpp20_battery_event(struct hidpp_device *hidpp, |
| 737 | u8 *data, int size) |
| 738 | { |
| 739 | struct hidpp_report *report = (struct hidpp_report *)data; |
| 740 | int status, level, next_level; |
| 741 | bool changed; |
| 742 | |
| 743 | if (report->fap.feature_index != hidpp->battery.feature_index || |
| 744 | report->fap.funcindex_clientid != EVENT_BATTERY_LEVEL_STATUS_BROADCAST) |
| 745 | return 0; |
| 746 | |
| 747 | status = hidpp20_batterylevel_map_status_level(report->fap.params, |
| 748 | &level, &next_level); |
| 749 | |
| 750 | changed = level != hidpp->battery.level || |
| 751 | status != hidpp->battery.status; |
| 752 | |
| 753 | if (changed) { |
| 754 | hidpp->battery.level = level; |
| 755 | hidpp->battery.status = status; |
| 756 | if (hidpp->battery.ps) |
| 757 | power_supply_changed(hidpp->battery.ps); |
| 758 | } |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | static enum power_supply_property hidpp_battery_props[] = { |
| 764 | POWER_SUPPLY_PROP_STATUS, |
| 765 | POWER_SUPPLY_PROP_CAPACITY, |
Bastien Nocera | 3861e6c | 2017-03-27 16:59:22 +0200 | [diff] [blame] | 766 | POWER_SUPPLY_PROP_SCOPE, |
Peter Hutterer | 5a2b190 | 2016-06-29 19:28:01 +1000 | [diff] [blame] | 767 | }; |
| 768 | |
| 769 | static int hidpp_battery_get_property(struct power_supply *psy, |
| 770 | enum power_supply_property psp, |
| 771 | union power_supply_propval *val) |
| 772 | { |
| 773 | struct hidpp_device *hidpp = power_supply_get_drvdata(psy); |
| 774 | int ret = 0; |
| 775 | |
| 776 | switch(psp) { |
| 777 | case POWER_SUPPLY_PROP_STATUS: |
| 778 | val->intval = hidpp->battery.status; |
| 779 | break; |
| 780 | case POWER_SUPPLY_PROP_CAPACITY: |
| 781 | val->intval = hidpp->battery.level; |
| 782 | break; |
Bastien Nocera | 3861e6c | 2017-03-27 16:59:22 +0200 | [diff] [blame] | 783 | case POWER_SUPPLY_PROP_SCOPE: |
| 784 | val->intval = POWER_SUPPLY_SCOPE_DEVICE; |
| 785 | break; |
Peter Hutterer | 5a2b190 | 2016-06-29 19:28:01 +1000 | [diff] [blame] | 786 | default: |
| 787 | ret = -EINVAL; |
| 788 | break; |
| 789 | } |
| 790 | |
| 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | static int hidpp20_initialize_battery(struct hidpp_device *hidpp) |
| 795 | { |
| 796 | static atomic_t battery_no = ATOMIC_INIT(0); |
| 797 | struct power_supply_config cfg = { .drv_data = hidpp }; |
| 798 | struct power_supply_desc *desc = &hidpp->battery.desc; |
| 799 | struct hidpp_battery *battery; |
| 800 | unsigned long n; |
| 801 | int ret; |
| 802 | |
| 803 | ret = hidpp20_query_battery_info(hidpp); |
| 804 | if (ret) |
| 805 | return ret; |
| 806 | |
| 807 | battery = &hidpp->battery; |
| 808 | |
| 809 | n = atomic_inc_return(&battery_no) - 1; |
| 810 | desc->properties = hidpp_battery_props; |
| 811 | desc->num_properties = ARRAY_SIZE(hidpp_battery_props); |
| 812 | desc->get_property = hidpp_battery_get_property; |
| 813 | sprintf(battery->name, "hidpp_battery_%ld", n); |
| 814 | desc->name = battery->name; |
| 815 | desc->type = POWER_SUPPLY_TYPE_BATTERY; |
| 816 | desc->use_for_apm = 0; |
| 817 | |
| 818 | battery->ps = devm_power_supply_register(&hidpp->hid_dev->dev, |
| 819 | &battery->desc, |
| 820 | &cfg); |
| 821 | if (IS_ERR(battery->ps)) |
| 822 | return PTR_ERR(battery->ps); |
| 823 | |
| 824 | power_supply_powers(battery->ps, &hidpp->hid_dev->dev); |
| 825 | |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | static int hidpp_initialize_battery(struct hidpp_device *hidpp) |
| 830 | { |
| 831 | int ret; |
| 832 | |
Benjamin Tissoires | 680de74 | 2017-03-27 16:59:23 +0200 | [diff] [blame] | 833 | if (hidpp->battery.ps) |
| 834 | return 0; |
| 835 | |
Peter Hutterer | 5a2b190 | 2016-06-29 19:28:01 +1000 | [diff] [blame] | 836 | if (hidpp->protocol_major >= 2) { |
| 837 | ret = hidpp20_initialize_battery(hidpp); |
| 838 | if (ret == 0) |
Benjamin Tissoires | 206d7c6 | 2017-03-27 16:59:25 +0200 | [diff] [blame^] | 839 | hidpp->capabilities |= HIDPP_CAPABILITY_HIDPP20_BATTERY; |
Peter Hutterer | 5a2b190 | 2016-06-29 19:28:01 +1000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | /* -------------------------------------------------------------------------- */ |
Benjamin Tissoires | 90cdd98 | 2015-09-03 09:08:30 -0400 | [diff] [blame] | 846 | /* 0x6010: Touchpad FW items */ |
| 847 | /* -------------------------------------------------------------------------- */ |
| 848 | |
| 849 | #define HIDPP_PAGE_TOUCHPAD_FW_ITEMS 0x6010 |
| 850 | |
| 851 | #define CMD_TOUCHPAD_FW_ITEMS_SET 0x10 |
| 852 | |
| 853 | struct hidpp_touchpad_fw_items { |
| 854 | uint8_t presence; |
| 855 | uint8_t desired_state; |
| 856 | uint8_t state; |
| 857 | uint8_t persistent; |
| 858 | }; |
| 859 | |
| 860 | /** |
| 861 | * send a set state command to the device by reading the current items->state |
| 862 | * field. items is then filled with the current state. |
| 863 | */ |
| 864 | static int hidpp_touchpad_fw_items_set(struct hidpp_device *hidpp, |
| 865 | u8 feature_index, |
| 866 | struct hidpp_touchpad_fw_items *items) |
| 867 | { |
| 868 | struct hidpp_report response; |
| 869 | int ret; |
| 870 | u8 *params = (u8 *)response.fap.params; |
| 871 | |
| 872 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, |
| 873 | CMD_TOUCHPAD_FW_ITEMS_SET, &items->state, 1, &response); |
| 874 | |
| 875 | if (ret > 0) { |
| 876 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", |
| 877 | __func__, ret); |
| 878 | return -EPROTO; |
| 879 | } |
| 880 | if (ret) |
| 881 | return ret; |
| 882 | |
| 883 | items->presence = params[0]; |
| 884 | items->desired_state = params[1]; |
| 885 | items->state = params[2]; |
| 886 | items->persistent = params[3]; |
| 887 | |
| 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | /* -------------------------------------------------------------------------- */ |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 892 | /* 0x6100: TouchPadRawXY */ |
| 893 | /* -------------------------------------------------------------------------- */ |
| 894 | |
| 895 | #define HIDPP_PAGE_TOUCHPAD_RAW_XY 0x6100 |
| 896 | |
| 897 | #define CMD_TOUCHPAD_GET_RAW_INFO 0x01 |
Benjamin Tissoires | 586bdc4 | 2014-09-30 13:18:33 -0400 | [diff] [blame] | 898 | #define CMD_TOUCHPAD_SET_RAW_REPORT_STATE 0x21 |
| 899 | |
| 900 | #define EVENT_TOUCHPAD_RAW_XY 0x00 |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 901 | |
| 902 | #define TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT 0x01 |
| 903 | #define TOUCHPAD_RAW_XY_ORIGIN_UPPER_LEFT 0x03 |
| 904 | |
| 905 | struct hidpp_touchpad_raw_info { |
| 906 | u16 x_size; |
| 907 | u16 y_size; |
| 908 | u8 z_range; |
| 909 | u8 area_range; |
| 910 | u8 timestamp_unit; |
| 911 | u8 maxcontacts; |
| 912 | u8 origin; |
| 913 | u16 res; |
| 914 | }; |
| 915 | |
| 916 | struct hidpp_touchpad_raw_xy_finger { |
| 917 | u8 contact_type; |
| 918 | u8 contact_status; |
| 919 | u16 x; |
| 920 | u16 y; |
| 921 | u8 z; |
| 922 | u8 area; |
| 923 | u8 finger_id; |
| 924 | }; |
| 925 | |
| 926 | struct hidpp_touchpad_raw_xy { |
| 927 | u16 timestamp; |
| 928 | struct hidpp_touchpad_raw_xy_finger fingers[2]; |
| 929 | u8 spurious_flag; |
| 930 | u8 end_of_frame; |
| 931 | u8 finger_count; |
| 932 | u8 button; |
| 933 | }; |
| 934 | |
| 935 | static int hidpp_touchpad_get_raw_info(struct hidpp_device *hidpp, |
| 936 | u8 feature_index, struct hidpp_touchpad_raw_info *raw_info) |
| 937 | { |
| 938 | struct hidpp_report response; |
| 939 | int ret; |
| 940 | u8 *params = (u8 *)response.fap.params; |
| 941 | |
| 942 | ret = hidpp_send_fap_command_sync(hidpp, feature_index, |
| 943 | CMD_TOUCHPAD_GET_RAW_INFO, NULL, 0, &response); |
| 944 | |
Benjamin Tissoires | 8c9952b | 2014-11-03 16:09:58 -0500 | [diff] [blame] | 945 | if (ret > 0) { |
| 946 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", |
| 947 | __func__, ret); |
| 948 | return -EPROTO; |
| 949 | } |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 950 | if (ret) |
Benjamin Tissoires | 8c9952b | 2014-11-03 16:09:58 -0500 | [diff] [blame] | 951 | return ret; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 952 | |
| 953 | raw_info->x_size = get_unaligned_be16(¶ms[0]); |
| 954 | raw_info->y_size = get_unaligned_be16(¶ms[2]); |
| 955 | raw_info->z_range = params[4]; |
| 956 | raw_info->area_range = params[5]; |
| 957 | raw_info->maxcontacts = params[7]; |
| 958 | raw_info->origin = params[8]; |
| 959 | /* res is given in unit per inch */ |
| 960 | raw_info->res = get_unaligned_be16(¶ms[13]) * 2 / 51; |
| 961 | |
| 962 | return ret; |
| 963 | } |
| 964 | |
Benjamin Tissoires | 586bdc4 | 2014-09-30 13:18:33 -0400 | [diff] [blame] | 965 | static int hidpp_touchpad_set_raw_report_state(struct hidpp_device *hidpp_dev, |
| 966 | u8 feature_index, bool send_raw_reports, |
| 967 | bool sensor_enhanced_settings) |
| 968 | { |
| 969 | struct hidpp_report response; |
| 970 | |
| 971 | /* |
| 972 | * Params: |
| 973 | * bit 0 - enable raw |
| 974 | * bit 1 - 16bit Z, no area |
| 975 | * bit 2 - enhanced sensitivity |
| 976 | * bit 3 - width, height (4 bits each) instead of area |
| 977 | * bit 4 - send raw + gestures (degrades smoothness) |
| 978 | * remaining bits - reserved |
| 979 | */ |
| 980 | u8 params = send_raw_reports | (sensor_enhanced_settings << 2); |
| 981 | |
| 982 | return hidpp_send_fap_command_sync(hidpp_dev, feature_index, |
| 983 | CMD_TOUCHPAD_SET_RAW_REPORT_STATE, ¶ms, 1, &response); |
| 984 | } |
| 985 | |
| 986 | static void hidpp_touchpad_touch_event(u8 *data, |
| 987 | struct hidpp_touchpad_raw_xy_finger *finger) |
| 988 | { |
| 989 | u8 x_m = data[0] << 2; |
| 990 | u8 y_m = data[2] << 2; |
| 991 | |
| 992 | finger->x = x_m << 6 | data[1]; |
| 993 | finger->y = y_m << 6 | data[3]; |
| 994 | |
| 995 | finger->contact_type = data[0] >> 6; |
| 996 | finger->contact_status = data[2] >> 6; |
| 997 | |
| 998 | finger->z = data[4]; |
| 999 | finger->area = data[5]; |
| 1000 | finger->finger_id = data[6] >> 4; |
| 1001 | } |
| 1002 | |
| 1003 | static void hidpp_touchpad_raw_xy_event(struct hidpp_device *hidpp_dev, |
| 1004 | u8 *data, struct hidpp_touchpad_raw_xy *raw_xy) |
| 1005 | { |
| 1006 | memset(raw_xy, 0, sizeof(struct hidpp_touchpad_raw_xy)); |
| 1007 | raw_xy->end_of_frame = data[8] & 0x01; |
| 1008 | raw_xy->spurious_flag = (data[8] >> 1) & 0x01; |
| 1009 | raw_xy->finger_count = data[15] & 0x0f; |
| 1010 | raw_xy->button = (data[8] >> 2) & 0x01; |
| 1011 | |
| 1012 | if (raw_xy->finger_count) { |
| 1013 | hidpp_touchpad_touch_event(&data[2], &raw_xy->fingers[0]); |
| 1014 | hidpp_touchpad_touch_event(&data[9], &raw_xy->fingers[1]); |
| 1015 | } |
| 1016 | } |
| 1017 | |
Edwin Velds | ff21a63 | 2016-01-11 00:25:15 +0100 | [diff] [blame] | 1018 | /* -------------------------------------------------------------------------- */ |
| 1019 | /* 0x8123: Force feedback support */ |
| 1020 | /* -------------------------------------------------------------------------- */ |
| 1021 | |
| 1022 | #define HIDPP_FF_GET_INFO 0x01 |
| 1023 | #define HIDPP_FF_RESET_ALL 0x11 |
| 1024 | #define HIDPP_FF_DOWNLOAD_EFFECT 0x21 |
| 1025 | #define HIDPP_FF_SET_EFFECT_STATE 0x31 |
| 1026 | #define HIDPP_FF_DESTROY_EFFECT 0x41 |
| 1027 | #define HIDPP_FF_GET_APERTURE 0x51 |
| 1028 | #define HIDPP_FF_SET_APERTURE 0x61 |
| 1029 | #define HIDPP_FF_GET_GLOBAL_GAINS 0x71 |
| 1030 | #define HIDPP_FF_SET_GLOBAL_GAINS 0x81 |
| 1031 | |
| 1032 | #define HIDPP_FF_EFFECT_STATE_GET 0x00 |
| 1033 | #define HIDPP_FF_EFFECT_STATE_STOP 0x01 |
| 1034 | #define HIDPP_FF_EFFECT_STATE_PLAY 0x02 |
| 1035 | #define HIDPP_FF_EFFECT_STATE_PAUSE 0x03 |
| 1036 | |
| 1037 | #define HIDPP_FF_EFFECT_CONSTANT 0x00 |
| 1038 | #define HIDPP_FF_EFFECT_PERIODIC_SINE 0x01 |
| 1039 | #define HIDPP_FF_EFFECT_PERIODIC_SQUARE 0x02 |
| 1040 | #define HIDPP_FF_EFFECT_PERIODIC_TRIANGLE 0x03 |
| 1041 | #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP 0x04 |
| 1042 | #define HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN 0x05 |
| 1043 | #define HIDPP_FF_EFFECT_SPRING 0x06 |
| 1044 | #define HIDPP_FF_EFFECT_DAMPER 0x07 |
| 1045 | #define HIDPP_FF_EFFECT_FRICTION 0x08 |
| 1046 | #define HIDPP_FF_EFFECT_INERTIA 0x09 |
| 1047 | #define HIDPP_FF_EFFECT_RAMP 0x0A |
| 1048 | |
| 1049 | #define HIDPP_FF_EFFECT_AUTOSTART 0x80 |
| 1050 | |
| 1051 | #define HIDPP_FF_EFFECTID_NONE -1 |
| 1052 | #define HIDPP_FF_EFFECTID_AUTOCENTER -2 |
| 1053 | |
| 1054 | #define HIDPP_FF_MAX_PARAMS 20 |
| 1055 | #define HIDPP_FF_RESERVED_SLOTS 1 |
| 1056 | |
| 1057 | struct hidpp_ff_private_data { |
| 1058 | struct hidpp_device *hidpp; |
| 1059 | u8 feature_index; |
| 1060 | u8 version; |
| 1061 | u16 gain; |
| 1062 | s16 range; |
| 1063 | u8 slot_autocenter; |
| 1064 | u8 num_effects; |
| 1065 | int *effect_ids; |
| 1066 | struct workqueue_struct *wq; |
| 1067 | atomic_t workqueue_size; |
| 1068 | }; |
| 1069 | |
| 1070 | struct hidpp_ff_work_data { |
| 1071 | struct work_struct work; |
| 1072 | struct hidpp_ff_private_data *data; |
| 1073 | int effect_id; |
| 1074 | u8 command; |
| 1075 | u8 params[HIDPP_FF_MAX_PARAMS]; |
| 1076 | u8 size; |
| 1077 | }; |
| 1078 | |
| 1079 | static const signed short hiddpp_ff_effects[] = { |
| 1080 | FF_CONSTANT, |
| 1081 | FF_PERIODIC, |
| 1082 | FF_SINE, |
| 1083 | FF_SQUARE, |
| 1084 | FF_SAW_UP, |
| 1085 | FF_SAW_DOWN, |
| 1086 | FF_TRIANGLE, |
| 1087 | FF_SPRING, |
| 1088 | FF_DAMPER, |
| 1089 | FF_AUTOCENTER, |
| 1090 | FF_GAIN, |
| 1091 | -1 |
| 1092 | }; |
| 1093 | |
| 1094 | static const signed short hiddpp_ff_effects_v2[] = { |
| 1095 | FF_RAMP, |
| 1096 | FF_FRICTION, |
| 1097 | FF_INERTIA, |
| 1098 | -1 |
| 1099 | }; |
| 1100 | |
| 1101 | static const u8 HIDPP_FF_CONDITION_CMDS[] = { |
| 1102 | HIDPP_FF_EFFECT_SPRING, |
| 1103 | HIDPP_FF_EFFECT_FRICTION, |
| 1104 | HIDPP_FF_EFFECT_DAMPER, |
| 1105 | HIDPP_FF_EFFECT_INERTIA |
| 1106 | }; |
| 1107 | |
| 1108 | static const char *HIDPP_FF_CONDITION_NAMES[] = { |
| 1109 | "spring", |
| 1110 | "friction", |
| 1111 | "damper", |
| 1112 | "inertia" |
| 1113 | }; |
| 1114 | |
| 1115 | |
| 1116 | static u8 hidpp_ff_find_effect(struct hidpp_ff_private_data *data, int effect_id) |
| 1117 | { |
| 1118 | int i; |
| 1119 | |
| 1120 | for (i = 0; i < data->num_effects; i++) |
| 1121 | if (data->effect_ids[i] == effect_id) |
| 1122 | return i+1; |
| 1123 | |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | static void hidpp_ff_work_handler(struct work_struct *w) |
| 1128 | { |
| 1129 | struct hidpp_ff_work_data *wd = container_of(w, struct hidpp_ff_work_data, work); |
| 1130 | struct hidpp_ff_private_data *data = wd->data; |
| 1131 | struct hidpp_report response; |
| 1132 | u8 slot; |
| 1133 | int ret; |
| 1134 | |
| 1135 | /* add slot number if needed */ |
| 1136 | switch (wd->effect_id) { |
| 1137 | case HIDPP_FF_EFFECTID_AUTOCENTER: |
| 1138 | wd->params[0] = data->slot_autocenter; |
| 1139 | break; |
| 1140 | case HIDPP_FF_EFFECTID_NONE: |
| 1141 | /* leave slot as zero */ |
| 1142 | break; |
| 1143 | default: |
| 1144 | /* find current slot for effect */ |
| 1145 | wd->params[0] = hidpp_ff_find_effect(data, wd->effect_id); |
| 1146 | break; |
| 1147 | } |
| 1148 | |
| 1149 | /* send command and wait for reply */ |
| 1150 | ret = hidpp_send_fap_command_sync(data->hidpp, data->feature_index, |
| 1151 | wd->command, wd->params, wd->size, &response); |
| 1152 | |
| 1153 | if (ret) { |
| 1154 | hid_err(data->hidpp->hid_dev, "Failed to send command to device!\n"); |
| 1155 | goto out; |
| 1156 | } |
| 1157 | |
| 1158 | /* parse return data */ |
| 1159 | switch (wd->command) { |
| 1160 | case HIDPP_FF_DOWNLOAD_EFFECT: |
| 1161 | slot = response.fap.params[0]; |
| 1162 | if (slot > 0 && slot <= data->num_effects) { |
| 1163 | if (wd->effect_id >= 0) |
| 1164 | /* regular effect uploaded */ |
| 1165 | data->effect_ids[slot-1] = wd->effect_id; |
| 1166 | else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER) |
| 1167 | /* autocenter spring uploaded */ |
| 1168 | data->slot_autocenter = slot; |
| 1169 | } |
| 1170 | break; |
| 1171 | case HIDPP_FF_DESTROY_EFFECT: |
| 1172 | if (wd->effect_id >= 0) |
| 1173 | /* regular effect destroyed */ |
| 1174 | data->effect_ids[wd->params[0]-1] = -1; |
| 1175 | else if (wd->effect_id >= HIDPP_FF_EFFECTID_AUTOCENTER) |
| 1176 | /* autocenter spring destoyed */ |
| 1177 | data->slot_autocenter = 0; |
| 1178 | break; |
| 1179 | case HIDPP_FF_SET_GLOBAL_GAINS: |
| 1180 | data->gain = (wd->params[0] << 8) + wd->params[1]; |
| 1181 | break; |
| 1182 | case HIDPP_FF_SET_APERTURE: |
| 1183 | data->range = (wd->params[0] << 8) + wd->params[1]; |
| 1184 | break; |
| 1185 | default: |
| 1186 | /* no action needed */ |
| 1187 | break; |
| 1188 | } |
| 1189 | |
| 1190 | out: |
| 1191 | atomic_dec(&data->workqueue_size); |
| 1192 | kfree(wd); |
| 1193 | } |
| 1194 | |
| 1195 | static int hidpp_ff_queue_work(struct hidpp_ff_private_data *data, int effect_id, u8 command, u8 *params, u8 size) |
| 1196 | { |
| 1197 | struct hidpp_ff_work_data *wd = kzalloc(sizeof(*wd), GFP_KERNEL); |
| 1198 | int s; |
| 1199 | |
| 1200 | if (!wd) |
| 1201 | return -ENOMEM; |
| 1202 | |
| 1203 | INIT_WORK(&wd->work, hidpp_ff_work_handler); |
| 1204 | |
| 1205 | wd->data = data; |
| 1206 | wd->effect_id = effect_id; |
| 1207 | wd->command = command; |
| 1208 | wd->size = size; |
| 1209 | memcpy(wd->params, params, size); |
| 1210 | |
| 1211 | atomic_inc(&data->workqueue_size); |
| 1212 | queue_work(data->wq, &wd->work); |
| 1213 | |
| 1214 | /* warn about excessive queue size */ |
| 1215 | s = atomic_read(&data->workqueue_size); |
| 1216 | if (s >= 20 && s % 20 == 0) |
| 1217 | hid_warn(data->hidpp->hid_dev, "Force feedback command queue contains %d commands, causing substantial delays!", s); |
| 1218 | |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | static int hidpp_ff_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old) |
| 1223 | { |
| 1224 | struct hidpp_ff_private_data *data = dev->ff->private; |
| 1225 | u8 params[20]; |
| 1226 | u8 size; |
| 1227 | int force; |
| 1228 | |
| 1229 | /* set common parameters */ |
| 1230 | params[2] = effect->replay.length >> 8; |
| 1231 | params[3] = effect->replay.length & 255; |
| 1232 | params[4] = effect->replay.delay >> 8; |
| 1233 | params[5] = effect->replay.delay & 255; |
| 1234 | |
| 1235 | switch (effect->type) { |
| 1236 | case FF_CONSTANT: |
| 1237 | force = (effect->u.constant.level * fixp_sin16((effect->direction * 360) >> 16)) >> 15; |
| 1238 | params[1] = HIDPP_FF_EFFECT_CONSTANT; |
| 1239 | params[6] = force >> 8; |
| 1240 | params[7] = force & 255; |
| 1241 | params[8] = effect->u.constant.envelope.attack_level >> 7; |
| 1242 | params[9] = effect->u.constant.envelope.attack_length >> 8; |
| 1243 | params[10] = effect->u.constant.envelope.attack_length & 255; |
| 1244 | params[11] = effect->u.constant.envelope.fade_level >> 7; |
| 1245 | params[12] = effect->u.constant.envelope.fade_length >> 8; |
| 1246 | params[13] = effect->u.constant.envelope.fade_length & 255; |
| 1247 | size = 14; |
| 1248 | dbg_hid("Uploading constant force level=%d in dir %d = %d\n", |
| 1249 | effect->u.constant.level, |
| 1250 | effect->direction, force); |
| 1251 | dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n", |
| 1252 | effect->u.constant.envelope.attack_level, |
| 1253 | effect->u.constant.envelope.attack_length, |
| 1254 | effect->u.constant.envelope.fade_level, |
| 1255 | effect->u.constant.envelope.fade_length); |
| 1256 | break; |
| 1257 | case FF_PERIODIC: |
| 1258 | { |
| 1259 | switch (effect->u.periodic.waveform) { |
| 1260 | case FF_SINE: |
| 1261 | params[1] = HIDPP_FF_EFFECT_PERIODIC_SINE; |
| 1262 | break; |
| 1263 | case FF_SQUARE: |
| 1264 | params[1] = HIDPP_FF_EFFECT_PERIODIC_SQUARE; |
| 1265 | break; |
| 1266 | case FF_SAW_UP: |
| 1267 | params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHUP; |
| 1268 | break; |
| 1269 | case FF_SAW_DOWN: |
| 1270 | params[1] = HIDPP_FF_EFFECT_PERIODIC_SAWTOOTHDOWN; |
| 1271 | break; |
| 1272 | case FF_TRIANGLE: |
| 1273 | params[1] = HIDPP_FF_EFFECT_PERIODIC_TRIANGLE; |
| 1274 | break; |
| 1275 | default: |
| 1276 | hid_err(data->hidpp->hid_dev, "Unexpected periodic waveform type %i!\n", effect->u.periodic.waveform); |
| 1277 | return -EINVAL; |
| 1278 | } |
| 1279 | force = (effect->u.periodic.magnitude * fixp_sin16((effect->direction * 360) >> 16)) >> 15; |
| 1280 | params[6] = effect->u.periodic.magnitude >> 8; |
| 1281 | params[7] = effect->u.periodic.magnitude & 255; |
| 1282 | params[8] = effect->u.periodic.offset >> 8; |
| 1283 | params[9] = effect->u.periodic.offset & 255; |
| 1284 | params[10] = effect->u.periodic.period >> 8; |
| 1285 | params[11] = effect->u.periodic.period & 255; |
| 1286 | params[12] = effect->u.periodic.phase >> 8; |
| 1287 | params[13] = effect->u.periodic.phase & 255; |
| 1288 | params[14] = effect->u.periodic.envelope.attack_level >> 7; |
| 1289 | params[15] = effect->u.periodic.envelope.attack_length >> 8; |
| 1290 | params[16] = effect->u.periodic.envelope.attack_length & 255; |
| 1291 | params[17] = effect->u.periodic.envelope.fade_level >> 7; |
| 1292 | params[18] = effect->u.periodic.envelope.fade_length >> 8; |
| 1293 | params[19] = effect->u.periodic.envelope.fade_length & 255; |
| 1294 | size = 20; |
| 1295 | dbg_hid("Uploading periodic force mag=%d/dir=%d, offset=%d, period=%d ms, phase=%d\n", |
| 1296 | effect->u.periodic.magnitude, effect->direction, |
| 1297 | effect->u.periodic.offset, |
| 1298 | effect->u.periodic.period, |
| 1299 | effect->u.periodic.phase); |
| 1300 | dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n", |
| 1301 | effect->u.periodic.envelope.attack_level, |
| 1302 | effect->u.periodic.envelope.attack_length, |
| 1303 | effect->u.periodic.envelope.fade_level, |
| 1304 | effect->u.periodic.envelope.fade_length); |
| 1305 | break; |
| 1306 | } |
| 1307 | case FF_RAMP: |
| 1308 | params[1] = HIDPP_FF_EFFECT_RAMP; |
| 1309 | force = (effect->u.ramp.start_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15; |
| 1310 | params[6] = force >> 8; |
| 1311 | params[7] = force & 255; |
| 1312 | force = (effect->u.ramp.end_level * fixp_sin16((effect->direction * 360) >> 16)) >> 15; |
| 1313 | params[8] = force >> 8; |
| 1314 | params[9] = force & 255; |
| 1315 | params[10] = effect->u.ramp.envelope.attack_level >> 7; |
| 1316 | params[11] = effect->u.ramp.envelope.attack_length >> 8; |
| 1317 | params[12] = effect->u.ramp.envelope.attack_length & 255; |
| 1318 | params[13] = effect->u.ramp.envelope.fade_level >> 7; |
| 1319 | params[14] = effect->u.ramp.envelope.fade_length >> 8; |
| 1320 | params[15] = effect->u.ramp.envelope.fade_length & 255; |
| 1321 | size = 16; |
| 1322 | dbg_hid("Uploading ramp force level=%d -> %d in dir %d = %d\n", |
| 1323 | effect->u.ramp.start_level, |
| 1324 | effect->u.ramp.end_level, |
| 1325 | effect->direction, force); |
| 1326 | dbg_hid(" envelope attack=(%d, %d ms) fade=(%d, %d ms)\n", |
| 1327 | effect->u.ramp.envelope.attack_level, |
| 1328 | effect->u.ramp.envelope.attack_length, |
| 1329 | effect->u.ramp.envelope.fade_level, |
| 1330 | effect->u.ramp.envelope.fade_length); |
| 1331 | break; |
| 1332 | case FF_FRICTION: |
| 1333 | case FF_INERTIA: |
| 1334 | case FF_SPRING: |
| 1335 | case FF_DAMPER: |
| 1336 | params[1] = HIDPP_FF_CONDITION_CMDS[effect->type - FF_SPRING]; |
| 1337 | params[6] = effect->u.condition[0].left_saturation >> 9; |
| 1338 | params[7] = (effect->u.condition[0].left_saturation >> 1) & 255; |
| 1339 | params[8] = effect->u.condition[0].left_coeff >> 8; |
| 1340 | params[9] = effect->u.condition[0].left_coeff & 255; |
| 1341 | params[10] = effect->u.condition[0].deadband >> 9; |
| 1342 | params[11] = (effect->u.condition[0].deadband >> 1) & 255; |
| 1343 | params[12] = effect->u.condition[0].center >> 8; |
| 1344 | params[13] = effect->u.condition[0].center & 255; |
| 1345 | params[14] = effect->u.condition[0].right_coeff >> 8; |
| 1346 | params[15] = effect->u.condition[0].right_coeff & 255; |
| 1347 | params[16] = effect->u.condition[0].right_saturation >> 9; |
| 1348 | params[17] = (effect->u.condition[0].right_saturation >> 1) & 255; |
| 1349 | size = 18; |
| 1350 | dbg_hid("Uploading %s force left coeff=%d, left sat=%d, right coeff=%d, right sat=%d\n", |
| 1351 | HIDPP_FF_CONDITION_NAMES[effect->type - FF_SPRING], |
| 1352 | effect->u.condition[0].left_coeff, |
| 1353 | effect->u.condition[0].left_saturation, |
| 1354 | effect->u.condition[0].right_coeff, |
| 1355 | effect->u.condition[0].right_saturation); |
| 1356 | dbg_hid(" deadband=%d, center=%d\n", |
| 1357 | effect->u.condition[0].deadband, |
| 1358 | effect->u.condition[0].center); |
| 1359 | break; |
| 1360 | default: |
| 1361 | hid_err(data->hidpp->hid_dev, "Unexpected force type %i!\n", effect->type); |
| 1362 | return -EINVAL; |
| 1363 | } |
| 1364 | |
| 1365 | return hidpp_ff_queue_work(data, effect->id, HIDPP_FF_DOWNLOAD_EFFECT, params, size); |
| 1366 | } |
| 1367 | |
| 1368 | static int hidpp_ff_playback(struct input_dev *dev, int effect_id, int value) |
| 1369 | { |
| 1370 | struct hidpp_ff_private_data *data = dev->ff->private; |
| 1371 | u8 params[2]; |
| 1372 | |
| 1373 | params[1] = value ? HIDPP_FF_EFFECT_STATE_PLAY : HIDPP_FF_EFFECT_STATE_STOP; |
| 1374 | |
| 1375 | dbg_hid("St%sing playback of effect %d.\n", value?"art":"opp", effect_id); |
| 1376 | |
| 1377 | return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_SET_EFFECT_STATE, params, ARRAY_SIZE(params)); |
| 1378 | } |
| 1379 | |
| 1380 | static int hidpp_ff_erase_effect(struct input_dev *dev, int effect_id) |
| 1381 | { |
| 1382 | struct hidpp_ff_private_data *data = dev->ff->private; |
| 1383 | u8 slot = 0; |
| 1384 | |
| 1385 | dbg_hid("Erasing effect %d.\n", effect_id); |
| 1386 | |
| 1387 | return hidpp_ff_queue_work(data, effect_id, HIDPP_FF_DESTROY_EFFECT, &slot, 1); |
| 1388 | } |
| 1389 | |
| 1390 | static void hidpp_ff_set_autocenter(struct input_dev *dev, u16 magnitude) |
| 1391 | { |
| 1392 | struct hidpp_ff_private_data *data = dev->ff->private; |
| 1393 | u8 params[18]; |
| 1394 | |
| 1395 | dbg_hid("Setting autocenter to %d.\n", magnitude); |
| 1396 | |
| 1397 | /* start a standard spring effect */ |
| 1398 | params[1] = HIDPP_FF_EFFECT_SPRING | HIDPP_FF_EFFECT_AUTOSTART; |
| 1399 | /* zero delay and duration */ |
| 1400 | params[2] = params[3] = params[4] = params[5] = 0; |
| 1401 | /* set coeff to 25% of saturation */ |
| 1402 | params[8] = params[14] = magnitude >> 11; |
| 1403 | params[9] = params[15] = (magnitude >> 3) & 255; |
| 1404 | params[6] = params[16] = magnitude >> 9; |
| 1405 | params[7] = params[17] = (magnitude >> 1) & 255; |
| 1406 | /* zero deadband and center */ |
| 1407 | params[10] = params[11] = params[12] = params[13] = 0; |
| 1408 | |
| 1409 | hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_AUTOCENTER, HIDPP_FF_DOWNLOAD_EFFECT, params, ARRAY_SIZE(params)); |
| 1410 | } |
| 1411 | |
| 1412 | static void hidpp_ff_set_gain(struct input_dev *dev, u16 gain) |
| 1413 | { |
| 1414 | struct hidpp_ff_private_data *data = dev->ff->private; |
| 1415 | u8 params[4]; |
| 1416 | |
| 1417 | dbg_hid("Setting gain to %d.\n", gain); |
| 1418 | |
| 1419 | params[0] = gain >> 8; |
| 1420 | params[1] = gain & 255; |
| 1421 | params[2] = 0; /* no boost */ |
| 1422 | params[3] = 0; |
| 1423 | |
| 1424 | hidpp_ff_queue_work(data, HIDPP_FF_EFFECTID_NONE, HIDPP_FF_SET_GLOBAL_GAINS, params, ARRAY_SIZE(params)); |
| 1425 | } |
| 1426 | |
| 1427 | static ssize_t hidpp_ff_range_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 1428 | { |
| 1429 | struct hid_device *hid = to_hid_device(dev); |
| 1430 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); |
| 1431 | struct input_dev *idev = hidinput->input; |
| 1432 | struct hidpp_ff_private_data *data = idev->ff->private; |
| 1433 | |
| 1434 | return scnprintf(buf, PAGE_SIZE, "%u\n", data->range); |
| 1435 | } |
| 1436 | |
| 1437 | static ssize_t hidpp_ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 1438 | { |
| 1439 | struct hid_device *hid = to_hid_device(dev); |
| 1440 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); |
| 1441 | struct input_dev *idev = hidinput->input; |
| 1442 | struct hidpp_ff_private_data *data = idev->ff->private; |
| 1443 | u8 params[2]; |
| 1444 | int range = simple_strtoul(buf, NULL, 10); |
| 1445 | |
| 1446 | range = clamp(range, 180, 900); |
| 1447 | |
| 1448 | params[0] = range >> 8; |
| 1449 | params[1] = range & 0x00FF; |
| 1450 | |
| 1451 | hidpp_ff_queue_work(data, -1, HIDPP_FF_SET_APERTURE, params, ARRAY_SIZE(params)); |
| 1452 | |
| 1453 | return count; |
| 1454 | } |
| 1455 | |
| 1456 | static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, hidpp_ff_range_show, hidpp_ff_range_store); |
| 1457 | |
| 1458 | static void hidpp_ff_destroy(struct ff_device *ff) |
| 1459 | { |
| 1460 | struct hidpp_ff_private_data *data = ff->private; |
| 1461 | |
| 1462 | kfree(data->effect_ids); |
| 1463 | } |
| 1464 | |
Jiri Kosina | af2e628 | 2016-01-28 14:28:39 +0100 | [diff] [blame] | 1465 | static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index) |
Edwin Velds | ff21a63 | 2016-01-11 00:25:15 +0100 | [diff] [blame] | 1466 | { |
| 1467 | struct hid_device *hid = hidpp->hid_dev; |
| 1468 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); |
| 1469 | struct input_dev *dev = hidinput->input; |
| 1470 | const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor); |
| 1471 | const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice); |
| 1472 | struct ff_device *ff; |
| 1473 | struct hidpp_report response; |
| 1474 | struct hidpp_ff_private_data *data; |
| 1475 | int error, j, num_slots; |
| 1476 | u8 version; |
| 1477 | |
| 1478 | if (!dev) { |
| 1479 | hid_err(hid, "Struct input_dev not set!\n"); |
| 1480 | return -EINVAL; |
| 1481 | } |
| 1482 | |
| 1483 | /* Get firmware release */ |
| 1484 | version = bcdDevice & 255; |
| 1485 | |
| 1486 | /* Set supported force feedback capabilities */ |
| 1487 | for (j = 0; hiddpp_ff_effects[j] >= 0; j++) |
| 1488 | set_bit(hiddpp_ff_effects[j], dev->ffbit); |
| 1489 | if (version > 1) |
| 1490 | for (j = 0; hiddpp_ff_effects_v2[j] >= 0; j++) |
| 1491 | set_bit(hiddpp_ff_effects_v2[j], dev->ffbit); |
| 1492 | |
| 1493 | /* Read number of slots available in device */ |
| 1494 | error = hidpp_send_fap_command_sync(hidpp, feature_index, |
| 1495 | HIDPP_FF_GET_INFO, NULL, 0, &response); |
| 1496 | if (error) { |
| 1497 | if (error < 0) |
| 1498 | return error; |
| 1499 | hid_err(hidpp->hid_dev, "%s: received protocol error 0x%02x\n", |
| 1500 | __func__, error); |
| 1501 | return -EPROTO; |
| 1502 | } |
| 1503 | |
| 1504 | num_slots = response.fap.params[0] - HIDPP_FF_RESERVED_SLOTS; |
| 1505 | |
| 1506 | error = input_ff_create(dev, num_slots); |
| 1507 | |
| 1508 | if (error) { |
| 1509 | hid_err(dev, "Failed to create FF device!\n"); |
| 1510 | return error; |
| 1511 | } |
| 1512 | |
| 1513 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 1514 | if (!data) |
| 1515 | return -ENOMEM; |
| 1516 | data->effect_ids = kcalloc(num_slots, sizeof(int), GFP_KERNEL); |
| 1517 | if (!data->effect_ids) { |
| 1518 | kfree(data); |
| 1519 | return -ENOMEM; |
| 1520 | } |
| 1521 | data->hidpp = hidpp; |
| 1522 | data->feature_index = feature_index; |
| 1523 | data->version = version; |
| 1524 | data->slot_autocenter = 0; |
| 1525 | data->num_effects = num_slots; |
| 1526 | for (j = 0; j < num_slots; j++) |
| 1527 | data->effect_ids[j] = -1; |
| 1528 | |
| 1529 | ff = dev->ff; |
| 1530 | ff->private = data; |
| 1531 | |
| 1532 | ff->upload = hidpp_ff_upload_effect; |
| 1533 | ff->erase = hidpp_ff_erase_effect; |
| 1534 | ff->playback = hidpp_ff_playback; |
| 1535 | ff->set_gain = hidpp_ff_set_gain; |
| 1536 | ff->set_autocenter = hidpp_ff_set_autocenter; |
| 1537 | ff->destroy = hidpp_ff_destroy; |
| 1538 | |
| 1539 | |
| 1540 | /* reset all forces */ |
| 1541 | error = hidpp_send_fap_command_sync(hidpp, feature_index, |
| 1542 | HIDPP_FF_RESET_ALL, NULL, 0, &response); |
| 1543 | |
| 1544 | /* Read current Range */ |
| 1545 | error = hidpp_send_fap_command_sync(hidpp, feature_index, |
| 1546 | HIDPP_FF_GET_APERTURE, NULL, 0, &response); |
| 1547 | if (error) |
| 1548 | hid_warn(hidpp->hid_dev, "Failed to read range from device!\n"); |
| 1549 | data->range = error ? 900 : get_unaligned_be16(&response.fap.params[0]); |
| 1550 | |
| 1551 | /* Create sysfs interface */ |
| 1552 | error = device_create_file(&(hidpp->hid_dev->dev), &dev_attr_range); |
| 1553 | if (error) |
| 1554 | hid_warn(hidpp->hid_dev, "Unable to create sysfs interface for \"range\", errno %d!\n", error); |
| 1555 | |
| 1556 | /* Read the current gain values */ |
| 1557 | error = hidpp_send_fap_command_sync(hidpp, feature_index, |
| 1558 | HIDPP_FF_GET_GLOBAL_GAINS, NULL, 0, &response); |
| 1559 | if (error) |
| 1560 | hid_warn(hidpp->hid_dev, "Failed to read gain values from device!\n"); |
| 1561 | data->gain = error ? 0xffff : get_unaligned_be16(&response.fap.params[0]); |
| 1562 | /* ignore boost value at response.fap.params[2] */ |
| 1563 | |
| 1564 | /* init the hardware command queue */ |
| 1565 | data->wq = create_singlethread_workqueue("hidpp-ff-sendqueue"); |
| 1566 | atomic_set(&data->workqueue_size, 0); |
| 1567 | |
| 1568 | /* initialize with zero autocenter to get wheel in usable state */ |
| 1569 | hidpp_ff_set_autocenter(dev, 0); |
| 1570 | |
| 1571 | hid_info(hid, "Force feeback support loaded (firmware release %d).\n", version); |
| 1572 | |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
Jiri Kosina | af2e628 | 2016-01-28 14:28:39 +0100 | [diff] [blame] | 1576 | static int hidpp_ff_deinit(struct hid_device *hid) |
Edwin Velds | ff21a63 | 2016-01-11 00:25:15 +0100 | [diff] [blame] | 1577 | { |
| 1578 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); |
| 1579 | struct input_dev *dev = hidinput->input; |
| 1580 | struct hidpp_ff_private_data *data; |
| 1581 | |
| 1582 | if (!dev) { |
| 1583 | hid_err(hid, "Struct input_dev not found!\n"); |
| 1584 | return -EINVAL; |
| 1585 | } |
| 1586 | |
| 1587 | hid_info(hid, "Unloading HID++ force feedback.\n"); |
| 1588 | data = dev->ff->private; |
| 1589 | if (!data) { |
| 1590 | hid_err(hid, "Private data not found!\n"); |
| 1591 | return -EINVAL; |
| 1592 | } |
| 1593 | |
| 1594 | destroy_workqueue(data->wq); |
| 1595 | device_remove_file(&hid->dev, &dev_attr_range); |
| 1596 | |
| 1597 | return 0; |
| 1598 | } |
| 1599 | |
| 1600 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 1601 | /* ************************************************************************** */ |
| 1602 | /* */ |
| 1603 | /* Device Support */ |
| 1604 | /* */ |
| 1605 | /* ************************************************************************** */ |
| 1606 | |
| 1607 | /* -------------------------------------------------------------------------- */ |
| 1608 | /* Touchpad HID++ devices */ |
| 1609 | /* -------------------------------------------------------------------------- */ |
| 1610 | |
Benjamin Tissoires | 57ac86c | 2014-09-30 13:18:34 -0400 | [diff] [blame] | 1611 | #define WTP_MANUAL_RESOLUTION 39 |
| 1612 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 1613 | struct wtp_data { |
| 1614 | struct input_dev *input; |
| 1615 | u16 x_size, y_size; |
| 1616 | u8 finger_count; |
| 1617 | u8 mt_feature_index; |
| 1618 | u8 button_feature_index; |
| 1619 | u8 maxcontacts; |
| 1620 | bool flip_y; |
| 1621 | unsigned int resolution; |
| 1622 | }; |
| 1623 | |
| 1624 | static int wtp_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
| 1625 | struct hid_field *field, struct hid_usage *usage, |
| 1626 | unsigned long **bit, int *max) |
| 1627 | { |
| 1628 | return -1; |
| 1629 | } |
| 1630 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 1631 | static void wtp_populate_input(struct hidpp_device *hidpp, |
| 1632 | struct input_dev *input_dev, bool origin_is_hid_core) |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 1633 | { |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 1634 | struct wtp_data *wd = hidpp->private_data; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 1635 | |
| 1636 | __set_bit(EV_ABS, input_dev->evbit); |
| 1637 | __set_bit(EV_KEY, input_dev->evbit); |
| 1638 | __clear_bit(EV_REL, input_dev->evbit); |
| 1639 | __clear_bit(EV_LED, input_dev->evbit); |
| 1640 | |
| 1641 | input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, wd->x_size, 0, 0); |
| 1642 | input_abs_set_res(input_dev, ABS_MT_POSITION_X, wd->resolution); |
| 1643 | input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, wd->y_size, 0, 0); |
| 1644 | input_abs_set_res(input_dev, ABS_MT_POSITION_Y, wd->resolution); |
| 1645 | |
| 1646 | /* Max pressure is not given by the devices, pick one */ |
| 1647 | input_set_abs_params(input_dev, ABS_MT_PRESSURE, 0, 50, 0, 0); |
| 1648 | |
| 1649 | input_set_capability(input_dev, EV_KEY, BTN_LEFT); |
| 1650 | |
Benjamin Tissoires | 57ac86c | 2014-09-30 13:18:34 -0400 | [diff] [blame] | 1651 | if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) |
| 1652 | input_set_capability(input_dev, EV_KEY, BTN_RIGHT); |
| 1653 | else |
| 1654 | __set_bit(INPUT_PROP_BUTTONPAD, input_dev->propbit); |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 1655 | |
| 1656 | input_mt_init_slots(input_dev, wd->maxcontacts, INPUT_MT_POINTER | |
| 1657 | INPUT_MT_DROP_UNUSED); |
| 1658 | |
| 1659 | wd->input = input_dev; |
| 1660 | } |
| 1661 | |
| 1662 | static void wtp_touch_event(struct wtp_data *wd, |
| 1663 | struct hidpp_touchpad_raw_xy_finger *touch_report) |
| 1664 | { |
| 1665 | int slot; |
| 1666 | |
| 1667 | if (!touch_report->finger_id || touch_report->contact_type) |
| 1668 | /* no actual data */ |
| 1669 | return; |
| 1670 | |
| 1671 | slot = input_mt_get_slot_by_key(wd->input, touch_report->finger_id); |
| 1672 | |
| 1673 | input_mt_slot(wd->input, slot); |
| 1674 | input_mt_report_slot_state(wd->input, MT_TOOL_FINGER, |
| 1675 | touch_report->contact_status); |
| 1676 | if (touch_report->contact_status) { |
| 1677 | input_event(wd->input, EV_ABS, ABS_MT_POSITION_X, |
| 1678 | touch_report->x); |
| 1679 | input_event(wd->input, EV_ABS, ABS_MT_POSITION_Y, |
| 1680 | wd->flip_y ? wd->y_size - touch_report->y : |
| 1681 | touch_report->y); |
| 1682 | input_event(wd->input, EV_ABS, ABS_MT_PRESSURE, |
| 1683 | touch_report->area); |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | static void wtp_send_raw_xy_event(struct hidpp_device *hidpp, |
| 1688 | struct hidpp_touchpad_raw_xy *raw) |
| 1689 | { |
| 1690 | struct wtp_data *wd = hidpp->private_data; |
| 1691 | int i; |
| 1692 | |
| 1693 | for (i = 0; i < 2; i++) |
| 1694 | wtp_touch_event(wd, &(raw->fingers[i])); |
| 1695 | |
Benjamin Tissoires | 57ac86c | 2014-09-30 13:18:34 -0400 | [diff] [blame] | 1696 | if (raw->end_of_frame && |
| 1697 | !(hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS)) |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 1698 | input_event(wd->input, EV_KEY, BTN_LEFT, raw->button); |
| 1699 | |
| 1700 | if (raw->end_of_frame || raw->finger_count <= 2) { |
| 1701 | input_mt_sync_frame(wd->input); |
| 1702 | input_sync(wd->input); |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | static int wtp_mouse_raw_xy_event(struct hidpp_device *hidpp, u8 *data) |
| 1707 | { |
| 1708 | struct wtp_data *wd = hidpp->private_data; |
| 1709 | u8 c1_area = ((data[7] & 0xf) * (data[7] & 0xf) + |
| 1710 | (data[7] >> 4) * (data[7] >> 4)) / 2; |
| 1711 | u8 c2_area = ((data[13] & 0xf) * (data[13] & 0xf) + |
| 1712 | (data[13] >> 4) * (data[13] >> 4)) / 2; |
| 1713 | struct hidpp_touchpad_raw_xy raw = { |
| 1714 | .timestamp = data[1], |
| 1715 | .fingers = { |
| 1716 | { |
| 1717 | .contact_type = 0, |
| 1718 | .contact_status = !!data[7], |
| 1719 | .x = get_unaligned_le16(&data[3]), |
| 1720 | .y = get_unaligned_le16(&data[5]), |
| 1721 | .z = c1_area, |
| 1722 | .area = c1_area, |
| 1723 | .finger_id = data[2], |
| 1724 | }, { |
| 1725 | .contact_type = 0, |
| 1726 | .contact_status = !!data[13], |
| 1727 | .x = get_unaligned_le16(&data[9]), |
| 1728 | .y = get_unaligned_le16(&data[11]), |
| 1729 | .z = c2_area, |
| 1730 | .area = c2_area, |
| 1731 | .finger_id = data[8], |
| 1732 | } |
| 1733 | }, |
| 1734 | .finger_count = wd->maxcontacts, |
| 1735 | .spurious_flag = 0, |
| 1736 | .end_of_frame = (data[0] >> 7) == 0, |
| 1737 | .button = data[0] & 0x01, |
| 1738 | }; |
| 1739 | |
| 1740 | wtp_send_raw_xy_event(hidpp, &raw); |
| 1741 | |
| 1742 | return 1; |
| 1743 | } |
| 1744 | |
| 1745 | static int wtp_raw_event(struct hid_device *hdev, u8 *data, int size) |
| 1746 | { |
| 1747 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
| 1748 | struct wtp_data *wd = hidpp->private_data; |
Benjamin Tissoires | 586bdc4 | 2014-09-30 13:18:33 -0400 | [diff] [blame] | 1749 | struct hidpp_report *report = (struct hidpp_report *)data; |
| 1750 | struct hidpp_touchpad_raw_xy raw; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 1751 | |
Benjamin Tissoires | 586bdc4 | 2014-09-30 13:18:33 -0400 | [diff] [blame] | 1752 | if (!wd || !wd->input) |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 1753 | return 1; |
| 1754 | |
Benjamin Tissoires | 586bdc4 | 2014-09-30 13:18:33 -0400 | [diff] [blame] | 1755 | switch (data[0]) { |
| 1756 | case 0x02: |
Peter Wu | 0b3f656 | 2014-12-16 16:55:22 +0100 | [diff] [blame] | 1757 | if (size < 2) { |
| 1758 | hid_err(hdev, "Received HID report of bad size (%d)", |
| 1759 | size); |
| 1760 | return 1; |
| 1761 | } |
Benjamin Tissoires | 57ac86c | 2014-09-30 13:18:34 -0400 | [diff] [blame] | 1762 | if (hidpp->quirks & HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS) { |
| 1763 | input_event(wd->input, EV_KEY, BTN_LEFT, |
| 1764 | !!(data[1] & 0x01)); |
| 1765 | input_event(wd->input, EV_KEY, BTN_RIGHT, |
| 1766 | !!(data[1] & 0x02)); |
| 1767 | input_sync(wd->input); |
Peter Wu | 8abd820 | 2014-12-16 01:50:16 +0100 | [diff] [blame] | 1768 | return 0; |
Benjamin Tissoires | 57ac86c | 2014-09-30 13:18:34 -0400 | [diff] [blame] | 1769 | } else { |
| 1770 | if (size < 21) |
| 1771 | return 1; |
| 1772 | return wtp_mouse_raw_xy_event(hidpp, &data[7]); |
| 1773 | } |
Benjamin Tissoires | 586bdc4 | 2014-09-30 13:18:33 -0400 | [diff] [blame] | 1774 | case REPORT_ID_HIDPP_LONG: |
Peter Wu | 0b3f656 | 2014-12-16 16:55:22 +0100 | [diff] [blame] | 1775 | /* size is already checked in hidpp_raw_event. */ |
Benjamin Tissoires | 586bdc4 | 2014-09-30 13:18:33 -0400 | [diff] [blame] | 1776 | if ((report->fap.feature_index != wd->mt_feature_index) || |
| 1777 | (report->fap.funcindex_clientid != EVENT_TOUCHPAD_RAW_XY)) |
| 1778 | return 1; |
| 1779 | hidpp_touchpad_raw_xy_event(hidpp, data + 4, &raw); |
| 1780 | |
| 1781 | wtp_send_raw_xy_event(hidpp, &raw); |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
| 1785 | return 0; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | static int wtp_get_config(struct hidpp_device *hidpp) |
| 1789 | { |
| 1790 | struct wtp_data *wd = hidpp->private_data; |
| 1791 | struct hidpp_touchpad_raw_info raw_info = {0}; |
| 1792 | u8 feature_type; |
| 1793 | int ret; |
| 1794 | |
| 1795 | ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_TOUCHPAD_RAW_XY, |
| 1796 | &wd->mt_feature_index, &feature_type); |
| 1797 | if (ret) |
| 1798 | /* means that the device is not powered up */ |
| 1799 | return ret; |
| 1800 | |
| 1801 | ret = hidpp_touchpad_get_raw_info(hidpp, wd->mt_feature_index, |
| 1802 | &raw_info); |
| 1803 | if (ret) |
| 1804 | return ret; |
| 1805 | |
| 1806 | wd->x_size = raw_info.x_size; |
| 1807 | wd->y_size = raw_info.y_size; |
| 1808 | wd->maxcontacts = raw_info.maxcontacts; |
| 1809 | wd->flip_y = raw_info.origin == TOUCHPAD_RAW_XY_ORIGIN_LOWER_LEFT; |
| 1810 | wd->resolution = raw_info.res; |
Benjamin Tissoires | 57ac86c | 2014-09-30 13:18:34 -0400 | [diff] [blame] | 1811 | if (!wd->resolution) |
| 1812 | wd->resolution = WTP_MANUAL_RESOLUTION; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 1813 | |
| 1814 | return 0; |
| 1815 | } |
| 1816 | |
| 1817 | static int wtp_allocate(struct hid_device *hdev, const struct hid_device_id *id) |
| 1818 | { |
| 1819 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
| 1820 | struct wtp_data *wd; |
| 1821 | |
| 1822 | wd = devm_kzalloc(&hdev->dev, sizeof(struct wtp_data), |
| 1823 | GFP_KERNEL); |
| 1824 | if (!wd) |
| 1825 | return -ENOMEM; |
| 1826 | |
| 1827 | hidpp->private_data = wd; |
| 1828 | |
| 1829 | return 0; |
| 1830 | }; |
| 1831 | |
Benjamin Tissoires | bf15944 | 2014-12-16 17:06:01 -0500 | [diff] [blame] | 1832 | static int wtp_connect(struct hid_device *hdev, bool connected) |
Benjamin Tissoires | 586bdc4 | 2014-09-30 13:18:33 -0400 | [diff] [blame] | 1833 | { |
| 1834 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
| 1835 | struct wtp_data *wd = hidpp->private_data; |
| 1836 | int ret; |
| 1837 | |
| 1838 | if (!connected) |
Benjamin Tissoires | bf15944 | 2014-12-16 17:06:01 -0500 | [diff] [blame] | 1839 | return 0; |
Benjamin Tissoires | 586bdc4 | 2014-09-30 13:18:33 -0400 | [diff] [blame] | 1840 | |
| 1841 | if (!wd->x_size) { |
| 1842 | ret = wtp_get_config(hidpp); |
| 1843 | if (ret) { |
| 1844 | hid_err(hdev, "Can not get wtp config: %d\n", ret); |
Benjamin Tissoires | bf15944 | 2014-12-16 17:06:01 -0500 | [diff] [blame] | 1845 | return ret; |
Benjamin Tissoires | 586bdc4 | 2014-09-30 13:18:33 -0400 | [diff] [blame] | 1846 | } |
| 1847 | } |
| 1848 | |
Benjamin Tissoires | bf15944 | 2014-12-16 17:06:01 -0500 | [diff] [blame] | 1849 | return hidpp_touchpad_set_raw_report_state(hidpp, wd->mt_feature_index, |
Benjamin Tissoires | 586bdc4 | 2014-09-30 13:18:33 -0400 | [diff] [blame] | 1850 | true, true); |
| 1851 | } |
| 1852 | |
Goffredo Baroncelli | 8a09b4f | 2015-05-30 11:00:27 +0200 | [diff] [blame] | 1853 | /* ------------------------------------------------------------------------- */ |
| 1854 | /* Logitech M560 devices */ |
| 1855 | /* ------------------------------------------------------------------------- */ |
| 1856 | |
| 1857 | /* |
| 1858 | * Logitech M560 protocol overview |
| 1859 | * |
| 1860 | * The Logitech M560 mouse, is designed for windows 8. When the middle and/or |
| 1861 | * the sides buttons are pressed, it sends some keyboard keys events |
| 1862 | * instead of buttons ones. |
| 1863 | * To complicate things further, the middle button keys sequence |
| 1864 | * is different from the odd press and the even press. |
| 1865 | * |
| 1866 | * forward button -> Super_R |
| 1867 | * backward button -> Super_L+'d' (press only) |
| 1868 | * middle button -> 1st time: Alt_L+SuperL+XF86TouchpadOff (press only) |
| 1869 | * 2nd time: left-click (press only) |
| 1870 | * NB: press-only means that when the button is pressed, the |
| 1871 | * KeyPress/ButtonPress and KeyRelease/ButtonRelease events are generated |
| 1872 | * together sequentially; instead when the button is released, no event is |
| 1873 | * generated ! |
| 1874 | * |
| 1875 | * With the command |
| 1876 | * 10<xx>0a 3500af03 (where <xx> is the mouse id), |
| 1877 | * the mouse reacts differently: |
| 1878 | * - it never sends a keyboard key event |
| 1879 | * - for the three mouse button it sends: |
| 1880 | * middle button press 11<xx>0a 3500af00... |
| 1881 | * side 1 button (forward) press 11<xx>0a 3500b000... |
| 1882 | * side 2 button (backward) press 11<xx>0a 3500ae00... |
| 1883 | * middle/side1/side2 button release 11<xx>0a 35000000... |
| 1884 | */ |
| 1885 | |
| 1886 | static const u8 m560_config_parameter[] = {0x00, 0xaf, 0x03}; |
| 1887 | |
| 1888 | struct m560_private_data { |
| 1889 | struct input_dev *input; |
| 1890 | }; |
| 1891 | |
| 1892 | /* how buttons are mapped in the report */ |
| 1893 | #define M560_MOUSE_BTN_LEFT 0x01 |
| 1894 | #define M560_MOUSE_BTN_RIGHT 0x02 |
| 1895 | #define M560_MOUSE_BTN_WHEEL_LEFT 0x08 |
| 1896 | #define M560_MOUSE_BTN_WHEEL_RIGHT 0x10 |
| 1897 | |
| 1898 | #define M560_SUB_ID 0x0a |
| 1899 | #define M560_BUTTON_MODE_REGISTER 0x35 |
| 1900 | |
| 1901 | static int m560_send_config_command(struct hid_device *hdev, bool connected) |
| 1902 | { |
| 1903 | struct hidpp_report response; |
| 1904 | struct hidpp_device *hidpp_dev; |
| 1905 | |
| 1906 | hidpp_dev = hid_get_drvdata(hdev); |
| 1907 | |
| 1908 | if (!connected) |
| 1909 | return -ENODEV; |
| 1910 | |
| 1911 | return hidpp_send_rap_command_sync( |
| 1912 | hidpp_dev, |
| 1913 | REPORT_ID_HIDPP_SHORT, |
| 1914 | M560_SUB_ID, |
| 1915 | M560_BUTTON_MODE_REGISTER, |
| 1916 | (u8 *)m560_config_parameter, |
| 1917 | sizeof(m560_config_parameter), |
| 1918 | &response |
| 1919 | ); |
| 1920 | } |
| 1921 | |
| 1922 | static int m560_allocate(struct hid_device *hdev) |
| 1923 | { |
| 1924 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
| 1925 | struct m560_private_data *d; |
| 1926 | |
| 1927 | d = devm_kzalloc(&hdev->dev, sizeof(struct m560_private_data), |
| 1928 | GFP_KERNEL); |
| 1929 | if (!d) |
| 1930 | return -ENOMEM; |
| 1931 | |
| 1932 | hidpp->private_data = d; |
| 1933 | |
| 1934 | return 0; |
| 1935 | }; |
| 1936 | |
| 1937 | static int m560_raw_event(struct hid_device *hdev, u8 *data, int size) |
| 1938 | { |
| 1939 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
| 1940 | struct m560_private_data *mydata = hidpp->private_data; |
| 1941 | |
| 1942 | /* sanity check */ |
| 1943 | if (!mydata || !mydata->input) { |
| 1944 | hid_err(hdev, "error in parameter\n"); |
| 1945 | return -EINVAL; |
| 1946 | } |
| 1947 | |
| 1948 | if (size < 7) { |
| 1949 | hid_err(hdev, "error in report\n"); |
| 1950 | return 0; |
| 1951 | } |
| 1952 | |
| 1953 | if (data[0] == REPORT_ID_HIDPP_LONG && |
| 1954 | data[2] == M560_SUB_ID && data[6] == 0x00) { |
| 1955 | /* |
| 1956 | * m560 mouse report for middle, forward and backward button |
| 1957 | * |
| 1958 | * data[0] = 0x11 |
| 1959 | * data[1] = device-id |
| 1960 | * data[2] = 0x0a |
| 1961 | * data[5] = 0xaf -> middle |
| 1962 | * 0xb0 -> forward |
| 1963 | * 0xae -> backward |
| 1964 | * 0x00 -> release all |
| 1965 | * data[6] = 0x00 |
| 1966 | */ |
| 1967 | |
| 1968 | switch (data[5]) { |
| 1969 | case 0xaf: |
| 1970 | input_report_key(mydata->input, BTN_MIDDLE, 1); |
| 1971 | break; |
| 1972 | case 0xb0: |
| 1973 | input_report_key(mydata->input, BTN_FORWARD, 1); |
| 1974 | break; |
| 1975 | case 0xae: |
| 1976 | input_report_key(mydata->input, BTN_BACK, 1); |
| 1977 | break; |
| 1978 | case 0x00: |
| 1979 | input_report_key(mydata->input, BTN_BACK, 0); |
| 1980 | input_report_key(mydata->input, BTN_FORWARD, 0); |
| 1981 | input_report_key(mydata->input, BTN_MIDDLE, 0); |
| 1982 | break; |
| 1983 | default: |
| 1984 | hid_err(hdev, "error in report\n"); |
| 1985 | return 0; |
| 1986 | } |
| 1987 | input_sync(mydata->input); |
| 1988 | |
| 1989 | } else if (data[0] == 0x02) { |
| 1990 | /* |
| 1991 | * Logitech M560 mouse report |
| 1992 | * |
| 1993 | * data[0] = type (0x02) |
| 1994 | * data[1..2] = buttons |
| 1995 | * data[3..5] = xy |
| 1996 | * data[6] = wheel |
| 1997 | */ |
| 1998 | |
| 1999 | int v; |
| 2000 | |
| 2001 | input_report_key(mydata->input, BTN_LEFT, |
| 2002 | !!(data[1] & M560_MOUSE_BTN_LEFT)); |
| 2003 | input_report_key(mydata->input, BTN_RIGHT, |
| 2004 | !!(data[1] & M560_MOUSE_BTN_RIGHT)); |
| 2005 | |
| 2006 | if (data[1] & M560_MOUSE_BTN_WHEEL_LEFT) |
| 2007 | input_report_rel(mydata->input, REL_HWHEEL, -1); |
| 2008 | else if (data[1] & M560_MOUSE_BTN_WHEEL_RIGHT) |
| 2009 | input_report_rel(mydata->input, REL_HWHEEL, 1); |
| 2010 | |
| 2011 | v = hid_snto32(hid_field_extract(hdev, data+3, 0, 12), 12); |
| 2012 | input_report_rel(mydata->input, REL_X, v); |
| 2013 | |
| 2014 | v = hid_snto32(hid_field_extract(hdev, data+3, 12, 12), 12); |
| 2015 | input_report_rel(mydata->input, REL_Y, v); |
| 2016 | |
| 2017 | v = hid_snto32(data[6], 8); |
| 2018 | input_report_rel(mydata->input, REL_WHEEL, v); |
| 2019 | |
| 2020 | input_sync(mydata->input); |
| 2021 | } |
| 2022 | |
| 2023 | return 1; |
| 2024 | } |
| 2025 | |
| 2026 | static void m560_populate_input(struct hidpp_device *hidpp, |
| 2027 | struct input_dev *input_dev, bool origin_is_hid_core) |
| 2028 | { |
| 2029 | struct m560_private_data *mydata = hidpp->private_data; |
| 2030 | |
| 2031 | mydata->input = input_dev; |
| 2032 | |
| 2033 | __set_bit(EV_KEY, mydata->input->evbit); |
| 2034 | __set_bit(BTN_MIDDLE, mydata->input->keybit); |
| 2035 | __set_bit(BTN_RIGHT, mydata->input->keybit); |
| 2036 | __set_bit(BTN_LEFT, mydata->input->keybit); |
| 2037 | __set_bit(BTN_BACK, mydata->input->keybit); |
| 2038 | __set_bit(BTN_FORWARD, mydata->input->keybit); |
| 2039 | |
| 2040 | __set_bit(EV_REL, mydata->input->evbit); |
| 2041 | __set_bit(REL_X, mydata->input->relbit); |
| 2042 | __set_bit(REL_Y, mydata->input->relbit); |
| 2043 | __set_bit(REL_WHEEL, mydata->input->relbit); |
| 2044 | __set_bit(REL_HWHEEL, mydata->input->relbit); |
| 2045 | } |
| 2046 | |
| 2047 | static int m560_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
| 2048 | struct hid_field *field, struct hid_usage *usage, |
| 2049 | unsigned long **bit, int *max) |
| 2050 | { |
| 2051 | return -1; |
| 2052 | } |
| 2053 | |
Benjamin Tissoires | 90cdd98 | 2015-09-03 09:08:30 -0400 | [diff] [blame] | 2054 | /* ------------------------------------------------------------------------- */ |
| 2055 | /* Logitech K400 devices */ |
| 2056 | /* ------------------------------------------------------------------------- */ |
| 2057 | |
| 2058 | /* |
| 2059 | * The Logitech K400 keyboard has an embedded touchpad which is seen |
| 2060 | * as a mouse from the OS point of view. There is a hardware shortcut to disable |
| 2061 | * tap-to-click but the setting is not remembered accross reset, annoying some |
| 2062 | * users. |
| 2063 | * |
| 2064 | * We can toggle this feature from the host by using the feature 0x6010: |
| 2065 | * Touchpad FW items |
| 2066 | */ |
| 2067 | |
| 2068 | struct k400_private_data { |
| 2069 | u8 feature_index; |
| 2070 | }; |
| 2071 | |
| 2072 | static int k400_disable_tap_to_click(struct hidpp_device *hidpp) |
| 2073 | { |
| 2074 | struct k400_private_data *k400 = hidpp->private_data; |
| 2075 | struct hidpp_touchpad_fw_items items = {}; |
| 2076 | int ret; |
| 2077 | u8 feature_type; |
| 2078 | |
| 2079 | if (!k400->feature_index) { |
| 2080 | ret = hidpp_root_get_feature(hidpp, |
| 2081 | HIDPP_PAGE_TOUCHPAD_FW_ITEMS, |
| 2082 | &k400->feature_index, &feature_type); |
| 2083 | if (ret) |
| 2084 | /* means that the device is not powered up */ |
| 2085 | return ret; |
| 2086 | } |
| 2087 | |
| 2088 | ret = hidpp_touchpad_fw_items_set(hidpp, k400->feature_index, &items); |
| 2089 | if (ret) |
| 2090 | return ret; |
| 2091 | |
| 2092 | return 0; |
| 2093 | } |
| 2094 | |
| 2095 | static int k400_allocate(struct hid_device *hdev) |
| 2096 | { |
| 2097 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
| 2098 | struct k400_private_data *k400; |
| 2099 | |
| 2100 | k400 = devm_kzalloc(&hdev->dev, sizeof(struct k400_private_data), |
| 2101 | GFP_KERNEL); |
| 2102 | if (!k400) |
| 2103 | return -ENOMEM; |
| 2104 | |
| 2105 | hidpp->private_data = k400; |
| 2106 | |
| 2107 | return 0; |
| 2108 | }; |
| 2109 | |
| 2110 | static int k400_connect(struct hid_device *hdev, bool connected) |
| 2111 | { |
| 2112 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
| 2113 | |
| 2114 | if (!connected) |
| 2115 | return 0; |
| 2116 | |
| 2117 | if (!disable_tap_to_click) |
| 2118 | return 0; |
| 2119 | |
| 2120 | return k400_disable_tap_to_click(hidpp); |
| 2121 | } |
| 2122 | |
Simon Wood | 7f4b49f | 2015-11-19 16:42:13 -0700 | [diff] [blame] | 2123 | /* ------------------------------------------------------------------------- */ |
| 2124 | /* Logitech G920 Driving Force Racing Wheel for Xbox One */ |
| 2125 | /* ------------------------------------------------------------------------- */ |
| 2126 | |
| 2127 | #define HIDPP_PAGE_G920_FORCE_FEEDBACK 0x8123 |
| 2128 | |
Simon Wood | 7f4b49f | 2015-11-19 16:42:13 -0700 | [diff] [blame] | 2129 | static int g920_get_config(struct hidpp_device *hidpp) |
| 2130 | { |
Simon Wood | 7f4b49f | 2015-11-19 16:42:13 -0700 | [diff] [blame] | 2131 | u8 feature_type; |
| 2132 | u8 feature_index; |
| 2133 | int ret; |
| 2134 | |
Simon Wood | 7f4b49f | 2015-11-19 16:42:13 -0700 | [diff] [blame] | 2135 | /* Find feature and store for later use */ |
| 2136 | ret = hidpp_root_get_feature(hidpp, HIDPP_PAGE_G920_FORCE_FEEDBACK, |
| 2137 | &feature_index, &feature_type); |
| 2138 | if (ret) |
| 2139 | return ret; |
| 2140 | |
Edwin Velds | ff21a63 | 2016-01-11 00:25:15 +0100 | [diff] [blame] | 2141 | ret = hidpp_ff_init(hidpp, feature_index); |
Simon Wood | 7f4b49f | 2015-11-19 16:42:13 -0700 | [diff] [blame] | 2142 | if (ret) |
Edwin Velds | ff21a63 | 2016-01-11 00:25:15 +0100 | [diff] [blame] | 2143 | hid_warn(hidpp->hid_dev, "Unable to initialize force feedback support, errno %d\n", |
| 2144 | ret); |
Simon Wood | 7f4b49f | 2015-11-19 16:42:13 -0700 | [diff] [blame] | 2145 | |
| 2146 | return 0; |
| 2147 | } |
| 2148 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2149 | /* -------------------------------------------------------------------------- */ |
| 2150 | /* Generic HID++ devices */ |
| 2151 | /* -------------------------------------------------------------------------- */ |
| 2152 | |
| 2153 | static int hidpp_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
| 2154 | struct hid_field *field, struct hid_usage *usage, |
| 2155 | unsigned long **bit, int *max) |
| 2156 | { |
| 2157 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
| 2158 | |
| 2159 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) |
| 2160 | return wtp_input_mapping(hdev, hi, field, usage, bit, max); |
Goffredo Baroncelli | 8a09b4f | 2015-05-30 11:00:27 +0200 | [diff] [blame] | 2161 | else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560 && |
| 2162 | field->application != HID_GD_MOUSE) |
| 2163 | return m560_input_mapping(hdev, hi, field, usage, bit, max); |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2164 | |
| 2165 | return 0; |
| 2166 | } |
| 2167 | |
Simon Wood | 0b1804e | 2015-11-19 16:42:15 -0700 | [diff] [blame] | 2168 | static int hidpp_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
| 2169 | struct hid_field *field, struct hid_usage *usage, |
| 2170 | unsigned long **bit, int *max) |
| 2171 | { |
| 2172 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
| 2173 | |
| 2174 | /* Ensure that Logitech G920 is not given a default fuzz/flat value */ |
| 2175 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) { |
| 2176 | if (usage->type == EV_ABS && (usage->code == ABS_X || |
| 2177 | usage->code == ABS_Y || usage->code == ABS_Z || |
| 2178 | usage->code == ABS_RZ)) { |
| 2179 | field->application = HID_GD_MULTIAXIS; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | return 0; |
| 2184 | } |
| 2185 | |
| 2186 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2187 | static void hidpp_populate_input(struct hidpp_device *hidpp, |
| 2188 | struct input_dev *input, bool origin_is_hid_core) |
| 2189 | { |
| 2190 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) |
| 2191 | wtp_populate_input(hidpp, input, origin_is_hid_core); |
Goffredo Baroncelli | 8a09b4f | 2015-05-30 11:00:27 +0200 | [diff] [blame] | 2192 | else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) |
| 2193 | m560_populate_input(hidpp, input, origin_is_hid_core); |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2194 | } |
| 2195 | |
Dmitry Torokhov | b2c68a2 | 2015-09-29 15:52:59 -0700 | [diff] [blame] | 2196 | static int hidpp_input_configured(struct hid_device *hdev, |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2197 | struct hid_input *hidinput) |
| 2198 | { |
| 2199 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2200 | struct input_dev *input = hidinput->input; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2201 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2202 | hidpp_populate_input(hidpp, input, true); |
Dmitry Torokhov | b2c68a2 | 2015-09-29 15:52:59 -0700 | [diff] [blame] | 2203 | |
| 2204 | return 0; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | static int hidpp_raw_hidpp_event(struct hidpp_device *hidpp, u8 *data, |
| 2208 | int size) |
| 2209 | { |
| 2210 | struct hidpp_report *question = hidpp->send_receive_buf; |
| 2211 | struct hidpp_report *answer = hidpp->send_receive_buf; |
| 2212 | struct hidpp_report *report = (struct hidpp_report *)data; |
| 2213 | |
| 2214 | /* |
| 2215 | * If the mutex is locked then we have a pending answer from a |
Peter Wu | e529fea | 2014-12-17 00:23:51 +0100 | [diff] [blame] | 2216 | * previously sent command. |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2217 | */ |
| 2218 | if (unlikely(mutex_is_locked(&hidpp->send_mutex))) { |
| 2219 | /* |
| 2220 | * Check for a correct hidpp20 answer or the corresponding |
| 2221 | * error |
| 2222 | */ |
| 2223 | if (hidpp_match_answer(question, report) || |
| 2224 | hidpp_match_error(question, report)) { |
| 2225 | *answer = *report; |
| 2226 | hidpp->answer_available = true; |
| 2227 | wake_up(&hidpp->wait); |
| 2228 | /* |
| 2229 | * This was an answer to a command that this driver sent |
| 2230 | * We return 1 to hid-core to avoid forwarding the |
| 2231 | * command upstream as it has been treated by the driver |
| 2232 | */ |
| 2233 | |
| 2234 | return 1; |
| 2235 | } |
| 2236 | } |
| 2237 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2238 | if (unlikely(hidpp_report_is_connect_event(report))) { |
| 2239 | atomic_set(&hidpp->connected, |
| 2240 | !(report->rap.params[0] & (1 << 6))); |
Benjamin Tissoires | 6bd4e65 | 2016-06-29 19:28:02 +1000 | [diff] [blame] | 2241 | if (schedule_work(&hidpp->work) == 0) |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2242 | dbg_hid("%s: connect event already queued\n", __func__); |
| 2243 | return 1; |
| 2244 | } |
| 2245 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2246 | return 0; |
| 2247 | } |
| 2248 | |
| 2249 | static int hidpp_raw_event(struct hid_device *hdev, struct hid_report *report, |
| 2250 | u8 *data, int size) |
| 2251 | { |
| 2252 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
Peter Wu | e529fea | 2014-12-17 00:23:51 +0100 | [diff] [blame] | 2253 | int ret = 0; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2254 | |
Peter Wu | e529fea | 2014-12-17 00:23:51 +0100 | [diff] [blame] | 2255 | /* Generic HID++ processing. */ |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2256 | switch (data[0]) { |
Simon Wood | a5ce8f5 | 2015-11-19 16:42:11 -0700 | [diff] [blame] | 2257 | case REPORT_ID_HIDPP_VERY_LONG: |
| 2258 | if (size != HIDPP_REPORT_VERY_LONG_LENGTH) { |
| 2259 | hid_err(hdev, "received hid++ report of bad size (%d)", |
| 2260 | size); |
| 2261 | return 1; |
| 2262 | } |
| 2263 | ret = hidpp_raw_hidpp_event(hidpp, data, size); |
| 2264 | break; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2265 | case REPORT_ID_HIDPP_LONG: |
| 2266 | if (size != HIDPP_REPORT_LONG_LENGTH) { |
| 2267 | hid_err(hdev, "received hid++ report of bad size (%d)", |
| 2268 | size); |
| 2269 | return 1; |
| 2270 | } |
Peter Wu | e529fea | 2014-12-17 00:23:51 +0100 | [diff] [blame] | 2271 | ret = hidpp_raw_hidpp_event(hidpp, data, size); |
| 2272 | break; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2273 | case REPORT_ID_HIDPP_SHORT: |
| 2274 | if (size != HIDPP_REPORT_SHORT_LENGTH) { |
| 2275 | hid_err(hdev, "received hid++ report of bad size (%d)", |
| 2276 | size); |
| 2277 | return 1; |
| 2278 | } |
Peter Wu | e529fea | 2014-12-17 00:23:51 +0100 | [diff] [blame] | 2279 | ret = hidpp_raw_hidpp_event(hidpp, data, size); |
| 2280 | break; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2281 | } |
| 2282 | |
Peter Wu | e529fea | 2014-12-17 00:23:51 +0100 | [diff] [blame] | 2283 | /* If no report is available for further processing, skip calling |
| 2284 | * raw_event of subclasses. */ |
| 2285 | if (ret != 0) |
| 2286 | return ret; |
| 2287 | |
Benjamin Tissoires | 206d7c6 | 2017-03-27 16:59:25 +0200 | [diff] [blame^] | 2288 | if (hidpp->capabilities & HIDPP_CAPABILITY_HIDPP20_BATTERY) { |
Peter Hutterer | 5a2b190 | 2016-06-29 19:28:01 +1000 | [diff] [blame] | 2289 | ret = hidpp20_battery_event(hidpp, data, size); |
| 2290 | if (ret != 0) |
| 2291 | return ret; |
| 2292 | } |
| 2293 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2294 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) |
| 2295 | return wtp_raw_event(hdev, data, size); |
Goffredo Baroncelli | 8a09b4f | 2015-05-30 11:00:27 +0200 | [diff] [blame] | 2296 | else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) |
| 2297 | return m560_raw_event(hdev, data, size); |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2298 | |
| 2299 | return 0; |
| 2300 | } |
| 2301 | |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 2302 | static void hidpp_overwrite_name(struct hid_device *hdev, bool use_unifying) |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2303 | { |
| 2304 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
| 2305 | char *name; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2306 | |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 2307 | if (use_unifying) |
| 2308 | /* |
| 2309 | * the device is connected through an Unifying receiver, and |
| 2310 | * might not be already connected. |
| 2311 | * Ask the receiver for its name. |
| 2312 | */ |
| 2313 | name = hidpp_get_unifying_name(hidpp); |
Benjamin Tissoires | b4f8ce0 | 2017-03-27 16:59:24 +0200 | [diff] [blame] | 2314 | else if (hidpp->protocol_major < 2) |
| 2315 | return; |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 2316 | else |
Peter Wu | 02cc097 | 2014-12-11 13:51:17 +0100 | [diff] [blame] | 2317 | name = hidpp_get_device_name(hidpp); |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2318 | |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 2319 | if (!name) { |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2320 | hid_err(hdev, "unable to retrieve the name of the device"); |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 2321 | } else { |
| 2322 | dbg_hid("HID++: Got name: %s\n", name); |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2323 | snprintf(hdev->name, sizeof(hdev->name), "%s", name); |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 2324 | } |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2325 | |
| 2326 | kfree(name); |
| 2327 | } |
| 2328 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2329 | static int hidpp_input_open(struct input_dev *dev) |
| 2330 | { |
| 2331 | struct hid_device *hid = input_get_drvdata(dev); |
| 2332 | |
| 2333 | return hid_hw_open(hid); |
| 2334 | } |
| 2335 | |
| 2336 | static void hidpp_input_close(struct input_dev *dev) |
| 2337 | { |
| 2338 | struct hid_device *hid = input_get_drvdata(dev); |
| 2339 | |
| 2340 | hid_hw_close(hid); |
| 2341 | } |
| 2342 | |
| 2343 | static struct input_dev *hidpp_allocate_input(struct hid_device *hdev) |
| 2344 | { |
| 2345 | struct input_dev *input_dev = devm_input_allocate_device(&hdev->dev); |
Benjamin Tissoires | 005b3f5 | 2015-01-08 14:37:12 -0500 | [diff] [blame] | 2346 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2347 | |
| 2348 | if (!input_dev) |
| 2349 | return NULL; |
| 2350 | |
| 2351 | input_set_drvdata(input_dev, hdev); |
| 2352 | input_dev->open = hidpp_input_open; |
| 2353 | input_dev->close = hidpp_input_close; |
| 2354 | |
Benjamin Tissoires | 005b3f5 | 2015-01-08 14:37:12 -0500 | [diff] [blame] | 2355 | input_dev->name = hidpp->name; |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2356 | input_dev->phys = hdev->phys; |
| 2357 | input_dev->uniq = hdev->uniq; |
| 2358 | input_dev->id.bustype = hdev->bus; |
| 2359 | input_dev->id.vendor = hdev->vendor; |
| 2360 | input_dev->id.product = hdev->product; |
| 2361 | input_dev->id.version = hdev->version; |
| 2362 | input_dev->dev.parent = &hdev->dev; |
| 2363 | |
| 2364 | return input_dev; |
| 2365 | } |
| 2366 | |
| 2367 | static void hidpp_connect_event(struct hidpp_device *hidpp) |
| 2368 | { |
| 2369 | struct hid_device *hdev = hidpp->hid_dev; |
| 2370 | int ret = 0; |
| 2371 | bool connected = atomic_read(&hidpp->connected); |
| 2372 | struct input_dev *input; |
| 2373 | char *name, *devm_name; |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2374 | |
Benjamin Tissoires | bf15944 | 2014-12-16 17:06:01 -0500 | [diff] [blame] | 2375 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) { |
| 2376 | ret = wtp_connect(hdev, connected); |
| 2377 | if (ret) |
| 2378 | return; |
Goffredo Baroncelli | 8a09b4f | 2015-05-30 11:00:27 +0200 | [diff] [blame] | 2379 | } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) { |
| 2380 | ret = m560_send_config_command(hdev, connected); |
| 2381 | if (ret) |
| 2382 | return; |
Benjamin Tissoires | 90cdd98 | 2015-09-03 09:08:30 -0400 | [diff] [blame] | 2383 | } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) { |
| 2384 | ret = k400_connect(hdev, connected); |
| 2385 | if (ret) |
| 2386 | return; |
Benjamin Tissoires | bf15944 | 2014-12-16 17:06:01 -0500 | [diff] [blame] | 2387 | } |
Benjamin Tissoires | 586bdc4 | 2014-09-30 13:18:33 -0400 | [diff] [blame] | 2388 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2389 | if (!connected || hidpp->delayed_input) |
| 2390 | return; |
| 2391 | |
Benjamin Tissoires | 580a7e8 | 2015-09-03 09:08:29 -0400 | [diff] [blame] | 2392 | /* the device is already connected, we can ask for its name and |
| 2393 | * protocol */ |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2394 | if (!hidpp->protocol_major) { |
| 2395 | ret = !hidpp_is_connected(hidpp); |
| 2396 | if (ret) { |
| 2397 | hid_err(hdev, "Can not get the protocol version.\n"); |
| 2398 | return; |
| 2399 | } |
Benjamin Tissoires | 580a7e8 | 2015-09-03 09:08:29 -0400 | [diff] [blame] | 2400 | hid_info(hdev, "HID++ %u.%u device connected.\n", |
| 2401 | hidpp->protocol_major, hidpp->protocol_minor); |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2402 | } |
| 2403 | |
Peter Hutterer | 5a2b190 | 2016-06-29 19:28:01 +1000 | [diff] [blame] | 2404 | hidpp_initialize_battery(hidpp); |
| 2405 | |
Benjamin Tissoires | 580a7e8 | 2015-09-03 09:08:29 -0400 | [diff] [blame] | 2406 | if (!(hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT)) |
| 2407 | /* if HID created the input nodes for us, we can stop now */ |
| 2408 | return; |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2409 | |
Benjamin Tissoires | 005b3f5 | 2015-01-08 14:37:12 -0500 | [diff] [blame] | 2410 | if (!hidpp->name || hidpp->name == hdev->name) { |
| 2411 | name = hidpp_get_device_name(hidpp); |
| 2412 | if (!name) { |
| 2413 | hid_err(hdev, |
| 2414 | "unable to retrieve the name of the device"); |
| 2415 | return; |
| 2416 | } |
| 2417 | |
| 2418 | devm_name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s", name); |
| 2419 | kfree(name); |
| 2420 | if (!devm_name) |
| 2421 | return; |
| 2422 | |
| 2423 | hidpp->name = devm_name; |
| 2424 | } |
| 2425 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2426 | input = hidpp_allocate_input(hdev); |
| 2427 | if (!input) { |
| 2428 | hid_err(hdev, "cannot allocate new input device: %d\n", ret); |
| 2429 | return; |
| 2430 | } |
| 2431 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2432 | hidpp_populate_input(hidpp, input, false); |
| 2433 | |
| 2434 | ret = input_register_device(input); |
| 2435 | if (ret) |
| 2436 | input_free_device(input); |
| 2437 | |
| 2438 | hidpp->delayed_input = input; |
| 2439 | } |
| 2440 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2441 | static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 2442 | { |
| 2443 | struct hidpp_device *hidpp; |
| 2444 | int ret; |
| 2445 | bool connected; |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2446 | unsigned int connect_mask = HID_CONNECT_DEFAULT; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2447 | |
| 2448 | hidpp = devm_kzalloc(&hdev->dev, sizeof(struct hidpp_device), |
| 2449 | GFP_KERNEL); |
| 2450 | if (!hidpp) |
| 2451 | return -ENOMEM; |
| 2452 | |
| 2453 | hidpp->hid_dev = hdev; |
Benjamin Tissoires | 005b3f5 | 2015-01-08 14:37:12 -0500 | [diff] [blame] | 2454 | hidpp->name = hdev->name; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2455 | hid_set_drvdata(hdev, hidpp); |
| 2456 | |
| 2457 | hidpp->quirks = id->driver_data; |
| 2458 | |
Benjamin Tissoires | 9188dba | 2015-03-26 12:41:57 -0400 | [diff] [blame] | 2459 | if (disable_raw_mode) { |
| 2460 | hidpp->quirks &= ~HIDPP_QUIRK_CLASS_WTP; |
Benjamin Tissoires | 580a7e8 | 2015-09-03 09:08:29 -0400 | [diff] [blame] | 2461 | hidpp->quirks &= ~HIDPP_QUIRK_NO_HIDINPUT; |
Benjamin Tissoires | 9188dba | 2015-03-26 12:41:57 -0400 | [diff] [blame] | 2462 | } |
| 2463 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2464 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP) { |
| 2465 | ret = wtp_allocate(hdev, id); |
| 2466 | if (ret) |
Goffredo Baroncelli | 8a09b4f | 2015-05-30 11:00:27 +0200 | [diff] [blame] | 2467 | goto allocate_fail; |
| 2468 | } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_M560) { |
| 2469 | ret = m560_allocate(hdev); |
| 2470 | if (ret) |
| 2471 | goto allocate_fail; |
Benjamin Tissoires | 90cdd98 | 2015-09-03 09:08:30 -0400 | [diff] [blame] | 2472 | } else if (hidpp->quirks & HIDPP_QUIRK_CLASS_K400) { |
| 2473 | ret = k400_allocate(hdev); |
| 2474 | if (ret) |
| 2475 | goto allocate_fail; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2476 | } |
| 2477 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2478 | INIT_WORK(&hidpp->work, delayed_work_cb); |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2479 | mutex_init(&hidpp->send_mutex); |
| 2480 | init_waitqueue_head(&hidpp->wait); |
| 2481 | |
| 2482 | ret = hid_parse(hdev); |
| 2483 | if (ret) { |
| 2484 | hid_err(hdev, "%s:parse failed\n", __func__); |
| 2485 | goto hid_parse_fail; |
| 2486 | } |
| 2487 | |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 2488 | if (hidpp->quirks & HIDPP_QUIRK_NO_HIDINPUT) |
| 2489 | connect_mask &= ~HID_CONNECT_HIDINPUT; |
| 2490 | |
| 2491 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) { |
| 2492 | ret = hid_hw_start(hdev, connect_mask); |
| 2493 | if (ret) { |
| 2494 | hid_err(hdev, "hw start failed\n"); |
| 2495 | goto hid_hw_start_fail; |
| 2496 | } |
| 2497 | ret = hid_hw_open(hdev); |
| 2498 | if (ret < 0) { |
| 2499 | dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n", |
| 2500 | __func__, ret); |
| 2501 | hid_hw_stop(hdev); |
| 2502 | goto hid_hw_start_fail; |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2507 | /* Allow incoming packets */ |
| 2508 | hid_device_io_start(hdev); |
| 2509 | |
| 2510 | connected = hidpp_is_connected(hidpp); |
Benjamin Tissoires | ab94e56 | 2014-09-30 13:18:28 -0400 | [diff] [blame] | 2511 | if (id->group != HID_GROUP_LOGITECH_DJ_DEVICE) { |
| 2512 | if (!connected) { |
Julia Lawall | b832da5 | 2015-04-05 14:06:29 +0200 | [diff] [blame] | 2513 | ret = -ENODEV; |
Benjamin Tissoires | ab94e56 | 2014-09-30 13:18:28 -0400 | [diff] [blame] | 2514 | hid_err(hdev, "Device not connected"); |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 2515 | goto hid_hw_open_failed; |
Benjamin Tissoires | ab94e56 | 2014-09-30 13:18:28 -0400 | [diff] [blame] | 2516 | } |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2517 | |
Benjamin Tissoires | ab94e56 | 2014-09-30 13:18:28 -0400 | [diff] [blame] | 2518 | hid_info(hdev, "HID++ %u.%u device connected.\n", |
| 2519 | hidpp->protocol_major, hidpp->protocol_minor); |
Benjamin Tissoires | ab94e56 | 2014-09-30 13:18:28 -0400 | [diff] [blame] | 2520 | } |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2521 | |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 2522 | hidpp_overwrite_name(hdev, id->group == HID_GROUP_LOGITECH_DJ_DEVICE); |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2523 | atomic_set(&hidpp->connected, connected); |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 2524 | |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2525 | if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_WTP)) { |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2526 | ret = wtp_get_config(hidpp); |
| 2527 | if (ret) |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 2528 | goto hid_hw_open_failed; |
Simon Wood | 7f4b49f | 2015-11-19 16:42:13 -0700 | [diff] [blame] | 2529 | } else if (connected && (hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) { |
| 2530 | ret = g920_get_config(hidpp); |
| 2531 | if (ret) |
| 2532 | goto hid_hw_open_failed; |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | /* Block incoming packets */ |
| 2536 | hid_device_io_stop(hdev); |
| 2537 | |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 2538 | if (!(hidpp->quirks & HIDPP_QUIRK_CLASS_G920)) { |
| 2539 | ret = hid_hw_start(hdev, connect_mask); |
| 2540 | if (ret) { |
| 2541 | hid_err(hdev, "%s:hid_hw_start returned error\n", __func__); |
| 2542 | goto hid_hw_start_fail; |
| 2543 | } |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2544 | } |
| 2545 | |
Benjamin Tissoires | 6bd4e65 | 2016-06-29 19:28:02 +1000 | [diff] [blame] | 2546 | /* Allow incoming packets */ |
| 2547 | hid_device_io_start(hdev); |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2548 | |
Benjamin Tissoires | 6bd4e65 | 2016-06-29 19:28:02 +1000 | [diff] [blame] | 2549 | hidpp_connect_event(hidpp); |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2550 | |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2551 | return ret; |
| 2552 | |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 2553 | hid_hw_open_failed: |
| 2554 | hid_device_io_stop(hdev); |
| 2555 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) { |
| 2556 | hid_hw_close(hdev); |
| 2557 | hid_hw_stop(hdev); |
| 2558 | } |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2559 | hid_hw_start_fail: |
| 2560 | hid_parse_fail: |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2561 | cancel_work_sync(&hidpp->work); |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2562 | mutex_destroy(&hidpp->send_mutex); |
Goffredo Baroncelli | 8a09b4f | 2015-05-30 11:00:27 +0200 | [diff] [blame] | 2563 | allocate_fail: |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2564 | hid_set_drvdata(hdev, NULL); |
| 2565 | return ret; |
| 2566 | } |
| 2567 | |
| 2568 | static void hidpp_remove(struct hid_device *hdev) |
| 2569 | { |
| 2570 | struct hidpp_device *hidpp = hid_get_drvdata(hdev); |
| 2571 | |
Simon Wood | 7f4b49f | 2015-11-19 16:42:13 -0700 | [diff] [blame] | 2572 | if (hidpp->quirks & HIDPP_QUIRK_CLASS_G920) { |
Edwin Velds | ff21a63 | 2016-01-11 00:25:15 +0100 | [diff] [blame] | 2573 | hidpp_ff_deinit(hdev); |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 2574 | hid_hw_close(hdev); |
Simon Wood | 7f4b49f | 2015-11-19 16:42:13 -0700 | [diff] [blame] | 2575 | } |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 2576 | hid_hw_stop(hdev); |
Benjamin Tissoires | c39e3d5 | 2014-09-30 13:18:32 -0400 | [diff] [blame] | 2577 | cancel_work_sync(&hidpp->work); |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2578 | mutex_destroy(&hidpp->send_mutex); |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | static const struct hid_device_id hidpp_devices[] = { |
Benjamin Tissoires | 57ac86c | 2014-09-30 13:18:34 -0400 | [diff] [blame] | 2582 | { /* wireless touchpad */ |
| 2583 | HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, |
| 2584 | USB_VENDOR_ID_LOGITECH, 0x4011), |
| 2585 | .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT | |
| 2586 | HIDPP_QUIRK_WTP_PHYSICAL_BUTTONS }, |
Benjamin Tissoires | 586bdc4 | 2014-09-30 13:18:33 -0400 | [diff] [blame] | 2587 | { /* wireless touchpad T650 */ |
| 2588 | HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, |
| 2589 | USB_VENDOR_ID_LOGITECH, 0x4101), |
| 2590 | .driver_data = HIDPP_QUIRK_CLASS_WTP | HIDPP_QUIRK_DELAYED_INIT }, |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2591 | { /* wireless touchpad T651 */ |
| 2592 | HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, |
| 2593 | USB_DEVICE_ID_LOGITECH_T651), |
| 2594 | .driver_data = HIDPP_QUIRK_CLASS_WTP }, |
Goffredo Baroncelli | 8a09b4f | 2015-05-30 11:00:27 +0200 | [diff] [blame] | 2595 | { /* Mouse logitech M560 */ |
Benjamin Tissoires | 3a61e97 | 2014-09-30 13:18:35 -0400 | [diff] [blame] | 2596 | HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, |
Goffredo Baroncelli | 8a09b4f | 2015-05-30 11:00:27 +0200 | [diff] [blame] | 2597 | USB_VENDOR_ID_LOGITECH, 0x402d), |
| 2598 | .driver_data = HIDPP_QUIRK_DELAYED_INIT | HIDPP_QUIRK_CLASS_M560 }, |
Benjamin Tissoires | 90cdd98 | 2015-09-03 09:08:30 -0400 | [diff] [blame] | 2599 | { /* Keyboard logitech K400 */ |
| 2600 | HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, |
| 2601 | USB_VENDOR_ID_LOGITECH, 0x4024), |
Benjamin Tissoires | 6bd4e65 | 2016-06-29 19:28:02 +1000 | [diff] [blame] | 2602 | .driver_data = HIDPP_QUIRK_CLASS_K400 }, |
Benjamin Tissoires | ab94e56 | 2014-09-30 13:18:28 -0400 | [diff] [blame] | 2603 | |
| 2604 | { HID_DEVICE(BUS_USB, HID_GROUP_LOGITECH_DJ_DEVICE, |
| 2605 | USB_VENDOR_ID_LOGITECH, HID_ANY_ID)}, |
Simon Wood | 7bfd292 | 2015-11-19 16:42:12 -0700 | [diff] [blame] | 2606 | |
| 2607 | { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL), |
| 2608 | .driver_data = HIDPP_QUIRK_CLASS_G920 | HIDPP_QUIRK_FORCE_OUTPUT_REPORTS}, |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2609 | {} |
| 2610 | }; |
| 2611 | |
| 2612 | MODULE_DEVICE_TABLE(hid, hidpp_devices); |
| 2613 | |
| 2614 | static struct hid_driver hidpp_driver = { |
| 2615 | .name = "logitech-hidpp-device", |
| 2616 | .id_table = hidpp_devices, |
| 2617 | .probe = hidpp_probe, |
| 2618 | .remove = hidpp_remove, |
| 2619 | .raw_event = hidpp_raw_event, |
| 2620 | .input_configured = hidpp_input_configured, |
| 2621 | .input_mapping = hidpp_input_mapping, |
Simon Wood | 0b1804e | 2015-11-19 16:42:15 -0700 | [diff] [blame] | 2622 | .input_mapped = hidpp_input_mapped, |
Benjamin Tissoires | 2f31c52 | 2014-09-30 13:18:27 -0400 | [diff] [blame] | 2623 | }; |
| 2624 | |
| 2625 | module_hid_driver(hidpp_driver); |