[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 |
|
---|
[b8100da] | 29 | /** @addtogroup libusbvirt usb
|
---|
[bc9a629] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[ca07cd3] | 33 | * @brief Device registration with virtual USB framework.
|
---|
[bc9a629] | 34 | */
|
---|
[e27595b] | 35 | #include <devman.h>
|
---|
[bc9a629] | 36 | #include <errno.h>
|
---|
| 37 | #include <stdlib.h>
|
---|
[7a7bfeb3] | 38 | #include <mem.h>
|
---|
[ca07cd3] | 39 | #include <assert.h>
|
---|
[bc9a629] | 40 |
|
---|
[b8100da] | 41 | #include "hub.h"
|
---|
| 42 | #include "device.h"
|
---|
| 43 | #include "private.h"
|
---|
| 44 |
|
---|
[bc9a629] | 45 | #define NAMESPACE "usb"
|
---|
| 46 |
|
---|
[ca07cd3] | 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;
|
---|
[b8100da] | 58 |
|
---|
[ca07cd3] | 59 | /*** List of known device. */
|
---|
| 60 | static LIST_INITIALIZE(device_list);
|
---|
[7a7bfeb3] | 61 |
|
---|
[ca07cd3] | 62 | /** Find virtual device wrapper based on the contents. */
|
---|
| 63 | static virtual_device_t *find_device(usbvirt_device_t *device)
|
---|
[7a7bfeb3] | 64 | {
|
---|
[ca07cd3] | 65 | if (list_empty(&device_list)) {
|
---|
| 66 | return NULL;
|
---|
[7a7bfeb3] | 67 | }
|
---|
| 68 |
|
---|
[ca07cd3] | 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;
|
---|
[b791e3e] | 75 | }
|
---|
[bc9a629] | 76 | }
|
---|
| 77 |
|
---|
[ca07cd3] | 78 | return NULL;
|
---|
[7a7bfeb3] | 79 | }
|
---|
| 80 |
|
---|
[ca07cd3] | 81 | /** Find virtual device wrapper by its id. */
|
---|
| 82 | static virtual_device_t *find_device_by_id(ipcarg_t id)
|
---|
[7a7bfeb3] | 83 | {
|
---|
[ca07cd3] | 84 | if (list_empty(&device_list)) {
|
---|
| 85 | return NULL;
|
---|
[7a7bfeb3] | 86 | }
|
---|
| 87 |
|
---|
[ca07cd3] | 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;
|
---|
[7a7bfeb3] | 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[ca07cd3] | 97 | return NULL;
|
---|
[bc9a629] | 98 | }
|
---|
| 99 |
|
---|
[ca07cd3] | 100 | /** Reply to a control transfer. */
|
---|
| 101 | static int control_transfer_reply(usbvirt_device_t *device,
|
---|
[7a7bfeb3] | 102 | usb_endpoint_t endpoint, void *buffer, size_t size)
|
---|
[47e3a8e] | 103 | {
|
---|
[7a7bfeb3] | 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;
|
---|
[47e3a8e] | 113 | }
|
---|
| 114 |
|
---|
[ca07cd3] | 115 | /** Initialize virtual device. */
|
---|
[7a7bfeb3] | 116 | static void device_init(usbvirt_device_t *dev)
|
---|
[b8100da] | 117 | {
|
---|
[7a7bfeb3] | 118 | dev->transaction_out = transaction_out;
|
---|
| 119 | dev->transaction_setup = transaction_setup;
|
---|
| 120 | dev->transaction_in = transaction_in;
|
---|
[b8100da] | 121 |
|
---|
[7a7bfeb3] | 122 | dev->control_transfer_reply = control_transfer_reply;
|
---|
[b8100da] | 123 |
|
---|
[aab02fb] | 124 | dev->debug = user_debug;
|
---|
| 125 | dev->lib_debug = lib_debug;
|
---|
| 126 |
|
---|
[7a7bfeb3] | 127 | dev->state = USBVIRT_STATE_DEFAULT;
|
---|
| 128 | dev->address = 0;
|
---|
[ca07cd3] | 129 | dev->new_address = -1;
|
---|
[b8100da] | 130 |
|
---|
[7a7bfeb3] | 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;
|
---|
[b8100da] | 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[ca07cd3] | 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 | }
|
---|
[e27595b] | 181 |
|
---|
[ca07cd3] | 182 | device_callback_connection(dev->device, iid, icall);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[bc9a629] | 185 | /** Create necessary phones for comunication with virtual HCD.
|
---|
| 186 | * This function wraps following calls:
|
---|
[e27595b] | 187 | * -# open <code>/dev/devices/\\vhc for reading
|
---|
[bc9a629] | 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>).
|
---|
[186d630] | 199 | * @param dev Device to connect.
|
---|
[b8100da] | 200 | * @return EOK on success or error code from errno.h.
|
---|
[bc9a629] | 201 | */
|
---|
[e27595b] | 202 | int usbvirt_connect(usbvirt_device_t *dev)
|
---|
[bc9a629] | 203 | {
|
---|
[ca07cd3] | 204 | virtual_device_t *virtual_device = find_device(dev);
|
---|
| 205 | if (virtual_device != NULL) {
|
---|
| 206 | return EEXISTS;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[e27595b] | 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;
|
---|
[bc9a629] | 217 | }
|
---|
| 218 |
|
---|
[e27595b] | 219 | int hcd_phone = devman_device_connect(handle, 0);
|
---|
[bc9a629] | 220 |
|
---|
| 221 | if (hcd_phone < 0) {
|
---|
[e27595b] | 222 | printf("devman_device_connect() failed\n");
|
---|
[bc9a629] | 223 | return hcd_phone;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | ipcarg_t phonehash;
|
---|
[e27595b] | 227 | rc = ipc_connect_to_me(hcd_phone, 0, 0, 0, &phonehash);
|
---|
[bc9a629] | 228 | if (rc != EOK) {
|
---|
[e27595b] | 229 | printf("ipc_connect_to_me() failed\n");
|
---|
[bc9a629] | 230 | return rc;
|
---|
| 231 | }
|
---|
[b8100da] | 232 |
|
---|
[47e3a8e] | 233 | device_init(dev);
|
---|
[b8100da] | 234 |
|
---|
[ca07cd3] | 235 | virtual_device = add_device(dev);
|
---|
| 236 | virtual_device->vhcd_phone = hcd_phone;
|
---|
| 237 | virtual_device->id = 0;
|
---|
[b8100da] | 238 |
|
---|
[bc9a629] | 239 | async_new_connection(phonehash, 0, NULL, callback_connection);
|
---|
| 240 |
|
---|
[b8100da] | 241 | return EOK;
|
---|
[bc9a629] | 242 | }
|
---|
| 243 |
|
---|
[186d630] | 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.
|
---|
[ca07cd3] | 249 | * @return Error code.
|
---|
| 250 | * @retval EOK Device connected.
|
---|
| 251 | * @retval EEXISTS This device is already connected.
|
---|
[186d630] | 252 | */
|
---|
| 253 | int usbvirt_connect_local(usbvirt_device_t *dev)
|
---|
| 254 | {
|
---|
[ca07cd3] | 255 | virtual_device_t *virtual_device = find_device(dev);
|
---|
| 256 | if (virtual_device != NULL) {
|
---|
| 257 | return EEXISTS;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[186d630] | 260 | device_init(dev);
|
---|
| 261 |
|
---|
[ca07cd3] | 262 | virtual_device = add_device(dev);
|
---|
| 263 | virtual_device->vhcd_phone = -1;
|
---|
| 264 | virtual_device->id = 0;
|
---|
[b791e3e] | 265 |
|
---|
[186d630] | 266 | return EOK;
|
---|
| 267 | }
|
---|
[bc9a629] | 268 |
|
---|
[186d630] | 269 | /** Disconnects device from HCD.
|
---|
| 270 | *
|
---|
[ca07cd3] | 271 | * @param dev Device to be disconnected.
|
---|
| 272 | * @return Error code.
|
---|
| 273 | * @retval EOK Device connected.
|
---|
| 274 | * @retval ENOENT This device is not connected.
|
---|
[186d630] | 275 | */
|
---|
[ca07cd3] | 276 | int usbvirt_disconnect(usbvirt_device_t *dev)
|
---|
[b8100da] | 277 | {
|
---|
[ca07cd3] | 278 | virtual_device_t *virtual_device = find_device(dev);
|
---|
| 279 | if (virtual_device == NULL) {
|
---|
| 280 | return ENOENT;
|
---|
| 281 | }
|
---|
[bc9a629] | 282 |
|
---|
[ca07cd3] | 283 | destroy_device(virtual_device);
|
---|
[bc9a629] | 284 |
|
---|
| 285 | return EOK;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[b8100da] | 288 |
|
---|
[bc9a629] | 289 | /**
|
---|
| 290 | * @}
|
---|
| 291 | */
|
---|