Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HID driver for Logitech Unifying receivers |
| 3 | * |
| 4 | * Copyright (c) 2011 Logitech |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include <linux/device.h> |
| 26 | #include <linux/hid.h> |
| 27 | #include <linux/module.h> |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 28 | #include <linux/kfifo.h> |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 29 | #include <linux/delay.h> |
Jonathan Nieder | 44d27f7 | 2012-05-11 16:17:16 +0200 | [diff] [blame] | 30 | #include <asm/unaligned.h> |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 31 | #include "hid-ids.h" |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 32 | |
| 33 | #define DJ_MAX_PAIRED_DEVICES 6 |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 34 | #define DJ_MAX_NUMBER_NOTIFS 8 |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 35 | #define DJ_RECEIVER_INDEX 0 |
| 36 | #define DJ_DEVICE_INDEX_MIN 1 |
| 37 | #define DJ_DEVICE_INDEX_MAX 6 |
| 38 | |
| 39 | #define DJREPORT_SHORT_LENGTH 15 |
| 40 | #define DJREPORT_LONG_LENGTH 32 |
| 41 | |
| 42 | #define REPORT_ID_DJ_SHORT 0x20 |
| 43 | #define REPORT_ID_DJ_LONG 0x21 |
| 44 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 45 | #define REPORT_ID_HIDPP_SHORT 0x10 |
| 46 | #define REPORT_ID_HIDPP_LONG 0x11 |
| 47 | |
| 48 | #define HIDPP_REPORT_SHORT_LENGTH 7 |
| 49 | #define HIDPP_REPORT_LONG_LENGTH 20 |
| 50 | |
| 51 | #define HIDPP_RECEIVER_INDEX 0xff |
| 52 | |
| 53 | #define REPORT_TYPE_RFREPORT_FIRST 0x01 |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 54 | #define REPORT_TYPE_RFREPORT_LAST 0x1F |
| 55 | |
| 56 | /* Command Switch to DJ mode */ |
| 57 | #define REPORT_TYPE_CMD_SWITCH 0x80 |
| 58 | #define CMD_SWITCH_PARAM_DEVBITFIELD 0x00 |
| 59 | #define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01 |
| 60 | #define TIMEOUT_NO_KEEPALIVE 0x00 |
| 61 | |
| 62 | /* Command to Get the list of Paired devices */ |
| 63 | #define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81 |
| 64 | |
| 65 | /* Device Paired Notification */ |
| 66 | #define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41 |
| 67 | #define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01 |
| 68 | #define SPFUNCTION_DEVICE_LIST_EMPTY 0x02 |
| 69 | #define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00 |
| 70 | #define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01 |
| 71 | #define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02 |
| 72 | #define DEVICE_PAIRED_RF_REPORT_TYPE 0x03 |
| 73 | |
| 74 | /* Device Un-Paired Notification */ |
| 75 | #define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40 |
| 76 | |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 77 | /* Connection Status Notification */ |
| 78 | #define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42 |
| 79 | #define CONNECTION_STATUS_PARAM_STATUS 0x00 |
| 80 | #define STATUS_LINKLOSS 0x01 |
| 81 | |
| 82 | /* Error Notification */ |
| 83 | #define REPORT_TYPE_NOTIF_ERROR 0x7F |
| 84 | #define NOTIF_ERROR_PARAM_ETYPE 0x00 |
| 85 | #define ETYPE_KEEPALIVE_TIMEOUT 0x01 |
| 86 | |
| 87 | /* supported DJ HID && RF report types */ |
| 88 | #define REPORT_TYPE_KEYBOARD 0x01 |
| 89 | #define REPORT_TYPE_MOUSE 0x02 |
| 90 | #define REPORT_TYPE_CONSUMER_CONTROL 0x03 |
| 91 | #define REPORT_TYPE_SYSTEM_CONTROL 0x04 |
| 92 | #define REPORT_TYPE_MEDIA_CENTER 0x08 |
| 93 | #define REPORT_TYPE_LEDS 0x0E |
| 94 | |
| 95 | /* RF Report types bitfield */ |
Benjamin Tissoires | a17dd1f | 2019-04-20 13:21:45 +0200 | [diff] [blame] | 96 | #define STD_KEYBOARD BIT(1) |
| 97 | #define STD_MOUSE BIT(2) |
| 98 | #define MULTIMEDIA BIT(3) |
| 99 | #define POWER_KEYS BIT(4) |
| 100 | #define MEDIA_CENTER BIT(8) |
| 101 | #define KBD_LEDS BIT(14) |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 102 | |
Benjamin Tissoires | c034041 | 2019-04-20 13:21:46 +0200 | [diff] [blame] | 103 | #define HIDPP_GET_LONG_REGISTER 0x83 |
| 104 | #define HIDPP_REG_PAIRING_INFORMATION 0xB5 |
| 105 | #define HIDPP_PAIRING_INFORMATION 0x20 |
| 106 | |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 107 | struct dj_report { |
| 108 | u8 report_id; |
| 109 | u8 device_index; |
| 110 | u8 report_type; |
| 111 | u8 report_params[DJREPORT_SHORT_LENGTH - 3]; |
| 112 | }; |
| 113 | |
Benjamin Tissoires | 7bb56a5 | 2019-04-20 13:21:44 +0200 | [diff] [blame] | 114 | struct hidpp_event { |
| 115 | u8 report_id; |
| 116 | u8 device_index; |
| 117 | u8 sub_id; |
| 118 | u8 params[HIDPP_REPORT_LONG_LENGTH - 3U]; |
| 119 | } __packed; |
| 120 | |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 121 | struct dj_receiver_dev { |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame^] | 122 | struct hid_device *hidpp; |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 123 | struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES + |
| 124 | DJ_DEVICE_INDEX_MIN]; |
| 125 | struct work_struct work; |
| 126 | struct kfifo notif_fifo; |
| 127 | spinlock_t lock; |
Benjamin Tissoires | 8cb3746 | 2014-09-30 13:18:26 -0400 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | struct dj_device { |
| 131 | struct hid_device *hdev; |
| 132 | struct dj_receiver_dev *dj_receiver_dev; |
| 133 | u32 reports_supported; |
| 134 | u8 device_index; |
| 135 | }; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 136 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 137 | #define WORKITEM_TYPE_EMPTY 0 |
| 138 | #define WORKITEM_TYPE_PAIRED 1 |
| 139 | #define WORKITEM_TYPE_UNPAIRED 2 |
| 140 | #define WORKITEM_TYPE_UNKNOWN 255 |
| 141 | |
| 142 | struct dj_workitem { |
| 143 | u8 type; /* WORKITEM_TYPE_* */ |
| 144 | u8 device_index; |
| 145 | u8 quad_id_msb; |
| 146 | u8 quad_id_lsb; |
| 147 | u32 reports_supported; |
| 148 | }; |
| 149 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 150 | /* Keyboard descriptor (1) */ |
| 151 | static const char kbd_descriptor[] = { |
| 152 | 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */ |
| 153 | 0x09, 0x06, /* USAGE (Keyboard) */ |
| 154 | 0xA1, 0x01, /* COLLECTION (Application) */ |
| 155 | 0x85, 0x01, /* REPORT_ID (1) */ |
| 156 | 0x95, 0x08, /* REPORT_COUNT (8) */ |
| 157 | 0x75, 0x01, /* REPORT_SIZE (1) */ |
| 158 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ |
| 159 | 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ |
| 160 | 0x05, 0x07, /* USAGE_PAGE (Keyboard) */ |
| 161 | 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */ |
| 162 | 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */ |
| 163 | 0x81, 0x02, /* INPUT (Data,Var,Abs) */ |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 164 | 0x95, 0x06, /* REPORT_COUNT (6) */ |
| 165 | 0x75, 0x08, /* REPORT_SIZE (8) */ |
| 166 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ |
| 167 | 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */ |
| 168 | 0x05, 0x07, /* USAGE_PAGE (Keyboard) */ |
| 169 | 0x19, 0x00, /* USAGE_MINIMUM (no event) */ |
| 170 | 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */ |
| 171 | 0x81, 0x00, /* INPUT (Data,Ary,Abs) */ |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 172 | 0x85, 0x0e, /* REPORT_ID (14) */ |
| 173 | 0x05, 0x08, /* USAGE PAGE (LED page) */ |
| 174 | 0x95, 0x05, /* REPORT COUNT (5) */ |
| 175 | 0x75, 0x01, /* REPORT SIZE (1) */ |
| 176 | 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ |
| 177 | 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ |
| 178 | 0x19, 0x01, /* USAGE MINIMUM (1) */ |
| 179 | 0x29, 0x05, /* USAGE MAXIMUM (5) */ |
| 180 | 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */ |
| 181 | 0x95, 0x01, /* REPORT COUNT (1) */ |
| 182 | 0x75, 0x03, /* REPORT SIZE (3) */ |
| 183 | 0x91, 0x01, /* OUTPUT (Constant) */ |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 184 | 0xC0 |
| 185 | }; |
| 186 | |
| 187 | /* Mouse descriptor (2) */ |
| 188 | static const char mse_descriptor[] = { |
| 189 | 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ |
| 190 | 0x09, 0x02, /* USAGE (Mouse) */ |
| 191 | 0xA1, 0x01, /* COLLECTION (Application) */ |
| 192 | 0x85, 0x02, /* REPORT_ID = 2 */ |
| 193 | 0x09, 0x01, /* USAGE (pointer) */ |
| 194 | 0xA1, 0x00, /* COLLECTION (physical) */ |
| 195 | 0x05, 0x09, /* USAGE_PAGE (buttons) */ |
| 196 | 0x19, 0x01, /* USAGE_MIN (1) */ |
| 197 | 0x29, 0x10, /* USAGE_MAX (16) */ |
| 198 | 0x15, 0x00, /* LOGICAL_MIN (0) */ |
| 199 | 0x25, 0x01, /* LOGICAL_MAX (1) */ |
| 200 | 0x95, 0x10, /* REPORT_COUNT (16) */ |
| 201 | 0x75, 0x01, /* REPORT_SIZE (1) */ |
| 202 | 0x81, 0x02, /* INPUT (data var abs) */ |
| 203 | 0x05, 0x01, /* USAGE_PAGE (generic desktop) */ |
| 204 | 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */ |
| 205 | 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */ |
| 206 | 0x75, 0x0C, /* REPORT_SIZE (12) */ |
| 207 | 0x95, 0x02, /* REPORT_COUNT (2) */ |
| 208 | 0x09, 0x30, /* USAGE (X) */ |
| 209 | 0x09, 0x31, /* USAGE (Y) */ |
| 210 | 0x81, 0x06, /* INPUT */ |
| 211 | 0x15, 0x81, /* LOGICAL_MIN (-127) */ |
| 212 | 0x25, 0x7F, /* LOGICAL_MAX (127) */ |
| 213 | 0x75, 0x08, /* REPORT_SIZE (8) */ |
| 214 | 0x95, 0x01, /* REPORT_COUNT (1) */ |
| 215 | 0x09, 0x38, /* USAGE (wheel) */ |
| 216 | 0x81, 0x06, /* INPUT */ |
| 217 | 0x05, 0x0C, /* USAGE_PAGE(consumer) */ |
| 218 | 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */ |
| 219 | 0x95, 0x01, /* REPORT_COUNT (1) */ |
| 220 | 0x81, 0x06, /* INPUT */ |
| 221 | 0xC0, /* END_COLLECTION */ |
| 222 | 0xC0, /* END_COLLECTION */ |
| 223 | }; |
| 224 | |
| 225 | /* Consumer Control descriptor (3) */ |
| 226 | static const char consumer_descriptor[] = { |
| 227 | 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */ |
| 228 | 0x09, 0x01, /* USAGE (Consumer Control) */ |
| 229 | 0xA1, 0x01, /* COLLECTION (Application) */ |
| 230 | 0x85, 0x03, /* REPORT_ID = 3 */ |
| 231 | 0x75, 0x10, /* REPORT_SIZE (16) */ |
| 232 | 0x95, 0x02, /* REPORT_COUNT (2) */ |
| 233 | 0x15, 0x01, /* LOGICAL_MIN (1) */ |
| 234 | 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */ |
| 235 | 0x19, 0x01, /* USAGE_MIN (1) */ |
| 236 | 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */ |
| 237 | 0x81, 0x00, /* INPUT (Data Ary Abs) */ |
| 238 | 0xC0, /* END_COLLECTION */ |
| 239 | }; /* */ |
| 240 | |
| 241 | /* System control descriptor (4) */ |
| 242 | static const char syscontrol_descriptor[] = { |
| 243 | 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ |
| 244 | 0x09, 0x80, /* USAGE (System Control) */ |
| 245 | 0xA1, 0x01, /* COLLECTION (Application) */ |
| 246 | 0x85, 0x04, /* REPORT_ID = 4 */ |
| 247 | 0x75, 0x02, /* REPORT_SIZE (2) */ |
| 248 | 0x95, 0x01, /* REPORT_COUNT (1) */ |
| 249 | 0x15, 0x01, /* LOGICAL_MIN (1) */ |
| 250 | 0x25, 0x03, /* LOGICAL_MAX (3) */ |
| 251 | 0x09, 0x82, /* USAGE (System Sleep) */ |
| 252 | 0x09, 0x81, /* USAGE (System Power Down) */ |
| 253 | 0x09, 0x83, /* USAGE (System Wake Up) */ |
| 254 | 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */ |
| 255 | 0x75, 0x06, /* REPORT_SIZE (6) */ |
| 256 | 0x81, 0x03, /* INPUT (Cnst Var Abs) */ |
| 257 | 0xC0, /* END_COLLECTION */ |
| 258 | }; |
| 259 | |
| 260 | /* Media descriptor (8) */ |
| 261 | static const char media_descriptor[] = { |
| 262 | 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */ |
| 263 | 0x09, 0x88, /* Usage 0x0088 */ |
| 264 | 0xa1, 0x01, /* BeginCollection */ |
| 265 | 0x85, 0x08, /* Report ID 8 */ |
| 266 | 0x19, 0x01, /* Usage Min 0x0001 */ |
| 267 | 0x29, 0xff, /* Usage Max 0x00ff */ |
| 268 | 0x15, 0x01, /* Logical Min 1 */ |
| 269 | 0x26, 0xff, 0x00, /* Logical Max 255 */ |
| 270 | 0x75, 0x08, /* Report Size 8 */ |
| 271 | 0x95, 0x01, /* Report Count 1 */ |
| 272 | 0x81, 0x00, /* Input */ |
| 273 | 0xc0, /* EndCollection */ |
| 274 | }; /* */ |
| 275 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 276 | /* HIDPP descriptor */ |
| 277 | static const char hidpp_descriptor[] = { |
| 278 | 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ |
| 279 | 0x09, 0x01, /* Usage (Vendor Usage 1) */ |
| 280 | 0xa1, 0x01, /* Collection (Application) */ |
| 281 | 0x85, 0x10, /* Report ID (16) */ |
| 282 | 0x75, 0x08, /* Report Size (8) */ |
| 283 | 0x95, 0x06, /* Report Count (6) */ |
| 284 | 0x15, 0x00, /* Logical Minimum (0) */ |
| 285 | 0x26, 0xff, 0x00, /* Logical Maximum (255) */ |
| 286 | 0x09, 0x01, /* Usage (Vendor Usage 1) */ |
| 287 | 0x81, 0x00, /* Input (Data,Arr,Abs) */ |
| 288 | 0x09, 0x01, /* Usage (Vendor Usage 1) */ |
| 289 | 0x91, 0x00, /* Output (Data,Arr,Abs) */ |
| 290 | 0xc0, /* End Collection */ |
| 291 | 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ |
| 292 | 0x09, 0x02, /* Usage (Vendor Usage 2) */ |
| 293 | 0xa1, 0x01, /* Collection (Application) */ |
| 294 | 0x85, 0x11, /* Report ID (17) */ |
| 295 | 0x75, 0x08, /* Report Size (8) */ |
| 296 | 0x95, 0x13, /* Report Count (19) */ |
| 297 | 0x15, 0x00, /* Logical Minimum (0) */ |
| 298 | 0x26, 0xff, 0x00, /* Logical Maximum (255) */ |
| 299 | 0x09, 0x02, /* Usage (Vendor Usage 2) */ |
| 300 | 0x81, 0x00, /* Input (Data,Arr,Abs) */ |
| 301 | 0x09, 0x02, /* Usage (Vendor Usage 2) */ |
| 302 | 0x91, 0x00, /* Output (Data,Arr,Abs) */ |
| 303 | 0xc0, /* End Collection */ |
| 304 | 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */ |
| 305 | 0x09, 0x04, /* Usage (Vendor Usage 0x04) */ |
| 306 | 0xa1, 0x01, /* Collection (Application) */ |
| 307 | 0x85, 0x20, /* Report ID (32) */ |
| 308 | 0x75, 0x08, /* Report Size (8) */ |
| 309 | 0x95, 0x0e, /* Report Count (14) */ |
| 310 | 0x15, 0x00, /* Logical Minimum (0) */ |
| 311 | 0x26, 0xff, 0x00, /* Logical Maximum (255) */ |
| 312 | 0x09, 0x41, /* Usage (Vendor Usage 0x41) */ |
| 313 | 0x81, 0x00, /* Input (Data,Arr,Abs) */ |
| 314 | 0x09, 0x41, /* Usage (Vendor Usage 0x41) */ |
| 315 | 0x91, 0x00, /* Output (Data,Arr,Abs) */ |
| 316 | 0x85, 0x21, /* Report ID (33) */ |
| 317 | 0x95, 0x1f, /* Report Count (31) */ |
| 318 | 0x15, 0x00, /* Logical Minimum (0) */ |
| 319 | 0x26, 0xff, 0x00, /* Logical Maximum (255) */ |
| 320 | 0x09, 0x42, /* Usage (Vendor Usage 0x42) */ |
| 321 | 0x81, 0x00, /* Input (Data,Arr,Abs) */ |
| 322 | 0x09, 0x42, /* Usage (Vendor Usage 0x42) */ |
| 323 | 0x91, 0x00, /* Output (Data,Arr,Abs) */ |
| 324 | 0xc0, /* End Collection */ |
| 325 | }; |
| 326 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 327 | /* Maximum size of all defined hid reports in bytes (including report id) */ |
| 328 | #define MAX_REPORT_SIZE 8 |
| 329 | |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 330 | /* Make sure all descriptors are present here */ |
| 331 | #define MAX_RDESC_SIZE \ |
| 332 | (sizeof(kbd_descriptor) + \ |
| 333 | sizeof(mse_descriptor) + \ |
| 334 | sizeof(consumer_descriptor) + \ |
| 335 | sizeof(syscontrol_descriptor) + \ |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 336 | sizeof(media_descriptor) + \ |
| 337 | sizeof(hidpp_descriptor)) |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 338 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 339 | /* Number of possible hid report types that can be created by this driver. |
| 340 | * |
| 341 | * Right now, RF report types have the same report types (or report id's) |
| 342 | * than the hid report created from those RF reports. In the future |
| 343 | * this doesnt have to be true. |
| 344 | * |
| 345 | * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds |
| 346 | * to hid report id 0x01, this is standard keyboard. Same thing applies to mice |
| 347 | * reports and consumer control, etc. If a new RF report is created, it doesn't |
| 348 | * has to have the same report id as its corresponding hid report, so an |
| 349 | * translation may have to take place for future report types. |
| 350 | */ |
| 351 | #define NUMBER_OF_HID_REPORTS 32 |
| 352 | static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = { |
| 353 | [1] = 8, /* Standard keyboard */ |
| 354 | [2] = 8, /* Standard mouse */ |
| 355 | [3] = 5, /* Consumer control */ |
| 356 | [4] = 2, /* System control */ |
| 357 | [8] = 2, /* Media Center */ |
| 358 | }; |
| 359 | |
| 360 | |
| 361 | #define LOGITECH_DJ_INTERFACE_NUMBER 0x02 |
| 362 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 363 | static struct hid_ll_driver logi_dj_ll_driver; |
| 364 | |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 365 | static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 366 | |
| 367 | 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] | 368 | struct dj_workitem *workitem) |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 369 | { |
| 370 | /* Called in delayed work context */ |
| 371 | struct dj_device *dj_dev; |
| 372 | unsigned long flags; |
| 373 | |
| 374 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 375 | dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index]; |
| 376 | djrcv_dev->paired_dj_devices[workitem->device_index] = NULL; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 377 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 378 | |
| 379 | if (dj_dev != NULL) { |
| 380 | hid_destroy_device(dj_dev->hdev); |
| 381 | kfree(dj_dev); |
| 382 | } else { |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame^] | 383 | 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] | 384 | __func__); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | 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] | 389 | struct dj_workitem *workitem) |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 390 | { |
| 391 | /* Called in delayed work context */ |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame^] | 392 | struct hid_device *djrcv_hdev = djrcv_dev->hidpp; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 393 | struct hid_device *dj_hiddev; |
| 394 | struct dj_device *dj_dev; |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 395 | u8 device_index = workitem->device_index; |
Hans de Goede | f41d766 | 2019-04-20 13:21:50 +0200 | [diff] [blame] | 396 | unsigned long flags; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 397 | |
| 398 | /* Device index goes from 1 to 6, we need 3 bytes to store the |
| 399 | * semicolon, the index, and a null terminator |
| 400 | */ |
| 401 | unsigned char tmpstr[3]; |
| 402 | |
Hans de Goede | f41d766 | 2019-04-20 13:21:50 +0200 | [diff] [blame] | 403 | /* 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] | 404 | if (djrcv_dev->paired_dj_devices[device_index]) { |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 405 | /* The device is already known. No need to reallocate it. */ |
| 406 | dbg_hid("%s: device is already known\n", __func__); |
| 407 | return; |
| 408 | } |
| 409 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 410 | dj_hiddev = hid_allocate_device(); |
| 411 | if (IS_ERR(dj_hiddev)) { |
| 412 | dev_err(&djrcv_hdev->dev, "%s: hid_allocate_device failed\n", |
| 413 | __func__); |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | dj_hiddev->ll_driver = &logi_dj_ll_driver; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 418 | |
| 419 | dj_hiddev->dev.parent = &djrcv_hdev->dev; |
| 420 | dj_hiddev->bus = BUS_USB; |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 421 | dj_hiddev->vendor = djrcv_hdev->vendor; |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 422 | dj_hiddev->product = (workitem->quad_id_msb << 8) | |
| 423 | workitem->quad_id_lsb; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 424 | snprintf(dj_hiddev->name, sizeof(dj_hiddev->name), |
Benjamin Tissoires | d610274 | 2014-09-30 13:18:25 -0400 | [diff] [blame] | 425 | "Logitech Unifying Device. Wireless PID:%04x", |
| 426 | dj_hiddev->product); |
| 427 | |
| 428 | dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 429 | |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 430 | memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys)); |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 431 | snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 432 | strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys)); |
| 433 | |
| 434 | dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL); |
| 435 | |
| 436 | if (!dj_dev) { |
| 437 | dev_err(&djrcv_hdev->dev, "%s: failed allocating dj_device\n", |
| 438 | __func__); |
| 439 | goto dj_device_allocate_fail; |
| 440 | } |
| 441 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 442 | dj_dev->reports_supported = workitem->reports_supported; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 443 | dj_dev->hdev = dj_hiddev; |
| 444 | dj_dev->dj_receiver_dev = djrcv_dev; |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 445 | dj_dev->device_index = device_index; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 446 | dj_hiddev->driver_data = dj_dev; |
| 447 | |
Hans de Goede | f41d766 | 2019-04-20 13:21:50 +0200 | [diff] [blame] | 448 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 449 | djrcv_dev->paired_dj_devices[device_index] = dj_dev; |
Hans de Goede | f41d766 | 2019-04-20 13:21:50 +0200 | [diff] [blame] | 450 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 451 | |
| 452 | if (hid_add_device(dj_hiddev)) { |
| 453 | dev_err(&djrcv_hdev->dev, "%s: failed adding dj_device\n", |
| 454 | __func__); |
| 455 | goto hid_add_device_fail; |
| 456 | } |
| 457 | |
| 458 | return; |
| 459 | |
| 460 | hid_add_device_fail: |
Hans de Goede | f41d766 | 2019-04-20 13:21:50 +0200 | [diff] [blame] | 461 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 462 | djrcv_dev->paired_dj_devices[device_index] = NULL; |
Hans de Goede | f41d766 | 2019-04-20 13:21:50 +0200 | [diff] [blame] | 463 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 464 | kfree(dj_dev); |
| 465 | dj_device_allocate_fail: |
| 466 | hid_destroy_device(dj_hiddev); |
| 467 | } |
| 468 | |
| 469 | static void delayedwork_callback(struct work_struct *work) |
| 470 | { |
| 471 | struct dj_receiver_dev *djrcv_dev = |
| 472 | container_of(work, struct dj_receiver_dev, work); |
| 473 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 474 | struct dj_workitem workitem; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 475 | unsigned long flags; |
| 476 | int count; |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 477 | int retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 478 | |
| 479 | dbg_hid("%s\n", __func__); |
| 480 | |
| 481 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
| 482 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 483 | count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 484 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 485 | if (count != sizeof(workitem)) { |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame^] | 486 | hid_err(djrcv_dev->hidpp, "delayedwork queued without workitems available\n"); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 487 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | if (!kfifo_is_empty(&djrcv_dev->notif_fifo)) { |
| 492 | if (schedule_work(&djrcv_dev->work) == 0) { |
| 493 | dbg_hid("%s: did not schedule the work item, was " |
| 494 | "already queued\n", __func__); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 499 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 500 | switch (workitem.type) { |
| 501 | case WORKITEM_TYPE_PAIRED: |
| 502 | logi_dj_recv_add_djhid_device(djrcv_dev, &workitem); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 503 | break; |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 504 | case WORKITEM_TYPE_UNPAIRED: |
| 505 | logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem); |
| 506 | break; |
| 507 | case WORKITEM_TYPE_UNKNOWN: |
| 508 | retval = logi_dj_recv_query_paired_devices(djrcv_dev); |
| 509 | if (retval) { |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame^] | 510 | 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] | 511 | __func__, retval); |
| 512 | } |
| 513 | break; |
| 514 | case WORKITEM_TYPE_EMPTY: |
| 515 | dbg_hid("%s: device list is empty\n", __func__); |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev, |
| 521 | struct dj_report *dj_report) |
| 522 | { |
| 523 | /* We are called from atomic context (tasklet && djrcv->lock held) */ |
| 524 | struct dj_workitem workitem = { |
| 525 | .device_index = dj_report->device_index, |
| 526 | }; |
| 527 | |
| 528 | switch (dj_report->report_type) { |
| 529 | case REPORT_TYPE_NOTIF_DEVICE_PAIRED: |
| 530 | workitem.type = WORKITEM_TYPE_PAIRED; |
| 531 | if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] & |
| 532 | SPFUNCTION_DEVICE_LIST_EMPTY) { |
| 533 | workitem.type = WORKITEM_TYPE_EMPTY; |
| 534 | break; |
| 535 | } |
| 536 | /* fall-through */ |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 537 | case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED: |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 538 | workitem.quad_id_msb = |
| 539 | dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB]; |
| 540 | workitem.quad_id_lsb = |
| 541 | dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB]; |
| 542 | workitem.reports_supported = get_unaligned_le32( |
| 543 | dj_report->report_params + |
| 544 | DEVICE_PAIRED_RF_REPORT_TYPE); |
| 545 | if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED) |
| 546 | workitem.type = WORKITEM_TYPE_UNPAIRED; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 547 | break; |
| 548 | default: |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 549 | /* A normal report (i. e. not belonging to a pair/unpair notification) |
| 550 | * arriving here, means that the report arrived but we did not have a |
| 551 | * paired dj_device associated to the report's device_index, this |
| 552 | * means that the original "device paired" notification corresponding |
| 553 | * to this dj_device never arrived to this driver. The reason is that |
| 554 | * hid-core discards all packets coming from a device while probe() is |
| 555 | * executing. */ |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 556 | if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) { |
| 557 | /* |
| 558 | * ok, we don't know the device, just re-ask the |
| 559 | * receiver for the list of connected devices in |
| 560 | * the delayed work callback. |
| 561 | */ |
| 562 | workitem.type = WORKITEM_TYPE_UNKNOWN; |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 563 | } |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 564 | } |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 565 | |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 566 | kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 567 | |
| 568 | if (schedule_work(&djrcv_dev->work) == 0) { |
| 569 | dbg_hid("%s: did not schedule the work item, was already " |
| 570 | "queued\n", __func__); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev, |
| 575 | struct dj_report *dj_report) |
| 576 | { |
| 577 | /* We are called from atomic context (tasklet && djrcv->lock held) */ |
| 578 | unsigned int i; |
| 579 | u8 reportbuffer[MAX_REPORT_SIZE]; |
| 580 | struct dj_device *djdev; |
| 581 | |
| 582 | djdev = djrcv_dev->paired_dj_devices[dj_report->device_index]; |
| 583 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 584 | memset(reportbuffer, 0, sizeof(reportbuffer)); |
| 585 | |
| 586 | for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) { |
| 587 | if (djdev->reports_supported & (1 << i)) { |
| 588 | reportbuffer[0] = i; |
| 589 | if (hid_input_report(djdev->hdev, |
| 590 | HID_INPUT_REPORT, |
| 591 | reportbuffer, |
| 592 | hid_reportid_size_map[i], 1)) { |
| 593 | dbg_hid("hid_input_report error sending null " |
| 594 | "report\n"); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
Benjamin Tissoires | 8389823 | 2019-04-20 13:21:43 +0200 | [diff] [blame] | 600 | static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev, |
| 601 | struct dj_report *dj_report) |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 602 | { |
| 603 | /* We are called from atomic context (tasklet && djrcv->lock held) */ |
| 604 | struct dj_device *dj_device; |
| 605 | |
| 606 | dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index]; |
| 607 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 608 | if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) || |
| 609 | (hid_reportid_size_map[dj_report->report_type] == 0)) { |
| 610 | dbg_hid("invalid report type:%x\n", dj_report->report_type); |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | if (hid_input_report(dj_device->hdev, |
| 615 | HID_INPUT_REPORT, &dj_report->report_type, |
| 616 | hid_reportid_size_map[dj_report->report_type], 1)) { |
| 617 | dbg_hid("hid_input_report error\n"); |
| 618 | } |
| 619 | } |
| 620 | |
Benjamin Tissoires | 8389823 | 2019-04-20 13:21:43 +0200 | [diff] [blame] | 621 | static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data, |
| 622 | int size) |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 623 | { |
| 624 | /* We are called from atomic context (tasklet && djrcv->lock held) */ |
| 625 | if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1)) |
| 626 | dbg_hid("hid_input_report error\n"); |
| 627 | } |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 628 | |
| 629 | static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev, |
| 630 | struct dj_report *dj_report) |
| 631 | { |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame^] | 632 | struct hid_device *hdev = djrcv_dev->hidpp; |
Benjamin Tissoires | dcd9006 | 2013-03-05 17:09:00 +0100 | [diff] [blame] | 633 | struct hid_report *report; |
| 634 | struct hid_report_enum *output_report_enum; |
| 635 | u8 *data = (u8 *)(&dj_report->device_index); |
Kees Cook | 297502a | 2013-09-11 21:56:56 +0200 | [diff] [blame] | 636 | unsigned int i; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 637 | |
Benjamin Tissoires | dcd9006 | 2013-03-05 17:09:00 +0100 | [diff] [blame] | 638 | output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT]; |
| 639 | report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT]; |
| 640 | |
| 641 | if (!report) { |
| 642 | dev_err(&hdev->dev, "%s: unable to find dj report\n", __func__); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 643 | return -ENODEV; |
| 644 | } |
| 645 | |
Kees Cook | 297502a | 2013-09-11 21:56:56 +0200 | [diff] [blame] | 646 | for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++) |
Benjamin Tissoires | dcd9006 | 2013-03-05 17:09:00 +0100 | [diff] [blame] | 647 | report->field[0]->value[i] = data[i]; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 648 | |
Jiri Kosina | 83a44ac | 2013-03-09 10:58:13 +0100 | [diff] [blame] | 649 | hid_hw_request(hdev, report, HID_REQ_SET_REPORT); |
Benjamin Tissoires | dcd9006 | 2013-03-05 17:09:00 +0100 | [diff] [blame] | 650 | |
| 651 | return 0; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev) |
| 655 | { |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 656 | struct dj_report *dj_report; |
| 657 | int retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 658 | |
Alan Cox | 8a55ade | 2012-09-04 15:10:08 +0100 | [diff] [blame] | 659 | dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL); |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 660 | if (!dj_report) |
| 661 | return -ENOMEM; |
| 662 | dj_report->report_id = REPORT_ID_DJ_SHORT; |
| 663 | dj_report->device_index = 0xFF; |
| 664 | dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES; |
| 665 | retval = logi_dj_recv_send_report(djrcv_dev, dj_report); |
| 666 | kfree(dj_report); |
| 667 | return retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 668 | } |
| 669 | |
Nestor Lopez Casado | c63e0e3 | 2013-07-18 06:21:30 -0700 | [diff] [blame] | 670 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 671 | static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev, |
| 672 | unsigned timeout) |
| 673 | { |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame^] | 674 | struct hid_device *hdev = djrcv_dev->hidpp; |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 675 | struct dj_report *dj_report; |
Benjamin Tissoires | 6a9ddc8 | 2014-09-30 13:18:31 -0400 | [diff] [blame] | 676 | u8 *buf; |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 677 | int retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 678 | |
Alan Cox | 8a55ade | 2012-09-04 15:10:08 +0100 | [diff] [blame] | 679 | dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL); |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 680 | if (!dj_report) |
| 681 | return -ENOMEM; |
| 682 | dj_report->report_id = REPORT_ID_DJ_SHORT; |
| 683 | dj_report->device_index = 0xFF; |
| 684 | dj_report->report_type = REPORT_TYPE_CMD_SWITCH; |
| 685 | dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F; |
| 686 | dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] = (u8)timeout; |
| 687 | retval = logi_dj_recv_send_report(djrcv_dev, dj_report); |
Benjamin Tisssoires | 42c22db | 2014-01-08 17:18:45 -0500 | [diff] [blame] | 688 | |
| 689 | /* |
| 690 | * Ugly sleep to work around a USB 3.0 bug when the receiver is still |
| 691 | * processing the "switch-to-dj" command while we send an other command. |
| 692 | * 50 msec should gives enough time to the receiver to be ready. |
| 693 | */ |
| 694 | msleep(50); |
| 695 | |
Benjamin Tissoires | 6a9ddc8 | 2014-09-30 13:18:31 -0400 | [diff] [blame] | 696 | /* |
| 697 | * Magical bits to set up hidpp notifications when the dj devices |
| 698 | * are connected/disconnected. |
| 699 | * |
| 700 | * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller |
| 701 | * than DJREPORT_SHORT_LENGTH. |
| 702 | */ |
| 703 | buf = (u8 *)dj_report; |
| 704 | |
| 705 | memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH); |
| 706 | |
| 707 | buf[0] = REPORT_ID_HIDPP_SHORT; |
| 708 | buf[1] = 0xFF; |
| 709 | buf[2] = 0x80; |
| 710 | buf[3] = 0x00; |
| 711 | buf[4] = 0x00; |
| 712 | buf[5] = 0x09; |
| 713 | buf[6] = 0x00; |
| 714 | |
| 715 | hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf, |
| 716 | HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT, |
| 717 | HID_REQ_SET_REPORT); |
| 718 | |
| 719 | kfree(dj_report); |
Marc Dionne | d8dc349 | 2012-06-01 18:12:14 -0400 | [diff] [blame] | 720 | return retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | |
| 724 | static int logi_dj_ll_open(struct hid_device *hid) |
| 725 | { |
| 726 | dbg_hid("%s:%s\n", __func__, hid->phys); |
| 727 | return 0; |
| 728 | |
| 729 | } |
| 730 | |
| 731 | static void logi_dj_ll_close(struct hid_device *hid) |
| 732 | { |
| 733 | dbg_hid("%s:%s\n", __func__, hid->phys); |
| 734 | } |
| 735 | |
Benjamin Tissoires | 8dba302 | 2017-03-27 16:59:21 +0200 | [diff] [blame] | 736 | /* |
| 737 | * Register 0xB5 is "pairing information". It is solely intended for the |
| 738 | * receiver, so do not overwrite the device index. |
| 739 | */ |
Benjamin Tissoires | c034041 | 2019-04-20 13:21:46 +0200 | [diff] [blame] | 740 | static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT, |
| 741 | HIDPP_RECEIVER_INDEX, |
| 742 | HIDPP_GET_LONG_REGISTER, |
| 743 | HIDPP_REG_PAIRING_INFORMATION }; |
| 744 | static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG, |
| 745 | HIDPP_RECEIVER_INDEX, |
| 746 | HIDPP_GET_LONG_REGISTER, |
| 747 | HIDPP_REG_PAIRING_INFORMATION }; |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 748 | |
Benjamin Tissoires | bd27e20 | 2014-02-10 12:58:53 -0500 | [diff] [blame] | 749 | static int logi_dj_ll_raw_request(struct hid_device *hid, |
| 750 | unsigned char reportnum, __u8 *buf, |
| 751 | size_t count, unsigned char report_type, |
| 752 | int reqtype) |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 753 | { |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 754 | struct dj_device *djdev = hid->driver_data; |
| 755 | struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev; |
| 756 | u8 *out_buf; |
| 757 | int ret; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 758 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 759 | if ((buf[0] == REPORT_ID_HIDPP_SHORT) || |
| 760 | (buf[0] == REPORT_ID_HIDPP_LONG)) { |
| 761 | if (count < 2) |
| 762 | return -EINVAL; |
| 763 | |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 764 | /* special case where we should not overwrite |
| 765 | * the device_index */ |
Benjamin Tissoires | 8dba302 | 2017-03-27 16:59:21 +0200 | [diff] [blame] | 766 | if (count == 7 && !memcmp(buf, unifying_pairing_query, |
| 767 | sizeof(unifying_pairing_query))) |
| 768 | buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1); |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 769 | else |
| 770 | buf[1] = djdev->device_index; |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame^] | 771 | return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf, |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 772 | count, report_type, reqtype); |
| 773 | } |
| 774 | |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 775 | if (buf[0] != REPORT_TYPE_LEDS) |
| 776 | return -EINVAL; |
| 777 | |
| 778 | out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC); |
| 779 | if (!out_buf) |
| 780 | return -ENOMEM; |
| 781 | |
Jiri Kosina | 51217e6 | 2014-08-21 09:56:47 -0500 | [diff] [blame] | 782 | if (count > DJREPORT_SHORT_LENGTH - 2) |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 783 | count = DJREPORT_SHORT_LENGTH - 2; |
| 784 | |
| 785 | out_buf[0] = REPORT_ID_DJ_SHORT; |
| 786 | out_buf[1] = djdev->device_index; |
| 787 | memcpy(out_buf + 2, buf, count); |
| 788 | |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame^] | 789 | 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] | 790 | DJREPORT_SHORT_LENGTH, report_type, reqtype); |
Benjamin Tissoires | 0e40d35 | 2014-02-05 16:33:17 -0500 | [diff] [blame] | 791 | |
| 792 | kfree(out_buf); |
| 793 | return ret; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 794 | } |
| 795 | |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 796 | 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] | 797 | { |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 798 | memcpy(rdesc + *rsize, data, size); |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 799 | *rsize += size; |
| 800 | } |
| 801 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 802 | static int logi_dj_ll_parse(struct hid_device *hid) |
| 803 | { |
| 804 | struct dj_device *djdev = hid->driver_data; |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 805 | unsigned int rsize = 0; |
| 806 | char *rdesc; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 807 | int retval; |
| 808 | |
| 809 | dbg_hid("%s\n", __func__); |
| 810 | |
| 811 | djdev->hdev->version = 0x0111; |
| 812 | djdev->hdev->country = 0x00; |
| 813 | |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 814 | rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL); |
| 815 | if (!rdesc) |
| 816 | return -ENOMEM; |
| 817 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 818 | if (djdev->reports_supported & STD_KEYBOARD) { |
| 819 | dbg_hid("%s: sending a kbd descriptor, reports_supported: %x\n", |
| 820 | __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 821 | rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | if (djdev->reports_supported & STD_MOUSE) { |
| 825 | dbg_hid("%s: sending a mouse descriptor, reports_supported: " |
| 826 | "%x\n", __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 827 | rdcat(rdesc, &rsize, mse_descriptor, sizeof(mse_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | if (djdev->reports_supported & MULTIMEDIA) { |
| 831 | dbg_hid("%s: sending a multimedia report descriptor: %x\n", |
| 832 | __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 833 | rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | if (djdev->reports_supported & POWER_KEYS) { |
| 837 | dbg_hid("%s: sending a power keys report descriptor: %x\n", |
| 838 | __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 839 | rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | if (djdev->reports_supported & MEDIA_CENTER) { |
| 843 | dbg_hid("%s: sending a media center report descriptor: %x\n", |
| 844 | __func__, djdev->reports_supported); |
Dan Carpenter | 6d60332 | 2013-10-19 12:08:59 +0300 | [diff] [blame] | 845 | rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor)); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | if (djdev->reports_supported & KBD_LEDS) { |
| 849 | dbg_hid("%s: need to send kbd leds report descriptor: %x\n", |
| 850 | __func__, djdev->reports_supported); |
| 851 | } |
| 852 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 853 | rdcat(rdesc, &rsize, hidpp_descriptor, sizeof(hidpp_descriptor)); |
| 854 | |
Henrik Rydberg | 2a039bf | 2012-04-22 14:21:39 +0200 | [diff] [blame] | 855 | retval = hid_parse_report(hid, rdesc, rsize); |
| 856 | kfree(rdesc); |
| 857 | |
| 858 | return retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 859 | } |
| 860 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 861 | static int logi_dj_ll_start(struct hid_device *hid) |
| 862 | { |
| 863 | dbg_hid("%s\n", __func__); |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | static void logi_dj_ll_stop(struct hid_device *hid) |
| 868 | { |
| 869 | dbg_hid("%s\n", __func__); |
| 870 | } |
| 871 | |
| 872 | |
| 873 | static struct hid_ll_driver logi_dj_ll_driver = { |
| 874 | .parse = logi_dj_ll_parse, |
| 875 | .start = logi_dj_ll_start, |
| 876 | .stop = logi_dj_ll_stop, |
| 877 | .open = logi_dj_ll_open, |
| 878 | .close = logi_dj_ll_close, |
Benjamin Tissoires | bd27e20 | 2014-02-10 12:58:53 -0500 | [diff] [blame] | 879 | .raw_request = logi_dj_ll_raw_request, |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 880 | }; |
| 881 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 882 | static int logi_dj_dj_event(struct hid_device *hdev, |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 883 | struct hid_report *report, u8 *data, |
| 884 | int size) |
| 885 | { |
| 886 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
| 887 | struct dj_report *dj_report = (struct dj_report *) data; |
| 888 | unsigned long flags; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 889 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 890 | /* |
| 891 | * 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] | 892 | * |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 893 | * 1) Data is intended for this driver i. e. data contains arrival, |
| 894 | * departure, etc notifications, in which case we queue them for delayed |
| 895 | * processing by the work queue. We return 1 to hid-core as no further |
| 896 | * processing is required from it. |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 897 | * |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 898 | * 2) Data informs a connection change, if the change means rf link |
| 899 | * loss, then we must send a null report to the upper layer to discard |
| 900 | * potentially pressed keys that may be repeated forever by the input |
| 901 | * 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] | 902 | * |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 903 | * 3) Data is an actual input event from a paired DJ device in which |
| 904 | * case we forward it to the correct hid device (via hid_input_report() |
| 905 | * ) 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] | 906 | */ |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 907 | |
Jiri Kosina | ad3e14d | 2014-08-21 09:57:17 -0500 | [diff] [blame] | 908 | if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) || |
| 909 | (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) { |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 910 | /* |
| 911 | * Device index is wrong, bail out. |
| 912 | * This driver can ignore safely the receiver notifications, |
| 913 | * so ignore those reports too. |
| 914 | */ |
| 915 | if (dj_report->device_index != DJ_RECEIVER_INDEX) |
| 916 | dev_err(&hdev->dev, "%s: invalid device index:%d\n", |
Jiri Kosina | ad3e14d | 2014-08-21 09:57:17 -0500 | [diff] [blame] | 917 | __func__, dj_report->device_index); |
| 918 | return false; |
| 919 | } |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 920 | |
| 921 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
Benjamin Tissoires | 368d4e59 | 2014-08-22 16:16:06 -0400 | [diff] [blame] | 922 | |
| 923 | if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) { |
| 924 | /* received an event for an unknown device, bail out */ |
| 925 | logi_dj_recv_queue_notification(djrcv_dev, dj_report); |
| 926 | goto out; |
| 927 | } |
| 928 | |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 929 | switch (dj_report->report_type) { |
| 930 | case REPORT_TYPE_NOTIF_DEVICE_PAIRED: |
Benjamin Tissoires | 368d4e59 | 2014-08-22 16:16:06 -0400 | [diff] [blame] | 931 | /* pairing notifications are handled above the switch */ |
| 932 | break; |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 933 | case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED: |
| 934 | logi_dj_recv_queue_notification(djrcv_dev, dj_report); |
| 935 | break; |
| 936 | case REPORT_TYPE_NOTIF_CONNECTION_STATUS: |
| 937 | if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] == |
| 938 | STATUS_LINKLOSS) { |
| 939 | logi_dj_recv_forward_null_report(djrcv_dev, dj_report); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 940 | } |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 941 | break; |
| 942 | default: |
Benjamin Tissoires | 8389823 | 2019-04-20 13:21:43 +0200 | [diff] [blame] | 943 | logi_dj_recv_forward_dj(djrcv_dev, dj_report); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 944 | } |
Benjamin Tissoires | 368d4e59 | 2014-08-22 16:16:06 -0400 | [diff] [blame] | 945 | |
| 946 | out: |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 947 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 948 | |
Benjamin Tissoires | 5abfe85 | 2014-08-22 16:16:05 -0400 | [diff] [blame] | 949 | return true; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 950 | } |
| 951 | |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 952 | static int logi_dj_hidpp_event(struct hid_device *hdev, |
| 953 | struct hid_report *report, u8 *data, |
| 954 | int size) |
| 955 | { |
| 956 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
Benjamin Tissoires | 7bb56a5 | 2019-04-20 13:21:44 +0200 | [diff] [blame] | 957 | struct hidpp_event *hidpp_report = (struct hidpp_event *) data; |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 958 | unsigned long flags; |
Benjamin Tissoires | 7bb56a5 | 2019-04-20 13:21:44 +0200 | [diff] [blame] | 959 | u8 device_index = hidpp_report->device_index; |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 960 | |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 961 | if (device_index == HIDPP_RECEIVER_INDEX) { |
| 962 | /* special case were the device wants to know its unifying |
| 963 | * name */ |
| 964 | if (size == HIDPP_REPORT_LONG_LENGTH && |
Benjamin Tissoires | 8dba302 | 2017-03-27 16:59:21 +0200 | [diff] [blame] | 965 | !memcmp(data, unifying_pairing_answer, |
| 966 | sizeof(unifying_pairing_answer))) |
Benjamin Tissoires | 3379782 | 2014-09-30 13:18:30 -0400 | [diff] [blame] | 967 | device_index = (data[4] & 0x0F) + 1; |
| 968 | else |
| 969 | return false; |
| 970 | } |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 971 | |
| 972 | /* |
| 973 | * Data is from the HID++ collection, in this case, we forward the |
| 974 | * data to the corresponding child dj device and return 0 to hid-core |
| 975 | * so he data also goes to the hidraw device of the receiver. This |
| 976 | * allows a user space application to implement the full HID++ routing |
| 977 | * via the receiver. |
| 978 | */ |
| 979 | |
| 980 | if ((device_index < DJ_DEVICE_INDEX_MIN) || |
| 981 | (device_index > DJ_DEVICE_INDEX_MAX)) { |
| 982 | /* |
| 983 | * Device index is wrong, bail out. |
| 984 | * This driver can ignore safely the receiver notifications, |
| 985 | * so ignore those reports too. |
| 986 | */ |
| 987 | dev_err(&hdev->dev, "%s: invalid device index:%d\n", |
Benjamin Tissoires | 7bb56a5 | 2019-04-20 13:21:44 +0200 | [diff] [blame] | 988 | __func__, hidpp_report->device_index); |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 989 | return false; |
| 990 | } |
| 991 | |
| 992 | spin_lock_irqsave(&djrcv_dev->lock, flags); |
| 993 | |
| 994 | if (!djrcv_dev->paired_dj_devices[device_index]) |
| 995 | /* received an event for an unknown device, bail out */ |
| 996 | goto out; |
| 997 | |
Benjamin Tissoires | 8389823 | 2019-04-20 13:21:43 +0200 | [diff] [blame] | 998 | logi_dj_recv_forward_report(djrcv_dev->paired_dj_devices[device_index], |
| 999 | data, size); |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1000 | |
| 1001 | out: |
| 1002 | spin_unlock_irqrestore(&djrcv_dev->lock, flags); |
| 1003 | |
| 1004 | return false; |
| 1005 | } |
| 1006 | |
| 1007 | static int logi_dj_raw_event(struct hid_device *hdev, |
| 1008 | struct hid_report *report, u8 *data, |
| 1009 | int size) |
| 1010 | { |
| 1011 | dbg_hid("%s, size:%d\n", __func__, size); |
| 1012 | |
| 1013 | switch (data[0]) { |
| 1014 | case REPORT_ID_DJ_SHORT: |
Peter Wu | f254ae9 | 2014-12-16 16:55:21 +0100 | [diff] [blame] | 1015 | if (size != DJREPORT_SHORT_LENGTH) { |
| 1016 | dev_err(&hdev->dev, "DJ report of bad size (%d)", size); |
| 1017 | return false; |
| 1018 | } |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1019 | return logi_dj_dj_event(hdev, report, data, size); |
| 1020 | case REPORT_ID_HIDPP_SHORT: |
Peter Wu | f254ae9 | 2014-12-16 16:55:21 +0100 | [diff] [blame] | 1021 | if (size != HIDPP_REPORT_SHORT_LENGTH) { |
| 1022 | dev_err(&hdev->dev, |
| 1023 | "Short HID++ report of bad size (%d)", size); |
| 1024 | return false; |
| 1025 | } |
| 1026 | return logi_dj_hidpp_event(hdev, report, data, size); |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1027 | case REPORT_ID_HIDPP_LONG: |
Peter Wu | f254ae9 | 2014-12-16 16:55:21 +0100 | [diff] [blame] | 1028 | if (size != HIDPP_REPORT_LONG_LENGTH) { |
| 1029 | dev_err(&hdev->dev, |
| 1030 | "Long HID++ report of bad size (%d)", size); |
| 1031 | return false; |
| 1032 | } |
Benjamin Tissoires | 925f0f3 | 2014-09-30 13:18:29 -0400 | [diff] [blame] | 1033 | return logi_dj_hidpp_event(hdev, report, data, size); |
| 1034 | } |
| 1035 | |
| 1036 | return false; |
| 1037 | } |
| 1038 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1039 | static int logi_dj_probe(struct hid_device *hdev, |
| 1040 | const struct hid_device_id *id) |
| 1041 | { |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 1042 | struct hid_report_enum *rep_enum; |
| 1043 | struct hid_report *rep; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1044 | struct dj_receiver_dev *djrcv_dev; |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 1045 | bool has_hidpp = false; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1046 | int retval; |
| 1047 | |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 1048 | /* |
| 1049 | * Call to usbhid to fetch the HID descriptors of the current |
| 1050 | * interface subsequently call to the hid/hid-core to parse the |
| 1051 | * fetched descriptors. |
| 1052 | */ |
| 1053 | retval = hid_parse(hdev); |
| 1054 | if (retval) { |
| 1055 | dev_err(&hdev->dev, |
| 1056 | "%s:parse failed\n", __func__); |
| 1057 | return retval; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1058 | } |
| 1059 | |
Benjamin Tissoires | 82c0beb | 2019-04-20 13:21:47 +0200 | [diff] [blame] | 1060 | rep_enum = &hdev->report_enum[HID_INPUT_REPORT]; |
| 1061 | |
| 1062 | /* |
| 1063 | * Check for the HID++ application. |
| 1064 | * Note: we should theoretically check for HID++ and DJ |
| 1065 | * collections, but this will do. |
| 1066 | */ |
| 1067 | list_for_each_entry(rep, &rep_enum->report_list, list) { |
| 1068 | if (rep->application == 0xff000001) |
| 1069 | has_hidpp = true; |
| 1070 | } |
| 1071 | |
| 1072 | /* |
| 1073 | * Ignore interfaces without DJ/HID++ collection, they will not carry |
| 1074 | * any data, dont create any hid_device for them. |
| 1075 | */ |
| 1076 | if (!has_hidpp) |
| 1077 | return -ENODEV; |
| 1078 | |
| 1079 | /* Treat DJ/HID++ interface */ |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1080 | |
| 1081 | djrcv_dev = kzalloc(sizeof(struct dj_receiver_dev), GFP_KERNEL); |
| 1082 | if (!djrcv_dev) { |
| 1083 | dev_err(&hdev->dev, |
| 1084 | "%s:failed allocating dj_receiver_dev\n", __func__); |
| 1085 | return -ENOMEM; |
| 1086 | } |
Hans de Goede | 0ee7554 | 2019-04-20 13:21:51 +0200 | [diff] [blame^] | 1087 | djrcv_dev->hidpp = hdev; |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1088 | INIT_WORK(&djrcv_dev->work, delayedwork_callback); |
| 1089 | spin_lock_init(&djrcv_dev->lock); |
| 1090 | if (kfifo_alloc(&djrcv_dev->notif_fifo, |
Benjamin Tissoires | 4fcad95 | 2019-04-20 13:21:48 +0200 | [diff] [blame] | 1091 | DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem), |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1092 | GFP_KERNEL)) { |
| 1093 | dev_err(&hdev->dev, |
| 1094 | "%s:failed allocating notif_fifo\n", __func__); |
| 1095 | kfree(djrcv_dev); |
| 1096 | return -ENOMEM; |
| 1097 | } |
| 1098 | hid_set_drvdata(hdev, djrcv_dev); |
| 1099 | |
Kees Cook | 297502a | 2013-09-11 21:56:56 +0200 | [diff] [blame] | 1100 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1101 | /* Starts the usb device and connects to upper interfaces hiddev and |
| 1102 | * hidraw */ |
| 1103 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 1104 | if (retval) { |
| 1105 | dev_err(&hdev->dev, |
| 1106 | "%s:hid_hw_start returned error\n", __func__); |
| 1107 | goto hid_hw_start_fail; |
| 1108 | } |
| 1109 | |
| 1110 | retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0); |
| 1111 | if (retval < 0) { |
| 1112 | dev_err(&hdev->dev, |
| 1113 | "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n", |
| 1114 | __func__, retval); |
| 1115 | goto switch_to_dj_mode_fail; |
| 1116 | } |
| 1117 | |
| 1118 | /* This is enabling the polling urb on the IN endpoint */ |
Benjamin Tissoires | ddf7540 | 2013-07-12 11:01:02 +0200 | [diff] [blame] | 1119 | retval = hid_hw_open(hdev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1120 | if (retval < 0) { |
Benjamin Tissoires | ddf7540 | 2013-07-12 11:01:02 +0200 | [diff] [blame] | 1121 | dev_err(&hdev->dev, "%s:hid_hw_open returned error:%d\n", |
| 1122 | __func__, retval); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1123 | goto llopen_failed; |
| 1124 | } |
| 1125 | |
Andrew de los Reyes | a9dd22b7 | 2013-02-18 09:20:22 -0800 | [diff] [blame] | 1126 | /* Allow incoming packets to arrive: */ |
| 1127 | hid_device_io_start(hdev); |
| 1128 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1129 | retval = logi_dj_recv_query_paired_devices(djrcv_dev); |
| 1130 | if (retval < 0) { |
| 1131 | dev_err(&hdev->dev, "%s:logi_dj_recv_query_paired_devices " |
| 1132 | "error:%d\n", __func__, retval); |
| 1133 | goto logi_dj_recv_query_paired_devices_failed; |
| 1134 | } |
| 1135 | |
| 1136 | return retval; |
| 1137 | |
| 1138 | logi_dj_recv_query_paired_devices_failed: |
Benjamin Tissoires | ddf7540 | 2013-07-12 11:01:02 +0200 | [diff] [blame] | 1139 | hid_hw_close(hdev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1140 | |
| 1141 | llopen_failed: |
| 1142 | switch_to_dj_mode_fail: |
| 1143 | hid_hw_stop(hdev); |
| 1144 | |
| 1145 | hid_hw_start_fail: |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1146 | kfifo_free(&djrcv_dev->notif_fifo); |
| 1147 | kfree(djrcv_dev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1148 | return retval; |
| 1149 | |
| 1150 | } |
| 1151 | |
| 1152 | #ifdef CONFIG_PM |
| 1153 | static int logi_dj_reset_resume(struct hid_device *hdev) |
| 1154 | { |
| 1155 | int retval; |
| 1156 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
| 1157 | |
| 1158 | retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0); |
| 1159 | if (retval < 0) { |
| 1160 | dev_err(&hdev->dev, |
| 1161 | "%s:logi_dj_recv_switch_to_dj_mode returned error:%d\n", |
| 1162 | __func__, retval); |
| 1163 | } |
| 1164 | |
| 1165 | return 0; |
| 1166 | } |
| 1167 | #endif |
| 1168 | |
| 1169 | static void logi_dj_remove(struct hid_device *hdev) |
| 1170 | { |
| 1171 | struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev); |
| 1172 | struct dj_device *dj_dev; |
| 1173 | int i; |
| 1174 | |
| 1175 | dbg_hid("%s\n", __func__); |
| 1176 | |
| 1177 | cancel_work_sync(&djrcv_dev->work); |
| 1178 | |
Benjamin Tissoires | ddf7540 | 2013-07-12 11:01:02 +0200 | [diff] [blame] | 1179 | hid_hw_close(hdev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1180 | hid_hw_stop(hdev); |
| 1181 | |
| 1182 | /* I suppose that at this point the only context that can access |
| 1183 | * the djrecv_data is this thread as the work item is guaranteed to |
| 1184 | * have finished and no more raw_event callbacks should arrive after |
| 1185 | * the remove callback was triggered so no locks are put around the |
| 1186 | * code below */ |
Nestor Lopez Casado | 844580f | 2011-09-20 15:59:03 +0200 | [diff] [blame] | 1187 | for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) { |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1188 | dj_dev = djrcv_dev->paired_dj_devices[i]; |
| 1189 | if (dj_dev != NULL) { |
| 1190 | hid_destroy_device(dj_dev->hdev); |
| 1191 | kfree(dj_dev); |
| 1192 | djrcv_dev->paired_dj_devices[i] = NULL; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | kfifo_free(&djrcv_dev->notif_fifo); |
| 1197 | kfree(djrcv_dev); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1198 | } |
| 1199 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1200 | static const struct hid_device_id logi_dj_receivers[] = { |
| 1201 | {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, |
| 1202 | USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER)}, |
| 1203 | {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, |
| 1204 | USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2)}, |
| 1205 | {} |
| 1206 | }; |
| 1207 | |
| 1208 | MODULE_DEVICE_TABLE(hid, logi_dj_receivers); |
| 1209 | |
| 1210 | static struct hid_driver logi_djreceiver_driver = { |
| 1211 | .name = "logitech-djreceiver", |
| 1212 | .id_table = logi_dj_receivers, |
| 1213 | .probe = logi_dj_probe, |
| 1214 | .remove = logi_dj_remove, |
| 1215 | .raw_event = logi_dj_raw_event, |
| 1216 | #ifdef CONFIG_PM |
| 1217 | .reset_resume = logi_dj_reset_resume, |
| 1218 | #endif |
| 1219 | }; |
| 1220 | |
Benjamin Tissoires | ab94e56 | 2014-09-30 13:18:28 -0400 | [diff] [blame] | 1221 | module_hid_driver(logi_djreceiver_driver); |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1222 | |
Nestor Lopez Casado | 534a7b8 | 2011-09-15 11:34:49 +0200 | [diff] [blame] | 1223 | MODULE_LICENSE("GPL"); |
| 1224 | MODULE_AUTHOR("Logitech"); |
| 1225 | MODULE_AUTHOR("Nestor Lopez Casado"); |
| 1226 | MODULE_AUTHOR("nlopezcasad@logitech.com"); |