blob: 0973c0aef331623e6326dd7cd97786be6ea9b0df [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 */
56#define LIBUSB(expression) \
57 ({ \
58 int error__ = (expression); \
59 \
60 if (error__ < 0) { \
61 msg_perr("libusb error: %s:%d %s\n", \
62 __FILE__, \
63 __LINE__, \
64 libusb_error_name(error__)); \
65 error__ = 0x20000 | -error__; \
66 } else { \
67 error__ = 0; \
68 } \
69 \
70 error__; \
71 })
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;
92 struct usb_match_value config;
93 struct usb_match_value interface;
94 struct usb_match_value altsetting;
95 struct usb_match_value class;
96 struct usb_match_value subclass;
97 struct usb_match_value protocol;
98};
99
100/*
101 * Initialize a usb_match structure so that each value's name matches the
102 * values name in the usb_match structure (so bus.name == "bus"...), and
103 * look for each value in the flashrom command line via
104 * extract_programmer_param. If the value is found convert it to an integer
105 * using strtol, accepting hex, decimal and octal encoding.
106 */
107void usb_match_init(struct usb_match *match);
108
109/*
110 * Add a default value to a usb_match_value. This must be done after calling
111 * usb_match_init. If usb_match_init already set the value of a usb_match_value
112 * we do nothing, otherwise set the value to default_value. This ensures that
113 * parameters passed on the command line override defaults.
114 */
115void usb_match_value_default(struct usb_match_value *match,
116 long int default_value);
117
118/*
119 * The usb_device structure is an entry in a linked list of devices that were
120 * matched by usb_device_find.
121 */
122struct usb_device {
123 struct libusb_device *device;
124 struct libusb_config_descriptor *config_descriptor;
125 struct libusb_interface_descriptor const *interface_descriptor;
126
127 /*
128 * Initially NULL, the libusb_device_handle is only valid once the
129 * usb_device has been successfully passed to usb_device_show or
130 * usb_device_claim.
131 */
132 struct libusb_device_handle *handle;
133
134 /*
135 * Link to next device, or NULL
136 */
137 struct usb_device *next;
138};
139
140/*
141 * Find and return a list of all compatible devices. Each device is added to
142 * the list with its first valid configuration and interface. If an alternate
143 * configuration (config, interface, altsetting...) is desired the specifics
144 * can be supplied as programmer parameters.
145 *
146 * Return:
147 * 0: At least one matching device was found.
148 * 1: No matching devices were found.
149 */
150int usb_device_find(struct usb_match const *match, struct usb_device **devices);
151
152/*
153 * Display the devices bus and address as well as its product string. The
154 * underlying libusb device is opened if it is not already open.
155 *
156 * Return:
157 * 0: The device information was displayed.
158 * non-zero: There was a failure while displaying the device information.
159 */
160int usb_device_show(char const *prefix, struct usb_device *device);
161
162/*
163 * Open the underlying libusb device, set its config, claim the interface and
164 * select the correct alternate interface.
165 *
166 * Return:
167 * 0: The device was successfully claimed.
168 * non-zero: There was a failure while trying to claim the device.
169 */
170int usb_device_claim(struct usb_device *device);
171
172/*
173 * Free a usb_device structure.
174 *
175 * This ensures that the libusb device is closed and that all allocated
176 * handles and descriptors are freed.
177 *
178 * Return:
179 * The next device in the device list.
180 */
181struct usb_device *usb_device_free(struct usb_device *device);