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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a157846 was a157846, checked in by Jan Vesely <jano.vesely@…>, 10 years ago

usb: Fix EINPROGRESS compile failures

why the removal of EINPROGRESS was hidden in networking changes is beyond me

  • Property mode set to 100644
File size: 8.4 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->driver.ep_add_hook)
57 return hcd->driver.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->driver.ep_remove_hook)
71 hcd->driver.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->driver.data = NULL;
102 hcd->driver.schedule = NULL;
103 hcd->driver.ep_add_hook = NULL;
104 hcd->driver.ep_remove_hook = NULL;
105}
106
107usb_address_t hcd_request_address(hcd_t *hcd, usb_speed_t speed)
108{
109 assert(hcd);
110 usb_address_t address = 0;
111 const int ret = usb_bus_request_address(
112 &hcd->bus, &address, false, speed);
113 if (ret != EOK)
114 return ret;
115 return address;
116}
117
118int hcd_release_address(hcd_t *hcd, usb_address_t address)
119{
120 assert(hcd);
121 return usb_bus_remove_address(&hcd->bus, address,
122 unregister_helper_warn, hcd);
123}
124
125int hcd_reserve_default_address(hcd_t *hcd, usb_speed_t speed)
126{
127 assert(hcd);
128 usb_address_t address = 0;
129 return usb_bus_request_address(
130 &hcd->bus, &address, true, speed);
131}
132
133int hcd_add_ep(hcd_t *hcd, usb_target_t target, usb_direction_t dir,
134 usb_transfer_type_t type, size_t max_packet_size, unsigned packets,
135 size_t size, usb_address_t tt_address, unsigned tt_port)
136{
137 assert(hcd);
138 return usb_bus_add_ep(&hcd->bus, target.address,
139 target.endpoint, dir, type, max_packet_size, packets, size,
140 register_helper, hcd, tt_address, tt_port);
141}
142
143int hcd_remove_ep(hcd_t *hcd, usb_target_t target, usb_direction_t dir)
144{
145 assert(hcd);
146 return usb_bus_remove_ep(&hcd->bus, target.address,
147 target.endpoint, dir, unregister_helper, hcd);
148}
149
150
151typedef struct {
152 void *original_data;
153 usbhc_iface_transfer_out_callback_t original_callback;
154 usb_target_t target;
155 hcd_t *hcd;
156} toggle_t;
157
158static void toggle_reset_callback(int retval, void *arg)
159{
160 assert(arg);
161 toggle_t *toggle = arg;
162 if (retval == EOK) {
163 usb_log_debug2("Reseting toggle on %d:%d.\n",
164 toggle->target.address, toggle->target.endpoint);
165 usb_bus_reset_toggle(&toggle->hcd->bus,
166 toggle->target, toggle->target.endpoint == 0);
167 }
168
169 toggle->original_callback(retval, toggle->original_data);
170}
171
172/** Prepare generic usb_transfer_batch and schedule it.
173 * @param hcd Host controller driver.
174 * @param fun DDF fun
175 * @param target address and endpoint number.
176 * @param setup_data Data to use in setup stage (Control communication type)
177 * @param in Callback for device to host communication.
178 * @param out Callback for host to device communication.
179 * @param arg Callback parameter.
180 * @param name Communication identifier (for nicer output).
181 * @return Error code.
182 */
183int hcd_send_batch(
184 hcd_t *hcd, usb_target_t target, usb_direction_t direction,
185 void *data, size_t size, uint64_t setup_data,
186 usbhc_iface_transfer_in_callback_t in,
187 usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
188{
189 assert(hcd);
190
191 endpoint_t *ep = usb_bus_find_ep(&hcd->bus,
192 target.address, target.endpoint, direction);
193 if (ep == NULL) {
194 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
195 target.address, target.endpoint, name);
196 return ENOENT;
197 }
198
199 usb_log_debug2("%s %d:%d %zu(%zu).\n",
200 name, target.address, target.endpoint, size, ep->max_packet_size);
201
202 const size_t bw = bandwidth_count_usb11(
203 ep->speed, ep->transfer_type, size, ep->max_packet_size);
204 /* Check if we have enough bandwidth reserved */
205 if (ep->bandwidth < bw) {
206 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
207 "but only %zu is reserved.\n",
208 ep->address, ep->endpoint, name, bw, ep->bandwidth);
209 return ENOSPC;
210 }
211 if (!hcd->driver.schedule) {
212 usb_log_error("HCD does not implement scheduler.\n");
213 return ENOTSUP;
214 }
215
216 /* Check for commands that reset toggle bit */
217 if (ep->transfer_type == USB_TRANSFER_CONTROL) {
218 const int reset_toggle = usb_request_needs_toggle_reset(
219 (usb_device_request_setup_packet_t *) &setup_data);
220 if (reset_toggle >= 0) {
221 assert(out);
222 toggle_t *toggle = malloc(sizeof(toggle_t));
223 if (!toggle)
224 return ENOMEM;
225 toggle->target.address = target.address;
226 toggle->target.endpoint = reset_toggle;
227 toggle->original_callback = out;
228 toggle->original_data = arg;
229 toggle->hcd = hcd;
230
231 arg = toggle;
232 out = toggle_reset_callback;
233 }
234 }
235
236 usb_transfer_batch_t *batch = usb_transfer_batch_create(
237 ep, data, size, setup_data, in, out, arg);
238 if (!batch) {
239 usb_log_error("Failed to create transfer batch.\n");
240 return ENOMEM;
241 }
242
243 const int ret = hcd->driver.schedule(hcd, batch);
244 if (ret != EOK)
245 usb_transfer_batch_destroy(batch);
246
247 return ret;
248}
249
250typedef struct {
251 volatile unsigned done;
252 int ret;
253 size_t size;
254} sync_data_t;
255
256static void transfer_in_cb(int ret, size_t size, void* data)
257{
258 sync_data_t *d = data;
259 assert(d);
260 d->ret = ret;
261 d->done = 1;
262 d->size = size;
263}
264
265static void transfer_out_cb(int ret, void* data)
266{
267 sync_data_t *d = data;
268 assert(data);
269 d->ret = ret;
270 d->done = 1;
271}
272
273/** this is really ugly version of sync usb communication */
274ssize_t hcd_send_batch_sync(
275 hcd_t *hcd, usb_target_t target, usb_direction_t dir,
276 void *data, size_t size, uint64_t setup_data, const char* name)
277{
278 assert(hcd);
279 sync_data_t sd = { .done = 0, .ret = EBUSY, .size = size };
280
281 const int ret = hcd_send_batch(hcd, target, dir, data, size, setup_data,
282 dir == USB_DIRECTION_IN ? transfer_in_cb : NULL,
283 dir == USB_DIRECTION_OUT ? transfer_out_cb : NULL, &sd, name);
284 if (ret != EOK)
285 return ret;
286
287 while (!sd.done) {
288 async_usleep(1000);
289 }
290
291 if (sd.ret == EOK)
292 return sd.size;
293 return sd.ret;
294}
295
296
297/**
298 * @}
299 */
Note: See TracBrowser for help on using the repository browser.