Commit | Line | Data |
---|---|---|
bd28ce00 | 1 | /* |
077147a3 | 2 | * HID driver for Sony / PS2 / PS3 / PS4 BD devices. |
bd28ce00 JS |
3 | * |
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 | |
bd28ce00 | 7 | * Copyright (c) 2008 Jiri Slaby |
078328da JK |
8 | * Copyright (c) 2012 David Dillow <dave@thedillows.org> |
9 | * Copyright (c) 2006-2013 Jiri Kosina | |
f04d5140 | 10 | * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com> |
c4425c8f | 11 | * Copyright (c) 2014-2016 Frank Praznik <frank.praznik@gmail.com> |
bd28ce00 JS |
12 | */ |
13 | ||
14 | /* | |
15 | * This program is free software; you can redistribute it and/or modify it | |
16 | * under the terms of the GNU General Public License as published by the Free | |
17 | * Software Foundation; either version 2 of the License, or (at your option) | |
18 | * any later version. | |
19 | */ | |
20 | ||
ad142b9e FP |
21 | /* |
22 | * NOTE: in order for the Sony PS3 BD Remote Control to be found by | |
078328da JK |
23 | * a Bluetooth host, the key combination Start+Enter has to be kept pressed |
24 | * for about 7 seconds with the Bluetooth Host Controller in discovering mode. | |
25 | * | |
26 | * There will be no PIN request from the device. | |
27 | */ | |
28 | ||
bd28ce00 JS |
29 | #include <linux/device.h> |
30 | #include <linux/hid.h> | |
31 | #include <linux/module.h> | |
5a0e3ad6 | 32 | #include <linux/slab.h> |
40e32ee6 | 33 | #include <linux/leds.h> |
d902f472 FP |
34 | #include <linux/power_supply.h> |
35 | #include <linux/spinlock.h> | |
d2d782fc | 36 | #include <linux/list.h> |
8025087a | 37 | #include <linux/idr.h> |
e5606230 | 38 | #include <linux/input/mt.h> |
49b9ca6c RC |
39 | #include <linux/crc32.h> |
40 | #include <asm/unaligned.h> | |
bd28ce00 JS |
41 | |
42 | #include "hid-ids.h" | |
43 | ||
6c79c18c FP |
44 | #define VAIO_RDESC_CONSTANT BIT(0) |
45 | #define SIXAXIS_CONTROLLER_USB BIT(1) | |
46 | #define SIXAXIS_CONTROLLER_BT BIT(2) | |
47 | #define BUZZ_CONTROLLER BIT(3) | |
48 | #define PS3REMOTE BIT(4) | |
8ab1676b FP |
49 | #define DUALSHOCK4_CONTROLLER_USB BIT(5) |
50 | #define DUALSHOCK4_CONTROLLER_BT BIT(6) | |
35f436c3 RC |
51 | #define DUALSHOCK4_DONGLE BIT(7) |
52 | #define MOTION_CONTROLLER_USB BIT(8) | |
53 | #define MOTION_CONTROLLER_BT BIT(9) | |
54 | #define NAVIGATION_CONTROLLER_USB BIT(10) | |
55 | #define NAVIGATION_CONTROLLER_BT BIT(11) | |
56 | #define SINO_LITE_CONTROLLER BIT(12) | |
57 | #define FUTUREMAX_DANCE_MAT BIT(13) | |
cc6e0bbb | 58 | |
fee4e2d5 | 59 | #define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT) |
b3bca326 | 60 | #define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT) |
4545ee0a SW |
61 | #define NAVIGATION_CONTROLLER (NAVIGATION_CONTROLLER_USB |\ |
62 | NAVIGATION_CONTROLLER_BT) | |
68330d83 | 63 | #define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\ |
35f436c3 RC |
64 | DUALSHOCK4_CONTROLLER_BT | \ |
65 | DUALSHOCK4_DONGLE) | |
fee4e2d5 | 66 | #define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\ |
4545ee0a SW |
67 | DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER |\ |
68 | NAVIGATION_CONTROLLER) | |
12e9a6d7 | 69 | #define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\ |
4545ee0a | 70 | MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER) |
c5e0c1c4 FP |
71 | #define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\ |
72 | MOTION_CONTROLLER) | |
0f398230 FP |
73 | #define SONY_BT_DEVICE (SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_BT |\ |
74 | MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER_BT) | |
60781cf4 FP |
75 | |
76 | #define MAX_LEDS 4 | |
0a286ef2 | 77 | |
e57a67da | 78 | |
c5e0c1c4 | 79 | /* PS/3 Motion controller */ |
1adf904e | 80 | static u8 motion_rdesc[] = { |
c5e0c1c4 FP |
81 | 0x05, 0x01, /* Usage Page (Desktop), */ |
82 | 0x09, 0x04, /* Usage (Joystick), */ | |
83 | 0xA1, 0x01, /* Collection (Application), */ | |
84 | 0xA1, 0x02, /* Collection (Logical), */ | |
85 | 0x85, 0x01, /* Report ID (1), */ | |
c5e0c1c4 | 86 | 0x75, 0x01, /* Report Size (1), */ |
8b2513c3 | 87 | 0x95, 0x15, /* Report Count (21), */ |
c5e0c1c4 FP |
88 | 0x15, 0x00, /* Logical Minimum (0), */ |
89 | 0x25, 0x01, /* Logical Maximum (1), */ | |
90 | 0x35, 0x00, /* Physical Minimum (0), */ | |
91 | 0x45, 0x01, /* Physical Maximum (1), */ | |
92 | 0x05, 0x09, /* Usage Page (Button), */ | |
93 | 0x19, 0x01, /* Usage Minimum (01h), */ | |
8b2513c3 SW |
94 | 0x29, 0x15, /* Usage Maximum (15h), */ |
95 | 0x81, 0x02, /* Input (Variable), * Buttons */ | |
96 | 0x95, 0x0B, /* Report Count (11), */ | |
c5e0c1c4 | 97 | 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ |
8b2513c3 | 98 | 0x81, 0x03, /* Input (Constant, Variable), * Padding */ |
c5e0c1c4 FP |
99 | 0x15, 0x00, /* Logical Minimum (0), */ |
100 | 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ | |
101 | 0x05, 0x01, /* Usage Page (Desktop), */ | |
c5e0c1c4 FP |
102 | 0xA1, 0x00, /* Collection (Physical), */ |
103 | 0x75, 0x08, /* Report Size (8), */ | |
8b2513c3 | 104 | 0x95, 0x01, /* Report Count (1), */ |
c5e0c1c4 FP |
105 | 0x35, 0x00, /* Physical Minimum (0), */ |
106 | 0x46, 0xFF, 0x00, /* Physical Maximum (255), */ | |
107 | 0x09, 0x30, /* Usage (X), */ | |
8b2513c3 | 108 | 0x81, 0x02, /* Input (Variable), * Trigger */ |
c5e0c1c4 | 109 | 0xC0, /* End Collection, */ |
8b2513c3 SW |
110 | 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ |
111 | 0x75, 0x08, /* Report Size (8), */ | |
112 | 0x95, 0x07, /* Report Count (7), * skip 7 bytes */ | |
c5e0c1c4 | 113 | 0x81, 0x02, /* Input (Variable), */ |
8b2513c3 | 114 | 0x05, 0x01, /* Usage Page (Desktop), */ |
c5e0c1c4 | 115 | 0x75, 0x10, /* Report Size (16), */ |
8b2513c3 SW |
116 | 0x46, 0xFF, 0xFF, /* Physical Maximum (65535), */ |
117 | 0x27, 0xFF, 0xFF, 0x00, 0x00, /* Logical Maximum (65535), */ | |
118 | 0x95, 0x03, /* Report Count (3), * 3x Accels */ | |
119 | 0x09, 0x33, /* Usage (rX), */ | |
120 | 0x09, 0x34, /* Usage (rY), */ | |
121 | 0x09, 0x35, /* Usage (rZ), */ | |
122 | 0x81, 0x02, /* Input (Variable), */ | |
123 | 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ | |
124 | 0x95, 0x03, /* Report Count (3), * Skip Accels 2nd frame */ | |
125 | 0x81, 0x02, /* Input (Variable), */ | |
126 | 0x05, 0x01, /* Usage Page (Desktop), */ | |
c5e0c1c4 | 127 | 0x09, 0x01, /* Usage (Pointer), */ |
8b2513c3 SW |
128 | 0x95, 0x03, /* Report Count (3), * 3x Gyros */ |
129 | 0x81, 0x02, /* Input (Variable), */ | |
130 | 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */ | |
131 | 0x95, 0x03, /* Report Count (3), * Skip Gyros 2nd frame */ | |
c5e0c1c4 | 132 | 0x81, 0x02, /* Input (Variable), */ |
8b2513c3 SW |
133 | 0x75, 0x0C, /* Report Size (12), */ |
134 | 0x46, 0xFF, 0x0F, /* Physical Maximum (4095), */ | |
135 | 0x26, 0xFF, 0x0F, /* Logical Maximum (4095), */ | |
136 | 0x95, 0x04, /* Report Count (4), * Skip Temp and Magnetometers */ | |
137 | 0x81, 0x02, /* Input (Variable), */ | |
138 | 0x75, 0x08, /* Report Size (8), */ | |
139 | 0x46, 0xFF, 0x00, /* Physical Maximum (255), */ | |
140 | 0x26, 0xFF, 0x00, /* Logical Maximum (255), */ | |
141 | 0x95, 0x06, /* Report Count (6), * Skip Timestamp and Extension Bytes */ | |
142 | 0x81, 0x02, /* Input (Variable), */ | |
143 | 0x75, 0x08, /* Report Size (8), */ | |
144 | 0x95, 0x30, /* Report Count (48), */ | |
145 | 0x09, 0x01, /* Usage (Pointer), */ | |
146 | 0x91, 0x02, /* Output (Variable), */ | |
147 | 0x75, 0x08, /* Report Size (8), */ | |
148 | 0x95, 0x30, /* Report Count (48), */ | |
149 | 0x09, 0x01, /* Usage (Pointer), */ | |
150 | 0xB1, 0x02, /* Feature (Variable), */ | |
c5e0c1c4 FP |
151 | 0xC0, /* End Collection, */ |
152 | 0xA1, 0x02, /* Collection (Logical), */ | |
153 | 0x85, 0x02, /* Report ID (2), */ | |
154 | 0x75, 0x08, /* Report Size (8), */ | |
155 | 0x95, 0x30, /* Report Count (48), */ | |
156 | 0x09, 0x01, /* Usage (Pointer), */ | |
157 | 0xB1, 0x02, /* Feature (Variable), */ | |
158 | 0xC0, /* End Collection, */ | |
159 | 0xA1, 0x02, /* Collection (Logical), */ | |
160 | 0x85, 0xEE, /* Report ID (238), */ | |
161 | 0x75, 0x08, /* Report Size (8), */ | |
162 | 0x95, 0x30, /* Report Count (48), */ | |
163 | 0x09, 0x01, /* Usage (Pointer), */ | |
164 | 0xB1, 0x02, /* Feature (Variable), */ | |
165 | 0xC0, /* End Collection, */ | |
166 | 0xA1, 0x02, /* Collection (Logical), */ | |
167 | 0x85, 0xEF, /* Report ID (239), */ | |
168 | 0x75, 0x08, /* Report Size (8), */ | |
169 | 0x95, 0x30, /* Report Count (48), */ | |
170 | 0x09, 0x01, /* Usage (Pointer), */ | |
171 | 0xB1, 0x02, /* Feature (Variable), */ | |
172 | 0xC0, /* End Collection, */ | |
173 | 0xC0 /* End Collection */ | |
174 | }; | |
175 | ||
1adf904e | 176 | static u8 ps3remote_rdesc[] = { |
078328da JK |
177 | 0x05, 0x01, /* GUsagePage Generic Desktop */ |
178 | 0x09, 0x05, /* LUsage 0x05 [Game Pad] */ | |
179 | 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */ | |
180 | ||
181 | /* Use collection 1 for joypad buttons */ | |
182 | 0xA1, 0x02, /* MCollection Logical (interrelated data) */ | |
183 | ||
ef916ef5 AO |
184 | /* |
185 | * Ignore the 1st byte, maybe it is used for a controller | |
186 | * number but it's not needed for correct operation | |
187 | */ | |
078328da JK |
188 | 0x75, 0x08, /* GReportSize 0x08 [8] */ |
189 | 0x95, 0x01, /* GReportCount 0x01 [1] */ | |
190 | 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */ | |
191 | ||
ef916ef5 AO |
192 | /* |
193 | * Bytes from 2nd to 4th are a bitmap for joypad buttons, for these | |
194 | * buttons multiple keypresses are allowed | |
195 | */ | |
078328da JK |
196 | 0x05, 0x09, /* GUsagePage Button */ |
197 | 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */ | |
198 | 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */ | |
199 | 0x14, /* GLogicalMinimum [0] */ | |
200 | 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */ | |
201 | 0x75, 0x01, /* GReportSize 0x01 [1] */ | |
202 | 0x95, 0x18, /* GReportCount 0x18 [24] */ | |
203 | 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */ | |
204 | ||
205 | 0xC0, /* MEndCollection */ | |
206 | ||
207 | /* Use collection 2 for remote control buttons */ | |
208 | 0xA1, 0x02, /* MCollection Logical (interrelated data) */ | |
209 | ||
210 | /* 5th byte is used for remote control buttons */ | |
211 | 0x05, 0x09, /* GUsagePage Button */ | |
212 | 0x18, /* LUsageMinimum [No button pressed] */ | |
213 | 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */ | |
214 | 0x14, /* GLogicalMinimum [0] */ | |
215 | 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */ | |
216 | 0x75, 0x08, /* GReportSize 0x08 [8] */ | |
217 | 0x95, 0x01, /* GReportCount 0x01 [1] */ | |
218 | 0x80, /* MInput */ | |
219 | ||
ef916ef5 AO |
220 | /* |
221 | * Ignore bytes from 6th to 11th, 6th to 10th are always constant at | |
222 | * 0xff and 11th is for press indication | |
223 | */ | |
078328da JK |
224 | 0x75, 0x08, /* GReportSize 0x08 [8] */ |
225 | 0x95, 0x06, /* GReportCount 0x06 [6] */ | |
226 | 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */ | |
227 | ||
228 | /* 12th byte is for battery strength */ | |
229 | 0x05, 0x06, /* GUsagePage Generic Device Controls */ | |
230 | 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */ | |
231 | 0x14, /* GLogicalMinimum [0] */ | |
232 | 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */ | |
233 | 0x75, 0x08, /* GReportSize 0x08 [8] */ | |
234 | 0x95, 0x01, /* GReportCount 0x01 [1] */ | |
235 | 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */ | |
236 | ||
237 | 0xC0, /* MEndCollection */ | |
238 | ||
239 | 0xC0 /* MEndCollection [Game Pad] */ | |
240 | }; | |
241 | ||
242 | static const unsigned int ps3remote_keymap_joypad_buttons[] = { | |
243 | [0x01] = KEY_SELECT, | |
244 | [0x02] = BTN_THUMBL, /* L3 */ | |
245 | [0x03] = BTN_THUMBR, /* R3 */ | |
246 | [0x04] = BTN_START, | |
247 | [0x05] = KEY_UP, | |
248 | [0x06] = KEY_RIGHT, | |
249 | [0x07] = KEY_DOWN, | |
250 | [0x08] = KEY_LEFT, | |
251 | [0x09] = BTN_TL2, /* L2 */ | |
252 | [0x0a] = BTN_TR2, /* R2 */ | |
253 | [0x0b] = BTN_TL, /* L1 */ | |
254 | [0x0c] = BTN_TR, /* R1 */ | |
255 | [0x0d] = KEY_OPTION, /* options/triangle */ | |
256 | [0x0e] = KEY_BACK, /* back/circle */ | |
257 | [0x0f] = BTN_0, /* cross */ | |
258 | [0x10] = KEY_SCREEN, /* view/square */ | |
259 | [0x11] = KEY_HOMEPAGE, /* PS button */ | |
260 | [0x14] = KEY_ENTER, | |
261 | }; | |
262 | static const unsigned int ps3remote_keymap_remote_buttons[] = { | |
263 | [0x00] = KEY_1, | |
264 | [0x01] = KEY_2, | |
265 | [0x02] = KEY_3, | |
266 | [0x03] = KEY_4, | |
267 | [0x04] = KEY_5, | |
268 | [0x05] = KEY_6, | |
269 | [0x06] = KEY_7, | |
270 | [0x07] = KEY_8, | |
271 | [0x08] = KEY_9, | |
272 | [0x09] = KEY_0, | |
273 | [0x0e] = KEY_ESC, /* return */ | |
274 | [0x0f] = KEY_CLEAR, | |
275 | [0x16] = KEY_EJECTCD, | |
276 | [0x1a] = KEY_MENU, /* top menu */ | |
277 | [0x28] = KEY_TIME, | |
278 | [0x30] = KEY_PREVIOUS, | |
279 | [0x31] = KEY_NEXT, | |
280 | [0x32] = KEY_PLAY, | |
281 | [0x33] = KEY_REWIND, /* scan back */ | |
282 | [0x34] = KEY_FORWARD, /* scan forward */ | |
283 | [0x38] = KEY_STOP, | |
284 | [0x39] = KEY_PAUSE, | |
285 | [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */ | |
286 | [0x60] = KEY_FRAMEBACK, /* slow/step back */ | |
287 | [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */ | |
288 | [0x63] = KEY_SUBTITLE, | |
289 | [0x64] = KEY_AUDIO, | |
290 | [0x65] = KEY_ANGLE, | |
291 | [0x70] = KEY_INFO, /* display */ | |
292 | [0x80] = KEY_BLUE, | |
293 | [0x81] = KEY_RED, | |
294 | [0x82] = KEY_GREEN, | |
295 | [0x83] = KEY_YELLOW, | |
296 | }; | |
297 | ||
f04d5140 | 298 | static const unsigned int buzz_keymap[] = { |
ad142b9e FP |
299 | /* |
300 | * The controller has 4 remote buzzers, each with one LED and 5 | |
f04d5140 | 301 | * buttons. |
09593e38 | 302 | * |
f04d5140 CL |
303 | * We use the mapping chosen by the controller, which is: |
304 | * | |
305 | * Key Offset | |
306 | * ------------------- | |
307 | * Buzz 1 | |
308 | * Blue 5 | |
309 | * Orange 4 | |
310 | * Green 3 | |
311 | * Yellow 2 | |
312 | * | |
313 | * So, for example, the orange button on the third buzzer is mapped to | |
314 | * BTN_TRIGGER_HAPPY14 | |
315 | */ | |
09593e38 AO |
316 | [1] = BTN_TRIGGER_HAPPY1, |
317 | [2] = BTN_TRIGGER_HAPPY2, | |
318 | [3] = BTN_TRIGGER_HAPPY3, | |
319 | [4] = BTN_TRIGGER_HAPPY4, | |
320 | [5] = BTN_TRIGGER_HAPPY5, | |
321 | [6] = BTN_TRIGGER_HAPPY6, | |
322 | [7] = BTN_TRIGGER_HAPPY7, | |
323 | [8] = BTN_TRIGGER_HAPPY8, | |
324 | [9] = BTN_TRIGGER_HAPPY9, | |
f04d5140 CL |
325 | [10] = BTN_TRIGGER_HAPPY10, |
326 | [11] = BTN_TRIGGER_HAPPY11, | |
327 | [12] = BTN_TRIGGER_HAPPY12, | |
328 | [13] = BTN_TRIGGER_HAPPY13, | |
329 | [14] = BTN_TRIGGER_HAPPY14, | |
330 | [15] = BTN_TRIGGER_HAPPY15, | |
331 | [16] = BTN_TRIGGER_HAPPY16, | |
332 | [17] = BTN_TRIGGER_HAPPY17, | |
333 | [18] = BTN_TRIGGER_HAPPY18, | |
334 | [19] = BTN_TRIGGER_HAPPY19, | |
335 | [20] = BTN_TRIGGER_HAPPY20, | |
336 | }; | |
337 | ||
b8f0970d RC |
338 | /* The Navigation controller is a partial DS3 and uses the same HID report |
339 | * and hence the same keymap indices, however not not all axes/buttons | |
340 | * are physically present. We use the same axis and button mapping as | |
341 | * the DS3, which uses the Linux gamepad spec. | |
342 | */ | |
343 | static const unsigned int navigation_absmap[] = { | |
344 | [0x30] = ABS_X, | |
345 | [0x31] = ABS_Y, | |
346 | [0x33] = ABS_Z, /* L2 */ | |
347 | }; | |
348 | ||
349 | /* Buttons not physically available on the device, but still available | |
350 | * in the reports are explicitly set to 0 for documentation purposes. | |
351 | */ | |
352 | static const unsigned int navigation_keymap[] = { | |
353 | [0x01] = 0, /* Select */ | |
354 | [0x02] = BTN_THUMBL, /* L3 */ | |
355 | [0x03] = 0, /* R3 */ | |
356 | [0x04] = 0, /* Start */ | |
357 | [0x05] = BTN_DPAD_UP, /* Up */ | |
358 | [0x06] = BTN_DPAD_RIGHT, /* Right */ | |
359 | [0x07] = BTN_DPAD_DOWN, /* Down */ | |
360 | [0x08] = BTN_DPAD_LEFT, /* Left */ | |
361 | [0x09] = BTN_TL2, /* L2 */ | |
362 | [0x0a] = 0, /* R2 */ | |
363 | [0x0b] = BTN_TL, /* L1 */ | |
364 | [0x0c] = 0, /* R1 */ | |
365 | [0x0d] = BTN_NORTH, /* Triangle */ | |
366 | [0x0e] = BTN_EAST, /* Circle */ | |
367 | [0x0f] = BTN_SOUTH, /* Cross */ | |
368 | [0x10] = BTN_WEST, /* Square */ | |
369 | [0x11] = BTN_MODE, /* PS */ | |
370 | }; | |
371 | ||
e19a267b RC |
372 | static const unsigned int sixaxis_absmap[] = { |
373 | [0x30] = ABS_X, | |
374 | [0x31] = ABS_Y, | |
375 | [0x32] = ABS_RX, /* right stick X */ | |
376 | [0x35] = ABS_RY, /* right stick Y */ | |
377 | }; | |
378 | ||
379 | static const unsigned int sixaxis_keymap[] = { | |
380 | [0x01] = BTN_SELECT, /* Select */ | |
381 | [0x02] = BTN_THUMBL, /* L3 */ | |
382 | [0x03] = BTN_THUMBR, /* R3 */ | |
383 | [0x04] = BTN_START, /* Start */ | |
384 | [0x05] = BTN_DPAD_UP, /* Up */ | |
385 | [0x06] = BTN_DPAD_RIGHT, /* Right */ | |
386 | [0x07] = BTN_DPAD_DOWN, /* Down */ | |
387 | [0x08] = BTN_DPAD_LEFT, /* Left */ | |
388 | [0x09] = BTN_TL2, /* L2 */ | |
389 | [0x0a] = BTN_TR2, /* R2 */ | |
390 | [0x0b] = BTN_TL, /* L1 */ | |
391 | [0x0c] = BTN_TR, /* R1 */ | |
392 | [0x0d] = BTN_NORTH, /* Triangle */ | |
393 | [0x0e] = BTN_EAST, /* Circle */ | |
394 | [0x0f] = BTN_SOUTH, /* Cross */ | |
395 | [0x10] = BTN_WEST, /* Square */ | |
396 | [0x11] = BTN_MODE, /* PS */ | |
397 | }; | |
398 | ||
9131f8cc RC |
399 | static const unsigned int ds4_absmap[] = { |
400 | [0x30] = ABS_X, | |
401 | [0x31] = ABS_Y, | |
402 | [0x32] = ABS_RX, /* right stick X */ | |
403 | [0x33] = ABS_Z, /* L2 */ | |
404 | [0x34] = ABS_RZ, /* R2 */ | |
405 | [0x35] = ABS_RY, /* right stick Y */ | |
406 | }; | |
407 | ||
408 | static const unsigned int ds4_keymap[] = { | |
409 | [0x1] = BTN_WEST, /* Square */ | |
410 | [0x2] = BTN_SOUTH, /* Cross */ | |
411 | [0x3] = BTN_EAST, /* Circle */ | |
412 | [0x4] = BTN_NORTH, /* Triangle */ | |
413 | [0x5] = BTN_TL, /* L1 */ | |
414 | [0x6] = BTN_TR, /* R1 */ | |
415 | [0x7] = BTN_TL2, /* L2 */ | |
416 | [0x8] = BTN_TR2, /* R2 */ | |
417 | [0x9] = BTN_SELECT, /* Share */ | |
418 | [0xa] = BTN_START, /* Options */ | |
419 | [0xb] = BTN_THUMBL, /* L3 */ | |
420 | [0xc] = BTN_THUMBR, /* R3 */ | |
421 | [0xd] = BTN_MODE, /* PS */ | |
422 | }; | |
423 | ||
d03ae2e1 RC |
424 | static const struct {int x; int y; } ds4_hat_mapping[] = { |
425 | {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, | |
426 | {0, 0} | |
427 | }; | |
9131f8cc | 428 | |
d902f472 FP |
429 | static enum power_supply_property sony_battery_props[] = { |
430 | POWER_SUPPLY_PROP_PRESENT, | |
431 | POWER_SUPPLY_PROP_CAPACITY, | |
432 | POWER_SUPPLY_PROP_SCOPE, | |
433 | POWER_SUPPLY_PROP_STATUS, | |
434 | }; | |
435 | ||
55d3b664 | 436 | struct sixaxis_led { |
1adf904e PM |
437 | u8 time_enabled; /* the total time the led is active (0xff means forever) */ |
438 | u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */ | |
439 | u8 enabled; | |
440 | u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */ | |
441 | u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */ | |
55d3b664 FP |
442 | } __packed; |
443 | ||
444 | struct sixaxis_rumble { | |
1adf904e PM |
445 | u8 padding; |
446 | u8 right_duration; /* Right motor duration (0xff means forever) */ | |
447 | u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */ | |
448 | u8 left_duration; /* Left motor duration (0xff means forever) */ | |
449 | u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */ | |
55d3b664 FP |
450 | } __packed; |
451 | ||
452 | struct sixaxis_output_report { | |
1adf904e | 453 | u8 report_id; |
55d3b664 | 454 | struct sixaxis_rumble rumble; |
1adf904e PM |
455 | u8 padding[4]; |
456 | u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */ | |
55d3b664 FP |
457 | struct sixaxis_led led[4]; /* LEDx at (4 - x) */ |
458 | struct sixaxis_led _reserved; /* LED5, not actually soldered */ | |
459 | } __packed; | |
460 | ||
461 | union sixaxis_output_report_01 { | |
462 | struct sixaxis_output_report data; | |
1adf904e | 463 | u8 buf[36]; |
55d3b664 FP |
464 | }; |
465 | ||
c5e0c1c4 FP |
466 | struct motion_output_report_02 { |
467 | u8 type, zero; | |
468 | u8 r, g, b; | |
469 | u8 zero2; | |
470 | u8 rumble; | |
471 | }; | |
472 | ||
2c159de0 | 473 | #define DS4_FEATURE_REPORT_0x02_SIZE 37 |
55a07d62 | 474 | #define DS4_FEATURE_REPORT_0x05_SIZE 41 |
2c159de0 | 475 | #define DS4_FEATURE_REPORT_0x81_SIZE 7 |
169f15ab | 476 | #define DS4_FEATURE_REPORT_0xA3_SIZE 49 |
49b9ca6c | 477 | #define DS4_INPUT_REPORT_0x11_SIZE 78 |
2c159de0 RC |
478 | #define DS4_OUTPUT_REPORT_0x05_SIZE 32 |
479 | #define DS4_OUTPUT_REPORT_0x11_SIZE 78 | |
29b691a8 | 480 | #define SIXAXIS_REPORT_0xF2_SIZE 17 |
a85d67b5 | 481 | #define SIXAXIS_REPORT_0xF5_SIZE 8 |
41d2d425 | 482 | #define MOTION_REPORT_0x02_SIZE 49 |
9b2b5c9a | 483 | |
cdc1c021 RC |
484 | /* Offsets relative to USB input report (0x1). Bluetooth (0x11) requires an |
485 | * additional +2. | |
486 | */ | |
d03ae2e1 | 487 | #define DS4_INPUT_REPORT_AXIS_OFFSET 1 |
ac797b95 | 488 | #define DS4_INPUT_REPORT_BUTTON_OFFSET 5 |
80786eb9 | 489 | #define DS4_INPUT_REPORT_TIMESTAMP_OFFSET 10 |
227c011b | 490 | #define DS4_INPUT_REPORT_GYRO_X_OFFSET 13 |
cdc1c021 RC |
491 | #define DS4_INPUT_REPORT_BATTERY_OFFSET 30 |
492 | #define DS4_INPUT_REPORT_TOUCHPAD_OFFSET 33 | |
493 | ||
510c8b7c | 494 | #define SENSOR_SUFFIX " Motion Sensors" |
ac797b95 RC |
495 | #define DS4_TOUCHPAD_SUFFIX " Touchpad" |
496 | ||
77b499e7 RC |
497 | /* Default to 4ms poll interval, which is same as USB (not adjustable). */ |
498 | #define DS4_BT_DEFAULT_POLL_INTERVAL_MS 4 | |
499 | #define DS4_BT_MAX_POLL_INTERVAL_MS 62 | |
55a07d62 RC |
500 | #define DS4_GYRO_RES_PER_DEG_S 1024 |
501 | #define DS4_ACC_RES_PER_G 8192 | |
502 | ||
510c8b7c RC |
503 | #define SIXAXIS_INPUT_REPORT_ACC_X_OFFSET 41 |
504 | #define SIXAXIS_ACC_RES_PER_G 113 | |
505 | ||
8b402c92 | 506 | static DEFINE_SPINLOCK(sony_dev_list_lock); |
d2d782fc | 507 | static LIST_HEAD(sony_device_list); |
8025087a | 508 | static DEFINE_IDA(sony_device_id_allocator); |
d2d782fc | 509 | |
55a07d62 RC |
510 | /* Used for calibration of DS4 accelerometer and gyro. */ |
511 | struct ds4_calibration_data { | |
512 | int abs_code; | |
513 | short bias; | |
514 | /* Calibration requires scaling against a sensitivity value, which is a | |
515 | * float. Store sensitivity as a fraction to limit floating point | |
516 | * calculations until final calibration. | |
517 | */ | |
518 | int sens_numer; | |
519 | int sens_denom; | |
520 | }; | |
521 | ||
f2f47c38 RC |
522 | enum ds4_dongle_state { |
523 | DONGLE_DISCONNECTED, | |
524 | DONGLE_CALIBRATING, | |
525 | DONGLE_CONNECTED, | |
526 | DONGLE_DISABLED | |
527 | }; | |
528 | ||
b5322736 | 529 | enum sony_worker { |
f2f47c38 RC |
530 | SONY_WORKER_STATE, |
531 | SONY_WORKER_HOTPLUG | |
b5322736 RC |
532 | }; |
533 | ||
cc6e0bbb | 534 | struct sony_sc { |
d902f472 | 535 | spinlock_t lock; |
d2d782fc | 536 | struct list_head list_node; |
0a286ef2 | 537 | struct hid_device *hdev; |
ac797b95 | 538 | struct input_dev *touchpad; |
227c011b | 539 | struct input_dev *sensor_dev; |
60781cf4 | 540 | struct led_classdev *leds[MAX_LEDS]; |
cc6e0bbb | 541 | unsigned long quirks; |
f2f47c38 | 542 | struct work_struct hotplug_worker; |
0a286ef2 | 543 | struct work_struct state_worker; |
09593e38 | 544 | void (*send_output_report)(struct sony_sc *); |
297d716f KK |
545 | struct power_supply *battery; |
546 | struct power_supply_desc battery_desc; | |
8025087a | 547 | int device_id; |
169f15ab RC |
548 | unsigned fw_version; |
549 | unsigned hw_version; | |
1adf904e | 550 | u8 *output_report_dmabuf; |
f04d5140 | 551 | |
9f323b68 | 552 | #ifdef CONFIG_SONY_FF |
1adf904e PM |
553 | u8 left; |
554 | u8 right; | |
9f323b68 SE |
555 | #endif |
556 | ||
1adf904e | 557 | u8 mac_address[6]; |
f2f47c38 | 558 | u8 hotplug_worker_initialized; |
b5322736 | 559 | u8 state_worker_initialized; |
2a242932 | 560 | u8 defer_initialization; |
1adf904e PM |
561 | u8 cable_state; |
562 | u8 battery_charging; | |
563 | u8 battery_capacity; | |
564 | u8 led_state[MAX_LEDS]; | |
1adf904e PM |
565 | u8 led_delay_on[MAX_LEDS]; |
566 | u8 led_delay_off[MAX_LEDS]; | |
567 | u8 led_count; | |
80786eb9 RC |
568 | |
569 | bool timestamp_initialized; | |
570 | u16 prev_timestamp; | |
571 | unsigned int timestamp_us; | |
572 | ||
77b499e7 | 573 | u8 ds4_bt_poll_interval; |
f2f47c38 | 574 | enum ds4_dongle_state ds4_dongle_state; |
55a07d62 RC |
575 | /* DS4 calibration data */ |
576 | struct ds4_calibration_data ds4_calib_data[6]; | |
cc6e0bbb JK |
577 | }; |
578 | ||
405182c2 RC |
579 | static void sony_set_leds(struct sony_sc *sc); |
580 | ||
b5322736 RC |
581 | static inline void sony_schedule_work(struct sony_sc *sc, |
582 | enum sony_worker which) | |
2a242932 | 583 | { |
b5322736 RC |
584 | switch (which) { |
585 | case SONY_WORKER_STATE: | |
586 | if (!sc->defer_initialization) | |
587 | schedule_work(&sc->state_worker); | |
f2f47c38 RC |
588 | break; |
589 | case SONY_WORKER_HOTPLUG: | |
590 | if (sc->hotplug_worker_initialized) | |
591 | schedule_work(&sc->hotplug_worker); | |
592 | break; | |
b5322736 | 593 | } |
2a242932 FP |
594 | } |
595 | ||
77b499e7 RC |
596 | static ssize_t ds4_show_poll_interval(struct device *dev, |
597 | struct device_attribute | |
598 | *attr, char *buf) | |
c607fb8d | 599 | { |
77b499e7 RC |
600 | struct hid_device *hdev = to_hid_device(dev); |
601 | struct sony_sc *sc = hid_get_drvdata(hdev); | |
602 | ||
603 | return snprintf(buf, PAGE_SIZE, "%i\n", sc->ds4_bt_poll_interval); | |
c607fb8d AO |
604 | } |
605 | ||
77b499e7 RC |
606 | static ssize_t ds4_store_poll_interval(struct device *dev, |
607 | struct device_attribute *attr, | |
608 | const char *buf, size_t count) | |
c5e0c1c4 | 609 | { |
77b499e7 RC |
610 | struct hid_device *hdev = to_hid_device(dev); |
611 | struct sony_sc *sc = hid_get_drvdata(hdev); | |
612 | unsigned long flags; | |
613 | u8 interval; | |
614 | ||
615 | if (kstrtou8(buf, 0, &interval)) | |
616 | return -EINVAL; | |
617 | ||
618 | if (interval > DS4_BT_MAX_POLL_INTERVAL_MS) | |
619 | return -EINVAL; | |
620 | ||
621 | spin_lock_irqsave(&sc->lock, flags); | |
622 | sc->ds4_bt_poll_interval = interval; | |
623 | spin_unlock_irqrestore(&sc->lock, flags); | |
624 | ||
625 | sony_schedule_work(sc, SONY_WORKER_STATE); | |
626 | ||
627 | return count; | |
c5e0c1c4 FP |
628 | } |
629 | ||
77b499e7 RC |
630 | static DEVICE_ATTR(bt_poll_interval, 0644, ds4_show_poll_interval, |
631 | ds4_store_poll_interval); | |
632 | ||
169f15ab RC |
633 | static ssize_t sony_show_firmware_version(struct device *dev, |
634 | struct device_attribute | |
635 | *attr, char *buf) | |
636 | { | |
637 | struct hid_device *hdev = to_hid_device(dev); | |
638 | struct sony_sc *sc = hid_get_drvdata(hdev); | |
639 | ||
640 | return snprintf(buf, PAGE_SIZE, "0x%04x\n", sc->fw_version); | |
641 | } | |
642 | ||
643 | static DEVICE_ATTR(firmware_version, 0444, sony_show_firmware_version, NULL); | |
644 | ||
645 | static ssize_t sony_show_hardware_version(struct device *dev, | |
646 | struct device_attribute | |
647 | *attr, char *buf) | |
648 | { | |
649 | struct hid_device *hdev = to_hid_device(dev); | |
650 | struct sony_sc *sc = hid_get_drvdata(hdev); | |
651 | ||
652 | return snprintf(buf, PAGE_SIZE, "0x%04x\n", sc->hw_version); | |
653 | } | |
654 | ||
655 | static DEVICE_ATTR(hardware_version, 0444, sony_show_hardware_version, NULL); | |
77b499e7 | 656 | |
c5e0c1c4 | 657 | static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc, |
b2723eb7 SW |
658 | unsigned int *rsize) |
659 | { | |
c5e0c1c4 FP |
660 | *rsize = sizeof(motion_rdesc); |
661 | return motion_rdesc; | |
b2723eb7 SW |
662 | } |
663 | ||
1adf904e | 664 | static u8 *ps3remote_fixup(struct hid_device *hdev, u8 *rdesc, |
078328da JK |
665 | unsigned int *rsize) |
666 | { | |
667 | *rsize = sizeof(ps3remote_rdesc); | |
668 | return ps3remote_rdesc; | |
669 | } | |
670 | ||
671 | static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi, | |
672 | struct hid_field *field, struct hid_usage *usage, | |
673 | unsigned long **bit, int *max) | |
674 | { | |
675 | unsigned int key = usage->hid & HID_USAGE; | |
676 | ||
677 | if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON) | |
678 | return -1; | |
679 | ||
680 | switch (usage->collection_index) { | |
681 | case 1: | |
682 | if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons)) | |
683 | return -1; | |
684 | ||
685 | key = ps3remote_keymap_joypad_buttons[key]; | |
686 | if (!key) | |
687 | return -1; | |
688 | break; | |
689 | case 2: | |
690 | if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons)) | |
691 | return -1; | |
692 | ||
693 | key = ps3remote_keymap_remote_buttons[key]; | |
694 | if (!key) | |
695 | return -1; | |
696 | break; | |
697 | default: | |
698 | return -1; | |
699 | } | |
700 | ||
701 | hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key); | |
702 | return 1; | |
703 | } | |
704 | ||
b8f0970d RC |
705 | static int navigation_mapping(struct hid_device *hdev, struct hid_input *hi, |
706 | struct hid_field *field, struct hid_usage *usage, | |
707 | unsigned long **bit, int *max) | |
708 | { | |
709 | if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) { | |
710 | unsigned int key = usage->hid & HID_USAGE; | |
711 | ||
712 | if (key >= ARRAY_SIZE(sixaxis_keymap)) | |
713 | return -1; | |
714 | ||
715 | key = navigation_keymap[key]; | |
716 | if (!key) | |
717 | return -1; | |
718 | ||
719 | hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key); | |
720 | return 1; | |
721 | } else if (usage->hid == HID_GD_POINTER) { | |
722 | /* See comment in sixaxis_mapping, basically the L2 (and R2) | |
723 | * triggers are reported through GD Pointer. | |
724 | * In addition we ignore any analog button 'axes' and only | |
725 | * support digital buttons. | |
726 | */ | |
727 | switch (usage->usage_index) { | |
728 | case 8: /* L2 */ | |
729 | usage->hid = HID_GD_Z; | |
730 | break; | |
731 | default: | |
732 | return -1; | |
733 | } | |
734 | ||
735 | hid_map_usage_clear(hi, usage, bit, max, EV_ABS, usage->hid & 0xf); | |
736 | return 1; | |
737 | } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) { | |
738 | unsigned int abs = usage->hid & HID_USAGE; | |
739 | ||
740 | if (abs >= ARRAY_SIZE(navigation_absmap)) | |
741 | return -1; | |
742 | ||
743 | abs = navigation_absmap[abs]; | |
744 | ||
745 | hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs); | |
746 | return 1; | |
747 | } | |
748 | ||
749 | return -1; | |
750 | } | |
751 | ||
752 | ||
e19a267b RC |
753 | static int sixaxis_mapping(struct hid_device *hdev, struct hid_input *hi, |
754 | struct hid_field *field, struct hid_usage *usage, | |
755 | unsigned long **bit, int *max) | |
756 | { | |
757 | if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) { | |
758 | unsigned int key = usage->hid & HID_USAGE; | |
759 | ||
760 | if (key >= ARRAY_SIZE(sixaxis_keymap)) | |
761 | return -1; | |
762 | ||
763 | key = sixaxis_keymap[key]; | |
764 | hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key); | |
765 | return 1; | |
766 | } else if (usage->hid == HID_GD_POINTER) { | |
767 | /* The DS3 provides analog values for most buttons and even | |
768 | * for HAT axes through GD Pointer. L2 and R2 are reported | |
769 | * among these as well instead of as GD Z / RZ. Remap L2 | |
770 | * and R2 and ignore other analog 'button axes' as there is | |
771 | * no good way for reporting them. | |
772 | */ | |
773 | switch (usage->usage_index) { | |
774 | case 8: /* L2 */ | |
775 | usage->hid = HID_GD_Z; | |
776 | break; | |
777 | case 9: /* R2 */ | |
778 | usage->hid = HID_GD_RZ; | |
779 | break; | |
780 | default: | |
781 | return -1; | |
782 | } | |
783 | ||
784 | hid_map_usage_clear(hi, usage, bit, max, EV_ABS, usage->hid & 0xf); | |
785 | return 1; | |
786 | } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) { | |
787 | unsigned int abs = usage->hid & HID_USAGE; | |
788 | ||
789 | if (abs >= ARRAY_SIZE(sixaxis_absmap)) | |
790 | return -1; | |
791 | ||
792 | abs = sixaxis_absmap[abs]; | |
793 | ||
794 | hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs); | |
795 | return 1; | |
796 | } | |
797 | ||
798 | return -1; | |
799 | } | |
800 | ||
9131f8cc RC |
801 | static int ds4_mapping(struct hid_device *hdev, struct hid_input *hi, |
802 | struct hid_field *field, struct hid_usage *usage, | |
803 | unsigned long **bit, int *max) | |
804 | { | |
805 | if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) { | |
806 | unsigned int key = usage->hid & HID_USAGE; | |
807 | ||
808 | if (key >= ARRAY_SIZE(ds4_keymap)) | |
809 | return -1; | |
810 | ||
811 | key = ds4_keymap[key]; | |
812 | hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key); | |
813 | return 1; | |
814 | } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) { | |
815 | unsigned int abs = usage->hid & HID_USAGE; | |
816 | ||
817 | /* Let the HID parser deal with the HAT. */ | |
818 | if (usage->hid == HID_GD_HATSWITCH) | |
819 | return 0; | |
820 | ||
821 | if (abs >= ARRAY_SIZE(ds4_absmap)) | |
822 | return -1; | |
823 | ||
824 | abs = ds4_absmap[abs]; | |
825 | hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs); | |
826 | return 1; | |
827 | } | |
828 | ||
829 | return 0; | |
830 | } | |
831 | ||
1adf904e | 832 | static u8 *sony_report_fixup(struct hid_device *hdev, u8 *rdesc, |
73e4008d | 833 | unsigned int *rsize) |
cc6e0bbb JK |
834 | { |
835 | struct sony_sc *sc = hid_get_drvdata(hdev); | |
836 | ||
4ba1eeeb | 837 | if (sc->quirks & (SINO_LITE_CONTROLLER | FUTUREMAX_DANCE_MAT)) |
74500cc8 SM |
838 | return rdesc; |
839 | ||
99d24902 FLVC |
840 | /* |
841 | * Some Sony RF receivers wrongly declare the mouse pointer as a | |
842 | * a constant non-data variable. | |
843 | */ | |
844 | if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 && | |
845 | /* usage page: generic desktop controls */ | |
846 | /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */ | |
847 | /* usage: mouse */ | |
848 | rdesc[2] == 0x09 && rdesc[3] == 0x02 && | |
849 | /* input (usage page for x,y axes): constant, variable, relative */ | |
850 | rdesc[54] == 0x81 && rdesc[55] == 0x07) { | |
a4649184 | 851 | hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n"); |
99d24902 | 852 | /* input: data, variable, relative */ |
cc6e0bbb JK |
853 | rdesc[55] = 0x06; |
854 | } | |
61ab44be | 855 | |
c5e0c1c4 FP |
856 | if (sc->quirks & MOTION_CONTROLLER) |
857 | return motion_fixup(hdev, rdesc, rsize); | |
858 | ||
078328da JK |
859 | if (sc->quirks & PS3REMOTE) |
860 | return ps3remote_fixup(hdev, rdesc, rsize); | |
861 | ||
73e4008d | 862 | return rdesc; |
cc6e0bbb JK |
863 | } |
864 | ||
1adf904e | 865 | static void sixaxis_parse_report(struct sony_sc *sc, u8 *rd, int size) |
d902f472 | 866 | { |
1adf904e | 867 | static const u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 }; |
d902f472 | 868 | unsigned long flags; |
12e9a6d7 | 869 | int offset; |
1adf904e | 870 | u8 cable_state, battery_capacity, battery_charging; |
d902f472 | 871 | |
ad142b9e FP |
872 | /* |
873 | * The sixaxis is charging if the battery value is 0xee | |
d902f472 FP |
874 | * and it is fully charged if the value is 0xef. |
875 | * It does not report the actual level while charging so it | |
876 | * is set to 100% while charging is in progress. | |
877 | */ | |
12e9a6d7 SW |
878 | offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30; |
879 | ||
880 | if (rd[offset] >= 0xee) { | |
d902f472 | 881 | battery_capacity = 100; |
12e9a6d7 | 882 | battery_charging = !(rd[offset] & 0x01); |
9fddd74a | 883 | cable_state = 1; |
d902f472 | 884 | } else { |
1adf904e | 885 | u8 index = rd[offset] <= 5 ? rd[offset] : 5; |
ac3c9a94 | 886 | battery_capacity = sixaxis_battery_capacity[index]; |
d902f472 | 887 | battery_charging = 0; |
9fddd74a | 888 | cable_state = 0; |
d902f472 | 889 | } |
d902f472 FP |
890 | |
891 | spin_lock_irqsave(&sc->lock, flags); | |
892 | sc->cable_state = cable_state; | |
893 | sc->battery_capacity = battery_capacity; | |
894 | sc->battery_charging = battery_charging; | |
895 | spin_unlock_irqrestore(&sc->lock, flags); | |
510c8b7c RC |
896 | |
897 | if (sc->quirks & SIXAXIS_CONTROLLER) { | |
898 | int val; | |
899 | ||
900 | offset = SIXAXIS_INPUT_REPORT_ACC_X_OFFSET; | |
901 | val = ((rd[offset+1] << 8) | rd[offset]) - 511; | |
902 | input_report_abs(sc->sensor_dev, ABS_X, val); | |
903 | ||
904 | /* Y and Z are swapped and inversed */ | |
905 | val = 511 - ((rd[offset+5] << 8) | rd[offset+4]); | |
906 | input_report_abs(sc->sensor_dev, ABS_Y, val); | |
907 | ||
908 | val = 511 - ((rd[offset+3] << 8) | rd[offset+2]); | |
909 | input_report_abs(sc->sensor_dev, ABS_Z, val); | |
910 | ||
911 | input_sync(sc->sensor_dev); | |
912 | } | |
d902f472 FP |
913 | } |
914 | ||
1adf904e | 915 | static void dualshock4_parse_report(struct sony_sc *sc, u8 *rd, int size) |
d902f472 | 916 | { |
d03ae2e1 RC |
917 | struct hid_input *hidinput = list_entry(sc->hdev->inputs.next, |
918 | struct hid_input, list); | |
919 | struct input_dev *input_dev = hidinput->input; | |
d902f472 | 920 | unsigned long flags; |
cdc1c021 | 921 | int n, m, offset, num_touch_data, max_touch_data; |
1adf904e | 922 | u8 cable_state, battery_capacity, battery_charging; |
80786eb9 | 923 | u16 timestamp; |
d902f472 | 924 | |
cdc1c021 | 925 | /* When using Bluetooth the header is 2 bytes longer, so skip these. */ |
35f436c3 | 926 | int data_offset = (sc->quirks & DUALSHOCK4_CONTROLLER_BT) ? 2 : 0; |
6c5f860d | 927 | |
ac797b95 RC |
928 | /* Second bit of third button byte is for the touchpad button. */ |
929 | offset = data_offset + DS4_INPUT_REPORT_BUTTON_OFFSET; | |
930 | input_report_key(sc->touchpad, BTN_LEFT, rd[offset+2] & 0x2); | |
931 | ||
d03ae2e1 RC |
932 | /* |
933 | * The default behavior of the Dualshock 4 is to send reports using | |
934 | * report type 1 when running over Bluetooth. However, when feature | |
935 | * report 2 is requested during the controller initialization it starts | |
936 | * sending input reports in report 17. Since report 17 is undefined | |
937 | * in the default HID descriptor, the HID layer won't generate events. | |
938 | * While it is possible (and this was done before) to fixup the HID | |
939 | * descriptor to add this mapping, it was better to do this manually. | |
940 | * The reason is there were various pieces software both open and closed | |
941 | * source, relying on the descriptors to be the same across various | |
942 | * operating systems. If the descriptors wouldn't match some | |
943 | * applications e.g. games on Wine would not be able to function due | |
944 | * to different descriptors, which such applications are not parsing. | |
945 | */ | |
946 | if (rd[0] == 17) { | |
947 | int value; | |
948 | ||
949 | offset = data_offset + DS4_INPUT_REPORT_AXIS_OFFSET; | |
950 | input_report_abs(input_dev, ABS_X, rd[offset]); | |
951 | input_report_abs(input_dev, ABS_Y, rd[offset+1]); | |
952 | input_report_abs(input_dev, ABS_RX, rd[offset+2]); | |
953 | input_report_abs(input_dev, ABS_RY, rd[offset+3]); | |
954 | ||
955 | value = rd[offset+4] & 0xf; | |
956 | if (value > 7) | |
957 | value = 8; /* Center 0, 0 */ | |
958 | input_report_abs(input_dev, ABS_HAT0X, ds4_hat_mapping[value].x); | |
959 | input_report_abs(input_dev, ABS_HAT0Y, ds4_hat_mapping[value].y); | |
960 | ||
961 | input_report_key(input_dev, BTN_WEST, rd[offset+4] & 0x10); | |
962 | input_report_key(input_dev, BTN_SOUTH, rd[offset+4] & 0x20); | |
963 | input_report_key(input_dev, BTN_EAST, rd[offset+4] & 0x40); | |
964 | input_report_key(input_dev, BTN_NORTH, rd[offset+4] & 0x80); | |
965 | ||
966 | input_report_key(input_dev, BTN_TL, rd[offset+5] & 0x1); | |
967 | input_report_key(input_dev, BTN_TR, rd[offset+5] & 0x2); | |
968 | input_report_key(input_dev, BTN_TL2, rd[offset+5] & 0x4); | |
969 | input_report_key(input_dev, BTN_TR2, rd[offset+5] & 0x8); | |
970 | input_report_key(input_dev, BTN_SELECT, rd[offset+5] & 0x10); | |
971 | input_report_key(input_dev, BTN_START, rd[offset+5] & 0x20); | |
972 | input_report_key(input_dev, BTN_THUMBL, rd[offset+5] & 0x40); | |
973 | input_report_key(input_dev, BTN_THUMBR, rd[offset+5] & 0x80); | |
974 | ||
975 | input_report_key(input_dev, BTN_MODE, rd[offset+6] & 0x1); | |
976 | ||
977 | input_report_abs(input_dev, ABS_Z, rd[offset+7]); | |
978 | input_report_abs(input_dev, ABS_RZ, rd[offset+8]); | |
979 | ||
980 | input_sync(input_dev); | |
981 | } | |
982 | ||
80786eb9 RC |
983 | /* Convert timestamp (in 5.33us unit) to timestamp_us */ |
984 | offset = data_offset + DS4_INPUT_REPORT_TIMESTAMP_OFFSET; | |
985 | timestamp = get_unaligned_le16(&rd[offset]); | |
986 | if (!sc->timestamp_initialized) { | |
987 | sc->timestamp_us = ((unsigned int)timestamp * 16) / 3; | |
988 | sc->timestamp_initialized = true; | |
989 | } else { | |
990 | u16 delta; | |
991 | ||
992 | if (sc->prev_timestamp > timestamp) | |
993 | delta = (U16_MAX - sc->prev_timestamp + timestamp + 1); | |
994 | else | |
995 | delta = timestamp - sc->prev_timestamp; | |
996 | sc->timestamp_us += (delta * 16) / 3; | |
997 | } | |
998 | sc->prev_timestamp = timestamp; | |
999 | input_event(sc->sensor_dev, EV_MSC, MSC_TIMESTAMP, sc->timestamp_us); | |
1000 | ||
227c011b | 1001 | offset = data_offset + DS4_INPUT_REPORT_GYRO_X_OFFSET; |
55a07d62 RC |
1002 | for (n = 0; n < 6; n++) { |
1003 | /* Store data in int for more precision during mult_frac. */ | |
1004 | int raw_data = (short)((rd[offset+1] << 8) | rd[offset]); | |
1005 | struct ds4_calibration_data *calib = &sc->ds4_calib_data[n]; | |
1006 | ||
1007 | /* High precision is needed during calibration, but the | |
1008 | * calibrated values are within 32-bit. | |
1009 | * Note: we swap numerator 'x' and 'numer' in mult_frac for | |
1010 | * precision reasons so we don't need 64-bit. | |
1011 | */ | |
1012 | int calib_data = mult_frac(calib->sens_numer, | |
1013 | raw_data - calib->bias, | |
1014 | calib->sens_denom); | |
227c011b | 1015 | |
55a07d62 RC |
1016 | input_report_abs(sc->sensor_dev, calib->abs_code, calib_data); |
1017 | offset += 2; | |
227c011b RC |
1018 | } |
1019 | input_sync(sc->sensor_dev); | |
1020 | ||
ad142b9e | 1021 | /* |
cdc1c021 | 1022 | * The lower 4 bits of byte 30 (or 32 for BT) contain the battery level |
d902f472 FP |
1023 | * and the 5th bit contains the USB cable state. |
1024 | */ | |
cdc1c021 | 1025 | offset = data_offset + DS4_INPUT_REPORT_BATTERY_OFFSET; |
6c5f860d FP |
1026 | cable_state = (rd[offset] >> 4) & 0x01; |
1027 | battery_capacity = rd[offset] & 0x0F; | |
d902f472 | 1028 | |
ad142b9e FP |
1029 | /* |
1030 | * When a USB power source is connected the battery level ranges from | |
6c5f860d FP |
1031 | * 0 to 10, and when running on battery power it ranges from 0 to 9. |
1032 | * A battery level above 10 when plugged in means charge completed. | |
d902f472 | 1033 | */ |
6c5f860d | 1034 | if (!cable_state || battery_capacity > 10) |
d902f472 FP |
1035 | battery_charging = 0; |
1036 | else | |
1037 | battery_charging = 1; | |
1038 | ||
6c5f860d FP |
1039 | if (!cable_state) |
1040 | battery_capacity++; | |
d902f472 | 1041 | if (battery_capacity > 10) |
6c5f860d FP |
1042 | battery_capacity = 10; |
1043 | ||
d902f472 FP |
1044 | battery_capacity *= 10; |
1045 | ||
1046 | spin_lock_irqsave(&sc->lock, flags); | |
1047 | sc->cable_state = cable_state; | |
1048 | sc->battery_capacity = battery_capacity; | |
1049 | sc->battery_charging = battery_charging; | |
1050 | spin_unlock_irqrestore(&sc->lock, flags); | |
e5606230 | 1051 | |
ad142b9e | 1052 | /* |
cdc1c021 RC |
1053 | * The Dualshock 4 multi-touch trackpad data starts at offset 33 on USB |
1054 | * and 35 on Bluetooth. | |
1055 | * The first byte indicates the number of touch data in the report. | |
1056 | * Trackpad data starts 2 bytes later (e.g. 35 for USB). | |
e5606230 | 1057 | */ |
cdc1c021 | 1058 | offset = data_offset + DS4_INPUT_REPORT_TOUCHPAD_OFFSET; |
35f436c3 | 1059 | max_touch_data = (sc->quirks & DUALSHOCK4_CONTROLLER_BT) ? 4 : 3; |
cdc1c021 RC |
1060 | if (rd[offset] > 0 && rd[offset] <= max_touch_data) |
1061 | num_touch_data = rd[offset]; | |
1062 | else | |
1063 | num_touch_data = 1; | |
1064 | offset += 1; | |
e5606230 | 1065 | |
cdc1c021 RC |
1066 | for (m = 0; m < num_touch_data; m++) { |
1067 | /* Skip past timestamp */ | |
1068 | offset += 1; | |
e5606230 | 1069 | |
cdc1c021 RC |
1070 | /* |
1071 | * The first 7 bits of the first byte is a counter and bit 8 is | |
1072 | * a touch indicator that is 0 when pressed and 1 when not | |
1073 | * pressed. | |
1074 | * The next 3 bytes are two 12 bit touch coordinates, X and Y. | |
1075 | * The data for the second touch is in the same format and | |
1076 | * immediately follows the data for the first. | |
1077 | */ | |
1078 | for (n = 0; n < 2; n++) { | |
1079 | u16 x, y; | |
1080 | bool active; | |
1081 | ||
1082 | x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8); | |
1083 | y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4); | |
1084 | ||
1085 | active = !(rd[offset] >> 7); | |
ac797b95 RC |
1086 | input_mt_slot(sc->touchpad, n); |
1087 | input_mt_report_slot_state(sc->touchpad, MT_TOOL_FINGER, active); | |
cdc1c021 RC |
1088 | |
1089 | if (active) { | |
ac797b95 RC |
1090 | input_report_abs(sc->touchpad, ABS_MT_POSITION_X, x); |
1091 | input_report_abs(sc->touchpad, ABS_MT_POSITION_Y, y); | |
cdc1c021 RC |
1092 | } |
1093 | ||
1094 | offset += 4; | |
1095 | } | |
ac797b95 RC |
1096 | input_mt_sync_frame(sc->touchpad); |
1097 | input_sync(sc->touchpad); | |
e5606230 | 1098 | } |
d902f472 FP |
1099 | } |
1100 | ||
c9e4d877 | 1101 | static int sony_raw_event(struct hid_device *hdev, struct hid_report *report, |
1adf904e | 1102 | u8 *rd, int size) |
c9e4d877 SW |
1103 | { |
1104 | struct sony_sc *sc = hid_get_drvdata(hdev); | |
1105 | ||
ad142b9e FP |
1106 | /* |
1107 | * Sixaxis HID report has acclerometers/gyro with MSByte first, this | |
c9e4d877 SW |
1108 | * has to be BYTE_SWAPPED before passing up to joystick interface |
1109 | */ | |
fee4e2d5 | 1110 | if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) { |
8f5f0bc2 FP |
1111 | /* |
1112 | * When connected via Bluetooth the Sixaxis occasionally sends | |
1113 | * a report with the second byte 0xff and the rest zeroed. | |
1114 | * | |
1115 | * This report does not reflect the actual state of the | |
1116 | * controller must be ignored to avoid generating false input | |
1117 | * events. | |
1118 | */ | |
1119 | if (rd[1] == 0xff) | |
1120 | return -EINVAL; | |
1121 | ||
c9e4d877 SW |
1122 | swap(rd[41], rd[42]); |
1123 | swap(rd[43], rd[44]); | |
1124 | swap(rd[45], rd[46]); | |
1125 | swap(rd[47], rd[48]); | |
d902f472 | 1126 | |
12e9a6d7 SW |
1127 | sixaxis_parse_report(sc, rd, size); |
1128 | } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) { | |
d902f472 | 1129 | sixaxis_parse_report(sc, rd, size); |
4545ee0a SW |
1130 | } else if ((sc->quirks & NAVIGATION_CONTROLLER) && rd[0] == 0x01 && |
1131 | size == 49) { | |
1132 | sixaxis_parse_report(sc, rd, size); | |
35f436c3 RC |
1133 | } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 && |
1134 | size == 64) { | |
1135 | dualshock4_parse_report(sc, rd, size); | |
1136 | } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && rd[0] == 0x11 && | |
1137 | size == 78)) { | |
1138 | /* CRC check */ | |
1139 | u8 bthdr = 0xA1; | |
1140 | u32 crc; | |
1141 | u32 report_crc; | |
49b9ca6c | 1142 | |
35f436c3 RC |
1143 | crc = crc32_le(0xFFFFFFFF, &bthdr, 1); |
1144 | crc = ~crc32_le(crc, rd, DS4_INPUT_REPORT_0x11_SIZE-4); | |
1145 | report_crc = get_unaligned_le32(&rd[DS4_INPUT_REPORT_0x11_SIZE-4]); | |
1146 | if (crc != report_crc) { | |
1147 | hid_dbg(sc->hdev, "DualShock 4 input report's CRC check failed, received crc 0x%0x != 0x%0x\n", | |
1148 | report_crc, crc); | |
1149 | return -EILSEQ; | |
49b9ca6c | 1150 | } |
405182c2 | 1151 | |
35f436c3 RC |
1152 | dualshock4_parse_report(sc, rd, size); |
1153 | } else if ((sc->quirks & DUALSHOCK4_DONGLE) && rd[0] == 0x01 && | |
1154 | size == 64) { | |
f2f47c38 RC |
1155 | unsigned long flags; |
1156 | enum ds4_dongle_state dongle_state; | |
1157 | ||
405182c2 RC |
1158 | /* |
1159 | * In the case of a DS4 USB dongle, bit[2] of byte 31 indicates | |
1160 | * if a DS4 is actually connected (indicated by '0'). | |
1161 | * For non-dongle, this bit is always 0 (connected). | |
1162 | */ | |
35f436c3 RC |
1163 | bool connected = (rd[31] & 0x04) ? false : true; |
1164 | ||
f2f47c38 RC |
1165 | spin_lock_irqsave(&sc->lock, flags); |
1166 | dongle_state = sc->ds4_dongle_state; | |
1167 | spin_unlock_irqrestore(&sc->lock, flags); | |
1168 | ||
1169 | /* | |
1170 | * The dongle always sends input reports even when no | |
1171 | * DS4 is attached. When a DS4 is connected, we need to | |
1172 | * obtain calibration data before we can use it. | |
1173 | * The code below tracks dongle state and kicks of | |
1174 | * calibration when needed and only allows us to process | |
1175 | * input if a DS4 is actually connected. | |
1176 | */ | |
1177 | if (dongle_state == DONGLE_DISCONNECTED && connected) { | |
35f436c3 RC |
1178 | hid_info(sc->hdev, "DualShock 4 USB dongle: controller connected\n"); |
1179 | sony_set_leds(sc); | |
f2f47c38 RC |
1180 | |
1181 | spin_lock_irqsave(&sc->lock, flags); | |
1182 | sc->ds4_dongle_state = DONGLE_CALIBRATING; | |
1183 | spin_unlock_irqrestore(&sc->lock, flags); | |
1184 | ||
1185 | sony_schedule_work(sc, SONY_WORKER_HOTPLUG); | |
1186 | ||
1187 | /* Don't process the report since we don't have | |
1188 | * calibration data, but let hidraw have it anyway. | |
1189 | */ | |
1190 | return 0; | |
1191 | } else if ((dongle_state == DONGLE_CONNECTED || | |
1192 | dongle_state == DONGLE_DISABLED) && !connected) { | |
35f436c3 | 1193 | hid_info(sc->hdev, "DualShock 4 USB dongle: controller disconnected\n"); |
f2f47c38 RC |
1194 | |
1195 | spin_lock_irqsave(&sc->lock, flags); | |
1196 | sc->ds4_dongle_state = DONGLE_DISCONNECTED; | |
1197 | spin_unlock_irqrestore(&sc->lock, flags); | |
1198 | ||
35f436c3 RC |
1199 | /* Return 0, so hidraw can get the report. */ |
1200 | return 0; | |
f2f47c38 RC |
1201 | } else if (dongle_state == DONGLE_CALIBRATING || |
1202 | dongle_state == DONGLE_DISABLED || | |
1203 | dongle_state == DONGLE_DISCONNECTED) { | |
35f436c3 RC |
1204 | /* Return 0, so hidraw can get the report. */ |
1205 | return 0; | |
405182c2 RC |
1206 | } |
1207 | ||
d902f472 | 1208 | dualshock4_parse_report(sc, rd, size); |
c9e4d877 SW |
1209 | } |
1210 | ||
2a242932 FP |
1211 | if (sc->defer_initialization) { |
1212 | sc->defer_initialization = 0; | |
b5322736 | 1213 | sony_schedule_work(sc, SONY_WORKER_STATE); |
2a242932 FP |
1214 | } |
1215 | ||
c9e4d877 SW |
1216 | return 0; |
1217 | } | |
1218 | ||
f04d5140 CL |
1219 | static int sony_mapping(struct hid_device *hdev, struct hid_input *hi, |
1220 | struct hid_field *field, struct hid_usage *usage, | |
1221 | unsigned long **bit, int *max) | |
1222 | { | |
1223 | struct sony_sc *sc = hid_get_drvdata(hdev); | |
1224 | ||
1225 | if (sc->quirks & BUZZ_CONTROLLER) { | |
1226 | unsigned int key = usage->hid & HID_USAGE; | |
1227 | ||
1228 | if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON) | |
1229 | return -1; | |
1230 | ||
1231 | switch (usage->collection_index) { | |
1232 | case 1: | |
1233 | if (key >= ARRAY_SIZE(buzz_keymap)) | |
1234 | return -1; | |
1235 | ||
1236 | key = buzz_keymap[key]; | |
1237 | if (!key) | |
1238 | return -1; | |
1239 | break; | |
1240 | default: | |
1241 | return -1; | |
1242 | } | |
1243 | ||
1244 | hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key); | |
1245 | return 1; | |
1246 | } | |
1247 | ||
078328da JK |
1248 | if (sc->quirks & PS3REMOTE) |
1249 | return ps3remote_mapping(hdev, hi, field, usage, bit, max); | |
1250 | ||
b8f0970d RC |
1251 | if (sc->quirks & NAVIGATION_CONTROLLER) |
1252 | return navigation_mapping(hdev, hi, field, usage, bit, max); | |
1253 | ||
e19a267b RC |
1254 | if (sc->quirks & SIXAXIS_CONTROLLER) |
1255 | return sixaxis_mapping(hdev, hi, field, usage, bit, max); | |
9131f8cc RC |
1256 | |
1257 | if (sc->quirks & DUALSHOCK4_CONTROLLER) | |
1258 | return ds4_mapping(hdev, hi, field, usage, bit, max); | |
1259 | ||
e19a267b | 1260 | |
6f498018 BT |
1261 | /* Let hid-core decide for the others */ |
1262 | return 0; | |
f04d5140 CL |
1263 | } |
1264 | ||
ac797b95 | 1265 | static int sony_register_touchpad(struct sony_sc *sc, int touch_count, |
ce8efc3b FP |
1266 | int w, int h) |
1267 | { | |
ac797b95 RC |
1268 | size_t name_sz; |
1269 | char *name; | |
ce8efc3b FP |
1270 | int ret; |
1271 | ||
ac797b95 RC |
1272 | sc->touchpad = input_allocate_device(); |
1273 | if (!sc->touchpad) | |
1274 | return -ENOMEM; | |
1275 | ||
1276 | input_set_drvdata(sc->touchpad, sc); | |
1277 | sc->touchpad->dev.parent = &sc->hdev->dev; | |
1278 | sc->touchpad->phys = sc->hdev->phys; | |
1279 | sc->touchpad->uniq = sc->hdev->uniq; | |
1280 | sc->touchpad->id.bustype = sc->hdev->bus; | |
1281 | sc->touchpad->id.vendor = sc->hdev->vendor; | |
1282 | sc->touchpad->id.product = sc->hdev->product; | |
1283 | sc->touchpad->id.version = sc->hdev->version; | |
1284 | ||
1285 | /* Append a suffix to the controller name as there are various | |
1286 | * DS4 compatible non-Sony devices with different names. | |
1287 | */ | |
1288 | name_sz = strlen(sc->hdev->name) + sizeof(DS4_TOUCHPAD_SUFFIX); | |
1289 | name = kzalloc(name_sz, GFP_KERNEL); | |
1290 | if (!name) { | |
1291 | ret = -ENOMEM; | |
1292 | goto err; | |
1293 | } | |
1294 | snprintf(name, name_sz, "%s" DS4_TOUCHPAD_SUFFIX, sc->hdev->name); | |
1295 | sc->touchpad->name = name; | |
1296 | ||
b9f7d245 | 1297 | ret = input_mt_init_slots(sc->touchpad, touch_count, INPUT_MT_POINTER); |
ce8efc3b | 1298 | if (ret < 0) |
ac797b95 RC |
1299 | goto err; |
1300 | ||
1301 | /* We map the button underneath the touchpad to BTN_LEFT. */ | |
1302 | __set_bit(EV_KEY, sc->touchpad->evbit); | |
1303 | __set_bit(BTN_LEFT, sc->touchpad->keybit); | |
1304 | __set_bit(INPUT_PROP_BUTTONPAD, sc->touchpad->propbit); | |
ce8efc3b | 1305 | |
ac797b95 RC |
1306 | input_set_abs_params(sc->touchpad, ABS_MT_POSITION_X, 0, w, 0, 0); |
1307 | input_set_abs_params(sc->touchpad, ABS_MT_POSITION_Y, 0, h, 0, 0); | |
1308 | ||
1309 | ret = input_register_device(sc->touchpad); | |
1310 | if (ret < 0) | |
1311 | goto err; | |
ce8efc3b FP |
1312 | |
1313 | return 0; | |
ac797b95 RC |
1314 | |
1315 | err: | |
1316 | kfree(sc->touchpad->name); | |
1317 | sc->touchpad->name = NULL; | |
1318 | ||
1319 | input_free_device(sc->touchpad); | |
1320 | sc->touchpad = NULL; | |
1321 | ||
1322 | return ret; | |
ce8efc3b FP |
1323 | } |
1324 | ||
ac797b95 RC |
1325 | static void sony_unregister_touchpad(struct sony_sc *sc) |
1326 | { | |
1327 | if (!sc->touchpad) | |
1328 | return; | |
1329 | ||
1330 | kfree(sc->touchpad->name); | |
1331 | sc->touchpad->name = NULL; | |
1332 | ||
1333 | input_unregister_device(sc->touchpad); | |
1334 | sc->touchpad = NULL; | |
1335 | } | |
ce8efc3b | 1336 | |
227c011b RC |
1337 | static int sony_register_sensors(struct sony_sc *sc) |
1338 | { | |
1339 | size_t name_sz; | |
1340 | char *name; | |
1341 | int ret; | |
55a07d62 | 1342 | int range; |
227c011b RC |
1343 | |
1344 | sc->sensor_dev = input_allocate_device(); | |
1345 | if (!sc->sensor_dev) | |
1346 | return -ENOMEM; | |
1347 | ||
1348 | input_set_drvdata(sc->sensor_dev, sc); | |
1349 | sc->sensor_dev->dev.parent = &sc->hdev->dev; | |
1350 | sc->sensor_dev->phys = sc->hdev->phys; | |
1351 | sc->sensor_dev->uniq = sc->hdev->uniq; | |
1352 | sc->sensor_dev->id.bustype = sc->hdev->bus; | |
1353 | sc->sensor_dev->id.vendor = sc->hdev->vendor; | |
1354 | sc->sensor_dev->id.product = sc->hdev->product; | |
1355 | sc->sensor_dev->id.version = sc->hdev->version; | |
1356 | ||
1357 | /* Append a suffix to the controller name as there are various | |
1358 | * DS4 compatible non-Sony devices with different names. | |
1359 | */ | |
510c8b7c | 1360 | name_sz = strlen(sc->hdev->name) + sizeof(SENSOR_SUFFIX); |
227c011b RC |
1361 | name = kzalloc(name_sz, GFP_KERNEL); |
1362 | if (!name) { | |
1363 | ret = -ENOMEM; | |
1364 | goto err; | |
1365 | } | |
510c8b7c | 1366 | snprintf(name, name_sz, "%s" SENSOR_SUFFIX, sc->hdev->name); |
227c011b RC |
1367 | sc->sensor_dev->name = name; |
1368 | ||
510c8b7c RC |
1369 | if (sc->quirks & SIXAXIS_CONTROLLER) { |
1370 | /* For the DS3 we only support the accelerometer, which works | |
1371 | * quite well even without calibration. The device also has | |
1372 | * a 1-axis gyro, but it is very difficult to manage from within | |
1373 | * the driver even to get data, the sensor is inaccurate and | |
1374 | * the behavior is very different between hardware revisions. | |
1375 | */ | |
1376 | input_set_abs_params(sc->sensor_dev, ABS_X, -512, 511, 4, 0); | |
1377 | input_set_abs_params(sc->sensor_dev, ABS_Y, -512, 511, 4, 0); | |
1378 | input_set_abs_params(sc->sensor_dev, ABS_Z, -512, 511, 4, 0); | |
1379 | input_abs_set_res(sc->sensor_dev, ABS_X, SIXAXIS_ACC_RES_PER_G); | |
1380 | input_abs_set_res(sc->sensor_dev, ABS_Y, SIXAXIS_ACC_RES_PER_G); | |
1381 | input_abs_set_res(sc->sensor_dev, ABS_Z, SIXAXIS_ACC_RES_PER_G); | |
1382 | } else if (sc->quirks & DUALSHOCK4_CONTROLLER) { | |
1383 | range = DS4_ACC_RES_PER_G*4; | |
1384 | input_set_abs_params(sc->sensor_dev, ABS_X, -range, range, 16, 0); | |
1385 | input_set_abs_params(sc->sensor_dev, ABS_Y, -range, range, 16, 0); | |
1386 | input_set_abs_params(sc->sensor_dev, ABS_Z, -range, range, 16, 0); | |
1387 | input_abs_set_res(sc->sensor_dev, ABS_X, DS4_ACC_RES_PER_G); | |
1388 | input_abs_set_res(sc->sensor_dev, ABS_Y, DS4_ACC_RES_PER_G); | |
1389 | input_abs_set_res(sc->sensor_dev, ABS_Z, DS4_ACC_RES_PER_G); | |
1390 | ||
1391 | range = DS4_GYRO_RES_PER_DEG_S*2048; | |
1392 | input_set_abs_params(sc->sensor_dev, ABS_RX, -range, range, 16, 0); | |
1393 | input_set_abs_params(sc->sensor_dev, ABS_RY, -range, range, 16, 0); | |
1394 | input_set_abs_params(sc->sensor_dev, ABS_RZ, -range, range, 16, 0); | |
1395 | input_abs_set_res(sc->sensor_dev, ABS_RX, DS4_GYRO_RES_PER_DEG_S); | |
1396 | input_abs_set_res(sc->sensor_dev, ABS_RY, DS4_GYRO_RES_PER_DEG_S); | |
1397 | input_abs_set_res(sc->sensor_dev, ABS_RZ, DS4_GYRO_RES_PER_DEG_S); | |
1398 | ||
1399 | __set_bit(EV_MSC, sc->sensor_dev->evbit); | |
1400 | __set_bit(MSC_TIMESTAMP, sc->sensor_dev->mscbit); | |
1401 | } | |
1402 | ||
227c011b RC |
1403 | __set_bit(INPUT_PROP_ACCELEROMETER, sc->sensor_dev->propbit); |
1404 | ||
1405 | ret = input_register_device(sc->sensor_dev); | |
1406 | if (ret < 0) | |
1407 | goto err; | |
1408 | ||
1409 | return 0; | |
1410 | ||
1411 | err: | |
1412 | kfree(sc->sensor_dev->name); | |
1413 | sc->sensor_dev->name = NULL; | |
1414 | ||
1415 | input_free_device(sc->sensor_dev); | |
1416 | sc->sensor_dev = NULL; | |
1417 | ||
1418 | return ret; | |
1419 | } | |
1420 | ||
1421 | static void sony_unregister_sensors(struct sony_sc *sc) | |
1422 | { | |
1423 | if (!sc->sensor_dev) | |
1424 | return; | |
1425 | ||
1426 | kfree(sc->sensor_dev->name); | |
1427 | sc->sensor_dev->name = NULL; | |
1428 | ||
1429 | input_unregister_device(sc->sensor_dev); | |
1430 | sc->sensor_dev = NULL; | |
1431 | } | |
1432 | ||
1433 | ||
bd28ce00 JS |
1434 | /* |
1435 | * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller | |
1436 | * to "operational". Without this, the ps3 controller will not report any | |
1437 | * events. | |
1438 | */ | |
816651a7 | 1439 | static int sixaxis_set_operational_usb(struct hid_device *hdev) |
bd28ce00 | 1440 | { |
a85d67b5 AO |
1441 | const int buf_size = |
1442 | max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE); | |
1adf904e | 1443 | u8 *buf; |
bd28ce00 | 1444 | int ret; |
bd28ce00 | 1445 | |
2e701a35 | 1446 | buf = kmalloc(buf_size, GFP_KERNEL); |
bd28ce00 JS |
1447 | if (!buf) |
1448 | return -ENOMEM; | |
1449 | ||
a85d67b5 AO |
1450 | ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE, |
1451 | HID_FEATURE_REPORT, HID_REQ_GET_REPORT); | |
a7de9b86 LK |
1452 | if (ret < 0) { |
1453 | hid_err(hdev, "can't set operational mode: step 1\n"); | |
1454 | goto out; | |
1455 | } | |
f204828a | 1456 | |
a7de9b86 LK |
1457 | /* |
1458 | * Some compatible controllers like the Speedlink Strike FX and | |
1459 | * Gasia need another query plus an USB interrupt to get operational. | |
1460 | */ | |
a85d67b5 AO |
1461 | ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE, |
1462 | HID_FEATURE_REPORT, HID_REQ_GET_REPORT); | |
a7de9b86 LK |
1463 | if (ret < 0) { |
1464 | hid_err(hdev, "can't set operational mode: step 2\n"); | |
1465 | goto out; | |
1466 | } | |
f204828a | 1467 | |
492ca83c BN |
1468 | /* |
1469 | * But the USB interrupt would cause SHANWAN controllers to | |
1470 | * start rumbling non-stop. | |
1471 | */ | |
1472 | if (strcmp(hdev->name, "SHANWAN PS3 GamePad")) { | |
1473 | ret = hid_hw_output_report(hdev, buf, 1); | |
1474 | if (ret < 0) { | |
1475 | hid_info(hdev, "can't set operational mode: step 3, ignoring\n"); | |
1476 | ret = 0; | |
1477 | } | |
19f4c2ba | 1478 | } |
bd28ce00 | 1479 | |
a7de9b86 | 1480 | out: |
bd28ce00 JS |
1481 | kfree(buf); |
1482 | ||
1483 | return ret; | |
1484 | } | |
1485 | ||
816651a7 | 1486 | static int sixaxis_set_operational_bt(struct hid_device *hdev) |
f9ce7c28 | 1487 | { |
1adf904e PM |
1488 | static const u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 }; |
1489 | u8 *buf; | |
9b2b5c9a FP |
1490 | int ret; |
1491 | ||
1492 | buf = kmemdup(report, sizeof(report), GFP_KERNEL); | |
1493 | if (!buf) | |
1494 | return -ENOMEM; | |
1495 | ||
1496 | ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report), | |
b0dd72aa | 1497 | HID_FEATURE_REPORT, HID_REQ_SET_REPORT); |
9b2b5c9a FP |
1498 | |
1499 | kfree(buf); | |
1500 | ||
1501 | return ret; | |
f9ce7c28 BN |
1502 | } |
1503 | ||
ad142b9e | 1504 | /* |
55a07d62 RC |
1505 | * Request DS4 calibration data for the motion sensors. |
1506 | * For Bluetooth this also affects the operating mode (see below). | |
68330d83 | 1507 | */ |
55a07d62 | 1508 | static int dualshock4_get_calibration_data(struct sony_sc *sc) |
68330d83 | 1509 | { |
1adf904e | 1510 | u8 *buf; |
9b2b5c9a | 1511 | int ret; |
55a07d62 RC |
1512 | short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus; |
1513 | short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus; | |
1514 | short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus; | |
1515 | short gyro_speed_plus, gyro_speed_minus; | |
1516 | short acc_x_plus, acc_x_minus; | |
1517 | short acc_y_plus, acc_y_minus; | |
1518 | short acc_z_plus, acc_z_minus; | |
1519 | int speed_2x; | |
1520 | int range_2g; | |
1521 | ||
1522 | /* For Bluetooth we use a different request, which supports CRC. | |
1523 | * Note: in Bluetooth mode feature report 0x02 also changes the state | |
1524 | * of the controller, so that it sends input reports of type 0x11. | |
1525 | */ | |
35f436c3 | 1526 | if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) { |
55a07d62 RC |
1527 | buf = kmalloc(DS4_FEATURE_REPORT_0x02_SIZE, GFP_KERNEL); |
1528 | if (!buf) | |
1529 | return -ENOMEM; | |
68330d83 | 1530 | |
55a07d62 RC |
1531 | ret = hid_hw_raw_request(sc->hdev, 0x02, buf, |
1532 | DS4_FEATURE_REPORT_0x02_SIZE, | |
1533 | HID_FEATURE_REPORT, | |
1534 | HID_REQ_GET_REPORT); | |
1535 | if (ret < 0) | |
1536 | goto err_stop; | |
1537 | } else { | |
1538 | u8 bthdr = 0xA3; | |
1539 | u32 crc; | |
1540 | u32 report_crc; | |
1541 | int retries; | |
9b2b5c9a | 1542 | |
55a07d62 RC |
1543 | buf = kmalloc(DS4_FEATURE_REPORT_0x05_SIZE, GFP_KERNEL); |
1544 | if (!buf) | |
1545 | return -ENOMEM; | |
9b2b5c9a | 1546 | |
55a07d62 RC |
1547 | for (retries = 0; retries < 3; retries++) { |
1548 | ret = hid_hw_raw_request(sc->hdev, 0x05, buf, | |
1549 | DS4_FEATURE_REPORT_0x05_SIZE, | |
1550 | HID_FEATURE_REPORT, | |
1551 | HID_REQ_GET_REPORT); | |
1552 | if (ret < 0) | |
1553 | goto err_stop; | |
9b2b5c9a | 1554 | |
55a07d62 RC |
1555 | /* CRC check */ |
1556 | crc = crc32_le(0xFFFFFFFF, &bthdr, 1); | |
1557 | crc = ~crc32_le(crc, buf, DS4_FEATURE_REPORT_0x05_SIZE-4); | |
1558 | report_crc = get_unaligned_le32(&buf[DS4_FEATURE_REPORT_0x05_SIZE-4]); | |
1559 | if (crc != report_crc) { | |
1560 | hid_warn(sc->hdev, "DualShock 4 calibration report's CRC check failed, received crc 0x%0x != 0x%0x\n", | |
1561 | report_crc, crc); | |
1562 | if (retries < 2) { | |
1563 | hid_warn(sc->hdev, "Retrying DualShock 4 get calibration report request\n"); | |
1564 | continue; | |
1565 | } else { | |
1566 | ret = -EILSEQ; | |
1567 | goto err_stop; | |
1568 | } | |
1569 | } else { | |
1570 | break; | |
1571 | } | |
1572 | } | |
1573 | } | |
9b2b5c9a | 1574 | |
55a07d62 RC |
1575 | gyro_pitch_bias = get_unaligned_le16(&buf[1]); |
1576 | gyro_yaw_bias = get_unaligned_le16(&buf[3]); | |
1577 | gyro_roll_bias = get_unaligned_le16(&buf[5]); | |
1578 | if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) { | |
1579 | gyro_pitch_plus = get_unaligned_le16(&buf[7]); | |
1580 | gyro_pitch_minus = get_unaligned_le16(&buf[9]); | |
1581 | gyro_yaw_plus = get_unaligned_le16(&buf[11]); | |
1582 | gyro_yaw_minus = get_unaligned_le16(&buf[13]); | |
1583 | gyro_roll_plus = get_unaligned_le16(&buf[15]); | |
1584 | gyro_roll_minus = get_unaligned_le16(&buf[17]); | |
1585 | } else { | |
35f436c3 | 1586 | /* BT + Dongle */ |
55a07d62 RC |
1587 | gyro_pitch_plus = get_unaligned_le16(&buf[7]); |
1588 | gyro_yaw_plus = get_unaligned_le16(&buf[9]); | |
1589 | gyro_roll_plus = get_unaligned_le16(&buf[11]); | |
1590 | gyro_pitch_minus = get_unaligned_le16(&buf[13]); | |
1591 | gyro_yaw_minus = get_unaligned_le16(&buf[15]); | |
1592 | gyro_roll_minus = get_unaligned_le16(&buf[17]); | |
1593 | } | |
1594 | gyro_speed_plus = get_unaligned_le16(&buf[19]); | |
1595 | gyro_speed_minus = get_unaligned_le16(&buf[21]); | |
1596 | acc_x_plus = get_unaligned_le16(&buf[23]); | |
1597 | acc_x_minus = get_unaligned_le16(&buf[25]); | |
1598 | acc_y_plus = get_unaligned_le16(&buf[27]); | |
1599 | acc_y_minus = get_unaligned_le16(&buf[29]); | |
1600 | acc_z_plus = get_unaligned_le16(&buf[31]); | |
1601 | acc_z_minus = get_unaligned_le16(&buf[33]); | |
1602 | ||
1603 | /* Set gyroscope calibration and normalization parameters. | |
1604 | * Data values will be normalized to 1/DS4_GYRO_RES_PER_DEG_S degree/s. | |
1605 | */ | |
1606 | speed_2x = (gyro_speed_plus + gyro_speed_minus); | |
1607 | sc->ds4_calib_data[0].abs_code = ABS_RX; | |
1608 | sc->ds4_calib_data[0].bias = gyro_pitch_bias; | |
1609 | sc->ds4_calib_data[0].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S; | |
1610 | sc->ds4_calib_data[0].sens_denom = gyro_pitch_plus - gyro_pitch_minus; | |
1611 | ||
1612 | sc->ds4_calib_data[1].abs_code = ABS_RY; | |
1613 | sc->ds4_calib_data[1].bias = gyro_yaw_bias; | |
1614 | sc->ds4_calib_data[1].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S; | |
1615 | sc->ds4_calib_data[1].sens_denom = gyro_yaw_plus - gyro_yaw_minus; | |
1616 | ||
1617 | sc->ds4_calib_data[2].abs_code = ABS_RZ; | |
1618 | sc->ds4_calib_data[2].bias = gyro_roll_bias; | |
1619 | sc->ds4_calib_data[2].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S; | |
1620 | sc->ds4_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus; | |
1621 | ||
1622 | /* Set accelerometer calibration and normalization parameters. | |
1623 | * Data values will be normalized to 1/DS4_ACC_RES_PER_G G. | |
1624 | */ | |
1625 | range_2g = acc_x_plus - acc_x_minus; | |
1626 | sc->ds4_calib_data[3].abs_code = ABS_X; | |
1627 | sc->ds4_calib_data[3].bias = acc_x_plus - range_2g / 2; | |
1628 | sc->ds4_calib_data[3].sens_numer = 2*DS4_ACC_RES_PER_G; | |
1629 | sc->ds4_calib_data[3].sens_denom = range_2g; | |
1630 | ||
1631 | range_2g = acc_y_plus - acc_y_minus; | |
1632 | sc->ds4_calib_data[4].abs_code = ABS_Y; | |
1633 | sc->ds4_calib_data[4].bias = acc_y_plus - range_2g / 2; | |
1634 | sc->ds4_calib_data[4].sens_numer = 2*DS4_ACC_RES_PER_G; | |
1635 | sc->ds4_calib_data[4].sens_denom = range_2g; | |
1636 | ||
1637 | range_2g = acc_z_plus - acc_z_minus; | |
1638 | sc->ds4_calib_data[5].abs_code = ABS_Z; | |
1639 | sc->ds4_calib_data[5].bias = acc_z_plus - range_2g / 2; | |
1640 | sc->ds4_calib_data[5].sens_numer = 2*DS4_ACC_RES_PER_G; | |
1641 | sc->ds4_calib_data[5].sens_denom = range_2g; | |
1642 | ||
1643 | err_stop: | |
1644 | kfree(buf); | |
9b2b5c9a | 1645 | return ret; |
f9ce7c28 BN |
1646 | } |
1647 | ||
f2f47c38 RC |
1648 | static void dualshock4_calibration_work(struct work_struct *work) |
1649 | { | |
1650 | struct sony_sc *sc = container_of(work, struct sony_sc, hotplug_worker); | |
1651 | unsigned long flags; | |
1652 | enum ds4_dongle_state dongle_state; | |
1653 | int ret; | |
1654 | ||
1655 | ret = dualshock4_get_calibration_data(sc); | |
1656 | if (ret < 0) { | |
1657 | /* This call is very unlikely to fail for the dongle. When it | |
1658 | * fails we are probably in a very bad state, so mark the | |
1659 | * dongle as disabled. We will re-enable the dongle if a new | |
1660 | * DS4 hotplug is detect from sony_raw_event as any issues | |
1661 | * are likely resolved then (the dongle is quite stupid). | |
1662 | */ | |
1663 | hid_err(sc->hdev, "DualShock 4 USB dongle: calibration failed, disabling device\n"); | |
1664 | dongle_state = DONGLE_DISABLED; | |
1665 | } else { | |
1666 | hid_info(sc->hdev, "DualShock 4 USB dongle: calibration completed\n"); | |
1667 | dongle_state = DONGLE_CONNECTED; | |
1668 | } | |
1669 | ||
1670 | spin_lock_irqsave(&sc->lock, flags); | |
1671 | sc->ds4_dongle_state = dongle_state; | |
1672 | spin_unlock_irqrestore(&sc->lock, flags); | |
1673 | } | |
1674 | ||
169f15ab RC |
1675 | static int dualshock4_get_version_info(struct sony_sc *sc) |
1676 | { | |
1677 | u8 *buf; | |
1678 | int ret; | |
1679 | ||
1680 | buf = kmalloc(DS4_FEATURE_REPORT_0xA3_SIZE, GFP_KERNEL); | |
1681 | if (!buf) | |
1682 | return -ENOMEM; | |
1683 | ||
1684 | ret = hid_hw_raw_request(sc->hdev, 0xA3, buf, | |
1685 | DS4_FEATURE_REPORT_0xA3_SIZE, | |
1686 | HID_FEATURE_REPORT, | |
1687 | HID_REQ_GET_REPORT); | |
1688 | if (ret < 0) { | |
1689 | kfree(buf); | |
1690 | return ret; | |
1691 | } | |
1692 | ||
1693 | sc->hw_version = get_unaligned_le16(&buf[35]); | |
1694 | sc->fw_version = get_unaligned_le16(&buf[41]); | |
1695 | ||
1696 | kfree(buf); | |
1697 | return 0; | |
1698 | } | |
1699 | ||
221399b3 | 1700 | static void sixaxis_set_leds_from_id(struct sony_sc *sc) |
8025087a | 1701 | { |
1adf904e | 1702 | static const u8 sixaxis_leds[10][4] = { |
8025087a FP |
1703 | { 0x01, 0x00, 0x00, 0x00 }, |
1704 | { 0x00, 0x01, 0x00, 0x00 }, | |
1705 | { 0x00, 0x00, 0x01, 0x00 }, | |
1706 | { 0x00, 0x00, 0x00, 0x01 }, | |
1707 | { 0x01, 0x00, 0x00, 0x01 }, | |
1708 | { 0x00, 0x01, 0x00, 0x01 }, | |
1709 | { 0x00, 0x00, 0x01, 0x01 }, | |
1710 | { 0x01, 0x00, 0x01, 0x01 }, | |
1711 | { 0x00, 0x01, 0x01, 0x01 }, | |
1712 | { 0x01, 0x01, 0x01, 0x01 } | |
1713 | }; | |
1714 | ||
221399b3 FP |
1715 | int id = sc->device_id; |
1716 | ||
1717 | BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0])); | |
8025087a FP |
1718 | |
1719 | if (id < 0) | |
1720 | return; | |
1721 | ||
1722 | id %= 10; | |
221399b3 | 1723 | memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id])); |
8025087a FP |
1724 | } |
1725 | ||
221399b3 | 1726 | static void dualshock4_set_leds_from_id(struct sony_sc *sc) |
8025087a FP |
1727 | { |
1728 | /* The first 4 color/index entries match what the PS4 assigns */ | |
1adf904e | 1729 | static const u8 color_code[7][3] = { |
39254a13 RC |
1730 | /* Blue */ { 0x00, 0x00, 0x40 }, |
1731 | /* Red */ { 0x40, 0x00, 0x00 }, | |
1732 | /* Green */ { 0x00, 0x40, 0x00 }, | |
1733 | /* Pink */ { 0x20, 0x00, 0x20 }, | |
8025087a FP |
1734 | /* Orange */ { 0x02, 0x01, 0x00 }, |
1735 | /* Teal */ { 0x00, 0x01, 0x01 }, | |
1736 | /* White */ { 0x01, 0x01, 0x01 } | |
1737 | }; | |
1738 | ||
221399b3 FP |
1739 | int id = sc->device_id; |
1740 | ||
1741 | BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0])); | |
8025087a FP |
1742 | |
1743 | if (id < 0) | |
1744 | return; | |
1745 | ||
1746 | id %= 7; | |
221399b3 | 1747 | memcpy(sc->led_state, color_code[id], sizeof(color_code[id])); |
8025087a FP |
1748 | } |
1749 | ||
221399b3 | 1750 | static void buzz_set_leds(struct sony_sc *sc) |
f04d5140 | 1751 | { |
221399b3 | 1752 | struct hid_device *hdev = sc->hdev; |
f04d5140 CL |
1753 | struct list_head *report_list = |
1754 | &hdev->report_enum[HID_OUTPUT_REPORT].report_list; | |
1755 | struct hid_report *report = list_entry(report_list->next, | |
1756 | struct hid_report, list); | |
1adf904e | 1757 | s32 *value = report->field[0]->value; |
f04d5140 | 1758 | |
221399b3 FP |
1759 | BUILD_BUG_ON(MAX_LEDS < 4); |
1760 | ||
f04d5140 | 1761 | value[0] = 0x00; |
221399b3 FP |
1762 | value[1] = sc->led_state[0] ? 0xff : 0x00; |
1763 | value[2] = sc->led_state[1] ? 0xff : 0x00; | |
1764 | value[3] = sc->led_state[2] ? 0xff : 0x00; | |
1765 | value[4] = sc->led_state[3] ? 0xff : 0x00; | |
f04d5140 CL |
1766 | value[5] = 0x00; |
1767 | value[6] = 0x00; | |
1768 | hid_hw_request(hdev, report, HID_REQ_SET_REPORT); | |
1769 | } | |
1770 | ||
221399b3 | 1771 | static void sony_set_leds(struct sony_sc *sc) |
0a286ef2 | 1772 | { |
221399b3 | 1773 | if (!(sc->quirks & BUZZ_CONTROLLER)) |
b5322736 | 1774 | sony_schedule_work(sc, SONY_WORKER_STATE); |
221399b3 FP |
1775 | else |
1776 | buzz_set_leds(sc); | |
0a286ef2 SE |
1777 | } |
1778 | ||
c5382519 | 1779 | static void sony_led_set_brightness(struct led_classdev *led, |
f04d5140 CL |
1780 | enum led_brightness value) |
1781 | { | |
1782 | struct device *dev = led->dev->parent; | |
ee79a8f8 | 1783 | struct hid_device *hdev = to_hid_device(dev); |
f04d5140 | 1784 | struct sony_sc *drv_data; |
f04d5140 CL |
1785 | |
1786 | int n; | |
b3ed458c | 1787 | int force_update; |
f04d5140 CL |
1788 | |
1789 | drv_data = hid_get_drvdata(hdev); | |
2251b85f | 1790 | if (!drv_data) { |
f04d5140 CL |
1791 | hid_err(hdev, "No device data\n"); |
1792 | return; | |
1793 | } | |
f04d5140 | 1794 | |
b3ed458c FP |
1795 | /* |
1796 | * The Sixaxis on USB will override any LED settings sent to it | |
1797 | * and keep flashing all of the LEDs until the PS button is pressed. | |
1798 | * Updates, even if redundant, must be always be sent to the | |
1799 | * controller to avoid having to toggle the state of an LED just to | |
1800 | * stop the flashing later on. | |
1801 | */ | |
1802 | force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB); | |
1803 | ||
60781cf4 | 1804 | for (n = 0; n < drv_data->led_count; n++) { |
b3ed458c FP |
1805 | if (led == drv_data->leds[n] && (force_update || |
1806 | (value != drv_data->led_state[n] || | |
1807 | drv_data->led_delay_on[n] || | |
1808 | drv_data->led_delay_off[n]))) { | |
1809 | ||
1810 | drv_data->led_state[n] = value; | |
1811 | ||
1812 | /* Setting the brightness stops the blinking */ | |
1813 | drv_data->led_delay_on[n] = 0; | |
1814 | drv_data->led_delay_off[n] = 0; | |
1815 | ||
221399b3 | 1816 | sony_set_leds(drv_data); |
f04d5140 CL |
1817 | break; |
1818 | } | |
1819 | } | |
1820 | } | |
1821 | ||
c5382519 | 1822 | static enum led_brightness sony_led_get_brightness(struct led_classdev *led) |
f04d5140 CL |
1823 | { |
1824 | struct device *dev = led->dev->parent; | |
ee79a8f8 | 1825 | struct hid_device *hdev = to_hid_device(dev); |
f04d5140 | 1826 | struct sony_sc *drv_data; |
f04d5140 CL |
1827 | |
1828 | int n; | |
f04d5140 CL |
1829 | |
1830 | drv_data = hid_get_drvdata(hdev); | |
2251b85f | 1831 | if (!drv_data) { |
f04d5140 CL |
1832 | hid_err(hdev, "No device data\n"); |
1833 | return LED_OFF; | |
1834 | } | |
f04d5140 | 1835 | |
60781cf4 | 1836 | for (n = 0; n < drv_data->led_count; n++) { |
7db7504a SW |
1837 | if (led == drv_data->leds[n]) |
1838 | return drv_data->led_state[n]; | |
f04d5140 CL |
1839 | } |
1840 | ||
7db7504a | 1841 | return LED_OFF; |
f04d5140 | 1842 | } |
f04d5140 | 1843 | |
b3ed458c FP |
1844 | static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on, |
1845 | unsigned long *delay_off) | |
1846 | { | |
1847 | struct device *dev = led->dev->parent; | |
ee79a8f8 | 1848 | struct hid_device *hdev = to_hid_device(dev); |
b3ed458c FP |
1849 | struct sony_sc *drv_data = hid_get_drvdata(hdev); |
1850 | int n; | |
1adf904e | 1851 | u8 new_on, new_off; |
b3ed458c FP |
1852 | |
1853 | if (!drv_data) { | |
1854 | hid_err(hdev, "No device data\n"); | |
1855 | return -EINVAL; | |
1856 | } | |
1857 | ||
1858 | /* Max delay is 255 deciseconds or 2550 milliseconds */ | |
1859 | if (*delay_on > 2550) | |
1860 | *delay_on = 2550; | |
1861 | if (*delay_off > 2550) | |
1862 | *delay_off = 2550; | |
1863 | ||
1864 | /* Blink at 1 Hz if both values are zero */ | |
1865 | if (!*delay_on && !*delay_off) | |
1866 | *delay_on = *delay_off = 500; | |
1867 | ||
1868 | new_on = *delay_on / 10; | |
1869 | new_off = *delay_off / 10; | |
1870 | ||
1871 | for (n = 0; n < drv_data->led_count; n++) { | |
1872 | if (led == drv_data->leds[n]) | |
1873 | break; | |
1874 | } | |
1875 | ||
1876 | /* This LED is not registered on this device */ | |
1877 | if (n >= drv_data->led_count) | |
1878 | return -EINVAL; | |
1879 | ||
1880 | /* Don't schedule work if the values didn't change */ | |
1881 | if (new_on != drv_data->led_delay_on[n] || | |
1882 | new_off != drv_data->led_delay_off[n]) { | |
1883 | drv_data->led_delay_on[n] = new_on; | |
1884 | drv_data->led_delay_off[n] = new_off; | |
b5322736 | 1885 | sony_schedule_work(drv_data, SONY_WORKER_STATE); |
b3ed458c FP |
1886 | } |
1887 | ||
1888 | return 0; | |
1889 | } | |
1890 | ||
fa57a810 | 1891 | static void sony_leds_remove(struct sony_sc *sc) |
0a286ef2 | 1892 | { |
0a286ef2 SE |
1893 | struct led_classdev *led; |
1894 | int n; | |
1895 | ||
fa57a810 | 1896 | BUG_ON(!(sc->quirks & SONY_LED_SUPPORT)); |
0a286ef2 | 1897 | |
fa57a810 FP |
1898 | for (n = 0; n < sc->led_count; n++) { |
1899 | led = sc->leds[n]; | |
1900 | sc->leds[n] = NULL; | |
0a286ef2 SE |
1901 | if (!led) |
1902 | continue; | |
1903 | led_classdev_unregister(led); | |
1904 | kfree(led); | |
1905 | } | |
60781cf4 | 1906 | |
fa57a810 | 1907 | sc->led_count = 0; |
0a286ef2 SE |
1908 | } |
1909 | ||
fa57a810 | 1910 | static int sony_leds_init(struct sony_sc *sc) |
f04d5140 | 1911 | { |
fa57a810 | 1912 | struct hid_device *hdev = sc->hdev; |
40e32ee6 | 1913 | int n, ret = 0; |
b3ed458c | 1914 | int use_ds4_names; |
40e32ee6 JK |
1915 | struct led_classdev *led; |
1916 | size_t name_sz; | |
1917 | char *name; | |
0a286ef2 SE |
1918 | size_t name_len; |
1919 | const char *name_fmt; | |
b3ed458c FP |
1920 | static const char * const ds4_name_str[] = { "red", "green", "blue", |
1921 | "global" }; | |
1adf904e PM |
1922 | u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 }; |
1923 | u8 use_hw_blink[MAX_LEDS] = { 0 }; | |
f04d5140 | 1924 | |
fa57a810 | 1925 | BUG_ON(!(sc->quirks & SONY_LED_SUPPORT)); |
0a286ef2 | 1926 | |
fa57a810 FP |
1927 | if (sc->quirks & BUZZ_CONTROLLER) { |
1928 | sc->led_count = 4; | |
b3ed458c | 1929 | use_ds4_names = 0; |
0a286ef2 SE |
1930 | name_len = strlen("::buzz#"); |
1931 | name_fmt = "%s::buzz%d"; | |
1932 | /* Validate expected report characteristics. */ | |
1933 | if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7)) | |
1934 | return -ENODEV; | |
fa57a810 | 1935 | } else if (sc->quirks & DUALSHOCK4_CONTROLLER) { |
221399b3 FP |
1936 | dualshock4_set_leds_from_id(sc); |
1937 | sc->led_state[3] = 1; | |
b3ed458c FP |
1938 | sc->led_count = 4; |
1939 | memset(max_brightness, 255, 3); | |
1940 | use_hw_blink[3] = 1; | |
1941 | use_ds4_names = 1; | |
61ebca93 FP |
1942 | name_len = 0; |
1943 | name_fmt = "%s:%s"; | |
c5e0c1c4 FP |
1944 | } else if (sc->quirks & MOTION_CONTROLLER) { |
1945 | sc->led_count = 3; | |
1946 | memset(max_brightness, 255, 3); | |
1947 | use_ds4_names = 1; | |
1948 | name_len = 0; | |
1949 | name_fmt = "%s:%s"; | |
4545ee0a | 1950 | } else if (sc->quirks & NAVIGATION_CONTROLLER) { |
1adf904e | 1951 | static const u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00}; |
4545ee0a SW |
1952 | |
1953 | memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds)); | |
1954 | sc->led_count = 1; | |
1955 | memset(use_hw_blink, 1, 4); | |
1956 | use_ds4_names = 0; | |
1957 | name_len = strlen("::sony#"); | |
1958 | name_fmt = "%s::sony%d"; | |
60781cf4 | 1959 | } else { |
221399b3 | 1960 | sixaxis_set_leds_from_id(sc); |
fa57a810 | 1961 | sc->led_count = 4; |
b3ed458c FP |
1962 | memset(use_hw_blink, 1, 4); |
1963 | use_ds4_names = 0; | |
61ebca93 FP |
1964 | name_len = strlen("::sony#"); |
1965 | name_fmt = "%s::sony%d"; | |
60781cf4 FP |
1966 | } |
1967 | ||
ad142b9e FP |
1968 | /* |
1969 | * Clear LEDs as we have no way of reading their initial state. This is | |
f04d5140 | 1970 | * only relevant if the driver is loaded after somebody actively set the |
ad142b9e FP |
1971 | * LEDs to on |
1972 | */ | |
221399b3 | 1973 | sony_set_leds(sc); |
f04d5140 | 1974 | |
0a286ef2 | 1975 | name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1; |
f04d5140 | 1976 | |
fa57a810 | 1977 | for (n = 0; n < sc->led_count; n++) { |
61ebca93 | 1978 | |
b3ed458c FP |
1979 | if (use_ds4_names) |
1980 | name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2; | |
61ebca93 | 1981 | |
40e32ee6 JK |
1982 | led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL); |
1983 | if (!led) { | |
1984 | hid_err(hdev, "Couldn't allocate memory for LED %d\n", n); | |
8cd5fcda | 1985 | ret = -ENOMEM; |
40e32ee6 JK |
1986 | goto error_leds; |
1987 | } | |
f04d5140 | 1988 | |
40e32ee6 | 1989 | name = (void *)(&led[1]); |
b3ed458c FP |
1990 | if (use_ds4_names) |
1991 | snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), | |
1992 | ds4_name_str[n]); | |
61ebca93 FP |
1993 | else |
1994 | snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1); | |
40e32ee6 | 1995 | led->name = name; |
221399b3 | 1996 | led->brightness = sc->led_state[n]; |
b3ed458c | 1997 | led->max_brightness = max_brightness[n]; |
765a1077 | 1998 | led->flags = LED_CORE_SUSPENDRESUME; |
c5382519 SE |
1999 | led->brightness_get = sony_led_get_brightness; |
2000 | led->brightness_set = sony_led_set_brightness; | |
f04d5140 | 2001 | |
b3ed458c FP |
2002 | if (use_hw_blink[n]) |
2003 | led->blink_set = sony_led_blink_set; | |
2004 | ||
8025087a FP |
2005 | sc->leds[n] = led; |
2006 | ||
8cd5fcda JL |
2007 | ret = led_classdev_register(&hdev->dev, led); |
2008 | if (ret) { | |
40e32ee6 | 2009 | hid_err(hdev, "Failed to register LED %d\n", n); |
8025087a | 2010 | sc->leds[n] = NULL; |
40e32ee6 JK |
2011 | kfree(led); |
2012 | goto error_leds; | |
f04d5140 CL |
2013 | } |
2014 | } | |
f04d5140 CL |
2015 | |
2016 | return ret; | |
2017 | ||
f04d5140 | 2018 | error_leds: |
fa57a810 | 2019 | sony_leds_remove(sc); |
f04d5140 | 2020 | |
f04d5140 | 2021 | return ret; |
f04d5140 CL |
2022 | } |
2023 | ||
d8aaccda | 2024 | static void sixaxis_send_output_report(struct sony_sc *sc) |
a08c22c0 | 2025 | { |
9b2b5c9a | 2026 | static const union sixaxis_output_report_01 default_report = { |
55d3b664 FP |
2027 | .buf = { |
2028 | 0x01, | |
ad07b7a6 | 2029 | 0x01, 0xff, 0x00, 0xff, 0x00, |
55d3b664 FP |
2030 | 0x00, 0x00, 0x00, 0x00, 0x00, |
2031 | 0xff, 0x27, 0x10, 0x00, 0x32, | |
2032 | 0xff, 0x27, 0x10, 0x00, 0x32, | |
2033 | 0xff, 0x27, 0x10, 0x00, 0x32, | |
2034 | 0xff, 0x27, 0x10, 0x00, 0x32, | |
2035 | 0x00, 0x00, 0x00, 0x00, 0x00 | |
2036 | } | |
a08c22c0 | 2037 | }; |
9b2b5c9a FP |
2038 | struct sixaxis_output_report *report = |
2039 | (struct sixaxis_output_report *)sc->output_report_dmabuf; | |
2040 | int n; | |
2041 | ||
2042 | /* Initialize the report with default values */ | |
2043 | memcpy(report, &default_report, sizeof(struct sixaxis_output_report)); | |
9f323b68 | 2044 | |
0a286ef2 | 2045 | #ifdef CONFIG_SONY_FF |
9b2b5c9a FP |
2046 | report->rumble.right_motor_on = sc->right ? 1 : 0; |
2047 | report->rumble.left_motor_force = sc->left; | |
0a286ef2 SE |
2048 | #endif |
2049 | ||
9b2b5c9a FP |
2050 | report->leds_bitmap |= sc->led_state[0] << 1; |
2051 | report->leds_bitmap |= sc->led_state[1] << 2; | |
2052 | report->leds_bitmap |= sc->led_state[2] << 3; | |
2053 | report->leds_bitmap |= sc->led_state[3] << 4; | |
9f323b68 | 2054 | |
88f6576f | 2055 | /* Set flag for all leds off, required for 3rd party INTEC controller */ |
9b2b5c9a FP |
2056 | if ((report->leds_bitmap & 0x1E) == 0) |
2057 | report->leds_bitmap |= 0x20; | |
88f6576f | 2058 | |
b3ed458c FP |
2059 | /* |
2060 | * The LEDs in the report are indexed in reverse order to their | |
2061 | * corresponding light on the controller. | |
2062 | * Index 0 = LED 4, index 1 = LED 3, etc... | |
2063 | * | |
2064 | * In the case of both delay values being zero (blinking disabled) the | |
2065 | * default report values should be used or the controller LED will be | |
2066 | * always off. | |
2067 | */ | |
2068 | for (n = 0; n < 4; n++) { | |
2069 | if (sc->led_delay_on[n] || sc->led_delay_off[n]) { | |
9b2b5c9a FP |
2070 | report->led[3 - n].duty_off = sc->led_delay_off[n]; |
2071 | report->led[3 - n].duty_on = sc->led_delay_on[n]; | |
b3ed458c FP |
2072 | } |
2073 | } | |
2074 | ||
1adf904e | 2075 | hid_hw_raw_request(sc->hdev, report->report_id, (u8 *)report, |
9b2b5c9a FP |
2076 | sizeof(struct sixaxis_output_report), |
2077 | HID_OUTPUT_REPORT, HID_REQ_SET_REPORT); | |
9f323b68 SE |
2078 | } |
2079 | ||
d8aaccda | 2080 | static void dualshock4_send_output_report(struct sony_sc *sc) |
0bd88dd3 | 2081 | { |
0da8ea65 | 2082 | struct hid_device *hdev = sc->hdev; |
1adf904e | 2083 | u8 *buf = sc->output_report_dmabuf; |
48220237 FP |
2084 | int offset; |
2085 | ||
c4425c8f | 2086 | /* |
77b499e7 RC |
2087 | * NOTE: The lower 6 bits of buf[1] field of the Bluetooth report |
2088 | * control the interval at which Dualshock 4 reports data: | |
2089 | * 0x00 - 1ms | |
2090 | * 0x01 - 1ms | |
2091 | * 0x02 - 2ms | |
2092 | * 0x3E - 62ms | |
2093 | * 0x3F - disabled | |
c4425c8f | 2094 | */ |
35f436c3 | 2095 | if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) { |
2c159de0 | 2096 | memset(buf, 0, DS4_OUTPUT_REPORT_0x05_SIZE); |
fdcf105d | 2097 | buf[0] = 0x05; |
5caceb06 | 2098 | buf[1] = 0x07; /* blink + LEDs + motor */ |
fdcf105d FP |
2099 | offset = 4; |
2100 | } else { | |
2c159de0 | 2101 | memset(buf, 0, DS4_OUTPUT_REPORT_0x11_SIZE); |
fdcf105d | 2102 | buf[0] = 0x11; |
77b499e7 | 2103 | buf[1] = 0xC0 /* HID + CRC */ | sc->ds4_bt_poll_interval; |
5caceb06 | 2104 | buf[3] = 0x07; /* blink + LEDs + motor */ |
fdcf105d FP |
2105 | offset = 6; |
2106 | } | |
0bd88dd3 FP |
2107 | |
2108 | #ifdef CONFIG_SONY_FF | |
48220237 FP |
2109 | buf[offset++] = sc->right; |
2110 | buf[offset++] = sc->left; | |
2111 | #else | |
2112 | offset += 2; | |
0bd88dd3 FP |
2113 | #endif |
2114 | ||
b3ed458c FP |
2115 | /* LED 3 is the global control */ |
2116 | if (sc->led_state[3]) { | |
2117 | buf[offset++] = sc->led_state[0]; | |
2118 | buf[offset++] = sc->led_state[1]; | |
2119 | buf[offset++] = sc->led_state[2]; | |
2120 | } else { | |
2121 | offset += 3; | |
2122 | } | |
2123 | ||
2124 | /* If both delay values are zero the DualShock 4 disables blinking. */ | |
2125 | buf[offset++] = sc->led_delay_on[3]; | |
2126 | buf[offset++] = sc->led_delay_off[3]; | |
60781cf4 | 2127 | |
35f436c3 | 2128 | if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) |
2c159de0 | 2129 | hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x05_SIZE); |
e7ef53ad RC |
2130 | else { |
2131 | /* CRC generation */ | |
2132 | u8 bthdr = 0xA2; | |
2133 | u32 crc; | |
2134 | ||
2135 | crc = crc32_le(0xFFFFFFFF, &bthdr, 1); | |
2136 | crc = ~crc32_le(crc, buf, DS4_OUTPUT_REPORT_0x11_SIZE-4); | |
2137 | put_unaligned_le32(crc, &buf[74]); | |
2138 | hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x11_SIZE); | |
2139 | } | |
0bd88dd3 FP |
2140 | } |
2141 | ||
d8aaccda | 2142 | static void motion_send_output_report(struct sony_sc *sc) |
c5e0c1c4 | 2143 | { |
c5e0c1c4 FP |
2144 | struct hid_device *hdev = sc->hdev; |
2145 | struct motion_output_report_02 *report = | |
2146 | (struct motion_output_report_02 *)sc->output_report_dmabuf; | |
2147 | ||
41d2d425 | 2148 | memset(report, 0, MOTION_REPORT_0x02_SIZE); |
c5e0c1c4 FP |
2149 | |
2150 | report->type = 0x02; /* set leds */ | |
2151 | report->r = sc->led_state[0]; | |
2152 | report->g = sc->led_state[1]; | |
2153 | report->b = sc->led_state[2]; | |
2154 | ||
2155 | #ifdef CONFIG_SONY_FF | |
2156 | report->rumble = max(sc->right, sc->left); | |
2157 | #endif | |
2158 | ||
1adf904e | 2159 | hid_hw_output_report(hdev, (u8 *)report, MOTION_REPORT_0x02_SIZE); |
c5e0c1c4 FP |
2160 | } |
2161 | ||
decd946c FP |
2162 | static inline void sony_send_output_report(struct sony_sc *sc) |
2163 | { | |
2164 | if (sc->send_output_report) | |
2165 | sc->send_output_report(sc); | |
2166 | } | |
2167 | ||
d8aaccda FP |
2168 | static void sony_state_worker(struct work_struct *work) |
2169 | { | |
2170 | struct sony_sc *sc = container_of(work, struct sony_sc, state_worker); | |
ef916ef5 | 2171 | |
d8aaccda FP |
2172 | sc->send_output_report(sc); |
2173 | } | |
2174 | ||
9b2b5c9a FP |
2175 | static int sony_allocate_output_report(struct sony_sc *sc) |
2176 | { | |
4545ee0a SW |
2177 | if ((sc->quirks & SIXAXIS_CONTROLLER) || |
2178 | (sc->quirks & NAVIGATION_CONTROLLER)) | |
9b2b5c9a FP |
2179 | sc->output_report_dmabuf = |
2180 | kmalloc(sizeof(union sixaxis_output_report_01), | |
2181 | GFP_KERNEL); | |
2182 | else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) | |
2c159de0 | 2183 | sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x11_SIZE, |
9b2b5c9a | 2184 | GFP_KERNEL); |
35f436c3 | 2185 | else if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) |
2c159de0 | 2186 | sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x05_SIZE, |
9b2b5c9a | 2187 | GFP_KERNEL); |
c5e0c1c4 | 2188 | else if (sc->quirks & MOTION_CONTROLLER) |
41d2d425 SW |
2189 | sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE, |
2190 | GFP_KERNEL); | |
9b2b5c9a FP |
2191 | else |
2192 | return 0; | |
2193 | ||
2194 | if (!sc->output_report_dmabuf) | |
2195 | return -ENOMEM; | |
2196 | ||
2197 | return 0; | |
2198 | } | |
2199 | ||
0a286ef2 | 2200 | #ifdef CONFIG_SONY_FF |
9f323b68 SE |
2201 | static int sony_play_effect(struct input_dev *dev, void *data, |
2202 | struct ff_effect *effect) | |
2203 | { | |
a08c22c0 | 2204 | struct hid_device *hid = input_get_drvdata(dev); |
9f323b68 | 2205 | struct sony_sc *sc = hid_get_drvdata(hid); |
a08c22c0 SE |
2206 | |
2207 | if (effect->type != FF_RUMBLE) | |
2208 | return 0; | |
2209 | ||
9f323b68 | 2210 | sc->left = effect->u.rumble.strong_magnitude / 256; |
0bd88dd3 | 2211 | sc->right = effect->u.rumble.weak_magnitude / 256; |
a08c22c0 | 2212 | |
b5322736 | 2213 | sony_schedule_work(sc, SONY_WORKER_STATE); |
9f323b68 | 2214 | return 0; |
a08c22c0 SE |
2215 | } |
2216 | ||
fa57a810 | 2217 | static int sony_init_ff(struct sony_sc *sc) |
a08c22c0 | 2218 | { |
fa57a810 | 2219 | struct hid_input *hidinput = list_entry(sc->hdev->inputs.next, |
a08c22c0 SE |
2220 | struct hid_input, list); |
2221 | struct input_dev *input_dev = hidinput->input; | |
2222 | ||
2223 | input_set_capability(input_dev, EV_FF, FF_RUMBLE); | |
2224 | return input_ff_create_memless(input_dev, NULL, sony_play_effect); | |
2225 | } | |
2226 | ||
2227 | #else | |
fa57a810 | 2228 | static int sony_init_ff(struct sony_sc *sc) |
a08c22c0 SE |
2229 | { |
2230 | return 0; | |
2231 | } | |
9f323b68 | 2232 | |
a08c22c0 SE |
2233 | #endif |
2234 | ||
d902f472 FP |
2235 | static int sony_battery_get_property(struct power_supply *psy, |
2236 | enum power_supply_property psp, | |
2237 | union power_supply_propval *val) | |
2238 | { | |
297d716f | 2239 | struct sony_sc *sc = power_supply_get_drvdata(psy); |
d902f472 FP |
2240 | unsigned long flags; |
2241 | int ret = 0; | |
2242 | u8 battery_charging, battery_capacity, cable_state; | |
2243 | ||
2244 | spin_lock_irqsave(&sc->lock, flags); | |
2245 | battery_charging = sc->battery_charging; | |
2246 | battery_capacity = sc->battery_capacity; | |
2247 | cable_state = sc->cable_state; | |
2248 | spin_unlock_irqrestore(&sc->lock, flags); | |
2249 | ||
2250 | switch (psp) { | |
2251 | case POWER_SUPPLY_PROP_PRESENT: | |
2252 | val->intval = 1; | |
2253 | break; | |
2254 | case POWER_SUPPLY_PROP_SCOPE: | |
2255 | val->intval = POWER_SUPPLY_SCOPE_DEVICE; | |
2256 | break; | |
2257 | case POWER_SUPPLY_PROP_CAPACITY: | |
2258 | val->intval = battery_capacity; | |
2259 | break; | |
2260 | case POWER_SUPPLY_PROP_STATUS: | |
2261 | if (battery_charging) | |
2262 | val->intval = POWER_SUPPLY_STATUS_CHARGING; | |
2263 | else | |
2264 | if (battery_capacity == 100 && cable_state) | |
2265 | val->intval = POWER_SUPPLY_STATUS_FULL; | |
2266 | else | |
2267 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | |
2268 | break; | |
2269 | default: | |
2270 | ret = -EINVAL; | |
2271 | break; | |
2272 | } | |
2273 | return ret; | |
9f323b68 SE |
2274 | } |
2275 | ||
0f398230 | 2276 | static int sony_battery_probe(struct sony_sc *sc, int append_dev_id) |
c4e1ddf2 | 2277 | { |
0f398230 FP |
2278 | const char *battery_str_fmt = append_dev_id ? |
2279 | "sony_controller_battery_%pMR_%i" : | |
2280 | "sony_controller_battery_%pMR"; | |
297d716f | 2281 | struct power_supply_config psy_cfg = { .drv_data = sc, }; |
c4e1ddf2 | 2282 | struct hid_device *hdev = sc->hdev; |
d902f472 | 2283 | int ret; |
c4e1ddf2 | 2284 | |
ad142b9e FP |
2285 | /* |
2286 | * Set the default battery level to 100% to avoid low battery warnings | |
d9a293a9 FP |
2287 | * if the battery is polled before the first device report is received. |
2288 | */ | |
2289 | sc->battery_capacity = 100; | |
2290 | ||
297d716f KK |
2291 | sc->battery_desc.properties = sony_battery_props; |
2292 | sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props); | |
2293 | sc->battery_desc.get_property = sony_battery_get_property; | |
2294 | sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY; | |
2295 | sc->battery_desc.use_for_apm = 0; | |
0f398230 FP |
2296 | sc->battery_desc.name = kasprintf(GFP_KERNEL, battery_str_fmt, |
2297 | sc->mac_address, sc->device_id); | |
297d716f | 2298 | if (!sc->battery_desc.name) |
d902f472 | 2299 | return -ENOMEM; |
c4e1ddf2 | 2300 | |
297d716f KK |
2301 | sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc, |
2302 | &psy_cfg); | |
2303 | if (IS_ERR(sc->battery)) { | |
2304 | ret = PTR_ERR(sc->battery); | |
d902f472 FP |
2305 | hid_err(hdev, "Unable to register battery device\n"); |
2306 | goto err_free; | |
2307 | } | |
c4e1ddf2 | 2308 | |
297d716f | 2309 | power_supply_powers(sc->battery, &hdev->dev); |
a08c22c0 | 2310 | return 0; |
d902f472 FP |
2311 | |
2312 | err_free: | |
297d716f KK |
2313 | kfree(sc->battery_desc.name); |
2314 | sc->battery_desc.name = NULL; | |
d902f472 | 2315 | return ret; |
a08c22c0 | 2316 | } |
9f323b68 | 2317 | |
d902f472 | 2318 | static void sony_battery_remove(struct sony_sc *sc) |
9f323b68 | 2319 | { |
297d716f | 2320 | if (!sc->battery_desc.name) |
d902f472 FP |
2321 | return; |
2322 | ||
297d716f KK |
2323 | power_supply_unregister(sc->battery); |
2324 | kfree(sc->battery_desc.name); | |
2325 | sc->battery_desc.name = NULL; | |
9f323b68 | 2326 | } |
a08c22c0 | 2327 | |
d2d782fc FP |
2328 | /* |
2329 | * If a controller is plugged in via USB while already connected via Bluetooth | |
2330 | * it will show up as two devices. A global list of connected controllers and | |
2331 | * their MAC addresses is maintained to ensure that a device is only connected | |
2332 | * once. | |
0f398230 FP |
2333 | * |
2334 | * Some USB-only devices masquerade as Sixaxis controllers and all have the | |
2335 | * same dummy Bluetooth address, so a comparison of the connection type is | |
2336 | * required. Devices are only rejected in the case where two devices have | |
2337 | * matching Bluetooth addresses on different bus types. | |
d2d782fc | 2338 | */ |
0f398230 FP |
2339 | static inline int sony_compare_connection_type(struct sony_sc *sc0, |
2340 | struct sony_sc *sc1) | |
2341 | { | |
2342 | const int sc0_not_bt = !(sc0->quirks & SONY_BT_DEVICE); | |
2343 | const int sc1_not_bt = !(sc1->quirks & SONY_BT_DEVICE); | |
2344 | ||
2345 | return sc0_not_bt == sc1_not_bt; | |
2346 | } | |
2347 | ||
d2d782fc FP |
2348 | static int sony_check_add_dev_list(struct sony_sc *sc) |
2349 | { | |
2350 | struct sony_sc *entry; | |
2351 | unsigned long flags; | |
2352 | int ret; | |
2353 | ||
2354 | spin_lock_irqsave(&sony_dev_list_lock, flags); | |
2355 | ||
2356 | list_for_each_entry(entry, &sony_device_list, list_node) { | |
2357 | ret = memcmp(sc->mac_address, entry->mac_address, | |
2358 | sizeof(sc->mac_address)); | |
2359 | if (!ret) { | |
0f398230 FP |
2360 | if (sony_compare_connection_type(sc, entry)) { |
2361 | ret = 1; | |
2362 | } else { | |
2363 | ret = -EEXIST; | |
2364 | hid_info(sc->hdev, | |
2365 | "controller with MAC address %pMR already connected\n", | |
d2d782fc | 2366 | sc->mac_address); |
0f398230 | 2367 | } |
d2d782fc | 2368 | goto unlock; |
c4e1ddf2 FP |
2369 | } |
2370 | } | |
2371 | ||
d2d782fc FP |
2372 | ret = 0; |
2373 | list_add(&(sc->list_node), &sony_device_list); | |
c4e1ddf2 | 2374 | |
d2d782fc FP |
2375 | unlock: |
2376 | spin_unlock_irqrestore(&sony_dev_list_lock, flags); | |
2377 | return ret; | |
2378 | } | |
2379 | ||
2380 | static void sony_remove_dev_list(struct sony_sc *sc) | |
2381 | { | |
2382 | unsigned long flags; | |
c4e1ddf2 | 2383 | |
d2d782fc FP |
2384 | if (sc->list_node.next) { |
2385 | spin_lock_irqsave(&sony_dev_list_lock, flags); | |
2386 | list_del(&(sc->list_node)); | |
2387 | spin_unlock_irqrestore(&sony_dev_list_lock, flags); | |
2388 | } | |
c4e1ddf2 FP |
2389 | } |
2390 | ||
d2d782fc FP |
2391 | static int sony_get_bt_devaddr(struct sony_sc *sc) |
2392 | { | |
2393 | int ret; | |
2394 | ||
2395 | /* HIDP stores the device MAC address as a string in the uniq field. */ | |
2396 | ret = strlen(sc->hdev->uniq); | |
2397 | if (ret != 17) | |
2398 | return -EINVAL; | |
2399 | ||
2400 | ret = sscanf(sc->hdev->uniq, | |
2401 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", | |
2402 | &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3], | |
2403 | &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]); | |
2404 | ||
2405 | if (ret != 6) | |
2406 | return -EINVAL; | |
2407 | ||
2408 | return 0; | |
2409 | } | |
2410 | ||
2411 | static int sony_check_add(struct sony_sc *sc) | |
2412 | { | |
1adf904e | 2413 | u8 *buf = NULL; |
d2d782fc FP |
2414 | int n, ret; |
2415 | ||
2416 | if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) || | |
12e9a6d7 | 2417 | (sc->quirks & MOTION_CONTROLLER_BT) || |
4545ee0a | 2418 | (sc->quirks & NAVIGATION_CONTROLLER_BT) || |
d2d782fc FP |
2419 | (sc->quirks & SIXAXIS_CONTROLLER_BT)) { |
2420 | /* | |
2421 | * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC | |
2422 | * address from the uniq string where HIDP stores it. | |
2423 | * As uniq cannot be guaranteed to be a MAC address in all cases | |
2424 | * a failure of this function should not prevent the connection. | |
2425 | */ | |
2426 | if (sony_get_bt_devaddr(sc) < 0) { | |
2427 | hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n"); | |
2428 | return 0; | |
2429 | } | |
35f436c3 | 2430 | } else if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) { |
2c159de0 | 2431 | buf = kmalloc(DS4_FEATURE_REPORT_0x81_SIZE, GFP_KERNEL); |
9b2b5c9a FP |
2432 | if (!buf) |
2433 | return -ENOMEM; | |
d2d782fc FP |
2434 | |
2435 | /* | |
2436 | * The MAC address of a DS4 controller connected via USB can be | |
2437 | * retrieved with feature report 0x81. The address begins at | |
2438 | * offset 1. | |
2439 | */ | |
9b2b5c9a | 2440 | ret = hid_hw_raw_request(sc->hdev, 0x81, buf, |
2c159de0 | 2441 | DS4_FEATURE_REPORT_0x81_SIZE, HID_FEATURE_REPORT, |
9b2b5c9a | 2442 | HID_REQ_GET_REPORT); |
d2d782fc | 2443 | |
2c159de0 | 2444 | if (ret != DS4_FEATURE_REPORT_0x81_SIZE) { |
d2d782fc | 2445 | hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n"); |
9b2b5c9a FP |
2446 | ret = ret < 0 ? ret : -EINVAL; |
2447 | goto out_free; | |
d2d782fc FP |
2448 | } |
2449 | ||
2450 | memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address)); | |
c70d5f70 RC |
2451 | |
2452 | snprintf(sc->hdev->uniq, sizeof(sc->hdev->uniq), | |
648d4932 | 2453 | "%pMR", sc->mac_address); |
4545ee0a SW |
2454 | } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) || |
2455 | (sc->quirks & NAVIGATION_CONTROLLER_USB)) { | |
9b2b5c9a FP |
2456 | buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL); |
2457 | if (!buf) | |
2458 | return -ENOMEM; | |
d2d782fc FP |
2459 | |
2460 | /* | |
2461 | * The MAC address of a Sixaxis controller connected via USB can | |
2462 | * be retrieved with feature report 0xf2. The address begins at | |
2463 | * offset 4. | |
2464 | */ | |
9b2b5c9a FP |
2465 | ret = hid_hw_raw_request(sc->hdev, 0xf2, buf, |
2466 | SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT, | |
2467 | HID_REQ_GET_REPORT); | |
d2d782fc | 2468 | |
9b2b5c9a | 2469 | if (ret != SIXAXIS_REPORT_0xF2_SIZE) { |
d2d782fc | 2470 | hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n"); |
9b2b5c9a FP |
2471 | ret = ret < 0 ? ret : -EINVAL; |
2472 | goto out_free; | |
d2d782fc FP |
2473 | } |
2474 | ||
2475 | /* | |
2476 | * The Sixaxis device MAC in the report is big-endian and must | |
2477 | * be byte-swapped. | |
2478 | */ | |
2479 | for (n = 0; n < 6; n++) | |
2480 | sc->mac_address[5-n] = buf[4+n]; | |
5a144be3 RC |
2481 | |
2482 | snprintf(sc->hdev->uniq, sizeof(sc->hdev->uniq), | |
648d4932 | 2483 | "%pMR", sc->mac_address); |
d2d782fc FP |
2484 | } else { |
2485 | return 0; | |
2486 | } | |
2487 | ||
9b2b5c9a FP |
2488 | ret = sony_check_add_dev_list(sc); |
2489 | ||
2490 | out_free: | |
2491 | ||
2492 | kfree(buf); | |
2493 | ||
2494 | return ret; | |
d2d782fc FP |
2495 | } |
2496 | ||
8025087a FP |
2497 | static int sony_set_device_id(struct sony_sc *sc) |
2498 | { | |
2499 | int ret; | |
2500 | ||
2501 | /* | |
2502 | * Only DualShock 4 or Sixaxis controllers get an id. | |
2503 | * All others are set to -1. | |
2504 | */ | |
2505 | if ((sc->quirks & SIXAXIS_CONTROLLER) || | |
2506 | (sc->quirks & DUALSHOCK4_CONTROLLER)) { | |
2507 | ret = ida_simple_get(&sony_device_id_allocator, 0, 0, | |
2508 | GFP_KERNEL); | |
2509 | if (ret < 0) { | |
2510 | sc->device_id = -1; | |
2511 | return ret; | |
2512 | } | |
2513 | sc->device_id = ret; | |
2514 | } else { | |
2515 | sc->device_id = -1; | |
2516 | } | |
2517 | ||
2518 | return 0; | |
2519 | } | |
2520 | ||
2521 | static void sony_release_device_id(struct sony_sc *sc) | |
2522 | { | |
2523 | if (sc->device_id >= 0) { | |
2524 | ida_simple_remove(&sony_device_id_allocator, sc->device_id); | |
2525 | sc->device_id = -1; | |
2526 | } | |
2527 | } | |
2528 | ||
d8aaccda | 2529 | static inline void sony_init_output_report(struct sony_sc *sc, |
09593e38 | 2530 | void (*send_output_report)(struct sony_sc *)) |
46262047 | 2531 | { |
d8aaccda FP |
2532 | sc->send_output_report = send_output_report; |
2533 | ||
b5322736 | 2534 | if (!sc->state_worker_initialized) |
d8aaccda | 2535 | INIT_WORK(&sc->state_worker, sony_state_worker); |
46262047 | 2536 | |
b5322736 | 2537 | sc->state_worker_initialized = 1; |
46262047 FP |
2538 | } |
2539 | ||
2540 | static inline void sony_cancel_work_sync(struct sony_sc *sc) | |
2541 | { | |
f2f47c38 RC |
2542 | if (sc->hotplug_worker_initialized) |
2543 | cancel_work_sync(&sc->hotplug_worker); | |
b5322736 | 2544 | if (sc->state_worker_initialized) |
46262047 FP |
2545 | cancel_work_sync(&sc->state_worker); |
2546 | } | |
d2d782fc | 2547 | |
77b499e7 | 2548 | |
e1bc84d0 RC |
2549 | static int sony_input_configured(struct hid_device *hdev, |
2550 | struct hid_input *hidinput) | |
bd28ce00 | 2551 | { |
e1bc84d0 | 2552 | struct sony_sc *sc = hid_get_drvdata(hdev); |
0f398230 | 2553 | int append_dev_id; |
e1bc84d0 | 2554 | int ret; |
bd28ce00 | 2555 | |
131a8a9a | 2556 | ret = sony_set_device_id(sc); |
9b2b5c9a | 2557 | if (ret < 0) { |
131a8a9a | 2558 | hid_err(hdev, "failed to allocate the device id\n"); |
9b2b5c9a FP |
2559 | goto err_stop; |
2560 | } | |
2561 | ||
df848bc0 RC |
2562 | ret = append_dev_id = sony_check_add(sc); |
2563 | if (ret < 0) | |
2564 | goto err_stop; | |
2565 | ||
131a8a9a | 2566 | ret = sony_allocate_output_report(sc); |
8025087a | 2567 | if (ret < 0) { |
131a8a9a | 2568 | hid_err(hdev, "failed to allocate the output report buffer\n"); |
8025087a FP |
2569 | goto err_stop; |
2570 | } | |
2571 | ||
510c8b7c | 2572 | if (sc->quirks & NAVIGATION_CONTROLLER_USB) { |
e534a935 BT |
2573 | /* |
2574 | * The Sony Sixaxis does not handle HID Output Reports on the | |
2575 | * Interrupt EP like it could, so we need to force HID Output | |
2576 | * Reports to use HID_REQ_SET_REPORT on the Control EP. | |
2577 | * | |
2578 | * There is also another issue about HID Output Reports via USB, | |
2579 | * the Sixaxis does not want the report_id as part of the data | |
2580 | * packet, so we have to discard buf[0] when sending the actual | |
2581 | * control message, even for numbered reports, humpf! | |
2a242932 FP |
2582 | * |
2583 | * Additionally, the Sixaxis on USB isn't properly initialized | |
2584 | * until the PS logo button is pressed and as such won't retain | |
2585 | * any state set by an output report, so the initial | |
2586 | * configuration report is deferred until the first input | |
2587 | * report arrives. | |
e534a935 BT |
2588 | */ |
2589 | hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP; | |
2590 | hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID; | |
2a242932 | 2591 | sc->defer_initialization = 1; |
80ecc48c | 2592 | |
816651a7 | 2593 | ret = sixaxis_set_operational_usb(hdev); |
80ecc48c RC |
2594 | if (ret < 0) { |
2595 | hid_err(hdev, "Failed to set controller into operational mode\n"); | |
2596 | goto err_stop; | |
2597 | } | |
2598 | ||
d8aaccda | 2599 | sony_init_output_report(sc, sixaxis_send_output_report); |
510c8b7c RC |
2600 | } else if (sc->quirks & NAVIGATION_CONTROLLER_BT) { |
2601 | /* | |
2602 | * The Navigation controller wants output reports sent on the ctrl | |
2603 | * endpoint when connected via Bluetooth. | |
2604 | */ | |
2605 | hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP; | |
2606 | ||
2607 | ret = sixaxis_set_operational_bt(hdev); | |
2608 | if (ret < 0) { | |
2609 | hid_err(hdev, "Failed to set controller into operational mode\n"); | |
2610 | goto err_stop; | |
2611 | } | |
2612 | ||
d8aaccda | 2613 | sony_init_output_report(sc, sixaxis_send_output_report); |
510c8b7c RC |
2614 | } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) { |
2615 | /* | |
2616 | * The Sony Sixaxis does not handle HID Output Reports on the | |
2617 | * Interrupt EP and the device only becomes active when the | |
2618 | * PS button is pressed. See comment for Navigation controller | |
2619 | * above for more details. | |
2620 | */ | |
2621 | hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP; | |
2622 | hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID; | |
2623 | sc->defer_initialization = 1; | |
2624 | ||
2625 | ret = sixaxis_set_operational_usb(hdev); | |
2626 | if (ret < 0) { | |
2627 | hid_err(hdev, "Failed to set controller into operational mode\n"); | |
2628 | goto err_stop; | |
2629 | } | |
2630 | ||
2631 | ret = sony_register_sensors(sc); | |
2632 | if (ret) { | |
2633 | hid_err(sc->hdev, | |
2634 | "Unable to initialize motion sensors: %d\n", ret); | |
2635 | goto err_stop; | |
2636 | } | |
2637 | ||
2638 | sony_init_output_report(sc, sixaxis_send_output_report); | |
2639 | } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) { | |
2078b9bb FP |
2640 | /* |
2641 | * The Sixaxis wants output reports sent on the ctrl endpoint | |
2642 | * when connected via Bluetooth. | |
2643 | */ | |
2644 | hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP; | |
80ecc48c | 2645 | |
816651a7 | 2646 | ret = sixaxis_set_operational_bt(hdev); |
80ecc48c RC |
2647 | if (ret < 0) { |
2648 | hid_err(hdev, "Failed to set controller into operational mode\n"); | |
2649 | goto err_stop; | |
2650 | } | |
2651 | ||
510c8b7c RC |
2652 | ret = sony_register_sensors(sc); |
2653 | if (ret) { | |
2654 | hid_err(sc->hdev, | |
2655 | "Unable to initialize motion sensors: %d\n", ret); | |
2656 | goto err_stop; | |
2657 | } | |
2658 | ||
d8aaccda | 2659 | sony_init_output_report(sc, sixaxis_send_output_report); |
fee4e2d5 | 2660 | } else if (sc->quirks & DUALSHOCK4_CONTROLLER) { |
55a07d62 RC |
2661 | ret = dualshock4_get_calibration_data(sc); |
2662 | if (ret < 0) { | |
2663 | hid_err(hdev, "Failed to get calibration data from Dualshock 4\n"); | |
2664 | goto err_stop; | |
68330d83 | 2665 | } |
c4e1ddf2 | 2666 | |
169f15ab RC |
2667 | ret = dualshock4_get_version_info(sc); |
2668 | if (ret < 0) { | |
2669 | hid_err(sc->hdev, "Failed to get version data from Dualshock 4\n"); | |
2670 | goto err_stop; | |
2671 | } | |
2672 | ||
2673 | ret = device_create_file(&sc->hdev->dev, &dev_attr_firmware_version); | |
2674 | if (ret) { | |
2675 | /* Make zero for cleanup reasons of sysfs entries. */ | |
2676 | sc->fw_version = 0; | |
2677 | sc->hw_version = 0; | |
2678 | hid_err(sc->hdev, "can't create sysfs firmware_version attribute err: %d\n", ret); | |
2679 | goto err_stop; | |
2680 | } | |
2681 | ||
2682 | ret = device_create_file(&sc->hdev->dev, &dev_attr_hardware_version); | |
2683 | if (ret) { | |
2684 | sc->hw_version = 0; | |
2685 | hid_err(sc->hdev, "can't create sysfs hardware_version attribute err: %d\n", ret); | |
2686 | goto err_stop; | |
2687 | } | |
2688 | ||
e1bc84d0 RC |
2689 | /* |
2690 | * The Dualshock 4 touchpad supports 2 touches and has a | |
2691 | * resolution of 1920x942 (44.86 dots/mm). | |
2692 | */ | |
ac797b95 | 2693 | ret = sony_register_touchpad(sc, 2, 1920, 942); |
e1bc84d0 RC |
2694 | if (ret) { |
2695 | hid_err(sc->hdev, | |
2696 | "Unable to initialize multi-touch slots: %d\n", | |
2697 | ret); | |
2b6579d4 | 2698 | goto err_stop; |
e1bc84d0 RC |
2699 | } |
2700 | ||
227c011b RC |
2701 | ret = sony_register_sensors(sc); |
2702 | if (ret) { | |
2703 | hid_err(sc->hdev, | |
2704 | "Unable to initialize motion sensors: %d\n", ret); | |
2705 | goto err_stop; | |
2706 | } | |
2707 | ||
77b499e7 RC |
2708 | if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) { |
2709 | sc->ds4_bt_poll_interval = DS4_BT_DEFAULT_POLL_INTERVAL_MS; | |
2710 | ret = device_create_file(&sc->hdev->dev, &dev_attr_bt_poll_interval); | |
2711 | if (ret) | |
2712 | hid_warn(sc->hdev, | |
2713 | "can't create sysfs bt_poll_interval attribute err: %d\n", | |
2714 | ret); | |
2715 | } | |
2716 | ||
f2f47c38 RC |
2717 | if (sc->quirks & DUALSHOCK4_DONGLE) { |
2718 | INIT_WORK(&sc->hotplug_worker, dualshock4_calibration_work); | |
2719 | sc->hotplug_worker_initialized = 1; | |
2720 | sc->ds4_dongle_state = DONGLE_DISCONNECTED; | |
2721 | } | |
2722 | ||
d8aaccda | 2723 | sony_init_output_report(sc, dualshock4_send_output_report); |
c5e0c1c4 | 2724 | } else if (sc->quirks & MOTION_CONTROLLER) { |
d8aaccda | 2725 | sony_init_output_report(sc, motion_send_output_report); |
0bd88dd3 FP |
2726 | } else { |
2727 | ret = 0; | |
2728 | } | |
f9ce7c28 | 2729 | |
0a286ef2 | 2730 | if (sc->quirks & SONY_LED_SUPPORT) { |
fa57a810 | 2731 | ret = sony_leds_init(sc); |
0a286ef2 SE |
2732 | if (ret < 0) |
2733 | goto err_stop; | |
2734 | } | |
2735 | ||
d902f472 | 2736 | if (sc->quirks & SONY_BATTERY_SUPPORT) { |
0f398230 | 2737 | ret = sony_battery_probe(sc, append_dev_id); |
d902f472 FP |
2738 | if (ret < 0) |
2739 | goto err_stop; | |
2740 | ||
2741 | /* Open the device to receive reports with battery info */ | |
2742 | ret = hid_hw_open(hdev); | |
2743 | if (ret < 0) { | |
2744 | hid_err(hdev, "hw open failed\n"); | |
2745 | goto err_stop; | |
2746 | } | |
2747 | } | |
2748 | ||
c8de9dbb | 2749 | if (sc->quirks & SONY_FF_SUPPORT) { |
fa57a810 | 2750 | ret = sony_init_ff(sc); |
c8de9dbb FP |
2751 | if (ret < 0) |
2752 | goto err_close; | |
5f5750d2 | 2753 | } |
a08c22c0 | 2754 | |
bd28ce00 | 2755 | return 0; |
d902f472 FP |
2756 | err_close: |
2757 | hid_hw_close(hdev); | |
bd28ce00 | 2758 | err_stop: |
77b499e7 RC |
2759 | /* Piggy back on the default ds4_bt_ poll_interval to determine |
2760 | * if we need to remove the file as we don't know for sure if we | |
2761 | * executed that logic. | |
2762 | */ | |
2763 | if (sc->ds4_bt_poll_interval) | |
2764 | device_remove_file(&sc->hdev->dev, &dev_attr_bt_poll_interval); | |
169f15ab RC |
2765 | if (sc->fw_version) |
2766 | device_remove_file(&sc->hdev->dev, &dev_attr_firmware_version); | |
2767 | if (sc->hw_version) | |
2768 | device_remove_file(&sc->hdev->dev, &dev_attr_hardware_version); | |
0a286ef2 | 2769 | if (sc->quirks & SONY_LED_SUPPORT) |
fa57a810 | 2770 | sony_leds_remove(sc); |
d902f472 FP |
2771 | if (sc->quirks & SONY_BATTERY_SUPPORT) |
2772 | sony_battery_remove(sc); | |
a687c576 RC |
2773 | if (sc->touchpad) |
2774 | sony_unregister_touchpad(sc); | |
a676bdc4 RC |
2775 | if (sc->sensor_dev) |
2776 | sony_unregister_sensors(sc); | |
46262047 | 2777 | sony_cancel_work_sync(sc); |
9b2b5c9a | 2778 | kfree(sc->output_report_dmabuf); |
d2d782fc | 2779 | sony_remove_dev_list(sc); |
8025087a | 2780 | sony_release_device_id(sc); |
bd28ce00 | 2781 | hid_hw_stop(hdev); |
bd28ce00 JS |
2782 | return ret; |
2783 | } | |
2784 | ||
e1bc84d0 RC |
2785 | static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id) |
2786 | { | |
2787 | int ret; | |
2788 | unsigned long quirks = id->driver_data; | |
2789 | struct sony_sc *sc; | |
2790 | unsigned int connect_mask = HID_CONNECT_DEFAULT; | |
2791 | ||
2792 | if (!strcmp(hdev->name, "FutureMax Dance Mat")) | |
2793 | quirks |= FUTUREMAX_DANCE_MAT; | |
2794 | ||
2795 | sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL); | |
2796 | if (sc == NULL) { | |
2797 | hid_err(hdev, "can't alloc sony descriptor\n"); | |
2798 | return -ENOMEM; | |
2799 | } | |
2800 | ||
2801 | spin_lock_init(&sc->lock); | |
2802 | ||
2803 | sc->quirks = quirks; | |
2804 | hid_set_drvdata(hdev, sc); | |
2805 | sc->hdev = hdev; | |
2806 | ||
2807 | ret = hid_parse(hdev); | |
2808 | if (ret) { | |
2809 | hid_err(hdev, "parse failed\n"); | |
2810 | return ret; | |
2811 | } | |
2812 | ||
2813 | if (sc->quirks & VAIO_RDESC_CONSTANT) | |
2814 | connect_mask |= HID_CONNECT_HIDDEV_FORCE; | |
2815 | else if (sc->quirks & SIXAXIS_CONTROLLER) | |
2816 | connect_mask |= HID_CONNECT_HIDDEV_FORCE; | |
2817 | ||
e19a267b | 2818 | /* Patch the hw version on DS3/4 compatible devices, so applications can |
9131f8cc RC |
2819 | * distinguish between the default HID mappings and the mappings defined |
2820 | * by the Linux game controller spec. This is important for the SDL2 | |
2821 | * library, which has a game controller database, which uses device ids | |
2822 | * in combination with version as a key. | |
2823 | */ | |
e19a267b | 2824 | if (sc->quirks & (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER)) |
9131f8cc RC |
2825 | hdev->version |= 0x8000; |
2826 | ||
e1bc84d0 RC |
2827 | ret = hid_hw_start(hdev, connect_mask); |
2828 | if (ret) { | |
2829 | hid_err(hdev, "hw start failed\n"); | |
2830 | return ret; | |
2831 | } | |
2832 | ||
4f967f6d RC |
2833 | /* sony_input_configured can fail, but this doesn't result |
2834 | * in hid_hw_start failures (intended). Check whether | |
2835 | * the HID layer claimed the device else fail. | |
2836 | * We don't know the actual reason for the failure, most | |
2837 | * likely it is due to EEXIST in case of double connection | |
2838 | * of USB and Bluetooth, but could have been due to ENOMEM | |
2839 | * or other reasons as well. | |
2840 | */ | |
2841 | if (!(hdev->claimed & HID_CLAIMED_INPUT)) { | |
2842 | hid_err(hdev, "failed to claim input\n"); | |
2843 | return -ENODEV; | |
2844 | } | |
2845 | ||
e1bc84d0 RC |
2846 | return ret; |
2847 | } | |
2848 | ||
cc6e0bbb JK |
2849 | static void sony_remove(struct hid_device *hdev) |
2850 | { | |
f04d5140 CL |
2851 | struct sony_sc *sc = hid_get_drvdata(hdev); |
2852 | ||
ac797b95 RC |
2853 | hid_hw_close(hdev); |
2854 | ||
0a286ef2 | 2855 | if (sc->quirks & SONY_LED_SUPPORT) |
fa57a810 | 2856 | sony_leds_remove(sc); |
f04d5140 | 2857 | |
ac797b95 | 2858 | if (sc->quirks & SONY_BATTERY_SUPPORT) |
d902f472 | 2859 | sony_battery_remove(sc); |
ac797b95 RC |
2860 | |
2861 | if (sc->touchpad) | |
2862 | sony_unregister_touchpad(sc); | |
d902f472 | 2863 | |
227c011b RC |
2864 | if (sc->sensor_dev) |
2865 | sony_unregister_sensors(sc); | |
2866 | ||
77b499e7 RC |
2867 | if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) |
2868 | device_remove_file(&sc->hdev->dev, &dev_attr_bt_poll_interval); | |
2869 | ||
169f15ab RC |
2870 | if (sc->fw_version) |
2871 | device_remove_file(&sc->hdev->dev, &dev_attr_firmware_version); | |
2872 | ||
2873 | if (sc->hw_version) | |
2874 | device_remove_file(&sc->hdev->dev, &dev_attr_hardware_version); | |
2875 | ||
46262047 | 2876 | sony_cancel_work_sync(sc); |
9f323b68 | 2877 | |
9b2b5c9a FP |
2878 | kfree(sc->output_report_dmabuf); |
2879 | ||
d2d782fc | 2880 | sony_remove_dev_list(sc); |
9f323b68 | 2881 | |
8025087a FP |
2882 | sony_release_device_id(sc); |
2883 | ||
cc6e0bbb | 2884 | hid_hw_stop(hdev); |
cc6e0bbb JK |
2885 | } |
2886 | ||
decd946c FP |
2887 | #ifdef CONFIG_PM |
2888 | ||
2889 | static int sony_suspend(struct hid_device *hdev, pm_message_t message) | |
2890 | { | |
decd946c | 2891 | #ifdef CONFIG_SONY_FF |
decd946c | 2892 | |
765a1077 FP |
2893 | /* On suspend stop any running force-feedback events */ |
2894 | if (SONY_FF_SUPPORT) { | |
2895 | struct sony_sc *sc = hid_get_drvdata(hdev); | |
decd946c | 2896 | |
765a1077 | 2897 | sc->left = sc->right = 0; |
decd946c FP |
2898 | sony_send_output_report(sc); |
2899 | } | |
2900 | ||
765a1077 | 2901 | #endif |
decd946c FP |
2902 | return 0; |
2903 | } | |
2904 | ||
2905 | static int sony_resume(struct hid_device *hdev) | |
2906 | { | |
765a1077 | 2907 | struct sony_sc *sc = hid_get_drvdata(hdev); |
decd946c | 2908 | |
765a1077 FP |
2909 | /* |
2910 | * The Sixaxis and navigation controllers on USB need to be | |
2911 | * reinitialized on resume or they won't behave properly. | |
2912 | */ | |
2913 | if ((sc->quirks & SIXAXIS_CONTROLLER_USB) || | |
2914 | (sc->quirks & NAVIGATION_CONTROLLER_USB)) { | |
2915 | sixaxis_set_operational_usb(sc->hdev); | |
2916 | sc->defer_initialization = 1; | |
decd946c FP |
2917 | } |
2918 | ||
2919 | return 0; | |
2920 | } | |
2921 | ||
2922 | #endif | |
2923 | ||
bd28ce00 | 2924 | static const struct hid_device_id sony_devices[] = { |
816651a7 AO |
2925 | { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER), |
2926 | .driver_data = SIXAXIS_CONTROLLER_USB }, | |
35dca5b4 | 2927 | { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER), |
4545ee0a | 2928 | .driver_data = NAVIGATION_CONTROLLER_USB }, |
6eabaaa0 | 2929 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER), |
4545ee0a | 2930 | .driver_data = NAVIGATION_CONTROLLER_BT }, |
c5e0c1c4 | 2931 | { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER), |
b3bca326 | 2932 | .driver_data = MOTION_CONTROLLER_USB }, |
a4afa854 | 2933 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER), |
b3bca326 | 2934 | .driver_data = MOTION_CONTROLLER_BT }, |
816651a7 AO |
2935 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER), |
2936 | .driver_data = SIXAXIS_CONTROLLER_BT }, | |
cc6e0bbb JK |
2937 | { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE), |
2938 | .driver_data = VAIO_RDESC_CONSTANT }, | |
a4649184 FLVC |
2939 | { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE), |
2940 | .driver_data = VAIO_RDESC_CONSTANT }, | |
ef916ef5 AO |
2941 | /* |
2942 | * Wired Buzz Controller. Reported as Sony Hub from its USB ID and as | |
2943 | * Logitech joystick from the device descriptor. | |
2944 | */ | |
f04d5140 CL |
2945 | { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER), |
2946 | .driver_data = BUZZ_CONTROLLER }, | |
2947 | { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER), | |
2948 | .driver_data = BUZZ_CONTROLLER }, | |
078328da JK |
2949 | /* PS3 BD Remote Control */ |
2950 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE), | |
2951 | .driver_data = PS3REMOTE }, | |
2952 | /* Logitech Harmony Adapter for PS3 */ | |
2953 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3), | |
2954 | .driver_data = PS3REMOTE }, | |
68a49e51 FP |
2955 | /* SMK-Link PS3 BD Remote Control */ |
2956 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE), | |
2957 | .driver_data = PS3REMOTE }, | |
0bd88dd3 FP |
2958 | /* Sony Dualshock 4 controllers for PS4 */ |
2959 | { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER), | |
8ab1676b | 2960 | .driver_data = DUALSHOCK4_CONTROLLER_USB }, |
0bd88dd3 | 2961 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER), |
8ab1676b | 2962 | .driver_data = DUALSHOCK4_CONTROLLER_BT }, |
cf1015d6 RC |
2963 | { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2), |
2964 | .driver_data = DUALSHOCK4_CONTROLLER_USB }, | |
2965 | { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2), | |
2966 | .driver_data = DUALSHOCK4_CONTROLLER_BT }, | |
de66a1a0 | 2967 | { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE), |
35f436c3 | 2968 | .driver_data = DUALSHOCK4_DONGLE }, |
74500cc8 SM |
2969 | /* Nyko Core Controller for PS3 */ |
2970 | { HID_USB_DEVICE(USB_VENDOR_ID_SINO_LITE, USB_DEVICE_ID_SINO_LITE_CONTROLLER), | |
2971 | .driver_data = SIXAXIS_CONTROLLER_USB | SINO_LITE_CONTROLLER }, | |
bd28ce00 JS |
2972 | { } |
2973 | }; | |
2974 | MODULE_DEVICE_TABLE(hid, sony_devices); | |
2975 | ||
2976 | static struct hid_driver sony_driver = { | |
ce8efc3b FP |
2977 | .name = "sony", |
2978 | .id_table = sony_devices, | |
2979 | .input_mapping = sony_mapping, | |
2980 | .input_configured = sony_input_configured, | |
2981 | .probe = sony_probe, | |
2982 | .remove = sony_remove, | |
2983 | .report_fixup = sony_report_fixup, | |
decd946c FP |
2984 | .raw_event = sony_raw_event, |
2985 | ||
2986 | #ifdef CONFIG_PM | |
2987 | .suspend = sony_suspend, | |
2988 | .resume = sony_resume, | |
2989 | .reset_resume = sony_resume, | |
2990 | #endif | |
bd28ce00 | 2991 | }; |
8025087a FP |
2992 | |
2993 | static int __init sony_init(void) | |
2994 | { | |
2995 | dbg_hid("Sony:%s\n", __func__); | |
2996 | ||
2997 | return hid_register_driver(&sony_driver); | |
2998 | } | |
2999 | ||
3000 | static void __exit sony_exit(void) | |
3001 | { | |
3002 | dbg_hid("Sony:%s\n", __func__); | |
3003 | ||
8025087a | 3004 | hid_unregister_driver(&sony_driver); |
6c40065f | 3005 | ida_destroy(&sony_device_id_allocator); |
8025087a FP |
3006 | } |
3007 | module_init(sony_init); | |
3008 | module_exit(sony_exit); | |
bd28ce00 | 3009 | |
bd28ce00 | 3010 | MODULE_LICENSE("GPL"); |