usb-sandbox.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. static void usbmon_trace(struct udevice *bus, ulong pipe,
  13. struct devrequest *setup, struct udevice *emul)
  14. {
  15. static const char types[] = "ZICB";
  16. int type;
  17. type = (pipe & USB_PIPE_TYPE_MASK) >> USB_PIPE_TYPE_SHIFT;
  18. debug("0 0 S %c%c:%d:%03ld:%ld", types[type],
  19. pipe & USB_DIR_IN ? 'i' : 'o',
  20. bus->seq,
  21. (pipe & USB_PIPE_DEV_MASK) >> USB_PIPE_DEV_SHIFT,
  22. (pipe & USB_PIPE_EP_MASK) >> USB_PIPE_EP_SHIFT);
  23. if (setup) {
  24. debug(" s %02x %02x %04x %04x %04x", setup->requesttype,
  25. setup->request, setup->value, setup->index,
  26. setup->length);
  27. }
  28. debug(" %s", emul ? emul->name : "(no emul found)");
  29. debug("\n");
  30. }
  31. static int sandbox_submit_control(struct udevice *bus,
  32. struct usb_device *udev,
  33. unsigned long pipe,
  34. void *buffer, int length,
  35. struct devrequest *setup)
  36. {
  37. struct udevice *emul;
  38. int ret;
  39. /* Just use child of dev as emulator? */
  40. debug("%s: bus=%s\n", __func__, bus->name);
  41. ret = usb_emul_find(bus, pipe, &emul);
  42. usbmon_trace(bus, pipe, setup, emul);
  43. if (ret)
  44. return ret;
  45. ret = usb_emul_control(emul, udev, pipe, buffer, length, setup);
  46. if (ret < 0) {
  47. debug("ret=%d\n", ret);
  48. udev->status = ret;
  49. udev->act_len = 0;
  50. } else {
  51. udev->status = 0;
  52. udev->act_len = ret;
  53. }
  54. return ret;
  55. }
  56. static int sandbox_submit_bulk(struct udevice *bus, struct usb_device *udev,
  57. unsigned long pipe, void *buffer, int length)
  58. {
  59. struct udevice *emul;
  60. int ret;
  61. /* Just use child of dev as emulator? */
  62. debug("%s: bus=%s\n", __func__, bus->name);
  63. ret = usb_emul_find(bus, pipe, &emul);
  64. usbmon_trace(bus, pipe, NULL, emul);
  65. if (ret)
  66. return ret;
  67. ret = usb_emul_bulk(emul, udev, pipe, buffer, length);
  68. if (ret < 0) {
  69. debug("ret=%d\n", ret);
  70. udev->status = ret;
  71. udev->act_len = 0;
  72. } else {
  73. udev->status = 0;
  74. udev->act_len = ret;
  75. }
  76. return ret;
  77. }
  78. static int sandbox_submit_int(struct udevice *bus, struct usb_device *udev,
  79. unsigned long pipe, void *buffer, int length,
  80. int interval)
  81. {
  82. struct udevice *emul;
  83. int ret;
  84. /* Just use child of dev as emulator? */
  85. debug("%s: bus=%s\n", __func__, bus->name);
  86. ret = usb_emul_find(bus, pipe, &emul);
  87. usbmon_trace(bus, pipe, NULL, emul);
  88. if (ret)
  89. return ret;
  90. ret = usb_emul_int(emul, udev, pipe, buffer, length, interval);
  91. return ret;
  92. }
  93. static int sandbox_alloc_device(struct udevice *dev, struct usb_device *udev)
  94. {
  95. return 0;
  96. }
  97. static int sandbox_usb_probe(struct udevice *dev)
  98. {
  99. return 0;
  100. }
  101. static const struct dm_usb_ops sandbox_usb_ops = {
  102. .control = sandbox_submit_control,
  103. .bulk = sandbox_submit_bulk,
  104. .interrupt = sandbox_submit_int,
  105. .alloc_device = sandbox_alloc_device,
  106. };
  107. static const struct udevice_id sandbox_usb_ids[] = {
  108. { .compatible = "sandbox,usb" },
  109. { }
  110. };
  111. U_BOOT_DRIVER(usb_sandbox) = {
  112. .name = "usb_sandbox",
  113. .id = UCLASS_USB,
  114. .of_match = sandbox_usb_ids,
  115. .probe = sandbox_usb_probe,
  116. .ops = &sandbox_usb_ops,
  117. };