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