| 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 device management (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>
|
|---|
| 42 | #include <errno.h>
|
|---|
| 43 | #include <str_error.h>
|
|---|
| 44 |
|
|---|
| 45 | #include <usbvirt/hub.h>
|
|---|
| 46 |
|
|---|
| 47 | #include "devices.h"
|
|---|
| 48 | #include "hub.h"
|
|---|
| 49 | #include "hub/virthub.h"
|
|---|
| 50 | #include "vhcd.h"
|
|---|
| 51 |
|
|---|
| 52 | #define list_foreach(pos, head) \
|
|---|
| 53 | for (pos = (head)->next; pos != (head); \
|
|---|
| 54 | pos = pos->next)
|
|---|
| 55 |
|
|---|
| 56 | static LIST_INITIALIZE(devices);
|
|---|
| 57 |
|
|---|
| 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 | */
|
|---|
| 65 | virtdev_connection_t *virtdev_add_device(int phone)
|
|---|
| 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 |
|
|---|
| 72 | virthub_connect_device(&virtual_hub_device, dev);
|
|---|
| 73 |
|
|---|
| 74 | return dev;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /** Destroy virtual device.
|
|---|
| 78 | */
|
|---|
| 79 | void virtdev_destroy_device(virtdev_connection_t *dev)
|
|---|
| 80 | {
|
|---|
| 81 | virthub_disconnect_device(&virtual_hub_device, dev);
|
|---|
| 82 | list_remove(&dev->link);
|
|---|
| 83 | free(dev);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 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 |
|
|---|
| 97 | if (!virthub_is_device_enabled(&virtual_hub_device, dev)) {
|
|---|
| 98 | continue;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | ipc_call_t answer_data;
|
|---|
| 102 | sysarg_t answer_rc;
|
|---|
| 103 | aid_t req;
|
|---|
| 104 | int rc = EOK;
|
|---|
| 105 | int method = IPC_M_USBVIRT_TRANSACTION_SETUP;
|
|---|
| 106 |
|
|---|
| 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,
|
|---|
| 121 | transaction->target.address,
|
|---|
| 122 | transaction->target.endpoint,
|
|---|
| 123 | transaction->len,
|
|---|
| 124 | &answer_data);
|
|---|
| 125 |
|
|---|
| 126 | if (transaction->len > 0) {
|
|---|
| 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 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | if (rc != EOK) {
|
|---|
| 137 | async_wait_for(req, NULL);
|
|---|
| 138 | } else {
|
|---|
| 139 | async_wait_for(req, &answer_rc);
|
|---|
| 140 | rc = (int)answer_rc;
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /*
|
|---|
| 145 | * Send the data to the virtual hub as well
|
|---|
| 146 | * (if the address matches).
|
|---|
| 147 | */
|
|---|
| 148 | if (virtual_hub_device.address == transaction->target.address) {
|
|---|
| 149 | size_t tmp;
|
|---|
| 150 | dprintf(1, "sending `%s' transaction to hub",
|
|---|
| 151 | usbvirt_str_transaction_type(transaction->type));
|
|---|
| 152 | switch (transaction->type) {
|
|---|
| 153 | case USBVIRT_TRANSACTION_SETUP:
|
|---|
| 154 | virtual_hub_device.transaction_setup(
|
|---|
| 155 | &virtual_hub_device,
|
|---|
| 156 | transaction->target.endpoint,
|
|---|
| 157 | transaction->buffer, transaction->len);
|
|---|
| 158 | break;
|
|---|
| 159 |
|
|---|
| 160 | case USBVIRT_TRANSACTION_IN:
|
|---|
| 161 | virtual_hub_device.transaction_in(
|
|---|
| 162 | &virtual_hub_device,
|
|---|
| 163 | transaction->target.endpoint,
|
|---|
| 164 | transaction->buffer, transaction->len,
|
|---|
| 165 | &tmp);
|
|---|
| 166 | if (tmp < transaction->len) {
|
|---|
| 167 | transaction->len = tmp;
|
|---|
| 168 | }
|
|---|
| 169 | break;
|
|---|
| 170 |
|
|---|
| 171 | case USBVIRT_TRANSACTION_OUT:
|
|---|
| 172 | virtual_hub_device.transaction_out(
|
|---|
| 173 | &virtual_hub_device,
|
|---|
| 174 | transaction->target.endpoint,
|
|---|
| 175 | transaction->buffer, transaction->len);
|
|---|
| 176 | break;
|
|---|
| 177 | }
|
|---|
| 178 | dprintf(4, "transaction on hub processed...");
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /*
|
|---|
| 182 | * TODO: maybe screw some transactions to get more
|
|---|
| 183 | * real-life image.
|
|---|
| 184 | */
|
|---|
| 185 | return USB_OUTCOME_OK;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | /**
|
|---|
| 189 | * @}
|
|---|
| 190 | */
|
|---|