1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky
|
---|
3 | * Copyright (c) 2018 Ondrej Hlavaty
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup drvusbvhc
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | * Virtual host controller.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <str_error.h>
|
---|
40 | #include <ddf/driver.h>
|
---|
41 |
|
---|
42 | #include <usb/host/ddf_helpers.h>
|
---|
43 | #include <usb/host/utility.h>
|
---|
44 |
|
---|
45 | #include <usb/debug.h>
|
---|
46 | #include "vhcd.h"
|
---|
47 |
|
---|
48 | static ddf_dev_ops_t vhc_ops = {
|
---|
49 | .close = on_client_close,
|
---|
50 | .default_handler = default_connection_handler
|
---|
51 | };
|
---|
52 |
|
---|
53 | static errno_t 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, "virtual");
|
---|
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 errno_t ret = ddf_fun_bind(*fun);
|
---|
68 | if (ret != EOK) {
|
---|
69 | ddf_fun_destroy(*fun);
|
---|
70 | *fun = NULL;
|
---|
71 | return ret;
|
---|
72 | }
|
---|
73 | return EOK;
|
---|
74 | }
|
---|
75 |
|
---|
76 | static errno_t vhc_dev_add(ddf_dev_t *dev)
|
---|
77 | {
|
---|
78 | /* Initialize generic structures */
|
---|
79 | errno_t ret = hcd_ddf_setup_hc(dev, sizeof(vhc_data_t));
|
---|
80 | if (ret != EOK) {
|
---|
81 | usb_log_error("Failed to init HCD structures: %s.",
|
---|
82 | str_error(ret));
|
---|
83 | return ret;
|
---|
84 | }
|
---|
85 | vhc_data_t *vhc = ddf_dev_data_get(dev);
|
---|
86 | vhc_init(vhc);
|
---|
87 |
|
---|
88 | hc_device_setup(&vhc->base, (bus_t *) &vhc->bus);
|
---|
89 |
|
---|
90 | /* Initialize virtual structure */
|
---|
91 | ddf_fun_t *ctl_fun = NULL;
|
---|
92 | ret = vhc_control_node(dev, &ctl_fun);
|
---|
93 | if (ret != EOK) {
|
---|
94 | usb_log_error("Failed to setup control node.");
|
---|
95 | return ret;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /* Add virtual hub device */
|
---|
99 | ret = vhc_virtdev_plug_hub(vhc, &vhc->hub, NULL, 0);
|
---|
100 | if (ret != EOK) {
|
---|
101 | usb_log_error("Failed to plug root hub: %s.", str_error(ret));
|
---|
102 | ddf_fun_destroy(ctl_fun);
|
---|
103 | return ret;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Creating root hub registers a new USB device so HC
|
---|
108 | * needs to be ready at this time.
|
---|
109 | */
|
---|
110 | ret = hc_setup_virtual_root_hub(&vhc->base, USB_SPEED_HIGH);
|
---|
111 | if (ret != EOK) {
|
---|
112 | usb_log_error("Failed to init VHC root hub: %s",
|
---|
113 | str_error(ret));
|
---|
114 | // TODO do something here...
|
---|
115 | }
|
---|
116 |
|
---|
117 | return ret;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static driver_ops_t vhc_driver_ops = {
|
---|
121 | .dev_add = vhc_dev_add,
|
---|
122 | };
|
---|
123 |
|
---|
124 | static driver_t vhc_driver = {
|
---|
125 | .name = NAME,
|
---|
126 | .driver_ops = &vhc_driver_ops
|
---|
127 | };
|
---|
128 |
|
---|
129 | int main(int argc, char *argv[])
|
---|
130 | {
|
---|
131 | log_init(NAME);
|
---|
132 | printf(NAME ": virtual USB host controller driver.\n");
|
---|
133 |
|
---|
134 | return ddf_driver_main(&vhc_driver);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * @}
|
---|
139 | */
|
---|