blob: 29a1aa90c29450c82989110d1b8740b13099de07 [file] [log] [blame]
Jiri Slabybd28ce02008-06-25 23:47:04 +02001/*
Frank Praznik077147a2014-09-14 11:56:39 -04002 * HID driver for Sony / PS2 / PS3 / PS4 BD devices.
Jiri Slabybd28ce02008-06-25 23:47:04 +02003 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
Jiri Slabybd28ce02008-06-25 23:47:04 +02007 * Copyright (c) 2008 Jiri Slaby
Jiri Kosina078328d2013-06-13 12:03:49 +02008 * Copyright (c) 2012 David Dillow <dave@thedillows.org>
9 * Copyright (c) 2006-2013 Jiri Kosina
Colin Leitnerf04d5142013-05-27 23:41:05 +020010 * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
Frank Praznik077147a2014-09-14 11:56:39 -040011 * Copyright (c) 2014 Frank Praznik <frank.praznik@gmail.com>
Jiri Slabybd28ce02008-06-25 23:47:04 +020012 */
13
14/*
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 2 of the License, or (at your option)
18 * any later version.
19 */
20
Frank Praznikad142b92014-02-20 11:36:00 -050021/*
22 * NOTE: in order for the Sony PS3 BD Remote Control to be found by
Jiri Kosina078328d2013-06-13 12:03:49 +020023 * a Bluetooth host, the key combination Start+Enter has to be kept pressed
24 * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
25 *
26 * There will be no PIN request from the device.
27 */
28
Jiri Slabybd28ce02008-06-25 23:47:04 +020029#include <linux/device.h>
30#include <linux/hid.h>
31#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jiri Kosina40e32ee2013-05-28 11:22:09 +020033#include <linux/leds.h>
Frank Praznikd902f472014-01-27 10:17:36 -050034#include <linux/power_supply.h>
35#include <linux/spinlock.h>
Frank Praznikd2d782f2014-02-20 11:36:03 -050036#include <linux/list.h>
Frank Praznik80250872014-04-14 10:11:35 -040037#include <linux/idr.h>
Frank Praznike5606232014-01-27 10:17:37 -050038#include <linux/input/mt.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020039
40#include "hid-ids.h"
41
Frank Praznik6c79c182014-01-16 21:43:03 -050042#define VAIO_RDESC_CONSTANT BIT(0)
43#define SIXAXIS_CONTROLLER_USB BIT(1)
44#define SIXAXIS_CONTROLLER_BT BIT(2)
45#define BUZZ_CONTROLLER BIT(3)
46#define PS3REMOTE BIT(4)
Frank Praznik8ab16762014-01-16 21:42:31 -050047#define DUALSHOCK4_CONTROLLER_USB BIT(5)
48#define DUALSHOCK4_CONTROLLER_BT BIT(6)
Simon Woodb3bca322015-06-09 21:27:04 -060049#define MOTION_CONTROLLER_USB BIT(7)
50#define MOTION_CONTROLLER_BT BIT(8)
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)
Mikko Perttunen4ba1eee2016-07-21 19:54:48 +030054#define FUTUREMAX_DANCE_MAT BIT(12)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020055
Frank Praznikfee4e2d2014-02-18 17:22:01 -050056#define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
Simon Woodb3bca322015-06-09 21:27:04 -060057#define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
Simon Wood4545ee02015-06-17 00:08:52 -060058#define NAVIGATION_CONTROLLER (NAVIGATION_CONTROLLER_USB |\
59 NAVIGATION_CONTROLLER_BT)
Frank Praznik68330d82014-02-05 20:03:49 -050060#define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
61 DUALSHOCK4_CONTROLLER_BT)
Frank Praznikfee4e2d2014-02-18 17:22:01 -050062#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
Simon Wood4545ee02015-06-17 00:08:52 -060063 DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER |\
64 NAVIGATION_CONTROLLER)
Simon Wood12e9a6d2015-06-09 21:27:05 -060065#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
Simon Wood4545ee02015-06-17 00:08:52 -060066 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040067#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
68 MOTION_CONTROLLER)
Frank Praznik60781cf2014-01-11 15:13:15 -050069
70#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010071
Frank Praznik4c3e8292015-05-05 20:47:33 -040072/*
73 * The Sixaxis reports both digital and analog values for each button on the
74 * controller except for Start, Select and the PS button. The controller ends
75 * up reporting 27 axes which causes them to spill over into the multi-touch
76 * axis values. Additionally, the controller only has 20 actual, physical axes
77 * so there are several unused axes in between the used ones.
78 */
Pavel Machek1adf9042016-02-09 13:55:08 +010079static u8 sixaxis_rdesc[] = {
Antonio Ospitefb705a62014-06-24 13:28:42 +020080 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznik4c3e8292015-05-05 20:47:33 -040081 0x09, 0x04, /* Usage (Joystick), */
Antonio Ospitefb705a62014-06-24 13:28:42 +020082 0xA1, 0x01, /* Collection (Application), */
83 0xA1, 0x02, /* Collection (Logical), */
84 0x85, 0x01, /* Report ID (1), */
85 0x75, 0x08, /* Report Size (8), */
86 0x95, 0x01, /* Report Count (1), */
87 0x15, 0x00, /* Logical Minimum (0), */
88 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
89 0x81, 0x03, /* Input (Constant, Variable), */
90 0x75, 0x01, /* Report Size (1), */
91 0x95, 0x13, /* Report Count (19), */
92 0x15, 0x00, /* Logical Minimum (0), */
93 0x25, 0x01, /* Logical Maximum (1), */
94 0x35, 0x00, /* Physical Minimum (0), */
95 0x45, 0x01, /* Physical Maximum (1), */
96 0x05, 0x09, /* Usage Page (Button), */
97 0x19, 0x01, /* Usage Minimum (01h), */
98 0x29, 0x13, /* Usage Maximum (13h), */
99 0x81, 0x02, /* Input (Variable), */
100 0x75, 0x01, /* Report Size (1), */
101 0x95, 0x0D, /* Report Count (13), */
102 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
103 0x81, 0x03, /* Input (Constant, Variable), */
104 0x15, 0x00, /* Logical Minimum (0), */
105 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
106 0x05, 0x01, /* Usage Page (Desktop), */
107 0x09, 0x01, /* Usage (Pointer), */
108 0xA1, 0x00, /* Collection (Physical), */
109 0x75, 0x08, /* Report Size (8), */
110 0x95, 0x04, /* Report Count (4), */
111 0x35, 0x00, /* Physical Minimum (0), */
112 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
113 0x09, 0x30, /* Usage (X), */
114 0x09, 0x31, /* Usage (Y), */
115 0x09, 0x32, /* Usage (Z), */
116 0x09, 0x35, /* Usage (Rz), */
117 0x81, 0x02, /* Input (Variable), */
118 0xC0, /* End Collection, */
119 0x05, 0x01, /* Usage Page (Desktop), */
120 0x95, 0x13, /* Report Count (19), */
121 0x09, 0x01, /* Usage (Pointer), */
122 0x81, 0x02, /* Input (Variable), */
123 0x95, 0x0C, /* Report Count (12), */
124 0x81, 0x01, /* Input (Constant), */
125 0x75, 0x10, /* Report Size (16), */
126 0x95, 0x04, /* Report Count (4), */
127 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
128 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
129 0x09, 0x01, /* Usage (Pointer), */
130 0x81, 0x02, /* Input (Variable), */
131 0xC0, /* End Collection, */
132 0xA1, 0x02, /* Collection (Logical), */
133 0x85, 0x02, /* Report ID (2), */
134 0x75, 0x08, /* Report Size (8), */
135 0x95, 0x30, /* Report Count (48), */
136 0x09, 0x01, /* Usage (Pointer), */
137 0xB1, 0x02, /* Feature (Variable), */
138 0xC0, /* End Collection, */
139 0xA1, 0x02, /* Collection (Logical), */
140 0x85, 0xEE, /* Report ID (238), */
141 0x75, 0x08, /* Report Size (8), */
142 0x95, 0x30, /* Report Count (48), */
143 0x09, 0x01, /* Usage (Pointer), */
144 0xB1, 0x02, /* Feature (Variable), */
145 0xC0, /* End Collection, */
146 0xA1, 0x02, /* Collection (Logical), */
147 0x85, 0xEF, /* Report ID (239), */
148 0x75, 0x08, /* Report Size (8), */
149 0x95, 0x30, /* Report Count (48), */
150 0x09, 0x01, /* Usage (Pointer), */
151 0xB1, 0x02, /* Feature (Variable), */
152 0xC0, /* End Collection, */
153 0xC0 /* End Collection */
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200154};
155
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400156/* PS/3 Motion controller */
Pavel Machek1adf9042016-02-09 13:55:08 +0100157static u8 motion_rdesc[] = {
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400158 0x05, 0x01, /* Usage Page (Desktop), */
159 0x09, 0x04, /* Usage (Joystick), */
160 0xA1, 0x01, /* Collection (Application), */
161 0xA1, 0x02, /* Collection (Logical), */
162 0x85, 0x01, /* Report ID (1), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400163 0x75, 0x01, /* Report Size (1), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600164 0x95, 0x15, /* Report Count (21), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400165 0x15, 0x00, /* Logical Minimum (0), */
166 0x25, 0x01, /* Logical Maximum (1), */
167 0x35, 0x00, /* Physical Minimum (0), */
168 0x45, 0x01, /* Physical Maximum (1), */
169 0x05, 0x09, /* Usage Page (Button), */
170 0x19, 0x01, /* Usage Minimum (01h), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600171 0x29, 0x15, /* Usage Maximum (15h), */
172 0x81, 0x02, /* Input (Variable), * Buttons */
173 0x95, 0x0B, /* Report Count (11), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400174 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600175 0x81, 0x03, /* Input (Constant, Variable), * Padding */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400176 0x15, 0x00, /* Logical Minimum (0), */
177 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
178 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400179 0xA1, 0x00, /* Collection (Physical), */
180 0x75, 0x08, /* Report Size (8), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600181 0x95, 0x01, /* Report Count (1), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400182 0x35, 0x00, /* Physical Minimum (0), */
183 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
184 0x09, 0x30, /* Usage (X), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600185 0x81, 0x02, /* Input (Variable), * Trigger */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400186 0xC0, /* End Collection, */
Simon Wood8b2513c2015-06-09 21:27:07 -0600187 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
188 0x75, 0x08, /* Report Size (8), */
189 0x95, 0x07, /* Report Count (7), * skip 7 bytes */
190 0x81, 0x02, /* Input (Variable), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400191 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400192 0x75, 0x10, /* Report Size (16), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600193 0x46, 0xFF, 0xFF, /* Physical Maximum (65535), */
194 0x27, 0xFF, 0xFF, 0x00, 0x00, /* Logical Maximum (65535), */
195 0x95, 0x03, /* Report Count (3), * 3x Accels */
196 0x09, 0x33, /* Usage (rX), */
197 0x09, 0x34, /* Usage (rY), */
198 0x09, 0x35, /* Usage (rZ), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400199 0x81, 0x02, /* Input (Variable), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600200 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
201 0x95, 0x03, /* Report Count (3), * Skip Accels 2nd frame */
202 0x81, 0x02, /* Input (Variable), */
203 0x05, 0x01, /* Usage Page (Desktop), */
204 0x09, 0x01, /* Usage (Pointer), */
205 0x95, 0x03, /* Report Count (3), * 3x Gyros */
206 0x81, 0x02, /* Input (Variable), */
207 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
208 0x95, 0x03, /* Report Count (3), * Skip Gyros 2nd frame */
209 0x81, 0x02, /* Input (Variable), */
210 0x75, 0x0C, /* Report Size (12), */
211 0x46, 0xFF, 0x0F, /* Physical Maximum (4095), */
212 0x26, 0xFF, 0x0F, /* Logical Maximum (4095), */
213 0x95, 0x04, /* Report Count (4), * Skip Temp and Magnetometers */
214 0x81, 0x02, /* Input (Variable), */
215 0x75, 0x08, /* Report Size (8), */
216 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
217 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
218 0x95, 0x06, /* Report Count (6), * Skip Timestamp and Extension Bytes */
219 0x81, 0x02, /* Input (Variable), */
220 0x75, 0x08, /* Report Size (8), */
221 0x95, 0x30, /* Report Count (48), */
222 0x09, 0x01, /* Usage (Pointer), */
223 0x91, 0x02, /* Output (Variable), */
224 0x75, 0x08, /* Report Size (8), */
225 0x95, 0x30, /* Report Count (48), */
226 0x09, 0x01, /* Usage (Pointer), */
227 0xB1, 0x02, /* Feature (Variable), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400228 0xC0, /* End Collection, */
229 0xA1, 0x02, /* Collection (Logical), */
230 0x85, 0x02, /* Report ID (2), */
231 0x75, 0x08, /* Report Size (8), */
232 0x95, 0x30, /* Report Count (48), */
233 0x09, 0x01, /* Usage (Pointer), */
234 0xB1, 0x02, /* Feature (Variable), */
235 0xC0, /* End Collection, */
236 0xA1, 0x02, /* Collection (Logical), */
237 0x85, 0xEE, /* Report ID (238), */
238 0x75, 0x08, /* Report Size (8), */
239 0x95, 0x30, /* Report Count (48), */
240 0x09, 0x01, /* Usage (Pointer), */
241 0xB1, 0x02, /* Feature (Variable), */
242 0xC0, /* End Collection, */
243 0xA1, 0x02, /* Collection (Logical), */
244 0x85, 0xEF, /* Report ID (239), */
245 0x75, 0x08, /* Report Size (8), */
246 0x95, 0x30, /* Report Count (48), */
247 0x09, 0x01, /* Usage (Pointer), */
248 0xB1, 0x02, /* Feature (Variable), */
249 0xC0, /* End Collection, */
250 0xC0 /* End Collection */
251};
252
Simon Woodb2723eb2015-06-17 00:08:53 -0600253/* PS/3 Navigation controller */
Pavel Machek1adf9042016-02-09 13:55:08 +0100254static u8 navigation_rdesc[] = {
Simon Woodb2723eb2015-06-17 00:08:53 -0600255 0x05, 0x01, /* Usage Page (Desktop), */
Antonio Ospited5421762016-01-28 18:23:43 +0100256 0x09, 0x04, /* Usage (Joystick), */
Simon Woodb2723eb2015-06-17 00:08:53 -0600257 0xA1, 0x01, /* Collection (Application), */
258 0xA1, 0x02, /* Collection (Logical), */
259 0x85, 0x01, /* Report ID (1), */
260 0x75, 0x08, /* Report Size (8), */
261 0x95, 0x01, /* Report Count (1), */
262 0x15, 0x00, /* Logical Minimum (0), */
263 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
264 0x81, 0x03, /* Input (Constant, Variable), */
265 0x75, 0x01, /* Report Size (1), */
266 0x95, 0x13, /* Report Count (19), */
267 0x15, 0x00, /* Logical Minimum (0), */
268 0x25, 0x01, /* Logical Maximum (1), */
269 0x35, 0x00, /* Physical Minimum (0), */
270 0x45, 0x01, /* Physical Maximum (1), */
271 0x05, 0x09, /* Usage Page (Button), */
272 0x19, 0x01, /* Usage Minimum (01h), */
273 0x29, 0x13, /* Usage Maximum (13h), */
274 0x81, 0x02, /* Input (Variable), */
275 0x75, 0x01, /* Report Size (1), */
276 0x95, 0x0D, /* Report Count (13), */
277 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
278 0x81, 0x03, /* Input (Constant, Variable), */
279 0x15, 0x00, /* Logical Minimum (0), */
280 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
281 0x05, 0x01, /* Usage Page (Desktop), */
282 0x09, 0x01, /* Usage (Pointer), */
283 0xA1, 0x00, /* Collection (Physical), */
284 0x75, 0x08, /* Report Size (8), */
285 0x95, 0x02, /* Report Count (2), */
286 0x35, 0x00, /* Physical Minimum (0), */
287 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
288 0x09, 0x30, /* Usage (X), */
289 0x09, 0x31, /* Usage (Y), */
290 0x81, 0x02, /* Input (Variable), */
291 0xC0, /* End Collection, */
292 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
293 0x95, 0x06, /* Report Count (6), */
294 0x81, 0x03, /* Input (Constant, Variable), */
295 0x05, 0x01, /* Usage Page (Desktop), */
296 0x75, 0x08, /* Report Size (8), */
297 0x95, 0x05, /* Report Count (5), */
298 0x09, 0x01, /* Usage (Pointer), */
299 0x81, 0x02, /* Input (Variable), */
300 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
Simon Wood2259b5bb2015-07-10 00:10:21 -0600301 0x95, 0x01, /* Report Count (1), */
302 0x81, 0x02, /* Input (Variable), */
303 0x05, 0x01, /* Usage Page (Desktop), */
304 0x95, 0x01, /* Report Count (1), */
305 0x09, 0x01, /* Usage (Pointer), */
306 0x81, 0x02, /* Input (Variable), */
307 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
308 0x95, 0x1E, /* Report Count (24), */
Simon Woodb2723eb2015-06-17 00:08:53 -0600309 0x81, 0x02, /* Input (Variable), */
310 0x75, 0x08, /* Report Size (8), */
311 0x95, 0x30, /* Report Count (48), */
312 0x09, 0x01, /* Usage (Pointer), */
313 0x91, 0x02, /* Output (Variable), */
314 0x75, 0x08, /* Report Size (8), */
315 0x95, 0x30, /* Report Count (48), */
316 0x09, 0x01, /* Usage (Pointer), */
317 0xB1, 0x02, /* Feature (Variable), */
318 0xC0, /* End Collection, */
319 0xA1, 0x02, /* Collection (Logical), */
320 0x85, 0x02, /* Report ID (2), */
321 0x75, 0x08, /* Report Size (8), */
322 0x95, 0x30, /* Report Count (48), */
323 0x09, 0x01, /* Usage (Pointer), */
324 0xB1, 0x02, /* Feature (Variable), */
325 0xC0, /* End Collection, */
326 0xA1, 0x02, /* Collection (Logical), */
327 0x85, 0xEE, /* Report ID (238), */
328 0x75, 0x08, /* Report Size (8), */
329 0x95, 0x30, /* Report Count (48), */
330 0x09, 0x01, /* Usage (Pointer), */
331 0xB1, 0x02, /* Feature (Variable), */
332 0xC0, /* End Collection, */
333 0xA1, 0x02, /* Collection (Logical), */
334 0x85, 0xEF, /* Report ID (239), */
335 0x75, 0x08, /* Report Size (8), */
336 0x95, 0x30, /* Report Count (48), */
337 0x09, 0x01, /* Usage (Pointer), */
338 0xB1, 0x02, /* Feature (Variable), */
339 0xC0, /* End Collection, */
340 0xC0 /* End Collection */
341};
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400342
Frank Praznikad142b92014-02-20 11:36:00 -0500343/*
344 * The default descriptor doesn't provide mapping for the accelerometers
Frank Praznik58d70272014-01-20 12:27:01 -0500345 * or orientation sensors. This fixed descriptor maps the accelerometers
346 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
347 * to usage values 0x43, 0x44 and 0x45.
348 */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500349static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -0500350 0x05, 0x01, /* Usage Page (Desktop), */
351 0x09, 0x05, /* Usage (Gamepad), */
352 0xA1, 0x01, /* Collection (Application), */
353 0x85, 0x01, /* Report ID (1), */
354 0x09, 0x30, /* Usage (X), */
355 0x09, 0x31, /* Usage (Y), */
356 0x09, 0x32, /* Usage (Z), */
357 0x09, 0x35, /* Usage (Rz), */
358 0x15, 0x00, /* Logical Minimum (0), */
359 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
360 0x75, 0x08, /* Report Size (8), */
361 0x95, 0x04, /* Report Count (4), */
362 0x81, 0x02, /* Input (Variable), */
363 0x09, 0x39, /* Usage (Hat Switch), */
364 0x15, 0x00, /* Logical Minimum (0), */
365 0x25, 0x07, /* Logical Maximum (7), */
366 0x35, 0x00, /* Physical Minimum (0), */
367 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
368 0x65, 0x14, /* Unit (Degrees), */
369 0x75, 0x04, /* Report Size (4), */
370 0x95, 0x01, /* Report Count (1), */
371 0x81, 0x42, /* Input (Variable, Null State), */
372 0x65, 0x00, /* Unit, */
373 0x05, 0x09, /* Usage Page (Button), */
374 0x19, 0x01, /* Usage Minimum (01h), */
375 0x29, 0x0E, /* Usage Maximum (0Eh), */
376 0x15, 0x00, /* Logical Minimum (0), */
377 0x25, 0x01, /* Logical Maximum (1), */
378 0x75, 0x01, /* Report Size (1), */
379 0x95, 0x0E, /* Report Count (14), */
380 0x81, 0x02, /* Input (Variable), */
381 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
382 0x09, 0x20, /* Usage (20h), */
383 0x75, 0x06, /* Report Size (6), */
384 0x95, 0x01, /* Report Count (1), */
385 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400386 0x25, 0x3F, /* Logical Maximum (63), */
Frank Praznik58d70272014-01-20 12:27:01 -0500387 0x81, 0x02, /* Input (Variable), */
388 0x05, 0x01, /* Usage Page (Desktop), */
389 0x09, 0x33, /* Usage (Rx), */
390 0x09, 0x34, /* Usage (Ry), */
391 0x15, 0x00, /* Logical Minimum (0), */
392 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
393 0x75, 0x08, /* Report Size (8), */
394 0x95, 0x02, /* Report Count (2), */
395 0x81, 0x02, /* Input (Variable), */
396 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
397 0x09, 0x21, /* Usage (21h), */
398 0x95, 0x03, /* Report Count (3), */
399 0x81, 0x02, /* Input (Variable), */
400 0x05, 0x01, /* Usage Page (Desktop), */
401 0x19, 0x40, /* Usage Minimum (40h), */
402 0x29, 0x42, /* Usage Maximum (42h), */
403 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
404 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
405 0x75, 0x10, /* Report Size (16), */
406 0x95, 0x03, /* Report Count (3), */
407 0x81, 0x02, /* Input (Variable), */
408 0x19, 0x43, /* Usage Minimum (43h), */
409 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400410 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
411 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznik58d70272014-01-20 12:27:01 -0500412 0x95, 0x03, /* Report Count (3), */
413 0x81, 0x02, /* Input (Variable), */
414 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
415 0x09, 0x21, /* Usage (21h), */
416 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400417 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
Frank Praznik58d70272014-01-20 12:27:01 -0500418 0x75, 0x08, /* Report Size (8), */
419 0x95, 0x27, /* Report Count (39), */
420 0x81, 0x02, /* Input (Variable), */
421 0x85, 0x05, /* Report ID (5), */
422 0x09, 0x22, /* Usage (22h), */
423 0x95, 0x1F, /* Report Count (31), */
424 0x91, 0x02, /* Output (Variable), */
425 0x85, 0x04, /* Report ID (4), */
426 0x09, 0x23, /* Usage (23h), */
427 0x95, 0x24, /* Report Count (36), */
428 0xB1, 0x02, /* Feature (Variable), */
429 0x85, 0x02, /* Report ID (2), */
430 0x09, 0x24, /* Usage (24h), */
431 0x95, 0x24, /* Report Count (36), */
432 0xB1, 0x02, /* Feature (Variable), */
433 0x85, 0x08, /* Report ID (8), */
434 0x09, 0x25, /* Usage (25h), */
435 0x95, 0x03, /* Report Count (3), */
436 0xB1, 0x02, /* Feature (Variable), */
437 0x85, 0x10, /* Report ID (16), */
438 0x09, 0x26, /* Usage (26h), */
439 0x95, 0x04, /* Report Count (4), */
440 0xB1, 0x02, /* Feature (Variable), */
441 0x85, 0x11, /* Report ID (17), */
442 0x09, 0x27, /* Usage (27h), */
443 0x95, 0x02, /* Report Count (2), */
444 0xB1, 0x02, /* Feature (Variable), */
445 0x85, 0x12, /* Report ID (18), */
446 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
447 0x09, 0x21, /* Usage (21h), */
448 0x95, 0x0F, /* Report Count (15), */
449 0xB1, 0x02, /* Feature (Variable), */
450 0x85, 0x13, /* Report ID (19), */
451 0x09, 0x22, /* Usage (22h), */
452 0x95, 0x16, /* Report Count (22), */
453 0xB1, 0x02, /* Feature (Variable), */
454 0x85, 0x14, /* Report ID (20), */
455 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
456 0x09, 0x20, /* Usage (20h), */
457 0x95, 0x10, /* Report Count (16), */
458 0xB1, 0x02, /* Feature (Variable), */
459 0x85, 0x15, /* Report ID (21), */
460 0x09, 0x21, /* Usage (21h), */
461 0x95, 0x2C, /* Report Count (44), */
462 0xB1, 0x02, /* Feature (Variable), */
463 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
464 0x85, 0x80, /* Report ID (128), */
465 0x09, 0x20, /* Usage (20h), */
466 0x95, 0x06, /* Report Count (6), */
467 0xB1, 0x02, /* Feature (Variable), */
468 0x85, 0x81, /* Report ID (129), */
469 0x09, 0x21, /* Usage (21h), */
470 0x95, 0x06, /* Report Count (6), */
471 0xB1, 0x02, /* Feature (Variable), */
472 0x85, 0x82, /* Report ID (130), */
473 0x09, 0x22, /* Usage (22h), */
474 0x95, 0x05, /* Report Count (5), */
475 0xB1, 0x02, /* Feature (Variable), */
476 0x85, 0x83, /* Report ID (131), */
477 0x09, 0x23, /* Usage (23h), */
478 0x95, 0x01, /* Report Count (1), */
479 0xB1, 0x02, /* Feature (Variable), */
480 0x85, 0x84, /* Report ID (132), */
481 0x09, 0x24, /* Usage (24h), */
482 0x95, 0x04, /* Report Count (4), */
483 0xB1, 0x02, /* Feature (Variable), */
484 0x85, 0x85, /* Report ID (133), */
485 0x09, 0x25, /* Usage (25h), */
486 0x95, 0x06, /* Report Count (6), */
487 0xB1, 0x02, /* Feature (Variable), */
488 0x85, 0x86, /* Report ID (134), */
489 0x09, 0x26, /* Usage (26h), */
490 0x95, 0x06, /* Report Count (6), */
491 0xB1, 0x02, /* Feature (Variable), */
492 0x85, 0x87, /* Report ID (135), */
493 0x09, 0x27, /* Usage (27h), */
494 0x95, 0x23, /* Report Count (35), */
495 0xB1, 0x02, /* Feature (Variable), */
496 0x85, 0x88, /* Report ID (136), */
497 0x09, 0x28, /* Usage (28h), */
498 0x95, 0x22, /* Report Count (34), */
499 0xB1, 0x02, /* Feature (Variable), */
500 0x85, 0x89, /* Report ID (137), */
501 0x09, 0x29, /* Usage (29h), */
502 0x95, 0x02, /* Report Count (2), */
503 0xB1, 0x02, /* Feature (Variable), */
504 0x85, 0x90, /* Report ID (144), */
505 0x09, 0x30, /* Usage (30h), */
506 0x95, 0x05, /* Report Count (5), */
507 0xB1, 0x02, /* Feature (Variable), */
508 0x85, 0x91, /* Report ID (145), */
509 0x09, 0x31, /* Usage (31h), */
510 0x95, 0x03, /* Report Count (3), */
511 0xB1, 0x02, /* Feature (Variable), */
512 0x85, 0x92, /* Report ID (146), */
513 0x09, 0x32, /* Usage (32h), */
514 0x95, 0x03, /* Report Count (3), */
515 0xB1, 0x02, /* Feature (Variable), */
516 0x85, 0x93, /* Report ID (147), */
517 0x09, 0x33, /* Usage (33h), */
518 0x95, 0x0C, /* Report Count (12), */
519 0xB1, 0x02, /* Feature (Variable), */
520 0x85, 0xA0, /* Report ID (160), */
521 0x09, 0x40, /* Usage (40h), */
522 0x95, 0x06, /* Report Count (6), */
523 0xB1, 0x02, /* Feature (Variable), */
524 0x85, 0xA1, /* Report ID (161), */
525 0x09, 0x41, /* Usage (41h), */
526 0x95, 0x01, /* Report Count (1), */
527 0xB1, 0x02, /* Feature (Variable), */
528 0x85, 0xA2, /* Report ID (162), */
529 0x09, 0x42, /* Usage (42h), */
530 0x95, 0x01, /* Report Count (1), */
531 0xB1, 0x02, /* Feature (Variable), */
532 0x85, 0xA3, /* Report ID (163), */
533 0x09, 0x43, /* Usage (43h), */
534 0x95, 0x30, /* Report Count (48), */
535 0xB1, 0x02, /* Feature (Variable), */
536 0x85, 0xA4, /* Report ID (164), */
537 0x09, 0x44, /* Usage (44h), */
538 0x95, 0x0D, /* Report Count (13), */
539 0xB1, 0x02, /* Feature (Variable), */
540 0x85, 0xA5, /* Report ID (165), */
541 0x09, 0x45, /* Usage (45h), */
542 0x95, 0x15, /* Report Count (21), */
543 0xB1, 0x02, /* Feature (Variable), */
544 0x85, 0xA6, /* Report ID (166), */
545 0x09, 0x46, /* Usage (46h), */
546 0x95, 0x15, /* Report Count (21), */
547 0xB1, 0x02, /* Feature (Variable), */
548 0x85, 0xF0, /* Report ID (240), */
549 0x09, 0x47, /* Usage (47h), */
550 0x95, 0x3F, /* Report Count (63), */
551 0xB1, 0x02, /* Feature (Variable), */
552 0x85, 0xF1, /* Report ID (241), */
553 0x09, 0x48, /* Usage (48h), */
554 0x95, 0x3F, /* Report Count (63), */
555 0xB1, 0x02, /* Feature (Variable), */
556 0x85, 0xF2, /* Report ID (242), */
557 0x09, 0x49, /* Usage (49h), */
558 0x95, 0x0F, /* Report Count (15), */
559 0xB1, 0x02, /* Feature (Variable), */
560 0x85, 0xA7, /* Report ID (167), */
561 0x09, 0x4A, /* Usage (4Ah), */
562 0x95, 0x01, /* Report Count (1), */
563 0xB1, 0x02, /* Feature (Variable), */
564 0x85, 0xA8, /* Report ID (168), */
565 0x09, 0x4B, /* Usage (4Bh), */
566 0x95, 0x01, /* Report Count (1), */
567 0xB1, 0x02, /* Feature (Variable), */
568 0x85, 0xA9, /* Report ID (169), */
569 0x09, 0x4C, /* Usage (4Ch), */
570 0x95, 0x08, /* Report Count (8), */
571 0xB1, 0x02, /* Feature (Variable), */
572 0x85, 0xAA, /* Report ID (170), */
573 0x09, 0x4E, /* Usage (4Eh), */
574 0x95, 0x01, /* Report Count (1), */
575 0xB1, 0x02, /* Feature (Variable), */
576 0x85, 0xAB, /* Report ID (171), */
577 0x09, 0x4F, /* Usage (4Fh), */
578 0x95, 0x39, /* Report Count (57), */
579 0xB1, 0x02, /* Feature (Variable), */
580 0x85, 0xAC, /* Report ID (172), */
581 0x09, 0x50, /* Usage (50h), */
582 0x95, 0x39, /* Report Count (57), */
583 0xB1, 0x02, /* Feature (Variable), */
584 0x85, 0xAD, /* Report ID (173), */
585 0x09, 0x51, /* Usage (51h), */
586 0x95, 0x0B, /* Report Count (11), */
587 0xB1, 0x02, /* Feature (Variable), */
588 0x85, 0xAE, /* Report ID (174), */
589 0x09, 0x52, /* Usage (52h), */
590 0x95, 0x01, /* Report Count (1), */
591 0xB1, 0x02, /* Feature (Variable), */
592 0x85, 0xAF, /* Report ID (175), */
593 0x09, 0x53, /* Usage (53h), */
594 0x95, 0x02, /* Report Count (2), */
595 0xB1, 0x02, /* Feature (Variable), */
596 0x85, 0xB0, /* Report ID (176), */
597 0x09, 0x54, /* Usage (54h), */
598 0x95, 0x3F, /* Report Count (63), */
599 0xB1, 0x02, /* Feature (Variable), */
600 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500601};
602
Frank Praznikad142b92014-02-20 11:36:00 -0500603/*
604 * The default behavior of the Dualshock 4 is to send reports using report
Frank Praznik077147a2014-09-14 11:56:39 -0400605 * type 1 when running over Bluetooth. However, when feature report 2 is
606 * requested during the controller initialization it starts sending input
607 * reports in report 17. Since report 17 is undefined in the default HID
Frank Praznikd829674d2014-02-05 20:03:45 -0500608 * descriptor the button and axis definitions must be moved to report 17 or
Frank Praznik077147a2014-09-14 11:56:39 -0400609 * the HID layer won't process the received input.
Frank Praznikd829674d2014-02-05 20:03:45 -0500610 */
611static u8 dualshock4_bt_rdesc[] = {
612 0x05, 0x01, /* Usage Page (Desktop), */
613 0x09, 0x05, /* Usage (Gamepad), */
614 0xA1, 0x01, /* Collection (Application), */
615 0x85, 0x01, /* Report ID (1), */
616 0x75, 0x08, /* Report Size (8), */
617 0x95, 0x0A, /* Report Count (9), */
618 0x81, 0x02, /* Input (Variable), */
619 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
620 0x85, 0x02, /* Report ID (2), */
621 0x09, 0x24, /* Usage (24h), */
622 0x95, 0x24, /* Report Count (36), */
623 0xB1, 0x02, /* Feature (Variable), */
624 0x85, 0xA3, /* Report ID (163), */
625 0x09, 0x25, /* Usage (25h), */
626 0x95, 0x30, /* Report Count (48), */
627 0xB1, 0x02, /* Feature (Variable), */
628 0x85, 0x05, /* Report ID (5), */
629 0x09, 0x26, /* Usage (26h), */
630 0x95, 0x28, /* Report Count (40), */
631 0xB1, 0x02, /* Feature (Variable), */
632 0x85, 0x06, /* Report ID (6), */
633 0x09, 0x27, /* Usage (27h), */
634 0x95, 0x34, /* Report Count (52), */
635 0xB1, 0x02, /* Feature (Variable), */
636 0x85, 0x07, /* Report ID (7), */
637 0x09, 0x28, /* Usage (28h), */
638 0x95, 0x30, /* Report Count (48), */
639 0xB1, 0x02, /* Feature (Variable), */
640 0x85, 0x08, /* Report ID (8), */
641 0x09, 0x29, /* Usage (29h), */
642 0x95, 0x2F, /* Report Count (47), */
643 0xB1, 0x02, /* Feature (Variable), */
644 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
645 0x85, 0x03, /* Report ID (3), */
646 0x09, 0x21, /* Usage (21h), */
647 0x95, 0x26, /* Report Count (38), */
648 0xB1, 0x02, /* Feature (Variable), */
649 0x85, 0x04, /* Report ID (4), */
650 0x09, 0x22, /* Usage (22h), */
651 0x95, 0x2E, /* Report Count (46), */
652 0xB1, 0x02, /* Feature (Variable), */
653 0x85, 0xF0, /* Report ID (240), */
654 0x09, 0x47, /* Usage (47h), */
655 0x95, 0x3F, /* Report Count (63), */
656 0xB1, 0x02, /* Feature (Variable), */
657 0x85, 0xF1, /* Report ID (241), */
658 0x09, 0x48, /* Usage (48h), */
659 0x95, 0x3F, /* Report Count (63), */
660 0xB1, 0x02, /* Feature (Variable), */
661 0x85, 0xF2, /* Report ID (242), */
662 0x09, 0x49, /* Usage (49h), */
663 0x95, 0x0F, /* Report Count (15), */
664 0xB1, 0x02, /* Feature (Variable), */
665 0x85, 0x11, /* Report ID (17), */
666 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
667 0x09, 0x20, /* Usage (20h), */
668 0x95, 0x02, /* Report Count (2), */
669 0x81, 0x02, /* Input (Variable), */
670 0x05, 0x01, /* Usage Page (Desktop), */
671 0x09, 0x30, /* Usage (X), */
672 0x09, 0x31, /* Usage (Y), */
673 0x09, 0x32, /* Usage (Z), */
674 0x09, 0x35, /* Usage (Rz), */
675 0x15, 0x00, /* Logical Minimum (0), */
676 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
677 0x75, 0x08, /* Report Size (8), */
678 0x95, 0x04, /* Report Count (4), */
679 0x81, 0x02, /* Input (Variable), */
680 0x09, 0x39, /* Usage (Hat Switch), */
681 0x15, 0x00, /* Logical Minimum (0), */
682 0x25, 0x07, /* Logical Maximum (7), */
683 0x75, 0x04, /* Report Size (4), */
684 0x95, 0x01, /* Report Count (1), */
685 0x81, 0x42, /* Input (Variable, Null State), */
686 0x05, 0x09, /* Usage Page (Button), */
687 0x19, 0x01, /* Usage Minimum (01h), */
688 0x29, 0x0E, /* Usage Maximum (0Eh), */
689 0x15, 0x00, /* Logical Minimum (0), */
690 0x25, 0x01, /* Logical Maximum (1), */
691 0x75, 0x01, /* Report Size (1), */
692 0x95, 0x0E, /* Report Count (14), */
693 0x81, 0x02, /* Input (Variable), */
694 0x75, 0x06, /* Report Size (6), */
695 0x95, 0x01, /* Report Count (1), */
696 0x81, 0x01, /* Input (Constant), */
697 0x05, 0x01, /* Usage Page (Desktop), */
698 0x09, 0x33, /* Usage (Rx), */
699 0x09, 0x34, /* Usage (Ry), */
700 0x15, 0x00, /* Logical Minimum (0), */
701 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
702 0x75, 0x08, /* Report Size (8), */
703 0x95, 0x02, /* Report Count (2), */
704 0x81, 0x02, /* Input (Variable), */
705 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
706 0x09, 0x20, /* Usage (20h), */
707 0x95, 0x03, /* Report Count (3), */
708 0x81, 0x02, /* Input (Variable), */
709 0x05, 0x01, /* Usage Page (Desktop), */
710 0x19, 0x40, /* Usage Minimum (40h), */
711 0x29, 0x42, /* Usage Maximum (42h), */
712 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
713 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
714 0x75, 0x10, /* Report Size (16), */
715 0x95, 0x03, /* Report Count (3), */
716 0x81, 0x02, /* Input (Variable), */
717 0x19, 0x43, /* Usage Minimum (43h), */
718 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400719 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
720 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznikd829674d2014-02-05 20:03:45 -0500721 0x95, 0x03, /* Report Count (3), */
722 0x81, 0x02, /* Input (Variable), */
723 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
724 0x09, 0x20, /* Usage (20h), */
725 0x15, 0x00, /* Logical Minimum (0), */
726 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
727 0x75, 0x08, /* Report Size (8), */
728 0x95, 0x31, /* Report Count (51), */
729 0x81, 0x02, /* Input (Variable), */
730 0x09, 0x21, /* Usage (21h), */
731 0x75, 0x08, /* Report Size (8), */
732 0x95, 0x4D, /* Report Count (77), */
733 0x91, 0x02, /* Output (Variable), */
734 0x85, 0x12, /* Report ID (18), */
735 0x09, 0x22, /* Usage (22h), */
736 0x95, 0x8D, /* Report Count (141), */
737 0x81, 0x02, /* Input (Variable), */
738 0x09, 0x23, /* Usage (23h), */
739 0x91, 0x02, /* Output (Variable), */
740 0x85, 0x13, /* Report ID (19), */
741 0x09, 0x24, /* Usage (24h), */
742 0x95, 0xCD, /* Report Count (205), */
743 0x81, 0x02, /* Input (Variable), */
744 0x09, 0x25, /* Usage (25h), */
745 0x91, 0x02, /* Output (Variable), */
746 0x85, 0x14, /* Report ID (20), */
747 0x09, 0x26, /* Usage (26h), */
748 0x96, 0x0D, 0x01, /* Report Count (269), */
749 0x81, 0x02, /* Input (Variable), */
750 0x09, 0x27, /* Usage (27h), */
751 0x91, 0x02, /* Output (Variable), */
752 0x85, 0x15, /* Report ID (21), */
753 0x09, 0x28, /* Usage (28h), */
754 0x96, 0x4D, 0x01, /* Report Count (333), */
755 0x81, 0x02, /* Input (Variable), */
756 0x09, 0x29, /* Usage (29h), */
757 0x91, 0x02, /* Output (Variable), */
758 0x85, 0x16, /* Report ID (22), */
759 0x09, 0x2A, /* Usage (2Ah), */
760 0x96, 0x8D, 0x01, /* Report Count (397), */
761 0x81, 0x02, /* Input (Variable), */
762 0x09, 0x2B, /* Usage (2Bh), */
763 0x91, 0x02, /* Output (Variable), */
764 0x85, 0x17, /* Report ID (23), */
765 0x09, 0x2C, /* Usage (2Ch), */
766 0x96, 0xCD, 0x01, /* Report Count (461), */
767 0x81, 0x02, /* Input (Variable), */
768 0x09, 0x2D, /* Usage (2Dh), */
769 0x91, 0x02, /* Output (Variable), */
770 0x85, 0x18, /* Report ID (24), */
771 0x09, 0x2E, /* Usage (2Eh), */
772 0x96, 0x0D, 0x02, /* Report Count (525), */
773 0x81, 0x02, /* Input (Variable), */
774 0x09, 0x2F, /* Usage (2Fh), */
775 0x91, 0x02, /* Output (Variable), */
776 0x85, 0x19, /* Report ID (25), */
777 0x09, 0x30, /* Usage (30h), */
778 0x96, 0x22, 0x02, /* Report Count (546), */
779 0x81, 0x02, /* Input (Variable), */
780 0x09, 0x31, /* Usage (31h), */
781 0x91, 0x02, /* Output (Variable), */
782 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
783 0x85, 0x82, /* Report ID (130), */
784 0x09, 0x22, /* Usage (22h), */
785 0x95, 0x3F, /* Report Count (63), */
786 0xB1, 0x02, /* Feature (Variable), */
787 0x85, 0x83, /* Report ID (131), */
788 0x09, 0x23, /* Usage (23h), */
789 0xB1, 0x02, /* Feature (Variable), */
790 0x85, 0x84, /* Report ID (132), */
791 0x09, 0x24, /* Usage (24h), */
792 0xB1, 0x02, /* Feature (Variable), */
793 0x85, 0x90, /* Report ID (144), */
794 0x09, 0x30, /* Usage (30h), */
795 0xB1, 0x02, /* Feature (Variable), */
796 0x85, 0x91, /* Report ID (145), */
797 0x09, 0x31, /* Usage (31h), */
798 0xB1, 0x02, /* Feature (Variable), */
799 0x85, 0x92, /* Report ID (146), */
800 0x09, 0x32, /* Usage (32h), */
801 0xB1, 0x02, /* Feature (Variable), */
802 0x85, 0x93, /* Report ID (147), */
803 0x09, 0x33, /* Usage (33h), */
804 0xB1, 0x02, /* Feature (Variable), */
805 0x85, 0xA0, /* Report ID (160), */
806 0x09, 0x40, /* Usage (40h), */
807 0xB1, 0x02, /* Feature (Variable), */
808 0x85, 0xA4, /* Report ID (164), */
809 0x09, 0x44, /* Usage (44h), */
810 0xB1, 0x02, /* Feature (Variable), */
811 0xC0 /* End Collection */
812};
813
Pavel Machek1adf9042016-02-09 13:55:08 +0100814static u8 ps3remote_rdesc[] = {
Jiri Kosina078328d2013-06-13 12:03:49 +0200815 0x05, 0x01, /* GUsagePage Generic Desktop */
816 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
817 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
818
819 /* Use collection 1 for joypad buttons */
820 0xA1, 0x02, /* MCollection Logical (interrelated data) */
821
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100822 /*
823 * Ignore the 1st byte, maybe it is used for a controller
824 * number but it's not needed for correct operation
825 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200826 0x75, 0x08, /* GReportSize 0x08 [8] */
827 0x95, 0x01, /* GReportCount 0x01 [1] */
828 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
829
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100830 /*
831 * Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
832 * buttons multiple keypresses are allowed
833 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200834 0x05, 0x09, /* GUsagePage Button */
835 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
836 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
837 0x14, /* GLogicalMinimum [0] */
838 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
839 0x75, 0x01, /* GReportSize 0x01 [1] */
840 0x95, 0x18, /* GReportCount 0x18 [24] */
841 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
842
843 0xC0, /* MEndCollection */
844
845 /* Use collection 2 for remote control buttons */
846 0xA1, 0x02, /* MCollection Logical (interrelated data) */
847
848 /* 5th byte is used for remote control buttons */
849 0x05, 0x09, /* GUsagePage Button */
850 0x18, /* LUsageMinimum [No button pressed] */
851 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
852 0x14, /* GLogicalMinimum [0] */
853 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
854 0x75, 0x08, /* GReportSize 0x08 [8] */
855 0x95, 0x01, /* GReportCount 0x01 [1] */
856 0x80, /* MInput */
857
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100858 /*
859 * Ignore bytes from 6th to 11th, 6th to 10th are always constant at
860 * 0xff and 11th is for press indication
861 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200862 0x75, 0x08, /* GReportSize 0x08 [8] */
863 0x95, 0x06, /* GReportCount 0x06 [6] */
864 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
865
866 /* 12th byte is for battery strength */
867 0x05, 0x06, /* GUsagePage Generic Device Controls */
868 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
869 0x14, /* GLogicalMinimum [0] */
870 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
871 0x75, 0x08, /* GReportSize 0x08 [8] */
872 0x95, 0x01, /* GReportCount 0x01 [1] */
873 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
874
875 0xC0, /* MEndCollection */
876
877 0xC0 /* MEndCollection [Game Pad] */
878};
879
880static const unsigned int ps3remote_keymap_joypad_buttons[] = {
881 [0x01] = KEY_SELECT,
882 [0x02] = BTN_THUMBL, /* L3 */
883 [0x03] = BTN_THUMBR, /* R3 */
884 [0x04] = BTN_START,
885 [0x05] = KEY_UP,
886 [0x06] = KEY_RIGHT,
887 [0x07] = KEY_DOWN,
888 [0x08] = KEY_LEFT,
889 [0x09] = BTN_TL2, /* L2 */
890 [0x0a] = BTN_TR2, /* R2 */
891 [0x0b] = BTN_TL, /* L1 */
892 [0x0c] = BTN_TR, /* R1 */
893 [0x0d] = KEY_OPTION, /* options/triangle */
894 [0x0e] = KEY_BACK, /* back/circle */
895 [0x0f] = BTN_0, /* cross */
896 [0x10] = KEY_SCREEN, /* view/square */
897 [0x11] = KEY_HOMEPAGE, /* PS button */
898 [0x14] = KEY_ENTER,
899};
900static const unsigned int ps3remote_keymap_remote_buttons[] = {
901 [0x00] = KEY_1,
902 [0x01] = KEY_2,
903 [0x02] = KEY_3,
904 [0x03] = KEY_4,
905 [0x04] = KEY_5,
906 [0x05] = KEY_6,
907 [0x06] = KEY_7,
908 [0x07] = KEY_8,
909 [0x08] = KEY_9,
910 [0x09] = KEY_0,
911 [0x0e] = KEY_ESC, /* return */
912 [0x0f] = KEY_CLEAR,
913 [0x16] = KEY_EJECTCD,
914 [0x1a] = KEY_MENU, /* top menu */
915 [0x28] = KEY_TIME,
916 [0x30] = KEY_PREVIOUS,
917 [0x31] = KEY_NEXT,
918 [0x32] = KEY_PLAY,
919 [0x33] = KEY_REWIND, /* scan back */
920 [0x34] = KEY_FORWARD, /* scan forward */
921 [0x38] = KEY_STOP,
922 [0x39] = KEY_PAUSE,
923 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
924 [0x60] = KEY_FRAMEBACK, /* slow/step back */
925 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
926 [0x63] = KEY_SUBTITLE,
927 [0x64] = KEY_AUDIO,
928 [0x65] = KEY_ANGLE,
929 [0x70] = KEY_INFO, /* display */
930 [0x80] = KEY_BLUE,
931 [0x81] = KEY_RED,
932 [0x82] = KEY_GREEN,
933 [0x83] = KEY_YELLOW,
934};
935
Colin Leitnerf04d5142013-05-27 23:41:05 +0200936static const unsigned int buzz_keymap[] = {
Frank Praznikad142b92014-02-20 11:36:00 -0500937 /*
938 * The controller has 4 remote buzzers, each with one LED and 5
Colin Leitnerf04d5142013-05-27 23:41:05 +0200939 * buttons.
Antonio Ospite09593e32016-02-09 13:55:06 +0100940 *
Colin Leitnerf04d5142013-05-27 23:41:05 +0200941 * We use the mapping chosen by the controller, which is:
942 *
943 * Key Offset
944 * -------------------
945 * Buzz 1
946 * Blue 5
947 * Orange 4
948 * Green 3
949 * Yellow 2
950 *
951 * So, for example, the orange button on the third buzzer is mapped to
952 * BTN_TRIGGER_HAPPY14
953 */
Antonio Ospite09593e32016-02-09 13:55:06 +0100954 [1] = BTN_TRIGGER_HAPPY1,
955 [2] = BTN_TRIGGER_HAPPY2,
956 [3] = BTN_TRIGGER_HAPPY3,
957 [4] = BTN_TRIGGER_HAPPY4,
958 [5] = BTN_TRIGGER_HAPPY5,
959 [6] = BTN_TRIGGER_HAPPY6,
960 [7] = BTN_TRIGGER_HAPPY7,
961 [8] = BTN_TRIGGER_HAPPY8,
962 [9] = BTN_TRIGGER_HAPPY9,
Colin Leitnerf04d5142013-05-27 23:41:05 +0200963 [10] = BTN_TRIGGER_HAPPY10,
964 [11] = BTN_TRIGGER_HAPPY11,
965 [12] = BTN_TRIGGER_HAPPY12,
966 [13] = BTN_TRIGGER_HAPPY13,
967 [14] = BTN_TRIGGER_HAPPY14,
968 [15] = BTN_TRIGGER_HAPPY15,
969 [16] = BTN_TRIGGER_HAPPY16,
970 [17] = BTN_TRIGGER_HAPPY17,
971 [18] = BTN_TRIGGER_HAPPY18,
972 [19] = BTN_TRIGGER_HAPPY19,
973 [20] = BTN_TRIGGER_HAPPY20,
974};
975
Frank Praznikd902f472014-01-27 10:17:36 -0500976static enum power_supply_property sony_battery_props[] = {
977 POWER_SUPPLY_PROP_PRESENT,
978 POWER_SUPPLY_PROP_CAPACITY,
979 POWER_SUPPLY_PROP_SCOPE,
980 POWER_SUPPLY_PROP_STATUS,
981};
982
Frank Praznik55d3b662014-04-14 10:11:32 -0400983struct sixaxis_led {
Pavel Machek1adf9042016-02-09 13:55:08 +0100984 u8 time_enabled; /* the total time the led is active (0xff means forever) */
985 u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
986 u8 enabled;
987 u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
988 u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
Frank Praznik55d3b662014-04-14 10:11:32 -0400989} __packed;
990
991struct sixaxis_rumble {
Pavel Machek1adf9042016-02-09 13:55:08 +0100992 u8 padding;
993 u8 right_duration; /* Right motor duration (0xff means forever) */
994 u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
995 u8 left_duration; /* Left motor duration (0xff means forever) */
996 u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
Frank Praznik55d3b662014-04-14 10:11:32 -0400997} __packed;
998
999struct sixaxis_output_report {
Pavel Machek1adf9042016-02-09 13:55:08 +01001000 u8 report_id;
Frank Praznik55d3b662014-04-14 10:11:32 -04001001 struct sixaxis_rumble rumble;
Pavel Machek1adf9042016-02-09 13:55:08 +01001002 u8 padding[4];
1003 u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
Frank Praznik55d3b662014-04-14 10:11:32 -04001004 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
1005 struct sixaxis_led _reserved; /* LED5, not actually soldered */
1006} __packed;
1007
1008union sixaxis_output_report_01 {
1009 struct sixaxis_output_report data;
Pavel Machek1adf9042016-02-09 13:55:08 +01001010 u8 buf[36];
Frank Praznik55d3b662014-04-14 10:11:32 -04001011};
1012
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001013struct motion_output_report_02 {
1014 u8 type, zero;
1015 u8 r, g, b;
1016 u8 zero2;
1017 u8 rumble;
1018};
1019
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001020#define DS4_REPORT_0x02_SIZE 37
1021#define DS4_REPORT_0x05_SIZE 32
1022#define DS4_REPORT_0x11_SIZE 78
1023#define DS4_REPORT_0x81_SIZE 7
Antonio Ospite29b691a2015-02-16 18:12:21 +01001024#define SIXAXIS_REPORT_0xF2_SIZE 17
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001025#define SIXAXIS_REPORT_0xF5_SIZE 8
Simon Wood41d2d422015-06-09 21:27:06 -06001026#define MOTION_REPORT_0x02_SIZE 49
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001027
Jiri Kosina8b402c92015-02-23 11:15:44 +01001028static DEFINE_SPINLOCK(sony_dev_list_lock);
Frank Praznikd2d782f2014-02-20 11:36:03 -05001029static LIST_HEAD(sony_device_list);
Frank Praznik80250872014-04-14 10:11:35 -04001030static DEFINE_IDA(sony_device_id_allocator);
Frank Praznikd2d782f2014-02-20 11:36:03 -05001031
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001032struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -05001033 spinlock_t lock;
Frank Praznikd2d782f2014-02-20 11:36:03 -05001034 struct list_head list_node;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001035 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -05001036 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001037 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001038 struct work_struct state_worker;
Antonio Ospite09593e32016-02-09 13:55:06 +01001039 void (*send_output_report)(struct sony_sc *);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001040 struct power_supply *battery;
1041 struct power_supply_desc battery_desc;
Frank Praznik80250872014-04-14 10:11:35 -04001042 int device_id;
Pavel Machek1adf9042016-02-09 13:55:08 +01001043 u8 *output_report_dmabuf;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001044
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001045#ifdef CONFIG_SONY_FF
Pavel Machek1adf9042016-02-09 13:55:08 +01001046 u8 left;
1047 u8 right;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001048#endif
1049
Pavel Machek1adf9042016-02-09 13:55:08 +01001050 u8 mac_address[6];
1051 u8 worker_initialized;
1052 u8 cable_state;
1053 u8 battery_charging;
1054 u8 battery_capacity;
1055 u8 led_state[MAX_LEDS];
1056 u8 resume_led_state[MAX_LEDS];
1057 u8 led_delay_on[MAX_LEDS];
1058 u8 led_delay_off[MAX_LEDS];
1059 u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001060};
1061
Pavel Machek1adf9042016-02-09 13:55:08 +01001062static u8 *sixaxis_fixup(struct hid_device *hdev, u8 *rdesc,
Antonio Ospitec607fb82014-06-24 13:28:41 +02001063 unsigned int *rsize)
1064{
1065 *rsize = sizeof(sixaxis_rdesc);
1066 return sixaxis_rdesc;
1067}
1068
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001069static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
1070 unsigned int *rsize)
1071{
1072 *rsize = sizeof(motion_rdesc);
1073 return motion_rdesc;
1074}
1075
Simon Woodb2723eb2015-06-17 00:08:53 -06001076static u8 *navigation_fixup(struct hid_device *hdev, u8 *rdesc,
1077 unsigned int *rsize)
1078{
1079 *rsize = sizeof(navigation_rdesc);
1080 return navigation_rdesc;
1081}
1082
Pavel Machek1adf9042016-02-09 13:55:08 +01001083static u8 *ps3remote_fixup(struct hid_device *hdev, u8 *rdesc,
Jiri Kosina078328d2013-06-13 12:03:49 +02001084 unsigned int *rsize)
1085{
1086 *rsize = sizeof(ps3remote_rdesc);
1087 return ps3remote_rdesc;
1088}
1089
1090static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
1091 struct hid_field *field, struct hid_usage *usage,
1092 unsigned long **bit, int *max)
1093{
1094 unsigned int key = usage->hid & HID_USAGE;
1095
1096 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1097 return -1;
1098
1099 switch (usage->collection_index) {
1100 case 1:
1101 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
1102 return -1;
1103
1104 key = ps3remote_keymap_joypad_buttons[key];
1105 if (!key)
1106 return -1;
1107 break;
1108 case 2:
1109 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
1110 return -1;
1111
1112 key = ps3remote_keymap_remote_buttons[key];
1113 if (!key)
1114 return -1;
1115 break;
1116 default:
1117 return -1;
1118 }
1119
1120 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1121 return 1;
1122}
1123
Pavel Machek1adf9042016-02-09 13:55:08 +01001124static u8 *sony_report_fixup(struct hid_device *hdev, u8 *rdesc,
Nikolai Kondrashov73e40082010-08-06 23:03:06 +04001125 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001126{
1127 struct sony_sc *sc = hid_get_drvdata(hdev);
1128
Mikko Perttunen4ba1eee2016-07-21 19:54:48 +03001129 if (sc->quirks & (SINO_LITE_CONTROLLER | FUTUREMAX_DANCE_MAT))
Scott Moreau74500cc2016-01-13 07:40:42 -07001130 return rdesc;
1131
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +09001132 /*
1133 * Some Sony RF receivers wrongly declare the mouse pointer as a
1134 * a constant non-data variable.
1135 */
1136 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
1137 /* usage page: generic desktop controls */
1138 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
1139 /* usage: mouse */
1140 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
1141 /* input (usage page for x,y axes): constant, variable, relative */
1142 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +09001143 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +09001144 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001145 rdesc[55] = 0x06;
1146 }
Simon Wood61ab44b2011-06-10 12:00:26 +02001147
Frank Prazniked19d8c2014-01-16 21:43:12 -05001148 /*
1149 * The default Dualshock 4 USB descriptor doesn't assign
1150 * the gyroscope values to corresponding axes so we need a
1151 * modified one.
1152 */
Frank Praznikb71b5572015-11-06 15:35:53 -05001153 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Prazniked19d8c2014-01-16 21:43:12 -05001154 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
1155 rdesc = dualshock4_usb_rdesc;
1156 *rsize = sizeof(dualshock4_usb_rdesc);
Frank Praznikb71b5572015-11-06 15:35:53 -05001157 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
Frank Praznikd829674d2014-02-05 20:03:45 -05001158 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
1159 rdesc = dualshock4_bt_rdesc;
1160 *rsize = sizeof(dualshock4_bt_rdesc);
Frank Prazniked19d8c2014-01-16 21:43:12 -05001161 }
1162
Antonio Ospitec607fb82014-06-24 13:28:41 +02001163 if (sc->quirks & SIXAXIS_CONTROLLER)
1164 return sixaxis_fixup(hdev, rdesc, rsize);
Jiri Kosina078328d2013-06-13 12:03:49 +02001165
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001166 if (sc->quirks & MOTION_CONTROLLER)
1167 return motion_fixup(hdev, rdesc, rsize);
1168
Simon Wood4545ee02015-06-17 00:08:52 -06001169 if (sc->quirks & NAVIGATION_CONTROLLER)
Simon Woodb2723eb2015-06-17 00:08:53 -06001170 return navigation_fixup(hdev, rdesc, rsize);
Simon Wood4545ee02015-06-17 00:08:52 -06001171
Jiri Kosina078328d2013-06-13 12:03:49 +02001172 if (sc->quirks & PS3REMOTE)
1173 return ps3remote_fixup(hdev, rdesc, rsize);
1174
Nikolai Kondrashov73e40082010-08-06 23:03:06 +04001175 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001176}
1177
Pavel Machek1adf9042016-02-09 13:55:08 +01001178static void sixaxis_parse_report(struct sony_sc *sc, u8 *rd, int size)
Frank Praznikd902f472014-01-27 10:17:36 -05001179{
Pavel Machek1adf9042016-02-09 13:55:08 +01001180 static const u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
Frank Praznikd902f472014-01-27 10:17:36 -05001181 unsigned long flags;
Simon Wood12e9a6d2015-06-09 21:27:05 -06001182 int offset;
Pavel Machek1adf9042016-02-09 13:55:08 +01001183 u8 cable_state, battery_capacity, battery_charging;
Frank Praznikd902f472014-01-27 10:17:36 -05001184
Frank Praznikad142b92014-02-20 11:36:00 -05001185 /*
1186 * The sixaxis is charging if the battery value is 0xee
Frank Praznikd902f472014-01-27 10:17:36 -05001187 * and it is fully charged if the value is 0xef.
1188 * It does not report the actual level while charging so it
1189 * is set to 100% while charging is in progress.
1190 */
Simon Wood12e9a6d2015-06-09 21:27:05 -06001191 offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
1192
1193 if (rd[offset] >= 0xee) {
Frank Praznikd902f472014-01-27 10:17:36 -05001194 battery_capacity = 100;
Simon Wood12e9a6d2015-06-09 21:27:05 -06001195 battery_charging = !(rd[offset] & 0x01);
Frank Praznik9fddd742014-08-29 13:11:52 -04001196 cable_state = 1;
Frank Praznikd902f472014-01-27 10:17:36 -05001197 } else {
Pavel Machek1adf9042016-02-09 13:55:08 +01001198 u8 index = rd[offset] <= 5 ? rd[offset] : 5;
Frank Praznikac3c9a92014-02-20 11:36:02 -05001199 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -05001200 battery_charging = 0;
Frank Praznik9fddd742014-08-29 13:11:52 -04001201 cable_state = 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001202 }
Frank Praznikd902f472014-01-27 10:17:36 -05001203
1204 spin_lock_irqsave(&sc->lock, flags);
1205 sc->cable_state = cable_state;
1206 sc->battery_capacity = battery_capacity;
1207 sc->battery_charging = battery_charging;
1208 spin_unlock_irqrestore(&sc->lock, flags);
1209}
1210
Pavel Machek1adf9042016-02-09 13:55:08 +01001211static void dualshock4_parse_report(struct sony_sc *sc, u8 *rd, int size)
Frank Praznikd902f472014-01-27 10:17:36 -05001212{
Frank Praznike5606232014-01-27 10:17:37 -05001213 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1214 struct hid_input, list);
1215 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -05001216 unsigned long flags;
Frank Praznik6c5f8602014-02-05 20:03:47 -05001217 int n, offset;
Pavel Machek1adf9042016-02-09 13:55:08 +01001218 u8 cable_state, battery_capacity, battery_charging;
Frank Praznikd902f472014-01-27 10:17:36 -05001219
Frank Praznikad142b92014-02-20 11:36:00 -05001220 /*
1221 * Battery and touchpad data starts at byte 30 in the USB report and
Frank Praznik6c5f8602014-02-05 20:03:47 -05001222 * 32 in Bluetooth report.
1223 */
1224 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
1225
Frank Praznikad142b92014-02-20 11:36:00 -05001226 /*
1227 * The lower 4 bits of byte 30 contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -05001228 * and the 5th bit contains the USB cable state.
1229 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001230 cable_state = (rd[offset] >> 4) & 0x01;
1231 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -05001232
Frank Praznikad142b92014-02-20 11:36:00 -05001233 /*
1234 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -05001235 * 0 to 10, and when running on battery power it ranges from 0 to 9.
1236 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -05001237 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001238 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -05001239 battery_charging = 0;
1240 else
1241 battery_charging = 1;
1242
Frank Praznik6c5f8602014-02-05 20:03:47 -05001243 if (!cable_state)
1244 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -05001245 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -05001246 battery_capacity = 10;
1247
Frank Praznikd902f472014-01-27 10:17:36 -05001248 battery_capacity *= 10;
1249
1250 spin_lock_irqsave(&sc->lock, flags);
1251 sc->cable_state = cable_state;
1252 sc->battery_capacity = battery_capacity;
1253 sc->battery_charging = battery_charging;
1254 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -05001255
Frank Praznik6c5f8602014-02-05 20:03:47 -05001256 offset += 5;
1257
Frank Praznikad142b92014-02-20 11:36:00 -05001258 /*
1259 * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
Frank Praznik6c5f8602014-02-05 20:03:47 -05001260 * and 37 on Bluetooth.
Frank Praznike5606232014-01-27 10:17:37 -05001261 * The first 7 bits of the first byte is a counter and bit 8 is a touch
1262 * indicator that is 0 when pressed and 1 when not pressed.
1263 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1264 * The data for the second touch is in the same format and immediatly
1265 * follows the data for the first.
1266 */
1267 for (n = 0; n < 2; n++) {
Pavel Machek1adf9042016-02-09 13:55:08 +01001268 u16 x, y;
Frank Praznike5606232014-01-27 10:17:37 -05001269
1270 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1271 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1272
1273 input_mt_slot(input_dev, n);
1274 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
1275 !(rd[offset] >> 7));
1276 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1277 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1278
1279 offset += 4;
1280 }
Frank Praznikd902f472014-01-27 10:17:36 -05001281}
1282
Simon Woodc9e4d872011-06-10 12:00:27 +02001283static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
Pavel Machek1adf9042016-02-09 13:55:08 +01001284 u8 *rd, int size)
Simon Woodc9e4d872011-06-10 12:00:27 +02001285{
1286 struct sony_sc *sc = hid_get_drvdata(hdev);
1287
Frank Praznikad142b92014-02-20 11:36:00 -05001288 /*
1289 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +02001290 * has to be BYTE_SWAPPED before passing up to joystick interface
1291 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001292 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Frank Praznik8f5f0bc2015-07-23 19:01:16 -04001293 /*
1294 * When connected via Bluetooth the Sixaxis occasionally sends
1295 * a report with the second byte 0xff and the rest zeroed.
1296 *
1297 * This report does not reflect the actual state of the
1298 * controller must be ignored to avoid generating false input
1299 * events.
1300 */
1301 if (rd[1] == 0xff)
1302 return -EINVAL;
1303
Simon Woodc9e4d872011-06-10 12:00:27 +02001304 swap(rd[41], rd[42]);
1305 swap(rd[43], rd[44]);
1306 swap(rd[45], rd[46]);
1307 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -05001308
1309 sixaxis_parse_report(sc, rd, size);
Simon Wood12e9a6d2015-06-09 21:27:05 -06001310 } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
1311 sixaxis_parse_report(sc, rd, size);
Simon Wood4545ee02015-06-17 00:08:52 -06001312 } else if ((sc->quirks & NAVIGATION_CONTROLLER) && rd[0] == 0x01 &&
1313 size == 49) {
1314 sixaxis_parse_report(sc, rd, size);
Frank Praznik68330d82014-02-05 20:03:49 -05001315 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1316 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1317 && rd[0] == 0x11 && size == 78)) {
Frank Praznikd902f472014-01-27 10:17:36 -05001318 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +02001319 }
1320
1321 return 0;
1322}
1323
Colin Leitnerf04d5142013-05-27 23:41:05 +02001324static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1325 struct hid_field *field, struct hid_usage *usage,
1326 unsigned long **bit, int *max)
1327{
1328 struct sony_sc *sc = hid_get_drvdata(hdev);
1329
1330 if (sc->quirks & BUZZ_CONTROLLER) {
1331 unsigned int key = usage->hid & HID_USAGE;
1332
1333 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1334 return -1;
1335
1336 switch (usage->collection_index) {
1337 case 1:
1338 if (key >= ARRAY_SIZE(buzz_keymap))
1339 return -1;
1340
1341 key = buzz_keymap[key];
1342 if (!key)
1343 return -1;
1344 break;
1345 default:
1346 return -1;
1347 }
1348
1349 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1350 return 1;
1351 }
1352
Jiri Kosina078328d2013-06-13 12:03:49 +02001353 if (sc->quirks & PS3REMOTE)
1354 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1355
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001356 /* Let hid-core decide for the others */
1357 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001358}
1359
Frank Praznikce8efc32014-09-18 21:15:01 -04001360static int sony_register_touchpad(struct hid_input *hi, int touch_count,
1361 int w, int h)
1362{
1363 struct input_dev *input_dev = hi->input;
1364 int ret;
1365
1366 ret = input_mt_init_slots(input_dev, touch_count, 0);
1367 if (ret < 0)
1368 return ret;
1369
1370 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1371 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1372
1373 return 0;
1374}
1375
Dmitry Torokhov91543012015-09-29 15:52:59 -07001376static int sony_input_configured(struct hid_device *hdev,
Frank Praznikce8efc32014-09-18 21:15:01 -04001377 struct hid_input *hidinput)
1378{
1379 struct sony_sc *sc = hid_get_drvdata(hdev);
Dmitry Torokhov91543012015-09-29 15:52:59 -07001380 int ret;
Frank Praznikce8efc32014-09-18 21:15:01 -04001381
1382 /*
1383 * The Dualshock 4 touchpad supports 2 touches and has a
Frank Praznik981c5b42014-09-24 09:38:19 -04001384 * resolution of 1920x942 (44.86 dots/mm).
Frank Praznikce8efc32014-09-18 21:15:01 -04001385 */
1386 if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Dmitry Torokhov91543012015-09-29 15:52:59 -07001387 ret = sony_register_touchpad(hidinput, 2, 1920, 942);
1388 if (ret) {
Frank Praznikce8efc32014-09-18 21:15:01 -04001389 hid_err(sc->hdev,
Dmitry Torokhov91543012015-09-29 15:52:59 -07001390 "Unable to initialize multi-touch slots: %d\n",
1391 ret);
1392 return ret;
1393 }
Frank Praznikce8efc32014-09-18 21:15:01 -04001394 }
Dmitry Torokhov91543012015-09-29 15:52:59 -07001395
1396 return 0;
Frank Praznikce8efc32014-09-18 21:15:01 -04001397}
1398
Antonio Ospite5710fab2011-02-20 18:26:45 +01001399/*
Jiri Slabybd28ce02008-06-25 23:47:04 +02001400 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1401 * to "operational". Without this, the ps3 controller will not report any
1402 * events.
1403 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001404static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001405{
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001406 const int buf_size =
1407 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
Pavel Machek1adf9042016-02-09 13:55:08 +01001408 u8 *buf;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001409 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001410
Antonio Ospite2e701a32015-02-16 18:12:24 +01001411 buf = kmalloc(buf_size, GFP_KERNEL);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001412 if (!buf)
1413 return -ENOMEM;
1414
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001415 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1416 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001417 if (ret < 0) {
1418 hid_err(hdev, "can't set operational mode: step 1\n");
1419 goto out;
1420 }
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001421
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001422 /*
1423 * Some compatible controllers like the Speedlink Strike FX and
1424 * Gasia need another query plus an USB interrupt to get operational.
1425 */
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001426 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1427 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001428 if (ret < 0) {
1429 hid_err(hdev, "can't set operational mode: step 2\n");
1430 goto out;
1431 }
1432
1433 ret = hid_hw_output_report(hdev, buf, 1);
Benjamin Tissoires19f4c2b2016-01-08 17:58:49 +01001434 if (ret < 0) {
1435 hid_info(hdev, "can't set operational mode: step 3, ignoring\n");
1436 ret = 0;
1437 }
Jiri Slabybd28ce02008-06-25 23:47:04 +02001438
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001439out:
Jiri Slabybd28ce02008-06-25 23:47:04 +02001440 kfree(buf);
1441
1442 return ret;
1443}
1444
Antonio Ospite816651a2010-05-03 22:15:55 +02001445static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001446{
Pavel Machek1adf9042016-02-09 13:55:08 +01001447 static const u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1448 u8 *buf;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001449 int ret;
1450
1451 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1452 if (!buf)
1453 return -ENOMEM;
1454
1455 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
Benjamin Tissoiresb0dd72a2014-02-10 12:58:54 -05001456 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001457
1458 kfree(buf);
1459
1460 return ret;
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001461}
1462
Frank Praznikad142b92014-02-20 11:36:00 -05001463/*
1464 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
Frank Praznik68330d82014-02-05 20:03:49 -05001465 * controller so that it sends full input reports of type 0x11.
1466 */
1467static int dualshock4_set_operational_bt(struct hid_device *hdev)
1468{
Pavel Machek1adf9042016-02-09 13:55:08 +01001469 u8 *buf;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001470 int ret;
Frank Praznik68330d82014-02-05 20:03:49 -05001471
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001472 buf = kmalloc(DS4_REPORT_0x02_SIZE, GFP_KERNEL);
1473 if (!buf)
1474 return -ENOMEM;
1475
1476 ret = hid_hw_raw_request(hdev, 0x02, buf, DS4_REPORT_0x02_SIZE,
Frank Praznik68330d82014-02-05 20:03:49 -05001477 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001478
1479 kfree(buf);
1480
1481 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001482}
1483
Frank Praznik221399b2015-05-05 20:47:32 -04001484static void sixaxis_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001485{
Pavel Machek1adf9042016-02-09 13:55:08 +01001486 static const u8 sixaxis_leds[10][4] = {
Frank Praznik80250872014-04-14 10:11:35 -04001487 { 0x01, 0x00, 0x00, 0x00 },
1488 { 0x00, 0x01, 0x00, 0x00 },
1489 { 0x00, 0x00, 0x01, 0x00 },
1490 { 0x00, 0x00, 0x00, 0x01 },
1491 { 0x01, 0x00, 0x00, 0x01 },
1492 { 0x00, 0x01, 0x00, 0x01 },
1493 { 0x00, 0x00, 0x01, 0x01 },
1494 { 0x01, 0x00, 0x01, 0x01 },
1495 { 0x00, 0x01, 0x01, 0x01 },
1496 { 0x01, 0x01, 0x01, 0x01 }
1497 };
1498
Frank Praznik221399b2015-05-05 20:47:32 -04001499 int id = sc->device_id;
1500
1501 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001502
1503 if (id < 0)
1504 return;
1505
1506 id %= 10;
Frank Praznik221399b2015-05-05 20:47:32 -04001507 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001508}
1509
Frank Praznik221399b2015-05-05 20:47:32 -04001510static void dualshock4_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001511{
1512 /* The first 4 color/index entries match what the PS4 assigns */
Pavel Machek1adf9042016-02-09 13:55:08 +01001513 static const u8 color_code[7][3] = {
Frank Praznik80250872014-04-14 10:11:35 -04001514 /* Blue */ { 0x00, 0x00, 0x01 },
1515 /* Red */ { 0x01, 0x00, 0x00 },
1516 /* Green */ { 0x00, 0x01, 0x00 },
1517 /* Pink */ { 0x02, 0x00, 0x01 },
1518 /* Orange */ { 0x02, 0x01, 0x00 },
1519 /* Teal */ { 0x00, 0x01, 0x01 },
1520 /* White */ { 0x01, 0x01, 0x01 }
1521 };
1522
Frank Praznik221399b2015-05-05 20:47:32 -04001523 int id = sc->device_id;
1524
1525 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001526
1527 if (id < 0)
1528 return;
1529
1530 id %= 7;
Frank Praznik221399b2015-05-05 20:47:32 -04001531 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001532}
1533
Frank Praznik221399b2015-05-05 20:47:32 -04001534static void buzz_set_leds(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001535{
Frank Praznik221399b2015-05-05 20:47:32 -04001536 struct hid_device *hdev = sc->hdev;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001537 struct list_head *report_list =
1538 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1539 struct hid_report *report = list_entry(report_list->next,
1540 struct hid_report, list);
Pavel Machek1adf9042016-02-09 13:55:08 +01001541 s32 *value = report->field[0]->value;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001542
Frank Praznik221399b2015-05-05 20:47:32 -04001543 BUILD_BUG_ON(MAX_LEDS < 4);
1544
Colin Leitnerf04d5142013-05-27 23:41:05 +02001545 value[0] = 0x00;
Frank Praznik221399b2015-05-05 20:47:32 -04001546 value[1] = sc->led_state[0] ? 0xff : 0x00;
1547 value[2] = sc->led_state[1] ? 0xff : 0x00;
1548 value[3] = sc->led_state[2] ? 0xff : 0x00;
1549 value[4] = sc->led_state[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001550 value[5] = 0x00;
1551 value[6] = 0x00;
1552 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1553}
1554
Frank Praznik221399b2015-05-05 20:47:32 -04001555static void sony_set_leds(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001556{
Frank Praznik221399b2015-05-05 20:47:32 -04001557 if (!(sc->quirks & BUZZ_CONTROLLER))
Frank Praznikfa57a812014-04-14 10:11:33 -04001558 schedule_work(&sc->state_worker);
Frank Praznik221399b2015-05-05 20:47:32 -04001559 else
1560 buzz_set_leds(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001561}
1562
Sven Eckelmannc5382512013-11-19 20:26:30 +01001563static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001564 enum led_brightness value)
1565{
1566 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001567 struct hid_device *hdev = to_hid_device(dev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001568 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001569
1570 int n;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001571 int force_update;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001572
1573 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001574 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001575 hid_err(hdev, "No device data\n");
1576 return;
1577 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001578
Frank Praznikb3ed4582014-04-14 10:11:36 -04001579 /*
1580 * The Sixaxis on USB will override any LED settings sent to it
1581 * and keep flashing all of the LEDs until the PS button is pressed.
1582 * Updates, even if redundant, must be always be sent to the
1583 * controller to avoid having to toggle the state of an LED just to
1584 * stop the flashing later on.
1585 */
1586 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1587
Frank Praznik60781cf2014-01-11 15:13:15 -05001588 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznikb3ed4582014-04-14 10:11:36 -04001589 if (led == drv_data->leds[n] && (force_update ||
1590 (value != drv_data->led_state[n] ||
1591 drv_data->led_delay_on[n] ||
1592 drv_data->led_delay_off[n]))) {
1593
1594 drv_data->led_state[n] = value;
1595
1596 /* Setting the brightness stops the blinking */
1597 drv_data->led_delay_on[n] = 0;
1598 drv_data->led_delay_off[n] = 0;
1599
Frank Praznik221399b2015-05-05 20:47:32 -04001600 sony_set_leds(drv_data);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001601 break;
1602 }
1603 }
1604}
1605
Sven Eckelmannc5382512013-11-19 20:26:30 +01001606static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001607{
1608 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001609 struct hid_device *hdev = to_hid_device(dev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001610 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001611
1612 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001613
1614 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001615 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001616 hid_err(hdev, "No device data\n");
1617 return LED_OFF;
1618 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001619
Frank Praznik60781cf2014-01-11 15:13:15 -05001620 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001621 if (led == drv_data->leds[n])
1622 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001623 }
1624
Simon Wood7db75042014-02-05 12:34:18 -07001625 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001626}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001627
Frank Praznikb3ed4582014-04-14 10:11:36 -04001628static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1629 unsigned long *delay_off)
1630{
1631 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001632 struct hid_device *hdev = to_hid_device(dev);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001633 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1634 int n;
Pavel Machek1adf9042016-02-09 13:55:08 +01001635 u8 new_on, new_off;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001636
1637 if (!drv_data) {
1638 hid_err(hdev, "No device data\n");
1639 return -EINVAL;
1640 }
1641
1642 /* Max delay is 255 deciseconds or 2550 milliseconds */
1643 if (*delay_on > 2550)
1644 *delay_on = 2550;
1645 if (*delay_off > 2550)
1646 *delay_off = 2550;
1647
1648 /* Blink at 1 Hz if both values are zero */
1649 if (!*delay_on && !*delay_off)
1650 *delay_on = *delay_off = 500;
1651
1652 new_on = *delay_on / 10;
1653 new_off = *delay_off / 10;
1654
1655 for (n = 0; n < drv_data->led_count; n++) {
1656 if (led == drv_data->leds[n])
1657 break;
1658 }
1659
1660 /* This LED is not registered on this device */
1661 if (n >= drv_data->led_count)
1662 return -EINVAL;
1663
1664 /* Don't schedule work if the values didn't change */
1665 if (new_on != drv_data->led_delay_on[n] ||
1666 new_off != drv_data->led_delay_off[n]) {
1667 drv_data->led_delay_on[n] = new_on;
1668 drv_data->led_delay_off[n] = new_off;
1669 schedule_work(&drv_data->state_worker);
1670 }
1671
1672 return 0;
1673}
1674
Frank Praznikfa57a812014-04-14 10:11:33 -04001675static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001676{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001677 struct led_classdev *led;
1678 int n;
1679
Frank Praznikfa57a812014-04-14 10:11:33 -04001680 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001681
Frank Praznikfa57a812014-04-14 10:11:33 -04001682 for (n = 0; n < sc->led_count; n++) {
1683 led = sc->leds[n];
1684 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001685 if (!led)
1686 continue;
1687 led_classdev_unregister(led);
1688 kfree(led);
1689 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001690
Frank Praznikfa57a812014-04-14 10:11:33 -04001691 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001692}
1693
Frank Praznikfa57a812014-04-14 10:11:33 -04001694static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001695{
Frank Praznikfa57a812014-04-14 10:11:33 -04001696 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001697 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001698 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001699 struct led_classdev *led;
1700 size_t name_sz;
1701 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001702 size_t name_len;
1703 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001704 static const char * const ds4_name_str[] = { "red", "green", "blue",
1705 "global" };
Pavel Machek1adf9042016-02-09 13:55:08 +01001706 u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
1707 u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001708
Frank Praznikfa57a812014-04-14 10:11:33 -04001709 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001710
Frank Praznikfa57a812014-04-14 10:11:33 -04001711 if (sc->quirks & BUZZ_CONTROLLER) {
1712 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001713 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001714 name_len = strlen("::buzz#");
1715 name_fmt = "%s::buzz%d";
1716 /* Validate expected report characteristics. */
1717 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1718 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001719 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik221399b2015-05-05 20:47:32 -04001720 dualshock4_set_leds_from_id(sc);
1721 sc->led_state[3] = 1;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001722 sc->led_count = 4;
1723 memset(max_brightness, 255, 3);
1724 use_hw_blink[3] = 1;
1725 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001726 name_len = 0;
1727 name_fmt = "%s:%s";
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001728 } else if (sc->quirks & MOTION_CONTROLLER) {
1729 sc->led_count = 3;
1730 memset(max_brightness, 255, 3);
1731 use_ds4_names = 1;
1732 name_len = 0;
1733 name_fmt = "%s:%s";
Simon Wood4545ee02015-06-17 00:08:52 -06001734 } else if (sc->quirks & NAVIGATION_CONTROLLER) {
Pavel Machek1adf9042016-02-09 13:55:08 +01001735 static const u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00};
Simon Wood4545ee02015-06-17 00:08:52 -06001736
1737 memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds));
1738 sc->led_count = 1;
1739 memset(use_hw_blink, 1, 4);
1740 use_ds4_names = 0;
1741 name_len = strlen("::sony#");
1742 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001743 } else {
Frank Praznik221399b2015-05-05 20:47:32 -04001744 sixaxis_set_leds_from_id(sc);
Frank Praznikfa57a812014-04-14 10:11:33 -04001745 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001746 memset(use_hw_blink, 1, 4);
1747 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001748 name_len = strlen("::sony#");
1749 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001750 }
1751
Frank Praznikad142b92014-02-20 11:36:00 -05001752 /*
1753 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001754 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001755 * LEDs to on
1756 */
Frank Praznik221399b2015-05-05 20:47:32 -04001757 sony_set_leds(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001758
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001759 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001760
Frank Praznikfa57a812014-04-14 10:11:33 -04001761 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001762
Frank Praznikb3ed4582014-04-14 10:11:36 -04001763 if (use_ds4_names)
1764 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001765
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001766 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1767 if (!led) {
1768 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001769 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001770 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001771 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001772
1773 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001774 if (use_ds4_names)
1775 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1776 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001777 else
1778 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001779 led->name = name;
Frank Praznik221399b2015-05-05 20:47:32 -04001780 led->brightness = sc->led_state[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001781 led->max_brightness = max_brightness[n];
Sven Eckelmannc5382512013-11-19 20:26:30 +01001782 led->brightness_get = sony_led_get_brightness;
1783 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001784
Frank Praznikb3ed4582014-04-14 10:11:36 -04001785 if (use_hw_blink[n])
1786 led->blink_set = sony_led_blink_set;
1787
Frank Praznik80250872014-04-14 10:11:35 -04001788 sc->leds[n] = led;
1789
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001790 ret = led_classdev_register(&hdev->dev, led);
1791 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001792 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001793 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001794 kfree(led);
1795 goto error_leds;
1796 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001797 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001798
1799 return ret;
1800
Colin Leitnerf04d5142013-05-27 23:41:05 +02001801error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001802 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001803
Colin Leitnerf04d5142013-05-27 23:41:05 +02001804 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001805}
1806
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001807static void sixaxis_send_output_report(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001808{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001809 static const union sixaxis_output_report_01 default_report = {
Frank Praznik55d3b662014-04-14 10:11:32 -04001810 .buf = {
1811 0x01,
Scott Moreauad07b7a2016-01-13 07:40:43 -07001812 0x01, 0xff, 0x00, 0xff, 0x00,
Frank Praznik55d3b662014-04-14 10:11:32 -04001813 0x00, 0x00, 0x00, 0x00, 0x00,
1814 0xff, 0x27, 0x10, 0x00, 0x32,
1815 0xff, 0x27, 0x10, 0x00, 0x32,
1816 0xff, 0x27, 0x10, 0x00, 0x32,
1817 0xff, 0x27, 0x10, 0x00, 0x32,
1818 0x00, 0x00, 0x00, 0x00, 0x00
1819 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001820 };
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001821 struct sixaxis_output_report *report =
1822 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1823 int n;
1824
1825 /* Initialize the report with default values */
1826 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001827
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001828#ifdef CONFIG_SONY_FF
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001829 report->rumble.right_motor_on = sc->right ? 1 : 0;
1830 report->rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001831#endif
1832
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001833 report->leds_bitmap |= sc->led_state[0] << 1;
1834 report->leds_bitmap |= sc->led_state[1] << 2;
1835 report->leds_bitmap |= sc->led_state[2] << 3;
1836 report->leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001837
Simon Wood88f65762014-04-14 10:11:37 -04001838 /* Set flag for all leds off, required for 3rd party INTEC controller */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001839 if ((report->leds_bitmap & 0x1E) == 0)
1840 report->leds_bitmap |= 0x20;
Simon Wood88f65762014-04-14 10:11:37 -04001841
Frank Praznikb3ed4582014-04-14 10:11:36 -04001842 /*
1843 * The LEDs in the report are indexed in reverse order to their
1844 * corresponding light on the controller.
1845 * Index 0 = LED 4, index 1 = LED 3, etc...
1846 *
1847 * In the case of both delay values being zero (blinking disabled) the
1848 * default report values should be used or the controller LED will be
1849 * always off.
1850 */
1851 for (n = 0; n < 4; n++) {
1852 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001853 report->led[3 - n].duty_off = sc->led_delay_off[n];
1854 report->led[3 - n].duty_on = sc->led_delay_on[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001855 }
1856 }
1857
Pavel Machek1adf9042016-02-09 13:55:08 +01001858 hid_hw_raw_request(sc->hdev, report->report_id, (u8 *)report,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001859 sizeof(struct sixaxis_output_report),
1860 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001861}
1862
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001863static void dualshock4_send_output_report(struct sony_sc *sc)
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001864{
Frank Praznik0da8ea62014-01-16 21:42:51 -05001865 struct hid_device *hdev = sc->hdev;
Pavel Machek1adf9042016-02-09 13:55:08 +01001866 u8 *buf = sc->output_report_dmabuf;
Frank Praznik48220232014-02-05 20:03:44 -05001867 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001868
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001869 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001870 memset(buf, 0, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001871 buf[0] = 0x05;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001872 buf[1] = 0xFF;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001873 offset = 4;
1874 } else {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001875 memset(buf, 0, DS4_REPORT_0x11_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001876 buf[0] = 0x11;
Rostislav Pehlivanov824deff2015-07-28 08:38:47 +01001877 buf[1] = 0x80;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001878 buf[3] = 0x0F;
1879 offset = 6;
1880 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001881
1882#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001883 buf[offset++] = sc->right;
1884 buf[offset++] = sc->left;
1885#else
1886 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001887#endif
1888
Frank Praznikb3ed4582014-04-14 10:11:36 -04001889 /* LED 3 is the global control */
1890 if (sc->led_state[3]) {
1891 buf[offset++] = sc->led_state[0];
1892 buf[offset++] = sc->led_state[1];
1893 buf[offset++] = sc->led_state[2];
1894 } else {
1895 offset += 3;
1896 }
1897
1898 /* If both delay values are zero the DualShock 4 disables blinking. */
1899 buf[offset++] = sc->led_delay_on[3];
1900 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05001901
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001902 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001903 hid_hw_output_report(hdev, buf, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001904 else
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001905 hid_hw_raw_request(hdev, 0x11, buf, DS4_REPORT_0x11_SIZE,
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001906 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001907}
1908
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001909static void motion_send_output_report(struct sony_sc *sc)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001910{
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001911 struct hid_device *hdev = sc->hdev;
1912 struct motion_output_report_02 *report =
1913 (struct motion_output_report_02 *)sc->output_report_dmabuf;
1914
Simon Wood41d2d422015-06-09 21:27:06 -06001915 memset(report, 0, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001916
1917 report->type = 0x02; /* set leds */
1918 report->r = sc->led_state[0];
1919 report->g = sc->led_state[1];
1920 report->b = sc->led_state[2];
1921
1922#ifdef CONFIG_SONY_FF
1923 report->rumble = max(sc->right, sc->left);
1924#endif
1925
Pavel Machek1adf9042016-02-09 13:55:08 +01001926 hid_hw_output_report(hdev, (u8 *)report, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001927}
1928
Frank Praznikdecd9462015-11-11 09:49:38 -05001929static inline void sony_send_output_report(struct sony_sc *sc)
1930{
1931 if (sc->send_output_report)
1932 sc->send_output_report(sc);
1933}
1934
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001935static void sony_state_worker(struct work_struct *work)
1936{
1937 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Antonio Ospiteef916ef2016-02-09 13:55:07 +01001938
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001939 sc->send_output_report(sc);
1940}
1941
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001942static int sony_allocate_output_report(struct sony_sc *sc)
1943{
Simon Wood4545ee02015-06-17 00:08:52 -06001944 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
1945 (sc->quirks & NAVIGATION_CONTROLLER))
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001946 sc->output_report_dmabuf =
1947 kmalloc(sizeof(union sixaxis_output_report_01),
1948 GFP_KERNEL);
1949 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1950 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x11_SIZE,
1951 GFP_KERNEL);
1952 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1953 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x05_SIZE,
1954 GFP_KERNEL);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001955 else if (sc->quirks & MOTION_CONTROLLER)
Simon Wood41d2d422015-06-09 21:27:06 -06001956 sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE,
1957 GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001958 else
1959 return 0;
1960
1961 if (!sc->output_report_dmabuf)
1962 return -ENOMEM;
1963
1964 return 0;
1965}
1966
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001967#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001968static int sony_play_effect(struct input_dev *dev, void *data,
1969 struct ff_effect *effect)
1970{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001971 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001972 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001973
1974 if (effect->type != FF_RUMBLE)
1975 return 0;
1976
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001977 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001978 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001979
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001980 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001981 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001982}
1983
Frank Praznikfa57a812014-04-14 10:11:33 -04001984static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001985{
Frank Praznikfa57a812014-04-14 10:11:33 -04001986 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001987 struct hid_input, list);
1988 struct input_dev *input_dev = hidinput->input;
1989
1990 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1991 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1992}
1993
1994#else
Frank Praznikfa57a812014-04-14 10:11:33 -04001995static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001996{
1997 return 0;
1998}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001999
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002000#endif
2001
Frank Praznikd902f472014-01-27 10:17:36 -05002002static int sony_battery_get_property(struct power_supply *psy,
2003 enum power_supply_property psp,
2004 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002005{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002006 struct sony_sc *sc = power_supply_get_drvdata(psy);
Frank Praznikd902f472014-01-27 10:17:36 -05002007 unsigned long flags;
2008 int ret = 0;
2009 u8 battery_charging, battery_capacity, cable_state;
2010
2011 spin_lock_irqsave(&sc->lock, flags);
2012 battery_charging = sc->battery_charging;
2013 battery_capacity = sc->battery_capacity;
2014 cable_state = sc->cable_state;
2015 spin_unlock_irqrestore(&sc->lock, flags);
2016
2017 switch (psp) {
2018 case POWER_SUPPLY_PROP_PRESENT:
2019 val->intval = 1;
2020 break;
2021 case POWER_SUPPLY_PROP_SCOPE:
2022 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
2023 break;
2024 case POWER_SUPPLY_PROP_CAPACITY:
2025 val->intval = battery_capacity;
2026 break;
2027 case POWER_SUPPLY_PROP_STATUS:
2028 if (battery_charging)
2029 val->intval = POWER_SUPPLY_STATUS_CHARGING;
2030 else
2031 if (battery_capacity == 100 && cable_state)
2032 val->intval = POWER_SUPPLY_STATUS_FULL;
2033 else
2034 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
2035 break;
2036 default:
2037 ret = -EINVAL;
2038 break;
2039 }
2040 return ret;
2041}
2042
2043static int sony_battery_probe(struct sony_sc *sc)
2044{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002045 struct power_supply_config psy_cfg = { .drv_data = sc, };
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002046 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05002047 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002048
Frank Praznikad142b92014-02-20 11:36:00 -05002049 /*
2050 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05002051 * if the battery is polled before the first device report is received.
2052 */
2053 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002054
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002055 sc->battery_desc.properties = sony_battery_props;
2056 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
2057 sc->battery_desc.get_property = sony_battery_get_property;
2058 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
2059 sc->battery_desc.use_for_apm = 0;
2060 sc->battery_desc.name = kasprintf(GFP_KERNEL,
2061 "sony_controller_battery_%pMR",
2062 sc->mac_address);
2063 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05002064 return -ENOMEM;
2065
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002066 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
2067 &psy_cfg);
2068 if (IS_ERR(sc->battery)) {
2069 ret = PTR_ERR(sc->battery);
Frank Praznikd902f472014-01-27 10:17:36 -05002070 hid_err(hdev, "Unable to register battery device\n");
2071 goto err_free;
2072 }
2073
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002074 power_supply_powers(sc->battery, &hdev->dev);
Frank Praznikd902f472014-01-27 10:17:36 -05002075 return 0;
2076
2077err_free:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002078 kfree(sc->battery_desc.name);
2079 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05002080 return ret;
2081}
2082
2083static void sony_battery_remove(struct sony_sc *sc)
2084{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002085 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05002086 return;
2087
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002088 power_supply_unregister(sc->battery);
2089 kfree(sc->battery_desc.name);
2090 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05002091}
2092
Frank Praznikd2d782f2014-02-20 11:36:03 -05002093/*
2094 * If a controller is plugged in via USB while already connected via Bluetooth
2095 * it will show up as two devices. A global list of connected controllers and
2096 * their MAC addresses is maintained to ensure that a device is only connected
2097 * once.
2098 */
2099static int sony_check_add_dev_list(struct sony_sc *sc)
2100{
2101 struct sony_sc *entry;
2102 unsigned long flags;
2103 int ret;
2104
2105 spin_lock_irqsave(&sony_dev_list_lock, flags);
2106
2107 list_for_each_entry(entry, &sony_device_list, list_node) {
2108 ret = memcmp(sc->mac_address, entry->mac_address,
2109 sizeof(sc->mac_address));
2110 if (!ret) {
2111 ret = -EEXIST;
2112 hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
2113 sc->mac_address);
2114 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002115 }
2116 }
2117
Frank Praznikd2d782f2014-02-20 11:36:03 -05002118 ret = 0;
2119 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002120
Frank Praznikd2d782f2014-02-20 11:36:03 -05002121unlock:
2122 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2123 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002124}
2125
Frank Praznikd2d782f2014-02-20 11:36:03 -05002126static void sony_remove_dev_list(struct sony_sc *sc)
2127{
2128 unsigned long flags;
2129
2130 if (sc->list_node.next) {
2131 spin_lock_irqsave(&sony_dev_list_lock, flags);
2132 list_del(&(sc->list_node));
2133 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2134 }
2135}
2136
2137static int sony_get_bt_devaddr(struct sony_sc *sc)
2138{
2139 int ret;
2140
2141 /* HIDP stores the device MAC address as a string in the uniq field. */
2142 ret = strlen(sc->hdev->uniq);
2143 if (ret != 17)
2144 return -EINVAL;
2145
2146 ret = sscanf(sc->hdev->uniq,
2147 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2148 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
2149 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
2150
2151 if (ret != 6)
2152 return -EINVAL;
2153
2154 return 0;
2155}
2156
2157static int sony_check_add(struct sony_sc *sc)
2158{
Pavel Machek1adf9042016-02-09 13:55:08 +01002159 u8 *buf = NULL;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002160 int n, ret;
2161
2162 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
Simon Wood12e9a6d2015-06-09 21:27:05 -06002163 (sc->quirks & MOTION_CONTROLLER_BT) ||
Simon Wood4545ee02015-06-17 00:08:52 -06002164 (sc->quirks & NAVIGATION_CONTROLLER_BT) ||
Frank Praznikd2d782f2014-02-20 11:36:03 -05002165 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
2166 /*
2167 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
2168 * address from the uniq string where HIDP stores it.
2169 * As uniq cannot be guaranteed to be a MAC address in all cases
2170 * a failure of this function should not prevent the connection.
2171 */
2172 if (sony_get_bt_devaddr(sc) < 0) {
2173 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
2174 return 0;
2175 }
2176 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002177 buf = kmalloc(DS4_REPORT_0x81_SIZE, GFP_KERNEL);
2178 if (!buf)
2179 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002180
2181 /*
2182 * The MAC address of a DS4 controller connected via USB can be
2183 * retrieved with feature report 0x81. The address begins at
2184 * offset 1.
2185 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002186 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
2187 DS4_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
2188 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002189
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002190 if (ret != DS4_REPORT_0x81_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002191 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002192 ret = ret < 0 ? ret : -EINVAL;
2193 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002194 }
2195
2196 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
Simon Wood4545ee02015-06-17 00:08:52 -06002197 } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2198 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002199 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2200 if (!buf)
2201 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002202
2203 /*
2204 * The MAC address of a Sixaxis controller connected via USB can
2205 * be retrieved with feature report 0xf2. The address begins at
2206 * offset 4.
2207 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002208 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2209 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2210 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002211
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002212 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002213 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002214 ret = ret < 0 ? ret : -EINVAL;
2215 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002216 }
2217
2218 /*
2219 * The Sixaxis device MAC in the report is big-endian and must
2220 * be byte-swapped.
2221 */
2222 for (n = 0; n < 6; n++)
2223 sc->mac_address[5-n] = buf[4+n];
2224 } else {
2225 return 0;
2226 }
2227
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002228 ret = sony_check_add_dev_list(sc);
2229
2230out_free:
2231
2232 kfree(buf);
2233
2234 return ret;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002235}
2236
Frank Praznik80250872014-04-14 10:11:35 -04002237static int sony_set_device_id(struct sony_sc *sc)
2238{
2239 int ret;
2240
2241 /*
2242 * Only DualShock 4 or Sixaxis controllers get an id.
2243 * All others are set to -1.
2244 */
2245 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2246 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2247 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2248 GFP_KERNEL);
2249 if (ret < 0) {
2250 sc->device_id = -1;
2251 return ret;
2252 }
2253 sc->device_id = ret;
2254 } else {
2255 sc->device_id = -1;
2256 }
2257
2258 return 0;
2259}
2260
2261static void sony_release_device_id(struct sony_sc *sc)
2262{
2263 if (sc->device_id >= 0) {
2264 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2265 sc->device_id = -1;
2266 }
2267}
2268
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002269static inline void sony_init_output_report(struct sony_sc *sc,
Antonio Ospite09593e32016-02-09 13:55:06 +01002270 void (*send_output_report)(struct sony_sc *))
Frank Praznik46262042014-04-14 10:11:31 -04002271{
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002272 sc->send_output_report = send_output_report;
2273
Frank Praznik46262042014-04-14 10:11:31 -04002274 if (!sc->worker_initialized)
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002275 INIT_WORK(&sc->state_worker, sony_state_worker);
Frank Praznik46262042014-04-14 10:11:31 -04002276
2277 sc->worker_initialized = 1;
2278}
2279
2280static inline void sony_cancel_work_sync(struct sony_sc *sc)
2281{
2282 if (sc->worker_initialized)
2283 cancel_work_sync(&sc->state_worker);
2284}
Frank Praznikd2d782f2014-02-20 11:36:03 -05002285
Jiri Slabybd28ce02008-06-25 23:47:04 +02002286static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2287{
2288 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002289 unsigned long quirks = id->driver_data;
2290 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02002291 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002292
Mikko Perttunen4ba1eee2016-07-21 19:54:48 +03002293 if (!strcmp(hdev->name, "FutureMax Dance Mat"))
2294 quirks |= FUTUREMAX_DANCE_MAT;
2295
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002296 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002297 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08002298 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002299 return -ENOMEM;
2300 }
2301
Frank Praznikb94993f2015-02-22 20:42:46 -05002302 spin_lock_init(&sc->lock);
2303
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002304 sc->quirks = quirks;
2305 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002306 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002307
Jiri Slabybd28ce02008-06-25 23:47:04 +02002308 ret = hid_parse(hdev);
2309 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002310 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002311 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002312 }
2313
Colin Leitnerf04d5142013-05-27 23:41:05 +02002314 if (sc->quirks & VAIO_RDESC_CONSTANT)
2315 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
Antonio Ospite50764652014-06-24 13:28:40 +02002316 else if (sc->quirks & SIXAXIS_CONTROLLER)
Colin Leitnerf04d5142013-05-27 23:41:05 +02002317 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2318
2319 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002320 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002321 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002322 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002323 }
2324
Frank Praznik80250872014-04-14 10:11:35 -04002325 ret = sony_set_device_id(sc);
2326 if (ret < 0) {
2327 hid_err(hdev, "failed to allocate the device id\n");
2328 goto err_stop;
2329 }
2330
Frank Praznik131a8a92015-05-05 20:47:28 -04002331 ret = sony_allocate_output_report(sc);
2332 if (ret < 0) {
2333 hid_err(hdev, "failed to allocate the output report buffer\n");
2334 goto err_stop;
2335 }
2336
Simon Wood4545ee02015-06-17 00:08:52 -06002337 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2338 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002339 /*
2340 * The Sony Sixaxis does not handle HID Output Reports on the
2341 * Interrupt EP like it could, so we need to force HID Output
2342 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2343 *
2344 * There is also another issue about HID Output Reports via USB,
2345 * the Sixaxis does not want the report_id as part of the data
2346 * packet, so we have to discard buf[0] when sending the actual
2347 * control message, even for numbered reports, humpf!
2348 */
2349 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2350 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
Antonio Ospite816651a2010-05-03 22:15:55 +02002351 ret = sixaxis_set_operational_usb(hdev);
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002352 sony_init_output_report(sc, sixaxis_send_output_report);
Simon Wood4545ee02015-06-17 00:08:52 -06002353 } else if ((sc->quirks & SIXAXIS_CONTROLLER_BT) ||
2354 (sc->quirks & NAVIGATION_CONTROLLER_BT)) {
Frank Praznik2078b9b2014-03-15 09:41:16 -04002355 /*
2356 * The Sixaxis wants output reports sent on the ctrl endpoint
2357 * when connected via Bluetooth.
2358 */
2359 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Antonio Ospite816651a2010-05-03 22:15:55 +02002360 ret = sixaxis_set_operational_bt(hdev);
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002361 sony_init_output_report(sc, sixaxis_send_output_report);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002362 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik68330d82014-02-05 20:03:49 -05002363 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
Frank Praznik2078b9b2014-03-15 09:41:16 -04002364 /*
2365 * The DualShock 4 wants output reports sent on the ctrl
2366 * endpoint when connected via Bluetooth.
2367 */
2368 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Frank Praznik68330d82014-02-05 20:03:49 -05002369 ret = dualshock4_set_operational_bt(hdev);
2370 if (ret < 0) {
2371 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
2372 goto err_stop;
2373 }
2374 }
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002375
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002376 sony_init_output_report(sc, dualshock4_send_output_report);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002377 } else if (sc->quirks & MOTION_CONTROLLER) {
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002378 sony_init_output_report(sc, motion_send_output_report);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002379 } else {
2380 ret = 0;
2381 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00002382
Jiri Kosina4dfdc462008-12-30 00:49:59 +01002383 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002384 goto err_stop;
2385
Frank Praznikd2d782f2014-02-20 11:36:03 -05002386 ret = sony_check_add(sc);
2387 if (ret < 0)
2388 goto err_stop;
2389
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002390 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002391 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002392 if (ret < 0)
2393 goto err_stop;
2394 }
2395
Frank Praznikd902f472014-01-27 10:17:36 -05002396 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2397 ret = sony_battery_probe(sc);
2398 if (ret < 0)
2399 goto err_stop;
2400
2401 /* Open the device to receive reports with battery info */
2402 ret = hid_hw_open(hdev);
2403 if (ret < 0) {
2404 hid_err(hdev, "hw open failed\n");
2405 goto err_stop;
2406 }
2407 }
2408
Frank Praznikc8de9db2014-02-20 11:36:01 -05002409 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002410 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05002411 if (ret < 0)
2412 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05002413 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002414
Jiri Slabybd28ce02008-06-25 23:47:04 +02002415 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05002416err_close:
2417 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002418err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002419 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002420 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002421 if (sc->quirks & SONY_BATTERY_SUPPORT)
2422 sony_battery_remove(sc);
Frank Praznik46262042014-04-14 10:11:31 -04002423 sony_cancel_work_sync(sc);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002424 kfree(sc->output_report_dmabuf);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002425 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04002426 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002427 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002428 return ret;
2429}
2430
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002431static void sony_remove(struct hid_device *hdev)
2432{
Colin Leitnerf04d5142013-05-27 23:41:05 +02002433 struct sony_sc *sc = hid_get_drvdata(hdev);
2434
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002435 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002436 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02002437
Frank Praznikd902f472014-01-27 10:17:36 -05002438 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2439 hid_hw_close(hdev);
2440 sony_battery_remove(sc);
2441 }
2442
Frank Praznik46262042014-04-14 10:11:31 -04002443 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002444
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002445 kfree(sc->output_report_dmabuf);
2446
Frank Praznikd2d782f2014-02-20 11:36:03 -05002447 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002448
Frank Praznik80250872014-04-14 10:11:35 -04002449 sony_release_device_id(sc);
2450
Jiri Slabybd28ce02008-06-25 23:47:04 +02002451 hid_hw_stop(hdev);
2452}
2453
Frank Praznikdecd9462015-11-11 09:49:38 -05002454#ifdef CONFIG_PM
2455
2456static int sony_suspend(struct hid_device *hdev, pm_message_t message)
2457{
2458 /*
2459 * On suspend save the current LED state,
2460 * stop running force-feedback and blank the LEDS.
Antonio Ospite09593e32016-02-09 13:55:06 +01002461 */
Frank Praznikdecd9462015-11-11 09:49:38 -05002462 if (SONY_LED_SUPPORT || SONY_FF_SUPPORT) {
2463 struct sony_sc *sc = hid_get_drvdata(hdev);
2464
2465#ifdef CONFIG_SONY_FF
2466 sc->left = sc->right = 0;
2467#endif
2468
2469 memcpy(sc->resume_led_state, sc->led_state,
2470 sizeof(sc->resume_led_state));
2471 memset(sc->led_state, 0, sizeof(sc->led_state));
2472
2473 sony_send_output_report(sc);
2474 }
2475
2476 return 0;
2477}
2478
2479static int sony_resume(struct hid_device *hdev)
2480{
2481 /* Restore the state of controller LEDs on resume */
2482 if (SONY_LED_SUPPORT) {
2483 struct sony_sc *sc = hid_get_drvdata(hdev);
2484
2485 memcpy(sc->led_state, sc->resume_led_state,
2486 sizeof(sc->led_state));
2487
2488 /*
2489 * The Sixaxis and navigation controllers on USB need to be
2490 * reinitialized on resume or they won't behave properly.
2491 */
2492 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2493 (sc->quirks & NAVIGATION_CONTROLLER_USB))
2494 sixaxis_set_operational_usb(sc->hdev);
2495
2496 sony_set_leds(sc);
2497 }
2498
2499 return 0;
2500}
2501
2502#endif
2503
Jiri Slabybd28ce02008-06-25 23:47:04 +02002504static const struct hid_device_id sony_devices[] = {
2505 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2506 .driver_data = SIXAXIS_CONTROLLER_USB },
2507 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
Simon Wood4545ee02015-06-17 00:08:52 -06002508 .driver_data = NAVIGATION_CONTROLLER_USB },
Simon Wood6eabaaa2015-06-17 00:08:51 -06002509 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
Simon Wood4545ee02015-06-17 00:08:52 -06002510 .driver_data = NAVIGATION_CONTROLLER_BT },
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002511 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002512 .driver_data = MOTION_CONTROLLER_USB },
Simon Wooda4afa852015-06-03 09:45:19 -06002513 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002514 .driver_data = MOTION_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002515 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2516 .driver_data = SIXAXIS_CONTROLLER_BT },
2517 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2518 .driver_data = VAIO_RDESC_CONSTANT },
2519 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2520 .driver_data = VAIO_RDESC_CONSTANT },
Antonio Ospiteef916ef2016-02-09 13:55:07 +01002521 /*
2522 * Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2523 * Logitech joystick from the device descriptor.
2524 */
Jiri Slabybd28ce02008-06-25 23:47:04 +02002525 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2526 .driver_data = BUZZ_CONTROLLER },
2527 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2528 .driver_data = BUZZ_CONTROLLER },
2529 /* PS3 BD Remote Control */
2530 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2531 .driver_data = PS3REMOTE },
2532 /* Logitech Harmony Adapter for PS3 */
2533 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2534 .driver_data = PS3REMOTE },
Frank Praznik68a49e52014-11-12 14:52:28 -05002535 /* SMK-Link PS3 BD Remote Control */
2536 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2537 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002538 /* Sony Dualshock 4 controllers for PS4 */
2539 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002540 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002541 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002542 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Scott Moreau74500cc2016-01-13 07:40:42 -07002543 /* Nyko Core Controller for PS3 */
2544 { HID_USB_DEVICE(USB_VENDOR_ID_SINO_LITE, USB_DEVICE_ID_SINO_LITE_CONTROLLER),
2545 .driver_data = SIXAXIS_CONTROLLER_USB | SINO_LITE_CONTROLLER },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002546 { }
2547};
2548MODULE_DEVICE_TABLE(hid, sony_devices);
2549
2550static struct hid_driver sony_driver = {
Frank Praznikce8efc32014-09-18 21:15:01 -04002551 .name = "sony",
2552 .id_table = sony_devices,
2553 .input_mapping = sony_mapping,
2554 .input_configured = sony_input_configured,
2555 .probe = sony_probe,
2556 .remove = sony_remove,
2557 .report_fixup = sony_report_fixup,
Frank Praznikdecd9462015-11-11 09:49:38 -05002558 .raw_event = sony_raw_event,
2559
2560#ifdef CONFIG_PM
2561 .suspend = sony_suspend,
2562 .resume = sony_resume,
2563 .reset_resume = sony_resume,
2564#endif
Jiri Slabybd28ce02008-06-25 23:47:04 +02002565};
Frank Praznik80250872014-04-14 10:11:35 -04002566
2567static int __init sony_init(void)
2568{
2569 dbg_hid("Sony:%s\n", __func__);
2570
2571 return hid_register_driver(&sony_driver);
2572}
2573
2574static void __exit sony_exit(void)
2575{
2576 dbg_hid("Sony:%s\n", __func__);
2577
Frank Praznik80250872014-04-14 10:11:35 -04002578 hid_unregister_driver(&sony_driver);
Antonio Ospite6c400652015-02-16 22:58:24 +01002579 ida_destroy(&sony_device_id_allocator);
Frank Praznik80250872014-04-14 10:11:35 -04002580}
2581module_init(sony_init);
2582module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002583
2584MODULE_LICENSE("GPL");