source: mainline/uspace/lib/usbhost/src/hcd.c@ 4547f11

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4547f11 was 68e5406, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Separate return value from error code in gen_irq_code*() and hcd_send_batch_sync().

  • Property mode set to 100644
File size: 8.3 KB
Line 
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/request.h>
38
39#include <assert.h>
40#include <async.h>
41#include <errno.h>
42#include <usb_iface.h>
43
44#include "hcd.h"
45
46/** Calls ep_add_hook upon endpoint registration.
47 * @param ep Endpoint to be registered.
48 * @param arg hcd_t in disguise.
49 * @return Error code.
50 */
51static int register_helper(endpoint_t *ep, void *arg)
52{
53 hcd_t *hcd = arg;
54 assert(ep);
55 assert(hcd);
56 if (hcd->ops.ep_add_hook)
57 return hcd->ops.ep_add_hook(hcd, ep);
58 return EOK;
59}
60
61/** Calls ep_remove_hook upon endpoint removal.
62 * @param ep Endpoint to be unregistered.
63 * @param arg hcd_t in disguise.
64 */
65static void unregister_helper(endpoint_t *ep, void *arg)
66{
67 hcd_t *hcd = arg;
68 assert(ep);
69 assert(hcd);
70 if (hcd->ops.ep_remove_hook)
71 hcd->ops.ep_remove_hook(hcd, ep);
72}
73
74/** Calls ep_remove_hook upon endpoint removal. Prints warning.
75 * * @param ep Endpoint to be unregistered.
76 * * @param arg hcd_t in disguise.
77 * */
78static void unregister_helper_warn(endpoint_t *ep, void *arg)
79{
80 assert(ep);
81 usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
82 ep->address, ep->endpoint, usb_str_direction(ep->direction));
83 unregister_helper(ep, arg);
84}
85
86
87/** Initialize hcd_t structure.
88 * Initializes device and endpoint managers. Sets data and hook pointer to NULL.
89 *
90 * @param hcd hcd_t structure to initialize, non-null.
91 * @param max_speed Maximum supported USB speed (full, high).
92 * @param bandwidth Available bandwidth, passed to endpoint manager.
93 * @param bw_count Bandwidth compute function, passed to endpoint manager.
94 */
95void hcd_init(hcd_t *hcd, usb_speed_t max_speed, size_t bandwidth,
96 bw_count_func_t bw_count)
97{
98 assert(hcd);
99 usb_bus_init(&hcd->bus, bandwidth, bw_count, max_speed);
100
101 hcd_set_implementation(hcd, NULL, NULL);
102}
103
104int hcd_request_address(hcd_t *hcd, usb_speed_t speed, usb_address_t *address)
105{
106 assert(hcd);
107 return usb_bus_request_address(&hcd->bus, address, false, speed);
108}
109
110int hcd_release_address(hcd_t *hcd, usb_address_t address)
111{
112 assert(hcd);
113 return usb_bus_remove_address(&hcd->bus, address,
114 unregister_helper_warn, hcd);
115}
116
117int hcd_reserve_default_address(hcd_t *hcd, usb_speed_t speed)
118{
119 assert(hcd);
120 usb_address_t address = 0;
121 return usb_bus_request_address(&hcd->bus, &address, true, speed);
122}
123
124int hcd_add_ep(hcd_t *hcd, usb_target_t target, usb_direction_t dir,
125 usb_transfer_type_t type, size_t max_packet_size, unsigned packets,
126 size_t size, usb_address_t tt_address, unsigned tt_port)
127{
128 assert(hcd);
129 return usb_bus_add_ep(&hcd->bus, target.address,
130 target.endpoint, dir, type, max_packet_size, packets, size,
131 register_helper, hcd, tt_address, tt_port);
132}
133
134int hcd_remove_ep(hcd_t *hcd, usb_target_t target, usb_direction_t dir)
135{
136 assert(hcd);
137 return usb_bus_remove_ep(&hcd->bus, target.address,
138 target.endpoint, dir, unregister_helper, hcd);
139}
140
141
142typedef struct {
143 void *original_data;
144 usbhc_iface_transfer_out_callback_t original_callback;
145 usb_target_t target;
146 hcd_t *hcd;
147} toggle_t;
148
149static void toggle_reset_callback(int retval, void *arg)
150{
151 assert(arg);
152 toggle_t *toggle = arg;
153 if (retval == EOK) {
154 usb_log_debug2("Reseting toggle on %d:%d.\n",
155 toggle->target.address, toggle->target.endpoint);
156 usb_bus_reset_toggle(&toggle->hcd->bus,
157 toggle->target, toggle->target.endpoint == 0);
158 }
159
160 toggle->original_callback(retval, toggle->original_data);
161}
162
163/** Prepare generic usb_transfer_batch and schedule it.
164 * @param hcd Host controller driver.
165 * @param fun DDF fun
166 * @param target address and endpoint number.
167 * @param setup_data Data to use in setup stage (Control communication type)
168 * @param in Callback for device to host communication.
169 * @param out Callback for host to device communication.
170 * @param arg Callback parameter.
171 * @param name Communication identifier (for nicer output).
172 * @return Error code.
173 */
174int hcd_send_batch(
175 hcd_t *hcd, usb_target_t target, usb_direction_t direction,
176 void *data, size_t size, uint64_t setup_data,
177 usbhc_iface_transfer_in_callback_t in,
178 usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
179{
180 assert(hcd);
181
182 endpoint_t *ep = usb_bus_find_ep(&hcd->bus,
183 target.address, target.endpoint, direction);
184 if (ep == NULL) {
185 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
186 target.address, target.endpoint, name);
187 return ENOENT;
188 }
189
190 usb_log_debug2("%s %d:%d %zu(%zu).\n",
191 name, target.address, target.endpoint, size, ep->max_packet_size);
192
193 const size_t bw = bandwidth_count_usb11(
194 ep->speed, ep->transfer_type, size, ep->max_packet_size);
195 /* Check if we have enough bandwidth reserved */
196 if (ep->bandwidth < bw) {
197 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
198 "but only %zu is reserved.\n",
199 ep->address, ep->endpoint, name, bw, ep->bandwidth);
200 return ENOSPC;
201 }
202 if (!hcd->ops.schedule) {
203 usb_log_error("HCD does not implement scheduler.\n");
204 return ENOTSUP;
205 }
206
207 /* Check for commands that reset toggle bit */
208 if (ep->transfer_type == USB_TRANSFER_CONTROL) {
209 const int reset_toggle = usb_request_needs_toggle_reset(
210 (usb_device_request_setup_packet_t *) &setup_data);
211 if (reset_toggle >= 0) {
212 assert(out);
213 toggle_t *toggle = malloc(sizeof(toggle_t));
214 if (!toggle)
215 return ENOMEM;
216 toggle->target.address = target.address;
217 toggle->target.endpoint = reset_toggle;
218 toggle->original_callback = out;
219 toggle->original_data = arg;
220 toggle->hcd = hcd;
221
222 arg = toggle;
223 out = toggle_reset_callback;
224 }
225 }
226
227 usb_transfer_batch_t *batch = usb_transfer_batch_create(
228 ep, data, size, setup_data, in, out, arg);
229 if (!batch) {
230 usb_log_error("Failed to create transfer batch.\n");
231 return ENOMEM;
232 }
233
234 const int ret = hcd->ops.schedule(hcd, batch);
235 if (ret != EOK)
236 usb_transfer_batch_destroy(batch);
237
238 /* Drop our own reference to ep. */
239 endpoint_del_ref(ep);
240
241 return ret;
242}
243
244typedef struct {
245 volatile unsigned done;
246 int ret;
247 size_t size;
248} sync_data_t;
249
250static void transfer_in_cb(int ret, size_t size, void* data)
251{
252 sync_data_t *d = data;
253 assert(d);
254 d->ret = ret;
255 d->done = 1;
256 d->size = size;
257}
258
259static void transfer_out_cb(int ret, void* data)
260{
261 sync_data_t *d = data;
262 assert(data);
263 d->ret = ret;
264 d->done = 1;
265}
266
267/** this is really ugly version of sync usb communication */
268int hcd_send_batch_sync(
269 hcd_t *hcd, usb_target_t target, usb_direction_t dir,
270 void *data, size_t size, uint64_t setup_data, const char* name, size_t *out_size)
271{
272 assert(hcd);
273 sync_data_t sd = { .done = 0, .ret = EBUSY, .size = size };
274
275 const int ret = hcd_send_batch(hcd, target, dir, data, size, setup_data,
276 dir == USB_DIRECTION_IN ? transfer_in_cb : NULL,
277 dir == USB_DIRECTION_OUT ? transfer_out_cb : NULL, &sd, name);
278 if (ret != EOK)
279 return ret;
280
281 while (!sd.done) {
282 async_usleep(1000);
283 }
284
285 if (sd.ret == EOK)
286 *out_size = sd.size;
287 return sd.ret;
288}
289
290
291/**
292 * @}
293 */
Note: See TracBrowser for help on using the repository browser.