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