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

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

libisbhost: Add message to error path.

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