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

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

libusbhost: Remove speed argument from add_ep.

It can be queried internally.

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