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