Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright 2014, Google Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are |
| 9 | * met: |
| 10 | * |
| 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following disclaimer |
| 15 | * in the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * * Neither the name of Google Inc. nor the names of its |
| 18 | * contributors may be used to endorse or promote products derived from |
| 19 | * this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | * Alternatively, this software may be distributed under the terms of the |
| 34 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 35 | * Software Foundation. |
| 36 | */ |
| 37 | |
| 38 | /* |
| 39 | * This SPI flash programming interface is designed to talk to a Chromium OS |
| 40 | * device over a Raiden USB connection. The USB connection is routed to a |
| 41 | * microcontroller running an image compiled from: |
| 42 | * |
| 43 | * https://chromium.googlesource.com/chromiumos/platform/ec |
| 44 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 45 | * The protocol for the USB-SPI bridge is implemented in the following files |
| 46 | * in that repository: |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 47 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 48 | * chip/stm32/usb_spi.h |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 49 | * chip/stm32/usb_spi.c |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 50 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 51 | * bInterfaceProtocol determines which protocol is used by the USB SPI device. |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 52 | * |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 53 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 54 | * USB SPI Version 1: |
| 55 | * |
| 56 | * SPI transactions of up to 62B in each direction with every command having |
| 57 | * a response. The initial packet from host contains a 2B header indicating |
| 58 | * write and read counts with an optional payload length equal to the write |
| 59 | * count. The device will respond with a message that reports the 2B status |
| 60 | * code and an optional payload response length equal to read count. |
| 61 | * |
| 62 | * |
| 63 | * Message Packets: |
| 64 | * |
| 65 | * Command First Packet (Host to Device): |
| 66 | * |
| 67 | * USB SPI command, containing the number of bytes to write and read |
| 68 | * and a payload of bytes to write. |
| 69 | * |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 70 | * +------------------+-----------------+------------------------+ |
| 71 | * | write count : 1B | read count : 1B | write payload : <= 62B | |
| 72 | * +------------------+-----------------+------------------------+ |
| 73 | * |
| 74 | * write count: 1 byte, zero based count of bytes to write |
| 75 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 76 | * read count: 1 byte, zero based count of bytes to read. Full duplex |
| 77 | * mode is enabled with UINT8_MAX |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 78 | * |
| 79 | * write payload: Up to 62 bytes of data to write to SPI, the total |
| 80 | * length of all TX packets must match write count. |
| 81 | * Due to data alignment constraints, this must be an |
| 82 | * even number of bytes unless this is the final packet. |
| 83 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 84 | * Response Packet (Device to Host): |
| 85 | * |
| 86 | * USB SPI response, containing the status code and any bytes of the |
| 87 | * read payload. |
| 88 | * |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 89 | * +-------------+-----------------------+ |
| 90 | * | status : 2B | read payload : <= 62B | |
| 91 | * +-------------+-----------------------+ |
| 92 | * |
| 93 | * status: 2 byte status |
| 94 | * 0x0000: Success |
| 95 | * 0x0001: SPI timeout |
| 96 | * 0x0002: Busy, try again |
| 97 | * This can happen if someone else has acquired the shared memory |
| 98 | * buffer that the SPI driver uses as /dev/null |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 99 | * 0x0003: Write count invalid (over 62 bytes) |
| 100 | * 0x0004: Read count invalid (over 62 bytes) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 101 | * 0x0005: The SPI bridge is disabled. |
| 102 | * 0x8000: Unknown error mask |
| 103 | * The bottom 15 bits will contain the bottom 15 bits from the EC |
| 104 | * error code. |
| 105 | * |
| 106 | * read payload: Up to 62 bytes of data read from SPI, the total |
| 107 | * length of all RX packets must match read count |
| 108 | * unless an error status was returned. Due to data |
| 109 | * alignment constraints, this must be a even number |
| 110 | * of bytes unless this is the final packet. |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 111 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 112 | * |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 113 | * USB Error Codes: |
| 114 | * |
| 115 | * send_command return codes have the following format: |
| 116 | * |
| 117 | * 0x00000: Status code success. |
| 118 | * 0x00001-0x0FFFF: Error code returned by the USB SPI device. |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 119 | * 0x10001-0x1FFFF: Error code returned by the USB SPI host. |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 120 | * 0x20001-0x20063 Lower bits store the positive value representation |
| 121 | * of the libusb_error enum. See the libusb documentation: |
| 122 | * http://libusb.sourceforge.net/api-1.0/group__misc.html |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 123 | */ |
| 124 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 125 | #include "programmer.h" |
| 126 | #include "spi.h" |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 127 | #include "usb_device.h" |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 128 | |
| 129 | #include <libusb.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 130 | #include <stdio.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 131 | #include <stdlib.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 132 | #include <string.h> |
Keith Short | 8453b55 | 2020-02-03 18:10:14 -0700 | [diff] [blame] | 133 | #include <unistd.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 134 | |
Brian J. Nemec | b42d6c1 | 2020-07-23 03:07:38 -0700 | [diff] [blame] | 135 | /* FIXME: Add some programmer IDs here */ |
| 136 | const struct dev_entry devs_raiden[] = { |
| 137 | {0}, |
| 138 | }; |
| 139 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 140 | #define GOOGLE_VID (0x18D1) |
| 141 | #define GOOGLE_RAIDEN_SPI_SUBCLASS (0x51) |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 142 | |
| 143 | enum { |
| 144 | GOOGLE_RAIDEN_SPI_PROTOCOL_V1 = 0x01, |
| 145 | GOOGLE_RAIDEN_SPI_PROTOCOL_V2 = 0x02, |
| 146 | }; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 147 | |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 148 | enum usb_spi_error { |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 149 | USB_SPI_SUCCESS = 0x0000, |
| 150 | USB_SPI_TIMEOUT = 0x0001, |
| 151 | USB_SPI_BUSY = 0x0002, |
| 152 | USB_SPI_WRITE_COUNT_INVALID = 0x0003, |
| 153 | USB_SPI_READ_COUNT_INVALID = 0x0004, |
| 154 | USB_SPI_DISABLED = 0x0005, |
| 155 | USB_SPI_UNKNOWN_ERROR = 0x8000, |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 156 | }; |
| 157 | |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 158 | enum raiden_debug_spi_request { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 159 | RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000, |
| 160 | RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001, |
| 161 | RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002, |
| 162 | RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003, |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 165 | #define PACKET_HEADER_SIZE (2) |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 166 | #define USB_MAX_PACKET_SIZE (64) |
| 167 | #define PAYLOAD_SIZE_V1 (USB_MAX_PACKET_SIZE - PACKET_HEADER_SIZE) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 168 | |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 169 | /* |
| 170 | * Servo Micro has an error where it is capable of acknowledging USB packets |
| 171 | * without loading it into the USB endpoint buffers or triggering interrupts. |
| 172 | * See crbug.com/952494. Retry mechanisms have been implemented to recover |
| 173 | * from these rare failures allowing the process to continue. |
| 174 | */ |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 175 | #define WRITE_RETRY_ATTEMPTS (3) |
| 176 | #define READ_RETRY_ATTEMPTS (3) |
| 177 | #define RETRY_INTERVAL_US (100 * 1000) |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 178 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 179 | /* |
| 180 | * This timeout is so large because the Raiden SPI timeout is 800ms. |
| 181 | */ |
Edward O'Callaghan | f1e6ef5 | 2020-03-03 13:57:15 +1100 | [diff] [blame] | 182 | #define TRANSFER_TIMEOUT_MS (200 + 800) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 183 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 184 | struct raiden_debug_spi_data { |
| 185 | struct usb_device *dev; |
| 186 | uint8_t in_ep; |
| 187 | uint8_t out_ep; |
| 188 | }; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 189 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 190 | /* |
| 191 | * Version 1 protocol specific attributes |
| 192 | */ |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 193 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 194 | struct usb_spi_command_v1 { |
| 195 | uint8_t write_count; |
| 196 | /* UINT8_MAX indicates full duplex mode on compliant devices. */ |
| 197 | uint8_t read_count; |
| 198 | uint8_t data[PAYLOAD_SIZE_V1]; |
| 199 | } __attribute__((packed)); |
| 200 | |
| 201 | struct usb_spi_response_v1 { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 202 | uint16_t status_code; |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 203 | uint8_t data[PAYLOAD_SIZE_V1]; |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 204 | } __attribute__((packed)); |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 205 | |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 206 | /* |
| 207 | * This function will return true when an error code can potentially recover |
| 208 | * if we attempt to write SPI data to the device or read from it. We know |
| 209 | * that some conditions are not recoverable in the current state so allows us |
| 210 | * to bypass the retry logic and terminate early. |
| 211 | */ |
| 212 | static bool retry_recovery(int error_code) |
| 213 | { |
| 214 | if (error_code < 0x10000) { |
Brian J. Nemec | 1a61f72 | 2020-05-04 20:58:06 -0700 | [diff] [blame] | 215 | /* |
| 216 | * Handle error codes returned from the device. USB_SPI_TIMEOUT, |
| 217 | * USB_SPI_BUSY, and USB_SPI_WRITE_COUNT_INVALID have been observed |
| 218 | * during transfer errors to the device and can be recovered. |
| 219 | */ |
| 220 | if (USB_SPI_READ_COUNT_INVALID <= error_code && |
| 221 | error_code <= USB_SPI_DISABLED) { |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 222 | return false; |
| 223 | } |
| 224 | } else if (usb_device_is_libusb_error(error_code)) { |
| 225 | /* Handle error codes returned from libusb. */ |
| 226 | if (error_code == LIBUSB_ERROR(LIBUSB_ERROR_NO_DEVICE)) { |
| 227 | return false; |
| 228 | } |
| 229 | } |
| 230 | return true; |
| 231 | } |
| 232 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 233 | static const struct raiden_debug_spi_data * |
| 234 | get_raiden_data_from_context(const struct flashctx *flash) |
| 235 | { |
| 236 | return (const struct raiden_debug_spi_data *)flash->mst->spi.data; |
| 237 | } |
| 238 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 239 | /* |
| 240 | * Version 1 Protocol: Responsible for constructing the packet to start |
| 241 | * a USB SPI transfer. Write and read counts and payloads to write from |
| 242 | * the write_buffer are transmitted to the device. |
| 243 | * |
| 244 | * @returns Returns status code with 0 on success. |
| 245 | */ |
| 246 | static int write_command_v1(const struct flashctx *flash, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 247 | unsigned int write_count, |
| 248 | unsigned int read_count, |
| 249 | const unsigned char *write_buffer, |
| 250 | unsigned char *read_buffer) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 251 | { |
| 252 | |
| 253 | int transferred; |
| 254 | int ret; |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 255 | struct usb_spi_command_v1 command_packet; |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 256 | const struct raiden_debug_spi_data * ctx_data = get_raiden_data_from_context(flash); |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 257 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 258 | command_packet.write_count = write_count; |
| 259 | command_packet.read_count = read_count; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 260 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 261 | memcpy(command_packet.data, write_buffer, write_count); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 262 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 263 | ret = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle, |
| 264 | ctx_data->out_ep, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 265 | (void*)&command_packet, |
| 266 | write_count + PACKET_HEADER_SIZE, |
| 267 | &transferred, |
| 268 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 269 | if (ret != 0) { |
| 270 | msg_perr("Raiden: OUT transfer failed\n" |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 271 | " write_count = %d\n" |
| 272 | " read_count = %d\n", |
| 273 | write_count, read_count); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 274 | return ret; |
| 275 | } |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 276 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 277 | if ((unsigned) transferred != write_count + PACKET_HEADER_SIZE) { |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 278 | msg_perr("Raiden: Write failure (wrote %d, expected %d)\n", |
| 279 | transferred, write_count + PACKET_HEADER_SIZE); |
| 280 | return 0x10001; |
| 281 | } |
| 282 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 286 | /* |
| 287 | * Version 1 Protocol: Responsible for reading the response of the USB SPI |
| 288 | * transfer. Status codes from the transfer and any read payload are copied |
| 289 | * to the read_buffer. |
| 290 | * |
| 291 | * @returns Returns status code with 0 on success. |
| 292 | */ |
| 293 | static int read_response_v1(const struct flashctx *flash, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 294 | unsigned int write_count, |
| 295 | unsigned int read_count, |
| 296 | const unsigned char *write_buffer, |
| 297 | unsigned char *read_buffer) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 298 | { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 299 | int transferred; |
| 300 | int ret; |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 301 | struct usb_spi_response_v1 response_packet; |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 302 | const struct raiden_debug_spi_data * ctx_data = get_raiden_data_from_context(flash); |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 303 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 304 | ret = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle, |
| 305 | ctx_data->in_ep, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 306 | (void*)&response_packet, |
| 307 | read_count + PACKET_HEADER_SIZE, |
| 308 | &transferred, |
| 309 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 310 | if (ret != 0) { |
| 311 | msg_perr("Raiden: IN transfer failed\n" |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 312 | " write_count = %d\n" |
| 313 | " read_count = %d\n", |
| 314 | write_count, read_count); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 315 | return ret; |
| 316 | } |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 317 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 318 | if ((unsigned) transferred != read_count + PACKET_HEADER_SIZE) { |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 319 | msg_perr("Raiden: Read failure (read %d, expected %d)\n", |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 320 | transferred, read_count + PACKET_HEADER_SIZE); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 321 | return 0x10002; |
| 322 | } |
| 323 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 324 | memcpy(read_buffer, response_packet.data, read_count); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 325 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 326 | return response_packet.status_code; |
| 327 | } |
| 328 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 329 | /* |
| 330 | * Version 1 Protocol: Sets up a USB SPI transfer, transmits data to the device, |
| 331 | * reads the status code and any payload from the device. This will also handle |
| 332 | * recovery if an error has occurred. |
| 333 | * |
| 334 | * @param flash Flash context storing SPI capabilities and USB device |
| 335 | * information. |
| 336 | * @param write_count Number of bytes to write |
| 337 | * @param read_count Number of bytes to read |
| 338 | * @param write_buffer Address of write buffer |
| 339 | * @param read_buffer Address of buffer to store read data |
| 340 | * |
| 341 | * @returns Returns status code with 0 on success. |
| 342 | */ |
| 343 | static int send_command_v1(const struct flashctx *flash, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 344 | unsigned int write_count, |
| 345 | unsigned int read_count, |
| 346 | const unsigned char *write_buffer, |
| 347 | unsigned char *read_buffer) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 348 | { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 349 | int status = -1; |
| 350 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 351 | if (write_count > PAYLOAD_SIZE_V1) { |
| 352 | msg_perr("Raiden: Invalid write count\n" |
| 353 | " write count = %u\n" |
| 354 | " max write = %d\n", |
| 355 | write_count, PAYLOAD_SIZE_V1); |
| 356 | return SPI_INVALID_LENGTH; |
| 357 | } |
| 358 | |
| 359 | if (read_count > PAYLOAD_SIZE_V1) { |
| 360 | msg_perr("Raiden: Invalid read count\n" |
| 361 | " read count = %d\n" |
| 362 | " max read = %d\n", |
| 363 | read_count, PAYLOAD_SIZE_V1); |
| 364 | return SPI_INVALID_LENGTH; |
| 365 | } |
| 366 | |
| 367 | for (unsigned int write_attempt = 0; write_attempt < WRITE_RETRY_ATTEMPTS; |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 368 | write_attempt++) { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 369 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 370 | status = write_command_v1(flash, write_count, read_count, |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 371 | write_buffer, read_buffer); |
| 372 | |
| 373 | if (status) { |
| 374 | /* Write operation failed. */ |
| 375 | msg_perr("Raiden: Write command failed\n" |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 376 | " write count = %u\n" |
| 377 | " read count = %u\n" |
| 378 | " write attempt = %u\n" |
| 379 | " status = 0x%05x\n", |
| 380 | write_count, read_count, |
| 381 | write_attempt + 1, status); |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 382 | if (!retry_recovery(status)) { |
| 383 | /* Reattempting will not result in a recovery. */ |
| 384 | return status; |
| 385 | } |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 386 | programmer_delay(RETRY_INTERVAL_US); |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 387 | continue; |
| 388 | } |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 389 | for (unsigned int read_attempt = 0; read_attempt < READ_RETRY_ATTEMPTS; |
| 390 | read_attempt++) { |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 391 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 392 | status = read_response_v1(flash, write_count, read_count, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 393 | write_buffer, read_buffer); |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 394 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 395 | if (status) { |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 396 | /* Read operation failed. */ |
| 397 | msg_perr("Raiden: Read response failed\n" |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 398 | " write count = %u\n" |
| 399 | " read count = %u\n" |
| 400 | " write attempt = %u\n" |
| 401 | " read attempt = %u\n" |
| 402 | " status = 0x%05x\n", |
| 403 | write_count, read_count, |
| 404 | write_attempt + 1, read_attempt + 1, status); |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 405 | if (!retry_recovery(status)) { |
| 406 | /* Reattempting will not result in a recovery. */ |
| 407 | return status; |
| 408 | } |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 409 | programmer_delay(RETRY_INTERVAL_US); |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 410 | } else { |
| 411 | /* We were successful at performing the SPI transfer. */ |
| 412 | return status; |
| 413 | } |
| 414 | } |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 415 | } |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 416 | return status; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Unfortunately there doesn't seem to be a way to specify the maximum number |
| 421 | * of bytes that your SPI device can read/write, these values are the maximum |
Duncan Laurie | 537fd1d | 2018-10-05 10:53:20 -0700 | [diff] [blame] | 422 | * data chunk size that flashrom will package up with an additional five bytes |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 423 | * of command for the flash device, resulting in a 62 byte packet, that we then |
| 424 | * add two bytes to in either direction, making our way up to the 64 byte |
| 425 | * maximum USB packet size for the device. |
| 426 | * |
| 427 | * The largest command that flashrom generates is the byte program command, so |
Duncan Laurie | 537fd1d | 2018-10-05 10:53:20 -0700 | [diff] [blame] | 428 | * we use that command header maximum size here. |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 429 | */ |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 430 | #define MAX_DATA_SIZE (PAYLOAD_SIZE_V1 - JEDEC_BYTE_PROGRAM_OUTSIZE) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 431 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 432 | static struct spi_master spi_master_raiden_debug = { |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 433 | .features = SPI_MASTER_4BA, |
| 434 | .max_data_read = MAX_DATA_SIZE, |
| 435 | .max_data_write = MAX_DATA_SIZE, |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame^] | 436 | .command = send_command_v1, |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 437 | .multicommand = default_spi_send_multicommand, |
| 438 | .read = default_spi_read, |
| 439 | .write_256 = default_spi_write_256, |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 440 | }; |
| 441 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 442 | static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor, |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 443 | enum libusb_endpoint_direction direction) |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 444 | { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 445 | return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == |
| 446 | direction) && |
| 447 | ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == |
| 448 | LIBUSB_TRANSFER_TYPE_BULK)); |
| 449 | } |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 450 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 451 | static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep) |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 452 | { |
| 453 | int i; |
| 454 | int in_count = 0; |
| 455 | int out_count = 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 456 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 457 | for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 458 | struct libusb_endpoint_descriptor const *endpoint = |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 459 | &dev->interface_descriptor->endpoint[i]; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 460 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 461 | if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) { |
| 462 | in_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 463 | *in_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 464 | } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) { |
| 465 | out_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 466 | *out_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
| 470 | if (in_count != 1 || out_count != 1) { |
| 471 | msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n" |
| 472 | " found %d IN and %d OUT endpoints\n", |
| 473 | in_count, |
| 474 | out_count); |
| 475 | return 1; |
| 476 | } |
| 477 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 478 | msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep); |
| 479 | msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep); |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 480 | |
| 481 | return 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 484 | static int raiden_debug_spi_shutdown(void * data) |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 485 | { |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 486 | struct raiden_debug_spi_data * ctx_data = |
| 487 | (struct raiden_debug_spi_data *)data; |
| 488 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 489 | int ret = LIBUSB(libusb_control_transfer( |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 490 | ctx_data->dev->handle, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 491 | LIBUSB_ENDPOINT_OUT | |
| 492 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 493 | LIBUSB_RECIPIENT_INTERFACE, |
| 494 | RAIDEN_DEBUG_SPI_REQ_DISABLE, |
| 495 | 0, |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 496 | ctx_data->dev->interface_descriptor->bInterfaceNumber, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 497 | NULL, |
| 498 | 0, |
| 499 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 500 | if (ret != 0) { |
| 501 | msg_perr("Raiden: Failed to disable SPI bridge\n"); |
| 502 | return ret; |
| 503 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 504 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 505 | usb_device_free(ctx_data->dev); |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 506 | libusb_exit(NULL); |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 507 | free(ctx_data); |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
Edward O'Callaghan | dda1e54 | 2020-02-03 12:45:01 +1100 | [diff] [blame] | 512 | static int get_target(void) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 513 | { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 514 | int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 515 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 516 | char *target_str = extract_programmer_param("target"); |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 517 | if (target_str) { |
| 518 | if (!strcasecmp(target_str, "ap")) |
| 519 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP; |
| 520 | else if (!strcasecmp(target_str, "ec")) |
| 521 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC; |
| 522 | else { |
| 523 | msg_perr("Invalid target: %s\n", target_str); |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 524 | request_enable = -1; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 527 | free(target_str); |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 528 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 529 | return request_enable; |
| 530 | } |
| 531 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 532 | static void free_dev_list(struct usb_device **dev_lst) |
| 533 | { |
| 534 | struct usb_device *dev = *dev_lst; |
| 535 | /* free devices we don't care about */ |
| 536 | dev = dev->next; |
| 537 | while (dev) |
| 538 | dev = usb_device_free(dev); |
| 539 | } |
| 540 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 541 | int raiden_debug_spi_init(void) |
| 542 | { |
| 543 | struct usb_match match; |
| 544 | char *serial = extract_programmer_param("serial"); |
| 545 | struct usb_device *current; |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 546 | struct usb_device *device = NULL; |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 547 | int found = 0; |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 548 | int ret; |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 549 | |
| 550 | int request_enable = get_target(); |
Brian J. Nemec | 7b5ad79 | 2020-07-23 03:35:54 -0700 | [diff] [blame] | 551 | if (request_enable < 0) { |
| 552 | free(serial); |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 553 | return 1; |
Brian J. Nemec | 7b5ad79 | 2020-07-23 03:35:54 -0700 | [diff] [blame] | 554 | } |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 555 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 556 | usb_match_init(&match); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 557 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 558 | usb_match_value_default(&match.vid, GOOGLE_VID); |
| 559 | usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC); |
| 560 | usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS); |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 561 | usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL_V1); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 562 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 563 | ret = LIBUSB(libusb_init(NULL)); |
| 564 | if (ret != 0) { |
| 565 | msg_perr("Raiden: libusb_init failed\n"); |
Brian J. Nemec | a88d96e | 2020-07-23 03:22:57 -0700 | [diff] [blame] | 566 | free(serial); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 567 | return ret; |
| 568 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 569 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 570 | ret = usb_device_find(&match, ¤t); |
| 571 | if (ret != 0) { |
| 572 | msg_perr("Raiden: Failed to find devices\n"); |
Brian J. Nemec | a88d96e | 2020-07-23 03:22:57 -0700 | [diff] [blame] | 573 | free(serial); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 574 | return ret; |
| 575 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 576 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 577 | uint8_t in_endpoint = 0; |
| 578 | uint8_t out_endpoint = 0; |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 579 | while (current) { |
| 580 | device = current; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 581 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 582 | if (find_endpoints(device, &in_endpoint, &out_endpoint)) { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 583 | msg_pdbg("Raiden: Failed to find valid endpoints on device"); |
| 584 | usb_device_show(" ", current); |
| 585 | goto loop_end; |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 586 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 587 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 588 | if (usb_device_claim(device)) { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 589 | msg_pdbg("Raiden: Failed to claim USB device"); |
| 590 | usb_device_show(" ", current); |
| 591 | goto loop_end; |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 592 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 593 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 594 | if (!serial) { |
| 595 | found = 1; |
| 596 | goto loop_end; |
| 597 | } else { |
| 598 | unsigned char dev_serial[32]; |
| 599 | struct libusb_device_descriptor descriptor; |
| 600 | int rc; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 601 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 602 | memset(dev_serial, 0, sizeof(dev_serial)); |
| 603 | |
| 604 | if (libusb_get_device_descriptor(device->device, &descriptor)) { |
| 605 | msg_pdbg("USB: Failed to get device descriptor.\n"); |
| 606 | goto loop_end; |
| 607 | } |
| 608 | |
| 609 | rc = libusb_get_string_descriptor_ascii(device->handle, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 610 | descriptor.iSerialNumber, |
| 611 | dev_serial, |
| 612 | sizeof(dev_serial)); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 613 | if (rc < 0) { |
| 614 | LIBUSB(rc); |
| 615 | } else { |
| 616 | if (strcmp(serial, (char *)dev_serial)) { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 617 | msg_pdbg("Raiden: Serial number %s did not match device", serial); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 618 | usb_device_show(" ", current); |
| 619 | } else { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 620 | msg_pinfo("Raiden: Serial number %s matched device", serial); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 621 | usb_device_show(" ", current); |
| 622 | found = 1; |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | loop_end: |
| 628 | if (found) |
| 629 | break; |
| 630 | else |
| 631 | current = usb_device_free(current); |
| 632 | } |
| 633 | |
| 634 | if (!device || !found) { |
| 635 | msg_perr("Raiden: No usable device found.\n"); |
Brian J. Nemec | a88d96e | 2020-07-23 03:22:57 -0700 | [diff] [blame] | 636 | free(serial); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 637 | return 1; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 640 | free_dev_list(¤t); |
Anton Staaf | b4661ee | 2014-10-21 11:24:36 -0700 | [diff] [blame] | 641 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 642 | ret = LIBUSB(libusb_control_transfer( |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 643 | device->handle, |
| 644 | LIBUSB_ENDPOINT_OUT | |
| 645 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 646 | LIBUSB_RECIPIENT_INTERFACE, |
| 647 | request_enable, |
| 648 | 0, |
| 649 | device->interface_descriptor->bInterfaceNumber, |
| 650 | NULL, |
| 651 | 0, |
| 652 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 653 | if (ret != 0) { |
| 654 | msg_perr("Raiden: Failed to enable SPI bridge\n"); |
| 655 | return ret; |
| 656 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 657 | |
Keith Short | 8453b55 | 2020-02-03 18:10:14 -0700 | [diff] [blame] | 658 | /* |
| 659 | * Allow for power to settle on the AP and EC flash devices. |
| 660 | * Load switches can have a 1-3 ms turn on time, and SPI flash devices |
| 661 | * can require up to 10 ms from power on to the first write. |
| 662 | */ |
| 663 | if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) || |
| 664 | (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC)) |
| 665 | usleep(50 * 1000); |
| 666 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 667 | struct raiden_debug_spi_data *data = calloc(1, sizeof(struct raiden_debug_spi_data)); |
| 668 | if (!data) { |
| 669 | msg_perr("Unable to allocate space for extra SPI master data.\n"); |
| 670 | return SPI_GENERIC_ERROR; |
| 671 | } |
| 672 | |
| 673 | data->dev = device; |
| 674 | data->in_ep = in_endpoint; |
| 675 | data->out_ep = out_endpoint; |
| 676 | |
| 677 | spi_master_raiden_debug.data = data; |
| 678 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 679 | register_spi_master(&spi_master_raiden_debug); |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 680 | register_shutdown(raiden_debug_spi_shutdown, data); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 681 | |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 682 | return 0; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 683 | } |