sandbox_hub.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. {
  82. {
  83. /* all ports removeable */
  84. .DeviceRemovable = {0, 0xff}
  85. }
  86. }
  87. #if SANDBOX_NUM_PORTS > 8
  88. #error "This code sets up an incorrect mask"
  89. #endif
  90. };
  91. static void *hub_desc_list[] = {
  92. &hub_device_desc,
  93. &hub_config1,
  94. &hub_interface0,
  95. &hub_endpoint0_in,
  96. &hub_desc,
  97. NULL,
  98. };
  99. struct sandbox_hub_priv {
  100. int status[SANDBOX_NUM_PORTS];
  101. int change[SANDBOX_NUM_PORTS];
  102. };
  103. static struct udevice *hub_find_device(struct udevice *hub, int port)
  104. {
  105. struct udevice *dev;
  106. for (device_find_first_child(hub, &dev);
  107. dev;
  108. device_find_next_child(&dev)) {
  109. struct sandbox_hub_platdata *plat;
  110. plat = dev_get_parent_platdata(dev);
  111. if (plat->port == port)
  112. return dev;
  113. }
  114. return NULL;
  115. }
  116. static int clrset_post_state(struct udevice *hub, int port, int clear, int set)
  117. {
  118. struct sandbox_hub_priv *priv = dev_get_priv(hub);
  119. int *status = &priv->status[port];
  120. int *change = &priv->change[port];
  121. int ret = 0;
  122. if ((clear | set) & USB_PORT_STAT_POWER) {
  123. struct udevice *dev = hub_find_device(hub, port);
  124. if (dev) {
  125. if (set & USB_PORT_STAT_POWER) {
  126. ret = device_probe(dev);
  127. debug("%s: %s: power on, probed, ret=%d\n",
  128. __func__, dev->name, ret);
  129. if (!ret) {
  130. set |= USB_PORT_STAT_CONNECTION |
  131. USB_PORT_STAT_ENABLE;
  132. }
  133. } else if (clear & USB_PORT_STAT_POWER) {
  134. debug("%s: %s: power off, removed, ret=%d\n",
  135. __func__, dev->name, ret);
  136. ret = device_remove(dev, DM_REMOVE_NORMAL);
  137. clear |= USB_PORT_STAT_CONNECTION;
  138. }
  139. }
  140. }
  141. *change |= *status & clear;
  142. *change |= ~*status & set;
  143. *change &= 0x1f;
  144. *status = (*status & ~clear) | set;
  145. return ret;
  146. }
  147. static int sandbox_hub_submit_control_msg(struct udevice *bus,
  148. struct usb_device *udev,
  149. unsigned long pipe,
  150. void *buffer, int length,
  151. struct devrequest *setup)
  152. {
  153. struct sandbox_hub_priv *priv = dev_get_priv(bus);
  154. int ret = 0;
  155. if (pipe == usb_rcvctrlpipe(udev, 0)) {
  156. switch (setup->requesttype) {
  157. case USB_RT_HUB | USB_DIR_IN:
  158. switch (setup->request) {
  159. case USB_REQ_GET_STATUS: {
  160. struct usb_hub_status *hubsts = buffer;
  161. hubsts->wHubStatus = 0;
  162. hubsts->wHubChange = 0;
  163. udev->status = 0;
  164. udev->act_len = sizeof(*hubsts);
  165. return 0;
  166. }
  167. default:
  168. debug("%s: rx ctl requesttype=%x, request=%x\n",
  169. __func__, setup->requesttype,
  170. setup->request);
  171. break;
  172. }
  173. case USB_RT_PORT | USB_DIR_IN:
  174. switch (setup->request) {
  175. case USB_REQ_GET_STATUS: {
  176. struct usb_port_status *portsts = buffer;
  177. int port;
  178. port = (setup->index & USB_HUB_PORT_MASK) - 1;
  179. portsts->wPortStatus = priv->status[port];
  180. portsts->wPortChange = priv->change[port];
  181. udev->status = 0;
  182. udev->act_len = sizeof(*portsts);
  183. return 0;
  184. }
  185. }
  186. default:
  187. debug("%s: rx ctl requesttype=%x, request=%x\n",
  188. __func__, setup->requesttype, setup->request);
  189. break;
  190. }
  191. } else if (pipe == usb_sndctrlpipe(udev, 0)) {
  192. switch (setup->requesttype) {
  193. case USB_RT_PORT:
  194. switch (setup->request) {
  195. case USB_REQ_SET_FEATURE: {
  196. int port;
  197. port = (setup->index & USB_HUB_PORT_MASK) - 1;
  198. debug("set feature port=%x, feature=%x\n",
  199. port, setup->value);
  200. if (setup->value < USB_PORT_FEAT_C_CONNECTION) {
  201. ret = clrset_post_state(bus, port, 0,
  202. 1 << setup->value);
  203. } else {
  204. debug(" ** Invalid feature\n");
  205. }
  206. return ret;
  207. }
  208. case USB_REQ_CLEAR_FEATURE: {
  209. int port;
  210. port = (setup->index & USB_HUB_PORT_MASK) - 1;
  211. debug("clear feature port=%x, feature=%x\n",
  212. port, setup->value);
  213. if (setup->value < USB_PORT_FEAT_C_CONNECTION) {
  214. ret = clrset_post_state(bus, port,
  215. 1 << setup->value, 0);
  216. } else {
  217. priv->change[port] &= 1 <<
  218. (setup->value - 16);
  219. }
  220. udev->status = 0;
  221. return 0;
  222. }
  223. default:
  224. debug("%s: tx ctl requesttype=%x, request=%x\n",
  225. __func__, setup->requesttype,
  226. setup->request);
  227. break;
  228. }
  229. default:
  230. debug("%s: tx ctl requesttype=%x, request=%x\n",
  231. __func__, setup->requesttype, setup->request);
  232. break;
  233. }
  234. }
  235. debug("pipe=%lx\n", pipe);
  236. return -EIO;
  237. }
  238. static int sandbox_hub_bind(struct udevice *dev)
  239. {
  240. return usb_emul_setup_device(dev, hub_strings, hub_desc_list);
  241. }
  242. static int sandbox_child_post_bind(struct udevice *dev)
  243. {
  244. struct sandbox_hub_platdata *plat = dev_get_parent_platdata(dev);
  245. struct usb_emul_platdata *emul = dev_get_uclass_platdata(dev);
  246. plat->port = dev_read_u32_default(dev, "reg", -1);
  247. emul->port1 = plat->port + 1;
  248. return 0;
  249. }
  250. static const struct dm_usb_ops sandbox_usb_hub_ops = {
  251. .control = sandbox_hub_submit_control_msg,
  252. };
  253. static const struct udevice_id sandbox_usb_hub_ids[] = {
  254. { .compatible = "sandbox,usb-hub" },
  255. { }
  256. };
  257. U_BOOT_DRIVER(usb_sandbox_hub) = {
  258. .name = "usb_sandbox_hub",
  259. .id = UCLASS_USB_EMUL,
  260. .of_match = sandbox_usb_hub_ids,
  261. .bind = sandbox_hub_bind,
  262. .ops = &sandbox_usb_hub_ops,
  263. .priv_auto_alloc_size = sizeof(struct sandbox_hub_priv),
  264. .per_child_platdata_auto_alloc_size =
  265. sizeof(struct sandbox_hub_platdata),
  266. .child_post_bind = sandbox_child_post_bind,
  267. };