blob: 28dba6c4c2732658083dbe1dd98df49169b911c2 [file] [log] [blame]
Jiri Slabybd28ce02008-06-25 23:47:04 +02001/*
Frank Praznik077147a2014-09-14 11:56:39 -04002 * HID driver for Sony / PS2 / PS3 / PS4 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>
Frank Praznik077147a2014-09-14 11:56:39 -040011 * Copyright (c) 2014 Frank Praznik <frank.praznik@gmail.com>
Jiri Slabybd28ce02008-06-25 23:47:04 +020012 */
13
14/*
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 2 of the License, or (at your option)
18 * any later version.
19 */
20
Frank Praznikad142b92014-02-20 11:36:00 -050021/*
22 * NOTE: in order for the Sony PS3 BD Remote Control to be found by
Jiri Kosina078328d2013-06-13 12:03:49 +020023 * a Bluetooth host, the key combination Start+Enter has to be kept pressed
24 * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
25 *
26 * There will be no PIN request from the device.
27 */
28
Jiri Slabybd28ce02008-06-25 23:47:04 +020029#include <linux/device.h>
30#include <linux/hid.h>
31#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jiri Kosina40e32ee2013-05-28 11:22:09 +020033#include <linux/leds.h>
Frank Praznikd902f472014-01-27 10:17:36 -050034#include <linux/power_supply.h>
35#include <linux/spinlock.h>
Frank Praznikd2d782f2014-02-20 11:36:03 -050036#include <linux/list.h>
Frank Praznik80250872014-04-14 10:11:35 -040037#include <linux/idr.h>
Frank Praznike5606232014-01-27 10:17:37 -050038#include <linux/input/mt.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020039
40#include "hid-ids.h"
41
Frank Praznik6c79c182014-01-16 21:43:03 -050042#define VAIO_RDESC_CONSTANT BIT(0)
43#define SIXAXIS_CONTROLLER_USB BIT(1)
44#define SIXAXIS_CONTROLLER_BT BIT(2)
45#define BUZZ_CONTROLLER BIT(3)
46#define PS3REMOTE BIT(4)
Frank Praznik8ab16762014-01-16 21:42:31 -050047#define DUALSHOCK4_CONTROLLER_USB BIT(5)
48#define DUALSHOCK4_CONTROLLER_BT BIT(6)
Simon Woodb3bca322015-06-09 21:27:04 -060049#define MOTION_CONTROLLER_USB BIT(7)
50#define MOTION_CONTROLLER_BT BIT(8)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020051
Frank Praznikfee4e2d2014-02-18 17:22:01 -050052#define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
Simon Woodb3bca322015-06-09 21:27:04 -060053#define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
Frank Praznik68330d82014-02-05 20:03:49 -050054#define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
55 DUALSHOCK4_CONTROLLER_BT)
Frank Praznikfee4e2d2014-02-18 17:22:01 -050056#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040057 DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER)
Frank Praznikfee4e2d2014-02-18 17:22:01 -050058#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040059#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
60 MOTION_CONTROLLER)
Frank Praznik60781cf2014-01-11 15:13:15 -050061
62#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010063
Frank Praznik4c3e8292015-05-05 20:47:33 -040064/*
65 * The Sixaxis reports both digital and analog values for each button on the
66 * controller except for Start, Select and the PS button. The controller ends
67 * up reporting 27 axes which causes them to spill over into the multi-touch
68 * axis values. Additionally, the controller only has 20 actual, physical axes
69 * so there are several unused axes in between the used ones.
70 */
Antonio Ospitec607fb82014-06-24 13:28:41 +020071static __u8 sixaxis_rdesc[] = {
Antonio Ospitefb705a62014-06-24 13:28:42 +020072 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznik4c3e8292015-05-05 20:47:33 -040073 0x09, 0x04, /* Usage (Joystick), */
Antonio Ospitefb705a62014-06-24 13:28:42 +020074 0xA1, 0x01, /* Collection (Application), */
75 0xA1, 0x02, /* Collection (Logical), */
76 0x85, 0x01, /* Report ID (1), */
77 0x75, 0x08, /* Report Size (8), */
78 0x95, 0x01, /* Report Count (1), */
79 0x15, 0x00, /* Logical Minimum (0), */
80 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
81 0x81, 0x03, /* Input (Constant, Variable), */
82 0x75, 0x01, /* Report Size (1), */
83 0x95, 0x13, /* Report Count (19), */
84 0x15, 0x00, /* Logical Minimum (0), */
85 0x25, 0x01, /* Logical Maximum (1), */
86 0x35, 0x00, /* Physical Minimum (0), */
87 0x45, 0x01, /* Physical Maximum (1), */
88 0x05, 0x09, /* Usage Page (Button), */
89 0x19, 0x01, /* Usage Minimum (01h), */
90 0x29, 0x13, /* Usage Maximum (13h), */
91 0x81, 0x02, /* Input (Variable), */
92 0x75, 0x01, /* Report Size (1), */
93 0x95, 0x0D, /* Report Count (13), */
94 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
95 0x81, 0x03, /* Input (Constant, Variable), */
96 0x15, 0x00, /* Logical Minimum (0), */
97 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
98 0x05, 0x01, /* Usage Page (Desktop), */
99 0x09, 0x01, /* Usage (Pointer), */
100 0xA1, 0x00, /* Collection (Physical), */
101 0x75, 0x08, /* Report Size (8), */
102 0x95, 0x04, /* Report Count (4), */
103 0x35, 0x00, /* Physical Minimum (0), */
104 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
105 0x09, 0x30, /* Usage (X), */
106 0x09, 0x31, /* Usage (Y), */
107 0x09, 0x32, /* Usage (Z), */
108 0x09, 0x35, /* Usage (Rz), */
109 0x81, 0x02, /* Input (Variable), */
110 0xC0, /* End Collection, */
111 0x05, 0x01, /* Usage Page (Desktop), */
112 0x95, 0x13, /* Report Count (19), */
113 0x09, 0x01, /* Usage (Pointer), */
114 0x81, 0x02, /* Input (Variable), */
115 0x95, 0x0C, /* Report Count (12), */
116 0x81, 0x01, /* Input (Constant), */
117 0x75, 0x10, /* Report Size (16), */
118 0x95, 0x04, /* Report Count (4), */
119 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
120 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
121 0x09, 0x01, /* Usage (Pointer), */
122 0x81, 0x02, /* Input (Variable), */
123 0xC0, /* End Collection, */
124 0xA1, 0x02, /* Collection (Logical), */
125 0x85, 0x02, /* Report ID (2), */
126 0x75, 0x08, /* Report Size (8), */
127 0x95, 0x30, /* Report Count (48), */
128 0x09, 0x01, /* Usage (Pointer), */
129 0xB1, 0x02, /* Feature (Variable), */
130 0xC0, /* End Collection, */
131 0xA1, 0x02, /* Collection (Logical), */
132 0x85, 0xEE, /* Report ID (238), */
133 0x75, 0x08, /* Report Size (8), */
134 0x95, 0x30, /* Report Count (48), */
135 0x09, 0x01, /* Usage (Pointer), */
136 0xB1, 0x02, /* Feature (Variable), */
137 0xC0, /* End Collection, */
138 0xA1, 0x02, /* Collection (Logical), */
139 0x85, 0xEF, /* Report ID (239), */
140 0x75, 0x08, /* Report Size (8), */
141 0x95, 0x30, /* Report Count (48), */
142 0x09, 0x01, /* Usage (Pointer), */
143 0xB1, 0x02, /* Feature (Variable), */
144 0xC0, /* End Collection, */
145 0xC0 /* End Collection */
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200146};
147
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400148/* PS/3 Motion controller */
149static __u8 motion_rdesc[] = {
150 0x05, 0x01, /* Usage Page (Desktop), */
151 0x09, 0x04, /* Usage (Joystick), */
152 0xA1, 0x01, /* Collection (Application), */
153 0xA1, 0x02, /* Collection (Logical), */
154 0x85, 0x01, /* Report ID (1), */
155 0x75, 0x08, /* Report Size (8), */
156 0x95, 0x01, /* Report Count (1), */
157 0x15, 0x00, /* Logical Minimum (0), */
158 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
159 0x81, 0x03, /* Input (Constant, Variable), */
160 0x75, 0x01, /* Report Size (1), */
161 0x95, 0x13, /* Report Count (19), */
162 0x15, 0x00, /* Logical Minimum (0), */
163 0x25, 0x01, /* Logical Maximum (1), */
164 0x35, 0x00, /* Physical Minimum (0), */
165 0x45, 0x01, /* Physical Maximum (1), */
166 0x05, 0x09, /* Usage Page (Button), */
167 0x19, 0x01, /* Usage Minimum (01h), */
168 0x29, 0x13, /* Usage Maximum (13h), */
169 0x81, 0x02, /* Input (Variable), */
170 0x75, 0x01, /* Report Size (1), */
171 0x95, 0x0D, /* Report Count (13), */
172 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
173 0x81, 0x03, /* Input (Constant, Variable), */
174 0x15, 0x00, /* Logical Minimum (0), */
175 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
176 0x05, 0x01, /* Usage Page (Desktop), */
177 0x09, 0x01, /* Usage (Pointer), */
178 0xA1, 0x00, /* Collection (Physical), */
179 0x75, 0x08, /* Report Size (8), */
180 0x95, 0x04, /* Report Count (4), */
181 0x35, 0x00, /* Physical Minimum (0), */
182 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
183 0x09, 0x30, /* Usage (X), */
184 0x09, 0x31, /* Usage (Y), */
185 0x09, 0x32, /* Usage (Z), */
186 0x09, 0x35, /* Usage (Rz), */
187 0x81, 0x02, /* Input (Variable), */
188 0xC0, /* End Collection, */
189 0x05, 0x01, /* Usage Page (Desktop), */
190 0x95, 0x13, /* Report Count (19), */
191 0x09, 0x01, /* Usage (Pointer), */
192 0x81, 0x02, /* Input (Variable), */
193 0x95, 0x0C, /* Report Count (12), */
194 0x81, 0x01, /* Input (Constant), */
195 0x75, 0x10, /* Report Size (16), */
196 0x95, 0x04, /* Report Count (4), */
197 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
198 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
199 0x09, 0x01, /* Usage (Pointer), */
200 0x81, 0x02, /* Input (Variable), */
201 0xC0, /* End Collection, */
202 0xA1, 0x02, /* Collection (Logical), */
203 0x85, 0x02, /* Report ID (2), */
204 0x75, 0x08, /* Report Size (8), */
205 0x95, 0x30, /* Report Count (48), */
206 0x09, 0x01, /* Usage (Pointer), */
207 0xB1, 0x02, /* Feature (Variable), */
208 0xC0, /* End Collection, */
209 0xA1, 0x02, /* Collection (Logical), */
210 0x85, 0xEE, /* Report ID (238), */
211 0x75, 0x08, /* Report Size (8), */
212 0x95, 0x30, /* Report Count (48), */
213 0x09, 0x01, /* Usage (Pointer), */
214 0xB1, 0x02, /* Feature (Variable), */
215 0xC0, /* End Collection, */
216 0xA1, 0x02, /* Collection (Logical), */
217 0x85, 0xEF, /* Report ID (239), */
218 0x75, 0x08, /* Report Size (8), */
219 0x95, 0x30, /* Report Count (48), */
220 0x09, 0x01, /* Usage (Pointer), */
221 0xB1, 0x02, /* Feature (Variable), */
222 0xC0, /* End Collection, */
223 0xC0 /* End Collection */
224};
225
226
Frank Praznikad142b92014-02-20 11:36:00 -0500227/*
228 * The default descriptor doesn't provide mapping for the accelerometers
Frank Praznik58d70272014-01-20 12:27:01 -0500229 * or orientation sensors. This fixed descriptor maps the accelerometers
230 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
231 * to usage values 0x43, 0x44 and 0x45.
232 */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500233static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -0500234 0x05, 0x01, /* Usage Page (Desktop), */
235 0x09, 0x05, /* Usage (Gamepad), */
236 0xA1, 0x01, /* Collection (Application), */
237 0x85, 0x01, /* Report ID (1), */
238 0x09, 0x30, /* Usage (X), */
239 0x09, 0x31, /* Usage (Y), */
240 0x09, 0x32, /* Usage (Z), */
241 0x09, 0x35, /* Usage (Rz), */
242 0x15, 0x00, /* Logical Minimum (0), */
243 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
244 0x75, 0x08, /* Report Size (8), */
245 0x95, 0x04, /* Report Count (4), */
246 0x81, 0x02, /* Input (Variable), */
247 0x09, 0x39, /* Usage (Hat Switch), */
248 0x15, 0x00, /* Logical Minimum (0), */
249 0x25, 0x07, /* Logical Maximum (7), */
250 0x35, 0x00, /* Physical Minimum (0), */
251 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
252 0x65, 0x14, /* Unit (Degrees), */
253 0x75, 0x04, /* Report Size (4), */
254 0x95, 0x01, /* Report Count (1), */
255 0x81, 0x42, /* Input (Variable, Null State), */
256 0x65, 0x00, /* Unit, */
257 0x05, 0x09, /* Usage Page (Button), */
258 0x19, 0x01, /* Usage Minimum (01h), */
259 0x29, 0x0E, /* Usage Maximum (0Eh), */
260 0x15, 0x00, /* Logical Minimum (0), */
261 0x25, 0x01, /* Logical Maximum (1), */
262 0x75, 0x01, /* Report Size (1), */
263 0x95, 0x0E, /* Report Count (14), */
264 0x81, 0x02, /* Input (Variable), */
265 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
266 0x09, 0x20, /* Usage (20h), */
267 0x75, 0x06, /* Report Size (6), */
268 0x95, 0x01, /* Report Count (1), */
269 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400270 0x25, 0x3F, /* Logical Maximum (63), */
Frank Praznik58d70272014-01-20 12:27:01 -0500271 0x81, 0x02, /* Input (Variable), */
272 0x05, 0x01, /* Usage Page (Desktop), */
273 0x09, 0x33, /* Usage (Rx), */
274 0x09, 0x34, /* Usage (Ry), */
275 0x15, 0x00, /* Logical Minimum (0), */
276 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
277 0x75, 0x08, /* Report Size (8), */
278 0x95, 0x02, /* Report Count (2), */
279 0x81, 0x02, /* Input (Variable), */
280 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
281 0x09, 0x21, /* Usage (21h), */
282 0x95, 0x03, /* Report Count (3), */
283 0x81, 0x02, /* Input (Variable), */
284 0x05, 0x01, /* Usage Page (Desktop), */
285 0x19, 0x40, /* Usage Minimum (40h), */
286 0x29, 0x42, /* Usage Maximum (42h), */
287 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
288 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
289 0x75, 0x10, /* Report Size (16), */
290 0x95, 0x03, /* Report Count (3), */
291 0x81, 0x02, /* Input (Variable), */
292 0x19, 0x43, /* Usage Minimum (43h), */
293 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400294 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
295 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznik58d70272014-01-20 12:27:01 -0500296 0x95, 0x03, /* Report Count (3), */
297 0x81, 0x02, /* Input (Variable), */
298 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
299 0x09, 0x21, /* Usage (21h), */
300 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400301 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
Frank Praznik58d70272014-01-20 12:27:01 -0500302 0x75, 0x08, /* Report Size (8), */
303 0x95, 0x27, /* Report Count (39), */
304 0x81, 0x02, /* Input (Variable), */
305 0x85, 0x05, /* Report ID (5), */
306 0x09, 0x22, /* Usage (22h), */
307 0x95, 0x1F, /* Report Count (31), */
308 0x91, 0x02, /* Output (Variable), */
309 0x85, 0x04, /* Report ID (4), */
310 0x09, 0x23, /* Usage (23h), */
311 0x95, 0x24, /* Report Count (36), */
312 0xB1, 0x02, /* Feature (Variable), */
313 0x85, 0x02, /* Report ID (2), */
314 0x09, 0x24, /* Usage (24h), */
315 0x95, 0x24, /* Report Count (36), */
316 0xB1, 0x02, /* Feature (Variable), */
317 0x85, 0x08, /* Report ID (8), */
318 0x09, 0x25, /* Usage (25h), */
319 0x95, 0x03, /* Report Count (3), */
320 0xB1, 0x02, /* Feature (Variable), */
321 0x85, 0x10, /* Report ID (16), */
322 0x09, 0x26, /* Usage (26h), */
323 0x95, 0x04, /* Report Count (4), */
324 0xB1, 0x02, /* Feature (Variable), */
325 0x85, 0x11, /* Report ID (17), */
326 0x09, 0x27, /* Usage (27h), */
327 0x95, 0x02, /* Report Count (2), */
328 0xB1, 0x02, /* Feature (Variable), */
329 0x85, 0x12, /* Report ID (18), */
330 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
331 0x09, 0x21, /* Usage (21h), */
332 0x95, 0x0F, /* Report Count (15), */
333 0xB1, 0x02, /* Feature (Variable), */
334 0x85, 0x13, /* Report ID (19), */
335 0x09, 0x22, /* Usage (22h), */
336 0x95, 0x16, /* Report Count (22), */
337 0xB1, 0x02, /* Feature (Variable), */
338 0x85, 0x14, /* Report ID (20), */
339 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
340 0x09, 0x20, /* Usage (20h), */
341 0x95, 0x10, /* Report Count (16), */
342 0xB1, 0x02, /* Feature (Variable), */
343 0x85, 0x15, /* Report ID (21), */
344 0x09, 0x21, /* Usage (21h), */
345 0x95, 0x2C, /* Report Count (44), */
346 0xB1, 0x02, /* Feature (Variable), */
347 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
348 0x85, 0x80, /* Report ID (128), */
349 0x09, 0x20, /* Usage (20h), */
350 0x95, 0x06, /* Report Count (6), */
351 0xB1, 0x02, /* Feature (Variable), */
352 0x85, 0x81, /* Report ID (129), */
353 0x09, 0x21, /* Usage (21h), */
354 0x95, 0x06, /* Report Count (6), */
355 0xB1, 0x02, /* Feature (Variable), */
356 0x85, 0x82, /* Report ID (130), */
357 0x09, 0x22, /* Usage (22h), */
358 0x95, 0x05, /* Report Count (5), */
359 0xB1, 0x02, /* Feature (Variable), */
360 0x85, 0x83, /* Report ID (131), */
361 0x09, 0x23, /* Usage (23h), */
362 0x95, 0x01, /* Report Count (1), */
363 0xB1, 0x02, /* Feature (Variable), */
364 0x85, 0x84, /* Report ID (132), */
365 0x09, 0x24, /* Usage (24h), */
366 0x95, 0x04, /* Report Count (4), */
367 0xB1, 0x02, /* Feature (Variable), */
368 0x85, 0x85, /* Report ID (133), */
369 0x09, 0x25, /* Usage (25h), */
370 0x95, 0x06, /* Report Count (6), */
371 0xB1, 0x02, /* Feature (Variable), */
372 0x85, 0x86, /* Report ID (134), */
373 0x09, 0x26, /* Usage (26h), */
374 0x95, 0x06, /* Report Count (6), */
375 0xB1, 0x02, /* Feature (Variable), */
376 0x85, 0x87, /* Report ID (135), */
377 0x09, 0x27, /* Usage (27h), */
378 0x95, 0x23, /* Report Count (35), */
379 0xB1, 0x02, /* Feature (Variable), */
380 0x85, 0x88, /* Report ID (136), */
381 0x09, 0x28, /* Usage (28h), */
382 0x95, 0x22, /* Report Count (34), */
383 0xB1, 0x02, /* Feature (Variable), */
384 0x85, 0x89, /* Report ID (137), */
385 0x09, 0x29, /* Usage (29h), */
386 0x95, 0x02, /* Report Count (2), */
387 0xB1, 0x02, /* Feature (Variable), */
388 0x85, 0x90, /* Report ID (144), */
389 0x09, 0x30, /* Usage (30h), */
390 0x95, 0x05, /* Report Count (5), */
391 0xB1, 0x02, /* Feature (Variable), */
392 0x85, 0x91, /* Report ID (145), */
393 0x09, 0x31, /* Usage (31h), */
394 0x95, 0x03, /* Report Count (3), */
395 0xB1, 0x02, /* Feature (Variable), */
396 0x85, 0x92, /* Report ID (146), */
397 0x09, 0x32, /* Usage (32h), */
398 0x95, 0x03, /* Report Count (3), */
399 0xB1, 0x02, /* Feature (Variable), */
400 0x85, 0x93, /* Report ID (147), */
401 0x09, 0x33, /* Usage (33h), */
402 0x95, 0x0C, /* Report Count (12), */
403 0xB1, 0x02, /* Feature (Variable), */
404 0x85, 0xA0, /* Report ID (160), */
405 0x09, 0x40, /* Usage (40h), */
406 0x95, 0x06, /* Report Count (6), */
407 0xB1, 0x02, /* Feature (Variable), */
408 0x85, 0xA1, /* Report ID (161), */
409 0x09, 0x41, /* Usage (41h), */
410 0x95, 0x01, /* Report Count (1), */
411 0xB1, 0x02, /* Feature (Variable), */
412 0x85, 0xA2, /* Report ID (162), */
413 0x09, 0x42, /* Usage (42h), */
414 0x95, 0x01, /* Report Count (1), */
415 0xB1, 0x02, /* Feature (Variable), */
416 0x85, 0xA3, /* Report ID (163), */
417 0x09, 0x43, /* Usage (43h), */
418 0x95, 0x30, /* Report Count (48), */
419 0xB1, 0x02, /* Feature (Variable), */
420 0x85, 0xA4, /* Report ID (164), */
421 0x09, 0x44, /* Usage (44h), */
422 0x95, 0x0D, /* Report Count (13), */
423 0xB1, 0x02, /* Feature (Variable), */
424 0x85, 0xA5, /* Report ID (165), */
425 0x09, 0x45, /* Usage (45h), */
426 0x95, 0x15, /* Report Count (21), */
427 0xB1, 0x02, /* Feature (Variable), */
428 0x85, 0xA6, /* Report ID (166), */
429 0x09, 0x46, /* Usage (46h), */
430 0x95, 0x15, /* Report Count (21), */
431 0xB1, 0x02, /* Feature (Variable), */
432 0x85, 0xF0, /* Report ID (240), */
433 0x09, 0x47, /* Usage (47h), */
434 0x95, 0x3F, /* Report Count (63), */
435 0xB1, 0x02, /* Feature (Variable), */
436 0x85, 0xF1, /* Report ID (241), */
437 0x09, 0x48, /* Usage (48h), */
438 0x95, 0x3F, /* Report Count (63), */
439 0xB1, 0x02, /* Feature (Variable), */
440 0x85, 0xF2, /* Report ID (242), */
441 0x09, 0x49, /* Usage (49h), */
442 0x95, 0x0F, /* Report Count (15), */
443 0xB1, 0x02, /* Feature (Variable), */
444 0x85, 0xA7, /* Report ID (167), */
445 0x09, 0x4A, /* Usage (4Ah), */
446 0x95, 0x01, /* Report Count (1), */
447 0xB1, 0x02, /* Feature (Variable), */
448 0x85, 0xA8, /* Report ID (168), */
449 0x09, 0x4B, /* Usage (4Bh), */
450 0x95, 0x01, /* Report Count (1), */
451 0xB1, 0x02, /* Feature (Variable), */
452 0x85, 0xA9, /* Report ID (169), */
453 0x09, 0x4C, /* Usage (4Ch), */
454 0x95, 0x08, /* Report Count (8), */
455 0xB1, 0x02, /* Feature (Variable), */
456 0x85, 0xAA, /* Report ID (170), */
457 0x09, 0x4E, /* Usage (4Eh), */
458 0x95, 0x01, /* Report Count (1), */
459 0xB1, 0x02, /* Feature (Variable), */
460 0x85, 0xAB, /* Report ID (171), */
461 0x09, 0x4F, /* Usage (4Fh), */
462 0x95, 0x39, /* Report Count (57), */
463 0xB1, 0x02, /* Feature (Variable), */
464 0x85, 0xAC, /* Report ID (172), */
465 0x09, 0x50, /* Usage (50h), */
466 0x95, 0x39, /* Report Count (57), */
467 0xB1, 0x02, /* Feature (Variable), */
468 0x85, 0xAD, /* Report ID (173), */
469 0x09, 0x51, /* Usage (51h), */
470 0x95, 0x0B, /* Report Count (11), */
471 0xB1, 0x02, /* Feature (Variable), */
472 0x85, 0xAE, /* Report ID (174), */
473 0x09, 0x52, /* Usage (52h), */
474 0x95, 0x01, /* Report Count (1), */
475 0xB1, 0x02, /* Feature (Variable), */
476 0x85, 0xAF, /* Report ID (175), */
477 0x09, 0x53, /* Usage (53h), */
478 0x95, 0x02, /* Report Count (2), */
479 0xB1, 0x02, /* Feature (Variable), */
480 0x85, 0xB0, /* Report ID (176), */
481 0x09, 0x54, /* Usage (54h), */
482 0x95, 0x3F, /* Report Count (63), */
483 0xB1, 0x02, /* Feature (Variable), */
484 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500485};
486
Frank Praznikad142b92014-02-20 11:36:00 -0500487/*
488 * The default behavior of the Dualshock 4 is to send reports using report
Frank Praznik077147a2014-09-14 11:56:39 -0400489 * type 1 when running over Bluetooth. However, when feature report 2 is
490 * requested during the controller initialization it starts sending input
491 * reports in report 17. Since report 17 is undefined in the default HID
Frank Praznikd8296742014-02-05 20:03:45 -0500492 * descriptor the button and axis definitions must be moved to report 17 or
Frank Praznik077147a2014-09-14 11:56:39 -0400493 * the HID layer won't process the received input.
Frank Praznikd8296742014-02-05 20:03:45 -0500494 */
495static u8 dualshock4_bt_rdesc[] = {
496 0x05, 0x01, /* Usage Page (Desktop), */
497 0x09, 0x05, /* Usage (Gamepad), */
498 0xA1, 0x01, /* Collection (Application), */
499 0x85, 0x01, /* Report ID (1), */
500 0x75, 0x08, /* Report Size (8), */
501 0x95, 0x0A, /* Report Count (9), */
502 0x81, 0x02, /* Input (Variable), */
503 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
504 0x85, 0x02, /* Report ID (2), */
505 0x09, 0x24, /* Usage (24h), */
506 0x95, 0x24, /* Report Count (36), */
507 0xB1, 0x02, /* Feature (Variable), */
508 0x85, 0xA3, /* Report ID (163), */
509 0x09, 0x25, /* Usage (25h), */
510 0x95, 0x30, /* Report Count (48), */
511 0xB1, 0x02, /* Feature (Variable), */
512 0x85, 0x05, /* Report ID (5), */
513 0x09, 0x26, /* Usage (26h), */
514 0x95, 0x28, /* Report Count (40), */
515 0xB1, 0x02, /* Feature (Variable), */
516 0x85, 0x06, /* Report ID (6), */
517 0x09, 0x27, /* Usage (27h), */
518 0x95, 0x34, /* Report Count (52), */
519 0xB1, 0x02, /* Feature (Variable), */
520 0x85, 0x07, /* Report ID (7), */
521 0x09, 0x28, /* Usage (28h), */
522 0x95, 0x30, /* Report Count (48), */
523 0xB1, 0x02, /* Feature (Variable), */
524 0x85, 0x08, /* Report ID (8), */
525 0x09, 0x29, /* Usage (29h), */
526 0x95, 0x2F, /* Report Count (47), */
527 0xB1, 0x02, /* Feature (Variable), */
528 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
529 0x85, 0x03, /* Report ID (3), */
530 0x09, 0x21, /* Usage (21h), */
531 0x95, 0x26, /* Report Count (38), */
532 0xB1, 0x02, /* Feature (Variable), */
533 0x85, 0x04, /* Report ID (4), */
534 0x09, 0x22, /* Usage (22h), */
535 0x95, 0x2E, /* Report Count (46), */
536 0xB1, 0x02, /* Feature (Variable), */
537 0x85, 0xF0, /* Report ID (240), */
538 0x09, 0x47, /* Usage (47h), */
539 0x95, 0x3F, /* Report Count (63), */
540 0xB1, 0x02, /* Feature (Variable), */
541 0x85, 0xF1, /* Report ID (241), */
542 0x09, 0x48, /* Usage (48h), */
543 0x95, 0x3F, /* Report Count (63), */
544 0xB1, 0x02, /* Feature (Variable), */
545 0x85, 0xF2, /* Report ID (242), */
546 0x09, 0x49, /* Usage (49h), */
547 0x95, 0x0F, /* Report Count (15), */
548 0xB1, 0x02, /* Feature (Variable), */
549 0x85, 0x11, /* Report ID (17), */
550 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
551 0x09, 0x20, /* Usage (20h), */
552 0x95, 0x02, /* Report Count (2), */
553 0x81, 0x02, /* Input (Variable), */
554 0x05, 0x01, /* Usage Page (Desktop), */
555 0x09, 0x30, /* Usage (X), */
556 0x09, 0x31, /* Usage (Y), */
557 0x09, 0x32, /* Usage (Z), */
558 0x09, 0x35, /* Usage (Rz), */
559 0x15, 0x00, /* Logical Minimum (0), */
560 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
561 0x75, 0x08, /* Report Size (8), */
562 0x95, 0x04, /* Report Count (4), */
563 0x81, 0x02, /* Input (Variable), */
564 0x09, 0x39, /* Usage (Hat Switch), */
565 0x15, 0x00, /* Logical Minimum (0), */
566 0x25, 0x07, /* Logical Maximum (7), */
567 0x75, 0x04, /* Report Size (4), */
568 0x95, 0x01, /* Report Count (1), */
569 0x81, 0x42, /* Input (Variable, Null State), */
570 0x05, 0x09, /* Usage Page (Button), */
571 0x19, 0x01, /* Usage Minimum (01h), */
572 0x29, 0x0E, /* Usage Maximum (0Eh), */
573 0x15, 0x00, /* Logical Minimum (0), */
574 0x25, 0x01, /* Logical Maximum (1), */
575 0x75, 0x01, /* Report Size (1), */
576 0x95, 0x0E, /* Report Count (14), */
577 0x81, 0x02, /* Input (Variable), */
578 0x75, 0x06, /* Report Size (6), */
579 0x95, 0x01, /* Report Count (1), */
580 0x81, 0x01, /* Input (Constant), */
581 0x05, 0x01, /* Usage Page (Desktop), */
582 0x09, 0x33, /* Usage (Rx), */
583 0x09, 0x34, /* Usage (Ry), */
584 0x15, 0x00, /* Logical Minimum (0), */
585 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
586 0x75, 0x08, /* Report Size (8), */
587 0x95, 0x02, /* Report Count (2), */
588 0x81, 0x02, /* Input (Variable), */
589 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
590 0x09, 0x20, /* Usage (20h), */
591 0x95, 0x03, /* Report Count (3), */
592 0x81, 0x02, /* Input (Variable), */
593 0x05, 0x01, /* Usage Page (Desktop), */
594 0x19, 0x40, /* Usage Minimum (40h), */
595 0x29, 0x42, /* Usage Maximum (42h), */
596 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
597 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
598 0x75, 0x10, /* Report Size (16), */
599 0x95, 0x03, /* Report Count (3), */
600 0x81, 0x02, /* Input (Variable), */
601 0x19, 0x43, /* Usage Minimum (43h), */
602 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400603 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
604 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznikd8296742014-02-05 20:03:45 -0500605 0x95, 0x03, /* Report Count (3), */
606 0x81, 0x02, /* Input (Variable), */
607 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
608 0x09, 0x20, /* Usage (20h), */
609 0x15, 0x00, /* Logical Minimum (0), */
610 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
611 0x75, 0x08, /* Report Size (8), */
612 0x95, 0x31, /* Report Count (51), */
613 0x81, 0x02, /* Input (Variable), */
614 0x09, 0x21, /* Usage (21h), */
615 0x75, 0x08, /* Report Size (8), */
616 0x95, 0x4D, /* Report Count (77), */
617 0x91, 0x02, /* Output (Variable), */
618 0x85, 0x12, /* Report ID (18), */
619 0x09, 0x22, /* Usage (22h), */
620 0x95, 0x8D, /* Report Count (141), */
621 0x81, 0x02, /* Input (Variable), */
622 0x09, 0x23, /* Usage (23h), */
623 0x91, 0x02, /* Output (Variable), */
624 0x85, 0x13, /* Report ID (19), */
625 0x09, 0x24, /* Usage (24h), */
626 0x95, 0xCD, /* Report Count (205), */
627 0x81, 0x02, /* Input (Variable), */
628 0x09, 0x25, /* Usage (25h), */
629 0x91, 0x02, /* Output (Variable), */
630 0x85, 0x14, /* Report ID (20), */
631 0x09, 0x26, /* Usage (26h), */
632 0x96, 0x0D, 0x01, /* Report Count (269), */
633 0x81, 0x02, /* Input (Variable), */
634 0x09, 0x27, /* Usage (27h), */
635 0x91, 0x02, /* Output (Variable), */
636 0x85, 0x15, /* Report ID (21), */
637 0x09, 0x28, /* Usage (28h), */
638 0x96, 0x4D, 0x01, /* Report Count (333), */
639 0x81, 0x02, /* Input (Variable), */
640 0x09, 0x29, /* Usage (29h), */
641 0x91, 0x02, /* Output (Variable), */
642 0x85, 0x16, /* Report ID (22), */
643 0x09, 0x2A, /* Usage (2Ah), */
644 0x96, 0x8D, 0x01, /* Report Count (397), */
645 0x81, 0x02, /* Input (Variable), */
646 0x09, 0x2B, /* Usage (2Bh), */
647 0x91, 0x02, /* Output (Variable), */
648 0x85, 0x17, /* Report ID (23), */
649 0x09, 0x2C, /* Usage (2Ch), */
650 0x96, 0xCD, 0x01, /* Report Count (461), */
651 0x81, 0x02, /* Input (Variable), */
652 0x09, 0x2D, /* Usage (2Dh), */
653 0x91, 0x02, /* Output (Variable), */
654 0x85, 0x18, /* Report ID (24), */
655 0x09, 0x2E, /* Usage (2Eh), */
656 0x96, 0x0D, 0x02, /* Report Count (525), */
657 0x81, 0x02, /* Input (Variable), */
658 0x09, 0x2F, /* Usage (2Fh), */
659 0x91, 0x02, /* Output (Variable), */
660 0x85, 0x19, /* Report ID (25), */
661 0x09, 0x30, /* Usage (30h), */
662 0x96, 0x22, 0x02, /* Report Count (546), */
663 0x81, 0x02, /* Input (Variable), */
664 0x09, 0x31, /* Usage (31h), */
665 0x91, 0x02, /* Output (Variable), */
666 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
667 0x85, 0x82, /* Report ID (130), */
668 0x09, 0x22, /* Usage (22h), */
669 0x95, 0x3F, /* Report Count (63), */
670 0xB1, 0x02, /* Feature (Variable), */
671 0x85, 0x83, /* Report ID (131), */
672 0x09, 0x23, /* Usage (23h), */
673 0xB1, 0x02, /* Feature (Variable), */
674 0x85, 0x84, /* Report ID (132), */
675 0x09, 0x24, /* Usage (24h), */
676 0xB1, 0x02, /* Feature (Variable), */
677 0x85, 0x90, /* Report ID (144), */
678 0x09, 0x30, /* Usage (30h), */
679 0xB1, 0x02, /* Feature (Variable), */
680 0x85, 0x91, /* Report ID (145), */
681 0x09, 0x31, /* Usage (31h), */
682 0xB1, 0x02, /* Feature (Variable), */
683 0x85, 0x92, /* Report ID (146), */
684 0x09, 0x32, /* Usage (32h), */
685 0xB1, 0x02, /* Feature (Variable), */
686 0x85, 0x93, /* Report ID (147), */
687 0x09, 0x33, /* Usage (33h), */
688 0xB1, 0x02, /* Feature (Variable), */
689 0x85, 0xA0, /* Report ID (160), */
690 0x09, 0x40, /* Usage (40h), */
691 0xB1, 0x02, /* Feature (Variable), */
692 0x85, 0xA4, /* Report ID (164), */
693 0x09, 0x44, /* Usage (44h), */
694 0xB1, 0x02, /* Feature (Variable), */
695 0xC0 /* End Collection */
696};
697
Jiri Kosina078328d2013-06-13 12:03:49 +0200698static __u8 ps3remote_rdesc[] = {
699 0x05, 0x01, /* GUsagePage Generic Desktop */
700 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
701 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
702
703 /* Use collection 1 for joypad buttons */
704 0xA1, 0x02, /* MCollection Logical (interrelated data) */
705
706 /* Ignore the 1st byte, maybe it is used for a controller
707 * number but it's not needed for correct operation */
708 0x75, 0x08, /* GReportSize 0x08 [8] */
709 0x95, 0x01, /* GReportCount 0x01 [1] */
710 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
711
712 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
713 * buttons multiple keypresses are allowed */
714 0x05, 0x09, /* GUsagePage Button */
715 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
716 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
717 0x14, /* GLogicalMinimum [0] */
718 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
719 0x75, 0x01, /* GReportSize 0x01 [1] */
720 0x95, 0x18, /* GReportCount 0x18 [24] */
721 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
722
723 0xC0, /* MEndCollection */
724
725 /* Use collection 2 for remote control buttons */
726 0xA1, 0x02, /* MCollection Logical (interrelated data) */
727
728 /* 5th byte is used for remote control buttons */
729 0x05, 0x09, /* GUsagePage Button */
730 0x18, /* LUsageMinimum [No button pressed] */
731 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
732 0x14, /* GLogicalMinimum [0] */
733 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
734 0x75, 0x08, /* GReportSize 0x08 [8] */
735 0x95, 0x01, /* GReportCount 0x01 [1] */
736 0x80, /* MInput */
737
738 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
739 * 0xff and 11th is for press indication */
740 0x75, 0x08, /* GReportSize 0x08 [8] */
741 0x95, 0x06, /* GReportCount 0x06 [6] */
742 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
743
744 /* 12th byte is for battery strength */
745 0x05, 0x06, /* GUsagePage Generic Device Controls */
746 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
747 0x14, /* GLogicalMinimum [0] */
748 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
749 0x75, 0x08, /* GReportSize 0x08 [8] */
750 0x95, 0x01, /* GReportCount 0x01 [1] */
751 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
752
753 0xC0, /* MEndCollection */
754
755 0xC0 /* MEndCollection [Game Pad] */
756};
757
758static const unsigned int ps3remote_keymap_joypad_buttons[] = {
759 [0x01] = KEY_SELECT,
760 [0x02] = BTN_THUMBL, /* L3 */
761 [0x03] = BTN_THUMBR, /* R3 */
762 [0x04] = BTN_START,
763 [0x05] = KEY_UP,
764 [0x06] = KEY_RIGHT,
765 [0x07] = KEY_DOWN,
766 [0x08] = KEY_LEFT,
767 [0x09] = BTN_TL2, /* L2 */
768 [0x0a] = BTN_TR2, /* R2 */
769 [0x0b] = BTN_TL, /* L1 */
770 [0x0c] = BTN_TR, /* R1 */
771 [0x0d] = KEY_OPTION, /* options/triangle */
772 [0x0e] = KEY_BACK, /* back/circle */
773 [0x0f] = BTN_0, /* cross */
774 [0x10] = KEY_SCREEN, /* view/square */
775 [0x11] = KEY_HOMEPAGE, /* PS button */
776 [0x14] = KEY_ENTER,
777};
778static const unsigned int ps3remote_keymap_remote_buttons[] = {
779 [0x00] = KEY_1,
780 [0x01] = KEY_2,
781 [0x02] = KEY_3,
782 [0x03] = KEY_4,
783 [0x04] = KEY_5,
784 [0x05] = KEY_6,
785 [0x06] = KEY_7,
786 [0x07] = KEY_8,
787 [0x08] = KEY_9,
788 [0x09] = KEY_0,
789 [0x0e] = KEY_ESC, /* return */
790 [0x0f] = KEY_CLEAR,
791 [0x16] = KEY_EJECTCD,
792 [0x1a] = KEY_MENU, /* top menu */
793 [0x28] = KEY_TIME,
794 [0x30] = KEY_PREVIOUS,
795 [0x31] = KEY_NEXT,
796 [0x32] = KEY_PLAY,
797 [0x33] = KEY_REWIND, /* scan back */
798 [0x34] = KEY_FORWARD, /* scan forward */
799 [0x38] = KEY_STOP,
800 [0x39] = KEY_PAUSE,
801 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
802 [0x60] = KEY_FRAMEBACK, /* slow/step back */
803 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
804 [0x63] = KEY_SUBTITLE,
805 [0x64] = KEY_AUDIO,
806 [0x65] = KEY_ANGLE,
807 [0x70] = KEY_INFO, /* display */
808 [0x80] = KEY_BLUE,
809 [0x81] = KEY_RED,
810 [0x82] = KEY_GREEN,
811 [0x83] = KEY_YELLOW,
812};
813
Colin Leitnerf04d5142013-05-27 23:41:05 +0200814static const unsigned int buzz_keymap[] = {
Frank Praznikad142b92014-02-20 11:36:00 -0500815 /*
816 * The controller has 4 remote buzzers, each with one LED and 5
Colin Leitnerf04d5142013-05-27 23:41:05 +0200817 * buttons.
818 *
819 * We use the mapping chosen by the controller, which is:
820 *
821 * Key Offset
822 * -------------------
823 * Buzz 1
824 * Blue 5
825 * Orange 4
826 * Green 3
827 * Yellow 2
828 *
829 * So, for example, the orange button on the third buzzer is mapped to
830 * BTN_TRIGGER_HAPPY14
831 */
832 [ 1] = BTN_TRIGGER_HAPPY1,
833 [ 2] = BTN_TRIGGER_HAPPY2,
834 [ 3] = BTN_TRIGGER_HAPPY3,
835 [ 4] = BTN_TRIGGER_HAPPY4,
836 [ 5] = BTN_TRIGGER_HAPPY5,
837 [ 6] = BTN_TRIGGER_HAPPY6,
838 [ 7] = BTN_TRIGGER_HAPPY7,
839 [ 8] = BTN_TRIGGER_HAPPY8,
840 [ 9] = BTN_TRIGGER_HAPPY9,
841 [10] = BTN_TRIGGER_HAPPY10,
842 [11] = BTN_TRIGGER_HAPPY11,
843 [12] = BTN_TRIGGER_HAPPY12,
844 [13] = BTN_TRIGGER_HAPPY13,
845 [14] = BTN_TRIGGER_HAPPY14,
846 [15] = BTN_TRIGGER_HAPPY15,
847 [16] = BTN_TRIGGER_HAPPY16,
848 [17] = BTN_TRIGGER_HAPPY17,
849 [18] = BTN_TRIGGER_HAPPY18,
850 [19] = BTN_TRIGGER_HAPPY19,
851 [20] = BTN_TRIGGER_HAPPY20,
852};
853
Frank Praznikd902f472014-01-27 10:17:36 -0500854static enum power_supply_property sony_battery_props[] = {
855 POWER_SUPPLY_PROP_PRESENT,
856 POWER_SUPPLY_PROP_CAPACITY,
857 POWER_SUPPLY_PROP_SCOPE,
858 POWER_SUPPLY_PROP_STATUS,
859};
860
Frank Praznik55d3b662014-04-14 10:11:32 -0400861struct sixaxis_led {
862 __u8 time_enabled; /* the total time the led is active (0xff means forever) */
863 __u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
864 __u8 enabled;
865 __u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
866 __u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
867} __packed;
868
869struct sixaxis_rumble {
870 __u8 padding;
871 __u8 right_duration; /* Right motor duration (0xff means forever) */
872 __u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
873 __u8 left_duration; /* Left motor duration (0xff means forever) */
874 __u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
875} __packed;
876
877struct sixaxis_output_report {
878 __u8 report_id;
879 struct sixaxis_rumble rumble;
880 __u8 padding[4];
881 __u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
882 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
883 struct sixaxis_led _reserved; /* LED5, not actually soldered */
884} __packed;
885
886union sixaxis_output_report_01 {
887 struct sixaxis_output_report data;
888 __u8 buf[36];
889};
890
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400891struct motion_output_report_02 {
892 u8 type, zero;
893 u8 r, g, b;
894 u8 zero2;
895 u8 rumble;
896};
897
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500898#define DS4_REPORT_0x02_SIZE 37
899#define DS4_REPORT_0x05_SIZE 32
900#define DS4_REPORT_0x11_SIZE 78
901#define DS4_REPORT_0x81_SIZE 7
Antonio Ospite29b691a2015-02-16 18:12:21 +0100902#define SIXAXIS_REPORT_0xF2_SIZE 17
Antonio Ospitea85d67b2015-02-16 18:12:22 +0100903#define SIXAXIS_REPORT_0xF5_SIZE 8
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500904
Jiri Kosina8b402c92015-02-23 11:15:44 +0100905static DEFINE_SPINLOCK(sony_dev_list_lock);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500906static LIST_HEAD(sony_device_list);
Frank Praznik80250872014-04-14 10:11:35 -0400907static DEFINE_IDA(sony_device_id_allocator);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500908
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200909struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500910 spinlock_t lock;
Frank Praznikd2d782f2014-02-20 11:36:03 -0500911 struct list_head list_node;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100912 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500913 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200914 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100915 struct work_struct state_worker;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100916 struct power_supply *battery;
917 struct power_supply_desc battery_desc;
Frank Praznik80250872014-04-14 10:11:35 -0400918 int device_id;
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500919 __u8 *output_report_dmabuf;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200920
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100921#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100922 __u8 left;
923 __u8 right;
924#endif
925
Frank Praznikd2d782f2014-02-20 11:36:03 -0500926 __u8 mac_address[6];
Frank Praznik5f5750d2014-02-19 13:09:22 -0500927 __u8 worker_initialized;
Frank Praznikd902f472014-01-27 10:17:36 -0500928 __u8 cable_state;
929 __u8 battery_charging;
930 __u8 battery_capacity;
Frank Praznik60781cf2014-01-11 15:13:15 -0500931 __u8 led_state[MAX_LEDS];
Frank Praznikb3ed4582014-04-14 10:11:36 -0400932 __u8 led_delay_on[MAX_LEDS];
933 __u8 led_delay_off[MAX_LEDS];
Frank Praznik60781cf2014-01-11 15:13:15 -0500934 __u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200935};
936
Antonio Ospitec607fb82014-06-24 13:28:41 +0200937static __u8 *sixaxis_fixup(struct hid_device *hdev, __u8 *rdesc,
938 unsigned int *rsize)
939{
940 *rsize = sizeof(sixaxis_rdesc);
941 return sixaxis_rdesc;
942}
943
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400944static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
945 unsigned int *rsize)
946{
947 *rsize = sizeof(motion_rdesc);
948 return motion_rdesc;
949}
950
Jiri Kosina078328d2013-06-13 12:03:49 +0200951static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
952 unsigned int *rsize)
953{
954 *rsize = sizeof(ps3remote_rdesc);
955 return ps3remote_rdesc;
956}
957
958static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
959 struct hid_field *field, struct hid_usage *usage,
960 unsigned long **bit, int *max)
961{
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(ps3remote_keymap_joypad_buttons))
970 return -1;
971
972 key = ps3remote_keymap_joypad_buttons[key];
973 if (!key)
974 return -1;
975 break;
976 case 2:
977 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
978 return -1;
979
980 key = ps3remote_keymap_remote_buttons[key];
981 if (!key)
982 return -1;
983 break;
984 default:
985 return -1;
986 }
987
988 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
989 return 1;
990}
991
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400992static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
993 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200994{
995 struct sony_sc *sc = hid_get_drvdata(hdev);
996
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900997 /*
998 * Some Sony RF receivers wrongly declare the mouse pointer as a
999 * a constant non-data variable.
1000 */
1001 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
1002 /* usage page: generic desktop controls */
1003 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
1004 /* usage: mouse */
1005 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
1006 /* input (usage page for x,y axes): constant, variable, relative */
1007 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa46491842013-01-15 19:40:48 +09001008 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +09001009 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001010 rdesc[55] = 0x06;
1011 }
Simon Wood61ab44b2011-06-10 12:00:26 +02001012
Frank Prazniked19d8c2014-01-16 21:43:12 -05001013 /*
1014 * The default Dualshock 4 USB descriptor doesn't assign
1015 * the gyroscope values to corresponding axes so we need a
1016 * modified one.
1017 */
1018 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
1019 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
1020 rdesc = dualshock4_usb_rdesc;
1021 *rsize = sizeof(dualshock4_usb_rdesc);
Frank Praznikd8296742014-02-05 20:03:45 -05001022 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
1023 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
1024 rdesc = dualshock4_bt_rdesc;
1025 *rsize = sizeof(dualshock4_bt_rdesc);
Frank Prazniked19d8c2014-01-16 21:43:12 -05001026 }
1027
Antonio Ospitec607fb82014-06-24 13:28:41 +02001028 if (sc->quirks & SIXAXIS_CONTROLLER)
1029 return sixaxis_fixup(hdev, rdesc, rsize);
Jiri Kosina078328d2013-06-13 12:03:49 +02001030
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001031 if (sc->quirks & MOTION_CONTROLLER)
1032 return motion_fixup(hdev, rdesc, rsize);
1033
Jiri Kosina078328d2013-06-13 12:03:49 +02001034 if (sc->quirks & PS3REMOTE)
1035 return ps3remote_fixup(hdev, rdesc, rsize);
1036
Nikolai Kondrashov73e40082010-08-06 23:03:06 +04001037 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001038}
1039
Frank Praznikd902f472014-01-27 10:17:36 -05001040static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1041{
1042 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
1043 unsigned long flags;
1044 __u8 cable_state, battery_capacity, battery_charging;
1045
Frank Praznikad142b92014-02-20 11:36:00 -05001046 /*
1047 * The sixaxis is charging if the battery value is 0xee
Frank Praznikd902f472014-01-27 10:17:36 -05001048 * and it is fully charged if the value is 0xef.
1049 * It does not report the actual level while charging so it
1050 * is set to 100% while charging is in progress.
1051 */
1052 if (rd[30] >= 0xee) {
1053 battery_capacity = 100;
Frank Praznika43e94a2014-02-15 13:35:42 -05001054 battery_charging = !(rd[30] & 0x01);
Frank Praznik9fddd742014-08-29 13:11:52 -04001055 cable_state = 1;
Frank Praznikd902f472014-01-27 10:17:36 -05001056 } else {
Frank Praznikac3c9a92014-02-20 11:36:02 -05001057 __u8 index = rd[30] <= 5 ? rd[30] : 5;
1058 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -05001059 battery_charging = 0;
Frank Praznik9fddd742014-08-29 13:11:52 -04001060 cable_state = 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001061 }
Frank Praznikd902f472014-01-27 10:17:36 -05001062
1063 spin_lock_irqsave(&sc->lock, flags);
1064 sc->cable_state = cable_state;
1065 sc->battery_capacity = battery_capacity;
1066 sc->battery_charging = battery_charging;
1067 spin_unlock_irqrestore(&sc->lock, flags);
1068}
1069
1070static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1071{
Frank Praznike5606232014-01-27 10:17:37 -05001072 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1073 struct hid_input, list);
1074 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -05001075 unsigned long flags;
Frank Praznik6c5f8602014-02-05 20:03:47 -05001076 int n, offset;
Frank Praznikd902f472014-01-27 10:17:36 -05001077 __u8 cable_state, battery_capacity, battery_charging;
1078
Frank Praznikad142b92014-02-20 11:36:00 -05001079 /*
1080 * Battery and touchpad data starts at byte 30 in the USB report and
Frank Praznik6c5f8602014-02-05 20:03:47 -05001081 * 32 in Bluetooth report.
1082 */
1083 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
1084
Frank Praznikad142b92014-02-20 11:36:00 -05001085 /*
1086 * The lower 4 bits of byte 30 contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -05001087 * and the 5th bit contains the USB cable state.
1088 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001089 cable_state = (rd[offset] >> 4) & 0x01;
1090 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -05001091
Frank Praznikad142b92014-02-20 11:36:00 -05001092 /*
1093 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -05001094 * 0 to 10, and when running on battery power it ranges from 0 to 9.
1095 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -05001096 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001097 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -05001098 battery_charging = 0;
1099 else
1100 battery_charging = 1;
1101
Frank Praznik6c5f8602014-02-05 20:03:47 -05001102 if (!cable_state)
1103 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -05001104 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -05001105 battery_capacity = 10;
1106
Frank Praznikd902f472014-01-27 10:17:36 -05001107 battery_capacity *= 10;
1108
1109 spin_lock_irqsave(&sc->lock, flags);
1110 sc->cable_state = cable_state;
1111 sc->battery_capacity = battery_capacity;
1112 sc->battery_charging = battery_charging;
1113 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -05001114
Frank Praznik6c5f8602014-02-05 20:03:47 -05001115 offset += 5;
1116
Frank Praznikad142b92014-02-20 11:36:00 -05001117 /*
1118 * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
Frank Praznik6c5f8602014-02-05 20:03:47 -05001119 * and 37 on Bluetooth.
Frank Praznike5606232014-01-27 10:17:37 -05001120 * The first 7 bits of the first byte is a counter and bit 8 is a touch
1121 * indicator that is 0 when pressed and 1 when not pressed.
1122 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1123 * The data for the second touch is in the same format and immediatly
1124 * follows the data for the first.
1125 */
1126 for (n = 0; n < 2; n++) {
1127 __u16 x, y;
1128
1129 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1130 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1131
1132 input_mt_slot(input_dev, n);
1133 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
1134 !(rd[offset] >> 7));
1135 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1136 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1137
1138 offset += 4;
1139 }
Frank Praznikd902f472014-01-27 10:17:36 -05001140}
1141
Simon Woodc9e4d872011-06-10 12:00:27 +02001142static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
1143 __u8 *rd, int size)
1144{
1145 struct sony_sc *sc = hid_get_drvdata(hdev);
1146
Frank Praznikad142b92014-02-20 11:36:00 -05001147 /*
1148 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +02001149 * has to be BYTE_SWAPPED before passing up to joystick interface
1150 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001151 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Simon Woodc9e4d872011-06-10 12:00:27 +02001152 swap(rd[41], rd[42]);
1153 swap(rd[43], rd[44]);
1154 swap(rd[45], rd[46]);
1155 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -05001156
1157 sixaxis_parse_report(sc, rd, size);
Frank Praznik68330d82014-02-05 20:03:49 -05001158 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1159 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1160 && rd[0] == 0x11 && size == 78)) {
Frank Praznikd902f472014-01-27 10:17:36 -05001161 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +02001162 }
1163
1164 return 0;
1165}
1166
Colin Leitnerf04d5142013-05-27 23:41:05 +02001167static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1168 struct hid_field *field, struct hid_usage *usage,
1169 unsigned long **bit, int *max)
1170{
1171 struct sony_sc *sc = hid_get_drvdata(hdev);
1172
1173 if (sc->quirks & BUZZ_CONTROLLER) {
1174 unsigned int key = usage->hid & HID_USAGE;
1175
1176 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1177 return -1;
1178
1179 switch (usage->collection_index) {
1180 case 1:
1181 if (key >= ARRAY_SIZE(buzz_keymap))
1182 return -1;
1183
1184 key = buzz_keymap[key];
1185 if (!key)
1186 return -1;
1187 break;
1188 default:
1189 return -1;
1190 }
1191
1192 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1193 return 1;
1194 }
1195
Jiri Kosina078328d2013-06-13 12:03:49 +02001196 if (sc->quirks & PS3REMOTE)
1197 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1198
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001199 /* Let hid-core decide for the others */
1200 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001201}
1202
Frank Praznikce8efc32014-09-18 21:15:01 -04001203static int sony_register_touchpad(struct hid_input *hi, int touch_count,
1204 int w, int h)
1205{
1206 struct input_dev *input_dev = hi->input;
1207 int ret;
1208
1209 ret = input_mt_init_slots(input_dev, touch_count, 0);
1210 if (ret < 0)
1211 return ret;
1212
1213 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1214 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1215
1216 return 0;
1217}
1218
1219static void sony_input_configured(struct hid_device *hdev,
1220 struct hid_input *hidinput)
1221{
1222 struct sony_sc *sc = hid_get_drvdata(hdev);
1223
1224 /*
1225 * The Dualshock 4 touchpad supports 2 touches and has a
Frank Praznik981c5b42014-09-24 09:38:19 -04001226 * resolution of 1920x942 (44.86 dots/mm).
Frank Praznikce8efc32014-09-18 21:15:01 -04001227 */
1228 if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik981c5b42014-09-24 09:38:19 -04001229 if (sony_register_touchpad(hidinput, 2, 1920, 942) != 0)
Frank Praznikce8efc32014-09-18 21:15:01 -04001230 hid_err(sc->hdev,
1231 "Unable to initialize multi-touch slots\n");
1232 }
1233}
1234
Antonio Ospite5710fab2011-02-20 18:26:45 +01001235/*
Jiri Slabybd28ce02008-06-25 23:47:04 +02001236 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1237 * to "operational". Without this, the ps3 controller will not report any
1238 * events.
1239 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001240static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001241{
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001242 const int buf_size =
1243 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
Antonio Ospite2e701a32015-02-16 18:12:24 +01001244 __u8 *buf;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001245 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001246
Antonio Ospite2e701a32015-02-16 18:12:24 +01001247 buf = kmalloc(buf_size, GFP_KERNEL);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001248 if (!buf)
1249 return -ENOMEM;
1250
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001251 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1252 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001253 if (ret < 0) {
1254 hid_err(hdev, "can't set operational mode: step 1\n");
1255 goto out;
1256 }
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001257
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001258 /*
1259 * Some compatible controllers like the Speedlink Strike FX and
1260 * Gasia need another query plus an USB interrupt to get operational.
1261 */
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001262 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1263 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001264 if (ret < 0) {
1265 hid_err(hdev, "can't set operational mode: step 2\n");
1266 goto out;
1267 }
1268
1269 ret = hid_hw_output_report(hdev, buf, 1);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001270 if (ret < 0)
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001271 hid_err(hdev, "can't set operational mode: step 3\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +02001272
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001273out:
Jiri Slabybd28ce02008-06-25 23:47:04 +02001274 kfree(buf);
1275
1276 return ret;
1277}
1278
Antonio Ospite816651a2010-05-03 22:15:55 +02001279static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001280{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001281 static const __u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1282 __u8 *buf;
1283 int ret;
1284
1285 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1286 if (!buf)
1287 return -ENOMEM;
1288
1289 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
Benjamin Tissoiresb0dd72a2014-02-10 12:58:54 -05001290 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001291
1292 kfree(buf);
1293
1294 return ret;
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001295}
1296
Frank Praznikad142b92014-02-20 11:36:00 -05001297/*
1298 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
Frank Praznik68330d82014-02-05 20:03:49 -05001299 * controller so that it sends full input reports of type 0x11.
1300 */
1301static int dualshock4_set_operational_bt(struct hid_device *hdev)
1302{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001303 __u8 *buf;
1304 int ret;
Frank Praznik68330d82014-02-05 20:03:49 -05001305
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001306 buf = kmalloc(DS4_REPORT_0x02_SIZE, GFP_KERNEL);
1307 if (!buf)
1308 return -ENOMEM;
1309
1310 ret = hid_hw_raw_request(hdev, 0x02, buf, DS4_REPORT_0x02_SIZE,
Frank Praznik68330d82014-02-05 20:03:49 -05001311 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001312
1313 kfree(buf);
1314
1315 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001316}
1317
Frank Praznik221399b2015-05-05 20:47:32 -04001318static void sixaxis_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001319{
1320 static const __u8 sixaxis_leds[10][4] = {
1321 { 0x01, 0x00, 0x00, 0x00 },
1322 { 0x00, 0x01, 0x00, 0x00 },
1323 { 0x00, 0x00, 0x01, 0x00 },
1324 { 0x00, 0x00, 0x00, 0x01 },
1325 { 0x01, 0x00, 0x00, 0x01 },
1326 { 0x00, 0x01, 0x00, 0x01 },
1327 { 0x00, 0x00, 0x01, 0x01 },
1328 { 0x01, 0x00, 0x01, 0x01 },
1329 { 0x00, 0x01, 0x01, 0x01 },
1330 { 0x01, 0x01, 0x01, 0x01 }
1331 };
1332
Frank Praznik221399b2015-05-05 20:47:32 -04001333 int id = sc->device_id;
1334
1335 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001336
1337 if (id < 0)
1338 return;
1339
1340 id %= 10;
Frank Praznik221399b2015-05-05 20:47:32 -04001341 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001342}
1343
Frank Praznik221399b2015-05-05 20:47:32 -04001344static void dualshock4_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001345{
1346 /* The first 4 color/index entries match what the PS4 assigns */
1347 static const __u8 color_code[7][3] = {
1348 /* Blue */ { 0x00, 0x00, 0x01 },
1349 /* Red */ { 0x01, 0x00, 0x00 },
1350 /* Green */ { 0x00, 0x01, 0x00 },
1351 /* Pink */ { 0x02, 0x00, 0x01 },
1352 /* Orange */ { 0x02, 0x01, 0x00 },
1353 /* Teal */ { 0x00, 0x01, 0x01 },
1354 /* White */ { 0x01, 0x01, 0x01 }
1355 };
1356
Frank Praznik221399b2015-05-05 20:47:32 -04001357 int id = sc->device_id;
1358
1359 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001360
1361 if (id < 0)
1362 return;
1363
1364 id %= 7;
Frank Praznik221399b2015-05-05 20:47:32 -04001365 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001366}
1367
Frank Praznik221399b2015-05-05 20:47:32 -04001368static void buzz_set_leds(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001369{
Frank Praznik221399b2015-05-05 20:47:32 -04001370 struct hid_device *hdev = sc->hdev;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001371 struct list_head *report_list =
1372 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1373 struct hid_report *report = list_entry(report_list->next,
1374 struct hid_report, list);
1375 __s32 *value = report->field[0]->value;
1376
Frank Praznik221399b2015-05-05 20:47:32 -04001377 BUILD_BUG_ON(MAX_LEDS < 4);
1378
Colin Leitnerf04d5142013-05-27 23:41:05 +02001379 value[0] = 0x00;
Frank Praznik221399b2015-05-05 20:47:32 -04001380 value[1] = sc->led_state[0] ? 0xff : 0x00;
1381 value[2] = sc->led_state[1] ? 0xff : 0x00;
1382 value[3] = sc->led_state[2] ? 0xff : 0x00;
1383 value[4] = sc->led_state[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001384 value[5] = 0x00;
1385 value[6] = 0x00;
1386 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1387}
1388
Frank Praznik221399b2015-05-05 20:47:32 -04001389static void sony_set_leds(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001390{
Frank Praznik221399b2015-05-05 20:47:32 -04001391 if (!(sc->quirks & BUZZ_CONTROLLER))
Frank Praznikfa57a812014-04-14 10:11:33 -04001392 schedule_work(&sc->state_worker);
Frank Praznik221399b2015-05-05 20:47:32 -04001393 else
1394 buzz_set_leds(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001395}
1396
Sven Eckelmannc5382512013-11-19 20:26:30 +01001397static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001398 enum led_brightness value)
1399{
1400 struct device *dev = led->dev->parent;
1401 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1402 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001403
1404 int n;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001405 int force_update;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001406
1407 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001408 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001409 hid_err(hdev, "No device data\n");
1410 return;
1411 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001412
Frank Praznikb3ed4582014-04-14 10:11:36 -04001413 /*
1414 * The Sixaxis on USB will override any LED settings sent to it
1415 * and keep flashing all of the LEDs until the PS button is pressed.
1416 * Updates, even if redundant, must be always be sent to the
1417 * controller to avoid having to toggle the state of an LED just to
1418 * stop the flashing later on.
1419 */
1420 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1421
Frank Praznik60781cf2014-01-11 15:13:15 -05001422 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznikb3ed4582014-04-14 10:11:36 -04001423 if (led == drv_data->leds[n] && (force_update ||
1424 (value != drv_data->led_state[n] ||
1425 drv_data->led_delay_on[n] ||
1426 drv_data->led_delay_off[n]))) {
1427
1428 drv_data->led_state[n] = value;
1429
1430 /* Setting the brightness stops the blinking */
1431 drv_data->led_delay_on[n] = 0;
1432 drv_data->led_delay_off[n] = 0;
1433
Frank Praznik221399b2015-05-05 20:47:32 -04001434 sony_set_leds(drv_data);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001435 break;
1436 }
1437 }
1438}
1439
Sven Eckelmannc5382512013-11-19 20:26:30 +01001440static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001441{
1442 struct device *dev = led->dev->parent;
1443 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1444 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001445
1446 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001447
1448 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001449 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001450 hid_err(hdev, "No device data\n");
1451 return LED_OFF;
1452 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001453
Frank Praznik60781cf2014-01-11 15:13:15 -05001454 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001455 if (led == drv_data->leds[n])
1456 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001457 }
1458
Simon Wood7db75042014-02-05 12:34:18 -07001459 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001460}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001461
Frank Praznikb3ed4582014-04-14 10:11:36 -04001462static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1463 unsigned long *delay_off)
1464{
1465 struct device *dev = led->dev->parent;
1466 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1467 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1468 int n;
1469 __u8 new_on, new_off;
1470
1471 if (!drv_data) {
1472 hid_err(hdev, "No device data\n");
1473 return -EINVAL;
1474 }
1475
1476 /* Max delay is 255 deciseconds or 2550 milliseconds */
1477 if (*delay_on > 2550)
1478 *delay_on = 2550;
1479 if (*delay_off > 2550)
1480 *delay_off = 2550;
1481
1482 /* Blink at 1 Hz if both values are zero */
1483 if (!*delay_on && !*delay_off)
1484 *delay_on = *delay_off = 500;
1485
1486 new_on = *delay_on / 10;
1487 new_off = *delay_off / 10;
1488
1489 for (n = 0; n < drv_data->led_count; n++) {
1490 if (led == drv_data->leds[n])
1491 break;
1492 }
1493
1494 /* This LED is not registered on this device */
1495 if (n >= drv_data->led_count)
1496 return -EINVAL;
1497
1498 /* Don't schedule work if the values didn't change */
1499 if (new_on != drv_data->led_delay_on[n] ||
1500 new_off != drv_data->led_delay_off[n]) {
1501 drv_data->led_delay_on[n] = new_on;
1502 drv_data->led_delay_off[n] = new_off;
1503 schedule_work(&drv_data->state_worker);
1504 }
1505
1506 return 0;
1507}
1508
Frank Praznikfa57a812014-04-14 10:11:33 -04001509static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001510{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001511 struct led_classdev *led;
1512 int n;
1513
Frank Praznikfa57a812014-04-14 10:11:33 -04001514 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001515
Frank Praznikfa57a812014-04-14 10:11:33 -04001516 for (n = 0; n < sc->led_count; n++) {
1517 led = sc->leds[n];
1518 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001519 if (!led)
1520 continue;
1521 led_classdev_unregister(led);
1522 kfree(led);
1523 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001524
Frank Praznikfa57a812014-04-14 10:11:33 -04001525 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001526}
1527
Frank Praznikfa57a812014-04-14 10:11:33 -04001528static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001529{
Frank Praznikfa57a812014-04-14 10:11:33 -04001530 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001531 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001532 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001533 struct led_classdev *led;
1534 size_t name_sz;
1535 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001536 size_t name_len;
1537 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001538 static const char * const ds4_name_str[] = { "red", "green", "blue",
1539 "global" };
Frank Praznik5607c892014-07-29 19:55:48 -07001540 __u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
Frank Praznikb3ed4582014-04-14 10:11:36 -04001541 __u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001542
Frank Praznikfa57a812014-04-14 10:11:33 -04001543 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001544
Frank Praznikfa57a812014-04-14 10:11:33 -04001545 if (sc->quirks & BUZZ_CONTROLLER) {
1546 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001547 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001548 name_len = strlen("::buzz#");
1549 name_fmt = "%s::buzz%d";
1550 /* Validate expected report characteristics. */
1551 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1552 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001553 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik221399b2015-05-05 20:47:32 -04001554 dualshock4_set_leds_from_id(sc);
1555 sc->led_state[3] = 1;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001556 sc->led_count = 4;
1557 memset(max_brightness, 255, 3);
1558 use_hw_blink[3] = 1;
1559 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001560 name_len = 0;
1561 name_fmt = "%s:%s";
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001562 } else if (sc->quirks & MOTION_CONTROLLER) {
1563 sc->led_count = 3;
1564 memset(max_brightness, 255, 3);
1565 use_ds4_names = 1;
1566 name_len = 0;
1567 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -05001568 } else {
Frank Praznik221399b2015-05-05 20:47:32 -04001569 sixaxis_set_leds_from_id(sc);
Frank Praznikfa57a812014-04-14 10:11:33 -04001570 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001571 memset(use_hw_blink, 1, 4);
1572 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001573 name_len = strlen("::sony#");
1574 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001575 }
1576
Frank Praznikad142b92014-02-20 11:36:00 -05001577 /*
1578 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001579 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001580 * LEDs to on
1581 */
Frank Praznik221399b2015-05-05 20:47:32 -04001582 sony_set_leds(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001583
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001584 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001585
Frank Praznikfa57a812014-04-14 10:11:33 -04001586 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001587
Frank Praznikb3ed4582014-04-14 10:11:36 -04001588 if (use_ds4_names)
1589 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001590
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001591 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1592 if (!led) {
1593 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001594 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001595 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001596 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001597
1598 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001599 if (use_ds4_names)
1600 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1601 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001602 else
1603 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001604 led->name = name;
Frank Praznik221399b2015-05-05 20:47:32 -04001605 led->brightness = sc->led_state[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001606 led->max_brightness = max_brightness[n];
Sven Eckelmannc5382512013-11-19 20:26:30 +01001607 led->brightness_get = sony_led_get_brightness;
1608 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001609
Frank Praznikb3ed4582014-04-14 10:11:36 -04001610 if (use_hw_blink[n])
1611 led->blink_set = sony_led_blink_set;
1612
Frank Praznik80250872014-04-14 10:11:35 -04001613 sc->leds[n] = led;
1614
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001615 ret = led_classdev_register(&hdev->dev, led);
1616 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001617 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001618 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001619 kfree(led);
1620 goto error_leds;
1621 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001622 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001623
1624 return ret;
1625
Colin Leitnerf04d5142013-05-27 23:41:05 +02001626error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001627 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001628
Colin Leitnerf04d5142013-05-27 23:41:05 +02001629 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001630}
1631
Frank Praznikcad665a2014-01-11 15:13:54 -05001632static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001633{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001634 static const union sixaxis_output_report_01 default_report = {
Frank Praznik55d3b662014-04-14 10:11:32 -04001635 .buf = {
1636 0x01,
1637 0x00, 0xff, 0x00, 0xff, 0x00,
1638 0x00, 0x00, 0x00, 0x00, 0x00,
1639 0xff, 0x27, 0x10, 0x00, 0x32,
1640 0xff, 0x27, 0x10, 0x00, 0x32,
1641 0xff, 0x27, 0x10, 0x00, 0x32,
1642 0xff, 0x27, 0x10, 0x00, 0x32,
1643 0x00, 0x00, 0x00, 0x00, 0x00
1644 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001645 };
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001646 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1647 struct sixaxis_output_report *report =
1648 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1649 int n;
1650
1651 /* Initialize the report with default values */
1652 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001653
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001654#ifdef CONFIG_SONY_FF
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001655 report->rumble.right_motor_on = sc->right ? 1 : 0;
1656 report->rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001657#endif
1658
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001659 report->leds_bitmap |= sc->led_state[0] << 1;
1660 report->leds_bitmap |= sc->led_state[1] << 2;
1661 report->leds_bitmap |= sc->led_state[2] << 3;
1662 report->leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001663
Simon Wood88f65762014-04-14 10:11:37 -04001664 /* Set flag for all leds off, required for 3rd party INTEC controller */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001665 if ((report->leds_bitmap & 0x1E) == 0)
1666 report->leds_bitmap |= 0x20;
Simon Wood88f65762014-04-14 10:11:37 -04001667
Frank Praznikb3ed4582014-04-14 10:11:36 -04001668 /*
1669 * The LEDs in the report are indexed in reverse order to their
1670 * corresponding light on the controller.
1671 * Index 0 = LED 4, index 1 = LED 3, etc...
1672 *
1673 * In the case of both delay values being zero (blinking disabled) the
1674 * default report values should be used or the controller LED will be
1675 * always off.
1676 */
1677 for (n = 0; n < 4; n++) {
1678 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001679 report->led[3 - n].duty_off = sc->led_delay_off[n];
1680 report->led[3 - n].duty_on = sc->led_delay_on[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001681 }
1682 }
1683
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001684 hid_hw_raw_request(sc->hdev, report->report_id, (__u8 *)report,
1685 sizeof(struct sixaxis_output_report),
1686 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001687}
1688
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001689static void dualshock4_state_worker(struct work_struct *work)
1690{
1691 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001692 struct hid_device *hdev = sc->hdev;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001693 __u8 *buf = sc->output_report_dmabuf;
Frank Praznik48220232014-02-05 20:03:44 -05001694 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001695
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001696 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001697 memset(buf, 0, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001698 buf[0] = 0x05;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001699 buf[1] = 0xFF;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001700 offset = 4;
1701 } else {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001702 memset(buf, 0, DS4_REPORT_0x11_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001703 buf[0] = 0x11;
1704 buf[1] = 0xB0;
1705 buf[3] = 0x0F;
1706 offset = 6;
1707 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001708
1709#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001710 buf[offset++] = sc->right;
1711 buf[offset++] = sc->left;
1712#else
1713 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001714#endif
1715
Frank Praznikb3ed4582014-04-14 10:11:36 -04001716 /* LED 3 is the global control */
1717 if (sc->led_state[3]) {
1718 buf[offset++] = sc->led_state[0];
1719 buf[offset++] = sc->led_state[1];
1720 buf[offset++] = sc->led_state[2];
1721 } else {
1722 offset += 3;
1723 }
1724
1725 /* If both delay values are zero the DualShock 4 disables blinking. */
1726 buf[offset++] = sc->led_delay_on[3];
1727 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05001728
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001729 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001730 hid_hw_output_report(hdev, buf, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001731 else
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001732 hid_hw_raw_request(hdev, 0x11, buf, DS4_REPORT_0x11_SIZE,
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001733 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001734}
1735
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001736static void motion_state_worker(struct work_struct *work)
1737{
1738 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1739 struct hid_device *hdev = sc->hdev;
1740 struct motion_output_report_02 *report =
1741 (struct motion_output_report_02 *)sc->output_report_dmabuf;
1742
1743 memset(report, 0, sizeof(struct motion_output_report_02));
1744
1745 report->type = 0x02; /* set leds */
1746 report->r = sc->led_state[0];
1747 report->g = sc->led_state[1];
1748 report->b = sc->led_state[2];
1749
1750#ifdef CONFIG_SONY_FF
1751 report->rumble = max(sc->right, sc->left);
1752#endif
1753
1754 hid_hw_output_report(hdev, (__u8 *)report,
1755 sizeof(struct motion_output_report_02));
1756}
1757
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001758static int sony_allocate_output_report(struct sony_sc *sc)
1759{
1760 if (sc->quirks & SIXAXIS_CONTROLLER)
1761 sc->output_report_dmabuf =
1762 kmalloc(sizeof(union sixaxis_output_report_01),
1763 GFP_KERNEL);
1764 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1765 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x11_SIZE,
1766 GFP_KERNEL);
1767 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1768 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x05_SIZE,
1769 GFP_KERNEL);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001770 else if (sc->quirks & MOTION_CONTROLLER)
1771 sc->output_report_dmabuf =
1772 kmalloc(sizeof(struct motion_output_report_02),
1773 GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001774 else
1775 return 0;
1776
1777 if (!sc->output_report_dmabuf)
1778 return -ENOMEM;
1779
1780 return 0;
1781}
1782
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001783#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001784static int sony_play_effect(struct input_dev *dev, void *data,
1785 struct ff_effect *effect)
1786{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001787 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001788 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001789
1790 if (effect->type != FF_RUMBLE)
1791 return 0;
1792
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001793 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001794 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001795
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001796 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001797 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001798}
1799
Frank Praznikfa57a812014-04-14 10:11:33 -04001800static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001801{
Frank Praznikfa57a812014-04-14 10:11:33 -04001802 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001803 struct hid_input, list);
1804 struct input_dev *input_dev = hidinput->input;
1805
1806 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1807 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1808}
1809
1810#else
Frank Praznikfa57a812014-04-14 10:11:33 -04001811static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001812{
1813 return 0;
1814}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001815
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001816#endif
1817
Frank Praznikd902f472014-01-27 10:17:36 -05001818static int sony_battery_get_property(struct power_supply *psy,
1819 enum power_supply_property psp,
1820 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001821{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001822 struct sony_sc *sc = power_supply_get_drvdata(psy);
Frank Praznikd902f472014-01-27 10:17:36 -05001823 unsigned long flags;
1824 int ret = 0;
1825 u8 battery_charging, battery_capacity, cable_state;
1826
1827 spin_lock_irqsave(&sc->lock, flags);
1828 battery_charging = sc->battery_charging;
1829 battery_capacity = sc->battery_capacity;
1830 cable_state = sc->cable_state;
1831 spin_unlock_irqrestore(&sc->lock, flags);
1832
1833 switch (psp) {
1834 case POWER_SUPPLY_PROP_PRESENT:
1835 val->intval = 1;
1836 break;
1837 case POWER_SUPPLY_PROP_SCOPE:
1838 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1839 break;
1840 case POWER_SUPPLY_PROP_CAPACITY:
1841 val->intval = battery_capacity;
1842 break;
1843 case POWER_SUPPLY_PROP_STATUS:
1844 if (battery_charging)
1845 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1846 else
1847 if (battery_capacity == 100 && cable_state)
1848 val->intval = POWER_SUPPLY_STATUS_FULL;
1849 else
1850 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1851 break;
1852 default:
1853 ret = -EINVAL;
1854 break;
1855 }
1856 return ret;
1857}
1858
1859static int sony_battery_probe(struct sony_sc *sc)
1860{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001861 struct power_supply_config psy_cfg = { .drv_data = sc, };
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001862 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05001863 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001864
Frank Praznikad142b92014-02-20 11:36:00 -05001865 /*
1866 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05001867 * if the battery is polled before the first device report is received.
1868 */
1869 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001870
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001871 sc->battery_desc.properties = sony_battery_props;
1872 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
1873 sc->battery_desc.get_property = sony_battery_get_property;
1874 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
1875 sc->battery_desc.use_for_apm = 0;
1876 sc->battery_desc.name = kasprintf(GFP_KERNEL,
1877 "sony_controller_battery_%pMR",
1878 sc->mac_address);
1879 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05001880 return -ENOMEM;
1881
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001882 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
1883 &psy_cfg);
1884 if (IS_ERR(sc->battery)) {
1885 ret = PTR_ERR(sc->battery);
Frank Praznikd902f472014-01-27 10:17:36 -05001886 hid_err(hdev, "Unable to register battery device\n");
1887 goto err_free;
1888 }
1889
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001890 power_supply_powers(sc->battery, &hdev->dev);
Frank Praznikd902f472014-01-27 10:17:36 -05001891 return 0;
1892
1893err_free:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001894 kfree(sc->battery_desc.name);
1895 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05001896 return ret;
1897}
1898
1899static void sony_battery_remove(struct sony_sc *sc)
1900{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001901 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05001902 return;
1903
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001904 power_supply_unregister(sc->battery);
1905 kfree(sc->battery_desc.name);
1906 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05001907}
1908
Frank Praznikd2d782f2014-02-20 11:36:03 -05001909/*
1910 * If a controller is plugged in via USB while already connected via Bluetooth
1911 * it will show up as two devices. A global list of connected controllers and
1912 * their MAC addresses is maintained to ensure that a device is only connected
1913 * once.
1914 */
1915static int sony_check_add_dev_list(struct sony_sc *sc)
1916{
1917 struct sony_sc *entry;
1918 unsigned long flags;
1919 int ret;
1920
1921 spin_lock_irqsave(&sony_dev_list_lock, flags);
1922
1923 list_for_each_entry(entry, &sony_device_list, list_node) {
1924 ret = memcmp(sc->mac_address, entry->mac_address,
1925 sizeof(sc->mac_address));
1926 if (!ret) {
1927 ret = -EEXIST;
1928 hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
1929 sc->mac_address);
1930 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001931 }
1932 }
1933
Frank Praznikd2d782f2014-02-20 11:36:03 -05001934 ret = 0;
1935 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001936
Frank Praznikd2d782f2014-02-20 11:36:03 -05001937unlock:
1938 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1939 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001940}
1941
Frank Praznikd2d782f2014-02-20 11:36:03 -05001942static void sony_remove_dev_list(struct sony_sc *sc)
1943{
1944 unsigned long flags;
1945
1946 if (sc->list_node.next) {
1947 spin_lock_irqsave(&sony_dev_list_lock, flags);
1948 list_del(&(sc->list_node));
1949 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1950 }
1951}
1952
1953static int sony_get_bt_devaddr(struct sony_sc *sc)
1954{
1955 int ret;
1956
1957 /* HIDP stores the device MAC address as a string in the uniq field. */
1958 ret = strlen(sc->hdev->uniq);
1959 if (ret != 17)
1960 return -EINVAL;
1961
1962 ret = sscanf(sc->hdev->uniq,
1963 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1964 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
1965 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
1966
1967 if (ret != 6)
1968 return -EINVAL;
1969
1970 return 0;
1971}
1972
1973static int sony_check_add(struct sony_sc *sc)
1974{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001975 __u8 *buf = NULL;
Frank Praznikd2d782f2014-02-20 11:36:03 -05001976 int n, ret;
1977
1978 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
1979 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
1980 /*
1981 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
1982 * address from the uniq string where HIDP stores it.
1983 * As uniq cannot be guaranteed to be a MAC address in all cases
1984 * a failure of this function should not prevent the connection.
1985 */
1986 if (sony_get_bt_devaddr(sc) < 0) {
1987 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
1988 return 0;
1989 }
1990 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001991 buf = kmalloc(DS4_REPORT_0x81_SIZE, GFP_KERNEL);
1992 if (!buf)
1993 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05001994
1995 /*
1996 * The MAC address of a DS4 controller connected via USB can be
1997 * retrieved with feature report 0x81. The address begins at
1998 * offset 1.
1999 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002000 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
2001 DS4_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
2002 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002003
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002004 if (ret != DS4_REPORT_0x81_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002005 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002006 ret = ret < 0 ? ret : -EINVAL;
2007 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002008 }
2009
2010 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
2011 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002012 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2013 if (!buf)
2014 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002015
2016 /*
2017 * The MAC address of a Sixaxis controller connected via USB can
2018 * be retrieved with feature report 0xf2. The address begins at
2019 * offset 4.
2020 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002021 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2022 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2023 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002024
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002025 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002026 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002027 ret = ret < 0 ? ret : -EINVAL;
2028 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002029 }
2030
2031 /*
2032 * The Sixaxis device MAC in the report is big-endian and must
2033 * be byte-swapped.
2034 */
2035 for (n = 0; n < 6; n++)
2036 sc->mac_address[5-n] = buf[4+n];
2037 } else {
2038 return 0;
2039 }
2040
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002041 ret = sony_check_add_dev_list(sc);
2042
2043out_free:
2044
2045 kfree(buf);
2046
2047 return ret;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002048}
2049
Frank Praznik80250872014-04-14 10:11:35 -04002050static int sony_set_device_id(struct sony_sc *sc)
2051{
2052 int ret;
2053
2054 /*
2055 * Only DualShock 4 or Sixaxis controllers get an id.
2056 * All others are set to -1.
2057 */
2058 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2059 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2060 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2061 GFP_KERNEL);
2062 if (ret < 0) {
2063 sc->device_id = -1;
2064 return ret;
2065 }
2066 sc->device_id = ret;
2067 } else {
2068 sc->device_id = -1;
2069 }
2070
2071 return 0;
2072}
2073
2074static void sony_release_device_id(struct sony_sc *sc)
2075{
2076 if (sc->device_id >= 0) {
2077 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2078 sc->device_id = -1;
2079 }
2080}
2081
Frank Praznik46262042014-04-14 10:11:31 -04002082static inline void sony_init_work(struct sony_sc *sc,
2083 void (*worker)(struct work_struct *))
2084{
2085 if (!sc->worker_initialized)
2086 INIT_WORK(&sc->state_worker, worker);
2087
2088 sc->worker_initialized = 1;
2089}
2090
2091static inline void sony_cancel_work_sync(struct sony_sc *sc)
2092{
2093 if (sc->worker_initialized)
2094 cancel_work_sync(&sc->state_worker);
2095}
Frank Praznikd2d782f2014-02-20 11:36:03 -05002096
Jiri Slabybd28ce02008-06-25 23:47:04 +02002097static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2098{
2099 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002100 unsigned long quirks = id->driver_data;
2101 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02002102 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002103
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002104 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002105 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08002106 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002107 return -ENOMEM;
2108 }
2109
Frank Praznikb94993f2015-02-22 20:42:46 -05002110 spin_lock_init(&sc->lock);
2111
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002112 sc->quirks = quirks;
2113 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002114 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002115
Jiri Slabybd28ce02008-06-25 23:47:04 +02002116 ret = hid_parse(hdev);
2117 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002118 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002119 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002120 }
2121
Colin Leitnerf04d5142013-05-27 23:41:05 +02002122 if (sc->quirks & VAIO_RDESC_CONSTANT)
2123 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
Antonio Ospite50764652014-06-24 13:28:40 +02002124 else if (sc->quirks & SIXAXIS_CONTROLLER)
Colin Leitnerf04d5142013-05-27 23:41:05 +02002125 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2126
2127 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002128 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002129 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002130 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002131 }
2132
Frank Praznik80250872014-04-14 10:11:35 -04002133 ret = sony_set_device_id(sc);
2134 if (ret < 0) {
2135 hid_err(hdev, "failed to allocate the device id\n");
2136 goto err_stop;
2137 }
2138
Frank Praznik131a8a92015-05-05 20:47:28 -04002139 ret = sony_allocate_output_report(sc);
2140 if (ret < 0) {
2141 hid_err(hdev, "failed to allocate the output report buffer\n");
2142 goto err_stop;
2143 }
2144
Antonio Ospite569b10a2010-10-19 16:13:10 +02002145 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002146 /*
2147 * The Sony Sixaxis does not handle HID Output Reports on the
2148 * Interrupt EP like it could, so we need to force HID Output
2149 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2150 *
2151 * There is also another issue about HID Output Reports via USB,
2152 * the Sixaxis does not want the report_id as part of the data
2153 * packet, so we have to discard buf[0] when sending the actual
2154 * control message, even for numbered reports, humpf!
2155 */
2156 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2157 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
Antonio Ospite816651a2010-05-03 22:15:55 +02002158 ret = sixaxis_set_operational_usb(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04002159 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002160 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04002161 /*
2162 * The Sixaxis wants output reports sent on the ctrl endpoint
2163 * when connected via Bluetooth.
2164 */
2165 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Antonio Ospite816651a2010-05-03 22:15:55 +02002166 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04002167 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002168 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik68330d82014-02-05 20:03:49 -05002169 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04002170 /*
2171 * The DualShock 4 wants output reports sent on the ctrl
2172 * endpoint when connected via Bluetooth.
2173 */
2174 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Frank Praznik68330d82014-02-05 20:03:49 -05002175 ret = dualshock4_set_operational_bt(hdev);
2176 if (ret < 0) {
2177 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
2178 goto err_stop;
2179 }
2180 }
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002181
Frank Praznik46262042014-04-14 10:11:31 -04002182 sony_init_work(sc, dualshock4_state_worker);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002183 } else if (sc->quirks & MOTION_CONTROLLER) {
2184 sony_init_work(sc, motion_state_worker);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002185 } else {
2186 ret = 0;
2187 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00002188
Jiri Kosina4dfdc462008-12-30 00:49:59 +01002189 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002190 goto err_stop;
2191
Frank Praznikd2d782f2014-02-20 11:36:03 -05002192 ret = sony_check_add(sc);
2193 if (ret < 0)
2194 goto err_stop;
2195
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002196 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002197 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002198 if (ret < 0)
2199 goto err_stop;
2200 }
2201
Frank Praznikd902f472014-01-27 10:17:36 -05002202 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2203 ret = sony_battery_probe(sc);
2204 if (ret < 0)
2205 goto err_stop;
2206
2207 /* Open the device to receive reports with battery info */
2208 ret = hid_hw_open(hdev);
2209 if (ret < 0) {
2210 hid_err(hdev, "hw open failed\n");
2211 goto err_stop;
2212 }
2213 }
2214
Frank Praznikc8de9db2014-02-20 11:36:01 -05002215 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002216 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05002217 if (ret < 0)
2218 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05002219 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002220
Jiri Slabybd28ce02008-06-25 23:47:04 +02002221 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05002222err_close:
2223 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002224err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002225 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002226 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002227 if (sc->quirks & SONY_BATTERY_SUPPORT)
2228 sony_battery_remove(sc);
Frank Praznik46262042014-04-14 10:11:31 -04002229 sony_cancel_work_sync(sc);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002230 kfree(sc->output_report_dmabuf);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002231 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04002232 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002233 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002234 return ret;
2235}
2236
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002237static void sony_remove(struct hid_device *hdev)
2238{
Colin Leitnerf04d5142013-05-27 23:41:05 +02002239 struct sony_sc *sc = hid_get_drvdata(hdev);
2240
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002241 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002242 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02002243
Frank Praznikd902f472014-01-27 10:17:36 -05002244 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2245 hid_hw_close(hdev);
2246 sony_battery_remove(sc);
2247 }
2248
Frank Praznik46262042014-04-14 10:11:31 -04002249 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002250
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002251 kfree(sc->output_report_dmabuf);
2252
Frank Praznikd2d782f2014-02-20 11:36:03 -05002253 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002254
Frank Praznik80250872014-04-14 10:11:35 -04002255 sony_release_device_id(sc);
2256
Jiri Slabybd28ce02008-06-25 23:47:04 +02002257 hid_hw_stop(hdev);
2258}
2259
2260static const struct hid_device_id sony_devices[] = {
2261 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2262 .driver_data = SIXAXIS_CONTROLLER_USB },
2263 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2264 .driver_data = SIXAXIS_CONTROLLER_USB },
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002265 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002266 .driver_data = MOTION_CONTROLLER_USB },
Simon Wooda4afa852015-06-03 09:45:19 -06002267 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002268 .driver_data = MOTION_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002269 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2270 .driver_data = SIXAXIS_CONTROLLER_BT },
2271 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2272 .driver_data = VAIO_RDESC_CONSTANT },
2273 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2274 .driver_data = VAIO_RDESC_CONSTANT },
2275 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2276 * Logitech joystick from the device descriptor. */
2277 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2278 .driver_data = BUZZ_CONTROLLER },
2279 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2280 .driver_data = BUZZ_CONTROLLER },
2281 /* PS3 BD Remote Control */
2282 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2283 .driver_data = PS3REMOTE },
2284 /* Logitech Harmony Adapter for PS3 */
2285 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2286 .driver_data = PS3REMOTE },
Frank Praznik68a49e52014-11-12 14:52:28 -05002287 /* SMK-Link PS3 BD Remote Control */
2288 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2289 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002290 /* Sony Dualshock 4 controllers for PS4 */
2291 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002292 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002293 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002294 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002295 { }
2296};
2297MODULE_DEVICE_TABLE(hid, sony_devices);
2298
2299static struct hid_driver sony_driver = {
Frank Praznikce8efc32014-09-18 21:15:01 -04002300 .name = "sony",
2301 .id_table = sony_devices,
2302 .input_mapping = sony_mapping,
2303 .input_configured = sony_input_configured,
2304 .probe = sony_probe,
2305 .remove = sony_remove,
2306 .report_fixup = sony_report_fixup,
2307 .raw_event = sony_raw_event
Jiri Slabybd28ce02008-06-25 23:47:04 +02002308};
Frank Praznik80250872014-04-14 10:11:35 -04002309
2310static int __init sony_init(void)
2311{
2312 dbg_hid("Sony:%s\n", __func__);
2313
2314 return hid_register_driver(&sony_driver);
2315}
2316
2317static void __exit sony_exit(void)
2318{
2319 dbg_hid("Sony:%s\n", __func__);
2320
Frank Praznik80250872014-04-14 10:11:35 -04002321 hid_unregister_driver(&sony_driver);
Antonio Ospite6c400652015-02-16 22:58:24 +01002322 ida_destroy(&sony_device_id_allocator);
Frank Praznik80250872014-04-14 10:11:35 -04002323}
2324module_init(sony_init);
2325module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002326
2327MODULE_LICENSE("GPL");