source: mainline/uspace/lib/usbhost/src/ddf_helpers.c@ 56db65d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 56db65d was 56db65d, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usbhost: provide usb_endpoint_desc_t to bus when registering endpoint

This finishes the path of arbitrary information fetched from device all the way down to registering the endpoint in the bus.

  • Property mode set to 100644
File size: 22.0 KB
Line 
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 <usb/classes/classes.h>
37#include <usb/host/bus.h>
38#include <usb/debug.h>
39#include <usb/descriptor.h>
40#include <usb/request.h>
41#include <usb/usb.h>
42
43#include <adt/list.h>
44#include <assert.h>
45#include <async.h>
46#include <ddf/driver.h>
47#include <ddf/interrupt.h>
48#include <device/hw_res_parsed.h>
49#include <errno.h>
50#include <fibril_synch.h>
51#include <macros.h>
52#include <stdlib.h>
53#include <str_error.h>
54#include <usb_iface.h>
55
56#include "ddf_helpers.h"
57
58typedef struct hc_dev {
59 ddf_fun_t *ctl_fun;
60 hcd_t hcd;
61} hc_dev_t;
62
63static hc_dev_t *dev_to_hc_dev(ddf_dev_t *dev)
64{
65 return ddf_dev_data_get(dev);
66}
67
68hcd_t *dev_to_hcd(ddf_dev_t *dev)
69{
70 hc_dev_t *hc_dev = dev_to_hc_dev(dev);
71 if (!hc_dev) {
72 usb_log_error("Invalid HCD device.\n");
73 return NULL;
74 }
75 return &hc_dev->hcd;
76}
77
78
79static int hcd_ddf_new_device(hcd_t *hcd, ddf_dev_t *hc, device_t *hub_dev, unsigned port);
80static int hcd_ddf_remove_device(ddf_dev_t *device, device_t *hub, unsigned port);
81
82
83/* DDF INTERFACE */
84
85/** Register endpoint interface function.
86 * @param fun DDF function.
87 * @param endpoint_desc Endpoint description.
88 * @return Error code.
89 */
90static int register_endpoint(
91 ddf_fun_t *fun, usb_endpoint_desc_t *endpoint_desc)
92{
93 assert(fun);
94 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
95 device_t *dev = ddf_fun_data_get(fun);
96 assert(hcd);
97 assert(hcd->bus);
98 assert(dev);
99
100 usb_log_debug("Register endpoint %d:%d %s-%s %zuB %ums.\n",
101 dev->address, endpoint_desc->endpoint_no,
102 usb_str_transfer_type(endpoint_desc->transfer_type),
103 usb_str_direction(endpoint_desc->direction),
104 endpoint_desc->max_packet_size, endpoint_desc->usb2.polling_interval);
105
106 return bus_add_ep(hcd->bus, dev, endpoint_desc);
107}
108
109 /** Unregister endpoint interface function.
110 * @param fun DDF function.
111 * @param endpoint_desc Endpoint description.
112 * @return Error code.
113 */
114static int unregister_endpoint(
115 ddf_fun_t *fun, usb_endpoint_desc_t *endpoint_desc)
116{
117 assert(fun);
118 hcd_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 const usb_target_t target = {{
125 .address = dev->address,
126 .endpoint = endpoint_desc->endpoint_no
127 }};
128
129 usb_log_debug("Unregister endpoint %d:%d %s.\n",
130 dev->address, endpoint_desc->endpoint_no,
131 usb_str_direction(endpoint_desc->direction));
132 return bus_remove_ep(hcd->bus, dev, target, endpoint_desc->direction);
133}
134
135static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
136{
137 assert(fun);
138 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
139 device_t *dev = ddf_fun_data_get(fun);
140 assert(hcd);
141 assert(hcd->bus);
142 assert(dev);
143
144 usb_log_debug("Device %d requested default address at %s speed\n",
145 dev->address, usb_str_speed(speed));
146 return bus_reserve_default_address(hcd->bus, speed);
147}
148
149static int release_default_address(ddf_fun_t *fun)
150{
151 assert(fun);
152 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
153 device_t *dev = ddf_fun_data_get(fun);
154 assert(hcd);
155 assert(hcd->bus);
156 assert(dev);
157
158 usb_log_debug("Device %d released default address\n", dev->address);
159 return bus_release_default_address(hcd->bus);
160}
161
162static int device_enumerate(ddf_fun_t *fun, unsigned port)
163{
164 assert(fun);
165 ddf_dev_t *hc = ddf_fun_get_dev(fun);
166 assert(hc);
167 hcd_t *hcd = dev_to_hcd(hc);
168 assert(hcd);
169 device_t *hub = ddf_fun_data_get(fun);
170 assert(hub);
171
172 usb_log_debug("Hub %d reported a new USB device on port: %u\n",
173 hub->address, port);
174 return hcd_ddf_new_device(hcd, hc, hub, port);
175}
176
177static int device_remove(ddf_fun_t *fun, unsigned port)
178{
179 assert(fun);
180 ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
181 device_t *dev = ddf_fun_data_get(fun);
182 assert(ddf_dev);
183 assert(dev);
184 usb_log_debug("Hub `%s' reported removal of device on port %u\n",
185 ddf_fun_get_name(fun), port);
186 return hcd_ddf_remove_device(ddf_dev, dev, port);
187}
188
189/** Gets handle of the respective device.
190 *
191 * @param[in] fun Device function.
192 * @param[out] handle Place to write the handle.
193 * @return Error code.
194 */
195static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
196{
197 assert(fun);
198 if (handle)
199 *handle = ddf_fun_get_handle(fun);
200 return EOK;
201}
202
203/** Inbound communication interface function.
204 * @param fun DDF function.
205 * @param target Communication target.
206 * @param setup_data Data to use in setup stage (control transfers).
207 * @param data Pointer to data buffer.
208 * @param size Size of the data buffer.
209 * @param callback Function to call on communication end.
210 * @param arg Argument passed to the callback function.
211 * @return Error code.
212 */
213static int dev_read(ddf_fun_t *fun, usb_target_t target,
214 uint64_t setup_data, char *data, size_t size,
215 usb_iface_transfer_callback_t callback, void *arg)
216{
217 assert(fun);
218 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
219 device_t *dev = ddf_fun_data_get(fun);
220 assert(dev);
221
222 target.address = dev->address;
223
224 return hcd_send_batch(hcd, dev, target, USB_DIRECTION_IN,
225 data, size, setup_data,
226 callback, arg, "READ");
227}
228
229/** Outbound communication interface function.
230 * @param fun DDF function.
231 * @param target Communication target.
232 * @param setup_data Data to use in setup stage (control transfers).
233 * @param data Pointer to data buffer.
234 * @param size Size of the data buffer.
235 * @param callback Function to call on communication end.
236 * @param arg Argument passed to the callback function.
237 * @return Error code.
238 */
239static int dev_write(ddf_fun_t *fun, usb_target_t target,
240 uint64_t setup_data, const char *data, size_t size,
241 usb_iface_transfer_callback_t callback, void *arg)
242{
243 assert(fun);
244 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
245 device_t *dev = ddf_fun_data_get(fun);
246 assert(dev);
247
248 target.address = dev->address;
249
250 return hcd_send_batch(hcd, dev, target, USB_DIRECTION_OUT,
251 (char *) data, size, setup_data,
252 callback, arg, "WRITE");
253}
254
255/** USB device interface */
256static usb_iface_t usb_iface = {
257 .get_my_device_handle = get_my_device_handle,
258
259 .reserve_default_address = reserve_default_address,
260 .release_default_address = release_default_address,
261
262 .device_enumerate = device_enumerate,
263 .device_remove = device_remove,
264
265 .register_endpoint = register_endpoint,
266 .unregister_endpoint = unregister_endpoint,
267
268 .read = dev_read,
269 .write = dev_write,
270};
271
272/** Standard USB device interface) */
273static ddf_dev_ops_t usb_ops = {
274 .interfaces[USB_DEV_IFACE] = &usb_iface,
275};
276
277
278/* DDF HELPERS */
279
280#define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
281do { \
282 match_id_t *mid = malloc(sizeof(match_id_t)); \
283 if (!mid) { \
284 clean_match_ids(list); \
285 return ENOMEM; \
286 } \
287 char *id = NULL; \
288 int ret = asprintf(&id, str, ##__VA_ARGS__); \
289 if (ret < 0) { \
290 clean_match_ids(list); \
291 free(mid); \
292 return ENOMEM; \
293 } \
294 mid->score = sc; \
295 mid->id = id; \
296 add_match_id(list, mid); \
297} while (0)
298
299/* This is a copy of lib/usbdev/src/recognise.c */
300static int create_match_ids(match_id_list_t *l,
301 usb_standard_device_descriptor_t *d)
302{
303 assert(l);
304 assert(d);
305
306 if (d->vendor_id != 0) {
307 /* First, with release number. */
308 ADD_MATCHID_OR_RETURN(l, 100,
309 "usb&vendor=%#04x&product=%#04x&release=%x.%x",
310 d->vendor_id, d->product_id, (d->device_version >> 8),
311 (d->device_version & 0xff));
312
313 /* Next, without release number. */
314 ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
315 d->vendor_id, d->product_id);
316 }
317
318 /* Class match id */
319 ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
320 usb_str_class(d->device_class));
321
322 /* As a last resort, try fallback driver. */
323 ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
324
325 return EOK;
326}
327
328static int hcd_ddf_remove_device(ddf_dev_t *device, device_t *hub,
329 unsigned port)
330{
331 assert(device);
332
333 hcd_t *hcd = dev_to_hcd(device);
334 assert(hcd);
335 assert(hcd->bus);
336
337 hc_dev_t *hc_dev = dev_to_hc_dev(device);
338 assert(hc_dev);
339
340 fibril_mutex_lock(&hub->guard);
341
342 device_t *victim = NULL;
343
344 list_foreach(hub->devices, link, device_t, it) {
345 if (it->port == port) {
346 victim = it;
347 break;
348 }
349 }
350 if (victim) {
351 assert(victim->fun);
352 assert(victim->port == port);
353 assert(victim->hub == hub);
354 list_remove(&victim->link);
355 fibril_mutex_unlock(&hub->guard);
356 const int ret = ddf_fun_unbind(victim->fun);
357 if (ret == EOK) {
358 usb_address_t address = victim->address;
359 bus_remove_device(hcd->bus, hcd, victim);
360 ddf_fun_destroy(victim->fun);
361 bus_release_address(hcd->bus, address);
362 } else {
363 usb_log_warning("Failed to unbind device `%s': %s\n",
364 ddf_fun_get_name(victim->fun), str_error(ret));
365 }
366 return EOK;
367 }
368 fibril_mutex_unlock(&hub->guard);
369 return ENOENT;
370}
371
372device_t *hcd_ddf_device_create(ddf_dev_t *hc, size_t device_size)
373{
374 /* Create DDF function for the new device */
375 ddf_fun_t *fun = ddf_fun_create(hc, fun_inner, NULL);
376 if (!fun)
377 return NULL;
378
379 ddf_fun_set_ops(fun, &usb_ops);
380
381 /* Create USB device node for the new device */
382 device_t *dev = ddf_fun_data_alloc(fun, device_size);
383 if (!dev) {
384 ddf_fun_destroy(fun);
385 return NULL;
386 }
387
388 device_init(dev);
389 dev->fun = fun;
390 return dev;
391}
392
393void hcd_ddf_device_destroy(device_t *dev)
394{
395 assert(dev);
396 assert(dev->fun);
397 ddf_fun_destroy(dev->fun);
398}
399
400int hcd_ddf_device_explore(hcd_t *hcd, device_t *device)
401{
402 int err;
403 match_id_list_t mids;
404 usb_standard_device_descriptor_t desc = { 0 };
405
406 init_match_ids(&mids);
407
408 const usb_target_t control_ep = {{
409 .address = device->address,
410 .endpoint = 0,
411 }};
412
413 /* Get std device descriptor */
414 const usb_device_request_setup_packet_t get_device_desc =
415 GET_DEVICE_DESC(sizeof(desc));
416
417 usb_log_debug("Device(%d): Requesting full device descriptor.",
418 device->address);
419 ssize_t got = hcd_send_batch_sync(hcd, device, control_ep, USB_DIRECTION_IN,
420 (char *) &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
421 "read device descriptor");
422 if (got < 0) {
423 err = got < 0 ? got : EOVERFLOW;
424 usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
425 device->address, str_error(err));
426 goto out;
427 }
428
429 /* Create match ids from the device descriptor */
430 usb_log_debug("Device(%d): Creating match IDs.", device->address);
431 if ((err = create_match_ids(&mids, &desc))) {
432 usb_log_error("Device(%d): Failed to create match ids: %s", device->address, str_error(err));
433 goto out;
434 }
435
436 list_foreach(mids.ids, link, const match_id_t, mid) {
437 ddf_fun_add_match_id(device->fun, mid->id, mid->score);
438 }
439
440out:
441 clean_match_ids(&mids);
442 return err;
443}
444
445static int hcd_ddf_new_device(hcd_t *hcd, ddf_dev_t *hc, device_t *hub, unsigned port)
446{
447 int err;
448 assert(hcd);
449 assert(hcd->bus);
450 assert(hub);
451 assert(hc);
452
453 device_t *dev = hcd_ddf_device_create(hc, hcd->bus->device_size);
454 if (!dev) {
455 usb_log_error("Failed to create USB device function.");
456 return ENOMEM;
457 }
458
459 dev->hub = hub;
460 dev->port = port;
461
462 if ((err = bus_enumerate_device(hcd->bus, hcd, dev))) {
463 usb_log_error("Failed to initialize USB dev memory structures.");
464 return err;
465 }
466
467 /* If the driver didn't name the dev when enumerating,
468 * do it in some generic way.
469 */
470 if (!ddf_fun_get_name(dev->fun)) {
471 device_set_default_name(dev);
472 }
473
474 if ((err = ddf_fun_bind(dev->fun))) {
475 usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
476 goto err_usb_dev;
477 }
478
479 fibril_mutex_lock(&hub->guard);
480 list_append(&dev->link, &hub->devices);
481 fibril_mutex_unlock(&hub->guard);
482
483 return EOK;
484
485err_usb_dev:
486 hcd_ddf_device_destroy(dev);
487 return err;
488}
489
490/** Announce root hub to the DDF
491 *
492 * @param[in] device Host controller ddf device
493 * @return Error code
494 */
495int hcd_setup_virtual_root_hub(hcd_t *hcd, ddf_dev_t *hc)
496{
497 int err;
498
499 assert(hc);
500 assert(hcd);
501 assert(hcd->bus);
502
503 if ((err = bus_reserve_default_address(hcd->bus, USB_SPEED_MAX))) {
504 usb_log_error("Failed to reserve default address for roothub setup: %s", str_error(err));
505 return err;
506 }
507
508 device_t *dev = hcd_ddf_device_create(hc, USB_SPEED_MAX);
509 if (!dev) {
510 usb_log_error("Failed to create function for the root hub.");
511 goto err_default_address;
512 }
513
514 ddf_fun_set_name(dev->fun, "roothub");
515
516 dev->tt = (usb_tt_address_t) {
517 .address = -1,
518 .port = 0,
519 };
520
521 /* Assign an address to the device */
522 if ((err = bus_enumerate_device(hcd->bus, hcd, dev))) {
523 usb_log_error("Failed to enumerate roothub device: %s", str_error(err));
524 goto err_usb_dev;
525 }
526
527 if ((err = ddf_fun_bind(dev->fun))) {
528 usb_log_error("Failed to register roothub: %s.", str_error(err));
529 goto err_usb_dev;
530 }
531
532 bus_release_default_address(hcd->bus);
533 return EOK;
534
535err_usb_dev:
536 hcd_ddf_device_destroy(dev);
537err_default_address:
538 bus_release_default_address(hcd->bus);
539 return err;
540}
541
542/** Initialize hc structures.
543 *
544 * @param[in] device DDF instance of the device to use.
545 * @param[in] max_speed Maximum supported USB speed.
546 * @param[in] bw available bandwidth.
547 * @param[in] bw_count Function to compute required ep bandwidth.
548 *
549 * @return Error code.
550 * This function does all the ddf work for hc driver.
551 */
552int hcd_ddf_setup_hc(ddf_dev_t *device)
553{
554 assert(device);
555
556 hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
557 if (instance == NULL) {
558 usb_log_error("Failed to allocate HCD ddf structure.\n");
559 return ENOMEM;
560 }
561 hcd_init(&instance->hcd);
562
563 int ret = ENOMEM;
564 instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
565 if (!instance->ctl_fun) {
566 usb_log_error("Failed to create HCD ddf fun.\n");
567 goto err_destroy_fun;
568 }
569
570 ret = ddf_fun_bind(instance->ctl_fun);
571 if (ret != EOK) {
572 usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
573 goto err_destroy_fun;
574 }
575
576 ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
577 if (ret != EOK) {
578 usb_log_error("Failed to add fun to category: %s.\n",
579 str_error(ret));
580 ddf_fun_unbind(instance->ctl_fun);
581 goto err_destroy_fun;
582 }
583
584 /* HC should be ok at this point (except it can't do anything) */
585 return EOK;
586
587err_destroy_fun:
588 ddf_fun_destroy(instance->ctl_fun);
589 instance->ctl_fun = NULL;
590 return ret;
591}
592
593void hcd_ddf_clean_hc(ddf_dev_t *device)
594{
595 assert(device);
596 hc_dev_t *hc = dev_to_hc_dev(device);
597 assert(hc);
598 const int ret = ddf_fun_unbind(hc->ctl_fun);
599 if (ret == EOK)
600 ddf_fun_destroy(hc->ctl_fun);
601}
602
603//TODO: Cache parent session in HCD
604/** Call the parent driver with a request to enable interrupt
605 *
606 * @param[in] device Device asking for interrupts
607 * @param[in] inum Interrupt number
608 * @return Error code.
609 */
610int hcd_ddf_enable_interrupt(ddf_dev_t *device, int inum)
611{
612 async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
613 if (parent_sess == NULL)
614 return EIO;
615
616 return hw_res_enable_interrupt(parent_sess, inum);
617}
618
619//TODO: Cache parent session in HCD
620int hcd_ddf_get_registers(ddf_dev_t *device, hw_res_list_parsed_t *hw_res)
621{
622 async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
623 if (parent_sess == NULL)
624 return EIO;
625
626 hw_res_list_parsed_init(hw_res);
627 const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
628 if (ret != EOK)
629 hw_res_list_parsed_clean(hw_res);
630 return ret;
631}
632
633// TODO: move this someplace else
634static inline void irq_code_clean(irq_code_t *code)
635{
636 if (code) {
637 free(code->ranges);
638 free(code->cmds);
639 code->ranges = NULL;
640 code->cmds = NULL;
641 code->rangecount = 0;
642 code->cmdcount = 0;
643 }
644}
645
646/** Register interrupt handler
647 *
648 * @param[in] device Host controller DDF device
649 * @param[in] regs Register range
650 * @param[in] irq Interrupt number
651 * @paran[in] handler Interrupt handler
652 * @param[in] gen_irq_code IRQ code generator.
653 *
654 * @return IRQ capability handle on success.
655 * @return Negative error code.
656 */
657int hcd_ddf_setup_interrupts(ddf_dev_t *device,
658 const hw_res_list_parsed_t *hw_res,
659 interrupt_handler_t handler,
660 irq_code_gen_t gen_irq_code)
661{
662 assert(device);
663
664 hcd_t *hcd = dev_to_hcd(device);
665
666 if (!handler || !gen_irq_code)
667 return ENOTSUP;
668
669 irq_code_t irq_code = {0};
670
671 const int irq = gen_irq_code(&irq_code, hcd, hw_res);
672 if (irq < 0) {
673 usb_log_error("Failed to generate IRQ code: %s.\n",
674 str_error(irq));
675 return irq;
676 }
677
678 /* Register handler to avoid interrupt lockup */
679 const int irq_cap = register_interrupt_handler(device, irq, handler,
680 &irq_code);
681 irq_code_clean(&irq_code);
682 if (irq_cap < 0) {
683 usb_log_error("Failed to register interrupt handler: %s.\n",
684 str_error(irq_cap));
685 return irq_cap;
686 }
687
688 /* Enable interrupts */
689 int ret = hcd_ddf_enable_interrupt(device, irq);
690 if (ret != EOK) {
691 usb_log_error("Failed to enable interrupts: %s.\n",
692 str_error(ret));
693 unregister_interrupt_handler(device, irq_cap);
694 return ret;
695 }
696 return irq_cap;
697}
698
699/** IRQ handling callback, forward status from call to diver structure.
700 *
701 * @param[in] dev DDF instance of the device to use.
702 * @param[in] iid (Unused).
703 * @param[in] call Pointer to the call from kernel.
704 */
705void ddf_hcd_gen_irq_handler(ipc_callid_t iid, ipc_call_t *call, ddf_dev_t *dev)
706{
707 assert(dev);
708 hcd_t *hcd = dev_to_hcd(dev);
709 if (!hcd || !hcd->ops.irq_hook) {
710 usb_log_error("Interrupt on not yet initialized device.\n");
711 return;
712 }
713 const uint32_t status = IPC_GET_ARG1(*call);
714 hcd->ops.irq_hook(hcd, status);
715}
716
717static int interrupt_polling(void *arg)
718{
719 hcd_t *hcd = arg;
720 assert(hcd);
721 if (!hcd->ops.status_hook || !hcd->ops.irq_hook)
722 return ENOTSUP;
723 uint32_t status = 0;
724 while (hcd->ops.status_hook(hcd, &status) == EOK) {
725 hcd->ops.irq_hook(hcd, status);
726 status = 0;
727 /* We should wait 1 frame - 1ms here, but this polling is a
728 * lame crutch anyway so don't hog the system. 10ms is still
729 * good enough for emergency mode */
730 async_usleep(10000);
731 }
732 return EOK;
733}
734
735/** Initialize hc and rh DDF structures and their respective drivers.
736 *
737 * @param device DDF instance of the device to use
738 * @param speed Maximum supported speed
739 * @param bw Available bandwidth (arbitrary units)
740 * @param bw_count Bandwidth computing function
741 * @param irq_handler IRQ handling function
742 * @param gen_irq_code Function to generate IRQ pseudocode
743 * (it needs to return used irq number)
744 * @param driver_init Function to initialize HC driver
745 * @param driver_fini Function to cleanup HC driver
746 * @return Error code
747 *
748 * This function does all the preparatory work for hc and rh drivers:
749 * - gets device's hw resources
750 * - attempts to enable interrupts
751 * - registers interrupt handler
752 * - calls driver specific initialization
753 * - registers root hub
754 */
755int hcd_ddf_add_hc(ddf_dev_t *device, const ddf_hc_driver_t *driver)
756{
757 assert(driver);
758
759 int ret = EOK;
760
761 hw_res_list_parsed_t hw_res;
762 ret = hcd_ddf_get_registers(device, &hw_res);
763 if (ret != EOK) {
764 usb_log_error("Failed to get register memory addresses "
765 "for `%s': %s.\n", ddf_dev_get_name(device),
766 str_error(ret));
767 return ret;
768 }
769
770 ret = hcd_ddf_setup_hc(device);
771 if (ret != EOK) {
772 usb_log_error("Failed to setup generic HCD.\n");
773 goto err_hw_res;
774 }
775
776 hcd_t *hcd = dev_to_hcd(device);
777
778 if (driver->init)
779 ret = driver->init(hcd, &hw_res, device);
780 if (ret != EOK) {
781 usb_log_error("Failed to init HCD.\n");
782 goto err_hcd;
783 }
784
785 /* Setup interrupts */
786 interrupt_handler_t *irq_handler =
787 driver->irq_handler ? driver->irq_handler : ddf_hcd_gen_irq_handler;
788 const int irq_cap = hcd_ddf_setup_interrupts(device, &hw_res,
789 irq_handler, driver->irq_code_gen);
790 bool irqs_enabled = !(irq_cap < 0);
791 if (irqs_enabled) {
792 usb_log_debug("Hw interrupts enabled.\n");
793 }
794
795 /* Claim the device from BIOS */
796 if (driver->claim)
797 ret = driver->claim(hcd, device);
798 if (ret != EOK) {
799 usb_log_error("Failed to claim `%s' for driver `%s': %s",
800 ddf_dev_get_name(device), driver->name, str_error(ret));
801 goto err_irq;
802 }
803
804 /* Start hw driver */
805 if (driver->start)
806 ret = driver->start(hcd, irqs_enabled);
807 if (ret != EOK) {
808 usb_log_error("Failed to start HCD: %s.\n", str_error(ret));
809 goto err_irq;
810 }
811
812 /* Need working irq replacement to setup root hub */
813 if (!irqs_enabled && hcd->ops.status_hook) {
814 hcd->polling_fibril = fibril_create(interrupt_polling, hcd);
815 if (hcd->polling_fibril == 0) {
816 usb_log_error("Failed to create polling fibril\n");
817 ret = ENOMEM;
818 goto err_started;
819 }
820 fibril_add_ready(hcd->polling_fibril);
821 usb_log_warning("Failed to enable interrupts: %s."
822 " Falling back to polling.\n", str_error(irq_cap));
823 }
824
825 /*
826 * Creating root hub registers a new USB device so HC
827 * needs to be ready at this time.
828 */
829 if (driver->setup_root_hub)
830 ret = driver->setup_root_hub(hcd, device);
831 if (ret != EOK) {
832 usb_log_error("Failed to setup HC root hub: %s.\n",
833 str_error(ret));
834 goto err_polling;
835 }
836
837 usb_log_info("Controlling new `%s' device `%s'.\n",
838 driver->name, ddf_dev_get_name(device));
839 return EOK;
840
841err_polling:
842 // TODO: Stop the polling fibril (refactor the interrupt_polling func)
843 //
844err_started:
845 if (driver->stop)
846 driver->stop(hcd);
847err_irq:
848 unregister_interrupt_handler(device, irq_cap);
849 if (driver->fini)
850 driver->fini(hcd);
851err_hcd:
852 hcd_ddf_clean_hc(device);
853err_hw_res:
854 hw_res_list_parsed_clean(&hw_res);
855 return ret;
856}
857
858/**
859 * @}
860 */
Note: See TracBrowser for help on using the repository browser.