blob: ba63bb89ab1874172f19a67ecaaee308350b4910 [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 Praznikc4425c82016-09-22 20:18:10 -040011 * Copyright (c) 2014-2016 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>
Roderick Colenbrander6dfd5bf2016-10-07 12:39:36 -070039#include <linux/crc32.h>
40#include <asm/unaligned.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020041
42#include "hid-ids.h"
43
Frank Praznik6c79c182014-01-16 21:43:03 -050044#define VAIO_RDESC_CONSTANT BIT(0)
45#define SIXAXIS_CONTROLLER_USB BIT(1)
46#define SIXAXIS_CONTROLLER_BT BIT(2)
47#define BUZZ_CONTROLLER BIT(3)
48#define PS3REMOTE BIT(4)
Frank Praznik8ab16762014-01-16 21:42:31 -050049#define DUALSHOCK4_CONTROLLER_USB BIT(5)
50#define DUALSHOCK4_CONTROLLER_BT BIT(6)
Simon Woodb3bca322015-06-09 21:27:04 -060051#define MOTION_CONTROLLER_USB BIT(7)
52#define MOTION_CONTROLLER_BT BIT(8)
Simon Wood4545ee02015-06-17 00:08:52 -060053#define NAVIGATION_CONTROLLER_USB BIT(9)
54#define NAVIGATION_CONTROLLER_BT BIT(10)
Scott Moreau74500cc2016-01-13 07:40:42 -070055#define SINO_LITE_CONTROLLER BIT(11)
Mikko Perttunen4ba1eee2016-07-21 19:54:48 +030056#define FUTUREMAX_DANCE_MAT BIT(12)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020057
Frank Praznikfee4e2d2014-02-18 17:22:01 -050058#define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
Simon Woodb3bca322015-06-09 21:27:04 -060059#define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
Simon Wood4545ee02015-06-17 00:08:52 -060060#define NAVIGATION_CONTROLLER (NAVIGATION_CONTROLLER_USB |\
61 NAVIGATION_CONTROLLER_BT)
Frank Praznik68330d82014-02-05 20:03:49 -050062#define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
63 DUALSHOCK4_CONTROLLER_BT)
Frank Praznikfee4e2d2014-02-18 17:22:01 -050064#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
Simon Wood4545ee02015-06-17 00:08:52 -060065 DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER |\
66 NAVIGATION_CONTROLLER)
Simon Wood12e9a6d2015-06-09 21:27:05 -060067#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
Simon Wood4545ee02015-06-17 00:08:52 -060068 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040069#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
70 MOTION_CONTROLLER)
Frank Praznik0f398232016-09-22 20:18:08 -040071#define SONY_BT_DEVICE (SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_BT |\
72 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER_BT)
Frank Praznik60781cf2014-01-11 15:13:15 -050073
74#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010075
Frank Praznik4c3e8292015-05-05 20:47:33 -040076/*
77 * The Sixaxis reports both digital and analog values for each button on the
78 * controller except for Start, Select and the PS button. The controller ends
79 * up reporting 27 axes which causes them to spill over into the multi-touch
80 * axis values. Additionally, the controller only has 20 actual, physical axes
81 * so there are several unused axes in between the used ones.
82 */
Pavel Machek1adf9042016-02-09 13:55:08 +010083static u8 sixaxis_rdesc[] = {
Antonio Ospitefb705a62014-06-24 13:28:42 +020084 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznik4c3e8292015-05-05 20:47:33 -040085 0x09, 0x04, /* Usage (Joystick), */
Antonio Ospitefb705a62014-06-24 13:28:42 +020086 0xA1, 0x01, /* Collection (Application), */
87 0xA1, 0x02, /* Collection (Logical), */
88 0x85, 0x01, /* Report ID (1), */
89 0x75, 0x08, /* Report Size (8), */
90 0x95, 0x01, /* Report Count (1), */
91 0x15, 0x00, /* Logical Minimum (0), */
92 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
93 0x81, 0x03, /* Input (Constant, Variable), */
94 0x75, 0x01, /* Report Size (1), */
95 0x95, 0x13, /* Report Count (19), */
96 0x15, 0x00, /* Logical Minimum (0), */
97 0x25, 0x01, /* Logical Maximum (1), */
98 0x35, 0x00, /* Physical Minimum (0), */
99 0x45, 0x01, /* Physical Maximum (1), */
100 0x05, 0x09, /* Usage Page (Button), */
101 0x19, 0x01, /* Usage Minimum (01h), */
102 0x29, 0x13, /* Usage Maximum (13h), */
103 0x81, 0x02, /* Input (Variable), */
104 0x75, 0x01, /* Report Size (1), */
105 0x95, 0x0D, /* Report Count (13), */
106 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
107 0x81, 0x03, /* Input (Constant, Variable), */
108 0x15, 0x00, /* Logical Minimum (0), */
109 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
110 0x05, 0x01, /* Usage Page (Desktop), */
111 0x09, 0x01, /* Usage (Pointer), */
112 0xA1, 0x00, /* Collection (Physical), */
113 0x75, 0x08, /* Report Size (8), */
114 0x95, 0x04, /* Report Count (4), */
115 0x35, 0x00, /* Physical Minimum (0), */
116 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
117 0x09, 0x30, /* Usage (X), */
118 0x09, 0x31, /* Usage (Y), */
119 0x09, 0x32, /* Usage (Z), */
120 0x09, 0x35, /* Usage (Rz), */
121 0x81, 0x02, /* Input (Variable), */
122 0xC0, /* End Collection, */
123 0x05, 0x01, /* Usage Page (Desktop), */
124 0x95, 0x13, /* Report Count (19), */
125 0x09, 0x01, /* Usage (Pointer), */
126 0x81, 0x02, /* Input (Variable), */
127 0x95, 0x0C, /* Report Count (12), */
128 0x81, 0x01, /* Input (Constant), */
129 0x75, 0x10, /* Report Size (16), */
130 0x95, 0x04, /* Report Count (4), */
131 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
132 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
133 0x09, 0x01, /* Usage (Pointer), */
134 0x81, 0x02, /* Input (Variable), */
135 0xC0, /* End Collection, */
136 0xA1, 0x02, /* Collection (Logical), */
137 0x85, 0x02, /* Report ID (2), */
138 0x75, 0x08, /* Report Size (8), */
139 0x95, 0x30, /* Report Count (48), */
140 0x09, 0x01, /* Usage (Pointer), */
141 0xB1, 0x02, /* Feature (Variable), */
142 0xC0, /* End Collection, */
143 0xA1, 0x02, /* Collection (Logical), */
144 0x85, 0xEE, /* Report ID (238), */
145 0x75, 0x08, /* Report Size (8), */
146 0x95, 0x30, /* Report Count (48), */
147 0x09, 0x01, /* Usage (Pointer), */
148 0xB1, 0x02, /* Feature (Variable), */
149 0xC0, /* End Collection, */
150 0xA1, 0x02, /* Collection (Logical), */
151 0x85, 0xEF, /* Report ID (239), */
152 0x75, 0x08, /* Report Size (8), */
153 0x95, 0x30, /* Report Count (48), */
154 0x09, 0x01, /* Usage (Pointer), */
155 0xB1, 0x02, /* Feature (Variable), */
156 0xC0, /* End Collection, */
157 0xC0 /* End Collection */
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200158};
159
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400160/* PS/3 Motion controller */
Pavel Machek1adf9042016-02-09 13:55:08 +0100161static u8 motion_rdesc[] = {
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400162 0x05, 0x01, /* Usage Page (Desktop), */
163 0x09, 0x04, /* Usage (Joystick), */
164 0xA1, 0x01, /* Collection (Application), */
165 0xA1, 0x02, /* Collection (Logical), */
166 0x85, 0x01, /* Report ID (1), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400167 0x75, 0x01, /* Report Size (1), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600168 0x95, 0x15, /* Report Count (21), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400169 0x15, 0x00, /* Logical Minimum (0), */
170 0x25, 0x01, /* Logical Maximum (1), */
171 0x35, 0x00, /* Physical Minimum (0), */
172 0x45, 0x01, /* Physical Maximum (1), */
173 0x05, 0x09, /* Usage Page (Button), */
174 0x19, 0x01, /* Usage Minimum (01h), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600175 0x29, 0x15, /* Usage Maximum (15h), */
176 0x81, 0x02, /* Input (Variable), * Buttons */
177 0x95, 0x0B, /* Report Count (11), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400178 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600179 0x81, 0x03, /* Input (Constant, Variable), * Padding */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400180 0x15, 0x00, /* Logical Minimum (0), */
181 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
182 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400183 0xA1, 0x00, /* Collection (Physical), */
184 0x75, 0x08, /* Report Size (8), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600185 0x95, 0x01, /* Report Count (1), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400186 0x35, 0x00, /* Physical Minimum (0), */
187 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
188 0x09, 0x30, /* Usage (X), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600189 0x81, 0x02, /* Input (Variable), * Trigger */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400190 0xC0, /* End Collection, */
Simon Wood8b2513c2015-06-09 21:27:07 -0600191 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
192 0x75, 0x08, /* Report Size (8), */
193 0x95, 0x07, /* Report Count (7), * skip 7 bytes */
194 0x81, 0x02, /* Input (Variable), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400195 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400196 0x75, 0x10, /* Report Size (16), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600197 0x46, 0xFF, 0xFF, /* Physical Maximum (65535), */
198 0x27, 0xFF, 0xFF, 0x00, 0x00, /* Logical Maximum (65535), */
199 0x95, 0x03, /* Report Count (3), * 3x Accels */
200 0x09, 0x33, /* Usage (rX), */
201 0x09, 0x34, /* Usage (rY), */
202 0x09, 0x35, /* Usage (rZ), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400203 0x81, 0x02, /* Input (Variable), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600204 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
205 0x95, 0x03, /* Report Count (3), * Skip Accels 2nd frame */
206 0x81, 0x02, /* Input (Variable), */
207 0x05, 0x01, /* Usage Page (Desktop), */
208 0x09, 0x01, /* Usage (Pointer), */
209 0x95, 0x03, /* Report Count (3), * 3x Gyros */
210 0x81, 0x02, /* Input (Variable), */
211 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
212 0x95, 0x03, /* Report Count (3), * Skip Gyros 2nd frame */
213 0x81, 0x02, /* Input (Variable), */
214 0x75, 0x0C, /* Report Size (12), */
215 0x46, 0xFF, 0x0F, /* Physical Maximum (4095), */
216 0x26, 0xFF, 0x0F, /* Logical Maximum (4095), */
217 0x95, 0x04, /* Report Count (4), * Skip Temp and Magnetometers */
218 0x81, 0x02, /* Input (Variable), */
219 0x75, 0x08, /* Report Size (8), */
220 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
221 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
222 0x95, 0x06, /* Report Count (6), * Skip Timestamp and Extension Bytes */
223 0x81, 0x02, /* Input (Variable), */
224 0x75, 0x08, /* Report Size (8), */
225 0x95, 0x30, /* Report Count (48), */
226 0x09, 0x01, /* Usage (Pointer), */
227 0x91, 0x02, /* Output (Variable), */
228 0x75, 0x08, /* Report Size (8), */
229 0x95, 0x30, /* Report Count (48), */
230 0x09, 0x01, /* Usage (Pointer), */
231 0xB1, 0x02, /* Feature (Variable), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400232 0xC0, /* End Collection, */
233 0xA1, 0x02, /* Collection (Logical), */
234 0x85, 0x02, /* Report ID (2), */
235 0x75, 0x08, /* Report Size (8), */
236 0x95, 0x30, /* Report Count (48), */
237 0x09, 0x01, /* Usage (Pointer), */
238 0xB1, 0x02, /* Feature (Variable), */
239 0xC0, /* End Collection, */
240 0xA1, 0x02, /* Collection (Logical), */
241 0x85, 0xEE, /* Report ID (238), */
242 0x75, 0x08, /* Report Size (8), */
243 0x95, 0x30, /* Report Count (48), */
244 0x09, 0x01, /* Usage (Pointer), */
245 0xB1, 0x02, /* Feature (Variable), */
246 0xC0, /* End Collection, */
247 0xA1, 0x02, /* Collection (Logical), */
248 0x85, 0xEF, /* Report ID (239), */
249 0x75, 0x08, /* Report Size (8), */
250 0x95, 0x30, /* Report Count (48), */
251 0x09, 0x01, /* Usage (Pointer), */
252 0xB1, 0x02, /* Feature (Variable), */
253 0xC0, /* End Collection, */
254 0xC0 /* End Collection */
255};
256
Simon Woodb2723eb2015-06-17 00:08:53 -0600257/* PS/3 Navigation controller */
Pavel Machek1adf9042016-02-09 13:55:08 +0100258static u8 navigation_rdesc[] = {
Simon Woodb2723eb2015-06-17 00:08:53 -0600259 0x05, 0x01, /* Usage Page (Desktop), */
Antonio Ospited5421762016-01-28 18:23:43 +0100260 0x09, 0x04, /* Usage (Joystick), */
Simon Woodb2723eb2015-06-17 00:08:53 -0600261 0xA1, 0x01, /* Collection (Application), */
262 0xA1, 0x02, /* Collection (Logical), */
263 0x85, 0x01, /* Report ID (1), */
264 0x75, 0x08, /* Report Size (8), */
265 0x95, 0x01, /* Report Count (1), */
266 0x15, 0x00, /* Logical Minimum (0), */
267 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
268 0x81, 0x03, /* Input (Constant, Variable), */
269 0x75, 0x01, /* Report Size (1), */
270 0x95, 0x13, /* Report Count (19), */
271 0x15, 0x00, /* Logical Minimum (0), */
272 0x25, 0x01, /* Logical Maximum (1), */
273 0x35, 0x00, /* Physical Minimum (0), */
274 0x45, 0x01, /* Physical Maximum (1), */
275 0x05, 0x09, /* Usage Page (Button), */
276 0x19, 0x01, /* Usage Minimum (01h), */
277 0x29, 0x13, /* Usage Maximum (13h), */
278 0x81, 0x02, /* Input (Variable), */
279 0x75, 0x01, /* Report Size (1), */
280 0x95, 0x0D, /* Report Count (13), */
281 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
282 0x81, 0x03, /* Input (Constant, Variable), */
283 0x15, 0x00, /* Logical Minimum (0), */
284 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
285 0x05, 0x01, /* Usage Page (Desktop), */
286 0x09, 0x01, /* Usage (Pointer), */
287 0xA1, 0x00, /* Collection (Physical), */
288 0x75, 0x08, /* Report Size (8), */
289 0x95, 0x02, /* Report Count (2), */
290 0x35, 0x00, /* Physical Minimum (0), */
291 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
292 0x09, 0x30, /* Usage (X), */
293 0x09, 0x31, /* Usage (Y), */
294 0x81, 0x02, /* Input (Variable), */
295 0xC0, /* End Collection, */
296 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
297 0x95, 0x06, /* Report Count (6), */
298 0x81, 0x03, /* Input (Constant, Variable), */
299 0x05, 0x01, /* Usage Page (Desktop), */
300 0x75, 0x08, /* Report Size (8), */
301 0x95, 0x05, /* Report Count (5), */
302 0x09, 0x01, /* Usage (Pointer), */
303 0x81, 0x02, /* Input (Variable), */
304 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
Simon Wood2259b5bb2015-07-10 00:10:21 -0600305 0x95, 0x01, /* Report Count (1), */
306 0x81, 0x02, /* Input (Variable), */
307 0x05, 0x01, /* Usage Page (Desktop), */
308 0x95, 0x01, /* Report Count (1), */
309 0x09, 0x01, /* Usage (Pointer), */
310 0x81, 0x02, /* Input (Variable), */
311 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
312 0x95, 0x1E, /* Report Count (24), */
Simon Woodb2723eb2015-06-17 00:08:53 -0600313 0x81, 0x02, /* Input (Variable), */
314 0x75, 0x08, /* Report Size (8), */
315 0x95, 0x30, /* Report Count (48), */
316 0x09, 0x01, /* Usage (Pointer), */
317 0x91, 0x02, /* Output (Variable), */
318 0x75, 0x08, /* Report Size (8), */
319 0x95, 0x30, /* Report Count (48), */
320 0x09, 0x01, /* Usage (Pointer), */
321 0xB1, 0x02, /* Feature (Variable), */
322 0xC0, /* End Collection, */
323 0xA1, 0x02, /* Collection (Logical), */
324 0x85, 0x02, /* Report ID (2), */
325 0x75, 0x08, /* Report Size (8), */
326 0x95, 0x30, /* Report Count (48), */
327 0x09, 0x01, /* Usage (Pointer), */
328 0xB1, 0x02, /* Feature (Variable), */
329 0xC0, /* End Collection, */
330 0xA1, 0x02, /* Collection (Logical), */
331 0x85, 0xEE, /* Report ID (238), */
332 0x75, 0x08, /* Report Size (8), */
333 0x95, 0x30, /* Report Count (48), */
334 0x09, 0x01, /* Usage (Pointer), */
335 0xB1, 0x02, /* Feature (Variable), */
336 0xC0, /* End Collection, */
337 0xA1, 0x02, /* Collection (Logical), */
338 0x85, 0xEF, /* Report ID (239), */
339 0x75, 0x08, /* Report Size (8), */
340 0x95, 0x30, /* Report Count (48), */
341 0x09, 0x01, /* Usage (Pointer), */
342 0xB1, 0x02, /* Feature (Variable), */
343 0xC0, /* End Collection, */
344 0xC0 /* End Collection */
345};
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400346
Frank Praznikad142b92014-02-20 11:36:00 -0500347/*
348 * The default descriptor doesn't provide mapping for the accelerometers
Frank Praznik58d70272014-01-20 12:27:01 -0500349 * or orientation sensors. This fixed descriptor maps the accelerometers
350 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
351 * to usage values 0x43, 0x44 and 0x45.
352 */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500353static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -0500354 0x05, 0x01, /* Usage Page (Desktop), */
355 0x09, 0x05, /* Usage (Gamepad), */
356 0xA1, 0x01, /* Collection (Application), */
357 0x85, 0x01, /* Report ID (1), */
358 0x09, 0x30, /* Usage (X), */
359 0x09, 0x31, /* Usage (Y), */
360 0x09, 0x32, /* Usage (Z), */
361 0x09, 0x35, /* Usage (Rz), */
362 0x15, 0x00, /* Logical Minimum (0), */
363 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
364 0x75, 0x08, /* Report Size (8), */
365 0x95, 0x04, /* Report Count (4), */
366 0x81, 0x02, /* Input (Variable), */
367 0x09, 0x39, /* Usage (Hat Switch), */
368 0x15, 0x00, /* Logical Minimum (0), */
369 0x25, 0x07, /* Logical Maximum (7), */
370 0x35, 0x00, /* Physical Minimum (0), */
371 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
372 0x65, 0x14, /* Unit (Degrees), */
373 0x75, 0x04, /* Report Size (4), */
374 0x95, 0x01, /* Report Count (1), */
375 0x81, 0x42, /* Input (Variable, Null State), */
376 0x65, 0x00, /* Unit, */
377 0x05, 0x09, /* Usage Page (Button), */
378 0x19, 0x01, /* Usage Minimum (01h), */
379 0x29, 0x0E, /* Usage Maximum (0Eh), */
380 0x15, 0x00, /* Logical Minimum (0), */
381 0x25, 0x01, /* Logical Maximum (1), */
382 0x75, 0x01, /* Report Size (1), */
383 0x95, 0x0E, /* Report Count (14), */
384 0x81, 0x02, /* Input (Variable), */
385 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
386 0x09, 0x20, /* Usage (20h), */
387 0x75, 0x06, /* Report Size (6), */
388 0x95, 0x01, /* Report Count (1), */
389 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400390 0x25, 0x3F, /* Logical Maximum (63), */
Frank Praznik58d70272014-01-20 12:27:01 -0500391 0x81, 0x02, /* Input (Variable), */
392 0x05, 0x01, /* Usage Page (Desktop), */
393 0x09, 0x33, /* Usage (Rx), */
394 0x09, 0x34, /* Usage (Ry), */
395 0x15, 0x00, /* Logical Minimum (0), */
396 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
397 0x75, 0x08, /* Report Size (8), */
398 0x95, 0x02, /* Report Count (2), */
399 0x81, 0x02, /* Input (Variable), */
400 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
401 0x09, 0x21, /* Usage (21h), */
402 0x95, 0x03, /* Report Count (3), */
403 0x81, 0x02, /* Input (Variable), */
404 0x05, 0x01, /* Usage Page (Desktop), */
405 0x19, 0x40, /* Usage Minimum (40h), */
406 0x29, 0x42, /* Usage Maximum (42h), */
407 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
408 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
409 0x75, 0x10, /* Report Size (16), */
410 0x95, 0x03, /* Report Count (3), */
411 0x81, 0x02, /* Input (Variable), */
412 0x19, 0x43, /* Usage Minimum (43h), */
413 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400414 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
415 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznik58d70272014-01-20 12:27:01 -0500416 0x95, 0x03, /* Report Count (3), */
417 0x81, 0x02, /* Input (Variable), */
418 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
419 0x09, 0x21, /* Usage (21h), */
420 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400421 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
Frank Praznik58d70272014-01-20 12:27:01 -0500422 0x75, 0x08, /* Report Size (8), */
423 0x95, 0x27, /* Report Count (39), */
424 0x81, 0x02, /* Input (Variable), */
425 0x85, 0x05, /* Report ID (5), */
426 0x09, 0x22, /* Usage (22h), */
427 0x95, 0x1F, /* Report Count (31), */
428 0x91, 0x02, /* Output (Variable), */
429 0x85, 0x04, /* Report ID (4), */
430 0x09, 0x23, /* Usage (23h), */
431 0x95, 0x24, /* Report Count (36), */
432 0xB1, 0x02, /* Feature (Variable), */
433 0x85, 0x02, /* Report ID (2), */
434 0x09, 0x24, /* Usage (24h), */
435 0x95, 0x24, /* Report Count (36), */
436 0xB1, 0x02, /* Feature (Variable), */
437 0x85, 0x08, /* Report ID (8), */
438 0x09, 0x25, /* Usage (25h), */
439 0x95, 0x03, /* Report Count (3), */
440 0xB1, 0x02, /* Feature (Variable), */
441 0x85, 0x10, /* Report ID (16), */
442 0x09, 0x26, /* Usage (26h), */
443 0x95, 0x04, /* Report Count (4), */
444 0xB1, 0x02, /* Feature (Variable), */
445 0x85, 0x11, /* Report ID (17), */
446 0x09, 0x27, /* Usage (27h), */
447 0x95, 0x02, /* Report Count (2), */
448 0xB1, 0x02, /* Feature (Variable), */
449 0x85, 0x12, /* Report ID (18), */
450 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
451 0x09, 0x21, /* Usage (21h), */
452 0x95, 0x0F, /* Report Count (15), */
453 0xB1, 0x02, /* Feature (Variable), */
454 0x85, 0x13, /* Report ID (19), */
455 0x09, 0x22, /* Usage (22h), */
456 0x95, 0x16, /* Report Count (22), */
457 0xB1, 0x02, /* Feature (Variable), */
458 0x85, 0x14, /* Report ID (20), */
459 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
460 0x09, 0x20, /* Usage (20h), */
461 0x95, 0x10, /* Report Count (16), */
462 0xB1, 0x02, /* Feature (Variable), */
463 0x85, 0x15, /* Report ID (21), */
464 0x09, 0x21, /* Usage (21h), */
465 0x95, 0x2C, /* Report Count (44), */
466 0xB1, 0x02, /* Feature (Variable), */
467 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
468 0x85, 0x80, /* Report ID (128), */
469 0x09, 0x20, /* Usage (20h), */
470 0x95, 0x06, /* Report Count (6), */
471 0xB1, 0x02, /* Feature (Variable), */
472 0x85, 0x81, /* Report ID (129), */
473 0x09, 0x21, /* Usage (21h), */
474 0x95, 0x06, /* Report Count (6), */
475 0xB1, 0x02, /* Feature (Variable), */
476 0x85, 0x82, /* Report ID (130), */
477 0x09, 0x22, /* Usage (22h), */
478 0x95, 0x05, /* Report Count (5), */
479 0xB1, 0x02, /* Feature (Variable), */
480 0x85, 0x83, /* Report ID (131), */
481 0x09, 0x23, /* Usage (23h), */
482 0x95, 0x01, /* Report Count (1), */
483 0xB1, 0x02, /* Feature (Variable), */
484 0x85, 0x84, /* Report ID (132), */
485 0x09, 0x24, /* Usage (24h), */
486 0x95, 0x04, /* Report Count (4), */
487 0xB1, 0x02, /* Feature (Variable), */
488 0x85, 0x85, /* Report ID (133), */
489 0x09, 0x25, /* Usage (25h), */
490 0x95, 0x06, /* Report Count (6), */
491 0xB1, 0x02, /* Feature (Variable), */
492 0x85, 0x86, /* Report ID (134), */
493 0x09, 0x26, /* Usage (26h), */
494 0x95, 0x06, /* Report Count (6), */
495 0xB1, 0x02, /* Feature (Variable), */
496 0x85, 0x87, /* Report ID (135), */
497 0x09, 0x27, /* Usage (27h), */
498 0x95, 0x23, /* Report Count (35), */
499 0xB1, 0x02, /* Feature (Variable), */
500 0x85, 0x88, /* Report ID (136), */
501 0x09, 0x28, /* Usage (28h), */
502 0x95, 0x22, /* Report Count (34), */
503 0xB1, 0x02, /* Feature (Variable), */
504 0x85, 0x89, /* Report ID (137), */
505 0x09, 0x29, /* Usage (29h), */
506 0x95, 0x02, /* Report Count (2), */
507 0xB1, 0x02, /* Feature (Variable), */
508 0x85, 0x90, /* Report ID (144), */
509 0x09, 0x30, /* Usage (30h), */
510 0x95, 0x05, /* Report Count (5), */
511 0xB1, 0x02, /* Feature (Variable), */
512 0x85, 0x91, /* Report ID (145), */
513 0x09, 0x31, /* Usage (31h), */
514 0x95, 0x03, /* Report Count (3), */
515 0xB1, 0x02, /* Feature (Variable), */
516 0x85, 0x92, /* Report ID (146), */
517 0x09, 0x32, /* Usage (32h), */
518 0x95, 0x03, /* Report Count (3), */
519 0xB1, 0x02, /* Feature (Variable), */
520 0x85, 0x93, /* Report ID (147), */
521 0x09, 0x33, /* Usage (33h), */
522 0x95, 0x0C, /* Report Count (12), */
523 0xB1, 0x02, /* Feature (Variable), */
524 0x85, 0xA0, /* Report ID (160), */
525 0x09, 0x40, /* Usage (40h), */
526 0x95, 0x06, /* Report Count (6), */
527 0xB1, 0x02, /* Feature (Variable), */
528 0x85, 0xA1, /* Report ID (161), */
529 0x09, 0x41, /* Usage (41h), */
530 0x95, 0x01, /* Report Count (1), */
531 0xB1, 0x02, /* Feature (Variable), */
532 0x85, 0xA2, /* Report ID (162), */
533 0x09, 0x42, /* Usage (42h), */
534 0x95, 0x01, /* Report Count (1), */
535 0xB1, 0x02, /* Feature (Variable), */
536 0x85, 0xA3, /* Report ID (163), */
537 0x09, 0x43, /* Usage (43h), */
538 0x95, 0x30, /* Report Count (48), */
539 0xB1, 0x02, /* Feature (Variable), */
540 0x85, 0xA4, /* Report ID (164), */
541 0x09, 0x44, /* Usage (44h), */
542 0x95, 0x0D, /* Report Count (13), */
543 0xB1, 0x02, /* Feature (Variable), */
544 0x85, 0xA5, /* Report ID (165), */
545 0x09, 0x45, /* Usage (45h), */
546 0x95, 0x15, /* Report Count (21), */
547 0xB1, 0x02, /* Feature (Variable), */
548 0x85, 0xA6, /* Report ID (166), */
549 0x09, 0x46, /* Usage (46h), */
550 0x95, 0x15, /* Report Count (21), */
551 0xB1, 0x02, /* Feature (Variable), */
552 0x85, 0xF0, /* Report ID (240), */
553 0x09, 0x47, /* Usage (47h), */
554 0x95, 0x3F, /* Report Count (63), */
555 0xB1, 0x02, /* Feature (Variable), */
556 0x85, 0xF1, /* Report ID (241), */
557 0x09, 0x48, /* Usage (48h), */
558 0x95, 0x3F, /* Report Count (63), */
559 0xB1, 0x02, /* Feature (Variable), */
560 0x85, 0xF2, /* Report ID (242), */
561 0x09, 0x49, /* Usage (49h), */
562 0x95, 0x0F, /* Report Count (15), */
563 0xB1, 0x02, /* Feature (Variable), */
564 0x85, 0xA7, /* Report ID (167), */
565 0x09, 0x4A, /* Usage (4Ah), */
566 0x95, 0x01, /* Report Count (1), */
567 0xB1, 0x02, /* Feature (Variable), */
568 0x85, 0xA8, /* Report ID (168), */
569 0x09, 0x4B, /* Usage (4Bh), */
570 0x95, 0x01, /* Report Count (1), */
571 0xB1, 0x02, /* Feature (Variable), */
572 0x85, 0xA9, /* Report ID (169), */
573 0x09, 0x4C, /* Usage (4Ch), */
574 0x95, 0x08, /* Report Count (8), */
575 0xB1, 0x02, /* Feature (Variable), */
576 0x85, 0xAA, /* Report ID (170), */
577 0x09, 0x4E, /* Usage (4Eh), */
578 0x95, 0x01, /* Report Count (1), */
579 0xB1, 0x02, /* Feature (Variable), */
580 0x85, 0xAB, /* Report ID (171), */
581 0x09, 0x4F, /* Usage (4Fh), */
582 0x95, 0x39, /* Report Count (57), */
583 0xB1, 0x02, /* Feature (Variable), */
584 0x85, 0xAC, /* Report ID (172), */
585 0x09, 0x50, /* Usage (50h), */
586 0x95, 0x39, /* Report Count (57), */
587 0xB1, 0x02, /* Feature (Variable), */
588 0x85, 0xAD, /* Report ID (173), */
589 0x09, 0x51, /* Usage (51h), */
590 0x95, 0x0B, /* Report Count (11), */
591 0xB1, 0x02, /* Feature (Variable), */
592 0x85, 0xAE, /* Report ID (174), */
593 0x09, 0x52, /* Usage (52h), */
594 0x95, 0x01, /* Report Count (1), */
595 0xB1, 0x02, /* Feature (Variable), */
596 0x85, 0xAF, /* Report ID (175), */
597 0x09, 0x53, /* Usage (53h), */
598 0x95, 0x02, /* Report Count (2), */
599 0xB1, 0x02, /* Feature (Variable), */
600 0x85, 0xB0, /* Report ID (176), */
601 0x09, 0x54, /* Usage (54h), */
602 0x95, 0x3F, /* Report Count (63), */
603 0xB1, 0x02, /* Feature (Variable), */
604 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500605};
606
Frank Praznikad142b92014-02-20 11:36:00 -0500607/*
608 * The default behavior of the Dualshock 4 is to send reports using report
Frank Praznik077147a2014-09-14 11:56:39 -0400609 * type 1 when running over Bluetooth. However, when feature report 2 is
610 * requested during the controller initialization it starts sending input
611 * reports in report 17. Since report 17 is undefined in the default HID
Frank Praznikd829674d2014-02-05 20:03:45 -0500612 * descriptor the button and axis definitions must be moved to report 17 or
Frank Praznik077147a2014-09-14 11:56:39 -0400613 * the HID layer won't process the received input.
Frank Praznikd829674d2014-02-05 20:03:45 -0500614 */
615static u8 dualshock4_bt_rdesc[] = {
616 0x05, 0x01, /* Usage Page (Desktop), */
617 0x09, 0x05, /* Usage (Gamepad), */
618 0xA1, 0x01, /* Collection (Application), */
619 0x85, 0x01, /* Report ID (1), */
620 0x75, 0x08, /* Report Size (8), */
621 0x95, 0x0A, /* Report Count (9), */
622 0x81, 0x02, /* Input (Variable), */
623 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
624 0x85, 0x02, /* Report ID (2), */
625 0x09, 0x24, /* Usage (24h), */
626 0x95, 0x24, /* Report Count (36), */
627 0xB1, 0x02, /* Feature (Variable), */
628 0x85, 0xA3, /* Report ID (163), */
629 0x09, 0x25, /* Usage (25h), */
630 0x95, 0x30, /* Report Count (48), */
631 0xB1, 0x02, /* Feature (Variable), */
632 0x85, 0x05, /* Report ID (5), */
633 0x09, 0x26, /* Usage (26h), */
634 0x95, 0x28, /* Report Count (40), */
635 0xB1, 0x02, /* Feature (Variable), */
636 0x85, 0x06, /* Report ID (6), */
637 0x09, 0x27, /* Usage (27h), */
638 0x95, 0x34, /* Report Count (52), */
639 0xB1, 0x02, /* Feature (Variable), */
640 0x85, 0x07, /* Report ID (7), */
641 0x09, 0x28, /* Usage (28h), */
642 0x95, 0x30, /* Report Count (48), */
643 0xB1, 0x02, /* Feature (Variable), */
644 0x85, 0x08, /* Report ID (8), */
645 0x09, 0x29, /* Usage (29h), */
646 0x95, 0x2F, /* Report Count (47), */
647 0xB1, 0x02, /* Feature (Variable), */
648 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
649 0x85, 0x03, /* Report ID (3), */
650 0x09, 0x21, /* Usage (21h), */
651 0x95, 0x26, /* Report Count (38), */
652 0xB1, 0x02, /* Feature (Variable), */
653 0x85, 0x04, /* Report ID (4), */
654 0x09, 0x22, /* Usage (22h), */
655 0x95, 0x2E, /* Report Count (46), */
656 0xB1, 0x02, /* Feature (Variable), */
657 0x85, 0xF0, /* Report ID (240), */
658 0x09, 0x47, /* Usage (47h), */
659 0x95, 0x3F, /* Report Count (63), */
660 0xB1, 0x02, /* Feature (Variable), */
661 0x85, 0xF1, /* Report ID (241), */
662 0x09, 0x48, /* Usage (48h), */
663 0x95, 0x3F, /* Report Count (63), */
664 0xB1, 0x02, /* Feature (Variable), */
665 0x85, 0xF2, /* Report ID (242), */
666 0x09, 0x49, /* Usage (49h), */
667 0x95, 0x0F, /* Report Count (15), */
668 0xB1, 0x02, /* Feature (Variable), */
669 0x85, 0x11, /* Report ID (17), */
670 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
671 0x09, 0x20, /* Usage (20h), */
672 0x95, 0x02, /* Report Count (2), */
673 0x81, 0x02, /* Input (Variable), */
674 0x05, 0x01, /* Usage Page (Desktop), */
675 0x09, 0x30, /* Usage (X), */
676 0x09, 0x31, /* Usage (Y), */
677 0x09, 0x32, /* Usage (Z), */
678 0x09, 0x35, /* Usage (Rz), */
679 0x15, 0x00, /* Logical Minimum (0), */
680 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
681 0x75, 0x08, /* Report Size (8), */
682 0x95, 0x04, /* Report Count (4), */
683 0x81, 0x02, /* Input (Variable), */
684 0x09, 0x39, /* Usage (Hat Switch), */
685 0x15, 0x00, /* Logical Minimum (0), */
686 0x25, 0x07, /* Logical Maximum (7), */
687 0x75, 0x04, /* Report Size (4), */
688 0x95, 0x01, /* Report Count (1), */
689 0x81, 0x42, /* Input (Variable, Null State), */
690 0x05, 0x09, /* Usage Page (Button), */
691 0x19, 0x01, /* Usage Minimum (01h), */
692 0x29, 0x0E, /* Usage Maximum (0Eh), */
693 0x15, 0x00, /* Logical Minimum (0), */
694 0x25, 0x01, /* Logical Maximum (1), */
695 0x75, 0x01, /* Report Size (1), */
696 0x95, 0x0E, /* Report Count (14), */
697 0x81, 0x02, /* Input (Variable), */
698 0x75, 0x06, /* Report Size (6), */
699 0x95, 0x01, /* Report Count (1), */
700 0x81, 0x01, /* Input (Constant), */
701 0x05, 0x01, /* Usage Page (Desktop), */
702 0x09, 0x33, /* Usage (Rx), */
703 0x09, 0x34, /* Usage (Ry), */
704 0x15, 0x00, /* Logical Minimum (0), */
705 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
706 0x75, 0x08, /* Report Size (8), */
707 0x95, 0x02, /* Report Count (2), */
708 0x81, 0x02, /* Input (Variable), */
709 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
710 0x09, 0x20, /* Usage (20h), */
711 0x95, 0x03, /* Report Count (3), */
712 0x81, 0x02, /* Input (Variable), */
713 0x05, 0x01, /* Usage Page (Desktop), */
714 0x19, 0x40, /* Usage Minimum (40h), */
715 0x29, 0x42, /* Usage Maximum (42h), */
716 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
717 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
718 0x75, 0x10, /* Report Size (16), */
719 0x95, 0x03, /* Report Count (3), */
720 0x81, 0x02, /* Input (Variable), */
721 0x19, 0x43, /* Usage Minimum (43h), */
722 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400723 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
724 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznikd829674d2014-02-05 20:03:45 -0500725 0x95, 0x03, /* Report Count (3), */
726 0x81, 0x02, /* Input (Variable), */
727 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
728 0x09, 0x20, /* Usage (20h), */
729 0x15, 0x00, /* Logical Minimum (0), */
730 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
731 0x75, 0x08, /* Report Size (8), */
732 0x95, 0x31, /* Report Count (51), */
733 0x81, 0x02, /* Input (Variable), */
734 0x09, 0x21, /* Usage (21h), */
735 0x75, 0x08, /* Report Size (8), */
736 0x95, 0x4D, /* Report Count (77), */
737 0x91, 0x02, /* Output (Variable), */
738 0x85, 0x12, /* Report ID (18), */
739 0x09, 0x22, /* Usage (22h), */
740 0x95, 0x8D, /* Report Count (141), */
741 0x81, 0x02, /* Input (Variable), */
742 0x09, 0x23, /* Usage (23h), */
743 0x91, 0x02, /* Output (Variable), */
744 0x85, 0x13, /* Report ID (19), */
745 0x09, 0x24, /* Usage (24h), */
746 0x95, 0xCD, /* Report Count (205), */
747 0x81, 0x02, /* Input (Variable), */
748 0x09, 0x25, /* Usage (25h), */
749 0x91, 0x02, /* Output (Variable), */
750 0x85, 0x14, /* Report ID (20), */
751 0x09, 0x26, /* Usage (26h), */
752 0x96, 0x0D, 0x01, /* Report Count (269), */
753 0x81, 0x02, /* Input (Variable), */
754 0x09, 0x27, /* Usage (27h), */
755 0x91, 0x02, /* Output (Variable), */
756 0x85, 0x15, /* Report ID (21), */
757 0x09, 0x28, /* Usage (28h), */
758 0x96, 0x4D, 0x01, /* Report Count (333), */
759 0x81, 0x02, /* Input (Variable), */
760 0x09, 0x29, /* Usage (29h), */
761 0x91, 0x02, /* Output (Variable), */
762 0x85, 0x16, /* Report ID (22), */
763 0x09, 0x2A, /* Usage (2Ah), */
764 0x96, 0x8D, 0x01, /* Report Count (397), */
765 0x81, 0x02, /* Input (Variable), */
766 0x09, 0x2B, /* Usage (2Bh), */
767 0x91, 0x02, /* Output (Variable), */
768 0x85, 0x17, /* Report ID (23), */
769 0x09, 0x2C, /* Usage (2Ch), */
770 0x96, 0xCD, 0x01, /* Report Count (461), */
771 0x81, 0x02, /* Input (Variable), */
772 0x09, 0x2D, /* Usage (2Dh), */
773 0x91, 0x02, /* Output (Variable), */
774 0x85, 0x18, /* Report ID (24), */
775 0x09, 0x2E, /* Usage (2Eh), */
776 0x96, 0x0D, 0x02, /* Report Count (525), */
777 0x81, 0x02, /* Input (Variable), */
778 0x09, 0x2F, /* Usage (2Fh), */
779 0x91, 0x02, /* Output (Variable), */
780 0x85, 0x19, /* Report ID (25), */
781 0x09, 0x30, /* Usage (30h), */
782 0x96, 0x22, 0x02, /* Report Count (546), */
783 0x81, 0x02, /* Input (Variable), */
784 0x09, 0x31, /* Usage (31h), */
785 0x91, 0x02, /* Output (Variable), */
786 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
787 0x85, 0x82, /* Report ID (130), */
788 0x09, 0x22, /* Usage (22h), */
789 0x95, 0x3F, /* Report Count (63), */
790 0xB1, 0x02, /* Feature (Variable), */
791 0x85, 0x83, /* Report ID (131), */
792 0x09, 0x23, /* Usage (23h), */
793 0xB1, 0x02, /* Feature (Variable), */
794 0x85, 0x84, /* Report ID (132), */
795 0x09, 0x24, /* Usage (24h), */
796 0xB1, 0x02, /* Feature (Variable), */
797 0x85, 0x90, /* Report ID (144), */
798 0x09, 0x30, /* Usage (30h), */
799 0xB1, 0x02, /* Feature (Variable), */
800 0x85, 0x91, /* Report ID (145), */
801 0x09, 0x31, /* Usage (31h), */
802 0xB1, 0x02, /* Feature (Variable), */
803 0x85, 0x92, /* Report ID (146), */
804 0x09, 0x32, /* Usage (32h), */
805 0xB1, 0x02, /* Feature (Variable), */
806 0x85, 0x93, /* Report ID (147), */
807 0x09, 0x33, /* Usage (33h), */
808 0xB1, 0x02, /* Feature (Variable), */
809 0x85, 0xA0, /* Report ID (160), */
810 0x09, 0x40, /* Usage (40h), */
811 0xB1, 0x02, /* Feature (Variable), */
812 0x85, 0xA4, /* Report ID (164), */
813 0x09, 0x44, /* Usage (44h), */
814 0xB1, 0x02, /* Feature (Variable), */
815 0xC0 /* End Collection */
816};
817
Pavel Machek1adf9042016-02-09 13:55:08 +0100818static u8 ps3remote_rdesc[] = {
Jiri Kosina078328d2013-06-13 12:03:49 +0200819 0x05, 0x01, /* GUsagePage Generic Desktop */
820 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
821 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
822
823 /* Use collection 1 for joypad buttons */
824 0xA1, 0x02, /* MCollection Logical (interrelated data) */
825
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100826 /*
827 * Ignore the 1st byte, maybe it is used for a controller
828 * number but it's not needed for correct operation
829 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200830 0x75, 0x08, /* GReportSize 0x08 [8] */
831 0x95, 0x01, /* GReportCount 0x01 [1] */
832 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
833
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100834 /*
835 * Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
836 * buttons multiple keypresses are allowed
837 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200838 0x05, 0x09, /* GUsagePage Button */
839 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
840 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
841 0x14, /* GLogicalMinimum [0] */
842 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
843 0x75, 0x01, /* GReportSize 0x01 [1] */
844 0x95, 0x18, /* GReportCount 0x18 [24] */
845 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
846
847 0xC0, /* MEndCollection */
848
849 /* Use collection 2 for remote control buttons */
850 0xA1, 0x02, /* MCollection Logical (interrelated data) */
851
852 /* 5th byte is used for remote control buttons */
853 0x05, 0x09, /* GUsagePage Button */
854 0x18, /* LUsageMinimum [No button pressed] */
855 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
856 0x14, /* GLogicalMinimum [0] */
857 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
858 0x75, 0x08, /* GReportSize 0x08 [8] */
859 0x95, 0x01, /* GReportCount 0x01 [1] */
860 0x80, /* MInput */
861
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100862 /*
863 * Ignore bytes from 6th to 11th, 6th to 10th are always constant at
864 * 0xff and 11th is for press indication
865 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200866 0x75, 0x08, /* GReportSize 0x08 [8] */
867 0x95, 0x06, /* GReportCount 0x06 [6] */
868 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
869
870 /* 12th byte is for battery strength */
871 0x05, 0x06, /* GUsagePage Generic Device Controls */
872 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
873 0x14, /* GLogicalMinimum [0] */
874 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
875 0x75, 0x08, /* GReportSize 0x08 [8] */
876 0x95, 0x01, /* GReportCount 0x01 [1] */
877 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
878
879 0xC0, /* MEndCollection */
880
881 0xC0 /* MEndCollection [Game Pad] */
882};
883
884static const unsigned int ps3remote_keymap_joypad_buttons[] = {
885 [0x01] = KEY_SELECT,
886 [0x02] = BTN_THUMBL, /* L3 */
887 [0x03] = BTN_THUMBR, /* R3 */
888 [0x04] = BTN_START,
889 [0x05] = KEY_UP,
890 [0x06] = KEY_RIGHT,
891 [0x07] = KEY_DOWN,
892 [0x08] = KEY_LEFT,
893 [0x09] = BTN_TL2, /* L2 */
894 [0x0a] = BTN_TR2, /* R2 */
895 [0x0b] = BTN_TL, /* L1 */
896 [0x0c] = BTN_TR, /* R1 */
897 [0x0d] = KEY_OPTION, /* options/triangle */
898 [0x0e] = KEY_BACK, /* back/circle */
899 [0x0f] = BTN_0, /* cross */
900 [0x10] = KEY_SCREEN, /* view/square */
901 [0x11] = KEY_HOMEPAGE, /* PS button */
902 [0x14] = KEY_ENTER,
903};
904static const unsigned int ps3remote_keymap_remote_buttons[] = {
905 [0x00] = KEY_1,
906 [0x01] = KEY_2,
907 [0x02] = KEY_3,
908 [0x03] = KEY_4,
909 [0x04] = KEY_5,
910 [0x05] = KEY_6,
911 [0x06] = KEY_7,
912 [0x07] = KEY_8,
913 [0x08] = KEY_9,
914 [0x09] = KEY_0,
915 [0x0e] = KEY_ESC, /* return */
916 [0x0f] = KEY_CLEAR,
917 [0x16] = KEY_EJECTCD,
918 [0x1a] = KEY_MENU, /* top menu */
919 [0x28] = KEY_TIME,
920 [0x30] = KEY_PREVIOUS,
921 [0x31] = KEY_NEXT,
922 [0x32] = KEY_PLAY,
923 [0x33] = KEY_REWIND, /* scan back */
924 [0x34] = KEY_FORWARD, /* scan forward */
925 [0x38] = KEY_STOP,
926 [0x39] = KEY_PAUSE,
927 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
928 [0x60] = KEY_FRAMEBACK, /* slow/step back */
929 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
930 [0x63] = KEY_SUBTITLE,
931 [0x64] = KEY_AUDIO,
932 [0x65] = KEY_ANGLE,
933 [0x70] = KEY_INFO, /* display */
934 [0x80] = KEY_BLUE,
935 [0x81] = KEY_RED,
936 [0x82] = KEY_GREEN,
937 [0x83] = KEY_YELLOW,
938};
939
Colin Leitnerf04d5142013-05-27 23:41:05 +0200940static const unsigned int buzz_keymap[] = {
Frank Praznikad142b92014-02-20 11:36:00 -0500941 /*
942 * The controller has 4 remote buzzers, each with one LED and 5
Colin Leitnerf04d5142013-05-27 23:41:05 +0200943 * buttons.
Antonio Ospite09593e32016-02-09 13:55:06 +0100944 *
Colin Leitnerf04d5142013-05-27 23:41:05 +0200945 * We use the mapping chosen by the controller, which is:
946 *
947 * Key Offset
948 * -------------------
949 * Buzz 1
950 * Blue 5
951 * Orange 4
952 * Green 3
953 * Yellow 2
954 *
955 * So, for example, the orange button on the third buzzer is mapped to
956 * BTN_TRIGGER_HAPPY14
957 */
Antonio Ospite09593e32016-02-09 13:55:06 +0100958 [1] = BTN_TRIGGER_HAPPY1,
959 [2] = BTN_TRIGGER_HAPPY2,
960 [3] = BTN_TRIGGER_HAPPY3,
961 [4] = BTN_TRIGGER_HAPPY4,
962 [5] = BTN_TRIGGER_HAPPY5,
963 [6] = BTN_TRIGGER_HAPPY6,
964 [7] = BTN_TRIGGER_HAPPY7,
965 [8] = BTN_TRIGGER_HAPPY8,
966 [9] = BTN_TRIGGER_HAPPY9,
Colin Leitnerf04d5142013-05-27 23:41:05 +0200967 [10] = BTN_TRIGGER_HAPPY10,
968 [11] = BTN_TRIGGER_HAPPY11,
969 [12] = BTN_TRIGGER_HAPPY12,
970 [13] = BTN_TRIGGER_HAPPY13,
971 [14] = BTN_TRIGGER_HAPPY14,
972 [15] = BTN_TRIGGER_HAPPY15,
973 [16] = BTN_TRIGGER_HAPPY16,
974 [17] = BTN_TRIGGER_HAPPY17,
975 [18] = BTN_TRIGGER_HAPPY18,
976 [19] = BTN_TRIGGER_HAPPY19,
977 [20] = BTN_TRIGGER_HAPPY20,
978};
979
Frank Praznikd902f472014-01-27 10:17:36 -0500980static enum power_supply_property sony_battery_props[] = {
981 POWER_SUPPLY_PROP_PRESENT,
982 POWER_SUPPLY_PROP_CAPACITY,
983 POWER_SUPPLY_PROP_SCOPE,
984 POWER_SUPPLY_PROP_STATUS,
985};
986
Frank Praznik55d3b662014-04-14 10:11:32 -0400987struct sixaxis_led {
Pavel Machek1adf9042016-02-09 13:55:08 +0100988 u8 time_enabled; /* the total time the led is active (0xff means forever) */
989 u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
990 u8 enabled;
991 u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
992 u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
Frank Praznik55d3b662014-04-14 10:11:32 -0400993} __packed;
994
995struct sixaxis_rumble {
Pavel Machek1adf9042016-02-09 13:55:08 +0100996 u8 padding;
997 u8 right_duration; /* Right motor duration (0xff means forever) */
998 u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
999 u8 left_duration; /* Left motor duration (0xff means forever) */
1000 u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
Frank Praznik55d3b662014-04-14 10:11:32 -04001001} __packed;
1002
1003struct sixaxis_output_report {
Pavel Machek1adf9042016-02-09 13:55:08 +01001004 u8 report_id;
Frank Praznik55d3b662014-04-14 10:11:32 -04001005 struct sixaxis_rumble rumble;
Pavel Machek1adf9042016-02-09 13:55:08 +01001006 u8 padding[4];
1007 u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
Frank Praznik55d3b662014-04-14 10:11:32 -04001008 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
1009 struct sixaxis_led _reserved; /* LED5, not actually soldered */
1010} __packed;
1011
1012union sixaxis_output_report_01 {
1013 struct sixaxis_output_report data;
Pavel Machek1adf9042016-02-09 13:55:08 +01001014 u8 buf[36];
Frank Praznik55d3b662014-04-14 10:11:32 -04001015};
1016
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001017struct motion_output_report_02 {
1018 u8 type, zero;
1019 u8 r, g, b;
1020 u8 zero2;
1021 u8 rumble;
1022};
1023
Roderick Colenbrander5443a632016-10-07 12:39:35 -07001024#define DS4_FEATURE_REPORT_0x02_SIZE 37
1025#define DS4_FEATURE_REPORT_0x81_SIZE 7
Roderick Colenbrander6dfd5bf2016-10-07 12:39:36 -07001026#define DS4_INPUT_REPORT_0x11_SIZE 78
Roderick Colenbrander5443a632016-10-07 12:39:35 -07001027#define DS4_OUTPUT_REPORT_0x05_SIZE 32
1028#define DS4_OUTPUT_REPORT_0x11_SIZE 78
Antonio Ospite29b691a2015-02-16 18:12:21 +01001029#define SIXAXIS_REPORT_0xF2_SIZE 17
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001030#define SIXAXIS_REPORT_0xF5_SIZE 8
Simon Wood41d2d422015-06-09 21:27:06 -06001031#define MOTION_REPORT_0x02_SIZE 49
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001032
Jiri Kosina8b402c92015-02-23 11:15:44 +01001033static DEFINE_SPINLOCK(sony_dev_list_lock);
Frank Praznikd2d782f2014-02-20 11:36:03 -05001034static LIST_HEAD(sony_device_list);
Frank Praznik80250872014-04-14 10:11:35 -04001035static DEFINE_IDA(sony_device_id_allocator);
Frank Praznikd2d782f2014-02-20 11:36:03 -05001036
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001037struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -05001038 spinlock_t lock;
Frank Praznikd2d782f2014-02-20 11:36:03 -05001039 struct list_head list_node;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001040 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -05001041 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001042 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001043 struct work_struct state_worker;
Antonio Ospite09593e32016-02-09 13:55:06 +01001044 void (*send_output_report)(struct sony_sc *);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001045 struct power_supply *battery;
1046 struct power_supply_desc battery_desc;
Frank Praznik80250872014-04-14 10:11:35 -04001047 int device_id;
Pavel Machek1adf9042016-02-09 13:55:08 +01001048 u8 *output_report_dmabuf;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001049
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001050#ifdef CONFIG_SONY_FF
Pavel Machek1adf9042016-02-09 13:55:08 +01001051 u8 left;
1052 u8 right;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001053#endif
1054
Pavel Machek1adf9042016-02-09 13:55:08 +01001055 u8 mac_address[6];
1056 u8 worker_initialized;
Frank Praznik2a242932016-09-22 20:18:09 -04001057 u8 defer_initialization;
Pavel Machek1adf9042016-02-09 13:55:08 +01001058 u8 cable_state;
1059 u8 battery_charging;
1060 u8 battery_capacity;
1061 u8 led_state[MAX_LEDS];
Pavel Machek1adf9042016-02-09 13:55:08 +01001062 u8 led_delay_on[MAX_LEDS];
1063 u8 led_delay_off[MAX_LEDS];
1064 u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001065};
1066
Frank Praznik2a242932016-09-22 20:18:09 -04001067static inline void sony_schedule_work(struct sony_sc *sc)
1068{
1069 if (!sc->defer_initialization)
1070 schedule_work(&sc->state_worker);
1071}
1072
Pavel Machek1adf9042016-02-09 13:55:08 +01001073static u8 *sixaxis_fixup(struct hid_device *hdev, u8 *rdesc,
Antonio Ospitec607fb82014-06-24 13:28:41 +02001074 unsigned int *rsize)
1075{
1076 *rsize = sizeof(sixaxis_rdesc);
1077 return sixaxis_rdesc;
1078}
1079
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001080static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
1081 unsigned int *rsize)
1082{
1083 *rsize = sizeof(motion_rdesc);
1084 return motion_rdesc;
1085}
1086
Simon Woodb2723eb2015-06-17 00:08:53 -06001087static u8 *navigation_fixup(struct hid_device *hdev, u8 *rdesc,
1088 unsigned int *rsize)
1089{
1090 *rsize = sizeof(navigation_rdesc);
1091 return navigation_rdesc;
1092}
1093
Pavel Machek1adf9042016-02-09 13:55:08 +01001094static u8 *ps3remote_fixup(struct hid_device *hdev, u8 *rdesc,
Jiri Kosina078328d2013-06-13 12:03:49 +02001095 unsigned int *rsize)
1096{
1097 *rsize = sizeof(ps3remote_rdesc);
1098 return ps3remote_rdesc;
1099}
1100
1101static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
1102 struct hid_field *field, struct hid_usage *usage,
1103 unsigned long **bit, int *max)
1104{
1105 unsigned int key = usage->hid & HID_USAGE;
1106
1107 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1108 return -1;
1109
1110 switch (usage->collection_index) {
1111 case 1:
1112 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
1113 return -1;
1114
1115 key = ps3remote_keymap_joypad_buttons[key];
1116 if (!key)
1117 return -1;
1118 break;
1119 case 2:
1120 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
1121 return -1;
1122
1123 key = ps3remote_keymap_remote_buttons[key];
1124 if (!key)
1125 return -1;
1126 break;
1127 default:
1128 return -1;
1129 }
1130
1131 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1132 return 1;
1133}
1134
Pavel Machek1adf9042016-02-09 13:55:08 +01001135static u8 *sony_report_fixup(struct hid_device *hdev, u8 *rdesc,
Nikolai Kondrashov73e40082010-08-06 23:03:06 +04001136 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001137{
1138 struct sony_sc *sc = hid_get_drvdata(hdev);
1139
Mikko Perttunen4ba1eee2016-07-21 19:54:48 +03001140 if (sc->quirks & (SINO_LITE_CONTROLLER | FUTUREMAX_DANCE_MAT))
Scott Moreau74500cc2016-01-13 07:40:42 -07001141 return rdesc;
1142
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +09001143 /*
1144 * Some Sony RF receivers wrongly declare the mouse pointer as a
1145 * a constant non-data variable.
1146 */
1147 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
1148 /* usage page: generic desktop controls */
1149 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
1150 /* usage: mouse */
1151 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
1152 /* input (usage page for x,y axes): constant, variable, relative */
1153 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +09001154 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +09001155 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001156 rdesc[55] = 0x06;
1157 }
Simon Wood61ab44b2011-06-10 12:00:26 +02001158
Frank Prazniked19d8c2014-01-16 21:43:12 -05001159 /*
1160 * The default Dualshock 4 USB descriptor doesn't assign
1161 * the gyroscope values to corresponding axes so we need a
1162 * modified one.
1163 */
Frank Praznikb71b5572015-11-06 15:35:53 -05001164 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Prazniked19d8c2014-01-16 21:43:12 -05001165 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
1166 rdesc = dualshock4_usb_rdesc;
1167 *rsize = sizeof(dualshock4_usb_rdesc);
Frank Praznikb71b5572015-11-06 15:35:53 -05001168 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
Frank Praznikd829674d2014-02-05 20:03:45 -05001169 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
1170 rdesc = dualshock4_bt_rdesc;
1171 *rsize = sizeof(dualshock4_bt_rdesc);
Frank Prazniked19d8c2014-01-16 21:43:12 -05001172 }
1173
Antonio Ospitec607fb82014-06-24 13:28:41 +02001174 if (sc->quirks & SIXAXIS_CONTROLLER)
1175 return sixaxis_fixup(hdev, rdesc, rsize);
Jiri Kosina078328d2013-06-13 12:03:49 +02001176
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001177 if (sc->quirks & MOTION_CONTROLLER)
1178 return motion_fixup(hdev, rdesc, rsize);
1179
Simon Wood4545ee02015-06-17 00:08:52 -06001180 if (sc->quirks & NAVIGATION_CONTROLLER)
Simon Woodb2723eb2015-06-17 00:08:53 -06001181 return navigation_fixup(hdev, rdesc, rsize);
Simon Wood4545ee02015-06-17 00:08:52 -06001182
Jiri Kosina078328d2013-06-13 12:03:49 +02001183 if (sc->quirks & PS3REMOTE)
1184 return ps3remote_fixup(hdev, rdesc, rsize);
1185
Nikolai Kondrashov73e40082010-08-06 23:03:06 +04001186 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001187}
1188
Pavel Machek1adf9042016-02-09 13:55:08 +01001189static void sixaxis_parse_report(struct sony_sc *sc, u8 *rd, int size)
Frank Praznikd902f472014-01-27 10:17:36 -05001190{
Pavel Machek1adf9042016-02-09 13:55:08 +01001191 static const u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
Frank Praznikd902f472014-01-27 10:17:36 -05001192 unsigned long flags;
Simon Wood12e9a6d2015-06-09 21:27:05 -06001193 int offset;
Pavel Machek1adf9042016-02-09 13:55:08 +01001194 u8 cable_state, battery_capacity, battery_charging;
Frank Praznikd902f472014-01-27 10:17:36 -05001195
Frank Praznikad142b92014-02-20 11:36:00 -05001196 /*
1197 * The sixaxis is charging if the battery value is 0xee
Frank Praznikd902f472014-01-27 10:17:36 -05001198 * and it is fully charged if the value is 0xef.
1199 * It does not report the actual level while charging so it
1200 * is set to 100% while charging is in progress.
1201 */
Simon Wood12e9a6d2015-06-09 21:27:05 -06001202 offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
1203
1204 if (rd[offset] >= 0xee) {
Frank Praznikd902f472014-01-27 10:17:36 -05001205 battery_capacity = 100;
Simon Wood12e9a6d2015-06-09 21:27:05 -06001206 battery_charging = !(rd[offset] & 0x01);
Frank Praznik9fddd742014-08-29 13:11:52 -04001207 cable_state = 1;
Frank Praznikd902f472014-01-27 10:17:36 -05001208 } else {
Pavel Machek1adf9042016-02-09 13:55:08 +01001209 u8 index = rd[offset] <= 5 ? rd[offset] : 5;
Frank Praznikac3c9a92014-02-20 11:36:02 -05001210 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -05001211 battery_charging = 0;
Frank Praznik9fddd742014-08-29 13:11:52 -04001212 cable_state = 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001213 }
Frank Praznikd902f472014-01-27 10:17:36 -05001214
1215 spin_lock_irqsave(&sc->lock, flags);
1216 sc->cable_state = cable_state;
1217 sc->battery_capacity = battery_capacity;
1218 sc->battery_charging = battery_charging;
1219 spin_unlock_irqrestore(&sc->lock, flags);
1220}
1221
Pavel Machek1adf9042016-02-09 13:55:08 +01001222static void dualshock4_parse_report(struct sony_sc *sc, u8 *rd, int size)
Frank Praznikd902f472014-01-27 10:17:36 -05001223{
Frank Praznike5606232014-01-27 10:17:37 -05001224 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1225 struct hid_input, list);
1226 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -05001227 unsigned long flags;
Frank Praznik6c5f8602014-02-05 20:03:47 -05001228 int n, offset;
Pavel Machek1adf9042016-02-09 13:55:08 +01001229 u8 cable_state, battery_capacity, battery_charging;
Frank Praznikd902f472014-01-27 10:17:36 -05001230
Frank Praznikad142b92014-02-20 11:36:00 -05001231 /*
1232 * Battery and touchpad data starts at byte 30 in the USB report and
Frank Praznik6c5f8602014-02-05 20:03:47 -05001233 * 32 in Bluetooth report.
1234 */
1235 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
1236
Frank Praznikad142b92014-02-20 11:36:00 -05001237 /*
1238 * The lower 4 bits of byte 30 contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -05001239 * and the 5th bit contains the USB cable state.
1240 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001241 cable_state = (rd[offset] >> 4) & 0x01;
1242 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -05001243
Frank Praznikad142b92014-02-20 11:36:00 -05001244 /*
1245 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -05001246 * 0 to 10, and when running on battery power it ranges from 0 to 9.
1247 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -05001248 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001249 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -05001250 battery_charging = 0;
1251 else
1252 battery_charging = 1;
1253
Frank Praznik6c5f8602014-02-05 20:03:47 -05001254 if (!cable_state)
1255 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -05001256 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -05001257 battery_capacity = 10;
1258
Frank Praznikd902f472014-01-27 10:17:36 -05001259 battery_capacity *= 10;
1260
1261 spin_lock_irqsave(&sc->lock, flags);
1262 sc->cable_state = cable_state;
1263 sc->battery_capacity = battery_capacity;
1264 sc->battery_charging = battery_charging;
1265 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -05001266
Frank Praznik6c5f8602014-02-05 20:03:47 -05001267 offset += 5;
1268
Frank Praznikad142b92014-02-20 11:36:00 -05001269 /*
1270 * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
Frank Praznik6c5f8602014-02-05 20:03:47 -05001271 * and 37 on Bluetooth.
Frank Praznike5606232014-01-27 10:17:37 -05001272 * The first 7 bits of the first byte is a counter and bit 8 is a touch
1273 * indicator that is 0 when pressed and 1 when not pressed.
1274 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1275 * The data for the second touch is in the same format and immediatly
1276 * follows the data for the first.
1277 */
1278 for (n = 0; n < 2; n++) {
Pavel Machek1adf9042016-02-09 13:55:08 +01001279 u16 x, y;
Frank Praznike5606232014-01-27 10:17:37 -05001280
1281 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1282 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1283
1284 input_mt_slot(input_dev, n);
1285 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
1286 !(rd[offset] >> 7));
1287 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1288 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1289
1290 offset += 4;
1291 }
Frank Praznikd902f472014-01-27 10:17:36 -05001292}
1293
Simon Woodc9e4d872011-06-10 12:00:27 +02001294static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
Pavel Machek1adf9042016-02-09 13:55:08 +01001295 u8 *rd, int size)
Simon Woodc9e4d872011-06-10 12:00:27 +02001296{
1297 struct sony_sc *sc = hid_get_drvdata(hdev);
1298
Frank Praznikad142b92014-02-20 11:36:00 -05001299 /*
1300 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +02001301 * has to be BYTE_SWAPPED before passing up to joystick interface
1302 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001303 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Frank Praznik8f5f0bc2015-07-23 19:01:16 -04001304 /*
1305 * When connected via Bluetooth the Sixaxis occasionally sends
1306 * a report with the second byte 0xff and the rest zeroed.
1307 *
1308 * This report does not reflect the actual state of the
1309 * controller must be ignored to avoid generating false input
1310 * events.
1311 */
1312 if (rd[1] == 0xff)
1313 return -EINVAL;
1314
Simon Woodc9e4d872011-06-10 12:00:27 +02001315 swap(rd[41], rd[42]);
1316 swap(rd[43], rd[44]);
1317 swap(rd[45], rd[46]);
1318 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -05001319
1320 sixaxis_parse_report(sc, rd, size);
Simon Wood12e9a6d2015-06-09 21:27:05 -06001321 } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
1322 sixaxis_parse_report(sc, rd, size);
Simon Wood4545ee02015-06-17 00:08:52 -06001323 } else if ((sc->quirks & NAVIGATION_CONTROLLER) && rd[0] == 0x01 &&
1324 size == 49) {
1325 sixaxis_parse_report(sc, rd, size);
Frank Praznik68330d82014-02-05 20:03:49 -05001326 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1327 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1328 && rd[0] == 0x11 && size == 78)) {
Roderick Colenbrander6dfd5bf2016-10-07 12:39:36 -07001329 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
1330 /* CRC check */
1331 u8 bthdr = 0xA1;
1332 u32 crc;
1333 u32 report_crc;
1334
1335 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
1336 crc = ~crc32_le(crc, rd, DS4_INPUT_REPORT_0x11_SIZE-4);
1337 report_crc = get_unaligned_le32(&rd[DS4_INPUT_REPORT_0x11_SIZE-4]);
1338 if (crc != report_crc) {
1339 hid_dbg(sc->hdev, "DualShock 4 input report's CRC check failed, received crc 0x%0x != 0x%0x\n",
1340 report_crc, crc);
1341 return -EILSEQ;
1342 }
1343 }
Frank Praznikd902f472014-01-27 10:17:36 -05001344 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +02001345 }
1346
Frank Praznik2a242932016-09-22 20:18:09 -04001347 if (sc->defer_initialization) {
1348 sc->defer_initialization = 0;
1349 sony_schedule_work(sc);
1350 }
1351
Simon Woodc9e4d872011-06-10 12:00:27 +02001352 return 0;
1353}
1354
Colin Leitnerf04d5142013-05-27 23:41:05 +02001355static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1356 struct hid_field *field, struct hid_usage *usage,
1357 unsigned long **bit, int *max)
1358{
1359 struct sony_sc *sc = hid_get_drvdata(hdev);
1360
1361 if (sc->quirks & BUZZ_CONTROLLER) {
1362 unsigned int key = usage->hid & HID_USAGE;
1363
1364 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1365 return -1;
1366
1367 switch (usage->collection_index) {
1368 case 1:
1369 if (key >= ARRAY_SIZE(buzz_keymap))
1370 return -1;
1371
1372 key = buzz_keymap[key];
1373 if (!key)
1374 return -1;
1375 break;
1376 default:
1377 return -1;
1378 }
1379
1380 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1381 return 1;
1382 }
1383
Jiri Kosina078328d2013-06-13 12:03:49 +02001384 if (sc->quirks & PS3REMOTE)
1385 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1386
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001387 /* Let hid-core decide for the others */
1388 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001389}
1390
Frank Praznikce8efc32014-09-18 21:15:01 -04001391static int sony_register_touchpad(struct hid_input *hi, int touch_count,
1392 int w, int h)
1393{
1394 struct input_dev *input_dev = hi->input;
1395 int ret;
1396
1397 ret = input_mt_init_slots(input_dev, touch_count, 0);
1398 if (ret < 0)
1399 return ret;
1400
1401 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1402 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1403
1404 return 0;
1405}
1406
Frank Praznikce8efc32014-09-18 21:15:01 -04001407
Antonio Ospite5710fab2011-02-20 18:26:45 +01001408/*
Jiri Slabybd28ce02008-06-25 23:47:04 +02001409 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1410 * to "operational". Without this, the ps3 controller will not report any
1411 * events.
1412 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001413static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001414{
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001415 const int buf_size =
1416 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
Pavel Machek1adf9042016-02-09 13:55:08 +01001417 u8 *buf;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001418 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001419
Antonio Ospite2e701a32015-02-16 18:12:24 +01001420 buf = kmalloc(buf_size, GFP_KERNEL);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001421 if (!buf)
1422 return -ENOMEM;
1423
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001424 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1425 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001426 if (ret < 0) {
1427 hid_err(hdev, "can't set operational mode: step 1\n");
1428 goto out;
1429 }
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001430
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001431 /*
1432 * Some compatible controllers like the Speedlink Strike FX and
1433 * Gasia need another query plus an USB interrupt to get operational.
1434 */
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001435 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1436 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001437 if (ret < 0) {
1438 hid_err(hdev, "can't set operational mode: step 2\n");
1439 goto out;
1440 }
1441
1442 ret = hid_hw_output_report(hdev, buf, 1);
Benjamin Tissoires19f4c2b2016-01-08 17:58:49 +01001443 if (ret < 0) {
1444 hid_info(hdev, "can't set operational mode: step 3, ignoring\n");
1445 ret = 0;
1446 }
Jiri Slabybd28ce02008-06-25 23:47:04 +02001447
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001448out:
Jiri Slabybd28ce02008-06-25 23:47:04 +02001449 kfree(buf);
1450
1451 return ret;
1452}
1453
Antonio Ospite816651a2010-05-03 22:15:55 +02001454static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001455{
Pavel Machek1adf9042016-02-09 13:55:08 +01001456 static const u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1457 u8 *buf;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001458 int ret;
1459
1460 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1461 if (!buf)
1462 return -ENOMEM;
1463
1464 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
Benjamin Tissoiresb0dd72a2014-02-10 12:58:54 -05001465 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001466
1467 kfree(buf);
1468
1469 return ret;
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001470}
1471
Frank Praznikad142b92014-02-20 11:36:00 -05001472/*
1473 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
Frank Praznik68330d82014-02-05 20:03:49 -05001474 * controller so that it sends full input reports of type 0x11.
1475 */
1476static int dualshock4_set_operational_bt(struct hid_device *hdev)
1477{
Pavel Machek1adf9042016-02-09 13:55:08 +01001478 u8 *buf;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001479 int ret;
Frank Praznik68330d82014-02-05 20:03:49 -05001480
Roderick Colenbrander5443a632016-10-07 12:39:35 -07001481 buf = kmalloc(DS4_FEATURE_REPORT_0x02_SIZE, GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001482 if (!buf)
1483 return -ENOMEM;
1484
Roderick Colenbrander5443a632016-10-07 12:39:35 -07001485 ret = hid_hw_raw_request(hdev, 0x02, buf, DS4_FEATURE_REPORT_0x02_SIZE,
Frank Praznik68330d82014-02-05 20:03:49 -05001486 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001487
1488 kfree(buf);
1489
1490 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001491}
1492
Frank Praznik221399b2015-05-05 20:47:32 -04001493static void sixaxis_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001494{
Pavel Machek1adf9042016-02-09 13:55:08 +01001495 static const u8 sixaxis_leds[10][4] = {
Frank Praznik80250872014-04-14 10:11:35 -04001496 { 0x01, 0x00, 0x00, 0x00 },
1497 { 0x00, 0x01, 0x00, 0x00 },
1498 { 0x00, 0x00, 0x01, 0x00 },
1499 { 0x00, 0x00, 0x00, 0x01 },
1500 { 0x01, 0x00, 0x00, 0x01 },
1501 { 0x00, 0x01, 0x00, 0x01 },
1502 { 0x00, 0x00, 0x01, 0x01 },
1503 { 0x01, 0x00, 0x01, 0x01 },
1504 { 0x00, 0x01, 0x01, 0x01 },
1505 { 0x01, 0x01, 0x01, 0x01 }
1506 };
1507
Frank Praznik221399b2015-05-05 20:47:32 -04001508 int id = sc->device_id;
1509
1510 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001511
1512 if (id < 0)
1513 return;
1514
1515 id %= 10;
Frank Praznik221399b2015-05-05 20:47:32 -04001516 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001517}
1518
Frank Praznik221399b2015-05-05 20:47:32 -04001519static void dualshock4_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001520{
1521 /* The first 4 color/index entries match what the PS4 assigns */
Pavel Machek1adf9042016-02-09 13:55:08 +01001522 static const u8 color_code[7][3] = {
Frank Praznik80250872014-04-14 10:11:35 -04001523 /* Blue */ { 0x00, 0x00, 0x01 },
1524 /* Red */ { 0x01, 0x00, 0x00 },
1525 /* Green */ { 0x00, 0x01, 0x00 },
1526 /* Pink */ { 0x02, 0x00, 0x01 },
1527 /* Orange */ { 0x02, 0x01, 0x00 },
1528 /* Teal */ { 0x00, 0x01, 0x01 },
1529 /* White */ { 0x01, 0x01, 0x01 }
1530 };
1531
Frank Praznik221399b2015-05-05 20:47:32 -04001532 int id = sc->device_id;
1533
1534 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001535
1536 if (id < 0)
1537 return;
1538
1539 id %= 7;
Frank Praznik221399b2015-05-05 20:47:32 -04001540 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001541}
1542
Frank Praznik221399b2015-05-05 20:47:32 -04001543static void buzz_set_leds(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001544{
Frank Praznik221399b2015-05-05 20:47:32 -04001545 struct hid_device *hdev = sc->hdev;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001546 struct list_head *report_list =
1547 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1548 struct hid_report *report = list_entry(report_list->next,
1549 struct hid_report, list);
Pavel Machek1adf9042016-02-09 13:55:08 +01001550 s32 *value = report->field[0]->value;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001551
Frank Praznik221399b2015-05-05 20:47:32 -04001552 BUILD_BUG_ON(MAX_LEDS < 4);
1553
Colin Leitnerf04d5142013-05-27 23:41:05 +02001554 value[0] = 0x00;
Frank Praznik221399b2015-05-05 20:47:32 -04001555 value[1] = sc->led_state[0] ? 0xff : 0x00;
1556 value[2] = sc->led_state[1] ? 0xff : 0x00;
1557 value[3] = sc->led_state[2] ? 0xff : 0x00;
1558 value[4] = sc->led_state[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001559 value[5] = 0x00;
1560 value[6] = 0x00;
1561 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1562}
1563
Frank Praznik221399b2015-05-05 20:47:32 -04001564static void sony_set_leds(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001565{
Frank Praznik221399b2015-05-05 20:47:32 -04001566 if (!(sc->quirks & BUZZ_CONTROLLER))
Frank Praznik2a242932016-09-22 20:18:09 -04001567 sony_schedule_work(sc);
Frank Praznik221399b2015-05-05 20:47:32 -04001568 else
1569 buzz_set_leds(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001570}
1571
Sven Eckelmannc5382512013-11-19 20:26:30 +01001572static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001573 enum led_brightness value)
1574{
1575 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001576 struct hid_device *hdev = to_hid_device(dev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001577 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001578
1579 int n;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001580 int force_update;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001581
1582 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001583 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001584 hid_err(hdev, "No device data\n");
1585 return;
1586 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001587
Frank Praznikb3ed4582014-04-14 10:11:36 -04001588 /*
1589 * The Sixaxis on USB will override any LED settings sent to it
1590 * and keep flashing all of the LEDs until the PS button is pressed.
1591 * Updates, even if redundant, must be always be sent to the
1592 * controller to avoid having to toggle the state of an LED just to
1593 * stop the flashing later on.
1594 */
1595 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1596
Frank Praznik60781cf2014-01-11 15:13:15 -05001597 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznikb3ed4582014-04-14 10:11:36 -04001598 if (led == drv_data->leds[n] && (force_update ||
1599 (value != drv_data->led_state[n] ||
1600 drv_data->led_delay_on[n] ||
1601 drv_data->led_delay_off[n]))) {
1602
1603 drv_data->led_state[n] = value;
1604
1605 /* Setting the brightness stops the blinking */
1606 drv_data->led_delay_on[n] = 0;
1607 drv_data->led_delay_off[n] = 0;
1608
Frank Praznik221399b2015-05-05 20:47:32 -04001609 sony_set_leds(drv_data);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001610 break;
1611 }
1612 }
1613}
1614
Sven Eckelmannc5382512013-11-19 20:26:30 +01001615static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001616{
1617 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001618 struct hid_device *hdev = to_hid_device(dev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001619 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001620
1621 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001622
1623 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001624 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001625 hid_err(hdev, "No device data\n");
1626 return LED_OFF;
1627 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001628
Frank Praznik60781cf2014-01-11 15:13:15 -05001629 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001630 if (led == drv_data->leds[n])
1631 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001632 }
1633
Simon Wood7db75042014-02-05 12:34:18 -07001634 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001635}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001636
Frank Praznikb3ed4582014-04-14 10:11:36 -04001637static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1638 unsigned long *delay_off)
1639{
1640 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001641 struct hid_device *hdev = to_hid_device(dev);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001642 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1643 int n;
Pavel Machek1adf9042016-02-09 13:55:08 +01001644 u8 new_on, new_off;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001645
1646 if (!drv_data) {
1647 hid_err(hdev, "No device data\n");
1648 return -EINVAL;
1649 }
1650
1651 /* Max delay is 255 deciseconds or 2550 milliseconds */
1652 if (*delay_on > 2550)
1653 *delay_on = 2550;
1654 if (*delay_off > 2550)
1655 *delay_off = 2550;
1656
1657 /* Blink at 1 Hz if both values are zero */
1658 if (!*delay_on && !*delay_off)
1659 *delay_on = *delay_off = 500;
1660
1661 new_on = *delay_on / 10;
1662 new_off = *delay_off / 10;
1663
1664 for (n = 0; n < drv_data->led_count; n++) {
1665 if (led == drv_data->leds[n])
1666 break;
1667 }
1668
1669 /* This LED is not registered on this device */
1670 if (n >= drv_data->led_count)
1671 return -EINVAL;
1672
1673 /* Don't schedule work if the values didn't change */
1674 if (new_on != drv_data->led_delay_on[n] ||
1675 new_off != drv_data->led_delay_off[n]) {
1676 drv_data->led_delay_on[n] = new_on;
1677 drv_data->led_delay_off[n] = new_off;
Frank Praznik2a242932016-09-22 20:18:09 -04001678 sony_schedule_work(drv_data);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001679 }
1680
1681 return 0;
1682}
1683
Frank Praznikfa57a812014-04-14 10:11:33 -04001684static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001685{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001686 struct led_classdev *led;
1687 int n;
1688
Frank Praznikfa57a812014-04-14 10:11:33 -04001689 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001690
Frank Praznikfa57a812014-04-14 10:11:33 -04001691 for (n = 0; n < sc->led_count; n++) {
1692 led = sc->leds[n];
1693 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001694 if (!led)
1695 continue;
1696 led_classdev_unregister(led);
1697 kfree(led);
1698 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001699
Frank Praznikfa57a812014-04-14 10:11:33 -04001700 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001701}
1702
Frank Praznikfa57a812014-04-14 10:11:33 -04001703static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001704{
Frank Praznikfa57a812014-04-14 10:11:33 -04001705 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001706 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001707 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001708 struct led_classdev *led;
1709 size_t name_sz;
1710 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001711 size_t name_len;
1712 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001713 static const char * const ds4_name_str[] = { "red", "green", "blue",
1714 "global" };
Pavel Machek1adf9042016-02-09 13:55:08 +01001715 u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
1716 u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001717
Frank Praznikfa57a812014-04-14 10:11:33 -04001718 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001719
Frank Praznikfa57a812014-04-14 10:11:33 -04001720 if (sc->quirks & BUZZ_CONTROLLER) {
1721 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001722 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001723 name_len = strlen("::buzz#");
1724 name_fmt = "%s::buzz%d";
1725 /* Validate expected report characteristics. */
1726 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1727 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001728 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik221399b2015-05-05 20:47:32 -04001729 dualshock4_set_leds_from_id(sc);
1730 sc->led_state[3] = 1;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001731 sc->led_count = 4;
1732 memset(max_brightness, 255, 3);
1733 use_hw_blink[3] = 1;
1734 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001735 name_len = 0;
1736 name_fmt = "%s:%s";
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001737 } else if (sc->quirks & MOTION_CONTROLLER) {
1738 sc->led_count = 3;
1739 memset(max_brightness, 255, 3);
1740 use_ds4_names = 1;
1741 name_len = 0;
1742 name_fmt = "%s:%s";
Simon Wood4545ee02015-06-17 00:08:52 -06001743 } else if (sc->quirks & NAVIGATION_CONTROLLER) {
Pavel Machek1adf9042016-02-09 13:55:08 +01001744 static const u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00};
Simon Wood4545ee02015-06-17 00:08:52 -06001745
1746 memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds));
1747 sc->led_count = 1;
1748 memset(use_hw_blink, 1, 4);
1749 use_ds4_names = 0;
1750 name_len = strlen("::sony#");
1751 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001752 } else {
Frank Praznik221399b2015-05-05 20:47:32 -04001753 sixaxis_set_leds_from_id(sc);
Frank Praznikfa57a812014-04-14 10:11:33 -04001754 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001755 memset(use_hw_blink, 1, 4);
1756 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001757 name_len = strlen("::sony#");
1758 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001759 }
1760
Frank Praznikad142b92014-02-20 11:36:00 -05001761 /*
1762 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001763 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001764 * LEDs to on
1765 */
Frank Praznik221399b2015-05-05 20:47:32 -04001766 sony_set_leds(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001767
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001768 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001769
Frank Praznikfa57a812014-04-14 10:11:33 -04001770 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001771
Frank Praznikb3ed4582014-04-14 10:11:36 -04001772 if (use_ds4_names)
1773 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001774
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001775 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1776 if (!led) {
1777 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001778 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001779 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001780 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001781
1782 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001783 if (use_ds4_names)
1784 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1785 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001786 else
1787 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001788 led->name = name;
Frank Praznik221399b2015-05-05 20:47:32 -04001789 led->brightness = sc->led_state[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001790 led->max_brightness = max_brightness[n];
Frank Praznike6061c12017-02-08 13:58:43 -05001791 led->flags = LED_CORE_SUSPENDRESUME;
Sven Eckelmannc5382512013-11-19 20:26:30 +01001792 led->brightness_get = sony_led_get_brightness;
1793 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001794
Frank Praznikb3ed4582014-04-14 10:11:36 -04001795 if (use_hw_blink[n])
1796 led->blink_set = sony_led_blink_set;
1797
Frank Praznik80250872014-04-14 10:11:35 -04001798 sc->leds[n] = led;
1799
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001800 ret = led_classdev_register(&hdev->dev, led);
1801 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001802 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001803 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001804 kfree(led);
1805 goto error_leds;
1806 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001807 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001808
1809 return ret;
1810
Colin Leitnerf04d5142013-05-27 23:41:05 +02001811error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001812 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001813
Colin Leitnerf04d5142013-05-27 23:41:05 +02001814 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001815}
1816
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001817static void sixaxis_send_output_report(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001818{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001819 static const union sixaxis_output_report_01 default_report = {
Frank Praznik55d3b662014-04-14 10:11:32 -04001820 .buf = {
1821 0x01,
Scott Moreauad07b7a2016-01-13 07:40:43 -07001822 0x01, 0xff, 0x00, 0xff, 0x00,
Frank Praznik55d3b662014-04-14 10:11:32 -04001823 0x00, 0x00, 0x00, 0x00, 0x00,
1824 0xff, 0x27, 0x10, 0x00, 0x32,
1825 0xff, 0x27, 0x10, 0x00, 0x32,
1826 0xff, 0x27, 0x10, 0x00, 0x32,
1827 0xff, 0x27, 0x10, 0x00, 0x32,
1828 0x00, 0x00, 0x00, 0x00, 0x00
1829 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001830 };
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001831 struct sixaxis_output_report *report =
1832 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1833 int n;
1834
1835 /* Initialize the report with default values */
1836 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001837
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001838#ifdef CONFIG_SONY_FF
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001839 report->rumble.right_motor_on = sc->right ? 1 : 0;
1840 report->rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001841#endif
1842
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001843 report->leds_bitmap |= sc->led_state[0] << 1;
1844 report->leds_bitmap |= sc->led_state[1] << 2;
1845 report->leds_bitmap |= sc->led_state[2] << 3;
1846 report->leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001847
Simon Wood88f65762014-04-14 10:11:37 -04001848 /* Set flag for all leds off, required for 3rd party INTEC controller */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001849 if ((report->leds_bitmap & 0x1E) == 0)
1850 report->leds_bitmap |= 0x20;
Simon Wood88f65762014-04-14 10:11:37 -04001851
Frank Praznikb3ed4582014-04-14 10:11:36 -04001852 /*
1853 * The LEDs in the report are indexed in reverse order to their
1854 * corresponding light on the controller.
1855 * Index 0 = LED 4, index 1 = LED 3, etc...
1856 *
1857 * In the case of both delay values being zero (blinking disabled) the
1858 * default report values should be used or the controller LED will be
1859 * always off.
1860 */
1861 for (n = 0; n < 4; n++) {
1862 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001863 report->led[3 - n].duty_off = sc->led_delay_off[n];
1864 report->led[3 - n].duty_on = sc->led_delay_on[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001865 }
1866 }
1867
Pavel Machek1adf9042016-02-09 13:55:08 +01001868 hid_hw_raw_request(sc->hdev, report->report_id, (u8 *)report,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001869 sizeof(struct sixaxis_output_report),
1870 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001871}
1872
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001873static void dualshock4_send_output_report(struct sony_sc *sc)
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001874{
Frank Praznik0da8ea62014-01-16 21:42:51 -05001875 struct hid_device *hdev = sc->hdev;
Pavel Machek1adf9042016-02-09 13:55:08 +01001876 u8 *buf = sc->output_report_dmabuf;
Frank Praznik48220232014-02-05 20:03:44 -05001877 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001878
Frank Praznikc4425c82016-09-22 20:18:10 -04001879 /*
1880 * NOTE: The buf[1] field of the Bluetooth report controls
1881 * the Dualshock 4 reporting rate.
1882 *
1883 * Known values include:
1884 *
1885 * 0x80 - 1000hz (full speed)
1886 * 0xA0 - 31hz
1887 * 0xB0 - 20hz
1888 * 0xD0 - 66hz
1889 */
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001890 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Roderick Colenbrander5443a632016-10-07 12:39:35 -07001891 memset(buf, 0, DS4_OUTPUT_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001892 buf[0] = 0x05;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001893 buf[1] = 0xFF;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001894 offset = 4;
1895 } else {
Roderick Colenbrander5443a632016-10-07 12:39:35 -07001896 memset(buf, 0, DS4_OUTPUT_REPORT_0x11_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001897 buf[0] = 0x11;
Roderick Colenbranderc076f052016-10-07 12:39:37 -07001898 buf[1] = 0xC0; /* HID + CRC */
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001899 buf[3] = 0x0F;
1900 offset = 6;
1901 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001902
1903#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001904 buf[offset++] = sc->right;
1905 buf[offset++] = sc->left;
1906#else
1907 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001908#endif
1909
Frank Praznikb3ed4582014-04-14 10:11:36 -04001910 /* LED 3 is the global control */
1911 if (sc->led_state[3]) {
1912 buf[offset++] = sc->led_state[0];
1913 buf[offset++] = sc->led_state[1];
1914 buf[offset++] = sc->led_state[2];
1915 } else {
1916 offset += 3;
1917 }
1918
1919 /* If both delay values are zero the DualShock 4 disables blinking. */
1920 buf[offset++] = sc->led_delay_on[3];
1921 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05001922
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001923 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
Roderick Colenbrander5443a632016-10-07 12:39:35 -07001924 hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x05_SIZE);
Roderick Colenbranderc076f052016-10-07 12:39:37 -07001925 else {
1926 /* CRC generation */
1927 u8 bthdr = 0xA2;
1928 u32 crc;
1929
1930 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
1931 crc = ~crc32_le(crc, buf, DS4_OUTPUT_REPORT_0x11_SIZE-4);
1932 put_unaligned_le32(crc, &buf[74]);
1933 hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x11_SIZE);
1934 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001935}
1936
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001937static void motion_send_output_report(struct sony_sc *sc)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001938{
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001939 struct hid_device *hdev = sc->hdev;
1940 struct motion_output_report_02 *report =
1941 (struct motion_output_report_02 *)sc->output_report_dmabuf;
1942
Simon Wood41d2d422015-06-09 21:27:06 -06001943 memset(report, 0, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001944
1945 report->type = 0x02; /* set leds */
1946 report->r = sc->led_state[0];
1947 report->g = sc->led_state[1];
1948 report->b = sc->led_state[2];
1949
1950#ifdef CONFIG_SONY_FF
1951 report->rumble = max(sc->right, sc->left);
1952#endif
1953
Pavel Machek1adf9042016-02-09 13:55:08 +01001954 hid_hw_output_report(hdev, (u8 *)report, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001955}
1956
Frank Praznikdecd9462015-11-11 09:49:38 -05001957static inline void sony_send_output_report(struct sony_sc *sc)
1958{
1959 if (sc->send_output_report)
1960 sc->send_output_report(sc);
1961}
1962
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001963static void sony_state_worker(struct work_struct *work)
1964{
1965 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Antonio Ospiteef916ef2016-02-09 13:55:07 +01001966
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001967 sc->send_output_report(sc);
1968}
1969
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001970static int sony_allocate_output_report(struct sony_sc *sc)
1971{
Simon Wood4545ee02015-06-17 00:08:52 -06001972 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
1973 (sc->quirks & NAVIGATION_CONTROLLER))
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001974 sc->output_report_dmabuf =
1975 kmalloc(sizeof(union sixaxis_output_report_01),
1976 GFP_KERNEL);
1977 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
Roderick Colenbrander5443a632016-10-07 12:39:35 -07001978 sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x11_SIZE,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001979 GFP_KERNEL);
1980 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
Roderick Colenbrander5443a632016-10-07 12:39:35 -07001981 sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x05_SIZE,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001982 GFP_KERNEL);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001983 else if (sc->quirks & MOTION_CONTROLLER)
Simon Wood41d2d422015-06-09 21:27:06 -06001984 sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE,
1985 GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001986 else
1987 return 0;
1988
1989 if (!sc->output_report_dmabuf)
1990 return -ENOMEM;
1991
1992 return 0;
1993}
1994
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001995#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001996static int sony_play_effect(struct input_dev *dev, void *data,
1997 struct ff_effect *effect)
1998{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001999 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002000 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002001
2002 if (effect->type != FF_RUMBLE)
2003 return 0;
2004
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002005 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002006 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002007
Frank Praznik2a242932016-09-22 20:18:09 -04002008 sony_schedule_work(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002009 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002010}
2011
Frank Praznikfa57a812014-04-14 10:11:33 -04002012static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002013{
Frank Praznikfa57a812014-04-14 10:11:33 -04002014 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002015 struct hid_input, list);
2016 struct input_dev *input_dev = hidinput->input;
2017
2018 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
2019 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
2020}
2021
2022#else
Frank Praznikfa57a812014-04-14 10:11:33 -04002023static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002024{
2025 return 0;
2026}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002027
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002028#endif
2029
Frank Praznikd902f472014-01-27 10:17:36 -05002030static int sony_battery_get_property(struct power_supply *psy,
2031 enum power_supply_property psp,
2032 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002033{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002034 struct sony_sc *sc = power_supply_get_drvdata(psy);
Frank Praznikd902f472014-01-27 10:17:36 -05002035 unsigned long flags;
2036 int ret = 0;
2037 u8 battery_charging, battery_capacity, cable_state;
2038
2039 spin_lock_irqsave(&sc->lock, flags);
2040 battery_charging = sc->battery_charging;
2041 battery_capacity = sc->battery_capacity;
2042 cable_state = sc->cable_state;
2043 spin_unlock_irqrestore(&sc->lock, flags);
2044
2045 switch (psp) {
2046 case POWER_SUPPLY_PROP_PRESENT:
2047 val->intval = 1;
2048 break;
2049 case POWER_SUPPLY_PROP_SCOPE:
2050 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
2051 break;
2052 case POWER_SUPPLY_PROP_CAPACITY:
2053 val->intval = battery_capacity;
2054 break;
2055 case POWER_SUPPLY_PROP_STATUS:
2056 if (battery_charging)
2057 val->intval = POWER_SUPPLY_STATUS_CHARGING;
2058 else
2059 if (battery_capacity == 100 && cable_state)
2060 val->intval = POWER_SUPPLY_STATUS_FULL;
2061 else
2062 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
2063 break;
2064 default:
2065 ret = -EINVAL;
2066 break;
2067 }
2068 return ret;
2069}
2070
Frank Praznik0f398232016-09-22 20:18:08 -04002071static int sony_battery_probe(struct sony_sc *sc, int append_dev_id)
Frank Praznikd902f472014-01-27 10:17:36 -05002072{
Frank Praznik0f398232016-09-22 20:18:08 -04002073 const char *battery_str_fmt = append_dev_id ?
2074 "sony_controller_battery_%pMR_%i" :
2075 "sony_controller_battery_%pMR";
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002076 struct power_supply_config psy_cfg = { .drv_data = sc, };
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002077 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05002078 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002079
Frank Praznikad142b92014-02-20 11:36:00 -05002080 /*
2081 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05002082 * if the battery is polled before the first device report is received.
2083 */
2084 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002085
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002086 sc->battery_desc.properties = sony_battery_props;
2087 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
2088 sc->battery_desc.get_property = sony_battery_get_property;
2089 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
2090 sc->battery_desc.use_for_apm = 0;
Frank Praznik0f398232016-09-22 20:18:08 -04002091 sc->battery_desc.name = kasprintf(GFP_KERNEL, battery_str_fmt,
2092 sc->mac_address, sc->device_id);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002093 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05002094 return -ENOMEM;
2095
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002096 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
2097 &psy_cfg);
2098 if (IS_ERR(sc->battery)) {
2099 ret = PTR_ERR(sc->battery);
Frank Praznikd902f472014-01-27 10:17:36 -05002100 hid_err(hdev, "Unable to register battery device\n");
2101 goto err_free;
2102 }
2103
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002104 power_supply_powers(sc->battery, &hdev->dev);
Frank Praznikd902f472014-01-27 10:17:36 -05002105 return 0;
2106
2107err_free:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002108 kfree(sc->battery_desc.name);
2109 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05002110 return ret;
2111}
2112
2113static void sony_battery_remove(struct sony_sc *sc)
2114{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002115 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05002116 return;
2117
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002118 power_supply_unregister(sc->battery);
2119 kfree(sc->battery_desc.name);
2120 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05002121}
2122
Frank Praznikd2d782f2014-02-20 11:36:03 -05002123/*
2124 * If a controller is plugged in via USB while already connected via Bluetooth
2125 * it will show up as two devices. A global list of connected controllers and
2126 * their MAC addresses is maintained to ensure that a device is only connected
2127 * once.
Frank Praznik0f398232016-09-22 20:18:08 -04002128 *
2129 * Some USB-only devices masquerade as Sixaxis controllers and all have the
2130 * same dummy Bluetooth address, so a comparison of the connection type is
2131 * required. Devices are only rejected in the case where two devices have
2132 * matching Bluetooth addresses on different bus types.
Frank Praznikd2d782f2014-02-20 11:36:03 -05002133 */
Frank Praznik0f398232016-09-22 20:18:08 -04002134static inline int sony_compare_connection_type(struct sony_sc *sc0,
2135 struct sony_sc *sc1)
2136{
2137 const int sc0_not_bt = !(sc0->quirks & SONY_BT_DEVICE);
2138 const int sc1_not_bt = !(sc1->quirks & SONY_BT_DEVICE);
2139
2140 return sc0_not_bt == sc1_not_bt;
2141}
2142
Frank Praznikd2d782f2014-02-20 11:36:03 -05002143static int sony_check_add_dev_list(struct sony_sc *sc)
2144{
2145 struct sony_sc *entry;
2146 unsigned long flags;
2147 int ret;
2148
2149 spin_lock_irqsave(&sony_dev_list_lock, flags);
2150
2151 list_for_each_entry(entry, &sony_device_list, list_node) {
2152 ret = memcmp(sc->mac_address, entry->mac_address,
2153 sizeof(sc->mac_address));
2154 if (!ret) {
Frank Praznik0f398232016-09-22 20:18:08 -04002155 if (sony_compare_connection_type(sc, entry)) {
2156 ret = 1;
2157 } else {
2158 ret = -EEXIST;
2159 hid_info(sc->hdev,
2160 "controller with MAC address %pMR already connected\n",
Frank Praznikd2d782f2014-02-20 11:36:03 -05002161 sc->mac_address);
Frank Praznik0f398232016-09-22 20:18:08 -04002162 }
Frank Praznikd2d782f2014-02-20 11:36:03 -05002163 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002164 }
2165 }
2166
Frank Praznikd2d782f2014-02-20 11:36:03 -05002167 ret = 0;
2168 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002169
Frank Praznikd2d782f2014-02-20 11:36:03 -05002170unlock:
2171 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2172 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002173}
2174
Frank Praznikd2d782f2014-02-20 11:36:03 -05002175static void sony_remove_dev_list(struct sony_sc *sc)
2176{
2177 unsigned long flags;
2178
2179 if (sc->list_node.next) {
2180 spin_lock_irqsave(&sony_dev_list_lock, flags);
2181 list_del(&(sc->list_node));
2182 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2183 }
2184}
2185
2186static int sony_get_bt_devaddr(struct sony_sc *sc)
2187{
2188 int ret;
2189
2190 /* HIDP stores the device MAC address as a string in the uniq field. */
2191 ret = strlen(sc->hdev->uniq);
2192 if (ret != 17)
2193 return -EINVAL;
2194
2195 ret = sscanf(sc->hdev->uniq,
2196 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2197 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
2198 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
2199
2200 if (ret != 6)
2201 return -EINVAL;
2202
2203 return 0;
2204}
2205
2206static int sony_check_add(struct sony_sc *sc)
2207{
Pavel Machek1adf9042016-02-09 13:55:08 +01002208 u8 *buf = NULL;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002209 int n, ret;
2210
2211 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
Simon Wood12e9a6d2015-06-09 21:27:05 -06002212 (sc->quirks & MOTION_CONTROLLER_BT) ||
Simon Wood4545ee02015-06-17 00:08:52 -06002213 (sc->quirks & NAVIGATION_CONTROLLER_BT) ||
Frank Praznikd2d782f2014-02-20 11:36:03 -05002214 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
2215 /*
2216 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
2217 * address from the uniq string where HIDP stores it.
2218 * As uniq cannot be guaranteed to be a MAC address in all cases
2219 * a failure of this function should not prevent the connection.
2220 */
2221 if (sony_get_bt_devaddr(sc) < 0) {
2222 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
2223 return 0;
2224 }
2225 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Roderick Colenbrander5443a632016-10-07 12:39:35 -07002226 buf = kmalloc(DS4_FEATURE_REPORT_0x81_SIZE, GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002227 if (!buf)
2228 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002229
2230 /*
2231 * The MAC address of a DS4 controller connected via USB can be
2232 * retrieved with feature report 0x81. The address begins at
2233 * offset 1.
2234 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002235 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
Roderick Colenbrander5443a632016-10-07 12:39:35 -07002236 DS4_FEATURE_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002237 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002238
Roderick Colenbrander5443a632016-10-07 12:39:35 -07002239 if (ret != DS4_FEATURE_REPORT_0x81_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002240 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002241 ret = ret < 0 ? ret : -EINVAL;
2242 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002243 }
2244
2245 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
Simon Wood4545ee02015-06-17 00:08:52 -06002246 } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2247 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002248 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2249 if (!buf)
2250 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002251
2252 /*
2253 * The MAC address of a Sixaxis controller connected via USB can
2254 * be retrieved with feature report 0xf2. The address begins at
2255 * offset 4.
2256 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002257 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2258 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2259 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002260
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002261 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002262 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002263 ret = ret < 0 ? ret : -EINVAL;
2264 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002265 }
2266
2267 /*
2268 * The Sixaxis device MAC in the report is big-endian and must
2269 * be byte-swapped.
2270 */
2271 for (n = 0; n < 6; n++)
2272 sc->mac_address[5-n] = buf[4+n];
2273 } else {
2274 return 0;
2275 }
2276
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002277 ret = sony_check_add_dev_list(sc);
2278
2279out_free:
2280
2281 kfree(buf);
2282
2283 return ret;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002284}
2285
Frank Praznik80250872014-04-14 10:11:35 -04002286static int sony_set_device_id(struct sony_sc *sc)
2287{
2288 int ret;
2289
2290 /*
2291 * Only DualShock 4 or Sixaxis controllers get an id.
2292 * All others are set to -1.
2293 */
2294 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2295 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2296 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2297 GFP_KERNEL);
2298 if (ret < 0) {
2299 sc->device_id = -1;
2300 return ret;
2301 }
2302 sc->device_id = ret;
2303 } else {
2304 sc->device_id = -1;
2305 }
2306
2307 return 0;
2308}
2309
2310static void sony_release_device_id(struct sony_sc *sc)
2311{
2312 if (sc->device_id >= 0) {
2313 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2314 sc->device_id = -1;
2315 }
2316}
2317
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002318static inline void sony_init_output_report(struct sony_sc *sc,
Antonio Ospite09593e32016-02-09 13:55:06 +01002319 void (*send_output_report)(struct sony_sc *))
Frank Praznik46262042014-04-14 10:11:31 -04002320{
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002321 sc->send_output_report = send_output_report;
2322
Frank Praznik46262042014-04-14 10:11:31 -04002323 if (!sc->worker_initialized)
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002324 INIT_WORK(&sc->state_worker, sony_state_worker);
Frank Praznik46262042014-04-14 10:11:31 -04002325
2326 sc->worker_initialized = 1;
2327}
2328
2329static inline void sony_cancel_work_sync(struct sony_sc *sc)
2330{
2331 if (sc->worker_initialized)
2332 cancel_work_sync(&sc->state_worker);
2333}
Frank Praznikd2d782f2014-02-20 11:36:03 -05002334
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002335static int sony_input_configured(struct hid_device *hdev,
2336 struct hid_input *hidinput)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002337{
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002338 struct sony_sc *sc = hid_get_drvdata(hdev);
Frank Praznik0f398232016-09-22 20:18:08 -04002339 int append_dev_id;
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002340 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002341
Frank Praznik80250872014-04-14 10:11:35 -04002342 ret = sony_set_device_id(sc);
2343 if (ret < 0) {
2344 hid_err(hdev, "failed to allocate the device id\n");
2345 goto err_stop;
2346 }
2347
Frank Praznik131a8a92015-05-05 20:47:28 -04002348 ret = sony_allocate_output_report(sc);
2349 if (ret < 0) {
2350 hid_err(hdev, "failed to allocate the output report buffer\n");
2351 goto err_stop;
2352 }
2353
Simon Wood4545ee02015-06-17 00:08:52 -06002354 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2355 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002356 /*
2357 * The Sony Sixaxis does not handle HID Output Reports on the
2358 * Interrupt EP like it could, so we need to force HID Output
2359 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2360 *
2361 * There is also another issue about HID Output Reports via USB,
2362 * the Sixaxis does not want the report_id as part of the data
2363 * packet, so we have to discard buf[0] when sending the actual
2364 * control message, even for numbered reports, humpf!
Frank Praznik2a242932016-09-22 20:18:09 -04002365 *
2366 * Additionally, the Sixaxis on USB isn't properly initialized
2367 * until the PS logo button is pressed and as such won't retain
2368 * any state set by an output report, so the initial
2369 * configuration report is deferred until the first input
2370 * report arrives.
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002371 */
2372 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2373 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
Frank Praznik2a242932016-09-22 20:18:09 -04002374 sc->defer_initialization = 1;
Antonio Ospite816651a2010-05-03 22:15:55 +02002375 ret = sixaxis_set_operational_usb(hdev);
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002376 sony_init_output_report(sc, sixaxis_send_output_report);
Simon Wood4545ee02015-06-17 00:08:52 -06002377 } else if ((sc->quirks & SIXAXIS_CONTROLLER_BT) ||
2378 (sc->quirks & NAVIGATION_CONTROLLER_BT)) {
Frank Praznik2078b9b2014-03-15 09:41:16 -04002379 /*
2380 * The Sixaxis wants output reports sent on the ctrl endpoint
2381 * when connected via Bluetooth.
2382 */
2383 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Antonio Ospite816651a2010-05-03 22:15:55 +02002384 ret = sixaxis_set_operational_bt(hdev);
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002385 sony_init_output_report(sc, sixaxis_send_output_report);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002386 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik68330d82014-02-05 20:03:49 -05002387 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
2388 ret = dualshock4_set_operational_bt(hdev);
2389 if (ret < 0) {
2390 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
2391 goto err_stop;
2392 }
2393 }
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002394
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002395 /*
2396 * The Dualshock 4 touchpad supports 2 touches and has a
2397 * resolution of 1920x942 (44.86 dots/mm).
2398 */
2399 ret = sony_register_touchpad(hidinput, 2, 1920, 942);
2400 if (ret) {
2401 hid_err(sc->hdev,
2402 "Unable to initialize multi-touch slots: %d\n",
2403 ret);
2404 return ret;
2405 }
2406
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002407 sony_init_output_report(sc, dualshock4_send_output_report);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002408 } else if (sc->quirks & MOTION_CONTROLLER) {
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002409 sony_init_output_report(sc, motion_send_output_report);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002410 } else {
2411 ret = 0;
2412 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00002413
Jiri Kosina4dfdc462008-12-30 00:49:59 +01002414 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002415 goto err_stop;
2416
Frank Praznik0f398232016-09-22 20:18:08 -04002417 ret = append_dev_id = sony_check_add(sc);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002418 if (ret < 0)
2419 goto err_stop;
2420
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002421 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002422 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002423 if (ret < 0)
2424 goto err_stop;
2425 }
2426
Frank Praznikd902f472014-01-27 10:17:36 -05002427 if (sc->quirks & SONY_BATTERY_SUPPORT) {
Frank Praznik0f398232016-09-22 20:18:08 -04002428 ret = sony_battery_probe(sc, append_dev_id);
Frank Praznikd902f472014-01-27 10:17:36 -05002429 if (ret < 0)
2430 goto err_stop;
2431
2432 /* Open the device to receive reports with battery info */
2433 ret = hid_hw_open(hdev);
2434 if (ret < 0) {
2435 hid_err(hdev, "hw open failed\n");
2436 goto err_stop;
2437 }
2438 }
2439
Frank Praznikc8de9db2014-02-20 11:36:01 -05002440 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002441 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05002442 if (ret < 0)
2443 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05002444 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002445
Jiri Slabybd28ce02008-06-25 23:47:04 +02002446 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05002447err_close:
2448 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002449err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002450 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002451 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002452 if (sc->quirks & SONY_BATTERY_SUPPORT)
2453 sony_battery_remove(sc);
Frank Praznik46262042014-04-14 10:11:31 -04002454 sony_cancel_work_sync(sc);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002455 kfree(sc->output_report_dmabuf);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002456 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04002457 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002458 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002459 return ret;
2460}
2461
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002462static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2463{
2464 int ret;
2465 unsigned long quirks = id->driver_data;
2466 struct sony_sc *sc;
2467 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2468
2469 if (!strcmp(hdev->name, "FutureMax Dance Mat"))
2470 quirks |= FUTUREMAX_DANCE_MAT;
2471
2472 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
2473 if (sc == NULL) {
2474 hid_err(hdev, "can't alloc sony descriptor\n");
2475 return -ENOMEM;
2476 }
2477
2478 spin_lock_init(&sc->lock);
2479
2480 sc->quirks = quirks;
2481 hid_set_drvdata(hdev, sc);
2482 sc->hdev = hdev;
2483
2484 ret = hid_parse(hdev);
2485 if (ret) {
2486 hid_err(hdev, "parse failed\n");
2487 return ret;
2488 }
2489
2490 if (sc->quirks & VAIO_RDESC_CONSTANT)
2491 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2492 else if (sc->quirks & SIXAXIS_CONTROLLER)
2493 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2494
2495 ret = hid_hw_start(hdev, connect_mask);
2496 if (ret) {
2497 hid_err(hdev, "hw start failed\n");
2498 return ret;
2499 }
2500
2501 return ret;
2502}
2503
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002504static void sony_remove(struct hid_device *hdev)
2505{
Colin Leitnerf04d5142013-05-27 23:41:05 +02002506 struct sony_sc *sc = hid_get_drvdata(hdev);
2507
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002508 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002509 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02002510
Frank Praznikd902f472014-01-27 10:17:36 -05002511 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2512 hid_hw_close(hdev);
2513 sony_battery_remove(sc);
2514 }
2515
Frank Praznik46262042014-04-14 10:11:31 -04002516 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002517
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002518 kfree(sc->output_report_dmabuf);
2519
Frank Praznikd2d782f2014-02-20 11:36:03 -05002520 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002521
Frank Praznik80250872014-04-14 10:11:35 -04002522 sony_release_device_id(sc);
2523
Jiri Slabybd28ce02008-06-25 23:47:04 +02002524 hid_hw_stop(hdev);
2525}
2526
Frank Praznikdecd9462015-11-11 09:49:38 -05002527#ifdef CONFIG_PM
2528
2529static int sony_suspend(struct hid_device *hdev, pm_message_t message)
2530{
Frank Praznike6061c12017-02-08 13:58:43 -05002531#ifdef CONFIG_SONY_FF
2532
2533 /* On suspend stop any running force-feedback events */
2534 if (SONY_FF_SUPPORT) {
Frank Praznikdecd9462015-11-11 09:49:38 -05002535 struct sony_sc *sc = hid_get_drvdata(hdev);
2536
Frank Praznikdecd9462015-11-11 09:49:38 -05002537 sc->left = sc->right = 0;
Frank Praznikdecd9462015-11-11 09:49:38 -05002538 sony_send_output_report(sc);
2539 }
2540
Frank Praznike6061c12017-02-08 13:58:43 -05002541#endif
Frank Praznikdecd9462015-11-11 09:49:38 -05002542 return 0;
2543}
2544
2545static int sony_resume(struct hid_device *hdev)
2546{
Frank Praznike6061c12017-02-08 13:58:43 -05002547 struct sony_sc *sc = hid_get_drvdata(hdev);
Frank Praznikdecd9462015-11-11 09:49:38 -05002548
Frank Praznike6061c12017-02-08 13:58:43 -05002549 /*
2550 * The Sixaxis and navigation controllers on USB need to be
2551 * reinitialized on resume or they won't behave properly.
2552 */
2553 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2554 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
2555 sixaxis_set_operational_usb(sc->hdev);
2556 sc->defer_initialization = 1;
Frank Praznikdecd9462015-11-11 09:49:38 -05002557 }
2558
2559 return 0;
2560}
2561
2562#endif
2563
Jiri Slabybd28ce02008-06-25 23:47:04 +02002564static const struct hid_device_id sony_devices[] = {
2565 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2566 .driver_data = SIXAXIS_CONTROLLER_USB },
2567 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
Simon Wood4545ee02015-06-17 00:08:52 -06002568 .driver_data = NAVIGATION_CONTROLLER_USB },
Simon Wood6eabaaa2015-06-17 00:08:51 -06002569 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
Simon Wood4545ee02015-06-17 00:08:52 -06002570 .driver_data = NAVIGATION_CONTROLLER_BT },
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002571 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002572 .driver_data = MOTION_CONTROLLER_USB },
Simon Wooda4afa852015-06-03 09:45:19 -06002573 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002574 .driver_data = MOTION_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002575 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2576 .driver_data = SIXAXIS_CONTROLLER_BT },
2577 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2578 .driver_data = VAIO_RDESC_CONSTANT },
2579 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2580 .driver_data = VAIO_RDESC_CONSTANT },
Antonio Ospiteef916ef2016-02-09 13:55:07 +01002581 /*
2582 * Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2583 * Logitech joystick from the device descriptor.
2584 */
Jiri Slabybd28ce02008-06-25 23:47:04 +02002585 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2586 .driver_data = BUZZ_CONTROLLER },
2587 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2588 .driver_data = BUZZ_CONTROLLER },
2589 /* PS3 BD Remote Control */
2590 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2591 .driver_data = PS3REMOTE },
2592 /* Logitech Harmony Adapter for PS3 */
2593 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2594 .driver_data = PS3REMOTE },
Frank Praznik68a49e52014-11-12 14:52:28 -05002595 /* SMK-Link PS3 BD Remote Control */
2596 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2597 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002598 /* Sony Dualshock 4 controllers for PS4 */
2599 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002600 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002601 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002602 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Roderick Colenbranderf5ff43c2016-10-07 12:39:40 -07002603 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
2604 .driver_data = DUALSHOCK4_CONTROLLER_USB },
2605 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
2606 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Roderick Colenbrander72386b22016-11-23 14:07:11 -08002607 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE),
2608 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Scott Moreau74500cc2016-01-13 07:40:42 -07002609 /* Nyko Core Controller for PS3 */
2610 { HID_USB_DEVICE(USB_VENDOR_ID_SINO_LITE, USB_DEVICE_ID_SINO_LITE_CONTROLLER),
2611 .driver_data = SIXAXIS_CONTROLLER_USB | SINO_LITE_CONTROLLER },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002612 { }
2613};
2614MODULE_DEVICE_TABLE(hid, sony_devices);
2615
2616static struct hid_driver sony_driver = {
Frank Praznikce8efc32014-09-18 21:15:01 -04002617 .name = "sony",
2618 .id_table = sony_devices,
2619 .input_mapping = sony_mapping,
2620 .input_configured = sony_input_configured,
2621 .probe = sony_probe,
2622 .remove = sony_remove,
2623 .report_fixup = sony_report_fixup,
Frank Praznikdecd9462015-11-11 09:49:38 -05002624 .raw_event = sony_raw_event,
2625
2626#ifdef CONFIG_PM
2627 .suspend = sony_suspend,
2628 .resume = sony_resume,
2629 .reset_resume = sony_resume,
2630#endif
Jiri Slabybd28ce02008-06-25 23:47:04 +02002631};
Frank Praznik80250872014-04-14 10:11:35 -04002632
2633static int __init sony_init(void)
2634{
2635 dbg_hid("Sony:%s\n", __func__);
2636
2637 return hid_register_driver(&sony_driver);
2638}
2639
2640static void __exit sony_exit(void)
2641{
2642 dbg_hid("Sony:%s\n", __func__);
2643
Frank Praznik80250872014-04-14 10:11:35 -04002644 hid_unregister_driver(&sony_driver);
Antonio Ospite6c400652015-02-16 22:58:24 +01002645 ida_destroy(&sony_device_id_allocator);
Frank Praznik80250872014-04-14 10:11:35 -04002646}
2647module_init(sony_init);
2648module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002649
2650MODULE_LICENSE("GPL");