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