HID: sony: Set initial battery level to 100% to avoid false low battery warnings
[linux-2.6-block.git] / drivers / hid / hid-sony.c
CommitLineData
bd28ce00 1/*
078328da 2 * HID driver for Sony / PS2 / PS3 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>
bd28ce00
JS
11 */
12
13/*
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
17 * any later version.
18 */
19
078328da
JK
20/* NOTE: in order for the Sony PS3 BD Remote Control to be found by
21 * a Bluetooth host, the key combination Start+Enter has to be kept pressed
22 * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
23 *
24 * There will be no PIN request from the device.
25 */
26
bd28ce00
JS
27#include <linux/device.h>
28#include <linux/hid.h>
29#include <linux/module.h>
5a0e3ad6 30#include <linux/slab.h>
bd28ce00 31#include <linux/usb.h>
40e32ee6 32#include <linux/leds.h>
d902f472
FP
33#include <linux/power_supply.h>
34#include <linux/spinlock.h>
e5606230 35#include <linux/input/mt.h>
bd28ce00
JS
36
37#include "hid-ids.h"
38
6c79c18c
FP
39#define VAIO_RDESC_CONSTANT BIT(0)
40#define SIXAXIS_CONTROLLER_USB BIT(1)
41#define SIXAXIS_CONTROLLER_BT BIT(2)
42#define BUZZ_CONTROLLER BIT(3)
43#define PS3REMOTE BIT(4)
8ab1676b
FP
44#define DUALSHOCK4_CONTROLLER_USB BIT(5)
45#define DUALSHOCK4_CONTROLLER_BT BIT(6)
cc6e0bbb 46
8ab1676b 47#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER_USB | BUZZ_CONTROLLER | DUALSHOCK4_CONTROLLER_USB)
d902f472 48#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_USB)
60781cf4
FP
49
50#define MAX_LEDS 4
0a286ef2 51
61ab44be
SW
52static const u8 sixaxis_rdesc_fixup[] = {
53 0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
54 0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
55 0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
56};
57
e57a67da
MCC
58static const u8 sixaxis_rdesc_fixup2[] = {
59 0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
60 0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
61 0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
62 0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
63 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
64 0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
65 0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
66 0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
67 0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
68 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
69 0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
70 0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
71 0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
72 0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
73 0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
74 0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
75 0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
76 0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
77 0xb1, 0x02, 0xc0, 0xc0,
78};
79
58d7027b
FP
80/* The default descriptor doesn't provide mapping for the accelerometers
81 * or orientation sensors. This fixed descriptor maps the accelerometers
82 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
83 * to usage values 0x43, 0x44 and 0x45.
84 */
ed19d8cf 85static u8 dualshock4_usb_rdesc[] = {
58d7027b
FP
86 0x05, 0x01, /* Usage Page (Desktop), */
87 0x09, 0x05, /* Usage (Gamepad), */
88 0xA1, 0x01, /* Collection (Application), */
89 0x85, 0x01, /* Report ID (1), */
90 0x09, 0x30, /* Usage (X), */
91 0x09, 0x31, /* Usage (Y), */
92 0x09, 0x32, /* Usage (Z), */
93 0x09, 0x35, /* Usage (Rz), */
94 0x15, 0x00, /* Logical Minimum (0), */
95 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
96 0x75, 0x08, /* Report Size (8), */
97 0x95, 0x04, /* Report Count (4), */
98 0x81, 0x02, /* Input (Variable), */
99 0x09, 0x39, /* Usage (Hat Switch), */
100 0x15, 0x00, /* Logical Minimum (0), */
101 0x25, 0x07, /* Logical Maximum (7), */
102 0x35, 0x00, /* Physical Minimum (0), */
103 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
104 0x65, 0x14, /* Unit (Degrees), */
105 0x75, 0x04, /* Report Size (4), */
106 0x95, 0x01, /* Report Count (1), */
107 0x81, 0x42, /* Input (Variable, Null State), */
108 0x65, 0x00, /* Unit, */
109 0x05, 0x09, /* Usage Page (Button), */
110 0x19, 0x01, /* Usage Minimum (01h), */
111 0x29, 0x0E, /* Usage Maximum (0Eh), */
112 0x15, 0x00, /* Logical Minimum (0), */
113 0x25, 0x01, /* Logical Maximum (1), */
114 0x75, 0x01, /* Report Size (1), */
115 0x95, 0x0E, /* Report Count (14), */
116 0x81, 0x02, /* Input (Variable), */
117 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
118 0x09, 0x20, /* Usage (20h), */
119 0x75, 0x06, /* Report Size (6), */
120 0x95, 0x01, /* Report Count (1), */
121 0x15, 0x00, /* Logical Minimum (0), */
122 0x25, 0x7F, /* Logical Maximum (127), */
123 0x81, 0x02, /* Input (Variable), */
124 0x05, 0x01, /* Usage Page (Desktop), */
125 0x09, 0x33, /* Usage (Rx), */
126 0x09, 0x34, /* Usage (Ry), */
127 0x15, 0x00, /* Logical Minimum (0), */
128 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
129 0x75, 0x08, /* Report Size (8), */
130 0x95, 0x02, /* Report Count (2), */
131 0x81, 0x02, /* Input (Variable), */
132 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
133 0x09, 0x21, /* Usage (21h), */
134 0x95, 0x03, /* Report Count (3), */
135 0x81, 0x02, /* Input (Variable), */
136 0x05, 0x01, /* Usage Page (Desktop), */
137 0x19, 0x40, /* Usage Minimum (40h), */
138 0x29, 0x42, /* Usage Maximum (42h), */
139 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
140 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
141 0x75, 0x10, /* Report Size (16), */
142 0x95, 0x03, /* Report Count (3), */
143 0x81, 0x02, /* Input (Variable), */
144 0x19, 0x43, /* Usage Minimum (43h), */
145 0x29, 0x45, /* Usage Maximum (45h), */
146 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
147 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
148 0x95, 0x03, /* Report Count (3), */
149 0x81, 0x02, /* Input (Variable), */
150 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
151 0x09, 0x21, /* Usage (21h), */
152 0x15, 0x00, /* Logical Minimum (0), */
153 0x25, 0xFF, /* Logical Maximum (255), */
154 0x75, 0x08, /* Report Size (8), */
155 0x95, 0x27, /* Report Count (39), */
156 0x81, 0x02, /* Input (Variable), */
157 0x85, 0x05, /* Report ID (5), */
158 0x09, 0x22, /* Usage (22h), */
159 0x95, 0x1F, /* Report Count (31), */
160 0x91, 0x02, /* Output (Variable), */
161 0x85, 0x04, /* Report ID (4), */
162 0x09, 0x23, /* Usage (23h), */
163 0x95, 0x24, /* Report Count (36), */
164 0xB1, 0x02, /* Feature (Variable), */
165 0x85, 0x02, /* Report ID (2), */
166 0x09, 0x24, /* Usage (24h), */
167 0x95, 0x24, /* Report Count (36), */
168 0xB1, 0x02, /* Feature (Variable), */
169 0x85, 0x08, /* Report ID (8), */
170 0x09, 0x25, /* Usage (25h), */
171 0x95, 0x03, /* Report Count (3), */
172 0xB1, 0x02, /* Feature (Variable), */
173 0x85, 0x10, /* Report ID (16), */
174 0x09, 0x26, /* Usage (26h), */
175 0x95, 0x04, /* Report Count (4), */
176 0xB1, 0x02, /* Feature (Variable), */
177 0x85, 0x11, /* Report ID (17), */
178 0x09, 0x27, /* Usage (27h), */
179 0x95, 0x02, /* Report Count (2), */
180 0xB1, 0x02, /* Feature (Variable), */
181 0x85, 0x12, /* Report ID (18), */
182 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
183 0x09, 0x21, /* Usage (21h), */
184 0x95, 0x0F, /* Report Count (15), */
185 0xB1, 0x02, /* Feature (Variable), */
186 0x85, 0x13, /* Report ID (19), */
187 0x09, 0x22, /* Usage (22h), */
188 0x95, 0x16, /* Report Count (22), */
189 0xB1, 0x02, /* Feature (Variable), */
190 0x85, 0x14, /* Report ID (20), */
191 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
192 0x09, 0x20, /* Usage (20h), */
193 0x95, 0x10, /* Report Count (16), */
194 0xB1, 0x02, /* Feature (Variable), */
195 0x85, 0x15, /* Report ID (21), */
196 0x09, 0x21, /* Usage (21h), */
197 0x95, 0x2C, /* Report Count (44), */
198 0xB1, 0x02, /* Feature (Variable), */
199 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
200 0x85, 0x80, /* Report ID (128), */
201 0x09, 0x20, /* Usage (20h), */
202 0x95, 0x06, /* Report Count (6), */
203 0xB1, 0x02, /* Feature (Variable), */
204 0x85, 0x81, /* Report ID (129), */
205 0x09, 0x21, /* Usage (21h), */
206 0x95, 0x06, /* Report Count (6), */
207 0xB1, 0x02, /* Feature (Variable), */
208 0x85, 0x82, /* Report ID (130), */
209 0x09, 0x22, /* Usage (22h), */
210 0x95, 0x05, /* Report Count (5), */
211 0xB1, 0x02, /* Feature (Variable), */
212 0x85, 0x83, /* Report ID (131), */
213 0x09, 0x23, /* Usage (23h), */
214 0x95, 0x01, /* Report Count (1), */
215 0xB1, 0x02, /* Feature (Variable), */
216 0x85, 0x84, /* Report ID (132), */
217 0x09, 0x24, /* Usage (24h), */
218 0x95, 0x04, /* Report Count (4), */
219 0xB1, 0x02, /* Feature (Variable), */
220 0x85, 0x85, /* Report ID (133), */
221 0x09, 0x25, /* Usage (25h), */
222 0x95, 0x06, /* Report Count (6), */
223 0xB1, 0x02, /* Feature (Variable), */
224 0x85, 0x86, /* Report ID (134), */
225 0x09, 0x26, /* Usage (26h), */
226 0x95, 0x06, /* Report Count (6), */
227 0xB1, 0x02, /* Feature (Variable), */
228 0x85, 0x87, /* Report ID (135), */
229 0x09, 0x27, /* Usage (27h), */
230 0x95, 0x23, /* Report Count (35), */
231 0xB1, 0x02, /* Feature (Variable), */
232 0x85, 0x88, /* Report ID (136), */
233 0x09, 0x28, /* Usage (28h), */
234 0x95, 0x22, /* Report Count (34), */
235 0xB1, 0x02, /* Feature (Variable), */
236 0x85, 0x89, /* Report ID (137), */
237 0x09, 0x29, /* Usage (29h), */
238 0x95, 0x02, /* Report Count (2), */
239 0xB1, 0x02, /* Feature (Variable), */
240 0x85, 0x90, /* Report ID (144), */
241 0x09, 0x30, /* Usage (30h), */
242 0x95, 0x05, /* Report Count (5), */
243 0xB1, 0x02, /* Feature (Variable), */
244 0x85, 0x91, /* Report ID (145), */
245 0x09, 0x31, /* Usage (31h), */
246 0x95, 0x03, /* Report Count (3), */
247 0xB1, 0x02, /* Feature (Variable), */
248 0x85, 0x92, /* Report ID (146), */
249 0x09, 0x32, /* Usage (32h), */
250 0x95, 0x03, /* Report Count (3), */
251 0xB1, 0x02, /* Feature (Variable), */
252 0x85, 0x93, /* Report ID (147), */
253 0x09, 0x33, /* Usage (33h), */
254 0x95, 0x0C, /* Report Count (12), */
255 0xB1, 0x02, /* Feature (Variable), */
256 0x85, 0xA0, /* Report ID (160), */
257 0x09, 0x40, /* Usage (40h), */
258 0x95, 0x06, /* Report Count (6), */
259 0xB1, 0x02, /* Feature (Variable), */
260 0x85, 0xA1, /* Report ID (161), */
261 0x09, 0x41, /* Usage (41h), */
262 0x95, 0x01, /* Report Count (1), */
263 0xB1, 0x02, /* Feature (Variable), */
264 0x85, 0xA2, /* Report ID (162), */
265 0x09, 0x42, /* Usage (42h), */
266 0x95, 0x01, /* Report Count (1), */
267 0xB1, 0x02, /* Feature (Variable), */
268 0x85, 0xA3, /* Report ID (163), */
269 0x09, 0x43, /* Usage (43h), */
270 0x95, 0x30, /* Report Count (48), */
271 0xB1, 0x02, /* Feature (Variable), */
272 0x85, 0xA4, /* Report ID (164), */
273 0x09, 0x44, /* Usage (44h), */
274 0x95, 0x0D, /* Report Count (13), */
275 0xB1, 0x02, /* Feature (Variable), */
276 0x85, 0xA5, /* Report ID (165), */
277 0x09, 0x45, /* Usage (45h), */
278 0x95, 0x15, /* Report Count (21), */
279 0xB1, 0x02, /* Feature (Variable), */
280 0x85, 0xA6, /* Report ID (166), */
281 0x09, 0x46, /* Usage (46h), */
282 0x95, 0x15, /* Report Count (21), */
283 0xB1, 0x02, /* Feature (Variable), */
284 0x85, 0xF0, /* Report ID (240), */
285 0x09, 0x47, /* Usage (47h), */
286 0x95, 0x3F, /* Report Count (63), */
287 0xB1, 0x02, /* Feature (Variable), */
288 0x85, 0xF1, /* Report ID (241), */
289 0x09, 0x48, /* Usage (48h), */
290 0x95, 0x3F, /* Report Count (63), */
291 0xB1, 0x02, /* Feature (Variable), */
292 0x85, 0xF2, /* Report ID (242), */
293 0x09, 0x49, /* Usage (49h), */
294 0x95, 0x0F, /* Report Count (15), */
295 0xB1, 0x02, /* Feature (Variable), */
296 0x85, 0xA7, /* Report ID (167), */
297 0x09, 0x4A, /* Usage (4Ah), */
298 0x95, 0x01, /* Report Count (1), */
299 0xB1, 0x02, /* Feature (Variable), */
300 0x85, 0xA8, /* Report ID (168), */
301 0x09, 0x4B, /* Usage (4Bh), */
302 0x95, 0x01, /* Report Count (1), */
303 0xB1, 0x02, /* Feature (Variable), */
304 0x85, 0xA9, /* Report ID (169), */
305 0x09, 0x4C, /* Usage (4Ch), */
306 0x95, 0x08, /* Report Count (8), */
307 0xB1, 0x02, /* Feature (Variable), */
308 0x85, 0xAA, /* Report ID (170), */
309 0x09, 0x4E, /* Usage (4Eh), */
310 0x95, 0x01, /* Report Count (1), */
311 0xB1, 0x02, /* Feature (Variable), */
312 0x85, 0xAB, /* Report ID (171), */
313 0x09, 0x4F, /* Usage (4Fh), */
314 0x95, 0x39, /* Report Count (57), */
315 0xB1, 0x02, /* Feature (Variable), */
316 0x85, 0xAC, /* Report ID (172), */
317 0x09, 0x50, /* Usage (50h), */
318 0x95, 0x39, /* Report Count (57), */
319 0xB1, 0x02, /* Feature (Variable), */
320 0x85, 0xAD, /* Report ID (173), */
321 0x09, 0x51, /* Usage (51h), */
322 0x95, 0x0B, /* Report Count (11), */
323 0xB1, 0x02, /* Feature (Variable), */
324 0x85, 0xAE, /* Report ID (174), */
325 0x09, 0x52, /* Usage (52h), */
326 0x95, 0x01, /* Report Count (1), */
327 0xB1, 0x02, /* Feature (Variable), */
328 0x85, 0xAF, /* Report ID (175), */
329 0x09, 0x53, /* Usage (53h), */
330 0x95, 0x02, /* Report Count (2), */
331 0xB1, 0x02, /* Feature (Variable), */
332 0x85, 0xB0, /* Report ID (176), */
333 0x09, 0x54, /* Usage (54h), */
334 0x95, 0x3F, /* Report Count (63), */
335 0xB1, 0x02, /* Feature (Variable), */
336 0xC0 /* End Collection */
ed19d8cf
FP
337};
338
d829674d
FP
339/* The default behavior of the Dualshock 4 is to send reports using report
340 * type 1 when running over Bluetooth. However, as soon as it receives a
341 * report of type 17 to set the LEDs or rumble it starts returning it's state
342 * in report 17 instead of 1. Since report 17 is undefined in the default HID
343 * descriptor the button and axis definitions must be moved to report 17 or
344 * the HID layer won't process the received input once a report is sent.
345 */
346static u8 dualshock4_bt_rdesc[] = {
347 0x05, 0x01, /* Usage Page (Desktop), */
348 0x09, 0x05, /* Usage (Gamepad), */
349 0xA1, 0x01, /* Collection (Application), */
350 0x85, 0x01, /* Report ID (1), */
351 0x75, 0x08, /* Report Size (8), */
352 0x95, 0x0A, /* Report Count (9), */
353 0x81, 0x02, /* Input (Variable), */
354 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
355 0x85, 0x02, /* Report ID (2), */
356 0x09, 0x24, /* Usage (24h), */
357 0x95, 0x24, /* Report Count (36), */
358 0xB1, 0x02, /* Feature (Variable), */
359 0x85, 0xA3, /* Report ID (163), */
360 0x09, 0x25, /* Usage (25h), */
361 0x95, 0x30, /* Report Count (48), */
362 0xB1, 0x02, /* Feature (Variable), */
363 0x85, 0x05, /* Report ID (5), */
364 0x09, 0x26, /* Usage (26h), */
365 0x95, 0x28, /* Report Count (40), */
366 0xB1, 0x02, /* Feature (Variable), */
367 0x85, 0x06, /* Report ID (6), */
368 0x09, 0x27, /* Usage (27h), */
369 0x95, 0x34, /* Report Count (52), */
370 0xB1, 0x02, /* Feature (Variable), */
371 0x85, 0x07, /* Report ID (7), */
372 0x09, 0x28, /* Usage (28h), */
373 0x95, 0x30, /* Report Count (48), */
374 0xB1, 0x02, /* Feature (Variable), */
375 0x85, 0x08, /* Report ID (8), */
376 0x09, 0x29, /* Usage (29h), */
377 0x95, 0x2F, /* Report Count (47), */
378 0xB1, 0x02, /* Feature (Variable), */
379 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
380 0x85, 0x03, /* Report ID (3), */
381 0x09, 0x21, /* Usage (21h), */
382 0x95, 0x26, /* Report Count (38), */
383 0xB1, 0x02, /* Feature (Variable), */
384 0x85, 0x04, /* Report ID (4), */
385 0x09, 0x22, /* Usage (22h), */
386 0x95, 0x2E, /* Report Count (46), */
387 0xB1, 0x02, /* Feature (Variable), */
388 0x85, 0xF0, /* Report ID (240), */
389 0x09, 0x47, /* Usage (47h), */
390 0x95, 0x3F, /* Report Count (63), */
391 0xB1, 0x02, /* Feature (Variable), */
392 0x85, 0xF1, /* Report ID (241), */
393 0x09, 0x48, /* Usage (48h), */
394 0x95, 0x3F, /* Report Count (63), */
395 0xB1, 0x02, /* Feature (Variable), */
396 0x85, 0xF2, /* Report ID (242), */
397 0x09, 0x49, /* Usage (49h), */
398 0x95, 0x0F, /* Report Count (15), */
399 0xB1, 0x02, /* Feature (Variable), */
400 0x85, 0x11, /* Report ID (17), */
401 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
402 0x09, 0x20, /* Usage (20h), */
403 0x95, 0x02, /* Report Count (2), */
404 0x81, 0x02, /* Input (Variable), */
405 0x05, 0x01, /* Usage Page (Desktop), */
406 0x09, 0x30, /* Usage (X), */
407 0x09, 0x31, /* Usage (Y), */
408 0x09, 0x32, /* Usage (Z), */
409 0x09, 0x35, /* Usage (Rz), */
410 0x15, 0x00, /* Logical Minimum (0), */
411 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
412 0x75, 0x08, /* Report Size (8), */
413 0x95, 0x04, /* Report Count (4), */
414 0x81, 0x02, /* Input (Variable), */
415 0x09, 0x39, /* Usage (Hat Switch), */
416 0x15, 0x00, /* Logical Minimum (0), */
417 0x25, 0x07, /* Logical Maximum (7), */
418 0x75, 0x04, /* Report Size (4), */
419 0x95, 0x01, /* Report Count (1), */
420 0x81, 0x42, /* Input (Variable, Null State), */
421 0x05, 0x09, /* Usage Page (Button), */
422 0x19, 0x01, /* Usage Minimum (01h), */
423 0x29, 0x0E, /* Usage Maximum (0Eh), */
424 0x15, 0x00, /* Logical Minimum (0), */
425 0x25, 0x01, /* Logical Maximum (1), */
426 0x75, 0x01, /* Report Size (1), */
427 0x95, 0x0E, /* Report Count (14), */
428 0x81, 0x02, /* Input (Variable), */
429 0x75, 0x06, /* Report Size (6), */
430 0x95, 0x01, /* Report Count (1), */
431 0x81, 0x01, /* Input (Constant), */
432 0x05, 0x01, /* Usage Page (Desktop), */
433 0x09, 0x33, /* Usage (Rx), */
434 0x09, 0x34, /* Usage (Ry), */
435 0x15, 0x00, /* Logical Minimum (0), */
436 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
437 0x75, 0x08, /* Report Size (8), */
438 0x95, 0x02, /* Report Count (2), */
439 0x81, 0x02, /* Input (Variable), */
440 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
441 0x09, 0x20, /* Usage (20h), */
442 0x95, 0x03, /* Report Count (3), */
443 0x81, 0x02, /* Input (Variable), */
444 0x05, 0x01, /* Usage Page (Desktop), */
445 0x19, 0x40, /* Usage Minimum (40h), */
446 0x29, 0x42, /* Usage Maximum (42h), */
447 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
448 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
449 0x75, 0x10, /* Report Size (16), */
450 0x95, 0x03, /* Report Count (3), */
451 0x81, 0x02, /* Input (Variable), */
452 0x19, 0x43, /* Usage Minimum (43h), */
453 0x29, 0x45, /* Usage Maximum (45h), */
454 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
455 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
456 0x95, 0x03, /* Report Count (3), */
457 0x81, 0x02, /* Input (Variable), */
458 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
459 0x09, 0x20, /* Usage (20h), */
460 0x15, 0x00, /* Logical Minimum (0), */
461 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
462 0x75, 0x08, /* Report Size (8), */
463 0x95, 0x31, /* Report Count (51), */
464 0x81, 0x02, /* Input (Variable), */
465 0x09, 0x21, /* Usage (21h), */
466 0x75, 0x08, /* Report Size (8), */
467 0x95, 0x4D, /* Report Count (77), */
468 0x91, 0x02, /* Output (Variable), */
469 0x85, 0x12, /* Report ID (18), */
470 0x09, 0x22, /* Usage (22h), */
471 0x95, 0x8D, /* Report Count (141), */
472 0x81, 0x02, /* Input (Variable), */
473 0x09, 0x23, /* Usage (23h), */
474 0x91, 0x02, /* Output (Variable), */
475 0x85, 0x13, /* Report ID (19), */
476 0x09, 0x24, /* Usage (24h), */
477 0x95, 0xCD, /* Report Count (205), */
478 0x81, 0x02, /* Input (Variable), */
479 0x09, 0x25, /* Usage (25h), */
480 0x91, 0x02, /* Output (Variable), */
481 0x85, 0x14, /* Report ID (20), */
482 0x09, 0x26, /* Usage (26h), */
483 0x96, 0x0D, 0x01, /* Report Count (269), */
484 0x81, 0x02, /* Input (Variable), */
485 0x09, 0x27, /* Usage (27h), */
486 0x91, 0x02, /* Output (Variable), */
487 0x85, 0x15, /* Report ID (21), */
488 0x09, 0x28, /* Usage (28h), */
489 0x96, 0x4D, 0x01, /* Report Count (333), */
490 0x81, 0x02, /* Input (Variable), */
491 0x09, 0x29, /* Usage (29h), */
492 0x91, 0x02, /* Output (Variable), */
493 0x85, 0x16, /* Report ID (22), */
494 0x09, 0x2A, /* Usage (2Ah), */
495 0x96, 0x8D, 0x01, /* Report Count (397), */
496 0x81, 0x02, /* Input (Variable), */
497 0x09, 0x2B, /* Usage (2Bh), */
498 0x91, 0x02, /* Output (Variable), */
499 0x85, 0x17, /* Report ID (23), */
500 0x09, 0x2C, /* Usage (2Ch), */
501 0x96, 0xCD, 0x01, /* Report Count (461), */
502 0x81, 0x02, /* Input (Variable), */
503 0x09, 0x2D, /* Usage (2Dh), */
504 0x91, 0x02, /* Output (Variable), */
505 0x85, 0x18, /* Report ID (24), */
506 0x09, 0x2E, /* Usage (2Eh), */
507 0x96, 0x0D, 0x02, /* Report Count (525), */
508 0x81, 0x02, /* Input (Variable), */
509 0x09, 0x2F, /* Usage (2Fh), */
510 0x91, 0x02, /* Output (Variable), */
511 0x85, 0x19, /* Report ID (25), */
512 0x09, 0x30, /* Usage (30h), */
513 0x96, 0x22, 0x02, /* Report Count (546), */
514 0x81, 0x02, /* Input (Variable), */
515 0x09, 0x31, /* Usage (31h), */
516 0x91, 0x02, /* Output (Variable), */
517 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
518 0x85, 0x82, /* Report ID (130), */
519 0x09, 0x22, /* Usage (22h), */
520 0x95, 0x3F, /* Report Count (63), */
521 0xB1, 0x02, /* Feature (Variable), */
522 0x85, 0x83, /* Report ID (131), */
523 0x09, 0x23, /* Usage (23h), */
524 0xB1, 0x02, /* Feature (Variable), */
525 0x85, 0x84, /* Report ID (132), */
526 0x09, 0x24, /* Usage (24h), */
527 0xB1, 0x02, /* Feature (Variable), */
528 0x85, 0x90, /* Report ID (144), */
529 0x09, 0x30, /* Usage (30h), */
530 0xB1, 0x02, /* Feature (Variable), */
531 0x85, 0x91, /* Report ID (145), */
532 0x09, 0x31, /* Usage (31h), */
533 0xB1, 0x02, /* Feature (Variable), */
534 0x85, 0x92, /* Report ID (146), */
535 0x09, 0x32, /* Usage (32h), */
536 0xB1, 0x02, /* Feature (Variable), */
537 0x85, 0x93, /* Report ID (147), */
538 0x09, 0x33, /* Usage (33h), */
539 0xB1, 0x02, /* Feature (Variable), */
540 0x85, 0xA0, /* Report ID (160), */
541 0x09, 0x40, /* Usage (40h), */
542 0xB1, 0x02, /* Feature (Variable), */
543 0x85, 0xA4, /* Report ID (164), */
544 0x09, 0x44, /* Usage (44h), */
545 0xB1, 0x02, /* Feature (Variable), */
546 0xC0 /* End Collection */
547};
548
078328da
JK
549static __u8 ps3remote_rdesc[] = {
550 0x05, 0x01, /* GUsagePage Generic Desktop */
551 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
552 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
553
554 /* Use collection 1 for joypad buttons */
555 0xA1, 0x02, /* MCollection Logical (interrelated data) */
556
557 /* Ignore the 1st byte, maybe it is used for a controller
558 * number but it's not needed for correct operation */
559 0x75, 0x08, /* GReportSize 0x08 [8] */
560 0x95, 0x01, /* GReportCount 0x01 [1] */
561 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
562
563 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
564 * buttons multiple keypresses are allowed */
565 0x05, 0x09, /* GUsagePage Button */
566 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
567 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
568 0x14, /* GLogicalMinimum [0] */
569 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
570 0x75, 0x01, /* GReportSize 0x01 [1] */
571 0x95, 0x18, /* GReportCount 0x18 [24] */
572 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
573
574 0xC0, /* MEndCollection */
575
576 /* Use collection 2 for remote control buttons */
577 0xA1, 0x02, /* MCollection Logical (interrelated data) */
578
579 /* 5th byte is used for remote control buttons */
580 0x05, 0x09, /* GUsagePage Button */
581 0x18, /* LUsageMinimum [No button pressed] */
582 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
583 0x14, /* GLogicalMinimum [0] */
584 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
585 0x75, 0x08, /* GReportSize 0x08 [8] */
586 0x95, 0x01, /* GReportCount 0x01 [1] */
587 0x80, /* MInput */
588
589 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
590 * 0xff and 11th is for press indication */
591 0x75, 0x08, /* GReportSize 0x08 [8] */
592 0x95, 0x06, /* GReportCount 0x06 [6] */
593 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
594
595 /* 12th byte is for battery strength */
596 0x05, 0x06, /* GUsagePage Generic Device Controls */
597 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
598 0x14, /* GLogicalMinimum [0] */
599 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
600 0x75, 0x08, /* GReportSize 0x08 [8] */
601 0x95, 0x01, /* GReportCount 0x01 [1] */
602 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
603
604 0xC0, /* MEndCollection */
605
606 0xC0 /* MEndCollection [Game Pad] */
607};
608
609static const unsigned int ps3remote_keymap_joypad_buttons[] = {
610 [0x01] = KEY_SELECT,
611 [0x02] = BTN_THUMBL, /* L3 */
612 [0x03] = BTN_THUMBR, /* R3 */
613 [0x04] = BTN_START,
614 [0x05] = KEY_UP,
615 [0x06] = KEY_RIGHT,
616 [0x07] = KEY_DOWN,
617 [0x08] = KEY_LEFT,
618 [0x09] = BTN_TL2, /* L2 */
619 [0x0a] = BTN_TR2, /* R2 */
620 [0x0b] = BTN_TL, /* L1 */
621 [0x0c] = BTN_TR, /* R1 */
622 [0x0d] = KEY_OPTION, /* options/triangle */
623 [0x0e] = KEY_BACK, /* back/circle */
624 [0x0f] = BTN_0, /* cross */
625 [0x10] = KEY_SCREEN, /* view/square */
626 [0x11] = KEY_HOMEPAGE, /* PS button */
627 [0x14] = KEY_ENTER,
628};
629static const unsigned int ps3remote_keymap_remote_buttons[] = {
630 [0x00] = KEY_1,
631 [0x01] = KEY_2,
632 [0x02] = KEY_3,
633 [0x03] = KEY_4,
634 [0x04] = KEY_5,
635 [0x05] = KEY_6,
636 [0x06] = KEY_7,
637 [0x07] = KEY_8,
638 [0x08] = KEY_9,
639 [0x09] = KEY_0,
640 [0x0e] = KEY_ESC, /* return */
641 [0x0f] = KEY_CLEAR,
642 [0x16] = KEY_EJECTCD,
643 [0x1a] = KEY_MENU, /* top menu */
644 [0x28] = KEY_TIME,
645 [0x30] = KEY_PREVIOUS,
646 [0x31] = KEY_NEXT,
647 [0x32] = KEY_PLAY,
648 [0x33] = KEY_REWIND, /* scan back */
649 [0x34] = KEY_FORWARD, /* scan forward */
650 [0x38] = KEY_STOP,
651 [0x39] = KEY_PAUSE,
652 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
653 [0x60] = KEY_FRAMEBACK, /* slow/step back */
654 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
655 [0x63] = KEY_SUBTITLE,
656 [0x64] = KEY_AUDIO,
657 [0x65] = KEY_ANGLE,
658 [0x70] = KEY_INFO, /* display */
659 [0x80] = KEY_BLUE,
660 [0x81] = KEY_RED,
661 [0x82] = KEY_GREEN,
662 [0x83] = KEY_YELLOW,
663};
664
f04d5140
CL
665static const unsigned int buzz_keymap[] = {
666 /* The controller has 4 remote buzzers, each with one LED and 5
667 * buttons.
668 *
669 * We use the mapping chosen by the controller, which is:
670 *
671 * Key Offset
672 * -------------------
673 * Buzz 1
674 * Blue 5
675 * Orange 4
676 * Green 3
677 * Yellow 2
678 *
679 * So, for example, the orange button on the third buzzer is mapped to
680 * BTN_TRIGGER_HAPPY14
681 */
682 [ 1] = BTN_TRIGGER_HAPPY1,
683 [ 2] = BTN_TRIGGER_HAPPY2,
684 [ 3] = BTN_TRIGGER_HAPPY3,
685 [ 4] = BTN_TRIGGER_HAPPY4,
686 [ 5] = BTN_TRIGGER_HAPPY5,
687 [ 6] = BTN_TRIGGER_HAPPY6,
688 [ 7] = BTN_TRIGGER_HAPPY7,
689 [ 8] = BTN_TRIGGER_HAPPY8,
690 [ 9] = BTN_TRIGGER_HAPPY9,
691 [10] = BTN_TRIGGER_HAPPY10,
692 [11] = BTN_TRIGGER_HAPPY11,
693 [12] = BTN_TRIGGER_HAPPY12,
694 [13] = BTN_TRIGGER_HAPPY13,
695 [14] = BTN_TRIGGER_HAPPY14,
696 [15] = BTN_TRIGGER_HAPPY15,
697 [16] = BTN_TRIGGER_HAPPY16,
698 [17] = BTN_TRIGGER_HAPPY17,
699 [18] = BTN_TRIGGER_HAPPY18,
700 [19] = BTN_TRIGGER_HAPPY19,
701 [20] = BTN_TRIGGER_HAPPY20,
702};
703
d902f472
FP
704static enum power_supply_property sony_battery_props[] = {
705 POWER_SUPPLY_PROP_PRESENT,
706 POWER_SUPPLY_PROP_CAPACITY,
707 POWER_SUPPLY_PROP_SCOPE,
708 POWER_SUPPLY_PROP_STATUS,
709};
710
cc6e0bbb 711struct sony_sc {
d902f472 712 spinlock_t lock;
0a286ef2 713 struct hid_device *hdev;
60781cf4 714 struct led_classdev *leds[MAX_LEDS];
cc6e0bbb 715 unsigned long quirks;
0a286ef2 716 struct work_struct state_worker;
d902f472 717 struct power_supply battery;
f04d5140 718
9f323b68 719#ifdef CONFIG_SONY_FF
9f323b68
SE
720 __u8 left;
721 __u8 right;
722#endif
723
d902f472
FP
724 __u8 cable_state;
725 __u8 battery_charging;
726 __u8 battery_capacity;
60781cf4
FP
727 __u8 led_state[MAX_LEDS];
728 __u8 led_count;
cc6e0bbb
JK
729};
730
078328da
JK
731static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
732 unsigned int *rsize)
733{
734 *rsize = sizeof(ps3remote_rdesc);
735 return ps3remote_rdesc;
736}
737
738static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
739 struct hid_field *field, struct hid_usage *usage,
740 unsigned long **bit, int *max)
741{
742 unsigned int key = usage->hid & HID_USAGE;
743
744 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
745 return -1;
746
747 switch (usage->collection_index) {
748 case 1:
749 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
750 return -1;
751
752 key = ps3remote_keymap_joypad_buttons[key];
753 if (!key)
754 return -1;
755 break;
756 case 2:
757 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
758 return -1;
759
760 key = ps3remote_keymap_remote_buttons[key];
761 if (!key)
762 return -1;
763 break;
764 default:
765 return -1;
766 }
767
768 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
769 return 1;
770}
771
772
cc6e0bbb 773/* Sony Vaio VGX has wrongly mouse pointer declared as constant */
73e4008d
NK
774static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
775 unsigned int *rsize)
cc6e0bbb
JK
776{
777 struct sony_sc *sc = hid_get_drvdata(hdev);
778
99d24902
FLVC
779 /*
780 * Some Sony RF receivers wrongly declare the mouse pointer as a
781 * a constant non-data variable.
782 */
783 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
784 /* usage page: generic desktop controls */
785 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
786 /* usage: mouse */
787 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
788 /* input (usage page for x,y axes): constant, variable, relative */
789 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
a4649184 790 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
99d24902 791 /* input: data, variable, relative */
cc6e0bbb
JK
792 rdesc[55] = 0x06;
793 }
61ab44be 794
ed19d8cf
FP
795 /*
796 * The default Dualshock 4 USB descriptor doesn't assign
797 * the gyroscope values to corresponding axes so we need a
798 * modified one.
799 */
800 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
801 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
802 rdesc = dualshock4_usb_rdesc;
803 *rsize = sizeof(dualshock4_usb_rdesc);
d829674d
FP
804 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
805 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
806 rdesc = dualshock4_bt_rdesc;
807 *rsize = sizeof(dualshock4_bt_rdesc);
ed19d8cf
FP
808 }
809
61ab44be
SW
810 /* The HID descriptor exposed over BT has a trailing zero byte */
811 if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
812 ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
813 rdesc[83] == 0x75) {
814 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
815 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
816 sizeof(sixaxis_rdesc_fixup));
e57a67da
MCC
817 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
818 *rsize > sizeof(sixaxis_rdesc_fixup2)) {
819 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
820 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
821 *rsize = sizeof(sixaxis_rdesc_fixup2);
822 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
61ab44be 823 }
078328da
JK
824
825 if (sc->quirks & PS3REMOTE)
826 return ps3remote_fixup(hdev, rdesc, rsize);
827
73e4008d 828 return rdesc;
cc6e0bbb
JK
829}
830
d902f472
FP
831static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
832{
833 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
834 unsigned long flags;
835 __u8 cable_state, battery_capacity, battery_charging;
836
837 /* The sixaxis is charging if the battery value is 0xee
838 * and it is fully charged if the value is 0xef.
839 * It does not report the actual level while charging so it
840 * is set to 100% while charging is in progress.
841 */
842 if (rd[30] >= 0xee) {
843 battery_capacity = 100;
844 battery_charging = rd[30] & 0x01;
845 } else {
846 battery_capacity = sixaxis_battery_capacity[rd[30]];
847 battery_charging = 0;
848 }
849 cable_state = (rd[31] >> 4) & 0x01;
850
851 spin_lock_irqsave(&sc->lock, flags);
852 sc->cable_state = cable_state;
853 sc->battery_capacity = battery_capacity;
854 sc->battery_charging = battery_charging;
855 spin_unlock_irqrestore(&sc->lock, flags);
856}
857
858static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
859{
e5606230
FP
860 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
861 struct hid_input, list);
862 struct input_dev *input_dev = hidinput->input;
d902f472 863 unsigned long flags;
6c5f860d 864 int n, offset;
d902f472
FP
865 __u8 cable_state, battery_capacity, battery_charging;
866
6c5f860d
FP
867 /* Battery and touchpad data starts at byte 30 in the USB report and
868 * 32 in Bluetooth report.
869 */
870 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
871
d902f472
FP
872 /* The lower 4 bits of byte 30 contain the battery level
873 * and the 5th bit contains the USB cable state.
874 */
6c5f860d
FP
875 cable_state = (rd[offset] >> 4) & 0x01;
876 battery_capacity = rd[offset] & 0x0F;
d902f472 877
6c5f860d
FP
878 /* When a USB power source is connected the battery level ranges from
879 * 0 to 10, and when running on battery power it ranges from 0 to 9.
880 * A battery level above 10 when plugged in means charge completed.
d902f472 881 */
6c5f860d 882 if (!cable_state || battery_capacity > 10)
d902f472
FP
883 battery_charging = 0;
884 else
885 battery_charging = 1;
886
6c5f860d
FP
887 if (!cable_state)
888 battery_capacity++;
d902f472 889 if (battery_capacity > 10)
6c5f860d
FP
890 battery_capacity = 10;
891
d902f472
FP
892 battery_capacity *= 10;
893
894 spin_lock_irqsave(&sc->lock, flags);
895 sc->cable_state = cable_state;
896 sc->battery_capacity = battery_capacity;
897 sc->battery_charging = battery_charging;
898 spin_unlock_irqrestore(&sc->lock, flags);
e5606230 899
6c5f860d
FP
900 offset += 5;
901
902 /* The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
903 * and 37 on Bluetooth.
e5606230
FP
904 * The first 7 bits of the first byte is a counter and bit 8 is a touch
905 * indicator that is 0 when pressed and 1 when not pressed.
906 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
907 * The data for the second touch is in the same format and immediatly
908 * follows the data for the first.
909 */
910 for (n = 0; n < 2; n++) {
911 __u16 x, y;
912
913 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
914 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
915
916 input_mt_slot(input_dev, n);
917 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
918 !(rd[offset] >> 7));
919 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
920 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
921
922 offset += 4;
923 }
d902f472
FP
924}
925
c9e4d877
SW
926static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
927 __u8 *rd, int size)
928{
929 struct sony_sc *sc = hid_get_drvdata(hdev);
930
931 /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
932 * has to be BYTE_SWAPPED before passing up to joystick interface
933 */
934 if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
935 rd[0] == 0x01 && size == 49) {
936 swap(rd[41], rd[42]);
937 swap(rd[43], rd[44]);
938 swap(rd[45], rd[46]);
939 swap(rd[47], rd[48]);
d902f472
FP
940
941 sixaxis_parse_report(sc, rd, size);
942 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
943 size == 64) {
944 dualshock4_parse_report(sc, rd, size);
c9e4d877
SW
945 }
946
947 return 0;
948}
949
f04d5140
CL
950static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
951 struct hid_field *field, struct hid_usage *usage,
952 unsigned long **bit, int *max)
953{
954 struct sony_sc *sc = hid_get_drvdata(hdev);
955
956 if (sc->quirks & BUZZ_CONTROLLER) {
957 unsigned int key = usage->hid & HID_USAGE;
958
959 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
960 return -1;
961
962 switch (usage->collection_index) {
963 case 1:
964 if (key >= ARRAY_SIZE(buzz_keymap))
965 return -1;
966
967 key = buzz_keymap[key];
968 if (!key)
969 return -1;
970 break;
971 default:
972 return -1;
973 }
974
975 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
976 return 1;
977 }
978
078328da
JK
979 if (sc->quirks & PS3REMOTE)
980 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
981
6f498018
BT
982 /* Let hid-core decide for the others */
983 return 0;
f04d5140
CL
984}
985
5710fabf
AO
986/*
987 * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
988 * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
989 * so we need to override that forcing HID Output Reports on the Control EP.
990 *
991 * There is also another issue about HID Output Reports via USB, the Sixaxis
992 * does not want the report_id as part of the data packet, so we have to
993 * discard buf[0] when sending the actual control message, even for numbered
994 * reports, humpf!
995 */
569b10a5
AO
996static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
997 size_t count, unsigned char report_type)
998{
999 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1000 struct usb_device *dev = interface_to_usbdev(intf);
1001 struct usb_host_interface *interface = intf->cur_altsetting;
1002 int report_id = buf[0];
1003 int ret;
1004
5710fabf
AO
1005 if (report_type == HID_OUTPUT_REPORT) {
1006 /* Don't send the Report ID */
1007 buf++;
1008 count--;
1009 }
1010
569b10a5
AO
1011 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1012 HID_REQ_SET_REPORT,
1013 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1014 ((report_type + 1) << 8) | report_id,
1015 interface->desc.bInterfaceNumber, buf, count,
1016 USB_CTRL_SET_TIMEOUT);
1017
5710fabf
AO
1018 /* Count also the Report ID, in case of an Output report. */
1019 if (ret > 0 && report_type == HID_OUTPUT_REPORT)
1020 ret++;
1021
569b10a5
AO
1022 return ret;
1023}
1024
bd28ce00
JS
1025/*
1026 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1027 * to "operational". Without this, the ps3 controller will not report any
1028 * events.
1029 */
816651a7 1030static int sixaxis_set_operational_usb(struct hid_device *hdev)
bd28ce00 1031{
bd28ce00
JS
1032 int ret;
1033 char *buf = kmalloc(18, GFP_KERNEL);
1034
1035 if (!buf)
1036 return -ENOMEM;
1037
cafebc05
BT
1038 ret = hid_hw_raw_request(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT,
1039 HID_REQ_GET_REPORT);
f204828a 1040
bd28ce00 1041 if (ret < 0)
4291ee30 1042 hid_err(hdev, "can't set operational mode\n");
bd28ce00
JS
1043
1044 kfree(buf);
1045
1046 return ret;
1047}
1048
816651a7 1049static int sixaxis_set_operational_bt(struct hid_device *hdev)
f9ce7c28 1050{
fddb33f2 1051 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
7e845d46
BT
1052 return hid_output_raw_report(hdev, buf, sizeof(buf),
1053 HID_FEATURE_REPORT);
f9ce7c28
BN
1054}
1055
60781cf4 1056static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
f04d5140
CL
1057{
1058 struct list_head *report_list =
1059 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1060 struct hid_report *report = list_entry(report_list->next,
1061 struct hid_report, list);
1062 __s32 *value = report->field[0]->value;
1063
1064 value[0] = 0x00;
60781cf4
FP
1065 value[1] = leds[0] ? 0xff : 0x00;
1066 value[2] = leds[1] ? 0xff : 0x00;
1067 value[3] = leds[2] ? 0xff : 0x00;
1068 value[4] = leds[3] ? 0xff : 0x00;
f04d5140
CL
1069 value[5] = 0x00;
1070 value[6] = 0x00;
1071 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1072}
1073
60781cf4 1074static void sony_set_leds(struct hid_device *hdev, const __u8 *leds, int count)
0a286ef2
SE
1075{
1076 struct sony_sc *drv_data = hid_get_drvdata(hdev);
60781cf4 1077 int n;
0a286ef2 1078
60781cf4
FP
1079 BUG_ON(count > MAX_LEDS);
1080
1081 if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) {
0a286ef2 1082 buzz_set_leds(hdev, leds);
60781cf4 1083 } else if ((drv_data->quirks & SIXAXIS_CONTROLLER_USB) ||
8ab1676b 1084 (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB)) {
60781cf4
FP
1085 for (n = 0; n < count; n++)
1086 drv_data->led_state[n] = leds[n];
0a286ef2
SE
1087 schedule_work(&drv_data->state_worker);
1088 }
1089}
1090
c5382519 1091static void sony_led_set_brightness(struct led_classdev *led,
f04d5140
CL
1092 enum led_brightness value)
1093{
1094 struct device *dev = led->dev->parent;
1095 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1096 struct sony_sc *drv_data;
f04d5140
CL
1097
1098 int n;
1099
1100 drv_data = hid_get_drvdata(hdev);
2251b85f 1101 if (!drv_data) {
f04d5140
CL
1102 hid_err(hdev, "No device data\n");
1103 return;
1104 }
f04d5140 1105
60781cf4 1106 for (n = 0; n < drv_data->led_count; n++) {
2251b85f 1107 if (led == drv_data->leds[n]) {
60781cf4
FP
1108 if (value != drv_data->led_state[n]) {
1109 drv_data->led_state[n] = value;
1110 sony_set_leds(hdev, drv_data->led_state, drv_data->led_count);
f04d5140
CL
1111 }
1112 break;
1113 }
1114 }
1115}
1116
c5382519 1117static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
f04d5140
CL
1118{
1119 struct device *dev = led->dev->parent;
1120 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1121 struct sony_sc *drv_data;
f04d5140
CL
1122
1123 int n;
f04d5140
CL
1124
1125 drv_data = hid_get_drvdata(hdev);
2251b85f 1126 if (!drv_data) {
f04d5140
CL
1127 hid_err(hdev, "No device data\n");
1128 return LED_OFF;
1129 }
f04d5140 1130
60781cf4 1131 for (n = 0; n < drv_data->led_count; n++) {
7db7504a
SW
1132 if (led == drv_data->leds[n])
1133 return drv_data->led_state[n];
f04d5140
CL
1134 }
1135
7db7504a 1136 return LED_OFF;
f04d5140 1137}
f04d5140 1138
0a286ef2
SE
1139static void sony_leds_remove(struct hid_device *hdev)
1140{
1141 struct sony_sc *drv_data;
1142 struct led_classdev *led;
1143 int n;
1144
1145 drv_data = hid_get_drvdata(hdev);
1146 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
1147
60781cf4 1148 for (n = 0; n < drv_data->led_count; n++) {
0a286ef2
SE
1149 led = drv_data->leds[n];
1150 drv_data->leds[n] = NULL;
1151 if (!led)
1152 continue;
1153 led_classdev_unregister(led);
1154 kfree(led);
1155 }
60781cf4
FP
1156
1157 drv_data->led_count = 0;
0a286ef2
SE
1158}
1159
c5382519 1160static int sony_leds_init(struct hid_device *hdev)
f04d5140
CL
1161{
1162 struct sony_sc *drv_data;
40e32ee6 1163 int n, ret = 0;
60781cf4 1164 int max_brightness;
61ebca93 1165 int use_colors;
40e32ee6
JK
1166 struct led_classdev *led;
1167 size_t name_sz;
1168 char *name;
0a286ef2
SE
1169 size_t name_len;
1170 const char *name_fmt;
61ebca93 1171 static const char * const color_str[] = { "red", "green", "blue" };
60781cf4 1172 static const __u8 initial_values[MAX_LEDS] = { 0x00, 0x00, 0x00, 0x00 };
f04d5140
CL
1173
1174 drv_data = hid_get_drvdata(hdev);
0a286ef2
SE
1175 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
1176
1177 if (drv_data->quirks & BUZZ_CONTROLLER) {
61ebca93
FP
1178 drv_data->led_count = 4;
1179 max_brightness = 1;
1180 use_colors = 0;
0a286ef2
SE
1181 name_len = strlen("::buzz#");
1182 name_fmt = "%s::buzz%d";
1183 /* Validate expected report characteristics. */
1184 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1185 return -ENODEV;
61ebca93 1186 } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB) {
60781cf4
FP
1187 drv_data->led_count = 3;
1188 max_brightness = 255;
61ebca93
FP
1189 use_colors = 1;
1190 name_len = 0;
1191 name_fmt = "%s:%s";
60781cf4
FP
1192 } else {
1193 drv_data->led_count = 4;
1194 max_brightness = 1;
61ebca93
FP
1195 use_colors = 0;
1196 name_len = strlen("::sony#");
1197 name_fmt = "%s::sony%d";
60781cf4
FP
1198 }
1199
f04d5140
CL
1200 /* Clear LEDs as we have no way of reading their initial state. This is
1201 * only relevant if the driver is loaded after somebody actively set the
1202 * LEDs to on */
60781cf4 1203 sony_set_leds(hdev, initial_values, drv_data->led_count);
f04d5140 1204
0a286ef2 1205 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
f04d5140 1206
60781cf4 1207 for (n = 0; n < drv_data->led_count; n++) {
61ebca93
FP
1208
1209 if (use_colors)
1210 name_sz = strlen(dev_name(&hdev->dev)) + strlen(color_str[n]) + 2;
1211
40e32ee6
JK
1212 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1213 if (!led) {
1214 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
8cd5fcda 1215 ret = -ENOMEM;
40e32ee6
JK
1216 goto error_leds;
1217 }
f04d5140 1218
40e32ee6 1219 name = (void *)(&led[1]);
61ebca93
FP
1220 if (use_colors)
1221 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), color_str[n]);
1222 else
1223 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
40e32ee6
JK
1224 led->name = name;
1225 led->brightness = 0;
60781cf4 1226 led->max_brightness = max_brightness;
c5382519
SE
1227 led->brightness_get = sony_led_get_brightness;
1228 led->brightness_set = sony_led_set_brightness;
f04d5140 1229
8cd5fcda
JL
1230 ret = led_classdev_register(&hdev->dev, led);
1231 if (ret) {
40e32ee6
JK
1232 hid_err(hdev, "Failed to register LED %d\n", n);
1233 kfree(led);
1234 goto error_leds;
f04d5140 1235 }
40e32ee6 1236
2251b85f 1237 drv_data->leds[n] = led;
f04d5140 1238 }
f04d5140
CL
1239
1240 return ret;
1241
f04d5140 1242error_leds:
0a286ef2 1243 sony_leds_remove(hdev);
f04d5140 1244
f04d5140 1245 return ret;
f04d5140
CL
1246}
1247
cad665a2 1248static void sixaxis_state_worker(struct work_struct *work)
a08c22c0 1249{
92b5c411 1250 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
a08c22c0
SE
1251 unsigned char buf[] = {
1252 0x01,
1253 0x00, 0xff, 0x00, 0xff, 0x00,
0a286ef2 1254 0x00, 0x00, 0x00, 0x00, 0x00,
a08c22c0
SE
1255 0xff, 0x27, 0x10, 0x00, 0x32,
1256 0xff, 0x27, 0x10, 0x00, 0x32,
1257 0xff, 0x27, 0x10, 0x00, 0x32,
1258 0xff, 0x27, 0x10, 0x00, 0x32,
1259 0x00, 0x00, 0x00, 0x00, 0x00
1260 };
9f323b68 1261
0a286ef2 1262#ifdef CONFIG_SONY_FF
0bd88dd3 1263 buf[3] = sc->right ? 1 : 0;
9f323b68 1264 buf[5] = sc->left;
0a286ef2
SE
1265#endif
1266
60781cf4
FP
1267 buf[10] |= sc->led_state[0] << 1;
1268 buf[10] |= sc->led_state[1] << 2;
1269 buf[10] |= sc->led_state[2] << 3;
1270 buf[10] |= sc->led_state[3] << 4;
9f323b68 1271
7e845d46 1272 hid_output_raw_report(sc->hdev, buf, sizeof(buf), HID_OUTPUT_REPORT);
9f323b68
SE
1273}
1274
0bd88dd3
FP
1275static void dualshock4_state_worker(struct work_struct *work)
1276{
1277 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
0da8ea65 1278 struct hid_device *hdev = sc->hdev;
48220237
FP
1279 int offset;
1280
fdcf105d 1281 __u8 buf[78] = { 0 };
0da8ea65 1282
fdcf105d
FP
1283 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1284 buf[0] = 0x05;
1285 buf[1] = 0x03;
1286 offset = 4;
1287 } else {
1288 buf[0] = 0x11;
1289 buf[1] = 0xB0;
1290 buf[3] = 0x0F;
1291 offset = 6;
1292 }
0bd88dd3
FP
1293
1294#ifdef CONFIG_SONY_FF
48220237
FP
1295 buf[offset++] = sc->right;
1296 buf[offset++] = sc->left;
1297#else
1298 offset += 2;
0bd88dd3
FP
1299#endif
1300
48220237
FP
1301 buf[offset++] = sc->led_state[0];
1302 buf[offset++] = sc->led_state[1];
1303 buf[offset++] = sc->led_state[2];
60781cf4 1304
fdcf105d
FP
1305 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1306 hid_hw_output_report(hdev, buf, 32);
1307 else
1308 hid_hw_raw_request(hdev, 0x11, buf, 78,
1309 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
0bd88dd3
FP
1310}
1311
0a286ef2 1312#ifdef CONFIG_SONY_FF
9f323b68
SE
1313static int sony_play_effect(struct input_dev *dev, void *data,
1314 struct ff_effect *effect)
1315{
a08c22c0 1316 struct hid_device *hid = input_get_drvdata(dev);
9f323b68 1317 struct sony_sc *sc = hid_get_drvdata(hid);
a08c22c0
SE
1318
1319 if (effect->type != FF_RUMBLE)
1320 return 0;
1321
9f323b68 1322 sc->left = effect->u.rumble.strong_magnitude / 256;
0bd88dd3 1323 sc->right = effect->u.rumble.weak_magnitude / 256;
a08c22c0 1324
92b5c411 1325 schedule_work(&sc->state_worker);
9f323b68 1326 return 0;
a08c22c0
SE
1327}
1328
1329static int sony_init_ff(struct hid_device *hdev)
1330{
1331 struct hid_input *hidinput = list_entry(hdev->inputs.next,
1332 struct hid_input, list);
1333 struct input_dev *input_dev = hidinput->input;
1334
1335 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1336 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1337}
1338
9f323b68
SE
1339static void sony_destroy_ff(struct hid_device *hdev)
1340{
1341 struct sony_sc *sc = hid_get_drvdata(hdev);
1342
92b5c411 1343 cancel_work_sync(&sc->state_worker);
9f323b68
SE
1344}
1345
a08c22c0
SE
1346#else
1347static int sony_init_ff(struct hid_device *hdev)
1348{
1349 return 0;
1350}
9f323b68
SE
1351
1352static void sony_destroy_ff(struct hid_device *hdev)
1353{
1354}
a08c22c0
SE
1355#endif
1356
d902f472
FP
1357static int sony_battery_get_property(struct power_supply *psy,
1358 enum power_supply_property psp,
1359 union power_supply_propval *val)
1360{
1361 struct sony_sc *sc = container_of(psy, struct sony_sc, battery);
1362 unsigned long flags;
1363 int ret = 0;
1364 u8 battery_charging, battery_capacity, cable_state;
1365
1366 spin_lock_irqsave(&sc->lock, flags);
1367 battery_charging = sc->battery_charging;
1368 battery_capacity = sc->battery_capacity;
1369 cable_state = sc->cable_state;
1370 spin_unlock_irqrestore(&sc->lock, flags);
1371
1372 switch (psp) {
1373 case POWER_SUPPLY_PROP_PRESENT:
1374 val->intval = 1;
1375 break;
1376 case POWER_SUPPLY_PROP_SCOPE:
1377 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1378 break;
1379 case POWER_SUPPLY_PROP_CAPACITY:
1380 val->intval = battery_capacity;
1381 break;
1382 case POWER_SUPPLY_PROP_STATUS:
1383 if (battery_charging)
1384 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1385 else
1386 if (battery_capacity == 100 && cable_state)
1387 val->intval = POWER_SUPPLY_STATUS_FULL;
1388 else
1389 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1390 break;
1391 default:
1392 ret = -EINVAL;
1393 break;
1394 }
1395 return ret;
1396}
1397
1398static int sony_battery_probe(struct sony_sc *sc)
1399{
1400 static atomic_t power_id_seq = ATOMIC_INIT(0);
1401 unsigned long power_id;
1402 struct hid_device *hdev = sc->hdev;
1403 int ret;
1404
d9a293a9
FP
1405 /* Set the default battery level to 100% to avoid low battery warnings
1406 * if the battery is polled before the first device report is received.
1407 */
1408 sc->battery_capacity = 100;
1409
d902f472
FP
1410 power_id = (unsigned long)atomic_inc_return(&power_id_seq);
1411
1412 sc->battery.properties = sony_battery_props;
1413 sc->battery.num_properties = ARRAY_SIZE(sony_battery_props);
1414 sc->battery.get_property = sony_battery_get_property;
1415 sc->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1416 sc->battery.use_for_apm = 0;
1417 sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%lu",
1418 power_id);
1419 if (!sc->battery.name)
1420 return -ENOMEM;
1421
1422 ret = power_supply_register(&hdev->dev, &sc->battery);
1423 if (ret) {
1424 hid_err(hdev, "Unable to register battery device\n");
1425 goto err_free;
1426 }
1427
1428 power_supply_powers(&sc->battery, &hdev->dev);
1429 return 0;
1430
1431err_free:
1432 kfree(sc->battery.name);
1433 sc->battery.name = NULL;
1434 return ret;
1435}
1436
1437static void sony_battery_remove(struct sony_sc *sc)
1438{
1439 if (!sc->battery.name)
1440 return;
1441
1442 power_supply_unregister(&sc->battery);
1443 kfree(sc->battery.name);
1444 sc->battery.name = NULL;
1445}
1446
e5606230
FP
1447static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
1448 int w, int h)
1449{
1450 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1451 struct hid_input, list);
1452 struct input_dev *input_dev = hidinput->input;
1453 int ret;
1454
1455 ret = input_mt_init_slots(input_dev, touch_count, 0);
1456 if (ret < 0) {
1457 hid_err(sc->hdev, "Unable to initialize multi-touch slots\n");
1458 return ret;
1459 }
1460
1461 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1462 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1463
1464 return 0;
1465}
1466
bd28ce00
JS
1467static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1468{
1469 int ret;
cc6e0bbb
JK
1470 unsigned long quirks = id->driver_data;
1471 struct sony_sc *sc;
f04d5140 1472 unsigned int connect_mask = HID_CONNECT_DEFAULT;
cc6e0bbb 1473
abf832bf 1474 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
cc6e0bbb 1475 if (sc == NULL) {
4291ee30 1476 hid_err(hdev, "can't alloc sony descriptor\n");
cc6e0bbb
JK
1477 return -ENOMEM;
1478 }
1479
1480 sc->quirks = quirks;
1481 hid_set_drvdata(hdev, sc);
0a286ef2 1482 sc->hdev = hdev;
bd28ce00 1483
bd28ce00
JS
1484 ret = hid_parse(hdev);
1485 if (ret) {
4291ee30 1486 hid_err(hdev, "parse failed\n");
abf832bf 1487 return ret;
bd28ce00
JS
1488 }
1489
f04d5140
CL
1490 if (sc->quirks & VAIO_RDESC_CONSTANT)
1491 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1492 else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
1493 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1494 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1495 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1496
1497 ret = hid_hw_start(hdev, connect_mask);
bd28ce00 1498 if (ret) {
4291ee30 1499 hid_err(hdev, "hw start failed\n");
abf832bf 1500 return ret;
bd28ce00
JS
1501 }
1502
569b10a5
AO
1503 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1504 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
816651a7 1505 ret = sixaxis_set_operational_usb(hdev);
cad665a2 1506 INIT_WORK(&sc->state_worker, sixaxis_state_worker);
569b10a5 1507 }
816651a7
AO
1508 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1509 ret = sixaxis_set_operational_bt(hdev);
8ab1676b 1510 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
e5606230
FP
1511 /* The Dualshock 4 touchpad supports 2 touches and has a
1512 * resolution of 1920x940.
1513 */
1514 ret = sony_register_touchpad(sc, 2, 1920, 940);
1515 if (ret < 0)
1516 goto err_stop;
1517
0bd88dd3
FP
1518 INIT_WORK(&sc->state_worker, dualshock4_state_worker);
1519 } else {
1520 ret = 0;
1521 }
f9ce7c28 1522
4dfdc464 1523 if (ret < 0)
bd28ce00
JS
1524 goto err_stop;
1525
0a286ef2
SE
1526 if (sc->quirks & SONY_LED_SUPPORT) {
1527 ret = sony_leds_init(hdev);
1528 if (ret < 0)
1529 goto err_stop;
1530 }
1531
d902f472
FP
1532 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1533 ret = sony_battery_probe(sc);
1534 if (ret < 0)
1535 goto err_stop;
1536
1537 /* Open the device to receive reports with battery info */
1538 ret = hid_hw_open(hdev);
1539 if (ret < 0) {
1540 hid_err(hdev, "hw open failed\n");
1541 goto err_stop;
1542 }
1543 }
1544
a08c22c0
SE
1545 ret = sony_init_ff(hdev);
1546 if (ret < 0)
d902f472 1547 goto err_close;
a08c22c0 1548
bd28ce00 1549 return 0;
d902f472
FP
1550err_close:
1551 hid_hw_close(hdev);
bd28ce00 1552err_stop:
0a286ef2
SE
1553 if (sc->quirks & SONY_LED_SUPPORT)
1554 sony_leds_remove(hdev);
d902f472
FP
1555 if (sc->quirks & SONY_BATTERY_SUPPORT)
1556 sony_battery_remove(sc);
bd28ce00 1557 hid_hw_stop(hdev);
bd28ce00
JS
1558 return ret;
1559}
1560
cc6e0bbb
JK
1561static void sony_remove(struct hid_device *hdev)
1562{
f04d5140
CL
1563 struct sony_sc *sc = hid_get_drvdata(hdev);
1564
0a286ef2 1565 if (sc->quirks & SONY_LED_SUPPORT)
c5382519 1566 sony_leds_remove(hdev);
f04d5140 1567
d902f472
FP
1568 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1569 hid_hw_close(hdev);
1570 sony_battery_remove(sc);
1571 }
1572
9f323b68
SE
1573 sony_destroy_ff(hdev);
1574
cc6e0bbb 1575 hid_hw_stop(hdev);
cc6e0bbb
JK
1576}
1577
bd28ce00 1578static const struct hid_device_id sony_devices[] = {
816651a7
AO
1579 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1580 .driver_data = SIXAXIS_CONTROLLER_USB },
35dca5b4
JK
1581 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
1582 .driver_data = SIXAXIS_CONTROLLER_USB },
816651a7
AO
1583 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1584 .driver_data = SIXAXIS_CONTROLLER_BT },
cc6e0bbb
JK
1585 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
1586 .driver_data = VAIO_RDESC_CONSTANT },
a4649184
FLVC
1587 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
1588 .driver_data = VAIO_RDESC_CONSTANT },
f04d5140
CL
1589 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
1590 * Logitech joystick from the device descriptor. */
1591 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
1592 .driver_data = BUZZ_CONTROLLER },
1593 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
1594 .driver_data = BUZZ_CONTROLLER },
078328da
JK
1595 /* PS3 BD Remote Control */
1596 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
1597 .driver_data = PS3REMOTE },
1598 /* Logitech Harmony Adapter for PS3 */
1599 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
1600 .driver_data = PS3REMOTE },
0bd88dd3
FP
1601 /* Sony Dualshock 4 controllers for PS4 */
1602 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
8ab1676b 1603 .driver_data = DUALSHOCK4_CONTROLLER_USB },
0bd88dd3 1604 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
8ab1676b 1605 .driver_data = DUALSHOCK4_CONTROLLER_BT },
bd28ce00
JS
1606 { }
1607};
1608MODULE_DEVICE_TABLE(hid, sony_devices);
1609
1610static struct hid_driver sony_driver = {
f04d5140
CL
1611 .name = "sony",
1612 .id_table = sony_devices,
1613 .input_mapping = sony_mapping,
1614 .probe = sony_probe,
1615 .remove = sony_remove,
1616 .report_fixup = sony_report_fixup,
1617 .raw_event = sony_raw_event
bd28ce00 1618};
f425458e 1619module_hid_driver(sony_driver);
bd28ce00 1620
bd28ce00 1621MODULE_LICENSE("GPL");