2
0

usb-hub.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 = 0,
  117. .idProduct = 0,
  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. }
  149. static void usb_hub_detach(USBPort *port1)
  150. {
  151. USBHubState *s = port1->opaque;
  152. USBHubPort *port = &s->ports[port1->index];
  153. /* Let upstream know the device on this port is gone */
  154. s->dev.port->ops->child_detach(s->dev.port, port1->dev);
  155. port->wPortStatus &= ~PORT_STAT_CONNECTION;
  156. port->wPortChange |= PORT_STAT_C_CONNECTION;
  157. if (port->wPortStatus & PORT_STAT_ENABLE) {
  158. port->wPortStatus &= ~PORT_STAT_ENABLE;
  159. port->wPortChange |= PORT_STAT_C_ENABLE;
  160. }
  161. }
  162. static void usb_hub_child_detach(USBPort *port1, USBDevice *child)
  163. {
  164. USBHubState *s = port1->opaque;
  165. /* Pass along upstream */
  166. s->dev.port->ops->child_detach(s->dev.port, child);
  167. }
  168. static void usb_hub_wakeup(USBPort *port1)
  169. {
  170. USBHubState *s = port1->opaque;
  171. USBHubPort *port = &s->ports[port1->index];
  172. if (port->wPortStatus & PORT_STAT_SUSPEND) {
  173. port->wPortChange |= PORT_STAT_C_SUSPEND;
  174. usb_wakeup(&s->dev);
  175. }
  176. }
  177. static void usb_hub_complete(USBPort *port, USBPacket *packet)
  178. {
  179. USBHubState *s = port->opaque;
  180. /*
  181. * Just pass it along upstream for now.
  182. *
  183. * If we ever inplement usb 2.0 split transactions this will
  184. * become a little more complicated ...
  185. */
  186. usb_packet_complete(&s->dev, packet);
  187. }
  188. static void usb_hub_handle_attach(USBDevice *dev)
  189. {
  190. USBHubState *s = DO_UPCAST(USBHubState, dev, dev);
  191. int i;
  192. for (i = 0; i < NUM_PORTS; i++) {
  193. usb_port_location(&s->ports[i].port, dev->port, i+1);
  194. }
  195. }
  196. static void usb_hub_handle_reset(USBDevice *dev)
  197. {
  198. /* XXX: do it */
  199. }
  200. static int usb_hub_handle_control(USBDevice *dev, USBPacket *p,
  201. int request, int value, int index, int length, uint8_t *data)
  202. {
  203. USBHubState *s = (USBHubState *)dev;
  204. int ret;
  205. ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
  206. if (ret >= 0) {
  207. return ret;
  208. }
  209. switch(request) {
  210. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  211. if (value == 0 && index != 0x81) { /* clear ep halt */
  212. goto fail;
  213. }
  214. ret = 0;
  215. break;
  216. case DeviceRequest | USB_REQ_GET_INTERFACE:
  217. data[0] = 0;
  218. ret = 1;
  219. break;
  220. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  221. ret = 0;
  222. break;
  223. /* usb specific requests */
  224. case GetHubStatus:
  225. data[0] = 0;
  226. data[1] = 0;
  227. data[2] = 0;
  228. data[3] = 0;
  229. ret = 4;
  230. break;
  231. case GetPortStatus:
  232. {
  233. unsigned int n = index - 1;
  234. USBHubPort *port;
  235. if (n >= NUM_PORTS) {
  236. goto fail;
  237. }
  238. port = &s->ports[n];
  239. data[0] = port->wPortStatus;
  240. data[1] = port->wPortStatus >> 8;
  241. data[2] = port->wPortChange;
  242. data[3] = port->wPortChange >> 8;
  243. ret = 4;
  244. }
  245. break;
  246. case SetHubFeature:
  247. case ClearHubFeature:
  248. if (value == 0 || value == 1) {
  249. } else {
  250. goto fail;
  251. }
  252. ret = 0;
  253. break;
  254. case SetPortFeature:
  255. {
  256. unsigned int n = index - 1;
  257. USBHubPort *port;
  258. USBDevice *dev;
  259. if (n >= NUM_PORTS) {
  260. goto fail;
  261. }
  262. port = &s->ports[n];
  263. dev = port->port.dev;
  264. switch(value) {
  265. case PORT_SUSPEND:
  266. port->wPortStatus |= PORT_STAT_SUSPEND;
  267. break;
  268. case PORT_RESET:
  269. if (dev) {
  270. usb_send_msg(dev, USB_MSG_RESET);
  271. port->wPortChange |= PORT_STAT_C_RESET;
  272. /* set enable bit */
  273. port->wPortStatus |= PORT_STAT_ENABLE;
  274. }
  275. break;
  276. case PORT_POWER:
  277. break;
  278. default:
  279. goto fail;
  280. }
  281. ret = 0;
  282. }
  283. break;
  284. case ClearPortFeature:
  285. {
  286. unsigned int n = index - 1;
  287. USBHubPort *port;
  288. if (n >= NUM_PORTS) {
  289. goto fail;
  290. }
  291. port = &s->ports[n];
  292. switch(value) {
  293. case PORT_ENABLE:
  294. port->wPortStatus &= ~PORT_STAT_ENABLE;
  295. break;
  296. case PORT_C_ENABLE:
  297. port->wPortChange &= ~PORT_STAT_C_ENABLE;
  298. break;
  299. case PORT_SUSPEND:
  300. port->wPortStatus &= ~PORT_STAT_SUSPEND;
  301. break;
  302. case PORT_C_SUSPEND:
  303. port->wPortChange &= ~PORT_STAT_C_SUSPEND;
  304. break;
  305. case PORT_C_CONNECTION:
  306. port->wPortChange &= ~PORT_STAT_C_CONNECTION;
  307. break;
  308. case PORT_C_OVERCURRENT:
  309. port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
  310. break;
  311. case PORT_C_RESET:
  312. port->wPortChange &= ~PORT_STAT_C_RESET;
  313. break;
  314. default:
  315. goto fail;
  316. }
  317. ret = 0;
  318. }
  319. break;
  320. case GetHubDescriptor:
  321. {
  322. unsigned int n, limit, var_hub_size = 0;
  323. memcpy(data, qemu_hub_hub_descriptor,
  324. sizeof(qemu_hub_hub_descriptor));
  325. data[2] = NUM_PORTS;
  326. /* fill DeviceRemovable bits */
  327. limit = ((NUM_PORTS + 1 + 7) / 8) + 7;
  328. for (n = 7; n < limit; n++) {
  329. data[n] = 0x00;
  330. var_hub_size++;
  331. }
  332. /* fill PortPwrCtrlMask bits */
  333. limit = limit + ((NUM_PORTS + 7) / 8);
  334. for (;n < limit; n++) {
  335. data[n] = 0xff;
  336. var_hub_size++;
  337. }
  338. ret = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
  339. data[0] = ret;
  340. break;
  341. }
  342. default:
  343. fail:
  344. ret = USB_RET_STALL;
  345. break;
  346. }
  347. return ret;
  348. }
  349. static int usb_hub_handle_data(USBDevice *dev, USBPacket *p)
  350. {
  351. USBHubState *s = (USBHubState *)dev;
  352. int ret;
  353. switch(p->pid) {
  354. case USB_TOKEN_IN:
  355. if (p->devep == 1) {
  356. USBHubPort *port;
  357. unsigned int status;
  358. int i, n;
  359. n = (NUM_PORTS + 1 + 7) / 8;
  360. if (p->len == 1) { /* FreeBSD workaround */
  361. n = 1;
  362. } else if (n > p->len) {
  363. return USB_RET_BABBLE;
  364. }
  365. status = 0;
  366. for(i = 0; i < NUM_PORTS; i++) {
  367. port = &s->ports[i];
  368. if (port->wPortChange)
  369. status |= (1 << (i + 1));
  370. }
  371. if (status != 0) {
  372. for(i = 0; i < n; i++) {
  373. p->data[i] = status >> (8 * i);
  374. }
  375. ret = n;
  376. } else {
  377. ret = USB_RET_NAK; /* usb11 11.13.1 */
  378. }
  379. } else {
  380. goto fail;
  381. }
  382. break;
  383. case USB_TOKEN_OUT:
  384. default:
  385. fail:
  386. ret = USB_RET_STALL;
  387. break;
  388. }
  389. return ret;
  390. }
  391. static int usb_hub_broadcast_packet(USBHubState *s, USBPacket *p)
  392. {
  393. USBHubPort *port;
  394. USBDevice *dev;
  395. int i, ret;
  396. for(i = 0; i < NUM_PORTS; i++) {
  397. port = &s->ports[i];
  398. dev = port->port.dev;
  399. if (dev && (port->wPortStatus & PORT_STAT_ENABLE)) {
  400. ret = usb_handle_packet(dev, p);
  401. if (ret != USB_RET_NODEV) {
  402. return ret;
  403. }
  404. }
  405. }
  406. return USB_RET_NODEV;
  407. }
  408. static int usb_hub_handle_packet(USBDevice *dev, USBPacket *p)
  409. {
  410. USBHubState *s = (USBHubState *)dev;
  411. #if defined(DEBUG) && 0
  412. printf("usb_hub: pid=0x%x\n", pid);
  413. #endif
  414. if (dev->state == USB_STATE_DEFAULT &&
  415. dev->addr != 0 &&
  416. p->devaddr != dev->addr &&
  417. (p->pid == USB_TOKEN_SETUP ||
  418. p->pid == USB_TOKEN_OUT ||
  419. p->pid == USB_TOKEN_IN)) {
  420. /* broadcast the packet to the devices */
  421. return usb_hub_broadcast_packet(s, p);
  422. }
  423. return usb_generic_handle_packet(dev, p);
  424. }
  425. static void usb_hub_handle_destroy(USBDevice *dev)
  426. {
  427. USBHubState *s = (USBHubState *)dev;
  428. int i;
  429. for (i = 0; i < NUM_PORTS; i++) {
  430. usb_unregister_port(usb_bus_from_device(dev),
  431. &s->ports[i].port);
  432. }
  433. }
  434. static USBPortOps usb_hub_port_ops = {
  435. .attach = usb_hub_attach,
  436. .detach = usb_hub_detach,
  437. .child_detach = usb_hub_child_detach,
  438. .wakeup = usb_hub_wakeup,
  439. .complete = usb_hub_complete,
  440. };
  441. static int usb_hub_initfn(USBDevice *dev)
  442. {
  443. USBHubState *s = DO_UPCAST(USBHubState, dev, dev);
  444. USBHubPort *port;
  445. int i;
  446. usb_desc_init(dev);
  447. for (i = 0; i < NUM_PORTS; i++) {
  448. port = &s->ports[i];
  449. usb_register_port(usb_bus_from_device(dev),
  450. &port->port, s, i, &usb_hub_port_ops,
  451. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
  452. port->wPortStatus = PORT_STAT_POWER;
  453. port->wPortChange = 0;
  454. }
  455. return 0;
  456. }
  457. static const VMStateDescription vmstate_usb_hub_port = {
  458. .name = "usb-hub-port",
  459. .version_id = 1,
  460. .minimum_version_id = 1,
  461. .fields = (VMStateField []) {
  462. VMSTATE_UINT16(wPortStatus, USBHubPort),
  463. VMSTATE_UINT16(wPortChange, USBHubPort),
  464. VMSTATE_END_OF_LIST()
  465. }
  466. };
  467. static const VMStateDescription vmstate_usb_hub = {
  468. .name = "usb-hub",
  469. .version_id = 1,
  470. .minimum_version_id = 1,
  471. .fields = (VMStateField []) {
  472. VMSTATE_USB_DEVICE(dev, USBHubState),
  473. VMSTATE_STRUCT_ARRAY(ports, USBHubState, NUM_PORTS, 0,
  474. vmstate_usb_hub_port, USBHubPort),
  475. VMSTATE_END_OF_LIST()
  476. }
  477. };
  478. static struct USBDeviceInfo hub_info = {
  479. .product_desc = "QEMU USB Hub",
  480. .qdev.name = "usb-hub",
  481. .qdev.fw_name = "hub",
  482. .qdev.size = sizeof(USBHubState),
  483. .qdev.vmsd = &vmstate_usb_hub,
  484. .usb_desc = &desc_hub,
  485. .init = usb_hub_initfn,
  486. .handle_packet = usb_hub_handle_packet,
  487. .handle_attach = usb_hub_handle_attach,
  488. .handle_reset = usb_hub_handle_reset,
  489. .handle_control = usb_hub_handle_control,
  490. .handle_data = usb_hub_handle_data,
  491. .handle_destroy = usb_hub_handle_destroy,
  492. };
  493. static void usb_hub_register_devices(void)
  494. {
  495. usb_qdev_register(&hub_info);
  496. }
  497. device_init(usb_hub_register_devices)