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