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