dev-hub.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * QEMU USB HUB emulation
  3. *
  4. * Copyright (c) 2005 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu-common.h"
  25. #include "trace.h"
  26. #include "hw/usb.h"
  27. #include "hw/usb/desc.h"
  28. #include "qemu/error-report.h"
  29. #define NUM_PORTS 8
  30. typedef struct USBHubPort {
  31. USBPort port;
  32. uint16_t wPortStatus;
  33. uint16_t wPortChange;
  34. } USBHubPort;
  35. typedef struct USBHubState {
  36. USBDevice dev;
  37. USBEndpoint *intr;
  38. USBHubPort ports[NUM_PORTS];
  39. } USBHubState;
  40. #define ClearHubFeature (0x2000 | USB_REQ_CLEAR_FEATURE)
  41. #define ClearPortFeature (0x2300 | USB_REQ_CLEAR_FEATURE)
  42. #define GetHubDescriptor (0xa000 | USB_REQ_GET_DESCRIPTOR)
  43. #define GetHubStatus (0xa000 | USB_REQ_GET_STATUS)
  44. #define GetPortStatus (0xa300 | USB_REQ_GET_STATUS)
  45. #define SetHubFeature (0x2000 | USB_REQ_SET_FEATURE)
  46. #define SetPortFeature (0x2300 | USB_REQ_SET_FEATURE)
  47. #define PORT_STAT_CONNECTION 0x0001
  48. #define PORT_STAT_ENABLE 0x0002
  49. #define PORT_STAT_SUSPEND 0x0004
  50. #define PORT_STAT_OVERCURRENT 0x0008
  51. #define PORT_STAT_RESET 0x0010
  52. #define PORT_STAT_POWER 0x0100
  53. #define PORT_STAT_LOW_SPEED 0x0200
  54. #define PORT_STAT_HIGH_SPEED 0x0400
  55. #define PORT_STAT_TEST 0x0800
  56. #define PORT_STAT_INDICATOR 0x1000
  57. #define PORT_STAT_C_CONNECTION 0x0001
  58. #define PORT_STAT_C_ENABLE 0x0002
  59. #define PORT_STAT_C_SUSPEND 0x0004
  60. #define PORT_STAT_C_OVERCURRENT 0x0008
  61. #define PORT_STAT_C_RESET 0x0010
  62. #define PORT_CONNECTION 0
  63. #define PORT_ENABLE 1
  64. #define PORT_SUSPEND 2
  65. #define PORT_OVERCURRENT 3
  66. #define PORT_RESET 4
  67. #define PORT_POWER 8
  68. #define PORT_LOWSPEED 9
  69. #define PORT_HIGHSPEED 10
  70. #define PORT_C_CONNECTION 16
  71. #define PORT_C_ENABLE 17
  72. #define PORT_C_SUSPEND 18
  73. #define PORT_C_OVERCURRENT 19
  74. #define PORT_C_RESET 20
  75. #define PORT_TEST 21
  76. #define PORT_INDICATOR 22
  77. /* same as Linux kernel root hubs */
  78. enum {
  79. STR_MANUFACTURER = 1,
  80. STR_PRODUCT,
  81. STR_SERIALNUMBER,
  82. };
  83. static const USBDescStrings desc_strings = {
  84. [STR_MANUFACTURER] = "QEMU",
  85. [STR_PRODUCT] = "QEMU USB Hub",
  86. [STR_SERIALNUMBER] = "314159",
  87. };
  88. static const USBDescIface desc_iface_hub = {
  89. .bInterfaceNumber = 0,
  90. .bNumEndpoints = 1,
  91. .bInterfaceClass = USB_CLASS_HUB,
  92. .eps = (USBDescEndpoint[]) {
  93. {
  94. .bEndpointAddress = USB_DIR_IN | 0x01,
  95. .bmAttributes = USB_ENDPOINT_XFER_INT,
  96. .wMaxPacketSize = 1 + (NUM_PORTS + 7) / 8,
  97. .bInterval = 0xff,
  98. },
  99. }
  100. };
  101. static const USBDescDevice desc_device_hub = {
  102. .bcdUSB = 0x0110,
  103. .bDeviceClass = USB_CLASS_HUB,
  104. .bMaxPacketSize0 = 8,
  105. .bNumConfigurations = 1,
  106. .confs = (USBDescConfig[]) {
  107. {
  108. .bNumInterfaces = 1,
  109. .bConfigurationValue = 1,
  110. .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER |
  111. USB_CFG_ATT_WAKEUP,
  112. .nif = 1,
  113. .ifs = &desc_iface_hub,
  114. },
  115. },
  116. };
  117. static const USBDesc desc_hub = {
  118. .id = {
  119. .idVendor = 0x0409,
  120. .idProduct = 0x55aa,
  121. .bcdDevice = 0x0101,
  122. .iManufacturer = STR_MANUFACTURER,
  123. .iProduct = STR_PRODUCT,
  124. .iSerialNumber = STR_SERIALNUMBER,
  125. },
  126. .full = &desc_device_hub,
  127. .str = desc_strings,
  128. };
  129. static const uint8_t qemu_hub_hub_descriptor[] =
  130. {
  131. 0x00, /* u8 bLength; patched in later */
  132. 0x29, /* u8 bDescriptorType; Hub-descriptor */
  133. 0x00, /* u8 bNbrPorts; (patched later) */
  134. 0x0a, /* u16 wHubCharacteristics; */
  135. 0x00, /* (per-port OC, no power switching) */
  136. 0x01, /* u8 bPwrOn2pwrGood; 2ms */
  137. 0x00 /* u8 bHubContrCurrent; 0 mA */
  138. /* DeviceRemovable and PortPwrCtrlMask patched in later */
  139. };
  140. static void usb_hub_attach(USBPort *port1)
  141. {
  142. USBHubState *s = port1->opaque;
  143. USBHubPort *port = &s->ports[port1->index];
  144. trace_usb_hub_attach(s->dev.addr, port1->index + 1);
  145. port->wPortStatus |= PORT_STAT_CONNECTION;
  146. port->wPortChange |= PORT_STAT_C_CONNECTION;
  147. if (port->port.dev->speed == USB_SPEED_LOW) {
  148. port->wPortStatus |= PORT_STAT_LOW_SPEED;
  149. } else {
  150. port->wPortStatus &= ~PORT_STAT_LOW_SPEED;
  151. }
  152. usb_wakeup(s->intr, 0);
  153. }
  154. static void usb_hub_detach(USBPort *port1)
  155. {
  156. USBHubState *s = port1->opaque;
  157. USBHubPort *port = &s->ports[port1->index];
  158. trace_usb_hub_detach(s->dev.addr, port1->index + 1);
  159. usb_wakeup(s->intr, 0);
  160. /* Let upstream know the device on this port is gone */
  161. s->dev.port->ops->child_detach(s->dev.port, port1->dev);
  162. port->wPortStatus &= ~PORT_STAT_CONNECTION;
  163. port->wPortChange |= PORT_STAT_C_CONNECTION;
  164. if (port->wPortStatus & PORT_STAT_ENABLE) {
  165. port->wPortStatus &= ~PORT_STAT_ENABLE;
  166. port->wPortChange |= PORT_STAT_C_ENABLE;
  167. }
  168. usb_wakeup(s->intr, 0);
  169. }
  170. static void usb_hub_child_detach(USBPort *port1, USBDevice *child)
  171. {
  172. USBHubState *s = port1->opaque;
  173. /* Pass along upstream */
  174. s->dev.port->ops->child_detach(s->dev.port, child);
  175. }
  176. static void usb_hub_wakeup(USBPort *port1)
  177. {
  178. USBHubState *s = port1->opaque;
  179. USBHubPort *port = &s->ports[port1->index];
  180. if (port->wPortStatus & PORT_STAT_SUSPEND) {
  181. port->wPortChange |= PORT_STAT_C_SUSPEND;
  182. usb_wakeup(s->intr, 0);
  183. }
  184. }
  185. static void usb_hub_complete(USBPort *port, USBPacket *packet)
  186. {
  187. USBHubState *s = port->opaque;
  188. /*
  189. * Just pass it along upstream for now.
  190. *
  191. * If we ever implement usb 2.0 split transactions this will
  192. * become a little more complicated ...
  193. *
  194. * Can't use usb_packet_complete() here because packet->owner is
  195. * cleared already, go call the ->complete() callback directly
  196. * instead.
  197. */
  198. s->dev.port->ops->complete(s->dev.port, packet);
  199. }
  200. static USBDevice *usb_hub_find_device(USBDevice *dev, uint8_t addr)
  201. {
  202. USBHubState *s = DO_UPCAST(USBHubState, dev, dev);
  203. USBHubPort *port;
  204. USBDevice *downstream;
  205. int i;
  206. for (i = 0; i < NUM_PORTS; i++) {
  207. port = &s->ports[i];
  208. if (!(port->wPortStatus & PORT_STAT_ENABLE)) {
  209. continue;
  210. }
  211. downstream = usb_find_device(&port->port, addr);
  212. if (downstream != NULL) {
  213. return downstream;
  214. }
  215. }
  216. return NULL;
  217. }
  218. static void usb_hub_handle_reset(USBDevice *dev)
  219. {
  220. USBHubState *s = DO_UPCAST(USBHubState, dev, dev);
  221. USBHubPort *port;
  222. int i;
  223. trace_usb_hub_reset(s->dev.addr);
  224. for (i = 0; i < NUM_PORTS; i++) {
  225. port = s->ports + i;
  226. port->wPortStatus = PORT_STAT_POWER;
  227. port->wPortChange = 0;
  228. if (port->port.dev && port->port.dev->attached) {
  229. port->wPortStatus |= PORT_STAT_CONNECTION;
  230. port->wPortChange |= PORT_STAT_C_CONNECTION;
  231. if (port->port.dev->speed == USB_SPEED_LOW) {
  232. port->wPortStatus |= PORT_STAT_LOW_SPEED;
  233. }
  234. }
  235. }
  236. }
  237. static const char *feature_name(int feature)
  238. {
  239. static const char *name[] = {
  240. [PORT_CONNECTION] = "connection",
  241. [PORT_ENABLE] = "enable",
  242. [PORT_SUSPEND] = "suspend",
  243. [PORT_OVERCURRENT] = "overcurrent",
  244. [PORT_RESET] = "reset",
  245. [PORT_POWER] = "power",
  246. [PORT_LOWSPEED] = "lowspeed",
  247. [PORT_HIGHSPEED] = "highspeed",
  248. [PORT_C_CONNECTION] = "change connection",
  249. [PORT_C_ENABLE] = "change enable",
  250. [PORT_C_SUSPEND] = "change suspend",
  251. [PORT_C_OVERCURRENT] = "change overcurrent",
  252. [PORT_C_RESET] = "change reset",
  253. [PORT_TEST] = "test",
  254. [PORT_INDICATOR] = "indicator",
  255. };
  256. if (feature < 0 || feature >= ARRAY_SIZE(name)) {
  257. return "?";
  258. }
  259. return name[feature] ?: "?";
  260. }
  261. static void usb_hub_handle_control(USBDevice *dev, USBPacket *p,
  262. int request, int value, int index, int length, uint8_t *data)
  263. {
  264. USBHubState *s = (USBHubState *)dev;
  265. int ret;
  266. trace_usb_hub_control(s->dev.addr, request, value, index, length);
  267. ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
  268. if (ret >= 0) {
  269. return;
  270. }
  271. switch(request) {
  272. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  273. if (value == 0 && index != 0x81) { /* clear ep halt */
  274. goto fail;
  275. }
  276. break;
  277. /* usb specific requests */
  278. case GetHubStatus:
  279. data[0] = 0;
  280. data[1] = 0;
  281. data[2] = 0;
  282. data[3] = 0;
  283. p->actual_length = 4;
  284. break;
  285. case GetPortStatus:
  286. {
  287. unsigned int n = index - 1;
  288. USBHubPort *port;
  289. if (n >= NUM_PORTS) {
  290. goto fail;
  291. }
  292. port = &s->ports[n];
  293. trace_usb_hub_get_port_status(s->dev.addr, index,
  294. port->wPortStatus,
  295. port->wPortChange);
  296. data[0] = port->wPortStatus;
  297. data[1] = port->wPortStatus >> 8;
  298. data[2] = port->wPortChange;
  299. data[3] = port->wPortChange >> 8;
  300. p->actual_length = 4;
  301. }
  302. break;
  303. case SetHubFeature:
  304. case ClearHubFeature:
  305. if (value != 0 && value != 1) {
  306. goto fail;
  307. }
  308. break;
  309. case SetPortFeature:
  310. {
  311. unsigned int n = index - 1;
  312. USBHubPort *port;
  313. USBDevice *dev;
  314. trace_usb_hub_set_port_feature(s->dev.addr, index,
  315. feature_name(value));
  316. if (n >= NUM_PORTS) {
  317. goto fail;
  318. }
  319. port = &s->ports[n];
  320. dev = port->port.dev;
  321. switch(value) {
  322. case PORT_SUSPEND:
  323. port->wPortStatus |= PORT_STAT_SUSPEND;
  324. break;
  325. case PORT_RESET:
  326. if (dev && dev->attached) {
  327. usb_device_reset(dev);
  328. port->wPortChange |= PORT_STAT_C_RESET;
  329. /* set enable bit */
  330. port->wPortStatus |= PORT_STAT_ENABLE;
  331. usb_wakeup(s->intr, 0);
  332. }
  333. break;
  334. case PORT_POWER:
  335. break;
  336. default:
  337. goto fail;
  338. }
  339. }
  340. break;
  341. case ClearPortFeature:
  342. {
  343. unsigned int n = index - 1;
  344. USBHubPort *port;
  345. trace_usb_hub_clear_port_feature(s->dev.addr, index,
  346. feature_name(value));
  347. if (n >= NUM_PORTS) {
  348. goto fail;
  349. }
  350. port = &s->ports[n];
  351. switch(value) {
  352. case PORT_ENABLE:
  353. port->wPortStatus &= ~PORT_STAT_ENABLE;
  354. break;
  355. case PORT_C_ENABLE:
  356. port->wPortChange &= ~PORT_STAT_C_ENABLE;
  357. break;
  358. case PORT_SUSPEND:
  359. port->wPortStatus &= ~PORT_STAT_SUSPEND;
  360. break;
  361. case PORT_C_SUSPEND:
  362. port->wPortChange &= ~PORT_STAT_C_SUSPEND;
  363. break;
  364. case PORT_C_CONNECTION:
  365. port->wPortChange &= ~PORT_STAT_C_CONNECTION;
  366. break;
  367. case PORT_C_OVERCURRENT:
  368. port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
  369. break;
  370. case PORT_C_RESET:
  371. port->wPortChange &= ~PORT_STAT_C_RESET;
  372. break;
  373. default:
  374. goto fail;
  375. }
  376. }
  377. break;
  378. case GetHubDescriptor:
  379. {
  380. unsigned int n, limit, var_hub_size = 0;
  381. memcpy(data, qemu_hub_hub_descriptor,
  382. sizeof(qemu_hub_hub_descriptor));
  383. data[2] = NUM_PORTS;
  384. /* fill DeviceRemovable bits */
  385. limit = ((NUM_PORTS + 1 + 7) / 8) + 7;
  386. for (n = 7; n < limit; n++) {
  387. data[n] = 0x00;
  388. var_hub_size++;
  389. }
  390. /* fill PortPwrCtrlMask bits */
  391. limit = limit + ((NUM_PORTS + 7) / 8);
  392. for (;n < limit; n++) {
  393. data[n] = 0xff;
  394. var_hub_size++;
  395. }
  396. p->actual_length = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
  397. data[0] = p->actual_length;
  398. break;
  399. }
  400. default:
  401. fail:
  402. p->status = USB_RET_STALL;
  403. break;
  404. }
  405. }
  406. static void usb_hub_handle_data(USBDevice *dev, USBPacket *p)
  407. {
  408. USBHubState *s = (USBHubState *)dev;
  409. switch(p->pid) {
  410. case USB_TOKEN_IN:
  411. if (p->ep->nr == 1) {
  412. USBHubPort *port;
  413. unsigned int status;
  414. uint8_t buf[4];
  415. int i, n;
  416. n = (NUM_PORTS + 1 + 7) / 8;
  417. if (p->iov.size == 1) { /* FreeBSD workaround */
  418. n = 1;
  419. } else if (n > p->iov.size) {
  420. p->status = USB_RET_BABBLE;
  421. return;
  422. }
  423. status = 0;
  424. for(i = 0; i < NUM_PORTS; i++) {
  425. port = &s->ports[i];
  426. if (port->wPortChange)
  427. status |= (1 << (i + 1));
  428. }
  429. if (status != 0) {
  430. trace_usb_hub_status_report(s->dev.addr, status);
  431. for(i = 0; i < n; i++) {
  432. buf[i] = status >> (8 * i);
  433. }
  434. usb_packet_copy(p, buf, n);
  435. } else {
  436. p->status = USB_RET_NAK; /* usb11 11.13.1 */
  437. }
  438. } else {
  439. goto fail;
  440. }
  441. break;
  442. case USB_TOKEN_OUT:
  443. default:
  444. fail:
  445. p->status = USB_RET_STALL;
  446. break;
  447. }
  448. }
  449. static void usb_hub_handle_destroy(USBDevice *dev)
  450. {
  451. USBHubState *s = (USBHubState *)dev;
  452. int i;
  453. for (i = 0; i < NUM_PORTS; i++) {
  454. usb_unregister_port(usb_bus_from_device(dev),
  455. &s->ports[i].port);
  456. }
  457. }
  458. static USBPortOps usb_hub_port_ops = {
  459. .attach = usb_hub_attach,
  460. .detach = usb_hub_detach,
  461. .child_detach = usb_hub_child_detach,
  462. .wakeup = usb_hub_wakeup,
  463. .complete = usb_hub_complete,
  464. };
  465. static int usb_hub_initfn(USBDevice *dev)
  466. {
  467. USBHubState *s = DO_UPCAST(USBHubState, dev, dev);
  468. USBHubPort *port;
  469. int i;
  470. if (dev->port->hubcount == 5) {
  471. error_report("usb hub chain too deep");
  472. return -1;
  473. }
  474. usb_desc_create_serial(dev);
  475. usb_desc_init(dev);
  476. s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
  477. for (i = 0; i < NUM_PORTS; i++) {
  478. port = &s->ports[i];
  479. usb_register_port(usb_bus_from_device(dev),
  480. &port->port, s, i, &usb_hub_port_ops,
  481. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
  482. usb_port_location(&port->port, dev->port, i+1);
  483. }
  484. usb_hub_handle_reset(dev);
  485. return 0;
  486. }
  487. static const VMStateDescription vmstate_usb_hub_port = {
  488. .name = "usb-hub-port",
  489. .version_id = 1,
  490. .minimum_version_id = 1,
  491. .fields = (VMStateField []) {
  492. VMSTATE_UINT16(wPortStatus, USBHubPort),
  493. VMSTATE_UINT16(wPortChange, USBHubPort),
  494. VMSTATE_END_OF_LIST()
  495. }
  496. };
  497. static const VMStateDescription vmstate_usb_hub = {
  498. .name = "usb-hub",
  499. .version_id = 1,
  500. .minimum_version_id = 1,
  501. .fields = (VMStateField []) {
  502. VMSTATE_USB_DEVICE(dev, USBHubState),
  503. VMSTATE_STRUCT_ARRAY(ports, USBHubState, NUM_PORTS, 0,
  504. vmstate_usb_hub_port, USBHubPort),
  505. VMSTATE_END_OF_LIST()
  506. }
  507. };
  508. static void usb_hub_class_initfn(ObjectClass *klass, void *data)
  509. {
  510. DeviceClass *dc = DEVICE_CLASS(klass);
  511. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  512. uc->init = usb_hub_initfn;
  513. uc->product_desc = "QEMU USB Hub";
  514. uc->usb_desc = &desc_hub;
  515. uc->find_device = usb_hub_find_device;
  516. uc->handle_reset = usb_hub_handle_reset;
  517. uc->handle_control = usb_hub_handle_control;
  518. uc->handle_data = usb_hub_handle_data;
  519. uc->handle_destroy = usb_hub_handle_destroy;
  520. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  521. dc->fw_name = "hub";
  522. dc->vmsd = &vmstate_usb_hub;
  523. }
  524. static const TypeInfo hub_info = {
  525. .name = "usb-hub",
  526. .parent = TYPE_USB_DEVICE,
  527. .instance_size = sizeof(USBHubState),
  528. .class_init = usb_hub_class_initfn,
  529. };
  530. static void usb_hub_register_types(void)
  531. {
  532. type_register_static(&hub_info);
  533. }
  534. type_init(usb_hub_register_types)