usb_device.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #include "programmer.h"
  38. #include "spi.h"
  39. #include "usb_device.h"
  40. #include <assert.h>
  41. #include <libusb.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. /*
  46. * Possibly extract a programmer parameter and use it to initialize the given
  47. * match value structure.
  48. */
  49. static void usb_match_value_init(struct usb_match_value *match,
  50. char const *parameter)
  51. {
  52. char *string = extract_programmer_param(parameter);
  53. match->name = parameter;
  54. if (string) {
  55. match->set = 1;
  56. match->value = strtol(string, NULL, 0);
  57. } else {
  58. match->set = 0;
  59. }
  60. free(string);
  61. }
  62. #define USB_MATCH_VALUE_INIT(NAME) \
  63. usb_match_value_init(&match->NAME, #NAME)
  64. void usb_match_init(struct usb_match *match)
  65. {
  66. USB_MATCH_VALUE_INIT(vid);
  67. USB_MATCH_VALUE_INIT(pid);
  68. USB_MATCH_VALUE_INIT(bus);
  69. USB_MATCH_VALUE_INIT(address);
  70. USB_MATCH_VALUE_INIT(config);
  71. USB_MATCH_VALUE_INIT(interface);
  72. USB_MATCH_VALUE_INIT(altsetting);
  73. USB_MATCH_VALUE_INIT(class);
  74. USB_MATCH_VALUE_INIT(subclass);
  75. USB_MATCH_VALUE_INIT(protocol);
  76. }
  77. void usb_match_value_default(struct usb_match_value *value,
  78. long int default_value)
  79. {
  80. if (value->set)
  81. return;
  82. value->set = 1;
  83. value->value = default_value;
  84. }
  85. /*
  86. * Match the value against a possible user supplied parameter.
  87. *
  88. * Return:
  89. * 0: The user supplied the given parameter and it did not match the value.
  90. * 1: Either the user didn't supply the parameter, or they did and it
  91. * matches the given value.
  92. */
  93. static int check_match(struct usb_match_value const *match_value, int value)
  94. {
  95. int reject = match_value->set && (match_value->value != value);
  96. if (reject)
  97. msg_pdbg("USB: Rejecting device because %s = %d != %d\n",
  98. match_value->name,
  99. value,
  100. match_value->value);
  101. return !reject;
  102. }
  103. /*
  104. * Allocate a copy of device and add it to the head of the devices list.
  105. */
  106. static void add_device(struct usb_device *device,
  107. struct usb_device **devices)
  108. {
  109. struct usb_device *copy = malloc(sizeof(struct usb_device));
  110. assert(copy != NULL);
  111. *copy = *device;
  112. copy->next = *devices;
  113. *devices = copy;
  114. libusb_ref_device(copy->device);
  115. }
  116. /*
  117. * Look through the interfaces of the current device config for a match. Stop
  118. * looking after the first valid match is found.
  119. *
  120. * Return:
  121. * 0: No matching interface was found.
  122. * 1: A complete match was found and added to the devices list.
  123. */
  124. static int find_interface(struct usb_match const *match,
  125. struct usb_device *current,
  126. struct usb_device **devices)
  127. {
  128. int i, j;
  129. for (i = 0; i < current->config_descriptor->bNumInterfaces; ++i) {
  130. struct libusb_interface const *interface;
  131. interface = &current->config_descriptor->interface[i];
  132. for (j = 0; j < interface->num_altsetting; ++j) {
  133. struct libusb_interface_descriptor const *descriptor;
  134. descriptor = &interface->altsetting[j];
  135. if (check_match(&match->interface,
  136. descriptor->bInterfaceNumber) &&
  137. check_match(&match->altsetting,
  138. descriptor->bAlternateSetting) &&
  139. check_match(&match->class,
  140. descriptor->bInterfaceClass) &&
  141. check_match(&match->subclass,
  142. descriptor->bInterfaceSubClass) &&
  143. check_match(&match->protocol,
  144. descriptor->bInterfaceProtocol)) {
  145. current->interface_descriptor = descriptor;
  146. add_device(current, devices);
  147. msg_pdbg("USB: Found matching device\n");
  148. return 1;
  149. }
  150. }
  151. }
  152. return 0;
  153. }
  154. /*
  155. * Look through the configs of the current device for a match. Stop looking
  156. * after the first valid match is found.
  157. *
  158. * Return:
  159. * 0: All configurations successfully checked, one may have been added to
  160. * the list.
  161. * non-zero: There was a failure while checking for a match.
  162. */
  163. static int find_config(struct usb_match const *match,
  164. struct usb_device *current,
  165. struct libusb_device_descriptor const *device_descriptor,
  166. struct usb_device **devices)
  167. {
  168. int i;
  169. for (i = 0; i < device_descriptor->bNumConfigurations; ++i) {
  170. CHECK(LIBUSB(libusb_get_config_descriptor(
  171. current->device,
  172. i,
  173. &current->config_descriptor)),
  174. "USB: Failed to get config descriptor");
  175. if (check_match(&match->config,
  176. current->config_descriptor->
  177. bConfigurationValue) &&
  178. find_interface(match, current, devices))
  179. break;
  180. libusb_free_config_descriptor(current->config_descriptor);
  181. }
  182. return 0;
  183. }
  184. int usb_device_find(struct usb_match const *match, struct usb_device **devices)
  185. {
  186. libusb_device **list;
  187. ssize_t count;
  188. size_t i;
  189. *devices = NULL;
  190. CHECK(LIBUSB(count = libusb_get_device_list(NULL, &list)),
  191. "USB: Failed to get device list");
  192. for (i = 0; i < count; ++i) {
  193. struct libusb_device_descriptor descriptor;
  194. struct usb_device current = {
  195. .device = list[i],
  196. .handle = NULL,
  197. .next = NULL,
  198. };
  199. uint8_t bus = libusb_get_bus_number(list[i]);
  200. uint8_t address = libusb_get_device_address(list[i]);
  201. msg_pdbg("USB: Inspecting device (Bus %d, Address %d)\n",
  202. bus,
  203. address);
  204. CHECK(LIBUSB(libusb_get_device_descriptor(list[i],
  205. &descriptor)),
  206. "USB: Failed to get device descriptor");
  207. if (check_match(&match->vid, descriptor.idVendor) &&
  208. check_match(&match->pid, descriptor.idProduct) &&
  209. check_match(&match->bus, bus) &&
  210. check_match(&match->address, address))
  211. CHECK(find_config(match,
  212. &current,
  213. &descriptor,
  214. devices),
  215. "USB: Failed to find config");
  216. }
  217. libusb_free_device_list(list, 1);
  218. return (*devices == NULL);
  219. }
  220. /*
  221. * If the underlying libusb device is not open, open it.
  222. *
  223. * Return:
  224. * 0: The device was already open or was successfully opened.
  225. * non-zero: There was a failure while opening the device.
  226. */
  227. static int usb_device_open(struct usb_device *device)
  228. {
  229. if (device->handle == NULL)
  230. CHECK(LIBUSB(libusb_open(device->device, &device->handle)),
  231. "USB: Failed to open device\n");
  232. return 0;
  233. }
  234. int usb_device_show(char const *prefix, struct usb_device *device)
  235. {
  236. struct libusb_device_descriptor descriptor;
  237. unsigned char product[256];
  238. CHECK(usb_device_open(device), "USB: Failed to open device\n");
  239. CHECK(LIBUSB(libusb_get_device_descriptor(device->device, &descriptor)),
  240. "USB: Failed to get device descriptor\n");
  241. CHECK(LIBUSB(libusb_get_string_descriptor_ascii(
  242. device->handle,
  243. descriptor.iProduct,
  244. product,
  245. sizeof(product))),
  246. "USB: Failed to get device product string\n");
  247. product[255] = '\0';
  248. msg_perr("%sbus=0x%02x,address=0x%02x | %s\n",
  249. prefix,
  250. libusb_get_bus_number(device->device),
  251. libusb_get_device_address(device->device),
  252. product);
  253. return 0;
  254. }
  255. int usb_device_claim(struct usb_device *device)
  256. {
  257. int current_config;
  258. CHECK(usb_device_open(device), "USB: Failed to open device\n");
  259. CHECK(LIBUSB(libusb_get_configuration(device->handle,
  260. &current_config)),
  261. "USB: Failed to get current device configuration\n");
  262. if (current_config != device->config_descriptor->bConfigurationValue)
  263. CHECK(LIBUSB(libusb_set_configuration(
  264. device->handle,
  265. device->
  266. config_descriptor->
  267. bConfigurationValue)),
  268. "USB: Failed to set new configuration from %d to %d\n",
  269. current_config,
  270. device->config_descriptor->bConfigurationValue);
  271. CHECK(LIBUSB(libusb_set_auto_detach_kernel_driver(device->handle, 1)),
  272. "USB: Failed to enable auto kernel driver detach\n");
  273. CHECK(LIBUSB(libusb_claim_interface(device->handle,
  274. device->
  275. interface_descriptor->
  276. bInterfaceNumber)),
  277. "USB: Could not claim device interface %d\n",
  278. device->interface_descriptor->bInterfaceNumber);
  279. if (device->interface_descriptor->bAlternateSetting != 0)
  280. CHECK(LIBUSB(libusb_set_interface_alt_setting(
  281. device->handle,
  282. device->
  283. interface_descriptor->
  284. bInterfaceNumber,
  285. device->
  286. interface_descriptor->
  287. bAlternateSetting)),
  288. "USB: Failed to set alternate setting %d\n",
  289. device->interface_descriptor->bAlternateSetting);
  290. return 0;
  291. }
  292. struct usb_device *usb_device_free(struct usb_device *device)
  293. {
  294. struct usb_device *next = device->next;
  295. if (device->handle != NULL)
  296. libusb_close(device->handle);
  297. /*
  298. * This unref balances the ref added in the add_device function.
  299. */
  300. libusb_unref_device(device->device);
  301. libusb_free_config_descriptor(device->config_descriptor);
  302. free(device);
  303. return next;
  304. }