| 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 |
|
|---|
| 29 | /** @addtogroup libusb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * USB endpoint pipes miscellaneous functions.
|
|---|
| 34 | */
|
|---|
| 35 | #include <usb/usb.h>
|
|---|
| 36 | #include <usb/pipes.h>
|
|---|
| 37 | #include <usb/debug.h>
|
|---|
| 38 | #include <usbhc_iface.h>
|
|---|
| 39 | #include <usb_iface.h>
|
|---|
| 40 | #include <devman.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <assert.h>
|
|---|
| 43 | #include "pipepriv.h"
|
|---|
| 44 |
|
|---|
| 45 | #define IPC_AGAIN_DELAY (1000 * 2) /* 2ms */
|
|---|
| 46 |
|
|---|
| 47 | /** Tell USB address assigned to given device.
|
|---|
| 48 | *
|
|---|
| 49 | * @param phone Phone to parent device.
|
|---|
| 50 | * @param dev Device in question.
|
|---|
| 51 | * @return USB address or error code.
|
|---|
| 52 | */
|
|---|
| 53 | static usb_address_t get_my_address(int phone, ddf_dev_t *dev)
|
|---|
| 54 | {
|
|---|
| 55 | sysarg_t address;
|
|---|
| 56 |
|
|---|
| 57 | /*
|
|---|
| 58 | * We are sending special value as a handle - zero - to get
|
|---|
| 59 | * handle of the parent function (that handle was used
|
|---|
| 60 | * when registering our device @p dev.
|
|---|
| 61 | */
|
|---|
| 62 | int rc = async_req_2_1(phone, DEV_IFACE_ID(USB_DEV_IFACE),
|
|---|
| 63 | IPC_M_USB_GET_ADDRESS,
|
|---|
| 64 | 0, &address);
|
|---|
| 65 |
|
|---|
| 66 | if (rc != EOK) {
|
|---|
| 67 | return rc;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | return (usb_address_t) address;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /** Tell USB interface assigned to given device.
|
|---|
| 74 | *
|
|---|
| 75 | * @param device Device in question.
|
|---|
| 76 | * @return Interface number (negative code means any).
|
|---|
| 77 | */
|
|---|
| 78 | int usb_device_get_assigned_interface(ddf_dev_t *device)
|
|---|
| 79 | {
|
|---|
| 80 | int parent_phone = devman_parent_device_connect(device->handle,
|
|---|
| 81 | IPC_FLAG_BLOCKING);
|
|---|
| 82 | if (parent_phone < 0) {
|
|---|
| 83 | return -1;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | sysarg_t iface_no;
|
|---|
| 87 | int rc = async_req_2_1(parent_phone, DEV_IFACE_ID(USB_DEV_IFACE),
|
|---|
| 88 | IPC_M_USB_GET_INTERFACE,
|
|---|
| 89 | device->handle, &iface_no);
|
|---|
| 90 |
|
|---|
| 91 | async_hangup(parent_phone);
|
|---|
| 92 |
|
|---|
| 93 | if (rc != EOK) {
|
|---|
| 94 | return -1;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | return (int) iface_no;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /** Tell USB address assigned to given device.
|
|---|
| 101 | *
|
|---|
| 102 | * @param dev_handle Devman handle of the USB device in question.
|
|---|
| 103 | * @return USB address or negative error code.
|
|---|
| 104 | */
|
|---|
| 105 | usb_address_t usb_device_get_assigned_address(devman_handle_t dev_handle)
|
|---|
| 106 | {
|
|---|
| 107 | int parent_phone = devman_parent_device_connect(dev_handle,
|
|---|
| 108 | IPC_FLAG_BLOCKING);
|
|---|
| 109 | if (parent_phone < 0) {
|
|---|
| 110 | return parent_phone;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | sysarg_t address;
|
|---|
| 114 |
|
|---|
| 115 | int rc = async_req_2_1(parent_phone, DEV_IFACE_ID(USB_DEV_IFACE),
|
|---|
| 116 | IPC_M_USB_GET_ADDRESS,
|
|---|
| 117 | dev_handle, &address);
|
|---|
| 118 |
|
|---|
| 119 | if (rc != EOK) {
|
|---|
| 120 | return rc;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | async_hangup(parent_phone);
|
|---|
| 124 |
|
|---|
| 125 | return (usb_address_t) address;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /** Initialize connection to USB device.
|
|---|
| 129 | *
|
|---|
| 130 | * @param connection Connection structure to be initialized.
|
|---|
| 131 | * @param dev Generic device backing the USB device.
|
|---|
| 132 | * @return Error code.
|
|---|
| 133 | */
|
|---|
| 134 | int usb_device_connection_initialize_from_device(
|
|---|
| 135 | usb_device_connection_t *connection, ddf_dev_t *dev)
|
|---|
| 136 | {
|
|---|
| 137 | assert(connection);
|
|---|
| 138 | assert(dev);
|
|---|
| 139 |
|
|---|
| 140 | int rc;
|
|---|
| 141 | devman_handle_t hc_handle;
|
|---|
| 142 | usb_address_t my_address;
|
|---|
| 143 |
|
|---|
| 144 | rc = usb_hc_find(dev->handle, &hc_handle);
|
|---|
| 145 | if (rc != EOK) {
|
|---|
| 146 | return rc;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | int parent_phone = devman_parent_device_connect(dev->handle,
|
|---|
| 150 | IPC_FLAG_BLOCKING);
|
|---|
| 151 | if (parent_phone < 0) {
|
|---|
| 152 | return parent_phone;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /*
|
|---|
| 156 | * Asking for "my" address may require several attempts.
|
|---|
| 157 | * That is because following scenario may happen:
|
|---|
| 158 | * - parent driver (i.e. driver of parent device) announces new device
|
|---|
| 159 | * and devman launches current driver
|
|---|
| 160 | * - parent driver is preempted and thus does not send address-handle
|
|---|
| 161 | * binding to HC driver
|
|---|
| 162 | * - this driver gets here and wants the binding
|
|---|
| 163 | * - the HC does not know the binding yet and thus it answers ENOENT
|
|---|
| 164 | * So, we need to wait for the HC to learn the binding.
|
|---|
| 165 | */
|
|---|
| 166 | do {
|
|---|
| 167 | my_address = get_my_address(parent_phone, dev);
|
|---|
| 168 |
|
|---|
| 169 | if (my_address == ENOENT) {
|
|---|
| 170 | /* Be nice, let other fibrils run and try again. */
|
|---|
| 171 | async_usleep(IPC_AGAIN_DELAY);
|
|---|
| 172 | } else if (my_address < 0) {
|
|---|
| 173 | /* Some other problem, no sense trying again. */
|
|---|
| 174 | rc = my_address;
|
|---|
| 175 | goto leave;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | } while (my_address < 0);
|
|---|
| 179 |
|
|---|
| 180 | rc = usb_device_connection_initialize(connection,
|
|---|
| 181 | hc_handle, my_address);
|
|---|
| 182 |
|
|---|
| 183 | leave:
|
|---|
| 184 | async_hangup(parent_phone);
|
|---|
| 185 | return rc;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | /** Initialize connection to USB device.
|
|---|
| 189 | *
|
|---|
| 190 | * @param connection Connection structure to be initialized.
|
|---|
| 191 | * @param host_controller_handle Devman handle of host controller device is
|
|---|
| 192 | * connected to.
|
|---|
| 193 | * @param device_address Device USB address.
|
|---|
| 194 | * @return Error code.
|
|---|
| 195 | */
|
|---|
| 196 | int usb_device_connection_initialize(usb_device_connection_t *connection,
|
|---|
| 197 | devman_handle_t host_controller_handle, usb_address_t device_address)
|
|---|
| 198 | {
|
|---|
| 199 | assert(connection);
|
|---|
| 200 |
|
|---|
| 201 | if ((device_address < 0) || (device_address >= USB11_ADDRESS_MAX)) {
|
|---|
| 202 | return EINVAL;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | connection->hc_handle = host_controller_handle;
|
|---|
| 206 | connection->address = device_address;
|
|---|
| 207 |
|
|---|
| 208 | return EOK;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /** Initialize connection to USB device on default address.
|
|---|
| 212 | *
|
|---|
| 213 | * @param dev_connection Device connection structure to be initialized.
|
|---|
| 214 | * @param hc_connection Initialized connection to host controller.
|
|---|
| 215 | * @return Error code.
|
|---|
| 216 | */
|
|---|
| 217 | int usb_device_connection_initialize_on_default_address(
|
|---|
| 218 | usb_device_connection_t *dev_connection,
|
|---|
| 219 | usb_hc_connection_t *hc_connection)
|
|---|
| 220 | {
|
|---|
| 221 | assert(dev_connection);
|
|---|
| 222 |
|
|---|
| 223 | if (hc_connection == NULL) {
|
|---|
| 224 | return EBADMEM;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | return usb_device_connection_initialize(dev_connection,
|
|---|
| 228 | hc_connection->hc_handle, (usb_address_t) 0);
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 | /** Start a session on the endpoint pipe.
|
|---|
| 233 | *
|
|---|
| 234 | * A session is something inside what any communication occurs.
|
|---|
| 235 | * It is expected that sessions would be started right before the transfer
|
|---|
| 236 | * and ended - see usb_pipe_end_session() - after the last
|
|---|
| 237 | * transfer.
|
|---|
| 238 | * The reason for this is that session actually opens some communication
|
|---|
| 239 | * channel to the host controller (or to the physical hardware if you
|
|---|
| 240 | * wish) and thus it involves acquiring kernel resources.
|
|---|
| 241 | * Since they are limited, sessions shall not be longer than strictly
|
|---|
| 242 | * necessary.
|
|---|
| 243 | *
|
|---|
| 244 | * @deprecated
|
|---|
| 245 | * Obsoleted with introduction of usb_pipe_start_long_transfer
|
|---|
| 246 | *
|
|---|
| 247 | * @param pipe Endpoint pipe to start the session on.
|
|---|
| 248 | * @return Error code.
|
|---|
| 249 | */
|
|---|
| 250 | int usb_pipe_start_session(usb_pipe_t *pipe)
|
|---|
| 251 | {
|
|---|
| 252 | usb_log_warning("usb_pipe_start_session() was deprecated.\n");
|
|---|
| 253 | return EOK;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 | /** Ends a session on the endpoint pipe.
|
|---|
| 258 | *
|
|---|
| 259 | * @deprecated
|
|---|
| 260 | * Obsoleted with introduction of usb_pipe_end_long_transfer
|
|---|
| 261 | *
|
|---|
| 262 | * @see usb_pipe_start_session
|
|---|
| 263 | *
|
|---|
| 264 | * @param pipe Endpoint pipe to end the session on.
|
|---|
| 265 | * @return Error code.
|
|---|
| 266 | */
|
|---|
| 267 | int usb_pipe_end_session(usb_pipe_t *pipe)
|
|---|
| 268 | {
|
|---|
| 269 | usb_log_warning("usb_pipe_end_session() was deprecated.\n");
|
|---|
| 270 | return EOK;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | /** Tell whether a session is started (open) on the endpoint pipe.
|
|---|
| 274 | *
|
|---|
| 275 | * The expected usage of this function is in assertions for some
|
|---|
| 276 | * nested functions.
|
|---|
| 277 | *
|
|---|
| 278 | * @param pipe Endpoint pipe in question.
|
|---|
| 279 | * @return Whether @p pipe has opened a session.
|
|---|
| 280 | */
|
|---|
| 281 | bool usb_pipe_is_session_started(usb_pipe_t *pipe)
|
|---|
| 282 | {
|
|---|
| 283 | pipe_acquire(pipe);
|
|---|
| 284 | bool started = pipe->refcount > 0;
|
|---|
| 285 | pipe_release(pipe);
|
|---|
| 286 | return started;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /** Prepare pipe for a long transfer.
|
|---|
| 290 | *
|
|---|
| 291 | * By a long transfer is mean transfer consisting of several
|
|---|
| 292 | * requests to the HC.
|
|---|
| 293 | * Calling such function is optional and it has positive effect of
|
|---|
| 294 | * improved performance because IPC session is initiated only once.
|
|---|
| 295 | *
|
|---|
| 296 | * @param pipe Pipe over which the transfer will happen.
|
|---|
| 297 | * @return Error code.
|
|---|
| 298 | */
|
|---|
| 299 | int usb_pipe_start_long_transfer(usb_pipe_t *pipe)
|
|---|
| 300 | {
|
|---|
| 301 | return pipe_add_ref(pipe);
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | /** Terminate a long transfer on a pipe.
|
|---|
| 305 | *
|
|---|
| 306 | * @see usb_pipe_start_long_transfer
|
|---|
| 307 | *
|
|---|
| 308 | * @param pipe Pipe where to end the long transfer.
|
|---|
| 309 | */
|
|---|
| 310 | void usb_pipe_end_long_transfer(usb_pipe_t *pipe)
|
|---|
| 311 | {
|
|---|
| 312 | pipe_drop_ref(pipe);
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | /**
|
|---|
| 316 | * @}
|
|---|
| 317 | */
|
|---|