blob: 04fd611d30993ac4592971152763ecd62d713297 [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>
Jiri Slabybd28ce02008-06-25 23:47:04 +020035
36#include "hid-ids.h"
37
Frank Praznik6c79c182014-01-16 21:43:03 -050038#define VAIO_RDESC_CONSTANT BIT(0)
39#define SIXAXIS_CONTROLLER_USB BIT(1)
40#define SIXAXIS_CONTROLLER_BT BIT(2)
41#define BUZZ_CONTROLLER BIT(3)
42#define PS3REMOTE BIT(4)
Frank Praznik8ab16762014-01-16 21:42:31 -050043#define DUALSHOCK4_CONTROLLER_USB BIT(5)
44#define DUALSHOCK4_CONTROLLER_BT BIT(6)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020045
Frank Praznik8ab16762014-01-16 21:42:31 -050046#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER_USB | BUZZ_CONTROLLER | DUALSHOCK4_CONTROLLER_USB)
Frank Praznikd902f472014-01-27 10:17:36 -050047#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_USB)
Frank Praznik60781cf2014-01-11 15:13:15 -050048
49#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010050
Simon Wood61ab44b2011-06-10 12:00:26 +020051static const u8 sixaxis_rdesc_fixup[] = {
52 0x95, 0x13, 0x09, 0x01, 0x81, 0x02, 0x95, 0x0C,
53 0x81, 0x01, 0x75, 0x10, 0x95, 0x04, 0x26, 0xFF,
54 0x03, 0x46, 0xFF, 0x03, 0x09, 0x01, 0x81, 0x02
55};
56
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -020057static const u8 sixaxis_rdesc_fixup2[] = {
58 0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0xa1, 0x02,
59 0x85, 0x01, 0x75, 0x08, 0x95, 0x01, 0x15, 0x00,
60 0x26, 0xff, 0x00, 0x81, 0x03, 0x75, 0x01, 0x95,
61 0x13, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00, 0x45,
62 0x01, 0x05, 0x09, 0x19, 0x01, 0x29, 0x13, 0x81,
63 0x02, 0x75, 0x01, 0x95, 0x0d, 0x06, 0x00, 0xff,
64 0x81, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x05,
65 0x01, 0x09, 0x01, 0xa1, 0x00, 0x75, 0x08, 0x95,
66 0x04, 0x35, 0x00, 0x46, 0xff, 0x00, 0x09, 0x30,
67 0x09, 0x31, 0x09, 0x32, 0x09, 0x35, 0x81, 0x02,
68 0xc0, 0x05, 0x01, 0x95, 0x13, 0x09, 0x01, 0x81,
69 0x02, 0x95, 0x0c, 0x81, 0x01, 0x75, 0x10, 0x95,
70 0x04, 0x26, 0xff, 0x03, 0x46, 0xff, 0x03, 0x09,
71 0x01, 0x81, 0x02, 0xc0, 0xa1, 0x02, 0x85, 0x02,
72 0x75, 0x08, 0x95, 0x30, 0x09, 0x01, 0xb1, 0x02,
73 0xc0, 0xa1, 0x02, 0x85, 0xee, 0x75, 0x08, 0x95,
74 0x30, 0x09, 0x01, 0xb1, 0x02, 0xc0, 0xa1, 0x02,
75 0x85, 0xef, 0x75, 0x08, 0x95, 0x30, 0x09, 0x01,
76 0xb1, 0x02, 0xc0, 0xc0,
77};
78
Frank Praznik58d70272014-01-20 12:27:01 -050079/* The default descriptor doesn't provide mapping for the accelerometers
80 * or orientation sensors. This fixed descriptor maps the accelerometers
81 * to usage values 0x40, 0x41 and 0x42 and maps the orientation sensors
82 * to usage values 0x43, 0x44 and 0x45.
83 */
Frank Prazniked19d8c2014-01-16 21:43:12 -050084static u8 dualshock4_usb_rdesc[] = {
Frank Praznik58d70272014-01-20 12:27:01 -050085 0x05, 0x01, /* Usage Page (Desktop), */
86 0x09, 0x05, /* Usage (Gamepad), */
87 0xA1, 0x01, /* Collection (Application), */
88 0x85, 0x01, /* Report ID (1), */
89 0x09, 0x30, /* Usage (X), */
90 0x09, 0x31, /* Usage (Y), */
91 0x09, 0x32, /* Usage (Z), */
92 0x09, 0x35, /* Usage (Rz), */
93 0x15, 0x00, /* Logical Minimum (0), */
94 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
95 0x75, 0x08, /* Report Size (8), */
96 0x95, 0x04, /* Report Count (4), */
97 0x81, 0x02, /* Input (Variable), */
98 0x09, 0x39, /* Usage (Hat Switch), */
99 0x15, 0x00, /* Logical Minimum (0), */
100 0x25, 0x07, /* Logical Maximum (7), */
101 0x35, 0x00, /* Physical Minimum (0), */
102 0x46, 0x3B, 0x01, /* Physical Maximum (315), */
103 0x65, 0x14, /* Unit (Degrees), */
104 0x75, 0x04, /* Report Size (4), */
105 0x95, 0x01, /* Report Count (1), */
106 0x81, 0x42, /* Input (Variable, Null State), */
107 0x65, 0x00, /* Unit, */
108 0x05, 0x09, /* Usage Page (Button), */
109 0x19, 0x01, /* Usage Minimum (01h), */
110 0x29, 0x0E, /* Usage Maximum (0Eh), */
111 0x15, 0x00, /* Logical Minimum (0), */
112 0x25, 0x01, /* Logical Maximum (1), */
113 0x75, 0x01, /* Report Size (1), */
114 0x95, 0x0E, /* Report Count (14), */
115 0x81, 0x02, /* Input (Variable), */
116 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
117 0x09, 0x20, /* Usage (20h), */
118 0x75, 0x06, /* Report Size (6), */
119 0x95, 0x01, /* Report Count (1), */
120 0x15, 0x00, /* Logical Minimum (0), */
121 0x25, 0x7F, /* Logical Maximum (127), */
122 0x81, 0x02, /* Input (Variable), */
123 0x05, 0x01, /* Usage Page (Desktop), */
124 0x09, 0x33, /* Usage (Rx), */
125 0x09, 0x34, /* Usage (Ry), */
126 0x15, 0x00, /* Logical Minimum (0), */
127 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
128 0x75, 0x08, /* Report Size (8), */
129 0x95, 0x02, /* Report Count (2), */
130 0x81, 0x02, /* Input (Variable), */
131 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
132 0x09, 0x21, /* Usage (21h), */
133 0x95, 0x03, /* Report Count (3), */
134 0x81, 0x02, /* Input (Variable), */
135 0x05, 0x01, /* Usage Page (Desktop), */
136 0x19, 0x40, /* Usage Minimum (40h), */
137 0x29, 0x42, /* Usage Maximum (42h), */
138 0x16, 0x00, 0x80, /* Logical Minimum (-32768), */
139 0x26, 0x00, 0x7F, /* Logical Maximum (32767), */
140 0x75, 0x10, /* Report Size (16), */
141 0x95, 0x03, /* Report Count (3), */
142 0x81, 0x02, /* Input (Variable), */
143 0x19, 0x43, /* Usage Minimum (43h), */
144 0x29, 0x45, /* Usage Maximum (45h), */
145 0x16, 0xFF, 0xBF, /* Logical Minimum (-16385), */
146 0x26, 0x00, 0x40, /* Logical Maximum (16384), */
147 0x95, 0x03, /* Report Count (3), */
148 0x81, 0x02, /* Input (Variable), */
149 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
150 0x09, 0x21, /* Usage (21h), */
151 0x15, 0x00, /* Logical Minimum (0), */
152 0x25, 0xFF, /* Logical Maximum (255), */
153 0x75, 0x08, /* Report Size (8), */
154 0x95, 0x27, /* Report Count (39), */
155 0x81, 0x02, /* Input (Variable), */
156 0x85, 0x05, /* Report ID (5), */
157 0x09, 0x22, /* Usage (22h), */
158 0x95, 0x1F, /* Report Count (31), */
159 0x91, 0x02, /* Output (Variable), */
160 0x85, 0x04, /* Report ID (4), */
161 0x09, 0x23, /* Usage (23h), */
162 0x95, 0x24, /* Report Count (36), */
163 0xB1, 0x02, /* Feature (Variable), */
164 0x85, 0x02, /* Report ID (2), */
165 0x09, 0x24, /* Usage (24h), */
166 0x95, 0x24, /* Report Count (36), */
167 0xB1, 0x02, /* Feature (Variable), */
168 0x85, 0x08, /* Report ID (8), */
169 0x09, 0x25, /* Usage (25h), */
170 0x95, 0x03, /* Report Count (3), */
171 0xB1, 0x02, /* Feature (Variable), */
172 0x85, 0x10, /* Report ID (16), */
173 0x09, 0x26, /* Usage (26h), */
174 0x95, 0x04, /* Report Count (4), */
175 0xB1, 0x02, /* Feature (Variable), */
176 0x85, 0x11, /* Report ID (17), */
177 0x09, 0x27, /* Usage (27h), */
178 0x95, 0x02, /* Report Count (2), */
179 0xB1, 0x02, /* Feature (Variable), */
180 0x85, 0x12, /* Report ID (18), */
181 0x06, 0x02, 0xFF, /* Usage Page (FF02h), */
182 0x09, 0x21, /* Usage (21h), */
183 0x95, 0x0F, /* Report Count (15), */
184 0xB1, 0x02, /* Feature (Variable), */
185 0x85, 0x13, /* Report ID (19), */
186 0x09, 0x22, /* Usage (22h), */
187 0x95, 0x16, /* Report Count (22), */
188 0xB1, 0x02, /* Feature (Variable), */
189 0x85, 0x14, /* Report ID (20), */
190 0x06, 0x05, 0xFF, /* Usage Page (FF05h), */
191 0x09, 0x20, /* Usage (20h), */
192 0x95, 0x10, /* Report Count (16), */
193 0xB1, 0x02, /* Feature (Variable), */
194 0x85, 0x15, /* Report ID (21), */
195 0x09, 0x21, /* Usage (21h), */
196 0x95, 0x2C, /* Report Count (44), */
197 0xB1, 0x02, /* Feature (Variable), */
198 0x06, 0x80, 0xFF, /* Usage Page (FF80h), */
199 0x85, 0x80, /* Report ID (128), */
200 0x09, 0x20, /* Usage (20h), */
201 0x95, 0x06, /* Report Count (6), */
202 0xB1, 0x02, /* Feature (Variable), */
203 0x85, 0x81, /* Report ID (129), */
204 0x09, 0x21, /* Usage (21h), */
205 0x95, 0x06, /* Report Count (6), */
206 0xB1, 0x02, /* Feature (Variable), */
207 0x85, 0x82, /* Report ID (130), */
208 0x09, 0x22, /* Usage (22h), */
209 0x95, 0x05, /* Report Count (5), */
210 0xB1, 0x02, /* Feature (Variable), */
211 0x85, 0x83, /* Report ID (131), */
212 0x09, 0x23, /* Usage (23h), */
213 0x95, 0x01, /* Report Count (1), */
214 0xB1, 0x02, /* Feature (Variable), */
215 0x85, 0x84, /* Report ID (132), */
216 0x09, 0x24, /* Usage (24h), */
217 0x95, 0x04, /* Report Count (4), */
218 0xB1, 0x02, /* Feature (Variable), */
219 0x85, 0x85, /* Report ID (133), */
220 0x09, 0x25, /* Usage (25h), */
221 0x95, 0x06, /* Report Count (6), */
222 0xB1, 0x02, /* Feature (Variable), */
223 0x85, 0x86, /* Report ID (134), */
224 0x09, 0x26, /* Usage (26h), */
225 0x95, 0x06, /* Report Count (6), */
226 0xB1, 0x02, /* Feature (Variable), */
227 0x85, 0x87, /* Report ID (135), */
228 0x09, 0x27, /* Usage (27h), */
229 0x95, 0x23, /* Report Count (35), */
230 0xB1, 0x02, /* Feature (Variable), */
231 0x85, 0x88, /* Report ID (136), */
232 0x09, 0x28, /* Usage (28h), */
233 0x95, 0x22, /* Report Count (34), */
234 0xB1, 0x02, /* Feature (Variable), */
235 0x85, 0x89, /* Report ID (137), */
236 0x09, 0x29, /* Usage (29h), */
237 0x95, 0x02, /* Report Count (2), */
238 0xB1, 0x02, /* Feature (Variable), */
239 0x85, 0x90, /* Report ID (144), */
240 0x09, 0x30, /* Usage (30h), */
241 0x95, 0x05, /* Report Count (5), */
242 0xB1, 0x02, /* Feature (Variable), */
243 0x85, 0x91, /* Report ID (145), */
244 0x09, 0x31, /* Usage (31h), */
245 0x95, 0x03, /* Report Count (3), */
246 0xB1, 0x02, /* Feature (Variable), */
247 0x85, 0x92, /* Report ID (146), */
248 0x09, 0x32, /* Usage (32h), */
249 0x95, 0x03, /* Report Count (3), */
250 0xB1, 0x02, /* Feature (Variable), */
251 0x85, 0x93, /* Report ID (147), */
252 0x09, 0x33, /* Usage (33h), */
253 0x95, 0x0C, /* Report Count (12), */
254 0xB1, 0x02, /* Feature (Variable), */
255 0x85, 0xA0, /* Report ID (160), */
256 0x09, 0x40, /* Usage (40h), */
257 0x95, 0x06, /* Report Count (6), */
258 0xB1, 0x02, /* Feature (Variable), */
259 0x85, 0xA1, /* Report ID (161), */
260 0x09, 0x41, /* Usage (41h), */
261 0x95, 0x01, /* Report Count (1), */
262 0xB1, 0x02, /* Feature (Variable), */
263 0x85, 0xA2, /* Report ID (162), */
264 0x09, 0x42, /* Usage (42h), */
265 0x95, 0x01, /* Report Count (1), */
266 0xB1, 0x02, /* Feature (Variable), */
267 0x85, 0xA3, /* Report ID (163), */
268 0x09, 0x43, /* Usage (43h), */
269 0x95, 0x30, /* Report Count (48), */
270 0xB1, 0x02, /* Feature (Variable), */
271 0x85, 0xA4, /* Report ID (164), */
272 0x09, 0x44, /* Usage (44h), */
273 0x95, 0x0D, /* Report Count (13), */
274 0xB1, 0x02, /* Feature (Variable), */
275 0x85, 0xA5, /* Report ID (165), */
276 0x09, 0x45, /* Usage (45h), */
277 0x95, 0x15, /* Report Count (21), */
278 0xB1, 0x02, /* Feature (Variable), */
279 0x85, 0xA6, /* Report ID (166), */
280 0x09, 0x46, /* Usage (46h), */
281 0x95, 0x15, /* Report Count (21), */
282 0xB1, 0x02, /* Feature (Variable), */
283 0x85, 0xF0, /* Report ID (240), */
284 0x09, 0x47, /* Usage (47h), */
285 0x95, 0x3F, /* Report Count (63), */
286 0xB1, 0x02, /* Feature (Variable), */
287 0x85, 0xF1, /* Report ID (241), */
288 0x09, 0x48, /* Usage (48h), */
289 0x95, 0x3F, /* Report Count (63), */
290 0xB1, 0x02, /* Feature (Variable), */
291 0x85, 0xF2, /* Report ID (242), */
292 0x09, 0x49, /* Usage (49h), */
293 0x95, 0x0F, /* Report Count (15), */
294 0xB1, 0x02, /* Feature (Variable), */
295 0x85, 0xA7, /* Report ID (167), */
296 0x09, 0x4A, /* Usage (4Ah), */
297 0x95, 0x01, /* Report Count (1), */
298 0xB1, 0x02, /* Feature (Variable), */
299 0x85, 0xA8, /* Report ID (168), */
300 0x09, 0x4B, /* Usage (4Bh), */
301 0x95, 0x01, /* Report Count (1), */
302 0xB1, 0x02, /* Feature (Variable), */
303 0x85, 0xA9, /* Report ID (169), */
304 0x09, 0x4C, /* Usage (4Ch), */
305 0x95, 0x08, /* Report Count (8), */
306 0xB1, 0x02, /* Feature (Variable), */
307 0x85, 0xAA, /* Report ID (170), */
308 0x09, 0x4E, /* Usage (4Eh), */
309 0x95, 0x01, /* Report Count (1), */
310 0xB1, 0x02, /* Feature (Variable), */
311 0x85, 0xAB, /* Report ID (171), */
312 0x09, 0x4F, /* Usage (4Fh), */
313 0x95, 0x39, /* Report Count (57), */
314 0xB1, 0x02, /* Feature (Variable), */
315 0x85, 0xAC, /* Report ID (172), */
316 0x09, 0x50, /* Usage (50h), */
317 0x95, 0x39, /* Report Count (57), */
318 0xB1, 0x02, /* Feature (Variable), */
319 0x85, 0xAD, /* Report ID (173), */
320 0x09, 0x51, /* Usage (51h), */
321 0x95, 0x0B, /* Report Count (11), */
322 0xB1, 0x02, /* Feature (Variable), */
323 0x85, 0xAE, /* Report ID (174), */
324 0x09, 0x52, /* Usage (52h), */
325 0x95, 0x01, /* Report Count (1), */
326 0xB1, 0x02, /* Feature (Variable), */
327 0x85, 0xAF, /* Report ID (175), */
328 0x09, 0x53, /* Usage (53h), */
329 0x95, 0x02, /* Report Count (2), */
330 0xB1, 0x02, /* Feature (Variable), */
331 0x85, 0xB0, /* Report ID (176), */
332 0x09, 0x54, /* Usage (54h), */
333 0x95, 0x3F, /* Report Count (63), */
334 0xB1, 0x02, /* Feature (Variable), */
335 0xC0 /* End Collection */
Frank Prazniked19d8c2014-01-16 21:43:12 -0500336};
337
Jiri Kosina078328d2013-06-13 12:03:49 +0200338static __u8 ps3remote_rdesc[] = {
339 0x05, 0x01, /* GUsagePage Generic Desktop */
340 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
341 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
342
343 /* Use collection 1 for joypad buttons */
344 0xA1, 0x02, /* MCollection Logical (interrelated data) */
345
346 /* Ignore the 1st byte, maybe it is used for a controller
347 * number but it's not needed for correct operation */
348 0x75, 0x08, /* GReportSize 0x08 [8] */
349 0x95, 0x01, /* GReportCount 0x01 [1] */
350 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
351
352 /* Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
353 * buttons multiple keypresses are allowed */
354 0x05, 0x09, /* GUsagePage Button */
355 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
356 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
357 0x14, /* GLogicalMinimum [0] */
358 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
359 0x75, 0x01, /* GReportSize 0x01 [1] */
360 0x95, 0x18, /* GReportCount 0x18 [24] */
361 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
362
363 0xC0, /* MEndCollection */
364
365 /* Use collection 2 for remote control buttons */
366 0xA1, 0x02, /* MCollection Logical (interrelated data) */
367
368 /* 5th byte is used for remote control buttons */
369 0x05, 0x09, /* GUsagePage Button */
370 0x18, /* LUsageMinimum [No button pressed] */
371 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
372 0x14, /* GLogicalMinimum [0] */
373 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
374 0x75, 0x08, /* GReportSize 0x08 [8] */
375 0x95, 0x01, /* GReportCount 0x01 [1] */
376 0x80, /* MInput */
377
378 /* Ignore bytes from 6th to 11th, 6th to 10th are always constant at
379 * 0xff and 11th is for press indication */
380 0x75, 0x08, /* GReportSize 0x08 [8] */
381 0x95, 0x06, /* GReportCount 0x06 [6] */
382 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
383
384 /* 12th byte is for battery strength */
385 0x05, 0x06, /* GUsagePage Generic Device Controls */
386 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
387 0x14, /* GLogicalMinimum [0] */
388 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
389 0x75, 0x08, /* GReportSize 0x08 [8] */
390 0x95, 0x01, /* GReportCount 0x01 [1] */
391 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
392
393 0xC0, /* MEndCollection */
394
395 0xC0 /* MEndCollection [Game Pad] */
396};
397
398static const unsigned int ps3remote_keymap_joypad_buttons[] = {
399 [0x01] = KEY_SELECT,
400 [0x02] = BTN_THUMBL, /* L3 */
401 [0x03] = BTN_THUMBR, /* R3 */
402 [0x04] = BTN_START,
403 [0x05] = KEY_UP,
404 [0x06] = KEY_RIGHT,
405 [0x07] = KEY_DOWN,
406 [0x08] = KEY_LEFT,
407 [0x09] = BTN_TL2, /* L2 */
408 [0x0a] = BTN_TR2, /* R2 */
409 [0x0b] = BTN_TL, /* L1 */
410 [0x0c] = BTN_TR, /* R1 */
411 [0x0d] = KEY_OPTION, /* options/triangle */
412 [0x0e] = KEY_BACK, /* back/circle */
413 [0x0f] = BTN_0, /* cross */
414 [0x10] = KEY_SCREEN, /* view/square */
415 [0x11] = KEY_HOMEPAGE, /* PS button */
416 [0x14] = KEY_ENTER,
417};
418static const unsigned int ps3remote_keymap_remote_buttons[] = {
419 [0x00] = KEY_1,
420 [0x01] = KEY_2,
421 [0x02] = KEY_3,
422 [0x03] = KEY_4,
423 [0x04] = KEY_5,
424 [0x05] = KEY_6,
425 [0x06] = KEY_7,
426 [0x07] = KEY_8,
427 [0x08] = KEY_9,
428 [0x09] = KEY_0,
429 [0x0e] = KEY_ESC, /* return */
430 [0x0f] = KEY_CLEAR,
431 [0x16] = KEY_EJECTCD,
432 [0x1a] = KEY_MENU, /* top menu */
433 [0x28] = KEY_TIME,
434 [0x30] = KEY_PREVIOUS,
435 [0x31] = KEY_NEXT,
436 [0x32] = KEY_PLAY,
437 [0x33] = KEY_REWIND, /* scan back */
438 [0x34] = KEY_FORWARD, /* scan forward */
439 [0x38] = KEY_STOP,
440 [0x39] = KEY_PAUSE,
441 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
442 [0x60] = KEY_FRAMEBACK, /* slow/step back */
443 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
444 [0x63] = KEY_SUBTITLE,
445 [0x64] = KEY_AUDIO,
446 [0x65] = KEY_ANGLE,
447 [0x70] = KEY_INFO, /* display */
448 [0x80] = KEY_BLUE,
449 [0x81] = KEY_RED,
450 [0x82] = KEY_GREEN,
451 [0x83] = KEY_YELLOW,
452};
453
Colin Leitnerf04d5142013-05-27 23:41:05 +0200454static const unsigned int buzz_keymap[] = {
455 /* The controller has 4 remote buzzers, each with one LED and 5
456 * buttons.
457 *
458 * We use the mapping chosen by the controller, which is:
459 *
460 * Key Offset
461 * -------------------
462 * Buzz 1
463 * Blue 5
464 * Orange 4
465 * Green 3
466 * Yellow 2
467 *
468 * So, for example, the orange button on the third buzzer is mapped to
469 * BTN_TRIGGER_HAPPY14
470 */
471 [ 1] = BTN_TRIGGER_HAPPY1,
472 [ 2] = BTN_TRIGGER_HAPPY2,
473 [ 3] = BTN_TRIGGER_HAPPY3,
474 [ 4] = BTN_TRIGGER_HAPPY4,
475 [ 5] = BTN_TRIGGER_HAPPY5,
476 [ 6] = BTN_TRIGGER_HAPPY6,
477 [ 7] = BTN_TRIGGER_HAPPY7,
478 [ 8] = BTN_TRIGGER_HAPPY8,
479 [ 9] = BTN_TRIGGER_HAPPY9,
480 [10] = BTN_TRIGGER_HAPPY10,
481 [11] = BTN_TRIGGER_HAPPY11,
482 [12] = BTN_TRIGGER_HAPPY12,
483 [13] = BTN_TRIGGER_HAPPY13,
484 [14] = BTN_TRIGGER_HAPPY14,
485 [15] = BTN_TRIGGER_HAPPY15,
486 [16] = BTN_TRIGGER_HAPPY16,
487 [17] = BTN_TRIGGER_HAPPY17,
488 [18] = BTN_TRIGGER_HAPPY18,
489 [19] = BTN_TRIGGER_HAPPY19,
490 [20] = BTN_TRIGGER_HAPPY20,
491};
492
Frank Praznikd902f472014-01-27 10:17:36 -0500493static enum power_supply_property sony_battery_props[] = {
494 POWER_SUPPLY_PROP_PRESENT,
495 POWER_SUPPLY_PROP_CAPACITY,
496 POWER_SUPPLY_PROP_SCOPE,
497 POWER_SUPPLY_PROP_STATUS,
498};
499
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200500struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500501 spinlock_t lock;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100502 struct hid_device *hdev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500503 struct led_classdev *leds[MAX_LEDS];
Frank Praznikc4e1ddf2014-01-17 14:46:26 -0500504 struct hid_report *output_report;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200505 unsigned long quirks;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100506 struct work_struct state_worker;
Frank Praznikd902f472014-01-27 10:17:36 -0500507 struct power_supply battery;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200508
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100509#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100510 __u8 left;
511 __u8 right;
512#endif
513
Frank Praznikd902f472014-01-27 10:17:36 -0500514 __u8 cable_state;
515 __u8 battery_charging;
516 __u8 battery_capacity;
Frank Praznik60781cf2014-01-11 15:13:15 -0500517 __u8 led_state[MAX_LEDS];
518 __u8 led_count;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200519};
520
Jiri Kosina078328d2013-06-13 12:03:49 +0200521static __u8 *ps3remote_fixup(struct hid_device *hdev, __u8 *rdesc,
522 unsigned int *rsize)
523{
524 *rsize = sizeof(ps3remote_rdesc);
525 return ps3remote_rdesc;
526}
527
528static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
529 struct hid_field *field, struct hid_usage *usage,
530 unsigned long **bit, int *max)
531{
532 unsigned int key = usage->hid & HID_USAGE;
533
534 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
535 return -1;
536
537 switch (usage->collection_index) {
538 case 1:
539 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
540 return -1;
541
542 key = ps3remote_keymap_joypad_buttons[key];
543 if (!key)
544 return -1;
545 break;
546 case 2:
547 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
548 return -1;
549
550 key = ps3remote_keymap_remote_buttons[key];
551 if (!key)
552 return -1;
553 break;
554 default:
555 return -1;
556 }
557
558 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
559 return 1;
560}
561
562
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200563/* Sony Vaio VGX has wrongly mouse pointer declared as constant */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400564static __u8 *sony_report_fixup(struct hid_device *hdev, __u8 *rdesc,
565 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200566{
567 struct sony_sc *sc = hid_get_drvdata(hdev);
568
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900569 /*
570 * Some Sony RF receivers wrongly declare the mouse pointer as a
571 * a constant non-data variable.
572 */
573 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
574 /* usage page: generic desktop controls */
575 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
576 /* usage: mouse */
577 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
578 /* input (usage page for x,y axes): constant, variable, relative */
579 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +0900580 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900581 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200582 rdesc[55] = 0x06;
583 }
Simon Wood61ab44b2011-06-10 12:00:26 +0200584
Frank Prazniked19d8c2014-01-16 21:43:12 -0500585 /*
586 * The default Dualshock 4 USB descriptor doesn't assign
587 * the gyroscope values to corresponding axes so we need a
588 * modified one.
589 */
590 if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && *rsize == 467) {
591 hid_info(hdev, "Using modified Dualshock 4 report descriptor with gyroscope axes\n");
592 rdesc = dualshock4_usb_rdesc;
593 *rsize = sizeof(dualshock4_usb_rdesc);
594 }
595
Simon Wood61ab44b2011-06-10 12:00:26 +0200596 /* The HID descriptor exposed over BT has a trailing zero byte */
597 if ((((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize == 148) ||
598 ((sc->quirks & SIXAXIS_CONTROLLER_BT) && *rsize == 149)) &&
599 rdesc[83] == 0x75) {
600 hid_info(hdev, "Fixing up Sony Sixaxis report descriptor\n");
601 memcpy((void *)&rdesc[83], (void *)&sixaxis_rdesc_fixup,
602 sizeof(sixaxis_rdesc_fixup));
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -0200603 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB &&
604 *rsize > sizeof(sixaxis_rdesc_fixup2)) {
605 hid_info(hdev, "Sony Sixaxis clone detected. Using original report descriptor (size: %d clone; %d new)\n",
606 *rsize, (int)sizeof(sixaxis_rdesc_fixup2));
607 *rsize = sizeof(sixaxis_rdesc_fixup2);
608 memcpy(rdesc, &sixaxis_rdesc_fixup2, *rsize);
Simon Wood61ab44b2011-06-10 12:00:26 +0200609 }
Jiri Kosina078328d2013-06-13 12:03:49 +0200610
611 if (sc->quirks & PS3REMOTE)
612 return ps3remote_fixup(hdev, rdesc, rsize);
613
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400614 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200615}
616
Frank Praznikd902f472014-01-27 10:17:36 -0500617static void sixaxis_parse_report(struct sony_sc *sc, __u8 *rd, int size)
618{
619 static const __u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
620 unsigned long flags;
621 __u8 cable_state, battery_capacity, battery_charging;
622
623 /* The sixaxis is charging if the battery value is 0xee
624 * and it is fully charged if the value is 0xef.
625 * It does not report the actual level while charging so it
626 * is set to 100% while charging is in progress.
627 */
628 if (rd[30] >= 0xee) {
629 battery_capacity = 100;
630 battery_charging = rd[30] & 0x01;
631 } else {
632 battery_capacity = sixaxis_battery_capacity[rd[30]];
633 battery_charging = 0;
634 }
635 cable_state = (rd[31] >> 4) & 0x01;
636
637 spin_lock_irqsave(&sc->lock, flags);
638 sc->cable_state = cable_state;
639 sc->battery_capacity = battery_capacity;
640 sc->battery_charging = battery_charging;
641 spin_unlock_irqrestore(&sc->lock, flags);
642}
643
644static void dualshock4_parse_report(struct sony_sc *sc, __u8 *rd, int size)
645{
646 unsigned long flags;
647 __u8 cable_state, battery_capacity, battery_charging;
648
649 /* The lower 4 bits of byte 30 contain the battery level
650 * and the 5th bit contains the USB cable state.
651 */
652 cable_state = (rd[30] >> 4) & 0x01;
653 battery_capacity = rd[30] & 0x0F;
654
655 /* On USB the Dualshock 4 battery level goes from 0 to 11.
656 * A battery level of 11 means fully charged.
657 */
658 if (cable_state && battery_capacity == 11)
659 battery_charging = 0;
660 else
661 battery_charging = 1;
662
663 if (battery_capacity > 10)
664 battery_capacity--;
665 battery_capacity *= 10;
666
667 spin_lock_irqsave(&sc->lock, flags);
668 sc->cable_state = cable_state;
669 sc->battery_capacity = battery_capacity;
670 sc->battery_charging = battery_charging;
671 spin_unlock_irqrestore(&sc->lock, flags);
672}
673
Simon Woodc9e4d872011-06-10 12:00:27 +0200674static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
675 __u8 *rd, int size)
676{
677 struct sony_sc *sc = hid_get_drvdata(hdev);
678
679 /* Sixaxis HID report has acclerometers/gyro with MSByte first, this
680 * has to be BYTE_SWAPPED before passing up to joystick interface
681 */
682 if ((sc->quirks & (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)) &&
683 rd[0] == 0x01 && size == 49) {
684 swap(rd[41], rd[42]);
685 swap(rd[43], rd[44]);
686 swap(rd[45], rd[46]);
687 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -0500688
689 sixaxis_parse_report(sc, rd, size);
690 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
691 size == 64) {
692 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +0200693 }
694
695 return 0;
696}
697
Colin Leitnerf04d5142013-05-27 23:41:05 +0200698static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
699 struct hid_field *field, struct hid_usage *usage,
700 unsigned long **bit, int *max)
701{
702 struct sony_sc *sc = hid_get_drvdata(hdev);
703
704 if (sc->quirks & BUZZ_CONTROLLER) {
705 unsigned int key = usage->hid & HID_USAGE;
706
707 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
708 return -1;
709
710 switch (usage->collection_index) {
711 case 1:
712 if (key >= ARRAY_SIZE(buzz_keymap))
713 return -1;
714
715 key = buzz_keymap[key];
716 if (!key)
717 return -1;
718 break;
719 default:
720 return -1;
721 }
722
723 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
724 return 1;
725 }
726
Jiri Kosina078328d2013-06-13 12:03:49 +0200727 if (sc->quirks & PS3REMOTE)
728 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
729
Benjamin Tissoires6f498012013-07-24 16:53:07 +0200730 /* Let hid-core decide for the others */
731 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200732}
733
Antonio Ospite5710fab2011-02-20 18:26:45 +0100734/*
735 * The Sony Sixaxis does not handle HID Output Reports on the Interrupt EP
736 * like it should according to usbhid/hid-core.c::usbhid_output_raw_report()
737 * so we need to override that forcing HID Output Reports on the Control EP.
738 *
739 * There is also another issue about HID Output Reports via USB, the Sixaxis
740 * does not want the report_id as part of the data packet, so we have to
741 * discard buf[0] when sending the actual control message, even for numbered
742 * reports, humpf!
743 */
Antonio Ospite569b10a2010-10-19 16:13:10 +0200744static int sixaxis_usb_output_raw_report(struct hid_device *hid, __u8 *buf,
745 size_t count, unsigned char report_type)
746{
747 struct usb_interface *intf = to_usb_interface(hid->dev.parent);
748 struct usb_device *dev = interface_to_usbdev(intf);
749 struct usb_host_interface *interface = intf->cur_altsetting;
750 int report_id = buf[0];
751 int ret;
752
Antonio Ospite5710fab2011-02-20 18:26:45 +0100753 if (report_type == HID_OUTPUT_REPORT) {
754 /* Don't send the Report ID */
755 buf++;
756 count--;
757 }
758
Antonio Ospite569b10a2010-10-19 16:13:10 +0200759 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
760 HID_REQ_SET_REPORT,
761 USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
762 ((report_type + 1) << 8) | report_id,
763 interface->desc.bInterfaceNumber, buf, count,
764 USB_CTRL_SET_TIMEOUT);
765
Antonio Ospite5710fab2011-02-20 18:26:45 +0100766 /* Count also the Report ID, in case of an Output report. */
767 if (ret > 0 && report_type == HID_OUTPUT_REPORT)
768 ret++;
769
Antonio Ospite569b10a2010-10-19 16:13:10 +0200770 return ret;
771}
772
Jiri Slabybd28ce02008-06-25 23:47:04 +0200773/*
774 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
775 * to "operational". Without this, the ps3 controller will not report any
776 * events.
777 */
Antonio Ospite816651a2010-05-03 22:15:55 +0200778static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +0200779{
Jiri Slabybd28ce02008-06-25 23:47:04 +0200780 int ret;
781 char *buf = kmalloc(18, GFP_KERNEL);
782
783 if (!buf)
784 return -ENOMEM;
785
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +0200786 ret = hdev->hid_get_raw_report(hdev, 0xf2, buf, 17, HID_FEATURE_REPORT);
787
Jiri Slabybd28ce02008-06-25 23:47:04 +0200788 if (ret < 0)
Joe Perches4291ee32010-12-09 19:29:03 -0800789 hid_err(hdev, "can't set operational mode\n");
Jiri Slabybd28ce02008-06-25 23:47:04 +0200790
791 kfree(buf);
792
793 return ret;
794}
795
Antonio Ospite816651a2010-05-03 22:15:55 +0200796static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +0000797{
Antonio Ospitefddb33f2010-05-03 17:19:03 +0200798 unsigned char buf[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
Bastien Noceraf9ce7c22010-01-20 12:01:53 +0000799 return hdev->hid_output_raw_report(hdev, buf, sizeof(buf), HID_FEATURE_REPORT);
800}
801
Frank Praznik60781cf2014-01-11 15:13:15 -0500802static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
Colin Leitnerf04d5142013-05-27 23:41:05 +0200803{
804 struct list_head *report_list =
805 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
806 struct hid_report *report = list_entry(report_list->next,
807 struct hid_report, list);
808 __s32 *value = report->field[0]->value;
809
810 value[0] = 0x00;
Frank Praznik60781cf2014-01-11 15:13:15 -0500811 value[1] = leds[0] ? 0xff : 0x00;
812 value[2] = leds[1] ? 0xff : 0x00;
813 value[3] = leds[2] ? 0xff : 0x00;
814 value[4] = leds[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200815 value[5] = 0x00;
816 value[6] = 0x00;
817 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
818}
819
Frank Praznik60781cf2014-01-11 15:13:15 -0500820static void sony_set_leds(struct hid_device *hdev, const __u8 *leds, int count)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100821{
822 struct sony_sc *drv_data = hid_get_drvdata(hdev);
Frank Praznik60781cf2014-01-11 15:13:15 -0500823 int n;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100824
Frank Praznik60781cf2014-01-11 15:13:15 -0500825 BUG_ON(count > MAX_LEDS);
826
827 if (drv_data->quirks & BUZZ_CONTROLLER && count == 4) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100828 buzz_set_leds(hdev, leds);
Frank Praznik60781cf2014-01-11 15:13:15 -0500829 } else if ((drv_data->quirks & SIXAXIS_CONTROLLER_USB) ||
Frank Praznik8ab16762014-01-16 21:42:31 -0500830 (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB)) {
Frank Praznik60781cf2014-01-11 15:13:15 -0500831 for (n = 0; n < count; n++)
832 drv_data->led_state[n] = leds[n];
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100833 schedule_work(&drv_data->state_worker);
834 }
835}
836
Sven Eckelmannc5382512013-11-19 20:26:30 +0100837static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +0200838 enum led_brightness value)
839{
840 struct device *dev = led->dev->parent;
841 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
842 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200843
844 int n;
845
846 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +0100847 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +0200848 hid_err(hdev, "No device data\n");
849 return;
850 }
Colin Leitnerf04d5142013-05-27 23:41:05 +0200851
Frank Praznik60781cf2014-01-11 15:13:15 -0500852 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann2251b852013-11-19 20:26:31 +0100853 if (led == drv_data->leds[n]) {
Frank Praznik60781cf2014-01-11 15:13:15 -0500854 if (value != drv_data->led_state[n]) {
855 drv_data->led_state[n] = value;
856 sony_set_leds(hdev, drv_data->led_state, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +0200857 }
858 break;
859 }
860 }
861}
862
Sven Eckelmannc5382512013-11-19 20:26:30 +0100863static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +0200864{
865 struct device *dev = led->dev->parent;
866 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
867 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200868
869 int n;
870 int on = 0;
871
872 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +0100873 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +0200874 hid_err(hdev, "No device data\n");
875 return LED_OFF;
876 }
Colin Leitnerf04d5142013-05-27 23:41:05 +0200877
Frank Praznik60781cf2014-01-11 15:13:15 -0500878 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann2251b852013-11-19 20:26:31 +0100879 if (led == drv_data->leds[n]) {
Frank Praznik60781cf2014-01-11 15:13:15 -0500880 on = !!(drv_data->led_state[n]);
Colin Leitnerf04d5142013-05-27 23:41:05 +0200881 break;
882 }
883 }
884
885 return on ? LED_FULL : LED_OFF;
886}
Colin Leitnerf04d5142013-05-27 23:41:05 +0200887
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100888static void sony_leds_remove(struct hid_device *hdev)
889{
890 struct sony_sc *drv_data;
891 struct led_classdev *led;
892 int n;
893
894 drv_data = hid_get_drvdata(hdev);
895 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
896
Frank Praznik60781cf2014-01-11 15:13:15 -0500897 for (n = 0; n < drv_data->led_count; n++) {
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100898 led = drv_data->leds[n];
899 drv_data->leds[n] = NULL;
900 if (!led)
901 continue;
902 led_classdev_unregister(led);
903 kfree(led);
904 }
Frank Praznik60781cf2014-01-11 15:13:15 -0500905
906 drv_data->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100907}
908
Sven Eckelmannc5382512013-11-19 20:26:30 +0100909static int sony_leds_init(struct hid_device *hdev)
Colin Leitnerf04d5142013-05-27 23:41:05 +0200910{
911 struct sony_sc *drv_data;
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200912 int n, ret = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -0500913 int max_brightness;
Frank Praznik61ebca92014-01-20 12:27:02 -0500914 int use_colors;
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200915 struct led_classdev *led;
916 size_t name_sz;
917 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100918 size_t name_len;
919 const char *name_fmt;
Frank Praznik61ebca92014-01-20 12:27:02 -0500920 static const char * const color_str[] = { "red", "green", "blue" };
Frank Praznik60781cf2014-01-11 15:13:15 -0500921 static const __u8 initial_values[MAX_LEDS] = { 0x00, 0x00, 0x00, 0x00 };
Colin Leitnerf04d5142013-05-27 23:41:05 +0200922
923 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100924 BUG_ON(!(drv_data->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +0200925
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100926 if (drv_data->quirks & BUZZ_CONTROLLER) {
Frank Praznik61ebca92014-01-20 12:27:02 -0500927 drv_data->led_count = 4;
928 max_brightness = 1;
929 use_colors = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100930 name_len = strlen("::buzz#");
931 name_fmt = "%s::buzz%d";
932 /* Validate expected report characteristics. */
933 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
934 return -ENODEV;
Frank Praznik61ebca92014-01-20 12:27:02 -0500935 } else if (drv_data->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznik60781cf2014-01-11 15:13:15 -0500936 drv_data->led_count = 3;
937 max_brightness = 255;
Frank Praznik61ebca92014-01-20 12:27:02 -0500938 use_colors = 1;
939 name_len = 0;
940 name_fmt = "%s:%s";
Frank Praznik60781cf2014-01-11 15:13:15 -0500941 } else {
942 drv_data->led_count = 4;
943 max_brightness = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -0500944 use_colors = 0;
945 name_len = strlen("::sony#");
946 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -0500947 }
948
Colin Leitnerf04d5142013-05-27 23:41:05 +0200949 /* Clear LEDs as we have no way of reading their initial state. This is
950 * only relevant if the driver is loaded after somebody actively set the
951 * LEDs to on */
Frank Praznik60781cf2014-01-11 15:13:15 -0500952 sony_set_leds(hdev, initial_values, drv_data->led_count);
Colin Leitnerf04d5142013-05-27 23:41:05 +0200953
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100954 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200955
Frank Praznik60781cf2014-01-11 15:13:15 -0500956 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -0500957
958 if (use_colors)
959 name_sz = strlen(dev_name(&hdev->dev)) + strlen(color_str[n]) + 2;
960
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200961 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
962 if (!led) {
963 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +0100964 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200965 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200966 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200967
968 name = (void *)(&led[1]);
Frank Praznik61ebca92014-01-20 12:27:02 -0500969 if (use_colors)
970 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), color_str[n]);
971 else
972 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200973 led->name = name;
974 led->brightness = 0;
Frank Praznik60781cf2014-01-11 15:13:15 -0500975 led->max_brightness = max_brightness;
Sven Eckelmannc5382512013-11-19 20:26:30 +0100976 led->brightness_get = sony_led_get_brightness;
977 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200978
Julia Lawall8cd5fcd2013-12-29 23:47:27 +0100979 ret = led_classdev_register(&hdev->dev, led);
980 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +0200981 hid_err(hdev, "Failed to register LED %d\n", n);
982 kfree(led);
983 goto error_leds;
984 }
985
Sven Eckelmann2251b852013-11-19 20:26:31 +0100986 drv_data->leds[n] = led;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200987 }
Colin Leitnerf04d5142013-05-27 23:41:05 +0200988
989 return ret;
990
Colin Leitnerf04d5142013-05-27 23:41:05 +0200991error_leds:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100992 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +0200993
Colin Leitnerf04d5142013-05-27 23:41:05 +0200994 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200995}
996
Frank Praznikcad665a2014-01-11 15:13:54 -0500997static void sixaxis_state_worker(struct work_struct *work)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +0100998{
Sven Eckelmann92b5c412013-11-19 20:26:28 +0100999 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001000 unsigned char buf[] = {
1001 0x01,
1002 0x00, 0xff, 0x00, 0xff, 0x00,
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001003 0x00, 0x00, 0x00, 0x00, 0x00,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001004 0xff, 0x27, 0x10, 0x00, 0x32,
1005 0xff, 0x27, 0x10, 0x00, 0x32,
1006 0xff, 0x27, 0x10, 0x00, 0x32,
1007 0xff, 0x27, 0x10, 0x00, 0x32,
1008 0x00, 0x00, 0x00, 0x00, 0x00
1009 };
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001010
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001011#ifdef CONFIG_SONY_FF
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001012 buf[3] = sc->right ? 1 : 0;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001013 buf[5] = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001014#endif
1015
Frank Praznik60781cf2014-01-11 15:13:15 -05001016 buf[10] |= sc->led_state[0] << 1;
1017 buf[10] |= sc->led_state[1] << 2;
1018 buf[10] |= sc->led_state[2] << 3;
1019 buf[10] |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001020
1021 sc->hdev->hid_output_raw_report(sc->hdev, buf, sizeof(buf),
1022 HID_OUTPUT_REPORT);
1023}
1024
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001025static void dualshock4_state_worker(struct work_struct *work)
1026{
1027 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Frank Praznik0da8ea62014-01-16 21:42:51 -05001028 struct hid_device *hdev = sc->hdev;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001029 struct hid_report *report = sc->output_report;
1030 __s32 *value = report->field[0]->value;
Frank Praznik0da8ea62014-01-16 21:42:51 -05001031
Frank Praznik0da8ea62014-01-16 21:42:51 -05001032 value[0] = 0x03;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001033
1034#ifdef CONFIG_SONY_FF
Frank Praznik0da8ea62014-01-16 21:42:51 -05001035 value[3] = sc->right;
1036 value[4] = sc->left;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001037#endif
1038
Frank Praznik0da8ea62014-01-16 21:42:51 -05001039 value[5] = sc->led_state[0];
1040 value[6] = sc->led_state[1];
1041 value[7] = sc->led_state[2];
Frank Praznik60781cf2014-01-11 15:13:15 -05001042
Frank Praznik0da8ea62014-01-16 21:42:51 -05001043 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001044}
1045
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001046#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001047static int sony_play_effect(struct input_dev *dev, void *data,
1048 struct ff_effect *effect)
1049{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001050 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001051 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001052
1053 if (effect->type != FF_RUMBLE)
1054 return 0;
1055
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001056 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001057 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001058
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001059 schedule_work(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001060 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001061}
1062
1063static int sony_init_ff(struct hid_device *hdev)
1064{
1065 struct hid_input *hidinput = list_entry(hdev->inputs.next,
1066 struct hid_input, list);
1067 struct input_dev *input_dev = hidinput->input;
1068
1069 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
1070 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
1071}
1072
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001073static void sony_destroy_ff(struct hid_device *hdev)
1074{
1075 struct sony_sc *sc = hid_get_drvdata(hdev);
1076
Sven Eckelmann92b5c412013-11-19 20:26:28 +01001077 cancel_work_sync(&sc->state_worker);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001078}
1079
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001080#else
1081static int sony_init_ff(struct hid_device *hdev)
1082{
1083 return 0;
1084}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001085
1086static void sony_destroy_ff(struct hid_device *hdev)
1087{
1088}
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001089#endif
1090
Frank Praznikd902f472014-01-27 10:17:36 -05001091static int sony_battery_get_property(struct power_supply *psy,
1092 enum power_supply_property psp,
1093 union power_supply_propval *val)
1094{
1095 struct sony_sc *sc = container_of(psy, struct sony_sc, battery);
1096 unsigned long flags;
1097 int ret = 0;
1098 u8 battery_charging, battery_capacity, cable_state;
1099
1100 spin_lock_irqsave(&sc->lock, flags);
1101 battery_charging = sc->battery_charging;
1102 battery_capacity = sc->battery_capacity;
1103 cable_state = sc->cable_state;
1104 spin_unlock_irqrestore(&sc->lock, flags);
1105
1106 switch (psp) {
1107 case POWER_SUPPLY_PROP_PRESENT:
1108 val->intval = 1;
1109 break;
1110 case POWER_SUPPLY_PROP_SCOPE:
1111 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
1112 break;
1113 case POWER_SUPPLY_PROP_CAPACITY:
1114 val->intval = battery_capacity;
1115 break;
1116 case POWER_SUPPLY_PROP_STATUS:
1117 if (battery_charging)
1118 val->intval = POWER_SUPPLY_STATUS_CHARGING;
1119 else
1120 if (battery_capacity == 100 && cable_state)
1121 val->intval = POWER_SUPPLY_STATUS_FULL;
1122 else
1123 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
1124 break;
1125 default:
1126 ret = -EINVAL;
1127 break;
1128 }
1129 return ret;
1130}
1131
1132static int sony_battery_probe(struct sony_sc *sc)
1133{
1134 static atomic_t power_id_seq = ATOMIC_INIT(0);
1135 unsigned long power_id;
1136 struct hid_device *hdev = sc->hdev;
1137 int ret;
1138
1139 power_id = (unsigned long)atomic_inc_return(&power_id_seq);
1140
1141 sc->battery.properties = sony_battery_props;
1142 sc->battery.num_properties = ARRAY_SIZE(sony_battery_props);
1143 sc->battery.get_property = sony_battery_get_property;
1144 sc->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1145 sc->battery.use_for_apm = 0;
1146 sc->battery.name = kasprintf(GFP_KERNEL, "sony_controller_battery_%lu",
1147 power_id);
1148 if (!sc->battery.name)
1149 return -ENOMEM;
1150
1151 ret = power_supply_register(&hdev->dev, &sc->battery);
1152 if (ret) {
1153 hid_err(hdev, "Unable to register battery device\n");
1154 goto err_free;
1155 }
1156
1157 power_supply_powers(&sc->battery, &hdev->dev);
1158 return 0;
1159
1160err_free:
1161 kfree(sc->battery.name);
1162 sc->battery.name = NULL;
1163 return ret;
1164}
1165
1166static void sony_battery_remove(struct sony_sc *sc)
1167{
1168 if (!sc->battery.name)
1169 return;
1170
1171 power_supply_unregister(&sc->battery);
1172 kfree(sc->battery.name);
1173 sc->battery.name = NULL;
1174}
1175
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001176static int sony_set_output_report(struct sony_sc *sc, int req_id, int req_size)
1177{
1178 struct list_head *head, *list;
1179 struct hid_report *report;
1180 struct hid_device *hdev = sc->hdev;
1181
1182 list = &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1183
1184 list_for_each(head, list) {
1185 report = list_entry(head, struct hid_report, list);
1186
1187 if (report->id == req_id) {
1188 if (report->size < req_size) {
1189 hid_err(hdev, "Output report 0x%02x (%i bits) is smaller than requested size (%i bits)\n",
1190 req_id, report->size, req_size);
1191 return -EINVAL;
1192 }
1193 sc->output_report = report;
1194 return 0;
1195 }
1196 }
1197
1198 hid_err(hdev, "Unable to locate output report 0x%02x\n", req_id);
1199
1200 return -EINVAL;
1201}
1202
Jiri Slabybd28ce02008-06-25 23:47:04 +02001203static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
1204{
1205 int ret;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001206 unsigned long quirks = id->driver_data;
1207 struct sony_sc *sc;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001208 unsigned int connect_mask = HID_CONNECT_DEFAULT;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001209
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001210 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001211 if (sc == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -08001212 hid_err(hdev, "can't alloc sony descriptor\n");
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001213 return -ENOMEM;
1214 }
1215
1216 sc->quirks = quirks;
1217 hid_set_drvdata(hdev, sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001218 sc->hdev = hdev;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001219
Jiri Slabybd28ce02008-06-25 23:47:04 +02001220 ret = hid_parse(hdev);
1221 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001222 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001223 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001224 }
1225
Colin Leitnerf04d5142013-05-27 23:41:05 +02001226 if (sc->quirks & VAIO_RDESC_CONSTANT)
1227 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1228 else if (sc->quirks & SIXAXIS_CONTROLLER_USB)
1229 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1230 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1231 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
1232
1233 ret = hid_hw_start(hdev, connect_mask);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001234 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -08001235 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +02001236 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001237 }
1238
Antonio Ospite569b10a2010-10-19 16:13:10 +02001239 if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
1240 hdev->hid_output_raw_report = sixaxis_usb_output_raw_report;
Antonio Ospite816651a2010-05-03 22:15:55 +02001241 ret = sixaxis_set_operational_usb(hdev);
Frank Praznikcad665a2014-01-11 15:13:54 -05001242 INIT_WORK(&sc->state_worker, sixaxis_state_worker);
Antonio Ospite569b10a2010-10-19 16:13:10 +02001243 }
Antonio Ospite816651a2010-05-03 22:15:55 +02001244 else if (sc->quirks & SIXAXIS_CONTROLLER_BT)
1245 ret = sixaxis_set_operational_bt(hdev);
Frank Praznik8ab16762014-01-16 21:42:31 -05001246 else if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05001247 /* Report 5 (31 bytes) is used to send data to the controller via USB */
1248 ret = sony_set_output_report(sc, 0x05, 248);
1249 if (ret < 0)
1250 goto err_stop;
1251
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001252 INIT_WORK(&sc->state_worker, dualshock4_state_worker);
1253 } else {
1254 ret = 0;
1255 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001256
Jiri Kosina4dfdc462008-12-30 00:49:59 +01001257 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001258 goto err_stop;
1259
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001260 if (sc->quirks & SONY_LED_SUPPORT) {
1261 ret = sony_leds_init(hdev);
1262 if (ret < 0)
1263 goto err_stop;
1264 }
1265
Frank Praznikd902f472014-01-27 10:17:36 -05001266 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1267 ret = sony_battery_probe(sc);
1268 if (ret < 0)
1269 goto err_stop;
1270
1271 /* Open the device to receive reports with battery info */
1272 ret = hid_hw_open(hdev);
1273 if (ret < 0) {
1274 hid_err(hdev, "hw open failed\n");
1275 goto err_stop;
1276 }
1277 }
1278
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001279 ret = sony_init_ff(hdev);
1280 if (ret < 0)
Frank Praznikd902f472014-01-27 10:17:36 -05001281 goto err_close;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001282
Jiri Slabybd28ce02008-06-25 23:47:04 +02001283 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05001284err_close:
1285 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001286err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001287 if (sc->quirks & SONY_LED_SUPPORT)
1288 sony_leds_remove(hdev);
Frank Praznikd902f472014-01-27 10:17:36 -05001289 if (sc->quirks & SONY_BATTERY_SUPPORT)
1290 sony_battery_remove(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001291 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001292 return ret;
1293}
1294
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001295static void sony_remove(struct hid_device *hdev)
1296{
Colin Leitnerf04d5142013-05-27 23:41:05 +02001297 struct sony_sc *sc = hid_get_drvdata(hdev);
1298
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001299 if (sc->quirks & SONY_LED_SUPPORT)
Sven Eckelmannc5382512013-11-19 20:26:30 +01001300 sony_leds_remove(hdev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001301
Frank Praznikd902f472014-01-27 10:17:36 -05001302 if (sc->quirks & SONY_BATTERY_SUPPORT) {
1303 hid_hw_close(hdev);
1304 sony_battery_remove(sc);
1305 }
1306
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001307 sony_destroy_ff(hdev);
1308
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001309 hid_hw_stop(hdev);
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001310}
1311
Jiri Slabybd28ce02008-06-25 23:47:04 +02001312static const struct hid_device_id sony_devices[] = {
Antonio Ospite816651a2010-05-03 22:15:55 +02001313 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1314 .driver_data = SIXAXIS_CONTROLLER_USB },
Jiri Kosina35dca5b2011-04-28 15:43:13 +02001315 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
1316 .driver_data = SIXAXIS_CONTROLLER_USB },
Antonio Ospite816651a2010-05-03 22:15:55 +02001317 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
1318 .driver_data = SIXAXIS_CONTROLLER_BT },
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02001319 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
1320 .driver_data = VAIO_RDESC_CONSTANT },
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +09001321 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
1322 .driver_data = VAIO_RDESC_CONSTANT },
Colin Leitnerf04d5142013-05-27 23:41:05 +02001323 /* Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
1324 * Logitech joystick from the device descriptor. */
1325 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
1326 .driver_data = BUZZ_CONTROLLER },
1327 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
1328 .driver_data = BUZZ_CONTROLLER },
Jiri Kosina078328d2013-06-13 12:03:49 +02001329 /* PS3 BD Remote Control */
1330 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
1331 .driver_data = PS3REMOTE },
1332 /* Logitech Harmony Adapter for PS3 */
1333 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
1334 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001335 /* Sony Dualshock 4 controllers for PS4 */
1336 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001337 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001338 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05001339 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02001340 { }
1341};
1342MODULE_DEVICE_TABLE(hid, sony_devices);
1343
1344static struct hid_driver sony_driver = {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001345 .name = "sony",
1346 .id_table = sony_devices,
1347 .input_mapping = sony_mapping,
1348 .probe = sony_probe,
1349 .remove = sony_remove,
1350 .report_fixup = sony_report_fixup,
1351 .raw_event = sony_raw_event
Jiri Slabybd28ce02008-06-25 23:47:04 +02001352};
H Hartley Sweetenf4254582012-12-17 15:28:26 -07001353module_hid_driver(sony_driver);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001354
Jiri Slabybd28ce02008-06-25 23:47:04 +02001355MODULE_LICENSE("GPL");