blob: a4f6d123b72e1ba6a4ef42d6b4e49c69b5303952 [file] [log] [blame]
Edward O'Callaghand3396402020-02-17 12:47:12 +11001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2020, Google Inc. All rights reserved.
5 *
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.
10 *
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.
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 */
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{
Angel Pons3bd47522021-06-07 12:33:53 +0200102 struct usb_device *copy = malloc(sizeof(*copy));
Edward O'Callaghand3396402020-02-17 12:47:12 +1100103
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) {
176 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 }
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
196int usb_device_find(struct usb_match const *match, struct usb_device **devices)
197{
198 libusb_device **list;
199 ssize_t count;
200 ssize_t i;
201
202 *devices = NULL;
203
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 }
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
225 ret = LIBUSB(libusb_get_device_descriptor(list[i],
226 &descriptor));
Angel Pons9635cb42021-02-28 12:40:07 +0100227 if (ret != 0) {
228 msg_perr("USB: Failed to get device descriptor");
229 free(*devices);
230 *devices = NULL;
231 return ret;
232 }
Edward O'Callaghand3396402020-02-17 12:47:12 +1100233
234 if (check_match(&match->vid, descriptor.idVendor) &&
235 check_match(&match->pid, descriptor.idProduct) &&
236 check_match(&match->bus, bus) &&
237 check_match(&match->address, address)) {
238 ret = find_config(match,
239 &current,
240 &descriptor,
241 devices);
242 if (ret != 0) {
243 msg_perr("USB: Failed to find config");
244 return ret;
245 }
246 }
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 */
261static int usb_device_open(struct usb_device *device)
262{
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 }
270
271 return 0;
272}
273
274int usb_device_show(char const *prefix, struct usb_device *device)
275{
276 struct libusb_device_descriptor descriptor;
277 unsigned char product[256];
278 int ret;
279
280 ret = usb_device_open(device);
281 if (ret != 0) {
Angel Pons9635cb42021-02-28 12:40:07 +0100282 msg_perr("USB: Failed to open device\n");
283 return ret;
Edward O'Callaghand3396402020-02-17 12:47:12 +1100284 }
285
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 }
291
292 ret = LIBUSB(libusb_get_string_descriptor_ascii(
293 device->handle,
294 descriptor.iProduct,
295 product,
296 sizeof(product)));
297 if (ret != 0) {
298 msg_perr("USB: Failed to get device product string\n");
299 return ret;
300 }
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
313int usb_device_claim(struct usb_device *device)
314{
315 int current_config;
316
317 int ret = usb_device_open(device);
318 if (ret != 0) {
319 msg_perr("USB: Failed to open device\n");
320 return ret;
321 }
322
323 ret = LIBUSB(libusb_get_configuration(device->handle,
324 &current_config));
325 if (ret != 0) {
326 msg_perr("USB: Failed to get current device configuration\n");
327 return ret;
328 }
329
330 if (current_config != device->config_descriptor->bConfigurationValue) {
331 ret = LIBUSB(libusb_set_configuration(
332 device->handle,
333 device->
334 config_descriptor->
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 }
343
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 }
349
350 ret = LIBUSB(libusb_claim_interface(device->handle,
351 device->
352 interface_descriptor->
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 }
359
360 if (device->interface_descriptor->bAlternateSetting != 0) {
361 ret = LIBUSB(libusb_set_interface_alt_setting(
362 device->handle,
363 device->
364 interface_descriptor->
365 bInterfaceNumber,
366 device->
367 interface_descriptor->
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 }
375
376 return 0;
377}
378
379struct 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}