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