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