[bc9a629] | 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
|
---|
[b371844] | 33 | * @brief Virtual device management (implementation).
|
---|
[bc9a629] | 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>
|
---|
| 42 | #include <errno.h>
|
---|
| 43 | #include <str_error.h>
|
---|
| 44 |
|
---|
[47e3a8e] | 45 | #include <usbvirt/hub.h>
|
---|
[bc9a629] | 46 |
|
---|
| 47 | #include "devices.h"
|
---|
[b8507a1] | 48 | #include "hub.h"
|
---|
[774afaae] | 49 | #include "hub/virthub.h"
|
---|
[b8a3cda] | 50 | #include "vhcd.h"
|
---|
[bc9a629] | 51 |
|
---|
| 52 | #define list_foreach(pos, head) \
|
---|
| 53 | for (pos = (head)->next; pos != (head); \
|
---|
| 54 | pos = pos->next)
|
---|
| 55 |
|
---|
[63b4f90] | 56 | static LIST_INITIALIZE(devices);
|
---|
[bc9a629] | 57 |
|
---|
[b371844] | 58 | /** Create virtual device.
|
---|
| 59 | *
|
---|
| 60 | * @param phone Callback phone.
|
---|
| 61 | * @return New device.
|
---|
[3b77628] | 62 | * @retval NULL Out of memory.
|
---|
[b371844] | 63 | */
|
---|
[47e3a8e] | 64 | virtdev_connection_t *virtdev_add_device(int phone)
|
---|
[bc9a629] | 65 | {
|
---|
| 66 | virtdev_connection_t *dev = (virtdev_connection_t *)
|
---|
| 67 | malloc(sizeof(virtdev_connection_t));
|
---|
[3b77628] | 68 | if (dev == NULL) {
|
---|
| 69 | return NULL;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[bc9a629] | 72 | dev->phone = phone;
|
---|
| 73 | list_append(&dev->link, &devices);
|
---|
| 74 |
|
---|
[774afaae] | 75 | virthub_connect_device(&virtual_hub_device, dev);
|
---|
[b8507a1] | 76 |
|
---|
[bc9a629] | 77 | return dev;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[b371844] | 80 | /** Destroy virtual device.
|
---|
| 81 | */
|
---|
| 82 | void virtdev_destroy_device(virtdev_connection_t *dev)
|
---|
[bc9a629] | 83 | {
|
---|
[774afaae] | 84 | virthub_disconnect_device(&virtual_hub_device, dev);
|
---|
[bc9a629] | 85 | list_remove(&dev->link);
|
---|
| 86 | free(dev);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[47e3a8e] | 89 | /** Send data to all connected devices.
|
---|
| 90 | *
|
---|
| 91 | * @param transaction Transaction to be sent over the bus.
|
---|
| 92 | */
|
---|
| 93 | usb_transaction_outcome_t virtdev_send_to_all(transaction_t *transaction)
|
---|
| 94 | {
|
---|
[4f380d06] | 95 | /* For easier debugging. */
|
---|
| 96 | switch (transaction->type) {
|
---|
| 97 | case USBVIRT_TRANSACTION_SETUP:
|
---|
| 98 | case USBVIRT_TRANSACTION_OUT:
|
---|
| 99 | transaction->actual_len = transaction->len;
|
---|
| 100 | break;
|
---|
| 101 | case USBVIRT_TRANSACTION_IN:
|
---|
| 102 | transaction->actual_len = 0;
|
---|
| 103 | break;
|
---|
| 104 | default:
|
---|
| 105 | assert(false && "unreachable branch in switch()");
|
---|
| 106 | }
|
---|
| 107 | usb_transaction_outcome_t outcome = USB_OUTCOME_BABBLE;
|
---|
| 108 |
|
---|
[47e3a8e] | 109 | link_t *pos;
|
---|
| 110 | list_foreach(pos, &devices) {
|
---|
| 111 | virtdev_connection_t *dev
|
---|
| 112 | = list_get_instance(pos, virtdev_connection_t, link);
|
---|
| 113 |
|
---|
[774afaae] | 114 | if (!virthub_is_device_enabled(&virtual_hub_device, dev)) {
|
---|
[b8507a1] | 115 | continue;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[47e3a8e] | 118 | ipc_call_t answer_data;
|
---|
[a9b6bec] | 119 | sysarg_t answer_rc;
|
---|
[47e3a8e] | 120 | aid_t req;
|
---|
[b8a3cda] | 121 | int rc = EOK;
|
---|
[7a7bfeb3] | 122 | int method = IPC_M_USBVIRT_TRANSACTION_SETUP;
|
---|
[47e3a8e] | 123 |
|
---|
[7a7bfeb3] | 124 | switch (transaction->type) {
|
---|
| 125 | case USBVIRT_TRANSACTION_SETUP:
|
---|
| 126 | method = IPC_M_USBVIRT_TRANSACTION_SETUP;
|
---|
| 127 | break;
|
---|
| 128 | case USBVIRT_TRANSACTION_IN:
|
---|
| 129 | method = IPC_M_USBVIRT_TRANSACTION_IN;
|
---|
| 130 | break;
|
---|
| 131 | case USBVIRT_TRANSACTION_OUT:
|
---|
| 132 | method = IPC_M_USBVIRT_TRANSACTION_OUT;
|
---|
| 133 | break;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | req = async_send_3(dev->phone,
|
---|
| 137 | method,
|
---|
[47e3a8e] | 138 | transaction->target.address,
|
---|
| 139 | transaction->target.endpoint,
|
---|
[b8a3cda] | 140 | transaction->len,
|
---|
[47e3a8e] | 141 | &answer_data);
|
---|
| 142 |
|
---|
[b8a3cda] | 143 | if (transaction->len > 0) {
|
---|
[7a7bfeb3] | 144 | if (transaction->type == USBVIRT_TRANSACTION_IN) {
|
---|
| 145 | rc = async_data_read_start(dev->phone,
|
---|
| 146 | transaction->buffer, transaction->len);
|
---|
| 147 | } else {
|
---|
| 148 | rc = async_data_write_start(dev->phone,
|
---|
| 149 | transaction->buffer, transaction->len);
|
---|
| 150 | }
|
---|
[b8a3cda] | 151 | }
|
---|
[7a7bfeb3] | 152 |
|
---|
[47e3a8e] | 153 | if (rc != EOK) {
|
---|
| 154 | async_wait_for(req, NULL);
|
---|
| 155 | } else {
|
---|
| 156 | async_wait_for(req, &answer_rc);
|
---|
[13101d06] | 157 | transaction->actual_len = IPC_GET_ARG1(answer_data);
|
---|
[47e3a8e] | 158 | rc = (int)answer_rc;
|
---|
| 159 | }
|
---|
[4f380d06] | 160 |
|
---|
| 161 | /*
|
---|
| 162 | * If at least one device was able to accept this
|
---|
| 163 | * transaction and process it, we can announce success.
|
---|
| 164 | */
|
---|
| 165 | if (rc == EOK) {
|
---|
| 166 | outcome = USB_OUTCOME_OK;
|
---|
| 167 | }
|
---|
[47e3a8e] | 168 | }
|
---|
| 169 |
|
---|
[b8507a1] | 170 | /*
|
---|
| 171 | * Send the data to the virtual hub as well
|
---|
| 172 | * (if the address matches).
|
---|
| 173 | */
|
---|
[774afaae] | 174 | if (virtual_hub_device.address == transaction->target.address) {
|
---|
[7a7bfeb3] | 175 | size_t tmp;
|
---|
[9223dc5c] | 176 | dprintf(1, "sending `%s' transaction to hub",
|
---|
[2185776] | 177 | usbvirt_str_transaction_type(transaction->type));
|
---|
[7a7bfeb3] | 178 | switch (transaction->type) {
|
---|
| 179 | case USBVIRT_TRANSACTION_SETUP:
|
---|
[774afaae] | 180 | virtual_hub_device.transaction_setup(
|
---|
| 181 | &virtual_hub_device,
|
---|
[7a7bfeb3] | 182 | transaction->target.endpoint,
|
---|
| 183 | transaction->buffer, transaction->len);
|
---|
| 184 | break;
|
---|
| 185 |
|
---|
| 186 | case USBVIRT_TRANSACTION_IN:
|
---|
[774afaae] | 187 | virtual_hub_device.transaction_in(
|
---|
| 188 | &virtual_hub_device,
|
---|
[7a7bfeb3] | 189 | transaction->target.endpoint,
|
---|
| 190 | transaction->buffer, transaction->len,
|
---|
| 191 | &tmp);
|
---|
[4a5e2f1] | 192 | transaction->actual_len = tmp;
|
---|
[7a7bfeb3] | 193 | break;
|
---|
| 194 |
|
---|
| 195 | case USBVIRT_TRANSACTION_OUT:
|
---|
[774afaae] | 196 | virtual_hub_device.transaction_out(
|
---|
| 197 | &virtual_hub_device,
|
---|
[7a7bfeb3] | 198 | transaction->target.endpoint,
|
---|
| 199 | transaction->buffer, transaction->len);
|
---|
| 200 | break;
|
---|
| 201 | }
|
---|
[2185776] | 202 | dprintf(4, "transaction on hub processed...");
|
---|
[4f380d06] | 203 | outcome = USB_OUTCOME_OK;
|
---|
[b8507a1] | 204 | }
|
---|
| 205 |
|
---|
[47e3a8e] | 206 | /*
|
---|
| 207 | * TODO: maybe screw some transactions to get more
|
---|
| 208 | * real-life image.
|
---|
| 209 | */
|
---|
[4f380d06] | 210 | return outcome;
|
---|
[47e3a8e] | 211 | }
|
---|
| 212 |
|
---|
[bc9a629] | 213 | /**
|
---|
| 214 | * @}
|
---|
| 215 | */
|
---|