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