[c5d61ae6] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 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 |
|
---|
[0edf7c7] | 29 | /** @addtogroup libusb
|
---|
[c5d61ae6] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[0edf7c7] | 33 | * General communication with host controller driver (implementation).
|
---|
[c5d61ae6] | 34 | */
|
---|
| 35 | #include <devman.h>
|
---|
| 36 | #include <async.h>
|
---|
[0edf7c7] | 37 | #include <dev_iface.h>
|
---|
[c5d61ae6] | 38 | #include <usb_iface.h>
|
---|
[0edf7c7] | 39 | #include <usbhc_iface.h>
|
---|
| 40 | #include <usb/hc.h>
|
---|
[eb1a2f4] | 41 | #include <usb/debug.h>
|
---|
[c5d61ae6] | 42 | #include <errno.h>
|
---|
[eb1a2f4] | 43 | #include <assert.h>
|
---|
[c5d61ae6] | 44 |
|
---|
| 45 | /** Initialize connection to USB host controller.
|
---|
| 46 | *
|
---|
| 47 | * @param connection Connection to be initialized.
|
---|
| 48 | * @param device Device connecting to the host controller.
|
---|
| 49 | * @return Error code.
|
---|
| 50 | */
|
---|
| 51 | int usb_hc_connection_initialize_from_device(usb_hc_connection_t *connection,
|
---|
[eb1a2f4] | 52 | ddf_dev_t *device)
|
---|
[c5d61ae6] | 53 | {
|
---|
| 54 | assert(connection);
|
---|
| 55 |
|
---|
| 56 | if (device == NULL) {
|
---|
| 57 | return EBADMEM;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | devman_handle_t hc_handle;
|
---|
| 61 | int rc = usb_hc_find(device->handle, &hc_handle);
|
---|
| 62 | if (rc != EOK) {
|
---|
| 63 | return rc;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | rc = usb_hc_connection_initialize(connection, hc_handle);
|
---|
| 67 |
|
---|
| 68 | return rc;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /** Manually initialize connection to USB host controller.
|
---|
| 72 | *
|
---|
| 73 | * @param connection Connection to be initialized.
|
---|
| 74 | * @param hc_handle Devman handle of the host controller.
|
---|
| 75 | * @return Error code.
|
---|
| 76 | */
|
---|
| 77 | int usb_hc_connection_initialize(usb_hc_connection_t *connection,
|
---|
| 78 | devman_handle_t hc_handle)
|
---|
| 79 | {
|
---|
| 80 | assert(connection);
|
---|
| 81 |
|
---|
| 82 | connection->hc_handle = hc_handle;
|
---|
[79ae36dd] | 83 | connection->hc_sess = NULL;
|
---|
[c5d61ae6] | 84 |
|
---|
| 85 | return EOK;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /** Open connection to host controller.
|
---|
| 89 | *
|
---|
| 90 | * @param connection Connection to the host controller.
|
---|
| 91 | * @return Error code.
|
---|
| 92 | */
|
---|
| 93 | int usb_hc_connection_open(usb_hc_connection_t *connection)
|
---|
| 94 | {
|
---|
| 95 | assert(connection);
|
---|
[79ae36dd] | 96 |
|
---|
| 97 | if (usb_hc_connection_is_opened(connection))
|
---|
[c5d61ae6] | 98 | return EBUSY;
|
---|
[79ae36dd] | 99 |
|
---|
[9f7276d] | 100 | async_sess_t *sess = devman_device_connect(EXCHANGE_ATOMIC,
|
---|
[79ae36dd] | 101 | connection->hc_handle, 0);
|
---|
| 102 | if (!sess)
|
---|
| 103 | return ENOMEM;
|
---|
| 104 |
|
---|
| 105 | connection->hc_sess = sess;
|
---|
[c5d61ae6] | 106 | return EOK;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /** Tells whether connection to host controller is opened.
|
---|
| 110 | *
|
---|
| 111 | * @param connection Connection to the host controller.
|
---|
| 112 | * @return Whether connection is opened.
|
---|
| 113 | */
|
---|
| 114 | bool usb_hc_connection_is_opened(const usb_hc_connection_t *connection)
|
---|
| 115 | {
|
---|
| 116 | assert(connection);
|
---|
[79ae36dd] | 117 | return (connection->hc_sess != NULL);
|
---|
[c5d61ae6] | 118 | }
|
---|
| 119 |
|
---|
| 120 | /** Close connection to the host controller.
|
---|
| 121 | *
|
---|
| 122 | * @param connection Connection to the host controller.
|
---|
| 123 | * @return Error code.
|
---|
| 124 | */
|
---|
| 125 | int usb_hc_connection_close(usb_hc_connection_t *connection)
|
---|
| 126 | {
|
---|
| 127 | assert(connection);
|
---|
| 128 |
|
---|
| 129 | if (!usb_hc_connection_is_opened(connection)) {
|
---|
| 130 | return ENOENT;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[79ae36dd] | 133 | int rc = async_hangup(connection->hc_sess);
|
---|
[c5d61ae6] | 134 | if (rc != EOK) {
|
---|
| 135 | return rc;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[79ae36dd] | 138 | connection->hc_sess = NULL;
|
---|
[c5d61ae6] | 139 |
|
---|
| 140 | return EOK;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[0edf7c7] | 143 | /** Get handle of USB device with given address.
|
---|
| 144 | *
|
---|
| 145 | * @param[in] connection Opened connection to host controller.
|
---|
| 146 | * @param[in] address Address of device in question.
|
---|
| 147 | * @param[out] handle Where to write the device handle.
|
---|
| 148 | * @return Error code.
|
---|
| 149 | */
|
---|
| 150 | int usb_hc_get_handle_by_address(usb_hc_connection_t *connection,
|
---|
| 151 | usb_address_t address, devman_handle_t *handle)
|
---|
| 152 | {
|
---|
[79ae36dd] | 153 | if (!usb_hc_connection_is_opened(connection))
|
---|
[0edf7c7] | 154 | return ENOENT;
|
---|
[79ae36dd] | 155 |
|
---|
| 156 | async_exch_t *exch = async_exchange_begin(connection->hc_sess);
|
---|
| 157 |
|
---|
[0edf7c7] | 158 | sysarg_t tmp;
|
---|
[79ae36dd] | 159 | int rc = async_req_2_1(exch, DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
[0edf7c7] | 160 | IPC_M_USBHC_GET_HANDLE_BY_ADDRESS,
|
---|
| 161 | address, &tmp);
|
---|
[79ae36dd] | 162 |
|
---|
| 163 | async_exchange_end(exch);
|
---|
| 164 |
|
---|
| 165 | if ((rc == EOK) && (handle != NULL))
|
---|
[0edf7c7] | 166 | *handle = tmp;
|
---|
[79ae36dd] | 167 |
|
---|
[0edf7c7] | 168 | return rc;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[04028225] | 171 | /** Tell USB address assigned to device with given handle.
|
---|
| 172 | *
|
---|
| 173 | * @param dev_handle Devman handle of the USB device in question.
|
---|
| 174 | * @return USB address or negative error code.
|
---|
| 175 | */
|
---|
| 176 | usb_address_t usb_hc_get_address_by_handle(devman_handle_t dev_handle)
|
---|
| 177 | {
|
---|
[79ae36dd] | 178 | async_sess_t *parent_sess =
|
---|
[9f7276d] | 179 | devman_parent_device_connect(EXCHANGE_ATOMIC, dev_handle,
|
---|
[04028225] | 180 | IPC_FLAG_BLOCKING);
|
---|
[79ae36dd] | 181 | if (!parent_sess)
|
---|
| 182 | return ENOMEM;
|
---|
| 183 |
|
---|
| 184 | async_exch_t *exch = async_exchange_begin(parent_sess);
|
---|
| 185 |
|
---|
[04028225] | 186 | sysarg_t address;
|
---|
[79ae36dd] | 187 | int rc = async_req_2_1(exch, DEV_IFACE_ID(USB_DEV_IFACE),
|
---|
[04028225] | 188 | IPC_M_USB_GET_ADDRESS,
|
---|
| 189 | dev_handle, &address);
|
---|
[79ae36dd] | 190 |
|
---|
| 191 | async_exchange_end(exch);
|
---|
| 192 | async_hangup(parent_sess);
|
---|
| 193 |
|
---|
| 194 | if (rc != EOK)
|
---|
[04028225] | 195 | return rc;
|
---|
[79ae36dd] | 196 |
|
---|
[04028225] | 197 | return (usb_address_t) address;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[0edf7c7] | 200 |
|
---|
| 201 | /** Get host controller handle by its class index.
|
---|
| 202 | *
|
---|
[e280857] | 203 | * @param sid Service ID of the HC function.
|
---|
[0edf7c7] | 204 | * @param hc_handle Where to store the HC handle
|
---|
| 205 | * (can be NULL for existence test only).
|
---|
| 206 | * @return Error code.
|
---|
| 207 | */
|
---|
[e280857] | 208 | int usb_ddf_get_hc_handle_by_sid(service_id_t sid, devman_handle_t *hc_handle)
|
---|
[0edf7c7] | 209 | {
|
---|
[e280857] | 210 | devman_handle_t handle;
|
---|
[0edf7c7] | 211 | int rc;
|
---|
[e280857] | 212 |
|
---|
| 213 | rc = devman_fun_sid_to_handle(sid, &handle);
|
---|
| 214 | if (hc_handle != NULL)
|
---|
| 215 | *hc_handle = handle;
|
---|
| 216 |
|
---|
| 217 | return rc;
|
---|
[0edf7c7] | 218 | }
|
---|
| 219 |
|
---|
[e8f826b] | 220 | /** Find host controller handle that is ancestor of given device.
|
---|
| 221 | *
|
---|
| 222 | * @param[in] device_handle Device devman handle.
|
---|
| 223 | * @param[out] hc_handle Where to store handle of host controller
|
---|
| 224 | * controlling device with @p device_handle handle.
|
---|
| 225 | * @return Error code.
|
---|
| 226 | */
|
---|
| 227 | int usb_hc_find(devman_handle_t device_handle, devman_handle_t *hc_handle)
|
---|
| 228 | {
|
---|
[79ae36dd] | 229 | async_sess_t *parent_sess =
|
---|
[9f7276d] | 230 | devman_parent_device_connect(EXCHANGE_ATOMIC, device_handle,
|
---|
[e8f826b] | 231 | IPC_FLAG_BLOCKING);
|
---|
[79ae36dd] | 232 | if (!parent_sess)
|
---|
| 233 | return ENOMEM;
|
---|
| 234 |
|
---|
| 235 | async_exch_t *exch = async_exchange_begin(parent_sess);
|
---|
| 236 |
|
---|
[e8f826b] | 237 | devman_handle_t h;
|
---|
[79ae36dd] | 238 | int rc = async_req_1_1(exch, DEV_IFACE_ID(USB_DEV_IFACE),
|
---|
[e8f826b] | 239 | IPC_M_USB_GET_HOST_CONTROLLER_HANDLE, &h);
|
---|
[79ae36dd] | 240 |
|
---|
| 241 | async_exchange_end(exch);
|
---|
| 242 | async_hangup(parent_sess);
|
---|
| 243 |
|
---|
| 244 | if (rc != EOK)
|
---|
[e8f826b] | 245 | return rc;
|
---|
[79ae36dd] | 246 |
|
---|
| 247 | if (hc_handle != NULL)
|
---|
[e8f826b] | 248 | *hc_handle = h;
|
---|
[79ae36dd] | 249 |
|
---|
[e8f826b] | 250 | return EOK;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[c5d61ae6] | 253 | /**
|
---|
| 254 | * @}
|
---|
| 255 | */
|
---|