blob: 38674f51be8e1bdd61c4dd601ad319c63ebe1f87 [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 Colenbrander49b9ca62016-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 Colenbrander35f436c2017-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 Colenbrander35f436c2017-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 Wood12e9a6d72015-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
Simon Woodb2723eb2015-06-17 00:08:53 -0600176/* PS/3 Navigation controller */
Pavel Machek1adf9042016-02-09 13:55:08 +0100177static u8 navigation_rdesc[] = {
Simon Woodb2723eb2015-06-17 00:08:53 -0600178 0x05, 0x01, /* Usage Page (Desktop), */
Antonio Ospited5421762016-01-28 18:23:43 +0100179 0x09, 0x04, /* Usage (Joystick), */
Simon Woodb2723eb2015-06-17 00:08:53 -0600180 0xA1, 0x01, /* Collection (Application), */
181 0xA1, 0x02, /* Collection (Logical), */
182 0x85, 0x01, /* Report ID (1), */
183 0x75, 0x08, /* Report Size (8), */
184 0x95, 0x01, /* Report Count (1), */
185 0x15, 0x00, /* Logical Minimum (0), */
186 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
187 0x81, 0x03, /* Input (Constant, Variable), */
188 0x75, 0x01, /* Report Size (1), */
189 0x95, 0x13, /* Report Count (19), */
190 0x15, 0x00, /* Logical Minimum (0), */
191 0x25, 0x01, /* Logical Maximum (1), */
192 0x35, 0x00, /* Physical Minimum (0), */
193 0x45, 0x01, /* Physical Maximum (1), */
194 0x05, 0x09, /* Usage Page (Button), */
195 0x19, 0x01, /* Usage Minimum (01h), */
196 0x29, 0x13, /* Usage Maximum (13h), */
197 0x81, 0x02, /* Input (Variable), */
198 0x75, 0x01, /* Report Size (1), */
199 0x95, 0x0D, /* Report Count (13), */
200 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
201 0x81, 0x03, /* Input (Constant, Variable), */
202 0x15, 0x00, /* Logical Minimum (0), */
203 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
204 0x05, 0x01, /* Usage Page (Desktop), */
205 0x09, 0x01, /* Usage (Pointer), */
206 0xA1, 0x00, /* Collection (Physical), */
207 0x75, 0x08, /* Report Size (8), */
208 0x95, 0x02, /* Report Count (2), */
209 0x35, 0x00, /* Physical Minimum (0), */
210 0x46, 0xFF, 0x00, /* Physical Maximum (255), */
211 0x09, 0x30, /* Usage (X), */
212 0x09, 0x31, /* Usage (Y), */
213 0x81, 0x02, /* Input (Variable), */
214 0xC0, /* End Collection, */
215 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
216 0x95, 0x06, /* Report Count (6), */
217 0x81, 0x03, /* Input (Constant, Variable), */
218 0x05, 0x01, /* Usage Page (Desktop), */
219 0x75, 0x08, /* Report Size (8), */
220 0x95, 0x05, /* Report Count (5), */
221 0x09, 0x01, /* Usage (Pointer), */
222 0x81, 0x02, /* Input (Variable), */
223 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
Simon Wood2259b5bb2015-07-10 00:10:21 -0600224 0x95, 0x01, /* Report Count (1), */
225 0x81, 0x02, /* Input (Variable), */
226 0x05, 0x01, /* Usage Page (Desktop), */
227 0x95, 0x01, /* Report Count (1), */
228 0x09, 0x01, /* Usage (Pointer), */
229 0x81, 0x02, /* Input (Variable), */
230 0x06, 0x00, 0xFF, /* Usage Page (FF00h), */
231 0x95, 0x1E, /* Report Count (24), */
Simon Woodb2723eb2015-06-17 00:08:53 -0600232 0x81, 0x02, /* Input (Variable), */
233 0x75, 0x08, /* Report Size (8), */
234 0x95, 0x30, /* Report Count (48), */
235 0x09, 0x01, /* Usage (Pointer), */
236 0x91, 0x02, /* Output (Variable), */
237 0x75, 0x08, /* Report Size (8), */
238 0x95, 0x30, /* Report Count (48), */
239 0x09, 0x01, /* Usage (Pointer), */
240 0xB1, 0x02, /* Feature (Variable), */
241 0xC0, /* End Collection, */
242 0xA1, 0x02, /* Collection (Logical), */
243 0x85, 0x02, /* Report ID (2), */
244 0x75, 0x08, /* Report Size (8), */
245 0x95, 0x30, /* Report Count (48), */
246 0x09, 0x01, /* Usage (Pointer), */
247 0xB1, 0x02, /* Feature (Variable), */
248 0xC0, /* End Collection, */
249 0xA1, 0x02, /* Collection (Logical), */
250 0x85, 0xEE, /* Report ID (238), */
251 0x75, 0x08, /* Report Size (8), */
252 0x95, 0x30, /* Report Count (48), */
253 0x09, 0x01, /* Usage (Pointer), */
254 0xB1, 0x02, /* Feature (Variable), */
255 0xC0, /* End Collection, */
256 0xA1, 0x02, /* Collection (Logical), */
257 0x85, 0xEF, /* Report ID (239), */
258 0x75, 0x08, /* Report Size (8), */
259 0x95, 0x30, /* Report Count (48), */
260 0x09, 0x01, /* Usage (Pointer), */
261 0xB1, 0x02, /* Feature (Variable), */
262 0xC0, /* End Collection, */
263 0xC0 /* End Collection */
264};
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400265
Pavel Machek1adf9042016-02-09 13:55:08 +0100266static u8 ps3remote_rdesc[] = {
Jiri Kosina078328d2013-06-13 12:03:49 +0200267 0x05, 0x01, /* GUsagePage Generic Desktop */
268 0x09, 0x05, /* LUsage 0x05 [Game Pad] */
269 0xA1, 0x01, /* MCollection Application (mouse, keyboard) */
270
271 /* Use collection 1 for joypad buttons */
272 0xA1, 0x02, /* MCollection Logical (interrelated data) */
273
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100274 /*
275 * Ignore the 1st byte, maybe it is used for a controller
276 * number but it's not needed for correct operation
277 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200278 0x75, 0x08, /* GReportSize 0x08 [8] */
279 0x95, 0x01, /* GReportCount 0x01 [1] */
280 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
281
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100282 /*
283 * Bytes from 2nd to 4th are a bitmap for joypad buttons, for these
284 * buttons multiple keypresses are allowed
285 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200286 0x05, 0x09, /* GUsagePage Button */
287 0x19, 0x01, /* LUsageMinimum 0x01 [Button 1 (primary/trigger)] */
288 0x29, 0x18, /* LUsageMaximum 0x18 [Button 24] */
289 0x14, /* GLogicalMinimum [0] */
290 0x25, 0x01, /* GLogicalMaximum 0x01 [1] */
291 0x75, 0x01, /* GReportSize 0x01 [1] */
292 0x95, 0x18, /* GReportCount 0x18 [24] */
293 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
294
295 0xC0, /* MEndCollection */
296
297 /* Use collection 2 for remote control buttons */
298 0xA1, 0x02, /* MCollection Logical (interrelated data) */
299
300 /* 5th byte is used for remote control buttons */
301 0x05, 0x09, /* GUsagePage Button */
302 0x18, /* LUsageMinimum [No button pressed] */
303 0x29, 0xFE, /* LUsageMaximum 0xFE [Button 254] */
304 0x14, /* GLogicalMinimum [0] */
305 0x26, 0xFE, 0x00, /* GLogicalMaximum 0x00FE [254] */
306 0x75, 0x08, /* GReportSize 0x08 [8] */
307 0x95, 0x01, /* GReportCount 0x01 [1] */
308 0x80, /* MInput */
309
Antonio Ospiteef916ef2016-02-09 13:55:07 +0100310 /*
311 * Ignore bytes from 6th to 11th, 6th to 10th are always constant at
312 * 0xff and 11th is for press indication
313 */
Jiri Kosina078328d2013-06-13 12:03:49 +0200314 0x75, 0x08, /* GReportSize 0x08 [8] */
315 0x95, 0x06, /* GReportCount 0x06 [6] */
316 0x81, 0x01, /* MInput 0x01 (Const[0] Arr[1] Abs[2]) */
317
318 /* 12th byte is for battery strength */
319 0x05, 0x06, /* GUsagePage Generic Device Controls */
320 0x09, 0x20, /* LUsage 0x20 [Battery Strength] */
321 0x14, /* GLogicalMinimum [0] */
322 0x25, 0x05, /* GLogicalMaximum 0x05 [5] */
323 0x75, 0x08, /* GReportSize 0x08 [8] */
324 0x95, 0x01, /* GReportCount 0x01 [1] */
325 0x81, 0x02, /* MInput 0x02 (Data[0] Var[1] Abs[2]) */
326
327 0xC0, /* MEndCollection */
328
329 0xC0 /* MEndCollection [Game Pad] */
330};
331
332static const unsigned int ps3remote_keymap_joypad_buttons[] = {
333 [0x01] = KEY_SELECT,
334 [0x02] = BTN_THUMBL, /* L3 */
335 [0x03] = BTN_THUMBR, /* R3 */
336 [0x04] = BTN_START,
337 [0x05] = KEY_UP,
338 [0x06] = KEY_RIGHT,
339 [0x07] = KEY_DOWN,
340 [0x08] = KEY_LEFT,
341 [0x09] = BTN_TL2, /* L2 */
342 [0x0a] = BTN_TR2, /* R2 */
343 [0x0b] = BTN_TL, /* L1 */
344 [0x0c] = BTN_TR, /* R1 */
345 [0x0d] = KEY_OPTION, /* options/triangle */
346 [0x0e] = KEY_BACK, /* back/circle */
347 [0x0f] = BTN_0, /* cross */
348 [0x10] = KEY_SCREEN, /* view/square */
349 [0x11] = KEY_HOMEPAGE, /* PS button */
350 [0x14] = KEY_ENTER,
351};
352static const unsigned int ps3remote_keymap_remote_buttons[] = {
353 [0x00] = KEY_1,
354 [0x01] = KEY_2,
355 [0x02] = KEY_3,
356 [0x03] = KEY_4,
357 [0x04] = KEY_5,
358 [0x05] = KEY_6,
359 [0x06] = KEY_7,
360 [0x07] = KEY_8,
361 [0x08] = KEY_9,
362 [0x09] = KEY_0,
363 [0x0e] = KEY_ESC, /* return */
364 [0x0f] = KEY_CLEAR,
365 [0x16] = KEY_EJECTCD,
366 [0x1a] = KEY_MENU, /* top menu */
367 [0x28] = KEY_TIME,
368 [0x30] = KEY_PREVIOUS,
369 [0x31] = KEY_NEXT,
370 [0x32] = KEY_PLAY,
371 [0x33] = KEY_REWIND, /* scan back */
372 [0x34] = KEY_FORWARD, /* scan forward */
373 [0x38] = KEY_STOP,
374 [0x39] = KEY_PAUSE,
375 [0x40] = KEY_CONTEXT_MENU, /* pop up/menu */
376 [0x60] = KEY_FRAMEBACK, /* slow/step back */
377 [0x61] = KEY_FRAMEFORWARD, /* slow/step forward */
378 [0x63] = KEY_SUBTITLE,
379 [0x64] = KEY_AUDIO,
380 [0x65] = KEY_ANGLE,
381 [0x70] = KEY_INFO, /* display */
382 [0x80] = KEY_BLUE,
383 [0x81] = KEY_RED,
384 [0x82] = KEY_GREEN,
385 [0x83] = KEY_YELLOW,
386};
387
Colin Leitnerf04d5142013-05-27 23:41:05 +0200388static const unsigned int buzz_keymap[] = {
Frank Praznikad142b92014-02-20 11:36:00 -0500389 /*
390 * The controller has 4 remote buzzers, each with one LED and 5
Colin Leitnerf04d5142013-05-27 23:41:05 +0200391 * buttons.
Antonio Ospite09593e32016-02-09 13:55:06 +0100392 *
Colin Leitnerf04d5142013-05-27 23:41:05 +0200393 * We use the mapping chosen by the controller, which is:
394 *
395 * Key Offset
396 * -------------------
397 * Buzz 1
398 * Blue 5
399 * Orange 4
400 * Green 3
401 * Yellow 2
402 *
403 * So, for example, the orange button on the third buzzer is mapped to
404 * BTN_TRIGGER_HAPPY14
405 */
Antonio Ospite09593e32016-02-09 13:55:06 +0100406 [1] = BTN_TRIGGER_HAPPY1,
407 [2] = BTN_TRIGGER_HAPPY2,
408 [3] = BTN_TRIGGER_HAPPY3,
409 [4] = BTN_TRIGGER_HAPPY4,
410 [5] = BTN_TRIGGER_HAPPY5,
411 [6] = BTN_TRIGGER_HAPPY6,
412 [7] = BTN_TRIGGER_HAPPY7,
413 [8] = BTN_TRIGGER_HAPPY8,
414 [9] = BTN_TRIGGER_HAPPY9,
Colin Leitnerf04d5142013-05-27 23:41:05 +0200415 [10] = BTN_TRIGGER_HAPPY10,
416 [11] = BTN_TRIGGER_HAPPY11,
417 [12] = BTN_TRIGGER_HAPPY12,
418 [13] = BTN_TRIGGER_HAPPY13,
419 [14] = BTN_TRIGGER_HAPPY14,
420 [15] = BTN_TRIGGER_HAPPY15,
421 [16] = BTN_TRIGGER_HAPPY16,
422 [17] = BTN_TRIGGER_HAPPY17,
423 [18] = BTN_TRIGGER_HAPPY18,
424 [19] = BTN_TRIGGER_HAPPY19,
425 [20] = BTN_TRIGGER_HAPPY20,
426};
427
Roderick Colenbrandere19a2672017-03-07 15:45:08 -0800428static const unsigned int sixaxis_absmap[] = {
429 [0x30] = ABS_X,
430 [0x31] = ABS_Y,
431 [0x32] = ABS_RX, /* right stick X */
432 [0x35] = ABS_RY, /* right stick Y */
433};
434
435static const unsigned int sixaxis_keymap[] = {
436 [0x01] = BTN_SELECT, /* Select */
437 [0x02] = BTN_THUMBL, /* L3 */
438 [0x03] = BTN_THUMBR, /* R3 */
439 [0x04] = BTN_START, /* Start */
440 [0x05] = BTN_DPAD_UP, /* Up */
441 [0x06] = BTN_DPAD_RIGHT, /* Right */
442 [0x07] = BTN_DPAD_DOWN, /* Down */
443 [0x08] = BTN_DPAD_LEFT, /* Left */
444 [0x09] = BTN_TL2, /* L2 */
445 [0x0a] = BTN_TR2, /* R2 */
446 [0x0b] = BTN_TL, /* L1 */
447 [0x0c] = BTN_TR, /* R1 */
448 [0x0d] = BTN_NORTH, /* Triangle */
449 [0x0e] = BTN_EAST, /* Circle */
450 [0x0f] = BTN_SOUTH, /* Cross */
451 [0x10] = BTN_WEST, /* Square */
452 [0x11] = BTN_MODE, /* PS */
453};
454
455
Roderick Colenbrander9131f8c2016-11-23 14:07:08 -0800456static const unsigned int ds4_absmap[] = {
457 [0x30] = ABS_X,
458 [0x31] = ABS_Y,
459 [0x32] = ABS_RX, /* right stick X */
460 [0x33] = ABS_Z, /* L2 */
461 [0x34] = ABS_RZ, /* R2 */
462 [0x35] = ABS_RY, /* right stick Y */
463};
464
465static const unsigned int ds4_keymap[] = {
466 [0x1] = BTN_WEST, /* Square */
467 [0x2] = BTN_SOUTH, /* Cross */
468 [0x3] = BTN_EAST, /* Circle */
469 [0x4] = BTN_NORTH, /* Triangle */
470 [0x5] = BTN_TL, /* L1 */
471 [0x6] = BTN_TR, /* R1 */
472 [0x7] = BTN_TL2, /* L2 */
473 [0x8] = BTN_TR2, /* R2 */
474 [0x9] = BTN_SELECT, /* Share */
475 [0xa] = BTN_START, /* Options */
476 [0xb] = BTN_THUMBL, /* L3 */
477 [0xc] = BTN_THUMBR, /* R3 */
478 [0xd] = BTN_MODE, /* PS */
479};
480
Roderick Colenbranderd03ae2e2017-03-07 15:45:03 -0800481static const struct {int x; int y; } ds4_hat_mapping[] = {
482 {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1},
483 {0, 0}
484};
Roderick Colenbrander9131f8c2016-11-23 14:07:08 -0800485
Frank Praznikd902f472014-01-27 10:17:36 -0500486static enum power_supply_property sony_battery_props[] = {
487 POWER_SUPPLY_PROP_PRESENT,
488 POWER_SUPPLY_PROP_CAPACITY,
489 POWER_SUPPLY_PROP_SCOPE,
490 POWER_SUPPLY_PROP_STATUS,
491};
492
Frank Praznik55d3b662014-04-14 10:11:32 -0400493struct sixaxis_led {
Pavel Machek1adf9042016-02-09 13:55:08 +0100494 u8 time_enabled; /* the total time the led is active (0xff means forever) */
495 u8 duty_length; /* how long a cycle is in deciseconds (0 means "really fast") */
496 u8 enabled;
497 u8 duty_off; /* % of duty_length the led is off (0xff means 100%) */
498 u8 duty_on; /* % of duty_length the led is on (0xff mean 100%) */
Frank Praznik55d3b662014-04-14 10:11:32 -0400499} __packed;
500
501struct sixaxis_rumble {
Pavel Machek1adf9042016-02-09 13:55:08 +0100502 u8 padding;
503 u8 right_duration; /* Right motor duration (0xff means forever) */
504 u8 right_motor_on; /* Right (small) motor on/off, only supports values of 0 or 1 (off/on) */
505 u8 left_duration; /* Left motor duration (0xff means forever) */
506 u8 left_motor_force; /* left (large) motor, supports force values from 0 to 255 */
Frank Praznik55d3b662014-04-14 10:11:32 -0400507} __packed;
508
509struct sixaxis_output_report {
Pavel Machek1adf9042016-02-09 13:55:08 +0100510 u8 report_id;
Frank Praznik55d3b662014-04-14 10:11:32 -0400511 struct sixaxis_rumble rumble;
Pavel Machek1adf9042016-02-09 13:55:08 +0100512 u8 padding[4];
513 u8 leds_bitmap; /* bitmap of enabled LEDs: LED_1 = 0x02, LED_2 = 0x04, ... */
Frank Praznik55d3b662014-04-14 10:11:32 -0400514 struct sixaxis_led led[4]; /* LEDx at (4 - x) */
515 struct sixaxis_led _reserved; /* LED5, not actually soldered */
516} __packed;
517
518union sixaxis_output_report_01 {
519 struct sixaxis_output_report data;
Pavel Machek1adf9042016-02-09 13:55:08 +0100520 u8 buf[36];
Frank Praznik55d3b662014-04-14 10:11:32 -0400521};
522
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400523struct motion_output_report_02 {
524 u8 type, zero;
525 u8 r, g, b;
526 u8 zero2;
527 u8 rumble;
528};
529
Roderick Colenbrander2c159de2016-10-07 12:39:35 -0700530#define DS4_FEATURE_REPORT_0x02_SIZE 37
Roderick Colenbrander55a07d62017-03-07 15:45:01 -0800531#define DS4_FEATURE_REPORT_0x05_SIZE 41
Roderick Colenbrander2c159de2016-10-07 12:39:35 -0700532#define DS4_FEATURE_REPORT_0x81_SIZE 7
Roderick Colenbrander49b9ca62016-10-07 12:39:36 -0700533#define DS4_INPUT_REPORT_0x11_SIZE 78
Roderick Colenbrander2c159de2016-10-07 12:39:35 -0700534#define DS4_OUTPUT_REPORT_0x05_SIZE 32
535#define DS4_OUTPUT_REPORT_0x11_SIZE 78
Antonio Ospite29b691a2015-02-16 18:12:21 +0100536#define SIXAXIS_REPORT_0xF2_SIZE 17
Antonio Ospitea85d67b2015-02-16 18:12:22 +0100537#define SIXAXIS_REPORT_0xF5_SIZE 8
Simon Wood41d2d422015-06-09 21:27:06 -0600538#define MOTION_REPORT_0x02_SIZE 49
Frank Praznik9b2b5c92014-11-12 14:10:09 -0500539
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700540/* Offsets relative to USB input report (0x1). Bluetooth (0x11) requires an
541 * additional +2.
542 */
Roderick Colenbranderd03ae2e2017-03-07 15:45:03 -0800543#define DS4_INPUT_REPORT_AXIS_OFFSET 1
Roderick Colenbranderac797b92016-11-23 14:07:07 -0800544#define DS4_INPUT_REPORT_BUTTON_OFFSET 5
Roderick Colenbrander80786eb2017-03-07 15:45:02 -0800545#define DS4_INPUT_REPORT_TIMESTAMP_OFFSET 10
Roderick Colenbrander227c0112017-03-07 15:45:00 -0800546#define DS4_INPUT_REPORT_GYRO_X_OFFSET 13
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700547#define DS4_INPUT_REPORT_BATTERY_OFFSET 30
548#define DS4_INPUT_REPORT_TOUCHPAD_OFFSET 33
549
Roderick Colenbrander510c8b72017-03-07 15:45:10 -0800550#define SENSOR_SUFFIX " Motion Sensors"
Roderick Colenbranderac797b92016-11-23 14:07:07 -0800551#define DS4_TOUCHPAD_SUFFIX " Touchpad"
552
Roderick Colenbrander55a07d62017-03-07 15:45:01 -0800553#define DS4_GYRO_RES_PER_DEG_S 1024
554#define DS4_ACC_RES_PER_G 8192
555
Roderick Colenbrander510c8b72017-03-07 15:45:10 -0800556#define SIXAXIS_INPUT_REPORT_ACC_X_OFFSET 41
557#define SIXAXIS_ACC_RES_PER_G 113
558
Jiri Kosina8b402c92015-02-23 11:15:44 +0100559static DEFINE_SPINLOCK(sony_dev_list_lock);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500560static LIST_HEAD(sony_device_list);
Frank Praznik80250872014-04-14 10:11:35 -0400561static DEFINE_IDA(sony_device_id_allocator);
Frank Praznikd2d782f2014-02-20 11:36:03 -0500562
Roderick Colenbrander55a07d62017-03-07 15:45:01 -0800563/* Used for calibration of DS4 accelerometer and gyro. */
564struct ds4_calibration_data {
565 int abs_code;
566 short bias;
567 /* Calibration requires scaling against a sensitivity value, which is a
568 * float. Store sensitivity as a fraction to limit floating point
569 * calculations until final calibration.
570 */
571 int sens_numer;
572 int sens_denom;
573};
574
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -0800575enum ds4_dongle_state {
576 DONGLE_DISCONNECTED,
577 DONGLE_CALIBRATING,
578 DONGLE_CONNECTED,
579 DONGLE_DISABLED
580};
581
Roderick Colenbranderb5322732017-03-07 15:45:05 -0800582enum sony_worker {
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -0800583 SONY_WORKER_STATE,
584 SONY_WORKER_HOTPLUG
Roderick Colenbranderb5322732017-03-07 15:45:05 -0800585};
586
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200587struct sony_sc {
Frank Praznikd902f472014-01-27 10:17:36 -0500588 spinlock_t lock;
Frank Praznikd2d782f2014-02-20 11:36:03 -0500589 struct list_head list_node;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100590 struct hid_device *hdev;
Roderick Colenbranderac797b92016-11-23 14:07:07 -0800591 struct input_dev *touchpad;
Roderick Colenbrander227c0112017-03-07 15:45:00 -0800592 struct input_dev *sensor_dev;
Frank Praznik60781cf2014-01-11 15:13:15 -0500593 struct led_classdev *leds[MAX_LEDS];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200594 unsigned long quirks;
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -0800595 struct work_struct hotplug_worker;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +0100596 struct work_struct state_worker;
Antonio Ospite09593e32016-02-09 13:55:06 +0100597 void (*send_output_report)(struct sony_sc *);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100598 struct power_supply *battery;
599 struct power_supply_desc battery_desc;
Frank Praznik80250872014-04-14 10:11:35 -0400600 int device_id;
Pavel Machek1adf9042016-02-09 13:55:08 +0100601 u8 *output_report_dmabuf;
Colin Leitnerf04d5142013-05-27 23:41:05 +0200602
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100603#ifdef CONFIG_SONY_FF
Pavel Machek1adf9042016-02-09 13:55:08 +0100604 u8 left;
605 u8 right;
Sven Eckelmann9f323b62013-11-17 20:38:21 +0100606#endif
607
Pavel Machek1adf9042016-02-09 13:55:08 +0100608 u8 mac_address[6];
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -0800609 u8 hotplug_worker_initialized;
Roderick Colenbranderb5322732017-03-07 15:45:05 -0800610 u8 state_worker_initialized;
Frank Praznik2a242932016-09-22 20:18:09 -0400611 u8 defer_initialization;
Pavel Machek1adf9042016-02-09 13:55:08 +0100612 u8 cable_state;
613 u8 battery_charging;
614 u8 battery_capacity;
615 u8 led_state[MAX_LEDS];
Pavel Machek1adf9042016-02-09 13:55:08 +0100616 u8 led_delay_on[MAX_LEDS];
617 u8 led_delay_off[MAX_LEDS];
618 u8 led_count;
Roderick Colenbrander80786eb2017-03-07 15:45:02 -0800619
620 bool timestamp_initialized;
621 u16 prev_timestamp;
622 unsigned int timestamp_us;
623
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -0800624 enum ds4_dongle_state ds4_dongle_state;
Roderick Colenbrander55a07d62017-03-07 15:45:01 -0800625 /* DS4 calibration data */
626 struct ds4_calibration_data ds4_calib_data[6];
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200627};
628
Roderick Colenbrander405182c2016-12-08 19:09:52 -0800629static void sony_set_leds(struct sony_sc *sc);
630
Roderick Colenbranderb5322732017-03-07 15:45:05 -0800631static inline void sony_schedule_work(struct sony_sc *sc,
632 enum sony_worker which)
Frank Praznik2a242932016-09-22 20:18:09 -0400633{
Roderick Colenbranderb5322732017-03-07 15:45:05 -0800634 switch (which) {
635 case SONY_WORKER_STATE:
636 if (!sc->defer_initialization)
637 schedule_work(&sc->state_worker);
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -0800638 break;
639 case SONY_WORKER_HOTPLUG:
640 if (sc->hotplug_worker_initialized)
641 schedule_work(&sc->hotplug_worker);
642 break;
Roderick Colenbranderb5322732017-03-07 15:45:05 -0800643 }
Frank Praznik2a242932016-09-22 20:18:09 -0400644}
645
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400646static u8 *motion_fixup(struct hid_device *hdev, u8 *rdesc,
647 unsigned int *rsize)
648{
649 *rsize = sizeof(motion_rdesc);
650 return motion_rdesc;
651}
652
Simon Woodb2723eb2015-06-17 00:08:53 -0600653static u8 *navigation_fixup(struct hid_device *hdev, u8 *rdesc,
654 unsigned int *rsize)
655{
656 *rsize = sizeof(navigation_rdesc);
657 return navigation_rdesc;
658}
659
Pavel Machek1adf9042016-02-09 13:55:08 +0100660static u8 *ps3remote_fixup(struct hid_device *hdev, u8 *rdesc,
Jiri Kosina078328d2013-06-13 12:03:49 +0200661 unsigned int *rsize)
662{
663 *rsize = sizeof(ps3remote_rdesc);
664 return ps3remote_rdesc;
665}
666
667static int ps3remote_mapping(struct hid_device *hdev, struct hid_input *hi,
668 struct hid_field *field, struct hid_usage *usage,
669 unsigned long **bit, int *max)
670{
671 unsigned int key = usage->hid & HID_USAGE;
672
673 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
674 return -1;
675
676 switch (usage->collection_index) {
677 case 1:
678 if (key >= ARRAY_SIZE(ps3remote_keymap_joypad_buttons))
679 return -1;
680
681 key = ps3remote_keymap_joypad_buttons[key];
682 if (!key)
683 return -1;
684 break;
685 case 2:
686 if (key >= ARRAY_SIZE(ps3remote_keymap_remote_buttons))
687 return -1;
688
689 key = ps3remote_keymap_remote_buttons[key];
690 if (!key)
691 return -1;
692 break;
693 default:
694 return -1;
695 }
696
697 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
698 return 1;
699}
700
Roderick Colenbrandere19a2672017-03-07 15:45:08 -0800701static int sixaxis_mapping(struct hid_device *hdev, struct hid_input *hi,
702 struct hid_field *field, struct hid_usage *usage,
703 unsigned long **bit, int *max)
704{
705 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
706 unsigned int key = usage->hid & HID_USAGE;
707
708 if (key >= ARRAY_SIZE(sixaxis_keymap))
709 return -1;
710
711 key = sixaxis_keymap[key];
712 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
713 return 1;
714 } else if (usage->hid == HID_GD_POINTER) {
715 /* The DS3 provides analog values for most buttons and even
716 * for HAT axes through GD Pointer. L2 and R2 are reported
717 * among these as well instead of as GD Z / RZ. Remap L2
718 * and R2 and ignore other analog 'button axes' as there is
719 * no good way for reporting them.
720 */
721 switch (usage->usage_index) {
722 case 8: /* L2 */
723 usage->hid = HID_GD_Z;
724 break;
725 case 9: /* R2 */
726 usage->hid = HID_GD_RZ;
727 break;
728 default:
729 return -1;
730 }
731
732 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, usage->hid & 0xf);
733 return 1;
734 } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
735 unsigned int abs = usage->hid & HID_USAGE;
736
737 if (abs >= ARRAY_SIZE(sixaxis_absmap))
738 return -1;
739
740 abs = sixaxis_absmap[abs];
741
742 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
743 return 1;
744 }
745
746 return -1;
747}
748
Roderick Colenbrander9131f8c2016-11-23 14:07:08 -0800749static int ds4_mapping(struct hid_device *hdev, struct hid_input *hi,
750 struct hid_field *field, struct hid_usage *usage,
751 unsigned long **bit, int *max)
752{
753 if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
754 unsigned int key = usage->hid & HID_USAGE;
755
756 if (key >= ARRAY_SIZE(ds4_keymap))
757 return -1;
758
759 key = ds4_keymap[key];
760 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
761 return 1;
762 } else if ((usage->hid & HID_USAGE_PAGE) == HID_UP_GENDESK) {
763 unsigned int abs = usage->hid & HID_USAGE;
764
765 /* Let the HID parser deal with the HAT. */
766 if (usage->hid == HID_GD_HATSWITCH)
767 return 0;
768
769 if (abs >= ARRAY_SIZE(ds4_absmap))
770 return -1;
771
772 abs = ds4_absmap[abs];
773 hid_map_usage_clear(hi, usage, bit, max, EV_ABS, abs);
774 return 1;
775 }
776
777 return 0;
778}
779
Pavel Machek1adf9042016-02-09 13:55:08 +0100780static u8 *sony_report_fixup(struct hid_device *hdev, u8 *rdesc,
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400781 unsigned int *rsize)
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200782{
783 struct sony_sc *sc = hid_get_drvdata(hdev);
784
Mikko Perttunen4ba1eee2016-07-21 19:54:48 +0300785 if (sc->quirks & (SINO_LITE_CONTROLLER | FUTUREMAX_DANCE_MAT))
Scott Moreau74500cc2016-01-13 07:40:42 -0700786 return rdesc;
787
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900788 /*
789 * Some Sony RF receivers wrongly declare the mouse pointer as a
790 * a constant non-data variable.
791 */
792 if ((sc->quirks & VAIO_RDESC_CONSTANT) && *rsize >= 56 &&
793 /* usage page: generic desktop controls */
794 /* rdesc[0] == 0x05 && rdesc[1] == 0x01 && */
795 /* usage: mouse */
796 rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
797 /* input (usage page for x,y axes): constant, variable, relative */
798 rdesc[54] == 0x81 && rdesc[55] == 0x07) {
Fernando Luis Vázquez Caoa4649182013-01-15 19:40:48 +0900799 hid_info(hdev, "Fixing up Sony RF Receiver report descriptor\n");
Fernando Luis Vázquez Cao99d24902013-01-22 15:20:38 +0900800 /* input: data, variable, relative */
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200801 rdesc[55] = 0x06;
802 }
Simon Wood61ab44b2011-06-10 12:00:26 +0200803
Frank Praznikc5e0c1c2015-05-05 20:47:30 -0400804 if (sc->quirks & MOTION_CONTROLLER)
805 return motion_fixup(hdev, rdesc, rsize);
806
Simon Wood4545ee02015-06-17 00:08:52 -0600807 if (sc->quirks & NAVIGATION_CONTROLLER)
Simon Woodb2723eb2015-06-17 00:08:53 -0600808 return navigation_fixup(hdev, rdesc, rsize);
Simon Wood4545ee02015-06-17 00:08:52 -0600809
Jiri Kosina078328d2013-06-13 12:03:49 +0200810 if (sc->quirks & PS3REMOTE)
811 return ps3remote_fixup(hdev, rdesc, rsize);
812
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400813 return rdesc;
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +0200814}
815
Pavel Machek1adf9042016-02-09 13:55:08 +0100816static void sixaxis_parse_report(struct sony_sc *sc, u8 *rd, int size)
Frank Praznikd902f472014-01-27 10:17:36 -0500817{
Pavel Machek1adf9042016-02-09 13:55:08 +0100818 static const u8 sixaxis_battery_capacity[] = { 0, 1, 25, 50, 75, 100 };
Frank Praznikd902f472014-01-27 10:17:36 -0500819 unsigned long flags;
Simon Wood12e9a6d72015-06-09 21:27:05 -0600820 int offset;
Pavel Machek1adf9042016-02-09 13:55:08 +0100821 u8 cable_state, battery_capacity, battery_charging;
Frank Praznikd902f472014-01-27 10:17:36 -0500822
Frank Praznikad142b92014-02-20 11:36:00 -0500823 /*
824 * The sixaxis is charging if the battery value is 0xee
Frank Praznikd902f472014-01-27 10:17:36 -0500825 * and it is fully charged if the value is 0xef.
826 * It does not report the actual level while charging so it
827 * is set to 100% while charging is in progress.
828 */
Simon Wood12e9a6d72015-06-09 21:27:05 -0600829 offset = (sc->quirks & MOTION_CONTROLLER) ? 12 : 30;
830
831 if (rd[offset] >= 0xee) {
Frank Praznikd902f472014-01-27 10:17:36 -0500832 battery_capacity = 100;
Simon Wood12e9a6d72015-06-09 21:27:05 -0600833 battery_charging = !(rd[offset] & 0x01);
Frank Praznik9fddd742014-08-29 13:11:52 -0400834 cable_state = 1;
Frank Praznikd902f472014-01-27 10:17:36 -0500835 } else {
Pavel Machek1adf9042016-02-09 13:55:08 +0100836 u8 index = rd[offset] <= 5 ? rd[offset] : 5;
Frank Praznikac3c9a92014-02-20 11:36:02 -0500837 battery_capacity = sixaxis_battery_capacity[index];
Frank Praznikd902f472014-01-27 10:17:36 -0500838 battery_charging = 0;
Frank Praznik9fddd742014-08-29 13:11:52 -0400839 cable_state = 0;
Frank Praznikd902f472014-01-27 10:17:36 -0500840 }
Frank Praznikd902f472014-01-27 10:17:36 -0500841
842 spin_lock_irqsave(&sc->lock, flags);
843 sc->cable_state = cable_state;
844 sc->battery_capacity = battery_capacity;
845 sc->battery_charging = battery_charging;
846 spin_unlock_irqrestore(&sc->lock, flags);
Roderick Colenbrander510c8b72017-03-07 15:45:10 -0800847
848 if (sc->quirks & SIXAXIS_CONTROLLER) {
849 int val;
850
851 offset = SIXAXIS_INPUT_REPORT_ACC_X_OFFSET;
852 val = ((rd[offset+1] << 8) | rd[offset]) - 511;
853 input_report_abs(sc->sensor_dev, ABS_X, val);
854
855 /* Y and Z are swapped and inversed */
856 val = 511 - ((rd[offset+5] << 8) | rd[offset+4]);
857 input_report_abs(sc->sensor_dev, ABS_Y, val);
858
859 val = 511 - ((rd[offset+3] << 8) | rd[offset+2]);
860 input_report_abs(sc->sensor_dev, ABS_Z, val);
861
862 input_sync(sc->sensor_dev);
863 }
Frank Praznikd902f472014-01-27 10:17:36 -0500864}
865
Pavel Machek1adf9042016-02-09 13:55:08 +0100866static void dualshock4_parse_report(struct sony_sc *sc, u8 *rd, int size)
Frank Praznikd902f472014-01-27 10:17:36 -0500867{
Roderick Colenbranderd03ae2e2017-03-07 15:45:03 -0800868 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
869 struct hid_input, list);
870 struct input_dev *input_dev = hidinput->input;
Frank Praznikd902f472014-01-27 10:17:36 -0500871 unsigned long flags;
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700872 int n, m, offset, num_touch_data, max_touch_data;
Pavel Machek1adf9042016-02-09 13:55:08 +0100873 u8 cable_state, battery_capacity, battery_charging;
Roderick Colenbrander80786eb2017-03-07 15:45:02 -0800874 u16 timestamp;
Frank Praznikd902f472014-01-27 10:17:36 -0500875
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700876 /* When using Bluetooth the header is 2 bytes longer, so skip these. */
Roderick Colenbrander35f436c2017-03-07 15:45:04 -0800877 int data_offset = (sc->quirks & DUALSHOCK4_CONTROLLER_BT) ? 2 : 0;
Frank Praznik6c5f8602014-02-05 20:03:47 -0500878
Roderick Colenbranderac797b92016-11-23 14:07:07 -0800879 /* Second bit of third button byte is for the touchpad button. */
880 offset = data_offset + DS4_INPUT_REPORT_BUTTON_OFFSET;
881 input_report_key(sc->touchpad, BTN_LEFT, rd[offset+2] & 0x2);
882
Roderick Colenbranderd03ae2e2017-03-07 15:45:03 -0800883 /*
884 * The default behavior of the Dualshock 4 is to send reports using
885 * report type 1 when running over Bluetooth. However, when feature
886 * report 2 is requested during the controller initialization it starts
887 * sending input reports in report 17. Since report 17 is undefined
888 * in the default HID descriptor, the HID layer won't generate events.
889 * While it is possible (and this was done before) to fixup the HID
890 * descriptor to add this mapping, it was better to do this manually.
891 * The reason is there were various pieces software both open and closed
892 * source, relying on the descriptors to be the same across various
893 * operating systems. If the descriptors wouldn't match some
894 * applications e.g. games on Wine would not be able to function due
895 * to different descriptors, which such applications are not parsing.
896 */
897 if (rd[0] == 17) {
898 int value;
899
900 offset = data_offset + DS4_INPUT_REPORT_AXIS_OFFSET;
901 input_report_abs(input_dev, ABS_X, rd[offset]);
902 input_report_abs(input_dev, ABS_Y, rd[offset+1]);
903 input_report_abs(input_dev, ABS_RX, rd[offset+2]);
904 input_report_abs(input_dev, ABS_RY, rd[offset+3]);
905
906 value = rd[offset+4] & 0xf;
907 if (value > 7)
908 value = 8; /* Center 0, 0 */
909 input_report_abs(input_dev, ABS_HAT0X, ds4_hat_mapping[value].x);
910 input_report_abs(input_dev, ABS_HAT0Y, ds4_hat_mapping[value].y);
911
912 input_report_key(input_dev, BTN_WEST, rd[offset+4] & 0x10);
913 input_report_key(input_dev, BTN_SOUTH, rd[offset+4] & 0x20);
914 input_report_key(input_dev, BTN_EAST, rd[offset+4] & 0x40);
915 input_report_key(input_dev, BTN_NORTH, rd[offset+4] & 0x80);
916
917 input_report_key(input_dev, BTN_TL, rd[offset+5] & 0x1);
918 input_report_key(input_dev, BTN_TR, rd[offset+5] & 0x2);
919 input_report_key(input_dev, BTN_TL2, rd[offset+5] & 0x4);
920 input_report_key(input_dev, BTN_TR2, rd[offset+5] & 0x8);
921 input_report_key(input_dev, BTN_SELECT, rd[offset+5] & 0x10);
922 input_report_key(input_dev, BTN_START, rd[offset+5] & 0x20);
923 input_report_key(input_dev, BTN_THUMBL, rd[offset+5] & 0x40);
924 input_report_key(input_dev, BTN_THUMBR, rd[offset+5] & 0x80);
925
926 input_report_key(input_dev, BTN_MODE, rd[offset+6] & 0x1);
927
928 input_report_abs(input_dev, ABS_Z, rd[offset+7]);
929 input_report_abs(input_dev, ABS_RZ, rd[offset+8]);
930
931 input_sync(input_dev);
932 }
933
Roderick Colenbrander80786eb2017-03-07 15:45:02 -0800934 /* Convert timestamp (in 5.33us unit) to timestamp_us */
935 offset = data_offset + DS4_INPUT_REPORT_TIMESTAMP_OFFSET;
936 timestamp = get_unaligned_le16(&rd[offset]);
937 if (!sc->timestamp_initialized) {
938 sc->timestamp_us = ((unsigned int)timestamp * 16) / 3;
939 sc->timestamp_initialized = true;
940 } else {
941 u16 delta;
942
943 if (sc->prev_timestamp > timestamp)
944 delta = (U16_MAX - sc->prev_timestamp + timestamp + 1);
945 else
946 delta = timestamp - sc->prev_timestamp;
947 sc->timestamp_us += (delta * 16) / 3;
948 }
949 sc->prev_timestamp = timestamp;
950 input_event(sc->sensor_dev, EV_MSC, MSC_TIMESTAMP, sc->timestamp_us);
951
Roderick Colenbrander227c0112017-03-07 15:45:00 -0800952 offset = data_offset + DS4_INPUT_REPORT_GYRO_X_OFFSET;
Roderick Colenbrander55a07d62017-03-07 15:45:01 -0800953 for (n = 0; n < 6; n++) {
954 /* Store data in int for more precision during mult_frac. */
955 int raw_data = (short)((rd[offset+1] << 8) | rd[offset]);
956 struct ds4_calibration_data *calib = &sc->ds4_calib_data[n];
Roderick Colenbrander227c0112017-03-07 15:45:00 -0800957
Roderick Colenbrander55a07d62017-03-07 15:45:01 -0800958 /* High precision is needed during calibration, but the
959 * calibrated values are within 32-bit.
960 * Note: we swap numerator 'x' and 'numer' in mult_frac for
961 * precision reasons so we don't need 64-bit.
962 */
963 int calib_data = mult_frac(calib->sens_numer,
964 raw_data - calib->bias,
965 calib->sens_denom);
966
967 input_report_abs(sc->sensor_dev, calib->abs_code, calib_data);
968 offset += 2;
Roderick Colenbrander227c0112017-03-07 15:45:00 -0800969 }
970 input_sync(sc->sensor_dev);
971
Frank Praznikad142b92014-02-20 11:36:00 -0500972 /*
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700973 * The lower 4 bits of byte 30 (or 32 for BT) contain the battery level
Frank Praznikd902f472014-01-27 10:17:36 -0500974 * and the 5th bit contains the USB cable state.
975 */
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -0700976 offset = data_offset + DS4_INPUT_REPORT_BATTERY_OFFSET;
Frank Praznik6c5f8602014-02-05 20:03:47 -0500977 cable_state = (rd[offset] >> 4) & 0x01;
978 battery_capacity = rd[offset] & 0x0F;
Frank Praznikd902f472014-01-27 10:17:36 -0500979
Frank Praznikad142b92014-02-20 11:36:00 -0500980 /*
981 * When a USB power source is connected the battery level ranges from
Frank Praznik6c5f8602014-02-05 20:03:47 -0500982 * 0 to 10, and when running on battery power it ranges from 0 to 9.
983 * A battery level above 10 when plugged in means charge completed.
Frank Praznikd902f472014-01-27 10:17:36 -0500984 */
Frank Praznik6c5f8602014-02-05 20:03:47 -0500985 if (!cable_state || battery_capacity > 10)
Frank Praznikd902f472014-01-27 10:17:36 -0500986 battery_charging = 0;
987 else
988 battery_charging = 1;
989
Frank Praznik6c5f8602014-02-05 20:03:47 -0500990 if (!cable_state)
991 battery_capacity++;
Frank Praznikd902f472014-01-27 10:17:36 -0500992 if (battery_capacity > 10)
Frank Praznik6c5f8602014-02-05 20:03:47 -0500993 battery_capacity = 10;
994
Frank Praznikd902f472014-01-27 10:17:36 -0500995 battery_capacity *= 10;
996
997 spin_lock_irqsave(&sc->lock, flags);
998 sc->cable_state = cable_state;
999 sc->battery_capacity = battery_capacity;
1000 sc->battery_charging = battery_charging;
1001 spin_unlock_irqrestore(&sc->lock, flags);
Frank Praznike5606232014-01-27 10:17:37 -05001002
Frank Praznikad142b92014-02-20 11:36:00 -05001003 /*
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -07001004 * The Dualshock 4 multi-touch trackpad data starts at offset 33 on USB
1005 * and 35 on Bluetooth.
1006 * The first byte indicates the number of touch data in the report.
1007 * Trackpad data starts 2 bytes later (e.g. 35 for USB).
Frank Praznike5606232014-01-27 10:17:37 -05001008 */
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -07001009 offset = data_offset + DS4_INPUT_REPORT_TOUCHPAD_OFFSET;
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001010 max_touch_data = (sc->quirks & DUALSHOCK4_CONTROLLER_BT) ? 4 : 3;
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -07001011 if (rd[offset] > 0 && rd[offset] <= max_touch_data)
1012 num_touch_data = rd[offset];
1013 else
1014 num_touch_data = 1;
1015 offset += 1;
Frank Praznike5606232014-01-27 10:17:37 -05001016
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -07001017 for (m = 0; m < num_touch_data; m++) {
1018 /* Skip past timestamp */
1019 offset += 1;
Frank Praznike5606232014-01-27 10:17:37 -05001020
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -07001021 /*
1022 * The first 7 bits of the first byte is a counter and bit 8 is
1023 * a touch indicator that is 0 when pressed and 1 when not
1024 * pressed.
1025 * The next 3 bytes are two 12 bit touch coordinates, X and Y.
1026 * The data for the second touch is in the same format and
1027 * immediately follows the data for the first.
1028 */
1029 for (n = 0; n < 2; n++) {
1030 u16 x, y;
1031 bool active;
Frank Praznike5606232014-01-27 10:17:37 -05001032
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -07001033 x = rd[offset+1] | ((rd[offset+2] & 0xF) << 8);
1034 y = ((rd[offset+2] & 0xF0) >> 4) | (rd[offset+3] << 4);
1035
1036 active = !(rd[offset] >> 7);
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001037 input_mt_slot(sc->touchpad, n);
1038 input_mt_report_slot_state(sc->touchpad, MT_TOOL_FINGER, active);
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -07001039
1040 if (active) {
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001041 input_report_abs(sc->touchpad, ABS_MT_POSITION_X, x);
1042 input_report_abs(sc->touchpad, ABS_MT_POSITION_Y, y);
Roderick Colenbrandercdc1c022016-10-07 12:39:38 -07001043 }
1044
1045 offset += 4;
1046 }
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001047 input_mt_sync_frame(sc->touchpad);
1048 input_sync(sc->touchpad);
Frank Praznike5606232014-01-27 10:17:37 -05001049 }
Frank Praznikd902f472014-01-27 10:17:36 -05001050}
1051
Simon Woodc9e4d872011-06-10 12:00:27 +02001052static int sony_raw_event(struct hid_device *hdev, struct hid_report *report,
Pavel Machek1adf9042016-02-09 13:55:08 +01001053 u8 *rd, int size)
Simon Woodc9e4d872011-06-10 12:00:27 +02001054{
1055 struct sony_sc *sc = hid_get_drvdata(hdev);
1056
Frank Praznikad142b92014-02-20 11:36:00 -05001057 /*
1058 * Sixaxis HID report has acclerometers/gyro with MSByte first, this
Simon Woodc9e4d872011-06-10 12:00:27 +02001059 * has to be BYTE_SWAPPED before passing up to joystick interface
1060 */
Frank Praznikfee4e2d2014-02-18 17:22:01 -05001061 if ((sc->quirks & SIXAXIS_CONTROLLER) && rd[0] == 0x01 && size == 49) {
Frank Praznik8f5f0bc2015-07-23 19:01:16 -04001062 /*
1063 * When connected via Bluetooth the Sixaxis occasionally sends
1064 * a report with the second byte 0xff and the rest zeroed.
1065 *
1066 * This report does not reflect the actual state of the
1067 * controller must be ignored to avoid generating false input
1068 * events.
1069 */
1070 if (rd[1] == 0xff)
1071 return -EINVAL;
1072
Simon Woodc9e4d872011-06-10 12:00:27 +02001073 swap(rd[41], rd[42]);
1074 swap(rd[43], rd[44]);
1075 swap(rd[45], rd[46]);
1076 swap(rd[47], rd[48]);
Frank Praznikd902f472014-01-27 10:17:36 -05001077
1078 sixaxis_parse_report(sc, rd, size);
Simon Wood12e9a6d72015-06-09 21:27:05 -06001079 } else if ((sc->quirks & MOTION_CONTROLLER_BT) && rd[0] == 0x01 && size == 49) {
1080 sixaxis_parse_report(sc, rd, size);
Simon Wood4545ee02015-06-17 00:08:52 -06001081 } else if ((sc->quirks & NAVIGATION_CONTROLLER) && rd[0] == 0x01 &&
1082 size == 49) {
1083 sixaxis_parse_report(sc, rd, size);
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001084 } else if ((sc->quirks & DUALSHOCK4_CONTROLLER_USB) && rd[0] == 0x01 &&
1085 size == 64) {
1086 dualshock4_parse_report(sc, rd, size);
1087 } else if (((sc->quirks & DUALSHOCK4_CONTROLLER_BT) && rd[0] == 0x11 &&
1088 size == 78)) {
1089 /* CRC check */
1090 u8 bthdr = 0xA1;
1091 u32 crc;
1092 u32 report_crc;
Roderick Colenbrander49b9ca62016-10-07 12:39:36 -07001093
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001094 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
1095 crc = ~crc32_le(crc, rd, DS4_INPUT_REPORT_0x11_SIZE-4);
1096 report_crc = get_unaligned_le32(&rd[DS4_INPUT_REPORT_0x11_SIZE-4]);
1097 if (crc != report_crc) {
1098 hid_dbg(sc->hdev, "DualShock 4 input report's CRC check failed, received crc 0x%0x != 0x%0x\n",
1099 report_crc, crc);
1100 return -EILSEQ;
Roderick Colenbrander49b9ca62016-10-07 12:39:36 -07001101 }
Roderick Colenbrander405182c2016-12-08 19:09:52 -08001102
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001103 dualshock4_parse_report(sc, rd, size);
1104 } else if ((sc->quirks & DUALSHOCK4_DONGLE) && rd[0] == 0x01 &&
1105 size == 64) {
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -08001106 unsigned long flags;
1107 enum ds4_dongle_state dongle_state;
1108
Roderick Colenbrander405182c2016-12-08 19:09:52 -08001109 /*
1110 * In the case of a DS4 USB dongle, bit[2] of byte 31 indicates
1111 * if a DS4 is actually connected (indicated by '0').
1112 * For non-dongle, this bit is always 0 (connected).
1113 */
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001114 bool connected = (rd[31] & 0x04) ? false : true;
Roderick Colenbrander405182c2016-12-08 19:09:52 -08001115
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -08001116 spin_lock_irqsave(&sc->lock, flags);
1117 dongle_state = sc->ds4_dongle_state;
1118 spin_unlock_irqrestore(&sc->lock, flags);
1119
1120 /*
1121 * The dongle always sends input reports even when no
1122 * DS4 is attached. When a DS4 is connected, we need to
1123 * obtain calibration data before we can use it.
1124 * The code below tracks dongle state and kicks of
1125 * calibration when needed and only allows us to process
1126 * input if a DS4 is actually connected.
1127 */
1128 if (dongle_state == DONGLE_DISCONNECTED && connected) {
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001129 hid_info(sc->hdev, "DualShock 4 USB dongle: controller connected\n");
1130 sony_set_leds(sc);
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -08001131
1132 spin_lock_irqsave(&sc->lock, flags);
1133 sc->ds4_dongle_state = DONGLE_CALIBRATING;
1134 spin_unlock_irqrestore(&sc->lock, flags);
1135
1136 sony_schedule_work(sc, SONY_WORKER_HOTPLUG);
1137
1138 /* Don't process the report since we don't have
1139 * calibration data, but let hidraw have it anyway.
1140 */
1141 return 0;
1142 } else if ((dongle_state == DONGLE_CONNECTED ||
1143 dongle_state == DONGLE_DISABLED) && !connected) {
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001144 hid_info(sc->hdev, "DualShock 4 USB dongle: controller disconnected\n");
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -08001145
1146 spin_lock_irqsave(&sc->lock, flags);
1147 sc->ds4_dongle_state = DONGLE_DISCONNECTED;
1148 spin_unlock_irqrestore(&sc->lock, flags);
1149
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001150 /* Return 0, so hidraw can get the report. */
1151 return 0;
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -08001152 } else if (dongle_state == DONGLE_CALIBRATING ||
1153 dongle_state == DONGLE_DISABLED ||
1154 dongle_state == DONGLE_DISCONNECTED) {
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001155 /* Return 0, so hidraw can get the report. */
1156 return 0;
Roderick Colenbrander405182c2016-12-08 19:09:52 -08001157 }
1158
Frank Praznikd902f472014-01-27 10:17:36 -05001159 dualshock4_parse_report(sc, rd, size);
Simon Woodc9e4d872011-06-10 12:00:27 +02001160 }
1161
Frank Praznik2a242932016-09-22 20:18:09 -04001162 if (sc->defer_initialization) {
1163 sc->defer_initialization = 0;
Roderick Colenbranderb5322732017-03-07 15:45:05 -08001164 sony_schedule_work(sc, SONY_WORKER_STATE);
Frank Praznik2a242932016-09-22 20:18:09 -04001165 }
1166
Simon Woodc9e4d872011-06-10 12:00:27 +02001167 return 0;
1168}
1169
Colin Leitnerf04d5142013-05-27 23:41:05 +02001170static int sony_mapping(struct hid_device *hdev, struct hid_input *hi,
1171 struct hid_field *field, struct hid_usage *usage,
1172 unsigned long **bit, int *max)
1173{
1174 struct sony_sc *sc = hid_get_drvdata(hdev);
1175
1176 if (sc->quirks & BUZZ_CONTROLLER) {
1177 unsigned int key = usage->hid & HID_USAGE;
1178
1179 if ((usage->hid & HID_USAGE_PAGE) != HID_UP_BUTTON)
1180 return -1;
1181
1182 switch (usage->collection_index) {
1183 case 1:
1184 if (key >= ARRAY_SIZE(buzz_keymap))
1185 return -1;
1186
1187 key = buzz_keymap[key];
1188 if (!key)
1189 return -1;
1190 break;
1191 default:
1192 return -1;
1193 }
1194
1195 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, key);
1196 return 1;
1197 }
1198
Jiri Kosina078328d2013-06-13 12:03:49 +02001199 if (sc->quirks & PS3REMOTE)
1200 return ps3remote_mapping(hdev, hi, field, usage, bit, max);
1201
Roderick Colenbrandere19a2672017-03-07 15:45:08 -08001202 if (sc->quirks & SIXAXIS_CONTROLLER)
1203 return sixaxis_mapping(hdev, hi, field, usage, bit, max);
Roderick Colenbrander9131f8c2016-11-23 14:07:08 -08001204
1205 if (sc->quirks & DUALSHOCK4_CONTROLLER)
1206 return ds4_mapping(hdev, hi, field, usage, bit, max);
1207
Roderick Colenbrandere19a2672017-03-07 15:45:08 -08001208
Benjamin Tissoires6f498012013-07-24 16:53:07 +02001209 /* Let hid-core decide for the others */
1210 return 0;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001211}
1212
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001213static int sony_register_touchpad(struct sony_sc *sc, int touch_count,
Frank Praznikce8efc32014-09-18 21:15:01 -04001214 int w, int h)
1215{
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001216 size_t name_sz;
1217 char *name;
Frank Praznikce8efc32014-09-18 21:15:01 -04001218 int ret;
1219
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001220 sc->touchpad = input_allocate_device();
1221 if (!sc->touchpad)
1222 return -ENOMEM;
Frank Praznikce8efc32014-09-18 21:15:01 -04001223
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001224 input_set_drvdata(sc->touchpad, sc);
1225 sc->touchpad->dev.parent = &sc->hdev->dev;
1226 sc->touchpad->phys = sc->hdev->phys;
1227 sc->touchpad->uniq = sc->hdev->uniq;
1228 sc->touchpad->id.bustype = sc->hdev->bus;
1229 sc->touchpad->id.vendor = sc->hdev->vendor;
1230 sc->touchpad->id.product = sc->hdev->product;
1231 sc->touchpad->id.version = sc->hdev->version;
1232
1233 /* Append a suffix to the controller name as there are various
1234 * DS4 compatible non-Sony devices with different names.
1235 */
1236 name_sz = strlen(sc->hdev->name) + sizeof(DS4_TOUCHPAD_SUFFIX);
1237 name = kzalloc(name_sz, GFP_KERNEL);
1238 if (!name) {
1239 ret = -ENOMEM;
1240 goto err;
1241 }
1242 snprintf(name, name_sz, "%s" DS4_TOUCHPAD_SUFFIX, sc->hdev->name);
1243 sc->touchpad->name = name;
1244
Roderick Colenbranderb9f7d242017-03-07 15:45:07 -08001245 ret = input_mt_init_slots(sc->touchpad, touch_count, INPUT_MT_POINTER);
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001246 if (ret < 0)
1247 goto err;
1248
1249 /* We map the button underneath the touchpad to BTN_LEFT. */
1250 __set_bit(EV_KEY, sc->touchpad->evbit);
1251 __set_bit(BTN_LEFT, sc->touchpad->keybit);
1252 __set_bit(INPUT_PROP_BUTTONPAD, sc->touchpad->propbit);
1253
1254 input_set_abs_params(sc->touchpad, ABS_MT_POSITION_X, 0, w, 0, 0);
1255 input_set_abs_params(sc->touchpad, ABS_MT_POSITION_Y, 0, h, 0, 0);
1256
1257 ret = input_register_device(sc->touchpad);
1258 if (ret < 0)
1259 goto err;
Frank Praznikce8efc32014-09-18 21:15:01 -04001260
1261 return 0;
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001262
1263err:
1264 kfree(sc->touchpad->name);
1265 sc->touchpad->name = NULL;
1266
1267 input_free_device(sc->touchpad);
1268 sc->touchpad = NULL;
1269
1270 return ret;
Frank Praznikce8efc32014-09-18 21:15:01 -04001271}
1272
Roderick Colenbranderac797b92016-11-23 14:07:07 -08001273static void sony_unregister_touchpad(struct sony_sc *sc)
1274{
1275 if (!sc->touchpad)
1276 return;
1277
1278 kfree(sc->touchpad->name);
1279 sc->touchpad->name = NULL;
1280
1281 input_unregister_device(sc->touchpad);
1282 sc->touchpad = NULL;
1283}
Frank Praznikce8efc32014-09-18 21:15:01 -04001284
Roderick Colenbrander227c0112017-03-07 15:45:00 -08001285static int sony_register_sensors(struct sony_sc *sc)
1286{
1287 size_t name_sz;
1288 char *name;
1289 int ret;
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001290 int range;
Roderick Colenbrander227c0112017-03-07 15:45:00 -08001291
1292 sc->sensor_dev = input_allocate_device();
1293 if (!sc->sensor_dev)
1294 return -ENOMEM;
1295
1296 input_set_drvdata(sc->sensor_dev, sc);
1297 sc->sensor_dev->dev.parent = &sc->hdev->dev;
1298 sc->sensor_dev->phys = sc->hdev->phys;
1299 sc->sensor_dev->uniq = sc->hdev->uniq;
1300 sc->sensor_dev->id.bustype = sc->hdev->bus;
1301 sc->sensor_dev->id.vendor = sc->hdev->vendor;
1302 sc->sensor_dev->id.product = sc->hdev->product;
1303 sc->sensor_dev->id.version = sc->hdev->version;
1304
1305 /* Append a suffix to the controller name as there are various
1306 * DS4 compatible non-Sony devices with different names.
1307 */
Roderick Colenbrander510c8b72017-03-07 15:45:10 -08001308 name_sz = strlen(sc->hdev->name) + sizeof(SENSOR_SUFFIX);
Roderick Colenbrander227c0112017-03-07 15:45:00 -08001309 name = kzalloc(name_sz, GFP_KERNEL);
1310 if (!name) {
1311 ret = -ENOMEM;
1312 goto err;
1313 }
Roderick Colenbrander510c8b72017-03-07 15:45:10 -08001314 snprintf(name, name_sz, "%s" SENSOR_SUFFIX, sc->hdev->name);
Roderick Colenbrander227c0112017-03-07 15:45:00 -08001315 sc->sensor_dev->name = name;
1316
Roderick Colenbrander510c8b72017-03-07 15:45:10 -08001317 if (sc->quirks & SIXAXIS_CONTROLLER) {
1318 /* For the DS3 we only support the accelerometer, which works
1319 * quite well even without calibration. The device also has
1320 * a 1-axis gyro, but it is very difficult to manage from within
1321 * the driver even to get data, the sensor is inaccurate and
1322 * the behavior is very different between hardware revisions.
1323 */
1324 input_set_abs_params(sc->sensor_dev, ABS_X, -512, 511, 4, 0);
1325 input_set_abs_params(sc->sensor_dev, ABS_Y, -512, 511, 4, 0);
1326 input_set_abs_params(sc->sensor_dev, ABS_Z, -512, 511, 4, 0);
1327 input_abs_set_res(sc->sensor_dev, ABS_X, SIXAXIS_ACC_RES_PER_G);
1328 input_abs_set_res(sc->sensor_dev, ABS_Y, SIXAXIS_ACC_RES_PER_G);
1329 input_abs_set_res(sc->sensor_dev, ABS_Z, SIXAXIS_ACC_RES_PER_G);
1330 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
1331 range = DS4_ACC_RES_PER_G*4;
1332 input_set_abs_params(sc->sensor_dev, ABS_X, -range, range, 16, 0);
1333 input_set_abs_params(sc->sensor_dev, ABS_Y, -range, range, 16, 0);
1334 input_set_abs_params(sc->sensor_dev, ABS_Z, -range, range, 16, 0);
1335 input_abs_set_res(sc->sensor_dev, ABS_X, DS4_ACC_RES_PER_G);
1336 input_abs_set_res(sc->sensor_dev, ABS_Y, DS4_ACC_RES_PER_G);
1337 input_abs_set_res(sc->sensor_dev, ABS_Z, DS4_ACC_RES_PER_G);
Roderick Colenbrander227c0112017-03-07 15:45:00 -08001338
Roderick Colenbrander510c8b72017-03-07 15:45:10 -08001339 range = DS4_GYRO_RES_PER_DEG_S*2048;
1340 input_set_abs_params(sc->sensor_dev, ABS_RX, -range, range, 16, 0);
1341 input_set_abs_params(sc->sensor_dev, ABS_RY, -range, range, 16, 0);
1342 input_set_abs_params(sc->sensor_dev, ABS_RZ, -range, range, 16, 0);
1343 input_abs_set_res(sc->sensor_dev, ABS_RX, DS4_GYRO_RES_PER_DEG_S);
1344 input_abs_set_res(sc->sensor_dev, ABS_RY, DS4_GYRO_RES_PER_DEG_S);
1345 input_abs_set_res(sc->sensor_dev, ABS_RZ, DS4_GYRO_RES_PER_DEG_S);
Roderick Colenbrander227c0112017-03-07 15:45:00 -08001346
Roderick Colenbrander510c8b72017-03-07 15:45:10 -08001347 __set_bit(EV_MSC, sc->sensor_dev->evbit);
1348 __set_bit(MSC_TIMESTAMP, sc->sensor_dev->mscbit);
1349 }
1350
Roderick Colenbrander227c0112017-03-07 15:45:00 -08001351 __set_bit(INPUT_PROP_ACCELEROMETER, sc->sensor_dev->propbit);
1352
1353 ret = input_register_device(sc->sensor_dev);
1354 if (ret < 0)
1355 goto err;
1356
1357 return 0;
1358
1359err:
1360 kfree(sc->sensor_dev->name);
1361 sc->sensor_dev->name = NULL;
1362
1363 input_free_device(sc->sensor_dev);
1364 sc->sensor_dev = NULL;
1365
1366 return ret;
1367}
1368
1369static void sony_unregister_sensors(struct sony_sc *sc)
1370{
1371 if (!sc->sensor_dev)
1372 return;
1373
1374 kfree(sc->sensor_dev->name);
1375 sc->sensor_dev->name = NULL;
1376
1377 input_unregister_device(sc->sensor_dev);
1378 sc->sensor_dev = NULL;
1379}
1380
1381
Antonio Ospite5710fab2011-02-20 18:26:45 +01001382/*
Jiri Slabybd28ce02008-06-25 23:47:04 +02001383 * Sending HID_REQ_GET_REPORT changes the operation mode of the ps3 controller
1384 * to "operational". Without this, the ps3 controller will not report any
1385 * events.
1386 */
Antonio Ospite816651a2010-05-03 22:15:55 +02001387static int sixaxis_set_operational_usb(struct hid_device *hdev)
Jiri Slabybd28ce02008-06-25 23:47:04 +02001388{
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001389 const int buf_size =
1390 max(SIXAXIS_REPORT_0xF2_SIZE, SIXAXIS_REPORT_0xF5_SIZE);
Pavel Machek1adf9042016-02-09 13:55:08 +01001391 u8 *buf;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001392 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001393
Antonio Ospite2e701a32015-02-16 18:12:24 +01001394 buf = kmalloc(buf_size, GFP_KERNEL);
Jiri Slabybd28ce02008-06-25 23:47:04 +02001395 if (!buf)
1396 return -ENOMEM;
1397
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001398 ret = hid_hw_raw_request(hdev, 0xf2, buf, SIXAXIS_REPORT_0xF2_SIZE,
1399 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001400 if (ret < 0) {
1401 hid_err(hdev, "can't set operational mode: step 1\n");
1402 goto out;
1403 }
Benjamin Tissoiresf204828a2013-09-11 22:12:25 +02001404
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001405 /*
1406 * Some compatible controllers like the Speedlink Strike FX and
1407 * Gasia need another query plus an USB interrupt to get operational.
1408 */
Antonio Ospitea85d67b2015-02-16 18:12:22 +01001409 ret = hid_hw_raw_request(hdev, 0xf5, buf, SIXAXIS_REPORT_0xF5_SIZE,
1410 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001411 if (ret < 0) {
1412 hid_err(hdev, "can't set operational mode: step 2\n");
1413 goto out;
1414 }
1415
1416 ret = hid_hw_output_report(hdev, buf, 1);
Benjamin Tissoires19f4c2b2016-01-08 17:58:49 +01001417 if (ret < 0) {
1418 hid_info(hdev, "can't set operational mode: step 3, ignoring\n");
1419 ret = 0;
1420 }
Jiri Slabybd28ce02008-06-25 23:47:04 +02001421
Lauri Kasanena7de9b82015-02-16 15:06:59 +02001422out:
Jiri Slabybd28ce02008-06-25 23:47:04 +02001423 kfree(buf);
1424
1425 return ret;
1426}
1427
Antonio Ospite816651a2010-05-03 22:15:55 +02001428static int sixaxis_set_operational_bt(struct hid_device *hdev)
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001429{
Pavel Machek1adf9042016-02-09 13:55:08 +01001430 static const u8 report[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
1431 u8 *buf;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001432 int ret;
1433
1434 buf = kmemdup(report, sizeof(report), GFP_KERNEL);
1435 if (!buf)
1436 return -ENOMEM;
1437
1438 ret = hid_hw_raw_request(hdev, buf[0], buf, sizeof(report),
Benjamin Tissoiresb0dd72a2014-02-10 12:58:54 -05001439 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001440
1441 kfree(buf);
1442
1443 return ret;
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00001444}
1445
Frank Praznikad142b92014-02-20 11:36:00 -05001446/*
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001447 * Request DS4 calibration data for the motion sensors.
1448 * For Bluetooth this also affects the operating mode (see below).
Frank Praznik68330d82014-02-05 20:03:49 -05001449 */
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001450static int dualshock4_get_calibration_data(struct sony_sc *sc)
Frank Praznik68330d82014-02-05 20:03:49 -05001451{
Pavel Machek1adf9042016-02-09 13:55:08 +01001452 u8 *buf;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001453 int ret;
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001454 short gyro_pitch_bias, gyro_pitch_plus, gyro_pitch_minus;
1455 short gyro_yaw_bias, gyro_yaw_plus, gyro_yaw_minus;
1456 short gyro_roll_bias, gyro_roll_plus, gyro_roll_minus;
1457 short gyro_speed_plus, gyro_speed_minus;
1458 short acc_x_plus, acc_x_minus;
1459 short acc_y_plus, acc_y_minus;
1460 short acc_z_plus, acc_z_minus;
1461 int speed_2x;
1462 int range_2g;
Frank Praznik68330d82014-02-05 20:03:49 -05001463
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001464 /* For Bluetooth we use a different request, which supports CRC.
1465 * Note: in Bluetooth mode feature report 0x02 also changes the state
1466 * of the controller, so that it sends input reports of type 0x11.
1467 */
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001468 if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001469 buf = kmalloc(DS4_FEATURE_REPORT_0x02_SIZE, GFP_KERNEL);
1470 if (!buf)
1471 return -ENOMEM;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001472
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001473 ret = hid_hw_raw_request(sc->hdev, 0x02, buf,
1474 DS4_FEATURE_REPORT_0x02_SIZE,
1475 HID_FEATURE_REPORT,
1476 HID_REQ_GET_REPORT);
1477 if (ret < 0)
1478 goto err_stop;
1479 } else {
1480 u8 bthdr = 0xA3;
1481 u32 crc;
1482 u32 report_crc;
1483 int retries;
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001484
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001485 buf = kmalloc(DS4_FEATURE_REPORT_0x05_SIZE, GFP_KERNEL);
1486 if (!buf)
1487 return -ENOMEM;
1488
1489 for (retries = 0; retries < 3; retries++) {
1490 ret = hid_hw_raw_request(sc->hdev, 0x05, buf,
1491 DS4_FEATURE_REPORT_0x05_SIZE,
1492 HID_FEATURE_REPORT,
1493 HID_REQ_GET_REPORT);
1494 if (ret < 0)
1495 goto err_stop;
1496
1497 /* CRC check */
1498 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
1499 crc = ~crc32_le(crc, buf, DS4_FEATURE_REPORT_0x05_SIZE-4);
1500 report_crc = get_unaligned_le32(&buf[DS4_FEATURE_REPORT_0x05_SIZE-4]);
1501 if (crc != report_crc) {
1502 hid_warn(sc->hdev, "DualShock 4 calibration report's CRC check failed, received crc 0x%0x != 0x%0x\n",
1503 report_crc, crc);
1504 if (retries < 2) {
1505 hid_warn(sc->hdev, "Retrying DualShock 4 get calibration report request\n");
1506 continue;
1507 } else {
1508 ret = -EILSEQ;
1509 goto err_stop;
1510 }
1511 } else {
1512 break;
1513 }
1514 }
1515 }
1516
1517 gyro_pitch_bias = get_unaligned_le16(&buf[1]);
1518 gyro_yaw_bias = get_unaligned_le16(&buf[3]);
1519 gyro_roll_bias = get_unaligned_le16(&buf[5]);
1520 if (sc->quirks & DUALSHOCK4_CONTROLLER_USB) {
1521 gyro_pitch_plus = get_unaligned_le16(&buf[7]);
1522 gyro_pitch_minus = get_unaligned_le16(&buf[9]);
1523 gyro_yaw_plus = get_unaligned_le16(&buf[11]);
1524 gyro_yaw_minus = get_unaligned_le16(&buf[13]);
1525 gyro_roll_plus = get_unaligned_le16(&buf[15]);
1526 gyro_roll_minus = get_unaligned_le16(&buf[17]);
1527 } else {
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08001528 /* BT + Dongle */
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08001529 gyro_pitch_plus = get_unaligned_le16(&buf[7]);
1530 gyro_yaw_plus = get_unaligned_le16(&buf[9]);
1531 gyro_roll_plus = get_unaligned_le16(&buf[11]);
1532 gyro_pitch_minus = get_unaligned_le16(&buf[13]);
1533 gyro_yaw_minus = get_unaligned_le16(&buf[15]);
1534 gyro_roll_minus = get_unaligned_le16(&buf[17]);
1535 }
1536 gyro_speed_plus = get_unaligned_le16(&buf[19]);
1537 gyro_speed_minus = get_unaligned_le16(&buf[21]);
1538 acc_x_plus = get_unaligned_le16(&buf[23]);
1539 acc_x_minus = get_unaligned_le16(&buf[25]);
1540 acc_y_plus = get_unaligned_le16(&buf[27]);
1541 acc_y_minus = get_unaligned_le16(&buf[29]);
1542 acc_z_plus = get_unaligned_le16(&buf[31]);
1543 acc_z_minus = get_unaligned_le16(&buf[33]);
1544
1545 /* Set gyroscope calibration and normalization parameters.
1546 * Data values will be normalized to 1/DS4_GYRO_RES_PER_DEG_S degree/s.
1547 */
1548 speed_2x = (gyro_speed_plus + gyro_speed_minus);
1549 sc->ds4_calib_data[0].abs_code = ABS_RX;
1550 sc->ds4_calib_data[0].bias = gyro_pitch_bias;
1551 sc->ds4_calib_data[0].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
1552 sc->ds4_calib_data[0].sens_denom = gyro_pitch_plus - gyro_pitch_minus;
1553
1554 sc->ds4_calib_data[1].abs_code = ABS_RY;
1555 sc->ds4_calib_data[1].bias = gyro_yaw_bias;
1556 sc->ds4_calib_data[1].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
1557 sc->ds4_calib_data[1].sens_denom = gyro_yaw_plus - gyro_yaw_minus;
1558
1559 sc->ds4_calib_data[2].abs_code = ABS_RZ;
1560 sc->ds4_calib_data[2].bias = gyro_roll_bias;
1561 sc->ds4_calib_data[2].sens_numer = speed_2x*DS4_GYRO_RES_PER_DEG_S;
1562 sc->ds4_calib_data[2].sens_denom = gyro_roll_plus - gyro_roll_minus;
1563
1564 /* Set accelerometer calibration and normalization parameters.
1565 * Data values will be normalized to 1/DS4_ACC_RES_PER_G G.
1566 */
1567 range_2g = acc_x_plus - acc_x_minus;
1568 sc->ds4_calib_data[3].abs_code = ABS_X;
1569 sc->ds4_calib_data[3].bias = acc_x_plus - range_2g / 2;
1570 sc->ds4_calib_data[3].sens_numer = 2*DS4_ACC_RES_PER_G;
1571 sc->ds4_calib_data[3].sens_denom = range_2g;
1572
1573 range_2g = acc_y_plus - acc_y_minus;
1574 sc->ds4_calib_data[4].abs_code = ABS_Y;
1575 sc->ds4_calib_data[4].bias = acc_y_plus - range_2g / 2;
1576 sc->ds4_calib_data[4].sens_numer = 2*DS4_ACC_RES_PER_G;
1577 sc->ds4_calib_data[4].sens_denom = range_2g;
1578
1579 range_2g = acc_z_plus - acc_z_minus;
1580 sc->ds4_calib_data[5].abs_code = ABS_Z;
1581 sc->ds4_calib_data[5].bias = acc_z_plus - range_2g / 2;
1582 sc->ds4_calib_data[5].sens_numer = 2*DS4_ACC_RES_PER_G;
1583 sc->ds4_calib_data[5].sens_denom = range_2g;
1584
1585err_stop:
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001586 kfree(buf);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001587 return ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02001588}
1589
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -08001590static void dualshock4_calibration_work(struct work_struct *work)
1591{
1592 struct sony_sc *sc = container_of(work, struct sony_sc, hotplug_worker);
1593 unsigned long flags;
1594 enum ds4_dongle_state dongle_state;
1595 int ret;
1596
1597 ret = dualshock4_get_calibration_data(sc);
1598 if (ret < 0) {
1599 /* This call is very unlikely to fail for the dongle. When it
1600 * fails we are probably in a very bad state, so mark the
1601 * dongle as disabled. We will re-enable the dongle if a new
1602 * DS4 hotplug is detect from sony_raw_event as any issues
1603 * are likely resolved then (the dongle is quite stupid).
1604 */
1605 hid_err(sc->hdev, "DualShock 4 USB dongle: calibration failed, disabling device\n");
1606 dongle_state = DONGLE_DISABLED;
1607 } else {
1608 hid_info(sc->hdev, "DualShock 4 USB dongle: calibration completed\n");
1609 dongle_state = DONGLE_CONNECTED;
1610 }
1611
1612 spin_lock_irqsave(&sc->lock, flags);
1613 sc->ds4_dongle_state = dongle_state;
1614 spin_unlock_irqrestore(&sc->lock, flags);
1615}
1616
Frank Praznik221399b2015-05-05 20:47:32 -04001617static void sixaxis_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001618{
Pavel Machek1adf9042016-02-09 13:55:08 +01001619 static const u8 sixaxis_leds[10][4] = {
Frank Praznik80250872014-04-14 10:11:35 -04001620 { 0x01, 0x00, 0x00, 0x00 },
1621 { 0x00, 0x01, 0x00, 0x00 },
1622 { 0x00, 0x00, 0x01, 0x00 },
1623 { 0x00, 0x00, 0x00, 0x01 },
1624 { 0x01, 0x00, 0x00, 0x01 },
1625 { 0x00, 0x01, 0x00, 0x01 },
1626 { 0x00, 0x00, 0x01, 0x01 },
1627 { 0x01, 0x00, 0x01, 0x01 },
1628 { 0x00, 0x01, 0x01, 0x01 },
1629 { 0x01, 0x01, 0x01, 0x01 }
1630 };
1631
Frank Praznik221399b2015-05-05 20:47:32 -04001632 int id = sc->device_id;
1633
1634 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001635
1636 if (id < 0)
1637 return;
1638
1639 id %= 10;
Frank Praznik221399b2015-05-05 20:47:32 -04001640 memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001641}
1642
Frank Praznik221399b2015-05-05 20:47:32 -04001643static void dualshock4_set_leds_from_id(struct sony_sc *sc)
Frank Praznik80250872014-04-14 10:11:35 -04001644{
1645 /* The first 4 color/index entries match what the PS4 assigns */
Pavel Machek1adf9042016-02-09 13:55:08 +01001646 static const u8 color_code[7][3] = {
Frank Praznik80250872014-04-14 10:11:35 -04001647 /* Blue */ { 0x00, 0x00, 0x01 },
1648 /* Red */ { 0x01, 0x00, 0x00 },
1649 /* Green */ { 0x00, 0x01, 0x00 },
1650 /* Pink */ { 0x02, 0x00, 0x01 },
1651 /* Orange */ { 0x02, 0x01, 0x00 },
1652 /* Teal */ { 0x00, 0x01, 0x01 },
1653 /* White */ { 0x01, 0x01, 0x01 }
1654 };
1655
Frank Praznik221399b2015-05-05 20:47:32 -04001656 int id = sc->device_id;
1657
1658 BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
Frank Praznik80250872014-04-14 10:11:35 -04001659
1660 if (id < 0)
1661 return;
1662
1663 id %= 7;
Frank Praznik221399b2015-05-05 20:47:32 -04001664 memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
Frank Praznik80250872014-04-14 10:11:35 -04001665}
1666
Frank Praznik221399b2015-05-05 20:47:32 -04001667static void buzz_set_leds(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001668{
Frank Praznik221399b2015-05-05 20:47:32 -04001669 struct hid_device *hdev = sc->hdev;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001670 struct list_head *report_list =
1671 &hdev->report_enum[HID_OUTPUT_REPORT].report_list;
1672 struct hid_report *report = list_entry(report_list->next,
1673 struct hid_report, list);
Pavel Machek1adf9042016-02-09 13:55:08 +01001674 s32 *value = report->field[0]->value;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001675
Frank Praznik221399b2015-05-05 20:47:32 -04001676 BUILD_BUG_ON(MAX_LEDS < 4);
1677
Colin Leitnerf04d5142013-05-27 23:41:05 +02001678 value[0] = 0x00;
Frank Praznik221399b2015-05-05 20:47:32 -04001679 value[1] = sc->led_state[0] ? 0xff : 0x00;
1680 value[2] = sc->led_state[1] ? 0xff : 0x00;
1681 value[3] = sc->led_state[2] ? 0xff : 0x00;
1682 value[4] = sc->led_state[3] ? 0xff : 0x00;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001683 value[5] = 0x00;
1684 value[6] = 0x00;
1685 hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
1686}
1687
Frank Praznik221399b2015-05-05 20:47:32 -04001688static void sony_set_leds(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001689{
Frank Praznik221399b2015-05-05 20:47:32 -04001690 if (!(sc->quirks & BUZZ_CONTROLLER))
Roderick Colenbranderb5322732017-03-07 15:45:05 -08001691 sony_schedule_work(sc, SONY_WORKER_STATE);
Frank Praznik221399b2015-05-05 20:47:32 -04001692 else
1693 buzz_set_leds(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001694}
1695
Sven Eckelmannc5382512013-11-19 20:26:30 +01001696static void sony_led_set_brightness(struct led_classdev *led,
Colin Leitnerf04d5142013-05-27 23:41:05 +02001697 enum led_brightness value)
1698{
1699 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001700 struct hid_device *hdev = to_hid_device(dev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001701 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001702
1703 int n;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001704 int force_update;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001705
1706 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001707 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001708 hid_err(hdev, "No device data\n");
1709 return;
1710 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001711
Frank Praznikb3ed4582014-04-14 10:11:36 -04001712 /*
1713 * The Sixaxis on USB will override any LED settings sent to it
1714 * and keep flashing all of the LEDs until the PS button is pressed.
1715 * Updates, even if redundant, must be always be sent to the
1716 * controller to avoid having to toggle the state of an LED just to
1717 * stop the flashing later on.
1718 */
1719 force_update = !!(drv_data->quirks & SIXAXIS_CONTROLLER_USB);
1720
Frank Praznik60781cf2014-01-11 15:13:15 -05001721 for (n = 0; n < drv_data->led_count; n++) {
Frank Praznikb3ed4582014-04-14 10:11:36 -04001722 if (led == drv_data->leds[n] && (force_update ||
1723 (value != drv_data->led_state[n] ||
1724 drv_data->led_delay_on[n] ||
1725 drv_data->led_delay_off[n]))) {
1726
1727 drv_data->led_state[n] = value;
1728
1729 /* Setting the brightness stops the blinking */
1730 drv_data->led_delay_on[n] = 0;
1731 drv_data->led_delay_off[n] = 0;
1732
Frank Praznik221399b2015-05-05 20:47:32 -04001733 sony_set_leds(drv_data);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001734 break;
1735 }
1736 }
1737}
1738
Sven Eckelmannc5382512013-11-19 20:26:30 +01001739static enum led_brightness sony_led_get_brightness(struct led_classdev *led)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001740{
1741 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001742 struct hid_device *hdev = to_hid_device(dev);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001743 struct sony_sc *drv_data;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001744
1745 int n;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001746
1747 drv_data = hid_get_drvdata(hdev);
Sven Eckelmann2251b852013-11-19 20:26:31 +01001748 if (!drv_data) {
Colin Leitnerf04d5142013-05-27 23:41:05 +02001749 hid_err(hdev, "No device data\n");
1750 return LED_OFF;
1751 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001752
Frank Praznik60781cf2014-01-11 15:13:15 -05001753 for (n = 0; n < drv_data->led_count; n++) {
Simon Wood7db75042014-02-05 12:34:18 -07001754 if (led == drv_data->leds[n])
1755 return drv_data->led_state[n];
Colin Leitnerf04d5142013-05-27 23:41:05 +02001756 }
1757
Simon Wood7db75042014-02-05 12:34:18 -07001758 return LED_OFF;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001759}
Colin Leitnerf04d5142013-05-27 23:41:05 +02001760
Frank Praznikb3ed4582014-04-14 10:11:36 -04001761static int sony_led_blink_set(struct led_classdev *led, unsigned long *delay_on,
1762 unsigned long *delay_off)
1763{
1764 struct device *dev = led->dev->parent;
Geliang Tangee79a8f2015-12-27 17:25:21 +08001765 struct hid_device *hdev = to_hid_device(dev);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001766 struct sony_sc *drv_data = hid_get_drvdata(hdev);
1767 int n;
Pavel Machek1adf9042016-02-09 13:55:08 +01001768 u8 new_on, new_off;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001769
1770 if (!drv_data) {
1771 hid_err(hdev, "No device data\n");
1772 return -EINVAL;
1773 }
1774
1775 /* Max delay is 255 deciseconds or 2550 milliseconds */
1776 if (*delay_on > 2550)
1777 *delay_on = 2550;
1778 if (*delay_off > 2550)
1779 *delay_off = 2550;
1780
1781 /* Blink at 1 Hz if both values are zero */
1782 if (!*delay_on && !*delay_off)
1783 *delay_on = *delay_off = 500;
1784
1785 new_on = *delay_on / 10;
1786 new_off = *delay_off / 10;
1787
1788 for (n = 0; n < drv_data->led_count; n++) {
1789 if (led == drv_data->leds[n])
1790 break;
1791 }
1792
1793 /* This LED is not registered on this device */
1794 if (n >= drv_data->led_count)
1795 return -EINVAL;
1796
1797 /* Don't schedule work if the values didn't change */
1798 if (new_on != drv_data->led_delay_on[n] ||
1799 new_off != drv_data->led_delay_off[n]) {
1800 drv_data->led_delay_on[n] = new_on;
1801 drv_data->led_delay_off[n] = new_off;
Roderick Colenbranderb5322732017-03-07 15:45:05 -08001802 sony_schedule_work(drv_data, SONY_WORKER_STATE);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001803 }
1804
1805 return 0;
1806}
1807
Frank Praznikfa57a812014-04-14 10:11:33 -04001808static void sony_leds_remove(struct sony_sc *sc)
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001809{
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001810 struct led_classdev *led;
1811 int n;
1812
Frank Praznikfa57a812014-04-14 10:11:33 -04001813 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001814
Frank Praznikfa57a812014-04-14 10:11:33 -04001815 for (n = 0; n < sc->led_count; n++) {
1816 led = sc->leds[n];
1817 sc->leds[n] = NULL;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001818 if (!led)
1819 continue;
1820 led_classdev_unregister(led);
1821 kfree(led);
1822 }
Frank Praznik60781cf2014-01-11 15:13:15 -05001823
Frank Praznikfa57a812014-04-14 10:11:33 -04001824 sc->led_count = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001825}
1826
Frank Praznikfa57a812014-04-14 10:11:33 -04001827static int sony_leds_init(struct sony_sc *sc)
Colin Leitnerf04d5142013-05-27 23:41:05 +02001828{
Frank Praznikfa57a812014-04-14 10:11:33 -04001829 struct hid_device *hdev = sc->hdev;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001830 int n, ret = 0;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001831 int use_ds4_names;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001832 struct led_classdev *led;
1833 size_t name_sz;
1834 char *name;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001835 size_t name_len;
1836 const char *name_fmt;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001837 static const char * const ds4_name_str[] = { "red", "green", "blue",
1838 "global" };
Pavel Machek1adf9042016-02-09 13:55:08 +01001839 u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
1840 u8 use_hw_blink[MAX_LEDS] = { 0 };
Colin Leitnerf04d5142013-05-27 23:41:05 +02001841
Frank Praznikfa57a812014-04-14 10:11:33 -04001842 BUG_ON(!(sc->quirks & SONY_LED_SUPPORT));
Colin Leitnerf04d5142013-05-27 23:41:05 +02001843
Frank Praznikfa57a812014-04-14 10:11:33 -04001844 if (sc->quirks & BUZZ_CONTROLLER) {
1845 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001846 use_ds4_names = 0;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001847 name_len = strlen("::buzz#");
1848 name_fmt = "%s::buzz%d";
1849 /* Validate expected report characteristics. */
1850 if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
1851 return -ENODEV;
Frank Praznikfa57a812014-04-14 10:11:33 -04001852 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Frank Praznik221399b2015-05-05 20:47:32 -04001853 dualshock4_set_leds_from_id(sc);
1854 sc->led_state[3] = 1;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001855 sc->led_count = 4;
1856 memset(max_brightness, 255, 3);
1857 use_hw_blink[3] = 1;
1858 use_ds4_names = 1;
Frank Praznik61ebca92014-01-20 12:27:02 -05001859 name_len = 0;
1860 name_fmt = "%s:%s";
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04001861 } else if (sc->quirks & MOTION_CONTROLLER) {
1862 sc->led_count = 3;
1863 memset(max_brightness, 255, 3);
1864 use_ds4_names = 1;
1865 name_len = 0;
1866 name_fmt = "%s:%s";
Simon Wood4545ee02015-06-17 00:08:52 -06001867 } else if (sc->quirks & NAVIGATION_CONTROLLER) {
Pavel Machek1adf9042016-02-09 13:55:08 +01001868 static const u8 navigation_leds[4] = {0x01, 0x00, 0x00, 0x00};
Simon Wood4545ee02015-06-17 00:08:52 -06001869
1870 memcpy(sc->led_state, navigation_leds, sizeof(navigation_leds));
1871 sc->led_count = 1;
1872 memset(use_hw_blink, 1, 4);
1873 use_ds4_names = 0;
1874 name_len = strlen("::sony#");
1875 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001876 } else {
Frank Praznik221399b2015-05-05 20:47:32 -04001877 sixaxis_set_leds_from_id(sc);
Frank Praznikfa57a812014-04-14 10:11:33 -04001878 sc->led_count = 4;
Frank Praznikb3ed4582014-04-14 10:11:36 -04001879 memset(use_hw_blink, 1, 4);
1880 use_ds4_names = 0;
Frank Praznik61ebca92014-01-20 12:27:02 -05001881 name_len = strlen("::sony#");
1882 name_fmt = "%s::sony%d";
Frank Praznik60781cf2014-01-11 15:13:15 -05001883 }
1884
Frank Praznikad142b92014-02-20 11:36:00 -05001885 /*
1886 * Clear LEDs as we have no way of reading their initial state. This is
Colin Leitnerf04d5142013-05-27 23:41:05 +02001887 * only relevant if the driver is loaded after somebody actively set the
Frank Praznikad142b92014-02-20 11:36:00 -05001888 * LEDs to on
1889 */
Frank Praznik221399b2015-05-05 20:47:32 -04001890 sony_set_leds(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001891
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001892 name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001893
Frank Praznikfa57a812014-04-14 10:11:33 -04001894 for (n = 0; n < sc->led_count; n++) {
Frank Praznik61ebca92014-01-20 12:27:02 -05001895
Frank Praznikb3ed4582014-04-14 10:11:36 -04001896 if (use_ds4_names)
1897 name_sz = strlen(dev_name(&hdev->dev)) + strlen(ds4_name_str[n]) + 2;
Frank Praznik61ebca92014-01-20 12:27:02 -05001898
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001899 led = kzalloc(sizeof(struct led_classdev) + name_sz, GFP_KERNEL);
1900 if (!led) {
1901 hid_err(hdev, "Couldn't allocate memory for LED %d\n", n);
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001902 ret = -ENOMEM;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001903 goto error_leds;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001904 }
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001905
1906 name = (void *)(&led[1]);
Frank Praznikb3ed4582014-04-14 10:11:36 -04001907 if (use_ds4_names)
1908 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev),
1909 ds4_name_str[n]);
Frank Praznik61ebca92014-01-20 12:27:02 -05001910 else
1911 snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001912 led->name = name;
Frank Praznik221399b2015-05-05 20:47:32 -04001913 led->brightness = sc->led_state[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001914 led->max_brightness = max_brightness[n];
Frank Praznik765a1072017-02-08 13:58:43 -05001915 led->flags = LED_CORE_SUSPENDRESUME;
Sven Eckelmannc5382512013-11-19 20:26:30 +01001916 led->brightness_get = sony_led_get_brightness;
1917 led->brightness_set = sony_led_set_brightness;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001918
Frank Praznikb3ed4582014-04-14 10:11:36 -04001919 if (use_hw_blink[n])
1920 led->blink_set = sony_led_blink_set;
1921
Frank Praznik80250872014-04-14 10:11:35 -04001922 sc->leds[n] = led;
1923
Julia Lawall8cd5fcd2013-12-29 23:47:27 +01001924 ret = led_classdev_register(&hdev->dev, led);
1925 if (ret) {
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001926 hid_err(hdev, "Failed to register LED %d\n", n);
Frank Praznik80250872014-04-14 10:11:35 -04001927 sc->leds[n] = NULL;
Jiri Kosina40e32ee2013-05-28 11:22:09 +02001928 kfree(led);
1929 goto error_leds;
1930 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001931 }
Colin Leitnerf04d5142013-05-27 23:41:05 +02001932
1933 return ret;
1934
Colin Leitnerf04d5142013-05-27 23:41:05 +02001935error_leds:
Frank Praznikfa57a812014-04-14 10:11:33 -04001936 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02001937
Colin Leitnerf04d5142013-05-27 23:41:05 +02001938 return ret;
Colin Leitnerf04d5142013-05-27 23:41:05 +02001939}
1940
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001941static void sixaxis_send_output_report(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001942{
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001943 static const union sixaxis_output_report_01 default_report = {
Frank Praznik55d3b662014-04-14 10:11:32 -04001944 .buf = {
1945 0x01,
Scott Moreauad07b7a2016-01-13 07:40:43 -07001946 0x01, 0xff, 0x00, 0xff, 0x00,
Frank Praznik55d3b662014-04-14 10:11:32 -04001947 0x00, 0x00, 0x00, 0x00, 0x00,
1948 0xff, 0x27, 0x10, 0x00, 0x32,
1949 0xff, 0x27, 0x10, 0x00, 0x32,
1950 0xff, 0x27, 0x10, 0x00, 0x32,
1951 0xff, 0x27, 0x10, 0x00, 0x32,
1952 0x00, 0x00, 0x00, 0x00, 0x00
1953 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01001954 };
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001955 struct sixaxis_output_report *report =
1956 (struct sixaxis_output_report *)sc->output_report_dmabuf;
1957 int n;
1958
1959 /* Initialize the report with default values */
1960 memcpy(report, &default_report, sizeof(struct sixaxis_output_report));
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001961
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001962#ifdef CONFIG_SONY_FF
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001963 report->rumble.right_motor_on = sc->right ? 1 : 0;
1964 report->rumble.left_motor_force = sc->left;
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01001965#endif
1966
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001967 report->leds_bitmap |= sc->led_state[0] << 1;
1968 report->leds_bitmap |= sc->led_state[1] << 2;
1969 report->leds_bitmap |= sc->led_state[2] << 3;
1970 report->leds_bitmap |= sc->led_state[3] << 4;
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001971
Simon Wood88f65762014-04-14 10:11:37 -04001972 /* Set flag for all leds off, required for 3rd party INTEC controller */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001973 if ((report->leds_bitmap & 0x1E) == 0)
1974 report->leds_bitmap |= 0x20;
Simon Wood88f65762014-04-14 10:11:37 -04001975
Frank Praznikb3ed4582014-04-14 10:11:36 -04001976 /*
1977 * The LEDs in the report are indexed in reverse order to their
1978 * corresponding light on the controller.
1979 * Index 0 = LED 4, index 1 = LED 3, etc...
1980 *
1981 * In the case of both delay values being zero (blinking disabled) the
1982 * default report values should be used or the controller LED will be
1983 * always off.
1984 */
1985 for (n = 0; n < 4; n++) {
1986 if (sc->led_delay_on[n] || sc->led_delay_off[n]) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001987 report->led[3 - n].duty_off = sc->led_delay_off[n];
1988 report->led[3 - n].duty_on = sc->led_delay_on[n];
Frank Praznikb3ed4582014-04-14 10:11:36 -04001989 }
1990 }
1991
Pavel Machek1adf9042016-02-09 13:55:08 +01001992 hid_hw_raw_request(sc->hdev, report->report_id, (u8 *)report,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05001993 sizeof(struct sixaxis_output_report),
1994 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01001995}
1996
Frank Praznikd8aaccd2015-11-11 09:49:37 -05001997static void dualshock4_send_output_report(struct sony_sc *sc)
Frank Praznik0bd88dd2014-01-11 15:12:42 -05001998{
Frank Praznik0da8ea62014-01-16 21:42:51 -05001999 struct hid_device *hdev = sc->hdev;
Pavel Machek1adf9042016-02-09 13:55:08 +01002000 u8 *buf = sc->output_report_dmabuf;
Frank Praznik48220232014-02-05 20:03:44 -05002001 int offset;
Frank Praznik0da8ea62014-01-16 21:42:51 -05002002
Frank Praznikc4425c82016-09-22 20:18:10 -04002003 /*
2004 * NOTE: The buf[1] field of the Bluetooth report controls
2005 * the Dualshock 4 reporting rate.
2006 *
2007 * Known values include:
2008 *
2009 * 0x80 - 1000hz (full speed)
2010 * 0xA0 - 31hz
2011 * 0xB0 - 20hz
2012 * 0xD0 - 66hz
2013 */
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08002014 if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07002015 memset(buf, 0, DS4_OUTPUT_REPORT_0x05_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05002016 buf[0] = 0x05;
Frank Praznikb3ed4582014-04-14 10:11:36 -04002017 buf[1] = 0xFF;
Frank Praznikfdcf105d2014-02-05 20:03:46 -05002018 offset = 4;
2019 } else {
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07002020 memset(buf, 0, DS4_OUTPUT_REPORT_0x11_SIZE);
Frank Praznikfdcf105d2014-02-05 20:03:46 -05002021 buf[0] = 0x11;
Roderick Colenbrandere7ef53a2016-10-07 12:39:37 -07002022 buf[1] = 0xC0; /* HID + CRC */
Frank Praznikfdcf105d2014-02-05 20:03:46 -05002023 buf[3] = 0x0F;
2024 offset = 6;
2025 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002026
2027#ifdef CONFIG_SONY_FF
Frank Praznik48220232014-02-05 20:03:44 -05002028 buf[offset++] = sc->right;
2029 buf[offset++] = sc->left;
2030#else
2031 offset += 2;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002032#endif
2033
Frank Praznikb3ed4582014-04-14 10:11:36 -04002034 /* LED 3 is the global control */
2035 if (sc->led_state[3]) {
2036 buf[offset++] = sc->led_state[0];
2037 buf[offset++] = sc->led_state[1];
2038 buf[offset++] = sc->led_state[2];
2039 } else {
2040 offset += 3;
2041 }
2042
2043 /* If both delay values are zero the DualShock 4 disables blinking. */
2044 buf[offset++] = sc->led_delay_on[3];
2045 buf[offset++] = sc->led_delay_off[3];
Frank Praznik60781cf2014-01-11 15:13:15 -05002046
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08002047 if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE))
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07002048 hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x05_SIZE);
Roderick Colenbrandere7ef53a2016-10-07 12:39:37 -07002049 else {
2050 /* CRC generation */
2051 u8 bthdr = 0xA2;
2052 u32 crc;
2053
2054 crc = crc32_le(0xFFFFFFFF, &bthdr, 1);
2055 crc = ~crc32_le(crc, buf, DS4_OUTPUT_REPORT_0x11_SIZE-4);
2056 put_unaligned_le32(crc, &buf[74]);
2057 hid_hw_output_report(hdev, buf, DS4_OUTPUT_REPORT_0x11_SIZE);
2058 }
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002059}
2060
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002061static void motion_send_output_report(struct sony_sc *sc)
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002062{
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002063 struct hid_device *hdev = sc->hdev;
2064 struct motion_output_report_02 *report =
2065 (struct motion_output_report_02 *)sc->output_report_dmabuf;
2066
Simon Wood41d2d422015-06-09 21:27:06 -06002067 memset(report, 0, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002068
2069 report->type = 0x02; /* set leds */
2070 report->r = sc->led_state[0];
2071 report->g = sc->led_state[1];
2072 report->b = sc->led_state[2];
2073
2074#ifdef CONFIG_SONY_FF
2075 report->rumble = max(sc->right, sc->left);
2076#endif
2077
Pavel Machek1adf9042016-02-09 13:55:08 +01002078 hid_hw_output_report(hdev, (u8 *)report, MOTION_REPORT_0x02_SIZE);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002079}
2080
Frank Praznikdecd9462015-11-11 09:49:38 -05002081static inline void sony_send_output_report(struct sony_sc *sc)
2082{
2083 if (sc->send_output_report)
2084 sc->send_output_report(sc);
2085}
2086
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002087static void sony_state_worker(struct work_struct *work)
2088{
2089 struct sony_sc *sc = container_of(work, struct sony_sc, state_worker);
Antonio Ospiteef916ef2016-02-09 13:55:07 +01002090
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002091 sc->send_output_report(sc);
2092}
2093
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002094static int sony_allocate_output_report(struct sony_sc *sc)
2095{
Simon Wood4545ee02015-06-17 00:08:52 -06002096 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2097 (sc->quirks & NAVIGATION_CONTROLLER))
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002098 sc->output_report_dmabuf =
2099 kmalloc(sizeof(union sixaxis_output_report_01),
2100 GFP_KERNEL);
2101 else if (sc->quirks & DUALSHOCK4_CONTROLLER_BT)
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07002102 sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x11_SIZE,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002103 GFP_KERNEL);
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08002104 else if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE))
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07002105 sc->output_report_dmabuf = kmalloc(DS4_OUTPUT_REPORT_0x05_SIZE,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002106 GFP_KERNEL);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002107 else if (sc->quirks & MOTION_CONTROLLER)
Simon Wood41d2d422015-06-09 21:27:06 -06002108 sc->output_report_dmabuf = kmalloc(MOTION_REPORT_0x02_SIZE,
2109 GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002110 else
2111 return 0;
2112
2113 if (!sc->output_report_dmabuf)
2114 return -ENOMEM;
2115
2116 return 0;
2117}
2118
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002119#ifdef CONFIG_SONY_FF
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002120static int sony_play_effect(struct input_dev *dev, void *data,
2121 struct ff_effect *effect)
2122{
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002123 struct hid_device *hid = input_get_drvdata(dev);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002124 struct sony_sc *sc = hid_get_drvdata(hid);
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002125
2126 if (effect->type != FF_RUMBLE)
2127 return 0;
2128
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002129 sc->left = effect->u.rumble.strong_magnitude / 256;
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002130 sc->right = effect->u.rumble.weak_magnitude / 256;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002131
Roderick Colenbranderb5322732017-03-07 15:45:05 -08002132 sony_schedule_work(sc, SONY_WORKER_STATE);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002133 return 0;
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002134}
2135
Frank Praznikfa57a812014-04-14 10:11:33 -04002136static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002137{
Frank Praznikfa57a812014-04-14 10:11:33 -04002138 struct hid_input *hidinput = list_entry(sc->hdev->inputs.next,
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002139 struct hid_input, list);
2140 struct input_dev *input_dev = hidinput->input;
2141
2142 input_set_capability(input_dev, EV_FF, FF_RUMBLE);
2143 return input_ff_create_memless(input_dev, NULL, sony_play_effect);
2144}
2145
2146#else
Frank Praznikfa57a812014-04-14 10:11:33 -04002147static int sony_init_ff(struct sony_sc *sc)
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002148{
2149 return 0;
2150}
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002151
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002152#endif
2153
Frank Praznikd902f472014-01-27 10:17:36 -05002154static int sony_battery_get_property(struct power_supply *psy,
2155 enum power_supply_property psp,
2156 union power_supply_propval *val)
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002157{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002158 struct sony_sc *sc = power_supply_get_drvdata(psy);
Frank Praznikd902f472014-01-27 10:17:36 -05002159 unsigned long flags;
2160 int ret = 0;
2161 u8 battery_charging, battery_capacity, cable_state;
2162
2163 spin_lock_irqsave(&sc->lock, flags);
2164 battery_charging = sc->battery_charging;
2165 battery_capacity = sc->battery_capacity;
2166 cable_state = sc->cable_state;
2167 spin_unlock_irqrestore(&sc->lock, flags);
2168
2169 switch (psp) {
2170 case POWER_SUPPLY_PROP_PRESENT:
2171 val->intval = 1;
2172 break;
2173 case POWER_SUPPLY_PROP_SCOPE:
2174 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
2175 break;
2176 case POWER_SUPPLY_PROP_CAPACITY:
2177 val->intval = battery_capacity;
2178 break;
2179 case POWER_SUPPLY_PROP_STATUS:
2180 if (battery_charging)
2181 val->intval = POWER_SUPPLY_STATUS_CHARGING;
2182 else
2183 if (battery_capacity == 100 && cable_state)
2184 val->intval = POWER_SUPPLY_STATUS_FULL;
2185 else
2186 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
2187 break;
2188 default:
2189 ret = -EINVAL;
2190 break;
2191 }
2192 return ret;
2193}
2194
Frank Praznik0f398232016-09-22 20:18:08 -04002195static int sony_battery_probe(struct sony_sc *sc, int append_dev_id)
Frank Praznikd902f472014-01-27 10:17:36 -05002196{
Frank Praznik0f398232016-09-22 20:18:08 -04002197 const char *battery_str_fmt = append_dev_id ?
2198 "sony_controller_battery_%pMR_%i" :
2199 "sony_controller_battery_%pMR";
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002200 struct power_supply_config psy_cfg = { .drv_data = sc, };
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002201 struct hid_device *hdev = sc->hdev;
Frank Praznikd902f472014-01-27 10:17:36 -05002202 int ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002203
Frank Praznikad142b92014-02-20 11:36:00 -05002204 /*
2205 * Set the default battery level to 100% to avoid low battery warnings
Frank Praznikd9a293a2014-02-05 20:03:48 -05002206 * if the battery is polled before the first device report is received.
2207 */
2208 sc->battery_capacity = 100;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002209
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002210 sc->battery_desc.properties = sony_battery_props;
2211 sc->battery_desc.num_properties = ARRAY_SIZE(sony_battery_props);
2212 sc->battery_desc.get_property = sony_battery_get_property;
2213 sc->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
2214 sc->battery_desc.use_for_apm = 0;
Frank Praznik0f398232016-09-22 20:18:08 -04002215 sc->battery_desc.name = kasprintf(GFP_KERNEL, battery_str_fmt,
2216 sc->mac_address, sc->device_id);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002217 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05002218 return -ENOMEM;
2219
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002220 sc->battery = power_supply_register(&hdev->dev, &sc->battery_desc,
2221 &psy_cfg);
2222 if (IS_ERR(sc->battery)) {
2223 ret = PTR_ERR(sc->battery);
Frank Praznikd902f472014-01-27 10:17:36 -05002224 hid_err(hdev, "Unable to register battery device\n");
2225 goto err_free;
2226 }
2227
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002228 power_supply_powers(sc->battery, &hdev->dev);
Frank Praznikd902f472014-01-27 10:17:36 -05002229 return 0;
2230
2231err_free:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002232 kfree(sc->battery_desc.name);
2233 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05002234 return ret;
2235}
2236
2237static void sony_battery_remove(struct sony_sc *sc)
2238{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002239 if (!sc->battery_desc.name)
Frank Praznikd902f472014-01-27 10:17:36 -05002240 return;
2241
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +01002242 power_supply_unregister(sc->battery);
2243 kfree(sc->battery_desc.name);
2244 sc->battery_desc.name = NULL;
Frank Praznikd902f472014-01-27 10:17:36 -05002245}
2246
Frank Praznikd2d782f2014-02-20 11:36:03 -05002247/*
2248 * If a controller is plugged in via USB while already connected via Bluetooth
2249 * it will show up as two devices. A global list of connected controllers and
2250 * their MAC addresses is maintained to ensure that a device is only connected
2251 * once.
Frank Praznik0f398232016-09-22 20:18:08 -04002252 *
2253 * Some USB-only devices masquerade as Sixaxis controllers and all have the
2254 * same dummy Bluetooth address, so a comparison of the connection type is
2255 * required. Devices are only rejected in the case where two devices have
2256 * matching Bluetooth addresses on different bus types.
Frank Praznikd2d782f2014-02-20 11:36:03 -05002257 */
Frank Praznik0f398232016-09-22 20:18:08 -04002258static inline int sony_compare_connection_type(struct sony_sc *sc0,
2259 struct sony_sc *sc1)
2260{
2261 const int sc0_not_bt = !(sc0->quirks & SONY_BT_DEVICE);
2262 const int sc1_not_bt = !(sc1->quirks & SONY_BT_DEVICE);
2263
2264 return sc0_not_bt == sc1_not_bt;
2265}
2266
Frank Praznikd2d782f2014-02-20 11:36:03 -05002267static int sony_check_add_dev_list(struct sony_sc *sc)
2268{
2269 struct sony_sc *entry;
2270 unsigned long flags;
2271 int ret;
2272
2273 spin_lock_irqsave(&sony_dev_list_lock, flags);
2274
2275 list_for_each_entry(entry, &sony_device_list, list_node) {
2276 ret = memcmp(sc->mac_address, entry->mac_address,
2277 sizeof(sc->mac_address));
2278 if (!ret) {
Frank Praznik0f398232016-09-22 20:18:08 -04002279 if (sony_compare_connection_type(sc, entry)) {
2280 ret = 1;
2281 } else {
2282 ret = -EEXIST;
2283 hid_info(sc->hdev,
2284 "controller with MAC address %pMR already connected\n",
Frank Praznikd2d782f2014-02-20 11:36:03 -05002285 sc->mac_address);
Frank Praznik0f398232016-09-22 20:18:08 -04002286 }
Frank Praznikd2d782f2014-02-20 11:36:03 -05002287 goto unlock;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002288 }
2289 }
2290
Frank Praznikd2d782f2014-02-20 11:36:03 -05002291 ret = 0;
2292 list_add(&(sc->list_node), &sony_device_list);
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002293
Frank Praznikd2d782f2014-02-20 11:36:03 -05002294unlock:
2295 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2296 return ret;
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002297}
2298
Frank Praznikd2d782f2014-02-20 11:36:03 -05002299static void sony_remove_dev_list(struct sony_sc *sc)
2300{
2301 unsigned long flags;
2302
2303 if (sc->list_node.next) {
2304 spin_lock_irqsave(&sony_dev_list_lock, flags);
2305 list_del(&(sc->list_node));
2306 spin_unlock_irqrestore(&sony_dev_list_lock, flags);
2307 }
2308}
2309
2310static int sony_get_bt_devaddr(struct sony_sc *sc)
2311{
2312 int ret;
2313
2314 /* HIDP stores the device MAC address as a string in the uniq field. */
2315 ret = strlen(sc->hdev->uniq);
2316 if (ret != 17)
2317 return -EINVAL;
2318
2319 ret = sscanf(sc->hdev->uniq,
2320 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2321 &sc->mac_address[5], &sc->mac_address[4], &sc->mac_address[3],
2322 &sc->mac_address[2], &sc->mac_address[1], &sc->mac_address[0]);
2323
2324 if (ret != 6)
2325 return -EINVAL;
2326
2327 return 0;
2328}
2329
2330static int sony_check_add(struct sony_sc *sc)
2331{
Pavel Machek1adf9042016-02-09 13:55:08 +01002332 u8 *buf = NULL;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002333 int n, ret;
2334
2335 if ((sc->quirks & DUALSHOCK4_CONTROLLER_BT) ||
Simon Wood12e9a6d72015-06-09 21:27:05 -06002336 (sc->quirks & MOTION_CONTROLLER_BT) ||
Simon Wood4545ee02015-06-17 00:08:52 -06002337 (sc->quirks & NAVIGATION_CONTROLLER_BT) ||
Frank Praznikd2d782f2014-02-20 11:36:03 -05002338 (sc->quirks & SIXAXIS_CONTROLLER_BT)) {
2339 /*
2340 * sony_get_bt_devaddr() attempts to parse the Bluetooth MAC
2341 * address from the uniq string where HIDP stores it.
2342 * As uniq cannot be guaranteed to be a MAC address in all cases
2343 * a failure of this function should not prevent the connection.
2344 */
2345 if (sony_get_bt_devaddr(sc) < 0) {
2346 hid_warn(sc->hdev, "UNIQ does not contain a MAC address; duplicate check skipped\n");
2347 return 0;
2348 }
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08002349 } else if (sc->quirks & (DUALSHOCK4_CONTROLLER_USB | DUALSHOCK4_DONGLE)) {
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07002350 buf = kmalloc(DS4_FEATURE_REPORT_0x81_SIZE, GFP_KERNEL);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002351 if (!buf)
2352 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002353
2354 /*
2355 * The MAC address of a DS4 controller connected via USB can be
2356 * retrieved with feature report 0x81. The address begins at
2357 * offset 1.
2358 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002359 ret = hid_hw_raw_request(sc->hdev, 0x81, buf,
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07002360 DS4_FEATURE_REPORT_0x81_SIZE, HID_FEATURE_REPORT,
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002361 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002362
Roderick Colenbrander2c159de2016-10-07 12:39:35 -07002363 if (ret != DS4_FEATURE_REPORT_0x81_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002364 hid_err(sc->hdev, "failed to retrieve feature report 0x81 with the DualShock 4 MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002365 ret = ret < 0 ? ret : -EINVAL;
2366 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002367 }
2368
2369 memcpy(sc->mac_address, &buf[1], sizeof(sc->mac_address));
Roderick Colenbranderc70d5f72016-12-08 19:09:51 -08002370
2371 snprintf(sc->hdev->uniq, sizeof(sc->hdev->uniq),
2372 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2373 sc->mac_address[5], sc->mac_address[4],
2374 sc->mac_address[3], sc->mac_address[2],
2375 sc->mac_address[1], sc->mac_address[0]);
Simon Wood4545ee02015-06-17 00:08:52 -06002376 } else if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2377 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002378 buf = kmalloc(SIXAXIS_REPORT_0xF2_SIZE, GFP_KERNEL);
2379 if (!buf)
2380 return -ENOMEM;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002381
2382 /*
2383 * The MAC address of a Sixaxis controller connected via USB can
2384 * be retrieved with feature report 0xf2. The address begins at
2385 * offset 4.
2386 */
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002387 ret = hid_hw_raw_request(sc->hdev, 0xf2, buf,
2388 SIXAXIS_REPORT_0xF2_SIZE, HID_FEATURE_REPORT,
2389 HID_REQ_GET_REPORT);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002390
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002391 if (ret != SIXAXIS_REPORT_0xF2_SIZE) {
Frank Praznikd2d782f2014-02-20 11:36:03 -05002392 hid_err(sc->hdev, "failed to retrieve feature report 0xf2 with the Sixaxis MAC address\n");
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002393 ret = ret < 0 ? ret : -EINVAL;
2394 goto out_free;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002395 }
2396
2397 /*
2398 * The Sixaxis device MAC in the report is big-endian and must
2399 * be byte-swapped.
2400 */
2401 for (n = 0; n < 6; n++)
2402 sc->mac_address[5-n] = buf[4+n];
Roderick Colenbrander5a144be2017-03-24 15:17:45 -07002403
2404 snprintf(sc->hdev->uniq, sizeof(sc->hdev->uniq),
2405 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2406 sc->mac_address[5], sc->mac_address[4],
2407 sc->mac_address[3], sc->mac_address[2],
2408 sc->mac_address[1], sc->mac_address[0]);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002409 } else {
2410 return 0;
2411 }
2412
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002413 ret = sony_check_add_dev_list(sc);
2414
2415out_free:
2416
2417 kfree(buf);
2418
2419 return ret;
Frank Praznikd2d782f2014-02-20 11:36:03 -05002420}
2421
Frank Praznik80250872014-04-14 10:11:35 -04002422static int sony_set_device_id(struct sony_sc *sc)
2423{
2424 int ret;
2425
2426 /*
2427 * Only DualShock 4 or Sixaxis controllers get an id.
2428 * All others are set to -1.
2429 */
2430 if ((sc->quirks & SIXAXIS_CONTROLLER) ||
2431 (sc->quirks & DUALSHOCK4_CONTROLLER)) {
2432 ret = ida_simple_get(&sony_device_id_allocator, 0, 0,
2433 GFP_KERNEL);
2434 if (ret < 0) {
2435 sc->device_id = -1;
2436 return ret;
2437 }
2438 sc->device_id = ret;
2439 } else {
2440 sc->device_id = -1;
2441 }
2442
2443 return 0;
2444}
2445
2446static void sony_release_device_id(struct sony_sc *sc)
2447{
2448 if (sc->device_id >= 0) {
2449 ida_simple_remove(&sony_device_id_allocator, sc->device_id);
2450 sc->device_id = -1;
2451 }
2452}
2453
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002454static inline void sony_init_output_report(struct sony_sc *sc,
Antonio Ospite09593e32016-02-09 13:55:06 +01002455 void (*send_output_report)(struct sony_sc *))
Frank Praznik46262042014-04-14 10:11:31 -04002456{
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002457 sc->send_output_report = send_output_report;
2458
Roderick Colenbranderb5322732017-03-07 15:45:05 -08002459 if (!sc->state_worker_initialized)
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002460 INIT_WORK(&sc->state_worker, sony_state_worker);
Frank Praznik46262042014-04-14 10:11:31 -04002461
Roderick Colenbranderb5322732017-03-07 15:45:05 -08002462 sc->state_worker_initialized = 1;
Frank Praznik46262042014-04-14 10:11:31 -04002463}
2464
2465static inline void sony_cancel_work_sync(struct sony_sc *sc)
2466{
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -08002467 if (sc->hotplug_worker_initialized)
2468 cancel_work_sync(&sc->hotplug_worker);
Roderick Colenbranderb5322732017-03-07 15:45:05 -08002469 if (sc->state_worker_initialized)
Frank Praznik46262042014-04-14 10:11:31 -04002470 cancel_work_sync(&sc->state_worker);
2471}
Frank Praznikd2d782f2014-02-20 11:36:03 -05002472
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002473static int sony_input_configured(struct hid_device *hdev,
2474 struct hid_input *hidinput)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002475{
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002476 struct sony_sc *sc = hid_get_drvdata(hdev);
Frank Praznik0f398232016-09-22 20:18:08 -04002477 int append_dev_id;
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002478 int ret;
Jiri Slabybd28ce02008-06-25 23:47:04 +02002479
Frank Praznik80250872014-04-14 10:11:35 -04002480 ret = sony_set_device_id(sc);
2481 if (ret < 0) {
2482 hid_err(hdev, "failed to allocate the device id\n");
2483 goto err_stop;
2484 }
2485
Roderick Colenbranderdf848bc2017-03-07 15:45:11 -08002486 ret = append_dev_id = sony_check_add(sc);
2487 if (ret < 0)
2488 goto err_stop;
2489
Frank Praznik131a8a92015-05-05 20:47:28 -04002490 ret = sony_allocate_output_report(sc);
2491 if (ret < 0) {
2492 hid_err(hdev, "failed to allocate the output report buffer\n");
2493 goto err_stop;
2494 }
2495
Roderick Colenbrander510c8b72017-03-07 15:45:10 -08002496 if (sc->quirks & NAVIGATION_CONTROLLER_USB) {
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002497 /*
2498 * The Sony Sixaxis does not handle HID Output Reports on the
2499 * Interrupt EP like it could, so we need to force HID Output
2500 * Reports to use HID_REQ_SET_REPORT on the Control EP.
2501 *
2502 * There is also another issue about HID Output Reports via USB,
2503 * the Sixaxis does not want the report_id as part of the data
2504 * packet, so we have to discard buf[0] when sending the actual
2505 * control message, even for numbered reports, humpf!
Frank Praznik2a242932016-09-22 20:18:09 -04002506 *
2507 * Additionally, the Sixaxis on USB isn't properly initialized
2508 * until the PS logo button is pressed and as such won't retain
2509 * any state set by an output report, so the initial
2510 * configuration report is deferred until the first input
2511 * report arrives.
Benjamin Tissoirese534a932014-03-08 22:52:42 -05002512 */
2513 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2514 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
Frank Praznik2a242932016-09-22 20:18:09 -04002515 sc->defer_initialization = 1;
Roderick Colenbrander80ecc482017-03-07 15:45:09 -08002516
Antonio Ospite816651a2010-05-03 22:15:55 +02002517 ret = sixaxis_set_operational_usb(hdev);
Roderick Colenbrander80ecc482017-03-07 15:45:09 -08002518 if (ret < 0) {
2519 hid_err(hdev, "Failed to set controller into operational mode\n");
2520 goto err_stop;
2521 }
2522
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002523 sony_init_output_report(sc, sixaxis_send_output_report);
Roderick Colenbrander510c8b72017-03-07 15:45:10 -08002524 } else if (sc->quirks & NAVIGATION_CONTROLLER_BT) {
2525 /*
2526 * The Navigation controller wants output reports sent on the ctrl
2527 * endpoint when connected via Bluetooth.
2528 */
2529 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2530
2531 ret = sixaxis_set_operational_bt(hdev);
2532 if (ret < 0) {
2533 hid_err(hdev, "Failed to set controller into operational mode\n");
2534 goto err_stop;
2535 }
2536
2537 sony_init_output_report(sc, sixaxis_send_output_report);
2538 } else if (sc->quirks & SIXAXIS_CONTROLLER_USB) {
2539 /*
2540 * The Sony Sixaxis does not handle HID Output Reports on the
2541 * Interrupt EP and the device only becomes active when the
2542 * PS button is pressed. See comment for Navigation controller
2543 * above for more details.
2544 */
2545 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
2546 hdev->quirks |= HID_QUIRK_SKIP_OUTPUT_REPORT_ID;
2547 sc->defer_initialization = 1;
2548
2549 ret = sixaxis_set_operational_usb(hdev);
2550 if (ret < 0) {
2551 hid_err(hdev, "Failed to set controller into operational mode\n");
2552 goto err_stop;
2553 }
2554
2555 ret = sony_register_sensors(sc);
2556 if (ret) {
2557 hid_err(sc->hdev,
2558 "Unable to initialize motion sensors: %d\n", ret);
2559 goto err_stop;
2560 }
2561
2562 sony_init_output_report(sc, sixaxis_send_output_report);
2563 } else if (sc->quirks & SIXAXIS_CONTROLLER_BT) {
Frank Praznik2078b9bb2014-03-15 09:41:16 -04002564 /*
2565 * The Sixaxis wants output reports sent on the ctrl endpoint
2566 * when connected via Bluetooth.
2567 */
2568 hdev->quirks |= HID_QUIRK_NO_OUTPUT_REPORTS_ON_INTR_EP;
Roderick Colenbrander80ecc482017-03-07 15:45:09 -08002569
Antonio Ospite816651a2010-05-03 22:15:55 +02002570 ret = sixaxis_set_operational_bt(hdev);
Roderick Colenbrander80ecc482017-03-07 15:45:09 -08002571 if (ret < 0) {
2572 hid_err(hdev, "Failed to set controller into operational mode\n");
2573 goto err_stop;
2574 }
2575
Roderick Colenbrander510c8b72017-03-07 15:45:10 -08002576 ret = sony_register_sensors(sc);
2577 if (ret) {
2578 hid_err(sc->hdev,
2579 "Unable to initialize motion sensors: %d\n", ret);
2580 goto err_stop;
2581 }
2582
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002583 sony_init_output_report(sc, sixaxis_send_output_report);
Frank Praznikfee4e2d2014-02-18 17:22:01 -05002584 } else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
Roderick Colenbrander55a07d62017-03-07 15:45:01 -08002585 ret = dualshock4_get_calibration_data(sc);
2586 if (ret < 0) {
2587 hid_err(hdev, "Failed to get calibration data from Dualshock 4\n");
2588 goto err_stop;
Frank Praznik68330d82014-02-05 20:03:49 -05002589 }
Frank Praznikc4e1ddf2014-01-17 14:46:26 -05002590
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002591 /*
2592 * The Dualshock 4 touchpad supports 2 touches and has a
2593 * resolution of 1920x942 (44.86 dots/mm).
2594 */
Roderick Colenbranderac797b92016-11-23 14:07:07 -08002595 ret = sony_register_touchpad(sc, 2, 1920, 942);
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002596 if (ret) {
2597 hid_err(sc->hdev,
2598 "Unable to initialize multi-touch slots: %d\n",
2599 ret);
Roderick Colenbrander2b6579d2016-12-08 19:09:50 -08002600 goto err_stop;
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002601 }
2602
Roderick Colenbrander227c0112017-03-07 15:45:00 -08002603 ret = sony_register_sensors(sc);
2604 if (ret) {
2605 hid_err(sc->hdev,
2606 "Unable to initialize motion sensors: %d\n", ret);
2607 goto err_stop;
2608 }
2609
Roderick Colenbranderf2f47c32017-03-07 15:45:06 -08002610 if (sc->quirks & DUALSHOCK4_DONGLE) {
2611 INIT_WORK(&sc->hotplug_worker, dualshock4_calibration_work);
2612 sc->hotplug_worker_initialized = 1;
2613 sc->ds4_dongle_state = DONGLE_DISCONNECTED;
2614 }
2615
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002616 sony_init_output_report(sc, dualshock4_send_output_report);
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002617 } else if (sc->quirks & MOTION_CONTROLLER) {
Frank Praznikd8aaccd2015-11-11 09:49:37 -05002618 sony_init_output_report(sc, motion_send_output_report);
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002619 } else {
2620 ret = 0;
2621 }
Bastien Noceraf9ce7c22010-01-20 12:01:53 +00002622
Jiri Kosina4dfdc462008-12-30 00:49:59 +01002623 if (ret < 0)
Jiri Slabybd28ce02008-06-25 23:47:04 +02002624 goto err_stop;
2625
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002626 if (sc->quirks & SONY_LED_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002627 ret = sony_leds_init(sc);
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002628 if (ret < 0)
2629 goto err_stop;
2630 }
2631
Frank Praznikd902f472014-01-27 10:17:36 -05002632 if (sc->quirks & SONY_BATTERY_SUPPORT) {
Frank Praznik0f398232016-09-22 20:18:08 -04002633 ret = sony_battery_probe(sc, append_dev_id);
Frank Praznikd902f472014-01-27 10:17:36 -05002634 if (ret < 0)
2635 goto err_stop;
2636
2637 /* Open the device to receive reports with battery info */
2638 ret = hid_hw_open(hdev);
2639 if (ret < 0) {
2640 hid_err(hdev, "hw open failed\n");
2641 goto err_stop;
2642 }
2643 }
2644
Frank Praznikc8de9db2014-02-20 11:36:01 -05002645 if (sc->quirks & SONY_FF_SUPPORT) {
Frank Praznikfa57a812014-04-14 10:11:33 -04002646 ret = sony_init_ff(sc);
Frank Praznikc8de9db2014-02-20 11:36:01 -05002647 if (ret < 0)
2648 goto err_close;
Frank Praznik5f5750d2014-02-19 13:09:22 -05002649 }
Sven Eckelmanna08c22c2013-11-09 19:25:57 +01002650
Jiri Slabybd28ce02008-06-25 23:47:04 +02002651 return 0;
Frank Praznikd902f472014-01-27 10:17:36 -05002652err_close:
2653 hid_hw_close(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002654err_stop:
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002655 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002656 sony_leds_remove(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002657 if (sc->quirks & SONY_BATTERY_SUPPORT)
2658 sony_battery_remove(sc);
Frank Praznik46262042014-04-14 10:11:31 -04002659 sony_cancel_work_sync(sc);
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002660 kfree(sc->output_report_dmabuf);
Frank Praznikd2d782f2014-02-20 11:36:03 -05002661 sony_remove_dev_list(sc);
Frank Praznik80250872014-04-14 10:11:35 -04002662 sony_release_device_id(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002663 hid_hw_stop(hdev);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002664 return ret;
2665}
2666
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002667static int sony_probe(struct hid_device *hdev, const struct hid_device_id *id)
2668{
2669 int ret;
2670 unsigned long quirks = id->driver_data;
2671 struct sony_sc *sc;
2672 unsigned int connect_mask = HID_CONNECT_DEFAULT;
2673
2674 if (!strcmp(hdev->name, "FutureMax Dance Mat"))
2675 quirks |= FUTUREMAX_DANCE_MAT;
2676
2677 sc = devm_kzalloc(&hdev->dev, sizeof(*sc), GFP_KERNEL);
2678 if (sc == NULL) {
2679 hid_err(hdev, "can't alloc sony descriptor\n");
2680 return -ENOMEM;
2681 }
2682
2683 spin_lock_init(&sc->lock);
2684
2685 sc->quirks = quirks;
2686 hid_set_drvdata(hdev, sc);
2687 sc->hdev = hdev;
2688
2689 ret = hid_parse(hdev);
2690 if (ret) {
2691 hid_err(hdev, "parse failed\n");
2692 return ret;
2693 }
2694
2695 if (sc->quirks & VAIO_RDESC_CONSTANT)
2696 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2697 else if (sc->quirks & SIXAXIS_CONTROLLER)
2698 connect_mask |= HID_CONNECT_HIDDEV_FORCE;
2699
Roderick Colenbrandere19a2672017-03-07 15:45:08 -08002700 /* Patch the hw version on DS3/4 compatible devices, so applications can
Roderick Colenbrander9131f8c2016-11-23 14:07:08 -08002701 * distinguish between the default HID mappings and the mappings defined
2702 * by the Linux game controller spec. This is important for the SDL2
2703 * library, which has a game controller database, which uses device ids
2704 * in combination with version as a key.
2705 */
Roderick Colenbrandere19a2672017-03-07 15:45:08 -08002706 if (sc->quirks & (SIXAXIS_CONTROLLER | DUALSHOCK4_CONTROLLER))
Roderick Colenbrander9131f8c2016-11-23 14:07:08 -08002707 hdev->version |= 0x8000;
2708
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002709 ret = hid_hw_start(hdev, connect_mask);
2710 if (ret) {
2711 hid_err(hdev, "hw start failed\n");
2712 return ret;
2713 }
2714
Roderick Colenbrander4f967f62016-11-23 14:07:06 -08002715 /* sony_input_configured can fail, but this doesn't result
2716 * in hid_hw_start failures (intended). Check whether
2717 * the HID layer claimed the device else fail.
2718 * We don't know the actual reason for the failure, most
2719 * likely it is due to EEXIST in case of double connection
2720 * of USB and Bluetooth, but could have been due to ENOMEM
2721 * or other reasons as well.
2722 */
2723 if (!(hdev->claimed & HID_CLAIMED_INPUT)) {
2724 hid_err(hdev, "failed to claim input\n");
2725 return -ENODEV;
2726 }
2727
Roderick Colenbrandere1bc84d2016-10-07 12:39:34 -07002728 return ret;
2729}
2730
Jiri Kosinacc6e0bb2008-10-23 12:58:38 +02002731static void sony_remove(struct hid_device *hdev)
2732{
Colin Leitnerf04d5142013-05-27 23:41:05 +02002733 struct sony_sc *sc = hid_get_drvdata(hdev);
2734
Roderick Colenbranderac797b92016-11-23 14:07:07 -08002735 hid_hw_close(hdev);
2736
Sven Eckelmann0a286ef2013-11-19 20:26:32 +01002737 if (sc->quirks & SONY_LED_SUPPORT)
Frank Praznikfa57a812014-04-14 10:11:33 -04002738 sony_leds_remove(sc);
Colin Leitnerf04d5142013-05-27 23:41:05 +02002739
Roderick Colenbranderac797b92016-11-23 14:07:07 -08002740 if (sc->quirks & SONY_BATTERY_SUPPORT)
Frank Praznikd902f472014-01-27 10:17:36 -05002741 sony_battery_remove(sc);
Roderick Colenbranderac797b92016-11-23 14:07:07 -08002742
2743 if (sc->touchpad)
2744 sony_unregister_touchpad(sc);
Frank Praznikd902f472014-01-27 10:17:36 -05002745
Roderick Colenbrander227c0112017-03-07 15:45:00 -08002746 if (sc->sensor_dev)
2747 sony_unregister_sensors(sc);
2748
2749 if (sc->sensor_dev)
2750 sony_unregister_sensors(sc);
2751
Frank Praznik46262042014-04-14 10:11:31 -04002752 sony_cancel_work_sync(sc);
Sven Eckelmann9f323b62013-11-17 20:38:21 +01002753
Frank Praznik9b2b5c92014-11-12 14:10:09 -05002754 kfree(sc->output_report_dmabuf);
2755
Frank Praznikd2d782f2014-02-20 11:36:03 -05002756 sony_remove_dev_list(sc);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002757
Frank Praznik80250872014-04-14 10:11:35 -04002758 sony_release_device_id(sc);
2759
Jiri Slabybd28ce02008-06-25 23:47:04 +02002760 hid_hw_stop(hdev);
2761}
2762
Frank Praznikdecd9462015-11-11 09:49:38 -05002763#ifdef CONFIG_PM
2764
2765static int sony_suspend(struct hid_device *hdev, pm_message_t message)
2766{
Frank Praznik765a1072017-02-08 13:58:43 -05002767#ifdef CONFIG_SONY_FF
2768
2769 /* On suspend stop any running force-feedback events */
2770 if (SONY_FF_SUPPORT) {
Frank Praznikdecd9462015-11-11 09:49:38 -05002771 struct sony_sc *sc = hid_get_drvdata(hdev);
2772
Frank Praznikdecd9462015-11-11 09:49:38 -05002773 sc->left = sc->right = 0;
Frank Praznikdecd9462015-11-11 09:49:38 -05002774 sony_send_output_report(sc);
2775 }
2776
Frank Praznik765a1072017-02-08 13:58:43 -05002777#endif
Frank Praznikdecd9462015-11-11 09:49:38 -05002778 return 0;
2779}
2780
2781static int sony_resume(struct hid_device *hdev)
2782{
Frank Praznik765a1072017-02-08 13:58:43 -05002783 struct sony_sc *sc = hid_get_drvdata(hdev);
Frank Praznikdecd9462015-11-11 09:49:38 -05002784
Frank Praznik765a1072017-02-08 13:58:43 -05002785 /*
2786 * The Sixaxis and navigation controllers on USB need to be
2787 * reinitialized on resume or they won't behave properly.
2788 */
2789 if ((sc->quirks & SIXAXIS_CONTROLLER_USB) ||
2790 (sc->quirks & NAVIGATION_CONTROLLER_USB)) {
2791 sixaxis_set_operational_usb(sc->hdev);
2792 sc->defer_initialization = 1;
Frank Praznikdecd9462015-11-11 09:49:38 -05002793 }
2794
2795 return 0;
2796}
2797
2798#endif
2799
Jiri Slabybd28ce02008-06-25 23:47:04 +02002800static const struct hid_device_id sony_devices[] = {
2801 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2802 .driver_data = SIXAXIS_CONTROLLER_USB },
2803 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
Simon Wood4545ee02015-06-17 00:08:52 -06002804 .driver_data = NAVIGATION_CONTROLLER_USB },
Simon Wood6eabaaa2015-06-17 00:08:51 -06002805 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_NAVIGATION_CONTROLLER),
Simon Wood4545ee02015-06-17 00:08:52 -06002806 .driver_data = NAVIGATION_CONTROLLER_BT },
Frank Praznikc5e0c1c2015-05-05 20:47:30 -04002807 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002808 .driver_data = MOTION_CONTROLLER_USB },
Simon Wooda4afa852015-06-03 09:45:19 -06002809 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_MOTION_CONTROLLER),
Simon Woodb3bca322015-06-09 21:27:04 -06002810 .driver_data = MOTION_CONTROLLER_BT },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002811 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_CONTROLLER),
2812 .driver_data = SIXAXIS_CONTROLLER_BT },
2813 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGX_MOUSE),
2814 .driver_data = VAIO_RDESC_CONSTANT },
2815 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_VAIO_VGP_MOUSE),
2816 .driver_data = VAIO_RDESC_CONSTANT },
Antonio Ospiteef916ef2016-02-09 13:55:07 +01002817 /*
2818 * Wired Buzz Controller. Reported as Sony Hub from its USB ID and as
2819 * Logitech joystick from the device descriptor.
2820 */
Jiri Slabybd28ce02008-06-25 23:47:04 +02002821 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_BUZZ_CONTROLLER),
2822 .driver_data = BUZZ_CONTROLLER },
2823 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_WIRELESS_BUZZ_CONTROLLER),
2824 .driver_data = BUZZ_CONTROLLER },
2825 /* PS3 BD Remote Control */
2826 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS3_BDREMOTE),
2827 .driver_data = PS3REMOTE },
2828 /* Logitech Harmony Adapter for PS3 */
2829 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_HARMONY_PS3),
2830 .driver_data = PS3REMOTE },
Frank Praznik68a49e52014-11-12 14:52:28 -05002831 /* SMK-Link PS3 BD Remote Control */
2832 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SMK, USB_DEVICE_ID_SMK_PS3_BDREMOTE),
2833 .driver_data = PS3REMOTE },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002834 /* Sony Dualshock 4 controllers for PS4 */
2835 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002836 .driver_data = DUALSHOCK4_CONTROLLER_USB },
Frank Praznik0bd88dd2014-01-11 15:12:42 -05002837 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER),
Frank Praznik8ab16762014-01-16 21:42:31 -05002838 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Roderick Colenbrandercf1015d2016-10-07 12:39:40 -07002839 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
2840 .driver_data = DUALSHOCK4_CONTROLLER_USB },
2841 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_2),
2842 .driver_data = DUALSHOCK4_CONTROLLER_BT },
Roderick Colenbranderde66a1a2016-11-23 14:07:11 -08002843 { HID_USB_DEVICE(USB_VENDOR_ID_SONY, USB_DEVICE_ID_SONY_PS4_CONTROLLER_DONGLE),
Roderick Colenbrander35f436c2017-03-07 15:45:04 -08002844 .driver_data = DUALSHOCK4_DONGLE },
Scott Moreau74500cc2016-01-13 07:40:42 -07002845 /* Nyko Core Controller for PS3 */
2846 { HID_USB_DEVICE(USB_VENDOR_ID_SINO_LITE, USB_DEVICE_ID_SINO_LITE_CONTROLLER),
2847 .driver_data = SIXAXIS_CONTROLLER_USB | SINO_LITE_CONTROLLER },
Jiri Slabybd28ce02008-06-25 23:47:04 +02002848 { }
2849};
2850MODULE_DEVICE_TABLE(hid, sony_devices);
2851
2852static struct hid_driver sony_driver = {
Frank Praznikce8efc32014-09-18 21:15:01 -04002853 .name = "sony",
2854 .id_table = sony_devices,
2855 .input_mapping = sony_mapping,
2856 .input_configured = sony_input_configured,
2857 .probe = sony_probe,
2858 .remove = sony_remove,
2859 .report_fixup = sony_report_fixup,
Frank Praznikdecd9462015-11-11 09:49:38 -05002860 .raw_event = sony_raw_event,
2861
2862#ifdef CONFIG_PM
2863 .suspend = sony_suspend,
2864 .resume = sony_resume,
2865 .reset_resume = sony_resume,
2866#endif
Jiri Slabybd28ce02008-06-25 23:47:04 +02002867};
Frank Praznik80250872014-04-14 10:11:35 -04002868
2869static int __init sony_init(void)
2870{
2871 dbg_hid("Sony:%s\n", __func__);
2872
2873 return hid_register_driver(&sony_driver);
2874}
2875
2876static void __exit sony_exit(void)
2877{
2878 dbg_hid("Sony:%s\n", __func__);
2879
Frank Praznik80250872014-04-14 10:11:35 -04002880 hid_unregister_driver(&sony_driver);
Antonio Ospite6c400652015-02-16 22:58:24 +01002881 ida_destroy(&sony_device_id_allocator);
Frank Praznik80250872014-04-14 10:11:35 -04002882}
2883module_init(sony_init);
2884module_exit(sony_exit);
Jiri Slabybd28ce02008-06-25 23:47:04 +02002885
2886MODULE_LICENSE("GPL");