source: mainline/uspace/drv/vhc/hub.c@ 98d06b8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 98d06b8 was 98d06b8, checked in by Matus Dekanek <smekideki@…>, 15 years ago

hub driver:
connect a new device
init a hub

  • Property mode set to 100644
File size: 6.8 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 usb
30 * @{
31 */
32/** @file
33 * @brief Virtual USB hub.
34 */
35#include <usb/classes/classes.h>
36#include <usbvirt/hub.h>
37#include <usbvirt/device.h>
38#include <errno.h>
39#include <stdlib.h>
40
41#include "vhcd.h"
42#include "hub.h"
43#include "hubintern.h"
44
45
46/** Standard device descriptor. */
47usb_standard_device_descriptor_t std_device_descriptor = {
48 .length = sizeof(usb_standard_device_descriptor_t),
49 .descriptor_type = USB_DESCTYPE_DEVICE,
50 .usb_spec_version = 0x110,
51 .device_class = USB_CLASS_HUB,
52 .device_subclass = 0,
53 .device_protocol = 0,
54 .max_packet_size = 64,
55 .configuration_count = 1
56};
57
58/** Standard interface descriptor. */
59usb_standard_interface_descriptor_t std_interface_descriptor = {
60 .length = sizeof(usb_standard_interface_descriptor_t),
61 .descriptor_type = USB_DESCTYPE_INTERFACE,
62 .interface_number = 0,
63 .alternate_setting = 0,
64 .endpoint_count = 1,
65 .interface_class = USB_CLASS_HUB,
66 .interface_subclass = 0,
67 .interface_protocol = 0,
68 .str_interface = 0
69};
70
71hub_descriptor_t hub_descriptor = {
72 .length = sizeof(hub_descriptor_t),
73 .type = USB_DESCTYPE_HUB,
74 .port_count = HUB_PORT_COUNT,
75 .characteristics = 0,
76 .power_on_warm_up = 50, /* Huh? */
77 .max_current = 100, /* Huh again. */
78 .removable_device = { 0 },
79 .port_power = { 0xFF }
80};
81
82/** Endpoint descriptor. */
83usb_standard_endpoint_descriptor_t endpoint_descriptor = {
84 .length = sizeof(usb_standard_endpoint_descriptor_t),
85 .descriptor_type = USB_DESCTYPE_ENDPOINT,
86 .endpoint_address = HUB_STATUS_CHANGE_PIPE | 128,
87 .attributes = USB_TRANSFER_INTERRUPT,
88 .max_packet_size = 8,
89 .poll_interval = 0xFF
90};
91
92/** Standard configuration descriptor. */
93usb_standard_configuration_descriptor_t std_configuration_descriptor = {
94 .length = sizeof(usb_standard_configuration_descriptor_t),
95 .descriptor_type = USB_DESCTYPE_CONFIGURATION,
96 .total_length =
97 sizeof(usb_standard_configuration_descriptor_t)
98 + sizeof(std_interface_descriptor)
99 + sizeof(hub_descriptor)
100 + sizeof(endpoint_descriptor)
101 ,
102 .interface_count = 1,
103 .configuration_number = HUB_CONFIGURATION_ID,
104 .str_configuration = 0,
105 .attributes = 128, /* denotes bus-powered device */
106 .max_power = 50
107};
108
109/** All hub configuration descriptors. */
110static usbvirt_device_configuration_extras_t extra_descriptors[] = {
111 {
112 .data = (uint8_t *) &std_interface_descriptor,
113 .length = sizeof(std_interface_descriptor)
114 },
115 {
116 .data = (uint8_t *) &hub_descriptor,
117 .length = sizeof(hub_descriptor)
118 },
119 {
120 .data = (uint8_t *) &endpoint_descriptor,
121 .length = sizeof(endpoint_descriptor)
122 }
123};
124
125/** Hub configuration. */
126usbvirt_device_configuration_t configuration = {
127 .descriptor = &std_configuration_descriptor,
128 .extra = extra_descriptors,
129 .extra_count = sizeof(extra_descriptors)/sizeof(extra_descriptors[0])
130};
131
132/** Hub standard descriptors. */
133usbvirt_descriptors_t descriptors = {
134 .device = &std_device_descriptor,
135 .configuration = &configuration,
136 .configuration_count = 1,
137};
138
139/** Hub as a virtual device. */
140usbvirt_device_t virthub_dev = {
141 .ops = &hub_ops,
142 .descriptors = &descriptors,
143 .lib_debug_level = 4,
144 .lib_debug_enabled_tags = USBVIRT_DEBUGTAG_ALL
145};
146
147/** Hub device. */
148hub_device_t hub_dev;
149
150/** Initialize virtual hub. */
151void hub_init(void)
152{
153 size_t i;
154 for (i = 0; i < HUB_PORT_COUNT; i++) {
155 hub_port_t *port = &hub_dev.ports[i];
156
157 port->device = NULL;
158 port->state = HUB_PORT_STATE_NOT_CONFIGURED;
159 port->status_change = 0;
160 }
161
162 usbvirt_connect_local(&virthub_dev);
163
164 virthub_dev.address = 7;
165
166 dprintf(1, "virtual hub (%d ports) created", HUB_PORT_COUNT);
167}
168
169/** Connect device to the hub.
170 *
171 * @param device Device to be connected.
172 * @return Port where the device was connected to.
173 */
174size_t hub_add_device(virtdev_connection_t *device)
175{
176 size_t i;
177 for (i = 0; i < HUB_PORT_COUNT; i++) {
178 hub_port_t *port = &hub_dev.ports[i];
179
180 if (port->device != NULL) {
181 continue;
182 }
183
184 port->device = device;
185
186 /*
187 * TODO:
188 * If the hub was configured, we can normally
189 * announce the plug-in.
190 * Otherwise, we will wait until hub is configured
191 * and announce changes in single burst.
192 */
193 //if (port->state == HUB_PORT_STATE_DISCONNECTED) {
194 port->state = HUB_PORT_STATE_DISABLED;
195 set_port_status_change(port, HUB_STATUS_C_PORT_CONNECTION);
196 //}
197
198 return i;
199 }
200
201 return (size_t)-1;
202}
203
204/** Disconnect device from the hub. */
205void hub_remove_device(virtdev_connection_t *device)
206{
207 size_t i;
208 for (i = 0; i < HUB_PORT_COUNT; i++) {
209 hub_port_t *port = &hub_dev.ports[i];
210
211 if (port->device != device) {
212 continue;
213 }
214
215 port->device = NULL;
216 port->state = HUB_PORT_STATE_DISCONNECTED;
217
218 set_port_status_change(port, HUB_STATUS_C_PORT_CONNECTION);
219 }
220}
221
222/** Tell whether device port is open.
223 *
224 * @return Whether communication to and from the device can go through the hub.
225 */
226bool hub_can_device_signal(virtdev_connection_t * device)
227{
228 size_t i;
229 for (i = 0; i < HUB_PORT_COUNT; i++) {
230 if (hub_dev.ports[i].device == device) {
231 return hub_dev.ports[i].state == HUB_PORT_STATE_ENABLED;
232 }
233 }
234
235 return false;
236}
237
238/** Format hub port status.
239 *
240 * @param result Buffer where to store status string.
241 * @param len Number of characters that is possible to store in @p result
242 * (excluding trailing zero).
243 */
244void hub_get_port_statuses(char *result, size_t len)
245{
246 if (len > HUB_PORT_COUNT) {
247 len = HUB_PORT_COUNT;
248 }
249 size_t i;
250 for (i = 0; i < len; i++) {
251 result[i] = hub_port_state_as_char(hub_dev.ports[i].state);
252 }
253 result[len] = 0;
254}
255
256/**
257 * @}
258 */
Note: See TracBrowser for help on using the repository browser.