Improved Xbox One controller initialization sequence
authorSam Lantinga <slouken@libsdl.org>
Wed, 15 Jan 2020 22:18:31 -0800
changeset 1341777bd9fb870f7
parent 13416 8d70e863f6e2
child 13418 2d5b5a5ccbfb
Improved Xbox One controller initialization sequence
This sequence works across Microsoft, PowerA, PDP, and HORI controllers.
The newer Microsoft XBox firmware requires synchronizing the rumble sequence number, when SDL sees it after the initial connect
The Razer Wildcat controller requires waiting for init responses before continuing the initialization sequence.
The PDP Battlefield 1 controller takes over a second to be ready for initialization, and if initialization is attempted before then, it will fail.
src/joystick/hidapi/SDL_hidapi_xboxone.c
     1.1 --- a/src/joystick/hidapi/SDL_hidapi_xboxone.c	Wed Jan 15 22:18:29 2020 -0800
     1.2 +++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c	Wed Jan 15 22:18:31 2020 -0800
     1.3 @@ -37,93 +37,42 @@
     1.4  #define USB_PACKET_LENGTH   64
     1.5  
     1.6  /* The amount of time to wait after hotplug to send controller init sequence */
     1.7 -#define CONTROLLER_INIT_DELAY_MS    100
     1.8 +#define CONTROLLER_INIT_DELAY_MS    1500 /* 475 for Xbox One S, 1275 for the PDP Battlefield 1 */
     1.9  
    1.10 -/* This is the full init sequence for the Xbox One Elite Series 2 controller.
    1.11 -   Normally it isn't needed, but this switches the controller back to wired report mode after being in Bluetooth mode.
    1.12 -*/
    1.13 -static const Uint8 xboxone_ms_init0[] = {
    1.14 -    0x04, 0x20, 0x01, 0x00
    1.15 +/* Connect controller */
    1.16 +static const Uint8 xboxone_init0[] = {
    1.17 +    0x04, 0x20, 0x00, 0x00
    1.18  };
    1.19 -static const Uint8 xboxone_ms_init1[] = {
    1.20 -    0x01, 0x20, 0x28, 0x09, 0x00, 0x04, 0x20, 0x3A,
    1.21 -    0x00, 0x00, 0x00, 0x31, 0x01
    1.22 +/* Finish initialization? */
    1.23 +static const Uint8 xboxone_init1[] = {
    1.24 +    0x01, 0x20, 0x01, 0x09, 0x00, 0x04, 0x20, 0x3a,
    1.25 +    0x00, 0x00, 0x00, 0x80, 0x00
    1.26  };
    1.27 -static const Uint8 xboxone_ms_init2[] = {
    1.28 -    0x01, 0x20, 0x28, 0x09, 0x00, 0x04, 0x20, 0x6B,
    1.29 -    0x01, 0x00, 0x00, 0x00, 0x00
    1.30 -};
    1.31 -static const Uint8 xboxone_ms_init3[] = {
    1.32 -    0x05, 0x20, 0x02, 0x0F, 0x06, 0x00, 0x00, 0x00,
    1.33 +/* Start controller - extended? */
    1.34 +static const Uint8 xboxone_init2[] = {
    1.35 +    0x05, 0x20, 0x00, 0x0F, 0x06, 0x00, 0x00, 0x00,
    1.36      0x00, 0x00, 0x00, 0x55, 0x53, 0x00, 0x00, 0x00,
    1.37      0x00, 0x00, 0x00
    1.38  };
    1.39 -static const Uint8 xboxone_ms_init4[] = {
    1.40 +/* Start controller with input */
    1.41 +static const Uint8 xboxone_init3[] = {
    1.42      0x05, 0x20, 0x03, 0x01, 0x00
    1.43  };
    1.44 -static const Uint8 xboxone_ms_init5[] = {
    1.45 -    0x0A, 0x20, 0x04, 0x03, 0x00, 0x01, 0x14
    1.46 +/* Enable LED */
    1.47 +static const Uint8 xboxone_init4[] = {
    1.48 +    0x0A, 0x20, 0x00, 0x03, 0x00, 0x01, 0x14
    1.49 +};
    1.50 +/* Start input reports? */
    1.51 +static const Uint8 xboxone_init5[] = {
    1.52 +    0x06, 0x20, 0x00, 0x02, 0x01, 0x00
    1.53 +};
    1.54 +/* Start rumble? */
    1.55 +static const Uint8 xboxone_init6[] = {
    1.56 +    0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
    1.57 +    0x00, 0x00, 0xFF, 0x00, 0xEB
    1.58  };
    1.59  
    1.60 -/*
    1.61 - * This packet is required for all Xbox One pads with 2015
    1.62 - * or later firmware installed (or present from the factory).
    1.63 - */
    1.64 -static const Uint8 xboxone_fw2015_init[] = {
    1.65 -    0x05, 0x20, 0x00, 0x01, 0x00
    1.66 -};
    1.67 -
    1.68 -/*
    1.69 - * This packet turns on the LED on some controllers, including PowerA
    1.70 - */
    1.71 -static const Uint8 xboxone_led_enable[] = {
    1.72 -    0x0A, 0x20, 0x04, 0x03, 0x00, 0x01, 0x14
    1.73 -};
    1.74 -
    1.75 -/*
    1.76 - * This packet is required for the Titanfall 2 Xbox One pads
    1.77 - * (0x0e6f:0x0165) to finish initialization and for Hori pads
    1.78 - * (0x0f0d:0x0067) to make the analog sticks work.
    1.79 - */
    1.80 -static const Uint8 xboxone_hori_init[] = {
    1.81 -    0x01, 0x20, 0x00, 0x09, 0x00, 0x04, 0x20, 0x3a,
    1.82 -    0x00, 0x00, 0x00, 0x80, 0x00
    1.83 -};
    1.84 -
    1.85 -/*
    1.86 - * This packet is required for some of the PDP pads to start
    1.87 - * sending input reports. These pads include: (0x0e6f:0x02ab),
    1.88 - * (0x0e6f:0x02a4).
    1.89 - */
    1.90 -static const Uint8 xboxone_pdp_init1[] = {
    1.91 -    0x0a, 0x20, 0x00, 0x03, 0x00, 0x01, 0x14
    1.92 -};
    1.93 -
    1.94 -/*
    1.95 - * This packet is required for some of the PDP pads to start
    1.96 - * sending input reports. These pads include: (0x0e6f:0x02ab),
    1.97 - * (0x0e6f:0x02a4).
    1.98 - */
    1.99 -static const Uint8 xboxone_pdp_init2[] = {
   1.100 -    0x06, 0x20, 0x00, 0x02, 0x01, 0x00
   1.101 -};
   1.102 -
   1.103 -/*
   1.104 - * A specific rumble packet is required for some PowerA pads to start
   1.105 - * sending input reports. One of those pads is (0x24c6:0x543a).
   1.106 - */
   1.107 -static const Uint8 xboxone_rumblebegin_init[] = {
   1.108 -    0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
   1.109 -    0x1D, 0x1D, 0xFF, 0x00, 0x00
   1.110 -};
   1.111 -
   1.112 -/*
   1.113 - * A rumble packet with zero FF intensity will immediately
   1.114 - * terminate the rumbling required to init PowerA pads.
   1.115 - * This should happen fast enough that the motors don't
   1.116 - * spin up to enough speed to actually vibrate the gamepad.
   1.117 - */
   1.118 -static const Uint8 xboxone_rumbleend_init[] = {
   1.119 +static const Uint8 xboxone_rumble_reset[] = {
   1.120      0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
   1.121      0x00, 0x00, 0x00, 0x00, 0x00
   1.122  };
   1.123 @@ -139,24 +88,18 @@
   1.124      Uint16 product_id;
   1.125      const Uint8 *data;
   1.126      int size;
   1.127 +    const Uint8 response[2];
   1.128  } SDL_DriverXboxOne_InitPacket;
   1.129  
   1.130  
   1.131  static const SDL_DriverXboxOne_InitPacket xboxone_init_packets[] = {
   1.132 -    { 0x0e6f, 0x0165, xboxone_hori_init, sizeof(xboxone_hori_init) },
   1.133 -    { 0x0f0d, 0x0067, xboxone_hori_init, sizeof(xboxone_hori_init) },
   1.134 -    { 0x045e, 0x0000, xboxone_ms_init0, sizeof(xboxone_ms_init0) },
   1.135 -    { 0x045e, 0x0000, xboxone_ms_init1, sizeof(xboxone_ms_init1) },
   1.136 -    { 0x045e, 0x0000, xboxone_ms_init2, sizeof(xboxone_ms_init2) },
   1.137 -    { 0x045e, 0x0000, xboxone_ms_init3, sizeof(xboxone_ms_init3) },
   1.138 -    { 0x045e, 0x0000, xboxone_ms_init4, sizeof(xboxone_ms_init4) },
   1.139 -    { 0x045e, 0x0000, xboxone_ms_init5, sizeof(xboxone_ms_init5) },
   1.140 -    { 0x0000, 0x0000, xboxone_fw2015_init, sizeof(xboxone_fw2015_init) },
   1.141 -    { 0x0000, 0x0000, xboxone_led_enable, sizeof(xboxone_led_enable) },
   1.142 -    { 0x0e6f, 0x0000, xboxone_pdp_init1, sizeof(xboxone_pdp_init1) },
   1.143 -    { 0x0e6f, 0x0000, xboxone_pdp_init2, sizeof(xboxone_pdp_init2) },
   1.144 -    { 0x24c6, 0x0000, xboxone_rumblebegin_init, sizeof(xboxone_rumblebegin_init) },
   1.145 -    { 0x24c6, 0x0000, xboxone_rumbleend_init, sizeof(xboxone_rumbleend_init) },
   1.146 +    { 0x0000, 0x0000, xboxone_init0, sizeof(xboxone_init0), { 0x04, 0xf0 } },
   1.147 +    { 0x0000, 0x0000, xboxone_init1, sizeof(xboxone_init1), { 0x04, 0xb0 } },
   1.148 +    { 0x0000, 0x0000, xboxone_init2, sizeof(xboxone_init2), { 0x00, 0x00 } },
   1.149 +    { 0x0000, 0x0000, xboxone_init3, sizeof(xboxone_init3), { 0x00, 0x00 } },
   1.150 +    { 0x0000, 0x0000, xboxone_init4, sizeof(xboxone_init4), { 0x00, 0x00 } },
   1.151 +    { 0x0000, 0x0000, xboxone_init5, sizeof(xboxone_init5), { 0x00, 0x00 } },
   1.152 +    { 0x0000, 0x0000, xboxone_init6, sizeof(xboxone_init6), { 0x00, 0x00 } },
   1.153  };
   1.154  
   1.155  typedef struct {
   1.156 @@ -166,6 +109,7 @@
   1.157      SDL_bool initialized;
   1.158      Uint8 sequence;
   1.159      Uint8 last_state[USB_PACKET_LENGTH];
   1.160 +    SDL_bool rumble_synchronized;
   1.161      Uint32 rumble_expiration;
   1.162  } SDL_DriverXboxOne_Context;
   1.163  
   1.164 @@ -189,6 +133,51 @@
   1.165      return SDL_FALSE;
   1.166  }
   1.167  
   1.168 +static SDL_bool
   1.169 +ControllerNeedsRumbleSequenceSynchronized(Uint16 vendor_id, Uint16 product_id)
   1.170 +{
   1.171 +    const Uint16 USB_VENDOR_MICROSOFT = 0x045e;
   1.172 +    const Uint16 USB_PRODUCT_XBOX_ONE_MODEL_1708 = 0x02ea;      /* Needed with the latest firmware */
   1.173 +    const Uint16 USB_PRODUCT_XBOX_ONE_ELITE_SERIES2 = 0x0b00;
   1.174 +
   1.175 +    if (vendor_id == USB_VENDOR_MICROSOFT) {
   1.176 +        if (product_id == USB_PRODUCT_XBOX_ONE_MODEL_1708 ||
   1.177 +            product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES2) {
   1.178 +            return SDL_TRUE;
   1.179 +        }
   1.180 +    }
   1.181 +    return SDL_FALSE;
   1.182 +}
   1.183 +
   1.184 +static SDL_bool
   1.185 +SynchronizeRumbleSequence(hid_device *dev, SDL_DriverXboxOne_Context *ctx)
   1.186 +{
   1.187 +    Uint16 vendor_id = ctx->vendor_id;
   1.188 +    Uint16 product_id = ctx->product_id;
   1.189 +
   1.190 +    if (ctx->rumble_synchronized) {
   1.191 +        return SDL_TRUE;
   1.192 +    }
   1.193 +
   1.194 +    if (ControllerNeedsRumbleSequenceSynchronized(vendor_id, product_id)) {
   1.195 +        int i;
   1.196 +        Uint8 init_packet[USB_PACKET_LENGTH];
   1.197 +
   1.198 +        SDL_memcpy(init_packet, xboxone_rumble_reset, sizeof(xboxone_rumble_reset));
   1.199 +        for (i = 0; i < 255; ++i) {
   1.200 +            init_packet[2] = ((ctx->sequence + i) % 255);
   1.201 +            if (hid_write(dev, init_packet, sizeof(xboxone_rumble_reset)) != sizeof(xboxone_rumble_reset)) {
   1.202 +                SDL_SetError("Couldn't write Xbox One initialization packet");
   1.203 +                return SDL_FALSE;
   1.204 +            }
   1.205 +        }
   1.206 +    }
   1.207 +    ctx->rumble_synchronized = SDL_TRUE;
   1.208 +
   1.209 +    return SDL_TRUE;
   1.210 +}
   1.211 +
   1.212 +
   1.213  /* Return true if this controller sends the 0x02 "waiting for init" packet */
   1.214  static SDL_bool
   1.215  ControllerSendsWaitingForInit(Uint16 vendor_id, Uint16 product_id)
   1.216 @@ -202,7 +191,8 @@
   1.217          */
   1.218          return SDL_TRUE;
   1.219      } else {
   1.220 -        /* Other controllers may or may not send 0x02, but it doesn't hurt */
   1.221 +        /* Other controllers may or may not send 0x02, but it doesn't hurt to reinit */
   1.222 +        /* The PDP and PowerA controllers don't always send 0x02 when plugged in on Linux */
   1.223          return SDL_FALSE;
   1.224      }
   1.225  }
   1.226 @@ -214,7 +204,7 @@
   1.227      Uint16 product_id = ctx->product_id;
   1.228  
   1.229      if (!IsBluetoothXboxOneController(vendor_id, product_id)) {
   1.230 -        int i, j;
   1.231 +        int i;
   1.232          Uint8 init_packet[USB_PACKET_LENGTH];
   1.233  
   1.234          for (i = 0; i < SDL_arraysize(xboxone_init_packets); ++i) {
   1.235 @@ -229,27 +219,51 @@
   1.236              }
   1.237  
   1.238              SDL_memcpy(init_packet, packet->data, packet->size);
   1.239 -            init_packet[2] = ctx->sequence++;
   1.240 +            if (init_packet[0] != 0x01) {
   1.241 +                init_packet[2] = ctx->sequence++;
   1.242 +            }
   1.243              if (hid_write(dev, init_packet, packet->size) != packet->size) {
   1.244                  SDL_SetError("Couldn't write Xbox One initialization packet");
   1.245                  return SDL_FALSE;
   1.246              }
   1.247  
   1.248 -            /* After the init we need to sync up the rumble sequence */
   1.249 -            if (packet->data == xboxone_fw2015_init) {
   1.250 -                for (j = 0; j < 255; ++j) {
   1.251 -                    if (hid_write(dev, xboxone_rumbleend_init, sizeof(xboxone_rumbleend_init)) != sizeof(xboxone_rumbleend_init)) {
   1.252 -                        SDL_SetError("Couldn't write Xbox One initialization packet");
   1.253 -                        return SDL_FALSE;
   1.254 +            if (packet->response[0]) {
   1.255 +                const Uint32 RESPONSE_TIMEOUT_MS = 50;
   1.256 +                Uint32 start = SDL_GetTicks();
   1.257 +                SDL_bool got_response = SDL_FALSE;
   1.258 +
   1.259 +                while (!got_response && !SDL_TICKS_PASSED(SDL_GetTicks(), start + RESPONSE_TIMEOUT_MS)) {
   1.260 +                    Uint8 data[USB_PACKET_LENGTH];
   1.261 +                    int size;
   1.262 +
   1.263 +                    while ((size = hid_read_timeout(dev, data, sizeof(data), 0)) > 0) {
   1.264 +#ifdef DEBUG_XBOX_PROTOCOL
   1.265 +                        SDL_Log("Xbox One INIT packet: size = %d\n"
   1.266 +                                "                 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n"
   1.267 +                                "                 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n"
   1.268 +                                "                 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
   1.269 +                                    size,
   1.270 +                                    data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
   1.271 +                                    data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15],
   1.272 +                                    data[16], data[17], data[18], data[19]);
   1.273 +#endif
   1.274 +                        if (size >= 2 && data[0] == packet->response[0] && data[1] == packet->response[1]) {
   1.275 +                            got_response = SDL_TRUE;
   1.276 +                        }
   1.277                      }
   1.278                  }
   1.279 +#ifdef DEBUG_XBOX_PROTOCOL
   1.280 +                SDL_Log("Init sequence %d got response: %s\n", i, got_response ? "TRUE" : "FALSE");
   1.281 +#endif
   1.282              }
   1.283          }
   1.284      }
   1.285 +
   1.286 +    SynchronizeRumbleSequence(dev, ctx);
   1.287 +
   1.288      return SDL_TRUE;
   1.289  }
   1.290  
   1.291 -
   1.292  static SDL_bool
   1.293  HIDAPI_DriverXboxOne_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, const char *name)
   1.294  {
   1.295 @@ -311,6 +325,7 @@
   1.296      ctx->vendor_id = device->vendor_id;
   1.297      ctx->product_id = device->product_id;
   1.298      ctx->start_time = SDL_GetTicks();
   1.299 +    ctx->sequence = 1;
   1.300  
   1.301      /* Initialize the joystick capabilities */
   1.302      joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX;
   1.303 @@ -326,9 +341,7 @@
   1.304      SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
   1.305      Uint8 rumble_packet[] = { 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF };
   1.306  
   1.307 -    if (!ctx->initialized) {
   1.308 -        return 0;
   1.309 -    }
   1.310 +    SynchronizeRumbleSequence(device->dev, ctx);
   1.311  
   1.312      /* Magnitude is 1..100 so scale the 16-bit input here */
   1.313      rumble_packet[2] = ctx->sequence++;
   1.314 @@ -425,7 +438,8 @@
   1.315          return SDL_FALSE;
   1.316      }
   1.317  
   1.318 -    if (!ctx->initialized && !ControllerSendsWaitingForInit(device->vendor_id, device->product_id)) {
   1.319 +    if (!ctx->initialized &&
   1.320 +        !ControllerSendsWaitingForInit(device->vendor_id, device->product_id)) {
   1.321          if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->start_time + CONTROLLER_INIT_DELAY_MS)) {
   1.322              if (!SendControllerInit(device->dev, ctx)) {
   1.323                  HIDAPI_JoystickDisconnected(device, joystick->instance_id);
   1.324 @@ -449,11 +463,16 @@
   1.325          switch (data[0]) {
   1.326          case 0x02:
   1.327              /* Controller is connected and waiting for initialization */
   1.328 -            if (!SendControllerInit(device->dev, ctx)) {
   1.329 -                HIDAPI_JoystickDisconnected(device, joystick->instance_id);
   1.330 -                return SDL_FALSE;
   1.331 +            if (!ctx->initialized) {
   1.332 +#ifdef DEBUG_XBOX_PROTOCOL
   1.333 +                SDL_Log("Delay after init: %ums\n", SDL_GetTicks() - ctx->start_time);
   1.334 +#endif
   1.335 +                if (!SendControllerInit(device->dev, ctx)) {
   1.336 +                    HIDAPI_JoystickDisconnected(device, joystick->instance_id);
   1.337 +                    return SDL_FALSE;
   1.338 +                }
   1.339 +                ctx->initialized = SDL_TRUE;
   1.340              }
   1.341 -            ctx->initialized = SDL_TRUE;
   1.342              break;
   1.343          case 0x03:
   1.344              /* Controller heartbeat */
   1.345 @@ -489,6 +508,12 @@
   1.346  static void
   1.347  HIDAPI_DriverXboxOne_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
   1.348  {
   1.349 +    SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
   1.350 +
   1.351 +    if (ctx->rumble_expiration) {
   1.352 +        HIDAPI_DriverXboxOne_RumbleJoystick(device, joystick, 0, 0, 0);
   1.353 +    }
   1.354 +
   1.355      hid_close(device->dev);
   1.356      device->dev = NULL;
   1.357