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 operations.
|
---|
34 | */
|
---|
35 | #include <usb/classes/classes.h>
|
---|
36 | #include <usb/classes/hub.h>
|
---|
37 | #include <usbvirt/hub.h>
|
---|
38 | #include <usbvirt/device.h>
|
---|
39 | #include <errno.h>
|
---|
40 |
|
---|
41 | #include "vhcd.h"
|
---|
42 | #include "hub.h"
|
---|
43 | #include "hubintern.h"
|
---|
44 |
|
---|
45 | /** Produce a byte from bit values.
|
---|
46 | */
|
---|
47 | #define MAKE_BYTE(b0, b1, b2, b3, b4, b5, b6, b7) \
|
---|
48 | (( \
|
---|
49 | ((b0) << 0) \
|
---|
50 | | ((b1) << 1) \
|
---|
51 | | ((b2) << 2) \
|
---|
52 | | ((b3) << 3) \
|
---|
53 | | ((b4) << 4) \
|
---|
54 | | ((b5) << 5) \
|
---|
55 | | ((b6) << 6) \
|
---|
56 | | ((b7) << 7) \
|
---|
57 | ))
|
---|
58 |
|
---|
59 | static int on_get_descriptor(struct usbvirt_device *dev,
|
---|
60 | usb_device_request_setup_packet_t *request, uint8_t *data);
|
---|
61 | static int on_class_request(struct usbvirt_device *dev,
|
---|
62 | usb_device_request_setup_packet_t *request, uint8_t *data);
|
---|
63 | static int on_data_request(struct usbvirt_device *dev,
|
---|
64 | usb_endpoint_t endpoint,
|
---|
65 | void *buffer, size_t size, size_t *actual_size);
|
---|
66 |
|
---|
67 | /** Standard USB requests. */
|
---|
68 | static usbvirt_standard_device_request_ops_t standard_request_ops = {
|
---|
69 | .on_get_status = NULL,
|
---|
70 | .on_clear_feature = NULL,
|
---|
71 | .on_set_feature = NULL,
|
---|
72 | .on_set_address = NULL,
|
---|
73 | .on_get_descriptor = on_get_descriptor,
|
---|
74 | .on_set_descriptor = NULL,
|
---|
75 | .on_get_configuration = NULL,
|
---|
76 | .on_set_configuration = NULL,
|
---|
77 | .on_get_interface = NULL,
|
---|
78 | .on_set_interface = NULL,
|
---|
79 | .on_synch_frame = NULL
|
---|
80 | };
|
---|
81 |
|
---|
82 | /** Hub operations. */
|
---|
83 | usbvirt_device_ops_t hub_ops = {
|
---|
84 | .standard_request_ops = &standard_request_ops,
|
---|
85 | .on_class_device_request = on_class_request,
|
---|
86 | .on_data = NULL,
|
---|
87 | .on_data_request = on_data_request
|
---|
88 | };
|
---|
89 |
|
---|
90 | /** Callback for GET_DESCRIPTOR request. */
|
---|
91 | static int on_get_descriptor(struct usbvirt_device *dev,
|
---|
92 | usb_device_request_setup_packet_t *request, uint8_t *data)
|
---|
93 | {
|
---|
94 | if (request->value_high == USB_DESCTYPE_HUB) {
|
---|
95 | int rc = dev->control_transfer_reply(dev, 0,
|
---|
96 | &hub_descriptor, hub_descriptor.length);
|
---|
97 |
|
---|
98 | return rc;
|
---|
99 | }
|
---|
100 | /* Let the framework handle all the rest. */
|
---|
101 | return EFORWARD;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Change port status and updates status change status fields.
|
---|
105 | */
|
---|
106 | static void set_port_state(hub_port_t *port, hub_port_state_t state)
|
---|
107 | {
|
---|
108 | port->state = state;
|
---|
109 | if (state == HUB_PORT_STATE_POWERED_OFF) {
|
---|
110 | clear_port_status_change(port, HUB_STATUS_C_PORT_CONNECTION);
|
---|
111 | clear_port_status_change(port, HUB_STATUS_C_PORT_ENABLE);
|
---|
112 | clear_port_status_change(port, HUB_STATUS_C_PORT_RESET);
|
---|
113 | }
|
---|
114 | if (state == HUB_PORT_STATE_RESUMING) {
|
---|
115 | async_usleep(10*1000);
|
---|
116 | if (port->state == state) {
|
---|
117 | set_port_state(port, HUB_PORT_STATE_ENABLED);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | if (state == HUB_PORT_STATE_RESETTING) {
|
---|
121 | async_usleep(10*1000);
|
---|
122 | if (port->state == state) {
|
---|
123 | set_port_status_change(port, HUB_STATUS_C_PORT_RESET);
|
---|
124 | set_port_state(port, HUB_PORT_STATE_ENABLED);
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | /** Get access to a port or return with EINVAL. */
|
---|
130 | #define _GET_PORT(portvar, index) \
|
---|
131 | do { \
|
---|
132 | if (virthub_dev.state != USBVIRT_STATE_CONFIGURED) { \
|
---|
133 | return EINVAL; \
|
---|
134 | } \
|
---|
135 | if (((index) == 0) || ((index) > HUB_PORT_COUNT)) { \
|
---|
136 | return EINVAL; \
|
---|
137 | } \
|
---|
138 | } while (false); \
|
---|
139 | hub_port_t *portvar = &hub_dev.ports[index]
|
---|
140 |
|
---|
141 |
|
---|
142 | static int clear_hub_feature(uint16_t feature)
|
---|
143 | {
|
---|
144 | return ENOTSUP;
|
---|
145 | }
|
---|
146 |
|
---|
147 | static int clear_port_feature(uint16_t feature, uint16_t portindex)
|
---|
148 | {
|
---|
149 | _GET_PORT(port, portindex);
|
---|
150 |
|
---|
151 | switch (feature) {
|
---|
152 | case USB_HUB_FEATURE_PORT_ENABLE:
|
---|
153 | if ((port->state != HUB_PORT_STATE_NOT_CONFIGURED)
|
---|
154 | && (port->state != HUB_PORT_STATE_POWERED_OFF)) {
|
---|
155 | set_port_state(port, HUB_PORT_STATE_DISABLED);
|
---|
156 | }
|
---|
157 | return EOK;
|
---|
158 |
|
---|
159 | case USB_HUB_FEATURE_PORT_SUSPEND:
|
---|
160 | if (port->state != HUB_PORT_STATE_SUSPENDED) {
|
---|
161 | return EOK;
|
---|
162 | }
|
---|
163 | set_port_state(port, HUB_PORT_STATE_RESUMING);
|
---|
164 | return EOK;
|
---|
165 |
|
---|
166 | case USB_HUB_FEATURE_PORT_POWER:
|
---|
167 | if (port->state != HUB_PORT_STATE_NOT_CONFIGURED) {
|
---|
168 | set_port_state(port, HUB_PORT_STATE_POWERED_OFF);
|
---|
169 | }
|
---|
170 | return EOK;
|
---|
171 |
|
---|
172 | case USB_HUB_FEATURE_C_PORT_CONNECTION:
|
---|
173 | clear_port_status_change(port, HUB_STATUS_C_PORT_CONNECTION);
|
---|
174 | return EOK;
|
---|
175 |
|
---|
176 | case USB_HUB_FEATURE_C_PORT_ENABLE:
|
---|
177 | clear_port_status_change(port, HUB_STATUS_C_PORT_ENABLE);
|
---|
178 | return EOK;
|
---|
179 |
|
---|
180 | case USB_HUB_FEATURE_C_PORT_SUSPEND:
|
---|
181 | clear_port_status_change(port, HUB_STATUS_C_PORT_SUSPEND);
|
---|
182 | return EOK;
|
---|
183 |
|
---|
184 | case USB_HUB_FEATURE_C_PORT_OVER_CURRENT:
|
---|
185 | clear_port_status_change(port, HUB_STATUS_C_PORT_OVER_CURRENT);
|
---|
186 | return EOK;
|
---|
187 | }
|
---|
188 |
|
---|
189 | return ENOTSUP;
|
---|
190 | }
|
---|
191 |
|
---|
192 | static int get_bus_state(uint16_t portindex)
|
---|
193 | {
|
---|
194 | return ENOTSUP;
|
---|
195 | }
|
---|
196 |
|
---|
197 | static int get_hub_descriptor(uint8_t descriptor_type,
|
---|
198 | uint8_t descriptor_index, uint16_t length)
|
---|
199 | {
|
---|
200 | return ENOTSUP;
|
---|
201 | }
|
---|
202 |
|
---|
203 | static int get_hub_status(void)
|
---|
204 | {
|
---|
205 | uint32_t hub_status = 0;
|
---|
206 |
|
---|
207 | return virthub_dev.control_transfer_reply(&virthub_dev, 0,
|
---|
208 | &hub_status, 4);
|
---|
209 | }
|
---|
210 |
|
---|
211 | static int get_port_status(uint16_t portindex)
|
---|
212 | {
|
---|
213 | _GET_PORT(port, portindex);
|
---|
214 |
|
---|
215 | uint32_t status;
|
---|
216 | status = MAKE_BYTE(
|
---|
217 | /* Current connect status. */
|
---|
218 | port->device == NULL ? 0 : 1,
|
---|
219 | /* Port enabled/disabled. */
|
---|
220 | port->state == HUB_PORT_STATE_ENABLED ? 1 : 0,
|
---|
221 | /* Suspend. */
|
---|
222 | (port->state == HUB_PORT_STATE_SUSPENDED)
|
---|
223 | || (port->state == HUB_PORT_STATE_RESUMING) ? 1 : 0,
|
---|
224 | /* Over-current. */
|
---|
225 | 0,
|
---|
226 | /* Reset. */
|
---|
227 | port->state == HUB_PORT_STATE_RESETTING ? 1 : 0,
|
---|
228 | /* Reserved. */
|
---|
229 | 0, 0, 0)
|
---|
230 |
|
---|
231 | | (MAKE_BYTE(
|
---|
232 | /* Port power. */
|
---|
233 | port->state == HUB_PORT_STATE_POWERED_OFF ? 0 : 1,
|
---|
234 | /* Full-speed device. */
|
---|
235 | 0,
|
---|
236 | /* Reserved. */
|
---|
237 | 0, 0, 0, 0, 0, 0
|
---|
238 | )) << 8;
|
---|
239 |
|
---|
240 | status |= (port->status_change << 16);
|
---|
241 |
|
---|
242 | return virthub_dev.control_transfer_reply(&virthub_dev, 0, &status, 4);
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | static int set_hub_feature(uint16_t feature)
|
---|
247 | {
|
---|
248 | return ENOTSUP;
|
---|
249 | }
|
---|
250 |
|
---|
251 | static int set_port_feature(uint16_t feature, uint16_t portindex)
|
---|
252 | {
|
---|
253 | _GET_PORT(port, portindex);
|
---|
254 |
|
---|
255 | switch (feature) {
|
---|
256 | case USB_HUB_FEATURE_PORT_RESET:
|
---|
257 | if (port->state != HUB_PORT_STATE_POWERED_OFF) {
|
---|
258 | set_port_state(port, HUB_PORT_STATE_RESETTING);
|
---|
259 | }
|
---|
260 | return EOK;
|
---|
261 |
|
---|
262 | case USB_HUB_FEATURE_PORT_SUSPEND:
|
---|
263 | if (port->state == HUB_PORT_STATE_ENABLED) {
|
---|
264 | set_port_state(port, HUB_PORT_STATE_SUSPENDED);
|
---|
265 | }
|
---|
266 | return EOK;
|
---|
267 |
|
---|
268 | case USB_HUB_FEATURE_PORT_POWER:
|
---|
269 | if (port->state == HUB_PORT_STATE_POWERED_OFF) {
|
---|
270 | set_port_state(port, HUB_PORT_STATE_DISCONNECTED);
|
---|
271 | }
|
---|
272 | return EOK;
|
---|
273 | }
|
---|
274 | return ENOTSUP;
|
---|
275 | }
|
---|
276 |
|
---|
277 | #undef _GET_PORT
|
---|
278 |
|
---|
279 |
|
---|
280 | /** Callback for class request. */
|
---|
281 | static int on_class_request(struct usbvirt_device *dev,
|
---|
282 | usb_device_request_setup_packet_t *request, uint8_t *data)
|
---|
283 | {
|
---|
284 | dprintf(2, "hub class request (%d)\n", (int) request->request);
|
---|
285 |
|
---|
286 | uint8_t recipient = request->request_type & 31;
|
---|
287 | uint8_t direction = request->request_type >> 7;
|
---|
288 |
|
---|
289 | #define _VERIFY(cond) \
|
---|
290 | do { \
|
---|
291 | if (!(cond)) { \
|
---|
292 | dprintf(0, "WARN: invalid class request (%s not met).\n", \
|
---|
293 | NAME, #cond); \
|
---|
294 | return EINVAL; \
|
---|
295 | } \
|
---|
296 | } while (0)
|
---|
297 |
|
---|
298 | switch (request->request) {
|
---|
299 | case USB_HUB_REQUEST_CLEAR_FEATURE:
|
---|
300 | _VERIFY(direction == 0);
|
---|
301 | _VERIFY(request->length == 0);
|
---|
302 | if (recipient == 0) {
|
---|
303 | _VERIFY(request->index == 0);
|
---|
304 | return clear_hub_feature(request->value);
|
---|
305 | } else {
|
---|
306 | _VERIFY(recipient == 3);
|
---|
307 | return clear_port_feature(request->value,
|
---|
308 | request->index);
|
---|
309 | }
|
---|
310 |
|
---|
311 | case USB_HUB_REQUEST_GET_STATE:
|
---|
312 | return get_bus_state(request->index);
|
---|
313 |
|
---|
314 | case USB_HUB_REQUEST_GET_DESCRIPTOR:
|
---|
315 | return get_hub_descriptor(request->value_low,
|
---|
316 | request->value_high, request->length);
|
---|
317 |
|
---|
318 | case USB_HUB_REQUEST_GET_STATUS:
|
---|
319 | if (recipient == 0) {
|
---|
320 | return get_hub_status();
|
---|
321 | } else {
|
---|
322 | return get_port_status(request->index);
|
---|
323 | }
|
---|
324 |
|
---|
325 | case USB_HUB_REQUEST_SET_FEATURE:
|
---|
326 | if (recipient == 0) {
|
---|
327 | return set_hub_feature(request->value);
|
---|
328 | } else {
|
---|
329 | return set_port_feature(request->value, request->index);
|
---|
330 | }
|
---|
331 |
|
---|
332 | default:
|
---|
333 | break;
|
---|
334 | }
|
---|
335 |
|
---|
336 | #undef _VERIFY
|
---|
337 |
|
---|
338 |
|
---|
339 | return EOK;
|
---|
340 | }
|
---|
341 |
|
---|
342 | void clear_port_status_change(hub_port_t *port, uint16_t change)
|
---|
343 | {
|
---|
344 | port->status_change &= (~change);
|
---|
345 | }
|
---|
346 |
|
---|
347 | void set_port_status_change(hub_port_t *port, uint16_t change)
|
---|
348 | {
|
---|
349 | port->status_change |= change;
|
---|
350 | }
|
---|
351 |
|
---|
352 | /** Callback for data request. */
|
---|
353 | static int on_data_request(struct usbvirt_device *dev,
|
---|
354 | usb_endpoint_t endpoint,
|
---|
355 | void *buffer, size_t size, size_t *actual_size)
|
---|
356 | {
|
---|
357 | if (endpoint != HUB_STATUS_CHANGE_PIPE) {
|
---|
358 | return EINVAL;
|
---|
359 | }
|
---|
360 |
|
---|
361 | uint8_t change_map = 0;
|
---|
362 |
|
---|
363 | size_t i;
|
---|
364 | for (i = 0; i < HUB_PORT_COUNT; i++) {
|
---|
365 | hub_port_t *port = &hub_dev.ports[i];
|
---|
366 |
|
---|
367 | if (port->status_change != 0) {
|
---|
368 | change_map |= (1 << (i + 1));
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | uint8_t *b = (uint8_t *) buffer;
|
---|
373 | if (size > 0) {
|
---|
374 | *b = change_map;
|
---|
375 | *actual_size = 1;
|
---|
376 | }
|
---|
377 |
|
---|
378 | return EOK;
|
---|
379 | }
|
---|
380 |
|
---|
381 |
|
---|
382 | /**
|
---|
383 | * @}
|
---|
384 | */
|
---|