xref: /openbmc/u-boot/drivers/usb/host/usb-sandbox.c (revision 29b103c7)
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 
8 #include <common.h>
9 #include <dm.h>
10 #include <usb.h>
11 #include <dm/root.h>
12 
13 DECLARE_GLOBAL_DATA_PTR;
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 
21 	type = (pipe & USB_PIPE_TYPE_MASK) >> USB_PIPE_TYPE_SHIFT;
22 	debug("0 0 S %c%c:%d:%03ld:%ld", types[type],
23 	      pipe & USB_DIR_IN ? 'i' : 'o',
24 	      bus->seq,
25 	      (pipe & USB_PIPE_DEV_MASK) >> USB_PIPE_DEV_SHIFT,
26 	      (pipe & USB_PIPE_EP_MASK) >> USB_PIPE_EP_SHIFT);
27 	if (setup) {
28 		debug(" s %02x %02x %04x %04x %04x", setup->requesttype,
29 		      setup->request, setup->value, setup->index,
30 		      setup->length);
31 	}
32 	debug(" %s", emul ? emul->name : "(no emul found)");
33 
34 	debug("\n");
35 }
36 
37 static int sandbox_submit_control(struct udevice *bus,
38 				      struct usb_device *udev,
39 				      unsigned long pipe,
40 				      void *buffer, int length,
41 				      struct devrequest *setup)
42 {
43 	struct udevice *emul;
44 	int ret;
45 
46 	/* Just use child of dev as emulator? */
47 	debug("%s: bus=%s\n", __func__, bus->name);
48 	ret = usb_emul_find(bus, pipe, &emul);
49 	usbmon_trace(bus, pipe, setup, emul);
50 	if (ret)
51 		return ret;
52 	ret = usb_emul_control(emul, udev, pipe, buffer, length, setup);
53 	if (ret < 0) {
54 		debug("ret=%d\n", ret);
55 		udev->status = ret;
56 		udev->act_len = 0;
57 	} else {
58 		udev->status = 0;
59 		udev->act_len = ret;
60 	}
61 
62 	return ret;
63 }
64 
65 static int sandbox_submit_bulk(struct udevice *bus, struct usb_device *udev,
66 			       unsigned long pipe, void *buffer, int length)
67 {
68 	struct udevice *emul;
69 	int ret;
70 
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 
87 	return ret;
88 }
89 
90 static int sandbox_alloc_device(struct udevice *dev, struct usb_device *udev)
91 {
92 	return 0;
93 }
94 
95 static int sandbox_usb_probe(struct udevice *dev)
96 {
97 	return 0;
98 }
99 
100 static const struct dm_usb_ops sandbox_usb_ops = {
101 	.control	= sandbox_submit_control,
102 	.bulk		= sandbox_submit_bulk,
103 	.alloc_device	= sandbox_alloc_device,
104 };
105 
106 static const struct udevice_id sandbox_usb_ids[] = {
107 	{ .compatible = "sandbox,usb" },
108 	{ }
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 };
118