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