1 | /*
|
---|
2 | * Copyright (c) 2011 Jan Vesely
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libusbhost
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | *
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <usb/debug.h>
|
---|
37 | #include <usb/descriptor.h>
|
---|
38 | #include <usb/request.h>
|
---|
39 |
|
---|
40 | #include <assert.h>
|
---|
41 | #include <async.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <macros.h>
|
---|
44 | #include <usb_iface.h>
|
---|
45 | #include <str_error.h>
|
---|
46 |
|
---|
47 | #include "hcd.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | /** Initialize hcd_t structure.
|
---|
51 | * Initializes device and endpoint managers. Sets data and hook pointer to NULL.
|
---|
52 | *
|
---|
53 | * @param hcd hcd_t structure to initialize, non-null.
|
---|
54 | * @param max_speed Maximum supported USB speed (full, high).
|
---|
55 | * @param bandwidth Available bandwidth, passed to endpoint manager.
|
---|
56 | * @param bw_count Bandwidth compute function, passed to endpoint manager.
|
---|
57 | */
|
---|
58 | void hcd_init(hcd_t *hcd) {
|
---|
59 | assert(hcd);
|
---|
60 |
|
---|
61 | hcd_set_implementation(hcd, NULL, NULL, NULL);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** Get max packet size for the control endpoint 0.
|
---|
65 | *
|
---|
66 | * For LS, HS, and SS devices this value is fixed. For FS devices we must fetch
|
---|
67 | * the first 8B of the device descriptor to determine it.
|
---|
68 | *
|
---|
69 | * @return Max packet size for EP 0
|
---|
70 | */
|
---|
71 | int hcd_get_ep0_max_packet_size(uint16_t *mps, hcd_t *hcd, device_t *dev)
|
---|
72 | {
|
---|
73 | assert(mps);
|
---|
74 |
|
---|
75 | static const uint16_t mps_fixed [] = {
|
---|
76 | [USB_SPEED_LOW] = 8,
|
---|
77 | [USB_SPEED_HIGH] = 64,
|
---|
78 | [USB_SPEED_SUPER] = 512,
|
---|
79 | };
|
---|
80 |
|
---|
81 | if (dev->speed < ARRAY_SIZE(mps_fixed) && mps_fixed[dev->speed] != 0) {
|
---|
82 | *mps = mps_fixed[dev->speed];
|
---|
83 | return EOK;
|
---|
84 | }
|
---|
85 |
|
---|
86 | const usb_target_t control_ep = {{
|
---|
87 | .address = dev->address,
|
---|
88 | .endpoint = 0,
|
---|
89 | }};
|
---|
90 |
|
---|
91 | usb_standard_device_descriptor_t desc = { 0 };
|
---|
92 | const usb_device_request_setup_packet_t get_device_desc_8 =
|
---|
93 | GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
|
---|
94 |
|
---|
95 | usb_log_debug("Requesting first 8B of device descriptor to determine MPS.");
|
---|
96 | ssize_t got = hcd_send_batch_sync(hcd, dev, control_ep, USB_DIRECTION_IN,
|
---|
97 | (char *) &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
|
---|
98 | "read first 8 bytes of dev descriptor");
|
---|
99 |
|
---|
100 | if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
|
---|
101 | const int err = got < 0 ? got : EOVERFLOW;
|
---|
102 | usb_log_error("Failed to get 8B of dev descr: %s.", str_error(err));
|
---|
103 | return err;
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (desc.descriptor_type != USB_DESCTYPE_DEVICE) {
|
---|
107 | usb_log_error("The device responded with wrong device descriptor.");
|
---|
108 | return EIO;
|
---|
109 | }
|
---|
110 |
|
---|
111 | uint16_t version = uint16_usb2host(desc.usb_spec_version);
|
---|
112 | if (version < 0x0300) {
|
---|
113 | /* USB 2 and below have MPS raw in the field */
|
---|
114 | *mps = desc.max_packet_size;
|
---|
115 | } else {
|
---|
116 | /* USB 3 have MPS as an 2-based exponent */
|
---|
117 | *mps = (1 << desc.max_packet_size);
|
---|
118 | }
|
---|
119 | return EOK;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Setup devices Transaction Translation.
|
---|
124 | *
|
---|
125 | * This applies for Low/Full speed devices under High speed hub only. Other
|
---|
126 | * devices just inherit TT from the hub.
|
---|
127 | *
|
---|
128 | * Roothub must be handled specially.
|
---|
129 | */
|
---|
130 | void hcd_setup_device_tt(device_t *dev)
|
---|
131 | {
|
---|
132 | if (!dev->hub)
|
---|
133 | return;
|
---|
134 |
|
---|
135 | if (dev->hub->speed == USB_SPEED_HIGH && usb_speed_is_11(dev->speed)) {
|
---|
136 | /* For LS devices under HS hub */
|
---|
137 | dev->tt.address = dev->hub->address;
|
---|
138 | dev->tt.port = dev->port;
|
---|
139 | }
|
---|
140 | else {
|
---|
141 | /* Inherit hub's TT */
|
---|
142 | dev->tt = dev->hub->tt;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | /** Prepare generic usb_transfer_batch and schedule it.
|
---|
147 | * @param hcd Host controller driver.
|
---|
148 | * @param target address and endpoint number.
|
---|
149 | * @param setup_data Data to use in setup stage (Control communication type)
|
---|
150 | * @param in Callback for device to host communication.
|
---|
151 | * @param out Callback for host to device communication.
|
---|
152 | * @param arg Callback parameter.
|
---|
153 | * @param name Communication identifier (for nicer output).
|
---|
154 | * @return Error code.
|
---|
155 | */
|
---|
156 | int hcd_send_batch(hcd_t *hcd, device_t *device, usb_target_t target,
|
---|
157 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
---|
158 | usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
|
---|
159 | {
|
---|
160 | assert(hcd);
|
---|
161 | assert(device->address == target.address);
|
---|
162 |
|
---|
163 | if (!hcd->ops.schedule) {
|
---|
164 | usb_log_error("HCD does not implement scheduler.\n");
|
---|
165 | return ENOTSUP;
|
---|
166 | }
|
---|
167 |
|
---|
168 | endpoint_t *ep = bus_find_endpoint(hcd->bus, device, target, direction);
|
---|
169 | if (ep == NULL) {
|
---|
170 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
171 | device->address, target.endpoint, name);
|
---|
172 | return ENOENT;
|
---|
173 | }
|
---|
174 |
|
---|
175 | // TODO cut here aka provide helper to call with instance of endpoint_t in hand
|
---|
176 |
|
---|
177 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
---|
178 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
179 |
|
---|
180 | const size_t bw = bus_count_bw(ep, size);
|
---|
181 | /* Check if we have enough bandwidth reserved */
|
---|
182 | if (ep->bandwidth < bw) {
|
---|
183 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
184 | "but only %zu is reserved.\n",
|
---|
185 | device->address, ep->endpoint, name, bw, ep->bandwidth);
|
---|
186 | return ENOSPC;
|
---|
187 | }
|
---|
188 |
|
---|
189 | usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
|
---|
190 | if (!batch) {
|
---|
191 | usb_log_error("Failed to create transfer batch.\n");
|
---|
192 | return ENOMEM;
|
---|
193 | }
|
---|
194 |
|
---|
195 | batch->target = target;
|
---|
196 | batch->buffer = data;
|
---|
197 | batch->buffer_size = size;
|
---|
198 | batch->setup.packed = setup_data;
|
---|
199 | batch->dir = direction;
|
---|
200 | batch->on_complete = on_complete;
|
---|
201 | batch->on_complete_data = arg;
|
---|
202 |
|
---|
203 | /* Check for commands that reset toggle bit */
|
---|
204 | if (ep->transfer_type == USB_TRANSFER_CONTROL)
|
---|
205 | batch->toggle_reset_mode
|
---|
206 | = usb_request_get_toggle_reset_mode(&batch->setup.packet);
|
---|
207 |
|
---|
208 | const int ret = hcd->ops.schedule(hcd, batch);
|
---|
209 | if (ret != EOK) {
|
---|
210 | usb_log_warning("Batch %p failed to schedule: %s", batch, str_error(ret));
|
---|
211 | usb_transfer_batch_destroy(batch);
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* Drop our own reference to ep. */
|
---|
215 | endpoint_del_ref(ep);
|
---|
216 |
|
---|
217 | return ret;
|
---|
218 | }
|
---|
219 |
|
---|
220 | typedef struct {
|
---|
221 | fibril_mutex_t done_mtx;
|
---|
222 | fibril_condvar_t done_cv;
|
---|
223 | unsigned done;
|
---|
224 |
|
---|
225 | size_t transfered_size;
|
---|
226 | int error;
|
---|
227 | } sync_data_t;
|
---|
228 |
|
---|
229 | static int sync_transfer_complete(void *arg, int error, size_t transfered_size)
|
---|
230 | {
|
---|
231 | sync_data_t *d = arg;
|
---|
232 | assert(d);
|
---|
233 | d->transfered_size = transfered_size;
|
---|
234 | d->error = error;
|
---|
235 | fibril_mutex_lock(&d->done_mtx);
|
---|
236 | d->done = 1;
|
---|
237 | fibril_condvar_broadcast(&d->done_cv);
|
---|
238 | fibril_mutex_unlock(&d->done_mtx);
|
---|
239 | return EOK;
|
---|
240 | }
|
---|
241 |
|
---|
242 | ssize_t hcd_send_batch_sync(hcd_t *hcd, device_t *device, usb_target_t target,
|
---|
243 | usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
|
---|
244 | const char *name)
|
---|
245 | {
|
---|
246 | assert(hcd);
|
---|
247 | sync_data_t sd = { .done = 0 };
|
---|
248 | fibril_mutex_initialize(&sd.done_mtx);
|
---|
249 | fibril_condvar_initialize(&sd.done_cv);
|
---|
250 |
|
---|
251 | const int ret = hcd_send_batch(hcd, device, target, direction,
|
---|
252 | data, size, setup_data,
|
---|
253 | sync_transfer_complete, &sd, name);
|
---|
254 | if (ret != EOK)
|
---|
255 | return ret;
|
---|
256 |
|
---|
257 | fibril_mutex_lock(&sd.done_mtx);
|
---|
258 | while (!sd.done)
|
---|
259 | fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
|
---|
260 | fibril_mutex_unlock(&sd.done_mtx);
|
---|
261 |
|
---|
262 | return (sd.error == EOK)
|
---|
263 | ? (ssize_t) sd.transfered_size
|
---|
264 | : (ssize_t) sd.error;
|
---|
265 | }
|
---|
266 |
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * @}
|
---|
270 | */
|
---|