blob: 48833fbd1ae20ea18cd9b64c7c9c2953bd51b6b5 [file] [log] [blame]
Anton Staaf5614e252015-03-24 14:33:33 -07001/*
2 * This file is part of the flashrom project.
3 *
Edward O'Callaghane413f732020-01-29 15:22:32 +11004 * Copyright (C) 2020, Google Inc. All rights reserved.
Anton Staaf5614e252015-03-24 14:33:33 -07005 *
Edward O'Callaghane413f732020-01-29 15:22:32 +11006 * 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 Staaf5614e252015-03-24 14:33:33 -070010 *
Edward O'Callaghane413f732020-01-29 15:22:32 +110011 * 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 Staaf5614e252015-03-24 14:33:33 -070015 */
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 */
31static 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
51void 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
65void 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 */
83static 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 */
99static 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 */
122static 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 = &current->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 */
168static 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'Callaghan5c81bf32020-01-29 14:26:55 +1100176 int ret = LIBUSB(libusb_get_config_descriptor(
177 current->device, i,
178 &current->config_descriptor));
179 if (ret != 0) {
180 msg_perr("USB: Failed to get config descriptor");
181 return ret;
182 }
Anton Staaf5614e252015-03-24 14:33:33 -0700183
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
196int usb_device_find(struct usb_match const *match, struct usb_device **devices)
197{
198 libusb_device **list;
199 ssize_t count;
Edward O'Callaghan0449a1f2020-02-17 13:12:33 +1100200 ssize_t i;
Anton Staaf5614e252015-03-24 14:33:33 -0700201
202 *devices = NULL;
203
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100204 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 Staaf5614e252015-03-24 14:33:33 -0700209
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'Callaghan5c81bf32020-01-29 14:26:55 +1100225 ret = LIBUSB(libusb_get_device_descriptor(list[i],
226 &descriptor));
227 if (ret != 0) {
228 msg_perr("USB: Failed to get device descriptor");
229 return ret;
230 }
Anton Staaf5614e252015-03-24 14:33:33 -0700231
232 if (check_match(&match->vid, descriptor.idVendor) &&
233 check_match(&match->pid, descriptor.idProduct) &&
234 check_match(&match->bus, bus) &&
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100235 check_match(&match->address, address)) {
236 ret = find_config(match,
Anton Staaf5614e252015-03-24 14:33:33 -0700237 &current,
238 &descriptor,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100239 devices);
240 if (ret != 0) {
241 msg_perr("USB: Failed to find config");
242 return ret;
243 }
244 }
Anton Staaf5614e252015-03-24 14:33:33 -0700245 }
246
247 libusb_free_device_list(list, 1);
248
249 return (*devices == NULL);
250}
251
252/*
253 * If the underlying libusb device is not open, open it.
254 *
255 * Return:
256 * 0: The device was already open or was successfully opened.
257 * non-zero: There was a failure while opening the device.
258 */
259static int usb_device_open(struct usb_device *device)
260{
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100261 if (device->handle == NULL) {
262 int ret = LIBUSB(libusb_open(device->device, &device->handle));
263 if (ret != 0) {
264 msg_perr("USB: Failed to open device\n");
265 return ret;
266 }
267 }
Anton Staaf5614e252015-03-24 14:33:33 -0700268
269 return 0;
270}
271
272int usb_device_show(char const *prefix, struct usb_device *device)
273{
274 struct libusb_device_descriptor descriptor;
275 unsigned char product[256];
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100276 int ret;
Anton Staaf5614e252015-03-24 14:33:33 -0700277
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100278 ret = usb_device_open(device);
279 if (ret != 0) {
280 msg_perr("USB: Failed to open device\n");
281 return ret;
282 }
Anton Staaf5614e252015-03-24 14:33:33 -0700283
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100284 ret = LIBUSB(libusb_get_device_descriptor(device->device, &descriptor));
285 if (ret != 0) {
286 msg_perr("USB: Failed to get device descriptor\n");
287 return ret;
288 }
Anton Staaf5614e252015-03-24 14:33:33 -0700289
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100290 ret = LIBUSB(libusb_get_string_descriptor_ascii(
Anton Staaf5614e252015-03-24 14:33:33 -0700291 device->handle,
292 descriptor.iProduct,
293 product,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100294 sizeof(product)));
295 if (ret != 0) {
296 msg_perr("USB: Failed to get device product string\n");
297 return ret;
298 }
Anton Staaf5614e252015-03-24 14:33:33 -0700299
300 product[255] = '\0';
301
302 msg_perr("%sbus=0x%02x,address=0x%02x | %s\n",
303 prefix,
304 libusb_get_bus_number(device->device),
305 libusb_get_device_address(device->device),
306 product);
307
308 return 0;
309}
310
311int usb_device_claim(struct usb_device *device)
312{
313 int current_config;
314
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100315 int ret = usb_device_open(device);
316 if (ret != 0) {
317 msg_perr("USB: Failed to open device\n");
318 return ret;
319 }
Anton Staaf5614e252015-03-24 14:33:33 -0700320
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100321 ret = LIBUSB(libusb_get_configuration(device->handle,
322 &current_config));
323 if (ret != 0) {
324 msg_perr("USB: Failed to get current device configuration\n");
325 return ret;
326 }
Anton Staaf5614e252015-03-24 14:33:33 -0700327
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100328 if (current_config != device->config_descriptor->bConfigurationValue) {
329 ret = LIBUSB(libusb_set_configuration(
Anton Staaf5614e252015-03-24 14:33:33 -0700330 device->handle,
331 device->
332 config_descriptor->
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100333 bConfigurationValue));
334 if (ret != 0) {
335 msg_perr("USB: Failed to set new configuration from %d to %d\n",
336 current_config,
337 device->config_descriptor->bConfigurationValue);
338 return ret;
339 }
340 }
Anton Staaf5614e252015-03-24 14:33:33 -0700341
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100342 ret = LIBUSB(libusb_set_auto_detach_kernel_driver(device->handle, 1));
343 if (ret != 0) {
344 msg_perr("USB: Failed to enable auto kernel driver detach\n");
345 return ret;
346 }
Anton Staaf5614e252015-03-24 14:33:33 -0700347
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100348 ret = LIBUSB(libusb_claim_interface(device->handle,
Anton Staaf5614e252015-03-24 14:33:33 -0700349 device->
350 interface_descriptor->
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100351 bInterfaceNumber));
352 if (ret != 0) {
353 msg_perr("USB: Could not claim device interface %d\n",
354 device->interface_descriptor->bInterfaceNumber);
355 return ret;
356 }
Anton Staaf5614e252015-03-24 14:33:33 -0700357
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100358 if (device->interface_descriptor->bAlternateSetting != 0) {
359 ret = LIBUSB(libusb_set_interface_alt_setting(
Anton Staaf5614e252015-03-24 14:33:33 -0700360 device->handle,
361 device->
362 interface_descriptor->
363 bInterfaceNumber,
364 device->
365 interface_descriptor->
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100366 bAlternateSetting));
367 if (ret != 0) {
368 msg_perr("USB: Failed to set alternate setting %d\n",
369 device->interface_descriptor->bAlternateSetting);
370 return ret;
371 }
372 }
Anton Staaf5614e252015-03-24 14:33:33 -0700373
374 return 0;
375}
376
377struct usb_device *usb_device_free(struct usb_device *device)
378{
379 struct usb_device *next = device->next;
380
381 if (device->handle != NULL)
382 libusb_close(device->handle);
383
384 /*
385 * This unref balances the ref added in the add_device function.
386 */
387 libusb_unref_device(device->device);
388 libusb_free_config_descriptor(device->config_descriptor);
389
390 free(device);
391
392 return next;
393}