| 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 | /** @addtogroup libusb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * General communication with host controller driver (implementation).
|
|---|
| 34 | */
|
|---|
| 35 | #include <usb/debug.h>
|
|---|
| 36 |
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <usbhc_iface.h>
|
|---|
| 40 | #include <usb/dev.h>
|
|---|
| 41 | #include <usb/hc.h>
|
|---|
| 42 |
|
|---|
| 43 | static int usb_hc_connection_add_ref(usb_hc_connection_t *connection)
|
|---|
| 44 | {
|
|---|
| 45 | assert(connection);
|
|---|
| 46 | fibril_mutex_lock(&connection->guard);
|
|---|
| 47 | if (connection->ref_count == 0) {
|
|---|
| 48 | assert(connection->hc_sess == NULL);
|
|---|
| 49 | /* Parallel exchange for us */
|
|---|
| 50 | connection->hc_sess = devman_device_connect(EXCHANGE_PARALLEL,
|
|---|
| 51 | connection->hc_handle, 0);
|
|---|
| 52 | if (!connection->hc_sess) {
|
|---|
| 53 | fibril_mutex_unlock(&connection->guard);
|
|---|
| 54 | return ENOMEM;
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | ++connection->ref_count;
|
|---|
| 58 | fibril_mutex_unlock(&connection->guard);
|
|---|
| 59 | return EOK;
|
|---|
| 60 | }
|
|---|
| 61 | /*----------------------------------------------------------------------------*/
|
|---|
| 62 | static int usb_hc_connection_del_ref(usb_hc_connection_t *connection)
|
|---|
| 63 | {
|
|---|
| 64 | assert(connection);
|
|---|
| 65 | fibril_mutex_lock(&connection->guard);
|
|---|
| 66 | if (connection->ref_count == 0) {
|
|---|
| 67 | /* Closing already closed connection... */
|
|---|
| 68 | assert(connection->hc_sess = NULL);
|
|---|
| 69 | fibril_mutex_unlock(&connection->guard);
|
|---|
| 70 | return EOK;
|
|---|
| 71 | }
|
|---|
| 72 | --connection->ref_count;
|
|---|
| 73 | int ret = EOK;
|
|---|
| 74 | if (connection->ref_count == 0) {
|
|---|
| 75 | assert(connection->hc_sess);
|
|---|
| 76 | ret = async_hangup(connection->hc_sess);
|
|---|
| 77 | connection->hc_sess = NULL;
|
|---|
| 78 | }
|
|---|
| 79 | fibril_mutex_unlock(&connection->guard);
|
|---|
| 80 | return ret;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | #define EXCH_INIT(connection, exch) \
|
|---|
| 84 | do { \
|
|---|
| 85 | exch = NULL; \
|
|---|
| 86 | if (!connection) \
|
|---|
| 87 | return EBADMEM; \
|
|---|
| 88 | const int ret = usb_hc_connection_add_ref(connection); \
|
|---|
| 89 | if (ret != EOK) \
|
|---|
| 90 | return ret; \
|
|---|
| 91 | exch = async_exchange_begin(connection->hc_sess); \
|
|---|
| 92 | if (exch == NULL) { \
|
|---|
| 93 | usb_hc_connection_del_ref(connection); \
|
|---|
| 94 | return ENOMEM; \
|
|---|
| 95 | } \
|
|---|
| 96 | } while (0)
|
|---|
| 97 |
|
|---|
| 98 | #define EXCH_FINI(connection, exch) \
|
|---|
| 99 | if (exch) { \
|
|---|
| 100 | async_exchange_end(exch); \
|
|---|
| 101 | usb_hc_connection_del_ref(connection); \
|
|---|
| 102 | } else (void)0
|
|---|
| 103 |
|
|---|
| 104 | /** Initialize connection to USB host controller.
|
|---|
| 105 | *
|
|---|
| 106 | * @param connection Connection to be initialized.
|
|---|
| 107 | * @param device Device connecting to the host controller.
|
|---|
| 108 | * @return Error code.
|
|---|
| 109 | */
|
|---|
| 110 | int usb_hc_connection_initialize_from_device(usb_hc_connection_t *connection,
|
|---|
| 111 | const ddf_dev_t *device)
|
|---|
| 112 | {
|
|---|
| 113 | assert(connection);
|
|---|
| 114 |
|
|---|
| 115 | if (device == NULL) {
|
|---|
| 116 | return EBADMEM;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | devman_handle_t hc_handle;
|
|---|
| 120 | const int rc = usb_get_hc_by_handle(device->handle, &hc_handle);
|
|---|
| 121 | if (rc == EOK) {
|
|---|
| 122 | usb_hc_connection_initialize(connection, hc_handle);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | return rc;
|
|---|
| 126 | }
|
|---|
| 127 | /*----------------------------------------------------------------------------*/
|
|---|
| 128 | void usb_hc_connection_deinitialize(usb_hc_connection_t *connection)
|
|---|
| 129 | {
|
|---|
| 130 | assert(connection);
|
|---|
| 131 | fibril_mutex_lock(&connection->guard);
|
|---|
| 132 | if (connection->ref_count != 0) {
|
|---|
| 133 | usb_log_warning("%u stale reference(s) to HC connection.\n",
|
|---|
| 134 | connection->ref_count);
|
|---|
| 135 | assert(connection->hc_sess);
|
|---|
| 136 | async_hangup(connection->hc_sess);
|
|---|
| 137 | connection->hc_sess = NULL;
|
|---|
| 138 | connection->ref_count = 0;
|
|---|
| 139 | }
|
|---|
| 140 | fibril_mutex_unlock(&connection->guard);
|
|---|
| 141 | }
|
|---|
| 142 | /*----------------------------------------------------------------------------*/
|
|---|
| 143 | /** Open connection to host controller.
|
|---|
| 144 | *
|
|---|
| 145 | * @param connection Connection to the host controller.
|
|---|
| 146 | * @return Error code.
|
|---|
| 147 | */
|
|---|
| 148 | int usb_hc_connection_open(usb_hc_connection_t *connection)
|
|---|
| 149 | {
|
|---|
| 150 | return usb_hc_connection_add_ref(connection);
|
|---|
| 151 | }
|
|---|
| 152 | /*----------------------------------------------------------------------------*/
|
|---|
| 153 | /** Close connection to the host controller.
|
|---|
| 154 | *
|
|---|
| 155 | * @param connection Connection to the host controller.
|
|---|
| 156 | * @return Error code.
|
|---|
| 157 | */
|
|---|
| 158 | int usb_hc_connection_close(usb_hc_connection_t *connection)
|
|---|
| 159 | {
|
|---|
| 160 | return usb_hc_connection_del_ref(connection);
|
|---|
| 161 | }
|
|---|
| 162 | /*----------------------------------------------------------------------------*/
|
|---|
| 163 | /** Ask host controller for free address assignment.
|
|---|
| 164 | *
|
|---|
| 165 | * @param connection Opened connection to host controller.
|
|---|
| 166 | * @param preferred Preferred SUB address.
|
|---|
| 167 | * @param strict Fail if the preferred address is not avialable.
|
|---|
| 168 | * @param speed Speed of the new device (device that will be assigned
|
|---|
| 169 | * the returned address).
|
|---|
| 170 | * @return Assigned USB address or negative error code.
|
|---|
| 171 | */
|
|---|
| 172 | usb_address_t usb_hc_request_address(usb_hc_connection_t *connection,
|
|---|
| 173 | usb_address_t preferred, bool strict, usb_speed_t speed)
|
|---|
| 174 | {
|
|---|
| 175 | async_exch_t *exch;
|
|---|
| 176 | EXCH_INIT(connection, exch);
|
|---|
| 177 |
|
|---|
| 178 | usb_address_t address = preferred;
|
|---|
| 179 | const int ret = usbhc_request_address(exch, &address, strict, speed);
|
|---|
| 180 |
|
|---|
| 181 | EXCH_FINI(connection, exch);
|
|---|
| 182 | return ret == EOK ? address : ret;
|
|---|
| 183 | }
|
|---|
| 184 | /*----------------------------------------------------------------------------*/
|
|---|
| 185 | int usb_hc_bind_address(usb_hc_connection_t * connection,
|
|---|
| 186 | usb_address_t address, devman_handle_t handle)
|
|---|
| 187 | {
|
|---|
| 188 | async_exch_t *exch;
|
|---|
| 189 | EXCH_INIT(connection, exch);
|
|---|
| 190 |
|
|---|
| 191 | const int ret = usbhc_bind_address(exch, address, handle);
|
|---|
| 192 |
|
|---|
| 193 | EXCH_FINI(connection, exch);
|
|---|
| 194 | return ret;
|
|---|
| 195 | }
|
|---|
| 196 | /*----------------------------------------------------------------------------*/
|
|---|
| 197 | /** Get handle of USB device with given address.
|
|---|
| 198 | *
|
|---|
| 199 | * @param[in] connection Opened connection to host controller.
|
|---|
| 200 | * @param[in] address Address of device in question.
|
|---|
| 201 | * @param[out] handle Where to write the device handle.
|
|---|
| 202 | * @return Error code.
|
|---|
| 203 | */
|
|---|
| 204 | int usb_hc_get_handle_by_address(usb_hc_connection_t *connection,
|
|---|
| 205 | usb_address_t address, devman_handle_t *handle)
|
|---|
| 206 | {
|
|---|
| 207 | async_exch_t *exch;
|
|---|
| 208 | EXCH_INIT(connection, exch);
|
|---|
| 209 |
|
|---|
| 210 | const int ret = usbhc_get_handle(exch, address, handle);
|
|---|
| 211 |
|
|---|
| 212 | EXCH_FINI(connection, exch);
|
|---|
| 213 | return ret;
|
|---|
| 214 | }
|
|---|
| 215 | /*----------------------------------------------------------------------------*/
|
|---|
| 216 | int usb_hc_release_address(usb_hc_connection_t *connection,
|
|---|
| 217 | usb_address_t address)
|
|---|
| 218 | {
|
|---|
| 219 | async_exch_t *exch;
|
|---|
| 220 | EXCH_INIT(connection, exch);
|
|---|
| 221 |
|
|---|
| 222 | const int ret = usbhc_release_address(exch, address);
|
|---|
| 223 |
|
|---|
| 224 | EXCH_FINI(connection, exch);
|
|---|
| 225 | return ret;
|
|---|
| 226 | }
|
|---|
| 227 | /*----------------------------------------------------------------------------*/
|
|---|
| 228 | int usb_hc_register_endpoint(usb_hc_connection_t *connection,
|
|---|
| 229 | usb_address_t address, usb_endpoint_t endpoint, usb_transfer_type_t type,
|
|---|
| 230 | usb_direction_t direction, size_t packet_size, unsigned interval)
|
|---|
| 231 | {
|
|---|
| 232 | async_exch_t *exch;
|
|---|
| 233 | EXCH_INIT(connection, exch);
|
|---|
| 234 |
|
|---|
| 235 | const int ret = usbhc_register_endpoint(exch, address, endpoint,
|
|---|
| 236 | type, direction, packet_size, interval);
|
|---|
| 237 |
|
|---|
| 238 | EXCH_FINI(connection, exch);
|
|---|
| 239 | return ret;
|
|---|
| 240 | }
|
|---|
| 241 | /*----------------------------------------------------------------------------*/
|
|---|
| 242 | int usb_hc_unregister_endpoint(usb_hc_connection_t *connection,
|
|---|
| 243 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
|
|---|
| 244 | {
|
|---|
| 245 | async_exch_t *exch;
|
|---|
| 246 | EXCH_INIT(connection, exch);
|
|---|
| 247 |
|
|---|
| 248 | const int ret =
|
|---|
| 249 | usbhc_unregister_endpoint(exch, address, endpoint, direction);
|
|---|
| 250 |
|
|---|
| 251 | EXCH_FINI(connection, exch);
|
|---|
| 252 | return ret;
|
|---|
| 253 | }
|
|---|
| 254 | /*----------------------------------------------------------------------------*/
|
|---|
| 255 | int usb_hc_read(usb_hc_connection_t *connection, usb_address_t address,
|
|---|
| 256 | usb_endpoint_t endpoint, uint64_t setup, void *data, size_t size,
|
|---|
| 257 | size_t *real_size)
|
|---|
| 258 | {
|
|---|
| 259 | async_exch_t *exch;
|
|---|
| 260 | EXCH_INIT(connection, exch);
|
|---|
| 261 |
|
|---|
| 262 | const int ret =
|
|---|
| 263 | usbhc_read(exch, address, endpoint, setup, data, size, real_size);
|
|---|
| 264 |
|
|---|
| 265 | EXCH_FINI(connection, exch);
|
|---|
| 266 | return ret;
|
|---|
| 267 | }
|
|---|
| 268 | /*----------------------------------------------------------------------------*/
|
|---|
| 269 | int usb_hc_write(usb_hc_connection_t *connection, usb_address_t address,
|
|---|
| 270 | usb_endpoint_t endpoint, uint64_t setup, const void *data, size_t size)
|
|---|
| 271 | {
|
|---|
| 272 | async_exch_t *exch;
|
|---|
| 273 | EXCH_INIT(connection, exch);
|
|---|
| 274 |
|
|---|
| 275 | const int ret = usbhc_write(exch, address, endpoint, setup, data, size);
|
|---|
| 276 |
|
|---|
| 277 | EXCH_FINI(connection, exch);
|
|---|
| 278 | return ret;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | /**
|
|---|
| 282 | * @}
|
|---|
| 283 | */
|
|---|