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

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

virtusb: named callbacks for pipe zero

  • Property mode set to 100644
File size: 5.2 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 libusbvirt usb
30 * @{
31 */
32/** @file
33 * @brief Device control pipe.
34 */
35#include <errno.h>
36
37#include "private.h"
38
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)
47{
48 return (
49 (handler->request_type == request_packet->request_type)
50 && (handler->request == request_packet->request));
51
52}
53
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.
59 */
60static usbvirt_control_transfer_handler_t *find_handler(
61 usbvirt_control_transfer_handler_t *handlers,
62 usb_device_request_setup_packet_t *request_packet)
63{
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;
76}
77
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
112
113
114/** Handle communication over control pipe zero.
115 */
116int control_pipe(usbvirt_device_t *device, usbvirt_control_transfer_t *transfer)
117{
118 device->lib_debug(device, 2, USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO,
119 "op on control pipe zero (request_size=%u)", transfer->request_size);
120
121 if (transfer->request_size < sizeof(usb_device_request_setup_packet_t)) {
122 return ENOMEM;
123 }
124
125 usb_device_request_setup_packet_t *request
126 = (usb_device_request_setup_packet_t *) transfer->request;
127
128 /*
129 * First, see whether user provided its own callback.
130 */
131 int rc = EFORWARD;
132 if (device->ops) {
133 rc = find_and_run_handler(device,
134 device->ops->control_transfer_handlers,
135 request, transfer->data);
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) {
143 rc = find_and_run_handler(device,
144 control_pipe_zero_local_handlers,
145 request, transfer->data);
146 }
147
148 /*
149 * Check for SET_ADDRESS finalization.
150 */
151 if (device->new_address != -1) {
152 /*
153 * TODO: handle when this request is invalid (e.g.
154 * setting address when in configured state).
155 */
156 if (device->new_address == 0) {
157 device->state = USBVIRT_STATE_DEFAULT;
158 } else {
159 device->state = USBVIRT_STATE_ADDRESS;
160 }
161 device->address = device->new_address;
162
163 device->new_address = -1;
164
165 device->lib_debug(device, 2, USBVIRT_DEBUGTAG_CONTROL_PIPE_ZERO,
166 "device address changed to %d (state %s)",
167 device->address, str_device_state(device->state));
168 }
169
170 return rc;
171}
172
173/**
174 * @}
175 */
Note: See TracBrowser for help on using the repository browser.