blob: 51cb1acce91d9f026cea69f1377550cce4bc7a1a [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)
Simon Wood12e9a6d72015-06-09 21:27:05 -060058#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
59 MOTION_CONTROLLER_BT)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040060#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
61 MOTION_CONTROLLER)
Frank Praznik60781cf2014-01-11 15:13:15 -050062
63#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010064
Frank Praznik4c3e8292015-05-05 20:47:33 -040065/*
66 * The Sixaxis reports both digital and analog values for each button on the
67 * controller except for Start, Select and the PS button. The controller ends
68 * up reporting 27 axes which causes them to spill over into the multi-touch
69 * axis values. Additionally, the controller only has 20 actual, physical axes
70 * so there are several unused axes in between the used ones.
71 */
Antonio Ospitec607fb82014-06-24 13:28:41 +020072static __u8 sixaxis_rdesc[] = {
Antonio Ospitefb705a62014-06-24 13:28:42 +020073 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznik4c3e8292015-05-05 20:47:33 -040074 0x09, 0x04, /* Usage (Joystick), */
Antonio Ospitefb705a62014-06-24 13:28:42 +020075 0xA1, 0x01, /* Collection (Application), */
76 0xA1, 0x02, /* Collection (Logical), */
77 0x85, 0x01, /* Report ID (1), */
78 0x75, 0x08, /* Report Size (8), */
79 0x95, 0x01, /* Report Count (1), */
80 0x15, 0x00, /* Logical Minimum (0), */
81 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
82 0x81, 0x03, /* Input (Constant, Variable), */
83 0x75, 0x01, /* Report Size (1), */
84 0x95, 0x13, /* Report Count (19), */
85 0x15, 0x00, /* Logical Minimum (0), */
86 0x25, 0x01, /* Logical Maximum (1), */
87 0x35, 0x00, /* Physical Minimum (0), */
88 0x45, 0x01, /* Physical Maximum (1), */
89 0x05, 0x09, /* Usage Page (Button), */
90 0x19, 0x01, /* Usage Minimum (01h), */
91 0x29, 0x13, /* Usage Maximum (13h), */
92 0x81, 0x02, /* Input (Variable), */
93 0x75, 0x01, /* Report Size (1), */
94 0x95, 0x0D, /* Report Count (13), */
95 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
96 0x81, 0x03, /* Input (Constant, Variable), */
97 0x15, 0x00, /* Logical Minimum (0), */
98 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
99 0x05, 0x01, /* Usage Page (Desktop), */
100 0x09, 0x01, /* Usage (Pointer), */
101 0xA1, 0x00, /* Collection (Physical), */
102 0x75, 0x08, /* Report Size (8), */
103 0x95, 0x04, /* Report Count (4), */
104 0x35, 0x00, /* Physical Minimum (0), */
105 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
106 0x09, 0x30, /* Usage (X), */
107 0x09, 0x31, /* Usage (Y), */
108 0x09, 0x32, /* Usage (Z), */
109 0x09, 0x35, /* Usage (Rz), */
110 0x81, 0x02, /* Input (Variable), */
111 0xC0, /* End Collection, */
112 0x05, 0x01, /* Usage Page (Desktop), */
113 0x95, 0x13, /* Report Count (19), */
114 0x09, 0x01, /* Usage (Pointer), */
115 0x81, 0x02, /* Input (Variable), */
116 0x95, 0x0C, /* Report Count (12), */
117 0x81, 0x01, /* Input (Constant), */
118 0x75, 0x10, /* Report Size (16), */
119 0x95, 0x04, /* Report Count (4), */
120 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
121 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
122 0x09, 0x01, /* Usage (Pointer), */
123 0x81, 0x02, /* Input (Variable), */
124 0xC0, /* End Collection, */
125 0xA1, 0x02, /* Collection (Logical), */
126 0x85, 0x02, /* Report ID (2), */
127 0x75, 0x08, /* Report Size (8), */
128 0x95, 0x30, /* Report Count (48), */
129 0x09, 0x01, /* Usage (Pointer), */
130 0xB1, 0x02, /* Feature (Variable), */
131 0xC0, /* End Collection, */
132 0xA1, 0x02, /* Collection (Logical), */
133 0x85, 0xEE, /* Report ID (238), */
134 0x75, 0x08, /* Report Size (8), */
135 0x95, 0x30, /* Report Count (48), */
136 0x09, 0x01, /* Usage (Pointer), */
137 0xB1, 0x02, /* Feature (Variable), */
138 0xC0, /* End Collection, */
139 0xA1, 0x02, /* Collection (Logical), */
140 0x85, 0xEF, /* Report ID (239), */
141 0x75, 0x08, /* Report Size (8), */
142 0x95, 0x30, /* Report Count (48), */
143 0x09, 0x01, /* Usage (Pointer), */
144 0xB1, 0x02, /* Feature (Variable), */
145 0xC0, /* End Collection, */
146 0xC0 /* End Collection */
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200147};
148
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400149/* PS/3 Motion controller */
150static __u8 motion_rdesc[] = {
151 0x05, 0x01, /* Usage Page (Desktop), */
152 0x09, 0x04, /* Usage (Joystick), */
153 0xA1, 0x01, /* Collection (Application), */
154 0xA1, 0x02, /* Collection (Logical), */
155 0x85, 0x01, /* Report ID (1), */
156 0x75, 0x08, /* Report Size (8), */
157 0x95, 0x01, /* Report Count (1), */
158 0x15, 0x00, /* Logical Minimum (0), */
159 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
160 0x81, 0x03, /* Input (Constant, Variable), */
161 0x75, 0x01, /* Report Size (1), */
162 0x95, 0x13, /* Report Count (19), */
163 0x15, 0x00, /* Logical Minimum (0), */
164 0x25, 0x01, /* Logical Maximum (1), */
165 0x35, 0x00, /* Physical Minimum (0), */
166 0x45, 0x01, /* Physical Maximum (1), */
167 0x05, 0x09, /* Usage Page (Button), */
168 0x19, 0x01, /* Usage Minimum (01h), */
169 0x29, 0x13, /* Usage Maximum (13h), */
170 0x81, 0x02, /* Input (Variable), */
171 0x75, 0x01, /* Report Size (1), */
172 0x95, 0x0D, /* Report Count (13), */
173 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
174 0x81, 0x03, /* Input (Constant, Variable), */
175 0x15, 0x00, /* Logical Minimum (0), */
176 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
177 0x05, 0x01, /* Usage Page (Desktop), */
178 0x09, 0x01, /* Usage (Pointer), */
179 0xA1, 0x00, /* Collection (Physical), */
180 0x75, 0x08, /* Report Size (8), */
181 0x95, 0x04, /* Report Count (4), */
182 0x35, 0x00, /* Physical Minimum (0), */
183 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
184 0x09, 0x30, /* Usage (X), */
185 0x09, 0x31, /* Usage (Y), */
186 0x09, 0x32, /* Usage (Z), */
187 0x09, 0x35, /* Usage (Rz), */
188 0x81, 0x02, /* Input (Variable), */
189 0xC0, /* End Collection, */
190 0x05, 0x01, /* Usage Page (Desktop), */
191 0x95, 0x13, /* Report Count (19), */
192 0x09, 0x01, /* Usage (Pointer), */
193 0x81, 0x02, /* Input (Variable), */
194 0x95, 0x0C, /* Report Count (12), */
195 0x81, 0x01, /* Input (Constant), */
196 0x75, 0x10, /* Report Size (16), */
197 0x95, 0x04, /* Report Count (4), */
198 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
199 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
200 0x09, 0x01, /* Usage (Pointer), */
201 0x81, 0x02, /* Input (Variable), */
202 0xC0, /* End Collection, */
203 0xA1, 0x02, /* Collection (Logical), */
204 0x85, 0x02, /* Report ID (2), */
205 0x75, 0x08, /* Report Size (8), */
206 0x95, 0x30, /* Report Count (48), */
207 0x09, 0x01, /* Usage (Pointer), */
208 0xB1, 0x02, /* Feature (Variable), */
209 0xC0, /* End Collection, */
210 0xA1, 0x02, /* Collection (Logical), */
211 0x85, 0xEE, /* Report ID (238), */
212 0x75, 0x08, /* Report Size (8), */
213 0x95, 0x30, /* Report Count (48), */
214 0x09, 0x01, /* Usage (Pointer), */
215 0xB1, 0x02, /* Feature (Variable), */
216 0xC0, /* End Collection, */
217 0xA1, 0x02, /* Collection (Logical), */
218 0x85, 0xEF, /* Report ID (239), */
219 0x75, 0x08, /* Report Size (8), */
220 0x95, 0x30, /* Report Count (48), */
221 0x09, 0x01, /* Usage (Pointer), */
222 0xB1, 0x02, /* Feature (Variable), */
223 0xC0, /* End Collection, */
224 0xC0 /* End Collection */
225};
226
227
Frank Praznikad142b92014-02-20 11:36:00 -0500228/*
229 * The default descriptor doesn't provide mapping for the accelerometers
Frank Praznik58d70272014-01-20 12:27:01 -0500230 * or orientation sensors. This fixed descriptor maps the accelerometers
231 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
232 * to usage values 0x43, 0x44 and 0x45.
233 */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500234static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -0500235 0x05, 0x01, /* Usage Page (Desktop), */
236 0x09, 0x05, /* Usage (Gamepad), */
237 0xA1, 0x01, /* Collection (Application), */
238 0x85, 0x01, /* Report ID (1), */
239 0x09, 0x30, /* Usage (X), */
240 0x09, 0x31, /* Usage (Y), */
241 0x09, 0x32, /* Usage (Z), */
242 0x09, 0x35, /* Usage (Rz), */
243 0x15, 0x00, /* Logical Minimum (0), */
244 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
245 0x75, 0x08, /* Report Size (8), */
246 0x95, 0x04, /* Report Count (4), */
247 0x81, 0x02, /* Input (Variable), */
248 0x09, 0x39, /* Usage (Hat Switch), */
249 0x15, 0x00, /* Logical Minimum (0), */
250 0x25, 0x07, /* Logical Maximum (7), */
251 0x35, 0x00, /* Physical Minimum (0), */
252 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
253 0x65, 0x14, /* Unit (Degrees), */
254 0x75, 0x04, /* Report Size (4), */
255 0x95, 0x01, /* Report Count (1), */
256 0x81, 0x42, /* Input (Variable, Null State), */
257 0x65, 0x00, /* Unit, */
258 0x05, 0x09, /* Usage Page (Button), */
259 0x19, 0x01, /* Usage Minimum (01h), */
260 0x29, 0x0E, /* Usage Maximum (0Eh), */
261 0x15, 0x00, /* Logical Minimum (0), */
262 0x25, 0x01, /* Logical Maximum (1), */
263 0x75, 0x01, /* Report Size (1), */
264 0x95, 0x0E, /* Report Count (14), */
265 0x81, 0x02, /* Input (Variable), */
266 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
267 0x09, 0x20, /* Usage (20h), */
268 0x75, 0x06, /* Report Size (6), */
269 0x95, 0x01, /* Report Count (1), */
270 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400271 0x25, 0x3F, /* Logical Maximum (63), */
Frank Praznik58d70272014-01-20 12:27:01 -0500272 0x81, 0x02, /* Input (Variable), */
273 0x05, 0x01, /* Usage Page (Desktop), */
274 0x09, 0x33, /* Usage (Rx), */
275 0x09, 0x34, /* Usage (Ry), */
276 0x15, 0x00, /* Logical Minimum (0), */
277 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
278 0x75, 0x08, /* Report Size (8), */
279 0x95, 0x02, /* Report Count (2), */
280 0x81, 0x02, /* Input (Variable), */
281 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
282 0x09, 0x21, /* Usage (21h), */
283 0x95, 0x03, /* Report Count (3), */
284 0x81, 0x02, /* Input (Variable), */
285 0x05, 0x01, /* Usage Page (Desktop), */
286 0x19, 0x40, /* Usage Minimum (40h), */
287 0x29, 0x42, /* Usage Maximum (42h), */
288 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
289 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
290 0x75, 0x10, /* Report Size (16), */
291 0x95, 0x03, /* Report Count (3), */
292 0x81, 0x02, /* Input (Variable), */
293 0x19, 0x43, /* Usage Minimum (43h), */
294 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400295 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
296 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznik58d70272014-01-20 12:27:01 -0500297 0x95, 0x03, /* Report Count (3), */
298 0x81, 0x02, /* Input (Variable), */
299 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
300 0x09, 0x21, /* Usage (21h), */
301 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400302 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
Frank Praznik58d70272014-01-20 12:27:01 -0500303 0x75, 0x08, /* Report Size (8), */
304 0x95, 0x27, /* Report Count (39), */
305 0x81, 0x02, /* Input (Variable), */
306 0x85, 0x05, /* Report ID (5), */
307 0x09, 0x22, /* Usage (22h), */
308 0x95, 0x1F, /* Report Count (31), */
309 0x91, 0x02, /* Output (Variable), */
310 0x85, 0x04, /* Report ID (4), */
311 0x09, 0x23, /* Usage (23h), */
312 0x95, 0x24, /* Report Count (36), */
313 0xB1, 0x02, /* Feature (Variable), */
314 0x85, 0x02, /* Report ID (2), */
315 0x09, 0x24, /* Usage (24h), */
316 0x95, 0x24, /* Report Count (36), */
317 0xB1, 0x02, /* Feature (Variable), */
318 0x85, 0x08, /* Report ID (8), */
319 0x09, 0x25, /* Usage (25h), */
320 0x95, 0x03, /* Report Count (3), */
321 0xB1, 0x02, /* Feature (Variable), */
322 0x85, 0x10, /* Report ID (16), */
323 0x09, 0x26, /* Usage (26h), */
324 0x95, 0x04, /* Report Count (4), */
325 0xB1, 0x02, /* Feature (Variable), */
326 0x85, 0x11, /* Report ID (17), */
327 0x09, 0x27, /* Usage (27h), */
328 0x95, 0x02, /* Report Count (2), */
329 0xB1, 0x02, /* Feature (Variable), */
330 0x85, 0x12, /* Report ID (18), */
331 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
332 0x09, 0x21, /* Usage (21h), */
333 0x95, 0x0F, /* Report Count (15), */
334 0xB1, 0x02, /* Feature (Variable), */
335 0x85, 0x13, /* Report ID (19), */
336 0x09, 0x22, /* Usage (22h), */
337 0x95, 0x16, /* Report Count (22), */
338 0xB1, 0x02, /* Feature (Variable), */
339 0x85, 0x14, /* Report ID (20), */
340 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
341 0x09, 0x20, /* Usage (20h), */
342 0x95, 0x10, /* Report Count (16), */
343 0xB1, 0x02, /* Feature (Variable), */
344 0x85, 0x15, /* Report ID (21), */
345 0x09, 0x21, /* Usage (21h), */
346 0x95, 0x2C, /* Report Count (44), */
347 0xB1, 0x02, /* Feature (Variable), */
348 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
349 0x85, 0x80, /* Report ID (128), */
350 0x09, 0x20, /* Usage (20h), */
351 0x95, 0x06, /* Report Count (6), */
352 0xB1, 0x02, /* Feature (Variable), */
353 0x85, 0x81, /* Report ID (129), */
354 0x09, 0x21, /* Usage (21h), */
355 0x95, 0x06, /* Report Count (6), */
356 0xB1, 0x02, /* Feature (Variable), */
357 0x85, 0x82, /* Report ID (130), */
358 0x09, 0x22, /* Usage (22h), */
359 0x95, 0x05, /* Report Count (5), */
360 0xB1, 0x02, /* Feature (Variable), */
361 0x85, 0x83, /* Report ID (131), */
362 0x09, 0x23, /* Usage (23h), */
363 0x95, 0x01, /* Report Count (1), */
364 0xB1, 0x02, /* Feature (Variable), */
365 0x85, 0x84, /* Report ID (132), */
366 0x09, 0x24, /* Usage (24h), */
367 0x95, 0x04, /* Report Count (4), */
368 0xB1, 0x02, /* Feature (Variable), */
369 0x85, 0x85, /* Report ID (133), */
370 0x09, 0x25, /* Usage (25h), */
371 0x95, 0x06, /* Report Count (6), */
372 0xB1, 0x02, /* Feature (Variable), */
373 0x85, 0x86, /* Report ID (134), */
374 0x09, 0x26, /* Usage (26h), */
375 0x95, 0x06, /* Report Count (6), */
376 0xB1, 0x02, /* Feature (Variable), */
377 0x85, 0x87, /* Report ID (135), */
378 0x09, 0x27, /* Usage (27h), */
379 0x95, 0x23, /* Report Count (35), */
380 0xB1, 0x02, /* Feature (Variable), */
381 0x85, 0x88, /* Report ID (136), */
382 0x09, 0x28, /* Usage (28h), */
383 0x95, 0x22, /* Report Count (34), */
384 0xB1, 0x02, /* Feature (Variable), */
385 0x85, 0x89, /* Report ID (137), */
386 0x09, 0x29, /* Usage (29h), */
387 0x95, 0x02, /* Report Count (2), */
388 0xB1, 0x02, /* Feature (Variable), */
389 0x85, 0x90, /* Report ID (144), */
390 0x09, 0x30, /* Usage (30h), */
391 0x95, 0x05, /* Report Count (5), */
392 0xB1, 0x02, /* Feature (Variable), */
393 0x85, 0x91, /* Report ID (145), */
394 0x09, 0x31, /* Usage (31h), */
395 0x95, 0x03, /* Report Count (3), */
396 0xB1, 0x02, /* Feature (Variable), */
397 0x85, 0x92, /* Report ID (146), */
398 0x09, 0x32, /* Usage (32h), */
399 0x95, 0x03, /* Report Count (3), */
400 0xB1, 0x02, /* Feature (Variable), */
401 0x85, 0x93, /* Report ID (147), */
402 0x09, 0x33, /* Usage (33h), */
403 0x95, 0x0C, /* Report Count (12), */
404 0xB1, 0x02, /* Feature (Variable), */
405 0x85, 0xA0, /* Report ID (160), */
406 0x09, 0x40, /* Usage (40h), */
407 0x95, 0x06, /* Report Count (6), */
408 0xB1, 0x02, /* Feature (Variable), */
409 0x85, 0xA1, /* Report ID (161), */
410 0x09, 0x41, /* Usage (41h), */
411 0x95, 0x01, /* Report Count (1), */
412 0xB1, 0x02, /* Feature (Variable), */
413 0x85, 0xA2, /* Report ID (162), */
414 0x09, 0x42, /* Usage (42h), */
415 0x95, 0x01, /* Report Count (1), */
416 0xB1, 0x02, /* Feature (Variable), */
417 0x85, 0xA3, /* Report ID (163), */
418 0x09, 0x43, /* Usage (43h), */
419 0x95, 0x30, /* Report Count (48), */
420 0xB1, 0x02, /* Feature (Variable), */
421 0x85, 0xA4, /* Report ID (164), */
422 0x09, 0x44, /* Usage (44h), */
423 0x95, 0x0D, /* Report Count (13), */
424 0xB1, 0x02, /* Feature (Variable), */
425 0x85, 0xA5, /* Report ID (165), */
426 0x09, 0x45, /* Usage (45h), */
427 0x95, 0x15, /* Report Count (21), */
428 0xB1, 0x02, /* Feature (Variable), */
429 0x85, 0xA6, /* Report ID (166), */
430 0x09, 0x46, /* Usage (46h), */
431 0x95, 0x15, /* Report Count (21), */
432 0xB1, 0x02, /* Feature (Variable), */
433 0x85, 0xF0, /* Report ID (240), */
434 0x09, 0x47, /* Usage (47h), */
435 0x95, 0x3F, /* Report Count (63), */
436 0xB1, 0x02, /* Feature (Variable), */
437 0x85, 0xF1, /* Report ID (241), */
438 0x09, 0x48, /* Usage (48h), */
439 0x95, 0x3F, /* Report Count (63), */
440 0xB1, 0x02, /* Feature (Variable), */
441 0x85, 0xF2, /* Report ID (242), */
442 0x09, 0x49, /* Usage (49h), */
443 0x95, 0x0F, /* Report Count (15), */
444 0xB1, 0x02, /* Feature (Variable), */
445 0x85, 0xA7, /* Report ID (167), */
446 0x09, 0x4A, /* Usage (4Ah), */
447 0x95, 0x01, /* Report Count (1), */
448 0xB1, 0x02, /* Feature (Variable), */
449 0x85, 0xA8, /* Report ID (168), */
450 0x09, 0x4B, /* Usage (4Bh), */
451 0x95, 0x01, /* Report Count (1), */
452 0xB1, 0x02, /* Feature (Variable), */
453 0x85, 0xA9, /* Report ID (169), */
454 0x09, 0x4C, /* Usage (4Ch), */
455 0x95, 0x08, /* Report Count (8), */
456 0xB1, 0x02, /* Feature (Variable), */
457 0x85, 0xAA, /* Report ID (170), */
458 0x09, 0x4E, /* Usage (4Eh), */
459 0x95, 0x01, /* Report Count (1), */
460 0xB1, 0x02, /* Feature (Variable), */
461 0x85, 0xAB, /* Report ID (171), */
462 0x09, 0x4F, /* Usage (4Fh), */
463 0x95, 0x39, /* Report Count (57), */
464 0xB1, 0x02, /* Feature (Variable), */
465 0x85, 0xAC, /* Report ID (172), */
466 0x09, 0x50, /* Usage (50h), */
467 0x95, 0x39, /* Report Count (57), */
468 0xB1, 0x02, /* Feature (Variable), */
469 0x85, 0xAD, /* Report ID (173), */
470 0x09, 0x51, /* Usage (51h), */
471 0x95, 0x0B, /* Report Count (11), */
472 0xB1, 0x02, /* Feature (Variable), */
473 0x85, 0xAE, /* Report ID (174), */
474 0x09, 0x52, /* Usage (52h), */
475 0x95, 0x01, /* Report Count (1), */
476 0xB1, 0x02, /* Feature (Variable), */
477 0x85, 0xAF, /* Report ID (175), */
478 0x09, 0x53, /* Usage (53h), */
479 0x95, 0x02, /* Report Count (2), */
480 0xB1, 0x02, /* Feature (Variable), */
481 0x85, 0xB0, /* Report ID (176), */
482 0x09, 0x54, /* Usage (54h), */
483 0x95, 0x3F, /* Report Count (63), */
484 0xB1, 0x02, /* Feature (Variable), */
485 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500486};
487
Frank Praznikad142b92014-02-20 11:36:00 -0500488/*
489 * The default behavior of the Dualshock 4 is to send reports using report
Frank Praznik077147a2014-09-14 11:56:39 -0400490 * type 1 when running over Bluetooth. However, when feature report 2 is
491 * requested during the controller initialization it starts sending input
492 * reports in report 17. Since report 17 is undefined in the default HID
Frank Praznikd8296742014-02-05 20:03:45 -0500493 * descriptor the button and axis definitions must be moved to report 17 or
Frank Praznik077147a2014-09-14 11:56:39 -0400494 * the HID layer won't process the received input.
Frank Praznikd8296742014-02-05 20:03:45 -0500495 */
496static u8 dualshock4_bt_rdesc[] = {
497 0x05, 0x01, /* Usage Page (Desktop), */
498 0x09, 0x05, /* Usage (Gamepad), */
499 0xA1, 0x01, /* Collection (Application), */
500 0x85, 0x01, /* Report ID (1), */
501 0x75, 0x08, /* Report Size (8), */
502 0x95, 0x0A, /* Report Count (9), */
503 0x81, 0x02, /* Input (Variable), */
504 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
505 0x85, 0x02, /* Report ID (2), */
506 0x09, 0x24, /* Usage (24h), */
507 0x95, 0x24, /* Report Count (36), */
508 0xB1, 0x02, /* Feature (Variable), */
509 0x85, 0xA3, /* Report ID (163), */
510 0x09, 0x25, /* Usage (25h), */
511 0x95, 0x30, /* Report Count (48), */
512 0xB1, 0x02, /* Feature (Variable), */
513 0x85, 0x05, /* Report ID (5), */
514 0x09, 0x26, /* Usage (26h), */
515 0x95, 0x28, /* Report Count (40), */
516 0xB1, 0x02, /* Feature (Variable), */
517 0x85, 0x06, /* Report ID (6), */
518 0x09, 0x27, /* Usage (27h), */
519 0x95, 0x34, /* Report Count (52), */
520 0xB1, 0x02, /* Feature (Variable), */
521 0x85, 0x07, /* Report ID (7), */
522 0x09, 0x28, /* Usage (28h), */
523 0x95, 0x30, /* Report Count (48), */
524 0xB1, 0x02, /* Feature (Variable), */
525 0x85, 0x08, /* Report ID (8), */
526 0x09, 0x29, /* Usage (29h), */
527 0x95, 0x2F, /* Report Count (47), */
528 0xB1, 0x02, /* Feature (Variable), */
529 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
530 0x85, 0x03, /* Report ID (3), */
531 0x09, 0x21, /* Usage (21h), */
532 0x95, 0x26, /* Report Count (38), */
533 0xB1, 0x02, /* Feature (Variable), */
534 0x85, 0x04, /* Report ID (4), */
535 0x09, 0x22, /* Usage (22h), */
536 0x95, 0x2E, /* Report Count (46), */
537 0xB1, 0x02, /* Feature (Variable), */
538 0x85, 0xF0, /* Report ID (240), */
539 0x09, 0x47, /* Usage (47h), */
540 0x95, 0x3F, /* Report Count (63), */
541 0xB1, 0x02, /* Feature (Variable), */
542 0x85, 0xF1, /* Report ID (241), */
543 0x09, 0x48, /* Usage (48h), */
544 0x95, 0x3F, /* Report Count (63), */
545 0xB1, 0x02, /* Feature (Variable), */
546 0x85, 0xF2, /* Report ID (242), */
547 0x09, 0x49, /* Usage (49h), */
548 0x95, 0x0F, /* Report Count (15), */
549 0xB1, 0x02, /* Feature (Variable), */
550 0x85, 0x11, /* Report ID (17), */
551 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
552 0x09, 0x20, /* Usage (20h), */
553 0x95, 0x02, /* Report Count (2), */
554 0x81, 0x02, /* Input (Variable), */
555 0x05, 0x01, /* Usage Page (Desktop), */
556 0x09, 0x30, /* Usage (X), */
557 0x09, 0x31, /* Usage (Y), */
558 0x09, 0x32, /* Usage (Z), */
559 0x09, 0x35, /* Usage (Rz), */
560 0x15, 0x00, /* Logical Minimum (0), */
561 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
562 0x75, 0x08, /* Report Size (8), */
563 0x95, 0x04, /* Report Count (4), */
564 0x81, 0x02, /* Input (Variable), */
565 0x09, 0x39, /* Usage (Hat Switch), */
566 0x15, 0x00, /* Logical Minimum (0), */
567 0x25, 0x07, /* Logical Maximum (7), */
568 0x75, 0x04, /* Report Size (4), */
569 0x95, 0x01, /* Report Count (1), */
570 0x81, 0x42, /* Input (Variable, Null State), */
571 0x05, 0x09, /* Usage Page (Button), */
572 0x19, 0x01, /* Usage Minimum (01h), */
573 0x29, 0x0E, /* Usage Maximum (0Eh), */
574 0x15, 0x00, /* Logical Minimum (0), */
575 0x25, 0x01, /* Logical Maximum (1), */
576 0x75, 0x01, /* Report Size (1), */
577 0x95, 0x0E, /* Report Count (14), */
578 0x81, 0x02, /* Input (Variable), */
579 0x75, 0x06, /* Report Size (6), */
580 0x95, 0x01, /* Report Count (1), */
581 0x81, 0x01, /* Input (Constant), */
582 0x05, 0x01, /* Usage Page (Desktop), */
583 0x09, 0x33, /* Usage (Rx), */
584 0x09, 0x34, /* Usage (Ry), */
585 0x15, 0x00, /* Logical Minimum (0), */
586 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
587 0x75, 0x08, /* Report Size (8), */
588 0x95, 0x02, /* Report Count (2), */
589 0x81, 0x02, /* Input (Variable), */
590 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
591 0x09, 0x20, /* Usage (20h), */
592 0x95, 0x03, /* Report Count (3), */
593 0x81, 0x02, /* Input (Variable), */
594 0x05, 0x01, /* Usage Page (Desktop), */
595 0x19, 0x40, /* Usage Minimum (40h), */
596 0x29, 0x42, /* Usage Maximum (42h), */
597 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
598 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
599 0x75, 0x10, /* Report Size (16), */
600 0x95, 0x03, /* Report Count (3), */
601 0x81, 0x02, /* Input (Variable), */
602 0x19, 0x43, /* Usage Minimum (43h), */
603 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400604 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
605 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznikd8296742014-02-05 20:03:45 -0500606 0x95, 0x03, /* Report Count (3), */
607 0x81, 0x02, /* Input (Variable), */
608 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
609 0x09, 0x20, /* Usage (20h), */
610 0x15, 0x00, /* Logical Minimum (0), */
611 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
612 0x75, 0x08, /* Report Size (8), */
613 0x95, 0x31, /* Report Count (51), */
614 0x81, 0x02, /* Input (Variable), */
615 0x09, 0x21, /* Usage (21h), */
616 0x75, 0x08, /* Report Size (8), */
617 0x95, 0x4D, /* Report Count (77), */
618 0x91, 0x02, /* Output (Variable), */
619 0x85, 0x12, /* Report ID (18), */
620 0x09, 0x22, /* Usage (22h), */
621 0x95, 0x8D, /* Report Count (141), */
622 0x81, 0x02, /* Input (Variable), */
623 0x09, 0x23, /* Usage (23h), */
624 0x91, 0x02, /* Output (Variable), */
625 0x85, 0x13, /* Report ID (19), */
626 0x09, 0x24, /* Usage (24h), */
627 0x95, 0xCD, /* Report Count (205), */
628 0x81, 0x02, /* Input (Variable), */
629 0x09, 0x25, /* Usage (25h), */
630 0x91, 0x02, /* Output (Variable), */
631 0x85, 0x14, /* Report ID (20), */
632 0x09, 0x26, /* Usage (26h), */
633 0x96, 0x0D, 0x01, /* Report Count (269), */
634 0x81, 0x02, /* Input (Variable), */
635 0x09, 0x27, /* Usage (27h), */
636 0x91, 0x02, /* Output (Variable), */
637 0x85, 0x15, /* Report ID (21), */
638 0x09, 0x28, /* Usage (28h), */
639 0x96, 0x4D, 0x01, /* Report Count (333), */
640 0x81, 0x02, /* Input (Variable), */
641 0x09, 0x29, /* Usage (29h), */
642 0x91, 0x02, /* Output (Variable), */
643 0x85, 0x16, /* Report ID (22), */
644 0x09, 0x2A, /* Usage (2Ah), */
645 0x96, 0x8D, 0x01, /* Report Count (397), */
646 0x81, 0x02, /* Input (Variable), */
647 0x09, 0x2B, /* Usage (2Bh), */
648 0x91, 0x02, /* Output (Variable), */
649 0x85, 0x17, /* Report ID (23), */
650 0x09, 0x2C, /* Usage (2Ch), */
651 0x96, 0xCD, 0x01, /* Report Count (461), */
652 0x81, 0x02, /* Input (Variable), */
653 0x09, 0x2D, /* Usage (2Dh), */
654 0x91, 0x02, /* Output (Variable), */
655 0x85, 0x18, /* Report ID (24), */
656 0x09, 0x2E, /* Usage (2Eh), */
657 0x96, 0x0D, 0x02, /* Report Count (525), */
658 0x81, 0x02, /* Input (Variable), */
659 0x09, 0x2F, /* Usage (2Fh), */
660 0x91, 0x02, /* Output (Variable), */
661 0x85, 0x19, /* Report ID (25), */
662 0x09, 0x30, /* Usage (30h), */
663 0x96, 0x22, 0x02, /* Report Count (546), */
664 0x81, 0x02, /* Input (Variable), */
665 0x09, 0x31, /* Usage (31h), */
666 0x91, 0x02, /* Output (Variable), */
667 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
668 0x85, 0x82, /* Report ID (130), */
669 0x09, 0x22, /* Usage (22h), */
670 0x95, 0x3F, /* Report Count (63), */
671 0xB1, 0x02, /* Feature (Variable), */
672 0x85, 0x83, /* Report ID (131), */
673 0x09, 0x23, /* Usage (23h), */
674 0xB1, 0x02, /* Feature (Variable), */
675 0x85, 0x84, /* Report ID (132), */
676 0x09, 0x24, /* Usage (24h), */
677 0xB1, 0x02, /* Feature (Variable), */
678 0x85, 0x90, /* Report ID (144), */
679 0x09, 0x30, /* Usage (30h), */
680 0xB1, 0x02, /* Feature (Variable), */
681 0x85, 0x91, /* Report ID (145), */
682 0x09, 0x31, /* Usage (31h), */
683 0xB1, 0x02, /* Feature (Variable), */
684 0x85, 0x92, /* Report ID (146), */
685 0x09, 0x32, /* Usage (32h), */
686 0xB1, 0x02, /* Feature (Variable), */
687 0x85, 0x93, /* Report ID (147), */
688 0x09, 0x33, /* Usage (33h), */
689 0xB1, 0x02, /* Feature (Variable), */
690 0x85, 0xA0, /* Report ID (160), */
691 0x09, 0x40, /* Usage (40h), */
692 0xB1, 0x02, /* Feature (Variable), */
693 0x85, 0xA4, /* Report ID (164), */
694 0x09, 0x44, /* Usage (44h), */
695 0xB1, 0x02, /* Feature (Variable), */
696 0xC0 /* End Collection */
697};
698
Jiri Kosina078328d2013-06-13 12:03:49 +0200699static __u8 ps3remote_rdesc[] = {
700 0x05, 0x01, /* GUsagePage Generic Desktop */
701 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
702 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
703
704 /* Use collection 1 for joypad buttons */
705 0xA1, 0x02, /* MCollection Logical (interrelated data) */
706
707 /* Ignore the 1st byte, maybe it is used for a controller
708 * number but it's not needed for correct operation */
709 0x75, 0x08, /* GReportSize 0x08 [8] */
710 0x95, 0x01, /* GReportCount 0x01 [1] */
711 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
712
713 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
714 * buttons multiple keypresses are allowed */
715 0x05, 0x09, /* GUsagePage Button */
716 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
717 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
718 0x14, /* GLogicalMinimum [0] */
719 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
720 0x75, 0x01, /* GReportSize 0x01 [1] */
721 0x95, 0x18, /* GReportCount 0x18 [24] */
722 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
723
724 0xC0, /* MEndCollection */
725
726 /* Use collection 2 for remote control buttons */
727 0xA1, 0x02, /* MCollection Logical (interrelated data) */
728
729 /* 5th byte is used for remote control buttons */
730 0x05, 0x09, /* GUsagePage Button */
731 0x18, /* LUsageMinimum [No button pressed] */
732 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
733 0x14, /* GLogicalMinimum [0] */
734 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
735 0x75, 0x08, /* GReportSize 0x08 [8] */
736 0x95, 0x01, /* GReportCount 0x01 [1] */
737 0x80, /* MInput */
738
739 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
740 * 0xff and 11th is for press indication */
741 0x75, 0x08, /* GReportSize 0x08 [8] */
742 0x95, 0x06, /* GReportCount 0x06 [6] */
743 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
744
745 /* 12th byte is for battery strength */
746 0x05, 0x06, /* GUsagePage Generic Device Controls */
747 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
748 0x14, /* GLogicalMinimum [0] */
749 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
750 0x75, 0x08, /* GReportSize 0x08 [8] */
751 0x95, 0x01, /* GReportCount 0x01 [1] */
752 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
753
754 0xC0, /* MEndCollection */
755
756 0xC0 /* MEndCollection [Game Pad] */
757};
758
759static const unsigned int ps3remote_keymap_joypad_buttons[] = {
760 [0x01] = KEY_SELECT,
761 [0x02] = BTN_THUMBL, /* L3 */
762 [0x03] = BTN_THUMBR, /* R3 */
763 [0x04] = BTN_START,
764 [0x05] = KEY_UP,
765 [0x06] = KEY_RIGHT,
766 [0x07] = KEY_DOWN,
767 [0x08] = KEY_LEFT,
768 [0x09] = BTN_TL2, /* L2 */
769 [0x0a] = BTN_TR2, /* R2 */
770 [0x0b] = BTN_TL, /* L1 */
771 [0x0c] = BTN_TR, /* R1 */
772 [0x0d] = KEY_OPTION, /* options/triangle */
773 [0x0e] = KEY_BACK, /* back/circle */
774 [0x0f] = BTN_0, /* cross */
775 [0x10] = KEY_SCREEN, /* view/square */
776 [0x11] = KEY_HOMEPAGE, /* PS button */
777 [0x14] = KEY_ENTER,
778};
779static const unsigned int ps3remote_keymap_remote_buttons[] = {
780 [0x00] = KEY_1,
781 [0x01] = KEY_2,
782 [0x02] = KEY_3,
783 [0x03] = KEY_4,
784 [0x04] = KEY_5,
785 [0x05] = KEY_6,
786 [0x06] = KEY_7,
787 [0x07] = KEY_8,
788 [0x08] = KEY_9,
789 [0x09] = KEY_0,
790 [0x0e] = KEY_ESC, /* return */
791 [0x0f] = KEY_CLEAR,
792 [0x16] = KEY_EJECTCD,
793 [0x1a] = KEY_MENU, /* top menu */
794 [0x28] = KEY_TIME,
795 [0x30] = KEY_PREVIOUS,
796 [0x31] = KEY_NEXT,
797 [0x32] = KEY_PLAY,
798 [0x33] = KEY_REWIND, /* scan back */
799 [0x34] = KEY_FORWARD, /* scan forward */
800 [0x38] = KEY_STOP,
801 [0x39] = KEY_PAUSE,
802 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
803 [0x60] = KEY_FRAMEBACK, /* slow/step back */
804 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
805 [0x63] = KEY_SUBTITLE,
806 [0x64] = KEY_AUDIO,
807 [0x65] = KEY_ANGLE,
808 [0x70] = KEY_INFO, /* display */
809 [0x80] = KEY_BLUE,
810 [0x81] = KEY_RED,
811 [0x82] = KEY_GREEN,
812 [0x83] = KEY_YELLOW,
813};
814
Colin Leitnerf04d5142013-05-27 23:41:05 +0200815static const unsigned int buzz_keymap[] = {
Frank Praznikad142b92014-02-20 11:36:00 -0500816 /*
817 * The controller has 4 remote buzzers, each with one LED and 5
Colin Leitnerf04d5142013-05-27 23:41:05 +0200818 * buttons.
819 *
820 * We use the mapping chosen by the controller, which is:
821 *
822 * Key Offset
823 * -------------------
824 * Buzz 1
825 * Blue 5
826 * Orange 4
827 * Green 3
828 * Yellow 2
829 *
830 * So, for example, the orange button on the third buzzer is mapped to
831 * BTN_TRIGGER_HAPPY14
832 */
833 [ 1] = BTN_TRIGGER_HAPPY1,
834 [ 2] = BTN_TRIGGER_HAPPY2,
835 [ 3] = BTN_TRIGGER_HAPPY3,
836 [ 4] = BTN_TRIGGER_HAPPY4,
837 [ 5] = BTN_TRIGGER_HAPPY5,
838 [ 6] = BTN_TRIGGER_HAPPY6,
839 [ 7] = BTN_TRIGGER_HAPPY7,
840 [ 8] = BTN_TRIGGER_HAPPY8,
841 [ 9] = BTN_TRIGGER_HAPPY9,
842 [10] = BTN_TRIGGER_HAPPY10,
843 [11] = BTN_TRIGGER_HAPPY11,
844 [12] = BTN_TRIGGER_HAPPY12,
845 [13] = BTN_TRIGGER_HAPPY13,
846 [14] = BTN_TRIGGER_HAPPY14,
847 [15] = BTN_TRIGGER_HAPPY15,
848 [16] = BTN_TRIGGER_HAPPY16,
849 [17] = BTN_TRIGGER_HAPPY17,
850 [18] = BTN_TRIGGER_HAPPY18,
851 [19] = BTN_TRIGGER_HAPPY19,
852 [20] = BTN_TRIGGER_HAPPY20,
853};
854
Frank Praznikd902f472014-01-27 10:17:36 -0500855static enum power_supply_property sony_battery_props[] = {
856 POWER_SUPPLY_PROP_PRESENT,
857 POWER_SUPPLY_PROP_CAPACITY,
858 POWER_SUPPLY_PROP_SCOPE,
859 POWER_SUPPLY_PROP_STATUS,
860};
861
Frank Praznik55d3b662014-04-14 10:11:32 -0400862struct sixaxis_led {
863 __u8 time_enabled; /* the total time the led is active (0xff means forever) */
864 __u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
865 __u8 enabled;
866 __u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
867 __u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
868} __packed;
869
870struct sixaxis_rumble {
871 __u8 padding;
872 __u8 right_duration; /* Right motor duration (0xff means forever) */
873 __u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
874 __u8 left_duration; /* Left motor duration (0xff means forever) */
875 __u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
876} __packed;
877
878struct sixaxis_output_report {
879 __u8 report_id;
880 struct sixaxis_rumble rumble;
881 __u8 padding[4];
882 __u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
883 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
884 struct sixaxis_led _reserved; /* LED5, not actually soldered */
885} __packed;
886
887union sixaxis_output_report_01 {
888 struct sixaxis_output_report data;
889 __u8 buf[36];
890};
891
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400892struct motion_output_report_02 {
893 u8 type, zero;
894 u8 r, g, b;
895 u8 zero2;
896 u8 rumble;
897};
898
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500899#define DS4_REPORT_0x02_SIZE 37
900#define DS4_REPORT_0x05_SIZE 32
901#define DS4_REPORT_0x11_SIZE 78
902#define DS4_REPORT_0x81_SIZE 7
Antonio Ospite29b691a2015-02-16 18:12:21 +0100903#define SIXAXIS_REPORT_0xF2_SIZE 17
Antonio Ospitea85d67b2015-02-16 18:12:22 +0100904#define SIXAXIS_REPORT_0xF5_SIZE 8
Simon Wood41d2d422015-06-09 21:27:06 -0600905#define MOTION_REPORT_0x02_SIZE 49
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500906
Jiri Kosina8b402c92015-02-23 11:15:44 +0100907static DEFINE_SPINLOCK(sony_dev_list_lock);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500908static LIST_HEAD(sony_device_list);
Frank Praznik80250872014-04-14 10:11:35 -0400909static DEFINE_IDA(sony_device_id_allocator);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500910
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200911struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500912 spinlock_t lock;
Frank Praznikd2d782f2014-02-20 11:36:03 -0500913 struct list_head list_node;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100914 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500915 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200916 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100917 struct work_struct state_worker;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100918 struct power_supply *battery;
919 struct power_supply_desc battery_desc;
Frank Praznik80250872014-04-14 10:11:35 -0400920 int device_id;
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500921 __u8 *output_report_dmabuf;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200922
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100923#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100924 __u8 left;
925 __u8 right;
926#endif
927
Frank Praznikd2d782f2014-02-20 11:36:03 -0500928 __u8 mac_address[6];
Frank Praznik5f5750d2014-02-19 13:09:22 -0500929 __u8 worker_initialized;
Frank Praznikd902f472014-01-27 10:17:36 -0500930 __u8 cable_state;
931 __u8 battery_charging;
932 __u8 battery_capacity;
Frank Praznik60781cf2014-01-11 15:13:15 -0500933 __u8 led_state[MAX_LEDS];
Frank Praznikb3ed4582014-04-14 10:11:36 -0400934 __u8 led_delay_on[MAX_LEDS];
935 __u8 led_delay_off[MAX_LEDS];
Frank Praznik60781cf2014-01-11 15:13:15 -0500936 __u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200937};
938
Antonio Ospitec607fb82014-06-24 13:28:41 +0200939static __u8 *sixaxis_fixup(struct hid_device *hdev, __u8 *rdesc,
940 unsigned int *rsize)
941{
942 *rsize = sizeof(sixaxis_rdesc);
943 return sixaxis_rdesc;
944}
945
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400946static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
947 unsigned int *rsize)
948{
949 *rsize = sizeof(motion_rdesc);
950 return motion_rdesc;
951}
952
Jiri Kosina078328d2013-06-13 12:03:49 +0200953static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
954 unsigned int *rsize)
955{
956 *rsize = sizeof(ps3remote_rdesc);
957 return ps3remote_rdesc;
958}
959
960static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
961 struct hid_field *field, struct hid_usage *usage,
962 unsigned long **bit, int *max)
963{
964 unsigned int key = usage->hid & HID_USAGE;
965
966 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
967 return -1;
968
969 switch (usage->collection_index) {
970 case 1:
971 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
972 return -1;
973
974 key = ps3remote_keymap_joypad_buttons[key];
975 if (!key)
976 return -1;
977 break;
978 case 2:
979 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
980 return -1;
981
982 key = ps3remote_keymap_remote_buttons[key];
983 if (!key)
984 return -1;
985 break;
986 default:
987 return -1;
988 }
989
990 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
991 return 1;
992}
993
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400994static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
995 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200996{
997 struct sony_sc *sc = hid_get_drvdata(hdev);
998
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900999 /*
1000 * Some Sony RF receivers wrongly declare the mouse pointer as a
1001 * a constant non-data variable.
1002 */
1003 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
1004 /* usage page: generic desktop controls */
1005 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
1006 /* usage: mouse */
1007 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
1008 /* input (usage page for x,y axes): constant, variable, relative */
1009 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa46491842013-01-15 19:40:48 +09001010 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +09001011 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001012 rdesc[55] = 0x06;
1013 }
Simon Wood61ab44b2011-06-10 12:00:26 +02001014
Frank Prazniked19d8c2014-01-16 21:43:12 -05001015 /*
1016 * The default Dualshock 4 USB descriptor doesn't assign
1017 * the gyroscope values to corresponding axes so we need a
1018 * modified one.
1019 */
1020 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
1021 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
1022 rdesc = dualshock4_usb_rdesc;
1023 *rsize = sizeof(dualshock4_usb_rdesc);
Frank Praznikd8296742014-02-05 20:03:45 -05001024 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
1025 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
1026 rdesc = dualshock4_bt_rdesc;
1027 *rsize = sizeof(dualshock4_bt_rdesc);
Frank Prazniked19d8c2014-01-16 21:43:12 -05001028 }
1029
Antonio Ospitec607fb82014-06-24 13:28:41 +02001030 if (sc->quirks & SIXAXIS_CONTROLLER)
1031 return sixaxis_fixup(hdev, rdesc, rsize);
Jiri Kosina078328d2013-06-13 12:03:49 +02001032
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001033 if (sc->quirks & MOTION_CONTROLLER)
1034 return motion_fixup(hdev, rdesc, rsize);
1035
Jiri Kosina078328d2013-06-13 12:03:49 +02001036 if (sc->quirks & PS3REMOTE)
1037 return ps3remote_fixup(hdev, rdesc, rsize);
1038
Nikolai Kondrashov73e40082010-08-06 23:03:06 +04001039 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001040}
1041
Frank Praznikd902f472014-01-27 10:17:36 -05001042static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1043{
1044 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
1045 unsigned long flags;
Simon Wood12e9a6d72015-06-09 21:27:05 -06001046 int offset;
Frank Praznikd902f472014-01-27 10:17:36 -05001047 __u8 cable_state, battery_capacity, battery_charging;
1048
Frank Praznikad142b92014-02-20 11:36:00 -05001049 /*
1050 * The sixaxis is charging if the battery value is 0xee
Frank Praznikd902f472014-01-27 10:17:36 -05001051 * and it is fully charged if the value is 0xef.
1052 * It does not report the actual level while charging so it
1053 * is set to 100% while charging is in progress.
1054 */
Simon Wood12e9a6d72015-06-09 21:27:05 -06001055 offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
1056
1057 if (rd[offset] >= 0xee) {
Frank Praznikd902f472014-01-27 10:17:36 -05001058 battery_capacity = 100;
Simon Wood12e9a6d72015-06-09 21:27:05 -06001059 battery_charging = !(rd[offset] & 0x01);
Frank Praznik9fddd742014-08-29 13:11:52 -04001060 cable_state = 1;
Frank Praznikd902f472014-01-27 10:17:36 -05001061 } else {
Simon Wood12e9a6d72015-06-09 21:27:05 -06001062 __u8 index = rd[offset] <= 5 ? rd[offset] : 5;
Frank Praznikac3c9a92014-02-20 11:36:02 -05001063 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -05001064 battery_charging = 0;
Frank Praznik9fddd742014-08-29 13:11:52 -04001065 cable_state = 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001066 }
Frank Praznikd902f472014-01-27 10:17:36 -05001067
1068 spin_lock_irqsave(&sc->lock, flags);
1069 sc->cable_state = cable_state;
1070 sc->battery_capacity = battery_capacity;
1071 sc->battery_charging = battery_charging;
1072 spin_unlock_irqrestore(&sc->lock, flags);
1073}
1074
1075static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1076{
Frank Praznike5606232014-01-27 10:17:37 -05001077 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1078 struct hid_input, list);
1079 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -05001080 unsigned long flags;
Frank Praznik6c5f8602014-02-05 20:03:47 -05001081 int n, offset;
Frank Praznikd902f472014-01-27 10:17:36 -05001082 __u8 cable_state, battery_capacity, battery_charging;
1083
Frank Praznikad142b92014-02-20 11:36:00 -05001084 /*
1085 * Battery and touchpad data starts at byte 30 in the USB report and
Frank Praznik6c5f8602014-02-05 20:03:47 -05001086 * 32 in Bluetooth report.
1087 */
1088 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
1089
Frank Praznikad142b92014-02-20 11:36:00 -05001090 /*
1091 * The lower 4 bits of byte 30 contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -05001092 * and the 5th bit contains the USB cable state.
1093 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001094 cable_state = (rd[offset] >> 4) & 0x01;
1095 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -05001096
Frank Praznikad142b92014-02-20 11:36:00 -05001097 /*
1098 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -05001099 * 0 to 10, and when running on battery power it ranges from 0 to 9.
1100 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -05001101 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001102 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -05001103 battery_charging = 0;
1104 else
1105 battery_charging = 1;
1106
Frank Praznik6c5f8602014-02-05 20:03:47 -05001107 if (!cable_state)
1108 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -05001109 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -05001110 battery_capacity = 10;
1111
Frank Praznikd902f472014-01-27 10:17:36 -05001112 battery_capacity *= 10;
1113
1114 spin_lock_irqsave(&sc->lock, flags);
1115 sc->cable_state = cable_state;
1116 sc->battery_capacity = battery_capacity;
1117 sc->battery_charging = battery_charging;
1118 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -05001119
Frank Praznik6c5f8602014-02-05 20:03:47 -05001120 offset += 5;
1121
Frank Praznikad142b92014-02-20 11:36:00 -05001122 /*
1123 * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
Frank Praznik6c5f8602014-02-05 20:03:47 -05001124 * and 37 on Bluetooth.
Frank Praznike5606232014-01-27 10:17:37 -05001125 * The first 7 bits of the first byte is a counter and bit 8 is a touch
1126 * indicator that is 0 when pressed and 1 when not pressed.
1127 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1128 * The data for the second touch is in the same format and immediatly
1129 * follows the data for the first.
1130 */
1131 for (n = 0; n < 2; n++) {
1132 __u16 x, y;
1133
1134 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1135 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1136
1137 input_mt_slot(input_dev, n);
1138 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
1139 !(rd[offset] >> 7));
1140 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1141 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1142
1143 offset += 4;
1144 }
Frank Praznikd902f472014-01-27 10:17:36 -05001145}
1146
Simon Woodc9e4d872011-06-10 12:00:27 +02001147static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
1148 __u8 *rd, int size)
1149{
1150 struct sony_sc *sc = hid_get_drvdata(hdev);
1151
Frank Praznikad142b92014-02-20 11:36:00 -05001152 /*
1153 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +02001154 * has to be BYTE_SWAPPED before passing up to joystick interface
1155 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001156 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Simon Woodc9e4d872011-06-10 12:00:27 +02001157 swap(rd[41], rd[42]);
1158 swap(rd[43], rd[44]);
1159 swap(rd[45], rd[46]);
1160 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -05001161
1162 sixaxis_parse_report(sc, rd, size);
Simon Wood12e9a6d72015-06-09 21:27:05 -06001163 } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
1164 sixaxis_parse_report(sc, rd, size);
Frank Praznik68330d82014-02-05 20:03:49 -05001165 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1166 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1167 && rd[0] == 0x11 && size == 78)) {
Frank Praznikd902f472014-01-27 10:17:36 -05001168 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +02001169 }
1170
1171 return 0;
1172}
1173
Colin Leitnerf04d5142013-05-27 23:41:05 +02001174static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1175 struct hid_field *field, struct hid_usage *usage,
1176 unsigned long **bit, int *max)
1177{
1178 struct sony_sc *sc = hid_get_drvdata(hdev);
1179
1180 if (sc->quirks & BUZZ_CONTROLLER) {
1181 unsigned int key = usage->hid & HID_USAGE;
1182
1183 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1184 return -1;
1185
1186 switch (usage->collection_index) {
1187 case 1:
1188 if (key >= ARRAY_SIZE(buzz_keymap))
1189 return -1;
1190
1191 key = buzz_keymap[key];
1192 if (!key)
1193 return -1;
1194 break;
1195 default:
1196 return -1;
1197 }
1198
1199 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1200 return 1;
1201 }
1202
Jiri Kosina078328d2013-06-13 12:03:49 +02001203 if (sc->quirks & PS3REMOTE)
1204 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1205
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001206 /* Let hid-core decide for the others */
1207 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001208}
1209
Frank Praznikce8efc32014-09-18 21:15:01 -04001210static int sony_register_touchpad(struct hid_input *hi, int touch_count,
1211 int w, int h)
1212{
1213 struct input_dev *input_dev = hi->input;
1214 int ret;
1215
1216 ret = input_mt_init_slots(input_dev, touch_count, 0);
1217 if (ret < 0)
1218 return ret;
1219
1220 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1221 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1222
1223 return 0;
1224}
1225
1226static void sony_input_configured(struct hid_device *hdev,
1227 struct hid_input *hidinput)
1228{
1229 struct sony_sc *sc = hid_get_drvdata(hdev);
1230
1231 /*
1232 * The Dualshock 4 touchpad supports 2 touches and has a
Frank Praznik981c5b42014-09-24 09:38:19 -04001233 * resolution of 1920x942 (44.86 dots/mm).
Frank Praznikce8efc32014-09-18 21:15:01 -04001234 */
1235 if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik981c5b42014-09-24 09:38:19 -04001236 if (sony_register_touchpad(hidinput, 2, 1920, 942) != 0)
Frank Praznikce8efc32014-09-18 21:15:01 -04001237 hid_err(sc->hdev,
1238 "Unable to initialize multi-touch slots\n");
1239 }
1240}
1241
Antonio Ospite5710fab2011-02-20 18:26:45 +01001242/*
Jiri Slabybd28ce02008-06-25 23:47:04 +02001243 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1244 * to "operational". Without this, the ps3 controller will not report any
1245 * events.
1246 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001247static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001248{
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001249 const int buf_size =
1250 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
Antonio Ospite2e701a32015-02-16 18:12:24 +01001251 __u8 *buf;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001252 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001253
Antonio Ospite2e701a32015-02-16 18:12:24 +01001254 buf = kmalloc(buf_size, GFP_KERNEL);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001255 if (!buf)
1256 return -ENOMEM;
1257
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001258 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1259 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001260 if (ret < 0) {
1261 hid_err(hdev, "can't set operational mode: step 1\n");
1262 goto out;
1263 }
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001264
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001265 /*
1266 * Some compatible controllers like the Speedlink Strike FX and
1267 * Gasia need another query plus an USB interrupt to get operational.
1268 */
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001269 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1270 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001271 if (ret < 0) {
1272 hid_err(hdev, "can't set operational mode: step 2\n");
1273 goto out;
1274 }
1275
1276 ret = hid_hw_output_report(hdev, buf, 1);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001277 if (ret < 0)
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001278 hid_err(hdev, "can't set operational mode: step 3\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +02001279
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001280out:
Jiri Slabybd28ce02008-06-25 23:47:04 +02001281 kfree(buf);
1282
1283 return ret;
1284}
1285
Antonio Ospite816651a2010-05-03 22:15:55 +02001286static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001287{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001288 static const __u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1289 __u8 *buf;
1290 int ret;
1291
1292 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1293 if (!buf)
1294 return -ENOMEM;
1295
1296 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
Benjamin Tissoiresb0dd72a2014-02-10 12:58:54 -05001297 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001298
1299 kfree(buf);
1300
1301 return ret;
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001302}
1303
Frank Praznikad142b92014-02-20 11:36:00 -05001304/*
1305 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
Frank Praznik68330d82014-02-05 20:03:49 -05001306 * controller so that it sends full input reports of type 0x11.
1307 */
1308static int dualshock4_set_operational_bt(struct hid_device *hdev)
1309{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001310 __u8 *buf;
1311 int ret;
Frank Praznik68330d82014-02-05 20:03:49 -05001312
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001313 buf = kmalloc(DS4_REPORT_0x02_SIZE, GFP_KERNEL);
1314 if (!buf)
1315 return -ENOMEM;
1316
1317 ret = hid_hw_raw_request(hdev, 0x02, buf, DS4_REPORT_0x02_SIZE,
Frank Praznik68330d82014-02-05 20:03:49 -05001318 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001319
1320 kfree(buf);
1321
1322 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001323}
1324
Frank Praznik221399b2015-05-05 20:47:32 -04001325static void sixaxis_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001326{
1327 static const __u8 sixaxis_leds[10][4] = {
1328 { 0x01, 0x00, 0x00, 0x00 },
1329 { 0x00, 0x01, 0x00, 0x00 },
1330 { 0x00, 0x00, 0x01, 0x00 },
1331 { 0x00, 0x00, 0x00, 0x01 },
1332 { 0x01, 0x00, 0x00, 0x01 },
1333 { 0x00, 0x01, 0x00, 0x01 },
1334 { 0x00, 0x00, 0x01, 0x01 },
1335 { 0x01, 0x00, 0x01, 0x01 },
1336 { 0x00, 0x01, 0x01, 0x01 },
1337 { 0x01, 0x01, 0x01, 0x01 }
1338 };
1339
Frank Praznik221399b2015-05-05 20:47:32 -04001340 int id = sc->device_id;
1341
1342 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001343
1344 if (id < 0)
1345 return;
1346
1347 id %= 10;
Frank Praznik221399b2015-05-05 20:47:32 -04001348 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001349}
1350
Frank Praznik221399b2015-05-05 20:47:32 -04001351static void dualshock4_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001352{
1353 /* The first 4 color/index entries match what the PS4 assigns */
1354 static const __u8 color_code[7][3] = {
1355 /* Blue */ { 0x00, 0x00, 0x01 },
1356 /* Red */ { 0x01, 0x00, 0x00 },
1357 /* Green */ { 0x00, 0x01, 0x00 },
1358 /* Pink */ { 0x02, 0x00, 0x01 },
1359 /* Orange */ { 0x02, 0x01, 0x00 },
1360 /* Teal */ { 0x00, 0x01, 0x01 },
1361 /* White */ { 0x01, 0x01, 0x01 }
1362 };
1363
Frank Praznik221399b2015-05-05 20:47:32 -04001364 int id = sc->device_id;
1365
1366 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001367
1368 if (id < 0)
1369 return;
1370
1371 id %= 7;
Frank Praznik221399b2015-05-05 20:47:32 -04001372 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001373}
1374
Frank Praznik221399b2015-05-05 20:47:32 -04001375static void buzz_set_leds(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001376{
Frank Praznik221399b2015-05-05 20:47:32 -04001377 struct hid_device *hdev = sc->hdev;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001378 struct list_head *report_list =
1379 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1380 struct hid_report *report = list_entry(report_list->next,
1381 struct hid_report, list);
1382 __s32 *value = report->field[0]->value;
1383
Frank Praznik221399b2015-05-05 20:47:32 -04001384 BUILD_BUG_ON(MAX_LEDS < 4);
1385
Colin Leitnerf04d5142013-05-27 23:41:05 +02001386 value[0] = 0x00;
Frank Praznik221399b2015-05-05 20:47:32 -04001387 value[1] = sc->led_state[0] ? 0xff : 0x00;
1388 value[2] = sc->led_state[1] ? 0xff : 0x00;
1389 value[3] = sc->led_state[2] ? 0xff : 0x00;
1390 value[4] = sc->led_state[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001391 value[5] = 0x00;
1392 value[6] = 0x00;
1393 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1394}
1395
Frank Praznik221399b2015-05-05 20:47:32 -04001396static void sony_set_leds(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001397{
Frank Praznik221399b2015-05-05 20:47:32 -04001398 if (!(sc->quirks & BUZZ_CONTROLLER))
Frank Praznikfa57a812014-04-14 10:11:33 -04001399 schedule_work(&sc->state_worker);
Frank Praznik221399b2015-05-05 20:47:32 -04001400 else
1401 buzz_set_leds(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001402}
1403
Sven Eckelmannc5382512013-11-19 20:26:30 +01001404static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001405 enum led_brightness value)
1406{
1407 struct device *dev = led->dev->parent;
1408 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1409 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001410
1411 int n;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001412 int force_update;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001413
1414 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001415 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001416 hid_err(hdev, "No device data\n");
1417 return;
1418 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001419
Frank Praznikb3ed4582014-04-14 10:11:36 -04001420 /*
1421 * The Sixaxis on USB will override any LED settings sent to it
1422 * and keep flashing all of the LEDs until the PS button is pressed.
1423 * Updates, even if redundant, must be always be sent to the
1424 * controller to avoid having to toggle the state of an LED just to
1425 * stop the flashing later on.
1426 */
1427 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1428
Frank Praznik60781cf2014-01-11 15:13:15 -05001429 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznikb3ed4582014-04-14 10:11:36 -04001430 if (led == drv_data->leds[n] && (force_update ||
1431 (value != drv_data->led_state[n] ||
1432 drv_data->led_delay_on[n] ||
1433 drv_data->led_delay_off[n]))) {
1434
1435 drv_data->led_state[n] = value;
1436
1437 /* Setting the brightness stops the blinking */
1438 drv_data->led_delay_on[n] = 0;
1439 drv_data->led_delay_off[n] = 0;
1440
Frank Praznik221399b2015-05-05 20:47:32 -04001441 sony_set_leds(drv_data);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001442 break;
1443 }
1444 }
1445}
1446
Sven Eckelmannc5382512013-11-19 20:26:30 +01001447static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001448{
1449 struct device *dev = led->dev->parent;
1450 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1451 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001452
1453 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001454
1455 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001456 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001457 hid_err(hdev, "No device data\n");
1458 return LED_OFF;
1459 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001460
Frank Praznik60781cf2014-01-11 15:13:15 -05001461 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001462 if (led == drv_data->leds[n])
1463 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001464 }
1465
Simon Wood7db75042014-02-05 12:34:18 -07001466 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001467}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001468
Frank Praznikb3ed4582014-04-14 10:11:36 -04001469static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1470 unsigned long *delay_off)
1471{
1472 struct device *dev = led->dev->parent;
1473 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1474 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1475 int n;
1476 __u8 new_on, new_off;
1477
1478 if (!drv_data) {
1479 hid_err(hdev, "No device data\n");
1480 return -EINVAL;
1481 }
1482
1483 /* Max delay is 255 deciseconds or 2550 milliseconds */
1484 if (*delay_on > 2550)
1485 *delay_on = 2550;
1486 if (*delay_off > 2550)
1487 *delay_off = 2550;
1488
1489 /* Blink at 1 Hz if both values are zero */
1490 if (!*delay_on && !*delay_off)
1491 *delay_on = *delay_off = 500;
1492
1493 new_on = *delay_on / 10;
1494 new_off = *delay_off / 10;
1495
1496 for (n = 0; n < drv_data->led_count; n++) {
1497 if (led == drv_data->leds[n])
1498 break;
1499 }
1500
1501 /* This LED is not registered on this device */
1502 if (n >= drv_data->led_count)
1503 return -EINVAL;
1504
1505 /* Don't schedule work if the values didn't change */
1506 if (new_on != drv_data->led_delay_on[n] ||
1507 new_off != drv_data->led_delay_off[n]) {
1508 drv_data->led_delay_on[n] = new_on;
1509 drv_data->led_delay_off[n] = new_off;
1510 schedule_work(&drv_data->state_worker);
1511 }
1512
1513 return 0;
1514}
1515
Frank Praznikfa57a812014-04-14 10:11:33 -04001516static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001517{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001518 struct led_classdev *led;
1519 int n;
1520
Frank Praznikfa57a812014-04-14 10:11:33 -04001521 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001522
Frank Praznikfa57a812014-04-14 10:11:33 -04001523 for (n = 0; n < sc->led_count; n++) {
1524 led = sc->leds[n];
1525 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001526 if (!led)
1527 continue;
1528 led_classdev_unregister(led);
1529 kfree(led);
1530 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001531
Frank Praznikfa57a812014-04-14 10:11:33 -04001532 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001533}
1534
Frank Praznikfa57a812014-04-14 10:11:33 -04001535static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001536{
Frank Praznikfa57a812014-04-14 10:11:33 -04001537 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001538 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001539 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001540 struct led_classdev *led;
1541 size_t name_sz;
1542 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001543 size_t name_len;
1544 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001545 static const char * const ds4_name_str[] = { "red", "green", "blue",
1546 "global" };
Frank Praznik5607c892014-07-29 19:55:48 -07001547 __u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
Frank Praznikb3ed4582014-04-14 10:11:36 -04001548 __u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001549
Frank Praznikfa57a812014-04-14 10:11:33 -04001550 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001551
Frank Praznikfa57a812014-04-14 10:11:33 -04001552 if (sc->quirks & BUZZ_CONTROLLER) {
1553 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001554 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001555 name_len = strlen("::buzz#");
1556 name_fmt = "%s::buzz%d";
1557 /* Validate expected report characteristics. */
1558 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1559 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001560 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik221399b2015-05-05 20:47:32 -04001561 dualshock4_set_leds_from_id(sc);
1562 sc->led_state[3] = 1;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001563 sc->led_count = 4;
1564 memset(max_brightness, 255, 3);
1565 use_hw_blink[3] = 1;
1566 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001567 name_len = 0;
1568 name_fmt = "%s:%s";
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001569 } else if (sc->quirks & MOTION_CONTROLLER) {
1570 sc->led_count = 3;
1571 memset(max_brightness, 255, 3);
1572 use_ds4_names = 1;
1573 name_len = 0;
1574 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -05001575 } else {
Frank Praznik221399b2015-05-05 20:47:32 -04001576 sixaxis_set_leds_from_id(sc);
Frank Praznikfa57a812014-04-14 10:11:33 -04001577 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001578 memset(use_hw_blink, 1, 4);
1579 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001580 name_len = strlen("::sony#");
1581 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001582 }
1583
Frank Praznikad142b92014-02-20 11:36:00 -05001584 /*
1585 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001586 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001587 * LEDs to on
1588 */
Frank Praznik221399b2015-05-05 20:47:32 -04001589 sony_set_leds(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001590
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001591 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001592
Frank Praznikfa57a812014-04-14 10:11:33 -04001593 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001594
Frank Praznikb3ed4582014-04-14 10:11:36 -04001595 if (use_ds4_names)
1596 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001597
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001598 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1599 if (!led) {
1600 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001601 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001602 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001603 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001604
1605 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001606 if (use_ds4_names)
1607 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1608 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001609 else
1610 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001611 led->name = name;
Frank Praznik221399b2015-05-05 20:47:32 -04001612 led->brightness = sc->led_state[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001613 led->max_brightness = max_brightness[n];
Sven Eckelmannc5382512013-11-19 20:26:30 +01001614 led->brightness_get = sony_led_get_brightness;
1615 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001616
Frank Praznikb3ed4582014-04-14 10:11:36 -04001617 if (use_hw_blink[n])
1618 led->blink_set = sony_led_blink_set;
1619
Frank Praznik80250872014-04-14 10:11:35 -04001620 sc->leds[n] = led;
1621
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001622 ret = led_classdev_register(&hdev->dev, led);
1623 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001624 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001625 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001626 kfree(led);
1627 goto error_leds;
1628 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001629 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001630
1631 return ret;
1632
Colin Leitnerf04d5142013-05-27 23:41:05 +02001633error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001634 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001635
Colin Leitnerf04d5142013-05-27 23:41:05 +02001636 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001637}
1638
Frank Praznikcad665a2014-01-11 15:13:54 -05001639static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001640{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001641 static const union sixaxis_output_report_01 default_report = {
Frank Praznik55d3b662014-04-14 10:11:32 -04001642 .buf = {
1643 0x01,
1644 0x00, 0xff, 0x00, 0xff, 0x00,
1645 0x00, 0x00, 0x00, 0x00, 0x00,
1646 0xff, 0x27, 0x10, 0x00, 0x32,
1647 0xff, 0x27, 0x10, 0x00, 0x32,
1648 0xff, 0x27, 0x10, 0x00, 0x32,
1649 0xff, 0x27, 0x10, 0x00, 0x32,
1650 0x00, 0x00, 0x00, 0x00, 0x00
1651 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001652 };
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001653 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1654 struct sixaxis_output_report *report =
1655 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1656 int n;
1657
1658 /* Initialize the report with default values */
1659 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001660
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001661#ifdef CONFIG_SONY_FF
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001662 report->rumble.right_motor_on = sc->right ? 1 : 0;
1663 report->rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001664#endif
1665
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001666 report->leds_bitmap |= sc->led_state[0] << 1;
1667 report->leds_bitmap |= sc->led_state[1] << 2;
1668 report->leds_bitmap |= sc->led_state[2] << 3;
1669 report->leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001670
Simon Wood88f65762014-04-14 10:11:37 -04001671 /* Set flag for all leds off, required for 3rd party INTEC controller */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001672 if ((report->leds_bitmap & 0x1E) == 0)
1673 report->leds_bitmap |= 0x20;
Simon Wood88f65762014-04-14 10:11:37 -04001674
Frank Praznikb3ed4582014-04-14 10:11:36 -04001675 /*
1676 * The LEDs in the report are indexed in reverse order to their
1677 * corresponding light on the controller.
1678 * Index 0 = LED 4, index 1 = LED 3, etc...
1679 *
1680 * In the case of both delay values being zero (blinking disabled) the
1681 * default report values should be used or the controller LED will be
1682 * always off.
1683 */
1684 for (n = 0; n < 4; n++) {
1685 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001686 report->led[3 - n].duty_off = sc->led_delay_off[n];
1687 report->led[3 - n].duty_on = sc->led_delay_on[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001688 }
1689 }
1690
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001691 hid_hw_raw_request(sc->hdev, report->report_id, (__u8 *)report,
1692 sizeof(struct sixaxis_output_report),
1693 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001694}
1695
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001696static void dualshock4_state_worker(struct work_struct *work)
1697{
1698 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001699 struct hid_device *hdev = sc->hdev;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001700 __u8 *buf = sc->output_report_dmabuf;
Frank Praznik48220232014-02-05 20:03:44 -05001701 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001702
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001703 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001704 memset(buf, 0, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001705 buf[0] = 0x05;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001706 buf[1] = 0xFF;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001707 offset = 4;
1708 } else {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001709 memset(buf, 0, DS4_REPORT_0x11_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001710 buf[0] = 0x11;
1711 buf[1] = 0xB0;
1712 buf[3] = 0x0F;
1713 offset = 6;
1714 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001715
1716#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001717 buf[offset++] = sc->right;
1718 buf[offset++] = sc->left;
1719#else
1720 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001721#endif
1722
Frank Praznikb3ed4582014-04-14 10:11:36 -04001723 /* LED 3 is the global control */
1724 if (sc->led_state[3]) {
1725 buf[offset++] = sc->led_state[0];
1726 buf[offset++] = sc->led_state[1];
1727 buf[offset++] = sc->led_state[2];
1728 } else {
1729 offset += 3;
1730 }
1731
1732 /* If both delay values are zero the DualShock 4 disables blinking. */
1733 buf[offset++] = sc->led_delay_on[3];
1734 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05001735
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001736 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001737 hid_hw_output_report(hdev, buf, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001738 else
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001739 hid_hw_raw_request(hdev, 0x11, buf, DS4_REPORT_0x11_SIZE,
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001740 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001741}
1742
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001743static void motion_state_worker(struct work_struct *work)
1744{
1745 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1746 struct hid_device *hdev = sc->hdev;
1747 struct motion_output_report_02 *report =
1748 (struct motion_output_report_02 *)sc->output_report_dmabuf;
1749
Simon Wood41d2d422015-06-09 21:27:06 -06001750 memset(report, 0, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001751
1752 report->type = 0x02; /* set leds */
1753 report->r = sc->led_state[0];
1754 report->g = sc->led_state[1];
1755 report->b = sc->led_state[2];
1756
1757#ifdef CONFIG_SONY_FF
1758 report->rumble = max(sc->right, sc->left);
1759#endif
1760
Simon Wood41d2d422015-06-09 21:27:06 -06001761 hid_hw_output_report(hdev, (__u8 *)report, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001762}
1763
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001764static int sony_allocate_output_report(struct sony_sc *sc)
1765{
1766 if (sc->quirks & SIXAXIS_CONTROLLER)
1767 sc->output_report_dmabuf =
1768 kmalloc(sizeof(union sixaxis_output_report_01),
1769 GFP_KERNEL);
1770 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1771 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x11_SIZE,
1772 GFP_KERNEL);
1773 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1774 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x05_SIZE,
1775 GFP_KERNEL);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001776 else if (sc->quirks & MOTION_CONTROLLER)
Simon Wood41d2d422015-06-09 21:27:06 -06001777 sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE,
1778 GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001779 else
1780 return 0;
1781
1782 if (!sc->output_report_dmabuf)
1783 return -ENOMEM;
1784
1785 return 0;
1786}
1787
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001788#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001789static int sony_play_effect(struct input_dev *dev, void *data,
1790 struct ff_effect *effect)
1791{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001792 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001793 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001794
1795 if (effect->type != FF_RUMBLE)
1796 return 0;
1797
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001798 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001799 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001800
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001801 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001802 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001803}
1804
Frank Praznikfa57a812014-04-14 10:11:33 -04001805static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001806{
Frank Praznikfa57a812014-04-14 10:11:33 -04001807 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001808 struct hid_input, list);
1809 struct input_dev *input_dev = hidinput->input;
1810
1811 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1812 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1813}
1814
1815#else
Frank Praznikfa57a812014-04-14 10:11:33 -04001816static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001817{
1818 return 0;
1819}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001820
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001821#endif
1822
Frank Praznikd902f472014-01-27 10:17:36 -05001823static int sony_battery_get_property(struct power_supply *psy,
1824 enum power_supply_property psp,
1825 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001826{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001827 struct sony_sc *sc = power_supply_get_drvdata(psy);
Frank Praznikd902f472014-01-27 10:17:36 -05001828 unsigned long flags;
1829 int ret = 0;
1830 u8 battery_charging, battery_capacity, cable_state;
1831
1832 spin_lock_irqsave(&sc->lock, flags);
1833 battery_charging = sc->battery_charging;
1834 battery_capacity = sc->battery_capacity;
1835 cable_state = sc->cable_state;
1836 spin_unlock_irqrestore(&sc->lock, flags);
1837
1838 switch (psp) {
1839 case POWER_SUPPLY_PROP_PRESENT:
1840 val->intval = 1;
1841 break;
1842 case POWER_SUPPLY_PROP_SCOPE:
1843 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1844 break;
1845 case POWER_SUPPLY_PROP_CAPACITY:
1846 val->intval = battery_capacity;
1847 break;
1848 case POWER_SUPPLY_PROP_STATUS:
1849 if (battery_charging)
1850 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1851 else
1852 if (battery_capacity == 100 && cable_state)
1853 val->intval = POWER_SUPPLY_STATUS_FULL;
1854 else
1855 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1856 break;
1857 default:
1858 ret = -EINVAL;
1859 break;
1860 }
1861 return ret;
1862}
1863
1864static int sony_battery_probe(struct sony_sc *sc)
1865{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001866 struct power_supply_config psy_cfg = { .drv_data = sc, };
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001867 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05001868 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001869
Frank Praznikad142b92014-02-20 11:36:00 -05001870 /*
1871 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05001872 * if the battery is polled before the first device report is received.
1873 */
1874 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001875
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001876 sc->battery_desc.properties = sony_battery_props;
1877 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
1878 sc->battery_desc.get_property = sony_battery_get_property;
1879 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
1880 sc->battery_desc.use_for_apm = 0;
1881 sc->battery_desc.name = kasprintf(GFP_KERNEL,
1882 "sony_controller_battery_%pMR",
1883 sc->mac_address);
1884 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05001885 return -ENOMEM;
1886
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001887 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
1888 &psy_cfg);
1889 if (IS_ERR(sc->battery)) {
1890 ret = PTR_ERR(sc->battery);
Frank Praznikd902f472014-01-27 10:17:36 -05001891 hid_err(hdev, "Unable to register battery device\n");
1892 goto err_free;
1893 }
1894
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001895 power_supply_powers(sc->battery, &hdev->dev);
Frank Praznikd902f472014-01-27 10:17:36 -05001896 return 0;
1897
1898err_free:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001899 kfree(sc->battery_desc.name);
1900 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05001901 return ret;
1902}
1903
1904static void sony_battery_remove(struct sony_sc *sc)
1905{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001906 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05001907 return;
1908
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001909 power_supply_unregister(sc->battery);
1910 kfree(sc->battery_desc.name);
1911 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05001912}
1913
Frank Praznikd2d782f2014-02-20 11:36:03 -05001914/*
1915 * If a controller is plugged in via USB while already connected via Bluetooth
1916 * it will show up as two devices. A global list of connected controllers and
1917 * their MAC addresses is maintained to ensure that a device is only connected
1918 * once.
1919 */
1920static int sony_check_add_dev_list(struct sony_sc *sc)
1921{
1922 struct sony_sc *entry;
1923 unsigned long flags;
1924 int ret;
1925
1926 spin_lock_irqsave(&sony_dev_list_lock, flags);
1927
1928 list_for_each_entry(entry, &sony_device_list, list_node) {
1929 ret = memcmp(sc->mac_address, entry->mac_address,
1930 sizeof(sc->mac_address));
1931 if (!ret) {
1932 ret = -EEXIST;
1933 hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
1934 sc->mac_address);
1935 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001936 }
1937 }
1938
Frank Praznikd2d782f2014-02-20 11:36:03 -05001939 ret = 0;
1940 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001941
Frank Praznikd2d782f2014-02-20 11:36:03 -05001942unlock:
1943 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1944 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001945}
1946
Frank Praznikd2d782f2014-02-20 11:36:03 -05001947static void sony_remove_dev_list(struct sony_sc *sc)
1948{
1949 unsigned long flags;
1950
1951 if (sc->list_node.next) {
1952 spin_lock_irqsave(&sony_dev_list_lock, flags);
1953 list_del(&(sc->list_node));
1954 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1955 }
1956}
1957
1958static int sony_get_bt_devaddr(struct sony_sc *sc)
1959{
1960 int ret;
1961
1962 /* HIDP stores the device MAC address as a string in the uniq field. */
1963 ret = strlen(sc->hdev->uniq);
1964 if (ret != 17)
1965 return -EINVAL;
1966
1967 ret = sscanf(sc->hdev->uniq,
1968 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1969 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
1970 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
1971
1972 if (ret != 6)
1973 return -EINVAL;
1974
1975 return 0;
1976}
1977
1978static int sony_check_add(struct sony_sc *sc)
1979{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001980 __u8 *buf = NULL;
Frank Praznikd2d782f2014-02-20 11:36:03 -05001981 int n, ret;
1982
1983 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
Simon Wood12e9a6d72015-06-09 21:27:05 -06001984 (sc->quirks & MOTION_CONTROLLER_BT) ||
Frank Praznikd2d782f2014-02-20 11:36:03 -05001985 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
1986 /*
1987 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
1988 * address from the uniq string where HIDP stores it.
1989 * As uniq cannot be guaranteed to be a MAC address in all cases
1990 * a failure of this function should not prevent the connection.
1991 */
1992 if (sony_get_bt_devaddr(sc) < 0) {
1993 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
1994 return 0;
1995 }
1996 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001997 buf = kmalloc(DS4_REPORT_0x81_SIZE, GFP_KERNEL);
1998 if (!buf)
1999 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002000
2001 /*
2002 * The MAC address of a DS4 controller connected via USB can be
2003 * retrieved with feature report 0x81. The address begins at
2004 * offset 1.
2005 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002006 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
2007 DS4_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
2008 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002009
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002010 if (ret != DS4_REPORT_0x81_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002011 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002012 ret = ret < 0 ? ret : -EINVAL;
2013 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002014 }
2015
2016 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
2017 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002018 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2019 if (!buf)
2020 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002021
2022 /*
2023 * The MAC address of a Sixaxis controller connected via USB can
2024 * be retrieved with feature report 0xf2. The address begins at
2025 * offset 4.
2026 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002027 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2028 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2029 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002030
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002031 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002032 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002033 ret = ret < 0 ? ret : -EINVAL;
2034 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002035 }
2036
2037 /*
2038 * The Sixaxis device MAC in the report is big-endian and must
2039 * be byte-swapped.
2040 */
2041 for (n = 0; n < 6; n++)
2042 sc->mac_address[5-n] = buf[4+n];
2043 } else {
2044 return 0;
2045 }
2046
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002047 ret = sony_check_add_dev_list(sc);
2048
2049out_free:
2050
2051 kfree(buf);
2052
2053 return ret;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002054}
2055
Frank Praznik80250872014-04-14 10:11:35 -04002056static int sony_set_device_id(struct sony_sc *sc)
2057{
2058 int ret;
2059
2060 /*
2061 * Only DualShock 4 or Sixaxis controllers get an id.
2062 * All others are set to -1.
2063 */
2064 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2065 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2066 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2067 GFP_KERNEL);
2068 if (ret < 0) {
2069 sc->device_id = -1;
2070 return ret;
2071 }
2072 sc->device_id = ret;
2073 } else {
2074 sc->device_id = -1;
2075 }
2076
2077 return 0;
2078}
2079
2080static void sony_release_device_id(struct sony_sc *sc)
2081{
2082 if (sc->device_id >= 0) {
2083 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2084 sc->device_id = -1;
2085 }
2086}
2087
Frank Praznik46262042014-04-14 10:11:31 -04002088static inline void sony_init_work(struct sony_sc *sc,
2089 void (*worker)(struct work_struct *))
2090{
2091 if (!sc->worker_initialized)
2092 INIT_WORK(&sc->state_worker, worker);
2093
2094 sc->worker_initialized = 1;
2095}
2096
2097static inline void sony_cancel_work_sync(struct sony_sc *sc)
2098{
2099 if (sc->worker_initialized)
2100 cancel_work_sync(&sc->state_worker);
2101}
Frank Praznikd2d782f2014-02-20 11:36:03 -05002102
Jiri Slabybd28ce02008-06-25 23:47:04 +02002103static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2104{
2105 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002106 unsigned long quirks = id->driver_data;
2107 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02002108 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002109
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002110 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002111 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08002112 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002113 return -ENOMEM;
2114 }
2115
Frank Praznikb94993f2015-02-22 20:42:46 -05002116 spin_lock_init(&sc->lock);
2117
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002118 sc->quirks = quirks;
2119 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002120 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002121
Jiri Slabybd28ce02008-06-25 23:47:04 +02002122 ret = hid_parse(hdev);
2123 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002124 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002125 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002126 }
2127
Colin Leitnerf04d5142013-05-27 23:41:05 +02002128 if (sc->quirks & VAIO_RDESC_CONSTANT)
2129 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
Antonio Ospite50764652014-06-24 13:28:40 +02002130 else if (sc->quirks & SIXAXIS_CONTROLLER)
Colin Leitnerf04d5142013-05-27 23:41:05 +02002131 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2132
2133 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002134 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002135 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002136 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002137 }
2138
Frank Praznik80250872014-04-14 10:11:35 -04002139 ret = sony_set_device_id(sc);
2140 if (ret < 0) {
2141 hid_err(hdev, "failed to allocate the device id\n");
2142 goto err_stop;
2143 }
2144
Frank Praznik131a8a92015-05-05 20:47:28 -04002145 ret = sony_allocate_output_report(sc);
2146 if (ret < 0) {
2147 hid_err(hdev, "failed to allocate the output report buffer\n");
2148 goto err_stop;
2149 }
2150
Antonio Ospite569b10a2010-10-19 16:13:10 +02002151 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002152 /*
2153 * The Sony Sixaxis does not handle HID Output Reports on the
2154 * Interrupt EP like it could, so we need to force HID Output
2155 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2156 *
2157 * There is also another issue about HID Output Reports via USB,
2158 * the Sixaxis does not want the report_id as part of the data
2159 * packet, so we have to discard buf[0] when sending the actual
2160 * control message, even for numbered reports, humpf!
2161 */
2162 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2163 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
Antonio Ospite816651a2010-05-03 22:15:55 +02002164 ret = sixaxis_set_operational_usb(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04002165 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002166 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04002167 /*
2168 * The Sixaxis wants output reports sent on the ctrl endpoint
2169 * when connected via Bluetooth.
2170 */
2171 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Antonio Ospite816651a2010-05-03 22:15:55 +02002172 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04002173 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002174 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik68330d82014-02-05 20:03:49 -05002175 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04002176 /*
2177 * The DualShock 4 wants output reports sent on the ctrl
2178 * endpoint when connected via Bluetooth.
2179 */
2180 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Frank Praznik68330d82014-02-05 20:03:49 -05002181 ret = dualshock4_set_operational_bt(hdev);
2182 if (ret < 0) {
2183 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
2184 goto err_stop;
2185 }
2186 }
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002187
Frank Praznik46262042014-04-14 10:11:31 -04002188 sony_init_work(sc, dualshock4_state_worker);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002189 } else if (sc->quirks & MOTION_CONTROLLER) {
2190 sony_init_work(sc, motion_state_worker);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002191 } else {
2192 ret = 0;
2193 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00002194
Jiri Kosina4dfdc462008-12-30 00:49:59 +01002195 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002196 goto err_stop;
2197
Frank Praznikd2d782f2014-02-20 11:36:03 -05002198 ret = sony_check_add(sc);
2199 if (ret < 0)
2200 goto err_stop;
2201
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002202 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002203 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002204 if (ret < 0)
2205 goto err_stop;
2206 }
2207
Frank Praznikd902f472014-01-27 10:17:36 -05002208 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2209 ret = sony_battery_probe(sc);
2210 if (ret < 0)
2211 goto err_stop;
2212
2213 /* Open the device to receive reports with battery info */
2214 ret = hid_hw_open(hdev);
2215 if (ret < 0) {
2216 hid_err(hdev, "hw open failed\n");
2217 goto err_stop;
2218 }
2219 }
2220
Frank Praznikc8de9db2014-02-20 11:36:01 -05002221 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002222 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05002223 if (ret < 0)
2224 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05002225 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002226
Jiri Slabybd28ce02008-06-25 23:47:04 +02002227 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05002228err_close:
2229 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002230err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002231 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002232 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002233 if (sc->quirks & SONY_BATTERY_SUPPORT)
2234 sony_battery_remove(sc);
Frank Praznik46262042014-04-14 10:11:31 -04002235 sony_cancel_work_sync(sc);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002236 kfree(sc->output_report_dmabuf);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002237 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04002238 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002239 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002240 return ret;
2241}
2242
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002243static void sony_remove(struct hid_device *hdev)
2244{
Colin Leitnerf04d5142013-05-27 23:41:05 +02002245 struct sony_sc *sc = hid_get_drvdata(hdev);
2246
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002247 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002248 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02002249
Frank Praznikd902f472014-01-27 10:17:36 -05002250 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2251 hid_hw_close(hdev);
2252 sony_battery_remove(sc);
2253 }
2254
Frank Praznik46262042014-04-14 10:11:31 -04002255 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002256
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002257 kfree(sc->output_report_dmabuf);
2258
Frank Praznikd2d782f2014-02-20 11:36:03 -05002259 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002260
Frank Praznik80250872014-04-14 10:11:35 -04002261 sony_release_device_id(sc);
2262
Jiri Slabybd28ce02008-06-25 23:47:04 +02002263 hid_hw_stop(hdev);
2264}
2265
2266static const struct hid_device_id sony_devices[] = {
2267 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2268 .driver_data = SIXAXIS_CONTROLLER_USB },
2269 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2270 .driver_data = SIXAXIS_CONTROLLER_USB },
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002271 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002272 .driver_data = MOTION_CONTROLLER_USB },
Simon Wooda4afa852015-06-03 09:45:19 -06002273 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002274 .driver_data = MOTION_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002275 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2276 .driver_data = SIXAXIS_CONTROLLER_BT },
2277 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2278 .driver_data = VAIO_RDESC_CONSTANT },
2279 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2280 .driver_data = VAIO_RDESC_CONSTANT },
2281 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2282 * Logitech joystick from the device descriptor. */
2283 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2284 .driver_data = BUZZ_CONTROLLER },
2285 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2286 .driver_data = BUZZ_CONTROLLER },
2287 /* PS3 BD Remote Control */
2288 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2289 .driver_data = PS3REMOTE },
2290 /* Logitech Harmony Adapter for PS3 */
2291 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2292 .driver_data = PS3REMOTE },
Frank Praznik68a49e52014-11-12 14:52:28 -05002293 /* SMK-Link PS3 BD Remote Control */
2294 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2295 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002296 /* Sony Dualshock 4 controllers for PS4 */
2297 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002298 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002299 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002300 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002301 { }
2302};
2303MODULE_DEVICE_TABLE(hid, sony_devices);
2304
2305static struct hid_driver sony_driver = {
Frank Praznikce8efc32014-09-18 21:15:01 -04002306 .name = "sony",
2307 .id_table = sony_devices,
2308 .input_mapping = sony_mapping,
2309 .input_configured = sony_input_configured,
2310 .probe = sony_probe,
2311 .remove = sony_remove,
2312 .report_fixup = sony_report_fixup,
2313 .raw_event = sony_raw_event
Jiri Slabybd28ce02008-06-25 23:47:04 +02002314};
Frank Praznik80250872014-04-14 10:11:35 -04002315
2316static int __init sony_init(void)
2317{
2318 dbg_hid("Sony:%s\n", __func__);
2319
2320 return hid_register_driver(&sony_driver);
2321}
2322
2323static void __exit sony_exit(void)
2324{
2325 dbg_hid("Sony:%s\n", __func__);
2326
Frank Praznik80250872014-04-14 10:11:35 -04002327 hid_unregister_driver(&sony_driver);
Antonio Ospite6c400652015-02-16 22:58:24 +01002328 ida_destroy(&sony_device_id_allocator);
Frank Praznik80250872014-04-14 10:11:35 -04002329}
2330module_init(sony_init);
2331module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002332
2333MODULE_LICENSE("GPL");