blob: be0d3861159806a88a2e10a538f2ef112c80fb84 [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
Jiri Kosina078328d2013-06-13 12:03:49 +020020/* NOTE: in order for the Sony PS3 BD Remote Control to be found by
21 * a Bluetooth host, the key combination Start+Enter has to be kept pressed
22 * for about 7 seconds with the Bluetooth Host Controller in discovering mode.
23 *
24 * There will be no PIN request from the device.
25 */
26
Jiri Slabybd28ce02008-06-25 23:47:04 +020027#include <linux/device.h>
28#include <linux/hid.h>
29#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020031#include <linux/usb.h>
Jiri Kosina40e32ee2013-05-28 11:22:09 +020032#include <linux/leds.h>
Frank Praznikd902f472014-01-27 10:17:36 -050033#include <linux/power_supply.h>
34#include <linux/spinlock.h>
Frank Praznike5606232014-01-27 10:17:37 -050035#include <linux/input/mt.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020036
37#include "hid-ids.h"
38
Frank Praznik6c79c182014-01-16 21:43:03 -050039#define VAIO_RDESC_CONSTANT BIT(0)
40#define SIXAXIS_CONTROLLER_USB BIT(1)
41#define SIXAXIS_CONTROLLER_BT BIT(2)
42#define BUZZ_CONTROLLER BIT(3)
43#define PS3REMOTE BIT(4)
Frank Praznik8ab16762014-01-16 21:42:31 -050044#define DUALSHOCK4_CONTROLLER_USB BIT(5)
45#define DUALSHOCK4_CONTROLLER_BT BIT(6)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020046
Frank Praznik8ab16762014-01-16 21:42:31 -050047#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER_USB | BUZZ_CONTROLLER | DUALSHOCK4_CONTROLLER_USB)
Frank Praznikd902f472014-01-27 10:17:36 -050048#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_USB)
Frank Praznik60781cf2014-01-11 15:13:15 -050049
50#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010051
Simon Wood61ab44b2011-06-10 12:00:26 +020052static const u8 sixaxis_rdesc_fixup[] = {
53 0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
54 0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
55 0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
56};
57
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -020058static const u8 sixaxis_rdesc_fixup2[] = {
59 0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
60 0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
61 0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
62 0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
63 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
64 0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
65 0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
66 0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
67 0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
68 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
69 0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
70 0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
71 0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
72 0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
73 0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
74 0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
75 0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
76 0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
77 0xb1, 0x02, 0xc0, 0xc0,
78};
79
Frank Praznik58d70272014-01-20 12:27:01 -050080/* The default descriptor doesn't provide mapping for the accelerometers
81 * or orientation sensors. This fixed descriptor maps the accelerometers
82 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
83 * to usage values 0x43, 0x44 and 0x45.
84 */
Frank Prazniked19d8c2014-01-16 21:43:12 -050085static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -050086 0x05, 0x01, /* Usage Page (Desktop), */
87 0x09, 0x05, /* Usage (Gamepad), */
88 0xA1, 0x01, /* Collection (Application), */
89 0x85, 0x01, /* Report ID (1), */
90 0x09, 0x30, /* Usage (X), */
91 0x09, 0x31, /* Usage (Y), */
92 0x09, 0x32, /* Usage (Z), */
93 0x09, 0x35, /* Usage (Rz), */
94 0x15, 0x00, /* Logical Minimum (0), */
95 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
96 0x75, 0x08, /* Report Size (8), */
97 0x95, 0x04, /* Report Count (4), */
98 0x81, 0x02, /* Input (Variable), */
99 0x09, 0x39, /* Usage (Hat Switch), */
100 0x15, 0x00, /* Logical Minimum (0), */
101 0x25, 0x07, /* Logical Maximum (7), */
102 0x35, 0x00, /* Physical Minimum (0), */
103 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
104 0x65, 0x14, /* Unit (Degrees), */
105 0x75, 0x04, /* Report Size (4), */
106 0x95, 0x01, /* Report Count (1), */
107 0x81, 0x42, /* Input (Variable, Null State), */
108 0x65, 0x00, /* Unit, */
109 0x05, 0x09, /* Usage Page (Button), */
110 0x19, 0x01, /* Usage Minimum (01h), */
111 0x29, 0x0E, /* Usage Maximum (0Eh), */
112 0x15, 0x00, /* Logical Minimum (0), */
113 0x25, 0x01, /* Logical Maximum (1), */
114 0x75, 0x01, /* Report Size (1), */
115 0x95, 0x0E, /* Report Count (14), */
116 0x81, 0x02, /* Input (Variable), */
117 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
118 0x09, 0x20, /* Usage (20h), */
119 0x75, 0x06, /* Report Size (6), */
120 0x95, 0x01, /* Report Count (1), */
121 0x15, 0x00, /* Logical Minimum (0), */
122 0x25, 0x7F, /* Logical Maximum (127), */
123 0x81, 0x02, /* Input (Variable), */
124 0x05, 0x01, /* Usage Page (Desktop), */
125 0x09, 0x33, /* Usage (Rx), */
126 0x09, 0x34, /* Usage (Ry), */
127 0x15, 0x00, /* Logical Minimum (0), */
128 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
129 0x75, 0x08, /* Report Size (8), */
130 0x95, 0x02, /* Report Count (2), */
131 0x81, 0x02, /* Input (Variable), */
132 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
133 0x09, 0x21, /* Usage (21h), */
134 0x95, 0x03, /* Report Count (3), */
135 0x81, 0x02, /* Input (Variable), */
136 0x05, 0x01, /* Usage Page (Desktop), */
137 0x19, 0x40, /* Usage Minimum (40h), */
138 0x29, 0x42, /* Usage Maximum (42h), */
139 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
140 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
141 0x75, 0x10, /* Report Size (16), */
142 0x95, 0x03, /* Report Count (3), */
143 0x81, 0x02, /* Input (Variable), */
144 0x19, 0x43, /* Usage Minimum (43h), */
145 0x29, 0x45, /* Usage Maximum (45h), */
146 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
147 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
148 0x95, 0x03, /* Report Count (3), */
149 0x81, 0x02, /* Input (Variable), */
150 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
151 0x09, 0x21, /* Usage (21h), */
152 0x15, 0x00, /* Logical Minimum (0), */
153 0x25, 0xFF, /* Logical Maximum (255), */
154 0x75, 0x08, /* Report Size (8), */
155 0x95, 0x27, /* Report Count (39), */
156 0x81, 0x02, /* Input (Variable), */
157 0x85, 0x05, /* Report ID (5), */
158 0x09, 0x22, /* Usage (22h), */
159 0x95, 0x1F, /* Report Count (31), */
160 0x91, 0x02, /* Output (Variable), */
161 0x85, 0x04, /* Report ID (4), */
162 0x09, 0x23, /* Usage (23h), */
163 0x95, 0x24, /* Report Count (36), */
164 0xB1, 0x02, /* Feature (Variable), */
165 0x85, 0x02, /* Report ID (2), */
166 0x09, 0x24, /* Usage (24h), */
167 0x95, 0x24, /* Report Count (36), */
168 0xB1, 0x02, /* Feature (Variable), */
169 0x85, 0x08, /* Report ID (8), */
170 0x09, 0x25, /* Usage (25h), */
171 0x95, 0x03, /* Report Count (3), */
172 0xB1, 0x02, /* Feature (Variable), */
173 0x85, 0x10, /* Report ID (16), */
174 0x09, 0x26, /* Usage (26h), */
175 0x95, 0x04, /* Report Count (4), */
176 0xB1, 0x02, /* Feature (Variable), */
177 0x85, 0x11, /* Report ID (17), */
178 0x09, 0x27, /* Usage (27h), */
179 0x95, 0x02, /* Report Count (2), */
180 0xB1, 0x02, /* Feature (Variable), */
181 0x85, 0x12, /* Report ID (18), */
182 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
183 0x09, 0x21, /* Usage (21h), */
184 0x95, 0x0F, /* Report Count (15), */
185 0xB1, 0x02, /* Feature (Variable), */
186 0x85, 0x13, /* Report ID (19), */
187 0x09, 0x22, /* Usage (22h), */
188 0x95, 0x16, /* Report Count (22), */
189 0xB1, 0x02, /* Feature (Variable), */
190 0x85, 0x14, /* Report ID (20), */
191 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
192 0x09, 0x20, /* Usage (20h), */
193 0x95, 0x10, /* Report Count (16), */
194 0xB1, 0x02, /* Feature (Variable), */
195 0x85, 0x15, /* Report ID (21), */
196 0x09, 0x21, /* Usage (21h), */
197 0x95, 0x2C, /* Report Count (44), */
198 0xB1, 0x02, /* Feature (Variable), */
199 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
200 0x85, 0x80, /* Report ID (128), */
201 0x09, 0x20, /* Usage (20h), */
202 0x95, 0x06, /* Report Count (6), */
203 0xB1, 0x02, /* Feature (Variable), */
204 0x85, 0x81, /* Report ID (129), */
205 0x09, 0x21, /* Usage (21h), */
206 0x95, 0x06, /* Report Count (6), */
207 0xB1, 0x02, /* Feature (Variable), */
208 0x85, 0x82, /* Report ID (130), */
209 0x09, 0x22, /* Usage (22h), */
210 0x95, 0x05, /* Report Count (5), */
211 0xB1, 0x02, /* Feature (Variable), */
212 0x85, 0x83, /* Report ID (131), */
213 0x09, 0x23, /* Usage (23h), */
214 0x95, 0x01, /* Report Count (1), */
215 0xB1, 0x02, /* Feature (Variable), */
216 0x85, 0x84, /* Report ID (132), */
217 0x09, 0x24, /* Usage (24h), */
218 0x95, 0x04, /* Report Count (4), */
219 0xB1, 0x02, /* Feature (Variable), */
220 0x85, 0x85, /* Report ID (133), */
221 0x09, 0x25, /* Usage (25h), */
222 0x95, 0x06, /* Report Count (6), */
223 0xB1, 0x02, /* Feature (Variable), */
224 0x85, 0x86, /* Report ID (134), */
225 0x09, 0x26, /* Usage (26h), */
226 0x95, 0x06, /* Report Count (6), */
227 0xB1, 0x02, /* Feature (Variable), */
228 0x85, 0x87, /* Report ID (135), */
229 0x09, 0x27, /* Usage (27h), */
230 0x95, 0x23, /* Report Count (35), */
231 0xB1, 0x02, /* Feature (Variable), */
232 0x85, 0x88, /* Report ID (136), */
233 0x09, 0x28, /* Usage (28h), */
234 0x95, 0x22, /* Report Count (34), */
235 0xB1, 0x02, /* Feature (Variable), */
236 0x85, 0x89, /* Report ID (137), */
237 0x09, 0x29, /* Usage (29h), */
238 0x95, 0x02, /* Report Count (2), */
239 0xB1, 0x02, /* Feature (Variable), */
240 0x85, 0x90, /* Report ID (144), */
241 0x09, 0x30, /* Usage (30h), */
242 0x95, 0x05, /* Report Count (5), */
243 0xB1, 0x02, /* Feature (Variable), */
244 0x85, 0x91, /* Report ID (145), */
245 0x09, 0x31, /* Usage (31h), */
246 0x95, 0x03, /* Report Count (3), */
247 0xB1, 0x02, /* Feature (Variable), */
248 0x85, 0x92, /* Report ID (146), */
249 0x09, 0x32, /* Usage (32h), */
250 0x95, 0x03, /* Report Count (3), */
251 0xB1, 0x02, /* Feature (Variable), */
252 0x85, 0x93, /* Report ID (147), */
253 0x09, 0x33, /* Usage (33h), */
254 0x95, 0x0C, /* Report Count (12), */
255 0xB1, 0x02, /* Feature (Variable), */
256 0x85, 0xA0, /* Report ID (160), */
257 0x09, 0x40, /* Usage (40h), */
258 0x95, 0x06, /* Report Count (6), */
259 0xB1, 0x02, /* Feature (Variable), */
260 0x85, 0xA1, /* Report ID (161), */
261 0x09, 0x41, /* Usage (41h), */
262 0x95, 0x01, /* Report Count (1), */
263 0xB1, 0x02, /* Feature (Variable), */
264 0x85, 0xA2, /* Report ID (162), */
265 0x09, 0x42, /* Usage (42h), */
266 0x95, 0x01, /* Report Count (1), */
267 0xB1, 0x02, /* Feature (Variable), */
268 0x85, 0xA3, /* Report ID (163), */
269 0x09, 0x43, /* Usage (43h), */
270 0x95, 0x30, /* Report Count (48), */
271 0xB1, 0x02, /* Feature (Variable), */
272 0x85, 0xA4, /* Report ID (164), */
273 0x09, 0x44, /* Usage (44h), */
274 0x95, 0x0D, /* Report Count (13), */
275 0xB1, 0x02, /* Feature (Variable), */
276 0x85, 0xA5, /* Report ID (165), */
277 0x09, 0x45, /* Usage (45h), */
278 0x95, 0x15, /* Report Count (21), */
279 0xB1, 0x02, /* Feature (Variable), */
280 0x85, 0xA6, /* Report ID (166), */
281 0x09, 0x46, /* Usage (46h), */
282 0x95, 0x15, /* Report Count (21), */
283 0xB1, 0x02, /* Feature (Variable), */
284 0x85, 0xF0, /* Report ID (240), */
285 0x09, 0x47, /* Usage (47h), */
286 0x95, 0x3F, /* Report Count (63), */
287 0xB1, 0x02, /* Feature (Variable), */
288 0x85, 0xF1, /* Report ID (241), */
289 0x09, 0x48, /* Usage (48h), */
290 0x95, 0x3F, /* Report Count (63), */
291 0xB1, 0x02, /* Feature (Variable), */
292 0x85, 0xF2, /* Report ID (242), */
293 0x09, 0x49, /* Usage (49h), */
294 0x95, 0x0F, /* Report Count (15), */
295 0xB1, 0x02, /* Feature (Variable), */
296 0x85, 0xA7, /* Report ID (167), */
297 0x09, 0x4A, /* Usage (4Ah), */
298 0x95, 0x01, /* Report Count (1), */
299 0xB1, 0x02, /* Feature (Variable), */
300 0x85, 0xA8, /* Report ID (168), */
301 0x09, 0x4B, /* Usage (4Bh), */
302 0x95, 0x01, /* Report Count (1), */
303 0xB1, 0x02, /* Feature (Variable), */
304 0x85, 0xA9, /* Report ID (169), */
305 0x09, 0x4C, /* Usage (4Ch), */
306 0x95, 0x08, /* Report Count (8), */
307 0xB1, 0x02, /* Feature (Variable), */
308 0x85, 0xAA, /* Report ID (170), */
309 0x09, 0x4E, /* Usage (4Eh), */
310 0x95, 0x01, /* Report Count (1), */
311 0xB1, 0x02, /* Feature (Variable), */
312 0x85, 0xAB, /* Report ID (171), */
313 0x09, 0x4F, /* Usage (4Fh), */
314 0x95, 0x39, /* Report Count (57), */
315 0xB1, 0x02, /* Feature (Variable), */
316 0x85, 0xAC, /* Report ID (172), */
317 0x09, 0x50, /* Usage (50h), */
318 0x95, 0x39, /* Report Count (57), */
319 0xB1, 0x02, /* Feature (Variable), */
320 0x85, 0xAD, /* Report ID (173), */
321 0x09, 0x51, /* Usage (51h), */
322 0x95, 0x0B, /* Report Count (11), */
323 0xB1, 0x02, /* Feature (Variable), */
324 0x85, 0xAE, /* Report ID (174), */
325 0x09, 0x52, /* Usage (52h), */
326 0x95, 0x01, /* Report Count (1), */
327 0xB1, 0x02, /* Feature (Variable), */
328 0x85, 0xAF, /* Report ID (175), */
329 0x09, 0x53, /* Usage (53h), */
330 0x95, 0x02, /* Report Count (2), */
331 0xB1, 0x02, /* Feature (Variable), */
332 0x85, 0xB0, /* Report ID (176), */
333 0x09, 0x54, /* Usage (54h), */
334 0x95, 0x3F, /* Report Count (63), */
335 0xB1, 0x02, /* Feature (Variable), */
336 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500337};
338
Jiri Kosina078328d2013-06-13 12:03:49 +0200339static __u8 ps3remote_rdesc[] = {
340 0x05, 0x01, /* GUsagePage Generic Desktop */
341 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
342 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
343
344 /* Use collection 1 for joypad buttons */
345 0xA1, 0x02, /* MCollection Logical (interrelated data) */
346
347 /* Ignore the 1st byte, maybe it is used for a controller
348 * number but it's not needed for correct operation */
349 0x75, 0x08, /* GReportSize 0x08 [8] */
350 0x95, 0x01, /* GReportCount 0x01 [1] */
351 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
352
353 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
354 * buttons multiple keypresses are allowed */
355 0x05, 0x09, /* GUsagePage Button */
356 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
357 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
358 0x14, /* GLogicalMinimum [0] */
359 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
360 0x75, 0x01, /* GReportSize 0x01 [1] */
361 0x95, 0x18, /* GReportCount 0x18 [24] */
362 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
363
364 0xC0, /* MEndCollection */
365
366 /* Use collection 2 for remote control buttons */
367 0xA1, 0x02, /* MCollection Logical (interrelated data) */
368
369 /* 5th byte is used for remote control buttons */
370 0x05, 0x09, /* GUsagePage Button */
371 0x18, /* LUsageMinimum [No button pressed] */
372 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
373 0x14, /* GLogicalMinimum [0] */
374 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
375 0x75, 0x08, /* GReportSize 0x08 [8] */
376 0x95, 0x01, /* GReportCount 0x01 [1] */
377 0x80, /* MInput */
378
379 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
380 * 0xff and 11th is for press indication */
381 0x75, 0x08, /* GReportSize 0x08 [8] */
382 0x95, 0x06, /* GReportCount 0x06 [6] */
383 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
384
385 /* 12th byte is for battery strength */
386 0x05, 0x06, /* GUsagePage Generic Device Controls */
387 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
388 0x14, /* GLogicalMinimum [0] */
389 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
390 0x75, 0x08, /* GReportSize 0x08 [8] */
391 0x95, 0x01, /* GReportCount 0x01 [1] */
392 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
393
394 0xC0, /* MEndCollection */
395
396 0xC0 /* MEndCollection [Game Pad] */
397};
398
399static const unsigned int ps3remote_keymap_joypad_buttons[] = {
400 [0x01] = KEY_SELECT,
401 [0x02] = BTN_THUMBL, /* L3 */
402 [0x03] = BTN_THUMBR, /* R3 */
403 [0x04] = BTN_START,
404 [0x05] = KEY_UP,
405 [0x06] = KEY_RIGHT,
406 [0x07] = KEY_DOWN,
407 [0x08] = KEY_LEFT,
408 [0x09] = BTN_TL2, /* L2 */
409 [0x0a] = BTN_TR2, /* R2 */
410 [0x0b] = BTN_TL, /* L1 */
411 [0x0c] = BTN_TR, /* R1 */
412 [0x0d] = KEY_OPTION, /* options/triangle */
413 [0x0e] = KEY_BACK, /* back/circle */
414 [0x0f] = BTN_0, /* cross */
415 [0x10] = KEY_SCREEN, /* view/square */
416 [0x11] = KEY_HOMEPAGE, /* PS button */
417 [0x14] = KEY_ENTER,
418};
419static const unsigned int ps3remote_keymap_remote_buttons[] = {
420 [0x00] = KEY_1,
421 [0x01] = KEY_2,
422 [0x02] = KEY_3,
423 [0x03] = KEY_4,
424 [0x04] = KEY_5,
425 [0x05] = KEY_6,
426 [0x06] = KEY_7,
427 [0x07] = KEY_8,
428 [0x08] = KEY_9,
429 [0x09] = KEY_0,
430 [0x0e] = KEY_ESC, /* return */
431 [0x0f] = KEY_CLEAR,
432 [0x16] = KEY_EJECTCD,
433 [0x1a] = KEY_MENU, /* top menu */
434 [0x28] = KEY_TIME,
435 [0x30] = KEY_PREVIOUS,
436 [0x31] = KEY_NEXT,
437 [0x32] = KEY_PLAY,
438 [0x33] = KEY_REWIND, /* scan back */
439 [0x34] = KEY_FORWARD, /* scan forward */
440 [0x38] = KEY_STOP,
441 [0x39] = KEY_PAUSE,
442 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
443 [0x60] = KEY_FRAMEBACK, /* slow/step back */
444 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
445 [0x63] = KEY_SUBTITLE,
446 [0x64] = KEY_AUDIO,
447 [0x65] = KEY_ANGLE,
448 [0x70] = KEY_INFO, /* display */
449 [0x80] = KEY_BLUE,
450 [0x81] = KEY_RED,
451 [0x82] = KEY_GREEN,
452 [0x83] = KEY_YELLOW,
453};
454
Colin Leitnerf04d5142013-05-27 23:41:05 +0200455static const unsigned int buzz_keymap[] = {
456 /* The controller has 4 remote buzzers, each with one LED and 5
457 * buttons.
458 *
459 * We use the mapping chosen by the controller, which is:
460 *
461 * Key Offset
462 * -------------------
463 * Buzz 1
464 * Blue 5
465 * Orange 4
466 * Green 3
467 * Yellow 2
468 *
469 * So, for example, the orange button on the third buzzer is mapped to
470 * BTN_TRIGGER_HAPPY14
471 */
472 [ 1] = BTN_TRIGGER_HAPPY1,
473 [ 2] = BTN_TRIGGER_HAPPY2,
474 [ 3] = BTN_TRIGGER_HAPPY3,
475 [ 4] = BTN_TRIGGER_HAPPY4,
476 [ 5] = BTN_TRIGGER_HAPPY5,
477 [ 6] = BTN_TRIGGER_HAPPY6,
478 [ 7] = BTN_TRIGGER_HAPPY7,
479 [ 8] = BTN_TRIGGER_HAPPY8,
480 [ 9] = BTN_TRIGGER_HAPPY9,
481 [10] = BTN_TRIGGER_HAPPY10,
482 [11] = BTN_TRIGGER_HAPPY11,
483 [12] = BTN_TRIGGER_HAPPY12,
484 [13] = BTN_TRIGGER_HAPPY13,
485 [14] = BTN_TRIGGER_HAPPY14,
486 [15] = BTN_TRIGGER_HAPPY15,
487 [16] = BTN_TRIGGER_HAPPY16,
488 [17] = BTN_TRIGGER_HAPPY17,
489 [18] = BTN_TRIGGER_HAPPY18,
490 [19] = BTN_TRIGGER_HAPPY19,
491 [20] = BTN_TRIGGER_HAPPY20,
492};
493
Frank Praznikd902f472014-01-27 10:17:36 -0500494static enum power_supply_property sony_battery_props[] = {
495 POWER_SUPPLY_PROP_PRESENT,
496 POWER_SUPPLY_PROP_CAPACITY,
497 POWER_SUPPLY_PROP_SCOPE,
498 POWER_SUPPLY_PROP_STATUS,
499};
500
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200501struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500502 spinlock_t lock;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100503 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500504 struct led_classdev *leds[MAX_LEDS];
Frank Praznikc4e1ddf2014-01-17 14:46:26 -0500505 struct hid_report *output_report;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200506 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100507 struct work_struct state_worker;
Frank Praznikd902f472014-01-27 10:17:36 -0500508 struct power_supply battery;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200509
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100510#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100511 __u8 left;
512 __u8 right;
513#endif
514
Frank Praznikd902f472014-01-27 10:17:36 -0500515 __u8 cable_state;
516 __u8 battery_charging;
517 __u8 battery_capacity;
Frank Praznik60781cf2014-01-11 15:13:15 -0500518 __u8 led_state[MAX_LEDS];
519 __u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200520};
521
Jiri Kosina078328d2013-06-13 12:03:49 +0200522static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
523 unsigned int *rsize)
524{
525 *rsize = sizeof(ps3remote_rdesc);
526 return ps3remote_rdesc;
527}
528
529static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
530 struct hid_field *field, struct hid_usage *usage,
531 unsigned long **bit, int *max)
532{
533 unsigned int key = usage->hid & HID_USAGE;
534
535 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
536 return -1;
537
538 switch (usage->collection_index) {
539 case 1:
540 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
541 return -1;
542
543 key = ps3remote_keymap_joypad_buttons[key];
544 if (!key)
545 return -1;
546 break;
547 case 2:
548 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
549 return -1;
550
551 key = ps3remote_keymap_remote_buttons[key];
552 if (!key)
553 return -1;
554 break;
555 default:
556 return -1;
557 }
558
559 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
560 return 1;
561}
562
563
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200564/* Sony Vaio VGX has wrongly mouse pointer declared as constant */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400565static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
566 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200567{
568 struct sony_sc *sc = hid_get_drvdata(hdev);
569
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900570 /*
571 * Some Sony RF receivers wrongly declare the mouse pointer as a
572 * a constant non-data variable.
573 */
574 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
575 /* usage page: generic desktop controls */
576 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
577 /* usage: mouse */
578 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
579 /* input (usage page for x,y axes): constant, variable, relative */
580 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +0900581 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900582 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200583 rdesc[55] = 0x06;
584 }
Simon Wood61ab44b2011-06-10 12:00:26 +0200585
Frank Prazniked19d8c2014-01-16 21:43:12 -0500586 /*
587 * The default Dualshock 4 USB descriptor doesn't assign
588 * the gyroscope values to corresponding axes so we need a
589 * modified one.
590 */
591 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
592 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
593 rdesc = dualshock4_usb_rdesc;
594 *rsize = sizeof(dualshock4_usb_rdesc);
595 }
596
Simon Wood61ab44b2011-06-10 12:00:26 +0200597 /* The HID descriptor exposed over BT has a trailing zero byte */
598 if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
599 ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
600 rdesc[83] == 0x75) {
601 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
602 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
603 sizeof(sixaxis_rdesc_fixup));
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200604 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
605 *rsize > sizeof(sixaxis_rdesc_fixup2)) {
606 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
607 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
608 *rsize = sizeof(sixaxis_rdesc_fixup2);
609 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
Simon Wood61ab44b2011-06-10 12:00:26 +0200610 }
Jiri Kosina078328d2013-06-13 12:03:49 +0200611
612 if (sc->quirks & PS3REMOTE)
613 return ps3remote_fixup(hdev, rdesc, rsize);
614
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400615 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200616}
617
Frank Praznikd902f472014-01-27 10:17:36 -0500618static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
619{
620 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
621 unsigned long flags;
622 __u8 cable_state, battery_capacity, battery_charging;
623
624 /* The sixaxis is charging if the battery value is 0xee
625 * and it is fully charged if the value is 0xef.
626 * It does not report the actual level while charging so it
627 * is set to 100% while charging is in progress.
628 */
629 if (rd[30] >= 0xee) {
630 battery_capacity = 100;
631 battery_charging = rd[30] & 0x01;
632 } else {
633 battery_capacity = sixaxis_battery_capacity[rd[30]];
634 battery_charging = 0;
635 }
636 cable_state = (rd[31] >> 4) & 0x01;
637
638 spin_lock_irqsave(&sc->lock, flags);
639 sc->cable_state = cable_state;
640 sc->battery_capacity = battery_capacity;
641 sc->battery_charging = battery_charging;
642 spin_unlock_irqrestore(&sc->lock, flags);
643}
644
645static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
646{
Frank Praznike5606232014-01-27 10:17:37 -0500647 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
648 struct hid_input, list);
649 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -0500650 unsigned long flags;
Frank Praznike5606232014-01-27 10:17:37 -0500651 int n, offset = 35;
Frank Praznikd902f472014-01-27 10:17:36 -0500652 __u8 cable_state, battery_capacity, battery_charging;
653
654 /* The lower 4 bits of byte 30 contain the battery level
655 * and the 5th bit contains the USB cable state.
656 */
657 cable_state = (rd[30] >> 4) & 0x01;
658 battery_capacity = rd[30] & 0x0F;
659
660 /* On USB the Dualshock 4 battery level goes from 0 to 11.
661 * A battery level of 11 means fully charged.
662 */
663 if (cable_state && battery_capacity == 11)
664 battery_charging = 0;
665 else
666 battery_charging = 1;
667
668 if (battery_capacity > 10)
669 battery_capacity--;
670 battery_capacity *= 10;
671
672 spin_lock_irqsave(&sc->lock, flags);
673 sc->cable_state = cable_state;
674 sc->battery_capacity = battery_capacity;
675 sc->battery_charging = battery_charging;
676 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -0500677
678 /* The Dualshock 4 multi-touch trackpad data starts at offset 35 on USB.
679 * The first 7 bits of the first byte is a counter and bit 8 is a touch
680 * indicator that is 0 when pressed and 1 when not pressed.
681 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
682 * The data for the second touch is in the same format and immediatly
683 * follows the data for the first.
684 */
685 for (n = 0; n < 2; n++) {
686 __u16 x, y;
687
688 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
689 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
690
691 input_mt_slot(input_dev, n);
692 input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
693 !(rd[offset] >> 7));
694 input_report_abs(input_dev, ABS_MT_POSITION_X, x);
695 input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
696
697 offset += 4;
698 }
Frank Praznikd902f472014-01-27 10:17:36 -0500699}
700
Simon Woodc9e4d872011-06-10 12:00:27 +0200701static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
702 __u8 *rd, int size)
703{
704 struct sony_sc *sc = hid_get_drvdata(hdev);
705
706 /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
707 * has to be BYTE_SWAPPED before passing up to joystick interface
708 */
709 if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
710 rd[0] == 0x01 && size == 49) {
711 swap(rd[41], rd[42]);
712 swap(rd[43], rd[44]);
713 swap(rd[45], rd[46]);
714 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -0500715
716 sixaxis_parse_report(sc, rd, size);
717 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
718 size == 64) {
719 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +0200720 }
721
722 return 0;
723}
724
Colin Leitnerf04d5142013-05-27 23:41:05 +0200725static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
726 struct hid_field *field, struct hid_usage *usage,
727 unsigned long **bit, int *max)
728{
729 struct sony_sc *sc = hid_get_drvdata(hdev);
730
731 if (sc->quirks & BUZZ_CONTROLLER) {
732 unsigned int key = usage->hid & HID_USAGE;
733
734 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
735 return -1;
736
737 switch (usage->collection_index) {
738 case 1:
739 if (key >= ARRAY_SIZE(buzz_keymap))
740 return -1;
741
742 key = buzz_keymap[key];
743 if (!key)
744 return -1;
745 break;
746 default:
747 return -1;
748 }
749
750 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
751 return 1;
752 }
753
Jiri Kosina078328d2013-06-13 12:03:49 +0200754 if (sc->quirks & PS3REMOTE)
755 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
756
Benjamin Tissoires6f498012013-07-24 16:53:07 +0200757 /* Let hid-core decide for the others */
758 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200759}
760
Antonio Ospite5710fab2011-02-20 18:26:45 +0100761/*
762 * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
763 * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
764 * so we need to override that forcing HID Output Reports on the Control EP.
765 *
766 * There is also another issue about HID Output Reports via USB, the Sixaxis
767 * does not want the report_id as part of the data packet, so we have to
768 * discard buf[0] when sending the actual control message, even for numbered
769 * reports, humpf!
770 */
Antonio Ospite569b10a2010-10-19 16:13:10 +0200771static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
772 size_t count, unsigned char report_type)
773{
774 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
775 struct usb_device *dev = interface_to_usbdev(intf);
776 struct usb_host_interface *interface = intf->cur_altsetting;
777 int report_id = buf[0];
778 int ret;
779
Antonio Ospite5710fab2011-02-20 18:26:45 +0100780 if (report_type == HID_OUTPUT_REPORT) {
781 /* Don't send the Report ID */
782 buf++;
783 count--;
784 }
785
Antonio Ospite569b10a2010-10-19 16:13:10 +0200786 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
787 HID_REQ_SET_REPORT,
788 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
789 ((report_type + 1) << 8) | report_id,
790 interface->desc.bInterfaceNumber, buf, count,
791 USB_CTRL_SET_TIMEOUT);
792
Antonio Ospite5710fab2011-02-20 18:26:45 +0100793 /* Count also the Report ID, in case of an Output report. */
794 if (ret > 0 && report_type == HID_OUTPUT_REPORT)
795 ret++;
796
Antonio Ospite569b10a2010-10-19 16:13:10 +0200797 return ret;
798}
799
Jiri Slabybd28ce02008-06-25 23:47:04 +0200800/*
801 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
802 * to "operational". Without this, the ps3 controller will not report any
803 * events.
804 */
Antonio Ospite816651a2010-05-03 22:15:55 +0200805static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +0200806{
Jiri Slabybd28ce02008-06-25 23:47:04 +0200807 int ret;
808 char *buf = kmalloc(18, GFP_KERNEL);
809
810 if (!buf)
811 return -ENOMEM;
812
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +0200813 ret = hdev->hid_get_raw_report(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT);
814
Jiri Slabybd28ce02008-06-25 23:47:04 +0200815 if (ret < 0)
Joe Perches4291ee32010-12-09 19:29:03 -0800816 hid_err(hdev, "can't set operational mode\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +0200817
818 kfree(buf);
819
820 return ret;
821}
822
Antonio Ospite816651a2010-05-03 22:15:55 +0200823static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +0000824{
Antonio Ospitefddb33f2010-05-03 17:19:03 +0200825 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
Bastien Noceraf9ce7c22010-01-20 12:01:53 +0000826 return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
827}
828
Frank Praznik60781cf2014-01-11 15:13:15 -0500829static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
Colin Leitnerf04d5142013-05-27 23:41:05 +0200830{
831 struct list_head *report_list =
832 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
833 struct hid_report *report = list_entry(report_list->next,
834 struct hid_report, list);
835 __s32 *value = report->field[0]->value;
836
837 value[0] = 0x00;
Frank Praznik60781cf2014-01-11 15:13:15 -0500838 value[1] = leds[0] ? 0xff : 0x00;
839 value[2] = leds[1] ? 0xff : 0x00;
840 value[3] = leds[2] ? 0xff : 0x00;
841 value[4] = leds[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200842 value[5] = 0x00;
843 value[6] = 0x00;
844 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
845}
846
Frank Praznik60781cf2014-01-11 15:13:15 -0500847static void sony_set_leds(struct hid_device *hdev, const __u8 *leds, int count)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100848{
849 struct sony_sc *drv_data = hid_get_drvdata(hdev);
Frank Praznik60781cf2014-01-11 15:13:15 -0500850 int n;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100851
Frank Praznik60781cf2014-01-11 15:13:15 -0500852 BUG_ON(count > MAX_LEDS);
853
854 if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100855 buzz_set_leds(hdev, leds);
Frank Praznik60781cf2014-01-11 15:13:15 -0500856 } else if ((drv_data->quirks & SIXAXIS_CONTROLLER_USB) ||
Frank Praznik8ab16762014-01-16 21:42:31 -0500857 (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB)) {
Frank Praznik60781cf2014-01-11 15:13:15 -0500858 for (n = 0; n < count; n++)
859 drv_data->led_state[n] = leds[n];
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100860 schedule_work(&drv_data->state_worker);
861 }
862}
863
Sven Eckelmannc5382512013-11-19 20:26:30 +0100864static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +0200865 enum led_brightness value)
866{
867 struct device *dev = led->dev->parent;
868 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
869 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200870
871 int n;
872
873 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +0100874 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +0200875 hid_err(hdev, "No device data\n");
876 return;
877 }
Colin Leitnerf04d5142013-05-27 23:41:05 +0200878
Frank Praznik60781cf2014-01-11 15:13:15 -0500879 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann2251b852013-11-19 20:26:31 +0100880 if (led == drv_data->leds[n]) {
Frank Praznik60781cf2014-01-11 15:13:15 -0500881 if (value != drv_data->led_state[n]) {
882 drv_data->led_state[n] = value;
883 sony_set_leds(hdev, drv_data->led_state, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +0200884 }
885 break;
886 }
887 }
888}
889
Sven Eckelmannc5382512013-11-19 20:26:30 +0100890static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +0200891{
892 struct device *dev = led->dev->parent;
893 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
894 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200895
896 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200897
898 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +0100899 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +0200900 hid_err(hdev, "No device data\n");
901 return LED_OFF;
902 }
Colin Leitnerf04d5142013-05-27 23:41:05 +0200903
Frank Praznik60781cf2014-01-11 15:13:15 -0500904 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -0700905 if (led == drv_data->leds[n])
906 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +0200907 }
908
Simon Wood7db75042014-02-05 12:34:18 -0700909 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200910}
Colin Leitnerf04d5142013-05-27 23:41:05 +0200911
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100912static void sony_leds_remove(struct hid_device *hdev)
913{
914 struct sony_sc *drv_data;
915 struct led_classdev *led;
916 int n;
917
918 drv_data = hid_get_drvdata(hdev);
919 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
920
Frank Praznik60781cf2014-01-11 15:13:15 -0500921 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100922 led = drv_data->leds[n];
923 drv_data->leds[n] = NULL;
924 if (!led)
925 continue;
926 led_classdev_unregister(led);
927 kfree(led);
928 }
Frank Praznik60781cf2014-01-11 15:13:15 -0500929
930 drv_data->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100931}
932
Sven Eckelmannc5382512013-11-19 20:26:30 +0100933static int sony_leds_init(struct hid_device *hdev)
Colin Leitnerf04d5142013-05-27 23:41:05 +0200934{
935 struct sony_sc *drv_data;
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200936 int n, ret = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -0500937 int max_brightness;
Frank Praznik61ebca92014-01-20 12:27:02 -0500938 int use_colors;
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200939 struct led_classdev *led;
940 size_t name_sz;
941 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100942 size_t name_len;
943 const char *name_fmt;
Frank Praznik61ebca92014-01-20 12:27:02 -0500944 static const char * const color_str[] = { "red", "green", "blue" };
Frank Praznik60781cf2014-01-11 15:13:15 -0500945 static const __u8 initial_values[MAX_LEDS] = { 0x00, 0x00, 0x00, 0x00 };
Colin Leitnerf04d5142013-05-27 23:41:05 +0200946
947 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100948 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +0200949
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100950 if (drv_data->quirks & BUZZ_CONTROLLER) {
Frank Praznik61ebca92014-01-20 12:27:02 -0500951 drv_data->led_count = 4;
952 max_brightness = 1;
953 use_colors = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100954 name_len = strlen("::buzz#");
955 name_fmt = "%s::buzz%d";
956 /* Validate expected report characteristics. */
957 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
958 return -ENODEV;
Frank Praznik61ebca92014-01-20 12:27:02 -0500959 } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik60781cf2014-01-11 15:13:15 -0500960 drv_data->led_count = 3;
961 max_brightness = 255;
Frank Praznik61ebca92014-01-20 12:27:02 -0500962 use_colors = 1;
963 name_len = 0;
964 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -0500965 } else {
966 drv_data->led_count = 4;
967 max_brightness = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -0500968 use_colors = 0;
969 name_len = strlen("::sony#");
970 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -0500971 }
972
Colin Leitnerf04d5142013-05-27 23:41:05 +0200973 /* Clear LEDs as we have no way of reading their initial state. This is
974 * only relevant if the driver is loaded after somebody actively set the
975 * LEDs to on */
Frank Praznik60781cf2014-01-11 15:13:15 -0500976 sony_set_leds(hdev, initial_values, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +0200977
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100978 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200979
Frank Praznik60781cf2014-01-11 15:13:15 -0500980 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -0500981
982 if (use_colors)
983 name_sz = strlen(dev_name(&hdev->dev)) + strlen(color_str[n]) + 2;
984
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200985 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
986 if (!led) {
987 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +0100988 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200989 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200990 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200991
992 name = (void *)(&led[1]);
Frank Praznik61ebca92014-01-20 12:27:02 -0500993 if (use_colors)
994 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), color_str[n]);
995 else
996 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200997 led->name = name;
998 led->brightness = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -0500999 led->max_brightness = max_brightness;
Sven Eckelmannc5382512013-11-19 20:26:30 +01001000 led->brightness_get = sony_led_get_brightness;
1001 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001002
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001003 ret = led_classdev_register(&hdev->dev, led);
1004 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001005 hid_err(hdev, "Failed to register LED %d\n", n);
1006 kfree(led);
1007 goto error_leds;
1008 }
1009
Sven Eckelmann2251b852013-11-19 20:26:31 +01001010 drv_data->leds[n] = led;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001011 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001012
1013 return ret;
1014
Colin Leitnerf04d5142013-05-27 23:41:05 +02001015error_leds:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001016 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001017
Colin Leitnerf04d5142013-05-27 23:41:05 +02001018 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001019}
1020
Frank Praznikcad665a2014-01-11 15:13:54 -05001021static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001022{
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001023 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001024 unsigned char buf[] = {
1025 0x01,
1026 0x00, 0xff, 0x00, 0xff, 0x00,
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001027 0x00, 0x00, 0x00, 0x00, 0x00,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001028 0xff, 0x27, 0x10, 0x00, 0x32,
1029 0xff, 0x27, 0x10, 0x00, 0x32,
1030 0xff, 0x27, 0x10, 0x00, 0x32,
1031 0xff, 0x27, 0x10, 0x00, 0x32,
1032 0x00, 0x00, 0x00, 0x00, 0x00
1033 };
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001034
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001035#ifdef CONFIG_SONY_FF
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001036 buf[3] = sc->right ? 1 : 0;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001037 buf[5] = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001038#endif
1039
Frank Praznik60781cf2014-01-11 15:13:15 -05001040 buf[10] |= sc->led_state[0] << 1;
1041 buf[10] |= sc->led_state[1] << 2;
1042 buf[10] |= sc->led_state[2] << 3;
1043 buf[10] |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001044
1045 sc->hdev->hid_output_raw_report(sc->hdev, buf, sizeof(buf),
1046 HID_OUTPUT_REPORT);
1047}
1048
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001049static void dualshock4_state_worker(struct work_struct *work)
1050{
1051 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001052 struct hid_device *hdev = sc->hdev;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001053 struct hid_report *report = sc->output_report;
1054 __s32 *value = report->field[0]->value;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001055
Frank Praznik0da8ea62014-01-16 21:42:51 -05001056 value[0] = 0x03;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001057
1058#ifdef CONFIG_SONY_FF
Frank Praznik0da8ea62014-01-16 21:42:51 -05001059 value[3] = sc->right;
1060 value[4] = sc->left;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001061#endif
1062
Frank Praznik0da8ea62014-01-16 21:42:51 -05001063 value[5] = sc->led_state[0];
1064 value[6] = sc->led_state[1];
1065 value[7] = sc->led_state[2];
Frank Praznik60781cf2014-01-11 15:13:15 -05001066
Frank Praznik0da8ea62014-01-16 21:42:51 -05001067 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001068}
1069
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001070#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001071static int sony_play_effect(struct input_dev *dev, void *data,
1072 struct ff_effect *effect)
1073{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001074 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001075 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001076
1077 if (effect->type != FF_RUMBLE)
1078 return 0;
1079
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001080 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001081 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001082
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001083 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001084 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001085}
1086
1087static int sony_init_ff(struct hid_device *hdev)
1088{
1089 struct hid_input *hidinput = list_entry(hdev->inputs.next,
1090 struct hid_input, list);
1091 struct input_dev *input_dev = hidinput->input;
1092
1093 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1094 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1095}
1096
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001097static void sony_destroy_ff(struct hid_device *hdev)
1098{
1099 struct sony_sc *sc = hid_get_drvdata(hdev);
1100
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001101 cancel_work_sync(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001102}
1103
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001104#else
1105static int sony_init_ff(struct hid_device *hdev)
1106{
1107 return 0;
1108}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001109
1110static void sony_destroy_ff(struct hid_device *hdev)
1111{
1112}
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001113#endif
1114
Frank Praznikd902f472014-01-27 10:17:36 -05001115static int sony_battery_get_property(struct power_supply *psy,
1116 enum power_supply_property psp,
1117 union power_supply_propval *val)
1118{
1119 struct sony_sc *sc = container_of(psy, struct sony_sc, battery);
1120 unsigned long flags;
1121 int ret = 0;
1122 u8 battery_charging, battery_capacity, cable_state;
1123
1124 spin_lock_irqsave(&sc->lock, flags);
1125 battery_charging = sc->battery_charging;
1126 battery_capacity = sc->battery_capacity;
1127 cable_state = sc->cable_state;
1128 spin_unlock_irqrestore(&sc->lock, flags);
1129
1130 switch (psp) {
1131 case POWER_SUPPLY_PROP_PRESENT:
1132 val->intval = 1;
1133 break;
1134 case POWER_SUPPLY_PROP_SCOPE:
1135 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1136 break;
1137 case POWER_SUPPLY_PROP_CAPACITY:
1138 val->intval = battery_capacity;
1139 break;
1140 case POWER_SUPPLY_PROP_STATUS:
1141 if (battery_charging)
1142 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1143 else
1144 if (battery_capacity == 100 && cable_state)
1145 val->intval = POWER_SUPPLY_STATUS_FULL;
1146 else
1147 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1148 break;
1149 default:
1150 ret = -EINVAL;
1151 break;
1152 }
1153 return ret;
1154}
1155
1156static int sony_battery_probe(struct sony_sc *sc)
1157{
1158 static atomic_t power_id_seq = ATOMIC_INIT(0);
1159 unsigned long power_id;
1160 struct hid_device *hdev = sc->hdev;
1161 int ret;
1162
1163 power_id = (unsigned long)atomic_inc_return(&power_id_seq);
1164
1165 sc->battery.properties = sony_battery_props;
1166 sc->battery.num_properties = ARRAY_SIZE(sony_battery_props);
1167 sc->battery.get_property = sony_battery_get_property;
1168 sc->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1169 sc->battery.use_for_apm = 0;
1170 sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%lu",
1171 power_id);
1172 if (!sc->battery.name)
1173 return -ENOMEM;
1174
1175 ret = power_supply_register(&hdev->dev, &sc->battery);
1176 if (ret) {
1177 hid_err(hdev, "Unable to register battery device\n");
1178 goto err_free;
1179 }
1180
1181 power_supply_powers(&sc->battery, &hdev->dev);
1182 return 0;
1183
1184err_free:
1185 kfree(sc->battery.name);
1186 sc->battery.name = NULL;
1187 return ret;
1188}
1189
1190static void sony_battery_remove(struct sony_sc *sc)
1191{
1192 if (!sc->battery.name)
1193 return;
1194
1195 power_supply_unregister(&sc->battery);
1196 kfree(sc->battery.name);
1197 sc->battery.name = NULL;
1198}
1199
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001200static int sony_set_output_report(struct sony_sc *sc, int req_id, int req_size)
1201{
1202 struct list_head *head, *list;
1203 struct hid_report *report;
1204 struct hid_device *hdev = sc->hdev;
1205
1206 list = &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1207
1208 list_for_each(head, list) {
1209 report = list_entry(head, struct hid_report, list);
1210
1211 if (report->id == req_id) {
1212 if (report->size < req_size) {
1213 hid_err(hdev, "Output report 0x%02x (%i bits) is smaller than requested size (%i bits)\n",
1214 req_id, report->size, req_size);
1215 return -EINVAL;
1216 }
1217 sc->output_report = report;
1218 return 0;
1219 }
1220 }
1221
1222 hid_err(hdev, "Unable to locate output report 0x%02x\n", req_id);
1223
1224 return -EINVAL;
1225}
1226
Frank Praznike5606232014-01-27 10:17:37 -05001227static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
1228 int w, int h)
1229{
1230 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
1231 struct hid_input, list);
1232 struct input_dev *input_dev = hidinput->input;
1233 int ret;
1234
1235 ret = input_mt_init_slots(input_dev, touch_count, 0);
1236 if (ret < 0) {
1237 hid_err(sc->hdev, "Unable to initialize multi-touch slots\n");
1238 return ret;
1239 }
1240
1241 input_set_abs_params(input_dev, ABS_MT_POSITION_X, 0, w, 0, 0);
1242 input_set_abs_params(input_dev, ABS_MT_POSITION_Y, 0, h, 0, 0);
1243
1244 return 0;
1245}
1246
Jiri Slabybd28ce02008-06-25 23:47:04 +02001247static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1248{
1249 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001250 unsigned long quirks = id->driver_data;
1251 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001252 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001253
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001254 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001255 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08001256 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001257 return -ENOMEM;
1258 }
1259
1260 sc->quirks = quirks;
1261 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001262 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001263
Jiri Slabybd28ce02008-06-25 23:47:04 +02001264 ret = hid_parse(hdev);
1265 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001266 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001267 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001268 }
1269
Colin Leitnerf04d5142013-05-27 23:41:05 +02001270 if (sc->quirks & VAIO_RDESC_CONSTANT)
1271 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1272 else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
1273 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1274 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1275 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1276
1277 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001278 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001279 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001280 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001281 }
1282
Antonio Ospite569b10a2010-10-19 16:13:10 +02001283 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1284 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
Antonio Ospite816651a2010-05-03 22:15:55 +02001285 ret = sixaxis_set_operational_usb(hdev);
Frank Praznikcad665a2014-01-11 15:13:54 -05001286 INIT_WORK(&sc->state_worker, sixaxis_state_worker);
Antonio Ospite569b10a2010-10-19 16:13:10 +02001287 }
Antonio Ospite816651a2010-05-03 22:15:55 +02001288 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1289 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik8ab16762014-01-16 21:42:31 -05001290 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001291 /* Report 5 (31 bytes) is used to send data to the controller via USB */
1292 ret = sony_set_output_report(sc, 0x05, 248);
1293 if (ret < 0)
1294 goto err_stop;
1295
Frank Praznike5606232014-01-27 10:17:37 -05001296 /* The Dualshock 4 touchpad supports 2 touches and has a
1297 * resolution of 1920x940.
1298 */
1299 ret = sony_register_touchpad(sc, 2, 1920, 940);
1300 if (ret < 0)
1301 goto err_stop;
1302
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001303 INIT_WORK(&sc->state_worker, dualshock4_state_worker);
1304 } else {
1305 ret = 0;
1306 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001307
Jiri Kosina4dfdc462008-12-30 00:49:59 +01001308 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001309 goto err_stop;
1310
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001311 if (sc->quirks & SONY_LED_SUPPORT) {
1312 ret = sony_leds_init(hdev);
1313 if (ret < 0)
1314 goto err_stop;
1315 }
1316
Frank Praznikd902f472014-01-27 10:17:36 -05001317 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1318 ret = sony_battery_probe(sc);
1319 if (ret < 0)
1320 goto err_stop;
1321
1322 /* Open the device to receive reports with battery info */
1323 ret = hid_hw_open(hdev);
1324 if (ret < 0) {
1325 hid_err(hdev, "hw open failed\n");
1326 goto err_stop;
1327 }
1328 }
1329
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001330 ret = sony_init_ff(hdev);
1331 if (ret < 0)
Frank Praznikd902f472014-01-27 10:17:36 -05001332 goto err_close;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001333
Jiri Slabybd28ce02008-06-25 23:47:04 +02001334 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001335err_close:
1336 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001337err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001338 if (sc->quirks & SONY_LED_SUPPORT)
1339 sony_leds_remove(hdev);
Frank Praznikd902f472014-01-27 10:17:36 -05001340 if (sc->quirks & SONY_BATTERY_SUPPORT)
1341 sony_battery_remove(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001342 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001343 return ret;
1344}
1345
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001346static void sony_remove(struct hid_device *hdev)
1347{
Colin Leitnerf04d5142013-05-27 23:41:05 +02001348 struct sony_sc *sc = hid_get_drvdata(hdev);
1349
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001350 if (sc->quirks & SONY_LED_SUPPORT)
Sven Eckelmannc5382512013-11-19 20:26:30 +01001351 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001352
Frank Praznikd902f472014-01-27 10:17:36 -05001353 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1354 hid_hw_close(hdev);
1355 sony_battery_remove(sc);
1356 }
1357
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001358 sony_destroy_ff(hdev);
1359
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001360 hid_hw_stop(hdev);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001361}
1362
Jiri Slabybd28ce02008-06-25 23:47:04 +02001363static const struct hid_device_id sony_devices[] = {
Antonio Ospite816651a2010-05-03 22:15:55 +02001364 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1365 .driver_data = SIXAXIS_CONTROLLER_USB },
Jiri Kosina35dca5b2011-04-28 15:43:13 +02001366 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
1367 .driver_data = SIXAXIS_CONTROLLER_USB },
Antonio Ospite816651a2010-05-03 22:15:55 +02001368 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1369 .driver_data = SIXAXIS_CONTROLLER_BT },
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001370 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
1371 .driver_data = VAIO_RDESC_CONSTANT },
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +09001372 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
1373 .driver_data = VAIO_RDESC_CONSTANT },
Colin Leitnerf04d5142013-05-27 23:41:05 +02001374 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
1375 * Logitech joystick from the device descriptor. */
1376 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
1377 .driver_data = BUZZ_CONTROLLER },
1378 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
1379 .driver_data = BUZZ_CONTROLLER },
Jiri Kosina078328d2013-06-13 12:03:49 +02001380 /* PS3 BD Remote Control */
1381 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
1382 .driver_data = PS3REMOTE },
1383 /* Logitech Harmony Adapter for PS3 */
1384 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
1385 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001386 /* Sony Dualshock 4 controllers for PS4 */
1387 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001388 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001389 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001390 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02001391 { }
1392};
1393MODULE_DEVICE_TABLE(hid, sony_devices);
1394
1395static struct hid_driver sony_driver = {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001396 .name = "sony",
1397 .id_table = sony_devices,
1398 .input_mapping = sony_mapping,
1399 .probe = sony_probe,
1400 .remove = sony_remove,
1401 .report_fixup = sony_report_fixup,
1402 .raw_event = sony_raw_event
Jiri Slabybd28ce02008-06-25 23:47:04 +02001403};
H Hartley Sweetenf4254582012-12-17 15:28:26 -07001404module_hid_driver(sony_driver);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001405
Jiri Slabybd28ce02008-06-25 23:47:04 +02001406MODULE_LICENSE("GPL");