source: mainline/uspace/drv/bus/usb/vhc/hub/virthub.c

Last change on this file was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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