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 | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 113 | * USB SPI Version 2: |
| 114 | * |
| 115 | * USB SPI version 2 adds support for larger SPI transfers and reduces the |
| 116 | * number of USB packets transferred. This improves performance when |
| 117 | * writing or reading large chunks of memory from a device. A packet ID |
| 118 | * field is used to distinguish the different packet types. Additional |
| 119 | * packets have been included to query the device for its configuration |
| 120 | * allowing the interface to be used on platforms with different SPI |
| 121 | * limitations. It includes validation and a packet to recover from the |
| 122 | * situations where USB packets are lost. |
| 123 | * |
| 124 | * The USB SPI hosts which support packet version 2 are backwards compatible |
| 125 | * and use the bInterfaceProtocol field to identify which type of target |
| 126 | * they are connected to. |
| 127 | * |
| 128 | * |
| 129 | * Example: USB SPI request with 128 byte write and 0 byte read. |
| 130 | * |
| 131 | * Packet #1 Host to Device: |
| 132 | * packet id = USB_SPI_PKT_ID_CMD_TRANSFER_START |
| 133 | * write count = 128 |
| 134 | * read count = 0 |
| 135 | * payload = First 58 bytes from the write buffer, |
| 136 | * starting at byte 0 in the buffer |
| 137 | * packet size = 64 bytes |
| 138 | * |
| 139 | * Packet #2 Host to Device: |
| 140 | * packet id = USB_SPI_PKT_ID_CMD_TRANSFER_CONTINUE |
| 141 | * data index = 58 |
| 142 | * payload = Next 60 bytes from the write buffer, |
| 143 | * starting at byte 58 in the buffer |
| 144 | * packet size = 64 bytes |
| 145 | * |
| 146 | * Packet #3 Host to Device: |
| 147 | * packet id = USB_SPI_PKT_ID_CMD_TRANSFER_CONTINUE |
| 148 | * data index = 118 |
| 149 | * payload = Next 10 bytes from the write buffer, |
| 150 | * starting at byte 118 in the buffer |
| 151 | * packet size = 14 bytes |
| 152 | * |
| 153 | * Packet #4 Device to Host: |
| 154 | * packet id = USB_SPI_PKT_ID_RSP_TRANSFER_START |
| 155 | * status code = status code from device |
| 156 | * payload = 0 bytes |
| 157 | * packet size = 4 bytes |
| 158 | * |
| 159 | * Example: USB SPI request with 2 byte write and 100 byte read. |
| 160 | * |
| 161 | * Packet #1 Host to Device: |
| 162 | * packet id = USB_SPI_PKT_ID_CMD_TRANSFER_START |
| 163 | * write count = 2 |
| 164 | * read count = 100 |
| 165 | * payload = The 2 byte write buffer |
| 166 | * packet size = 8 bytes |
| 167 | * |
| 168 | * Packet #2 Device to Host: |
| 169 | * packet id = USB_SPI_PKT_ID_RSP_TRANSFER_START |
| 170 | * status code = status code from device |
| 171 | * payload = First 60 bytes from the read buffer, |
| 172 | * starting at byte 0 in the buffer |
| 173 | * packet size = 64 bytes |
| 174 | * |
| 175 | * Packet #3 Device to Host: |
| 176 | * packet id = USB_SPI_PKT_ID_RSP_TRANSFER_CONTINUE |
| 177 | * data index = 60 |
| 178 | * payload = Next 40 bytes from the read buffer, |
| 179 | * starting at byte 60 in the buffer |
| 180 | * packet size = 44 bytes |
| 181 | * |
| 182 | * |
| 183 | * Message Packets: |
| 184 | * |
| 185 | * Command Start Packet (Host to Device): |
| 186 | * |
| 187 | * Start of the USB SPI command, contains the number of bytes to write |
| 188 | * and read on SPI and up to the first 58 bytes of write payload. |
| 189 | * Longer writes will use the continue packets with packet id |
| 190 | * USB_SPI_PKT_ID_CMD_TRANSFER_CONTINUE to transmit the remaining data. |
| 191 | * |
| 192 | * +----------------+------------------+-----------------+---------------+ |
| 193 | * | packet id : 2B | write count : 2B | read count : 2B | w.p. : <= 58B | |
| 194 | * +----------------+------------------+-----------------+---------------+ |
| 195 | * |
| 196 | * packet id: 2 byte enum defined by packet_id_type |
| 197 | * Valid values packet id = USB_SPI_PKT_ID_CMD_TRANSFER_START |
| 198 | * |
| 199 | * write count: 2 byte, zero based count of bytes to write |
| 200 | * |
| 201 | * read count: 2 byte, zero based count of bytes to read |
| 202 | * UINT16_MAX indicates full duplex mode with a read count |
| 203 | * equal to the write count. |
| 204 | * |
| 205 | * write payload: Up to 58 bytes of data to write to SPI, the total |
| 206 | * length of all TX packets must match write count. |
| 207 | * Due to data alignment constraints, this must be an |
| 208 | * even number of bytes unless this is the final packet. |
| 209 | * |
| 210 | * |
| 211 | * Response Start Packet (Device to Host): |
| 212 | * |
| 213 | * Start of the USB SPI response, contains the status code and up to |
| 214 | * the first 60 bytes of read payload. Longer reads will use the |
| 215 | * continue packets with packet id USB_SPI_PKT_ID_RSP_TRANSFER_CONTINUE |
| 216 | * to transmit the remaining data. |
| 217 | * |
| 218 | * +----------------+------------------+-----------------------+ |
| 219 | * | packet id : 2B | status code : 2B | read payload : <= 60B | |
| 220 | * +----------------+------------------+-----------------------+ |
| 221 | * |
| 222 | * packet id: 2 byte enum defined by packet_id_type |
| 223 | * Valid values packet id = USB_SPI_PKT_ID_RSP_TRANSFER_START |
| 224 | * |
| 225 | * status code: 2 byte status code |
| 226 | * 0x0000: Success |
| 227 | * 0x0001: SPI timeout |
| 228 | * 0x0002: Busy, try again |
| 229 | * This can happen if someone else has acquired the shared memory |
| 230 | * buffer that the SPI driver uses as /dev/null |
| 231 | * 0x0003: Write count invalid. The byte limit is platform specific |
| 232 | * and is set during the configure USB SPI response. |
| 233 | * 0x0004: Read count invalid. The byte limit is platform specific |
| 234 | * and is set during the configure USB SPI response. |
| 235 | * 0x0005: The SPI bridge is disabled. |
| 236 | * 0x0006: The RX continue packet's data index is invalid. This |
| 237 | * can indicate a USB transfer failure to the device. |
| 238 | * 0x0007: The RX endpoint has received more data than write count. |
| 239 | * This can indicate a USB transfer failure to the device. |
| 240 | * 0x0008: An unexpected packet arrived that the device could not |
| 241 | * process. |
| 242 | * 0x0009: The device does not support full duplex mode. |
| 243 | * 0x8000: Unknown error mask |
| 244 | * The bottom 15 bits will contain the bottom 15 bits from the EC |
| 245 | * error code. |
| 246 | * |
| 247 | * read payload: Up to 60 bytes of data read from SPI, the total |
| 248 | * length of all RX packets must match read count |
| 249 | * unless an error status was returned. Due to data |
| 250 | * alignment constraints, this must be a even number |
| 251 | * of bytes unless this is the final packet. |
| 252 | * |
| 253 | * |
| 254 | * Continue Packet (Bidirectional): |
| 255 | * |
| 256 | * Continuation packet for the writes and read buffers. Both packets |
| 257 | * follow the same format, a data index counts the number of bytes |
| 258 | * previously transferred in the USB SPI transfer and a payload of bytes. |
| 259 | * |
| 260 | * +----------------+-----------------+-------------------------------+ |
| 261 | * | packet id : 2B | data index : 2B | write / read payload : <= 60B | |
| 262 | * +----------------+-----------------+-------------------------------+ |
| 263 | * |
| 264 | * packet id: 2 byte enum defined by packet_id_type |
| 265 | * The packet id has 2 values depending on direction: |
| 266 | * packet id = USB_SPI_PKT_ID_CMD_TRANSFER_CONTINUE |
| 267 | * indicates the packet is being transmitted from the host |
| 268 | * to the device and contains SPI write payload. |
| 269 | * packet id = USB_SPI_PKT_ID_RSP_TRANSFER_CONTINUE |
| 270 | * indicates the packet is being transmitted from the device |
| 271 | * to the host and contains SPI read payload. |
| 272 | * |
| 273 | * data index: The data index indicates the number of bytes in the |
| 274 | * read or write buffers that have already been transmitted. |
| 275 | * It is used to validate that no packets have been dropped |
| 276 | * and that the prior packets have been correctly decoded. |
| 277 | * This value corresponds to the offset bytes in the buffer |
| 278 | * to start copying the payload into. |
| 279 | * |
| 280 | * read and write payload: |
| 281 | * Contains up to 60 bytes of payload data to transfer to |
| 282 | * the SPI write buffer or from the SPI read buffer. |
| 283 | * |
| 284 | * |
| 285 | * Command Get Configuration Packet (Host to Device): |
| 286 | * |
| 287 | * Query the device to request its USB SPI configuration indicating |
| 288 | * the number of bytes it can write and read. |
| 289 | * |
| 290 | * +----------------+ |
| 291 | * | packet id : 2B | |
| 292 | * +----------------+ |
| 293 | * |
| 294 | * packet id: 2 byte enum USB_SPI_PKT_ID_CMD_GET_USB_SPI_CONFIG |
| 295 | * |
| 296 | * Response Configuration Packet (Device to Host): |
| 297 | * |
| 298 | * Response packet form the device to report the maximum write and |
| 299 | * read size supported by the device. |
| 300 | * |
| 301 | * +----------------+----------------+---------------+----------------+ |
| 302 | * | packet id : 2B | max write : 2B | max read : 2B | feature bitmap | |
| 303 | * +----------------+----------------+---------------+----------------+ |
| 304 | * |
| 305 | * packet id: 2 byte enum USB_SPI_PKT_ID_RSP_USB_SPI_CONFIG |
| 306 | * |
| 307 | * max write count: 2 byte count of the maximum number of bytes |
| 308 | * the device can write to SPI in one transaction. |
| 309 | * |
| 310 | * max read count: 2 byte count of the maximum number of bytes |
| 311 | * the device can read from SPI in one transaction. |
| 312 | * |
| 313 | * feature bitmap: Bitmap of supported features. |
| 314 | * BIT(0): Full duplex SPI mode is supported |
| 315 | * BIT(1:15): Reserved for future use |
| 316 | * |
| 317 | * Command Restart Response Packet (Host to Device): |
| 318 | * |
| 319 | * Command to restart the response transfer from the device. This enables |
| 320 | * the host to recover from a lost packet when reading the response |
| 321 | * without restarting the SPI transfer. |
| 322 | * |
| 323 | * +----------------+ |
| 324 | * | packet id : 2B | |
| 325 | * +----------------+ |
| 326 | * |
| 327 | * packet id: 2 byte enum USB_SPI_PKT_ID_CMD_RESTART_RESPONSE |
| 328 | * |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 329 | * USB Error Codes: |
| 330 | * |
| 331 | * send_command return codes have the following format: |
| 332 | * |
| 333 | * 0x00000: Status code success. |
| 334 | * 0x00001-0x0FFFF: Error code returned by the USB SPI device. |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 335 | * 0x10001-0x1FFFF: Error code returned by the USB SPI host. |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 336 | * 0x20001-0x20063 Lower bits store the positive value representation |
| 337 | * of the libusb_error enum. See the libusb documentation: |
| 338 | * http://libusb.sourceforge.net/api-1.0/group__misc.html |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 339 | */ |
| 340 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 341 | #include "programmer.h" |
| 342 | #include "spi.h" |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 343 | #include "usb_device.h" |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 344 | |
| 345 | #include <libusb.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 346 | #include <stdio.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 347 | #include <stdlib.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 348 | #include <string.h> |
Keith Short | 8453b55 | 2020-02-03 18:10:14 -0700 | [diff] [blame] | 349 | #include <unistd.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 350 | |
Brian J. Nemec | b42d6c1 | 2020-07-23 03:07:38 -0700 | [diff] [blame] | 351 | /* FIXME: Add some programmer IDs here */ |
| 352 | const struct dev_entry devs_raiden[] = { |
| 353 | {0}, |
| 354 | }; |
| 355 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 356 | #define GOOGLE_VID (0x18D1) |
| 357 | #define GOOGLE_RAIDEN_SPI_SUBCLASS (0x51) |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 358 | |
| 359 | enum { |
| 360 | GOOGLE_RAIDEN_SPI_PROTOCOL_V1 = 0x01, |
| 361 | GOOGLE_RAIDEN_SPI_PROTOCOL_V2 = 0x02, |
| 362 | }; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 363 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 364 | enum { |
| 365 | /* The host failed to transfer the data with no libusb error. */ |
| 366 | USB_SPI_HOST_TX_BAD_TRANSFER = 0x10001, |
| 367 | /* The number of bytes written did not match expected. */ |
| 368 | USB_SPI_HOST_TX_WRITE_FAILURE = 0x10002, |
| 369 | |
| 370 | /* We did not receive the expected USB packet. */ |
| 371 | USB_SPI_HOST_RX_UNEXPECTED_PACKET = 0x11001, |
| 372 | /* We received a continue packet with an invalid data index. */ |
| 373 | USB_SPI_HOST_RX_BAD_DATA_INDEX = 0x11002, |
| 374 | /* We received too much data. */ |
| 375 | USB_SPI_HOST_RX_DATA_OVERFLOW = 0x11003, |
| 376 | /* The number of bytes read did not match expected. */ |
| 377 | USB_SPI_HOST_RX_READ_FAILURE = 0x11004, |
| 378 | |
| 379 | /* We were unable to configure the device. */ |
| 380 | USB_SPI_HOST_INIT_FAILURE = 0x12001, |
| 381 | }; |
| 382 | |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 383 | enum usb_spi_error { |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 384 | USB_SPI_SUCCESS = 0x0000, |
| 385 | USB_SPI_TIMEOUT = 0x0001, |
| 386 | USB_SPI_BUSY = 0x0002, |
| 387 | USB_SPI_WRITE_COUNT_INVALID = 0x0003, |
| 388 | USB_SPI_READ_COUNT_INVALID = 0x0004, |
| 389 | USB_SPI_DISABLED = 0x0005, |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 390 | /* The RX continue packet's data index is invalid. */ |
| 391 | USB_SPI_RX_BAD_DATA_INDEX = 0x0006, |
| 392 | /* The RX endpoint has received more data than write count. */ |
| 393 | USB_SPI_RX_DATA_OVERFLOW = 0x0007, |
| 394 | /* An unexpected packet arrived on the device. */ |
| 395 | USB_SPI_RX_UNEXPECTED_PACKET = 0x0008, |
| 396 | /* The device does not support full duplex mode. */ |
| 397 | USB_SPI_UNSUPPORTED_FULL_DUPLEX = 0x0009, |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 398 | USB_SPI_UNKNOWN_ERROR = 0x8000, |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 399 | }; |
| 400 | |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 401 | enum raiden_debug_spi_request { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 402 | RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000, |
| 403 | RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001, |
| 404 | RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002, |
| 405 | RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003, |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 406 | }; |
| 407 | |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 408 | /* |
| 409 | * Servo Micro has an error where it is capable of acknowledging USB packets |
| 410 | * without loading it into the USB endpoint buffers or triggering interrupts. |
| 411 | * See crbug.com/952494. Retry mechanisms have been implemented to recover |
| 412 | * from these rare failures allowing the process to continue. |
| 413 | */ |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 414 | #define WRITE_RETRY_ATTEMPTS (3) |
| 415 | #define READ_RETRY_ATTEMPTS (3) |
| 416 | #define GET_CONFIG_RETRY_ATTEMPTS (3) |
| 417 | #define RETRY_INTERVAL_US (100 * 1000) |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 418 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 419 | /* |
| 420 | * This timeout is so large because the Raiden SPI timeout is 800ms. |
| 421 | */ |
Edward O'Callaghan | f1e6ef5 | 2020-03-03 13:57:15 +1100 | [diff] [blame] | 422 | #define TRANSFER_TIMEOUT_MS (200 + 800) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 423 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 424 | struct raiden_debug_spi_data { |
| 425 | struct usb_device *dev; |
| 426 | uint8_t in_ep; |
| 427 | uint8_t out_ep; |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 428 | uint8_t protocol_version; |
| 429 | /* |
| 430 | * Note: Due to bugs, flashrom does not always treat the max_data_write |
| 431 | * and max_data_read counts as the maximum packet size. As a result, we |
| 432 | * have to store a local copy of the actual max packet sizes and validate |
| 433 | * against it when performing transfers. |
| 434 | */ |
| 435 | uint16_t max_spi_write_count; |
| 436 | uint16_t max_spi_read_count; |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 437 | }; |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 438 | /* |
| 439 | * USB permits a maximum bulk transfer of 64B. |
| 440 | */ |
| 441 | #define USB_MAX_PACKET_SIZE (64) |
| 442 | #define PACKET_HEADER_SIZE (2) |
| 443 | |
| 444 | /* |
| 445 | * All of the USB SPI packets have size equal to the max USB packet size of 64B |
| 446 | */ |
| 447 | #define PAYLOAD_SIZE_V1 (62) |
| 448 | |
| 449 | #define SPI_TRANSFER_V1_MAX (PAYLOAD_SIZE_V1) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 450 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 451 | /* |
| 452 | * Version 1 protocol specific attributes |
| 453 | */ |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 454 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 455 | struct usb_spi_command_v1 { |
| 456 | uint8_t write_count; |
| 457 | /* UINT8_MAX indicates full duplex mode on compliant devices. */ |
| 458 | uint8_t read_count; |
| 459 | uint8_t data[PAYLOAD_SIZE_V1]; |
| 460 | } __attribute__((packed)); |
| 461 | |
| 462 | struct usb_spi_response_v1 { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 463 | uint16_t status_code; |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 464 | uint8_t data[PAYLOAD_SIZE_V1]; |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 465 | } __attribute__((packed)); |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 466 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 467 | union usb_spi_packet_v1 { |
| 468 | struct usb_spi_command_v1 command; |
| 469 | struct usb_spi_response_v1 response; |
| 470 | } __attribute__((packed)); |
| 471 | |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 472 | /* |
| 473 | * Version 2 protocol specific attributes |
| 474 | */ |
| 475 | |
| 476 | #define USB_SPI_FULL_DUPLEX_ENABLED_V2 (UINT16_MAX) |
| 477 | |
| 478 | #define USB_SPI_PAYLOAD_SIZE_V2_START (58) |
| 479 | |
| 480 | #define USB_SPI_PAYLOAD_SIZE_V2_RESPONSE (60) |
| 481 | |
| 482 | #define USB_SPI_PAYLOAD_SIZE_V2_CONTINUE (60) |
| 483 | |
| 484 | enum packet_id_type { |
| 485 | /* Request USB SPI configuration data from device. */ |
| 486 | USB_SPI_PKT_ID_CMD_GET_USB_SPI_CONFIG = 0, |
| 487 | /* USB SPI configuration data from device. */ |
| 488 | USB_SPI_PKT_ID_RSP_USB_SPI_CONFIG = 1, |
| 489 | /* |
| 490 | * Start a USB SPI transfer specifying number of bytes to write, |
| 491 | * read and deliver first packet of data to write. |
| 492 | */ |
| 493 | USB_SPI_PKT_ID_CMD_TRANSFER_START = 2, |
| 494 | /* Additional packets containing write payload. */ |
| 495 | USB_SPI_PKT_ID_CMD_TRANSFER_CONTINUE = 3, |
| 496 | /* |
| 497 | * Request the device restart the response enabling us to recover |
| 498 | * from packet loss without another SPI transfer. |
| 499 | */ |
| 500 | USB_SPI_PKT_ID_CMD_RESTART_RESPONSE = 4, |
| 501 | /* |
| 502 | * First packet of USB SPI response with the status code |
| 503 | * and read payload if it was successful. |
| 504 | */ |
| 505 | USB_SPI_PKT_ID_RSP_TRANSFER_START = 5, |
| 506 | /* Additional packets containing read payload. */ |
| 507 | USB_SPI_PKT_ID_RSP_TRANSFER_CONTINUE = 6, |
| 508 | }; |
| 509 | |
| 510 | enum feature_bitmap { |
| 511 | /* Indicates the platform supports full duplex mode. */ |
| 512 | USB_SPI_FEATURE_FULL_DUPLEX_SUPPORTED = 0x01 |
| 513 | }; |
| 514 | |
| 515 | struct usb_spi_response_configuration_v2 { |
| 516 | uint16_t packet_id; |
| 517 | uint16_t max_write_count; |
| 518 | uint16_t max_read_count; |
| 519 | uint16_t feature_bitmap; |
| 520 | } __attribute__((packed)); |
| 521 | |
| 522 | struct usb_spi_command_v2 { |
| 523 | uint16_t packet_id; |
| 524 | uint16_t write_count; |
| 525 | /* UINT16_MAX Indicates readback all on halfduplex compliant devices. */ |
| 526 | uint16_t read_count; |
| 527 | uint8_t data[USB_SPI_PAYLOAD_SIZE_V2_START]; |
| 528 | } __attribute__((packed)); |
| 529 | |
| 530 | struct usb_spi_response_v2 { |
| 531 | uint16_t packet_id; |
| 532 | uint16_t status_code; |
| 533 | uint8_t data[USB_SPI_PAYLOAD_SIZE_V2_RESPONSE]; |
| 534 | } __attribute__((packed)); |
| 535 | |
| 536 | struct usb_spi_continue_v2 { |
| 537 | uint16_t packet_id; |
| 538 | uint16_t data_index; |
| 539 | uint8_t data[USB_SPI_PAYLOAD_SIZE_V2_CONTINUE]; |
| 540 | } __attribute__((packed)); |
| 541 | |
| 542 | union usb_spi_packet_v2 { |
| 543 | uint16_t packet_id; |
| 544 | struct usb_spi_command_v2 cmd_start; |
| 545 | struct usb_spi_continue_v2 cmd_continue; |
| 546 | struct usb_spi_response_configuration_v2 rsp_config; |
| 547 | struct usb_spi_response_v2 rsp_start; |
| 548 | struct usb_spi_continue_v2 rsp_continue; |
| 549 | } __attribute__((packed)); |
| 550 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 551 | struct usb_spi_packet_ctx { |
| 552 | union { |
| 553 | uint8_t bytes[USB_MAX_PACKET_SIZE]; |
| 554 | union usb_spi_packet_v1 packet_v1; |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 555 | union usb_spi_packet_v2 packet_v2; |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 556 | }; |
| 557 | /* |
| 558 | * By storing the number of bytes in the header and knowing that the |
| 559 | * USB data packets are all 64B long, we are able to use the header |
| 560 | * size to store the offset of the buffer and it's size without |
| 561 | * duplicating variables that can go out of sync. |
| 562 | */ |
| 563 | size_t header_size; |
| 564 | /* Number of bytes in the packet */ |
| 565 | size_t packet_size; |
| 566 | }; |
| 567 | |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 568 | struct usb_spi_transmit_ctx { |
| 569 | /* Buffer we are reading data from. */ |
| 570 | const uint8_t *buffer; |
| 571 | /* Number of bytes in the transfer. */ |
| 572 | size_t transmit_size; |
| 573 | /* Number of bytes transferred. */ |
| 574 | size_t transmit_index; |
| 575 | }; |
| 576 | |
| 577 | struct usb_spi_receive_ctx { |
| 578 | /* Buffer we are writing data into. */ |
| 579 | uint8_t *buffer; |
| 580 | /* Number of bytes in the transfer. */ |
| 581 | size_t receive_size; |
| 582 | /* Number of bytes transferred. */ |
| 583 | size_t receive_index; |
| 584 | }; |
| 585 | |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 586 | /* |
| 587 | * This function will return true when an error code can potentially recover |
| 588 | * if we attempt to write SPI data to the device or read from it. We know |
| 589 | * that some conditions are not recoverable in the current state so allows us |
| 590 | * to bypass the retry logic and terminate early. |
| 591 | */ |
| 592 | static bool retry_recovery(int error_code) |
| 593 | { |
| 594 | if (error_code < 0x10000) { |
Brian J. Nemec | 1a61f72 | 2020-05-04 20:58:06 -0700 | [diff] [blame] | 595 | /* |
| 596 | * Handle error codes returned from the device. USB_SPI_TIMEOUT, |
| 597 | * USB_SPI_BUSY, and USB_SPI_WRITE_COUNT_INVALID have been observed |
| 598 | * during transfer errors to the device and can be recovered. |
| 599 | */ |
| 600 | if (USB_SPI_READ_COUNT_INVALID <= error_code && |
| 601 | error_code <= USB_SPI_DISABLED) { |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 602 | return false; |
| 603 | } |
| 604 | } else if (usb_device_is_libusb_error(error_code)) { |
| 605 | /* Handle error codes returned from libusb. */ |
| 606 | if (error_code == LIBUSB_ERROR(LIBUSB_ERROR_NO_DEVICE)) { |
| 607 | return false; |
| 608 | } |
| 609 | } |
| 610 | return true; |
| 611 | } |
| 612 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 613 | static struct raiden_debug_spi_data * |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 614 | get_raiden_data_from_context(const struct flashctx *flash) |
| 615 | { |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 616 | return (struct raiden_debug_spi_data *)flash->mst->spi.data; |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 619 | /* |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 620 | * Read data into the receive buffer. |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 621 | * |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 622 | * @param dst Destination receive context we are writing data to. |
| 623 | * @param src Source packet context we are reading data from. |
| 624 | * |
| 625 | * @returns status code 0 on success. |
| 626 | * USB_SPI_HOST_RX_DATA_OVERFLOW if the source packet is too |
| 627 | * large to fit in read buffer. |
| 628 | */ |
| 629 | static int read_usb_packet(struct usb_spi_receive_ctx *dst, |
| 630 | const struct usb_spi_packet_ctx *src) |
| 631 | { |
| 632 | size_t max_read_length = dst->receive_size - dst->receive_index; |
| 633 | size_t bytes_in_buffer = src->packet_size - src->header_size; |
| 634 | const uint8_t *packet_buffer = src->bytes + src->header_size; |
| 635 | |
| 636 | if (bytes_in_buffer > max_read_length) { |
| 637 | /* |
| 638 | * An error occurred, we should not receive more data than |
| 639 | * the buffer can support. |
| 640 | */ |
| 641 | msg_perr("Raiden: Receive packet overflowed\n" |
| 642 | " bytes_in_buffer = %zu\n" |
| 643 | " max_read_length = %zu\n" |
| 644 | " receive_index = %zu\n" |
| 645 | " receive_size = %zu\n", |
| 646 | bytes_in_buffer, max_read_length, |
| 647 | dst->receive_size, dst->receive_index); |
| 648 | return USB_SPI_HOST_RX_DATA_OVERFLOW; |
| 649 | } |
| 650 | memcpy(dst->buffer + dst->receive_index, packet_buffer, |
| 651 | bytes_in_buffer); |
| 652 | |
| 653 | dst->receive_index += bytes_in_buffer; |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | * Fill the USB packet with data from the transmit buffer. |
| 659 | * |
| 660 | * @param dst Destination packet context we are writing data to. |
| 661 | * @param src Source transmit context we are reading data from. |
| 662 | */ |
| 663 | static void fill_usb_packet(struct usb_spi_packet_ctx *dst, |
| 664 | struct usb_spi_transmit_ctx *src) |
| 665 | { |
| 666 | size_t transmit_size = src->transmit_size - src->transmit_index; |
| 667 | size_t max_buffer_size = USB_MAX_PACKET_SIZE - dst->header_size; |
| 668 | uint8_t *packet_buffer = dst->bytes + dst->header_size; |
| 669 | |
| 670 | if (transmit_size > max_buffer_size) |
| 671 | transmit_size = max_buffer_size; |
| 672 | |
| 673 | memcpy(packet_buffer, src->buffer + src->transmit_index, transmit_size); |
| 674 | |
| 675 | dst->packet_size = dst->header_size + transmit_size; |
| 676 | src->transmit_index += transmit_size; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Receive the data from the device USB endpoint and store in the packet. |
| 681 | * |
| 682 | * @param ctx_data Raiden SPI config. |
| 683 | * @param packet Destination packet used to store the endpoint data. |
| 684 | * |
| 685 | * @returns Returns status code with 0 on success. |
| 686 | */ |
| 687 | static int receive_packet(const struct raiden_debug_spi_data *ctx_data, |
| 688 | struct usb_spi_packet_ctx *packet) |
| 689 | { |
| 690 | int received; |
| 691 | int status = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle, |
| 692 | ctx_data->in_ep, |
| 693 | packet->bytes, |
| 694 | USB_MAX_PACKET_SIZE, |
| 695 | &received, |
| 696 | TRANSFER_TIMEOUT_MS)); |
| 697 | packet->packet_size = received; |
| 698 | if (status) { |
| 699 | msg_perr("Raiden: IN transfer failed\n" |
| 700 | " received = %d\n" |
| 701 | " status = 0x%05x\n", |
| 702 | received, status); |
| 703 | } |
| 704 | return status; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Transmit data from the packet to the device's USB endpoint. |
| 709 | * |
| 710 | * @param ctx_data Raiden SPI config. |
| 711 | * @param packet Source packet we will write to the endpoint data. |
| 712 | * |
| 713 | * @returns Returns status code with 0 on success. |
| 714 | */ |
| 715 | static int transmit_packet(const struct raiden_debug_spi_data *ctx_data, |
| 716 | struct usb_spi_packet_ctx *packet) |
| 717 | { |
| 718 | int transferred; |
| 719 | int status = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle, |
| 720 | ctx_data->out_ep, |
| 721 | packet->bytes, |
| 722 | packet->packet_size, |
| 723 | &transferred, |
| 724 | TRANSFER_TIMEOUT_MS)); |
| 725 | if (status || (size_t)transferred != packet->packet_size) { |
| 726 | if (!status) { |
| 727 | /* No error was reported, but we didn't transmit the data expected. */ |
| 728 | status = USB_SPI_HOST_TX_BAD_TRANSFER; |
| 729 | } |
| 730 | msg_perr("Raiden: OUT transfer failed\n" |
| 731 | " transferred = %d\n" |
| 732 | " packet_size = %zu\n" |
| 733 | " status = 0x%05x\n", |
| 734 | transferred, packet->packet_size, status); |
| 735 | |
| 736 | } |
| 737 | return status; |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Version 1 protocol command to start a USB SPI transfer and write the payload. |
| 742 | * |
| 743 | * @param ctx_data Raiden SPI config. |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 744 | * @param write Write context of data to transmit and write payload. |
| 745 | * @param read Read context of data to receive and read buffer. |
| 746 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 747 | * @returns Returns status code with 0 on success. |
| 748 | */ |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 749 | static int write_command_v1(const struct raiden_debug_spi_data *ctx_data, |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 750 | struct usb_spi_transmit_ctx *write, |
| 751 | struct usb_spi_receive_ctx *read) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 752 | { |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 753 | struct usb_spi_packet_ctx command = { |
| 754 | .header_size = offsetof(struct usb_spi_command_v1, data), |
| 755 | .packet_v1.command.write_count = write->transmit_size, |
| 756 | .packet_v1.command.read_count = read->receive_size |
| 757 | }; |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 758 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 759 | /* Reset the write context to the start. */ |
| 760 | write->transmit_index = 0; |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 761 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 762 | fill_usb_packet(&command, write); |
| 763 | return transmit_packet(ctx_data, &command); |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 764 | } |
| 765 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 766 | /* |
| 767 | * Version 1 Protocol: Responsible for reading the response of the USB SPI |
| 768 | * transfer. Status codes from the transfer and any read payload are copied |
| 769 | * to the read_buffer. |
| 770 | * |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 771 | * @param ctx_data Raiden SPI config. |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 772 | * @param write Write context of data to transmit and write payload. |
| 773 | * @param read Read context of data to receive and read buffer. |
| 774 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 775 | * @returns Returns status code with 0 on success. |
| 776 | */ |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 777 | static int read_response_v1(const struct raiden_debug_spi_data *ctx_data, |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 778 | struct usb_spi_transmit_ctx *write, |
| 779 | struct usb_spi_receive_ctx *read) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 780 | { |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 781 | int status; |
| 782 | struct usb_spi_packet_ctx response; |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 783 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 784 | /* Reset the read context to the start. */ |
| 785 | read->receive_index = 0; |
| 786 | |
| 787 | status = receive_packet(ctx_data, &response); |
| 788 | if (status) { |
| 789 | /* Return the transfer error since the status_code is unreliable */ |
| 790 | return status; |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 791 | } |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 792 | if (response.packet_v1.response.status_code) { |
| 793 | return response.packet_v1.response.status_code; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 794 | } |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 795 | response.header_size = offsetof(struct usb_spi_response_v1, data); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 796 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 797 | status = read_usb_packet(read, &response); |
| 798 | return status; |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 799 | } |
| 800 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 801 | /* |
| 802 | * Version 1 Protocol: Sets up a USB SPI transfer, transmits data to the device, |
| 803 | * reads the status code and any payload from the device. This will also handle |
| 804 | * recovery if an error has occurred. |
| 805 | * |
| 806 | * @param flash Flash context storing SPI capabilities and USB device |
| 807 | * information. |
| 808 | * @param write_count Number of bytes to write |
| 809 | * @param read_count Number of bytes to read |
| 810 | * @param write_buffer Address of write buffer |
| 811 | * @param read_buffer Address of buffer to store read data |
| 812 | * |
| 813 | * @returns Returns status code with 0 on success. |
| 814 | */ |
| 815 | static int send_command_v1(const struct flashctx *flash, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 816 | unsigned int write_count, |
| 817 | unsigned int read_count, |
| 818 | const unsigned char *write_buffer, |
| 819 | unsigned char *read_buffer) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 820 | { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 821 | int status = -1; |
| 822 | |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 823 | struct usb_spi_transmit_ctx write_ctx = { |
| 824 | .buffer = write_buffer, |
| 825 | .transmit_size = write_count |
| 826 | }; |
| 827 | struct usb_spi_receive_ctx read_ctx = { |
| 828 | .buffer = read_buffer, |
| 829 | .receive_size = read_count |
| 830 | }; |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 831 | const struct raiden_debug_spi_data *ctx_data = get_raiden_data_from_context(flash); |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 832 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 833 | if (write_count > ctx_data->max_spi_write_count) { |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 834 | msg_perr("Raiden: Invalid write count\n" |
| 835 | " write count = %u\n" |
| 836 | " max write = %d\n", |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 837 | write_count, ctx_data->max_spi_write_count); |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 838 | return SPI_INVALID_LENGTH; |
| 839 | } |
| 840 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 841 | if (read_count > ctx_data->max_spi_read_count) { |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 842 | msg_perr("Raiden: Invalid read count\n" |
| 843 | " read count = %d\n" |
| 844 | " max read = %d\n", |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 845 | read_count, ctx_data->max_spi_read_count); |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 846 | return SPI_INVALID_LENGTH; |
| 847 | } |
| 848 | |
| 849 | for (unsigned int write_attempt = 0; write_attempt < WRITE_RETRY_ATTEMPTS; |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 850 | write_attempt++) { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 851 | |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 852 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 853 | status = write_command_v1(ctx_data, &write_ctx, &read_ctx); |
| 854 | |
| 855 | if (!status && |
| 856 | (write_ctx.transmit_index != write_ctx.transmit_size)) { |
| 857 | /* No errors were reported, but write is incomplete. */ |
| 858 | status = USB_SPI_HOST_TX_WRITE_FAILURE; |
| 859 | } |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 860 | |
| 861 | if (status) { |
| 862 | /* Write operation failed. */ |
| 863 | msg_perr("Raiden: Write command failed\n" |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 864 | " protocol = %u\n" |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 865 | " write count = %u\n" |
| 866 | " read count = %u\n" |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 867 | " transmitted bytes = %zu\n" |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 868 | " write attempt = %u\n" |
| 869 | " status = 0x%05x\n", |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 870 | ctx_data->protocol_version, |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 871 | write_count, read_count, write_ctx.transmit_index, |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 872 | write_attempt + 1, status); |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 873 | if (!retry_recovery(status)) { |
| 874 | /* Reattempting will not result in a recovery. */ |
| 875 | return status; |
| 876 | } |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 877 | programmer_delay(RETRY_INTERVAL_US); |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 878 | continue; |
| 879 | } |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 880 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 881 | for (unsigned int read_attempt = 0; read_attempt < READ_RETRY_ATTEMPTS; |
| 882 | read_attempt++) { |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 883 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 884 | status = read_response_v1(ctx_data, &write_ctx, &read_ctx); |
| 885 | |
| 886 | if (!status) { |
| 887 | if (read_ctx.receive_size == read_ctx.receive_index) { |
| 888 | /* Successful transfer. */ |
| 889 | return status; |
| 890 | } else { |
| 891 | /* Report the error from the failed read. */ |
| 892 | status = USB_SPI_HOST_RX_READ_FAILURE; |
| 893 | } |
| 894 | } |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 895 | |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 896 | /* Read operation failed. */ |
| 897 | msg_perr("Raiden: Read response failed\n" |
| 898 | " protocol = %u\n" |
| 899 | " write count = %u\n" |
| 900 | " read count = %u\n" |
| 901 | " received bytes = %zu\n" |
| 902 | " write attempt = %u\n" |
| 903 | " read attempt = %u\n" |
| 904 | " status = 0x%05x\n", |
| 905 | ctx_data->protocol_version, |
| 906 | write_count, read_count, read_ctx.receive_index, |
| 907 | write_attempt + 1, read_attempt + 1, status); |
| 908 | if (!retry_recovery(status)) { |
| 909 | /* Reattempting will not result in a recovery. */ |
| 910 | return status; |
| 911 | } |
| 912 | programmer_delay(RETRY_INTERVAL_US); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | return status; |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * Get the USB SPI configuration with the maximum write and read counts, and |
| 921 | * any enabled features. |
| 922 | * |
| 923 | * @param ctx_data Raiden SPI config. |
| 924 | * |
| 925 | * @returns Returns status code with 0 on success. |
| 926 | */ |
| 927 | static int get_spi_config_v2(struct raiden_debug_spi_data *ctx_data) |
| 928 | { |
| 929 | int status; |
| 930 | unsigned int config_attempt; |
| 931 | struct usb_spi_packet_ctx rsp_config; |
| 932 | |
| 933 | struct usb_spi_packet_ctx cmd_get_config = { |
| 934 | .header_size = PACKET_HEADER_SIZE, |
| 935 | .packet_size = PACKET_HEADER_SIZE, |
| 936 | .packet_v2.packet_id = USB_SPI_PKT_ID_CMD_GET_USB_SPI_CONFIG |
| 937 | }; |
| 938 | |
| 939 | for (config_attempt = 0; config_attempt < GET_CONFIG_RETRY_ATTEMPTS; config_attempt++) { |
| 940 | |
| 941 | status = transmit_packet(ctx_data, &cmd_get_config); |
| 942 | if (status) { |
| 943 | msg_perr("Raiden: Failed to transmit get config\n" |
| 944 | " config attempt = %d\n" |
| 945 | " status = 0x%05x\n", |
| 946 | config_attempt + 1, status); |
| 947 | programmer_delay(RETRY_INTERVAL_US); |
| 948 | continue; |
| 949 | } |
| 950 | |
| 951 | status = receive_packet(ctx_data, &rsp_config); |
| 952 | if (status) { |
| 953 | msg_perr("Raiden: Failed to receive packet\n" |
| 954 | " config attempt = %d\n" |
| 955 | " status = 0x%05x\n", |
| 956 | config_attempt + 1, status); |
| 957 | programmer_delay(RETRY_INTERVAL_US); |
| 958 | continue; |
| 959 | } |
| 960 | |
| 961 | /* |
| 962 | * Perform validation on the packet received to verify it is a valid |
| 963 | * configuration. If it is, we are ready to perform transfers. |
| 964 | */ |
| 965 | if ((rsp_config.packet_v2.packet_id == |
| 966 | USB_SPI_PKT_ID_RSP_USB_SPI_CONFIG) || |
| 967 | (rsp_config.packet_size == |
| 968 | sizeof(struct usb_spi_response_configuration_v2))) { |
| 969 | |
| 970 | /* Set the parameters from the configuration. */ |
| 971 | ctx_data->max_spi_write_count = |
| 972 | rsp_config.packet_v2.rsp_config.max_write_count; |
| 973 | ctx_data->max_spi_read_count = |
| 974 | rsp_config.packet_v2.rsp_config.max_read_count; |
| 975 | return status; |
| 976 | } |
| 977 | |
| 978 | msg_perr("Raiden: Packet is not a valid config\n" |
| 979 | " config attempt = %d\n" |
| 980 | " packet id = %u\n" |
| 981 | " packet size = %zu\n", |
| 982 | config_attempt + 1, |
| 983 | rsp_config.packet_v2.packet_id, |
| 984 | rsp_config.packet_size); |
| 985 | programmer_delay(RETRY_INTERVAL_US); |
| 986 | } |
| 987 | return USB_SPI_HOST_INIT_FAILURE; |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * Version 2 protocol restart the SPI response. This allows us to recover from |
| 992 | * USB packet errors without restarting the SPI transfer. |
| 993 | * |
| 994 | * @param ctx_data Raiden SPI config. |
| 995 | * |
| 996 | * @returns Returns status code with 0 on success. |
| 997 | */ |
| 998 | static int restart_response_v2(const struct raiden_debug_spi_data *ctx_data) |
| 999 | { |
| 1000 | struct usb_spi_packet_ctx restart_response = { |
| 1001 | .header_size = PACKET_HEADER_SIZE, |
| 1002 | .packet_size = PACKET_HEADER_SIZE, |
| 1003 | .packet_v2.packet_id = USB_SPI_PKT_ID_CMD_RESTART_RESPONSE |
| 1004 | }; |
| 1005 | |
| 1006 | return transmit_packet(ctx_data, &restart_response); |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * Version 2 Protocol: command to start a USB SPI transfer and write the payload. |
| 1011 | * |
| 1012 | * @param ctx_data Raiden SPI config. |
| 1013 | * @param write Write context of data to transmit and write payload. |
| 1014 | * @param read Read context of data to receive and read buffer. |
| 1015 | * |
| 1016 | * @returns Returns status code with 0 on success. |
| 1017 | */ |
| 1018 | static int write_command_v2(const struct raiden_debug_spi_data *ctx_data, |
| 1019 | struct usb_spi_transmit_ctx *write, |
| 1020 | struct usb_spi_receive_ctx *read) |
| 1021 | { |
| 1022 | int status; |
| 1023 | struct usb_spi_packet_ctx continue_packet; |
| 1024 | |
| 1025 | struct usb_spi_packet_ctx start_usb_spi_packet = { |
| 1026 | .header_size = offsetof(struct usb_spi_command_v2, data), |
| 1027 | .packet_v2.cmd_start.packet_id = USB_SPI_PKT_ID_CMD_TRANSFER_START, |
| 1028 | .packet_v2.cmd_start.write_count = write->transmit_size, |
| 1029 | .packet_v2.cmd_start.read_count = read->receive_size |
| 1030 | }; |
| 1031 | |
| 1032 | /* Reset the write context to the start. */ |
| 1033 | write->transmit_index = 0; |
| 1034 | |
| 1035 | fill_usb_packet(&start_usb_spi_packet, write); |
| 1036 | status = transmit_packet(ctx_data, &start_usb_spi_packet); |
| 1037 | if (status) { |
| 1038 | return status; |
| 1039 | } |
| 1040 | |
| 1041 | while (write->transmit_index < write->transmit_size) { |
| 1042 | /* Transmit any continue packets. */ |
| 1043 | continue_packet.header_size = offsetof(struct usb_spi_continue_v2, data); |
| 1044 | continue_packet.packet_v2.cmd_continue.packet_id = |
| 1045 | USB_SPI_PKT_ID_CMD_TRANSFER_CONTINUE; |
| 1046 | continue_packet.packet_v2.cmd_continue.data_index = |
| 1047 | write->transmit_index; |
| 1048 | |
| 1049 | fill_usb_packet(&continue_packet, write); |
| 1050 | |
| 1051 | status = transmit_packet(ctx_data, &continue_packet); |
| 1052 | if (status) { |
| 1053 | return status; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | return status; |
| 1058 | } |
| 1059 | |
| 1060 | /* |
| 1061 | * Version 2 Protocol: Command to read a USB SPI transfer response and read the payload. |
| 1062 | * |
| 1063 | * @param ctx_data Raiden SPI config. |
| 1064 | * @param write Write context of data to transmit and write payload. |
| 1065 | * @param read Read context of data to receive and read buffer. |
| 1066 | * |
| 1067 | * @returns Returns status code with 0 on success. |
| 1068 | */ |
| 1069 | static int read_response_v2(const struct raiden_debug_spi_data *ctx_data, |
| 1070 | struct usb_spi_transmit_ctx *write, |
| 1071 | struct usb_spi_receive_ctx *read) |
| 1072 | { |
| 1073 | int status = -1; |
| 1074 | struct usb_spi_packet_ctx response; |
| 1075 | |
| 1076 | /* Reset the read context to the start. */ |
| 1077 | read->receive_index = 0; |
| 1078 | |
| 1079 | /* Receive the payload to the servo micro. */ |
| 1080 | do { |
| 1081 | status = receive_packet(ctx_data, &response); |
| 1082 | if (status) { |
| 1083 | /* Return the transfer error. */ |
| 1084 | return status; |
| 1085 | } |
| 1086 | if (response.packet_v2.packet_id == USB_SPI_PKT_ID_RSP_TRANSFER_START) { |
| 1087 | /* |
| 1088 | * The host should only see this packet if an error occurs |
| 1089 | * on the device or if it's the first response packet. |
| 1090 | */ |
| 1091 | if (response.packet_v2.rsp_start.status_code) { |
| 1092 | return response.packet_v2.rsp_start.status_code; |
| 1093 | } |
| 1094 | if (read->receive_index) { |
| 1095 | msg_perr("Raiden: Unexpected start packet id = %u\n", |
| 1096 | response.packet_v2.rsp_start.packet_id); |
| 1097 | return USB_SPI_HOST_RX_UNEXPECTED_PACKET; |
| 1098 | } |
| 1099 | response.header_size = offsetof(struct usb_spi_response_v2, data); |
| 1100 | } else if (response.packet_v2.packet_id == |
| 1101 | USB_SPI_PKT_ID_RSP_TRANSFER_CONTINUE) { |
| 1102 | |
| 1103 | /* We validate that no packets were missed. */ |
| 1104 | if (read->receive_index != |
| 1105 | response.packet_v2.rsp_continue.data_index) { |
| 1106 | msg_perr("Raiden: Bad Index = %u Expected = %zu\n", |
| 1107 | response.packet_v2.rsp_continue.data_index, |
| 1108 | read->receive_index); |
| 1109 | return USB_SPI_HOST_RX_BAD_DATA_INDEX; |
| 1110 | } |
| 1111 | response.header_size = offsetof(struct usb_spi_continue_v2, data); |
| 1112 | } else { |
| 1113 | msg_perr("Raiden: Unexpected packet id = %u\n", |
| 1114 | response.packet_v2.packet_id); |
| 1115 | return USB_SPI_HOST_RX_UNEXPECTED_PACKET; |
| 1116 | } |
| 1117 | status = read_usb_packet(read, &response); |
| 1118 | if (status) { |
| 1119 | return status; |
| 1120 | } |
| 1121 | } while (read->receive_index < read->receive_size); |
| 1122 | |
| 1123 | return status; |
| 1124 | } |
| 1125 | |
| 1126 | /* |
| 1127 | * Version 2 Protocol: Sets up a USB SPI transfer, transmits data to the device, |
| 1128 | * reads the status code and any payload from the device. This will also handle |
| 1129 | * recovery if an error has occurred. |
| 1130 | * |
| 1131 | * In order to avoid having the v2 protocol held back by requiring |
| 1132 | * backwards compatibility with v1 we are duplicating the send_command |
| 1133 | * function. This will allow the 2 versions to diverge in the future |
| 1134 | * so fixes in one do not need to be compatible with the legacy. |
| 1135 | * |
| 1136 | * @param flash Flash context storing SPI capabilities and USB device |
| 1137 | * information. |
| 1138 | * @param write_count Number of bytes to write |
| 1139 | * @param read_count Number of bytes to read |
| 1140 | * @param write_buffer Address of write buffer |
| 1141 | * @param read_buffer Address of buffer to store read data |
| 1142 | * |
| 1143 | * @returns Returns status code with 0 on success. |
| 1144 | */ |
| 1145 | static int send_command_v2(const struct flashctx *flash, |
| 1146 | unsigned int write_count, |
| 1147 | unsigned int read_count, |
| 1148 | const unsigned char *write_buffer, |
| 1149 | unsigned char *read_buffer) |
| 1150 | { |
| 1151 | const struct raiden_debug_spi_data *ctx_data = |
| 1152 | get_raiden_data_from_context(flash); |
| 1153 | int status = -1; |
| 1154 | unsigned int write_attempt; |
| 1155 | unsigned int read_attempt; |
| 1156 | |
| 1157 | struct usb_spi_transmit_ctx write_ctx = { |
| 1158 | .buffer = write_buffer, |
| 1159 | .transmit_size = write_count |
| 1160 | }; |
| 1161 | struct usb_spi_receive_ctx read_ctx = { |
| 1162 | .buffer = read_buffer, |
| 1163 | .receive_size = read_count |
| 1164 | }; |
| 1165 | |
| 1166 | if (write_count > ctx_data->max_spi_write_count) { |
| 1167 | msg_perr("Raiden: Invalid write count\n" |
| 1168 | " write count = %u\n" |
| 1169 | " max write = %u\n", |
| 1170 | write_count, ctx_data->max_spi_write_count); |
| 1171 | return SPI_INVALID_LENGTH; |
| 1172 | } |
| 1173 | |
| 1174 | if (read_count > ctx_data->max_spi_read_count) { |
| 1175 | msg_perr("Raiden: Invalid read count\n" |
| 1176 | " read count = %u\n" |
| 1177 | " max read = %u\n", |
| 1178 | read_count, ctx_data->max_spi_read_count); |
| 1179 | return SPI_INVALID_LENGTH; |
| 1180 | } |
| 1181 | |
| 1182 | for (write_attempt = 0; write_attempt < WRITE_RETRY_ATTEMPTS; |
| 1183 | write_attempt++) { |
| 1184 | |
| 1185 | status = write_command_v2(ctx_data, &write_ctx, &read_ctx); |
| 1186 | |
| 1187 | if (!status && |
| 1188 | (write_ctx.transmit_index != write_ctx.transmit_size)) { |
| 1189 | /* No errors were reported, but write is incomplete. */ |
| 1190 | status = USB_SPI_HOST_TX_WRITE_FAILURE; |
| 1191 | } |
| 1192 | |
| 1193 | if (status) { |
| 1194 | /* Write operation failed. */ |
| 1195 | msg_perr("Raiden: Write command failed\n" |
| 1196 | " protocol = %u\n" |
| 1197 | " write count = %u\n" |
| 1198 | " read count = %u\n" |
| 1199 | " transmitted bytes = %zu\n" |
| 1200 | " write attempt = %u\n" |
| 1201 | " status = 0x%05x\n", |
| 1202 | ctx_data->protocol_version, |
| 1203 | write_count, read_count, write_ctx.transmit_index, |
| 1204 | write_attempt + 1, status); |
| 1205 | if (!retry_recovery(status)) { |
| 1206 | /* Reattempting will not result in a recovery. */ |
| 1207 | return status; |
| 1208 | } |
| 1209 | programmer_delay(RETRY_INTERVAL_US); |
| 1210 | continue; |
| 1211 | } |
| 1212 | for (read_attempt = 0; read_attempt < READ_RETRY_ATTEMPTS; |
| 1213 | read_attempt++) { |
| 1214 | |
| 1215 | status = read_response_v2(ctx_data, &write_ctx, &read_ctx); |
| 1216 | |
| 1217 | if (!status) { |
| 1218 | if (read_ctx.receive_size == read_ctx.receive_index) { |
| 1219 | /* Successful transfer. */ |
| 1220 | return status; |
| 1221 | } else { |
| 1222 | /* Report the error from the failed read. */ |
| 1223 | status = USB_SPI_HOST_RX_READ_FAILURE; |
| 1224 | } |
| 1225 | } |
| 1226 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 1227 | if (status) { |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 1228 | /* Read operation failed. */ |
| 1229 | msg_perr("Raiden: Read response failed\n" |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 1230 | " protocol = %u\n" |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 1231 | " write count = %u\n" |
| 1232 | " read count = %u\n" |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 1233 | " received bytes = %zu\n" |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 1234 | " write attempt = %u\n" |
| 1235 | " read attempt = %u\n" |
| 1236 | " status = 0x%05x\n", |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 1237 | ctx_data->protocol_version, |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 1238 | write_count, read_count, read_ctx.receive_index, |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 1239 | write_attempt + 1, read_attempt + 1, status); |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 1240 | if (!retry_recovery(status)) { |
| 1241 | /* Reattempting will not result in a recovery. */ |
| 1242 | return status; |
| 1243 | } |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 1244 | /* Device needs to reset its transmit index. */ |
| 1245 | restart_response_v2(ctx_data); |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 1246 | programmer_delay(RETRY_INTERVAL_US); |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 1247 | } |
| 1248 | } |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 1249 | } |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 1250 | return status; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1253 | static const struct spi_master spi_master_raiden_debug = { |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 1254 | .features = SPI_MASTER_4BA, |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1255 | .max_data_read = 0, |
| 1256 | .max_data_write = 0, |
| 1257 | .command = NULL, |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 1258 | .multicommand = default_spi_send_multicommand, |
| 1259 | .read = default_spi_read, |
| 1260 | .write_256 = default_spi_write_256, |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 1261 | }; |
| 1262 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1263 | static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor, |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 1264 | enum libusb_endpoint_direction direction) |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 1265 | { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1266 | return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == |
| 1267 | direction) && |
| 1268 | ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == |
| 1269 | LIBUSB_TRANSFER_TYPE_BULK)); |
| 1270 | } |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 1271 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 1272 | 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] | 1273 | { |
| 1274 | int i; |
| 1275 | int in_count = 0; |
| 1276 | int out_count = 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 1277 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 1278 | for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1279 | struct libusb_endpoint_descriptor const *endpoint = |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 1280 | &dev->interface_descriptor->endpoint[i]; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 1281 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1282 | if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) { |
| 1283 | in_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 1284 | *in_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1285 | } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) { |
| 1286 | out_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 1287 | *out_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | if (in_count != 1 || out_count != 1) { |
| 1292 | msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n" |
| 1293 | " found %d IN and %d OUT endpoints\n", |
| 1294 | in_count, |
| 1295 | out_count); |
| 1296 | return 1; |
| 1297 | } |
| 1298 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 1299 | msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep); |
| 1300 | msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep); |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1301 | |
| 1302 | return 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1305 | /* |
| 1306 | * Configure the USB SPI master based on the device we are connected to. |
| 1307 | * It will use the device's bInterfaceProtocol to identify which protocol |
| 1308 | * is being used by the device USB SPI interface and if needed query the |
| 1309 | * device for its capabilities. |
| 1310 | * |
| 1311 | * @param spi_config Raiden SPI config which will be modified. |
| 1312 | * |
| 1313 | * @returns Returns status code with 0 on success. |
| 1314 | */ |
| 1315 | static int configure_protocol(struct spi_master *spi_config) |
| 1316 | { |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 1317 | int status = 0; |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1318 | struct raiden_debug_spi_data *ctx_data = |
| 1319 | (struct raiden_debug_spi_data *)spi_config->data; |
| 1320 | |
| 1321 | ctx_data->protocol_version = |
| 1322 | ctx_data->dev->interface_descriptor->bInterfaceProtocol; |
| 1323 | |
| 1324 | switch (ctx_data->protocol_version) { |
| 1325 | case GOOGLE_RAIDEN_SPI_PROTOCOL_V1: |
| 1326 | /* |
| 1327 | * Protocol V1 is supported by adjusting the max data |
| 1328 | * read and write sizes which results in no continue packets. |
| 1329 | */ |
| 1330 | spi_config->command = send_command_v1; |
| 1331 | ctx_data->max_spi_write_count = SPI_TRANSFER_V1_MAX; |
| 1332 | ctx_data->max_spi_read_count = SPI_TRANSFER_V1_MAX; |
| 1333 | break; |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 1334 | case GOOGLE_RAIDEN_SPI_PROTOCOL_V2: |
| 1335 | /* |
| 1336 | * Protocol V2 requires the host to query the device for |
| 1337 | * its maximum read and write sizes |
| 1338 | */ |
| 1339 | spi_config->command = send_command_v2; |
| 1340 | status = get_spi_config_v2(ctx_data); |
| 1341 | if (status) { |
| 1342 | return status; |
| 1343 | } |
| 1344 | break; |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1345 | default: |
Brian J. Nemec | 7b75193 | 2020-07-22 03:32:37 -0700 | [diff] [blame] | 1346 | msg_pdbg("Raiden: Unknown USB SPI protocol version = %u\n", |
| 1347 | ctx_data->protocol_version); |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1348 | return USB_SPI_HOST_INIT_FAILURE; |
| 1349 | } |
| 1350 | |
| 1351 | /* |
| 1352 | * Unfortunately there doesn't seem to be a way to specify the maximum number |
| 1353 | * of bytes that your SPI device can read/write, these values are the maximum |
| 1354 | * data chunk size that flashrom will package up with an additional five bytes |
| 1355 | * of command for the flash device. |
| 1356 | * |
| 1357 | * The largest command that flashrom generates is the byte program command, so |
| 1358 | * we use that command header maximum size here. If we didn't include the |
| 1359 | * offset, flashrom may request a SPI transfer that is too large for the SPI |
| 1360 | * device to support. |
| 1361 | */ |
| 1362 | spi_config->max_data_write = ctx_data->max_spi_write_count - |
| 1363 | JEDEC_BYTE_PROGRAM_OUTSIZE; |
| 1364 | spi_config->max_data_read = ctx_data->max_spi_read_count - |
| 1365 | JEDEC_BYTE_PROGRAM_OUTSIZE; |
| 1366 | |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 1370 | static int raiden_debug_spi_shutdown(void * data) |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 1371 | { |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1372 | struct spi_master *spi_config = data; |
| 1373 | struct raiden_debug_spi_data *ctx_data = |
| 1374 | (struct raiden_debug_spi_data *)spi_config->data; |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 1375 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 1376 | int ret = LIBUSB(libusb_control_transfer( |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 1377 | ctx_data->dev->handle, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 1378 | LIBUSB_ENDPOINT_OUT | |
| 1379 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 1380 | LIBUSB_RECIPIENT_INTERFACE, |
| 1381 | RAIDEN_DEBUG_SPI_REQ_DISABLE, |
| 1382 | 0, |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 1383 | ctx_data->dev->interface_descriptor->bInterfaceNumber, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 1384 | NULL, |
| 1385 | 0, |
| 1386 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 1387 | if (ret != 0) { |
| 1388 | msg_perr("Raiden: Failed to disable SPI bridge\n"); |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1389 | free(ctx_data); |
| 1390 | free(spi_config); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 1391 | return ret; |
| 1392 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 1393 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 1394 | usb_device_free(ctx_data->dev); |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 1395 | libusb_exit(NULL); |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 1396 | free(ctx_data); |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1397 | free(spi_config); |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 1398 | |
| 1399 | return 0; |
| 1400 | } |
| 1401 | |
Edward O'Callaghan | dda1e54 | 2020-02-03 12:45:01 +1100 | [diff] [blame] | 1402 | static int get_target(void) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 1403 | { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 1404 | int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 1405 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 1406 | char *target_str = extract_programmer_param("target"); |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 1407 | if (target_str) { |
| 1408 | if (!strcasecmp(target_str, "ap")) |
| 1409 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP; |
| 1410 | else if (!strcasecmp(target_str, "ec")) |
| 1411 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC; |
| 1412 | else { |
| 1413 | msg_perr("Invalid target: %s\n", target_str); |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 1414 | request_enable = -1; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 1415 | } |
| 1416 | } |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1417 | free(target_str); |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 1418 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 1419 | return request_enable; |
| 1420 | } |
| 1421 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 1422 | static void free_dev_list(struct usb_device **dev_lst) |
| 1423 | { |
| 1424 | struct usb_device *dev = *dev_lst; |
| 1425 | /* free devices we don't care about */ |
| 1426 | dev = dev->next; |
| 1427 | while (dev) |
| 1428 | dev = usb_device_free(dev); |
| 1429 | } |
| 1430 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 1431 | int raiden_debug_spi_init(void) |
| 1432 | { |
| 1433 | struct usb_match match; |
| 1434 | char *serial = extract_programmer_param("serial"); |
| 1435 | struct usb_device *current; |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 1436 | struct usb_device *device = NULL; |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 1437 | int found = 0; |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 1438 | int ret; |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 1439 | |
| 1440 | int request_enable = get_target(); |
Brian J. Nemec | 7b5ad79 | 2020-07-23 03:35:54 -0700 | [diff] [blame] | 1441 | if (request_enable < 0) { |
| 1442 | free(serial); |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 1443 | return 1; |
Brian J. Nemec | 7b5ad79 | 2020-07-23 03:35:54 -0700 | [diff] [blame] | 1444 | } |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 1445 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1446 | usb_match_init(&match); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 1447 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1448 | usb_match_value_default(&match.vid, GOOGLE_VID); |
| 1449 | usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC); |
| 1450 | usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 1451 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 1452 | ret = LIBUSB(libusb_init(NULL)); |
| 1453 | if (ret != 0) { |
| 1454 | msg_perr("Raiden: libusb_init failed\n"); |
Brian J. Nemec | a88d96e | 2020-07-23 03:22:57 -0700 | [diff] [blame] | 1455 | free(serial); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 1456 | return ret; |
| 1457 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1458 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 1459 | ret = usb_device_find(&match, ¤t); |
| 1460 | if (ret != 0) { |
| 1461 | msg_perr("Raiden: Failed to find devices\n"); |
Brian J. Nemec | a88d96e | 2020-07-23 03:22:57 -0700 | [diff] [blame] | 1462 | free(serial); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 1463 | return ret; |
| 1464 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1465 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 1466 | uint8_t in_endpoint = 0; |
| 1467 | uint8_t out_endpoint = 0; |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 1468 | while (current) { |
| 1469 | device = current; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1470 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 1471 | if (find_endpoints(device, &in_endpoint, &out_endpoint)) { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 1472 | msg_pdbg("Raiden: Failed to find valid endpoints on device"); |
| 1473 | usb_device_show(" ", current); |
| 1474 | goto loop_end; |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 1475 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1476 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 1477 | if (usb_device_claim(device)) { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 1478 | msg_pdbg("Raiden: Failed to claim USB device"); |
| 1479 | usb_device_show(" ", current); |
| 1480 | goto loop_end; |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 1481 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1482 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 1483 | if (!serial) { |
| 1484 | found = 1; |
| 1485 | goto loop_end; |
| 1486 | } else { |
| 1487 | unsigned char dev_serial[32]; |
| 1488 | struct libusb_device_descriptor descriptor; |
| 1489 | int rc; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1490 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 1491 | memset(dev_serial, 0, sizeof(dev_serial)); |
| 1492 | |
| 1493 | if (libusb_get_device_descriptor(device->device, &descriptor)) { |
| 1494 | msg_pdbg("USB: Failed to get device descriptor.\n"); |
| 1495 | goto loop_end; |
| 1496 | } |
| 1497 | |
| 1498 | rc = libusb_get_string_descriptor_ascii(device->handle, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 1499 | descriptor.iSerialNumber, |
| 1500 | dev_serial, |
| 1501 | sizeof(dev_serial)); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 1502 | if (rc < 0) { |
| 1503 | LIBUSB(rc); |
| 1504 | } else { |
| 1505 | if (strcmp(serial, (char *)dev_serial)) { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 1506 | msg_pdbg("Raiden: Serial number %s did not match device", serial); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 1507 | usb_device_show(" ", current); |
| 1508 | } else { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 1509 | msg_pinfo("Raiden: Serial number %s matched device", serial); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 1510 | usb_device_show(" ", current); |
| 1511 | found = 1; |
| 1512 | } |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | loop_end: |
| 1517 | if (found) |
| 1518 | break; |
| 1519 | else |
| 1520 | current = usb_device_free(current); |
| 1521 | } |
| 1522 | |
| 1523 | if (!device || !found) { |
| 1524 | msg_perr("Raiden: No usable device found.\n"); |
Brian J. Nemec | a88d96e | 2020-07-23 03:22:57 -0700 | [diff] [blame] | 1525 | free(serial); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1526 | return 1; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 1529 | free_dev_list(¤t); |
Anton Staaf | b4661ee | 2014-10-21 11:24:36 -0700 | [diff] [blame] | 1530 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 1531 | ret = LIBUSB(libusb_control_transfer( |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 1532 | device->handle, |
| 1533 | LIBUSB_ENDPOINT_OUT | |
| 1534 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 1535 | LIBUSB_RECIPIENT_INTERFACE, |
| 1536 | request_enable, |
| 1537 | 0, |
| 1538 | device->interface_descriptor->bInterfaceNumber, |
| 1539 | NULL, |
| 1540 | 0, |
| 1541 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 1542 | if (ret != 0) { |
| 1543 | msg_perr("Raiden: Failed to enable SPI bridge\n"); |
| 1544 | return ret; |
| 1545 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 1546 | |
Keith Short | 8453b55 | 2020-02-03 18:10:14 -0700 | [diff] [blame] | 1547 | /* |
| 1548 | * Allow for power to settle on the AP and EC flash devices. |
| 1549 | * Load switches can have a 1-3 ms turn on time, and SPI flash devices |
| 1550 | * can require up to 10 ms from power on to the first write. |
| 1551 | */ |
| 1552 | if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) || |
| 1553 | (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC)) |
| 1554 | usleep(50 * 1000); |
| 1555 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1556 | struct spi_master *spi_config = calloc(1, sizeof(struct spi_master)); |
| 1557 | if (!spi_config) { |
| 1558 | msg_perr("Unable to allocate space for SPI master.\n"); |
| 1559 | return SPI_GENERIC_ERROR; |
| 1560 | } |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 1561 | struct raiden_debug_spi_data *data = calloc(1, sizeof(struct raiden_debug_spi_data)); |
| 1562 | if (!data) { |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1563 | free(spi_config); |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 1564 | msg_perr("Unable to allocate space for extra SPI master data.\n"); |
| 1565 | return SPI_GENERIC_ERROR; |
| 1566 | } |
| 1567 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1568 | memcpy(spi_config, &spi_master_raiden_debug, sizeof(struct spi_master)); |
| 1569 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 1570 | data->dev = device; |
| 1571 | data->in_ep = in_endpoint; |
| 1572 | data->out_ep = out_endpoint; |
| 1573 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1574 | spi_config->data = data; |
| 1575 | /* |
| 1576 | * The SPI master needs to be configured based on the device connected. |
| 1577 | * Using the device protocol interrogation, we will set the limits on |
| 1578 | * the write and read sizes and switch command functions. |
| 1579 | */ |
| 1580 | ret = configure_protocol(spi_config); |
| 1581 | if (ret) { |
| 1582 | msg_perr("Raiden: Error configuring protocol\n" |
| 1583 | " protocol = %u\n" |
| 1584 | " status = 0x%05x\n", |
| 1585 | data->dev->interface_descriptor->bInterfaceProtocol, ret); |
| 1586 | free(data); |
| 1587 | free(spi_config); |
| 1588 | return SPI_GENERIC_ERROR; |
| 1589 | } |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 1590 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame] | 1591 | register_spi_master(spi_config); |
| 1592 | register_shutdown(raiden_debug_spi_shutdown, spi_config); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 1593 | |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 1594 | return 0; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 1595 | } |