source: mainline/uspace/lib/usbvirt/transaction.c@ ca07cd3

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

Code cleanup, various bugfixes

The internal functions of virtual device framework always get
device structure as parameter, thus possible enabling more devices
within single task (that is not possible because currently there
is no way to pass extra argument into callback_connection()).

Also, added some missing comments and completely removed the device
id nonsense (devices can send their descriptors and the hub is able
to enable/disable its ports).

  • Property mode set to 100644
File size: 6.9 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 Transaction processing.
34 */
35#include <errno.h>
36#include <stdlib.h>
37#include <stdio.h>
38#include <mem.h>
39
40#include "hub.h"
41#include "private.h"
42
43static usb_direction_t setup_transaction_direction(usbvirt_device_t *,
44 usb_endpoint_t, void *, size_t);
45static void process_control_transfer(usbvirt_device_t *,
46 usb_endpoint_t, usbvirt_control_transfer_t *);
47
48/** Convert virtual USB transaction type to string.
49 */
50const char *usbvirt_str_transaction_type(usbvirt_transaction_type_t type)
51{
52 switch (type) {
53 case USBVIRT_TRANSACTION_SETUP:
54 return "setup";
55 case USBVIRT_TRANSACTION_IN:
56 return "in";
57 case USBVIRT_TRANSACTION_OUT:
58 return "out";
59 default:
60 return "unknown";
61 }
62}
63
64/** SETUP transaction handling.
65 * The setup transaction only prepares control transfer on given endpoint.
66 */
67int transaction_setup(usbvirt_device_t *device, usb_endpoint_t endpoint,
68 void *buffer, size_t size)
69{
70 usbvirt_control_transfer_t *transfer = &device->current_control_transfers[endpoint];
71
72 if (transfer->request != NULL) {
73 free(transfer->request);
74 }
75 if (transfer->data != NULL) {
76 free(transfer->data);
77 }
78
79 transfer->direction = setup_transaction_direction(device, endpoint,
80 buffer, size);
81 transfer->request = buffer;
82 transfer->request_size = size;
83 transfer->data = NULL;
84 transfer->data_size = 0;
85
86 if (transfer->direction == USB_DIRECTION_IN) {
87 process_control_transfer(device, endpoint, transfer);
88 }
89
90 return EOK;
91}
92
93/** OUT transaction handling.
94 * The OUT transaction can trigger processing of a control transfer.
95 */
96int transaction_out(usbvirt_device_t *device, usb_endpoint_t endpoint,
97 void *buffer, size_t size)
98{
99 /*
100 * First check whether it is a transaction over control pipe.
101 */
102 usbvirt_control_transfer_t *transfer = &device->current_control_transfers[endpoint];
103 if (transfer->request != NULL) {
104 if (transfer->direction == USB_DIRECTION_OUT) {
105 /*
106 * For out transactions, append the data to the buffer.
107 */
108 uint8_t *new_buffer = (uint8_t *) malloc(transfer->data_size + size);
109 if (transfer->data) {
110 memcpy(new_buffer, transfer->data, transfer->data_size);
111 }
112 memcpy(new_buffer + transfer->data_size, buffer, size);
113
114 if (transfer->data) {
115 free(transfer->data);
116 }
117 transfer->data = new_buffer;
118 transfer->data_size += size;
119 } else {
120 /*
121 * For in transactions, this means end of the
122 * transaction.
123 */
124 free(transfer->request);
125 if (transfer->data) {
126 free(transfer->data);
127 }
128 transfer->request = NULL;
129 transfer->request_size = 0;
130 transfer->data = NULL;
131 transfer->data_size = 0;
132 }
133
134 return EOK;
135 }
136
137 /*
138 * Otherwise, announce that some data has come.
139 */
140 if (device->ops && device->ops->on_data) {
141 return device->ops->on_data(device, endpoint, buffer, size);
142 } else {
143 return ENOTSUP;
144 }
145}
146
147/** IN transaction handling.
148 * The IN transaction can trigger processing of a control transfer.
149 */
150int transaction_in(usbvirt_device_t *device, usb_endpoint_t endpoint,
151 void *buffer, size_t size, size_t *data_size)
152{
153 /*
154 * First check whether it is a transaction over control pipe.
155 */
156 usbvirt_control_transfer_t *transfer = &device->current_control_transfers[endpoint];
157 if (transfer->request != NULL) {
158 if (transfer->direction == USB_DIRECTION_OUT) {
159 /*
160 * This means end of input data.
161 */
162 process_control_transfer(device, endpoint, transfer);
163 } else {
164 /*
165 * For in transactions, this means sending next part
166 * of the buffer.
167 */
168 // FIXME: handle when the HC wants the data back
169 // in more chunks
170 size_t actual_size = 0;
171 if (transfer->data) {
172 actual_size = transfer->data_size;
173 }
174 if (actual_size > size) {
175 actual_size = size;
176 }
177 if (actual_size > 0) {
178 memcpy(buffer, transfer->data, actual_size);
179 if (data_size) {
180 *data_size = actual_size;
181 }
182 }
183 }
184
185 return EOK;
186 }
187
188 if (size == 0) {
189 return EINVAL;
190 }
191
192 int rc = 1;
193
194 if (device->ops && device->ops->on_data_request) {
195 rc = device->ops->on_data_request(device, endpoint, buffer, size, data_size);
196 }
197
198 return rc;
199}
200
201/** Determine direction of control transfer.
202 * First, try the user provided callback, otherwise guess, believing that
203 * it uses the same format as control pipe 0.
204 */
205static usb_direction_t setup_transaction_direction(usbvirt_device_t *device,
206 usb_endpoint_t endpoint,
207 void *data, size_t size)
208{
209 int direction = -1;
210 if (device->ops && device->ops->decide_control_transfer_direction) {
211 direction = device->ops->decide_control_transfer_direction(endpoint,
212 data, size);
213 }
214
215 /*
216 * If the user-supplied routine have not handled the direction
217 * (or simply was not provided) we will guess, hoping that it
218 * uses same format as standard request on control pipe zero.
219 */
220 if (direction < 0) {
221 if (size > 0) {
222 uint8_t *ptr = (uint8_t *) data;
223 if ((ptr[0] & 128) == 128) {
224 direction = USB_DIRECTION_IN;
225 } else {
226 direction = USB_DIRECTION_OUT;
227 }
228 } else {
229 /* This shall not happen anyway. */
230 direction = USB_DIRECTION_OUT;
231 }
232 }
233
234 return (usb_direction_t) direction;
235}
236
237/** Process control transfer.
238 */
239static void process_control_transfer(usbvirt_device_t *device,
240 usb_endpoint_t endpoint,
241 usbvirt_control_transfer_t *transfer)
242{
243 int rc = EFORWARD;
244
245 if (device->ops && device->ops->on_control_transfer) {
246 rc = device->ops->on_control_transfer(device, endpoint, transfer);
247 }
248
249 if (rc == EFORWARD) {
250 if (endpoint == 0) {
251 rc = control_pipe(device, transfer);
252 }
253 }
254}
255
256/**
257 * @}
258 */
Note: See TracBrowser for help on using the repository browser.