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