usb-hub.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. //#define DEBUG
  27. #define MAX_PORTS 8
  28. typedef struct USBHubPort {
  29. USBPort port;
  30. uint16_t wPortStatus;
  31. uint16_t wPortChange;
  32. } USBHubPort;
  33. typedef struct USBHubState {
  34. USBDevice dev;
  35. int nb_ports;
  36. USBHubPort ports[MAX_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. static const uint8_t qemu_hub_dev_descriptor[] = {
  77. 0x12, /* u8 bLength; */
  78. 0x01, /* u8 bDescriptorType; Device */
  79. 0x10, 0x01, /* u16 bcdUSB; v1.1 */
  80. 0x09, /* u8 bDeviceClass; HUB_CLASSCODE */
  81. 0x00, /* u8 bDeviceSubClass; */
  82. 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
  83. 0x08, /* u8 bMaxPacketSize0; 8 Bytes */
  84. 0x00, 0x00, /* u16 idVendor; */
  85. 0x00, 0x00, /* u16 idProduct; */
  86. 0x01, 0x01, /* u16 bcdDevice */
  87. 0x03, /* u8 iManufacturer; */
  88. 0x02, /* u8 iProduct; */
  89. 0x01, /* u8 iSerialNumber; */
  90. 0x01 /* u8 bNumConfigurations; */
  91. };
  92. /* XXX: patch interrupt size */
  93. static const uint8_t qemu_hub_config_descriptor[] = {
  94. /* one configuration */
  95. 0x09, /* u8 bLength; */
  96. 0x02, /* u8 bDescriptorType; Configuration */
  97. 0x19, 0x00, /* u16 wTotalLength; */
  98. 0x01, /* u8 bNumInterfaces; (1) */
  99. 0x01, /* u8 bConfigurationValue; */
  100. 0x00, /* u8 iConfiguration; */
  101. 0xc0, /* u8 bmAttributes;
  102. Bit 7: must be set,
  103. 6: Self-powered,
  104. 5: Remote wakeup,
  105. 4..0: resvd */
  106. 0x00, /* u8 MaxPower; */
  107. /* USB 1.1:
  108. * USB 2.0, single TT organization (mandatory):
  109. * one interface, protocol 0
  110. *
  111. * USB 2.0, multiple TT organization (optional):
  112. * two interfaces, protocols 1 (like single TT)
  113. * and 2 (multiple TT mode) ... config is
  114. * sometimes settable
  115. * NOT IMPLEMENTED
  116. */
  117. /* one interface */
  118. 0x09, /* u8 if_bLength; */
  119. 0x04, /* u8 if_bDescriptorType; Interface */
  120. 0x00, /* u8 if_bInterfaceNumber; */
  121. 0x00, /* u8 if_bAlternateSetting; */
  122. 0x01, /* u8 if_bNumEndpoints; */
  123. 0x09, /* u8 if_bInterfaceClass; HUB_CLASSCODE */
  124. 0x00, /* u8 if_bInterfaceSubClass; */
  125. 0x00, /* u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
  126. 0x00, /* u8 if_iInterface; */
  127. /* one endpoint (status change endpoint) */
  128. 0x07, /* u8 ep_bLength; */
  129. 0x05, /* u8 ep_bDescriptorType; Endpoint */
  130. 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
  131. 0x03, /* u8 ep_bmAttributes; Interrupt */
  132. 0x02, 0x00, /* u16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
  133. 0xff /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  134. };
  135. static const uint8_t qemu_hub_hub_descriptor[] =
  136. {
  137. 0x00, /* u8 bLength; patched in later */
  138. 0x29, /* u8 bDescriptorType; Hub-descriptor */
  139. 0x00, /* u8 bNbrPorts; (patched later) */
  140. 0x0a, /* u16 wHubCharacteristics; */
  141. 0x00, /* (per-port OC, no power switching) */
  142. 0x01, /* u8 bPwrOn2pwrGood; 2ms */
  143. 0x00 /* u8 bHubContrCurrent; 0 mA */
  144. /* DeviceRemovable and PortPwrCtrlMask patched in later */
  145. };
  146. static void usb_hub_attach(USBPort *port1, USBDevice *dev)
  147. {
  148. USBHubState *s = port1->opaque;
  149. USBHubPort *port = &s->ports[port1->index];
  150. if (dev) {
  151. if (port->port.dev)
  152. usb_attach(port1, NULL);
  153. port->wPortStatus |= PORT_STAT_CONNECTION;
  154. port->wPortChange |= PORT_STAT_C_CONNECTION;
  155. if (dev->speed == USB_SPEED_LOW)
  156. port->wPortStatus |= PORT_STAT_LOW_SPEED;
  157. else
  158. port->wPortStatus &= ~PORT_STAT_LOW_SPEED;
  159. port->port.dev = dev;
  160. /* send the attach message */
  161. usb_send_msg(dev, USB_MSG_ATTACH);
  162. } else {
  163. dev = port->port.dev;
  164. if (dev) {
  165. port->wPortStatus &= ~PORT_STAT_CONNECTION;
  166. port->wPortChange |= PORT_STAT_C_CONNECTION;
  167. if (port->wPortStatus & PORT_STAT_ENABLE) {
  168. port->wPortStatus &= ~PORT_STAT_ENABLE;
  169. port->wPortChange |= PORT_STAT_C_ENABLE;
  170. }
  171. /* send the detach message */
  172. usb_send_msg(dev, USB_MSG_DETACH);
  173. port->port.dev = NULL;
  174. }
  175. }
  176. }
  177. static void usb_hub_handle_reset(USBDevice *dev)
  178. {
  179. /* XXX: do it */
  180. }
  181. static int usb_hub_handle_control(USBDevice *dev, int request, int value,
  182. int index, int length, uint8_t *data)
  183. {
  184. USBHubState *s = (USBHubState *)dev;
  185. int ret;
  186. switch(request) {
  187. case DeviceRequest | USB_REQ_GET_STATUS:
  188. data[0] = (1 << USB_DEVICE_SELF_POWERED) |
  189. (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
  190. data[1] = 0x00;
  191. ret = 2;
  192. break;
  193. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  194. if (value == USB_DEVICE_REMOTE_WAKEUP) {
  195. dev->remote_wakeup = 0;
  196. } else {
  197. goto fail;
  198. }
  199. ret = 0;
  200. break;
  201. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  202. if (value == 0 && index != 0x81) { /* clear ep halt */
  203. goto fail;
  204. }
  205. ret = 0;
  206. break;
  207. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  208. if (value == USB_DEVICE_REMOTE_WAKEUP) {
  209. dev->remote_wakeup = 1;
  210. } else {
  211. goto fail;
  212. }
  213. ret = 0;
  214. break;
  215. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  216. dev->addr = value;
  217. ret = 0;
  218. break;
  219. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  220. switch(value >> 8) {
  221. case USB_DT_DEVICE:
  222. memcpy(data, qemu_hub_dev_descriptor,
  223. sizeof(qemu_hub_dev_descriptor));
  224. ret = sizeof(qemu_hub_dev_descriptor);
  225. break;
  226. case USB_DT_CONFIG:
  227. memcpy(data, qemu_hub_config_descriptor,
  228. sizeof(qemu_hub_config_descriptor));
  229. /* status change endpoint size based on number
  230. * of ports */
  231. data[22] = (s->nb_ports + 1 + 7) / 8;
  232. ret = sizeof(qemu_hub_config_descriptor);
  233. break;
  234. case USB_DT_STRING:
  235. switch(value & 0xff) {
  236. case 0:
  237. /* language ids */
  238. data[0] = 4;
  239. data[1] = 3;
  240. data[2] = 0x09;
  241. data[3] = 0x04;
  242. ret = 4;
  243. break;
  244. case 1:
  245. /* serial number */
  246. ret = set_usb_string(data, "314159");
  247. break;
  248. case 2:
  249. /* product description */
  250. ret = set_usb_string(data, "QEMU USB Hub");
  251. break;
  252. case 3:
  253. /* vendor description */
  254. ret = set_usb_string(data, "QEMU " QEMU_VERSION);
  255. break;
  256. default:
  257. goto fail;
  258. }
  259. break;
  260. default:
  261. goto fail;
  262. }
  263. break;
  264. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  265. data[0] = 1;
  266. ret = 1;
  267. break;
  268. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  269. ret = 0;
  270. break;
  271. case DeviceRequest | USB_REQ_GET_INTERFACE:
  272. data[0] = 0;
  273. ret = 1;
  274. break;
  275. case DeviceOutRequest | USB_REQ_SET_INTERFACE:
  276. ret = 0;
  277. break;
  278. /* usb specific requests */
  279. case GetHubStatus:
  280. data[0] = 0;
  281. data[1] = 0;
  282. data[2] = 0;
  283. data[3] = 0;
  284. ret = 4;
  285. break;
  286. case GetPortStatus:
  287. {
  288. unsigned int n = index - 1;
  289. USBHubPort *port;
  290. if (n >= s->nb_ports)
  291. goto fail;
  292. port = &s->ports[n];
  293. data[0] = port->wPortStatus;
  294. data[1] = port->wPortStatus >> 8;
  295. data[2] = port->wPortChange;
  296. data[3] = port->wPortChange >> 8;
  297. ret = 4;
  298. }
  299. break;
  300. case SetHubFeature:
  301. case ClearHubFeature:
  302. if (value == 0 || value == 1) {
  303. } else {
  304. goto fail;
  305. }
  306. ret = 0;
  307. break;
  308. case SetPortFeature:
  309. {
  310. unsigned int n = index - 1;
  311. USBHubPort *port;
  312. USBDevice *dev;
  313. if (n >= s->nb_ports)
  314. goto fail;
  315. port = &s->ports[n];
  316. dev = port->port.dev;
  317. switch(value) {
  318. case PORT_SUSPEND:
  319. port->wPortStatus |= PORT_STAT_SUSPEND;
  320. break;
  321. case PORT_RESET:
  322. if (dev) {
  323. usb_send_msg(dev, USB_MSG_RESET);
  324. port->wPortChange |= PORT_STAT_C_RESET;
  325. /* set enable bit */
  326. port->wPortStatus |= PORT_STAT_ENABLE;
  327. }
  328. break;
  329. case PORT_POWER:
  330. break;
  331. default:
  332. goto fail;
  333. }
  334. ret = 0;
  335. }
  336. break;
  337. case ClearPortFeature:
  338. {
  339. unsigned int n = index - 1;
  340. USBHubPort *port;
  341. USBDevice *dev;
  342. if (n >= s->nb_ports)
  343. goto fail;
  344. port = &s->ports[n];
  345. dev = port->port.dev;
  346. switch(value) {
  347. case PORT_ENABLE:
  348. port->wPortStatus &= ~PORT_STAT_ENABLE;
  349. break;
  350. case PORT_C_ENABLE:
  351. port->wPortChange &= ~PORT_STAT_C_ENABLE;
  352. break;
  353. case PORT_SUSPEND:
  354. port->wPortStatus &= ~PORT_STAT_SUSPEND;
  355. break;
  356. case PORT_C_SUSPEND:
  357. port->wPortChange &= ~PORT_STAT_C_SUSPEND;
  358. break;
  359. case PORT_C_CONNECTION:
  360. port->wPortChange &= ~PORT_STAT_C_CONNECTION;
  361. break;
  362. case PORT_C_OVERCURRENT:
  363. port->wPortChange &= ~PORT_STAT_C_OVERCURRENT;
  364. break;
  365. case PORT_C_RESET:
  366. port->wPortChange &= ~PORT_STAT_C_RESET;
  367. break;
  368. default:
  369. goto fail;
  370. }
  371. ret = 0;
  372. }
  373. break;
  374. case GetHubDescriptor:
  375. {
  376. unsigned int n, limit, var_hub_size = 0;
  377. memcpy(data, qemu_hub_hub_descriptor,
  378. sizeof(qemu_hub_hub_descriptor));
  379. data[2] = s->nb_ports;
  380. /* fill DeviceRemovable bits */
  381. limit = ((s->nb_ports + 1 + 7) / 8) + 7;
  382. for (n = 7; n < limit; n++) {
  383. data[n] = 0x00;
  384. var_hub_size++;
  385. }
  386. /* fill PortPwrCtrlMask bits */
  387. limit = limit + ((s->nb_ports + 7) / 8);
  388. for (;n < limit; n++) {
  389. data[n] = 0xff;
  390. var_hub_size++;
  391. }
  392. ret = sizeof(qemu_hub_hub_descriptor) + var_hub_size;
  393. data[0] = ret;
  394. break;
  395. }
  396. default:
  397. fail:
  398. ret = USB_RET_STALL;
  399. break;
  400. }
  401. return ret;
  402. }
  403. static int usb_hub_handle_data(USBDevice *dev, USBPacket *p)
  404. {
  405. USBHubState *s = (USBHubState *)dev;
  406. int ret;
  407. switch(p->pid) {
  408. case USB_TOKEN_IN:
  409. if (p->devep == 1) {
  410. USBHubPort *port;
  411. unsigned int status;
  412. int i, n;
  413. n = (s->nb_ports + 1 + 7) / 8;
  414. if (p->len == 1) { /* FreeBSD workaround */
  415. n = 1;
  416. } else if (n > p->len) {
  417. return USB_RET_BABBLE;
  418. }
  419. status = 0;
  420. for(i = 0; i < s->nb_ports; i++) {
  421. port = &s->ports[i];
  422. if (port->wPortChange)
  423. status |= (1 << (i + 1));
  424. }
  425. if (status != 0) {
  426. for(i = 0; i < n; i++) {
  427. p->data[i] = status >> (8 * i);
  428. }
  429. ret = n;
  430. } else {
  431. ret = USB_RET_NAK; /* usb11 11.13.1 */
  432. }
  433. } else {
  434. goto fail;
  435. }
  436. break;
  437. case USB_TOKEN_OUT:
  438. default:
  439. fail:
  440. ret = USB_RET_STALL;
  441. break;
  442. }
  443. return ret;
  444. }
  445. static int usb_hub_broadcast_packet(USBHubState *s, USBPacket *p)
  446. {
  447. USBHubPort *port;
  448. USBDevice *dev;
  449. int i, ret;
  450. for(i = 0; i < s->nb_ports; i++) {
  451. port = &s->ports[i];
  452. dev = port->port.dev;
  453. if (dev && (port->wPortStatus & PORT_STAT_ENABLE)) {
  454. ret = dev->handle_packet(dev, p);
  455. if (ret != USB_RET_NODEV) {
  456. return ret;
  457. }
  458. }
  459. }
  460. return USB_RET_NODEV;
  461. }
  462. static int usb_hub_handle_packet(USBDevice *dev, USBPacket *p)
  463. {
  464. USBHubState *s = (USBHubState *)dev;
  465. #if defined(DEBUG) && 0
  466. printf("usb_hub: pid=0x%x\n", pid);
  467. #endif
  468. if (dev->state == USB_STATE_DEFAULT &&
  469. dev->addr != 0 &&
  470. p->devaddr != dev->addr &&
  471. (p->pid == USB_TOKEN_SETUP ||
  472. p->pid == USB_TOKEN_OUT ||
  473. p->pid == USB_TOKEN_IN)) {
  474. /* broadcast the packet to the devices */
  475. return usb_hub_broadcast_packet(s, p);
  476. }
  477. return usb_generic_handle_packet(dev, p);
  478. }
  479. static void usb_hub_handle_destroy(USBDevice *dev)
  480. {
  481. USBHubState *s = (USBHubState *)dev;
  482. qemu_free(s);
  483. }
  484. USBDevice *usb_hub_init(int nb_ports)
  485. {
  486. USBHubState *s;
  487. USBHubPort *port;
  488. int i;
  489. if (nb_ports > MAX_PORTS)
  490. return NULL;
  491. s = qemu_mallocz(sizeof(USBHubState));
  492. s->dev.speed = USB_SPEED_FULL;
  493. s->dev.handle_packet = usb_hub_handle_packet;
  494. /* generic USB device init */
  495. s->dev.handle_reset = usb_hub_handle_reset;
  496. s->dev.handle_control = usb_hub_handle_control;
  497. s->dev.handle_data = usb_hub_handle_data;
  498. s->dev.handle_destroy = usb_hub_handle_destroy;
  499. pstrcpy(s->dev.devname, sizeof(s->dev.devname), "QEMU USB Hub");
  500. s->nb_ports = nb_ports;
  501. for(i = 0; i < s->nb_ports; i++) {
  502. port = &s->ports[i];
  503. qemu_register_usb_port(&port->port, s, i, usb_hub_attach);
  504. port->wPortStatus = PORT_STAT_POWER;
  505. port->wPortChange = 0;
  506. }
  507. return (USBDevice *)s;
  508. }