blob: 39253ae8188ca583e2a034b9335fa30edda9237e [file] [log] [blame]
Anton Staafb2647882014-09-17 15:13:43 -07001/*
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. Nemecc5d69462020-07-22 03:12:15 -070045 * The protocol for the USB-SPI bridge is implemented in the following files
46 * in that repository:
Anton Staafb2647882014-09-17 15:13:43 -070047 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070048 * chip/stm32/usb_spi.h
Anton Staafb2647882014-09-17 15:13:43 -070049 * chip/stm32/usb_spi.c
Brian J. Nemecda496dc2020-02-04 11:13:05 -080050 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070051 * bInterfaceProtocol determines which protocol is used by the USB SPI device.
Brian J. Nemecda496dc2020-02-04 11:13:05 -080052 *
Brian J. Nemecda496dc2020-02-04 11:13:05 -080053 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070054 * 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. Nemecda496dc2020-02-04 11:13:05 -080070 * +------------------+-----------------+------------------------+
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. Nemecc5d69462020-07-22 03:12:15 -070076 * read count: 1 byte, zero based count of bytes to read. Full duplex
77 * mode is enabled with UINT8_MAX
Brian J. Nemecda496dc2020-02-04 11:13:05 -080078 *
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. Nemecc5d69462020-07-22 03:12:15 -070084 * 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. Nemecda496dc2020-02-04 11:13:05 -080089 * +-------------+-----------------------+
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. Nemecc5d69462020-07-22 03:12:15 -070099 * 0x0003: Write count invalid (over 62 bytes)
100 * 0x0004: Read count invalid (over 62 bytes)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800101 * 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. Nemeccea6bec2020-02-25 14:12:46 -0800111 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700112 *
Brian J. Nemec7b751932020-07-22 03:32:37 -0700113 * 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. Nemeccea6bec2020-02-25 14:12:46 -0800329 * 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. Nemecc5d69462020-07-22 03:12:15 -0700335 * 0x10001-0x1FFFF: Error code returned by the USB SPI host.
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800336 * 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 Staafb2647882014-09-17 15:13:43 -0700339 */
340
Anton Staafb2647882014-09-17 15:13:43 -0700341#include "programmer.h"
342#include "spi.h"
Anton Staaf5614e252015-03-24 14:33:33 -0700343#include "usb_device.h"
Anton Staafb2647882014-09-17 15:13:43 -0700344
345#include <libusb.h>
Anton Staaf5614e252015-03-24 14:33:33 -0700346#include <stdio.h>
Anton Staafb2647882014-09-17 15:13:43 -0700347#include <stdlib.h>
Anton Staaf5614e252015-03-24 14:33:33 -0700348#include <string.h>
Keith Short8453b552020-02-03 18:10:14 -0700349#include <unistd.h>
Anton Staafb2647882014-09-17 15:13:43 -0700350
Brian J. Nemecb42d6c12020-07-23 03:07:38 -0700351/* FIXME: Add some programmer IDs here */
352const struct dev_entry devs_raiden[] = {
353 {0},
354};
355
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800356#define GOOGLE_VID (0x18D1)
357#define GOOGLE_RAIDEN_SPI_SUBCLASS (0x51)
Brian J. Nemecb746f822020-07-22 02:57:56 -0700358
359enum {
360 GOOGLE_RAIDEN_SPI_PROTOCOL_V1 = 0x01,
361 GOOGLE_RAIDEN_SPI_PROTOCOL_V2 = 0x02,
362};
Anton Staafb2647882014-09-17 15:13:43 -0700363
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700364enum {
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. Nemeccea6bec2020-02-25 14:12:46 -0800383enum usb_spi_error {
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700384 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. Nemec7b751932020-07-22 03:32:37 -0700390 /* 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. Nemecc5d69462020-07-22 03:12:15 -0700398 USB_SPI_UNKNOWN_ERROR = 0x8000,
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800399};
400
Anton Staaf4589cd12015-03-23 13:36:44 -0700401enum raiden_debug_spi_request {
Mary Ruthveneafafd82016-05-03 14:33:53 -0700402 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 Staaf4589cd12015-03-23 13:36:44 -0700406};
407
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800408/*
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. Nemec7b751932020-07-22 03:32:37 -0700414#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. Nemec1118a582020-02-04 18:26:02 -0800418
Anton Staafb2647882014-09-17 15:13:43 -0700419/*
420 * This timeout is so large because the Raiden SPI timeout is 800ms.
421 */
Edward O'Callaghanf1e6ef52020-03-03 13:57:15 +1100422#define TRANSFER_TIMEOUT_MS (200 + 800)
Anton Staafb2647882014-09-17 15:13:43 -0700423
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700424struct raiden_debug_spi_data {
425 struct usb_device *dev;
426 uint8_t in_ep;
427 uint8_t out_ep;
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700428 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. Nemec721ac2c2020-07-23 03:30:53 -0700437};
Brian J. Nemec7b751932020-07-22 03:32:37 -0700438/*
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 Staafb2647882014-09-17 15:13:43 -0700450
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700451/*
452 * Version 1 protocol specific attributes
453 */
Anton Staafb2647882014-09-17 15:13:43 -0700454
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700455struct 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
462struct usb_spi_response_v1 {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800463 uint16_t status_code;
Brian J. Nemecb746f822020-07-22 02:57:56 -0700464 uint8_t data[PAYLOAD_SIZE_V1];
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700465} __attribute__((packed));
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800466
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700467union usb_spi_packet_v1 {
468 struct usb_spi_command_v1 command;
469 struct usb_spi_response_v1 response;
470} __attribute__((packed));
471
Brian J. Nemec7b751932020-07-22 03:32:37 -0700472/*
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
484enum 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
510enum feature_bitmap {
511 /* Indicates the platform supports full duplex mode. */
512 USB_SPI_FEATURE_FULL_DUPLEX_SUPPORTED = 0x01
513};
514
515struct 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
522struct 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
530struct 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
536struct 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
542union 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. Nemec3ddd6832020-07-22 03:20:18 -0700551struct usb_spi_packet_ctx {
552 union {
553 uint8_t bytes[USB_MAX_PACKET_SIZE];
554 union usb_spi_packet_v1 packet_v1;
Brian J. Nemec7b751932020-07-22 03:32:37 -0700555 union usb_spi_packet_v2 packet_v2;
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700556 };
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. Nemec1889a032020-07-22 03:16:45 -0700568struct 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
577struct 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. Nemeccea6bec2020-02-25 14:12:46 -0800586/*
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 */
592static bool retry_recovery(int error_code)
593{
594 if (error_code < 0x10000) {
Brian J. Nemec1a61f722020-05-04 20:58:06 -0700595 /*
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. Nemeccea6bec2020-02-25 14:12:46 -0800602 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. Nemec113c2b42020-07-22 03:28:55 -0700613static struct raiden_debug_spi_data *
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700614 get_raiden_data_from_context(const struct flashctx *flash)
615{
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700616 return (struct raiden_debug_spi_data *)flash->mst->spi.data;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700617}
618
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700619/*
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700620 * Read data into the receive buffer.
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700621 *
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700622 * @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 */
629static 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 */
663static 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 */
687static 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 */
715static 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. Nemec1889a032020-07-22 03:16:45 -0700744 * @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. Nemecc5d69462020-07-22 03:12:15 -0700747 * @returns Returns status code with 0 on success.
748 */
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700749static int write_command_v1(const struct raiden_debug_spi_data *ctx_data,
Brian J. Nemec1889a032020-07-22 03:16:45 -0700750 struct usb_spi_transmit_ctx *write,
751 struct usb_spi_receive_ctx *read)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800752{
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700753 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. Nemecda496dc2020-02-04 11:13:05 -0800758
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700759 /* Reset the write context to the start. */
760 write->transmit_index = 0;
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800761
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700762 fill_usb_packet(&command, write);
763 return transmit_packet(ctx_data, &command);
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800764}
765
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700766/*
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. Nemec3ddd6832020-07-22 03:20:18 -0700771 * @param ctx_data Raiden SPI config.
Brian J. Nemec1889a032020-07-22 03:16:45 -0700772 * @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. Nemecc5d69462020-07-22 03:12:15 -0700775 * @returns Returns status code with 0 on success.
776 */
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700777static int read_response_v1(const struct raiden_debug_spi_data *ctx_data,
Brian J. Nemec1889a032020-07-22 03:16:45 -0700778 struct usb_spi_transmit_ctx *write,
779 struct usb_spi_receive_ctx *read)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800780{
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700781 int status;
782 struct usb_spi_packet_ctx response;
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800783
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700784 /* 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'Callaghan5c81bf32020-01-29 14:26:55 +1100791 }
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700792 if (response.packet_v1.response.status_code) {
793 return response.packet_v1.response.status_code;
Anton Staafb2647882014-09-17 15:13:43 -0700794 }
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700795 response.header_size = offsetof(struct usb_spi_response_v1, data);
Anton Staafb2647882014-09-17 15:13:43 -0700796
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700797 status = read_usb_packet(read, &response);
798 return status;
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800799}
800
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700801/*
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 */
815static int send_command_v1(const struct flashctx *flash,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700816 unsigned int write_count,
817 unsigned int read_count,
818 const unsigned char *write_buffer,
819 unsigned char *read_buffer)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800820{
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800821 int status = -1;
822
Brian J. Nemec1889a032020-07-22 03:16:45 -0700823 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. Nemec3ddd6832020-07-22 03:20:18 -0700831 const struct raiden_debug_spi_data *ctx_data = get_raiden_data_from_context(flash);
Brian J. Nemec1889a032020-07-22 03:16:45 -0700832
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700833 if (write_count > ctx_data->max_spi_write_count) {
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700834 msg_perr("Raiden: Invalid write count\n"
835 " write count = %u\n"
836 " max write = %d\n",
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700837 write_count, ctx_data->max_spi_write_count);
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700838 return SPI_INVALID_LENGTH;
839 }
840
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700841 if (read_count > ctx_data->max_spi_read_count) {
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700842 msg_perr("Raiden: Invalid read count\n"
843 " read count = %d\n"
844 " max read = %d\n",
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700845 read_count, ctx_data->max_spi_read_count);
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700846 return SPI_INVALID_LENGTH;
847 }
848
849 for (unsigned int write_attempt = 0; write_attempt < WRITE_RETRY_ATTEMPTS;
Brian J. Nemec1118a582020-02-04 18:26:02 -0800850 write_attempt++) {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800851
Brian J. Nemec1889a032020-07-22 03:16:45 -0700852
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700853 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. Nemec1118a582020-02-04 18:26:02 -0800860
861 if (status) {
862 /* Write operation failed. */
863 msg_perr("Raiden: Write command failed\n"
Brian J. Nemec7b751932020-07-22 03:32:37 -0700864 " protocol = %u\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700865 " write count = %u\n"
866 " read count = %u\n"
Brian J. Nemec1889a032020-07-22 03:16:45 -0700867 " transmitted bytes = %zu\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700868 " write attempt = %u\n"
869 " status = 0x%05x\n",
Brian J. Nemec7b751932020-07-22 03:32:37 -0700870 ctx_data->protocol_version,
Brian J. Nemec1889a032020-07-22 03:16:45 -0700871 write_count, read_count, write_ctx.transmit_index,
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700872 write_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800873 if (!retry_recovery(status)) {
874 /* Reattempting will not result in a recovery. */
875 return status;
876 }
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700877 programmer_delay(RETRY_INTERVAL_US);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800878 continue;
879 }
Brian J. Nemec7b751932020-07-22 03:32:37 -0700880
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700881 for (unsigned int read_attempt = 0; read_attempt < READ_RETRY_ATTEMPTS;
882 read_attempt++) {
Brian J. Nemec1118a582020-02-04 18:26:02 -0800883
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700884 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. Nemec1118a582020-02-04 18:26:02 -0800895
Brian J. Nemec7b751932020-07-22 03:32:37 -0700896 /* 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 */
927static 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 */
998static 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 */
1018static 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 */
1069static 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 */
1145static 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. Nemecc5d69462020-07-22 03:12:15 -07001227 if (status) {
Brian J. Nemec1118a582020-02-04 18:26:02 -08001228 /* Read operation failed. */
1229 msg_perr("Raiden: Read response failed\n"
Brian J. Nemec7b751932020-07-22 03:32:37 -07001230 " protocol = %u\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -07001231 " write count = %u\n"
1232 " read count = %u\n"
Brian J. Nemec1889a032020-07-22 03:16:45 -07001233 " received bytes = %zu\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -07001234 " write attempt = %u\n"
1235 " read attempt = %u\n"
1236 " status = 0x%05x\n",
Brian J. Nemec7b751932020-07-22 03:32:37 -07001237 ctx_data->protocol_version,
Brian J. Nemec1889a032020-07-22 03:16:45 -07001238 write_count, read_count, read_ctx.receive_index,
Brian J. Nemecc5d69462020-07-22 03:12:15 -07001239 write_attempt + 1, read_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -08001240 if (!retry_recovery(status)) {
1241 /* Reattempting will not result in a recovery. */
1242 return status;
1243 }
Brian J. Nemec7b751932020-07-22 03:32:37 -07001244 /* Device needs to reset its transmit index. */
1245 restart_response_v2(ctx_data);
Brian J. Nemecc5d69462020-07-22 03:12:15 -07001246 programmer_delay(RETRY_INTERVAL_US);
Brian J. Nemec1118a582020-02-04 18:26:02 -08001247 }
1248 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -08001249 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -08001250 return status;
Anton Staafb2647882014-09-17 15:13:43 -07001251}
1252
Brian J. Nemec113c2b42020-07-22 03:28:55 -07001253static const struct spi_master spi_master_raiden_debug = {
Brian J. Nemecb746f822020-07-22 02:57:56 -07001254 .features = SPI_MASTER_4BA,
Brian J. Nemec113c2b42020-07-22 03:28:55 -07001255 .max_data_read = 0,
1256 .max_data_write = 0,
1257 .command = NULL,
Brian J. Nemecb746f822020-07-22 02:57:56 -07001258 .multicommand = default_spi_send_multicommand,
1259 .read = default_spi_read,
1260 .write_256 = default_spi_write_256,
Anton Staafb2647882014-09-17 15:13:43 -07001261};
1262
Anton Staaf5614e252015-03-24 14:33:33 -07001263static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
Brian J. Nemecda496dc2020-02-04 11:13:05 -08001264 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -07001265{
Anton Staaf5614e252015-03-24 14:33:33 -07001266 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
1267 direction) &&
1268 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
1269 LIBUSB_TRANSFER_TYPE_BULK));
1270}
Anton Staafd27536d2014-09-30 08:10:17 -07001271
Brian J. Nemecec15cc42020-07-22 02:30:55 -07001272static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -07001273{
1274 int i;
1275 int in_count = 0;
1276 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -07001277
Edward O'Callaghandabf7e82019-11-19 15:11:18 +11001278 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -07001279 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +11001280 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -07001281
Anton Staaf5614e252015-03-24 14:33:33 -07001282 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
1283 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +11001284 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -07001285 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
1286 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +11001287 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -07001288 }
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'Callaghandabf7e82019-11-19 15:11:18 +11001299 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
1300 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -07001301
1302 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -07001303}
1304
Brian J. Nemec113c2b42020-07-22 03:28:55 -07001305/*
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 */
1315static int configure_protocol(struct spi_master *spi_config)
1316{
Brian J. Nemec7b751932020-07-22 03:32:37 -07001317 int status = 0;
Brian J. Nemec113c2b42020-07-22 03:28:55 -07001318 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. Nemec7b751932020-07-22 03:32:37 -07001334 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. Nemec113c2b42020-07-22 03:28:55 -07001345 default:
Brian J. Nemec7b751932020-07-22 03:32:37 -07001346 msg_pdbg("Raiden: Unknown USB SPI protocol version = %u\n",
1347 ctx_data->protocol_version);
Brian J. Nemec113c2b42020-07-22 03:28:55 -07001348 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. Nemec721ac2c2020-07-23 03:30:53 -07001370static int raiden_debug_spi_shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -07001371{
Brian J. Nemec113c2b42020-07-22 03:28:55 -07001372 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. Nemec721ac2c2020-07-23 03:30:53 -07001375
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +11001376 int ret = LIBUSB(libusb_control_transfer(
Brian J. Nemec721ac2c2020-07-23 03:30:53 -07001377 ctx_data->dev->handle,
Brian J. Nemecec15cc42020-07-22 02:30:55 -07001378 LIBUSB_ENDPOINT_OUT |
1379 LIBUSB_REQUEST_TYPE_VENDOR |
1380 LIBUSB_RECIPIENT_INTERFACE,
1381 RAIDEN_DEBUG_SPI_REQ_DISABLE,
1382 0,
Brian J. Nemec721ac2c2020-07-23 03:30:53 -07001383 ctx_data->dev->interface_descriptor->bInterfaceNumber,
Brian J. Nemecec15cc42020-07-22 02:30:55 -07001384 NULL,
1385 0,
1386 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +11001387 if (ret != 0) {
1388 msg_perr("Raiden: Failed to disable SPI bridge\n");
Brian J. Nemec113c2b42020-07-22 03:28:55 -07001389 free(ctx_data);
1390 free(spi_config);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +11001391 return ret;
1392 }
Anton Staaf4589cd12015-03-23 13:36:44 -07001393
Brian J. Nemec721ac2c2020-07-23 03:30:53 -07001394 usb_device_free(ctx_data->dev);
Anton Staaf4589cd12015-03-23 13:36:44 -07001395 libusb_exit(NULL);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -07001396 free(ctx_data);
Brian J. Nemec113c2b42020-07-22 03:28:55 -07001397 free(spi_config);
Anton Staaf4589cd12015-03-23 13:36:44 -07001398
1399 return 0;
1400}
1401
Edward O'Callaghandda1e542020-02-03 12:45:01 +11001402static int get_target(void)
Anton Staafb2647882014-09-17 15:13:43 -07001403{
Mary Ruthveneafafd82016-05-03 14:33:53 -07001404 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
Mary Ruthveneafafd82016-05-03 14:33:53 -07001405
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +11001406 char *target_str = extract_programmer_param("target");
Mary Ruthveneafafd82016-05-03 14:33:53 -07001407 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'Callaghan8d2f6482019-11-19 15:42:44 +11001414 request_enable = -1;
Mary Ruthveneafafd82016-05-03 14:33:53 -07001415 }
1416 }
David Hendricks98b3c572016-11-30 01:50:08 +00001417 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -07001418
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +11001419 return request_enable;
1420}
1421
Brian J. Nemecec15cc42020-07-22 02:30:55 -07001422static 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'Callaghan8d2f6482019-11-19 15:42:44 +11001431int 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. Nemec721ac2c2020-07-23 03:30:53 -07001436 struct usb_device *device = NULL;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +11001437 int found = 0;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +11001438 int ret;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +11001439
1440 int request_enable = get_target();
Brian J. Nemec7b5ad792020-07-23 03:35:54 -07001441 if (request_enable < 0) {
1442 free(serial);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +11001443 return 1;
Brian J. Nemec7b5ad792020-07-23 03:35:54 -07001444 }
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +11001445
Anton Staaf5614e252015-03-24 14:33:33 -07001446 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -07001447
Anton Staaf5614e252015-03-24 14:33:33 -07001448 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 Staafb2647882014-09-17 15:13:43 -07001451
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +11001452 ret = LIBUSB(libusb_init(NULL));
1453 if (ret != 0) {
1454 msg_perr("Raiden: libusb_init failed\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -07001455 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +11001456 return ret;
1457 }
Anton Staaf5614e252015-03-24 14:33:33 -07001458
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +11001459 ret = usb_device_find(&match, &current);
1460 if (ret != 0) {
1461 msg_perr("Raiden: Failed to find devices\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -07001462 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +11001463 return ret;
1464 }
Anton Staaf5614e252015-03-24 14:33:33 -07001465
Brian J. Nemec721ac2c2020-07-23 03:30:53 -07001466 uint8_t in_endpoint = 0;
1467 uint8_t out_endpoint = 0;
David Hendricks5c79a492016-06-14 20:56:36 -07001468 while (current) {
1469 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -07001470
Edward O'Callaghandabf7e82019-11-19 15:11:18 +11001471 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -07001472 msg_pdbg("Raiden: Failed to find valid endpoints on device");
1473 usb_device_show(" ", current);
1474 goto loop_end;
David Hendricks5c79a492016-06-14 20:56:36 -07001475 }
Anton Staaf5614e252015-03-24 14:33:33 -07001476
David Hendricks5c79a492016-06-14 20:56:36 -07001477 if (usb_device_claim(device)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -07001478 msg_pdbg("Raiden: Failed to claim USB device");
1479 usb_device_show(" ", current);
1480 goto loop_end;
David Hendricks5c79a492016-06-14 20:56:36 -07001481 }
Anton Staaf5614e252015-03-24 14:33:33 -07001482
David Hendricks5c79a492016-06-14 20:56:36 -07001483 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 Staaf5614e252015-03-24 14:33:33 -07001490
David Hendricks5c79a492016-06-14 20:56:36 -07001491 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. Nemecec15cc42020-07-22 02:30:55 -07001499 descriptor.iSerialNumber,
1500 dev_serial,
1501 sizeof(dev_serial));
David Hendricks5c79a492016-06-14 20:56:36 -07001502 if (rc < 0) {
1503 LIBUSB(rc);
1504 } else {
1505 if (strcmp(serial, (char *)dev_serial)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -07001506 msg_pdbg("Raiden: Serial number %s did not match device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -07001507 usb_device_show(" ", current);
1508 } else {
Brian J. Nemecec15cc42020-07-22 02:30:55 -07001509 msg_pinfo("Raiden: Serial number %s matched device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -07001510 usb_device_show(" ", current);
1511 found = 1;
1512 }
1513 }
1514 }
1515
1516loop_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. Nemeca88d96e2020-07-23 03:22:57 -07001525 free(serial);
David Hendricks98b3c572016-11-30 01:50:08 +00001526 return 1;
Anton Staafb2647882014-09-17 15:13:43 -07001527 }
1528
Brian J. Nemecec15cc42020-07-22 02:30:55 -07001529 free_dev_list(&current);
Anton Staafb4661ee2014-10-21 11:24:36 -07001530
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +11001531 ret = LIBUSB(libusb_control_transfer(
Brian J. Nemecec15cc42020-07-22 02:30:55 -07001532 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'Callaghan5c81bf32020-01-29 14:26:55 +11001542 if (ret != 0) {
1543 msg_perr("Raiden: Failed to enable SPI bridge\n");
1544 return ret;
1545 }
Anton Staaf4589cd12015-03-23 13:36:44 -07001546
Keith Short8453b552020-02-03 18:10:14 -07001547 /*
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. Nemec113c2b42020-07-22 03:28:55 -07001556 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. Nemec721ac2c2020-07-23 03:30:53 -07001561 struct raiden_debug_spi_data *data = calloc(1, sizeof(struct raiden_debug_spi_data));
1562 if (!data) {
Brian J. Nemec113c2b42020-07-22 03:28:55 -07001563 free(spi_config);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -07001564 msg_perr("Unable to allocate space for extra SPI master data.\n");
1565 return SPI_GENERIC_ERROR;
1566 }
1567
Brian J. Nemec113c2b42020-07-22 03:28:55 -07001568 memcpy(spi_config, &spi_master_raiden_debug, sizeof(struct spi_master));
1569
Brian J. Nemec721ac2c2020-07-23 03:30:53 -07001570 data->dev = device;
1571 data->in_ep = in_endpoint;
1572 data->out_ep = out_endpoint;
1573
Brian J. Nemec113c2b42020-07-22 03:28:55 -07001574 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. Nemec721ac2c2020-07-23 03:30:53 -07001590
Brian J. Nemec113c2b42020-07-22 03:28:55 -07001591 register_spi_master(spi_config);
1592 register_shutdown(raiden_debug_spi_shutdown, spi_config);
Anton Staafb2647882014-09-17 15:13:43 -07001593
David Hendricks98b3c572016-11-30 01:50:08 +00001594 return 0;
Anton Staafb2647882014-09-17 15:13:43 -07001595}