descriptor.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
  2. /*
  3. * USB descriptor handling functions for libusb
  4. * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
  5. * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libusbi.h"
  22. #include <string.h>
  23. #define DESC_HEADER_LENGTH 2
  24. /** @defgroup libusb_desc USB descriptors
  25. * This page details how to examine the various standard USB descriptors
  26. * for detected devices
  27. */
  28. #define READ_LE16(p) ((uint16_t) \
  29. (((uint16_t)((p)[1]) << 8) | \
  30. ((uint16_t)((p)[0]))))
  31. #define READ_LE32(p) ((uint32_t) \
  32. (((uint32_t)((p)[3]) << 24) | \
  33. ((uint32_t)((p)[2]) << 16) | \
  34. ((uint32_t)((p)[1]) << 8) | \
  35. ((uint32_t)((p)[0]))))
  36. static void parse_descriptor(const void *source, const char *descriptor, void *dest)
  37. {
  38. const uint8_t *sp = source;
  39. uint8_t *dp = dest;
  40. char field_type;
  41. while (*descriptor) {
  42. field_type = *descriptor++;
  43. switch (field_type) {
  44. case 'b': /* 8-bit byte */
  45. *dp++ = *sp++;
  46. break;
  47. case 'w': /* 16-bit word, convert from little endian to CPU */
  48. dp += ((uintptr_t)dp & 1); /* Align to 16-bit word boundary */
  49. *((uint16_t *)dp) = READ_LE16(sp);
  50. sp += 2;
  51. dp += 2;
  52. break;
  53. case 'd': /* 32-bit word, convert from little endian to CPU */
  54. dp += 4 - ((uintptr_t)dp & 3); /* Align to 32-bit word boundary */
  55. *((uint32_t *)dp) = READ_LE32(sp);
  56. sp += 4;
  57. dp += 4;
  58. break;
  59. case 'u': /* 16 byte UUID */
  60. memcpy(dp, sp, 16);
  61. sp += 16;
  62. dp += 16;
  63. break;
  64. }
  65. }
  66. }
  67. static void clear_endpoint(struct libusb_endpoint_descriptor *endpoint)
  68. {
  69. free((void *)endpoint->extra);
  70. }
  71. static int parse_endpoint(struct libusb_context *ctx,
  72. struct libusb_endpoint_descriptor *endpoint, const uint8_t *buffer, int size)
  73. {
  74. const struct usbi_descriptor_header *header;
  75. const uint8_t *begin;
  76. void *extra;
  77. int parsed = 0;
  78. int len;
  79. if (size < DESC_HEADER_LENGTH) {
  80. usbi_err(ctx, "short endpoint descriptor read %d/%d",
  81. size, DESC_HEADER_LENGTH);
  82. return LIBUSB_ERROR_IO;
  83. }
  84. header = (const struct usbi_descriptor_header *)buffer;
  85. if (header->bDescriptorType != LIBUSB_DT_ENDPOINT) {
  86. usbi_err(ctx, "unexpected descriptor 0x%x (expected 0x%x)",
  87. header->bDescriptorType, LIBUSB_DT_ENDPOINT);
  88. return parsed;
  89. } else if (header->bLength < LIBUSB_DT_ENDPOINT_SIZE) {
  90. usbi_err(ctx, "invalid endpoint bLength (%u)", header->bLength);
  91. return LIBUSB_ERROR_IO;
  92. } else if (header->bLength > size) {
  93. usbi_warn(ctx, "short endpoint descriptor read %d/%u",
  94. size, header->bLength);
  95. return parsed;
  96. }
  97. if (header->bLength >= LIBUSB_DT_ENDPOINT_AUDIO_SIZE)
  98. parse_descriptor(buffer, "bbbbwbbb", endpoint);
  99. else
  100. parse_descriptor(buffer, "bbbbwb", endpoint);
  101. buffer += header->bLength;
  102. size -= header->bLength;
  103. parsed += header->bLength;
  104. /* Skip over the rest of the Class Specific or Vendor Specific */
  105. /* descriptors */
  106. begin = buffer;
  107. while (size >= DESC_HEADER_LENGTH) {
  108. header = (const struct usbi_descriptor_header *)buffer;
  109. if (header->bLength < DESC_HEADER_LENGTH) {
  110. usbi_err(ctx, "invalid extra ep desc len (%u)",
  111. header->bLength);
  112. return LIBUSB_ERROR_IO;
  113. } else if (header->bLength > size) {
  114. usbi_warn(ctx, "short extra ep desc read %d/%u",
  115. size, header->bLength);
  116. return parsed;
  117. }
  118. /* If we find another "proper" descriptor then we're done */
  119. if (header->bDescriptorType == LIBUSB_DT_ENDPOINT ||
  120. header->bDescriptorType == LIBUSB_DT_INTERFACE ||
  121. header->bDescriptorType == LIBUSB_DT_CONFIG ||
  122. header->bDescriptorType == LIBUSB_DT_DEVICE)
  123. break;
  124. usbi_dbg(ctx, "skipping descriptor 0x%x", header->bDescriptorType);
  125. buffer += header->bLength;
  126. size -= header->bLength;
  127. parsed += header->bLength;
  128. }
  129. /* Copy any unknown descriptors into a storage area for drivers */
  130. /* to later parse */
  131. len = (int)(buffer - begin);
  132. if (len <= 0)
  133. return parsed;
  134. extra = malloc((size_t)len);
  135. if (!extra)
  136. return LIBUSB_ERROR_NO_MEM;
  137. memcpy(extra, begin, len);
  138. endpoint->extra = extra;
  139. endpoint->extra_length = len;
  140. return parsed;
  141. }
  142. static void clear_interface(struct libusb_interface *usb_interface)
  143. {
  144. int i;
  145. if (usb_interface->altsetting) {
  146. for (i = 0; i < usb_interface->num_altsetting; i++) {
  147. struct libusb_interface_descriptor *ifp =
  148. (struct libusb_interface_descriptor *)
  149. usb_interface->altsetting + i;
  150. free((void *)ifp->extra);
  151. if (ifp->endpoint) {
  152. uint8_t j;
  153. for (j = 0; j < ifp->bNumEndpoints; j++)
  154. clear_endpoint((struct libusb_endpoint_descriptor *)
  155. ifp->endpoint + j);
  156. }
  157. free((void *)ifp->endpoint);
  158. }
  159. }
  160. free((void *)usb_interface->altsetting);
  161. usb_interface->altsetting = NULL;
  162. }
  163. static int parse_interface(libusb_context *ctx,
  164. struct libusb_interface *usb_interface, const uint8_t *buffer, int size)
  165. {
  166. int len;
  167. int r;
  168. int parsed = 0;
  169. int interface_number = -1;
  170. const struct usbi_descriptor_header *header;
  171. const struct usbi_interface_descriptor *if_desc;
  172. struct libusb_interface_descriptor *ifp;
  173. const uint8_t *begin;
  174. while (size >= LIBUSB_DT_INTERFACE_SIZE) {
  175. struct libusb_interface_descriptor *altsetting;
  176. altsetting = realloc((void *)usb_interface->altsetting,
  177. sizeof(*altsetting) * (size_t)(usb_interface->num_altsetting + 1));
  178. if (!altsetting) {
  179. r = LIBUSB_ERROR_NO_MEM;
  180. goto err;
  181. }
  182. usb_interface->altsetting = altsetting;
  183. ifp = altsetting + usb_interface->num_altsetting;
  184. parse_descriptor(buffer, "bbbbbbbbb", ifp);
  185. if (ifp->bDescriptorType != LIBUSB_DT_INTERFACE) {
  186. usbi_err(ctx, "unexpected descriptor 0x%x (expected 0x%x)",
  187. ifp->bDescriptorType, LIBUSB_DT_INTERFACE);
  188. return parsed;
  189. } else if (ifp->bLength < LIBUSB_DT_INTERFACE_SIZE) {
  190. usbi_err(ctx, "invalid interface bLength (%u)",
  191. ifp->bLength);
  192. r = LIBUSB_ERROR_IO;
  193. goto err;
  194. } else if (ifp->bLength > size) {
  195. usbi_warn(ctx, "short intf descriptor read %d/%u",
  196. size, ifp->bLength);
  197. return parsed;
  198. } else if (ifp->bNumEndpoints > USB_MAXENDPOINTS) {
  199. usbi_err(ctx, "too many endpoints (%u)", ifp->bNumEndpoints);
  200. r = LIBUSB_ERROR_IO;
  201. goto err;
  202. }
  203. usb_interface->num_altsetting++;
  204. ifp->extra = NULL;
  205. ifp->extra_length = 0;
  206. ifp->endpoint = NULL;
  207. if (interface_number == -1)
  208. interface_number = ifp->bInterfaceNumber;
  209. /* Skip over the interface */
  210. buffer += ifp->bLength;
  211. parsed += ifp->bLength;
  212. size -= ifp->bLength;
  213. begin = buffer;
  214. /* Skip over any interface, class or vendor descriptors */
  215. while (size >= DESC_HEADER_LENGTH) {
  216. header = (const struct usbi_descriptor_header *)buffer;
  217. if (header->bLength < DESC_HEADER_LENGTH) {
  218. usbi_err(ctx,
  219. "invalid extra intf desc len (%u)",
  220. header->bLength);
  221. r = LIBUSB_ERROR_IO;
  222. goto err;
  223. } else if (header->bLength > size) {
  224. usbi_warn(ctx,
  225. "short extra intf desc read %d/%u",
  226. size, header->bLength);
  227. return parsed;
  228. }
  229. /* If we find another "proper" descriptor then we're done */
  230. if (header->bDescriptorType == LIBUSB_DT_INTERFACE ||
  231. header->bDescriptorType == LIBUSB_DT_ENDPOINT ||
  232. header->bDescriptorType == LIBUSB_DT_CONFIG ||
  233. header->bDescriptorType == LIBUSB_DT_DEVICE)
  234. break;
  235. buffer += header->bLength;
  236. parsed += header->bLength;
  237. size -= header->bLength;
  238. }
  239. /* Copy any unknown descriptors into a storage area for */
  240. /* drivers to later parse */
  241. len = (int)(buffer - begin);
  242. if (len > 0) {
  243. void *extra = malloc((size_t)len);
  244. if (!extra) {
  245. r = LIBUSB_ERROR_NO_MEM;
  246. goto err;
  247. }
  248. memcpy(extra, begin, len);
  249. ifp->extra = extra;
  250. ifp->extra_length = len;
  251. }
  252. if (ifp->bNumEndpoints > 0) {
  253. struct libusb_endpoint_descriptor *endpoint;
  254. uint8_t i;
  255. endpoint = calloc(ifp->bNumEndpoints, sizeof(*endpoint));
  256. if (!endpoint) {
  257. r = LIBUSB_ERROR_NO_MEM;
  258. goto err;
  259. }
  260. ifp->endpoint = endpoint;
  261. for (i = 0; i < ifp->bNumEndpoints; i++) {
  262. r = parse_endpoint(ctx, endpoint + i, buffer, size);
  263. if (r < 0)
  264. goto err;
  265. if (r == 0) {
  266. ifp->bNumEndpoints = i;
  267. break;
  268. }
  269. buffer += r;
  270. parsed += r;
  271. size -= r;
  272. }
  273. }
  274. /* We check to see if it's an alternate to this one */
  275. if_desc = (const struct usbi_interface_descriptor *)buffer;
  276. if (size < LIBUSB_DT_INTERFACE_SIZE ||
  277. if_desc->bDescriptorType != LIBUSB_DT_INTERFACE ||
  278. if_desc->bInterfaceNumber != interface_number)
  279. return parsed;
  280. }
  281. return parsed;
  282. err:
  283. clear_interface(usb_interface);
  284. return r;
  285. }
  286. static void clear_configuration(struct libusb_config_descriptor *config)
  287. {
  288. uint8_t i;
  289. if (config->interface) {
  290. for (i = 0; i < config->bNumInterfaces; i++)
  291. clear_interface((struct libusb_interface *)
  292. config->interface + i);
  293. }
  294. free((void *)config->interface);
  295. free((void *)config->extra);
  296. }
  297. static int parse_configuration(struct libusb_context *ctx,
  298. struct libusb_config_descriptor *config, const uint8_t *buffer, int size)
  299. {
  300. uint8_t i;
  301. int r;
  302. const struct usbi_descriptor_header *header;
  303. struct libusb_interface *usb_interface;
  304. if (size < LIBUSB_DT_CONFIG_SIZE) {
  305. usbi_err(ctx, "short config descriptor read %d/%d",
  306. size, LIBUSB_DT_CONFIG_SIZE);
  307. return LIBUSB_ERROR_IO;
  308. }
  309. parse_descriptor(buffer, "bbwbbbbb", config);
  310. if (config->bDescriptorType != LIBUSB_DT_CONFIG) {
  311. usbi_err(ctx, "unexpected descriptor 0x%x (expected 0x%x)",
  312. config->bDescriptorType, LIBUSB_DT_CONFIG);
  313. return LIBUSB_ERROR_IO;
  314. } else if (config->bLength < LIBUSB_DT_CONFIG_SIZE) {
  315. usbi_err(ctx, "invalid config bLength (%u)", config->bLength);
  316. return LIBUSB_ERROR_IO;
  317. } else if (config->bLength > size) {
  318. usbi_err(ctx, "short config descriptor read %d/%u",
  319. size, config->bLength);
  320. return LIBUSB_ERROR_IO;
  321. } else if (config->bNumInterfaces > USB_MAXINTERFACES) {
  322. usbi_err(ctx, "too many interfaces (%u)", config->bNumInterfaces);
  323. return LIBUSB_ERROR_IO;
  324. }
  325. usb_interface = calloc(config->bNumInterfaces, sizeof(*usb_interface));
  326. if (!usb_interface)
  327. return LIBUSB_ERROR_NO_MEM;
  328. config->interface = usb_interface;
  329. buffer += config->bLength;
  330. size -= config->bLength;
  331. for (i = 0; i < config->bNumInterfaces; i++) {
  332. int len;
  333. const uint8_t *begin;
  334. /* Skip over the rest of the Class Specific or Vendor */
  335. /* Specific descriptors */
  336. begin = buffer;
  337. while (size >= DESC_HEADER_LENGTH) {
  338. header = (const struct usbi_descriptor_header *)buffer;
  339. if (header->bLength < DESC_HEADER_LENGTH) {
  340. usbi_err(ctx,
  341. "invalid extra config desc len (%u)",
  342. header->bLength);
  343. r = LIBUSB_ERROR_IO;
  344. goto err;
  345. } else if (header->bLength > size) {
  346. usbi_warn(ctx,
  347. "short extra config desc read %d/%u",
  348. size, header->bLength);
  349. config->bNumInterfaces = i;
  350. return size;
  351. }
  352. /* If we find another "proper" descriptor then we're done */
  353. if (header->bDescriptorType == LIBUSB_DT_ENDPOINT ||
  354. header->bDescriptorType == LIBUSB_DT_INTERFACE ||
  355. header->bDescriptorType == LIBUSB_DT_CONFIG ||
  356. header->bDescriptorType == LIBUSB_DT_DEVICE)
  357. break;
  358. usbi_dbg(ctx, "skipping descriptor 0x%x", header->bDescriptorType);
  359. buffer += header->bLength;
  360. size -= header->bLength;
  361. }
  362. /* Copy any unknown descriptors into a storage area for */
  363. /* drivers to later parse */
  364. len = (int)(buffer - begin);
  365. if (len > 0) {
  366. uint8_t *extra = realloc((void *)config->extra,
  367. (size_t)(config->extra_length + len));
  368. if (!extra) {
  369. r = LIBUSB_ERROR_NO_MEM;
  370. goto err;
  371. }
  372. memcpy(extra + config->extra_length, begin, len);
  373. config->extra = extra;
  374. config->extra_length += len;
  375. }
  376. r = parse_interface(ctx, usb_interface + i, buffer, size);
  377. if (r < 0)
  378. goto err;
  379. if (r == 0) {
  380. config->bNumInterfaces = i;
  381. break;
  382. }
  383. buffer += r;
  384. size -= r;
  385. }
  386. return size;
  387. err:
  388. clear_configuration(config);
  389. return r;
  390. }
  391. static int raw_desc_to_config(struct libusb_context *ctx,
  392. const uint8_t *buf, int size, struct libusb_config_descriptor **config)
  393. {
  394. struct libusb_config_descriptor *_config = calloc(1, sizeof(*_config));
  395. int r;
  396. if (!_config)
  397. return LIBUSB_ERROR_NO_MEM;
  398. r = parse_configuration(ctx, _config, buf, size);
  399. if (r < 0) {
  400. usbi_err(ctx, "parse_configuration failed with error %d", r);
  401. free(_config);
  402. return r;
  403. } else if (r > 0) {
  404. usbi_warn(ctx, "still %d bytes of descriptor data left", r);
  405. }
  406. *config = _config;
  407. return LIBUSB_SUCCESS;
  408. }
  409. static int get_active_config_descriptor(struct libusb_device *dev,
  410. uint8_t *buffer, size_t size)
  411. {
  412. int r = usbi_backend.get_active_config_descriptor(dev, buffer, size);
  413. if (r < 0)
  414. return r;
  415. if (r < LIBUSB_DT_CONFIG_SIZE) {
  416. usbi_err(DEVICE_CTX(dev), "short config descriptor read %d/%d",
  417. r, LIBUSB_DT_CONFIG_SIZE);
  418. return LIBUSB_ERROR_IO;
  419. } else if (r != (int)size) {
  420. usbi_warn(DEVICE_CTX(dev), "short config descriptor read %d/%d",
  421. r, (int)size);
  422. }
  423. return r;
  424. }
  425. static int get_config_descriptor(struct libusb_device *dev, uint8_t config_idx,
  426. uint8_t *buffer, size_t size)
  427. {
  428. int r = usbi_backend.get_config_descriptor(dev, config_idx, buffer, size);
  429. if (r < 0)
  430. return r;
  431. if (r < LIBUSB_DT_CONFIG_SIZE) {
  432. usbi_err(DEVICE_CTX(dev), "short config descriptor read %d/%d",
  433. r, LIBUSB_DT_CONFIG_SIZE);
  434. return LIBUSB_ERROR_IO;
  435. } else if (r != (int)size) {
  436. usbi_warn(DEVICE_CTX(dev), "short config descriptor read %d/%d",
  437. r, (int)size);
  438. }
  439. return r;
  440. }
  441. /** \ingroup libusb_desc
  442. * Get the USB device descriptor for a given device.
  443. *
  444. * This is a non-blocking function; the device descriptor is cached in memory.
  445. *
  446. * Note since libusb-1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102, this
  447. * function always succeeds.
  448. *
  449. * \param dev the device
  450. * \param desc output location for the descriptor data
  451. * \returns 0 on success or a LIBUSB_ERROR code on failure
  452. */
  453. int API_EXPORTED libusb_get_device_descriptor(libusb_device *dev,
  454. struct libusb_device_descriptor *desc)
  455. {
  456. usbi_dbg(DEVICE_CTX(dev), " ");
  457. static_assert(sizeof(dev->device_descriptor) == LIBUSB_DT_DEVICE_SIZE,
  458. "struct libusb_device_descriptor is not expected size");
  459. *desc = dev->device_descriptor;
  460. return 0;
  461. }
  462. /** \ingroup libusb_desc
  463. * Get the USB configuration descriptor for the currently active configuration.
  464. * This is a non-blocking function which does not involve any requests being
  465. * sent to the device.
  466. *
  467. * \param dev a device
  468. * \param config output location for the USB configuration descriptor. Only
  469. * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
  470. * after use.
  471. * \returns 0 on success
  472. * \returns LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
  473. * \returns another LIBUSB_ERROR code on error
  474. * \see libusb_get_config_descriptor
  475. */
  476. int API_EXPORTED libusb_get_active_config_descriptor(libusb_device *dev,
  477. struct libusb_config_descriptor **config)
  478. {
  479. union usbi_config_desc_buf _config;
  480. uint16_t config_len;
  481. uint8_t *buf;
  482. int r;
  483. r = get_active_config_descriptor(dev, _config.buf, sizeof(_config.buf));
  484. if (r < 0)
  485. return r;
  486. config_len = libusb_le16_to_cpu(_config.desc.wTotalLength);
  487. buf = malloc(config_len);
  488. if (!buf)
  489. return LIBUSB_ERROR_NO_MEM;
  490. r = get_active_config_descriptor(dev, buf, config_len);
  491. if (r >= 0)
  492. r = raw_desc_to_config(DEVICE_CTX(dev), buf, r, config);
  493. free(buf);
  494. return r;
  495. }
  496. /** \ingroup libusb_desc
  497. * Get a USB configuration descriptor based on its index.
  498. * This is a non-blocking function which does not involve any requests being
  499. * sent to the device.
  500. *
  501. * \param dev a device
  502. * \param config_index the index of the configuration you wish to retrieve
  503. * \param config output location for the USB configuration descriptor. Only
  504. * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
  505. * after use.
  506. * \returns 0 on success
  507. * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
  508. * \returns another LIBUSB_ERROR code on error
  509. * \see libusb_get_active_config_descriptor()
  510. * \see libusb_get_config_descriptor_by_value()
  511. */
  512. int API_EXPORTED libusb_get_config_descriptor(libusb_device *dev,
  513. uint8_t config_index, struct libusb_config_descriptor **config)
  514. {
  515. union usbi_config_desc_buf _config;
  516. uint16_t config_len;
  517. uint8_t *buf;
  518. int r;
  519. usbi_dbg(DEVICE_CTX(dev), "index %u", config_index);
  520. if (config_index >= dev->device_descriptor.bNumConfigurations)
  521. return LIBUSB_ERROR_NOT_FOUND;
  522. r = get_config_descriptor(dev, config_index, _config.buf, sizeof(_config.buf));
  523. if (r < 0)
  524. return r;
  525. config_len = libusb_le16_to_cpu(_config.desc.wTotalLength);
  526. buf = malloc(config_len);
  527. if (!buf)
  528. return LIBUSB_ERROR_NO_MEM;
  529. r = get_config_descriptor(dev, config_index, buf, config_len);
  530. if (r >= 0)
  531. r = raw_desc_to_config(DEVICE_CTX(dev), buf, r, config);
  532. free(buf);
  533. return r;
  534. }
  535. /** \ingroup libusb_desc
  536. * Get a USB configuration descriptor with a specific bConfigurationValue.
  537. * This is a non-blocking function which does not involve any requests being
  538. * sent to the device.
  539. *
  540. * \param dev a device
  541. * \param bConfigurationValue the bConfigurationValue of the configuration you
  542. * wish to retrieve
  543. * \param config output location for the USB configuration descriptor. Only
  544. * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
  545. * after use.
  546. * \returns 0 on success
  547. * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
  548. * \returns another LIBUSB_ERROR code on error
  549. * \see libusb_get_active_config_descriptor()
  550. * \see libusb_get_config_descriptor()
  551. */
  552. int API_EXPORTED libusb_get_config_descriptor_by_value(libusb_device *dev,
  553. uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
  554. {
  555. uint8_t idx;
  556. int r;
  557. if (usbi_backend.get_config_descriptor_by_value) {
  558. void *buf;
  559. r = usbi_backend.get_config_descriptor_by_value(dev,
  560. bConfigurationValue, &buf);
  561. if (r < 0)
  562. return r;
  563. return raw_desc_to_config(DEVICE_CTX(dev), buf, r, config);
  564. }
  565. usbi_dbg(DEVICE_CTX(dev), "value %u", bConfigurationValue);
  566. for (idx = 0; idx < dev->device_descriptor.bNumConfigurations; idx++) {
  567. union usbi_config_desc_buf _config;
  568. r = get_config_descriptor(dev, idx, _config.buf, sizeof(_config.buf));
  569. if (r < 0)
  570. return r;
  571. if (_config.desc.bConfigurationValue == bConfigurationValue)
  572. return libusb_get_config_descriptor(dev, idx, config);
  573. }
  574. return LIBUSB_ERROR_NOT_FOUND;
  575. }
  576. /** \ingroup libusb_desc
  577. * Free a configuration descriptor obtained from
  578. * libusb_get_active_config_descriptor() or libusb_get_config_descriptor().
  579. * It is safe to call this function with a NULL config parameter, in which
  580. * case the function simply returns.
  581. *
  582. * \param config the configuration descriptor to free
  583. */
  584. void API_EXPORTED libusb_free_config_descriptor(
  585. struct libusb_config_descriptor *config)
  586. {
  587. if (!config)
  588. return;
  589. clear_configuration(config);
  590. free(config);
  591. }
  592. /** \ingroup libusb_desc
  593. * Get an endpoints superspeed endpoint companion descriptor (if any)
  594. *
  595. * \param ctx the context to operate on, or NULL for the default context
  596. * \param endpoint endpoint descriptor from which to get the superspeed
  597. * endpoint companion descriptor
  598. * \param ep_comp output location for the superspeed endpoint companion
  599. * descriptor. Only valid if 0 was returned. Must be freed with
  600. * libusb_free_ss_endpoint_companion_descriptor() after use.
  601. * \returns 0 on success
  602. * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
  603. * \returns another LIBUSB_ERROR code on error
  604. */
  605. int API_EXPORTED libusb_get_ss_endpoint_companion_descriptor(
  606. libusb_context *ctx,
  607. const struct libusb_endpoint_descriptor *endpoint,
  608. struct libusb_ss_endpoint_companion_descriptor **ep_comp)
  609. {
  610. struct usbi_descriptor_header *header;
  611. const uint8_t *buffer = endpoint->extra;
  612. int size = endpoint->extra_length;
  613. *ep_comp = NULL;
  614. while (size >= DESC_HEADER_LENGTH) {
  615. header = (struct usbi_descriptor_header *)buffer;
  616. if (header->bDescriptorType != LIBUSB_DT_SS_ENDPOINT_COMPANION) {
  617. if (header->bLength < DESC_HEADER_LENGTH) {
  618. usbi_err(ctx, "invalid descriptor length %u",
  619. header->bLength);
  620. return LIBUSB_ERROR_IO;
  621. }
  622. buffer += header->bLength;
  623. size -= header->bLength;
  624. continue;
  625. } else if (header->bLength < LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE) {
  626. usbi_err(ctx, "invalid ss-ep-comp-desc length %u",
  627. header->bLength);
  628. return LIBUSB_ERROR_IO;
  629. } else if (header->bLength > size) {
  630. usbi_err(ctx, "short ss-ep-comp-desc read %d/%u",
  631. size, header->bLength);
  632. return LIBUSB_ERROR_IO;
  633. }
  634. *ep_comp = malloc(sizeof(**ep_comp));
  635. if (!*ep_comp)
  636. return LIBUSB_ERROR_NO_MEM;
  637. parse_descriptor(buffer, "bbbbw", *ep_comp);
  638. return LIBUSB_SUCCESS;
  639. }
  640. return LIBUSB_ERROR_NOT_FOUND;
  641. }
  642. /** \ingroup libusb_desc
  643. * Free a superspeed endpoint companion descriptor obtained from
  644. * libusb_get_ss_endpoint_companion_descriptor().
  645. * It is safe to call this function with a NULL ep_comp parameter, in which
  646. * case the function simply returns.
  647. *
  648. * \param ep_comp the superspeed endpoint companion descriptor to free
  649. */
  650. void API_EXPORTED libusb_free_ss_endpoint_companion_descriptor(
  651. struct libusb_ss_endpoint_companion_descriptor *ep_comp)
  652. {
  653. free(ep_comp);
  654. }
  655. static int parse_bos(struct libusb_context *ctx,
  656. struct libusb_bos_descriptor **bos,
  657. const uint8_t *buffer, int size)
  658. {
  659. struct libusb_bos_descriptor *_bos;
  660. const struct usbi_bos_descriptor *bos_desc;
  661. const struct usbi_descriptor_header *header;
  662. uint8_t i;
  663. if (size < LIBUSB_DT_BOS_SIZE) {
  664. usbi_err(ctx, "short bos descriptor read %d/%d",
  665. size, LIBUSB_DT_BOS_SIZE);
  666. return LIBUSB_ERROR_IO;
  667. }
  668. bos_desc = (const struct usbi_bos_descriptor *)buffer;
  669. if (bos_desc->bDescriptorType != LIBUSB_DT_BOS) {
  670. usbi_err(ctx, "unexpected descriptor 0x%x (expected 0x%x)",
  671. bos_desc->bDescriptorType, LIBUSB_DT_BOS);
  672. return LIBUSB_ERROR_IO;
  673. } else if (bos_desc->bLength < LIBUSB_DT_BOS_SIZE) {
  674. usbi_err(ctx, "invalid bos bLength (%u)", bos_desc->bLength);
  675. return LIBUSB_ERROR_IO;
  676. } else if (bos_desc->bLength > size) {
  677. usbi_err(ctx, "short bos descriptor read %d/%u",
  678. size, bos_desc->bLength);
  679. return LIBUSB_ERROR_IO;
  680. }
  681. _bos = calloc(1, sizeof(*_bos) + bos_desc->bNumDeviceCaps * sizeof(void *));
  682. if (!_bos)
  683. return LIBUSB_ERROR_NO_MEM;
  684. parse_descriptor(buffer, "bbwb", _bos);
  685. buffer += _bos->bLength;
  686. size -= _bos->bLength;
  687. /* Get the device capability descriptors */
  688. for (i = 0; i < _bos->bNumDeviceCaps; i++) {
  689. if (size < LIBUSB_DT_DEVICE_CAPABILITY_SIZE) {
  690. usbi_warn(ctx, "short dev-cap descriptor read %d/%d",
  691. size, LIBUSB_DT_DEVICE_CAPABILITY_SIZE);
  692. break;
  693. }
  694. header = (const struct usbi_descriptor_header *)buffer;
  695. if (header->bDescriptorType != LIBUSB_DT_DEVICE_CAPABILITY) {
  696. usbi_warn(ctx, "unexpected descriptor 0x%x (expected 0x%x)",
  697. header->bDescriptorType, LIBUSB_DT_DEVICE_CAPABILITY);
  698. break;
  699. } else if (header->bLength < LIBUSB_DT_DEVICE_CAPABILITY_SIZE) {
  700. usbi_err(ctx, "invalid dev-cap bLength (%u)",
  701. header->bLength);
  702. libusb_free_bos_descriptor(_bos);
  703. return LIBUSB_ERROR_IO;
  704. } else if (header->bLength > size) {
  705. usbi_warn(ctx, "short dev-cap descriptor read %d/%u",
  706. size, header->bLength);
  707. break;
  708. }
  709. _bos->dev_capability[i] = malloc(header->bLength);
  710. if (!_bos->dev_capability[i]) {
  711. libusb_free_bos_descriptor(_bos);
  712. return LIBUSB_ERROR_NO_MEM;
  713. }
  714. memcpy(_bos->dev_capability[i], buffer, header->bLength);
  715. buffer += header->bLength;
  716. size -= header->bLength;
  717. }
  718. _bos->bNumDeviceCaps = i;
  719. *bos = _bos;
  720. return LIBUSB_SUCCESS;
  721. }
  722. /** \ingroup libusb_desc
  723. * Get a Binary Object Store (BOS) descriptor
  724. * This is a BLOCKING function, which will send requests to the device.
  725. *
  726. * \param dev_handle the handle of an open libusb device
  727. * \param bos output location for the BOS descriptor. Only valid if 0 was returned.
  728. * Must be freed with \ref libusb_free_bos_descriptor() after use.
  729. * \returns 0 on success
  730. * \returns LIBUSB_ERROR_NOT_FOUND if the device doesn't have a BOS descriptor
  731. * \returns another LIBUSB_ERROR code on error
  732. */
  733. int API_EXPORTED libusb_get_bos_descriptor(libusb_device_handle *dev_handle,
  734. struct libusb_bos_descriptor **bos)
  735. {
  736. union usbi_bos_desc_buf _bos;
  737. uint16_t bos_len;
  738. uint8_t *bos_data;
  739. int r;
  740. struct libusb_context *ctx = HANDLE_CTX(dev_handle);
  741. /* Read the BOS. This generates 2 requests on the bus,
  742. * one for the header, and one for the full BOS */
  743. r = libusb_get_descriptor(dev_handle, LIBUSB_DT_BOS, 0, _bos.buf, sizeof(_bos.buf));
  744. if (r < 0) {
  745. if (r != LIBUSB_ERROR_PIPE)
  746. usbi_err(ctx, "failed to read BOS (%d)", r);
  747. return r;
  748. }
  749. if (r < LIBUSB_DT_BOS_SIZE) {
  750. usbi_err(ctx, "short BOS read %d/%d",
  751. r, LIBUSB_DT_BOS_SIZE);
  752. return LIBUSB_ERROR_IO;
  753. }
  754. bos_len = libusb_le16_to_cpu(_bos.desc.wTotalLength);
  755. usbi_dbg(ctx, "found BOS descriptor: size %u bytes, %u capabilities",
  756. bos_len, _bos.desc.bNumDeviceCaps);
  757. bos_data = calloc(1, bos_len);
  758. if (!bos_data)
  759. return LIBUSB_ERROR_NO_MEM;
  760. r = libusb_get_descriptor(dev_handle, LIBUSB_DT_BOS, 0, bos_data, bos_len);
  761. if (r >= 0) {
  762. if (r != (int)bos_len)
  763. usbi_warn(ctx, "short BOS read %d/%u", r, bos_len);
  764. r = parse_bos(HANDLE_CTX(dev_handle), bos, bos_data, r);
  765. } else {
  766. usbi_err(ctx, "failed to read BOS (%d)", r);
  767. }
  768. free(bos_data);
  769. return r;
  770. }
  771. /** \ingroup libusb_desc
  772. * Free a BOS descriptor obtained from libusb_get_bos_descriptor().
  773. * It is safe to call this function with a NULL bos parameter, in which
  774. * case the function simply returns.
  775. *
  776. * \param bos the BOS descriptor to free
  777. */
  778. void API_EXPORTED libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos)
  779. {
  780. uint8_t i;
  781. if (!bos)
  782. return;
  783. for (i = 0; i < bos->bNumDeviceCaps; i++)
  784. free(bos->dev_capability[i]);
  785. free(bos);
  786. }
  787. /** \ingroup libusb_desc
  788. * Get an USB 2.0 Extension descriptor
  789. *
  790. * \param ctx the context to operate on, or NULL for the default context
  791. * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
  792. * \ref libusb_capability_type::LIBUSB_BT_USB_2_0_EXTENSION
  793. * LIBUSB_BT_USB_2_0_EXTENSION
  794. * \param usb_2_0_extension output location for the USB 2.0 Extension
  795. * descriptor. Only valid if 0 was returned. Must be freed with
  796. * libusb_free_usb_2_0_extension_descriptor() after use.
  797. * \returns 0 on success
  798. * \returns a LIBUSB_ERROR code on error
  799. */
  800. int API_EXPORTED libusb_get_usb_2_0_extension_descriptor(
  801. libusb_context *ctx,
  802. struct libusb_bos_dev_capability_descriptor *dev_cap,
  803. struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension)
  804. {
  805. struct libusb_usb_2_0_extension_descriptor *_usb_2_0_extension;
  806. if (dev_cap->bDevCapabilityType != LIBUSB_BT_USB_2_0_EXTENSION) {
  807. usbi_err(ctx, "unexpected bDevCapabilityType 0x%x (expected 0x%x)",
  808. dev_cap->bDevCapabilityType,
  809. LIBUSB_BT_USB_2_0_EXTENSION);
  810. return LIBUSB_ERROR_INVALID_PARAM;
  811. } else if (dev_cap->bLength < LIBUSB_BT_USB_2_0_EXTENSION_SIZE) {
  812. usbi_err(ctx, "short dev-cap descriptor read %u/%d",
  813. dev_cap->bLength, LIBUSB_BT_USB_2_0_EXTENSION_SIZE);
  814. return LIBUSB_ERROR_IO;
  815. }
  816. _usb_2_0_extension = malloc(sizeof(*_usb_2_0_extension));
  817. if (!_usb_2_0_extension)
  818. return LIBUSB_ERROR_NO_MEM;
  819. parse_descriptor(dev_cap, "bbbd", _usb_2_0_extension);
  820. *usb_2_0_extension = _usb_2_0_extension;
  821. return LIBUSB_SUCCESS;
  822. }
  823. /** \ingroup libusb_desc
  824. * Free a USB 2.0 Extension descriptor obtained from
  825. * libusb_get_usb_2_0_extension_descriptor().
  826. * It is safe to call this function with a NULL usb_2_0_extension parameter,
  827. * in which case the function simply returns.
  828. *
  829. * \param usb_2_0_extension the USB 2.0 Extension descriptor to free
  830. */
  831. void API_EXPORTED libusb_free_usb_2_0_extension_descriptor(
  832. struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension)
  833. {
  834. free(usb_2_0_extension);
  835. }
  836. /** \ingroup libusb_desc
  837. * Get a SuperSpeed USB Device Capability descriptor
  838. *
  839. * \param ctx the context to operate on, or NULL for the default context
  840. * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
  841. * \ref libusb_capability_type::LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
  842. * LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
  843. * \param ss_usb_device_cap output location for the SuperSpeed USB Device
  844. * Capability descriptor. Only valid if 0 was returned. Must be freed with
  845. * libusb_free_ss_usb_device_capability_descriptor() after use.
  846. * \returns 0 on success
  847. * \returns a LIBUSB_ERROR code on error
  848. */
  849. int API_EXPORTED libusb_get_ss_usb_device_capability_descriptor(
  850. libusb_context *ctx,
  851. struct libusb_bos_dev_capability_descriptor *dev_cap,
  852. struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap)
  853. {
  854. struct libusb_ss_usb_device_capability_descriptor *_ss_usb_device_cap;
  855. if (dev_cap->bDevCapabilityType != LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
  856. usbi_err(ctx, "unexpected bDevCapabilityType 0x%x (expected 0x%x)",
  857. dev_cap->bDevCapabilityType,
  858. LIBUSB_BT_SS_USB_DEVICE_CAPABILITY);
  859. return LIBUSB_ERROR_INVALID_PARAM;
  860. } else if (dev_cap->bLength < LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE) {
  861. usbi_err(ctx, "short dev-cap descriptor read %u/%d",
  862. dev_cap->bLength, LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE);
  863. return LIBUSB_ERROR_IO;
  864. }
  865. _ss_usb_device_cap = malloc(sizeof(*_ss_usb_device_cap));
  866. if (!_ss_usb_device_cap)
  867. return LIBUSB_ERROR_NO_MEM;
  868. parse_descriptor(dev_cap, "bbbbwbbw", _ss_usb_device_cap);
  869. *ss_usb_device_cap = _ss_usb_device_cap;
  870. return LIBUSB_SUCCESS;
  871. }
  872. /** \ingroup libusb_desc
  873. * Free a SuperSpeed USB Device Capability descriptor obtained from
  874. * libusb_get_ss_usb_device_capability_descriptor().
  875. * It is safe to call this function with a NULL ss_usb_device_cap
  876. * parameter, in which case the function simply returns.
  877. *
  878. * \param ss_usb_device_cap the SuperSpeed USB Device Capability descriptor
  879. * to free
  880. */
  881. void API_EXPORTED libusb_free_ss_usb_device_capability_descriptor(
  882. struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap)
  883. {
  884. free(ss_usb_device_cap);
  885. }
  886. /** \ingroup libusb_desc
  887. * Get a Container ID descriptor
  888. *
  889. * \param ctx the context to operate on, or NULL for the default context
  890. * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
  891. * \ref libusb_capability_type::LIBUSB_BT_CONTAINER_ID
  892. * LIBUSB_BT_CONTAINER_ID
  893. * \param container_id output location for the Container ID descriptor.
  894. * Only valid if 0 was returned. Must be freed with
  895. * libusb_free_container_id_descriptor() after use.
  896. * \returns 0 on success
  897. * \returns a LIBUSB_ERROR code on error
  898. */
  899. int API_EXPORTED libusb_get_container_id_descriptor(libusb_context *ctx,
  900. struct libusb_bos_dev_capability_descriptor *dev_cap,
  901. struct libusb_container_id_descriptor **container_id)
  902. {
  903. struct libusb_container_id_descriptor *_container_id;
  904. if (dev_cap->bDevCapabilityType != LIBUSB_BT_CONTAINER_ID) {
  905. usbi_err(ctx, "unexpected bDevCapabilityType 0x%x (expected 0x%x)",
  906. dev_cap->bDevCapabilityType,
  907. LIBUSB_BT_CONTAINER_ID);
  908. return LIBUSB_ERROR_INVALID_PARAM;
  909. } else if (dev_cap->bLength < LIBUSB_BT_CONTAINER_ID_SIZE) {
  910. usbi_err(ctx, "short dev-cap descriptor read %u/%d",
  911. dev_cap->bLength, LIBUSB_BT_CONTAINER_ID_SIZE);
  912. return LIBUSB_ERROR_IO;
  913. }
  914. _container_id = malloc(sizeof(*_container_id));
  915. if (!_container_id)
  916. return LIBUSB_ERROR_NO_MEM;
  917. parse_descriptor(dev_cap, "bbbbu", _container_id);
  918. *container_id = _container_id;
  919. return LIBUSB_SUCCESS;
  920. }
  921. /** \ingroup libusb_desc
  922. * Free a Container ID descriptor obtained from
  923. * libusb_get_container_id_descriptor().
  924. * It is safe to call this function with a NULL container_id parameter,
  925. * in which case the function simply returns.
  926. *
  927. * \param container_id the Container ID descriptor to free
  928. */
  929. void API_EXPORTED libusb_free_container_id_descriptor(
  930. struct libusb_container_id_descriptor *container_id)
  931. {
  932. free(container_id);
  933. }
  934. /** \ingroup libusb_desc
  935. * Retrieve a string descriptor in C style ASCII.
  936. *
  937. * Wrapper around libusb_get_string_descriptor(). Uses the first language
  938. * supported by the device.
  939. *
  940. * \param dev_handle a device handle
  941. * \param desc_index the index of the descriptor to retrieve
  942. * \param data output buffer for ASCII string descriptor
  943. * \param length size of data buffer
  944. * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
  945. */
  946. int API_EXPORTED libusb_get_string_descriptor_ascii(libusb_device_handle *dev_handle,
  947. uint8_t desc_index, unsigned char *data, int length)
  948. {
  949. union usbi_string_desc_buf str;
  950. int r, si, di;
  951. uint16_t langid, wdata;
  952. /* Asking for the zero'th index is special - it returns a string
  953. * descriptor that contains all the language IDs supported by the
  954. * device. Typically there aren't many - often only one. Language
  955. * IDs are 16 bit numbers, and they start at the third byte in the
  956. * descriptor. There's also no point in trying to read descriptor 0
  957. * with this function. See USB 2.0 specification section 9.6.7 for
  958. * more information.
  959. */
  960. if (desc_index == 0)
  961. return LIBUSB_ERROR_INVALID_PARAM;
  962. r = libusb_get_string_descriptor(dev_handle, 0, 0, str.buf, 4);
  963. if (r < 0)
  964. return r;
  965. else if (r != 4 || str.desc.bLength < 4)
  966. return LIBUSB_ERROR_IO;
  967. else if (str.desc.bDescriptorType != LIBUSB_DT_STRING)
  968. return LIBUSB_ERROR_IO;
  969. else if (str.desc.bLength & 1)
  970. usbi_warn(HANDLE_CTX(dev_handle), "suspicious bLength %u for language ID string descriptor", str.desc.bLength);
  971. langid = libusb_le16_to_cpu(str.desc.wData[0]);
  972. r = libusb_get_string_descriptor(dev_handle, desc_index, langid, str.buf, sizeof(str.buf));
  973. if (r < 0)
  974. return r;
  975. else if (r < DESC_HEADER_LENGTH || str.desc.bLength > r)
  976. return LIBUSB_ERROR_IO;
  977. else if (str.desc.bDescriptorType != LIBUSB_DT_STRING)
  978. return LIBUSB_ERROR_IO;
  979. else if ((str.desc.bLength & 1) || str.desc.bLength != r)
  980. usbi_warn(HANDLE_CTX(dev_handle), "suspicious bLength %u for string descriptor (read %d)", str.desc.bLength, r);
  981. di = 0;
  982. for (si = 2; si < str.desc.bLength; si += 2) {
  983. if (di >= (length - 1))
  984. break;
  985. wdata = libusb_le16_to_cpu(str.desc.wData[di]);
  986. if (wdata < 0x80)
  987. data[di++] = (unsigned char)wdata;
  988. else
  989. data[di++] = '?'; /* non-ASCII */
  990. }
  991. data[di] = 0;
  992. return di;
  993. }