source: mainline/uspace/drv/bus/usb/vhc/main.c@ 01eeaaf

lfn serial ticket/834-toolchain-update topic/fix-logger-deadlock topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 01eeaaf was 01eeaaf, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

vhc: Port to libusbhost.

Device removal works OK.
Tested on vuh boot and vuh lw1.

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
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 drvusbvhc
30 * @{
31 */
32/** @file
33 * Virtual host controller.
34 */
35
36#include <loc.h>
37#include <async.h>
38#include <unistd.h>
39#include <stdlib.h>
40#include <sysinfo.h>
41#include <stdio.h>
42#include <errno.h>
43#include <str_error.h>
44#include <ddf/driver.h>
45
46#include <usb/host/ddf_helpers.h>
47
48#include <usb/usb.h>
49#include <usb/ddfiface.h>
50#include <usb_iface.h>
51#include "vhcd.h"
52#include "hub.h"
53#include "conn.h"
54
55static ddf_dev_ops_t vhc_ops = {
56#if 0
57 .interfaces[USBHC_DEV_IFACE] = &vhc_iface,
58 .interfaces[USB_DEV_IFACE] = &vhc_usb_iface,
59#endif
60 .close = on_client_close,
61 .default_handler = default_connection_handler
62};
63
64static int vhc_control_node(ddf_dev_t *dev, ddf_fun_t **fun)
65{
66 assert(dev);
67 assert(fun);
68
69 *fun = ddf_fun_create(dev, fun_exposed, "ctl");
70 if (!*fun)
71 return ENOMEM;
72
73 vhc_data_t *vhc = ddf_fun_data_alloc(*fun, sizeof(vhc_data_t));
74 if (!vhc) {
75 ddf_fun_destroy(*fun);
76 }
77 ddf_fun_set_ops(*fun, &vhc_ops);
78 const int ret = ddf_fun_bind(*fun);
79 if (ret != EOK) {
80 ddf_fun_destroy(*fun);
81 *fun = NULL;
82 return ret;
83 }
84 vhc_data_init(vhc);
85 // TODO: This limits us to single vhc instance.
86 virthub_init(&virtual_hub_device);
87 vhc->hub = &virtual_hub_device;
88 return EOK;
89}
90
91
92static int vhc_dev_add(ddf_dev_t *dev)
93{
94 /* Initialize virtual structure */
95 ddf_fun_t *ctl_fun = NULL;
96 int ret = vhc_control_node(dev, &ctl_fun);
97 if (ret != EOK) {
98 usb_log_error("Failed to setup control node.\n");
99 return ret;
100 }
101 vhc_data_t *data = ddf_fun_data_get(ctl_fun);
102
103 /* Initialize generic structures */
104 ret = hcd_ddf_setup_device(dev, NULL, USB_SPEED_FULL,
105 BANDWIDTH_AVAILABLE_USB11, bandwidth_count_usb11);
106 if (ret != EOK) {
107 usb_log_error("Failed to init HCD structures: %s.\n",
108 str_error(ret));
109 free(data);
110 return ret;
111 }
112
113 hcd_set_implementation(dev_to_hcd(dev), data, vhc_schedule, NULL, NULL);
114
115 /* Add virtual hub device */
116 usb_address_t address = 1;
117 ret = vhc_virtdev_plug_hub(data, data->hub, NULL, address);
118 if (ret != EOK) {
119 usb_log_error("Failed to plug root hub: %s.\n", str_error(ret));
120 free(data);
121 return ret;
122 }
123
124 // TODO fix the address hack
125 ret = hcd_ddf_setup_hub(dev, &address);
126 if (ret != EOK) {
127 usb_log_error("Failed to init VHC root hub: %s\n",
128 str_error(ret));
129 // TODO do something here...
130 }
131
132 return ret;
133#if 0
134 static int vhc_count = 0;
135 int rc;
136
137 if (vhc_count > 0) {
138 return ELIMIT;
139 }
140
141 vhc_data_t *data = ddf_dev_data_alloc(dev, sizeof(vhc_data_t));
142 if (data == NULL) {
143 usb_log_fatal("Failed to allocate memory.\n");
144 return ENOMEM;
145 }
146 data->magic = 0xDEADBEEF;
147 rc = usb_endpoint_manager_init(&data->ep_manager, (size_t) -1,
148 bandwidth_count_usb11);
149 if (rc != EOK) {
150 usb_log_fatal("Failed to initialize endpoint manager.\n");
151 free(data);
152 return rc;
153 }
154 usb_device_manager_init(&data->dev_manager, USB_SPEED_MAX);
155
156 ddf_fun_t *hc = ddf_fun_create(dev, fun_exposed, "hc");
157 if (hc == NULL) {
158 usb_log_fatal("Failed to create device function.\n");
159 free(data);
160 return ENOMEM;
161 }
162
163 ddf_fun_set_ops(hc, &vhc_ops);
164 list_initialize(&data->devices);
165 fibril_mutex_initialize(&data->guard);
166 data->hub = &virtual_hub_device;
167 data->hc_fun = hc;
168
169 rc = ddf_fun_bind(hc);
170 if (rc != EOK) {
171 usb_log_fatal("Failed to bind HC function: %s.\n",
172 str_error(rc));
173 free(data);
174 return rc;
175 }
176
177 rc = ddf_fun_add_to_category(hc, USB_HC_CATEGORY);
178 if (rc != EOK) {
179 usb_log_fatal("Failed to add function to HC class: %s.\n",
180 str_error(rc));
181 free(data);
182 return rc;
183 }
184
185 virtual_hub_device_init(hc);
186
187 usb_log_info("Virtual USB host controller ready (dev %zu, hc %zu).\n",
188 (size_t) ddf_dev_get_handle(dev), (size_t) ddf_fun_get_handle(hc));
189
190 rc = vhc_virtdev_plug_hub(data, data->hub, NULL);
191 if (rc != EOK) {
192 usb_log_fatal("Failed to plug root hub: %s.\n", str_error(rc));
193 free(data);
194 return rc;
195 }
196
197 return EOK;
198#endif
199}
200
201static driver_ops_t vhc_driver_ops = {
202 .dev_add = vhc_dev_add,
203};
204
205static driver_t vhc_driver = {
206 .name = NAME,
207 .driver_ops = &vhc_driver_ops
208};
209
210
211int main(int argc, char * argv[])
212{
213 log_init(NAME);
214
215 printf(NAME ": virtual USB host controller driver.\n");
216
217 return ddf_driver_main(&vhc_driver);
218}
219
220/**
221 * @}
222 */
Note: See TracBrowser for help on using the repository browser.