sandbox_hub.c 7.9 KB

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