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