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

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

VHCD handles new methods

Not completely, though. Will finish later.

  • 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.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
146static int send_data(struct usbvirt_device *dev,
147 usb_endpoint_t endpoint, void *buffer, size_t size)
148{
149 usb_target_t target = { dev->address, endpoint };
150 void *my_buffer = NULL;
151 if (size > 0) {
152 my_buffer = malloc(size);
153 memcpy(my_buffer, buffer, size);
154 }
155 hc_fillin_transaction_from_device(target, my_buffer, size);
156
157 return EOK;
158}
159
160void hub_init(void)
161{
162 size_t i;
163 for (i = 0; i < HUB_PORT_COUNT; i++) {
164 hub_port_t *port = &hub_dev.ports[i];
165
166 port->device = NULL;
167 port->state = HUB_PORT_STATE_NOT_CONFIGURED;
168 port->status_change = 0;
169 }
170
171 usbvirt_connect_local(&virthub_dev);
172 virthub_dev.send_data = send_data;
173
174 printf("%s: virtual hub (%d ports) created.\n", NAME, HUB_PORT_COUNT);
175}
176
177size_t hub_add_device(virtdev_connection_t *device)
178{
179 size_t i;
180 for (i = 0; i < HUB_PORT_COUNT; i++) {
181 hub_port_t *port = &hub_dev.ports[i];
182
183 if (port->device != NULL) {
184 continue;
185 }
186
187 port->device = device;
188
189 /*
190 * TODO:
191 * If the hub was configured, we can normally
192 * announce the plug-in.
193 * Otherwise, we will wait until hub is configured
194 * and announce changes in single burst.
195 */
196 //if (port->state == HUB_PORT_STATE_DISCONNECTED) {
197 port->state = HUB_PORT_STATE_DISABLED;
198 set_port_status_change(port, HUB_STATUS_C_PORT_CONNECTION);
199 //}
200
201 return i;
202 }
203
204 return (size_t)-1;
205}
206
207
208void hub_remove_device(virtdev_connection_t *device)
209{
210 size_t i;
211 for (i = 0; i < HUB_PORT_COUNT; i++) {
212 hub_port_t *port = &hub_dev.ports[i];
213
214 if (port->device != device) {
215 continue;
216 }
217
218 port->device = NULL;
219 port->state = HUB_PORT_STATE_DISCONNECTED;
220
221 set_port_status_change(port, HUB_STATUS_C_PORT_CONNECTION);
222 }
223}
224
225bool hub_can_device_signal(virtdev_connection_t * device)
226{
227 size_t i;
228 for (i = 0; i < HUB_PORT_COUNT; i++) {
229 if (hub_dev.ports[i].device == device) {
230 return hub_dev.ports[i].state == HUB_PORT_STATE_ENABLED;
231 }
232 }
233
234 return false;
235}
236
237void hub_check_port_changes(void)
238{
239 /* FIXME - what if HUB_PORT_COUNT is greater than 8. */
240 uint8_t change_map = 0;
241
242 size_t i;
243 for (i = 0; i < HUB_PORT_COUNT; i++) {
244 hub_port_t *port = &hub_dev.ports[i];
245
246 if (port->status_change != 0) {
247 change_map |= (1 << (i + 1));
248 }
249 }
250
251 /* FIXME - do not send when it has not changed since previous run. */
252 if (change_map != 0) {
253 virthub_dev.send_data(&virthub_dev, HUB_STATUS_CHANGE_PIPE,
254 &change_map, 1);
255 }
256}
257
258/**
259 * @}
260 */
Note: See TracBrowser for help on using the repository browser.