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