]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hid/hid-sony.c
HID: Kconfig updates for the Sony module
[mirror_ubuntu-bionic-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>
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
078328da
JK
339static __u8 ps3remote_rdesc[] = {
340 0x05, 0x01, /* GUsagePage Generic Desktop */
341 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
342 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
343
344 /* Use collection 1 for joypad buttons */
345 0xA1, 0x02, /* MCollection Logical (interrelated data) */
346
347 /* Ignore the 1st byte, maybe it is used for a controller
348 * number but it's not needed for correct operation */
349 0x75, 0x08, /* GReportSize 0x08 [8] */
350 0x95, 0x01, /* GReportCount 0x01 [1] */
351 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
352
353 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
354 * buttons multiple keypresses are allowed */
355 0x05, 0x09, /* GUsagePage Button */
356 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
357 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
358 0x14, /* GLogicalMinimum [0] */
359 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
360 0x75, 0x01, /* GReportSize 0x01 [1] */
361 0x95, 0x18, /* GReportCount 0x18 [24] */
362 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
363
364 0xC0, /* MEndCollection */
365
366 /* Use collection 2 for remote control buttons */
367 0xA1, 0x02, /* MCollection Logical (interrelated data) */
368
369 /* 5th byte is used for remote control buttons */
370 0x05, 0x09, /* GUsagePage Button */
371 0x18, /* LUsageMinimum [No button pressed] */
372 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
373 0x14, /* GLogicalMinimum [0] */
374 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
375 0x75, 0x08, /* GReportSize 0x08 [8] */
376 0x95, 0x01, /* GReportCount 0x01 [1] */
377 0x80, /* MInput */
378
379 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
380 * 0xff and 11th is for press indication */
381 0x75, 0x08, /* GReportSize 0x08 [8] */
382 0x95, 0x06, /* GReportCount 0x06 [6] */
383 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
384
385 /* 12th byte is for battery strength */
386 0x05, 0x06, /* GUsagePage Generic Device Controls */
387 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
388 0x14, /* GLogicalMinimum [0] */
389 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
390 0x75, 0x08, /* GReportSize 0x08 [8] */
391 0x95, 0x01, /* GReportCount 0x01 [1] */
392 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
393
394 0xC0, /* MEndCollection */
395
396 0xC0 /* MEndCollection [Game Pad] */
397};
398
399static const unsigned int ps3remote_keymap_joypad_buttons[] = {
400 [0x01] = KEY_SELECT,
401 [0x02] = BTN_THUMBL, /* L3 */
402 [0x03] = BTN_THUMBR, /* R3 */
403 [0x04] = BTN_START,
404 [0x05] = KEY_UP,
405 [0x06] = KEY_RIGHT,
406 [0x07] = KEY_DOWN,
407 [0x08] = KEY_LEFT,
408 [0x09] = BTN_TL2, /* L2 */
409 [0x0a] = BTN_TR2, /* R2 */
410 [0x0b] = BTN_TL, /* L1 */
411 [0x0c] = BTN_TR, /* R1 */
412 [0x0d] = KEY_OPTION, /* options/triangle */
413 [0x0e] = KEY_BACK, /* back/circle */
414 [0x0f] = BTN_0, /* cross */
415 [0x10] = KEY_SCREEN, /* view/square */
416 [0x11] = KEY_HOMEPAGE, /* PS button */
417 [0x14] = KEY_ENTER,
418};
419static const unsigned int ps3remote_keymap_remote_buttons[] = {
420 [0x00] = KEY_1,
421 [0x01] = KEY_2,
422 [0x02] = KEY_3,
423 [0x03] = KEY_4,
424 [0x04] = KEY_5,
425 [0x05] = KEY_6,
426 [0x06] = KEY_7,
427 [0x07] = KEY_8,
428 [0x08] = KEY_9,
429 [0x09] = KEY_0,
430 [0x0e] = KEY_ESC, /* return */
431 [0x0f] = KEY_CLEAR,
432 [0x16] = KEY_EJECTCD,
433 [0x1a] = KEY_MENU, /* top menu */
434 [0x28] = KEY_TIME,
435 [0x30] = KEY_PREVIOUS,
436 [0x31] = KEY_NEXT,
437 [0x32] = KEY_PLAY,
438 [0x33] = KEY_REWIND, /* scan back */
439 [0x34] = KEY_FORWARD, /* scan forward */
440 [0x38] = KEY_STOP,
441 [0x39] = KEY_PAUSE,
442 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
443 [0x60] = KEY_FRAMEBACK, /* slow/step back */
444 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
445 [0x63] = KEY_SUBTITLE,
446 [0x64] = KEY_AUDIO,
447 [0x65] = KEY_ANGLE,
448 [0x70] = KEY_INFO, /* display */
449 [0x80] = KEY_BLUE,
450 [0x81] = KEY_RED,
451 [0x82] = KEY_GREEN,
452 [0x83] = KEY_YELLOW,
453};
454
f04d5140
CL
455static const unsigned int buzz_keymap[] = {
456 /* The controller has 4 remote buzzers, each with one LED and 5
457 * buttons.
458 *
459 * We use the mapping chosen by the controller, which is:
460 *
461 * Key Offset
462 * -------------------
463 * Buzz 1
464 * Blue 5
465 * Orange 4
466 * Green 3
467 * Yellow 2
468 *
469 * So, for example, the orange button on the third buzzer is mapped to
470 * BTN_TRIGGER_HAPPY14
471 */
472 [ 1] = BTN_TRIGGER_HAPPY1,
473 [ 2] = BTN_TRIGGER_HAPPY2,
474 [ 3] = BTN_TRIGGER_HAPPY3,
475 [ 4] = BTN_TRIGGER_HAPPY4,
476 [ 5] = BTN_TRIGGER_HAPPY5,
477 [ 6] = BTN_TRIGGER_HAPPY6,
478 [ 7] = BTN_TRIGGER_HAPPY7,
479 [ 8] = BTN_TRIGGER_HAPPY8,
480 [ 9] = BTN_TRIGGER_HAPPY9,
481 [10] = BTN_TRIGGER_HAPPY10,
482 [11] = BTN_TRIGGER_HAPPY11,
483 [12] = BTN_TRIGGER_HAPPY12,
484 [13] = BTN_TRIGGER_HAPPY13,
485 [14] = BTN_TRIGGER_HAPPY14,
486 [15] = BTN_TRIGGER_HAPPY15,
487 [16] = BTN_TRIGGER_HAPPY16,
488 [17] = BTN_TRIGGER_HAPPY17,
489 [18] = BTN_TRIGGER_HAPPY18,
490 [19] = BTN_TRIGGER_HAPPY19,
491 [20] = BTN_TRIGGER_HAPPY20,
492};
493
d902f472
FP
494static enum power_supply_property sony_battery_props[] = {
495 POWER_SUPPLY_PROP_PRESENT,
496 POWER_SUPPLY_PROP_CAPACITY,
497 POWER_SUPPLY_PROP_SCOPE,
498 POWER_SUPPLY_PROP_STATUS,
499};
500
cc6e0bbb 501struct sony_sc {
d902f472 502 spinlock_t lock;
0a286ef2 503 struct hid_device *hdev;
60781cf4 504 struct led_classdev *leds[MAX_LEDS];
c4e1ddf2 505 struct hid_report *output_report;
cc6e0bbb 506 unsigned long quirks;
0a286ef2 507 struct work_struct state_worker;
d902f472 508 struct power_supply battery;
f04d5140 509
9f323b68 510#ifdef CONFIG_SONY_FF
9f323b68
SE
511 __u8 left;
512 __u8 right;
513#endif
514
d902f472
FP
515 __u8 cable_state;
516 __u8 battery_charging;
517 __u8 battery_capacity;
60781cf4
FP
518 __u8 led_state[MAX_LEDS];
519 __u8 led_count;
cc6e0bbb
JK
520};
521
078328da
JK
522static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
523 unsigned int *rsize)
524{
525 *rsize = sizeof(ps3remote_rdesc);
526 return ps3remote_rdesc;
527}
528
529static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
530 struct hid_field *field, struct hid_usage *usage,
531 unsigned long **bit, int *max)
532{
533 unsigned int key = usage->hid & HID_USAGE;
534
535 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
536 return -1;
537
538 switch (usage->collection_index) {
539 case 1:
540 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
541 return -1;
542
543 key = ps3remote_keymap_joypad_buttons[key];
544 if (!key)
545 return -1;
546 break;
547 case 2:
548 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
549 return -1;
550
551 key = ps3remote_keymap_remote_buttons[key];
552 if (!key)
553 return -1;
554 break;
555 default:
556 return -1;
557 }
558
559 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
560 return 1;
561}
562
563
cc6e0bbb 564/* Sony Vaio VGX has wrongly mouse pointer declared as constant */
73e4008d
NK
565static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
566 unsigned int *rsize)
cc6e0bbb
JK
567{
568 struct sony_sc *sc = hid_get_drvdata(hdev);
569
99d24902
FLVC
570 /*
571 * Some Sony RF receivers wrongly declare the mouse pointer as a
572 * a constant non-data variable.
573 */
574 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
575 /* usage page: generic desktop controls */
576 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
577 /* usage: mouse */
578 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
579 /* input (usage page for x,y axes): constant, variable, relative */
580 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
a4649184 581 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
99d24902 582 /* input: data, variable, relative */
cc6e0bbb
JK
583 rdesc[55] = 0x06;
584 }
61ab44be 585
ed19d8cf
FP
586 /*
587 * The default Dualshock 4 USB descriptor doesn't assign
588 * the gyroscope values to corresponding axes so we need a
589 * modified one.
590 */
591 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
592 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
593 rdesc = dualshock4_usb_rdesc;
594 *rsize = sizeof(dualshock4_usb_rdesc);
595 }
596
61ab44be
SW
597 /* The HID descriptor exposed over BT has a trailing zero byte */
598 if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
599 ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
600 rdesc[83] == 0x75) {
601 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
602 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
603 sizeof(sixaxis_rdesc_fixup));
e57a67da
MCC
604 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
605 *rsize > sizeof(sixaxis_rdesc_fixup2)) {
606 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
607 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
608 *rsize = sizeof(sixaxis_rdesc_fixup2);
609 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
61ab44be 610 }
078328da
JK
611
612 if (sc->quirks & PS3REMOTE)
613 return ps3remote_fixup(hdev, rdesc, rsize);
614
73e4008d 615 return rdesc;
cc6e0bbb
JK
616}
617
d902f472
FP
618static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
619{
620 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
621 unsigned long flags;
622 __u8 cable_state, battery_capacity, battery_charging;
623
624 /* The sixaxis is charging if the battery value is 0xee
625 * and it is fully charged if the value is 0xef.
626 * It does not report the actual level while charging so it
627 * is set to 100% while charging is in progress.
628 */
629 if (rd[30] >= 0xee) {
630 battery_capacity = 100;
631 battery_charging = rd[30] & 0x01;
632 } else {
633 battery_capacity = sixaxis_battery_capacity[rd[30]];
634 battery_charging = 0;
635 }
636 cable_state = (rd[31] >> 4) & 0x01;
637
638 spin_lock_irqsave(&sc->lock, flags);
639 sc->cable_state = cable_state;
640 sc->battery_capacity = battery_capacity;
641 sc->battery_charging = battery_charging;
642 spin_unlock_irqrestore(&sc->lock, flags);
643}
644
645static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
646{
e5606230
FP
647 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
648 struct hid_input, list);
649 struct input_dev *input_dev = hidinput->input;
d902f472 650 unsigned long flags;
e5606230 651 int n, offset = 35;
d902f472
FP
652 __u8 cable_state, battery_capacity, battery_charging;
653
654 /* The lower 4 bits of byte 30 contain the battery level
655 * and the 5th bit contains the USB cable state.
656 */
657 cable_state = (rd[30] >> 4) & 0x01;
658 battery_capacity = rd[30] & 0x0F;
659
660 /* On USB the Dualshock 4 battery level goes from 0 to 11.
661 * A battery level of 11 means fully charged.
662 */
663 if (cable_state && battery_capacity == 11)
664 battery_charging = 0;
665 else
666 battery_charging = 1;
667
668 if (battery_capacity > 10)
669 battery_capacity--;
670 battery_capacity *= 10;
671
672 spin_lock_irqsave(&sc->lock, flags);
673 sc->cable_state = cable_state;
674 sc->battery_capacity = battery_capacity;
675 sc->battery_charging = battery_charging;
676 spin_unlock_irqrestore(&sc->lock, flags);
e5606230
FP
677
678 /* The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB.
679 * The first 7 bits of the first byte is a counter and bit 8 is a touch
680 * indicator that is 0 when pressed and 1 when not pressed.
681 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
682 * The data for the second touch is in the same format and immediatly
683 * follows the data for the first.
684 */
685 for (n = 0; n < 2; n++) {
686 __u16 x, y;
687
688 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
689 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
690
691 input_mt_slot(input_dev, n);
692 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
693 !(rd[offset] >> 7));
694 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
695 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
696
697 offset += 4;
698 }
d902f472
FP
699}
700
c9e4d877
SW
701static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
702 __u8 *rd, int size)
703{
704 struct sony_sc *sc = hid_get_drvdata(hdev);
705
706 /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
707 * has to be BYTE_SWAPPED before passing up to joystick interface
708 */
709 if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
710 rd[0] == 0x01 && size == 49) {
711 swap(rd[41], rd[42]);
712 swap(rd[43], rd[44]);
713 swap(rd[45], rd[46]);
714 swap(rd[47], rd[48]);
d902f472
FP
715
716 sixaxis_parse_report(sc, rd, size);
717 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
718 size == 64) {
719 dualshock4_parse_report(sc, rd, size);
c9e4d877
SW
720 }
721
722 return 0;
723}
724
f04d5140
CL
725static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
726 struct hid_field *field, struct hid_usage *usage,
727 unsigned long **bit, int *max)
728{
729 struct sony_sc *sc = hid_get_drvdata(hdev);
730
731 if (sc->quirks & BUZZ_CONTROLLER) {
732 unsigned int key = usage->hid & HID_USAGE;
733
734 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
735 return -1;
736
737 switch (usage->collection_index) {
738 case 1:
739 if (key >= ARRAY_SIZE(buzz_keymap))
740 return -1;
741
742 key = buzz_keymap[key];
743 if (!key)
744 return -1;
745 break;
746 default:
747 return -1;
748 }
749
750 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
751 return 1;
752 }
753
078328da
JK
754 if (sc->quirks & PS3REMOTE)
755 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
756
6f498018
BT
757 /* Let hid-core decide for the others */
758 return 0;
f04d5140
CL
759}
760
5710fabf
AO
761/*
762 * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
763 * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
764 * so we need to override that forcing HID Output Reports on the Control EP.
765 *
766 * There is also another issue about HID Output Reports via USB, the Sixaxis
767 * does not want the report_id as part of the data packet, so we have to
768 * discard buf[0] when sending the actual control message, even for numbered
769 * reports, humpf!
770 */
569b10a5
AO
771static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
772 size_t count, unsigned char report_type)
773{
774 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
775 struct usb_device *dev = interface_to_usbdev(intf);
776 struct usb_host_interface *interface = intf->cur_altsetting;
777 int report_id = buf[0];
778 int ret;
779
5710fabf
AO
780 if (report_type == HID_OUTPUT_REPORT) {
781 /* Don't send the Report ID */
782 buf++;
783 count--;
784 }
785
569b10a5
AO
786 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
787 HID_REQ_SET_REPORT,
788 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
789 ((report_type + 1) << 8) | report_id,
790 interface->desc.bInterfaceNumber, buf, count,
791 USB_CTRL_SET_TIMEOUT);
792
5710fabf
AO
793 /* Count also the Report ID, in case of an Output report. */
794 if (ret > 0 && report_type == HID_OUTPUT_REPORT)
795 ret++;
796
569b10a5
AO
797 return ret;
798}
799
bd28ce00
JS
800/*
801 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
802 * to "operational". Without this, the ps3 controller will not report any
803 * events.
804 */
816651a7 805static int sixaxis_set_operational_usb(struct hid_device *hdev)
bd28ce00 806{
bd28ce00
JS
807 int ret;
808 char *buf = kmalloc(18, GFP_KERNEL);
809
810 if (!buf)
811 return -ENOMEM;
812
f204828a
BT
813 ret = hdev->hid_get_raw_report(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT);
814
bd28ce00 815 if (ret < 0)
4291ee30 816 hid_err(hdev, "can't set operational mode\n");
bd28ce00
JS
817
818 kfree(buf);
819
820 return ret;
821}
822
816651a7 823static int sixaxis_set_operational_bt(struct hid_device *hdev)
f9ce7c28 824{
fddb33f2 825 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
f9ce7c28
BN
826 return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
827}
828
60781cf4 829static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
f04d5140
CL
830{
831 struct list_head *report_list =
832 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
833 struct hid_report *report = list_entry(report_list->next,
834 struct hid_report, list);
835 __s32 *value = report->field[0]->value;
836
837 value[0] = 0x00;
60781cf4
FP
838 value[1] = leds[0] ? 0xff : 0x00;
839 value[2] = leds[1] ? 0xff : 0x00;
840 value[3] = leds[2] ? 0xff : 0x00;
841 value[4] = leds[3] ? 0xff : 0x00;
f04d5140
CL
842 value[5] = 0x00;
843 value[6] = 0x00;
844 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
845}
846
60781cf4 847static void sony_set_leds(struct hid_device *hdev, const __u8 *leds, int count)
0a286ef2
SE
848{
849 struct sony_sc *drv_data = hid_get_drvdata(hdev);
60781cf4 850 int n;
0a286ef2 851
60781cf4
FP
852 BUG_ON(count > MAX_LEDS);
853
854 if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) {
0a286ef2 855 buzz_set_leds(hdev, leds);
60781cf4 856 } else if ((drv_data->quirks & SIXAXIS_CONTROLLER_USB) ||
8ab1676b 857 (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB)) {
60781cf4
FP
858 for (n = 0; n < count; n++)
859 drv_data->led_state[n] = leds[n];
0a286ef2
SE
860 schedule_work(&drv_data->state_worker);
861 }
862}
863
c5382519 864static void sony_led_set_brightness(struct led_classdev *led,
f04d5140
CL
865 enum led_brightness value)
866{
867 struct device *dev = led->dev->parent;
868 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
869 struct sony_sc *drv_data;
f04d5140
CL
870
871 int n;
872
873 drv_data = hid_get_drvdata(hdev);
2251b85f 874 if (!drv_data) {
f04d5140
CL
875 hid_err(hdev, "No device data\n");
876 return;
877 }
f04d5140 878
60781cf4 879 for (n = 0; n < drv_data->led_count; n++) {
2251b85f 880 if (led == drv_data->leds[n]) {
60781cf4
FP
881 if (value != drv_data->led_state[n]) {
882 drv_data->led_state[n] = value;
883 sony_set_leds(hdev, drv_data->led_state, drv_data->led_count);
f04d5140
CL
884 }
885 break;
886 }
887 }
888}
889
c5382519 890static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
f04d5140
CL
891{
892 struct device *dev = led->dev->parent;
893 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
894 struct sony_sc *drv_data;
f04d5140
CL
895
896 int n;
897 int on = 0;
898
899 drv_data = hid_get_drvdata(hdev);
2251b85f 900 if (!drv_data) {
f04d5140
CL
901 hid_err(hdev, "No device data\n");
902 return LED_OFF;
903 }
f04d5140 904
60781cf4 905 for (n = 0; n < drv_data->led_count; n++) {
2251b85f 906 if (led == drv_data->leds[n]) {
60781cf4 907 on = !!(drv_data->led_state[n]);
f04d5140
CL
908 break;
909 }
910 }
911
912 return on ? LED_FULL : LED_OFF;
913}
f04d5140 914
0a286ef2
SE
915static void sony_leds_remove(struct hid_device *hdev)
916{
917 struct sony_sc *drv_data;
918 struct led_classdev *led;
919 int n;
920
921 drv_data = hid_get_drvdata(hdev);
922 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
923
60781cf4 924 for (n = 0; n < drv_data->led_count; n++) {
0a286ef2
SE
925 led = drv_data->leds[n];
926 drv_data->leds[n] = NULL;
927 if (!led)
928 continue;
929 led_classdev_unregister(led);
930 kfree(led);
931 }
60781cf4
FP
932
933 drv_data->led_count = 0;
0a286ef2
SE
934}
935
c5382519 936static int sony_leds_init(struct hid_device *hdev)
f04d5140
CL
937{
938 struct sony_sc *drv_data;
40e32ee6 939 int n, ret = 0;
60781cf4 940 int max_brightness;
61ebca93 941 int use_colors;
40e32ee6
JK
942 struct led_classdev *led;
943 size_t name_sz;
944 char *name;
0a286ef2
SE
945 size_t name_len;
946 const char *name_fmt;
61ebca93 947 static const char * const color_str[] = { "red", "green", "blue" };
60781cf4 948 static const __u8 initial_values[MAX_LEDS] = { 0x00, 0x00, 0x00, 0x00 };
f04d5140
CL
949
950 drv_data = hid_get_drvdata(hdev);
0a286ef2
SE
951 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
952
953 if (drv_data->quirks & BUZZ_CONTROLLER) {
61ebca93
FP
954 drv_data->led_count = 4;
955 max_brightness = 1;
956 use_colors = 0;
0a286ef2
SE
957 name_len = strlen("::buzz#");
958 name_fmt = "%s::buzz%d";
959 /* Validate expected report characteristics. */
960 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
961 return -ENODEV;
61ebca93 962 } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB) {
60781cf4
FP
963 drv_data->led_count = 3;
964 max_brightness = 255;
61ebca93
FP
965 use_colors = 1;
966 name_len = 0;
967 name_fmt = "%s:%s";
60781cf4
FP
968 } else {
969 drv_data->led_count = 4;
970 max_brightness = 1;
61ebca93
FP
971 use_colors = 0;
972 name_len = strlen("::sony#");
973 name_fmt = "%s::sony%d";
60781cf4
FP
974 }
975
f04d5140
CL
976 /* Clear LEDs as we have no way of reading their initial state. This is
977 * only relevant if the driver is loaded after somebody actively set the
978 * LEDs to on */
60781cf4 979 sony_set_leds(hdev, initial_values, drv_data->led_count);
f04d5140 980
0a286ef2 981 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
f04d5140 982
60781cf4 983 for (n = 0; n < drv_data->led_count; n++) {
61ebca93
FP
984
985 if (use_colors)
986 name_sz = strlen(dev_name(&hdev->dev)) + strlen(color_str[n]) + 2;
987
40e32ee6
JK
988 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
989 if (!led) {
990 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
8cd5fcda 991 ret = -ENOMEM;
40e32ee6
JK
992 goto error_leds;
993 }
f04d5140 994
40e32ee6 995 name = (void *)(&led[1]);
61ebca93
FP
996 if (use_colors)
997 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), color_str[n]);
998 else
999 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
40e32ee6
JK
1000 led->name = name;
1001 led->brightness = 0;
60781cf4 1002 led->max_brightness = max_brightness;
c5382519
SE
1003 led->brightness_get = sony_led_get_brightness;
1004 led->brightness_set = sony_led_set_brightness;
f04d5140 1005
8cd5fcda
JL
1006 ret = led_classdev_register(&hdev->dev, led);
1007 if (ret) {
40e32ee6
JK
1008 hid_err(hdev, "Failed to register LED %d\n", n);
1009 kfree(led);
1010 goto error_leds;
f04d5140 1011 }
40e32ee6 1012
2251b85f 1013 drv_data->leds[n] = led;
f04d5140 1014 }
f04d5140
CL
1015
1016 return ret;
1017
f04d5140 1018error_leds:
0a286ef2 1019 sony_leds_remove(hdev);
f04d5140 1020
f04d5140 1021 return ret;
f04d5140
CL
1022}
1023
cad665a2 1024static void sixaxis_state_worker(struct work_struct *work)
a08c22c0 1025{
92b5c411 1026 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
a08c22c0
SE
1027 unsigned char buf[] = {
1028 0x01,
1029 0x00, 0xff, 0x00, 0xff, 0x00,
0a286ef2 1030 0x00, 0x00, 0x00, 0x00, 0x00,
a08c22c0
SE
1031 0xff, 0x27, 0x10, 0x00, 0x32,
1032 0xff, 0x27, 0x10, 0x00, 0x32,
1033 0xff, 0x27, 0x10, 0x00, 0x32,
1034 0xff, 0x27, 0x10, 0x00, 0x32,
1035 0x00, 0x00, 0x00, 0x00, 0x00
1036 };
9f323b68 1037
0a286ef2 1038#ifdef CONFIG_SONY_FF
0bd88dd3 1039 buf[3] = sc->right ? 1 : 0;
9f323b68 1040 buf[5] = sc->left;
0a286ef2
SE
1041#endif
1042
60781cf4
FP
1043 buf[10] |= sc->led_state[0] << 1;
1044 buf[10] |= sc->led_state[1] << 2;
1045 buf[10] |= sc->led_state[2] << 3;
1046 buf[10] |= sc->led_state[3] << 4;
9f323b68
SE
1047
1048 sc->hdev->hid_output_raw_report(sc->hdev, buf, sizeof(buf),
1049 HID_OUTPUT_REPORT);
1050}
1051
0bd88dd3
FP
1052static void dualshock4_state_worker(struct work_struct *work)
1053{
1054 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
0da8ea65 1055 struct hid_device *hdev = sc->hdev;
c4e1ddf2
FP
1056 struct hid_report *report = sc->output_report;
1057 __s32 *value = report->field[0]->value;
0da8ea65 1058
0da8ea65 1059 value[0] = 0x03;
0bd88dd3
FP
1060
1061#ifdef CONFIG_SONY_FF
0da8ea65
FP
1062 value[3] = sc->right;
1063 value[4] = sc->left;
0bd88dd3
FP
1064#endif
1065
0da8ea65
FP
1066 value[5] = sc->led_state[0];
1067 value[6] = sc->led_state[1];
1068 value[7] = sc->led_state[2];
60781cf4 1069
0da8ea65 1070 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
0bd88dd3
FP
1071}
1072
0a286ef2 1073#ifdef CONFIG_SONY_FF
9f323b68
SE
1074static int sony_play_effect(struct input_dev *dev, void *data,
1075 struct ff_effect *effect)
1076{
a08c22c0 1077 struct hid_device *hid = input_get_drvdata(dev);
9f323b68 1078 struct sony_sc *sc = hid_get_drvdata(hid);
a08c22c0
SE
1079
1080 if (effect->type != FF_RUMBLE)
1081 return 0;
1082
9f323b68 1083 sc->left = effect->u.rumble.strong_magnitude / 256;
0bd88dd3 1084 sc->right = effect->u.rumble.weak_magnitude / 256;
a08c22c0 1085
92b5c411 1086 schedule_work(&sc->state_worker);
9f323b68 1087 return 0;
a08c22c0
SE
1088}
1089
1090static int sony_init_ff(struct hid_device *hdev)
1091{
1092 struct hid_input *hidinput = list_entry(hdev->inputs.next,
1093 struct hid_input, list);
1094 struct input_dev *input_dev = hidinput->input;
1095
1096 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1097 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1098}
1099
9f323b68
SE
1100static void sony_destroy_ff(struct hid_device *hdev)
1101{
1102 struct sony_sc *sc = hid_get_drvdata(hdev);
1103
92b5c411 1104 cancel_work_sync(&sc->state_worker);
9f323b68
SE
1105}
1106
a08c22c0
SE
1107#else
1108static int sony_init_ff(struct hid_device *hdev)
1109{
1110 return 0;
1111}
9f323b68
SE
1112
1113static void sony_destroy_ff(struct hid_device *hdev)
1114{
1115}
a08c22c0
SE
1116#endif
1117
d902f472
FP
1118static int sony_battery_get_property(struct power_supply *psy,
1119 enum power_supply_property psp,
1120 union power_supply_propval *val)
1121{
1122 struct sony_sc *sc = container_of(psy, struct sony_sc, battery);
1123 unsigned long flags;
1124 int ret = 0;
1125 u8 battery_charging, battery_capacity, cable_state;
1126
1127 spin_lock_irqsave(&sc->lock, flags);
1128 battery_charging = sc->battery_charging;
1129 battery_capacity = sc->battery_capacity;
1130 cable_state = sc->cable_state;
1131 spin_unlock_irqrestore(&sc->lock, flags);
1132
1133 switch (psp) {
1134 case POWER_SUPPLY_PROP_PRESENT:
1135 val->intval = 1;
1136 break;
1137 case POWER_SUPPLY_PROP_SCOPE:
1138 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1139 break;
1140 case POWER_SUPPLY_PROP_CAPACITY:
1141 val->intval = battery_capacity;
1142 break;
1143 case POWER_SUPPLY_PROP_STATUS:
1144 if (battery_charging)
1145 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1146 else
1147 if (battery_capacity == 100 && cable_state)
1148 val->intval = POWER_SUPPLY_STATUS_FULL;
1149 else
1150 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1151 break;
1152 default:
1153 ret = -EINVAL;
1154 break;
1155 }
1156 return ret;
1157}
1158
1159static int sony_battery_probe(struct sony_sc *sc)
1160{
1161 static atomic_t power_id_seq = ATOMIC_INIT(0);
1162 unsigned long power_id;
1163 struct hid_device *hdev = sc->hdev;
1164 int ret;
1165
1166 power_id = (unsigned long)atomic_inc_return(&power_id_seq);
1167
1168 sc->battery.properties = sony_battery_props;
1169 sc->battery.num_properties = ARRAY_SIZE(sony_battery_props);
1170 sc->battery.get_property = sony_battery_get_property;
1171 sc->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1172 sc->battery.use_for_apm = 0;
1173 sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%lu",
1174 power_id);
1175 if (!sc->battery.name)
1176 return -ENOMEM;
1177
1178 ret = power_supply_register(&hdev->dev, &sc->battery);
1179 if (ret) {
1180 hid_err(hdev, "Unable to register battery device\n");
1181 goto err_free;
1182 }
1183
1184 power_supply_powers(&sc->battery, &hdev->dev);
1185 return 0;
1186
1187err_free:
1188 kfree(sc->battery.name);
1189 sc->battery.name = NULL;
1190 return ret;
1191}
1192
1193static void sony_battery_remove(struct sony_sc *sc)
1194{
1195 if (!sc->battery.name)
1196 return;
1197
1198 power_supply_unregister(&sc->battery);
1199 kfree(sc->battery.name);
1200 sc->battery.name = NULL;
1201}
1202
c4e1ddf2
FP
1203static int sony_set_output_report(struct sony_sc *sc, int req_id, int req_size)
1204{
1205 struct list_head *head, *list;
1206 struct hid_report *report;
1207 struct hid_device *hdev = sc->hdev;
1208
1209 list = &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1210
1211 list_for_each(head, list) {
1212 report = list_entry(head, struct hid_report, list);
1213
1214 if (report->id == req_id) {
1215 if (report->size < req_size) {
1216 hid_err(hdev, "Output report 0x%02x (%i bits) is smaller than requested size (%i bits)\n",
1217 req_id, report->size, req_size);
1218 return -EINVAL;
1219 }
1220 sc->output_report = report;
1221 return 0;
1222 }
1223 }
1224
1225 hid_err(hdev, "Unable to locate output report 0x%02x\n", req_id);
1226
1227 return -EINVAL;
1228}
1229
e5606230
FP
1230static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
1231 int w, int h)
1232{
1233 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1234 struct hid_input, list);
1235 struct input_dev *input_dev = hidinput->input;
1236 int ret;
1237
1238 ret = input_mt_init_slots(input_dev, touch_count, 0);
1239 if (ret < 0) {
1240 hid_err(sc->hdev, "Unable to initialize multi-touch slots\n");
1241 return ret;
1242 }
1243
1244 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1245 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1246
1247 return 0;
1248}
1249
bd28ce00
JS
1250static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1251{
1252 int ret;
cc6e0bbb
JK
1253 unsigned long quirks = id->driver_data;
1254 struct sony_sc *sc;
f04d5140 1255 unsigned int connect_mask = HID_CONNECT_DEFAULT;
cc6e0bbb 1256
abf832bf 1257 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
cc6e0bbb 1258 if (sc == NULL) {
4291ee30 1259 hid_err(hdev, "can't alloc sony descriptor\n");
cc6e0bbb
JK
1260 return -ENOMEM;
1261 }
1262
1263 sc->quirks = quirks;
1264 hid_set_drvdata(hdev, sc);
0a286ef2 1265 sc->hdev = hdev;
bd28ce00 1266
bd28ce00
JS
1267 ret = hid_parse(hdev);
1268 if (ret) {
4291ee30 1269 hid_err(hdev, "parse failed\n");
abf832bf 1270 return ret;
bd28ce00
JS
1271 }
1272
f04d5140
CL
1273 if (sc->quirks & VAIO_RDESC_CONSTANT)
1274 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1275 else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
1276 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1277 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1278 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1279
1280 ret = hid_hw_start(hdev, connect_mask);
bd28ce00 1281 if (ret) {
4291ee30 1282 hid_err(hdev, "hw start failed\n");
abf832bf 1283 return ret;
bd28ce00
JS
1284 }
1285
569b10a5
AO
1286 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1287 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
816651a7 1288 ret = sixaxis_set_operational_usb(hdev);
cad665a2 1289 INIT_WORK(&sc->state_worker, sixaxis_state_worker);
569b10a5 1290 }
816651a7
AO
1291 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1292 ret = sixaxis_set_operational_bt(hdev);
8ab1676b 1293 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
c4e1ddf2
FP
1294 /* Report 5 (31 bytes) is used to send data to the controller via USB */
1295 ret = sony_set_output_report(sc, 0x05, 248);
1296 if (ret < 0)
1297 goto err_stop;
1298
e5606230
FP
1299 /* The Dualshock 4 touchpad supports 2 touches and has a
1300 * resolution of 1920x940.
1301 */
1302 ret = sony_register_touchpad(sc, 2, 1920, 940);
1303 if (ret < 0)
1304 goto err_stop;
1305
0bd88dd3
FP
1306 INIT_WORK(&sc->state_worker, dualshock4_state_worker);
1307 } else {
1308 ret = 0;
1309 }
f9ce7c28 1310
4dfdc464 1311 if (ret < 0)
bd28ce00
JS
1312 goto err_stop;
1313
0a286ef2
SE
1314 if (sc->quirks & SONY_LED_SUPPORT) {
1315 ret = sony_leds_init(hdev);
1316 if (ret < 0)
1317 goto err_stop;
1318 }
1319
d902f472
FP
1320 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1321 ret = sony_battery_probe(sc);
1322 if (ret < 0)
1323 goto err_stop;
1324
1325 /* Open the device to receive reports with battery info */
1326 ret = hid_hw_open(hdev);
1327 if (ret < 0) {
1328 hid_err(hdev, "hw open failed\n");
1329 goto err_stop;
1330 }
1331 }
1332
a08c22c0
SE
1333 ret = sony_init_ff(hdev);
1334 if (ret < 0)
d902f472 1335 goto err_close;
a08c22c0 1336
bd28ce00 1337 return 0;
d902f472
FP
1338err_close:
1339 hid_hw_close(hdev);
bd28ce00 1340err_stop:
0a286ef2
SE
1341 if (sc->quirks & SONY_LED_SUPPORT)
1342 sony_leds_remove(hdev);
d902f472
FP
1343 if (sc->quirks & SONY_BATTERY_SUPPORT)
1344 sony_battery_remove(sc);
bd28ce00 1345 hid_hw_stop(hdev);
bd28ce00
JS
1346 return ret;
1347}
1348
cc6e0bbb
JK
1349static void sony_remove(struct hid_device *hdev)
1350{
f04d5140
CL
1351 struct sony_sc *sc = hid_get_drvdata(hdev);
1352
0a286ef2 1353 if (sc->quirks & SONY_LED_SUPPORT)
c5382519 1354 sony_leds_remove(hdev);
f04d5140 1355
d902f472
FP
1356 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1357 hid_hw_close(hdev);
1358 sony_battery_remove(sc);
1359 }
1360
9f323b68
SE
1361 sony_destroy_ff(hdev);
1362
cc6e0bbb 1363 hid_hw_stop(hdev);
cc6e0bbb
JK
1364}
1365
bd28ce00 1366static const struct hid_device_id sony_devices[] = {
816651a7
AO
1367 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1368 .driver_data = SIXAXIS_CONTROLLER_USB },
35dca5b4
JK
1369 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
1370 .driver_data = SIXAXIS_CONTROLLER_USB },
816651a7
AO
1371 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1372 .driver_data = SIXAXIS_CONTROLLER_BT },
cc6e0bbb
JK
1373 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
1374 .driver_data = VAIO_RDESC_CONSTANT },
a4649184
FLVC
1375 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
1376 .driver_data = VAIO_RDESC_CONSTANT },
f04d5140
CL
1377 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
1378 * Logitech joystick from the device descriptor. */
1379 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
1380 .driver_data = BUZZ_CONTROLLER },
1381 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
1382 .driver_data = BUZZ_CONTROLLER },
078328da
JK
1383 /* PS3 BD Remote Control */
1384 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
1385 .driver_data = PS3REMOTE },
1386 /* Logitech Harmony Adapter for PS3 */
1387 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
1388 .driver_data = PS3REMOTE },
0bd88dd3
FP
1389 /* Sony Dualshock 4 controllers for PS4 */
1390 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
8ab1676b 1391 .driver_data = DUALSHOCK4_CONTROLLER_USB },
0bd88dd3 1392 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
8ab1676b 1393 .driver_data = DUALSHOCK4_CONTROLLER_BT },
bd28ce00
JS
1394 { }
1395};
1396MODULE_DEVICE_TABLE(hid, sony_devices);
1397
1398static struct hid_driver sony_driver = {
f04d5140
CL
1399 .name = "sony",
1400 .id_table = sony_devices,
1401 .input_mapping = sony_mapping,
1402 .probe = sony_probe,
1403 .remove = sony_remove,
1404 .report_fixup = sony_report_fixup,
1405 .raw_event = sony_raw_event
bd28ce00 1406};
f425458e 1407module_hid_driver(sony_driver);
bd28ce00 1408
bd28ce00 1409MODULE_LICENSE("GPL");