[79ae36dd] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
[e0a5d4c] | 3 | * Copyright (c) 2018 Ondrej Hlavaty
|
---|
[79ae36dd] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[6cb58e6] | 30 | #include <errno.h>
|
---|
| 31 | #include <str_error.h>
|
---|
[f6577d9] | 32 | #include <usb/debug.h>
|
---|
[6cb58e6] | 33 | #include <usbvirt/device.h>
|
---|
[c0e4b5b2] | 34 | #include <usb/host/bandwidth.h>
|
---|
[64fea02] | 35 | #include <usb/host/endpoint.h>
|
---|
| 36 | #include <usb/host/usb_transfer_batch.h>
|
---|
[6cb58e6] | 37 | #include <usbvirt/ipc.h>
|
---|
| 38 | #include "vhcd.h"
|
---|
[f5f0cfb] | 39 | #include "hub/virthub.h"
|
---|
[01eeaaf] | 40 |
|
---|
[f6577d9] | 41 | static bool is_set_address_transfer(vhc_transfer_t *transfer)
|
---|
[6cb58e6] | 42 | {
|
---|
[32fb6bce] | 43 | if (transfer->batch.target.endpoint != 0) {
|
---|
[f6577d9] | 44 | return false;
|
---|
[6cb58e6] | 45 | }
|
---|
[32fb6bce] | 46 | if (transfer->batch.ep->transfer_type != USB_TRANSFER_CONTROL) {
|
---|
[f6577d9] | 47 | return false;
|
---|
| 48 | }
|
---|
[32fb6bce] | 49 | if (transfer->batch.dir != USB_DIRECTION_OUT) {
|
---|
[f6577d9] | 50 | return false;
|
---|
| 51 | }
|
---|
[ae3a941] | 52 | const usb_device_request_setup_packet_t *setup =
|
---|
| 53 | &transfer->batch.setup.packet;
|
---|
[f6577d9] | 54 | if (setup->request_type != 0) {
|
---|
| 55 | return false;
|
---|
[6cb58e6] | 56 | }
|
---|
[f6577d9] | 57 | if (setup->request != USB_DEVREQ_SET_ADDRESS) {
|
---|
| 58 | return false;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | return true;
|
---|
[6cb58e6] | 62 | }
|
---|
| 63 |
|
---|
[5a6cc679] | 64 | static errno_t process_transfer_local(usb_transfer_batch_t *batch,
|
---|
[6cb58e6] | 65 | usbvirt_device_t *dev, size_t *actual_data_size)
|
---|
| 66 | {
|
---|
[5a6cc679] | 67 | errno_t rc;
|
---|
[5fd9c30] | 68 |
|
---|
| 69 | const usb_direction_t dir = batch->dir;
|
---|
[6cb58e6] | 70 |
|
---|
[01eeaaf] | 71 | if (batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 72 | if (dir == USB_DIRECTION_IN) {
|
---|
[6cb58e6] | 73 | rc = usbvirt_control_read(dev,
|
---|
[5fd9c30] | 74 | batch->setup.buffer, USB_SETUP_PACKET_SIZE,
|
---|
[1d758fc] | 75 | batch->dma_buffer.virt, batch->size,
|
---|
[6cb58e6] | 76 | actual_data_size);
|
---|
| 77 | } else {
|
---|
[01eeaaf] | 78 | assert(dir == USB_DIRECTION_OUT);
|
---|
[6cb58e6] | 79 | rc = usbvirt_control_write(dev,
|
---|
[5fd9c30] | 80 | batch->setup.buffer, USB_SETUP_PACKET_SIZE,
|
---|
[1d758fc] | 81 | batch->dma_buffer.virt, batch->size);
|
---|
[6cb58e6] | 82 | }
|
---|
| 83 | } else {
|
---|
[01eeaaf] | 84 | if (dir == USB_DIRECTION_IN) {
|
---|
| 85 | rc = usbvirt_data_in(dev, batch->ep->transfer_type,
|
---|
[a5b3de6] | 86 | batch->ep->endpoint,
|
---|
[1d758fc] | 87 | batch->dma_buffer.virt, batch->size,
|
---|
[6cb58e6] | 88 | actual_data_size);
|
---|
| 89 | } else {
|
---|
[01eeaaf] | 90 | assert(dir == USB_DIRECTION_OUT);
|
---|
| 91 | rc = usbvirt_data_out(dev, batch->ep->transfer_type,
|
---|
[a5b3de6] | 92 | batch->ep->endpoint,
|
---|
[1d758fc] | 93 | batch->dma_buffer.virt, batch->size);
|
---|
[6cb58e6] | 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | return rc;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[5a6cc679] | 100 | static errno_t process_transfer_remote(usb_transfer_batch_t *batch,
|
---|
[79ae36dd] | 101 | async_sess_t *sess, size_t *actual_data_size)
|
---|
[6cb58e6] | 102 | {
|
---|
[5a6cc679] | 103 | errno_t rc;
|
---|
[6cb58e6] | 104 |
|
---|
[5fd9c30] | 105 | const usb_direction_t dir = batch->dir;
|
---|
[01eeaaf] | 106 |
|
---|
| 107 | if (batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 108 | if (dir == USB_DIRECTION_IN) {
|
---|
[79ae36dd] | 109 | rc = usbvirt_ipc_send_control_read(sess,
|
---|
[5fd9c30] | 110 | batch->setup.buffer, USB_SETUP_PACKET_SIZE,
|
---|
[1d758fc] | 111 | batch->dma_buffer.virt, batch->size,
|
---|
[6cb58e6] | 112 | actual_data_size);
|
---|
| 113 | } else {
|
---|
[01eeaaf] | 114 | assert(dir == USB_DIRECTION_OUT);
|
---|
[79ae36dd] | 115 | rc = usbvirt_ipc_send_control_write(sess,
|
---|
[5fd9c30] | 116 | batch->setup.buffer, USB_SETUP_PACKET_SIZE,
|
---|
[1d758fc] | 117 | batch->dma_buffer.virt, batch->size);
|
---|
[6cb58e6] | 118 | }
|
---|
| 119 | } else {
|
---|
[01eeaaf] | 120 | if (dir == USB_DIRECTION_IN) {
|
---|
[a5b3de6] | 121 | rc = usbvirt_ipc_send_data_in(sess, batch->ep->endpoint,
|
---|
[01eeaaf] | 122 | batch->ep->transfer_type,
|
---|
[1d758fc] | 123 | batch->dma_buffer.virt, batch->size,
|
---|
[6cb58e6] | 124 | actual_data_size);
|
---|
| 125 | } else {
|
---|
[01eeaaf] | 126 | assert(dir == USB_DIRECTION_OUT);
|
---|
[a5b3de6] | 127 | rc = usbvirt_ipc_send_data_out(sess, batch->ep->endpoint,
|
---|
[01eeaaf] | 128 | batch->ep->transfer_type,
|
---|
[1d758fc] | 129 | batch->dma_buffer.virt, batch->size);
|
---|
[6cb58e6] | 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | return rc;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[e913cc9] | 136 | static vhc_transfer_t *dequeue_first_transfer(vhc_virtdev_t *dev)
|
---|
| 137 | {
|
---|
| 138 | assert(fibril_mutex_is_locked(&dev->guard));
|
---|
| 139 | assert(!list_empty(&dev->transfer_queue));
|
---|
| 140 |
|
---|
[ae3a941] | 141 | vhc_transfer_t *transfer =
|
---|
| 142 | list_get_instance(list_first(&dev->transfer_queue),
|
---|
| 143 | vhc_transfer_t, link);
|
---|
[e913cc9] | 144 | list_remove(&transfer->link);
|
---|
| 145 |
|
---|
| 146 | return transfer;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | static void execute_transfer_callback_and_free(vhc_transfer_t *transfer,
|
---|
[5a6cc679] | 150 | size_t data_transfer_size, errno_t outcome)
|
---|
[e913cc9] | 151 | {
|
---|
| 152 | assert(outcome != ENAK);
|
---|
[01eeaaf] | 153 | assert(transfer);
|
---|
[32fb6bce] | 154 | transfer->batch.error = outcome;
|
---|
[db51a6a6] | 155 | transfer->batch.transferred_size = data_transfer_size;
|
---|
[32fb6bce] | 156 | usb_transfer_batch_finish(&transfer->batch);
|
---|
[e913cc9] | 157 | }
|
---|
[6cb58e6] | 158 |
|
---|
[32fb6bce] | 159 | static usb_transfer_batch_t *batch_create(endpoint_t *ep)
|
---|
| 160 | {
|
---|
| 161 | vhc_transfer_t *transfer = calloc(1, sizeof(vhc_transfer_t));
|
---|
| 162 | usb_transfer_batch_init(&transfer->batch, ep);
|
---|
| 163 | link_initialize(&transfer->link);
|
---|
| 164 | return &transfer->batch;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[d369b3b] | 167 | static int device_enumerate(device_t *device)
|
---|
| 168 | {
|
---|
| 169 | vhc_data_t *vhc = bus_to_vhc(device->bus);
|
---|
| 170 | return usb2_bus_device_enumerate(&vhc->bus_helper, device);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | static int endpoint_register(endpoint_t *endpoint)
|
---|
| 174 | {
|
---|
| 175 | vhc_data_t *vhc = bus_to_vhc(endpoint->device->bus);
|
---|
| 176 | return usb2_bus_endpoint_register(&vhc->bus_helper, endpoint);
|
---|
| 177 | }
|
---|
[32fb6bce] | 178 |
|
---|
[d369b3b] | 179 | static void endpoint_unregister(endpoint_t *endpoint)
|
---|
| 180 | {
|
---|
| 181 | vhc_data_t *vhc = bus_to_vhc(endpoint->device->bus);
|
---|
| 182 | usb2_bus_endpoint_unregister(&vhc->bus_helper, endpoint);
|
---|
| 183 |
|
---|
| 184 | // TODO: abort transfer?
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | static const bus_ops_t vhc_bus_ops = {
|
---|
[32fb6bce] | 188 | .batch_create = batch_create,
|
---|
| 189 | .batch_schedule = vhc_schedule,
|
---|
[d369b3b] | 190 |
|
---|
| 191 | .device_enumerate = device_enumerate,
|
---|
| 192 | .endpoint_register = endpoint_register,
|
---|
| 193 | .endpoint_unregister = endpoint_unregister,
|
---|
[6832245] | 194 | };
|
---|
| 195 |
|
---|
[5a6cc679] | 196 | errno_t vhc_init(vhc_data_t *instance)
|
---|
[f5f0cfb] | 197 | {
|
---|
| 198 | assert(instance);
|
---|
| 199 | list_initialize(&instance->devices);
|
---|
| 200 | fibril_mutex_initialize(&instance->guard);
|
---|
[d369b3b] | 201 | bus_init(&instance->bus, sizeof(device_t));
|
---|
| 202 | usb2_bus_helper_init(&instance->bus_helper, &bandwidth_accounting_usb11);
|
---|
| 203 | instance->bus.ops = &vhc_bus_ops;
|
---|
[f5f0cfb] | 204 | return virthub_init(&instance->hub, "root hub");
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[5a6cc679] | 207 | errno_t vhc_schedule(usb_transfer_batch_t *batch)
|
---|
[f5f0cfb] | 208 | {
|
---|
| 209 | assert(batch);
|
---|
[32fb6bce] | 210 | vhc_transfer_t *transfer = (vhc_transfer_t *) batch;
|
---|
| 211 | vhc_data_t *vhc = bus_to_vhc(endpoint_get_bus(batch->ep));
|
---|
[f5f0cfb] | 212 | assert(vhc);
|
---|
| 213 |
|
---|
| 214 | fibril_mutex_lock(&vhc->guard);
|
---|
| 215 |
|
---|
| 216 | int targets = 0;
|
---|
| 217 |
|
---|
[3f03199] | 218 | list_foreach(vhc->devices, link, vhc_virtdev_t, dev) {
|
---|
[f5f0cfb] | 219 | fibril_mutex_lock(&dev->guard);
|
---|
[32fb6bce] | 220 | if (dev->address == transfer->batch.target.address) {
|
---|
[f5f0cfb] | 221 | if (!targets) {
|
---|
| 222 | list_append(&transfer->link, &dev->transfer_queue);
|
---|
| 223 | }
|
---|
| 224 | ++targets;
|
---|
| 225 | }
|
---|
| 226 | fibril_mutex_unlock(&dev->guard);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | fibril_mutex_unlock(&vhc->guard);
|
---|
[a35b458] | 230 |
|
---|
[f5f0cfb] | 231 | if (targets > 1)
|
---|
[a1732929] | 232 | usb_log_warning("Transfer would be accepted by more devices!");
|
---|
[f5f0cfb] | 233 |
|
---|
| 234 | return targets ? EOK : ENOENT;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[5a6cc679] | 237 | errno_t vhc_transfer_queue_processor(void *arg)
|
---|
[6cb58e6] | 238 | {
|
---|
| 239 | vhc_virtdev_t *dev = arg;
|
---|
| 240 | fibril_mutex_lock(&dev->guard);
|
---|
| 241 | while (dev->plugged) {
|
---|
| 242 | if (list_empty(&dev->transfer_queue)) {
|
---|
| 243 | fibril_mutex_unlock(&dev->guard);
|
---|
[5f97ef44] | 244 | fibril_usleep(10 * 1000);
|
---|
[6cb58e6] | 245 | fibril_mutex_lock(&dev->guard);
|
---|
| 246 | continue;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[e913cc9] | 249 | vhc_transfer_t *transfer = dequeue_first_transfer(dev);
|
---|
[6cb58e6] | 250 | fibril_mutex_unlock(&dev->guard);
|
---|
| 251 |
|
---|
[5a6cc679] | 252 | errno_t rc = EOK;
|
---|
[6cb58e6] | 253 | size_t data_transfer_size = 0;
|
---|
[79ae36dd] | 254 | if (dev->dev_sess) {
|
---|
[32fb6bce] | 255 | rc = process_transfer_remote(&transfer->batch,
|
---|
[01eeaaf] | 256 | dev->dev_sess, &data_transfer_size);
|
---|
[6cb58e6] | 257 | } else if (dev->dev_local != NULL) {
|
---|
[32fb6bce] | 258 | rc = process_transfer_local(&transfer->batch,
|
---|
[01eeaaf] | 259 | dev->dev_local, &data_transfer_size);
|
---|
[6cb58e6] | 260 | } else {
|
---|
[ae3a941] | 261 | usb_log_warning("Device has no remote phone "
|
---|
| 262 | "nor local node.");
|
---|
[6cb58e6] | 263 | rc = ESTALL;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[a1732929] | 266 | usb_log_debug2("Transfer %p processed: %s.",
|
---|
[6cb58e6] | 267 | transfer, str_error(rc));
|
---|
| 268 |
|
---|
| 269 | fibril_mutex_lock(&dev->guard);
|
---|
| 270 | if (rc == EOK) {
|
---|
| 271 | if (is_set_address_transfer(transfer)) {
|
---|
[58563585] | 272 | usb_device_request_setup_packet_t *setup =
|
---|
[ae3a941] | 273 | (void *) transfer->batch.setup.buffer;
|
---|
[6cb58e6] | 274 | dev->address = setup->value;
|
---|
[a1732929] | 275 | usb_log_debug2("Address changed to %d",
|
---|
[6cb58e6] | 276 | dev->address);
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 | if (rc == ENAK) {
|
---|
| 280 | // FIXME: this will work only because we do
|
---|
| 281 | // not NAK control transfers but this is generally
|
---|
| 282 | // a VERY bad idea indeed
|
---|
| 283 | list_append(&transfer->link, &dev->transfer_queue);
|
---|
| 284 | }
|
---|
| 285 | fibril_mutex_unlock(&dev->guard);
|
---|
| 286 |
|
---|
| 287 | if (rc != ENAK) {
|
---|
[e913cc9] | 288 | execute_transfer_callback_and_free(transfer,
|
---|
| 289 | data_transfer_size, rc);
|
---|
[6cb58e6] | 290 | }
|
---|
| 291 |
|
---|
[5f97ef44] | 292 | fibril_usleep(1000 * 100);
|
---|
[6cb58e6] | 293 | fibril_mutex_lock(&dev->guard);
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[e913cc9] | 296 | /* Immediately fail all remaining transfers. */
|
---|
| 297 | while (!list_empty(&dev->transfer_queue)) {
|
---|
| 298 | vhc_transfer_t *transfer = dequeue_first_transfer(dev);
|
---|
| 299 | execute_transfer_callback_and_free(transfer, 0, EBADCHECKSUM);
|
---|
| 300 | }
|
---|
[6cb58e6] | 301 |
|
---|
[e913cc9] | 302 | fibril_mutex_unlock(&dev->guard);
|
---|
[6cb58e6] | 303 |
|
---|
| 304 | return EOK;
|
---|
| 305 | }
|
---|