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