blob: 88401fc59886c68faa2828d06a2faa827ca1c64f [file] [log] [blame]
Jiri Slabybd28ce02008-06-25 23:47:04 +02001/*
Jiri Kosina078328d2013-06-13 12:03:49 +02002 * HID driver for Sony / PS2 / PS3 BD devices.
Jiri Slabybd28ce02008-06-25 23:47:04 +02003 *
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
Jiri Slabybd28ce02008-06-25 23:47:04 +02007 * Copyright (c) 2008 Jiri Slaby
Jiri Kosina078328d2013-06-13 12:03:49 +02008 * Copyright (c) 2012 David Dillow <dave@thedillows.org>
9 * Copyright (c) 2006-2013 Jiri Kosina
Colin Leitnerf04d5142013-05-27 23:41:05 +020010 * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
Jiri Slabybd28ce02008-06-25 23:47:04 +020011 */
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
Jiri Kosina078328d2013-06-13 12:03:49 +020020/* 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
Jiri Slabybd28ce02008-06-25 23:47:04 +020027#include <linux/device.h>
28#include <linux/hid.h>
29#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020031#include <linux/usb.h>
Jiri Kosina40e32ee2013-05-28 11:22:09 +020032#include <linux/leds.h>
Frank Praznikd902f472014-01-27 10:17:36 -050033#include <linux/power_supply.h>
34#include <linux/spinlock.h>
Frank Praznike5606232014-01-27 10:17:37 -050035#include <linux/input/mt.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020036
37#include "hid-ids.h"
38
Frank Praznik6c79c182014-01-16 21:43:03 -050039#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)
Frank Praznik8ab16762014-01-16 21:42:31 -050044#define DUALSHOCK4_CONTROLLER_USB BIT(5)
45#define DUALSHOCK4_CONTROLLER_BT BIT(6)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020046
Frank Praznik8ab16762014-01-16 21:42:31 -050047#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER_USB | BUZZ_CONTROLLER | DUALSHOCK4_CONTROLLER_USB)
Frank Praznikd902f472014-01-27 10:17:36 -050048#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_USB)
Frank Praznik60781cf2014-01-11 15:13:15 -050049
50#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010051
Simon Wood61ab44b2011-06-10 12:00:26 +020052static 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
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -020058static 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
Frank Praznik58d70272014-01-20 12:27:01 -050080/* 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 */
Frank Prazniked19d8c2014-01-16 21:43:12 -050085static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -050086 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 */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500337};
338
Frank Praznikd8296742014-02-05 20:03:45 -0500339/* The default behavior of the Dualshock 4 is to send reports using report
340 * type 1 when running over Bluetooth. However, as soon as it receives a
341 * report of type 17 to set the LEDs or rumble it starts returning it's state
342 * in report 17 instead of 1. Since report 17 is undefined in the default HID
343 * descriptor the button and axis definitions must be moved to report 17 or
344 * the HID layer won't process the received input once a report is sent.
345 */
346static u8 dualshock4_bt_rdesc[] = {
347 0x05, 0x01, /* Usage Page (Desktop), */
348 0x09, 0x05, /* Usage (Gamepad), */
349 0xA1, 0x01, /* Collection (Application), */
350 0x85, 0x01, /* Report ID (1), */
351 0x75, 0x08, /* Report Size (8), */
352 0x95, 0x0A, /* Report Count (9), */
353 0x81, 0x02, /* Input (Variable), */
354 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
355 0x85, 0x02, /* Report ID (2), */
356 0x09, 0x24, /* Usage (24h), */
357 0x95, 0x24, /* Report Count (36), */
358 0xB1, 0x02, /* Feature (Variable), */
359 0x85, 0xA3, /* Report ID (163), */
360 0x09, 0x25, /* Usage (25h), */
361 0x95, 0x30, /* Report Count (48), */
362 0xB1, 0x02, /* Feature (Variable), */
363 0x85, 0x05, /* Report ID (5), */
364 0x09, 0x26, /* Usage (26h), */
365 0x95, 0x28, /* Report Count (40), */
366 0xB1, 0x02, /* Feature (Variable), */
367 0x85, 0x06, /* Report ID (6), */
368 0x09, 0x27, /* Usage (27h), */
369 0x95, 0x34, /* Report Count (52), */
370 0xB1, 0x02, /* Feature (Variable), */
371 0x85, 0x07, /* Report ID (7), */
372 0x09, 0x28, /* Usage (28h), */
373 0x95, 0x30, /* Report Count (48), */
374 0xB1, 0x02, /* Feature (Variable), */
375 0x85, 0x08, /* Report ID (8), */
376 0x09, 0x29, /* Usage (29h), */
377 0x95, 0x2F, /* Report Count (47), */
378 0xB1, 0x02, /* Feature (Variable), */
379 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
380 0x85, 0x03, /* Report ID (3), */
381 0x09, 0x21, /* Usage (21h), */
382 0x95, 0x26, /* Report Count (38), */
383 0xB1, 0x02, /* Feature (Variable), */
384 0x85, 0x04, /* Report ID (4), */
385 0x09, 0x22, /* Usage (22h), */
386 0x95, 0x2E, /* Report Count (46), */
387 0xB1, 0x02, /* Feature (Variable), */
388 0x85, 0xF0, /* Report ID (240), */
389 0x09, 0x47, /* Usage (47h), */
390 0x95, 0x3F, /* Report Count (63), */
391 0xB1, 0x02, /* Feature (Variable), */
392 0x85, 0xF1, /* Report ID (241), */
393 0x09, 0x48, /* Usage (48h), */
394 0x95, 0x3F, /* Report Count (63), */
395 0xB1, 0x02, /* Feature (Variable), */
396 0x85, 0xF2, /* Report ID (242), */
397 0x09, 0x49, /* Usage (49h), */
398 0x95, 0x0F, /* Report Count (15), */
399 0xB1, 0x02, /* Feature (Variable), */
400 0x85, 0x11, /* Report ID (17), */
401 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
402 0x09, 0x20, /* Usage (20h), */
403 0x95, 0x02, /* Report Count (2), */
404 0x81, 0x02, /* Input (Variable), */
405 0x05, 0x01, /* Usage Page (Desktop), */
406 0x09, 0x30, /* Usage (X), */
407 0x09, 0x31, /* Usage (Y), */
408 0x09, 0x32, /* Usage (Z), */
409 0x09, 0x35, /* Usage (Rz), */
410 0x15, 0x00, /* Logical Minimum (0), */
411 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
412 0x75, 0x08, /* Report Size (8), */
413 0x95, 0x04, /* Report Count (4), */
414 0x81, 0x02, /* Input (Variable), */
415 0x09, 0x39, /* Usage (Hat Switch), */
416 0x15, 0x00, /* Logical Minimum (0), */
417 0x25, 0x07, /* Logical Maximum (7), */
418 0x75, 0x04, /* Report Size (4), */
419 0x95, 0x01, /* Report Count (1), */
420 0x81, 0x42, /* Input (Variable, Null State), */
421 0x05, 0x09, /* Usage Page (Button), */
422 0x19, 0x01, /* Usage Minimum (01h), */
423 0x29, 0x0E, /* Usage Maximum (0Eh), */
424 0x15, 0x00, /* Logical Minimum (0), */
425 0x25, 0x01, /* Logical Maximum (1), */
426 0x75, 0x01, /* Report Size (1), */
427 0x95, 0x0E, /* Report Count (14), */
428 0x81, 0x02, /* Input (Variable), */
429 0x75, 0x06, /* Report Size (6), */
430 0x95, 0x01, /* Report Count (1), */
431 0x81, 0x01, /* Input (Constant), */
432 0x05, 0x01, /* Usage Page (Desktop), */
433 0x09, 0x33, /* Usage (Rx), */
434 0x09, 0x34, /* Usage (Ry), */
435 0x15, 0x00, /* Logical Minimum (0), */
436 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
437 0x75, 0x08, /* Report Size (8), */
438 0x95, 0x02, /* Report Count (2), */
439 0x81, 0x02, /* Input (Variable), */
440 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
441 0x09, 0x20, /* Usage (20h), */
442 0x95, 0x03, /* Report Count (3), */
443 0x81, 0x02, /* Input (Variable), */
444 0x05, 0x01, /* Usage Page (Desktop), */
445 0x19, 0x40, /* Usage Minimum (40h), */
446 0x29, 0x42, /* Usage Maximum (42h), */
447 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
448 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
449 0x75, 0x10, /* Report Size (16), */
450 0x95, 0x03, /* Report Count (3), */
451 0x81, 0x02, /* Input (Variable), */
452 0x19, 0x43, /* Usage Minimum (43h), */
453 0x29, 0x45, /* Usage Maximum (45h), */
454 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
455 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
456 0x95, 0x03, /* Report Count (3), */
457 0x81, 0x02, /* Input (Variable), */
458 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
459 0x09, 0x20, /* Usage (20h), */
460 0x15, 0x00, /* Logical Minimum (0), */
461 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
462 0x75, 0x08, /* Report Size (8), */
463 0x95, 0x31, /* Report Count (51), */
464 0x81, 0x02, /* Input (Variable), */
465 0x09, 0x21, /* Usage (21h), */
466 0x75, 0x08, /* Report Size (8), */
467 0x95, 0x4D, /* Report Count (77), */
468 0x91, 0x02, /* Output (Variable), */
469 0x85, 0x12, /* Report ID (18), */
470 0x09, 0x22, /* Usage (22h), */
471 0x95, 0x8D, /* Report Count (141), */
472 0x81, 0x02, /* Input (Variable), */
473 0x09, 0x23, /* Usage (23h), */
474 0x91, 0x02, /* Output (Variable), */
475 0x85, 0x13, /* Report ID (19), */
476 0x09, 0x24, /* Usage (24h), */
477 0x95, 0xCD, /* Report Count (205), */
478 0x81, 0x02, /* Input (Variable), */
479 0x09, 0x25, /* Usage (25h), */
480 0x91, 0x02, /* Output (Variable), */
481 0x85, 0x14, /* Report ID (20), */
482 0x09, 0x26, /* Usage (26h), */
483 0x96, 0x0D, 0x01, /* Report Count (269), */
484 0x81, 0x02, /* Input (Variable), */
485 0x09, 0x27, /* Usage (27h), */
486 0x91, 0x02, /* Output (Variable), */
487 0x85, 0x15, /* Report ID (21), */
488 0x09, 0x28, /* Usage (28h), */
489 0x96, 0x4D, 0x01, /* Report Count (333), */
490 0x81, 0x02, /* Input (Variable), */
491 0x09, 0x29, /* Usage (29h), */
492 0x91, 0x02, /* Output (Variable), */
493 0x85, 0x16, /* Report ID (22), */
494 0x09, 0x2A, /* Usage (2Ah), */
495 0x96, 0x8D, 0x01, /* Report Count (397), */
496 0x81, 0x02, /* Input (Variable), */
497 0x09, 0x2B, /* Usage (2Bh), */
498 0x91, 0x02, /* Output (Variable), */
499 0x85, 0x17, /* Report ID (23), */
500 0x09, 0x2C, /* Usage (2Ch), */
501 0x96, 0xCD, 0x01, /* Report Count (461), */
502 0x81, 0x02, /* Input (Variable), */
503 0x09, 0x2D, /* Usage (2Dh), */
504 0x91, 0x02, /* Output (Variable), */
505 0x85, 0x18, /* Report ID (24), */
506 0x09, 0x2E, /* Usage (2Eh), */
507 0x96, 0x0D, 0x02, /* Report Count (525), */
508 0x81, 0x02, /* Input (Variable), */
509 0x09, 0x2F, /* Usage (2Fh), */
510 0x91, 0x02, /* Output (Variable), */
511 0x85, 0x19, /* Report ID (25), */
512 0x09, 0x30, /* Usage (30h), */
513 0x96, 0x22, 0x02, /* Report Count (546), */
514 0x81, 0x02, /* Input (Variable), */
515 0x09, 0x31, /* Usage (31h), */
516 0x91, 0x02, /* Output (Variable), */
517 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
518 0x85, 0x82, /* Report ID (130), */
519 0x09, 0x22, /* Usage (22h), */
520 0x95, 0x3F, /* Report Count (63), */
521 0xB1, 0x02, /* Feature (Variable), */
522 0x85, 0x83, /* Report ID (131), */
523 0x09, 0x23, /* Usage (23h), */
524 0xB1, 0x02, /* Feature (Variable), */
525 0x85, 0x84, /* Report ID (132), */
526 0x09, 0x24, /* Usage (24h), */
527 0xB1, 0x02, /* Feature (Variable), */
528 0x85, 0x90, /* Report ID (144), */
529 0x09, 0x30, /* Usage (30h), */
530 0xB1, 0x02, /* Feature (Variable), */
531 0x85, 0x91, /* Report ID (145), */
532 0x09, 0x31, /* Usage (31h), */
533 0xB1, 0x02, /* Feature (Variable), */
534 0x85, 0x92, /* Report ID (146), */
535 0x09, 0x32, /* Usage (32h), */
536 0xB1, 0x02, /* Feature (Variable), */
537 0x85, 0x93, /* Report ID (147), */
538 0x09, 0x33, /* Usage (33h), */
539 0xB1, 0x02, /* Feature (Variable), */
540 0x85, 0xA0, /* Report ID (160), */
541 0x09, 0x40, /* Usage (40h), */
542 0xB1, 0x02, /* Feature (Variable), */
543 0x85, 0xA4, /* Report ID (164), */
544 0x09, 0x44, /* Usage (44h), */
545 0xB1, 0x02, /* Feature (Variable), */
546 0xC0 /* End Collection */
547};
548
Jiri Kosina078328d2013-06-13 12:03:49 +0200549static __u8 ps3remote_rdesc[] = {
550 0x05, 0x01, /* GUsagePage Generic Desktop */
551 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
552 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
553
554 /* Use collection 1 for joypad buttons */
555 0xA1, 0x02, /* MCollection Logical (interrelated data) */
556
557 /* Ignore the 1st byte, maybe it is used for a controller
558 * number but it's not needed for correct operation */
559 0x75, 0x08, /* GReportSize 0x08 [8] */
560 0x95, 0x01, /* GReportCount 0x01 [1] */
561 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
562
563 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
564 * buttons multiple keypresses are allowed */
565 0x05, 0x09, /* GUsagePage Button */
566 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
567 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
568 0x14, /* GLogicalMinimum [0] */
569 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
570 0x75, 0x01, /* GReportSize 0x01 [1] */
571 0x95, 0x18, /* GReportCount 0x18 [24] */
572 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
573
574 0xC0, /* MEndCollection */
575
576 /* Use collection 2 for remote control buttons */
577 0xA1, 0x02, /* MCollection Logical (interrelated data) */
578
579 /* 5th byte is used for remote control buttons */
580 0x05, 0x09, /* GUsagePage Button */
581 0x18, /* LUsageMinimum [No button pressed] */
582 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
583 0x14, /* GLogicalMinimum [0] */
584 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
585 0x75, 0x08, /* GReportSize 0x08 [8] */
586 0x95, 0x01, /* GReportCount 0x01 [1] */
587 0x80, /* MInput */
588
589 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
590 * 0xff and 11th is for press indication */
591 0x75, 0x08, /* GReportSize 0x08 [8] */
592 0x95, 0x06, /* GReportCount 0x06 [6] */
593 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
594
595 /* 12th byte is for battery strength */
596 0x05, 0x06, /* GUsagePage Generic Device Controls */
597 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
598 0x14, /* GLogicalMinimum [0] */
599 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
600 0x75, 0x08, /* GReportSize 0x08 [8] */
601 0x95, 0x01, /* GReportCount 0x01 [1] */
602 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
603
604 0xC0, /* MEndCollection */
605
606 0xC0 /* MEndCollection [Game Pad] */
607};
608
609static const unsigned int ps3remote_keymap_joypad_buttons[] = {
610 [0x01] = KEY_SELECT,
611 [0x02] = BTN_THUMBL, /* L3 */
612 [0x03] = BTN_THUMBR, /* R3 */
613 [0x04] = BTN_START,
614 [0x05] = KEY_UP,
615 [0x06] = KEY_RIGHT,
616 [0x07] = KEY_DOWN,
617 [0x08] = KEY_LEFT,
618 [0x09] = BTN_TL2, /* L2 */
619 [0x0a] = BTN_TR2, /* R2 */
620 [0x0b] = BTN_TL, /* L1 */
621 [0x0c] = BTN_TR, /* R1 */
622 [0x0d] = KEY_OPTION, /* options/triangle */
623 [0x0e] = KEY_BACK, /* back/circle */
624 [0x0f] = BTN_0, /* cross */
625 [0x10] = KEY_SCREEN, /* view/square */
626 [0x11] = KEY_HOMEPAGE, /* PS button */
627 [0x14] = KEY_ENTER,
628};
629static const unsigned int ps3remote_keymap_remote_buttons[] = {
630 [0x00] = KEY_1,
631 [0x01] = KEY_2,
632 [0x02] = KEY_3,
633 [0x03] = KEY_4,
634 [0x04] = KEY_5,
635 [0x05] = KEY_6,
636 [0x06] = KEY_7,
637 [0x07] = KEY_8,
638 [0x08] = KEY_9,
639 [0x09] = KEY_0,
640 [0x0e] = KEY_ESC, /* return */
641 [0x0f] = KEY_CLEAR,
642 [0x16] = KEY_EJECTCD,
643 [0x1a] = KEY_MENU, /* top menu */
644 [0x28] = KEY_TIME,
645 [0x30] = KEY_PREVIOUS,
646 [0x31] = KEY_NEXT,
647 [0x32] = KEY_PLAY,
648 [0x33] = KEY_REWIND, /* scan back */
649 [0x34] = KEY_FORWARD, /* scan forward */
650 [0x38] = KEY_STOP,
651 [0x39] = KEY_PAUSE,
652 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
653 [0x60] = KEY_FRAMEBACK, /* slow/step back */
654 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
655 [0x63] = KEY_SUBTITLE,
656 [0x64] = KEY_AUDIO,
657 [0x65] = KEY_ANGLE,
658 [0x70] = KEY_INFO, /* display */
659 [0x80] = KEY_BLUE,
660 [0x81] = KEY_RED,
661 [0x82] = KEY_GREEN,
662 [0x83] = KEY_YELLOW,
663};
664
Colin Leitnerf04d5142013-05-27 23:41:05 +0200665static const unsigned int buzz_keymap[] = {
666 /* The controller has 4 remote buzzers, each with one LED and 5
667 * buttons.
668 *
669 * We use the mapping chosen by the controller, which is:
670 *
671 * Key Offset
672 * -------------------
673 * Buzz 1
674 * Blue 5
675 * Orange 4
676 * Green 3
677 * Yellow 2
678 *
679 * So, for example, the orange button on the third buzzer is mapped to
680 * BTN_TRIGGER_HAPPY14
681 */
682 [ 1] = BTN_TRIGGER_HAPPY1,
683 [ 2] = BTN_TRIGGER_HAPPY2,
684 [ 3] = BTN_TRIGGER_HAPPY3,
685 [ 4] = BTN_TRIGGER_HAPPY4,
686 [ 5] = BTN_TRIGGER_HAPPY5,
687 [ 6] = BTN_TRIGGER_HAPPY6,
688 [ 7] = BTN_TRIGGER_HAPPY7,
689 [ 8] = BTN_TRIGGER_HAPPY8,
690 [ 9] = BTN_TRIGGER_HAPPY9,
691 [10] = BTN_TRIGGER_HAPPY10,
692 [11] = BTN_TRIGGER_HAPPY11,
693 [12] = BTN_TRIGGER_HAPPY12,
694 [13] = BTN_TRIGGER_HAPPY13,
695 [14] = BTN_TRIGGER_HAPPY14,
696 [15] = BTN_TRIGGER_HAPPY15,
697 [16] = BTN_TRIGGER_HAPPY16,
698 [17] = BTN_TRIGGER_HAPPY17,
699 [18] = BTN_TRIGGER_HAPPY18,
700 [19] = BTN_TRIGGER_HAPPY19,
701 [20] = BTN_TRIGGER_HAPPY20,
702};
703
Frank Praznikd902f472014-01-27 10:17:36 -0500704static enum power_supply_property sony_battery_props[] = {
705 POWER_SUPPLY_PROP_PRESENT,
706 POWER_SUPPLY_PROP_CAPACITY,
707 POWER_SUPPLY_PROP_SCOPE,
708 POWER_SUPPLY_PROP_STATUS,
709};
710
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200711struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500712 spinlock_t lock;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100713 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500714 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200715 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100716 struct work_struct state_worker;
Frank Praznikd902f472014-01-27 10:17:36 -0500717 struct power_supply battery;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200718
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100719#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100720 __u8 left;
721 __u8 right;
722#endif
723
Frank Praznikd902f472014-01-27 10:17:36 -0500724 __u8 cable_state;
725 __u8 battery_charging;
726 __u8 battery_capacity;
Frank Praznik60781cf2014-01-11 15:13:15 -0500727 __u8 led_state[MAX_LEDS];
728 __u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200729};
730
Jiri Kosina078328d2013-06-13 12:03:49 +0200731static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
732 unsigned int *rsize)
733{
734 *rsize = sizeof(ps3remote_rdesc);
735 return ps3remote_rdesc;
736}
737
738static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
739 struct hid_field *field, struct hid_usage *usage,
740 unsigned long **bit, int *max)
741{
742 unsigned int key = usage->hid & HID_USAGE;
743
744 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
745 return -1;
746
747 switch (usage->collection_index) {
748 case 1:
749 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
750 return -1;
751
752 key = ps3remote_keymap_joypad_buttons[key];
753 if (!key)
754 return -1;
755 break;
756 case 2:
757 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
758 return -1;
759
760 key = ps3remote_keymap_remote_buttons[key];
761 if (!key)
762 return -1;
763 break;
764 default:
765 return -1;
766 }
767
768 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
769 return 1;
770}
771
772
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200773/* Sony Vaio VGX has wrongly mouse pointer declared as constant */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400774static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
775 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200776{
777 struct sony_sc *sc = hid_get_drvdata(hdev);
778
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900779 /*
780 * Some Sony RF receivers wrongly declare the mouse pointer as a
781 * a constant non-data variable.
782 */
783 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
784 /* usage page: generic desktop controls */
785 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
786 /* usage: mouse */
787 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
788 /* input (usage page for x,y axes): constant, variable, relative */
789 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa46491842013-01-15 19:40:48 +0900790 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900791 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200792 rdesc[55] = 0x06;
793 }
Simon Wood61ab44b2011-06-10 12:00:26 +0200794
Frank Prazniked19d8c2014-01-16 21:43:12 -0500795 /*
796 * The default Dualshock 4 USB descriptor doesn't assign
797 * the gyroscope values to corresponding axes so we need a
798 * modified one.
799 */
800 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
801 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
802 rdesc = dualshock4_usb_rdesc;
803 *rsize = sizeof(dualshock4_usb_rdesc);
Frank Praznikd8296742014-02-05 20:03:45 -0500804 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
805 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
806 rdesc = dualshock4_bt_rdesc;
807 *rsize = sizeof(dualshock4_bt_rdesc);
Frank Prazniked19d8c2014-01-16 21:43:12 -0500808 }
809
Simon Wood61ab44b2011-06-10 12:00:26 +0200810 /* The HID descriptor exposed over BT has a trailing zero byte */
811 if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
812 ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
813 rdesc[83] == 0x75) {
814 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
815 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
816 sizeof(sixaxis_rdesc_fixup));
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200817 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
818 *rsize > sizeof(sixaxis_rdesc_fixup2)) {
819 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
820 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
821 *rsize = sizeof(sixaxis_rdesc_fixup2);
822 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
Simon Wood61ab44b2011-06-10 12:00:26 +0200823 }
Jiri Kosina078328d2013-06-13 12:03:49 +0200824
825 if (sc->quirks & PS3REMOTE)
826 return ps3remote_fixup(hdev, rdesc, rsize);
827
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400828 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200829}
830
Frank Praznikd902f472014-01-27 10:17:36 -0500831static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
832{
833 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
834 unsigned long flags;
835 __u8 cable_state, battery_capacity, battery_charging;
836
837 /* The sixaxis is charging if the battery value is 0xee
838 * and it is fully charged if the value is 0xef.
839 * It does not report the actual level while charging so it
840 * is set to 100% while charging is in progress.
841 */
842 if (rd[30] >= 0xee) {
843 battery_capacity = 100;
844 battery_charging = rd[30] & 0x01;
845 } else {
846 battery_capacity = sixaxis_battery_capacity[rd[30]];
847 battery_charging = 0;
848 }
849 cable_state = (rd[31] >> 4) & 0x01;
850
851 spin_lock_irqsave(&sc->lock, flags);
852 sc->cable_state = cable_state;
853 sc->battery_capacity = battery_capacity;
854 sc->battery_charging = battery_charging;
855 spin_unlock_irqrestore(&sc->lock, flags);
856}
857
858static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
859{
Frank Praznike5606232014-01-27 10:17:37 -0500860 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
861 struct hid_input, list);
862 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -0500863 unsigned long flags;
Frank Praznike5606232014-01-27 10:17:37 -0500864 int n, offset = 35;
Frank Praznikd902f472014-01-27 10:17:36 -0500865 __u8 cable_state, battery_capacity, battery_charging;
866
867 /* The lower 4 bits of byte 30 contain the battery level
868 * and the 5th bit contains the USB cable state.
869 */
870 cable_state = (rd[30] >> 4) & 0x01;
871 battery_capacity = rd[30] & 0x0F;
872
873 /* On USB the Dualshock 4 battery level goes from 0 to 11.
874 * A battery level of 11 means fully charged.
875 */
876 if (cable_state && battery_capacity == 11)
877 battery_charging = 0;
878 else
879 battery_charging = 1;
880
881 if (battery_capacity > 10)
882 battery_capacity--;
883 battery_capacity *= 10;
884
885 spin_lock_irqsave(&sc->lock, flags);
886 sc->cable_state = cable_state;
887 sc->battery_capacity = battery_capacity;
888 sc->battery_charging = battery_charging;
889 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -0500890
891 /* The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB.
892 * The first 7 bits of the first byte is a counter and bit 8 is a touch
893 * indicator that is 0 when pressed and 1 when not pressed.
894 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
895 * The data for the second touch is in the same format and immediatly
896 * follows the data for the first.
897 */
898 for (n = 0; n < 2; n++) {
899 __u16 x, y;
900
901 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
902 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
903
904 input_mt_slot(input_dev, n);
905 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
906 !(rd[offset] >> 7));
907 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
908 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
909
910 offset += 4;
911 }
Frank Praznikd902f472014-01-27 10:17:36 -0500912}
913
Simon Woodc9e4d872011-06-10 12:00:27 +0200914static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
915 __u8 *rd, int size)
916{
917 struct sony_sc *sc = hid_get_drvdata(hdev);
918
919 /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
920 * has to be BYTE_SWAPPED before passing up to joystick interface
921 */
922 if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
923 rd[0] == 0x01 && size == 49) {
924 swap(rd[41], rd[42]);
925 swap(rd[43], rd[44]);
926 swap(rd[45], rd[46]);
927 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -0500928
929 sixaxis_parse_report(sc, rd, size);
930 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
931 size == 64) {
932 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +0200933 }
934
935 return 0;
936}
937
Colin Leitnerf04d5142013-05-27 23:41:05 +0200938static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
939 struct hid_field *field, struct hid_usage *usage,
940 unsigned long **bit, int *max)
941{
942 struct sony_sc *sc = hid_get_drvdata(hdev);
943
944 if (sc->quirks & BUZZ_CONTROLLER) {
945 unsigned int key = usage->hid & HID_USAGE;
946
947 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
948 return -1;
949
950 switch (usage->collection_index) {
951 case 1:
952 if (key >= ARRAY_SIZE(buzz_keymap))
953 return -1;
954
955 key = buzz_keymap[key];
956 if (!key)
957 return -1;
958 break;
959 default:
960 return -1;
961 }
962
963 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
964 return 1;
965 }
966
Jiri Kosina078328d2013-06-13 12:03:49 +0200967 if (sc->quirks & PS3REMOTE)
968 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
969
Benjamin Tissoires6f498012013-07-24 16:53:07 +0200970 /* Let hid-core decide for the others */
971 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200972}
973
Antonio Ospite5710fab2011-02-20 18:26:45 +0100974/*
975 * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
976 * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
977 * so we need to override that forcing HID Output Reports on the Control EP.
978 *
979 * There is also another issue about HID Output Reports via USB, the Sixaxis
980 * does not want the report_id as part of the data packet, so we have to
981 * discard buf[0] when sending the actual control message, even for numbered
982 * reports, humpf!
983 */
Antonio Ospite569b10a2010-10-19 16:13:10 +0200984static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
985 size_t count, unsigned char report_type)
986{
987 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
988 struct usb_device *dev = interface_to_usbdev(intf);
989 struct usb_host_interface *interface = intf->cur_altsetting;
990 int report_id = buf[0];
991 int ret;
992
Antonio Ospite5710fab2011-02-20 18:26:45 +0100993 if (report_type == HID_OUTPUT_REPORT) {
994 /* Don't send the Report ID */
995 buf++;
996 count--;
997 }
998
Antonio Ospite569b10a2010-10-19 16:13:10 +0200999 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1000 HID_REQ_SET_REPORT,
1001 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1002 ((report_type + 1) << 8) | report_id,
1003 interface->desc.bInterfaceNumber, buf, count,
1004 USB_CTRL_SET_TIMEOUT);
1005
Antonio Ospite5710fab2011-02-20 18:26:45 +01001006 /* Count also the Report ID, in case of an Output report. */
1007 if (ret > 0 && report_type == HID_OUTPUT_REPORT)
1008 ret++;
1009
Antonio Ospite569b10a2010-10-19 16:13:10 +02001010 return ret;
1011}
1012
Jiri Slabybd28ce02008-06-25 23:47:04 +02001013/*
1014 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1015 * to "operational". Without this, the ps3 controller will not report any
1016 * events.
1017 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001018static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001019{
Jiri Slabybd28ce02008-06-25 23:47:04 +02001020 int ret;
1021 char *buf = kmalloc(18, GFP_KERNEL);
1022
1023 if (!buf)
1024 return -ENOMEM;
1025
Benjamin Tissoirescafebc02014-02-05 16:33:22 -05001026 ret = hid_hw_raw_request(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT,
1027 HID_REQ_GET_REPORT);
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001028
Jiri Slabybd28ce02008-06-25 23:47:04 +02001029 if (ret < 0)
Joe Perches4291ee32010-12-09 19:29:03 -08001030 hid_err(hdev, "can't set operational mode\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +02001031
1032 kfree(buf);
1033
1034 return ret;
1035}
1036
Antonio Ospite816651a2010-05-03 22:15:55 +02001037static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001038{
Antonio Ospitefddb33f2010-05-03 17:19:03 +02001039 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
Benjamin Tissoires7e845d42014-02-05 16:33:23 -05001040 return hid_output_raw_report(hdev, buf, sizeof(buf),
1041 HID_FEATURE_REPORT);
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001042}
1043
Frank Praznik60781cf2014-01-11 15:13:15 -05001044static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001045{
1046 struct list_head *report_list =
1047 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1048 struct hid_report *report = list_entry(report_list->next,
1049 struct hid_report, list);
1050 __s32 *value = report->field[0]->value;
1051
1052 value[0] = 0x00;
Frank Praznik60781cf2014-01-11 15:13:15 -05001053 value[1] = leds[0] ? 0xff : 0x00;
1054 value[2] = leds[1] ? 0xff : 0x00;
1055 value[3] = leds[2] ? 0xff : 0x00;
1056 value[4] = leds[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001057 value[5] = 0x00;
1058 value[6] = 0x00;
1059 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1060}
1061
Frank Praznik60781cf2014-01-11 15:13:15 -05001062static void sony_set_leds(struct hid_device *hdev, const __u8 *leds, int count)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001063{
1064 struct sony_sc *drv_data = hid_get_drvdata(hdev);
Frank Praznik60781cf2014-01-11 15:13:15 -05001065 int n;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001066
Frank Praznik60781cf2014-01-11 15:13:15 -05001067 BUG_ON(count > MAX_LEDS);
1068
1069 if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001070 buzz_set_leds(hdev, leds);
Frank Praznik60781cf2014-01-11 15:13:15 -05001071 } else if ((drv_data->quirks & SIXAXIS_CONTROLLER_USB) ||
Frank Praznik8ab16762014-01-16 21:42:31 -05001072 (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB)) {
Frank Praznik60781cf2014-01-11 15:13:15 -05001073 for (n = 0; n < count; n++)
1074 drv_data->led_state[n] = leds[n];
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001075 schedule_work(&drv_data->state_worker);
1076 }
1077}
1078
Sven Eckelmannc5382512013-11-19 20:26:30 +01001079static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001080 enum led_brightness value)
1081{
1082 struct device *dev = led->dev->parent;
1083 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1084 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001085
1086 int n;
1087
1088 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001089 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001090 hid_err(hdev, "No device data\n");
1091 return;
1092 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001093
Frank Praznik60781cf2014-01-11 15:13:15 -05001094 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann2251b852013-11-19 20:26:31 +01001095 if (led == drv_data->leds[n]) {
Frank Praznik60781cf2014-01-11 15:13:15 -05001096 if (value != drv_data->led_state[n]) {
1097 drv_data->led_state[n] = value;
1098 sony_set_leds(hdev, drv_data->led_state, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001099 }
1100 break;
1101 }
1102 }
1103}
1104
Sven Eckelmannc5382512013-11-19 20:26:30 +01001105static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001106{
1107 struct device *dev = led->dev->parent;
1108 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1109 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001110
1111 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001112
1113 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001114 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001115 hid_err(hdev, "No device data\n");
1116 return LED_OFF;
1117 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001118
Frank Praznik60781cf2014-01-11 15:13:15 -05001119 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001120 if (led == drv_data->leds[n])
1121 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001122 }
1123
Simon Wood7db75042014-02-05 12:34:18 -07001124 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001125}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001126
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001127static void sony_leds_remove(struct hid_device *hdev)
1128{
1129 struct sony_sc *drv_data;
1130 struct led_classdev *led;
1131 int n;
1132
1133 drv_data = hid_get_drvdata(hdev);
1134 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
1135
Frank Praznik60781cf2014-01-11 15:13:15 -05001136 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001137 led = drv_data->leds[n];
1138 drv_data->leds[n] = NULL;
1139 if (!led)
1140 continue;
1141 led_classdev_unregister(led);
1142 kfree(led);
1143 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001144
1145 drv_data->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001146}
1147
Sven Eckelmannc5382512013-11-19 20:26:30 +01001148static int sony_leds_init(struct hid_device *hdev)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001149{
1150 struct sony_sc *drv_data;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001151 int n, ret = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -05001152 int max_brightness;
Frank Praznik61ebca92014-01-20 12:27:02 -05001153 int use_colors;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001154 struct led_classdev *led;
1155 size_t name_sz;
1156 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001157 size_t name_len;
1158 const char *name_fmt;
Frank Praznik61ebca92014-01-20 12:27:02 -05001159 static const char * const color_str[] = { "red", "green", "blue" };
Frank Praznik60781cf2014-01-11 15:13:15 -05001160 static const __u8 initial_values[MAX_LEDS] = { 0x00, 0x00, 0x00, 0x00 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001161
1162 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001163 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001164
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001165 if (drv_data->quirks & BUZZ_CONTROLLER) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001166 drv_data->led_count = 4;
1167 max_brightness = 1;
1168 use_colors = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001169 name_len = strlen("::buzz#");
1170 name_fmt = "%s::buzz%d";
1171 /* Validate expected report characteristics. */
1172 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1173 return -ENODEV;
Frank Praznik61ebca92014-01-20 12:27:02 -05001174 } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik60781cf2014-01-11 15:13:15 -05001175 drv_data->led_count = 3;
1176 max_brightness = 255;
Frank Praznik61ebca92014-01-20 12:27:02 -05001177 use_colors = 1;
1178 name_len = 0;
1179 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -05001180 } else {
1181 drv_data->led_count = 4;
1182 max_brightness = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001183 use_colors = 0;
1184 name_len = strlen("::sony#");
1185 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001186 }
1187
Colin Leitnerf04d5142013-05-27 23:41:05 +02001188 /* Clear LEDs as we have no way of reading their initial state. This is
1189 * only relevant if the driver is loaded after somebody actively set the
1190 * LEDs to on */
Frank Praznik60781cf2014-01-11 15:13:15 -05001191 sony_set_leds(hdev, initial_values, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001192
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001193 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001194
Frank Praznik60781cf2014-01-11 15:13:15 -05001195 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001196
1197 if (use_colors)
1198 name_sz = strlen(dev_name(&hdev->dev)) + strlen(color_str[n]) + 2;
1199
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001200 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1201 if (!led) {
1202 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001203 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001204 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001205 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001206
1207 name = (void *)(&led[1]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001208 if (use_colors)
1209 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), color_str[n]);
1210 else
1211 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001212 led->name = name;
1213 led->brightness = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -05001214 led->max_brightness = max_brightness;
Sven Eckelmannc5382512013-11-19 20:26:30 +01001215 led->brightness_get = sony_led_get_brightness;
1216 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001217
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001218 ret = led_classdev_register(&hdev->dev, led);
1219 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001220 hid_err(hdev, "Failed to register LED %d\n", n);
1221 kfree(led);
1222 goto error_leds;
1223 }
1224
Sven Eckelmann2251b852013-11-19 20:26:31 +01001225 drv_data->leds[n] = led;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001226 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001227
1228 return ret;
1229
Colin Leitnerf04d5142013-05-27 23:41:05 +02001230error_leds:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001231 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001232
Colin Leitnerf04d5142013-05-27 23:41:05 +02001233 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001234}
1235
Frank Praznikcad665a2014-01-11 15:13:54 -05001236static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001237{
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001238 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001239 unsigned char buf[] = {
1240 0x01,
1241 0x00, 0xff, 0x00, 0xff, 0x00,
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001242 0x00, 0x00, 0x00, 0x00, 0x00,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001243 0xff, 0x27, 0x10, 0x00, 0x32,
1244 0xff, 0x27, 0x10, 0x00, 0x32,
1245 0xff, 0x27, 0x10, 0x00, 0x32,
1246 0xff, 0x27, 0x10, 0x00, 0x32,
1247 0x00, 0x00, 0x00, 0x00, 0x00
1248 };
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001249
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001250#ifdef CONFIG_SONY_FF
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001251 buf[3] = sc->right ? 1 : 0;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001252 buf[5] = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001253#endif
1254
Frank Praznik60781cf2014-01-11 15:13:15 -05001255 buf[10] |= sc->led_state[0] << 1;
1256 buf[10] |= sc->led_state[1] << 2;
1257 buf[10] |= sc->led_state[2] << 3;
1258 buf[10] |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001259
Benjamin Tissoires7e845d42014-02-05 16:33:23 -05001260 hid_output_raw_report(sc->hdev, buf, sizeof(buf), HID_OUTPUT_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001261}
1262
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001263static void dualshock4_state_worker(struct work_struct *work)
1264{
1265 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001266 struct hid_device *hdev = sc->hdev;
Frank Praznik48220232014-02-05 20:03:44 -05001267 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001268
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001269 __u8 buf[78] = { 0 };
Frank Praznik48220232014-02-05 20:03:44 -05001270
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001271 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1272 buf[0] = 0x05;
1273 buf[1] = 0x03;
1274 offset = 4;
1275 } else {
1276 buf[0] = 0x11;
1277 buf[1] = 0xB0;
1278 buf[3] = 0x0F;
1279 offset = 6;
1280 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001281
1282#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001283 buf[offset++] = sc->right;
1284 buf[offset++] = sc->left;
1285#else
1286 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001287#endif
1288
Frank Praznik48220232014-02-05 20:03:44 -05001289 buf[offset++] = sc->led_state[0];
1290 buf[offset++] = sc->led_state[1];
1291 buf[offset++] = sc->led_state[2];
Frank Praznik60781cf2014-01-11 15:13:15 -05001292
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001293 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1294 hid_hw_output_report(hdev, buf, 32);
1295 else
1296 hid_hw_raw_request(hdev, 0x11, buf, 78,
1297 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001298}
1299
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001300#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001301static int sony_play_effect(struct input_dev *dev, void *data,
1302 struct ff_effect *effect)
1303{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001304 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001305 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001306
1307 if (effect->type != FF_RUMBLE)
1308 return 0;
1309
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001310 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001311 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001312
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001313 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001314 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001315}
1316
1317static int sony_init_ff(struct hid_device *hdev)
1318{
1319 struct hid_input *hidinput = list_entry(hdev->inputs.next,
1320 struct hid_input, list);
1321 struct input_dev *input_dev = hidinput->input;
1322
1323 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1324 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1325}
1326
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001327static void sony_destroy_ff(struct hid_device *hdev)
1328{
1329 struct sony_sc *sc = hid_get_drvdata(hdev);
1330
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001331 cancel_work_sync(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001332}
1333
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001334#else
1335static int sony_init_ff(struct hid_device *hdev)
1336{
1337 return 0;
1338}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001339
1340static void sony_destroy_ff(struct hid_device *hdev)
1341{
1342}
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001343#endif
1344
Frank Praznikd902f472014-01-27 10:17:36 -05001345static int sony_battery_get_property(struct power_supply *psy,
1346 enum power_supply_property psp,
1347 union power_supply_propval *val)
1348{
1349 struct sony_sc *sc = container_of(psy, struct sony_sc, battery);
1350 unsigned long flags;
1351 int ret = 0;
1352 u8 battery_charging, battery_capacity, cable_state;
1353
1354 spin_lock_irqsave(&sc->lock, flags);
1355 battery_charging = sc->battery_charging;
1356 battery_capacity = sc->battery_capacity;
1357 cable_state = sc->cable_state;
1358 spin_unlock_irqrestore(&sc->lock, flags);
1359
1360 switch (psp) {
1361 case POWER_SUPPLY_PROP_PRESENT:
1362 val->intval = 1;
1363 break;
1364 case POWER_SUPPLY_PROP_SCOPE:
1365 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1366 break;
1367 case POWER_SUPPLY_PROP_CAPACITY:
1368 val->intval = battery_capacity;
1369 break;
1370 case POWER_SUPPLY_PROP_STATUS:
1371 if (battery_charging)
1372 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1373 else
1374 if (battery_capacity == 100 && cable_state)
1375 val->intval = POWER_SUPPLY_STATUS_FULL;
1376 else
1377 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1378 break;
1379 default:
1380 ret = -EINVAL;
1381 break;
1382 }
1383 return ret;
1384}
1385
1386static int sony_battery_probe(struct sony_sc *sc)
1387{
1388 static atomic_t power_id_seq = ATOMIC_INIT(0);
1389 unsigned long power_id;
1390 struct hid_device *hdev = sc->hdev;
1391 int ret;
1392
1393 power_id = (unsigned long)atomic_inc_return(&power_id_seq);
1394
1395 sc->battery.properties = sony_battery_props;
1396 sc->battery.num_properties = ARRAY_SIZE(sony_battery_props);
1397 sc->battery.get_property = sony_battery_get_property;
1398 sc->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1399 sc->battery.use_for_apm = 0;
1400 sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%lu",
1401 power_id);
1402 if (!sc->battery.name)
1403 return -ENOMEM;
1404
1405 ret = power_supply_register(&hdev->dev, &sc->battery);
1406 if (ret) {
1407 hid_err(hdev, "Unable to register battery device\n");
1408 goto err_free;
1409 }
1410
1411 power_supply_powers(&sc->battery, &hdev->dev);
1412 return 0;
1413
1414err_free:
1415 kfree(sc->battery.name);
1416 sc->battery.name = NULL;
1417 return ret;
1418}
1419
1420static void sony_battery_remove(struct sony_sc *sc)
1421{
1422 if (!sc->battery.name)
1423 return;
1424
1425 power_supply_unregister(&sc->battery);
1426 kfree(sc->battery.name);
1427 sc->battery.name = NULL;
1428}
1429
Frank Praznike5606232014-01-27 10:17:37 -05001430static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
1431 int w, int h)
1432{
1433 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1434 struct hid_input, list);
1435 struct input_dev *input_dev = hidinput->input;
1436 int ret;
1437
1438 ret = input_mt_init_slots(input_dev, touch_count, 0);
1439 if (ret < 0) {
1440 hid_err(sc->hdev, "Unable to initialize multi-touch slots\n");
1441 return ret;
1442 }
1443
1444 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1445 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1446
1447 return 0;
1448}
1449
Jiri Slabybd28ce02008-06-25 23:47:04 +02001450static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1451{
1452 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001453 unsigned long quirks = id->driver_data;
1454 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001455 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001456
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001457 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001458 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08001459 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001460 return -ENOMEM;
1461 }
1462
1463 sc->quirks = quirks;
1464 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001465 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001466
Jiri Slabybd28ce02008-06-25 23:47:04 +02001467 ret = hid_parse(hdev);
1468 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001469 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001470 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001471 }
1472
Colin Leitnerf04d5142013-05-27 23:41:05 +02001473 if (sc->quirks & VAIO_RDESC_CONSTANT)
1474 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1475 else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
1476 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1477 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1478 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1479
1480 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001481 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001482 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001483 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001484 }
1485
Antonio Ospite569b10a2010-10-19 16:13:10 +02001486 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1487 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
Antonio Ospite816651a2010-05-03 22:15:55 +02001488 ret = sixaxis_set_operational_usb(hdev);
Frank Praznikcad665a2014-01-11 15:13:54 -05001489 INIT_WORK(&sc->state_worker, sixaxis_state_worker);
Antonio Ospite569b10a2010-10-19 16:13:10 +02001490 }
Antonio Ospite816651a2010-05-03 22:15:55 +02001491 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1492 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik8ab16762014-01-16 21:42:31 -05001493 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznike5606232014-01-27 10:17:37 -05001494 /* The Dualshock 4 touchpad supports 2 touches and has a
1495 * resolution of 1920x940.
1496 */
1497 ret = sony_register_touchpad(sc, 2, 1920, 940);
1498 if (ret < 0)
1499 goto err_stop;
1500
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001501 INIT_WORK(&sc->state_worker, dualshock4_state_worker);
1502 } else {
1503 ret = 0;
1504 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001505
Jiri Kosina4dfdc462008-12-30 00:49:59 +01001506 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001507 goto err_stop;
1508
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001509 if (sc->quirks & SONY_LED_SUPPORT) {
1510 ret = sony_leds_init(hdev);
1511 if (ret < 0)
1512 goto err_stop;
1513 }
1514
Frank Praznikd902f472014-01-27 10:17:36 -05001515 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1516 ret = sony_battery_probe(sc);
1517 if (ret < 0)
1518 goto err_stop;
1519
1520 /* Open the device to receive reports with battery info */
1521 ret = hid_hw_open(hdev);
1522 if (ret < 0) {
1523 hid_err(hdev, "hw open failed\n");
1524 goto err_stop;
1525 }
1526 }
1527
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001528 ret = sony_init_ff(hdev);
1529 if (ret < 0)
Frank Praznikd902f472014-01-27 10:17:36 -05001530 goto err_close;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001531
Jiri Slabybd28ce02008-06-25 23:47:04 +02001532 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001533err_close:
1534 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001535err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001536 if (sc->quirks & SONY_LED_SUPPORT)
1537 sony_leds_remove(hdev);
Frank Praznikd902f472014-01-27 10:17:36 -05001538 if (sc->quirks & SONY_BATTERY_SUPPORT)
1539 sony_battery_remove(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001540 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001541 return ret;
1542}
1543
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001544static void sony_remove(struct hid_device *hdev)
1545{
Colin Leitnerf04d5142013-05-27 23:41:05 +02001546 struct sony_sc *sc = hid_get_drvdata(hdev);
1547
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001548 if (sc->quirks & SONY_LED_SUPPORT)
Sven Eckelmannc5382512013-11-19 20:26:30 +01001549 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001550
Frank Praznikd902f472014-01-27 10:17:36 -05001551 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1552 hid_hw_close(hdev);
1553 sony_battery_remove(sc);
1554 }
1555
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001556 sony_destroy_ff(hdev);
1557
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001558 hid_hw_stop(hdev);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001559}
1560
Jiri Slabybd28ce02008-06-25 23:47:04 +02001561static const struct hid_device_id sony_devices[] = {
Antonio Ospite816651a2010-05-03 22:15:55 +02001562 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1563 .driver_data = SIXAXIS_CONTROLLER_USB },
Jiri Kosina35dca5b2011-04-28 15:43:13 +02001564 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
1565 .driver_data = SIXAXIS_CONTROLLER_USB },
Antonio Ospite816651a2010-05-03 22:15:55 +02001566 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1567 .driver_data = SIXAXIS_CONTROLLER_BT },
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001568 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
1569 .driver_data = VAIO_RDESC_CONSTANT },
Fernando Luis Vázquez Caoa46491842013-01-15 19:40:48 +09001570 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
1571 .driver_data = VAIO_RDESC_CONSTANT },
Colin Leitnerf04d5142013-05-27 23:41:05 +02001572 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
1573 * Logitech joystick from the device descriptor. */
1574 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
1575 .driver_data = BUZZ_CONTROLLER },
1576 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
1577 .driver_data = BUZZ_CONTROLLER },
Jiri Kosina078328d2013-06-13 12:03:49 +02001578 /* PS3 BD Remote Control */
1579 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
1580 .driver_data = PS3REMOTE },
1581 /* Logitech Harmony Adapter for PS3 */
1582 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
1583 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001584 /* Sony Dualshock 4 controllers for PS4 */
1585 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001586 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001587 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001588 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02001589 { }
1590};
1591MODULE_DEVICE_TABLE(hid, sony_devices);
1592
1593static struct hid_driver sony_driver = {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001594 .name = "sony",
1595 .id_table = sony_devices,
1596 .input_mapping = sony_mapping,
1597 .probe = sony_probe,
1598 .remove = sony_remove,
1599 .report_fixup = sony_report_fixup,
1600 .raw_event = sony_raw_event
Jiri Slabybd28ce02008-06-25 23:47:04 +02001601};
H Hartley Sweetenf4254582012-12-17 15:28:26 -07001602module_hid_driver(sony_driver);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001603
Jiri Slabybd28ce02008-06-25 23:47:04 +02001604MODULE_LICENSE("GPL");