sandbox_hub.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * (C) Copyright 2015 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <usb.h>
  10. #include <dm/device-internal.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. /* We only support up to 8 */
  13. #define SANDBOX_NUM_PORTS 4
  14. struct sandbox_hub_platdata {
  15. struct usb_dev_platdata plat;
  16. int port; /* Port number (numbered from 0) */
  17. };
  18. enum {
  19. STRING_MANUFACTURER = 1,
  20. STRING_PRODUCT,
  21. STRING_SERIAL,
  22. STRING_count,
  23. };
  24. static struct usb_string hub_strings[] = {
  25. {STRING_MANUFACTURER, "sandbox"},
  26. {STRING_PRODUCT, "hub"},
  27. {STRING_SERIAL, "2345"},
  28. {},
  29. };
  30. static struct usb_device_descriptor hub_device_desc = {
  31. .bLength = sizeof(hub_device_desc),
  32. .bDescriptorType = USB_DT_DEVICE,
  33. .bcdUSB = __constant_cpu_to_le16(0x0200),
  34. .bDeviceClass = USB_CLASS_HUB,
  35. .bDeviceSubClass = 0,
  36. .bDeviceProtocol = 0,
  37. .idVendor = __constant_cpu_to_le16(0x1234),
  38. .idProduct = __constant_cpu_to_le16(0x5678),
  39. .iManufacturer = STRING_MANUFACTURER,
  40. .iProduct = STRING_PRODUCT,
  41. .iSerialNumber = STRING_SERIAL,
  42. .bNumConfigurations = 1,
  43. };
  44. static struct usb_config_descriptor hub_config1 = {
  45. .bLength = sizeof(hub_config1),
  46. .bDescriptorType = USB_DT_CONFIG,
  47. /* wTotalLength is set up by usb-emul-uclass */
  48. .bNumInterfaces = 1,
  49. .bConfigurationValue = 0,
  50. .iConfiguration = 0,
  51. .bmAttributes = 1 << 7,
  52. .bMaxPower = 50,
  53. };
  54. static struct usb_interface_descriptor hub_interface0 = {
  55. .bLength = sizeof(hub_interface0),
  56. .bDescriptorType = USB_DT_INTERFACE,
  57. .bInterfaceNumber = 0,
  58. .bAlternateSetting = 0,
  59. .bNumEndpoints = 1,
  60. .bInterfaceClass = USB_CLASS_HUB,
  61. .bInterfaceSubClass = 0,
  62. .bInterfaceProtocol = US_PR_CB,
  63. .iInterface = 0,
  64. };
  65. static struct usb_endpoint_descriptor hub_endpoint0_in = {
  66. .bLength = USB_DT_ENDPOINT_SIZE,
  67. .bDescriptorType = USB_DT_ENDPOINT,
  68. .bEndpointAddress = 1 | USB_DIR_IN,
  69. .bmAttributes = USB_ENDPOINT_XFER_INT,
  70. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  71. .bInterval = 0,
  72. };
  73. static struct usb_hub_descriptor hub_desc = {
  74. .bLength = sizeof(hub_desc),
  75. .bDescriptorType = USB_DT_HUB,
  76. .bNbrPorts = SANDBOX_NUM_PORTS,
  77. .wHubCharacteristics = __constant_cpu_to_le16(1 << 0 | 1 << 3 |
  78. 1 << 7),
  79. .bPwrOn2PwrGood = 2,
  80. .bHubContrCurrent = 5,
  81. .DeviceRemovable = {0, 0xff}, /* all ports removeable */
  82. #if SANDBOX_NUM_PORTS > 8
  83. #error "This code sets up an incorrect mask"
  84. #endif
  85. };
  86. static void *hub_desc_list[] = {
  87. &hub_device_desc,
  88. &hub_config1,
  89. &hub_interface0,
  90. &hub_endpoint0_in,
  91. &hub_desc,
  92. NULL,
  93. };
  94. struct sandbox_hub_priv {
  95. int status[SANDBOX_NUM_PORTS];
  96. int change[SANDBOX_NUM_PORTS];
  97. };
  98. static struct udevice *hub_find_device(struct udevice *hub, int port)
  99. {
  100. struct udevice *dev;
  101. for (device_find_first_child(hub, &dev);
  102. dev;
  103. device_find_next_child(&dev)) {
  104. struct sandbox_hub_platdata *plat;
  105. plat = dev_get_parent_platdata(dev);
  106. if (plat->port == port)
  107. return dev;
  108. }
  109. return NULL;
  110. }
  111. static int clrset_post_state(struct udevice *hub, int port, int clear, int set)
  112. {
  113. struct sandbox_hub_priv *priv = dev_get_priv(hub);
  114. int *status = &priv->status[port];
  115. int *change = &priv->change[port];
  116. int ret = 0;
  117. if ((clear | set) & USB_PORT_STAT_POWER) {
  118. struct udevice *dev = hub_find_device(hub, port);
  119. if (dev) {
  120. if (set & USB_PORT_STAT_POWER) {
  121. ret = device_probe(dev);
  122. debug("%s: %s: power on, probed, ret=%d\n",
  123. __func__, dev->name, ret);
  124. if (!ret) {
  125. set |= USB_PORT_STAT_CONNECTION |
  126. USB_PORT_STAT_ENABLE;
  127. }
  128. } else if (clear & USB_PORT_STAT_POWER) {
  129. debug("%s: %s: power off, removed, ret=%d\n",
  130. __func__, dev->name, ret);
  131. ret = device_remove(dev);
  132. clear |= USB_PORT_STAT_CONNECTION;
  133. }
  134. }
  135. }
  136. *change |= *status & clear;
  137. *change |= ~*status & set;
  138. *change &= 0x1f;
  139. *status = (*status & ~clear) | set;
  140. return ret;
  141. }
  142. static int sandbox_hub_submit_control_msg(struct udevice *bus,
  143. struct usb_device *udev,
  144. unsigned long pipe,
  145. void *buffer, int length,
  146. struct devrequest *setup)
  147. {
  148. struct sandbox_hub_priv *priv = dev_get_priv(bus);
  149. int ret = 0;
  150. if (pipe == usb_rcvctrlpipe(udev, 0)) {
  151. switch (setup->requesttype) {
  152. case USB_RT_HUB | USB_DIR_IN:
  153. switch (setup->request) {
  154. case USB_REQ_GET_STATUS: {
  155. struct usb_hub_status *hubsts = buffer;
  156. hubsts->wHubStatus = 0;
  157. hubsts->wHubChange = 0;
  158. udev->status = 0;
  159. udev->act_len = sizeof(*hubsts);
  160. return 0;
  161. }
  162. default:
  163. debug("%s: rx ctl requesttype=%x, request=%x\n",
  164. __func__, setup->requesttype,
  165. setup->request);
  166. break;
  167. }
  168. case USB_RT_PORT | USB_DIR_IN:
  169. switch (setup->request) {
  170. case USB_REQ_GET_STATUS: {
  171. struct usb_port_status *portsts = buffer;
  172. int port;
  173. port = (setup->index & USB_HUB_PORT_MASK) - 1;
  174. portsts->wPortStatus = priv->status[port];
  175. portsts->wPortChange = priv->change[port];
  176. udev->status = 0;
  177. udev->act_len = sizeof(*portsts);
  178. return 0;
  179. }
  180. }
  181. default:
  182. debug("%s: rx ctl requesttype=%x, request=%x\n",
  183. __func__, setup->requesttype, setup->request);
  184. break;
  185. }
  186. } else if (pipe == usb_sndctrlpipe(udev, 0)) {
  187. switch (setup->requesttype) {
  188. case USB_RT_PORT:
  189. switch (setup->request) {
  190. case USB_REQ_SET_FEATURE: {
  191. int port;
  192. port = (setup->index & USB_HUB_PORT_MASK) - 1;
  193. debug("set feature port=%x, feature=%x\n",
  194. port, setup->value);
  195. if (setup->value < USB_PORT_FEAT_C_CONNECTION) {
  196. ret = clrset_post_state(bus, port, 0,
  197. 1 << setup->value);
  198. } else {
  199. debug(" ** Invalid feature\n");
  200. }
  201. return ret;
  202. }
  203. case USB_REQ_CLEAR_FEATURE: {
  204. int port;
  205. port = (setup->index & USB_HUB_PORT_MASK) - 1;
  206. debug("clear feature port=%x, feature=%x\n",
  207. port, setup->value);
  208. if (setup->value < USB_PORT_FEAT_C_CONNECTION) {
  209. ret = clrset_post_state(bus, port,
  210. 1 << setup->value, 0);
  211. } else {
  212. priv->change[port] &= 1 <<
  213. (setup->value - 16);
  214. }
  215. udev->status = 0;
  216. return 0;
  217. }
  218. default:
  219. debug("%s: tx ctl requesttype=%x, request=%x\n",
  220. __func__, setup->requesttype,
  221. setup->request);
  222. break;
  223. }
  224. default:
  225. debug("%s: tx ctl requesttype=%x, request=%x\n",
  226. __func__, setup->requesttype, setup->request);
  227. break;
  228. }
  229. }
  230. debug("pipe=%lx\n", pipe);
  231. return -EIO;
  232. }
  233. static int sandbox_hub_bind(struct udevice *dev)
  234. {
  235. return usb_emul_setup_device(dev, PACKET_SIZE_64, hub_strings,
  236. hub_desc_list);
  237. }
  238. static int sandbox_child_post_bind(struct udevice *dev)
  239. {
  240. struct sandbox_hub_platdata *plat = dev_get_parent_platdata(dev);
  241. plat->port = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "reg", -1);
  242. return 0;
  243. }
  244. static const struct dm_usb_ops sandbox_usb_hub_ops = {
  245. .control = sandbox_hub_submit_control_msg,
  246. };
  247. static const struct udevice_id sandbox_usb_hub_ids[] = {
  248. { .compatible = "sandbox,usb-hub" },
  249. { }
  250. };
  251. U_BOOT_DRIVER(usb_sandbox_hub) = {
  252. .name = "usb_sandbox_hub",
  253. .id = UCLASS_USB_EMUL,
  254. .of_match = sandbox_usb_hub_ids,
  255. .bind = sandbox_hub_bind,
  256. .ops = &sandbox_usb_hub_ops,
  257. .priv_auto_alloc_size = sizeof(struct sandbox_hub_priv),
  258. .per_child_platdata_auto_alloc_size =
  259. sizeof(struct sandbox_hub_platdata),
  260. .child_post_bind = sandbox_child_post_bind,
  261. };