blob: 98b1b7550745967def12d59eb13243aca1a4e712 [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 Colenbrander49b9ca62016-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)
Roderick Colenbrander35f436c2017-03-07 15:45:04 -080051#define DUALSHOCK4_DONGLE BIT(7)
52#define MOTION_CONTROLLER_USB BIT(8)
53#define MOTION_CONTROLLER_BT BIT(9)
54#define NAVIGATION_CONTROLLER_USB BIT(10)
55#define NAVIGATION_CONTROLLER_BT BIT(11)
56#define SINO_LITE_CONTROLLER BIT(12)
57#define FUTUREMAX_DANCE_MAT BIT(13)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020058
Frank Praznikfee4e2d2014-02-18 17:22:01 -050059#define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
Simon Woodb3bca322015-06-09 21:27:04 -060060#define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
Simon Wood4545ee02015-06-17 00:08:52 -060061#define NAVIGATION_CONTROLLER (NAVIGATION_CONTROLLER_USB |\
62 NAVIGATION_CONTROLLER_BT)
Frank Praznik68330d82014-02-05 20:03:49 -050063#define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
Roderick Colenbrander35f436c2017-03-07 15:45:04 -080064 DUALSHOCK4_CONTROLLER_BT | \
65 DUALSHOCK4_DONGLE)
Frank Praznikfee4e2d2014-02-18 17:22:01 -050066#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
Simon Wood4545ee02015-06-17 00:08:52 -060067 DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER |\
68 NAVIGATION_CONTROLLER)
Simon Wood12e9a6d72015-06-09 21:27:05 -060069#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
Simon Wood4545ee02015-06-17 00:08:52 -060070 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040071#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
72 MOTION_CONTROLLER)
Frank Praznik0f398232016-09-22 20:18:08 -040073#define SONY_BT_DEVICE (SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_BT |\
74 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER_BT)
Frank Praznik60781cf2014-01-11 15:13:15 -050075
76#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010077
Frank Praznik4c3e8292015-05-05 20:47:33 -040078/*
79 * The Sixaxis reports both digital and analog values for each button on the
80 * controller except for Start, Select and the PS button. The controller ends
81 * up reporting 27 axes which causes them to spill over into the multi-touch
82 * axis values. Additionally, the controller only has 20 actual, physical axes
83 * so there are several unused axes in between the used ones.
84 */
Pavel Machek1adf9042016-02-09 13:55:08 +010085static u8 sixaxis_rdesc[] = {
Antonio Ospitefb705a62014-06-24 13:28:42 +020086 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznik4c3e8292015-05-05 20:47:33 -040087 0x09, 0x04, /* Usage (Joystick), */
Antonio Ospitefb705a62014-06-24 13:28:42 +020088 0xA1, 0x01, /* Collection (Application), */
89 0xA1, 0x02, /* Collection (Logical), */
90 0x85, 0x01, /* Report ID (1), */
91 0x75, 0x08, /* Report Size (8), */
92 0x95, 0x01, /* Report Count (1), */
93 0x15, 0x00, /* Logical Minimum (0), */
94 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
95 0x81, 0x03, /* Input (Constant, Variable), */
96 0x75, 0x01, /* Report Size (1), */
97 0x95, 0x13, /* Report Count (19), */
98 0x15, 0x00, /* Logical Minimum (0), */
99 0x25, 0x01, /* Logical Maximum (1), */
100 0x35, 0x00, /* Physical Minimum (0), */
101 0x45, 0x01, /* Physical Maximum (1), */
102 0x05, 0x09, /* Usage Page (Button), */
103 0x19, 0x01, /* Usage Minimum (01h), */
104 0x29, 0x13, /* Usage Maximum (13h), */
105 0x81, 0x02, /* Input (Variable), */
106 0x75, 0x01, /* Report Size (1), */
107 0x95, 0x0D, /* Report Count (13), */
108 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
109 0x81, 0x03, /* Input (Constant, Variable), */
110 0x15, 0x00, /* Logical Minimum (0), */
111 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
112 0x05, 0x01, /* Usage Page (Desktop), */
113 0x09, 0x01, /* Usage (Pointer), */
114 0xA1, 0x00, /* Collection (Physical), */
115 0x75, 0x08, /* Report Size (8), */
116 0x95, 0x04, /* Report Count (4), */
117 0x35, 0x00, /* Physical Minimum (0), */
118 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
119 0x09, 0x30, /* Usage (X), */
120 0x09, 0x31, /* Usage (Y), */
121 0x09, 0x32, /* Usage (Z), */
122 0x09, 0x35, /* Usage (Rz), */
123 0x81, 0x02, /* Input (Variable), */
124 0xC0, /* End Collection, */
125 0x05, 0x01, /* Usage Page (Desktop), */
126 0x95, 0x13, /* Report Count (19), */
127 0x09, 0x01, /* Usage (Pointer), */
128 0x81, 0x02, /* Input (Variable), */
129 0x95, 0x0C, /* Report Count (12), */
130 0x81, 0x01, /* Input (Constant), */
131 0x75, 0x10, /* Report Size (16), */
132 0x95, 0x04, /* Report Count (4), */
133 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
134 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
135 0x09, 0x01, /* Usage (Pointer), */
136 0x81, 0x02, /* Input (Variable), */
137 0xC0, /* End Collection, */
138 0xA1, 0x02, /* Collection (Logical), */
139 0x85, 0x02, /* Report ID (2), */
140 0x75, 0x08, /* Report Size (8), */
141 0x95, 0x30, /* Report Count (48), */
142 0x09, 0x01, /* Usage (Pointer), */
143 0xB1, 0x02, /* Feature (Variable), */
144 0xC0, /* End Collection, */
145 0xA1, 0x02, /* Collection (Logical), */
146 0x85, 0xEE, /* Report ID (238), */
147 0x75, 0x08, /* Report Size (8), */
148 0x95, 0x30, /* Report Count (48), */
149 0x09, 0x01, /* Usage (Pointer), */
150 0xB1, 0x02, /* Feature (Variable), */
151 0xC0, /* End Collection, */
152 0xA1, 0x02, /* Collection (Logical), */
153 0x85, 0xEF, /* Report ID (239), */
154 0x75, 0x08, /* Report Size (8), */
155 0x95, 0x30, /* Report Count (48), */
156 0x09, 0x01, /* Usage (Pointer), */
157 0xB1, 0x02, /* Feature (Variable), */
158 0xC0, /* End Collection, */
159 0xC0 /* End Collection */
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200160};
161
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400162/* PS/3 Motion controller */
Pavel Machek1adf9042016-02-09 13:55:08 +0100163static u8 motion_rdesc[] = {
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400164 0x05, 0x01, /* Usage Page (Desktop), */
165 0x09, 0x04, /* Usage (Joystick), */
166 0xA1, 0x01, /* Collection (Application), */
167 0xA1, 0x02, /* Collection (Logical), */
168 0x85, 0x01, /* Report ID (1), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400169 0x75, 0x01, /* Report Size (1), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600170 0x95, 0x15, /* Report Count (21), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400171 0x15, 0x00, /* Logical Minimum (0), */
172 0x25, 0x01, /* Logical Maximum (1), */
173 0x35, 0x00, /* Physical Minimum (0), */
174 0x45, 0x01, /* Physical Maximum (1), */
175 0x05, 0x09, /* Usage Page (Button), */
176 0x19, 0x01, /* Usage Minimum (01h), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600177 0x29, 0x15, /* Usage Maximum (15h), */
178 0x81, 0x02, /* Input (Variable), * Buttons */
179 0x95, 0x0B, /* Report Count (11), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400180 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600181 0x81, 0x03, /* Input (Constant, Variable), * Padding */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400182 0x15, 0x00, /* Logical Minimum (0), */
183 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
184 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400185 0xA1, 0x00, /* Collection (Physical), */
186 0x75, 0x08, /* Report Size (8), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600187 0x95, 0x01, /* Report Count (1), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400188 0x35, 0x00, /* Physical Minimum (0), */
189 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
190 0x09, 0x30, /* Usage (X), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600191 0x81, 0x02, /* Input (Variable), * Trigger */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400192 0xC0, /* End Collection, */
Simon Wood8b2513c2015-06-09 21:27:07 -0600193 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
194 0x75, 0x08, /* Report Size (8), */
195 0x95, 0x07, /* Report Count (7), * skip 7 bytes */
196 0x81, 0x02, /* Input (Variable), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400197 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400198 0x75, 0x10, /* Report Size (16), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600199 0x46, 0xFF, 0xFF, /* Physical Maximum (65535), */
200 0x27, 0xFF, 0xFF, 0x00, 0x00, /* Logical Maximum (65535), */
201 0x95, 0x03, /* Report Count (3), * 3x Accels */
202 0x09, 0x33, /* Usage (rX), */
203 0x09, 0x34, /* Usage (rY), */
204 0x09, 0x35, /* Usage (rZ), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400205 0x81, 0x02, /* Input (Variable), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600206 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
207 0x95, 0x03, /* Report Count (3), * Skip Accels 2nd frame */
208 0x81, 0x02, /* Input (Variable), */
209 0x05, 0x01, /* Usage Page (Desktop), */
210 0x09, 0x01, /* Usage (Pointer), */
211 0x95, 0x03, /* Report Count (3), * 3x Gyros */
212 0x81, 0x02, /* Input (Variable), */
213 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
214 0x95, 0x03, /* Report Count (3), * Skip Gyros 2nd frame */
215 0x81, 0x02, /* Input (Variable), */
216 0x75, 0x0C, /* Report Size (12), */
217 0x46, 0xFF, 0x0F, /* Physical Maximum (4095), */
218 0x26, 0xFF, 0x0F, /* Logical Maximum (4095), */
219 0x95, 0x04, /* Report Count (4), * Skip Temp and Magnetometers */
220 0x81, 0x02, /* Input (Variable), */
221 0x75, 0x08, /* Report Size (8), */
222 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
223 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
224 0x95, 0x06, /* Report Count (6), * Skip Timestamp and Extension Bytes */
225 0x81, 0x02, /* Input (Variable), */
226 0x75, 0x08, /* Report Size (8), */
227 0x95, 0x30, /* Report Count (48), */
228 0x09, 0x01, /* Usage (Pointer), */
229 0x91, 0x02, /* Output (Variable), */
230 0x75, 0x08, /* Report Size (8), */
231 0x95, 0x30, /* Report Count (48), */
232 0x09, 0x01, /* Usage (Pointer), */
233 0xB1, 0x02, /* Feature (Variable), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400234 0xC0, /* End Collection, */
235 0xA1, 0x02, /* Collection (Logical), */
236 0x85, 0x02, /* Report ID (2), */
237 0x75, 0x08, /* Report Size (8), */
238 0x95, 0x30, /* Report Count (48), */
239 0x09, 0x01, /* Usage (Pointer), */
240 0xB1, 0x02, /* Feature (Variable), */
241 0xC0, /* End Collection, */
242 0xA1, 0x02, /* Collection (Logical), */
243 0x85, 0xEE, /* Report ID (238), */
244 0x75, 0x08, /* Report Size (8), */
245 0x95, 0x30, /* Report Count (48), */
246 0x09, 0x01, /* Usage (Pointer), */
247 0xB1, 0x02, /* Feature (Variable), */
248 0xC0, /* End Collection, */
249 0xA1, 0x02, /* Collection (Logical), */
250 0x85, 0xEF, /* Report ID (239), */
251 0x75, 0x08, /* Report Size (8), */
252 0x95, 0x30, /* Report Count (48), */
253 0x09, 0x01, /* Usage (Pointer), */
254 0xB1, 0x02, /* Feature (Variable), */
255 0xC0, /* End Collection, */
256 0xC0 /* End Collection */
257};
258
Simon Woodb2723eb2015-06-17 00:08:53 -0600259/* PS/3 Navigation controller */
Pavel Machek1adf9042016-02-09 13:55:08 +0100260static u8 navigation_rdesc[] = {
Simon Woodb2723eb2015-06-17 00:08:53 -0600261 0x05, 0x01, /* Usage Page (Desktop), */
Antonio Ospited5421762016-01-28 18:23:43 +0100262 0x09, 0x04, /* Usage (Joystick), */
Simon Woodb2723eb2015-06-17 00:08:53 -0600263 0xA1, 0x01, /* Collection (Application), */
264 0xA1, 0x02, /* Collection (Logical), */
265 0x85, 0x01, /* Report ID (1), */
266 0x75, 0x08, /* Report Size (8), */
267 0x95, 0x01, /* Report Count (1), */
268 0x15, 0x00, /* Logical Minimum (0), */
269 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
270 0x81, 0x03, /* Input (Constant, Variable), */
271 0x75, 0x01, /* Report Size (1), */
272 0x95, 0x13, /* Report Count (19), */
273 0x15, 0x00, /* Logical Minimum (0), */
274 0x25, 0x01, /* Logical Maximum (1), */
275 0x35, 0x00, /* Physical Minimum (0), */
276 0x45, 0x01, /* Physical Maximum (1), */
277 0x05, 0x09, /* Usage Page (Button), */
278 0x19, 0x01, /* Usage Minimum (01h), */
279 0x29, 0x13, /* Usage Maximum (13h), */
280 0x81, 0x02, /* Input (Variable), */
281 0x75, 0x01, /* Report Size (1), */
282 0x95, 0x0D, /* Report Count (13), */
283 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
284 0x81, 0x03, /* Input (Constant, Variable), */
285 0x15, 0x00, /* Logical Minimum (0), */
286 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
287 0x05, 0x01, /* Usage Page (Desktop), */
288 0x09, 0x01, /* Usage (Pointer), */
289 0xA1, 0x00, /* Collection (Physical), */
290 0x75, 0x08, /* Report Size (8), */
291 0x95, 0x02, /* Report Count (2), */
292 0x35, 0x00, /* Physical Minimum (0), */
293 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
294 0x09, 0x30, /* Usage (X), */
295 0x09, 0x31, /* Usage (Y), */
296 0x81, 0x02, /* Input (Variable), */
297 0xC0, /* End Collection, */
298 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
299 0x95, 0x06, /* Report Count (6), */
300 0x81, 0x03, /* Input (Constant, Variable), */
301 0x05, 0x01, /* Usage Page (Desktop), */
302 0x75, 0x08, /* Report Size (8), */
303 0x95, 0x05, /* Report Count (5), */
304 0x09, 0x01, /* Usage (Pointer), */
305 0x81, 0x02, /* Input (Variable), */
306 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
Simon Wood2259b5bb2015-07-10 00:10:21 -0600307 0x95, 0x01, /* Report Count (1), */
308 0x81, 0x02, /* Input (Variable), */
309 0x05, 0x01, /* Usage Page (Desktop), */
310 0x95, 0x01, /* Report Count (1), */
311 0x09, 0x01, /* Usage (Pointer), */
312 0x81, 0x02, /* Input (Variable), */
313 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
314 0x95, 0x1E, /* Report Count (24), */
Simon Woodb2723eb2015-06-17 00:08:53 -0600315 0x81, 0x02, /* Input (Variable), */
316 0x75, 0x08, /* Report Size (8), */
317 0x95, 0x30, /* Report Count (48), */
318 0x09, 0x01, /* Usage (Pointer), */
319 0x91, 0x02, /* Output (Variable), */
320 0x75, 0x08, /* Report Size (8), */
321 0x95, 0x30, /* Report Count (48), */
322 0x09, 0x01, /* Usage (Pointer), */
323 0xB1, 0x02, /* Feature (Variable), */
324 0xC0, /* End Collection, */
325 0xA1, 0x02, /* Collection (Logical), */
326 0x85, 0x02, /* Report ID (2), */
327 0x75, 0x08, /* Report Size (8), */
328 0x95, 0x30, /* Report Count (48), */
329 0x09, 0x01, /* Usage (Pointer), */
330 0xB1, 0x02, /* Feature (Variable), */
331 0xC0, /* End Collection, */
332 0xA1, 0x02, /* Collection (Logical), */
333 0x85, 0xEE, /* Report ID (238), */
334 0x75, 0x08, /* Report Size (8), */
335 0x95, 0x30, /* Report Count (48), */
336 0x09, 0x01, /* Usage (Pointer), */
337 0xB1, 0x02, /* Feature (Variable), */
338 0xC0, /* End Collection, */
339 0xA1, 0x02, /* Collection (Logical), */
340 0x85, 0xEF, /* Report ID (239), */
341 0x75, 0x08, /* Report Size (8), */
342 0x95, 0x30, /* Report Count (48), */
343 0x09, 0x01, /* Usage (Pointer), */
344 0xB1, 0x02, /* Feature (Variable), */
345 0xC0, /* End Collection, */
346 0xC0 /* End Collection */
347};
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400348
Pavel Machek1adf9042016-02-09 13:55:08 +0100349static u8 ps3remote_rdesc[] = {
Jiri Kosina078328d2013-06-13 12:03:49 +0200350 0x05, 0x01, /* GUsagePage Generic Desktop */
351 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
352 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
353
354 /* Use collection 1 for joypad buttons */
355 0xA1, 0x02, /* MCollection Logical (interrelated data) */
356
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100357 /*
358 * Ignore the 1st byte, maybe it is used for a controller
359 * number but it's not needed for correct operation
360 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200361 0x75, 0x08, /* GReportSize 0x08 [8] */
362 0x95, 0x01, /* GReportCount 0x01 [1] */
363 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
364
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100365 /*
366 * Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
367 * buttons multiple keypresses are allowed
368 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200369 0x05, 0x09, /* GUsagePage Button */
370 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
371 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
372 0x14, /* GLogicalMinimum [0] */
373 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
374 0x75, 0x01, /* GReportSize 0x01 [1] */
375 0x95, 0x18, /* GReportCount 0x18 [24] */
376 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
377
378 0xC0, /* MEndCollection */
379
380 /* Use collection 2 for remote control buttons */
381 0xA1, 0x02, /* MCollection Logical (interrelated data) */
382
383 /* 5th byte is used for remote control buttons */
384 0x05, 0x09, /* GUsagePage Button */
385 0x18, /* LUsageMinimum [No button pressed] */
386 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
387 0x14, /* GLogicalMinimum [0] */
388 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
389 0x75, 0x08, /* GReportSize 0x08 [8] */
390 0x95, 0x01, /* GReportCount 0x01 [1] */
391 0x80, /* MInput */
392
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100393 /*
394 * Ignore bytes from 6th to 11th, 6th to 10th are always constant at
395 * 0xff and 11th is for press indication
396 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200397 0x75, 0x08, /* GReportSize 0x08 [8] */
398 0x95, 0x06, /* GReportCount 0x06 [6] */
399 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
400
401 /* 12th byte is for battery strength */
402 0x05, 0x06, /* GUsagePage Generic Device Controls */
403 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
404 0x14, /* GLogicalMinimum [0] */
405 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
406 0x75, 0x08, /* GReportSize 0x08 [8] */
407 0x95, 0x01, /* GReportCount 0x01 [1] */
408 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
409
410 0xC0, /* MEndCollection */
411
412 0xC0 /* MEndCollection [Game Pad] */
413};
414
415static const unsigned int ps3remote_keymap_joypad_buttons[] = {
416 [0x01] = KEY_SELECT,
417 [0x02] = BTN_THUMBL, /* L3 */
418 [0x03] = BTN_THUMBR, /* R3 */
419 [0x04] = BTN_START,
420 [0x05] = KEY_UP,
421 [0x06] = KEY_RIGHT,
422 [0x07] = KEY_DOWN,
423 [0x08] = KEY_LEFT,
424 [0x09] = BTN_TL2, /* L2 */
425 [0x0a] = BTN_TR2, /* R2 */
426 [0x0b] = BTN_TL, /* L1 */
427 [0x0c] = BTN_TR, /* R1 */
428 [0x0d] = KEY_OPTION, /* options/triangle */
429 [0x0e] = KEY_BACK, /* back/circle */
430 [0x0f] = BTN_0, /* cross */
431 [0x10] = KEY_SCREEN, /* view/square */
432 [0x11] = KEY_HOMEPAGE, /* PS button */
433 [0x14] = KEY_ENTER,
434};
435static const unsigned int ps3remote_keymap_remote_buttons[] = {
436 [0x00] = KEY_1,
437 [0x01] = KEY_2,
438 [0x02] = KEY_3,
439 [0x03] = KEY_4,
440 [0x04] = KEY_5,
441 [0x05] = KEY_6,
442 [0x06] = KEY_7,
443 [0x07] = KEY_8,
444 [0x08] = KEY_9,
445 [0x09] = KEY_0,
446 [0x0e] = KEY_ESC, /* return */
447 [0x0f] = KEY_CLEAR,
448 [0x16] = KEY_EJECTCD,
449 [0x1a] = KEY_MENU, /* top menu */
450 [0x28] = KEY_TIME,
451 [0x30] = KEY_PREVIOUS,
452 [0x31] = KEY_NEXT,
453 [0x32] = KEY_PLAY,
454 [0x33] = KEY_REWIND, /* scan back */
455 [0x34] = KEY_FORWARD, /* scan forward */
456 [0x38] = KEY_STOP,
457 [0x39] = KEY_PAUSE,
458 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
459 [0x60] = KEY_FRAMEBACK, /* slow/step back */
460 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
461 [0x63] = KEY_SUBTITLE,
462 [0x64] = KEY_AUDIO,
463 [0x65] = KEY_ANGLE,
464 [0x70] = KEY_INFO, /* display */
465 [0x80] = KEY_BLUE,
466 [0x81] = KEY_RED,
467 [0x82] = KEY_GREEN,
468 [0x83] = KEY_YELLOW,
469};
470
Colin Leitnerf04d5142013-05-27 23:41:05 +0200471static const unsigned int buzz_keymap[] = {
Frank Praznikad142b92014-02-20 11:36:00 -0500472 /*
473 * The controller has 4 remote buzzers, each with one LED and 5
Colin Leitnerf04d5142013-05-27 23:41:05 +0200474 * buttons.
Antonio Ospite09593e32016-02-09 13:55:06 +0100475 *
Colin Leitnerf04d5142013-05-27 23:41:05 +0200476 * We use the mapping chosen by the controller, which is:
477 *
478 * Key Offset
479 * -------------------
480 * Buzz 1
481 * Blue 5
482 * Orange 4
483 * Green 3
484 * Yellow 2
485 *
486 * So, for example, the orange button on the third buzzer is mapped to
487 * BTN_TRIGGER_HAPPY14
488 */
Antonio Ospite09593e32016-02-09 13:55:06 +0100489 [1] = BTN_TRIGGER_HAPPY1,
490 [2] = BTN_TRIGGER_HAPPY2,
491 [3] = BTN_TRIGGER_HAPPY3,
492 [4] = BTN_TRIGGER_HAPPY4,
493 [5] = BTN_TRIGGER_HAPPY5,
494 [6] = BTN_TRIGGER_HAPPY6,
495 [7] = BTN_TRIGGER_HAPPY7,
496 [8] = BTN_TRIGGER_HAPPY8,
497 [9] = BTN_TRIGGER_HAPPY9,
Colin Leitnerf04d5142013-05-27 23:41:05 +0200498 [10] = BTN_TRIGGER_HAPPY10,
499 [11] = BTN_TRIGGER_HAPPY11,
500 [12] = BTN_TRIGGER_HAPPY12,
501 [13] = BTN_TRIGGER_HAPPY13,
502 [14] = BTN_TRIGGER_HAPPY14,
503 [15] = BTN_TRIGGER_HAPPY15,
504 [16] = BTN_TRIGGER_HAPPY16,
505 [17] = BTN_TRIGGER_HAPPY17,
506 [18] = BTN_TRIGGER_HAPPY18,
507 [19] = BTN_TRIGGER_HAPPY19,
508 [20] = BTN_TRIGGER_HAPPY20,
509};
510
Roderick Colenbrander9131f8c2016-11-23 14:07:08 -0800511static const unsigned int ds4_absmap[] = {
512 [0x30] = ABS_X,
513 [0x31] = ABS_Y,
514 [0x32] = ABS_RX, /* right stick X */
515 [0x33] = ABS_Z, /* L2 */
516 [0x34] = ABS_RZ, /* R2 */
517 [0x35] = ABS_RY, /* right stick Y */
518};
519
520static const unsigned int ds4_keymap[] = {
521 [0x1] = BTN_WEST, /* Square */
522 [0x2] = BTN_SOUTH, /* Cross */
523 [0x3] = BTN_EAST, /* Circle */
524 [0x4] = BTN_NORTH, /* Triangle */
525 [0x5] = BTN_TL, /* L1 */
526 [0x6] = BTN_TR, /* R1 */
527 [0x7] = BTN_TL2, /* L2 */
528 [0x8] = BTN_TR2, /* R2 */
529 [0x9] = BTN_SELECT, /* Share */
530 [0xa] = BTN_START, /* Options */
531 [0xb] = BTN_THUMBL, /* L3 */
532 [0xc] = BTN_THUMBR, /* R3 */
533 [0xd] = BTN_MODE, /* PS */
534};
535
Roderick Colenbranderd03ae2e2017-03-07 15:45:03 -0800536static const struct {int x; int y; } ds4_hat_mapping[] = {
537 {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1},
538 {0, 0}
539};
Roderick Colenbrander9131f8c2016-11-23 14:07:08 -0800540
Frank Praznikd902f472014-01-27 10:17:36 -0500541static enum power_supply_property sony_battery_props[] = {
542 POWER_SUPPLY_PROP_PRESENT,
543 POWER_SUPPLY_PROP_CAPACITY,
544 POWER_SUPPLY_PROP_SCOPE,
545 POWER_SUPPLY_PROP_STATUS,
546};
547
Frank Praznik55d3b662014-04-14 10:11:32 -0400548struct sixaxis_led {
Pavel Machek1adf9042016-02-09 13:55:08 +0100549 u8 time_enabled; /* the total time the led is active (0xff means forever) */
550 u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
551 u8 enabled;
552 u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
553 u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
Frank Praznik55d3b662014-04-14 10:11:32 -0400554} __packed;
555
556struct sixaxis_rumble {
Pavel Machek1adf9042016-02-09 13:55:08 +0100557 u8 padding;
558 u8 right_duration; /* Right motor duration (0xff means forever) */
559 u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
560 u8 left_duration; /* Left motor duration (0xff means forever) */
561 u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
Frank Praznik55d3b662014-04-14 10:11:32 -0400562} __packed;
563
564struct sixaxis_output_report {
Pavel Machek1adf9042016-02-09 13:55:08 +0100565 u8 report_id;
Frank Praznik55d3b662014-04-14 10:11:32 -0400566 struct sixaxis_rumble rumble;
Pavel Machek1adf9042016-02-09 13:55:08 +0100567 u8 padding[4];
568 u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
Frank Praznik55d3b662014-04-14 10:11:32 -0400569 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
570 struct sixaxis_led _reserved; /* LED5, not actually soldered */
571} __packed;
572
573union sixaxis_output_report_01 {
574 struct sixaxis_output_report data;
Pavel Machek1adf9042016-02-09 13:55:08 +0100575 u8 buf[36];
Frank Praznik55d3b662014-04-14 10:11:32 -0400576};
577
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400578struct motion_output_report_02 {
579 u8 type, zero;
580 u8 r, g, b;
581 u8 zero2;
582 u8 rumble;
583};
584
Roderick Colenbrander2c159de2016-10-07 12:39:35 -0700585#define DS4_FEATURE_REPORT_0x02_SIZE 37
Roderick Colenbrander55a07d62017-03-07 15:45:01 -0800586#define DS4_FEATURE_REPORT_0x05_SIZE 41
Roderick Colenbrander2c159de2016-10-07 12:39:35 -0700587#define DS4_FEATURE_REPORT_0x81_SIZE 7
Roderick Colenbrander49b9ca62016-10-07 12:39:36 -0700588#define DS4_INPUT_REPORT_0x11_SIZE 78
Roderick Colenbrander2c159de2016-10-07 12:39:35 -0700589#define DS4_OUTPUT_REPORT_0x05_SIZE 32
590#define DS4_OUTPUT_REPORT_0x11_SIZE 78
Antonio Ospite29b691a2015-02-16 18:12:21 +0100591#define SIXAXIS_REPORT_0xF2_SIZE 17
Antonio Ospitea85d67b2015-02-16 18:12:22 +0100592#define SIXAXIS_REPORT_0xF5_SIZE 8
Simon Wood41d2d422015-06-09 21:27:06 -0600593#define MOTION_REPORT_0x02_SIZE 49
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500594
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700595/* Offsets relative to USB input report (0x1). Bluetooth (0x11) requires an
596 * additional +2.
597 */
Roderick Colenbranderd03ae2e2017-03-07 15:45:03 -0800598#define DS4_INPUT_REPORT_AXIS_OFFSET 1
Roderick Colenbranderac797b92016-11-23 14:07:07 -0800599#define DS4_INPUT_REPORT_BUTTON_OFFSET 5
Roderick Colenbrander80786eb2017-03-07 15:45:02 -0800600#define DS4_INPUT_REPORT_TIMESTAMP_OFFSET 10
Roderick Colenbrander227c0112017-03-07 15:45:00 -0800601#define DS4_INPUT_REPORT_GYRO_X_OFFSET 13
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700602#define DS4_INPUT_REPORT_BATTERY_OFFSET 30
603#define DS4_INPUT_REPORT_TOUCHPAD_OFFSET 33
604
Roderick Colenbrander227c0112017-03-07 15:45:00 -0800605#define DS4_SENSOR_SUFFIX " Motion Sensors"
Roderick Colenbranderac797b92016-11-23 14:07:07 -0800606#define DS4_TOUCHPAD_SUFFIX " Touchpad"
607
Roderick Colenbrander55a07d62017-03-07 15:45:01 -0800608#define DS4_GYRO_RES_PER_DEG_S 1024
609#define DS4_ACC_RES_PER_G 8192
610
Jiri Kosina8b402c92015-02-23 11:15:44 +0100611static DEFINE_SPINLOCK(sony_dev_list_lock);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500612static LIST_HEAD(sony_device_list);
Frank Praznik80250872014-04-14 10:11:35 -0400613static DEFINE_IDA(sony_device_id_allocator);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500614
Roderick Colenbrander55a07d62017-03-07 15:45:01 -0800615/* Used for calibration of DS4 accelerometer and gyro. */
616struct ds4_calibration_data {
617 int abs_code;
618 short bias;
619 /* Calibration requires scaling against a sensitivity value, which is a
620 * float. Store sensitivity as a fraction to limit floating point
621 * calculations until final calibration.
622 */
623 int sens_numer;
624 int sens_denom;
625};
626
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200627struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500628 spinlock_t lock;
Frank Praznikd2d782f2014-02-20 11:36:03 -0500629 struct list_head list_node;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100630 struct hid_device *hdev;
Roderick Colenbranderac797b92016-11-23 14:07:07 -0800631 struct input_dev *touchpad;
Roderick Colenbrander227c0112017-03-07 15:45:00 -0800632 struct input_dev *sensor_dev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500633 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200634 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100635 struct work_struct state_worker;
Antonio Ospite09593e32016-02-09 13:55:06 +0100636 void (*send_output_report)(struct sony_sc *);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100637 struct power_supply *battery;
638 struct power_supply_desc battery_desc;
Frank Praznik80250872014-04-14 10:11:35 -0400639 int device_id;
Pavel Machek1adf9042016-02-09 13:55:08 +0100640 u8 *output_report_dmabuf;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200641
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100642#ifdef CONFIG_SONY_FF
Pavel Machek1adf9042016-02-09 13:55:08 +0100643 u8 left;
644 u8 right;
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100645#endif
646
Pavel Machek1adf9042016-02-09 13:55:08 +0100647 u8 mac_address[6];
648 u8 worker_initialized;
Frank Praznik2a242932016-09-22 20:18:09 -0400649 u8 defer_initialization;
Pavel Machek1adf9042016-02-09 13:55:08 +0100650 u8 cable_state;
651 u8 battery_charging;
652 u8 battery_capacity;
653 u8 led_state[MAX_LEDS];
Pavel Machek1adf9042016-02-09 13:55:08 +0100654 u8 led_delay_on[MAX_LEDS];
655 u8 led_delay_off[MAX_LEDS];
656 u8 led_count;
Roderick Colenbrander80786eb2017-03-07 15:45:02 -0800657
658 bool timestamp_initialized;
659 u16 prev_timestamp;
660 unsigned int timestamp_us;
661
Roderick Colenbrander405182c2016-12-08 19:09:52 -0800662 bool ds4_dongle_connected;
Roderick Colenbrander55a07d62017-03-07 15:45:01 -0800663 /* DS4 calibration data */
664 struct ds4_calibration_data ds4_calib_data[6];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200665};
666
Roderick Colenbrander405182c2016-12-08 19:09:52 -0800667static void sony_set_leds(struct sony_sc *sc);
668
Frank Praznik2a242932016-09-22 20:18:09 -0400669static inline void sony_schedule_work(struct sony_sc *sc)
670{
671 if (!sc->defer_initialization)
672 schedule_work(&sc->state_worker);
673}
674
Pavel Machek1adf9042016-02-09 13:55:08 +0100675static u8 *sixaxis_fixup(struct hid_device *hdev, u8 *rdesc,
Antonio Ospitec607fb82014-06-24 13:28:41 +0200676 unsigned int *rsize)
677{
678 *rsize = sizeof(sixaxis_rdesc);
679 return sixaxis_rdesc;
680}
681
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400682static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
683 unsigned int *rsize)
684{
685 *rsize = sizeof(motion_rdesc);
686 return motion_rdesc;
687}
688
Simon Woodb2723eb2015-06-17 00:08:53 -0600689static u8 *navigation_fixup(struct hid_device *hdev, u8 *rdesc,
690 unsigned int *rsize)
691{
692 *rsize = sizeof(navigation_rdesc);
693 return navigation_rdesc;
694}
695
Pavel Machek1adf9042016-02-09 13:55:08 +0100696static u8 *ps3remote_fixup(struct hid_device *hdev, u8 *rdesc,
Jiri Kosina078328d2013-06-13 12:03:49 +0200697 unsigned int *rsize)
698{
699 *rsize = sizeof(ps3remote_rdesc);
700 return ps3remote_rdesc;
701}
702
703static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
704 struct hid_field *field, struct hid_usage *usage,
705 unsigned long **bit, int *max)
706{
707 unsigned int key = usage->hid & HID_USAGE;
708
709 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
710 return -1;
711
712 switch (usage->collection_index) {
713 case 1:
714 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
715 return -1;
716
717 key = ps3remote_keymap_joypad_buttons[key];
718 if (!key)
719 return -1;
720 break;
721 case 2:
722 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
723 return -1;
724
725 key = ps3remote_keymap_remote_buttons[key];
726 if (!key)
727 return -1;
728 break;
729 default:
730 return -1;
731 }
732
733 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
734 return 1;
735}
736
Roderick Colenbrander9131f8c2016-11-23 14:07:08 -0800737static int ds4_mapping(struct hid_device *hdev, struct hid_input *hi,
738 struct hid_field *field, struct hid_usage *usage,
739 unsigned long **bit, int *max)
740{
741 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
742 unsigned int key = usage->hid & HID_USAGE;
743
744 if (key >= ARRAY_SIZE(ds4_keymap))
745 return -1;
746
747 key = ds4_keymap[key];
748 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
749 return 1;
750 } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
751 unsigned int abs = usage->hid & HID_USAGE;
752
753 /* Let the HID parser deal with the HAT. */
754 if (usage->hid == HID_GD_HATSWITCH)
755 return 0;
756
757 if (abs >= ARRAY_SIZE(ds4_absmap))
758 return -1;
759
760 abs = ds4_absmap[abs];
761 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
762 return 1;
763 }
764
765 return 0;
766}
767
Pavel Machek1adf9042016-02-09 13:55:08 +0100768static u8 *sony_report_fixup(struct hid_device *hdev, u8 *rdesc,
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400769 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200770{
771 struct sony_sc *sc = hid_get_drvdata(hdev);
772
Mikko Perttunen4ba1eee2016-07-21 19:54:48 +0300773 if (sc->quirks & (SINO_LITE_CONTROLLER | FUTUREMAX_DANCE_MAT))
Scott Moreau74500cc2016-01-13 07:40:42 -0700774 return rdesc;
775
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900776 /*
777 * Some Sony RF receivers wrongly declare the mouse pointer as a
778 * a constant non-data variable.
779 */
780 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
781 /* usage page: generic desktop controls */
782 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
783 /* usage: mouse */
784 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
785 /* input (usage page for x,y axes): constant, variable, relative */
786 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +0900787 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900788 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200789 rdesc[55] = 0x06;
790 }
Simon Wood61ab44b2011-06-10 12:00:26 +0200791
Antonio Ospitec607fb82014-06-24 13:28:41 +0200792 if (sc->quirks & SIXAXIS_CONTROLLER)
793 return sixaxis_fixup(hdev, rdesc, rsize);
Jiri Kosina078328d2013-06-13 12:03:49 +0200794
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400795 if (sc->quirks & MOTION_CONTROLLER)
796 return motion_fixup(hdev, rdesc, rsize);
797
Simon Wood4545ee02015-06-17 00:08:52 -0600798 if (sc->quirks & NAVIGATION_CONTROLLER)
Simon Woodb2723eb2015-06-17 00:08:53 -0600799 return navigation_fixup(hdev, rdesc, rsize);
Simon Wood4545ee02015-06-17 00:08:52 -0600800
Jiri Kosina078328d2013-06-13 12:03:49 +0200801 if (sc->quirks & PS3REMOTE)
802 return ps3remote_fixup(hdev, rdesc, rsize);
803
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400804 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200805}
806
Pavel Machek1adf9042016-02-09 13:55:08 +0100807static void sixaxis_parse_report(struct sony_sc *sc, u8 *rd, int size)
Frank Praznikd902f472014-01-27 10:17:36 -0500808{
Pavel Machek1adf9042016-02-09 13:55:08 +0100809 static const u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
Frank Praznikd902f472014-01-27 10:17:36 -0500810 unsigned long flags;
Simon Wood12e9a6d72015-06-09 21:27:05 -0600811 int offset;
Pavel Machek1adf9042016-02-09 13:55:08 +0100812 u8 cable_state, battery_capacity, battery_charging;
Frank Praznikd902f472014-01-27 10:17:36 -0500813
Frank Praznikad142b92014-02-20 11:36:00 -0500814 /*
815 * The sixaxis is charging if the battery value is 0xee
Frank Praznikd902f472014-01-27 10:17:36 -0500816 * and it is fully charged if the value is 0xef.
817 * It does not report the actual level while charging so it
818 * is set to 100% while charging is in progress.
819 */
Simon Wood12e9a6d72015-06-09 21:27:05 -0600820 offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
821
822 if (rd[offset] >= 0xee) {
Frank Praznikd902f472014-01-27 10:17:36 -0500823 battery_capacity = 100;
Simon Wood12e9a6d72015-06-09 21:27:05 -0600824 battery_charging = !(rd[offset] & 0x01);
Frank Praznik9fddd742014-08-29 13:11:52 -0400825 cable_state = 1;
Frank Praznikd902f472014-01-27 10:17:36 -0500826 } else {
Pavel Machek1adf9042016-02-09 13:55:08 +0100827 u8 index = rd[offset] <= 5 ? rd[offset] : 5;
Frank Praznikac3c9a92014-02-20 11:36:02 -0500828 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -0500829 battery_charging = 0;
Frank Praznik9fddd742014-08-29 13:11:52 -0400830 cable_state = 0;
Frank Praznikd902f472014-01-27 10:17:36 -0500831 }
Frank Praznikd902f472014-01-27 10:17:36 -0500832
833 spin_lock_irqsave(&sc->lock, flags);
834 sc->cable_state = cable_state;
835 sc->battery_capacity = battery_capacity;
836 sc->battery_charging = battery_charging;
837 spin_unlock_irqrestore(&sc->lock, flags);
838}
839
Pavel Machek1adf9042016-02-09 13:55:08 +0100840static void dualshock4_parse_report(struct sony_sc *sc, u8 *rd, int size)
Frank Praznikd902f472014-01-27 10:17:36 -0500841{
Roderick Colenbranderd03ae2e2017-03-07 15:45:03 -0800842 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
843 struct hid_input, list);
844 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -0500845 unsigned long flags;
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700846 int n, m, offset, num_touch_data, max_touch_data;
Pavel Machek1adf9042016-02-09 13:55:08 +0100847 u8 cable_state, battery_capacity, battery_charging;
Roderick Colenbrander80786eb2017-03-07 15:45:02 -0800848 u16 timestamp;
Frank Praznikd902f472014-01-27 10:17:36 -0500849
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700850 /* When using Bluetooth the header is 2 bytes longer, so skip these. */
Roderick Colenbrander35f436c2017-03-07 15:45:04 -0800851 int data_offset = (sc->quirks & DUALSHOCK4_CONTROLLER_BT) ? 2 : 0;
Frank Praznik6c5f8602014-02-05 20:03:47 -0500852
Roderick Colenbranderac797b92016-11-23 14:07:07 -0800853 /* Second bit of third button byte is for the touchpad button. */
854 offset = data_offset + DS4_INPUT_REPORT_BUTTON_OFFSET;
855 input_report_key(sc->touchpad, BTN_LEFT, rd[offset+2] & 0x2);
856
Roderick Colenbranderd03ae2e2017-03-07 15:45:03 -0800857 /*
858 * The default behavior of the Dualshock 4 is to send reports using
859 * report type 1 when running over Bluetooth. However, when feature
860 * report 2 is requested during the controller initialization it starts
861 * sending input reports in report 17. Since report 17 is undefined
862 * in the default HID descriptor, the HID layer won't generate events.
863 * While it is possible (and this was done before) to fixup the HID
864 * descriptor to add this mapping, it was better to do this manually.
865 * The reason is there were various pieces software both open and closed
866 * source, relying on the descriptors to be the same across various
867 * operating systems. If the descriptors wouldn't match some
868 * applications e.g. games on Wine would not be able to function due
869 * to different descriptors, which such applications are not parsing.
870 */
871 if (rd[0] == 17) {
872 int value;
873
874 offset = data_offset + DS4_INPUT_REPORT_AXIS_OFFSET;
875 input_report_abs(input_dev, ABS_X, rd[offset]);
876 input_report_abs(input_dev, ABS_Y, rd[offset+1]);
877 input_report_abs(input_dev, ABS_RX, rd[offset+2]);
878 input_report_abs(input_dev, ABS_RY, rd[offset+3]);
879
880 value = rd[offset+4] & 0xf;
881 if (value > 7)
882 value = 8; /* Center 0, 0 */
883 input_report_abs(input_dev, ABS_HAT0X, ds4_hat_mapping[value].x);
884 input_report_abs(input_dev, ABS_HAT0Y, ds4_hat_mapping[value].y);
885
886 input_report_key(input_dev, BTN_WEST, rd[offset+4] & 0x10);
887 input_report_key(input_dev, BTN_SOUTH, rd[offset+4] & 0x20);
888 input_report_key(input_dev, BTN_EAST, rd[offset+4] & 0x40);
889 input_report_key(input_dev, BTN_NORTH, rd[offset+4] & 0x80);
890
891 input_report_key(input_dev, BTN_TL, rd[offset+5] & 0x1);
892 input_report_key(input_dev, BTN_TR, rd[offset+5] & 0x2);
893 input_report_key(input_dev, BTN_TL2, rd[offset+5] & 0x4);
894 input_report_key(input_dev, BTN_TR2, rd[offset+5] & 0x8);
895 input_report_key(input_dev, BTN_SELECT, rd[offset+5] & 0x10);
896 input_report_key(input_dev, BTN_START, rd[offset+5] & 0x20);
897 input_report_key(input_dev, BTN_THUMBL, rd[offset+5] & 0x40);
898 input_report_key(input_dev, BTN_THUMBR, rd[offset+5] & 0x80);
899
900 input_report_key(input_dev, BTN_MODE, rd[offset+6] & 0x1);
901
902 input_report_abs(input_dev, ABS_Z, rd[offset+7]);
903 input_report_abs(input_dev, ABS_RZ, rd[offset+8]);
904
905 input_sync(input_dev);
906 }
907
Roderick Colenbrander80786eb2017-03-07 15:45:02 -0800908 /* Convert timestamp (in 5.33us unit) to timestamp_us */
909 offset = data_offset + DS4_INPUT_REPORT_TIMESTAMP_OFFSET;
910 timestamp = get_unaligned_le16(&rd[offset]);
911 if (!sc->timestamp_initialized) {
912 sc->timestamp_us = ((unsigned int)timestamp * 16) / 3;
913 sc->timestamp_initialized = true;
914 } else {
915 u16 delta;
916
917 if (sc->prev_timestamp > timestamp)
918 delta = (U16_MAX - sc->prev_timestamp + timestamp + 1);
919 else
920 delta = timestamp - sc->prev_timestamp;
921 sc->timestamp_us += (delta * 16) / 3;
922 }
923 sc->prev_timestamp = timestamp;
924 input_event(sc->sensor_dev, EV_MSC, MSC_TIMESTAMP, sc->timestamp_us);
925
Roderick Colenbrander227c0112017-03-07 15:45:00 -0800926 offset = data_offset + DS4_INPUT_REPORT_GYRO_X_OFFSET;
Roderick Colenbrander55a07d62017-03-07 15:45:01 -0800927 for (n = 0; n < 6; n++) {
928 /* Store data in int for more precision during mult_frac. */
929 int raw_data = (short)((rd[offset+1] << 8) | rd[offset]);
930 struct ds4_calibration_data *calib = &sc->ds4_calib_data[n];
Roderick Colenbrander227c0112017-03-07 15:45:00 -0800931
Roderick Colenbrander55a07d62017-03-07 15:45:01 -0800932 /* High precision is needed during calibration, but the
933 * calibrated values are within 32-bit.
934 * Note: we swap numerator 'x' and 'numer' in mult_frac for
935 * precision reasons so we don't need 64-bit.
936 */
937 int calib_data = mult_frac(calib->sens_numer,
938 raw_data - calib->bias,
939 calib->sens_denom);
940
941 input_report_abs(sc->sensor_dev, calib->abs_code, calib_data);
942 offset += 2;
Roderick Colenbrander227c0112017-03-07 15:45:00 -0800943 }
944 input_sync(sc->sensor_dev);
945
Frank Praznikad142b92014-02-20 11:36:00 -0500946 /*
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700947 * The lower 4 bits of byte 30 (or 32 for BT) contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -0500948 * and the 5th bit contains the USB cable state.
949 */
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700950 offset = data_offset + DS4_INPUT_REPORT_BATTERY_OFFSET;
Frank Praznik6c5f8602014-02-05 20:03:47 -0500951 cable_state = (rd[offset] >> 4) & 0x01;
952 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -0500953
Frank Praznikad142b92014-02-20 11:36:00 -0500954 /*
955 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -0500956 * 0 to 10, and when running on battery power it ranges from 0 to 9.
957 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -0500958 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500959 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -0500960 battery_charging = 0;
961 else
962 battery_charging = 1;
963
Frank Praznik6c5f8602014-02-05 20:03:47 -0500964 if (!cable_state)
965 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -0500966 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -0500967 battery_capacity = 10;
968
Frank Praznikd902f472014-01-27 10:17:36 -0500969 battery_capacity *= 10;
970
971 spin_lock_irqsave(&sc->lock, flags);
972 sc->cable_state = cable_state;
973 sc->battery_capacity = battery_capacity;
974 sc->battery_charging = battery_charging;
975 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -0500976
Frank Praznikad142b92014-02-20 11:36:00 -0500977 /*
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700978 * The Dualshock 4 multi-touch trackpad data starts at offset 33 on USB
979 * and 35 on Bluetooth.
980 * The first byte indicates the number of touch data in the report.
981 * Trackpad data starts 2 bytes later (e.g. 35 for USB).
Frank Praznike5606232014-01-27 10:17:37 -0500982 */
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700983 offset = data_offset + DS4_INPUT_REPORT_TOUCHPAD_OFFSET;
Roderick Colenbrander35f436c2017-03-07 15:45:04 -0800984 max_touch_data = (sc->quirks & DUALSHOCK4_CONTROLLER_BT) ? 4 : 3;
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700985 if (rd[offset] > 0 && rd[offset] <= max_touch_data)
986 num_touch_data = rd[offset];
987 else
988 num_touch_data = 1;
989 offset += 1;
Frank Praznike5606232014-01-27 10:17:37 -0500990
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700991 for (m = 0; m < num_touch_data; m++) {
992 /* Skip past timestamp */
993 offset += 1;
Frank Praznike5606232014-01-27 10:17:37 -0500994
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700995 /*
996 * The first 7 bits of the first byte is a counter and bit 8 is
997 * a touch indicator that is 0 when pressed and 1 when not
998 * pressed.
999 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1000 * The data for the second touch is in the same format and
1001 * immediately follows the data for the first.
1002 */
1003 for (n = 0; n < 2; n++) {
1004 u16 x, y;
1005 bool active;
Frank Praznike5606232014-01-27 10:17:37 -05001006
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -07001007 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1008 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1009
1010 active = !(rd[offset] >> 7);
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001011 input_mt_slot(sc->touchpad, n);
1012 input_mt_report_slot_state(sc->touchpad, MT_TOOL_FINGER, active);
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -07001013
1014 if (active) {
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001015 input_report_abs(sc->touchpad, ABS_MT_POSITION_X, x);
1016 input_report_abs(sc->touchpad, ABS_MT_POSITION_Y, y);
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -07001017 }
1018
1019 offset += 4;
1020 }
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001021 input_mt_sync_frame(sc->touchpad);
1022 input_sync(sc->touchpad);
Frank Praznike5606232014-01-27 10:17:37 -05001023 }
Frank Praznikd902f472014-01-27 10:17:36 -05001024}
1025
Simon Woodc9e4d872011-06-10 12:00:27 +02001026static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
Pavel Machek1adf9042016-02-09 13:55:08 +01001027 u8 *rd, int size)
Simon Woodc9e4d872011-06-10 12:00:27 +02001028{
1029 struct sony_sc *sc = hid_get_drvdata(hdev);
1030
Frank Praznikad142b92014-02-20 11:36:00 -05001031 /*
1032 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +02001033 * has to be BYTE_SWAPPED before passing up to joystick interface
1034 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001035 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Frank Praznik8f5f0bc2015-07-23 19:01:16 -04001036 /*
1037 * When connected via Bluetooth the Sixaxis occasionally sends
1038 * a report with the second byte 0xff and the rest zeroed.
1039 *
1040 * This report does not reflect the actual state of the
1041 * controller must be ignored to avoid generating false input
1042 * events.
1043 */
1044 if (rd[1] == 0xff)
1045 return -EINVAL;
1046
Simon Woodc9e4d872011-06-10 12:00:27 +02001047 swap(rd[41], rd[42]);
1048 swap(rd[43], rd[44]);
1049 swap(rd[45], rd[46]);
1050 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -05001051
1052 sixaxis_parse_report(sc, rd, size);
Simon Wood12e9a6d72015-06-09 21:27:05 -06001053 } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
1054 sixaxis_parse_report(sc, rd, size);
Simon Wood4545ee02015-06-17 00:08:52 -06001055 } else if ((sc->quirks & NAVIGATION_CONTROLLER) && rd[0] == 0x01 &&
1056 size == 49) {
1057 sixaxis_parse_report(sc, rd, size);
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001058 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1059 size == 64) {
1060 dualshock4_parse_report(sc, rd, size);
1061 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && rd[0] == 0x11 &&
1062 size == 78)) {
1063 /* CRC check */
1064 u8 bthdr = 0xA1;
1065 u32 crc;
1066 u32 report_crc;
Roderick Colenbrander49b9ca62016-10-07 12:39:36 -07001067
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001068 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
1069 crc = ~crc32_le(crc, rd, DS4_INPUT_REPORT_0x11_SIZE-4);
1070 report_crc = get_unaligned_le32(&rd[DS4_INPUT_REPORT_0x11_SIZE-4]);
1071 if (crc != report_crc) {
1072 hid_dbg(sc->hdev, "DualShock 4 input report's CRC check failed, received crc 0x%0x != 0x%0x\n",
1073 report_crc, crc);
1074 return -EILSEQ;
Roderick Colenbrander49b9ca62016-10-07 12:39:36 -07001075 }
Roderick Colenbrander405182c2016-12-08 19:09:52 -08001076
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001077 dualshock4_parse_report(sc, rd, size);
1078 } else if ((sc->quirks & DUALSHOCK4_DONGLE) && rd[0] == 0x01 &&
1079 size == 64) {
Roderick Colenbrander405182c2016-12-08 19:09:52 -08001080 /*
1081 * In the case of a DS4 USB dongle, bit[2] of byte 31 indicates
1082 * if a DS4 is actually connected (indicated by '0').
1083 * For non-dongle, this bit is always 0 (connected).
1084 */
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001085 bool connected = (rd[31] & 0x04) ? false : true;
Roderick Colenbrander405182c2016-12-08 19:09:52 -08001086
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001087 if (!sc->ds4_dongle_connected && connected) {
1088 hid_info(sc->hdev, "DualShock 4 USB dongle: controller connected\n");
1089 sony_set_leds(sc);
1090 sc->ds4_dongle_connected = true;
1091 } else if (sc->ds4_dongle_connected && !connected) {
1092 hid_info(sc->hdev, "DualShock 4 USB dongle: controller disconnected\n");
1093 sc->ds4_dongle_connected = false;
1094 /* Return 0, so hidraw can get the report. */
1095 return 0;
1096 } else if (!sc->ds4_dongle_connected) {
1097 /* Return 0, so hidraw can get the report. */
1098 return 0;
Roderick Colenbrander405182c2016-12-08 19:09:52 -08001099 }
1100
Frank Praznikd902f472014-01-27 10:17:36 -05001101 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +02001102 }
1103
Frank Praznik2a242932016-09-22 20:18:09 -04001104 if (sc->defer_initialization) {
1105 sc->defer_initialization = 0;
1106 sony_schedule_work(sc);
1107 }
1108
Simon Woodc9e4d872011-06-10 12:00:27 +02001109 return 0;
1110}
1111
Colin Leitnerf04d5142013-05-27 23:41:05 +02001112static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1113 struct hid_field *field, struct hid_usage *usage,
1114 unsigned long **bit, int *max)
1115{
1116 struct sony_sc *sc = hid_get_drvdata(hdev);
1117
1118 if (sc->quirks & BUZZ_CONTROLLER) {
1119 unsigned int key = usage->hid & HID_USAGE;
1120
1121 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1122 return -1;
1123
1124 switch (usage->collection_index) {
1125 case 1:
1126 if (key >= ARRAY_SIZE(buzz_keymap))
1127 return -1;
1128
1129 key = buzz_keymap[key];
1130 if (!key)
1131 return -1;
1132 break;
1133 default:
1134 return -1;
1135 }
1136
1137 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1138 return 1;
1139 }
1140
Jiri Kosina078328d2013-06-13 12:03:49 +02001141 if (sc->quirks & PS3REMOTE)
1142 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1143
Roderick Colenbrander9131f8c2016-11-23 14:07:08 -08001144
1145 if (sc->quirks & DUALSHOCK4_CONTROLLER)
1146 return ds4_mapping(hdev, hi, field, usage, bit, max);
1147
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001148 /* Let hid-core decide for the others */
1149 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001150}
1151
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001152static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
Frank Praznikce8efc32014-09-18 21:15:01 -04001153 int w, int h)
1154{
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001155 size_t name_sz;
1156 char *name;
Frank Praznikce8efc32014-09-18 21:15:01 -04001157 int ret;
1158
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001159 sc->touchpad = input_allocate_device();
1160 if (!sc->touchpad)
1161 return -ENOMEM;
Frank Praznikce8efc32014-09-18 21:15:01 -04001162
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001163 input_set_drvdata(sc->touchpad, sc);
1164 sc->touchpad->dev.parent = &sc->hdev->dev;
1165 sc->touchpad->phys = sc->hdev->phys;
1166 sc->touchpad->uniq = sc->hdev->uniq;
1167 sc->touchpad->id.bustype = sc->hdev->bus;
1168 sc->touchpad->id.vendor = sc->hdev->vendor;
1169 sc->touchpad->id.product = sc->hdev->product;
1170 sc->touchpad->id.version = sc->hdev->version;
1171
1172 /* Append a suffix to the controller name as there are various
1173 * DS4 compatible non-Sony devices with different names.
1174 */
1175 name_sz = strlen(sc->hdev->name) + sizeof(DS4_TOUCHPAD_SUFFIX);
1176 name = kzalloc(name_sz, GFP_KERNEL);
1177 if (!name) {
1178 ret = -ENOMEM;
1179 goto err;
1180 }
1181 snprintf(name, name_sz, "%s" DS4_TOUCHPAD_SUFFIX, sc->hdev->name);
1182 sc->touchpad->name = name;
1183
1184 ret = input_mt_init_slots(sc->touchpad, touch_count, 0);
1185 if (ret < 0)
1186 goto err;
1187
1188 /* We map the button underneath the touchpad to BTN_LEFT. */
1189 __set_bit(EV_KEY, sc->touchpad->evbit);
1190 __set_bit(BTN_LEFT, sc->touchpad->keybit);
1191 __set_bit(INPUT_PROP_BUTTONPAD, sc->touchpad->propbit);
1192
1193 input_set_abs_params(sc->touchpad, ABS_MT_POSITION_X, 0, w, 0, 0);
1194 input_set_abs_params(sc->touchpad, ABS_MT_POSITION_Y, 0, h, 0, 0);
1195
1196 ret = input_register_device(sc->touchpad);
1197 if (ret < 0)
1198 goto err;
Frank Praznikce8efc32014-09-18 21:15:01 -04001199
1200 return 0;
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001201
1202err:
1203 kfree(sc->touchpad->name);
1204 sc->touchpad->name = NULL;
1205
1206 input_free_device(sc->touchpad);
1207 sc->touchpad = NULL;
1208
1209 return ret;
Frank Praznikce8efc32014-09-18 21:15:01 -04001210}
1211
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001212static void sony_unregister_touchpad(struct sony_sc *sc)
1213{
1214 if (!sc->touchpad)
1215 return;
1216
1217 kfree(sc->touchpad->name);
1218 sc->touchpad->name = NULL;
1219
1220 input_unregister_device(sc->touchpad);
1221 sc->touchpad = NULL;
1222}
Frank Praznikce8efc32014-09-18 21:15:01 -04001223
Roderick Colenbrander227c0112017-03-07 15:45:00 -08001224static int sony_register_sensors(struct sony_sc *sc)
1225{
1226 size_t name_sz;
1227 char *name;
1228 int ret;
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001229 int range;
Roderick Colenbrander227c0112017-03-07 15:45:00 -08001230
1231 sc->sensor_dev = input_allocate_device();
1232 if (!sc->sensor_dev)
1233 return -ENOMEM;
1234
1235 input_set_drvdata(sc->sensor_dev, sc);
1236 sc->sensor_dev->dev.parent = &sc->hdev->dev;
1237 sc->sensor_dev->phys = sc->hdev->phys;
1238 sc->sensor_dev->uniq = sc->hdev->uniq;
1239 sc->sensor_dev->id.bustype = sc->hdev->bus;
1240 sc->sensor_dev->id.vendor = sc->hdev->vendor;
1241 sc->sensor_dev->id.product = sc->hdev->product;
1242 sc->sensor_dev->id.version = sc->hdev->version;
1243
1244 /* Append a suffix to the controller name as there are various
1245 * DS4 compatible non-Sony devices with different names.
1246 */
1247 name_sz = strlen(sc->hdev->name) + sizeof(DS4_SENSOR_SUFFIX);
1248 name = kzalloc(name_sz, GFP_KERNEL);
1249 if (!name) {
1250 ret = -ENOMEM;
1251 goto err;
1252 }
1253 snprintf(name, name_sz, "%s" DS4_SENSOR_SUFFIX, sc->hdev->name);
1254 sc->sensor_dev->name = name;
1255
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001256 range = DS4_ACC_RES_PER_G*4;
1257 input_set_abs_params(sc->sensor_dev, ABS_X, -range, range, 16, 0);
1258 input_set_abs_params(sc->sensor_dev, ABS_Y, -range, range, 16, 0);
1259 input_set_abs_params(sc->sensor_dev, ABS_Z, -range, range, 16, 0);
1260 input_abs_set_res(sc->sensor_dev, ABS_X, DS4_ACC_RES_PER_G);
1261 input_abs_set_res(sc->sensor_dev, ABS_Y, DS4_ACC_RES_PER_G);
1262 input_abs_set_res(sc->sensor_dev, ABS_Z, DS4_ACC_RES_PER_G);
Roderick Colenbrander227c0112017-03-07 15:45:00 -08001263
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001264 range = DS4_GYRO_RES_PER_DEG_S*2048;
1265 input_set_abs_params(sc->sensor_dev, ABS_RX, -range, range, 16, 0);
1266 input_set_abs_params(sc->sensor_dev, ABS_RY, -range, range, 16, 0);
1267 input_set_abs_params(sc->sensor_dev, ABS_RZ, -range, range, 16, 0);
1268 input_abs_set_res(sc->sensor_dev, ABS_RX, DS4_GYRO_RES_PER_DEG_S);
1269 input_abs_set_res(sc->sensor_dev, ABS_RY, DS4_GYRO_RES_PER_DEG_S);
1270 input_abs_set_res(sc->sensor_dev, ABS_RZ, DS4_GYRO_RES_PER_DEG_S);
Roderick Colenbrander227c0112017-03-07 15:45:00 -08001271
Roderick Colenbrander80786eb2017-03-07 15:45:02 -08001272 __set_bit(EV_MSC, sc->sensor_dev->evbit);
1273 __set_bit(MSC_TIMESTAMP, sc->sensor_dev->mscbit);
Roderick Colenbrander227c0112017-03-07 15:45:00 -08001274 __set_bit(INPUT_PROP_ACCELEROMETER, sc->sensor_dev->propbit);
1275
1276 ret = input_register_device(sc->sensor_dev);
1277 if (ret < 0)
1278 goto err;
1279
1280 return 0;
1281
1282err:
1283 kfree(sc->sensor_dev->name);
1284 sc->sensor_dev->name = NULL;
1285
1286 input_free_device(sc->sensor_dev);
1287 sc->sensor_dev = NULL;
1288
1289 return ret;
1290}
1291
1292static void sony_unregister_sensors(struct sony_sc *sc)
1293{
1294 if (!sc->sensor_dev)
1295 return;
1296
1297 kfree(sc->sensor_dev->name);
1298 sc->sensor_dev->name = NULL;
1299
1300 input_unregister_device(sc->sensor_dev);
1301 sc->sensor_dev = NULL;
1302}
1303
1304
Antonio Ospite5710fabf2011-02-20 18:26:45 +01001305/*
Jiri Slabybd28ce02008-06-25 23:47:04 +02001306 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1307 * to "operational". Without this, the ps3 controller will not report any
1308 * events.
1309 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001310static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001311{
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001312 const int buf_size =
1313 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
Pavel Machek1adf9042016-02-09 13:55:08 +01001314 u8 *buf;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001315 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001316
Antonio Ospite2e701a32015-02-16 18:12:24 +01001317 buf = kmalloc(buf_size, GFP_KERNEL);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001318 if (!buf)
1319 return -ENOMEM;
1320
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001321 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1322 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001323 if (ret < 0) {
1324 hid_err(hdev, "can't set operational mode: step 1\n");
1325 goto out;
1326 }
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001327
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001328 /*
1329 * Some compatible controllers like the Speedlink Strike FX and
1330 * Gasia need another query plus an USB interrupt to get operational.
1331 */
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001332 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1333 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001334 if (ret < 0) {
1335 hid_err(hdev, "can't set operational mode: step 2\n");
1336 goto out;
1337 }
1338
1339 ret = hid_hw_output_report(hdev, buf, 1);
Benjamin Tissoires19f4c2b2016-01-08 17:58:49 +01001340 if (ret < 0) {
1341 hid_info(hdev, "can't set operational mode: step 3, ignoring\n");
1342 ret = 0;
1343 }
Jiri Slabybd28ce02008-06-25 23:47:04 +02001344
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001345out:
Jiri Slabybd28ce02008-06-25 23:47:04 +02001346 kfree(buf);
1347
1348 return ret;
1349}
1350
Antonio Ospite816651a2010-05-03 22:15:55 +02001351static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001352{
Pavel Machek1adf9042016-02-09 13:55:08 +01001353 static const u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1354 u8 *buf;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001355 int ret;
1356
1357 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1358 if (!buf)
1359 return -ENOMEM;
1360
1361 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
Benjamin Tissoiresb0dd72a2014-02-10 12:58:54 -05001362 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001363
1364 kfree(buf);
1365
1366 return ret;
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001367}
1368
Frank Praznikad142b92014-02-20 11:36:00 -05001369/*
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001370 * Request DS4 calibration data for the motion sensors.
1371 * For Bluetooth this also affects the operating mode (see below).
Frank Praznik68330d82014-02-05 20:03:49 -05001372 */
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001373static int dualshock4_get_calibration_data(struct sony_sc *sc)
Frank Praznik68330d82014-02-05 20:03:49 -05001374{
Pavel Machek1adf9042016-02-09 13:55:08 +01001375 u8 *buf;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001376 int ret;
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001377 short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus;
1378 short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus;
1379 short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus;
1380 short gyro_speed_plus, gyro_speed_minus;
1381 short acc_x_plus, acc_x_minus;
1382 short acc_y_plus, acc_y_minus;
1383 short acc_z_plus, acc_z_minus;
1384 int speed_2x;
1385 int range_2g;
Frank Praznik68330d82014-02-05 20:03:49 -05001386
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001387 /* For Bluetooth we use a different request, which supports CRC.
1388 * Note: in Bluetooth mode feature report 0x02 also changes the state
1389 * of the controller, so that it sends input reports of type 0x11.
1390 */
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001391 if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001392 buf = kmalloc(DS4_FEATURE_REPORT_0x02_SIZE, GFP_KERNEL);
1393 if (!buf)
1394 return -ENOMEM;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001395
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001396 ret = hid_hw_raw_request(sc->hdev, 0x02, buf,
1397 DS4_FEATURE_REPORT_0x02_SIZE,
1398 HID_FEATURE_REPORT,
1399 HID_REQ_GET_REPORT);
1400 if (ret < 0)
1401 goto err_stop;
1402 } else {
1403 u8 bthdr = 0xA3;
1404 u32 crc;
1405 u32 report_crc;
1406 int retries;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001407
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001408 buf = kmalloc(DS4_FEATURE_REPORT_0x05_SIZE, GFP_KERNEL);
1409 if (!buf)
1410 return -ENOMEM;
1411
1412 for (retries = 0; retries < 3; retries++) {
1413 ret = hid_hw_raw_request(sc->hdev, 0x05, buf,
1414 DS4_FEATURE_REPORT_0x05_SIZE,
1415 HID_FEATURE_REPORT,
1416 HID_REQ_GET_REPORT);
1417 if (ret < 0)
1418 goto err_stop;
1419
1420 /* CRC check */
1421 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
1422 crc = ~crc32_le(crc, buf, DS4_FEATURE_REPORT_0x05_SIZE-4);
1423 report_crc = get_unaligned_le32(&buf[DS4_FEATURE_REPORT_0x05_SIZE-4]);
1424 if (crc != report_crc) {
1425 hid_warn(sc->hdev, "DualShock 4 calibration report's CRC check failed, received crc 0x%0x != 0x%0x\n",
1426 report_crc, crc);
1427 if (retries < 2) {
1428 hid_warn(sc->hdev, "Retrying DualShock 4 get calibration report request\n");
1429 continue;
1430 } else {
1431 ret = -EILSEQ;
1432 goto err_stop;
1433 }
1434 } else {
1435 break;
1436 }
1437 }
1438 }
1439
1440 gyro_pitch_bias = get_unaligned_le16(&buf[1]);
1441 gyro_yaw_bias = get_unaligned_le16(&buf[3]);
1442 gyro_roll_bias = get_unaligned_le16(&buf[5]);
1443 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1444 gyro_pitch_plus = get_unaligned_le16(&buf[7]);
1445 gyro_pitch_minus = get_unaligned_le16(&buf[9]);
1446 gyro_yaw_plus = get_unaligned_le16(&buf[11]);
1447 gyro_yaw_minus = get_unaligned_le16(&buf[13]);
1448 gyro_roll_plus = get_unaligned_le16(&buf[15]);
1449 gyro_roll_minus = get_unaligned_le16(&buf[17]);
1450 } else {
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001451 /* BT + Dongle */
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001452 gyro_pitch_plus = get_unaligned_le16(&buf[7]);
1453 gyro_yaw_plus = get_unaligned_le16(&buf[9]);
1454 gyro_roll_plus = get_unaligned_le16(&buf[11]);
1455 gyro_pitch_minus = get_unaligned_le16(&buf[13]);
1456 gyro_yaw_minus = get_unaligned_le16(&buf[15]);
1457 gyro_roll_minus = get_unaligned_le16(&buf[17]);
1458 }
1459 gyro_speed_plus = get_unaligned_le16(&buf[19]);
1460 gyro_speed_minus = get_unaligned_le16(&buf[21]);
1461 acc_x_plus = get_unaligned_le16(&buf[23]);
1462 acc_x_minus = get_unaligned_le16(&buf[25]);
1463 acc_y_plus = get_unaligned_le16(&buf[27]);
1464 acc_y_minus = get_unaligned_le16(&buf[29]);
1465 acc_z_plus = get_unaligned_le16(&buf[31]);
1466 acc_z_minus = get_unaligned_le16(&buf[33]);
1467
1468 /* Set gyroscope calibration and normalization parameters.
1469 * Data values will be normalized to 1/DS4_GYRO_RES_PER_DEG_S degree/s.
1470 */
1471 speed_2x = (gyro_speed_plus + gyro_speed_minus);
1472 sc->ds4_calib_data[0].abs_code = ABS_RX;
1473 sc->ds4_calib_data[0].bias = gyro_pitch_bias;
1474 sc->ds4_calib_data[0].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
1475 sc->ds4_calib_data[0].sens_denom = gyro_pitch_plus - gyro_pitch_minus;
1476
1477 sc->ds4_calib_data[1].abs_code = ABS_RY;
1478 sc->ds4_calib_data[1].bias = gyro_yaw_bias;
1479 sc->ds4_calib_data[1].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
1480 sc->ds4_calib_data[1].sens_denom = gyro_yaw_plus - gyro_yaw_minus;
1481
1482 sc->ds4_calib_data[2].abs_code = ABS_RZ;
1483 sc->ds4_calib_data[2].bias = gyro_roll_bias;
1484 sc->ds4_calib_data[2].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
1485 sc->ds4_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus;
1486
1487 /* Set accelerometer calibration and normalization parameters.
1488 * Data values will be normalized to 1/DS4_ACC_RES_PER_G G.
1489 */
1490 range_2g = acc_x_plus - acc_x_minus;
1491 sc->ds4_calib_data[3].abs_code = ABS_X;
1492 sc->ds4_calib_data[3].bias = acc_x_plus - range_2g / 2;
1493 sc->ds4_calib_data[3].sens_numer = 2*DS4_ACC_RES_PER_G;
1494 sc->ds4_calib_data[3].sens_denom = range_2g;
1495
1496 range_2g = acc_y_plus - acc_y_minus;
1497 sc->ds4_calib_data[4].abs_code = ABS_Y;
1498 sc->ds4_calib_data[4].bias = acc_y_plus - range_2g / 2;
1499 sc->ds4_calib_data[4].sens_numer = 2*DS4_ACC_RES_PER_G;
1500 sc->ds4_calib_data[4].sens_denom = range_2g;
1501
1502 range_2g = acc_z_plus - acc_z_minus;
1503 sc->ds4_calib_data[5].abs_code = ABS_Z;
1504 sc->ds4_calib_data[5].bias = acc_z_plus - range_2g / 2;
1505 sc->ds4_calib_data[5].sens_numer = 2*DS4_ACC_RES_PER_G;
1506 sc->ds4_calib_data[5].sens_denom = range_2g;
1507
1508err_stop:
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001509 kfree(buf);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001510 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001511}
1512
Frank Praznik221399b2015-05-05 20:47:32 -04001513static void sixaxis_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001514{
Pavel Machek1adf9042016-02-09 13:55:08 +01001515 static const u8 sixaxis_leds[10][4] = {
Frank Praznik80250872014-04-14 10:11:35 -04001516 { 0x01, 0x00, 0x00, 0x00 },
1517 { 0x00, 0x01, 0x00, 0x00 },
1518 { 0x00, 0x00, 0x01, 0x00 },
1519 { 0x00, 0x00, 0x00, 0x01 },
1520 { 0x01, 0x00, 0x00, 0x01 },
1521 { 0x00, 0x01, 0x00, 0x01 },
1522 { 0x00, 0x00, 0x01, 0x01 },
1523 { 0x01, 0x00, 0x01, 0x01 },
1524 { 0x00, 0x01, 0x01, 0x01 },
1525 { 0x01, 0x01, 0x01, 0x01 }
1526 };
1527
Frank Praznik221399b2015-05-05 20:47:32 -04001528 int id = sc->device_id;
1529
1530 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001531
1532 if (id < 0)
1533 return;
1534
1535 id %= 10;
Frank Praznik221399b2015-05-05 20:47:32 -04001536 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001537}
1538
Frank Praznik221399b2015-05-05 20:47:32 -04001539static void dualshock4_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001540{
1541 /* The first 4 color/index entries match what the PS4 assigns */
Pavel Machek1adf9042016-02-09 13:55:08 +01001542 static const u8 color_code[7][3] = {
Frank Praznik80250872014-04-14 10:11:35 -04001543 /* Blue */ { 0x00, 0x00, 0x01 },
1544 /* Red */ { 0x01, 0x00, 0x00 },
1545 /* Green */ { 0x00, 0x01, 0x00 },
1546 /* Pink */ { 0x02, 0x00, 0x01 },
1547 /* Orange */ { 0x02, 0x01, 0x00 },
1548 /* Teal */ { 0x00, 0x01, 0x01 },
1549 /* White */ { 0x01, 0x01, 0x01 }
1550 };
1551
Frank Praznik221399b2015-05-05 20:47:32 -04001552 int id = sc->device_id;
1553
1554 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001555
1556 if (id < 0)
1557 return;
1558
1559 id %= 7;
Frank Praznik221399b2015-05-05 20:47:32 -04001560 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001561}
1562
Frank Praznik221399b2015-05-05 20:47:32 -04001563static void buzz_set_leds(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001564{
Frank Praznik221399b2015-05-05 20:47:32 -04001565 struct hid_device *hdev = sc->hdev;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001566 struct list_head *report_list =
1567 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1568 struct hid_report *report = list_entry(report_list->next,
1569 struct hid_report, list);
Pavel Machek1adf9042016-02-09 13:55:08 +01001570 s32 *value = report->field[0]->value;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001571
Frank Praznik221399b2015-05-05 20:47:32 -04001572 BUILD_BUG_ON(MAX_LEDS < 4);
1573
Colin Leitnerf04d5142013-05-27 23:41:05 +02001574 value[0] = 0x00;
Frank Praznik221399b2015-05-05 20:47:32 -04001575 value[1] = sc->led_state[0] ? 0xff : 0x00;
1576 value[2] = sc->led_state[1] ? 0xff : 0x00;
1577 value[3] = sc->led_state[2] ? 0xff : 0x00;
1578 value[4] = sc->led_state[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001579 value[5] = 0x00;
1580 value[6] = 0x00;
1581 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1582}
1583
Frank Praznik221399b2015-05-05 20:47:32 -04001584static void sony_set_leds(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001585{
Frank Praznik221399b2015-05-05 20:47:32 -04001586 if (!(sc->quirks & BUZZ_CONTROLLER))
Frank Praznik2a242932016-09-22 20:18:09 -04001587 sony_schedule_work(sc);
Frank Praznik221399b2015-05-05 20:47:32 -04001588 else
1589 buzz_set_leds(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001590}
1591
Sven Eckelmannc5382512013-11-19 20:26:30 +01001592static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001593 enum led_brightness value)
1594{
1595 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001596 struct hid_device *hdev = to_hid_device(dev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001597 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001598
1599 int n;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001600 int force_update;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001601
1602 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001603 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001604 hid_err(hdev, "No device data\n");
1605 return;
1606 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001607
Frank Praznikb3ed4582014-04-14 10:11:36 -04001608 /*
1609 * The Sixaxis on USB will override any LED settings sent to it
1610 * and keep flashing all of the LEDs until the PS button is pressed.
1611 * Updates, even if redundant, must be always be sent to the
1612 * controller to avoid having to toggle the state of an LED just to
1613 * stop the flashing later on.
1614 */
1615 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1616
Frank Praznik60781cf2014-01-11 15:13:15 -05001617 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznikb3ed4582014-04-14 10:11:36 -04001618 if (led == drv_data->leds[n] && (force_update ||
1619 (value != drv_data->led_state[n] ||
1620 drv_data->led_delay_on[n] ||
1621 drv_data->led_delay_off[n]))) {
1622
1623 drv_data->led_state[n] = value;
1624
1625 /* Setting the brightness stops the blinking */
1626 drv_data->led_delay_on[n] = 0;
1627 drv_data->led_delay_off[n] = 0;
1628
Frank Praznik221399b2015-05-05 20:47:32 -04001629 sony_set_leds(drv_data);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001630 break;
1631 }
1632 }
1633}
1634
Sven Eckelmannc5382512013-11-19 20:26:30 +01001635static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001636{
1637 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001638 struct hid_device *hdev = to_hid_device(dev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001639 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001640
1641 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001642
1643 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001644 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001645 hid_err(hdev, "No device data\n");
1646 return LED_OFF;
1647 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001648
Frank Praznik60781cf2014-01-11 15:13:15 -05001649 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001650 if (led == drv_data->leds[n])
1651 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001652 }
1653
Simon Wood7db75042014-02-05 12:34:18 -07001654 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001655}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001656
Frank Praznikb3ed4582014-04-14 10:11:36 -04001657static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1658 unsigned long *delay_off)
1659{
1660 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001661 struct hid_device *hdev = to_hid_device(dev);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001662 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1663 int n;
Pavel Machek1adf9042016-02-09 13:55:08 +01001664 u8 new_on, new_off;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001665
1666 if (!drv_data) {
1667 hid_err(hdev, "No device data\n");
1668 return -EINVAL;
1669 }
1670
1671 /* Max delay is 255 deciseconds or 2550 milliseconds */
1672 if (*delay_on > 2550)
1673 *delay_on = 2550;
1674 if (*delay_off > 2550)
1675 *delay_off = 2550;
1676
1677 /* Blink at 1 Hz if both values are zero */
1678 if (!*delay_on && !*delay_off)
1679 *delay_on = *delay_off = 500;
1680
1681 new_on = *delay_on / 10;
1682 new_off = *delay_off / 10;
1683
1684 for (n = 0; n < drv_data->led_count; n++) {
1685 if (led == drv_data->leds[n])
1686 break;
1687 }
1688
1689 /* This LED is not registered on this device */
1690 if (n >= drv_data->led_count)
1691 return -EINVAL;
1692
1693 /* Don't schedule work if the values didn't change */
1694 if (new_on != drv_data->led_delay_on[n] ||
1695 new_off != drv_data->led_delay_off[n]) {
1696 drv_data->led_delay_on[n] = new_on;
1697 drv_data->led_delay_off[n] = new_off;
Frank Praznik2a242932016-09-22 20:18:09 -04001698 sony_schedule_work(drv_data);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001699 }
1700
1701 return 0;
1702}
1703
Frank Praznikfa57a812014-04-14 10:11:33 -04001704static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001705{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001706 struct led_classdev *led;
1707 int n;
1708
Frank Praznikfa57a812014-04-14 10:11:33 -04001709 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001710
Frank Praznikfa57a812014-04-14 10:11:33 -04001711 for (n = 0; n < sc->led_count; n++) {
1712 led = sc->leds[n];
1713 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001714 if (!led)
1715 continue;
1716 led_classdev_unregister(led);
1717 kfree(led);
1718 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001719
Frank Praznikfa57a812014-04-14 10:11:33 -04001720 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001721}
1722
Frank Praznikfa57a812014-04-14 10:11:33 -04001723static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001724{
Frank Praznikfa57a812014-04-14 10:11:33 -04001725 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001726 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001727 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001728 struct led_classdev *led;
1729 size_t name_sz;
1730 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001731 size_t name_len;
1732 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001733 static const char * const ds4_name_str[] = { "red", "green", "blue",
1734 "global" };
Pavel Machek1adf9042016-02-09 13:55:08 +01001735 u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
1736 u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001737
Frank Praznikfa57a812014-04-14 10:11:33 -04001738 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001739
Frank Praznikfa57a812014-04-14 10:11:33 -04001740 if (sc->quirks & BUZZ_CONTROLLER) {
1741 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001742 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001743 name_len = strlen("::buzz#");
1744 name_fmt = "%s::buzz%d";
1745 /* Validate expected report characteristics. */
1746 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1747 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001748 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik221399b2015-05-05 20:47:32 -04001749 dualshock4_set_leds_from_id(sc);
1750 sc->led_state[3] = 1;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001751 sc->led_count = 4;
1752 memset(max_brightness, 255, 3);
1753 use_hw_blink[3] = 1;
1754 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001755 name_len = 0;
1756 name_fmt = "%s:%s";
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001757 } else if (sc->quirks & MOTION_CONTROLLER) {
1758 sc->led_count = 3;
1759 memset(max_brightness, 255, 3);
1760 use_ds4_names = 1;
1761 name_len = 0;
1762 name_fmt = "%s:%s";
Simon Wood4545ee02015-06-17 00:08:52 -06001763 } else if (sc->quirks & NAVIGATION_CONTROLLER) {
Pavel Machek1adf9042016-02-09 13:55:08 +01001764 static const u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00};
Simon Wood4545ee02015-06-17 00:08:52 -06001765
1766 memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds));
1767 sc->led_count = 1;
1768 memset(use_hw_blink, 1, 4);
1769 use_ds4_names = 0;
1770 name_len = strlen("::sony#");
1771 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001772 } else {
Frank Praznik221399b2015-05-05 20:47:32 -04001773 sixaxis_set_leds_from_id(sc);
Frank Praznikfa57a812014-04-14 10:11:33 -04001774 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001775 memset(use_hw_blink, 1, 4);
1776 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001777 name_len = strlen("::sony#");
1778 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001779 }
1780
Frank Praznikad142b92014-02-20 11:36:00 -05001781 /*
1782 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001783 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001784 * LEDs to on
1785 */
Frank Praznik221399b2015-05-05 20:47:32 -04001786 sony_set_leds(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001787
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001788 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001789
Frank Praznikfa57a812014-04-14 10:11:33 -04001790 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001791
Frank Praznikb3ed4582014-04-14 10:11:36 -04001792 if (use_ds4_names)
1793 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001794
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001795 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1796 if (!led) {
1797 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001798 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001799 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001800 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001801
1802 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001803 if (use_ds4_names)
1804 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1805 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001806 else
1807 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001808 led->name = name;
Frank Praznik221399b2015-05-05 20:47:32 -04001809 led->brightness = sc->led_state[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001810 led->max_brightness = max_brightness[n];
Frank Praznik765a1072017-02-08 13:58:43 -05001811 led->flags = LED_CORE_SUSPENDRESUME;
Sven Eckelmannc5382512013-11-19 20:26:30 +01001812 led->brightness_get = sony_led_get_brightness;
1813 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001814
Frank Praznikb3ed4582014-04-14 10:11:36 -04001815 if (use_hw_blink[n])
1816 led->blink_set = sony_led_blink_set;
1817
Frank Praznik80250872014-04-14 10:11:35 -04001818 sc->leds[n] = led;
1819
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001820 ret = led_classdev_register(&hdev->dev, led);
1821 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001822 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001823 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001824 kfree(led);
1825 goto error_leds;
1826 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001827 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001828
1829 return ret;
1830
Colin Leitnerf04d5142013-05-27 23:41:05 +02001831error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001832 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001833
Colin Leitnerf04d5142013-05-27 23:41:05 +02001834 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001835}
1836
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001837static void sixaxis_send_output_report(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001838{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001839 static const union sixaxis_output_report_01 default_report = {
Frank Praznik55d3b662014-04-14 10:11:32 -04001840 .buf = {
1841 0x01,
Scott Moreauad07b7a2016-01-13 07:40:43 -07001842 0x01, 0xff, 0x00, 0xff, 0x00,
Frank Praznik55d3b662014-04-14 10:11:32 -04001843 0x00, 0x00, 0x00, 0x00, 0x00,
1844 0xff, 0x27, 0x10, 0x00, 0x32,
1845 0xff, 0x27, 0x10, 0x00, 0x32,
1846 0xff, 0x27, 0x10, 0x00, 0x32,
1847 0xff, 0x27, 0x10, 0x00, 0x32,
1848 0x00, 0x00, 0x00, 0x00, 0x00
1849 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001850 };
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001851 struct sixaxis_output_report *report =
1852 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1853 int n;
1854
1855 /* Initialize the report with default values */
1856 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001857
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001858#ifdef CONFIG_SONY_FF
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001859 report->rumble.right_motor_on = sc->right ? 1 : 0;
1860 report->rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001861#endif
1862
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001863 report->leds_bitmap |= sc->led_state[0] << 1;
1864 report->leds_bitmap |= sc->led_state[1] << 2;
1865 report->leds_bitmap |= sc->led_state[2] << 3;
1866 report->leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001867
Simon Wood88f65762014-04-14 10:11:37 -04001868 /* Set flag for all leds off, required for 3rd party INTEC controller */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001869 if ((report->leds_bitmap & 0x1E) == 0)
1870 report->leds_bitmap |= 0x20;
Simon Wood88f65762014-04-14 10:11:37 -04001871
Frank Praznikb3ed4582014-04-14 10:11:36 -04001872 /*
1873 * The LEDs in the report are indexed in reverse order to their
1874 * corresponding light on the controller.
1875 * Index 0 = LED 4, index 1 = LED 3, etc...
1876 *
1877 * In the case of both delay values being zero (blinking disabled) the
1878 * default report values should be used or the controller LED will be
1879 * always off.
1880 */
1881 for (n = 0; n < 4; n++) {
1882 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001883 report->led[3 - n].duty_off = sc->led_delay_off[n];
1884 report->led[3 - n].duty_on = sc->led_delay_on[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001885 }
1886 }
1887
Pavel Machek1adf9042016-02-09 13:55:08 +01001888 hid_hw_raw_request(sc->hdev, report->report_id, (u8 *)report,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001889 sizeof(struct sixaxis_output_report),
1890 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001891}
1892
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001893static void dualshock4_send_output_report(struct sony_sc *sc)
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001894{
Frank Praznik0da8ea62014-01-16 21:42:51 -05001895 struct hid_device *hdev = sc->hdev;
Pavel Machek1adf9042016-02-09 13:55:08 +01001896 u8 *buf = sc->output_report_dmabuf;
Frank Praznik48220232014-02-05 20:03:44 -05001897 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001898
Frank Praznikc4425c82016-09-22 20:18:10 -04001899 /*
1900 * NOTE: The buf[1] field of the Bluetooth report controls
1901 * the Dualshock 4 reporting rate.
1902 *
1903 * Known values include:
1904 *
1905 * 0x80 - 1000hz (full speed)
1906 * 0xA0 - 31hz
1907 * 0xB0 - 20hz
1908 * 0xD0 - 66hz
1909 */
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001910 if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07001911 memset(buf, 0, DS4_OUTPUT_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001912 buf[0] = 0x05;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001913 buf[1] = 0xFF;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001914 offset = 4;
1915 } else {
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07001916 memset(buf, 0, DS4_OUTPUT_REPORT_0x11_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001917 buf[0] = 0x11;
Roderick Colenbrandere7ef53a2016-10-07 12:39:37 -07001918 buf[1] = 0xC0; /* HID + CRC */
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001919 buf[3] = 0x0F;
1920 offset = 6;
1921 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001922
1923#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001924 buf[offset++] = sc->right;
1925 buf[offset++] = sc->left;
1926#else
1927 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001928#endif
1929
Frank Praznikb3ed4582014-04-14 10:11:36 -04001930 /* LED 3 is the global control */
1931 if (sc->led_state[3]) {
1932 buf[offset++] = sc->led_state[0];
1933 buf[offset++] = sc->led_state[1];
1934 buf[offset++] = sc->led_state[2];
1935 } else {
1936 offset += 3;
1937 }
1938
1939 /* If both delay values are zero the DualShock 4 disables blinking. */
1940 buf[offset++] = sc->led_delay_on[3];
1941 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05001942
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001943 if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE))
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07001944 hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x05_SIZE);
Roderick Colenbrandere7ef53a2016-10-07 12:39:37 -07001945 else {
1946 /* CRC generation */
1947 u8 bthdr = 0xA2;
1948 u32 crc;
1949
1950 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
1951 crc = ~crc32_le(crc, buf, DS4_OUTPUT_REPORT_0x11_SIZE-4);
1952 put_unaligned_le32(crc, &buf[74]);
1953 hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x11_SIZE);
1954 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001955}
1956
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001957static void motion_send_output_report(struct sony_sc *sc)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001958{
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001959 struct hid_device *hdev = sc->hdev;
1960 struct motion_output_report_02 *report =
1961 (struct motion_output_report_02 *)sc->output_report_dmabuf;
1962
Simon Wood41d2d422015-06-09 21:27:06 -06001963 memset(report, 0, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001964
1965 report->type = 0x02; /* set leds */
1966 report->r = sc->led_state[0];
1967 report->g = sc->led_state[1];
1968 report->b = sc->led_state[2];
1969
1970#ifdef CONFIG_SONY_FF
1971 report->rumble = max(sc->right, sc->left);
1972#endif
1973
Pavel Machek1adf9042016-02-09 13:55:08 +01001974 hid_hw_output_report(hdev, (u8 *)report, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001975}
1976
Frank Praznikdecd9462015-11-11 09:49:38 -05001977static inline void sony_send_output_report(struct sony_sc *sc)
1978{
1979 if (sc->send_output_report)
1980 sc->send_output_report(sc);
1981}
1982
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001983static void sony_state_worker(struct work_struct *work)
1984{
1985 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Antonio Ospiteef916ef2016-02-09 13:55:07 +01001986
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001987 sc->send_output_report(sc);
1988}
1989
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001990static int sony_allocate_output_report(struct sony_sc *sc)
1991{
Simon Wood4545ee02015-06-17 00:08:52 -06001992 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
1993 (sc->quirks & NAVIGATION_CONTROLLER))
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001994 sc->output_report_dmabuf =
1995 kmalloc(sizeof(union sixaxis_output_report_01),
1996 GFP_KERNEL);
1997 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07001998 sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x11_SIZE,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001999 GFP_KERNEL);
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08002000 else if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE))
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07002001 sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x05_SIZE,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002002 GFP_KERNEL);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002003 else if (sc->quirks & MOTION_CONTROLLER)
Simon Wood41d2d422015-06-09 21:27:06 -06002004 sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE,
2005 GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002006 else
2007 return 0;
2008
2009 if (!sc->output_report_dmabuf)
2010 return -ENOMEM;
2011
2012 return 0;
2013}
2014
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002015#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002016static int sony_play_effect(struct input_dev *dev, void *data,
2017 struct ff_effect *effect)
2018{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002019 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002020 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002021
2022 if (effect->type != FF_RUMBLE)
2023 return 0;
2024
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002025 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002026 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002027
Frank Praznik2a242932016-09-22 20:18:09 -04002028 sony_schedule_work(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002029 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002030}
2031
Frank Praznikfa57a812014-04-14 10:11:33 -04002032static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002033{
Frank Praznikfa57a812014-04-14 10:11:33 -04002034 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002035 struct hid_input, list);
2036 struct input_dev *input_dev = hidinput->input;
2037
2038 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
2039 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
2040}
2041
2042#else
Frank Praznikfa57a812014-04-14 10:11:33 -04002043static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002044{
2045 return 0;
2046}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002047
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002048#endif
2049
Frank Praznikd902f472014-01-27 10:17:36 -05002050static int sony_battery_get_property(struct power_supply *psy,
2051 enum power_supply_property psp,
2052 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002053{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002054 struct sony_sc *sc = power_supply_get_drvdata(psy);
Frank Praznikd902f472014-01-27 10:17:36 -05002055 unsigned long flags;
2056 int ret = 0;
2057 u8 battery_charging, battery_capacity, cable_state;
2058
2059 spin_lock_irqsave(&sc->lock, flags);
2060 battery_charging = sc->battery_charging;
2061 battery_capacity = sc->battery_capacity;
2062 cable_state = sc->cable_state;
2063 spin_unlock_irqrestore(&sc->lock, flags);
2064
2065 switch (psp) {
2066 case POWER_SUPPLY_PROP_PRESENT:
2067 val->intval = 1;
2068 break;
2069 case POWER_SUPPLY_PROP_SCOPE:
2070 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
2071 break;
2072 case POWER_SUPPLY_PROP_CAPACITY:
2073 val->intval = battery_capacity;
2074 break;
2075 case POWER_SUPPLY_PROP_STATUS:
2076 if (battery_charging)
2077 val->intval = POWER_SUPPLY_STATUS_CHARGING;
2078 else
2079 if (battery_capacity == 100 && cable_state)
2080 val->intval = POWER_SUPPLY_STATUS_FULL;
2081 else
2082 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
2083 break;
2084 default:
2085 ret = -EINVAL;
2086 break;
2087 }
2088 return ret;
2089}
2090
Frank Praznik0f398232016-09-22 20:18:08 -04002091static int sony_battery_probe(struct sony_sc *sc, int append_dev_id)
Frank Praznikd902f472014-01-27 10:17:36 -05002092{
Frank Praznik0f398232016-09-22 20:18:08 -04002093 const char *battery_str_fmt = append_dev_id ?
2094 "sony_controller_battery_%pMR_%i" :
2095 "sony_controller_battery_%pMR";
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002096 struct power_supply_config psy_cfg = { .drv_data = sc, };
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002097 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05002098 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002099
Frank Praznikad142b92014-02-20 11:36:00 -05002100 /*
2101 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05002102 * if the battery is polled before the first device report is received.
2103 */
2104 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002105
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002106 sc->battery_desc.properties = sony_battery_props;
2107 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
2108 sc->battery_desc.get_property = sony_battery_get_property;
2109 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
2110 sc->battery_desc.use_for_apm = 0;
Frank Praznik0f398232016-09-22 20:18:08 -04002111 sc->battery_desc.name = kasprintf(GFP_KERNEL, battery_str_fmt,
2112 sc->mac_address, sc->device_id);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002113 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05002114 return -ENOMEM;
2115
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002116 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
2117 &psy_cfg);
2118 if (IS_ERR(sc->battery)) {
2119 ret = PTR_ERR(sc->battery);
Frank Praznikd902f472014-01-27 10:17:36 -05002120 hid_err(hdev, "Unable to register battery device\n");
2121 goto err_free;
2122 }
2123
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002124 power_supply_powers(sc->battery, &hdev->dev);
Frank Praznikd902f472014-01-27 10:17:36 -05002125 return 0;
2126
2127err_free:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002128 kfree(sc->battery_desc.name);
2129 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05002130 return ret;
2131}
2132
2133static void sony_battery_remove(struct sony_sc *sc)
2134{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002135 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05002136 return;
2137
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002138 power_supply_unregister(sc->battery);
2139 kfree(sc->battery_desc.name);
2140 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05002141}
2142
Frank Praznikd2d782f2014-02-20 11:36:03 -05002143/*
2144 * If a controller is plugged in via USB while already connected via Bluetooth
2145 * it will show up as two devices. A global list of connected controllers and
2146 * their MAC addresses is maintained to ensure that a device is only connected
2147 * once.
Frank Praznik0f398232016-09-22 20:18:08 -04002148 *
2149 * Some USB-only devices masquerade as Sixaxis controllers and all have the
2150 * same dummy Bluetooth address, so a comparison of the connection type is
2151 * required. Devices are only rejected in the case where two devices have
2152 * matching Bluetooth addresses on different bus types.
Frank Praznikd2d782f2014-02-20 11:36:03 -05002153 */
Frank Praznik0f398232016-09-22 20:18:08 -04002154static inline int sony_compare_connection_type(struct sony_sc *sc0,
2155 struct sony_sc *sc1)
2156{
2157 const int sc0_not_bt = !(sc0->quirks & SONY_BT_DEVICE);
2158 const int sc1_not_bt = !(sc1->quirks & SONY_BT_DEVICE);
2159
2160 return sc0_not_bt == sc1_not_bt;
2161}
2162
Frank Praznikd2d782f2014-02-20 11:36:03 -05002163static int sony_check_add_dev_list(struct sony_sc *sc)
2164{
2165 struct sony_sc *entry;
2166 unsigned long flags;
2167 int ret;
2168
2169 spin_lock_irqsave(&sony_dev_list_lock, flags);
2170
2171 list_for_each_entry(entry, &sony_device_list, list_node) {
2172 ret = memcmp(sc->mac_address, entry->mac_address,
2173 sizeof(sc->mac_address));
2174 if (!ret) {
Frank Praznik0f398232016-09-22 20:18:08 -04002175 if (sony_compare_connection_type(sc, entry)) {
2176 ret = 1;
2177 } else {
2178 ret = -EEXIST;
2179 hid_info(sc->hdev,
2180 "controller with MAC address %pMR already connected\n",
Frank Praznikd2d782f2014-02-20 11:36:03 -05002181 sc->mac_address);
Frank Praznik0f398232016-09-22 20:18:08 -04002182 }
Frank Praznikd2d782f2014-02-20 11:36:03 -05002183 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002184 }
2185 }
2186
Frank Praznikd2d782f2014-02-20 11:36:03 -05002187 ret = 0;
2188 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002189
Frank Praznikd2d782f2014-02-20 11:36:03 -05002190unlock:
2191 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2192 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002193}
2194
Frank Praznikd2d782f2014-02-20 11:36:03 -05002195static void sony_remove_dev_list(struct sony_sc *sc)
2196{
2197 unsigned long flags;
2198
2199 if (sc->list_node.next) {
2200 spin_lock_irqsave(&sony_dev_list_lock, flags);
2201 list_del(&(sc->list_node));
2202 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2203 }
2204}
2205
2206static int sony_get_bt_devaddr(struct sony_sc *sc)
2207{
2208 int ret;
2209
2210 /* HIDP stores the device MAC address as a string in the uniq field. */
2211 ret = strlen(sc->hdev->uniq);
2212 if (ret != 17)
2213 return -EINVAL;
2214
2215 ret = sscanf(sc->hdev->uniq,
2216 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2217 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
2218 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
2219
2220 if (ret != 6)
2221 return -EINVAL;
2222
2223 return 0;
2224}
2225
2226static int sony_check_add(struct sony_sc *sc)
2227{
Pavel Machek1adf9042016-02-09 13:55:08 +01002228 u8 *buf = NULL;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002229 int n, ret;
2230
2231 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
Simon Wood12e9a6d72015-06-09 21:27:05 -06002232 (sc->quirks & MOTION_CONTROLLER_BT) ||
Simon Wood4545ee02015-06-17 00:08:52 -06002233 (sc->quirks & NAVIGATION_CONTROLLER_BT) ||
Frank Praznikd2d782f2014-02-20 11:36:03 -05002234 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
2235 /*
2236 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
2237 * address from the uniq string where HIDP stores it.
2238 * As uniq cannot be guaranteed to be a MAC address in all cases
2239 * a failure of this function should not prevent the connection.
2240 */
2241 if (sony_get_bt_devaddr(sc) < 0) {
2242 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
2243 return 0;
2244 }
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08002245 } else if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07002246 buf = kmalloc(DS4_FEATURE_REPORT_0x81_SIZE, GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002247 if (!buf)
2248 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002249
2250 /*
2251 * The MAC address of a DS4 controller connected via USB can be
2252 * retrieved with feature report 0x81. The address begins at
2253 * offset 1.
2254 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002255 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07002256 DS4_FEATURE_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002257 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002258
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07002259 if (ret != DS4_FEATURE_REPORT_0x81_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002260 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002261 ret = ret < 0 ? ret : -EINVAL;
2262 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002263 }
2264
2265 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
Roderick Colenbranderc70d5f72016-12-08 19:09:51 -08002266
2267 snprintf(sc->hdev->uniq, sizeof(sc->hdev->uniq),
2268 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2269 sc->mac_address[5], sc->mac_address[4],
2270 sc->mac_address[3], sc->mac_address[2],
2271 sc->mac_address[1], sc->mac_address[0]);
Simon Wood4545ee02015-06-17 00:08:52 -06002272 } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2273 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002274 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2275 if (!buf)
2276 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002277
2278 /*
2279 * The MAC address of a Sixaxis controller connected via USB can
2280 * be retrieved with feature report 0xf2. The address begins at
2281 * offset 4.
2282 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002283 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2284 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2285 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002286
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002287 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002288 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002289 ret = ret < 0 ? ret : -EINVAL;
2290 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002291 }
2292
2293 /*
2294 * The Sixaxis device MAC in the report is big-endian and must
2295 * be byte-swapped.
2296 */
2297 for (n = 0; n < 6; n++)
2298 sc->mac_address[5-n] = buf[4+n];
2299 } else {
2300 return 0;
2301 }
2302
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002303 ret = sony_check_add_dev_list(sc);
2304
2305out_free:
2306
2307 kfree(buf);
2308
2309 return ret;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002310}
2311
Frank Praznik80250872014-04-14 10:11:35 -04002312static int sony_set_device_id(struct sony_sc *sc)
2313{
2314 int ret;
2315
2316 /*
2317 * Only DualShock 4 or Sixaxis controllers get an id.
2318 * All others are set to -1.
2319 */
2320 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2321 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2322 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2323 GFP_KERNEL);
2324 if (ret < 0) {
2325 sc->device_id = -1;
2326 return ret;
2327 }
2328 sc->device_id = ret;
2329 } else {
2330 sc->device_id = -1;
2331 }
2332
2333 return 0;
2334}
2335
2336static void sony_release_device_id(struct sony_sc *sc)
2337{
2338 if (sc->device_id >= 0) {
2339 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2340 sc->device_id = -1;
2341 }
2342}
2343
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002344static inline void sony_init_output_report(struct sony_sc *sc,
Antonio Ospite09593e32016-02-09 13:55:06 +01002345 void (*send_output_report)(struct sony_sc *))
Frank Praznik46262042014-04-14 10:11:31 -04002346{
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002347 sc->send_output_report = send_output_report;
2348
Frank Praznik46262042014-04-14 10:11:31 -04002349 if (!sc->worker_initialized)
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002350 INIT_WORK(&sc->state_worker, sony_state_worker);
Frank Praznik46262042014-04-14 10:11:31 -04002351
2352 sc->worker_initialized = 1;
2353}
2354
2355static inline void sony_cancel_work_sync(struct sony_sc *sc)
2356{
2357 if (sc->worker_initialized)
2358 cancel_work_sync(&sc->state_worker);
2359}
Frank Praznikd2d782f2014-02-20 11:36:03 -05002360
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002361static int sony_input_configured(struct hid_device *hdev,
2362 struct hid_input *hidinput)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002363{
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002364 struct sony_sc *sc = hid_get_drvdata(hdev);
Frank Praznik0f398232016-09-22 20:18:08 -04002365 int append_dev_id;
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002366 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002367
Frank Praznik80250872014-04-14 10:11:35 -04002368 ret = sony_set_device_id(sc);
2369 if (ret < 0) {
2370 hid_err(hdev, "failed to allocate the device id\n");
2371 goto err_stop;
2372 }
2373
Frank Praznik131a8a92015-05-05 20:47:28 -04002374 ret = sony_allocate_output_report(sc);
2375 if (ret < 0) {
2376 hid_err(hdev, "failed to allocate the output report buffer\n");
2377 goto err_stop;
2378 }
2379
Simon Wood4545ee02015-06-17 00:08:52 -06002380 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2381 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002382 /*
2383 * The Sony Sixaxis does not handle HID Output Reports on the
2384 * Interrupt EP like it could, so we need to force HID Output
2385 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2386 *
2387 * There is also another issue about HID Output Reports via USB,
2388 * the Sixaxis does not want the report_id as part of the data
2389 * packet, so we have to discard buf[0] when sending the actual
2390 * control message, even for numbered reports, humpf!
Frank Praznik2a242932016-09-22 20:18:09 -04002391 *
2392 * Additionally, the Sixaxis on USB isn't properly initialized
2393 * until the PS logo button is pressed and as such won't retain
2394 * any state set by an output report, so the initial
2395 * configuration report is deferred until the first input
2396 * report arrives.
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002397 */
2398 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2399 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
Frank Praznik2a242932016-09-22 20:18:09 -04002400 sc->defer_initialization = 1;
Antonio Ospite816651a2010-05-03 22:15:55 +02002401 ret = sixaxis_set_operational_usb(hdev);
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002402 sony_init_output_report(sc, sixaxis_send_output_report);
Simon Wood4545ee02015-06-17 00:08:52 -06002403 } else if ((sc->quirks & SIXAXIS_CONTROLLER_BT) ||
2404 (sc->quirks & NAVIGATION_CONTROLLER_BT)) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04002405 /*
2406 * The Sixaxis wants output reports sent on the ctrl endpoint
2407 * when connected via Bluetooth.
2408 */
2409 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Antonio Ospite816651a2010-05-03 22:15:55 +02002410 ret = sixaxis_set_operational_bt(hdev);
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002411 sony_init_output_report(sc, sixaxis_send_output_report);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002412 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08002413 ret = dualshock4_get_calibration_data(sc);
2414 if (ret < 0) {
2415 hid_err(hdev, "Failed to get calibration data from Dualshock 4\n");
2416 goto err_stop;
Frank Praznik68330d82014-02-05 20:03:49 -05002417 }
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002418
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002419 /*
2420 * The Dualshock 4 touchpad supports 2 touches and has a
2421 * resolution of 1920x942 (44.86 dots/mm).
2422 */
Roderick Colenbranderac797b92016-11-23 14:07:07 -08002423 ret = sony_register_touchpad(sc, 2, 1920, 942);
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002424 if (ret) {
2425 hid_err(sc->hdev,
2426 "Unable to initialize multi-touch slots: %d\n",
2427 ret);
Roderick Colenbrander2b6579d2016-12-08 19:09:50 -08002428 goto err_stop;
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002429 }
2430
Roderick Colenbrander227c0112017-03-07 15:45:00 -08002431 ret = sony_register_sensors(sc);
2432 if (ret) {
2433 hid_err(sc->hdev,
2434 "Unable to initialize motion sensors: %d\n", ret);
2435 goto err_stop;
2436 }
2437
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002438 sony_init_output_report(sc, dualshock4_send_output_report);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002439 } else if (sc->quirks & MOTION_CONTROLLER) {
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002440 sony_init_output_report(sc, motion_send_output_report);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002441 } else {
2442 ret = 0;
2443 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00002444
Jiri Kosina4dfdc462008-12-30 00:49:59 +01002445 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002446 goto err_stop;
2447
Frank Praznik0f398232016-09-22 20:18:08 -04002448 ret = append_dev_id = sony_check_add(sc);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002449 if (ret < 0)
2450 goto err_stop;
2451
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002452 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002453 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002454 if (ret < 0)
2455 goto err_stop;
2456 }
2457
Frank Praznikd902f472014-01-27 10:17:36 -05002458 if (sc->quirks & SONY_BATTERY_SUPPORT) {
Frank Praznik0f398232016-09-22 20:18:08 -04002459 ret = sony_battery_probe(sc, append_dev_id);
Frank Praznikd902f472014-01-27 10:17:36 -05002460 if (ret < 0)
2461 goto err_stop;
2462
2463 /* Open the device to receive reports with battery info */
2464 ret = hid_hw_open(hdev);
2465 if (ret < 0) {
2466 hid_err(hdev, "hw open failed\n");
2467 goto err_stop;
2468 }
2469 }
2470
Frank Praznikc8de9db2014-02-20 11:36:01 -05002471 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002472 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05002473 if (ret < 0)
2474 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05002475 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002476
Jiri Slabybd28ce02008-06-25 23:47:04 +02002477 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05002478err_close:
2479 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002480err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002481 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002482 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002483 if (sc->quirks & SONY_BATTERY_SUPPORT)
2484 sony_battery_remove(sc);
Frank Praznik46262042014-04-14 10:11:31 -04002485 sony_cancel_work_sync(sc);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002486 kfree(sc->output_report_dmabuf);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002487 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04002488 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002489 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002490 return ret;
2491}
2492
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002493static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2494{
2495 int ret;
2496 unsigned long quirks = id->driver_data;
2497 struct sony_sc *sc;
2498 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2499
2500 if (!strcmp(hdev->name, "FutureMax Dance Mat"))
2501 quirks |= FUTUREMAX_DANCE_MAT;
2502
2503 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
2504 if (sc == NULL) {
2505 hid_err(hdev, "can't alloc sony descriptor\n");
2506 return -ENOMEM;
2507 }
2508
2509 spin_lock_init(&sc->lock);
2510
2511 sc->quirks = quirks;
2512 hid_set_drvdata(hdev, sc);
2513 sc->hdev = hdev;
2514
2515 ret = hid_parse(hdev);
2516 if (ret) {
2517 hid_err(hdev, "parse failed\n");
2518 return ret;
2519 }
2520
2521 if (sc->quirks & VAIO_RDESC_CONSTANT)
2522 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2523 else if (sc->quirks & SIXAXIS_CONTROLLER)
2524 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2525
Roderick Colenbrander9131f8c2016-11-23 14:07:08 -08002526 /* Patch the hw version on DS4 compatible devices, so applications can
2527 * distinguish between the default HID mappings and the mappings defined
2528 * by the Linux game controller spec. This is important for the SDL2
2529 * library, which has a game controller database, which uses device ids
2530 * in combination with version as a key.
2531 */
2532 if (sc->quirks & DUALSHOCK4_CONTROLLER)
2533 hdev->version |= 0x8000;
2534
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002535 ret = hid_hw_start(hdev, connect_mask);
2536 if (ret) {
2537 hid_err(hdev, "hw start failed\n");
2538 return ret;
2539 }
2540
Roderick Colenbrander4f967f62016-11-23 14:07:06 -08002541 /* sony_input_configured can fail, but this doesn't result
2542 * in hid_hw_start failures (intended). Check whether
2543 * the HID layer claimed the device else fail.
2544 * We don't know the actual reason for the failure, most
2545 * likely it is due to EEXIST in case of double connection
2546 * of USB and Bluetooth, but could have been due to ENOMEM
2547 * or other reasons as well.
2548 */
2549 if (!(hdev->claimed & HID_CLAIMED_INPUT)) {
2550 hid_err(hdev, "failed to claim input\n");
2551 return -ENODEV;
2552 }
2553
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002554 return ret;
2555}
2556
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002557static void sony_remove(struct hid_device *hdev)
2558{
Colin Leitnerf04d5142013-05-27 23:41:05 +02002559 struct sony_sc *sc = hid_get_drvdata(hdev);
2560
Roderick Colenbranderac797b92016-11-23 14:07:07 -08002561 hid_hw_close(hdev);
2562
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002563 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002564 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02002565
Roderick Colenbranderac797b92016-11-23 14:07:07 -08002566 if (sc->quirks & SONY_BATTERY_SUPPORT)
Frank Praznikd902f472014-01-27 10:17:36 -05002567 sony_battery_remove(sc);
Roderick Colenbranderac797b92016-11-23 14:07:07 -08002568
2569 if (sc->touchpad)
2570 sony_unregister_touchpad(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002571
Roderick Colenbrander227c0112017-03-07 15:45:00 -08002572 if (sc->sensor_dev)
2573 sony_unregister_sensors(sc);
2574
2575 if (sc->sensor_dev)
2576 sony_unregister_sensors(sc);
2577
Frank Praznik46262042014-04-14 10:11:31 -04002578 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002579
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002580 kfree(sc->output_report_dmabuf);
2581
Frank Praznikd2d782f2014-02-20 11:36:03 -05002582 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002583
Frank Praznik80250872014-04-14 10:11:35 -04002584 sony_release_device_id(sc);
2585
Jiri Slabybd28ce02008-06-25 23:47:04 +02002586 hid_hw_stop(hdev);
2587}
2588
Frank Praznikdecd9462015-11-11 09:49:38 -05002589#ifdef CONFIG_PM
2590
2591static int sony_suspend(struct hid_device *hdev, pm_message_t message)
2592{
Frank Praznik765a1072017-02-08 13:58:43 -05002593#ifdef CONFIG_SONY_FF
2594
2595 /* On suspend stop any running force-feedback events */
2596 if (SONY_FF_SUPPORT) {
Frank Praznikdecd9462015-11-11 09:49:38 -05002597 struct sony_sc *sc = hid_get_drvdata(hdev);
2598
Frank Praznikdecd9462015-11-11 09:49:38 -05002599 sc->left = sc->right = 0;
Frank Praznikdecd9462015-11-11 09:49:38 -05002600 sony_send_output_report(sc);
2601 }
2602
Frank Praznik765a1072017-02-08 13:58:43 -05002603#endif
Frank Praznikdecd9462015-11-11 09:49:38 -05002604 return 0;
2605}
2606
2607static int sony_resume(struct hid_device *hdev)
2608{
Frank Praznik765a1072017-02-08 13:58:43 -05002609 struct sony_sc *sc = hid_get_drvdata(hdev);
Frank Praznikdecd9462015-11-11 09:49:38 -05002610
Frank Praznik765a1072017-02-08 13:58:43 -05002611 /*
2612 * The Sixaxis and navigation controllers on USB need to be
2613 * reinitialized on resume or they won't behave properly.
2614 */
2615 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2616 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
2617 sixaxis_set_operational_usb(sc->hdev);
2618 sc->defer_initialization = 1;
Frank Praznikdecd9462015-11-11 09:49:38 -05002619 }
2620
2621 return 0;
2622}
2623
2624#endif
2625
Jiri Slabybd28ce02008-06-25 23:47:04 +02002626static const struct hid_device_id sony_devices[] = {
2627 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2628 .driver_data = SIXAXIS_CONTROLLER_USB },
2629 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
Simon Wood4545ee02015-06-17 00:08:52 -06002630 .driver_data = NAVIGATION_CONTROLLER_USB },
Simon Wood6eabaaa2015-06-17 00:08:51 -06002631 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
Simon Wood4545ee02015-06-17 00:08:52 -06002632 .driver_data = NAVIGATION_CONTROLLER_BT },
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002633 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002634 .driver_data = MOTION_CONTROLLER_USB },
Simon Wooda4afa852015-06-03 09:45:19 -06002635 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002636 .driver_data = MOTION_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002637 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2638 .driver_data = SIXAXIS_CONTROLLER_BT },
2639 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2640 .driver_data = VAIO_RDESC_CONSTANT },
2641 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2642 .driver_data = VAIO_RDESC_CONSTANT },
Antonio Ospiteef916ef2016-02-09 13:55:07 +01002643 /*
2644 * Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2645 * Logitech joystick from the device descriptor.
2646 */
Jiri Slabybd28ce02008-06-25 23:47:04 +02002647 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2648 .driver_data = BUZZ_CONTROLLER },
2649 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2650 .driver_data = BUZZ_CONTROLLER },
2651 /* PS3 BD Remote Control */
2652 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2653 .driver_data = PS3REMOTE },
2654 /* Logitech Harmony Adapter for PS3 */
2655 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2656 .driver_data = PS3REMOTE },
Frank Praznik68a49e52014-11-12 14:52:28 -05002657 /* SMK-Link PS3 BD Remote Control */
2658 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2659 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002660 /* Sony Dualshock 4 controllers for PS4 */
2661 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002662 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002663 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002664 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Roderick Colenbrandercf1015d2016-10-07 12:39:40 -07002665 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
2666 .driver_data = DUALSHOCK4_CONTROLLER_USB },
2667 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
2668 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Roderick Colenbranderde66a1a2016-11-23 14:07:11 -08002669 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE),
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08002670 .driver_data = DUALSHOCK4_DONGLE },
Scott Moreau74500cc2016-01-13 07:40:42 -07002671 /* Nyko Core Controller for PS3 */
2672 { HID_USB_DEVICE(USB_VENDOR_ID_SINO_LITE, USB_DEVICE_ID_SINO_LITE_CONTROLLER),
2673 .driver_data = SIXAXIS_CONTROLLER_USB | SINO_LITE_CONTROLLER },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002674 { }
2675};
2676MODULE_DEVICE_TABLE(hid, sony_devices);
2677
2678static struct hid_driver sony_driver = {
Frank Praznikce8efc32014-09-18 21:15:01 -04002679 .name = "sony",
2680 .id_table = sony_devices,
2681 .input_mapping = sony_mapping,
2682 .input_configured = sony_input_configured,
2683 .probe = sony_probe,
2684 .remove = sony_remove,
2685 .report_fixup = sony_report_fixup,
Frank Praznikdecd9462015-11-11 09:49:38 -05002686 .raw_event = sony_raw_event,
2687
2688#ifdef CONFIG_PM
2689 .suspend = sony_suspend,
2690 .resume = sony_resume,
2691 .reset_resume = sony_resume,
2692#endif
Jiri Slabybd28ce02008-06-25 23:47:04 +02002693};
Frank Praznik80250872014-04-14 10:11:35 -04002694
2695static int __init sony_init(void)
2696{
2697 dbg_hid("Sony:%s\n", __func__);
2698
2699 return hid_register_driver(&sony_driver);
2700}
2701
2702static void __exit sony_exit(void)
2703{
2704 dbg_hid("Sony:%s\n", __func__);
2705
Frank Praznik80250872014-04-14 10:11:35 -04002706 hid_unregister_driver(&sony_driver);
Antonio Ospite6c400652015-02-16 22:58:24 +01002707 ida_destroy(&sony_device_id_allocator);
Frank Praznik80250872014-04-14 10:11:35 -04002708}
2709module_init(sony_init);
2710module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002711
2712MODULE_LICENSE("GPL");