1 // SPDX-License-Identifier: GPL-2.0-only
3 * HID driver for Logitech Unifying receivers
5 * Copyright (c) 2011 Logitech
10 #include <linux/device.h>
11 #include <linux/hid.h>
12 #include <linux/module.h>
13 #include <linux/kfifo.h>
14 #include <linux/delay.h>
15 #include <linux/usb.h> /* For to_usb_interface for kvm extra intf check */
16 #include <asm/unaligned.h>
19 #define DJ_MAX_PAIRED_DEVICES 6
20 #define DJ_MAX_NUMBER_NOTIFS 8
21 #define DJ_RECEIVER_INDEX 0
22 #define DJ_DEVICE_INDEX_MIN 1
23 #define DJ_DEVICE_INDEX_MAX 6
25 #define DJREPORT_SHORT_LENGTH 15
26 #define DJREPORT_LONG_LENGTH 32
28 #define REPORT_ID_DJ_SHORT 0x20
29 #define REPORT_ID_DJ_LONG 0x21
31 #define REPORT_ID_HIDPP_SHORT 0x10
32 #define REPORT_ID_HIDPP_LONG 0x11
34 #define HIDPP_REPORT_SHORT_LENGTH 7
35 #define HIDPP_REPORT_LONG_LENGTH 20
37 #define HIDPP_RECEIVER_INDEX 0xff
39 #define REPORT_TYPE_RFREPORT_FIRST 0x01
40 #define REPORT_TYPE_RFREPORT_LAST 0x1F
42 /* Command Switch to DJ mode */
43 #define REPORT_TYPE_CMD_SWITCH 0x80
44 #define CMD_SWITCH_PARAM_DEVBITFIELD 0x00
45 #define CMD_SWITCH_PARAM_TIMEOUT_SECONDS 0x01
46 #define TIMEOUT_NO_KEEPALIVE 0x00
48 /* Command to Get the list of Paired devices */
49 #define REPORT_TYPE_CMD_GET_PAIRED_DEVICES 0x81
51 /* Device Paired Notification */
52 #define REPORT_TYPE_NOTIF_DEVICE_PAIRED 0x41
53 #define SPFUNCTION_MORE_NOTIF_EXPECTED 0x01
54 #define SPFUNCTION_DEVICE_LIST_EMPTY 0x02
55 #define DEVICE_PAIRED_PARAM_SPFUNCTION 0x00
56 #define DEVICE_PAIRED_PARAM_EQUAD_ID_LSB 0x01
57 #define DEVICE_PAIRED_PARAM_EQUAD_ID_MSB 0x02
58 #define DEVICE_PAIRED_RF_REPORT_TYPE 0x03
60 /* Device Un-Paired Notification */
61 #define REPORT_TYPE_NOTIF_DEVICE_UNPAIRED 0x40
63 /* Connection Status Notification */
64 #define REPORT_TYPE_NOTIF_CONNECTION_STATUS 0x42
65 #define CONNECTION_STATUS_PARAM_STATUS 0x00
66 #define STATUS_LINKLOSS 0x01
68 /* Error Notification */
69 #define REPORT_TYPE_NOTIF_ERROR 0x7F
70 #define NOTIF_ERROR_PARAM_ETYPE 0x00
71 #define ETYPE_KEEPALIVE_TIMEOUT 0x01
73 /* supported DJ HID && RF report types */
74 #define REPORT_TYPE_KEYBOARD 0x01
75 #define REPORT_TYPE_MOUSE 0x02
76 #define REPORT_TYPE_CONSUMER_CONTROL 0x03
77 #define REPORT_TYPE_SYSTEM_CONTROL 0x04
78 #define REPORT_TYPE_MEDIA_CENTER 0x08
79 #define REPORT_TYPE_LEDS 0x0E
81 /* RF Report types bitfield */
82 #define STD_KEYBOARD BIT(1)
83 #define STD_MOUSE BIT(2)
84 #define MULTIMEDIA BIT(3)
85 #define POWER_KEYS BIT(4)
86 #define MEDIA_CENTER BIT(8)
87 #define KBD_LEDS BIT(14)
88 /* Fake (bitnr > NUMBER_OF_HID_REPORTS) bit to track HID++ capability */
89 #define HIDPP BIT_ULL(63)
91 /* HID++ Device Connected Notification */
92 #define REPORT_TYPE_NOTIF_DEVICE_CONNECTED 0x41
93 #define HIDPP_PARAM_PROTO_TYPE 0x00
94 #define HIDPP_PARAM_DEVICE_INFO 0x01
95 #define HIDPP_PARAM_EQUAD_LSB 0x02
96 #define HIDPP_PARAM_EQUAD_MSB 0x03
97 #define HIDPP_PARAM_27MHZ_DEVID 0x03
98 #define HIDPP_DEVICE_TYPE_MASK GENMASK(3, 0)
99 #define HIDPP_LINK_STATUS_MASK BIT(6)
100 #define HIDPP_MANUFACTURER_MASK BIT(7)
102 #define HIDPP_DEVICE_TYPE_KEYBOARD 1
103 #define HIDPP_DEVICE_TYPE_MOUSE 2
105 #define HIDPP_SET_REGISTER 0x80
106 #define HIDPP_GET_LONG_REGISTER 0x83
107 #define HIDPP_REG_CONNECTION_STATE 0x02
108 #define HIDPP_REG_PAIRING_INFORMATION 0xB5
109 #define HIDPP_PAIRING_INFORMATION 0x20
110 #define HIDPP_FAKE_DEVICE_ARRIVAL 0x02
115 recvr_type_gaming_hidpp,
117 recvr_type_bluetooth,
124 u8 report_params[DJREPORT_SHORT_LENGTH - 3];
131 u8 params[HIDPP_REPORT_LONG_LENGTH - 3U];
134 struct dj_receiver_dev {
135 struct hid_device *mouse;
136 struct hid_device *keyboard;
137 struct hid_device *hidpp;
138 struct dj_device *paired_dj_devices[DJ_MAX_PAIRED_DEVICES +
139 DJ_DEVICE_INDEX_MIN];
140 struct list_head list;
142 struct work_struct work;
143 struct kfifo notif_fifo;
144 unsigned long last_query; /* in jiffies */
146 enum recvr_type type;
147 unsigned int unnumbered_application;
152 struct hid_device *hdev;
153 struct dj_receiver_dev *dj_receiver_dev;
154 u64 reports_supported;
158 #define WORKITEM_TYPE_EMPTY 0
159 #define WORKITEM_TYPE_PAIRED 1
160 #define WORKITEM_TYPE_UNPAIRED 2
161 #define WORKITEM_TYPE_UNKNOWN 255
164 u8 type; /* WORKITEM_TYPE_* */
169 u64 reports_supported;
172 /* Keyboard descriptor (1) */
173 static const char kbd_descriptor[] = {
174 0x05, 0x01, /* USAGE_PAGE (generic Desktop) */
175 0x09, 0x06, /* USAGE (Keyboard) */
176 0xA1, 0x01, /* COLLECTION (Application) */
177 0x85, 0x01, /* REPORT_ID (1) */
178 0x95, 0x08, /* REPORT_COUNT (8) */
179 0x75, 0x01, /* REPORT_SIZE (1) */
180 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
181 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
182 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
183 0x19, 0xE0, /* USAGE_MINIMUM (Left Control) */
184 0x29, 0xE7, /* USAGE_MAXIMUM (Right GUI) */
185 0x81, 0x02, /* INPUT (Data,Var,Abs) */
186 0x95, 0x06, /* REPORT_COUNT (6) */
187 0x75, 0x08, /* REPORT_SIZE (8) */
188 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
189 0x26, 0xFF, 0x00, /* LOGICAL_MAXIMUM (255) */
190 0x05, 0x07, /* USAGE_PAGE (Keyboard) */
191 0x19, 0x00, /* USAGE_MINIMUM (no event) */
192 0x2A, 0xFF, 0x00, /* USAGE_MAXIMUM (reserved) */
193 0x81, 0x00, /* INPUT (Data,Ary,Abs) */
194 0x85, 0x0e, /* REPORT_ID (14) */
195 0x05, 0x08, /* USAGE PAGE (LED page) */
196 0x95, 0x05, /* REPORT COUNT (5) */
197 0x75, 0x01, /* REPORT SIZE (1) */
198 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
199 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
200 0x19, 0x01, /* USAGE MINIMUM (1) */
201 0x29, 0x05, /* USAGE MAXIMUM (5) */
202 0x91, 0x02, /* OUTPUT (Data, Variable, Absolute) */
203 0x95, 0x01, /* REPORT COUNT (1) */
204 0x75, 0x03, /* REPORT SIZE (3) */
205 0x91, 0x01, /* OUTPUT (Constant) */
209 /* Mouse descriptor (2) */
210 static const char mse_descriptor[] = {
211 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
212 0x09, 0x02, /* USAGE (Mouse) */
213 0xA1, 0x01, /* COLLECTION (Application) */
214 0x85, 0x02, /* REPORT_ID = 2 */
215 0x09, 0x01, /* USAGE (pointer) */
216 0xA1, 0x00, /* COLLECTION (physical) */
217 0x05, 0x09, /* USAGE_PAGE (buttons) */
218 0x19, 0x01, /* USAGE_MIN (1) */
219 0x29, 0x10, /* USAGE_MAX (16) */
220 0x15, 0x00, /* LOGICAL_MIN (0) */
221 0x25, 0x01, /* LOGICAL_MAX (1) */
222 0x95, 0x10, /* REPORT_COUNT (16) */
223 0x75, 0x01, /* REPORT_SIZE (1) */
224 0x81, 0x02, /* INPUT (data var abs) */
225 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
226 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
227 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
228 0x75, 0x0C, /* REPORT_SIZE (12) */
229 0x95, 0x02, /* REPORT_COUNT (2) */
230 0x09, 0x30, /* USAGE (X) */
231 0x09, 0x31, /* USAGE (Y) */
232 0x81, 0x06, /* INPUT */
233 0x15, 0x81, /* LOGICAL_MIN (-127) */
234 0x25, 0x7F, /* LOGICAL_MAX (127) */
235 0x75, 0x08, /* REPORT_SIZE (8) */
236 0x95, 0x01, /* REPORT_COUNT (1) */
237 0x09, 0x38, /* USAGE (wheel) */
238 0x81, 0x06, /* INPUT */
239 0x05, 0x0C, /* USAGE_PAGE(consumer) */
240 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
241 0x95, 0x01, /* REPORT_COUNT (1) */
242 0x81, 0x06, /* INPUT */
243 0xC0, /* END_COLLECTION */
244 0xC0, /* END_COLLECTION */
247 /* Mouse descriptor (2) for 27 MHz receiver, only 8 buttons */
248 static const char mse_27mhz_descriptor[] = {
249 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
250 0x09, 0x02, /* USAGE (Mouse) */
251 0xA1, 0x01, /* COLLECTION (Application) */
252 0x85, 0x02, /* REPORT_ID = 2 */
253 0x09, 0x01, /* USAGE (pointer) */
254 0xA1, 0x00, /* COLLECTION (physical) */
255 0x05, 0x09, /* USAGE_PAGE (buttons) */
256 0x19, 0x01, /* USAGE_MIN (1) */
257 0x29, 0x08, /* USAGE_MAX (8) */
258 0x15, 0x00, /* LOGICAL_MIN (0) */
259 0x25, 0x01, /* LOGICAL_MAX (1) */
260 0x95, 0x08, /* REPORT_COUNT (8) */
261 0x75, 0x01, /* REPORT_SIZE (1) */
262 0x81, 0x02, /* INPUT (data var abs) */
263 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
264 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
265 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
266 0x75, 0x0C, /* REPORT_SIZE (12) */
267 0x95, 0x02, /* REPORT_COUNT (2) */
268 0x09, 0x30, /* USAGE (X) */
269 0x09, 0x31, /* USAGE (Y) */
270 0x81, 0x06, /* INPUT */
271 0x15, 0x81, /* LOGICAL_MIN (-127) */
272 0x25, 0x7F, /* LOGICAL_MAX (127) */
273 0x75, 0x08, /* REPORT_SIZE (8) */
274 0x95, 0x01, /* REPORT_COUNT (1) */
275 0x09, 0x38, /* USAGE (wheel) */
276 0x81, 0x06, /* INPUT */
277 0x05, 0x0C, /* USAGE_PAGE(consumer) */
278 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
279 0x95, 0x01, /* REPORT_COUNT (1) */
280 0x81, 0x06, /* INPUT */
281 0xC0, /* END_COLLECTION */
282 0xC0, /* END_COLLECTION */
285 /* Mouse descriptor (2) for Bluetooth receiver, low-res hwheel, 12 buttons */
286 static const char mse_bluetooth_descriptor[] = {
287 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
288 0x09, 0x02, /* USAGE (Mouse) */
289 0xA1, 0x01, /* COLLECTION (Application) */
290 0x85, 0x02, /* REPORT_ID = 2 */
291 0x09, 0x01, /* USAGE (pointer) */
292 0xA1, 0x00, /* COLLECTION (physical) */
293 0x05, 0x09, /* USAGE_PAGE (buttons) */
294 0x19, 0x01, /* USAGE_MIN (1) */
295 0x29, 0x08, /* USAGE_MAX (8) */
296 0x15, 0x00, /* LOGICAL_MIN (0) */
297 0x25, 0x01, /* LOGICAL_MAX (1) */
298 0x95, 0x08, /* REPORT_COUNT (8) */
299 0x75, 0x01, /* REPORT_SIZE (1) */
300 0x81, 0x02, /* INPUT (data var abs) */
301 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
302 0x16, 0x01, 0xF8, /* LOGICAL_MIN (-2047) */
303 0x26, 0xFF, 0x07, /* LOGICAL_MAX (2047) */
304 0x75, 0x0C, /* REPORT_SIZE (12) */
305 0x95, 0x02, /* REPORT_COUNT (2) */
306 0x09, 0x30, /* USAGE (X) */
307 0x09, 0x31, /* USAGE (Y) */
308 0x81, 0x06, /* INPUT */
309 0x15, 0x81, /* LOGICAL_MIN (-127) */
310 0x25, 0x7F, /* LOGICAL_MAX (127) */
311 0x75, 0x08, /* REPORT_SIZE (8) */
312 0x95, 0x01, /* REPORT_COUNT (1) */
313 0x09, 0x38, /* USAGE (wheel) */
314 0x81, 0x06, /* INPUT */
315 0x05, 0x0C, /* USAGE_PAGE(consumer) */
316 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
317 0x15, 0xF9, /* LOGICAL_MIN (-7) */
318 0x25, 0x07, /* LOGICAL_MAX (7) */
319 0x75, 0x04, /* REPORT_SIZE (4) */
320 0x95, 0x01, /* REPORT_COUNT (1) */
321 0x81, 0x06, /* INPUT */
322 0x05, 0x09, /* USAGE_PAGE (buttons) */
323 0x19, 0x09, /* USAGE_MIN (9) */
324 0x29, 0x0C, /* USAGE_MAX (12) */
325 0x15, 0x00, /* LOGICAL_MIN (0) */
326 0x25, 0x01, /* LOGICAL_MAX (1) */
327 0x75, 0x01, /* REPORT_SIZE (1) */
328 0x95, 0x04, /* REPORT_COUNT (4) */
329 0x81, 0x06, /* INPUT */
330 0xC0, /* END_COLLECTION */
331 0xC0, /* END_COLLECTION */
334 /* Gaming Mouse descriptor (2) */
335 static const char mse_high_res_descriptor[] = {
336 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
337 0x09, 0x02, /* USAGE (Mouse) */
338 0xA1, 0x01, /* COLLECTION (Application) */
339 0x85, 0x02, /* REPORT_ID = 2 */
340 0x09, 0x01, /* USAGE (pointer) */
341 0xA1, 0x00, /* COLLECTION (physical) */
342 0x05, 0x09, /* USAGE_PAGE (buttons) */
343 0x19, 0x01, /* USAGE_MIN (1) */
344 0x29, 0x10, /* USAGE_MAX (16) */
345 0x15, 0x00, /* LOGICAL_MIN (0) */
346 0x25, 0x01, /* LOGICAL_MAX (1) */
347 0x95, 0x10, /* REPORT_COUNT (16) */
348 0x75, 0x01, /* REPORT_SIZE (1) */
349 0x81, 0x02, /* INPUT (data var abs) */
350 0x05, 0x01, /* USAGE_PAGE (generic desktop) */
351 0x16, 0x01, 0x80, /* LOGICAL_MIN (-32767) */
352 0x26, 0xFF, 0x7F, /* LOGICAL_MAX (32767) */
353 0x75, 0x10, /* REPORT_SIZE (16) */
354 0x95, 0x02, /* REPORT_COUNT (2) */
355 0x09, 0x30, /* USAGE (X) */
356 0x09, 0x31, /* USAGE (Y) */
357 0x81, 0x06, /* INPUT */
358 0x15, 0x81, /* LOGICAL_MIN (-127) */
359 0x25, 0x7F, /* LOGICAL_MAX (127) */
360 0x75, 0x08, /* REPORT_SIZE (8) */
361 0x95, 0x01, /* REPORT_COUNT (1) */
362 0x09, 0x38, /* USAGE (wheel) */
363 0x81, 0x06, /* INPUT */
364 0x05, 0x0C, /* USAGE_PAGE(consumer) */
365 0x0A, 0x38, 0x02, /* USAGE(AC Pan) */
366 0x95, 0x01, /* REPORT_COUNT (1) */
367 0x81, 0x06, /* INPUT */
368 0xC0, /* END_COLLECTION */
369 0xC0, /* END_COLLECTION */
372 /* Consumer Control descriptor (3) */
373 static const char consumer_descriptor[] = {
374 0x05, 0x0C, /* USAGE_PAGE (Consumer Devices) */
375 0x09, 0x01, /* USAGE (Consumer Control) */
376 0xA1, 0x01, /* COLLECTION (Application) */
377 0x85, 0x03, /* REPORT_ID = 3 */
378 0x75, 0x10, /* REPORT_SIZE (16) */
379 0x95, 0x02, /* REPORT_COUNT (2) */
380 0x15, 0x01, /* LOGICAL_MIN (1) */
381 0x26, 0x8C, 0x02, /* LOGICAL_MAX (652) */
382 0x19, 0x01, /* USAGE_MIN (1) */
383 0x2A, 0x8C, 0x02, /* USAGE_MAX (652) */
384 0x81, 0x00, /* INPUT (Data Ary Abs) */
385 0xC0, /* END_COLLECTION */
388 /* System control descriptor (4) */
389 static const char syscontrol_descriptor[] = {
390 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
391 0x09, 0x80, /* USAGE (System Control) */
392 0xA1, 0x01, /* COLLECTION (Application) */
393 0x85, 0x04, /* REPORT_ID = 4 */
394 0x75, 0x02, /* REPORT_SIZE (2) */
395 0x95, 0x01, /* REPORT_COUNT (1) */
396 0x15, 0x01, /* LOGICAL_MIN (1) */
397 0x25, 0x03, /* LOGICAL_MAX (3) */
398 0x09, 0x82, /* USAGE (System Sleep) */
399 0x09, 0x81, /* USAGE (System Power Down) */
400 0x09, 0x83, /* USAGE (System Wake Up) */
401 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */
402 0x75, 0x06, /* REPORT_SIZE (6) */
403 0x81, 0x03, /* INPUT (Cnst Var Abs) */
404 0xC0, /* END_COLLECTION */
407 /* Media descriptor (8) */
408 static const char media_descriptor[] = {
409 0x06, 0xbc, 0xff, /* Usage Page 0xffbc */
410 0x09, 0x88, /* Usage 0x0088 */
411 0xa1, 0x01, /* BeginCollection */
412 0x85, 0x08, /* Report ID 8 */
413 0x19, 0x01, /* Usage Min 0x0001 */
414 0x29, 0xff, /* Usage Max 0x00ff */
415 0x15, 0x01, /* Logical Min 1 */
416 0x26, 0xff, 0x00, /* Logical Max 255 */
417 0x75, 0x08, /* Report Size 8 */
418 0x95, 0x01, /* Report Count 1 */
419 0x81, 0x00, /* Input */
420 0xc0, /* EndCollection */
423 /* HIDPP descriptor */
424 static const char hidpp_descriptor[] = {
425 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
426 0x09, 0x01, /* Usage (Vendor Usage 1) */
427 0xa1, 0x01, /* Collection (Application) */
428 0x85, 0x10, /* Report ID (16) */
429 0x75, 0x08, /* Report Size (8) */
430 0x95, 0x06, /* Report Count (6) */
431 0x15, 0x00, /* Logical Minimum (0) */
432 0x26, 0xff, 0x00, /* Logical Maximum (255) */
433 0x09, 0x01, /* Usage (Vendor Usage 1) */
434 0x81, 0x00, /* Input (Data,Arr,Abs) */
435 0x09, 0x01, /* Usage (Vendor Usage 1) */
436 0x91, 0x00, /* Output (Data,Arr,Abs) */
437 0xc0, /* End Collection */
438 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
439 0x09, 0x02, /* Usage (Vendor Usage 2) */
440 0xa1, 0x01, /* Collection (Application) */
441 0x85, 0x11, /* Report ID (17) */
442 0x75, 0x08, /* Report Size (8) */
443 0x95, 0x13, /* Report Count (19) */
444 0x15, 0x00, /* Logical Minimum (0) */
445 0x26, 0xff, 0x00, /* Logical Maximum (255) */
446 0x09, 0x02, /* Usage (Vendor Usage 2) */
447 0x81, 0x00, /* Input (Data,Arr,Abs) */
448 0x09, 0x02, /* Usage (Vendor Usage 2) */
449 0x91, 0x00, /* Output (Data,Arr,Abs) */
450 0xc0, /* End Collection */
451 0x06, 0x00, 0xff, /* Usage Page (Vendor Defined Page 1) */
452 0x09, 0x04, /* Usage (Vendor Usage 0x04) */
453 0xa1, 0x01, /* Collection (Application) */
454 0x85, 0x20, /* Report ID (32) */
455 0x75, 0x08, /* Report Size (8) */
456 0x95, 0x0e, /* Report Count (14) */
457 0x15, 0x00, /* Logical Minimum (0) */
458 0x26, 0xff, 0x00, /* Logical Maximum (255) */
459 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
460 0x81, 0x00, /* Input (Data,Arr,Abs) */
461 0x09, 0x41, /* Usage (Vendor Usage 0x41) */
462 0x91, 0x00, /* Output (Data,Arr,Abs) */
463 0x85, 0x21, /* Report ID (33) */
464 0x95, 0x1f, /* Report Count (31) */
465 0x15, 0x00, /* Logical Minimum (0) */
466 0x26, 0xff, 0x00, /* Logical Maximum (255) */
467 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
468 0x81, 0x00, /* Input (Data,Arr,Abs) */
469 0x09, 0x42, /* Usage (Vendor Usage 0x42) */
470 0x91, 0x00, /* Output (Data,Arr,Abs) */
471 0xc0, /* End Collection */
474 /* Maximum size of all defined hid reports in bytes (including report id) */
475 #define MAX_REPORT_SIZE 8
477 /* Make sure all descriptors are present here */
478 #define MAX_RDESC_SIZE \
479 (sizeof(kbd_descriptor) + \
480 sizeof(mse_bluetooth_descriptor) + \
481 sizeof(consumer_descriptor) + \
482 sizeof(syscontrol_descriptor) + \
483 sizeof(media_descriptor) + \
484 sizeof(hidpp_descriptor))
486 /* Number of possible hid report types that can be created by this driver.
488 * Right now, RF report types have the same report types (or report id's)
489 * than the hid report created from those RF reports. In the future
490 * this doesnt have to be true.
492 * For instance, RF report type 0x01 which has a size of 8 bytes, corresponds
493 * to hid report id 0x01, this is standard keyboard. Same thing applies to mice
494 * reports and consumer control, etc. If a new RF report is created, it doesn't
495 * has to have the same report id as its corresponding hid report, so an
496 * translation may have to take place for future report types.
498 #define NUMBER_OF_HID_REPORTS 32
499 static const u8 hid_reportid_size_map[NUMBER_OF_HID_REPORTS] = {
500 [1] = 8, /* Standard keyboard */
501 [2] = 8, /* Standard mouse */
502 [3] = 5, /* Consumer control */
503 [4] = 2, /* System control */
504 [8] = 2, /* Media Center */
508 #define LOGITECH_DJ_INTERFACE_NUMBER 0x02
510 static struct hid_ll_driver logi_dj_ll_driver;
512 static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev);
513 static void delayedwork_callback(struct work_struct *work);
515 static LIST_HEAD(dj_hdev_list);
516 static DEFINE_MUTEX(dj_hdev_list_lock);
519 * dj/HID++ receivers are really a single logical entity, but for BIOS/Windows
520 * compatibility they have multiple USB interfaces. On HID++ receivers we need
521 * to listen for input reports on both interfaces. The functions below are used
522 * to create a single struct dj_receiver_dev for all interfaces belonging to
523 * a single USB-device / receiver.
525 static struct dj_receiver_dev *dj_find_receiver_dev(struct hid_device *hdev,
526 enum recvr_type type)
528 struct dj_receiver_dev *djrcv_dev;
532 * The bluetooth receiver contains a built-in hub and has separate
533 * USB-devices for the keyboard and mouse interfaces.
535 sep = (type == recvr_type_bluetooth) ? '.' : '/';
537 /* Try to find an already-probed interface from the same device */
538 list_for_each_entry(djrcv_dev, &dj_hdev_list, list) {
539 if (djrcv_dev->mouse &&
540 hid_compare_device_paths(hdev, djrcv_dev->mouse, sep)) {
541 kref_get(&djrcv_dev->kref);
544 if (djrcv_dev->keyboard &&
545 hid_compare_device_paths(hdev, djrcv_dev->keyboard, sep)) {
546 kref_get(&djrcv_dev->kref);
549 if (djrcv_dev->hidpp &&
550 hid_compare_device_paths(hdev, djrcv_dev->hidpp, sep)) {
551 kref_get(&djrcv_dev->kref);
559 static void dj_release_receiver_dev(struct kref *kref)
561 struct dj_receiver_dev *djrcv_dev = container_of(kref, struct dj_receiver_dev, kref);
563 list_del(&djrcv_dev->list);
564 kfifo_free(&djrcv_dev->notif_fifo);
568 static void dj_put_receiver_dev(struct hid_device *hdev)
570 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
572 mutex_lock(&dj_hdev_list_lock);
574 if (djrcv_dev->mouse == hdev)
575 djrcv_dev->mouse = NULL;
576 if (djrcv_dev->keyboard == hdev)
577 djrcv_dev->keyboard = NULL;
578 if (djrcv_dev->hidpp == hdev)
579 djrcv_dev->hidpp = NULL;
581 kref_put(&djrcv_dev->kref, dj_release_receiver_dev);
583 mutex_unlock(&dj_hdev_list_lock);
586 static struct dj_receiver_dev *dj_get_receiver_dev(struct hid_device *hdev,
587 enum recvr_type type,
588 unsigned int application,
591 struct dj_receiver_dev *djrcv_dev;
593 mutex_lock(&dj_hdev_list_lock);
595 djrcv_dev = dj_find_receiver_dev(hdev, type);
597 djrcv_dev = kzalloc(sizeof(*djrcv_dev), GFP_KERNEL);
601 INIT_WORK(&djrcv_dev->work, delayedwork_callback);
602 spin_lock_init(&djrcv_dev->lock);
603 if (kfifo_alloc(&djrcv_dev->notif_fifo,
604 DJ_MAX_NUMBER_NOTIFS * sizeof(struct dj_workitem),
610 kref_init(&djrcv_dev->kref);
611 list_add_tail(&djrcv_dev->list, &dj_hdev_list);
612 djrcv_dev->last_query = jiffies;
613 djrcv_dev->type = type;
616 if (application == HID_GD_KEYBOARD)
617 djrcv_dev->keyboard = hdev;
618 if (application == HID_GD_MOUSE)
619 djrcv_dev->mouse = hdev;
621 djrcv_dev->hidpp = hdev;
623 hid_set_drvdata(hdev, djrcv_dev);
625 mutex_unlock(&dj_hdev_list_lock);
629 static void logi_dj_recv_destroy_djhid_device(struct dj_receiver_dev *djrcv_dev,
630 struct dj_workitem *workitem)
632 /* Called in delayed work context */
633 struct dj_device *dj_dev;
636 spin_lock_irqsave(&djrcv_dev->lock, flags);
637 dj_dev = djrcv_dev->paired_dj_devices[workitem->device_index];
638 djrcv_dev->paired_dj_devices[workitem->device_index] = NULL;
639 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
641 if (dj_dev != NULL) {
642 hid_destroy_device(dj_dev->hdev);
645 hid_err(djrcv_dev->hidpp, "%s: can't destroy a NULL device\n",
650 static void logi_dj_recv_add_djhid_device(struct dj_receiver_dev *djrcv_dev,
651 struct dj_workitem *workitem)
653 /* Called in delayed work context */
654 struct hid_device *djrcv_hdev = djrcv_dev->hidpp;
655 struct hid_device *dj_hiddev;
656 struct dj_device *dj_dev;
657 u8 device_index = workitem->device_index;
660 /* Device index goes from 1 to 6, we need 3 bytes to store the
661 * semicolon, the index, and a null terminator
663 unsigned char tmpstr[3];
665 /* We are the only one ever adding a device, no need to lock */
666 if (djrcv_dev->paired_dj_devices[device_index]) {
667 /* The device is already known. No need to reallocate it. */
668 dbg_hid("%s: device is already known\n", __func__);
672 dj_hiddev = hid_allocate_device();
673 if (IS_ERR(dj_hiddev)) {
674 hid_err(djrcv_hdev, "%s: hid_allocate_dev failed\n", __func__);
678 dj_hiddev->ll_driver = &logi_dj_ll_driver;
680 dj_hiddev->dev.parent = &djrcv_hdev->dev;
681 dj_hiddev->bus = BUS_USB;
682 dj_hiddev->vendor = djrcv_hdev->vendor;
683 dj_hiddev->product = (workitem->quad_id_msb << 8) |
684 workitem->quad_id_lsb;
685 if (workitem->device_type) {
686 const char *type_str = "Device";
688 switch (workitem->device_type) {
689 case 0x01: type_str = "Keyboard"; break;
690 case 0x02: type_str = "Mouse"; break;
691 case 0x03: type_str = "Numpad"; break;
692 case 0x04: type_str = "Presenter"; break;
693 case 0x07: type_str = "Remote Control"; break;
694 case 0x08: type_str = "Trackball"; break;
695 case 0x09: type_str = "Touchpad"; break;
697 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
698 "Logitech Wireless %s PID:%04x",
699 type_str, dj_hiddev->product);
701 snprintf(dj_hiddev->name, sizeof(dj_hiddev->name),
702 "Logitech Unifying Device. Wireless PID:%04x",
706 if (djrcv_dev->type == recvr_type_27mhz)
707 dj_hiddev->group = HID_GROUP_LOGITECH_27MHZ_DEVICE;
709 dj_hiddev->group = HID_GROUP_LOGITECH_DJ_DEVICE;
711 memcpy(dj_hiddev->phys, djrcv_hdev->phys, sizeof(djrcv_hdev->phys));
712 snprintf(tmpstr, sizeof(tmpstr), ":%d", device_index);
713 strlcat(dj_hiddev->phys, tmpstr, sizeof(dj_hiddev->phys));
715 dj_dev = kzalloc(sizeof(struct dj_device), GFP_KERNEL);
718 hid_err(djrcv_hdev, "%s: failed allocating dj_dev\n", __func__);
719 goto dj_device_allocate_fail;
722 dj_dev->reports_supported = workitem->reports_supported;
723 dj_dev->hdev = dj_hiddev;
724 dj_dev->dj_receiver_dev = djrcv_dev;
725 dj_dev->device_index = device_index;
726 dj_hiddev->driver_data = dj_dev;
728 spin_lock_irqsave(&djrcv_dev->lock, flags);
729 djrcv_dev->paired_dj_devices[device_index] = dj_dev;
730 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
732 if (hid_add_device(dj_hiddev)) {
733 hid_err(djrcv_hdev, "%s: failed adding dj_device\n", __func__);
734 goto hid_add_device_fail;
740 spin_lock_irqsave(&djrcv_dev->lock, flags);
741 djrcv_dev->paired_dj_devices[device_index] = NULL;
742 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
744 dj_device_allocate_fail:
745 hid_destroy_device(dj_hiddev);
748 static void delayedwork_callback(struct work_struct *work)
750 struct dj_receiver_dev *djrcv_dev =
751 container_of(work, struct dj_receiver_dev, work);
753 struct dj_workitem workitem;
758 dbg_hid("%s\n", __func__);
760 spin_lock_irqsave(&djrcv_dev->lock, flags);
763 * Since we attach to multiple interfaces, we may get scheduled before
764 * we are bound to the HID++ interface, catch this.
766 if (!djrcv_dev->ready) {
767 pr_warn("%s: delayedwork queued before hidpp interface was enumerated\n",
769 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
773 count = kfifo_out(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
775 if (count != sizeof(workitem)) {
776 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
780 if (!kfifo_is_empty(&djrcv_dev->notif_fifo))
781 schedule_work(&djrcv_dev->work);
783 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
785 switch (workitem.type) {
786 case WORKITEM_TYPE_PAIRED:
787 logi_dj_recv_add_djhid_device(djrcv_dev, &workitem);
789 case WORKITEM_TYPE_UNPAIRED:
790 logi_dj_recv_destroy_djhid_device(djrcv_dev, &workitem);
792 case WORKITEM_TYPE_UNKNOWN:
793 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
795 hid_err(djrcv_dev->hidpp, "%s: logi_dj_recv_query_paired_devices error: %d\n",
799 case WORKITEM_TYPE_EMPTY:
800 dbg_hid("%s: device list is empty\n", __func__);
806 * Sometimes we receive reports for which we do not have a paired dj_device
807 * associated with the device_index or report-type to forward the report to.
808 * This means that the original "device paired" notification corresponding
809 * to the dj_device never arrived to this driver. Possible reasons for this are:
810 * 1) hid-core discards all packets coming from a device during probe().
811 * 2) if the receiver is plugged into a KVM switch then the pairing reports
812 * are only forwarded to it if the focus is on this PC.
813 * This function deals with this by re-asking the receiver for the list of
814 * connected devices in the delayed work callback.
815 * This function MUST be called with djrcv->lock held.
817 static void logi_dj_recv_queue_unknown_work(struct dj_receiver_dev *djrcv_dev)
819 struct dj_workitem workitem = { .type = WORKITEM_TYPE_UNKNOWN };
821 /* Rate limit queries done because of unhandeled reports to 2/sec */
822 if (time_before(jiffies, djrcv_dev->last_query + HZ / 2))
825 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
826 schedule_work(&djrcv_dev->work);
829 static void logi_dj_recv_queue_notification(struct dj_receiver_dev *djrcv_dev,
830 struct dj_report *dj_report)
832 /* We are called from atomic context (tasklet && djrcv->lock held) */
833 struct dj_workitem workitem = {
834 .device_index = dj_report->device_index,
837 switch (dj_report->report_type) {
838 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
839 workitem.type = WORKITEM_TYPE_PAIRED;
840 if (dj_report->report_params[DEVICE_PAIRED_PARAM_SPFUNCTION] &
841 SPFUNCTION_DEVICE_LIST_EMPTY) {
842 workitem.type = WORKITEM_TYPE_EMPTY;
846 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
847 workitem.quad_id_msb =
848 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_MSB];
849 workitem.quad_id_lsb =
850 dj_report->report_params[DEVICE_PAIRED_PARAM_EQUAD_ID_LSB];
851 workitem.reports_supported = get_unaligned_le32(
852 dj_report->report_params +
853 DEVICE_PAIRED_RF_REPORT_TYPE);
854 workitem.reports_supported |= HIDPP;
855 if (dj_report->report_type == REPORT_TYPE_NOTIF_DEVICE_UNPAIRED)
856 workitem.type = WORKITEM_TYPE_UNPAIRED;
859 logi_dj_recv_queue_unknown_work(djrcv_dev);
863 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
864 schedule_work(&djrcv_dev->work);
867 static void logi_hidpp_dev_conn_notif_equad(struct hidpp_event *hidpp_report,
868 struct dj_workitem *workitem)
870 workitem->type = WORKITEM_TYPE_PAIRED;
871 workitem->device_type = hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
872 HIDPP_DEVICE_TYPE_MASK;
873 workitem->quad_id_msb = hidpp_report->params[HIDPP_PARAM_EQUAD_MSB];
874 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_EQUAD_LSB];
875 switch (workitem->device_type) {
876 case REPORT_TYPE_KEYBOARD:
877 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
878 POWER_KEYS | MEDIA_CENTER |
881 case REPORT_TYPE_MOUSE:
882 workitem->reports_supported |= STD_MOUSE | HIDPP;
887 static void logi_hidpp_dev_conn_notif_27mhz(struct hid_device *hdev,
888 struct hidpp_event *hidpp_report,
889 struct dj_workitem *workitem)
891 workitem->type = WORKITEM_TYPE_PAIRED;
892 workitem->quad_id_lsb = hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID];
893 switch (hidpp_report->device_index) {
894 case 1: /* Index 1 is always a mouse */
895 case 2: /* Index 2 is always a mouse */
896 workitem->device_type = HIDPP_DEVICE_TYPE_MOUSE;
897 workitem->reports_supported |= STD_MOUSE | HIDPP;
899 case 3: /* Index 3 is always the keyboard */
900 case 4: /* Index 4 is used for an optional separate numpad */
901 workitem->device_type = HIDPP_DEVICE_TYPE_KEYBOARD;
902 workitem->reports_supported |= STD_KEYBOARD | MULTIMEDIA |
906 hid_warn(hdev, "%s: unexpected device-index %d", __func__,
907 hidpp_report->device_index);
911 static void logi_hidpp_recv_queue_notif(struct hid_device *hdev,
912 struct hidpp_event *hidpp_report)
914 /* We are called from atomic context (tasklet && djrcv->lock held) */
915 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
916 const char *device_type = "UNKNOWN";
917 struct dj_workitem workitem = {
918 .type = WORKITEM_TYPE_EMPTY,
919 .device_index = hidpp_report->device_index,
922 switch (hidpp_report->params[HIDPP_PARAM_PROTO_TYPE]) {
924 device_type = "Bluetooth";
925 /* Bluetooth connect packet contents is the same as (e)QUAD */
926 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
927 if (!(hidpp_report->params[HIDPP_PARAM_DEVICE_INFO] &
928 HIDPP_MANUFACTURER_MASK)) {
929 hid_info(hdev, "Non Logitech device connected on slot %d\n",
930 hidpp_report->device_index);
931 workitem.reports_supported &= ~HIDPP;
935 device_type = "27 Mhz";
936 logi_hidpp_dev_conn_notif_27mhz(hdev, hidpp_report, &workitem);
939 device_type = "QUAD or eQUAD";
940 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
943 device_type = "eQUAD step 4 DJ";
944 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
947 device_type = "DFU Lite";
950 device_type = "eQUAD step 4 Lite";
951 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
954 device_type = "eQUAD step 4 Gaming";
957 device_type = "eQUAD step 4 for gamepads";
960 device_type = "eQUAD nano Lite";
961 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
964 device_type = "eQUAD Lightspeed";
965 logi_hidpp_dev_conn_notif_equad(hidpp_report, &workitem);
966 workitem.reports_supported |= STD_KEYBOARD;
970 if (workitem.type == WORKITEM_TYPE_EMPTY) {
972 "unusable device of type %s (0x%02x) connected on slot %d",
974 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
975 hidpp_report->device_index);
979 hid_info(hdev, "device of type %s (0x%02x) connected on slot %d",
980 device_type, hidpp_report->params[HIDPP_PARAM_PROTO_TYPE],
981 hidpp_report->device_index);
983 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
984 schedule_work(&djrcv_dev->work);
987 static void logi_dj_recv_forward_null_report(struct dj_receiver_dev *djrcv_dev,
988 struct dj_report *dj_report)
990 /* We are called from atomic context (tasklet && djrcv->lock held) */
992 u8 reportbuffer[MAX_REPORT_SIZE];
993 struct dj_device *djdev;
995 djdev = djrcv_dev->paired_dj_devices[dj_report->device_index];
997 memset(reportbuffer, 0, sizeof(reportbuffer));
999 for (i = 0; i < NUMBER_OF_HID_REPORTS; i++) {
1000 if (djdev->reports_supported & (1 << i)) {
1001 reportbuffer[0] = i;
1002 if (hid_input_report(djdev->hdev,
1005 hid_reportid_size_map[i], 1)) {
1006 dbg_hid("hid_input_report error sending null "
1013 static void logi_dj_recv_forward_dj(struct dj_receiver_dev *djrcv_dev,
1014 struct dj_report *dj_report)
1016 /* We are called from atomic context (tasklet && djrcv->lock held) */
1017 struct dj_device *dj_device;
1019 dj_device = djrcv_dev->paired_dj_devices[dj_report->device_index];
1021 if ((dj_report->report_type > ARRAY_SIZE(hid_reportid_size_map) - 1) ||
1022 (hid_reportid_size_map[dj_report->report_type] == 0)) {
1023 dbg_hid("invalid report type:%x\n", dj_report->report_type);
1027 if (hid_input_report(dj_device->hdev,
1028 HID_INPUT_REPORT, &dj_report->report_type,
1029 hid_reportid_size_map[dj_report->report_type], 1)) {
1030 dbg_hid("hid_input_report error\n");
1034 static void logi_dj_recv_forward_report(struct dj_device *dj_dev, u8 *data,
1037 /* We are called from atomic context (tasklet && djrcv->lock held) */
1038 if (hid_input_report(dj_dev->hdev, HID_INPUT_REPORT, data, size, 1))
1039 dbg_hid("hid_input_report error\n");
1042 static void logi_dj_recv_forward_input_report(struct hid_device *hdev,
1045 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1046 struct dj_device *dj_dev;
1047 unsigned long flags;
1048 u8 report = data[0];
1051 if (report > REPORT_TYPE_RFREPORT_LAST) {
1052 hid_err(hdev, "Unexpected input report number %d\n", report);
1056 spin_lock_irqsave(&djrcv_dev->lock, flags);
1057 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
1058 dj_dev = djrcv_dev->paired_dj_devices[i];
1059 if (dj_dev && (dj_dev->reports_supported & BIT(report))) {
1060 logi_dj_recv_forward_report(dj_dev, data, size);
1061 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1066 logi_dj_recv_queue_unknown_work(djrcv_dev);
1067 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1069 dbg_hid("No dj-devs handling input report number %d\n", report);
1072 static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
1073 struct dj_report *dj_report)
1075 struct hid_device *hdev = djrcv_dev->hidpp;
1076 struct hid_report *report;
1077 struct hid_report_enum *output_report_enum;
1078 u8 *data = (u8 *)(&dj_report->device_index);
1081 output_report_enum = &hdev->report_enum[HID_OUTPUT_REPORT];
1082 report = output_report_enum->report_id_hash[REPORT_ID_DJ_SHORT];
1085 hid_err(hdev, "%s: unable to find dj report\n", __func__);
1089 for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
1090 report->field[0]->value[i] = data[i];
1092 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1097 static int logi_dj_recv_query_hidpp_devices(struct dj_receiver_dev *djrcv_dev)
1099 const u8 template[] = {REPORT_ID_HIDPP_SHORT,
1100 HIDPP_RECEIVER_INDEX,
1102 HIDPP_REG_CONNECTION_STATE,
1103 HIDPP_FAKE_DEVICE_ARRIVAL,
1108 hidpp_report = kmemdup(template, sizeof(template), GFP_KERNEL);
1112 retval = hid_hw_raw_request(djrcv_dev->hidpp,
1113 REPORT_ID_HIDPP_SHORT,
1114 hidpp_report, sizeof(template),
1116 HID_REQ_SET_REPORT);
1118 kfree(hidpp_report);
1122 static int logi_dj_recv_query_paired_devices(struct dj_receiver_dev *djrcv_dev)
1124 struct dj_report *dj_report;
1127 djrcv_dev->last_query = jiffies;
1129 if (djrcv_dev->type != recvr_type_dj)
1130 return logi_dj_recv_query_hidpp_devices(djrcv_dev);
1132 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
1135 dj_report->report_id = REPORT_ID_DJ_SHORT;
1136 dj_report->device_index = 0xFF;
1137 dj_report->report_type = REPORT_TYPE_CMD_GET_PAIRED_DEVICES;
1138 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1144 static int logi_dj_recv_switch_to_dj_mode(struct dj_receiver_dev *djrcv_dev,
1147 struct hid_device *hdev = djrcv_dev->hidpp;
1148 struct dj_report *dj_report;
1152 dj_report = kzalloc(sizeof(struct dj_report), GFP_KERNEL);
1156 if (djrcv_dev->type == recvr_type_dj) {
1157 dj_report->report_id = REPORT_ID_DJ_SHORT;
1158 dj_report->device_index = 0xFF;
1159 dj_report->report_type = REPORT_TYPE_CMD_SWITCH;
1160 dj_report->report_params[CMD_SWITCH_PARAM_DEVBITFIELD] = 0x3F;
1161 dj_report->report_params[CMD_SWITCH_PARAM_TIMEOUT_SECONDS] =
1164 retval = logi_dj_recv_send_report(djrcv_dev, dj_report);
1167 * Ugly sleep to work around a USB 3.0 bug when the receiver is
1168 * still processing the "switch-to-dj" command while we send an
1170 * 50 msec should gives enough time to the receiver to be ready.
1176 * Magical bits to set up hidpp notifications when the dj devices
1177 * are connected/disconnected.
1179 * We can reuse dj_report because HIDPP_REPORT_SHORT_LENGTH is smaller
1180 * than DJREPORT_SHORT_LENGTH.
1182 buf = (u8 *)dj_report;
1184 memset(buf, 0, HIDPP_REPORT_SHORT_LENGTH);
1186 buf[0] = REPORT_ID_HIDPP_SHORT;
1194 hid_hw_raw_request(hdev, REPORT_ID_HIDPP_SHORT, buf,
1195 HIDPP_REPORT_SHORT_LENGTH, HID_OUTPUT_REPORT,
1196 HID_REQ_SET_REPORT);
1203 static int logi_dj_ll_open(struct hid_device *hid)
1205 dbg_hid("%s: %s\n", __func__, hid->phys);
1210 static void logi_dj_ll_close(struct hid_device *hid)
1212 dbg_hid("%s: %s\n", __func__, hid->phys);
1216 * Register 0xB5 is "pairing information". It is solely intended for the
1217 * receiver, so do not overwrite the device index.
1219 static u8 unifying_pairing_query[] = { REPORT_ID_HIDPP_SHORT,
1220 HIDPP_RECEIVER_INDEX,
1221 HIDPP_GET_LONG_REGISTER,
1222 HIDPP_REG_PAIRING_INFORMATION };
1223 static u8 unifying_pairing_answer[] = { REPORT_ID_HIDPP_LONG,
1224 HIDPP_RECEIVER_INDEX,
1225 HIDPP_GET_LONG_REGISTER,
1226 HIDPP_REG_PAIRING_INFORMATION };
1228 static int logi_dj_ll_raw_request(struct hid_device *hid,
1229 unsigned char reportnum, __u8 *buf,
1230 size_t count, unsigned char report_type,
1233 struct dj_device *djdev = hid->driver_data;
1234 struct dj_receiver_dev *djrcv_dev = djdev->dj_receiver_dev;
1238 if ((buf[0] == REPORT_ID_HIDPP_SHORT) ||
1239 (buf[0] == REPORT_ID_HIDPP_LONG)) {
1243 /* special case where we should not overwrite
1244 * the device_index */
1245 if (count == 7 && !memcmp(buf, unifying_pairing_query,
1246 sizeof(unifying_pairing_query)))
1247 buf[4] = (buf[4] & 0xf0) | (djdev->device_index - 1);
1249 buf[1] = djdev->device_index;
1250 return hid_hw_raw_request(djrcv_dev->hidpp, reportnum, buf,
1251 count, report_type, reqtype);
1254 if (buf[0] != REPORT_TYPE_LEDS)
1257 if (djrcv_dev->type != recvr_type_dj && count >= 2) {
1258 if (!djrcv_dev->keyboard) {
1259 hid_warn(hid, "Received REPORT_TYPE_LEDS request before the keyboard interface was enumerated\n");
1262 /* usbhid overrides the report ID and ignores the first byte */
1263 return hid_hw_raw_request(djrcv_dev->keyboard, 0, buf, count,
1264 report_type, reqtype);
1267 out_buf = kzalloc(DJREPORT_SHORT_LENGTH, GFP_ATOMIC);
1271 if (count > DJREPORT_SHORT_LENGTH - 2)
1272 count = DJREPORT_SHORT_LENGTH - 2;
1274 out_buf[0] = REPORT_ID_DJ_SHORT;
1275 out_buf[1] = djdev->device_index;
1276 memcpy(out_buf + 2, buf, count);
1278 ret = hid_hw_raw_request(djrcv_dev->hidpp, out_buf[0], out_buf,
1279 DJREPORT_SHORT_LENGTH, report_type, reqtype);
1285 static void rdcat(char *rdesc, unsigned int *rsize, const char *data, unsigned int size)
1287 memcpy(rdesc + *rsize, data, size);
1291 static int logi_dj_ll_parse(struct hid_device *hid)
1293 struct dj_device *djdev = hid->driver_data;
1294 unsigned int rsize = 0;
1298 dbg_hid("%s\n", __func__);
1300 djdev->hdev->version = 0x0111;
1301 djdev->hdev->country = 0x00;
1303 rdesc = kmalloc(MAX_RDESC_SIZE, GFP_KERNEL);
1307 if (djdev->reports_supported & STD_KEYBOARD) {
1308 dbg_hid("%s: sending a kbd descriptor, reports_supported: %llx\n",
1309 __func__, djdev->reports_supported);
1310 rdcat(rdesc, &rsize, kbd_descriptor, sizeof(kbd_descriptor));
1313 if (djdev->reports_supported & STD_MOUSE) {
1314 dbg_hid("%s: sending a mouse descriptor, reports_supported: %llx\n",
1315 __func__, djdev->reports_supported);
1316 if (djdev->dj_receiver_dev->type == recvr_type_gaming_hidpp)
1317 rdcat(rdesc, &rsize, mse_high_res_descriptor,
1318 sizeof(mse_high_res_descriptor));
1319 else if (djdev->dj_receiver_dev->type == recvr_type_27mhz)
1320 rdcat(rdesc, &rsize, mse_27mhz_descriptor,
1321 sizeof(mse_27mhz_descriptor));
1322 else if (djdev->dj_receiver_dev->type == recvr_type_bluetooth)
1323 rdcat(rdesc, &rsize, mse_bluetooth_descriptor,
1324 sizeof(mse_bluetooth_descriptor));
1326 rdcat(rdesc, &rsize, mse_descriptor,
1327 sizeof(mse_descriptor));
1330 if (djdev->reports_supported & MULTIMEDIA) {
1331 dbg_hid("%s: sending a multimedia report descriptor: %llx\n",
1332 __func__, djdev->reports_supported);
1333 rdcat(rdesc, &rsize, consumer_descriptor, sizeof(consumer_descriptor));
1336 if (djdev->reports_supported & POWER_KEYS) {
1337 dbg_hid("%s: sending a power keys report descriptor: %llx\n",
1338 __func__, djdev->reports_supported);
1339 rdcat(rdesc, &rsize, syscontrol_descriptor, sizeof(syscontrol_descriptor));
1342 if (djdev->reports_supported & MEDIA_CENTER) {
1343 dbg_hid("%s: sending a media center report descriptor: %llx\n",
1344 __func__, djdev->reports_supported);
1345 rdcat(rdesc, &rsize, media_descriptor, sizeof(media_descriptor));
1348 if (djdev->reports_supported & KBD_LEDS) {
1349 dbg_hid("%s: need to send kbd leds report descriptor: %llx\n",
1350 __func__, djdev->reports_supported);
1353 if (djdev->reports_supported & HIDPP) {
1354 rdcat(rdesc, &rsize, hidpp_descriptor,
1355 sizeof(hidpp_descriptor));
1358 retval = hid_parse_report(hid, rdesc, rsize);
1364 static int logi_dj_ll_start(struct hid_device *hid)
1366 dbg_hid("%s\n", __func__);
1370 static void logi_dj_ll_stop(struct hid_device *hid)
1372 dbg_hid("%s\n", __func__);
1376 static struct hid_ll_driver logi_dj_ll_driver = {
1377 .parse = logi_dj_ll_parse,
1378 .start = logi_dj_ll_start,
1379 .stop = logi_dj_ll_stop,
1380 .open = logi_dj_ll_open,
1381 .close = logi_dj_ll_close,
1382 .raw_request = logi_dj_ll_raw_request,
1385 static int logi_dj_dj_event(struct hid_device *hdev,
1386 struct hid_report *report, u8 *data,
1389 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1390 struct dj_report *dj_report = (struct dj_report *) data;
1391 unsigned long flags;
1394 * Here we receive all data coming from iface 2, there are 3 cases:
1396 * 1) Data is intended for this driver i. e. data contains arrival,
1397 * departure, etc notifications, in which case we queue them for delayed
1398 * processing by the work queue. We return 1 to hid-core as no further
1399 * processing is required from it.
1401 * 2) Data informs a connection change, if the change means rf link
1402 * loss, then we must send a null report to the upper layer to discard
1403 * potentially pressed keys that may be repeated forever by the input
1404 * layer. Return 1 to hid-core as no further processing is required.
1406 * 3) Data is an actual input event from a paired DJ device in which
1407 * case we forward it to the correct hid device (via hid_input_report()
1408 * ) and return 1 so hid-core does not anything else with it.
1411 if ((dj_report->device_index < DJ_DEVICE_INDEX_MIN) ||
1412 (dj_report->device_index > DJ_DEVICE_INDEX_MAX)) {
1414 * Device index is wrong, bail out.
1415 * This driver can ignore safely the receiver notifications,
1416 * so ignore those reports too.
1418 if (dj_report->device_index != DJ_RECEIVER_INDEX)
1419 hid_err(hdev, "%s: invalid device index:%d\n",
1420 __func__, dj_report->device_index);
1424 spin_lock_irqsave(&djrcv_dev->lock, flags);
1426 if (!djrcv_dev->paired_dj_devices[dj_report->device_index]) {
1427 /* received an event for an unknown device, bail out */
1428 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1432 switch (dj_report->report_type) {
1433 case REPORT_TYPE_NOTIF_DEVICE_PAIRED:
1434 /* pairing notifications are handled above the switch */
1436 case REPORT_TYPE_NOTIF_DEVICE_UNPAIRED:
1437 logi_dj_recv_queue_notification(djrcv_dev, dj_report);
1439 case REPORT_TYPE_NOTIF_CONNECTION_STATUS:
1440 if (dj_report->report_params[CONNECTION_STATUS_PARAM_STATUS] ==
1442 logi_dj_recv_forward_null_report(djrcv_dev, dj_report);
1446 logi_dj_recv_forward_dj(djrcv_dev, dj_report);
1450 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1455 static int logi_dj_hidpp_event(struct hid_device *hdev,
1456 struct hid_report *report, u8 *data,
1459 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1460 struct hidpp_event *hidpp_report = (struct hidpp_event *) data;
1461 struct dj_device *dj_dev;
1462 unsigned long flags;
1463 u8 device_index = hidpp_report->device_index;
1465 if (device_index == HIDPP_RECEIVER_INDEX) {
1466 /* special case were the device wants to know its unifying
1468 if (size == HIDPP_REPORT_LONG_LENGTH &&
1469 !memcmp(data, unifying_pairing_answer,
1470 sizeof(unifying_pairing_answer)))
1471 device_index = (data[4] & 0x0F) + 1;
1477 * Data is from the HID++ collection, in this case, we forward the
1478 * data to the corresponding child dj device and return 0 to hid-core
1479 * so he data also goes to the hidraw device of the receiver. This
1480 * allows a user space application to implement the full HID++ routing
1484 if ((device_index < DJ_DEVICE_INDEX_MIN) ||
1485 (device_index > DJ_DEVICE_INDEX_MAX)) {
1487 * Device index is wrong, bail out.
1488 * This driver can ignore safely the receiver notifications,
1489 * so ignore those reports too.
1491 hid_err(hdev, "%s: invalid device index:%d\n", __func__,
1492 hidpp_report->device_index);
1496 spin_lock_irqsave(&djrcv_dev->lock, flags);
1498 dj_dev = djrcv_dev->paired_dj_devices[device_index];
1501 * With 27 MHz receivers, we do not get an explicit unpair event,
1502 * remove the old device if the user has paired a *different* device.
1504 if (djrcv_dev->type == recvr_type_27mhz && dj_dev &&
1505 hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED &&
1506 hidpp_report->params[HIDPP_PARAM_PROTO_TYPE] == 0x02 &&
1507 hidpp_report->params[HIDPP_PARAM_27MHZ_DEVID] !=
1508 dj_dev->hdev->product) {
1509 struct dj_workitem workitem = {
1510 .device_index = hidpp_report->device_index,
1511 .type = WORKITEM_TYPE_UNPAIRED,
1513 kfifo_in(&djrcv_dev->notif_fifo, &workitem, sizeof(workitem));
1514 /* logi_hidpp_recv_queue_notif will queue the work */
1519 logi_dj_recv_forward_report(dj_dev, data, size);
1521 if (hidpp_report->sub_id == REPORT_TYPE_NOTIF_DEVICE_CONNECTED)
1522 logi_hidpp_recv_queue_notif(hdev, hidpp_report);
1524 logi_dj_recv_queue_unknown_work(djrcv_dev);
1527 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1532 static int logi_dj_raw_event(struct hid_device *hdev,
1533 struct hid_report *report, u8 *data,
1536 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1537 dbg_hid("%s, size:%d\n", __func__, size);
1542 if (!hdev->report_enum[HID_INPUT_REPORT].numbered) {
1544 if (djrcv_dev->unnumbered_application == HID_GD_KEYBOARD) {
1546 * For the keyboard, we can reuse the same report by
1547 * using the second byte which is constant in the USB
1548 * HID report descriptor.
1551 data[0] = REPORT_TYPE_KEYBOARD;
1553 logi_dj_recv_forward_input_report(hdev, data, size);
1555 /* restore previous state */
1559 /* The 27 MHz mouse-only receiver sends unnumbered mouse data */
1560 if (djrcv_dev->unnumbered_application == HID_GD_MOUSE &&
1564 /* Prepend report id */
1565 mouse_report[0] = REPORT_TYPE_MOUSE;
1566 memcpy(mouse_report + 1, data, 6);
1567 logi_dj_recv_forward_input_report(hdev, mouse_report, 7);
1574 case REPORT_ID_DJ_SHORT:
1575 if (size != DJREPORT_SHORT_LENGTH) {
1576 hid_err(hdev, "Short DJ report bad size (%d)", size);
1579 return logi_dj_dj_event(hdev, report, data, size);
1580 case REPORT_ID_DJ_LONG:
1581 if (size != DJREPORT_LONG_LENGTH) {
1582 hid_err(hdev, "Long DJ report bad size (%d)", size);
1585 return logi_dj_dj_event(hdev, report, data, size);
1586 case REPORT_ID_HIDPP_SHORT:
1587 if (size != HIDPP_REPORT_SHORT_LENGTH) {
1588 hid_err(hdev, "Short HID++ report bad size (%d)", size);
1591 return logi_dj_hidpp_event(hdev, report, data, size);
1592 case REPORT_ID_HIDPP_LONG:
1593 if (size != HIDPP_REPORT_LONG_LENGTH) {
1594 hid_err(hdev, "Long HID++ report bad size (%d)", size);
1597 return logi_dj_hidpp_event(hdev, report, data, size);
1600 logi_dj_recv_forward_input_report(hdev, data, size);
1605 static int logi_dj_probe(struct hid_device *hdev,
1606 const struct hid_device_id *id)
1608 struct hid_report_enum *rep_enum;
1609 struct hid_report *rep;
1610 struct dj_receiver_dev *djrcv_dev;
1611 struct usb_interface *intf;
1612 unsigned int no_dj_interfaces = 0;
1613 bool has_hidpp = false;
1614 unsigned long flags;
1618 * Call to usbhid to fetch the HID descriptors of the current
1619 * interface subsequently call to the hid/hid-core to parse the
1620 * fetched descriptors.
1622 retval = hid_parse(hdev);
1624 hid_err(hdev, "%s: parse failed\n", __func__);
1629 * Some KVMs add an extra interface for e.g. mouse emulation. If we
1630 * treat these as logitech-dj interfaces then this causes input events
1631 * reported through this extra interface to not be reported correctly.
1632 * To avoid this, we treat these as generic-hid devices.
1634 switch (id->driver_data) {
1635 case recvr_type_dj: no_dj_interfaces = 3; break;
1636 case recvr_type_hidpp: no_dj_interfaces = 2; break;
1637 case recvr_type_gaming_hidpp: no_dj_interfaces = 3; break;
1638 case recvr_type_27mhz: no_dj_interfaces = 2; break;
1639 case recvr_type_bluetooth: no_dj_interfaces = 2; break;
1641 if (hid_is_using_ll_driver(hdev, &usb_hid_driver)) {
1642 intf = to_usb_interface(hdev->dev.parent);
1643 if (intf && intf->altsetting->desc.bInterfaceNumber >=
1645 hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
1646 return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1650 rep_enum = &hdev->report_enum[HID_INPUT_REPORT];
1652 /* no input reports, bail out */
1653 if (list_empty(&rep_enum->report_list))
1657 * Check for the HID++ application.
1658 * Note: we should theoretically check for HID++ and DJ
1659 * collections, but this will do.
1661 list_for_each_entry(rep, &rep_enum->report_list, list) {
1662 if (rep->application == 0xff000001)
1667 * Ignore interfaces without DJ/HID++ collection, they will not carry
1668 * any data, dont create any hid_device for them.
1670 if (!has_hidpp && id->driver_data == recvr_type_dj)
1673 /* get the current application attached to the node */
1674 rep = list_first_entry(&rep_enum->report_list, struct hid_report, list);
1675 djrcv_dev = dj_get_receiver_dev(hdev, id->driver_data,
1676 rep->application, has_hidpp);
1678 hid_err(hdev, "%s: dj_get_receiver_dev failed\n", __func__);
1682 if (!rep_enum->numbered)
1683 djrcv_dev->unnumbered_application = rep->application;
1685 /* Starts the usb device and connects to upper interfaces hiddev and
1687 retval = hid_hw_start(hdev, HID_CONNECT_HIDRAW|HID_CONNECT_HIDDEV);
1689 hid_err(hdev, "%s: hid_hw_start returned error\n", __func__);
1690 goto hid_hw_start_fail;
1694 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1696 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1698 goto switch_to_dj_mode_fail;
1702 /* This is enabling the polling urb on the IN endpoint */
1703 retval = hid_hw_open(hdev);
1705 hid_err(hdev, "%s: hid_hw_open returned error:%d\n",
1710 /* Allow incoming packets to arrive: */
1711 hid_device_io_start(hdev);
1714 spin_lock_irqsave(&djrcv_dev->lock, flags);
1715 djrcv_dev->ready = true;
1716 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1717 retval = logi_dj_recv_query_paired_devices(djrcv_dev);
1719 hid_err(hdev, "%s: logi_dj_recv_query_paired_devices error:%d\n",
1721 goto logi_dj_recv_query_paired_devices_failed;
1727 logi_dj_recv_query_paired_devices_failed:
1731 switch_to_dj_mode_fail:
1735 dj_put_receiver_dev(hdev);
1740 static int logi_dj_reset_resume(struct hid_device *hdev)
1743 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1745 if (!djrcv_dev || djrcv_dev->hidpp != hdev)
1748 retval = logi_dj_recv_switch_to_dj_mode(djrcv_dev, 0);
1750 hid_err(hdev, "%s: logi_dj_recv_switch_to_dj_mode returned error:%d\n",
1758 static void logi_dj_remove(struct hid_device *hdev)
1760 struct dj_receiver_dev *djrcv_dev = hid_get_drvdata(hdev);
1761 struct dj_device *dj_dev;
1762 unsigned long flags;
1765 dbg_hid("%s\n", __func__);
1768 return hid_hw_stop(hdev);
1771 * This ensures that if the work gets requeued from another
1772 * interface of the same receiver it will be a no-op.
1774 spin_lock_irqsave(&djrcv_dev->lock, flags);
1775 djrcv_dev->ready = false;
1776 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1778 cancel_work_sync(&djrcv_dev->work);
1784 * For proper operation we need access to all interfaces, so we destroy
1785 * the paired devices when we're unbound from any interface.
1787 * Note we may still be bound to other interfaces, sharing the same
1788 * djrcv_dev, so we need locking here.
1790 for (i = 0; i < (DJ_MAX_PAIRED_DEVICES + DJ_DEVICE_INDEX_MIN); i++) {
1791 spin_lock_irqsave(&djrcv_dev->lock, flags);
1792 dj_dev = djrcv_dev->paired_dj_devices[i];
1793 djrcv_dev->paired_dj_devices[i] = NULL;
1794 spin_unlock_irqrestore(&djrcv_dev->lock, flags);
1795 if (dj_dev != NULL) {
1796 hid_destroy_device(dj_dev->hdev);
1801 dj_put_receiver_dev(hdev);
1804 static const struct hid_device_id logi_dj_receivers[] = {
1805 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1806 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER),
1807 .driver_data = recvr_type_dj},
1808 {HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1809 USB_DEVICE_ID_LOGITECH_UNIFYING_RECEIVER_2),
1810 .driver_data = recvr_type_dj},
1811 { /* Logitech Nano (non DJ) receiver */
1812 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1813 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER),
1814 .driver_data = recvr_type_hidpp},
1815 { /* Logitech Nano (non DJ) receiver */
1816 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1817 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_2),
1818 .driver_data = recvr_type_hidpp},
1819 { /* Logitech gaming receiver (0xc539) */
1820 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1821 USB_DEVICE_ID_LOGITECH_NANO_RECEIVER_GAMING),
1822 .driver_data = recvr_type_gaming_hidpp},
1823 { /* Logitech 27 MHz HID++ 1.0 receiver (0xc517) */
1824 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1825 USB_DEVICE_ID_S510_RECEIVER_2),
1826 .driver_data = recvr_type_27mhz},
1827 { /* Logitech 27 MHz HID++ 1.0 mouse-only receiver (0xc51b) */
1828 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1829 USB_DEVICE_ID_LOGITECH_27MHZ_MOUSE_RECEIVER),
1830 .driver_data = recvr_type_27mhz},
1831 { /* Logitech MX5000 HID++ / bluetooth receiver keyboard intf. */
1832 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1834 .driver_data = recvr_type_bluetooth},
1835 { /* Logitech MX5000 HID++ / bluetooth receiver mouse intf. */
1836 HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH,
1838 .driver_data = recvr_type_bluetooth},
1842 MODULE_DEVICE_TABLE(hid, logi_dj_receivers);
1844 static struct hid_driver logi_djreceiver_driver = {
1845 .name = "logitech-djreceiver",
1846 .id_table = logi_dj_receivers,
1847 .probe = logi_dj_probe,
1848 .remove = logi_dj_remove,
1849 .raw_event = logi_dj_raw_event,
1851 .reset_resume = logi_dj_reset_resume,
1855 module_hid_driver(logi_djreceiver_driver);
1857 MODULE_LICENSE("GPL");
1858 MODULE_AUTHOR("Logitech");
1859 MODULE_AUTHOR("Nestor Lopez Casado");
1860 MODULE_AUTHOR("nlopezcasad@logitech.com");