blob: 0111d3028c4ba41b91503aadd593be2ff4213f04 [file] [log] [blame]
Jiri Slabybd28ce02008-06-25 23:47:04 +02001/*
Jiri Kosina078328d2013-06-13 12:03:49 +02002 * HID driver for Sony / PS2 / PS3 BD devices.
Jiri Slabybd28ce02008-06-25 23:47:04 +02003 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
Jiri Slabybd28ce02008-06-25 23:47:04 +02007 * Copyright (c) 2008 Jiri Slaby
Jiri Kosina078328d2013-06-13 12:03:49 +02008 * Copyright (c) 2012 David Dillow <dave@thedillows.org>
9 * Copyright (c) 2006-2013 Jiri Kosina
Colin Leitnerf04d5142013-05-27 23:41:05 +020010 * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
Jiri Slabybd28ce02008-06-25 23:47:04 +020011 */
12
13/*
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
17 * any later version.
18 */
19
Jiri Kosina078328d2013-06-13 12:03:49 +020020/* NOTE: in order for the Sony PS3 BD Remote Control to be found by
21 * a Bluetooth host, the key combination Start+Enter has to be kept pressed
22 * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
23 *
24 * There will be no PIN request from the device.
25 */
26
Jiri Slabybd28ce02008-06-25 23:47:04 +020027#include <linux/device.h>
28#include <linux/hid.h>
29#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020031#include <linux/usb.h>
Jiri Kosina40e32ee2013-05-28 11:22:09 +020032#include <linux/leds.h>
Frank Praznikd902f472014-01-27 10:17:36 -050033#include <linux/power_supply.h>
34#include <linux/spinlock.h>
Frank Praznike5606232014-01-27 10:17:37 -050035#include <linux/input/mt.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020036
37#include "hid-ids.h"
38
Frank Praznik6c79c182014-01-16 21:43:03 -050039#define VAIO_RDESC_CONSTANT BIT(0)
40#define SIXAXIS_CONTROLLER_USB BIT(1)
41#define SIXAXIS_CONTROLLER_BT BIT(2)
42#define BUZZ_CONTROLLER BIT(3)
43#define PS3REMOTE BIT(4)
Frank Praznik8ab16762014-01-16 21:42:31 -050044#define DUALSHOCK4_CONTROLLER_USB BIT(5)
45#define DUALSHOCK4_CONTROLLER_BT BIT(6)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020046
Frank Praznik68330d82014-02-05 20:03:49 -050047#define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
48 DUALSHOCK4_CONTROLLER_BT)
49#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER_USB | BUZZ_CONTROLLER |\
50 DUALSHOCK4_CONTROLLER)
51#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT |\
52 DUALSHOCK4_CONTROLLER)
Frank Praznik60781cf2014-01-11 15:13:15 -050053
54#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010055
Simon Wood61ab44b2011-06-10 12:00:26 +020056static const u8 sixaxis_rdesc_fixup[] = {
57 0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
58 0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
59 0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
60};
61
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -020062static const u8 sixaxis_rdesc_fixup2[] = {
63 0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
64 0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
65 0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
66 0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
67 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
68 0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
69 0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
70 0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
71 0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
72 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
73 0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
74 0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
75 0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
76 0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
77 0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
78 0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
79 0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
80 0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
81 0xb1, 0x02, 0xc0, 0xc0,
82};
83
Frank Praznik58d70272014-01-20 12:27:01 -050084/* The default descriptor doesn't provide mapping for the accelerometers
85 * or orientation sensors. This fixed descriptor maps the accelerometers
86 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
87 * to usage values 0x43, 0x44 and 0x45.
88 */
Frank Prazniked19d8c2014-01-16 21:43:12 -050089static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -050090 0x05, 0x01, /* Usage Page (Desktop), */
91 0x09, 0x05, /* Usage (Gamepad), */
92 0xA1, 0x01, /* Collection (Application), */
93 0x85, 0x01, /* Report ID (1), */
94 0x09, 0x30, /* Usage (X), */
95 0x09, 0x31, /* Usage (Y), */
96 0x09, 0x32, /* Usage (Z), */
97 0x09, 0x35, /* Usage (Rz), */
98 0x15, 0x00, /* Logical Minimum (0), */
99 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
100 0x75, 0x08, /* Report Size (8), */
101 0x95, 0x04, /* Report Count (4), */
102 0x81, 0x02, /* Input (Variable), */
103 0x09, 0x39, /* Usage (Hat Switch), */
104 0x15, 0x00, /* Logical Minimum (0), */
105 0x25, 0x07, /* Logical Maximum (7), */
106 0x35, 0x00, /* Physical Minimum (0), */
107 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
108 0x65, 0x14, /* Unit (Degrees), */
109 0x75, 0x04, /* Report Size (4), */
110 0x95, 0x01, /* Report Count (1), */
111 0x81, 0x42, /* Input (Variable, Null State), */
112 0x65, 0x00, /* Unit, */
113 0x05, 0x09, /* Usage Page (Button), */
114 0x19, 0x01, /* Usage Minimum (01h), */
115 0x29, 0x0E, /* Usage Maximum (0Eh), */
116 0x15, 0x00, /* Logical Minimum (0), */
117 0x25, 0x01, /* Logical Maximum (1), */
118 0x75, 0x01, /* Report Size (1), */
119 0x95, 0x0E, /* Report Count (14), */
120 0x81, 0x02, /* Input (Variable), */
121 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
122 0x09, 0x20, /* Usage (20h), */
123 0x75, 0x06, /* Report Size (6), */
124 0x95, 0x01, /* Report Count (1), */
125 0x15, 0x00, /* Logical Minimum (0), */
126 0x25, 0x7F, /* Logical Maximum (127), */
127 0x81, 0x02, /* Input (Variable), */
128 0x05, 0x01, /* Usage Page (Desktop), */
129 0x09, 0x33, /* Usage (Rx), */
130 0x09, 0x34, /* Usage (Ry), */
131 0x15, 0x00, /* Logical Minimum (0), */
132 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
133 0x75, 0x08, /* Report Size (8), */
134 0x95, 0x02, /* Report Count (2), */
135 0x81, 0x02, /* Input (Variable), */
136 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
137 0x09, 0x21, /* Usage (21h), */
138 0x95, 0x03, /* Report Count (3), */
139 0x81, 0x02, /* Input (Variable), */
140 0x05, 0x01, /* Usage Page (Desktop), */
141 0x19, 0x40, /* Usage Minimum (40h), */
142 0x29, 0x42, /* Usage Maximum (42h), */
143 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
144 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
145 0x75, 0x10, /* Report Size (16), */
146 0x95, 0x03, /* Report Count (3), */
147 0x81, 0x02, /* Input (Variable), */
148 0x19, 0x43, /* Usage Minimum (43h), */
149 0x29, 0x45, /* Usage Maximum (45h), */
150 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
151 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
152 0x95, 0x03, /* Report Count (3), */
153 0x81, 0x02, /* Input (Variable), */
154 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
155 0x09, 0x21, /* Usage (21h), */
156 0x15, 0x00, /* Logical Minimum (0), */
157 0x25, 0xFF, /* Logical Maximum (255), */
158 0x75, 0x08, /* Report Size (8), */
159 0x95, 0x27, /* Report Count (39), */
160 0x81, 0x02, /* Input (Variable), */
161 0x85, 0x05, /* Report ID (5), */
162 0x09, 0x22, /* Usage (22h), */
163 0x95, 0x1F, /* Report Count (31), */
164 0x91, 0x02, /* Output (Variable), */
165 0x85, 0x04, /* Report ID (4), */
166 0x09, 0x23, /* Usage (23h), */
167 0x95, 0x24, /* Report Count (36), */
168 0xB1, 0x02, /* Feature (Variable), */
169 0x85, 0x02, /* Report ID (2), */
170 0x09, 0x24, /* Usage (24h), */
171 0x95, 0x24, /* Report Count (36), */
172 0xB1, 0x02, /* Feature (Variable), */
173 0x85, 0x08, /* Report ID (8), */
174 0x09, 0x25, /* Usage (25h), */
175 0x95, 0x03, /* Report Count (3), */
176 0xB1, 0x02, /* Feature (Variable), */
177 0x85, 0x10, /* Report ID (16), */
178 0x09, 0x26, /* Usage (26h), */
179 0x95, 0x04, /* Report Count (4), */
180 0xB1, 0x02, /* Feature (Variable), */
181 0x85, 0x11, /* Report ID (17), */
182 0x09, 0x27, /* Usage (27h), */
183 0x95, 0x02, /* Report Count (2), */
184 0xB1, 0x02, /* Feature (Variable), */
185 0x85, 0x12, /* Report ID (18), */
186 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
187 0x09, 0x21, /* Usage (21h), */
188 0x95, 0x0F, /* Report Count (15), */
189 0xB1, 0x02, /* Feature (Variable), */
190 0x85, 0x13, /* Report ID (19), */
191 0x09, 0x22, /* Usage (22h), */
192 0x95, 0x16, /* Report Count (22), */
193 0xB1, 0x02, /* Feature (Variable), */
194 0x85, 0x14, /* Report ID (20), */
195 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
196 0x09, 0x20, /* Usage (20h), */
197 0x95, 0x10, /* Report Count (16), */
198 0xB1, 0x02, /* Feature (Variable), */
199 0x85, 0x15, /* Report ID (21), */
200 0x09, 0x21, /* Usage (21h), */
201 0x95, 0x2C, /* Report Count (44), */
202 0xB1, 0x02, /* Feature (Variable), */
203 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
204 0x85, 0x80, /* Report ID (128), */
205 0x09, 0x20, /* Usage (20h), */
206 0x95, 0x06, /* Report Count (6), */
207 0xB1, 0x02, /* Feature (Variable), */
208 0x85, 0x81, /* Report ID (129), */
209 0x09, 0x21, /* Usage (21h), */
210 0x95, 0x06, /* Report Count (6), */
211 0xB1, 0x02, /* Feature (Variable), */
212 0x85, 0x82, /* Report ID (130), */
213 0x09, 0x22, /* Usage (22h), */
214 0x95, 0x05, /* Report Count (5), */
215 0xB1, 0x02, /* Feature (Variable), */
216 0x85, 0x83, /* Report ID (131), */
217 0x09, 0x23, /* Usage (23h), */
218 0x95, 0x01, /* Report Count (1), */
219 0xB1, 0x02, /* Feature (Variable), */
220 0x85, 0x84, /* Report ID (132), */
221 0x09, 0x24, /* Usage (24h), */
222 0x95, 0x04, /* Report Count (4), */
223 0xB1, 0x02, /* Feature (Variable), */
224 0x85, 0x85, /* Report ID (133), */
225 0x09, 0x25, /* Usage (25h), */
226 0x95, 0x06, /* Report Count (6), */
227 0xB1, 0x02, /* Feature (Variable), */
228 0x85, 0x86, /* Report ID (134), */
229 0x09, 0x26, /* Usage (26h), */
230 0x95, 0x06, /* Report Count (6), */
231 0xB1, 0x02, /* Feature (Variable), */
232 0x85, 0x87, /* Report ID (135), */
233 0x09, 0x27, /* Usage (27h), */
234 0x95, 0x23, /* Report Count (35), */
235 0xB1, 0x02, /* Feature (Variable), */
236 0x85, 0x88, /* Report ID (136), */
237 0x09, 0x28, /* Usage (28h), */
238 0x95, 0x22, /* Report Count (34), */
239 0xB1, 0x02, /* Feature (Variable), */
240 0x85, 0x89, /* Report ID (137), */
241 0x09, 0x29, /* Usage (29h), */
242 0x95, 0x02, /* Report Count (2), */
243 0xB1, 0x02, /* Feature (Variable), */
244 0x85, 0x90, /* Report ID (144), */
245 0x09, 0x30, /* Usage (30h), */
246 0x95, 0x05, /* Report Count (5), */
247 0xB1, 0x02, /* Feature (Variable), */
248 0x85, 0x91, /* Report ID (145), */
249 0x09, 0x31, /* Usage (31h), */
250 0x95, 0x03, /* Report Count (3), */
251 0xB1, 0x02, /* Feature (Variable), */
252 0x85, 0x92, /* Report ID (146), */
253 0x09, 0x32, /* Usage (32h), */
254 0x95, 0x03, /* Report Count (3), */
255 0xB1, 0x02, /* Feature (Variable), */
256 0x85, 0x93, /* Report ID (147), */
257 0x09, 0x33, /* Usage (33h), */
258 0x95, 0x0C, /* Report Count (12), */
259 0xB1, 0x02, /* Feature (Variable), */
260 0x85, 0xA0, /* Report ID (160), */
261 0x09, 0x40, /* Usage (40h), */
262 0x95, 0x06, /* Report Count (6), */
263 0xB1, 0x02, /* Feature (Variable), */
264 0x85, 0xA1, /* Report ID (161), */
265 0x09, 0x41, /* Usage (41h), */
266 0x95, 0x01, /* Report Count (1), */
267 0xB1, 0x02, /* Feature (Variable), */
268 0x85, 0xA2, /* Report ID (162), */
269 0x09, 0x42, /* Usage (42h), */
270 0x95, 0x01, /* Report Count (1), */
271 0xB1, 0x02, /* Feature (Variable), */
272 0x85, 0xA3, /* Report ID (163), */
273 0x09, 0x43, /* Usage (43h), */
274 0x95, 0x30, /* Report Count (48), */
275 0xB1, 0x02, /* Feature (Variable), */
276 0x85, 0xA4, /* Report ID (164), */
277 0x09, 0x44, /* Usage (44h), */
278 0x95, 0x0D, /* Report Count (13), */
279 0xB1, 0x02, /* Feature (Variable), */
280 0x85, 0xA5, /* Report ID (165), */
281 0x09, 0x45, /* Usage (45h), */
282 0x95, 0x15, /* Report Count (21), */
283 0xB1, 0x02, /* Feature (Variable), */
284 0x85, 0xA6, /* Report ID (166), */
285 0x09, 0x46, /* Usage (46h), */
286 0x95, 0x15, /* Report Count (21), */
287 0xB1, 0x02, /* Feature (Variable), */
288 0x85, 0xF0, /* Report ID (240), */
289 0x09, 0x47, /* Usage (47h), */
290 0x95, 0x3F, /* Report Count (63), */
291 0xB1, 0x02, /* Feature (Variable), */
292 0x85, 0xF1, /* Report ID (241), */
293 0x09, 0x48, /* Usage (48h), */
294 0x95, 0x3F, /* Report Count (63), */
295 0xB1, 0x02, /* Feature (Variable), */
296 0x85, 0xF2, /* Report ID (242), */
297 0x09, 0x49, /* Usage (49h), */
298 0x95, 0x0F, /* Report Count (15), */
299 0xB1, 0x02, /* Feature (Variable), */
300 0x85, 0xA7, /* Report ID (167), */
301 0x09, 0x4A, /* Usage (4Ah), */
302 0x95, 0x01, /* Report Count (1), */
303 0xB1, 0x02, /* Feature (Variable), */
304 0x85, 0xA8, /* Report ID (168), */
305 0x09, 0x4B, /* Usage (4Bh), */
306 0x95, 0x01, /* Report Count (1), */
307 0xB1, 0x02, /* Feature (Variable), */
308 0x85, 0xA9, /* Report ID (169), */
309 0x09, 0x4C, /* Usage (4Ch), */
310 0x95, 0x08, /* Report Count (8), */
311 0xB1, 0x02, /* Feature (Variable), */
312 0x85, 0xAA, /* Report ID (170), */
313 0x09, 0x4E, /* Usage (4Eh), */
314 0x95, 0x01, /* Report Count (1), */
315 0xB1, 0x02, /* Feature (Variable), */
316 0x85, 0xAB, /* Report ID (171), */
317 0x09, 0x4F, /* Usage (4Fh), */
318 0x95, 0x39, /* Report Count (57), */
319 0xB1, 0x02, /* Feature (Variable), */
320 0x85, 0xAC, /* Report ID (172), */
321 0x09, 0x50, /* Usage (50h), */
322 0x95, 0x39, /* Report Count (57), */
323 0xB1, 0x02, /* Feature (Variable), */
324 0x85, 0xAD, /* Report ID (173), */
325 0x09, 0x51, /* Usage (51h), */
326 0x95, 0x0B, /* Report Count (11), */
327 0xB1, 0x02, /* Feature (Variable), */
328 0x85, 0xAE, /* Report ID (174), */
329 0x09, 0x52, /* Usage (52h), */
330 0x95, 0x01, /* Report Count (1), */
331 0xB1, 0x02, /* Feature (Variable), */
332 0x85, 0xAF, /* Report ID (175), */
333 0x09, 0x53, /* Usage (53h), */
334 0x95, 0x02, /* Report Count (2), */
335 0xB1, 0x02, /* Feature (Variable), */
336 0x85, 0xB0, /* Report ID (176), */
337 0x09, 0x54, /* Usage (54h), */
338 0x95, 0x3F, /* Report Count (63), */
339 0xB1, 0x02, /* Feature (Variable), */
340 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500341};
342
Frank Praznikd8296742014-02-05 20:03:45 -0500343/* The default behavior of the Dualshock 4 is to send reports using report
344 * type 1 when running over Bluetooth. However, as soon as it receives a
345 * report of type 17 to set the LEDs or rumble it starts returning it's state
346 * in report 17 instead of 1. Since report 17 is undefined in the default HID
347 * descriptor the button and axis definitions must be moved to report 17 or
348 * the HID layer won't process the received input once a report is sent.
349 */
350static u8 dualshock4_bt_rdesc[] = {
351 0x05, 0x01, /* Usage Page (Desktop), */
352 0x09, 0x05, /* Usage (Gamepad), */
353 0xA1, 0x01, /* Collection (Application), */
354 0x85, 0x01, /* Report ID (1), */
355 0x75, 0x08, /* Report Size (8), */
356 0x95, 0x0A, /* Report Count (9), */
357 0x81, 0x02, /* Input (Variable), */
358 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
359 0x85, 0x02, /* Report ID (2), */
360 0x09, 0x24, /* Usage (24h), */
361 0x95, 0x24, /* Report Count (36), */
362 0xB1, 0x02, /* Feature (Variable), */
363 0x85, 0xA3, /* Report ID (163), */
364 0x09, 0x25, /* Usage (25h), */
365 0x95, 0x30, /* Report Count (48), */
366 0xB1, 0x02, /* Feature (Variable), */
367 0x85, 0x05, /* Report ID (5), */
368 0x09, 0x26, /* Usage (26h), */
369 0x95, 0x28, /* Report Count (40), */
370 0xB1, 0x02, /* Feature (Variable), */
371 0x85, 0x06, /* Report ID (6), */
372 0x09, 0x27, /* Usage (27h), */
373 0x95, 0x34, /* Report Count (52), */
374 0xB1, 0x02, /* Feature (Variable), */
375 0x85, 0x07, /* Report ID (7), */
376 0x09, 0x28, /* Usage (28h), */
377 0x95, 0x30, /* Report Count (48), */
378 0xB1, 0x02, /* Feature (Variable), */
379 0x85, 0x08, /* Report ID (8), */
380 0x09, 0x29, /* Usage (29h), */
381 0x95, 0x2F, /* Report Count (47), */
382 0xB1, 0x02, /* Feature (Variable), */
383 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
384 0x85, 0x03, /* Report ID (3), */
385 0x09, 0x21, /* Usage (21h), */
386 0x95, 0x26, /* Report Count (38), */
387 0xB1, 0x02, /* Feature (Variable), */
388 0x85, 0x04, /* Report ID (4), */
389 0x09, 0x22, /* Usage (22h), */
390 0x95, 0x2E, /* Report Count (46), */
391 0xB1, 0x02, /* Feature (Variable), */
392 0x85, 0xF0, /* Report ID (240), */
393 0x09, 0x47, /* Usage (47h), */
394 0x95, 0x3F, /* Report Count (63), */
395 0xB1, 0x02, /* Feature (Variable), */
396 0x85, 0xF1, /* Report ID (241), */
397 0x09, 0x48, /* Usage (48h), */
398 0x95, 0x3F, /* Report Count (63), */
399 0xB1, 0x02, /* Feature (Variable), */
400 0x85, 0xF2, /* Report ID (242), */
401 0x09, 0x49, /* Usage (49h), */
402 0x95, 0x0F, /* Report Count (15), */
403 0xB1, 0x02, /* Feature (Variable), */
404 0x85, 0x11, /* Report ID (17), */
405 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
406 0x09, 0x20, /* Usage (20h), */
407 0x95, 0x02, /* Report Count (2), */
408 0x81, 0x02, /* Input (Variable), */
409 0x05, 0x01, /* Usage Page (Desktop), */
410 0x09, 0x30, /* Usage (X), */
411 0x09, 0x31, /* Usage (Y), */
412 0x09, 0x32, /* Usage (Z), */
413 0x09, 0x35, /* Usage (Rz), */
414 0x15, 0x00, /* Logical Minimum (0), */
415 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
416 0x75, 0x08, /* Report Size (8), */
417 0x95, 0x04, /* Report Count (4), */
418 0x81, 0x02, /* Input (Variable), */
419 0x09, 0x39, /* Usage (Hat Switch), */
420 0x15, 0x00, /* Logical Minimum (0), */
421 0x25, 0x07, /* Logical Maximum (7), */
422 0x75, 0x04, /* Report Size (4), */
423 0x95, 0x01, /* Report Count (1), */
424 0x81, 0x42, /* Input (Variable, Null State), */
425 0x05, 0x09, /* Usage Page (Button), */
426 0x19, 0x01, /* Usage Minimum (01h), */
427 0x29, 0x0E, /* Usage Maximum (0Eh), */
428 0x15, 0x00, /* Logical Minimum (0), */
429 0x25, 0x01, /* Logical Maximum (1), */
430 0x75, 0x01, /* Report Size (1), */
431 0x95, 0x0E, /* Report Count (14), */
432 0x81, 0x02, /* Input (Variable), */
433 0x75, 0x06, /* Report Size (6), */
434 0x95, 0x01, /* Report Count (1), */
435 0x81, 0x01, /* Input (Constant), */
436 0x05, 0x01, /* Usage Page (Desktop), */
437 0x09, 0x33, /* Usage (Rx), */
438 0x09, 0x34, /* Usage (Ry), */
439 0x15, 0x00, /* Logical Minimum (0), */
440 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
441 0x75, 0x08, /* Report Size (8), */
442 0x95, 0x02, /* Report Count (2), */
443 0x81, 0x02, /* Input (Variable), */
444 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
445 0x09, 0x20, /* Usage (20h), */
446 0x95, 0x03, /* Report Count (3), */
447 0x81, 0x02, /* Input (Variable), */
448 0x05, 0x01, /* Usage Page (Desktop), */
449 0x19, 0x40, /* Usage Minimum (40h), */
450 0x29, 0x42, /* Usage Maximum (42h), */
451 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
452 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
453 0x75, 0x10, /* Report Size (16), */
454 0x95, 0x03, /* Report Count (3), */
455 0x81, 0x02, /* Input (Variable), */
456 0x19, 0x43, /* Usage Minimum (43h), */
457 0x29, 0x45, /* Usage Maximum (45h), */
458 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
459 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
460 0x95, 0x03, /* Report Count (3), */
461 0x81, 0x02, /* Input (Variable), */
462 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
463 0x09, 0x20, /* Usage (20h), */
464 0x15, 0x00, /* Logical Minimum (0), */
465 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
466 0x75, 0x08, /* Report Size (8), */
467 0x95, 0x31, /* Report Count (51), */
468 0x81, 0x02, /* Input (Variable), */
469 0x09, 0x21, /* Usage (21h), */
470 0x75, 0x08, /* Report Size (8), */
471 0x95, 0x4D, /* Report Count (77), */
472 0x91, 0x02, /* Output (Variable), */
473 0x85, 0x12, /* Report ID (18), */
474 0x09, 0x22, /* Usage (22h), */
475 0x95, 0x8D, /* Report Count (141), */
476 0x81, 0x02, /* Input (Variable), */
477 0x09, 0x23, /* Usage (23h), */
478 0x91, 0x02, /* Output (Variable), */
479 0x85, 0x13, /* Report ID (19), */
480 0x09, 0x24, /* Usage (24h), */
481 0x95, 0xCD, /* Report Count (205), */
482 0x81, 0x02, /* Input (Variable), */
483 0x09, 0x25, /* Usage (25h), */
484 0x91, 0x02, /* Output (Variable), */
485 0x85, 0x14, /* Report ID (20), */
486 0x09, 0x26, /* Usage (26h), */
487 0x96, 0x0D, 0x01, /* Report Count (269), */
488 0x81, 0x02, /* Input (Variable), */
489 0x09, 0x27, /* Usage (27h), */
490 0x91, 0x02, /* Output (Variable), */
491 0x85, 0x15, /* Report ID (21), */
492 0x09, 0x28, /* Usage (28h), */
493 0x96, 0x4D, 0x01, /* Report Count (333), */
494 0x81, 0x02, /* Input (Variable), */
495 0x09, 0x29, /* Usage (29h), */
496 0x91, 0x02, /* Output (Variable), */
497 0x85, 0x16, /* Report ID (22), */
498 0x09, 0x2A, /* Usage (2Ah), */
499 0x96, 0x8D, 0x01, /* Report Count (397), */
500 0x81, 0x02, /* Input (Variable), */
501 0x09, 0x2B, /* Usage (2Bh), */
502 0x91, 0x02, /* Output (Variable), */
503 0x85, 0x17, /* Report ID (23), */
504 0x09, 0x2C, /* Usage (2Ch), */
505 0x96, 0xCD, 0x01, /* Report Count (461), */
506 0x81, 0x02, /* Input (Variable), */
507 0x09, 0x2D, /* Usage (2Dh), */
508 0x91, 0x02, /* Output (Variable), */
509 0x85, 0x18, /* Report ID (24), */
510 0x09, 0x2E, /* Usage (2Eh), */
511 0x96, 0x0D, 0x02, /* Report Count (525), */
512 0x81, 0x02, /* Input (Variable), */
513 0x09, 0x2F, /* Usage (2Fh), */
514 0x91, 0x02, /* Output (Variable), */
515 0x85, 0x19, /* Report ID (25), */
516 0x09, 0x30, /* Usage (30h), */
517 0x96, 0x22, 0x02, /* Report Count (546), */
518 0x81, 0x02, /* Input (Variable), */
519 0x09, 0x31, /* Usage (31h), */
520 0x91, 0x02, /* Output (Variable), */
521 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
522 0x85, 0x82, /* Report ID (130), */
523 0x09, 0x22, /* Usage (22h), */
524 0x95, 0x3F, /* Report Count (63), */
525 0xB1, 0x02, /* Feature (Variable), */
526 0x85, 0x83, /* Report ID (131), */
527 0x09, 0x23, /* Usage (23h), */
528 0xB1, 0x02, /* Feature (Variable), */
529 0x85, 0x84, /* Report ID (132), */
530 0x09, 0x24, /* Usage (24h), */
531 0xB1, 0x02, /* Feature (Variable), */
532 0x85, 0x90, /* Report ID (144), */
533 0x09, 0x30, /* Usage (30h), */
534 0xB1, 0x02, /* Feature (Variable), */
535 0x85, 0x91, /* Report ID (145), */
536 0x09, 0x31, /* Usage (31h), */
537 0xB1, 0x02, /* Feature (Variable), */
538 0x85, 0x92, /* Report ID (146), */
539 0x09, 0x32, /* Usage (32h), */
540 0xB1, 0x02, /* Feature (Variable), */
541 0x85, 0x93, /* Report ID (147), */
542 0x09, 0x33, /* Usage (33h), */
543 0xB1, 0x02, /* Feature (Variable), */
544 0x85, 0xA0, /* Report ID (160), */
545 0x09, 0x40, /* Usage (40h), */
546 0xB1, 0x02, /* Feature (Variable), */
547 0x85, 0xA4, /* Report ID (164), */
548 0x09, 0x44, /* Usage (44h), */
549 0xB1, 0x02, /* Feature (Variable), */
550 0xC0 /* End Collection */
551};
552
Jiri Kosina078328d2013-06-13 12:03:49 +0200553static __u8 ps3remote_rdesc[] = {
554 0x05, 0x01, /* GUsagePage Generic Desktop */
555 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
556 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
557
558 /* Use collection 1 for joypad buttons */
559 0xA1, 0x02, /* MCollection Logical (interrelated data) */
560
561 /* Ignore the 1st byte, maybe it is used for a controller
562 * number but it's not needed for correct operation */
563 0x75, 0x08, /* GReportSize 0x08 [8] */
564 0x95, 0x01, /* GReportCount 0x01 [1] */
565 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
566
567 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
568 * buttons multiple keypresses are allowed */
569 0x05, 0x09, /* GUsagePage Button */
570 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
571 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
572 0x14, /* GLogicalMinimum [0] */
573 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
574 0x75, 0x01, /* GReportSize 0x01 [1] */
575 0x95, 0x18, /* GReportCount 0x18 [24] */
576 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
577
578 0xC0, /* MEndCollection */
579
580 /* Use collection 2 for remote control buttons */
581 0xA1, 0x02, /* MCollection Logical (interrelated data) */
582
583 /* 5th byte is used for remote control buttons */
584 0x05, 0x09, /* GUsagePage Button */
585 0x18, /* LUsageMinimum [No button pressed] */
586 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
587 0x14, /* GLogicalMinimum [0] */
588 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
589 0x75, 0x08, /* GReportSize 0x08 [8] */
590 0x95, 0x01, /* GReportCount 0x01 [1] */
591 0x80, /* MInput */
592
593 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
594 * 0xff and 11th is for press indication */
595 0x75, 0x08, /* GReportSize 0x08 [8] */
596 0x95, 0x06, /* GReportCount 0x06 [6] */
597 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
598
599 /* 12th byte is for battery strength */
600 0x05, 0x06, /* GUsagePage Generic Device Controls */
601 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
602 0x14, /* GLogicalMinimum [0] */
603 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
604 0x75, 0x08, /* GReportSize 0x08 [8] */
605 0x95, 0x01, /* GReportCount 0x01 [1] */
606 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
607
608 0xC0, /* MEndCollection */
609
610 0xC0 /* MEndCollection [Game Pad] */
611};
612
613static const unsigned int ps3remote_keymap_joypad_buttons[] = {
614 [0x01] = KEY_SELECT,
615 [0x02] = BTN_THUMBL, /* L3 */
616 [0x03] = BTN_THUMBR, /* R3 */
617 [0x04] = BTN_START,
618 [0x05] = KEY_UP,
619 [0x06] = KEY_RIGHT,
620 [0x07] = KEY_DOWN,
621 [0x08] = KEY_LEFT,
622 [0x09] = BTN_TL2, /* L2 */
623 [0x0a] = BTN_TR2, /* R2 */
624 [0x0b] = BTN_TL, /* L1 */
625 [0x0c] = BTN_TR, /* R1 */
626 [0x0d] = KEY_OPTION, /* options/triangle */
627 [0x0e] = KEY_BACK, /* back/circle */
628 [0x0f] = BTN_0, /* cross */
629 [0x10] = KEY_SCREEN, /* view/square */
630 [0x11] = KEY_HOMEPAGE, /* PS button */
631 [0x14] = KEY_ENTER,
632};
633static const unsigned int ps3remote_keymap_remote_buttons[] = {
634 [0x00] = KEY_1,
635 [0x01] = KEY_2,
636 [0x02] = KEY_3,
637 [0x03] = KEY_4,
638 [0x04] = KEY_5,
639 [0x05] = KEY_6,
640 [0x06] = KEY_7,
641 [0x07] = KEY_8,
642 [0x08] = KEY_9,
643 [0x09] = KEY_0,
644 [0x0e] = KEY_ESC, /* return */
645 [0x0f] = KEY_CLEAR,
646 [0x16] = KEY_EJECTCD,
647 [0x1a] = KEY_MENU, /* top menu */
648 [0x28] = KEY_TIME,
649 [0x30] = KEY_PREVIOUS,
650 [0x31] = KEY_NEXT,
651 [0x32] = KEY_PLAY,
652 [0x33] = KEY_REWIND, /* scan back */
653 [0x34] = KEY_FORWARD, /* scan forward */
654 [0x38] = KEY_STOP,
655 [0x39] = KEY_PAUSE,
656 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
657 [0x60] = KEY_FRAMEBACK, /* slow/step back */
658 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
659 [0x63] = KEY_SUBTITLE,
660 [0x64] = KEY_AUDIO,
661 [0x65] = KEY_ANGLE,
662 [0x70] = KEY_INFO, /* display */
663 [0x80] = KEY_BLUE,
664 [0x81] = KEY_RED,
665 [0x82] = KEY_GREEN,
666 [0x83] = KEY_YELLOW,
667};
668
Colin Leitnerf04d5142013-05-27 23:41:05 +0200669static const unsigned int buzz_keymap[] = {
670 /* The controller has 4 remote buzzers, each with one LED and 5
671 * 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
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200715struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500716 spinlock_t lock;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100717 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500718 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200719 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100720 struct work_struct state_worker;
Frank Praznikd902f472014-01-27 10:17:36 -0500721 struct power_supply battery;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200722
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100723#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100724 __u8 left;
725 __u8 right;
726#endif
727
Frank Praznikd902f472014-01-27 10:17:36 -0500728 __u8 cable_state;
729 __u8 battery_charging;
730 __u8 battery_capacity;
Frank Praznik60781cf2014-01-11 15:13:15 -0500731 __u8 led_state[MAX_LEDS];
732 __u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200733};
734
Jiri Kosina078328d2013-06-13 12:03:49 +0200735static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
736 unsigned int *rsize)
737{
738 *rsize = sizeof(ps3remote_rdesc);
739 return ps3remote_rdesc;
740}
741
742static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
743 struct hid_field *field, struct hid_usage *usage,
744 unsigned long **bit, int *max)
745{
746 unsigned int key = usage->hid & HID_USAGE;
747
748 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
749 return -1;
750
751 switch (usage->collection_index) {
752 case 1:
753 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
754 return -1;
755
756 key = ps3remote_keymap_joypad_buttons[key];
757 if (!key)
758 return -1;
759 break;
760 case 2:
761 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
762 return -1;
763
764 key = ps3remote_keymap_remote_buttons[key];
765 if (!key)
766 return -1;
767 break;
768 default:
769 return -1;
770 }
771
772 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
773 return 1;
774}
775
776
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200777/* Sony Vaio VGX has wrongly mouse pointer declared as constant */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400778static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
779 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200780{
781 struct sony_sc *sc = hid_get_drvdata(hdev);
782
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900783 /*
784 * Some Sony RF receivers wrongly declare the mouse pointer as a
785 * a constant non-data variable.
786 */
787 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
788 /* usage page: generic desktop controls */
789 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
790 /* usage: mouse */
791 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
792 /* input (usage page for x,y axes): constant, variable, relative */
793 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +0900794 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900795 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200796 rdesc[55] = 0x06;
797 }
Simon Wood61ab44b2011-06-10 12:00:26 +0200798
Frank Prazniked19d8c2014-01-16 21:43:12 -0500799 /*
800 * The default Dualshock 4 USB descriptor doesn't assign
801 * the gyroscope values to corresponding axes so we need a
802 * modified one.
803 */
804 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
805 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
806 rdesc = dualshock4_usb_rdesc;
807 *rsize = sizeof(dualshock4_usb_rdesc);
Frank Praznikd8296742014-02-05 20:03:45 -0500808 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
809 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
810 rdesc = dualshock4_bt_rdesc;
811 *rsize = sizeof(dualshock4_bt_rdesc);
Frank Prazniked19d8c2014-01-16 21:43:12 -0500812 }
813
Simon Wood61ab44b2011-06-10 12:00:26 +0200814 /* The HID descriptor exposed over BT has a trailing zero byte */
815 if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
816 ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
817 rdesc[83] == 0x75) {
818 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
819 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
820 sizeof(sixaxis_rdesc_fixup));
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200821 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
822 *rsize > sizeof(sixaxis_rdesc_fixup2)) {
823 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
824 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
825 *rsize = sizeof(sixaxis_rdesc_fixup2);
826 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
Simon Wood61ab44b2011-06-10 12:00:26 +0200827 }
Jiri Kosina078328d2013-06-13 12:03:49 +0200828
829 if (sc->quirks & PS3REMOTE)
830 return ps3remote_fixup(hdev, rdesc, rsize);
831
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400832 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200833}
834
Frank Praznikd902f472014-01-27 10:17:36 -0500835static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
836{
837 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
838 unsigned long flags;
839 __u8 cable_state, battery_capacity, battery_charging;
840
841 /* The sixaxis is charging if the battery value is 0xee
842 * and it is fully charged if the value is 0xef.
843 * It does not report the actual level while charging so it
844 * is set to 100% while charging is in progress.
845 */
846 if (rd[30] >= 0xee) {
847 battery_capacity = 100;
Frank Praznika43e94a2014-02-15 13:35:42 -0500848 battery_charging = !(rd[30] & 0x01);
Frank Praznikd902f472014-01-27 10:17:36 -0500849 } else {
850 battery_capacity = sixaxis_battery_capacity[rd[30]];
851 battery_charging = 0;
852 }
Frank Praznika43e94a2014-02-15 13:35:42 -0500853 cable_state = !((rd[31] >> 4) & 0x01);
Frank Praznikd902f472014-01-27 10:17:36 -0500854
855 spin_lock_irqsave(&sc->lock, flags);
856 sc->cable_state = cable_state;
857 sc->battery_capacity = battery_capacity;
858 sc->battery_charging = battery_charging;
859 spin_unlock_irqrestore(&sc->lock, flags);
860}
861
862static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
863{
Frank Praznike5606232014-01-27 10:17:37 -0500864 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
865 struct hid_input, list);
866 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -0500867 unsigned long flags;
Frank Praznik6c5f8602014-02-05 20:03:47 -0500868 int n, offset;
Frank Praznikd902f472014-01-27 10:17:36 -0500869 __u8 cable_state, battery_capacity, battery_charging;
870
Frank Praznik6c5f8602014-02-05 20:03:47 -0500871 /* Battery and touchpad data starts at byte 30 in the USB report and
872 * 32 in Bluetooth report.
873 */
874 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
875
Frank Praznikd902f472014-01-27 10:17:36 -0500876 /* The lower 4 bits of byte 30 contain the battery level
877 * and the 5th bit contains the USB cable state.
878 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500879 cable_state = (rd[offset] >> 4) & 0x01;
880 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -0500881
Frank Praznik6c5f8602014-02-05 20:03:47 -0500882 /* When a USB power source is connected the battery level ranges from
883 * 0 to 10, and when running on battery power it ranges from 0 to 9.
884 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -0500885 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500886 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -0500887 battery_charging = 0;
888 else
889 battery_charging = 1;
890
Frank Praznik6c5f8602014-02-05 20:03:47 -0500891 if (!cable_state)
892 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -0500893 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -0500894 battery_capacity = 10;
895
Frank Praznikd902f472014-01-27 10:17:36 -0500896 battery_capacity *= 10;
897
898 spin_lock_irqsave(&sc->lock, flags);
899 sc->cable_state = cable_state;
900 sc->battery_capacity = battery_capacity;
901 sc->battery_charging = battery_charging;
902 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -0500903
Frank Praznik6c5f8602014-02-05 20:03:47 -0500904 offset += 5;
905
906 /* The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
907 * and 37 on Bluetooth.
Frank Praznike5606232014-01-27 10:17:37 -0500908 * The first 7 bits of the first byte is a counter and bit 8 is a touch
909 * indicator that is 0 when pressed and 1 when not pressed.
910 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
911 * The data for the second touch is in the same format and immediatly
912 * follows the data for the first.
913 */
914 for (n = 0; n < 2; n++) {
915 __u16 x, y;
916
917 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
918 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
919
920 input_mt_slot(input_dev, n);
921 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
922 !(rd[offset] >> 7));
923 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
924 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
925
926 offset += 4;
927 }
Frank Praznikd902f472014-01-27 10:17:36 -0500928}
929
Simon Woodc9e4d872011-06-10 12:00:27 +0200930static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
931 __u8 *rd, int size)
932{
933 struct sony_sc *sc = hid_get_drvdata(hdev);
934
935 /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
936 * has to be BYTE_SWAPPED before passing up to joystick interface
937 */
938 if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
939 rd[0] == 0x01 && size == 49) {
940 swap(rd[41], rd[42]);
941 swap(rd[43], rd[44]);
942 swap(rd[45], rd[46]);
943 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -0500944
945 sixaxis_parse_report(sc, rd, size);
Frank Praznik68330d82014-02-05 20:03:49 -0500946 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
947 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
948 && rd[0] == 0x11 && size == 78)) {
Frank Praznikd902f472014-01-27 10:17:36 -0500949 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +0200950 }
951
952 return 0;
953}
954
Colin Leitnerf04d5142013-05-27 23:41:05 +0200955static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
956 struct hid_field *field, struct hid_usage *usage,
957 unsigned long **bit, int *max)
958{
959 struct sony_sc *sc = hid_get_drvdata(hdev);
960
961 if (sc->quirks & BUZZ_CONTROLLER) {
962 unsigned int key = usage->hid & HID_USAGE;
963
964 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
965 return -1;
966
967 switch (usage->collection_index) {
968 case 1:
969 if (key >= ARRAY_SIZE(buzz_keymap))
970 return -1;
971
972 key = buzz_keymap[key];
973 if (!key)
974 return -1;
975 break;
976 default:
977 return -1;
978 }
979
980 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
981 return 1;
982 }
983
Jiri Kosina078328d2013-06-13 12:03:49 +0200984 if (sc->quirks & PS3REMOTE)
985 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
986
Benjamin Tissoires6f498012013-07-24 16:53:07 +0200987 /* Let hid-core decide for the others */
988 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200989}
990
Antonio Ospite5710fab2011-02-20 18:26:45 +0100991/*
992 * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
993 * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
994 * so we need to override that forcing HID Output Reports on the Control EP.
995 *
996 * There is also another issue about HID Output Reports via USB, the Sixaxis
997 * does not want the report_id as part of the data packet, so we have to
998 * discard buf[0] when sending the actual control message, even for numbered
999 * reports, humpf!
1000 */
Antonio Ospite569b10a2010-10-19 16:13:10 +02001001static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
1002 size_t count, unsigned char report_type)
1003{
1004 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1005 struct usb_device *dev = interface_to_usbdev(intf);
1006 struct usb_host_interface *interface = intf->cur_altsetting;
1007 int report_id = buf[0];
1008 int ret;
1009
Antonio Ospite5710fab2011-02-20 18:26:45 +01001010 if (report_type == HID_OUTPUT_REPORT) {
1011 /* Don't send the Report ID */
1012 buf++;
1013 count--;
1014 }
1015
Antonio Ospite569b10a2010-10-19 16:13:10 +02001016 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1017 HID_REQ_SET_REPORT,
1018 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1019 ((report_type + 1) << 8) | report_id,
1020 interface->desc.bInterfaceNumber, buf, count,
1021 USB_CTRL_SET_TIMEOUT);
1022
Antonio Ospite5710fab2011-02-20 18:26:45 +01001023 /* Count also the Report ID, in case of an Output report. */
1024 if (ret > 0 && report_type == HID_OUTPUT_REPORT)
1025 ret++;
1026
Antonio Ospite569b10a2010-10-19 16:13:10 +02001027 return ret;
1028}
1029
Jiri Slabybd28ce02008-06-25 23:47:04 +02001030/*
1031 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1032 * to "operational". Without this, the ps3 controller will not report any
1033 * events.
1034 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001035static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001036{
Jiri Slabybd28ce02008-06-25 23:47:04 +02001037 int ret;
1038 char *buf = kmalloc(18, GFP_KERNEL);
1039
1040 if (!buf)
1041 return -ENOMEM;
1042
Benjamin Tissoirescafebc02014-02-05 16:33:22 -05001043 ret = hid_hw_raw_request(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT,
1044 HID_REQ_GET_REPORT);
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001045
Jiri Slabybd28ce02008-06-25 23:47:04 +02001046 if (ret < 0)
Joe Perches4291ee32010-12-09 19:29:03 -08001047 hid_err(hdev, "can't set operational mode\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +02001048
1049 kfree(buf);
1050
1051 return ret;
1052}
1053
Antonio Ospite816651a2010-05-03 22:15:55 +02001054static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001055{
Antonio Ospitefddb33f2010-05-03 17:19:03 +02001056 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
Benjamin Tissoires7e845d42014-02-05 16:33:23 -05001057 return hid_output_raw_report(hdev, buf, sizeof(buf),
1058 HID_FEATURE_REPORT);
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001059}
1060
Frank Praznik68330d82014-02-05 20:03:49 -05001061/* Requesting feature report 0x02 in Bluetooth mode changes the state of the
1062 * controller so that it sends full input reports of type 0x11.
1063 */
1064static int dualshock4_set_operational_bt(struct hid_device *hdev)
1065{
1066 __u8 buf[37] = { 0 };
1067
1068 return hid_hw_raw_request(hdev, 0x02, buf, sizeof(buf),
1069 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1070}
1071
Frank Praznik60781cf2014-01-11 15:13:15 -05001072static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001073{
1074 struct list_head *report_list =
1075 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1076 struct hid_report *report = list_entry(report_list->next,
1077 struct hid_report, list);
1078 __s32 *value = report->field[0]->value;
1079
1080 value[0] = 0x00;
Frank Praznik60781cf2014-01-11 15:13:15 -05001081 value[1] = leds[0] ? 0xff : 0x00;
1082 value[2] = leds[1] ? 0xff : 0x00;
1083 value[3] = leds[2] ? 0xff : 0x00;
1084 value[4] = leds[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001085 value[5] = 0x00;
1086 value[6] = 0x00;
1087 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1088}
1089
Frank Praznik60781cf2014-01-11 15:13:15 -05001090static void sony_set_leds(struct hid_device *hdev, const __u8 *leds, int count)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001091{
1092 struct sony_sc *drv_data = hid_get_drvdata(hdev);
Frank Praznik60781cf2014-01-11 15:13:15 -05001093 int n;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001094
Frank Praznik60781cf2014-01-11 15:13:15 -05001095 BUG_ON(count > MAX_LEDS);
1096
1097 if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001098 buzz_set_leds(hdev, leds);
Frank Praznik60781cf2014-01-11 15:13:15 -05001099 } else if ((drv_data->quirks & SIXAXIS_CONTROLLER_USB) ||
Frank Praznik68330d82014-02-05 20:03:49 -05001100 (drv_data->quirks & DUALSHOCK4_CONTROLLER)) {
Frank Praznik60781cf2014-01-11 15:13:15 -05001101 for (n = 0; n < count; n++)
1102 drv_data->led_state[n] = leds[n];
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001103 schedule_work(&drv_data->state_worker);
1104 }
1105}
1106
Sven Eckelmannc5382512013-11-19 20:26:30 +01001107static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001108 enum led_brightness value)
1109{
1110 struct device *dev = led->dev->parent;
1111 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1112 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001113
1114 int n;
1115
1116 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001117 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001118 hid_err(hdev, "No device data\n");
1119 return;
1120 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001121
Frank Praznik60781cf2014-01-11 15:13:15 -05001122 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann2251b852013-11-19 20:26:31 +01001123 if (led == drv_data->leds[n]) {
Frank Praznik60781cf2014-01-11 15:13:15 -05001124 if (value != drv_data->led_state[n]) {
1125 drv_data->led_state[n] = value;
1126 sony_set_leds(hdev, drv_data->led_state, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001127 }
1128 break;
1129 }
1130 }
1131}
1132
Sven Eckelmannc5382512013-11-19 20:26:30 +01001133static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001134{
1135 struct device *dev = led->dev->parent;
1136 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1137 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001138
1139 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001140
1141 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001142 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001143 hid_err(hdev, "No device data\n");
1144 return LED_OFF;
1145 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001146
Frank Praznik60781cf2014-01-11 15:13:15 -05001147 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001148 if (led == drv_data->leds[n])
1149 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001150 }
1151
Simon Wood7db75042014-02-05 12:34:18 -07001152 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001153}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001154
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001155static void sony_leds_remove(struct hid_device *hdev)
1156{
1157 struct sony_sc *drv_data;
1158 struct led_classdev *led;
1159 int n;
1160
1161 drv_data = hid_get_drvdata(hdev);
1162 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
1163
Frank Praznik60781cf2014-01-11 15:13:15 -05001164 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001165 led = drv_data->leds[n];
1166 drv_data->leds[n] = NULL;
1167 if (!led)
1168 continue;
1169 led_classdev_unregister(led);
1170 kfree(led);
1171 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001172
1173 drv_data->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001174}
1175
Sven Eckelmannc5382512013-11-19 20:26:30 +01001176static int sony_leds_init(struct hid_device *hdev)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001177{
1178 struct sony_sc *drv_data;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001179 int n, ret = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -05001180 int max_brightness;
Frank Praznik61ebca92014-01-20 12:27:02 -05001181 int use_colors;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001182 struct led_classdev *led;
1183 size_t name_sz;
1184 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001185 size_t name_len;
1186 const char *name_fmt;
Frank Praznik61ebca92014-01-20 12:27:02 -05001187 static const char * const color_str[] = { "red", "green", "blue" };
Frank Praznik60781cf2014-01-11 15:13:15 -05001188 static const __u8 initial_values[MAX_LEDS] = { 0x00, 0x00, 0x00, 0x00 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001189
1190 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001191 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001192
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001193 if (drv_data->quirks & BUZZ_CONTROLLER) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001194 drv_data->led_count = 4;
1195 max_brightness = 1;
1196 use_colors = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001197 name_len = strlen("::buzz#");
1198 name_fmt = "%s::buzz%d";
1199 /* Validate expected report characteristics. */
1200 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1201 return -ENODEV;
Frank Praznik68330d82014-02-05 20:03:49 -05001202 } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik60781cf2014-01-11 15:13:15 -05001203 drv_data->led_count = 3;
1204 max_brightness = 255;
Frank Praznik61ebca92014-01-20 12:27:02 -05001205 use_colors = 1;
1206 name_len = 0;
1207 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -05001208 } else {
1209 drv_data->led_count = 4;
1210 max_brightness = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001211 use_colors = 0;
1212 name_len = strlen("::sony#");
1213 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001214 }
1215
Colin Leitnerf04d5142013-05-27 23:41:05 +02001216 /* Clear LEDs as we have no way of reading their initial state. This is
1217 * only relevant if the driver is loaded after somebody actively set the
1218 * LEDs to on */
Frank Praznik60781cf2014-01-11 15:13:15 -05001219 sony_set_leds(hdev, initial_values, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001220
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001221 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001222
Frank Praznik60781cf2014-01-11 15:13:15 -05001223 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001224
1225 if (use_colors)
1226 name_sz = strlen(dev_name(&hdev->dev)) + strlen(color_str[n]) + 2;
1227
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001228 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1229 if (!led) {
1230 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001231 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001232 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001233 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001234
1235 name = (void *)(&led[1]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001236 if (use_colors)
1237 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), color_str[n]);
1238 else
1239 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001240 led->name = name;
1241 led->brightness = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -05001242 led->max_brightness = max_brightness;
Sven Eckelmannc5382512013-11-19 20:26:30 +01001243 led->brightness_get = sony_led_get_brightness;
1244 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001245
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001246 ret = led_classdev_register(&hdev->dev, led);
1247 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001248 hid_err(hdev, "Failed to register LED %d\n", n);
1249 kfree(led);
1250 goto error_leds;
1251 }
1252
Sven Eckelmann2251b852013-11-19 20:26:31 +01001253 drv_data->leds[n] = led;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001254 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001255
1256 return ret;
1257
Colin Leitnerf04d5142013-05-27 23:41:05 +02001258error_leds:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001259 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001260
Colin Leitnerf04d5142013-05-27 23:41:05 +02001261 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001262}
1263
Frank Praznikcad665a2014-01-11 15:13:54 -05001264static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001265{
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001266 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001267 unsigned char buf[] = {
1268 0x01,
1269 0x00, 0xff, 0x00, 0xff, 0x00,
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001270 0x00, 0x00, 0x00, 0x00, 0x00,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001271 0xff, 0x27, 0x10, 0x00, 0x32,
1272 0xff, 0x27, 0x10, 0x00, 0x32,
1273 0xff, 0x27, 0x10, 0x00, 0x32,
1274 0xff, 0x27, 0x10, 0x00, 0x32,
1275 0x00, 0x00, 0x00, 0x00, 0x00
1276 };
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001277
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001278#ifdef CONFIG_SONY_FF
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001279 buf[3] = sc->right ? 1 : 0;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001280 buf[5] = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001281#endif
1282
Frank Praznik60781cf2014-01-11 15:13:15 -05001283 buf[10] |= sc->led_state[0] << 1;
1284 buf[10] |= sc->led_state[1] << 2;
1285 buf[10] |= sc->led_state[2] << 3;
1286 buf[10] |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001287
Benjamin Tissoires7e845d42014-02-05 16:33:23 -05001288 hid_output_raw_report(sc->hdev, buf, sizeof(buf), HID_OUTPUT_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001289}
1290
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001291static void dualshock4_state_worker(struct work_struct *work)
1292{
1293 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001294 struct hid_device *hdev = sc->hdev;
Frank Praznik48220232014-02-05 20:03:44 -05001295 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001296
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001297 __u8 buf[78] = { 0 };
Frank Praznik48220232014-02-05 20:03:44 -05001298
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001299 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1300 buf[0] = 0x05;
1301 buf[1] = 0x03;
1302 offset = 4;
1303 } else {
1304 buf[0] = 0x11;
1305 buf[1] = 0xB0;
1306 buf[3] = 0x0F;
1307 offset = 6;
1308 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001309
1310#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001311 buf[offset++] = sc->right;
1312 buf[offset++] = sc->left;
1313#else
1314 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001315#endif
1316
Frank Praznik48220232014-02-05 20:03:44 -05001317 buf[offset++] = sc->led_state[0];
1318 buf[offset++] = sc->led_state[1];
1319 buf[offset++] = sc->led_state[2];
Frank Praznik60781cf2014-01-11 15:13:15 -05001320
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001321 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1322 hid_hw_output_report(hdev, buf, 32);
1323 else
1324 hid_hw_raw_request(hdev, 0x11, buf, 78,
1325 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001326}
1327
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001328#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001329static int sony_play_effect(struct input_dev *dev, void *data,
1330 struct ff_effect *effect)
1331{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001332 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001333 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001334
1335 if (effect->type != FF_RUMBLE)
1336 return 0;
1337
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001338 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001339 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001340
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001341 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001342 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001343}
1344
1345static int sony_init_ff(struct hid_device *hdev)
1346{
1347 struct hid_input *hidinput = list_entry(hdev->inputs.next,
1348 struct hid_input, list);
1349 struct input_dev *input_dev = hidinput->input;
1350
1351 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1352 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1353}
1354
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001355static void sony_destroy_ff(struct hid_device *hdev)
1356{
1357 struct sony_sc *sc = hid_get_drvdata(hdev);
1358
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001359 cancel_work_sync(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001360}
1361
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001362#else
1363static int sony_init_ff(struct hid_device *hdev)
1364{
1365 return 0;
1366}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001367
1368static void sony_destroy_ff(struct hid_device *hdev)
1369{
1370}
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001371#endif
1372
Frank Praznikd902f472014-01-27 10:17:36 -05001373static int sony_battery_get_property(struct power_supply *psy,
1374 enum power_supply_property psp,
1375 union power_supply_propval *val)
1376{
1377 struct sony_sc *sc = container_of(psy, struct sony_sc, battery);
1378 unsigned long flags;
1379 int ret = 0;
1380 u8 battery_charging, battery_capacity, cable_state;
1381
1382 spin_lock_irqsave(&sc->lock, flags);
1383 battery_charging = sc->battery_charging;
1384 battery_capacity = sc->battery_capacity;
1385 cable_state = sc->cable_state;
1386 spin_unlock_irqrestore(&sc->lock, flags);
1387
1388 switch (psp) {
1389 case POWER_SUPPLY_PROP_PRESENT:
1390 val->intval = 1;
1391 break;
1392 case POWER_SUPPLY_PROP_SCOPE:
1393 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1394 break;
1395 case POWER_SUPPLY_PROP_CAPACITY:
1396 val->intval = battery_capacity;
1397 break;
1398 case POWER_SUPPLY_PROP_STATUS:
1399 if (battery_charging)
1400 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1401 else
1402 if (battery_capacity == 100 && cable_state)
1403 val->intval = POWER_SUPPLY_STATUS_FULL;
1404 else
1405 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1406 break;
1407 default:
1408 ret = -EINVAL;
1409 break;
1410 }
1411 return ret;
1412}
1413
1414static int sony_battery_probe(struct sony_sc *sc)
1415{
1416 static atomic_t power_id_seq = ATOMIC_INIT(0);
1417 unsigned long power_id;
1418 struct hid_device *hdev = sc->hdev;
1419 int ret;
1420
Frank Praznikd9a293a2014-02-05 20:03:48 -05001421 /* Set the default battery level to 100% to avoid low battery warnings
1422 * if the battery is polled before the first device report is received.
1423 */
1424 sc->battery_capacity = 100;
1425
Frank Praznikd902f472014-01-27 10:17:36 -05001426 power_id = (unsigned long)atomic_inc_return(&power_id_seq);
1427
1428 sc->battery.properties = sony_battery_props;
1429 sc->battery.num_properties = ARRAY_SIZE(sony_battery_props);
1430 sc->battery.get_property = sony_battery_get_property;
1431 sc->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1432 sc->battery.use_for_apm = 0;
1433 sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%lu",
1434 power_id);
1435 if (!sc->battery.name)
1436 return -ENOMEM;
1437
1438 ret = power_supply_register(&hdev->dev, &sc->battery);
1439 if (ret) {
1440 hid_err(hdev, "Unable to register battery device\n");
1441 goto err_free;
1442 }
1443
1444 power_supply_powers(&sc->battery, &hdev->dev);
1445 return 0;
1446
1447err_free:
1448 kfree(sc->battery.name);
1449 sc->battery.name = NULL;
1450 return ret;
1451}
1452
1453static void sony_battery_remove(struct sony_sc *sc)
1454{
1455 if (!sc->battery.name)
1456 return;
1457
1458 power_supply_unregister(&sc->battery);
1459 kfree(sc->battery.name);
1460 sc->battery.name = NULL;
1461}
1462
Frank Praznike5606232014-01-27 10:17:37 -05001463static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
1464 int w, int h)
1465{
1466 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1467 struct hid_input, list);
1468 struct input_dev *input_dev = hidinput->input;
1469 int ret;
1470
1471 ret = input_mt_init_slots(input_dev, touch_count, 0);
1472 if (ret < 0) {
1473 hid_err(sc->hdev, "Unable to initialize multi-touch slots\n");
1474 return ret;
1475 }
1476
1477 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1478 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1479
1480 return 0;
1481}
1482
Jiri Slabybd28ce02008-06-25 23:47:04 +02001483static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1484{
1485 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001486 unsigned long quirks = id->driver_data;
1487 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001488 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001489
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001490 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001491 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08001492 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001493 return -ENOMEM;
1494 }
1495
1496 sc->quirks = quirks;
1497 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001498 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001499
Jiri Slabybd28ce02008-06-25 23:47:04 +02001500 ret = hid_parse(hdev);
1501 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001502 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001503 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001504 }
1505
Colin Leitnerf04d5142013-05-27 23:41:05 +02001506 if (sc->quirks & VAIO_RDESC_CONSTANT)
1507 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1508 else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
1509 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1510 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1511 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1512
1513 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001514 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001515 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001516 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001517 }
1518
Antonio Ospite569b10a2010-10-19 16:13:10 +02001519 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1520 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
Antonio Ospite816651a2010-05-03 22:15:55 +02001521 ret = sixaxis_set_operational_usb(hdev);
Frank Praznikcad665a2014-01-11 15:13:54 -05001522 INIT_WORK(&sc->state_worker, sixaxis_state_worker);
Antonio Ospite569b10a2010-10-19 16:13:10 +02001523 }
Antonio Ospite816651a2010-05-03 22:15:55 +02001524 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1525 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik68330d82014-02-05 20:03:49 -05001526 else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
1527 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
1528 ret = dualshock4_set_operational_bt(hdev);
1529 if (ret < 0) {
1530 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
1531 goto err_stop;
1532 }
1533 }
Frank Praznike5606232014-01-27 10:17:37 -05001534 /* The Dualshock 4 touchpad supports 2 touches and has a
1535 * resolution of 1920x940.
1536 */
1537 ret = sony_register_touchpad(sc, 2, 1920, 940);
1538 if (ret < 0)
1539 goto err_stop;
1540
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001541 INIT_WORK(&sc->state_worker, dualshock4_state_worker);
1542 } else {
1543 ret = 0;
1544 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001545
Jiri Kosina4dfdc462008-12-30 00:49:59 +01001546 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001547 goto err_stop;
1548
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001549 if (sc->quirks & SONY_LED_SUPPORT) {
1550 ret = sony_leds_init(hdev);
1551 if (ret < 0)
1552 goto err_stop;
1553 }
1554
Frank Praznikd902f472014-01-27 10:17:36 -05001555 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1556 ret = sony_battery_probe(sc);
1557 if (ret < 0)
1558 goto err_stop;
1559
1560 /* Open the device to receive reports with battery info */
1561 ret = hid_hw_open(hdev);
1562 if (ret < 0) {
1563 hid_err(hdev, "hw open failed\n");
1564 goto err_stop;
1565 }
1566 }
1567
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001568 ret = sony_init_ff(hdev);
1569 if (ret < 0)
Frank Praznikd902f472014-01-27 10:17:36 -05001570 goto err_close;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001571
Jiri Slabybd28ce02008-06-25 23:47:04 +02001572 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001573err_close:
1574 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001575err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001576 if (sc->quirks & SONY_LED_SUPPORT)
1577 sony_leds_remove(hdev);
Frank Praznikd902f472014-01-27 10:17:36 -05001578 if (sc->quirks & SONY_BATTERY_SUPPORT)
1579 sony_battery_remove(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001580 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001581 return ret;
1582}
1583
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001584static void sony_remove(struct hid_device *hdev)
1585{
Colin Leitnerf04d5142013-05-27 23:41:05 +02001586 struct sony_sc *sc = hid_get_drvdata(hdev);
1587
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001588 if (sc->quirks & SONY_LED_SUPPORT)
Sven Eckelmannc5382512013-11-19 20:26:30 +01001589 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001590
Frank Praznikd902f472014-01-27 10:17:36 -05001591 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1592 hid_hw_close(hdev);
1593 sony_battery_remove(sc);
1594 }
1595
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001596 sony_destroy_ff(hdev);
1597
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001598 hid_hw_stop(hdev);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001599}
1600
Jiri Slabybd28ce02008-06-25 23:47:04 +02001601static const struct hid_device_id sony_devices[] = {
Antonio Ospite816651a2010-05-03 22:15:55 +02001602 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1603 .driver_data = SIXAXIS_CONTROLLER_USB },
Jiri Kosina35dca5b2011-04-28 15:43:13 +02001604 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
1605 .driver_data = SIXAXIS_CONTROLLER_USB },
Antonio Ospite816651a2010-05-03 22:15:55 +02001606 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1607 .driver_data = SIXAXIS_CONTROLLER_BT },
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001608 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
1609 .driver_data = VAIO_RDESC_CONSTANT },
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +09001610 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
1611 .driver_data = VAIO_RDESC_CONSTANT },
Colin Leitnerf04d5142013-05-27 23:41:05 +02001612 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
1613 * Logitech joystick from the device descriptor. */
1614 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
1615 .driver_data = BUZZ_CONTROLLER },
1616 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
1617 .driver_data = BUZZ_CONTROLLER },
Jiri Kosina078328d2013-06-13 12:03:49 +02001618 /* PS3 BD Remote Control */
1619 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
1620 .driver_data = PS3REMOTE },
1621 /* Logitech Harmony Adapter for PS3 */
1622 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
1623 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001624 /* Sony Dualshock 4 controllers for PS4 */
1625 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001626 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001627 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001628 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02001629 { }
1630};
1631MODULE_DEVICE_TABLE(hid, sony_devices);
1632
1633static struct hid_driver sony_driver = {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001634 .name = "sony",
1635 .id_table = sony_devices,
1636 .input_mapping = sony_mapping,
1637 .probe = sony_probe,
1638 .remove = sony_remove,
1639 .report_fixup = sony_report_fixup,
1640 .raw_event = sony_raw_event
Jiri Slabybd28ce02008-06-25 23:47:04 +02001641};
H Hartley Sweetenf4254582012-12-17 15:28:26 -07001642module_hid_driver(sony_driver);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001643
Jiri Slabybd28ce02008-06-25 23:47:04 +02001644MODULE_LICENSE("GPL");