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 | * |
| 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 |
| 49 | */ |
| 50 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 51 | #include "programmer.h" |
| 52 | #include "spi.h" |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 53 | #include "usb_device.h" |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 54 | |
| 55 | #include <libusb.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 56 | #include <stdio.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 57 | #include <stdlib.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 58 | #include <string.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 59 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 60 | #define GOOGLE_VID 0x18D1 |
| 61 | #define GOOGLE_RAIDEN_SPI_SUBCLASS 0x51 |
| 62 | #define GOOGLE_RAIDEN_SPI_PROTOCOL 0x01 |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 63 | |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 64 | enum raiden_debug_spi_request { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 65 | RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000, |
| 66 | RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001, |
| 67 | RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002, |
| 68 | RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003, |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 71 | #define PACKET_HEADER_SIZE 2 |
| 72 | #define MAX_PACKET_SIZE 64 |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * This timeout is so large because the Raiden SPI timeout is 800ms. |
| 76 | */ |
| 77 | #define TRANSFER_TIMEOUT_MS 1000 |
| 78 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 79 | struct usb_device *device = NULL; |
| 80 | uint8_t in_endpoint = 0; |
| 81 | uint8_t out_endpoint = 0; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 82 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 83 | static int send_command(const struct flashctx *flash, |
| 84 | unsigned int write_count, |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 85 | unsigned int read_count, |
| 86 | const unsigned char *write_buffer, |
| 87 | unsigned char *read_buffer) |
| 88 | { |
| 89 | uint8_t buffer[MAX_PACKET_SIZE]; |
| 90 | int transferred; |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 91 | int ret; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 92 | |
| 93 | if (write_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) { |
| 94 | msg_perr("Raiden: invalid write_count of %d\n", write_count); |
| 95 | return SPI_INVALID_LENGTH; |
| 96 | } |
| 97 | |
| 98 | if (read_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) { |
| 99 | msg_perr("Raiden: invalid read_count of %d\n", read_count); |
| 100 | return SPI_INVALID_LENGTH; |
| 101 | } |
| 102 | |
| 103 | buffer[0] = write_count; |
| 104 | buffer[1] = read_count; |
| 105 | |
| 106 | memcpy(buffer + PACKET_HEADER_SIZE, write_buffer, write_count); |
| 107 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 108 | ret = LIBUSB(libusb_bulk_transfer(device->handle, |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 109 | out_endpoint, |
| 110 | buffer, |
| 111 | write_count + PACKET_HEADER_SIZE, |
| 112 | &transferred, |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 113 | TRANSFER_TIMEOUT_MS)); |
| 114 | if (ret != 0) { |
| 115 | msg_perr("Raiden: OUT transfer failed\n" |
| 116 | " write_count = %d\n" |
| 117 | " read_count = %d\n", |
| 118 | write_count, read_count); |
| 119 | return ret; |
| 120 | } |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 121 | |
| 122 | if (transferred != write_count + PACKET_HEADER_SIZE) { |
| 123 | msg_perr("Raiden: Write failure (wrote %d, expected %d)\n", |
| 124 | transferred, write_count + PACKET_HEADER_SIZE); |
| 125 | return 0x10001; |
| 126 | } |
| 127 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 128 | ret = LIBUSB(libusb_bulk_transfer(device->handle, |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 129 | in_endpoint, |
| 130 | buffer, |
| 131 | read_count + PACKET_HEADER_SIZE, |
| 132 | &transferred, |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 133 | TRANSFER_TIMEOUT_MS)); |
| 134 | if (ret != 0) { |
| 135 | msg_perr("Raiden: IN transfer failed\n" |
| 136 | " write_count = %d\n" |
| 137 | " read_count = %d\n", |
| 138 | write_count, read_count); |
| 139 | return ret; |
| 140 | } |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 141 | |
| 142 | if (transferred != read_count + PACKET_HEADER_SIZE) { |
| 143 | msg_perr("Raiden: Read failure (read %d, expected %d)\n", |
| 144 | transferred, read_count + PACKET_HEADER_SIZE); |
| 145 | return 0x10002; |
| 146 | } |
| 147 | |
| 148 | memcpy(read_buffer, buffer + PACKET_HEADER_SIZE, read_count); |
| 149 | |
| 150 | return buffer[0] | (buffer[1] << 8); |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Unfortunately there doesn't seem to be a way to specify the maximum number |
| 155 | * of bytes that your SPI device can read/write, these values are the maximum |
Duncan Laurie | 537fd1d | 2018-10-05 10:53:20 -0700 | [diff] [blame] | 156 | * data chunk size that flashrom will package up with an additional five bytes |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 157 | * of command for the flash device, resulting in a 62 byte packet, that we then |
| 158 | * add two bytes to in either direction, making our way up to the 64 byte |
| 159 | * maximum USB packet size for the device. |
| 160 | * |
| 161 | * The largest command that flashrom generates is the byte program command, so |
Duncan Laurie | 537fd1d | 2018-10-05 10:53:20 -0700 | [diff] [blame] | 162 | * we use that command header maximum size here. |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 163 | */ |
| 164 | #define MAX_DATA_SIZE (MAX_PACKET_SIZE - \ |
| 165 | PACKET_HEADER_SIZE - \ |
Duncan Laurie | 537fd1d | 2018-10-05 10:53:20 -0700 | [diff] [blame] | 166 | JEDEC_BYTE_PROGRAM_OUTSIZE) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 167 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 168 | static const struct spi_master spi_master_raiden_debug = { |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 169 | .type = SPI_CONTROLLER_RAIDEN_DEBUG, |
Mathew King | bd9670f | 2019-08-07 14:17:48 -0600 | [diff] [blame] | 170 | .features = SPI_MASTER_4BA, |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 171 | .max_data_read = MAX_DATA_SIZE, |
| 172 | .max_data_write = MAX_DATA_SIZE, |
| 173 | .command = send_command, |
| 174 | .multicommand = default_spi_send_multicommand, |
| 175 | .read = default_spi_read, |
| 176 | .write_256 = default_spi_write_256, |
| 177 | }; |
| 178 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 179 | static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor, |
| 180 | enum libusb_endpoint_direction direction) |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 181 | { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 182 | return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == |
| 183 | direction) && |
| 184 | ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == |
| 185 | LIBUSB_TRANSFER_TYPE_BULK)); |
| 186 | } |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 187 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 188 | 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] | 189 | { |
| 190 | int i; |
| 191 | int in_count = 0; |
| 192 | int out_count = 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 193 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 194 | for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 195 | struct libusb_endpoint_descriptor const *endpoint = |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 196 | &dev->interface_descriptor->endpoint[i]; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 197 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 198 | if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) { |
| 199 | in_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 200 | *in_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 201 | } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) { |
| 202 | out_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 203 | *out_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | if (in_count != 1 || out_count != 1) { |
| 208 | msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n" |
| 209 | " found %d IN and %d OUT endpoints\n", |
| 210 | in_count, |
| 211 | out_count); |
| 212 | return 1; |
| 213 | } |
| 214 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 215 | msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep); |
| 216 | msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep); |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 217 | |
| 218 | return 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 219 | } |
| 220 | |
David Hendricks | 93784b4 | 2016-08-09 17:00:38 -0700 | [diff] [blame] | 221 | static int shutdown(void * data) |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 222 | { |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 223 | int ret = LIBUSB(libusb_control_transfer( |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 224 | device->handle, |
| 225 | LIBUSB_ENDPOINT_OUT | |
| 226 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 227 | LIBUSB_RECIPIENT_INTERFACE, |
| 228 | RAIDEN_DEBUG_SPI_REQ_DISABLE, |
| 229 | 0, |
| 230 | device->interface_descriptor->bInterfaceNumber, |
| 231 | NULL, |
| 232 | 0, |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 233 | TRANSFER_TIMEOUT_MS)); |
| 234 | if (ret != 0) { |
| 235 | msg_perr("Raiden: Failed to disable SPI bridge\n"); |
| 236 | return ret; |
| 237 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 238 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 239 | usb_device_free(device); |
| 240 | |
| 241 | device = NULL; |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 242 | libusb_exit(NULL); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 247 | static int get_target() |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 248 | { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 249 | int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 250 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 251 | char *target_str = extract_programmer_param("target"); |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 252 | if (target_str) { |
| 253 | if (!strcasecmp(target_str, "ap")) |
| 254 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP; |
| 255 | else if (!strcasecmp(target_str, "ec")) |
| 256 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC; |
| 257 | else { |
| 258 | msg_perr("Invalid target: %s\n", target_str); |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 259 | request_enable = -1; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 262 | free(target_str); |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 263 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 264 | return request_enable; |
| 265 | } |
| 266 | |
| 267 | int raiden_debug_spi_init(void) |
| 268 | { |
| 269 | struct usb_match match; |
| 270 | char *serial = extract_programmer_param("serial"); |
| 271 | struct usb_device *current; |
| 272 | int found = 0; |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 273 | int ret; |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 274 | |
| 275 | int request_enable = get_target(); |
| 276 | if (request_enable < 0) |
| 277 | return 1; |
| 278 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 279 | usb_match_init(&match); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 280 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 281 | usb_match_value_default(&match.vid, GOOGLE_VID); |
| 282 | usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC); |
| 283 | usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS); |
| 284 | usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 285 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 286 | ret = LIBUSB(libusb_init(NULL)); |
| 287 | if (ret != 0) { |
| 288 | msg_perr("Raiden: libusb_init failed\n"); |
| 289 | return ret; |
| 290 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 291 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 292 | ret = usb_device_find(&match, ¤t); |
| 293 | if (ret != 0) { |
| 294 | msg_perr("Raiden: Failed to find devices\n"); |
| 295 | return ret; |
| 296 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 297 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 298 | while (current) { |
| 299 | device = current; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 300 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 301 | if (find_endpoints(device, &in_endpoint, &out_endpoint)) { |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 302 | msg_pdbg("Raiden: Failed to find valid endpoints on device"); |
| 303 | usb_device_show(" ", current); |
| 304 | goto loop_end; |
| 305 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 306 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 307 | if (usb_device_claim(device)) { |
| 308 | msg_pdbg("Raiden: Failed to claim USB device"); |
| 309 | usb_device_show(" ", current); |
| 310 | goto loop_end; |
| 311 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 312 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 313 | if (!serial) { |
| 314 | found = 1; |
| 315 | goto loop_end; |
| 316 | } else { |
| 317 | unsigned char dev_serial[32]; |
| 318 | struct libusb_device_descriptor descriptor; |
| 319 | int rc; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 320 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 321 | memset(dev_serial, 0, sizeof(dev_serial)); |
| 322 | |
| 323 | if (libusb_get_device_descriptor(device->device, &descriptor)) { |
| 324 | msg_pdbg("USB: Failed to get device descriptor.\n"); |
| 325 | goto loop_end; |
| 326 | } |
| 327 | |
| 328 | rc = libusb_get_string_descriptor_ascii(device->handle, |
| 329 | descriptor.iSerialNumber, |
| 330 | dev_serial, |
| 331 | sizeof(dev_serial)); |
| 332 | if (rc < 0) { |
| 333 | LIBUSB(rc); |
| 334 | } else { |
| 335 | if (strcmp(serial, (char *)dev_serial)) { |
| 336 | msg_pdbg("Raiden: Serial number %s did not match device", serial); |
| 337 | usb_device_show(" ", current); |
| 338 | } else { |
| 339 | msg_pinfo("Raiden: Serial number %s matched device", serial); |
| 340 | usb_device_show(" ", current); |
| 341 | found = 1; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | loop_end: |
| 347 | if (found) |
| 348 | break; |
| 349 | else |
| 350 | current = usb_device_free(current); |
| 351 | } |
| 352 | |
| 353 | if (!device || !found) { |
| 354 | msg_perr("Raiden: No usable device found.\n"); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 355 | return 1; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 356 | } |
| 357 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 358 | /* free devices we don't care about */ |
| 359 | current = current->next; |
| 360 | while (current) |
| 361 | current = usb_device_free(current); |
Anton Staaf | b4661ee | 2014-10-21 11:24:36 -0700 | [diff] [blame] | 362 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 363 | ret = LIBUSB(libusb_control_transfer( |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 364 | device->handle, |
| 365 | LIBUSB_ENDPOINT_OUT | |
| 366 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 367 | LIBUSB_RECIPIENT_INTERFACE, |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 368 | request_enable, |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 369 | 0, |
| 370 | device->interface_descriptor->bInterfaceNumber, |
| 371 | NULL, |
| 372 | 0, |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 373 | TRANSFER_TIMEOUT_MS)); |
| 374 | if (ret != 0) { |
| 375 | msg_perr("Raiden: Failed to enable SPI bridge\n"); |
| 376 | return ret; |
| 377 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 378 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 379 | register_spi_master(&spi_master_raiden_debug); |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 380 | register_shutdown(shutdown, NULL); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 381 | |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 382 | return 0; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 383 | } |