| [355f7c2] | 1 | /*
|
|---|
| [6cb58e6] | 2 | * Copyright (c) 2011 Vojtech Horky
|
|---|
| [355f7c2] | 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 |
|
|---|
| [bd8c753d] | 29 | /** @addtogroup drvusbvhc
|
|---|
| [355f7c2] | 30 | * @{
|
|---|
| [79ae36dd] | 31 | */
|
|---|
| [355f7c2] | 32 | /** @file
|
|---|
| [6cb58e6] | 33 | * Virtual host controller.
|
|---|
| [355f7c2] | 34 | */
|
|---|
| [63b4f90] | 35 |
|
|---|
| 36 | #include <stdio.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <str_error.h>
|
|---|
| [eb1a2f4] | 39 | #include <ddf/driver.h>
|
|---|
| [355f7c2] | 40 |
|
|---|
| [01eeaaf] | 41 | #include <usb/host/ddf_helpers.h>
|
|---|
| 42 |
|
|---|
| [357a302] | 43 | #include <usb/ddfiface.h>
|
|---|
| [f6577d9] | 44 | #include <usb/debug.h>
|
|---|
| [355f7c2] | 45 | #include "vhcd.h"
|
|---|
| [f6577d9] | 46 | #include "hub/hub.h"
|
|---|
| 47 | #include "hub/virthub.h"
|
|---|
| 48 |
|
|---|
| 49 | static usbvirt_device_t virtual_hub_device = {
|
|---|
| 50 | .name = "root hub",
|
|---|
| 51 | .ops = &hub_ops,
|
|---|
| 52 | .address = 0
|
|---|
| 53 | };
|
|---|
| [355f7c2] | 54 |
|
|---|
| [eb1a2f4] | 55 | static ddf_dev_ops_t vhc_ops = {
|
|---|
| [6967c14] | 56 | .close = on_client_close,
|
|---|
| [4317827] | 57 | .default_handler = default_connection_handler
|
|---|
| 58 | };
|
|---|
| [e27595b] | 59 |
|
|---|
| [01eeaaf] | 60 | static int vhc_control_node(ddf_dev_t *dev, ddf_fun_t **fun)
|
|---|
| 61 | {
|
|---|
| 62 | assert(dev);
|
|---|
| 63 | assert(fun);
|
|---|
| 64 |
|
|---|
| 65 | *fun = ddf_fun_create(dev, fun_exposed, "ctl");
|
|---|
| 66 | if (!*fun)
|
|---|
| 67 | return ENOMEM;
|
|---|
| 68 |
|
|---|
| 69 | vhc_data_t *vhc = ddf_fun_data_alloc(*fun, sizeof(vhc_data_t));
|
|---|
| 70 | if (!vhc) {
|
|---|
| 71 | ddf_fun_destroy(*fun);
|
|---|
| 72 | }
|
|---|
| 73 | ddf_fun_set_ops(*fun, &vhc_ops);
|
|---|
| 74 | const int ret = ddf_fun_bind(*fun);
|
|---|
| 75 | if (ret != EOK) {
|
|---|
| 76 | ddf_fun_destroy(*fun);
|
|---|
| 77 | *fun = NULL;
|
|---|
| 78 | return ret;
|
|---|
| 79 | }
|
|---|
| 80 | vhc_data_init(vhc);
|
|---|
| 81 | // TODO: This limits us to single vhc instance.
|
|---|
| 82 | virthub_init(&virtual_hub_device);
|
|---|
| 83 | vhc->hub = &virtual_hub_device;
|
|---|
| 84 | return EOK;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| [0c0f823b] | 88 | static int vhc_dev_add(ddf_dev_t *dev)
|
|---|
| [355f7c2] | 89 | {
|
|---|
| [01eeaaf] | 90 | /* Initialize virtual structure */
|
|---|
| 91 | ddf_fun_t *ctl_fun = NULL;
|
|---|
| 92 | int ret = vhc_control_node(dev, &ctl_fun);
|
|---|
| 93 | if (ret != EOK) {
|
|---|
| 94 | usb_log_error("Failed to setup control node.\n");
|
|---|
| 95 | return ret;
|
|---|
| 96 | }
|
|---|
| 97 | vhc_data_t *data = ddf_fun_data_get(ctl_fun);
|
|---|
| 98 |
|
|---|
| 99 | /* Initialize generic structures */
|
|---|
| 100 | ret = hcd_ddf_setup_device(dev, NULL, USB_SPEED_FULL,
|
|---|
| 101 | BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
|
|---|
| 102 | if (ret != EOK) {
|
|---|
| 103 | usb_log_error("Failed to init HCD structures: %s.\n",
|
|---|
| 104 | str_error(ret));
|
|---|
| 105 | free(data);
|
|---|
| 106 | return ret;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | hcd_set_implementation(dev_to_hcd(dev), data, vhc_schedule, NULL, NULL);
|
|---|
| 110 |
|
|---|
| 111 | /* Add virtual hub device */
|
|---|
| 112 | usb_address_t address = 1;
|
|---|
| 113 | ret = vhc_virtdev_plug_hub(data, data->hub, NULL, address);
|
|---|
| 114 | if (ret != EOK) {
|
|---|
| 115 | usb_log_error("Failed to plug root hub: %s.\n", str_error(ret));
|
|---|
| 116 | free(data);
|
|---|
| 117 | return ret;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | // TODO fix the address hack
|
|---|
| 121 | ret = hcd_ddf_setup_hub(dev, &address);
|
|---|
| 122 | if (ret != EOK) {
|
|---|
| 123 | usb_log_error("Failed to init VHC root hub: %s\n",
|
|---|
| 124 | str_error(ret));
|
|---|
| 125 | // TODO do something here...
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | return ret;
|
|---|
| [355f7c2] | 129 | }
|
|---|
| 130 |
|
|---|
| [4317827] | 131 | static driver_ops_t vhc_driver_ops = {
|
|---|
| [0c0f823b] | 132 | .dev_add = vhc_dev_add,
|
|---|
| [4317827] | 133 | };
|
|---|
| 134 |
|
|---|
| 135 | static driver_t vhc_driver = {
|
|---|
| [63b4f90] | 136 | .name = NAME,
|
|---|
| [4317827] | 137 | .driver_ops = &vhc_driver_ops
|
|---|
| [63b4f90] | 138 | };
|
|---|
| 139 |
|
|---|
| 140 | int main(int argc, char * argv[])
|
|---|
| [01eeaaf] | 141 | {
|
|---|
| [920d0fc] | 142 | log_init(NAME);
|
|---|
| [e63a4e1] | 143 | printf(NAME ": virtual USB host controller driver.\n");
|
|---|
| [e27595b] | 144 |
|
|---|
| [eb1a2f4] | 145 | return ddf_driver_main(&vhc_driver);
|
|---|
| [355f7c2] | 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * @}
|
|---|
| 150 | */
|
|---|