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