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