HID: sony: Set the Sixaxis cable state correctly
[firefly-linux-kernel-4.4.55.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                 cable_state = 1;
939         } else {
940                 __u8 index = rd[30] <= 5 ? rd[30] : 5;
941                 battery_capacity = sixaxis_battery_capacity[index];
942                 battery_charging = 0;
943                 cable_state = 0;
944         }
945
946         spin_lock_irqsave(&sc->lock, flags);
947         sc->cable_state = cable_state;
948         sc->battery_capacity = battery_capacity;
949         sc->battery_charging = battery_charging;
950         spin_unlock_irqrestore(&sc->lock, flags);
951 }
952
953 static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
954 {
955         struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
956                                                 struct hid_input, list);
957         struct input_dev *input_dev = hidinput->input;
958         unsigned long flags;
959         int n, offset;
960         __u8 cable_state, battery_capacity, battery_charging;
961
962         /*
963          * Battery and touchpad data starts at byte 30 in the USB report and
964          * 32 in Bluetooth report.
965          */
966         offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
967
968         /*
969          * The lower 4 bits of byte 30 contain the battery level
970          * and the 5th bit contains the USB cable state.
971          */
972         cable_state = (rd[offset] >> 4) & 0x01;
973         battery_capacity = rd[offset] & 0x0F;
974
975         /*
976          * When a USB power source is connected the battery level ranges from
977          * 0 to 10, and when running on battery power it ranges from 0 to 9.
978          * A battery level above 10 when plugged in means charge completed.
979          */
980         if (!cable_state || battery_capacity > 10)
981                 battery_charging = 0;
982         else
983                 battery_charging = 1;
984
985         if (!cable_state)
986                 battery_capacity++;
987         if (battery_capacity > 10)
988                 battery_capacity = 10;
989
990         battery_capacity *= 10;
991
992         spin_lock_irqsave(&sc->lock, flags);
993         sc->cable_state = cable_state;
994         sc->battery_capacity = battery_capacity;
995         sc->battery_charging = battery_charging;
996         spin_unlock_irqrestore(&sc->lock, flags);
997
998         offset += 5;
999
1000         /*
1001          * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
1002          * and 37 on Bluetooth.
1003          * The first 7 bits of the first byte is a counter and bit 8 is a touch
1004          * indicator that is 0 when pressed and 1 when not pressed.
1005          * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1006          * The data for the second touch is in the same format and immediatly
1007          * follows the data for the first.
1008          */
1009         for (n = 0; n < 2; n++) {
1010                 __u16 x, y;
1011
1012                 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1013                 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1014
1015                 input_mt_slot(input_dev, n);
1016                 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
1017                                         !(rd[offset] >> 7));
1018                 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1019                 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1020
1021                 offset += 4;
1022         }
1023 }
1024
1025 static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
1026                 __u8 *rd, int size)
1027 {
1028         struct sony_sc *sc = hid_get_drvdata(hdev);
1029
1030         /*
1031          * Sixaxis HID report has acclerometers/gyro with MSByte first, this
1032          * has to be BYTE_SWAPPED before passing up to joystick interface
1033          */
1034         if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
1035                 swap(rd[41], rd[42]);
1036                 swap(rd[43], rd[44]);
1037                 swap(rd[45], rd[46]);
1038                 swap(rd[47], rd[48]);
1039
1040                 sixaxis_parse_report(sc, rd, size);
1041         } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1042                         size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1043                         && rd[0] == 0x11 && size == 78)) {
1044                 dualshock4_parse_report(sc, rd, size);
1045         }
1046
1047         return 0;
1048 }
1049
1050 static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1051                         struct hid_field *field, struct hid_usage *usage,
1052                         unsigned long **bit, int *max)
1053 {
1054         struct sony_sc *sc = hid_get_drvdata(hdev);
1055
1056         if (sc->quirks & BUZZ_CONTROLLER) {
1057                 unsigned int key = usage->hid & HID_USAGE;
1058
1059                 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1060                         return -1;
1061
1062                 switch (usage->collection_index) {
1063                 case 1:
1064                         if (key >= ARRAY_SIZE(buzz_keymap))
1065                                 return -1;
1066
1067                         key = buzz_keymap[key];
1068                         if (!key)
1069                                 return -1;
1070                         break;
1071                 default:
1072                         return -1;
1073                 }
1074
1075                 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1076                 return 1;
1077         }
1078
1079         if (sc->quirks & PS3REMOTE)
1080                 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1081
1082         /* Let hid-core decide for the others */
1083         return 0;
1084 }
1085
1086 /*
1087  * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1088  * to "operational".  Without this, the ps3 controller will not report any
1089  * events.
1090  */
1091 static int sixaxis_set_operational_usb(struct hid_device *hdev)
1092 {
1093         int ret;
1094         char *buf = kmalloc(18, GFP_KERNEL);
1095
1096         if (!buf)
1097                 return -ENOMEM;
1098
1099         ret = hid_hw_raw_request(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT,
1100                                  HID_REQ_GET_REPORT);
1101
1102         if (ret < 0)
1103                 hid_err(hdev, "can't set operational mode\n");
1104
1105         kfree(buf);
1106
1107         return ret;
1108 }
1109
1110 static int sixaxis_set_operational_bt(struct hid_device *hdev)
1111 {
1112         unsigned char buf[] = { 0xf4,  0x42, 0x03, 0x00, 0x00 };
1113         return hid_hw_raw_request(hdev, buf[0], buf, sizeof(buf),
1114                                   HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
1115 }
1116
1117 /*
1118  * Requesting feature report 0x02 in Bluetooth mode changes the state of the
1119  * controller so that it sends full input reports of type 0x11.
1120  */
1121 static int dualshock4_set_operational_bt(struct hid_device *hdev)
1122 {
1123         __u8 buf[37] = { 0 };
1124
1125         return hid_hw_raw_request(hdev, 0x02, buf, sizeof(buf),
1126                                 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1127 }
1128
1129 static void sixaxis_set_leds_from_id(int id, __u8 values[MAX_LEDS])
1130 {
1131         static const __u8 sixaxis_leds[10][4] = {
1132                                 { 0x01, 0x00, 0x00, 0x00 },
1133                                 { 0x00, 0x01, 0x00, 0x00 },
1134                                 { 0x00, 0x00, 0x01, 0x00 },
1135                                 { 0x00, 0x00, 0x00, 0x01 },
1136                                 { 0x01, 0x00, 0x00, 0x01 },
1137                                 { 0x00, 0x01, 0x00, 0x01 },
1138                                 { 0x00, 0x00, 0x01, 0x01 },
1139                                 { 0x01, 0x00, 0x01, 0x01 },
1140                                 { 0x00, 0x01, 0x01, 0x01 },
1141                                 { 0x01, 0x01, 0x01, 0x01 }
1142         };
1143
1144         BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
1145
1146         if (id < 0)
1147                 return;
1148
1149         id %= 10;
1150         memcpy(values, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
1151 }
1152
1153 static void dualshock4_set_leds_from_id(int id, __u8 values[MAX_LEDS])
1154 {
1155         /* The first 4 color/index entries match what the PS4 assigns */
1156         static const __u8 color_code[7][3] = {
1157                         /* Blue   */    { 0x00, 0x00, 0x01 },
1158                         /* Red    */    { 0x01, 0x00, 0x00 },
1159                         /* Green  */    { 0x00, 0x01, 0x00 },
1160                         /* Pink   */    { 0x02, 0x00, 0x01 },
1161                         /* Orange */    { 0x02, 0x01, 0x00 },
1162                         /* Teal   */    { 0x00, 0x01, 0x01 },
1163                         /* White  */    { 0x01, 0x01, 0x01 }
1164         };
1165
1166         BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
1167
1168         if (id < 0)
1169                 return;
1170
1171         id %= 7;
1172         memcpy(values, color_code[id], sizeof(color_code[id]));
1173 }
1174
1175 static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
1176 {
1177         struct list_head *report_list =
1178                 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1179         struct hid_report *report = list_entry(report_list->next,
1180                 struct hid_report, list);
1181         __s32 *value = report->field[0]->value;
1182
1183         value[0] = 0x00;
1184         value[1] = leds[0] ? 0xff : 0x00;
1185         value[2] = leds[1] ? 0xff : 0x00;
1186         value[3] = leds[2] ? 0xff : 0x00;
1187         value[4] = leds[3] ? 0xff : 0x00;
1188         value[5] = 0x00;
1189         value[6] = 0x00;
1190         hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1191 }
1192
1193 static void sony_set_leds(struct sony_sc *sc, const __u8 *leds, int count)
1194 {
1195         int n;
1196
1197         BUG_ON(count > MAX_LEDS);
1198
1199         if (sc->quirks & BUZZ_CONTROLLER && count == 4) {
1200                 buzz_set_leds(sc->hdev, leds);
1201         } else {
1202                 for (n = 0; n < count; n++)
1203                         sc->led_state[n] = leds[n];
1204                 schedule_work(&sc->state_worker);
1205         }
1206 }
1207
1208 static void sony_led_set_brightness(struct led_classdev *led,
1209                                     enum led_brightness value)
1210 {
1211         struct device *dev = led->dev->parent;
1212         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1213         struct sony_sc *drv_data;
1214
1215         int n;
1216         int force_update;
1217
1218         drv_data = hid_get_drvdata(hdev);
1219         if (!drv_data) {
1220                 hid_err(hdev, "No device data\n");
1221                 return;
1222         }
1223
1224         /*
1225          * The Sixaxis on USB will override any LED settings sent to it
1226          * and keep flashing all of the LEDs until the PS button is pressed.
1227          * Updates, even if redundant, must be always be sent to the
1228          * controller to avoid having to toggle the state of an LED just to
1229          * stop the flashing later on.
1230          */
1231         force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1232
1233         for (n = 0; n < drv_data->led_count; n++) {
1234                 if (led == drv_data->leds[n] && (force_update ||
1235                         (value != drv_data->led_state[n] ||
1236                         drv_data->led_delay_on[n] ||
1237                         drv_data->led_delay_off[n]))) {
1238
1239                         drv_data->led_state[n] = value;
1240
1241                         /* Setting the brightness stops the blinking */
1242                         drv_data->led_delay_on[n] = 0;
1243                         drv_data->led_delay_off[n] = 0;
1244
1245                         sony_set_leds(drv_data, drv_data->led_state,
1246                                         drv_data->led_count);
1247                         break;
1248                 }
1249         }
1250 }
1251
1252 static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
1253 {
1254         struct device *dev = led->dev->parent;
1255         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1256         struct sony_sc *drv_data;
1257
1258         int n;
1259
1260         drv_data = hid_get_drvdata(hdev);
1261         if (!drv_data) {
1262                 hid_err(hdev, "No device data\n");
1263                 return LED_OFF;
1264         }
1265
1266         for (n = 0; n < drv_data->led_count; n++) {
1267                 if (led == drv_data->leds[n])
1268                         return drv_data->led_state[n];
1269         }
1270
1271         return LED_OFF;
1272 }
1273
1274 static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1275                                 unsigned long *delay_off)
1276 {
1277         struct device *dev = led->dev->parent;
1278         struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1279         struct sony_sc *drv_data = hid_get_drvdata(hdev);
1280         int n;
1281         __u8 new_on, new_off;
1282
1283         if (!drv_data) {
1284                 hid_err(hdev, "No device data\n");
1285                 return -EINVAL;
1286         }
1287
1288         /* Max delay is 255 deciseconds or 2550 milliseconds */
1289         if (*delay_on > 2550)
1290                 *delay_on = 2550;
1291         if (*delay_off > 2550)
1292                 *delay_off = 2550;
1293
1294         /* Blink at 1 Hz if both values are zero */
1295         if (!*delay_on && !*delay_off)
1296                 *delay_on = *delay_off = 500;
1297
1298         new_on = *delay_on / 10;
1299         new_off = *delay_off / 10;
1300
1301         for (n = 0; n < drv_data->led_count; n++) {
1302                 if (led == drv_data->leds[n])
1303                         break;
1304         }
1305
1306         /* This LED is not registered on this device */
1307         if (n >= drv_data->led_count)
1308                 return -EINVAL;
1309
1310         /* Don't schedule work if the values didn't change */
1311         if (new_on != drv_data->led_delay_on[n] ||
1312                 new_off != drv_data->led_delay_off[n]) {
1313                 drv_data->led_delay_on[n] = new_on;
1314                 drv_data->led_delay_off[n] = new_off;
1315                 schedule_work(&drv_data->state_worker);
1316         }
1317
1318         return 0;
1319 }
1320
1321 static void sony_leds_remove(struct sony_sc *sc)
1322 {
1323         struct led_classdev *led;
1324         int n;
1325
1326         BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
1327
1328         for (n = 0; n < sc->led_count; n++) {
1329                 led = sc->leds[n];
1330                 sc->leds[n] = NULL;
1331                 if (!led)
1332                         continue;
1333                 led_classdev_unregister(led);
1334                 kfree(led);
1335         }
1336
1337         sc->led_count = 0;
1338 }
1339
1340 static int sony_leds_init(struct sony_sc *sc)
1341 {
1342         struct hid_device *hdev = sc->hdev;
1343         int n, ret = 0;
1344         int use_ds4_names;
1345         struct led_classdev *led;
1346         size_t name_sz;
1347         char *name;
1348         size_t name_len;
1349         const char *name_fmt;
1350         static const char * const ds4_name_str[] = { "red", "green", "blue",
1351                                                   "global" };
1352         __u8 initial_values[MAX_LEDS] = { 0 };
1353         __u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
1354         __u8 use_hw_blink[MAX_LEDS] = { 0 };
1355
1356         BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
1357
1358         if (sc->quirks & BUZZ_CONTROLLER) {
1359                 sc->led_count = 4;
1360                 use_ds4_names = 0;
1361                 name_len = strlen("::buzz#");
1362                 name_fmt = "%s::buzz%d";
1363                 /* Validate expected report characteristics. */
1364                 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1365                         return -ENODEV;
1366         } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
1367                 dualshock4_set_leds_from_id(sc->device_id, initial_values);
1368                 initial_values[3] = 1;
1369                 sc->led_count = 4;
1370                 memset(max_brightness, 255, 3);
1371                 use_hw_blink[3] = 1;
1372                 use_ds4_names = 1;
1373                 name_len = 0;
1374                 name_fmt = "%s:%s";
1375         } else {
1376                 sixaxis_set_leds_from_id(sc->device_id, initial_values);
1377                 sc->led_count = 4;
1378                 memset(use_hw_blink, 1, 4);
1379                 use_ds4_names = 0;
1380                 name_len = strlen("::sony#");
1381                 name_fmt = "%s::sony%d";
1382         }
1383
1384         /*
1385          * Clear LEDs as we have no way of reading their initial state. This is
1386          * only relevant if the driver is loaded after somebody actively set the
1387          * LEDs to on
1388          */
1389         sony_set_leds(sc, initial_values, sc->led_count);
1390
1391         name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
1392
1393         for (n = 0; n < sc->led_count; n++) {
1394
1395                 if (use_ds4_names)
1396                         name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
1397
1398                 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1399                 if (!led) {
1400                         hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
1401                         ret = -ENOMEM;
1402                         goto error_leds;
1403                 }
1404
1405                 name = (void *)(&led[1]);
1406                 if (use_ds4_names)
1407                         snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1408                         ds4_name_str[n]);
1409                 else
1410                         snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
1411                 led->name = name;
1412                 led->brightness = initial_values[n];
1413                 led->max_brightness = max_brightness[n];
1414                 led->brightness_get = sony_led_get_brightness;
1415                 led->brightness_set = sony_led_set_brightness;
1416
1417                 if (use_hw_blink[n])
1418                         led->blink_set = sony_led_blink_set;
1419
1420                 sc->leds[n] = led;
1421
1422                 ret = led_classdev_register(&hdev->dev, led);
1423                 if (ret) {
1424                         hid_err(hdev, "Failed to register LED %d\n", n);
1425                         sc->leds[n] = NULL;
1426                         kfree(led);
1427                         goto error_leds;
1428                 }
1429         }
1430
1431         return ret;
1432
1433 error_leds:
1434         sony_leds_remove(sc);
1435
1436         return ret;
1437 }
1438
1439 static void sixaxis_state_worker(struct work_struct *work)
1440 {
1441         struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1442         int n;
1443         union sixaxis_output_report_01 report = {
1444                 .buf = {
1445                         0x01,
1446                         0x00, 0xff, 0x00, 0xff, 0x00,
1447                         0x00, 0x00, 0x00, 0x00, 0x00,
1448                         0xff, 0x27, 0x10, 0x00, 0x32,
1449                         0xff, 0x27, 0x10, 0x00, 0x32,
1450                         0xff, 0x27, 0x10, 0x00, 0x32,
1451                         0xff, 0x27, 0x10, 0x00, 0x32,
1452                         0x00, 0x00, 0x00, 0x00, 0x00
1453                 }
1454         };
1455
1456 #ifdef CONFIG_SONY_FF
1457         report.data.rumble.right_motor_on = sc->right ? 1 : 0;
1458         report.data.rumble.left_motor_force = sc->left;
1459 #endif
1460
1461         report.data.leds_bitmap |= sc->led_state[0] << 1;
1462         report.data.leds_bitmap |= sc->led_state[1] << 2;
1463         report.data.leds_bitmap |= sc->led_state[2] << 3;
1464         report.data.leds_bitmap |= sc->led_state[3] << 4;
1465
1466         /* Set flag for all leds off, required for 3rd party INTEC controller */
1467         if ((report.data.leds_bitmap & 0x1E) == 0)
1468                 report.data.leds_bitmap |= 0x20;
1469
1470         /*
1471          * The LEDs in the report are indexed in reverse order to their
1472          * corresponding light on the controller.
1473          * Index 0 = LED 4, index 1 = LED 3, etc...
1474          *
1475          * In the case of both delay values being zero (blinking disabled) the
1476          * default report values should be used or the controller LED will be
1477          * always off.
1478          */
1479         for (n = 0; n < 4; n++) {
1480                 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
1481                         report.data.led[3 - n].duty_off = sc->led_delay_off[n];
1482                         report.data.led[3 - n].duty_on = sc->led_delay_on[n];
1483                 }
1484         }
1485
1486         hid_hw_raw_request(sc->hdev, report.data.report_id, report.buf,
1487                         sizeof(report), HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
1488 }
1489
1490 static void dualshock4_state_worker(struct work_struct *work)
1491 {
1492         struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1493         struct hid_device *hdev = sc->hdev;
1494         int offset;
1495
1496         __u8 buf[78] = { 0 };
1497
1498         if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1499                 buf[0] = 0x05;
1500                 buf[1] = 0xFF;
1501                 offset = 4;
1502         } else {
1503                 buf[0] = 0x11;
1504                 buf[1] = 0xB0;
1505                 buf[3] = 0x0F;
1506                 offset = 6;
1507         }
1508
1509 #ifdef CONFIG_SONY_FF
1510         buf[offset++] = sc->right;
1511         buf[offset++] = sc->left;
1512 #else
1513         offset += 2;
1514 #endif
1515
1516         /* LED 3 is the global control */
1517         if (sc->led_state[3]) {
1518                 buf[offset++] = sc->led_state[0];
1519                 buf[offset++] = sc->led_state[1];
1520                 buf[offset++] = sc->led_state[2];
1521         } else {
1522                 offset += 3;
1523         }
1524
1525         /* If both delay values are zero the DualShock 4 disables blinking. */
1526         buf[offset++] = sc->led_delay_on[3];
1527         buf[offset++] = sc->led_delay_off[3];
1528
1529         if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1530                 hid_hw_output_report(hdev, buf, 32);
1531         else
1532                 hid_hw_raw_request(hdev, 0x11, buf, 78,
1533                                 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
1534 }
1535
1536 #ifdef CONFIG_SONY_FF
1537 static int sony_play_effect(struct input_dev *dev, void *data,
1538                             struct ff_effect *effect)
1539 {
1540         struct hid_device *hid = input_get_drvdata(dev);
1541         struct sony_sc *sc = hid_get_drvdata(hid);
1542
1543         if (effect->type != FF_RUMBLE)
1544                 return 0;
1545
1546         sc->left = effect->u.rumble.strong_magnitude / 256;
1547         sc->right = effect->u.rumble.weak_magnitude / 256;
1548
1549         schedule_work(&sc->state_worker);
1550         return 0;
1551 }
1552
1553 static int sony_init_ff(struct sony_sc *sc)
1554 {
1555         struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1556                                                 struct hid_input, list);
1557         struct input_dev *input_dev = hidinput->input;
1558
1559         input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1560         return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1561 }
1562
1563 #else
1564 static int sony_init_ff(struct sony_sc *sc)
1565 {
1566         return 0;
1567 }
1568
1569 #endif
1570
1571 static int sony_battery_get_property(struct power_supply *psy,
1572                                      enum power_supply_property psp,
1573                                      union power_supply_propval *val)
1574 {
1575         struct sony_sc *sc = container_of(psy, struct sony_sc, battery);
1576         unsigned long flags;
1577         int ret = 0;
1578         u8 battery_charging, battery_capacity, cable_state;
1579
1580         spin_lock_irqsave(&sc->lock, flags);
1581         battery_charging = sc->battery_charging;
1582         battery_capacity = sc->battery_capacity;
1583         cable_state = sc->cable_state;
1584         spin_unlock_irqrestore(&sc->lock, flags);
1585
1586         switch (psp) {
1587         case POWER_SUPPLY_PROP_PRESENT:
1588                 val->intval = 1;
1589                 break;
1590         case POWER_SUPPLY_PROP_SCOPE:
1591                 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1592                 break;
1593         case POWER_SUPPLY_PROP_CAPACITY:
1594                 val->intval = battery_capacity;
1595                 break;
1596         case POWER_SUPPLY_PROP_STATUS:
1597                 if (battery_charging)
1598                         val->intval = POWER_SUPPLY_STATUS_CHARGING;
1599                 else
1600                         if (battery_capacity == 100 && cable_state)
1601                                 val->intval = POWER_SUPPLY_STATUS_FULL;
1602                         else
1603                                 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1604                 break;
1605         default:
1606                 ret = -EINVAL;
1607                 break;
1608         }
1609         return ret;
1610 }
1611
1612 static int sony_battery_probe(struct sony_sc *sc)
1613 {
1614         struct hid_device *hdev = sc->hdev;
1615         int ret;
1616
1617         /*
1618          * Set the default battery level to 100% to avoid low battery warnings
1619          * if the battery is polled before the first device report is received.
1620          */
1621         sc->battery_capacity = 100;
1622
1623         sc->battery.properties = sony_battery_props;
1624         sc->battery.num_properties = ARRAY_SIZE(sony_battery_props);
1625         sc->battery.get_property = sony_battery_get_property;
1626         sc->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1627         sc->battery.use_for_apm = 0;
1628         sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%pMR",
1629                                      sc->mac_address);
1630         if (!sc->battery.name)
1631                 return -ENOMEM;
1632
1633         ret = power_supply_register(&hdev->dev, &sc->battery);
1634         if (ret) {
1635                 hid_err(hdev, "Unable to register battery device\n");
1636                 goto err_free;
1637         }
1638
1639         power_supply_powers(&sc->battery, &hdev->dev);
1640         return 0;
1641
1642 err_free:
1643         kfree(sc->battery.name);
1644         sc->battery.name = NULL;
1645         return ret;
1646 }
1647
1648 static void sony_battery_remove(struct sony_sc *sc)
1649 {
1650         if (!sc->battery.name)
1651                 return;
1652
1653         power_supply_unregister(&sc->battery);
1654         kfree(sc->battery.name);
1655         sc->battery.name = NULL;
1656 }
1657
1658 static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
1659                                         int w, int h)
1660 {
1661         struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1662                                                 struct hid_input, list);
1663         struct input_dev *input_dev = hidinput->input;
1664         int ret;
1665
1666         ret = input_mt_init_slots(input_dev, touch_count, 0);
1667         if (ret < 0) {
1668                 hid_err(sc->hdev, "Unable to initialize multi-touch slots\n");
1669                 return ret;
1670         }
1671
1672         input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1673         input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1674
1675         return 0;
1676 }
1677
1678 /*
1679  * If a controller is plugged in via USB while already connected via Bluetooth
1680  * it will show up as two devices. A global list of connected controllers and
1681  * their MAC addresses is maintained to ensure that a device is only connected
1682  * once.
1683  */
1684 static int sony_check_add_dev_list(struct sony_sc *sc)
1685 {
1686         struct sony_sc *entry;
1687         unsigned long flags;
1688         int ret;
1689
1690         spin_lock_irqsave(&sony_dev_list_lock, flags);
1691
1692         list_for_each_entry(entry, &sony_device_list, list_node) {
1693                 ret = memcmp(sc->mac_address, entry->mac_address,
1694                                 sizeof(sc->mac_address));
1695                 if (!ret) {
1696                         ret = -EEXIST;
1697                         hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
1698                                 sc->mac_address);
1699                         goto unlock;
1700                 }
1701         }
1702
1703         ret = 0;
1704         list_add(&(sc->list_node), &sony_device_list);
1705
1706 unlock:
1707         spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1708         return ret;
1709 }
1710
1711 static void sony_remove_dev_list(struct sony_sc *sc)
1712 {
1713         unsigned long flags;
1714
1715         if (sc->list_node.next) {
1716                 spin_lock_irqsave(&sony_dev_list_lock, flags);
1717                 list_del(&(sc->list_node));
1718                 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1719         }
1720 }
1721
1722 static int sony_get_bt_devaddr(struct sony_sc *sc)
1723 {
1724         int ret;
1725
1726         /* HIDP stores the device MAC address as a string in the uniq field. */
1727         ret = strlen(sc->hdev->uniq);
1728         if (ret != 17)
1729                 return -EINVAL;
1730
1731         ret = sscanf(sc->hdev->uniq,
1732                 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1733                 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
1734                 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
1735
1736         if (ret != 6)
1737                 return -EINVAL;
1738
1739         return 0;
1740 }
1741
1742 static int sony_check_add(struct sony_sc *sc)
1743 {
1744         int n, ret;
1745
1746         if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
1747             (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
1748                 /*
1749                  * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
1750                  * address from the uniq string where HIDP stores it.
1751                  * As uniq cannot be guaranteed to be a MAC address in all cases
1752                  * a failure of this function should not prevent the connection.
1753                  */
1754                 if (sony_get_bt_devaddr(sc) < 0) {
1755                         hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
1756                         return 0;
1757                 }
1758         } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1759                 __u8 buf[7];
1760
1761                 /*
1762                  * The MAC address of a DS4 controller connected via USB can be
1763                  * retrieved with feature report 0x81. The address begins at
1764                  * offset 1.
1765                  */
1766                 ret = hid_hw_raw_request(sc->hdev, 0x81, buf, sizeof(buf),
1767                                 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1768
1769                 if (ret != 7) {
1770                         hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
1771                         return ret < 0 ? ret : -EINVAL;
1772                 }
1773
1774                 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
1775         } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1776                 __u8 buf[18];
1777
1778                 /*
1779                  * The MAC address of a Sixaxis controller connected via USB can
1780                  * be retrieved with feature report 0xf2. The address begins at
1781                  * offset 4.
1782                  */
1783                 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf, sizeof(buf),
1784                                 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1785
1786                 if (ret != 18) {
1787                         hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
1788                         return ret < 0 ? ret : -EINVAL;
1789                 }
1790
1791                 /*
1792                  * The Sixaxis device MAC in the report is big-endian and must
1793                  * be byte-swapped.
1794                  */
1795                 for (n = 0; n < 6; n++)
1796                         sc->mac_address[5-n] = buf[4+n];
1797         } else {
1798                 return 0;
1799         }
1800
1801         return sony_check_add_dev_list(sc);
1802 }
1803
1804 static int sony_set_device_id(struct sony_sc *sc)
1805 {
1806         int ret;
1807
1808         /*
1809          * Only DualShock 4 or Sixaxis controllers get an id.
1810          * All others are set to -1.
1811          */
1812         if ((sc->quirks & SIXAXIS_CONTROLLER) ||
1813             (sc->quirks & DUALSHOCK4_CONTROLLER)) {
1814                 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
1815                                         GFP_KERNEL);
1816                 if (ret < 0) {
1817                         sc->device_id = -1;
1818                         return ret;
1819                 }
1820                 sc->device_id = ret;
1821         } else {
1822                 sc->device_id = -1;
1823         }
1824
1825         return 0;
1826 }
1827
1828 static void sony_release_device_id(struct sony_sc *sc)
1829 {
1830         if (sc->device_id >= 0) {
1831                 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
1832                 sc->device_id = -1;
1833         }
1834 }
1835
1836 static inline void sony_init_work(struct sony_sc *sc,
1837                                         void (*worker)(struct work_struct *))
1838 {
1839         if (!sc->worker_initialized)
1840                 INIT_WORK(&sc->state_worker, worker);
1841
1842         sc->worker_initialized = 1;
1843 }
1844
1845 static inline void sony_cancel_work_sync(struct sony_sc *sc)
1846 {
1847         if (sc->worker_initialized)
1848                 cancel_work_sync(&sc->state_worker);
1849 }
1850
1851 static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1852 {
1853         int ret;
1854         unsigned long quirks = id->driver_data;
1855         struct sony_sc *sc;
1856         unsigned int connect_mask = HID_CONNECT_DEFAULT;
1857
1858         sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
1859         if (sc == NULL) {
1860                 hid_err(hdev, "can't alloc sony descriptor\n");
1861                 return -ENOMEM;
1862         }
1863
1864         sc->quirks = quirks;
1865         hid_set_drvdata(hdev, sc);
1866         sc->hdev = hdev;
1867
1868         ret = hid_parse(hdev);
1869         if (ret) {
1870                 hid_err(hdev, "parse failed\n");
1871                 return ret;
1872         }
1873
1874         if (sc->quirks & VAIO_RDESC_CONSTANT)
1875                 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1876         else if (sc->quirks & SIXAXIS_CONTROLLER)
1877                 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1878
1879         ret = hid_hw_start(hdev, connect_mask);
1880         if (ret) {
1881                 hid_err(hdev, "hw start failed\n");
1882                 return ret;
1883         }
1884
1885         ret = sony_set_device_id(sc);
1886         if (ret < 0) {
1887                 hid_err(hdev, "failed to allocate the device id\n");
1888                 goto err_stop;
1889         }
1890
1891         if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1892                 /*
1893                  * The Sony Sixaxis does not handle HID Output Reports on the
1894                  * Interrupt EP like it could, so we need to force HID Output
1895                  * Reports to use HID_REQ_SET_REPORT on the Control EP.
1896                  *
1897                  * There is also another issue about HID Output Reports via USB,
1898                  * the Sixaxis does not want the report_id as part of the data
1899                  * packet, so we have to discard buf[0] when sending the actual
1900                  * control message, even for numbered reports, humpf!
1901                  */
1902                 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
1903                 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
1904                 ret = sixaxis_set_operational_usb(hdev);
1905                 sony_init_work(sc, sixaxis_state_worker);
1906         } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
1907                 /*
1908                  * The Sixaxis wants output reports sent on the ctrl endpoint
1909                  * when connected via Bluetooth.
1910                  */
1911                 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
1912                 ret = sixaxis_set_operational_bt(hdev);
1913                 sony_init_work(sc, sixaxis_state_worker);
1914         } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
1915                 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
1916                         /*
1917                          * The DualShock 4 wants output reports sent on the ctrl
1918                          * endpoint when connected via Bluetooth.
1919                          */
1920                         hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
1921                         ret = dualshock4_set_operational_bt(hdev);
1922                         if (ret < 0) {
1923                                 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
1924                                 goto err_stop;
1925                         }
1926                 }
1927                 /*
1928                  * The Dualshock 4 touchpad supports 2 touches and has a
1929                  * resolution of 1920x940.
1930                  */
1931                 ret = sony_register_touchpad(sc, 2, 1920, 940);
1932                 if (ret < 0)
1933                         goto err_stop;
1934
1935                 sony_init_work(sc, dualshock4_state_worker);
1936         } else {
1937                 ret = 0;
1938         }
1939
1940         if (ret < 0)
1941                 goto err_stop;
1942
1943         ret = sony_check_add(sc);
1944         if (ret < 0)
1945                 goto err_stop;
1946
1947         if (sc->quirks & SONY_LED_SUPPORT) {
1948                 ret = sony_leds_init(sc);
1949                 if (ret < 0)
1950                         goto err_stop;
1951         }
1952
1953         if (sc->quirks & SONY_BATTERY_SUPPORT) {
1954                 ret = sony_battery_probe(sc);
1955                 if (ret < 0)
1956                         goto err_stop;
1957
1958                 /* Open the device to receive reports with battery info */
1959                 ret = hid_hw_open(hdev);
1960                 if (ret < 0) {
1961                         hid_err(hdev, "hw open failed\n");
1962                         goto err_stop;
1963                 }
1964         }
1965
1966         if (sc->quirks & SONY_FF_SUPPORT) {
1967                 ret = sony_init_ff(sc);
1968                 if (ret < 0)
1969                         goto err_close;
1970         }
1971
1972         return 0;
1973 err_close:
1974         hid_hw_close(hdev);
1975 err_stop:
1976         if (sc->quirks & SONY_LED_SUPPORT)
1977                 sony_leds_remove(sc);
1978         if (sc->quirks & SONY_BATTERY_SUPPORT)
1979                 sony_battery_remove(sc);
1980         sony_cancel_work_sync(sc);
1981         sony_remove_dev_list(sc);
1982         sony_release_device_id(sc);
1983         hid_hw_stop(hdev);
1984         return ret;
1985 }
1986
1987 static void sony_remove(struct hid_device *hdev)
1988 {
1989         struct sony_sc *sc = hid_get_drvdata(hdev);
1990
1991         if (sc->quirks & SONY_LED_SUPPORT)
1992                 sony_leds_remove(sc);
1993
1994         if (sc->quirks & SONY_BATTERY_SUPPORT) {
1995                 hid_hw_close(hdev);
1996                 sony_battery_remove(sc);
1997         }
1998
1999         sony_cancel_work_sync(sc);
2000
2001         sony_remove_dev_list(sc);
2002
2003         sony_release_device_id(sc);
2004
2005         hid_hw_stop(hdev);
2006 }
2007
2008 static const struct hid_device_id sony_devices[] = {
2009         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2010                 .driver_data = SIXAXIS_CONTROLLER_USB },
2011         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2012                 .driver_data = SIXAXIS_CONTROLLER_USB },
2013         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2014                 .driver_data = SIXAXIS_CONTROLLER_BT },
2015         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2016                 .driver_data = VAIO_RDESC_CONSTANT },
2017         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2018                 .driver_data = VAIO_RDESC_CONSTANT },
2019         /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2020          * Logitech joystick from the device descriptor. */
2021         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2022                 .driver_data = BUZZ_CONTROLLER },
2023         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2024                 .driver_data = BUZZ_CONTROLLER },
2025         /* PS3 BD Remote Control */
2026         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2027                 .driver_data = PS3REMOTE },
2028         /* Logitech Harmony Adapter for PS3 */
2029         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2030                 .driver_data = PS3REMOTE },
2031         /* Sony Dualshock 4 controllers for PS4 */
2032         { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
2033                 .driver_data = DUALSHOCK4_CONTROLLER_USB },
2034         { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
2035                 .driver_data = DUALSHOCK4_CONTROLLER_BT },
2036         { }
2037 };
2038 MODULE_DEVICE_TABLE(hid, sony_devices);
2039
2040 static struct hid_driver sony_driver = {
2041         .name          = "sony",
2042         .id_table      = sony_devices,
2043         .input_mapping = sony_mapping,
2044         .probe         = sony_probe,
2045         .remove        = sony_remove,
2046         .report_fixup  = sony_report_fixup,
2047         .raw_event     = sony_raw_event
2048 };
2049
2050 static int __init sony_init(void)
2051 {
2052         dbg_hid("Sony:%s\n", __func__);
2053
2054         return hid_register_driver(&sony_driver);
2055 }
2056
2057 static void __exit sony_exit(void)
2058 {
2059         dbg_hid("Sony:%s\n", __func__);
2060
2061         ida_destroy(&sony_device_id_allocator);
2062         hid_unregister_driver(&sony_driver);
2063 }
2064 module_init(sony_init);
2065 module_exit(sony_exit);
2066
2067 MODULE_LICENSE("GPL");