blob: 11026b55669b6fac03d4aec9b0001fa4a2ae6693 [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[] = {
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -020060 0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
61 0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
62 0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
63 0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
64 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
65 0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
66 0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
67 0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
68 0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
69 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
70 0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
71 0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
72 0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
73 0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
74 0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
75 0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
76 0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
77 0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
78 0xb1, 0x02, 0xc0, 0xc0,
79};
80
Frank Praznikad142b92014-02-20 11:36:00 -050081/*
82 * The default descriptor doesn't provide mapping for the accelerometers
Frank Praznik58d70272014-01-20 12:27:01 -050083 * or orientation sensors. This fixed descriptor maps the accelerometers
84 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
85 * to usage values 0x43, 0x44 and 0x45.
86 */
Frank Prazniked19d8c2014-01-16 21:43:12 -050087static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -050088 0x05, 0x01, /* Usage Page (Desktop), */
89 0x09, 0x05, /* Usage (Gamepad), */
90 0xA1, 0x01, /* Collection (Application), */
91 0x85, 0x01, /* Report ID (1), */
92 0x09, 0x30, /* Usage (X), */
93 0x09, 0x31, /* Usage (Y), */
94 0x09, 0x32, /* Usage (Z), */
95 0x09, 0x35, /* Usage (Rz), */
96 0x15, 0x00, /* Logical Minimum (0), */
97 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
98 0x75, 0x08, /* Report Size (8), */
99 0x95, 0x04, /* Report Count (4), */
100 0x81, 0x02, /* Input (Variable), */
101 0x09, 0x39, /* Usage (Hat Switch), */
102 0x15, 0x00, /* Logical Minimum (0), */
103 0x25, 0x07, /* Logical Maximum (7), */
104 0x35, 0x00, /* Physical Minimum (0), */
105 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
106 0x65, 0x14, /* Unit (Degrees), */
107 0x75, 0x04, /* Report Size (4), */
108 0x95, 0x01, /* Report Count (1), */
109 0x81, 0x42, /* Input (Variable, Null State), */
110 0x65, 0x00, /* Unit, */
111 0x05, 0x09, /* Usage Page (Button), */
112 0x19, 0x01, /* Usage Minimum (01h), */
113 0x29, 0x0E, /* Usage Maximum (0Eh), */
114 0x15, 0x00, /* Logical Minimum (0), */
115 0x25, 0x01, /* Logical Maximum (1), */
116 0x75, 0x01, /* Report Size (1), */
117 0x95, 0x0E, /* Report Count (14), */
118 0x81, 0x02, /* Input (Variable), */
119 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
120 0x09, 0x20, /* Usage (20h), */
121 0x75, 0x06, /* Report Size (6), */
122 0x95, 0x01, /* Report Count (1), */
123 0x15, 0x00, /* Logical Minimum (0), */
124 0x25, 0x7F, /* Logical Maximum (127), */
125 0x81, 0x02, /* Input (Variable), */
126 0x05, 0x01, /* Usage Page (Desktop), */
127 0x09, 0x33, /* Usage (Rx), */
128 0x09, 0x34, /* Usage (Ry), */
129 0x15, 0x00, /* Logical Minimum (0), */
130 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
131 0x75, 0x08, /* Report Size (8), */
132 0x95, 0x02, /* Report Count (2), */
133 0x81, 0x02, /* Input (Variable), */
134 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
135 0x09, 0x21, /* Usage (21h), */
136 0x95, 0x03, /* Report Count (3), */
137 0x81, 0x02, /* Input (Variable), */
138 0x05, 0x01, /* Usage Page (Desktop), */
139 0x19, 0x40, /* Usage Minimum (40h), */
140 0x29, 0x42, /* Usage Maximum (42h), */
141 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
142 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
143 0x75, 0x10, /* Report Size (16), */
144 0x95, 0x03, /* Report Count (3), */
145 0x81, 0x02, /* Input (Variable), */
146 0x19, 0x43, /* Usage Minimum (43h), */
147 0x29, 0x45, /* Usage Maximum (45h), */
148 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
149 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
150 0x95, 0x03, /* Report Count (3), */
151 0x81, 0x02, /* Input (Variable), */
152 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
153 0x09, 0x21, /* Usage (21h), */
154 0x15, 0x00, /* Logical Minimum (0), */
155 0x25, 0xFF, /* Logical Maximum (255), */
156 0x75, 0x08, /* Report Size (8), */
157 0x95, 0x27, /* Report Count (39), */
158 0x81, 0x02, /* Input (Variable), */
159 0x85, 0x05, /* Report ID (5), */
160 0x09, 0x22, /* Usage (22h), */
161 0x95, 0x1F, /* Report Count (31), */
162 0x91, 0x02, /* Output (Variable), */
163 0x85, 0x04, /* Report ID (4), */
164 0x09, 0x23, /* Usage (23h), */
165 0x95, 0x24, /* Report Count (36), */
166 0xB1, 0x02, /* Feature (Variable), */
167 0x85, 0x02, /* Report ID (2), */
168 0x09, 0x24, /* Usage (24h), */
169 0x95, 0x24, /* Report Count (36), */
170 0xB1, 0x02, /* Feature (Variable), */
171 0x85, 0x08, /* Report ID (8), */
172 0x09, 0x25, /* Usage (25h), */
173 0x95, 0x03, /* Report Count (3), */
174 0xB1, 0x02, /* Feature (Variable), */
175 0x85, 0x10, /* Report ID (16), */
176 0x09, 0x26, /* Usage (26h), */
177 0x95, 0x04, /* Report Count (4), */
178 0xB1, 0x02, /* Feature (Variable), */
179 0x85, 0x11, /* Report ID (17), */
180 0x09, 0x27, /* Usage (27h), */
181 0x95, 0x02, /* Report Count (2), */
182 0xB1, 0x02, /* Feature (Variable), */
183 0x85, 0x12, /* Report ID (18), */
184 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
185 0x09, 0x21, /* Usage (21h), */
186 0x95, 0x0F, /* Report Count (15), */
187 0xB1, 0x02, /* Feature (Variable), */
188 0x85, 0x13, /* Report ID (19), */
189 0x09, 0x22, /* Usage (22h), */
190 0x95, 0x16, /* Report Count (22), */
191 0xB1, 0x02, /* Feature (Variable), */
192 0x85, 0x14, /* Report ID (20), */
193 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
194 0x09, 0x20, /* Usage (20h), */
195 0x95, 0x10, /* Report Count (16), */
196 0xB1, 0x02, /* Feature (Variable), */
197 0x85, 0x15, /* Report ID (21), */
198 0x09, 0x21, /* Usage (21h), */
199 0x95, 0x2C, /* Report Count (44), */
200 0xB1, 0x02, /* Feature (Variable), */
201 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
202 0x85, 0x80, /* Report ID (128), */
203 0x09, 0x20, /* Usage (20h), */
204 0x95, 0x06, /* Report Count (6), */
205 0xB1, 0x02, /* Feature (Variable), */
206 0x85, 0x81, /* Report ID (129), */
207 0x09, 0x21, /* Usage (21h), */
208 0x95, 0x06, /* Report Count (6), */
209 0xB1, 0x02, /* Feature (Variable), */
210 0x85, 0x82, /* Report ID (130), */
211 0x09, 0x22, /* Usage (22h), */
212 0x95, 0x05, /* Report Count (5), */
213 0xB1, 0x02, /* Feature (Variable), */
214 0x85, 0x83, /* Report ID (131), */
215 0x09, 0x23, /* Usage (23h), */
216 0x95, 0x01, /* Report Count (1), */
217 0xB1, 0x02, /* Feature (Variable), */
218 0x85, 0x84, /* Report ID (132), */
219 0x09, 0x24, /* Usage (24h), */
220 0x95, 0x04, /* Report Count (4), */
221 0xB1, 0x02, /* Feature (Variable), */
222 0x85, 0x85, /* Report ID (133), */
223 0x09, 0x25, /* Usage (25h), */
224 0x95, 0x06, /* Report Count (6), */
225 0xB1, 0x02, /* Feature (Variable), */
226 0x85, 0x86, /* Report ID (134), */
227 0x09, 0x26, /* Usage (26h), */
228 0x95, 0x06, /* Report Count (6), */
229 0xB1, 0x02, /* Feature (Variable), */
230 0x85, 0x87, /* Report ID (135), */
231 0x09, 0x27, /* Usage (27h), */
232 0x95, 0x23, /* Report Count (35), */
233 0xB1, 0x02, /* Feature (Variable), */
234 0x85, 0x88, /* Report ID (136), */
235 0x09, 0x28, /* Usage (28h), */
236 0x95, 0x22, /* Report Count (34), */
237 0xB1, 0x02, /* Feature (Variable), */
238 0x85, 0x89, /* Report ID (137), */
239 0x09, 0x29, /* Usage (29h), */
240 0x95, 0x02, /* Report Count (2), */
241 0xB1, 0x02, /* Feature (Variable), */
242 0x85, 0x90, /* Report ID (144), */
243 0x09, 0x30, /* Usage (30h), */
244 0x95, 0x05, /* Report Count (5), */
245 0xB1, 0x02, /* Feature (Variable), */
246 0x85, 0x91, /* Report ID (145), */
247 0x09, 0x31, /* Usage (31h), */
248 0x95, 0x03, /* Report Count (3), */
249 0xB1, 0x02, /* Feature (Variable), */
250 0x85, 0x92, /* Report ID (146), */
251 0x09, 0x32, /* Usage (32h), */
252 0x95, 0x03, /* Report Count (3), */
253 0xB1, 0x02, /* Feature (Variable), */
254 0x85, 0x93, /* Report ID (147), */
255 0x09, 0x33, /* Usage (33h), */
256 0x95, 0x0C, /* Report Count (12), */
257 0xB1, 0x02, /* Feature (Variable), */
258 0x85, 0xA0, /* Report ID (160), */
259 0x09, 0x40, /* Usage (40h), */
260 0x95, 0x06, /* Report Count (6), */
261 0xB1, 0x02, /* Feature (Variable), */
262 0x85, 0xA1, /* Report ID (161), */
263 0x09, 0x41, /* Usage (41h), */
264 0x95, 0x01, /* Report Count (1), */
265 0xB1, 0x02, /* Feature (Variable), */
266 0x85, 0xA2, /* Report ID (162), */
267 0x09, 0x42, /* Usage (42h), */
268 0x95, 0x01, /* Report Count (1), */
269 0xB1, 0x02, /* Feature (Variable), */
270 0x85, 0xA3, /* Report ID (163), */
271 0x09, 0x43, /* Usage (43h), */
272 0x95, 0x30, /* Report Count (48), */
273 0xB1, 0x02, /* Feature (Variable), */
274 0x85, 0xA4, /* Report ID (164), */
275 0x09, 0x44, /* Usage (44h), */
276 0x95, 0x0D, /* Report Count (13), */
277 0xB1, 0x02, /* Feature (Variable), */
278 0x85, 0xA5, /* Report ID (165), */
279 0x09, 0x45, /* Usage (45h), */
280 0x95, 0x15, /* Report Count (21), */
281 0xB1, 0x02, /* Feature (Variable), */
282 0x85, 0xA6, /* Report ID (166), */
283 0x09, 0x46, /* Usage (46h), */
284 0x95, 0x15, /* Report Count (21), */
285 0xB1, 0x02, /* Feature (Variable), */
286 0x85, 0xF0, /* Report ID (240), */
287 0x09, 0x47, /* Usage (47h), */
288 0x95, 0x3F, /* Report Count (63), */
289 0xB1, 0x02, /* Feature (Variable), */
290 0x85, 0xF1, /* Report ID (241), */
291 0x09, 0x48, /* Usage (48h), */
292 0x95, 0x3F, /* Report Count (63), */
293 0xB1, 0x02, /* Feature (Variable), */
294 0x85, 0xF2, /* Report ID (242), */
295 0x09, 0x49, /* Usage (49h), */
296 0x95, 0x0F, /* Report Count (15), */
297 0xB1, 0x02, /* Feature (Variable), */
298 0x85, 0xA7, /* Report ID (167), */
299 0x09, 0x4A, /* Usage (4Ah), */
300 0x95, 0x01, /* Report Count (1), */
301 0xB1, 0x02, /* Feature (Variable), */
302 0x85, 0xA8, /* Report ID (168), */
303 0x09, 0x4B, /* Usage (4Bh), */
304 0x95, 0x01, /* Report Count (1), */
305 0xB1, 0x02, /* Feature (Variable), */
306 0x85, 0xA9, /* Report ID (169), */
307 0x09, 0x4C, /* Usage (4Ch), */
308 0x95, 0x08, /* Report Count (8), */
309 0xB1, 0x02, /* Feature (Variable), */
310 0x85, 0xAA, /* Report ID (170), */
311 0x09, 0x4E, /* Usage (4Eh), */
312 0x95, 0x01, /* Report Count (1), */
313 0xB1, 0x02, /* Feature (Variable), */
314 0x85, 0xAB, /* Report ID (171), */
315 0x09, 0x4F, /* Usage (4Fh), */
316 0x95, 0x39, /* Report Count (57), */
317 0xB1, 0x02, /* Feature (Variable), */
318 0x85, 0xAC, /* Report ID (172), */
319 0x09, 0x50, /* Usage (50h), */
320 0x95, 0x39, /* Report Count (57), */
321 0xB1, 0x02, /* Feature (Variable), */
322 0x85, 0xAD, /* Report ID (173), */
323 0x09, 0x51, /* Usage (51h), */
324 0x95, 0x0B, /* Report Count (11), */
325 0xB1, 0x02, /* Feature (Variable), */
326 0x85, 0xAE, /* Report ID (174), */
327 0x09, 0x52, /* Usage (52h), */
328 0x95, 0x01, /* Report Count (1), */
329 0xB1, 0x02, /* Feature (Variable), */
330 0x85, 0xAF, /* Report ID (175), */
331 0x09, 0x53, /* Usage (53h), */
332 0x95, 0x02, /* Report Count (2), */
333 0xB1, 0x02, /* Feature (Variable), */
334 0x85, 0xB0, /* Report ID (176), */
335 0x09, 0x54, /* Usage (54h), */
336 0x95, 0x3F, /* Report Count (63), */
337 0xB1, 0x02, /* Feature (Variable), */
338 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500339};
340
Frank Praznikad142b92014-02-20 11:36:00 -0500341/*
342 * The default behavior of the Dualshock 4 is to send reports using report
Frank Praznikd8296742014-02-05 20:03:45 -0500343 * type 1 when running over Bluetooth. However, as soon as it receives a
344 * report of type 17 to set the LEDs or rumble it starts returning it's state
345 * in report 17 instead of 1. Since report 17 is undefined in the default HID
346 * descriptor the button and axis definitions must be moved to report 17 or
347 * the HID layer won't process the received input once a report is sent.
348 */
349static u8 dualshock4_bt_rdesc[] = {
350 0x05, 0x01, /* Usage Page (Desktop), */
351 0x09, 0x05, /* Usage (Gamepad), */
352 0xA1, 0x01, /* Collection (Application), */
353 0x85, 0x01, /* Report ID (1), */
354 0x75, 0x08, /* Report Size (8), */
355 0x95, 0x0A, /* Report Count (9), */
356 0x81, 0x02, /* Input (Variable), */
357 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
358 0x85, 0x02, /* Report ID (2), */
359 0x09, 0x24, /* Usage (24h), */
360 0x95, 0x24, /* Report Count (36), */
361 0xB1, 0x02, /* Feature (Variable), */
362 0x85, 0xA3, /* Report ID (163), */
363 0x09, 0x25, /* Usage (25h), */
364 0x95, 0x30, /* Report Count (48), */
365 0xB1, 0x02, /* Feature (Variable), */
366 0x85, 0x05, /* Report ID (5), */
367 0x09, 0x26, /* Usage (26h), */
368 0x95, 0x28, /* Report Count (40), */
369 0xB1, 0x02, /* Feature (Variable), */
370 0x85, 0x06, /* Report ID (6), */
371 0x09, 0x27, /* Usage (27h), */
372 0x95, 0x34, /* Report Count (52), */
373 0xB1, 0x02, /* Feature (Variable), */
374 0x85, 0x07, /* Report ID (7), */
375 0x09, 0x28, /* Usage (28h), */
376 0x95, 0x30, /* Report Count (48), */
377 0xB1, 0x02, /* Feature (Variable), */
378 0x85, 0x08, /* Report ID (8), */
379 0x09, 0x29, /* Usage (29h), */
380 0x95, 0x2F, /* Report Count (47), */
381 0xB1, 0x02, /* Feature (Variable), */
382 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
383 0x85, 0x03, /* Report ID (3), */
384 0x09, 0x21, /* Usage (21h), */
385 0x95, 0x26, /* Report Count (38), */
386 0xB1, 0x02, /* Feature (Variable), */
387 0x85, 0x04, /* Report ID (4), */
388 0x09, 0x22, /* Usage (22h), */
389 0x95, 0x2E, /* Report Count (46), */
390 0xB1, 0x02, /* Feature (Variable), */
391 0x85, 0xF0, /* Report ID (240), */
392 0x09, 0x47, /* Usage (47h), */
393 0x95, 0x3F, /* Report Count (63), */
394 0xB1, 0x02, /* Feature (Variable), */
395 0x85, 0xF1, /* Report ID (241), */
396 0x09, 0x48, /* Usage (48h), */
397 0x95, 0x3F, /* Report Count (63), */
398 0xB1, 0x02, /* Feature (Variable), */
399 0x85, 0xF2, /* Report ID (242), */
400 0x09, 0x49, /* Usage (49h), */
401 0x95, 0x0F, /* Report Count (15), */
402 0xB1, 0x02, /* Feature (Variable), */
403 0x85, 0x11, /* Report ID (17), */
404 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
405 0x09, 0x20, /* Usage (20h), */
406 0x95, 0x02, /* Report Count (2), */
407 0x81, 0x02, /* Input (Variable), */
408 0x05, 0x01, /* Usage Page (Desktop), */
409 0x09, 0x30, /* Usage (X), */
410 0x09, 0x31, /* Usage (Y), */
411 0x09, 0x32, /* Usage (Z), */
412 0x09, 0x35, /* Usage (Rz), */
413 0x15, 0x00, /* Logical Minimum (0), */
414 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
415 0x75, 0x08, /* Report Size (8), */
416 0x95, 0x04, /* Report Count (4), */
417 0x81, 0x02, /* Input (Variable), */
418 0x09, 0x39, /* Usage (Hat Switch), */
419 0x15, 0x00, /* Logical Minimum (0), */
420 0x25, 0x07, /* Logical Maximum (7), */
421 0x75, 0x04, /* Report Size (4), */
422 0x95, 0x01, /* Report Count (1), */
423 0x81, 0x42, /* Input (Variable, Null State), */
424 0x05, 0x09, /* Usage Page (Button), */
425 0x19, 0x01, /* Usage Minimum (01h), */
426 0x29, 0x0E, /* Usage Maximum (0Eh), */
427 0x15, 0x00, /* Logical Minimum (0), */
428 0x25, 0x01, /* Logical Maximum (1), */
429 0x75, 0x01, /* Report Size (1), */
430 0x95, 0x0E, /* Report Count (14), */
431 0x81, 0x02, /* Input (Variable), */
432 0x75, 0x06, /* Report Size (6), */
433 0x95, 0x01, /* Report Count (1), */
434 0x81, 0x01, /* Input (Constant), */
435 0x05, 0x01, /* Usage Page (Desktop), */
436 0x09, 0x33, /* Usage (Rx), */
437 0x09, 0x34, /* Usage (Ry), */
438 0x15, 0x00, /* Logical Minimum (0), */
439 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
440 0x75, 0x08, /* Report Size (8), */
441 0x95, 0x02, /* Report Count (2), */
442 0x81, 0x02, /* Input (Variable), */
443 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
444 0x09, 0x20, /* Usage (20h), */
445 0x95, 0x03, /* Report Count (3), */
446 0x81, 0x02, /* Input (Variable), */
447 0x05, 0x01, /* Usage Page (Desktop), */
448 0x19, 0x40, /* Usage Minimum (40h), */
449 0x29, 0x42, /* Usage Maximum (42h), */
450 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
451 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
452 0x75, 0x10, /* Report Size (16), */
453 0x95, 0x03, /* Report Count (3), */
454 0x81, 0x02, /* Input (Variable), */
455 0x19, 0x43, /* Usage Minimum (43h), */
456 0x29, 0x45, /* Usage Maximum (45h), */
457 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
458 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
459 0x95, 0x03, /* Report Count (3), */
460 0x81, 0x02, /* Input (Variable), */
461 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
462 0x09, 0x20, /* Usage (20h), */
463 0x15, 0x00, /* Logical Minimum (0), */
464 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
465 0x75, 0x08, /* Report Size (8), */
466 0x95, 0x31, /* Report Count (51), */
467 0x81, 0x02, /* Input (Variable), */
468 0x09, 0x21, /* Usage (21h), */
469 0x75, 0x08, /* Report Size (8), */
470 0x95, 0x4D, /* Report Count (77), */
471 0x91, 0x02, /* Output (Variable), */
472 0x85, 0x12, /* Report ID (18), */
473 0x09, 0x22, /* Usage (22h), */
474 0x95, 0x8D, /* Report Count (141), */
475 0x81, 0x02, /* Input (Variable), */
476 0x09, 0x23, /* Usage (23h), */
477 0x91, 0x02, /* Output (Variable), */
478 0x85, 0x13, /* Report ID (19), */
479 0x09, 0x24, /* Usage (24h), */
480 0x95, 0xCD, /* Report Count (205), */
481 0x81, 0x02, /* Input (Variable), */
482 0x09, 0x25, /* Usage (25h), */
483 0x91, 0x02, /* Output (Variable), */
484 0x85, 0x14, /* Report ID (20), */
485 0x09, 0x26, /* Usage (26h), */
486 0x96, 0x0D, 0x01, /* Report Count (269), */
487 0x81, 0x02, /* Input (Variable), */
488 0x09, 0x27, /* Usage (27h), */
489 0x91, 0x02, /* Output (Variable), */
490 0x85, 0x15, /* Report ID (21), */
491 0x09, 0x28, /* Usage (28h), */
492 0x96, 0x4D, 0x01, /* Report Count (333), */
493 0x81, 0x02, /* Input (Variable), */
494 0x09, 0x29, /* Usage (29h), */
495 0x91, 0x02, /* Output (Variable), */
496 0x85, 0x16, /* Report ID (22), */
497 0x09, 0x2A, /* Usage (2Ah), */
498 0x96, 0x8D, 0x01, /* Report Count (397), */
499 0x81, 0x02, /* Input (Variable), */
500 0x09, 0x2B, /* Usage (2Bh), */
501 0x91, 0x02, /* Output (Variable), */
502 0x85, 0x17, /* Report ID (23), */
503 0x09, 0x2C, /* Usage (2Ch), */
504 0x96, 0xCD, 0x01, /* Report Count (461), */
505 0x81, 0x02, /* Input (Variable), */
506 0x09, 0x2D, /* Usage (2Dh), */
507 0x91, 0x02, /* Output (Variable), */
508 0x85, 0x18, /* Report ID (24), */
509 0x09, 0x2E, /* Usage (2Eh), */
510 0x96, 0x0D, 0x02, /* Report Count (525), */
511 0x81, 0x02, /* Input (Variable), */
512 0x09, 0x2F, /* Usage (2Fh), */
513 0x91, 0x02, /* Output (Variable), */
514 0x85, 0x19, /* Report ID (25), */
515 0x09, 0x30, /* Usage (30h), */
516 0x96, 0x22, 0x02, /* Report Count (546), */
517 0x81, 0x02, /* Input (Variable), */
518 0x09, 0x31, /* Usage (31h), */
519 0x91, 0x02, /* Output (Variable), */
520 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
521 0x85, 0x82, /* Report ID (130), */
522 0x09, 0x22, /* Usage (22h), */
523 0x95, 0x3F, /* Report Count (63), */
524 0xB1, 0x02, /* Feature (Variable), */
525 0x85, 0x83, /* Report ID (131), */
526 0x09, 0x23, /* Usage (23h), */
527 0xB1, 0x02, /* Feature (Variable), */
528 0x85, 0x84, /* Report ID (132), */
529 0x09, 0x24, /* Usage (24h), */
530 0xB1, 0x02, /* Feature (Variable), */
531 0x85, 0x90, /* Report ID (144), */
532 0x09, 0x30, /* Usage (30h), */
533 0xB1, 0x02, /* Feature (Variable), */
534 0x85, 0x91, /* Report ID (145), */
535 0x09, 0x31, /* Usage (31h), */
536 0xB1, 0x02, /* Feature (Variable), */
537 0x85, 0x92, /* Report ID (146), */
538 0x09, 0x32, /* Usage (32h), */
539 0xB1, 0x02, /* Feature (Variable), */
540 0x85, 0x93, /* Report ID (147), */
541 0x09, 0x33, /* Usage (33h), */
542 0xB1, 0x02, /* Feature (Variable), */
543 0x85, 0xA0, /* Report ID (160), */
544 0x09, 0x40, /* Usage (40h), */
545 0xB1, 0x02, /* Feature (Variable), */
546 0x85, 0xA4, /* Report ID (164), */
547 0x09, 0x44, /* Usage (44h), */
548 0xB1, 0x02, /* Feature (Variable), */
549 0xC0 /* End Collection */
550};
551
Jiri Kosina078328d2013-06-13 12:03:49 +0200552static __u8 ps3remote_rdesc[] = {
553 0x05, 0x01, /* GUsagePage Generic Desktop */
554 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
555 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
556
557 /* Use collection 1 for joypad buttons */
558 0xA1, 0x02, /* MCollection Logical (interrelated data) */
559
560 /* Ignore the 1st byte, maybe it is used for a controller
561 * number but it's not needed for correct operation */
562 0x75, 0x08, /* GReportSize 0x08 [8] */
563 0x95, 0x01, /* GReportCount 0x01 [1] */
564 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
565
566 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
567 * buttons multiple keypresses are allowed */
568 0x05, 0x09, /* GUsagePage Button */
569 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
570 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
571 0x14, /* GLogicalMinimum [0] */
572 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
573 0x75, 0x01, /* GReportSize 0x01 [1] */
574 0x95, 0x18, /* GReportCount 0x18 [24] */
575 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
576
577 0xC0, /* MEndCollection */
578
579 /* Use collection 2 for remote control buttons */
580 0xA1, 0x02, /* MCollection Logical (interrelated data) */
581
582 /* 5th byte is used for remote control buttons */
583 0x05, 0x09, /* GUsagePage Button */
584 0x18, /* LUsageMinimum [No button pressed] */
585 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
586 0x14, /* GLogicalMinimum [0] */
587 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
588 0x75, 0x08, /* GReportSize 0x08 [8] */
589 0x95, 0x01, /* GReportCount 0x01 [1] */
590 0x80, /* MInput */
591
592 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
593 * 0xff and 11th is for press indication */
594 0x75, 0x08, /* GReportSize 0x08 [8] */
595 0x95, 0x06, /* GReportCount 0x06 [6] */
596 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
597
598 /* 12th byte is for battery strength */
599 0x05, 0x06, /* GUsagePage Generic Device Controls */
600 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
601 0x14, /* GLogicalMinimum [0] */
602 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
603 0x75, 0x08, /* GReportSize 0x08 [8] */
604 0x95, 0x01, /* GReportCount 0x01 [1] */
605 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
606
607 0xC0, /* MEndCollection */
608
609 0xC0 /* MEndCollection [Game Pad] */
610};
611
612static const unsigned int ps3remote_keymap_joypad_buttons[] = {
613 [0x01] = KEY_SELECT,
614 [0x02] = BTN_THUMBL, /* L3 */
615 [0x03] = BTN_THUMBR, /* R3 */
616 [0x04] = BTN_START,
617 [0x05] = KEY_UP,
618 [0x06] = KEY_RIGHT,
619 [0x07] = KEY_DOWN,
620 [0x08] = KEY_LEFT,
621 [0x09] = BTN_TL2, /* L2 */
622 [0x0a] = BTN_TR2, /* R2 */
623 [0x0b] = BTN_TL, /* L1 */
624 [0x0c] = BTN_TR, /* R1 */
625 [0x0d] = KEY_OPTION, /* options/triangle */
626 [0x0e] = KEY_BACK, /* back/circle */
627 [0x0f] = BTN_0, /* cross */
628 [0x10] = KEY_SCREEN, /* view/square */
629 [0x11] = KEY_HOMEPAGE, /* PS button */
630 [0x14] = KEY_ENTER,
631};
632static const unsigned int ps3remote_keymap_remote_buttons[] = {
633 [0x00] = KEY_1,
634 [0x01] = KEY_2,
635 [0x02] = KEY_3,
636 [0x03] = KEY_4,
637 [0x04] = KEY_5,
638 [0x05] = KEY_6,
639 [0x06] = KEY_7,
640 [0x07] = KEY_8,
641 [0x08] = KEY_9,
642 [0x09] = KEY_0,
643 [0x0e] = KEY_ESC, /* return */
644 [0x0f] = KEY_CLEAR,
645 [0x16] = KEY_EJECTCD,
646 [0x1a] = KEY_MENU, /* top menu */
647 [0x28] = KEY_TIME,
648 [0x30] = KEY_PREVIOUS,
649 [0x31] = KEY_NEXT,
650 [0x32] = KEY_PLAY,
651 [0x33] = KEY_REWIND, /* scan back */
652 [0x34] = KEY_FORWARD, /* scan forward */
653 [0x38] = KEY_STOP,
654 [0x39] = KEY_PAUSE,
655 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
656 [0x60] = KEY_FRAMEBACK, /* slow/step back */
657 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
658 [0x63] = KEY_SUBTITLE,
659 [0x64] = KEY_AUDIO,
660 [0x65] = KEY_ANGLE,
661 [0x70] = KEY_INFO, /* display */
662 [0x80] = KEY_BLUE,
663 [0x81] = KEY_RED,
664 [0x82] = KEY_GREEN,
665 [0x83] = KEY_YELLOW,
666};
667
Colin Leitnerf04d5142013-05-27 23:41:05 +0200668static const unsigned int buzz_keymap[] = {
Frank Praznikad142b92014-02-20 11:36:00 -0500669 /*
670 * The controller has 4 remote buzzers, each with one LED and 5
Colin Leitnerf04d5142013-05-27 23:41:05 +0200671 * buttons.
672 *
673 * We use the mapping chosen by the controller, which is:
674 *
675 * Key Offset
676 * -------------------
677 * Buzz 1
678 * Blue 5
679 * Orange 4
680 * Green 3
681 * Yellow 2
682 *
683 * So, for example, the orange button on the third buzzer is mapped to
684 * BTN_TRIGGER_HAPPY14
685 */
686 [ 1] = BTN_TRIGGER_HAPPY1,
687 [ 2] = BTN_TRIGGER_HAPPY2,
688 [ 3] = BTN_TRIGGER_HAPPY3,
689 [ 4] = BTN_TRIGGER_HAPPY4,
690 [ 5] = BTN_TRIGGER_HAPPY5,
691 [ 6] = BTN_TRIGGER_HAPPY6,
692 [ 7] = BTN_TRIGGER_HAPPY7,
693 [ 8] = BTN_TRIGGER_HAPPY8,
694 [ 9] = BTN_TRIGGER_HAPPY9,
695 [10] = BTN_TRIGGER_HAPPY10,
696 [11] = BTN_TRIGGER_HAPPY11,
697 [12] = BTN_TRIGGER_HAPPY12,
698 [13] = BTN_TRIGGER_HAPPY13,
699 [14] = BTN_TRIGGER_HAPPY14,
700 [15] = BTN_TRIGGER_HAPPY15,
701 [16] = BTN_TRIGGER_HAPPY16,
702 [17] = BTN_TRIGGER_HAPPY17,
703 [18] = BTN_TRIGGER_HAPPY18,
704 [19] = BTN_TRIGGER_HAPPY19,
705 [20] = BTN_TRIGGER_HAPPY20,
706};
707
Frank Praznikd902f472014-01-27 10:17:36 -0500708static enum power_supply_property sony_battery_props[] = {
709 POWER_SUPPLY_PROP_PRESENT,
710 POWER_SUPPLY_PROP_CAPACITY,
711 POWER_SUPPLY_PROP_SCOPE,
712 POWER_SUPPLY_PROP_STATUS,
713};
714
Frank Praznik55d3b662014-04-14 10:11:32 -0400715struct sixaxis_led {
716 __u8 time_enabled; /* the total time the led is active (0xff means forever) */
717 __u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
718 __u8 enabled;
719 __u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
720 __u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
721} __packed;
722
723struct sixaxis_rumble {
724 __u8 padding;
725 __u8 right_duration; /* Right motor duration (0xff means forever) */
726 __u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
727 __u8 left_duration; /* Left motor duration (0xff means forever) */
728 __u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
729} __packed;
730
731struct sixaxis_output_report {
732 __u8 report_id;
733 struct sixaxis_rumble rumble;
734 __u8 padding[4];
735 __u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
736 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
737 struct sixaxis_led _reserved; /* LED5, not actually soldered */
738} __packed;
739
740union sixaxis_output_report_01 {
741 struct sixaxis_output_report data;
742 __u8 buf[36];
743};
744
Frank Praznikd2d782f2014-02-20 11:36:03 -0500745static spinlock_t sony_dev_list_lock;
746static LIST_HEAD(sony_device_list);
Frank Praznik80250872014-04-14 10:11:35 -0400747static DEFINE_IDA(sony_device_id_allocator);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500748
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200749struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500750 spinlock_t lock;
Frank Praznikd2d782f2014-02-20 11:36:03 -0500751 struct list_head list_node;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100752 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500753 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200754 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100755 struct work_struct state_worker;
Frank Praznikd902f472014-01-27 10:17:36 -0500756 struct power_supply battery;
Frank Praznik80250872014-04-14 10:11:35 -0400757 int device_id;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200758
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100759#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100760 __u8 left;
761 __u8 right;
762#endif
763
Frank Praznikd2d782f2014-02-20 11:36:03 -0500764 __u8 mac_address[6];
Frank Praznik5f5750d2014-02-19 13:09:22 -0500765 __u8 worker_initialized;
Frank Praznikd902f472014-01-27 10:17:36 -0500766 __u8 cable_state;
767 __u8 battery_charging;
768 __u8 battery_capacity;
Frank Praznik60781cf2014-01-11 15:13:15 -0500769 __u8 led_state[MAX_LEDS];
Frank Praznikb3ed4582014-04-14 10:11:36 -0400770 __u8 led_delay_on[MAX_LEDS];
771 __u8 led_delay_off[MAX_LEDS];
Frank Praznik60781cf2014-01-11 15:13:15 -0500772 __u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200773};
774
Antonio Ospitec607fb82014-06-24 13:28:41 +0200775static __u8 *sixaxis_fixup(struct hid_device *hdev, __u8 *rdesc,
776 unsigned int *rsize)
777{
778 *rsize = sizeof(sixaxis_rdesc);
779 return sixaxis_rdesc;
780}
781
Jiri Kosina078328d2013-06-13 12:03:49 +0200782static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
783 unsigned int *rsize)
784{
785 *rsize = sizeof(ps3remote_rdesc);
786 return ps3remote_rdesc;
787}
788
789static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
790 struct hid_field *field, struct hid_usage *usage,
791 unsigned long **bit, int *max)
792{
793 unsigned int key = usage->hid & HID_USAGE;
794
795 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
796 return -1;
797
798 switch (usage->collection_index) {
799 case 1:
800 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
801 return -1;
802
803 key = ps3remote_keymap_joypad_buttons[key];
804 if (!key)
805 return -1;
806 break;
807 case 2:
808 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
809 return -1;
810
811 key = ps3remote_keymap_remote_buttons[key];
812 if (!key)
813 return -1;
814 break;
815 default:
816 return -1;
817 }
818
819 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
820 return 1;
821}
822
823
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200824/* Sony Vaio VGX has wrongly mouse pointer declared as constant */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400825static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
826 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200827{
828 struct sony_sc *sc = hid_get_drvdata(hdev);
829
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900830 /*
831 * Some Sony RF receivers wrongly declare the mouse pointer as a
832 * a constant non-data variable.
833 */
834 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
835 /* usage page: generic desktop controls */
836 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
837 /* usage: mouse */
838 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
839 /* input (usage page for x,y axes): constant, variable, relative */
840 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa46491842013-01-15 19:40:48 +0900841 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900842 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200843 rdesc[55] = 0x06;
844 }
Simon Wood61ab44b2011-06-10 12:00:26 +0200845
Frank Prazniked19d8c2014-01-16 21:43:12 -0500846 /*
847 * The default Dualshock 4 USB descriptor doesn't assign
848 * the gyroscope values to corresponding axes so we need a
849 * modified one.
850 */
851 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
852 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
853 rdesc = dualshock4_usb_rdesc;
854 *rsize = sizeof(dualshock4_usb_rdesc);
Frank Praznikd8296742014-02-05 20:03:45 -0500855 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
856 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
857 rdesc = dualshock4_bt_rdesc;
858 *rsize = sizeof(dualshock4_bt_rdesc);
Frank Prazniked19d8c2014-01-16 21:43:12 -0500859 }
860
Antonio Ospitec607fb82014-06-24 13:28:41 +0200861 if (sc->quirks & SIXAXIS_CONTROLLER)
862 return sixaxis_fixup(hdev, rdesc, rsize);
Jiri Kosina078328d2013-06-13 12:03:49 +0200863
864 if (sc->quirks & PS3REMOTE)
865 return ps3remote_fixup(hdev, rdesc, rsize);
866
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400867 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200868}
869
Frank Praznikd902f472014-01-27 10:17:36 -0500870static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
871{
872 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
873 unsigned long flags;
874 __u8 cable_state, battery_capacity, battery_charging;
875
Frank Praznikad142b92014-02-20 11:36:00 -0500876 /*
877 * The sixaxis is charging if the battery value is 0xee
Frank Praznikd902f472014-01-27 10:17:36 -0500878 * and it is fully charged if the value is 0xef.
879 * It does not report the actual level while charging so it
880 * is set to 100% while charging is in progress.
881 */
882 if (rd[30] >= 0xee) {
883 battery_capacity = 100;
Frank Praznika43e94a2014-02-15 13:35:42 -0500884 battery_charging = !(rd[30] & 0x01);
Frank Praznikd902f472014-01-27 10:17:36 -0500885 } else {
Frank Praznikac3c9a92014-02-20 11:36:02 -0500886 __u8 index = rd[30] <= 5 ? rd[30] : 5;
887 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -0500888 battery_charging = 0;
889 }
Frank Praznik914c5782014-03-15 09:41:15 -0400890 cable_state = !(rd[31] & 0x04);
Frank Praznikd902f472014-01-27 10:17:36 -0500891
892 spin_lock_irqsave(&sc->lock, flags);
893 sc->cable_state = cable_state;
894 sc->battery_capacity = battery_capacity;
895 sc->battery_charging = battery_charging;
896 spin_unlock_irqrestore(&sc->lock, flags);
897}
898
899static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
900{
Frank Praznike5606232014-01-27 10:17:37 -0500901 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
902 struct hid_input, list);
903 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -0500904 unsigned long flags;
Frank Praznik6c5f8602014-02-05 20:03:47 -0500905 int n, offset;
Frank Praznikd902f472014-01-27 10:17:36 -0500906 __u8 cable_state, battery_capacity, battery_charging;
907
Frank Praznikad142b92014-02-20 11:36:00 -0500908 /*
909 * Battery and touchpad data starts at byte 30 in the USB report and
Frank Praznik6c5f8602014-02-05 20:03:47 -0500910 * 32 in Bluetooth report.
911 */
912 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
913
Frank Praznikad142b92014-02-20 11:36:00 -0500914 /*
915 * The lower 4 bits of byte 30 contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -0500916 * and the 5th bit contains the USB cable state.
917 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500918 cable_state = (rd[offset] >> 4) & 0x01;
919 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -0500920
Frank Praznikad142b92014-02-20 11:36:00 -0500921 /*
922 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -0500923 * 0 to 10, and when running on battery power it ranges from 0 to 9.
924 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -0500925 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500926 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -0500927 battery_charging = 0;
928 else
929 battery_charging = 1;
930
Frank Praznik6c5f8602014-02-05 20:03:47 -0500931 if (!cable_state)
932 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -0500933 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -0500934 battery_capacity = 10;
935
Frank Praznikd902f472014-01-27 10:17:36 -0500936 battery_capacity *= 10;
937
938 spin_lock_irqsave(&sc->lock, flags);
939 sc->cable_state = cable_state;
940 sc->battery_capacity = battery_capacity;
941 sc->battery_charging = battery_charging;
942 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -0500943
Frank Praznik6c5f8602014-02-05 20:03:47 -0500944 offset += 5;
945
Frank Praznikad142b92014-02-20 11:36:00 -0500946 /*
947 * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
Frank Praznik6c5f8602014-02-05 20:03:47 -0500948 * and 37 on Bluetooth.
Frank Praznike5606232014-01-27 10:17:37 -0500949 * The first 7 bits of the first byte is a counter and bit 8 is a touch
950 * indicator that is 0 when pressed and 1 when not pressed.
951 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
952 * The data for the second touch is in the same format and immediatly
953 * follows the data for the first.
954 */
955 for (n = 0; n < 2; n++) {
956 __u16 x, y;
957
958 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
959 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
960
961 input_mt_slot(input_dev, n);
962 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
963 !(rd[offset] >> 7));
964 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
965 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
966
967 offset += 4;
968 }
Frank Praznikd902f472014-01-27 10:17:36 -0500969}
970
Simon Woodc9e4d872011-06-10 12:00:27 +0200971static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
972 __u8 *rd, int size)
973{
974 struct sony_sc *sc = hid_get_drvdata(hdev);
975
Frank Praznikad142b92014-02-20 11:36:00 -0500976 /*
977 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +0200978 * has to be BYTE_SWAPPED before passing up to joystick interface
979 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -0500980 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Simon Woodc9e4d872011-06-10 12:00:27 +0200981 swap(rd[41], rd[42]);
982 swap(rd[43], rd[44]);
983 swap(rd[45], rd[46]);
984 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -0500985
986 sixaxis_parse_report(sc, rd, size);
Frank Praznik68330d82014-02-05 20:03:49 -0500987 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
988 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
989 && rd[0] == 0x11 && size == 78)) {
Frank Praznikd902f472014-01-27 10:17:36 -0500990 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +0200991 }
992
993 return 0;
994}
995
Colin Leitnerf04d5142013-05-27 23:41:05 +0200996static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
997 struct hid_field *field, struct hid_usage *usage,
998 unsigned long **bit, int *max)
999{
1000 struct sony_sc *sc = hid_get_drvdata(hdev);
1001
1002 if (sc->quirks & BUZZ_CONTROLLER) {
1003 unsigned int key = usage->hid & HID_USAGE;
1004
1005 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1006 return -1;
1007
1008 switch (usage->collection_index) {
1009 case 1:
1010 if (key >= ARRAY_SIZE(buzz_keymap))
1011 return -1;
1012
1013 key = buzz_keymap[key];
1014 if (!key)
1015 return -1;
1016 break;
1017 default:
1018 return -1;
1019 }
1020
1021 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1022 return 1;
1023 }
1024
Jiri Kosina078328d2013-06-13 12:03:49 +02001025 if (sc->quirks & PS3REMOTE)
1026 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1027
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001028 /* Let hid-core decide for the others */
1029 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001030}
1031
Antonio Ospite5710fab2011-02-20 18:26:45 +01001032/*
Jiri Slabybd28ce02008-06-25 23:47:04 +02001033 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1034 * to "operational". Without this, the ps3 controller will not report any
1035 * events.
1036 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001037static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001038{
Jiri Slabybd28ce02008-06-25 23:47:04 +02001039 int ret;
1040 char *buf = kmalloc(18, GFP_KERNEL);
1041
1042 if (!buf)
1043 return -ENOMEM;
1044
Benjamin Tissoirescafebc02014-02-05 16:33:22 -05001045 ret = hid_hw_raw_request(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT,
1046 HID_REQ_GET_REPORT);
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001047
Jiri Slabybd28ce02008-06-25 23:47:04 +02001048 if (ret < 0)
Joe Perches4291ee32010-12-09 19:29:03 -08001049 hid_err(hdev, "can't set operational mode\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +02001050
1051 kfree(buf);
1052
1053 return ret;
1054}
1055
Antonio Ospite816651a2010-05-03 22:15:55 +02001056static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001057{
Antonio Ospitefddb33f2010-05-03 17:19:03 +02001058 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
Benjamin Tissoiresb0dd72a2014-02-10 12:58:54 -05001059 return hid_hw_raw_request(hdev, buf[0], buf, sizeof(buf),
1060 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001061}
1062
Frank Praznikad142b92014-02-20 11:36:00 -05001063/*
1064 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
Frank Praznik68330d82014-02-05 20:03:49 -05001065 * controller so that it sends full input reports of type 0x11.
1066 */
1067static int dualshock4_set_operational_bt(struct hid_device *hdev)
1068{
1069 __u8 buf[37] = { 0 };
1070
1071 return hid_hw_raw_request(hdev, 0x02, buf, sizeof(buf),
1072 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001073}
1074
Frank Praznik80250872014-04-14 10:11:35 -04001075static void sixaxis_set_leds_from_id(int id, __u8 values[MAX_LEDS])
1076{
1077 static const __u8 sixaxis_leds[10][4] = {
1078 { 0x01, 0x00, 0x00, 0x00 },
1079 { 0x00, 0x01, 0x00, 0x00 },
1080 { 0x00, 0x00, 0x01, 0x00 },
1081 { 0x00, 0x00, 0x00, 0x01 },
1082 { 0x01, 0x00, 0x00, 0x01 },
1083 { 0x00, 0x01, 0x00, 0x01 },
1084 { 0x00, 0x00, 0x01, 0x01 },
1085 { 0x01, 0x00, 0x01, 0x01 },
1086 { 0x00, 0x01, 0x01, 0x01 },
1087 { 0x01, 0x01, 0x01, 0x01 }
1088 };
1089
1090 BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
1091
1092 if (id < 0)
1093 return;
1094
1095 id %= 10;
1096 memcpy(values, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
1097}
1098
1099static void dualshock4_set_leds_from_id(int id, __u8 values[MAX_LEDS])
1100{
1101 /* The first 4 color/index entries match what the PS4 assigns */
1102 static const __u8 color_code[7][3] = {
1103 /* Blue */ { 0x00, 0x00, 0x01 },
1104 /* Red */ { 0x01, 0x00, 0x00 },
1105 /* Green */ { 0x00, 0x01, 0x00 },
1106 /* Pink */ { 0x02, 0x00, 0x01 },
1107 /* Orange */ { 0x02, 0x01, 0x00 },
1108 /* Teal */ { 0x00, 0x01, 0x01 },
1109 /* White */ { 0x01, 0x01, 0x01 }
1110 };
1111
1112 BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
1113
1114 if (id < 0)
1115 return;
1116
1117 id %= 7;
1118 memcpy(values, color_code[id], sizeof(color_code[id]));
1119}
1120
Frank Praznik60781cf2014-01-11 15:13:15 -05001121static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001122{
1123 struct list_head *report_list =
1124 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1125 struct hid_report *report = list_entry(report_list->next,
1126 struct hid_report, list);
1127 __s32 *value = report->field[0]->value;
1128
1129 value[0] = 0x00;
Frank Praznik60781cf2014-01-11 15:13:15 -05001130 value[1] = leds[0] ? 0xff : 0x00;
1131 value[2] = leds[1] ? 0xff : 0x00;
1132 value[3] = leds[2] ? 0xff : 0x00;
1133 value[4] = leds[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001134 value[5] = 0x00;
1135 value[6] = 0x00;
1136 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1137}
1138
Frank Praznikfa57a812014-04-14 10:11:33 -04001139static void sony_set_leds(struct sony_sc *sc, const __u8 *leds, int count)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001140{
Frank Praznik60781cf2014-01-11 15:13:15 -05001141 int n;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001142
Frank Praznik60781cf2014-01-11 15:13:15 -05001143 BUG_ON(count > MAX_LEDS);
1144
Frank Praznikfa57a812014-04-14 10:11:33 -04001145 if (sc->quirks & BUZZ_CONTROLLER && count == 4) {
1146 buzz_set_leds(sc->hdev, leds);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001147 } else {
Frank Praznik60781cf2014-01-11 15:13:15 -05001148 for (n = 0; n < count; n++)
Frank Praznikfa57a812014-04-14 10:11:33 -04001149 sc->led_state[n] = leds[n];
1150 schedule_work(&sc->state_worker);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001151 }
1152}
1153
Sven Eckelmannc5382512013-11-19 20:26:30 +01001154static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001155 enum led_brightness value)
1156{
1157 struct device *dev = led->dev->parent;
1158 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1159 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001160
1161 int n;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001162 int force_update;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001163
1164 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001165 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001166 hid_err(hdev, "No device data\n");
1167 return;
1168 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001169
Frank Praznikb3ed4582014-04-14 10:11:36 -04001170 /*
1171 * The Sixaxis on USB will override any LED settings sent to it
1172 * and keep flashing all of the LEDs until the PS button is pressed.
1173 * Updates, even if redundant, must be always be sent to the
1174 * controller to avoid having to toggle the state of an LED just to
1175 * stop the flashing later on.
1176 */
1177 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1178
Frank Praznik60781cf2014-01-11 15:13:15 -05001179 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznikb3ed4582014-04-14 10:11:36 -04001180 if (led == drv_data->leds[n] && (force_update ||
1181 (value != drv_data->led_state[n] ||
1182 drv_data->led_delay_on[n] ||
1183 drv_data->led_delay_off[n]))) {
1184
1185 drv_data->led_state[n] = value;
1186
1187 /* Setting the brightness stops the blinking */
1188 drv_data->led_delay_on[n] = 0;
1189 drv_data->led_delay_off[n] = 0;
1190
1191 sony_set_leds(drv_data, drv_data->led_state,
1192 drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001193 break;
1194 }
1195 }
1196}
1197
Sven Eckelmannc5382512013-11-19 20:26:30 +01001198static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001199{
1200 struct device *dev = led->dev->parent;
1201 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1202 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001203
1204 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001205
1206 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001207 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001208 hid_err(hdev, "No device data\n");
1209 return LED_OFF;
1210 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001211
Frank Praznik60781cf2014-01-11 15:13:15 -05001212 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001213 if (led == drv_data->leds[n])
1214 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001215 }
1216
Simon Wood7db75042014-02-05 12:34:18 -07001217 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001218}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001219
Frank Praznikb3ed4582014-04-14 10:11:36 -04001220static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1221 unsigned long *delay_off)
1222{
1223 struct device *dev = led->dev->parent;
1224 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1225 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1226 int n;
1227 __u8 new_on, new_off;
1228
1229 if (!drv_data) {
1230 hid_err(hdev, "No device data\n");
1231 return -EINVAL;
1232 }
1233
1234 /* Max delay is 255 deciseconds or 2550 milliseconds */
1235 if (*delay_on > 2550)
1236 *delay_on = 2550;
1237 if (*delay_off > 2550)
1238 *delay_off = 2550;
1239
1240 /* Blink at 1 Hz if both values are zero */
1241 if (!*delay_on && !*delay_off)
1242 *delay_on = *delay_off = 500;
1243
1244 new_on = *delay_on / 10;
1245 new_off = *delay_off / 10;
1246
1247 for (n = 0; n < drv_data->led_count; n++) {
1248 if (led == drv_data->leds[n])
1249 break;
1250 }
1251
1252 /* This LED is not registered on this device */
1253 if (n >= drv_data->led_count)
1254 return -EINVAL;
1255
1256 /* Don't schedule work if the values didn't change */
1257 if (new_on != drv_data->led_delay_on[n] ||
1258 new_off != drv_data->led_delay_off[n]) {
1259 drv_data->led_delay_on[n] = new_on;
1260 drv_data->led_delay_off[n] = new_off;
1261 schedule_work(&drv_data->state_worker);
1262 }
1263
1264 return 0;
1265}
1266
Frank Praznikfa57a812014-04-14 10:11:33 -04001267static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001268{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001269 struct led_classdev *led;
1270 int n;
1271
Frank Praznikfa57a812014-04-14 10:11:33 -04001272 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001273
Frank Praznikfa57a812014-04-14 10:11:33 -04001274 for (n = 0; n < sc->led_count; n++) {
1275 led = sc->leds[n];
1276 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001277 if (!led)
1278 continue;
1279 led_classdev_unregister(led);
1280 kfree(led);
1281 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001282
Frank Praznikfa57a812014-04-14 10:11:33 -04001283 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001284}
1285
Frank Praznikfa57a812014-04-14 10:11:33 -04001286static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001287{
Frank Praznikfa57a812014-04-14 10:11:33 -04001288 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001289 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001290 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001291 struct led_classdev *led;
1292 size_t name_sz;
1293 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001294 size_t name_len;
1295 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001296 static const char * const ds4_name_str[] = { "red", "green", "blue",
1297 "global" };
Frank Praznik80250872014-04-14 10:11:35 -04001298 __u8 initial_values[MAX_LEDS] = { 0 };
Frank Praznikb3ed4582014-04-14 10:11:36 -04001299 __u8 max_brightness[MAX_LEDS] = { 1 };
1300 __u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001301
Frank Praznikfa57a812014-04-14 10:11:33 -04001302 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001303
Frank Praznikfa57a812014-04-14 10:11:33 -04001304 if (sc->quirks & BUZZ_CONTROLLER) {
1305 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001306 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001307 name_len = strlen("::buzz#");
1308 name_fmt = "%s::buzz%d";
1309 /* Validate expected report characteristics. */
1310 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1311 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001312 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik80250872014-04-14 10:11:35 -04001313 dualshock4_set_leds_from_id(sc->device_id, initial_values);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001314 initial_values[3] = 1;
1315 sc->led_count = 4;
1316 memset(max_brightness, 255, 3);
1317 use_hw_blink[3] = 1;
1318 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001319 name_len = 0;
1320 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -05001321 } else {
Frank Praznik80250872014-04-14 10:11:35 -04001322 sixaxis_set_leds_from_id(sc->device_id, initial_values);
Frank Praznikfa57a812014-04-14 10:11:33 -04001323 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001324 memset(use_hw_blink, 1, 4);
1325 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001326 name_len = strlen("::sony#");
1327 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001328 }
1329
Frank Praznikad142b92014-02-20 11:36:00 -05001330 /*
1331 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001332 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001333 * LEDs to on
1334 */
Frank Praznikfa57a812014-04-14 10:11:33 -04001335 sony_set_leds(sc, initial_values, sc->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001336
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001337 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001338
Frank Praznikfa57a812014-04-14 10:11:33 -04001339 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001340
Frank Praznikb3ed4582014-04-14 10:11:36 -04001341 if (use_ds4_names)
1342 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001343
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001344 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1345 if (!led) {
1346 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001347 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001348 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001349 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001350
1351 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001352 if (use_ds4_names)
1353 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1354 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001355 else
1356 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001357 led->name = name;
Frank Praznik80250872014-04-14 10:11:35 -04001358 led->brightness = initial_values[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001359 led->max_brightness = max_brightness[n];
Sven Eckelmannc5382512013-11-19 20:26:30 +01001360 led->brightness_get = sony_led_get_brightness;
1361 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001362
Frank Praznikb3ed4582014-04-14 10:11:36 -04001363 if (use_hw_blink[n])
1364 led->blink_set = sony_led_blink_set;
1365
Frank Praznik80250872014-04-14 10:11:35 -04001366 sc->leds[n] = led;
1367
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001368 ret = led_classdev_register(&hdev->dev, led);
1369 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001370 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001371 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001372 kfree(led);
1373 goto error_leds;
1374 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001375 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001376
1377 return ret;
1378
Colin Leitnerf04d5142013-05-27 23:41:05 +02001379error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001380 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001381
Colin Leitnerf04d5142013-05-27 23:41:05 +02001382 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001383}
1384
Frank Praznikcad665a2014-01-11 15:13:54 -05001385static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001386{
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001387 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001388 int n;
Frank Praznik55d3b662014-04-14 10:11:32 -04001389 union sixaxis_output_report_01 report = {
1390 .buf = {
1391 0x01,
1392 0x00, 0xff, 0x00, 0xff, 0x00,
1393 0x00, 0x00, 0x00, 0x00, 0x00,
1394 0xff, 0x27, 0x10, 0x00, 0x32,
1395 0xff, 0x27, 0x10, 0x00, 0x32,
1396 0xff, 0x27, 0x10, 0x00, 0x32,
1397 0xff, 0x27, 0x10, 0x00, 0x32,
1398 0x00, 0x00, 0x00, 0x00, 0x00
1399 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001400 };
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001401
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001402#ifdef CONFIG_SONY_FF
Frank Praznik55d3b662014-04-14 10:11:32 -04001403 report.data.rumble.right_motor_on = sc->right ? 1 : 0;
1404 report.data.rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001405#endif
1406
Frank Praznik55d3b662014-04-14 10:11:32 -04001407 report.data.leds_bitmap |= sc->led_state[0] << 1;
1408 report.data.leds_bitmap |= sc->led_state[1] << 2;
1409 report.data.leds_bitmap |= sc->led_state[2] << 3;
1410 report.data.leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001411
Simon Wood88f65762014-04-14 10:11:37 -04001412 /* Set flag for all leds off, required for 3rd party INTEC controller */
1413 if ((report.data.leds_bitmap & 0x1E) == 0)
1414 report.data.leds_bitmap |= 0x20;
1415
Frank Praznikb3ed4582014-04-14 10:11:36 -04001416 /*
1417 * The LEDs in the report are indexed in reverse order to their
1418 * corresponding light on the controller.
1419 * Index 0 = LED 4, index 1 = LED 3, etc...
1420 *
1421 * In the case of both delay values being zero (blinking disabled) the
1422 * default report values should be used or the controller LED will be
1423 * always off.
1424 */
1425 for (n = 0; n < 4; n++) {
1426 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
1427 report.data.led[3 - n].duty_off = sc->led_delay_off[n];
1428 report.data.led[3 - n].duty_on = sc->led_delay_on[n];
1429 }
1430 }
1431
Frank Praznik55d3b662014-04-14 10:11:32 -04001432 hid_hw_raw_request(sc->hdev, report.data.report_id, report.buf,
1433 sizeof(report), HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001434}
1435
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001436static void dualshock4_state_worker(struct work_struct *work)
1437{
1438 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001439 struct hid_device *hdev = sc->hdev;
Frank Praznik48220232014-02-05 20:03:44 -05001440 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001441
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001442 __u8 buf[78] = { 0 };
Frank Praznik48220232014-02-05 20:03:44 -05001443
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001444 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1445 buf[0] = 0x05;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001446 buf[1] = 0xFF;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001447 offset = 4;
1448 } else {
1449 buf[0] = 0x11;
1450 buf[1] = 0xB0;
1451 buf[3] = 0x0F;
1452 offset = 6;
1453 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001454
1455#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001456 buf[offset++] = sc->right;
1457 buf[offset++] = sc->left;
1458#else
1459 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001460#endif
1461
Frank Praznikb3ed4582014-04-14 10:11:36 -04001462 /* LED 3 is the global control */
1463 if (sc->led_state[3]) {
1464 buf[offset++] = sc->led_state[0];
1465 buf[offset++] = sc->led_state[1];
1466 buf[offset++] = sc->led_state[2];
1467 } else {
1468 offset += 3;
1469 }
1470
1471 /* If both delay values are zero the DualShock 4 disables blinking. */
1472 buf[offset++] = sc->led_delay_on[3];
1473 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05001474
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001475 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1476 hid_hw_output_report(hdev, buf, 32);
1477 else
1478 hid_hw_raw_request(hdev, 0x11, buf, 78,
1479 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001480}
1481
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001482#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001483static int sony_play_effect(struct input_dev *dev, void *data,
1484 struct ff_effect *effect)
1485{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001486 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001487 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001488
1489 if (effect->type != FF_RUMBLE)
1490 return 0;
1491
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001492 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001493 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001494
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001495 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001496 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001497}
1498
Frank Praznikfa57a812014-04-14 10:11:33 -04001499static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001500{
Frank Praznikfa57a812014-04-14 10:11:33 -04001501 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001502 struct hid_input, list);
1503 struct input_dev *input_dev = hidinput->input;
1504
1505 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1506 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1507}
1508
1509#else
Frank Praznikfa57a812014-04-14 10:11:33 -04001510static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001511{
1512 return 0;
1513}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001514
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001515#endif
1516
Frank Praznikd902f472014-01-27 10:17:36 -05001517static int sony_battery_get_property(struct power_supply *psy,
1518 enum power_supply_property psp,
1519 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001520{
Frank Praznikd902f472014-01-27 10:17:36 -05001521 struct sony_sc *sc = container_of(psy, struct sony_sc, battery);
1522 unsigned long flags;
1523 int ret = 0;
1524 u8 battery_charging, battery_capacity, cable_state;
1525
1526 spin_lock_irqsave(&sc->lock, flags);
1527 battery_charging = sc->battery_charging;
1528 battery_capacity = sc->battery_capacity;
1529 cable_state = sc->cable_state;
1530 spin_unlock_irqrestore(&sc->lock, flags);
1531
1532 switch (psp) {
1533 case POWER_SUPPLY_PROP_PRESENT:
1534 val->intval = 1;
1535 break;
1536 case POWER_SUPPLY_PROP_SCOPE:
1537 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1538 break;
1539 case POWER_SUPPLY_PROP_CAPACITY:
1540 val->intval = battery_capacity;
1541 break;
1542 case POWER_SUPPLY_PROP_STATUS:
1543 if (battery_charging)
1544 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1545 else
1546 if (battery_capacity == 100 && cable_state)
1547 val->intval = POWER_SUPPLY_STATUS_FULL;
1548 else
1549 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1550 break;
1551 default:
1552 ret = -EINVAL;
1553 break;
1554 }
1555 return ret;
1556}
1557
1558static int sony_battery_probe(struct sony_sc *sc)
1559{
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001560 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05001561 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001562
Frank Praznikad142b92014-02-20 11:36:00 -05001563 /*
1564 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05001565 * if the battery is polled before the first device report is received.
1566 */
1567 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001568
Frank Praznikd902f472014-01-27 10:17:36 -05001569 sc->battery.properties = sony_battery_props;
1570 sc->battery.num_properties = ARRAY_SIZE(sony_battery_props);
1571 sc->battery.get_property = sony_battery_get_property;
1572 sc->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1573 sc->battery.use_for_apm = 0;
Frank Praznik314531f2014-04-14 10:11:34 -04001574 sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%pMR",
1575 sc->mac_address);
Frank Praznikd902f472014-01-27 10:17:36 -05001576 if (!sc->battery.name)
1577 return -ENOMEM;
1578
1579 ret = power_supply_register(&hdev->dev, &sc->battery);
1580 if (ret) {
1581 hid_err(hdev, "Unable to register battery device\n");
1582 goto err_free;
1583 }
1584
1585 power_supply_powers(&sc->battery, &hdev->dev);
1586 return 0;
1587
1588err_free:
1589 kfree(sc->battery.name);
1590 sc->battery.name = NULL;
1591 return ret;
1592}
1593
1594static void sony_battery_remove(struct sony_sc *sc)
1595{
1596 if (!sc->battery.name)
1597 return;
1598
1599 power_supply_unregister(&sc->battery);
1600 kfree(sc->battery.name);
1601 sc->battery.name = NULL;
1602}
1603
Frank Praznike5606232014-01-27 10:17:37 -05001604static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
1605 int w, int h)
1606{
1607 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1608 struct hid_input, list);
1609 struct input_dev *input_dev = hidinput->input;
1610 int ret;
1611
1612 ret = input_mt_init_slots(input_dev, touch_count, 0);
1613 if (ret < 0) {
1614 hid_err(sc->hdev, "Unable to initialize multi-touch slots\n");
1615 return ret;
1616 }
1617
1618 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1619 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1620
1621 return 0;
1622}
1623
Frank Praznikd2d782f2014-02-20 11:36:03 -05001624/*
1625 * If a controller is plugged in via USB while already connected via Bluetooth
1626 * it will show up as two devices. A global list of connected controllers and
1627 * their MAC addresses is maintained to ensure that a device is only connected
1628 * once.
1629 */
1630static int sony_check_add_dev_list(struct sony_sc *sc)
1631{
1632 struct sony_sc *entry;
1633 unsigned long flags;
1634 int ret;
1635
1636 spin_lock_irqsave(&sony_dev_list_lock, flags);
1637
1638 list_for_each_entry(entry, &sony_device_list, list_node) {
1639 ret = memcmp(sc->mac_address, entry->mac_address,
1640 sizeof(sc->mac_address));
1641 if (!ret) {
1642 ret = -EEXIST;
1643 hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
1644 sc->mac_address);
1645 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001646 }
1647 }
1648
Frank Praznikd2d782f2014-02-20 11:36:03 -05001649 ret = 0;
1650 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001651
Frank Praznikd2d782f2014-02-20 11:36:03 -05001652unlock:
1653 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1654 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001655}
1656
Frank Praznikd2d782f2014-02-20 11:36:03 -05001657static void sony_remove_dev_list(struct sony_sc *sc)
1658{
1659 unsigned long flags;
1660
1661 if (sc->list_node.next) {
1662 spin_lock_irqsave(&sony_dev_list_lock, flags);
1663 list_del(&(sc->list_node));
1664 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1665 }
1666}
1667
1668static int sony_get_bt_devaddr(struct sony_sc *sc)
1669{
1670 int ret;
1671
1672 /* HIDP stores the device MAC address as a string in the uniq field. */
1673 ret = strlen(sc->hdev->uniq);
1674 if (ret != 17)
1675 return -EINVAL;
1676
1677 ret = sscanf(sc->hdev->uniq,
1678 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1679 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
1680 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
1681
1682 if (ret != 6)
1683 return -EINVAL;
1684
1685 return 0;
1686}
1687
1688static int sony_check_add(struct sony_sc *sc)
1689{
1690 int n, ret;
1691
1692 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
1693 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
1694 /*
1695 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
1696 * address from the uniq string where HIDP stores it.
1697 * As uniq cannot be guaranteed to be a MAC address in all cases
1698 * a failure of this function should not prevent the connection.
1699 */
1700 if (sony_get_bt_devaddr(sc) < 0) {
1701 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
1702 return 0;
1703 }
1704 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1705 __u8 buf[7];
1706
1707 /*
1708 * The MAC address of a DS4 controller connected via USB can be
1709 * retrieved with feature report 0x81. The address begins at
1710 * offset 1.
1711 */
1712 ret = hid_hw_raw_request(sc->hdev, 0x81, buf, sizeof(buf),
1713 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1714
1715 if (ret != 7) {
1716 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
1717 return ret < 0 ? ret : -EINVAL;
1718 }
1719
1720 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
1721 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1722 __u8 buf[18];
1723
1724 /*
1725 * The MAC address of a Sixaxis controller connected via USB can
1726 * be retrieved with feature report 0xf2. The address begins at
1727 * offset 4.
1728 */
1729 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf, sizeof(buf),
1730 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1731
1732 if (ret != 18) {
1733 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
1734 return ret < 0 ? ret : -EINVAL;
1735 }
1736
1737 /*
1738 * The Sixaxis device MAC in the report is big-endian and must
1739 * be byte-swapped.
1740 */
1741 for (n = 0; n < 6; n++)
1742 sc->mac_address[5-n] = buf[4+n];
1743 } else {
1744 return 0;
1745 }
1746
1747 return sony_check_add_dev_list(sc);
1748}
1749
Frank Praznik80250872014-04-14 10:11:35 -04001750static int sony_set_device_id(struct sony_sc *sc)
1751{
1752 int ret;
1753
1754 /*
1755 * Only DualShock 4 or Sixaxis controllers get an id.
1756 * All others are set to -1.
1757 */
1758 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
1759 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
1760 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
1761 GFP_KERNEL);
1762 if (ret < 0) {
1763 sc->device_id = -1;
1764 return ret;
1765 }
1766 sc->device_id = ret;
1767 } else {
1768 sc->device_id = -1;
1769 }
1770
1771 return 0;
1772}
1773
1774static void sony_release_device_id(struct sony_sc *sc)
1775{
1776 if (sc->device_id >= 0) {
1777 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
1778 sc->device_id = -1;
1779 }
1780}
1781
Frank Praznik46262042014-04-14 10:11:31 -04001782static inline void sony_init_work(struct sony_sc *sc,
1783 void (*worker)(struct work_struct *))
1784{
1785 if (!sc->worker_initialized)
1786 INIT_WORK(&sc->state_worker, worker);
1787
1788 sc->worker_initialized = 1;
1789}
1790
1791static inline void sony_cancel_work_sync(struct sony_sc *sc)
1792{
1793 if (sc->worker_initialized)
1794 cancel_work_sync(&sc->state_worker);
1795}
Frank Praznikd2d782f2014-02-20 11:36:03 -05001796
Jiri Slabybd28ce02008-06-25 23:47:04 +02001797static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1798{
1799 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001800 unsigned long quirks = id->driver_data;
1801 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001802 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001803
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001804 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001805 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08001806 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001807 return -ENOMEM;
1808 }
1809
1810 sc->quirks = quirks;
1811 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001812 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001813
Jiri Slabybd28ce02008-06-25 23:47:04 +02001814 ret = hid_parse(hdev);
1815 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001816 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001817 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001818 }
1819
Colin Leitnerf04d5142013-05-27 23:41:05 +02001820 if (sc->quirks & VAIO_RDESC_CONSTANT)
1821 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
Antonio Ospite50764652014-06-24 13:28:40 +02001822 else if (sc->quirks & SIXAXIS_CONTROLLER)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001823 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1824
1825 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001826 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001827 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001828 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001829 }
1830
Frank Praznik80250872014-04-14 10:11:35 -04001831 ret = sony_set_device_id(sc);
1832 if (ret < 0) {
1833 hid_err(hdev, "failed to allocate the device id\n");
1834 goto err_stop;
1835 }
1836
Antonio Ospite569b10a2010-10-19 16:13:10 +02001837 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
Benjamin Tissoirese534a932014-03-08 22:52:42 -05001838 /*
1839 * The Sony Sixaxis does not handle HID Output Reports on the
1840 * Interrupt EP like it could, so we need to force HID Output
1841 * Reports to use HID_REQ_SET_REPORT on the Control EP.
1842 *
1843 * There is also another issue about HID Output Reports via USB,
1844 * the Sixaxis does not want the report_id as part of the data
1845 * packet, so we have to discard buf[0] when sending the actual
1846 * control message, even for numbered reports, humpf!
1847 */
1848 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
1849 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
Antonio Ospite816651a2010-05-03 22:15:55 +02001850 ret = sixaxis_set_operational_usb(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04001851 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001852 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04001853 /*
1854 * The Sixaxis wants output reports sent on the ctrl endpoint
1855 * when connected via Bluetooth.
1856 */
1857 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Antonio Ospite816651a2010-05-03 22:15:55 +02001858 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04001859 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001860 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik68330d82014-02-05 20:03:49 -05001861 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04001862 /*
1863 * The DualShock 4 wants output reports sent on the ctrl
1864 * endpoint when connected via Bluetooth.
1865 */
1866 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Frank Praznik68330d82014-02-05 20:03:49 -05001867 ret = dualshock4_set_operational_bt(hdev);
1868 if (ret < 0) {
1869 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
1870 goto err_stop;
1871 }
1872 }
Frank Praznikad142b92014-02-20 11:36:00 -05001873 /*
1874 * The Dualshock 4 touchpad supports 2 touches and has a
Frank Praznike5606232014-01-27 10:17:37 -05001875 * resolution of 1920x940.
1876 */
1877 ret = sony_register_touchpad(sc, 2, 1920, 940);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001878 if (ret < 0)
1879 goto err_stop;
1880
Frank Praznik46262042014-04-14 10:11:31 -04001881 sony_init_work(sc, dualshock4_state_worker);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001882 } else {
1883 ret = 0;
1884 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001885
Jiri Kosina4dfdc462008-12-30 00:49:59 +01001886 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001887 goto err_stop;
1888
Frank Praznikd2d782f2014-02-20 11:36:03 -05001889 ret = sony_check_add(sc);
1890 if (ret < 0)
1891 goto err_stop;
1892
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001893 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04001894 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001895 if (ret < 0)
1896 goto err_stop;
1897 }
1898
Frank Praznikd902f472014-01-27 10:17:36 -05001899 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1900 ret = sony_battery_probe(sc);
1901 if (ret < 0)
1902 goto err_stop;
1903
1904 /* Open the device to receive reports with battery info */
1905 ret = hid_hw_open(hdev);
1906 if (ret < 0) {
1907 hid_err(hdev, "hw open failed\n");
1908 goto err_stop;
1909 }
1910 }
1911
Frank Praznikc8de9db2014-02-20 11:36:01 -05001912 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04001913 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05001914 if (ret < 0)
1915 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05001916 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001917
Jiri Slabybd28ce02008-06-25 23:47:04 +02001918 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001919err_close:
1920 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001921err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001922 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04001923 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05001924 if (sc->quirks & SONY_BATTERY_SUPPORT)
1925 sony_battery_remove(sc);
Frank Praznik46262042014-04-14 10:11:31 -04001926 sony_cancel_work_sync(sc);
Frank Praznikd2d782f2014-02-20 11:36:03 -05001927 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04001928 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001929 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001930 return ret;
1931}
1932
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001933static void sony_remove(struct hid_device *hdev)
1934{
Colin Leitnerf04d5142013-05-27 23:41:05 +02001935 struct sony_sc *sc = hid_get_drvdata(hdev);
1936
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001937 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04001938 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001939
Frank Praznikd902f472014-01-27 10:17:36 -05001940 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1941 hid_hw_close(hdev);
1942 sony_battery_remove(sc);
1943 }
1944
Frank Praznik46262042014-04-14 10:11:31 -04001945 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001946
Frank Praznikd2d782f2014-02-20 11:36:03 -05001947 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001948
Frank Praznik80250872014-04-14 10:11:35 -04001949 sony_release_device_id(sc);
1950
Jiri Slabybd28ce02008-06-25 23:47:04 +02001951 hid_hw_stop(hdev);
1952}
1953
1954static const struct hid_device_id sony_devices[] = {
1955 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1956 .driver_data = SIXAXIS_CONTROLLER_USB },
1957 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
1958 .driver_data = SIXAXIS_CONTROLLER_USB },
1959 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1960 .driver_data = SIXAXIS_CONTROLLER_BT },
1961 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
1962 .driver_data = VAIO_RDESC_CONSTANT },
1963 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
1964 .driver_data = VAIO_RDESC_CONSTANT },
1965 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
1966 * Logitech joystick from the device descriptor. */
1967 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
1968 .driver_data = BUZZ_CONTROLLER },
1969 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
1970 .driver_data = BUZZ_CONTROLLER },
1971 /* PS3 BD Remote Control */
1972 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
1973 .driver_data = PS3REMOTE },
1974 /* Logitech Harmony Adapter for PS3 */
1975 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
1976 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001977 /* Sony Dualshock 4 controllers for PS4 */
1978 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001979 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001980 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001981 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02001982 { }
1983};
1984MODULE_DEVICE_TABLE(hid, sony_devices);
1985
1986static struct hid_driver sony_driver = {
1987 .name = "sony",
1988 .id_table = sony_devices,
1989 .input_mapping = sony_mapping,
1990 .probe = sony_probe,
1991 .remove = sony_remove,
1992 .report_fixup = sony_report_fixup,
1993 .raw_event = sony_raw_event
1994};
Frank Praznik80250872014-04-14 10:11:35 -04001995
1996static int __init sony_init(void)
1997{
1998 dbg_hid("Sony:%s\n", __func__);
1999
2000 return hid_register_driver(&sony_driver);
2001}
2002
2003static void __exit sony_exit(void)
2004{
2005 dbg_hid("Sony:%s\n", __func__);
2006
2007 ida_destroy(&sony_device_id_allocator);
2008 hid_unregister_driver(&sony_driver);
2009}
2010module_init(sony_init);
2011module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002012
2013MODULE_LICENSE("GPL");