[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 address USB address.
|
---|
| 61 | * @param phone Callback phone.
|
---|
| 62 | * @return New device.
|
---|
| 63 | * @retval NULL Out of memory or address already occupied.
|
---|
| 64 | */
|
---|
[47e3a8e] | 65 | virtdev_connection_t *virtdev_add_device(int phone)
|
---|
[bc9a629] | 66 | {
|
---|
| 67 | virtdev_connection_t *dev = (virtdev_connection_t *)
|
---|
| 68 | malloc(sizeof(virtdev_connection_t));
|
---|
| 69 | dev->phone = phone;
|
---|
| 70 | list_append(&dev->link, &devices);
|
---|
| 71 |
|
---|
[774afaae] | 72 | virthub_connect_device(&virtual_hub_device, dev);
|
---|
[b8507a1] | 73 |
|
---|
[bc9a629] | 74 | return dev;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[b371844] | 77 | /** Destroy virtual device.
|
---|
| 78 | */
|
---|
| 79 | void virtdev_destroy_device(virtdev_connection_t *dev)
|
---|
[bc9a629] | 80 | {
|
---|
[774afaae] | 81 | virthub_disconnect_device(&virtual_hub_device, dev);
|
---|
[bc9a629] | 82 | list_remove(&dev->link);
|
---|
| 83 | free(dev);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[47e3a8e] | 86 | /** Send data to all connected devices.
|
---|
| 87 | *
|
---|
| 88 | * @param transaction Transaction to be sent over the bus.
|
---|
| 89 | */
|
---|
| 90 | usb_transaction_outcome_t virtdev_send_to_all(transaction_t *transaction)
|
---|
| 91 | {
|
---|
| 92 | link_t *pos;
|
---|
| 93 | list_foreach(pos, &devices) {
|
---|
| 94 | virtdev_connection_t *dev
|
---|
| 95 | = list_get_instance(pos, virtdev_connection_t, link);
|
---|
| 96 |
|
---|
[774afaae] | 97 | if (!virthub_is_device_enabled(&virtual_hub_device, dev)) {
|
---|
[b8507a1] | 98 | continue;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[47e3a8e] | 101 | ipc_call_t answer_data;
|
---|
[a9b6bec] | 102 | sysarg_t answer_rc;
|
---|
[47e3a8e] | 103 | aid_t req;
|
---|
[b8a3cda] | 104 | int rc = EOK;
|
---|
[7a7bfeb3] | 105 | int method = IPC_M_USBVIRT_TRANSACTION_SETUP;
|
---|
[47e3a8e] | 106 |
|
---|
[7a7bfeb3] | 107 | switch (transaction->type) {
|
---|
| 108 | case USBVIRT_TRANSACTION_SETUP:
|
---|
| 109 | method = IPC_M_USBVIRT_TRANSACTION_SETUP;
|
---|
| 110 | break;
|
---|
| 111 | case USBVIRT_TRANSACTION_IN:
|
---|
| 112 | method = IPC_M_USBVIRT_TRANSACTION_IN;
|
---|
| 113 | break;
|
---|
| 114 | case USBVIRT_TRANSACTION_OUT:
|
---|
| 115 | method = IPC_M_USBVIRT_TRANSACTION_OUT;
|
---|
| 116 | break;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | req = async_send_3(dev->phone,
|
---|
| 120 | method,
|
---|
[47e3a8e] | 121 | transaction->target.address,
|
---|
| 122 | transaction->target.endpoint,
|
---|
[b8a3cda] | 123 | transaction->len,
|
---|
[47e3a8e] | 124 | &answer_data);
|
---|
| 125 |
|
---|
[b8a3cda] | 126 | if (transaction->len > 0) {
|
---|
[7a7bfeb3] | 127 | if (transaction->type == USBVIRT_TRANSACTION_IN) {
|
---|
| 128 | rc = async_data_read_start(dev->phone,
|
---|
| 129 | transaction->buffer, transaction->len);
|
---|
| 130 | } else {
|
---|
| 131 | rc = async_data_write_start(dev->phone,
|
---|
| 132 | transaction->buffer, transaction->len);
|
---|
| 133 | }
|
---|
[b8a3cda] | 134 | }
|
---|
[7a7bfeb3] | 135 |
|
---|
[47e3a8e] | 136 | if (rc != EOK) {
|
---|
| 137 | async_wait_for(req, NULL);
|
---|
| 138 | } else {
|
---|
| 139 | async_wait_for(req, &answer_rc);
|
---|
[13101d06] | 140 | transaction->actual_len = IPC_GET_ARG1(answer_data);
|
---|
[47e3a8e] | 141 | rc = (int)answer_rc;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[b8507a1] | 145 | /*
|
---|
| 146 | * Send the data to the virtual hub as well
|
---|
| 147 | * (if the address matches).
|
---|
| 148 | */
|
---|
[774afaae] | 149 | if (virtual_hub_device.address == transaction->target.address) {
|
---|
[7a7bfeb3] | 150 | size_t tmp;
|
---|
[9223dc5c] | 151 | dprintf(1, "sending `%s' transaction to hub",
|
---|
[2185776] | 152 | usbvirt_str_transaction_type(transaction->type));
|
---|
[7a7bfeb3] | 153 | switch (transaction->type) {
|
---|
| 154 | case USBVIRT_TRANSACTION_SETUP:
|
---|
[774afaae] | 155 | virtual_hub_device.transaction_setup(
|
---|
| 156 | &virtual_hub_device,
|
---|
[7a7bfeb3] | 157 | transaction->target.endpoint,
|
---|
| 158 | transaction->buffer, transaction->len);
|
---|
| 159 | break;
|
---|
| 160 |
|
---|
| 161 | case USBVIRT_TRANSACTION_IN:
|
---|
[774afaae] | 162 | virtual_hub_device.transaction_in(
|
---|
| 163 | &virtual_hub_device,
|
---|
[7a7bfeb3] | 164 | transaction->target.endpoint,
|
---|
| 165 | transaction->buffer, transaction->len,
|
---|
| 166 | &tmp);
|
---|
| 167 | if (tmp < transaction->len) {
|
---|
| 168 | transaction->len = tmp;
|
---|
| 169 | }
|
---|
| 170 | break;
|
---|
| 171 |
|
---|
| 172 | case USBVIRT_TRANSACTION_OUT:
|
---|
[774afaae] | 173 | virtual_hub_device.transaction_out(
|
---|
| 174 | &virtual_hub_device,
|
---|
[7a7bfeb3] | 175 | transaction->target.endpoint,
|
---|
| 176 | transaction->buffer, transaction->len);
|
---|
| 177 | break;
|
---|
| 178 | }
|
---|
[2185776] | 179 | dprintf(4, "transaction on hub processed...");
|
---|
[b8507a1] | 180 | }
|
---|
| 181 |
|
---|
[47e3a8e] | 182 | /*
|
---|
| 183 | * TODO: maybe screw some transactions to get more
|
---|
| 184 | * real-life image.
|
---|
| 185 | */
|
---|
| 186 | return USB_OUTCOME_OK;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[bc9a629] | 189 | /**
|
---|
| 190 | * @}
|
---|
| 191 | */
|
---|