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