[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
|
---|
[b8100da] | 33 | * @brief Main handler for virtual USB device.
|
---|
[bc9a629] | 34 | */
|
---|
| 35 | #include <devmap.h>
|
---|
| 36 | #include <fcntl.h>
|
---|
| 37 | #include <vfs/vfs.h>
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <stdlib.h>
|
---|
| 40 |
|
---|
[b8100da] | 41 | #include "hub.h"
|
---|
| 42 | #include "device.h"
|
---|
| 43 | #include "private.h"
|
---|
| 44 |
|
---|
[bc9a629] | 45 | #define NAMESPACE "usb"
|
---|
| 46 |
|
---|
[b8100da] | 47 | usbvirt_device_t *device = NULL;
|
---|
| 48 |
|
---|
[bc9a629] | 49 |
|
---|
| 50 | static void handle_data_to_device(ipc_callid_t iid, ipc_call_t icall)
|
---|
| 51 | {
|
---|
[47e3a8e] | 52 | usb_address_t address = IPC_GET_ARG1(icall);
|
---|
| 53 | usb_endpoint_t endpoint = IPC_GET_ARG2(icall);
|
---|
| 54 |
|
---|
| 55 | if (address != device->address) {
|
---|
| 56 | ipc_answer_0(iid, EADDRNOTAVAIL);
|
---|
| 57 | return;
|
---|
| 58 | }
|
---|
[bc9a629] | 59 |
|
---|
| 60 | size_t len;
|
---|
| 61 | void * buffer;
|
---|
| 62 | int rc = async_data_write_accept(&buffer, false,
|
---|
| 63 | 1, USB_MAX_PAYLOAD_SIZE,
|
---|
| 64 | 0, &len);
|
---|
| 65 |
|
---|
| 66 | if (rc != EOK) {
|
---|
| 67 | ipc_answer_0(iid, rc);
|
---|
| 68 | return;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[b8100da] | 71 | handle_incoming_data(endpoint, buffer, len);
|
---|
[bc9a629] | 72 |
|
---|
| 73 | free(buffer);
|
---|
| 74 |
|
---|
| 75 | ipc_answer_0(iid, EOK);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static void callback_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 79 | {
|
---|
| 80 | ipc_answer_0(iid, EOK);
|
---|
| 81 |
|
---|
| 82 | while (true) {
|
---|
| 83 | ipc_callid_t callid;
|
---|
| 84 | ipc_call_t call;
|
---|
| 85 |
|
---|
| 86 | callid = async_get_call(&call);
|
---|
| 87 | switch (IPC_GET_METHOD(call)) {
|
---|
| 88 | case IPC_M_PHONE_HUNGUP:
|
---|
| 89 | ipc_answer_0(callid, EOK);
|
---|
| 90 | return;
|
---|
| 91 |
|
---|
[b8100da] | 92 | case IPC_M_USBVIRT_DATA_TO_DEVICE:
|
---|
[bc9a629] | 93 | handle_data_to_device(callid, call);
|
---|
| 94 | break;
|
---|
| 95 |
|
---|
| 96 | default:
|
---|
| 97 | ipc_answer_0(callid, EINVAL);
|
---|
| 98 | break;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[47e3a8e] | 103 | static void device_init(usbvirt_device_t *dev)
|
---|
| 104 | {
|
---|
| 105 | dev->send_data = usbvirt_data_to_host;
|
---|
| 106 | dev->state = USBVIRT_STATE_DEFAULT;
|
---|
| 107 | dev->address = 0;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[b8100da] | 110 | int usbvirt_data_to_host(struct usbvirt_device *dev,
|
---|
| 111 | usb_endpoint_t endpoint, void *buffer, size_t size)
|
---|
| 112 | {
|
---|
| 113 | int phone = dev->vhcd_phone_;
|
---|
| 114 |
|
---|
| 115 | if (phone < 0) {
|
---|
| 116 | return EINVAL;
|
---|
| 117 | }
|
---|
| 118 | if ((buffer == NULL) || (size == 0)) {
|
---|
| 119 | return EINVAL;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | ipc_call_t answer_data;
|
---|
| 123 | ipcarg_t answer_rc;
|
---|
| 124 | aid_t req;
|
---|
| 125 | int rc;
|
---|
| 126 |
|
---|
[47e3a8e] | 127 | req = async_send_2(phone,
|
---|
[b8100da] | 128 | IPC_M_USBVIRT_DATA_FROM_DEVICE,
|
---|
[47e3a8e] | 129 | dev->address,
|
---|
[b8100da] | 130 | endpoint,
|
---|
| 131 | &answer_data);
|
---|
| 132 |
|
---|
| 133 | rc = async_data_write_start(phone, buffer, size);
|
---|
| 134 | if (rc != EOK) {
|
---|
| 135 | async_wait_for(req, NULL);
|
---|
| 136 | return rc;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | async_wait_for(req, &answer_rc);
|
---|
| 140 | rc = (int)answer_rc;
|
---|
| 141 | if (rc != EOK) {
|
---|
| 142 | return rc;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | return EOK;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[bc9a629] | 148 | /** Create necessary phones for comunication with virtual HCD.
|
---|
| 149 | * This function wraps following calls:
|
---|
| 150 | * -# open <code>/dev/usb/<i>hcd_path</i></code> for reading
|
---|
| 151 | * -# access phone of file opened in previous step
|
---|
| 152 | * -# create callback through just opened phone
|
---|
| 153 | * -# create handler for calling on data from host to function
|
---|
| 154 | * -# return the (outgoing) phone
|
---|
| 155 | *
|
---|
| 156 | * @warning This function is wrapper for several actions and therefore
|
---|
| 157 | * it is not possible - in case of error - to determine at which point
|
---|
| 158 | * error occured.
|
---|
| 159 | *
|
---|
| 160 | * @param hcd_path HCD identification under devfs
|
---|
| 161 | * (without <code>/dev/usb/</code>).
|
---|
| 162 | * @param device_id Internal device identification (used by HCD).
|
---|
| 163 | * @param callback Handler for callbacks from HCD.
|
---|
[b8100da] | 164 | * @return EOK on success or error code from errno.h.
|
---|
[bc9a629] | 165 | */
|
---|
[b8100da] | 166 | int usbvirt_connect(usbvirt_device_t *dev, const char *hcd_path)
|
---|
[bc9a629] | 167 | {
|
---|
| 168 | char dev_path[DEVMAP_NAME_MAXLEN + 1];
|
---|
| 169 | snprintf(dev_path, DEVMAP_NAME_MAXLEN,
|
---|
| 170 | "/dev/%s/%s", NAMESPACE, hcd_path);
|
---|
| 171 |
|
---|
| 172 | int fd = open(dev_path, O_RDONLY);
|
---|
| 173 | if (fd < 0) {
|
---|
| 174 | return fd;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | int hcd_phone = fd_phone(fd);
|
---|
| 178 |
|
---|
| 179 | if (hcd_phone < 0) {
|
---|
| 180 | return hcd_phone;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | ipcarg_t phonehash;
|
---|
[b8100da] | 184 | int rc = ipc_connect_to_me(hcd_phone, 1, dev->device_id_, 0, &phonehash);
|
---|
[bc9a629] | 185 | if (rc != EOK) {
|
---|
| 186 | return rc;
|
---|
| 187 | }
|
---|
[b8100da] | 188 |
|
---|
| 189 | dev->vhcd_phone_ = hcd_phone;
|
---|
[47e3a8e] | 190 | device_init(dev);
|
---|
[b8100da] | 191 |
|
---|
| 192 | device = dev;
|
---|
| 193 |
|
---|
[bc9a629] | 194 | async_new_connection(phonehash, 0, NULL, callback_connection);
|
---|
| 195 |
|
---|
[b8100da] | 196 | return EOK;
|
---|
[bc9a629] | 197 | }
|
---|
| 198 |
|
---|
| 199 |
|
---|
[b8100da] | 200 | int usbvirt_disconnect(void)
|
---|
| 201 | {
|
---|
| 202 | ipc_hangup(device->vhcd_phone_);
|
---|
[bc9a629] | 203 |
|
---|
[b8100da] | 204 | device = NULL;
|
---|
[bc9a629] | 205 |
|
---|
| 206 | return EOK;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[b8100da] | 209 |
|
---|
[bc9a629] | 210 | /**
|
---|
| 211 | * @}
|
---|
| 212 | */
|
---|