1 | /*
|
---|
2 | * Copyright (c) 2013 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 <adt/list.h>
|
---|
37 | #include <assert.h>
|
---|
38 | #include <async.h>
|
---|
39 | #include <ddf/driver.h>
|
---|
40 | #include <ddf/interrupt.h>
|
---|
41 | #include <device/hw_res_parsed.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <str_error.h>
|
---|
44 | #include <usb/classes/classes.h>
|
---|
45 | #include <usb/debug.h>
|
---|
46 | #include <usb/descriptor.h>
|
---|
47 | #include <usb/usb.h>
|
---|
48 | #include <usb_iface.h>
|
---|
49 | #include <usbhc_iface.h>
|
---|
50 |
|
---|
51 | #include "bus.h"
|
---|
52 | #include "endpoint.h"
|
---|
53 |
|
---|
54 | #include "ddf_helpers.h"
|
---|
55 |
|
---|
56 |
|
---|
57 | static int hcd_ddf_new_device(hc_device_t *hcd, ddf_dev_t *hc, device_t *hub_dev, unsigned port);
|
---|
58 | static int hcd_ddf_remove_device(ddf_dev_t *device, device_t *hub, unsigned port);
|
---|
59 |
|
---|
60 |
|
---|
61 | /* DDF INTERFACE */
|
---|
62 |
|
---|
63 | /** Register endpoint interface function.
|
---|
64 | * @param fun DDF function.
|
---|
65 | * @param endpoint_desc Endpoint description.
|
---|
66 | * @return Error code.
|
---|
67 | */
|
---|
68 | static int register_endpoint(ddf_fun_t *fun, usb_pipe_desc_t *pipe_desc,
|
---|
69 | const usb_endpoint_descriptors_t *ep_desc)
|
---|
70 | {
|
---|
71 | assert(fun);
|
---|
72 | hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
73 | device_t *dev = ddf_fun_data_get(fun);
|
---|
74 | assert(hcd);
|
---|
75 | assert(hcd->bus);
|
---|
76 | assert(dev);
|
---|
77 |
|
---|
78 | endpoint_t *ep;
|
---|
79 | const int err = bus_endpoint_add(dev, ep_desc, &ep);
|
---|
80 | if (err)
|
---|
81 | return err;
|
---|
82 |
|
---|
83 | if (pipe_desc) {
|
---|
84 | pipe_desc->endpoint_no = ep->endpoint;
|
---|
85 | pipe_desc->direction = ep->direction;
|
---|
86 | pipe_desc->transfer_type = ep->transfer_type;
|
---|
87 | pipe_desc->max_transfer_size = ep->max_transfer_size;
|
---|
88 | }
|
---|
89 | endpoint_del_ref(ep);
|
---|
90 |
|
---|
91 | return EOK;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** Unregister endpoint interface function.
|
---|
95 | * @param fun DDF function.
|
---|
96 | * @param endpoint_desc Endpoint description.
|
---|
97 | * @return Error code.
|
---|
98 | */
|
---|
99 | static int unregister_endpoint(ddf_fun_t *fun, const usb_pipe_desc_t *endpoint_desc)
|
---|
100 | {
|
---|
101 | assert(fun);
|
---|
102 | hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
103 | device_t *dev = ddf_fun_data_get(fun);
|
---|
104 | assert(hcd);
|
---|
105 | assert(hcd->bus);
|
---|
106 | assert(dev);
|
---|
107 |
|
---|
108 | endpoint_t *ep = bus_find_endpoint(dev, endpoint_desc->endpoint_no);
|
---|
109 | if (!ep)
|
---|
110 | return ENOENT;
|
---|
111 |
|
---|
112 | return bus_endpoint_remove(ep);
|
---|
113 | }
|
---|
114 |
|
---|
115 | static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
|
---|
116 | {
|
---|
117 | assert(fun);
|
---|
118 | hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
119 | device_t *dev = ddf_fun_data_get(fun);
|
---|
120 | assert(hcd);
|
---|
121 | assert(hcd->bus);
|
---|
122 | assert(dev);
|
---|
123 |
|
---|
124 | usb_log_debug("Device %d requested default address at %s speed\n",
|
---|
125 | dev->address, usb_str_speed(speed));
|
---|
126 | return bus_reserve_default_address(hcd->bus, speed);
|
---|
127 | }
|
---|
128 |
|
---|
129 | static int release_default_address(ddf_fun_t *fun)
|
---|
130 | {
|
---|
131 | assert(fun);
|
---|
132 | hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
133 | device_t *dev = ddf_fun_data_get(fun);
|
---|
134 | assert(hcd);
|
---|
135 | assert(hcd->bus);
|
---|
136 | assert(dev);
|
---|
137 |
|
---|
138 | usb_log_debug("Device %d released default address\n", dev->address);
|
---|
139 | bus_release_default_address(hcd->bus);
|
---|
140 |
|
---|
141 | return EOK;
|
---|
142 | }
|
---|
143 |
|
---|
144 | static int device_enumerate(ddf_fun_t *fun, unsigned port)
|
---|
145 | {
|
---|
146 | assert(fun);
|
---|
147 | ddf_dev_t *hc = ddf_fun_get_dev(fun);
|
---|
148 | assert(hc);
|
---|
149 | hc_device_t *hcd = dev_to_hcd(hc);
|
---|
150 | assert(hcd);
|
---|
151 | device_t *hub = ddf_fun_data_get(fun);
|
---|
152 | assert(hub);
|
---|
153 |
|
---|
154 | usb_log_debug("Hub %d reported a new USB device on port: %u\n",
|
---|
155 | hub->address, port);
|
---|
156 | return hcd_ddf_new_device(hcd, hc, hub, port);
|
---|
157 | }
|
---|
158 |
|
---|
159 | static int device_remove(ddf_fun_t *fun, unsigned port)
|
---|
160 | {
|
---|
161 | assert(fun);
|
---|
162 | ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
|
---|
163 | device_t *dev = ddf_fun_data_get(fun);
|
---|
164 | assert(ddf_dev);
|
---|
165 | assert(dev);
|
---|
166 | usb_log_debug("Hub `%s' reported removal of device on port %u\n",
|
---|
167 | ddf_fun_get_name(fun), port);
|
---|
168 | return hcd_ddf_remove_device(ddf_dev, dev, port);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /** Gets handle of the respective device.
|
---|
172 | *
|
---|
173 | * @param[in] fun Device function.
|
---|
174 | * @param[out] handle Place to write the handle.
|
---|
175 | * @return Error code.
|
---|
176 | */
|
---|
177 | static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
|
---|
178 | {
|
---|
179 | assert(fun);
|
---|
180 | if (handle)
|
---|
181 | *handle = ddf_fun_get_handle(fun);
|
---|
182 | return EOK;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /** Inbound communication interface function.
|
---|
186 | * @param fun DDF function.
|
---|
187 | * @param target Communication target.
|
---|
188 | * @param setup_data Data to use in setup stage (control transfers).
|
---|
189 | * @param data Pointer to data buffer.
|
---|
190 | * @param size Size of the data buffer.
|
---|
191 | * @param callback Function to call on communication end.
|
---|
192 | * @param arg Argument passed to the callback function.
|
---|
193 | * @return Error code.
|
---|
194 | */
|
---|
195 | static int dev_read(ddf_fun_t *fun, usb_target_t target,
|
---|
196 | uint64_t setup_data, char *data, size_t size,
|
---|
197 | usbhc_iface_transfer_callback_t callback, void *arg)
|
---|
198 | {
|
---|
199 | assert(fun);
|
---|
200 | device_t *dev = ddf_fun_data_get(fun);
|
---|
201 | assert(dev);
|
---|
202 |
|
---|
203 | target.address = dev->address;
|
---|
204 |
|
---|
205 | return bus_device_send_batch(dev, target, USB_DIRECTION_IN,
|
---|
206 | data, size, setup_data,
|
---|
207 | callback, arg, "READ");
|
---|
208 | }
|
---|
209 |
|
---|
210 | /** Outbound communication interface function.
|
---|
211 | * @param fun DDF function.
|
---|
212 | * @param target Communication target.
|
---|
213 | * @param setup_data Data to use in setup stage (control transfers).
|
---|
214 | * @param data Pointer to data buffer.
|
---|
215 | * @param size Size of the data buffer.
|
---|
216 | * @param callback Function to call on communication end.
|
---|
217 | * @param arg Argument passed to the callback function.
|
---|
218 | * @return Error code.
|
---|
219 | */
|
---|
220 | static int dev_write(ddf_fun_t *fun, usb_target_t target,
|
---|
221 | uint64_t setup_data, const char *data, size_t size,
|
---|
222 | usbhc_iface_transfer_callback_t callback, void *arg)
|
---|
223 | {
|
---|
224 | assert(fun);
|
---|
225 | device_t *dev = ddf_fun_data_get(fun);
|
---|
226 | assert(dev);
|
---|
227 |
|
---|
228 | target.address = dev->address;
|
---|
229 |
|
---|
230 | return bus_device_send_batch(dev, target, USB_DIRECTION_OUT,
|
---|
231 | (char *) data, size, setup_data,
|
---|
232 | callback, arg, "WRITE");
|
---|
233 | }
|
---|
234 |
|
---|
235 | /** USB device interface */
|
---|
236 | static usb_iface_t usb_iface = {
|
---|
237 | .get_my_device_handle = get_my_device_handle,
|
---|
238 | };
|
---|
239 |
|
---|
240 | /** USB host controller interface */
|
---|
241 | static usbhc_iface_t usbhc_iface = {
|
---|
242 | .reserve_default_address = reserve_default_address,
|
---|
243 | .release_default_address = release_default_address,
|
---|
244 |
|
---|
245 | .device_enumerate = device_enumerate,
|
---|
246 | .device_remove = device_remove,
|
---|
247 |
|
---|
248 | .register_endpoint = register_endpoint,
|
---|
249 | .unregister_endpoint = unregister_endpoint,
|
---|
250 |
|
---|
251 | .read = dev_read,
|
---|
252 | .write = dev_write,
|
---|
253 | };
|
---|
254 |
|
---|
255 | /** Standard USB device interface) */
|
---|
256 | static ddf_dev_ops_t usb_ops = {
|
---|
257 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
258 | .interfaces[USBHC_DEV_IFACE] = &usbhc_iface,
|
---|
259 | };
|
---|
260 |
|
---|
261 |
|
---|
262 | /* DDF HELPERS */
|
---|
263 |
|
---|
264 | #define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
|
---|
265 | do { \
|
---|
266 | match_id_t *mid = malloc(sizeof(match_id_t)); \
|
---|
267 | if (!mid) { \
|
---|
268 | clean_match_ids(list); \
|
---|
269 | return ENOMEM; \
|
---|
270 | } \
|
---|
271 | char *id = NULL; \
|
---|
272 | int ret = asprintf(&id, str, ##__VA_ARGS__); \
|
---|
273 | if (ret < 0) { \
|
---|
274 | clean_match_ids(list); \
|
---|
275 | free(mid); \
|
---|
276 | return ENOMEM; \
|
---|
277 | } \
|
---|
278 | mid->score = sc; \
|
---|
279 | mid->id = id; \
|
---|
280 | add_match_id(list, mid); \
|
---|
281 | } while (0)
|
---|
282 |
|
---|
283 | /* This is a copy of lib/usbdev/src/recognise.c */
|
---|
284 | static int create_match_ids(match_id_list_t *l,
|
---|
285 | usb_standard_device_descriptor_t *d)
|
---|
286 | {
|
---|
287 | assert(l);
|
---|
288 | assert(d);
|
---|
289 |
|
---|
290 | if (d->vendor_id != 0) {
|
---|
291 | /* First, with release number. */
|
---|
292 | ADD_MATCHID_OR_RETURN(l, 100,
|
---|
293 | "usb&vendor=%#04x&product=%#04x&release=%x.%x",
|
---|
294 | d->vendor_id, d->product_id, (d->device_version >> 8),
|
---|
295 | (d->device_version & 0xff));
|
---|
296 |
|
---|
297 | /* Next, without release number. */
|
---|
298 | ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
|
---|
299 | d->vendor_id, d->product_id);
|
---|
300 | }
|
---|
301 |
|
---|
302 | /* Class match id */
|
---|
303 | ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
|
---|
304 | usb_str_class(d->device_class));
|
---|
305 |
|
---|
306 | /* As a last resort, try fallback driver. */
|
---|
307 | ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
|
---|
308 |
|
---|
309 | return EOK;
|
---|
310 | }
|
---|
311 |
|
---|
312 | static int hcd_ddf_remove_device(ddf_dev_t *device, device_t *hub,
|
---|
313 | unsigned port)
|
---|
314 | {
|
---|
315 | assert(device);
|
---|
316 |
|
---|
317 | hc_device_t *hcd = dev_to_hcd(device);
|
---|
318 | assert(hcd);
|
---|
319 | assert(hcd->bus);
|
---|
320 |
|
---|
321 | fibril_mutex_lock(&hub->guard);
|
---|
322 |
|
---|
323 | device_t *victim = NULL;
|
---|
324 |
|
---|
325 | list_foreach(hub->devices, link, device_t, it) {
|
---|
326 | if (it->port == port) {
|
---|
327 | victim = it;
|
---|
328 | break;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | if (victim) {
|
---|
332 | assert(victim->fun);
|
---|
333 | assert(victim->port == port);
|
---|
334 | assert(victim->hub == hub);
|
---|
335 | list_remove(&victim->link);
|
---|
336 | fibril_mutex_unlock(&hub->guard);
|
---|
337 | const int ret = ddf_fun_unbind(victim->fun);
|
---|
338 | if (ret == EOK) {
|
---|
339 | bus_device_remove(victim);
|
---|
340 | ddf_fun_destroy(victim->fun);
|
---|
341 | } else {
|
---|
342 | usb_log_warning("Failed to unbind device `%s': %s\n",
|
---|
343 | ddf_fun_get_name(victim->fun), str_error(ret));
|
---|
344 | }
|
---|
345 | return EOK;
|
---|
346 | }
|
---|
347 | fibril_mutex_unlock(&hub->guard);
|
---|
348 | return ENOENT;
|
---|
349 | }
|
---|
350 |
|
---|
351 | device_t *hcd_ddf_fun_create(hc_device_t *hc)
|
---|
352 | {
|
---|
353 | /* Create DDF function for the new device */
|
---|
354 | ddf_fun_t *fun = ddf_fun_create(hc->ddf_dev, fun_inner, NULL);
|
---|
355 | if (!fun)
|
---|
356 | return NULL;
|
---|
357 |
|
---|
358 | ddf_fun_set_ops(fun, &usb_ops);
|
---|
359 |
|
---|
360 | /* Create USB device node for the new device */
|
---|
361 | device_t *dev = ddf_fun_data_alloc(fun, hc->bus->device_size);
|
---|
362 | if (!dev) {
|
---|
363 | ddf_fun_destroy(fun);
|
---|
364 | return NULL;
|
---|
365 | }
|
---|
366 |
|
---|
367 | bus_device_init(dev, hc->bus);
|
---|
368 | dev->fun = fun;
|
---|
369 | return dev;
|
---|
370 | }
|
---|
371 |
|
---|
372 | void hcd_ddf_fun_destroy(device_t *dev)
|
---|
373 | {
|
---|
374 | assert(dev);
|
---|
375 | assert(dev->fun);
|
---|
376 | ddf_fun_destroy(dev->fun);
|
---|
377 | }
|
---|
378 |
|
---|
379 | int hcd_device_explore(device_t *device)
|
---|
380 | {
|
---|
381 | int err;
|
---|
382 | match_id_list_t mids;
|
---|
383 | usb_standard_device_descriptor_t desc = { 0 };
|
---|
384 |
|
---|
385 | init_match_ids(&mids);
|
---|
386 |
|
---|
387 | const usb_target_t control_ep = {{
|
---|
388 | .address = device->address,
|
---|
389 | .endpoint = 0,
|
---|
390 | }};
|
---|
391 |
|
---|
392 | /* Get std device descriptor */
|
---|
393 | const usb_device_request_setup_packet_t get_device_desc =
|
---|
394 | GET_DEVICE_DESC(sizeof(desc));
|
---|
395 |
|
---|
396 | usb_log_debug("Device(%d): Requesting full device descriptor.",
|
---|
397 | device->address);
|
---|
398 | ssize_t got = bus_device_send_batch_sync(device, control_ep, USB_DIRECTION_IN,
|
---|
399 | (char *) &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
|
---|
400 | "read device descriptor");
|
---|
401 | if (got < 0) {
|
---|
402 | err = got < 0 ? got : EOVERFLOW;
|
---|
403 | usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
|
---|
404 | device->address, str_error(err));
|
---|
405 | goto out;
|
---|
406 | }
|
---|
407 |
|
---|
408 | /* Create match ids from the device descriptor */
|
---|
409 | usb_log_debug("Device(%d): Creating match IDs.", device->address);
|
---|
410 | if ((err = create_match_ids(&mids, &desc))) {
|
---|
411 | usb_log_error("Device(%d): Failed to create match ids: %s", device->address, str_error(err));
|
---|
412 | goto out;
|
---|
413 | }
|
---|
414 |
|
---|
415 | list_foreach(mids.ids, link, const match_id_t, mid) {
|
---|
416 | ddf_fun_add_match_id(device->fun, mid->id, mid->score);
|
---|
417 | }
|
---|
418 |
|
---|
419 | out:
|
---|
420 | clean_match_ids(&mids);
|
---|
421 | return err;
|
---|
422 | }
|
---|
423 |
|
---|
424 | static int hcd_ddf_new_device(hc_device_t *hcd, ddf_dev_t *hc, device_t *hub, unsigned port)
|
---|
425 | {
|
---|
426 | int err;
|
---|
427 | assert(hcd);
|
---|
428 | assert(hcd->bus);
|
---|
429 | assert(hub);
|
---|
430 | assert(hc);
|
---|
431 |
|
---|
432 | device_t *dev = hcd_ddf_fun_create(hcd);
|
---|
433 | if (!dev) {
|
---|
434 | usb_log_error("Failed to create USB device function.");
|
---|
435 | return ENOMEM;
|
---|
436 | }
|
---|
437 |
|
---|
438 | dev->hub = hub;
|
---|
439 | dev->port = port;
|
---|
440 |
|
---|
441 | if ((err = bus_device_enumerate(dev))) {
|
---|
442 | usb_log_error("Failed to initialize USB dev memory structures.");
|
---|
443 | return err;
|
---|
444 | }
|
---|
445 |
|
---|
446 | /* If the driver didn't name the dev when enumerating,
|
---|
447 | * do it in some generic way.
|
---|
448 | */
|
---|
449 | if (!ddf_fun_get_name(dev->fun)) {
|
---|
450 | bus_device_set_default_name(dev);
|
---|
451 | }
|
---|
452 |
|
---|
453 | if ((err = ddf_fun_bind(dev->fun))) {
|
---|
454 | usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
|
---|
455 | goto err_usb_dev;
|
---|
456 | }
|
---|
457 |
|
---|
458 | fibril_mutex_lock(&hub->guard);
|
---|
459 | list_append(&dev->link, &hub->devices);
|
---|
460 | fibril_mutex_unlock(&hub->guard);
|
---|
461 |
|
---|
462 | return EOK;
|
---|
463 |
|
---|
464 | err_usb_dev:
|
---|
465 | hcd_ddf_fun_destroy(dev);
|
---|
466 | return err;
|
---|
467 | }
|
---|
468 |
|
---|
469 | /** Announce root hub to the DDF
|
---|
470 | *
|
---|
471 | * @param[in] device Host controller ddf device
|
---|
472 | * @return Error code
|
---|
473 | */
|
---|
474 | int hcd_setup_virtual_root_hub(hc_device_t *hcd)
|
---|
475 | {
|
---|
476 | int err;
|
---|
477 |
|
---|
478 | assert(hcd);
|
---|
479 |
|
---|
480 | if ((err = bus_reserve_default_address(hcd->bus, USB_SPEED_MAX))) {
|
---|
481 | usb_log_error("Failed to reserve default address for roothub setup: %s", str_error(err));
|
---|
482 | return err;
|
---|
483 | }
|
---|
484 |
|
---|
485 | device_t *dev = hcd_ddf_fun_create(hcd);
|
---|
486 | if (!dev) {
|
---|
487 | usb_log_error("Failed to create function for the root hub.");
|
---|
488 | goto err_default_address;
|
---|
489 | }
|
---|
490 |
|
---|
491 | ddf_fun_set_name(dev->fun, "roothub");
|
---|
492 |
|
---|
493 | /* Assign an address to the device */
|
---|
494 | if ((err = bus_device_enumerate(dev))) {
|
---|
495 | usb_log_error("Failed to enumerate roothub device: %s", str_error(err));
|
---|
496 | goto err_usb_dev;
|
---|
497 | }
|
---|
498 |
|
---|
499 | if ((err = ddf_fun_bind(dev->fun))) {
|
---|
500 | usb_log_error("Failed to register roothub: %s.", str_error(err));
|
---|
501 | goto err_usb_dev;
|
---|
502 | }
|
---|
503 |
|
---|
504 | bus_release_default_address(hcd->bus);
|
---|
505 | return EOK;
|
---|
506 |
|
---|
507 | err_usb_dev:
|
---|
508 | hcd_ddf_fun_destroy(dev);
|
---|
509 | err_default_address:
|
---|
510 | bus_release_default_address(hcd->bus);
|
---|
511 | return err;
|
---|
512 | }
|
---|
513 |
|
---|
514 | /** Initialize hc structures.
|
---|
515 | *
|
---|
516 | * @param[in] device DDF instance of the device to use.
|
---|
517 | * @param[in] max_speed Maximum supported USB speed.
|
---|
518 | * @param[in] bw available bandwidth.
|
---|
519 | * @param[in] bw_count Function to compute required ep bandwidth.
|
---|
520 | *
|
---|
521 | * @return Error code.
|
---|
522 | * This function does all the ddf work for hc driver.
|
---|
523 | */
|
---|
524 | int hcd_ddf_setup_hc(ddf_dev_t *device, size_t size)
|
---|
525 | {
|
---|
526 | assert(device);
|
---|
527 |
|
---|
528 | hc_device_t *instance = ddf_dev_data_alloc(device, size);
|
---|
529 | if (instance == NULL) {
|
---|
530 | usb_log_error("Failed to allocate HCD ddf structure.\n");
|
---|
531 | return ENOMEM;
|
---|
532 | }
|
---|
533 | instance->ddf_dev = device;
|
---|
534 |
|
---|
535 | int ret = ENOMEM;
|
---|
536 | instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
|
---|
537 | if (!instance->ctl_fun) {
|
---|
538 | usb_log_error("Failed to create HCD ddf fun.\n");
|
---|
539 | goto err_destroy_fun;
|
---|
540 | }
|
---|
541 |
|
---|
542 | ret = ddf_fun_bind(instance->ctl_fun);
|
---|
543 | if (ret != EOK) {
|
---|
544 | usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
|
---|
545 | goto err_destroy_fun;
|
---|
546 | }
|
---|
547 |
|
---|
548 | ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
|
---|
549 | if (ret != EOK) {
|
---|
550 | usb_log_error("Failed to add fun to category: %s.\n",
|
---|
551 | str_error(ret));
|
---|
552 | ddf_fun_unbind(instance->ctl_fun);
|
---|
553 | goto err_destroy_fun;
|
---|
554 | }
|
---|
555 |
|
---|
556 | /* HC should be ok at this point (except it can't do anything) */
|
---|
557 | return EOK;
|
---|
558 |
|
---|
559 | err_destroy_fun:
|
---|
560 | ddf_fun_destroy(instance->ctl_fun);
|
---|
561 | instance->ctl_fun = NULL;
|
---|
562 | return ret;
|
---|
563 | }
|
---|
564 |
|
---|
565 | void hcd_ddf_clean_hc(hc_device_t *hcd)
|
---|
566 | {
|
---|
567 | if (ddf_fun_unbind(hcd->ctl_fun) == EOK)
|
---|
568 | ddf_fun_destroy(hcd->ctl_fun);
|
---|
569 | }
|
---|
570 |
|
---|
571 | /** Call the parent driver with a request to enable interrupt
|
---|
572 | *
|
---|
573 | * @param[in] device Device asking for interrupts
|
---|
574 | * @param[in] inum Interrupt number
|
---|
575 | * @return Error code.
|
---|
576 | */
|
---|
577 | int hcd_ddf_enable_interrupt(hc_device_t *hcd, int inum)
|
---|
578 | {
|
---|
579 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
|
---|
580 | if (parent_sess == NULL)
|
---|
581 | return EIO;
|
---|
582 |
|
---|
583 | return hw_res_enable_interrupt(parent_sess, inum);
|
---|
584 | }
|
---|
585 |
|
---|
586 | int hcd_ddf_get_registers(hc_device_t *hcd, hw_res_list_parsed_t *hw_res)
|
---|
587 | {
|
---|
588 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
|
---|
589 | if (parent_sess == NULL)
|
---|
590 | return EIO;
|
---|
591 |
|
---|
592 | hw_res_list_parsed_init(hw_res);
|
---|
593 | const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
|
---|
594 | if (ret != EOK)
|
---|
595 | hw_res_list_parsed_clean(hw_res);
|
---|
596 | return ret;
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * @}
|
---|
601 | */
|
---|