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