blob: 75da56d0cfaca1138419e9ea77357b992585dc4f [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
Frank Praznikad142b92014-02-20 11:36:00 -050020/*
21 * NOTE: in order for the Sony PS3 BD Remote Control to be found by
Jiri Kosina078328d2013-06-13 12:03:49 +020022 * a Bluetooth host, the key combination Start+Enter has to be kept pressed
23 * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
24 *
25 * There will be no PIN request from the device.
26 */
27
Jiri Slabybd28ce02008-06-25 23:47:04 +020028#include <linux/device.h>
29#include <linux/hid.h>
30#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.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 Praznikd2d782f2014-02-20 11:36:03 -050035#include <linux/list.h>
Frank Praznik80250872014-04-14 10:11:35 -040036#include <linux/idr.h>
Frank Praznike5606232014-01-27 10:17:37 -050037#include <linux/input/mt.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020038
39#include "hid-ids.h"
40
Frank Praznik6c79c182014-01-16 21:43:03 -050041#define VAIO_RDESC_CONSTANT BIT(0)
42#define SIXAXIS_CONTROLLER_USB BIT(1)
43#define SIXAXIS_CONTROLLER_BT BIT(2)
44#define BUZZ_CONTROLLER BIT(3)
45#define PS3REMOTE BIT(4)
Frank Praznik8ab16762014-01-16 21:42:31 -050046#define DUALSHOCK4_CONTROLLER_USB BIT(5)
47#define DUALSHOCK4_CONTROLLER_BT BIT(6)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020048
Frank Praznikfee4e2d2014-02-18 17:22:01 -050049#define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
Frank Praznik68330d82014-02-05 20:03:49 -050050#define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
51 DUALSHOCK4_CONTROLLER_BT)
Frank Praznikfee4e2d2014-02-18 17:22:01 -050052#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
Frank Praznik68330d82014-02-05 20:03:49 -050053 DUALSHOCK4_CONTROLLER)
Frank Praznikfee4e2d2014-02-18 17:22:01 -050054#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER)
Frank Praznikc8de9db2014-02-20 11:36:01 -050055#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER)
Frank Praznik60781cf2014-01-11 15:13:15 -050056
57#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010058
Antonio Ospitec607fb82014-06-24 13:28:41 +020059static __u8 sixaxis_rdesc[] = {
Antonio Ospitefb705a62014-06-24 13:28:42 +020060 0x05, 0x01, /* Usage Page (Desktop), */
61 0x09, 0x04, /* Usage (Joystik), */
62 0xA1, 0x01, /* Collection (Application), */
63 0xA1, 0x02, /* Collection (Logical), */
64 0x85, 0x01, /* Report ID (1), */
65 0x75, 0x08, /* Report Size (8), */
66 0x95, 0x01, /* Report Count (1), */
67 0x15, 0x00, /* Logical Minimum (0), */
68 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
69 0x81, 0x03, /* Input (Constant, Variable), */
70 0x75, 0x01, /* Report Size (1), */
71 0x95, 0x13, /* Report Count (19), */
72 0x15, 0x00, /* Logical Minimum (0), */
73 0x25, 0x01, /* Logical Maximum (1), */
74 0x35, 0x00, /* Physical Minimum (0), */
75 0x45, 0x01, /* Physical Maximum (1), */
76 0x05, 0x09, /* Usage Page (Button), */
77 0x19, 0x01, /* Usage Minimum (01h), */
78 0x29, 0x13, /* Usage Maximum (13h), */
79 0x81, 0x02, /* Input (Variable), */
80 0x75, 0x01, /* Report Size (1), */
81 0x95, 0x0D, /* Report Count (13), */
82 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
83 0x81, 0x03, /* Input (Constant, Variable), */
84 0x15, 0x00, /* Logical Minimum (0), */
85 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
86 0x05, 0x01, /* Usage Page (Desktop), */
87 0x09, 0x01, /* Usage (Pointer), */
88 0xA1, 0x00, /* Collection (Physical), */
89 0x75, 0x08, /* Report Size (8), */
90 0x95, 0x04, /* Report Count (4), */
91 0x35, 0x00, /* Physical Minimum (0), */
92 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
93 0x09, 0x30, /* Usage (X), */
94 0x09, 0x31, /* Usage (Y), */
95 0x09, 0x32, /* Usage (Z), */
96 0x09, 0x35, /* Usage (Rz), */
97 0x81, 0x02, /* Input (Variable), */
98 0xC0, /* End Collection, */
99 0x05, 0x01, /* Usage Page (Desktop), */
100 0x95, 0x13, /* Report Count (19), */
101 0x09, 0x01, /* Usage (Pointer), */
102 0x81, 0x02, /* Input (Variable), */
103 0x95, 0x0C, /* Report Count (12), */
104 0x81, 0x01, /* Input (Constant), */
105 0x75, 0x10, /* Report Size (16), */
106 0x95, 0x04, /* Report Count (4), */
107 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
108 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
109 0x09, 0x01, /* Usage (Pointer), */
110 0x81, 0x02, /* Input (Variable), */
111 0xC0, /* End Collection, */
112 0xA1, 0x02, /* Collection (Logical), */
113 0x85, 0x02, /* Report ID (2), */
114 0x75, 0x08, /* Report Size (8), */
115 0x95, 0x30, /* Report Count (48), */
116 0x09, 0x01, /* Usage (Pointer), */
117 0xB1, 0x02, /* Feature (Variable), */
118 0xC0, /* End Collection, */
119 0xA1, 0x02, /* Collection (Logical), */
120 0x85, 0xEE, /* Report ID (238), */
121 0x75, 0x08, /* Report Size (8), */
122 0x95, 0x30, /* Report Count (48), */
123 0x09, 0x01, /* Usage (Pointer), */
124 0xB1, 0x02, /* Feature (Variable), */
125 0xC0, /* End Collection, */
126 0xA1, 0x02, /* Collection (Logical), */
127 0x85, 0xEF, /* Report ID (239), */
128 0x75, 0x08, /* Report Size (8), */
129 0x95, 0x30, /* Report Count (48), */
130 0x09, 0x01, /* Usage (Pointer), */
131 0xB1, 0x02, /* Feature (Variable), */
132 0xC0, /* End Collection, */
133 0xC0 /* End Collection */
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200134};
135
Frank Praznikad142b92014-02-20 11:36:00 -0500136/*
137 * The default descriptor doesn't provide mapping for the accelerometers
Frank Praznik58d70272014-01-20 12:27:01 -0500138 * or orientation sensors. This fixed descriptor maps the accelerometers
139 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
140 * to usage values 0x43, 0x44 and 0x45.
141 */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500142static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -0500143 0x05, 0x01, /* Usage Page (Desktop), */
144 0x09, 0x05, /* Usage (Gamepad), */
145 0xA1, 0x01, /* Collection (Application), */
146 0x85, 0x01, /* Report ID (1), */
147 0x09, 0x30, /* Usage (X), */
148 0x09, 0x31, /* Usage (Y), */
149 0x09, 0x32, /* Usage (Z), */
150 0x09, 0x35, /* Usage (Rz), */
151 0x15, 0x00, /* Logical Minimum (0), */
152 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
153 0x75, 0x08, /* Report Size (8), */
154 0x95, 0x04, /* Report Count (4), */
155 0x81, 0x02, /* Input (Variable), */
156 0x09, 0x39, /* Usage (Hat Switch), */
157 0x15, 0x00, /* Logical Minimum (0), */
158 0x25, 0x07, /* Logical Maximum (7), */
159 0x35, 0x00, /* Physical Minimum (0), */
160 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
161 0x65, 0x14, /* Unit (Degrees), */
162 0x75, 0x04, /* Report Size (4), */
163 0x95, 0x01, /* Report Count (1), */
164 0x81, 0x42, /* Input (Variable, Null State), */
165 0x65, 0x00, /* Unit, */
166 0x05, 0x09, /* Usage Page (Button), */
167 0x19, 0x01, /* Usage Minimum (01h), */
168 0x29, 0x0E, /* Usage Maximum (0Eh), */
169 0x15, 0x00, /* Logical Minimum (0), */
170 0x25, 0x01, /* Logical Maximum (1), */
171 0x75, 0x01, /* Report Size (1), */
172 0x95, 0x0E, /* Report Count (14), */
173 0x81, 0x02, /* Input (Variable), */
174 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
175 0x09, 0x20, /* Usage (20h), */
176 0x75, 0x06, /* Report Size (6), */
177 0x95, 0x01, /* Report Count (1), */
178 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400179 0x25, 0x3F, /* Logical Maximum (63), */
Frank Praznik58d70272014-01-20 12:27:01 -0500180 0x81, 0x02, /* Input (Variable), */
181 0x05, 0x01, /* Usage Page (Desktop), */
182 0x09, 0x33, /* Usage (Rx), */
183 0x09, 0x34, /* Usage (Ry), */
184 0x15, 0x00, /* Logical Minimum (0), */
185 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
186 0x75, 0x08, /* Report Size (8), */
187 0x95, 0x02, /* Report Count (2), */
188 0x81, 0x02, /* Input (Variable), */
189 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
190 0x09, 0x21, /* Usage (21h), */
191 0x95, 0x03, /* Report Count (3), */
192 0x81, 0x02, /* Input (Variable), */
193 0x05, 0x01, /* Usage Page (Desktop), */
194 0x19, 0x40, /* Usage Minimum (40h), */
195 0x29, 0x42, /* Usage Maximum (42h), */
196 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
197 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
198 0x75, 0x10, /* Report Size (16), */
199 0x95, 0x03, /* Report Count (3), */
200 0x81, 0x02, /* Input (Variable), */
201 0x19, 0x43, /* Usage Minimum (43h), */
202 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400203 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
204 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznik58d70272014-01-20 12:27:01 -0500205 0x95, 0x03, /* Report Count (3), */
206 0x81, 0x02, /* Input (Variable), */
207 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
208 0x09, 0x21, /* Usage (21h), */
209 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400210 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
Frank Praznik58d70272014-01-20 12:27:01 -0500211 0x75, 0x08, /* Report Size (8), */
212 0x95, 0x27, /* Report Count (39), */
213 0x81, 0x02, /* Input (Variable), */
214 0x85, 0x05, /* Report ID (5), */
215 0x09, 0x22, /* Usage (22h), */
216 0x95, 0x1F, /* Report Count (31), */
217 0x91, 0x02, /* Output (Variable), */
218 0x85, 0x04, /* Report ID (4), */
219 0x09, 0x23, /* Usage (23h), */
220 0x95, 0x24, /* Report Count (36), */
221 0xB1, 0x02, /* Feature (Variable), */
222 0x85, 0x02, /* Report ID (2), */
223 0x09, 0x24, /* Usage (24h), */
224 0x95, 0x24, /* Report Count (36), */
225 0xB1, 0x02, /* Feature (Variable), */
226 0x85, 0x08, /* Report ID (8), */
227 0x09, 0x25, /* Usage (25h), */
228 0x95, 0x03, /* Report Count (3), */
229 0xB1, 0x02, /* Feature (Variable), */
230 0x85, 0x10, /* Report ID (16), */
231 0x09, 0x26, /* Usage (26h), */
232 0x95, 0x04, /* Report Count (4), */
233 0xB1, 0x02, /* Feature (Variable), */
234 0x85, 0x11, /* Report ID (17), */
235 0x09, 0x27, /* Usage (27h), */
236 0x95, 0x02, /* Report Count (2), */
237 0xB1, 0x02, /* Feature (Variable), */
238 0x85, 0x12, /* Report ID (18), */
239 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
240 0x09, 0x21, /* Usage (21h), */
241 0x95, 0x0F, /* Report Count (15), */
242 0xB1, 0x02, /* Feature (Variable), */
243 0x85, 0x13, /* Report ID (19), */
244 0x09, 0x22, /* Usage (22h), */
245 0x95, 0x16, /* Report Count (22), */
246 0xB1, 0x02, /* Feature (Variable), */
247 0x85, 0x14, /* Report ID (20), */
248 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
249 0x09, 0x20, /* Usage (20h), */
250 0x95, 0x10, /* Report Count (16), */
251 0xB1, 0x02, /* Feature (Variable), */
252 0x85, 0x15, /* Report ID (21), */
253 0x09, 0x21, /* Usage (21h), */
254 0x95, 0x2C, /* Report Count (44), */
255 0xB1, 0x02, /* Feature (Variable), */
256 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
257 0x85, 0x80, /* Report ID (128), */
258 0x09, 0x20, /* Usage (20h), */
259 0x95, 0x06, /* Report Count (6), */
260 0xB1, 0x02, /* Feature (Variable), */
261 0x85, 0x81, /* Report ID (129), */
262 0x09, 0x21, /* Usage (21h), */
263 0x95, 0x06, /* Report Count (6), */
264 0xB1, 0x02, /* Feature (Variable), */
265 0x85, 0x82, /* Report ID (130), */
266 0x09, 0x22, /* Usage (22h), */
267 0x95, 0x05, /* Report Count (5), */
268 0xB1, 0x02, /* Feature (Variable), */
269 0x85, 0x83, /* Report ID (131), */
270 0x09, 0x23, /* Usage (23h), */
271 0x95, 0x01, /* Report Count (1), */
272 0xB1, 0x02, /* Feature (Variable), */
273 0x85, 0x84, /* Report ID (132), */
274 0x09, 0x24, /* Usage (24h), */
275 0x95, 0x04, /* Report Count (4), */
276 0xB1, 0x02, /* Feature (Variable), */
277 0x85, 0x85, /* Report ID (133), */
278 0x09, 0x25, /* Usage (25h), */
279 0x95, 0x06, /* Report Count (6), */
280 0xB1, 0x02, /* Feature (Variable), */
281 0x85, 0x86, /* Report ID (134), */
282 0x09, 0x26, /* Usage (26h), */
283 0x95, 0x06, /* Report Count (6), */
284 0xB1, 0x02, /* Feature (Variable), */
285 0x85, 0x87, /* Report ID (135), */
286 0x09, 0x27, /* Usage (27h), */
287 0x95, 0x23, /* Report Count (35), */
288 0xB1, 0x02, /* Feature (Variable), */
289 0x85, 0x88, /* Report ID (136), */
290 0x09, 0x28, /* Usage (28h), */
291 0x95, 0x22, /* Report Count (34), */
292 0xB1, 0x02, /* Feature (Variable), */
293 0x85, 0x89, /* Report ID (137), */
294 0x09, 0x29, /* Usage (29h), */
295 0x95, 0x02, /* Report Count (2), */
296 0xB1, 0x02, /* Feature (Variable), */
297 0x85, 0x90, /* Report ID (144), */
298 0x09, 0x30, /* Usage (30h), */
299 0x95, 0x05, /* Report Count (5), */
300 0xB1, 0x02, /* Feature (Variable), */
301 0x85, 0x91, /* Report ID (145), */
302 0x09, 0x31, /* Usage (31h), */
303 0x95, 0x03, /* Report Count (3), */
304 0xB1, 0x02, /* Feature (Variable), */
305 0x85, 0x92, /* Report ID (146), */
306 0x09, 0x32, /* Usage (32h), */
307 0x95, 0x03, /* Report Count (3), */
308 0xB1, 0x02, /* Feature (Variable), */
309 0x85, 0x93, /* Report ID (147), */
310 0x09, 0x33, /* Usage (33h), */
311 0x95, 0x0C, /* Report Count (12), */
312 0xB1, 0x02, /* Feature (Variable), */
313 0x85, 0xA0, /* Report ID (160), */
314 0x09, 0x40, /* Usage (40h), */
315 0x95, 0x06, /* Report Count (6), */
316 0xB1, 0x02, /* Feature (Variable), */
317 0x85, 0xA1, /* Report ID (161), */
318 0x09, 0x41, /* Usage (41h), */
319 0x95, 0x01, /* Report Count (1), */
320 0xB1, 0x02, /* Feature (Variable), */
321 0x85, 0xA2, /* Report ID (162), */
322 0x09, 0x42, /* Usage (42h), */
323 0x95, 0x01, /* Report Count (1), */
324 0xB1, 0x02, /* Feature (Variable), */
325 0x85, 0xA3, /* Report ID (163), */
326 0x09, 0x43, /* Usage (43h), */
327 0x95, 0x30, /* Report Count (48), */
328 0xB1, 0x02, /* Feature (Variable), */
329 0x85, 0xA4, /* Report ID (164), */
330 0x09, 0x44, /* Usage (44h), */
331 0x95, 0x0D, /* Report Count (13), */
332 0xB1, 0x02, /* Feature (Variable), */
333 0x85, 0xA5, /* Report ID (165), */
334 0x09, 0x45, /* Usage (45h), */
335 0x95, 0x15, /* Report Count (21), */
336 0xB1, 0x02, /* Feature (Variable), */
337 0x85, 0xA6, /* Report ID (166), */
338 0x09, 0x46, /* Usage (46h), */
339 0x95, 0x15, /* Report Count (21), */
340 0xB1, 0x02, /* Feature (Variable), */
341 0x85, 0xF0, /* Report ID (240), */
342 0x09, 0x47, /* Usage (47h), */
343 0x95, 0x3F, /* Report Count (63), */
344 0xB1, 0x02, /* Feature (Variable), */
345 0x85, 0xF1, /* Report ID (241), */
346 0x09, 0x48, /* Usage (48h), */
347 0x95, 0x3F, /* Report Count (63), */
348 0xB1, 0x02, /* Feature (Variable), */
349 0x85, 0xF2, /* Report ID (242), */
350 0x09, 0x49, /* Usage (49h), */
351 0x95, 0x0F, /* Report Count (15), */
352 0xB1, 0x02, /* Feature (Variable), */
353 0x85, 0xA7, /* Report ID (167), */
354 0x09, 0x4A, /* Usage (4Ah), */
355 0x95, 0x01, /* Report Count (1), */
356 0xB1, 0x02, /* Feature (Variable), */
357 0x85, 0xA8, /* Report ID (168), */
358 0x09, 0x4B, /* Usage (4Bh), */
359 0x95, 0x01, /* Report Count (1), */
360 0xB1, 0x02, /* Feature (Variable), */
361 0x85, 0xA9, /* Report ID (169), */
362 0x09, 0x4C, /* Usage (4Ch), */
363 0x95, 0x08, /* Report Count (8), */
364 0xB1, 0x02, /* Feature (Variable), */
365 0x85, 0xAA, /* Report ID (170), */
366 0x09, 0x4E, /* Usage (4Eh), */
367 0x95, 0x01, /* Report Count (1), */
368 0xB1, 0x02, /* Feature (Variable), */
369 0x85, 0xAB, /* Report ID (171), */
370 0x09, 0x4F, /* Usage (4Fh), */
371 0x95, 0x39, /* Report Count (57), */
372 0xB1, 0x02, /* Feature (Variable), */
373 0x85, 0xAC, /* Report ID (172), */
374 0x09, 0x50, /* Usage (50h), */
375 0x95, 0x39, /* Report Count (57), */
376 0xB1, 0x02, /* Feature (Variable), */
377 0x85, 0xAD, /* Report ID (173), */
378 0x09, 0x51, /* Usage (51h), */
379 0x95, 0x0B, /* Report Count (11), */
380 0xB1, 0x02, /* Feature (Variable), */
381 0x85, 0xAE, /* Report ID (174), */
382 0x09, 0x52, /* Usage (52h), */
383 0x95, 0x01, /* Report Count (1), */
384 0xB1, 0x02, /* Feature (Variable), */
385 0x85, 0xAF, /* Report ID (175), */
386 0x09, 0x53, /* Usage (53h), */
387 0x95, 0x02, /* Report Count (2), */
388 0xB1, 0x02, /* Feature (Variable), */
389 0x85, 0xB0, /* Report ID (176), */
390 0x09, 0x54, /* Usage (54h), */
391 0x95, 0x3F, /* Report Count (63), */
392 0xB1, 0x02, /* Feature (Variable), */
393 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500394};
395
Frank Praznikad142b92014-02-20 11:36:00 -0500396/*
397 * The default behavior of the Dualshock 4 is to send reports using report
Frank Praznikd8296742014-02-05 20:03:45 -0500398 * type 1 when running over Bluetooth. However, as soon as it receives a
399 * report of type 17 to set the LEDs or rumble it starts returning it's state
400 * in report 17 instead of 1. Since report 17 is undefined in the default HID
401 * descriptor the button and axis definitions must be moved to report 17 or
402 * the HID layer won't process the received input once a report is sent.
403 */
404static u8 dualshock4_bt_rdesc[] = {
405 0x05, 0x01, /* Usage Page (Desktop), */
406 0x09, 0x05, /* Usage (Gamepad), */
407 0xA1, 0x01, /* Collection (Application), */
408 0x85, 0x01, /* Report ID (1), */
409 0x75, 0x08, /* Report Size (8), */
410 0x95, 0x0A, /* Report Count (9), */
411 0x81, 0x02, /* Input (Variable), */
412 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
413 0x85, 0x02, /* Report ID (2), */
414 0x09, 0x24, /* Usage (24h), */
415 0x95, 0x24, /* Report Count (36), */
416 0xB1, 0x02, /* Feature (Variable), */
417 0x85, 0xA3, /* Report ID (163), */
418 0x09, 0x25, /* Usage (25h), */
419 0x95, 0x30, /* Report Count (48), */
420 0xB1, 0x02, /* Feature (Variable), */
421 0x85, 0x05, /* Report ID (5), */
422 0x09, 0x26, /* Usage (26h), */
423 0x95, 0x28, /* Report Count (40), */
424 0xB1, 0x02, /* Feature (Variable), */
425 0x85, 0x06, /* Report ID (6), */
426 0x09, 0x27, /* Usage (27h), */
427 0x95, 0x34, /* Report Count (52), */
428 0xB1, 0x02, /* Feature (Variable), */
429 0x85, 0x07, /* Report ID (7), */
430 0x09, 0x28, /* Usage (28h), */
431 0x95, 0x30, /* Report Count (48), */
432 0xB1, 0x02, /* Feature (Variable), */
433 0x85, 0x08, /* Report ID (8), */
434 0x09, 0x29, /* Usage (29h), */
435 0x95, 0x2F, /* Report Count (47), */
436 0xB1, 0x02, /* Feature (Variable), */
437 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
438 0x85, 0x03, /* Report ID (3), */
439 0x09, 0x21, /* Usage (21h), */
440 0x95, 0x26, /* Report Count (38), */
441 0xB1, 0x02, /* Feature (Variable), */
442 0x85, 0x04, /* Report ID (4), */
443 0x09, 0x22, /* Usage (22h), */
444 0x95, 0x2E, /* Report Count (46), */
445 0xB1, 0x02, /* Feature (Variable), */
446 0x85, 0xF0, /* Report ID (240), */
447 0x09, 0x47, /* Usage (47h), */
448 0x95, 0x3F, /* Report Count (63), */
449 0xB1, 0x02, /* Feature (Variable), */
450 0x85, 0xF1, /* Report ID (241), */
451 0x09, 0x48, /* Usage (48h), */
452 0x95, 0x3F, /* Report Count (63), */
453 0xB1, 0x02, /* Feature (Variable), */
454 0x85, 0xF2, /* Report ID (242), */
455 0x09, 0x49, /* Usage (49h), */
456 0x95, 0x0F, /* Report Count (15), */
457 0xB1, 0x02, /* Feature (Variable), */
458 0x85, 0x11, /* Report ID (17), */
459 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
460 0x09, 0x20, /* Usage (20h), */
461 0x95, 0x02, /* Report Count (2), */
462 0x81, 0x02, /* Input (Variable), */
463 0x05, 0x01, /* Usage Page (Desktop), */
464 0x09, 0x30, /* Usage (X), */
465 0x09, 0x31, /* Usage (Y), */
466 0x09, 0x32, /* Usage (Z), */
467 0x09, 0x35, /* Usage (Rz), */
468 0x15, 0x00, /* Logical Minimum (0), */
469 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
470 0x75, 0x08, /* Report Size (8), */
471 0x95, 0x04, /* Report Count (4), */
472 0x81, 0x02, /* Input (Variable), */
473 0x09, 0x39, /* Usage (Hat Switch), */
474 0x15, 0x00, /* Logical Minimum (0), */
475 0x25, 0x07, /* Logical Maximum (7), */
476 0x75, 0x04, /* Report Size (4), */
477 0x95, 0x01, /* Report Count (1), */
478 0x81, 0x42, /* Input (Variable, Null State), */
479 0x05, 0x09, /* Usage Page (Button), */
480 0x19, 0x01, /* Usage Minimum (01h), */
481 0x29, 0x0E, /* Usage Maximum (0Eh), */
482 0x15, 0x00, /* Logical Minimum (0), */
483 0x25, 0x01, /* Logical Maximum (1), */
484 0x75, 0x01, /* Report Size (1), */
485 0x95, 0x0E, /* Report Count (14), */
486 0x81, 0x02, /* Input (Variable), */
487 0x75, 0x06, /* Report Size (6), */
488 0x95, 0x01, /* Report Count (1), */
489 0x81, 0x01, /* Input (Constant), */
490 0x05, 0x01, /* Usage Page (Desktop), */
491 0x09, 0x33, /* Usage (Rx), */
492 0x09, 0x34, /* Usage (Ry), */
493 0x15, 0x00, /* Logical Minimum (0), */
494 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
495 0x75, 0x08, /* Report Size (8), */
496 0x95, 0x02, /* Report Count (2), */
497 0x81, 0x02, /* Input (Variable), */
498 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
499 0x09, 0x20, /* Usage (20h), */
500 0x95, 0x03, /* Report Count (3), */
501 0x81, 0x02, /* Input (Variable), */
502 0x05, 0x01, /* Usage Page (Desktop), */
503 0x19, 0x40, /* Usage Minimum (40h), */
504 0x29, 0x42, /* Usage Maximum (42h), */
505 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
506 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
507 0x75, 0x10, /* Report Size (16), */
508 0x95, 0x03, /* Report Count (3), */
509 0x81, 0x02, /* Input (Variable), */
510 0x19, 0x43, /* Usage Minimum (43h), */
511 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400512 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
513 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznikd8296742014-02-05 20:03:45 -0500514 0x95, 0x03, /* Report Count (3), */
515 0x81, 0x02, /* Input (Variable), */
516 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
517 0x09, 0x20, /* Usage (20h), */
518 0x15, 0x00, /* Logical Minimum (0), */
519 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
520 0x75, 0x08, /* Report Size (8), */
521 0x95, 0x31, /* Report Count (51), */
522 0x81, 0x02, /* Input (Variable), */
523 0x09, 0x21, /* Usage (21h), */
524 0x75, 0x08, /* Report Size (8), */
525 0x95, 0x4D, /* Report Count (77), */
526 0x91, 0x02, /* Output (Variable), */
527 0x85, 0x12, /* Report ID (18), */
528 0x09, 0x22, /* Usage (22h), */
529 0x95, 0x8D, /* Report Count (141), */
530 0x81, 0x02, /* Input (Variable), */
531 0x09, 0x23, /* Usage (23h), */
532 0x91, 0x02, /* Output (Variable), */
533 0x85, 0x13, /* Report ID (19), */
534 0x09, 0x24, /* Usage (24h), */
535 0x95, 0xCD, /* Report Count (205), */
536 0x81, 0x02, /* Input (Variable), */
537 0x09, 0x25, /* Usage (25h), */
538 0x91, 0x02, /* Output (Variable), */
539 0x85, 0x14, /* Report ID (20), */
540 0x09, 0x26, /* Usage (26h), */
541 0x96, 0x0D, 0x01, /* Report Count (269), */
542 0x81, 0x02, /* Input (Variable), */
543 0x09, 0x27, /* Usage (27h), */
544 0x91, 0x02, /* Output (Variable), */
545 0x85, 0x15, /* Report ID (21), */
546 0x09, 0x28, /* Usage (28h), */
547 0x96, 0x4D, 0x01, /* Report Count (333), */
548 0x81, 0x02, /* Input (Variable), */
549 0x09, 0x29, /* Usage (29h), */
550 0x91, 0x02, /* Output (Variable), */
551 0x85, 0x16, /* Report ID (22), */
552 0x09, 0x2A, /* Usage (2Ah), */
553 0x96, 0x8D, 0x01, /* Report Count (397), */
554 0x81, 0x02, /* Input (Variable), */
555 0x09, 0x2B, /* Usage (2Bh), */
556 0x91, 0x02, /* Output (Variable), */
557 0x85, 0x17, /* Report ID (23), */
558 0x09, 0x2C, /* Usage (2Ch), */
559 0x96, 0xCD, 0x01, /* Report Count (461), */
560 0x81, 0x02, /* Input (Variable), */
561 0x09, 0x2D, /* Usage (2Dh), */
562 0x91, 0x02, /* Output (Variable), */
563 0x85, 0x18, /* Report ID (24), */
564 0x09, 0x2E, /* Usage (2Eh), */
565 0x96, 0x0D, 0x02, /* Report Count (525), */
566 0x81, 0x02, /* Input (Variable), */
567 0x09, 0x2F, /* Usage (2Fh), */
568 0x91, 0x02, /* Output (Variable), */
569 0x85, 0x19, /* Report ID (25), */
570 0x09, 0x30, /* Usage (30h), */
571 0x96, 0x22, 0x02, /* Report Count (546), */
572 0x81, 0x02, /* Input (Variable), */
573 0x09, 0x31, /* Usage (31h), */
574 0x91, 0x02, /* Output (Variable), */
575 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
576 0x85, 0x82, /* Report ID (130), */
577 0x09, 0x22, /* Usage (22h), */
578 0x95, 0x3F, /* Report Count (63), */
579 0xB1, 0x02, /* Feature (Variable), */
580 0x85, 0x83, /* Report ID (131), */
581 0x09, 0x23, /* Usage (23h), */
582 0xB1, 0x02, /* Feature (Variable), */
583 0x85, 0x84, /* Report ID (132), */
584 0x09, 0x24, /* Usage (24h), */
585 0xB1, 0x02, /* Feature (Variable), */
586 0x85, 0x90, /* Report ID (144), */
587 0x09, 0x30, /* Usage (30h), */
588 0xB1, 0x02, /* Feature (Variable), */
589 0x85, 0x91, /* Report ID (145), */
590 0x09, 0x31, /* Usage (31h), */
591 0xB1, 0x02, /* Feature (Variable), */
592 0x85, 0x92, /* Report ID (146), */
593 0x09, 0x32, /* Usage (32h), */
594 0xB1, 0x02, /* Feature (Variable), */
595 0x85, 0x93, /* Report ID (147), */
596 0x09, 0x33, /* Usage (33h), */
597 0xB1, 0x02, /* Feature (Variable), */
598 0x85, 0xA0, /* Report ID (160), */
599 0x09, 0x40, /* Usage (40h), */
600 0xB1, 0x02, /* Feature (Variable), */
601 0x85, 0xA4, /* Report ID (164), */
602 0x09, 0x44, /* Usage (44h), */
603 0xB1, 0x02, /* Feature (Variable), */
604 0xC0 /* End Collection */
605};
606
Jiri Kosina078328d2013-06-13 12:03:49 +0200607static __u8 ps3remote_rdesc[] = {
608 0x05, 0x01, /* GUsagePage Generic Desktop */
609 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
610 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
611
612 /* Use collection 1 for joypad buttons */
613 0xA1, 0x02, /* MCollection Logical (interrelated data) */
614
615 /* Ignore the 1st byte, maybe it is used for a controller
616 * number but it's not needed for correct operation */
617 0x75, 0x08, /* GReportSize 0x08 [8] */
618 0x95, 0x01, /* GReportCount 0x01 [1] */
619 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
620
621 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
622 * buttons multiple keypresses are allowed */
623 0x05, 0x09, /* GUsagePage Button */
624 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
625 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
626 0x14, /* GLogicalMinimum [0] */
627 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
628 0x75, 0x01, /* GReportSize 0x01 [1] */
629 0x95, 0x18, /* GReportCount 0x18 [24] */
630 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
631
632 0xC0, /* MEndCollection */
633
634 /* Use collection 2 for remote control buttons */
635 0xA1, 0x02, /* MCollection Logical (interrelated data) */
636
637 /* 5th byte is used for remote control buttons */
638 0x05, 0x09, /* GUsagePage Button */
639 0x18, /* LUsageMinimum [No button pressed] */
640 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
641 0x14, /* GLogicalMinimum [0] */
642 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
643 0x75, 0x08, /* GReportSize 0x08 [8] */
644 0x95, 0x01, /* GReportCount 0x01 [1] */
645 0x80, /* MInput */
646
647 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
648 * 0xff and 11th is for press indication */
649 0x75, 0x08, /* GReportSize 0x08 [8] */
650 0x95, 0x06, /* GReportCount 0x06 [6] */
651 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
652
653 /* 12th byte is for battery strength */
654 0x05, 0x06, /* GUsagePage Generic Device Controls */
655 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
656 0x14, /* GLogicalMinimum [0] */
657 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
658 0x75, 0x08, /* GReportSize 0x08 [8] */
659 0x95, 0x01, /* GReportCount 0x01 [1] */
660 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
661
662 0xC0, /* MEndCollection */
663
664 0xC0 /* MEndCollection [Game Pad] */
665};
666
667static const unsigned int ps3remote_keymap_joypad_buttons[] = {
668 [0x01] = KEY_SELECT,
669 [0x02] = BTN_THUMBL, /* L3 */
670 [0x03] = BTN_THUMBR, /* R3 */
671 [0x04] = BTN_START,
672 [0x05] = KEY_UP,
673 [0x06] = KEY_RIGHT,
674 [0x07] = KEY_DOWN,
675 [0x08] = KEY_LEFT,
676 [0x09] = BTN_TL2, /* L2 */
677 [0x0a] = BTN_TR2, /* R2 */
678 [0x0b] = BTN_TL, /* L1 */
679 [0x0c] = BTN_TR, /* R1 */
680 [0x0d] = KEY_OPTION, /* options/triangle */
681 [0x0e] = KEY_BACK, /* back/circle */
682 [0x0f] = BTN_0, /* cross */
683 [0x10] = KEY_SCREEN, /* view/square */
684 [0x11] = KEY_HOMEPAGE, /* PS button */
685 [0x14] = KEY_ENTER,
686};
687static const unsigned int ps3remote_keymap_remote_buttons[] = {
688 [0x00] = KEY_1,
689 [0x01] = KEY_2,
690 [0x02] = KEY_3,
691 [0x03] = KEY_4,
692 [0x04] = KEY_5,
693 [0x05] = KEY_6,
694 [0x06] = KEY_7,
695 [0x07] = KEY_8,
696 [0x08] = KEY_9,
697 [0x09] = KEY_0,
698 [0x0e] = KEY_ESC, /* return */
699 [0x0f] = KEY_CLEAR,
700 [0x16] = KEY_EJECTCD,
701 [0x1a] = KEY_MENU, /* top menu */
702 [0x28] = KEY_TIME,
703 [0x30] = KEY_PREVIOUS,
704 [0x31] = KEY_NEXT,
705 [0x32] = KEY_PLAY,
706 [0x33] = KEY_REWIND, /* scan back */
707 [0x34] = KEY_FORWARD, /* scan forward */
708 [0x38] = KEY_STOP,
709 [0x39] = KEY_PAUSE,
710 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
711 [0x60] = KEY_FRAMEBACK, /* slow/step back */
712 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
713 [0x63] = KEY_SUBTITLE,
714 [0x64] = KEY_AUDIO,
715 [0x65] = KEY_ANGLE,
716 [0x70] = KEY_INFO, /* display */
717 [0x80] = KEY_BLUE,
718 [0x81] = KEY_RED,
719 [0x82] = KEY_GREEN,
720 [0x83] = KEY_YELLOW,
721};
722
Colin Leitnerf04d5142013-05-27 23:41:05 +0200723static const unsigned int buzz_keymap[] = {
Frank Praznikad142b92014-02-20 11:36:00 -0500724 /*
725 * The controller has 4 remote buzzers, each with one LED and 5
Colin Leitnerf04d5142013-05-27 23:41:05 +0200726 * buttons.
727 *
728 * We use the mapping chosen by the controller, which is:
729 *
730 * Key Offset
731 * -------------------
732 * Buzz 1
733 * Blue 5
734 * Orange 4
735 * Green 3
736 * Yellow 2
737 *
738 * So, for example, the orange button on the third buzzer is mapped to
739 * BTN_TRIGGER_HAPPY14
740 */
741 [ 1] = BTN_TRIGGER_HAPPY1,
742 [ 2] = BTN_TRIGGER_HAPPY2,
743 [ 3] = BTN_TRIGGER_HAPPY3,
744 [ 4] = BTN_TRIGGER_HAPPY4,
745 [ 5] = BTN_TRIGGER_HAPPY5,
746 [ 6] = BTN_TRIGGER_HAPPY6,
747 [ 7] = BTN_TRIGGER_HAPPY7,
748 [ 8] = BTN_TRIGGER_HAPPY8,
749 [ 9] = BTN_TRIGGER_HAPPY9,
750 [10] = BTN_TRIGGER_HAPPY10,
751 [11] = BTN_TRIGGER_HAPPY11,
752 [12] = BTN_TRIGGER_HAPPY12,
753 [13] = BTN_TRIGGER_HAPPY13,
754 [14] = BTN_TRIGGER_HAPPY14,
755 [15] = BTN_TRIGGER_HAPPY15,
756 [16] = BTN_TRIGGER_HAPPY16,
757 [17] = BTN_TRIGGER_HAPPY17,
758 [18] = BTN_TRIGGER_HAPPY18,
759 [19] = BTN_TRIGGER_HAPPY19,
760 [20] = BTN_TRIGGER_HAPPY20,
761};
762
Frank Praznikd902f472014-01-27 10:17:36 -0500763static enum power_supply_property sony_battery_props[] = {
764 POWER_SUPPLY_PROP_PRESENT,
765 POWER_SUPPLY_PROP_CAPACITY,
766 POWER_SUPPLY_PROP_SCOPE,
767 POWER_SUPPLY_PROP_STATUS,
768};
769
Frank Praznik55d3b662014-04-14 10:11:32 -0400770struct sixaxis_led {
771 __u8 time_enabled; /* the total time the led is active (0xff means forever) */
772 __u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
773 __u8 enabled;
774 __u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
775 __u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
776} __packed;
777
778struct sixaxis_rumble {
779 __u8 padding;
780 __u8 right_duration; /* Right motor duration (0xff means forever) */
781 __u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
782 __u8 left_duration; /* Left motor duration (0xff means forever) */
783 __u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
784} __packed;
785
786struct sixaxis_output_report {
787 __u8 report_id;
788 struct sixaxis_rumble rumble;
789 __u8 padding[4];
790 __u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
791 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
792 struct sixaxis_led _reserved; /* LED5, not actually soldered */
793} __packed;
794
795union sixaxis_output_report_01 {
796 struct sixaxis_output_report data;
797 __u8 buf[36];
798};
799
Frank Praznikd2d782f2014-02-20 11:36:03 -0500800static spinlock_t sony_dev_list_lock;
801static LIST_HEAD(sony_device_list);
Frank Praznik80250872014-04-14 10:11:35 -0400802static DEFINE_IDA(sony_device_id_allocator);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500803
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200804struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500805 spinlock_t lock;
Frank Praznikd2d782f2014-02-20 11:36:03 -0500806 struct list_head list_node;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100807 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500808 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200809 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100810 struct work_struct state_worker;
Frank Praznikd902f472014-01-27 10:17:36 -0500811 struct power_supply battery;
Frank Praznik80250872014-04-14 10:11:35 -0400812 int device_id;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200813
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100814#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100815 __u8 left;
816 __u8 right;
817#endif
818
Frank Praznikd2d782f2014-02-20 11:36:03 -0500819 __u8 mac_address[6];
Frank Praznik5f5750d2014-02-19 13:09:22 -0500820 __u8 worker_initialized;
Frank Praznikd902f472014-01-27 10:17:36 -0500821 __u8 cable_state;
822 __u8 battery_charging;
823 __u8 battery_capacity;
Frank Praznik60781cf2014-01-11 15:13:15 -0500824 __u8 led_state[MAX_LEDS];
Frank Praznikb3ed4582014-04-14 10:11:36 -0400825 __u8 led_delay_on[MAX_LEDS];
826 __u8 led_delay_off[MAX_LEDS];
Frank Praznik60781cf2014-01-11 15:13:15 -0500827 __u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200828};
829
Antonio Ospitec607fb82014-06-24 13:28:41 +0200830static __u8 *sixaxis_fixup(struct hid_device *hdev, __u8 *rdesc,
831 unsigned int *rsize)
832{
833 *rsize = sizeof(sixaxis_rdesc);
834 return sixaxis_rdesc;
835}
836
Jiri Kosina078328d2013-06-13 12:03:49 +0200837static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
838 unsigned int *rsize)
839{
840 *rsize = sizeof(ps3remote_rdesc);
841 return ps3remote_rdesc;
842}
843
844static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
845 struct hid_field *field, struct hid_usage *usage,
846 unsigned long **bit, int *max)
847{
848 unsigned int key = usage->hid & HID_USAGE;
849
850 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
851 return -1;
852
853 switch (usage->collection_index) {
854 case 1:
855 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
856 return -1;
857
858 key = ps3remote_keymap_joypad_buttons[key];
859 if (!key)
860 return -1;
861 break;
862 case 2:
863 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
864 return -1;
865
866 key = ps3remote_keymap_remote_buttons[key];
867 if (!key)
868 return -1;
869 break;
870 default:
871 return -1;
872 }
873
874 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
875 return 1;
876}
877
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400878static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
879 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200880{
881 struct sony_sc *sc = hid_get_drvdata(hdev);
882
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900883 /*
884 * Some Sony RF receivers wrongly declare the mouse pointer as a
885 * a constant non-data variable.
886 */
887 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
888 /* usage page: generic desktop controls */
889 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
890 /* usage: mouse */
891 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
892 /* input (usage page for x,y axes): constant, variable, relative */
893 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +0900894 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900895 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200896 rdesc[55] = 0x06;
897 }
Simon Wood61ab44b2011-06-10 12:00:26 +0200898
Frank Prazniked19d8c2014-01-16 21:43:12 -0500899 /*
900 * The default Dualshock 4 USB descriptor doesn't assign
901 * the gyroscope values to corresponding axes so we need a
902 * modified one.
903 */
904 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
905 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
906 rdesc = dualshock4_usb_rdesc;
907 *rsize = sizeof(dualshock4_usb_rdesc);
Frank Praznikd8296742014-02-05 20:03:45 -0500908 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
909 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
910 rdesc = dualshock4_bt_rdesc;
911 *rsize = sizeof(dualshock4_bt_rdesc);
Frank Prazniked19d8c2014-01-16 21:43:12 -0500912 }
913
Antonio Ospitec607fb82014-06-24 13:28:41 +0200914 if (sc->quirks & SIXAXIS_CONTROLLER)
915 return sixaxis_fixup(hdev, rdesc, rsize);
Jiri Kosina078328d2013-06-13 12:03:49 +0200916
917 if (sc->quirks & PS3REMOTE)
918 return ps3remote_fixup(hdev, rdesc, rsize);
919
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400920 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200921}
922
Frank Praznikd902f472014-01-27 10:17:36 -0500923static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
924{
925 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
926 unsigned long flags;
927 __u8 cable_state, battery_capacity, battery_charging;
928
Frank Praznikad142b92014-02-20 11:36:00 -0500929 /*
930 * The sixaxis is charging if the battery value is 0xee
Frank Praznikd902f472014-01-27 10:17:36 -0500931 * and it is fully charged if the value is 0xef.
932 * It does not report the actual level while charging so it
933 * is set to 100% while charging is in progress.
934 */
935 if (rd[30] >= 0xee) {
936 battery_capacity = 100;
Frank Praznika43e94a2014-02-15 13:35:42 -0500937 battery_charging = !(rd[30] & 0x01);
Frank Praznik9fddd742014-08-29 13:11:52 -0400938 cable_state = 1;
Frank Praznikd902f472014-01-27 10:17:36 -0500939 } else {
Frank Praznikac3c9a92014-02-20 11:36:02 -0500940 __u8 index = rd[30] <= 5 ? rd[30] : 5;
941 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -0500942 battery_charging = 0;
Frank Praznik9fddd742014-08-29 13:11:52 -0400943 cable_state = 0;
Frank Praznikd902f472014-01-27 10:17:36 -0500944 }
Frank Praznikd902f472014-01-27 10:17:36 -0500945
946 spin_lock_irqsave(&sc->lock, flags);
947 sc->cable_state = cable_state;
948 sc->battery_capacity = battery_capacity;
949 sc->battery_charging = battery_charging;
950 spin_unlock_irqrestore(&sc->lock, flags);
951}
952
953static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
954{
Frank Praznike5606232014-01-27 10:17:37 -0500955 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
956 struct hid_input, list);
957 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -0500958 unsigned long flags;
Frank Praznik6c5f8602014-02-05 20:03:47 -0500959 int n, offset;
Frank Praznikd902f472014-01-27 10:17:36 -0500960 __u8 cable_state, battery_capacity, battery_charging;
961
Frank Praznikad142b92014-02-20 11:36:00 -0500962 /*
963 * Battery and touchpad data starts at byte 30 in the USB report and
Frank Praznik6c5f8602014-02-05 20:03:47 -0500964 * 32 in Bluetooth report.
965 */
966 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
967
Frank Praznikad142b92014-02-20 11:36:00 -0500968 /*
969 * The lower 4 bits of byte 30 contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -0500970 * and the 5th bit contains the USB cable state.
971 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500972 cable_state = (rd[offset] >> 4) & 0x01;
973 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -0500974
Frank Praznikad142b92014-02-20 11:36:00 -0500975 /*
976 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -0500977 * 0 to 10, and when running on battery power it ranges from 0 to 9.
978 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -0500979 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500980 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -0500981 battery_charging = 0;
982 else
983 battery_charging = 1;
984
Frank Praznik6c5f8602014-02-05 20:03:47 -0500985 if (!cable_state)
986 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -0500987 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -0500988 battery_capacity = 10;
989
Frank Praznikd902f472014-01-27 10:17:36 -0500990 battery_capacity *= 10;
991
992 spin_lock_irqsave(&sc->lock, flags);
993 sc->cable_state = cable_state;
994 sc->battery_capacity = battery_capacity;
995 sc->battery_charging = battery_charging;
996 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -0500997
Frank Praznik6c5f8602014-02-05 20:03:47 -0500998 offset += 5;
999
Frank Praznikad142b92014-02-20 11:36:00 -05001000 /*
1001 * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
Frank Praznik6c5f8602014-02-05 20:03:47 -05001002 * and 37 on Bluetooth.
Frank Praznike5606232014-01-27 10:17:37 -05001003 * The first 7 bits of the first byte is a counter and bit 8 is a touch
1004 * indicator that is 0 when pressed and 1 when not pressed.
1005 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1006 * The data for the second touch is in the same format and immediatly
1007 * follows the data for the first.
1008 */
1009 for (n = 0; n < 2; n++) {
1010 __u16 x, y;
1011
1012 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1013 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1014
1015 input_mt_slot(input_dev, n);
1016 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
1017 !(rd[offset] >> 7));
1018 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1019 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1020
1021 offset += 4;
1022 }
Frank Praznikd902f472014-01-27 10:17:36 -05001023}
1024
Simon Woodc9e4d872011-06-10 12:00:27 +02001025static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
1026 __u8 *rd, int size)
1027{
1028 struct sony_sc *sc = hid_get_drvdata(hdev);
1029
Frank Praznikad142b92014-02-20 11:36:00 -05001030 /*
1031 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +02001032 * has to be BYTE_SWAPPED before passing up to joystick interface
1033 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001034 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Simon Woodc9e4d872011-06-10 12:00:27 +02001035 swap(rd[41], rd[42]);
1036 swap(rd[43], rd[44]);
1037 swap(rd[45], rd[46]);
1038 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -05001039
1040 sixaxis_parse_report(sc, rd, size);
Frank Praznik68330d82014-02-05 20:03:49 -05001041 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1042 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1043 && rd[0] == 0x11 && size == 78)) {
Frank Praznikd902f472014-01-27 10:17:36 -05001044 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +02001045 }
1046
1047 return 0;
1048}
1049
Colin Leitnerf04d5142013-05-27 23:41:05 +02001050static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1051 struct hid_field *field, struct hid_usage *usage,
1052 unsigned long **bit, int *max)
1053{
1054 struct sony_sc *sc = hid_get_drvdata(hdev);
1055
1056 if (sc->quirks & BUZZ_CONTROLLER) {
1057 unsigned int key = usage->hid & HID_USAGE;
1058
1059 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1060 return -1;
1061
1062 switch (usage->collection_index) {
1063 case 1:
1064 if (key >= ARRAY_SIZE(buzz_keymap))
1065 return -1;
1066
1067 key = buzz_keymap[key];
1068 if (!key)
1069 return -1;
1070 break;
1071 default:
1072 return -1;
1073 }
1074
1075 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1076 return 1;
1077 }
1078
Jiri Kosina078328d2013-06-13 12:03:49 +02001079 if (sc->quirks & PS3REMOTE)
1080 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1081
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001082 /* Let hid-core decide for the others */
1083 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001084}
1085
Antonio Ospite5710fab2011-02-20 18:26:45 +01001086/*
Jiri Slabybd28ce02008-06-25 23:47:04 +02001087 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1088 * to "operational". Without this, the ps3 controller will not report any
1089 * events.
1090 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001091static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001092{
Jiri Slabybd28ce02008-06-25 23:47:04 +02001093 int ret;
1094 char *buf = kmalloc(18, GFP_KERNEL);
1095
1096 if (!buf)
1097 return -ENOMEM;
1098
Benjamin Tissoirescafebc02014-02-05 16:33:22 -05001099 ret = hid_hw_raw_request(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT,
1100 HID_REQ_GET_REPORT);
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001101
Jiri Slabybd28ce02008-06-25 23:47:04 +02001102 if (ret < 0)
Joe Perches4291ee32010-12-09 19:29:03 -08001103 hid_err(hdev, "can't set operational mode\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +02001104
1105 kfree(buf);
1106
1107 return ret;
1108}
1109
Antonio Ospite816651a2010-05-03 22:15:55 +02001110static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001111{
Antonio Ospitefddb33f2010-05-03 17:19:03 +02001112 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
Benjamin Tissoiresb0dd72a2014-02-10 12:58:54 -05001113 return hid_hw_raw_request(hdev, buf[0], buf, sizeof(buf),
1114 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001115}
1116
Frank Praznikad142b92014-02-20 11:36:00 -05001117/*
1118 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
Frank Praznik68330d82014-02-05 20:03:49 -05001119 * controller so that it sends full input reports of type 0x11.
1120 */
1121static int dualshock4_set_operational_bt(struct hid_device *hdev)
1122{
1123 __u8 buf[37] = { 0 };
1124
1125 return hid_hw_raw_request(hdev, 0x02, buf, sizeof(buf),
1126 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001127}
1128
Frank Praznik80250872014-04-14 10:11:35 -04001129static void sixaxis_set_leds_from_id(int id, __u8 values[MAX_LEDS])
1130{
1131 static const __u8 sixaxis_leds[10][4] = {
1132 { 0x01, 0x00, 0x00, 0x00 },
1133 { 0x00, 0x01, 0x00, 0x00 },
1134 { 0x00, 0x00, 0x01, 0x00 },
1135 { 0x00, 0x00, 0x00, 0x01 },
1136 { 0x01, 0x00, 0x00, 0x01 },
1137 { 0x00, 0x01, 0x00, 0x01 },
1138 { 0x00, 0x00, 0x01, 0x01 },
1139 { 0x01, 0x00, 0x01, 0x01 },
1140 { 0x00, 0x01, 0x01, 0x01 },
1141 { 0x01, 0x01, 0x01, 0x01 }
1142 };
1143
1144 BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
1145
1146 if (id < 0)
1147 return;
1148
1149 id %= 10;
1150 memcpy(values, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
1151}
1152
1153static void dualshock4_set_leds_from_id(int id, __u8 values[MAX_LEDS])
1154{
1155 /* The first 4 color/index entries match what the PS4 assigns */
1156 static const __u8 color_code[7][3] = {
1157 /* Blue */ { 0x00, 0x00, 0x01 },
1158 /* Red */ { 0x01, 0x00, 0x00 },
1159 /* Green */ { 0x00, 0x01, 0x00 },
1160 /* Pink */ { 0x02, 0x00, 0x01 },
1161 /* Orange */ { 0x02, 0x01, 0x00 },
1162 /* Teal */ { 0x00, 0x01, 0x01 },
1163 /* White */ { 0x01, 0x01, 0x01 }
1164 };
1165
1166 BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
1167
1168 if (id < 0)
1169 return;
1170
1171 id %= 7;
1172 memcpy(values, color_code[id], sizeof(color_code[id]));
1173}
1174
Frank Praznik60781cf2014-01-11 15:13:15 -05001175static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001176{
1177 struct list_head *report_list =
1178 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1179 struct hid_report *report = list_entry(report_list->next,
1180 struct hid_report, list);
1181 __s32 *value = report->field[0]->value;
1182
1183 value[0] = 0x00;
Frank Praznik60781cf2014-01-11 15:13:15 -05001184 value[1] = leds[0] ? 0xff : 0x00;
1185 value[2] = leds[1] ? 0xff : 0x00;
1186 value[3] = leds[2] ? 0xff : 0x00;
1187 value[4] = leds[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001188 value[5] = 0x00;
1189 value[6] = 0x00;
1190 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1191}
1192
Frank Praznikfa57a812014-04-14 10:11:33 -04001193static void sony_set_leds(struct sony_sc *sc, const __u8 *leds, int count)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001194{
Frank Praznik60781cf2014-01-11 15:13:15 -05001195 int n;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001196
Frank Praznik60781cf2014-01-11 15:13:15 -05001197 BUG_ON(count > MAX_LEDS);
1198
Frank Praznikfa57a812014-04-14 10:11:33 -04001199 if (sc->quirks & BUZZ_CONTROLLER && count == 4) {
1200 buzz_set_leds(sc->hdev, leds);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001201 } else {
Frank Praznik60781cf2014-01-11 15:13:15 -05001202 for (n = 0; n < count; n++)
Frank Praznikfa57a812014-04-14 10:11:33 -04001203 sc->led_state[n] = leds[n];
1204 schedule_work(&sc->state_worker);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001205 }
1206}
1207
Sven Eckelmannc5382512013-11-19 20:26:30 +01001208static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001209 enum led_brightness value)
1210{
1211 struct device *dev = led->dev->parent;
1212 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1213 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001214
1215 int n;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001216 int force_update;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001217
1218 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001219 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001220 hid_err(hdev, "No device data\n");
1221 return;
1222 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001223
Frank Praznikb3ed4582014-04-14 10:11:36 -04001224 /*
1225 * The Sixaxis on USB will override any LED settings sent to it
1226 * and keep flashing all of the LEDs until the PS button is pressed.
1227 * Updates, even if redundant, must be always be sent to the
1228 * controller to avoid having to toggle the state of an LED just to
1229 * stop the flashing later on.
1230 */
1231 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1232
Frank Praznik60781cf2014-01-11 15:13:15 -05001233 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznikb3ed4582014-04-14 10:11:36 -04001234 if (led == drv_data->leds[n] && (force_update ||
1235 (value != drv_data->led_state[n] ||
1236 drv_data->led_delay_on[n] ||
1237 drv_data->led_delay_off[n]))) {
1238
1239 drv_data->led_state[n] = value;
1240
1241 /* Setting the brightness stops the blinking */
1242 drv_data->led_delay_on[n] = 0;
1243 drv_data->led_delay_off[n] = 0;
1244
1245 sony_set_leds(drv_data, drv_data->led_state,
1246 drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001247 break;
1248 }
1249 }
1250}
1251
Sven Eckelmannc5382512013-11-19 20:26:30 +01001252static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001253{
1254 struct device *dev = led->dev->parent;
1255 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1256 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001257
1258 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001259
1260 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001261 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001262 hid_err(hdev, "No device data\n");
1263 return LED_OFF;
1264 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001265
Frank Praznik60781cf2014-01-11 15:13:15 -05001266 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001267 if (led == drv_data->leds[n])
1268 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001269 }
1270
Simon Wood7db75042014-02-05 12:34:18 -07001271 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001272}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001273
Frank Praznikb3ed4582014-04-14 10:11:36 -04001274static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1275 unsigned long *delay_off)
1276{
1277 struct device *dev = led->dev->parent;
1278 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1279 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1280 int n;
1281 __u8 new_on, new_off;
1282
1283 if (!drv_data) {
1284 hid_err(hdev, "No device data\n");
1285 return -EINVAL;
1286 }
1287
1288 /* Max delay is 255 deciseconds or 2550 milliseconds */
1289 if (*delay_on > 2550)
1290 *delay_on = 2550;
1291 if (*delay_off > 2550)
1292 *delay_off = 2550;
1293
1294 /* Blink at 1 Hz if both values are zero */
1295 if (!*delay_on && !*delay_off)
1296 *delay_on = *delay_off = 500;
1297
1298 new_on = *delay_on / 10;
1299 new_off = *delay_off / 10;
1300
1301 for (n = 0; n < drv_data->led_count; n++) {
1302 if (led == drv_data->leds[n])
1303 break;
1304 }
1305
1306 /* This LED is not registered on this device */
1307 if (n >= drv_data->led_count)
1308 return -EINVAL;
1309
1310 /* Don't schedule work if the values didn't change */
1311 if (new_on != drv_data->led_delay_on[n] ||
1312 new_off != drv_data->led_delay_off[n]) {
1313 drv_data->led_delay_on[n] = new_on;
1314 drv_data->led_delay_off[n] = new_off;
1315 schedule_work(&drv_data->state_worker);
1316 }
1317
1318 return 0;
1319}
1320
Frank Praznikfa57a812014-04-14 10:11:33 -04001321static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001322{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001323 struct led_classdev *led;
1324 int n;
1325
Frank Praznikfa57a812014-04-14 10:11:33 -04001326 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001327
Frank Praznikfa57a812014-04-14 10:11:33 -04001328 for (n = 0; n < sc->led_count; n++) {
1329 led = sc->leds[n];
1330 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001331 if (!led)
1332 continue;
1333 led_classdev_unregister(led);
1334 kfree(led);
1335 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001336
Frank Praznikfa57a812014-04-14 10:11:33 -04001337 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001338}
1339
Frank Praznikfa57a812014-04-14 10:11:33 -04001340static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001341{
Frank Praznikfa57a812014-04-14 10:11:33 -04001342 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001343 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001344 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001345 struct led_classdev *led;
1346 size_t name_sz;
1347 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001348 size_t name_len;
1349 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001350 static const char * const ds4_name_str[] = { "red", "green", "blue",
1351 "global" };
Frank Praznik80250872014-04-14 10:11:35 -04001352 __u8 initial_values[MAX_LEDS] = { 0 };
Frank Praznik5607c892014-07-29 19:55:48 -07001353 __u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
Frank Praznikb3ed4582014-04-14 10:11:36 -04001354 __u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001355
Frank Praznikfa57a812014-04-14 10:11:33 -04001356 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001357
Frank Praznikfa57a812014-04-14 10:11:33 -04001358 if (sc->quirks & BUZZ_CONTROLLER) {
1359 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001360 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001361 name_len = strlen("::buzz#");
1362 name_fmt = "%s::buzz%d";
1363 /* Validate expected report characteristics. */
1364 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1365 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001366 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik80250872014-04-14 10:11:35 -04001367 dualshock4_set_leds_from_id(sc->device_id, initial_values);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001368 initial_values[3] = 1;
1369 sc->led_count = 4;
1370 memset(max_brightness, 255, 3);
1371 use_hw_blink[3] = 1;
1372 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001373 name_len = 0;
1374 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -05001375 } else {
Frank Praznik80250872014-04-14 10:11:35 -04001376 sixaxis_set_leds_from_id(sc->device_id, initial_values);
Frank Praznikfa57a812014-04-14 10:11:33 -04001377 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001378 memset(use_hw_blink, 1, 4);
1379 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001380 name_len = strlen("::sony#");
1381 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001382 }
1383
Frank Praznikad142b92014-02-20 11:36:00 -05001384 /*
1385 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001386 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001387 * LEDs to on
1388 */
Frank Praznikfa57a812014-04-14 10:11:33 -04001389 sony_set_leds(sc, initial_values, sc->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001390
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001391 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001392
Frank Praznikfa57a812014-04-14 10:11:33 -04001393 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001394
Frank Praznikb3ed4582014-04-14 10:11:36 -04001395 if (use_ds4_names)
1396 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001397
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001398 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1399 if (!led) {
1400 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001401 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001402 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001403 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001404
1405 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001406 if (use_ds4_names)
1407 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1408 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001409 else
1410 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001411 led->name = name;
Frank Praznik80250872014-04-14 10:11:35 -04001412 led->brightness = initial_values[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001413 led->max_brightness = max_brightness[n];
Sven Eckelmannc5382512013-11-19 20:26:30 +01001414 led->brightness_get = sony_led_get_brightness;
1415 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001416
Frank Praznikb3ed4582014-04-14 10:11:36 -04001417 if (use_hw_blink[n])
1418 led->blink_set = sony_led_blink_set;
1419
Frank Praznik80250872014-04-14 10:11:35 -04001420 sc->leds[n] = led;
1421
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001422 ret = led_classdev_register(&hdev->dev, led);
1423 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001424 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001425 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001426 kfree(led);
1427 goto error_leds;
1428 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001429 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001430
1431 return ret;
1432
Colin Leitnerf04d5142013-05-27 23:41:05 +02001433error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001434 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001435
Colin Leitnerf04d5142013-05-27 23:41:05 +02001436 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001437}
1438
Frank Praznikcad665a2014-01-11 15:13:54 -05001439static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001440{
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001441 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001442 int n;
Frank Praznik55d3b662014-04-14 10:11:32 -04001443 union sixaxis_output_report_01 report = {
1444 .buf = {
1445 0x01,
1446 0x00, 0xff, 0x00, 0xff, 0x00,
1447 0x00, 0x00, 0x00, 0x00, 0x00,
1448 0xff, 0x27, 0x10, 0x00, 0x32,
1449 0xff, 0x27, 0x10, 0x00, 0x32,
1450 0xff, 0x27, 0x10, 0x00, 0x32,
1451 0xff, 0x27, 0x10, 0x00, 0x32,
1452 0x00, 0x00, 0x00, 0x00, 0x00
1453 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001454 };
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001455
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001456#ifdef CONFIG_SONY_FF
Frank Praznik55d3b662014-04-14 10:11:32 -04001457 report.data.rumble.right_motor_on = sc->right ? 1 : 0;
1458 report.data.rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001459#endif
1460
Frank Praznik55d3b662014-04-14 10:11:32 -04001461 report.data.leds_bitmap |= sc->led_state[0] << 1;
1462 report.data.leds_bitmap |= sc->led_state[1] << 2;
1463 report.data.leds_bitmap |= sc->led_state[2] << 3;
1464 report.data.leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001465
Simon Wood88f65762014-04-14 10:11:37 -04001466 /* Set flag for all leds off, required for 3rd party INTEC controller */
1467 if ((report.data.leds_bitmap & 0x1E) == 0)
1468 report.data.leds_bitmap |= 0x20;
1469
Frank Praznikb3ed4582014-04-14 10:11:36 -04001470 /*
1471 * The LEDs in the report are indexed in reverse order to their
1472 * corresponding light on the controller.
1473 * Index 0 = LED 4, index 1 = LED 3, etc...
1474 *
1475 * In the case of both delay values being zero (blinking disabled) the
1476 * default report values should be used or the controller LED will be
1477 * always off.
1478 */
1479 for (n = 0; n < 4; n++) {
1480 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
1481 report.data.led[3 - n].duty_off = sc->led_delay_off[n];
1482 report.data.led[3 - n].duty_on = sc->led_delay_on[n];
1483 }
1484 }
1485
Frank Praznik55d3b662014-04-14 10:11:32 -04001486 hid_hw_raw_request(sc->hdev, report.data.report_id, report.buf,
1487 sizeof(report), HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001488}
1489
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001490static void dualshock4_state_worker(struct work_struct *work)
1491{
1492 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001493 struct hid_device *hdev = sc->hdev;
Frank Praznik48220232014-02-05 20:03:44 -05001494 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001495
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001496 __u8 buf[78] = { 0 };
Frank Praznik48220232014-02-05 20:03:44 -05001497
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001498 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1499 buf[0] = 0x05;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001500 buf[1] = 0xFF;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001501 offset = 4;
1502 } else {
1503 buf[0] = 0x11;
1504 buf[1] = 0xB0;
1505 buf[3] = 0x0F;
1506 offset = 6;
1507 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001508
1509#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001510 buf[offset++] = sc->right;
1511 buf[offset++] = sc->left;
1512#else
1513 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001514#endif
1515
Frank Praznikb3ed4582014-04-14 10:11:36 -04001516 /* LED 3 is the global control */
1517 if (sc->led_state[3]) {
1518 buf[offset++] = sc->led_state[0];
1519 buf[offset++] = sc->led_state[1];
1520 buf[offset++] = sc->led_state[2];
1521 } else {
1522 offset += 3;
1523 }
1524
1525 /* If both delay values are zero the DualShock 4 disables blinking. */
1526 buf[offset++] = sc->led_delay_on[3];
1527 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05001528
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001529 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1530 hid_hw_output_report(hdev, buf, 32);
1531 else
1532 hid_hw_raw_request(hdev, 0x11, buf, 78,
1533 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001534}
1535
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001536#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001537static int sony_play_effect(struct input_dev *dev, void *data,
1538 struct ff_effect *effect)
1539{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001540 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001541 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001542
1543 if (effect->type != FF_RUMBLE)
1544 return 0;
1545
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001546 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001547 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001548
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001549 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001550 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001551}
1552
Frank Praznikfa57a812014-04-14 10:11:33 -04001553static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001554{
Frank Praznikfa57a812014-04-14 10:11:33 -04001555 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001556 struct hid_input, list);
1557 struct input_dev *input_dev = hidinput->input;
1558
1559 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1560 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1561}
1562
1563#else
Frank Praznikfa57a812014-04-14 10:11:33 -04001564static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001565{
1566 return 0;
1567}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001568
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001569#endif
1570
Frank Praznikd902f472014-01-27 10:17:36 -05001571static int sony_battery_get_property(struct power_supply *psy,
1572 enum power_supply_property psp,
1573 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001574{
Frank Praznikd902f472014-01-27 10:17:36 -05001575 struct sony_sc *sc = container_of(psy, struct sony_sc, battery);
1576 unsigned long flags;
1577 int ret = 0;
1578 u8 battery_charging, battery_capacity, cable_state;
1579
1580 spin_lock_irqsave(&sc->lock, flags);
1581 battery_charging = sc->battery_charging;
1582 battery_capacity = sc->battery_capacity;
1583 cable_state = sc->cable_state;
1584 spin_unlock_irqrestore(&sc->lock, flags);
1585
1586 switch (psp) {
1587 case POWER_SUPPLY_PROP_PRESENT:
1588 val->intval = 1;
1589 break;
1590 case POWER_SUPPLY_PROP_SCOPE:
1591 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1592 break;
1593 case POWER_SUPPLY_PROP_CAPACITY:
1594 val->intval = battery_capacity;
1595 break;
1596 case POWER_SUPPLY_PROP_STATUS:
1597 if (battery_charging)
1598 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1599 else
1600 if (battery_capacity == 100 && cable_state)
1601 val->intval = POWER_SUPPLY_STATUS_FULL;
1602 else
1603 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1604 break;
1605 default:
1606 ret = -EINVAL;
1607 break;
1608 }
1609 return ret;
1610}
1611
1612static int sony_battery_probe(struct sony_sc *sc)
1613{
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001614 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05001615 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001616
Frank Praznikad142b92014-02-20 11:36:00 -05001617 /*
1618 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05001619 * if the battery is polled before the first device report is received.
1620 */
1621 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001622
Frank Praznikd902f472014-01-27 10:17:36 -05001623 sc->battery.properties = sony_battery_props;
1624 sc->battery.num_properties = ARRAY_SIZE(sony_battery_props);
1625 sc->battery.get_property = sony_battery_get_property;
1626 sc->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1627 sc->battery.use_for_apm = 0;
Frank Praznik314531f2014-04-14 10:11:34 -04001628 sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%pMR",
1629 sc->mac_address);
Frank Praznikd902f472014-01-27 10:17:36 -05001630 if (!sc->battery.name)
1631 return -ENOMEM;
1632
1633 ret = power_supply_register(&hdev->dev, &sc->battery);
1634 if (ret) {
1635 hid_err(hdev, "Unable to register battery device\n");
1636 goto err_free;
1637 }
1638
1639 power_supply_powers(&sc->battery, &hdev->dev);
1640 return 0;
1641
1642err_free:
1643 kfree(sc->battery.name);
1644 sc->battery.name = NULL;
1645 return ret;
1646}
1647
1648static void sony_battery_remove(struct sony_sc *sc)
1649{
1650 if (!sc->battery.name)
1651 return;
1652
1653 power_supply_unregister(&sc->battery);
1654 kfree(sc->battery.name);
1655 sc->battery.name = NULL;
1656}
1657
Frank Praznike5606232014-01-27 10:17:37 -05001658static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
1659 int w, int h)
1660{
1661 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1662 struct hid_input, list);
1663 struct input_dev *input_dev = hidinput->input;
1664 int ret;
1665
1666 ret = input_mt_init_slots(input_dev, touch_count, 0);
1667 if (ret < 0) {
1668 hid_err(sc->hdev, "Unable to initialize multi-touch slots\n");
1669 return ret;
1670 }
1671
1672 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1673 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1674
1675 return 0;
1676}
1677
Frank Praznikd2d782f2014-02-20 11:36:03 -05001678/*
1679 * If a controller is plugged in via USB while already connected via Bluetooth
1680 * it will show up as two devices. A global list of connected controllers and
1681 * their MAC addresses is maintained to ensure that a device is only connected
1682 * once.
1683 */
1684static int sony_check_add_dev_list(struct sony_sc *sc)
1685{
1686 struct sony_sc *entry;
1687 unsigned long flags;
1688 int ret;
1689
1690 spin_lock_irqsave(&sony_dev_list_lock, flags);
1691
1692 list_for_each_entry(entry, &sony_device_list, list_node) {
1693 ret = memcmp(sc->mac_address, entry->mac_address,
1694 sizeof(sc->mac_address));
1695 if (!ret) {
1696 ret = -EEXIST;
1697 hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
1698 sc->mac_address);
1699 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001700 }
1701 }
1702
Frank Praznikd2d782f2014-02-20 11:36:03 -05001703 ret = 0;
1704 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001705
Frank Praznikd2d782f2014-02-20 11:36:03 -05001706unlock:
1707 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1708 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001709}
1710
Frank Praznikd2d782f2014-02-20 11:36:03 -05001711static void sony_remove_dev_list(struct sony_sc *sc)
1712{
1713 unsigned long flags;
1714
1715 if (sc->list_node.next) {
1716 spin_lock_irqsave(&sony_dev_list_lock, flags);
1717 list_del(&(sc->list_node));
1718 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1719 }
1720}
1721
1722static int sony_get_bt_devaddr(struct sony_sc *sc)
1723{
1724 int ret;
1725
1726 /* HIDP stores the device MAC address as a string in the uniq field. */
1727 ret = strlen(sc->hdev->uniq);
1728 if (ret != 17)
1729 return -EINVAL;
1730
1731 ret = sscanf(sc->hdev->uniq,
1732 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1733 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
1734 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
1735
1736 if (ret != 6)
1737 return -EINVAL;
1738
1739 return 0;
1740}
1741
1742static int sony_check_add(struct sony_sc *sc)
1743{
1744 int n, ret;
1745
1746 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
1747 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
1748 /*
1749 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
1750 * address from the uniq string where HIDP stores it.
1751 * As uniq cannot be guaranteed to be a MAC address in all cases
1752 * a failure of this function should not prevent the connection.
1753 */
1754 if (sony_get_bt_devaddr(sc) < 0) {
1755 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
1756 return 0;
1757 }
1758 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1759 __u8 buf[7];
1760
1761 /*
1762 * The MAC address of a DS4 controller connected via USB can be
1763 * retrieved with feature report 0x81. The address begins at
1764 * offset 1.
1765 */
1766 ret = hid_hw_raw_request(sc->hdev, 0x81, buf, sizeof(buf),
1767 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1768
1769 if (ret != 7) {
1770 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
1771 return ret < 0 ? ret : -EINVAL;
1772 }
1773
1774 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
1775 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1776 __u8 buf[18];
1777
1778 /*
1779 * The MAC address of a Sixaxis controller connected via USB can
1780 * be retrieved with feature report 0xf2. The address begins at
1781 * offset 4.
1782 */
1783 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf, sizeof(buf),
1784 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1785
1786 if (ret != 18) {
1787 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
1788 return ret < 0 ? ret : -EINVAL;
1789 }
1790
1791 /*
1792 * The Sixaxis device MAC in the report is big-endian and must
1793 * be byte-swapped.
1794 */
1795 for (n = 0; n < 6; n++)
1796 sc->mac_address[5-n] = buf[4+n];
1797 } else {
1798 return 0;
1799 }
1800
1801 return sony_check_add_dev_list(sc);
1802}
1803
Frank Praznik80250872014-04-14 10:11:35 -04001804static int sony_set_device_id(struct sony_sc *sc)
1805{
1806 int ret;
1807
1808 /*
1809 * Only DualShock 4 or Sixaxis controllers get an id.
1810 * All others are set to -1.
1811 */
1812 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
1813 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
1814 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
1815 GFP_KERNEL);
1816 if (ret < 0) {
1817 sc->device_id = -1;
1818 return ret;
1819 }
1820 sc->device_id = ret;
1821 } else {
1822 sc->device_id = -1;
1823 }
1824
1825 return 0;
1826}
1827
1828static void sony_release_device_id(struct sony_sc *sc)
1829{
1830 if (sc->device_id >= 0) {
1831 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
1832 sc->device_id = -1;
1833 }
1834}
1835
Frank Praznik46262042014-04-14 10:11:31 -04001836static inline void sony_init_work(struct sony_sc *sc,
1837 void (*worker)(struct work_struct *))
1838{
1839 if (!sc->worker_initialized)
1840 INIT_WORK(&sc->state_worker, worker);
1841
1842 sc->worker_initialized = 1;
1843}
1844
1845static inline void sony_cancel_work_sync(struct sony_sc *sc)
1846{
1847 if (sc->worker_initialized)
1848 cancel_work_sync(&sc->state_worker);
1849}
Frank Praznikd2d782f2014-02-20 11:36:03 -05001850
Jiri Slabybd28ce02008-06-25 23:47:04 +02001851static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1852{
1853 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001854 unsigned long quirks = id->driver_data;
1855 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001856 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001857
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001858 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001859 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08001860 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001861 return -ENOMEM;
1862 }
1863
1864 sc->quirks = quirks;
1865 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001866 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001867
Jiri Slabybd28ce02008-06-25 23:47:04 +02001868 ret = hid_parse(hdev);
1869 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001870 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001871 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001872 }
1873
Colin Leitnerf04d5142013-05-27 23:41:05 +02001874 if (sc->quirks & VAIO_RDESC_CONSTANT)
1875 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
Antonio Ospite50764652014-06-24 13:28:40 +02001876 else if (sc->quirks & SIXAXIS_CONTROLLER)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001877 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1878
1879 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001880 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001881 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001882 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001883 }
1884
Frank Praznik80250872014-04-14 10:11:35 -04001885 ret = sony_set_device_id(sc);
1886 if (ret < 0) {
1887 hid_err(hdev, "failed to allocate the device id\n");
1888 goto err_stop;
1889 }
1890
Antonio Ospite569b10a2010-10-19 16:13:10 +02001891 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
Benjamin Tissoirese534a932014-03-08 22:52:42 -05001892 /*
1893 * The Sony Sixaxis does not handle HID Output Reports on the
1894 * Interrupt EP like it could, so we need to force HID Output
1895 * Reports to use HID_REQ_SET_REPORT on the Control EP.
1896 *
1897 * There is also another issue about HID Output Reports via USB,
1898 * the Sixaxis does not want the report_id as part of the data
1899 * packet, so we have to discard buf[0] when sending the actual
1900 * control message, even for numbered reports, humpf!
1901 */
1902 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
1903 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
Antonio Ospite816651a2010-05-03 22:15:55 +02001904 ret = sixaxis_set_operational_usb(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04001905 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001906 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04001907 /*
1908 * The Sixaxis wants output reports sent on the ctrl endpoint
1909 * when connected via Bluetooth.
1910 */
1911 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Antonio Ospite816651a2010-05-03 22:15:55 +02001912 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04001913 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001914 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik68330d82014-02-05 20:03:49 -05001915 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04001916 /*
1917 * The DualShock 4 wants output reports sent on the ctrl
1918 * endpoint when connected via Bluetooth.
1919 */
1920 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Frank Praznik68330d82014-02-05 20:03:49 -05001921 ret = dualshock4_set_operational_bt(hdev);
1922 if (ret < 0) {
1923 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
1924 goto err_stop;
1925 }
1926 }
Frank Praznikad142b92014-02-20 11:36:00 -05001927 /*
1928 * The Dualshock 4 touchpad supports 2 touches and has a
Frank Praznike5606232014-01-27 10:17:37 -05001929 * resolution of 1920x940.
1930 */
1931 ret = sony_register_touchpad(sc, 2, 1920, 940);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001932 if (ret < 0)
1933 goto err_stop;
1934
Frank Praznik46262042014-04-14 10:11:31 -04001935 sony_init_work(sc, dualshock4_state_worker);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001936 } else {
1937 ret = 0;
1938 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001939
Jiri Kosina4dfdc462008-12-30 00:49:59 +01001940 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001941 goto err_stop;
1942
Frank Praznikd2d782f2014-02-20 11:36:03 -05001943 ret = sony_check_add(sc);
1944 if (ret < 0)
1945 goto err_stop;
1946
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001947 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04001948 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001949 if (ret < 0)
1950 goto err_stop;
1951 }
1952
Frank Praznikd902f472014-01-27 10:17:36 -05001953 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1954 ret = sony_battery_probe(sc);
1955 if (ret < 0)
1956 goto err_stop;
1957
1958 /* Open the device to receive reports with battery info */
1959 ret = hid_hw_open(hdev);
1960 if (ret < 0) {
1961 hid_err(hdev, "hw open failed\n");
1962 goto err_stop;
1963 }
1964 }
1965
Frank Praznikc8de9db2014-02-20 11:36:01 -05001966 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04001967 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05001968 if (ret < 0)
1969 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05001970 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001971
Jiri Slabybd28ce02008-06-25 23:47:04 +02001972 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001973err_close:
1974 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001975err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001976 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04001977 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05001978 if (sc->quirks & SONY_BATTERY_SUPPORT)
1979 sony_battery_remove(sc);
Frank Praznik46262042014-04-14 10:11:31 -04001980 sony_cancel_work_sync(sc);
Frank Praznikd2d782f2014-02-20 11:36:03 -05001981 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04001982 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001983 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001984 return ret;
1985}
1986
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001987static void sony_remove(struct hid_device *hdev)
1988{
Colin Leitnerf04d5142013-05-27 23:41:05 +02001989 struct sony_sc *sc = hid_get_drvdata(hdev);
1990
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001991 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04001992 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001993
Frank Praznikd902f472014-01-27 10:17:36 -05001994 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1995 hid_hw_close(hdev);
1996 sony_battery_remove(sc);
1997 }
1998
Frank Praznik46262042014-04-14 10:11:31 -04001999 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002000
Frank Praznikd2d782f2014-02-20 11:36:03 -05002001 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002002
Frank Praznik80250872014-04-14 10:11:35 -04002003 sony_release_device_id(sc);
2004
Jiri Slabybd28ce02008-06-25 23:47:04 +02002005 hid_hw_stop(hdev);
2006}
2007
2008static const struct hid_device_id sony_devices[] = {
2009 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2010 .driver_data = SIXAXIS_CONTROLLER_USB },
2011 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2012 .driver_data = SIXAXIS_CONTROLLER_USB },
2013 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2014 .driver_data = SIXAXIS_CONTROLLER_BT },
2015 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2016 .driver_data = VAIO_RDESC_CONSTANT },
2017 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2018 .driver_data = VAIO_RDESC_CONSTANT },
2019 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2020 * Logitech joystick from the device descriptor. */
2021 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2022 .driver_data = BUZZ_CONTROLLER },
2023 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2024 .driver_data = BUZZ_CONTROLLER },
2025 /* PS3 BD Remote Control */
2026 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2027 .driver_data = PS3REMOTE },
2028 /* Logitech Harmony Adapter for PS3 */
2029 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2030 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002031 /* Sony Dualshock 4 controllers for PS4 */
2032 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002033 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002034 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002035 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002036 { }
2037};
2038MODULE_DEVICE_TABLE(hid, sony_devices);
2039
2040static struct hid_driver sony_driver = {
2041 .name = "sony",
2042 .id_table = sony_devices,
2043 .input_mapping = sony_mapping,
2044 .probe = sony_probe,
2045 .remove = sony_remove,
2046 .report_fixup = sony_report_fixup,
2047 .raw_event = sony_raw_event
2048};
Frank Praznik80250872014-04-14 10:11:35 -04002049
2050static int __init sony_init(void)
2051{
2052 dbg_hid("Sony:%s\n", __func__);
2053
2054 return hid_register_driver(&sony_driver);
2055}
2056
2057static void __exit sony_exit(void)
2058{
2059 dbg_hid("Sony:%s\n", __func__);
2060
2061 ida_destroy(&sony_device_id_allocator);
2062 hid_unregister_driver(&sony_driver);
2063}
2064module_init(sony_init);
2065module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002066
2067MODULE_LICENSE("GPL");