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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cecba66e was 58563585, checked in by Martin Decky <martin@…>, 9 years ago

code review and cstyle cleanup (no change in functionality)

  • 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 <ddf/driver.h>
45
46#include "virthub.h"
47#include "hub.h"
48
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 ,
107 .interface_count = 1,
108 .configuration_number = HUB_CONFIGURATION_ID,
109 .str_configuration = 0,
110 .attributes = 128, /* denotes bus-powered device */
111 .max_power = 50
112};
113
114/** All hub configuration descriptors. */
115static usbvirt_device_configuration_extras_t extra_descriptors[] = {
116 {
117 .data = (uint8_t *) &std_interface_descriptor,
118 .length = sizeof(std_interface_descriptor)
119 },
120 {
121 .data = (uint8_t *) &hub_descriptor,
122 .length = sizeof(hub_descriptor)
123 },
124 {
125 .data = (uint8_t *) &endpoint_descriptor,
126 .length = sizeof(endpoint_descriptor)
127 }
128};
129
130/** Hub configuration. */
131usbvirt_device_configuration_t configuration = {
132 .descriptor = &std_configuration_descriptor,
133 .extra = extra_descriptors,
134 .extra_count = sizeof(extra_descriptors)/sizeof(extra_descriptors[0])
135};
136
137/** Hub standard descriptors. */
138usbvirt_descriptors_t descriptors = {
139 .device = &std_device_descriptor,
140 .configuration = &configuration,
141 .configuration_count = 1,
142};
143
144/** Initializes virtual hub device.
145 *
146 * @param dev Virtual USB device backend.
147 * @return Error code.
148 */
149int virthub_init(usbvirt_device_t *dev, const char* name)
150{
151 if (dev == NULL) {
152 return EBADMEM;
153 }
154 dev->ops = &hub_ops;
155 dev->descriptors = &descriptors;
156 dev->address = 0;
157 dev->name = str_dup(name);
158 if (!dev->name)
159 return ENOMEM;
160
161 hub_t *hub = malloc(sizeof(hub_t));
162 if (hub == NULL) {
163 free(dev->name);
164 return ENOMEM;
165 }
166
167 hub_init(hub);
168 dev->device_data = hub;
169
170 return EOK;
171}
172
173/** Connect a device to a virtual hub.
174 *
175 * @param dev Virtual device representing the hub.
176 * @param conn Device to be connected.
177 * @return Port device was connected to.
178 */
179int virthub_connect_device(usbvirt_device_t *dev, vhc_virtdev_t *conn)
180{
181 assert(dev != NULL);
182 assert(conn != NULL);
183
184 hub_t *hub = (hub_t *) dev->device_data;
185
186 hub_acquire(hub);
187 size_t port = hub_connect_device(hub, conn);
188 hub_release(hub);
189
190 return port;
191}
192
193/** Disconnect a device from a virtual hub.
194 *
195 * @param dev Virtual device representing the hub.
196 * @param conn Device to be disconnected.
197 * @return Error code.
198 */
199int virthub_disconnect_device(usbvirt_device_t *dev, vhc_virtdev_t *conn)
200{
201 assert(dev != NULL);
202 assert(conn != NULL);
203
204 hub_t *hub = (hub_t *) dev->device_data;
205
206 hub_acquire(hub);
207 hub_disconnect_device(hub, conn);
208 hub_release(hub);
209
210 return EOK;
211}
212
213/** Whether trafic is propagated to given device.
214 *
215 * @param dev Virtual device representing the hub.
216 * @param conn Connected device.
217 * @return Whether port is signalling to the device.
218 */
219bool virthub_is_device_enabled(usbvirt_device_t *dev, vhc_virtdev_t *conn)
220{
221 assert(dev != NULL);
222 assert(conn != NULL);
223
224 hub_t *hub = (hub_t *) dev->device_data;
225
226 hub_acquire(hub);
227
228 hub_port_state_t state = HUB_PORT_STATE_UNKNOWN;
229 size_t port = hub_find_device(hub, conn);
230 if (port != (size_t) -1) {
231 state = hub_get_port_state(hub, port);
232 }
233 hub_release(hub);
234
235 return state == HUB_PORT_STATE_ENABLED;
236}
237
238/** Format status of a virtual hub.
239 *
240 * @param dev Virtual device representing the hub.
241 * @param[out] status Hub status information.
242 * @param[in] len Size of the @p status buffer.
243 */
244void virthub_get_status(usbvirt_device_t *dev, char *status, size_t len)
245{
246 assert(dev != NULL);
247 if (len == 0) {
248 return;
249 }
250
251 hub_t *hub = (hub_t *) dev->device_data;
252
253 char port_status[HUB_PORT_COUNT + 1];
254
255 size_t i;
256 for (i = 0; i < HUB_PORT_COUNT; i++) {
257 port_status[i] = hub_port_state_to_char(
258 hub_get_port_state(hub, i));
259 }
260 port_status[HUB_PORT_COUNT] = 0;
261
262 snprintf(status, len, "vhub:%s", port_status);
263}
264
265/**
266 * @}
267 */
Note: See TracBrowser for help on using the repository browser.