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

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

vhc: Remove single instance limitation.

Embed hub vitrul device instead of using global instance.

  • 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->private_data;
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, pos) {
183 vhc_virtdev_t *dev = list_get_instance(pos, vhc_virtdev_t, link);
184 fibril_mutex_lock(&dev->guard);
185 if (dev->address == transfer->batch->ep->address) {
186 if (!targets) {
187 list_append(&transfer->link, &dev->transfer_queue);
188 }
189 ++targets;
190 }
191 fibril_mutex_unlock(&dev->guard);
192 }
193
194 fibril_mutex_unlock(&vhc->guard);
195
196 if (targets > 1)
197 usb_log_warning("Transfer would be accepted by more devices!\n");
198
199 return targets ? EOK : ENOENT;
200}
201
202int vhc_transfer_queue_processor(void *arg)
203{
204 vhc_virtdev_t *dev = arg;
205 fibril_mutex_lock(&dev->guard);
206 while (dev->plugged) {
207 if (list_empty(&dev->transfer_queue)) {
208 fibril_mutex_unlock(&dev->guard);
209 async_usleep(10 * 1000);
210 fibril_mutex_lock(&dev->guard);
211 continue;
212 }
213
214 vhc_transfer_t *transfer = dequeue_first_transfer(dev);
215 fibril_mutex_unlock(&dev->guard);
216
217 int rc = EOK;
218 size_t data_transfer_size = 0;
219 if (dev->dev_sess) {
220 rc = process_transfer_remote(transfer->batch,
221 dev->dev_sess, &data_transfer_size);
222 } else if (dev->dev_local != NULL) {
223 rc = process_transfer_local(transfer->batch,
224 dev->dev_local, &data_transfer_size);
225 } else {
226 usb_log_warning("Device has no remote phone nor local node.\n");
227 rc = ESTALL;
228 }
229
230 usb_log_debug2("Transfer %p processed: %s.\n",
231 transfer, str_error(rc));
232
233 fibril_mutex_lock(&dev->guard);
234 if (rc == EOK) {
235 if (is_set_address_transfer(transfer)) {
236 usb_device_request_setup_packet_t *setup
237 = (void*)transfer->batch->setup_buffer;
238 dev->address = setup->value;
239 usb_log_debug2("Address changed to %d\n",
240 dev->address);
241 }
242 }
243 if (rc == ENAK) {
244 // FIXME: this will work only because we do
245 // not NAK control transfers but this is generally
246 // a VERY bad idea indeed
247 list_append(&transfer->link, &dev->transfer_queue);
248 }
249 fibril_mutex_unlock(&dev->guard);
250
251 if (rc != ENAK) {
252 execute_transfer_callback_and_free(transfer,
253 data_transfer_size, rc);
254 }
255
256 async_usleep(1000 * 100);
257 fibril_mutex_lock(&dev->guard);
258 }
259
260 /* Immediately fail all remaining transfers. */
261 while (!list_empty(&dev->transfer_queue)) {
262 vhc_transfer_t *transfer = dequeue_first_transfer(dev);
263 execute_transfer_callback_and_free(transfer, 0, EBADCHECKSUM);
264 }
265
266 fibril_mutex_unlock(&dev->guard);
267
268 return EOK;
269}
Note: See TracBrowser for help on using the repository browser.