blob: 31ba01a39e819defa0f3a216190309fec498d319 [file] [log] [blame]
Jiri Slabybd28ce02008-06-25 23:47:04 +02001/*
Jiri Kosina078328d2013-06-13 12:03:49 +02002 * HID driver for Sony / PS2 / PS3 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>
Jiri Slabybd28ce02008-06-25 23:47:04 +020011 */
12
13/*
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
17 * any later version.
18 */
19
Frank Praznikad142b92014-02-20 11:36:00 -050020/*
21 * NOTE: in order for the Sony PS3 BD Remote Control to be found by
Jiri Kosina078328d2013-06-13 12:03:49 +020022 * a Bluetooth host, the key combination Start+Enter has to be kept pressed
23 * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
24 *
25 * There will be no PIN request from the device.
26 */
27
Jiri Slabybd28ce02008-06-25 23:47:04 +020028#include <linux/device.h>
29#include <linux/hid.h>
30#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020032#include <linux/usb.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 Praznike5606232014-01-27 10:17:37 -050037#include <linux/input/mt.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020038
39#include "hid-ids.h"
40
Frank Praznik6c79c182014-01-16 21:43:03 -050041#define VAIO_RDESC_CONSTANT BIT(0)
42#define SIXAXIS_CONTROLLER_USB BIT(1)
43#define SIXAXIS_CONTROLLER_BT BIT(2)
44#define BUZZ_CONTROLLER BIT(3)
45#define PS3REMOTE BIT(4)
Frank Praznik8ab16762014-01-16 21:42:31 -050046#define DUALSHOCK4_CONTROLLER_USB BIT(5)
47#define DUALSHOCK4_CONTROLLER_BT BIT(6)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020048
Frank Praznikfee4e2d2014-02-18 17:22:01 -050049#define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
Frank Praznik68330d82014-02-05 20:03:49 -050050#define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
51 DUALSHOCK4_CONTROLLER_BT)
Frank Praznikfee4e2d2014-02-18 17:22:01 -050052#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
Frank Praznik68330d82014-02-05 20:03:49 -050053 DUALSHOCK4_CONTROLLER)
Frank Praznikfee4e2d2014-02-18 17:22:01 -050054#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER)
Frank Praznikc8de9db2014-02-20 11:36:01 -050055#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER)
Frank Praznik60781cf2014-01-11 15:13:15 -050056
57#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010058
Simon Wood61ab44b2011-06-10 12:00:26 +020059static const u8 sixaxis_rdesc_fixup[] = {
60 0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
61 0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
62 0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
63};
64
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -020065static const u8 sixaxis_rdesc_fixup2[] = {
66 0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
67 0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
68 0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
69 0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
70 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
71 0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
72 0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
73 0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
74 0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
75 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
76 0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
77 0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
78 0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
79 0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
80 0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
81 0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
82 0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
83 0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
84 0xb1, 0x02, 0xc0, 0xc0,
85};
86
Frank Praznikad142b92014-02-20 11:36:00 -050087/*
88 * The default descriptor doesn't provide mapping for the accelerometers
Frank Praznik58d70272014-01-20 12:27:01 -050089 * or orientation sensors. This fixed descriptor maps the accelerometers
90 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
91 * to usage values 0x43, 0x44 and 0x45.
92 */
Frank Prazniked19d8c2014-01-16 21:43:12 -050093static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -050094 0x05, 0x01, /* Usage Page (Desktop), */
95 0x09, 0x05, /* Usage (Gamepad), */
96 0xA1, 0x01, /* Collection (Application), */
97 0x85, 0x01, /* Report ID (1), */
98 0x09, 0x30, /* Usage (X), */
99 0x09, 0x31, /* Usage (Y), */
100 0x09, 0x32, /* Usage (Z), */
101 0x09, 0x35, /* Usage (Rz), */
102 0x15, 0x00, /* Logical Minimum (0), */
103 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
104 0x75, 0x08, /* Report Size (8), */
105 0x95, 0x04, /* Report Count (4), */
106 0x81, 0x02, /* Input (Variable), */
107 0x09, 0x39, /* Usage (Hat Switch), */
108 0x15, 0x00, /* Logical Minimum (0), */
109 0x25, 0x07, /* Logical Maximum (7), */
110 0x35, 0x00, /* Physical Minimum (0), */
111 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
112 0x65, 0x14, /* Unit (Degrees), */
113 0x75, 0x04, /* Report Size (4), */
114 0x95, 0x01, /* Report Count (1), */
115 0x81, 0x42, /* Input (Variable, Null State), */
116 0x65, 0x00, /* Unit, */
117 0x05, 0x09, /* Usage Page (Button), */
118 0x19, 0x01, /* Usage Minimum (01h), */
119 0x29, 0x0E, /* Usage Maximum (0Eh), */
120 0x15, 0x00, /* Logical Minimum (0), */
121 0x25, 0x01, /* Logical Maximum (1), */
122 0x75, 0x01, /* Report Size (1), */
123 0x95, 0x0E, /* Report Count (14), */
124 0x81, 0x02, /* Input (Variable), */
125 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
126 0x09, 0x20, /* Usage (20h), */
127 0x75, 0x06, /* Report Size (6), */
128 0x95, 0x01, /* Report Count (1), */
129 0x15, 0x00, /* Logical Minimum (0), */
130 0x25, 0x7F, /* Logical Maximum (127), */
131 0x81, 0x02, /* Input (Variable), */
132 0x05, 0x01, /* Usage Page (Desktop), */
133 0x09, 0x33, /* Usage (Rx), */
134 0x09, 0x34, /* Usage (Ry), */
135 0x15, 0x00, /* Logical Minimum (0), */
136 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
137 0x75, 0x08, /* Report Size (8), */
138 0x95, 0x02, /* Report Count (2), */
139 0x81, 0x02, /* Input (Variable), */
140 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
141 0x09, 0x21, /* Usage (21h), */
142 0x95, 0x03, /* Report Count (3), */
143 0x81, 0x02, /* Input (Variable), */
144 0x05, 0x01, /* Usage Page (Desktop), */
145 0x19, 0x40, /* Usage Minimum (40h), */
146 0x29, 0x42, /* Usage Maximum (42h), */
147 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
148 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
149 0x75, 0x10, /* Report Size (16), */
150 0x95, 0x03, /* Report Count (3), */
151 0x81, 0x02, /* Input (Variable), */
152 0x19, 0x43, /* Usage Minimum (43h), */
153 0x29, 0x45, /* Usage Maximum (45h), */
154 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
155 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
156 0x95, 0x03, /* Report Count (3), */
157 0x81, 0x02, /* Input (Variable), */
158 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
159 0x09, 0x21, /* Usage (21h), */
160 0x15, 0x00, /* Logical Minimum (0), */
161 0x25, 0xFF, /* Logical Maximum (255), */
162 0x75, 0x08, /* Report Size (8), */
163 0x95, 0x27, /* Report Count (39), */
164 0x81, 0x02, /* Input (Variable), */
165 0x85, 0x05, /* Report ID (5), */
166 0x09, 0x22, /* Usage (22h), */
167 0x95, 0x1F, /* Report Count (31), */
168 0x91, 0x02, /* Output (Variable), */
169 0x85, 0x04, /* Report ID (4), */
170 0x09, 0x23, /* Usage (23h), */
171 0x95, 0x24, /* Report Count (36), */
172 0xB1, 0x02, /* Feature (Variable), */
173 0x85, 0x02, /* Report ID (2), */
174 0x09, 0x24, /* Usage (24h), */
175 0x95, 0x24, /* Report Count (36), */
176 0xB1, 0x02, /* Feature (Variable), */
177 0x85, 0x08, /* Report ID (8), */
178 0x09, 0x25, /* Usage (25h), */
179 0x95, 0x03, /* Report Count (3), */
180 0xB1, 0x02, /* Feature (Variable), */
181 0x85, 0x10, /* Report ID (16), */
182 0x09, 0x26, /* Usage (26h), */
183 0x95, 0x04, /* Report Count (4), */
184 0xB1, 0x02, /* Feature (Variable), */
185 0x85, 0x11, /* Report ID (17), */
186 0x09, 0x27, /* Usage (27h), */
187 0x95, 0x02, /* Report Count (2), */
188 0xB1, 0x02, /* Feature (Variable), */
189 0x85, 0x12, /* Report ID (18), */
190 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
191 0x09, 0x21, /* Usage (21h), */
192 0x95, 0x0F, /* Report Count (15), */
193 0xB1, 0x02, /* Feature (Variable), */
194 0x85, 0x13, /* Report ID (19), */
195 0x09, 0x22, /* Usage (22h), */
196 0x95, 0x16, /* Report Count (22), */
197 0xB1, 0x02, /* Feature (Variable), */
198 0x85, 0x14, /* Report ID (20), */
199 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
200 0x09, 0x20, /* Usage (20h), */
201 0x95, 0x10, /* Report Count (16), */
202 0xB1, 0x02, /* Feature (Variable), */
203 0x85, 0x15, /* Report ID (21), */
204 0x09, 0x21, /* Usage (21h), */
205 0x95, 0x2C, /* Report Count (44), */
206 0xB1, 0x02, /* Feature (Variable), */
207 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
208 0x85, 0x80, /* Report ID (128), */
209 0x09, 0x20, /* Usage (20h), */
210 0x95, 0x06, /* Report Count (6), */
211 0xB1, 0x02, /* Feature (Variable), */
212 0x85, 0x81, /* Report ID (129), */
213 0x09, 0x21, /* Usage (21h), */
214 0x95, 0x06, /* Report Count (6), */
215 0xB1, 0x02, /* Feature (Variable), */
216 0x85, 0x82, /* Report ID (130), */
217 0x09, 0x22, /* Usage (22h), */
218 0x95, 0x05, /* Report Count (5), */
219 0xB1, 0x02, /* Feature (Variable), */
220 0x85, 0x83, /* Report ID (131), */
221 0x09, 0x23, /* Usage (23h), */
222 0x95, 0x01, /* Report Count (1), */
223 0xB1, 0x02, /* Feature (Variable), */
224 0x85, 0x84, /* Report ID (132), */
225 0x09, 0x24, /* Usage (24h), */
226 0x95, 0x04, /* Report Count (4), */
227 0xB1, 0x02, /* Feature (Variable), */
228 0x85, 0x85, /* Report ID (133), */
229 0x09, 0x25, /* Usage (25h), */
230 0x95, 0x06, /* Report Count (6), */
231 0xB1, 0x02, /* Feature (Variable), */
232 0x85, 0x86, /* Report ID (134), */
233 0x09, 0x26, /* Usage (26h), */
234 0x95, 0x06, /* Report Count (6), */
235 0xB1, 0x02, /* Feature (Variable), */
236 0x85, 0x87, /* Report ID (135), */
237 0x09, 0x27, /* Usage (27h), */
238 0x95, 0x23, /* Report Count (35), */
239 0xB1, 0x02, /* Feature (Variable), */
240 0x85, 0x88, /* Report ID (136), */
241 0x09, 0x28, /* Usage (28h), */
242 0x95, 0x22, /* Report Count (34), */
243 0xB1, 0x02, /* Feature (Variable), */
244 0x85, 0x89, /* Report ID (137), */
245 0x09, 0x29, /* Usage (29h), */
246 0x95, 0x02, /* Report Count (2), */
247 0xB1, 0x02, /* Feature (Variable), */
248 0x85, 0x90, /* Report ID (144), */
249 0x09, 0x30, /* Usage (30h), */
250 0x95, 0x05, /* Report Count (5), */
251 0xB1, 0x02, /* Feature (Variable), */
252 0x85, 0x91, /* Report ID (145), */
253 0x09, 0x31, /* Usage (31h), */
254 0x95, 0x03, /* Report Count (3), */
255 0xB1, 0x02, /* Feature (Variable), */
256 0x85, 0x92, /* Report ID (146), */
257 0x09, 0x32, /* Usage (32h), */
258 0x95, 0x03, /* Report Count (3), */
259 0xB1, 0x02, /* Feature (Variable), */
260 0x85, 0x93, /* Report ID (147), */
261 0x09, 0x33, /* Usage (33h), */
262 0x95, 0x0C, /* Report Count (12), */
263 0xB1, 0x02, /* Feature (Variable), */
264 0x85, 0xA0, /* Report ID (160), */
265 0x09, 0x40, /* Usage (40h), */
266 0x95, 0x06, /* Report Count (6), */
267 0xB1, 0x02, /* Feature (Variable), */
268 0x85, 0xA1, /* Report ID (161), */
269 0x09, 0x41, /* Usage (41h), */
270 0x95, 0x01, /* Report Count (1), */
271 0xB1, 0x02, /* Feature (Variable), */
272 0x85, 0xA2, /* Report ID (162), */
273 0x09, 0x42, /* Usage (42h), */
274 0x95, 0x01, /* Report Count (1), */
275 0xB1, 0x02, /* Feature (Variable), */
276 0x85, 0xA3, /* Report ID (163), */
277 0x09, 0x43, /* Usage (43h), */
278 0x95, 0x30, /* Report Count (48), */
279 0xB1, 0x02, /* Feature (Variable), */
280 0x85, 0xA4, /* Report ID (164), */
281 0x09, 0x44, /* Usage (44h), */
282 0x95, 0x0D, /* Report Count (13), */
283 0xB1, 0x02, /* Feature (Variable), */
284 0x85, 0xA5, /* Report ID (165), */
285 0x09, 0x45, /* Usage (45h), */
286 0x95, 0x15, /* Report Count (21), */
287 0xB1, 0x02, /* Feature (Variable), */
288 0x85, 0xA6, /* Report ID (166), */
289 0x09, 0x46, /* Usage (46h), */
290 0x95, 0x15, /* Report Count (21), */
291 0xB1, 0x02, /* Feature (Variable), */
292 0x85, 0xF0, /* Report ID (240), */
293 0x09, 0x47, /* Usage (47h), */
294 0x95, 0x3F, /* Report Count (63), */
295 0xB1, 0x02, /* Feature (Variable), */
296 0x85, 0xF1, /* Report ID (241), */
297 0x09, 0x48, /* Usage (48h), */
298 0x95, 0x3F, /* Report Count (63), */
299 0xB1, 0x02, /* Feature (Variable), */
300 0x85, 0xF2, /* Report ID (242), */
301 0x09, 0x49, /* Usage (49h), */
302 0x95, 0x0F, /* Report Count (15), */
303 0xB1, 0x02, /* Feature (Variable), */
304 0x85, 0xA7, /* Report ID (167), */
305 0x09, 0x4A, /* Usage (4Ah), */
306 0x95, 0x01, /* Report Count (1), */
307 0xB1, 0x02, /* Feature (Variable), */
308 0x85, 0xA8, /* Report ID (168), */
309 0x09, 0x4B, /* Usage (4Bh), */
310 0x95, 0x01, /* Report Count (1), */
311 0xB1, 0x02, /* Feature (Variable), */
312 0x85, 0xA9, /* Report ID (169), */
313 0x09, 0x4C, /* Usage (4Ch), */
314 0x95, 0x08, /* Report Count (8), */
315 0xB1, 0x02, /* Feature (Variable), */
316 0x85, 0xAA, /* Report ID (170), */
317 0x09, 0x4E, /* Usage (4Eh), */
318 0x95, 0x01, /* Report Count (1), */
319 0xB1, 0x02, /* Feature (Variable), */
320 0x85, 0xAB, /* Report ID (171), */
321 0x09, 0x4F, /* Usage (4Fh), */
322 0x95, 0x39, /* Report Count (57), */
323 0xB1, 0x02, /* Feature (Variable), */
324 0x85, 0xAC, /* Report ID (172), */
325 0x09, 0x50, /* Usage (50h), */
326 0x95, 0x39, /* Report Count (57), */
327 0xB1, 0x02, /* Feature (Variable), */
328 0x85, 0xAD, /* Report ID (173), */
329 0x09, 0x51, /* Usage (51h), */
330 0x95, 0x0B, /* Report Count (11), */
331 0xB1, 0x02, /* Feature (Variable), */
332 0x85, 0xAE, /* Report ID (174), */
333 0x09, 0x52, /* Usage (52h), */
334 0x95, 0x01, /* Report Count (1), */
335 0xB1, 0x02, /* Feature (Variable), */
336 0x85, 0xAF, /* Report ID (175), */
337 0x09, 0x53, /* Usage (53h), */
338 0x95, 0x02, /* Report Count (2), */
339 0xB1, 0x02, /* Feature (Variable), */
340 0x85, 0xB0, /* Report ID (176), */
341 0x09, 0x54, /* Usage (54h), */
342 0x95, 0x3F, /* Report Count (63), */
343 0xB1, 0x02, /* Feature (Variable), */
344 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500345};
346
Frank Praznikad142b92014-02-20 11:36:00 -0500347/*
348 * The default behavior of the Dualshock 4 is to send reports using report
Frank Praznikd8296742014-02-05 20:03:45 -0500349 * type 1 when running over Bluetooth. However, as soon as it receives a
350 * report of type 17 to set the LEDs or rumble it starts returning it's state
351 * in report 17 instead of 1. Since report 17 is undefined in the default HID
352 * descriptor the button and axis definitions must be moved to report 17 or
353 * the HID layer won't process the received input once a report is sent.
354 */
355static u8 dualshock4_bt_rdesc[] = {
356 0x05, 0x01, /* Usage Page (Desktop), */
357 0x09, 0x05, /* Usage (Gamepad), */
358 0xA1, 0x01, /* Collection (Application), */
359 0x85, 0x01, /* Report ID (1), */
360 0x75, 0x08, /* Report Size (8), */
361 0x95, 0x0A, /* Report Count (9), */
362 0x81, 0x02, /* Input (Variable), */
363 0x06, 0x04, 0xFF, /* Usage Page (FF04h), */
364 0x85, 0x02, /* Report ID (2), */
365 0x09, 0x24, /* Usage (24h), */
366 0x95, 0x24, /* Report Count (36), */
367 0xB1, 0x02, /* Feature (Variable), */
368 0x85, 0xA3, /* Report ID (163), */
369 0x09, 0x25, /* Usage (25h), */
370 0x95, 0x30, /* Report Count (48), */
371 0xB1, 0x02, /* Feature (Variable), */
372 0x85, 0x05, /* Report ID (5), */
373 0x09, 0x26, /* Usage (26h), */
374 0x95, 0x28, /* Report Count (40), */
375 0xB1, 0x02, /* Feature (Variable), */
376 0x85, 0x06, /* Report ID (6), */
377 0x09, 0x27, /* Usage (27h), */
378 0x95, 0x34, /* Report Count (52), */
379 0xB1, 0x02, /* Feature (Variable), */
380 0x85, 0x07, /* Report ID (7), */
381 0x09, 0x28, /* Usage (28h), */
382 0x95, 0x30, /* Report Count (48), */
383 0xB1, 0x02, /* Feature (Variable), */
384 0x85, 0x08, /* Report ID (8), */
385 0x09, 0x29, /* Usage (29h), */
386 0x95, 0x2F, /* Report Count (47), */
387 0xB1, 0x02, /* Feature (Variable), */
388 0x06, 0x03, 0xFF, /* Usage Page (FF03h), */
389 0x85, 0x03, /* Report ID (3), */
390 0x09, 0x21, /* Usage (21h), */
391 0x95, 0x26, /* Report Count (38), */
392 0xB1, 0x02, /* Feature (Variable), */
393 0x85, 0x04, /* Report ID (4), */
394 0x09, 0x22, /* Usage (22h), */
395 0x95, 0x2E, /* Report Count (46), */
396 0xB1, 0x02, /* Feature (Variable), */
397 0x85, 0xF0, /* Report ID (240), */
398 0x09, 0x47, /* Usage (47h), */
399 0x95, 0x3F, /* Report Count (63), */
400 0xB1, 0x02, /* Feature (Variable), */
401 0x85, 0xF1, /* Report ID (241), */
402 0x09, 0x48, /* Usage (48h), */
403 0x95, 0x3F, /* Report Count (63), */
404 0xB1, 0x02, /* Feature (Variable), */
405 0x85, 0xF2, /* Report ID (242), */
406 0x09, 0x49, /* Usage (49h), */
407 0x95, 0x0F, /* Report Count (15), */
408 0xB1, 0x02, /* Feature (Variable), */
409 0x85, 0x11, /* Report ID (17), */
410 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
411 0x09, 0x20, /* Usage (20h), */
412 0x95, 0x02, /* Report Count (2), */
413 0x81, 0x02, /* Input (Variable), */
414 0x05, 0x01, /* Usage Page (Desktop), */
415 0x09, 0x30, /* Usage (X), */
416 0x09, 0x31, /* Usage (Y), */
417 0x09, 0x32, /* Usage (Z), */
418 0x09, 0x35, /* Usage (Rz), */
419 0x15, 0x00, /* Logical Minimum (0), */
420 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
421 0x75, 0x08, /* Report Size (8), */
422 0x95, 0x04, /* Report Count (4), */
423 0x81, 0x02, /* Input (Variable), */
424 0x09, 0x39, /* Usage (Hat Switch), */
425 0x15, 0x00, /* Logical Minimum (0), */
426 0x25, 0x07, /* Logical Maximum (7), */
427 0x75, 0x04, /* Report Size (4), */
428 0x95, 0x01, /* Report Count (1), */
429 0x81, 0x42, /* Input (Variable, Null State), */
430 0x05, 0x09, /* Usage Page (Button), */
431 0x19, 0x01, /* Usage Minimum (01h), */
432 0x29, 0x0E, /* Usage Maximum (0Eh), */
433 0x15, 0x00, /* Logical Minimum (0), */
434 0x25, 0x01, /* Logical Maximum (1), */
435 0x75, 0x01, /* Report Size (1), */
436 0x95, 0x0E, /* Report Count (14), */
437 0x81, 0x02, /* Input (Variable), */
438 0x75, 0x06, /* Report Size (6), */
439 0x95, 0x01, /* Report Count (1), */
440 0x81, 0x01, /* Input (Constant), */
441 0x05, 0x01, /* Usage Page (Desktop), */
442 0x09, 0x33, /* Usage (Rx), */
443 0x09, 0x34, /* Usage (Ry), */
444 0x15, 0x00, /* Logical Minimum (0), */
445 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
446 0x75, 0x08, /* Report Size (8), */
447 0x95, 0x02, /* Report Count (2), */
448 0x81, 0x02, /* Input (Variable), */
449 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
450 0x09, 0x20, /* Usage (20h), */
451 0x95, 0x03, /* Report Count (3), */
452 0x81, 0x02, /* Input (Variable), */
453 0x05, 0x01, /* Usage Page (Desktop), */
454 0x19, 0x40, /* Usage Minimum (40h), */
455 0x29, 0x42, /* Usage Maximum (42h), */
456 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
457 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
458 0x75, 0x10, /* Report Size (16), */
459 0x95, 0x03, /* Report Count (3), */
460 0x81, 0x02, /* Input (Variable), */
461 0x19, 0x43, /* Usage Minimum (43h), */
462 0x29, 0x45, /* Usage Maximum (45h), */
463 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
464 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
465 0x95, 0x03, /* Report Count (3), */
466 0x81, 0x02, /* Input (Variable), */
467 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
468 0x09, 0x20, /* Usage (20h), */
469 0x15, 0x00, /* Logical Minimum (0), */
470 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
471 0x75, 0x08, /* Report Size (8), */
472 0x95, 0x31, /* Report Count (51), */
473 0x81, 0x02, /* Input (Variable), */
474 0x09, 0x21, /* Usage (21h), */
475 0x75, 0x08, /* Report Size (8), */
476 0x95, 0x4D, /* Report Count (77), */
477 0x91, 0x02, /* Output (Variable), */
478 0x85, 0x12, /* Report ID (18), */
479 0x09, 0x22, /* Usage (22h), */
480 0x95, 0x8D, /* Report Count (141), */
481 0x81, 0x02, /* Input (Variable), */
482 0x09, 0x23, /* Usage (23h), */
483 0x91, 0x02, /* Output (Variable), */
484 0x85, 0x13, /* Report ID (19), */
485 0x09, 0x24, /* Usage (24h), */
486 0x95, 0xCD, /* Report Count (205), */
487 0x81, 0x02, /* Input (Variable), */
488 0x09, 0x25, /* Usage (25h), */
489 0x91, 0x02, /* Output (Variable), */
490 0x85, 0x14, /* Report ID (20), */
491 0x09, 0x26, /* Usage (26h), */
492 0x96, 0x0D, 0x01, /* Report Count (269), */
493 0x81, 0x02, /* Input (Variable), */
494 0x09, 0x27, /* Usage (27h), */
495 0x91, 0x02, /* Output (Variable), */
496 0x85, 0x15, /* Report ID (21), */
497 0x09, 0x28, /* Usage (28h), */
498 0x96, 0x4D, 0x01, /* Report Count (333), */
499 0x81, 0x02, /* Input (Variable), */
500 0x09, 0x29, /* Usage (29h), */
501 0x91, 0x02, /* Output (Variable), */
502 0x85, 0x16, /* Report ID (22), */
503 0x09, 0x2A, /* Usage (2Ah), */
504 0x96, 0x8D, 0x01, /* Report Count (397), */
505 0x81, 0x02, /* Input (Variable), */
506 0x09, 0x2B, /* Usage (2Bh), */
507 0x91, 0x02, /* Output (Variable), */
508 0x85, 0x17, /* Report ID (23), */
509 0x09, 0x2C, /* Usage (2Ch), */
510 0x96, 0xCD, 0x01, /* Report Count (461), */
511 0x81, 0x02, /* Input (Variable), */
512 0x09, 0x2D, /* Usage (2Dh), */
513 0x91, 0x02, /* Output (Variable), */
514 0x85, 0x18, /* Report ID (24), */
515 0x09, 0x2E, /* Usage (2Eh), */
516 0x96, 0x0D, 0x02, /* Report Count (525), */
517 0x81, 0x02, /* Input (Variable), */
518 0x09, 0x2F, /* Usage (2Fh), */
519 0x91, 0x02, /* Output (Variable), */
520 0x85, 0x19, /* Report ID (25), */
521 0x09, 0x30, /* Usage (30h), */
522 0x96, 0x22, 0x02, /* Report Count (546), */
523 0x81, 0x02, /* Input (Variable), */
524 0x09, 0x31, /* Usage (31h), */
525 0x91, 0x02, /* Output (Variable), */
526 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
527 0x85, 0x82, /* Report ID (130), */
528 0x09, 0x22, /* Usage (22h), */
529 0x95, 0x3F, /* Report Count (63), */
530 0xB1, 0x02, /* Feature (Variable), */
531 0x85, 0x83, /* Report ID (131), */
532 0x09, 0x23, /* Usage (23h), */
533 0xB1, 0x02, /* Feature (Variable), */
534 0x85, 0x84, /* Report ID (132), */
535 0x09, 0x24, /* Usage (24h), */
536 0xB1, 0x02, /* Feature (Variable), */
537 0x85, 0x90, /* Report ID (144), */
538 0x09, 0x30, /* Usage (30h), */
539 0xB1, 0x02, /* Feature (Variable), */
540 0x85, 0x91, /* Report ID (145), */
541 0x09, 0x31, /* Usage (31h), */
542 0xB1, 0x02, /* Feature (Variable), */
543 0x85, 0x92, /* Report ID (146), */
544 0x09, 0x32, /* Usage (32h), */
545 0xB1, 0x02, /* Feature (Variable), */
546 0x85, 0x93, /* Report ID (147), */
547 0x09, 0x33, /* Usage (33h), */
548 0xB1, 0x02, /* Feature (Variable), */
549 0x85, 0xA0, /* Report ID (160), */
550 0x09, 0x40, /* Usage (40h), */
551 0xB1, 0x02, /* Feature (Variable), */
552 0x85, 0xA4, /* Report ID (164), */
553 0x09, 0x44, /* Usage (44h), */
554 0xB1, 0x02, /* Feature (Variable), */
555 0xC0 /* End Collection */
556};
557
Jiri Kosina078328d2013-06-13 12:03:49 +0200558static __u8 ps3remote_rdesc[] = {
559 0x05, 0x01, /* GUsagePage Generic Desktop */
560 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
561 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
562
563 /* Use collection 1 for joypad buttons */
564 0xA1, 0x02, /* MCollection Logical (interrelated data) */
565
566 /* Ignore the 1st byte, maybe it is used for a controller
567 * number but it's not needed for correct operation */
568 0x75, 0x08, /* GReportSize 0x08 [8] */
569 0x95, 0x01, /* GReportCount 0x01 [1] */
570 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
571
572 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
573 * buttons multiple keypresses are allowed */
574 0x05, 0x09, /* GUsagePage Button */
575 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
576 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
577 0x14, /* GLogicalMinimum [0] */
578 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
579 0x75, 0x01, /* GReportSize 0x01 [1] */
580 0x95, 0x18, /* GReportCount 0x18 [24] */
581 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
582
583 0xC0, /* MEndCollection */
584
585 /* Use collection 2 for remote control buttons */
586 0xA1, 0x02, /* MCollection Logical (interrelated data) */
587
588 /* 5th byte is used for remote control buttons */
589 0x05, 0x09, /* GUsagePage Button */
590 0x18, /* LUsageMinimum [No button pressed] */
591 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
592 0x14, /* GLogicalMinimum [0] */
593 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
594 0x75, 0x08, /* GReportSize 0x08 [8] */
595 0x95, 0x01, /* GReportCount 0x01 [1] */
596 0x80, /* MInput */
597
598 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
599 * 0xff and 11th is for press indication */
600 0x75, 0x08, /* GReportSize 0x08 [8] */
601 0x95, 0x06, /* GReportCount 0x06 [6] */
602 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
603
604 /* 12th byte is for battery strength */
605 0x05, 0x06, /* GUsagePage Generic Device Controls */
606 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
607 0x14, /* GLogicalMinimum [0] */
608 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
609 0x75, 0x08, /* GReportSize 0x08 [8] */
610 0x95, 0x01, /* GReportCount 0x01 [1] */
611 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
612
613 0xC0, /* MEndCollection */
614
615 0xC0 /* MEndCollection [Game Pad] */
616};
617
618static const unsigned int ps3remote_keymap_joypad_buttons[] = {
619 [0x01] = KEY_SELECT,
620 [0x02] = BTN_THUMBL, /* L3 */
621 [0x03] = BTN_THUMBR, /* R3 */
622 [0x04] = BTN_START,
623 [0x05] = KEY_UP,
624 [0x06] = KEY_RIGHT,
625 [0x07] = KEY_DOWN,
626 [0x08] = KEY_LEFT,
627 [0x09] = BTN_TL2, /* L2 */
628 [0x0a] = BTN_TR2, /* R2 */
629 [0x0b] = BTN_TL, /* L1 */
630 [0x0c] = BTN_TR, /* R1 */
631 [0x0d] = KEY_OPTION, /* options/triangle */
632 [0x0e] = KEY_BACK, /* back/circle */
633 [0x0f] = BTN_0, /* cross */
634 [0x10] = KEY_SCREEN, /* view/square */
635 [0x11] = KEY_HOMEPAGE, /* PS button */
636 [0x14] = KEY_ENTER,
637};
638static const unsigned int ps3remote_keymap_remote_buttons[] = {
639 [0x00] = KEY_1,
640 [0x01] = KEY_2,
641 [0x02] = KEY_3,
642 [0x03] = KEY_4,
643 [0x04] = KEY_5,
644 [0x05] = KEY_6,
645 [0x06] = KEY_7,
646 [0x07] = KEY_8,
647 [0x08] = KEY_9,
648 [0x09] = KEY_0,
649 [0x0e] = KEY_ESC, /* return */
650 [0x0f] = KEY_CLEAR,
651 [0x16] = KEY_EJECTCD,
652 [0x1a] = KEY_MENU, /* top menu */
653 [0x28] = KEY_TIME,
654 [0x30] = KEY_PREVIOUS,
655 [0x31] = KEY_NEXT,
656 [0x32] = KEY_PLAY,
657 [0x33] = KEY_REWIND, /* scan back */
658 [0x34] = KEY_FORWARD, /* scan forward */
659 [0x38] = KEY_STOP,
660 [0x39] = KEY_PAUSE,
661 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
662 [0x60] = KEY_FRAMEBACK, /* slow/step back */
663 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
664 [0x63] = KEY_SUBTITLE,
665 [0x64] = KEY_AUDIO,
666 [0x65] = KEY_ANGLE,
667 [0x70] = KEY_INFO, /* display */
668 [0x80] = KEY_BLUE,
669 [0x81] = KEY_RED,
670 [0x82] = KEY_GREEN,
671 [0x83] = KEY_YELLOW,
672};
673
Colin Leitnerf04d5142013-05-27 23:41:05 +0200674static const unsigned int buzz_keymap[] = {
Frank Praznikad142b92014-02-20 11:36:00 -0500675 /*
676 * The controller has 4 remote buzzers, each with one LED and 5
Colin Leitnerf04d5142013-05-27 23:41:05 +0200677 * buttons.
678 *
679 * We use the mapping chosen by the controller, which is:
680 *
681 * Key Offset
682 * -------------------
683 * Buzz 1
684 * Blue 5
685 * Orange 4
686 * Green 3
687 * Yellow 2
688 *
689 * So, for example, the orange button on the third buzzer is mapped to
690 * BTN_TRIGGER_HAPPY14
691 */
692 [ 1] = BTN_TRIGGER_HAPPY1,
693 [ 2] = BTN_TRIGGER_HAPPY2,
694 [ 3] = BTN_TRIGGER_HAPPY3,
695 [ 4] = BTN_TRIGGER_HAPPY4,
696 [ 5] = BTN_TRIGGER_HAPPY5,
697 [ 6] = BTN_TRIGGER_HAPPY6,
698 [ 7] = BTN_TRIGGER_HAPPY7,
699 [ 8] = BTN_TRIGGER_HAPPY8,
700 [ 9] = BTN_TRIGGER_HAPPY9,
701 [10] = BTN_TRIGGER_HAPPY10,
702 [11] = BTN_TRIGGER_HAPPY11,
703 [12] = BTN_TRIGGER_HAPPY12,
704 [13] = BTN_TRIGGER_HAPPY13,
705 [14] = BTN_TRIGGER_HAPPY14,
706 [15] = BTN_TRIGGER_HAPPY15,
707 [16] = BTN_TRIGGER_HAPPY16,
708 [17] = BTN_TRIGGER_HAPPY17,
709 [18] = BTN_TRIGGER_HAPPY18,
710 [19] = BTN_TRIGGER_HAPPY19,
711 [20] = BTN_TRIGGER_HAPPY20,
712};
713
Frank Praznikd902f472014-01-27 10:17:36 -0500714static enum power_supply_property sony_battery_props[] = {
715 POWER_SUPPLY_PROP_PRESENT,
716 POWER_SUPPLY_PROP_CAPACITY,
717 POWER_SUPPLY_PROP_SCOPE,
718 POWER_SUPPLY_PROP_STATUS,
719};
720
Frank Praznikd2d782f2014-02-20 11:36:03 -0500721static spinlock_t sony_dev_list_lock;
722static LIST_HEAD(sony_device_list);
723
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200724struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500725 spinlock_t lock;
Frank Praznikd2d782f2014-02-20 11:36:03 -0500726 struct list_head list_node;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100727 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500728 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200729 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100730 struct work_struct state_worker;
Frank Praznikd902f472014-01-27 10:17:36 -0500731 struct power_supply battery;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200732
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100733#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100734 __u8 left;
735 __u8 right;
736#endif
737
Frank Praznikd2d782f2014-02-20 11:36:03 -0500738 __u8 mac_address[6];
Frank Praznikc8de9db2014-02-20 11:36:01 -0500739 __u8 worker_initialized;
Frank Praznikd902f472014-01-27 10:17:36 -0500740 __u8 cable_state;
741 __u8 battery_charging;
742 __u8 battery_capacity;
Frank Praznik60781cf2014-01-11 15:13:15 -0500743 __u8 led_state[MAX_LEDS];
744 __u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200745};
746
Jiri Kosina078328d2013-06-13 12:03:49 +0200747static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
748 unsigned int *rsize)
749{
750 *rsize = sizeof(ps3remote_rdesc);
751 return ps3remote_rdesc;
752}
753
754static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
755 struct hid_field *field, struct hid_usage *usage,
756 unsigned long **bit, int *max)
757{
758 unsigned int key = usage->hid & HID_USAGE;
759
760 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
761 return -1;
762
763 switch (usage->collection_index) {
764 case 1:
765 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
766 return -1;
767
768 key = ps3remote_keymap_joypad_buttons[key];
769 if (!key)
770 return -1;
771 break;
772 case 2:
773 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
774 return -1;
775
776 key = ps3remote_keymap_remote_buttons[key];
777 if (!key)
778 return -1;
779 break;
780 default:
781 return -1;
782 }
783
784 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
785 return 1;
786}
787
788
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200789/* Sony Vaio VGX has wrongly mouse pointer declared as constant */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400790static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
791 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200792{
793 struct sony_sc *sc = hid_get_drvdata(hdev);
794
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900795 /*
796 * Some Sony RF receivers wrongly declare the mouse pointer as a
797 * a constant non-data variable.
798 */
799 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
800 /* usage page: generic desktop controls */
801 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
802 /* usage: mouse */
803 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
804 /* input (usage page for x,y axes): constant, variable, relative */
805 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +0900806 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900807 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200808 rdesc[55] = 0x06;
809 }
Simon Wood61ab44b2011-06-10 12:00:26 +0200810
Frank Prazniked19d8c2014-01-16 21:43:12 -0500811 /*
812 * The default Dualshock 4 USB descriptor doesn't assign
813 * the gyroscope values to corresponding axes so we need a
814 * modified one.
815 */
816 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
817 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
818 rdesc = dualshock4_usb_rdesc;
819 *rsize = sizeof(dualshock4_usb_rdesc);
Frank Praznikd8296742014-02-05 20:03:45 -0500820 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && *rsize == 357) {
821 hid_info(hdev, "Using modified Dualshock 4 Bluetooth report descriptor\n");
822 rdesc = dualshock4_bt_rdesc;
823 *rsize = sizeof(dualshock4_bt_rdesc);
Frank Prazniked19d8c2014-01-16 21:43:12 -0500824 }
825
Simon Wood61ab44b2011-06-10 12:00:26 +0200826 /* The HID descriptor exposed over BT has a trailing zero byte */
827 if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
828 ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
829 rdesc[83] == 0x75) {
830 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
831 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
832 sizeof(sixaxis_rdesc_fixup));
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200833 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
834 *rsize > sizeof(sixaxis_rdesc_fixup2)) {
835 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
836 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
837 *rsize = sizeof(sixaxis_rdesc_fixup2);
838 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
Simon Wood61ab44b2011-06-10 12:00:26 +0200839 }
Jiri Kosina078328d2013-06-13 12:03:49 +0200840
841 if (sc->quirks & PS3REMOTE)
842 return ps3remote_fixup(hdev, rdesc, rsize);
843
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400844 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200845}
846
Frank Praznikd902f472014-01-27 10:17:36 -0500847static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
848{
849 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
850 unsigned long flags;
851 __u8 cable_state, battery_capacity, battery_charging;
852
Frank Praznikad142b92014-02-20 11:36:00 -0500853 /*
854 * The sixaxis is charging if the battery value is 0xee
Frank Praznikd902f472014-01-27 10:17:36 -0500855 * and it is fully charged if the value is 0xef.
856 * It does not report the actual level while charging so it
857 * is set to 100% while charging is in progress.
858 */
859 if (rd[30] >= 0xee) {
860 battery_capacity = 100;
Frank Praznika43e94a2014-02-15 13:35:42 -0500861 battery_charging = !(rd[30] & 0x01);
Frank Praznikd902f472014-01-27 10:17:36 -0500862 } else {
Frank Praznikac3c9a92014-02-20 11:36:02 -0500863 __u8 index = rd[30] <= 5 ? rd[30] : 5;
864 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -0500865 battery_charging = 0;
866 }
Frank Praznik914c5782014-03-15 09:41:15 -0400867 cable_state = !(rd[31] & 0x04);
Frank Praznikd902f472014-01-27 10:17:36 -0500868
869 spin_lock_irqsave(&sc->lock, flags);
870 sc->cable_state = cable_state;
871 sc->battery_capacity = battery_capacity;
872 sc->battery_charging = battery_charging;
873 spin_unlock_irqrestore(&sc->lock, flags);
874}
875
876static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
877{
Frank Praznike5606232014-01-27 10:17:37 -0500878 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
879 struct hid_input, list);
880 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -0500881 unsigned long flags;
Frank Praznik6c5f8602014-02-05 20:03:47 -0500882 int n, offset;
Frank Praznikd902f472014-01-27 10:17:36 -0500883 __u8 cable_state, battery_capacity, battery_charging;
884
Frank Praznikad142b92014-02-20 11:36:00 -0500885 /*
886 * Battery and touchpad data starts at byte 30 in the USB report and
Frank Praznik6c5f8602014-02-05 20:03:47 -0500887 * 32 in Bluetooth report.
888 */
889 offset = (sc->quirks & DUALSHOCK4_CONTROLLER_USB) ? 30 : 32;
890
Frank Praznikad142b92014-02-20 11:36:00 -0500891 /*
892 * The lower 4 bits of byte 30 contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -0500893 * and the 5th bit contains the USB cable state.
894 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500895 cable_state = (rd[offset] >> 4) & 0x01;
896 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -0500897
Frank Praznikad142b92014-02-20 11:36:00 -0500898 /*
899 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -0500900 * 0 to 10, and when running on battery power it ranges from 0 to 9.
901 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -0500902 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500903 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -0500904 battery_charging = 0;
905 else
906 battery_charging = 1;
907
Frank Praznik6c5f8602014-02-05 20:03:47 -0500908 if (!cable_state)
909 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -0500910 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -0500911 battery_capacity = 10;
912
Frank Praznikd902f472014-01-27 10:17:36 -0500913 battery_capacity *= 10;
914
915 spin_lock_irqsave(&sc->lock, flags);
916 sc->cable_state = cable_state;
917 sc->battery_capacity = battery_capacity;
918 sc->battery_charging = battery_charging;
919 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -0500920
Frank Praznik6c5f8602014-02-05 20:03:47 -0500921 offset += 5;
922
Frank Praznikad142b92014-02-20 11:36:00 -0500923 /*
924 * The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB
Frank Praznik6c5f8602014-02-05 20:03:47 -0500925 * and 37 on Bluetooth.
Frank Praznike5606232014-01-27 10:17:37 -0500926 * The first 7 bits of the first byte is a counter and bit 8 is a touch
927 * indicator that is 0 when pressed and 1 when not pressed.
928 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
929 * The data for the second touch is in the same format and immediatly
930 * follows the data for the first.
931 */
932 for (n = 0; n < 2; n++) {
933 __u16 x, y;
934
935 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
936 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
937
938 input_mt_slot(input_dev, n);
939 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
940 !(rd[offset] >> 7));
941 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
942 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
943
944 offset += 4;
945 }
Frank Praznikd902f472014-01-27 10:17:36 -0500946}
947
Simon Woodc9e4d872011-06-10 12:00:27 +0200948static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
949 __u8 *rd, int size)
950{
951 struct sony_sc *sc = hid_get_drvdata(hdev);
952
Frank Praznikad142b92014-02-20 11:36:00 -0500953 /*
954 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +0200955 * has to be BYTE_SWAPPED before passing up to joystick interface
956 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -0500957 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Simon Woodc9e4d872011-06-10 12:00:27 +0200958 swap(rd[41], rd[42]);
959 swap(rd[43], rd[44]);
960 swap(rd[45], rd[46]);
961 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -0500962
963 sixaxis_parse_report(sc, rd, size);
Frank Praznik68330d82014-02-05 20:03:49 -0500964 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
965 size == 64) || ((sc->quirks & DUALSHOCK4_CONTROLLER_BT)
966 && rd[0] == 0x11 && size == 78)) {
Frank Praznikd902f472014-01-27 10:17:36 -0500967 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +0200968 }
969
970 return 0;
971}
972
Colin Leitnerf04d5142013-05-27 23:41:05 +0200973static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
974 struct hid_field *field, struct hid_usage *usage,
975 unsigned long **bit, int *max)
976{
977 struct sony_sc *sc = hid_get_drvdata(hdev);
978
979 if (sc->quirks & BUZZ_CONTROLLER) {
980 unsigned int key = usage->hid & HID_USAGE;
981
982 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
983 return -1;
984
985 switch (usage->collection_index) {
986 case 1:
987 if (key >= ARRAY_SIZE(buzz_keymap))
988 return -1;
989
990 key = buzz_keymap[key];
991 if (!key)
992 return -1;
993 break;
994 default:
995 return -1;
996 }
997
998 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
999 return 1;
1000 }
1001
Jiri Kosina078328d2013-06-13 12:03:49 +02001002 if (sc->quirks & PS3REMOTE)
1003 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1004
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001005 /* Let hid-core decide for the others */
1006 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001007}
1008
Antonio Ospite5710fab2011-02-20 18:26:45 +01001009/*
1010 * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
1011 * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
1012 * so we need to override that forcing HID Output Reports on the Control EP.
1013 *
1014 * There is also another issue about HID Output Reports via USB, the Sixaxis
1015 * does not want the report_id as part of the data packet, so we have to
1016 * discard buf[0] when sending the actual control message, even for numbered
1017 * reports, humpf!
1018 */
Antonio Ospite569b10a2010-10-19 16:13:10 +02001019static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
1020 size_t count, unsigned char report_type)
1021{
1022 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
1023 struct usb_device *dev = interface_to_usbdev(intf);
1024 struct usb_host_interface *interface = intf->cur_altsetting;
1025 int report_id = buf[0];
1026 int ret;
1027
Antonio Ospite5710fab2011-02-20 18:26:45 +01001028 if (report_type == HID_OUTPUT_REPORT) {
1029 /* Don't send the Report ID */
1030 buf++;
1031 count--;
1032 }
1033
Antonio Ospite569b10a2010-10-19 16:13:10 +02001034 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1035 HID_REQ_SET_REPORT,
1036 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1037 ((report_type + 1) << 8) | report_id,
1038 interface->desc.bInterfaceNumber, buf, count,
1039 USB_CTRL_SET_TIMEOUT);
1040
Antonio Ospite5710fab2011-02-20 18:26:45 +01001041 /* Count also the Report ID, in case of an Output report. */
1042 if (ret > 0 && report_type == HID_OUTPUT_REPORT)
1043 ret++;
1044
Antonio Ospite569b10a2010-10-19 16:13:10 +02001045 return ret;
1046}
1047
Jiri Slabybd28ce02008-06-25 23:47:04 +02001048/*
1049 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1050 * to "operational". Without this, the ps3 controller will not report any
1051 * events.
1052 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001053static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001054{
Jiri Slabybd28ce02008-06-25 23:47:04 +02001055 int ret;
1056 char *buf = kmalloc(18, GFP_KERNEL);
1057
1058 if (!buf)
1059 return -ENOMEM;
1060
Benjamin Tissoirescafebc02014-02-05 16:33:22 -05001061 ret = hid_hw_raw_request(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT,
1062 HID_REQ_GET_REPORT);
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001063
Jiri Slabybd28ce02008-06-25 23:47:04 +02001064 if (ret < 0)
Joe Perches4291ee32010-12-09 19:29:03 -08001065 hid_err(hdev, "can't set operational mode\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +02001066
1067 kfree(buf);
1068
1069 return ret;
1070}
1071
Antonio Ospite816651a2010-05-03 22:15:55 +02001072static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001073{
Antonio Ospitefddb33f2010-05-03 17:19:03 +02001074 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
Benjamin Tissoires7e845d42014-02-05 16:33:23 -05001075 return hid_output_raw_report(hdev, buf, sizeof(buf),
1076 HID_FEATURE_REPORT);
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001077}
1078
Frank Praznikad142b92014-02-20 11:36:00 -05001079/*
1080 * Requesting feature report 0x02 in Bluetooth mode changes the state of the
Frank Praznik68330d82014-02-05 20:03:49 -05001081 * controller so that it sends full input reports of type 0x11.
1082 */
1083static int dualshock4_set_operational_bt(struct hid_device *hdev)
1084{
1085 __u8 buf[37] = { 0 };
1086
1087 return hid_hw_raw_request(hdev, 0x02, buf, sizeof(buf),
1088 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1089}
1090
Frank Praznik60781cf2014-01-11 15:13:15 -05001091static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001092{
1093 struct list_head *report_list =
1094 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1095 struct hid_report *report = list_entry(report_list->next,
1096 struct hid_report, list);
1097 __s32 *value = report->field[0]->value;
1098
1099 value[0] = 0x00;
Frank Praznik60781cf2014-01-11 15:13:15 -05001100 value[1] = leds[0] ? 0xff : 0x00;
1101 value[2] = leds[1] ? 0xff : 0x00;
1102 value[3] = leds[2] ? 0xff : 0x00;
1103 value[4] = leds[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001104 value[5] = 0x00;
1105 value[6] = 0x00;
1106 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1107}
1108
Frank Praznik60781cf2014-01-11 15:13:15 -05001109static void sony_set_leds(struct hid_device *hdev, const __u8 *leds, int count)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001110{
1111 struct sony_sc *drv_data = hid_get_drvdata(hdev);
Frank Praznik60781cf2014-01-11 15:13:15 -05001112 int n;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001113
Frank Praznik60781cf2014-01-11 15:13:15 -05001114 BUG_ON(count > MAX_LEDS);
1115
1116 if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001117 buzz_set_leds(hdev, leds);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001118 } else {
Frank Praznik60781cf2014-01-11 15:13:15 -05001119 for (n = 0; n < count; n++)
1120 drv_data->led_state[n] = leds[n];
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001121 schedule_work(&drv_data->state_worker);
1122 }
1123}
1124
Sven Eckelmannc5382512013-11-19 20:26:30 +01001125static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001126 enum led_brightness value)
1127{
1128 struct device *dev = led->dev->parent;
1129 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1130 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001131
1132 int n;
1133
1134 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001135 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001136 hid_err(hdev, "No device data\n");
1137 return;
1138 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001139
Frank Praznik60781cf2014-01-11 15:13:15 -05001140 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann2251b852013-11-19 20:26:31 +01001141 if (led == drv_data->leds[n]) {
Frank Praznik60781cf2014-01-11 15:13:15 -05001142 if (value != drv_data->led_state[n]) {
1143 drv_data->led_state[n] = value;
1144 sony_set_leds(hdev, drv_data->led_state, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001145 }
1146 break;
1147 }
1148 }
1149}
1150
Sven Eckelmannc5382512013-11-19 20:26:30 +01001151static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001152{
1153 struct device *dev = led->dev->parent;
1154 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
1155 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001156
1157 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001158
1159 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001160 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001161 hid_err(hdev, "No device data\n");
1162 return LED_OFF;
1163 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001164
Frank Praznik60781cf2014-01-11 15:13:15 -05001165 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001166 if (led == drv_data->leds[n])
1167 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001168 }
1169
Simon Wood7db75042014-02-05 12:34:18 -07001170 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001171}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001172
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001173static void sony_leds_remove(struct hid_device *hdev)
1174{
1175 struct sony_sc *drv_data;
1176 struct led_classdev *led;
1177 int n;
1178
1179 drv_data = hid_get_drvdata(hdev);
1180 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
1181
Frank Praznik60781cf2014-01-11 15:13:15 -05001182 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001183 led = drv_data->leds[n];
1184 drv_data->leds[n] = NULL;
1185 if (!led)
1186 continue;
1187 led_classdev_unregister(led);
1188 kfree(led);
1189 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001190
1191 drv_data->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001192}
1193
Sven Eckelmannc5382512013-11-19 20:26:30 +01001194static int sony_leds_init(struct hid_device *hdev)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001195{
1196 struct sony_sc *drv_data;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001197 int n, ret = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -05001198 int max_brightness;
Frank Praznik61ebca92014-01-20 12:27:02 -05001199 int use_colors;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001200 struct led_classdev *led;
1201 size_t name_sz;
1202 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001203 size_t name_len;
1204 const char *name_fmt;
Frank Praznik61ebca92014-01-20 12:27:02 -05001205 static const char * const color_str[] = { "red", "green", "blue" };
Frank Praznik60781cf2014-01-11 15:13:15 -05001206 static const __u8 initial_values[MAX_LEDS] = { 0x00, 0x00, 0x00, 0x00 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001207
1208 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001209 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001210
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001211 if (drv_data->quirks & BUZZ_CONTROLLER) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001212 drv_data->led_count = 4;
1213 max_brightness = 1;
1214 use_colors = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001215 name_len = strlen("::buzz#");
1216 name_fmt = "%s::buzz%d";
1217 /* Validate expected report characteristics. */
1218 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1219 return -ENODEV;
Frank Praznik68330d82014-02-05 20:03:49 -05001220 } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik60781cf2014-01-11 15:13:15 -05001221 drv_data->led_count = 3;
1222 max_brightness = 255;
Frank Praznik61ebca92014-01-20 12:27:02 -05001223 use_colors = 1;
1224 name_len = 0;
1225 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -05001226 } else {
1227 drv_data->led_count = 4;
1228 max_brightness = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001229 use_colors = 0;
1230 name_len = strlen("::sony#");
1231 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001232 }
1233
Frank Praznikad142b92014-02-20 11:36:00 -05001234 /*
1235 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001236 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001237 * LEDs to on
1238 */
Frank Praznik60781cf2014-01-11 15:13:15 -05001239 sony_set_leds(hdev, initial_values, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001240
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001241 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001242
Frank Praznik60781cf2014-01-11 15:13:15 -05001243 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001244
1245 if (use_colors)
1246 name_sz = strlen(dev_name(&hdev->dev)) + strlen(color_str[n]) + 2;
1247
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001248 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1249 if (!led) {
1250 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001251 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001252 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001253 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001254
1255 name = (void *)(&led[1]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001256 if (use_colors)
1257 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), color_str[n]);
1258 else
1259 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001260 led->name = name;
1261 led->brightness = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -05001262 led->max_brightness = max_brightness;
Sven Eckelmannc5382512013-11-19 20:26:30 +01001263 led->brightness_get = sony_led_get_brightness;
1264 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001265
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001266 ret = led_classdev_register(&hdev->dev, led);
1267 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001268 hid_err(hdev, "Failed to register LED %d\n", n);
1269 kfree(led);
1270 goto error_leds;
1271 }
1272
Sven Eckelmann2251b852013-11-19 20:26:31 +01001273 drv_data->leds[n] = led;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001274 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001275
1276 return ret;
1277
Colin Leitnerf04d5142013-05-27 23:41:05 +02001278error_leds:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001279 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001280
Colin Leitnerf04d5142013-05-27 23:41:05 +02001281 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001282}
1283
Frank Praznikcad665a2014-01-11 15:13:54 -05001284static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001285{
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001286 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001287 unsigned char buf[] = {
1288 0x01,
1289 0x00, 0xff, 0x00, 0xff, 0x00,
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001290 0x00, 0x00, 0x00, 0x00, 0x00,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001291 0xff, 0x27, 0x10, 0x00, 0x32,
1292 0xff, 0x27, 0x10, 0x00, 0x32,
1293 0xff, 0x27, 0x10, 0x00, 0x32,
1294 0xff, 0x27, 0x10, 0x00, 0x32,
1295 0x00, 0x00, 0x00, 0x00, 0x00
1296 };
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001297
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001298#ifdef CONFIG_SONY_FF
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001299 buf[3] = sc->right ? 1 : 0;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001300 buf[5] = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001301#endif
1302
Frank Praznik60781cf2014-01-11 15:13:15 -05001303 buf[10] |= sc->led_state[0] << 1;
1304 buf[10] |= sc->led_state[1] << 2;
1305 buf[10] |= sc->led_state[2] << 3;
1306 buf[10] |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001307
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001308 if (sc->quirks & SIXAXIS_CONTROLLER_USB)
1309 hid_output_raw_report(sc->hdev, buf, sizeof(buf), HID_OUTPUT_REPORT);
1310 else
1311 hid_hw_raw_request(sc->hdev, 0x01, buf, sizeof(buf),
1312 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001313}
1314
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001315static void dualshock4_state_worker(struct work_struct *work)
1316{
1317 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001318 struct hid_device *hdev = sc->hdev;
Frank Praznik48220232014-02-05 20:03:44 -05001319 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001320
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001321 __u8 buf[78] = { 0 };
Frank Praznik48220232014-02-05 20:03:44 -05001322
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001323 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1324 buf[0] = 0x05;
1325 buf[1] = 0x03;
1326 offset = 4;
1327 } else {
1328 buf[0] = 0x11;
1329 buf[1] = 0xB0;
1330 buf[3] = 0x0F;
1331 offset = 6;
1332 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001333
1334#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05001335 buf[offset++] = sc->right;
1336 buf[offset++] = sc->left;
1337#else
1338 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001339#endif
1340
Frank Praznik48220232014-02-05 20:03:44 -05001341 buf[offset++] = sc->led_state[0];
1342 buf[offset++] = sc->led_state[1];
1343 buf[offset++] = sc->led_state[2];
Frank Praznik60781cf2014-01-11 15:13:15 -05001344
Frank Praznikfdcf105d2014-02-05 20:03:46 -05001345 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB)
1346 hid_hw_output_report(hdev, buf, 32);
1347 else
1348 hid_hw_raw_request(hdev, 0x11, buf, 78,
1349 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001350}
1351
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001352#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001353static int sony_play_effect(struct input_dev *dev, void *data,
1354 struct ff_effect *effect)
1355{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001356 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001357 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001358
1359 if (effect->type != FF_RUMBLE)
1360 return 0;
1361
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001362 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001363 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001364
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001365 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001366 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001367}
1368
1369static int sony_init_ff(struct hid_device *hdev)
1370{
1371 struct hid_input *hidinput = list_entry(hdev->inputs.next,
1372 struct hid_input, list);
1373 struct input_dev *input_dev = hidinput->input;
1374
1375 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1376 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1377}
1378
1379#else
1380static int sony_init_ff(struct hid_device *hdev)
1381{
1382 return 0;
1383}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001384
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001385#endif
1386
Frank Praznikd902f472014-01-27 10:17:36 -05001387static int sony_battery_get_property(struct power_supply *psy,
1388 enum power_supply_property psp,
1389 union power_supply_propval *val)
1390{
1391 struct sony_sc *sc = container_of(psy, struct sony_sc, battery);
1392 unsigned long flags;
1393 int ret = 0;
1394 u8 battery_charging, battery_capacity, cable_state;
1395
1396 spin_lock_irqsave(&sc->lock, flags);
1397 battery_charging = sc->battery_charging;
1398 battery_capacity = sc->battery_capacity;
1399 cable_state = sc->cable_state;
1400 spin_unlock_irqrestore(&sc->lock, flags);
1401
1402 switch (psp) {
1403 case POWER_SUPPLY_PROP_PRESENT:
1404 val->intval = 1;
1405 break;
1406 case POWER_SUPPLY_PROP_SCOPE:
1407 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1408 break;
1409 case POWER_SUPPLY_PROP_CAPACITY:
1410 val->intval = battery_capacity;
1411 break;
1412 case POWER_SUPPLY_PROP_STATUS:
1413 if (battery_charging)
1414 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1415 else
1416 if (battery_capacity == 100 && cable_state)
1417 val->intval = POWER_SUPPLY_STATUS_FULL;
1418 else
1419 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1420 break;
1421 default:
1422 ret = -EINVAL;
1423 break;
1424 }
1425 return ret;
1426}
1427
1428static int sony_battery_probe(struct sony_sc *sc)
1429{
1430 static atomic_t power_id_seq = ATOMIC_INIT(0);
1431 unsigned long power_id;
1432 struct hid_device *hdev = sc->hdev;
1433 int ret;
1434
Frank Praznikad142b92014-02-20 11:36:00 -05001435 /*
1436 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05001437 * if the battery is polled before the first device report is received.
1438 */
1439 sc->battery_capacity = 100;
1440
Frank Praznikd902f472014-01-27 10:17:36 -05001441 power_id = (unsigned long)atomic_inc_return(&power_id_seq);
1442
1443 sc->battery.properties = sony_battery_props;
1444 sc->battery.num_properties = ARRAY_SIZE(sony_battery_props);
1445 sc->battery.get_property = sony_battery_get_property;
1446 sc->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1447 sc->battery.use_for_apm = 0;
1448 sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%lu",
1449 power_id);
1450 if (!sc->battery.name)
1451 return -ENOMEM;
1452
1453 ret = power_supply_register(&hdev->dev, &sc->battery);
1454 if (ret) {
1455 hid_err(hdev, "Unable to register battery device\n");
1456 goto err_free;
1457 }
1458
1459 power_supply_powers(&sc->battery, &hdev->dev);
1460 return 0;
1461
1462err_free:
1463 kfree(sc->battery.name);
1464 sc->battery.name = NULL;
1465 return ret;
1466}
1467
1468static void sony_battery_remove(struct sony_sc *sc)
1469{
1470 if (!sc->battery.name)
1471 return;
1472
1473 power_supply_unregister(&sc->battery);
1474 kfree(sc->battery.name);
1475 sc->battery.name = NULL;
1476}
1477
Frank Praznike5606232014-01-27 10:17:37 -05001478static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
1479 int w, int h)
1480{
1481 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1482 struct hid_input, list);
1483 struct input_dev *input_dev = hidinput->input;
1484 int ret;
1485
1486 ret = input_mt_init_slots(input_dev, touch_count, 0);
1487 if (ret < 0) {
1488 hid_err(sc->hdev, "Unable to initialize multi-touch slots\n");
1489 return ret;
1490 }
1491
1492 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1493 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1494
1495 return 0;
1496}
1497
Frank Praznikd2d782f2014-02-20 11:36:03 -05001498/*
1499 * If a controller is plugged in via USB while already connected via Bluetooth
1500 * it will show up as two devices. A global list of connected controllers and
1501 * their MAC addresses is maintained to ensure that a device is only connected
1502 * once.
1503 */
1504static int sony_check_add_dev_list(struct sony_sc *sc)
1505{
1506 struct sony_sc *entry;
1507 unsigned long flags;
1508 int ret;
1509
1510 spin_lock_irqsave(&sony_dev_list_lock, flags);
1511
1512 list_for_each_entry(entry, &sony_device_list, list_node) {
1513 ret = memcmp(sc->mac_address, entry->mac_address,
1514 sizeof(sc->mac_address));
1515 if (!ret) {
1516 ret = -EEXIST;
1517 hid_info(sc->hdev, "controller with MAC address %pMR already connected\n",
1518 sc->mac_address);
1519 goto unlock;
1520 }
1521 }
1522
1523 ret = 0;
1524 list_add(&(sc->list_node), &sony_device_list);
1525
1526unlock:
1527 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1528 return ret;
1529}
1530
1531static void sony_remove_dev_list(struct sony_sc *sc)
1532{
1533 unsigned long flags;
1534
1535 if (sc->list_node.next) {
1536 spin_lock_irqsave(&sony_dev_list_lock, flags);
1537 list_del(&(sc->list_node));
1538 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
1539 }
1540}
1541
1542static int sony_get_bt_devaddr(struct sony_sc *sc)
1543{
1544 int ret;
1545
1546 /* HIDP stores the device MAC address as a string in the uniq field. */
1547 ret = strlen(sc->hdev->uniq);
1548 if (ret != 17)
1549 return -EINVAL;
1550
1551 ret = sscanf(sc->hdev->uniq,
1552 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1553 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
1554 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
1555
1556 if (ret != 6)
1557 return -EINVAL;
1558
1559 return 0;
1560}
1561
1562static int sony_check_add(struct sony_sc *sc)
1563{
1564 int n, ret;
1565
1566 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
1567 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
1568 /*
1569 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
1570 * address from the uniq string where HIDP stores it.
1571 * As uniq cannot be guaranteed to be a MAC address in all cases
1572 * a failure of this function should not prevent the connection.
1573 */
1574 if (sony_get_bt_devaddr(sc) < 0) {
1575 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
1576 return 0;
1577 }
1578 } else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1579 __u8 buf[7];
1580
1581 /*
1582 * The MAC address of a DS4 controller connected via USB can be
1583 * retrieved with feature report 0x81. The address begins at
1584 * offset 1.
1585 */
1586 ret = hid_hw_raw_request(sc->hdev, 0x81, buf, sizeof(buf),
1587 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1588
1589 if (ret != 7) {
1590 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
1591 return ret < 0 ? ret : -EINVAL;
1592 }
1593
1594 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
1595 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1596 __u8 buf[18];
1597
1598 /*
1599 * The MAC address of a Sixaxis controller connected via USB can
1600 * be retrieved with feature report 0xf2. The address begins at
1601 * offset 4.
1602 */
1603 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf, sizeof(buf),
1604 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
1605
1606 if (ret != 18) {
1607 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
1608 return ret < 0 ? ret : -EINVAL;
1609 }
1610
1611 /*
1612 * The Sixaxis device MAC in the report is big-endian and must
1613 * be byte-swapped.
1614 */
1615 for (n = 0; n < 6; n++)
1616 sc->mac_address[5-n] = buf[4+n];
1617 } else {
1618 return 0;
1619 }
1620
1621 return sony_check_add_dev_list(sc);
1622}
1623
1624
Jiri Slabybd28ce02008-06-25 23:47:04 +02001625static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1626{
1627 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001628 unsigned long quirks = id->driver_data;
1629 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001630 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001631
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001632 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001633 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08001634 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001635 return -ENOMEM;
1636 }
1637
1638 sc->quirks = quirks;
1639 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001640 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001641
Jiri Slabybd28ce02008-06-25 23:47:04 +02001642 ret = hid_parse(hdev);
1643 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001644 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001645 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001646 }
1647
Colin Leitnerf04d5142013-05-27 23:41:05 +02001648 if (sc->quirks & VAIO_RDESC_CONSTANT)
1649 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1650 else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
1651 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1652 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1653 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1654
1655 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001656 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001657 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001658 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001659 }
1660
Antonio Ospite569b10a2010-10-19 16:13:10 +02001661 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1662 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
Antonio Ospite816651a2010-05-03 22:15:55 +02001663 ret = sixaxis_set_operational_usb(hdev);
Frank Praznikc8de9db2014-02-20 11:36:01 -05001664 sc->worker_initialized = 1;
Frank Praznikcad665a2014-01-11 15:13:54 -05001665 INIT_WORK(&sc->state_worker, sixaxis_state_worker);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001666 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
Antonio Ospite816651a2010-05-03 22:15:55 +02001667 ret = sixaxis_set_operational_bt(hdev);
Frank Praznikc8de9db2014-02-20 11:36:01 -05001668 sc->worker_initialized = 1;
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001669 INIT_WORK(&sc->state_worker, sixaxis_state_worker);
1670 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik68330d82014-02-05 20:03:49 -05001671 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
1672 ret = dualshock4_set_operational_bt(hdev);
1673 if (ret < 0) {
1674 hid_err(hdev, "failed to set the Dualshock 4 operational mode\n");
1675 goto err_stop;
1676 }
1677 }
Frank Praznikad142b92014-02-20 11:36:00 -05001678 /*
1679 * The Dualshock 4 touchpad supports 2 touches and has a
Frank Praznike5606232014-01-27 10:17:37 -05001680 * resolution of 1920x940.
1681 */
1682 ret = sony_register_touchpad(sc, 2, 1920, 940);
1683 if (ret < 0)
1684 goto err_stop;
1685
Frank Praznikc8de9db2014-02-20 11:36:01 -05001686 sc->worker_initialized = 1;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001687 INIT_WORK(&sc->state_worker, dualshock4_state_worker);
1688 } else {
1689 ret = 0;
1690 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001691
Jiri Kosina4dfdc462008-12-30 00:49:59 +01001692 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001693 goto err_stop;
1694
Frank Praznikd2d782f2014-02-20 11:36:03 -05001695 ret = sony_check_add(sc);
1696 if (ret < 0)
1697 goto err_stop;
1698
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001699 if (sc->quirks & SONY_LED_SUPPORT) {
1700 ret = sony_leds_init(hdev);
1701 if (ret < 0)
1702 goto err_stop;
1703 }
1704
Frank Praznikd902f472014-01-27 10:17:36 -05001705 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1706 ret = sony_battery_probe(sc);
1707 if (ret < 0)
1708 goto err_stop;
1709
1710 /* Open the device to receive reports with battery info */
1711 ret = hid_hw_open(hdev);
1712 if (ret < 0) {
1713 hid_err(hdev, "hw open failed\n");
1714 goto err_stop;
1715 }
1716 }
1717
Frank Praznikc8de9db2014-02-20 11:36:01 -05001718 if (sc->quirks & SONY_FF_SUPPORT) {
1719 ret = sony_init_ff(hdev);
1720 if (ret < 0)
1721 goto err_close;
1722 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001723
Jiri Slabybd28ce02008-06-25 23:47:04 +02001724 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001725err_close:
1726 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001727err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001728 if (sc->quirks & SONY_LED_SUPPORT)
1729 sony_leds_remove(hdev);
Frank Praznikd902f472014-01-27 10:17:36 -05001730 if (sc->quirks & SONY_BATTERY_SUPPORT)
1731 sony_battery_remove(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05001732 if (sc->worker_initialized)
1733 cancel_work_sync(&sc->state_worker);
Frank Praznikd2d782f2014-02-20 11:36:03 -05001734 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001735 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001736 return ret;
1737}
1738
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001739static void sony_remove(struct hid_device *hdev)
1740{
Colin Leitnerf04d5142013-05-27 23:41:05 +02001741 struct sony_sc *sc = hid_get_drvdata(hdev);
1742
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001743 if (sc->quirks & SONY_LED_SUPPORT)
Sven Eckelmannc5382512013-11-19 20:26:30 +01001744 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001745
Frank Praznikd902f472014-01-27 10:17:36 -05001746 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1747 hid_hw_close(hdev);
1748 sony_battery_remove(sc);
1749 }
1750
Frank Praznikc8de9db2014-02-20 11:36:01 -05001751 if (sc->worker_initialized)
1752 cancel_work_sync(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001753
Frank Praznikd2d782f2014-02-20 11:36:03 -05001754 sony_remove_dev_list(sc);
1755
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001756 hid_hw_stop(hdev);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001757}
1758
Jiri Slabybd28ce02008-06-25 23:47:04 +02001759static const struct hid_device_id sony_devices[] = {
Antonio Ospite816651a2010-05-03 22:15:55 +02001760 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1761 .driver_data = SIXAXIS_CONTROLLER_USB },
Jiri Kosina35dca5b2011-04-28 15:43:13 +02001762 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
1763 .driver_data = SIXAXIS_CONTROLLER_USB },
Antonio Ospite816651a2010-05-03 22:15:55 +02001764 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1765 .driver_data = SIXAXIS_CONTROLLER_BT },
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001766 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
1767 .driver_data = VAIO_RDESC_CONSTANT },
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +09001768 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
1769 .driver_data = VAIO_RDESC_CONSTANT },
Colin Leitnerf04d5142013-05-27 23:41:05 +02001770 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
1771 * Logitech joystick from the device descriptor. */
1772 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
1773 .driver_data = BUZZ_CONTROLLER },
1774 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
1775 .driver_data = BUZZ_CONTROLLER },
Jiri Kosina078328d2013-06-13 12:03:49 +02001776 /* PS3 BD Remote Control */
1777 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
1778 .driver_data = PS3REMOTE },
1779 /* Logitech Harmony Adapter for PS3 */
1780 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
1781 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001782 /* Sony Dualshock 4 controllers for PS4 */
1783 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001784 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001785 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001786 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02001787 { }
1788};
1789MODULE_DEVICE_TABLE(hid, sony_devices);
1790
1791static struct hid_driver sony_driver = {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001792 .name = "sony",
1793 .id_table = sony_devices,
1794 .input_mapping = sony_mapping,
1795 .probe = sony_probe,
1796 .remove = sony_remove,
1797 .report_fixup = sony_report_fixup,
1798 .raw_event = sony_raw_event
Jiri Slabybd28ce02008-06-25 23:47:04 +02001799};
H Hartley Sweetenf4254582012-12-17 15:28:26 -07001800module_hid_driver(sony_driver);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001801
Jiri Slabybd28ce02008-06-25 23:47:04 +02001802MODULE_LICENSE("GPL");