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