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