usb-sandbox.c 3.7 KB

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