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

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

libusbhost: Remove usb_device_manager.

Functions merged to usb_endpoint_manager.

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