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 |
|
---|
41 | #include <usb/host/hcd.h>
|
---|
42 |
|
---|
43 | typedef struct hc_dev {
|
---|
44 | ddf_fun_t *hc_fun;
|
---|
45 | } hc_dev_t;
|
---|
46 |
|
---|
47 | static hc_dev_t *dev_to_hc_dev(ddf_dev_t *dev)
|
---|
48 | {
|
---|
49 | return ddf_dev_data_get(dev);
|
---|
50 | }
|
---|
51 |
|
---|
52 | hcd_t *dev_to_hcd(ddf_dev_t *dev)
|
---|
53 | {
|
---|
54 | hc_dev_t *hc_dev = dev_to_hc_dev(dev);
|
---|
55 | if (!hc_dev || !hc_dev->hc_fun) {
|
---|
56 | usb_log_error("Invalid OHCI device.\n");
|
---|
57 | return NULL;
|
---|
58 | }
|
---|
59 | return ddf_fun_data_get(hc_dev->hc_fun);
|
---|
60 | }
|
---|
61 |
|
---|
62 | typedef struct usb_dev {
|
---|
63 | link_t link;
|
---|
64 | ddf_fun_t *fun;
|
---|
65 | usb_address_t address;
|
---|
66 | usb_speed_t speed;
|
---|
67 | devman_handle_t handle;
|
---|
68 | } usb_dev_t;
|
---|
69 |
|
---|
70 | /** Get USB address assigned to root hub.
|
---|
71 | *
|
---|
72 | * @param[in] fun Root hub function.
|
---|
73 | * @param[out] address Store the address here.
|
---|
74 | * @return Error code.
|
---|
75 | */
|
---|
76 | static int rh_get_my_address(ddf_fun_t *fun, usb_address_t *address)
|
---|
77 | {
|
---|
78 | assert(fun);
|
---|
79 | if (address != NULL) {
|
---|
80 | usb_dev_t *usb_dev = ddf_fun_data_get(fun);
|
---|
81 | *address = usb_dev->address;
|
---|
82 | }
|
---|
83 | return EOK;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** Gets handle of the respective hc (this device, hc function).
|
---|
87 | *
|
---|
88 | * @param[in] root_hub_fun Root hub function seeking hc handle.
|
---|
89 | * @param[out] handle Place to write the handle.
|
---|
90 | * @return Error code.
|
---|
91 | */
|
---|
92 | static int rh_get_hc_handle(ddf_fun_t *fun, devman_handle_t *handle)
|
---|
93 | {
|
---|
94 | assert(fun);
|
---|
95 |
|
---|
96 | if (handle != NULL) {
|
---|
97 | usb_dev_t *usb_dev = ddf_fun_data_get(fun);
|
---|
98 | *handle = usb_dev->handle;
|
---|
99 | }
|
---|
100 | return EOK;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Root hub USB interface */
|
---|
104 | static usb_iface_t usb_iface = {
|
---|
105 | .get_hc_handle = rh_get_hc_handle,
|
---|
106 | .get_my_address = rh_get_my_address,
|
---|
107 | };
|
---|
108 | /** Standard USB RH options (RH interface) */
|
---|
109 | static ddf_dev_ops_t usb_ops = {
|
---|
110 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
111 | };
|
---|
112 |
|
---|
113 | /** Standard USB HC options (HC interface) */
|
---|
114 | static ddf_dev_ops_t hc_ops = {
|
---|
115 | .interfaces[USBHC_DEV_IFACE] = &hcd_iface,
|
---|
116 | };
|
---|
117 |
|
---|
118 | /** Initialize hcd_t structure.
|
---|
119 | * Initializes device and endpoint managers. Sets data and hook pointer to NULL.
|
---|
120 | *
|
---|
121 | * @param hcd hcd_t structure to initialize, non-null.
|
---|
122 | * @param max_speed Maximum supported USB speed (full, high).
|
---|
123 | * @param bandwidth Available bandwidth, passed to endpoint manager.
|
---|
124 | * @param bw_count Bandwidth compute function, passed to endpoint manager.
|
---|
125 | */
|
---|
126 | void hcd_init(hcd_t *hcd, usb_speed_t max_speed, size_t bandwidth,
|
---|
127 | bw_count_func_t bw_count)
|
---|
128 | {
|
---|
129 | assert(hcd);
|
---|
130 | usb_device_manager_init(&hcd->dev_manager, max_speed);
|
---|
131 | usb_endpoint_manager_init(&hcd->ep_manager, bandwidth, bw_count);
|
---|
132 | list_initialize(&hcd->devices);
|
---|
133 |
|
---|
134 | hcd->private_data = NULL;
|
---|
135 | hcd->schedule = NULL;
|
---|
136 | hcd->ep_add_hook = NULL;
|
---|
137 | hcd->ep_remove_hook = NULL;
|
---|
138 | }
|
---|
139 |
|
---|
140 | int hcd_add_device(hcd_t *instance, ddf_dev_t *parent,
|
---|
141 | usb_address_t address, usb_speed_t speed, const char *name,
|
---|
142 | const match_id_list_t *mids)
|
---|
143 | {
|
---|
144 | assert(instance);
|
---|
145 | assert(parent);
|
---|
146 | hc_dev_t *hc_dev = ddf_dev_data_get(parent);
|
---|
147 | devman_handle_t hc_handle = ddf_fun_get_handle(hc_dev->hc_fun);
|
---|
148 |
|
---|
149 | //TODO more checks
|
---|
150 | ddf_fun_t *fun = ddf_fun_create(parent, fun_inner, name);
|
---|
151 | if (!fun)
|
---|
152 | return ENOMEM;
|
---|
153 | usb_dev_t *info = ddf_fun_data_alloc(fun, sizeof(usb_dev_t));
|
---|
154 | if (!info) {
|
---|
155 | ddf_fun_destroy(fun);
|
---|
156 | return ENOMEM;
|
---|
157 | }
|
---|
158 | info->address = address;
|
---|
159 | info->speed = speed;
|
---|
160 | info->handle = hc_handle;
|
---|
161 | info->fun = fun;
|
---|
162 | link_initialize(&info->link);
|
---|
163 |
|
---|
164 | ddf_fun_set_ops(fun, &usb_ops);
|
---|
165 | list_foreach(mids->ids, iter) {
|
---|
166 | match_id_t *mid = list_get_instance(iter, match_id_t, link);
|
---|
167 | ddf_fun_add_match_id(fun, mid->id, mid->score);
|
---|
168 | }
|
---|
169 |
|
---|
170 | int ret = ddf_fun_bind(fun);
|
---|
171 | if (ret != EOK) {
|
---|
172 | ddf_fun_destroy(fun);
|
---|
173 | return ret;
|
---|
174 | }
|
---|
175 |
|
---|
176 | ret = usb_device_manager_bind_address(&instance->dev_manager,
|
---|
177 | address, ddf_fun_get_handle(fun));
|
---|
178 | if (ret != EOK)
|
---|
179 | usb_log_warning("Failed to bind root hub address: %s.\n",
|
---|
180 | str_error(ret));
|
---|
181 |
|
---|
182 | list_append(&info->link, &instance->devices);
|
---|
183 | return EOK;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /** Prepare generic usb_transfer_batch and schedule it.
|
---|
187 | * @param hcd Host controller driver.
|
---|
188 | * @param fun DDF fun
|
---|
189 | * @param target address and endpoint number.
|
---|
190 | * @param setup_data Data to use in setup stage (Control communication type)
|
---|
191 | * @param in Callback for device to host communication.
|
---|
192 | * @param out Callback for host to device communication.
|
---|
193 | * @param arg Callback parameter.
|
---|
194 | * @param name Communication identifier (for nicer output).
|
---|
195 | * @return Error code.
|
---|
196 | */
|
---|
197 | int hcd_send_batch(
|
---|
198 | hcd_t *hcd, ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
|
---|
199 | void *data, size_t size, uint64_t setup_data,
|
---|
200 | usbhc_iface_transfer_in_callback_t in,
|
---|
201 | usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
|
---|
202 | {
|
---|
203 | assert(hcd);
|
---|
204 |
|
---|
205 | endpoint_t *ep = usb_endpoint_manager_find_ep(&hcd->ep_manager,
|
---|
206 | target.address, target.endpoint, direction);
|
---|
207 | if (ep == NULL) {
|
---|
208 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
209 | target.address, target.endpoint, name);
|
---|
210 | return ENOENT;
|
---|
211 | }
|
---|
212 |
|
---|
213 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
---|
214 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
215 |
|
---|
216 | const size_t bw = bandwidth_count_usb11(
|
---|
217 | ep->speed, ep->transfer_type, size, ep->max_packet_size);
|
---|
218 | /* Check if we have enough bandwidth reserved */
|
---|
219 | if (ep->bandwidth < bw) {
|
---|
220 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
221 | "but only %zu is reserved.\n",
|
---|
222 | ep->address, ep->endpoint, name, bw, ep->bandwidth);
|
---|
223 | return ENOSPC;
|
---|
224 | }
|
---|
225 | if (!hcd->schedule) {
|
---|
226 | usb_log_error("HCD does not implement scheduler.\n");
|
---|
227 | return ENOTSUP;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /* No private data and no private data dtor */
|
---|
231 | usb_transfer_batch_t *batch =
|
---|
232 | usb_transfer_batch_create(ep, data, size, setup_data,
|
---|
233 | in, out, arg, fun, NULL, NULL);
|
---|
234 | if (!batch) {
|
---|
235 | return ENOMEM;
|
---|
236 | }
|
---|
237 |
|
---|
238 | const int ret = hcd->schedule(hcd, batch);
|
---|
239 | if (ret != EOK)
|
---|
240 | usb_transfer_batch_destroy(batch);
|
---|
241 |
|
---|
242 | return ret;
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | /** Announce root hub to the DDF
|
---|
247 | *
|
---|
248 | * @param[in] instance OHCI driver instance
|
---|
249 | * @param[in] hub_fun DDF function representing OHCI root hub
|
---|
250 | * @return Error code
|
---|
251 | */
|
---|
252 | int hcd_setup_hub(hcd_t *instance, usb_address_t *address, ddf_dev_t *device)
|
---|
253 | {
|
---|
254 | assert(instance);
|
---|
255 | assert(address);
|
---|
256 | assert(device);
|
---|
257 |
|
---|
258 | int ret = usb_device_manager_request_address(&instance->dev_manager,
|
---|
259 | address, false, USB_SPEED_FULL);
|
---|
260 | if (ret != EOK) {
|
---|
261 | usb_log_error("Failed to get root hub address: %s\n",
|
---|
262 | str_error(ret));
|
---|
263 | return ret;
|
---|
264 | }
|
---|
265 |
|
---|
266 | #define CHECK_RET_UNREG_RETURN(ret, message...) \
|
---|
267 | if (ret != EOK) { \
|
---|
268 | usb_log_error(message); \
|
---|
269 | usb_endpoint_manager_remove_ep( \
|
---|
270 | &instance->ep_manager, *address, 0, \
|
---|
271 | USB_DIRECTION_BOTH, NULL, NULL); \
|
---|
272 | usb_device_manager_release_address( \
|
---|
273 | &instance->dev_manager, *address); \
|
---|
274 | return ret; \
|
---|
275 | } else (void)0
|
---|
276 |
|
---|
277 | ret = usb_endpoint_manager_add_ep(
|
---|
278 | &instance->ep_manager, *address, 0,
|
---|
279 | USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL, USB_SPEED_FULL, 64,
|
---|
280 | 0, NULL, NULL);
|
---|
281 | CHECK_RET_UNREG_RETURN(ret,
|
---|
282 | "Failed to add root hub control endpoint: %s.\n", str_error(ret));
|
---|
283 |
|
---|
284 | match_id_t mid = { .id = "usb&class=hub", .score = 100 };
|
---|
285 | link_initialize(&mid.link);
|
---|
286 | match_id_list_t mid_list;
|
---|
287 | init_match_ids(&mid_list);
|
---|
288 | add_match_id(&mid_list, &mid);
|
---|
289 |
|
---|
290 | ret = hcd_add_device(
|
---|
291 | instance, device, *address, USB_SPEED_FULL, "rh", &mid_list);
|
---|
292 | CHECK_RET_UNREG_RETURN(ret,
|
---|
293 | "Failed to add hcd device: %s.\n", str_error(ret));
|
---|
294 |
|
---|
295 | return EOK;
|
---|
296 | #undef CHECK_RET_UNREG_RETURN
|
---|
297 | }
|
---|
298 |
|
---|
299 | /** Initialize hc structures.
|
---|
300 | *
|
---|
301 | * @param[in] device DDF instance of the device to use.
|
---|
302 | *
|
---|
303 | * This function does all the preparatory work for hc driver implementation.
|
---|
304 | * - gets device hw resources
|
---|
305 | * - disables OHCI legacy support
|
---|
306 | * - asks for interrupt
|
---|
307 | * - registers interrupt handler
|
---|
308 | */
|
---|
309 | int hcd_setup_device(ddf_dev_t *device, ddf_fun_t **hc_fun)
|
---|
310 | {
|
---|
311 | if (device == NULL)
|
---|
312 | return EBADMEM;
|
---|
313 |
|
---|
314 | hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
|
---|
315 | if (instance == NULL) {
|
---|
316 | usb_log_error("Failed to allocate OHCI driver.\n");
|
---|
317 | return ENOMEM;
|
---|
318 | }
|
---|
319 |
|
---|
320 | #define CHECK_RET_DEST_FREE_RETURN(ret, message...) \
|
---|
321 | if (ret != EOK) { \
|
---|
322 | if (instance->hc_fun) { \
|
---|
323 | ddf_fun_destroy(instance->hc_fun); \
|
---|
324 | } \
|
---|
325 | usb_log_error(message); \
|
---|
326 | return ret; \
|
---|
327 | } else (void)0
|
---|
328 |
|
---|
329 | instance->hc_fun = ddf_fun_create(device, fun_exposed, "hc");
|
---|
330 | int ret = instance->hc_fun ? EOK : ENOMEM;
|
---|
331 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
332 | "Failed to create OHCI HC function: %s.\n", str_error(ret));
|
---|
333 | ddf_fun_set_ops(instance->hc_fun, &hc_ops);
|
---|
334 | hcd_t *hcd = ddf_fun_data_alloc(instance->hc_fun, sizeof(hcd_t));
|
---|
335 | ret = hcd ? EOK : ENOMEM;
|
---|
336 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
337 | "Failed to allocate HCD structure: %s.\n", str_error(ret));
|
---|
338 |
|
---|
339 | hcd_init(hcd, USB_SPEED_FULL, BANDWIDTH_AVAILABLE_USB11,
|
---|
340 | bandwidth_count_usb11);
|
---|
341 |
|
---|
342 | ret = ddf_fun_bind(instance->hc_fun);
|
---|
343 | CHECK_RET_DEST_FREE_RETURN(ret,
|
---|
344 | "Failed to bind OHCI device function: %s.\n", str_error(ret));
|
---|
345 |
|
---|
346 | #define CHECK_RET_UNBIND_FREE_RETURN(ret, message...) \
|
---|
347 | if (ret != EOK) { \
|
---|
348 | ddf_fun_unbind(instance->hc_fun); \
|
---|
349 | CHECK_RET_DEST_FREE_RETURN(ret, \
|
---|
350 | "Failed to add OHCI to HC class: %s.\n", str_error(ret)); \
|
---|
351 | } else (void)0
|
---|
352 | ret = ddf_fun_add_to_category(instance->hc_fun, USB_HC_CATEGORY);
|
---|
353 | CHECK_RET_UNBIND_FREE_RETURN(ret,
|
---|
354 | "Failed to add hc to category: %s\n", str_error(ret));
|
---|
355 |
|
---|
356 | /* HC should be ok at this point (except it can't do anything) */
|
---|
357 | if (hc_fun)
|
---|
358 | *hc_fun = instance->hc_fun;
|
---|
359 |
|
---|
360 | return EOK;
|
---|
361 | }
|
---|
362 |
|
---|
363 | /**
|
---|
364 | * @}
|
---|
365 | */
|
---|