source: mainline/uspace/lib/usbvirt/src/ctrlpipe.c@ bd8c753d

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

Doxygen group for USB virtualization

Virtual host controller and libusbvirt has separate Doxygen groups because
the usb group was polluted way too much.

  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[bc9a629]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
[bd8c753d]29/** @addtogroup libusbvirt
[bc9a629]30 * @{
31 */
32/** @file
[b8100da]33 * @brief Device control pipe.
[bc9a629]34 */
[b8100da]35#include <errno.h>
[bc9a629]36
[b8100da]37#include "private.h"
[bc9a629]38
[7feeb84]39/** Compares handler type with request packet type.
40 *
41 * @param handler Handler.
42 * @param request_packet Request packet.
43 * @return Whether handler can serve this packet.
44 */
45static bool is_suitable_handler(usbvirt_control_transfer_handler_t *handler,
46 usb_device_request_setup_packet_t *request_packet)
[aab02fb]47{
[7feeb84]48 return (
49 (handler->request_type == request_packet->request_type)
50 && (handler->request == request_packet->request));
51
[aab02fb]52}
[954ea70]53
[7feeb84]54/** Find suitable transfer handler for given request packet.
55 *
56 * @param handlers Array of available handlers.
57 * @param request_packet Request SETUP packet.
58 * @return Handler or NULL.
[ca07cd3]59 */
[7feeb84]60static usbvirt_control_transfer_handler_t *find_handler(
61 usbvirt_control_transfer_handler_t *handlers,
62 usb_device_request_setup_packet_t *request_packet)
[4971812]63{
[7feeb84]64 if (handlers == NULL) {
65 return NULL;
66 }
67
68 while (handlers->callback != NULL) {
69 if (is_suitable_handler(handlers, request_packet)) {
70 return handlers;
71 }
72 handlers++;
73 }
74
75 return NULL;
[4971812]76}
77
[d5e7668]78#define _GET_BIT(byte, bit) \
79 (((byte) & (1 << (bit))) ? '1' : '0')
80#define _GET_BITS(byte) \
81 _GET_BIT(byte, 7), _GET_BIT(byte, 6), _GET_BIT(byte, 5), \
82 _GET_BIT(byte, 4), _GET_BIT(byte, 3), _GET_BIT(byte, 2), \
83 _GET_BIT(byte, 1), _GET_BIT(byte, 0)
84
85static int find_and_run_handler(usbvirt_device_t *device,
86 usbvirt_control_transfer_handler_t *handlers,
87 usb_device_request_setup_packet_t *setup_packet,
88 uint8_t *data)
89{
90 int rc = EFORWARD;
91 usbvirt_control_transfer_handler_t *suitable_handler
92 = find_handler(handlers, setup_packet);
93 if (suitable_handler != NULL) {
94 const char *callback_name = "user handler";
95 if (suitable_handler->name != NULL) {
96 callback_name = suitable_handler->name;
97 }
98 device->lib_debug(device, 1, USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO,
99 "pipe #0 - calling %s " \
100 "[%c.%c%c.%c%c%c%c%c, R%d, V%d, I%d, L%d]",
101 callback_name,
102 _GET_BITS(setup_packet->request_type),
103 setup_packet->request, setup_packet->value,
104 setup_packet->index, setup_packet->length);
105 rc = suitable_handler->callback(device, setup_packet, data);
106 }
107
108 return rc;
109}
110#undef _GET_BITS
111#undef _GET_BIT
[7feeb84]112
113
[ca07cd3]114/** Handle communication over control pipe zero.
115 */
116int control_pipe(usbvirt_device_t *device, usbvirt_control_transfer_t *transfer)
[b8100da]117{
[d5e7668]118 device->lib_debug(device, 2, USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO,
[aab02fb]119 "op on control pipe zero (request_size=%u)", transfer->request_size);
120
[7a7bfeb3]121 if (transfer->request_size < sizeof(usb_device_request_setup_packet_t)) {
[4971812]122 return ENOMEM;
123 }
124
[7feeb84]125 usb_device_request_setup_packet_t *request
126 = (usb_device_request_setup_packet_t *) transfer->request;
[d5e7668]127
[7feeb84]128 /*
129 * First, see whether user provided its own callback.
130 */
131 int rc = EFORWARD;
132 if (device->ops) {
[d5e7668]133 rc = find_and_run_handler(device,
134 device->ops->control_transfer_handlers,
135 request, transfer->data);
[7feeb84]136 }
137
138 /*
139 * If there was no user callback or the callback returned EFORWARD,
140 * we need to run a local handler.
141 */
142 if (rc == EFORWARD) {
[d5e7668]143 rc = find_and_run_handler(device,
144 control_pipe_zero_local_handlers,
145 request, transfer->data);
[4971812]146 }
147
[7feeb84]148 /*
149 * Check for SET_ADDRESS finalization.
150 */
[ca07cd3]151 if (device->new_address != -1) {
[954ea70]152 /*
153 * TODO: handle when this request is invalid (e.g.
154 * setting address when in configured state).
155 */
[266d0871]156 usbvirt_device_state_t new_state;
[ca07cd3]157 if (device->new_address == 0) {
[266d0871]158 new_state = USBVIRT_STATE_DEFAULT;
[954ea70]159 } else {
[266d0871]160 new_state = USBVIRT_STATE_ADDRESS;
[954ea70]161 }
[ca07cd3]162 device->address = device->new_address;
[954ea70]163
[ca07cd3]164 device->new_address = -1;
[aab02fb]165
[266d0871]166 if (DEVICE_HAS_OP(device, on_state_change)) {
167 device->ops->on_state_change(device, device->state,
168 new_state);
169 }
170 device->state = new_state;
171
[aab02fb]172 device->lib_debug(device, 2, USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO,
173 "device address changed to %d (state %s)",
174 device->address, str_device_state(device->state));
[954ea70]175 }
176
177 return rc;
[b8100da]178}
[bc9a629]179
180/**
181 * @}
182 */
Note: See TracBrowser for help on using the repository browser.