desc.c 23 KB

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