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