[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>
|
---|
[563fb40] | 38 | #include <usbhc_iface.h>
|
---|
[357a302] | 39 | #include <usb_iface.h>
|
---|
[eb1a2f4] | 40 | #include <devman.h>
|
---|
[6865243c] | 41 | #include <errno.h>
|
---|
[43f698b] | 42 | #include <assert.h>
|
---|
[563fb40] | 43 |
|
---|
| 44 | /** Tell USB address assigned to given device.
|
---|
| 45 | *
|
---|
[357a302] | 46 | * @param phone Phone to parent device.
|
---|
[563fb40] | 47 | * @param dev Device in question.
|
---|
| 48 | * @return USB address or error code.
|
---|
| 49 | */
|
---|
[eb1a2f4] | 50 | static usb_address_t get_my_address(int phone, ddf_dev_t *dev)
|
---|
[563fb40] | 51 | {
|
---|
| 52 | sysarg_t address;
|
---|
[eb1a2f4] | 53 |
|
---|
| 54 |
|
---|
| 55 | /*
|
---|
| 56 | * We are sending special value as a handle - zero - to get
|
---|
| 57 | * handle of the parent function (that handle was used
|
---|
| 58 | * when registering our device @p dev.
|
---|
| 59 | */
|
---|
[357a302] | 60 | int rc = async_req_2_1(phone, DEV_IFACE_ID(USB_DEV_IFACE),
|
---|
| 61 | IPC_M_USB_GET_ADDRESS,
|
---|
[eb1a2f4] | 62 | 0, &address);
|
---|
[563fb40] | 63 |
|
---|
| 64 | if (rc != EOK) {
|
---|
| 65 | return rc;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | return (usb_address_t) address;
|
---|
| 69 | }
|
---|
[43f698b] | 70 |
|
---|
[27a0012] | 71 | /** Tell USB interface assigned to given device.
|
---|
| 72 | *
|
---|
| 73 | * @param device Device in question.
|
---|
| 74 | * @return Interface number (negative code means any).
|
---|
| 75 | */
|
---|
[eb1a2f4] | 76 | int usb_device_get_assigned_interface(ddf_dev_t *device)
|
---|
[27a0012] | 77 | {
|
---|
| 78 | int parent_phone = devman_parent_device_connect(device->handle,
|
---|
| 79 | IPC_FLAG_BLOCKING);
|
---|
| 80 | if (parent_phone < 0) {
|
---|
| 81 | return -1;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | sysarg_t iface_no;
|
---|
| 85 | int rc = async_req_2_1(parent_phone, DEV_IFACE_ID(USB_DEV_IFACE),
|
---|
| 86 | IPC_M_USB_GET_INTERFACE,
|
---|
| 87 | device->handle, &iface_no);
|
---|
| 88 |
|
---|
| 89 | async_hangup(parent_phone);
|
---|
| 90 |
|
---|
| 91 | if (rc != EOK) {
|
---|
| 92 | return -1;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | return (int) iface_no;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[6865243c] | 98 | /** Initialize connection to USB device.
|
---|
| 99 | *
|
---|
| 100 | * @param connection Connection structure to be initialized.
|
---|
| 101 | * @param device Generic device backing the USB device.
|
---|
| 102 | * @return Error code.
|
---|
| 103 | */
|
---|
[23c7f4d] | 104 | int usb_device_connection_initialize_from_device(
|
---|
[eb1a2f4] | 105 | usb_device_connection_t *connection, ddf_dev_t *dev)
|
---|
[6865243c] | 106 | {
|
---|
[43f698b] | 107 | assert(connection);
|
---|
[eb1a2f4] | 108 | assert(dev);
|
---|
[43f698b] | 109 |
|
---|
| 110 | int rc;
|
---|
| 111 | devman_handle_t hc_handle;
|
---|
| 112 | usb_address_t my_address;
|
---|
| 113 |
|
---|
[eb1a2f4] | 114 | rc = usb_hc_find(dev->handle, &hc_handle);
|
---|
[43f698b] | 115 | if (rc != EOK) {
|
---|
| 116 | return rc;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[eb1a2f4] | 119 | int parent_phone = devman_parent_device_connect(dev->handle,
|
---|
[357a302] | 120 | IPC_FLAG_BLOCKING);
|
---|
| 121 | if (parent_phone < 0) {
|
---|
| 122 | return parent_phone;
|
---|
[43f698b] | 123 | }
|
---|
| 124 |
|
---|
[eb1a2f4] | 125 | my_address = get_my_address(parent_phone, dev);
|
---|
[43f698b] | 126 | if (my_address < 0) {
|
---|
[2a11192] | 127 | rc = my_address;
|
---|
| 128 | goto leave;
|
---|
[43f698b] | 129 | }
|
---|
| 130 |
|
---|
[52fb76e] | 131 | rc = usb_device_connection_initialize(connection,
|
---|
| 132 | hc_handle, my_address);
|
---|
| 133 |
|
---|
[2a11192] | 134 | leave:
|
---|
[357a302] | 135 | async_hangup(parent_phone);
|
---|
[52fb76e] | 136 | return rc;
|
---|
[6865243c] | 137 | }
|
---|
| 138 |
|
---|
[52fb76e] | 139 | /** Initialize connection to USB device.
|
---|
| 140 | *
|
---|
| 141 | * @param connection Connection structure to be initialized.
|
---|
| 142 | * @param host_controller_handle Devman handle of host controller device is
|
---|
| 143 | * connected to.
|
---|
| 144 | * @param device_address Device USB address.
|
---|
| 145 | * @return Error code.
|
---|
| 146 | */
|
---|
| 147 | int usb_device_connection_initialize(usb_device_connection_t *connection,
|
---|
| 148 | devman_handle_t host_controller_handle, usb_address_t device_address)
|
---|
| 149 | {
|
---|
| 150 | assert(connection);
|
---|
| 151 |
|
---|
| 152 | if ((device_address < 0) || (device_address >= USB11_ADDRESS_MAX)) {
|
---|
| 153 | return EINVAL;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | connection->hc_handle = host_controller_handle;
|
---|
| 157 | connection->address = device_address;
|
---|
| 158 |
|
---|
| 159 | return EOK;
|
---|
| 160 | }
|
---|
[6865243c] | 161 |
|
---|
[d714945] | 162 | /** Initialize connection to USB device on default address.
|
---|
| 163 | *
|
---|
| 164 | * @param dev_connection Device connection structure to be initialized.
|
---|
| 165 | * @param hc_connection Initialized connection to host controller.
|
---|
| 166 | * @return Error code.
|
---|
| 167 | */
|
---|
| 168 | int usb_device_connection_initialize_on_default_address(
|
---|
| 169 | usb_device_connection_t *dev_connection,
|
---|
| 170 | usb_hc_connection_t *hc_connection)
|
---|
| 171 | {
|
---|
| 172 | assert(dev_connection);
|
---|
| 173 |
|
---|
| 174 | if (hc_connection == NULL) {
|
---|
| 175 | return EBADMEM;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | return usb_device_connection_initialize(dev_connection,
|
---|
| 179 | hc_connection->hc_handle, (usb_address_t) 0);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[6865243c] | 182 |
|
---|
| 183 | /** Start a session on the endpoint pipe.
|
---|
| 184 | *
|
---|
| 185 | * A session is something inside what any communication occurs.
|
---|
| 186 | * It is expected that sessions would be started right before the transfer
|
---|
| 187 | * and ended - see usb_endpoint_pipe_end_session() - after the last
|
---|
| 188 | * transfer.
|
---|
| 189 | * The reason for this is that session actually opens some communication
|
---|
| 190 | * channel to the host controller (or to the physical hardware if you
|
---|
| 191 | * wish) and thus it involves acquiring kernel resources.
|
---|
| 192 | * Since they are limited, sessions shall not be longer than strictly
|
---|
| 193 | * necessary.
|
---|
| 194 | *
|
---|
| 195 | * @param pipe Endpoint pipe to start the session on.
|
---|
| 196 | * @return Error code.
|
---|
| 197 | */
|
---|
| 198 | int usb_endpoint_pipe_start_session(usb_endpoint_pipe_t *pipe)
|
---|
| 199 | {
|
---|
[43f698b] | 200 | assert(pipe);
|
---|
| 201 |
|
---|
[e936e8e] | 202 | if (usb_endpoint_pipe_is_session_started(pipe)) {
|
---|
[43f698b] | 203 | return EBUSY;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | int phone = devman_device_connect(pipe->wire->hc_handle, 0);
|
---|
| 207 | if (phone < 0) {
|
---|
| 208 | return phone;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | pipe->hc_phone = phone;
|
---|
| 212 |
|
---|
| 213 | return EOK;
|
---|
[6865243c] | 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | /** Ends a session on the endpoint pipe.
|
---|
| 218 | *
|
---|
| 219 | * @see usb_endpoint_pipe_start_session
|
---|
| 220 | *
|
---|
| 221 | * @param pipe Endpoint pipe to end the session on.
|
---|
| 222 | * @return Error code.
|
---|
| 223 | */
|
---|
| 224 | int usb_endpoint_pipe_end_session(usb_endpoint_pipe_t *pipe)
|
---|
| 225 | {
|
---|
[43f698b] | 226 | assert(pipe);
|
---|
| 227 |
|
---|
[e936e8e] | 228 | if (!usb_endpoint_pipe_is_session_started(pipe)) {
|
---|
[43f698b] | 229 | return ENOENT;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[17aca1c] | 232 | int rc = async_hangup(pipe->hc_phone);
|
---|
[43f698b] | 233 | if (rc != EOK) {
|
---|
| 234 | return rc;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | pipe->hc_phone = -1;
|
---|
| 238 |
|
---|
| 239 | return EOK;
|
---|
[6865243c] | 240 | }
|
---|
| 241 |
|
---|
[e936e8e] | 242 | /** Tell whether a session is started (open) on the endpoint pipe.
|
---|
| 243 | *
|
---|
| 244 | * The expected usage of this function is in assertions for some
|
---|
| 245 | * nested functions.
|
---|
| 246 | *
|
---|
| 247 | * @param pipe Endpoint pipe in question.
|
---|
| 248 | * @return Whether @p pipe has opened a session.
|
---|
| 249 | */
|
---|
| 250 | bool usb_endpoint_pipe_is_session_started(usb_endpoint_pipe_t *pipe)
|
---|
| 251 | {
|
---|
| 252 | return (pipe->hc_phone >= 0);
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[6865243c] | 255 | /**
|
---|
| 256 | * @}
|
---|
| 257 | */
|
---|