| 1 | #include <assert.h>
|
|---|
| 2 | #include <errno.h>
|
|---|
| 3 | #include <stdio.h>
|
|---|
| 4 | #include <usb_iface.h>
|
|---|
| 5 |
|
|---|
| 6 | #include "debug.h"
|
|---|
| 7 | #include "root_hub.h"
|
|---|
| 8 | /*----------------------------------------------------------------------------*/
|
|---|
| 9 | static int usb_iface_get_hc_handle(device_t *dev, devman_handle_t *handle)
|
|---|
| 10 | {
|
|---|
| 11 | assert(dev);
|
|---|
| 12 | assert(dev->parent != NULL);
|
|---|
| 13 |
|
|---|
| 14 | device_t *parent = dev->parent;
|
|---|
| 15 |
|
|---|
| 16 | if (parent->ops && parent->ops->interfaces[USB_DEV_IFACE]) {
|
|---|
| 17 | usb_iface_t *usb_iface
|
|---|
| 18 | = (usb_iface_t *) parent->ops->interfaces[USB_DEV_IFACE];
|
|---|
| 19 | assert(usb_iface != NULL);
|
|---|
| 20 | if (usb_iface->get_hc_handle) {
|
|---|
| 21 | int rc = usb_iface->get_hc_handle(parent, handle);
|
|---|
| 22 | return rc;
|
|---|
| 23 | }
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | return ENOTSUP;
|
|---|
| 27 | }
|
|---|
| 28 | /*----------------------------------------------------------------------------*/
|
|---|
| 29 |
|
|---|
| 30 | static usb_iface_t usb_iface = {
|
|---|
| 31 | .get_hc_handle = usb_iface_get_hc_handle
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | static device_ops_t rh_ops = {
|
|---|
| 35 | .interfaces[USB_DEV_IFACE] = &usb_iface
|
|---|
| 36 | };
|
|---|
| 37 |
|
|---|
| 38 | int setup_root_hub(device_t **device, device_t *hc)
|
|---|
| 39 | {
|
|---|
| 40 | assert(device);
|
|---|
| 41 | device_t *hub = create_device();
|
|---|
| 42 | if (!hub) {
|
|---|
| 43 | uhci_print_error("Failed to create root hub device structure.\n");
|
|---|
| 44 | return ENOMEM;
|
|---|
| 45 | }
|
|---|
| 46 | char *name;
|
|---|
| 47 | int ret = asprintf(&name, "UHCI Root Hub");
|
|---|
| 48 | if (ret < 0) {
|
|---|
| 49 | uhci_print_error("Failed to create root hub name.\n");
|
|---|
| 50 | free(hub);
|
|---|
| 51 | return ENOMEM;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | char *match_str;
|
|---|
| 55 | ret = asprintf(&match_str, "usb&uhci&root-hub");
|
|---|
| 56 | if (ret < 0) {
|
|---|
| 57 | uhci_print_error("Failed to create root hub match string.\n");
|
|---|
| 58 | free(hub);
|
|---|
| 59 | free(name);
|
|---|
| 60 | return ENOMEM;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | match_id_t *match_id = create_match_id();
|
|---|
| 64 | if (!match_id) {
|
|---|
| 65 | uhci_print_error("Failed to create root hub match id.\n");
|
|---|
| 66 | free(hub);
|
|---|
| 67 | free(match_str);
|
|---|
| 68 | return ENOMEM;
|
|---|
| 69 | }
|
|---|
| 70 | match_id->id = match_str;
|
|---|
| 71 | match_id->score = 90;
|
|---|
| 72 |
|
|---|
| 73 | add_match_id(&hub->match_ids, match_id);
|
|---|
| 74 | hub->name = name;
|
|---|
| 75 | hub->parent = hc;
|
|---|
| 76 | hub->ops = &rh_ops;
|
|---|
| 77 |
|
|---|
| 78 | *device = hub;
|
|---|
| 79 | return EOK;
|
|---|
| 80 | }
|
|---|