desc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. #include <ctype.h>
  2. #include "hw/usb.h"
  3. #include "hw/usb/desc.h"
  4. #include "trace.h"
  5. /* ------------------------------------------------------------------ */
  6. int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
  7. bool msos, uint8_t *dest, size_t len)
  8. {
  9. uint8_t bLength = 0x12;
  10. USBDescriptor *d = (void *)dest;
  11. if (len < bLength) {
  12. return -1;
  13. }
  14. d->bLength = bLength;
  15. d->bDescriptorType = USB_DT_DEVICE;
  16. if (msos && dev->bcdUSB < 0x0200) {
  17. /*
  18. * Version 2.0+ required for microsoft os descriptors to work.
  19. * Done this way so msos-desc compat property will handle both
  20. * the version and the new descriptors being present.
  21. */
  22. d->u.device.bcdUSB_lo = usb_lo(0x0200);
  23. d->u.device.bcdUSB_hi = usb_hi(0x0200);
  24. } else {
  25. d->u.device.bcdUSB_lo = usb_lo(dev->bcdUSB);
  26. d->u.device.bcdUSB_hi = usb_hi(dev->bcdUSB);
  27. }
  28. d->u.device.bDeviceClass = dev->bDeviceClass;
  29. d->u.device.bDeviceSubClass = dev->bDeviceSubClass;
  30. d->u.device.bDeviceProtocol = dev->bDeviceProtocol;
  31. d->u.device.bMaxPacketSize0 = dev->bMaxPacketSize0;
  32. d->u.device.idVendor_lo = usb_lo(id->idVendor);
  33. d->u.device.idVendor_hi = usb_hi(id->idVendor);
  34. d->u.device.idProduct_lo = usb_lo(id->idProduct);
  35. d->u.device.idProduct_hi = usb_hi(id->idProduct);
  36. d->u.device.bcdDevice_lo = usb_lo(id->bcdDevice);
  37. d->u.device.bcdDevice_hi = usb_hi(id->bcdDevice);
  38. d->u.device.iManufacturer = id->iManufacturer;
  39. d->u.device.iProduct = id->iProduct;
  40. d->u.device.iSerialNumber = id->iSerialNumber;
  41. d->u.device.bNumConfigurations = dev->bNumConfigurations;
  42. return bLength;
  43. }
  44. int usb_desc_device_qualifier(const USBDescDevice *dev,
  45. uint8_t *dest, size_t len)
  46. {
  47. uint8_t bLength = 0x0a;
  48. USBDescriptor *d = (void *)dest;
  49. if (len < bLength) {
  50. return -1;
  51. }
  52. d->bLength = bLength;
  53. d->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  54. d->u.device_qualifier.bcdUSB_lo = usb_lo(dev->bcdUSB);
  55. d->u.device_qualifier.bcdUSB_hi = usb_hi(dev->bcdUSB);
  56. d->u.device_qualifier.bDeviceClass = dev->bDeviceClass;
  57. d->u.device_qualifier.bDeviceSubClass = dev->bDeviceSubClass;
  58. d->u.device_qualifier.bDeviceProtocol = dev->bDeviceProtocol;
  59. d->u.device_qualifier.bMaxPacketSize0 = dev->bMaxPacketSize0;
  60. d->u.device_qualifier.bNumConfigurations = dev->bNumConfigurations;
  61. d->u.device_qualifier.bReserved = 0;
  62. return bLength;
  63. }
  64. int usb_desc_config(const USBDescConfig *conf, int flags,
  65. uint8_t *dest, size_t len)
  66. {
  67. uint8_t bLength = 0x09;
  68. uint16_t wTotalLength = 0;
  69. USBDescriptor *d = (void *)dest;
  70. int i, rc;
  71. if (len < bLength) {
  72. return -1;
  73. }
  74. d->bLength = bLength;
  75. d->bDescriptorType = USB_DT_CONFIG;
  76. d->u.config.bNumInterfaces = conf->bNumInterfaces;
  77. d->u.config.bConfigurationValue = conf->bConfigurationValue;
  78. d->u.config.iConfiguration = conf->iConfiguration;
  79. d->u.config.bmAttributes = conf->bmAttributes;
  80. d->u.config.bMaxPower = conf->bMaxPower;
  81. wTotalLength += bLength;
  82. /* handle grouped interfaces if any */
  83. for (i = 0; i < conf->nif_groups; i++) {
  84. rc = usb_desc_iface_group(&(conf->if_groups[i]), flags,
  85. dest + wTotalLength,
  86. len - wTotalLength);
  87. if (rc < 0) {
  88. return rc;
  89. }
  90. wTotalLength += rc;
  91. }
  92. /* handle normal (ungrouped / no IAD) interfaces if any */
  93. for (i = 0; i < conf->nif; i++) {
  94. rc = usb_desc_iface(conf->ifs + i, flags,
  95. dest + wTotalLength, len - wTotalLength);
  96. if (rc < 0) {
  97. return rc;
  98. }
  99. wTotalLength += rc;
  100. }
  101. d->u.config.wTotalLength_lo = usb_lo(wTotalLength);
  102. d->u.config.wTotalLength_hi = usb_hi(wTotalLength);
  103. return wTotalLength;
  104. }
  105. int usb_desc_iface_group(const USBDescIfaceAssoc *iad, int flags,
  106. uint8_t *dest, size_t len)
  107. {
  108. int pos = 0;
  109. int i = 0;
  110. /* handle interface association descriptor */
  111. uint8_t bLength = 0x08;
  112. if (len < bLength) {
  113. return -1;
  114. }
  115. dest[0x00] = bLength;
  116. dest[0x01] = USB_DT_INTERFACE_ASSOC;
  117. dest[0x02] = iad->bFirstInterface;
  118. dest[0x03] = iad->bInterfaceCount;
  119. dest[0x04] = iad->bFunctionClass;
  120. dest[0x05] = iad->bFunctionSubClass;
  121. dest[0x06] = iad->bFunctionProtocol;
  122. dest[0x07] = iad->iFunction;
  123. pos += bLength;
  124. /* handle associated interfaces in this group */
  125. for (i = 0; i < iad->nif; i++) {
  126. int rc = usb_desc_iface(&(iad->ifs[i]), flags, dest + pos, len - pos);
  127. if (rc < 0) {
  128. return rc;
  129. }
  130. pos += rc;
  131. }
  132. return pos;
  133. }
  134. int usb_desc_iface(const USBDescIface *iface, int flags,
  135. uint8_t *dest, size_t len)
  136. {
  137. uint8_t bLength = 0x09;
  138. int i, rc, pos = 0;
  139. USBDescriptor *d = (void *)dest;
  140. if (len < bLength) {
  141. return -1;
  142. }
  143. d->bLength = bLength;
  144. d->bDescriptorType = USB_DT_INTERFACE;
  145. d->u.interface.bInterfaceNumber = iface->bInterfaceNumber;
  146. d->u.interface.bAlternateSetting = iface->bAlternateSetting;
  147. d->u.interface.bNumEndpoints = iface->bNumEndpoints;
  148. d->u.interface.bInterfaceClass = iface->bInterfaceClass;
  149. d->u.interface.bInterfaceSubClass = iface->bInterfaceSubClass;
  150. d->u.interface.bInterfaceProtocol = iface->bInterfaceProtocol;
  151. d->u.interface.iInterface = iface->iInterface;
  152. pos += bLength;
  153. for (i = 0; i < iface->ndesc; i++) {
  154. rc = usb_desc_other(iface->descs + i, dest + pos, len - pos);
  155. if (rc < 0) {
  156. return rc;
  157. }
  158. pos += rc;
  159. }
  160. for (i = 0; i < iface->bNumEndpoints; i++) {
  161. rc = usb_desc_endpoint(iface->eps + i, flags, dest + pos, len - pos);
  162. if (rc < 0) {
  163. return rc;
  164. }
  165. pos += rc;
  166. }
  167. return pos;
  168. }
  169. int usb_desc_endpoint(const USBDescEndpoint *ep, int flags,
  170. uint8_t *dest, size_t len)
  171. {
  172. uint8_t bLength = ep->is_audio ? 0x09 : 0x07;
  173. uint8_t extralen = ep->extra ? ep->extra[0] : 0;
  174. uint8_t superlen = (flags & USB_DESC_FLAG_SUPER) ? 0x06 : 0;
  175. USBDescriptor *d = (void *)dest;
  176. if (len < bLength + extralen + superlen) {
  177. return -1;
  178. }
  179. d->bLength = bLength;
  180. d->bDescriptorType = USB_DT_ENDPOINT;
  181. d->u.endpoint.bEndpointAddress = ep->bEndpointAddress;
  182. d->u.endpoint.bmAttributes = ep->bmAttributes;
  183. d->u.endpoint.wMaxPacketSize_lo = usb_lo(ep->wMaxPacketSize);
  184. d->u.endpoint.wMaxPacketSize_hi = usb_hi(ep->wMaxPacketSize);
  185. d->u.endpoint.bInterval = ep->bInterval;
  186. if (ep->is_audio) {
  187. d->u.endpoint.bRefresh = ep->bRefresh;
  188. d->u.endpoint.bSynchAddress = ep->bSynchAddress;
  189. }
  190. if (superlen) {
  191. USBDescriptor *d = (void *)(dest + bLength);
  192. d->bLength = 0x06;
  193. d->bDescriptorType = USB_DT_ENDPOINT_COMPANION;
  194. d->u.super_endpoint.bMaxBurst = ep->bMaxBurst;
  195. d->u.super_endpoint.bmAttributes = ep->bmAttributes_super;
  196. d->u.super_endpoint.wBytesPerInterval_lo =
  197. usb_lo(ep->wBytesPerInterval);
  198. d->u.super_endpoint.wBytesPerInterval_hi =
  199. usb_hi(ep->wBytesPerInterval);
  200. }
  201. if (ep->extra) {
  202. memcpy(dest + bLength + superlen, ep->extra, extralen);
  203. }
  204. return bLength + extralen + superlen;
  205. }
  206. int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len)
  207. {
  208. int bLength = desc->length ? desc->length : desc->data[0];
  209. if (len < bLength) {
  210. return -1;
  211. }
  212. memcpy(dest, desc->data, bLength);
  213. return bLength;
  214. }
  215. static int usb_desc_cap_usb2_ext(const USBDesc *desc, uint8_t *dest, size_t len)
  216. {
  217. uint8_t bLength = 0x07;
  218. USBDescriptor *d = (void *)dest;
  219. if (len < bLength) {
  220. return -1;
  221. }
  222. d->bLength = bLength;
  223. d->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  224. d->u.cap.bDevCapabilityType = USB_DEV_CAP_USB2_EXT;
  225. d->u.cap.u.usb2_ext.bmAttributes_1 = (1 << 1); /* LPM */
  226. d->u.cap.u.usb2_ext.bmAttributes_2 = 0;
  227. d->u.cap.u.usb2_ext.bmAttributes_3 = 0;
  228. d->u.cap.u.usb2_ext.bmAttributes_4 = 0;
  229. return bLength;
  230. }
  231. static int usb_desc_cap_super(const USBDesc *desc, uint8_t *dest, size_t len)
  232. {
  233. uint8_t bLength = 0x0a;
  234. USBDescriptor *d = (void *)dest;
  235. if (len < bLength) {
  236. return -1;
  237. }
  238. d->bLength = bLength;
  239. d->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  240. d->u.cap.bDevCapabilityType = USB_DEV_CAP_SUPERSPEED;
  241. d->u.cap.u.super.bmAttributes = 0;
  242. d->u.cap.u.super.wSpeedsSupported_lo = 0;
  243. d->u.cap.u.super.wSpeedsSupported_hi = 0;
  244. d->u.cap.u.super.bFunctionalitySupport = 0;
  245. d->u.cap.u.super.bU1DevExitLat = 0x0a;
  246. d->u.cap.u.super.wU2DevExitLat_lo = 0x20;
  247. d->u.cap.u.super.wU2DevExitLat_hi = 0;
  248. if (desc->full) {
  249. d->u.cap.u.super.wSpeedsSupported_lo |= (1 << 1);
  250. d->u.cap.u.super.bFunctionalitySupport = 1;
  251. }
  252. if (desc->high) {
  253. d->u.cap.u.super.wSpeedsSupported_lo |= (1 << 2);
  254. if (!d->u.cap.u.super.bFunctionalitySupport) {
  255. d->u.cap.u.super.bFunctionalitySupport = 2;
  256. }
  257. }
  258. if (desc->super) {
  259. d->u.cap.u.super.wSpeedsSupported_lo |= (1 << 3);
  260. if (!d->u.cap.u.super.bFunctionalitySupport) {
  261. d->u.cap.u.super.bFunctionalitySupport = 3;
  262. }
  263. }
  264. return bLength;
  265. }
  266. static int usb_desc_bos(const USBDesc *desc, uint8_t *dest, size_t len)
  267. {
  268. uint8_t bLength = 0x05;
  269. uint16_t wTotalLength = 0;
  270. uint8_t bNumDeviceCaps = 0;
  271. USBDescriptor *d = (void *)dest;
  272. int rc;
  273. if (len < bLength) {
  274. return -1;
  275. }
  276. d->bLength = bLength;
  277. d->bDescriptorType = USB_DT_BOS;
  278. wTotalLength += bLength;
  279. if (desc->high != NULL) {
  280. rc = usb_desc_cap_usb2_ext(desc, dest + wTotalLength,
  281. len - wTotalLength);
  282. if (rc < 0) {
  283. return rc;
  284. }
  285. wTotalLength += rc;
  286. bNumDeviceCaps++;
  287. }
  288. if (desc->super != NULL) {
  289. rc = usb_desc_cap_super(desc, dest + wTotalLength,
  290. len - wTotalLength);
  291. if (rc < 0) {
  292. return rc;
  293. }
  294. wTotalLength += rc;
  295. bNumDeviceCaps++;
  296. }
  297. d->u.bos.wTotalLength_lo = usb_lo(wTotalLength);
  298. d->u.bos.wTotalLength_hi = usb_hi(wTotalLength);
  299. d->u.bos.bNumDeviceCaps = bNumDeviceCaps;
  300. return wTotalLength;
  301. }
  302. /* ------------------------------------------------------------------ */
  303. static void usb_desc_ep_init(USBDevice *dev)
  304. {
  305. const USBDescIface *iface;
  306. int i, e, pid, ep;
  307. usb_ep_init(dev);
  308. for (i = 0; i < dev->ninterfaces; i++) {
  309. iface = dev->ifaces[i];
  310. if (iface == NULL) {
  311. continue;
  312. }
  313. for (e = 0; e < iface->bNumEndpoints; e++) {
  314. pid = (iface->eps[e].bEndpointAddress & USB_DIR_IN) ?
  315. USB_TOKEN_IN : USB_TOKEN_OUT;
  316. ep = iface->eps[e].bEndpointAddress & 0x0f;
  317. usb_ep_set_type(dev, pid, ep, iface->eps[e].bmAttributes & 0x03);
  318. usb_ep_set_ifnum(dev, pid, ep, iface->bInterfaceNumber);
  319. usb_ep_set_max_packet_size(dev, pid, ep,
  320. iface->eps[e].wMaxPacketSize);
  321. usb_ep_set_max_streams(dev, pid, ep,
  322. iface->eps[e].bmAttributes_super);
  323. }
  324. }
  325. }
  326. static const USBDescIface *usb_desc_find_interface(USBDevice *dev,
  327. int nif, int alt)
  328. {
  329. const USBDescIface *iface;
  330. int g, i;
  331. if (!dev->config) {
  332. return NULL;
  333. }
  334. for (g = 0; g < dev->config->nif_groups; g++) {
  335. for (i = 0; i < dev->config->if_groups[g].nif; i++) {
  336. iface = &dev->config->if_groups[g].ifs[i];
  337. if (iface->bInterfaceNumber == nif &&
  338. iface->bAlternateSetting == alt) {
  339. return iface;
  340. }
  341. }
  342. }
  343. for (i = 0; i < dev->config->nif; i++) {
  344. iface = &dev->config->ifs[i];
  345. if (iface->bInterfaceNumber == nif &&
  346. iface->bAlternateSetting == alt) {
  347. return iface;
  348. }
  349. }
  350. return NULL;
  351. }
  352. static int usb_desc_set_interface(USBDevice *dev, int index, int value)
  353. {
  354. const USBDescIface *iface;
  355. int old;
  356. iface = usb_desc_find_interface(dev, index, value);
  357. if (iface == NULL) {
  358. return -1;
  359. }
  360. old = dev->altsetting[index];
  361. dev->altsetting[index] = value;
  362. dev->ifaces[index] = iface;
  363. usb_desc_ep_init(dev);
  364. if (old != value) {
  365. usb_device_set_interface(dev, index, old, value);
  366. }
  367. return 0;
  368. }
  369. static int usb_desc_set_config(USBDevice *dev, int value)
  370. {
  371. int i;
  372. if (value == 0) {
  373. dev->configuration = 0;
  374. dev->ninterfaces = 0;
  375. dev->config = NULL;
  376. } else {
  377. for (i = 0; i < dev->device->bNumConfigurations; i++) {
  378. if (dev->device->confs[i].bConfigurationValue == value) {
  379. dev->configuration = value;
  380. dev->ninterfaces = dev->device->confs[i].bNumInterfaces;
  381. dev->config = dev->device->confs + i;
  382. assert(dev->ninterfaces <= USB_MAX_INTERFACES);
  383. }
  384. }
  385. if (i < dev->device->bNumConfigurations) {
  386. return -1;
  387. }
  388. }
  389. for (i = 0; i < dev->ninterfaces; i++) {
  390. usb_desc_set_interface(dev, i, 0);
  391. }
  392. for (; i < USB_MAX_INTERFACES; i++) {
  393. dev->altsetting[i] = 0;
  394. dev->ifaces[i] = NULL;
  395. }
  396. return 0;
  397. }
  398. static void usb_desc_setdefaults(USBDevice *dev)
  399. {
  400. const USBDesc *desc = usb_device_get_usb_desc(dev);
  401. assert(desc != NULL);
  402. switch (dev->speed) {
  403. case USB_SPEED_LOW:
  404. case USB_SPEED_FULL:
  405. dev->device = desc->full;
  406. break;
  407. case USB_SPEED_HIGH:
  408. dev->device = desc->high;
  409. break;
  410. case USB_SPEED_SUPER:
  411. dev->device = desc->super;
  412. break;
  413. }
  414. usb_desc_set_config(dev, 0);
  415. }
  416. void usb_desc_init(USBDevice *dev)
  417. {
  418. const USBDesc *desc = usb_device_get_usb_desc(dev);
  419. assert(desc != NULL);
  420. dev->speed = USB_SPEED_FULL;
  421. dev->speedmask = 0;
  422. if (desc->full) {
  423. dev->speedmask |= USB_SPEED_MASK_FULL;
  424. }
  425. if (desc->high) {
  426. dev->speedmask |= USB_SPEED_MASK_HIGH;
  427. }
  428. if (desc->super) {
  429. dev->speedmask |= USB_SPEED_MASK_SUPER;
  430. }
  431. if (desc->msos && (dev->flags & (1 << USB_DEV_FLAG_MSOS_DESC_ENABLE))) {
  432. dev->flags |= (1 << USB_DEV_FLAG_MSOS_DESC_IN_USE);
  433. usb_desc_set_string(dev, 0xee, "MSFT100Q");
  434. }
  435. usb_desc_setdefaults(dev);
  436. }
  437. void usb_desc_attach(USBDevice *dev)
  438. {
  439. const USBDesc *desc = usb_device_get_usb_desc(dev);
  440. assert(desc != NULL);
  441. if (desc->super && (dev->port->speedmask & USB_SPEED_MASK_SUPER)) {
  442. dev->speed = USB_SPEED_SUPER;
  443. } else if (desc->high && (dev->port->speedmask & USB_SPEED_MASK_HIGH)) {
  444. dev->speed = USB_SPEED_HIGH;
  445. } else if (desc->full && (dev->port->speedmask & USB_SPEED_MASK_FULL)) {
  446. dev->speed = USB_SPEED_FULL;
  447. } else {
  448. return;
  449. }
  450. usb_desc_setdefaults(dev);
  451. }
  452. void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str)
  453. {
  454. USBDescString *s;
  455. QLIST_FOREACH(s, &dev->strings, next) {
  456. if (s->index == index) {
  457. break;
  458. }
  459. }
  460. if (s == NULL) {
  461. s = g_malloc0(sizeof(*s));
  462. s->index = index;
  463. QLIST_INSERT_HEAD(&dev->strings, s, next);
  464. }
  465. g_free(s->str);
  466. s->str = g_strdup(str);
  467. }
  468. /*
  469. * This function creates a serial number for a usb device.
  470. * The serial number should:
  471. * (a) Be unique within the virtual machine.
  472. * (b) Be constant, so you don't get a new one each
  473. * time the guest is started.
  474. * So we are using the physical location to generate a serial number
  475. * from it. It has three pieces: First a fixed, device-specific
  476. * prefix. Second the device path of the host controller (which is
  477. * the pci address in most cases). Third the physical port path.
  478. * Results in serial numbers like this: "314159-0000:00:1d.7-3".
  479. */
  480. void usb_desc_create_serial(USBDevice *dev)
  481. {
  482. DeviceState *hcd = dev->qdev.parent_bus->parent;
  483. const USBDesc *desc = usb_device_get_usb_desc(dev);
  484. int index = desc->id.iSerialNumber;
  485. char serial[64];
  486. char *path;
  487. int dst;
  488. if (dev->serial) {
  489. /* 'serial' usb bus property has priority if present */
  490. usb_desc_set_string(dev, index, dev->serial);
  491. return;
  492. }
  493. assert(index != 0 && desc->str[index] != NULL);
  494. dst = snprintf(serial, sizeof(serial), "%s", desc->str[index]);
  495. path = qdev_get_dev_path(hcd);
  496. if (path) {
  497. dst += snprintf(serial+dst, sizeof(serial)-dst, "-%s", path);
  498. }
  499. dst += snprintf(serial+dst, sizeof(serial)-dst, "-%s", dev->port->path);
  500. usb_desc_set_string(dev, index, serial);
  501. }
  502. const char *usb_desc_get_string(USBDevice *dev, uint8_t index)
  503. {
  504. USBDescString *s;
  505. QLIST_FOREACH(s, &dev->strings, next) {
  506. if (s->index == index) {
  507. return s->str;
  508. }
  509. }
  510. return NULL;
  511. }
  512. int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len)
  513. {
  514. uint8_t bLength, pos, i;
  515. const char *str;
  516. if (len < 4) {
  517. return -1;
  518. }
  519. if (index == 0) {
  520. /* language ids */
  521. dest[0] = 4;
  522. dest[1] = USB_DT_STRING;
  523. dest[2] = 0x09;
  524. dest[3] = 0x04;
  525. return 4;
  526. }
  527. str = usb_desc_get_string(dev, index);
  528. if (str == NULL) {
  529. str = usb_device_get_usb_desc(dev)->str[index];
  530. if (str == NULL) {
  531. return 0;
  532. }
  533. }
  534. bLength = strlen(str) * 2 + 2;
  535. dest[0] = bLength;
  536. dest[1] = USB_DT_STRING;
  537. i = 0; pos = 2;
  538. while (pos+1 < bLength && pos+1 < len) {
  539. dest[pos++] = str[i++];
  540. dest[pos++] = 0;
  541. }
  542. return pos;
  543. }
  544. int usb_desc_get_descriptor(USBDevice *dev, USBPacket *p,
  545. int value, uint8_t *dest, size_t len)
  546. {
  547. bool msos = (dev->flags & (1 << USB_DEV_FLAG_MSOS_DESC_IN_USE));
  548. const USBDesc *desc = usb_device_get_usb_desc(dev);
  549. const USBDescDevice *other_dev;
  550. uint8_t buf[256];
  551. uint8_t type = value >> 8;
  552. uint8_t index = value & 0xff;
  553. int flags, ret = -1;
  554. if (dev->speed == USB_SPEED_HIGH) {
  555. other_dev = usb_device_get_usb_desc(dev)->full;
  556. } else {
  557. other_dev = usb_device_get_usb_desc(dev)->high;
  558. }
  559. flags = 0;
  560. if (dev->device->bcdUSB >= 0x0300) {
  561. flags |= USB_DESC_FLAG_SUPER;
  562. }
  563. switch(type) {
  564. case USB_DT_DEVICE:
  565. ret = usb_desc_device(&desc->id, dev->device, msos, buf, sizeof(buf));
  566. trace_usb_desc_device(dev->addr, len, ret);
  567. break;
  568. case USB_DT_CONFIG:
  569. if (index < dev->device->bNumConfigurations) {
  570. ret = usb_desc_config(dev->device->confs + index, flags,
  571. buf, sizeof(buf));
  572. }
  573. trace_usb_desc_config(dev->addr, index, len, ret);
  574. break;
  575. case USB_DT_STRING:
  576. ret = usb_desc_string(dev, index, buf, sizeof(buf));
  577. trace_usb_desc_string(dev->addr, index, len, ret);
  578. break;
  579. case USB_DT_DEVICE_QUALIFIER:
  580. if (other_dev != NULL) {
  581. ret = usb_desc_device_qualifier(other_dev, buf, sizeof(buf));
  582. }
  583. trace_usb_desc_device_qualifier(dev->addr, len, ret);
  584. break;
  585. case USB_DT_OTHER_SPEED_CONFIG:
  586. if (other_dev != NULL && index < other_dev->bNumConfigurations) {
  587. ret = usb_desc_config(other_dev->confs + index, flags,
  588. buf, sizeof(buf));
  589. buf[0x01] = USB_DT_OTHER_SPEED_CONFIG;
  590. }
  591. trace_usb_desc_other_speed_config(dev->addr, index, len, ret);
  592. break;
  593. case USB_DT_BOS:
  594. ret = usb_desc_bos(desc, buf, sizeof(buf));
  595. trace_usb_desc_bos(dev->addr, len, ret);
  596. break;
  597. case USB_DT_DEBUG:
  598. /* ignore silently */
  599. break;
  600. default:
  601. fprintf(stderr, "%s: %d unknown type %d (len %zd)\n", __FUNCTION__,
  602. dev->addr, type, len);
  603. break;
  604. }
  605. if (ret > 0) {
  606. if (ret > len) {
  607. ret = len;
  608. }
  609. memcpy(dest, buf, ret);
  610. p->actual_length = ret;
  611. ret = 0;
  612. }
  613. return ret;
  614. }
  615. int usb_desc_handle_control(USBDevice *dev, USBPacket *p,
  616. int request, int value, int index, int length, uint8_t *data)
  617. {
  618. bool msos = (dev->flags & (1 << USB_DEV_FLAG_MSOS_DESC_IN_USE));
  619. const USBDesc *desc = usb_device_get_usb_desc(dev);
  620. int ret = -1;
  621. assert(desc != NULL);
  622. switch(request) {
  623. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  624. dev->addr = value;
  625. trace_usb_set_addr(dev->addr);
  626. ret = 0;
  627. break;
  628. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  629. ret = usb_desc_get_descriptor(dev, p, value, data, length);
  630. break;
  631. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  632. /*
  633. * 9.4.2: 0 should be returned if the device is unconfigured, otherwise
  634. * the non zero value of bConfigurationValue.
  635. */
  636. data[0] = dev->config ? dev->config->bConfigurationValue : 0;
  637. p->actual_length = 1;
  638. ret = 0;
  639. break;
  640. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  641. ret = usb_desc_set_config(dev, value);
  642. trace_usb_set_config(dev->addr, value, ret);
  643. break;
  644. case DeviceRequest | USB_REQ_GET_STATUS: {
  645. const USBDescConfig *config = dev->config ?
  646. dev->config : &dev->device->confs[0];
  647. data[0] = 0;
  648. /*
  649. * Default state: Device behavior when this request is received while
  650. * the device is in the Default state is not specified.
  651. * We return the same value that a configured device would return if
  652. * it used the first configuration.
  653. */
  654. if (config->bmAttributes & USB_CFG_ATT_SELFPOWER) {
  655. data[0] |= 1 << USB_DEVICE_SELF_POWERED;
  656. }
  657. if (dev->remote_wakeup) {
  658. data[0] |= 1 << USB_DEVICE_REMOTE_WAKEUP;
  659. }
  660. data[1] = 0x00;
  661. p->actual_length = 2;
  662. ret = 0;
  663. break;
  664. }
  665. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  666. if (value == USB_DEVICE_REMOTE_WAKEUP) {
  667. dev->remote_wakeup = 0;
  668. ret = 0;
  669. }
  670. trace_usb_clear_device_feature(dev->addr, value, ret);
  671. break;
  672. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  673. if (value == USB_DEVICE_REMOTE_WAKEUP) {
  674. dev->remote_wakeup = 1;
  675. ret = 0;
  676. }
  677. trace_usb_set_device_feature(dev->addr, value, ret);
  678. break;
  679. case InterfaceRequest | USB_REQ_GET_INTERFACE:
  680. if (index < 0 || index >= dev->ninterfaces) {
  681. break;
  682. }
  683. data[0] = dev->altsetting[index];
  684. p->actual_length = 1;
  685. ret = 0;
  686. break;
  687. case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
  688. ret = usb_desc_set_interface(dev, index, value);
  689. trace_usb_set_interface(dev->addr, index, value, ret);
  690. break;
  691. case VendorDeviceRequest | 'Q':
  692. if (msos) {
  693. ret = usb_desc_msos(desc, p, index, data, length);
  694. trace_usb_desc_msos(dev->addr, index, length, ret);
  695. }
  696. break;
  697. case VendorInterfaceRequest | 'Q':
  698. if (msos) {
  699. ret = usb_desc_msos(desc, p, index, data, length);
  700. trace_usb_desc_msos(dev->addr, index, length, ret);
  701. }
  702. break;
  703. }
  704. return ret;
  705. }