blob: 3fba2dcae0c266f0ff1d95bfc968c391d8f2648d [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), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400156 0x75, 0x01, /* Report Size (1), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600157 0x95, 0x15, /* Report Count (21), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400158 0x15, 0x00, /* Logical Minimum (0), */
159 0x25, 0x01, /* Logical Maximum (1), */
160 0x35, 0x00, /* Physical Minimum (0), */
161 0x45, 0x01, /* Physical Maximum (1), */
162 0x05, 0x09, /* Usage Page (Button), */
163 0x19, 0x01, /* Usage Minimum (01h), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600164 0x29, 0x15, /* Usage Maximum (15h), */
165 0x81, 0x02, /* Input (Variable), * Buttons */
166 0x95, 0x0B, /* Report Count (11), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400167 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600168 0x81, 0x03, /* Input (Constant, Variable), * Padding */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400169 0x15, 0x00, /* Logical Minimum (0), */
170 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
171 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400172 0xA1, 0x00, /* Collection (Physical), */
173 0x75, 0x08, /* Report Size (8), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600174 0x95, 0x01, /* Report Count (1), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400175 0x35, 0x00, /* Physical Minimum (0), */
176 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
177 0x09, 0x30, /* Usage (X), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600178 0x81, 0x02, /* Input (Variable), * Trigger */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400179 0xC0, /* End Collection, */
Simon Wood8b2513c2015-06-09 21:27:07 -0600180 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
181 0x75, 0x08, /* Report Size (8), */
182 0x95, 0x07, /* Report Count (7), * skip 7 bytes */
183 0x81, 0x02, /* Input (Variable), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400184 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400185 0x75, 0x10, /* Report Size (16), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600186 0x46, 0xFF, 0xFF, /* Physical Maximum (65535), */
187 0x27, 0xFF, 0xFF, 0x00, 0x00, /* Logical Maximum (65535), */
188 0x95, 0x03, /* Report Count (3), * 3x Accels */
189 0x09, 0x33, /* Usage (rX), */
190 0x09, 0x34, /* Usage (rY), */
191 0x09, 0x35, /* Usage (rZ), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400192 0x81, 0x02, /* Input (Variable), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600193 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
194 0x95, 0x03, /* Report Count (3), * Skip Accels 2nd frame */
195 0x81, 0x02, /* Input (Variable), */
196 0x05, 0x01, /* Usage Page (Desktop), */
197 0x09, 0x01, /* Usage (Pointer), */
198 0x95, 0x03, /* Report Count (3), * 3x Gyros */
199 0x81, 0x02, /* Input (Variable), */
200 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
201 0x95, 0x03, /* Report Count (3), * Skip Gyros 2nd frame */
202 0x81, 0x02, /* Input (Variable), */
203 0x75, 0x0C, /* Report Size (12), */
204 0x46, 0xFF, 0x0F, /* Physical Maximum (4095), */
205 0x26, 0xFF, 0x0F, /* Logical Maximum (4095), */
206 0x95, 0x04, /* Report Count (4), * Skip Temp and Magnetometers */
207 0x81, 0x02, /* Input (Variable), */
208 0x75, 0x08, /* Report Size (8), */
209 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
210 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
211 0x95, 0x06, /* Report Count (6), * Skip Timestamp and Extension Bytes */
212 0x81, 0x02, /* Input (Variable), */
213 0x75, 0x08, /* Report Size (8), */
214 0x95, 0x30, /* Report Count (48), */
215 0x09, 0x01, /* Usage (Pointer), */
216 0x91, 0x02, /* Output (Variable), */
217 0x75, 0x08, /* Report Size (8), */
218 0x95, 0x30, /* Report Count (48), */
219 0x09, 0x01, /* Usage (Pointer), */
220 0xB1, 0x02, /* Feature (Variable), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400221 0xC0, /* End Collection, */
222 0xA1, 0x02, /* Collection (Logical), */
223 0x85, 0x02, /* Report ID (2), */
224 0x75, 0x08, /* Report Size (8), */
225 0x95, 0x30, /* Report Count (48), */
226 0x09, 0x01, /* Usage (Pointer), */
227 0xB1, 0x02, /* Feature (Variable), */
228 0xC0, /* End Collection, */
229 0xA1, 0x02, /* Collection (Logical), */
230 0x85, 0xEE, /* Report ID (238), */
231 0x75, 0x08, /* Report Size (8), */
232 0x95, 0x30, /* Report Count (48), */
233 0x09, 0x01, /* Usage (Pointer), */
234 0xB1, 0x02, /* Feature (Variable), */
235 0xC0, /* End Collection, */
236 0xA1, 0x02, /* Collection (Logical), */
237 0x85, 0xEF, /* Report ID (239), */
238 0x75, 0x08, /* Report Size (8), */
239 0x95, 0x30, /* Report Count (48), */
240 0x09, 0x01, /* Usage (Pointer), */
241 0xB1, 0x02, /* Feature (Variable), */
242 0xC0, /* End Collection, */
243 0xC0 /* End Collection */
244};
245
246
Frank Praznikad142b92014-02-20 11:36:00 -0500247/*
248 * The default descriptor doesn't provide mapping for the accelerometers
Frank Praznik58d70272014-01-20 12:27:01 -0500249 * or orientation sensors. This fixed descriptor maps the accelerometers
250 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
251 * to usage values 0x43, 0x44 and 0x45.
252 */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500253static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -0500254 0x05, 0x01, /* Usage Page (Desktop), */
255 0x09, 0x05, /* Usage (Gamepad), */
256 0xA1, 0x01, /* Collection (Application), */
257 0x85, 0x01, /* Report ID (1), */
258 0x09, 0x30, /* Usage (X), */
259 0x09, 0x31, /* Usage (Y), */
260 0x09, 0x32, /* Usage (Z), */
261 0x09, 0x35, /* Usage (Rz), */
262 0x15, 0x00, /* Logical Minimum (0), */
263 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
264 0x75, 0x08, /* Report Size (8), */
265 0x95, 0x04, /* Report Count (4), */
266 0x81, 0x02, /* Input (Variable), */
267 0x09, 0x39, /* Usage (Hat Switch), */
268 0x15, 0x00, /* Logical Minimum (0), */
269 0x25, 0x07, /* Logical Maximum (7), */
270 0x35, 0x00, /* Physical Minimum (0), */
271 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
272 0x65, 0x14, /* Unit (Degrees), */
273 0x75, 0x04, /* Report Size (4), */
274 0x95, 0x01, /* Report Count (1), */
275 0x81, 0x42, /* Input (Variable, Null State), */
276 0x65, 0x00, /* Unit, */
277 0x05, 0x09, /* Usage Page (Button), */
278 0x19, 0x01, /* Usage Minimum (01h), */
279 0x29, 0x0E, /* Usage Maximum (0Eh), */
280 0x15, 0x00, /* Logical Minimum (0), */
281 0x25, 0x01, /* Logical Maximum (1), */
282 0x75, 0x01, /* Report Size (1), */
283 0x95, 0x0E, /* Report Count (14), */
284 0x81, 0x02, /* Input (Variable), */
285 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
286 0x09, 0x20, /* Usage (20h), */
287 0x75, 0x06, /* Report Size (6), */
288 0x95, 0x01, /* Report Count (1), */
289 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400290 0x25, 0x3F, /* Logical Maximum (63), */
Frank Praznik58d70272014-01-20 12:27:01 -0500291 0x81, 0x02, /* Input (Variable), */
292 0x05, 0x01, /* Usage Page (Desktop), */
293 0x09, 0x33, /* Usage (Rx), */
294 0x09, 0x34, /* Usage (Ry), */
295 0x15, 0x00, /* Logical Minimum (0), */
296 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
297 0x75, 0x08, /* Report Size (8), */
298 0x95, 0x02, /* Report Count (2), */
299 0x81, 0x02, /* Input (Variable), */
300 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
301 0x09, 0x21, /* Usage (21h), */
302 0x95, 0x03, /* Report Count (3), */
303 0x81, 0x02, /* Input (Variable), */
304 0x05, 0x01, /* Usage Page (Desktop), */
305 0x19, 0x40, /* Usage Minimum (40h), */
306 0x29, 0x42, /* Usage Maximum (42h), */
307 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
308 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
309 0x75, 0x10, /* Report Size (16), */
310 0x95, 0x03, /* Report Count (3), */
311 0x81, 0x02, /* Input (Variable), */
312 0x19, 0x43, /* Usage Minimum (43h), */
313 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400314 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
315 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznik58d70272014-01-20 12:27:01 -0500316 0x95, 0x03, /* Report Count (3), */
317 0x81, 0x02, /* Input (Variable), */
318 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
319 0x09, 0x21, /* Usage (21h), */
320 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400321 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
Frank Praznik58d70272014-01-20 12:27:01 -0500322 0x75, 0x08, /* Report Size (8), */
323 0x95, 0x27, /* Report Count (39), */
324 0x81, 0x02, /* Input (Variable), */
325 0x85, 0x05, /* Report ID (5), */
326 0x09, 0x22, /* Usage (22h), */
327 0x95, 0x1F, /* Report Count (31), */
328 0x91, 0x02, /* Output (Variable), */
329 0x85, 0x04, /* Report ID (4), */
330 0x09, 0x23, /* Usage (23h), */
331 0x95, 0x24, /* Report Count (36), */
332 0xB1, 0x02, /* Feature (Variable), */
333 0x85, 0x02, /* Report ID (2), */
334 0x09, 0x24, /* Usage (24h), */
335 0x95, 0x24, /* Report Count (36), */
336 0xB1, 0x02, /* Feature (Variable), */
337 0x85, 0x08, /* Report ID (8), */
338 0x09, 0x25, /* Usage (25h), */
339 0x95, 0x03, /* Report Count (3), */
340 0xB1, 0x02, /* Feature (Variable), */
341 0x85, 0x10, /* Report ID (16), */
342 0x09, 0x26, /* Usage (26h), */
343 0x95, 0x04, /* Report Count (4), */
344 0xB1, 0x02, /* Feature (Variable), */
345 0x85, 0x11, /* Report ID (17), */
346 0x09, 0x27, /* Usage (27h), */
347 0x95, 0x02, /* Report Count (2), */
348 0xB1, 0x02, /* Feature (Variable), */
349 0x85, 0x12, /* Report ID (18), */
350 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
351 0x09, 0x21, /* Usage (21h), */
352 0x95, 0x0F, /* Report Count (15), */
353 0xB1, 0x02, /* Feature (Variable), */
354 0x85, 0x13, /* Report ID (19), */
355 0x09, 0x22, /* Usage (22h), */
356 0x95, 0x16, /* Report Count (22), */
357 0xB1, 0x02, /* Feature (Variable), */
358 0x85, 0x14, /* Report ID (20), */
359 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
360 0x09, 0x20, /* Usage (20h), */
361 0x95, 0x10, /* Report Count (16), */
362 0xB1, 0x02, /* Feature (Variable), */
363 0x85, 0x15, /* Report ID (21), */
364 0x09, 0x21, /* Usage (21h), */
365 0x95, 0x2C, /* Report Count (44), */
366 0xB1, 0x02, /* Feature (Variable), */
367 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
368 0x85, 0x80, /* Report ID (128), */
369 0x09, 0x20, /* Usage (20h), */
370 0x95, 0x06, /* Report Count (6), */
371 0xB1, 0x02, /* Feature (Variable), */
372 0x85, 0x81, /* Report ID (129), */
373 0x09, 0x21, /* Usage (21h), */
374 0x95, 0x06, /* Report Count (6), */
375 0xB1, 0x02, /* Feature (Variable), */
376 0x85, 0x82, /* Report ID (130), */
377 0x09, 0x22, /* Usage (22h), */
378 0x95, 0x05, /* Report Count (5), */
379 0xB1, 0x02, /* Feature (Variable), */
380 0x85, 0x83, /* Report ID (131), */
381 0x09, 0x23, /* Usage (23h), */
382 0x95, 0x01, /* Report Count (1), */
383 0xB1, 0x02, /* Feature (Variable), */
384 0x85, 0x84, /* Report ID (132), */
385 0x09, 0x24, /* Usage (24h), */
386 0x95, 0x04, /* Report Count (4), */
387 0xB1, 0x02, /* Feature (Variable), */
388 0x85, 0x85, /* Report ID (133), */
389 0x09, 0x25, /* Usage (25h), */
390 0x95, 0x06, /* Report Count (6), */
391 0xB1, 0x02, /* Feature (Variable), */
392 0x85, 0x86, /* Report ID (134), */
393 0x09, 0x26, /* Usage (26h), */
394 0x95, 0x06, /* Report Count (6), */
395 0xB1, 0x02, /* Feature (Variable), */
396 0x85, 0x87, /* Report ID (135), */
397 0x09, 0x27, /* Usage (27h), */
398 0x95, 0x23, /* Report Count (35), */
399 0xB1, 0x02, /* Feature (Variable), */
400 0x85, 0x88, /* Report ID (136), */
401 0x09, 0x28, /* Usage (28h), */
402 0x95, 0x22, /* Report Count (34), */
403 0xB1, 0x02, /* Feature (Variable), */
404 0x85, 0x89, /* Report ID (137), */
405 0x09, 0x29, /* Usage (29h), */
406 0x95, 0x02, /* Report Count (2), */
407 0xB1, 0x02, /* Feature (Variable), */
408 0x85, 0x90, /* Report ID (144), */
409 0x09, 0x30, /* Usage (30h), */
410 0x95, 0x05, /* Report Count (5), */
411 0xB1, 0x02, /* Feature (Variable), */
412 0x85, 0x91, /* Report ID (145), */
413 0x09, 0x31, /* Usage (31h), */
414 0x95, 0x03, /* Report Count (3), */
415 0xB1, 0x02, /* Feature (Variable), */
416 0x85, 0x92, /* Report ID (146), */
417 0x09, 0x32, /* Usage (32h), */
418 0x95, 0x03, /* Report Count (3), */
419 0xB1, 0x02, /* Feature (Variable), */
420 0x85, 0x93, /* Report ID (147), */
421 0x09, 0x33, /* Usage (33h), */
422 0x95, 0x0C, /* Report Count (12), */
423 0xB1, 0x02, /* Feature (Variable), */
424 0x85, 0xA0, /* Report ID (160), */
425 0x09, 0x40, /* Usage (40h), */
426 0x95, 0x06, /* Report Count (6), */
427 0xB1, 0x02, /* Feature (Variable), */
428 0x85, 0xA1, /* Report ID (161), */
429 0x09, 0x41, /* Usage (41h), */
430 0x95, 0x01, /* Report Count (1), */
431 0xB1, 0x02, /* Feature (Variable), */
432 0x85, 0xA2, /* Report ID (162), */
433 0x09, 0x42, /* Usage (42h), */
434 0x95, 0x01, /* Report Count (1), */
435 0xB1, 0x02, /* Feature (Variable), */
436 0x85, 0xA3, /* Report ID (163), */
437 0x09, 0x43, /* Usage (43h), */
438 0x95, 0x30, /* Report Count (48), */
439 0xB1, 0x02, /* Feature (Variable), */
440 0x85, 0xA4, /* Report ID (164), */
441 0x09, 0x44, /* Usage (44h), */
442 0x95, 0x0D, /* Report Count (13), */
443 0xB1, 0x02, /* Feature (Variable), */
444 0x85, 0xA5, /* Report ID (165), */
445 0x09, 0x45, /* Usage (45h), */
446 0x95, 0x15, /* Report Count (21), */
447 0xB1, 0x02, /* Feature (Variable), */
448 0x85, 0xA6, /* Report ID (166), */
449 0x09, 0x46, /* Usage (46h), */
450 0x95, 0x15, /* Report Count (21), */
451 0xB1, 0x02, /* Feature (Variable), */
452 0x85, 0xF0, /* Report ID (240), */
453 0x09, 0x47, /* Usage (47h), */
454 0x95, 0x3F, /* Report Count (63), */
455 0xB1, 0x02, /* Feature (Variable), */
456 0x85, 0xF1, /* Report ID (241), */
457 0x09, 0x48, /* Usage (48h), */
458 0x95, 0x3F, /* Report Count (63), */
459 0xB1, 0x02, /* Feature (Variable), */
460 0x85, 0xF2, /* Report ID (242), */
461 0x09, 0x49, /* Usage (49h), */
462 0x95, 0x0F, /* Report Count (15), */
463 0xB1, 0x02, /* Feature (Variable), */
464 0x85, 0xA7, /* Report ID (167), */
465 0x09, 0x4A, /* Usage (4Ah), */
466 0x95, 0x01, /* Report Count (1), */
467 0xB1, 0x02, /* Feature (Variable), */
468 0x85, 0xA8, /* Report ID (168), */
469 0x09, 0x4B, /* Usage (4Bh), */
470 0x95, 0x01, /* Report Count (1), */
471 0xB1, 0x02, /* Feature (Variable), */
472 0x85, 0xA9, /* Report ID (169), */
473 0x09, 0x4C, /* Usage (4Ch), */
474 0x95, 0x08, /* Report Count (8), */
475 0xB1, 0x02, /* Feature (Variable), */
476 0x85, 0xAA, /* Report ID (170), */
477 0x09, 0x4E, /* Usage (4Eh), */
478 0x95, 0x01, /* Report Count (1), */
479 0xB1, 0x02, /* Feature (Variable), */
480 0x85, 0xAB, /* Report ID (171), */
481 0x09, 0x4F, /* Usage (4Fh), */
482 0x95, 0x39, /* Report Count (57), */
483 0xB1, 0x02, /* Feature (Variable), */
484 0x85, 0xAC, /* Report ID (172), */
485 0x09, 0x50, /* Usage (50h), */
486 0x95, 0x39, /* Report Count (57), */
487 0xB1, 0x02, /* Feature (Variable), */
488 0x85, 0xAD, /* Report ID (173), */
489 0x09, 0x51, /* Usage (51h), */
490 0x95, 0x0B, /* Report Count (11), */
491 0xB1, 0x02, /* Feature (Variable), */
492 0x85, 0xAE, /* Report ID (174), */
493 0x09, 0x52, /* Usage (52h), */
494 0x95, 0x01, /* Report Count (1), */
495 0xB1, 0x02, /* Feature (Variable), */
496 0x85, 0xAF, /* Report ID (175), */
497 0x09, 0x53, /* Usage (53h), */
498 0x95, 0x02, /* Report Count (2), */
499 0xB1, 0x02, /* Feature (Variable), */
500 0x85, 0xB0, /* Report ID (176), */
501 0x09, 0x54, /* Usage (54h), */
502 0x95, 0x3F, /* Report Count (63), */
503 0xB1, 0x02, /* Feature (Variable), */
504 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500505};
506
Frank Praznikad142b92014-02-20 11:36:00 -0500507/*
508 * The default behavior of the Dualshock 4 is to send reports using report
Frank Praznik077147a2014-09-14 11:56:39 -0400509 * type 1 when running over Bluetooth. However, when feature report 2 is
510 * requested during the controller initialization it starts sending input
511 * reports in report 17. Since report 17 is undefined in the default HID
Frank Praznikd8296742014-02-05 20:03:45 -0500512 * descriptor the button and axis definitions must be moved to report 17 or
Frank Praznik077147a2014-09-14 11:56:39 -0400513 * the HID layer won't process the received input.
Frank Praznikd8296742014-02-05 20:03:45 -0500514 */
515static u8 dualshock4_bt_rdesc[] = {
516 0x05, 0x01, /* Usage Page (Desktop), */
517 0x09, 0x05, /* Usage (Gamepad), */
518 0xA1, 0x01, /* Collection (Application), */
519 0x85, 0x01, /* Report ID (1), */
520 0x75, 0x08, /* Report Size (8), */
521 0x95, 0x0A, /* Report Count (9), */
522 0x81, 0x02, /* Input (Variable), */
523 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
524 0x85, 0x02, /* Report ID (2), */
525 0x09, 0x24, /* Usage (24h), */
526 0x95, 0x24, /* Report Count (36), */
527 0xB1, 0x02, /* Feature (Variable), */
528 0x85, 0xA3, /* Report ID (163), */
529 0x09, 0x25, /* Usage (25h), */
530 0x95, 0x30, /* Report Count (48), */
531 0xB1, 0x02, /* Feature (Variable), */
532 0x85, 0x05, /* Report ID (5), */
533 0x09, 0x26, /* Usage (26h), */
534 0x95, 0x28, /* Report Count (40), */
535 0xB1, 0x02, /* Feature (Variable), */
536 0x85, 0x06, /* Report ID (6), */
537 0x09, 0x27, /* Usage (27h), */
538 0x95, 0x34, /* Report Count (52), */
539 0xB1, 0x02, /* Feature (Variable), */
540 0x85, 0x07, /* Report ID (7), */
541 0x09, 0x28, /* Usage (28h), */
542 0x95, 0x30, /* Report Count (48), */
543 0xB1, 0x02, /* Feature (Variable), */
544 0x85, 0x08, /* Report ID (8), */
545 0x09, 0x29, /* Usage (29h), */
546 0x95, 0x2F, /* Report Count (47), */
547 0xB1, 0x02, /* Feature (Variable), */
548 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
549 0x85, 0x03, /* Report ID (3), */
550 0x09, 0x21, /* Usage (21h), */
551 0x95, 0x26, /* Report Count (38), */
552 0xB1, 0x02, /* Feature (Variable), */
553 0x85, 0x04, /* Report ID (4), */
554 0x09, 0x22, /* Usage (22h), */
555 0x95, 0x2E, /* Report Count (46), */
556 0xB1, 0x02, /* Feature (Variable), */
557 0x85, 0xF0, /* Report ID (240), */
558 0x09, 0x47, /* Usage (47h), */
559 0x95, 0x3F, /* Report Count (63), */
560 0xB1, 0x02, /* Feature (Variable), */
561 0x85, 0xF1, /* Report ID (241), */
562 0x09, 0x48, /* Usage (48h), */
563 0x95, 0x3F, /* Report Count (63), */
564 0xB1, 0x02, /* Feature (Variable), */
565 0x85, 0xF2, /* Report ID (242), */
566 0x09, 0x49, /* Usage (49h), */
567 0x95, 0x0F, /* Report Count (15), */
568 0xB1, 0x02, /* Feature (Variable), */
569 0x85, 0x11, /* Report ID (17), */
570 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
571 0x09, 0x20, /* Usage (20h), */
572 0x95, 0x02, /* Report Count (2), */
573 0x81, 0x02, /* Input (Variable), */
574 0x05, 0x01, /* Usage Page (Desktop), */
575 0x09, 0x30, /* Usage (X), */
576 0x09, 0x31, /* Usage (Y), */
577 0x09, 0x32, /* Usage (Z), */
578 0x09, 0x35, /* Usage (Rz), */
579 0x15, 0x00, /* Logical Minimum (0), */
580 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
581 0x75, 0x08, /* Report Size (8), */
582 0x95, 0x04, /* Report Count (4), */
583 0x81, 0x02, /* Input (Variable), */
584 0x09, 0x39, /* Usage (Hat Switch), */
585 0x15, 0x00, /* Logical Minimum (0), */
586 0x25, 0x07, /* Logical Maximum (7), */
587 0x75, 0x04, /* Report Size (4), */
588 0x95, 0x01, /* Report Count (1), */
589 0x81, 0x42, /* Input (Variable, Null State), */
590 0x05, 0x09, /* Usage Page (Button), */
591 0x19, 0x01, /* Usage Minimum (01h), */
592 0x29, 0x0E, /* Usage Maximum (0Eh), */
593 0x15, 0x00, /* Logical Minimum (0), */
594 0x25, 0x01, /* Logical Maximum (1), */
595 0x75, 0x01, /* Report Size (1), */
596 0x95, 0x0E, /* Report Count (14), */
597 0x81, 0x02, /* Input (Variable), */
598 0x75, 0x06, /* Report Size (6), */
599 0x95, 0x01, /* Report Count (1), */
600 0x81, 0x01, /* Input (Constant), */
601 0x05, 0x01, /* Usage Page (Desktop), */
602 0x09, 0x33, /* Usage (Rx), */
603 0x09, 0x34, /* Usage (Ry), */
604 0x15, 0x00, /* Logical Minimum (0), */
605 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
606 0x75, 0x08, /* Report Size (8), */
607 0x95, 0x02, /* Report Count (2), */
608 0x81, 0x02, /* Input (Variable), */
609 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
610 0x09, 0x20, /* Usage (20h), */
611 0x95, 0x03, /* Report Count (3), */
612 0x81, 0x02, /* Input (Variable), */
613 0x05, 0x01, /* Usage Page (Desktop), */
614 0x19, 0x40, /* Usage Minimum (40h), */
615 0x29, 0x42, /* Usage Maximum (42h), */
616 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
617 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
618 0x75, 0x10, /* Report Size (16), */
619 0x95, 0x03, /* Report Count (3), */
620 0x81, 0x02, /* Input (Variable), */
621 0x19, 0x43, /* Usage Minimum (43h), */
622 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400623 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
624 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznikd8296742014-02-05 20:03:45 -0500625 0x95, 0x03, /* Report Count (3), */
626 0x81, 0x02, /* Input (Variable), */
627 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
628 0x09, 0x20, /* Usage (20h), */
629 0x15, 0x00, /* Logical Minimum (0), */
630 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
631 0x75, 0x08, /* Report Size (8), */
632 0x95, 0x31, /* Report Count (51), */
633 0x81, 0x02, /* Input (Variable), */
634 0x09, 0x21, /* Usage (21h), */
635 0x75, 0x08, /* Report Size (8), */
636 0x95, 0x4D, /* Report Count (77), */
637 0x91, 0x02, /* Output (Variable), */
638 0x85, 0x12, /* Report ID (18), */
639 0x09, 0x22, /* Usage (22h), */
640 0x95, 0x8D, /* Report Count (141), */
641 0x81, 0x02, /* Input (Variable), */
642 0x09, 0x23, /* Usage (23h), */
643 0x91, 0x02, /* Output (Variable), */
644 0x85, 0x13, /* Report ID (19), */
645 0x09, 0x24, /* Usage (24h), */
646 0x95, 0xCD, /* Report Count (205), */
647 0x81, 0x02, /* Input (Variable), */
648 0x09, 0x25, /* Usage (25h), */
649 0x91, 0x02, /* Output (Variable), */
650 0x85, 0x14, /* Report ID (20), */
651 0x09, 0x26, /* Usage (26h), */
652 0x96, 0x0D, 0x01, /* Report Count (269), */
653 0x81, 0x02, /* Input (Variable), */
654 0x09, 0x27, /* Usage (27h), */
655 0x91, 0x02, /* Output (Variable), */
656 0x85, 0x15, /* Report ID (21), */
657 0x09, 0x28, /* Usage (28h), */
658 0x96, 0x4D, 0x01, /* Report Count (333), */
659 0x81, 0x02, /* Input (Variable), */
660 0x09, 0x29, /* Usage (29h), */
661 0x91, 0x02, /* Output (Variable), */
662 0x85, 0x16, /* Report ID (22), */
663 0x09, 0x2A, /* Usage (2Ah), */
664 0x96, 0x8D, 0x01, /* Report Count (397), */
665 0x81, 0x02, /* Input (Variable), */
666 0x09, 0x2B, /* Usage (2Bh), */
667 0x91, 0x02, /* Output (Variable), */
668 0x85, 0x17, /* Report ID (23), */
669 0x09, 0x2C, /* Usage (2Ch), */
670 0x96, 0xCD, 0x01, /* Report Count (461), */
671 0x81, 0x02, /* Input (Variable), */
672 0x09, 0x2D, /* Usage (2Dh), */
673 0x91, 0x02, /* Output (Variable), */
674 0x85, 0x18, /* Report ID (24), */
675 0x09, 0x2E, /* Usage (2Eh), */
676 0x96, 0x0D, 0x02, /* Report Count (525), */
677 0x81, 0x02, /* Input (Variable), */
678 0x09, 0x2F, /* Usage (2Fh), */
679 0x91, 0x02, /* Output (Variable), */
680 0x85, 0x19, /* Report ID (25), */
681 0x09, 0x30, /* Usage (30h), */
682 0x96, 0x22, 0x02, /* Report Count (546), */
683 0x81, 0x02, /* Input (Variable), */
684 0x09, 0x31, /* Usage (31h), */
685 0x91, 0x02, /* Output (Variable), */
686 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
687 0x85, 0x82, /* Report ID (130), */
688 0x09, 0x22, /* Usage (22h), */
689 0x95, 0x3F, /* Report Count (63), */
690 0xB1, 0x02, /* Feature (Variable), */
691 0x85, 0x83, /* Report ID (131), */
692 0x09, 0x23, /* Usage (23h), */
693 0xB1, 0x02, /* Feature (Variable), */
694 0x85, 0x84, /* Report ID (132), */
695 0x09, 0x24, /* Usage (24h), */
696 0xB1, 0x02, /* Feature (Variable), */
697 0x85, 0x90, /* Report ID (144), */
698 0x09, 0x30, /* Usage (30h), */
699 0xB1, 0x02, /* Feature (Variable), */
700 0x85, 0x91, /* Report ID (145), */
701 0x09, 0x31, /* Usage (31h), */
702 0xB1, 0x02, /* Feature (Variable), */
703 0x85, 0x92, /* Report ID (146), */
704 0x09, 0x32, /* Usage (32h), */
705 0xB1, 0x02, /* Feature (Variable), */
706 0x85, 0x93, /* Report ID (147), */
707 0x09, 0x33, /* Usage (33h), */
708 0xB1, 0x02, /* Feature (Variable), */
709 0x85, 0xA0, /* Report ID (160), */
710 0x09, 0x40, /* Usage (40h), */
711 0xB1, 0x02, /* Feature (Variable), */
712 0x85, 0xA4, /* Report ID (164), */
713 0x09, 0x44, /* Usage (44h), */
714 0xB1, 0x02, /* Feature (Variable), */
715 0xC0 /* End Collection */
716};
717
Jiri Kosina078328d2013-06-13 12:03:49 +0200718static __u8 ps3remote_rdesc[] = {
719 0x05, 0x01, /* GUsagePage Generic Desktop */
720 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
721 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
722
723 /* Use collection 1 for joypad buttons */
724 0xA1, 0x02, /* MCollection Logical (interrelated data) */
725
726 /* Ignore the 1st byte, maybe it is used for a controller
727 * number but it's not needed for correct operation */
728 0x75, 0x08, /* GReportSize 0x08 [8] */
729 0x95, 0x01, /* GReportCount 0x01 [1] */
730 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
731
732 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
733 * buttons multiple keypresses are allowed */
734 0x05, 0x09, /* GUsagePage Button */
735 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
736 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
737 0x14, /* GLogicalMinimum [0] */
738 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
739 0x75, 0x01, /* GReportSize 0x01 [1] */
740 0x95, 0x18, /* GReportCount 0x18 [24] */
741 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
742
743 0xC0, /* MEndCollection */
744
745 /* Use collection 2 for remote control buttons */
746 0xA1, 0x02, /* MCollection Logical (interrelated data) */
747
748 /* 5th byte is used for remote control buttons */
749 0x05, 0x09, /* GUsagePage Button */
750 0x18, /* LUsageMinimum [No button pressed] */
751 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
752 0x14, /* GLogicalMinimum [0] */
753 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
754 0x75, 0x08, /* GReportSize 0x08 [8] */
755 0x95, 0x01, /* GReportCount 0x01 [1] */
756 0x80, /* MInput */
757
758 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
759 * 0xff and 11th is for press indication */
760 0x75, 0x08, /* GReportSize 0x08 [8] */
761 0x95, 0x06, /* GReportCount 0x06 [6] */
762 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
763
764 /* 12th byte is for battery strength */
765 0x05, 0x06, /* GUsagePage Generic Device Controls */
766 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
767 0x14, /* GLogicalMinimum [0] */
768 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
769 0x75, 0x08, /* GReportSize 0x08 [8] */
770 0x95, 0x01, /* GReportCount 0x01 [1] */
771 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
772
773 0xC0, /* MEndCollection */
774
775 0xC0 /* MEndCollection [Game Pad] */
776};
777
778static const unsigned int ps3remote_keymap_joypad_buttons[] = {
779 [0x01] = KEY_SELECT,
780 [0x02] = BTN_THUMBL, /* L3 */
781 [0x03] = BTN_THUMBR, /* R3 */
782 [0x04] = BTN_START,
783 [0x05] = KEY_UP,
784 [0x06] = KEY_RIGHT,
785 [0x07] = KEY_DOWN,
786 [0x08] = KEY_LEFT,
787 [0x09] = BTN_TL2, /* L2 */
788 [0x0a] = BTN_TR2, /* R2 */
789 [0x0b] = BTN_TL, /* L1 */
790 [0x0c] = BTN_TR, /* R1 */
791 [0x0d] = KEY_OPTION, /* options/triangle */
792 [0x0e] = KEY_BACK, /* back/circle */
793 [0x0f] = BTN_0, /* cross */
794 [0x10] = KEY_SCREEN, /* view/square */
795 [0x11] = KEY_HOMEPAGE, /* PS button */
796 [0x14] = KEY_ENTER,
797};
798static const unsigned int ps3remote_keymap_remote_buttons[] = {
799 [0x00] = KEY_1,
800 [0x01] = KEY_2,
801 [0x02] = KEY_3,
802 [0x03] = KEY_4,
803 [0x04] = KEY_5,
804 [0x05] = KEY_6,
805 [0x06] = KEY_7,
806 [0x07] = KEY_8,
807 [0x08] = KEY_9,
808 [0x09] = KEY_0,
809 [0x0e] = KEY_ESC, /* return */
810 [0x0f] = KEY_CLEAR,
811 [0x16] = KEY_EJECTCD,
812 [0x1a] = KEY_MENU, /* top menu */
813 [0x28] = KEY_TIME,
814 [0x30] = KEY_PREVIOUS,
815 [0x31] = KEY_NEXT,
816 [0x32] = KEY_PLAY,
817 [0x33] = KEY_REWIND, /* scan back */
818 [0x34] = KEY_FORWARD, /* scan forward */
819 [0x38] = KEY_STOP,
820 [0x39] = KEY_PAUSE,
821 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
822 [0x60] = KEY_FRAMEBACK, /* slow/step back */
823 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
824 [0x63] = KEY_SUBTITLE,
825 [0x64] = KEY_AUDIO,
826 [0x65] = KEY_ANGLE,
827 [0x70] = KEY_INFO, /* display */
828 [0x80] = KEY_BLUE,
829 [0x81] = KEY_RED,
830 [0x82] = KEY_GREEN,
831 [0x83] = KEY_YELLOW,
832};
833
Colin Leitnerf04d5142013-05-27 23:41:05 +0200834static const unsigned int buzz_keymap[] = {
Frank Praznikad142b92014-02-20 11:36:00 -0500835 /*
836 * The controller has 4 remote buzzers, each with one LED and 5
Colin Leitnerf04d5142013-05-27 23:41:05 +0200837 * buttons.
838 *
839 * We use the mapping chosen by the controller, which is:
840 *
841 * Key Offset
842 * -------------------
843 * Buzz 1
844 * Blue 5
845 * Orange 4
846 * Green 3
847 * Yellow 2
848 *
849 * So, for example, the orange button on the third buzzer is mapped to
850 * BTN_TRIGGER_HAPPY14
851 */
852 [ 1] = BTN_TRIGGER_HAPPY1,
853 [ 2] = BTN_TRIGGER_HAPPY2,
854 [ 3] = BTN_TRIGGER_HAPPY3,
855 [ 4] = BTN_TRIGGER_HAPPY4,
856 [ 5] = BTN_TRIGGER_HAPPY5,
857 [ 6] = BTN_TRIGGER_HAPPY6,
858 [ 7] = BTN_TRIGGER_HAPPY7,
859 [ 8] = BTN_TRIGGER_HAPPY8,
860 [ 9] = BTN_TRIGGER_HAPPY9,
861 [10] = BTN_TRIGGER_HAPPY10,
862 [11] = BTN_TRIGGER_HAPPY11,
863 [12] = BTN_TRIGGER_HAPPY12,
864 [13] = BTN_TRIGGER_HAPPY13,
865 [14] = BTN_TRIGGER_HAPPY14,
866 [15] = BTN_TRIGGER_HAPPY15,
867 [16] = BTN_TRIGGER_HAPPY16,
868 [17] = BTN_TRIGGER_HAPPY17,
869 [18] = BTN_TRIGGER_HAPPY18,
870 [19] = BTN_TRIGGER_HAPPY19,
871 [20] = BTN_TRIGGER_HAPPY20,
872};
873
Frank Praznikd902f472014-01-27 10:17:36 -0500874static enum power_supply_property sony_battery_props[] = {
875 POWER_SUPPLY_PROP_PRESENT,
876 POWER_SUPPLY_PROP_CAPACITY,
877 POWER_SUPPLY_PROP_SCOPE,
878 POWER_SUPPLY_PROP_STATUS,
879};
880
Frank Praznik55d3b662014-04-14 10:11:32 -0400881struct sixaxis_led {
882 __u8 time_enabled; /* the total time the led is active (0xff means forever) */
883 __u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
884 __u8 enabled;
885 __u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
886 __u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
887} __packed;
888
889struct sixaxis_rumble {
890 __u8 padding;
891 __u8 right_duration; /* Right motor duration (0xff means forever) */
892 __u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
893 __u8 left_duration; /* Left motor duration (0xff means forever) */
894 __u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
895} __packed;
896
897struct sixaxis_output_report {
898 __u8 report_id;
899 struct sixaxis_rumble rumble;
900 __u8 padding[4];
901 __u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
902 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
903 struct sixaxis_led _reserved; /* LED5, not actually soldered */
904} __packed;
905
906union sixaxis_output_report_01 {
907 struct sixaxis_output_report data;
908 __u8 buf[36];
909};
910
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400911struct motion_output_report_02 {
912 u8 type, zero;
913 u8 r, g, b;
914 u8 zero2;
915 u8 rumble;
916};
917
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500918#define DS4_REPORT_0x02_SIZE 37
919#define DS4_REPORT_0x05_SIZE 32
920#define DS4_REPORT_0x11_SIZE 78
921#define DS4_REPORT_0x81_SIZE 7
Antonio Ospite29b691a2015-02-16 18:12:21 +0100922#define SIXAXIS_REPORT_0xF2_SIZE 17
Antonio Ospitea85d67b2015-02-16 18:12:22 +0100923#define SIXAXIS_REPORT_0xF5_SIZE 8
Simon Wood41d2d422015-06-09 21:27:06 -0600924#define MOTION_REPORT_0x02_SIZE 49
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500925
Jiri Kosina8b402c92015-02-23 11:15:44 +0100926static DEFINE_SPINLOCK(sony_dev_list_lock);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500927static LIST_HEAD(sony_device_list);
Frank Praznik80250872014-04-14 10:11:35 -0400928static DEFINE_IDA(sony_device_id_allocator);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500929
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200930struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500931 spinlock_t lock;
Frank Praznikd2d782f2014-02-20 11:36:03 -0500932 struct list_head list_node;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100933 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500934 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200935 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100936 struct work_struct state_worker;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100937 struct power_supply *battery;
938 struct power_supply_desc battery_desc;
Frank Praznik80250872014-04-14 10:11:35 -0400939 int device_id;
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500940 __u8 *output_report_dmabuf;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200941
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100942#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100943 __u8 left;
944 __u8 right;
945#endif
946
Frank Praznikd2d782f2014-02-20 11:36:03 -0500947 __u8 mac_address[6];
Frank Praznik5f5750d2014-02-19 13:09:22 -0500948 __u8 worker_initialized;
Frank Praznikd902f472014-01-27 10:17:36 -0500949 __u8 cable_state;
950 __u8 battery_charging;
951 __u8 battery_capacity;
Frank Praznik60781cf2014-01-11 15:13:15 -0500952 __u8 led_state[MAX_LEDS];
Frank Praznikb3ed4582014-04-14 10:11:36 -0400953 __u8 led_delay_on[MAX_LEDS];
954 __u8 led_delay_off[MAX_LEDS];
Frank Praznik60781cf2014-01-11 15:13:15 -0500955 __u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200956};
957
Antonio Ospitec607fb82014-06-24 13:28:41 +0200958static __u8 *sixaxis_fixup(struct hid_device *hdev, __u8 *rdesc,
959 unsigned int *rsize)
960{
961 *rsize = sizeof(sixaxis_rdesc);
962 return sixaxis_rdesc;
963}
964
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400965static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
966 unsigned int *rsize)
967{
968 *rsize = sizeof(motion_rdesc);
969 return motion_rdesc;
970}
971
Jiri Kosina078328d2013-06-13 12:03:49 +0200972static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
973 unsigned int *rsize)
974{
975 *rsize = sizeof(ps3remote_rdesc);
976 return ps3remote_rdesc;
977}
978
979static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
980 struct hid_field *field, struct hid_usage *usage,
981 unsigned long **bit, int *max)
982{
983 unsigned int key = usage->hid & HID_USAGE;
984
985 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
986 return -1;
987
988 switch (usage->collection_index) {
989 case 1:
990 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
991 return -1;
992
993 key = ps3remote_keymap_joypad_buttons[key];
994 if (!key)
995 return -1;
996 break;
997 case 2:
998 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
999 return -1;
1000
1001 key = ps3remote_keymap_remote_buttons[key];
1002 if (!key)
1003 return -1;
1004 break;
1005 default:
1006 return -1;
1007 }
1008
1009 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1010 return 1;
1011}
1012
Nikolai Kondrashov73e40082010-08-06 23:03:06 +04001013static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
1014 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001015{
1016 struct sony_sc *sc = hid_get_drvdata(hdev);
1017
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +09001018 /*
1019 * Some Sony RF receivers wrongly declare the mouse pointer as a
1020 * a constant non-data variable.
1021 */
1022 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
1023 /* usage page: generic desktop controls */
1024 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
1025 /* usage: mouse */
1026 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
1027 /* input (usage page for x,y axes): constant, variable, relative */
1028 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa46491842013-01-15 19:40:48 +09001029 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +09001030 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001031 rdesc[55] = 0x06;
1032 }
Simon Wood61ab44b2011-06-10 12:00:26 +02001033
Frank Prazniked19d8c2014-01-16 21:43:12 -05001034 /*
1035 * The default Dualshock 4 USB descriptor doesn't assign
1036 * the gyroscope values to corresponding axes so we need a
1037 * modified one.
1038 */
1039 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
1040 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
1041 rdesc = dualshock4_usb_rdesc;
1042 *rsize = sizeof(dualshock4_usb_rdesc);
Frank Praznikd8296742014-02-05 20:03:45 -05001043 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
1044 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
1045 rdesc = dualshock4_bt_rdesc;
1046 *rsize = sizeof(dualshock4_bt_rdesc);
Frank Prazniked19d8c2014-01-16 21:43:12 -05001047 }
1048
Antonio Ospitec607fb82014-06-24 13:28:41 +02001049 if (sc->quirks & SIXAXIS_CONTROLLER)
1050 return sixaxis_fixup(hdev, rdesc, rsize);
Jiri Kosina078328d2013-06-13 12:03:49 +02001051
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001052 if (sc->quirks & MOTION_CONTROLLER)
1053 return motion_fixup(hdev, rdesc, rsize);
1054
Jiri Kosina078328d2013-06-13 12:03:49 +02001055 if (sc->quirks & PS3REMOTE)
1056 return ps3remote_fixup(hdev, rdesc, rsize);
1057
Nikolai Kondrashov73e40082010-08-06 23:03:06 +04001058 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001059}
1060
Frank Praznikd902f472014-01-27 10:17:36 -05001061static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1062{
1063 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
1064 unsigned long flags;
Simon Wood12e9a6d72015-06-09 21:27:05 -06001065 int offset;
Frank Praznikd902f472014-01-27 10:17:36 -05001066 __u8 cable_state, battery_capacity, battery_charging;
1067
Frank Praznikad142b92014-02-20 11:36:00 -05001068 /*
1069 * The sixaxis is charging if the battery value is 0xee
Frank Praznikd902f472014-01-27 10:17:36 -05001070 * and it is fully charged if the value is 0xef.
1071 * It does not report the actual level while charging so it
1072 * is set to 100% while charging is in progress.
1073 */
Simon Wood12e9a6d72015-06-09 21:27:05 -06001074 offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
1075
1076 if (rd[offset] >= 0xee) {
Frank Praznikd902f472014-01-27 10:17:36 -05001077 battery_capacity = 100;
Simon Wood12e9a6d72015-06-09 21:27:05 -06001078 battery_charging = !(rd[offset] & 0x01);
Frank Praznik9fddd742014-08-29 13:11:52 -04001079 cable_state = 1;
Frank Praznikd902f472014-01-27 10:17:36 -05001080 } else {
Simon Wood12e9a6d72015-06-09 21:27:05 -06001081 __u8 index = rd[offset] <= 5 ? rd[offset] : 5;
Frank Praznikac3c9a92014-02-20 11:36:02 -05001082 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -05001083 battery_charging = 0;
Frank Praznik9fddd742014-08-29 13:11:52 -04001084 cable_state = 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001085 }
Frank Praznikd902f472014-01-27 10:17:36 -05001086
1087 spin_lock_irqsave(&sc->lock, flags);
1088 sc->cable_state = cable_state;
1089 sc->battery_capacity = battery_capacity;
1090 sc->battery_charging = battery_charging;
1091 spin_unlock_irqrestore(&sc->lock, flags);
1092}
1093
1094static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1095{
Frank Praznike5606232014-01-27 10:17:37 -05001096 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1097 struct hid_input, list);
1098 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -05001099 unsigned long flags;
Frank Praznik6c5f8602014-02-05 20:03:47 -05001100 int n, offset;
Frank Praznikd902f472014-01-27 10:17:36 -05001101 __u8 cable_state, battery_capacity, battery_charging;
1102
Frank Praznikad142b92014-02-20 11:36:00 -05001103 /*
1104 * Battery and touchpad data starts at byte 30 in the USB report and
Frank Praznik6c5f8602014-02-05 20:03:47 -05001105 * 32 in Bluetooth report.
1106 */
1107 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
1108
Frank Praznikad142b92014-02-20 11:36:00 -05001109 /*
1110 * The lower 4 bits of byte 30 contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -05001111 * and the 5th bit contains the USB cable state.
1112 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001113 cable_state = (rd[offset] >> 4) & 0x01;
1114 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -05001115
Frank Praznikad142b92014-02-20 11:36:00 -05001116 /*
1117 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -05001118 * 0 to 10, and when running on battery power it ranges from 0 to 9.
1119 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -05001120 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001121 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -05001122 battery_charging = 0;
1123 else
1124 battery_charging = 1;
1125
Frank Praznik6c5f8602014-02-05 20:03:47 -05001126 if (!cable_state)
1127 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -05001128 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -05001129 battery_capacity = 10;
1130
Frank Praznikd902f472014-01-27 10:17:36 -05001131 battery_capacity *= 10;
1132
1133 spin_lock_irqsave(&sc->lock, flags);
1134 sc->cable_state = cable_state;
1135 sc->battery_capacity = battery_capacity;
1136 sc->battery_charging = battery_charging;
1137 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -05001138
Frank Praznik6c5f8602014-02-05 20:03:47 -05001139 offset += 5;
1140
Frank Praznikad142b92014-02-20 11:36:00 -05001141 /*
1142 * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
Frank Praznik6c5f8602014-02-05 20:03:47 -05001143 * and 37 on Bluetooth.
Frank Praznike5606232014-01-27 10:17:37 -05001144 * The first 7 bits of the first byte is a counter and bit 8 is a touch
1145 * indicator that is 0 when pressed and 1 when not pressed.
1146 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1147 * The data for the second touch is in the same format and immediatly
1148 * follows the data for the first.
1149 */
1150 for (n = 0; n < 2; n++) {
1151 __u16 x, y;
1152
1153 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1154 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1155
1156 input_mt_slot(input_dev, n);
1157 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
1158 !(rd[offset] >> 7));
1159 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1160 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1161
1162 offset += 4;
1163 }
Frank Praznikd902f472014-01-27 10:17:36 -05001164}
1165
Simon Woodc9e4d872011-06-10 12:00:27 +02001166static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
1167 __u8 *rd, int size)
1168{
1169 struct sony_sc *sc = hid_get_drvdata(hdev);
1170
Frank Praznikad142b92014-02-20 11:36:00 -05001171 /*
1172 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +02001173 * has to be BYTE_SWAPPED before passing up to joystick interface
1174 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001175 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Simon Woodc9e4d872011-06-10 12:00:27 +02001176 swap(rd[41], rd[42]);
1177 swap(rd[43], rd[44]);
1178 swap(rd[45], rd[46]);
1179 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -05001180
1181 sixaxis_parse_report(sc, rd, size);
Simon Wood12e9a6d72015-06-09 21:27:05 -06001182 } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
1183 sixaxis_parse_report(sc, rd, size);
Frank Praznik68330d82014-02-05 20:03:49 -05001184 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1185 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1186 && rd[0] == 0x11 && size == 78)) {
Frank Praznikd902f472014-01-27 10:17:36 -05001187 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +02001188 }
1189
1190 return 0;
1191}
1192
Colin Leitnerf04d5142013-05-27 23:41:05 +02001193static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1194 struct hid_field *field, struct hid_usage *usage,
1195 unsigned long **bit, int *max)
1196{
1197 struct sony_sc *sc = hid_get_drvdata(hdev);
1198
1199 if (sc->quirks & BUZZ_CONTROLLER) {
1200 unsigned int key = usage->hid & HID_USAGE;
1201
1202 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1203 return -1;
1204
1205 switch (usage->collection_index) {
1206 case 1:
1207 if (key >= ARRAY_SIZE(buzz_keymap))
1208 return -1;
1209
1210 key = buzz_keymap[key];
1211 if (!key)
1212 return -1;
1213 break;
1214 default:
1215 return -1;
1216 }
1217
1218 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1219 return 1;
1220 }
1221
Jiri Kosina078328d2013-06-13 12:03:49 +02001222 if (sc->quirks & PS3REMOTE)
1223 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1224
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001225 /* Let hid-core decide for the others */
1226 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001227}
1228
Frank Praznikce8efc32014-09-18 21:15:01 -04001229static int sony_register_touchpad(struct hid_input *hi, int touch_count,
1230 int w, int h)
1231{
1232 struct input_dev *input_dev = hi->input;
1233 int ret;
1234
1235 ret = input_mt_init_slots(input_dev, touch_count, 0);
1236 if (ret < 0)
1237 return ret;
1238
1239 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1240 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1241
1242 return 0;
1243}
1244
1245static void sony_input_configured(struct hid_device *hdev,
1246 struct hid_input *hidinput)
1247{
1248 struct sony_sc *sc = hid_get_drvdata(hdev);
1249
1250 /*
1251 * The Dualshock 4 touchpad supports 2 touches and has a
Frank Praznik981c5b42014-09-24 09:38:19 -04001252 * resolution of 1920x942 (44.86 dots/mm).
Frank Praznikce8efc32014-09-18 21:15:01 -04001253 */
1254 if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik981c5b42014-09-24 09:38:19 -04001255 if (sony_register_touchpad(hidinput, 2, 1920, 942) != 0)
Frank Praznikce8efc32014-09-18 21:15:01 -04001256 hid_err(sc->hdev,
1257 "Unable to initialize multi-touch slots\n");
1258 }
1259}
1260
Antonio Ospite5710fabf2011-02-20 18:26:45 +01001261/*
Jiri Slabybd28ce02008-06-25 23:47:04 +02001262 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1263 * to "operational". Without this, the ps3 controller will not report any
1264 * events.
1265 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001266static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001267{
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001268 const int buf_size =
1269 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
Antonio Ospite2e701a32015-02-16 18:12:24 +01001270 __u8 *buf;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001271 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001272
Antonio Ospite2e701a32015-02-16 18:12:24 +01001273 buf = kmalloc(buf_size, GFP_KERNEL);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001274 if (!buf)
1275 return -ENOMEM;
1276
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001277 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1278 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001279 if (ret < 0) {
1280 hid_err(hdev, "can't set operational mode: step 1\n");
1281 goto out;
1282 }
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001283
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001284 /*
1285 * Some compatible controllers like the Speedlink Strike FX and
1286 * Gasia need another query plus an USB interrupt to get operational.
1287 */
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001288 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1289 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001290 if (ret < 0) {
1291 hid_err(hdev, "can't set operational mode: step 2\n");
1292 goto out;
1293 }
1294
1295 ret = hid_hw_output_report(hdev, buf, 1);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001296 if (ret < 0)
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001297 hid_err(hdev, "can't set operational mode: step 3\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +02001298
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001299out:
Jiri Slabybd28ce02008-06-25 23:47:04 +02001300 kfree(buf);
1301
1302 return ret;
1303}
1304
Antonio Ospite816651a2010-05-03 22:15:55 +02001305static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001306{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001307 static const __u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1308 __u8 *buf;
1309 int ret;
1310
1311 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1312 if (!buf)
1313 return -ENOMEM;
1314
1315 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
Benjamin Tissoiresb0dd72a2014-02-10 12:58:54 -05001316 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001317
1318 kfree(buf);
1319
1320 return ret;
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001321}
1322
Frank Praznikad142b92014-02-20 11:36:00 -05001323/*
1324 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
Frank Praznik68330d82014-02-05 20:03:49 -05001325 * controller so that it sends full input reports of type 0x11.
1326 */
1327static int dualshock4_set_operational_bt(struct hid_device *hdev)
1328{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001329 __u8 *buf;
1330 int ret;
Frank Praznik68330d82014-02-05 20:03:49 -05001331
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001332 buf = kmalloc(DS4_REPORT_0x02_SIZE, GFP_KERNEL);
1333 if (!buf)
1334 return -ENOMEM;
1335
1336 ret = hid_hw_raw_request(hdev, 0x02, buf, DS4_REPORT_0x02_SIZE,
Frank Praznik68330d82014-02-05 20:03:49 -05001337 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001338
1339 kfree(buf);
1340
1341 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001342}
1343
Frank Praznik221399b2015-05-05 20:47:32 -04001344static void sixaxis_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001345{
1346 static const __u8 sixaxis_leds[10][4] = {
1347 { 0x01, 0x00, 0x00, 0x00 },
1348 { 0x00, 0x01, 0x00, 0x00 },
1349 { 0x00, 0x00, 0x01, 0x00 },
1350 { 0x00, 0x00, 0x00, 0x01 },
1351 { 0x01, 0x00, 0x00, 0x01 },
1352 { 0x00, 0x01, 0x00, 0x01 },
1353 { 0x00, 0x00, 0x01, 0x01 },
1354 { 0x01, 0x00, 0x01, 0x01 },
1355 { 0x00, 0x01, 0x01, 0x01 },
1356 { 0x01, 0x01, 0x01, 0x01 }
1357 };
1358
Frank Praznik221399b2015-05-05 20:47:32 -04001359 int id = sc->device_id;
1360
1361 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001362
1363 if (id < 0)
1364 return;
1365
1366 id %= 10;
Frank Praznik221399b2015-05-05 20:47:32 -04001367 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001368}
1369
Frank Praznik221399b2015-05-05 20:47:32 -04001370static void dualshock4_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001371{
1372 /* The first 4 color/index entries match what the PS4 assigns */
1373 static const __u8 color_code[7][3] = {
1374 /* Blue */ { 0x00, 0x00, 0x01 },
1375 /* Red */ { 0x01, 0x00, 0x00 },
1376 /* Green */ { 0x00, 0x01, 0x00 },
1377 /* Pink */ { 0x02, 0x00, 0x01 },
1378 /* Orange */ { 0x02, 0x01, 0x00 },
1379 /* Teal */ { 0x00, 0x01, 0x01 },
1380 /* White */ { 0x01, 0x01, 0x01 }
1381 };
1382
Frank Praznik221399b2015-05-05 20:47:32 -04001383 int id = sc->device_id;
1384
1385 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001386
1387 if (id < 0)
1388 return;
1389
1390 id %= 7;
Frank Praznik221399b2015-05-05 20:47:32 -04001391 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001392}
1393
Frank Praznik221399b2015-05-05 20:47:32 -04001394static void buzz_set_leds(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001395{
Frank Praznik221399b2015-05-05 20:47:32 -04001396 struct hid_device *hdev = sc->hdev;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001397 struct list_head *report_list =
1398 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1399 struct hid_report *report = list_entry(report_list->next,
1400 struct hid_report, list);
1401 __s32 *value = report->field[0]->value;
1402
Frank Praznik221399b2015-05-05 20:47:32 -04001403 BUILD_BUG_ON(MAX_LEDS < 4);
1404
Colin Leitnerf04d5142013-05-27 23:41:05 +02001405 value[0] = 0x00;
Frank Praznik221399b2015-05-05 20:47:32 -04001406 value[1] = sc->led_state[0] ? 0xff : 0x00;
1407 value[2] = sc->led_state[1] ? 0xff : 0x00;
1408 value[3] = sc->led_state[2] ? 0xff : 0x00;
1409 value[4] = sc->led_state[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001410 value[5] = 0x00;
1411 value[6] = 0x00;
1412 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1413}
1414
Frank Praznik221399b2015-05-05 20:47:32 -04001415static void sony_set_leds(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001416{
Frank Praznik221399b2015-05-05 20:47:32 -04001417 if (!(sc->quirks & BUZZ_CONTROLLER))
Frank Praznikfa57a812014-04-14 10:11:33 -04001418 schedule_work(&sc->state_worker);
Frank Praznik221399b2015-05-05 20:47:32 -04001419 else
1420 buzz_set_leds(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001421}
1422
Sven Eckelmannc5382512013-11-19 20:26:30 +01001423static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001424 enum led_brightness value)
1425{
1426 struct device *dev = led->dev->parent;
1427 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1428 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001429
1430 int n;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001431 int force_update;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001432
1433 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001434 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001435 hid_err(hdev, "No device data\n");
1436 return;
1437 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001438
Frank Praznikb3ed4582014-04-14 10:11:36 -04001439 /*
1440 * The Sixaxis on USB will override any LED settings sent to it
1441 * and keep flashing all of the LEDs until the PS button is pressed.
1442 * Updates, even if redundant, must be always be sent to the
1443 * controller to avoid having to toggle the state of an LED just to
1444 * stop the flashing later on.
1445 */
1446 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1447
Frank Praznik60781cf2014-01-11 15:13:15 -05001448 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznikb3ed4582014-04-14 10:11:36 -04001449 if (led == drv_data->leds[n] && (force_update ||
1450 (value != drv_data->led_state[n] ||
1451 drv_data->led_delay_on[n] ||
1452 drv_data->led_delay_off[n]))) {
1453
1454 drv_data->led_state[n] = value;
1455
1456 /* Setting the brightness stops the blinking */
1457 drv_data->led_delay_on[n] = 0;
1458 drv_data->led_delay_off[n] = 0;
1459
Frank Praznik221399b2015-05-05 20:47:32 -04001460 sony_set_leds(drv_data);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001461 break;
1462 }
1463 }
1464}
1465
Sven Eckelmannc5382512013-11-19 20:26:30 +01001466static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001467{
1468 struct device *dev = led->dev->parent;
1469 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1470 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001471
1472 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001473
1474 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001475 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001476 hid_err(hdev, "No device data\n");
1477 return LED_OFF;
1478 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001479
Frank Praznik60781cf2014-01-11 15:13:15 -05001480 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001481 if (led == drv_data->leds[n])
1482 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001483 }
1484
Simon Wood7db75042014-02-05 12:34:18 -07001485 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001486}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001487
Frank Praznikb3ed4582014-04-14 10:11:36 -04001488static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1489 unsigned long *delay_off)
1490{
1491 struct device *dev = led->dev->parent;
1492 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1493 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1494 int n;
1495 __u8 new_on, new_off;
1496
1497 if (!drv_data) {
1498 hid_err(hdev, "No device data\n");
1499 return -EINVAL;
1500 }
1501
1502 /* Max delay is 255 deciseconds or 2550 milliseconds */
1503 if (*delay_on > 2550)
1504 *delay_on = 2550;
1505 if (*delay_off > 2550)
1506 *delay_off = 2550;
1507
1508 /* Blink at 1 Hz if both values are zero */
1509 if (!*delay_on && !*delay_off)
1510 *delay_on = *delay_off = 500;
1511
1512 new_on = *delay_on / 10;
1513 new_off = *delay_off / 10;
1514
1515 for (n = 0; n < drv_data->led_count; n++) {
1516 if (led == drv_data->leds[n])
1517 break;
1518 }
1519
1520 /* This LED is not registered on this device */
1521 if (n >= drv_data->led_count)
1522 return -EINVAL;
1523
1524 /* Don't schedule work if the values didn't change */
1525 if (new_on != drv_data->led_delay_on[n] ||
1526 new_off != drv_data->led_delay_off[n]) {
1527 drv_data->led_delay_on[n] = new_on;
1528 drv_data->led_delay_off[n] = new_off;
1529 schedule_work(&drv_data->state_worker);
1530 }
1531
1532 return 0;
1533}
1534
Frank Praznikfa57a812014-04-14 10:11:33 -04001535static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001536{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001537 struct led_classdev *led;
1538 int n;
1539
Frank Praznikfa57a812014-04-14 10:11:33 -04001540 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001541
Frank Praznikfa57a812014-04-14 10:11:33 -04001542 for (n = 0; n < sc->led_count; n++) {
1543 led = sc->leds[n];
1544 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001545 if (!led)
1546 continue;
1547 led_classdev_unregister(led);
1548 kfree(led);
1549 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001550
Frank Praznikfa57a812014-04-14 10:11:33 -04001551 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001552}
1553
Frank Praznikfa57a812014-04-14 10:11:33 -04001554static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001555{
Frank Praznikfa57a812014-04-14 10:11:33 -04001556 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001557 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001558 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001559 struct led_classdev *led;
1560 size_t name_sz;
1561 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001562 size_t name_len;
1563 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001564 static const char * const ds4_name_str[] = { "red", "green", "blue",
1565 "global" };
Frank Praznik5607c892014-07-29 19:55:48 -07001566 __u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
Frank Praznikb3ed4582014-04-14 10:11:36 -04001567 __u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001568
Frank Praznikfa57a812014-04-14 10:11:33 -04001569 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001570
Frank Praznikfa57a812014-04-14 10:11:33 -04001571 if (sc->quirks & BUZZ_CONTROLLER) {
1572 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001573 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001574 name_len = strlen("::buzz#");
1575 name_fmt = "%s::buzz%d";
1576 /* Validate expected report characteristics. */
1577 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1578 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001579 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik221399b2015-05-05 20:47:32 -04001580 dualshock4_set_leds_from_id(sc);
1581 sc->led_state[3] = 1;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001582 sc->led_count = 4;
1583 memset(max_brightness, 255, 3);
1584 use_hw_blink[3] = 1;
1585 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001586 name_len = 0;
1587 name_fmt = "%s:%s";
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001588 } else if (sc->quirks & MOTION_CONTROLLER) {
1589 sc->led_count = 3;
1590 memset(max_brightness, 255, 3);
1591 use_ds4_names = 1;
1592 name_len = 0;
1593 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -05001594 } else {
Frank Praznik221399b2015-05-05 20:47:32 -04001595 sixaxis_set_leds_from_id(sc);
Frank Praznikfa57a812014-04-14 10:11:33 -04001596 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001597 memset(use_hw_blink, 1, 4);
1598 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001599 name_len = strlen("::sony#");
1600 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001601 }
1602
Frank Praznikad142b92014-02-20 11:36:00 -05001603 /*
1604 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001605 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001606 * LEDs to on
1607 */
Frank Praznik221399b2015-05-05 20:47:32 -04001608 sony_set_leds(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001609
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001610 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001611
Frank Praznikfa57a812014-04-14 10:11:33 -04001612 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001613
Frank Praznikb3ed4582014-04-14 10:11:36 -04001614 if (use_ds4_names)
1615 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001616
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001617 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1618 if (!led) {
1619 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001620 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001621 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001622 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001623
1624 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001625 if (use_ds4_names)
1626 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1627 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001628 else
1629 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001630 led->name = name;
Frank Praznik221399b2015-05-05 20:47:32 -04001631 led->brightness = sc->led_state[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001632 led->max_brightness = max_brightness[n];
Sven Eckelmannc5382512013-11-19 20:26:30 +01001633 led->brightness_get = sony_led_get_brightness;
1634 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001635
Frank Praznikb3ed4582014-04-14 10:11:36 -04001636 if (use_hw_blink[n])
1637 led->blink_set = sony_led_blink_set;
1638
Frank Praznik80250872014-04-14 10:11:35 -04001639 sc->leds[n] = led;
1640
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001641 ret = led_classdev_register(&hdev->dev, led);
1642 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001643 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001644 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001645 kfree(led);
1646 goto error_leds;
1647 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001648 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001649
1650 return ret;
1651
Colin Leitnerf04d5142013-05-27 23:41:05 +02001652error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001653 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001654
Colin Leitnerf04d5142013-05-27 23:41:05 +02001655 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001656}
1657
Frank Praznikcad665a2014-01-11 15:13:54 -05001658static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001659{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001660 static const union sixaxis_output_report_01 default_report = {
Frank Praznik55d3b662014-04-14 10:11:32 -04001661 .buf = {
1662 0x01,
1663 0x00, 0xff, 0x00, 0xff, 0x00,
1664 0x00, 0x00, 0x00, 0x00, 0x00,
1665 0xff, 0x27, 0x10, 0x00, 0x32,
1666 0xff, 0x27, 0x10, 0x00, 0x32,
1667 0xff, 0x27, 0x10, 0x00, 0x32,
1668 0xff, 0x27, 0x10, 0x00, 0x32,
1669 0x00, 0x00, 0x00, 0x00, 0x00
1670 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001671 };
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001672 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1673 struct sixaxis_output_report *report =
1674 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1675 int n;
1676
1677 /* Initialize the report with default values */
1678 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001679
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001680#ifdef CONFIG_SONY_FF
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001681 report->rumble.right_motor_on = sc->right ? 1 : 0;
1682 report->rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001683#endif
1684
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001685 report->leds_bitmap |= sc->led_state[0] << 1;
1686 report->leds_bitmap |= sc->led_state[1] << 2;
1687 report->leds_bitmap |= sc->led_state[2] << 3;
1688 report->leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001689
Simon Wood88f65762014-04-14 10:11:37 -04001690 /* Set flag for all leds off, required for 3rd party INTEC controller */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001691 if ((report->leds_bitmap & 0x1E) == 0)
1692 report->leds_bitmap |= 0x20;
Simon Wood88f65762014-04-14 10:11:37 -04001693
Frank Praznikb3ed4582014-04-14 10:11:36 -04001694 /*
1695 * The LEDs in the report are indexed in reverse order to their
1696 * corresponding light on the controller.
1697 * Index 0 = LED 4, index 1 = LED 3, etc...
1698 *
1699 * In the case of both delay values being zero (blinking disabled) the
1700 * default report values should be used or the controller LED will be
1701 * always off.
1702 */
1703 for (n = 0; n < 4; n++) {
1704 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001705 report->led[3 - n].duty_off = sc->led_delay_off[n];
1706 report->led[3 - n].duty_on = sc->led_delay_on[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001707 }
1708 }
1709
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001710 hid_hw_raw_request(sc->hdev, report->report_id, (__u8 *)report,
1711 sizeof(struct sixaxis_output_report),
1712 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001713}
1714
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001715static void dualshock4_state_worker(struct work_struct *work)
1716{
1717 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001718 struct hid_device *hdev = sc->hdev;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001719 __u8 *buf = sc->output_report_dmabuf;
Frank Praznik48220232014-02-05 20:03:44 -05001720 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001721
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001722 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001723 memset(buf, 0, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001724 buf[0] = 0x05;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001725 buf[1] = 0xFF;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001726 offset = 4;
1727 } else {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001728 memset(buf, 0, DS4_REPORT_0x11_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001729 buf[0] = 0x11;
1730 buf[1] = 0xB0;
1731 buf[3] = 0x0F;
1732 offset = 6;
1733 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001734
1735#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001736 buf[offset++] = sc->right;
1737 buf[offset++] = sc->left;
1738#else
1739 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001740#endif
1741
Frank Praznikb3ed4582014-04-14 10:11:36 -04001742 /* LED 3 is the global control */
1743 if (sc->led_state[3]) {
1744 buf[offset++] = sc->led_state[0];
1745 buf[offset++] = sc->led_state[1];
1746 buf[offset++] = sc->led_state[2];
1747 } else {
1748 offset += 3;
1749 }
1750
1751 /* If both delay values are zero the DualShock 4 disables blinking. */
1752 buf[offset++] = sc->led_delay_on[3];
1753 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05001754
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001755 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001756 hid_hw_output_report(hdev, buf, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001757 else
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001758 hid_hw_raw_request(hdev, 0x11, buf, DS4_REPORT_0x11_SIZE,
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001759 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001760}
1761
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001762static void motion_state_worker(struct work_struct *work)
1763{
1764 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1765 struct hid_device *hdev = sc->hdev;
1766 struct motion_output_report_02 *report =
1767 (struct motion_output_report_02 *)sc->output_report_dmabuf;
1768
Simon Wood41d2d422015-06-09 21:27:06 -06001769 memset(report, 0, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001770
1771 report->type = 0x02; /* set leds */
1772 report->r = sc->led_state[0];
1773 report->g = sc->led_state[1];
1774 report->b = sc->led_state[2];
1775
1776#ifdef CONFIG_SONY_FF
1777 report->rumble = max(sc->right, sc->left);
1778#endif
1779
Simon Wood41d2d422015-06-09 21:27:06 -06001780 hid_hw_output_report(hdev, (__u8 *)report, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001781}
1782
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001783static int sony_allocate_output_report(struct sony_sc *sc)
1784{
1785 if (sc->quirks & SIXAXIS_CONTROLLER)
1786 sc->output_report_dmabuf =
1787 kmalloc(sizeof(union sixaxis_output_report_01),
1788 GFP_KERNEL);
1789 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1790 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x11_SIZE,
1791 GFP_KERNEL);
1792 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1793 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x05_SIZE,
1794 GFP_KERNEL);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001795 else if (sc->quirks & MOTION_CONTROLLER)
Simon Wood41d2d422015-06-09 21:27:06 -06001796 sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE,
1797 GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001798 else
1799 return 0;
1800
1801 if (!sc->output_report_dmabuf)
1802 return -ENOMEM;
1803
1804 return 0;
1805}
1806
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001807#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001808static int sony_play_effect(struct input_dev *dev, void *data,
1809 struct ff_effect *effect)
1810{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001811 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001812 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001813
1814 if (effect->type != FF_RUMBLE)
1815 return 0;
1816
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001817 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001818 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001819
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001820 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001821 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001822}
1823
Frank Praznikfa57a812014-04-14 10:11:33 -04001824static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001825{
Frank Praznikfa57a812014-04-14 10:11:33 -04001826 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001827 struct hid_input, list);
1828 struct input_dev *input_dev = hidinput->input;
1829
1830 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1831 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1832}
1833
1834#else
Frank Praznikfa57a812014-04-14 10:11:33 -04001835static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001836{
1837 return 0;
1838}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001839
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001840#endif
1841
Frank Praznikd902f472014-01-27 10:17:36 -05001842static int sony_battery_get_property(struct power_supply *psy,
1843 enum power_supply_property psp,
1844 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001845{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001846 struct sony_sc *sc = power_supply_get_drvdata(psy);
Frank Praznikd902f472014-01-27 10:17:36 -05001847 unsigned long flags;
1848 int ret = 0;
1849 u8 battery_charging, battery_capacity, cable_state;
1850
1851 spin_lock_irqsave(&sc->lock, flags);
1852 battery_charging = sc->battery_charging;
1853 battery_capacity = sc->battery_capacity;
1854 cable_state = sc->cable_state;
1855 spin_unlock_irqrestore(&sc->lock, flags);
1856
1857 switch (psp) {
1858 case POWER_SUPPLY_PROP_PRESENT:
1859 val->intval = 1;
1860 break;
1861 case POWER_SUPPLY_PROP_SCOPE:
1862 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1863 break;
1864 case POWER_SUPPLY_PROP_CAPACITY:
1865 val->intval = battery_capacity;
1866 break;
1867 case POWER_SUPPLY_PROP_STATUS:
1868 if (battery_charging)
1869 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1870 else
1871 if (battery_capacity == 100 && cable_state)
1872 val->intval = POWER_SUPPLY_STATUS_FULL;
1873 else
1874 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1875 break;
1876 default:
1877 ret = -EINVAL;
1878 break;
1879 }
1880 return ret;
1881}
1882
1883static int sony_battery_probe(struct sony_sc *sc)
1884{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001885 struct power_supply_config psy_cfg = { .drv_data = sc, };
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001886 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05001887 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001888
Frank Praznikad142b92014-02-20 11:36:00 -05001889 /*
1890 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05001891 * if the battery is polled before the first device report is received.
1892 */
1893 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001894
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001895 sc->battery_desc.properties = sony_battery_props;
1896 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
1897 sc->battery_desc.get_property = sony_battery_get_property;
1898 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
1899 sc->battery_desc.use_for_apm = 0;
1900 sc->battery_desc.name = kasprintf(GFP_KERNEL,
1901 "sony_controller_battery_%pMR",
1902 sc->mac_address);
1903 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05001904 return -ENOMEM;
1905
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001906 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
1907 &psy_cfg);
1908 if (IS_ERR(sc->battery)) {
1909 ret = PTR_ERR(sc->battery);
Frank Praznikd902f472014-01-27 10:17:36 -05001910 hid_err(hdev, "Unable to register battery device\n");
1911 goto err_free;
1912 }
1913
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001914 power_supply_powers(sc->battery, &hdev->dev);
Frank Praznikd902f472014-01-27 10:17:36 -05001915 return 0;
1916
1917err_free:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001918 kfree(sc->battery_desc.name);
1919 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05001920 return ret;
1921}
1922
1923static void sony_battery_remove(struct sony_sc *sc)
1924{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001925 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05001926 return;
1927
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001928 power_supply_unregister(sc->battery);
1929 kfree(sc->battery_desc.name);
1930 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05001931}
1932
Frank Praznikd2d782f2014-02-20 11:36:03 -05001933/*
1934 * If a controller is plugged in via USB while already connected via Bluetooth
1935 * it will show up as two devices. A global list of connected controllers and
1936 * their MAC addresses is maintained to ensure that a device is only connected
1937 * once.
1938 */
1939static int sony_check_add_dev_list(struct sony_sc *sc)
1940{
1941 struct sony_sc *entry;
1942 unsigned long flags;
1943 int ret;
1944
1945 spin_lock_irqsave(&sony_dev_list_lock, flags);
1946
1947 list_for_each_entry(entry, &sony_device_list, list_node) {
1948 ret = memcmp(sc->mac_address, entry->mac_address,
1949 sizeof(sc->mac_address));
1950 if (!ret) {
1951 ret = -EEXIST;
1952 hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
1953 sc->mac_address);
1954 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001955 }
1956 }
1957
Frank Praznikd2d782f2014-02-20 11:36:03 -05001958 ret = 0;
1959 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001960
Frank Praznikd2d782f2014-02-20 11:36:03 -05001961unlock:
1962 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1963 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001964}
1965
Frank Praznikd2d782f2014-02-20 11:36:03 -05001966static void sony_remove_dev_list(struct sony_sc *sc)
1967{
1968 unsigned long flags;
1969
1970 if (sc->list_node.next) {
1971 spin_lock_irqsave(&sony_dev_list_lock, flags);
1972 list_del(&(sc->list_node));
1973 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1974 }
1975}
1976
1977static int sony_get_bt_devaddr(struct sony_sc *sc)
1978{
1979 int ret;
1980
1981 /* HIDP stores the device MAC address as a string in the uniq field. */
1982 ret = strlen(sc->hdev->uniq);
1983 if (ret != 17)
1984 return -EINVAL;
1985
1986 ret = sscanf(sc->hdev->uniq,
1987 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1988 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
1989 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
1990
1991 if (ret != 6)
1992 return -EINVAL;
1993
1994 return 0;
1995}
1996
1997static int sony_check_add(struct sony_sc *sc)
1998{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001999 __u8 *buf = NULL;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002000 int n, ret;
2001
2002 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
Simon Wood12e9a6d72015-06-09 21:27:05 -06002003 (sc->quirks & MOTION_CONTROLLER_BT) ||
Frank Praznikd2d782f2014-02-20 11:36:03 -05002004 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
2005 /*
2006 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
2007 * address from the uniq string where HIDP stores it.
2008 * As uniq cannot be guaranteed to be a MAC address in all cases
2009 * a failure of this function should not prevent the connection.
2010 */
2011 if (sony_get_bt_devaddr(sc) < 0) {
2012 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
2013 return 0;
2014 }
2015 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002016 buf = kmalloc(DS4_REPORT_0x81_SIZE, GFP_KERNEL);
2017 if (!buf)
2018 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002019
2020 /*
2021 * The MAC address of a DS4 controller connected via USB can be
2022 * retrieved with feature report 0x81. The address begins at
2023 * offset 1.
2024 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002025 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
2026 DS4_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
2027 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002028
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002029 if (ret != DS4_REPORT_0x81_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002030 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002031 ret = ret < 0 ? ret : -EINVAL;
2032 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002033 }
2034
2035 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
2036 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002037 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2038 if (!buf)
2039 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002040
2041 /*
2042 * The MAC address of a Sixaxis controller connected via USB can
2043 * be retrieved with feature report 0xf2. The address begins at
2044 * offset 4.
2045 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002046 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2047 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2048 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002049
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002050 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002051 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002052 ret = ret < 0 ? ret : -EINVAL;
2053 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002054 }
2055
2056 /*
2057 * The Sixaxis device MAC in the report is big-endian and must
2058 * be byte-swapped.
2059 */
2060 for (n = 0; n < 6; n++)
2061 sc->mac_address[5-n] = buf[4+n];
2062 } else {
2063 return 0;
2064 }
2065
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002066 ret = sony_check_add_dev_list(sc);
2067
2068out_free:
2069
2070 kfree(buf);
2071
2072 return ret;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002073}
2074
Frank Praznik80250872014-04-14 10:11:35 -04002075static int sony_set_device_id(struct sony_sc *sc)
2076{
2077 int ret;
2078
2079 /*
2080 * Only DualShock 4 or Sixaxis controllers get an id.
2081 * All others are set to -1.
2082 */
2083 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2084 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2085 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2086 GFP_KERNEL);
2087 if (ret < 0) {
2088 sc->device_id = -1;
2089 return ret;
2090 }
2091 sc->device_id = ret;
2092 } else {
2093 sc->device_id = -1;
2094 }
2095
2096 return 0;
2097}
2098
2099static void sony_release_device_id(struct sony_sc *sc)
2100{
2101 if (sc->device_id >= 0) {
2102 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2103 sc->device_id = -1;
2104 }
2105}
2106
Frank Praznik46262042014-04-14 10:11:31 -04002107static inline void sony_init_work(struct sony_sc *sc,
2108 void (*worker)(struct work_struct *))
2109{
2110 if (!sc->worker_initialized)
2111 INIT_WORK(&sc->state_worker, worker);
2112
2113 sc->worker_initialized = 1;
2114}
2115
2116static inline void sony_cancel_work_sync(struct sony_sc *sc)
2117{
2118 if (sc->worker_initialized)
2119 cancel_work_sync(&sc->state_worker);
2120}
Frank Praznikd2d782f2014-02-20 11:36:03 -05002121
Jiri Slabybd28ce02008-06-25 23:47:04 +02002122static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2123{
2124 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002125 unsigned long quirks = id->driver_data;
2126 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02002127 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002128
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002129 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002130 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08002131 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002132 return -ENOMEM;
2133 }
2134
Frank Praznikb94993f2015-02-22 20:42:46 -05002135 spin_lock_init(&sc->lock);
2136
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002137 sc->quirks = quirks;
2138 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002139 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002140
Jiri Slabybd28ce02008-06-25 23:47:04 +02002141 ret = hid_parse(hdev);
2142 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002143 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002144 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002145 }
2146
Colin Leitnerf04d5142013-05-27 23:41:05 +02002147 if (sc->quirks & VAIO_RDESC_CONSTANT)
2148 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
Antonio Ospite50764652014-06-24 13:28:40 +02002149 else if (sc->quirks & SIXAXIS_CONTROLLER)
Colin Leitnerf04d5142013-05-27 23:41:05 +02002150 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2151
2152 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002153 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002154 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002155 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002156 }
2157
Frank Praznik80250872014-04-14 10:11:35 -04002158 ret = sony_set_device_id(sc);
2159 if (ret < 0) {
2160 hid_err(hdev, "failed to allocate the device id\n");
2161 goto err_stop;
2162 }
2163
Frank Praznik131a8a92015-05-05 20:47:28 -04002164 ret = sony_allocate_output_report(sc);
2165 if (ret < 0) {
2166 hid_err(hdev, "failed to allocate the output report buffer\n");
2167 goto err_stop;
2168 }
2169
Antonio Ospite569b10a2010-10-19 16:13:10 +02002170 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002171 /*
2172 * The Sony Sixaxis does not handle HID Output Reports on the
2173 * Interrupt EP like it could, so we need to force HID Output
2174 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2175 *
2176 * There is also another issue about HID Output Reports via USB,
2177 * the Sixaxis does not want the report_id as part of the data
2178 * packet, so we have to discard buf[0] when sending the actual
2179 * control message, even for numbered reports, humpf!
2180 */
2181 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2182 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
Antonio Ospite816651a2010-05-03 22:15:55 +02002183 ret = sixaxis_set_operational_usb(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04002184 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002185 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04002186 /*
2187 * The Sixaxis wants output reports sent on the ctrl endpoint
2188 * when connected via Bluetooth.
2189 */
2190 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Antonio Ospite816651a2010-05-03 22:15:55 +02002191 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04002192 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002193 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik68330d82014-02-05 20:03:49 -05002194 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04002195 /*
2196 * The DualShock 4 wants output reports sent on the ctrl
2197 * endpoint when connected via Bluetooth.
2198 */
2199 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Frank Praznik68330d82014-02-05 20:03:49 -05002200 ret = dualshock4_set_operational_bt(hdev);
2201 if (ret < 0) {
2202 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
2203 goto err_stop;
2204 }
2205 }
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002206
Frank Praznik46262042014-04-14 10:11:31 -04002207 sony_init_work(sc, dualshock4_state_worker);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002208 } else if (sc->quirks & MOTION_CONTROLLER) {
2209 sony_init_work(sc, motion_state_worker);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002210 } else {
2211 ret = 0;
2212 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00002213
Jiri Kosina4dfdc462008-12-30 00:49:59 +01002214 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002215 goto err_stop;
2216
Frank Praznikd2d782f2014-02-20 11:36:03 -05002217 ret = sony_check_add(sc);
2218 if (ret < 0)
2219 goto err_stop;
2220
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002221 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002222 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002223 if (ret < 0)
2224 goto err_stop;
2225 }
2226
Frank Praznikd902f472014-01-27 10:17:36 -05002227 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2228 ret = sony_battery_probe(sc);
2229 if (ret < 0)
2230 goto err_stop;
2231
2232 /* Open the device to receive reports with battery info */
2233 ret = hid_hw_open(hdev);
2234 if (ret < 0) {
2235 hid_err(hdev, "hw open failed\n");
2236 goto err_stop;
2237 }
2238 }
2239
Frank Praznikc8de9db2014-02-20 11:36:01 -05002240 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002241 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05002242 if (ret < 0)
2243 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05002244 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002245
Jiri Slabybd28ce02008-06-25 23:47:04 +02002246 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05002247err_close:
2248 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002249err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002250 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002251 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002252 if (sc->quirks & SONY_BATTERY_SUPPORT)
2253 sony_battery_remove(sc);
Frank Praznik46262042014-04-14 10:11:31 -04002254 sony_cancel_work_sync(sc);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002255 kfree(sc->output_report_dmabuf);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002256 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04002257 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002258 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002259 return ret;
2260}
2261
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002262static void sony_remove(struct hid_device *hdev)
2263{
Colin Leitnerf04d5142013-05-27 23:41:05 +02002264 struct sony_sc *sc = hid_get_drvdata(hdev);
2265
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002266 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002267 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02002268
Frank Praznikd902f472014-01-27 10:17:36 -05002269 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2270 hid_hw_close(hdev);
2271 sony_battery_remove(sc);
2272 }
2273
Frank Praznik46262042014-04-14 10:11:31 -04002274 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002275
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002276 kfree(sc->output_report_dmabuf);
2277
Frank Praznikd2d782f2014-02-20 11:36:03 -05002278 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002279
Frank Praznik80250872014-04-14 10:11:35 -04002280 sony_release_device_id(sc);
2281
Jiri Slabybd28ce02008-06-25 23:47:04 +02002282 hid_hw_stop(hdev);
2283}
2284
2285static const struct hid_device_id sony_devices[] = {
2286 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2287 .driver_data = SIXAXIS_CONTROLLER_USB },
2288 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2289 .driver_data = SIXAXIS_CONTROLLER_USB },
Simon Wood6eabaaa2015-06-17 00:08:51 -06002290 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2291 .driver_data = SIXAXIS_CONTROLLER_BT },
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002292 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002293 .driver_data = MOTION_CONTROLLER_USB },
Simon Wooda4afa852015-06-03 09:45:19 -06002294 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002295 .driver_data = MOTION_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002296 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2297 .driver_data = SIXAXIS_CONTROLLER_BT },
2298 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2299 .driver_data = VAIO_RDESC_CONSTANT },
2300 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2301 .driver_data = VAIO_RDESC_CONSTANT },
2302 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2303 * Logitech joystick from the device descriptor. */
2304 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2305 .driver_data = BUZZ_CONTROLLER },
2306 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2307 .driver_data = BUZZ_CONTROLLER },
2308 /* PS3 BD Remote Control */
2309 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2310 .driver_data = PS3REMOTE },
2311 /* Logitech Harmony Adapter for PS3 */
2312 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2313 .driver_data = PS3REMOTE },
Frank Praznik68a49e52014-11-12 14:52:28 -05002314 /* SMK-Link PS3 BD Remote Control */
2315 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2316 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002317 /* Sony Dualshock 4 controllers for PS4 */
2318 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002319 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002320 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002321 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002322 { }
2323};
2324MODULE_DEVICE_TABLE(hid, sony_devices);
2325
2326static struct hid_driver sony_driver = {
Frank Praznikce8efc32014-09-18 21:15:01 -04002327 .name = "sony",
2328 .id_table = sony_devices,
2329 .input_mapping = sony_mapping,
2330 .input_configured = sony_input_configured,
2331 .probe = sony_probe,
2332 .remove = sony_remove,
2333 .report_fixup = sony_report_fixup,
2334 .raw_event = sony_raw_event
Jiri Slabybd28ce02008-06-25 23:47:04 +02002335};
Frank Praznik80250872014-04-14 10:11:35 -04002336
2337static int __init sony_init(void)
2338{
2339 dbg_hid("Sony:%s\n", __func__);
2340
2341 return hid_register_driver(&sony_driver);
2342}
2343
2344static void __exit sony_exit(void)
2345{
2346 dbg_hid("Sony:%s\n", __func__);
2347
Frank Praznik80250872014-04-14 10:11:35 -04002348 hid_unregister_driver(&sony_driver);
Antonio Ospite6c400652015-02-16 22:58:24 +01002349 ida_destroy(&sony_device_id_allocator);
Frank Praznik80250872014-04-14 10:11:35 -04002350}
2351module_init(sony_init);
2352module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002353
2354MODULE_LICENSE("GPL");