| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 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 | /** @addtogroup drvusbvhc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief Virtual HC (implementation).
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <adt/list.h>
|
|---|
| 37 | #include <bool.h>
|
|---|
| 38 | #include <async.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 40 | #include <stdio.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <str_error.h>
|
|---|
| 43 |
|
|---|
| 44 | #include <usbvirt/hub.h>
|
|---|
| 45 |
|
|---|
| 46 | #include "vhcd.h"
|
|---|
| 47 | #include "hc.h"
|
|---|
| 48 | #include "devices.h"
|
|---|
| 49 | #include "hub.h"
|
|---|
| 50 |
|
|---|
| 51 | #define USLEEP_BASE (0 * 5 * 1000)
|
|---|
| 52 |
|
|---|
| 53 | #define USLEEP_VAR 50
|
|---|
| 54 |
|
|---|
| 55 | #define SHORTENING_VAR 15
|
|---|
| 56 |
|
|---|
| 57 | #define PROB_OUTCOME_BABBLE 5
|
|---|
| 58 | #define PROB_OUTCOME_CRCERROR 7
|
|---|
| 59 |
|
|---|
| 60 | #define PROB_TEST(var, new_value, prob, number) \
|
|---|
| 61 | do { \
|
|---|
| 62 | if (((number) % (prob)) == 0) { \
|
|---|
| 63 | var = (new_value); \
|
|---|
| 64 | } \
|
|---|
| 65 | } while (0)
|
|---|
| 66 |
|
|---|
| 67 | static link_t transaction_list;
|
|---|
| 68 |
|
|---|
| 69 | #define TRANSACTION_FORMAT "T[%d.%d %s/%s (%d)]"
|
|---|
| 70 | #define TRANSACTION_PRINTF(t) \
|
|---|
| 71 | (t).target.address, (t).target.endpoint, \
|
|---|
| 72 | usb_str_transfer_type((t).transfer_type), \
|
|---|
| 73 | usbvirt_str_transaction_type((t).type), \
|
|---|
| 74 | (int)(t).len
|
|---|
| 75 |
|
|---|
| 76 | #define transaction_get_instance(lnk) \
|
|---|
| 77 | list_get_instance(lnk, transaction_t, link)
|
|---|
| 78 |
|
|---|
| 79 | #define HUB_STATUS_MAX_LEN (HUB_PORT_COUNT + 64)
|
|---|
| 80 |
|
|---|
| 81 | static inline unsigned int pseudo_random(unsigned int *seed)
|
|---|
| 82 | {
|
|---|
| 83 | *seed = ((*seed) * 873511) % 22348977 + 7;
|
|---|
| 84 | return ((*seed) >> 8);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /** Call transaction callback.
|
|---|
| 88 | * Calling this callback informs the backend that transaction was processed.
|
|---|
| 89 | */
|
|---|
| 90 | static void process_transaction_with_outcome(transaction_t * transaction,
|
|---|
| 91 | int outcome)
|
|---|
| 92 | {
|
|---|
| 93 | usb_log_debug2("Transaction " TRANSACTION_FORMAT " done: %s.\n",
|
|---|
| 94 | TRANSACTION_PRINTF(*transaction),
|
|---|
| 95 | str_error(outcome));
|
|---|
| 96 |
|
|---|
| 97 | transaction->callback(transaction->buffer, transaction->actual_len,
|
|---|
| 98 | outcome, transaction->callback_arg);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /** Host controller manager main function.
|
|---|
| 102 | */
|
|---|
| 103 | static int hc_manager_fibril(void *arg)
|
|---|
| 104 | {
|
|---|
| 105 | list_initialize(&transaction_list);
|
|---|
| 106 |
|
|---|
| 107 | static unsigned int seed = 4573;
|
|---|
| 108 |
|
|---|
| 109 | usb_log_info("Transaction processor ready.\n");
|
|---|
| 110 |
|
|---|
| 111 | while (true) {
|
|---|
| 112 | async_usleep(USLEEP_BASE + (pseudo_random(&seed) % USLEEP_VAR));
|
|---|
| 113 |
|
|---|
| 114 | if (list_empty(&transaction_list)) {
|
|---|
| 115 | continue;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | char ports[HUB_STATUS_MAX_LEN + 1];
|
|---|
| 119 | virthub_get_status(&virtual_hub_device, ports, HUB_STATUS_MAX_LEN);
|
|---|
| 120 |
|
|---|
| 121 | link_t *first_transaction_link = transaction_list.next;
|
|---|
| 122 | transaction_t *transaction
|
|---|
| 123 | = transaction_get_instance(first_transaction_link);
|
|---|
| 124 | list_remove(first_transaction_link);
|
|---|
| 125 |
|
|---|
| 126 | usb_log_debug("Processing " TRANSACTION_FORMAT " [%s].\n",
|
|---|
| 127 | TRANSACTION_PRINTF(*transaction), ports);
|
|---|
| 128 |
|
|---|
| 129 | int outcome;
|
|---|
| 130 | outcome = virtdev_send_to_all(transaction);
|
|---|
| 131 |
|
|---|
| 132 | process_transaction_with_outcome(transaction, outcome);
|
|---|
| 133 |
|
|---|
| 134 | free(transaction);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | assert(false && "unreachable");
|
|---|
| 138 | return EOK;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | void hc_manager(void)
|
|---|
| 142 | {
|
|---|
| 143 | fid_t fid = fibril_create(hc_manager_fibril, NULL);
|
|---|
| 144 | if (fid == 0) {
|
|---|
| 145 | usb_log_fatal("Failed to start HC manager fibril.\n");
|
|---|
| 146 | return;
|
|---|
| 147 | }
|
|---|
| 148 | fibril_add_ready(fid);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /** Create new transaction
|
|---|
| 152 | */
|
|---|
| 153 | static transaction_t *transaction_create(usbvirt_transaction_type_t type,
|
|---|
| 154 | usb_target_t target, usb_transfer_type_t transfer_type,
|
|---|
| 155 | void * buffer, size_t len,
|
|---|
| 156 | hc_transaction_done_callback_t callback, void * arg)
|
|---|
| 157 | {
|
|---|
| 158 | transaction_t * transaction = malloc(sizeof(transaction_t));
|
|---|
| 159 |
|
|---|
| 160 | list_initialize(&transaction->link);
|
|---|
| 161 | transaction->type = type;
|
|---|
| 162 | transaction->transfer_type = transfer_type;
|
|---|
| 163 | transaction->target = target;
|
|---|
| 164 | transaction->buffer = buffer;
|
|---|
| 165 | transaction->len = len;
|
|---|
| 166 | transaction->actual_len = len;
|
|---|
| 167 | transaction->callback = callback;
|
|---|
| 168 | transaction->callback_arg = arg;
|
|---|
| 169 |
|
|---|
| 170 | return transaction;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | static void hc_add_transaction(transaction_t *transaction)
|
|---|
| 174 | {
|
|---|
| 175 | usb_log_debug("Adding transaction " TRANSACTION_FORMAT ".\n",
|
|---|
| 176 | TRANSACTION_PRINTF(*transaction));
|
|---|
| 177 | list_append(&transaction->link, &transaction_list);
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | /** Add transaction directioned towards the device.
|
|---|
| 181 | */
|
|---|
| 182 | void hc_add_transaction_to_device(bool setup, usb_target_t target,
|
|---|
| 183 | usb_transfer_type_t transfer_type,
|
|---|
| 184 | void * buffer, size_t len,
|
|---|
| 185 | hc_transaction_done_callback_t callback, void * arg)
|
|---|
| 186 | {
|
|---|
| 187 | transaction_t *transaction = transaction_create(
|
|---|
| 188 | setup ? USBVIRT_TRANSACTION_SETUP : USBVIRT_TRANSACTION_OUT,
|
|---|
| 189 | target, transfer_type,
|
|---|
| 190 | buffer, len, callback, arg);
|
|---|
| 191 | hc_add_transaction(transaction);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /** Add transaction directioned from the device.
|
|---|
| 195 | */
|
|---|
| 196 | void hc_add_transaction_from_device(usb_target_t target,
|
|---|
| 197 | usb_transfer_type_t transfer_type,
|
|---|
| 198 | void * buffer, size_t len,
|
|---|
| 199 | hc_transaction_done_callback_t callback, void * arg)
|
|---|
| 200 | {
|
|---|
| 201 | transaction_t *transaction = transaction_create(USBVIRT_TRANSACTION_IN,
|
|---|
| 202 | target, transfer_type,
|
|---|
| 203 | buffer, len, callback, arg);
|
|---|
| 204 | hc_add_transaction(transaction);
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /**
|
|---|
| 208 | * @}
|
|---|
| 209 | */
|
|---|