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

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

libusbhost,ehci,ohci,uhci,vhc: Pass ops structure instead of function pointers to intialization

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