source: mainline/uspace/drv/vhc/hub/virthub.c@ 0f21c0c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0f21c0c was 0f21c0c, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

Removal of API that use phones directly

  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 * Copyright (c) 2010 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 * @brief
34 */
35#include <usb/classes/classes.h>
36#include <usbvirt/hub.h>
37#include <usbvirt/device.h>
38#include <assert.h>
39#include <errno.h>
40#include <str_error.h>
41#include <stdlib.h>
42#include <driver.h>
43
44#include "virthub.h"
45#include "hub.h"
46
47
48/** Standard device descriptor. */
49usb_standard_device_descriptor_t std_device_descriptor = {
50 .length = sizeof(usb_standard_device_descriptor_t),
51 .descriptor_type = USB_DESCTYPE_DEVICE,
52 .usb_spec_version = 0x110,
53 .device_class = USB_CLASS_HUB,
54 .device_subclass = 0,
55 .device_protocol = 0,
56 .max_packet_size = 64,
57 .configuration_count = 1
58};
59
60/** Standard interface descriptor. */
61usb_standard_interface_descriptor_t std_interface_descriptor = {
62 .length = sizeof(usb_standard_interface_descriptor_t),
63 .descriptor_type = USB_DESCTYPE_INTERFACE,
64 .interface_number = 0,
65 .alternate_setting = 0,
66 .endpoint_count = 1,
67 .interface_class = USB_CLASS_HUB,
68 .interface_subclass = 0,
69 .interface_protocol = 0,
70 .str_interface = 0
71};
72
73/** Hub descriptor. */
74hub_descriptor_t hub_descriptor = {
75 .length = sizeof(hub_descriptor_t),
76 .type = USB_DESCTYPE_HUB,
77 .port_count = HUB_PORT_COUNT,
78 .characteristics = 0,
79 .power_on_warm_up = 50, /* Huh? */
80 .max_current = 100, /* Huh again. */
81 .removable_device = { 0 },
82 .port_power = { 0xFF }
83};
84
85/** Endpoint descriptor. */
86usb_standard_endpoint_descriptor_t endpoint_descriptor = {
87 .length = sizeof(usb_standard_endpoint_descriptor_t),
88 .descriptor_type = USB_DESCTYPE_ENDPOINT,
89 .endpoint_address = HUB_STATUS_CHANGE_PIPE | 128,
90 .attributes = USB_TRANSFER_INTERRUPT,
91 .max_packet_size = 8,
92 .poll_interval = 0xFF
93};
94
95/** Standard configuration descriptor. */
96usb_standard_configuration_descriptor_t std_configuration_descriptor = {
97 .length = sizeof(usb_standard_configuration_descriptor_t),
98 .descriptor_type = USB_DESCTYPE_CONFIGURATION,
99 .total_length =
100 sizeof(usb_standard_configuration_descriptor_t)
101 + sizeof(std_interface_descriptor)
102 + sizeof(hub_descriptor)
103 + sizeof(endpoint_descriptor)
104 ,
105 .interface_count = 1,
106 .configuration_number = HUB_CONFIGURATION_ID,
107 .str_configuration = 0,
108 .attributes = 128, /* denotes bus-powered device */
109 .max_power = 50
110};
111
112/** All hub configuration descriptors. */
113static usbvirt_device_configuration_extras_t extra_descriptors[] = {
114 {
115 .data = (uint8_t *) &std_interface_descriptor,
116 .length = sizeof(std_interface_descriptor)
117 },
118 {
119 .data = (uint8_t *) &hub_descriptor,
120 .length = sizeof(hub_descriptor)
121 },
122 {
123 .data = (uint8_t *) &endpoint_descriptor,
124 .length = sizeof(endpoint_descriptor)
125 }
126};
127
128/** Hub configuration. */
129usbvirt_device_configuration_t configuration = {
130 .descriptor = &std_configuration_descriptor,
131 .extra = extra_descriptors,
132 .extra_count = sizeof(extra_descriptors)/sizeof(extra_descriptors[0])
133};
134
135/** Hub standard descriptors. */
136usbvirt_descriptors_t descriptors = {
137 .device = &std_device_descriptor,
138 .configuration = &configuration,
139 .configuration_count = 1,
140};
141
142/** Initializes virtual hub device.
143 *
144 * @param dev Virtual USB device backend.
145 * @return Error code.
146 */
147int virthub_init(usbvirt_device_t *dev)
148{
149 if (dev == NULL) {
150 return EBADMEM;
151 }
152 dev->ops = &hub_ops;
153 dev->descriptors = &descriptors;
154 dev->lib_debug_level = 0;
155 dev->lib_debug_enabled_tags = USBVIRT_DEBUGTAG_ALL;
156
157 hub_t *hub = malloc(sizeof(hub_t));
158 if (hub == NULL) {
159 return ENOMEM;
160 }
161
162 hub_init(hub);
163 dev->device_data = hub;
164
165 int rc;
166#ifdef STANDALONE_HUB
167 dev->name = "hub";
168 rc = usbvirt_connect(dev);
169#else
170 rc = usbvirt_connect_local(dev);
171#endif
172
173 return rc;
174}
175
176/** Connect a device to a virtual hub.
177 *
178 * @param dev Virtual device representing the hub.
179 * @param conn Device to be connected.
180 * @return Port device was connected to.
181 */
182int virthub_connect_device(usbvirt_device_t *dev, virtdev_connection_t *conn)
183{
184 assert(dev != NULL);
185 assert(conn != NULL);
186
187 hub_t *hub = (hub_t *) dev->device_data;
188
189 hub_acquire(hub);
190 size_t port = hub_connect_device(hub, conn);
191 hub_release(hub);
192
193 return port;
194}
195
196/** Disconnect a device from a virtual hub.
197 *
198 * @param dev Virtual device representing the hub.
199 * @param conn Device to be disconnected.
200 * @return Error code.
201 */
202int virthub_disconnect_device(usbvirt_device_t *dev, virtdev_connection_t *conn)
203{
204 assert(dev != NULL);
205 assert(conn != NULL);
206
207 hub_t *hub = (hub_t *) dev->device_data;
208
209 hub_acquire(hub);
210 hub_disconnect_device(hub, conn);
211 hub_release(hub);
212
213 return ENOTSUP;
214}
215
216/** Whether trafic is propagated to given device.
217 *
218 * @param dev Virtual device representing the hub.
219 * @param conn Connected device.
220 * @return Whether port is signalling to the device.
221 */
222bool virthub_is_device_enabled(usbvirt_device_t *dev, virtdev_connection_t *conn)
223{
224 assert(dev != NULL);
225 assert(conn != NULL);
226
227 hub_t *hub = (hub_t *) dev->device_data;
228
229 hub_acquire(hub);
230
231 hub_port_state_t state = HUB_PORT_STATE_UNKNOWN;
232 size_t port = hub_find_device(hub, conn);
233 if (port != (size_t) -1) {
234 state = hub_get_port_state(hub, port);
235 }
236 hub_release(hub);
237
238 return state == HUB_PORT_STATE_ENABLED;
239}
240
241/** Format status of a virtual hub.
242 *
243 * @param dev Virtual device representing the hub.
244 * @param[out] status Hub status information.
245 * @param[in] len Size of the @p status buffer.
246 */
247void virthub_get_status(usbvirt_device_t *dev, char *status, size_t len)
248{
249 assert(dev != NULL);
250 if (len == 0) {
251 return;
252 }
253
254 hub_t *hub = (hub_t *) dev->device_data;
255
256 char port_status[HUB_PORT_COUNT + 1];
257
258 size_t i;
259 for (i = 0; i < HUB_PORT_COUNT; i++) {
260 port_status[i] = hub_port_state_to_char(
261 hub_get_port_state(hub, i));
262 }
263 port_status[HUB_PORT_COUNT] = 0;
264
265 snprintf(status, len, "vhub:%s", port_status);
266}
267
268/**
269 * @}
270 */
Note: See TracBrowser for help on using the repository browser.