Merge branch 'sched-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.git] / drivers / hid / hid-sony.c
1 /*
2  *  HID driver for Sony / PS2 / PS3 / PS4 BD devices.
3  *
4  *  Copyright (c) 1999 Andreas Gal
5  *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6  *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
7  *  Copyright (c) 2008 Jiri Slaby
8  *  Copyright (c) 2012 David Dillow <dave@thedillows.org>
9  *  Copyright (c) 2006-2013 Jiri Kosina
10  *  Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
11  *  Copyright (c) 2014 Frank Praznik <frank.praznik@gmail.com>
12  */
13
14 /*
15  * This program is free software; you can redistribute it and/or modify it
16  * under the terms of the GNU General Public License as published by the Free
17  * Software Foundation; either version 2 of the License, or (at your option)
18  * any later version.
19  */
20
21 /*
22  * NOTE: in order for the Sony PS3 BD Remote Control to be found by
23  * a Bluetooth host, the key combination Start+Enter has to be kept pressed
24  * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
25  *
26  * There will be no PIN request from the device.
27  */
28
29 #include <linux/device.h>
30 #include <linux/hid.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/leds.h>
34 #include <linux/power_supply.h>
35 #include <linux/spinlock.h>
36 #include <linux/list.h>
37 #include <linux/idr.h>
38 #include <linux/input/mt.h>
39
40 #include "hid-ids.h"
41
42 #define VAIO_RDESC_CONSTANT       BIT(0)
43 #define SIXAXIS_CONTROLLER_USB    BIT(1)
44 #define SIXAXIS_CONTROLLER_BT     BIT(2)
45 #define BUZZ_CONTROLLER           BIT(3)
46 #define PS3REMOTE                 BIT(4)
47 #define DUALSHOCK4_CONTROLLER_USB BIT(5)
48 #define DUALSHOCK4_CONTROLLER_BT  BIT(6)
49 #define MOTION_CONTROLLER_USB     BIT(7)
50 #define MOTION_CONTROLLER_BT      BIT(8)
51 #define NAVIGATION_CONTROLLER_USB BIT(9)
52 #define NAVIGATION_CONTROLLER_BT  BIT(10)
53
54 #define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
55 #define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
56 #define NAVIGATION_CONTROLLER (NAVIGATION_CONTROLLER_USB |\
57                                 NAVIGATION_CONTROLLER_BT)
58 #define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
59                                 DUALSHOCK4_CONTROLLER_BT)
60 #define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
61                                 DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER |\
62                                 NAVIGATION_CONTROLLER)
63 #define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
64                                 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER)
65 #define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
66                                 MOTION_CONTROLLER)
67
68 #define MAX_LEDS 4
69
70 /*
71  * The Sixaxis reports both digital and analog values for each button on the
72  * controller except for Start, Select and the PS button.  The controller ends
73  * up reporting 27 axes which causes them to spill over into the multi-touch
74  * axis values.  Additionally, the controller only has 20 actual, physical axes
75  * so there are several unused axes in between the used ones.
76  */
77 static __u8 sixaxis_rdesc[] = {
78         0x05, 0x01,         /*  Usage Page (Desktop),               */
79         0x09, 0x04,         /*  Usage (Joystick),                   */
80         0xA1, 0x01,         /*  Collection (Application),           */
81         0xA1, 0x02,         /*      Collection (Logical),           */
82         0x85, 0x01,         /*          Report ID (1),              */
83         0x75, 0x08,         /*          Report Size (8),            */
84         0x95, 0x01,         /*          Report Count (1),           */
85         0x15, 0x00,         /*          Logical Minimum (0),        */
86         0x26, 0xFF, 0x00,   /*          Logical Maximum (255),      */
87         0x81, 0x03,         /*          Input (Constant, Variable), */
88         0x75, 0x01,         /*          Report Size (1),            */
89         0x95, 0x13,         /*          Report Count (19),          */
90         0x15, 0x00,         /*          Logical Minimum (0),        */
91         0x25, 0x01,         /*          Logical Maximum (1),        */
92         0x35, 0x00,         /*          Physical Minimum (0),       */
93         0x45, 0x01,         /*          Physical Maximum (1),       */
94         0x05, 0x09,         /*          Usage Page (Button),        */
95         0x19, 0x01,         /*          Usage Minimum (01h),        */
96         0x29, 0x13,         /*          Usage Maximum (13h),        */
97         0x81, 0x02,         /*          Input (Variable),           */
98         0x75, 0x01,         /*          Report Size (1),            */
99         0x95, 0x0D,         /*          Report Count (13),          */
100         0x06, 0x00, 0xFF,   /*          Usage Page (FF00h),         */
101         0x81, 0x03,         /*          Input (Constant, Variable), */
102         0x15, 0x00,         /*          Logical Minimum (0),        */
103         0x26, 0xFF, 0x00,   /*          Logical Maximum (255),      */
104         0x05, 0x01,         /*          Usage Page (Desktop),       */
105         0x09, 0x01,         /*          Usage (Pointer),            */
106         0xA1, 0x00,         /*          Collection (Physical),      */
107         0x75, 0x08,         /*              Report Size (8),        */
108         0x95, 0x04,         /*              Report Count (4),       */
109         0x35, 0x00,         /*              Physical Minimum (0),   */
110         0x46, 0xFF, 0x00,   /*              Physical Maximum (255), */
111         0x09, 0x30,         /*              Usage (X),              */
112         0x09, 0x31,         /*              Usage (Y),              */
113         0x09, 0x32,         /*              Usage (Z),              */
114         0x09, 0x35,         /*              Usage (Rz),             */
115         0x81, 0x02,         /*              Input (Variable),       */
116         0xC0,               /*          End Collection,             */
117         0x05, 0x01,         /*          Usage Page (Desktop),       */
118         0x95, 0x13,         /*          Report Count (19),          */
119         0x09, 0x01,         /*          Usage (Pointer),            */
120         0x81, 0x02,         /*          Input (Variable),           */
121         0x95, 0x0C,         /*          Report Count (12),          */
122         0x81, 0x01,         /*          Input (Constant),           */
123         0x75, 0x10,         /*          Report Size (16),           */
124         0x95, 0x04,         /*          Report Count (4),           */
125         0x26, 0xFF, 0x03,   /*          Logical Maximum (1023),     */
126         0x46, 0xFF, 0x03,   /*          Physical Maximum (1023),    */
127         0x09, 0x01,         /*          Usage (Pointer),            */
128         0x81, 0x02,         /*          Input (Variable),           */
129         0xC0,               /*      End Collection,                 */
130         0xA1, 0x02,         /*      Collection (Logical),           */
131         0x85, 0x02,         /*          Report ID (2),              */
132         0x75, 0x08,         /*          Report Size (8),            */
133         0x95, 0x30,         /*          Report Count (48),          */
134         0x09, 0x01,         /*          Usage (Pointer),            */
135         0xB1, 0x02,         /*          Feature (Variable),         */
136         0xC0,               /*      End Collection,                 */
137         0xA1, 0x02,         /*      Collection (Logical),           */
138         0x85, 0xEE,         /*          Report ID (238),            */
139         0x75, 0x08,         /*          Report Size (8),            */
140         0x95, 0x30,         /*          Report Count (48),          */
141         0x09, 0x01,         /*          Usage (Pointer),            */
142         0xB1, 0x02,         /*          Feature (Variable),         */
143         0xC0,               /*      End Collection,                 */
144         0xA1, 0x02,         /*      Collection (Logical),           */
145         0x85, 0xEF,         /*          Report ID (239),            */
146         0x75, 0x08,         /*          Report Size (8),            */
147         0x95, 0x30,         /*          Report Count (48),          */
148         0x09, 0x01,         /*          Usage (Pointer),            */
149         0xB1, 0x02,         /*          Feature (Variable),         */
150         0xC0,               /*      End Collection,                 */
151         0xC0                /*  End Collection                      */
152 };
153
154 /* PS/3 Motion controller */
155 static __u8 motion_rdesc[] = {
156         0x05, 0x01,         /*  Usage Page (Desktop),               */
157         0x09, 0x04,         /*  Usage (Joystick),                   */
158         0xA1, 0x01,         /*  Collection (Application),           */
159         0xA1, 0x02,         /*      Collection (Logical),           */
160         0x85, 0x01,         /*          Report ID (1),              */
161         0x75, 0x01,         /*          Report Size (1),            */
162         0x95, 0x15,         /*          Report Count (21),          */
163         0x15, 0x00,         /*          Logical Minimum (0),        */
164         0x25, 0x01,         /*          Logical Maximum (1),        */
165         0x35, 0x00,         /*          Physical Minimum (0),       */
166         0x45, 0x01,         /*          Physical Maximum (1),       */
167         0x05, 0x09,         /*          Usage Page (Button),        */
168         0x19, 0x01,         /*          Usage Minimum (01h),        */
169         0x29, 0x15,         /*          Usage Maximum (15h),        */
170         0x81, 0x02,         /*          Input (Variable),           * Buttons */
171         0x95, 0x0B,         /*          Report Count (11),          */
172         0x06, 0x00, 0xFF,   /*          Usage Page (FF00h),         */
173         0x81, 0x03,         /*          Input (Constant, Variable), * Padding */
174         0x15, 0x00,         /*          Logical Minimum (0),        */
175         0x26, 0xFF, 0x00,   /*          Logical Maximum (255),      */
176         0x05, 0x01,         /*          Usage Page (Desktop),       */
177         0xA1, 0x00,         /*          Collection (Physical),      */
178         0x75, 0x08,         /*              Report Size (8),        */
179         0x95, 0x01,         /*              Report Count (1),       */
180         0x35, 0x00,         /*              Physical Minimum (0),   */
181         0x46, 0xFF, 0x00,   /*              Physical Maximum (255), */
182         0x09, 0x30,         /*              Usage (X),              */
183         0x81, 0x02,         /*              Input (Variable),       * Trigger */
184         0xC0,               /*          End Collection,             */
185         0x06, 0x00, 0xFF,   /*          Usage Page (FF00h),         */
186         0x75, 0x08,         /*          Report Size (8),            */
187         0x95, 0x07,         /*          Report Count (7),           * skip 7 bytes */
188         0x81, 0x02,         /*          Input (Variable),           */
189         0x05, 0x01,         /*          Usage Page (Desktop),       */
190         0x75, 0x10,         /*          Report Size (16),           */
191         0x46, 0xFF, 0xFF,   /*          Physical Maximum (65535),   */
192         0x27, 0xFF, 0xFF, 0x00, 0x00, /*      Logical Maximum (65535),    */
193         0x95, 0x03,         /*          Report Count (3),           * 3x Accels */
194         0x09, 0x33,         /*              Usage (rX),             */
195         0x09, 0x34,         /*              Usage (rY),             */
196         0x09, 0x35,         /*              Usage (rZ),             */
197         0x81, 0x02,         /*          Input (Variable),           */
198         0x06, 0x00, 0xFF,   /*          Usage Page (FF00h),         */
199         0x95, 0x03,         /*          Report Count (3),           * Skip Accels 2nd frame */
200         0x81, 0x02,         /*          Input (Variable),           */
201         0x05, 0x01,         /*          Usage Page (Desktop),       */
202         0x09, 0x01,         /*          Usage (Pointer),            */
203         0x95, 0x03,         /*          Report Count (3),           * 3x Gyros */
204         0x81, 0x02,         /*          Input (Variable),           */
205         0x06, 0x00, 0xFF,   /*          Usage Page (FF00h),         */
206         0x95, 0x03,         /*          Report Count (3),           * Skip Gyros 2nd frame */
207         0x81, 0x02,         /*          Input (Variable),           */
208         0x75, 0x0C,         /*          Report Size (12),           */
209         0x46, 0xFF, 0x0F,   /*          Physical Maximum (4095),    */
210         0x26, 0xFF, 0x0F,   /*          Logical Maximum (4095),     */
211         0x95, 0x04,         /*          Report Count (4),           * Skip Temp and Magnetometers */
212         0x81, 0x02,         /*          Input (Variable),           */
213         0x75, 0x08,         /*          Report Size (8),            */
214         0x46, 0xFF, 0x00,   /*          Physical Maximum (255),     */
215         0x26, 0xFF, 0x00,   /*          Logical Maximum (255),      */
216         0x95, 0x06,         /*          Report Count (6),           * Skip Timestamp and Extension Bytes */
217         0x81, 0x02,         /*          Input (Variable),           */
218         0x75, 0x08,         /*          Report Size (8),            */
219         0x95, 0x30,         /*          Report Count (48),          */
220         0x09, 0x01,         /*          Usage (Pointer),            */
221         0x91, 0x02,         /*          Output (Variable),          */
222         0x75, 0x08,         /*          Report Size (8),            */
223         0x95, 0x30,         /*          Report Count (48),          */
224         0x09, 0x01,         /*          Usage (Pointer),            */
225         0xB1, 0x02,         /*          Feature (Variable),         */
226         0xC0,               /*      End Collection,                 */
227         0xA1, 0x02,         /*      Collection (Logical),           */
228         0x85, 0x02,         /*          Report ID (2),              */
229         0x75, 0x08,         /*          Report Size (8),            */
230         0x95, 0x30,         /*          Report Count (48),          */
231         0x09, 0x01,         /*          Usage (Pointer),            */
232         0xB1, 0x02,         /*          Feature (Variable),         */
233         0xC0,               /*      End Collection,                 */
234         0xA1, 0x02,         /*      Collection (Logical),           */
235         0x85, 0xEE,         /*          Report ID (238),            */
236         0x75, 0x08,         /*          Report Size (8),            */
237         0x95, 0x30,         /*          Report Count (48),          */
238         0x09, 0x01,         /*          Usage (Pointer),            */
239         0xB1, 0x02,         /*          Feature (Variable),         */
240         0xC0,               /*      End Collection,                 */
241         0xA1, 0x02,         /*      Collection (Logical),           */
242         0x85, 0xEF,         /*          Report ID (239),            */
243         0x75, 0x08,         /*          Report Size (8),            */
244         0x95, 0x30,         /*          Report Count (48),          */
245         0x09, 0x01,         /*          Usage (Pointer),            */
246         0xB1, 0x02,         /*          Feature (Variable),         */
247         0xC0,               /*      End Collection,                 */
248         0xC0                /*  End Collection                      */
249 };
250
251 /* PS/3 Navigation controller */
252 static __u8 navigation_rdesc[] = {
253         0x05, 0x01,         /*  Usage Page (Desktop),               */
254         0x09, 0x04,         /*  Usage (Joystik),                    */
255         0xA1, 0x01,         /*  Collection (Application),           */
256         0xA1, 0x02,         /*      Collection (Logical),           */
257         0x85, 0x01,         /*          Report ID (1),              */
258         0x75, 0x08,         /*          Report Size (8),            */
259         0x95, 0x01,         /*          Report Count (1),           */
260         0x15, 0x00,         /*          Logical Minimum (0),        */
261         0x26, 0xFF, 0x00,   /*          Logical Maximum (255),      */
262         0x81, 0x03,         /*          Input (Constant, Variable), */
263         0x75, 0x01,         /*          Report Size (1),            */
264         0x95, 0x13,         /*          Report Count (19),          */
265         0x15, 0x00,         /*          Logical Minimum (0),        */
266         0x25, 0x01,         /*          Logical Maximum (1),        */
267         0x35, 0x00,         /*          Physical Minimum (0),       */
268         0x45, 0x01,         /*          Physical Maximum (1),       */
269         0x05, 0x09,         /*          Usage Page (Button),        */
270         0x19, 0x01,         /*          Usage Minimum (01h),        */
271         0x29, 0x13,         /*          Usage Maximum (13h),        */
272         0x81, 0x02,         /*          Input (Variable),           */
273         0x75, 0x01,         /*          Report Size (1),            */
274         0x95, 0x0D,         /*          Report Count (13),          */
275         0x06, 0x00, 0xFF,   /*          Usage Page (FF00h),         */
276         0x81, 0x03,         /*          Input (Constant, Variable), */
277         0x15, 0x00,         /*          Logical Minimum (0),        */
278         0x26, 0xFF, 0x00,   /*          Logical Maximum (255),      */
279         0x05, 0x01,         /*          Usage Page (Desktop),       */
280         0x09, 0x01,         /*          Usage (Pointer),            */
281         0xA1, 0x00,         /*          Collection (Physical),      */
282         0x75, 0x08,         /*              Report Size (8),        */
283         0x95, 0x02,         /*              Report Count (2),       */
284         0x35, 0x00,         /*              Physical Minimum (0),   */
285         0x46, 0xFF, 0x00,   /*              Physical Maximum (255), */
286         0x09, 0x30,         /*              Usage (X),              */
287         0x09, 0x31,         /*              Usage (Y),              */
288         0x81, 0x02,         /*              Input (Variable),       */
289         0xC0,               /*          End Collection,             */
290         0x06, 0x00, 0xFF,   /*          Usage Page (FF00h),         */
291         0x95, 0x06,         /*          Report Count (6),           */
292         0x81, 0x03,         /*          Input (Constant, Variable), */
293         0x05, 0x01,         /*          Usage Page (Desktop),       */
294         0x75, 0x08,         /*          Report Size (8),            */
295         0x95, 0x05,         /*          Report Count (5),           */
296         0x09, 0x01,         /*          Usage (Pointer),            */
297         0x81, 0x02,         /*          Input (Variable),           */
298         0x06, 0x00, 0xFF,   /*          Usage Page (FF00h),         */
299         0x95, 0x20,         /*          Report Count (26),          */
300         0x81, 0x02,         /*          Input (Variable),           */
301         0x75, 0x08,         /*          Report Size (8),            */
302         0x95, 0x30,         /*          Report Count (48),          */
303         0x09, 0x01,         /*          Usage (Pointer),            */
304         0x91, 0x02,         /*          Output (Variable),          */
305         0x75, 0x08,         /*          Report Size (8),            */
306         0x95, 0x30,         /*          Report Count (48),          */
307         0x09, 0x01,         /*          Usage (Pointer),            */
308         0xB1, 0x02,         /*          Feature (Variable),         */
309         0xC0,               /*      End Collection,                 */
310         0xA1, 0x02,         /*      Collection (Logical),           */
311         0x85, 0x02,         /*          Report ID (2),              */
312         0x75, 0x08,         /*          Report Size (8),            */
313         0x95, 0x30,         /*          Report Count (48),          */
314         0x09, 0x01,         /*          Usage (Pointer),            */
315         0xB1, 0x02,         /*          Feature (Variable),         */
316         0xC0,               /*      End Collection,                 */
317         0xA1, 0x02,         /*      Collection (Logical),           */
318         0x85, 0xEE,         /*          Report ID (238),            */
319         0x75, 0x08,         /*          Report Size (8),            */
320         0x95, 0x30,         /*          Report Count (48),          */
321         0x09, 0x01,         /*          Usage (Pointer),            */
322         0xB1, 0x02,         /*          Feature (Variable),         */
323         0xC0,               /*      End Collection,                 */
324         0xA1, 0x02,         /*      Collection (Logical),           */
325         0x85, 0xEF,         /*          Report ID (239),            */
326         0x75, 0x08,         /*          Report Size (8),            */
327         0x95, 0x30,         /*          Report Count (48),          */
328         0x09, 0x01,         /*          Usage (Pointer),            */
329         0xB1, 0x02,         /*          Feature (Variable),         */
330         0xC0,               /*      End Collection,                 */
331         0xC0                /*  End Collection                      */
332 };
333
334 /*
335  * The default descriptor doesn't provide mapping for the accelerometers
336  * or orientation sensors.  This fixed descriptor maps the accelerometers
337  * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
338  * to usage values 0x43, 0x44 and 0x45.
339  */
340 static u8 dualshock4_usb_rdesc[] = {
341         0x05, 0x01,         /*  Usage Page (Desktop),               */
342         0x09, 0x05,         /*  Usage (Gamepad),                    */
343         0xA1, 0x01,         /*  Collection (Application),           */
344         0x85, 0x01,         /*      Report ID (1),                  */
345         0x09, 0x30,         /*      Usage (X),                      */
346         0x09, 0x31,         /*      Usage (Y),                      */
347         0x09, 0x32,         /*      Usage (Z),                      */
348         0x09, 0x35,         /*      Usage (Rz),                     */
349         0x15, 0x00,         /*      Logical Minimum (0),            */
350         0x26, 0xFF, 0x00,   /*      Logical Maximum (255),          */
351         0x75, 0x08,         /*      Report Size (8),                */
352         0x95, 0x04,         /*      Report Count (4),               */
353         0x81, 0x02,         /*      Input (Variable),               */
354         0x09, 0x39,         /*      Usage (Hat Switch),             */
355         0x15, 0x00,         /*      Logical Minimum (0),            */
356         0x25, 0x07,         /*      Logical Maximum (7),            */
357         0x35, 0x00,         /*      Physical Minimum (0),           */
358         0x46, 0x3B, 0x01,   /*      Physical Maximum (315),         */
359         0x65, 0x14,         /*      Unit (Degrees),                 */
360         0x75, 0x04,         /*      Report Size (4),                */
361         0x95, 0x01,         /*      Report Count (1),               */
362         0x81, 0x42,         /*      Input (Variable, Null State),   */
363         0x65, 0x00,         /*      Unit,                           */
364         0x05, 0x09,         /*      Usage Page (Button),            */
365         0x19, 0x01,         /*      Usage Minimum (01h),            */
366         0x29, 0x0E,         /*      Usage Maximum (0Eh),            */
367         0x15, 0x00,         /*      Logical Minimum (0),            */
368         0x25, 0x01,         /*      Logical Maximum (1),            */
369         0x75, 0x01,         /*      Report Size (1),                */
370         0x95, 0x0E,         /*      Report Count (14),              */
371         0x81, 0x02,         /*      Input (Variable),               */
372         0x06, 0x00, 0xFF,   /*      Usage Page (FF00h),             */
373         0x09, 0x20,         /*      Usage (20h),                    */
374         0x75, 0x06,         /*      Report Size (6),                */
375         0x95, 0x01,         /*      Report Count (1),               */
376         0x15, 0x00,         /*      Logical Minimum (0),            */
377         0x25, 0x3F,         /*      Logical Maximum (63),           */
378         0x81, 0x02,         /*      Input (Variable),               */
379         0x05, 0x01,         /*      Usage Page (Desktop),           */
380         0x09, 0x33,         /*      Usage (Rx),                     */
381         0x09, 0x34,         /*      Usage (Ry),                     */
382         0x15, 0x00,         /*      Logical Minimum (0),            */
383         0x26, 0xFF, 0x00,   /*      Logical Maximum (255),          */
384         0x75, 0x08,         /*      Report Size (8),                */
385         0x95, 0x02,         /*      Report Count (2),               */
386         0x81, 0x02,         /*      Input (Variable),               */
387         0x06, 0x00, 0xFF,   /*      Usage Page (FF00h),             */
388         0x09, 0x21,         /*      Usage (21h),                    */
389         0x95, 0x03,         /*      Report Count (3),               */
390         0x81, 0x02,         /*      Input (Variable),               */
391         0x05, 0x01,         /*      Usage Page (Desktop),           */
392         0x19, 0x40,         /*      Usage Minimum (40h),            */
393         0x29, 0x42,         /*      Usage Maximum (42h),            */
394         0x16, 0x00, 0x80,   /*      Logical Minimum (-32768),       */
395         0x26, 0x00, 0x7F,   /*      Logical Maximum (32767),        */
396         0x75, 0x10,         /*      Report Size (16),               */
397         0x95, 0x03,         /*      Report Count (3),               */
398         0x81, 0x02,         /*      Input (Variable),               */
399         0x19, 0x43,         /*      Usage Minimum (43h),            */
400         0x29, 0x45,         /*      Usage Maximum (45h),            */
401         0x16, 0x00, 0xE0,   /*      Logical Minimum (-8192),        */
402         0x26, 0xFF, 0x1F,   /*      Logical Maximum (8191),         */
403         0x95, 0x03,         /*      Report Count (3),               */
404         0x81, 0x02,         /*      Input (Variable),               */
405         0x06, 0x00, 0xFF,   /*      Usage Page (FF00h),             */
406         0x09, 0x21,         /*      Usage (21h),                    */
407         0x15, 0x00,         /*      Logical Minimum (0),            */
408         0x26, 0xFF, 0x00,   /*      Logical Maximum (255),          */
409         0x75, 0x08,         /*      Report Size (8),                */
410         0x95, 0x27,         /*      Report Count (39),              */
411         0x81, 0x02,         /*      Input (Variable),               */
412         0x85, 0x05,         /*      Report ID (5),                  */
413         0x09, 0x22,         /*      Usage (22h),                    */
414         0x95, 0x1F,         /*      Report Count (31),              */
415         0x91, 0x02,         /*      Output (Variable),              */
416         0x85, 0x04,         /*      Report ID (4),                  */
417         0x09, 0x23,         /*      Usage (23h),                    */
418         0x95, 0x24,         /*      Report Count (36),              */
419         0xB1, 0x02,         /*      Feature (Variable),             */
420         0x85, 0x02,         /*      Report ID (2),                  */
421         0x09, 0x24,         /*      Usage (24h),                    */
422         0x95, 0x24,         /*      Report Count (36),              */
423         0xB1, 0x02,         /*      Feature (Variable),             */
424         0x85, 0x08,         /*      Report ID (8),                  */
425         0x09, 0x25,         /*      Usage (25h),                    */
426         0x95, 0x03,         /*      Report Count (3),               */
427         0xB1, 0x02,         /*      Feature (Variable),             */
428         0x85, 0x10,         /*      Report ID (16),                 */
429         0x09, 0x26,         /*      Usage (26h),                    */
430         0x95, 0x04,         /*      Report Count (4),               */
431         0xB1, 0x02,         /*      Feature (Variable),             */
432         0x85, 0x11,         /*      Report ID (17),                 */
433         0x09, 0x27,         /*      Usage (27h),                    */
434         0x95, 0x02,         /*      Report Count (2),               */
435         0xB1, 0x02,         /*      Feature (Variable),             */
436         0x85, 0x12,         /*      Report ID (18),                 */
437         0x06, 0x02, 0xFF,   /*      Usage Page (FF02h),             */
438         0x09, 0x21,         /*      Usage (21h),                    */
439         0x95, 0x0F,         /*      Report Count (15),              */
440         0xB1, 0x02,         /*      Feature (Variable),             */
441         0x85, 0x13,         /*      Report ID (19),                 */
442         0x09, 0x22,         /*      Usage (22h),                    */
443         0x95, 0x16,         /*      Report Count (22),              */
444         0xB1, 0x02,         /*      Feature (Variable),             */
445         0x85, 0x14,         /*      Report ID (20),                 */
446         0x06, 0x05, 0xFF,   /*      Usage Page (FF05h),             */
447         0x09, 0x20,         /*      Usage (20h),                    */
448         0x95, 0x10,         /*      Report Count (16),              */
449         0xB1, 0x02,         /*      Feature (Variable),             */
450         0x85, 0x15,         /*      Report ID (21),                 */
451         0x09, 0x21,         /*      Usage (21h),                    */
452         0x95, 0x2C,         /*      Report Count (44),              */
453         0xB1, 0x02,         /*      Feature (Variable),             */
454         0x06, 0x80, 0xFF,   /*      Usage Page (FF80h),             */
455         0x85, 0x80,         /*      Report ID (128),                */
456         0x09, 0x20,         /*      Usage (20h),                    */
457         0x95, 0x06,         /*      Report Count (6),               */
458         0xB1, 0x02,         /*      Feature (Variable),             */
459         0x85, 0x81,         /*      Report ID (129),                */
460         0x09, 0x21,         /*      Usage (21h),                    */
461         0x95, 0x06,         /*      Report Count (6),               */
462         0xB1, 0x02,         /*      Feature (Variable),             */
463         0x85, 0x82,         /*      Report ID (130),                */
464         0x09, 0x22,         /*      Usage (22h),                    */
465         0x95, 0x05,         /*      Report Count (5),               */
466         0xB1, 0x02,         /*      Feature (Variable),             */
467         0x85, 0x83,         /*      Report ID (131),                */
468         0x09, 0x23,         /*      Usage (23h),                    */
469         0x95, 0x01,         /*      Report Count (1),               */
470         0xB1, 0x02,         /*      Feature (Variable),             */
471         0x85, 0x84,         /*      Report ID (132),                */
472         0x09, 0x24,         /*      Usage (24h),                    */
473         0x95, 0x04,         /*      Report Count (4),               */
474         0xB1, 0x02,         /*      Feature (Variable),             */
475         0x85, 0x85,         /*      Report ID (133),                */
476         0x09, 0x25,         /*      Usage (25h),                    */
477         0x95, 0x06,         /*      Report Count (6),               */
478         0xB1, 0x02,         /*      Feature (Variable),             */
479         0x85, 0x86,         /*      Report ID (134),                */
480         0x09, 0x26,         /*      Usage (26h),                    */
481         0x95, 0x06,         /*      Report Count (6),               */
482         0xB1, 0x02,         /*      Feature (Variable),             */
483         0x85, 0x87,         /*      Report ID (135),                */
484         0x09, 0x27,         /*      Usage (27h),                    */
485         0x95, 0x23,         /*      Report Count (35),              */
486         0xB1, 0x02,         /*      Feature (Variable),             */
487         0x85, 0x88,         /*      Report ID (136),                */
488         0x09, 0x28,         /*      Usage (28h),                    */
489         0x95, 0x22,         /*      Report Count (34),              */
490         0xB1, 0x02,         /*      Feature (Variable),             */
491         0x85, 0x89,         /*      Report ID (137),                */
492         0x09, 0x29,         /*      Usage (29h),                    */
493         0x95, 0x02,         /*      Report Count (2),               */
494         0xB1, 0x02,         /*      Feature (Variable),             */
495         0x85, 0x90,         /*      Report ID (144),                */
496         0x09, 0x30,         /*      Usage (30h),                    */
497         0x95, 0x05,         /*      Report Count (5),               */
498         0xB1, 0x02,         /*      Feature (Variable),             */
499         0x85, 0x91,         /*      Report ID (145),                */
500         0x09, 0x31,         /*      Usage (31h),                    */
501         0x95, 0x03,         /*      Report Count (3),               */
502         0xB1, 0x02,         /*      Feature (Variable),             */
503         0x85, 0x92,         /*      Report ID (146),                */
504         0x09, 0x32,         /*      Usage (32h),                    */
505         0x95, 0x03,         /*      Report Count (3),               */
506         0xB1, 0x02,         /*      Feature (Variable),             */
507         0x85, 0x93,         /*      Report ID (147),                */
508         0x09, 0x33,         /*      Usage (33h),                    */
509         0x95, 0x0C,         /*      Report Count (12),              */
510         0xB1, 0x02,         /*      Feature (Variable),             */
511         0x85, 0xA0,         /*      Report ID (160),                */
512         0x09, 0x40,         /*      Usage (40h),                    */
513         0x95, 0x06,         /*      Report Count (6),               */
514         0xB1, 0x02,         /*      Feature (Variable),             */
515         0x85, 0xA1,         /*      Report ID (161),                */
516         0x09, 0x41,         /*      Usage (41h),                    */
517         0x95, 0x01,         /*      Report Count (1),               */
518         0xB1, 0x02,         /*      Feature (Variable),             */
519         0x85, 0xA2,         /*      Report ID (162),                */
520         0x09, 0x42,         /*      Usage (42h),                    */
521         0x95, 0x01,         /*      Report Count (1),               */
522         0xB1, 0x02,         /*      Feature (Variable),             */
523         0x85, 0xA3,         /*      Report ID (163),                */
524         0x09, 0x43,         /*      Usage (43h),                    */
525         0x95, 0x30,         /*      Report Count (48),              */
526         0xB1, 0x02,         /*      Feature (Variable),             */
527         0x85, 0xA4,         /*      Report ID (164),                */
528         0x09, 0x44,         /*      Usage (44h),                    */
529         0x95, 0x0D,         /*      Report Count (13),              */
530         0xB1, 0x02,         /*      Feature (Variable),             */
531         0x85, 0xA5,         /*      Report ID (165),                */
532         0x09, 0x45,         /*      Usage (45h),                    */
533         0x95, 0x15,         /*      Report Count (21),              */
534         0xB1, 0x02,         /*      Feature (Variable),             */
535         0x85, 0xA6,         /*      Report ID (166),                */
536         0x09, 0x46,         /*      Usage (46h),                    */
537         0x95, 0x15,         /*      Report Count (21),              */
538         0xB1, 0x02,         /*      Feature (Variable),             */
539         0x85, 0xF0,         /*      Report ID (240),                */
540         0x09, 0x47,         /*      Usage (47h),                    */
541         0x95, 0x3F,         /*      Report Count (63),              */
542         0xB1, 0x02,         /*      Feature (Variable),             */
543         0x85, 0xF1,         /*      Report ID (241),                */
544         0x09, 0x48,         /*      Usage (48h),                    */
545         0x95, 0x3F,         /*      Report Count (63),              */
546         0xB1, 0x02,         /*      Feature (Variable),             */
547         0x85, 0xF2,         /*      Report ID (242),                */
548         0x09, 0x49,         /*      Usage (49h),                    */
549         0x95, 0x0F,         /*      Report Count (15),              */
550         0xB1, 0x02,         /*      Feature (Variable),             */
551         0x85, 0xA7,         /*      Report ID (167),                */
552         0x09, 0x4A,         /*      Usage (4Ah),                    */
553         0x95, 0x01,         /*      Report Count (1),               */
554         0xB1, 0x02,         /*      Feature (Variable),             */
555         0x85, 0xA8,         /*      Report ID (168),                */
556         0x09, 0x4B,         /*      Usage (4Bh),                    */
557         0x95, 0x01,         /*      Report Count (1),               */
558         0xB1, 0x02,         /*      Feature (Variable),             */
559         0x85, 0xA9,         /*      Report ID (169),                */
560         0x09, 0x4C,         /*      Usage (4Ch),                    */
561         0x95, 0x08,         /*      Report Count (8),               */
562         0xB1, 0x02,         /*      Feature (Variable),             */
563         0x85, 0xAA,         /*      Report ID (170),                */
564         0x09, 0x4E,         /*      Usage (4Eh),                    */
565         0x95, 0x01,         /*      Report Count (1),               */
566         0xB1, 0x02,         /*      Feature (Variable),             */
567         0x85, 0xAB,         /*      Report ID (171),                */
568         0x09, 0x4F,         /*      Usage (4Fh),                    */
569         0x95, 0x39,         /*      Report Count (57),              */
570         0xB1, 0x02,         /*      Feature (Variable),             */
571         0x85, 0xAC,         /*      Report ID (172),                */
572         0x09, 0x50,         /*      Usage (50h),                    */
573         0x95, 0x39,         /*      Report Count (57),              */
574         0xB1, 0x02,         /*      Feature (Variable),             */
575         0x85, 0xAD,         /*      Report ID (173),                */
576         0x09, 0x51,         /*      Usage (51h),                    */
577         0x95, 0x0B,         /*      Report Count (11),              */
578         0xB1, 0x02,         /*      Feature (Variable),             */
579         0x85, 0xAE,         /*      Report ID (174),                */
580         0x09, 0x52,         /*      Usage (52h),                    */
581         0x95, 0x01,         /*      Report Count (1),               */
582         0xB1, 0x02,         /*      Feature (Variable),             */
583         0x85, 0xAF,         /*      Report ID (175),                */
584         0x09, 0x53,         /*      Usage (53h),                    */
585         0x95, 0x02,         /*      Report Count (2),               */
586         0xB1, 0x02,         /*      Feature (Variable),             */
587         0x85, 0xB0,         /*      Report ID (176),                */
588         0x09, 0x54,         /*      Usage (54h),                    */
589         0x95, 0x3F,         /*      Report Count (63),              */
590         0xB1, 0x02,         /*      Feature (Variable),             */
591         0xC0                /*  End Collection                      */
592 };
593
594 /*
595  * The default behavior of the Dualshock 4 is to send reports using report
596  * type 1 when running over Bluetooth. However, when feature report 2 is
597  * requested during the controller initialization it starts sending input
598  * reports in report 17.  Since report 17 is undefined in the default HID
599  * descriptor the button and axis definitions must be moved to report 17 or
600  * the HID layer won't process the received input.
601  */
602 static u8 dualshock4_bt_rdesc[] = {
603         0x05, 0x01,         /*  Usage Page (Desktop),               */
604         0x09, 0x05,         /*  Usage (Gamepad),                    */
605         0xA1, 0x01,         /*  Collection (Application),           */
606         0x85, 0x01,         /*      Report ID (1),                  */
607         0x75, 0x08,         /*      Report Size (8),                */
608         0x95, 0x0A,         /*      Report Count (9),               */
609         0x81, 0x02,         /*      Input (Variable),               */
610         0x06, 0x04, 0xFF,   /*      Usage Page (FF04h),             */
611         0x85, 0x02,         /*      Report ID (2),                  */
612         0x09, 0x24,         /*      Usage (24h),                    */
613         0x95, 0x24,         /*      Report Count (36),              */
614         0xB1, 0x02,         /*      Feature (Variable),             */
615         0x85, 0xA3,         /*      Report ID (163),                */
616         0x09, 0x25,         /*      Usage (25h),                    */
617         0x95, 0x30,         /*      Report Count (48),              */
618         0xB1, 0x02,         /*      Feature (Variable),             */
619         0x85, 0x05,         /*      Report ID (5),                  */
620         0x09, 0x26,         /*      Usage (26h),                    */
621         0x95, 0x28,         /*      Report Count (40),              */
622         0xB1, 0x02,         /*      Feature (Variable),             */
623         0x85, 0x06,         /*      Report ID (6),                  */
624         0x09, 0x27,         /*      Usage (27h),                    */
625         0x95, 0x34,         /*      Report Count (52),              */
626         0xB1, 0x02,         /*      Feature (Variable),             */
627         0x85, 0x07,         /*      Report ID (7),                  */
628         0x09, 0x28,         /*      Usage (28h),                    */
629         0x95, 0x30,         /*      Report Count (48),              */
630         0xB1, 0x02,         /*      Feature (Variable),             */
631         0x85, 0x08,         /*      Report ID (8),                  */
632         0x09, 0x29,         /*      Usage (29h),                    */
633         0x95, 0x2F,         /*      Report Count (47),              */
634         0xB1, 0x02,         /*      Feature (Variable),             */
635         0x06, 0x03, 0xFF,   /*      Usage Page (FF03h),             */
636         0x85, 0x03,         /*      Report ID (3),                  */
637         0x09, 0x21,         /*      Usage (21h),                    */
638         0x95, 0x26,         /*      Report Count (38),              */
639         0xB1, 0x02,         /*      Feature (Variable),             */
640         0x85, 0x04,         /*      Report ID (4),                  */
641         0x09, 0x22,         /*      Usage (22h),                    */
642         0x95, 0x2E,         /*      Report Count (46),              */
643         0xB1, 0x02,         /*      Feature (Variable),             */
644         0x85, 0xF0,         /*      Report ID (240),                */
645         0x09, 0x47,         /*      Usage (47h),                    */
646         0x95, 0x3F,         /*      Report Count (63),              */
647         0xB1, 0x02,         /*      Feature (Variable),             */
648         0x85, 0xF1,         /*      Report ID (241),                */
649         0x09, 0x48,         /*      Usage (48h),                    */
650         0x95, 0x3F,         /*      Report Count (63),              */
651         0xB1, 0x02,         /*      Feature (Variable),             */
652         0x85, 0xF2,         /*      Report ID (242),                */
653         0x09, 0x49,         /*      Usage (49h),                    */
654         0x95, 0x0F,         /*      Report Count (15),              */
655         0xB1, 0x02,         /*      Feature (Variable),             */
656         0x85, 0x11,         /*      Report ID (17),                 */
657         0x06, 0x00, 0xFF,   /*      Usage Page (FF00h),             */
658         0x09, 0x20,         /*      Usage (20h),                    */
659         0x95, 0x02,         /*      Report Count (2),               */
660         0x81, 0x02,         /*      Input (Variable),               */
661         0x05, 0x01,         /*      Usage Page (Desktop),           */
662         0x09, 0x30,         /*      Usage (X),                      */
663         0x09, 0x31,         /*      Usage (Y),                      */
664         0x09, 0x32,         /*      Usage (Z),                      */
665         0x09, 0x35,         /*      Usage (Rz),                     */
666         0x15, 0x00,         /*      Logical Minimum (0),            */
667         0x26, 0xFF, 0x00,   /*      Logical Maximum (255),          */
668         0x75, 0x08,         /*      Report Size (8),                */
669         0x95, 0x04,         /*      Report Count (4),               */
670         0x81, 0x02,         /*      Input (Variable),               */
671         0x09, 0x39,         /*      Usage (Hat Switch),             */
672         0x15, 0x00,         /*      Logical Minimum (0),            */
673         0x25, 0x07,         /*      Logical Maximum (7),            */
674         0x75, 0x04,         /*      Report Size (4),                */
675         0x95, 0x01,         /*      Report Count (1),               */
676         0x81, 0x42,         /*      Input (Variable, Null State),   */
677         0x05, 0x09,         /*      Usage Page (Button),            */
678         0x19, 0x01,         /*      Usage Minimum (01h),            */
679         0x29, 0x0E,         /*      Usage Maximum (0Eh),            */
680         0x15, 0x00,         /*      Logical Minimum (0),            */
681         0x25, 0x01,         /*      Logical Maximum (1),            */
682         0x75, 0x01,         /*      Report Size (1),                */
683         0x95, 0x0E,         /*      Report Count (14),              */
684         0x81, 0x02,         /*      Input (Variable),               */
685         0x75, 0x06,         /*      Report Size (6),                */
686         0x95, 0x01,         /*      Report Count (1),               */
687         0x81, 0x01,         /*      Input (Constant),               */
688         0x05, 0x01,         /*      Usage Page (Desktop),           */
689         0x09, 0x33,         /*      Usage (Rx),                     */
690         0x09, 0x34,         /*      Usage (Ry),                     */
691         0x15, 0x00,         /*      Logical Minimum (0),            */
692         0x26, 0xFF, 0x00,   /*      Logical Maximum (255),          */
693         0x75, 0x08,         /*      Report Size (8),                */
694         0x95, 0x02,         /*      Report Count (2),               */
695         0x81, 0x02,         /*      Input (Variable),               */
696         0x06, 0x00, 0xFF,   /*      Usage Page (FF00h),             */
697         0x09, 0x20,         /*      Usage (20h),                    */
698         0x95, 0x03,         /*      Report Count (3),               */
699         0x81, 0x02,         /*      Input (Variable),               */
700         0x05, 0x01,         /*      Usage Page (Desktop),           */
701         0x19, 0x40,         /*      Usage Minimum (40h),            */
702         0x29, 0x42,         /*      Usage Maximum (42h),            */
703         0x16, 0x00, 0x80,   /*      Logical Minimum (-32768),       */
704         0x26, 0x00, 0x7F,   /*      Logical Maximum (32767),        */
705         0x75, 0x10,         /*      Report Size (16),               */
706         0x95, 0x03,         /*      Report Count (3),               */
707         0x81, 0x02,         /*      Input (Variable),               */
708         0x19, 0x43,         /*      Usage Minimum (43h),            */
709         0x29, 0x45,         /*      Usage Maximum (45h),            */
710         0x16, 0x00, 0xE0,   /*      Logical Minimum (-8192),        */
711         0x26, 0xFF, 0x1F,   /*      Logical Maximum (8191),         */
712         0x95, 0x03,         /*      Report Count (3),               */
713         0x81, 0x02,         /*      Input (Variable),               */
714         0x06, 0x00, 0xFF,   /*      Usage Page (FF00h),             */
715         0x09, 0x20,         /*      Usage (20h),                    */
716         0x15, 0x00,         /*      Logical Minimum (0),            */
717         0x26, 0xFF, 0x00,   /*      Logical Maximum (255),          */
718         0x75, 0x08,         /*      Report Size (8),                */
719         0x95, 0x31,         /*      Report Count (51),              */
720         0x81, 0x02,         /*      Input (Variable),               */
721         0x09, 0x21,         /*      Usage (21h),                    */
722         0x75, 0x08,         /*      Report Size (8),                */
723         0x95, 0x4D,         /*      Report Count (77),              */
724         0x91, 0x02,         /*      Output (Variable),              */
725         0x85, 0x12,         /*      Report ID (18),                 */
726         0x09, 0x22,         /*      Usage (22h),                    */
727         0x95, 0x8D,         /*      Report Count (141),             */
728         0x81, 0x02,         /*      Input (Variable),               */
729         0x09, 0x23,         /*      Usage (23h),                    */
730         0x91, 0x02,         /*      Output (Variable),              */
731         0x85, 0x13,         /*      Report ID (19),                 */
732         0x09, 0x24,         /*      Usage (24h),                    */
733         0x95, 0xCD,         /*      Report Count (205),             */
734         0x81, 0x02,         /*      Input (Variable),               */
735         0x09, 0x25,         /*      Usage (25h),                    */
736         0x91, 0x02,         /*      Output (Variable),              */
737         0x85, 0x14,         /*      Report ID (20),                 */
738         0x09, 0x26,         /*      Usage (26h),                    */
739         0x96, 0x0D, 0x01,   /*      Report Count (269),             */
740         0x81, 0x02,         /*      Input (Variable),               */
741         0x09, 0x27,         /*      Usage (27h),                    */
742         0x91, 0x02,         /*      Output (Variable),              */
743         0x85, 0x15,         /*      Report ID (21),                 */
744         0x09, 0x28,         /*      Usage (28h),                    */
745         0x96, 0x4D, 0x01,   /*      Report Count (333),             */
746         0x81, 0x02,         /*      Input (Variable),               */
747         0x09, 0x29,         /*      Usage (29h),                    */
748         0x91, 0x02,         /*      Output (Variable),              */
749         0x85, 0x16,         /*      Report ID (22),                 */
750         0x09, 0x2A,         /*      Usage (2Ah),                    */
751         0x96, 0x8D, 0x01,   /*      Report Count (397),             */
752         0x81, 0x02,         /*      Input (Variable),               */
753         0x09, 0x2B,         /*      Usage (2Bh),                    */
754         0x91, 0x02,         /*      Output (Variable),              */
755         0x85, 0x17,         /*      Report ID (23),                 */
756         0x09, 0x2C,         /*      Usage (2Ch),                    */
757         0x96, 0xCD, 0x01,   /*      Report Count (461),             */
758         0x81, 0x02,         /*      Input (Variable),               */
759         0x09, 0x2D,         /*      Usage (2Dh),                    */
760         0x91, 0x02,         /*      Output (Variable),              */
761         0x85, 0x18,         /*      Report ID (24),                 */
762         0x09, 0x2E,         /*      Usage (2Eh),                    */
763         0x96, 0x0D, 0x02,   /*      Report Count (525),             */
764         0x81, 0x02,         /*      Input (Variable),               */
765         0x09, 0x2F,         /*      Usage (2Fh),                    */
766         0x91, 0x02,         /*      Output (Variable),              */
767         0x85, 0x19,         /*      Report ID (25),                 */
768         0x09, 0x30,         /*      Usage (30h),                    */
769         0x96, 0x22, 0x02,   /*      Report Count (546),             */
770         0x81, 0x02,         /*      Input (Variable),               */
771         0x09, 0x31,         /*      Usage (31h),                    */
772         0x91, 0x02,         /*      Output (Variable),              */
773         0x06, 0x80, 0xFF,   /*      Usage Page (FF80h),             */
774         0x85, 0x82,         /*      Report ID (130),                */
775         0x09, 0x22,         /*      Usage (22h),                    */
776         0x95, 0x3F,         /*      Report Count (63),              */
777         0xB1, 0x02,         /*      Feature (Variable),             */
778         0x85, 0x83,         /*      Report ID (131),                */
779         0x09, 0x23,         /*      Usage (23h),                    */
780         0xB1, 0x02,         /*      Feature (Variable),             */
781         0x85, 0x84,         /*      Report ID (132),                */
782         0x09, 0x24,         /*      Usage (24h),                    */
783         0xB1, 0x02,         /*      Feature (Variable),             */
784         0x85, 0x90,         /*      Report ID (144),                */
785         0x09, 0x30,         /*      Usage (30h),                    */
786         0xB1, 0x02,         /*      Feature (Variable),             */
787         0x85, 0x91,         /*      Report ID (145),                */
788         0x09, 0x31,         /*      Usage (31h),                    */
789         0xB1, 0x02,         /*      Feature (Variable),             */
790         0x85, 0x92,         /*      Report ID (146),                */
791         0x09, 0x32,         /*      Usage (32h),                    */
792         0xB1, 0x02,         /*      Feature (Variable),             */
793         0x85, 0x93,         /*      Report ID (147),                */
794         0x09, 0x33,         /*      Usage (33h),                    */
795         0xB1, 0x02,         /*      Feature (Variable),             */
796         0x85, 0xA0,         /*      Report ID (160),                */
797         0x09, 0x40,         /*      Usage (40h),                    */
798         0xB1, 0x02,         /*      Feature (Variable),             */
799         0x85, 0xA4,         /*      Report ID (164),                */
800         0x09, 0x44,         /*      Usage (44h),                    */
801         0xB1, 0x02,         /*      Feature (Variable),             */
802         0xC0                /*  End Collection                      */
803 };
804
805 static __u8 ps3remote_rdesc[] = {
806         0x05, 0x01,          /* GUsagePage Generic Desktop */
807         0x09, 0x05,          /* LUsage 0x05 [Game Pad] */
808         0xA1, 0x01,          /* MCollection Application (mouse, keyboard) */
809
810          /* Use collection 1 for joypad buttons */
811          0xA1, 0x02,         /* MCollection Logical (interrelated data) */
812
813           /* Ignore the 1st byte, maybe it is used for a controller
814            * number but it's not needed for correct operation */
815           0x75, 0x08,        /* GReportSize 0x08 [8] */
816           0x95, 0x01,        /* GReportCount 0x01 [1] */
817           0x81, 0x01,        /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
818
819           /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
820            * buttons multiple keypresses are allowed */
821           0x05, 0x09,        /* GUsagePage Button */
822           0x19, 0x01,        /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
823           0x29, 0x18,        /* LUsageMaximum 0x18 [Button 24] */
824           0x14,              /* GLogicalMinimum [0] */
825           0x25, 0x01,        /* GLogicalMaximum 0x01 [1] */
826           0x75, 0x01,        /* GReportSize 0x01 [1] */
827           0x95, 0x18,        /* GReportCount 0x18 [24] */
828           0x81, 0x02,        /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
829
830           0xC0,              /* MEndCollection */
831
832          /* Use collection 2 for remote control buttons */
833          0xA1, 0x02,         /* MCollection Logical (interrelated data) */
834
835           /* 5th byte is used for remote control buttons */
836           0x05, 0x09,        /* GUsagePage Button */
837           0x18,              /* LUsageMinimum [No button pressed] */
838           0x29, 0xFE,        /* LUsageMaximum 0xFE [Button 254] */
839           0x14,              /* GLogicalMinimum [0] */
840           0x26, 0xFE, 0x00,  /* GLogicalMaximum 0x00FE [254] */
841           0x75, 0x08,        /* GReportSize 0x08 [8] */
842           0x95, 0x01,        /* GReportCount 0x01 [1] */
843           0x80,              /* MInput  */
844
845           /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
846            * 0xff and 11th is for press indication */
847           0x75, 0x08,        /* GReportSize 0x08 [8] */
848           0x95, 0x06,        /* GReportCount 0x06 [6] */
849           0x81, 0x01,        /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
850
851           /* 12th byte is for battery strength */
852           0x05, 0x06,        /* GUsagePage Generic Device Controls */
853           0x09, 0x20,        /* LUsage 0x20 [Battery Strength] */
854           0x14,              /* GLogicalMinimum [0] */
855           0x25, 0x05,        /* GLogicalMaximum 0x05 [5] */
856           0x75, 0x08,        /* GReportSize 0x08 [8] */
857           0x95, 0x01,        /* GReportCount 0x01 [1] */
858           0x81, 0x02,        /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
859
860           0xC0,              /* MEndCollection */
861
862          0xC0                /* MEndCollection [Game Pad] */
863 };
864
865 static const unsigned int ps3remote_keymap_joypad_buttons[] = {
866         [0x01] = KEY_SELECT,
867         [0x02] = BTN_THUMBL,            /* L3 */
868         [0x03] = BTN_THUMBR,            /* R3 */
869         [0x04] = BTN_START,
870         [0x05] = KEY_UP,
871         [0x06] = KEY_RIGHT,
872         [0x07] = KEY_DOWN,
873         [0x08] = KEY_LEFT,
874         [0x09] = BTN_TL2,               /* L2 */
875         [0x0a] = BTN_TR2,               /* R2 */
876         [0x0b] = BTN_TL,                /* L1 */
877         [0x0c] = BTN_TR,                /* R1 */
878         [0x0d] = KEY_OPTION,            /* options/triangle */
879         [0x0e] = KEY_BACK,              /* back/circle */
880         [0x0f] = BTN_0,                 /* cross */
881         [0x10] = KEY_SCREEN,            /* view/square */
882         [0x11] = KEY_HOMEPAGE,          /* PS button */
883         [0x14] = KEY_ENTER,
884 };
885 static const unsigned int ps3remote_keymap_remote_buttons[] = {
886         [0x00] = KEY_1,
887         [0x01] = KEY_2,
888         [0x02] = KEY_3,
889         [0x03] = KEY_4,
890         [0x04] = KEY_5,
891         [0x05] = KEY_6,
892         [0x06] = KEY_7,
893         [0x07] = KEY_8,
894         [0x08] = KEY_9,
895         [0x09] = KEY_0,
896         [0x0e] = KEY_ESC,               /* return */
897         [0x0f] = KEY_CLEAR,
898         [0x16] = KEY_EJECTCD,
899         [0x1a] = KEY_MENU,              /* top menu */
900         [0x28] = KEY_TIME,
901         [0x30] = KEY_PREVIOUS,
902         [0x31] = KEY_NEXT,
903         [0x32] = KEY_PLAY,
904         [0x33] = KEY_REWIND,            /* scan back */
905         [0x34] = KEY_FORWARD,           /* scan forward */
906         [0x38] = KEY_STOP,
907         [0x39] = KEY_PAUSE,
908         [0x40] = KEY_CONTEXT_MENU,      /* pop up/menu */
909         [0x60] = KEY_FRAMEBACK,         /* slow/step back */
910         [0x61] = KEY_FRAMEFORWARD,      /* slow/step forward */
911         [0x63] = KEY_SUBTITLE,
912         [0x64] = KEY_AUDIO,
913         [0x65] = KEY_ANGLE,
914         [0x70] = KEY_INFO,              /* display */
915         [0x80] = KEY_BLUE,
916         [0x81] = KEY_RED,
917         [0x82] = KEY_GREEN,
918         [0x83] = KEY_YELLOW,
919 };
920
921 static const unsigned int buzz_keymap[] = {
922         /*
923          * The controller has 4 remote buzzers, each with one LED and 5
924          * buttons.
925          * 
926          * We use the mapping chosen by the controller, which is:
927          *
928          * Key          Offset
929          * -------------------
930          * Buzz              1
931          * Blue              5
932          * Orange            4
933          * Green             3
934          * Yellow            2
935          *
936          * So, for example, the orange button on the third buzzer is mapped to
937          * BTN_TRIGGER_HAPPY14
938          */
939         [ 1] = BTN_TRIGGER_HAPPY1,
940         [ 2] = BTN_TRIGGER_HAPPY2,
941         [ 3] = BTN_TRIGGER_HAPPY3,
942         [ 4] = BTN_TRIGGER_HAPPY4,
943         [ 5] = BTN_TRIGGER_HAPPY5,
944         [ 6] = BTN_TRIGGER_HAPPY6,
945         [ 7] = BTN_TRIGGER_HAPPY7,
946         [ 8] = BTN_TRIGGER_HAPPY8,
947         [ 9] = BTN_TRIGGER_HAPPY9,
948         [10] = BTN_TRIGGER_HAPPY10,
949         [11] = BTN_TRIGGER_HAPPY11,
950         [12] = BTN_TRIGGER_HAPPY12,
951         [13] = BTN_TRIGGER_HAPPY13,
952         [14] = BTN_TRIGGER_HAPPY14,
953         [15] = BTN_TRIGGER_HAPPY15,
954         [16] = BTN_TRIGGER_HAPPY16,
955         [17] = BTN_TRIGGER_HAPPY17,
956         [18] = BTN_TRIGGER_HAPPY18,
957         [19] = BTN_TRIGGER_HAPPY19,
958         [20] = BTN_TRIGGER_HAPPY20,
959 };
960
961 static enum power_supply_property sony_battery_props[] = {
962         POWER_SUPPLY_PROP_PRESENT,
963         POWER_SUPPLY_PROP_CAPACITY,
964         POWER_SUPPLY_PROP_SCOPE,
965         POWER_SUPPLY_PROP_STATUS,
966 };
967
968 struct sixaxis_led {
969         __u8 time_enabled; /* the total time the led is active (0xff means forever) */
970         __u8 duty_length;  /* how long a cycle is in deciseconds (0 means "really fast") */
971         __u8 enabled;
972         __u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
973         __u8 duty_on;  /* % of duty_length the led is on (0xff mean 100%) */
974 } __packed;
975
976 struct sixaxis_rumble {
977         __u8 padding;
978         __u8 right_duration; /* Right motor duration (0xff means forever) */
979         __u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
980         __u8 left_duration;    /* Left motor duration (0xff means forever) */
981         __u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
982 } __packed;
983
984 struct sixaxis_output_report {
985         __u8 report_id;
986         struct sixaxis_rumble rumble;
987         __u8 padding[4];
988         __u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
989         struct sixaxis_led led[4];    /* LEDx at (4 - x) */
990         struct sixaxis_led _reserved; /* LED5, not actually soldered */
991 } __packed;
992
993 union sixaxis_output_report_01 {
994         struct sixaxis_output_report data;
995         __u8 buf[36];
996 };
997
998 struct motion_output_report_02 {
999         u8 type, zero;
1000         u8 r, g, b;
1001         u8 zero2;
1002         u8 rumble;
1003 };
1004
1005 #define DS4_REPORT_0x02_SIZE 37
1006 #define DS4_REPORT_0x05_SIZE 32
1007 #define DS4_REPORT_0x11_SIZE 78
1008 #define DS4_REPORT_0x81_SIZE 7
1009 #define SIXAXIS_REPORT_0xF2_SIZE 17
1010 #define SIXAXIS_REPORT_0xF5_SIZE 8
1011 #define MOTION_REPORT_0x02_SIZE 49
1012
1013 static DEFINE_SPINLOCK(sony_dev_list_lock);
1014 static LIST_HEAD(sony_device_list);
1015 static DEFINE_IDA(sony_device_id_allocator);
1016
1017 struct sony_sc {
1018         spinlock_t lock;
1019         struct list_head list_node;
1020         struct hid_device *hdev;
1021         struct led_classdev *leds[MAX_LEDS];
1022         unsigned long quirks;
1023         struct work_struct state_worker;
1024         struct power_supply *battery;
1025         struct power_supply_desc battery_desc;
1026         int device_id;
1027         __u8 *output_report_dmabuf;
1028
1029 #ifdef CONFIG_SONY_FF
1030         __u8 left;
1031         __u8 right;
1032 #endif
1033
1034         __u8 mac_address[6];
1035         __u8 worker_initialized;
1036         __u8 cable_state;
1037         __u8 battery_charging;
1038         __u8 battery_capacity;
1039         __u8 led_state[MAX_LEDS];
1040         __u8 led_delay_on[MAX_LEDS];
1041         __u8 led_delay_off[MAX_LEDS];
1042         __u8 led_count;
1043 };
1044
1045 static __u8 *sixaxis_fixup(struct hid_device *hdev, __u8 *rdesc,
1046                              unsigned int *rsize)
1047 {
1048         *rsize = sizeof(sixaxis_rdesc);
1049         return sixaxis_rdesc;
1050 }
1051
1052 static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
1053                              unsigned int *rsize)
1054 {
1055         *rsize = sizeof(motion_rdesc);
1056         return motion_rdesc;
1057 }
1058
1059 static u8 *navigation_fixup(struct hid_device *hdev, u8 *rdesc,
1060                              unsigned int *rsize)
1061 {
1062         *rsize = sizeof(navigation_rdesc);
1063         return navigation_rdesc;
1064 }
1065
1066 static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
1067                              unsigned int *rsize)
1068 {
1069         *rsize = sizeof(ps3remote_rdesc);
1070         return ps3remote_rdesc;
1071 }
1072
1073 static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
1074                              struct hid_field *field, struct hid_usage *usage,
1075                              unsigned long **bit, int *max)
1076 {
1077         unsigned int key = usage->hid & HID_USAGE;
1078
1079         if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1080                 return -1;
1081
1082         switch (usage->collection_index) {
1083         case 1:
1084                 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
1085                         return -1;
1086
1087                 key = ps3remote_keymap_joypad_buttons[key];
1088                 if (!key)
1089                         return -1;
1090                 break;
1091         case 2:
1092                 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
1093                         return -1;
1094
1095                 key = ps3remote_keymap_remote_buttons[key];
1096                 if (!key)
1097                         return -1;
1098                 break;
1099         default:
1100                 return -1;
1101         }
1102
1103         hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1104         return 1;
1105 }
1106
1107 static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
1108                 unsigned int *rsize)
1109 {
1110         struct sony_sc *sc = hid_get_drvdata(hdev);
1111
1112         /*
1113          * Some Sony RF receivers wrongly declare the mouse pointer as a
1114          * a constant non-data variable.
1115          */
1116         if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
1117             /* usage page: generic desktop controls */
1118             /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
1119             /* usage: mouse */
1120             rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
1121             /* input (usage page for x,y axes): constant, variable, relative */
1122             rdesc[54] == 0x81 && rdesc[55] == 0x07) {
1123                 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
1124                 /* input: data, variable, relative */
1125                 rdesc[55] = 0x06;
1126         }
1127
1128         /*
1129          * The default Dualshock 4 USB descriptor doesn't assign
1130          * the gyroscope values to corresponding axes so we need a
1131          * modified one.
1132          */
1133         if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
1134                 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
1135                 rdesc = dualshock4_usb_rdesc;
1136                 *rsize = sizeof(dualshock4_usb_rdesc);
1137         } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
1138                 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
1139                 rdesc = dualshock4_bt_rdesc;
1140                 *rsize = sizeof(dualshock4_bt_rdesc);
1141         }
1142
1143         if (sc->quirks & SIXAXIS_CONTROLLER)
1144                 return sixaxis_fixup(hdev, rdesc, rsize);
1145
1146         if (sc->quirks & MOTION_CONTROLLER)
1147                 return motion_fixup(hdev, rdesc, rsize);
1148
1149         if (sc->quirks & NAVIGATION_CONTROLLER)
1150                 return navigation_fixup(hdev, rdesc, rsize);
1151
1152         if (sc->quirks & PS3REMOTE)
1153                 return ps3remote_fixup(hdev, rdesc, rsize);
1154
1155         return rdesc;
1156 }
1157
1158 static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1159 {
1160         static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
1161         unsigned long flags;
1162         int offset;
1163         __u8 cable_state, battery_capacity, battery_charging;
1164
1165         /*
1166          * The sixaxis is charging if the battery value is 0xee
1167          * and it is fully charged if the value is 0xef.
1168          * It does not report the actual level while charging so it
1169          * is set to 100% while charging is in progress.
1170          */
1171         offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
1172
1173         if (rd[offset] >= 0xee) {
1174                 battery_capacity = 100;
1175                 battery_charging = !(rd[offset] & 0x01);
1176                 cable_state = 1;
1177         } else {
1178                 __u8 index = rd[offset] <= 5 ? rd[offset] : 5;
1179                 battery_capacity = sixaxis_battery_capacity[index];
1180                 battery_charging = 0;
1181                 cable_state = 0;
1182         }
1183
1184         spin_lock_irqsave(&sc->lock, flags);
1185         sc->cable_state = cable_state;
1186         sc->battery_capacity = battery_capacity;
1187         sc->battery_charging = battery_charging;
1188         spin_unlock_irqrestore(&sc->lock, flags);
1189 }
1190
1191 static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1192 {
1193         struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1194                                                 struct hid_input, list);
1195         struct input_dev *input_dev = hidinput->input;
1196         unsigned long flags;
1197         int n, offset;
1198         __u8 cable_state, battery_capacity, battery_charging;
1199
1200         /*
1201          * Battery and touchpad data starts at byte 30 in the USB report and
1202          * 32 in Bluetooth report.
1203          */
1204         offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
1205
1206         /*
1207          * The lower 4 bits of byte 30 contain the battery level
1208          * and the 5th bit contains the USB cable state.
1209          */
1210         cable_state = (rd[offset] >> 4) & 0x01;
1211         battery_capacity = rd[offset] & 0x0F;
1212
1213         /*
1214          * When a USB power source is connected the battery level ranges from
1215          * 0 to 10, and when running on battery power it ranges from 0 to 9.
1216          * A battery level above 10 when plugged in means charge completed.
1217          */
1218         if (!cable_state || battery_capacity > 10)
1219                 battery_charging = 0;
1220         else
1221                 battery_charging = 1;
1222
1223         if (!cable_state)
1224                 battery_capacity++;
1225         if (battery_capacity > 10)
1226                 battery_capacity = 10;
1227
1228         battery_capacity *= 10;
1229
1230         spin_lock_irqsave(&sc->lock, flags);
1231         sc->cable_state = cable_state;
1232         sc->battery_capacity = battery_capacity;
1233         sc->battery_charging = battery_charging;
1234         spin_unlock_irqrestore(&sc->lock, flags);
1235
1236         offset += 5;
1237
1238         /*
1239          * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
1240          * and 37 on Bluetooth.
1241          * The first 7 bits of the first byte is a counter and bit 8 is a touch
1242          * indicator that is 0 when pressed and 1 when not pressed.
1243          * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1244          * The data for the second touch is in the same format and immediatly
1245          * follows the data for the first.
1246          */
1247         for (n = 0; n < 2; n++) {
1248                 __u16 x, y;
1249
1250                 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1251                 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1252
1253                 input_mt_slot(input_dev, n);
1254                 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
1255                                         !(rd[offset] >> 7));
1256                 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1257                 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1258
1259                 offset += 4;
1260         }
1261 }
1262
1263 static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
1264                 __u8 *rd, int size)
1265 {
1266         struct sony_sc *sc = hid_get_drvdata(hdev);
1267
1268         /*
1269          * Sixaxis HID report has acclerometers/gyro with MSByte first, this
1270          * has to be BYTE_SWAPPED before passing up to joystick interface
1271          */
1272         if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
1273                 swap(rd[41], rd[42]);
1274                 swap(rd[43], rd[44]);
1275                 swap(rd[45], rd[46]);
1276                 swap(rd[47], rd[48]);
1277
1278                 sixaxis_parse_report(sc, rd, size);
1279         } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
1280                 sixaxis_parse_report(sc, rd, size);
1281         } else if ((sc->quirks & NAVIGATION_CONTROLLER) && rd[0] == 0x01 &&
1282                         size == 49) {
1283                 sixaxis_parse_report(sc, rd, size);
1284         } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1285                         size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1286                         && rd[0] == 0x11 && size == 78)) {
1287                 dualshock4_parse_report(sc, rd, size);
1288         }
1289
1290         return 0;
1291 }
1292
1293 static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1294                         struct hid_field *field, struct hid_usage *usage,
1295                         unsigned long **bit, int *max)
1296 {
1297         struct sony_sc *sc = hid_get_drvdata(hdev);
1298
1299         if (sc->quirks & BUZZ_CONTROLLER) {
1300                 unsigned int key = usage->hid & HID_USAGE;
1301
1302                 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1303                         return -1;
1304
1305                 switch (usage->collection_index) {
1306                 case 1:
1307                         if (key >= ARRAY_SIZE(buzz_keymap))
1308                                 return -1;
1309
1310                         key = buzz_keymap[key];
1311                         if (!key)
1312                                 return -1;
1313                         break;
1314                 default:
1315                         return -1;
1316                 }
1317
1318                 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1319                 return 1;
1320         }
1321
1322         if (sc->quirks & PS3REMOTE)
1323                 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1324
1325         /* Let hid-core decide for the others */
1326         return 0;
1327 }
1328
1329 static int sony_register_touchpad(struct hid_input *hi, int touch_count,
1330                                         int w, int h)
1331 {
1332         struct input_dev *input_dev = hi->input;
1333         int ret;
1334
1335         ret = input_mt_init_slots(input_dev, touch_count, 0);
1336         if (ret < 0)
1337                 return ret;
1338
1339         input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1340         input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1341
1342         return 0;
1343 }
1344
1345 static void sony_input_configured(struct hid_device *hdev,
1346                                         struct hid_input *hidinput)
1347 {
1348         struct sony_sc *sc = hid_get_drvdata(hdev);
1349
1350         /*
1351          * The Dualshock 4 touchpad supports 2 touches and has a
1352          * resolution of 1920x942 (44.86 dots/mm).
1353          */
1354         if (sc->quirks & DUALSHOCK4_CONTROLLER) {
1355                 if (sony_register_touchpad(hidinput, 2, 1920, 942) != 0)
1356                         hid_err(sc->hdev,
1357                                 "Unable to initialize multi-touch slots\n");
1358         }
1359 }
1360
1361 /*
1362  * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1363  * to "operational".  Without this, the ps3 controller will not report any
1364  * events.
1365  */
1366 static int sixaxis_set_operational_usb(struct hid_device *hdev)
1367 {
1368         const int buf_size =
1369                 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
1370         __u8 *buf;
1371         int ret;
1372
1373         buf = kmalloc(buf_size, GFP_KERNEL);
1374         if (!buf)
1375                 return -ENOMEM;
1376
1377         ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1378                                  HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1379         if (ret < 0) {
1380                 hid_err(hdev, "can't set operational mode: step 1\n");
1381                 goto out;
1382         }
1383
1384         /*
1385          * Some compatible controllers like the Speedlink Strike FX and
1386          * Gasia need another query plus an USB interrupt to get operational.
1387          */
1388         ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1389                                  HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1390         if (ret < 0) {
1391                 hid_err(hdev, "can't set operational mode: step 2\n");
1392                 goto out;
1393         }
1394
1395         ret = hid_hw_output_report(hdev, buf, 1);
1396         if (ret < 0)
1397                 hid_err(hdev, "can't set operational mode: step 3\n");
1398
1399 out:
1400         kfree(buf);
1401
1402         return ret;
1403 }
1404
1405 static int sixaxis_set_operational_bt(struct hid_device *hdev)
1406 {
1407         static const __u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1408         __u8 *buf;
1409         int ret;
1410
1411         buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1412         if (!buf)
1413                 return -ENOMEM;
1414
1415         ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
1416                                   HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
1417
1418         kfree(buf);
1419
1420         return ret;
1421 }
1422
1423 /*
1424  * Requesting feature report 0x02 in Bluetooth mode changes the state of the
1425  * controller so that it sends full input reports of type 0x11.
1426  */
1427 static int dualshock4_set_operational_bt(struct hid_device *hdev)
1428 {
1429         __u8 *buf;
1430         int ret;
1431
1432         buf = kmalloc(DS4_REPORT_0x02_SIZE, GFP_KERNEL);
1433         if (!buf)
1434                 return -ENOMEM;
1435
1436         ret = hid_hw_raw_request(hdev, 0x02, buf, DS4_REPORT_0x02_SIZE,
1437                                 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1438
1439         kfree(buf);
1440
1441         return ret;
1442 }
1443
1444 static void sixaxis_set_leds_from_id(struct sony_sc *sc)
1445 {
1446         static const __u8 sixaxis_leds[10][4] = {
1447                                 { 0x01, 0x00, 0x00, 0x00 },
1448                                 { 0x00, 0x01, 0x00, 0x00 },
1449                                 { 0x00, 0x00, 0x01, 0x00 },
1450                                 { 0x00, 0x00, 0x00, 0x01 },
1451                                 { 0x01, 0x00, 0x00, 0x01 },
1452                                 { 0x00, 0x01, 0x00, 0x01 },
1453                                 { 0x00, 0x00, 0x01, 0x01 },
1454                                 { 0x01, 0x00, 0x01, 0x01 },
1455                                 { 0x00, 0x01, 0x01, 0x01 },
1456                                 { 0x01, 0x01, 0x01, 0x01 }
1457         };
1458
1459         int id = sc->device_id;
1460
1461         BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
1462
1463         if (id < 0)
1464                 return;
1465
1466         id %= 10;
1467         memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
1468 }
1469
1470 static void dualshock4_set_leds_from_id(struct sony_sc *sc)
1471 {
1472         /* The first 4 color/index entries match what the PS4 assigns */
1473         static const __u8 color_code[7][3] = {
1474                         /* Blue   */    { 0x00, 0x00, 0x01 },
1475                         /* Red    */    { 0x01, 0x00, 0x00 },
1476                         /* Green  */    { 0x00, 0x01, 0x00 },
1477                         /* Pink   */    { 0x02, 0x00, 0x01 },
1478                         /* Orange */    { 0x02, 0x01, 0x00 },
1479                         /* Teal   */    { 0x00, 0x01, 0x01 },
1480                         /* White  */    { 0x01, 0x01, 0x01 }
1481         };
1482
1483         int id = sc->device_id;
1484
1485         BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
1486
1487         if (id < 0)
1488                 return;
1489
1490         id %= 7;
1491         memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
1492 }
1493
1494 static void buzz_set_leds(struct sony_sc *sc)
1495 {
1496         struct hid_device *hdev = sc->hdev;
1497         struct list_head *report_list =
1498                 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1499         struct hid_report *report = list_entry(report_list->next,
1500                 struct hid_report, list);
1501         __s32 *value = report->field[0]->value;
1502
1503         BUILD_BUG_ON(MAX_LEDS < 4);
1504
1505         value[0] = 0x00;
1506         value[1] = sc->led_state[0] ? 0xff : 0x00;
1507         value[2] = sc->led_state[1] ? 0xff : 0x00;
1508         value[3] = sc->led_state[2] ? 0xff : 0x00;
1509         value[4] = sc->led_state[3] ? 0xff : 0x00;
1510         value[5] = 0x00;
1511         value[6] = 0x00;
1512         hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1513 }
1514
1515 static void sony_set_leds(struct sony_sc *sc)
1516 {
1517         if (!(sc->quirks & BUZZ_CONTROLLER))
1518                 schedule_work(&sc->state_worker);
1519         else
1520                 buzz_set_leds(sc);
1521 }
1522
1523 static void sony_led_set_brightness(struct led_classdev *led,
1524                                     enum led_brightness value)
1525 {
1526         struct device *dev = led->dev->parent;
1527         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1528         struct sony_sc *drv_data;
1529
1530         int n;
1531         int force_update;
1532
1533         drv_data = hid_get_drvdata(hdev);
1534         if (!drv_data) {
1535                 hid_err(hdev, "No device data\n");
1536                 return;
1537         }
1538
1539         /*
1540          * The Sixaxis on USB will override any LED settings sent to it
1541          * and keep flashing all of the LEDs until the PS button is pressed.
1542          * Updates, even if redundant, must be always be sent to the
1543          * controller to avoid having to toggle the state of an LED just to
1544          * stop the flashing later on.
1545          */
1546         force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1547
1548         for (n = 0; n < drv_data->led_count; n++) {
1549                 if (led == drv_data->leds[n] && (force_update ||
1550                         (value != drv_data->led_state[n] ||
1551                         drv_data->led_delay_on[n] ||
1552                         drv_data->led_delay_off[n]))) {
1553
1554                         drv_data->led_state[n] = value;
1555
1556                         /* Setting the brightness stops the blinking */
1557                         drv_data->led_delay_on[n] = 0;
1558                         drv_data->led_delay_off[n] = 0;
1559
1560                         sony_set_leds(drv_data);
1561                         break;
1562                 }
1563         }
1564 }
1565
1566 static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
1567 {
1568         struct device *dev = led->dev->parent;
1569         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1570         struct sony_sc *drv_data;
1571
1572         int n;
1573
1574         drv_data = hid_get_drvdata(hdev);
1575         if (!drv_data) {
1576                 hid_err(hdev, "No device data\n");
1577                 return LED_OFF;
1578         }
1579
1580         for (n = 0; n < drv_data->led_count; n++) {
1581                 if (led == drv_data->leds[n])
1582                         return drv_data->led_state[n];
1583         }
1584
1585         return LED_OFF;
1586 }
1587
1588 static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1589                                 unsigned long *delay_off)
1590 {
1591         struct device *dev = led->dev->parent;
1592         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1593         struct sony_sc *drv_data = hid_get_drvdata(hdev);
1594         int n;
1595         __u8 new_on, new_off;
1596
1597         if (!drv_data) {
1598                 hid_err(hdev, "No device data\n");
1599                 return -EINVAL;
1600         }
1601
1602         /* Max delay is 255 deciseconds or 2550 milliseconds */
1603         if (*delay_on > 2550)
1604                 *delay_on = 2550;
1605         if (*delay_off > 2550)
1606                 *delay_off = 2550;
1607
1608         /* Blink at 1 Hz if both values are zero */
1609         if (!*delay_on && !*delay_off)
1610                 *delay_on = *delay_off = 500;
1611
1612         new_on = *delay_on / 10;
1613         new_off = *delay_off / 10;
1614
1615         for (n = 0; n < drv_data->led_count; n++) {
1616                 if (led == drv_data->leds[n])
1617                         break;
1618         }
1619
1620         /* This LED is not registered on this device */
1621         if (n >= drv_data->led_count)
1622                 return -EINVAL;
1623
1624         /* Don't schedule work if the values didn't change */
1625         if (new_on != drv_data->led_delay_on[n] ||
1626                 new_off != drv_data->led_delay_off[n]) {
1627                 drv_data->led_delay_on[n] = new_on;
1628                 drv_data->led_delay_off[n] = new_off;
1629                 schedule_work(&drv_data->state_worker);
1630         }
1631
1632         return 0;
1633 }
1634
1635 static void sony_leds_remove(struct sony_sc *sc)
1636 {
1637         struct led_classdev *led;
1638         int n;
1639
1640         BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
1641
1642         for (n = 0; n < sc->led_count; n++) {
1643                 led = sc->leds[n];
1644                 sc->leds[n] = NULL;
1645                 if (!led)
1646                         continue;
1647                 led_classdev_unregister(led);
1648                 kfree(led);
1649         }
1650
1651         sc->led_count = 0;
1652 }
1653
1654 static int sony_leds_init(struct sony_sc *sc)
1655 {
1656         struct hid_device *hdev = sc->hdev;
1657         int n, ret = 0;
1658         int use_ds4_names;
1659         struct led_classdev *led;
1660         size_t name_sz;
1661         char *name;
1662         size_t name_len;
1663         const char *name_fmt;
1664         static const char * const ds4_name_str[] = { "red", "green", "blue",
1665                                                   "global" };
1666         __u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
1667         __u8 use_hw_blink[MAX_LEDS] = { 0 };
1668
1669         BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
1670
1671         if (sc->quirks & BUZZ_CONTROLLER) {
1672                 sc->led_count = 4;
1673                 use_ds4_names = 0;
1674                 name_len = strlen("::buzz#");
1675                 name_fmt = "%s::buzz%d";
1676                 /* Validate expected report characteristics. */
1677                 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1678                         return -ENODEV;
1679         } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
1680                 dualshock4_set_leds_from_id(sc);
1681                 sc->led_state[3] = 1;
1682                 sc->led_count = 4;
1683                 memset(max_brightness, 255, 3);
1684                 use_hw_blink[3] = 1;
1685                 use_ds4_names = 1;
1686                 name_len = 0;
1687                 name_fmt = "%s:%s";
1688         } else if (sc->quirks & MOTION_CONTROLLER) {
1689                 sc->led_count = 3;
1690                 memset(max_brightness, 255, 3);
1691                 use_ds4_names = 1;
1692                 name_len = 0;
1693                 name_fmt = "%s:%s";
1694         } else if (sc->quirks & NAVIGATION_CONTROLLER) {
1695                 static const __u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00};
1696
1697                 memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds));
1698                 sc->led_count = 1;
1699                 memset(use_hw_blink, 1, 4);
1700                 use_ds4_names = 0;
1701                 name_len = strlen("::sony#");
1702                 name_fmt = "%s::sony%d";
1703         } else {
1704                 sixaxis_set_leds_from_id(sc);
1705                 sc->led_count = 4;
1706                 memset(use_hw_blink, 1, 4);
1707                 use_ds4_names = 0;
1708                 name_len = strlen("::sony#");
1709                 name_fmt = "%s::sony%d";
1710         }
1711
1712         /*
1713          * Clear LEDs as we have no way of reading their initial state. This is
1714          * only relevant if the driver is loaded after somebody actively set the
1715          * LEDs to on
1716          */
1717         sony_set_leds(sc);
1718
1719         name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
1720
1721         for (n = 0; n < sc->led_count; n++) {
1722
1723                 if (use_ds4_names)
1724                         name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
1725
1726                 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1727                 if (!led) {
1728                         hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
1729                         ret = -ENOMEM;
1730                         goto error_leds;
1731                 }
1732
1733                 name = (void *)(&led[1]);
1734                 if (use_ds4_names)
1735                         snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1736                         ds4_name_str[n]);
1737                 else
1738                         snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
1739                 led->name = name;
1740                 led->brightness = sc->led_state[n];
1741                 led->max_brightness = max_brightness[n];
1742                 led->brightness_get = sony_led_get_brightness;
1743                 led->brightness_set = sony_led_set_brightness;
1744
1745                 if (use_hw_blink[n])
1746                         led->blink_set = sony_led_blink_set;
1747
1748                 sc->leds[n] = led;
1749
1750                 ret = led_classdev_register(&hdev->dev, led);
1751                 if (ret) {
1752                         hid_err(hdev, "Failed to register LED %d\n", n);
1753                         sc->leds[n] = NULL;
1754                         kfree(led);
1755                         goto error_leds;
1756                 }
1757         }
1758
1759         return ret;
1760
1761 error_leds:
1762         sony_leds_remove(sc);
1763
1764         return ret;
1765 }
1766
1767 static void sixaxis_state_worker(struct work_struct *work)
1768 {
1769         static const union sixaxis_output_report_01 default_report = {
1770                 .buf = {
1771                         0x01,
1772                         0x00, 0xff, 0x00, 0xff, 0x00,
1773                         0x00, 0x00, 0x00, 0x00, 0x00,
1774                         0xff, 0x27, 0x10, 0x00, 0x32,
1775                         0xff, 0x27, 0x10, 0x00, 0x32,
1776                         0xff, 0x27, 0x10, 0x00, 0x32,
1777                         0xff, 0x27, 0x10, 0x00, 0x32,
1778                         0x00, 0x00, 0x00, 0x00, 0x00
1779                 }
1780         };
1781         struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1782         struct sixaxis_output_report *report =
1783                 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1784         int n;
1785
1786         /* Initialize the report with default values */
1787         memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
1788
1789 #ifdef CONFIG_SONY_FF
1790         report->rumble.right_motor_on = sc->right ? 1 : 0;
1791         report->rumble.left_motor_force = sc->left;
1792 #endif
1793
1794         report->leds_bitmap |= sc->led_state[0] << 1;
1795         report->leds_bitmap |= sc->led_state[1] << 2;
1796         report->leds_bitmap |= sc->led_state[2] << 3;
1797         report->leds_bitmap |= sc->led_state[3] << 4;
1798
1799         /* Set flag for all leds off, required for 3rd party INTEC controller */
1800         if ((report->leds_bitmap & 0x1E) == 0)
1801                 report->leds_bitmap |= 0x20;
1802
1803         /*
1804          * The LEDs in the report are indexed in reverse order to their
1805          * corresponding light on the controller.
1806          * Index 0 = LED 4, index 1 = LED 3, etc...
1807          *
1808          * In the case of both delay values being zero (blinking disabled) the
1809          * default report values should be used or the controller LED will be
1810          * always off.
1811          */
1812         for (n = 0; n < 4; n++) {
1813                 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
1814                         report->led[3 - n].duty_off = sc->led_delay_off[n];
1815                         report->led[3 - n].duty_on = sc->led_delay_on[n];
1816                 }
1817         }
1818
1819         hid_hw_raw_request(sc->hdev, report->report_id, (__u8 *)report,
1820                         sizeof(struct sixaxis_output_report),
1821                         HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
1822 }
1823
1824 static void dualshock4_state_worker(struct work_struct *work)
1825 {
1826         struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1827         struct hid_device *hdev = sc->hdev;
1828         __u8 *buf = sc->output_report_dmabuf;
1829         int offset;
1830
1831         if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1832                 memset(buf, 0, DS4_REPORT_0x05_SIZE);
1833                 buf[0] = 0x05;
1834                 buf[1] = 0xFF;
1835                 offset = 4;
1836         } else {
1837                 memset(buf, 0, DS4_REPORT_0x11_SIZE);
1838                 buf[0] = 0x11;
1839                 buf[1] = 0xB0;
1840                 buf[3] = 0x0F;
1841                 offset = 6;
1842         }
1843
1844 #ifdef CONFIG_SONY_FF
1845         buf[offset++] = sc->right;
1846         buf[offset++] = sc->left;
1847 #else
1848         offset += 2;
1849 #endif
1850
1851         /* LED 3 is the global control */
1852         if (sc->led_state[3]) {
1853                 buf[offset++] = sc->led_state[0];
1854                 buf[offset++] = sc->led_state[1];
1855                 buf[offset++] = sc->led_state[2];
1856         } else {
1857                 offset += 3;
1858         }
1859
1860         /* If both delay values are zero the DualShock 4 disables blinking. */
1861         buf[offset++] = sc->led_delay_on[3];
1862         buf[offset++] = sc->led_delay_off[3];
1863
1864         if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1865                 hid_hw_output_report(hdev, buf, DS4_REPORT_0x05_SIZE);
1866         else
1867                 hid_hw_raw_request(hdev, 0x11, buf, DS4_REPORT_0x11_SIZE,
1868                                 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
1869 }
1870
1871 static void motion_state_worker(struct work_struct *work)
1872 {
1873         struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1874         struct hid_device *hdev = sc->hdev;
1875         struct motion_output_report_02 *report =
1876                 (struct motion_output_report_02 *)sc->output_report_dmabuf;
1877
1878         memset(report, 0, MOTION_REPORT_0x02_SIZE);
1879
1880         report->type = 0x02; /* set leds */
1881         report->r = sc->led_state[0];
1882         report->g = sc->led_state[1];
1883         report->b = sc->led_state[2];
1884
1885 #ifdef CONFIG_SONY_FF
1886         report->rumble = max(sc->right, sc->left);
1887 #endif
1888
1889         hid_hw_output_report(hdev, (__u8 *)report, MOTION_REPORT_0x02_SIZE);
1890 }
1891
1892 static int sony_allocate_output_report(struct sony_sc *sc)
1893 {
1894         if ((sc->quirks & SIXAXIS_CONTROLLER) ||
1895                         (sc->quirks & NAVIGATION_CONTROLLER))
1896                 sc->output_report_dmabuf =
1897                         kmalloc(sizeof(union sixaxis_output_report_01),
1898                                 GFP_KERNEL);
1899         else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1900                 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x11_SIZE,
1901                                                 GFP_KERNEL);
1902         else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1903                 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x05_SIZE,
1904                                                 GFP_KERNEL);
1905         else if (sc->quirks & MOTION_CONTROLLER)
1906                 sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE,
1907                                                 GFP_KERNEL);
1908         else
1909                 return 0;
1910
1911         if (!sc->output_report_dmabuf)
1912                 return -ENOMEM;
1913
1914         return 0;
1915 }
1916
1917 #ifdef CONFIG_SONY_FF
1918 static int sony_play_effect(struct input_dev *dev, void *data,
1919                             struct ff_effect *effect)
1920 {
1921         struct hid_device *hid = input_get_drvdata(dev);
1922         struct sony_sc *sc = hid_get_drvdata(hid);
1923
1924         if (effect->type != FF_RUMBLE)
1925                 return 0;
1926
1927         sc->left = effect->u.rumble.strong_magnitude / 256;
1928         sc->right = effect->u.rumble.weak_magnitude / 256;
1929
1930         schedule_work(&sc->state_worker);
1931         return 0;
1932 }
1933
1934 static int sony_init_ff(struct sony_sc *sc)
1935 {
1936         struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1937                                                 struct hid_input, list);
1938         struct input_dev *input_dev = hidinput->input;
1939
1940         input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1941         return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1942 }
1943
1944 #else
1945 static int sony_init_ff(struct sony_sc *sc)
1946 {
1947         return 0;
1948 }
1949
1950 #endif
1951
1952 static int sony_battery_get_property(struct power_supply *psy,
1953                                      enum power_supply_property psp,
1954                                      union power_supply_propval *val)
1955 {
1956         struct sony_sc *sc = power_supply_get_drvdata(psy);
1957         unsigned long flags;
1958         int ret = 0;
1959         u8 battery_charging, battery_capacity, cable_state;
1960
1961         spin_lock_irqsave(&sc->lock, flags);
1962         battery_charging = sc->battery_charging;
1963         battery_capacity = sc->battery_capacity;
1964         cable_state = sc->cable_state;
1965         spin_unlock_irqrestore(&sc->lock, flags);
1966
1967         switch (psp) {
1968         case POWER_SUPPLY_PROP_PRESENT:
1969                 val->intval = 1;
1970                 break;
1971         case POWER_SUPPLY_PROP_SCOPE:
1972                 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1973                 break;
1974         case POWER_SUPPLY_PROP_CAPACITY:
1975                 val->intval = battery_capacity;
1976                 break;
1977         case POWER_SUPPLY_PROP_STATUS:
1978                 if (battery_charging)
1979                         val->intval = POWER_SUPPLY_STATUS_CHARGING;
1980                 else
1981                         if (battery_capacity == 100 && cable_state)
1982                                 val->intval = POWER_SUPPLY_STATUS_FULL;
1983                         else
1984                                 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1985                 break;
1986         default:
1987                 ret = -EINVAL;
1988                 break;
1989         }
1990         return ret;
1991 }
1992
1993 static int sony_battery_probe(struct sony_sc *sc)
1994 {
1995         struct power_supply_config psy_cfg = { .drv_data = sc, };
1996         struct hid_device *hdev = sc->hdev;
1997         int ret;
1998
1999         /*
2000          * Set the default battery level to 100% to avoid low battery warnings
2001          * if the battery is polled before the first device report is received.
2002          */
2003         sc->battery_capacity = 100;
2004
2005         sc->battery_desc.properties = sony_battery_props;
2006         sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
2007         sc->battery_desc.get_property = sony_battery_get_property;
2008         sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
2009         sc->battery_desc.use_for_apm = 0;
2010         sc->battery_desc.name = kasprintf(GFP_KERNEL,
2011                                           "sony_controller_battery_%pMR",
2012                                           sc->mac_address);
2013         if (!sc->battery_desc.name)
2014                 return -ENOMEM;
2015
2016         sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
2017                                             &psy_cfg);
2018         if (IS_ERR(sc->battery)) {
2019                 ret = PTR_ERR(sc->battery);
2020                 hid_err(hdev, "Unable to register battery device\n");
2021                 goto err_free;
2022         }
2023
2024         power_supply_powers(sc->battery, &hdev->dev);
2025         return 0;
2026
2027 err_free:
2028         kfree(sc->battery_desc.name);
2029         sc->battery_desc.name = NULL;
2030         return ret;
2031 }
2032
2033 static void sony_battery_remove(struct sony_sc *sc)
2034 {
2035         if (!sc->battery_desc.name)
2036                 return;
2037
2038         power_supply_unregister(sc->battery);
2039         kfree(sc->battery_desc.name);
2040         sc->battery_desc.name = NULL;
2041 }
2042
2043 /*
2044  * If a controller is plugged in via USB while already connected via Bluetooth
2045  * it will show up as two devices. A global list of connected controllers and
2046  * their MAC addresses is maintained to ensure that a device is only connected
2047  * once.
2048  */
2049 static int sony_check_add_dev_list(struct sony_sc *sc)
2050 {
2051         struct sony_sc *entry;
2052         unsigned long flags;
2053         int ret;
2054
2055         spin_lock_irqsave(&sony_dev_list_lock, flags);
2056
2057         list_for_each_entry(entry, &sony_device_list, list_node) {
2058                 ret = memcmp(sc->mac_address, entry->mac_address,
2059                                 sizeof(sc->mac_address));
2060                 if (!ret) {
2061                         ret = -EEXIST;
2062                         hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
2063                                 sc->mac_address);
2064                         goto unlock;
2065                 }
2066         }
2067
2068         ret = 0;
2069         list_add(&(sc->list_node), &sony_device_list);
2070
2071 unlock:
2072         spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2073         return ret;
2074 }
2075
2076 static void sony_remove_dev_list(struct sony_sc *sc)
2077 {
2078         unsigned long flags;
2079
2080         if (sc->list_node.next) {
2081                 spin_lock_irqsave(&sony_dev_list_lock, flags);
2082                 list_del(&(sc->list_node));
2083                 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2084         }
2085 }
2086
2087 static int sony_get_bt_devaddr(struct sony_sc *sc)
2088 {
2089         int ret;
2090
2091         /* HIDP stores the device MAC address as a string in the uniq field. */
2092         ret = strlen(sc->hdev->uniq);
2093         if (ret != 17)
2094                 return -EINVAL;
2095
2096         ret = sscanf(sc->hdev->uniq,
2097                 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2098                 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
2099                 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
2100
2101         if (ret != 6)
2102                 return -EINVAL;
2103
2104         return 0;
2105 }
2106
2107 static int sony_check_add(struct sony_sc *sc)
2108 {
2109         __u8 *buf = NULL;
2110         int n, ret;
2111
2112         if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
2113             (sc->quirks & MOTION_CONTROLLER_BT) ||
2114             (sc->quirks & NAVIGATION_CONTROLLER_BT) ||
2115             (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
2116                 /*
2117                  * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
2118                  * address from the uniq string where HIDP stores it.
2119                  * As uniq cannot be guaranteed to be a MAC address in all cases
2120                  * a failure of this function should not prevent the connection.
2121                  */
2122                 if (sony_get_bt_devaddr(sc) < 0) {
2123                         hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
2124                         return 0;
2125                 }
2126         } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
2127                 buf = kmalloc(DS4_REPORT_0x81_SIZE, GFP_KERNEL);
2128                 if (!buf)
2129                         return -ENOMEM;
2130
2131                 /*
2132                  * The MAC address of a DS4 controller connected via USB can be
2133                  * retrieved with feature report 0x81. The address begins at
2134                  * offset 1.
2135                  */
2136                 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
2137                                 DS4_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
2138                                 HID_REQ_GET_REPORT);
2139
2140                 if (ret != DS4_REPORT_0x81_SIZE) {
2141                         hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
2142                         ret = ret < 0 ? ret : -EINVAL;
2143                         goto out_free;
2144                 }
2145
2146                 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
2147         } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2148                         (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
2149                 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2150                 if (!buf)
2151                         return -ENOMEM;
2152
2153                 /*
2154                  * The MAC address of a Sixaxis controller connected via USB can
2155                  * be retrieved with feature report 0xf2. The address begins at
2156                  * offset 4.
2157                  */
2158                 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2159                                 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2160                                 HID_REQ_GET_REPORT);
2161
2162                 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
2163                         hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
2164                         ret = ret < 0 ? ret : -EINVAL;
2165                         goto out_free;
2166                 }
2167
2168                 /*
2169                  * The Sixaxis device MAC in the report is big-endian and must
2170                  * be byte-swapped.
2171                  */
2172                 for (n = 0; n < 6; n++)
2173                         sc->mac_address[5-n] = buf[4+n];
2174         } else {
2175                 return 0;
2176         }
2177
2178         ret = sony_check_add_dev_list(sc);
2179
2180 out_free:
2181
2182         kfree(buf);
2183
2184         return ret;
2185 }
2186
2187 static int sony_set_device_id(struct sony_sc *sc)
2188 {
2189         int ret;
2190
2191         /*
2192          * Only DualShock 4 or Sixaxis controllers get an id.
2193          * All others are set to -1.
2194          */
2195         if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2196             (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2197                 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2198                                         GFP_KERNEL);
2199                 if (ret < 0) {
2200                         sc->device_id = -1;
2201                         return ret;
2202                 }
2203                 sc->device_id = ret;
2204         } else {
2205                 sc->device_id = -1;
2206         }
2207
2208         return 0;
2209 }
2210
2211 static void sony_release_device_id(struct sony_sc *sc)
2212 {
2213         if (sc->device_id >= 0) {
2214                 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2215                 sc->device_id = -1;
2216         }
2217 }
2218
2219 static inline void sony_init_work(struct sony_sc *sc,
2220                                         void (*worker)(struct work_struct *))
2221 {
2222         if (!sc->worker_initialized)
2223                 INIT_WORK(&sc->state_worker, worker);
2224
2225         sc->worker_initialized = 1;
2226 }
2227
2228 static inline void sony_cancel_work_sync(struct sony_sc *sc)
2229 {
2230         if (sc->worker_initialized)
2231                 cancel_work_sync(&sc->state_worker);
2232 }
2233
2234 static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2235 {
2236         int ret;
2237         unsigned long quirks = id->driver_data;
2238         struct sony_sc *sc;
2239         unsigned int connect_mask = HID_CONNECT_DEFAULT;
2240
2241         sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
2242         if (sc == NULL) {
2243                 hid_err(hdev, "can't alloc sony descriptor\n");
2244                 return -ENOMEM;
2245         }
2246
2247         spin_lock_init(&sc->lock);
2248
2249         sc->quirks = quirks;
2250         hid_set_drvdata(hdev, sc);
2251         sc->hdev = hdev;
2252
2253         ret = hid_parse(hdev);
2254         if (ret) {
2255                 hid_err(hdev, "parse failed\n");
2256                 return ret;
2257         }
2258
2259         if (sc->quirks & VAIO_RDESC_CONSTANT)
2260                 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2261         else if (sc->quirks & SIXAXIS_CONTROLLER)
2262                 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2263
2264         ret = hid_hw_start(hdev, connect_mask);
2265         if (ret) {
2266                 hid_err(hdev, "hw start failed\n");
2267                 return ret;
2268         }
2269
2270         ret = sony_set_device_id(sc);
2271         if (ret < 0) {
2272                 hid_err(hdev, "failed to allocate the device id\n");
2273                 goto err_stop;
2274         }
2275
2276         ret = sony_allocate_output_report(sc);
2277         if (ret < 0) {
2278                 hid_err(hdev, "failed to allocate the output report buffer\n");
2279                 goto err_stop;
2280         }
2281
2282         if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2283                         (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
2284                 /*
2285                  * The Sony Sixaxis does not handle HID Output Reports on the
2286                  * Interrupt EP like it could, so we need to force HID Output
2287                  * Reports to use HID_REQ_SET_REPORT on the Control EP.
2288                  *
2289                  * There is also another issue about HID Output Reports via USB,
2290                  * the Sixaxis does not want the report_id as part of the data
2291                  * packet, so we have to discard buf[0] when sending the actual
2292                  * control message, even for numbered reports, humpf!
2293                  */
2294                 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2295                 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
2296                 ret = sixaxis_set_operational_usb(hdev);
2297                 sony_init_work(sc, sixaxis_state_worker);
2298         } else if ((sc->quirks & SIXAXIS_CONTROLLER_BT) ||
2299                         (sc->quirks & NAVIGATION_CONTROLLER_BT)) {
2300                 /*
2301                  * The Sixaxis wants output reports sent on the ctrl endpoint
2302                  * when connected via Bluetooth.
2303                  */
2304                 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2305                 ret = sixaxis_set_operational_bt(hdev);
2306                 sony_init_work(sc, sixaxis_state_worker);
2307         } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
2308                 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
2309                         /*
2310                          * The DualShock 4 wants output reports sent on the ctrl
2311                          * endpoint when connected via Bluetooth.
2312                          */
2313                         hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2314                         ret = dualshock4_set_operational_bt(hdev);
2315                         if (ret < 0) {
2316                                 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
2317                                 goto err_stop;
2318                         }
2319                 }
2320
2321                 sony_init_work(sc, dualshock4_state_worker);
2322         } else if (sc->quirks & MOTION_CONTROLLER) {
2323                 sony_init_work(sc, motion_state_worker);
2324         } else {
2325                 ret = 0;
2326         }
2327
2328         if (ret < 0)
2329                 goto err_stop;
2330
2331         ret = sony_check_add(sc);
2332         if (ret < 0)
2333                 goto err_stop;
2334
2335         if (sc->quirks & SONY_LED_SUPPORT) {
2336                 ret = sony_leds_init(sc);
2337                 if (ret < 0)
2338                         goto err_stop;
2339         }
2340
2341         if (sc->quirks & SONY_BATTERY_SUPPORT) {
2342                 ret = sony_battery_probe(sc);
2343                 if (ret < 0)
2344                         goto err_stop;
2345
2346                 /* Open the device to receive reports with battery info */
2347                 ret = hid_hw_open(hdev);
2348                 if (ret < 0) {
2349                         hid_err(hdev, "hw open failed\n");
2350                         goto err_stop;
2351                 }
2352         }
2353
2354         if (sc->quirks & SONY_FF_SUPPORT) {
2355                 ret = sony_init_ff(sc);
2356                 if (ret < 0)
2357                         goto err_close;
2358         }
2359
2360         return 0;
2361 err_close:
2362         hid_hw_close(hdev);
2363 err_stop:
2364         if (sc->quirks & SONY_LED_SUPPORT)
2365                 sony_leds_remove(sc);
2366         if (sc->quirks & SONY_BATTERY_SUPPORT)
2367                 sony_battery_remove(sc);
2368         sony_cancel_work_sync(sc);
2369         kfree(sc->output_report_dmabuf);
2370         sony_remove_dev_list(sc);
2371         sony_release_device_id(sc);
2372         hid_hw_stop(hdev);
2373         return ret;
2374 }
2375
2376 static void sony_remove(struct hid_device *hdev)
2377 {
2378         struct sony_sc *sc = hid_get_drvdata(hdev);
2379
2380         if (sc->quirks & SONY_LED_SUPPORT)
2381                 sony_leds_remove(sc);
2382
2383         if (sc->quirks & SONY_BATTERY_SUPPORT) {
2384                 hid_hw_close(hdev);
2385                 sony_battery_remove(sc);
2386         }
2387
2388         sony_cancel_work_sync(sc);
2389
2390         kfree(sc->output_report_dmabuf);
2391
2392         sony_remove_dev_list(sc);
2393
2394         sony_release_device_id(sc);
2395
2396         hid_hw_stop(hdev);
2397 }
2398
2399 static const struct hid_device_id sony_devices[] = {
2400         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2401                 .driver_data = SIXAXIS_CONTROLLER_USB },
2402         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2403                 .driver_data = NAVIGATION_CONTROLLER_USB },
2404         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2405                 .driver_data = NAVIGATION_CONTROLLER_BT },
2406         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
2407                 .driver_data = MOTION_CONTROLLER_USB },
2408         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
2409                 .driver_data = MOTION_CONTROLLER_BT },
2410         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2411                 .driver_data = SIXAXIS_CONTROLLER_BT },
2412         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2413                 .driver_data = VAIO_RDESC_CONSTANT },
2414         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2415                 .driver_data = VAIO_RDESC_CONSTANT },
2416         /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2417          * Logitech joystick from the device descriptor. */
2418         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2419                 .driver_data = BUZZ_CONTROLLER },
2420         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2421                 .driver_data = BUZZ_CONTROLLER },
2422         /* PS3 BD Remote Control */
2423         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2424                 .driver_data = PS3REMOTE },
2425         /* Logitech Harmony Adapter for PS3 */
2426         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2427                 .driver_data = PS3REMOTE },
2428         /* SMK-Link PS3 BD Remote Control */
2429         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2430                 .driver_data = PS3REMOTE },
2431         /* Sony Dualshock 4 controllers for PS4 */
2432         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
2433                 .driver_data = DUALSHOCK4_CONTROLLER_USB },
2434         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
2435                 .driver_data = DUALSHOCK4_CONTROLLER_BT },
2436         { }
2437 };
2438 MODULE_DEVICE_TABLE(hid, sony_devices);
2439
2440 static struct hid_driver sony_driver = {
2441         .name             = "sony",
2442         .id_table         = sony_devices,
2443         .input_mapping    = sony_mapping,
2444         .input_configured = sony_input_configured,
2445         .probe            = sony_probe,
2446         .remove           = sony_remove,
2447         .report_fixup     = sony_report_fixup,
2448         .raw_event        = sony_raw_event
2449 };
2450
2451 static int __init sony_init(void)
2452 {
2453         dbg_hid("Sony:%s\n", __func__);
2454
2455         return hid_register_driver(&sony_driver);
2456 }
2457
2458 static void __exit sony_exit(void)
2459 {
2460         dbg_hid("Sony:%s\n", __func__);
2461
2462         hid_unregister_driver(&sony_driver);
2463         ida_destroy(&sony_device_id_allocator);
2464 }
2465 module_init(sony_init);
2466 module_exit(sony_exit);
2467
2468 MODULE_LICENSE("GPL");