usb_device.h 6.2 KB

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