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

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

libusbhost: Merge _remove_address and _release_address.

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