usb-hub.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 "usb.h"
  26. #include "usb-desc.h"
  27. //#define DEBUG
  28. #define NUM_PORTS 8
  29. typedef struct USBHubPort {
  30. USBPort port;
  31. uint16_t wPortStatus;
  32. uint16_t wPortChange;
  33. } USBHubPort;
  34. typedef struct USBHubState {
  35. USBDevice dev;
  36. USBHubPort ports[NUM_PORTS];
  37. } USBHubState;
  38. #define ClearHubFeature (0x2000 | USB_REQ_CLEAR_FEATURE)
  39. #define ClearPortFeature (0x2300 | USB_REQ_CLEAR_FEATURE)
  40. #define GetHubDescriptor (0xa000 | USB_REQ_GET_DESCRIPTOR)
  41. #define GetHubStatus (0xa000 | USB_REQ_GET_STATUS)
  42. #define GetPortStatus (0xa300 | USB_REQ_GET_STATUS)
  43. #define SetHubFeature (0x2000 | USB_REQ_SET_FEATURE)
  44. #define SetPortFeature (0x2300 | USB_REQ_SET_FEATURE)
  45. #define PORT_STAT_CONNECTION 0x0001
  46. #define PORT_STAT_ENABLE 0x0002
  47. #define PORT_STAT_SUSPEND 0x0004
  48. #define PORT_STAT_OVERCURRENT 0x0008
  49. #define PORT_STAT_RESET 0x0010
  50. #define PORT_STAT_POWER 0x0100
  51. #define PORT_STAT_LOW_SPEED 0x0200
  52. #define PORT_STAT_HIGH_SPEED 0x0400
  53. #define PORT_STAT_TEST 0x0800
  54. #define PORT_STAT_INDICATOR 0x1000
  55. #define PORT_STAT_C_CONNECTION 0x0001
  56. #define PORT_STAT_C_ENABLE 0x0002
  57. #define PORT_STAT_C_SUSPEND 0x0004
  58. #define PORT_STAT_C_OVERCURRENT 0x0008
  59. #define PORT_STAT_C_RESET 0x0010
  60. #define PORT_CONNECTION 0
  61. #define PORT_ENABLE 1
  62. #define PORT_SUSPEND 2
  63. #define PORT_OVERCURRENT 3
  64. #define PORT_RESET 4
  65. #define PORT_POWER 8
  66. #define PORT_LOWSPEED 9
  67. #define PORT_HIGHSPEED 10
  68. #define PORT_C_CONNECTION 16
  69. #define PORT_C_ENABLE 17
  70. #define PORT_C_SUSPEND 18
  71. #define PORT_C_OVERCURRENT 19
  72. #define PORT_C_RESET 20
  73. #define PORT_TEST 21
  74. #define PORT_INDICATOR 22
  75. /* same as Linux kernel root hubs */
  76. enum {
  77. STR_MANUFACTURER = 1,
  78. STR_PRODUCT,
  79. STR_SERIALNUMBER,
  80. };
  81. static const USBDescStrings desc_strings = {
  82. [STR_MANUFACTURER] = "QEMU " QEMU_VERSION,
  83. [STR_PRODUCT] = "QEMU USB Hub",
  84. [STR_SERIALNUMBER] = "314159",
  85. };
  86. static const USBDescIface desc_iface_hub = {
  87. .bInterfaceNumber = 0,
  88. .bNumEndpoints = 1,
  89. .bInterfaceClass = USB_CLASS_HUB,
  90. .eps = (USBDescEndpoint[]) {
  91. {
  92. .bEndpointAddress = USB_DIR_IN | 0x01,
  93. .bmAttributes = USB_ENDPOINT_XFER_INT,
  94. .wMaxPacketSize = 1 + (NUM_PORTS + 7) / 8,
  95. .bInterval = 0xff,
  96. },
  97. }
  98. };
  99. static const USBDescDevice desc_device_hub = {
  100. .bcdUSB = 0x0110,
  101. .bDeviceClass = USB_CLASS_HUB,
  102. .bMaxPacketSize0 = 8,
  103. .bNumConfigurations = 1,
  104. .confs = (USBDescConfig[]) {
  105. {
  106. .bNumInterfaces = 1,
  107. .bConfigurationValue = 1,
  108. .bmAttributes = 0xe0,
  109. .nif = 1,
  110. .ifs = &desc_iface_hub,
  111. },
  112. },
  113. };
  114. static const USBDesc desc_hub = {
  115. .id = {
  116. .idVendor = 0x0409,
  117. .idProduct = 0x55aa,
  118. .bcdDevice = 0x0101,
  119. .iManufacturer = STR_MANUFACTURER,
  120. .iProduct = STR_PRODUCT,
  121. .iSerialNumber = STR_SERIALNUMBER,
  122. },
  123. .full = &desc_device_hub,
  124. .str = desc_strings,
  125. };
  126. static const uint8_t qemu_hub_hub_descriptor[] =
  127. {
  128. 0x00, /* u8 bLength; patched in later */
  129. 0x29, /* u8 bDescriptorType; Hub-descriptor */
  130. 0x00, /* u8 bNbrPorts; (patched later) */
  131. 0x0a, /* u16 wHubCharacteristics; */
  132. 0x00, /* (per-port OC, no power switching) */
  133. 0x01, /* u8 bPwrOn2pwrGood; 2ms */
  134. 0x00 /* u8 bHubContrCurrent; 0 mA */
  135. /* DeviceRemovable and PortPwrCtrlMask patched in later */
  136. };
  137. static void usb_hub_attach(USBPort *port1)
  138. {
  139. USBHubState *s = port1->opaque;
  140. USBHubPort *port = &s->ports[port1->index];
  141. port->wPortStatus |= PORT_STAT_CONNECTION;
  142. port->wPortChange |= PORT_STAT_C_CONNECTION;
  143. if (port->port.dev->speed == USB_SPEED_LOW) {
  144. port->wPortStatus |= PORT_STAT_LOW_SPEED;
  145. } else {
  146. port->wPortStatus &= ~PORT_STAT_LOW_SPEED;
  147. }
  148. usb_wakeup(&s->dev);
  149. }
  150. static void usb_hub_detach(USBPort *port1)
  151. {
  152. USBHubState *s = port1->opaque;
  153. USBHubPort *port = &s->ports[port1->index];
  154. usb_wakeup(&s->dev);
  155. /* Let upstream know the device on this port is gone */
  156. s->dev.port->ops->child_detach(s->dev.port, port1->dev);
  157. port->wPortStatus &= ~PORT_STAT_CONNECTION;
  158. port->wPortChange |= PORT_STAT_C_CONNECTION;
  159. if (port->wPortStatus & PORT_STAT_ENABLE) {
  160. port->wPortStatus &= ~PORT_STAT_ENABLE;
  161. port->wPortChange |= PORT_STAT_C_ENABLE;
  162. }
  163. }
  164. static void usb_hub_child_detach(USBPort *port1, USBDevice *child)
  165. {
  166. USBHubState *s = port1->opaque;
  167. /* Pass along upstream */
  168. s->dev.port->ops->child_detach(s->dev.port, child);
  169. }
  170. static void usb_hub_wakeup(USBPort *port1)
  171. {
  172. USBHubState *s = port1->opaque;
  173. USBHubPort *port = &s->ports[port1->index];
  174. if (port->wPortStatus & PORT_STAT_SUSPEND) {
  175. port->wPortChange |= PORT_STAT_C_SUSPEND;
  176. usb_wakeup(&s->dev);
  177. }
  178. }
  179. static void usb_hub_complete(USBPort *port, USBPacket *packet)
  180. {
  181. USBHubState *s = port->opaque;
  182. /*
  183. * Just pass it along upstream for now.
  184. *
  185. * If we ever implement usb 2.0 split transactions this will
  186. * become a little more complicated ...
  187. *
  188. * Can't use usb_packet_complete() here because packet->owner is
  189. * cleared already, go call the ->complete() callback directly
  190. * instead.
  191. */
  192. s->dev.port->ops->complete(s->dev.port, packet);
  193. }
  194. static void usb_hub_handle_reset(USBDevice *dev)
  195. {
  196. USBHubState *s = DO_UPCAST(USBHubState, dev, dev);
  197. USBHubPort *port;
  198. int i;
  199. for (i = 0; i < NUM_PORTS; i++) {
  200. port = s->ports + i;
  201. port->wPortStatus = PORT_STAT_POWER;
  202. port->wPortChange = 0;
  203. if (port->port.dev && port->port.dev->attached) {
  204. port->wPortStatus |= PORT_STAT_CONNECTION;
  205. port->wPortChange |= PORT_STAT_C_CONNECTION;
  206. if (port->port.dev->speed == USB_SPEED_LOW) {
  207. port->wPortStatus |= PORT_STAT_LOW_SPEED;
  208. }
  209. }
  210. }
  211. }
  212. static int usb_hub_handle_control(USBDevice *dev, USBPacket *p,
  213. int request, int value, int index, int length, uint8_t *data)
  214. {
  215. USBHubState *s = (USBHubState *)dev;
  216. int ret;
  217. ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
  218. if (ret >= 0) {
  219. return ret;
  220. }
  221. switch(request) {
  222. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  223. if (value == 0 && index != 0x81) { /* clear ep halt */
  224. goto fail;
  225. }
  226. ret = 0;
  227. break;
  228. case DeviceRequest | USB_REQ_GET_INTERFACE:
  229. data[0] = 0;
  230. ret = 1;
  231. break;
  232. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  233. ret = 0;
  234. break;
  235. /* usb specific requests */
  236. case GetHubStatus:
  237. data[0] = 0;
  238. data[1] = 0;
  239. data[2] = 0;
  240. data[3] = 0;
  241. ret = 4;
  242. break;
  243. case GetPortStatus:
  244. {
  245. unsigned int n = index - 1;
  246. USBHubPort *port;
  247. if (n >= NUM_PORTS) {
  248. goto fail;
  249. }
  250. port = &s->ports[n];
  251. data[0] = port->wPortStatus;
  252. data[1] = port->wPortStatus >> 8;
  253. data[2] = port->wPortChange;
  254. data[3] = port->wPortChange >> 8;
  255. ret = 4;
  256. }
  257. break;
  258. case SetHubFeature:
  259. case ClearHubFeature:
  260. if (value == 0 || value == 1) {
  261. } else {
  262. goto fail;
  263. }
  264. ret = 0;
  265. break;
  266. case SetPortFeature:
  267. {
  268. unsigned int n = index - 1;
  269. USBHubPort *port;
  270. USBDevice *dev;
  271. if (n >= NUM_PORTS) {
  272. goto fail;
  273. }
  274. port = &s->ports[n];
  275. dev = port->port.dev;
  276. switch(value) {
  277. case PORT_SUSPEND:
  278. port->wPortStatus |= PORT_STAT_SUSPEND;
  279. break;
  280. case PORT_RESET:
  281. if (dev && dev->attached) {
  282. usb_send_msg(dev, USB_MSG_RESET);
  283. port->wPortChange |= PORT_STAT_C_RESET;
  284. /* set enable bit */
  285. port->wPortStatus |= PORT_STAT_ENABLE;
  286. }
  287. break;
  288. case PORT_POWER:
  289. break;
  290. default:
  291. goto fail;
  292. }
  293. ret = 0;
  294. }
  295. break;
  296. case ClearPortFeature:
  297. {
  298. unsigned int n = index - 1;
  299. USBHubPort *port;
  300. if (n >= NUM_PORTS) {
  301. goto fail;
  302. }
  303. port = &s->ports[n];
  304. switch(value) {
  305. case PORT_ENABLE:
  306. port->wPortStatus &= ~PORT_STAT_ENABLE;
  307. break;
  308. case PORT_C_ENABLE:
  309. port->wPortChange &= ~PORT_STAT_C_ENABLE;
  310. break;
  311. case PORT_SUSPEND:
  312. port->wPortStatus &= ~PORT_STAT_SUSPEND;
  313. break;
  314. case PORT_C_SUSPEND:
  315. port->wPortChange &= ~PORT_STAT_C_SUSPEND;
  316. break;
  317. case PORT_C_CONNECTION:
  318. port->wPortChange &= ~PORT_STAT_C_CONNECTION;
  319. break;
  320. case PORT_C_OVERCURRENT:
  321. port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
  322. break;
  323. case PORT_C_RESET:
  324. port->wPortChange &= ~PORT_STAT_C_RESET;
  325. break;
  326. default:
  327. goto fail;
  328. }
  329. ret = 0;
  330. }
  331. break;
  332. case GetHubDescriptor:
  333. {
  334. unsigned int n, limit, var_hub_size = 0;
  335. memcpy(data, qemu_hub_hub_descriptor,
  336. sizeof(qemu_hub_hub_descriptor));
  337. data[2] = NUM_PORTS;
  338. /* fill DeviceRemovable bits */
  339. limit = ((NUM_PORTS + 1 + 7) / 8) + 7;
  340. for (n = 7; n < limit; n++) {
  341. data[n] = 0x00;
  342. var_hub_size++;
  343. }
  344. /* fill PortPwrCtrlMask bits */
  345. limit = limit + ((NUM_PORTS + 7) / 8);
  346. for (;n < limit; n++) {
  347. data[n] = 0xff;
  348. var_hub_size++;
  349. }
  350. ret = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
  351. data[0] = ret;
  352. break;
  353. }
  354. default:
  355. fail:
  356. ret = USB_RET_STALL;
  357. break;
  358. }
  359. return ret;
  360. }
  361. static int usb_hub_handle_data(USBDevice *dev, USBPacket *p)
  362. {
  363. USBHubState *s = (USBHubState *)dev;
  364. int ret;
  365. switch(p->pid) {
  366. case USB_TOKEN_IN:
  367. if (p->devep == 1) {
  368. USBHubPort *port;
  369. unsigned int status;
  370. uint8_t buf[4];
  371. int i, n;
  372. n = (NUM_PORTS + 1 + 7) / 8;
  373. if (p->iov.size == 1) { /* FreeBSD workaround */
  374. n = 1;
  375. } else if (n > p->iov.size) {
  376. return USB_RET_BABBLE;
  377. }
  378. status = 0;
  379. for(i = 0; i < NUM_PORTS; i++) {
  380. port = &s->ports[i];
  381. if (port->wPortChange)
  382. status |= (1 << (i + 1));
  383. }
  384. if (status != 0) {
  385. for(i = 0; i < n; i++) {
  386. buf[i] = status >> (8 * i);
  387. }
  388. usb_packet_copy(p, buf, n);
  389. ret = n;
  390. } else {
  391. ret = USB_RET_NAK; /* usb11 11.13.1 */
  392. }
  393. } else {
  394. goto fail;
  395. }
  396. break;
  397. case USB_TOKEN_OUT:
  398. default:
  399. fail:
  400. ret = USB_RET_STALL;
  401. break;
  402. }
  403. return ret;
  404. }
  405. static int usb_hub_broadcast_packet(USBHubState *s, USBPacket *p)
  406. {
  407. USBHubPort *port;
  408. USBDevice *dev;
  409. int i, ret;
  410. for(i = 0; i < NUM_PORTS; i++) {
  411. port = &s->ports[i];
  412. dev = port->port.dev;
  413. if (dev && dev->attached && (port->wPortStatus & PORT_STAT_ENABLE)) {
  414. ret = usb_handle_packet(dev, p);
  415. if (ret != USB_RET_NODEV) {
  416. return ret;
  417. }
  418. }
  419. }
  420. return USB_RET_NODEV;
  421. }
  422. static int usb_hub_handle_packet(USBDevice *dev, USBPacket *p)
  423. {
  424. USBHubState *s = (USBHubState *)dev;
  425. #if defined(DEBUG) && 0
  426. printf("usb_hub: pid=0x%x\n", pid);
  427. #endif
  428. if (dev->state == USB_STATE_DEFAULT &&
  429. dev->addr != 0 &&
  430. p->devaddr != dev->addr &&
  431. (p->pid == USB_TOKEN_SETUP ||
  432. p->pid == USB_TOKEN_OUT ||
  433. p->pid == USB_TOKEN_IN)) {
  434. /* broadcast the packet to the devices */
  435. return usb_hub_broadcast_packet(s, p);
  436. }
  437. return usb_generic_handle_packet(dev, p);
  438. }
  439. static void usb_hub_handle_destroy(USBDevice *dev)
  440. {
  441. USBHubState *s = (USBHubState *)dev;
  442. int i;
  443. for (i = 0; i < NUM_PORTS; i++) {
  444. usb_unregister_port(usb_bus_from_device(dev),
  445. &s->ports[i].port);
  446. }
  447. }
  448. static USBPortOps usb_hub_port_ops = {
  449. .attach = usb_hub_attach,
  450. .detach = usb_hub_detach,
  451. .child_detach = usb_hub_child_detach,
  452. .wakeup = usb_hub_wakeup,
  453. .complete = usb_hub_complete,
  454. };
  455. static int usb_hub_initfn(USBDevice *dev)
  456. {
  457. USBHubState *s = DO_UPCAST(USBHubState, dev, dev);
  458. USBHubPort *port;
  459. int i;
  460. usb_desc_init(dev);
  461. for (i = 0; i < NUM_PORTS; i++) {
  462. port = &s->ports[i];
  463. usb_register_port(usb_bus_from_device(dev),
  464. &port->port, s, i, &usb_hub_port_ops,
  465. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
  466. usb_port_location(&port->port, dev->port, i+1);
  467. }
  468. usb_hub_handle_reset(dev);
  469. return 0;
  470. }
  471. static const VMStateDescription vmstate_usb_hub_port = {
  472. .name = "usb-hub-port",
  473. .version_id = 1,
  474. .minimum_version_id = 1,
  475. .fields = (VMStateField []) {
  476. VMSTATE_UINT16(wPortStatus, USBHubPort),
  477. VMSTATE_UINT16(wPortChange, USBHubPort),
  478. VMSTATE_END_OF_LIST()
  479. }
  480. };
  481. static const VMStateDescription vmstate_usb_hub = {
  482. .name = "usb-hub",
  483. .version_id = 1,
  484. .minimum_version_id = 1,
  485. .fields = (VMStateField []) {
  486. VMSTATE_USB_DEVICE(dev, USBHubState),
  487. VMSTATE_STRUCT_ARRAY(ports, USBHubState, NUM_PORTS, 0,
  488. vmstate_usb_hub_port, USBHubPort),
  489. VMSTATE_END_OF_LIST()
  490. }
  491. };
  492. static struct USBDeviceInfo hub_info = {
  493. .product_desc = "QEMU USB Hub",
  494. .qdev.name = "usb-hub",
  495. .qdev.fw_name = "hub",
  496. .qdev.size = sizeof(USBHubState),
  497. .qdev.vmsd = &vmstate_usb_hub,
  498. .usb_desc = &desc_hub,
  499. .init = usb_hub_initfn,
  500. .handle_packet = usb_hub_handle_packet,
  501. .handle_reset = usb_hub_handle_reset,
  502. .handle_control = usb_hub_handle_control,
  503. .handle_data = usb_hub_handle_data,
  504. .handle_destroy = usb_hub_handle_destroy,
  505. };
  506. static void usb_hub_register_devices(void)
  507. {
  508. usb_qdev_register(&hub_info);
  509. }
  510. device_init(usb_hub_register_devices)