[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 |
|
---|
| 29 | /** @addtogroup usb
|
---|
| 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 |
|
---|
| 52 | #define USLEEP_BASE (500 * 1000)
|
---|
| 53 |
|
---|
[b8a3cda] | 54 | #define USLEEP_VAR 5000
|
---|
[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 |
|
---|
[355f7c2] | 70 | #define TRANSACTION_FORMAT "T[%d:%d %s (%d)]"
|
---|
[c0e1be7] | 71 | #define TRANSACTION_PRINTF(t) \
|
---|
| 72 | (t).target.address, (t).target.endpoint, \
|
---|
[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 |
|
---|
| 79 | static inline unsigned int pseudo_random(unsigned int *seed)
|
---|
| 80 | {
|
---|
| 81 | *seed = ((*seed) * 873511) % 22348977 + 7;
|
---|
| 82 | return ((*seed) >> 8);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[b371844] | 85 | /** Call transaction callback.
|
---|
| 86 | * Calling this callback informs the backend that transaction was processed.
|
---|
| 87 | */
|
---|
[c0e1be7] | 88 | static void process_transaction_with_outcome(transaction_t * transaction,
|
---|
| 89 | usb_transaction_outcome_t outcome)
|
---|
| 90 | {
|
---|
[355f7c2] | 91 | dprintf(3, "processing transaction " TRANSACTION_FORMAT ", outcome: %s",
|
---|
[c0e1be7] | 92 | TRANSACTION_PRINTF(*transaction),
|
---|
| 93 | usb_str_transaction_outcome(outcome));
|
---|
| 94 |
|
---|
| 95 | transaction->callback(transaction->buffer, transaction->len, outcome,
|
---|
| 96 | transaction->callback_arg);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[b371844] | 99 | /** Host controller manager main function.
|
---|
| 100 | */
|
---|
[c0e1be7] | 101 | void hc_manager(void)
|
---|
| 102 | {
|
---|
[7a7bfeb3] | 103 | list_initialize(&transaction_list);
|
---|
[c0e1be7] | 104 |
|
---|
| 105 | static unsigned int seed = 4573;
|
---|
| 106 |
|
---|
| 107 | printf("%s: transaction processor ready.\n", NAME);
|
---|
| 108 |
|
---|
| 109 | while (true) {
|
---|
| 110 | async_usleep(USLEEP_BASE + (pseudo_random(&seed) % USLEEP_VAR));
|
---|
| 111 |
|
---|
[7a7bfeb3] | 112 | if (list_empty(&transaction_list)) {
|
---|
[c0e1be7] | 113 | continue;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[355f7c2] | 116 | char ports[HUB_PORT_COUNT + 2];
|
---|
| 117 | hub_get_port_statuses(ports, HUB_PORT_COUNT + 1);
|
---|
| 118 | dprintf(3, "virtual hub: addr=%d ports=%s",
|
---|
| 119 | virthub_dev.address, ports);
|
---|
[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 |
|
---|
[355f7c2] | 126 | dprintf(3, "processing transaction " TRANSACTION_FORMAT "",
|
---|
[7a7bfeb3] | 127 | TRANSACTION_PRINTF(*transaction));
|
---|
| 128 |
|
---|
[47e3a8e] | 129 | usb_transaction_outcome_t outcome;
|
---|
| 130 | outcome = virtdev_send_to_all(transaction);
|
---|
[c0e1be7] | 131 |
|
---|
[b371844] | 132 | process_transaction_with_outcome(transaction, outcome);
|
---|
| 133 |
|
---|
[bc9a629] | 134 | free(transaction);
|
---|
[c0e1be7] | 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[b371844] | 138 | /** Create new transaction
|
---|
| 139 | */
|
---|
[b8a3cda] | 140 | static transaction_t *transaction_create(usbvirt_transaction_type_t type,
|
---|
| 141 | usb_target_t target,
|
---|
[c0e1be7] | 142 | void * buffer, size_t len,
|
---|
| 143 | hc_transaction_done_callback_t callback, void * arg)
|
---|
| 144 | {
|
---|
| 145 | transaction_t * transaction = malloc(sizeof(transaction_t));
|
---|
| 146 |
|
---|
| 147 | list_initialize(&transaction->link);
|
---|
| 148 | transaction->type = type;
|
---|
| 149 | transaction->target = target;
|
---|
| 150 | transaction->buffer = buffer;
|
---|
| 151 | transaction->len = len;
|
---|
| 152 | transaction->callback = callback;
|
---|
| 153 | transaction->callback_arg = arg;
|
---|
| 154 |
|
---|
[355f7c2] | 155 | dprintf(1, "creating transaction " TRANSACTION_FORMAT,
|
---|
| 156 | TRANSACTION_PRINTF(*transaction));
|
---|
| 157 |
|
---|
[bc9a629] | 158 | return transaction;
|
---|
[c0e1be7] | 159 | }
|
---|
| 160 |
|
---|
[b371844] | 161 | /** Add transaction directioned towards the device.
|
---|
| 162 | */
|
---|
[b8a3cda] | 163 | void hc_add_transaction_to_device(bool setup, usb_target_t target,
|
---|
[c0e1be7] | 164 | void * buffer, size_t len,
|
---|
| 165 | hc_transaction_done_callback_t callback, void * arg)
|
---|
| 166 | {
|
---|
[b8a3cda] | 167 | transaction_t *transaction = transaction_create(
|
---|
| 168 | setup ? USBVIRT_TRANSACTION_SETUP : USBVIRT_TRANSACTION_OUT, target,
|
---|
| 169 | buffer, len, callback, arg);
|
---|
[7a7bfeb3] | 170 | list_append(&transaction->link, &transaction_list);
|
---|
[c0e1be7] | 171 | }
|
---|
| 172 |
|
---|
[b371844] | 173 | /** Add transaction directioned from the device.
|
---|
| 174 | */
|
---|
[b8a3cda] | 175 | void hc_add_transaction_from_device(usb_target_t target,
|
---|
[c0e1be7] | 176 | void * buffer, size_t len,
|
---|
| 177 | hc_transaction_done_callback_t callback, void * arg)
|
---|
| 178 | {
|
---|
[b8a3cda] | 179 | transaction_t *transaction = transaction_create(USBVIRT_TRANSACTION_IN,
|
---|
| 180 | target, buffer, len, callback, arg);
|
---|
[7a7bfeb3] | 181 | list_append(&transaction->link, &transaction_list);
|
---|
[c0e1be7] | 182 | }
|
---|
| 183 |
|
---|
| 184 | /**
|
---|
| 185 | * @}
|
---|
| 186 | */
|
---|