blob: 40dfa4b4252e9c3c3beb53962c881e95d202c288 [file] [log] [blame]
Jiri Slabybd28ce02008-06-25 23:47:04 +02001/*
Jiri Kosina078328d2013-06-13 12:03:49 +02002 * HID driver for Sony / PS2 / PS3 BD devices.
Jiri Slabybd28ce02008-06-25 23:47:04 +02003 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
Jiri Slabybd28ce02008-06-25 23:47:04 +02007 * Copyright (c) 2008 Jiri Slaby
Jiri Kosina078328d2013-06-13 12:03:49 +02008 * Copyright (c) 2012 David Dillow <dave@thedillows.org>
9 * Copyright (c) 2006-2013 Jiri Kosina
Colin Leitnerf04d5142013-05-27 23:41:05 +020010 * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
Jiri Slabybd28ce02008-06-25 23:47:04 +020011 */
12
13/*
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
17 * any later version.
18 */
19
Jiri Kosina078328d2013-06-13 12:03:49 +020020/* NOTE: in order for the Sony PS3 BD Remote Control to be found by
21 * a Bluetooth host, the key combination Start+Enter has to be kept pressed
22 * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
23 *
24 * There will be no PIN request from the device.
25 */
26
Jiri Slabybd28ce02008-06-25 23:47:04 +020027#include <linux/device.h>
28#include <linux/hid.h>
29#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020031#include <linux/usb.h>
Jiri Kosina40e32ee2013-05-28 11:22:09 +020032#include <linux/leds.h>
Frank Praznikd902f472014-01-27 10:17:36 -050033#include <linux/power_supply.h>
34#include <linux/spinlock.h>
Frank Praznike5606232014-01-27 10:17:37 -050035#include <linux/input/mt.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020036
37#include "hid-ids.h"
38
Frank Praznik6c79c182014-01-16 21:43:03 -050039#define VAIO_RDESC_CONSTANT BIT(0)
40#define SIXAXIS_CONTROLLER_USB BIT(1)
41#define SIXAXIS_CONTROLLER_BT BIT(2)
42#define BUZZ_CONTROLLER BIT(3)
43#define PS3REMOTE BIT(4)
Frank Praznik8ab16762014-01-16 21:42:31 -050044#define DUALSHOCK4_CONTROLLER_USB BIT(5)
45#define DUALSHOCK4_CONTROLLER_BT BIT(6)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020046
Frank Praznik8ab16762014-01-16 21:42:31 -050047#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER_USB | BUZZ_CONTROLLER | DUALSHOCK4_CONTROLLER_USB)
Frank Praznikd902f472014-01-27 10:17:36 -050048#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_USB)
Frank Praznik60781cf2014-01-11 15:13:15 -050049
50#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010051
Simon Wood61ab44b2011-06-10 12:00:26 +020052static const u8 sixaxis_rdesc_fixup[] = {
53 0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
54 0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
55 0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
56};
57
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -020058static const u8 sixaxis_rdesc_fixup2[] = {
59 0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
60 0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
61 0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
62 0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
63 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
64 0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
65 0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
66 0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
67 0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
68 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
69 0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
70 0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
71 0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
72 0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
73 0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
74 0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
75 0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
76 0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
77 0xb1, 0x02, 0xc0, 0xc0,
78};
79
Frank Praznik58d70272014-01-20 12:27:01 -050080/* The default descriptor doesn't provide mapping for the accelerometers
81 * or orientation sensors. This fixed descriptor maps the accelerometers
82 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
83 * to usage values 0x43, 0x44 and 0x45.
84 */
Frank Prazniked19d8c2014-01-16 21:43:12 -050085static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -050086 0x05, 0x01, /* Usage Page (Desktop), */
87 0x09, 0x05, /* Usage (Gamepad), */
88 0xA1, 0x01, /* Collection (Application), */
89 0x85, 0x01, /* Report ID (1), */
90 0x09, 0x30, /* Usage (X), */
91 0x09, 0x31, /* Usage (Y), */
92 0x09, 0x32, /* Usage (Z), */
93 0x09, 0x35, /* Usage (Rz), */
94 0x15, 0x00, /* Logical Minimum (0), */
95 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
96 0x75, 0x08, /* Report Size (8), */
97 0x95, 0x04, /* Report Count (4), */
98 0x81, 0x02, /* Input (Variable), */
99 0x09, 0x39, /* Usage (Hat Switch), */
100 0x15, 0x00, /* Logical Minimum (0), */
101 0x25, 0x07, /* Logical Maximum (7), */
102 0x35, 0x00, /* Physical Minimum (0), */
103 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
104 0x65, 0x14, /* Unit (Degrees), */
105 0x75, 0x04, /* Report Size (4), */
106 0x95, 0x01, /* Report Count (1), */
107 0x81, 0x42, /* Input (Variable, Null State), */
108 0x65, 0x00, /* Unit, */
109 0x05, 0x09, /* Usage Page (Button), */
110 0x19, 0x01, /* Usage Minimum (01h), */
111 0x29, 0x0E, /* Usage Maximum (0Eh), */
112 0x15, 0x00, /* Logical Minimum (0), */
113 0x25, 0x01, /* Logical Maximum (1), */
114 0x75, 0x01, /* Report Size (1), */
115 0x95, 0x0E, /* Report Count (14), */
116 0x81, 0x02, /* Input (Variable), */
117 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
118 0x09, 0x20, /* Usage (20h), */
119 0x75, 0x06, /* Report Size (6), */
120 0x95, 0x01, /* Report Count (1), */
121 0x15, 0x00, /* Logical Minimum (0), */
122 0x25, 0x7F, /* Logical Maximum (127), */
123 0x81, 0x02, /* Input (Variable), */
124 0x05, 0x01, /* Usage Page (Desktop), */
125 0x09, 0x33, /* Usage (Rx), */
126 0x09, 0x34, /* Usage (Ry), */
127 0x15, 0x00, /* Logical Minimum (0), */
128 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
129 0x75, 0x08, /* Report Size (8), */
130 0x95, 0x02, /* Report Count (2), */
131 0x81, 0x02, /* Input (Variable), */
132 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
133 0x09, 0x21, /* Usage (21h), */
134 0x95, 0x03, /* Report Count (3), */
135 0x81, 0x02, /* Input (Variable), */
136 0x05, 0x01, /* Usage Page (Desktop), */
137 0x19, 0x40, /* Usage Minimum (40h), */
138 0x29, 0x42, /* Usage Maximum (42h), */
139 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
140 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
141 0x75, 0x10, /* Report Size (16), */
142 0x95, 0x03, /* Report Count (3), */
143 0x81, 0x02, /* Input (Variable), */
144 0x19, 0x43, /* Usage Minimum (43h), */
145 0x29, 0x45, /* Usage Maximum (45h), */
146 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
147 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
148 0x95, 0x03, /* Report Count (3), */
149 0x81, 0x02, /* Input (Variable), */
150 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
151 0x09, 0x21, /* Usage (21h), */
152 0x15, 0x00, /* Logical Minimum (0), */
153 0x25, 0xFF, /* Logical Maximum (255), */
154 0x75, 0x08, /* Report Size (8), */
155 0x95, 0x27, /* Report Count (39), */
156 0x81, 0x02, /* Input (Variable), */
157 0x85, 0x05, /* Report ID (5), */
158 0x09, 0x22, /* Usage (22h), */
159 0x95, 0x1F, /* Report Count (31), */
160 0x91, 0x02, /* Output (Variable), */
161 0x85, 0x04, /* Report ID (4), */
162 0x09, 0x23, /* Usage (23h), */
163 0x95, 0x24, /* Report Count (36), */
164 0xB1, 0x02, /* Feature (Variable), */
165 0x85, 0x02, /* Report ID (2), */
166 0x09, 0x24, /* Usage (24h), */
167 0x95, 0x24, /* Report Count (36), */
168 0xB1, 0x02, /* Feature (Variable), */
169 0x85, 0x08, /* Report ID (8), */
170 0x09, 0x25, /* Usage (25h), */
171 0x95, 0x03, /* Report Count (3), */
172 0xB1, 0x02, /* Feature (Variable), */
173 0x85, 0x10, /* Report ID (16), */
174 0x09, 0x26, /* Usage (26h), */
175 0x95, 0x04, /* Report Count (4), */
176 0xB1, 0x02, /* Feature (Variable), */
177 0x85, 0x11, /* Report ID (17), */
178 0x09, 0x27, /* Usage (27h), */
179 0x95, 0x02, /* Report Count (2), */
180 0xB1, 0x02, /* Feature (Variable), */
181 0x85, 0x12, /* Report ID (18), */
182 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
183 0x09, 0x21, /* Usage (21h), */
184 0x95, 0x0F, /* Report Count (15), */
185 0xB1, 0x02, /* Feature (Variable), */
186 0x85, 0x13, /* Report ID (19), */
187 0x09, 0x22, /* Usage (22h), */
188 0x95, 0x16, /* Report Count (22), */
189 0xB1, 0x02, /* Feature (Variable), */
190 0x85, 0x14, /* Report ID (20), */
191 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
192 0x09, 0x20, /* Usage (20h), */
193 0x95, 0x10, /* Report Count (16), */
194 0xB1, 0x02, /* Feature (Variable), */
195 0x85, 0x15, /* Report ID (21), */
196 0x09, 0x21, /* Usage (21h), */
197 0x95, 0x2C, /* Report Count (44), */
198 0xB1, 0x02, /* Feature (Variable), */
199 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
200 0x85, 0x80, /* Report ID (128), */
201 0x09, 0x20, /* Usage (20h), */
202 0x95, 0x06, /* Report Count (6), */
203 0xB1, 0x02, /* Feature (Variable), */
204 0x85, 0x81, /* Report ID (129), */
205 0x09, 0x21, /* Usage (21h), */
206 0x95, 0x06, /* Report Count (6), */
207 0xB1, 0x02, /* Feature (Variable), */
208 0x85, 0x82, /* Report ID (130), */
209 0x09, 0x22, /* Usage (22h), */
210 0x95, 0x05, /* Report Count (5), */
211 0xB1, 0x02, /* Feature (Variable), */
212 0x85, 0x83, /* Report ID (131), */
213 0x09, 0x23, /* Usage (23h), */
214 0x95, 0x01, /* Report Count (1), */
215 0xB1, 0x02, /* Feature (Variable), */
216 0x85, 0x84, /* Report ID (132), */
217 0x09, 0x24, /* Usage (24h), */
218 0x95, 0x04, /* Report Count (4), */
219 0xB1, 0x02, /* Feature (Variable), */
220 0x85, 0x85, /* Report ID (133), */
221 0x09, 0x25, /* Usage (25h), */
222 0x95, 0x06, /* Report Count (6), */
223 0xB1, 0x02, /* Feature (Variable), */
224 0x85, 0x86, /* Report ID (134), */
225 0x09, 0x26, /* Usage (26h), */
226 0x95, 0x06, /* Report Count (6), */
227 0xB1, 0x02, /* Feature (Variable), */
228 0x85, 0x87, /* Report ID (135), */
229 0x09, 0x27, /* Usage (27h), */
230 0x95, 0x23, /* Report Count (35), */
231 0xB1, 0x02, /* Feature (Variable), */
232 0x85, 0x88, /* Report ID (136), */
233 0x09, 0x28, /* Usage (28h), */
234 0x95, 0x22, /* Report Count (34), */
235 0xB1, 0x02, /* Feature (Variable), */
236 0x85, 0x89, /* Report ID (137), */
237 0x09, 0x29, /* Usage (29h), */
238 0x95, 0x02, /* Report Count (2), */
239 0xB1, 0x02, /* Feature (Variable), */
240 0x85, 0x90, /* Report ID (144), */
241 0x09, 0x30, /* Usage (30h), */
242 0x95, 0x05, /* Report Count (5), */
243 0xB1, 0x02, /* Feature (Variable), */
244 0x85, 0x91, /* Report ID (145), */
245 0x09, 0x31, /* Usage (31h), */
246 0x95, 0x03, /* Report Count (3), */
247 0xB1, 0x02, /* Feature (Variable), */
248 0x85, 0x92, /* Report ID (146), */
249 0x09, 0x32, /* Usage (32h), */
250 0x95, 0x03, /* Report Count (3), */
251 0xB1, 0x02, /* Feature (Variable), */
252 0x85, 0x93, /* Report ID (147), */
253 0x09, 0x33, /* Usage (33h), */
254 0x95, 0x0C, /* Report Count (12), */
255 0xB1, 0x02, /* Feature (Variable), */
256 0x85, 0xA0, /* Report ID (160), */
257 0x09, 0x40, /* Usage (40h), */
258 0x95, 0x06, /* Report Count (6), */
259 0xB1, 0x02, /* Feature (Variable), */
260 0x85, 0xA1, /* Report ID (161), */
261 0x09, 0x41, /* Usage (41h), */
262 0x95, 0x01, /* Report Count (1), */
263 0xB1, 0x02, /* Feature (Variable), */
264 0x85, 0xA2, /* Report ID (162), */
265 0x09, 0x42, /* Usage (42h), */
266 0x95, 0x01, /* Report Count (1), */
267 0xB1, 0x02, /* Feature (Variable), */
268 0x85, 0xA3, /* Report ID (163), */
269 0x09, 0x43, /* Usage (43h), */
270 0x95, 0x30, /* Report Count (48), */
271 0xB1, 0x02, /* Feature (Variable), */
272 0x85, 0xA4, /* Report ID (164), */
273 0x09, 0x44, /* Usage (44h), */
274 0x95, 0x0D, /* Report Count (13), */
275 0xB1, 0x02, /* Feature (Variable), */
276 0x85, 0xA5, /* Report ID (165), */
277 0x09, 0x45, /* Usage (45h), */
278 0x95, 0x15, /* Report Count (21), */
279 0xB1, 0x02, /* Feature (Variable), */
280 0x85, 0xA6, /* Report ID (166), */
281 0x09, 0x46, /* Usage (46h), */
282 0x95, 0x15, /* Report Count (21), */
283 0xB1, 0x02, /* Feature (Variable), */
284 0x85, 0xF0, /* Report ID (240), */
285 0x09, 0x47, /* Usage (47h), */
286 0x95, 0x3F, /* Report Count (63), */
287 0xB1, 0x02, /* Feature (Variable), */
288 0x85, 0xF1, /* Report ID (241), */
289 0x09, 0x48, /* Usage (48h), */
290 0x95, 0x3F, /* Report Count (63), */
291 0xB1, 0x02, /* Feature (Variable), */
292 0x85, 0xF2, /* Report ID (242), */
293 0x09, 0x49, /* Usage (49h), */
294 0x95, 0x0F, /* Report Count (15), */
295 0xB1, 0x02, /* Feature (Variable), */
296 0x85, 0xA7, /* Report ID (167), */
297 0x09, 0x4A, /* Usage (4Ah), */
298 0x95, 0x01, /* Report Count (1), */
299 0xB1, 0x02, /* Feature (Variable), */
300 0x85, 0xA8, /* Report ID (168), */
301 0x09, 0x4B, /* Usage (4Bh), */
302 0x95, 0x01, /* Report Count (1), */
303 0xB1, 0x02, /* Feature (Variable), */
304 0x85, 0xA9, /* Report ID (169), */
305 0x09, 0x4C, /* Usage (4Ch), */
306 0x95, 0x08, /* Report Count (8), */
307 0xB1, 0x02, /* Feature (Variable), */
308 0x85, 0xAA, /* Report ID (170), */
309 0x09, 0x4E, /* Usage (4Eh), */
310 0x95, 0x01, /* Report Count (1), */
311 0xB1, 0x02, /* Feature (Variable), */
312 0x85, 0xAB, /* Report ID (171), */
313 0x09, 0x4F, /* Usage (4Fh), */
314 0x95, 0x39, /* Report Count (57), */
315 0xB1, 0x02, /* Feature (Variable), */
316 0x85, 0xAC, /* Report ID (172), */
317 0x09, 0x50, /* Usage (50h), */
318 0x95, 0x39, /* Report Count (57), */
319 0xB1, 0x02, /* Feature (Variable), */
320 0x85, 0xAD, /* Report ID (173), */
321 0x09, 0x51, /* Usage (51h), */
322 0x95, 0x0B, /* Report Count (11), */
323 0xB1, 0x02, /* Feature (Variable), */
324 0x85, 0xAE, /* Report ID (174), */
325 0x09, 0x52, /* Usage (52h), */
326 0x95, 0x01, /* Report Count (1), */
327 0xB1, 0x02, /* Feature (Variable), */
328 0x85, 0xAF, /* Report ID (175), */
329 0x09, 0x53, /* Usage (53h), */
330 0x95, 0x02, /* Report Count (2), */
331 0xB1, 0x02, /* Feature (Variable), */
332 0x85, 0xB0, /* Report ID (176), */
333 0x09, 0x54, /* Usage (54h), */
334 0x95, 0x3F, /* Report Count (63), */
335 0xB1, 0x02, /* Feature (Variable), */
336 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500337};
338
Frank Praznikd8296742014-02-05 20:03:45 -0500339/* The default behavior of the Dualshock 4 is to send reports using report
340 * type 1 when running over Bluetooth. However, as soon as it receives a
341 * report of type 17 to set the LEDs or rumble it starts returning it's state
342 * in report 17 instead of 1. Since report 17 is undefined in the default HID
343 * descriptor the button and axis definitions must be moved to report 17 or
344 * the HID layer won't process the received input once a report is sent.
345 */
346static u8 dualshock4_bt_rdesc[] = {
347 0x05, 0x01, /* Usage Page (Desktop), */
348 0x09, 0x05, /* Usage (Gamepad), */
349 0xA1, 0x01, /* Collection (Application), */
350 0x85, 0x01, /* Report ID (1), */
351 0x75, 0x08, /* Report Size (8), */
352 0x95, 0x0A, /* Report Count (9), */
353 0x81, 0x02, /* Input (Variable), */
354 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
355 0x85, 0x02, /* Report ID (2), */
356 0x09, 0x24, /* Usage (24h), */
357 0x95, 0x24, /* Report Count (36), */
358 0xB1, 0x02, /* Feature (Variable), */
359 0x85, 0xA3, /* Report ID (163), */
360 0x09, 0x25, /* Usage (25h), */
361 0x95, 0x30, /* Report Count (48), */
362 0xB1, 0x02, /* Feature (Variable), */
363 0x85, 0x05, /* Report ID (5), */
364 0x09, 0x26, /* Usage (26h), */
365 0x95, 0x28, /* Report Count (40), */
366 0xB1, 0x02, /* Feature (Variable), */
367 0x85, 0x06, /* Report ID (6), */
368 0x09, 0x27, /* Usage (27h), */
369 0x95, 0x34, /* Report Count (52), */
370 0xB1, 0x02, /* Feature (Variable), */
371 0x85, 0x07, /* Report ID (7), */
372 0x09, 0x28, /* Usage (28h), */
373 0x95, 0x30, /* Report Count (48), */
374 0xB1, 0x02, /* Feature (Variable), */
375 0x85, 0x08, /* Report ID (8), */
376 0x09, 0x29, /* Usage (29h), */
377 0x95, 0x2F, /* Report Count (47), */
378 0xB1, 0x02, /* Feature (Variable), */
379 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
380 0x85, 0x03, /* Report ID (3), */
381 0x09, 0x21, /* Usage (21h), */
382 0x95, 0x26, /* Report Count (38), */
383 0xB1, 0x02, /* Feature (Variable), */
384 0x85, 0x04, /* Report ID (4), */
385 0x09, 0x22, /* Usage (22h), */
386 0x95, 0x2E, /* Report Count (46), */
387 0xB1, 0x02, /* Feature (Variable), */
388 0x85, 0xF0, /* Report ID (240), */
389 0x09, 0x47, /* Usage (47h), */
390 0x95, 0x3F, /* Report Count (63), */
391 0xB1, 0x02, /* Feature (Variable), */
392 0x85, 0xF1, /* Report ID (241), */
393 0x09, 0x48, /* Usage (48h), */
394 0x95, 0x3F, /* Report Count (63), */
395 0xB1, 0x02, /* Feature (Variable), */
396 0x85, 0xF2, /* Report ID (242), */
397 0x09, 0x49, /* Usage (49h), */
398 0x95, 0x0F, /* Report Count (15), */
399 0xB1, 0x02, /* Feature (Variable), */
400 0x85, 0x11, /* Report ID (17), */
401 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
402 0x09, 0x20, /* Usage (20h), */
403 0x95, 0x02, /* Report Count (2), */
404 0x81, 0x02, /* Input (Variable), */
405 0x05, 0x01, /* Usage Page (Desktop), */
406 0x09, 0x30, /* Usage (X), */
407 0x09, 0x31, /* Usage (Y), */
408 0x09, 0x32, /* Usage (Z), */
409 0x09, 0x35, /* Usage (Rz), */
410 0x15, 0x00, /* Logical Minimum (0), */
411 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
412 0x75, 0x08, /* Report Size (8), */
413 0x95, 0x04, /* Report Count (4), */
414 0x81, 0x02, /* Input (Variable), */
415 0x09, 0x39, /* Usage (Hat Switch), */
416 0x15, 0x00, /* Logical Minimum (0), */
417 0x25, 0x07, /* Logical Maximum (7), */
418 0x75, 0x04, /* Report Size (4), */
419 0x95, 0x01, /* Report Count (1), */
420 0x81, 0x42, /* Input (Variable, Null State), */
421 0x05, 0x09, /* Usage Page (Button), */
422 0x19, 0x01, /* Usage Minimum (01h), */
423 0x29, 0x0E, /* Usage Maximum (0Eh), */
424 0x15, 0x00, /* Logical Minimum (0), */
425 0x25, 0x01, /* Logical Maximum (1), */
426 0x75, 0x01, /* Report Size (1), */
427 0x95, 0x0E, /* Report Count (14), */
428 0x81, 0x02, /* Input (Variable), */
429 0x75, 0x06, /* Report Size (6), */
430 0x95, 0x01, /* Report Count (1), */
431 0x81, 0x01, /* Input (Constant), */
432 0x05, 0x01, /* Usage Page (Desktop), */
433 0x09, 0x33, /* Usage (Rx), */
434 0x09, 0x34, /* Usage (Ry), */
435 0x15, 0x00, /* Logical Minimum (0), */
436 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
437 0x75, 0x08, /* Report Size (8), */
438 0x95, 0x02, /* Report Count (2), */
439 0x81, 0x02, /* Input (Variable), */
440 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
441 0x09, 0x20, /* Usage (20h), */
442 0x95, 0x03, /* Report Count (3), */
443 0x81, 0x02, /* Input (Variable), */
444 0x05, 0x01, /* Usage Page (Desktop), */
445 0x19, 0x40, /* Usage Minimum (40h), */
446 0x29, 0x42, /* Usage Maximum (42h), */
447 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
448 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
449 0x75, 0x10, /* Report Size (16), */
450 0x95, 0x03, /* Report Count (3), */
451 0x81, 0x02, /* Input (Variable), */
452 0x19, 0x43, /* Usage Minimum (43h), */
453 0x29, 0x45, /* Usage Maximum (45h), */
454 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
455 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
456 0x95, 0x03, /* Report Count (3), */
457 0x81, 0x02, /* Input (Variable), */
458 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
459 0x09, 0x20, /* Usage (20h), */
460 0x15, 0x00, /* Logical Minimum (0), */
461 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
462 0x75, 0x08, /* Report Size (8), */
463 0x95, 0x31, /* Report Count (51), */
464 0x81, 0x02, /* Input (Variable), */
465 0x09, 0x21, /* Usage (21h), */
466 0x75, 0x08, /* Report Size (8), */
467 0x95, 0x4D, /* Report Count (77), */
468 0x91, 0x02, /* Output (Variable), */
469 0x85, 0x12, /* Report ID (18), */
470 0x09, 0x22, /* Usage (22h), */
471 0x95, 0x8D, /* Report Count (141), */
472 0x81, 0x02, /* Input (Variable), */
473 0x09, 0x23, /* Usage (23h), */
474 0x91, 0x02, /* Output (Variable), */
475 0x85, 0x13, /* Report ID (19), */
476 0x09, 0x24, /* Usage (24h), */
477 0x95, 0xCD, /* Report Count (205), */
478 0x81, 0x02, /* Input (Variable), */
479 0x09, 0x25, /* Usage (25h), */
480 0x91, 0x02, /* Output (Variable), */
481 0x85, 0x14, /* Report ID (20), */
482 0x09, 0x26, /* Usage (26h), */
483 0x96, 0x0D, 0x01, /* Report Count (269), */
484 0x81, 0x02, /* Input (Variable), */
485 0x09, 0x27, /* Usage (27h), */
486 0x91, 0x02, /* Output (Variable), */
487 0x85, 0x15, /* Report ID (21), */
488 0x09, 0x28, /* Usage (28h), */
489 0x96, 0x4D, 0x01, /* Report Count (333), */
490 0x81, 0x02, /* Input (Variable), */
491 0x09, 0x29, /* Usage (29h), */
492 0x91, 0x02, /* Output (Variable), */
493 0x85, 0x16, /* Report ID (22), */
494 0x09, 0x2A, /* Usage (2Ah), */
495 0x96, 0x8D, 0x01, /* Report Count (397), */
496 0x81, 0x02, /* Input (Variable), */
497 0x09, 0x2B, /* Usage (2Bh), */
498 0x91, 0x02, /* Output (Variable), */
499 0x85, 0x17, /* Report ID (23), */
500 0x09, 0x2C, /* Usage (2Ch), */
501 0x96, 0xCD, 0x01, /* Report Count (461), */
502 0x81, 0x02, /* Input (Variable), */
503 0x09, 0x2D, /* Usage (2Dh), */
504 0x91, 0x02, /* Output (Variable), */
505 0x85, 0x18, /* Report ID (24), */
506 0x09, 0x2E, /* Usage (2Eh), */
507 0x96, 0x0D, 0x02, /* Report Count (525), */
508 0x81, 0x02, /* Input (Variable), */
509 0x09, 0x2F, /* Usage (2Fh), */
510 0x91, 0x02, /* Output (Variable), */
511 0x85, 0x19, /* Report ID (25), */
512 0x09, 0x30, /* Usage (30h), */
513 0x96, 0x22, 0x02, /* Report Count (546), */
514 0x81, 0x02, /* Input (Variable), */
515 0x09, 0x31, /* Usage (31h), */
516 0x91, 0x02, /* Output (Variable), */
517 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
518 0x85, 0x82, /* Report ID (130), */
519 0x09, 0x22, /* Usage (22h), */
520 0x95, 0x3F, /* Report Count (63), */
521 0xB1, 0x02, /* Feature (Variable), */
522 0x85, 0x83, /* Report ID (131), */
523 0x09, 0x23, /* Usage (23h), */
524 0xB1, 0x02, /* Feature (Variable), */
525 0x85, 0x84, /* Report ID (132), */
526 0x09, 0x24, /* Usage (24h), */
527 0xB1, 0x02, /* Feature (Variable), */
528 0x85, 0x90, /* Report ID (144), */
529 0x09, 0x30, /* Usage (30h), */
530 0xB1, 0x02, /* Feature (Variable), */
531 0x85, 0x91, /* Report ID (145), */
532 0x09, 0x31, /* Usage (31h), */
533 0xB1, 0x02, /* Feature (Variable), */
534 0x85, 0x92, /* Report ID (146), */
535 0x09, 0x32, /* Usage (32h), */
536 0xB1, 0x02, /* Feature (Variable), */
537 0x85, 0x93, /* Report ID (147), */
538 0x09, 0x33, /* Usage (33h), */
539 0xB1, 0x02, /* Feature (Variable), */
540 0x85, 0xA0, /* Report ID (160), */
541 0x09, 0x40, /* Usage (40h), */
542 0xB1, 0x02, /* Feature (Variable), */
543 0x85, 0xA4, /* Report ID (164), */
544 0x09, 0x44, /* Usage (44h), */
545 0xB1, 0x02, /* Feature (Variable), */
546 0xC0 /* End Collection */
547};
548
Jiri Kosina078328d2013-06-13 12:03:49 +0200549static __u8 ps3remote_rdesc[] = {
550 0x05, 0x01, /* GUsagePage Generic Desktop */
551 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
552 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
553
554 /* Use collection 1 for joypad buttons */
555 0xA1, 0x02, /* MCollection Logical (interrelated data) */
556
557 /* Ignore the 1st byte, maybe it is used for a controller
558 * number but it's not needed for correct operation */
559 0x75, 0x08, /* GReportSize 0x08 [8] */
560 0x95, 0x01, /* GReportCount 0x01 [1] */
561 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
562
563 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
564 * buttons multiple keypresses are allowed */
565 0x05, 0x09, /* GUsagePage Button */
566 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
567 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
568 0x14, /* GLogicalMinimum [0] */
569 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
570 0x75, 0x01, /* GReportSize 0x01 [1] */
571 0x95, 0x18, /* GReportCount 0x18 [24] */
572 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
573
574 0xC0, /* MEndCollection */
575
576 /* Use collection 2 for remote control buttons */
577 0xA1, 0x02, /* MCollection Logical (interrelated data) */
578
579 /* 5th byte is used for remote control buttons */
580 0x05, 0x09, /* GUsagePage Button */
581 0x18, /* LUsageMinimum [No button pressed] */
582 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
583 0x14, /* GLogicalMinimum [0] */
584 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
585 0x75, 0x08, /* GReportSize 0x08 [8] */
586 0x95, 0x01, /* GReportCount 0x01 [1] */
587 0x80, /* MInput */
588
589 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
590 * 0xff and 11th is for press indication */
591 0x75, 0x08, /* GReportSize 0x08 [8] */
592 0x95, 0x06, /* GReportCount 0x06 [6] */
593 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
594
595 /* 12th byte is for battery strength */
596 0x05, 0x06, /* GUsagePage Generic Device Controls */
597 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
598 0x14, /* GLogicalMinimum [0] */
599 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
600 0x75, 0x08, /* GReportSize 0x08 [8] */
601 0x95, 0x01, /* GReportCount 0x01 [1] */
602 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
603
604 0xC0, /* MEndCollection */
605
606 0xC0 /* MEndCollection [Game Pad] */
607};
608
609static const unsigned int ps3remote_keymap_joypad_buttons[] = {
610 [0x01] = KEY_SELECT,
611 [0x02] = BTN_THUMBL, /* L3 */
612 [0x03] = BTN_THUMBR, /* R3 */
613 [0x04] = BTN_START,
614 [0x05] = KEY_UP,
615 [0x06] = KEY_RIGHT,
616 [0x07] = KEY_DOWN,
617 [0x08] = KEY_LEFT,
618 [0x09] = BTN_TL2, /* L2 */
619 [0x0a] = BTN_TR2, /* R2 */
620 [0x0b] = BTN_TL, /* L1 */
621 [0x0c] = BTN_TR, /* R1 */
622 [0x0d] = KEY_OPTION, /* options/triangle */
623 [0x0e] = KEY_BACK, /* back/circle */
624 [0x0f] = BTN_0, /* cross */
625 [0x10] = KEY_SCREEN, /* view/square */
626 [0x11] = KEY_HOMEPAGE, /* PS button */
627 [0x14] = KEY_ENTER,
628};
629static const unsigned int ps3remote_keymap_remote_buttons[] = {
630 [0x00] = KEY_1,
631 [0x01] = KEY_2,
632 [0x02] = KEY_3,
633 [0x03] = KEY_4,
634 [0x04] = KEY_5,
635 [0x05] = KEY_6,
636 [0x06] = KEY_7,
637 [0x07] = KEY_8,
638 [0x08] = KEY_9,
639 [0x09] = KEY_0,
640 [0x0e] = KEY_ESC, /* return */
641 [0x0f] = KEY_CLEAR,
642 [0x16] = KEY_EJECTCD,
643 [0x1a] = KEY_MENU, /* top menu */
644 [0x28] = KEY_TIME,
645 [0x30] = KEY_PREVIOUS,
646 [0x31] = KEY_NEXT,
647 [0x32] = KEY_PLAY,
648 [0x33] = KEY_REWIND, /* scan back */
649 [0x34] = KEY_FORWARD, /* scan forward */
650 [0x38] = KEY_STOP,
651 [0x39] = KEY_PAUSE,
652 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
653 [0x60] = KEY_FRAMEBACK, /* slow/step back */
654 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
655 [0x63] = KEY_SUBTITLE,
656 [0x64] = KEY_AUDIO,
657 [0x65] = KEY_ANGLE,
658 [0x70] = KEY_INFO, /* display */
659 [0x80] = KEY_BLUE,
660 [0x81] = KEY_RED,
661 [0x82] = KEY_GREEN,
662 [0x83] = KEY_YELLOW,
663};
664
Colin Leitnerf04d5142013-05-27 23:41:05 +0200665static const unsigned int buzz_keymap[] = {
666 /* The controller has 4 remote buzzers, each with one LED and 5
667 * buttons.
668 *
669 * We use the mapping chosen by the controller, which is:
670 *
671 * Key Offset
672 * -------------------
673 * Buzz 1
674 * Blue 5
675 * Orange 4
676 * Green 3
677 * Yellow 2
678 *
679 * So, for example, the orange button on the third buzzer is mapped to
680 * BTN_TRIGGER_HAPPY14
681 */
682 [ 1] = BTN_TRIGGER_HAPPY1,
683 [ 2] = BTN_TRIGGER_HAPPY2,
684 [ 3] = BTN_TRIGGER_HAPPY3,
685 [ 4] = BTN_TRIGGER_HAPPY4,
686 [ 5] = BTN_TRIGGER_HAPPY5,
687 [ 6] = BTN_TRIGGER_HAPPY6,
688 [ 7] = BTN_TRIGGER_HAPPY7,
689 [ 8] = BTN_TRIGGER_HAPPY8,
690 [ 9] = BTN_TRIGGER_HAPPY9,
691 [10] = BTN_TRIGGER_HAPPY10,
692 [11] = BTN_TRIGGER_HAPPY11,
693 [12] = BTN_TRIGGER_HAPPY12,
694 [13] = BTN_TRIGGER_HAPPY13,
695 [14] = BTN_TRIGGER_HAPPY14,
696 [15] = BTN_TRIGGER_HAPPY15,
697 [16] = BTN_TRIGGER_HAPPY16,
698 [17] = BTN_TRIGGER_HAPPY17,
699 [18] = BTN_TRIGGER_HAPPY18,
700 [19] = BTN_TRIGGER_HAPPY19,
701 [20] = BTN_TRIGGER_HAPPY20,
702};
703
Frank Praznikd902f472014-01-27 10:17:36 -0500704static enum power_supply_property sony_battery_props[] = {
705 POWER_SUPPLY_PROP_PRESENT,
706 POWER_SUPPLY_PROP_CAPACITY,
707 POWER_SUPPLY_PROP_SCOPE,
708 POWER_SUPPLY_PROP_STATUS,
709};
710
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200711struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500712 spinlock_t lock;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100713 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500714 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200715 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100716 struct work_struct state_worker;
Frank Praznikd902f472014-01-27 10:17:36 -0500717 struct power_supply battery;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200718
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100719#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100720 __u8 left;
721 __u8 right;
722#endif
723
Frank Praznikd902f472014-01-27 10:17:36 -0500724 __u8 cable_state;
725 __u8 battery_charging;
726 __u8 battery_capacity;
Frank Praznik60781cf2014-01-11 15:13:15 -0500727 __u8 led_state[MAX_LEDS];
728 __u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200729};
730
Jiri Kosina078328d2013-06-13 12:03:49 +0200731static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
732 unsigned int *rsize)
733{
734 *rsize = sizeof(ps3remote_rdesc);
735 return ps3remote_rdesc;
736}
737
738static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
739 struct hid_field *field, struct hid_usage *usage,
740 unsigned long **bit, int *max)
741{
742 unsigned int key = usage->hid & HID_USAGE;
743
744 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
745 return -1;
746
747 switch (usage->collection_index) {
748 case 1:
749 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
750 return -1;
751
752 key = ps3remote_keymap_joypad_buttons[key];
753 if (!key)
754 return -1;
755 break;
756 case 2:
757 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
758 return -1;
759
760 key = ps3remote_keymap_remote_buttons[key];
761 if (!key)
762 return -1;
763 break;
764 default:
765 return -1;
766 }
767
768 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
769 return 1;
770}
771
772
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200773/* Sony Vaio VGX has wrongly mouse pointer declared as constant */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400774static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
775 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200776{
777 struct sony_sc *sc = hid_get_drvdata(hdev);
778
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900779 /*
780 * Some Sony RF receivers wrongly declare the mouse pointer as a
781 * a constant non-data variable.
782 */
783 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
784 /* usage page: generic desktop controls */
785 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
786 /* usage: mouse */
787 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
788 /* input (usage page for x,y axes): constant, variable, relative */
789 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +0900790 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900791 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200792 rdesc[55] = 0x06;
793 }
Simon Wood61ab44b2011-06-10 12:00:26 +0200794
Frank Prazniked19d8c2014-01-16 21:43:12 -0500795 /*
796 * The default Dualshock 4 USB descriptor doesn't assign
797 * the gyroscope values to corresponding axes so we need a
798 * modified one.
799 */
800 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
801 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
802 rdesc = dualshock4_usb_rdesc;
803 *rsize = sizeof(dualshock4_usb_rdesc);
Frank Praznikd8296742014-02-05 20:03:45 -0500804 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
805 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
806 rdesc = dualshock4_bt_rdesc;
807 *rsize = sizeof(dualshock4_bt_rdesc);
Frank Prazniked19d8c2014-01-16 21:43:12 -0500808 }
809
Simon Wood61ab44b2011-06-10 12:00:26 +0200810 /* The HID descriptor exposed over BT has a trailing zero byte */
811 if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
812 ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
813 rdesc[83] == 0x75) {
814 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
815 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
816 sizeof(sixaxis_rdesc_fixup));
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200817 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
818 *rsize > sizeof(sixaxis_rdesc_fixup2)) {
819 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
820 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
821 *rsize = sizeof(sixaxis_rdesc_fixup2);
822 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
Simon Wood61ab44b2011-06-10 12:00:26 +0200823 }
Jiri Kosina078328d2013-06-13 12:03:49 +0200824
825 if (sc->quirks & PS3REMOTE)
826 return ps3remote_fixup(hdev, rdesc, rsize);
827
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400828 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200829}
830
Frank Praznikd902f472014-01-27 10:17:36 -0500831static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
832{
833 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
834 unsigned long flags;
835 __u8 cable_state, battery_capacity, battery_charging;
836
837 /* The sixaxis is charging if the battery value is 0xee
838 * and it is fully charged if the value is 0xef.
839 * It does not report the actual level while charging so it
840 * is set to 100% while charging is in progress.
841 */
842 if (rd[30] >= 0xee) {
843 battery_capacity = 100;
844 battery_charging = rd[30] & 0x01;
845 } else {
846 battery_capacity = sixaxis_battery_capacity[rd[30]];
847 battery_charging = 0;
848 }
849 cable_state = (rd[31] >> 4) & 0x01;
850
851 spin_lock_irqsave(&sc->lock, flags);
852 sc->cable_state = cable_state;
853 sc->battery_capacity = battery_capacity;
854 sc->battery_charging = battery_charging;
855 spin_unlock_irqrestore(&sc->lock, flags);
856}
857
858static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
859{
Frank Praznike5606232014-01-27 10:17:37 -0500860 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
861 struct hid_input, list);
862 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -0500863 unsigned long flags;
Frank Praznik6c5f8602014-02-05 20:03:47 -0500864 int n, offset;
Frank Praznikd902f472014-01-27 10:17:36 -0500865 __u8 cable_state, battery_capacity, battery_charging;
866
Frank Praznik6c5f8602014-02-05 20:03:47 -0500867 /* Battery and touchpad data starts at byte 30 in the USB report and
868 * 32 in Bluetooth report.
869 */
870 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
871
Frank Praznikd902f472014-01-27 10:17:36 -0500872 /* The lower 4 bits of byte 30 contain the battery level
873 * and the 5th bit contains the USB cable state.
874 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500875 cable_state = (rd[offset] >> 4) & 0x01;
876 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -0500877
Frank Praznik6c5f8602014-02-05 20:03:47 -0500878 /* When a USB power source is connected the battery level ranges from
879 * 0 to 10, and when running on battery power it ranges from 0 to 9.
880 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -0500881 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500882 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -0500883 battery_charging = 0;
884 else
885 battery_charging = 1;
886
Frank Praznik6c5f8602014-02-05 20:03:47 -0500887 if (!cable_state)
888 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -0500889 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -0500890 battery_capacity = 10;
891
Frank Praznikd902f472014-01-27 10:17:36 -0500892 battery_capacity *= 10;
893
894 spin_lock_irqsave(&sc->lock, flags);
895 sc->cable_state = cable_state;
896 sc->battery_capacity = battery_capacity;
897 sc->battery_charging = battery_charging;
898 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -0500899
Frank Praznik6c5f8602014-02-05 20:03:47 -0500900 offset += 5;
901
902 /* The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
903 * and 37 on Bluetooth.
Frank Praznike5606232014-01-27 10:17:37 -0500904 * The first 7 bits of the first byte is a counter and bit 8 is a touch
905 * indicator that is 0 when pressed and 1 when not pressed.
906 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
907 * The data for the second touch is in the same format and immediatly
908 * follows the data for the first.
909 */
910 for (n = 0; n < 2; n++) {
911 __u16 x, y;
912
913 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
914 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
915
916 input_mt_slot(input_dev, n);
917 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
918 !(rd[offset] >> 7));
919 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
920 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
921
922 offset += 4;
923 }
Frank Praznikd902f472014-01-27 10:17:36 -0500924}
925
Simon Woodc9e4d872011-06-10 12:00:27 +0200926static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
927 __u8 *rd, int size)
928{
929 struct sony_sc *sc = hid_get_drvdata(hdev);
930
931 /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
932 * has to be BYTE_SWAPPED before passing up to joystick interface
933 */
934 if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
935 rd[0] == 0x01 && size == 49) {
936 swap(rd[41], rd[42]);
937 swap(rd[43], rd[44]);
938 swap(rd[45], rd[46]);
939 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -0500940
941 sixaxis_parse_report(sc, rd, size);
942 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
943 size == 64) {
944 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +0200945 }
946
947 return 0;
948}
949
Colin Leitnerf04d5142013-05-27 23:41:05 +0200950static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
951 struct hid_field *field, struct hid_usage *usage,
952 unsigned long **bit, int *max)
953{
954 struct sony_sc *sc = hid_get_drvdata(hdev);
955
956 if (sc->quirks & BUZZ_CONTROLLER) {
957 unsigned int key = usage->hid & HID_USAGE;
958
959 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
960 return -1;
961
962 switch (usage->collection_index) {
963 case 1:
964 if (key >= ARRAY_SIZE(buzz_keymap))
965 return -1;
966
967 key = buzz_keymap[key];
968 if (!key)
969 return -1;
970 break;
971 default:
972 return -1;
973 }
974
975 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
976 return 1;
977 }
978
Jiri Kosina078328d2013-06-13 12:03:49 +0200979 if (sc->quirks & PS3REMOTE)
980 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
981
Benjamin Tissoires6f498012013-07-24 16:53:07 +0200982 /* Let hid-core decide for the others */
983 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200984}
985
Antonio Ospite5710fab2011-02-20 18:26:45 +0100986/*
987 * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
988 * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
989 * so we need to override that forcing HID Output Reports on the Control EP.
990 *
991 * There is also another issue about HID Output Reports via USB, the Sixaxis
992 * does not want the report_id as part of the data packet, so we have to
993 * discard buf[0] when sending the actual control message, even for numbered
994 * reports, humpf!
995 */
Antonio Ospite569b10a2010-10-19 16:13:10 +0200996static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
997 size_t count, unsigned char report_type)
998{
999 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1000 struct usb_device *dev = interface_to_usbdev(intf);
1001 struct usb_host_interface *interface = intf->cur_altsetting;
1002 int report_id = buf[0];
1003 int ret;
1004
Antonio Ospite5710fab2011-02-20 18:26:45 +01001005 if (report_type == HID_OUTPUT_REPORT) {
1006 /* Don't send the Report ID */
1007 buf++;
1008 count--;
1009 }
1010
Antonio Ospite569b10a2010-10-19 16:13:10 +02001011 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1012 HID_REQ_SET_REPORT,
1013 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1014 ((report_type + 1) << 8) | report_id,
1015 interface->desc.bInterfaceNumber, buf, count,
1016 USB_CTRL_SET_TIMEOUT);
1017
Antonio Ospite5710fab2011-02-20 18:26:45 +01001018 /* Count also the Report ID, in case of an Output report. */
1019 if (ret > 0 && report_type == HID_OUTPUT_REPORT)
1020 ret++;
1021
Antonio Ospite569b10a2010-10-19 16:13:10 +02001022 return ret;
1023}
1024
Jiri Slabybd28ce02008-06-25 23:47:04 +02001025/*
1026 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1027 * to "operational". Without this, the ps3 controller will not report any
1028 * events.
1029 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001030static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001031{
Jiri Slabybd28ce02008-06-25 23:47:04 +02001032 int ret;
1033 char *buf = kmalloc(18, GFP_KERNEL);
1034
1035 if (!buf)
1036 return -ENOMEM;
1037
Benjamin Tissoirescafebc02014-02-05 16:33:22 -05001038 ret = hid_hw_raw_request(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT,
1039 HID_REQ_GET_REPORT);
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001040
Jiri Slabybd28ce02008-06-25 23:47:04 +02001041 if (ret < 0)
Joe Perches4291ee32010-12-09 19:29:03 -08001042 hid_err(hdev, "can't set operational mode\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +02001043
1044 kfree(buf);
1045
1046 return ret;
1047}
1048
Antonio Ospite816651a2010-05-03 22:15:55 +02001049static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001050{
Antonio Ospitefddb33f2010-05-03 17:19:03 +02001051 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
Benjamin Tissoires7e845d42014-02-05 16:33:23 -05001052 return hid_output_raw_report(hdev, buf, sizeof(buf),
1053 HID_FEATURE_REPORT);
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001054}
1055
Frank Praznik60781cf2014-01-11 15:13:15 -05001056static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001057{
1058 struct list_head *report_list =
1059 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1060 struct hid_report *report = list_entry(report_list->next,
1061 struct hid_report, list);
1062 __s32 *value = report->field[0]->value;
1063
1064 value[0] = 0x00;
Frank Praznik60781cf2014-01-11 15:13:15 -05001065 value[1] = leds[0] ? 0xff : 0x00;
1066 value[2] = leds[1] ? 0xff : 0x00;
1067 value[3] = leds[2] ? 0xff : 0x00;
1068 value[4] = leds[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001069 value[5] = 0x00;
1070 value[6] = 0x00;
1071 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1072}
1073
Frank Praznik60781cf2014-01-11 15:13:15 -05001074static void sony_set_leds(struct hid_device *hdev, const __u8 *leds, int count)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001075{
1076 struct sony_sc *drv_data = hid_get_drvdata(hdev);
Frank Praznik60781cf2014-01-11 15:13:15 -05001077 int n;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001078
Frank Praznik60781cf2014-01-11 15:13:15 -05001079 BUG_ON(count > MAX_LEDS);
1080
1081 if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001082 buzz_set_leds(hdev, leds);
Frank Praznik60781cf2014-01-11 15:13:15 -05001083 } else if ((drv_data->quirks & SIXAXIS_CONTROLLER_USB) ||
Frank Praznik8ab16762014-01-16 21:42:31 -05001084 (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB)) {
Frank Praznik60781cf2014-01-11 15:13:15 -05001085 for (n = 0; n < count; n++)
1086 drv_data->led_state[n] = leds[n];
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001087 schedule_work(&drv_data->state_worker);
1088 }
1089}
1090
Sven Eckelmannc5382512013-11-19 20:26:30 +01001091static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001092 enum led_brightness value)
1093{
1094 struct device *dev = led->dev->parent;
1095 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1096 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001097
1098 int n;
1099
1100 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001101 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001102 hid_err(hdev, "No device data\n");
1103 return;
1104 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001105
Frank Praznik60781cf2014-01-11 15:13:15 -05001106 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann2251b852013-11-19 20:26:31 +01001107 if (led == drv_data->leds[n]) {
Frank Praznik60781cf2014-01-11 15:13:15 -05001108 if (value != drv_data->led_state[n]) {
1109 drv_data->led_state[n] = value;
1110 sony_set_leds(hdev, drv_data->led_state, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001111 }
1112 break;
1113 }
1114 }
1115}
1116
Sven Eckelmannc5382512013-11-19 20:26:30 +01001117static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001118{
1119 struct device *dev = led->dev->parent;
1120 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1121 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001122
1123 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001124
1125 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001126 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001127 hid_err(hdev, "No device data\n");
1128 return LED_OFF;
1129 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001130
Frank Praznik60781cf2014-01-11 15:13:15 -05001131 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001132 if (led == drv_data->leds[n])
1133 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001134 }
1135
Simon Wood7db75042014-02-05 12:34:18 -07001136 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001137}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001138
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001139static void sony_leds_remove(struct hid_device *hdev)
1140{
1141 struct sony_sc *drv_data;
1142 struct led_classdev *led;
1143 int n;
1144
1145 drv_data = hid_get_drvdata(hdev);
1146 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
1147
Frank Praznik60781cf2014-01-11 15:13:15 -05001148 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001149 led = drv_data->leds[n];
1150 drv_data->leds[n] = NULL;
1151 if (!led)
1152 continue;
1153 led_classdev_unregister(led);
1154 kfree(led);
1155 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001156
1157 drv_data->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001158}
1159
Sven Eckelmannc5382512013-11-19 20:26:30 +01001160static int sony_leds_init(struct hid_device *hdev)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001161{
1162 struct sony_sc *drv_data;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001163 int n, ret = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -05001164 int max_brightness;
Frank Praznik61ebca92014-01-20 12:27:02 -05001165 int use_colors;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001166 struct led_classdev *led;
1167 size_t name_sz;
1168 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001169 size_t name_len;
1170 const char *name_fmt;
Frank Praznik61ebca92014-01-20 12:27:02 -05001171 static const char * const color_str[] = { "red", "green", "blue" };
Frank Praznik60781cf2014-01-11 15:13:15 -05001172 static const __u8 initial_values[MAX_LEDS] = { 0x00, 0x00, 0x00, 0x00 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001173
1174 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001175 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001176
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001177 if (drv_data->quirks & BUZZ_CONTROLLER) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001178 drv_data->led_count = 4;
1179 max_brightness = 1;
1180 use_colors = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001181 name_len = strlen("::buzz#");
1182 name_fmt = "%s::buzz%d";
1183 /* Validate expected report characteristics. */
1184 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1185 return -ENODEV;
Frank Praznik61ebca92014-01-20 12:27:02 -05001186 } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik60781cf2014-01-11 15:13:15 -05001187 drv_data->led_count = 3;
1188 max_brightness = 255;
Frank Praznik61ebca92014-01-20 12:27:02 -05001189 use_colors = 1;
1190 name_len = 0;
1191 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -05001192 } else {
1193 drv_data->led_count = 4;
1194 max_brightness = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001195 use_colors = 0;
1196 name_len = strlen("::sony#");
1197 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001198 }
1199
Colin Leitnerf04d5142013-05-27 23:41:05 +02001200 /* Clear LEDs as we have no way of reading their initial state. This is
1201 * only relevant if the driver is loaded after somebody actively set the
1202 * LEDs to on */
Frank Praznik60781cf2014-01-11 15:13:15 -05001203 sony_set_leds(hdev, initial_values, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001204
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001205 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001206
Frank Praznik60781cf2014-01-11 15:13:15 -05001207 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001208
1209 if (use_colors)
1210 name_sz = strlen(dev_name(&hdev->dev)) + strlen(color_str[n]) + 2;
1211
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001212 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1213 if (!led) {
1214 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001215 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001216 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001217 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001218
1219 name = (void *)(&led[1]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001220 if (use_colors)
1221 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), color_str[n]);
1222 else
1223 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001224 led->name = name;
1225 led->brightness = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -05001226 led->max_brightness = max_brightness;
Sven Eckelmannc5382512013-11-19 20:26:30 +01001227 led->brightness_get = sony_led_get_brightness;
1228 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001229
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001230 ret = led_classdev_register(&hdev->dev, led);
1231 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001232 hid_err(hdev, "Failed to register LED %d\n", n);
1233 kfree(led);
1234 goto error_leds;
1235 }
1236
Sven Eckelmann2251b852013-11-19 20:26:31 +01001237 drv_data->leds[n] = led;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001238 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001239
1240 return ret;
1241
Colin Leitnerf04d5142013-05-27 23:41:05 +02001242error_leds:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001243 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001244
Colin Leitnerf04d5142013-05-27 23:41:05 +02001245 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001246}
1247
Frank Praznikcad665a2014-01-11 15:13:54 -05001248static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001249{
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001250 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001251 unsigned char buf[] = {
1252 0x01,
1253 0x00, 0xff, 0x00, 0xff, 0x00,
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001254 0x00, 0x00, 0x00, 0x00, 0x00,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001255 0xff, 0x27, 0x10, 0x00, 0x32,
1256 0xff, 0x27, 0x10, 0x00, 0x32,
1257 0xff, 0x27, 0x10, 0x00, 0x32,
1258 0xff, 0x27, 0x10, 0x00, 0x32,
1259 0x00, 0x00, 0x00, 0x00, 0x00
1260 };
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001261
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001262#ifdef CONFIG_SONY_FF
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001263 buf[3] = sc->right ? 1 : 0;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001264 buf[5] = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001265#endif
1266
Frank Praznik60781cf2014-01-11 15:13:15 -05001267 buf[10] |= sc->led_state[0] << 1;
1268 buf[10] |= sc->led_state[1] << 2;
1269 buf[10] |= sc->led_state[2] << 3;
1270 buf[10] |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001271
Benjamin Tissoires7e845d42014-02-05 16:33:23 -05001272 hid_output_raw_report(sc->hdev, buf, sizeof(buf), HID_OUTPUT_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001273}
1274
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001275static void dualshock4_state_worker(struct work_struct *work)
1276{
1277 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001278 struct hid_device *hdev = sc->hdev;
Frank Praznik48220232014-02-05 20:03:44 -05001279 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001280
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001281 __u8 buf[78] = { 0 };
Frank Praznik48220232014-02-05 20:03:44 -05001282
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001283 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1284 buf[0] = 0x05;
1285 buf[1] = 0x03;
1286 offset = 4;
1287 } else {
1288 buf[0] = 0x11;
1289 buf[1] = 0xB0;
1290 buf[3] = 0x0F;
1291 offset = 6;
1292 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001293
1294#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001295 buf[offset++] = sc->right;
1296 buf[offset++] = sc->left;
1297#else
1298 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001299#endif
1300
Frank Praznik48220232014-02-05 20:03:44 -05001301 buf[offset++] = sc->led_state[0];
1302 buf[offset++] = sc->led_state[1];
1303 buf[offset++] = sc->led_state[2];
Frank Praznik60781cf2014-01-11 15:13:15 -05001304
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001305 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1306 hid_hw_output_report(hdev, buf, 32);
1307 else
1308 hid_hw_raw_request(hdev, 0x11, buf, 78,
1309 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001310}
1311
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001312#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001313static int sony_play_effect(struct input_dev *dev, void *data,
1314 struct ff_effect *effect)
1315{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001316 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001317 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001318
1319 if (effect->type != FF_RUMBLE)
1320 return 0;
1321
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001322 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001323 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001324
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001325 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001326 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001327}
1328
1329static int sony_init_ff(struct hid_device *hdev)
1330{
1331 struct hid_input *hidinput = list_entry(hdev->inputs.next,
1332 struct hid_input, list);
1333 struct input_dev *input_dev = hidinput->input;
1334
1335 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1336 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1337}
1338
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001339static void sony_destroy_ff(struct hid_device *hdev)
1340{
1341 struct sony_sc *sc = hid_get_drvdata(hdev);
1342
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001343 cancel_work_sync(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001344}
1345
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001346#else
1347static int sony_init_ff(struct hid_device *hdev)
1348{
1349 return 0;
1350}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001351
1352static void sony_destroy_ff(struct hid_device *hdev)
1353{
1354}
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001355#endif
1356
Frank Praznikd902f472014-01-27 10:17:36 -05001357static int sony_battery_get_property(struct power_supply *psy,
1358 enum power_supply_property psp,
1359 union power_supply_propval *val)
1360{
1361 struct sony_sc *sc = container_of(psy, struct sony_sc, battery);
1362 unsigned long flags;
1363 int ret = 0;
1364 u8 battery_charging, battery_capacity, cable_state;
1365
1366 spin_lock_irqsave(&sc->lock, flags);
1367 battery_charging = sc->battery_charging;
1368 battery_capacity = sc->battery_capacity;
1369 cable_state = sc->cable_state;
1370 spin_unlock_irqrestore(&sc->lock, flags);
1371
1372 switch (psp) {
1373 case POWER_SUPPLY_PROP_PRESENT:
1374 val->intval = 1;
1375 break;
1376 case POWER_SUPPLY_PROP_SCOPE:
1377 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1378 break;
1379 case POWER_SUPPLY_PROP_CAPACITY:
1380 val->intval = battery_capacity;
1381 break;
1382 case POWER_SUPPLY_PROP_STATUS:
1383 if (battery_charging)
1384 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1385 else
1386 if (battery_capacity == 100 && cable_state)
1387 val->intval = POWER_SUPPLY_STATUS_FULL;
1388 else
1389 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1390 break;
1391 default:
1392 ret = -EINVAL;
1393 break;
1394 }
1395 return ret;
1396}
1397
1398static int sony_battery_probe(struct sony_sc *sc)
1399{
1400 static atomic_t power_id_seq = ATOMIC_INIT(0);
1401 unsigned long power_id;
1402 struct hid_device *hdev = sc->hdev;
1403 int ret;
1404
1405 power_id = (unsigned long)atomic_inc_return(&power_id_seq);
1406
1407 sc->battery.properties = sony_battery_props;
1408 sc->battery.num_properties = ARRAY_SIZE(sony_battery_props);
1409 sc->battery.get_property = sony_battery_get_property;
1410 sc->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1411 sc->battery.use_for_apm = 0;
1412 sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%lu",
1413 power_id);
1414 if (!sc->battery.name)
1415 return -ENOMEM;
1416
1417 ret = power_supply_register(&hdev->dev, &sc->battery);
1418 if (ret) {
1419 hid_err(hdev, "Unable to register battery device\n");
1420 goto err_free;
1421 }
1422
1423 power_supply_powers(&sc->battery, &hdev->dev);
1424 return 0;
1425
1426err_free:
1427 kfree(sc->battery.name);
1428 sc->battery.name = NULL;
1429 return ret;
1430}
1431
1432static void sony_battery_remove(struct sony_sc *sc)
1433{
1434 if (!sc->battery.name)
1435 return;
1436
1437 power_supply_unregister(&sc->battery);
1438 kfree(sc->battery.name);
1439 sc->battery.name = NULL;
1440}
1441
Frank Praznike5606232014-01-27 10:17:37 -05001442static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
1443 int w, int h)
1444{
1445 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1446 struct hid_input, list);
1447 struct input_dev *input_dev = hidinput->input;
1448 int ret;
1449
1450 ret = input_mt_init_slots(input_dev, touch_count, 0);
1451 if (ret < 0) {
1452 hid_err(sc->hdev, "Unable to initialize multi-touch slots\n");
1453 return ret;
1454 }
1455
1456 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1457 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1458
1459 return 0;
1460}
1461
Jiri Slabybd28ce02008-06-25 23:47:04 +02001462static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1463{
1464 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001465 unsigned long quirks = id->driver_data;
1466 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001467 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001468
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001469 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001470 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08001471 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001472 return -ENOMEM;
1473 }
1474
1475 sc->quirks = quirks;
1476 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001477 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001478
Jiri Slabybd28ce02008-06-25 23:47:04 +02001479 ret = hid_parse(hdev);
1480 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001481 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001482 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001483 }
1484
Colin Leitnerf04d5142013-05-27 23:41:05 +02001485 if (sc->quirks & VAIO_RDESC_CONSTANT)
1486 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1487 else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
1488 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1489 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1490 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1491
1492 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001493 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001494 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001495 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001496 }
1497
Antonio Ospite569b10a2010-10-19 16:13:10 +02001498 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1499 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
Antonio Ospite816651a2010-05-03 22:15:55 +02001500 ret = sixaxis_set_operational_usb(hdev);
Frank Praznikcad665a2014-01-11 15:13:54 -05001501 INIT_WORK(&sc->state_worker, sixaxis_state_worker);
Antonio Ospite569b10a2010-10-19 16:13:10 +02001502 }
Antonio Ospite816651a2010-05-03 22:15:55 +02001503 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1504 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik8ab16762014-01-16 21:42:31 -05001505 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznike5606232014-01-27 10:17:37 -05001506 /* The Dualshock 4 touchpad supports 2 touches and has a
1507 * resolution of 1920x940.
1508 */
1509 ret = sony_register_touchpad(sc, 2, 1920, 940);
1510 if (ret < 0)
1511 goto err_stop;
1512
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001513 INIT_WORK(&sc->state_worker, dualshock4_state_worker);
1514 } else {
1515 ret = 0;
1516 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001517
Jiri Kosina4dfdc462008-12-30 00:49:59 +01001518 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001519 goto err_stop;
1520
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001521 if (sc->quirks & SONY_LED_SUPPORT) {
1522 ret = sony_leds_init(hdev);
1523 if (ret < 0)
1524 goto err_stop;
1525 }
1526
Frank Praznikd902f472014-01-27 10:17:36 -05001527 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1528 ret = sony_battery_probe(sc);
1529 if (ret < 0)
1530 goto err_stop;
1531
1532 /* Open the device to receive reports with battery info */
1533 ret = hid_hw_open(hdev);
1534 if (ret < 0) {
1535 hid_err(hdev, "hw open failed\n");
1536 goto err_stop;
1537 }
1538 }
1539
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001540 ret = sony_init_ff(hdev);
1541 if (ret < 0)
Frank Praznikd902f472014-01-27 10:17:36 -05001542 goto err_close;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001543
Jiri Slabybd28ce02008-06-25 23:47:04 +02001544 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001545err_close:
1546 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001547err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001548 if (sc->quirks & SONY_LED_SUPPORT)
1549 sony_leds_remove(hdev);
Frank Praznikd902f472014-01-27 10:17:36 -05001550 if (sc->quirks & SONY_BATTERY_SUPPORT)
1551 sony_battery_remove(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001552 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001553 return ret;
1554}
1555
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001556static void sony_remove(struct hid_device *hdev)
1557{
Colin Leitnerf04d5142013-05-27 23:41:05 +02001558 struct sony_sc *sc = hid_get_drvdata(hdev);
1559
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001560 if (sc->quirks & SONY_LED_SUPPORT)
Sven Eckelmannc5382512013-11-19 20:26:30 +01001561 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001562
Frank Praznikd902f472014-01-27 10:17:36 -05001563 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1564 hid_hw_close(hdev);
1565 sony_battery_remove(sc);
1566 }
1567
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001568 sony_destroy_ff(hdev);
1569
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001570 hid_hw_stop(hdev);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001571}
1572
Jiri Slabybd28ce02008-06-25 23:47:04 +02001573static const struct hid_device_id sony_devices[] = {
Antonio Ospite816651a2010-05-03 22:15:55 +02001574 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1575 .driver_data = SIXAXIS_CONTROLLER_USB },
Jiri Kosina35dca5b2011-04-28 15:43:13 +02001576 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
1577 .driver_data = SIXAXIS_CONTROLLER_USB },
Antonio Ospite816651a2010-05-03 22:15:55 +02001578 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1579 .driver_data = SIXAXIS_CONTROLLER_BT },
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001580 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
1581 .driver_data = VAIO_RDESC_CONSTANT },
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +09001582 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
1583 .driver_data = VAIO_RDESC_CONSTANT },
Colin Leitnerf04d5142013-05-27 23:41:05 +02001584 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
1585 * Logitech joystick from the device descriptor. */
1586 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
1587 .driver_data = BUZZ_CONTROLLER },
1588 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
1589 .driver_data = BUZZ_CONTROLLER },
Jiri Kosina078328d2013-06-13 12:03:49 +02001590 /* PS3 BD Remote Control */
1591 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
1592 .driver_data = PS3REMOTE },
1593 /* Logitech Harmony Adapter for PS3 */
1594 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
1595 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001596 /* Sony Dualshock 4 controllers for PS4 */
1597 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001598 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001599 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001600 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02001601 { }
1602};
1603MODULE_DEVICE_TABLE(hid, sony_devices);
1604
1605static struct hid_driver sony_driver = {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001606 .name = "sony",
1607 .id_table = sony_devices,
1608 .input_mapping = sony_mapping,
1609 .probe = sony_probe,
1610 .remove = sony_remove,
1611 .report_fixup = sony_report_fixup,
1612 .raw_event = sony_raw_event
Jiri Slabybd28ce02008-06-25 23:47:04 +02001613};
H Hartley Sweetenf4254582012-12-17 15:28:26 -07001614module_hid_driver(sony_driver);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001615
Jiri Slabybd28ce02008-06-25 23:47:04 +02001616MODULE_LICENSE("GPL");