[79ae36dd] | 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 |
|
---|
[6cb58e6] | 29 | #include <errno.h>
|
---|
| 30 | #include <str_error.h>
|
---|
[f6577d9] | 31 | #include <usb/debug.h>
|
---|
[6cb58e6] | 32 | #include <usbvirt/device.h>
|
---|
| 33 | #include <usbvirt/ipc.h>
|
---|
| 34 | #include "vhcd.h"
|
---|
[f5f0cfb] | 35 | #include "hub/virthub.h"
|
---|
[01eeaaf] | 36 |
|
---|
[f6577d9] | 37 | static bool is_set_address_transfer(vhc_transfer_t *transfer)
|
---|
[6cb58e6] | 38 | {
|
---|
[f6577d9] | 39 | if (transfer->batch->ep->endpoint != 0) {
|
---|
| 40 | return false;
|
---|
[6cb58e6] | 41 | }
|
---|
[f6577d9] | 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;
|
---|
[6cb58e6] | 52 | }
|
---|
[f6577d9] | 53 | if (setup->request != USB_DEVREQ_SET_ADDRESS) {
|
---|
| 54 | return false;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | return true;
|
---|
[6cb58e6] | 58 | }
|
---|
| 59 |
|
---|
[01eeaaf] | 60 | static int process_transfer_local(usb_transfer_batch_t *batch,
|
---|
[6cb58e6] | 61 | usbvirt_device_t *dev, size_t *actual_data_size)
|
---|
| 62 | {
|
---|
| 63 | int rc;
|
---|
[01eeaaf] | 64 |
|
---|
| 65 | const usb_direction_t dir = usb_transfer_batch_direction(batch);
|
---|
[6cb58e6] | 66 |
|
---|
[01eeaaf] | 67 | if (batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 68 | if (dir == USB_DIRECTION_IN) {
|
---|
[6cb58e6] | 69 | rc = usbvirt_control_read(dev,
|
---|
[01eeaaf] | 70 | batch->setup_buffer, batch->setup_size,
|
---|
| 71 | batch->buffer, batch->buffer_size,
|
---|
[6cb58e6] | 72 | actual_data_size);
|
---|
| 73 | } else {
|
---|
[01eeaaf] | 74 | assert(dir == USB_DIRECTION_OUT);
|
---|
[6cb58e6] | 75 | rc = usbvirt_control_write(dev,
|
---|
[01eeaaf] | 76 | batch->setup_buffer, batch->setup_size,
|
---|
| 77 | batch->buffer, batch->buffer_size);
|
---|
[6cb58e6] | 78 | }
|
---|
| 79 | } else {
|
---|
[01eeaaf] | 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,
|
---|
[6cb58e6] | 84 | actual_data_size);
|
---|
| 85 | } else {
|
---|
[01eeaaf] | 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);
|
---|
[6cb58e6] | 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | return rc;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[01eeaaf] | 96 | static int process_transfer_remote(usb_transfer_batch_t *batch,
|
---|
[79ae36dd] | 97 | async_sess_t *sess, size_t *actual_data_size)
|
---|
[6cb58e6] | 98 | {
|
---|
| 99 | int rc;
|
---|
| 100 |
|
---|
[01eeaaf] | 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) {
|
---|
[79ae36dd] | 105 | rc = usbvirt_ipc_send_control_read(sess,
|
---|
[01eeaaf] | 106 | batch->setup_buffer, batch->setup_size,
|
---|
| 107 | batch->buffer, batch->buffer_size,
|
---|
[6cb58e6] | 108 | actual_data_size);
|
---|
| 109 | } else {
|
---|
[01eeaaf] | 110 | assert(dir == USB_DIRECTION_OUT);
|
---|
[79ae36dd] | 111 | rc = usbvirt_ipc_send_control_write(sess,
|
---|
[01eeaaf] | 112 | batch->setup_buffer, batch->setup_size,
|
---|
| 113 | batch->buffer, batch->buffer_size);
|
---|
[6cb58e6] | 114 | }
|
---|
| 115 | } else {
|
---|
[01eeaaf] | 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,
|
---|
[6cb58e6] | 120 | actual_data_size);
|
---|
| 121 | } else {
|
---|
[01eeaaf] | 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);
|
---|
[6cb58e6] | 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | return rc;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[e913cc9] | 132 | static 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 |
|
---|
[b72efe8] | 137 | vhc_transfer_t *transfer = list_get_instance(
|
---|
| 138 | list_first(&dev->transfer_queue), vhc_transfer_t, link);
|
---|
[e913cc9] | 139 | list_remove(&transfer->link);
|
---|
| 140 |
|
---|
| 141 | return transfer;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | static void execute_transfer_callback_and_free(vhc_transfer_t *transfer,
|
---|
| 145 | size_t data_transfer_size, int outcome)
|
---|
| 146 | {
|
---|
| 147 | assert(outcome != ENAK);
|
---|
[01eeaaf] | 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);
|
---|
[e913cc9] | 154 | }
|
---|
[6cb58e6] | 155 |
|
---|
[f5f0cfb] | 156 | int 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 |
|
---|
| 165 | int vhc_schedule(hcd_t *hcd, usb_transfer_batch_t *batch)
|
---|
| 166 | {
|
---|
| 167 | assert(hcd);
|
---|
| 168 | assert(batch);
|
---|
[b5f813c] | 169 | vhc_data_t *vhc = hcd_get_driver_data(hcd);
|
---|
[f5f0cfb] | 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 |
|
---|
[3f03199] | 182 | list_foreach(vhc->devices, link, vhc_virtdev_t, dev) {
|
---|
[f5f0cfb] | 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 |
|
---|
[6cb58e6] | 201 | int 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 |
|
---|
[e913cc9] | 213 | vhc_transfer_t *transfer = dequeue_first_transfer(dev);
|
---|
[6cb58e6] | 214 | fibril_mutex_unlock(&dev->guard);
|
---|
| 215 |
|
---|
| 216 | int rc = EOK;
|
---|
| 217 | size_t data_transfer_size = 0;
|
---|
[79ae36dd] | 218 | if (dev->dev_sess) {
|
---|
[01eeaaf] | 219 | rc = process_transfer_remote(transfer->batch,
|
---|
| 220 | dev->dev_sess, &data_transfer_size);
|
---|
[6cb58e6] | 221 | } else if (dev->dev_local != NULL) {
|
---|
[01eeaaf] | 222 | rc = process_transfer_local(transfer->batch,
|
---|
| 223 | dev->dev_local, &data_transfer_size);
|
---|
[6cb58e6] | 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)) {
|
---|
[58563585] | 235 | usb_device_request_setup_packet_t *setup =
|
---|
| 236 | (void*) transfer->batch->setup_buffer;
|
---|
[6cb58e6] | 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) {
|
---|
[e913cc9] | 251 | execute_transfer_callback_and_free(transfer,
|
---|
| 252 | data_transfer_size, rc);
|
---|
[6cb58e6] | 253 | }
|
---|
| 254 |
|
---|
| 255 | async_usleep(1000 * 100);
|
---|
| 256 | fibril_mutex_lock(&dev->guard);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[e913cc9] | 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 | }
|
---|
[6cb58e6] | 264 |
|
---|
[e913cc9] | 265 | fibril_mutex_unlock(&dev->guard);
|
---|
[6cb58e6] | 266 |
|
---|
| 267 | return EOK;
|
---|
| 268 | }
|
---|