blob: 04fbaf33e0548387857ce852e623cdea19e4ae25 [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 *
45 * The protocol for the USB-SPI bridge is documented in the following file in
46 * that respository:
47 *
48 * chip/stm32/usb_spi.c
Brian J. Nemecda496dc2020-02-04 11:13:05 -080049 *
50 * Version 1:
51 * SPI transactions of up to 62B in each direction with every command having
52 * a response. The initial packet from host contains a 2B header indicating
53 * write and read counts with an optional payload length equal to the write
54 * count. The device will respond with a message that reports the 2B status
55 * code and an optional payload response length equal to read count.
56 *
57 * Message Format:
58 *
59 * Command Packet:
60 * +------------------+-----------------+------------------------+
61 * | write count : 1B | read count : 1B | write payload : <= 62B |
62 * +------------------+-----------------+------------------------+
63 *
64 * write count: 1 byte, zero based count of bytes to write
65 *
66 * read count: 1 byte, zero based count of bytes to read
67 *
68 * write payload: Up to 62 bytes of data to write to SPI, the total
69 * length of all TX packets must match write count.
70 * Due to data alignment constraints, this must be an
71 * even number of bytes unless this is the final packet.
72 *
73 * Response Packet:
74 * +-------------+-----------------------+
75 * | status : 2B | read payload : <= 62B |
76 * +-------------+-----------------------+
77 *
78 * status: 2 byte status
79 * 0x0000: Success
80 * 0x0001: SPI timeout
81 * 0x0002: Busy, try again
82 * This can happen if someone else has acquired the shared memory
83 * buffer that the SPI driver uses as /dev/null
84 * 0x0003: Write count invalid (V1 > 62B)
85 * 0x0004: Read count invalid (V1 > 62B)
86 * 0x0005: The SPI bridge is disabled.
87 * 0x8000: Unknown error mask
88 * The bottom 15 bits will contain the bottom 15 bits from the EC
89 * error code.
90 *
91 * read payload: Up to 62 bytes of data read from SPI, the total
92 * length of all RX packets must match read count
93 * unless an error status was returned. Due to data
94 * alignment constraints, this must be a even number
95 * of bytes unless this is the final packet.
Anton Staafb2647882014-09-17 15:13:43 -070096 */
97
Anton Staafb2647882014-09-17 15:13:43 -070098#include "programmer.h"
99#include "spi.h"
Anton Staaf5614e252015-03-24 14:33:33 -0700100#include "usb_device.h"
Anton Staafb2647882014-09-17 15:13:43 -0700101
102#include <libusb.h>
Anton Staaf5614e252015-03-24 14:33:33 -0700103#include <stdio.h>
Anton Staafb2647882014-09-17 15:13:43 -0700104#include <stdlib.h>
Anton Staaf5614e252015-03-24 14:33:33 -0700105#include <string.h>
Keith Short8453b552020-02-03 18:10:14 -0700106#include <unistd.h>
Anton Staafb2647882014-09-17 15:13:43 -0700107
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800108#define GOOGLE_VID (0x18D1)
109#define GOOGLE_RAIDEN_SPI_SUBCLASS (0x51)
110#define GOOGLE_RAIDEN_SPI_PROTOCOL (0x01)
Anton Staafb2647882014-09-17 15:13:43 -0700111
Anton Staaf4589cd12015-03-23 13:36:44 -0700112enum raiden_debug_spi_request {
Mary Ruthveneafafd82016-05-03 14:33:53 -0700113 RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000,
114 RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001,
115 RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002,
116 RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003,
Anton Staaf4589cd12015-03-23 13:36:44 -0700117};
118
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800119#define PACKET_HEADER_SIZE (2)
120#define MAX_PACKET_SIZE (64)
121#define PAYLOAD_SIZE (MAX_PACKET_SIZE - PACKET_HEADER_SIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700122
Brian J. Nemec1118a582020-02-04 18:26:02 -0800123#define WRITE_RETY_ATTEMPTS (3)
124#define READ_RETY_ATTEMPTS (3)
125#define RETY_INTERVAL_US (100 * 1000)
126
Anton Staafb2647882014-09-17 15:13:43 -0700127/*
128 * This timeout is so large because the Raiden SPI timeout is 800ms.
129 */
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800130#define TRANSFER_TIMEOUT_MS (1000)
Anton Staafb2647882014-09-17 15:13:43 -0700131
Anton Staaf5614e252015-03-24 14:33:33 -0700132struct usb_device *device = NULL;
133uint8_t in_endpoint = 0;
134uint8_t out_endpoint = 0;
Anton Staafb2647882014-09-17 15:13:43 -0700135
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800136typedef struct {
137 int8_t write_count;
138 /* -1 Indicates readback all on halfduplex compliant devices. */
139 int8_t read_count;
140 uint8_t data[PAYLOAD_SIZE];
141} __attribute__((packed)) usb_spi_command_t;
Anton Staafb2647882014-09-17 15:13:43 -0700142
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800143typedef struct {
144 uint16_t status_code;
145 uint8_t data[PAYLOAD_SIZE];
146} __attribute__((packed)) usb_spi_response_t;
147
148static int write_command(const struct flashctx *flash,
149 unsigned int write_count,
150 unsigned int read_count,
151 const unsigned char *write_buffer,
152 unsigned char *read_buffer)
153{
154
155 int transferred;
156 int ret;
157 usb_spi_command_t command_packet;
158
159 if (write_count > PAYLOAD_SIZE) {
Anton Staafb2647882014-09-17 15:13:43 -0700160 msg_perr("Raiden: invalid write_count of %d\n", write_count);
161 return SPI_INVALID_LENGTH;
162 }
163
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800164 if (read_count > PAYLOAD_SIZE) {
Anton Staafb2647882014-09-17 15:13:43 -0700165 msg_perr("Raiden: invalid read_count of %d\n", read_count);
166 return SPI_INVALID_LENGTH;
167 }
168
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800169 command_packet.write_count = write_count;
170 command_packet.read_count = read_count;
Anton Staafb2647882014-09-17 15:13:43 -0700171
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800172 memcpy(command_packet.data, write_buffer, write_count);
Anton Staafb2647882014-09-17 15:13:43 -0700173
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100174 ret = LIBUSB(libusb_bulk_transfer(device->handle,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800175 out_endpoint,
176 (void*)&command_packet,
177 write_count + PACKET_HEADER_SIZE,
178 &transferred,
179 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100180 if (ret != 0) {
181 msg_perr("Raiden: OUT transfer failed\n"
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800182 " write_count = %d\n"
183 " read_count = %d\n",
184 write_count, read_count);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100185 return ret;
186 }
Anton Staafb2647882014-09-17 15:13:43 -0700187
188 if (transferred != write_count + PACKET_HEADER_SIZE) {
189 msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
190 transferred, write_count + PACKET_HEADER_SIZE);
191 return 0x10001;
192 }
193
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800194 return 0;
195}
196
197static int read_response(const struct flashctx *flash,
198 unsigned int write_count,
199 unsigned int read_count,
200 const unsigned char *write_buffer,
201 unsigned char *read_buffer)
202{
203
204 int transferred;
205 int ret;
206 usb_spi_response_t response_packet;
207
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100208 ret = LIBUSB(libusb_bulk_transfer(device->handle,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800209 in_endpoint,
210 (void*)&response_packet,
211 read_count + PACKET_HEADER_SIZE,
212 &transferred,
213 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100214 if (ret != 0) {
215 msg_perr("Raiden: IN transfer failed\n"
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800216 " write_count = %d\n"
217 " read_count = %d\n",
218 write_count, read_count);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100219 return ret;
220 }
Anton Staafb2647882014-09-17 15:13:43 -0700221
222 if (transferred != read_count + PACKET_HEADER_SIZE) {
223 msg_perr("Raiden: Read failure (read %d, expected %d)\n",
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800224 transferred, read_count + PACKET_HEADER_SIZE);
Anton Staafb2647882014-09-17 15:13:43 -0700225 return 0x10002;
226 }
227
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800228 memcpy(read_buffer, response_packet.data, read_count);
Anton Staafb2647882014-09-17 15:13:43 -0700229
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800230 return response_packet.status_code;
231}
232
233static int send_command(const struct flashctx *flash,
234 unsigned int write_count,
235 unsigned int read_count,
236 const unsigned char *write_buffer,
237 unsigned char *read_buffer)
238{
239
240 int status = -1;
241
Brian J. Nemec1118a582020-02-04 18:26:02 -0800242 for (int write_attempt = 0; write_attempt < WRITE_RETY_ATTEMPTS;
243 write_attempt++) {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800244
Brian J. Nemec1118a582020-02-04 18:26:02 -0800245 status = write_command(flash, write_count, read_count,
246 write_buffer, read_buffer);
247
248 if (status) {
249 /* Write operation failed. */
250 msg_perr("Raiden: Write command failed\n"
251 "Write attempt = %d\n"
252 "status = %d\n",
253 write_attempt + 1, status);
254 programmer_delay(RETY_INTERVAL_US);
255 continue;
256 }
257 for (int read_attempt = 0; read_attempt < READ_RETY_ATTEMPTS;
258 read_attempt++) {
259
260 status = read_response(flash, write_count, read_count,
261 write_buffer, read_buffer);
262
263 if (status != 0) {
264 /* Read operation failed. */
265 msg_perr("Raiden: Read response failed\n"
266 "Write attempt = %d\n"
267 "Read attempt = %d\n"
268 "status = %d\n",
269 write_attempt + 1, read_attempt + 1, status);
270 programmer_delay(RETY_INTERVAL_US);
271 } else {
272 /* We were successful at performing the SPI transfer. */
273 return status;
274 }
275 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800276 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800277 return status;
Anton Staafb2647882014-09-17 15:13:43 -0700278}
279
280/*
281 * Unfortunately there doesn't seem to be a way to specify the maximum number
282 * of bytes that your SPI device can read/write, these values are the maximum
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700283 * data chunk size that flashrom will package up with an additional five bytes
Anton Staafb2647882014-09-17 15:13:43 -0700284 * of command for the flash device, resulting in a 62 byte packet, that we then
285 * add two bytes to in either direction, making our way up to the 64 byte
286 * maximum USB packet size for the device.
287 *
288 * The largest command that flashrom generates is the byte program command, so
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700289 * we use that command header maximum size here.
Anton Staafb2647882014-09-17 15:13:43 -0700290 */
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800291#define MAX_DATA_SIZE (PAYLOAD_SIZE - JEDEC_BYTE_PROGRAM_OUTSIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700292
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100293static const struct spi_master spi_master_raiden_debug = {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800294 .type = SPI_CONTROLLER_RAIDEN_DEBUG,
295 .features = SPI_MASTER_4BA,
296 .max_data_read = MAX_DATA_SIZE,
297 .max_data_write = MAX_DATA_SIZE,
298 .command = send_command,
299 .multicommand = default_spi_send_multicommand,
300 .read = default_spi_read,
301 .write_256 = default_spi_write_256,
Anton Staafb2647882014-09-17 15:13:43 -0700302};
303
Anton Staaf5614e252015-03-24 14:33:33 -0700304static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800305 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700306{
Anton Staaf5614e252015-03-24 14:33:33 -0700307 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
308 direction) &&
309 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
310 LIBUSB_TRANSFER_TYPE_BULK));
311}
Anton Staafd27536d2014-09-30 08:10:17 -0700312
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800313static int find_endpoints(struct usb_device *dev, uint8_t *in_ep,
314 uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -0700315{
316 int i;
317 int in_count = 0;
318 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700319
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100320 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -0700321 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100322 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700323
Anton Staaf5614e252015-03-24 14:33:33 -0700324 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
325 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100326 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700327 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
328 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100329 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700330 }
331 }
332
333 if (in_count != 1 || out_count != 1) {
334 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
335 " found %d IN and %d OUT endpoints\n",
336 in_count,
337 out_count);
338 return 1;
339 }
340
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100341 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
342 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -0700343
344 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700345}
346
David Hendricks93784b42016-08-09 17:00:38 -0700347static int shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700348{
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100349 int ret = LIBUSB(libusb_control_transfer(
Anton Staaf5614e252015-03-24 14:33:33 -0700350 device->handle,
351 LIBUSB_ENDPOINT_OUT |
352 LIBUSB_REQUEST_TYPE_VENDOR |
353 LIBUSB_RECIPIENT_INTERFACE,
354 RAIDEN_DEBUG_SPI_REQ_DISABLE,
355 0,
356 device->interface_descriptor->bInterfaceNumber,
357 NULL,
358 0,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100359 TRANSFER_TIMEOUT_MS));
360 if (ret != 0) {
361 msg_perr("Raiden: Failed to disable SPI bridge\n");
362 return ret;
363 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700364
Anton Staaf5614e252015-03-24 14:33:33 -0700365 usb_device_free(device);
366
367 device = NULL;
Anton Staaf4589cd12015-03-23 13:36:44 -0700368 libusb_exit(NULL);
369
370 return 0;
371}
372
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100373static int get_target()
Anton Staafb2647882014-09-17 15:13:43 -0700374{
Mary Ruthveneafafd82016-05-03 14:33:53 -0700375 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700376
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100377 char *target_str = extract_programmer_param("target");
Mary Ruthveneafafd82016-05-03 14:33:53 -0700378 if (target_str) {
379 if (!strcasecmp(target_str, "ap"))
380 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
381 else if (!strcasecmp(target_str, "ec"))
382 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
383 else {
384 msg_perr("Invalid target: %s\n", target_str);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100385 request_enable = -1;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700386 }
387 }
David Hendricks98b3c572016-11-30 01:50:08 +0000388 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700389
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100390 return request_enable;
391}
392
393int raiden_debug_spi_init(void)
394{
395 struct usb_match match;
396 char *serial = extract_programmer_param("serial");
397 struct usb_device *current;
398 int found = 0;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100399 int ret;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100400
401 int request_enable = get_target();
402 if (request_enable < 0)
403 return 1;
404
Anton Staaf5614e252015-03-24 14:33:33 -0700405 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700406
Anton Staaf5614e252015-03-24 14:33:33 -0700407 usb_match_value_default(&match.vid, GOOGLE_VID);
408 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
409 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
410 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL);
Anton Staafb2647882014-09-17 15:13:43 -0700411
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100412 ret = LIBUSB(libusb_init(NULL));
413 if (ret != 0) {
414 msg_perr("Raiden: libusb_init failed\n");
415 return ret;
416 }
Anton Staaf5614e252015-03-24 14:33:33 -0700417
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100418 ret = usb_device_find(&match, &current);
419 if (ret != 0) {
420 msg_perr("Raiden: Failed to find devices\n");
421 return ret;
422 }
Anton Staaf5614e252015-03-24 14:33:33 -0700423
David Hendricks5c79a492016-06-14 20:56:36 -0700424 while (current) {
425 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -0700426
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100427 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
David Hendricks5c79a492016-06-14 20:56:36 -0700428 msg_pdbg("Raiden: Failed to find valid endpoints on device");
429 usb_device_show(" ", current);
430 goto loop_end;
431 }
Anton Staaf5614e252015-03-24 14:33:33 -0700432
David Hendricks5c79a492016-06-14 20:56:36 -0700433 if (usb_device_claim(device)) {
434 msg_pdbg("Raiden: Failed to claim USB device");
435 usb_device_show(" ", current);
436 goto loop_end;
437 }
Anton Staaf5614e252015-03-24 14:33:33 -0700438
David Hendricks5c79a492016-06-14 20:56:36 -0700439 if (!serial) {
440 found = 1;
441 goto loop_end;
442 } else {
443 unsigned char dev_serial[32];
444 struct libusb_device_descriptor descriptor;
445 int rc;
Anton Staaf5614e252015-03-24 14:33:33 -0700446
David Hendricks5c79a492016-06-14 20:56:36 -0700447 memset(dev_serial, 0, sizeof(dev_serial));
448
449 if (libusb_get_device_descriptor(device->device, &descriptor)) {
450 msg_pdbg("USB: Failed to get device descriptor.\n");
451 goto loop_end;
452 }
453
454 rc = libusb_get_string_descriptor_ascii(device->handle,
455 descriptor.iSerialNumber,
456 dev_serial,
457 sizeof(dev_serial));
458 if (rc < 0) {
459 LIBUSB(rc);
460 } else {
461 if (strcmp(serial, (char *)dev_serial)) {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800462 msg_pdbg("Raiden: Serial number %s did not match device",
463 serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700464 usb_device_show(" ", current);
465 } else {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800466 msg_pinfo("Raiden: Serial number %s matched device",
467 serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700468 usb_device_show(" ", current);
469 found = 1;
470 }
471 }
472 }
473
474loop_end:
475 if (found)
476 break;
477 else
478 current = usb_device_free(current);
479 }
480
481 if (!device || !found) {
482 msg_perr("Raiden: No usable device found.\n");
David Hendricks98b3c572016-11-30 01:50:08 +0000483 return 1;
Anton Staafb2647882014-09-17 15:13:43 -0700484 }
485
David Hendricks5c79a492016-06-14 20:56:36 -0700486 /* free devices we don't care about */
487 current = current->next;
488 while (current)
489 current = usb_device_free(current);
Anton Staafb4661ee2014-10-21 11:24:36 -0700490
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100491 ret = LIBUSB(libusb_control_transfer(
Anton Staaf5614e252015-03-24 14:33:33 -0700492 device->handle,
493 LIBUSB_ENDPOINT_OUT |
494 LIBUSB_REQUEST_TYPE_VENDOR |
495 LIBUSB_RECIPIENT_INTERFACE,
Mary Ruthveneafafd82016-05-03 14:33:53 -0700496 request_enable,
Anton Staaf5614e252015-03-24 14:33:33 -0700497 0,
498 device->interface_descriptor->bInterfaceNumber,
499 NULL,
500 0,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100501 TRANSFER_TIMEOUT_MS));
502 if (ret != 0) {
503 msg_perr("Raiden: Failed to enable SPI bridge\n");
504 return ret;
505 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700506
Keith Short8453b552020-02-03 18:10:14 -0700507 /*
508 * Allow for power to settle on the AP and EC flash devices.
509 * Load switches can have a 1-3 ms turn on time, and SPI flash devices
510 * can require up to 10 ms from power on to the first write.
511 */
512 if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) ||
513 (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC))
514 usleep(50 * 1000);
515
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100516 register_spi_master(&spi_master_raiden_debug);
Anton Staaf4589cd12015-03-23 13:36:44 -0700517 register_shutdown(shutdown, NULL);
Anton Staafb2647882014-09-17 15:13:43 -0700518
David Hendricks98b3c572016-11-30 01:50:08 +0000519 return 0;
Anton Staafb2647882014-09-17 15:13:43 -0700520}