2
0

dev-hub.c 17 KB

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