| 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 libusbvirt usb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief Device registration with virtual USB framework.
|
|---|
| 34 | */
|
|---|
| 35 | #include <devman.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <stdlib.h>
|
|---|
| 38 | #include <mem.h>
|
|---|
| 39 | #include <assert.h>
|
|---|
| 40 |
|
|---|
| 41 | #include "hub.h"
|
|---|
| 42 | #include "device.h"
|
|---|
| 43 | #include "private.h"
|
|---|
| 44 |
|
|---|
| 45 | #define NAMESPACE "usb"
|
|---|
| 46 |
|
|---|
| 47 | /** Virtual device wrapper. */
|
|---|
| 48 | typedef struct {
|
|---|
| 49 | /** Actual device. */
|
|---|
| 50 | usbvirt_device_t *device;
|
|---|
| 51 | /** Phone to host controller. */
|
|---|
| 52 | int vhcd_phone;
|
|---|
| 53 | /** Device id. */
|
|---|
| 54 | ipcarg_t id;
|
|---|
| 55 | /** Linked-list member. */
|
|---|
| 56 | link_t link;
|
|---|
| 57 | } virtual_device_t;
|
|---|
| 58 |
|
|---|
| 59 | /*** List of known device. */
|
|---|
| 60 | static LIST_INITIALIZE(device_list);
|
|---|
| 61 |
|
|---|
| 62 | /** Find virtual device wrapper based on the contents. */
|
|---|
| 63 | static virtual_device_t *find_device(usbvirt_device_t *device)
|
|---|
| 64 | {
|
|---|
| 65 | if (list_empty(&device_list)) {
|
|---|
| 66 | return NULL;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | link_t *pos;
|
|---|
| 70 | for (pos = device_list.next; pos != &device_list; pos = pos->next) {
|
|---|
| 71 | virtual_device_t *dev
|
|---|
| 72 | = list_get_instance(pos, virtual_device_t, link);
|
|---|
| 73 | if (dev->device == device) {
|
|---|
| 74 | return dev;
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | return NULL;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /** Find virtual device wrapper by its id. */
|
|---|
| 82 | static virtual_device_t *find_device_by_id(ipcarg_t id)
|
|---|
| 83 | {
|
|---|
| 84 | if (list_empty(&device_list)) {
|
|---|
| 85 | return NULL;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | link_t *pos;
|
|---|
| 89 | for (pos = device_list.next; pos != &device_list; pos = pos->next) {
|
|---|
| 90 | virtual_device_t *dev
|
|---|
| 91 | = list_get_instance(pos, virtual_device_t, link);
|
|---|
| 92 | if (dev->id == id) {
|
|---|
| 93 | return dev;
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | return NULL;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /** Reply to a control transfer. */
|
|---|
| 101 | static int control_transfer_reply(usbvirt_device_t *device,
|
|---|
| 102 | usb_endpoint_t endpoint, void *buffer, size_t size)
|
|---|
| 103 | {
|
|---|
| 104 | usbvirt_control_transfer_t *transfer = &device->current_control_transfers[endpoint];
|
|---|
| 105 | if (transfer->data != NULL) {
|
|---|
| 106 | free(transfer->data);
|
|---|
| 107 | }
|
|---|
| 108 | transfer->data = malloc(size);
|
|---|
| 109 | memcpy(transfer->data, buffer, size);
|
|---|
| 110 | transfer->data_size = size;
|
|---|
| 111 |
|
|---|
| 112 | return EOK;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /** Initialize virtual device. */
|
|---|
| 116 | static void device_init(usbvirt_device_t *dev)
|
|---|
| 117 | {
|
|---|
| 118 | dev->transaction_out = transaction_out;
|
|---|
| 119 | dev->transaction_setup = transaction_setup;
|
|---|
| 120 | dev->transaction_in = transaction_in;
|
|---|
| 121 |
|
|---|
| 122 | dev->control_transfer_reply = control_transfer_reply;
|
|---|
| 123 |
|
|---|
| 124 | dev->debug = user_debug;
|
|---|
| 125 | dev->lib_debug = lib_debug;
|
|---|
| 126 |
|
|---|
| 127 | dev->state = USBVIRT_STATE_DEFAULT;
|
|---|
| 128 | dev->address = 0;
|
|---|
| 129 | dev->new_address = -1;
|
|---|
| 130 |
|
|---|
| 131 | size_t i;
|
|---|
| 132 | for (i = 0; i < USB11_ENDPOINT_MAX; i++) {
|
|---|
| 133 | usbvirt_control_transfer_t *transfer = &dev->current_control_transfers[i];
|
|---|
| 134 | transfer->direction = 0;
|
|---|
| 135 | transfer->request = NULL;
|
|---|
| 136 | transfer->request_size = 0;
|
|---|
| 137 | transfer->data = NULL;
|
|---|
| 138 | transfer->data_size = 0;
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /** Add a virtual device.
|
|---|
| 143 | * The returned device (if not NULL) shall be destroy via destroy_device().
|
|---|
| 144 | */
|
|---|
| 145 | static virtual_device_t *add_device(usbvirt_device_t *dev)
|
|---|
| 146 | {
|
|---|
| 147 | assert(find_device(dev) == NULL);
|
|---|
| 148 | virtual_device_t *new_device
|
|---|
| 149 | = (virtual_device_t *) malloc(sizeof(virtual_device_t));
|
|---|
| 150 |
|
|---|
| 151 | new_device->device = dev;
|
|---|
| 152 | link_initialize(&new_device->link);
|
|---|
| 153 |
|
|---|
| 154 | list_append(&new_device->link, &device_list);
|
|---|
| 155 |
|
|---|
| 156 | return new_device;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /** Destroy virtual device. */
|
|---|
| 160 | static void destroy_device(virtual_device_t *dev)
|
|---|
| 161 | {
|
|---|
| 162 | if (dev->vhcd_phone > 0) {
|
|---|
| 163 | ipc_hangup(dev->vhcd_phone);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | list_remove(&dev->link);
|
|---|
| 167 |
|
|---|
| 168 | free(dev);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /** Callback connection handler. */
|
|---|
| 172 | static void callback_connection(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 173 | {
|
|---|
| 174 | // FIXME - determine which device just called back
|
|---|
| 175 | virtual_device_t *dev = find_device_by_id(0);
|
|---|
| 176 | if (dev == NULL) {
|
|---|
| 177 | ipc_answer_0(iid, EINVAL);
|
|---|
| 178 | printf("Ooops\n");
|
|---|
| 179 | return;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | device_callback_connection(dev->device, iid, icall);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | /** Create necessary phones for comunication with virtual HCD.
|
|---|
| 186 | * This function wraps following calls:
|
|---|
| 187 | * -# open <code>/dev/devices/\\vhc for reading
|
|---|
| 188 | * -# access phone of file opened in previous step
|
|---|
| 189 | * -# create callback through just opened phone
|
|---|
| 190 | * -# create handler for calling on data from host to function
|
|---|
| 191 | * -# return the (outgoing) phone
|
|---|
| 192 | *
|
|---|
| 193 | * @warning This function is wrapper for several actions and therefore
|
|---|
| 194 | * it is not possible - in case of error - to determine at which point
|
|---|
| 195 | * error occured.
|
|---|
| 196 | *
|
|---|
| 197 | * @param hcd_path HCD identification under devfs
|
|---|
| 198 | * (without <code>/dev/usb/</code>).
|
|---|
| 199 | * @param dev Device to connect.
|
|---|
| 200 | * @return EOK on success or error code from errno.h.
|
|---|
| 201 | */
|
|---|
| 202 | int usbvirt_connect(usbvirt_device_t *dev)
|
|---|
| 203 | {
|
|---|
| 204 | virtual_device_t *virtual_device = find_device(dev);
|
|---|
| 205 | if (virtual_device != NULL) {
|
|---|
| 206 | return EEXISTS;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | const char *vhc_path = "/vhc";
|
|---|
| 210 | int rc;
|
|---|
| 211 | devman_handle_t handle;
|
|---|
| 212 |
|
|---|
| 213 | rc = devman_device_get_handle(vhc_path, &handle, 0);
|
|---|
| 214 | if (rc != EOK) {
|
|---|
| 215 | printf("devman_device_get_handle() failed\n");
|
|---|
| 216 | return rc;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | int hcd_phone = devman_device_connect(handle, 0);
|
|---|
| 220 |
|
|---|
| 221 | if (hcd_phone < 0) {
|
|---|
| 222 | printf("devman_device_connect() failed\n");
|
|---|
| 223 | return hcd_phone;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | ipcarg_t phonehash;
|
|---|
| 227 | rc = ipc_connect_to_me(hcd_phone, 0, 0, 0, &phonehash);
|
|---|
| 228 | if (rc != EOK) {
|
|---|
| 229 | printf("ipc_connect_to_me() failed\n");
|
|---|
| 230 | return rc;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | device_init(dev);
|
|---|
| 234 |
|
|---|
| 235 | virtual_device = add_device(dev);
|
|---|
| 236 | virtual_device->vhcd_phone = hcd_phone;
|
|---|
| 237 | virtual_device->id = 0;
|
|---|
| 238 |
|
|---|
| 239 | async_new_connection(phonehash, 0, NULL, callback_connection);
|
|---|
| 240 |
|
|---|
| 241 | return EOK;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | /** Prepares device as local.
|
|---|
| 245 | * This is useful if you want to have a virtual device in the same task
|
|---|
| 246 | * as HCD.
|
|---|
| 247 | *
|
|---|
| 248 | * @param dev Device to connect.
|
|---|
| 249 | * @return Error code.
|
|---|
| 250 | * @retval EOK Device connected.
|
|---|
| 251 | * @retval EEXISTS This device is already connected.
|
|---|
| 252 | */
|
|---|
| 253 | int usbvirt_connect_local(usbvirt_device_t *dev)
|
|---|
| 254 | {
|
|---|
| 255 | virtual_device_t *virtual_device = find_device(dev);
|
|---|
| 256 | if (virtual_device != NULL) {
|
|---|
| 257 | return EEXISTS;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | device_init(dev);
|
|---|
| 261 |
|
|---|
| 262 | virtual_device = add_device(dev);
|
|---|
| 263 | virtual_device->vhcd_phone = -1;
|
|---|
| 264 | virtual_device->id = 0;
|
|---|
| 265 |
|
|---|
| 266 | return EOK;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | /** Disconnects device from HCD.
|
|---|
| 270 | *
|
|---|
| 271 | * @param dev Device to be disconnected.
|
|---|
| 272 | * @return Error code.
|
|---|
| 273 | * @retval EOK Device connected.
|
|---|
| 274 | * @retval ENOENT This device is not connected.
|
|---|
| 275 | */
|
|---|
| 276 | int usbvirt_disconnect(usbvirt_device_t *dev)
|
|---|
| 277 | {
|
|---|
| 278 | virtual_device_t *virtual_device = find_device(dev);
|
|---|
| 279 | if (virtual_device == NULL) {
|
|---|
| 280 | return ENOENT;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | destroy_device(virtual_device);
|
|---|
| 284 |
|
|---|
| 285 | return EOK;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 | /**
|
|---|
| 290 | * @}
|
|---|
| 291 | */
|
|---|