source: mainline/uspace/drv/bus/usb/vhc/transfer.c@ cecba66e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cecba66e was 58563585, checked in by Martin Decky <martin@…>, 9 years ago

code review and cstyle cleanup (no change in functionality)

  • Property mode set to 100644
File size: 7.6 KB
Line 
1/*
2 * Copyright (c) 2011 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#include <errno.h>
30#include <str_error.h>
31#include <usb/debug.h>
32#include <usbvirt/device.h>
33#include <usbvirt/ipc.h>
34#include "vhcd.h"
35#include "hub/virthub.h"
36
37static bool is_set_address_transfer(vhc_transfer_t *transfer)
38{
39 if (transfer->batch->ep->endpoint != 0) {
40 return false;
41 }
42 if (transfer->batch->ep->transfer_type != USB_TRANSFER_CONTROL) {
43 return false;
44 }
45 if (usb_transfer_batch_direction(transfer->batch) != USB_DIRECTION_OUT) {
46 return false;
47 }
48 const usb_device_request_setup_packet_t *setup =
49 (void*)transfer->batch->setup_buffer;
50 if (setup->request_type != 0) {
51 return false;
52 }
53 if (setup->request != USB_DEVREQ_SET_ADDRESS) {
54 return false;
55 }
56
57 return true;
58}
59
60static int process_transfer_local(usb_transfer_batch_t *batch,
61 usbvirt_device_t *dev, size_t *actual_data_size)
62{
63 int rc;
64
65 const usb_direction_t dir = usb_transfer_batch_direction(batch);
66
67 if (batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
68 if (dir == USB_DIRECTION_IN) {
69 rc = usbvirt_control_read(dev,
70 batch->setup_buffer, batch->setup_size,
71 batch->buffer, batch->buffer_size,
72 actual_data_size);
73 } else {
74 assert(dir == USB_DIRECTION_OUT);
75 rc = usbvirt_control_write(dev,
76 batch->setup_buffer, batch->setup_size,
77 batch->buffer, batch->buffer_size);
78 }
79 } else {
80 if (dir == USB_DIRECTION_IN) {
81 rc = usbvirt_data_in(dev, batch->ep->transfer_type,
82 batch->ep->endpoint,
83 batch->buffer, batch->buffer_size,
84 actual_data_size);
85 } else {
86 assert(dir == USB_DIRECTION_OUT);
87 rc = usbvirt_data_out(dev, batch->ep->transfer_type,
88 batch->ep->endpoint,
89 batch->buffer, batch->buffer_size);
90 }
91 }
92
93 return rc;
94}
95
96static int process_transfer_remote(usb_transfer_batch_t *batch,
97 async_sess_t *sess, size_t *actual_data_size)
98{
99 int rc;
100
101 const usb_direction_t dir = usb_transfer_batch_direction(batch);
102
103 if (batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
104 if (dir == USB_DIRECTION_IN) {
105 rc = usbvirt_ipc_send_control_read(sess,
106 batch->setup_buffer, batch->setup_size,
107 batch->buffer, batch->buffer_size,
108 actual_data_size);
109 } else {
110 assert(dir == USB_DIRECTION_OUT);
111 rc = usbvirt_ipc_send_control_write(sess,
112 batch->setup_buffer, batch->setup_size,
113 batch->buffer, batch->buffer_size);
114 }
115 } else {
116 if (dir == USB_DIRECTION_IN) {
117 rc = usbvirt_ipc_send_data_in(sess, batch->ep->endpoint,
118 batch->ep->transfer_type,
119 batch->buffer, batch->buffer_size,
120 actual_data_size);
121 } else {
122 assert(dir == USB_DIRECTION_OUT);
123 rc = usbvirt_ipc_send_data_out(sess, batch->ep->endpoint,
124 batch->ep->transfer_type,
125 batch->buffer, batch->buffer_size);
126 }
127 }
128
129 return rc;
130}
131
132static vhc_transfer_t *dequeue_first_transfer(vhc_virtdev_t *dev)
133{
134 assert(fibril_mutex_is_locked(&dev->guard));
135 assert(!list_empty(&dev->transfer_queue));
136
137 vhc_transfer_t *transfer = list_get_instance(
138 list_first(&dev->transfer_queue), vhc_transfer_t, link);
139 list_remove(&transfer->link);
140
141 return transfer;
142}
143
144static void execute_transfer_callback_and_free(vhc_transfer_t *transfer,
145 size_t data_transfer_size, int outcome)
146{
147 assert(outcome != ENAK);
148 assert(transfer);
149 assert(transfer->batch);
150 usb_transfer_batch_finish_error(transfer->batch, NULL,
151 data_transfer_size, outcome);
152 usb_transfer_batch_destroy(transfer->batch);
153 free(transfer);
154}
155
156int vhc_init(vhc_data_t *instance)
157{
158 assert(instance);
159 list_initialize(&instance->devices);
160 fibril_mutex_initialize(&instance->guard);
161 instance->magic = 0xDEADBEEF;
162 return virthub_init(&instance->hub, "root hub");
163}
164
165int vhc_schedule(hcd_t *hcd, usb_transfer_batch_t *batch)
166{
167 assert(hcd);
168 assert(batch);
169 vhc_data_t *vhc = hcd_get_driver_data(hcd);
170 assert(vhc);
171
172 vhc_transfer_t *transfer = malloc(sizeof(vhc_transfer_t));
173 if (!transfer)
174 return ENOMEM;
175 link_initialize(&transfer->link);
176 transfer->batch = batch;
177
178 fibril_mutex_lock(&vhc->guard);
179
180 int targets = 0;
181
182 list_foreach(vhc->devices, link, vhc_virtdev_t, dev) {
183 fibril_mutex_lock(&dev->guard);
184 if (dev->address == transfer->batch->ep->address) {
185 if (!targets) {
186 list_append(&transfer->link, &dev->transfer_queue);
187 }
188 ++targets;
189 }
190 fibril_mutex_unlock(&dev->guard);
191 }
192
193 fibril_mutex_unlock(&vhc->guard);
194
195 if (targets > 1)
196 usb_log_warning("Transfer would be accepted by more devices!\n");
197
198 return targets ? EOK : ENOENT;
199}
200
201int vhc_transfer_queue_processor(void *arg)
202{
203 vhc_virtdev_t *dev = arg;
204 fibril_mutex_lock(&dev->guard);
205 while (dev->plugged) {
206 if (list_empty(&dev->transfer_queue)) {
207 fibril_mutex_unlock(&dev->guard);
208 async_usleep(10 * 1000);
209 fibril_mutex_lock(&dev->guard);
210 continue;
211 }
212
213 vhc_transfer_t *transfer = dequeue_first_transfer(dev);
214 fibril_mutex_unlock(&dev->guard);
215
216 int rc = EOK;
217 size_t data_transfer_size = 0;
218 if (dev->dev_sess) {
219 rc = process_transfer_remote(transfer->batch,
220 dev->dev_sess, &data_transfer_size);
221 } else if (dev->dev_local != NULL) {
222 rc = process_transfer_local(transfer->batch,
223 dev->dev_local, &data_transfer_size);
224 } else {
225 usb_log_warning("Device has no remote phone nor local node.\n");
226 rc = ESTALL;
227 }
228
229 usb_log_debug2("Transfer %p processed: %s.\n",
230 transfer, str_error(rc));
231
232 fibril_mutex_lock(&dev->guard);
233 if (rc == EOK) {
234 if (is_set_address_transfer(transfer)) {
235 usb_device_request_setup_packet_t *setup =
236 (void*) transfer->batch->setup_buffer;
237 dev->address = setup->value;
238 usb_log_debug2("Address changed to %d\n",
239 dev->address);
240 }
241 }
242 if (rc == ENAK) {
243 // FIXME: this will work only because we do
244 // not NAK control transfers but this is generally
245 // a VERY bad idea indeed
246 list_append(&transfer->link, &dev->transfer_queue);
247 }
248 fibril_mutex_unlock(&dev->guard);
249
250 if (rc != ENAK) {
251 execute_transfer_callback_and_free(transfer,
252 data_transfer_size, rc);
253 }
254
255 async_usleep(1000 * 100);
256 fibril_mutex_lock(&dev->guard);
257 }
258
259 /* Immediately fail all remaining transfers. */
260 while (!list_empty(&dev->transfer_queue)) {
261 vhc_transfer_t *transfer = dequeue_first_transfer(dev);
262 execute_transfer_callback_and_free(transfer, 0, EBADCHECKSUM);
263 }
264
265 fibril_mutex_unlock(&dev->guard);
266
267 return EOK;
268}
Note: See TracBrowser for help on using the repository browser.