blob: 9bf4e3637ba2652244091ef5089e3e1af2db2dd9 [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>
Jiri Slabybd28ce02008-06-25 23:47:04 +020039
40#include "hid-ids.h"
41
Frank Praznik6c79c182014-01-16 21:43:03 -050042#define VAIO_RDESC_CONSTANT BIT(0)
43#define SIXAXIS_CONTROLLER_USB BIT(1)
44#define SIXAXIS_CONTROLLER_BT BIT(2)
45#define BUZZ_CONTROLLER BIT(3)
46#define PS3REMOTE BIT(4)
Frank Praznik8ab16762014-01-16 21:42:31 -050047#define DUALSHOCK4_CONTROLLER_USB BIT(5)
48#define DUALSHOCK4_CONTROLLER_BT BIT(6)
Simon Woodb3bca322015-06-09 21:27:04 -060049#define MOTION_CONTROLLER_USB BIT(7)
50#define MOTION_CONTROLLER_BT BIT(8)
Simon Wood4545ee02015-06-17 00:08:52 -060051#define NAVIGATION_CONTROLLER_USB BIT(9)
52#define NAVIGATION_CONTROLLER_BT BIT(10)
Scott Moreau74500cc2016-01-13 07:40:42 -070053#define SINO_LITE_CONTROLLER BIT(11)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020054
Frank Praznikfee4e2d2014-02-18 17:22:01 -050055#define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
Simon Woodb3bca322015-06-09 21:27:04 -060056#define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
Simon Wood4545ee02015-06-17 00:08:52 -060057#define NAVIGATION_CONTROLLER (NAVIGATION_CONTROLLER_USB |\
58 NAVIGATION_CONTROLLER_BT)
Frank Praznik68330d82014-02-05 20:03:49 -050059#define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
60 DUALSHOCK4_CONTROLLER_BT)
Frank Praznikfee4e2d2014-02-18 17:22:01 -050061#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
Simon Wood4545ee02015-06-17 00:08:52 -060062 DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER |\
63 NAVIGATION_CONTROLLER)
Simon Wood12e9a6d72015-06-09 21:27:05 -060064#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
Simon Wood4545ee02015-06-17 00:08:52 -060065 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040066#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
67 MOTION_CONTROLLER)
Frank Praznik0f398232016-09-22 20:18:08 -040068#define SONY_BT_DEVICE (SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_BT |\
69 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER_BT)
Frank Praznik60781cf2014-01-11 15:13:15 -050070
71#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010072
Frank Praznik4c3e8292015-05-05 20:47:33 -040073/*
74 * The Sixaxis reports both digital and analog values for each button on the
75 * controller except for Start, Select and the PS button. The controller ends
76 * up reporting 27 axes which causes them to spill over into the multi-touch
77 * axis values. Additionally, the controller only has 20 actual, physical axes
78 * so there are several unused axes in between the used ones.
79 */
Pavel Machek1adf9042016-02-09 13:55:08 +010080static u8 sixaxis_rdesc[] = {
Antonio Ospitefb705a62014-06-24 13:28:42 +020081 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznik4c3e8292015-05-05 20:47:33 -040082 0x09, 0x04, /* Usage (Joystick), */
Antonio Ospitefb705a62014-06-24 13:28:42 +020083 0xA1, 0x01, /* Collection (Application), */
84 0xA1, 0x02, /* Collection (Logical), */
85 0x85, 0x01, /* Report ID (1), */
86 0x75, 0x08, /* Report Size (8), */
87 0x95, 0x01, /* Report Count (1), */
88 0x15, 0x00, /* Logical Minimum (0), */
89 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
90 0x81, 0x03, /* Input (Constant, Variable), */
91 0x75, 0x01, /* Report Size (1), */
92 0x95, 0x13, /* Report Count (19), */
93 0x15, 0x00, /* Logical Minimum (0), */
94 0x25, 0x01, /* Logical Maximum (1), */
95 0x35, 0x00, /* Physical Minimum (0), */
96 0x45, 0x01, /* Physical Maximum (1), */
97 0x05, 0x09, /* Usage Page (Button), */
98 0x19, 0x01, /* Usage Minimum (01h), */
99 0x29, 0x13, /* Usage Maximum (13h), */
100 0x81, 0x02, /* Input (Variable), */
101 0x75, 0x01, /* Report Size (1), */
102 0x95, 0x0D, /* Report Count (13), */
103 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
104 0x81, 0x03, /* Input (Constant, Variable), */
105 0x15, 0x00, /* Logical Minimum (0), */
106 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
107 0x05, 0x01, /* Usage Page (Desktop), */
108 0x09, 0x01, /* Usage (Pointer), */
109 0xA1, 0x00, /* Collection (Physical), */
110 0x75, 0x08, /* Report Size (8), */
111 0x95, 0x04, /* Report Count (4), */
112 0x35, 0x00, /* Physical Minimum (0), */
113 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
114 0x09, 0x30, /* Usage (X), */
115 0x09, 0x31, /* Usage (Y), */
116 0x09, 0x32, /* Usage (Z), */
117 0x09, 0x35, /* Usage (Rz), */
118 0x81, 0x02, /* Input (Variable), */
119 0xC0, /* End Collection, */
120 0x05, 0x01, /* Usage Page (Desktop), */
121 0x95, 0x13, /* Report Count (19), */
122 0x09, 0x01, /* Usage (Pointer), */
123 0x81, 0x02, /* Input (Variable), */
124 0x95, 0x0C, /* Report Count (12), */
125 0x81, 0x01, /* Input (Constant), */
126 0x75, 0x10, /* Report Size (16), */
127 0x95, 0x04, /* Report Count (4), */
128 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
129 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
130 0x09, 0x01, /* Usage (Pointer), */
131 0x81, 0x02, /* Input (Variable), */
132 0xC0, /* End Collection, */
133 0xA1, 0x02, /* Collection (Logical), */
134 0x85, 0x02, /* Report ID (2), */
135 0x75, 0x08, /* Report Size (8), */
136 0x95, 0x30, /* Report Count (48), */
137 0x09, 0x01, /* Usage (Pointer), */
138 0xB1, 0x02, /* Feature (Variable), */
139 0xC0, /* End Collection, */
140 0xA1, 0x02, /* Collection (Logical), */
141 0x85, 0xEE, /* Report ID (238), */
142 0x75, 0x08, /* Report Size (8), */
143 0x95, 0x30, /* Report Count (48), */
144 0x09, 0x01, /* Usage (Pointer), */
145 0xB1, 0x02, /* Feature (Variable), */
146 0xC0, /* End Collection, */
147 0xA1, 0x02, /* Collection (Logical), */
148 0x85, 0xEF, /* Report ID (239), */
149 0x75, 0x08, /* Report Size (8), */
150 0x95, 0x30, /* Report Count (48), */
151 0x09, 0x01, /* Usage (Pointer), */
152 0xB1, 0x02, /* Feature (Variable), */
153 0xC0, /* End Collection, */
154 0xC0 /* End Collection */
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200155};
156
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400157/* PS/3 Motion controller */
Pavel Machek1adf9042016-02-09 13:55:08 +0100158static u8 motion_rdesc[] = {
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400159 0x05, 0x01, /* Usage Page (Desktop), */
160 0x09, 0x04, /* Usage (Joystick), */
161 0xA1, 0x01, /* Collection (Application), */
162 0xA1, 0x02, /* Collection (Logical), */
163 0x85, 0x01, /* Report ID (1), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400164 0x75, 0x01, /* Report Size (1), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600165 0x95, 0x15, /* Report Count (21), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400166 0x15, 0x00, /* Logical Minimum (0), */
167 0x25, 0x01, /* Logical Maximum (1), */
168 0x35, 0x00, /* Physical Minimum (0), */
169 0x45, 0x01, /* Physical Maximum (1), */
170 0x05, 0x09, /* Usage Page (Button), */
171 0x19, 0x01, /* Usage Minimum (01h), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600172 0x29, 0x15, /* Usage Maximum (15h), */
173 0x81, 0x02, /* Input (Variable), * Buttons */
174 0x95, 0x0B, /* Report Count (11), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400175 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600176 0x81, 0x03, /* Input (Constant, Variable), * Padding */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400177 0x15, 0x00, /* Logical Minimum (0), */
178 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
179 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400180 0xA1, 0x00, /* Collection (Physical), */
181 0x75, 0x08, /* Report Size (8), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600182 0x95, 0x01, /* Report Count (1), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400183 0x35, 0x00, /* Physical Minimum (0), */
184 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
185 0x09, 0x30, /* Usage (X), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600186 0x81, 0x02, /* Input (Variable), * Trigger */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400187 0xC0, /* End Collection, */
Simon Wood8b2513c2015-06-09 21:27:07 -0600188 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
189 0x75, 0x08, /* Report Size (8), */
190 0x95, 0x07, /* Report Count (7), * skip 7 bytes */
191 0x81, 0x02, /* Input (Variable), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400192 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400193 0x75, 0x10, /* Report Size (16), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600194 0x46, 0xFF, 0xFF, /* Physical Maximum (65535), */
195 0x27, 0xFF, 0xFF, 0x00, 0x00, /* Logical Maximum (65535), */
196 0x95, 0x03, /* Report Count (3), * 3x Accels */
197 0x09, 0x33, /* Usage (rX), */
198 0x09, 0x34, /* Usage (rY), */
199 0x09, 0x35, /* Usage (rZ), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400200 0x81, 0x02, /* Input (Variable), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600201 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
202 0x95, 0x03, /* Report Count (3), * Skip Accels 2nd frame */
203 0x81, 0x02, /* Input (Variable), */
204 0x05, 0x01, /* Usage Page (Desktop), */
205 0x09, 0x01, /* Usage (Pointer), */
206 0x95, 0x03, /* Report Count (3), * 3x Gyros */
207 0x81, 0x02, /* Input (Variable), */
208 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
209 0x95, 0x03, /* Report Count (3), * Skip Gyros 2nd frame */
210 0x81, 0x02, /* Input (Variable), */
211 0x75, 0x0C, /* Report Size (12), */
212 0x46, 0xFF, 0x0F, /* Physical Maximum (4095), */
213 0x26, 0xFF, 0x0F, /* Logical Maximum (4095), */
214 0x95, 0x04, /* Report Count (4), * Skip Temp and Magnetometers */
215 0x81, 0x02, /* Input (Variable), */
216 0x75, 0x08, /* Report Size (8), */
217 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
218 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
219 0x95, 0x06, /* Report Count (6), * Skip Timestamp and Extension Bytes */
220 0x81, 0x02, /* Input (Variable), */
221 0x75, 0x08, /* Report Size (8), */
222 0x95, 0x30, /* Report Count (48), */
223 0x09, 0x01, /* Usage (Pointer), */
224 0x91, 0x02, /* Output (Variable), */
225 0x75, 0x08, /* Report Size (8), */
226 0x95, 0x30, /* Report Count (48), */
227 0x09, 0x01, /* Usage (Pointer), */
228 0xB1, 0x02, /* Feature (Variable), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400229 0xC0, /* End Collection, */
230 0xA1, 0x02, /* Collection (Logical), */
231 0x85, 0x02, /* Report ID (2), */
232 0x75, 0x08, /* Report Size (8), */
233 0x95, 0x30, /* Report Count (48), */
234 0x09, 0x01, /* Usage (Pointer), */
235 0xB1, 0x02, /* Feature (Variable), */
236 0xC0, /* End Collection, */
237 0xA1, 0x02, /* Collection (Logical), */
238 0x85, 0xEE, /* Report ID (238), */
239 0x75, 0x08, /* Report Size (8), */
240 0x95, 0x30, /* Report Count (48), */
241 0x09, 0x01, /* Usage (Pointer), */
242 0xB1, 0x02, /* Feature (Variable), */
243 0xC0, /* End Collection, */
244 0xA1, 0x02, /* Collection (Logical), */
245 0x85, 0xEF, /* Report ID (239), */
246 0x75, 0x08, /* Report Size (8), */
247 0x95, 0x30, /* Report Count (48), */
248 0x09, 0x01, /* Usage (Pointer), */
249 0xB1, 0x02, /* Feature (Variable), */
250 0xC0, /* End Collection, */
251 0xC0 /* End Collection */
252};
253
Simon Woodb2723eb2015-06-17 00:08:53 -0600254/* PS/3 Navigation controller */
Pavel Machek1adf9042016-02-09 13:55:08 +0100255static u8 navigation_rdesc[] = {
Simon Woodb2723eb2015-06-17 00:08:53 -0600256 0x05, 0x01, /* Usage Page (Desktop), */
Antonio Ospited5421762016-01-28 18:23:43 +0100257 0x09, 0x04, /* Usage (Joystick), */
Simon Woodb2723eb2015-06-17 00:08:53 -0600258 0xA1, 0x01, /* Collection (Application), */
259 0xA1, 0x02, /* Collection (Logical), */
260 0x85, 0x01, /* Report ID (1), */
261 0x75, 0x08, /* Report Size (8), */
262 0x95, 0x01, /* Report Count (1), */
263 0x15, 0x00, /* Logical Minimum (0), */
264 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
265 0x81, 0x03, /* Input (Constant, Variable), */
266 0x75, 0x01, /* Report Size (1), */
267 0x95, 0x13, /* Report Count (19), */
268 0x15, 0x00, /* Logical Minimum (0), */
269 0x25, 0x01, /* Logical Maximum (1), */
270 0x35, 0x00, /* Physical Minimum (0), */
271 0x45, 0x01, /* Physical Maximum (1), */
272 0x05, 0x09, /* Usage Page (Button), */
273 0x19, 0x01, /* Usage Minimum (01h), */
274 0x29, 0x13, /* Usage Maximum (13h), */
275 0x81, 0x02, /* Input (Variable), */
276 0x75, 0x01, /* Report Size (1), */
277 0x95, 0x0D, /* Report Count (13), */
278 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
279 0x81, 0x03, /* Input (Constant, Variable), */
280 0x15, 0x00, /* Logical Minimum (0), */
281 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
282 0x05, 0x01, /* Usage Page (Desktop), */
283 0x09, 0x01, /* Usage (Pointer), */
284 0xA1, 0x00, /* Collection (Physical), */
285 0x75, 0x08, /* Report Size (8), */
286 0x95, 0x02, /* Report Count (2), */
287 0x35, 0x00, /* Physical Minimum (0), */
288 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
289 0x09, 0x30, /* Usage (X), */
290 0x09, 0x31, /* Usage (Y), */
291 0x81, 0x02, /* Input (Variable), */
292 0xC0, /* End Collection, */
293 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
294 0x95, 0x06, /* Report Count (6), */
295 0x81, 0x03, /* Input (Constant, Variable), */
296 0x05, 0x01, /* Usage Page (Desktop), */
297 0x75, 0x08, /* Report Size (8), */
298 0x95, 0x05, /* Report Count (5), */
299 0x09, 0x01, /* Usage (Pointer), */
300 0x81, 0x02, /* Input (Variable), */
301 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
Simon Wood2259b5bb2015-07-10 00:10:21 -0600302 0x95, 0x01, /* Report Count (1), */
303 0x81, 0x02, /* Input (Variable), */
304 0x05, 0x01, /* Usage Page (Desktop), */
305 0x95, 0x01, /* Report Count (1), */
306 0x09, 0x01, /* Usage (Pointer), */
307 0x81, 0x02, /* Input (Variable), */
308 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
309 0x95, 0x1E, /* Report Count (24), */
Simon Woodb2723eb2015-06-17 00:08:53 -0600310 0x81, 0x02, /* Input (Variable), */
311 0x75, 0x08, /* Report Size (8), */
312 0x95, 0x30, /* Report Count (48), */
313 0x09, 0x01, /* Usage (Pointer), */
314 0x91, 0x02, /* Output (Variable), */
315 0x75, 0x08, /* Report Size (8), */
316 0x95, 0x30, /* Report Count (48), */
317 0x09, 0x01, /* Usage (Pointer), */
318 0xB1, 0x02, /* Feature (Variable), */
319 0xC0, /* End Collection, */
320 0xA1, 0x02, /* Collection (Logical), */
321 0x85, 0x02, /* Report ID (2), */
322 0x75, 0x08, /* Report Size (8), */
323 0x95, 0x30, /* Report Count (48), */
324 0x09, 0x01, /* Usage (Pointer), */
325 0xB1, 0x02, /* Feature (Variable), */
326 0xC0, /* End Collection, */
327 0xA1, 0x02, /* Collection (Logical), */
328 0x85, 0xEE, /* Report ID (238), */
329 0x75, 0x08, /* Report Size (8), */
330 0x95, 0x30, /* Report Count (48), */
331 0x09, 0x01, /* Usage (Pointer), */
332 0xB1, 0x02, /* Feature (Variable), */
333 0xC0, /* End Collection, */
334 0xA1, 0x02, /* Collection (Logical), */
335 0x85, 0xEF, /* Report ID (239), */
336 0x75, 0x08, /* Report Size (8), */
337 0x95, 0x30, /* Report Count (48), */
338 0x09, 0x01, /* Usage (Pointer), */
339 0xB1, 0x02, /* Feature (Variable), */
340 0xC0, /* End Collection, */
341 0xC0 /* End Collection */
342};
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400343
Frank Praznikad142b92014-02-20 11:36:00 -0500344/*
345 * The default descriptor doesn't provide mapping for the accelerometers
Frank Praznik58d70272014-01-20 12:27:01 -0500346 * or orientation sensors. This fixed descriptor maps the accelerometers
347 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
348 * to usage values 0x43, 0x44 and 0x45.
349 */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500350static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -0500351 0x05, 0x01, /* Usage Page (Desktop), */
352 0x09, 0x05, /* Usage (Gamepad), */
353 0xA1, 0x01, /* Collection (Application), */
354 0x85, 0x01, /* Report ID (1), */
355 0x09, 0x30, /* Usage (X), */
356 0x09, 0x31, /* Usage (Y), */
357 0x09, 0x32, /* Usage (Z), */
358 0x09, 0x35, /* Usage (Rz), */
359 0x15, 0x00, /* Logical Minimum (0), */
360 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
361 0x75, 0x08, /* Report Size (8), */
362 0x95, 0x04, /* Report Count (4), */
363 0x81, 0x02, /* Input (Variable), */
364 0x09, 0x39, /* Usage (Hat Switch), */
365 0x15, 0x00, /* Logical Minimum (0), */
366 0x25, 0x07, /* Logical Maximum (7), */
367 0x35, 0x00, /* Physical Minimum (0), */
368 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
369 0x65, 0x14, /* Unit (Degrees), */
370 0x75, 0x04, /* Report Size (4), */
371 0x95, 0x01, /* Report Count (1), */
372 0x81, 0x42, /* Input (Variable, Null State), */
373 0x65, 0x00, /* Unit, */
374 0x05, 0x09, /* Usage Page (Button), */
375 0x19, 0x01, /* Usage Minimum (01h), */
376 0x29, 0x0E, /* Usage Maximum (0Eh), */
377 0x15, 0x00, /* Logical Minimum (0), */
378 0x25, 0x01, /* Logical Maximum (1), */
379 0x75, 0x01, /* Report Size (1), */
380 0x95, 0x0E, /* Report Count (14), */
381 0x81, 0x02, /* Input (Variable), */
382 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
383 0x09, 0x20, /* Usage (20h), */
384 0x75, 0x06, /* Report Size (6), */
385 0x95, 0x01, /* Report Count (1), */
386 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400387 0x25, 0x3F, /* Logical Maximum (63), */
Frank Praznik58d70272014-01-20 12:27:01 -0500388 0x81, 0x02, /* Input (Variable), */
389 0x05, 0x01, /* Usage Page (Desktop), */
390 0x09, 0x33, /* Usage (Rx), */
391 0x09, 0x34, /* Usage (Ry), */
392 0x15, 0x00, /* Logical Minimum (0), */
393 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
394 0x75, 0x08, /* Report Size (8), */
395 0x95, 0x02, /* Report Count (2), */
396 0x81, 0x02, /* Input (Variable), */
397 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
398 0x09, 0x21, /* Usage (21h), */
399 0x95, 0x03, /* Report Count (3), */
400 0x81, 0x02, /* Input (Variable), */
401 0x05, 0x01, /* Usage Page (Desktop), */
402 0x19, 0x40, /* Usage Minimum (40h), */
403 0x29, 0x42, /* Usage Maximum (42h), */
404 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
405 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
406 0x75, 0x10, /* Report Size (16), */
407 0x95, 0x03, /* Report Count (3), */
408 0x81, 0x02, /* Input (Variable), */
409 0x19, 0x43, /* Usage Minimum (43h), */
410 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400411 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
412 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznik58d70272014-01-20 12:27:01 -0500413 0x95, 0x03, /* Report Count (3), */
414 0x81, 0x02, /* Input (Variable), */
415 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
416 0x09, 0x21, /* Usage (21h), */
417 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400418 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
Frank Praznik58d70272014-01-20 12:27:01 -0500419 0x75, 0x08, /* Report Size (8), */
420 0x95, 0x27, /* Report Count (39), */
421 0x81, 0x02, /* Input (Variable), */
422 0x85, 0x05, /* Report ID (5), */
423 0x09, 0x22, /* Usage (22h), */
424 0x95, 0x1F, /* Report Count (31), */
425 0x91, 0x02, /* Output (Variable), */
426 0x85, 0x04, /* Report ID (4), */
427 0x09, 0x23, /* Usage (23h), */
428 0x95, 0x24, /* Report Count (36), */
429 0xB1, 0x02, /* Feature (Variable), */
430 0x85, 0x02, /* Report ID (2), */
431 0x09, 0x24, /* Usage (24h), */
432 0x95, 0x24, /* Report Count (36), */
433 0xB1, 0x02, /* Feature (Variable), */
434 0x85, 0x08, /* Report ID (8), */
435 0x09, 0x25, /* Usage (25h), */
436 0x95, 0x03, /* Report Count (3), */
437 0xB1, 0x02, /* Feature (Variable), */
438 0x85, 0x10, /* Report ID (16), */
439 0x09, 0x26, /* Usage (26h), */
440 0x95, 0x04, /* Report Count (4), */
441 0xB1, 0x02, /* Feature (Variable), */
442 0x85, 0x11, /* Report ID (17), */
443 0x09, 0x27, /* Usage (27h), */
444 0x95, 0x02, /* Report Count (2), */
445 0xB1, 0x02, /* Feature (Variable), */
446 0x85, 0x12, /* Report ID (18), */
447 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
448 0x09, 0x21, /* Usage (21h), */
449 0x95, 0x0F, /* Report Count (15), */
450 0xB1, 0x02, /* Feature (Variable), */
451 0x85, 0x13, /* Report ID (19), */
452 0x09, 0x22, /* Usage (22h), */
453 0x95, 0x16, /* Report Count (22), */
454 0xB1, 0x02, /* Feature (Variable), */
455 0x85, 0x14, /* Report ID (20), */
456 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
457 0x09, 0x20, /* Usage (20h), */
458 0x95, 0x10, /* Report Count (16), */
459 0xB1, 0x02, /* Feature (Variable), */
460 0x85, 0x15, /* Report ID (21), */
461 0x09, 0x21, /* Usage (21h), */
462 0x95, 0x2C, /* Report Count (44), */
463 0xB1, 0x02, /* Feature (Variable), */
464 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
465 0x85, 0x80, /* Report ID (128), */
466 0x09, 0x20, /* Usage (20h), */
467 0x95, 0x06, /* Report Count (6), */
468 0xB1, 0x02, /* Feature (Variable), */
469 0x85, 0x81, /* Report ID (129), */
470 0x09, 0x21, /* Usage (21h), */
471 0x95, 0x06, /* Report Count (6), */
472 0xB1, 0x02, /* Feature (Variable), */
473 0x85, 0x82, /* Report ID (130), */
474 0x09, 0x22, /* Usage (22h), */
475 0x95, 0x05, /* Report Count (5), */
476 0xB1, 0x02, /* Feature (Variable), */
477 0x85, 0x83, /* Report ID (131), */
478 0x09, 0x23, /* Usage (23h), */
479 0x95, 0x01, /* Report Count (1), */
480 0xB1, 0x02, /* Feature (Variable), */
481 0x85, 0x84, /* Report ID (132), */
482 0x09, 0x24, /* Usage (24h), */
483 0x95, 0x04, /* Report Count (4), */
484 0xB1, 0x02, /* Feature (Variable), */
485 0x85, 0x85, /* Report ID (133), */
486 0x09, 0x25, /* Usage (25h), */
487 0x95, 0x06, /* Report Count (6), */
488 0xB1, 0x02, /* Feature (Variable), */
489 0x85, 0x86, /* Report ID (134), */
490 0x09, 0x26, /* Usage (26h), */
491 0x95, 0x06, /* Report Count (6), */
492 0xB1, 0x02, /* Feature (Variable), */
493 0x85, 0x87, /* Report ID (135), */
494 0x09, 0x27, /* Usage (27h), */
495 0x95, 0x23, /* Report Count (35), */
496 0xB1, 0x02, /* Feature (Variable), */
497 0x85, 0x88, /* Report ID (136), */
498 0x09, 0x28, /* Usage (28h), */
499 0x95, 0x22, /* Report Count (34), */
500 0xB1, 0x02, /* Feature (Variable), */
501 0x85, 0x89, /* Report ID (137), */
502 0x09, 0x29, /* Usage (29h), */
503 0x95, 0x02, /* Report Count (2), */
504 0xB1, 0x02, /* Feature (Variable), */
505 0x85, 0x90, /* Report ID (144), */
506 0x09, 0x30, /* Usage (30h), */
507 0x95, 0x05, /* Report Count (5), */
508 0xB1, 0x02, /* Feature (Variable), */
509 0x85, 0x91, /* Report ID (145), */
510 0x09, 0x31, /* Usage (31h), */
511 0x95, 0x03, /* Report Count (3), */
512 0xB1, 0x02, /* Feature (Variable), */
513 0x85, 0x92, /* Report ID (146), */
514 0x09, 0x32, /* Usage (32h), */
515 0x95, 0x03, /* Report Count (3), */
516 0xB1, 0x02, /* Feature (Variable), */
517 0x85, 0x93, /* Report ID (147), */
518 0x09, 0x33, /* Usage (33h), */
519 0x95, 0x0C, /* Report Count (12), */
520 0xB1, 0x02, /* Feature (Variable), */
521 0x85, 0xA0, /* Report ID (160), */
522 0x09, 0x40, /* Usage (40h), */
523 0x95, 0x06, /* Report Count (6), */
524 0xB1, 0x02, /* Feature (Variable), */
525 0x85, 0xA1, /* Report ID (161), */
526 0x09, 0x41, /* Usage (41h), */
527 0x95, 0x01, /* Report Count (1), */
528 0xB1, 0x02, /* Feature (Variable), */
529 0x85, 0xA2, /* Report ID (162), */
530 0x09, 0x42, /* Usage (42h), */
531 0x95, 0x01, /* Report Count (1), */
532 0xB1, 0x02, /* Feature (Variable), */
533 0x85, 0xA3, /* Report ID (163), */
534 0x09, 0x43, /* Usage (43h), */
535 0x95, 0x30, /* Report Count (48), */
536 0xB1, 0x02, /* Feature (Variable), */
537 0x85, 0xA4, /* Report ID (164), */
538 0x09, 0x44, /* Usage (44h), */
539 0x95, 0x0D, /* Report Count (13), */
540 0xB1, 0x02, /* Feature (Variable), */
541 0x85, 0xA5, /* Report ID (165), */
542 0x09, 0x45, /* Usage (45h), */
543 0x95, 0x15, /* Report Count (21), */
544 0xB1, 0x02, /* Feature (Variable), */
545 0x85, 0xA6, /* Report ID (166), */
546 0x09, 0x46, /* Usage (46h), */
547 0x95, 0x15, /* Report Count (21), */
548 0xB1, 0x02, /* Feature (Variable), */
549 0x85, 0xF0, /* Report ID (240), */
550 0x09, 0x47, /* Usage (47h), */
551 0x95, 0x3F, /* Report Count (63), */
552 0xB1, 0x02, /* Feature (Variable), */
553 0x85, 0xF1, /* Report ID (241), */
554 0x09, 0x48, /* Usage (48h), */
555 0x95, 0x3F, /* Report Count (63), */
556 0xB1, 0x02, /* Feature (Variable), */
557 0x85, 0xF2, /* Report ID (242), */
558 0x09, 0x49, /* Usage (49h), */
559 0x95, 0x0F, /* Report Count (15), */
560 0xB1, 0x02, /* Feature (Variable), */
561 0x85, 0xA7, /* Report ID (167), */
562 0x09, 0x4A, /* Usage (4Ah), */
563 0x95, 0x01, /* Report Count (1), */
564 0xB1, 0x02, /* Feature (Variable), */
565 0x85, 0xA8, /* Report ID (168), */
566 0x09, 0x4B, /* Usage (4Bh), */
567 0x95, 0x01, /* Report Count (1), */
568 0xB1, 0x02, /* Feature (Variable), */
569 0x85, 0xA9, /* Report ID (169), */
570 0x09, 0x4C, /* Usage (4Ch), */
571 0x95, 0x08, /* Report Count (8), */
572 0xB1, 0x02, /* Feature (Variable), */
573 0x85, 0xAA, /* Report ID (170), */
574 0x09, 0x4E, /* Usage (4Eh), */
575 0x95, 0x01, /* Report Count (1), */
576 0xB1, 0x02, /* Feature (Variable), */
577 0x85, 0xAB, /* Report ID (171), */
578 0x09, 0x4F, /* Usage (4Fh), */
579 0x95, 0x39, /* Report Count (57), */
580 0xB1, 0x02, /* Feature (Variable), */
581 0x85, 0xAC, /* Report ID (172), */
582 0x09, 0x50, /* Usage (50h), */
583 0x95, 0x39, /* Report Count (57), */
584 0xB1, 0x02, /* Feature (Variable), */
585 0x85, 0xAD, /* Report ID (173), */
586 0x09, 0x51, /* Usage (51h), */
587 0x95, 0x0B, /* Report Count (11), */
588 0xB1, 0x02, /* Feature (Variable), */
589 0x85, 0xAE, /* Report ID (174), */
590 0x09, 0x52, /* Usage (52h), */
591 0x95, 0x01, /* Report Count (1), */
592 0xB1, 0x02, /* Feature (Variable), */
593 0x85, 0xAF, /* Report ID (175), */
594 0x09, 0x53, /* Usage (53h), */
595 0x95, 0x02, /* Report Count (2), */
596 0xB1, 0x02, /* Feature (Variable), */
597 0x85, 0xB0, /* Report ID (176), */
598 0x09, 0x54, /* Usage (54h), */
599 0x95, 0x3F, /* Report Count (63), */
600 0xB1, 0x02, /* Feature (Variable), */
601 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500602};
603
Frank Praznikad142b92014-02-20 11:36:00 -0500604/*
605 * The default behavior of the Dualshock 4 is to send reports using report
Frank Praznik077147a2014-09-14 11:56:39 -0400606 * type 1 when running over Bluetooth. However, when feature report 2 is
607 * requested during the controller initialization it starts sending input
608 * reports in report 17. Since report 17 is undefined in the default HID
Frank Praznikd8296742014-02-05 20:03:45 -0500609 * descriptor the button and axis definitions must be moved to report 17 or
Frank Praznik077147a2014-09-14 11:56:39 -0400610 * the HID layer won't process the received input.
Frank Praznikd8296742014-02-05 20:03:45 -0500611 */
612static u8 dualshock4_bt_rdesc[] = {
613 0x05, 0x01, /* Usage Page (Desktop), */
614 0x09, 0x05, /* Usage (Gamepad), */
615 0xA1, 0x01, /* Collection (Application), */
616 0x85, 0x01, /* Report ID (1), */
617 0x75, 0x08, /* Report Size (8), */
618 0x95, 0x0A, /* Report Count (9), */
619 0x81, 0x02, /* Input (Variable), */
620 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
621 0x85, 0x02, /* Report ID (2), */
622 0x09, 0x24, /* Usage (24h), */
623 0x95, 0x24, /* Report Count (36), */
624 0xB1, 0x02, /* Feature (Variable), */
625 0x85, 0xA3, /* Report ID (163), */
626 0x09, 0x25, /* Usage (25h), */
627 0x95, 0x30, /* Report Count (48), */
628 0xB1, 0x02, /* Feature (Variable), */
629 0x85, 0x05, /* Report ID (5), */
630 0x09, 0x26, /* Usage (26h), */
631 0x95, 0x28, /* Report Count (40), */
632 0xB1, 0x02, /* Feature (Variable), */
633 0x85, 0x06, /* Report ID (6), */
634 0x09, 0x27, /* Usage (27h), */
635 0x95, 0x34, /* Report Count (52), */
636 0xB1, 0x02, /* Feature (Variable), */
637 0x85, 0x07, /* Report ID (7), */
638 0x09, 0x28, /* Usage (28h), */
639 0x95, 0x30, /* Report Count (48), */
640 0xB1, 0x02, /* Feature (Variable), */
641 0x85, 0x08, /* Report ID (8), */
642 0x09, 0x29, /* Usage (29h), */
643 0x95, 0x2F, /* Report Count (47), */
644 0xB1, 0x02, /* Feature (Variable), */
645 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
646 0x85, 0x03, /* Report ID (3), */
647 0x09, 0x21, /* Usage (21h), */
648 0x95, 0x26, /* Report Count (38), */
649 0xB1, 0x02, /* Feature (Variable), */
650 0x85, 0x04, /* Report ID (4), */
651 0x09, 0x22, /* Usage (22h), */
652 0x95, 0x2E, /* Report Count (46), */
653 0xB1, 0x02, /* Feature (Variable), */
654 0x85, 0xF0, /* Report ID (240), */
655 0x09, 0x47, /* Usage (47h), */
656 0x95, 0x3F, /* Report Count (63), */
657 0xB1, 0x02, /* Feature (Variable), */
658 0x85, 0xF1, /* Report ID (241), */
659 0x09, 0x48, /* Usage (48h), */
660 0x95, 0x3F, /* Report Count (63), */
661 0xB1, 0x02, /* Feature (Variable), */
662 0x85, 0xF2, /* Report ID (242), */
663 0x09, 0x49, /* Usage (49h), */
664 0x95, 0x0F, /* Report Count (15), */
665 0xB1, 0x02, /* Feature (Variable), */
666 0x85, 0x11, /* Report ID (17), */
667 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
668 0x09, 0x20, /* Usage (20h), */
669 0x95, 0x02, /* Report Count (2), */
670 0x81, 0x02, /* Input (Variable), */
671 0x05, 0x01, /* Usage Page (Desktop), */
672 0x09, 0x30, /* Usage (X), */
673 0x09, 0x31, /* Usage (Y), */
674 0x09, 0x32, /* Usage (Z), */
675 0x09, 0x35, /* Usage (Rz), */
676 0x15, 0x00, /* Logical Minimum (0), */
677 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
678 0x75, 0x08, /* Report Size (8), */
679 0x95, 0x04, /* Report Count (4), */
680 0x81, 0x02, /* Input (Variable), */
681 0x09, 0x39, /* Usage (Hat Switch), */
682 0x15, 0x00, /* Logical Minimum (0), */
683 0x25, 0x07, /* Logical Maximum (7), */
684 0x75, 0x04, /* Report Size (4), */
685 0x95, 0x01, /* Report Count (1), */
686 0x81, 0x42, /* Input (Variable, Null State), */
687 0x05, 0x09, /* Usage Page (Button), */
688 0x19, 0x01, /* Usage Minimum (01h), */
689 0x29, 0x0E, /* Usage Maximum (0Eh), */
690 0x15, 0x00, /* Logical Minimum (0), */
691 0x25, 0x01, /* Logical Maximum (1), */
692 0x75, 0x01, /* Report Size (1), */
693 0x95, 0x0E, /* Report Count (14), */
694 0x81, 0x02, /* Input (Variable), */
695 0x75, 0x06, /* Report Size (6), */
696 0x95, 0x01, /* Report Count (1), */
697 0x81, 0x01, /* Input (Constant), */
698 0x05, 0x01, /* Usage Page (Desktop), */
699 0x09, 0x33, /* Usage (Rx), */
700 0x09, 0x34, /* Usage (Ry), */
701 0x15, 0x00, /* Logical Minimum (0), */
702 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
703 0x75, 0x08, /* Report Size (8), */
704 0x95, 0x02, /* Report Count (2), */
705 0x81, 0x02, /* Input (Variable), */
706 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
707 0x09, 0x20, /* Usage (20h), */
708 0x95, 0x03, /* Report Count (3), */
709 0x81, 0x02, /* Input (Variable), */
710 0x05, 0x01, /* Usage Page (Desktop), */
711 0x19, 0x40, /* Usage Minimum (40h), */
712 0x29, 0x42, /* Usage Maximum (42h), */
713 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
714 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
715 0x75, 0x10, /* Report Size (16), */
716 0x95, 0x03, /* Report Count (3), */
717 0x81, 0x02, /* Input (Variable), */
718 0x19, 0x43, /* Usage Minimum (43h), */
719 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400720 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
721 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznikd8296742014-02-05 20:03:45 -0500722 0x95, 0x03, /* Report Count (3), */
723 0x81, 0x02, /* Input (Variable), */
724 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
725 0x09, 0x20, /* Usage (20h), */
726 0x15, 0x00, /* Logical Minimum (0), */
727 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
728 0x75, 0x08, /* Report Size (8), */
729 0x95, 0x31, /* Report Count (51), */
730 0x81, 0x02, /* Input (Variable), */
731 0x09, 0x21, /* Usage (21h), */
732 0x75, 0x08, /* Report Size (8), */
733 0x95, 0x4D, /* Report Count (77), */
734 0x91, 0x02, /* Output (Variable), */
735 0x85, 0x12, /* Report ID (18), */
736 0x09, 0x22, /* Usage (22h), */
737 0x95, 0x8D, /* Report Count (141), */
738 0x81, 0x02, /* Input (Variable), */
739 0x09, 0x23, /* Usage (23h), */
740 0x91, 0x02, /* Output (Variable), */
741 0x85, 0x13, /* Report ID (19), */
742 0x09, 0x24, /* Usage (24h), */
743 0x95, 0xCD, /* Report Count (205), */
744 0x81, 0x02, /* Input (Variable), */
745 0x09, 0x25, /* Usage (25h), */
746 0x91, 0x02, /* Output (Variable), */
747 0x85, 0x14, /* Report ID (20), */
748 0x09, 0x26, /* Usage (26h), */
749 0x96, 0x0D, 0x01, /* Report Count (269), */
750 0x81, 0x02, /* Input (Variable), */
751 0x09, 0x27, /* Usage (27h), */
752 0x91, 0x02, /* Output (Variable), */
753 0x85, 0x15, /* Report ID (21), */
754 0x09, 0x28, /* Usage (28h), */
755 0x96, 0x4D, 0x01, /* Report Count (333), */
756 0x81, 0x02, /* Input (Variable), */
757 0x09, 0x29, /* Usage (29h), */
758 0x91, 0x02, /* Output (Variable), */
759 0x85, 0x16, /* Report ID (22), */
760 0x09, 0x2A, /* Usage (2Ah), */
761 0x96, 0x8D, 0x01, /* Report Count (397), */
762 0x81, 0x02, /* Input (Variable), */
763 0x09, 0x2B, /* Usage (2Bh), */
764 0x91, 0x02, /* Output (Variable), */
765 0x85, 0x17, /* Report ID (23), */
766 0x09, 0x2C, /* Usage (2Ch), */
767 0x96, 0xCD, 0x01, /* Report Count (461), */
768 0x81, 0x02, /* Input (Variable), */
769 0x09, 0x2D, /* Usage (2Dh), */
770 0x91, 0x02, /* Output (Variable), */
771 0x85, 0x18, /* Report ID (24), */
772 0x09, 0x2E, /* Usage (2Eh), */
773 0x96, 0x0D, 0x02, /* Report Count (525), */
774 0x81, 0x02, /* Input (Variable), */
775 0x09, 0x2F, /* Usage (2Fh), */
776 0x91, 0x02, /* Output (Variable), */
777 0x85, 0x19, /* Report ID (25), */
778 0x09, 0x30, /* Usage (30h), */
779 0x96, 0x22, 0x02, /* Report Count (546), */
780 0x81, 0x02, /* Input (Variable), */
781 0x09, 0x31, /* Usage (31h), */
782 0x91, 0x02, /* Output (Variable), */
783 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
784 0x85, 0x82, /* Report ID (130), */
785 0x09, 0x22, /* Usage (22h), */
786 0x95, 0x3F, /* Report Count (63), */
787 0xB1, 0x02, /* Feature (Variable), */
788 0x85, 0x83, /* Report ID (131), */
789 0x09, 0x23, /* Usage (23h), */
790 0xB1, 0x02, /* Feature (Variable), */
791 0x85, 0x84, /* Report ID (132), */
792 0x09, 0x24, /* Usage (24h), */
793 0xB1, 0x02, /* Feature (Variable), */
794 0x85, 0x90, /* Report ID (144), */
795 0x09, 0x30, /* Usage (30h), */
796 0xB1, 0x02, /* Feature (Variable), */
797 0x85, 0x91, /* Report ID (145), */
798 0x09, 0x31, /* Usage (31h), */
799 0xB1, 0x02, /* Feature (Variable), */
800 0x85, 0x92, /* Report ID (146), */
801 0x09, 0x32, /* Usage (32h), */
802 0xB1, 0x02, /* Feature (Variable), */
803 0x85, 0x93, /* Report ID (147), */
804 0x09, 0x33, /* Usage (33h), */
805 0xB1, 0x02, /* Feature (Variable), */
806 0x85, 0xA0, /* Report ID (160), */
807 0x09, 0x40, /* Usage (40h), */
808 0xB1, 0x02, /* Feature (Variable), */
809 0x85, 0xA4, /* Report ID (164), */
810 0x09, 0x44, /* Usage (44h), */
811 0xB1, 0x02, /* Feature (Variable), */
812 0xC0 /* End Collection */
813};
814
Pavel Machek1adf9042016-02-09 13:55:08 +0100815static u8 ps3remote_rdesc[] = {
Jiri Kosina078328d2013-06-13 12:03:49 +0200816 0x05, 0x01, /* GUsagePage Generic Desktop */
817 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
818 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
819
820 /* Use collection 1 for joypad buttons */
821 0xA1, 0x02, /* MCollection Logical (interrelated data) */
822
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100823 /*
824 * Ignore the 1st byte, maybe it is used for a controller
825 * number but it's not needed for correct operation
826 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200827 0x75, 0x08, /* GReportSize 0x08 [8] */
828 0x95, 0x01, /* GReportCount 0x01 [1] */
829 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
830
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100831 /*
832 * Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
833 * buttons multiple keypresses are allowed
834 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200835 0x05, 0x09, /* GUsagePage Button */
836 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
837 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
838 0x14, /* GLogicalMinimum [0] */
839 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
840 0x75, 0x01, /* GReportSize 0x01 [1] */
841 0x95, 0x18, /* GReportCount 0x18 [24] */
842 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
843
844 0xC0, /* MEndCollection */
845
846 /* Use collection 2 for remote control buttons */
847 0xA1, 0x02, /* MCollection Logical (interrelated data) */
848
849 /* 5th byte is used for remote control buttons */
850 0x05, 0x09, /* GUsagePage Button */
851 0x18, /* LUsageMinimum [No button pressed] */
852 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
853 0x14, /* GLogicalMinimum [0] */
854 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
855 0x75, 0x08, /* GReportSize 0x08 [8] */
856 0x95, 0x01, /* GReportCount 0x01 [1] */
857 0x80, /* MInput */
858
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100859 /*
860 * Ignore bytes from 6th to 11th, 6th to 10th are always constant at
861 * 0xff and 11th is for press indication
862 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200863 0x75, 0x08, /* GReportSize 0x08 [8] */
864 0x95, 0x06, /* GReportCount 0x06 [6] */
865 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
866
867 /* 12th byte is for battery strength */
868 0x05, 0x06, /* GUsagePage Generic Device Controls */
869 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
870 0x14, /* GLogicalMinimum [0] */
871 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
872 0x75, 0x08, /* GReportSize 0x08 [8] */
873 0x95, 0x01, /* GReportCount 0x01 [1] */
874 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
875
876 0xC0, /* MEndCollection */
877
878 0xC0 /* MEndCollection [Game Pad] */
879};
880
881static const unsigned int ps3remote_keymap_joypad_buttons[] = {
882 [0x01] = KEY_SELECT,
883 [0x02] = BTN_THUMBL, /* L3 */
884 [0x03] = BTN_THUMBR, /* R3 */
885 [0x04] = BTN_START,
886 [0x05] = KEY_UP,
887 [0x06] = KEY_RIGHT,
888 [0x07] = KEY_DOWN,
889 [0x08] = KEY_LEFT,
890 [0x09] = BTN_TL2, /* L2 */
891 [0x0a] = BTN_TR2, /* R2 */
892 [0x0b] = BTN_TL, /* L1 */
893 [0x0c] = BTN_TR, /* R1 */
894 [0x0d] = KEY_OPTION, /* options/triangle */
895 [0x0e] = KEY_BACK, /* back/circle */
896 [0x0f] = BTN_0, /* cross */
897 [0x10] = KEY_SCREEN, /* view/square */
898 [0x11] = KEY_HOMEPAGE, /* PS button */
899 [0x14] = KEY_ENTER,
900};
901static const unsigned int ps3remote_keymap_remote_buttons[] = {
902 [0x00] = KEY_1,
903 [0x01] = KEY_2,
904 [0x02] = KEY_3,
905 [0x03] = KEY_4,
906 [0x04] = KEY_5,
907 [0x05] = KEY_6,
908 [0x06] = KEY_7,
909 [0x07] = KEY_8,
910 [0x08] = KEY_9,
911 [0x09] = KEY_0,
912 [0x0e] = KEY_ESC, /* return */
913 [0x0f] = KEY_CLEAR,
914 [0x16] = KEY_EJECTCD,
915 [0x1a] = KEY_MENU, /* top menu */
916 [0x28] = KEY_TIME,
917 [0x30] = KEY_PREVIOUS,
918 [0x31] = KEY_NEXT,
919 [0x32] = KEY_PLAY,
920 [0x33] = KEY_REWIND, /* scan back */
921 [0x34] = KEY_FORWARD, /* scan forward */
922 [0x38] = KEY_STOP,
923 [0x39] = KEY_PAUSE,
924 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
925 [0x60] = KEY_FRAMEBACK, /* slow/step back */
926 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
927 [0x63] = KEY_SUBTITLE,
928 [0x64] = KEY_AUDIO,
929 [0x65] = KEY_ANGLE,
930 [0x70] = KEY_INFO, /* display */
931 [0x80] = KEY_BLUE,
932 [0x81] = KEY_RED,
933 [0x82] = KEY_GREEN,
934 [0x83] = KEY_YELLOW,
935};
936
Colin Leitnerf04d5142013-05-27 23:41:05 +0200937static const unsigned int buzz_keymap[] = {
Frank Praznikad142b92014-02-20 11:36:00 -0500938 /*
939 * The controller has 4 remote buzzers, each with one LED and 5
Colin Leitnerf04d5142013-05-27 23:41:05 +0200940 * buttons.
Antonio Ospite09593e32016-02-09 13:55:06 +0100941 *
Colin Leitnerf04d5142013-05-27 23:41:05 +0200942 * We use the mapping chosen by the controller, which is:
943 *
944 * Key Offset
945 * -------------------
946 * Buzz 1
947 * Blue 5
948 * Orange 4
949 * Green 3
950 * Yellow 2
951 *
952 * So, for example, the orange button on the third buzzer is mapped to
953 * BTN_TRIGGER_HAPPY14
954 */
Antonio Ospite09593e32016-02-09 13:55:06 +0100955 [1] = BTN_TRIGGER_HAPPY1,
956 [2] = BTN_TRIGGER_HAPPY2,
957 [3] = BTN_TRIGGER_HAPPY3,
958 [4] = BTN_TRIGGER_HAPPY4,
959 [5] = BTN_TRIGGER_HAPPY5,
960 [6] = BTN_TRIGGER_HAPPY6,
961 [7] = BTN_TRIGGER_HAPPY7,
962 [8] = BTN_TRIGGER_HAPPY8,
963 [9] = BTN_TRIGGER_HAPPY9,
Colin Leitnerf04d5142013-05-27 23:41:05 +0200964 [10] = BTN_TRIGGER_HAPPY10,
965 [11] = BTN_TRIGGER_HAPPY11,
966 [12] = BTN_TRIGGER_HAPPY12,
967 [13] = BTN_TRIGGER_HAPPY13,
968 [14] = BTN_TRIGGER_HAPPY14,
969 [15] = BTN_TRIGGER_HAPPY15,
970 [16] = BTN_TRIGGER_HAPPY16,
971 [17] = BTN_TRIGGER_HAPPY17,
972 [18] = BTN_TRIGGER_HAPPY18,
973 [19] = BTN_TRIGGER_HAPPY19,
974 [20] = BTN_TRIGGER_HAPPY20,
975};
976
Frank Praznikd902f472014-01-27 10:17:36 -0500977static enum power_supply_property sony_battery_props[] = {
978 POWER_SUPPLY_PROP_PRESENT,
979 POWER_SUPPLY_PROP_CAPACITY,
980 POWER_SUPPLY_PROP_SCOPE,
981 POWER_SUPPLY_PROP_STATUS,
982};
983
Frank Praznik55d3b662014-04-14 10:11:32 -0400984struct sixaxis_led {
Pavel Machek1adf9042016-02-09 13:55:08 +0100985 u8 time_enabled; /* the total time the led is active (0xff means forever) */
986 u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
987 u8 enabled;
988 u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
989 u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
Frank Praznik55d3b662014-04-14 10:11:32 -0400990} __packed;
991
992struct sixaxis_rumble {
Pavel Machek1adf9042016-02-09 13:55:08 +0100993 u8 padding;
994 u8 right_duration; /* Right motor duration (0xff means forever) */
995 u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
996 u8 left_duration; /* Left motor duration (0xff means forever) */
997 u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
Frank Praznik55d3b662014-04-14 10:11:32 -0400998} __packed;
999
1000struct sixaxis_output_report {
Pavel Machek1adf9042016-02-09 13:55:08 +01001001 u8 report_id;
Frank Praznik55d3b662014-04-14 10:11:32 -04001002 struct sixaxis_rumble rumble;
Pavel Machek1adf9042016-02-09 13:55:08 +01001003 u8 padding[4];
1004 u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
Frank Praznik55d3b662014-04-14 10:11:32 -04001005 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
1006 struct sixaxis_led _reserved; /* LED5, not actually soldered */
1007} __packed;
1008
1009union sixaxis_output_report_01 {
1010 struct sixaxis_output_report data;
Pavel Machek1adf9042016-02-09 13:55:08 +01001011 u8 buf[36];
Frank Praznik55d3b662014-04-14 10:11:32 -04001012};
1013
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001014struct motion_output_report_02 {
1015 u8 type, zero;
1016 u8 r, g, b;
1017 u8 zero2;
1018 u8 rumble;
1019};
1020
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001021#define DS4_REPORT_0x02_SIZE 37
1022#define DS4_REPORT_0x05_SIZE 32
1023#define DS4_REPORT_0x11_SIZE 78
1024#define DS4_REPORT_0x81_SIZE 7
Antonio Ospite29b691a2015-02-16 18:12:21 +01001025#define SIXAXIS_REPORT_0xF2_SIZE 17
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001026#define SIXAXIS_REPORT_0xF5_SIZE 8
Simon Wood41d2d422015-06-09 21:27:06 -06001027#define MOTION_REPORT_0x02_SIZE 49
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001028
Jiri Kosina8b402c92015-02-23 11:15:44 +01001029static DEFINE_SPINLOCK(sony_dev_list_lock);
Frank Praznikd2d782f2014-02-20 11:36:03 -05001030static LIST_HEAD(sony_device_list);
Frank Praznik80250872014-04-14 10:11:35 -04001031static DEFINE_IDA(sony_device_id_allocator);
Frank Praznikd2d782f2014-02-20 11:36:03 -05001032
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001033struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -05001034 spinlock_t lock;
Frank Praznikd2d782f2014-02-20 11:36:03 -05001035 struct list_head list_node;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001036 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -05001037 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001038 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001039 struct work_struct state_worker;
Antonio Ospite09593e32016-02-09 13:55:06 +01001040 void (*send_output_report)(struct sony_sc *);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001041 struct power_supply *battery;
1042 struct power_supply_desc battery_desc;
Frank Praznik80250872014-04-14 10:11:35 -04001043 int device_id;
Pavel Machek1adf9042016-02-09 13:55:08 +01001044 u8 *output_report_dmabuf;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001045
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001046#ifdef CONFIG_SONY_FF
Pavel Machek1adf9042016-02-09 13:55:08 +01001047 u8 left;
1048 u8 right;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001049#endif
1050
Pavel Machek1adf9042016-02-09 13:55:08 +01001051 u8 mac_address[6];
1052 u8 worker_initialized;
Frank Praznik2a242932016-09-22 20:18:09 -04001053 u8 defer_initialization;
Pavel Machek1adf9042016-02-09 13:55:08 +01001054 u8 cable_state;
1055 u8 battery_charging;
1056 u8 battery_capacity;
1057 u8 led_state[MAX_LEDS];
1058 u8 resume_led_state[MAX_LEDS];
1059 u8 led_delay_on[MAX_LEDS];
1060 u8 led_delay_off[MAX_LEDS];
1061 u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001062};
1063
Frank Praznik2a242932016-09-22 20:18:09 -04001064static inline void sony_schedule_work(struct sony_sc *sc)
1065{
1066 if (!sc->defer_initialization)
1067 schedule_work(&sc->state_worker);
1068}
1069
Pavel Machek1adf9042016-02-09 13:55:08 +01001070static u8 *sixaxis_fixup(struct hid_device *hdev, u8 *rdesc,
Antonio Ospitec607fb82014-06-24 13:28:41 +02001071 unsigned int *rsize)
1072{
1073 *rsize = sizeof(sixaxis_rdesc);
1074 return sixaxis_rdesc;
1075}
1076
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001077static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
1078 unsigned int *rsize)
1079{
1080 *rsize = sizeof(motion_rdesc);
1081 return motion_rdesc;
1082}
1083
Simon Woodb2723eb2015-06-17 00:08:53 -06001084static u8 *navigation_fixup(struct hid_device *hdev, u8 *rdesc,
1085 unsigned int *rsize)
1086{
1087 *rsize = sizeof(navigation_rdesc);
1088 return navigation_rdesc;
1089}
1090
Pavel Machek1adf9042016-02-09 13:55:08 +01001091static u8 *ps3remote_fixup(struct hid_device *hdev, u8 *rdesc,
Jiri Kosina078328d2013-06-13 12:03:49 +02001092 unsigned int *rsize)
1093{
1094 *rsize = sizeof(ps3remote_rdesc);
1095 return ps3remote_rdesc;
1096}
1097
1098static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
1099 struct hid_field *field, struct hid_usage *usage,
1100 unsigned long **bit, int *max)
1101{
1102 unsigned int key = usage->hid & HID_USAGE;
1103
1104 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1105 return -1;
1106
1107 switch (usage->collection_index) {
1108 case 1:
1109 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
1110 return -1;
1111
1112 key = ps3remote_keymap_joypad_buttons[key];
1113 if (!key)
1114 return -1;
1115 break;
1116 case 2:
1117 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
1118 return -1;
1119
1120 key = ps3remote_keymap_remote_buttons[key];
1121 if (!key)
1122 return -1;
1123 break;
1124 default:
1125 return -1;
1126 }
1127
1128 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1129 return 1;
1130}
1131
Pavel Machek1adf9042016-02-09 13:55:08 +01001132static u8 *sony_report_fixup(struct hid_device *hdev, u8 *rdesc,
Nikolai Kondrashov73e40082010-08-06 23:03:06 +04001133 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001134{
1135 struct sony_sc *sc = hid_get_drvdata(hdev);
1136
Scott Moreau74500cc2016-01-13 07:40:42 -07001137 if (sc->quirks & SINO_LITE_CONTROLLER)
1138 return rdesc;
1139
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +09001140 /*
1141 * Some Sony RF receivers wrongly declare the mouse pointer as a
1142 * a constant non-data variable.
1143 */
1144 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
1145 /* usage page: generic desktop controls */
1146 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
1147 /* usage: mouse */
1148 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
1149 /* input (usage page for x,y axes): constant, variable, relative */
1150 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +09001151 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +09001152 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001153 rdesc[55] = 0x06;
1154 }
Simon Wood61ab44b2011-06-10 12:00:26 +02001155
Frank Prazniked19d8c2014-01-16 21:43:12 -05001156 /*
1157 * The default Dualshock 4 USB descriptor doesn't assign
1158 * the gyroscope values to corresponding axes so we need a
1159 * modified one.
1160 */
Frank Praznikb71b5572015-11-06 15:35:53 -05001161 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Prazniked19d8c2014-01-16 21:43:12 -05001162 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
1163 rdesc = dualshock4_usb_rdesc;
1164 *rsize = sizeof(dualshock4_usb_rdesc);
Frank Praznikb71b5572015-11-06 15:35:53 -05001165 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
Frank Praznikd8296742014-02-05 20:03:45 -05001166 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
1167 rdesc = dualshock4_bt_rdesc;
1168 *rsize = sizeof(dualshock4_bt_rdesc);
Frank Prazniked19d8c2014-01-16 21:43:12 -05001169 }
1170
Antonio Ospitec607fb82014-06-24 13:28:41 +02001171 if (sc->quirks & SIXAXIS_CONTROLLER)
1172 return sixaxis_fixup(hdev, rdesc, rsize);
Jiri Kosina078328d2013-06-13 12:03:49 +02001173
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001174 if (sc->quirks & MOTION_CONTROLLER)
1175 return motion_fixup(hdev, rdesc, rsize);
1176
Simon Wood4545ee02015-06-17 00:08:52 -06001177 if (sc->quirks & NAVIGATION_CONTROLLER)
Simon Woodb2723eb2015-06-17 00:08:53 -06001178 return navigation_fixup(hdev, rdesc, rsize);
Simon Wood4545ee02015-06-17 00:08:52 -06001179
Jiri Kosina078328d2013-06-13 12:03:49 +02001180 if (sc->quirks & PS3REMOTE)
1181 return ps3remote_fixup(hdev, rdesc, rsize);
1182
Nikolai Kondrashov73e40082010-08-06 23:03:06 +04001183 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001184}
1185
Pavel Machek1adf9042016-02-09 13:55:08 +01001186static void sixaxis_parse_report(struct sony_sc *sc, u8 *rd, int size)
Frank Praznikd902f472014-01-27 10:17:36 -05001187{
Pavel Machek1adf9042016-02-09 13:55:08 +01001188 static const u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
Frank Praznikd902f472014-01-27 10:17:36 -05001189 unsigned long flags;
Simon Wood12e9a6d72015-06-09 21:27:05 -06001190 int offset;
Pavel Machek1adf9042016-02-09 13:55:08 +01001191 u8 cable_state, battery_capacity, battery_charging;
Frank Praznikd902f472014-01-27 10:17:36 -05001192
Frank Praznikad142b92014-02-20 11:36:00 -05001193 /*
1194 * The sixaxis is charging if the battery value is 0xee
Frank Praznikd902f472014-01-27 10:17:36 -05001195 * and it is fully charged if the value is 0xef.
1196 * It does not report the actual level while charging so it
1197 * is set to 100% while charging is in progress.
1198 */
Simon Wood12e9a6d72015-06-09 21:27:05 -06001199 offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
1200
1201 if (rd[offset] >= 0xee) {
Frank Praznikd902f472014-01-27 10:17:36 -05001202 battery_capacity = 100;
Simon Wood12e9a6d72015-06-09 21:27:05 -06001203 battery_charging = !(rd[offset] & 0x01);
Frank Praznik9fddd742014-08-29 13:11:52 -04001204 cable_state = 1;
Frank Praznikd902f472014-01-27 10:17:36 -05001205 } else {
Pavel Machek1adf9042016-02-09 13:55:08 +01001206 u8 index = rd[offset] <= 5 ? rd[offset] : 5;
Frank Praznikac3c9a92014-02-20 11:36:02 -05001207 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -05001208 battery_charging = 0;
Frank Praznik9fddd742014-08-29 13:11:52 -04001209 cable_state = 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001210 }
Frank Praznikd902f472014-01-27 10:17:36 -05001211
1212 spin_lock_irqsave(&sc->lock, flags);
1213 sc->cable_state = cable_state;
1214 sc->battery_capacity = battery_capacity;
1215 sc->battery_charging = battery_charging;
1216 spin_unlock_irqrestore(&sc->lock, flags);
1217}
1218
Pavel Machek1adf9042016-02-09 13:55:08 +01001219static void dualshock4_parse_report(struct sony_sc *sc, u8 *rd, int size)
Frank Praznikd902f472014-01-27 10:17:36 -05001220{
Frank Praznike5606232014-01-27 10:17:37 -05001221 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1222 struct hid_input, list);
1223 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -05001224 unsigned long flags;
Frank Praznik6c5f8602014-02-05 20:03:47 -05001225 int n, offset;
Pavel Machek1adf9042016-02-09 13:55:08 +01001226 u8 cable_state, battery_capacity, battery_charging;
Frank Praznikd902f472014-01-27 10:17:36 -05001227
Frank Praznikad142b92014-02-20 11:36:00 -05001228 /*
1229 * Battery and touchpad data starts at byte 30 in the USB report and
Frank Praznik6c5f8602014-02-05 20:03:47 -05001230 * 32 in Bluetooth report.
1231 */
1232 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
1233
Frank Praznikad142b92014-02-20 11:36:00 -05001234 /*
1235 * The lower 4 bits of byte 30 contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -05001236 * and the 5th bit contains the USB cable state.
1237 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001238 cable_state = (rd[offset] >> 4) & 0x01;
1239 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -05001240
Frank Praznikad142b92014-02-20 11:36:00 -05001241 /*
1242 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -05001243 * 0 to 10, and when running on battery power it ranges from 0 to 9.
1244 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -05001245 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001246 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -05001247 battery_charging = 0;
1248 else
1249 battery_charging = 1;
1250
Frank Praznik6c5f8602014-02-05 20:03:47 -05001251 if (!cable_state)
1252 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -05001253 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -05001254 battery_capacity = 10;
1255
Frank Praznikd902f472014-01-27 10:17:36 -05001256 battery_capacity *= 10;
1257
1258 spin_lock_irqsave(&sc->lock, flags);
1259 sc->cable_state = cable_state;
1260 sc->battery_capacity = battery_capacity;
1261 sc->battery_charging = battery_charging;
1262 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -05001263
Frank Praznik6c5f8602014-02-05 20:03:47 -05001264 offset += 5;
1265
Frank Praznikad142b92014-02-20 11:36:00 -05001266 /*
1267 * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
Frank Praznik6c5f8602014-02-05 20:03:47 -05001268 * and 37 on Bluetooth.
Frank Praznike5606232014-01-27 10:17:37 -05001269 * The first 7 bits of the first byte is a counter and bit 8 is a touch
1270 * indicator that is 0 when pressed and 1 when not pressed.
1271 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1272 * The data for the second touch is in the same format and immediatly
1273 * follows the data for the first.
1274 */
1275 for (n = 0; n < 2; n++) {
Pavel Machek1adf9042016-02-09 13:55:08 +01001276 u16 x, y;
Frank Praznike5606232014-01-27 10:17:37 -05001277
1278 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1279 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1280
1281 input_mt_slot(input_dev, n);
1282 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
1283 !(rd[offset] >> 7));
1284 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1285 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1286
1287 offset += 4;
1288 }
Frank Praznikd902f472014-01-27 10:17:36 -05001289}
1290
Simon Woodc9e4d872011-06-10 12:00:27 +02001291static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
Pavel Machek1adf9042016-02-09 13:55:08 +01001292 u8 *rd, int size)
Simon Woodc9e4d872011-06-10 12:00:27 +02001293{
1294 struct sony_sc *sc = hid_get_drvdata(hdev);
1295
Frank Praznikad142b92014-02-20 11:36:00 -05001296 /*
1297 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +02001298 * has to be BYTE_SWAPPED before passing up to joystick interface
1299 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001300 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Frank Praznik8f5f0bc2015-07-23 19:01:16 -04001301 /*
1302 * When connected via Bluetooth the Sixaxis occasionally sends
1303 * a report with the second byte 0xff and the rest zeroed.
1304 *
1305 * This report does not reflect the actual state of the
1306 * controller must be ignored to avoid generating false input
1307 * events.
1308 */
1309 if (rd[1] == 0xff)
1310 return -EINVAL;
1311
Simon Woodc9e4d872011-06-10 12:00:27 +02001312 swap(rd[41], rd[42]);
1313 swap(rd[43], rd[44]);
1314 swap(rd[45], rd[46]);
1315 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -05001316
1317 sixaxis_parse_report(sc, rd, size);
Simon Wood12e9a6d72015-06-09 21:27:05 -06001318 } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
1319 sixaxis_parse_report(sc, rd, size);
Simon Wood4545ee02015-06-17 00:08:52 -06001320 } else if ((sc->quirks & NAVIGATION_CONTROLLER) && rd[0] == 0x01 &&
1321 size == 49) {
1322 sixaxis_parse_report(sc, rd, size);
Frank Praznik68330d82014-02-05 20:03:49 -05001323 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1324 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1325 && rd[0] == 0x11 && size == 78)) {
Frank Praznikd902f472014-01-27 10:17:36 -05001326 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +02001327 }
1328
Frank Praznik2a242932016-09-22 20:18:09 -04001329 if (sc->defer_initialization) {
1330 sc->defer_initialization = 0;
1331 sony_schedule_work(sc);
1332 }
1333
Simon Woodc9e4d872011-06-10 12:00:27 +02001334 return 0;
1335}
1336
Colin Leitnerf04d5142013-05-27 23:41:05 +02001337static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1338 struct hid_field *field, struct hid_usage *usage,
1339 unsigned long **bit, int *max)
1340{
1341 struct sony_sc *sc = hid_get_drvdata(hdev);
1342
1343 if (sc->quirks & BUZZ_CONTROLLER) {
1344 unsigned int key = usage->hid & HID_USAGE;
1345
1346 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1347 return -1;
1348
1349 switch (usage->collection_index) {
1350 case 1:
1351 if (key >= ARRAY_SIZE(buzz_keymap))
1352 return -1;
1353
1354 key = buzz_keymap[key];
1355 if (!key)
1356 return -1;
1357 break;
1358 default:
1359 return -1;
1360 }
1361
1362 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1363 return 1;
1364 }
1365
Jiri Kosina078328d2013-06-13 12:03:49 +02001366 if (sc->quirks & PS3REMOTE)
1367 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1368
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001369 /* Let hid-core decide for the others */
1370 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001371}
1372
Frank Praznikce8efc32014-09-18 21:15:01 -04001373static int sony_register_touchpad(struct hid_input *hi, int touch_count,
1374 int w, int h)
1375{
1376 struct input_dev *input_dev = hi->input;
1377 int ret;
1378
1379 ret = input_mt_init_slots(input_dev, touch_count, 0);
1380 if (ret < 0)
1381 return ret;
1382
1383 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1384 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1385
1386 return 0;
1387}
1388
Dmitry Torokhov91543012015-09-29 15:52:59 -07001389static int sony_input_configured(struct hid_device *hdev,
Frank Praznikce8efc32014-09-18 21:15:01 -04001390 struct hid_input *hidinput)
1391{
1392 struct sony_sc *sc = hid_get_drvdata(hdev);
Dmitry Torokhov91543012015-09-29 15:52:59 -07001393 int ret;
Frank Praznikce8efc32014-09-18 21:15:01 -04001394
1395 /*
1396 * The Dualshock 4 touchpad supports 2 touches and has a
Frank Praznik981c5b42014-09-24 09:38:19 -04001397 * resolution of 1920x942 (44.86 dots/mm).
Frank Praznikce8efc32014-09-18 21:15:01 -04001398 */
1399 if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Dmitry Torokhov91543012015-09-29 15:52:59 -07001400 ret = sony_register_touchpad(hidinput, 2, 1920, 942);
1401 if (ret) {
Frank Praznikce8efc32014-09-18 21:15:01 -04001402 hid_err(sc->hdev,
Dmitry Torokhov91543012015-09-29 15:52:59 -07001403 "Unable to initialize multi-touch slots: %d\n",
1404 ret);
1405 return ret;
1406 }
Frank Praznikce8efc32014-09-18 21:15:01 -04001407 }
Dmitry Torokhov91543012015-09-29 15:52:59 -07001408
1409 return 0;
Frank Praznikce8efc32014-09-18 21:15:01 -04001410}
1411
Antonio Ospite5710fab2011-02-20 18:26:45 +01001412/*
Jiri Slabybd28ce02008-06-25 23:47:04 +02001413 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1414 * to "operational". Without this, the ps3 controller will not report any
1415 * events.
1416 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001417static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001418{
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001419 const int buf_size =
1420 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
Pavel Machek1adf9042016-02-09 13:55:08 +01001421 u8 *buf;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001422 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001423
Antonio Ospite2e701a32015-02-16 18:12:24 +01001424 buf = kmalloc(buf_size, GFP_KERNEL);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001425 if (!buf)
1426 return -ENOMEM;
1427
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001428 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1429 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001430 if (ret < 0) {
1431 hid_err(hdev, "can't set operational mode: step 1\n");
1432 goto out;
1433 }
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001434
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001435 /*
1436 * Some compatible controllers like the Speedlink Strike FX and
1437 * Gasia need another query plus an USB interrupt to get operational.
1438 */
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001439 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1440 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001441 if (ret < 0) {
1442 hid_err(hdev, "can't set operational mode: step 2\n");
1443 goto out;
1444 }
1445
1446 ret = hid_hw_output_report(hdev, buf, 1);
Benjamin Tissoires19f4c2b2016-01-08 17:58:49 +01001447 if (ret < 0) {
1448 hid_info(hdev, "can't set operational mode: step 3, ignoring\n");
1449 ret = 0;
1450 }
Jiri Slabybd28ce02008-06-25 23:47:04 +02001451
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001452out:
Jiri Slabybd28ce02008-06-25 23:47:04 +02001453 kfree(buf);
1454
1455 return ret;
1456}
1457
Antonio Ospite816651a2010-05-03 22:15:55 +02001458static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001459{
Pavel Machek1adf9042016-02-09 13:55:08 +01001460 static const u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1461 u8 *buf;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001462 int ret;
1463
1464 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1465 if (!buf)
1466 return -ENOMEM;
1467
1468 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
Benjamin Tissoiresb0dd72a2014-02-10 12:58:54 -05001469 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001470
1471 kfree(buf);
1472
1473 return ret;
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001474}
1475
Frank Praznikad142b92014-02-20 11:36:00 -05001476/*
1477 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
Frank Praznik68330d82014-02-05 20:03:49 -05001478 * controller so that it sends full input reports of type 0x11.
1479 */
1480static int dualshock4_set_operational_bt(struct hid_device *hdev)
1481{
Pavel Machek1adf9042016-02-09 13:55:08 +01001482 u8 *buf;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001483 int ret;
Frank Praznik68330d82014-02-05 20:03:49 -05001484
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001485 buf = kmalloc(DS4_REPORT_0x02_SIZE, GFP_KERNEL);
1486 if (!buf)
1487 return -ENOMEM;
1488
1489 ret = hid_hw_raw_request(hdev, 0x02, buf, DS4_REPORT_0x02_SIZE,
Frank Praznik68330d82014-02-05 20:03:49 -05001490 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001491
1492 kfree(buf);
1493
1494 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001495}
1496
Frank Praznik221399b2015-05-05 20:47:32 -04001497static void sixaxis_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001498{
Pavel Machek1adf9042016-02-09 13:55:08 +01001499 static const u8 sixaxis_leds[10][4] = {
Frank Praznik80250872014-04-14 10:11:35 -04001500 { 0x01, 0x00, 0x00, 0x00 },
1501 { 0x00, 0x01, 0x00, 0x00 },
1502 { 0x00, 0x00, 0x01, 0x00 },
1503 { 0x00, 0x00, 0x00, 0x01 },
1504 { 0x01, 0x00, 0x00, 0x01 },
1505 { 0x00, 0x01, 0x00, 0x01 },
1506 { 0x00, 0x00, 0x01, 0x01 },
1507 { 0x01, 0x00, 0x01, 0x01 },
1508 { 0x00, 0x01, 0x01, 0x01 },
1509 { 0x01, 0x01, 0x01, 0x01 }
1510 };
1511
Frank Praznik221399b2015-05-05 20:47:32 -04001512 int id = sc->device_id;
1513
1514 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001515
1516 if (id < 0)
1517 return;
1518
1519 id %= 10;
Frank Praznik221399b2015-05-05 20:47:32 -04001520 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001521}
1522
Frank Praznik221399b2015-05-05 20:47:32 -04001523static void dualshock4_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001524{
1525 /* The first 4 color/index entries match what the PS4 assigns */
Pavel Machek1adf9042016-02-09 13:55:08 +01001526 static const u8 color_code[7][3] = {
Frank Praznik80250872014-04-14 10:11:35 -04001527 /* Blue */ { 0x00, 0x00, 0x01 },
1528 /* Red */ { 0x01, 0x00, 0x00 },
1529 /* Green */ { 0x00, 0x01, 0x00 },
1530 /* Pink */ { 0x02, 0x00, 0x01 },
1531 /* Orange */ { 0x02, 0x01, 0x00 },
1532 /* Teal */ { 0x00, 0x01, 0x01 },
1533 /* White */ { 0x01, 0x01, 0x01 }
1534 };
1535
Frank Praznik221399b2015-05-05 20:47:32 -04001536 int id = sc->device_id;
1537
1538 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001539
1540 if (id < 0)
1541 return;
1542
1543 id %= 7;
Frank Praznik221399b2015-05-05 20:47:32 -04001544 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001545}
1546
Frank Praznik221399b2015-05-05 20:47:32 -04001547static void buzz_set_leds(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001548{
Frank Praznik221399b2015-05-05 20:47:32 -04001549 struct hid_device *hdev = sc->hdev;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001550 struct list_head *report_list =
1551 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1552 struct hid_report *report = list_entry(report_list->next,
1553 struct hid_report, list);
Pavel Machek1adf9042016-02-09 13:55:08 +01001554 s32 *value = report->field[0]->value;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001555
Frank Praznik221399b2015-05-05 20:47:32 -04001556 BUILD_BUG_ON(MAX_LEDS < 4);
1557
Colin Leitnerf04d5142013-05-27 23:41:05 +02001558 value[0] = 0x00;
Frank Praznik221399b2015-05-05 20:47:32 -04001559 value[1] = sc->led_state[0] ? 0xff : 0x00;
1560 value[2] = sc->led_state[1] ? 0xff : 0x00;
1561 value[3] = sc->led_state[2] ? 0xff : 0x00;
1562 value[4] = sc->led_state[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001563 value[5] = 0x00;
1564 value[6] = 0x00;
1565 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1566}
1567
Frank Praznik221399b2015-05-05 20:47:32 -04001568static void sony_set_leds(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001569{
Frank Praznik221399b2015-05-05 20:47:32 -04001570 if (!(sc->quirks & BUZZ_CONTROLLER))
Frank Praznik2a242932016-09-22 20:18:09 -04001571 sony_schedule_work(sc);
Frank Praznik221399b2015-05-05 20:47:32 -04001572 else
1573 buzz_set_leds(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001574}
1575
Sven Eckelmannc5382512013-11-19 20:26:30 +01001576static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001577 enum led_brightness value)
1578{
1579 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001580 struct hid_device *hdev = to_hid_device(dev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001581 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001582
1583 int n;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001584 int force_update;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001585
1586 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001587 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001588 hid_err(hdev, "No device data\n");
1589 return;
1590 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001591
Frank Praznikb3ed4582014-04-14 10:11:36 -04001592 /*
1593 * The Sixaxis on USB will override any LED settings sent to it
1594 * and keep flashing all of the LEDs until the PS button is pressed.
1595 * Updates, even if redundant, must be always be sent to the
1596 * controller to avoid having to toggle the state of an LED just to
1597 * stop the flashing later on.
1598 */
1599 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1600
Frank Praznik60781cf2014-01-11 15:13:15 -05001601 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznikb3ed4582014-04-14 10:11:36 -04001602 if (led == drv_data->leds[n] && (force_update ||
1603 (value != drv_data->led_state[n] ||
1604 drv_data->led_delay_on[n] ||
1605 drv_data->led_delay_off[n]))) {
1606
1607 drv_data->led_state[n] = value;
1608
1609 /* Setting the brightness stops the blinking */
1610 drv_data->led_delay_on[n] = 0;
1611 drv_data->led_delay_off[n] = 0;
1612
Frank Praznik221399b2015-05-05 20:47:32 -04001613 sony_set_leds(drv_data);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001614 break;
1615 }
1616 }
1617}
1618
Sven Eckelmannc5382512013-11-19 20:26:30 +01001619static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001620{
1621 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001622 struct hid_device *hdev = to_hid_device(dev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001623 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001624
1625 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001626
1627 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001628 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001629 hid_err(hdev, "No device data\n");
1630 return LED_OFF;
1631 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001632
Frank Praznik60781cf2014-01-11 15:13:15 -05001633 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001634 if (led == drv_data->leds[n])
1635 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001636 }
1637
Simon Wood7db75042014-02-05 12:34:18 -07001638 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001639}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001640
Frank Praznikb3ed4582014-04-14 10:11:36 -04001641static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1642 unsigned long *delay_off)
1643{
1644 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001645 struct hid_device *hdev = to_hid_device(dev);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001646 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1647 int n;
Pavel Machek1adf9042016-02-09 13:55:08 +01001648 u8 new_on, new_off;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001649
1650 if (!drv_data) {
1651 hid_err(hdev, "No device data\n");
1652 return -EINVAL;
1653 }
1654
1655 /* Max delay is 255 deciseconds or 2550 milliseconds */
1656 if (*delay_on > 2550)
1657 *delay_on = 2550;
1658 if (*delay_off > 2550)
1659 *delay_off = 2550;
1660
1661 /* Blink at 1 Hz if both values are zero */
1662 if (!*delay_on && !*delay_off)
1663 *delay_on = *delay_off = 500;
1664
1665 new_on = *delay_on / 10;
1666 new_off = *delay_off / 10;
1667
1668 for (n = 0; n < drv_data->led_count; n++) {
1669 if (led == drv_data->leds[n])
1670 break;
1671 }
1672
1673 /* This LED is not registered on this device */
1674 if (n >= drv_data->led_count)
1675 return -EINVAL;
1676
1677 /* Don't schedule work if the values didn't change */
1678 if (new_on != drv_data->led_delay_on[n] ||
1679 new_off != drv_data->led_delay_off[n]) {
1680 drv_data->led_delay_on[n] = new_on;
1681 drv_data->led_delay_off[n] = new_off;
Frank Praznik2a242932016-09-22 20:18:09 -04001682 sony_schedule_work(drv_data);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001683 }
1684
1685 return 0;
1686}
1687
Frank Praznikfa57a812014-04-14 10:11:33 -04001688static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001689{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001690 struct led_classdev *led;
1691 int n;
1692
Frank Praznikfa57a812014-04-14 10:11:33 -04001693 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001694
Frank Praznikfa57a812014-04-14 10:11:33 -04001695 for (n = 0; n < sc->led_count; n++) {
1696 led = sc->leds[n];
1697 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001698 if (!led)
1699 continue;
1700 led_classdev_unregister(led);
1701 kfree(led);
1702 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001703
Frank Praznikfa57a812014-04-14 10:11:33 -04001704 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001705}
1706
Frank Praznikfa57a812014-04-14 10:11:33 -04001707static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001708{
Frank Praznikfa57a812014-04-14 10:11:33 -04001709 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001710 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001711 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001712 struct led_classdev *led;
1713 size_t name_sz;
1714 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001715 size_t name_len;
1716 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001717 static const char * const ds4_name_str[] = { "red", "green", "blue",
1718 "global" };
Pavel Machek1adf9042016-02-09 13:55:08 +01001719 u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
1720 u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001721
Frank Praznikfa57a812014-04-14 10:11:33 -04001722 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001723
Frank Praznikfa57a812014-04-14 10:11:33 -04001724 if (sc->quirks & BUZZ_CONTROLLER) {
1725 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001726 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001727 name_len = strlen("::buzz#");
1728 name_fmt = "%s::buzz%d";
1729 /* Validate expected report characteristics. */
1730 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1731 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001732 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik221399b2015-05-05 20:47:32 -04001733 dualshock4_set_leds_from_id(sc);
1734 sc->led_state[3] = 1;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001735 sc->led_count = 4;
1736 memset(max_brightness, 255, 3);
1737 use_hw_blink[3] = 1;
1738 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001739 name_len = 0;
1740 name_fmt = "%s:%s";
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001741 } else if (sc->quirks & MOTION_CONTROLLER) {
1742 sc->led_count = 3;
1743 memset(max_brightness, 255, 3);
1744 use_ds4_names = 1;
1745 name_len = 0;
1746 name_fmt = "%s:%s";
Simon Wood4545ee02015-06-17 00:08:52 -06001747 } else if (sc->quirks & NAVIGATION_CONTROLLER) {
Pavel Machek1adf9042016-02-09 13:55:08 +01001748 static const u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00};
Simon Wood4545ee02015-06-17 00:08:52 -06001749
1750 memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds));
1751 sc->led_count = 1;
1752 memset(use_hw_blink, 1, 4);
1753 use_ds4_names = 0;
1754 name_len = strlen("::sony#");
1755 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001756 } else {
Frank Praznik221399b2015-05-05 20:47:32 -04001757 sixaxis_set_leds_from_id(sc);
Frank Praznikfa57a812014-04-14 10:11:33 -04001758 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001759 memset(use_hw_blink, 1, 4);
1760 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001761 name_len = strlen("::sony#");
1762 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001763 }
1764
Frank Praznikad142b92014-02-20 11:36:00 -05001765 /*
1766 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001767 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001768 * LEDs to on
1769 */
Frank Praznik221399b2015-05-05 20:47:32 -04001770 sony_set_leds(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001771
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001772 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001773
Frank Praznikfa57a812014-04-14 10:11:33 -04001774 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001775
Frank Praznikb3ed4582014-04-14 10:11:36 -04001776 if (use_ds4_names)
1777 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001778
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001779 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1780 if (!led) {
1781 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001782 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001783 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001784 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001785
1786 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001787 if (use_ds4_names)
1788 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1789 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001790 else
1791 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001792 led->name = name;
Frank Praznik221399b2015-05-05 20:47:32 -04001793 led->brightness = sc->led_state[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001794 led->max_brightness = max_brightness[n];
Sven Eckelmannc5382512013-11-19 20:26:30 +01001795 led->brightness_get = sony_led_get_brightness;
1796 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001797
Frank Praznikb3ed4582014-04-14 10:11:36 -04001798 if (use_hw_blink[n])
1799 led->blink_set = sony_led_blink_set;
1800
Frank Praznik80250872014-04-14 10:11:35 -04001801 sc->leds[n] = led;
1802
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001803 ret = led_classdev_register(&hdev->dev, led);
1804 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001805 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001806 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001807 kfree(led);
1808 goto error_leds;
1809 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001810 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001811
1812 return ret;
1813
Colin Leitnerf04d5142013-05-27 23:41:05 +02001814error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001815 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001816
Colin Leitnerf04d5142013-05-27 23:41:05 +02001817 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001818}
1819
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001820static void sixaxis_send_output_report(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001821{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001822 static const union sixaxis_output_report_01 default_report = {
Frank Praznik55d3b662014-04-14 10:11:32 -04001823 .buf = {
1824 0x01,
Scott Moreauad07b7a2016-01-13 07:40:43 -07001825 0x01, 0xff, 0x00, 0xff, 0x00,
Frank Praznik55d3b662014-04-14 10:11:32 -04001826 0x00, 0x00, 0x00, 0x00, 0x00,
1827 0xff, 0x27, 0x10, 0x00, 0x32,
1828 0xff, 0x27, 0x10, 0x00, 0x32,
1829 0xff, 0x27, 0x10, 0x00, 0x32,
1830 0xff, 0x27, 0x10, 0x00, 0x32,
1831 0x00, 0x00, 0x00, 0x00, 0x00
1832 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001833 };
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001834 struct sixaxis_output_report *report =
1835 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1836 int n;
1837
1838 /* Initialize the report with default values */
1839 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001840
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001841#ifdef CONFIG_SONY_FF
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001842 report->rumble.right_motor_on = sc->right ? 1 : 0;
1843 report->rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001844#endif
1845
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001846 report->leds_bitmap |= sc->led_state[0] << 1;
1847 report->leds_bitmap |= sc->led_state[1] << 2;
1848 report->leds_bitmap |= sc->led_state[2] << 3;
1849 report->leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001850
Simon Wood88f65762014-04-14 10:11:37 -04001851 /* Set flag for all leds off, required for 3rd party INTEC controller */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001852 if ((report->leds_bitmap & 0x1E) == 0)
1853 report->leds_bitmap |= 0x20;
Simon Wood88f65762014-04-14 10:11:37 -04001854
Frank Praznikb3ed4582014-04-14 10:11:36 -04001855 /*
1856 * The LEDs in the report are indexed in reverse order to their
1857 * corresponding light on the controller.
1858 * Index 0 = LED 4, index 1 = LED 3, etc...
1859 *
1860 * In the case of both delay values being zero (blinking disabled) the
1861 * default report values should be used or the controller LED will be
1862 * always off.
1863 */
1864 for (n = 0; n < 4; n++) {
1865 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001866 report->led[3 - n].duty_off = sc->led_delay_off[n];
1867 report->led[3 - n].duty_on = sc->led_delay_on[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001868 }
1869 }
1870
Pavel Machek1adf9042016-02-09 13:55:08 +01001871 hid_hw_raw_request(sc->hdev, report->report_id, (u8 *)report,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001872 sizeof(struct sixaxis_output_report),
1873 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001874}
1875
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001876static void dualshock4_send_output_report(struct sony_sc *sc)
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001877{
Frank Praznik0da8ea62014-01-16 21:42:51 -05001878 struct hid_device *hdev = sc->hdev;
Pavel Machek1adf9042016-02-09 13:55:08 +01001879 u8 *buf = sc->output_report_dmabuf;
Frank Praznik48220232014-02-05 20:03:44 -05001880 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001881
Frank Praznikc4425c82016-09-22 20:18:10 -04001882 /*
1883 * NOTE: The buf[1] field of the Bluetooth report controls
1884 * the Dualshock 4 reporting rate.
1885 *
1886 * Known values include:
1887 *
1888 * 0x80 - 1000hz (full speed)
1889 * 0xA0 - 31hz
1890 * 0xB0 - 20hz
1891 * 0xD0 - 66hz
1892 */
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001893 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001894 memset(buf, 0, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001895 buf[0] = 0x05;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001896 buf[1] = 0xFF;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001897 offset = 4;
1898 } else {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001899 memset(buf, 0, DS4_REPORT_0x11_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001900 buf[0] = 0x11;
Rostislav Pehlivanov824deff2015-07-28 08:38:47 +01001901 buf[1] = 0x80;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001902 buf[3] = 0x0F;
1903 offset = 6;
1904 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001905
1906#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001907 buf[offset++] = sc->right;
1908 buf[offset++] = sc->left;
1909#else
1910 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001911#endif
1912
Frank Praznikb3ed4582014-04-14 10:11:36 -04001913 /* LED 3 is the global control */
1914 if (sc->led_state[3]) {
1915 buf[offset++] = sc->led_state[0];
1916 buf[offset++] = sc->led_state[1];
1917 buf[offset++] = sc->led_state[2];
1918 } else {
1919 offset += 3;
1920 }
1921
1922 /* If both delay values are zero the DualShock 4 disables blinking. */
1923 buf[offset++] = sc->led_delay_on[3];
1924 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05001925
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001926 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001927 hid_hw_output_report(hdev, buf, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001928 else
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001929 hid_hw_raw_request(hdev, 0x11, buf, DS4_REPORT_0x11_SIZE,
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001930 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001931}
1932
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001933static void motion_send_output_report(struct sony_sc *sc)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001934{
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001935 struct hid_device *hdev = sc->hdev;
1936 struct motion_output_report_02 *report =
1937 (struct motion_output_report_02 *)sc->output_report_dmabuf;
1938
Simon Wood41d2d422015-06-09 21:27:06 -06001939 memset(report, 0, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001940
1941 report->type = 0x02; /* set leds */
1942 report->r = sc->led_state[0];
1943 report->g = sc->led_state[1];
1944 report->b = sc->led_state[2];
1945
1946#ifdef CONFIG_SONY_FF
1947 report->rumble = max(sc->right, sc->left);
1948#endif
1949
Pavel Machek1adf9042016-02-09 13:55:08 +01001950 hid_hw_output_report(hdev, (u8 *)report, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001951}
1952
Frank Praznikdecd9462015-11-11 09:49:38 -05001953static inline void sony_send_output_report(struct sony_sc *sc)
1954{
1955 if (sc->send_output_report)
1956 sc->send_output_report(sc);
1957}
1958
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001959static void sony_state_worker(struct work_struct *work)
1960{
1961 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Antonio Ospiteef916ef2016-02-09 13:55:07 +01001962
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001963 sc->send_output_report(sc);
1964}
1965
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001966static int sony_allocate_output_report(struct sony_sc *sc)
1967{
Simon Wood4545ee02015-06-17 00:08:52 -06001968 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
1969 (sc->quirks & NAVIGATION_CONTROLLER))
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001970 sc->output_report_dmabuf =
1971 kmalloc(sizeof(union sixaxis_output_report_01),
1972 GFP_KERNEL);
1973 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1974 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x11_SIZE,
1975 GFP_KERNEL);
1976 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1977 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x05_SIZE,
1978 GFP_KERNEL);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001979 else if (sc->quirks & MOTION_CONTROLLER)
Simon Wood41d2d422015-06-09 21:27:06 -06001980 sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE,
1981 GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001982 else
1983 return 0;
1984
1985 if (!sc->output_report_dmabuf)
1986 return -ENOMEM;
1987
1988 return 0;
1989}
1990
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001991#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001992static int sony_play_effect(struct input_dev *dev, void *data,
1993 struct ff_effect *effect)
1994{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001995 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001996 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001997
1998 if (effect->type != FF_RUMBLE)
1999 return 0;
2000
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002001 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002002 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002003
Frank Praznik2a242932016-09-22 20:18:09 -04002004 sony_schedule_work(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002005 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002006}
2007
Frank Praznikfa57a812014-04-14 10:11:33 -04002008static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002009{
Frank Praznikfa57a812014-04-14 10:11:33 -04002010 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002011 struct hid_input, list);
2012 struct input_dev *input_dev = hidinput->input;
2013
2014 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
2015 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
2016}
2017
2018#else
Frank Praznikfa57a812014-04-14 10:11:33 -04002019static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002020{
2021 return 0;
2022}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002023
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002024#endif
2025
Frank Praznikd902f472014-01-27 10:17:36 -05002026static int sony_battery_get_property(struct power_supply *psy,
2027 enum power_supply_property psp,
2028 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002029{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002030 struct sony_sc *sc = power_supply_get_drvdata(psy);
Frank Praznikd902f472014-01-27 10:17:36 -05002031 unsigned long flags;
2032 int ret = 0;
2033 u8 battery_charging, battery_capacity, cable_state;
2034
2035 spin_lock_irqsave(&sc->lock, flags);
2036 battery_charging = sc->battery_charging;
2037 battery_capacity = sc->battery_capacity;
2038 cable_state = sc->cable_state;
2039 spin_unlock_irqrestore(&sc->lock, flags);
2040
2041 switch (psp) {
2042 case POWER_SUPPLY_PROP_PRESENT:
2043 val->intval = 1;
2044 break;
2045 case POWER_SUPPLY_PROP_SCOPE:
2046 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
2047 break;
2048 case POWER_SUPPLY_PROP_CAPACITY:
2049 val->intval = battery_capacity;
2050 break;
2051 case POWER_SUPPLY_PROP_STATUS:
2052 if (battery_charging)
2053 val->intval = POWER_SUPPLY_STATUS_CHARGING;
2054 else
2055 if (battery_capacity == 100 && cable_state)
2056 val->intval = POWER_SUPPLY_STATUS_FULL;
2057 else
2058 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
2059 break;
2060 default:
2061 ret = -EINVAL;
2062 break;
2063 }
2064 return ret;
2065}
2066
Frank Praznik0f398232016-09-22 20:18:08 -04002067static int sony_battery_probe(struct sony_sc *sc, int append_dev_id)
Frank Praznikd902f472014-01-27 10:17:36 -05002068{
Frank Praznik0f398232016-09-22 20:18:08 -04002069 const char *battery_str_fmt = append_dev_id ?
2070 "sony_controller_battery_%pMR_%i" :
2071 "sony_controller_battery_%pMR";
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002072 struct power_supply_config psy_cfg = { .drv_data = sc, };
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002073 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05002074 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002075
Frank Praznikad142b92014-02-20 11:36:00 -05002076 /*
2077 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05002078 * if the battery is polled before the first device report is received.
2079 */
2080 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002081
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002082 sc->battery_desc.properties = sony_battery_props;
2083 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
2084 sc->battery_desc.get_property = sony_battery_get_property;
2085 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
2086 sc->battery_desc.use_for_apm = 0;
Frank Praznik0f398232016-09-22 20:18:08 -04002087 sc->battery_desc.name = kasprintf(GFP_KERNEL, battery_str_fmt,
2088 sc->mac_address, sc->device_id);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002089 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05002090 return -ENOMEM;
2091
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002092 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
2093 &psy_cfg);
2094 if (IS_ERR(sc->battery)) {
2095 ret = PTR_ERR(sc->battery);
Frank Praznikd902f472014-01-27 10:17:36 -05002096 hid_err(hdev, "Unable to register battery device\n");
2097 goto err_free;
2098 }
2099
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002100 power_supply_powers(sc->battery, &hdev->dev);
Frank Praznikd902f472014-01-27 10:17:36 -05002101 return 0;
2102
2103err_free:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002104 kfree(sc->battery_desc.name);
2105 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05002106 return ret;
2107}
2108
2109static void sony_battery_remove(struct sony_sc *sc)
2110{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002111 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05002112 return;
2113
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002114 power_supply_unregister(sc->battery);
2115 kfree(sc->battery_desc.name);
2116 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05002117}
2118
Frank Praznikd2d782f2014-02-20 11:36:03 -05002119/*
2120 * If a controller is plugged in via USB while already connected via Bluetooth
2121 * it will show up as two devices. A global list of connected controllers and
2122 * their MAC addresses is maintained to ensure that a device is only connected
2123 * once.
Frank Praznik0f398232016-09-22 20:18:08 -04002124 *
2125 * Some USB-only devices masquerade as Sixaxis controllers and all have the
2126 * same dummy Bluetooth address, so a comparison of the connection type is
2127 * required. Devices are only rejected in the case where two devices have
2128 * matching Bluetooth addresses on different bus types.
Frank Praznikd2d782f2014-02-20 11:36:03 -05002129 */
Frank Praznik0f398232016-09-22 20:18:08 -04002130static inline int sony_compare_connection_type(struct sony_sc *sc0,
2131 struct sony_sc *sc1)
2132{
2133 const int sc0_not_bt = !(sc0->quirks & SONY_BT_DEVICE);
2134 const int sc1_not_bt = !(sc1->quirks & SONY_BT_DEVICE);
2135
2136 return sc0_not_bt == sc1_not_bt;
2137}
2138
Frank Praznikd2d782f2014-02-20 11:36:03 -05002139static int sony_check_add_dev_list(struct sony_sc *sc)
2140{
2141 struct sony_sc *entry;
2142 unsigned long flags;
2143 int ret;
2144
2145 spin_lock_irqsave(&sony_dev_list_lock, flags);
2146
2147 list_for_each_entry(entry, &sony_device_list, list_node) {
2148 ret = memcmp(sc->mac_address, entry->mac_address,
2149 sizeof(sc->mac_address));
2150 if (!ret) {
Frank Praznik0f398232016-09-22 20:18:08 -04002151 if (sony_compare_connection_type(sc, entry)) {
2152 ret = 1;
2153 } else {
2154 ret = -EEXIST;
2155 hid_info(sc->hdev,
2156 "controller with MAC address %pMR already connected\n",
Frank Praznikd2d782f2014-02-20 11:36:03 -05002157 sc->mac_address);
Frank Praznik0f398232016-09-22 20:18:08 -04002158 }
Frank Praznikd2d782f2014-02-20 11:36:03 -05002159 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002160 }
2161 }
2162
Frank Praznikd2d782f2014-02-20 11:36:03 -05002163 ret = 0;
2164 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002165
Frank Praznikd2d782f2014-02-20 11:36:03 -05002166unlock:
2167 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2168 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002169}
2170
Frank Praznikd2d782f2014-02-20 11:36:03 -05002171static void sony_remove_dev_list(struct sony_sc *sc)
2172{
2173 unsigned long flags;
2174
2175 if (sc->list_node.next) {
2176 spin_lock_irqsave(&sony_dev_list_lock, flags);
2177 list_del(&(sc->list_node));
2178 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2179 }
2180}
2181
2182static int sony_get_bt_devaddr(struct sony_sc *sc)
2183{
2184 int ret;
2185
2186 /* HIDP stores the device MAC address as a string in the uniq field. */
2187 ret = strlen(sc->hdev->uniq);
2188 if (ret != 17)
2189 return -EINVAL;
2190
2191 ret = sscanf(sc->hdev->uniq,
2192 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2193 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
2194 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
2195
2196 if (ret != 6)
2197 return -EINVAL;
2198
2199 return 0;
2200}
2201
2202static int sony_check_add(struct sony_sc *sc)
2203{
Pavel Machek1adf9042016-02-09 13:55:08 +01002204 u8 *buf = NULL;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002205 int n, ret;
2206
2207 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
Simon Wood12e9a6d72015-06-09 21:27:05 -06002208 (sc->quirks & MOTION_CONTROLLER_BT) ||
Simon Wood4545ee02015-06-17 00:08:52 -06002209 (sc->quirks & NAVIGATION_CONTROLLER_BT) ||
Frank Praznikd2d782f2014-02-20 11:36:03 -05002210 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
2211 /*
2212 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
2213 * address from the uniq string where HIDP stores it.
2214 * As uniq cannot be guaranteed to be a MAC address in all cases
2215 * a failure of this function should not prevent the connection.
2216 */
2217 if (sony_get_bt_devaddr(sc) < 0) {
2218 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
2219 return 0;
2220 }
2221 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002222 buf = kmalloc(DS4_REPORT_0x81_SIZE, GFP_KERNEL);
2223 if (!buf)
2224 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002225
2226 /*
2227 * The MAC address of a DS4 controller connected via USB can be
2228 * retrieved with feature report 0x81. The address begins at
2229 * offset 1.
2230 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002231 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
2232 DS4_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
2233 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002234
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002235 if (ret != DS4_REPORT_0x81_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002236 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002237 ret = ret < 0 ? ret : -EINVAL;
2238 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002239 }
2240
2241 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
Simon Wood4545ee02015-06-17 00:08:52 -06002242 } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2243 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002244 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2245 if (!buf)
2246 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002247
2248 /*
2249 * The MAC address of a Sixaxis controller connected via USB can
2250 * be retrieved with feature report 0xf2. The address begins at
2251 * offset 4.
2252 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002253 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2254 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2255 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002256
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002257 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002258 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002259 ret = ret < 0 ? ret : -EINVAL;
2260 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002261 }
2262
2263 /*
2264 * The Sixaxis device MAC in the report is big-endian and must
2265 * be byte-swapped.
2266 */
2267 for (n = 0; n < 6; n++)
2268 sc->mac_address[5-n] = buf[4+n];
2269 } else {
2270 return 0;
2271 }
2272
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002273 ret = sony_check_add_dev_list(sc);
2274
2275out_free:
2276
2277 kfree(buf);
2278
2279 return ret;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002280}
2281
Frank Praznik80250872014-04-14 10:11:35 -04002282static int sony_set_device_id(struct sony_sc *sc)
2283{
2284 int ret;
2285
2286 /*
2287 * Only DualShock 4 or Sixaxis controllers get an id.
2288 * All others are set to -1.
2289 */
2290 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2291 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2292 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2293 GFP_KERNEL);
2294 if (ret < 0) {
2295 sc->device_id = -1;
2296 return ret;
2297 }
2298 sc->device_id = ret;
2299 } else {
2300 sc->device_id = -1;
2301 }
2302
2303 return 0;
2304}
2305
2306static void sony_release_device_id(struct sony_sc *sc)
2307{
2308 if (sc->device_id >= 0) {
2309 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2310 sc->device_id = -1;
2311 }
2312}
2313
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002314static inline void sony_init_output_report(struct sony_sc *sc,
Antonio Ospite09593e32016-02-09 13:55:06 +01002315 void (*send_output_report)(struct sony_sc *))
Frank Praznik46262042014-04-14 10:11:31 -04002316{
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002317 sc->send_output_report = send_output_report;
2318
Frank Praznik46262042014-04-14 10:11:31 -04002319 if (!sc->worker_initialized)
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002320 INIT_WORK(&sc->state_worker, sony_state_worker);
Frank Praznik46262042014-04-14 10:11:31 -04002321
2322 sc->worker_initialized = 1;
2323}
2324
2325static inline void sony_cancel_work_sync(struct sony_sc *sc)
2326{
2327 if (sc->worker_initialized)
2328 cancel_work_sync(&sc->state_worker);
2329}
Frank Praznikd2d782f2014-02-20 11:36:03 -05002330
Jiri Slabybd28ce02008-06-25 23:47:04 +02002331static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2332{
2333 int ret;
Frank Praznik0f398232016-09-22 20:18:08 -04002334 int append_dev_id;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002335 unsigned long quirks = id->driver_data;
2336 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02002337 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002338
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002339 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002340 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08002341 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002342 return -ENOMEM;
2343 }
2344
Frank Praznikb94993f2015-02-22 20:42:46 -05002345 spin_lock_init(&sc->lock);
2346
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002347 sc->quirks = quirks;
2348 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002349 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002350
Jiri Slabybd28ce02008-06-25 23:47:04 +02002351 ret = hid_parse(hdev);
2352 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002353 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002354 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002355 }
2356
Colin Leitnerf04d5142013-05-27 23:41:05 +02002357 if (sc->quirks & VAIO_RDESC_CONSTANT)
2358 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
Antonio Ospite50764652014-06-24 13:28:40 +02002359 else if (sc->quirks & SIXAXIS_CONTROLLER)
Colin Leitnerf04d5142013-05-27 23:41:05 +02002360 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2361
2362 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002363 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002364 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002365 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002366 }
2367
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) {
Frank Praznik68330d82014-02-05 20:03:49 -05002413 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04002414 /*
2415 * The DualShock 4 wants output reports sent on the ctrl
2416 * endpoint when connected via Bluetooth.
2417 */
2418 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Frank Praznik68330d82014-02-05 20:03:49 -05002419 ret = dualshock4_set_operational_bt(hdev);
2420 if (ret < 0) {
2421 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
2422 goto err_stop;
2423 }
2424 }
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002425
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002426 sony_init_output_report(sc, dualshock4_send_output_report);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002427 } else if (sc->quirks & MOTION_CONTROLLER) {
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002428 sony_init_output_report(sc, motion_send_output_report);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002429 } else {
2430 ret = 0;
2431 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00002432
Jiri Kosina4dfdc462008-12-30 00:49:59 +01002433 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002434 goto err_stop;
2435
Frank Praznik0f398232016-09-22 20:18:08 -04002436 ret = append_dev_id = sony_check_add(sc);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002437 if (ret < 0)
2438 goto err_stop;
2439
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002440 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002441 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002442 if (ret < 0)
2443 goto err_stop;
2444 }
2445
Frank Praznikd902f472014-01-27 10:17:36 -05002446 if (sc->quirks & SONY_BATTERY_SUPPORT) {
Frank Praznik0f398232016-09-22 20:18:08 -04002447 ret = sony_battery_probe(sc, append_dev_id);
Frank Praznikd902f472014-01-27 10:17:36 -05002448 if (ret < 0)
2449 goto err_stop;
2450
2451 /* Open the device to receive reports with battery info */
2452 ret = hid_hw_open(hdev);
2453 if (ret < 0) {
2454 hid_err(hdev, "hw open failed\n");
2455 goto err_stop;
2456 }
2457 }
2458
Frank Praznikc8de9db2014-02-20 11:36:01 -05002459 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002460 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05002461 if (ret < 0)
2462 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05002463 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002464
Jiri Slabybd28ce02008-06-25 23:47:04 +02002465 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05002466err_close:
2467 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002468err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002469 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002470 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002471 if (sc->quirks & SONY_BATTERY_SUPPORT)
2472 sony_battery_remove(sc);
Frank Praznik46262042014-04-14 10:11:31 -04002473 sony_cancel_work_sync(sc);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002474 kfree(sc->output_report_dmabuf);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002475 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04002476 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002477 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002478 return ret;
2479}
2480
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002481static void sony_remove(struct hid_device *hdev)
2482{
Colin Leitnerf04d5142013-05-27 23:41:05 +02002483 struct sony_sc *sc = hid_get_drvdata(hdev);
2484
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002485 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002486 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02002487
Frank Praznikd902f472014-01-27 10:17:36 -05002488 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2489 hid_hw_close(hdev);
2490 sony_battery_remove(sc);
2491 }
2492
Frank Praznik46262042014-04-14 10:11:31 -04002493 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002494
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002495 kfree(sc->output_report_dmabuf);
2496
Frank Praznikd2d782f2014-02-20 11:36:03 -05002497 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002498
Frank Praznik80250872014-04-14 10:11:35 -04002499 sony_release_device_id(sc);
2500
Jiri Slabybd28ce02008-06-25 23:47:04 +02002501 hid_hw_stop(hdev);
2502}
2503
Frank Praznikdecd9462015-11-11 09:49:38 -05002504#ifdef CONFIG_PM
2505
2506static int sony_suspend(struct hid_device *hdev, pm_message_t message)
2507{
2508 /*
2509 * On suspend save the current LED state,
2510 * stop running force-feedback and blank the LEDS.
Antonio Ospite09593e32016-02-09 13:55:06 +01002511 */
Frank Praznikdecd9462015-11-11 09:49:38 -05002512 if (SONY_LED_SUPPORT || SONY_FF_SUPPORT) {
2513 struct sony_sc *sc = hid_get_drvdata(hdev);
2514
2515#ifdef CONFIG_SONY_FF
2516 sc->left = sc->right = 0;
2517#endif
2518
2519 memcpy(sc->resume_led_state, sc->led_state,
2520 sizeof(sc->resume_led_state));
2521 memset(sc->led_state, 0, sizeof(sc->led_state));
2522
2523 sony_send_output_report(sc);
2524 }
2525
2526 return 0;
2527}
2528
2529static int sony_resume(struct hid_device *hdev)
2530{
2531 /* Restore the state of controller LEDs on resume */
2532 if (SONY_LED_SUPPORT) {
2533 struct sony_sc *sc = hid_get_drvdata(hdev);
2534
2535 memcpy(sc->led_state, sc->resume_led_state,
2536 sizeof(sc->led_state));
2537
2538 /*
2539 * The Sixaxis and navigation controllers on USB need to be
2540 * reinitialized on resume or they won't behave properly.
2541 */
2542 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
Frank Praznik2a242932016-09-22 20:18:09 -04002543 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
Frank Praznikdecd9462015-11-11 09:49:38 -05002544 sixaxis_set_operational_usb(sc->hdev);
Frank Praznik2a242932016-09-22 20:18:09 -04002545 sc->defer_initialization = 1;
2546 }
Frank Praznikdecd9462015-11-11 09:49:38 -05002547
2548 sony_set_leds(sc);
2549 }
2550
2551 return 0;
2552}
2553
2554#endif
2555
Jiri Slabybd28ce02008-06-25 23:47:04 +02002556static const struct hid_device_id sony_devices[] = {
2557 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2558 .driver_data = SIXAXIS_CONTROLLER_USB },
2559 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
Simon Wood4545ee02015-06-17 00:08:52 -06002560 .driver_data = NAVIGATION_CONTROLLER_USB },
Simon Wood6eabaaa2015-06-17 00:08:51 -06002561 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
Simon Wood4545ee02015-06-17 00:08:52 -06002562 .driver_data = NAVIGATION_CONTROLLER_BT },
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002563 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002564 .driver_data = MOTION_CONTROLLER_USB },
Simon Wooda4afa852015-06-03 09:45:19 -06002565 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002566 .driver_data = MOTION_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002567 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2568 .driver_data = SIXAXIS_CONTROLLER_BT },
2569 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2570 .driver_data = VAIO_RDESC_CONSTANT },
2571 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2572 .driver_data = VAIO_RDESC_CONSTANT },
Antonio Ospiteef916ef2016-02-09 13:55:07 +01002573 /*
2574 * Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2575 * Logitech joystick from the device descriptor.
2576 */
Jiri Slabybd28ce02008-06-25 23:47:04 +02002577 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2578 .driver_data = BUZZ_CONTROLLER },
2579 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2580 .driver_data = BUZZ_CONTROLLER },
2581 /* PS3 BD Remote Control */
2582 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2583 .driver_data = PS3REMOTE },
2584 /* Logitech Harmony Adapter for PS3 */
2585 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2586 .driver_data = PS3REMOTE },
Frank Praznik68a49e52014-11-12 14:52:28 -05002587 /* SMK-Link PS3 BD Remote Control */
2588 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2589 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002590 /* Sony Dualshock 4 controllers for PS4 */
2591 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002592 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002593 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002594 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Scott Moreau74500cc2016-01-13 07:40:42 -07002595 /* Nyko Core Controller for PS3 */
2596 { HID_USB_DEVICE(USB_VENDOR_ID_SINO_LITE, USB_DEVICE_ID_SINO_LITE_CONTROLLER),
2597 .driver_data = SIXAXIS_CONTROLLER_USB | SINO_LITE_CONTROLLER },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002598 { }
2599};
2600MODULE_DEVICE_TABLE(hid, sony_devices);
2601
2602static struct hid_driver sony_driver = {
Frank Praznikce8efc32014-09-18 21:15:01 -04002603 .name = "sony",
2604 .id_table = sony_devices,
2605 .input_mapping = sony_mapping,
2606 .input_configured = sony_input_configured,
2607 .probe = sony_probe,
2608 .remove = sony_remove,
2609 .report_fixup = sony_report_fixup,
Frank Praznikdecd9462015-11-11 09:49:38 -05002610 .raw_event = sony_raw_event,
2611
2612#ifdef CONFIG_PM
2613 .suspend = sony_suspend,
2614 .resume = sony_resume,
2615 .reset_resume = sony_resume,
2616#endif
Jiri Slabybd28ce02008-06-25 23:47:04 +02002617};
Frank Praznik80250872014-04-14 10:11:35 -04002618
2619static int __init sony_init(void)
2620{
2621 dbg_hid("Sony:%s\n", __func__);
2622
2623 return hid_register_driver(&sony_driver);
2624}
2625
2626static void __exit sony_exit(void)
2627{
2628 dbg_hid("Sony:%s\n", __func__);
2629
Frank Praznik80250872014-04-14 10:11:35 -04002630 hid_unregister_driver(&sony_driver);
Antonio Ospite6c400652015-02-16 22:58:24 +01002631 ida_destroy(&sony_device_id_allocator);
Frank Praznik80250872014-04-14 10:11:35 -04002632}
2633module_init(sony_init);
2634module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002635
2636MODULE_LICENSE("GPL");