source: mainline/uspace/drv/bus/usb/vhc/hub/virthubops.c@ 2a6e2358

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2a6e2358 was 2a6e2358, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libusbvirt: Make request creation macros available in header.

  • Property mode set to 100644
File size: 10.7 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 Virtual USB hub operations.
34 */
35#include <errno.h>
36#include <usb/classes/hub.h>
37#include <usbvirt/device.h>
38#include "virthub.h"
39#include "hub.h"
40
41/** Callback when device changes states. */
42static void on_state_change(usbvirt_device_t *dev,
43 usbvirt_device_state_t old_state, usbvirt_device_state_t new_state)
44{
45 hub_t *hub = (hub_t *)dev->device_data;
46
47 hub_acquire(hub);
48
49 switch (new_state) {
50 case USBVIRT_STATE_CONFIGURED:
51 hub_set_port_state_all(hub, HUB_PORT_STATE_POWERED_OFF);
52 break;
53 case USBVIRT_STATE_ADDRESS:
54 hub_set_port_state_all(hub, HUB_PORT_STATE_NOT_CONFIGURED);
55 break;
56 default:
57 break;
58 }
59
60 hub_release(hub);
61}
62
63/** Callback for data request. */
64static int req_on_status_change_pipe(usbvirt_device_t *dev,
65 usb_endpoint_t endpoint, usb_transfer_type_t tr_type,
66 void *buffer, size_t buffer_size, size_t *actual_size)
67{
68 if (endpoint != HUB_STATUS_CHANGE_PIPE) {
69 return ESTALL;
70 }
71 if (tr_type != USB_TRANSFER_INTERRUPT) {
72 return ESTALL;
73 }
74
75 hub_t *hub = dev->device_data;
76
77 hub_acquire(hub);
78
79 if (!hub->signal_changes) {
80 hub_release(hub);
81
82 return ENAK;
83 }
84
85
86 uint8_t change_map = hub_get_status_change_bitmap(hub);
87
88 uint8_t *b = (uint8_t *) buffer;
89 if (buffer_size > 0) {
90 *b = change_map;
91 *actual_size = 1;
92 } else {
93 *actual_size = 0;
94 }
95
96 hub->signal_changes = false;
97
98 hub_release(hub);
99
100 return EOK;
101}
102
103/** Handle ClearHubFeature request.
104 *
105 * @param dev Virtual device representing the hub.
106 * @param request The SETUP packet of the control request.
107 * @param data Extra data (when DATA stage present).
108 * @return Error code.
109 */
110static int req_clear_hub_feature(usbvirt_device_t *dev,
111 const usb_device_request_setup_packet_t *request, uint8_t *data,
112 size_t *act_size)
113{
114 return ENOTSUP;
115}
116
117/** Handle ClearPortFeature request.
118 *
119 * @param dev Virtual device representing the hub.
120 * @param request The SETUP packet of the control request.
121 * @param data Extra data (when DATA stage present).
122 * @return Error code.
123 */
124static int req_clear_port_feature(usbvirt_device_t *dev,
125 const usb_device_request_setup_packet_t *request, uint8_t *data,
126 size_t *act_size)
127{
128 int rc;
129 size_t port = request->index - 1;
130 usb_hub_class_feature_t feature = request->value;
131 hub_t *hub = (hub_t *) dev->device_data;
132
133 hub_acquire(hub);
134
135 hub_port_state_t port_state = hub_get_port_state(hub, port);
136
137 switch (feature) {
138 case USB_HUB_FEATURE_PORT_ENABLE:
139 if ((port_state != HUB_PORT_STATE_NOT_CONFIGURED)
140 && (port_state != HUB_PORT_STATE_POWERED_OFF)) {
141 hub_set_port_state(hub, port, HUB_PORT_STATE_DISABLED);
142 }
143 rc = EOK;
144 break;
145
146 case USB_HUB_FEATURE_PORT_SUSPEND:
147 if (port_state != HUB_PORT_STATE_SUSPENDED) {
148 rc = EOK;
149 break;
150 }
151 hub_set_port_state(hub, port, HUB_PORT_STATE_RESUMING);
152 rc = EOK;
153 break;
154
155 case USB_HUB_FEATURE_PORT_POWER:
156 if (port_state != HUB_PORT_STATE_NOT_CONFIGURED) {
157 hub_set_port_state(hub, port, HUB_PORT_STATE_POWERED_OFF);
158 }
159 rc = EOK;
160 break;
161
162 case USB_HUB_FEATURE_C_PORT_CONNECTION:
163 hub_clear_port_status_change(hub, port, HUB_STATUS_C_PORT_CONNECTION);
164 rc = EOK;
165 break;
166
167 case USB_HUB_FEATURE_C_PORT_ENABLE:
168 hub_clear_port_status_change(hub, port, HUB_STATUS_C_PORT_ENABLE);
169 rc = EOK;
170 break;
171
172 case USB_HUB_FEATURE_C_PORT_SUSPEND:
173 hub_clear_port_status_change(hub, port, HUB_STATUS_C_PORT_SUSPEND);
174 rc = EOK;
175 break;
176
177 case USB_HUB_FEATURE_C_PORT_OVER_CURRENT:
178 hub_clear_port_status_change(hub, port, HUB_STATUS_C_PORT_OVER_CURRENT);
179 rc = EOK;
180 break;
181
182 case USB_HUB_FEATURE_C_PORT_RESET:
183 hub_clear_port_status_change(hub, port, HUB_STATUS_C_PORT_RESET);
184 rc = EOK;
185 break;
186
187 default:
188 rc = ENOTSUP;
189 break;
190 }
191
192 hub_release(hub);
193
194 return rc;
195}
196
197/** Handle GetBusState request.
198 *
199 * @param dev Virtual device representing the hub.
200 * @param request The SETUP packet of the control request.
201 * @param data Extra data (when DATA stage present).
202 * @return Error code.
203 */
204static int req_get_bus_state(usbvirt_device_t *dev,
205 const usb_device_request_setup_packet_t *request, uint8_t *data,
206 size_t *act_size)
207{
208 return ENOTSUP;
209}
210
211/** Handle GetDescriptor request.
212 *
213 * @param dev Virtual device representing the hub.
214 * @param request The SETUP packet of the control request.
215 * @param data Extra data (when DATA stage present).
216 * @return Error code.
217 */
218static int req_get_descriptor(usbvirt_device_t *dev,
219 const usb_device_request_setup_packet_t *request, uint8_t *data,
220 size_t *act_size)
221{
222 if (request->value_high == USB_DESCTYPE_HUB) {
223 usbvirt_control_reply_helper(request, data, act_size,
224 &hub_descriptor, hub_descriptor.length);
225
226 return EOK;
227 }
228 /* Let the framework handle all the rest. */
229 return EFORWARD;
230}
231
232/** Handle GetHubStatus request.
233 *
234 * @param dev Virtual device representing the hub.
235 * @param request The SETUP packet of the control request.
236 * @param data Extra data (when DATA stage present).
237 * @return Error code.
238 */
239static int req_get_hub_status(usbvirt_device_t *dev,
240 const usb_device_request_setup_packet_t *request, uint8_t *data,
241 size_t *act_size)
242{
243 uint32_t hub_status = 0;
244
245 usbvirt_control_reply_helper(request, data, act_size,
246 &hub_status, sizeof(hub_status));
247
248 return EOK;
249}
250
251/** Handle GetPortStatus request.
252 *
253 * @param dev Virtual device representing the hub.
254 * @param request The SETUP packet of the control request.
255 * @param data Extra data (when DATA stage present).
256 * @return Error code.
257 */
258static int req_get_port_status(usbvirt_device_t *dev,
259 const usb_device_request_setup_packet_t *request, uint8_t *data,
260 size_t *act_size)
261{
262 hub_t *hub = (hub_t *) dev->device_data;
263
264 hub_acquire(hub);
265
266 uint32_t status = hub_get_port_status(hub, request->index - 1);
267
268 hub_release(hub);
269
270 usbvirt_control_reply_helper(request, data, act_size,
271 &status, sizeof(status));
272
273 return EOK;
274}
275
276/** Handle SetHubFeature request.
277 *
278 * @param dev Virtual device representing the hub.
279 * @param request The SETUP packet of the control request.
280 * @param data Extra data (when DATA stage present).
281 * @return Error code.
282 */
283static int req_set_hub_feature(usbvirt_device_t *dev,
284 const usb_device_request_setup_packet_t *request, uint8_t *data,
285 size_t *act_size)
286{
287 return ENOTSUP;
288}
289
290/** Handle SetPortFeature request.
291 *
292 * @param dev Virtual device representing the hub.
293 * @param request The SETUP packet of the control request.
294 * @param data Extra data (when DATA stage present).
295 * @return Error code.
296 */
297static int req_set_port_feature(usbvirt_device_t *dev,
298 const usb_device_request_setup_packet_t *request, uint8_t *data,
299 size_t *act_size)
300{
301 int rc;
302 size_t port = request->index - 1;
303 usb_hub_class_feature_t feature = request->value;
304 hub_t *hub = (hub_t *) dev->device_data;
305
306 hub_acquire(hub);
307
308 hub_port_state_t port_state = hub_get_port_state(hub, port);
309
310 switch (feature) {
311 case USB_HUB_FEATURE_PORT_RESET:
312 if (port_state != HUB_PORT_STATE_POWERED_OFF) {
313 hub_set_port_state(hub, port, HUB_PORT_STATE_RESETTING);
314 }
315 rc = EOK;
316 break;
317
318 case USB_HUB_FEATURE_PORT_SUSPEND:
319 if (port_state == HUB_PORT_STATE_ENABLED) {
320 hub_set_port_state(hub, port, HUB_PORT_STATE_SUSPENDED);
321 }
322 rc = EOK;
323 break;
324
325 case USB_HUB_FEATURE_PORT_POWER:
326 if (port_state == HUB_PORT_STATE_POWERED_OFF) {
327 hub_set_port_state(hub, port, HUB_PORT_STATE_DISCONNECTED);
328 }
329 rc = EOK;
330 break;
331
332 default:
333 break;
334 }
335
336 hub_release(hub);
337
338 return rc;
339}
340
341
342
343/** Recipient: other. */
344#define REC_OTHER USB_REQUEST_RECIPIENT_OTHER
345/** Recipient: device. */
346#define REC_DEVICE USB_REQUEST_RECIPIENT_DEVICE
347
348
349/** Hub operations on control endpoint zero. */
350static usbvirt_control_request_handler_t endpoint_zero_handlers[] = {
351 {
352 STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
353 .name = "GetDescriptor",
354 .callback = req_get_descriptor
355 },
356 {
357 CLASS_REQ_IN(REC_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
358 .name = "GetDescriptor",
359 .callback = req_get_descriptor
360 },
361 {
362 CLASS_REQ_IN(REC_OTHER, USB_HUB_REQUEST_GET_STATUS),
363 .name = "GetPortStatus",
364 .callback = req_get_port_status
365 },
366 {
367 CLASS_REQ_OUT(REC_DEVICE, USB_HUB_REQUEST_CLEAR_FEATURE),
368 .name = "ClearHubFeature",
369 .callback = req_clear_hub_feature
370 },
371 {
372 CLASS_REQ_OUT(REC_OTHER, USB_HUB_REQUEST_CLEAR_FEATURE),
373 .name = "ClearPortFeature",
374 .callback = req_clear_port_feature
375 },
376 {
377 CLASS_REQ_IN(REC_OTHER, USB_HUB_REQUEST_GET_STATE),
378 .name = "GetBusState",
379 .callback = req_get_bus_state
380 },
381 {
382 CLASS_REQ_IN(REC_DEVICE, USB_HUB_REQUEST_GET_DESCRIPTOR),
383 .name = "GetHubDescriptor",
384 .callback = req_get_descriptor
385 },
386 {
387 CLASS_REQ_IN(REC_DEVICE, USB_HUB_REQUEST_GET_STATUS),
388 .name = "GetHubStatus",
389 .callback = req_get_hub_status
390 },
391 {
392 CLASS_REQ_IN(REC_OTHER, USB_HUB_REQUEST_GET_STATUS),
393 .name = "GetPortStatus",
394 .callback = req_get_port_status
395 },
396 {
397 CLASS_REQ_OUT(REC_DEVICE, USB_HUB_REQUEST_SET_FEATURE),
398 .name = "SetHubFeature",
399 .callback = req_set_hub_feature
400 },
401 {
402 CLASS_REQ_OUT(REC_OTHER, USB_HUB_REQUEST_SET_FEATURE),
403 .name = "SetPortFeature",
404 .callback = req_set_port_feature
405 },
406 {
407 .callback = NULL
408 }
409};
410
411
412/** Hub operations. */
413usbvirt_device_ops_t hub_ops = {
414 .control = endpoint_zero_handlers,
415 .data_in[HUB_STATUS_CHANGE_PIPE] = req_on_status_change_pipe,
416 .state_changed = on_state_change,
417};
418
419/**
420 * @}
421 */
Note: See TracBrowser for help on using the repository browser.