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

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

libusbhost: Rename usb_endpoint_manager → usb_bus

It's shorter and more accurate.

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