blob: c372368e438c7d486fa54b64d2f29ea49dcb218d [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), */
179 0x25, 0x7F, /* Logical Maximum (127), */
180 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), */
203 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
204 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
205 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), */
210 0x25, 0xFF, /* Logical Maximum (255), */
211 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), */
512 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
513 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
514 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 Praznikd902f472014-01-27 10:17:36 -0500938 } else {
Frank Praznikac3c9a92014-02-20 11:36:02 -0500939 __u8 index = rd[30] <= 5 ? rd[30] : 5;
940 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -0500941 battery_charging = 0;
942 }
Frank Praznik914c5782014-03-15 09:41:15 -0400943 cable_state = !(rd[31] & 0x04);
Frank Praznikd902f472014-01-27 10:17:36 -0500944
945 spin_lock_irqsave(&sc->lock, flags);
946 sc->cable_state = cable_state;
947 sc->battery_capacity = battery_capacity;
948 sc->battery_charging = battery_charging;
949 spin_unlock_irqrestore(&sc->lock, flags);
950}
951
952static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
953{
Frank Praznike5606232014-01-27 10:17:37 -0500954 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
955 struct hid_input, list);
956 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -0500957 unsigned long flags;
Frank Praznik6c5f8602014-02-05 20:03:47 -0500958 int n, offset;
Frank Praznikd902f472014-01-27 10:17:36 -0500959 __u8 cable_state, battery_capacity, battery_charging;
960
Frank Praznikad142b92014-02-20 11:36:00 -0500961 /*
962 * Battery and touchpad data starts at byte 30 in the USB report and
Frank Praznik6c5f8602014-02-05 20:03:47 -0500963 * 32 in Bluetooth report.
964 */
965 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
966
Frank Praznikad142b92014-02-20 11:36:00 -0500967 /*
968 * The lower 4 bits of byte 30 contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -0500969 * and the 5th bit contains the USB cable state.
970 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500971 cable_state = (rd[offset] >> 4) & 0x01;
972 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -0500973
Frank Praznikad142b92014-02-20 11:36:00 -0500974 /*
975 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -0500976 * 0 to 10, and when running on battery power it ranges from 0 to 9.
977 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -0500978 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500979 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -0500980 battery_charging = 0;
981 else
982 battery_charging = 1;
983
Frank Praznik6c5f8602014-02-05 20:03:47 -0500984 if (!cable_state)
985 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -0500986 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -0500987 battery_capacity = 10;
988
Frank Praznikd902f472014-01-27 10:17:36 -0500989 battery_capacity *= 10;
990
991 spin_lock_irqsave(&sc->lock, flags);
992 sc->cable_state = cable_state;
993 sc->battery_capacity = battery_capacity;
994 sc->battery_charging = battery_charging;
995 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -0500996
Frank Praznik6c5f8602014-02-05 20:03:47 -0500997 offset += 5;
998
Frank Praznikad142b92014-02-20 11:36:00 -0500999 /*
1000 * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
Frank Praznik6c5f8602014-02-05 20:03:47 -05001001 * and 37 on Bluetooth.
Frank Praznike5606232014-01-27 10:17:37 -05001002 * The first 7 bits of the first byte is a counter and bit 8 is a touch
1003 * indicator that is 0 when pressed and 1 when not pressed.
1004 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1005 * The data for the second touch is in the same format and immediatly
1006 * follows the data for the first.
1007 */
1008 for (n = 0; n < 2; n++) {
1009 __u16 x, y;
1010
1011 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1012 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1013
1014 input_mt_slot(input_dev, n);
1015 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
1016 !(rd[offset] >> 7));
1017 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1018 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1019
1020 offset += 4;
1021 }
Frank Praznikd902f472014-01-27 10:17:36 -05001022}
1023
Simon Woodc9e4d872011-06-10 12:00:27 +02001024static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
1025 __u8 *rd, int size)
1026{
1027 struct sony_sc *sc = hid_get_drvdata(hdev);
1028
Frank Praznikad142b92014-02-20 11:36:00 -05001029 /*
1030 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +02001031 * has to be BYTE_SWAPPED before passing up to joystick interface
1032 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001033 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Simon Woodc9e4d872011-06-10 12:00:27 +02001034 swap(rd[41], rd[42]);
1035 swap(rd[43], rd[44]);
1036 swap(rd[45], rd[46]);
1037 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -05001038
1039 sixaxis_parse_report(sc, rd, size);
Frank Praznik68330d82014-02-05 20:03:49 -05001040 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1041 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1042 && rd[0] == 0x11 && size == 78)) {
Frank Praznikd902f472014-01-27 10:17:36 -05001043 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +02001044 }
1045
1046 return 0;
1047}
1048
Colin Leitnerf04d5142013-05-27 23:41:05 +02001049static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1050 struct hid_field *field, struct hid_usage *usage,
1051 unsigned long **bit, int *max)
1052{
1053 struct sony_sc *sc = hid_get_drvdata(hdev);
1054
1055 if (sc->quirks & BUZZ_CONTROLLER) {
1056 unsigned int key = usage->hid & HID_USAGE;
1057
1058 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1059 return -1;
1060
1061 switch (usage->collection_index) {
1062 case 1:
1063 if (key >= ARRAY_SIZE(buzz_keymap))
1064 return -1;
1065
1066 key = buzz_keymap[key];
1067 if (!key)
1068 return -1;
1069 break;
1070 default:
1071 return -1;
1072 }
1073
1074 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1075 return 1;
1076 }
1077
Jiri Kosina078328d2013-06-13 12:03:49 +02001078 if (sc->quirks & PS3REMOTE)
1079 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1080
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001081 /* Let hid-core decide for the others */
1082 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001083}
1084
Antonio Ospite5710fab2011-02-20 18:26:45 +01001085/*
Jiri Slabybd28ce02008-06-25 23:47:04 +02001086 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1087 * to "operational". Without this, the ps3 controller will not report any
1088 * events.
1089 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001090static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001091{
Jiri Slabybd28ce02008-06-25 23:47:04 +02001092 int ret;
1093 char *buf = kmalloc(18, GFP_KERNEL);
1094
1095 if (!buf)
1096 return -ENOMEM;
1097
Benjamin Tissoirescafebc02014-02-05 16:33:22 -05001098 ret = hid_hw_raw_request(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT,
1099 HID_REQ_GET_REPORT);
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001100
Jiri Slabybd28ce02008-06-25 23:47:04 +02001101 if (ret < 0)
Joe Perches4291ee32010-12-09 19:29:03 -08001102 hid_err(hdev, "can't set operational mode\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +02001103
1104 kfree(buf);
1105
1106 return ret;
1107}
1108
Antonio Ospite816651a2010-05-03 22:15:55 +02001109static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001110{
Antonio Ospitefddb33f2010-05-03 17:19:03 +02001111 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
Benjamin Tissoiresb0dd72a2014-02-10 12:58:54 -05001112 return hid_hw_raw_request(hdev, buf[0], buf, sizeof(buf),
1113 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001114}
1115
Frank Praznikad142b92014-02-20 11:36:00 -05001116/*
1117 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
Frank Praznik68330d82014-02-05 20:03:49 -05001118 * controller so that it sends full input reports of type 0x11.
1119 */
1120static int dualshock4_set_operational_bt(struct hid_device *hdev)
1121{
1122 __u8 buf[37] = { 0 };
1123
1124 return hid_hw_raw_request(hdev, 0x02, buf, sizeof(buf),
1125 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001126}
1127
Frank Praznik80250872014-04-14 10:11:35 -04001128static void sixaxis_set_leds_from_id(int id, __u8 values[MAX_LEDS])
1129{
1130 static const __u8 sixaxis_leds[10][4] = {
1131 { 0x01, 0x00, 0x00, 0x00 },
1132 { 0x00, 0x01, 0x00, 0x00 },
1133 { 0x00, 0x00, 0x01, 0x00 },
1134 { 0x00, 0x00, 0x00, 0x01 },
1135 { 0x01, 0x00, 0x00, 0x01 },
1136 { 0x00, 0x01, 0x00, 0x01 },
1137 { 0x00, 0x00, 0x01, 0x01 },
1138 { 0x01, 0x00, 0x01, 0x01 },
1139 { 0x00, 0x01, 0x01, 0x01 },
1140 { 0x01, 0x01, 0x01, 0x01 }
1141 };
1142
1143 BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
1144
1145 if (id < 0)
1146 return;
1147
1148 id %= 10;
1149 memcpy(values, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
1150}
1151
1152static void dualshock4_set_leds_from_id(int id, __u8 values[MAX_LEDS])
1153{
1154 /* The first 4 color/index entries match what the PS4 assigns */
1155 static const __u8 color_code[7][3] = {
1156 /* Blue */ { 0x00, 0x00, 0x01 },
1157 /* Red */ { 0x01, 0x00, 0x00 },
1158 /* Green */ { 0x00, 0x01, 0x00 },
1159 /* Pink */ { 0x02, 0x00, 0x01 },
1160 /* Orange */ { 0x02, 0x01, 0x00 },
1161 /* Teal */ { 0x00, 0x01, 0x01 },
1162 /* White */ { 0x01, 0x01, 0x01 }
1163 };
1164
1165 BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
1166
1167 if (id < 0)
1168 return;
1169
1170 id %= 7;
1171 memcpy(values, color_code[id], sizeof(color_code[id]));
1172}
1173
Frank Praznik60781cf2014-01-11 15:13:15 -05001174static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001175{
1176 struct list_head *report_list =
1177 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1178 struct hid_report *report = list_entry(report_list->next,
1179 struct hid_report, list);
1180 __s32 *value = report->field[0]->value;
1181
1182 value[0] = 0x00;
Frank Praznik60781cf2014-01-11 15:13:15 -05001183 value[1] = leds[0] ? 0xff : 0x00;
1184 value[2] = leds[1] ? 0xff : 0x00;
1185 value[3] = leds[2] ? 0xff : 0x00;
1186 value[4] = leds[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001187 value[5] = 0x00;
1188 value[6] = 0x00;
1189 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1190}
1191
Frank Praznikfa57a812014-04-14 10:11:33 -04001192static void sony_set_leds(struct sony_sc *sc, const __u8 *leds, int count)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001193{
Frank Praznik60781cf2014-01-11 15:13:15 -05001194 int n;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001195
Frank Praznik60781cf2014-01-11 15:13:15 -05001196 BUG_ON(count > MAX_LEDS);
1197
Frank Praznikfa57a812014-04-14 10:11:33 -04001198 if (sc->quirks & BUZZ_CONTROLLER && count == 4) {
1199 buzz_set_leds(sc->hdev, leds);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001200 } else {
Frank Praznik60781cf2014-01-11 15:13:15 -05001201 for (n = 0; n < count; n++)
Frank Praznikfa57a812014-04-14 10:11:33 -04001202 sc->led_state[n] = leds[n];
1203 schedule_work(&sc->state_worker);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001204 }
1205}
1206
Sven Eckelmannc5382512013-11-19 20:26:30 +01001207static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001208 enum led_brightness value)
1209{
1210 struct device *dev = led->dev->parent;
1211 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1212 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001213
1214 int n;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001215 int force_update;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001216
1217 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001218 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001219 hid_err(hdev, "No device data\n");
1220 return;
1221 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001222
Frank Praznikb3ed4582014-04-14 10:11:36 -04001223 /*
1224 * The Sixaxis on USB will override any LED settings sent to it
1225 * and keep flashing all of the LEDs until the PS button is pressed.
1226 * Updates, even if redundant, must be always be sent to the
1227 * controller to avoid having to toggle the state of an LED just to
1228 * stop the flashing later on.
1229 */
1230 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1231
Frank Praznik60781cf2014-01-11 15:13:15 -05001232 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznikb3ed4582014-04-14 10:11:36 -04001233 if (led == drv_data->leds[n] && (force_update ||
1234 (value != drv_data->led_state[n] ||
1235 drv_data->led_delay_on[n] ||
1236 drv_data->led_delay_off[n]))) {
1237
1238 drv_data->led_state[n] = value;
1239
1240 /* Setting the brightness stops the blinking */
1241 drv_data->led_delay_on[n] = 0;
1242 drv_data->led_delay_off[n] = 0;
1243
1244 sony_set_leds(drv_data, drv_data->led_state,
1245 drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001246 break;
1247 }
1248 }
1249}
1250
Sven Eckelmannc5382512013-11-19 20:26:30 +01001251static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001252{
1253 struct device *dev = led->dev->parent;
1254 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1255 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001256
1257 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001258
1259 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001260 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001261 hid_err(hdev, "No device data\n");
1262 return LED_OFF;
1263 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001264
Frank Praznik60781cf2014-01-11 15:13:15 -05001265 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001266 if (led == drv_data->leds[n])
1267 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001268 }
1269
Simon Wood7db75042014-02-05 12:34:18 -07001270 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001271}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001272
Frank Praznikb3ed4582014-04-14 10:11:36 -04001273static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1274 unsigned long *delay_off)
1275{
1276 struct device *dev = led->dev->parent;
1277 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1278 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1279 int n;
1280 __u8 new_on, new_off;
1281
1282 if (!drv_data) {
1283 hid_err(hdev, "No device data\n");
1284 return -EINVAL;
1285 }
1286
1287 /* Max delay is 255 deciseconds or 2550 milliseconds */
1288 if (*delay_on > 2550)
1289 *delay_on = 2550;
1290 if (*delay_off > 2550)
1291 *delay_off = 2550;
1292
1293 /* Blink at 1 Hz if both values are zero */
1294 if (!*delay_on && !*delay_off)
1295 *delay_on = *delay_off = 500;
1296
1297 new_on = *delay_on / 10;
1298 new_off = *delay_off / 10;
1299
1300 for (n = 0; n < drv_data->led_count; n++) {
1301 if (led == drv_data->leds[n])
1302 break;
1303 }
1304
1305 /* This LED is not registered on this device */
1306 if (n >= drv_data->led_count)
1307 return -EINVAL;
1308
1309 /* Don't schedule work if the values didn't change */
1310 if (new_on != drv_data->led_delay_on[n] ||
1311 new_off != drv_data->led_delay_off[n]) {
1312 drv_data->led_delay_on[n] = new_on;
1313 drv_data->led_delay_off[n] = new_off;
1314 schedule_work(&drv_data->state_worker);
1315 }
1316
1317 return 0;
1318}
1319
Frank Praznikfa57a812014-04-14 10:11:33 -04001320static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001321{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001322 struct led_classdev *led;
1323 int n;
1324
Frank Praznikfa57a812014-04-14 10:11:33 -04001325 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001326
Frank Praznikfa57a812014-04-14 10:11:33 -04001327 for (n = 0; n < sc->led_count; n++) {
1328 led = sc->leds[n];
1329 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001330 if (!led)
1331 continue;
1332 led_classdev_unregister(led);
1333 kfree(led);
1334 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001335
Frank Praznikfa57a812014-04-14 10:11:33 -04001336 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001337}
1338
Frank Praznikfa57a812014-04-14 10:11:33 -04001339static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001340{
Frank Praznikfa57a812014-04-14 10:11:33 -04001341 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001342 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001343 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001344 struct led_classdev *led;
1345 size_t name_sz;
1346 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001347 size_t name_len;
1348 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001349 static const char * const ds4_name_str[] = { "red", "green", "blue",
1350 "global" };
Frank Praznik80250872014-04-14 10:11:35 -04001351 __u8 initial_values[MAX_LEDS] = { 0 };
Frank Praznik5607c892014-07-29 19:55:48 -07001352 __u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
Frank Praznikb3ed4582014-04-14 10:11:36 -04001353 __u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001354
Frank Praznikfa57a812014-04-14 10:11:33 -04001355 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001356
Frank Praznikfa57a812014-04-14 10:11:33 -04001357 if (sc->quirks & BUZZ_CONTROLLER) {
1358 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001359 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001360 name_len = strlen("::buzz#");
1361 name_fmt = "%s::buzz%d";
1362 /* Validate expected report characteristics. */
1363 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1364 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001365 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik80250872014-04-14 10:11:35 -04001366 dualshock4_set_leds_from_id(sc->device_id, initial_values);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001367 initial_values[3] = 1;
1368 sc->led_count = 4;
1369 memset(max_brightness, 255, 3);
1370 use_hw_blink[3] = 1;
1371 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001372 name_len = 0;
1373 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -05001374 } else {
Frank Praznik80250872014-04-14 10:11:35 -04001375 sixaxis_set_leds_from_id(sc->device_id, initial_values);
Frank Praznikfa57a812014-04-14 10:11:33 -04001376 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001377 memset(use_hw_blink, 1, 4);
1378 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001379 name_len = strlen("::sony#");
1380 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001381 }
1382
Frank Praznikad142b92014-02-20 11:36:00 -05001383 /*
1384 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001385 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001386 * LEDs to on
1387 */
Frank Praznikfa57a812014-04-14 10:11:33 -04001388 sony_set_leds(sc, initial_values, sc->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001389
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001390 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001391
Frank Praznikfa57a812014-04-14 10:11:33 -04001392 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001393
Frank Praznikb3ed4582014-04-14 10:11:36 -04001394 if (use_ds4_names)
1395 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001396
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001397 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1398 if (!led) {
1399 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001400 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001401 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001402 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001403
1404 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001405 if (use_ds4_names)
1406 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1407 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001408 else
1409 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001410 led->name = name;
Frank Praznik80250872014-04-14 10:11:35 -04001411 led->brightness = initial_values[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001412 led->max_brightness = max_brightness[n];
Sven Eckelmannc5382512013-11-19 20:26:30 +01001413 led->brightness_get = sony_led_get_brightness;
1414 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001415
Frank Praznikb3ed4582014-04-14 10:11:36 -04001416 if (use_hw_blink[n])
1417 led->blink_set = sony_led_blink_set;
1418
Frank Praznik80250872014-04-14 10:11:35 -04001419 sc->leds[n] = led;
1420
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001421 ret = led_classdev_register(&hdev->dev, led);
1422 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001423 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001424 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001425 kfree(led);
1426 goto error_leds;
1427 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001428 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001429
1430 return ret;
1431
Colin Leitnerf04d5142013-05-27 23:41:05 +02001432error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001433 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001434
Colin Leitnerf04d5142013-05-27 23:41:05 +02001435 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001436}
1437
Frank Praznikcad665a2014-01-11 15:13:54 -05001438static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001439{
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001440 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001441 int n;
Frank Praznik55d3b662014-04-14 10:11:32 -04001442 union sixaxis_output_report_01 report = {
1443 .buf = {
1444 0x01,
1445 0x00, 0xff, 0x00, 0xff, 0x00,
1446 0x00, 0x00, 0x00, 0x00, 0x00,
1447 0xff, 0x27, 0x10, 0x00, 0x32,
1448 0xff, 0x27, 0x10, 0x00, 0x32,
1449 0xff, 0x27, 0x10, 0x00, 0x32,
1450 0xff, 0x27, 0x10, 0x00, 0x32,
1451 0x00, 0x00, 0x00, 0x00, 0x00
1452 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001453 };
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001454
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001455#ifdef CONFIG_SONY_FF
Frank Praznik55d3b662014-04-14 10:11:32 -04001456 report.data.rumble.right_motor_on = sc->right ? 1 : 0;
1457 report.data.rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001458#endif
1459
Frank Praznik55d3b662014-04-14 10:11:32 -04001460 report.data.leds_bitmap |= sc->led_state[0] << 1;
1461 report.data.leds_bitmap |= sc->led_state[1] << 2;
1462 report.data.leds_bitmap |= sc->led_state[2] << 3;
1463 report.data.leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001464
Simon Wood88f65762014-04-14 10:11:37 -04001465 /* Set flag for all leds off, required for 3rd party INTEC controller */
1466 if ((report.data.leds_bitmap & 0x1E) == 0)
1467 report.data.leds_bitmap |= 0x20;
1468
Frank Praznikb3ed4582014-04-14 10:11:36 -04001469 /*
1470 * The LEDs in the report are indexed in reverse order to their
1471 * corresponding light on the controller.
1472 * Index 0 = LED 4, index 1 = LED 3, etc...
1473 *
1474 * In the case of both delay values being zero (blinking disabled) the
1475 * default report values should be used or the controller LED will be
1476 * always off.
1477 */
1478 for (n = 0; n < 4; n++) {
1479 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
1480 report.data.led[3 - n].duty_off = sc->led_delay_off[n];
1481 report.data.led[3 - n].duty_on = sc->led_delay_on[n];
1482 }
1483 }
1484
Frank Praznik55d3b662014-04-14 10:11:32 -04001485 hid_hw_raw_request(sc->hdev, report.data.report_id, report.buf,
1486 sizeof(report), HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001487}
1488
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001489static void dualshock4_state_worker(struct work_struct *work)
1490{
1491 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001492 struct hid_device *hdev = sc->hdev;
Frank Praznik48220232014-02-05 20:03:44 -05001493 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001494
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001495 __u8 buf[78] = { 0 };
Frank Praznik48220232014-02-05 20:03:44 -05001496
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001497 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1498 buf[0] = 0x05;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001499 buf[1] = 0xFF;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001500 offset = 4;
1501 } else {
1502 buf[0] = 0x11;
1503 buf[1] = 0xB0;
1504 buf[3] = 0x0F;
1505 offset = 6;
1506 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001507
1508#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001509 buf[offset++] = sc->right;
1510 buf[offset++] = sc->left;
1511#else
1512 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001513#endif
1514
Frank Praznikb3ed4582014-04-14 10:11:36 -04001515 /* LED 3 is the global control */
1516 if (sc->led_state[3]) {
1517 buf[offset++] = sc->led_state[0];
1518 buf[offset++] = sc->led_state[1];
1519 buf[offset++] = sc->led_state[2];
1520 } else {
1521 offset += 3;
1522 }
1523
1524 /* If both delay values are zero the DualShock 4 disables blinking. */
1525 buf[offset++] = sc->led_delay_on[3];
1526 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05001527
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001528 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1529 hid_hw_output_report(hdev, buf, 32);
1530 else
1531 hid_hw_raw_request(hdev, 0x11, buf, 78,
1532 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001533}
1534
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001535#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001536static int sony_play_effect(struct input_dev *dev, void *data,
1537 struct ff_effect *effect)
1538{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001539 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001540 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001541
1542 if (effect->type != FF_RUMBLE)
1543 return 0;
1544
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001545 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001546 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001547
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001548 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001549 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001550}
1551
Frank Praznikfa57a812014-04-14 10:11:33 -04001552static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001553{
Frank Praznikfa57a812014-04-14 10:11:33 -04001554 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001555 struct hid_input, list);
1556 struct input_dev *input_dev = hidinput->input;
1557
1558 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1559 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1560}
1561
1562#else
Frank Praznikfa57a812014-04-14 10:11:33 -04001563static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001564{
1565 return 0;
1566}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001567
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001568#endif
1569
Frank Praznikd902f472014-01-27 10:17:36 -05001570static int sony_battery_get_property(struct power_supply *psy,
1571 enum power_supply_property psp,
1572 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001573{
Frank Praznikd902f472014-01-27 10:17:36 -05001574 struct sony_sc *sc = container_of(psy, struct sony_sc, battery);
1575 unsigned long flags;
1576 int ret = 0;
1577 u8 battery_charging, battery_capacity, cable_state;
1578
1579 spin_lock_irqsave(&sc->lock, flags);
1580 battery_charging = sc->battery_charging;
1581 battery_capacity = sc->battery_capacity;
1582 cable_state = sc->cable_state;
1583 spin_unlock_irqrestore(&sc->lock, flags);
1584
1585 switch (psp) {
1586 case POWER_SUPPLY_PROP_PRESENT:
1587 val->intval = 1;
1588 break;
1589 case POWER_SUPPLY_PROP_SCOPE:
1590 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1591 break;
1592 case POWER_SUPPLY_PROP_CAPACITY:
1593 val->intval = battery_capacity;
1594 break;
1595 case POWER_SUPPLY_PROP_STATUS:
1596 if (battery_charging)
1597 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1598 else
1599 if (battery_capacity == 100 && cable_state)
1600 val->intval = POWER_SUPPLY_STATUS_FULL;
1601 else
1602 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1603 break;
1604 default:
1605 ret = -EINVAL;
1606 break;
1607 }
1608 return ret;
1609}
1610
1611static int sony_battery_probe(struct sony_sc *sc)
1612{
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001613 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05001614 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001615
Frank Praznikad142b92014-02-20 11:36:00 -05001616 /*
1617 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05001618 * if the battery is polled before the first device report is received.
1619 */
1620 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001621
Frank Praznikd902f472014-01-27 10:17:36 -05001622 sc->battery.properties = sony_battery_props;
1623 sc->battery.num_properties = ARRAY_SIZE(sony_battery_props);
1624 sc->battery.get_property = sony_battery_get_property;
1625 sc->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1626 sc->battery.use_for_apm = 0;
Frank Praznik314531f2014-04-14 10:11:34 -04001627 sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%pMR",
1628 sc->mac_address);
Frank Praznikd902f472014-01-27 10:17:36 -05001629 if (!sc->battery.name)
1630 return -ENOMEM;
1631
1632 ret = power_supply_register(&hdev->dev, &sc->battery);
1633 if (ret) {
1634 hid_err(hdev, "Unable to register battery device\n");
1635 goto err_free;
1636 }
1637
1638 power_supply_powers(&sc->battery, &hdev->dev);
1639 return 0;
1640
1641err_free:
1642 kfree(sc->battery.name);
1643 sc->battery.name = NULL;
1644 return ret;
1645}
1646
1647static void sony_battery_remove(struct sony_sc *sc)
1648{
1649 if (!sc->battery.name)
1650 return;
1651
1652 power_supply_unregister(&sc->battery);
1653 kfree(sc->battery.name);
1654 sc->battery.name = NULL;
1655}
1656
Frank Praznike5606232014-01-27 10:17:37 -05001657static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
1658 int w, int h)
1659{
1660 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1661 struct hid_input, list);
1662 struct input_dev *input_dev = hidinput->input;
1663 int ret;
1664
1665 ret = input_mt_init_slots(input_dev, touch_count, 0);
1666 if (ret < 0) {
1667 hid_err(sc->hdev, "Unable to initialize multi-touch slots\n");
1668 return ret;
1669 }
1670
1671 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1672 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1673
1674 return 0;
1675}
1676
Frank Praznikd2d782f2014-02-20 11:36:03 -05001677/*
1678 * If a controller is plugged in via USB while already connected via Bluetooth
1679 * it will show up as two devices. A global list of connected controllers and
1680 * their MAC addresses is maintained to ensure that a device is only connected
1681 * once.
1682 */
1683static int sony_check_add_dev_list(struct sony_sc *sc)
1684{
1685 struct sony_sc *entry;
1686 unsigned long flags;
1687 int ret;
1688
1689 spin_lock_irqsave(&sony_dev_list_lock, flags);
1690
1691 list_for_each_entry(entry, &sony_device_list, list_node) {
1692 ret = memcmp(sc->mac_address, entry->mac_address,
1693 sizeof(sc->mac_address));
1694 if (!ret) {
1695 ret = -EEXIST;
1696 hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
1697 sc->mac_address);
1698 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001699 }
1700 }
1701
Frank Praznikd2d782f2014-02-20 11:36:03 -05001702 ret = 0;
1703 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001704
Frank Praznikd2d782f2014-02-20 11:36:03 -05001705unlock:
1706 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1707 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001708}
1709
Frank Praznikd2d782f2014-02-20 11:36:03 -05001710static void sony_remove_dev_list(struct sony_sc *sc)
1711{
1712 unsigned long flags;
1713
1714 if (sc->list_node.next) {
1715 spin_lock_irqsave(&sony_dev_list_lock, flags);
1716 list_del(&(sc->list_node));
1717 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1718 }
1719}
1720
1721static int sony_get_bt_devaddr(struct sony_sc *sc)
1722{
1723 int ret;
1724
1725 /* HIDP stores the device MAC address as a string in the uniq field. */
1726 ret = strlen(sc->hdev->uniq);
1727 if (ret != 17)
1728 return -EINVAL;
1729
1730 ret = sscanf(sc->hdev->uniq,
1731 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1732 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
1733 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
1734
1735 if (ret != 6)
1736 return -EINVAL;
1737
1738 return 0;
1739}
1740
1741static int sony_check_add(struct sony_sc *sc)
1742{
1743 int n, ret;
1744
1745 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
1746 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
1747 /*
1748 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
1749 * address from the uniq string where HIDP stores it.
1750 * As uniq cannot be guaranteed to be a MAC address in all cases
1751 * a failure of this function should not prevent the connection.
1752 */
1753 if (sony_get_bt_devaddr(sc) < 0) {
1754 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
1755 return 0;
1756 }
1757 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1758 __u8 buf[7];
1759
1760 /*
1761 * The MAC address of a DS4 controller connected via USB can be
1762 * retrieved with feature report 0x81. The address begins at
1763 * offset 1.
1764 */
1765 ret = hid_hw_raw_request(sc->hdev, 0x81, buf, sizeof(buf),
1766 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1767
1768 if (ret != 7) {
1769 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
1770 return ret < 0 ? ret : -EINVAL;
1771 }
1772
1773 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
1774 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1775 __u8 buf[18];
1776
1777 /*
1778 * The MAC address of a Sixaxis controller connected via USB can
1779 * be retrieved with feature report 0xf2. The address begins at
1780 * offset 4.
1781 */
1782 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf, sizeof(buf),
1783 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1784
1785 if (ret != 18) {
1786 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
1787 return ret < 0 ? ret : -EINVAL;
1788 }
1789
1790 /*
1791 * The Sixaxis device MAC in the report is big-endian and must
1792 * be byte-swapped.
1793 */
1794 for (n = 0; n < 6; n++)
1795 sc->mac_address[5-n] = buf[4+n];
1796 } else {
1797 return 0;
1798 }
1799
1800 return sony_check_add_dev_list(sc);
1801}
1802
Frank Praznik80250872014-04-14 10:11:35 -04001803static int sony_set_device_id(struct sony_sc *sc)
1804{
1805 int ret;
1806
1807 /*
1808 * Only DualShock 4 or Sixaxis controllers get an id.
1809 * All others are set to -1.
1810 */
1811 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
1812 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
1813 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
1814 GFP_KERNEL);
1815 if (ret < 0) {
1816 sc->device_id = -1;
1817 return ret;
1818 }
1819 sc->device_id = ret;
1820 } else {
1821 sc->device_id = -1;
1822 }
1823
1824 return 0;
1825}
1826
1827static void sony_release_device_id(struct sony_sc *sc)
1828{
1829 if (sc->device_id >= 0) {
1830 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
1831 sc->device_id = -1;
1832 }
1833}
1834
Frank Praznik46262042014-04-14 10:11:31 -04001835static inline void sony_init_work(struct sony_sc *sc,
1836 void (*worker)(struct work_struct *))
1837{
1838 if (!sc->worker_initialized)
1839 INIT_WORK(&sc->state_worker, worker);
1840
1841 sc->worker_initialized = 1;
1842}
1843
1844static inline void sony_cancel_work_sync(struct sony_sc *sc)
1845{
1846 if (sc->worker_initialized)
1847 cancel_work_sync(&sc->state_worker);
1848}
Frank Praznikd2d782f2014-02-20 11:36:03 -05001849
Jiri Slabybd28ce02008-06-25 23:47:04 +02001850static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1851{
1852 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001853 unsigned long quirks = id->driver_data;
1854 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001855 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001856
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001857 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001858 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08001859 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001860 return -ENOMEM;
1861 }
1862
1863 sc->quirks = quirks;
1864 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001865 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001866
Jiri Slabybd28ce02008-06-25 23:47:04 +02001867 ret = hid_parse(hdev);
1868 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001869 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001870 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001871 }
1872
Colin Leitnerf04d5142013-05-27 23:41:05 +02001873 if (sc->quirks & VAIO_RDESC_CONSTANT)
1874 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
Antonio Ospite50764652014-06-24 13:28:40 +02001875 else if (sc->quirks & SIXAXIS_CONTROLLER)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001876 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1877
1878 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001879 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001880 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001881 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001882 }
1883
Frank Praznik80250872014-04-14 10:11:35 -04001884 ret = sony_set_device_id(sc);
1885 if (ret < 0) {
1886 hid_err(hdev, "failed to allocate the device id\n");
1887 goto err_stop;
1888 }
1889
Antonio Ospite569b10a2010-10-19 16:13:10 +02001890 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
Benjamin Tissoirese534a932014-03-08 22:52:42 -05001891 /*
1892 * The Sony Sixaxis does not handle HID Output Reports on the
1893 * Interrupt EP like it could, so we need to force HID Output
1894 * Reports to use HID_REQ_SET_REPORT on the Control EP.
1895 *
1896 * There is also another issue about HID Output Reports via USB,
1897 * the Sixaxis does not want the report_id as part of the data
1898 * packet, so we have to discard buf[0] when sending the actual
1899 * control message, even for numbered reports, humpf!
1900 */
1901 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
1902 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
Antonio Ospite816651a2010-05-03 22:15:55 +02001903 ret = sixaxis_set_operational_usb(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04001904 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001905 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04001906 /*
1907 * The Sixaxis wants output reports sent on the ctrl endpoint
1908 * when connected via Bluetooth.
1909 */
1910 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Antonio Ospite816651a2010-05-03 22:15:55 +02001911 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04001912 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001913 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik68330d82014-02-05 20:03:49 -05001914 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04001915 /*
1916 * The DualShock 4 wants output reports sent on the ctrl
1917 * endpoint when connected via Bluetooth.
1918 */
1919 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Frank Praznik68330d82014-02-05 20:03:49 -05001920 ret = dualshock4_set_operational_bt(hdev);
1921 if (ret < 0) {
1922 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
1923 goto err_stop;
1924 }
1925 }
Frank Praznikad142b92014-02-20 11:36:00 -05001926 /*
1927 * The Dualshock 4 touchpad supports 2 touches and has a
Frank Praznike5606232014-01-27 10:17:37 -05001928 * resolution of 1920x940.
1929 */
1930 ret = sony_register_touchpad(sc, 2, 1920, 940);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001931 if (ret < 0)
1932 goto err_stop;
1933
Frank Praznik46262042014-04-14 10:11:31 -04001934 sony_init_work(sc, dualshock4_state_worker);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001935 } else {
1936 ret = 0;
1937 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001938
Jiri Kosina4dfdc462008-12-30 00:49:59 +01001939 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001940 goto err_stop;
1941
Frank Praznikd2d782f2014-02-20 11:36:03 -05001942 ret = sony_check_add(sc);
1943 if (ret < 0)
1944 goto err_stop;
1945
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001946 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04001947 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001948 if (ret < 0)
1949 goto err_stop;
1950 }
1951
Frank Praznikd902f472014-01-27 10:17:36 -05001952 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1953 ret = sony_battery_probe(sc);
1954 if (ret < 0)
1955 goto err_stop;
1956
1957 /* Open the device to receive reports with battery info */
1958 ret = hid_hw_open(hdev);
1959 if (ret < 0) {
1960 hid_err(hdev, "hw open failed\n");
1961 goto err_stop;
1962 }
1963 }
1964
Frank Praznikc8de9db2014-02-20 11:36:01 -05001965 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04001966 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05001967 if (ret < 0)
1968 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05001969 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001970
Jiri Slabybd28ce02008-06-25 23:47:04 +02001971 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001972err_close:
1973 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001974err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001975 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04001976 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05001977 if (sc->quirks & SONY_BATTERY_SUPPORT)
1978 sony_battery_remove(sc);
Frank Praznik46262042014-04-14 10:11:31 -04001979 sony_cancel_work_sync(sc);
Frank Praznikd2d782f2014-02-20 11:36:03 -05001980 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04001981 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001982 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001983 return ret;
1984}
1985
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001986static void sony_remove(struct hid_device *hdev)
1987{
Colin Leitnerf04d5142013-05-27 23:41:05 +02001988 struct sony_sc *sc = hid_get_drvdata(hdev);
1989
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001990 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04001991 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001992
Frank Praznikd902f472014-01-27 10:17:36 -05001993 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1994 hid_hw_close(hdev);
1995 sony_battery_remove(sc);
1996 }
1997
Frank Praznik46262042014-04-14 10:11:31 -04001998 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001999
Frank Praznikd2d782f2014-02-20 11:36:03 -05002000 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002001
Frank Praznik80250872014-04-14 10:11:35 -04002002 sony_release_device_id(sc);
2003
Jiri Slabybd28ce02008-06-25 23:47:04 +02002004 hid_hw_stop(hdev);
2005}
2006
2007static const struct hid_device_id sony_devices[] = {
2008 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2009 .driver_data = SIXAXIS_CONTROLLER_USB },
2010 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2011 .driver_data = SIXAXIS_CONTROLLER_USB },
2012 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2013 .driver_data = SIXAXIS_CONTROLLER_BT },
2014 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2015 .driver_data = VAIO_RDESC_CONSTANT },
2016 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2017 .driver_data = VAIO_RDESC_CONSTANT },
2018 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2019 * Logitech joystick from the device descriptor. */
2020 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2021 .driver_data = BUZZ_CONTROLLER },
2022 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2023 .driver_data = BUZZ_CONTROLLER },
2024 /* PS3 BD Remote Control */
2025 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2026 .driver_data = PS3REMOTE },
2027 /* Logitech Harmony Adapter for PS3 */
2028 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2029 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002030 /* Sony Dualshock 4 controllers for PS4 */
2031 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002032 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002033 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002034 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002035 { }
2036};
2037MODULE_DEVICE_TABLE(hid, sony_devices);
2038
2039static struct hid_driver sony_driver = {
2040 .name = "sony",
2041 .id_table = sony_devices,
2042 .input_mapping = sony_mapping,
2043 .probe = sony_probe,
2044 .remove = sony_remove,
2045 .report_fixup = sony_report_fixup,
2046 .raw_event = sony_raw_event
2047};
Frank Praznik80250872014-04-14 10:11:35 -04002048
2049static int __init sony_init(void)
2050{
2051 dbg_hid("Sony:%s\n", __func__);
2052
2053 return hid_register_driver(&sony_driver);
2054}
2055
2056static void __exit sony_exit(void)
2057{
2058 dbg_hid("Sony:%s\n", __func__);
2059
2060 ida_destroy(&sony_device_id_allocator);
2061 hid_unregister_driver(&sony_driver);
2062}
2063module_init(sony_init);
2064module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002065
2066MODULE_LICENSE("GPL");