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