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