blob: 2f19b15f47f2ffca68de731ef0e0ba3ae53c972b [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>
Jiri Slabybd28ce02008-06-25 23:47:04 +020033
34#include "hid-ids.h"
35
Frank Praznik6c79c182014-01-16 21:43:03 -050036#define VAIO_RDESC_CONSTANT BIT(0)
37#define SIXAXIS_CONTROLLER_USB BIT(1)
38#define SIXAXIS_CONTROLLER_BT BIT(2)
39#define BUZZ_CONTROLLER BIT(3)
40#define PS3REMOTE BIT(4)
Frank Praznik8ab16762014-01-16 21:42:31 -050041#define DUALSHOCK4_CONTROLLER_USB BIT(5)
42#define DUALSHOCK4_CONTROLLER_BT BIT(6)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020043
Frank Praznik8ab16762014-01-16 21:42:31 -050044#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER_USB | BUZZ_CONTROLLER | DUALSHOCK4_CONTROLLER_USB)
Frank Praznik5f5750d2014-02-19 13:09:22 -050045#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER_USB | DUALSHOCK4_CONTROLLER_USB)
Frank Praznik60781cf2014-01-11 15:13:15 -050046
47#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010048
Simon Wood61ab44b2011-06-10 12:00:26 +020049static const u8 sixaxis_rdesc_fixup[] = {
50 0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
51 0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
52 0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
53};
54
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -020055static const u8 sixaxis_rdesc_fixup2[] = {
56 0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
57 0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
58 0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
59 0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
60 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
61 0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
62 0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
63 0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
64 0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
65 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
66 0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
67 0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
68 0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
69 0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
70 0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
71 0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
72 0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
73 0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
74 0xb1, 0x02, 0xc0, 0xc0,
75};
76
Frank Praznik58d70272014-01-20 12:27:01 -050077/* The default descriptor doesn't provide mapping for the accelerometers
78 * or orientation sensors. This fixed descriptor maps the accelerometers
79 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
80 * to usage values 0x43, 0x44 and 0x45.
81 */
Frank Prazniked19d8c2014-01-16 21:43:12 -050082static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -050083 0x05, 0x01, /* Usage Page (Desktop), */
84 0x09, 0x05, /* Usage (Gamepad), */
85 0xA1, 0x01, /* Collection (Application), */
86 0x85, 0x01, /* Report ID (1), */
87 0x09, 0x30, /* Usage (X), */
88 0x09, 0x31, /* Usage (Y), */
89 0x09, 0x32, /* Usage (Z), */
90 0x09, 0x35, /* Usage (Rz), */
91 0x15, 0x00, /* Logical Minimum (0), */
92 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
93 0x75, 0x08, /* Report Size (8), */
94 0x95, 0x04, /* Report Count (4), */
95 0x81, 0x02, /* Input (Variable), */
96 0x09, 0x39, /* Usage (Hat Switch), */
97 0x15, 0x00, /* Logical Minimum (0), */
98 0x25, 0x07, /* Logical Maximum (7), */
99 0x35, 0x00, /* Physical Minimum (0), */
100 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
101 0x65, 0x14, /* Unit (Degrees), */
102 0x75, 0x04, /* Report Size (4), */
103 0x95, 0x01, /* Report Count (1), */
104 0x81, 0x42, /* Input (Variable, Null State), */
105 0x65, 0x00, /* Unit, */
106 0x05, 0x09, /* Usage Page (Button), */
107 0x19, 0x01, /* Usage Minimum (01h), */
108 0x29, 0x0E, /* Usage Maximum (0Eh), */
109 0x15, 0x00, /* Logical Minimum (0), */
110 0x25, 0x01, /* Logical Maximum (1), */
111 0x75, 0x01, /* Report Size (1), */
112 0x95, 0x0E, /* Report Count (14), */
113 0x81, 0x02, /* Input (Variable), */
114 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
115 0x09, 0x20, /* Usage (20h), */
116 0x75, 0x06, /* Report Size (6), */
117 0x95, 0x01, /* Report Count (1), */
118 0x15, 0x00, /* Logical Minimum (0), */
119 0x25, 0x7F, /* Logical Maximum (127), */
120 0x81, 0x02, /* Input (Variable), */
121 0x05, 0x01, /* Usage Page (Desktop), */
122 0x09, 0x33, /* Usage (Rx), */
123 0x09, 0x34, /* Usage (Ry), */
124 0x15, 0x00, /* Logical Minimum (0), */
125 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
126 0x75, 0x08, /* Report Size (8), */
127 0x95, 0x02, /* Report Count (2), */
128 0x81, 0x02, /* Input (Variable), */
129 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
130 0x09, 0x21, /* Usage (21h), */
131 0x95, 0x03, /* Report Count (3), */
132 0x81, 0x02, /* Input (Variable), */
133 0x05, 0x01, /* Usage Page (Desktop), */
134 0x19, 0x40, /* Usage Minimum (40h), */
135 0x29, 0x42, /* Usage Maximum (42h), */
136 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
137 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
138 0x75, 0x10, /* Report Size (16), */
139 0x95, 0x03, /* Report Count (3), */
140 0x81, 0x02, /* Input (Variable), */
141 0x19, 0x43, /* Usage Minimum (43h), */
142 0x29, 0x45, /* Usage Maximum (45h), */
143 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
144 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
145 0x95, 0x03, /* Report Count (3), */
146 0x81, 0x02, /* Input (Variable), */
147 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
148 0x09, 0x21, /* Usage (21h), */
149 0x15, 0x00, /* Logical Minimum (0), */
150 0x25, 0xFF, /* Logical Maximum (255), */
151 0x75, 0x08, /* Report Size (8), */
152 0x95, 0x27, /* Report Count (39), */
153 0x81, 0x02, /* Input (Variable), */
154 0x85, 0x05, /* Report ID (5), */
155 0x09, 0x22, /* Usage (22h), */
156 0x95, 0x1F, /* Report Count (31), */
157 0x91, 0x02, /* Output (Variable), */
158 0x85, 0x04, /* Report ID (4), */
159 0x09, 0x23, /* Usage (23h), */
160 0x95, 0x24, /* Report Count (36), */
161 0xB1, 0x02, /* Feature (Variable), */
162 0x85, 0x02, /* Report ID (2), */
163 0x09, 0x24, /* Usage (24h), */
164 0x95, 0x24, /* Report Count (36), */
165 0xB1, 0x02, /* Feature (Variable), */
166 0x85, 0x08, /* Report ID (8), */
167 0x09, 0x25, /* Usage (25h), */
168 0x95, 0x03, /* Report Count (3), */
169 0xB1, 0x02, /* Feature (Variable), */
170 0x85, 0x10, /* Report ID (16), */
171 0x09, 0x26, /* Usage (26h), */
172 0x95, 0x04, /* Report Count (4), */
173 0xB1, 0x02, /* Feature (Variable), */
174 0x85, 0x11, /* Report ID (17), */
175 0x09, 0x27, /* Usage (27h), */
176 0x95, 0x02, /* Report Count (2), */
177 0xB1, 0x02, /* Feature (Variable), */
178 0x85, 0x12, /* Report ID (18), */
179 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
180 0x09, 0x21, /* Usage (21h), */
181 0x95, 0x0F, /* Report Count (15), */
182 0xB1, 0x02, /* Feature (Variable), */
183 0x85, 0x13, /* Report ID (19), */
184 0x09, 0x22, /* Usage (22h), */
185 0x95, 0x16, /* Report Count (22), */
186 0xB1, 0x02, /* Feature (Variable), */
187 0x85, 0x14, /* Report ID (20), */
188 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
189 0x09, 0x20, /* Usage (20h), */
190 0x95, 0x10, /* Report Count (16), */
191 0xB1, 0x02, /* Feature (Variable), */
192 0x85, 0x15, /* Report ID (21), */
193 0x09, 0x21, /* Usage (21h), */
194 0x95, 0x2C, /* Report Count (44), */
195 0xB1, 0x02, /* Feature (Variable), */
196 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
197 0x85, 0x80, /* Report ID (128), */
198 0x09, 0x20, /* Usage (20h), */
199 0x95, 0x06, /* Report Count (6), */
200 0xB1, 0x02, /* Feature (Variable), */
201 0x85, 0x81, /* Report ID (129), */
202 0x09, 0x21, /* Usage (21h), */
203 0x95, 0x06, /* Report Count (6), */
204 0xB1, 0x02, /* Feature (Variable), */
205 0x85, 0x82, /* Report ID (130), */
206 0x09, 0x22, /* Usage (22h), */
207 0x95, 0x05, /* Report Count (5), */
208 0xB1, 0x02, /* Feature (Variable), */
209 0x85, 0x83, /* Report ID (131), */
210 0x09, 0x23, /* Usage (23h), */
211 0x95, 0x01, /* Report Count (1), */
212 0xB1, 0x02, /* Feature (Variable), */
213 0x85, 0x84, /* Report ID (132), */
214 0x09, 0x24, /* Usage (24h), */
215 0x95, 0x04, /* Report Count (4), */
216 0xB1, 0x02, /* Feature (Variable), */
217 0x85, 0x85, /* Report ID (133), */
218 0x09, 0x25, /* Usage (25h), */
219 0x95, 0x06, /* Report Count (6), */
220 0xB1, 0x02, /* Feature (Variable), */
221 0x85, 0x86, /* Report ID (134), */
222 0x09, 0x26, /* Usage (26h), */
223 0x95, 0x06, /* Report Count (6), */
224 0xB1, 0x02, /* Feature (Variable), */
225 0x85, 0x87, /* Report ID (135), */
226 0x09, 0x27, /* Usage (27h), */
227 0x95, 0x23, /* Report Count (35), */
228 0xB1, 0x02, /* Feature (Variable), */
229 0x85, 0x88, /* Report ID (136), */
230 0x09, 0x28, /* Usage (28h), */
231 0x95, 0x22, /* Report Count (34), */
232 0xB1, 0x02, /* Feature (Variable), */
233 0x85, 0x89, /* Report ID (137), */
234 0x09, 0x29, /* Usage (29h), */
235 0x95, 0x02, /* Report Count (2), */
236 0xB1, 0x02, /* Feature (Variable), */
237 0x85, 0x90, /* Report ID (144), */
238 0x09, 0x30, /* Usage (30h), */
239 0x95, 0x05, /* Report Count (5), */
240 0xB1, 0x02, /* Feature (Variable), */
241 0x85, 0x91, /* Report ID (145), */
242 0x09, 0x31, /* Usage (31h), */
243 0x95, 0x03, /* Report Count (3), */
244 0xB1, 0x02, /* Feature (Variable), */
245 0x85, 0x92, /* Report ID (146), */
246 0x09, 0x32, /* Usage (32h), */
247 0x95, 0x03, /* Report Count (3), */
248 0xB1, 0x02, /* Feature (Variable), */
249 0x85, 0x93, /* Report ID (147), */
250 0x09, 0x33, /* Usage (33h), */
251 0x95, 0x0C, /* Report Count (12), */
252 0xB1, 0x02, /* Feature (Variable), */
253 0x85, 0xA0, /* Report ID (160), */
254 0x09, 0x40, /* Usage (40h), */
255 0x95, 0x06, /* Report Count (6), */
256 0xB1, 0x02, /* Feature (Variable), */
257 0x85, 0xA1, /* Report ID (161), */
258 0x09, 0x41, /* Usage (41h), */
259 0x95, 0x01, /* Report Count (1), */
260 0xB1, 0x02, /* Feature (Variable), */
261 0x85, 0xA2, /* Report ID (162), */
262 0x09, 0x42, /* Usage (42h), */
263 0x95, 0x01, /* Report Count (1), */
264 0xB1, 0x02, /* Feature (Variable), */
265 0x85, 0xA3, /* Report ID (163), */
266 0x09, 0x43, /* Usage (43h), */
267 0x95, 0x30, /* Report Count (48), */
268 0xB1, 0x02, /* Feature (Variable), */
269 0x85, 0xA4, /* Report ID (164), */
270 0x09, 0x44, /* Usage (44h), */
271 0x95, 0x0D, /* Report Count (13), */
272 0xB1, 0x02, /* Feature (Variable), */
273 0x85, 0xA5, /* Report ID (165), */
274 0x09, 0x45, /* Usage (45h), */
275 0x95, 0x15, /* Report Count (21), */
276 0xB1, 0x02, /* Feature (Variable), */
277 0x85, 0xA6, /* Report ID (166), */
278 0x09, 0x46, /* Usage (46h), */
279 0x95, 0x15, /* Report Count (21), */
280 0xB1, 0x02, /* Feature (Variable), */
281 0x85, 0xF0, /* Report ID (240), */
282 0x09, 0x47, /* Usage (47h), */
283 0x95, 0x3F, /* Report Count (63), */
284 0xB1, 0x02, /* Feature (Variable), */
285 0x85, 0xF1, /* Report ID (241), */
286 0x09, 0x48, /* Usage (48h), */
287 0x95, 0x3F, /* Report Count (63), */
288 0xB1, 0x02, /* Feature (Variable), */
289 0x85, 0xF2, /* Report ID (242), */
290 0x09, 0x49, /* Usage (49h), */
291 0x95, 0x0F, /* Report Count (15), */
292 0xB1, 0x02, /* Feature (Variable), */
293 0x85, 0xA7, /* Report ID (167), */
294 0x09, 0x4A, /* Usage (4Ah), */
295 0x95, 0x01, /* Report Count (1), */
296 0xB1, 0x02, /* Feature (Variable), */
297 0x85, 0xA8, /* Report ID (168), */
298 0x09, 0x4B, /* Usage (4Bh), */
299 0x95, 0x01, /* Report Count (1), */
300 0xB1, 0x02, /* Feature (Variable), */
301 0x85, 0xA9, /* Report ID (169), */
302 0x09, 0x4C, /* Usage (4Ch), */
303 0x95, 0x08, /* Report Count (8), */
304 0xB1, 0x02, /* Feature (Variable), */
305 0x85, 0xAA, /* Report ID (170), */
306 0x09, 0x4E, /* Usage (4Eh), */
307 0x95, 0x01, /* Report Count (1), */
308 0xB1, 0x02, /* Feature (Variable), */
309 0x85, 0xAB, /* Report ID (171), */
310 0x09, 0x4F, /* Usage (4Fh), */
311 0x95, 0x39, /* Report Count (57), */
312 0xB1, 0x02, /* Feature (Variable), */
313 0x85, 0xAC, /* Report ID (172), */
314 0x09, 0x50, /* Usage (50h), */
315 0x95, 0x39, /* Report Count (57), */
316 0xB1, 0x02, /* Feature (Variable), */
317 0x85, 0xAD, /* Report ID (173), */
318 0x09, 0x51, /* Usage (51h), */
319 0x95, 0x0B, /* Report Count (11), */
320 0xB1, 0x02, /* Feature (Variable), */
321 0x85, 0xAE, /* Report ID (174), */
322 0x09, 0x52, /* Usage (52h), */
323 0x95, 0x01, /* Report Count (1), */
324 0xB1, 0x02, /* Feature (Variable), */
325 0x85, 0xAF, /* Report ID (175), */
326 0x09, 0x53, /* Usage (53h), */
327 0x95, 0x02, /* Report Count (2), */
328 0xB1, 0x02, /* Feature (Variable), */
329 0x85, 0xB0, /* Report ID (176), */
330 0x09, 0x54, /* Usage (54h), */
331 0x95, 0x3F, /* Report Count (63), */
332 0xB1, 0x02, /* Feature (Variable), */
333 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500334};
335
Jiri Kosina078328d2013-06-13 12:03:49 +0200336static __u8 ps3remote_rdesc[] = {
337 0x05, 0x01, /* GUsagePage Generic Desktop */
338 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
339 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
340
341 /* Use collection 1 for joypad buttons */
342 0xA1, 0x02, /* MCollection Logical (interrelated data) */
343
344 /* Ignore the 1st byte, maybe it is used for a controller
345 * number but it's not needed for correct operation */
346 0x75, 0x08, /* GReportSize 0x08 [8] */
347 0x95, 0x01, /* GReportCount 0x01 [1] */
348 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
349
350 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
351 * buttons multiple keypresses are allowed */
352 0x05, 0x09, /* GUsagePage Button */
353 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
354 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
355 0x14, /* GLogicalMinimum [0] */
356 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
357 0x75, 0x01, /* GReportSize 0x01 [1] */
358 0x95, 0x18, /* GReportCount 0x18 [24] */
359 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
360
361 0xC0, /* MEndCollection */
362
363 /* Use collection 2 for remote control buttons */
364 0xA1, 0x02, /* MCollection Logical (interrelated data) */
365
366 /* 5th byte is used for remote control buttons */
367 0x05, 0x09, /* GUsagePage Button */
368 0x18, /* LUsageMinimum [No button pressed] */
369 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
370 0x14, /* GLogicalMinimum [0] */
371 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
372 0x75, 0x08, /* GReportSize 0x08 [8] */
373 0x95, 0x01, /* GReportCount 0x01 [1] */
374 0x80, /* MInput */
375
376 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
377 * 0xff and 11th is for press indication */
378 0x75, 0x08, /* GReportSize 0x08 [8] */
379 0x95, 0x06, /* GReportCount 0x06 [6] */
380 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
381
382 /* 12th byte is for battery strength */
383 0x05, 0x06, /* GUsagePage Generic Device Controls */
384 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
385 0x14, /* GLogicalMinimum [0] */
386 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
387 0x75, 0x08, /* GReportSize 0x08 [8] */
388 0x95, 0x01, /* GReportCount 0x01 [1] */
389 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
390
391 0xC0, /* MEndCollection */
392
393 0xC0 /* MEndCollection [Game Pad] */
394};
395
396static const unsigned int ps3remote_keymap_joypad_buttons[] = {
397 [0x01] = KEY_SELECT,
398 [0x02] = BTN_THUMBL, /* L3 */
399 [0x03] = BTN_THUMBR, /* R3 */
400 [0x04] = BTN_START,
401 [0x05] = KEY_UP,
402 [0x06] = KEY_RIGHT,
403 [0x07] = KEY_DOWN,
404 [0x08] = KEY_LEFT,
405 [0x09] = BTN_TL2, /* L2 */
406 [0x0a] = BTN_TR2, /* R2 */
407 [0x0b] = BTN_TL, /* L1 */
408 [0x0c] = BTN_TR, /* R1 */
409 [0x0d] = KEY_OPTION, /* options/triangle */
410 [0x0e] = KEY_BACK, /* back/circle */
411 [0x0f] = BTN_0, /* cross */
412 [0x10] = KEY_SCREEN, /* view/square */
413 [0x11] = KEY_HOMEPAGE, /* PS button */
414 [0x14] = KEY_ENTER,
415};
416static const unsigned int ps3remote_keymap_remote_buttons[] = {
417 [0x00] = KEY_1,
418 [0x01] = KEY_2,
419 [0x02] = KEY_3,
420 [0x03] = KEY_4,
421 [0x04] = KEY_5,
422 [0x05] = KEY_6,
423 [0x06] = KEY_7,
424 [0x07] = KEY_8,
425 [0x08] = KEY_9,
426 [0x09] = KEY_0,
427 [0x0e] = KEY_ESC, /* return */
428 [0x0f] = KEY_CLEAR,
429 [0x16] = KEY_EJECTCD,
430 [0x1a] = KEY_MENU, /* top menu */
431 [0x28] = KEY_TIME,
432 [0x30] = KEY_PREVIOUS,
433 [0x31] = KEY_NEXT,
434 [0x32] = KEY_PLAY,
435 [0x33] = KEY_REWIND, /* scan back */
436 [0x34] = KEY_FORWARD, /* scan forward */
437 [0x38] = KEY_STOP,
438 [0x39] = KEY_PAUSE,
439 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
440 [0x60] = KEY_FRAMEBACK, /* slow/step back */
441 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
442 [0x63] = KEY_SUBTITLE,
443 [0x64] = KEY_AUDIO,
444 [0x65] = KEY_ANGLE,
445 [0x70] = KEY_INFO, /* display */
446 [0x80] = KEY_BLUE,
447 [0x81] = KEY_RED,
448 [0x82] = KEY_GREEN,
449 [0x83] = KEY_YELLOW,
450};
451
Colin Leitnerf04d5142013-05-27 23:41:05 +0200452static const unsigned int buzz_keymap[] = {
453 /* The controller has 4 remote buzzers, each with one LED and 5
454 * buttons.
455 *
456 * We use the mapping chosen by the controller, which is:
457 *
458 * Key Offset
459 * -------------------
460 * Buzz 1
461 * Blue 5
462 * Orange 4
463 * Green 3
464 * Yellow 2
465 *
466 * So, for example, the orange button on the third buzzer is mapped to
467 * BTN_TRIGGER_HAPPY14
468 */
469 [ 1] = BTN_TRIGGER_HAPPY1,
470 [ 2] = BTN_TRIGGER_HAPPY2,
471 [ 3] = BTN_TRIGGER_HAPPY3,
472 [ 4] = BTN_TRIGGER_HAPPY4,
473 [ 5] = BTN_TRIGGER_HAPPY5,
474 [ 6] = BTN_TRIGGER_HAPPY6,
475 [ 7] = BTN_TRIGGER_HAPPY7,
476 [ 8] = BTN_TRIGGER_HAPPY8,
477 [ 9] = BTN_TRIGGER_HAPPY9,
478 [10] = BTN_TRIGGER_HAPPY10,
479 [11] = BTN_TRIGGER_HAPPY11,
480 [12] = BTN_TRIGGER_HAPPY12,
481 [13] = BTN_TRIGGER_HAPPY13,
482 [14] = BTN_TRIGGER_HAPPY14,
483 [15] = BTN_TRIGGER_HAPPY15,
484 [16] = BTN_TRIGGER_HAPPY16,
485 [17] = BTN_TRIGGER_HAPPY17,
486 [18] = BTN_TRIGGER_HAPPY18,
487 [19] = BTN_TRIGGER_HAPPY19,
488 [20] = BTN_TRIGGER_HAPPY20,
489};
490
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200491struct sony_sc {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100492 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500493 struct led_classdev *leds[MAX_LEDS];
Frank Praznikc4e1ddf2014-01-17 14:46:26 -0500494 struct hid_report *output_report;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200495 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100496 struct work_struct state_worker;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200497
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100498#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100499 __u8 left;
500 __u8 right;
501#endif
502
Frank Praznik5f5750d2014-02-19 13:09:22 -0500503 __u8 worker_initialized;
Frank Praznik60781cf2014-01-11 15:13:15 -0500504 __u8 led_state[MAX_LEDS];
505 __u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200506};
507
Jiri Kosina078328d2013-06-13 12:03:49 +0200508static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
509 unsigned int *rsize)
510{
511 *rsize = sizeof(ps3remote_rdesc);
512 return ps3remote_rdesc;
513}
514
515static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
516 struct hid_field *field, struct hid_usage *usage,
517 unsigned long **bit, int *max)
518{
519 unsigned int key = usage->hid & HID_USAGE;
520
521 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
522 return -1;
523
524 switch (usage->collection_index) {
525 case 1:
526 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
527 return -1;
528
529 key = ps3remote_keymap_joypad_buttons[key];
530 if (!key)
531 return -1;
532 break;
533 case 2:
534 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
535 return -1;
536
537 key = ps3remote_keymap_remote_buttons[key];
538 if (!key)
539 return -1;
540 break;
541 default:
542 return -1;
543 }
544
545 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
546 return 1;
547}
548
549
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200550/* Sony Vaio VGX has wrongly mouse pointer declared as constant */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400551static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
552 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200553{
554 struct sony_sc *sc = hid_get_drvdata(hdev);
555
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900556 /*
557 * Some Sony RF receivers wrongly declare the mouse pointer as a
558 * a constant non-data variable.
559 */
560 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
561 /* usage page: generic desktop controls */
562 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
563 /* usage: mouse */
564 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
565 /* input (usage page for x,y axes): constant, variable, relative */
566 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +0900567 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900568 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200569 rdesc[55] = 0x06;
570 }
Simon Wood61ab44b2011-06-10 12:00:26 +0200571
Frank Prazniked19d8c2014-01-16 21:43:12 -0500572 /*
573 * The default Dualshock 4 USB descriptor doesn't assign
574 * the gyroscope values to corresponding axes so we need a
575 * modified one.
576 */
577 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
578 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
579 rdesc = dualshock4_usb_rdesc;
580 *rsize = sizeof(dualshock4_usb_rdesc);
581 }
582
Simon Wood61ab44b2011-06-10 12:00:26 +0200583 /* The HID descriptor exposed over BT has a trailing zero byte */
584 if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
585 ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
586 rdesc[83] == 0x75) {
587 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
588 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
589 sizeof(sixaxis_rdesc_fixup));
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200590 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
591 *rsize > sizeof(sixaxis_rdesc_fixup2)) {
592 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
593 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
594 *rsize = sizeof(sixaxis_rdesc_fixup2);
595 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
Simon Wood61ab44b2011-06-10 12:00:26 +0200596 }
Jiri Kosina078328d2013-06-13 12:03:49 +0200597
598 if (sc->quirks & PS3REMOTE)
599 return ps3remote_fixup(hdev, rdesc, rsize);
600
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400601 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200602}
603
Simon Woodc9e4d872011-06-10 12:00:27 +0200604static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
605 __u8 *rd, int size)
606{
607 struct sony_sc *sc = hid_get_drvdata(hdev);
608
609 /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
610 * has to be BYTE_SWAPPED before passing up to joystick interface
611 */
612 if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
613 rd[0] == 0x01 && size == 49) {
614 swap(rd[41], rd[42]);
615 swap(rd[43], rd[44]);
616 swap(rd[45], rd[46]);
617 swap(rd[47], rd[48]);
618 }
619
620 return 0;
621}
622
Colin Leitnerf04d5142013-05-27 23:41:05 +0200623static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
624 struct hid_field *field, struct hid_usage *usage,
625 unsigned long **bit, int *max)
626{
627 struct sony_sc *sc = hid_get_drvdata(hdev);
628
629 if (sc->quirks & BUZZ_CONTROLLER) {
630 unsigned int key = usage->hid & HID_USAGE;
631
632 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
633 return -1;
634
635 switch (usage->collection_index) {
636 case 1:
637 if (key >= ARRAY_SIZE(buzz_keymap))
638 return -1;
639
640 key = buzz_keymap[key];
641 if (!key)
642 return -1;
643 break;
644 default:
645 return -1;
646 }
647
648 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
649 return 1;
650 }
651
Jiri Kosina078328d2013-06-13 12:03:49 +0200652 if (sc->quirks & PS3REMOTE)
653 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
654
Benjamin Tissoires6f498012013-07-24 16:53:07 +0200655 /* Let hid-core decide for the others */
656 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200657}
658
Antonio Ospite5710fab2011-02-20 18:26:45 +0100659/*
660 * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
661 * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
662 * so we need to override that forcing HID Output Reports on the Control EP.
663 *
664 * There is also another issue about HID Output Reports via USB, the Sixaxis
665 * does not want the report_id as part of the data packet, so we have to
666 * discard buf[0] when sending the actual control message, even for numbered
667 * reports, humpf!
668 */
Antonio Ospite569b10a2010-10-19 16:13:10 +0200669static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
670 size_t count, unsigned char report_type)
671{
672 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
673 struct usb_device *dev = interface_to_usbdev(intf);
674 struct usb_host_interface *interface = intf->cur_altsetting;
675 int report_id = buf[0];
676 int ret;
677
Antonio Ospite5710fab2011-02-20 18:26:45 +0100678 if (report_type == HID_OUTPUT_REPORT) {
679 /* Don't send the Report ID */
680 buf++;
681 count--;
682 }
683
Antonio Ospite569b10a2010-10-19 16:13:10 +0200684 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
685 HID_REQ_SET_REPORT,
686 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
687 ((report_type + 1) << 8) | report_id,
688 interface->desc.bInterfaceNumber, buf, count,
689 USB_CTRL_SET_TIMEOUT);
690
Antonio Ospite5710fab2011-02-20 18:26:45 +0100691 /* Count also the Report ID, in case of an Output report. */
692 if (ret > 0 && report_type == HID_OUTPUT_REPORT)
693 ret++;
694
Antonio Ospite569b10a2010-10-19 16:13:10 +0200695 return ret;
696}
697
Jiri Slabybd28ce02008-06-25 23:47:04 +0200698/*
699 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
700 * to "operational". Without this, the ps3 controller will not report any
701 * events.
702 */
Antonio Ospite816651a2010-05-03 22:15:55 +0200703static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +0200704{
Jiri Slabybd28ce02008-06-25 23:47:04 +0200705 int ret;
706 char *buf = kmalloc(18, GFP_KERNEL);
707
708 if (!buf)
709 return -ENOMEM;
710
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +0200711 ret = hdev->hid_get_raw_report(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT);
712
Jiri Slabybd28ce02008-06-25 23:47:04 +0200713 if (ret < 0)
Joe Perches4291ee32010-12-09 19:29:03 -0800714 hid_err(hdev, "can't set operational mode\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +0200715
716 kfree(buf);
717
718 return ret;
719}
720
Antonio Ospite816651a2010-05-03 22:15:55 +0200721static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +0000722{
Antonio Ospitefddb33f2010-05-03 17:19:03 +0200723 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
Bastien Noceraf9ce7c22010-01-20 12:01:53 +0000724 return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
725}
726
Frank Praznik60781cf2014-01-11 15:13:15 -0500727static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
Colin Leitnerf04d5142013-05-27 23:41:05 +0200728{
729 struct list_head *report_list =
730 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
731 struct hid_report *report = list_entry(report_list->next,
732 struct hid_report, list);
733 __s32 *value = report->field[0]->value;
734
735 value[0] = 0x00;
Frank Praznik60781cf2014-01-11 15:13:15 -0500736 value[1] = leds[0] ? 0xff : 0x00;
737 value[2] = leds[1] ? 0xff : 0x00;
738 value[3] = leds[2] ? 0xff : 0x00;
739 value[4] = leds[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200740 value[5] = 0x00;
741 value[6] = 0x00;
742 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
743}
744
Frank Praznik60781cf2014-01-11 15:13:15 -0500745static void sony_set_leds(struct hid_device *hdev, const __u8 *leds, int count)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100746{
747 struct sony_sc *drv_data = hid_get_drvdata(hdev);
Frank Praznik60781cf2014-01-11 15:13:15 -0500748 int n;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100749
Frank Praznik60781cf2014-01-11 15:13:15 -0500750 BUG_ON(count > MAX_LEDS);
751
752 if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100753 buzz_set_leds(hdev, leds);
Frank Praznik60781cf2014-01-11 15:13:15 -0500754 } else if ((drv_data->quirks & SIXAXIS_CONTROLLER_USB) ||
Frank Praznik8ab16762014-01-16 21:42:31 -0500755 (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB)) {
Frank Praznik60781cf2014-01-11 15:13:15 -0500756 for (n = 0; n < count; n++)
757 drv_data->led_state[n] = leds[n];
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100758 schedule_work(&drv_data->state_worker);
759 }
760}
761
Sven Eckelmannc5382512013-11-19 20:26:30 +0100762static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +0200763 enum led_brightness value)
764{
765 struct device *dev = led->dev->parent;
766 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
767 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200768
769 int n;
770
771 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +0100772 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +0200773 hid_err(hdev, "No device data\n");
774 return;
775 }
Colin Leitnerf04d5142013-05-27 23:41:05 +0200776
Frank Praznik60781cf2014-01-11 15:13:15 -0500777 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann2251b852013-11-19 20:26:31 +0100778 if (led == drv_data->leds[n]) {
Frank Praznik60781cf2014-01-11 15:13:15 -0500779 if (value != drv_data->led_state[n]) {
780 drv_data->led_state[n] = value;
781 sony_set_leds(hdev, drv_data->led_state, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +0200782 }
783 break;
784 }
785 }
786}
787
Sven Eckelmannc5382512013-11-19 20:26:30 +0100788static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +0200789{
790 struct device *dev = led->dev->parent;
791 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
792 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200793
794 int n;
795 int on = 0;
796
797 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +0100798 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +0200799 hid_err(hdev, "No device data\n");
800 return LED_OFF;
801 }
Colin Leitnerf04d5142013-05-27 23:41:05 +0200802
Frank Praznik60781cf2014-01-11 15:13:15 -0500803 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann2251b852013-11-19 20:26:31 +0100804 if (led == drv_data->leds[n]) {
Frank Praznik60781cf2014-01-11 15:13:15 -0500805 on = !!(drv_data->led_state[n]);
Colin Leitnerf04d5142013-05-27 23:41:05 +0200806 break;
807 }
808 }
809
810 return on ? LED_FULL : LED_OFF;
811}
Colin Leitnerf04d5142013-05-27 23:41:05 +0200812
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100813static void sony_leds_remove(struct hid_device *hdev)
814{
815 struct sony_sc *drv_data;
816 struct led_classdev *led;
817 int n;
818
819 drv_data = hid_get_drvdata(hdev);
820 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
821
Frank Praznik60781cf2014-01-11 15:13:15 -0500822 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100823 led = drv_data->leds[n];
824 drv_data->leds[n] = NULL;
825 if (!led)
826 continue;
827 led_classdev_unregister(led);
828 kfree(led);
829 }
Frank Praznik60781cf2014-01-11 15:13:15 -0500830
831 drv_data->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100832}
833
Sven Eckelmannc5382512013-11-19 20:26:30 +0100834static int sony_leds_init(struct hid_device *hdev)
Colin Leitnerf04d5142013-05-27 23:41:05 +0200835{
836 struct sony_sc *drv_data;
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200837 int n, ret = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -0500838 int max_brightness;
Frank Praznik61ebca92014-01-20 12:27:02 -0500839 int use_colors;
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200840 struct led_classdev *led;
841 size_t name_sz;
842 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100843 size_t name_len;
844 const char *name_fmt;
Frank Praznik61ebca92014-01-20 12:27:02 -0500845 static const char * const color_str[] = { "red", "green", "blue" };
Frank Praznik60781cf2014-01-11 15:13:15 -0500846 static const __u8 initial_values[MAX_LEDS] = { 0x00, 0x00, 0x00, 0x00 };
Colin Leitnerf04d5142013-05-27 23:41:05 +0200847
848 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100849 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +0200850
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100851 if (drv_data->quirks & BUZZ_CONTROLLER) {
Frank Praznik61ebca92014-01-20 12:27:02 -0500852 drv_data->led_count = 4;
853 max_brightness = 1;
854 use_colors = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100855 name_len = strlen("::buzz#");
856 name_fmt = "%s::buzz%d";
857 /* Validate expected report characteristics. */
858 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
859 return -ENODEV;
Frank Praznik61ebca92014-01-20 12:27:02 -0500860 } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik60781cf2014-01-11 15:13:15 -0500861 drv_data->led_count = 3;
862 max_brightness = 255;
Frank Praznik61ebca92014-01-20 12:27:02 -0500863 use_colors = 1;
864 name_len = 0;
865 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -0500866 } else {
867 drv_data->led_count = 4;
868 max_brightness = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -0500869 use_colors = 0;
870 name_len = strlen("::sony#");
871 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -0500872 }
873
Colin Leitnerf04d5142013-05-27 23:41:05 +0200874 /* Clear LEDs as we have no way of reading their initial state. This is
875 * only relevant if the driver is loaded after somebody actively set the
876 * LEDs to on */
Frank Praznik60781cf2014-01-11 15:13:15 -0500877 sony_set_leds(hdev, initial_values, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +0200878
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100879 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200880
Frank Praznik60781cf2014-01-11 15:13:15 -0500881 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -0500882
883 if (use_colors)
884 name_sz = strlen(dev_name(&hdev->dev)) + strlen(color_str[n]) + 2;
885
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200886 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
887 if (!led) {
888 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +0100889 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200890 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200891 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200892
893 name = (void *)(&led[1]);
Frank Praznik61ebca92014-01-20 12:27:02 -0500894 if (use_colors)
895 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), color_str[n]);
896 else
897 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200898 led->name = name;
899 led->brightness = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -0500900 led->max_brightness = max_brightness;
Sven Eckelmannc5382512013-11-19 20:26:30 +0100901 led->brightness_get = sony_led_get_brightness;
902 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200903
Julia Lawall8cd5fcd2013-12-29 23:47:27 +0100904 ret = led_classdev_register(&hdev->dev, led);
905 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200906 hid_err(hdev, "Failed to register LED %d\n", n);
907 kfree(led);
908 goto error_leds;
909 }
910
Sven Eckelmann2251b852013-11-19 20:26:31 +0100911 drv_data->leds[n] = led;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200912 }
Colin Leitnerf04d5142013-05-27 23:41:05 +0200913
914 return ret;
915
Colin Leitnerf04d5142013-05-27 23:41:05 +0200916error_leds:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100917 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +0200918
Colin Leitnerf04d5142013-05-27 23:41:05 +0200919 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200920}
921
Frank Praznikcad665a2014-01-11 15:13:54 -0500922static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +0100923{
Sven Eckelmann92b5c412013-11-19 20:26:28 +0100924 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +0100925 unsigned char buf[] = {
926 0x01,
927 0x00, 0xff, 0x00, 0xff, 0x00,
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100928 0x00, 0x00, 0x00, 0x00, 0x00,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +0100929 0xff, 0x27, 0x10, 0x00, 0x32,
930 0xff, 0x27, 0x10, 0x00, 0x32,
931 0xff, 0x27, 0x10, 0x00, 0x32,
932 0xff, 0x27, 0x10, 0x00, 0x32,
933 0x00, 0x00, 0x00, 0x00, 0x00
934 };
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100935
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100936#ifdef CONFIG_SONY_FF
Frank Praznik0bd88dd2014-01-11 15:12:42 -0500937 buf[3] = sc->right ? 1 : 0;
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100938 buf[5] = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100939#endif
940
Frank Praznik60781cf2014-01-11 15:13:15 -0500941 buf[10] |= sc->led_state[0] << 1;
942 buf[10] |= sc->led_state[1] << 2;
943 buf[10] |= sc->led_state[2] << 3;
944 buf[10] |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100945
946 sc->hdev->hid_output_raw_report(sc->hdev, buf, sizeof(buf),
947 HID_OUTPUT_REPORT);
948}
949
Frank Praznik0bd88dd2014-01-11 15:12:42 -0500950static void dualshock4_state_worker(struct work_struct *work)
951{
952 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -0500953 struct hid_device *hdev = sc->hdev;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -0500954 struct hid_report *report = sc->output_report;
955 __s32 *value = report->field[0]->value;
Frank Praznik0da8ea62014-01-16 21:42:51 -0500956
Frank Praznik0da8ea62014-01-16 21:42:51 -0500957 value[0] = 0x03;
Frank Praznik0bd88dd2014-01-11 15:12:42 -0500958
959#ifdef CONFIG_SONY_FF
Frank Praznik0da8ea62014-01-16 21:42:51 -0500960 value[3] = sc->right;
961 value[4] = sc->left;
Frank Praznik0bd88dd2014-01-11 15:12:42 -0500962#endif
963
Frank Praznik0da8ea62014-01-16 21:42:51 -0500964 value[5] = sc->led_state[0];
965 value[6] = sc->led_state[1];
966 value[7] = sc->led_state[2];
Frank Praznik60781cf2014-01-11 15:13:15 -0500967
Frank Praznik0da8ea62014-01-16 21:42:51 -0500968 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -0500969}
970
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100971#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100972static int sony_play_effect(struct input_dev *dev, void *data,
973 struct ff_effect *effect)
974{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +0100975 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100976 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +0100977
978 if (effect->type != FF_RUMBLE)
979 return 0;
980
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100981 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -0500982 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +0100983
Sven Eckelmann92b5c412013-11-19 20:26:28 +0100984 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100985 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +0100986}
987
988static int sony_init_ff(struct hid_device *hdev)
989{
990 struct hid_input *hidinput = list_entry(hdev->inputs.next,
991 struct hid_input, list);
992 struct input_dev *input_dev = hidinput->input;
993
994 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
995 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
996}
997
998#else
999static int sony_init_ff(struct hid_device *hdev)
1000{
1001 return 0;
1002}
1003#endif
1004
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001005static int sony_set_output_report(struct sony_sc *sc, int req_id, int req_size)
1006{
1007 struct list_head *head, *list;
1008 struct hid_report *report;
1009 struct hid_device *hdev = sc->hdev;
1010
1011 list = &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1012
1013 list_for_each(head, list) {
1014 report = list_entry(head, struct hid_report, list);
1015
1016 if (report->id == req_id) {
1017 if (report->size < req_size) {
1018 hid_err(hdev, "Output report 0x%02x (%i bits) is smaller than requested size (%i bits)\n",
1019 req_id, report->size, req_size);
1020 return -EINVAL;
1021 }
1022 sc->output_report = report;
1023 return 0;
1024 }
1025 }
1026
1027 hid_err(hdev, "Unable to locate output report 0x%02x\n", req_id);
1028
1029 return -EINVAL;
1030}
1031
Jiri Slabybd28ce02008-06-25 23:47:04 +02001032static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1033{
1034 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001035 unsigned long quirks = id->driver_data;
1036 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001037 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001038
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001039 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001040 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08001041 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001042 return -ENOMEM;
1043 }
1044
1045 sc->quirks = quirks;
1046 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001047 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001048
Jiri Slabybd28ce02008-06-25 23:47:04 +02001049 ret = hid_parse(hdev);
1050 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001051 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001052 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001053 }
1054
Colin Leitnerf04d5142013-05-27 23:41:05 +02001055 if (sc->quirks & VAIO_RDESC_CONSTANT)
1056 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1057 else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
1058 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1059 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1060 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1061
1062 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001063 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001064 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001065 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001066 }
1067
Antonio Ospite569b10a2010-10-19 16:13:10 +02001068 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1069 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
Antonio Ospite816651a2010-05-03 22:15:55 +02001070 ret = sixaxis_set_operational_usb(hdev);
Frank Praznik5f5750d2014-02-19 13:09:22 -05001071
1072 sc->worker_initialized = 1;
Frank Praznikcad665a2014-01-11 15:13:54 -05001073 INIT_WORK(&sc->state_worker, sixaxis_state_worker);
Antonio Ospite569b10a2010-10-19 16:13:10 +02001074 }
Antonio Ospite816651a2010-05-03 22:15:55 +02001075 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1076 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik8ab16762014-01-16 21:42:31 -05001077 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001078 /* Report 5 (31 bytes) is used to send data to the controller via USB */
1079 ret = sony_set_output_report(sc, 0x05, 248);
1080 if (ret < 0)
1081 goto err_stop;
1082
Frank Praznik5f5750d2014-02-19 13:09:22 -05001083 sc->worker_initialized = 1;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001084 INIT_WORK(&sc->state_worker, dualshock4_state_worker);
1085 } else {
1086 ret = 0;
1087 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001088
Jiri Kosina4dfdc462008-12-30 00:49:59 +01001089 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001090 goto err_stop;
1091
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001092 if (sc->quirks & SONY_LED_SUPPORT) {
1093 ret = sony_leds_init(hdev);
1094 if (ret < 0)
1095 goto err_stop;
1096 }
1097
Frank Praznik5f5750d2014-02-19 13:09:22 -05001098 if (sc->quirks & SONY_FF_SUPPORT) {
1099 ret = sony_init_ff(hdev);
1100 if (ret < 0)
1101 goto err_stop;
1102 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001103
Jiri Slabybd28ce02008-06-25 23:47:04 +02001104 return 0;
1105err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001106 if (sc->quirks & SONY_LED_SUPPORT)
1107 sony_leds_remove(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001108 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001109 return ret;
1110}
1111
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001112static void sony_remove(struct hid_device *hdev)
1113{
Colin Leitnerf04d5142013-05-27 23:41:05 +02001114 struct sony_sc *sc = hid_get_drvdata(hdev);
1115
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001116 if (sc->quirks & SONY_LED_SUPPORT)
Sven Eckelmannc5382512013-11-19 20:26:30 +01001117 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001118
Frank Praznik5f5750d2014-02-19 13:09:22 -05001119 if (sc->worker_initialized)
1120 cancel_work_sync(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001121
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001122 hid_hw_stop(hdev);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001123}
1124
Jiri Slabybd28ce02008-06-25 23:47:04 +02001125static const struct hid_device_id sony_devices[] = {
Antonio Ospite816651a2010-05-03 22:15:55 +02001126 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1127 .driver_data = SIXAXIS_CONTROLLER_USB },
Jiri Kosina35dca5b2011-04-28 15:43:13 +02001128 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
1129 .driver_data = SIXAXIS_CONTROLLER_USB },
Antonio Ospite816651a2010-05-03 22:15:55 +02001130 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1131 .driver_data = SIXAXIS_CONTROLLER_BT },
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001132 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
1133 .driver_data = VAIO_RDESC_CONSTANT },
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +09001134 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
1135 .driver_data = VAIO_RDESC_CONSTANT },
Colin Leitnerf04d5142013-05-27 23:41:05 +02001136 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
1137 * Logitech joystick from the device descriptor. */
1138 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
1139 .driver_data = BUZZ_CONTROLLER },
1140 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
1141 .driver_data = BUZZ_CONTROLLER },
Jiri Kosina078328d2013-06-13 12:03:49 +02001142 /* PS3 BD Remote Control */
1143 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
1144 .driver_data = PS3REMOTE },
1145 /* Logitech Harmony Adapter for PS3 */
1146 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
1147 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001148 /* Sony Dualshock 4 controllers for PS4 */
1149 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001150 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001151 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001152 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02001153 { }
1154};
1155MODULE_DEVICE_TABLE(hid, sony_devices);
1156
1157static struct hid_driver sony_driver = {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001158 .name = "sony",
1159 .id_table = sony_devices,
1160 .input_mapping = sony_mapping,
1161 .probe = sony_probe,
1162 .remove = sony_remove,
1163 .report_fixup = sony_report_fixup,
1164 .raw_event = sony_raw_event
Jiri Slabybd28ce02008-06-25 23:47:04 +02001165};
H Hartley Sweetenf4254582012-12-17 15:28:26 -07001166module_hid_driver(sony_driver);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001167
Jiri Slabybd28ce02008-06-25 23:47:04 +02001168MODULE_LICENSE("GPL");