blob: cd7ded281136a74f93d526c88083d04f038e6de3 [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/*
39 * USB device matching framework
40 *
41 * This can be used to match a USB device by a number of different parameters.
42 * The parameters can be passed on the command line and defaults can be set
43 * by the programmer.
44 */
45
46#include "check.h"
47
48#include <libusb.h>
49#include <stdint.h>
50
51/*
52 * The LIBUSB macro converts a libusb failure code into an error code that
53 * flashrom recognizes. It also displays additional libusb specific
54 * information about the failure.
55 */
Souvik Ghosh5cdb7e52016-06-23 12:57:38 -070056#define LIBUSB(expression) \
57 ({ \
58 int libusb_error__ = (expression); \
59 \
60 if (libusb_error__ < 0) { \
61 msg_perr("libusb error: %s:%d %s\n", \
62 __FILE__, \
63 __LINE__, \
64 libusb_error_name(libusb_error__)); \
65 libusb_error__ = 0x20000 | -libusb_error__; \
66 } else { \
67 libusb_error__ = 0; \
68 } \
69 \
70 libusb_error__; \
Anton Staaf5614e252015-03-24 14:33:33 -070071 })
72
73/*
74 * A USB match and associated value struct are used to encode the information
75 * about a device against which we wish to match. If the value of a
76 * usb_match_value has been set then a device must match that value. The name
77 * of the usb_match_value is used to fetch the programmer parameter from the
78 * flashrom command line and is the same as the name of the corresponding
79 * field in usb_match.
80 */
81struct usb_match_value {
82 char const *name;
83 int value;
84 int set;
85};
86
87struct usb_match {
88 struct usb_match_value bus;
89 struct usb_match_value address;
90 struct usb_match_value vid;
91 struct usb_match_value pid;
David Hendricks5c79a492016-06-14 20:56:36 -070092 struct usb_match_value serial;
Anton Staaf5614e252015-03-24 14:33:33 -070093 struct usb_match_value config;
94 struct usb_match_value interface;
95 struct usb_match_value altsetting;
96 struct usb_match_value class;
97 struct usb_match_value subclass;
98 struct usb_match_value protocol;
99};
100
101/*
102 * Initialize a usb_match structure so that each value's name matches the
103 * values name in the usb_match structure (so bus.name == "bus"...), and
104 * look for each value in the flashrom command line via
105 * extract_programmer_param. If the value is found convert it to an integer
106 * using strtol, accepting hex, decimal and octal encoding.
107 */
108void usb_match_init(struct usb_match *match);
109
110/*
111 * Add a default value to a usb_match_value. This must be done after calling
112 * usb_match_init. If usb_match_init already set the value of a usb_match_value
113 * we do nothing, otherwise set the value to default_value. This ensures that
114 * parameters passed on the command line override defaults.
115 */
116void usb_match_value_default(struct usb_match_value *match,
117 long int default_value);
118
119/*
120 * The usb_device structure is an entry in a linked list of devices that were
121 * matched by usb_device_find.
122 */
123struct usb_device {
124 struct libusb_device *device;
125 struct libusb_config_descriptor *config_descriptor;
126 struct libusb_interface_descriptor const *interface_descriptor;
127
128 /*
129 * Initially NULL, the libusb_device_handle is only valid once the
130 * usb_device has been successfully passed to usb_device_show or
131 * usb_device_claim.
132 */
133 struct libusb_device_handle *handle;
134
135 /*
136 * Link to next device, or NULL
137 */
138 struct usb_device *next;
139};
140
141/*
142 * Find and return a list of all compatible devices. Each device is added to
143 * the list with its first valid configuration and interface. If an alternate
144 * configuration (config, interface, altsetting...) is desired the specifics
145 * can be supplied as programmer parameters.
146 *
147 * Return:
148 * 0: At least one matching device was found.
149 * 1: No matching devices were found.
150 */
151int usb_device_find(struct usb_match const *match, struct usb_device **devices);
152
153/*
154 * Display the devices bus and address as well as its product string. The
155 * underlying libusb device is opened if it is not already open.
156 *
157 * Return:
158 * 0: The device information was displayed.
159 * non-zero: There was a failure while displaying the device information.
160 */
161int usb_device_show(char const *prefix, struct usb_device *device);
162
163/*
164 * Open the underlying libusb device, set its config, claim the interface and
165 * select the correct alternate interface.
166 *
167 * Return:
168 * 0: The device was successfully claimed.
169 * non-zero: There was a failure while trying to claim the device.
170 */
171int usb_device_claim(struct usb_device *device);
172
173/*
174 * Free a usb_device structure.
175 *
176 * This ensures that the libusb device is closed and that all allocated
177 * handles and descriptors are freed.
178 *
179 * Return:
180 * The next device in the device list.
181 */
182struct usb_device *usb_device_free(struct usb_device *device);