source: mainline/uspace/srv/hw/bus/usb/hcd/virtual/hub.c@ 7a7bfeb3

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

Virtual USB overhaul almost complete

The virtual HC, hub and keyboard are rewritten after changes to HCD API.
Comments will be added later.

  • Property mode set to 100644
File size: 6.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 usb
30 * @{
31 */
32/** @file
33 * @brief Virtual USB hub.
34 */
35#include <usb/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
45hub_port_t hub_ports[HUB_PORT_COUNT];
46
47/** Standard device descriptor. */
48usb_standard_device_descriptor_t std_device_descriptor = {
49 .length = sizeof(usb_standard_device_descriptor_t),
50 .descriptor_type = USB_DESCTYPE_DEVICE,
51 .usb_spec_version = 0x110,
52 .device_class = USB_CLASS_HUB,
53 .device_subclass = 0,
54 .device_protocol = 0,
55 .max_packet_size = 64,
56 .configuration_count = 1
57};
58
59/** Standard interface descriptor. */
60usb_standard_interface_descriptor_t std_interface_descriptor = {
61 .length = sizeof(usb_standard_interface_descriptor_t),
62 .descriptor_type = USB_DESCTYPE_INTERFACE,
63 .interface_number = 0,
64 .alternate_setting = 0,
65 .endpoint_count = 1,
66 .interface_class = USB_CLASS_HUB,
67 .interface_subclass = 0,
68 .interface_protocol = 0,
69 .str_interface = 0
70};
71
72hub_descriptor_t hub_descriptor = {
73 .length = sizeof(hub_descriptor_t),
74 .type = USB_DESCTYPE_HUB,
75 .port_count = HUB_PORT_COUNT,
76 .characteristics = 0,
77 .power_on_warm_up = 50, /* Huh? */
78 .max_current = 100, /* Huh again. */
79 .removable_device = { 0 },
80 .port_power = { 0xFF }
81};
82
83/** Endpoint descriptor. */
84usb_standard_endpoint_descriptor_t endpoint_descriptor = {
85 .length = sizeof(usb_standard_endpoint_descriptor_t),
86 .descriptor_type = USB_DESCTYPE_ENDPOINT,
87 .endpoint_address = HUB_STATUS_CHANGE_PIPE | 128,
88 .attributes = USB_TRANSFER_INTERRUPT,
89 .max_packet_size = 8,
90 .poll_interval = 0xFF
91};
92
93/** Standard configuration descriptor. */
94usb_standard_configuration_descriptor_t std_configuration_descriptor = {
95 .length = sizeof(usb_standard_configuration_descriptor_t),
96 .descriptor_type = USB_DESCTYPE_CONFIGURATION,
97 .total_length =
98 sizeof(usb_standard_configuration_descriptor_t)
99 + sizeof(std_interface_descriptor)
100 + sizeof(hub_descriptor)
101 + sizeof(endpoint_descriptor)
102 ,
103 .interface_count = 1,
104 .configuration_number = HUB_CONFIGURATION_ID,
105 .str_configuration = 0,
106 .attributes = 128, /* denotes bus-powered device */
107 .max_power = 50
108};
109
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
139usbvirt_device_t virthub_dev = {
140 .ops = &hub_ops,
141 .descriptors = &descriptors,
142};
143
144hub_device_t hub_dev;
145
146void hub_init(void)
147{
148 size_t i;
149 for (i = 0; i < HUB_PORT_COUNT; i++) {
150 hub_port_t *port = &hub_dev.ports[i];
151
152 port->device = NULL;
153 port->state = HUB_PORT_STATE_NOT_CONFIGURED;
154 port->status_change = 0;
155 }
156
157 usbvirt_connect_local(&virthub_dev);
158 //virthub_dev.send_data = send_data;
159
160 printf("%s: virtual hub (%d ports) created.\n", NAME, HUB_PORT_COUNT);
161}
162
163size_t hub_add_device(virtdev_connection_t *device)
164{
165 size_t i;
166 for (i = 0; i < HUB_PORT_COUNT; i++) {
167 hub_port_t *port = &hub_dev.ports[i];
168
169 if (port->device != NULL) {
170 continue;
171 }
172
173 port->device = device;
174
175 /*
176 * TODO:
177 * If the hub was configured, we can normally
178 * announce the plug-in.
179 * Otherwise, we will wait until hub is configured
180 * and announce changes in single burst.
181 */
182 //if (port->state == HUB_PORT_STATE_DISCONNECTED) {
183 port->state = HUB_PORT_STATE_DISABLED;
184 set_port_status_change(port, HUB_STATUS_C_PORT_CONNECTION);
185 //}
186
187 return i;
188 }
189
190 return (size_t)-1;
191}
192
193
194void hub_remove_device(virtdev_connection_t *device)
195{
196 size_t i;
197 for (i = 0; i < HUB_PORT_COUNT; i++) {
198 hub_port_t *port = &hub_dev.ports[i];
199
200 if (port->device != device) {
201 continue;
202 }
203
204 port->device = NULL;
205 port->state = HUB_PORT_STATE_DISCONNECTED;
206
207 set_port_status_change(port, HUB_STATUS_C_PORT_CONNECTION);
208 }
209}
210
211bool hub_can_device_signal(virtdev_connection_t * device)
212{
213 size_t i;
214 for (i = 0; i < HUB_PORT_COUNT; i++) {
215 if (hub_dev.ports[i].device == device) {
216 return hub_dev.ports[i].state == HUB_PORT_STATE_ENABLED;
217 }
218 }
219
220 return false;
221}
222
223
224/**
225 * @}
226 */
Note: See TracBrowser for help on using the repository browser.