blob: f8fb8a61fb90e1b365515e7b69f25ba2a535c70d [file] [log] [blame]
Jiri Slabybd28ce02008-06-25 23:47:04 +02001/*
Frank Praznik077147a2014-09-14 11:56:39 -04002 * HID driver for Sony / PS2 / PS3 / PS4 BD devices.
Jiri Slabybd28ce02008-06-25 23:47:04 +02003 *
4 * Copyright (c) 1999 Andreas Gal
5 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
Jiri Slabybd28ce02008-06-25 23:47:04 +02007 * Copyright (c) 2008 Jiri Slaby
Jiri Kosina078328d2013-06-13 12:03:49 +02008 * Copyright (c) 2012 David Dillow <dave@thedillows.org>
9 * Copyright (c) 2006-2013 Jiri Kosina
Colin Leitnerf04d5142013-05-27 23:41:05 +020010 * Copyright (c) 2013 Colin Leitner <colin.leitner@gmail.com>
Frank Praznikc4425c82016-09-22 20:18:10 -040011 * Copyright (c) 2014-2016 Frank Praznik <frank.praznik@gmail.com>
Jiri Slabybd28ce02008-06-25 23:47:04 +020012 */
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
Frank Praznikad142b92014-02-20 11:36:00 -050021/*
22 * NOTE: in order for the Sony PS3 BD Remote Control to be found by
Jiri Kosina078328d2013-06-13 12:03:49 +020023 * 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
Jiri Slabybd28ce02008-06-25 23:47:04 +020029#include <linux/device.h>
30#include <linux/hid.h>
31#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jiri Kosina40e32ee2013-05-28 11:22:09 +020033#include <linux/leds.h>
Frank Praznikd902f472014-01-27 10:17:36 -050034#include <linux/power_supply.h>
35#include <linux/spinlock.h>
Frank Praznikd2d782f2014-02-20 11:36:03 -050036#include <linux/list.h>
Frank Praznik80250872014-04-14 10:11:35 -040037#include <linux/idr.h>
Frank Praznike5606232014-01-27 10:17:37 -050038#include <linux/input/mt.h>
Roderick Colenbrander6dfd5bf2016-10-07 12:39:36 -070039#include <linux/crc32.h>
40#include <asm/unaligned.h>
Jiri Slabybd28ce02008-06-25 23:47:04 +020041
42#include "hid-ids.h"
43
Frank Praznik6c79c182014-01-16 21:43:03 -050044#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)
Frank Praznik8ab16762014-01-16 21:42:31 -050049#define DUALSHOCK4_CONTROLLER_USB BIT(5)
50#define DUALSHOCK4_CONTROLLER_BT BIT(6)
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -080051#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)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +020058
Frank Praznikfee4e2d2014-02-18 17:22:01 -050059#define SIXAXIS_CONTROLLER (SIXAXIS_CONTROLLER_USB | SIXAXIS_CONTROLLER_BT)
Simon Woodb3bca322015-06-09 21:27:04 -060060#define MOTION_CONTROLLER (MOTION_CONTROLLER_USB | MOTION_CONTROLLER_BT)
Simon Wood4545ee02015-06-17 00:08:52 -060061#define NAVIGATION_CONTROLLER (NAVIGATION_CONTROLLER_USB |\
62 NAVIGATION_CONTROLLER_BT)
Frank Praznik68330d82014-02-05 20:03:49 -050063#define DUALSHOCK4_CONTROLLER (DUALSHOCK4_CONTROLLER_USB |\
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -080064 DUALSHOCK4_CONTROLLER_BT | \
65 DUALSHOCK4_DONGLE)
Frank Praznikfee4e2d2014-02-18 17:22:01 -050066#define SONY_LED_SUPPORT (SIXAXIS_CONTROLLER | BUZZ_CONTROLLER |\
Simon Wood4545ee02015-06-17 00:08:52 -060067 DUALSHOCK4_CONTROLLER | MOTION_CONTROLLER |\
68 NAVIGATION_CONTROLLER)
Simon Wood12e9a6d2015-06-09 21:27:05 -060069#define SONY_BATTERY_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
Simon Wood4545ee02015-06-17 00:08:52 -060070 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040071#define SONY_FF_SUPPORT (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER |\
72 MOTION_CONTROLLER)
Frank Praznik0f398232016-09-22 20:18:08 -040073#define SONY_BT_DEVICE (SIXAXIS_CONTROLLER_BT | DUALSHOCK4_CONTROLLER_BT |\
74 MOTION_CONTROLLER_BT | NAVIGATION_CONTROLLER_BT)
Frank Praznik60781cf2014-01-11 15:13:15 -050075
76#define MAX_LEDS 4
Sven Eckelmann0a286ef2013-11-19 20:26:32 +010077
Mauro Carvalho Chehabe57a67d2012-12-14 20:57:34 -020078
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040079/* PS/3 Motion controller */
Pavel Machek1adf9042016-02-09 13:55:08 +010080static u8 motion_rdesc[] = {
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040081 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), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040086 0x75, 0x01, /* Report Size (1), */
Simon Wood8b2513c2015-06-09 21:27:07 -060087 0x95, 0x15, /* Report Count (21), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040088 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), */
Simon Wood8b2513c2015-06-09 21:27:07 -060094 0x29, 0x15, /* Usage Maximum (15h), */
95 0x81, 0x02, /* Input (Variable), * Buttons */
96 0x95, 0x0B, /* Report Count (11), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040097 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
Simon Wood8b2513c2015-06-09 21:27:07 -060098 0x81, 0x03, /* Input (Constant, Variable), * Padding */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -040099 0x15, 0x00, /* Logical Minimum (0), */
100 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
101 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400102 0xA1, 0x00, /* Collection (Physical), */
103 0x75, 0x08, /* Report Size (8), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600104 0x95, 0x01, /* Report Count (1), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400105 0x35, 0x00, /* Physical Minimum (0), */
106 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
107 0x09, 0x30, /* Usage (X), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600108 0x81, 0x02, /* Input (Variable), * Trigger */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400109 0xC0, /* End Collection, */
Simon Wood8b2513c2015-06-09 21:27:07 -0600110 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
111 0x75, 0x08, /* Report Size (8), */
112 0x95, 0x07, /* Report Count (7), * skip 7 bytes */
113 0x81, 0x02, /* Input (Variable), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400114 0x05, 0x01, /* Usage Page (Desktop), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400115 0x75, 0x10, /* Report Size (16), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600116 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), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400122 0x81, 0x02, /* Input (Variable), */
Simon Wood8b2513c2015-06-09 21:27:07 -0600123 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), */
127 0x09, 0x01, /* Usage (Pointer), */
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 */
132 0x81, 0x02, /* Input (Variable), */
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), */
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400151 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
Pavel Machek1adf9042016-02-09 13:55:08 +0100176static u8 ps3remote_rdesc[] = {
Jiri Kosina078328d2013-06-13 12:03:49 +0200177 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
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100184 /*
185 * Ignore the 1st byte, maybe it is used for a controller
186 * number but it's not needed for correct operation
187 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200188 0x75, 0x08, /* GReportSize 0x08 [8] */
189 0x95, 0x01, /* GReportCount 0x01 [1] */
190 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
191
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100192 /*
193 * Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
194 * buttons multiple keypresses are allowed
195 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200196 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
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100220 /*
221 * Ignore bytes from 6th to 11th, 6th to 10th are always constant at
222 * 0xff and 11th is for press indication
223 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200224 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
242static 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};
262static 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
Colin Leitnerf04d5142013-05-27 23:41:05 +0200298static const unsigned int buzz_keymap[] = {
Frank Praznikad142b92014-02-20 11:36:00 -0500299 /*
300 * The controller has 4 remote buzzers, each with one LED and 5
Colin Leitnerf04d5142013-05-27 23:41:05 +0200301 * buttons.
Antonio Ospite09593e32016-02-09 13:55:06 +0100302 *
Colin Leitnerf04d5142013-05-27 23:41:05 +0200303 * 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 */
Antonio Ospite09593e32016-02-09 13:55:06 +0100316 [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,
Colin Leitnerf04d5142013-05-27 23:41:05 +0200325 [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
Roderick Colenbrander41deb922017-03-24 15:17:46 -0700338/* 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 */
343static 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 */
352static 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
Roderick Colenbranderbe985302017-03-07 15:45:08 -0800372static 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
379static 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
Roderick Colenbrander687aa022016-11-23 14:07:08 -0800399static 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
408static 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
Roderick Colenbrander8621b7b2017-03-07 15:45:03 -0800424static 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};
Roderick Colenbrander687aa022016-11-23 14:07:08 -0800428
Frank Praznikd902f472014-01-27 10:17:36 -0500429static 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
Frank Praznik55d3b662014-04-14 10:11:32 -0400436struct sixaxis_led {
Pavel Machek1adf9042016-02-09 13:55:08 +0100437 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%) */
Frank Praznik55d3b662014-04-14 10:11:32 -0400442} __packed;
443
444struct sixaxis_rumble {
Pavel Machek1adf9042016-02-09 13:55:08 +0100445 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 */
Frank Praznik55d3b662014-04-14 10:11:32 -0400450} __packed;
451
452struct sixaxis_output_report {
Pavel Machek1adf9042016-02-09 13:55:08 +0100453 u8 report_id;
Frank Praznik55d3b662014-04-14 10:11:32 -0400454 struct sixaxis_rumble rumble;
Pavel Machek1adf9042016-02-09 13:55:08 +0100455 u8 padding[4];
456 u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
Frank Praznik55d3b662014-04-14 10:11:32 -0400457 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
458 struct sixaxis_led _reserved; /* LED5, not actually soldered */
459} __packed;
460
461union sixaxis_output_report_01 {
462 struct sixaxis_output_report data;
Pavel Machek1adf9042016-02-09 13:55:08 +0100463 u8 buf[36];
Frank Praznik55d3b662014-04-14 10:11:32 -0400464};
465
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400466struct motion_output_report_02 {
467 u8 type, zero;
468 u8 r, g, b;
469 u8 zero2;
470 u8 rumble;
471};
472
Roderick Colenbrander5443a632016-10-07 12:39:35 -0700473#define DS4_FEATURE_REPORT_0x02_SIZE 37
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -0800474#define DS4_FEATURE_REPORT_0x05_SIZE 41
Roderick Colenbrander5443a632016-10-07 12:39:35 -0700475#define DS4_FEATURE_REPORT_0x81_SIZE 7
Roderick Colenbrander6dfd5bf2016-10-07 12:39:36 -0700476#define DS4_INPUT_REPORT_0x11_SIZE 78
Roderick Colenbrander5443a632016-10-07 12:39:35 -0700477#define DS4_OUTPUT_REPORT_0x05_SIZE 32
478#define DS4_OUTPUT_REPORT_0x11_SIZE 78
Antonio Ospite29b691a2015-02-16 18:12:21 +0100479#define SIXAXIS_REPORT_0xF2_SIZE 17
Antonio Ospitea85d67b2015-02-16 18:12:22 +0100480#define SIXAXIS_REPORT_0xF5_SIZE 8
Simon Wood41d2d422015-06-09 21:27:06 -0600481#define MOTION_REPORT_0x02_SIZE 49
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500482
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -0700483/* Offsets relative to USB input report (0x1). Bluetooth (0x11) requires an
484 * additional +2.
485 */
Roderick Colenbrander8621b7b2017-03-07 15:45:03 -0800486#define DS4_INPUT_REPORT_AXIS_OFFSET 1
Roderick Colenbrander42027ee2016-11-23 14:07:07 -0800487#define DS4_INPUT_REPORT_BUTTON_OFFSET 5
Roderick Colenbranderf79133c2017-03-07 15:45:02 -0800488#define DS4_INPUT_REPORT_TIMESTAMP_OFFSET 10
Roderick Colenbrander6f561e92017-03-07 15:45:00 -0800489#define DS4_INPUT_REPORT_GYRO_X_OFFSET 13
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -0700490#define DS4_INPUT_REPORT_BATTERY_OFFSET 30
491#define DS4_INPUT_REPORT_TOUCHPAD_OFFSET 33
492
Roderick Colenbrander89e9ccd2017-03-07 15:45:10 -0800493#define SENSOR_SUFFIX " Motion Sensors"
Roderick Colenbrander42027ee2016-11-23 14:07:07 -0800494#define DS4_TOUCHPAD_SUFFIX " Touchpad"
495
Roderick Colenbrander786bd402017-03-24 15:17:49 -0700496/* Default to 4ms poll interval, which is same as USB (not adjustable). */
497#define DS4_BT_DEFAULT_POLL_INTERVAL_MS 4
498#define DS4_BT_MAX_POLL_INTERVAL_MS 62
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -0800499#define DS4_GYRO_RES_PER_DEG_S 1024
500#define DS4_ACC_RES_PER_G 8192
501
Roderick Colenbrander89e9ccd2017-03-07 15:45:10 -0800502#define SIXAXIS_INPUT_REPORT_ACC_X_OFFSET 41
503#define SIXAXIS_ACC_RES_PER_G 113
504
Jiri Kosina8b402c92015-02-23 11:15:44 +0100505static DEFINE_SPINLOCK(sony_dev_list_lock);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500506static LIST_HEAD(sony_device_list);
Frank Praznik80250872014-04-14 10:11:35 -0400507static DEFINE_IDA(sony_device_id_allocator);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500508
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -0800509/* Used for calibration of DS4 accelerometer and gyro. */
510struct ds4_calibration_data {
511 int abs_code;
512 short bias;
513 /* Calibration requires scaling against a sensitivity value, which is a
514 * float. Store sensitivity as a fraction to limit floating point
515 * calculations until final calibration.
516 */
517 int sens_numer;
518 int sens_denom;
519};
520
Roderick Colenbrander72dbb852017-03-07 15:45:06 -0800521enum ds4_dongle_state {
522 DONGLE_DISCONNECTED,
523 DONGLE_CALIBRATING,
524 DONGLE_CONNECTED,
525 DONGLE_DISABLED
526};
527
Roderick Colenbrander3a60a132017-03-07 15:45:05 -0800528enum sony_worker {
Roderick Colenbrander72dbb852017-03-07 15:45:06 -0800529 SONY_WORKER_STATE,
530 SONY_WORKER_HOTPLUG
Roderick Colenbrander3a60a132017-03-07 15:45:05 -0800531};
532
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200533struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500534 spinlock_t lock;
Frank Praznikd2d782f2014-02-20 11:36:03 -0500535 struct list_head list_node;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100536 struct hid_device *hdev;
Roderick Colenbrander42027ee2016-11-23 14:07:07 -0800537 struct input_dev *touchpad;
Roderick Colenbrander6f561e92017-03-07 15:45:00 -0800538 struct input_dev *sensor_dev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500539 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200540 unsigned long quirks;
Roderick Colenbrander72dbb852017-03-07 15:45:06 -0800541 struct work_struct hotplug_worker;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100542 struct work_struct state_worker;
Antonio Ospite09593e32016-02-09 13:55:06 +0100543 void (*send_output_report)(struct sony_sc *);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100544 struct power_supply *battery;
545 struct power_supply_desc battery_desc;
Frank Praznik80250872014-04-14 10:11:35 -0400546 int device_id;
Pavel Machek1adf9042016-02-09 13:55:08 +0100547 u8 *output_report_dmabuf;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200548
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100549#ifdef CONFIG_SONY_FF
Pavel Machek1adf9042016-02-09 13:55:08 +0100550 u8 left;
551 u8 right;
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100552#endif
553
Pavel Machek1adf9042016-02-09 13:55:08 +0100554 u8 mac_address[6];
Roderick Colenbrander72dbb852017-03-07 15:45:06 -0800555 u8 hotplug_worker_initialized;
Roderick Colenbrander3a60a132017-03-07 15:45:05 -0800556 u8 state_worker_initialized;
Frank Praznik2a242932016-09-22 20:18:09 -0400557 u8 defer_initialization;
Pavel Machek1adf9042016-02-09 13:55:08 +0100558 u8 cable_state;
559 u8 battery_charging;
560 u8 battery_capacity;
561 u8 led_state[MAX_LEDS];
Pavel Machek1adf9042016-02-09 13:55:08 +0100562 u8 led_delay_on[MAX_LEDS];
563 u8 led_delay_off[MAX_LEDS];
564 u8 led_count;
Roderick Colenbranderf79133c2017-03-07 15:45:02 -0800565
566 bool timestamp_initialized;
567 u16 prev_timestamp;
568 unsigned int timestamp_us;
569
Roderick Colenbrander786bd402017-03-24 15:17:49 -0700570 u8 ds4_bt_poll_interval;
Roderick Colenbrander72dbb852017-03-07 15:45:06 -0800571 enum ds4_dongle_state ds4_dongle_state;
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -0800572 /* DS4 calibration data */
573 struct ds4_calibration_data ds4_calib_data[6];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200574};
575
Roderick Colenbrander40e1f202016-12-08 19:09:52 -0800576static void sony_set_leds(struct sony_sc *sc);
577
Roderick Colenbrander3a60a132017-03-07 15:45:05 -0800578static inline void sony_schedule_work(struct sony_sc *sc,
579 enum sony_worker which)
Frank Praznik2a242932016-09-22 20:18:09 -0400580{
Roderick Colenbrander5bf3ad52019-08-02 14:27:28 -0700581 unsigned long flags;
582
Roderick Colenbrander3a60a132017-03-07 15:45:05 -0800583 switch (which) {
584 case SONY_WORKER_STATE:
Roderick Colenbrander5bf3ad52019-08-02 14:27:28 -0700585 spin_lock_irqsave(&sc->lock, flags);
586 if (!sc->defer_initialization && sc->state_worker_initialized)
Roderick Colenbrander3a60a132017-03-07 15:45:05 -0800587 schedule_work(&sc->state_worker);
Roderick Colenbrander5bf3ad52019-08-02 14:27:28 -0700588 spin_unlock_irqrestore(&sc->lock, flags);
Roderick Colenbrander72dbb852017-03-07 15:45:06 -0800589 break;
590 case SONY_WORKER_HOTPLUG:
591 if (sc->hotplug_worker_initialized)
592 schedule_work(&sc->hotplug_worker);
593 break;
Roderick Colenbrander3a60a132017-03-07 15:45:05 -0800594 }
Frank Praznik2a242932016-09-22 20:18:09 -0400595}
596
Roderick Colenbrander786bd402017-03-24 15:17:49 -0700597static ssize_t ds4_show_poll_interval(struct device *dev,
598 struct device_attribute
599 *attr, char *buf)
600{
601 struct hid_device *hdev = to_hid_device(dev);
602 struct sony_sc *sc = hid_get_drvdata(hdev);
603
604 return snprintf(buf, PAGE_SIZE, "%i\n", sc->ds4_bt_poll_interval);
605}
606
607static ssize_t ds4_store_poll_interval(struct device *dev,
608 struct device_attribute *attr,
609 const char *buf, size_t count)
610{
611 struct hid_device *hdev = to_hid_device(dev);
612 struct sony_sc *sc = hid_get_drvdata(hdev);
613 unsigned long flags;
614 u8 interval;
615
616 if (kstrtou8(buf, 0, &interval))
617 return -EINVAL;
618
619 if (interval > DS4_BT_MAX_POLL_INTERVAL_MS)
620 return -EINVAL;
621
622 spin_lock_irqsave(&sc->lock, flags);
623 sc->ds4_bt_poll_interval = interval;
624 spin_unlock_irqrestore(&sc->lock, flags);
625
626 sony_schedule_work(sc, SONY_WORKER_STATE);
627
628 return count;
629}
630
631static DEVICE_ATTR(bt_poll_interval, 0644, ds4_show_poll_interval,
632 ds4_store_poll_interval);
633
634
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400635static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
636 unsigned int *rsize)
637{
638 *rsize = sizeof(motion_rdesc);
639 return motion_rdesc;
640}
641
Pavel Machek1adf9042016-02-09 13:55:08 +0100642static u8 *ps3remote_fixup(struct hid_device *hdev, u8 *rdesc,
Jiri Kosina078328d2013-06-13 12:03:49 +0200643 unsigned int *rsize)
644{
645 *rsize = sizeof(ps3remote_rdesc);
646 return ps3remote_rdesc;
647}
648
649static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
650 struct hid_field *field, struct hid_usage *usage,
651 unsigned long **bit, int *max)
652{
653 unsigned int key = usage->hid & HID_USAGE;
654
655 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
656 return -1;
657
658 switch (usage->collection_index) {
659 case 1:
660 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
661 return -1;
662
663 key = ps3remote_keymap_joypad_buttons[key];
664 if (!key)
665 return -1;
666 break;
667 case 2:
668 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
669 return -1;
670
671 key = ps3remote_keymap_remote_buttons[key];
672 if (!key)
673 return -1;
674 break;
675 default:
676 return -1;
677 }
678
679 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
680 return 1;
681}
682
Roderick Colenbrander41deb922017-03-24 15:17:46 -0700683static int navigation_mapping(struct hid_device *hdev, struct hid_input *hi,
684 struct hid_field *field, struct hid_usage *usage,
685 unsigned long **bit, int *max)
686{
687 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
688 unsigned int key = usage->hid & HID_USAGE;
689
690 if (key >= ARRAY_SIZE(sixaxis_keymap))
691 return -1;
692
693 key = navigation_keymap[key];
694 if (!key)
695 return -1;
696
697 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
698 return 1;
699 } else if (usage->hid == HID_GD_POINTER) {
700 /* See comment in sixaxis_mapping, basically the L2 (and R2)
701 * triggers are reported through GD Pointer.
702 * In addition we ignore any analog button 'axes' and only
703 * support digital buttons.
704 */
705 switch (usage->usage_index) {
706 case 8: /* L2 */
707 usage->hid = HID_GD_Z;
708 break;
709 default:
710 return -1;
711 }
712
713 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, usage->hid & 0xf);
714 return 1;
715 } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
716 unsigned int abs = usage->hid & HID_USAGE;
717
718 if (abs >= ARRAY_SIZE(navigation_absmap))
719 return -1;
720
721 abs = navigation_absmap[abs];
722
723 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
724 return 1;
725 }
726
727 return -1;
728}
729
730
Roderick Colenbranderbe985302017-03-07 15:45:08 -0800731static int sixaxis_mapping(struct hid_device *hdev, struct hid_input *hi,
732 struct hid_field *field, struct hid_usage *usage,
733 unsigned long **bit, int *max)
734{
735 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
736 unsigned int key = usage->hid & HID_USAGE;
737
738 if (key >= ARRAY_SIZE(sixaxis_keymap))
739 return -1;
740
741 key = sixaxis_keymap[key];
742 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
743 return 1;
744 } else if (usage->hid == HID_GD_POINTER) {
745 /* The DS3 provides analog values for most buttons and even
746 * for HAT axes through GD Pointer. L2 and R2 are reported
747 * among these as well instead of as GD Z / RZ. Remap L2
748 * and R2 and ignore other analog 'button axes' as there is
749 * no good way for reporting them.
750 */
751 switch (usage->usage_index) {
752 case 8: /* L2 */
753 usage->hid = HID_GD_Z;
754 break;
755 case 9: /* R2 */
756 usage->hid = HID_GD_RZ;
757 break;
758 default:
759 return -1;
760 }
761
762 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, usage->hid & 0xf);
763 return 1;
764 } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
765 unsigned int abs = usage->hid & HID_USAGE;
766
767 if (abs >= ARRAY_SIZE(sixaxis_absmap))
768 return -1;
769
770 abs = sixaxis_absmap[abs];
771
772 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
773 return 1;
774 }
775
776 return -1;
777}
778
Roderick Colenbrander687aa022016-11-23 14:07:08 -0800779static int ds4_mapping(struct hid_device *hdev, struct hid_input *hi,
780 struct hid_field *field, struct hid_usage *usage,
781 unsigned long **bit, int *max)
782{
783 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
784 unsigned int key = usage->hid & HID_USAGE;
785
786 if (key >= ARRAY_SIZE(ds4_keymap))
787 return -1;
788
789 key = ds4_keymap[key];
790 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
791 return 1;
792 } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
793 unsigned int abs = usage->hid & HID_USAGE;
794
795 /* Let the HID parser deal with the HAT. */
796 if (usage->hid == HID_GD_HATSWITCH)
797 return 0;
798
799 if (abs >= ARRAY_SIZE(ds4_absmap))
800 return -1;
801
802 abs = ds4_absmap[abs];
803 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
804 return 1;
805 }
806
807 return 0;
808}
809
Pavel Machek1adf9042016-02-09 13:55:08 +0100810static u8 *sony_report_fixup(struct hid_device *hdev, u8 *rdesc,
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400811 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200812{
813 struct sony_sc *sc = hid_get_drvdata(hdev);
814
Mikko Perttunen4ba1eee2016-07-21 19:54:48 +0300815 if (sc->quirks & (SINO_LITE_CONTROLLER | FUTUREMAX_DANCE_MAT))
Scott Moreau74500cc2016-01-13 07:40:42 -0700816 return rdesc;
817
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900818 /*
819 * Some Sony RF receivers wrongly declare the mouse pointer as a
820 * a constant non-data variable.
821 */
822 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
823 /* usage page: generic desktop controls */
824 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
825 /* usage: mouse */
826 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
827 /* input (usage page for x,y axes): constant, variable, relative */
828 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +0900829 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900830 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200831 rdesc[55] = 0x06;
832 }
Simon Wood61ab44b2011-06-10 12:00:26 +0200833
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400834 if (sc->quirks & MOTION_CONTROLLER)
835 return motion_fixup(hdev, rdesc, rsize);
836
Jiri Kosina078328d2013-06-13 12:03:49 +0200837 if (sc->quirks & PS3REMOTE)
838 return ps3remote_fixup(hdev, rdesc, rsize);
839
Scott Shumatebe560f72020-05-13 13:39:26 -0500840 /*
841 * Some knock-off USB dongles incorrectly report their button count
842 * as 13 instead of 16 causing three non-functional buttons.
843 */
844 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) && *rsize >= 45 &&
845 /* Report Count (13) */
846 rdesc[23] == 0x95 && rdesc[24] == 0x0D &&
847 /* Usage Maximum (13) */
848 rdesc[37] == 0x29 && rdesc[38] == 0x0D &&
849 /* Report Count (3) */
850 rdesc[43] == 0x95 && rdesc[44] == 0x03) {
851 hid_info(hdev, "Fixing up USB dongle report descriptor\n");
852 rdesc[24] = 0x10;
853 rdesc[38] = 0x10;
854 rdesc[44] = 0x00;
855 }
856
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400857 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200858}
859
Pavel Machek1adf9042016-02-09 13:55:08 +0100860static void sixaxis_parse_report(struct sony_sc *sc, u8 *rd, int size)
Frank Praznikd902f472014-01-27 10:17:36 -0500861{
Pavel Machek1adf9042016-02-09 13:55:08 +0100862 static const u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
Frank Praznikd902f472014-01-27 10:17:36 -0500863 unsigned long flags;
Simon Wood12e9a6d2015-06-09 21:27:05 -0600864 int offset;
Pavel Machek1adf9042016-02-09 13:55:08 +0100865 u8 cable_state, battery_capacity, battery_charging;
Frank Praznikd902f472014-01-27 10:17:36 -0500866
Frank Praznikad142b92014-02-20 11:36:00 -0500867 /*
868 * The sixaxis is charging if the battery value is 0xee
Frank Praznikd902f472014-01-27 10:17:36 -0500869 * and it is fully charged if the value is 0xef.
870 * It does not report the actual level while charging so it
871 * is set to 100% while charging is in progress.
872 */
Simon Wood12e9a6d2015-06-09 21:27:05 -0600873 offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
874
875 if (rd[offset] >= 0xee) {
Frank Praznikd902f472014-01-27 10:17:36 -0500876 battery_capacity = 100;
Simon Wood12e9a6d2015-06-09 21:27:05 -0600877 battery_charging = !(rd[offset] & 0x01);
Frank Praznik9fddd742014-08-29 13:11:52 -0400878 cable_state = 1;
Frank Praznikd902f472014-01-27 10:17:36 -0500879 } else {
Pavel Machek1adf9042016-02-09 13:55:08 +0100880 u8 index = rd[offset] <= 5 ? rd[offset] : 5;
Frank Praznikac3c9a92014-02-20 11:36:02 -0500881 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -0500882 battery_charging = 0;
Frank Praznik9fddd742014-08-29 13:11:52 -0400883 cable_state = 0;
Frank Praznikd902f472014-01-27 10:17:36 -0500884 }
Frank Praznikd902f472014-01-27 10:17:36 -0500885
886 spin_lock_irqsave(&sc->lock, flags);
887 sc->cable_state = cable_state;
888 sc->battery_capacity = battery_capacity;
889 sc->battery_charging = battery_charging;
890 spin_unlock_irqrestore(&sc->lock, flags);
Roderick Colenbrander89e9ccd2017-03-07 15:45:10 -0800891
892 if (sc->quirks & SIXAXIS_CONTROLLER) {
893 int val;
894
895 offset = SIXAXIS_INPUT_REPORT_ACC_X_OFFSET;
896 val = ((rd[offset+1] << 8) | rd[offset]) - 511;
897 input_report_abs(sc->sensor_dev, ABS_X, val);
898
899 /* Y and Z are swapped and inversed */
900 val = 511 - ((rd[offset+5] << 8) | rd[offset+4]);
901 input_report_abs(sc->sensor_dev, ABS_Y, val);
902
903 val = 511 - ((rd[offset+3] << 8) | rd[offset+2]);
904 input_report_abs(sc->sensor_dev, ABS_Z, val);
905
906 input_sync(sc->sensor_dev);
907 }
Frank Praznikd902f472014-01-27 10:17:36 -0500908}
909
Pavel Machek1adf9042016-02-09 13:55:08 +0100910static void dualshock4_parse_report(struct sony_sc *sc, u8 *rd, int size)
Frank Praznikd902f472014-01-27 10:17:36 -0500911{
Roderick Colenbrander8621b7b2017-03-07 15:45:03 -0800912 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
913 struct hid_input, list);
914 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -0500915 unsigned long flags;
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -0700916 int n, m, offset, num_touch_data, max_touch_data;
Pavel Machek1adf9042016-02-09 13:55:08 +0100917 u8 cable_state, battery_capacity, battery_charging;
Roderick Colenbranderf79133c2017-03-07 15:45:02 -0800918 u16 timestamp;
Frank Praznikd902f472014-01-27 10:17:36 -0500919
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -0700920 /* When using Bluetooth the header is 2 bytes longer, so skip these. */
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -0800921 int data_offset = (sc->quirks & DUALSHOCK4_CONTROLLER_BT) ? 2 : 0;
Frank Praznik6c5f8602014-02-05 20:03:47 -0500922
Roderick Colenbrander42027ee2016-11-23 14:07:07 -0800923 /* Second bit of third button byte is for the touchpad button. */
924 offset = data_offset + DS4_INPUT_REPORT_BUTTON_OFFSET;
925 input_report_key(sc->touchpad, BTN_LEFT, rd[offset+2] & 0x2);
926
Roderick Colenbrander8621b7b2017-03-07 15:45:03 -0800927 /*
928 * The default behavior of the Dualshock 4 is to send reports using
929 * report type 1 when running over Bluetooth. However, when feature
930 * report 2 is requested during the controller initialization it starts
931 * sending input reports in report 17. Since report 17 is undefined
932 * in the default HID descriptor, the HID layer won't generate events.
933 * While it is possible (and this was done before) to fixup the HID
934 * descriptor to add this mapping, it was better to do this manually.
935 * The reason is there were various pieces software both open and closed
936 * source, relying on the descriptors to be the same across various
937 * operating systems. If the descriptors wouldn't match some
938 * applications e.g. games on Wine would not be able to function due
939 * to different descriptors, which such applications are not parsing.
940 */
941 if (rd[0] == 17) {
942 int value;
943
944 offset = data_offset + DS4_INPUT_REPORT_AXIS_OFFSET;
945 input_report_abs(input_dev, ABS_X, rd[offset]);
946 input_report_abs(input_dev, ABS_Y, rd[offset+1]);
947 input_report_abs(input_dev, ABS_RX, rd[offset+2]);
948 input_report_abs(input_dev, ABS_RY, rd[offset+3]);
949
950 value = rd[offset+4] & 0xf;
951 if (value > 7)
952 value = 8; /* Center 0, 0 */
953 input_report_abs(input_dev, ABS_HAT0X, ds4_hat_mapping[value].x);
954 input_report_abs(input_dev, ABS_HAT0Y, ds4_hat_mapping[value].y);
955
956 input_report_key(input_dev, BTN_WEST, rd[offset+4] & 0x10);
957 input_report_key(input_dev, BTN_SOUTH, rd[offset+4] & 0x20);
958 input_report_key(input_dev, BTN_EAST, rd[offset+4] & 0x40);
959 input_report_key(input_dev, BTN_NORTH, rd[offset+4] & 0x80);
960
961 input_report_key(input_dev, BTN_TL, rd[offset+5] & 0x1);
962 input_report_key(input_dev, BTN_TR, rd[offset+5] & 0x2);
963 input_report_key(input_dev, BTN_TL2, rd[offset+5] & 0x4);
964 input_report_key(input_dev, BTN_TR2, rd[offset+5] & 0x8);
965 input_report_key(input_dev, BTN_SELECT, rd[offset+5] & 0x10);
966 input_report_key(input_dev, BTN_START, rd[offset+5] & 0x20);
967 input_report_key(input_dev, BTN_THUMBL, rd[offset+5] & 0x40);
968 input_report_key(input_dev, BTN_THUMBR, rd[offset+5] & 0x80);
969
970 input_report_key(input_dev, BTN_MODE, rd[offset+6] & 0x1);
971
972 input_report_abs(input_dev, ABS_Z, rd[offset+7]);
973 input_report_abs(input_dev, ABS_RZ, rd[offset+8]);
974
975 input_sync(input_dev);
976 }
977
Roderick Colenbranderf79133c2017-03-07 15:45:02 -0800978 /* Convert timestamp (in 5.33us unit) to timestamp_us */
979 offset = data_offset + DS4_INPUT_REPORT_TIMESTAMP_OFFSET;
980 timestamp = get_unaligned_le16(&rd[offset]);
981 if (!sc->timestamp_initialized) {
982 sc->timestamp_us = ((unsigned int)timestamp * 16) / 3;
983 sc->timestamp_initialized = true;
984 } else {
985 u16 delta;
986
987 if (sc->prev_timestamp > timestamp)
988 delta = (U16_MAX - sc->prev_timestamp + timestamp + 1);
989 else
990 delta = timestamp - sc->prev_timestamp;
991 sc->timestamp_us += (delta * 16) / 3;
992 }
993 sc->prev_timestamp = timestamp;
994 input_event(sc->sensor_dev, EV_MSC, MSC_TIMESTAMP, sc->timestamp_us);
995
Roderick Colenbrander6f561e92017-03-07 15:45:00 -0800996 offset = data_offset + DS4_INPUT_REPORT_GYRO_X_OFFSET;
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -0800997 for (n = 0; n < 6; n++) {
998 /* Store data in int for more precision during mult_frac. */
999 int raw_data = (short)((rd[offset+1] << 8) | rd[offset]);
1000 struct ds4_calibration_data *calib = &sc->ds4_calib_data[n];
Roderick Colenbrander6f561e92017-03-07 15:45:00 -08001001
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -08001002 /* High precision is needed during calibration, but the
1003 * calibrated values are within 32-bit.
1004 * Note: we swap numerator 'x' and 'numer' in mult_frac for
1005 * precision reasons so we don't need 64-bit.
1006 */
1007 int calib_data = mult_frac(calib->sens_numer,
1008 raw_data - calib->bias,
1009 calib->sens_denom);
1010
1011 input_report_abs(sc->sensor_dev, calib->abs_code, calib_data);
1012 offset += 2;
Roderick Colenbrander6f561e92017-03-07 15:45:00 -08001013 }
1014 input_sync(sc->sensor_dev);
1015
Frank Praznikad142b92014-02-20 11:36:00 -05001016 /*
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -07001017 * The lower 4 bits of byte 30 (or 32 for BT) contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -05001018 * and the 5th bit contains the USB cable state.
1019 */
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -07001020 offset = data_offset + DS4_INPUT_REPORT_BATTERY_OFFSET;
Frank Praznik6c5f8602014-02-05 20:03:47 -05001021 cable_state = (rd[offset] >> 4) & 0x01;
1022 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -05001023
Frank Praznikad142b92014-02-20 11:36:00 -05001024 /*
1025 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -05001026 * 0 to 10, and when running on battery power it ranges from 0 to 9.
1027 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -05001028 */
Frank Praznik6c5f8602014-02-05 20:03:47 -05001029 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -05001030 battery_charging = 0;
1031 else
1032 battery_charging = 1;
1033
Frank Praznik6c5f8602014-02-05 20:03:47 -05001034 if (!cable_state)
1035 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -05001036 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -05001037 battery_capacity = 10;
1038
Frank Praznikd902f472014-01-27 10:17:36 -05001039 battery_capacity *= 10;
1040
1041 spin_lock_irqsave(&sc->lock, flags);
1042 sc->cable_state = cable_state;
1043 sc->battery_capacity = battery_capacity;
1044 sc->battery_charging = battery_charging;
1045 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -05001046
Frank Praznikad142b92014-02-20 11:36:00 -05001047 /*
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -07001048 * The Dualshock 4 multi-touch trackpad data starts at offset 33 on USB
1049 * and 35 on Bluetooth.
1050 * The first byte indicates the number of touch data in the report.
1051 * Trackpad data starts 2 bytes later (e.g. 35 for USB).
Frank Praznike5606232014-01-27 10:17:37 -05001052 */
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -07001053 offset = data_offset + DS4_INPUT_REPORT_TOUCHPAD_OFFSET;
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08001054 max_touch_data = (sc->quirks & DUALSHOCK4_CONTROLLER_BT) ? 4 : 3;
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -07001055 if (rd[offset] > 0 && rd[offset] <= max_touch_data)
1056 num_touch_data = rd[offset];
1057 else
1058 num_touch_data = 1;
1059 offset += 1;
Frank Praznike5606232014-01-27 10:17:37 -05001060
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -07001061 for (m = 0; m < num_touch_data; m++) {
1062 /* Skip past timestamp */
1063 offset += 1;
Frank Praznike5606232014-01-27 10:17:37 -05001064
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -07001065 /*
1066 * The first 7 bits of the first byte is a counter and bit 8 is
1067 * a touch indicator that is 0 when pressed and 1 when not
1068 * pressed.
1069 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1070 * The data for the second touch is in the same format and
1071 * immediately follows the data for the first.
1072 */
1073 for (n = 0; n < 2; n++) {
1074 u16 x, y;
1075 bool active;
Frank Praznike5606232014-01-27 10:17:37 -05001076
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -07001077 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1078 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1079
1080 active = !(rd[offset] >> 7);
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08001081 input_mt_slot(sc->touchpad, n);
1082 input_mt_report_slot_state(sc->touchpad, MT_TOOL_FINGER, active);
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -07001083
1084 if (active) {
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08001085 input_report_abs(sc->touchpad, ABS_MT_POSITION_X, x);
1086 input_report_abs(sc->touchpad, ABS_MT_POSITION_Y, y);
Roderick Colenbrander6e62c0b2016-10-07 12:39:38 -07001087 }
1088
1089 offset += 4;
1090 }
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08001091 input_mt_sync_frame(sc->touchpad);
1092 input_sync(sc->touchpad);
Frank Praznike5606232014-01-27 10:17:37 -05001093 }
Frank Praznikd902f472014-01-27 10:17:36 -05001094}
1095
Simon Woodc9e4d872011-06-10 12:00:27 +02001096static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
Pavel Machek1adf9042016-02-09 13:55:08 +01001097 u8 *rd, int size)
Simon Woodc9e4d872011-06-10 12:00:27 +02001098{
1099 struct sony_sc *sc = hid_get_drvdata(hdev);
1100
Frank Praznikad142b92014-02-20 11:36:00 -05001101 /*
1102 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +02001103 * has to be BYTE_SWAPPED before passing up to joystick interface
1104 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001105 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Frank Praznik8f5f0bc2015-07-23 19:01:16 -04001106 /*
1107 * When connected via Bluetooth the Sixaxis occasionally sends
1108 * a report with the second byte 0xff and the rest zeroed.
1109 *
1110 * This report does not reflect the actual state of the
1111 * controller must be ignored to avoid generating false input
1112 * events.
1113 */
1114 if (rd[1] == 0xff)
1115 return -EINVAL;
1116
Simon Woodc9e4d872011-06-10 12:00:27 +02001117 swap(rd[41], rd[42]);
1118 swap(rd[43], rd[44]);
1119 swap(rd[45], rd[46]);
1120 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -05001121
1122 sixaxis_parse_report(sc, rd, size);
Simon Wood12e9a6d2015-06-09 21:27:05 -06001123 } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
1124 sixaxis_parse_report(sc, rd, size);
Simon Wood4545ee02015-06-17 00:08:52 -06001125 } else if ((sc->quirks & NAVIGATION_CONTROLLER) && rd[0] == 0x01 &&
1126 size == 49) {
1127 sixaxis_parse_report(sc, rd, size);
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08001128 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1129 size == 64) {
1130 dualshock4_parse_report(sc, rd, size);
1131 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && rd[0] == 0x11 &&
1132 size == 78)) {
1133 /* CRC check */
1134 u8 bthdr = 0xA1;
1135 u32 crc;
1136 u32 report_crc;
Roderick Colenbrander6dfd5bf2016-10-07 12:39:36 -07001137
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08001138 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
1139 crc = ~crc32_le(crc, rd, DS4_INPUT_REPORT_0x11_SIZE-4);
1140 report_crc = get_unaligned_le32(&rd[DS4_INPUT_REPORT_0x11_SIZE-4]);
1141 if (crc != report_crc) {
1142 hid_dbg(sc->hdev, "DualShock 4 input report's CRC check failed, received crc 0x%0x != 0x%0x\n",
1143 report_crc, crc);
1144 return -EILSEQ;
Roderick Colenbrander6dfd5bf2016-10-07 12:39:36 -07001145 }
Roderick Colenbrander40e1f202016-12-08 19:09:52 -08001146
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08001147 dualshock4_parse_report(sc, rd, size);
1148 } else if ((sc->quirks & DUALSHOCK4_DONGLE) && rd[0] == 0x01 &&
1149 size == 64) {
Roderick Colenbrander72dbb852017-03-07 15:45:06 -08001150 unsigned long flags;
1151 enum ds4_dongle_state dongle_state;
1152
Roderick Colenbrander40e1f202016-12-08 19:09:52 -08001153 /*
1154 * In the case of a DS4 USB dongle, bit[2] of byte 31 indicates
1155 * if a DS4 is actually connected (indicated by '0').
1156 * For non-dongle, this bit is always 0 (connected).
1157 */
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08001158 bool connected = (rd[31] & 0x04) ? false : true;
Roderick Colenbrander40e1f202016-12-08 19:09:52 -08001159
Roderick Colenbrander72dbb852017-03-07 15:45:06 -08001160 spin_lock_irqsave(&sc->lock, flags);
1161 dongle_state = sc->ds4_dongle_state;
1162 spin_unlock_irqrestore(&sc->lock, flags);
1163
1164 /*
1165 * The dongle always sends input reports even when no
1166 * DS4 is attached. When a DS4 is connected, we need to
1167 * obtain calibration data before we can use it.
1168 * The code below tracks dongle state and kicks of
1169 * calibration when needed and only allows us to process
1170 * input if a DS4 is actually connected.
1171 */
1172 if (dongle_state == DONGLE_DISCONNECTED && connected) {
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08001173 hid_info(sc->hdev, "DualShock 4 USB dongle: controller connected\n");
1174 sony_set_leds(sc);
Roderick Colenbrander72dbb852017-03-07 15:45:06 -08001175
1176 spin_lock_irqsave(&sc->lock, flags);
1177 sc->ds4_dongle_state = DONGLE_CALIBRATING;
1178 spin_unlock_irqrestore(&sc->lock, flags);
1179
1180 sony_schedule_work(sc, SONY_WORKER_HOTPLUG);
1181
1182 /* Don't process the report since we don't have
1183 * calibration data, but let hidraw have it anyway.
1184 */
1185 return 0;
1186 } else if ((dongle_state == DONGLE_CONNECTED ||
1187 dongle_state == DONGLE_DISABLED) && !connected) {
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08001188 hid_info(sc->hdev, "DualShock 4 USB dongle: controller disconnected\n");
Roderick Colenbrander72dbb852017-03-07 15:45:06 -08001189
1190 spin_lock_irqsave(&sc->lock, flags);
1191 sc->ds4_dongle_state = DONGLE_DISCONNECTED;
1192 spin_unlock_irqrestore(&sc->lock, flags);
1193
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08001194 /* Return 0, so hidraw can get the report. */
1195 return 0;
Roderick Colenbrander72dbb852017-03-07 15:45:06 -08001196 } else if (dongle_state == DONGLE_CALIBRATING ||
1197 dongle_state == DONGLE_DISABLED ||
1198 dongle_state == DONGLE_DISCONNECTED) {
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08001199 /* Return 0, so hidraw can get the report. */
1200 return 0;
Roderick Colenbrander40e1f202016-12-08 19:09:52 -08001201 }
1202
Frank Praznikd902f472014-01-27 10:17:36 -05001203 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +02001204 }
1205
Frank Praznik2a242932016-09-22 20:18:09 -04001206 if (sc->defer_initialization) {
1207 sc->defer_initialization = 0;
Roderick Colenbrander3a60a132017-03-07 15:45:05 -08001208 sony_schedule_work(sc, SONY_WORKER_STATE);
Frank Praznik2a242932016-09-22 20:18:09 -04001209 }
1210
Simon Woodc9e4d872011-06-10 12:00:27 +02001211 return 0;
1212}
1213
Colin Leitnerf04d5142013-05-27 23:41:05 +02001214static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1215 struct hid_field *field, struct hid_usage *usage,
1216 unsigned long **bit, int *max)
1217{
1218 struct sony_sc *sc = hid_get_drvdata(hdev);
1219
1220 if (sc->quirks & BUZZ_CONTROLLER) {
1221 unsigned int key = usage->hid & HID_USAGE;
1222
1223 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1224 return -1;
1225
1226 switch (usage->collection_index) {
1227 case 1:
1228 if (key >= ARRAY_SIZE(buzz_keymap))
1229 return -1;
1230
1231 key = buzz_keymap[key];
1232 if (!key)
1233 return -1;
1234 break;
1235 default:
1236 return -1;
1237 }
1238
1239 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1240 return 1;
1241 }
1242
Jiri Kosina078328d2013-06-13 12:03:49 +02001243 if (sc->quirks & PS3REMOTE)
1244 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1245
Roderick Colenbrander41deb922017-03-24 15:17:46 -07001246 if (sc->quirks & NAVIGATION_CONTROLLER)
1247 return navigation_mapping(hdev, hi, field, usage, bit, max);
1248
Roderick Colenbranderbe985302017-03-07 15:45:08 -08001249 if (sc->quirks & SIXAXIS_CONTROLLER)
1250 return sixaxis_mapping(hdev, hi, field, usage, bit, max);
Roderick Colenbrander687aa022016-11-23 14:07:08 -08001251
1252 if (sc->quirks & DUALSHOCK4_CONTROLLER)
1253 return ds4_mapping(hdev, hi, field, usage, bit, max);
1254
Roderick Colenbranderbe985302017-03-07 15:45:08 -08001255
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001256 /* Let hid-core decide for the others */
1257 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001258}
1259
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08001260static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
Frank Praznikce8efc32014-09-18 21:15:01 -04001261 int w, int h)
1262{
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08001263 size_t name_sz;
1264 char *name;
Frank Praznikce8efc32014-09-18 21:15:01 -04001265 int ret;
1266
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08001267 sc->touchpad = input_allocate_device();
1268 if (!sc->touchpad)
1269 return -ENOMEM;
Frank Praznikce8efc32014-09-18 21:15:01 -04001270
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08001271 input_set_drvdata(sc->touchpad, sc);
1272 sc->touchpad->dev.parent = &sc->hdev->dev;
1273 sc->touchpad->phys = sc->hdev->phys;
1274 sc->touchpad->uniq = sc->hdev->uniq;
1275 sc->touchpad->id.bustype = sc->hdev->bus;
1276 sc->touchpad->id.vendor = sc->hdev->vendor;
1277 sc->touchpad->id.product = sc->hdev->product;
1278 sc->touchpad->id.version = sc->hdev->version;
1279
1280 /* Append a suffix to the controller name as there are various
1281 * DS4 compatible non-Sony devices with different names.
1282 */
1283 name_sz = strlen(sc->hdev->name) + sizeof(DS4_TOUCHPAD_SUFFIX);
1284 name = kzalloc(name_sz, GFP_KERNEL);
1285 if (!name) {
1286 ret = -ENOMEM;
1287 goto err;
1288 }
1289 snprintf(name, name_sz, "%s" DS4_TOUCHPAD_SUFFIX, sc->hdev->name);
1290 sc->touchpad->name = name;
1291
Roderick Colenbrander73416502017-03-07 15:45:07 -08001292 ret = input_mt_init_slots(sc->touchpad, touch_count, INPUT_MT_POINTER);
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08001293 if (ret < 0)
1294 goto err;
1295
1296 /* We map the button underneath the touchpad to BTN_LEFT. */
1297 __set_bit(EV_KEY, sc->touchpad->evbit);
1298 __set_bit(BTN_LEFT, sc->touchpad->keybit);
1299 __set_bit(INPUT_PROP_BUTTONPAD, sc->touchpad->propbit);
1300
1301 input_set_abs_params(sc->touchpad, ABS_MT_POSITION_X, 0, w, 0, 0);
1302 input_set_abs_params(sc->touchpad, ABS_MT_POSITION_Y, 0, h, 0, 0);
1303
1304 ret = input_register_device(sc->touchpad);
1305 if (ret < 0)
1306 goto err;
Frank Praznikce8efc32014-09-18 21:15:01 -04001307
1308 return 0;
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08001309
1310err:
1311 kfree(sc->touchpad->name);
1312 sc->touchpad->name = NULL;
1313
1314 input_free_device(sc->touchpad);
1315 sc->touchpad = NULL;
1316
1317 return ret;
Frank Praznikce8efc32014-09-18 21:15:01 -04001318}
1319
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08001320static void sony_unregister_touchpad(struct sony_sc *sc)
1321{
1322 if (!sc->touchpad)
1323 return;
1324
1325 kfree(sc->touchpad->name);
1326 sc->touchpad->name = NULL;
1327
1328 input_unregister_device(sc->touchpad);
1329 sc->touchpad = NULL;
1330}
Frank Praznikce8efc32014-09-18 21:15:01 -04001331
Roderick Colenbrander6f561e92017-03-07 15:45:00 -08001332static int sony_register_sensors(struct sony_sc *sc)
1333{
1334 size_t name_sz;
1335 char *name;
1336 int ret;
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -08001337 int range;
Roderick Colenbrander6f561e92017-03-07 15:45:00 -08001338
1339 sc->sensor_dev = input_allocate_device();
1340 if (!sc->sensor_dev)
1341 return -ENOMEM;
1342
1343 input_set_drvdata(sc->sensor_dev, sc);
1344 sc->sensor_dev->dev.parent = &sc->hdev->dev;
1345 sc->sensor_dev->phys = sc->hdev->phys;
1346 sc->sensor_dev->uniq = sc->hdev->uniq;
1347 sc->sensor_dev->id.bustype = sc->hdev->bus;
1348 sc->sensor_dev->id.vendor = sc->hdev->vendor;
1349 sc->sensor_dev->id.product = sc->hdev->product;
1350 sc->sensor_dev->id.version = sc->hdev->version;
1351
1352 /* Append a suffix to the controller name as there are various
1353 * DS4 compatible non-Sony devices with different names.
1354 */
Roderick Colenbrander89e9ccd2017-03-07 15:45:10 -08001355 name_sz = strlen(sc->hdev->name) + sizeof(SENSOR_SUFFIX);
Roderick Colenbrander6f561e92017-03-07 15:45:00 -08001356 name = kzalloc(name_sz, GFP_KERNEL);
1357 if (!name) {
1358 ret = -ENOMEM;
1359 goto err;
1360 }
Roderick Colenbrander89e9ccd2017-03-07 15:45:10 -08001361 snprintf(name, name_sz, "%s" SENSOR_SUFFIX, sc->hdev->name);
Roderick Colenbrander6f561e92017-03-07 15:45:00 -08001362 sc->sensor_dev->name = name;
1363
Roderick Colenbrander89e9ccd2017-03-07 15:45:10 -08001364 if (sc->quirks & SIXAXIS_CONTROLLER) {
1365 /* For the DS3 we only support the accelerometer, which works
1366 * quite well even without calibration. The device also has
1367 * a 1-axis gyro, but it is very difficult to manage from within
1368 * the driver even to get data, the sensor is inaccurate and
1369 * the behavior is very different between hardware revisions.
1370 */
1371 input_set_abs_params(sc->sensor_dev, ABS_X, -512, 511, 4, 0);
1372 input_set_abs_params(sc->sensor_dev, ABS_Y, -512, 511, 4, 0);
1373 input_set_abs_params(sc->sensor_dev, ABS_Z, -512, 511, 4, 0);
1374 input_abs_set_res(sc->sensor_dev, ABS_X, SIXAXIS_ACC_RES_PER_G);
1375 input_abs_set_res(sc->sensor_dev, ABS_Y, SIXAXIS_ACC_RES_PER_G);
1376 input_abs_set_res(sc->sensor_dev, ABS_Z, SIXAXIS_ACC_RES_PER_G);
1377 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
1378 range = DS4_ACC_RES_PER_G*4;
1379 input_set_abs_params(sc->sensor_dev, ABS_X, -range, range, 16, 0);
1380 input_set_abs_params(sc->sensor_dev, ABS_Y, -range, range, 16, 0);
1381 input_set_abs_params(sc->sensor_dev, ABS_Z, -range, range, 16, 0);
1382 input_abs_set_res(sc->sensor_dev, ABS_X, DS4_ACC_RES_PER_G);
1383 input_abs_set_res(sc->sensor_dev, ABS_Y, DS4_ACC_RES_PER_G);
1384 input_abs_set_res(sc->sensor_dev, ABS_Z, DS4_ACC_RES_PER_G);
Roderick Colenbrander6f561e92017-03-07 15:45:00 -08001385
Roderick Colenbrander89e9ccd2017-03-07 15:45:10 -08001386 range = DS4_GYRO_RES_PER_DEG_S*2048;
1387 input_set_abs_params(sc->sensor_dev, ABS_RX, -range, range, 16, 0);
1388 input_set_abs_params(sc->sensor_dev, ABS_RY, -range, range, 16, 0);
1389 input_set_abs_params(sc->sensor_dev, ABS_RZ, -range, range, 16, 0);
1390 input_abs_set_res(sc->sensor_dev, ABS_RX, DS4_GYRO_RES_PER_DEG_S);
1391 input_abs_set_res(sc->sensor_dev, ABS_RY, DS4_GYRO_RES_PER_DEG_S);
1392 input_abs_set_res(sc->sensor_dev, ABS_RZ, DS4_GYRO_RES_PER_DEG_S);
Roderick Colenbrander6f561e92017-03-07 15:45:00 -08001393
Roderick Colenbrander89e9ccd2017-03-07 15:45:10 -08001394 __set_bit(EV_MSC, sc->sensor_dev->evbit);
1395 __set_bit(MSC_TIMESTAMP, sc->sensor_dev->mscbit);
1396 }
1397
Roderick Colenbrander6f561e92017-03-07 15:45:00 -08001398 __set_bit(INPUT_PROP_ACCELEROMETER, sc->sensor_dev->propbit);
1399
1400 ret = input_register_device(sc->sensor_dev);
1401 if (ret < 0)
1402 goto err;
1403
1404 return 0;
1405
1406err:
1407 kfree(sc->sensor_dev->name);
1408 sc->sensor_dev->name = NULL;
1409
1410 input_free_device(sc->sensor_dev);
1411 sc->sensor_dev = NULL;
1412
1413 return ret;
1414}
1415
1416static void sony_unregister_sensors(struct sony_sc *sc)
1417{
1418 if (!sc->sensor_dev)
1419 return;
1420
1421 kfree(sc->sensor_dev->name);
1422 sc->sensor_dev->name = NULL;
1423
1424 input_unregister_device(sc->sensor_dev);
1425 sc->sensor_dev = NULL;
1426}
1427
1428
Antonio Ospite5710fab2011-02-20 18:26:45 +01001429/*
Jiri Slabybd28ce02008-06-25 23:47:04 +02001430 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1431 * to "operational". Without this, the ps3 controller will not report any
1432 * events.
1433 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001434static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001435{
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001436 const int buf_size =
1437 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
Pavel Machek1adf9042016-02-09 13:55:08 +01001438 u8 *buf;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001439 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001440
Antonio Ospite2e701a32015-02-16 18:12:24 +01001441 buf = kmalloc(buf_size, GFP_KERNEL);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001442 if (!buf)
1443 return -ENOMEM;
1444
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001445 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1446 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001447 if (ret < 0) {
1448 hid_err(hdev, "can't set operational mode: step 1\n");
1449 goto out;
1450 }
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001451
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001452 /*
1453 * Some compatible controllers like the Speedlink Strike FX and
1454 * Gasia need another query plus an USB interrupt to get operational.
1455 */
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001456 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1457 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001458 if (ret < 0) {
1459 hid_err(hdev, "can't set operational mode: step 2\n");
1460 goto out;
1461 }
1462
1463 ret = hid_hw_output_report(hdev, buf, 1);
Benjamin Tissoires19f4c2b2016-01-08 17:58:49 +01001464 if (ret < 0) {
1465 hid_info(hdev, "can't set operational mode: step 3, ignoring\n");
1466 ret = 0;
1467 }
Jiri Slabybd28ce02008-06-25 23:47:04 +02001468
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001469out:
Jiri Slabybd28ce02008-06-25 23:47:04 +02001470 kfree(buf);
1471
1472 return ret;
1473}
1474
Antonio Ospite816651a2010-05-03 22:15:55 +02001475static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001476{
Pavel Machek1adf9042016-02-09 13:55:08 +01001477 static const u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1478 u8 *buf;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001479 int ret;
1480
1481 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1482 if (!buf)
1483 return -ENOMEM;
1484
1485 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
Benjamin Tissoiresb0dd72a2014-02-10 12:58:54 -05001486 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001487
1488 kfree(buf);
1489
1490 return ret;
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001491}
1492
Frank Praznikad142b92014-02-20 11:36:00 -05001493/*
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -08001494 * Request DS4 calibration data for the motion sensors.
1495 * For Bluetooth this also affects the operating mode (see below).
Frank Praznik68330d82014-02-05 20:03:49 -05001496 */
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -08001497static int dualshock4_get_calibration_data(struct sony_sc *sc)
Frank Praznik68330d82014-02-05 20:03:49 -05001498{
Pavel Machek1adf9042016-02-09 13:55:08 +01001499 u8 *buf;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001500 int ret;
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -08001501 short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus;
1502 short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus;
1503 short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus;
1504 short gyro_speed_plus, gyro_speed_minus;
1505 short acc_x_plus, acc_x_minus;
1506 short acc_y_plus, acc_y_minus;
1507 short acc_z_plus, acc_z_minus;
1508 int speed_2x;
1509 int range_2g;
Frank Praznik68330d82014-02-05 20:03:49 -05001510
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -08001511 /* For Bluetooth we use a different request, which supports CRC.
1512 * Note: in Bluetooth mode feature report 0x02 also changes the state
1513 * of the controller, so that it sends input reports of type 0x11.
1514 */
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08001515 if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -08001516 buf = kmalloc(DS4_FEATURE_REPORT_0x02_SIZE, GFP_KERNEL);
1517 if (!buf)
1518 return -ENOMEM;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001519
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -08001520 ret = hid_hw_raw_request(sc->hdev, 0x02, buf,
1521 DS4_FEATURE_REPORT_0x02_SIZE,
1522 HID_FEATURE_REPORT,
1523 HID_REQ_GET_REPORT);
1524 if (ret < 0)
1525 goto err_stop;
1526 } else {
1527 u8 bthdr = 0xA3;
1528 u32 crc;
1529 u32 report_crc;
1530 int retries;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001531
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -08001532 buf = kmalloc(DS4_FEATURE_REPORT_0x05_SIZE, GFP_KERNEL);
1533 if (!buf)
1534 return -ENOMEM;
1535
1536 for (retries = 0; retries < 3; retries++) {
1537 ret = hid_hw_raw_request(sc->hdev, 0x05, buf,
1538 DS4_FEATURE_REPORT_0x05_SIZE,
1539 HID_FEATURE_REPORT,
1540 HID_REQ_GET_REPORT);
1541 if (ret < 0)
1542 goto err_stop;
1543
1544 /* CRC check */
1545 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
1546 crc = ~crc32_le(crc, buf, DS4_FEATURE_REPORT_0x05_SIZE-4);
1547 report_crc = get_unaligned_le32(&buf[DS4_FEATURE_REPORT_0x05_SIZE-4]);
1548 if (crc != report_crc) {
1549 hid_warn(sc->hdev, "DualShock 4 calibration report's CRC check failed, received crc 0x%0x != 0x%0x\n",
1550 report_crc, crc);
1551 if (retries < 2) {
1552 hid_warn(sc->hdev, "Retrying DualShock 4 get calibration report request\n");
1553 continue;
1554 } else {
1555 ret = -EILSEQ;
1556 goto err_stop;
1557 }
1558 } else {
1559 break;
1560 }
1561 }
1562 }
1563
1564 gyro_pitch_bias = get_unaligned_le16(&buf[1]);
1565 gyro_yaw_bias = get_unaligned_le16(&buf[3]);
1566 gyro_roll_bias = get_unaligned_le16(&buf[5]);
1567 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1568 gyro_pitch_plus = get_unaligned_le16(&buf[7]);
1569 gyro_pitch_minus = get_unaligned_le16(&buf[9]);
1570 gyro_yaw_plus = get_unaligned_le16(&buf[11]);
1571 gyro_yaw_minus = get_unaligned_le16(&buf[13]);
1572 gyro_roll_plus = get_unaligned_le16(&buf[15]);
1573 gyro_roll_minus = get_unaligned_le16(&buf[17]);
1574 } else {
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08001575 /* BT + Dongle */
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -08001576 gyro_pitch_plus = get_unaligned_le16(&buf[7]);
1577 gyro_yaw_plus = get_unaligned_le16(&buf[9]);
1578 gyro_roll_plus = get_unaligned_le16(&buf[11]);
1579 gyro_pitch_minus = get_unaligned_le16(&buf[13]);
1580 gyro_yaw_minus = get_unaligned_le16(&buf[15]);
1581 gyro_roll_minus = get_unaligned_le16(&buf[17]);
1582 }
1583 gyro_speed_plus = get_unaligned_le16(&buf[19]);
1584 gyro_speed_minus = get_unaligned_le16(&buf[21]);
1585 acc_x_plus = get_unaligned_le16(&buf[23]);
1586 acc_x_minus = get_unaligned_le16(&buf[25]);
1587 acc_y_plus = get_unaligned_le16(&buf[27]);
1588 acc_y_minus = get_unaligned_le16(&buf[29]);
1589 acc_z_plus = get_unaligned_le16(&buf[31]);
1590 acc_z_minus = get_unaligned_le16(&buf[33]);
1591
1592 /* Set gyroscope calibration and normalization parameters.
1593 * Data values will be normalized to 1/DS4_GYRO_RES_PER_DEG_S degree/s.
1594 */
1595 speed_2x = (gyro_speed_plus + gyro_speed_minus);
1596 sc->ds4_calib_data[0].abs_code = ABS_RX;
1597 sc->ds4_calib_data[0].bias = gyro_pitch_bias;
1598 sc->ds4_calib_data[0].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
1599 sc->ds4_calib_data[0].sens_denom = gyro_pitch_plus - gyro_pitch_minus;
1600
1601 sc->ds4_calib_data[1].abs_code = ABS_RY;
1602 sc->ds4_calib_data[1].bias = gyro_yaw_bias;
1603 sc->ds4_calib_data[1].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
1604 sc->ds4_calib_data[1].sens_denom = gyro_yaw_plus - gyro_yaw_minus;
1605
1606 sc->ds4_calib_data[2].abs_code = ABS_RZ;
1607 sc->ds4_calib_data[2].bias = gyro_roll_bias;
1608 sc->ds4_calib_data[2].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
1609 sc->ds4_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus;
1610
1611 /* Set accelerometer calibration and normalization parameters.
1612 * Data values will be normalized to 1/DS4_ACC_RES_PER_G G.
1613 */
1614 range_2g = acc_x_plus - acc_x_minus;
1615 sc->ds4_calib_data[3].abs_code = ABS_X;
1616 sc->ds4_calib_data[3].bias = acc_x_plus - range_2g / 2;
1617 sc->ds4_calib_data[3].sens_numer = 2*DS4_ACC_RES_PER_G;
1618 sc->ds4_calib_data[3].sens_denom = range_2g;
1619
1620 range_2g = acc_y_plus - acc_y_minus;
1621 sc->ds4_calib_data[4].abs_code = ABS_Y;
1622 sc->ds4_calib_data[4].bias = acc_y_plus - range_2g / 2;
1623 sc->ds4_calib_data[4].sens_numer = 2*DS4_ACC_RES_PER_G;
1624 sc->ds4_calib_data[4].sens_denom = range_2g;
1625
1626 range_2g = acc_z_plus - acc_z_minus;
1627 sc->ds4_calib_data[5].abs_code = ABS_Z;
1628 sc->ds4_calib_data[5].bias = acc_z_plus - range_2g / 2;
1629 sc->ds4_calib_data[5].sens_numer = 2*DS4_ACC_RES_PER_G;
1630 sc->ds4_calib_data[5].sens_denom = range_2g;
1631
1632err_stop:
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001633 kfree(buf);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001634 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001635}
1636
Roderick Colenbrander72dbb852017-03-07 15:45:06 -08001637static void dualshock4_calibration_work(struct work_struct *work)
1638{
1639 struct sony_sc *sc = container_of(work, struct sony_sc, hotplug_worker);
1640 unsigned long flags;
1641 enum ds4_dongle_state dongle_state;
1642 int ret;
1643
1644 ret = dualshock4_get_calibration_data(sc);
1645 if (ret < 0) {
1646 /* This call is very unlikely to fail for the dongle. When it
1647 * fails we are probably in a very bad state, so mark the
1648 * dongle as disabled. We will re-enable the dongle if a new
1649 * DS4 hotplug is detect from sony_raw_event as any issues
1650 * are likely resolved then (the dongle is quite stupid).
1651 */
1652 hid_err(sc->hdev, "DualShock 4 USB dongle: calibration failed, disabling device\n");
1653 dongle_state = DONGLE_DISABLED;
1654 } else {
1655 hid_info(sc->hdev, "DualShock 4 USB dongle: calibration completed\n");
1656 dongle_state = DONGLE_CONNECTED;
1657 }
1658
1659 spin_lock_irqsave(&sc->lock, flags);
1660 sc->ds4_dongle_state = dongle_state;
1661 spin_unlock_irqrestore(&sc->lock, flags);
1662}
1663
Frank Praznik221399b2015-05-05 20:47:32 -04001664static void sixaxis_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001665{
Pavel Machek1adf9042016-02-09 13:55:08 +01001666 static const u8 sixaxis_leds[10][4] = {
Frank Praznik80250872014-04-14 10:11:35 -04001667 { 0x01, 0x00, 0x00, 0x00 },
1668 { 0x00, 0x01, 0x00, 0x00 },
1669 { 0x00, 0x00, 0x01, 0x00 },
1670 { 0x00, 0x00, 0x00, 0x01 },
1671 { 0x01, 0x00, 0x00, 0x01 },
1672 { 0x00, 0x01, 0x00, 0x01 },
1673 { 0x00, 0x00, 0x01, 0x01 },
1674 { 0x01, 0x00, 0x01, 0x01 },
1675 { 0x00, 0x01, 0x01, 0x01 },
1676 { 0x01, 0x01, 0x01, 0x01 }
1677 };
1678
Frank Praznik221399b2015-05-05 20:47:32 -04001679 int id = sc->device_id;
1680
1681 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001682
1683 if (id < 0)
1684 return;
1685
1686 id %= 10;
Frank Praznik221399b2015-05-05 20:47:32 -04001687 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001688}
1689
Frank Praznik221399b2015-05-05 20:47:32 -04001690static void dualshock4_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001691{
1692 /* The first 4 color/index entries match what the PS4 assigns */
Pavel Machek1adf9042016-02-09 13:55:08 +01001693 static const u8 color_code[7][3] = {
Roderick Colenbranderacde02b2017-03-24 15:17:47 -07001694 /* Blue */ { 0x00, 0x00, 0x40 },
1695 /* Red */ { 0x40, 0x00, 0x00 },
1696 /* Green */ { 0x00, 0x40, 0x00 },
1697 /* Pink */ { 0x20, 0x00, 0x20 },
Frank Praznik80250872014-04-14 10:11:35 -04001698 /* Orange */ { 0x02, 0x01, 0x00 },
1699 /* Teal */ { 0x00, 0x01, 0x01 },
1700 /* White */ { 0x01, 0x01, 0x01 }
1701 };
1702
Frank Praznik221399b2015-05-05 20:47:32 -04001703 int id = sc->device_id;
1704
1705 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001706
1707 if (id < 0)
1708 return;
1709
1710 id %= 7;
Frank Praznik221399b2015-05-05 20:47:32 -04001711 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001712}
1713
Frank Praznik221399b2015-05-05 20:47:32 -04001714static void buzz_set_leds(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001715{
Frank Praznik221399b2015-05-05 20:47:32 -04001716 struct hid_device *hdev = sc->hdev;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001717 struct list_head *report_list =
1718 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1719 struct hid_report *report = list_entry(report_list->next,
1720 struct hid_report, list);
Pavel Machek1adf9042016-02-09 13:55:08 +01001721 s32 *value = report->field[0]->value;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001722
Frank Praznik221399b2015-05-05 20:47:32 -04001723 BUILD_BUG_ON(MAX_LEDS < 4);
1724
Colin Leitnerf04d5142013-05-27 23:41:05 +02001725 value[0] = 0x00;
Frank Praznik221399b2015-05-05 20:47:32 -04001726 value[1] = sc->led_state[0] ? 0xff : 0x00;
1727 value[2] = sc->led_state[1] ? 0xff : 0x00;
1728 value[3] = sc->led_state[2] ? 0xff : 0x00;
1729 value[4] = sc->led_state[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001730 value[5] = 0x00;
1731 value[6] = 0x00;
1732 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1733}
1734
Frank Praznik221399b2015-05-05 20:47:32 -04001735static void sony_set_leds(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001736{
Frank Praznik221399b2015-05-05 20:47:32 -04001737 if (!(sc->quirks & BUZZ_CONTROLLER))
Roderick Colenbrander3a60a132017-03-07 15:45:05 -08001738 sony_schedule_work(sc, SONY_WORKER_STATE);
Frank Praznik221399b2015-05-05 20:47:32 -04001739 else
1740 buzz_set_leds(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001741}
1742
Sven Eckelmannc5382512013-11-19 20:26:30 +01001743static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001744 enum led_brightness value)
1745{
1746 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001747 struct hid_device *hdev = to_hid_device(dev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001748 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001749
1750 int n;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001751 int force_update;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001752
1753 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001754 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001755 hid_err(hdev, "No device data\n");
1756 return;
1757 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001758
Frank Praznikb3ed4582014-04-14 10:11:36 -04001759 /*
1760 * The Sixaxis on USB will override any LED settings sent to it
1761 * and keep flashing all of the LEDs until the PS button is pressed.
1762 * Updates, even if redundant, must be always be sent to the
1763 * controller to avoid having to toggle the state of an LED just to
1764 * stop the flashing later on.
1765 */
1766 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1767
Frank Praznik60781cf2014-01-11 15:13:15 -05001768 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznikb3ed4582014-04-14 10:11:36 -04001769 if (led == drv_data->leds[n] && (force_update ||
1770 (value != drv_data->led_state[n] ||
1771 drv_data->led_delay_on[n] ||
1772 drv_data->led_delay_off[n]))) {
1773
1774 drv_data->led_state[n] = value;
1775
1776 /* Setting the brightness stops the blinking */
1777 drv_data->led_delay_on[n] = 0;
1778 drv_data->led_delay_off[n] = 0;
1779
Frank Praznik221399b2015-05-05 20:47:32 -04001780 sony_set_leds(drv_data);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001781 break;
1782 }
1783 }
1784}
1785
Sven Eckelmannc5382512013-11-19 20:26:30 +01001786static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001787{
1788 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001789 struct hid_device *hdev = to_hid_device(dev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001790 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001791
1792 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001793
1794 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001795 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001796 hid_err(hdev, "No device data\n");
1797 return LED_OFF;
1798 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001799
Frank Praznik60781cf2014-01-11 15:13:15 -05001800 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001801 if (led == drv_data->leds[n])
1802 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001803 }
1804
Simon Wood7db75042014-02-05 12:34:18 -07001805 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001806}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001807
Frank Praznikb3ed4582014-04-14 10:11:36 -04001808static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1809 unsigned long *delay_off)
1810{
1811 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001812 struct hid_device *hdev = to_hid_device(dev);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001813 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1814 int n;
Pavel Machek1adf9042016-02-09 13:55:08 +01001815 u8 new_on, new_off;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001816
1817 if (!drv_data) {
1818 hid_err(hdev, "No device data\n");
1819 return -EINVAL;
1820 }
1821
1822 /* Max delay is 255 deciseconds or 2550 milliseconds */
1823 if (*delay_on > 2550)
1824 *delay_on = 2550;
1825 if (*delay_off > 2550)
1826 *delay_off = 2550;
1827
1828 /* Blink at 1 Hz if both values are zero */
1829 if (!*delay_on && !*delay_off)
1830 *delay_on = *delay_off = 500;
1831
1832 new_on = *delay_on / 10;
1833 new_off = *delay_off / 10;
1834
1835 for (n = 0; n < drv_data->led_count; n++) {
1836 if (led == drv_data->leds[n])
1837 break;
1838 }
1839
1840 /* This LED is not registered on this device */
1841 if (n >= drv_data->led_count)
1842 return -EINVAL;
1843
1844 /* Don't schedule work if the values didn't change */
1845 if (new_on != drv_data->led_delay_on[n] ||
1846 new_off != drv_data->led_delay_off[n]) {
1847 drv_data->led_delay_on[n] = new_on;
1848 drv_data->led_delay_off[n] = new_off;
Roderick Colenbrander3a60a132017-03-07 15:45:05 -08001849 sony_schedule_work(drv_data, SONY_WORKER_STATE);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001850 }
1851
1852 return 0;
1853}
1854
Frank Praznikfa57a812014-04-14 10:11:33 -04001855static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001856{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001857 struct led_classdev *led;
1858 int n;
1859
Frank Praznikfa57a812014-04-14 10:11:33 -04001860 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001861
Frank Praznikfa57a812014-04-14 10:11:33 -04001862 for (n = 0; n < sc->led_count; n++) {
1863 led = sc->leds[n];
1864 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001865 if (!led)
1866 continue;
1867 led_classdev_unregister(led);
1868 kfree(led);
1869 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001870
Frank Praznikfa57a812014-04-14 10:11:33 -04001871 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001872}
1873
Frank Praznikfa57a812014-04-14 10:11:33 -04001874static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001875{
Frank Praznikfa57a812014-04-14 10:11:33 -04001876 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001877 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001878 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001879 struct led_classdev *led;
1880 size_t name_sz;
1881 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001882 size_t name_len;
1883 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001884 static const char * const ds4_name_str[] = { "red", "green", "blue",
1885 "global" };
Pavel Machek1adf9042016-02-09 13:55:08 +01001886 u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
1887 u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001888
Frank Praznikfa57a812014-04-14 10:11:33 -04001889 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001890
Frank Praznikfa57a812014-04-14 10:11:33 -04001891 if (sc->quirks & BUZZ_CONTROLLER) {
1892 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001893 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001894 name_len = strlen("::buzz#");
1895 name_fmt = "%s::buzz%d";
1896 /* Validate expected report characteristics. */
1897 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1898 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001899 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik221399b2015-05-05 20:47:32 -04001900 dualshock4_set_leds_from_id(sc);
1901 sc->led_state[3] = 1;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001902 sc->led_count = 4;
1903 memset(max_brightness, 255, 3);
1904 use_hw_blink[3] = 1;
1905 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001906 name_len = 0;
1907 name_fmt = "%s:%s";
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001908 } else if (sc->quirks & MOTION_CONTROLLER) {
1909 sc->led_count = 3;
1910 memset(max_brightness, 255, 3);
1911 use_ds4_names = 1;
1912 name_len = 0;
1913 name_fmt = "%s:%s";
Simon Wood4545ee02015-06-17 00:08:52 -06001914 } else if (sc->quirks & NAVIGATION_CONTROLLER) {
Pavel Machek1adf9042016-02-09 13:55:08 +01001915 static const u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00};
Simon Wood4545ee02015-06-17 00:08:52 -06001916
1917 memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds));
1918 sc->led_count = 1;
1919 memset(use_hw_blink, 1, 4);
1920 use_ds4_names = 0;
1921 name_len = strlen("::sony#");
1922 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001923 } else {
Frank Praznik221399b2015-05-05 20:47:32 -04001924 sixaxis_set_leds_from_id(sc);
Frank Praznikfa57a812014-04-14 10:11:33 -04001925 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001926 memset(use_hw_blink, 1, 4);
1927 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001928 name_len = strlen("::sony#");
1929 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001930 }
1931
Frank Praznikad142b92014-02-20 11:36:00 -05001932 /*
1933 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001934 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001935 * LEDs to on
1936 */
Frank Praznik221399b2015-05-05 20:47:32 -04001937 sony_set_leds(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001938
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001939 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001940
Frank Praznikfa57a812014-04-14 10:11:33 -04001941 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001942
Frank Praznikb3ed4582014-04-14 10:11:36 -04001943 if (use_ds4_names)
1944 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001945
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001946 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1947 if (!led) {
1948 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001949 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001950 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001951 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001952
1953 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001954 if (use_ds4_names)
1955 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1956 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001957 else
1958 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001959 led->name = name;
Frank Praznik221399b2015-05-05 20:47:32 -04001960 led->brightness = sc->led_state[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001961 led->max_brightness = max_brightness[n];
Frank Praznike6061c12017-02-08 13:58:43 -05001962 led->flags = LED_CORE_SUSPENDRESUME;
Sven Eckelmannc5382512013-11-19 20:26:30 +01001963 led->brightness_get = sony_led_get_brightness;
1964 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001965
Frank Praznikb3ed4582014-04-14 10:11:36 -04001966 if (use_hw_blink[n])
1967 led->blink_set = sony_led_blink_set;
1968
Frank Praznik80250872014-04-14 10:11:35 -04001969 sc->leds[n] = led;
1970
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001971 ret = led_classdev_register(&hdev->dev, led);
1972 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001973 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001974 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001975 kfree(led);
1976 goto error_leds;
1977 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001978 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001979
1980 return ret;
1981
Colin Leitnerf04d5142013-05-27 23:41:05 +02001982error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001983 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001984
Colin Leitnerf04d5142013-05-27 23:41:05 +02001985 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001986}
1987
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001988static void sixaxis_send_output_report(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001989{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001990 static const union sixaxis_output_report_01 default_report = {
Frank Praznik55d3b662014-04-14 10:11:32 -04001991 .buf = {
1992 0x01,
Scott Moreauad07b7a2016-01-13 07:40:43 -07001993 0x01, 0xff, 0x00, 0xff, 0x00,
Frank Praznik55d3b662014-04-14 10:11:32 -04001994 0x00, 0x00, 0x00, 0x00, 0x00,
1995 0xff, 0x27, 0x10, 0x00, 0x32,
1996 0xff, 0x27, 0x10, 0x00, 0x32,
1997 0xff, 0x27, 0x10, 0x00, 0x32,
1998 0xff, 0x27, 0x10, 0x00, 0x32,
1999 0x00, 0x00, 0x00, 0x00, 0x00
2000 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002001 };
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002002 struct sixaxis_output_report *report =
2003 (struct sixaxis_output_report *)sc->output_report_dmabuf;
2004 int n;
2005
2006 /* Initialize the report with default values */
2007 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002008
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002009#ifdef CONFIG_SONY_FF
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002010 report->rumble.right_motor_on = sc->right ? 1 : 0;
2011 report->rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002012#endif
2013
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002014 report->leds_bitmap |= sc->led_state[0] << 1;
2015 report->leds_bitmap |= sc->led_state[1] << 2;
2016 report->leds_bitmap |= sc->led_state[2] << 3;
2017 report->leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002018
Simon Wood88f65762014-04-14 10:11:37 -04002019 /* Set flag for all leds off, required for 3rd party INTEC controller */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002020 if ((report->leds_bitmap & 0x1E) == 0)
2021 report->leds_bitmap |= 0x20;
Simon Wood88f65762014-04-14 10:11:37 -04002022
Frank Praznikb3ed4582014-04-14 10:11:36 -04002023 /*
2024 * The LEDs in the report are indexed in reverse order to their
2025 * corresponding light on the controller.
2026 * Index 0 = LED 4, index 1 = LED 3, etc...
2027 *
2028 * In the case of both delay values being zero (blinking disabled) the
2029 * default report values should be used or the controller LED will be
2030 * always off.
2031 */
2032 for (n = 0; n < 4; n++) {
2033 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002034 report->led[3 - n].duty_off = sc->led_delay_off[n];
2035 report->led[3 - n].duty_on = sc->led_delay_on[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04002036 }
2037 }
2038
Pavel Machek1adf9042016-02-09 13:55:08 +01002039 hid_hw_raw_request(sc->hdev, report->report_id, (u8 *)report,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002040 sizeof(struct sixaxis_output_report),
2041 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002042}
2043
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002044static void dualshock4_send_output_report(struct sony_sc *sc)
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002045{
Frank Praznik0da8ea62014-01-16 21:42:51 -05002046 struct hid_device *hdev = sc->hdev;
Pavel Machek1adf9042016-02-09 13:55:08 +01002047 u8 *buf = sc->output_report_dmabuf;
Frank Praznik48220232014-02-05 20:03:44 -05002048 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05002049
Frank Praznikc4425c82016-09-22 20:18:10 -04002050 /*
Roderick Colenbrander786bd402017-03-24 15:17:49 -07002051 * NOTE: The lower 6 bits of buf[1] field of the Bluetooth report
2052 * control the interval at which Dualshock 4 reports data:
2053 * 0x00 - 1ms
2054 * 0x01 - 1ms
2055 * 0x02 - 2ms
2056 * 0x3E - 62ms
2057 * 0x3F - disabled
Frank Praznikc4425c82016-09-22 20:18:10 -04002058 */
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08002059 if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
Roderick Colenbrander5443a632016-10-07 12:39:35 -07002060 memset(buf, 0, DS4_OUTPUT_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05002061 buf[0] = 0x05;
Roderick Colenbrander9c4dcd72017-03-24 15:17:48 -07002062 buf[1] = 0x07; /* blink + LEDs + motor */
Frank Praznikfdcf105d2014-02-05 20:03:46 -05002063 offset = 4;
2064 } else {
Roderick Colenbrander5443a632016-10-07 12:39:35 -07002065 memset(buf, 0, DS4_OUTPUT_REPORT_0x11_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05002066 buf[0] = 0x11;
Roderick Colenbrander786bd402017-03-24 15:17:49 -07002067 buf[1] = 0xC0 /* HID + CRC */ | sc->ds4_bt_poll_interval;
Roderick Colenbrander9c4dcd72017-03-24 15:17:48 -07002068 buf[3] = 0x07; /* blink + LEDs + motor */
Frank Praznikfdcf105d2014-02-05 20:03:46 -05002069 offset = 6;
2070 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002071
2072#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05002073 buf[offset++] = sc->right;
2074 buf[offset++] = sc->left;
2075#else
2076 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002077#endif
2078
Frank Praznikb3ed4582014-04-14 10:11:36 -04002079 /* LED 3 is the global control */
2080 if (sc->led_state[3]) {
2081 buf[offset++] = sc->led_state[0];
2082 buf[offset++] = sc->led_state[1];
2083 buf[offset++] = sc->led_state[2];
2084 } else {
2085 offset += 3;
2086 }
2087
2088 /* If both delay values are zero the DualShock 4 disables blinking. */
2089 buf[offset++] = sc->led_delay_on[3];
2090 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05002091
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08002092 if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE))
Roderick Colenbrander5443a632016-10-07 12:39:35 -07002093 hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x05_SIZE);
Roderick Colenbranderc076f052016-10-07 12:39:37 -07002094 else {
2095 /* CRC generation */
2096 u8 bthdr = 0xA2;
2097 u32 crc;
2098
2099 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
2100 crc = ~crc32_le(crc, buf, DS4_OUTPUT_REPORT_0x11_SIZE-4);
2101 put_unaligned_le32(crc, &buf[74]);
2102 hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x11_SIZE);
2103 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002104}
2105
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002106static void motion_send_output_report(struct sony_sc *sc)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002107{
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002108 struct hid_device *hdev = sc->hdev;
2109 struct motion_output_report_02 *report =
2110 (struct motion_output_report_02 *)sc->output_report_dmabuf;
2111
Simon Wood41d2d422015-06-09 21:27:06 -06002112 memset(report, 0, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002113
2114 report->type = 0x02; /* set leds */
2115 report->r = sc->led_state[0];
2116 report->g = sc->led_state[1];
2117 report->b = sc->led_state[2];
2118
2119#ifdef CONFIG_SONY_FF
2120 report->rumble = max(sc->right, sc->left);
2121#endif
2122
Pavel Machek1adf9042016-02-09 13:55:08 +01002123 hid_hw_output_report(hdev, (u8 *)report, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002124}
2125
Frank Praznikdecd9462015-11-11 09:49:38 -05002126static inline void sony_send_output_report(struct sony_sc *sc)
2127{
2128 if (sc->send_output_report)
2129 sc->send_output_report(sc);
2130}
2131
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002132static void sony_state_worker(struct work_struct *work)
2133{
2134 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Antonio Ospiteef916ef2016-02-09 13:55:07 +01002135
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002136 sc->send_output_report(sc);
2137}
2138
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002139static int sony_allocate_output_report(struct sony_sc *sc)
2140{
Simon Wood4545ee02015-06-17 00:08:52 -06002141 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2142 (sc->quirks & NAVIGATION_CONTROLLER))
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002143 sc->output_report_dmabuf =
2144 kmalloc(sizeof(union sixaxis_output_report_01),
2145 GFP_KERNEL);
2146 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
Roderick Colenbrander5443a632016-10-07 12:39:35 -07002147 sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x11_SIZE,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002148 GFP_KERNEL);
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08002149 else if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE))
Roderick Colenbrander5443a632016-10-07 12:39:35 -07002150 sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x05_SIZE,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002151 GFP_KERNEL);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002152 else if (sc->quirks & MOTION_CONTROLLER)
Simon Wood41d2d422015-06-09 21:27:06 -06002153 sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE,
2154 GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002155 else
2156 return 0;
2157
2158 if (!sc->output_report_dmabuf)
2159 return -ENOMEM;
2160
2161 return 0;
2162}
2163
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002164#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002165static int sony_play_effect(struct input_dev *dev, void *data,
2166 struct ff_effect *effect)
2167{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002168 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002169 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002170
2171 if (effect->type != FF_RUMBLE)
2172 return 0;
2173
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002174 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002175 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002176
Roderick Colenbrander3a60a132017-03-07 15:45:05 -08002177 sony_schedule_work(sc, SONY_WORKER_STATE);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002178 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002179}
2180
Frank Praznikfa57a812014-04-14 10:11:33 -04002181static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002182{
Alan Stern7b5e3ad2019-10-03 14:53:59 -04002183 struct hid_input *hidinput;
2184 struct input_dev *input_dev;
2185
2186 if (list_empty(&sc->hdev->inputs)) {
2187 hid_err(sc->hdev, "no inputs found\n");
2188 return -ENODEV;
2189 }
2190 hidinput = list_entry(sc->hdev->inputs.next, struct hid_input, list);
2191 input_dev = hidinput->input;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002192
2193 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
2194 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
2195}
2196
2197#else
Frank Praznikfa57a812014-04-14 10:11:33 -04002198static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002199{
2200 return 0;
2201}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002202
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002203#endif
2204
Frank Praznikd902f472014-01-27 10:17:36 -05002205static int sony_battery_get_property(struct power_supply *psy,
2206 enum power_supply_property psp,
2207 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002208{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002209 struct sony_sc *sc = power_supply_get_drvdata(psy);
Frank Praznikd902f472014-01-27 10:17:36 -05002210 unsigned long flags;
2211 int ret = 0;
2212 u8 battery_charging, battery_capacity, cable_state;
2213
2214 spin_lock_irqsave(&sc->lock, flags);
2215 battery_charging = sc->battery_charging;
2216 battery_capacity = sc->battery_capacity;
2217 cable_state = sc->cable_state;
2218 spin_unlock_irqrestore(&sc->lock, flags);
2219
2220 switch (psp) {
2221 case POWER_SUPPLY_PROP_PRESENT:
2222 val->intval = 1;
2223 break;
2224 case POWER_SUPPLY_PROP_SCOPE:
2225 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
2226 break;
2227 case POWER_SUPPLY_PROP_CAPACITY:
2228 val->intval = battery_capacity;
2229 break;
2230 case POWER_SUPPLY_PROP_STATUS:
2231 if (battery_charging)
2232 val->intval = POWER_SUPPLY_STATUS_CHARGING;
2233 else
2234 if (battery_capacity == 100 && cable_state)
2235 val->intval = POWER_SUPPLY_STATUS_FULL;
2236 else
2237 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
2238 break;
2239 default:
2240 ret = -EINVAL;
2241 break;
2242 }
2243 return ret;
2244}
2245
Frank Praznik0f398232016-09-22 20:18:08 -04002246static int sony_battery_probe(struct sony_sc *sc, int append_dev_id)
Frank Praznikd902f472014-01-27 10:17:36 -05002247{
Frank Praznik0f398232016-09-22 20:18:08 -04002248 const char *battery_str_fmt = append_dev_id ?
2249 "sony_controller_battery_%pMR_%i" :
2250 "sony_controller_battery_%pMR";
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002251 struct power_supply_config psy_cfg = { .drv_data = sc, };
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002252 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05002253 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002254
Frank Praznikad142b92014-02-20 11:36:00 -05002255 /*
2256 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05002257 * if the battery is polled before the first device report is received.
2258 */
2259 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002260
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002261 sc->battery_desc.properties = sony_battery_props;
2262 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
2263 sc->battery_desc.get_property = sony_battery_get_property;
2264 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
2265 sc->battery_desc.use_for_apm = 0;
Frank Praznik0f398232016-09-22 20:18:08 -04002266 sc->battery_desc.name = kasprintf(GFP_KERNEL, battery_str_fmt,
2267 sc->mac_address, sc->device_id);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002268 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05002269 return -ENOMEM;
2270
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002271 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
2272 &psy_cfg);
2273 if (IS_ERR(sc->battery)) {
2274 ret = PTR_ERR(sc->battery);
Frank Praznikd902f472014-01-27 10:17:36 -05002275 hid_err(hdev, "Unable to register battery device\n");
2276 goto err_free;
2277 }
2278
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002279 power_supply_powers(sc->battery, &hdev->dev);
Frank Praznikd902f472014-01-27 10:17:36 -05002280 return 0;
2281
2282err_free:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002283 kfree(sc->battery_desc.name);
2284 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05002285 return ret;
2286}
2287
2288static void sony_battery_remove(struct sony_sc *sc)
2289{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002290 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05002291 return;
2292
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002293 power_supply_unregister(sc->battery);
2294 kfree(sc->battery_desc.name);
2295 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05002296}
2297
Frank Praznikd2d782f2014-02-20 11:36:03 -05002298/*
2299 * If a controller is plugged in via USB while already connected via Bluetooth
2300 * it will show up as two devices. A global list of connected controllers and
2301 * their MAC addresses is maintained to ensure that a device is only connected
2302 * once.
Frank Praznik0f398232016-09-22 20:18:08 -04002303 *
2304 * Some USB-only devices masquerade as Sixaxis controllers and all have the
2305 * same dummy Bluetooth address, so a comparison of the connection type is
2306 * required. Devices are only rejected in the case where two devices have
2307 * matching Bluetooth addresses on different bus types.
Frank Praznikd2d782f2014-02-20 11:36:03 -05002308 */
Frank Praznik0f398232016-09-22 20:18:08 -04002309static inline int sony_compare_connection_type(struct sony_sc *sc0,
2310 struct sony_sc *sc1)
2311{
2312 const int sc0_not_bt = !(sc0->quirks & SONY_BT_DEVICE);
2313 const int sc1_not_bt = !(sc1->quirks & SONY_BT_DEVICE);
2314
2315 return sc0_not_bt == sc1_not_bt;
2316}
2317
Frank Praznikd2d782f2014-02-20 11:36:03 -05002318static int sony_check_add_dev_list(struct sony_sc *sc)
2319{
2320 struct sony_sc *entry;
2321 unsigned long flags;
2322 int ret;
2323
2324 spin_lock_irqsave(&sony_dev_list_lock, flags);
2325
2326 list_for_each_entry(entry, &sony_device_list, list_node) {
2327 ret = memcmp(sc->mac_address, entry->mac_address,
2328 sizeof(sc->mac_address));
2329 if (!ret) {
Frank Praznik0f398232016-09-22 20:18:08 -04002330 if (sony_compare_connection_type(sc, entry)) {
2331 ret = 1;
2332 } else {
2333 ret = -EEXIST;
2334 hid_info(sc->hdev,
2335 "controller with MAC address %pMR already connected\n",
Frank Praznikd2d782f2014-02-20 11:36:03 -05002336 sc->mac_address);
Frank Praznik0f398232016-09-22 20:18:08 -04002337 }
Frank Praznikd2d782f2014-02-20 11:36:03 -05002338 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002339 }
2340 }
2341
Frank Praznikd2d782f2014-02-20 11:36:03 -05002342 ret = 0;
2343 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002344
Frank Praznikd2d782f2014-02-20 11:36:03 -05002345unlock:
2346 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2347 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002348}
2349
Frank Praznikd2d782f2014-02-20 11:36:03 -05002350static void sony_remove_dev_list(struct sony_sc *sc)
2351{
2352 unsigned long flags;
2353
2354 if (sc->list_node.next) {
2355 spin_lock_irqsave(&sony_dev_list_lock, flags);
2356 list_del(&(sc->list_node));
2357 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2358 }
2359}
2360
2361static int sony_get_bt_devaddr(struct sony_sc *sc)
2362{
2363 int ret;
2364
2365 /* HIDP stores the device MAC address as a string in the uniq field. */
2366 ret = strlen(sc->hdev->uniq);
2367 if (ret != 17)
2368 return -EINVAL;
2369
2370 ret = sscanf(sc->hdev->uniq,
2371 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2372 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
2373 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
2374
2375 if (ret != 6)
2376 return -EINVAL;
2377
2378 return 0;
2379}
2380
2381static int sony_check_add(struct sony_sc *sc)
2382{
Pavel Machek1adf9042016-02-09 13:55:08 +01002383 u8 *buf = NULL;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002384 int n, ret;
2385
2386 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
Simon Wood12e9a6d2015-06-09 21:27:05 -06002387 (sc->quirks & MOTION_CONTROLLER_BT) ||
Simon Wood4545ee02015-06-17 00:08:52 -06002388 (sc->quirks & NAVIGATION_CONTROLLER_BT) ||
Frank Praznikd2d782f2014-02-20 11:36:03 -05002389 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
2390 /*
2391 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
2392 * address from the uniq string where HIDP stores it.
2393 * As uniq cannot be guaranteed to be a MAC address in all cases
2394 * a failure of this function should not prevent the connection.
2395 */
2396 if (sony_get_bt_devaddr(sc) < 0) {
2397 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
2398 return 0;
2399 }
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08002400 } else if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
Roderick Colenbrander5443a632016-10-07 12:39:35 -07002401 buf = kmalloc(DS4_FEATURE_REPORT_0x81_SIZE, GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002402 if (!buf)
2403 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002404
2405 /*
2406 * The MAC address of a DS4 controller connected via USB can be
2407 * retrieved with feature report 0x81. The address begins at
2408 * offset 1.
2409 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002410 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
Roderick Colenbrander5443a632016-10-07 12:39:35 -07002411 DS4_FEATURE_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002412 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002413
Roderick Colenbrander5443a632016-10-07 12:39:35 -07002414 if (ret != DS4_FEATURE_REPORT_0x81_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002415 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002416 ret = ret < 0 ? ret : -EINVAL;
2417 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002418 }
2419
2420 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
Roderick Colenbranderfe9724e2016-12-08 19:09:51 -08002421
2422 snprintf(sc->hdev->uniq, sizeof(sc->hdev->uniq),
2423 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2424 sc->mac_address[5], sc->mac_address[4],
2425 sc->mac_address[3], sc->mac_address[2],
2426 sc->mac_address[1], sc->mac_address[0]);
Simon Wood4545ee02015-06-17 00:08:52 -06002427 } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2428 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002429 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2430 if (!buf)
2431 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002432
2433 /*
2434 * The MAC address of a Sixaxis controller connected via USB can
2435 * be retrieved with feature report 0xf2. The address begins at
2436 * offset 4.
2437 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002438 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2439 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2440 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002441
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002442 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002443 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002444 ret = ret < 0 ? ret : -EINVAL;
2445 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002446 }
2447
2448 /*
2449 * The Sixaxis device MAC in the report is big-endian and must
2450 * be byte-swapped.
2451 */
2452 for (n = 0; n < 6; n++)
2453 sc->mac_address[5-n] = buf[4+n];
Roderick Colenbrander83df3ca2017-03-24 15:17:45 -07002454
2455 snprintf(sc->hdev->uniq, sizeof(sc->hdev->uniq),
2456 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2457 sc->mac_address[5], sc->mac_address[4],
2458 sc->mac_address[3], sc->mac_address[2],
2459 sc->mac_address[1], sc->mac_address[0]);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002460 } else {
2461 return 0;
2462 }
2463
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002464 ret = sony_check_add_dev_list(sc);
2465
2466out_free:
2467
2468 kfree(buf);
2469
2470 return ret;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002471}
2472
Frank Praznik80250872014-04-14 10:11:35 -04002473static int sony_set_device_id(struct sony_sc *sc)
2474{
2475 int ret;
2476
2477 /*
2478 * Only DualShock 4 or Sixaxis controllers get an id.
2479 * All others are set to -1.
2480 */
2481 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2482 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2483 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2484 GFP_KERNEL);
2485 if (ret < 0) {
2486 sc->device_id = -1;
2487 return ret;
2488 }
2489 sc->device_id = ret;
2490 } else {
2491 sc->device_id = -1;
2492 }
2493
2494 return 0;
2495}
2496
2497static void sony_release_device_id(struct sony_sc *sc)
2498{
2499 if (sc->device_id >= 0) {
2500 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2501 sc->device_id = -1;
2502 }
2503}
2504
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002505static inline void sony_init_output_report(struct sony_sc *sc,
Antonio Ospite09593e32016-02-09 13:55:06 +01002506 void (*send_output_report)(struct sony_sc *))
Frank Praznik46262042014-04-14 10:11:31 -04002507{
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002508 sc->send_output_report = send_output_report;
2509
Roderick Colenbrander3a60a132017-03-07 15:45:05 -08002510 if (!sc->state_worker_initialized)
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002511 INIT_WORK(&sc->state_worker, sony_state_worker);
Frank Praznik46262042014-04-14 10:11:31 -04002512
Roderick Colenbrander3a60a132017-03-07 15:45:05 -08002513 sc->state_worker_initialized = 1;
Frank Praznik46262042014-04-14 10:11:31 -04002514}
2515
2516static inline void sony_cancel_work_sync(struct sony_sc *sc)
2517{
Roderick Colenbrander5bf3ad52019-08-02 14:27:28 -07002518 unsigned long flags;
2519
Roderick Colenbrander72dbb852017-03-07 15:45:06 -08002520 if (sc->hotplug_worker_initialized)
2521 cancel_work_sync(&sc->hotplug_worker);
Roderick Colenbrander5bf3ad52019-08-02 14:27:28 -07002522 if (sc->state_worker_initialized) {
2523 spin_lock_irqsave(&sc->lock, flags);
2524 sc->state_worker_initialized = 0;
2525 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznik46262042014-04-14 10:11:31 -04002526 cancel_work_sync(&sc->state_worker);
Roderick Colenbrander5bf3ad52019-08-02 14:27:28 -07002527 }
Frank Praznik46262042014-04-14 10:11:31 -04002528}
Frank Praznikd2d782f2014-02-20 11:36:03 -05002529
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002530static int sony_input_configured(struct hid_device *hdev,
2531 struct hid_input *hidinput)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002532{
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002533 struct sony_sc *sc = hid_get_drvdata(hdev);
Frank Praznik0f398232016-09-22 20:18:08 -04002534 int append_dev_id;
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002535 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002536
Frank Praznik80250872014-04-14 10:11:35 -04002537 ret = sony_set_device_id(sc);
2538 if (ret < 0) {
2539 hid_err(hdev, "failed to allocate the device id\n");
2540 goto err_stop;
2541 }
2542
Roderick Colenbranderef1f52b2017-03-07 15:45:11 -08002543 ret = append_dev_id = sony_check_add(sc);
2544 if (ret < 0)
2545 goto err_stop;
2546
Frank Praznik131a8a92015-05-05 20:47:28 -04002547 ret = sony_allocate_output_report(sc);
2548 if (ret < 0) {
2549 hid_err(hdev, "failed to allocate the output report buffer\n");
2550 goto err_stop;
2551 }
2552
Roderick Colenbrander89e9ccd2017-03-07 15:45:10 -08002553 if (sc->quirks & NAVIGATION_CONTROLLER_USB) {
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002554 /*
2555 * The Sony Sixaxis does not handle HID Output Reports on the
2556 * Interrupt EP like it could, so we need to force HID Output
2557 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2558 *
2559 * There is also another issue about HID Output Reports via USB,
2560 * the Sixaxis does not want the report_id as part of the data
2561 * packet, so we have to discard buf[0] when sending the actual
2562 * control message, even for numbered reports, humpf!
Frank Praznik2a242932016-09-22 20:18:09 -04002563 *
2564 * Additionally, the Sixaxis on USB isn't properly initialized
2565 * until the PS logo button is pressed and as such won't retain
2566 * any state set by an output report, so the initial
2567 * configuration report is deferred until the first input
2568 * report arrives.
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002569 */
2570 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2571 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
Frank Praznik2a242932016-09-22 20:18:09 -04002572 sc->defer_initialization = 1;
Roderick Colenbrander7bfac192017-03-07 15:45:09 -08002573
Antonio Ospite816651a2010-05-03 22:15:55 +02002574 ret = sixaxis_set_operational_usb(hdev);
Roderick Colenbrander7bfac192017-03-07 15:45:09 -08002575 if (ret < 0) {
2576 hid_err(hdev, "Failed to set controller into operational mode\n");
2577 goto err_stop;
2578 }
2579
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002580 sony_init_output_report(sc, sixaxis_send_output_report);
Roderick Colenbrander89e9ccd2017-03-07 15:45:10 -08002581 } else if (sc->quirks & NAVIGATION_CONTROLLER_BT) {
2582 /*
2583 * The Navigation controller wants output reports sent on the ctrl
2584 * endpoint when connected via Bluetooth.
2585 */
2586 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2587
2588 ret = sixaxis_set_operational_bt(hdev);
2589 if (ret < 0) {
2590 hid_err(hdev, "Failed to set controller into operational mode\n");
2591 goto err_stop;
2592 }
2593
2594 sony_init_output_report(sc, sixaxis_send_output_report);
2595 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
2596 /*
2597 * The Sony Sixaxis does not handle HID Output Reports on the
2598 * Interrupt EP and the device only becomes active when the
2599 * PS button is pressed. See comment for Navigation controller
2600 * above for more details.
2601 */
2602 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2603 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
2604 sc->defer_initialization = 1;
2605
2606 ret = sixaxis_set_operational_usb(hdev);
2607 if (ret < 0) {
2608 hid_err(hdev, "Failed to set controller into operational mode\n");
2609 goto err_stop;
2610 }
2611
2612 ret = sony_register_sensors(sc);
2613 if (ret) {
2614 hid_err(sc->hdev,
2615 "Unable to initialize motion sensors: %d\n", ret);
2616 goto err_stop;
2617 }
2618
2619 sony_init_output_report(sc, sixaxis_send_output_report);
2620 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
Frank Praznik2078b9b2014-03-15 09:41:16 -04002621 /*
2622 * The Sixaxis wants output reports sent on the ctrl endpoint
2623 * when connected via Bluetooth.
2624 */
2625 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Roderick Colenbrander7bfac192017-03-07 15:45:09 -08002626
Antonio Ospite816651a2010-05-03 22:15:55 +02002627 ret = sixaxis_set_operational_bt(hdev);
Roderick Colenbrander7bfac192017-03-07 15:45:09 -08002628 if (ret < 0) {
2629 hid_err(hdev, "Failed to set controller into operational mode\n");
2630 goto err_stop;
2631 }
2632
Roderick Colenbrander89e9ccd2017-03-07 15:45:10 -08002633 ret = sony_register_sensors(sc);
2634 if (ret) {
2635 hid_err(sc->hdev,
2636 "Unable to initialize motion sensors: %d\n", ret);
2637 goto err_stop;
2638 }
2639
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002640 sony_init_output_report(sc, sixaxis_send_output_report);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002641 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Roderick Colenbrander9b70faa2017-03-07 15:45:01 -08002642 ret = dualshock4_get_calibration_data(sc);
2643 if (ret < 0) {
2644 hid_err(hdev, "Failed to get calibration data from Dualshock 4\n");
2645 goto err_stop;
Frank Praznik68330d82014-02-05 20:03:49 -05002646 }
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002647
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002648 /*
2649 * The Dualshock 4 touchpad supports 2 touches and has a
2650 * resolution of 1920x942 (44.86 dots/mm).
2651 */
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08002652 ret = sony_register_touchpad(sc, 2, 1920, 942);
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002653 if (ret) {
2654 hid_err(sc->hdev,
2655 "Unable to initialize multi-touch slots: %d\n",
2656 ret);
Roderick Colenbranderf2217fd2016-12-08 19:09:50 -08002657 goto err_stop;
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002658 }
2659
Roderick Colenbrander6f561e92017-03-07 15:45:00 -08002660 ret = sony_register_sensors(sc);
2661 if (ret) {
2662 hid_err(sc->hdev,
2663 "Unable to initialize motion sensors: %d\n", ret);
2664 goto err_stop;
2665 }
2666
Roderick Colenbrander786bd402017-03-24 15:17:49 -07002667 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT) {
2668 sc->ds4_bt_poll_interval = DS4_BT_DEFAULT_POLL_INTERVAL_MS;
2669 ret = device_create_file(&sc->hdev->dev, &dev_attr_bt_poll_interval);
2670 if (ret)
2671 hid_warn(sc->hdev,
2672 "can't create sysfs bt_poll_interval attribute err: %d\n",
2673 ret);
2674 }
2675
Roderick Colenbrander72dbb852017-03-07 15:45:06 -08002676 if (sc->quirks & DUALSHOCK4_DONGLE) {
2677 INIT_WORK(&sc->hotplug_worker, dualshock4_calibration_work);
2678 sc->hotplug_worker_initialized = 1;
2679 sc->ds4_dongle_state = DONGLE_DISCONNECTED;
2680 }
2681
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002682 sony_init_output_report(sc, dualshock4_send_output_report);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002683 } else if (sc->quirks & MOTION_CONTROLLER) {
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002684 sony_init_output_report(sc, motion_send_output_report);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002685 } else {
2686 ret = 0;
2687 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00002688
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002689 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002690 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002691 if (ret < 0)
2692 goto err_stop;
2693 }
2694
Frank Praznikd902f472014-01-27 10:17:36 -05002695 if (sc->quirks & SONY_BATTERY_SUPPORT) {
Frank Praznik0f398232016-09-22 20:18:08 -04002696 ret = sony_battery_probe(sc, append_dev_id);
Frank Praznikd902f472014-01-27 10:17:36 -05002697 if (ret < 0)
2698 goto err_stop;
2699
2700 /* Open the device to receive reports with battery info */
2701 ret = hid_hw_open(hdev);
2702 if (ret < 0) {
2703 hid_err(hdev, "hw open failed\n");
2704 goto err_stop;
2705 }
2706 }
2707
Frank Praznikc8de9db2014-02-20 11:36:01 -05002708 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002709 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05002710 if (ret < 0)
2711 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05002712 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002713
Jiri Slabybd28ce02008-06-25 23:47:04 +02002714 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05002715err_close:
2716 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002717err_stop:
Roderick Colenbrander786bd402017-03-24 15:17:49 -07002718 /* Piggy back on the default ds4_bt_ poll_interval to determine
2719 * if we need to remove the file as we don't know for sure if we
2720 * executed that logic.
2721 */
2722 if (sc->ds4_bt_poll_interval)
2723 device_remove_file(&sc->hdev->dev, &dev_attr_bt_poll_interval);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002724 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002725 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002726 if (sc->quirks & SONY_BATTERY_SUPPORT)
2727 sony_battery_remove(sc);
Roderick Colenbrandere40b52f2017-02-24 16:14:15 -08002728 if (sc->touchpad)
2729 sony_unregister_touchpad(sc);
Roderick Colenbrander71120842017-03-24 15:17:50 -07002730 if (sc->sensor_dev)
2731 sony_unregister_sensors(sc);
Frank Praznik46262042014-04-14 10:11:31 -04002732 sony_cancel_work_sync(sc);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002733 kfree(sc->output_report_dmabuf);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002734 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04002735 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002736 return ret;
2737}
2738
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002739static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2740{
2741 int ret;
2742 unsigned long quirks = id->driver_data;
2743 struct sony_sc *sc;
2744 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2745
2746 if (!strcmp(hdev->name, "FutureMax Dance Mat"))
2747 quirks |= FUTUREMAX_DANCE_MAT;
2748
2749 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
2750 if (sc == NULL) {
2751 hid_err(hdev, "can't alloc sony descriptor\n");
2752 return -ENOMEM;
2753 }
2754
2755 spin_lock_init(&sc->lock);
2756
2757 sc->quirks = quirks;
2758 hid_set_drvdata(hdev, sc);
2759 sc->hdev = hdev;
2760
2761 ret = hid_parse(hdev);
2762 if (ret) {
2763 hid_err(hdev, "parse failed\n");
2764 return ret;
2765 }
2766
2767 if (sc->quirks & VAIO_RDESC_CONSTANT)
2768 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2769 else if (sc->quirks & SIXAXIS_CONTROLLER)
2770 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2771
Roderick Colenbranderbe985302017-03-07 15:45:08 -08002772 /* Patch the hw version on DS3/4 compatible devices, so applications can
Roderick Colenbrander687aa022016-11-23 14:07:08 -08002773 * distinguish between the default HID mappings and the mappings defined
2774 * by the Linux game controller spec. This is important for the SDL2
2775 * library, which has a game controller database, which uses device ids
2776 * in combination with version as a key.
2777 */
Roderick Colenbranderbe985302017-03-07 15:45:08 -08002778 if (sc->quirks & (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER))
Roderick Colenbrander687aa022016-11-23 14:07:08 -08002779 hdev->version |= 0x8000;
2780
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002781 ret = hid_hw_start(hdev, connect_mask);
2782 if (ret) {
2783 hid_err(hdev, "hw start failed\n");
2784 return ret;
2785 }
2786
Roderick Colenbrandere7914622016-11-23 14:07:06 -08002787 /* sony_input_configured can fail, but this doesn't result
2788 * in hid_hw_start failures (intended). Check whether
2789 * the HID layer claimed the device else fail.
2790 * We don't know the actual reason for the failure, most
2791 * likely it is due to EEXIST in case of double connection
2792 * of USB and Bluetooth, but could have been due to ENOMEM
2793 * or other reasons as well.
2794 */
2795 if (!(hdev->claimed & HID_CLAIMED_INPUT)) {
2796 hid_err(hdev, "failed to claim input\n");
Roderick Colenbrander56cfa642019-09-04 14:22:11 -07002797 hid_hw_stop(hdev);
Roderick Colenbrandere7914622016-11-23 14:07:06 -08002798 return -ENODEV;
2799 }
2800
Roderick Colenbrander5a3ee242016-10-07 12:39:34 -07002801 return ret;
2802}
2803
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002804static void sony_remove(struct hid_device *hdev)
2805{
Colin Leitnerf04d5142013-05-27 23:41:05 +02002806 struct sony_sc *sc = hid_get_drvdata(hdev);
2807
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08002808 hid_hw_close(hdev);
2809
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002810 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002811 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02002812
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08002813 if (sc->quirks & SONY_BATTERY_SUPPORT)
Frank Praznikd902f472014-01-27 10:17:36 -05002814 sony_battery_remove(sc);
Roderick Colenbrander42027ee2016-11-23 14:07:07 -08002815
2816 if (sc->touchpad)
2817 sony_unregister_touchpad(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002818
Roderick Colenbrander6f561e92017-03-07 15:45:00 -08002819 if (sc->sensor_dev)
2820 sony_unregister_sensors(sc);
2821
Roderick Colenbrander786bd402017-03-24 15:17:49 -07002822 if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
2823 device_remove_file(&sc->hdev->dev, &dev_attr_bt_poll_interval);
2824
Frank Praznik46262042014-04-14 10:11:31 -04002825 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002826
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002827 kfree(sc->output_report_dmabuf);
2828
Frank Praznikd2d782f2014-02-20 11:36:03 -05002829 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002830
Frank Praznik80250872014-04-14 10:11:35 -04002831 sony_release_device_id(sc);
2832
Jiri Slabybd28ce02008-06-25 23:47:04 +02002833 hid_hw_stop(hdev);
2834}
2835
Frank Praznikdecd9462015-11-11 09:49:38 -05002836#ifdef CONFIG_PM
2837
2838static int sony_suspend(struct hid_device *hdev, pm_message_t message)
2839{
Frank Praznike6061c12017-02-08 13:58:43 -05002840#ifdef CONFIG_SONY_FF
2841
2842 /* On suspend stop any running force-feedback events */
2843 if (SONY_FF_SUPPORT) {
Frank Praznikdecd9462015-11-11 09:49:38 -05002844 struct sony_sc *sc = hid_get_drvdata(hdev);
2845
Frank Praznikdecd9462015-11-11 09:49:38 -05002846 sc->left = sc->right = 0;
Frank Praznikdecd9462015-11-11 09:49:38 -05002847 sony_send_output_report(sc);
2848 }
2849
Frank Praznike6061c12017-02-08 13:58:43 -05002850#endif
Frank Praznikdecd9462015-11-11 09:49:38 -05002851 return 0;
2852}
2853
2854static int sony_resume(struct hid_device *hdev)
2855{
Frank Praznike6061c12017-02-08 13:58:43 -05002856 struct sony_sc *sc = hid_get_drvdata(hdev);
Frank Praznikdecd9462015-11-11 09:49:38 -05002857
Frank Praznike6061c12017-02-08 13:58:43 -05002858 /*
2859 * The Sixaxis and navigation controllers on USB need to be
2860 * reinitialized on resume or they won't behave properly.
2861 */
2862 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2863 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
2864 sixaxis_set_operational_usb(sc->hdev);
2865 sc->defer_initialization = 1;
Frank Praznikdecd9462015-11-11 09:49:38 -05002866 }
2867
2868 return 0;
2869}
2870
2871#endif
2872
Jiri Slabybd28ce02008-06-25 23:47:04 +02002873static const struct hid_device_id sony_devices[] = {
2874 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2875 .driver_data = SIXAXIS_CONTROLLER_USB },
2876 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
Simon Wood4545ee02015-06-17 00:08:52 -06002877 .driver_data = NAVIGATION_CONTROLLER_USB },
Simon Wood6eabaaa2015-06-17 00:08:51 -06002878 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
Simon Wood4545ee02015-06-17 00:08:52 -06002879 .driver_data = NAVIGATION_CONTROLLER_BT },
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002880 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002881 .driver_data = MOTION_CONTROLLER_USB },
Simon Wooda4afa852015-06-03 09:45:19 -06002882 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002883 .driver_data = MOTION_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002884 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2885 .driver_data = SIXAXIS_CONTROLLER_BT },
2886 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2887 .driver_data = VAIO_RDESC_CONSTANT },
2888 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2889 .driver_data = VAIO_RDESC_CONSTANT },
Antonio Ospiteef916ef2016-02-09 13:55:07 +01002890 /*
2891 * Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2892 * Logitech joystick from the device descriptor.
2893 */
Jiri Slabybd28ce02008-06-25 23:47:04 +02002894 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2895 .driver_data = BUZZ_CONTROLLER },
2896 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2897 .driver_data = BUZZ_CONTROLLER },
2898 /* PS3 BD Remote Control */
2899 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2900 .driver_data = PS3REMOTE },
2901 /* Logitech Harmony Adapter for PS3 */
2902 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2903 .driver_data = PS3REMOTE },
Frank Praznik68a49e52014-11-12 14:52:28 -05002904 /* SMK-Link PS3 BD Remote Control */
2905 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2906 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002907 /* Sony Dualshock 4 controllers for PS4 */
2908 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002909 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002910 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002911 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Roderick Colenbranderf5ff43c2016-10-07 12:39:40 -07002912 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
2913 .driver_data = DUALSHOCK4_CONTROLLER_USB },
2914 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
2915 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Roderick Colenbrander72386b22016-11-23 14:07:11 -08002916 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE),
Roderick Colenbrander8f1d4702017-03-07 15:45:04 -08002917 .driver_data = DUALSHOCK4_DONGLE },
Scott Moreau74500cc2016-01-13 07:40:42 -07002918 /* Nyko Core Controller for PS3 */
2919 { HID_USB_DEVICE(USB_VENDOR_ID_SINO_LITE, USB_DEVICE_ID_SINO_LITE_CONTROLLER),
2920 .driver_data = SIXAXIS_CONTROLLER_USB | SINO_LITE_CONTROLLER },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002921 { }
2922};
2923MODULE_DEVICE_TABLE(hid, sony_devices);
2924
2925static struct hid_driver sony_driver = {
Frank Praznikce8efc32014-09-18 21:15:01 -04002926 .name = "sony",
2927 .id_table = sony_devices,
2928 .input_mapping = sony_mapping,
2929 .input_configured = sony_input_configured,
2930 .probe = sony_probe,
2931 .remove = sony_remove,
2932 .report_fixup = sony_report_fixup,
Frank Praznikdecd9462015-11-11 09:49:38 -05002933 .raw_event = sony_raw_event,
2934
2935#ifdef CONFIG_PM
2936 .suspend = sony_suspend,
2937 .resume = sony_resume,
2938 .reset_resume = sony_resume,
2939#endif
Jiri Slabybd28ce02008-06-25 23:47:04 +02002940};
Frank Praznik80250872014-04-14 10:11:35 -04002941
2942static int __init sony_init(void)
2943{
2944 dbg_hid("Sony:%s\n", __func__);
2945
2946 return hid_register_driver(&sony_driver);
2947}
2948
2949static void __exit sony_exit(void)
2950{
2951 dbg_hid("Sony:%s\n", __func__);
2952
Frank Praznik80250872014-04-14 10:11:35 -04002953 hid_unregister_driver(&sony_driver);
Antonio Ospite6c400652015-02-16 22:58:24 +01002954 ida_destroy(&sony_device_id_allocator);
Frank Praznik80250872014-04-14 10:11:35 -04002955}
2956module_init(sony_init);
2957module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002958
2959MODULE_LICENSE("GPL");