blob: c8d596801714038abee35b6cbcb73f96a782406f [file] [log] [blame]
Anton Staaf5614e252015-03-24 14:33:33 -07001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright 2015, 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#include "programmer.h"
39#include "spi.h"
40#include "usb_device.h"
41
42#include <assert.h>
43#include <libusb.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47
48/*
49 * Possibly extract a programmer parameter and use it to initialize the given
50 * match value structure.
51 */
52static void usb_match_value_init(struct usb_match_value *match,
53 char const *parameter)
54{
55 char *string = extract_programmer_param(parameter);
56
57 match->name = parameter;
58
59 if (string) {
60 match->set = 1;
61 match->value = strtol(string, NULL, 0);
62 } else {
63 match->set = 0;
64 }
65
66 free(string);
67}
68
69#define USB_MATCH_VALUE_INIT(NAME) \
70 usb_match_value_init(&match->NAME, #NAME)
71
72void usb_match_init(struct usb_match *match)
73{
74 USB_MATCH_VALUE_INIT(vid);
75 USB_MATCH_VALUE_INIT(pid);
76 USB_MATCH_VALUE_INIT(bus);
77 USB_MATCH_VALUE_INIT(address);
78 USB_MATCH_VALUE_INIT(config);
79 USB_MATCH_VALUE_INIT(interface);
80 USB_MATCH_VALUE_INIT(altsetting);
81 USB_MATCH_VALUE_INIT(class);
82 USB_MATCH_VALUE_INIT(subclass);
83 USB_MATCH_VALUE_INIT(protocol);
84}
85
86void usb_match_value_default(struct usb_match_value *value,
87 long int default_value)
88{
89 if (value->set)
90 return;
91
92 value->set = 1;
93 value->value = default_value;
94}
95
96/*
97 * Match the value against a possible user supplied parameter.
98 *
99 * Return:
100 * 0: The user supplied the given parameter and it did not match the value.
101 * 1: Either the user didn't supply the parameter, or they did and it
102 * matches the given value.
103 */
104static int check_match(struct usb_match_value const *match_value, int value)
105{
106 int reject = match_value->set && (match_value->value != value);
107
108 if (reject)
109 msg_pdbg("USB: Rejecting device because %s = %d != %d\n",
110 match_value->name,
111 value,
112 match_value->value);
113
114 return !reject;
115}
116
117/*
118 * Allocate a copy of device and add it to the head of the devices list.
119 */
120static void add_device(struct usb_device *device,
121 struct usb_device **devices)
122{
123 struct usb_device *copy = malloc(sizeof(struct usb_device));
124
125 assert(copy != NULL);
126
127 *copy = *device;
128
129 copy->next = *devices;
130 *devices = copy;
131
132 libusb_ref_device(copy->device);
133}
134
135/*
136 * Look through the interfaces of the current device config for a match. Stop
137 * looking after the first valid match is found.
138 *
139 * Return:
140 * 0: No matching interface was found.
141 * 1: A complete match was found and added to the devices list.
142 */
143static int find_interface(struct usb_match const *match,
144 struct usb_device *current,
145 struct usb_device **devices)
146{
147 int i, j;
148
149 for (i = 0; i < current->config_descriptor->bNumInterfaces; ++i) {
150 struct libusb_interface const *interface;
151
152 interface = &current->config_descriptor->interface[i];
153
154 for (j = 0; j < interface->num_altsetting; ++j) {
155 struct libusb_interface_descriptor const *descriptor;
156
157 descriptor = &interface->altsetting[j];
158
159 if (check_match(&match->interface,
160 descriptor->bInterfaceNumber) &&
161 check_match(&match->altsetting,
162 descriptor->bAlternateSetting) &&
163 check_match(&match->class,
164 descriptor->bInterfaceClass) &&
165 check_match(&match->subclass,
166 descriptor->bInterfaceSubClass) &&
167 check_match(&match->protocol,
168 descriptor->bInterfaceProtocol)) {
169 current->interface_descriptor = descriptor;
170 add_device(current, devices);
171 msg_pdbg("USB: Found matching device\n");
172 return 1;
173 }
174 }
175 }
176
177 return 0;
178}
179
180/*
181 * Look through the configs of the current device for a match. Stop looking
182 * after the first valid match is found.
183 *
184 * Return:
185 * 0: All configurations successfully checked, one may have been added to
186 * the list.
187 * non-zero: There was a failure while checking for a match.
188 */
189static int find_config(struct usb_match const *match,
190 struct usb_device *current,
191 struct libusb_device_descriptor const *device_descriptor,
192 struct usb_device **devices)
193{
194 int i;
195
196 for (i = 0; i < device_descriptor->bNumConfigurations; ++i) {
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100197 int ret = LIBUSB(libusb_get_config_descriptor(
198 current->device, i,
199 &current->config_descriptor));
200 if (ret != 0) {
201 msg_perr("USB: Failed to get config descriptor");
202 return ret;
203 }
Anton Staaf5614e252015-03-24 14:33:33 -0700204
205 if (check_match(&match->config,
206 current->config_descriptor->
207 bConfigurationValue) &&
208 find_interface(match, current, devices))
209 break;
210
211 libusb_free_config_descriptor(current->config_descriptor);
212 }
213
214 return 0;
215}
216
217int usb_device_find(struct usb_match const *match, struct usb_device **devices)
218{
219 libusb_device **list;
220 ssize_t count;
221 size_t i;
222
223 *devices = NULL;
224
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100225 int ret = LIBUSB(count = libusb_get_device_list(NULL, &list));
226 if (ret != 0) {
227 msg_perr("USB: Failed to get device list");
228 return ret;
229 }
Anton Staaf5614e252015-03-24 14:33:33 -0700230
231 for (i = 0; i < count; ++i) {
232 struct libusb_device_descriptor descriptor;
233 struct usb_device current = {
234 .device = list[i],
235 .handle = NULL,
236 .next = NULL,
237 };
238
239 uint8_t bus = libusb_get_bus_number(list[i]);
240 uint8_t address = libusb_get_device_address(list[i]);
241
242 msg_pdbg("USB: Inspecting device (Bus %d, Address %d)\n",
243 bus,
244 address);
245
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100246 ret = LIBUSB(libusb_get_device_descriptor(list[i],
247 &descriptor));
248 if (ret != 0) {
249 msg_perr("USB: Failed to get device descriptor");
250 return ret;
251 }
Anton Staaf5614e252015-03-24 14:33:33 -0700252
253 if (check_match(&match->vid, descriptor.idVendor) &&
254 check_match(&match->pid, descriptor.idProduct) &&
255 check_match(&match->bus, bus) &&
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100256 check_match(&match->address, address)) {
257 ret = find_config(match,
Anton Staaf5614e252015-03-24 14:33:33 -0700258 &current,
259 &descriptor,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100260 devices);
261 if (ret != 0) {
262 msg_perr("USB: Failed to find config");
263 return ret;
264 }
265 }
Anton Staaf5614e252015-03-24 14:33:33 -0700266 }
267
268 libusb_free_device_list(list, 1);
269
270 return (*devices == NULL);
271}
272
273/*
274 * If the underlying libusb device is not open, open it.
275 *
276 * Return:
277 * 0: The device was already open or was successfully opened.
278 * non-zero: There was a failure while opening the device.
279 */
280static int usb_device_open(struct usb_device *device)
281{
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100282 if (device->handle == NULL) {
283 int ret = LIBUSB(libusb_open(device->device, &device->handle));
284 if (ret != 0) {
285 msg_perr("USB: Failed to open device\n");
286 return ret;
287 }
288 }
Anton Staaf5614e252015-03-24 14:33:33 -0700289
290 return 0;
291}
292
293int usb_device_show(char const *prefix, struct usb_device *device)
294{
295 struct libusb_device_descriptor descriptor;
296 unsigned char product[256];
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100297 int ret;
Anton Staaf5614e252015-03-24 14:33:33 -0700298
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100299 ret = usb_device_open(device);
300 if (ret != 0) {
301 msg_perr("USB: Failed to open device\n");
302 return ret;
303 }
Anton Staaf5614e252015-03-24 14:33:33 -0700304
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100305 ret = LIBUSB(libusb_get_device_descriptor(device->device, &descriptor));
306 if (ret != 0) {
307 msg_perr("USB: Failed to get device descriptor\n");
308 return ret;
309 }
Anton Staaf5614e252015-03-24 14:33:33 -0700310
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100311 ret = LIBUSB(libusb_get_string_descriptor_ascii(
Anton Staaf5614e252015-03-24 14:33:33 -0700312 device->handle,
313 descriptor.iProduct,
314 product,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100315 sizeof(product)));
316 if (ret != 0) {
317 msg_perr("USB: Failed to get device product string\n");
318 return ret;
319 }
Anton Staaf5614e252015-03-24 14:33:33 -0700320
321 product[255] = '\0';
322
323 msg_perr("%sbus=0x%02x,address=0x%02x | %s\n",
324 prefix,
325 libusb_get_bus_number(device->device),
326 libusb_get_device_address(device->device),
327 product);
328
329 return 0;
330}
331
332int usb_device_claim(struct usb_device *device)
333{
334 int current_config;
335
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100336 int ret = usb_device_open(device);
337 if (ret != 0) {
338 msg_perr("USB: Failed to open device\n");
339 return ret;
340 }
Anton Staaf5614e252015-03-24 14:33:33 -0700341
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100342 ret = LIBUSB(libusb_get_configuration(device->handle,
343 &current_config));
344 if (ret != 0) {
345 msg_perr("USB: Failed to get current device configuration\n");
346 return ret;
347 }
Anton Staaf5614e252015-03-24 14:33:33 -0700348
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100349 if (current_config != device->config_descriptor->bConfigurationValue) {
350 ret = LIBUSB(libusb_set_configuration(
Anton Staaf5614e252015-03-24 14:33:33 -0700351 device->handle,
352 device->
353 config_descriptor->
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100354 bConfigurationValue));
355 if (ret != 0) {
356 msg_perr("USB: Failed to set new configuration from %d to %d\n",
357 current_config,
358 device->config_descriptor->bConfigurationValue);
359 return ret;
360 }
361 }
Anton Staaf5614e252015-03-24 14:33:33 -0700362
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100363 ret = LIBUSB(libusb_set_auto_detach_kernel_driver(device->handle, 1));
364 if (ret != 0) {
365 msg_perr("USB: Failed to enable auto kernel driver detach\n");
366 return ret;
367 }
Anton Staaf5614e252015-03-24 14:33:33 -0700368
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100369 ret = LIBUSB(libusb_claim_interface(device->handle,
Anton Staaf5614e252015-03-24 14:33:33 -0700370 device->
371 interface_descriptor->
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100372 bInterfaceNumber));
373 if (ret != 0) {
374 msg_perr("USB: Could not claim device interface %d\n",
375 device->interface_descriptor->bInterfaceNumber);
376 return ret;
377 }
Anton Staaf5614e252015-03-24 14:33:33 -0700378
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100379 if (device->interface_descriptor->bAlternateSetting != 0) {
380 ret = LIBUSB(libusb_set_interface_alt_setting(
Anton Staaf5614e252015-03-24 14:33:33 -0700381 device->handle,
382 device->
383 interface_descriptor->
384 bInterfaceNumber,
385 device->
386 interface_descriptor->
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100387 bAlternateSetting));
388 if (ret != 0) {
389 msg_perr("USB: Failed to set alternate setting %d\n",
390 device->interface_descriptor->bAlternateSetting);
391 return ret;
392 }
393 }
Anton Staaf5614e252015-03-24 14:33:33 -0700394
395 return 0;
396}
397
398struct usb_device *usb_device_free(struct usb_device *device)
399{
400 struct usb_device *next = device->next;
401
402 if (device->handle != NULL)
403 libusb_close(device->handle);
404
405 /*
406 * This unref balances the ref added in the add_device function.
407 */
408 libusb_unref_device(device->device);
409 libusb_free_config_descriptor(device->config_descriptor);
410
411 free(device);
412
413 return next;
414}