blob: d0b18a5779d7ad2facf73efe0ed1af141886fe02 [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 Caoa46491842013-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 Praznik80250872014-04-14 10:11:35 -04001309static void sixaxis_set_leds_from_id(int id, __u8 values[MAX_LEDS])
1310{
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
1324 BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
1325
1326 if (id < 0)
1327 return;
1328
1329 id %= 10;
1330 memcpy(values, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
1331}
1332
1333static void dualshock4_set_leds_from_id(int id, __u8 values[MAX_LEDS])
1334{
1335 /* The first 4 color/index entries match what the PS4 assigns */
1336 static const __u8 color_code[7][3] = {
1337 /* Blue */ { 0x00, 0x00, 0x01 },
1338 /* Red */ { 0x01, 0x00, 0x00 },
1339 /* Green */ { 0x00, 0x01, 0x00 },
1340 /* Pink */ { 0x02, 0x00, 0x01 },
1341 /* Orange */ { 0x02, 0x01, 0x00 },
1342 /* Teal */ { 0x00, 0x01, 0x01 },
1343 /* White */ { 0x01, 0x01, 0x01 }
1344 };
1345
1346 BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
1347
1348 if (id < 0)
1349 return;
1350
1351 id %= 7;
1352 memcpy(values, color_code[id], sizeof(color_code[id]));
1353}
1354
Frank Praznik60781cf2014-01-11 15:13:15 -05001355static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001356{
1357 struct list_head *report_list =
1358 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1359 struct hid_report *report = list_entry(report_list->next,
1360 struct hid_report, list);
1361 __s32 *value = report->field[0]->value;
1362
1363 value[0] = 0x00;
Frank Praznik60781cf2014-01-11 15:13:15 -05001364 value[1] = leds[0] ? 0xff : 0x00;
1365 value[2] = leds[1] ? 0xff : 0x00;
1366 value[3] = leds[2] ? 0xff : 0x00;
1367 value[4] = leds[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001368 value[5] = 0x00;
1369 value[6] = 0x00;
1370 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1371}
1372
Frank Praznikfa57a812014-04-14 10:11:33 -04001373static void sony_set_leds(struct sony_sc *sc, const __u8 *leds, int count)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001374{
Frank Praznik60781cf2014-01-11 15:13:15 -05001375 int n;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001376
Frank Praznik60781cf2014-01-11 15:13:15 -05001377 BUG_ON(count > MAX_LEDS);
1378
Frank Praznikfa57a812014-04-14 10:11:33 -04001379 if (sc->quirks & BUZZ_CONTROLLER && count == 4) {
1380 buzz_set_leds(sc->hdev, leds);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001381 } else {
Frank Praznik60781cf2014-01-11 15:13:15 -05001382 for (n = 0; n < count; n++)
Frank Praznikfa57a812014-04-14 10:11:33 -04001383 sc->led_state[n] = leds[n];
1384 schedule_work(&sc->state_worker);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001385 }
1386}
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
1425 sony_set_leds(drv_data, drv_data->led_state,
1426 drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001427 break;
1428 }
1429 }
1430}
1431
Sven Eckelmannc5382512013-11-19 20:26:30 +01001432static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001433{
1434 struct device *dev = led->dev->parent;
1435 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1436 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001437
1438 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001439
1440 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001441 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001442 hid_err(hdev, "No device data\n");
1443 return LED_OFF;
1444 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001445
Frank Praznik60781cf2014-01-11 15:13:15 -05001446 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001447 if (led == drv_data->leds[n])
1448 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001449 }
1450
Simon Wood7db75042014-02-05 12:34:18 -07001451 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001452}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001453
Frank Praznikb3ed4582014-04-14 10:11:36 -04001454static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1455 unsigned long *delay_off)
1456{
1457 struct device *dev = led->dev->parent;
1458 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1459 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1460 int n;
1461 __u8 new_on, new_off;
1462
1463 if (!drv_data) {
1464 hid_err(hdev, "No device data\n");
1465 return -EINVAL;
1466 }
1467
1468 /* Max delay is 255 deciseconds or 2550 milliseconds */
1469 if (*delay_on > 2550)
1470 *delay_on = 2550;
1471 if (*delay_off > 2550)
1472 *delay_off = 2550;
1473
1474 /* Blink at 1 Hz if both values are zero */
1475 if (!*delay_on && !*delay_off)
1476 *delay_on = *delay_off = 500;
1477
1478 new_on = *delay_on / 10;
1479 new_off = *delay_off / 10;
1480
1481 for (n = 0; n < drv_data->led_count; n++) {
1482 if (led == drv_data->leds[n])
1483 break;
1484 }
1485
1486 /* This LED is not registered on this device */
1487 if (n >= drv_data->led_count)
1488 return -EINVAL;
1489
1490 /* Don't schedule work if the values didn't change */
1491 if (new_on != drv_data->led_delay_on[n] ||
1492 new_off != drv_data->led_delay_off[n]) {
1493 drv_data->led_delay_on[n] = new_on;
1494 drv_data->led_delay_off[n] = new_off;
1495 schedule_work(&drv_data->state_worker);
1496 }
1497
1498 return 0;
1499}
1500
Frank Praznikfa57a812014-04-14 10:11:33 -04001501static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001502{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001503 struct led_classdev *led;
1504 int n;
1505
Frank Praznikfa57a812014-04-14 10:11:33 -04001506 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001507
Frank Praznikfa57a812014-04-14 10:11:33 -04001508 for (n = 0; n < sc->led_count; n++) {
1509 led = sc->leds[n];
1510 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001511 if (!led)
1512 continue;
1513 led_classdev_unregister(led);
1514 kfree(led);
1515 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001516
Frank Praznikfa57a812014-04-14 10:11:33 -04001517 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001518}
1519
Frank Praznikfa57a812014-04-14 10:11:33 -04001520static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001521{
Frank Praznikfa57a812014-04-14 10:11:33 -04001522 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001523 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001524 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001525 struct led_classdev *led;
1526 size_t name_sz;
1527 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001528 size_t name_len;
1529 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001530 static const char * const ds4_name_str[] = { "red", "green", "blue",
1531 "global" };
Frank Praznik80250872014-04-14 10:11:35 -04001532 __u8 initial_values[MAX_LEDS] = { 0 };
Frank Praznik5607c892014-07-29 19:55:48 -07001533 __u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
Frank Praznikb3ed4582014-04-14 10:11:36 -04001534 __u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001535
Frank Praznikfa57a812014-04-14 10:11:33 -04001536 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001537
Frank Praznikfa57a812014-04-14 10:11:33 -04001538 if (sc->quirks & BUZZ_CONTROLLER) {
1539 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001540 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001541 name_len = strlen("::buzz#");
1542 name_fmt = "%s::buzz%d";
1543 /* Validate expected report characteristics. */
1544 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1545 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001546 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik80250872014-04-14 10:11:35 -04001547 dualshock4_set_leds_from_id(sc->device_id, initial_values);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001548 initial_values[3] = 1;
1549 sc->led_count = 4;
1550 memset(max_brightness, 255, 3);
1551 use_hw_blink[3] = 1;
1552 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001553 name_len = 0;
1554 name_fmt = "%s:%s";
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001555 } else if (sc->quirks & MOTION_CONTROLLER) {
1556 sc->led_count = 3;
1557 memset(max_brightness, 255, 3);
1558 use_ds4_names = 1;
1559 name_len = 0;
1560 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -05001561 } else {
Frank Praznik80250872014-04-14 10:11:35 -04001562 sixaxis_set_leds_from_id(sc->device_id, initial_values);
Frank Praznikfa57a812014-04-14 10:11:33 -04001563 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001564 memset(use_hw_blink, 1, 4);
1565 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001566 name_len = strlen("::sony#");
1567 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001568 }
1569
Frank Praznikad142b92014-02-20 11:36:00 -05001570 /*
1571 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001572 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001573 * LEDs to on
1574 */
Frank Praznikfa57a812014-04-14 10:11:33 -04001575 sony_set_leds(sc, initial_values, sc->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001576
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001577 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001578
Frank Praznikfa57a812014-04-14 10:11:33 -04001579 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001580
Frank Praznikb3ed4582014-04-14 10:11:36 -04001581 if (use_ds4_names)
1582 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001583
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001584 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1585 if (!led) {
1586 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001587 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001588 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001589 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001590
1591 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001592 if (use_ds4_names)
1593 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1594 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001595 else
1596 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001597 led->name = name;
Frank Praznik80250872014-04-14 10:11:35 -04001598 led->brightness = initial_values[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001599 led->max_brightness = max_brightness[n];
Sven Eckelmannc5382512013-11-19 20:26:30 +01001600 led->brightness_get = sony_led_get_brightness;
1601 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001602
Frank Praznikb3ed4582014-04-14 10:11:36 -04001603 if (use_hw_blink[n])
1604 led->blink_set = sony_led_blink_set;
1605
Frank Praznik80250872014-04-14 10:11:35 -04001606 sc->leds[n] = led;
1607
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001608 ret = led_classdev_register(&hdev->dev, led);
1609 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001610 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001611 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001612 kfree(led);
1613 goto error_leds;
1614 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001615 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001616
1617 return ret;
1618
Colin Leitnerf04d5142013-05-27 23:41:05 +02001619error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001620 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001621
Colin Leitnerf04d5142013-05-27 23:41:05 +02001622 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001623}
1624
Frank Praznikcad665a2014-01-11 15:13:54 -05001625static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001626{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001627 static const union sixaxis_output_report_01 default_report = {
Frank Praznik55d3b662014-04-14 10:11:32 -04001628 .buf = {
1629 0x01,
1630 0x00, 0xff, 0x00, 0xff, 0x00,
1631 0x00, 0x00, 0x00, 0x00, 0x00,
1632 0xff, 0x27, 0x10, 0x00, 0x32,
1633 0xff, 0x27, 0x10, 0x00, 0x32,
1634 0xff, 0x27, 0x10, 0x00, 0x32,
1635 0xff, 0x27, 0x10, 0x00, 0x32,
1636 0x00, 0x00, 0x00, 0x00, 0x00
1637 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001638 };
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001639 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1640 struct sixaxis_output_report *report =
1641 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1642 int n;
1643
1644 /* Initialize the report with default values */
1645 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001646
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001647#ifdef CONFIG_SONY_FF
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001648 report->rumble.right_motor_on = sc->right ? 1 : 0;
1649 report->rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001650#endif
1651
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001652 report->leds_bitmap |= sc->led_state[0] << 1;
1653 report->leds_bitmap |= sc->led_state[1] << 2;
1654 report->leds_bitmap |= sc->led_state[2] << 3;
1655 report->leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001656
Simon Wood88f65762014-04-14 10:11:37 -04001657 /* Set flag for all leds off, required for 3rd party INTEC controller */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001658 if ((report->leds_bitmap & 0x1E) == 0)
1659 report->leds_bitmap |= 0x20;
Simon Wood88f65762014-04-14 10:11:37 -04001660
Frank Praznikb3ed4582014-04-14 10:11:36 -04001661 /*
1662 * The LEDs in the report are indexed in reverse order to their
1663 * corresponding light on the controller.
1664 * Index 0 = LED 4, index 1 = LED 3, etc...
1665 *
1666 * In the case of both delay values being zero (blinking disabled) the
1667 * default report values should be used or the controller LED will be
1668 * always off.
1669 */
1670 for (n = 0; n < 4; n++) {
1671 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001672 report->led[3 - n].duty_off = sc->led_delay_off[n];
1673 report->led[3 - n].duty_on = sc->led_delay_on[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001674 }
1675 }
1676
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001677 hid_hw_raw_request(sc->hdev, report->report_id, (__u8 *)report,
1678 sizeof(struct sixaxis_output_report),
1679 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001680}
1681
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001682static void dualshock4_state_worker(struct work_struct *work)
1683{
1684 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001685 struct hid_device *hdev = sc->hdev;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001686 __u8 *buf = sc->output_report_dmabuf;
Frank Praznik48220232014-02-05 20:03:44 -05001687 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001688
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001689 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001690 memset(buf, 0, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001691 buf[0] = 0x05;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001692 buf[1] = 0xFF;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001693 offset = 4;
1694 } else {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001695 memset(buf, 0, DS4_REPORT_0x11_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001696 buf[0] = 0x11;
1697 buf[1] = 0xB0;
1698 buf[3] = 0x0F;
1699 offset = 6;
1700 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001701
1702#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001703 buf[offset++] = sc->right;
1704 buf[offset++] = sc->left;
1705#else
1706 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001707#endif
1708
Frank Praznikb3ed4582014-04-14 10:11:36 -04001709 /* LED 3 is the global control */
1710 if (sc->led_state[3]) {
1711 buf[offset++] = sc->led_state[0];
1712 buf[offset++] = sc->led_state[1];
1713 buf[offset++] = sc->led_state[2];
1714 } else {
1715 offset += 3;
1716 }
1717
1718 /* If both delay values are zero the DualShock 4 disables blinking. */
1719 buf[offset++] = sc->led_delay_on[3];
1720 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05001721
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001722 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001723 hid_hw_output_report(hdev, buf, DS4_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001724 else
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001725 hid_hw_raw_request(hdev, 0x11, buf, DS4_REPORT_0x11_SIZE,
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001726 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001727}
1728
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001729static void motion_state_worker(struct work_struct *work)
1730{
1731 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
1732 struct hid_device *hdev = sc->hdev;
1733 struct motion_output_report_02 *report =
1734 (struct motion_output_report_02 *)sc->output_report_dmabuf;
1735
1736 memset(report, 0, sizeof(struct motion_output_report_02));
1737
1738 report->type = 0x02; /* set leds */
1739 report->r = sc->led_state[0];
1740 report->g = sc->led_state[1];
1741 report->b = sc->led_state[2];
1742
1743#ifdef CONFIG_SONY_FF
1744 report->rumble = max(sc->right, sc->left);
1745#endif
1746
1747 hid_hw_output_report(hdev, (__u8 *)report,
1748 sizeof(struct motion_output_report_02));
1749}
1750
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001751static int sony_allocate_output_report(struct sony_sc *sc)
1752{
1753 if (sc->quirks & SIXAXIS_CONTROLLER)
1754 sc->output_report_dmabuf =
1755 kmalloc(sizeof(union sixaxis_output_report_01),
1756 GFP_KERNEL);
1757 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
1758 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x11_SIZE,
1759 GFP_KERNEL);
1760 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1761 sc->output_report_dmabuf = kmalloc(DS4_REPORT_0x05_SIZE,
1762 GFP_KERNEL);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001763 else if (sc->quirks & MOTION_CONTROLLER)
1764 sc->output_report_dmabuf =
1765 kmalloc(sizeof(struct motion_output_report_02),
1766 GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001767 else
1768 return 0;
1769
1770 if (!sc->output_report_dmabuf)
1771 return -ENOMEM;
1772
1773 return 0;
1774}
1775
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001776#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001777static int sony_play_effect(struct input_dev *dev, void *data,
1778 struct ff_effect *effect)
1779{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001780 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001781 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001782
1783 if (effect->type != FF_RUMBLE)
1784 return 0;
1785
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001786 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001787 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001788
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001789 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001790 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001791}
1792
Frank Praznikfa57a812014-04-14 10:11:33 -04001793static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001794{
Frank Praznikfa57a812014-04-14 10:11:33 -04001795 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001796 struct hid_input, list);
1797 struct input_dev *input_dev = hidinput->input;
1798
1799 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1800 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1801}
1802
1803#else
Frank Praznikfa57a812014-04-14 10:11:33 -04001804static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001805{
1806 return 0;
1807}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001808
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001809#endif
1810
Frank Praznikd902f472014-01-27 10:17:36 -05001811static int sony_battery_get_property(struct power_supply *psy,
1812 enum power_supply_property psp,
1813 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001814{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001815 struct sony_sc *sc = power_supply_get_drvdata(psy);
Frank Praznikd902f472014-01-27 10:17:36 -05001816 unsigned long flags;
1817 int ret = 0;
1818 u8 battery_charging, battery_capacity, cable_state;
1819
1820 spin_lock_irqsave(&sc->lock, flags);
1821 battery_charging = sc->battery_charging;
1822 battery_capacity = sc->battery_capacity;
1823 cable_state = sc->cable_state;
1824 spin_unlock_irqrestore(&sc->lock, flags);
1825
1826 switch (psp) {
1827 case POWER_SUPPLY_PROP_PRESENT:
1828 val->intval = 1;
1829 break;
1830 case POWER_SUPPLY_PROP_SCOPE:
1831 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1832 break;
1833 case POWER_SUPPLY_PROP_CAPACITY:
1834 val->intval = battery_capacity;
1835 break;
1836 case POWER_SUPPLY_PROP_STATUS:
1837 if (battery_charging)
1838 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1839 else
1840 if (battery_capacity == 100 && cable_state)
1841 val->intval = POWER_SUPPLY_STATUS_FULL;
1842 else
1843 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1844 break;
1845 default:
1846 ret = -EINVAL;
1847 break;
1848 }
1849 return ret;
1850}
1851
1852static int sony_battery_probe(struct sony_sc *sc)
1853{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001854 struct power_supply_config psy_cfg = { .drv_data = sc, };
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001855 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05001856 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001857
Frank Praznikad142b92014-02-20 11:36:00 -05001858 /*
1859 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05001860 * if the battery is polled before the first device report is received.
1861 */
1862 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001863
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001864 sc->battery_desc.properties = sony_battery_props;
1865 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
1866 sc->battery_desc.get_property = sony_battery_get_property;
1867 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
1868 sc->battery_desc.use_for_apm = 0;
1869 sc->battery_desc.name = kasprintf(GFP_KERNEL,
1870 "sony_controller_battery_%pMR",
1871 sc->mac_address);
1872 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05001873 return -ENOMEM;
1874
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001875 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
1876 &psy_cfg);
1877 if (IS_ERR(sc->battery)) {
1878 ret = PTR_ERR(sc->battery);
Frank Praznikd902f472014-01-27 10:17:36 -05001879 hid_err(hdev, "Unable to register battery device\n");
1880 goto err_free;
1881 }
1882
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001883 power_supply_powers(sc->battery, &hdev->dev);
Frank Praznikd902f472014-01-27 10:17:36 -05001884 return 0;
1885
1886err_free:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001887 kfree(sc->battery_desc.name);
1888 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05001889 return ret;
1890}
1891
1892static void sony_battery_remove(struct sony_sc *sc)
1893{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001894 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05001895 return;
1896
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01001897 power_supply_unregister(sc->battery);
1898 kfree(sc->battery_desc.name);
1899 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05001900}
1901
Frank Praznikd2d782f2014-02-20 11:36:03 -05001902/*
1903 * If a controller is plugged in via USB while already connected via Bluetooth
1904 * it will show up as two devices. A global list of connected controllers and
1905 * their MAC addresses is maintained to ensure that a device is only connected
1906 * once.
1907 */
1908static int sony_check_add_dev_list(struct sony_sc *sc)
1909{
1910 struct sony_sc *entry;
1911 unsigned long flags;
1912 int ret;
1913
1914 spin_lock_irqsave(&sony_dev_list_lock, flags);
1915
1916 list_for_each_entry(entry, &sony_device_list, list_node) {
1917 ret = memcmp(sc->mac_address, entry->mac_address,
1918 sizeof(sc->mac_address));
1919 if (!ret) {
1920 ret = -EEXIST;
1921 hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
1922 sc->mac_address);
1923 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001924 }
1925 }
1926
Frank Praznikd2d782f2014-02-20 11:36:03 -05001927 ret = 0;
1928 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001929
Frank Praznikd2d782f2014-02-20 11:36:03 -05001930unlock:
1931 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1932 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001933}
1934
Frank Praznikd2d782f2014-02-20 11:36:03 -05001935static void sony_remove_dev_list(struct sony_sc *sc)
1936{
1937 unsigned long flags;
1938
1939 if (sc->list_node.next) {
1940 spin_lock_irqsave(&sony_dev_list_lock, flags);
1941 list_del(&(sc->list_node));
1942 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1943 }
1944}
1945
1946static int sony_get_bt_devaddr(struct sony_sc *sc)
1947{
1948 int ret;
1949
1950 /* HIDP stores the device MAC address as a string in the uniq field. */
1951 ret = strlen(sc->hdev->uniq);
1952 if (ret != 17)
1953 return -EINVAL;
1954
1955 ret = sscanf(sc->hdev->uniq,
1956 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1957 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
1958 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
1959
1960 if (ret != 6)
1961 return -EINVAL;
1962
1963 return 0;
1964}
1965
1966static int sony_check_add(struct sony_sc *sc)
1967{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001968 __u8 *buf = NULL;
Frank Praznikd2d782f2014-02-20 11:36:03 -05001969 int n, ret;
1970
1971 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
1972 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
1973 /*
1974 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
1975 * address from the uniq string where HIDP stores it.
1976 * As uniq cannot be guaranteed to be a MAC address in all cases
1977 * a failure of this function should not prevent the connection.
1978 */
1979 if (sony_get_bt_devaddr(sc) < 0) {
1980 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
1981 return 0;
1982 }
1983 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001984 buf = kmalloc(DS4_REPORT_0x81_SIZE, GFP_KERNEL);
1985 if (!buf)
1986 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05001987
1988 /*
1989 * The MAC address of a DS4 controller connected via USB can be
1990 * retrieved with feature report 0x81. The address begins at
1991 * offset 1.
1992 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001993 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
1994 DS4_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
1995 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05001996
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001997 if (ret != DS4_REPORT_0x81_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05001998 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001999 ret = ret < 0 ? ret : -EINVAL;
2000 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002001 }
2002
2003 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
2004 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002005 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2006 if (!buf)
2007 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002008
2009 /*
2010 * The MAC address of a Sixaxis controller connected via USB can
2011 * be retrieved with feature report 0xf2. The address begins at
2012 * offset 4.
2013 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002014 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2015 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2016 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002017
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002018 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002019 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002020 ret = ret < 0 ? ret : -EINVAL;
2021 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002022 }
2023
2024 /*
2025 * The Sixaxis device MAC in the report is big-endian and must
2026 * be byte-swapped.
2027 */
2028 for (n = 0; n < 6; n++)
2029 sc->mac_address[5-n] = buf[4+n];
2030 } else {
2031 return 0;
2032 }
2033
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002034 ret = sony_check_add_dev_list(sc);
2035
2036out_free:
2037
2038 kfree(buf);
2039
2040 return ret;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002041}
2042
Frank Praznik80250872014-04-14 10:11:35 -04002043static int sony_set_device_id(struct sony_sc *sc)
2044{
2045 int ret;
2046
2047 /*
2048 * Only DualShock 4 or Sixaxis controllers get an id.
2049 * All others are set to -1.
2050 */
2051 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2052 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2053 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2054 GFP_KERNEL);
2055 if (ret < 0) {
2056 sc->device_id = -1;
2057 return ret;
2058 }
2059 sc->device_id = ret;
2060 } else {
2061 sc->device_id = -1;
2062 }
2063
2064 return 0;
2065}
2066
2067static void sony_release_device_id(struct sony_sc *sc)
2068{
2069 if (sc->device_id >= 0) {
2070 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2071 sc->device_id = -1;
2072 }
2073}
2074
Frank Praznik46262042014-04-14 10:11:31 -04002075static inline void sony_init_work(struct sony_sc *sc,
2076 void (*worker)(struct work_struct *))
2077{
2078 if (!sc->worker_initialized)
2079 INIT_WORK(&sc->state_worker, worker);
2080
2081 sc->worker_initialized = 1;
2082}
2083
2084static inline void sony_cancel_work_sync(struct sony_sc *sc)
2085{
2086 if (sc->worker_initialized)
2087 cancel_work_sync(&sc->state_worker);
2088}
Frank Praznikd2d782f2014-02-20 11:36:03 -05002089
Jiri Slabybd28ce02008-06-25 23:47:04 +02002090static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2091{
2092 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002093 unsigned long quirks = id->driver_data;
2094 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02002095 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002096
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002097 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002098 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08002099 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002100 return -ENOMEM;
2101 }
2102
Frank Praznikb94993f2015-02-22 20:42:46 -05002103 spin_lock_init(&sc->lock);
2104
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002105 sc->quirks = quirks;
2106 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002107 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002108
Jiri Slabybd28ce02008-06-25 23:47:04 +02002109 ret = hid_parse(hdev);
2110 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002111 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002112 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002113 }
2114
Colin Leitnerf04d5142013-05-27 23:41:05 +02002115 if (sc->quirks & VAIO_RDESC_CONSTANT)
2116 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
Antonio Ospite50764652014-06-24 13:28:40 +02002117 else if (sc->quirks & SIXAXIS_CONTROLLER)
Colin Leitnerf04d5142013-05-27 23:41:05 +02002118 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2119
2120 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002121 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08002122 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02002123 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002124 }
2125
Frank Praznik80250872014-04-14 10:11:35 -04002126 ret = sony_set_device_id(sc);
2127 if (ret < 0) {
2128 hid_err(hdev, "failed to allocate the device id\n");
2129 goto err_stop;
2130 }
2131
Frank Praznik131a8a92015-05-05 20:47:28 -04002132 ret = sony_allocate_output_report(sc);
2133 if (ret < 0) {
2134 hid_err(hdev, "failed to allocate the output report buffer\n");
2135 goto err_stop;
2136 }
2137
Antonio Ospite569b10a2010-10-19 16:13:10 +02002138 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002139 /*
2140 * The Sony Sixaxis does not handle HID Output Reports on the
2141 * Interrupt EP like it could, so we need to force HID Output
2142 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2143 *
2144 * There is also another issue about HID Output Reports via USB,
2145 * the Sixaxis does not want the report_id as part of the data
2146 * packet, so we have to discard buf[0] when sending the actual
2147 * control message, even for numbered reports, humpf!
2148 */
2149 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2150 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
Antonio Ospite816651a2010-05-03 22:15:55 +02002151 ret = sixaxis_set_operational_usb(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04002152 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002153 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04002154 /*
2155 * The Sixaxis wants output reports sent on the ctrl endpoint
2156 * when connected via Bluetooth.
2157 */
2158 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Antonio Ospite816651a2010-05-03 22:15:55 +02002159 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik46262042014-04-14 10:11:31 -04002160 sony_init_work(sc, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002161 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik68330d82014-02-05 20:03:49 -05002162 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04002163 /*
2164 * The DualShock 4 wants output reports sent on the ctrl
2165 * endpoint when connected via Bluetooth.
2166 */
2167 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Frank Praznik68330d82014-02-05 20:03:49 -05002168 ret = dualshock4_set_operational_bt(hdev);
2169 if (ret < 0) {
2170 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
2171 goto err_stop;
2172 }
2173 }
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002174
Frank Praznik46262042014-04-14 10:11:31 -04002175 sony_init_work(sc, dualshock4_state_worker);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002176 } else if (sc->quirks & MOTION_CONTROLLER) {
2177 sony_init_work(sc, motion_state_worker);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002178 } else {
2179 ret = 0;
2180 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00002181
Jiri Kosina4dfdc462008-12-30 00:49:59 +01002182 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002183 goto err_stop;
2184
Frank Praznikd2d782f2014-02-20 11:36:03 -05002185 ret = sony_check_add(sc);
2186 if (ret < 0)
2187 goto err_stop;
2188
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002189 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002190 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002191 if (ret < 0)
2192 goto err_stop;
2193 }
2194
Frank Praznikd902f472014-01-27 10:17:36 -05002195 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2196 ret = sony_battery_probe(sc);
2197 if (ret < 0)
2198 goto err_stop;
2199
2200 /* Open the device to receive reports with battery info */
2201 ret = hid_hw_open(hdev);
2202 if (ret < 0) {
2203 hid_err(hdev, "hw open failed\n");
2204 goto err_stop;
2205 }
2206 }
2207
Frank Praznikc8de9db2014-02-20 11:36:01 -05002208 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002209 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05002210 if (ret < 0)
2211 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05002212 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002213
Jiri Slabybd28ce02008-06-25 23:47:04 +02002214 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05002215err_close:
2216 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002217err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002218 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002219 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002220 if (sc->quirks & SONY_BATTERY_SUPPORT)
2221 sony_battery_remove(sc);
Frank Praznik46262042014-04-14 10:11:31 -04002222 sony_cancel_work_sync(sc);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002223 kfree(sc->output_report_dmabuf);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002224 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04002225 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002226 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002227 return ret;
2228}
2229
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002230static void sony_remove(struct hid_device *hdev)
2231{
Colin Leitnerf04d5142013-05-27 23:41:05 +02002232 struct sony_sc *sc = hid_get_drvdata(hdev);
2233
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002234 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002235 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02002236
Frank Praznikd902f472014-01-27 10:17:36 -05002237 if (sc->quirks & SONY_BATTERY_SUPPORT) {
2238 hid_hw_close(hdev);
2239 sony_battery_remove(sc);
2240 }
2241
Frank Praznik46262042014-04-14 10:11:31 -04002242 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002243
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002244 kfree(sc->output_report_dmabuf);
2245
Frank Praznikd2d782f2014-02-20 11:36:03 -05002246 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002247
Frank Praznik80250872014-04-14 10:11:35 -04002248 sony_release_device_id(sc);
2249
Jiri Slabybd28ce02008-06-25 23:47:04 +02002250 hid_hw_stop(hdev);
2251}
2252
2253static const struct hid_device_id sony_devices[] = {
2254 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2255 .driver_data = SIXAXIS_CONTROLLER_USB },
2256 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
2257 .driver_data = SIXAXIS_CONTROLLER_USB },
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002258 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
2259 .driver_data = MOTION_CONTROLLER },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002260 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2261 .driver_data = SIXAXIS_CONTROLLER_BT },
2262 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2263 .driver_data = VAIO_RDESC_CONSTANT },
2264 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2265 .driver_data = VAIO_RDESC_CONSTANT },
2266 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2267 * Logitech joystick from the device descriptor. */
2268 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2269 .driver_data = BUZZ_CONTROLLER },
2270 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2271 .driver_data = BUZZ_CONTROLLER },
2272 /* PS3 BD Remote Control */
2273 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2274 .driver_data = PS3REMOTE },
2275 /* Logitech Harmony Adapter for PS3 */
2276 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2277 .driver_data = PS3REMOTE },
Frank Praznik68a49e52014-11-12 14:52:28 -05002278 /* SMK-Link PS3 BD Remote Control */
2279 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2280 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002281 /* Sony Dualshock 4 controllers for PS4 */
2282 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002283 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002284 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002285 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002286 { }
2287};
2288MODULE_DEVICE_TABLE(hid, sony_devices);
2289
2290static struct hid_driver sony_driver = {
Frank Praznikce8efc32014-09-18 21:15:01 -04002291 .name = "sony",
2292 .id_table = sony_devices,
2293 .input_mapping = sony_mapping,
2294 .input_configured = sony_input_configured,
2295 .probe = sony_probe,
2296 .remove = sony_remove,
2297 .report_fixup = sony_report_fixup,
2298 .raw_event = sony_raw_event
Jiri Slabybd28ce02008-06-25 23:47:04 +02002299};
Frank Praznik80250872014-04-14 10:11:35 -04002300
2301static int __init sony_init(void)
2302{
2303 dbg_hid("Sony:%s\n", __func__);
2304
2305 return hid_register_driver(&sony_driver);
2306}
2307
2308static void __exit sony_exit(void)
2309{
2310 dbg_hid("Sony:%s\n", __func__);
2311
Frank Praznik80250872014-04-14 10:11:35 -04002312 hid_unregister_driver(&sony_driver);
Antonio Ospite6c400652015-02-16 22:58:24 +01002313 ida_destroy(&sony_device_id_allocator);
Frank Praznik80250872014-04-14 10:11:35 -04002314}
2315module_init(sony_init);
2316module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002317
2318MODULE_LICENSE("GPL");