[6865243c] | 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 |
|
---|
[160b75e] | 29 | /** @addtogroup libusbdev
|
---|
[6865243c] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[dc04868] | 33 | * USB endpoint pipes miscellaneous functions.
|
---|
[6865243c] | 34 | */
|
---|
| 35 | #include <usb/usb.h>
|
---|
[7d521e24] | 36 | #include <usb/dev/pipes.h>
|
---|
[eb1a2f4] | 37 | #include <usb/debug.h>
|
---|
[e8f826b] | 38 | #include <usb/hc.h>
|
---|
[563fb40] | 39 | #include <usbhc_iface.h>
|
---|
[357a302] | 40 | #include <usb_iface.h>
|
---|
[eb1a2f4] | 41 | #include <devman.h>
|
---|
[6865243c] | 42 | #include <errno.h>
|
---|
[43f698b] | 43 | #include <assert.h>
|
---|
[a546687] | 44 | #include "pipepriv.h"
|
---|
[563fb40] | 45 |
|
---|
[f0fdc7d] | 46 | #define IPC_AGAIN_DELAY (1000 * 2) /* 2ms */
|
---|
| 47 |
|
---|
[563fb40] | 48 | /** Tell USB address assigned to given device.
|
---|
| 49 | *
|
---|
[79ae36dd] | 50 | * @param sess Session to parent device.
|
---|
[563fb40] | 51 | * @param dev Device in question.
|
---|
| 52 | * @return USB address or error code.
|
---|
| 53 | */
|
---|
[8e5ce07] | 54 | static usb_address_t get_my_address(async_sess_t *sess, const ddf_dev_t *dev)
|
---|
[563fb40] | 55 | {
|
---|
[79ae36dd] | 56 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 57 |
|
---|
[eb1a2f4] | 58 | /*
|
---|
| 59 | * We are sending special value as a handle - zero - to get
|
---|
| 60 | * handle of the parent function (that handle was used
|
---|
| 61 | * when registering our device @p dev.
|
---|
| 62 | */
|
---|
[79ae36dd] | 63 | sysarg_t address;
|
---|
| 64 | int rc = async_req_2_1(exch, DEV_IFACE_ID(USB_DEV_IFACE),
|
---|
| 65 | IPC_M_USB_GET_ADDRESS, 0, &address);
|
---|
| 66 |
|
---|
| 67 | async_exchange_end(exch);
|
---|
| 68 |
|
---|
| 69 | if (rc != EOK)
|
---|
[563fb40] | 70 | return rc;
|
---|
[79ae36dd] | 71 |
|
---|
[563fb40] | 72 | return (usb_address_t) address;
|
---|
| 73 | }
|
---|
[43f698b] | 74 |
|
---|
[27a0012] | 75 | /** Tell USB interface assigned to given device.
|
---|
| 76 | *
|
---|
| 77 | * @param device Device in question.
|
---|
| 78 | * @return Interface number (negative code means any).
|
---|
| 79 | */
|
---|
[8e5ce07] | 80 | int usb_device_get_assigned_interface(const ddf_dev_t *device)
|
---|
[27a0012] | 81 | {
|
---|
[79ae36dd] | 82 | async_sess_t *parent_sess =
|
---|
[9f7276d] | 83 | devman_parent_device_connect(EXCHANGE_ATOMIC, device->handle,
|
---|
[27a0012] | 84 | IPC_FLAG_BLOCKING);
|
---|
[79ae36dd] | 85 | if (!parent_sess)
|
---|
[27a0012] | 86 | return -1;
|
---|
[79ae36dd] | 87 |
|
---|
| 88 | async_exch_t *exch = async_exchange_begin(parent_sess);
|
---|
| 89 |
|
---|
[27a0012] | 90 | sysarg_t iface_no;
|
---|
[79ae36dd] | 91 | int rc = async_req_2_1(exch, DEV_IFACE_ID(USB_DEV_IFACE),
|
---|
| 92 | IPC_M_USB_GET_INTERFACE, device->handle, &iface_no);
|
---|
| 93 |
|
---|
| 94 | async_exchange_end(exch);
|
---|
| 95 | async_hangup(parent_sess);
|
---|
| 96 |
|
---|
| 97 | if (rc != EOK)
|
---|
[27a0012] | 98 | return -1;
|
---|
[79ae36dd] | 99 |
|
---|
[27a0012] | 100 | return (int) iface_no;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[6865243c] | 103 | /** Initialize connection to USB device.
|
---|
| 104 | *
|
---|
| 105 | * @param connection Connection structure to be initialized.
|
---|
[bc1c6fb] | 106 | * @param dev Generic device backing the USB device.
|
---|
[6865243c] | 107 | * @return Error code.
|
---|
| 108 | */
|
---|
[23c7f4d] | 109 | int usb_device_connection_initialize_from_device(
|
---|
[8e5ce07] | 110 | usb_device_connection_t *connection, const ddf_dev_t *dev)
|
---|
[6865243c] | 111 | {
|
---|
[43f698b] | 112 | assert(connection);
|
---|
[eb1a2f4] | 113 | assert(dev);
|
---|
[79ae36dd] | 114 |
|
---|
[43f698b] | 115 | int rc;
|
---|
| 116 | devman_handle_t hc_handle;
|
---|
| 117 | usb_address_t my_address;
|
---|
[79ae36dd] | 118 |
|
---|
[eb1a2f4] | 119 | rc = usb_hc_find(dev->handle, &hc_handle);
|
---|
[79ae36dd] | 120 | if (rc != EOK)
|
---|
[43f698b] | 121 | return rc;
|
---|
[79ae36dd] | 122 |
|
---|
| 123 | async_sess_t *parent_sess =
|
---|
[9f7276d] | 124 | devman_parent_device_connect(EXCHANGE_ATOMIC, dev->handle,
|
---|
[357a302] | 125 | IPC_FLAG_BLOCKING);
|
---|
[79ae36dd] | 126 | if (!parent_sess)
|
---|
| 127 | return ENOMEM;
|
---|
| 128 |
|
---|
[f0fdc7d] | 129 | /*
|
---|
| 130 | * Asking for "my" address may require several attempts.
|
---|
| 131 | * That is because following scenario may happen:
|
---|
| 132 | * - parent driver (i.e. driver of parent device) announces new device
|
---|
| 133 | * and devman launches current driver
|
---|
| 134 | * - parent driver is preempted and thus does not send address-handle
|
---|
| 135 | * binding to HC driver
|
---|
| 136 | * - this driver gets here and wants the binding
|
---|
| 137 | * - the HC does not know the binding yet and thus it answers ENOENT
|
---|
| 138 | * So, we need to wait for the HC to learn the binding.
|
---|
| 139 | */
|
---|
[79ae36dd] | 140 |
|
---|
[f0fdc7d] | 141 | do {
|
---|
[79ae36dd] | 142 | my_address = get_my_address(parent_sess, dev);
|
---|
| 143 |
|
---|
[f0fdc7d] | 144 | if (my_address == ENOENT) {
|
---|
| 145 | /* Be nice, let other fibrils run and try again. */
|
---|
| 146 | async_usleep(IPC_AGAIN_DELAY);
|
---|
| 147 | } else if (my_address < 0) {
|
---|
| 148 | /* Some other problem, no sense trying again. */
|
---|
| 149 | rc = my_address;
|
---|
| 150 | goto leave;
|
---|
| 151 | }
|
---|
[79ae36dd] | 152 |
|
---|
[f0fdc7d] | 153 | } while (my_address < 0);
|
---|
[79ae36dd] | 154 |
|
---|
[52fb76e] | 155 | rc = usb_device_connection_initialize(connection,
|
---|
| 156 | hc_handle, my_address);
|
---|
[79ae36dd] | 157 |
|
---|
[2a11192] | 158 | leave:
|
---|
[79ae36dd] | 159 | async_hangup(parent_sess);
|
---|
[52fb76e] | 160 | return rc;
|
---|
[6865243c] | 161 | }
|
---|
| 162 |
|
---|
[52fb76e] | 163 | /** Initialize connection to USB device.
|
---|
| 164 | *
|
---|
| 165 | * @param connection Connection structure to be initialized.
|
---|
| 166 | * @param host_controller_handle Devman handle of host controller device is
|
---|
| 167 | * connected to.
|
---|
| 168 | * @param device_address Device USB address.
|
---|
| 169 | * @return Error code.
|
---|
| 170 | */
|
---|
| 171 | int usb_device_connection_initialize(usb_device_connection_t *connection,
|
---|
| 172 | devman_handle_t host_controller_handle, usb_address_t device_address)
|
---|
| 173 | {
|
---|
| 174 | assert(connection);
|
---|
| 175 |
|
---|
| 176 | if ((device_address < 0) || (device_address >= USB11_ADDRESS_MAX)) {
|
---|
| 177 | return EINVAL;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | connection->hc_handle = host_controller_handle;
|
---|
| 181 | connection->address = device_address;
|
---|
| 182 |
|
---|
| 183 | return EOK;
|
---|
| 184 | }
|
---|
[6865243c] | 185 |
|
---|
[d714945] | 186 | /** Initialize connection to USB device on default address.
|
---|
| 187 | *
|
---|
| 188 | * @param dev_connection Device connection structure to be initialized.
|
---|
| 189 | * @param hc_connection Initialized connection to host controller.
|
---|
| 190 | * @return Error code.
|
---|
| 191 | */
|
---|
| 192 | int usb_device_connection_initialize_on_default_address(
|
---|
| 193 | usb_device_connection_t *dev_connection,
|
---|
| 194 | usb_hc_connection_t *hc_connection)
|
---|
| 195 | {
|
---|
| 196 | assert(dev_connection);
|
---|
| 197 |
|
---|
| 198 | if (hc_connection == NULL) {
|
---|
| 199 | return EBADMEM;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | return usb_device_connection_initialize(dev_connection,
|
---|
| 203 | hc_connection->hc_handle, (usb_address_t) 0);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[e9ce696] | 206 | /** Prepare pipe for a long transfer.
|
---|
| 207 | *
|
---|
| 208 | * By a long transfer is mean transfer consisting of several
|
---|
| 209 | * requests to the HC.
|
---|
| 210 | * Calling such function is optional and it has positive effect of
|
---|
| 211 | * improved performance because IPC session is initiated only once.
|
---|
| 212 | *
|
---|
| 213 | * @param pipe Pipe over which the transfer will happen.
|
---|
| 214 | * @return Error code.
|
---|
| 215 | */
|
---|
[2c2cbcf] | 216 | void usb_pipe_start_long_transfer(usb_pipe_t *pipe)
|
---|
[e9ce696] | 217 | {
|
---|
[2c2cbcf] | 218 | (void) pipe_add_ref(pipe, true);
|
---|
[e9ce696] | 219 | }
|
---|
| 220 |
|
---|
| 221 | /** Terminate a long transfer on a pipe.
|
---|
| 222 | *
|
---|
| 223 | * @see usb_pipe_start_long_transfer
|
---|
| 224 | *
|
---|
| 225 | * @param pipe Pipe where to end the long transfer.
|
---|
| 226 | */
|
---|
| 227 | void usb_pipe_end_long_transfer(usb_pipe_t *pipe)
|
---|
| 228 | {
|
---|
| 229 | pipe_drop_ref(pipe);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[6865243c] | 232 | /**
|
---|
| 233 | * @}
|
---|
| 234 | */
|
---|