blob: d979eafb2a3db25990a3ca856176e427c821e2db [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) {
197 CHECK(LIBUSB(libusb_get_config_descriptor(
198 current->device,
199 i,
200 &current->config_descriptor)),
201 "USB: Failed to get config descriptor");
202
203 if (check_match(&match->config,
204 current->config_descriptor->
205 bConfigurationValue) &&
206 find_interface(match, current, devices))
207 break;
208
209 libusb_free_config_descriptor(current->config_descriptor);
210 }
211
212 return 0;
213}
214
215int usb_device_find(struct usb_match const *match, struct usb_device **devices)
216{
217 libusb_device **list;
218 ssize_t count;
219 size_t i;
220
221 *devices = NULL;
222
223 CHECK(LIBUSB(count = libusb_get_device_list(NULL, &list)),
224 "USB: Failed to get device list");
225
226 for (i = 0; i < count; ++i) {
227 struct libusb_device_descriptor descriptor;
228 struct usb_device current = {
229 .device = list[i],
230 .handle = NULL,
231 .next = NULL,
232 };
233
234 uint8_t bus = libusb_get_bus_number(list[i]);
235 uint8_t address = libusb_get_device_address(list[i]);
236
237 msg_pdbg("USB: Inspecting device (Bus %d, Address %d)\n",
238 bus,
239 address);
240
241 CHECK(LIBUSB(libusb_get_device_descriptor(list[i],
242 &descriptor)),
243 "USB: Failed to get device descriptor");
244
245 if (check_match(&match->vid, descriptor.idVendor) &&
246 check_match(&match->pid, descriptor.idProduct) &&
247 check_match(&match->bus, bus) &&
248 check_match(&match->address, address))
249 CHECK(find_config(match,
250 &current,
251 &descriptor,
252 devices),
253 "USB: Failed to find config");
254 }
255
256 libusb_free_device_list(list, 1);
257
258 return (*devices == NULL);
259}
260
261/*
262 * If the underlying libusb device is not open, open it.
263 *
264 * Return:
265 * 0: The device was already open or was successfully opened.
266 * non-zero: There was a failure while opening the device.
267 */
268static int usb_device_open(struct usb_device *device)
269{
Anton Staaf5614e252015-03-24 14:33:33 -0700270 if (device->handle == NULL)
271 CHECK(LIBUSB(libusb_open(device->device, &device->handle)),
272 "USB: Failed to open device\n");
273
274 return 0;
275}
276
277int usb_device_show(char const *prefix, struct usb_device *device)
278{
279 struct libusb_device_descriptor descriptor;
280 unsigned char product[256];
281
David Hendricksaf999b52015-11-05 20:32:16 -0800282 CHECK(usb_device_open(device), "USB: Failed to open device\n");
Anton Staaf5614e252015-03-24 14:33:33 -0700283
284 CHECK(LIBUSB(libusb_get_device_descriptor(device->device, &descriptor)),
285 "USB: Failed to get device descriptor\n");
286
287 CHECK(LIBUSB(libusb_get_string_descriptor_ascii(
288 device->handle,
289 descriptor.iProduct,
290 product,
291 sizeof(product))),
292 "USB: Failed to get device product string\n");
293
294 product[255] = '\0';
295
296 msg_perr("%sbus=0x%02x,address=0x%02x | %s\n",
297 prefix,
298 libusb_get_bus_number(device->device),
299 libusb_get_device_address(device->device),
300 product);
301
302 return 0;
303}
304
305int usb_device_claim(struct usb_device *device)
306{
307 int current_config;
308
David Hendricksaf999b52015-11-05 20:32:16 -0800309 CHECK(usb_device_open(device), "USB: Failed to open device\n");
Anton Staaf5614e252015-03-24 14:33:33 -0700310
311 CHECK(LIBUSB(libusb_get_configuration(device->handle,
312 &current_config)),
313 "USB: Failed to get current device configuration\n");
314
315 if (current_config != device->config_descriptor->bConfigurationValue)
316 CHECK(LIBUSB(libusb_set_configuration(
317 device->handle,
318 device->
319 config_descriptor->
320 bConfigurationValue)),
321 "USB: Failed to set new configuration from %d to %d\n",
322 current_config,
323 device->config_descriptor->bConfigurationValue);
324
325 CHECK(LIBUSB(libusb_set_auto_detach_kernel_driver(device->handle, 1)),
326 "USB: Failed to enable auto kernel driver detach\n");
327
328 CHECK(LIBUSB(libusb_claim_interface(device->handle,
329 device->
330 interface_descriptor->
331 bInterfaceNumber)),
332 "USB: Could not claim device interface %d\n",
333 device->interface_descriptor->bInterfaceNumber);
334
335 if (device->interface_descriptor->bAlternateSetting != 0)
336 CHECK(LIBUSB(libusb_set_interface_alt_setting(
337 device->handle,
338 device->
339 interface_descriptor->
340 bInterfaceNumber,
341 device->
342 interface_descriptor->
343 bAlternateSetting)),
344 "USB: Failed to set alternate setting %d\n",
345 device->interface_descriptor->bAlternateSetting);
346
347 return 0;
348}
349
350struct usb_device *usb_device_free(struct usb_device *device)
351{
352 struct usb_device *next = device->next;
353
354 if (device->handle != NULL)
355 libusb_close(device->handle);
356
357 /*
358 * This unref balances the ref added in the add_device function.
359 */
360 libusb_unref_device(device->device);
361 libusb_free_config_descriptor(device->config_descriptor);
362
363 free(device);
364
365 return next;
366}