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