usb-sandbox.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/root.h>
  11. DECLARE_GLOBAL_DATA_PTR;
  12. struct sandbox_usb_ctrl {
  13. int rootdev;
  14. };
  15. static void usbmon_trace(struct udevice *bus, ulong pipe,
  16. struct devrequest *setup, struct udevice *emul)
  17. {
  18. static const char types[] = "ZICB";
  19. int type;
  20. type = (pipe & USB_PIPE_TYPE_MASK) >> USB_PIPE_TYPE_SHIFT;
  21. debug("0 0 S %c%c:%d:%03ld:%ld", types[type],
  22. pipe & USB_DIR_IN ? 'i' : 'o',
  23. bus->seq,
  24. (pipe & USB_PIPE_DEV_MASK) >> USB_PIPE_DEV_SHIFT,
  25. (pipe & USB_PIPE_EP_MASK) >> USB_PIPE_EP_SHIFT);
  26. if (setup) {
  27. debug(" s %02x %02x %04x %04x %04x", setup->requesttype,
  28. setup->request, setup->value, setup->index,
  29. setup->length);
  30. }
  31. debug(" %s", emul ? emul->name : "(no emul found)");
  32. debug("\n");
  33. }
  34. static int sandbox_submit_control(struct udevice *bus,
  35. struct usb_device *udev,
  36. unsigned long pipe,
  37. void *buffer, int length,
  38. struct devrequest *setup)
  39. {
  40. struct sandbox_usb_ctrl *ctrl = dev_get_priv(bus);
  41. struct udevice *emul;
  42. int ret;
  43. /* Just use child of dev as emulator? */
  44. debug("%s: bus=%s\n", __func__, bus->name);
  45. ret = usb_emul_find(bus, pipe, &emul);
  46. usbmon_trace(bus, pipe, setup, emul);
  47. if (ret)
  48. return ret;
  49. if (usb_pipedevice(pipe) == ctrl->rootdev) {
  50. if (setup->request == USB_REQ_SET_ADDRESS) {
  51. debug("%s: Set root hub's USB address\n", __func__);
  52. ctrl->rootdev = le16_to_cpu(setup->value);
  53. }
  54. }
  55. ret = usb_emul_control(emul, udev, pipe, buffer, length, setup);
  56. if (ret < 0) {
  57. debug("ret=%d\n", ret);
  58. udev->status = ret;
  59. udev->act_len = 0;
  60. } else {
  61. udev->status = 0;
  62. udev->act_len = ret;
  63. }
  64. return ret;
  65. }
  66. static int sandbox_submit_bulk(struct udevice *bus, struct usb_device *udev,
  67. unsigned long pipe, void *buffer, int length)
  68. {
  69. struct udevice *emul;
  70. int ret;
  71. /* Just use child of dev as emulator? */
  72. debug("%s: bus=%s\n", __func__, bus->name);
  73. ret = usb_emul_find(bus, pipe, &emul);
  74. usbmon_trace(bus, pipe, NULL, emul);
  75. if (ret)
  76. return ret;
  77. ret = usb_emul_bulk(emul, udev, pipe, buffer, length);
  78. if (ret < 0) {
  79. debug("ret=%d\n", ret);
  80. udev->status = ret;
  81. udev->act_len = 0;
  82. } else {
  83. udev->status = 0;
  84. udev->act_len = ret;
  85. }
  86. return ret;
  87. }
  88. static int sandbox_submit_int(struct udevice *bus, struct usb_device *udev,
  89. unsigned long pipe, void *buffer, int length,
  90. int interval)
  91. {
  92. struct udevice *emul;
  93. int ret;
  94. /* Just use child of dev as emulator? */
  95. debug("%s: bus=%s\n", __func__, bus->name);
  96. ret = usb_emul_find(bus, pipe, &emul);
  97. usbmon_trace(bus, pipe, NULL, emul);
  98. if (ret)
  99. return ret;
  100. ret = usb_emul_int(emul, udev, pipe, buffer, length, interval);
  101. return ret;
  102. }
  103. static int sandbox_alloc_device(struct udevice *dev, struct usb_device *udev)
  104. {
  105. struct sandbox_usb_ctrl *ctrl = dev_get_priv(dev);
  106. /*
  107. * Root hub will be the first device to be initailized.
  108. * If this device is a root hub, initialize its device speed
  109. * to high speed as we are a USB 2.0 controller.
  110. */
  111. if (ctrl->rootdev == 0)
  112. udev->speed = USB_SPEED_HIGH;
  113. return 0;
  114. }
  115. static int sandbox_usb_probe(struct udevice *dev)
  116. {
  117. return 0;
  118. }
  119. static const struct dm_usb_ops sandbox_usb_ops = {
  120. .control = sandbox_submit_control,
  121. .bulk = sandbox_submit_bulk,
  122. .interrupt = sandbox_submit_int,
  123. .alloc_device = sandbox_alloc_device,
  124. };
  125. static const struct udevice_id sandbox_usb_ids[] = {
  126. { .compatible = "sandbox,usb" },
  127. { }
  128. };
  129. U_BOOT_DRIVER(usb_sandbox) = {
  130. .name = "usb_sandbox",
  131. .id = UCLASS_USB,
  132. .of_match = sandbox_usb_ids,
  133. .probe = sandbox_usb_probe,
  134. .ops = &sandbox_usb_ops,
  135. .priv_auto_alloc_size = sizeof(struct sandbox_usb_ctrl),
  136. };