| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
|---|
| 3 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup libusb
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | * General communication with host controller driver (implementation).
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <usb/debug.h>
|
|---|
| 38 |
|
|---|
| 39 | #include <assert.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <usbhc_iface.h>
|
|---|
| 42 | #include <usb/dev.h>
|
|---|
| 43 | #include <usb/hc.h>
|
|---|
| 44 |
|
|---|
| 45 | static int usb_hc_connection_add_ref(usb_hc_connection_t *connection)
|
|---|
| 46 | {
|
|---|
| 47 | assert(connection);
|
|---|
| 48 |
|
|---|
| 49 | fibril_mutex_lock(&connection->guard);
|
|---|
| 50 | if (connection->ref_count == 0) {
|
|---|
| 51 | assert(connection->hc_sess == NULL);
|
|---|
| 52 | /* Parallel exchange for us */
|
|---|
| 53 | connection->hc_sess = devman_device_connect(EXCHANGE_PARALLEL,
|
|---|
| 54 | connection->hc_handle, 0);
|
|---|
| 55 | if (!connection->hc_sess) {
|
|---|
| 56 | fibril_mutex_unlock(&connection->guard);
|
|---|
| 57 | return ENOMEM;
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | ++connection->ref_count;
|
|---|
| 62 | fibril_mutex_unlock(&connection->guard);
|
|---|
| 63 | return EOK;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | static int usb_hc_connection_del_ref(usb_hc_connection_t *connection)
|
|---|
| 67 | {
|
|---|
| 68 | assert(connection);
|
|---|
| 69 |
|
|---|
| 70 | fibril_mutex_lock(&connection->guard);
|
|---|
| 71 | if (connection->ref_count == 0) {
|
|---|
| 72 | /* Closing already closed connection... */
|
|---|
| 73 | assert(connection->hc_sess == NULL);
|
|---|
| 74 | fibril_mutex_unlock(&connection->guard);
|
|---|
| 75 | return EOK;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | --connection->ref_count;
|
|---|
| 79 | int ret = EOK;
|
|---|
| 80 | if (connection->ref_count == 0) {
|
|---|
| 81 | assert(connection->hc_sess);
|
|---|
| 82 | ret = async_hangup(connection->hc_sess);
|
|---|
| 83 | connection->hc_sess = NULL;
|
|---|
| 84 | }
|
|---|
| 85 | fibril_mutex_unlock(&connection->guard);
|
|---|
| 86 | return ret;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | #define EXCH_INIT(connection, exch) \
|
|---|
| 90 | do { \
|
|---|
| 91 | exch = NULL; \
|
|---|
| 92 | if (!connection) \
|
|---|
| 93 | return EBADMEM; \
|
|---|
| 94 | const int ret = usb_hc_connection_add_ref(connection); \
|
|---|
| 95 | if (ret != EOK) \
|
|---|
| 96 | return ret; \
|
|---|
| 97 | exch = async_exchange_begin(connection->hc_sess); \
|
|---|
| 98 | if (exch == NULL) { \
|
|---|
| 99 | usb_hc_connection_del_ref(connection); \
|
|---|
| 100 | return ENOMEM; \
|
|---|
| 101 | } \
|
|---|
| 102 | } while (0)
|
|---|
| 103 |
|
|---|
| 104 | #define EXCH_FINI(connection, exch) \
|
|---|
| 105 | if (exch) { \
|
|---|
| 106 | async_exchange_end(exch); \
|
|---|
| 107 | usb_hc_connection_del_ref(connection); \
|
|---|
| 108 | } else (void)0
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 | void usb_hc_connection_deinitialize(usb_hc_connection_t *connection)
|
|---|
| 112 | {
|
|---|
| 113 | assert(connection);
|
|---|
| 114 | fibril_mutex_lock(&connection->guard);
|
|---|
| 115 | if (connection->ref_count != 0) {
|
|---|
| 116 | usb_log_warning("%u stale reference(s) to HC connection.\n",
|
|---|
| 117 | connection->ref_count);
|
|---|
| 118 | assert(connection->hc_sess);
|
|---|
| 119 | async_hangup(connection->hc_sess);
|
|---|
| 120 | connection->hc_sess = NULL;
|
|---|
| 121 | connection->ref_count = 0;
|
|---|
| 122 | }
|
|---|
| 123 | fibril_mutex_unlock(&connection->guard);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /** Open connection to host controller.
|
|---|
| 127 | *
|
|---|
| 128 | * @param connection Connection to the host controller.
|
|---|
| 129 | * @return Error code.
|
|---|
| 130 | */
|
|---|
| 131 | int usb_hc_connection_open(usb_hc_connection_t *connection)
|
|---|
| 132 | {
|
|---|
| 133 | return usb_hc_connection_add_ref(connection);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /** Close connection to the host controller.
|
|---|
| 137 | *
|
|---|
| 138 | * @param connection Connection to the host controller.
|
|---|
| 139 | * @return Error code.
|
|---|
| 140 | */
|
|---|
| 141 | int usb_hc_connection_close(usb_hc_connection_t *connection)
|
|---|
| 142 | {
|
|---|
| 143 | return usb_hc_connection_del_ref(connection);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 | /** Get handle of USB device with given address.
|
|---|
| 148 | *
|
|---|
| 149 | * @param[in] connection Opened connection to host controller.
|
|---|
| 150 | * @param[in] address Address of device in question.
|
|---|
| 151 | * @param[out] handle Where to write the device handle.
|
|---|
| 152 | * @return Error code.
|
|---|
| 153 | */
|
|---|
| 154 | int usb_hc_get_handle_by_address(usb_hc_connection_t *connection,
|
|---|
| 155 | usb_address_t address, devman_handle_t *handle)
|
|---|
| 156 | {
|
|---|
| 157 | async_exch_t *exch;
|
|---|
| 158 | EXCH_INIT(connection, exch);
|
|---|
| 159 |
|
|---|
| 160 | const int ret = usbhc_get_handle(exch, address, handle);
|
|---|
| 161 |
|
|---|
| 162 | EXCH_FINI(connection, exch);
|
|---|
| 163 | return ret;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | int usb_hc_read(usb_hc_connection_t *connection, usb_address_t address,
|
|---|
| 167 | usb_endpoint_t endpoint, uint64_t setup, void *data, size_t size,
|
|---|
| 168 | size_t *real_size)
|
|---|
| 169 | {
|
|---|
| 170 | async_exch_t *exch;
|
|---|
| 171 | EXCH_INIT(connection, exch);
|
|---|
| 172 |
|
|---|
| 173 | const int ret =
|
|---|
| 174 | usbhc_read(exch, address, endpoint, setup, data, size, real_size);
|
|---|
| 175 |
|
|---|
| 176 | EXCH_FINI(connection, exch);
|
|---|
| 177 | return ret;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | int usb_hc_write(usb_hc_connection_t *connection, usb_address_t address,
|
|---|
| 181 | usb_endpoint_t endpoint, uint64_t setup, const void *data, size_t size)
|
|---|
| 182 | {
|
|---|
| 183 | async_exch_t *exch;
|
|---|
| 184 | EXCH_INIT(connection, exch);
|
|---|
| 185 |
|
|---|
| 186 | const int ret = usbhc_write(exch, address, endpoint, setup, data, size);
|
|---|
| 187 |
|
|---|
| 188 | EXCH_FINI(connection, exch);
|
|---|
| 189 | return ret;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /**
|
|---|
| 193 | * @}
|
|---|
| 194 | */
|
|---|