[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 |
|
---|
[355f7c2] | 47 |
|
---|
[eb1a2f4] | 48 | static ddf_dev_ops_t vhc_ops = {
|
---|
[6967c14] | 49 | .close = on_client_close,
|
---|
[4317827] | 50 | .default_handler = default_connection_handler
|
---|
| 51 | };
|
---|
[e27595b] | 52 |
|
---|
[01eeaaf] | 53 | static int vhc_control_node(ddf_dev_t *dev, ddf_fun_t **fun)
|
---|
| 54 | {
|
---|
| 55 | assert(dev);
|
---|
| 56 | assert(fun);
|
---|
| 57 |
|
---|
| 58 | *fun = ddf_fun_create(dev, fun_exposed, "ctl");
|
---|
| 59 | if (!*fun)
|
---|
| 60 | return ENOMEM;
|
---|
| 61 |
|
---|
| 62 | vhc_data_t *vhc = ddf_fun_data_alloc(*fun, sizeof(vhc_data_t));
|
---|
| 63 | if (!vhc) {
|
---|
| 64 | ddf_fun_destroy(*fun);
|
---|
| 65 | }
|
---|
| 66 | ddf_fun_set_ops(*fun, &vhc_ops);
|
---|
| 67 | const int ret = ddf_fun_bind(*fun);
|
---|
| 68 | if (ret != EOK) {
|
---|
| 69 | ddf_fun_destroy(*fun);
|
---|
| 70 | *fun = NULL;
|
---|
| 71 | return ret;
|
---|
| 72 | }
|
---|
[f5f0cfb] | 73 | vhc_init(vhc);
|
---|
[01eeaaf] | 74 | return EOK;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[0c0f823b] | 77 | static int vhc_dev_add(ddf_dev_t *dev)
|
---|
[355f7c2] | 78 | {
|
---|
[01eeaaf] | 79 | /* Initialize virtual structure */
|
---|
| 80 | ddf_fun_t *ctl_fun = NULL;
|
---|
| 81 | int ret = vhc_control_node(dev, &ctl_fun);
|
---|
| 82 | if (ret != EOK) {
|
---|
| 83 | usb_log_error("Failed to setup control node.\n");
|
---|
| 84 | return ret;
|
---|
| 85 | }
|
---|
| 86 | vhc_data_t *data = ddf_fun_data_get(ctl_fun);
|
---|
| 87 |
|
---|
| 88 | /* Initialize generic structures */
|
---|
| 89 | ret = hcd_ddf_setup_device(dev, NULL, USB_SPEED_FULL,
|
---|
| 90 | BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
|
---|
| 91 | if (ret != EOK) {
|
---|
| 92 | usb_log_error("Failed to init HCD structures: %s.\n",
|
---|
| 93 | str_error(ret));
|
---|
[0e97b4b5] | 94 | ddf_fun_destroy(ctl_fun);
|
---|
[01eeaaf] | 95 | return ret;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | hcd_set_implementation(dev_to_hcd(dev), data, vhc_schedule, NULL, NULL);
|
---|
| 99 |
|
---|
| 100 | /* Add virtual hub device */
|
---|
[0e97b4b5] | 101 | ret = vhc_virtdev_plug_hub(data, &data->hub, NULL, 0);
|
---|
[01eeaaf] | 102 | if (ret != EOK) {
|
---|
| 103 | usb_log_error("Failed to plug root hub: %s.\n", str_error(ret));
|
---|
[0e97b4b5] | 104 | ddf_fun_destroy(ctl_fun);
|
---|
[01eeaaf] | 105 | return ret;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[0e97b4b5] | 108 | ret = hcd_ddf_setup_root_hub(dev, USB_SPEED_FULL);
|
---|
[01eeaaf] | 109 | if (ret != EOK) {
|
---|
| 110 | usb_log_error("Failed to init VHC root hub: %s\n",
|
---|
| 111 | str_error(ret));
|
---|
| 112 | // TODO do something here...
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | return ret;
|
---|
[355f7c2] | 116 | }
|
---|
| 117 |
|
---|
[4317827] | 118 | static driver_ops_t vhc_driver_ops = {
|
---|
[0c0f823b] | 119 | .dev_add = vhc_dev_add,
|
---|
[4317827] | 120 | };
|
---|
| 121 |
|
---|
| 122 | static driver_t vhc_driver = {
|
---|
[63b4f90] | 123 | .name = NAME,
|
---|
[4317827] | 124 | .driver_ops = &vhc_driver_ops
|
---|
[63b4f90] | 125 | };
|
---|
| 126 |
|
---|
| 127 | int main(int argc, char * argv[])
|
---|
[01eeaaf] | 128 | {
|
---|
[920d0fc] | 129 | log_init(NAME);
|
---|
[e63a4e1] | 130 | printf(NAME ": virtual USB host controller driver.\n");
|
---|
[e27595b] | 131 |
|
---|
[eb1a2f4] | 132 | return ddf_driver_main(&vhc_driver);
|
---|
[355f7c2] | 133 | }
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * @}
|
---|
| 137 | */
|
---|