Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HID driver for Logitech Unifying receivers |
| 3 | * |
| 4 | * Copyright (c) 2011 Logitech |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include <linux/device.h> |
| 26 | #include <linux/hid.h> |
| 27 | #include <linux/module.h> |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 28 | #include <linux/kfifo.h> |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 29 | #include <linux/delay.h> |
Jonathan Nieder | 44d27f7 | 2012-05-11 16:17:16 +0200 | [diff] [blame] | 30 | #include <asm/unaligned.h> |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 31 | #include "hid-ids.h" |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 32 | |
| 33 | #define DJ_MAX_PAIRED_DEVICES 6 |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 34 | #define DJ_MAX_NUMBER_NOTIFS 8 |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 35 | #define DJ_RECEIVER_INDEX 0 |
| 36 | #define DJ_DEVICE_INDEX_MIN 1 |
| 37 | #define DJ_DEVICE_INDEX_MAX 6 |
| 38 | |
| 39 | #define DJREPORT_SHORT_LENGTH 15 |
| 40 | #define DJREPORT_LONG_LENGTH 32 |
| 41 | |
| 42 | #define REPORT_ID_DJ_SHORT 0x20 |
| 43 | #define REPORT_ID_DJ_LONG 0x21 |
| 44 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 45 | #define REPORT_ID_HIDPP_SHORT 0x10 |
| 46 | #define REPORT_ID_HIDPP_LONG 0x11 |
| 47 | |
| 48 | #define HIDPP_REPORT_SHORT_LENGTH 7 |
| 49 | #define HIDPP_REPORT_LONG_LENGTH 20 |
| 50 | |
| 51 | #define HIDPP_RECEIVER_INDEX 0xff |
| 52 | |
| 53 | #define REPORT_TYPE_RFREPORT_FIRST 0x01 |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 54 | #define REPORT_TYPE_RFREPORT_LAST 0x1F |
| 55 | |
| 56 | /* Command Switch to DJ mode */ |
| 57 | #define REPORT_TYPE_CMD_SWITCH 0x80 |
| 58 | #define CMD_SWITCH_PARAM_DEVBITFIELD 0x00 |
| 59 | #define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01 |
| 60 | #define TIMEOUT_NO_KEEPALIVE 0x00 |
| 61 | |
| 62 | /* Command to Get the list of Paired devices */ |
| 63 | #define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81 |
| 64 | |
| 65 | /* Device Paired Notification */ |
| 66 | #define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41 |
| 67 | #define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01 |
| 68 | #define SPFUNCTION_DEVICE_LIST_EMPTY 0x02 |
| 69 | #define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00 |
| 70 | #define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01 |
| 71 | #define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02 |
| 72 | #define DEVICE_PAIRED_RF_REPORT_TYPE 0x03 |
| 73 | |
| 74 | /* Device Un-Paired Notification */ |
| 75 | #define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40 |
| 76 | |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 77 | /* Connection Status Notification */ |
| 78 | #define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42 |
| 79 | #define CONNECTION_STATUS_PARAM_STATUS 0x00 |
| 80 | #define STATUS_LINKLOSS 0x01 |
| 81 | |
| 82 | /* Error Notification */ |
| 83 | #define REPORT_TYPE_NOTIF_ERROR 0x7F |
| 84 | #define NOTIF_ERROR_PARAM_ETYPE 0x00 |
| 85 | #define ETYPE_KEEPALIVE_TIMEOUT 0x01 |
| 86 | |
| 87 | /* supported DJ HID && RF report types */ |
| 88 | #define REPORT_TYPE_KEYBOARD 0x01 |
| 89 | #define REPORT_TYPE_MOUSE 0x02 |
| 90 | #define REPORT_TYPE_CONSUMER_CONTROL 0x03 |
| 91 | #define REPORT_TYPE_SYSTEM_CONTROL 0x04 |
| 92 | #define REPORT_TYPE_MEDIA_CENTER 0x08 |
| 93 | #define REPORT_TYPE_LEDS 0x0E |
| 94 | |
| 95 | /* RF Report types bitfield */ |
Benjamin Tissoires | a17dd1f | 2019-04-20 13:21:45 +0200 | [diff] [blame] | 96 | #define STD_KEYBOARD BIT(1) |
| 97 | #define STD_MOUSE BIT(2) |
| 98 | #define MULTIMEDIA BIT(3) |
| 99 | #define POWER_KEYS BIT(4) |
| 100 | #define MEDIA_CENTER BIT(8) |
| 101 | #define KBD_LEDS BIT(14) |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 102 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 103 | /* HID++ Device Connected Notification */ |
| 104 | #define REPORT_TYPE_NOTIF_DEVICE_CONNECTED 0x41 |
| 105 | #define HIDPP_PARAM_PROTO_TYPE 0x00 |
| 106 | #define HIDPP_PARAM_DEVICE_INFO 0x01 |
| 107 | #define HIDPP_PARAM_EQUAD_LSB 0x02 |
| 108 | #define HIDPP_PARAM_EQUAD_MSB 0x03 |
| 109 | #define HIDPP_DEVICE_TYPE_MASK GENMASK(3, 0) |
| 110 | #define HIDPP_LINK_STATUS_MASK BIT(6) |
| 111 | |
| 112 | #define HIDPP_SET_REGISTER 0x80 |
Benjamin Tissoires | c034041 | 2019-04-20 13:21:46 +0200 | [diff] [blame] | 113 | #define HIDPP_GET_LONG_REGISTER 0x83 |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 114 | #define HIDPP_REG_CONNECTION_STATE 0x02 |
Benjamin Tissoires | c034041 | 2019-04-20 13:21:46 +0200 | [diff] [blame] | 115 | #define HIDPP_REG_PAIRING_INFORMATION 0xB5 |
| 116 | #define HIDPP_PAIRING_INFORMATION 0x20 |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 117 | #define HIDPP_FAKE_DEVICE_ARRIVAL 0x02 |
| 118 | |
| 119 | enum recvr_type { |
| 120 | recvr_type_dj, |
| 121 | recvr_type_hidpp, |
| 122 | }; |
Benjamin Tissoires | c034041 | 2019-04-20 13:21:46 +0200 | [diff] [blame] | 123 | |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 124 | struct dj_report { |
| 125 | u8 report_id; |
| 126 | u8 device_index; |
| 127 | u8 report_type; |
| 128 | u8 report_params[DJREPORT_SHORT_LENGTH - 3]; |
| 129 | }; |
| 130 | |
Benjamin Tissoires | 7bb56a5 | 2019-04-20 13:21:44 +0200 | [diff] [blame] | 131 | struct hidpp_event { |
| 132 | u8 report_id; |
| 133 | u8 device_index; |
| 134 | u8 sub_id; |
| 135 | u8 params[HIDPP_REPORT_LONG_LENGTH - 3U]; |
| 136 | } __packed; |
| 137 | |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 138 | struct dj_receiver_dev { |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 139 | struct hid_device *mouse; |
| 140 | struct hid_device *keyboard; |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame] | 141 | struct hid_device *hidpp; |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 142 | struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES + |
| 143 | DJ_DEVICE_INDEX_MIN]; |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 144 | struct list_head list; |
| 145 | struct kref kref; |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 146 | struct work_struct work; |
| 147 | struct kfifo notif_fifo; |
Hans de Goede | b6aeedd | 2019-04-20 13:21:53 +0200 | [diff] [blame] | 148 | unsigned long last_query; /* in jiffies */ |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 149 | bool ready; |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 150 | enum recvr_type type; |
| 151 | unsigned int unnumbered_application; |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 152 | spinlock_t lock; |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | struct dj_device { |
| 156 | struct hid_device *hdev; |
| 157 | struct dj_receiver_dev *dj_receiver_dev; |
| 158 | u32 reports_supported; |
| 159 | u8 device_index; |
| 160 | }; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 161 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 162 | #define WORKITEM_TYPE_EMPTY 0 |
| 163 | #define WORKITEM_TYPE_PAIRED 1 |
| 164 | #define WORKITEM_TYPE_UNPAIRED 2 |
| 165 | #define WORKITEM_TYPE_UNKNOWN 255 |
| 166 | |
| 167 | struct dj_workitem { |
| 168 | u8 type; /* WORKITEM_TYPE_* */ |
| 169 | u8 device_index; |
| 170 | u8 quad_id_msb; |
| 171 | u8 quad_id_lsb; |
| 172 | u32 reports_supported; |
| 173 | }; |
| 174 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 175 | /* Keyboard descriptor (1) */ |
| 176 | static const char kbd_descriptor[] = { |
| 177 | 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */ |
| 178 | 0x09, 0x06, /* USAGE (Keyboard) */ |
| 179 | 0xA1, 0x01, /* COLLECTION (Application) */ |
| 180 | 0x85, 0x01, /* REPORT_ID (1) */ |
| 181 | 0x95, 0x08, /* REPORT_COUNT (8) */ |
| 182 | 0x75, 0x01, /* REPORT_SIZE (1) */ |
| 183 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ |
| 184 | 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ |
| 185 | 0x05, 0x07, /* USAGE_PAGE (Keyboard) */ |
| 186 | 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */ |
| 187 | 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */ |
| 188 | 0x81, 0x02, /* INPUT (Data,Var,Abs) */ |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 189 | 0x95, 0x06, /* REPORT_COUNT (6) */ |
| 190 | 0x75, 0x08, /* REPORT_SIZE (8) */ |
| 191 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ |
| 192 | 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */ |
| 193 | 0x05, 0x07, /* USAGE_PAGE (Keyboard) */ |
| 194 | 0x19, 0x00, /* USAGE_MINIMUM (no event) */ |
| 195 | 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */ |
| 196 | 0x81, 0x00, /* INPUT (Data,Ary,Abs) */ |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 197 | 0x85, 0x0e, /* REPORT_ID (14) */ |
| 198 | 0x05, 0x08, /* USAGE PAGE (LED page) */ |
| 199 | 0x95, 0x05, /* REPORT COUNT (5) */ |
| 200 | 0x75, 0x01, /* REPORT SIZE (1) */ |
| 201 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ |
| 202 | 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ |
| 203 | 0x19, 0x01, /* USAGE MINIMUM (1) */ |
| 204 | 0x29, 0x05, /* USAGE MAXIMUM (5) */ |
| 205 | 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */ |
| 206 | 0x95, 0x01, /* REPORT COUNT (1) */ |
| 207 | 0x75, 0x03, /* REPORT SIZE (3) */ |
| 208 | 0x91, 0x01, /* OUTPUT (Constant) */ |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 209 | 0xC0 |
| 210 | }; |
| 211 | |
| 212 | /* Mouse descriptor (2) */ |
| 213 | static const char mse_descriptor[] = { |
| 214 | 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ |
| 215 | 0x09, 0x02, /* USAGE (Mouse) */ |
| 216 | 0xA1, 0x01, /* COLLECTION (Application) */ |
| 217 | 0x85, 0x02, /* REPORT_ID = 2 */ |
| 218 | 0x09, 0x01, /* USAGE (pointer) */ |
| 219 | 0xA1, 0x00, /* COLLECTION (physical) */ |
| 220 | 0x05, 0x09, /* USAGE_PAGE (buttons) */ |
| 221 | 0x19, 0x01, /* USAGE_MIN (1) */ |
| 222 | 0x29, 0x10, /* USAGE_MAX (16) */ |
| 223 | 0x15, 0x00, /* LOGICAL_MIN (0) */ |
| 224 | 0x25, 0x01, /* LOGICAL_MAX (1) */ |
| 225 | 0x95, 0x10, /* REPORT_COUNT (16) */ |
| 226 | 0x75, 0x01, /* REPORT_SIZE (1) */ |
| 227 | 0x81, 0x02, /* INPUT (data var abs) */ |
| 228 | 0x05, 0x01, /* USAGE_PAGE (generic desktop) */ |
| 229 | 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */ |
| 230 | 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */ |
| 231 | 0x75, 0x0C, /* REPORT_SIZE (12) */ |
| 232 | 0x95, 0x02, /* REPORT_COUNT (2) */ |
| 233 | 0x09, 0x30, /* USAGE (X) */ |
| 234 | 0x09, 0x31, /* USAGE (Y) */ |
| 235 | 0x81, 0x06, /* INPUT */ |
| 236 | 0x15, 0x81, /* LOGICAL_MIN (-127) */ |
| 237 | 0x25, 0x7F, /* LOGICAL_MAX (127) */ |
| 238 | 0x75, 0x08, /* REPORT_SIZE (8) */ |
| 239 | 0x95, 0x01, /* REPORT_COUNT (1) */ |
| 240 | 0x09, 0x38, /* USAGE (wheel) */ |
| 241 | 0x81, 0x06, /* INPUT */ |
| 242 | 0x05, 0x0C, /* USAGE_PAGE(consumer) */ |
| 243 | 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */ |
| 244 | 0x95, 0x01, /* REPORT_COUNT (1) */ |
| 245 | 0x81, 0x06, /* INPUT */ |
| 246 | 0xC0, /* END_COLLECTION */ |
| 247 | 0xC0, /* END_COLLECTION */ |
| 248 | }; |
| 249 | |
| 250 | /* Consumer Control descriptor (3) */ |
| 251 | static const char consumer_descriptor[] = { |
| 252 | 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */ |
| 253 | 0x09, 0x01, /* USAGE (Consumer Control) */ |
| 254 | 0xA1, 0x01, /* COLLECTION (Application) */ |
| 255 | 0x85, 0x03, /* REPORT_ID = 3 */ |
| 256 | 0x75, 0x10, /* REPORT_SIZE (16) */ |
| 257 | 0x95, 0x02, /* REPORT_COUNT (2) */ |
| 258 | 0x15, 0x01, /* LOGICAL_MIN (1) */ |
| 259 | 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */ |
| 260 | 0x19, 0x01, /* USAGE_MIN (1) */ |
| 261 | 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */ |
| 262 | 0x81, 0x00, /* INPUT (Data Ary Abs) */ |
| 263 | 0xC0, /* END_COLLECTION */ |
| 264 | }; /* */ |
| 265 | |
| 266 | /* System control descriptor (4) */ |
| 267 | static const char syscontrol_descriptor[] = { |
| 268 | 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ |
| 269 | 0x09, 0x80, /* USAGE (System Control) */ |
| 270 | 0xA1, 0x01, /* COLLECTION (Application) */ |
| 271 | 0x85, 0x04, /* REPORT_ID = 4 */ |
| 272 | 0x75, 0x02, /* REPORT_SIZE (2) */ |
| 273 | 0x95, 0x01, /* REPORT_COUNT (1) */ |
| 274 | 0x15, 0x01, /* LOGICAL_MIN (1) */ |
| 275 | 0x25, 0x03, /* LOGICAL_MAX (3) */ |
| 276 | 0x09, 0x82, /* USAGE (System Sleep) */ |
| 277 | 0x09, 0x81, /* USAGE (System Power Down) */ |
| 278 | 0x09, 0x83, /* USAGE (System Wake Up) */ |
| 279 | 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */ |
| 280 | 0x75, 0x06, /* REPORT_SIZE (6) */ |
| 281 | 0x81, 0x03, /* INPUT (Cnst Var Abs) */ |
| 282 | 0xC0, /* END_COLLECTION */ |
| 283 | }; |
| 284 | |
| 285 | /* Media descriptor (8) */ |
| 286 | static const char media_descriptor[] = { |
| 287 | 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */ |
| 288 | 0x09, 0x88, /* Usage 0x0088 */ |
| 289 | 0xa1, 0x01, /* BeginCollection */ |
| 290 | 0x85, 0x08, /* Report ID 8 */ |
| 291 | 0x19, 0x01, /* Usage Min 0x0001 */ |
| 292 | 0x29, 0xff, /* Usage Max 0x00ff */ |
| 293 | 0x15, 0x01, /* Logical Min 1 */ |
| 294 | 0x26, 0xff, 0x00, /* Logical Max 255 */ |
| 295 | 0x75, 0x08, /* Report Size 8 */ |
| 296 | 0x95, 0x01, /* Report Count 1 */ |
| 297 | 0x81, 0x00, /* Input */ |
| 298 | 0xc0, /* EndCollection */ |
| 299 | }; /* */ |
| 300 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 301 | /* HIDPP descriptor */ |
| 302 | static const char hidpp_descriptor[] = { |
| 303 | 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ |
| 304 | 0x09, 0x01, /* Usage (Vendor Usage 1) */ |
| 305 | 0xa1, 0x01, /* Collection (Application) */ |
| 306 | 0x85, 0x10, /* Report ID (16) */ |
| 307 | 0x75, 0x08, /* Report Size (8) */ |
| 308 | 0x95, 0x06, /* Report Count (6) */ |
| 309 | 0x15, 0x00, /* Logical Minimum (0) */ |
| 310 | 0x26, 0xff, 0x00, /* Logical Maximum (255) */ |
| 311 | 0x09, 0x01, /* Usage (Vendor Usage 1) */ |
| 312 | 0x81, 0x00, /* Input (Data,Arr,Abs) */ |
| 313 | 0x09, 0x01, /* Usage (Vendor Usage 1) */ |
| 314 | 0x91, 0x00, /* Output (Data,Arr,Abs) */ |
| 315 | 0xc0, /* End Collection */ |
| 316 | 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ |
| 317 | 0x09, 0x02, /* Usage (Vendor Usage 2) */ |
| 318 | 0xa1, 0x01, /* Collection (Application) */ |
| 319 | 0x85, 0x11, /* Report ID (17) */ |
| 320 | 0x75, 0x08, /* Report Size (8) */ |
| 321 | 0x95, 0x13, /* Report Count (19) */ |
| 322 | 0x15, 0x00, /* Logical Minimum (0) */ |
| 323 | 0x26, 0xff, 0x00, /* Logical Maximum (255) */ |
| 324 | 0x09, 0x02, /* Usage (Vendor Usage 2) */ |
| 325 | 0x81, 0x00, /* Input (Data,Arr,Abs) */ |
| 326 | 0x09, 0x02, /* Usage (Vendor Usage 2) */ |
| 327 | 0x91, 0x00, /* Output (Data,Arr,Abs) */ |
| 328 | 0xc0, /* End Collection */ |
| 329 | 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ |
| 330 | 0x09, 0x04, /* Usage (Vendor Usage 0x04) */ |
| 331 | 0xa1, 0x01, /* Collection (Application) */ |
| 332 | 0x85, 0x20, /* Report ID (32) */ |
| 333 | 0x75, 0x08, /* Report Size (8) */ |
| 334 | 0x95, 0x0e, /* Report Count (14) */ |
| 335 | 0x15, 0x00, /* Logical Minimum (0) */ |
| 336 | 0x26, 0xff, 0x00, /* Logical Maximum (255) */ |
| 337 | 0x09, 0x41, /* Usage (Vendor Usage 0x41) */ |
| 338 | 0x81, 0x00, /* Input (Data,Arr,Abs) */ |
| 339 | 0x09, 0x41, /* Usage (Vendor Usage 0x41) */ |
| 340 | 0x91, 0x00, /* Output (Data,Arr,Abs) */ |
| 341 | 0x85, 0x21, /* Report ID (33) */ |
| 342 | 0x95, 0x1f, /* Report Count (31) */ |
| 343 | 0x15, 0x00, /* Logical Minimum (0) */ |
| 344 | 0x26, 0xff, 0x00, /* Logical Maximum (255) */ |
| 345 | 0x09, 0x42, /* Usage (Vendor Usage 0x42) */ |
| 346 | 0x81, 0x00, /* Input (Data,Arr,Abs) */ |
| 347 | 0x09, 0x42, /* Usage (Vendor Usage 0x42) */ |
| 348 | 0x91, 0x00, /* Output (Data,Arr,Abs) */ |
| 349 | 0xc0, /* End Collection */ |
| 350 | }; |
| 351 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 352 | /* Maximum size of all defined hid reports in bytes (including report id) */ |
| 353 | #define MAX_REPORT_SIZE 8 |
| 354 | |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 355 | /* Make sure all descriptors are present here */ |
| 356 | #define MAX_RDESC_SIZE \ |
| 357 | (sizeof(kbd_descriptor) + \ |
| 358 | sizeof(mse_descriptor) + \ |
| 359 | sizeof(consumer_descriptor) + \ |
| 360 | sizeof(syscontrol_descriptor) + \ |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 361 | sizeof(media_descriptor) + \ |
| 362 | sizeof(hidpp_descriptor)) |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 363 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 364 | /* Number of possible hid report types that can be created by this driver. |
| 365 | * |
| 366 | * Right now, RF report types have the same report types (or report id's) |
| 367 | * than the hid report created from those RF reports. In the future |
| 368 | * this doesnt have to be true. |
| 369 | * |
| 370 | * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds |
| 371 | * to hid report id 0x01, this is standard keyboard. Same thing applies to mice |
| 372 | * reports and consumer control, etc. If a new RF report is created, it doesn't |
| 373 | * has to have the same report id as its corresponding hid report, so an |
| 374 | * translation may have to take place for future report types. |
| 375 | */ |
| 376 | #define NUMBER_OF_HID_REPORTS 32 |
| 377 | static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = { |
| 378 | [1] = 8, /* Standard keyboard */ |
| 379 | [2] = 8, /* Standard mouse */ |
| 380 | [3] = 5, /* Consumer control */ |
| 381 | [4] = 2, /* System control */ |
| 382 | [8] = 2, /* Media Center */ |
| 383 | }; |
| 384 | |
| 385 | |
| 386 | #define LOGITECH_DJ_INTERFACE_NUMBER 0x02 |
| 387 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 388 | static struct hid_ll_driver logi_dj_ll_driver; |
| 389 | |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 390 | static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev); |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 391 | static void delayedwork_callback(struct work_struct *work); |
| 392 | |
| 393 | static LIST_HEAD(dj_hdev_list); |
| 394 | static DEFINE_MUTEX(dj_hdev_list_lock); |
| 395 | |
| 396 | /* |
| 397 | * dj/HID++ receivers are really a single logical entity, but for BIOS/Windows |
| 398 | * compatibility they have multiple USB interfaces. On HID++ receivers we need |
| 399 | * to listen for input reports on both interfaces. The functions below are used |
| 400 | * to create a single struct dj_receiver_dev for all interfaces belonging to |
| 401 | * a single USB-device / receiver. |
| 402 | */ |
| 403 | static struct dj_receiver_dev *dj_find_receiver_dev(struct hid_device *hdev) |
| 404 | { |
| 405 | struct dj_receiver_dev *djrcv_dev; |
| 406 | |
| 407 | /* Try to find an already-probed interface from the same device */ |
| 408 | list_for_each_entry(djrcv_dev, &dj_hdev_list, list) { |
| 409 | if (djrcv_dev->mouse && |
| 410 | hid_compare_device_paths(hdev, djrcv_dev->mouse, '/')) { |
| 411 | kref_get(&djrcv_dev->kref); |
| 412 | return djrcv_dev; |
| 413 | } |
| 414 | if (djrcv_dev->keyboard && |
| 415 | hid_compare_device_paths(hdev, djrcv_dev->keyboard, '/')) { |
| 416 | kref_get(&djrcv_dev->kref); |
| 417 | return djrcv_dev; |
| 418 | } |
| 419 | if (djrcv_dev->hidpp && |
| 420 | hid_compare_device_paths(hdev, djrcv_dev->hidpp, '/')) { |
| 421 | kref_get(&djrcv_dev->kref); |
| 422 | return djrcv_dev; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return NULL; |
| 427 | } |
| 428 | |
| 429 | static void dj_release_receiver_dev(struct kref *kref) |
| 430 | { |
| 431 | struct dj_receiver_dev *djrcv_dev = container_of(kref, struct dj_receiver_dev, kref); |
| 432 | |
| 433 | list_del(&djrcv_dev->list); |
| 434 | kfifo_free(&djrcv_dev->notif_fifo); |
| 435 | kfree(djrcv_dev); |
| 436 | } |
| 437 | |
| 438 | static void dj_put_receiver_dev(struct hid_device *hdev) |
| 439 | { |
| 440 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
| 441 | |
| 442 | mutex_lock(&dj_hdev_list_lock); |
| 443 | |
| 444 | if (djrcv_dev->mouse == hdev) |
| 445 | djrcv_dev->mouse = NULL; |
| 446 | if (djrcv_dev->keyboard == hdev) |
| 447 | djrcv_dev->keyboard = NULL; |
| 448 | if (djrcv_dev->hidpp == hdev) |
| 449 | djrcv_dev->hidpp = NULL; |
| 450 | |
| 451 | kref_put(&djrcv_dev->kref, dj_release_receiver_dev); |
| 452 | |
| 453 | mutex_unlock(&dj_hdev_list_lock); |
| 454 | } |
| 455 | |
| 456 | static struct dj_receiver_dev *dj_get_receiver_dev(struct hid_device *hdev, |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 457 | enum recvr_type type, |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 458 | unsigned int application, |
| 459 | bool is_hidpp) |
| 460 | { |
| 461 | struct dj_receiver_dev *djrcv_dev; |
| 462 | |
| 463 | mutex_lock(&dj_hdev_list_lock); |
| 464 | |
| 465 | djrcv_dev = dj_find_receiver_dev(hdev); |
| 466 | if (!djrcv_dev) { |
| 467 | djrcv_dev = kzalloc(sizeof(*djrcv_dev), GFP_KERNEL); |
| 468 | if (!djrcv_dev) |
| 469 | goto out; |
| 470 | |
| 471 | INIT_WORK(&djrcv_dev->work, delayedwork_callback); |
| 472 | spin_lock_init(&djrcv_dev->lock); |
| 473 | if (kfifo_alloc(&djrcv_dev->notif_fifo, |
| 474 | DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem), |
| 475 | GFP_KERNEL)) { |
| 476 | kfree(djrcv_dev); |
| 477 | djrcv_dev = NULL; |
| 478 | goto out; |
| 479 | } |
| 480 | kref_init(&djrcv_dev->kref); |
| 481 | list_add_tail(&djrcv_dev->list, &dj_hdev_list); |
Hans de Goede | b6aeedd | 2019-04-20 13:21:53 +0200 | [diff] [blame] | 482 | djrcv_dev->last_query = jiffies; |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 483 | djrcv_dev->type = type; |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | if (application == HID_GD_KEYBOARD) |
| 487 | djrcv_dev->keyboard = hdev; |
| 488 | if (application == HID_GD_MOUSE) |
| 489 | djrcv_dev->mouse = hdev; |
| 490 | if (is_hidpp) |
| 491 | djrcv_dev->hidpp = hdev; |
| 492 | |
| 493 | hid_set_drvdata(hdev, djrcv_dev); |
| 494 | out: |
| 495 | mutex_unlock(&dj_hdev_list_lock); |
| 496 | return djrcv_dev; |
| 497 | } |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 498 | |
| 499 | static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev, |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 500 | struct dj_workitem *workitem) |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 501 | { |
| 502 | /* Called in delayed work context */ |
| 503 | struct dj_device *dj_dev; |
| 504 | unsigned long flags; |
| 505 | |
| 506 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 507 | dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index]; |
| 508 | djrcv_dev->paired_dj_devices[workitem->device_index] = NULL; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 509 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 510 | |
| 511 | if (dj_dev != NULL) { |
| 512 | hid_destroy_device(dj_dev->hdev); |
| 513 | kfree(dj_dev); |
| 514 | } else { |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame] | 515 | hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n", |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 516 | __func__); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev, |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 521 | struct dj_workitem *workitem) |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 522 | { |
| 523 | /* Called in delayed work context */ |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame] | 524 | struct hid_device *djrcv_hdev = djrcv_dev->hidpp; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 525 | struct hid_device *dj_hiddev; |
| 526 | struct dj_device *dj_dev; |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 527 | u8 device_index = workitem->device_index; |
Hans de Goede | f41d766 | 2019-04-20 13:21:50 +0200 | [diff] [blame] | 528 | unsigned long flags; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 529 | |
| 530 | /* Device index goes from 1 to 6, we need 3 bytes to store the |
| 531 | * semicolon, the index, and a null terminator |
| 532 | */ |
| 533 | unsigned char tmpstr[3]; |
| 534 | |
Hans de Goede | f41d766 | 2019-04-20 13:21:50 +0200 | [diff] [blame] | 535 | /* We are the only one ever adding a device, no need to lock */ |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 536 | if (djrcv_dev->paired_dj_devices[device_index]) { |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 537 | /* The device is already known. No need to reallocate it. */ |
| 538 | dbg_hid("%s: device is already known\n", __func__); |
| 539 | return; |
| 540 | } |
| 541 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 542 | dj_hiddev = hid_allocate_device(); |
| 543 | if (IS_ERR(dj_hiddev)) { |
| 544 | dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n", |
| 545 | __func__); |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | dj_hiddev->ll_driver = &logi_dj_ll_driver; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 550 | |
| 551 | dj_hiddev->dev.parent = &djrcv_hdev->dev; |
| 552 | dj_hiddev->bus = BUS_USB; |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 553 | dj_hiddev->vendor = djrcv_hdev->vendor; |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 554 | dj_hiddev->product = (workitem->quad_id_msb << 8) | |
| 555 | workitem->quad_id_lsb; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 556 | snprintf(dj_hiddev->name, sizeof(dj_hiddev->name), |
Benjamin Tissoires | d610274 | 2014-09-30 13:18:25 -0400 | [diff] [blame] | 557 | "Logitech Unifying Device. Wireless PID:%04x", |
| 558 | dj_hiddev->product); |
| 559 | |
| 560 | dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 561 | |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 562 | memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys)); |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 563 | snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 564 | strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys)); |
| 565 | |
| 566 | dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL); |
| 567 | |
| 568 | if (!dj_dev) { |
| 569 | dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n", |
| 570 | __func__); |
| 571 | goto dj_device_allocate_fail; |
| 572 | } |
| 573 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 574 | dj_dev->reports_supported = workitem->reports_supported; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 575 | dj_dev->hdev = dj_hiddev; |
| 576 | dj_dev->dj_receiver_dev = djrcv_dev; |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 577 | dj_dev->device_index = device_index; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 578 | dj_hiddev->driver_data = dj_dev; |
| 579 | |
Hans de Goede | f41d766 | 2019-04-20 13:21:50 +0200 | [diff] [blame] | 580 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 581 | djrcv_dev->paired_dj_devices[device_index] = dj_dev; |
Hans de Goede | f41d766 | 2019-04-20 13:21:50 +0200 | [diff] [blame] | 582 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 583 | |
| 584 | if (hid_add_device(dj_hiddev)) { |
| 585 | dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n", |
| 586 | __func__); |
| 587 | goto hid_add_device_fail; |
| 588 | } |
| 589 | |
| 590 | return; |
| 591 | |
| 592 | hid_add_device_fail: |
Hans de Goede | f41d766 | 2019-04-20 13:21:50 +0200 | [diff] [blame] | 593 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 594 | djrcv_dev->paired_dj_devices[device_index] = NULL; |
Hans de Goede | f41d766 | 2019-04-20 13:21:50 +0200 | [diff] [blame] | 595 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 596 | kfree(dj_dev); |
| 597 | dj_device_allocate_fail: |
| 598 | hid_destroy_device(dj_hiddev); |
| 599 | } |
| 600 | |
| 601 | static void delayedwork_callback(struct work_struct *work) |
| 602 | { |
| 603 | struct dj_receiver_dev *djrcv_dev = |
| 604 | container_of(work, struct dj_receiver_dev, work); |
| 605 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 606 | struct dj_workitem workitem; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 607 | unsigned long flags; |
| 608 | int count; |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 609 | int retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 610 | |
| 611 | dbg_hid("%s\n", __func__); |
| 612 | |
| 613 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
| 614 | |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 615 | /* |
| 616 | * Since we attach to multiple interfaces, we may get scheduled before |
| 617 | * we are bound to the HID++ interface, catch this. |
| 618 | */ |
| 619 | if (!djrcv_dev->ready) { |
| 620 | pr_warn("%s: delayedwork queued before hidpp interface was enumerated\n", |
| 621 | __func__); |
| 622 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 623 | return; |
| 624 | } |
| 625 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 626 | count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 627 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 628 | if (count != sizeof(workitem)) { |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame] | 629 | hid_err(djrcv_dev->hidpp, "delayedwork queued without workitems available\n"); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 630 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) { |
| 635 | if (schedule_work(&djrcv_dev->work) == 0) { |
| 636 | dbg_hid("%s: did not schedule the work item, was " |
| 637 | "already queued\n", __func__); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 642 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 643 | switch (workitem.type) { |
| 644 | case WORKITEM_TYPE_PAIRED: |
| 645 | logi_dj_recv_add_djhid_device(djrcv_dev, &workitem); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 646 | break; |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 647 | case WORKITEM_TYPE_UNPAIRED: |
| 648 | logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem); |
| 649 | break; |
| 650 | case WORKITEM_TYPE_UNKNOWN: |
| 651 | retval = logi_dj_recv_query_paired_devices(djrcv_dev); |
| 652 | if (retval) { |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame] | 653 | hid_err(djrcv_dev->hidpp, "%s: logi_dj_recv_query_paired_devices error: %d\n", |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 654 | __func__, retval); |
| 655 | } |
| 656 | break; |
| 657 | case WORKITEM_TYPE_EMPTY: |
| 658 | dbg_hid("%s: device list is empty\n", __func__); |
| 659 | break; |
| 660 | } |
| 661 | } |
| 662 | |
Hans de Goede | b6aeedd | 2019-04-20 13:21:53 +0200 | [diff] [blame] | 663 | /* |
| 664 | * Sometimes we receive reports for which we do not have a paired dj_device |
| 665 | * associated with the device_index or report-type to forward the report to. |
| 666 | * This means that the original "device paired" notification corresponding |
| 667 | * to the dj_device never arrived to this driver. Possible reasons for this are: |
| 668 | * 1) hid-core discards all packets coming from a device during probe(). |
| 669 | * 2) if the receiver is plugged into a KVM switch then the pairing reports |
| 670 | * are only forwarded to it if the focus is on this PC. |
| 671 | * This function deals with this by re-asking the receiver for the list of |
| 672 | * connected devices in the delayed work callback. |
| 673 | * This function MUST be called with djrcv->lock held. |
| 674 | */ |
| 675 | static void logi_dj_recv_queue_unknown_work(struct dj_receiver_dev *djrcv_dev) |
| 676 | { |
| 677 | struct dj_workitem workitem = { .type = WORKITEM_TYPE_UNKNOWN }; |
| 678 | |
| 679 | /* Rate limit queries done because of unhandeled reports to 2/sec */ |
| 680 | if (time_before(jiffies, djrcv_dev->last_query + HZ / 2)) |
| 681 | return; |
| 682 | |
| 683 | kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem)); |
| 684 | schedule_work(&djrcv_dev->work); |
| 685 | } |
| 686 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 687 | static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev, |
| 688 | struct dj_report *dj_report) |
| 689 | { |
| 690 | /* We are called from atomic context (tasklet && djrcv->lock held) */ |
| 691 | struct dj_workitem workitem = { |
| 692 | .device_index = dj_report->device_index, |
| 693 | }; |
| 694 | |
| 695 | switch (dj_report->report_type) { |
| 696 | case REPORT_TYPE_NOTIF_DEVICE_PAIRED: |
| 697 | workitem.type = WORKITEM_TYPE_PAIRED; |
| 698 | if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] & |
| 699 | SPFUNCTION_DEVICE_LIST_EMPTY) { |
| 700 | workitem.type = WORKITEM_TYPE_EMPTY; |
| 701 | break; |
| 702 | } |
| 703 | /* fall-through */ |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 704 | case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED: |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 705 | workitem.quad_id_msb = |
| 706 | dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB]; |
| 707 | workitem.quad_id_lsb = |
| 708 | dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB]; |
| 709 | workitem.reports_supported = get_unaligned_le32( |
| 710 | dj_report->report_params + |
| 711 | DEVICE_PAIRED_RF_REPORT_TYPE); |
| 712 | if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED) |
| 713 | workitem.type = WORKITEM_TYPE_UNPAIRED; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 714 | break; |
| 715 | default: |
Hans de Goede | b6aeedd | 2019-04-20 13:21:53 +0200 | [diff] [blame] | 716 | logi_dj_recv_queue_unknown_work(djrcv_dev); |
| 717 | return; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 718 | } |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 719 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 720 | kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 721 | |
| 722 | if (schedule_work(&djrcv_dev->work) == 0) { |
| 723 | dbg_hid("%s: did not schedule the work item, was already " |
| 724 | "queued\n", __func__); |
| 725 | } |
| 726 | } |
| 727 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 728 | static void logi_hidpp_dev_conn_notif_equad(struct hidpp_event *hidpp_report, |
| 729 | struct dj_workitem *workitem) |
| 730 | { |
| 731 | workitem->type = WORKITEM_TYPE_PAIRED; |
| 732 | workitem->quad_id_msb = hidpp_report->params[HIDPP_PARAM_EQUAD_MSB]; |
| 733 | workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_EQUAD_LSB]; |
| 734 | switch (hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] & |
| 735 | HIDPP_DEVICE_TYPE_MASK) { |
| 736 | case REPORT_TYPE_KEYBOARD: |
| 737 | workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA | |
| 738 | POWER_KEYS | MEDIA_CENTER; |
| 739 | break; |
| 740 | case REPORT_TYPE_MOUSE: |
| 741 | workitem->reports_supported |= STD_MOUSE; |
| 742 | break; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | static void logi_hidpp_recv_queue_notif(struct hid_device *hdev, |
| 747 | struct hidpp_event *hidpp_report) |
| 748 | { |
| 749 | /* We are called from atomic context (tasklet && djrcv->lock held) */ |
| 750 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
| 751 | const char *device_type = "UNKNOWN"; |
| 752 | struct dj_workitem workitem = { |
| 753 | .type = WORKITEM_TYPE_EMPTY, |
| 754 | .device_index = hidpp_report->device_index, |
| 755 | }; |
| 756 | |
| 757 | switch (hidpp_report->params[HIDPP_PARAM_PROTO_TYPE]) { |
| 758 | case 0x01: |
| 759 | device_type = "Bluetooth"; |
| 760 | break; |
| 761 | case 0x02: |
| 762 | device_type = "27 Mhz"; |
| 763 | break; |
| 764 | case 0x03: |
| 765 | device_type = "QUAD or eQUAD"; |
| 766 | logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem); |
| 767 | break; |
| 768 | case 0x04: |
| 769 | device_type = "eQUAD step 4 DJ"; |
| 770 | logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem); |
| 771 | break; |
| 772 | case 0x05: |
| 773 | device_type = "DFU Lite"; |
| 774 | break; |
| 775 | case 0x06: |
| 776 | device_type = "eQUAD step 4 Lite"; |
| 777 | logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem); |
| 778 | break; |
| 779 | case 0x07: |
| 780 | device_type = "eQUAD step 4 Gaming"; |
| 781 | break; |
| 782 | case 0x08: |
| 783 | device_type = "eQUAD step 4 for gamepads"; |
| 784 | break; |
| 785 | case 0x0a: |
| 786 | device_type = "eQUAD nano Lite"; |
| 787 | logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem); |
| 788 | break; |
| 789 | case 0x0c: |
| 790 | device_type = "eQUAD Lightspeed"; |
| 791 | break; |
| 792 | } |
| 793 | |
| 794 | if (workitem.type == WORKITEM_TYPE_EMPTY) { |
| 795 | hid_warn(hdev, |
| 796 | "unusable device of type %s (0x%02x) connected on slot %d", |
| 797 | device_type, |
| 798 | hidpp_report->params[HIDPP_PARAM_PROTO_TYPE], |
| 799 | hidpp_report->device_index); |
| 800 | return; |
| 801 | } |
| 802 | |
| 803 | hid_info(hdev, "device of type %s (0x%02x) connected on slot %d", |
| 804 | device_type, hidpp_report->params[HIDPP_PARAM_PROTO_TYPE], |
| 805 | hidpp_report->device_index); |
| 806 | |
| 807 | |
| 808 | kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem)); |
| 809 | |
| 810 | if (schedule_work(&djrcv_dev->work) == 0) { |
| 811 | dbg_hid("%s: did not schedule the work item, was already queued\n", |
| 812 | __func__); |
| 813 | } |
| 814 | } |
| 815 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 816 | static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev, |
| 817 | struct dj_report *dj_report) |
| 818 | { |
| 819 | /* We are called from atomic context (tasklet && djrcv->lock held) */ |
| 820 | unsigned int i; |
| 821 | u8 reportbuffer[MAX_REPORT_SIZE]; |
| 822 | struct dj_device *djdev; |
| 823 | |
| 824 | djdev = djrcv_dev->paired_dj_devices[dj_report->device_index]; |
| 825 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 826 | memset(reportbuffer, 0, sizeof(reportbuffer)); |
| 827 | |
| 828 | for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) { |
| 829 | if (djdev->reports_supported & (1 << i)) { |
| 830 | reportbuffer[0] = i; |
| 831 | if (hid_input_report(djdev->hdev, |
| 832 | HID_INPUT_REPORT, |
| 833 | reportbuffer, |
| 834 | hid_reportid_size_map[i], 1)) { |
| 835 | dbg_hid("hid_input_report error sending null " |
| 836 | "report\n"); |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | |
Benjamin Tissoires | 8389823 | 2019-04-20 13:21:43 +0200 | [diff] [blame] | 842 | static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev, |
| 843 | struct dj_report *dj_report) |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 844 | { |
| 845 | /* We are called from atomic context (tasklet && djrcv->lock held) */ |
| 846 | struct dj_device *dj_device; |
| 847 | |
| 848 | dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index]; |
| 849 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 850 | if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) || |
| 851 | (hid_reportid_size_map[dj_report->report_type] == 0)) { |
| 852 | dbg_hid("invalid report type:%x\n", dj_report->report_type); |
| 853 | return; |
| 854 | } |
| 855 | |
| 856 | if (hid_input_report(dj_device->hdev, |
| 857 | HID_INPUT_REPORT, &dj_report->report_type, |
| 858 | hid_reportid_size_map[dj_report->report_type], 1)) { |
| 859 | dbg_hid("hid_input_report error\n"); |
| 860 | } |
| 861 | } |
| 862 | |
Benjamin Tissoires | 8389823 | 2019-04-20 13:21:43 +0200 | [diff] [blame] | 863 | static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data, |
| 864 | int size) |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 865 | { |
| 866 | /* We are called from atomic context (tasklet && djrcv->lock held) */ |
| 867 | if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1)) |
| 868 | dbg_hid("hid_input_report error\n"); |
| 869 | } |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 870 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 871 | static void logi_dj_recv_forward_input_report(struct hid_device *hdev, |
| 872 | u8 *data, int size) |
| 873 | { |
| 874 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
| 875 | struct dj_device *dj_dev; |
| 876 | unsigned long flags; |
| 877 | u8 report = data[0]; |
| 878 | int i; |
| 879 | |
| 880 | if (report > REPORT_TYPE_RFREPORT_LAST) { |
| 881 | hid_err(hdev, "Unexpect input report number %d\n", report); |
| 882 | return; |
| 883 | } |
| 884 | |
| 885 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
| 886 | for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) { |
| 887 | dj_dev = djrcv_dev->paired_dj_devices[i]; |
| 888 | if (dj_dev && (dj_dev->reports_supported & BIT(report))) { |
| 889 | logi_dj_recv_forward_report(dj_dev, data, size); |
| 890 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 891 | return; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | logi_dj_recv_queue_unknown_work(djrcv_dev); |
| 896 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 897 | |
| 898 | dbg_hid("No dj-devs handling input report number %d\n", report); |
| 899 | } |
| 900 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 901 | static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev, |
| 902 | struct dj_report *dj_report) |
| 903 | { |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame] | 904 | struct hid_device *hdev = djrcv_dev->hidpp; |
Benjamin Tissoires | dcd9006 | 2013-03-05 17:09:00 +0100 | [diff] [blame] | 905 | struct hid_report *report; |
| 906 | struct hid_report_enum *output_report_enum; |
| 907 | u8 *data = (u8 *)(&dj_report->device_index); |
Kees Cook | 297502a | 2013-09-11 21:56:56 +0200 | [diff] [blame] | 908 | unsigned int i; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 909 | |
Benjamin Tissoires | dcd9006 | 2013-03-05 17:09:00 +0100 | [diff] [blame] | 910 | output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT]; |
| 911 | report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT]; |
| 912 | |
| 913 | if (!report) { |
| 914 | dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 915 | return -ENODEV; |
| 916 | } |
| 917 | |
Kees Cook | 297502a | 2013-09-11 21:56:56 +0200 | [diff] [blame] | 918 | for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++) |
Benjamin Tissoires | dcd9006 | 2013-03-05 17:09:00 +0100 | [diff] [blame] | 919 | report->field[0]->value[i] = data[i]; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 920 | |
Jiri Kosina | 83a44ac | 2013-03-09 10:58:13 +0100 | [diff] [blame] | 921 | hid_hw_request(hdev, report, HID_REQ_SET_REPORT); |
Benjamin Tissoires | dcd9006 | 2013-03-05 17:09:00 +0100 | [diff] [blame] | 922 | |
| 923 | return 0; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 924 | } |
| 925 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 926 | static int logi_dj_recv_query_hidpp_devices(struct dj_receiver_dev *djrcv_dev) |
| 927 | { |
| 928 | const u8 template[] = {REPORT_ID_HIDPP_SHORT, |
| 929 | HIDPP_RECEIVER_INDEX, |
| 930 | HIDPP_SET_REGISTER, |
| 931 | HIDPP_REG_CONNECTION_STATE, |
| 932 | HIDPP_FAKE_DEVICE_ARRIVAL, |
| 933 | 0x00, 0x00}; |
| 934 | u8 *hidpp_report; |
| 935 | int retval; |
| 936 | |
| 937 | hidpp_report = kmemdup(template, sizeof(template), GFP_KERNEL); |
| 938 | if (!hidpp_report) |
| 939 | return -ENOMEM; |
| 940 | |
| 941 | retval = hid_hw_raw_request(djrcv_dev->hidpp, |
| 942 | REPORT_ID_HIDPP_SHORT, |
| 943 | hidpp_report, sizeof(template), |
| 944 | HID_OUTPUT_REPORT, |
| 945 | HID_REQ_SET_REPORT); |
| 946 | |
| 947 | kfree(hidpp_report); |
| 948 | return 0; |
| 949 | } |
| 950 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 951 | static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev) |
| 952 | { |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 953 | struct dj_report *dj_report; |
| 954 | int retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 955 | |
Hans de Goede | b6aeedd | 2019-04-20 13:21:53 +0200 | [diff] [blame] | 956 | djrcv_dev->last_query = jiffies; |
| 957 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 958 | if (djrcv_dev->type != recvr_type_dj) |
| 959 | return logi_dj_recv_query_hidpp_devices(djrcv_dev); |
| 960 | |
Alan Cox | 8a55ade | 2012-09-04 15:10:08 +0100 | [diff] [blame] | 961 | dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL); |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 962 | if (!dj_report) |
| 963 | return -ENOMEM; |
| 964 | dj_report->report_id = REPORT_ID_DJ_SHORT; |
| 965 | dj_report->device_index = 0xFF; |
| 966 | dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES; |
| 967 | retval = logi_dj_recv_send_report(djrcv_dev, dj_report); |
| 968 | kfree(dj_report); |
| 969 | return retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 970 | } |
| 971 | |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 972 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 973 | static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev, |
| 974 | unsigned timeout) |
| 975 | { |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame] | 976 | struct hid_device *hdev = djrcv_dev->hidpp; |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 977 | struct dj_report *dj_report; |
Benjamin Tissoires | 6a9ddc8 | 2014-09-30 13:18:31 -0400 | [diff] [blame] | 978 | u8 *buf; |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 979 | int retval = 0; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 980 | |
Alan Cox | 8a55ade | 2012-09-04 15:10:08 +0100 | [diff] [blame] | 981 | dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL); |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 982 | if (!dj_report) |
| 983 | return -ENOMEM; |
Benjamin Tisssoires | 42c22db | 2014-01-08 17:18:45 -0500 | [diff] [blame] | 984 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 985 | if (djrcv_dev->type == recvr_type_dj) { |
| 986 | dj_report->report_id = REPORT_ID_DJ_SHORT; |
| 987 | dj_report->device_index = 0xFF; |
| 988 | dj_report->report_type = REPORT_TYPE_CMD_SWITCH; |
| 989 | dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F; |
| 990 | dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = |
| 991 | (u8)timeout; |
| 992 | |
| 993 | retval = logi_dj_recv_send_report(djrcv_dev, dj_report); |
| 994 | |
| 995 | /* |
| 996 | * Ugly sleep to work around a USB 3.0 bug when the receiver is |
| 997 | * still processing the "switch-to-dj" command while we send an |
| 998 | * other command. |
| 999 | * 50 msec should gives enough time to the receiver to be ready. |
| 1000 | */ |
| 1001 | msleep(50); |
| 1002 | } |
Benjamin Tisssoires | 42c22db | 2014-01-08 17:18:45 -0500 | [diff] [blame] | 1003 | |
Benjamin Tissoires | 6a9ddc8 | 2014-09-30 13:18:31 -0400 | [diff] [blame] | 1004 | /* |
| 1005 | * Magical bits to set up hidpp notifications when the dj devices |
| 1006 | * are connected/disconnected. |
| 1007 | * |
| 1008 | * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller |
| 1009 | * than DJREPORT_SHORT_LENGTH. |
| 1010 | */ |
| 1011 | buf = (u8 *)dj_report; |
| 1012 | |
| 1013 | memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH); |
| 1014 | |
| 1015 | buf[0] = REPORT_ID_HIDPP_SHORT; |
| 1016 | buf[1] = 0xFF; |
| 1017 | buf[2] = 0x80; |
| 1018 | buf[3] = 0x00; |
| 1019 | buf[4] = 0x00; |
| 1020 | buf[5] = 0x09; |
| 1021 | buf[6] = 0x00; |
| 1022 | |
| 1023 | hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf, |
| 1024 | HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT, |
| 1025 | HID_REQ_SET_REPORT); |
| 1026 | |
| 1027 | kfree(dj_report); |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 1028 | return retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | |
| 1032 | static int logi_dj_ll_open(struct hid_device *hid) |
| 1033 | { |
| 1034 | dbg_hid("%s:%s\n", __func__, hid->phys); |
| 1035 | return 0; |
| 1036 | |
| 1037 | } |
| 1038 | |
| 1039 | static void logi_dj_ll_close(struct hid_device *hid) |
| 1040 | { |
| 1041 | dbg_hid("%s:%s\n", __func__, hid->phys); |
| 1042 | } |
| 1043 | |
Benjamin Tissoires | 8dba302 | 2017-03-27 16:59:21 +0200 | [diff] [blame] | 1044 | /* |
| 1045 | * Register 0xB5 is "pairing information". It is solely intended for the |
| 1046 | * receiver, so do not overwrite the device index. |
| 1047 | */ |
Benjamin Tissoires | c034041 | 2019-04-20 13:21:46 +0200 | [diff] [blame] | 1048 | static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT, |
| 1049 | HIDPP_RECEIVER_INDEX, |
| 1050 | HIDPP_GET_LONG_REGISTER, |
| 1051 | HIDPP_REG_PAIRING_INFORMATION }; |
| 1052 | static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG, |
| 1053 | HIDPP_RECEIVER_INDEX, |
| 1054 | HIDPP_GET_LONG_REGISTER, |
| 1055 | HIDPP_REG_PAIRING_INFORMATION }; |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 1056 | |
Benjamin Tissoires | bd27e20 | 2014-02-10 12:58:53 -0500 | [diff] [blame] | 1057 | static int logi_dj_ll_raw_request(struct hid_device *hid, |
| 1058 | unsigned char reportnum, __u8 *buf, |
| 1059 | size_t count, unsigned char report_type, |
| 1060 | int reqtype) |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1061 | { |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 1062 | struct dj_device *djdev = hid->driver_data; |
| 1063 | struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev; |
| 1064 | u8 *out_buf; |
| 1065 | int ret; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1066 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1067 | if ((buf[0] == REPORT_ID_HIDPP_SHORT) || |
| 1068 | (buf[0] == REPORT_ID_HIDPP_LONG)) { |
| 1069 | if (count < 2) |
| 1070 | return -EINVAL; |
| 1071 | |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 1072 | /* special case where we should not overwrite |
| 1073 | * the device_index */ |
Benjamin Tissoires | 8dba302 | 2017-03-27 16:59:21 +0200 | [diff] [blame] | 1074 | if (count == 7 && !memcmp(buf, unifying_pairing_query, |
| 1075 | sizeof(unifying_pairing_query))) |
| 1076 | buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1); |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 1077 | else |
| 1078 | buf[1] = djdev->device_index; |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame] | 1079 | return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf, |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1080 | count, report_type, reqtype); |
| 1081 | } |
| 1082 | |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 1083 | if (buf[0] != REPORT_TYPE_LEDS) |
| 1084 | return -EINVAL; |
| 1085 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1086 | if (djrcv_dev->type != recvr_type_dj && count >= 2) { |
| 1087 | if (!djrcv_dev->keyboard) { |
| 1088 | hid_warn(hid, "Received REPORT_TYPE_LEDS request before the keyboard interface was enumerated\n"); |
| 1089 | return 0; |
| 1090 | } |
| 1091 | /* usbhid overrides the report ID and ignores the first byte */ |
| 1092 | return hid_hw_raw_request(djrcv_dev->keyboard, 0, buf, count, |
| 1093 | report_type, reqtype); |
| 1094 | } |
| 1095 | |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 1096 | out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC); |
| 1097 | if (!out_buf) |
| 1098 | return -ENOMEM; |
| 1099 | |
Jiri Kosina | 51217e6 | 2014-08-21 09:56:47 -0500 | [diff] [blame] | 1100 | if (count > DJREPORT_SHORT_LENGTH - 2) |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 1101 | count = DJREPORT_SHORT_LENGTH - 2; |
| 1102 | |
| 1103 | out_buf[0] = REPORT_ID_DJ_SHORT; |
| 1104 | out_buf[1] = djdev->device_index; |
| 1105 | memcpy(out_buf + 2, buf, count); |
| 1106 | |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame] | 1107 | ret = hid_hw_raw_request(djrcv_dev->hidpp, out_buf[0], out_buf, |
Benjamin Tissoires | bd27e20 | 2014-02-10 12:58:53 -0500 | [diff] [blame] | 1108 | DJREPORT_SHORT_LENGTH, report_type, reqtype); |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 1109 | |
| 1110 | kfree(out_buf); |
| 1111 | return ret; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 1114 | static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size) |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 1115 | { |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 1116 | memcpy(rdesc + *rsize, data, size); |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 1117 | *rsize += size; |
| 1118 | } |
| 1119 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1120 | static int logi_dj_ll_parse(struct hid_device *hid) |
| 1121 | { |
| 1122 | struct dj_device *djdev = hid->driver_data; |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 1123 | unsigned int rsize = 0; |
| 1124 | char *rdesc; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1125 | int retval; |
| 1126 | |
| 1127 | dbg_hid("%s\n", __func__); |
| 1128 | |
| 1129 | djdev->hdev->version = 0x0111; |
| 1130 | djdev->hdev->country = 0x00; |
| 1131 | |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 1132 | rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL); |
| 1133 | if (!rdesc) |
| 1134 | return -ENOMEM; |
| 1135 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1136 | if (djdev->reports_supported & STD_KEYBOARD) { |
| 1137 | dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n", |
| 1138 | __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 1139 | rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | if (djdev->reports_supported & STD_MOUSE) { |
| 1143 | dbg_hid("%s: sending a mouse descriptor, reports_supported: " |
| 1144 | "%x\n", __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 1145 | rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | if (djdev->reports_supported & MULTIMEDIA) { |
| 1149 | dbg_hid("%s: sending a multimedia report descriptor: %x\n", |
| 1150 | __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 1151 | rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | if (djdev->reports_supported & POWER_KEYS) { |
| 1155 | dbg_hid("%s: sending a power keys report descriptor: %x\n", |
| 1156 | __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 1157 | rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | if (djdev->reports_supported & MEDIA_CENTER) { |
| 1161 | dbg_hid("%s: sending a media center report descriptor: %x\n", |
| 1162 | __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 1163 | rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | if (djdev->reports_supported & KBD_LEDS) { |
| 1167 | dbg_hid("%s: need to send kbd leds report descriptor: %x\n", |
| 1168 | __func__, djdev->reports_supported); |
| 1169 | } |
| 1170 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1171 | rdcat(rdesc, &rsize, hidpp_descriptor, sizeof(hidpp_descriptor)); |
| 1172 | |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 1173 | retval = hid_parse_report(hid, rdesc, rsize); |
| 1174 | kfree(rdesc); |
| 1175 | |
| 1176 | return retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1177 | } |
| 1178 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1179 | static int logi_dj_ll_start(struct hid_device *hid) |
| 1180 | { |
| 1181 | dbg_hid("%s\n", __func__); |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | static void logi_dj_ll_stop(struct hid_device *hid) |
| 1186 | { |
| 1187 | dbg_hid("%s\n", __func__); |
| 1188 | } |
| 1189 | |
| 1190 | |
| 1191 | static struct hid_ll_driver logi_dj_ll_driver = { |
| 1192 | .parse = logi_dj_ll_parse, |
| 1193 | .start = logi_dj_ll_start, |
| 1194 | .stop = logi_dj_ll_stop, |
| 1195 | .open = logi_dj_ll_open, |
| 1196 | .close = logi_dj_ll_close, |
Benjamin Tissoires | bd27e20 | 2014-02-10 12:58:53 -0500 | [diff] [blame] | 1197 | .raw_request = logi_dj_ll_raw_request, |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1198 | }; |
| 1199 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1200 | static int logi_dj_dj_event(struct hid_device *hdev, |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1201 | struct hid_report *report, u8 *data, |
| 1202 | int size) |
| 1203 | { |
| 1204 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
| 1205 | struct dj_report *dj_report = (struct dj_report *) data; |
| 1206 | unsigned long flags; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1207 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1208 | /* |
| 1209 | * Here we receive all data coming from iface 2, there are 3 cases: |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1210 | * |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1211 | * 1) Data is intended for this driver i. e. data contains arrival, |
| 1212 | * departure, etc notifications, in which case we queue them for delayed |
| 1213 | * processing by the work queue. We return 1 to hid-core as no further |
| 1214 | * processing is required from it. |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1215 | * |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1216 | * 2) Data informs a connection change, if the change means rf link |
| 1217 | * loss, then we must send a null report to the upper layer to discard |
| 1218 | * potentially pressed keys that may be repeated forever by the input |
| 1219 | * layer. Return 1 to hid-core as no further processing is required. |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1220 | * |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1221 | * 3) Data is an actual input event from a paired DJ device in which |
| 1222 | * case we forward it to the correct hid device (via hid_input_report() |
| 1223 | * ) and return 1 so hid-core does not anything else with it. |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1224 | */ |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 1225 | |
Jiri Kosina | ad3e14d | 2014-08-21 09:57:17 -0500 | [diff] [blame] | 1226 | if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) || |
| 1227 | (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) { |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 1228 | /* |
| 1229 | * Device index is wrong, bail out. |
| 1230 | * This driver can ignore safely the receiver notifications, |
| 1231 | * so ignore those reports too. |
| 1232 | */ |
| 1233 | if (dj_report->device_index != DJ_RECEIVER_INDEX) |
| 1234 | dev_err(&hdev->dev, "%s: invalid device index:%d\n", |
Jiri Kosina | ad3e14d | 2014-08-21 09:57:17 -0500 | [diff] [blame] | 1235 | __func__, dj_report->device_index); |
| 1236 | return false; |
| 1237 | } |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1238 | |
| 1239 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
Benjamin Tissoires | 368d4e59 | 2014-08-22 16:16:06 -0400 | [diff] [blame] | 1240 | |
| 1241 | if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) { |
| 1242 | /* received an event for an unknown device, bail out */ |
| 1243 | logi_dj_recv_queue_notification(djrcv_dev, dj_report); |
| 1244 | goto out; |
| 1245 | } |
| 1246 | |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 1247 | switch (dj_report->report_type) { |
| 1248 | case REPORT_TYPE_NOTIF_DEVICE_PAIRED: |
Benjamin Tissoires | 368d4e59 | 2014-08-22 16:16:06 -0400 | [diff] [blame] | 1249 | /* pairing notifications are handled above the switch */ |
| 1250 | break; |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 1251 | case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED: |
| 1252 | logi_dj_recv_queue_notification(djrcv_dev, dj_report); |
| 1253 | break; |
| 1254 | case REPORT_TYPE_NOTIF_CONNECTION_STATUS: |
| 1255 | if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] == |
| 1256 | STATUS_LINKLOSS) { |
| 1257 | logi_dj_recv_forward_null_report(djrcv_dev, dj_report); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1258 | } |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 1259 | break; |
| 1260 | default: |
Benjamin Tissoires | 8389823 | 2019-04-20 13:21:43 +0200 | [diff] [blame] | 1261 | logi_dj_recv_forward_dj(djrcv_dev, dj_report); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1262 | } |
Benjamin Tissoires | 368d4e59 | 2014-08-22 16:16:06 -0400 | [diff] [blame] | 1263 | |
| 1264 | out: |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1265 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 1266 | |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 1267 | return true; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1268 | } |
| 1269 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1270 | static int logi_dj_hidpp_event(struct hid_device *hdev, |
| 1271 | struct hid_report *report, u8 *data, |
| 1272 | int size) |
| 1273 | { |
| 1274 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
Benjamin Tissoires | 7bb56a5 | 2019-04-20 13:21:44 +0200 | [diff] [blame] | 1275 | struct hidpp_event *hidpp_report = (struct hidpp_event *) data; |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1276 | struct dj_device *dj_dev; |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1277 | unsigned long flags; |
Benjamin Tissoires | 7bb56a5 | 2019-04-20 13:21:44 +0200 | [diff] [blame] | 1278 | u8 device_index = hidpp_report->device_index; |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1279 | |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 1280 | if (device_index == HIDPP_RECEIVER_INDEX) { |
| 1281 | /* special case were the device wants to know its unifying |
| 1282 | * name */ |
| 1283 | if (size == HIDPP_REPORT_LONG_LENGTH && |
Benjamin Tissoires | 8dba302 | 2017-03-27 16:59:21 +0200 | [diff] [blame] | 1284 | !memcmp(data, unifying_pairing_answer, |
| 1285 | sizeof(unifying_pairing_answer))) |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 1286 | device_index = (data[4] & 0x0F) + 1; |
| 1287 | else |
| 1288 | return false; |
| 1289 | } |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1290 | |
| 1291 | /* |
| 1292 | * Data is from the HID++ collection, in this case, we forward the |
| 1293 | * data to the corresponding child dj device and return 0 to hid-core |
| 1294 | * so he data also goes to the hidraw device of the receiver. This |
| 1295 | * allows a user space application to implement the full HID++ routing |
| 1296 | * via the receiver. |
| 1297 | */ |
| 1298 | |
| 1299 | if ((device_index < DJ_DEVICE_INDEX_MIN) || |
| 1300 | (device_index > DJ_DEVICE_INDEX_MAX)) { |
| 1301 | /* |
| 1302 | * Device index is wrong, bail out. |
| 1303 | * This driver can ignore safely the receiver notifications, |
| 1304 | * so ignore those reports too. |
| 1305 | */ |
| 1306 | dev_err(&hdev->dev, "%s: invalid device index:%d\n", |
Benjamin Tissoires | 7bb56a5 | 2019-04-20 13:21:44 +0200 | [diff] [blame] | 1307 | __func__, hidpp_report->device_index); |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1308 | return false; |
| 1309 | } |
| 1310 | |
| 1311 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
| 1312 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1313 | dj_dev = djrcv_dev->paired_dj_devices[device_index]; |
| 1314 | if (dj_dev) { |
| 1315 | logi_dj_recv_forward_report(dj_dev, data, size); |
| 1316 | } else { |
| 1317 | if (hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED) |
| 1318 | logi_hidpp_recv_queue_notif(hdev, hidpp_report); |
| 1319 | else |
| 1320 | logi_dj_recv_queue_unknown_work(djrcv_dev); |
| 1321 | } |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1322 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1323 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 1324 | |
| 1325 | return false; |
| 1326 | } |
| 1327 | |
| 1328 | static int logi_dj_raw_event(struct hid_device *hdev, |
| 1329 | struct hid_report *report, u8 *data, |
| 1330 | int size) |
| 1331 | { |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1332 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1333 | dbg_hid("%s, size:%d\n", __func__, size); |
| 1334 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1335 | if (!hdev->report_enum[HID_INPUT_REPORT].numbered) { |
| 1336 | |
| 1337 | if (djrcv_dev->unnumbered_application == HID_GD_KEYBOARD) { |
| 1338 | /* |
| 1339 | * For the keyboard, we can reuse the same report by |
| 1340 | * using the second byte which is constant in the USB |
| 1341 | * HID report descriptor. |
| 1342 | */ |
| 1343 | data[1] = data[0]; |
| 1344 | data[0] = REPORT_TYPE_KEYBOARD; |
| 1345 | |
| 1346 | logi_dj_recv_forward_input_report(hdev, data, size); |
| 1347 | |
| 1348 | /* restore previous state */ |
| 1349 | data[0] = data[1]; |
| 1350 | data[1] = 0; |
| 1351 | } |
| 1352 | |
| 1353 | return false; |
| 1354 | } |
| 1355 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1356 | switch (data[0]) { |
| 1357 | case REPORT_ID_DJ_SHORT: |
Peter Wu | f254ae9 | 2014-12-16 16:55:21 +0100 | [diff] [blame] | 1358 | if (size != DJREPORT_SHORT_LENGTH) { |
| 1359 | dev_err(&hdev->dev, "DJ report of bad size (%d)", size); |
| 1360 | return false; |
| 1361 | } |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1362 | return logi_dj_dj_event(hdev, report, data, size); |
| 1363 | case REPORT_ID_HIDPP_SHORT: |
Peter Wu | f254ae9 | 2014-12-16 16:55:21 +0100 | [diff] [blame] | 1364 | if (size != HIDPP_REPORT_SHORT_LENGTH) { |
| 1365 | dev_err(&hdev->dev, |
| 1366 | "Short HID++ report of bad size (%d)", size); |
| 1367 | return false; |
| 1368 | } |
| 1369 | return logi_dj_hidpp_event(hdev, report, data, size); |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1370 | case REPORT_ID_HIDPP_LONG: |
Peter Wu | f254ae9 | 2014-12-16 16:55:21 +0100 | [diff] [blame] | 1371 | if (size != HIDPP_REPORT_LONG_LENGTH) { |
| 1372 | dev_err(&hdev->dev, |
| 1373 | "Long HID++ report of bad size (%d)", size); |
| 1374 | return false; |
| 1375 | } |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1376 | return logi_dj_hidpp_event(hdev, report, data, size); |
| 1377 | } |
| 1378 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1379 | logi_dj_recv_forward_input_report(hdev, data, size); |
| 1380 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1381 | return false; |
| 1382 | } |
| 1383 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1384 | static int logi_dj_probe(struct hid_device *hdev, |
| 1385 | const struct hid_device_id *id) |
| 1386 | { |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 1387 | struct hid_report_enum *rep_enum; |
| 1388 | struct hid_report *rep; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1389 | struct dj_receiver_dev *djrcv_dev; |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 1390 | bool has_hidpp = false; |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 1391 | unsigned long flags; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1392 | int retval; |
| 1393 | |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 1394 | /* |
| 1395 | * Call to usbhid to fetch the HID descriptors of the current |
| 1396 | * interface subsequently call to the hid/hid-core to parse the |
| 1397 | * fetched descriptors. |
| 1398 | */ |
| 1399 | retval = hid_parse(hdev); |
| 1400 | if (retval) { |
| 1401 | dev_err(&hdev->dev, |
| 1402 | "%s:parse failed\n", __func__); |
| 1403 | return retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1404 | } |
| 1405 | |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 1406 | rep_enum = &hdev->report_enum[HID_INPUT_REPORT]; |
| 1407 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1408 | /* no input reports, bail out */ |
| 1409 | if (list_empty(&rep_enum->report_list)) |
| 1410 | return -ENODEV; |
| 1411 | |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 1412 | /* |
| 1413 | * Check for the HID++ application. |
| 1414 | * Note: we should theoretically check for HID++ and DJ |
| 1415 | * collections, but this will do. |
| 1416 | */ |
| 1417 | list_for_each_entry(rep, &rep_enum->report_list, list) { |
| 1418 | if (rep->application == 0xff000001) |
| 1419 | has_hidpp = true; |
| 1420 | } |
| 1421 | |
| 1422 | /* |
| 1423 | * Ignore interfaces without DJ/HID++ collection, they will not carry |
| 1424 | * any data, dont create any hid_device for them. |
| 1425 | */ |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1426 | if (!has_hidpp && id->driver_data == recvr_type_dj) |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 1427 | return -ENODEV; |
| 1428 | |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 1429 | /* get the current application attached to the node */ |
| 1430 | rep = list_first_entry(&rep_enum->report_list, struct hid_report, list); |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1431 | djrcv_dev = dj_get_receiver_dev(hdev, id->driver_data, |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 1432 | rep->application, has_hidpp); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1433 | if (!djrcv_dev) { |
| 1434 | dev_err(&hdev->dev, |
| 1435 | "%s:failed allocating dj_receiver_dev\n", __func__); |
| 1436 | return -ENOMEM; |
| 1437 | } |
Kees Cook | 297502a | 2013-09-11 21:56:56 +0200 | [diff] [blame] | 1438 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1439 | if (!rep_enum->numbered) |
| 1440 | djrcv_dev->unnumbered_application = rep->application; |
| 1441 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1442 | /* Starts the usb device and connects to upper interfaces hiddev and |
| 1443 | * hidraw */ |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1444 | retval = hid_hw_start(hdev, HID_CONNECT_HIDRAW|HID_CONNECT_HIDDEV); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1445 | if (retval) { |
| 1446 | dev_err(&hdev->dev, |
| 1447 | "%s:hid_hw_start returned error\n", __func__); |
| 1448 | goto hid_hw_start_fail; |
| 1449 | } |
| 1450 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1451 | if (has_hidpp) { |
| 1452 | retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0); |
| 1453 | if (retval < 0) { |
| 1454 | hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n", |
| 1455 | __func__, retval); |
| 1456 | goto switch_to_dj_mode_fail; |
| 1457 | } |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | /* This is enabling the polling urb on the IN endpoint */ |
Benjamin Tissoires | ddf7540 | 2013-07-12 11:01:02 +0200 | [diff] [blame] | 1461 | retval = hid_hw_open(hdev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1462 | if (retval < 0) { |
Benjamin Tissoires | ddf7540 | 2013-07-12 11:01:02 +0200 | [diff] [blame] | 1463 | dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n", |
| 1464 | __func__, retval); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1465 | goto llopen_failed; |
| 1466 | } |
| 1467 | |
Andrew de los Reyes | a9dd22b7 | 2013-02-18 09:20:22 -0800 | [diff] [blame] | 1468 | /* Allow incoming packets to arrive: */ |
| 1469 | hid_device_io_start(hdev); |
| 1470 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1471 | if (has_hidpp) { |
| 1472 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
| 1473 | djrcv_dev->ready = true; |
| 1474 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 1475 | retval = logi_dj_recv_query_paired_devices(djrcv_dev); |
| 1476 | if (retval < 0) { |
| 1477 | hid_err(hdev, "%s: logi_dj_recv_query_paired_devices error:%d\n", |
| 1478 | __func__, retval); |
| 1479 | goto logi_dj_recv_query_paired_devices_failed; |
| 1480 | } |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | return retval; |
| 1484 | |
| 1485 | logi_dj_recv_query_paired_devices_failed: |
Benjamin Tissoires | ddf7540 | 2013-07-12 11:01:02 +0200 | [diff] [blame] | 1486 | hid_hw_close(hdev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1487 | |
| 1488 | llopen_failed: |
| 1489 | switch_to_dj_mode_fail: |
| 1490 | hid_hw_stop(hdev); |
| 1491 | |
| 1492 | hid_hw_start_fail: |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 1493 | dj_put_receiver_dev(hdev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1494 | return retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | #ifdef CONFIG_PM |
| 1498 | static int logi_dj_reset_resume(struct hid_device *hdev) |
| 1499 | { |
| 1500 | int retval; |
| 1501 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
| 1502 | |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1503 | if (djrcv_dev->hidpp != hdev) |
| 1504 | return 0; |
| 1505 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1506 | retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0); |
| 1507 | if (retval < 0) { |
| 1508 | dev_err(&hdev->dev, |
| 1509 | "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n", |
| 1510 | __func__, retval); |
| 1511 | } |
| 1512 | |
| 1513 | return 0; |
| 1514 | } |
| 1515 | #endif |
| 1516 | |
| 1517 | static void logi_dj_remove(struct hid_device *hdev) |
| 1518 | { |
| 1519 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
| 1520 | struct dj_device *dj_dev; |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 1521 | unsigned long flags; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1522 | int i; |
| 1523 | |
| 1524 | dbg_hid("%s\n", __func__); |
| 1525 | |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 1526 | /* |
| 1527 | * This ensures that if the work gets requeued from another |
| 1528 | * interface of the same receiver it will be a no-op. |
| 1529 | */ |
| 1530 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
| 1531 | djrcv_dev->ready = false; |
| 1532 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 1533 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1534 | cancel_work_sync(&djrcv_dev->work); |
| 1535 | |
Benjamin Tissoires | ddf7540 | 2013-07-12 11:01:02 +0200 | [diff] [blame] | 1536 | hid_hw_close(hdev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1537 | hid_hw_stop(hdev); |
| 1538 | |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 1539 | /* |
| 1540 | * For proper operation we need access to all interfaces, so we destroy |
| 1541 | * the paired devices when we're unbound from any interface. |
| 1542 | * |
| 1543 | * Note we may still be bound to other interfaces, sharing the same |
| 1544 | * djrcv_dev, so we need locking here. |
| 1545 | */ |
Nestor Lopez Casado | 844580f | 2011-09-20 15:59:03 +0200 | [diff] [blame] | 1546 | for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) { |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 1547 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1548 | dj_dev = djrcv_dev->paired_dj_devices[i]; |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 1549 | djrcv_dev->paired_dj_devices[i] = NULL; |
| 1550 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1551 | if (dj_dev != NULL) { |
| 1552 | hid_destroy_device(dj_dev->hdev); |
| 1553 | kfree(dj_dev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | |
Hans de Goede | a1d97cc | 2019-04-20 13:21:52 +0200 | [diff] [blame] | 1557 | dj_put_receiver_dev(hdev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1558 | } |
| 1559 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1560 | static const struct hid_device_id logi_dj_receivers[] = { |
| 1561 | {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1562 | USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER), |
| 1563 | .driver_data = recvr_type_dj}, |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1564 | {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, |
Hans de Goede | 74808f9 | 2019-04-20 13:21:54 +0200 | [diff] [blame^] | 1565 | USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2), |
| 1566 | .driver_data = recvr_type_dj}, |
| 1567 | { /* Logitech Nano (non DJ) receiver */ |
| 1568 | HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, |
| 1569 | USB_DEVICE_ID_LOGITECH_NANO_RECEIVER), |
| 1570 | .driver_data = recvr_type_hidpp}, |
| 1571 | { /* Logitech Nano (non DJ) receiver */ |
| 1572 | HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, |
| 1573 | USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_2), |
| 1574 | .driver_data = recvr_type_hidpp}, |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1575 | {} |
| 1576 | }; |
| 1577 | |
| 1578 | MODULE_DEVICE_TABLE(hid, logi_dj_receivers); |
| 1579 | |
| 1580 | static struct hid_driver logi_djreceiver_driver = { |
| 1581 | .name = "logitech-djreceiver", |
| 1582 | .id_table = logi_dj_receivers, |
| 1583 | .probe = logi_dj_probe, |
| 1584 | .remove = logi_dj_remove, |
| 1585 | .raw_event = logi_dj_raw_event, |
| 1586 | #ifdef CONFIG_PM |
| 1587 | .reset_resume = logi_dj_reset_resume, |
| 1588 | #endif |
| 1589 | }; |
| 1590 | |
Benjamin Tissoires | ab94e56 | 2014-09-30 13:18:28 -0400 | [diff] [blame] | 1591 | module_hid_driver(logi_djreceiver_driver); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1592 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1593 | MODULE_LICENSE("GPL"); |
| 1594 | MODULE_AUTHOR("Logitech"); |
| 1595 | MODULE_AUTHOR("Nestor Lopez Casado"); |
| 1596 | MODULE_AUTHOR("nlopezcasad@logitech.com"); |