Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Edward O'Callaghan | e413f73 | 2020-01-29 15:22:32 +1100 | [diff] [blame] | 4 | * Copyright (C) 2020, Google Inc. All rights reserved. |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 5 | * |
Edward O'Callaghan | e413f73 | 2020-01-29 15:22:32 +1100 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 10 | * |
Edward O'Callaghan | e413f73 | 2020-01-29 15:22:32 +1100 | [diff] [blame] | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "programmer.h" |
| 18 | #include "spi.h" |
| 19 | #include "usb_device.h" |
| 20 | |
| 21 | #include <assert.h> |
| 22 | #include <libusb.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | /* |
| 28 | * Possibly extract a programmer parameter and use it to initialize the given |
| 29 | * match value structure. |
| 30 | */ |
| 31 | static void usb_match_value_init(struct usb_match_value *match, |
| 32 | char const *parameter) |
| 33 | { |
| 34 | char *string = extract_programmer_param(parameter); |
| 35 | |
| 36 | match->name = parameter; |
| 37 | |
| 38 | if (string) { |
| 39 | match->set = 1; |
| 40 | match->value = strtol(string, NULL, 0); |
| 41 | } else { |
| 42 | match->set = 0; |
| 43 | } |
| 44 | |
| 45 | free(string); |
| 46 | } |
| 47 | |
| 48 | #define USB_MATCH_VALUE_INIT(NAME) \ |
| 49 | usb_match_value_init(&match->NAME, #NAME) |
| 50 | |
| 51 | void usb_match_init(struct usb_match *match) |
| 52 | { |
| 53 | USB_MATCH_VALUE_INIT(vid); |
| 54 | USB_MATCH_VALUE_INIT(pid); |
| 55 | USB_MATCH_VALUE_INIT(bus); |
| 56 | USB_MATCH_VALUE_INIT(address); |
| 57 | USB_MATCH_VALUE_INIT(config); |
| 58 | USB_MATCH_VALUE_INIT(interface); |
| 59 | USB_MATCH_VALUE_INIT(altsetting); |
| 60 | USB_MATCH_VALUE_INIT(class); |
| 61 | USB_MATCH_VALUE_INIT(subclass); |
| 62 | USB_MATCH_VALUE_INIT(protocol); |
| 63 | } |
| 64 | |
| 65 | void usb_match_value_default(struct usb_match_value *value, |
| 66 | long int default_value) |
| 67 | { |
| 68 | if (value->set) |
| 69 | return; |
| 70 | |
| 71 | value->set = 1; |
| 72 | value->value = default_value; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Match the value against a possible user supplied parameter. |
| 77 | * |
| 78 | * Return: |
| 79 | * 0: The user supplied the given parameter and it did not match the value. |
| 80 | * 1: Either the user didn't supply the parameter, or they did and it |
| 81 | * matches the given value. |
| 82 | */ |
| 83 | static int check_match(struct usb_match_value const *match_value, int value) |
| 84 | { |
| 85 | int reject = match_value->set && (match_value->value != value); |
| 86 | |
| 87 | if (reject) |
| 88 | msg_pdbg("USB: Rejecting device because %s = %d != %d\n", |
| 89 | match_value->name, |
| 90 | value, |
| 91 | match_value->value); |
| 92 | |
| 93 | return !reject; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Allocate a copy of device and add it to the head of the devices list. |
| 98 | */ |
| 99 | static void add_device(struct usb_device *device, |
| 100 | struct usb_device **devices) |
| 101 | { |
| 102 | struct usb_device *copy = malloc(sizeof(struct usb_device)); |
| 103 | |
| 104 | assert(copy != NULL); |
| 105 | |
| 106 | *copy = *device; |
| 107 | |
| 108 | copy->next = *devices; |
| 109 | *devices = copy; |
| 110 | |
| 111 | libusb_ref_device(copy->device); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Look through the interfaces of the current device config for a match. Stop |
| 116 | * looking after the first valid match is found. |
| 117 | * |
| 118 | * Return: |
| 119 | * 0: No matching interface was found. |
| 120 | * 1: A complete match was found and added to the devices list. |
| 121 | */ |
| 122 | static int find_interface(struct usb_match const *match, |
| 123 | struct usb_device *current, |
| 124 | struct usb_device **devices) |
| 125 | { |
| 126 | int i, j; |
| 127 | |
| 128 | for (i = 0; i < current->config_descriptor->bNumInterfaces; ++i) { |
| 129 | struct libusb_interface const *interface; |
| 130 | |
| 131 | interface = ¤t->config_descriptor->interface[i]; |
| 132 | |
| 133 | for (j = 0; j < interface->num_altsetting; ++j) { |
| 134 | struct libusb_interface_descriptor const *descriptor; |
| 135 | |
| 136 | descriptor = &interface->altsetting[j]; |
| 137 | |
| 138 | if (check_match(&match->interface, |
| 139 | descriptor->bInterfaceNumber) && |
| 140 | check_match(&match->altsetting, |
| 141 | descriptor->bAlternateSetting) && |
| 142 | check_match(&match->class, |
| 143 | descriptor->bInterfaceClass) && |
| 144 | check_match(&match->subclass, |
| 145 | descriptor->bInterfaceSubClass) && |
| 146 | check_match(&match->protocol, |
| 147 | descriptor->bInterfaceProtocol)) { |
| 148 | current->interface_descriptor = descriptor; |
| 149 | add_device(current, devices); |
| 150 | msg_pdbg("USB: Found matching device\n"); |
| 151 | return 1; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Look through the configs of the current device for a match. Stop looking |
| 161 | * after the first valid match is found. |
| 162 | * |
| 163 | * Return: |
| 164 | * 0: All configurations successfully checked, one may have been added to |
| 165 | * the list. |
| 166 | * non-zero: There was a failure while checking for a match. |
| 167 | */ |
| 168 | static int find_config(struct usb_match const *match, |
| 169 | struct usb_device *current, |
| 170 | struct libusb_device_descriptor const *device_descriptor, |
| 171 | struct usb_device **devices) |
| 172 | { |
| 173 | int i; |
| 174 | |
| 175 | for (i = 0; i < device_descriptor->bNumConfigurations; ++i) { |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 176 | int ret = LIBUSB(libusb_get_config_descriptor( |
| 177 | current->device, i, |
| 178 | ¤t->config_descriptor)); |
| 179 | if (ret != 0) { |
| 180 | msg_perr("USB: Failed to get config descriptor"); |
| 181 | return ret; |
| 182 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 183 | |
| 184 | if (check_match(&match->config, |
| 185 | current->config_descriptor-> |
| 186 | bConfigurationValue) && |
| 187 | find_interface(match, current, devices)) |
| 188 | break; |
| 189 | |
| 190 | libusb_free_config_descriptor(current->config_descriptor); |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | int usb_device_find(struct usb_match const *match, struct usb_device **devices) |
| 197 | { |
| 198 | libusb_device **list; |
| 199 | ssize_t count; |
Edward O'Callaghan | 0449a1f | 2020-02-17 13:12:33 +1100 | [diff] [blame] | 200 | ssize_t i; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 201 | |
| 202 | *devices = NULL; |
| 203 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 204 | int ret = LIBUSB(count = libusb_get_device_list(NULL, &list)); |
| 205 | if (ret != 0) { |
| 206 | msg_perr("USB: Failed to get device list"); |
| 207 | return ret; |
| 208 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 209 | |
| 210 | for (i = 0; i < count; ++i) { |
| 211 | struct libusb_device_descriptor descriptor; |
| 212 | struct usb_device current = { |
| 213 | .device = list[i], |
| 214 | .handle = NULL, |
| 215 | .next = NULL, |
| 216 | }; |
| 217 | |
| 218 | uint8_t bus = libusb_get_bus_number(list[i]); |
| 219 | uint8_t address = libusb_get_device_address(list[i]); |
| 220 | |
| 221 | msg_pdbg("USB: Inspecting device (Bus %d, Address %d)\n", |
| 222 | bus, |
| 223 | address); |
| 224 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 225 | ret = LIBUSB(libusb_get_device_descriptor(list[i], |
| 226 | &descriptor)); |
Angel Pons | 6f33cec | 2021-02-28 12:40:07 +0100 | [diff] [blame] | 227 | if (ret != 0) { |
| 228 | msg_perr("USB: Failed to get device descriptor"); |
| 229 | free(*devices); |
| 230 | *devices = NULL; |
| 231 | return ret; |
| 232 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 233 | |
| 234 | if (check_match(&match->vid, descriptor.idVendor) && |
| 235 | check_match(&match->pid, descriptor.idProduct) && |
| 236 | check_match(&match->bus, bus) && |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 237 | check_match(&match->address, address)) { |
| 238 | ret = find_config(match, |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 239 | ¤t, |
| 240 | &descriptor, |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 241 | devices); |
| 242 | if (ret != 0) { |
| 243 | msg_perr("USB: Failed to find config"); |
| 244 | return ret; |
| 245 | } |
| 246 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | libusb_free_device_list(list, 1); |
| 250 | |
| 251 | return (*devices == NULL); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * If the underlying libusb device is not open, open it. |
| 256 | * |
| 257 | * Return: |
| 258 | * 0: The device was already open or was successfully opened. |
| 259 | * non-zero: There was a failure while opening the device. |
| 260 | */ |
| 261 | static int usb_device_open(struct usb_device *device) |
| 262 | { |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 263 | if (device->handle == NULL) { |
| 264 | int ret = LIBUSB(libusb_open(device->device, &device->handle)); |
| 265 | if (ret != 0) { |
| 266 | msg_perr("USB: Failed to open device\n"); |
| 267 | return ret; |
| 268 | } |
| 269 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | int usb_device_show(char const *prefix, struct usb_device *device) |
| 275 | { |
| 276 | struct libusb_device_descriptor descriptor; |
| 277 | unsigned char product[256]; |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 278 | int ret; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 279 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 280 | ret = usb_device_open(device); |
| 281 | if (ret != 0) { |
Angel Pons | 6f33cec | 2021-02-28 12:40:07 +0100 | [diff] [blame] | 282 | msg_perr("USB: Failed to open device\n"); |
| 283 | return ret; |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 284 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 285 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 286 | ret = LIBUSB(libusb_get_device_descriptor(device->device, &descriptor)); |
| 287 | if (ret != 0) { |
| 288 | msg_perr("USB: Failed to get device descriptor\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 = LIBUSB(libusb_get_string_descriptor_ascii( |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 293 | device->handle, |
| 294 | descriptor.iProduct, |
| 295 | product, |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 296 | sizeof(product))); |
| 297 | if (ret != 0) { |
| 298 | msg_perr("USB: Failed to get device product string\n"); |
| 299 | return ret; |
| 300 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 301 | |
| 302 | product[255] = '\0'; |
| 303 | |
| 304 | msg_perr("%sbus=0x%02x,address=0x%02x | %s\n", |
| 305 | prefix, |
| 306 | libusb_get_bus_number(device->device), |
| 307 | libusb_get_device_address(device->device), |
| 308 | product); |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | int usb_device_claim(struct usb_device *device) |
| 314 | { |
| 315 | int current_config; |
| 316 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 317 | int ret = usb_device_open(device); |
| 318 | if (ret != 0) { |
| 319 | msg_perr("USB: Failed to open device\n"); |
| 320 | return ret; |
| 321 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 322 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 323 | ret = LIBUSB(libusb_get_configuration(device->handle, |
| 324 | ¤t_config)); |
| 325 | if (ret != 0) { |
| 326 | msg_perr("USB: Failed to get current device configuration\n"); |
| 327 | return ret; |
| 328 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 329 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 330 | if (current_config != device->config_descriptor->bConfigurationValue) { |
| 331 | ret = LIBUSB(libusb_set_configuration( |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 332 | device->handle, |
| 333 | device-> |
| 334 | config_descriptor-> |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 335 | bConfigurationValue)); |
| 336 | if (ret != 0) { |
| 337 | msg_perr("USB: Failed to set new configuration from %d to %d\n", |
| 338 | current_config, |
| 339 | device->config_descriptor->bConfigurationValue); |
| 340 | return ret; |
| 341 | } |
| 342 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 343 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 344 | ret = LIBUSB(libusb_set_auto_detach_kernel_driver(device->handle, 1)); |
| 345 | if (ret != 0) { |
| 346 | msg_perr("USB: Failed to enable auto kernel driver detach\n"); |
| 347 | return ret; |
| 348 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 349 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 350 | ret = LIBUSB(libusb_claim_interface(device->handle, |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 351 | device-> |
| 352 | interface_descriptor-> |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 353 | bInterfaceNumber)); |
| 354 | if (ret != 0) { |
| 355 | msg_perr("USB: Could not claim device interface %d\n", |
| 356 | device->interface_descriptor->bInterfaceNumber); |
| 357 | return ret; |
| 358 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 359 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 360 | if (device->interface_descriptor->bAlternateSetting != 0) { |
| 361 | ret = LIBUSB(libusb_set_interface_alt_setting( |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 362 | device->handle, |
| 363 | device-> |
| 364 | interface_descriptor-> |
| 365 | bInterfaceNumber, |
| 366 | device-> |
| 367 | interface_descriptor-> |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 368 | bAlternateSetting)); |
| 369 | if (ret != 0) { |
| 370 | msg_perr("USB: Failed to set alternate setting %d\n", |
| 371 | device->interface_descriptor->bAlternateSetting); |
| 372 | return ret; |
| 373 | } |
| 374 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | struct usb_device *usb_device_free(struct usb_device *device) |
| 380 | { |
| 381 | struct usb_device *next = device->next; |
| 382 | |
| 383 | if (device->handle != NULL) |
| 384 | libusb_close(device->handle); |
| 385 | |
| 386 | /* |
| 387 | * This unref balances the ref added in the add_device function. |
| 388 | */ |
| 389 | libusb_unref_device(device->device); |
| 390 | libusb_free_config_descriptor(device->config_descriptor); |
| 391 | |
| 392 | free(device); |
| 393 | |
| 394 | return next; |
| 395 | } |