blob: 937050971ccd48e7f1aec5bc61b099c13181f0ab [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)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040049#define MOTION_CONTROLLER BIT(7)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020050
Frank Praznikfee4e2d2014-02-18 17:22:01 -050051#define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
Frank Praznik68330d82014-02-05 20:03:49 -050052#define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
53 DUALSHOCK4_CONTROLLER_BT)
Frank Praznikfee4e2d2014-02-18 17:22:01 -050054#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040055 DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER)
Frank Praznikfee4e2d2014-02-18 17:22:01 -050056#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040057#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
58 MOTION_CONTROLLER)
Frank Praznik60781cf2014-01-11 15:13:15 -050059
60#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010061
Antonio Ospitec607fb82014-06-24 13:28:41 +020062static __u8 sixaxis_rdesc[] = {
Antonio Ospitefb705a62014-06-24 13:28:42 +020063 0x05, 0x01, /* Usage Page (Desktop), */
64 0x09, 0x04, /* Usage (Joystik), */
65 0xA1, 0x01, /* Collection (Application), */
66 0xA1, 0x02, /* Collection (Logical), */
67 0x85, 0x01, /* Report ID (1), */
68 0x75, 0x08, /* Report Size (8), */
69 0x95, 0x01, /* Report Count (1), */
70 0x15, 0x00, /* Logical Minimum (0), */
71 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
72 0x81, 0x03, /* Input (Constant, Variable), */
73 0x75, 0x01, /* Report Size (1), */
74 0x95, 0x13, /* Report Count (19), */
75 0x15, 0x00, /* Logical Minimum (0), */
76 0x25, 0x01, /* Logical Maximum (1), */
77 0x35, 0x00, /* Physical Minimum (0), */
78 0x45, 0x01, /* Physical Maximum (1), */
79 0x05, 0x09, /* Usage Page (Button), */
80 0x19, 0x01, /* Usage Minimum (01h), */
81 0x29, 0x13, /* Usage Maximum (13h), */
82 0x81, 0x02, /* Input (Variable), */
83 0x75, 0x01, /* Report Size (1), */
84 0x95, 0x0D, /* Report Count (13), */
85 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
86 0x81, 0x03, /* Input (Constant, Variable), */
87 0x15, 0x00, /* Logical Minimum (0), */
88 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
89 0x05, 0x01, /* Usage Page (Desktop), */
90 0x09, 0x01, /* Usage (Pointer), */
91 0xA1, 0x00, /* Collection (Physical), */
92 0x75, 0x08, /* Report Size (8), */
93 0x95, 0x04, /* Report Count (4), */
94 0x35, 0x00, /* Physical Minimum (0), */
95 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
96 0x09, 0x30, /* Usage (X), */
97 0x09, 0x31, /* Usage (Y), */
98 0x09, 0x32, /* Usage (Z), */
99 0x09, 0x35, /* Usage (Rz), */
100 0x81, 0x02, /* Input (Variable), */
101 0xC0, /* End Collection, */
102 0x05, 0x01, /* Usage Page (Desktop), */
103 0x95, 0x13, /* Report Count (19), */
104 0x09, 0x01, /* Usage (Pointer), */
105 0x81, 0x02, /* Input (Variable), */
106 0x95, 0x0C, /* Report Count (12), */
107 0x81, 0x01, /* Input (Constant), */
108 0x75, 0x10, /* Report Size (16), */
109 0x95, 0x04, /* Report Count (4), */
110 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
111 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
112 0x09, 0x01, /* Usage (Pointer), */
113 0x81, 0x02, /* Input (Variable), */
114 0xC0, /* End Collection, */
115 0xA1, 0x02, /* Collection (Logical), */
116 0x85, 0x02, /* Report ID (2), */
117 0x75, 0x08, /* Report Size (8), */
118 0x95, 0x30, /* Report Count (48), */
119 0x09, 0x01, /* Usage (Pointer), */
120 0xB1, 0x02, /* Feature (Variable), */
121 0xC0, /* End Collection, */
122 0xA1, 0x02, /* Collection (Logical), */
123 0x85, 0xEE, /* Report ID (238), */
124 0x75, 0x08, /* Report Size (8), */
125 0x95, 0x30, /* Report Count (48), */
126 0x09, 0x01, /* Usage (Pointer), */
127 0xB1, 0x02, /* Feature (Variable), */
128 0xC0, /* End Collection, */
129 0xA1, 0x02, /* Collection (Logical), */
130 0x85, 0xEF, /* Report ID (239), */
131 0x75, 0x08, /* Report Size (8), */
132 0x95, 0x30, /* Report Count (48), */
133 0x09, 0x01, /* Usage (Pointer), */
134 0xB1, 0x02, /* Feature (Variable), */
135 0xC0, /* End Collection, */
136 0xC0 /* End Collection */
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200137};
138
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400139/* PS/3 Motion controller */
140static __u8 motion_rdesc[] = {
141 0x05, 0x01, /* Usage Page (Desktop), */
142 0x09, 0x04, /* Usage (Joystick), */
143 0xA1, 0x01, /* Collection (Application), */
144 0xA1, 0x02, /* Collection (Logical), */
145 0x85, 0x01, /* Report ID (1), */
146 0x75, 0x08, /* Report Size (8), */
147 0x95, 0x01, /* Report Count (1), */
148 0x15, 0x00, /* Logical Minimum (0), */
149 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
150 0x81, 0x03, /* Input (Constant, Variable), */
151 0x75, 0x01, /* Report Size (1), */
152 0x95, 0x13, /* Report Count (19), */
153 0x15, 0x00, /* Logical Minimum (0), */
154 0x25, 0x01, /* Logical Maximum (1), */
155 0x35, 0x00, /* Physical Minimum (0), */
156 0x45, 0x01, /* Physical Maximum (1), */
157 0x05, 0x09, /* Usage Page (Button), */
158 0x19, 0x01, /* Usage Minimum (01h), */
159 0x29, 0x13, /* Usage Maximum (13h), */
160 0x81, 0x02, /* Input (Variable), */
161 0x75, 0x01, /* Report Size (1), */
162 0x95, 0x0D, /* Report Count (13), */
163 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
164 0x81, 0x03, /* Input (Constant, Variable), */
165 0x15, 0x00, /* Logical Minimum (0), */
166 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
167 0x05, 0x01, /* Usage Page (Desktop), */
168 0x09, 0x01, /* Usage (Pointer), */
169 0xA1, 0x00, /* Collection (Physical), */
170 0x75, 0x08, /* Report Size (8), */
171 0x95, 0x04, /* Report Count (4), */
172 0x35, 0x00, /* Physical Minimum (0), */
173 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
174 0x09, 0x30, /* Usage (X), */
175 0x09, 0x31, /* Usage (Y), */
176 0x09, 0x32, /* Usage (Z), */
177 0x09, 0x35, /* Usage (Rz), */
178 0x81, 0x02, /* Input (Variable), */
179 0xC0, /* End Collection, */
180 0x05, 0x01, /* Usage Page (Desktop), */
181 0x95, 0x13, /* Report Count (19), */
182 0x09, 0x01, /* Usage (Pointer), */
183 0x81, 0x02, /* Input (Variable), */
184 0x95, 0x0C, /* Report Count (12), */
185 0x81, 0x01, /* Input (Constant), */
186 0x75, 0x10, /* Report Size (16), */
187 0x95, 0x04, /* Report Count (4), */
188 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
189 0x46, 0xFF, 0x03, /* Physical Maximum (1023), */
190 0x09, 0x01, /* Usage (Pointer), */
191 0x81, 0x02, /* Input (Variable), */
192 0xC0, /* End Collection, */
193 0xA1, 0x02, /* Collection (Logical), */
194 0x85, 0x02, /* Report ID (2), */
195 0x75, 0x08, /* Report Size (8), */
196 0x95, 0x30, /* Report Count (48), */
197 0x09, 0x01, /* Usage (Pointer), */
198 0xB1, 0x02, /* Feature (Variable), */
199 0xC0, /* End Collection, */
200 0xA1, 0x02, /* Collection (Logical), */
201 0x85, 0xEE, /* Report ID (238), */
202 0x75, 0x08, /* Report Size (8), */
203 0x95, 0x30, /* Report Count (48), */
204 0x09, 0x01, /* Usage (Pointer), */
205 0xB1, 0x02, /* Feature (Variable), */
206 0xC0, /* End Collection, */
207 0xA1, 0x02, /* Collection (Logical), */
208 0x85, 0xEF, /* Report ID (239), */
209 0x75, 0x08, /* Report Size (8), */
210 0x95, 0x30, /* Report Count (48), */
211 0x09, 0x01, /* Usage (Pointer), */
212 0xB1, 0x02, /* Feature (Variable), */
213 0xC0, /* End Collection, */
214 0xC0 /* End Collection */
215};
216
217
Frank Praznikad142b92014-02-20 11:36:00 -0500218/*
219 * The default descriptor doesn't provide mapping for the accelerometers
Frank Praznik58d70272014-01-20 12:27:01 -0500220 * or orientation sensors. This fixed descriptor maps the accelerometers
221 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
222 * to usage values 0x43, 0x44 and 0x45.
223 */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500224static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -0500225 0x05, 0x01, /* Usage Page (Desktop), */
226 0x09, 0x05, /* Usage (Gamepad), */
227 0xA1, 0x01, /* Collection (Application), */
228 0x85, 0x01, /* Report ID (1), */
229 0x09, 0x30, /* Usage (X), */
230 0x09, 0x31, /* Usage (Y), */
231 0x09, 0x32, /* Usage (Z), */
232 0x09, 0x35, /* Usage (Rz), */
233 0x15, 0x00, /* Logical Minimum (0), */
234 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
235 0x75, 0x08, /* Report Size (8), */
236 0x95, 0x04, /* Report Count (4), */
237 0x81, 0x02, /* Input (Variable), */
238 0x09, 0x39, /* Usage (Hat Switch), */
239 0x15, 0x00, /* Logical Minimum (0), */
240 0x25, 0x07, /* Logical Maximum (7), */
241 0x35, 0x00, /* Physical Minimum (0), */
242 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
243 0x65, 0x14, /* Unit (Degrees), */
244 0x75, 0x04, /* Report Size (4), */
245 0x95, 0x01, /* Report Count (1), */
246 0x81, 0x42, /* Input (Variable, Null State), */
247 0x65, 0x00, /* Unit, */
248 0x05, 0x09, /* Usage Page (Button), */
249 0x19, 0x01, /* Usage Minimum (01h), */
250 0x29, 0x0E, /* Usage Maximum (0Eh), */
251 0x15, 0x00, /* Logical Minimum (0), */
252 0x25, 0x01, /* Logical Maximum (1), */
253 0x75, 0x01, /* Report Size (1), */
254 0x95, 0x0E, /* Report Count (14), */
255 0x81, 0x02, /* Input (Variable), */
256 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
257 0x09, 0x20, /* Usage (20h), */
258 0x75, 0x06, /* Report Size (6), */
259 0x95, 0x01, /* Report Count (1), */
260 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400261 0x25, 0x3F, /* Logical Maximum (63), */
Frank Praznik58d70272014-01-20 12:27:01 -0500262 0x81, 0x02, /* Input (Variable), */
263 0x05, 0x01, /* Usage Page (Desktop), */
264 0x09, 0x33, /* Usage (Rx), */
265 0x09, 0x34, /* Usage (Ry), */
266 0x15, 0x00, /* Logical Minimum (0), */
267 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
268 0x75, 0x08, /* Report Size (8), */
269 0x95, 0x02, /* Report Count (2), */
270 0x81, 0x02, /* Input (Variable), */
271 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
272 0x09, 0x21, /* Usage (21h), */
273 0x95, 0x03, /* Report Count (3), */
274 0x81, 0x02, /* Input (Variable), */
275 0x05, 0x01, /* Usage Page (Desktop), */
276 0x19, 0x40, /* Usage Minimum (40h), */
277 0x29, 0x42, /* Usage Maximum (42h), */
278 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
279 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
280 0x75, 0x10, /* Report Size (16), */
281 0x95, 0x03, /* Report Count (3), */
282 0x81, 0x02, /* Input (Variable), */
283 0x19, 0x43, /* Usage Minimum (43h), */
284 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400285 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
286 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznik58d70272014-01-20 12:27:01 -0500287 0x95, 0x03, /* Report Count (3), */
288 0x81, 0x02, /* Input (Variable), */
289 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
290 0x09, 0x21, /* Usage (21h), */
291 0x15, 0x00, /* Logical Minimum (0), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400292 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
Frank Praznik58d70272014-01-20 12:27:01 -0500293 0x75, 0x08, /* Report Size (8), */
294 0x95, 0x27, /* Report Count (39), */
295 0x81, 0x02, /* Input (Variable), */
296 0x85, 0x05, /* Report ID (5), */
297 0x09, 0x22, /* Usage (22h), */
298 0x95, 0x1F, /* Report Count (31), */
299 0x91, 0x02, /* Output (Variable), */
300 0x85, 0x04, /* Report ID (4), */
301 0x09, 0x23, /* Usage (23h), */
302 0x95, 0x24, /* Report Count (36), */
303 0xB1, 0x02, /* Feature (Variable), */
304 0x85, 0x02, /* Report ID (2), */
305 0x09, 0x24, /* Usage (24h), */
306 0x95, 0x24, /* Report Count (36), */
307 0xB1, 0x02, /* Feature (Variable), */
308 0x85, 0x08, /* Report ID (8), */
309 0x09, 0x25, /* Usage (25h), */
310 0x95, 0x03, /* Report Count (3), */
311 0xB1, 0x02, /* Feature (Variable), */
312 0x85, 0x10, /* Report ID (16), */
313 0x09, 0x26, /* Usage (26h), */
314 0x95, 0x04, /* Report Count (4), */
315 0xB1, 0x02, /* Feature (Variable), */
316 0x85, 0x11, /* Report ID (17), */
317 0x09, 0x27, /* Usage (27h), */
318 0x95, 0x02, /* Report Count (2), */
319 0xB1, 0x02, /* Feature (Variable), */
320 0x85, 0x12, /* Report ID (18), */
321 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
322 0x09, 0x21, /* Usage (21h), */
323 0x95, 0x0F, /* Report Count (15), */
324 0xB1, 0x02, /* Feature (Variable), */
325 0x85, 0x13, /* Report ID (19), */
326 0x09, 0x22, /* Usage (22h), */
327 0x95, 0x16, /* Report Count (22), */
328 0xB1, 0x02, /* Feature (Variable), */
329 0x85, 0x14, /* Report ID (20), */
330 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
331 0x09, 0x20, /* Usage (20h), */
332 0x95, 0x10, /* Report Count (16), */
333 0xB1, 0x02, /* Feature (Variable), */
334 0x85, 0x15, /* Report ID (21), */
335 0x09, 0x21, /* Usage (21h), */
336 0x95, 0x2C, /* Report Count (44), */
337 0xB1, 0x02, /* Feature (Variable), */
338 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
339 0x85, 0x80, /* Report ID (128), */
340 0x09, 0x20, /* Usage (20h), */
341 0x95, 0x06, /* Report Count (6), */
342 0xB1, 0x02, /* Feature (Variable), */
343 0x85, 0x81, /* Report ID (129), */
344 0x09, 0x21, /* Usage (21h), */
345 0x95, 0x06, /* Report Count (6), */
346 0xB1, 0x02, /* Feature (Variable), */
347 0x85, 0x82, /* Report ID (130), */
348 0x09, 0x22, /* Usage (22h), */
349 0x95, 0x05, /* Report Count (5), */
350 0xB1, 0x02, /* Feature (Variable), */
351 0x85, 0x83, /* Report ID (131), */
352 0x09, 0x23, /* Usage (23h), */
353 0x95, 0x01, /* Report Count (1), */
354 0xB1, 0x02, /* Feature (Variable), */
355 0x85, 0x84, /* Report ID (132), */
356 0x09, 0x24, /* Usage (24h), */
357 0x95, 0x04, /* Report Count (4), */
358 0xB1, 0x02, /* Feature (Variable), */
359 0x85, 0x85, /* Report ID (133), */
360 0x09, 0x25, /* Usage (25h), */
361 0x95, 0x06, /* Report Count (6), */
362 0xB1, 0x02, /* Feature (Variable), */
363 0x85, 0x86, /* Report ID (134), */
364 0x09, 0x26, /* Usage (26h), */
365 0x95, 0x06, /* Report Count (6), */
366 0xB1, 0x02, /* Feature (Variable), */
367 0x85, 0x87, /* Report ID (135), */
368 0x09, 0x27, /* Usage (27h), */
369 0x95, 0x23, /* Report Count (35), */
370 0xB1, 0x02, /* Feature (Variable), */
371 0x85, 0x88, /* Report ID (136), */
372 0x09, 0x28, /* Usage (28h), */
373 0x95, 0x22, /* Report Count (34), */
374 0xB1, 0x02, /* Feature (Variable), */
375 0x85, 0x89, /* Report ID (137), */
376 0x09, 0x29, /* Usage (29h), */
377 0x95, 0x02, /* Report Count (2), */
378 0xB1, 0x02, /* Feature (Variable), */
379 0x85, 0x90, /* Report ID (144), */
380 0x09, 0x30, /* Usage (30h), */
381 0x95, 0x05, /* Report Count (5), */
382 0xB1, 0x02, /* Feature (Variable), */
383 0x85, 0x91, /* Report ID (145), */
384 0x09, 0x31, /* Usage (31h), */
385 0x95, 0x03, /* Report Count (3), */
386 0xB1, 0x02, /* Feature (Variable), */
387 0x85, 0x92, /* Report ID (146), */
388 0x09, 0x32, /* Usage (32h), */
389 0x95, 0x03, /* Report Count (3), */
390 0xB1, 0x02, /* Feature (Variable), */
391 0x85, 0x93, /* Report ID (147), */
392 0x09, 0x33, /* Usage (33h), */
393 0x95, 0x0C, /* Report Count (12), */
394 0xB1, 0x02, /* Feature (Variable), */
395 0x85, 0xA0, /* Report ID (160), */
396 0x09, 0x40, /* Usage (40h), */
397 0x95, 0x06, /* Report Count (6), */
398 0xB1, 0x02, /* Feature (Variable), */
399 0x85, 0xA1, /* Report ID (161), */
400 0x09, 0x41, /* Usage (41h), */
401 0x95, 0x01, /* Report Count (1), */
402 0xB1, 0x02, /* Feature (Variable), */
403 0x85, 0xA2, /* Report ID (162), */
404 0x09, 0x42, /* Usage (42h), */
405 0x95, 0x01, /* Report Count (1), */
406 0xB1, 0x02, /* Feature (Variable), */
407 0x85, 0xA3, /* Report ID (163), */
408 0x09, 0x43, /* Usage (43h), */
409 0x95, 0x30, /* Report Count (48), */
410 0xB1, 0x02, /* Feature (Variable), */
411 0x85, 0xA4, /* Report ID (164), */
412 0x09, 0x44, /* Usage (44h), */
413 0x95, 0x0D, /* Report Count (13), */
414 0xB1, 0x02, /* Feature (Variable), */
415 0x85, 0xA5, /* Report ID (165), */
416 0x09, 0x45, /* Usage (45h), */
417 0x95, 0x15, /* Report Count (21), */
418 0xB1, 0x02, /* Feature (Variable), */
419 0x85, 0xA6, /* Report ID (166), */
420 0x09, 0x46, /* Usage (46h), */
421 0x95, 0x15, /* Report Count (21), */
422 0xB1, 0x02, /* Feature (Variable), */
423 0x85, 0xF0, /* Report ID (240), */
424 0x09, 0x47, /* Usage (47h), */
425 0x95, 0x3F, /* Report Count (63), */
426 0xB1, 0x02, /* Feature (Variable), */
427 0x85, 0xF1, /* Report ID (241), */
428 0x09, 0x48, /* Usage (48h), */
429 0x95, 0x3F, /* Report Count (63), */
430 0xB1, 0x02, /* Feature (Variable), */
431 0x85, 0xF2, /* Report ID (242), */
432 0x09, 0x49, /* Usage (49h), */
433 0x95, 0x0F, /* Report Count (15), */
434 0xB1, 0x02, /* Feature (Variable), */
435 0x85, 0xA7, /* Report ID (167), */
436 0x09, 0x4A, /* Usage (4Ah), */
437 0x95, 0x01, /* Report Count (1), */
438 0xB1, 0x02, /* Feature (Variable), */
439 0x85, 0xA8, /* Report ID (168), */
440 0x09, 0x4B, /* Usage (4Bh), */
441 0x95, 0x01, /* Report Count (1), */
442 0xB1, 0x02, /* Feature (Variable), */
443 0x85, 0xA9, /* Report ID (169), */
444 0x09, 0x4C, /* Usage (4Ch), */
445 0x95, 0x08, /* Report Count (8), */
446 0xB1, 0x02, /* Feature (Variable), */
447 0x85, 0xAA, /* Report ID (170), */
448 0x09, 0x4E, /* Usage (4Eh), */
449 0x95, 0x01, /* Report Count (1), */
450 0xB1, 0x02, /* Feature (Variable), */
451 0x85, 0xAB, /* Report ID (171), */
452 0x09, 0x4F, /* Usage (4Fh), */
453 0x95, 0x39, /* Report Count (57), */
454 0xB1, 0x02, /* Feature (Variable), */
455 0x85, 0xAC, /* Report ID (172), */
456 0x09, 0x50, /* Usage (50h), */
457 0x95, 0x39, /* Report Count (57), */
458 0xB1, 0x02, /* Feature (Variable), */
459 0x85, 0xAD, /* Report ID (173), */
460 0x09, 0x51, /* Usage (51h), */
461 0x95, 0x0B, /* Report Count (11), */
462 0xB1, 0x02, /* Feature (Variable), */
463 0x85, 0xAE, /* Report ID (174), */
464 0x09, 0x52, /* Usage (52h), */
465 0x95, 0x01, /* Report Count (1), */
466 0xB1, 0x02, /* Feature (Variable), */
467 0x85, 0xAF, /* Report ID (175), */
468 0x09, 0x53, /* Usage (53h), */
469 0x95, 0x02, /* Report Count (2), */
470 0xB1, 0x02, /* Feature (Variable), */
471 0x85, 0xB0, /* Report ID (176), */
472 0x09, 0x54, /* Usage (54h), */
473 0x95, 0x3F, /* Report Count (63), */
474 0xB1, 0x02, /* Feature (Variable), */
475 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500476};
477
Frank Praznikad142b92014-02-20 11:36:00 -0500478/*
479 * The default behavior of the Dualshock 4 is to send reports using report
Frank Praznik077147a2014-09-14 11:56:39 -0400480 * type 1 when running over Bluetooth. However, when feature report 2 is
481 * requested during the controller initialization it starts sending input
482 * reports in report 17. Since report 17 is undefined in the default HID
Frank Praznikd8296742014-02-05 20:03:45 -0500483 * descriptor the button and axis definitions must be moved to report 17 or
Frank Praznik077147a2014-09-14 11:56:39 -0400484 * the HID layer won't process the received input.
Frank Praznikd8296742014-02-05 20:03:45 -0500485 */
486static u8 dualshock4_bt_rdesc[] = {
487 0x05, 0x01, /* Usage Page (Desktop), */
488 0x09, 0x05, /* Usage (Gamepad), */
489 0xA1, 0x01, /* Collection (Application), */
490 0x85, 0x01, /* Report ID (1), */
491 0x75, 0x08, /* Report Size (8), */
492 0x95, 0x0A, /* Report Count (9), */
493 0x81, 0x02, /* Input (Variable), */
494 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
495 0x85, 0x02, /* Report ID (2), */
496 0x09, 0x24, /* Usage (24h), */
497 0x95, 0x24, /* Report Count (36), */
498 0xB1, 0x02, /* Feature (Variable), */
499 0x85, 0xA3, /* Report ID (163), */
500 0x09, 0x25, /* Usage (25h), */
501 0x95, 0x30, /* Report Count (48), */
502 0xB1, 0x02, /* Feature (Variable), */
503 0x85, 0x05, /* Report ID (5), */
504 0x09, 0x26, /* Usage (26h), */
505 0x95, 0x28, /* Report Count (40), */
506 0xB1, 0x02, /* Feature (Variable), */
507 0x85, 0x06, /* Report ID (6), */
508 0x09, 0x27, /* Usage (27h), */
509 0x95, 0x34, /* Report Count (52), */
510 0xB1, 0x02, /* Feature (Variable), */
511 0x85, 0x07, /* Report ID (7), */
512 0x09, 0x28, /* Usage (28h), */
513 0x95, 0x30, /* Report Count (48), */
514 0xB1, 0x02, /* Feature (Variable), */
515 0x85, 0x08, /* Report ID (8), */
516 0x09, 0x29, /* Usage (29h), */
517 0x95, 0x2F, /* Report Count (47), */
518 0xB1, 0x02, /* Feature (Variable), */
519 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
520 0x85, 0x03, /* Report ID (3), */
521 0x09, 0x21, /* Usage (21h), */
522 0x95, 0x26, /* Report Count (38), */
523 0xB1, 0x02, /* Feature (Variable), */
524 0x85, 0x04, /* Report ID (4), */
525 0x09, 0x22, /* Usage (22h), */
526 0x95, 0x2E, /* Report Count (46), */
527 0xB1, 0x02, /* Feature (Variable), */
528 0x85, 0xF0, /* Report ID (240), */
529 0x09, 0x47, /* Usage (47h), */
530 0x95, 0x3F, /* Report Count (63), */
531 0xB1, 0x02, /* Feature (Variable), */
532 0x85, 0xF1, /* Report ID (241), */
533 0x09, 0x48, /* Usage (48h), */
534 0x95, 0x3F, /* Report Count (63), */
535 0xB1, 0x02, /* Feature (Variable), */
536 0x85, 0xF2, /* Report ID (242), */
537 0x09, 0x49, /* Usage (49h), */
538 0x95, 0x0F, /* Report Count (15), */
539 0xB1, 0x02, /* Feature (Variable), */
540 0x85, 0x11, /* Report ID (17), */
541 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
542 0x09, 0x20, /* Usage (20h), */
543 0x95, 0x02, /* Report Count (2), */
544 0x81, 0x02, /* Input (Variable), */
545 0x05, 0x01, /* Usage Page (Desktop), */
546 0x09, 0x30, /* Usage (X), */
547 0x09, 0x31, /* Usage (Y), */
548 0x09, 0x32, /* Usage (Z), */
549 0x09, 0x35, /* Usage (Rz), */
550 0x15, 0x00, /* Logical Minimum (0), */
551 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
552 0x75, 0x08, /* Report Size (8), */
553 0x95, 0x04, /* Report Count (4), */
554 0x81, 0x02, /* Input (Variable), */
555 0x09, 0x39, /* Usage (Hat Switch), */
556 0x15, 0x00, /* Logical Minimum (0), */
557 0x25, 0x07, /* Logical Maximum (7), */
558 0x75, 0x04, /* Report Size (4), */
559 0x95, 0x01, /* Report Count (1), */
560 0x81, 0x42, /* Input (Variable, Null State), */
561 0x05, 0x09, /* Usage Page (Button), */
562 0x19, 0x01, /* Usage Minimum (01h), */
563 0x29, 0x0E, /* Usage Maximum (0Eh), */
564 0x15, 0x00, /* Logical Minimum (0), */
565 0x25, 0x01, /* Logical Maximum (1), */
566 0x75, 0x01, /* Report Size (1), */
567 0x95, 0x0E, /* Report Count (14), */
568 0x81, 0x02, /* Input (Variable), */
569 0x75, 0x06, /* Report Size (6), */
570 0x95, 0x01, /* Report Count (1), */
571 0x81, 0x01, /* Input (Constant), */
572 0x05, 0x01, /* Usage Page (Desktop), */
573 0x09, 0x33, /* Usage (Rx), */
574 0x09, 0x34, /* Usage (Ry), */
575 0x15, 0x00, /* Logical Minimum (0), */
576 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
577 0x75, 0x08, /* Report Size (8), */
578 0x95, 0x02, /* Report Count (2), */
579 0x81, 0x02, /* Input (Variable), */
580 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
581 0x09, 0x20, /* Usage (20h), */
582 0x95, 0x03, /* Report Count (3), */
583 0x81, 0x02, /* Input (Variable), */
584 0x05, 0x01, /* Usage Page (Desktop), */
585 0x19, 0x40, /* Usage Minimum (40h), */
586 0x29, 0x42, /* Usage Maximum (42h), */
587 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
588 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
589 0x75, 0x10, /* Report Size (16), */
590 0x95, 0x03, /* Report Count (3), */
591 0x81, 0x02, /* Input (Variable), */
592 0x19, 0x43, /* Usage Minimum (43h), */
593 0x29, 0x45, /* Usage Maximum (45h), */
Frank Praznikfb291cb2014-09-14 11:56:38 -0400594 0x16, 0x00, 0xE0, /* Logical Minimum (-8192), */
595 0x26, 0xFF, 0x1F, /* Logical Maximum (8191), */
Frank Praznikd8296742014-02-05 20:03:45 -0500596 0x95, 0x03, /* Report Count (3), */
597 0x81, 0x02, /* Input (Variable), */
598 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
599 0x09, 0x20, /* Usage (20h), */
600 0x15, 0x00, /* Logical Minimum (0), */
601 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
602 0x75, 0x08, /* Report Size (8), */
603 0x95, 0x31, /* Report Count (51), */
604 0x81, 0x02, /* Input (Variable), */
605 0x09, 0x21, /* Usage (21h), */
606 0x75, 0x08, /* Report Size (8), */
607 0x95, 0x4D, /* Report Count (77), */
608 0x91, 0x02, /* Output (Variable), */
609 0x85, 0x12, /* Report ID (18), */
610 0x09, 0x22, /* Usage (22h), */
611 0x95, 0x8D, /* Report Count (141), */
612 0x81, 0x02, /* Input (Variable), */
613 0x09, 0x23, /* Usage (23h), */
614 0x91, 0x02, /* Output (Variable), */
615 0x85, 0x13, /* Report ID (19), */
616 0x09, 0x24, /* Usage (24h), */
617 0x95, 0xCD, /* Report Count (205), */
618 0x81, 0x02, /* Input (Variable), */
619 0x09, 0x25, /* Usage (25h), */
620 0x91, 0x02, /* Output (Variable), */
621 0x85, 0x14, /* Report ID (20), */
622 0x09, 0x26, /* Usage (26h), */
623 0x96, 0x0D, 0x01, /* Report Count (269), */
624 0x81, 0x02, /* Input (Variable), */
625 0x09, 0x27, /* Usage (27h), */
626 0x91, 0x02, /* Output (Variable), */
627 0x85, 0x15, /* Report ID (21), */
628 0x09, 0x28, /* Usage (28h), */
629 0x96, 0x4D, 0x01, /* Report Count (333), */
630 0x81, 0x02, /* Input (Variable), */
631 0x09, 0x29, /* Usage (29h), */
632 0x91, 0x02, /* Output (Variable), */
633 0x85, 0x16, /* Report ID (22), */
634 0x09, 0x2A, /* Usage (2Ah), */
635 0x96, 0x8D, 0x01, /* Report Count (397), */
636 0x81, 0x02, /* Input (Variable), */
637 0x09, 0x2B, /* Usage (2Bh), */
638 0x91, 0x02, /* Output (Variable), */
639 0x85, 0x17, /* Report ID (23), */
640 0x09, 0x2C, /* Usage (2Ch), */
641 0x96, 0xCD, 0x01, /* Report Count (461), */
642 0x81, 0x02, /* Input (Variable), */
643 0x09, 0x2D, /* Usage (2Dh), */
644 0x91, 0x02, /* Output (Variable), */
645 0x85, 0x18, /* Report ID (24), */
646 0x09, 0x2E, /* Usage (2Eh), */
647 0x96, 0x0D, 0x02, /* Report Count (525), */
648 0x81, 0x02, /* Input (Variable), */
649 0x09, 0x2F, /* Usage (2Fh), */
650 0x91, 0x02, /* Output (Variable), */
651 0x85, 0x19, /* Report ID (25), */
652 0x09, 0x30, /* Usage (30h), */
653 0x96, 0x22, 0x02, /* Report Count (546), */
654 0x81, 0x02, /* Input (Variable), */
655 0x09, 0x31, /* Usage (31h), */
656 0x91, 0x02, /* Output (Variable), */
657 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
658 0x85, 0x82, /* Report ID (130), */
659 0x09, 0x22, /* Usage (22h), */
660 0x95, 0x3F, /* Report Count (63), */
661 0xB1, 0x02, /* Feature (Variable), */
662 0x85, 0x83, /* Report ID (131), */
663 0x09, 0x23, /* Usage (23h), */
664 0xB1, 0x02, /* Feature (Variable), */
665 0x85, 0x84, /* Report ID (132), */
666 0x09, 0x24, /* Usage (24h), */
667 0xB1, 0x02, /* Feature (Variable), */
668 0x85, 0x90, /* Report ID (144), */
669 0x09, 0x30, /* Usage (30h), */
670 0xB1, 0x02, /* Feature (Variable), */
671 0x85, 0x91, /* Report ID (145), */
672 0x09, 0x31, /* Usage (31h), */
673 0xB1, 0x02, /* Feature (Variable), */
674 0x85, 0x92, /* Report ID (146), */
675 0x09, 0x32, /* Usage (32h), */
676 0xB1, 0x02, /* Feature (Variable), */
677 0x85, 0x93, /* Report ID (147), */
678 0x09, 0x33, /* Usage (33h), */
679 0xB1, 0x02, /* Feature (Variable), */
680 0x85, 0xA0, /* Report ID (160), */
681 0x09, 0x40, /* Usage (40h), */
682 0xB1, 0x02, /* Feature (Variable), */
683 0x85, 0xA4, /* Report ID (164), */
684 0x09, 0x44, /* Usage (44h), */
685 0xB1, 0x02, /* Feature (Variable), */
686 0xC0 /* End Collection */
687};
688
Jiri Kosina078328d2013-06-13 12:03:49 +0200689static __u8 ps3remote_rdesc[] = {
690 0x05, 0x01, /* GUsagePage Generic Desktop */
691 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
692 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
693
694 /* Use collection 1 for joypad buttons */
695 0xA1, 0x02, /* MCollection Logical (interrelated data) */
696
697 /* Ignore the 1st byte, maybe it is used for a controller
698 * number but it's not needed for correct operation */
699 0x75, 0x08, /* GReportSize 0x08 [8] */
700 0x95, 0x01, /* GReportCount 0x01 [1] */
701 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
702
703 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
704 * buttons multiple keypresses are allowed */
705 0x05, 0x09, /* GUsagePage Button */
706 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
707 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
708 0x14, /* GLogicalMinimum [0] */
709 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
710 0x75, 0x01, /* GReportSize 0x01 [1] */
711 0x95, 0x18, /* GReportCount 0x18 [24] */
712 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
713
714 0xC0, /* MEndCollection */
715
716 /* Use collection 2 for remote control buttons */
717 0xA1, 0x02, /* MCollection Logical (interrelated data) */
718
719 /* 5th byte is used for remote control buttons */
720 0x05, 0x09, /* GUsagePage Button */
721 0x18, /* LUsageMinimum [No button pressed] */
722 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
723 0x14, /* GLogicalMinimum [0] */
724 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
725 0x75, 0x08, /* GReportSize 0x08 [8] */
726 0x95, 0x01, /* GReportCount 0x01 [1] */
727 0x80, /* MInput */
728
729 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
730 * 0xff and 11th is for press indication */
731 0x75, 0x08, /* GReportSize 0x08 [8] */
732 0x95, 0x06, /* GReportCount 0x06 [6] */
733 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
734
735 /* 12th byte is for battery strength */
736 0x05, 0x06, /* GUsagePage Generic Device Controls */
737 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
738 0x14, /* GLogicalMinimum [0] */
739 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
740 0x75, 0x08, /* GReportSize 0x08 [8] */
741 0x95, 0x01, /* GReportCount 0x01 [1] */
742 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
743
744 0xC0, /* MEndCollection */
745
746 0xC0 /* MEndCollection [Game Pad] */
747};
748
749static const unsigned int ps3remote_keymap_joypad_buttons[] = {
750 [0x01] = KEY_SELECT,
751 [0x02] = BTN_THUMBL, /* L3 */
752 [0x03] = BTN_THUMBR, /* R3 */
753 [0x04] = BTN_START,
754 [0x05] = KEY_UP,
755 [0x06] = KEY_RIGHT,
756 [0x07] = KEY_DOWN,
757 [0x08] = KEY_LEFT,
758 [0x09] = BTN_TL2, /* L2 */
759 [0x0a] = BTN_TR2, /* R2 */
760 [0x0b] = BTN_TL, /* L1 */
761 [0x0c] = BTN_TR, /* R1 */
762 [0x0d] = KEY_OPTION, /* options/triangle */
763 [0x0e] = KEY_BACK, /* back/circle */
764 [0x0f] = BTN_0, /* cross */
765 [0x10] = KEY_SCREEN, /* view/square */
766 [0x11] = KEY_HOMEPAGE, /* PS button */
767 [0x14] = KEY_ENTER,
768};
769static const unsigned int ps3remote_keymap_remote_buttons[] = {
770 [0x00] = KEY_1,
771 [0x01] = KEY_2,
772 [0x02] = KEY_3,
773 [0x03] = KEY_4,
774 [0x04] = KEY_5,
775 [0x05] = KEY_6,
776 [0x06] = KEY_7,
777 [0x07] = KEY_8,
778 [0x08] = KEY_9,
779 [0x09] = KEY_0,
780 [0x0e] = KEY_ESC, /* return */
781 [0x0f] = KEY_CLEAR,
782 [0x16] = KEY_EJECTCD,
783 [0x1a] = KEY_MENU, /* top menu */
784 [0x28] = KEY_TIME,
785 [0x30] = KEY_PREVIOUS,
786 [0x31] = KEY_NEXT,
787 [0x32] = KEY_PLAY,
788 [0x33] = KEY_REWIND, /* scan back */
789 [0x34] = KEY_FORWARD, /* scan forward */
790 [0x38] = KEY_STOP,
791 [0x39] = KEY_PAUSE,
792 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
793 [0x60] = KEY_FRAMEBACK, /* slow/step back */
794 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
795 [0x63] = KEY_SUBTITLE,
796 [0x64] = KEY_AUDIO,
797 [0x65] = KEY_ANGLE,
798 [0x70] = KEY_INFO, /* display */
799 [0x80] = KEY_BLUE,
800 [0x81] = KEY_RED,
801 [0x82] = KEY_GREEN,
802 [0x83] = KEY_YELLOW,
803};
804
Colin Leitnerf04d5142013-05-27 23:41:05 +0200805static const unsigned int buzz_keymap[] = {
Frank Praznikad142b92014-02-20 11:36:00 -0500806 /*
807 * The controller has 4 remote buzzers, each with one LED and 5
Colin Leitnerf04d5142013-05-27 23:41:05 +0200808 * buttons.
809 *
810 * We use the mapping chosen by the controller, which is:
811 *
812 * Key Offset
813 * -------------------
814 * Buzz 1
815 * Blue 5
816 * Orange 4
817 * Green 3
818 * Yellow 2
819 *
820 * So, for example, the orange button on the third buzzer is mapped to
821 * BTN_TRIGGER_HAPPY14
822 */
823 [ 1] = BTN_TRIGGER_HAPPY1,
824 [ 2] = BTN_TRIGGER_HAPPY2,
825 [ 3] = BTN_TRIGGER_HAPPY3,
826 [ 4] = BTN_TRIGGER_HAPPY4,
827 [ 5] = BTN_TRIGGER_HAPPY5,
828 [ 6] = BTN_TRIGGER_HAPPY6,
829 [ 7] = BTN_TRIGGER_HAPPY7,
830 [ 8] = BTN_TRIGGER_HAPPY8,
831 [ 9] = BTN_TRIGGER_HAPPY9,
832 [10] = BTN_TRIGGER_HAPPY10,
833 [11] = BTN_TRIGGER_HAPPY11,
834 [12] = BTN_TRIGGER_HAPPY12,
835 [13] = BTN_TRIGGER_HAPPY13,
836 [14] = BTN_TRIGGER_HAPPY14,
837 [15] = BTN_TRIGGER_HAPPY15,
838 [16] = BTN_TRIGGER_HAPPY16,
839 [17] = BTN_TRIGGER_HAPPY17,
840 [18] = BTN_TRIGGER_HAPPY18,
841 [19] = BTN_TRIGGER_HAPPY19,
842 [20] = BTN_TRIGGER_HAPPY20,
843};
844
Frank Praznikd902f472014-01-27 10:17:36 -0500845static enum power_supply_property sony_battery_props[] = {
846 POWER_SUPPLY_PROP_PRESENT,
847 POWER_SUPPLY_PROP_CAPACITY,
848 POWER_SUPPLY_PROP_SCOPE,
849 POWER_SUPPLY_PROP_STATUS,
850};
851
Frank Praznik55d3b662014-04-14 10:11:32 -0400852struct sixaxis_led {
853 __u8 time_enabled; /* the total time the led is active (0xff means forever) */
854 __u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
855 __u8 enabled;
856 __u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
857 __u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
858} __packed;
859
860struct sixaxis_rumble {
861 __u8 padding;
862 __u8 right_duration; /* Right motor duration (0xff means forever) */
863 __u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
864 __u8 left_duration; /* Left motor duration (0xff means forever) */
865 __u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
866} __packed;
867
868struct sixaxis_output_report {
869 __u8 report_id;
870 struct sixaxis_rumble rumble;
871 __u8 padding[4];
872 __u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
873 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
874 struct sixaxis_led _reserved; /* LED5, not actually soldered */
875} __packed;
876
877union sixaxis_output_report_01 {
878 struct sixaxis_output_report data;
879 __u8 buf[36];
880};
881
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400882struct motion_output_report_02 {
883 u8 type, zero;
884 u8 r, g, b;
885 u8 zero2;
886 u8 rumble;
887};
888
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500889#define DS4_REPORT_0x02_SIZE 37
890#define DS4_REPORT_0x05_SIZE 32
891#define DS4_REPORT_0x11_SIZE 78
892#define DS4_REPORT_0x81_SIZE 7
Antonio Ospite29b691a2015-02-16 18:12:21 +0100893#define SIXAXIS_REPORT_0xF2_SIZE 17
Antonio Ospitea85d67b2015-02-16 18:12:22 +0100894#define SIXAXIS_REPORT_0xF5_SIZE 8
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500895
Jiri Kosina8b402c92015-02-23 11:15:44 +0100896static DEFINE_SPINLOCK(sony_dev_list_lock);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500897static LIST_HEAD(sony_device_list);
Frank Praznik80250872014-04-14 10:11:35 -0400898static DEFINE_IDA(sony_device_id_allocator);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500899
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200900struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500901 spinlock_t lock;
Frank Praznikd2d782f2014-02-20 11:36:03 -0500902 struct list_head list_node;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100903 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500904 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200905 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100906 struct work_struct state_worker;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100907 struct power_supply *battery;
908 struct power_supply_desc battery_desc;
Frank Praznik80250872014-04-14 10:11:35 -0400909 int device_id;
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500910 __u8 *output_report_dmabuf;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200911
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100912#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100913 __u8 left;
914 __u8 right;
915#endif
916
Frank Praznikd2d782f2014-02-20 11:36:03 -0500917 __u8 mac_address[6];
Frank Praznik5f5750d2014-02-19 13:09:22 -0500918 __u8 worker_initialized;
Frank Praznikd902f472014-01-27 10:17:36 -0500919 __u8 cable_state;
920 __u8 battery_charging;
921 __u8 battery_capacity;
Frank Praznik60781cf2014-01-11 15:13:15 -0500922 __u8 led_state[MAX_LEDS];
Frank Praznikb3ed4582014-04-14 10:11:36 -0400923 __u8 led_delay_on[MAX_LEDS];
924 __u8 led_delay_off[MAX_LEDS];
Frank Praznik60781cf2014-01-11 15:13:15 -0500925 __u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200926};
927
Antonio Ospitec607fb82014-06-24 13:28:41 +0200928static __u8 *sixaxis_fixup(struct hid_device *hdev, __u8 *rdesc,
929 unsigned int *rsize)
930{
931 *rsize = sizeof(sixaxis_rdesc);
932 return sixaxis_rdesc;
933}
934
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400935static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
936 unsigned int *rsize)
937{
938 *rsize = sizeof(motion_rdesc);
939 return motion_rdesc;
940}
941
Jiri Kosina078328d2013-06-13 12:03:49 +0200942static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
943 unsigned int *rsize)
944{
945 *rsize = sizeof(ps3remote_rdesc);
946 return ps3remote_rdesc;
947}
948
949static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
950 struct hid_field *field, struct hid_usage *usage,
951 unsigned long **bit, int *max)
952{
953 unsigned int key = usage->hid & HID_USAGE;
954
955 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
956 return -1;
957
958 switch (usage->collection_index) {
959 case 1:
960 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
961 return -1;
962
963 key = ps3remote_keymap_joypad_buttons[key];
964 if (!key)
965 return -1;
966 break;
967 case 2:
968 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
969 return -1;
970
971 key = ps3remote_keymap_remote_buttons[key];
972 if (!key)
973 return -1;
974 break;
975 default:
976 return -1;
977 }
978
979 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
980 return 1;
981}
982
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400983static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
984 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200985{
986 struct sony_sc *sc = hid_get_drvdata(hdev);
987
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900988 /*
989 * Some Sony RF receivers wrongly declare the mouse pointer as a
990 * a constant non-data variable.
991 */
992 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
993 /* usage page: generic desktop controls */
994 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
995 /* usage: mouse */
996 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
997 /* input (usage page for x,y axes): constant, variable, relative */
998 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +0900999 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +09001000 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001001 rdesc[55] = 0x06;
1002 }
Simon Wood61ab44b2011-06-10 12:00:26 +02001003
Frank Prazniked19d8c2014-01-16 21:43:12 -05001004 /*
1005 * The default Dualshock 4 USB descriptor doesn't assign
1006 * the gyroscope values to corresponding axes so we need a
1007 * modified one.
1008 */
1009 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
1010 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
1011 rdesc = dualshock4_usb_rdesc;
1012 *rsize = sizeof(dualshock4_usb_rdesc);
Frank Praznikd8296742014-02-05 20:03:45 -05001013 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
1014 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
1015 rdesc = dualshock4_bt_rdesc;
1016 *rsize = sizeof(dualshock4_bt_rdesc);
Frank Prazniked19d8c2014-01-16 21:43:12 -05001017 }
1018
Antonio Ospitec607fb82014-06-24 13:28:41 +02001019 if (sc->quirks & SIXAXIS_CONTROLLER)
1020 return sixaxis_fixup(hdev, rdesc, rsize);
Jiri Kosina078328d2013-06-13 12:03:49 +02001021
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001022 if (sc->quirks & MOTION_CONTROLLER)
1023 return motion_fixup(hdev, rdesc, rsize);
1024
Jiri Kosina078328d2013-06-13 12:03:49 +02001025 if (sc->quirks & PS3REMOTE)
1026 return ps3remote_fixup(hdev, rdesc, rsize);
1027
Nikolai Kondrashov73e40082010-08-06 23:03:06 +04001028 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001029}
1030
Frank Praznikd902f472014-01-27 10:17:36 -05001031static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1032{
1033 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
1034 unsigned long flags;
1035 __u8 cable_state, battery_capacity, battery_charging;
1036
Frank Praznikad142b92014-02-20 11:36:00 -05001037 /*
1038 * The sixaxis is charging if the battery value is 0xee
Frank Praznikd902f472014-01-27 10:17:36 -05001039 * and it is fully charged if the value is 0xef.
1040 * It does not report the actual level while charging so it
1041 * is set to 100% while charging is in progress.
1042 */
1043 if (rd[30] >= 0xee) {
1044 battery_capacity = 100;
Frank Praznika43e94a2014-02-15 13:35:42 -05001045 battery_charging = !(rd[30] & 0x01);
Frank Praznik9fddd742014-08-29 13:11:52 -04001046 cable_state = 1;
Frank Praznikd902f472014-01-27 10:17:36 -05001047 } else {
Frank Praznikac3c9a92014-02-20 11:36:02 -05001048 __u8 index = rd[30] <= 5 ? rd[30] : 5;
1049 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -05001050 battery_charging = 0;
Frank Praznik9fddd742014-08-29 13:11:52 -04001051 cable_state = 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001052 }
Frank Praznikd902f472014-01-27 10:17:36 -05001053
1054 spin_lock_irqsave(&sc->lock, flags);
1055 sc->cable_state = cable_state;
1056 sc->battery_capacity = battery_capacity;
1057 sc->battery_charging = battery_charging;
1058 spin_unlock_irqrestore(&sc->lock, flags);
1059}
1060
1061static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
1062{
Frank Praznike5606232014-01-27 10:17:37 -05001063 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1064 struct hid_input, list);
1065 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -05001066 unsigned long flags;
Frank Praznik6c5f8602014-02-05 20:03:47 -05001067 int n, offset;
Frank Praznikd902f472014-01-27 10:17:36 -05001068 __u8 cable_state, battery_capacity, battery_charging;
1069
Frank Praznikad142b92014-02-20 11:36:00 -05001070 /*
1071 * Battery and touchpad data starts at byte 30 in the USB report and
Frank Praznik6c5f8602014-02-05 20:03:47 -05001072 * 32 in Bluetooth report.
1073 */
1074 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
1075
Frank Praznikad142b92014-02-20 11:36:00 -05001076 /*
1077 * The lower 4 bits of byte 30 contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -05001078 * and the 5th bit contains the USB cable state.
1079 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001080 cable_state = (rd[offset] >> 4) & 0x01;
1081 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -05001082
Frank Praznikad142b92014-02-20 11:36:00 -05001083 /*
1084 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -05001085 * 0 to 10, and when running on battery power it ranges from 0 to 9.
1086 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -05001087 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001088 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -05001089 battery_charging = 0;
1090 else
1091 battery_charging = 1;
1092
Frank Praznik6c5f8602014-02-05 20:03:47 -05001093 if (!cable_state)
1094 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -05001095 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -05001096 battery_capacity = 10;
1097
Frank Praznikd902f472014-01-27 10:17:36 -05001098 battery_capacity *= 10;
1099
1100 spin_lock_irqsave(&sc->lock, flags);
1101 sc->cable_state = cable_state;
1102 sc->battery_capacity = battery_capacity;
1103 sc->battery_charging = battery_charging;
1104 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -05001105
Frank Praznik6c5f8602014-02-05 20:03:47 -05001106 offset += 5;
1107
Frank Praznikad142b92014-02-20 11:36:00 -05001108 /*
1109 * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
Frank Praznik6c5f8602014-02-05 20:03:47 -05001110 * and 37 on Bluetooth.
Frank Praznike5606232014-01-27 10:17:37 -05001111 * The first 7 bits of the first byte is a counter and bit 8 is a touch
1112 * indicator that is 0 when pressed and 1 when not pressed.
1113 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1114 * The data for the second touch is in the same format and immediatly
1115 * follows the data for the first.
1116 */
1117 for (n = 0; n < 2; n++) {
1118 __u16 x, y;
1119
1120 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1121 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1122
1123 input_mt_slot(input_dev, n);
1124 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
1125 !(rd[offset] >> 7));
1126 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
1127 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
1128
1129 offset += 4;
1130 }
Frank Praznikd902f472014-01-27 10:17:36 -05001131}
1132
Simon Woodc9e4d872011-06-10 12:00:27 +02001133static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
1134 __u8 *rd, int size)
1135{
1136 struct sony_sc *sc = hid_get_drvdata(hdev);
1137
Frank Praznikad142b92014-02-20 11:36:00 -05001138 /*
1139 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +02001140 * has to be BYTE_SWAPPED before passing up to joystick interface
1141 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001142 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Simon Woodc9e4d872011-06-10 12:00:27 +02001143 swap(rd[41], rd[42]);
1144 swap(rd[43], rd[44]);
1145 swap(rd[45], rd[46]);
1146 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -05001147
1148 sixaxis_parse_report(sc, rd, size);
Frank Praznik68330d82014-02-05 20:03:49 -05001149 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1150 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1151 && rd[0] == 0x11 && size == 78)) {
Frank Praznikd902f472014-01-27 10:17:36 -05001152 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +02001153 }
1154
1155 return 0;
1156}
1157
Colin Leitnerf04d5142013-05-27 23:41:05 +02001158static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1159 struct hid_field *field, struct hid_usage *usage,
1160 unsigned long **bit, int *max)
1161{
1162 struct sony_sc *sc = hid_get_drvdata(hdev);
1163
1164 if (sc->quirks & BUZZ_CONTROLLER) {
1165 unsigned int key = usage->hid & HID_USAGE;
1166
1167 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1168 return -1;
1169
1170 switch (usage->collection_index) {
1171 case 1:
1172 if (key >= ARRAY_SIZE(buzz_keymap))
1173 return -1;
1174
1175 key = buzz_keymap[key];
1176 if (!key)
1177 return -1;
1178 break;
1179 default:
1180 return -1;
1181 }
1182
1183 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1184 return 1;
1185 }
1186
Jiri Kosina078328d2013-06-13 12:03:49 +02001187 if (sc->quirks & PS3REMOTE)
1188 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1189
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001190 /* Let hid-core decide for the others */
1191 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001192}
1193
Frank Praznikce8efc32014-09-18 21:15:01 -04001194static int sony_register_touchpad(struct hid_input *hi, int touch_count,
1195 int w, int h)
1196{
1197 struct input_dev *input_dev = hi->input;
1198 int ret;
1199
1200 ret = input_mt_init_slots(input_dev, touch_count, 0);
1201 if (ret < 0)
1202 return ret;
1203
1204 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1205 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1206
1207 return 0;
1208}
1209
1210static void sony_input_configured(struct hid_device *hdev,
1211 struct hid_input *hidinput)
1212{
1213 struct sony_sc *sc = hid_get_drvdata(hdev);
1214
1215 /*
1216 * The Dualshock 4 touchpad supports 2 touches and has a
Frank Praznik981c5b42014-09-24 09:38:19 -04001217 * resolution of 1920x942 (44.86 dots/mm).
Frank Praznikce8efc32014-09-18 21:15:01 -04001218 */
1219 if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik981c5b42014-09-24 09:38:19 -04001220 if (sony_register_touchpad(hidinput, 2, 1920, 942) != 0)
Frank Praznikce8efc32014-09-18 21:15:01 -04001221 hid_err(sc->hdev,
1222 "Unable to initialize multi-touch slots\n");
1223 }
1224}
1225
Antonio Ospite5710fabf2011-02-20 18:26:45 +01001226/*
Jiri Slabybd28ce02008-06-25 23:47:04 +02001227 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1228 * to "operational". Without this, the ps3 controller will not report any
1229 * events.
1230 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001231static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001232{
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001233 const int buf_size =
1234 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
Antonio Ospite2e701a32015-02-16 18:12:24 +01001235 __u8 *buf;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001236 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001237
Antonio Ospite2e701a32015-02-16 18:12:24 +01001238 buf = kmalloc(buf_size, GFP_KERNEL);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001239 if (!buf)
1240 return -ENOMEM;
1241
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001242 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1243 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001244 if (ret < 0) {
1245 hid_err(hdev, "can't set operational mode: step 1\n");
1246 goto out;
1247 }
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001248
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001249 /*
1250 * Some compatible controllers like the Speedlink Strike FX and
1251 * Gasia need another query plus an USB interrupt to get operational.
1252 */
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001253 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1254 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001255 if (ret < 0) {
1256 hid_err(hdev, "can't set operational mode: step 2\n");
1257 goto out;
1258 }
1259
1260 ret = hid_hw_output_report(hdev, buf, 1);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001261 if (ret < 0)
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001262 hid_err(hdev, "can't set operational mode: step 3\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +02001263
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001264out:
Jiri Slabybd28ce02008-06-25 23:47:04 +02001265 kfree(buf);
1266
1267 return ret;
1268}
1269
Antonio Ospite816651a2010-05-03 22:15:55 +02001270static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001271{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001272 static const __u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1273 __u8 *buf;
1274 int ret;
1275
1276 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1277 if (!buf)
1278 return -ENOMEM;
1279
1280 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
Benjamin Tissoiresb0dd72a2014-02-10 12:58:54 -05001281 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001282
1283 kfree(buf);
1284
1285 return ret;
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001286}
1287
Frank Praznikad142b92014-02-20 11:36:00 -05001288/*
1289 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
Frank Praznik68330d82014-02-05 20:03:49 -05001290 * controller so that it sends full input reports of type 0x11.
1291 */
1292static int dualshock4_set_operational_bt(struct hid_device *hdev)
1293{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001294 __u8 *buf;
1295 int ret;
Frank Praznik68330d82014-02-05 20:03:49 -05001296
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001297 buf = kmalloc(DS4_REPORT_0x02_SIZE, GFP_KERNEL);
1298 if (!buf)
1299 return -ENOMEM;
1300
1301 ret = hid_hw_raw_request(hdev, 0x02, buf, DS4_REPORT_0x02_SIZE,
Frank Praznik68330d82014-02-05 20:03:49 -05001302 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001303
1304 kfree(buf);
1305
1306 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001307}
1308
Frank Praznik221399b2015-05-05 20:47:32 -04001309static void sixaxis_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001310{
1311 static const __u8 sixaxis_leds[10][4] = {
1312 { 0x01, 0x00, 0x00, 0x00 },
1313 { 0x00, 0x01, 0x00, 0x00 },
1314 { 0x00, 0x00, 0x01, 0x00 },
1315 { 0x00, 0x00, 0x00, 0x01 },
1316 { 0x01, 0x00, 0x00, 0x01 },
1317 { 0x00, 0x01, 0x00, 0x01 },
1318 { 0x00, 0x00, 0x01, 0x01 },
1319 { 0x01, 0x00, 0x01, 0x01 },
1320 { 0x00, 0x01, 0x01, 0x01 },
1321 { 0x01, 0x01, 0x01, 0x01 }
1322 };
1323
Frank Praznik221399b2015-05-05 20:47:32 -04001324 int id = sc->device_id;
1325
1326 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001327
1328 if (id < 0)
1329 return;
1330
1331 id %= 10;
Frank Praznik221399b2015-05-05 20:47:32 -04001332 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001333}
1334
Frank Praznik221399b2015-05-05 20:47:32 -04001335static void dualshock4_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001336{
1337 /* The first 4 color/index entries match what the PS4 assigns */
1338 static const __u8 color_code[7][3] = {
1339 /* Blue */ { 0x00, 0x00, 0x01 },
1340 /* Red */ { 0x01, 0x00, 0x00 },
1341 /* Green */ { 0x00, 0x01, 0x00 },
1342 /* Pink */ { 0x02, 0x00, 0x01 },
1343 /* Orange */ { 0x02, 0x01, 0x00 },
1344 /* Teal */ { 0x00, 0x01, 0x01 },
1345 /* White */ { 0x01, 0x01, 0x01 }
1346 };
1347
Frank Praznik221399b2015-05-05 20:47:32 -04001348 int id = sc->device_id;
1349
1350 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001351
1352 if (id < 0)
1353 return;
1354
1355 id %= 7;
Frank Praznik221399b2015-05-05 20:47:32 -04001356 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001357}
1358
Frank Praznik221399b2015-05-05 20:47:32 -04001359static void buzz_set_leds(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001360{
Frank Praznik221399b2015-05-05 20:47:32 -04001361 struct hid_device *hdev = sc->hdev;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001362 struct list_head *report_list =
1363 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1364 struct hid_report *report = list_entry(report_list->next,
1365 struct hid_report, list);
1366 __s32 *value = report->field[0]->value;
1367
Frank Praznik221399b2015-05-05 20:47:32 -04001368 BUILD_BUG_ON(MAX_LEDS < 4);
1369
Colin Leitnerf04d5142013-05-27 23:41:05 +02001370 value[0] = 0x00;
Frank Praznik221399b2015-05-05 20:47:32 -04001371 value[1] = sc->led_state[0] ? 0xff : 0x00;
1372 value[2] = sc->led_state[1] ? 0xff : 0x00;
1373 value[3] = sc->led_state[2] ? 0xff : 0x00;
1374 value[4] = sc->led_state[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001375 value[5] = 0x00;
1376 value[6] = 0x00;
1377 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1378}
1379
Frank Praznik221399b2015-05-05 20:47:32 -04001380static void sony_set_leds(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001381{
Frank Praznik221399b2015-05-05 20:47:32 -04001382 if (!(sc->quirks & BUZZ_CONTROLLER))
Frank Praznikfa57a812014-04-14 10:11:33 -04001383 schedule_work(&sc->state_worker);
Frank Praznik221399b2015-05-05 20:47:32 -04001384 else
1385 buzz_set_leds(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001386}
1387
Sven Eckelmannc5382512013-11-19 20:26:30 +01001388static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001389 enum led_brightness value)
1390{
1391 struct device *dev = led->dev->parent;
1392 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1393 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001394
1395 int n;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001396 int force_update;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001397
1398 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001399 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001400 hid_err(hdev, "No device data\n");
1401 return;
1402 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001403
Frank Praznikb3ed4582014-04-14 10:11:36 -04001404 /*
1405 * The Sixaxis on USB will override any LED settings sent to it
1406 * and keep flashing all of the LEDs until the PS button is pressed.
1407 * Updates, even if redundant, must be always be sent to the
1408 * controller to avoid having to toggle the state of an LED just to
1409 * stop the flashing later on.
1410 */
1411 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1412
Frank Praznik60781cf2014-01-11 15:13:15 -05001413 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznikb3ed4582014-04-14 10:11:36 -04001414 if (led == drv_data->leds[n] && (force_update ||
1415 (value != drv_data->led_state[n] ||
1416 drv_data->led_delay_on[n] ||
1417 drv_data->led_delay_off[n]))) {
1418
1419 drv_data->led_state[n] = value;
1420
1421 /* Setting the brightness stops the blinking */
1422 drv_data->led_delay_on[n] = 0;
1423 drv_data->led_delay_off[n] = 0;
1424
Frank Praznik221399b2015-05-05 20:47:32 -04001425 sony_set_leds(drv_data);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001426 break;
1427 }
1428 }
1429}
1430
Sven Eckelmannc5382512013-11-19 20:26:30 +01001431static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001432{
1433 struct device *dev = led->dev->parent;
1434 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1435 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001436
1437 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001438
1439 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001440 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001441 hid_err(hdev, "No device data\n");
1442 return LED_OFF;
1443 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001444
Frank Praznik60781cf2014-01-11 15:13:15 -05001445 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001446 if (led == drv_data->leds[n])
1447 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001448 }
1449
Simon Wood7db75042014-02-05 12:34:18 -07001450 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001451}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001452
Frank Praznikb3ed4582014-04-14 10:11:36 -04001453static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1454 unsigned long *delay_off)
1455{
1456 struct device *dev = led->dev->parent;
1457 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1458 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1459 int n;
1460 __u8 new_on, new_off;
1461
1462 if (!drv_data) {
1463 hid_err(hdev, "No device data\n");
1464 return -EINVAL;
1465 }
1466
1467 /* Max delay is 255 deciseconds or 2550 milliseconds */
1468 if (*delay_on > 2550)
1469 *delay_on = 2550;
1470 if (*delay_off > 2550)
1471 *delay_off = 2550;
1472
1473 /* Blink at 1 Hz if both values are zero */
1474 if (!*delay_on && !*delay_off)
1475 *delay_on = *delay_off = 500;
1476
1477 new_on = *delay_on / 10;
1478 new_off = *delay_off / 10;
1479
1480 for (n = 0; n < drv_data->led_count; n++) {
1481 if (led == drv_data->leds[n])
1482 break;
1483 }
1484
1485 /* This LED is not registered on this device */
1486 if (n >= drv_data->led_count)
1487 return -EINVAL;
1488
1489 /* Don't schedule work if the values didn't change */
1490 if (new_on != drv_data->led_delay_on[n] ||
1491 new_off != drv_data->led_delay_off[n]) {
1492 drv_data->led_delay_on[n] = new_on;
1493 drv_data->led_delay_off[n] = new_off;
1494 schedule_work(&drv_data->state_worker);
1495 }
1496
1497 return 0;
1498}
1499
Frank Praznikfa57a812014-04-14 10:11:33 -04001500static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001501{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001502 struct led_classdev *led;
1503 int n;
1504
Frank Praznikfa57a812014-04-14 10:11:33 -04001505 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001506
Frank Praznikfa57a812014-04-14 10:11:33 -04001507 for (n = 0; n < sc->led_count; n++) {
1508 led = sc->leds[n];
1509 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001510 if (!led)
1511 continue;
1512 led_classdev_unregister(led);
1513 kfree(led);
1514 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001515
Frank Praznikfa57a812014-04-14 10:11:33 -04001516 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001517}
1518
Frank Praznikfa57a812014-04-14 10:11:33 -04001519static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001520{
Frank Praznikfa57a812014-04-14 10:11:33 -04001521 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001522 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001523 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001524 struct led_classdev *led;
1525 size_t name_sz;
1526 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001527 size_t name_len;
1528 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001529 static const char * const ds4_name_str[] = { "red", "green", "blue",
1530 "global" };
Frank Praznik5607c892014-07-29 19:55:48 -07001531 __u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
Frank Praznikb3ed4582014-04-14 10:11:36 -04001532 __u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001533
Frank Praznikfa57a812014-04-14 10:11:33 -04001534 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001535
Frank Praznikfa57a812014-04-14 10:11:33 -04001536 if (sc->quirks & BUZZ_CONTROLLER) {
1537 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001538 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001539 name_len = strlen("::buzz#");
1540 name_fmt = "%s::buzz%d";
1541 /* Validate expected report characteristics. */
1542 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1543 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001544 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik221399b2015-05-05 20:47:32 -04001545 dualshock4_set_leds_from_id(sc);
1546 sc->led_state[3] = 1;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001547 sc->led_count = 4;
1548 memset(max_brightness, 255, 3);
1549 use_hw_blink[3] = 1;
1550 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001551 name_len = 0;
1552 name_fmt = "%s:%s";
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001553 } else if (sc->quirks & MOTION_CONTROLLER) {
1554 sc->led_count = 3;
1555 memset(max_brightness, 255, 3);
1556 use_ds4_names = 1;
1557 name_len = 0;
1558 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -05001559 } else {
Frank Praznik221399b2015-05-05 20:47:32 -04001560 sixaxis_set_leds_from_id(sc);
Frank Praznikfa57a812014-04-14 10:11:33 -04001561 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001562 memset(use_hw_blink, 1, 4);
1563 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001564 name_len = strlen("::sony#");
1565 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001566 }
1567
Frank Praznikad142b92014-02-20 11:36:00 -05001568 /*
1569 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001570 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001571 * LEDs to on
1572 */
Frank Praznik221399b2015-05-05 20:47:32 -04001573 sony_set_leds(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001574
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001575 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001576
Frank Praznikfa57a812014-04-14 10:11:33 -04001577 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001578
Frank Praznikb3ed4582014-04-14 10:11:36 -04001579 if (use_ds4_names)
1580 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001581
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001582 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1583 if (!led) {
1584 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001585 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001586 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001587 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001588
1589 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001590 if (use_ds4_names)
1591 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1592 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001593 else
1594 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001595 led->name = name;
Frank Praznik221399b2015-05-05 20:47:32 -04001596 led->brightness = sc->led_state[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001597 led->max_brightness = max_brightness[n];
Sven Eckelmannc5382512013-11-19 20:26:30 +01001598 led->brightness_get = sony_led_get_brightness;
1599 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001600
Frank Praznikb3ed4582014-04-14 10:11:36 -04001601 if (use_hw_blink[n])
1602 led->blink_set = sony_led_blink_set;
1603
Frank Praznik80250872014-04-14 10:11:35 -04001604 sc->leds[n] = led;
1605
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001606 ret = led_classdev_register(&hdev->dev, led);
1607 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001608 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001609 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001610 kfree(led);
1611 goto error_leds;
1612 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001613 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001614
1615 return ret;
1616
Colin Leitnerf04d5142013-05-27 23:41:05 +02001617error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001618 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001619
Colin Leitnerf04d5142013-05-27 23:41:05 +02001620 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001621}
1622
Frank Praznikcad665a2014-01-11 15:13:54 -05001623static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001624{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001625 static const union sixaxis_output_report_01 default_report = {
Frank Praznik55d3b662014-04-14 10:11:32 -04001626 .buf = {
1627 0x01,
1628 0x00, 0xff, 0x00, 0xff, 0x00,
1629 0x00, 0x00, 0x00, 0x00, 0x00,
1630 0xff, 0x27, 0x10, 0x00, 0x32,
1631 0xff, 0x27, 0x10, 0x00, 0x32,
1632 0xff, 0x27, 0x10, 0x00, 0x32,
1633 0xff, 0x27, 0x10, 0x00, 0x32,
1634 0x00, 0x00, 0x00, 0x00, 0x00
1635 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001636 };
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001637 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1638 struct sixaxis_output_report *report =
1639 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1640 int n;
1641
1642 /* Initialize the report with default values */
1643 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001644
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001645#ifdef CONFIG_SONY_FF
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001646 report->rumble.right_motor_on = sc->right ? 1 : 0;
1647 report->rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001648#endif
1649
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001650 report->leds_bitmap |= sc->led_state[0] << 1;
1651 report->leds_bitmap |= sc->led_state[1] << 2;
1652 report->leds_bitmap |= sc->led_state[2] << 3;
1653 report->leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001654
Simon Wood88f65762014-04-14 10:11:37 -04001655 /* Set flag for all leds off, required for 3rd party INTEC controller */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001656 if ((report->leds_bitmap & 0x1E) == 0)
1657 report->leds_bitmap |= 0x20;
Simon Wood88f65762014-04-14 10:11:37 -04001658
Frank Praznikb3ed4582014-04-14 10:11:36 -04001659 /*
1660 * The LEDs in the report are indexed in reverse order to their
1661 * corresponding light on the controller.
1662 * Index 0 = LED 4, index 1 = LED 3, etc...
1663 *
1664 * In the case of both delay values being zero (blinking disabled) the
1665 * default report values should be used or the controller LED will be
1666 * always off.
1667 */
1668 for (n = 0; n < 4; n++) {
1669 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001670 report->led[3 - n].duty_off = sc->led_delay_off[n];
1671 report->led[3 - n].duty_on = sc->led_delay_on[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001672 }
1673 }
1674
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001675 hid_hw_raw_request(sc->hdev, report->report_id, (__u8 *)report,
1676 sizeof(struct sixaxis_output_report),
1677 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001678}
1679
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001680static void dualshock4_state_worker(struct work_struct *work)
1681{
1682 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001683 struct hid_device *hdev = sc->hdev;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001684 __u8 *buf = sc->output_report_dmabuf;
Frank Praznik48220232014-02-05 20:03:44 -05001685 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001686
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001687 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001688 memset(buf, 0, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001689 buf[0] = 0x05;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001690 buf[1] = 0xFF;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001691 offset = 4;
1692 } else {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001693 memset(buf, 0, DS4_REPORT_0x11_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001694 buf[0] = 0x11;
1695 buf[1] = 0xB0;
1696 buf[3] = 0x0F;
1697 offset = 6;
1698 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001699
1700#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001701 buf[offset++] = sc->right;
1702 buf[offset++] = sc->left;
1703#else
1704 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001705#endif
1706
Frank Praznikb3ed4582014-04-14 10:11:36 -04001707 /* LED 3 is the global control */
1708 if (sc->led_state[3]) {
1709 buf[offset++] = sc->led_state[0];
1710 buf[offset++] = sc->led_state[1];
1711 buf[offset++] = sc->led_state[2];
1712 } else {
1713 offset += 3;
1714 }
1715
1716 /* If both delay values are zero the DualShock 4 disables blinking. */
1717 buf[offset++] = sc->led_delay_on[3];
1718 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05001719
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001720 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001721 hid_hw_output_report(hdev, buf, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001722 else
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001723 hid_hw_raw_request(hdev, 0x11, buf, DS4_REPORT_0x11_SIZE,
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001724 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001725}
1726
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001727static void motion_state_worker(struct work_struct *work)
1728{
1729 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1730 struct hid_device *hdev = sc->hdev;
1731 struct motion_output_report_02 *report =
1732 (struct motion_output_report_02 *)sc->output_report_dmabuf;
1733
1734 memset(report, 0, sizeof(struct motion_output_report_02));
1735
1736 report->type = 0x02; /* set leds */
1737 report->r = sc->led_state[0];
1738 report->g = sc->led_state[1];
1739 report->b = sc->led_state[2];
1740
1741#ifdef CONFIG_SONY_FF
1742 report->rumble = max(sc->right, sc->left);
1743#endif
1744
1745 hid_hw_output_report(hdev, (__u8 *)report,
1746 sizeof(struct motion_output_report_02));
1747}
1748
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001749static int sony_allocate_output_report(struct sony_sc *sc)
1750{
1751 if (sc->quirks & SIXAXIS_CONTROLLER)
1752 sc->output_report_dmabuf =
1753 kmalloc(sizeof(union sixaxis_output_report_01),
1754 GFP_KERNEL);
1755 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1756 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x11_SIZE,
1757 GFP_KERNEL);
1758 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1759 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x05_SIZE,
1760 GFP_KERNEL);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001761 else if (sc->quirks & MOTION_CONTROLLER)
1762 sc->output_report_dmabuf =
1763 kmalloc(sizeof(struct motion_output_report_02),
1764 GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001765 else
1766 return 0;
1767
1768 if (!sc->output_report_dmabuf)
1769 return -ENOMEM;
1770
1771 return 0;
1772}
1773
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001774#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001775static int sony_play_effect(struct input_dev *dev, void *data,
1776 struct ff_effect *effect)
1777{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001778 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001779 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001780
1781 if (effect->type != FF_RUMBLE)
1782 return 0;
1783
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001784 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001785 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001786
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001787 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001788 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001789}
1790
Frank Praznikfa57a812014-04-14 10:11:33 -04001791static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001792{
Frank Praznikfa57a812014-04-14 10:11:33 -04001793 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001794 struct hid_input, list);
1795 struct input_dev *input_dev = hidinput->input;
1796
1797 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1798 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1799}
1800
1801#else
Frank Praznikfa57a812014-04-14 10:11:33 -04001802static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001803{
1804 return 0;
1805}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001806
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001807#endif
1808
Frank Praznikd902f472014-01-27 10:17:36 -05001809static int sony_battery_get_property(struct power_supply *psy,
1810 enum power_supply_property psp,
1811 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001812{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001813 struct sony_sc *sc = power_supply_get_drvdata(psy);
Frank Praznikd902f472014-01-27 10:17:36 -05001814 unsigned long flags;
1815 int ret = 0;
1816 u8 battery_charging, battery_capacity, cable_state;
1817
1818 spin_lock_irqsave(&sc->lock, flags);
1819 battery_charging = sc->battery_charging;
1820 battery_capacity = sc->battery_capacity;
1821 cable_state = sc->cable_state;
1822 spin_unlock_irqrestore(&sc->lock, flags);
1823
1824 switch (psp) {
1825 case POWER_SUPPLY_PROP_PRESENT:
1826 val->intval = 1;
1827 break;
1828 case POWER_SUPPLY_PROP_SCOPE:
1829 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1830 break;
1831 case POWER_SUPPLY_PROP_CAPACITY:
1832 val->intval = battery_capacity;
1833 break;
1834 case POWER_SUPPLY_PROP_STATUS:
1835 if (battery_charging)
1836 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1837 else
1838 if (battery_capacity == 100 && cable_state)
1839 val->intval = POWER_SUPPLY_STATUS_FULL;
1840 else
1841 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1842 break;
1843 default:
1844 ret = -EINVAL;
1845 break;
1846 }
1847 return ret;
1848}
1849
1850static int sony_battery_probe(struct sony_sc *sc)
1851{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001852 struct power_supply_config psy_cfg = { .drv_data = sc, };
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001853 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05001854 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001855
Frank Praznikad142b92014-02-20 11:36:00 -05001856 /*
1857 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05001858 * if the battery is polled before the first device report is received.
1859 */
1860 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001861
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001862 sc->battery_desc.properties = sony_battery_props;
1863 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
1864 sc->battery_desc.get_property = sony_battery_get_property;
1865 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
1866 sc->battery_desc.use_for_apm = 0;
1867 sc->battery_desc.name = kasprintf(GFP_KERNEL,
1868 "sony_controller_battery_%pMR",
1869 sc->mac_address);
1870 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05001871 return -ENOMEM;
1872
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001873 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
1874 &psy_cfg);
1875 if (IS_ERR(sc->battery)) {
1876 ret = PTR_ERR(sc->battery);
Frank Praznikd902f472014-01-27 10:17:36 -05001877 hid_err(hdev, "Unable to register battery device\n");
1878 goto err_free;
1879 }
1880
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001881 power_supply_powers(sc->battery, &hdev->dev);
Frank Praznikd902f472014-01-27 10:17:36 -05001882 return 0;
1883
1884err_free:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001885 kfree(sc->battery_desc.name);
1886 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05001887 return ret;
1888}
1889
1890static void sony_battery_remove(struct sony_sc *sc)
1891{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001892 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05001893 return;
1894
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001895 power_supply_unregister(sc->battery);
1896 kfree(sc->battery_desc.name);
1897 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05001898}
1899
Frank Praznikd2d782f2014-02-20 11:36:03 -05001900/*
1901 * If a controller is plugged in via USB while already connected via Bluetooth
1902 * it will show up as two devices. A global list of connected controllers and
1903 * their MAC addresses is maintained to ensure that a device is only connected
1904 * once.
1905 */
1906static int sony_check_add_dev_list(struct sony_sc *sc)
1907{
1908 struct sony_sc *entry;
1909 unsigned long flags;
1910 int ret;
1911
1912 spin_lock_irqsave(&sony_dev_list_lock, flags);
1913
1914 list_for_each_entry(entry, &sony_device_list, list_node) {
1915 ret = memcmp(sc->mac_address, entry->mac_address,
1916 sizeof(sc->mac_address));
1917 if (!ret) {
1918 ret = -EEXIST;
1919 hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
1920 sc->mac_address);
1921 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001922 }
1923 }
1924
Frank Praznikd2d782f2014-02-20 11:36:03 -05001925 ret = 0;
1926 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001927
Frank Praznikd2d782f2014-02-20 11:36:03 -05001928unlock:
1929 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1930 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001931}
1932
Frank Praznikd2d782f2014-02-20 11:36:03 -05001933static void sony_remove_dev_list(struct sony_sc *sc)
1934{
1935 unsigned long flags;
1936
1937 if (sc->list_node.next) {
1938 spin_lock_irqsave(&sony_dev_list_lock, flags);
1939 list_del(&(sc->list_node));
1940 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1941 }
1942}
1943
1944static int sony_get_bt_devaddr(struct sony_sc *sc)
1945{
1946 int ret;
1947
1948 /* HIDP stores the device MAC address as a string in the uniq field. */
1949 ret = strlen(sc->hdev->uniq);
1950 if (ret != 17)
1951 return -EINVAL;
1952
1953 ret = sscanf(sc->hdev->uniq,
1954 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1955 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
1956 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
1957
1958 if (ret != 6)
1959 return -EINVAL;
1960
1961 return 0;
1962}
1963
1964static int sony_check_add(struct sony_sc *sc)
1965{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001966 __u8 *buf = NULL;
Frank Praznikd2d782f2014-02-20 11:36:03 -05001967 int n, ret;
1968
1969 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
1970 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
1971 /*
1972 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
1973 * address from the uniq string where HIDP stores it.
1974 * As uniq cannot be guaranteed to be a MAC address in all cases
1975 * a failure of this function should not prevent the connection.
1976 */
1977 if (sony_get_bt_devaddr(sc) < 0) {
1978 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
1979 return 0;
1980 }
1981 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001982 buf = kmalloc(DS4_REPORT_0x81_SIZE, GFP_KERNEL);
1983 if (!buf)
1984 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05001985
1986 /*
1987 * The MAC address of a DS4 controller connected via USB can be
1988 * retrieved with feature report 0x81. The address begins at
1989 * offset 1.
1990 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001991 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
1992 DS4_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
1993 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05001994
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001995 if (ret != DS4_REPORT_0x81_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05001996 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001997 ret = ret < 0 ? ret : -EINVAL;
1998 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05001999 }
2000
2001 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
2002 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002003 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2004 if (!buf)
2005 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002006
2007 /*
2008 * The MAC address of a Sixaxis controller connected via USB can
2009 * be retrieved with feature report 0xf2. The address begins at
2010 * offset 4.
2011 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002012 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2013 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2014 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002015
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002016 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002017 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002018 ret = ret < 0 ? ret : -EINVAL;
2019 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002020 }
2021
2022 /*
2023 * The Sixaxis device MAC in the report is big-endian and must
2024 * be byte-swapped.
2025 */
2026 for (n = 0; n < 6; n++)
2027 sc->mac_address[5-n] = buf[4+n];
2028 } else {
2029 return 0;
2030 }
2031
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002032 ret = sony_check_add_dev_list(sc);
2033
2034out_free:
2035
2036 kfree(buf);
2037
2038 return ret;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002039}
2040
Frank Praznik80250872014-04-14 10:11:35 -04002041static int sony_set_device_id(struct sony_sc *sc)
2042{
2043 int ret;
2044
2045 /*
2046 * Only DualShock 4 or Sixaxis controllers get an id.
2047 * All others are set to -1.
2048 */
2049 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2050 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2051 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2052 GFP_KERNEL);
2053 if (ret < 0) {
2054 sc->device_id = -1;
2055 return ret;
2056 }
2057 sc->device_id = ret;
2058 } else {
2059 sc->device_id = -1;
2060 }
2061
2062 return 0;
2063}
2064
2065static void sony_release_device_id(struct sony_sc *sc)
2066{
2067 if (sc->device_id >= 0) {
2068 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2069 sc->device_id = -1;
2070 }
2071}
2072
Frank Praznik46262042014-04-14 10:11:31 -04002073static inline void sony_init_work(struct sony_sc *sc,
2074 void (*worker)(struct work_struct *))
2075{
2076 if (!sc->worker_initialized)
2077 INIT_WORK(&sc->state_worker, worker);
2078
2079 sc->worker_initialized = 1;
2080}
2081
2082static inline void sony_cancel_work_sync(struct sony_sc *sc)
2083{
2084 if (sc->worker_initialized)
2085 cancel_work_sync(&sc->state_worker);
2086}
Frank Praznikd2d782f2014-02-20 11:36:03 -05002087
Jiri Slabybd28ce02008-06-25 23:47:04 +02002088static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2089{
2090 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002091 unsigned long quirks = id->driver_data;
2092 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02002093 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002094
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002095 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002096 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08002097 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002098 return -ENOMEM;
2099 }
2100
Frank Praznikb94993f2015-02-22 20:42:46 -05002101 spin_lock_init(&sc->lock);
2102
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002103 sc->quirks = quirks;
2104 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002105 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002106
Jiri Slabybd28ce02008-06-25 23:47:04 +02002107 ret = hid_parse(hdev);
2108 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002109 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002110 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002111 }
2112
Colin Leitnerf04d5142013-05-27 23:41:05 +02002113 if (sc->quirks & VAIO_RDESC_CONSTANT)
2114 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
Antonio Ospite50764652014-06-24 13:28:40 +02002115 else if (sc->quirks & SIXAXIS_CONTROLLER)
Colin Leitnerf04d5142013-05-27 23:41:05 +02002116 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2117
2118 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002119 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002120 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002121 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002122 }
2123
Frank Praznik80250872014-04-14 10:11:35 -04002124 ret = sony_set_device_id(sc);
2125 if (ret < 0) {
2126 hid_err(hdev, "failed to allocate the device id\n");
2127 goto err_stop;
2128 }
2129
Frank Praznik131a8a92015-05-05 20:47:28 -04002130 ret = sony_allocate_output_report(sc);
2131 if (ret < 0) {
2132 hid_err(hdev, "failed to allocate the output report buffer\n");
2133 goto err_stop;
2134 }
2135
Antonio Ospite569b10a2010-10-19 16:13:10 +02002136 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002137 /*
2138 * The Sony Sixaxis does not handle HID Output Reports on the
2139 * Interrupt EP like it could, so we need to force HID Output
2140 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2141 *
2142 * There is also another issue about HID Output Reports via USB,
2143 * the Sixaxis does not want the report_id as part of the data
2144 * packet, so we have to discard buf[0] when sending the actual
2145 * control message, even for numbered reports, humpf!
2146 */
2147 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2148 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
Antonio Ospite816651a2010-05-03 22:15:55 +02002149 ret = sixaxis_set_operational_usb(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04002150 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002151 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04002152 /*
2153 * The Sixaxis wants output reports sent on the ctrl endpoint
2154 * when connected via Bluetooth.
2155 */
2156 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Antonio Ospite816651a2010-05-03 22:15:55 +02002157 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04002158 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002159 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik68330d82014-02-05 20:03:49 -05002160 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04002161 /*
2162 * The DualShock 4 wants output reports sent on the ctrl
2163 * endpoint when connected via Bluetooth.
2164 */
2165 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Frank Praznik68330d82014-02-05 20:03:49 -05002166 ret = dualshock4_set_operational_bt(hdev);
2167 if (ret < 0) {
2168 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
2169 goto err_stop;
2170 }
2171 }
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002172
Frank Praznik46262042014-04-14 10:11:31 -04002173 sony_init_work(sc, dualshock4_state_worker);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002174 } else if (sc->quirks & MOTION_CONTROLLER) {
2175 sony_init_work(sc, motion_state_worker);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002176 } else {
2177 ret = 0;
2178 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00002179
Jiri Kosina4dfdc462008-12-30 00:49:59 +01002180 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002181 goto err_stop;
2182
Frank Praznikd2d782f2014-02-20 11:36:03 -05002183 ret = sony_check_add(sc);
2184 if (ret < 0)
2185 goto err_stop;
2186
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002187 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002188 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002189 if (ret < 0)
2190 goto err_stop;
2191 }
2192
Frank Praznikd902f472014-01-27 10:17:36 -05002193 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2194 ret = sony_battery_probe(sc);
2195 if (ret < 0)
2196 goto err_stop;
2197
2198 /* Open the device to receive reports with battery info */
2199 ret = hid_hw_open(hdev);
2200 if (ret < 0) {
2201 hid_err(hdev, "hw open failed\n");
2202 goto err_stop;
2203 }
2204 }
2205
Frank Praznikc8de9db2014-02-20 11:36:01 -05002206 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002207 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05002208 if (ret < 0)
2209 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05002210 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002211
Jiri Slabybd28ce02008-06-25 23:47:04 +02002212 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05002213err_close:
2214 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002215err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002216 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002217 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002218 if (sc->quirks & SONY_BATTERY_SUPPORT)
2219 sony_battery_remove(sc);
Frank Praznik46262042014-04-14 10:11:31 -04002220 sony_cancel_work_sync(sc);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002221 kfree(sc->output_report_dmabuf);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002222 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04002223 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002224 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002225 return ret;
2226}
2227
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002228static void sony_remove(struct hid_device *hdev)
2229{
Colin Leitnerf04d5142013-05-27 23:41:05 +02002230 struct sony_sc *sc = hid_get_drvdata(hdev);
2231
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002232 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002233 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02002234
Frank Praznikd902f472014-01-27 10:17:36 -05002235 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2236 hid_hw_close(hdev);
2237 sony_battery_remove(sc);
2238 }
2239
Frank Praznik46262042014-04-14 10:11:31 -04002240 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002241
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002242 kfree(sc->output_report_dmabuf);
2243
Frank Praznikd2d782f2014-02-20 11:36:03 -05002244 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002245
Frank Praznik80250872014-04-14 10:11:35 -04002246 sony_release_device_id(sc);
2247
Jiri Slabybd28ce02008-06-25 23:47:04 +02002248 hid_hw_stop(hdev);
2249}
2250
2251static const struct hid_device_id sony_devices[] = {
2252 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2253 .driver_data = SIXAXIS_CONTROLLER_USB },
2254 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2255 .driver_data = SIXAXIS_CONTROLLER_USB },
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002256 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
2257 .driver_data = MOTION_CONTROLLER },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002258 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2259 .driver_data = SIXAXIS_CONTROLLER_BT },
2260 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2261 .driver_data = VAIO_RDESC_CONSTANT },
2262 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2263 .driver_data = VAIO_RDESC_CONSTANT },
2264 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2265 * Logitech joystick from the device descriptor. */
2266 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2267 .driver_data = BUZZ_CONTROLLER },
2268 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2269 .driver_data = BUZZ_CONTROLLER },
2270 /* PS3 BD Remote Control */
2271 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2272 .driver_data = PS3REMOTE },
2273 /* Logitech Harmony Adapter for PS3 */
2274 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2275 .driver_data = PS3REMOTE },
Frank Praznik68a49e52014-11-12 14:52:28 -05002276 /* SMK-Link PS3 BD Remote Control */
2277 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2278 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002279 /* Sony Dualshock 4 controllers for PS4 */
2280 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002281 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002282 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002283 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002284 { }
2285};
2286MODULE_DEVICE_TABLE(hid, sony_devices);
2287
2288static struct hid_driver sony_driver = {
Frank Praznikce8efc32014-09-18 21:15:01 -04002289 .name = "sony",
2290 .id_table = sony_devices,
2291 .input_mapping = sony_mapping,
2292 .input_configured = sony_input_configured,
2293 .probe = sony_probe,
2294 .remove = sony_remove,
2295 .report_fixup = sony_report_fixup,
2296 .raw_event = sony_raw_event
Jiri Slabybd28ce02008-06-25 23:47:04 +02002297};
Frank Praznik80250872014-04-14 10:11:35 -04002298
2299static int __init sony_init(void)
2300{
2301 dbg_hid("Sony:%s\n", __func__);
2302
2303 return hid_register_driver(&sony_driver);
2304}
2305
2306static void __exit sony_exit(void)
2307{
2308 dbg_hid("Sony:%s\n", __func__);
2309
Frank Praznik80250872014-04-14 10:11:35 -04002310 hid_unregister_driver(&sony_driver);
Antonio Ospite6c400652015-02-16 22:58:24 +01002311 ida_destroy(&sony_device_id_allocator);
Frank Praznik80250872014-04-14 10:11:35 -04002312}
2313module_init(sony_init);
2314module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002315
2316MODULE_LICENSE("GPL");