[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>
|
---|
[563fb40] | 37 | #include <usbhc_iface.h>
|
---|
[6865243c] | 38 | #include <errno.h>
|
---|
[43f698b] | 39 | #include <assert.h>
|
---|
[563fb40] | 40 |
|
---|
| 41 | /** Tell USB address assigned to given device.
|
---|
| 42 | *
|
---|
| 43 | * @param phone Phone to my HC.
|
---|
| 44 | * @param dev Device in question.
|
---|
| 45 | * @return USB address or error code.
|
---|
| 46 | */
|
---|
| 47 | static usb_address_t get_my_address(int phone, device_t *dev)
|
---|
| 48 | {
|
---|
| 49 | sysarg_t address;
|
---|
| 50 | int rc = async_req_2_1(phone, DEV_IFACE_ID(USBHC_DEV_IFACE),
|
---|
| 51 | IPC_M_USBHC_GET_ADDRESS,
|
---|
| 52 | dev->handle, &address);
|
---|
| 53 |
|
---|
| 54 | if (rc != EOK) {
|
---|
| 55 | return rc;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | return (usb_address_t) address;
|
---|
| 59 | }
|
---|
[43f698b] | 60 |
|
---|
[6865243c] | 61 | /** Initialize connection to USB device.
|
---|
| 62 | *
|
---|
| 63 | * @param connection Connection structure to be initialized.
|
---|
| 64 | * @param device Generic device backing the USB device.
|
---|
| 65 | * @return Error code.
|
---|
| 66 | */
|
---|
[23c7f4d] | 67 | int usb_device_connection_initialize_from_device(
|
---|
| 68 | usb_device_connection_t *connection, device_t *device)
|
---|
[6865243c] | 69 | {
|
---|
[43f698b] | 70 | assert(connection);
|
---|
| 71 | assert(device);
|
---|
| 72 |
|
---|
| 73 | int rc;
|
---|
| 74 | devman_handle_t hc_handle;
|
---|
| 75 | usb_address_t my_address;
|
---|
| 76 |
|
---|
[563fb40] | 77 | rc = usb_hc_find(device->handle, &hc_handle);
|
---|
[43f698b] | 78 | if (rc != EOK) {
|
---|
| 79 | return rc;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | int hc_phone = devman_device_connect(hc_handle, 0);
|
---|
| 83 | if (hc_phone < 0) {
|
---|
| 84 | return hc_phone;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[563fb40] | 87 | my_address = get_my_address(hc_phone, device);
|
---|
[43f698b] | 88 | if (my_address < 0) {
|
---|
[2a11192] | 89 | rc = my_address;
|
---|
| 90 | goto leave;
|
---|
[43f698b] | 91 | }
|
---|
| 92 |
|
---|
[52fb76e] | 93 | rc = usb_device_connection_initialize(connection,
|
---|
| 94 | hc_handle, my_address);
|
---|
| 95 |
|
---|
[2a11192] | 96 | leave:
|
---|
[17aca1c] | 97 | async_hangup(hc_phone);
|
---|
[52fb76e] | 98 | return rc;
|
---|
[6865243c] | 99 | }
|
---|
| 100 |
|
---|
[52fb76e] | 101 | /** Initialize connection to USB device.
|
---|
| 102 | *
|
---|
| 103 | * @param connection Connection structure to be initialized.
|
---|
| 104 | * @param host_controller_handle Devman handle of host controller device is
|
---|
| 105 | * connected to.
|
---|
| 106 | * @param device_address Device USB address.
|
---|
| 107 | * @return Error code.
|
---|
| 108 | */
|
---|
| 109 | int usb_device_connection_initialize(usb_device_connection_t *connection,
|
---|
| 110 | devman_handle_t host_controller_handle, usb_address_t device_address)
|
---|
| 111 | {
|
---|
| 112 | assert(connection);
|
---|
| 113 |
|
---|
| 114 | if ((device_address < 0) || (device_address >= USB11_ADDRESS_MAX)) {
|
---|
| 115 | return EINVAL;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | connection->hc_handle = host_controller_handle;
|
---|
| 119 | connection->address = device_address;
|
---|
| 120 |
|
---|
| 121 | return EOK;
|
---|
| 122 | }
|
---|
[6865243c] | 123 |
|
---|
[d714945] | 124 | /** Initialize connection to USB device on default address.
|
---|
| 125 | *
|
---|
| 126 | * @param dev_connection Device connection structure to be initialized.
|
---|
| 127 | * @param hc_connection Initialized connection to host controller.
|
---|
| 128 | * @return Error code.
|
---|
| 129 | */
|
---|
| 130 | int usb_device_connection_initialize_on_default_address(
|
---|
| 131 | usb_device_connection_t *dev_connection,
|
---|
| 132 | usb_hc_connection_t *hc_connection)
|
---|
| 133 | {
|
---|
| 134 | assert(dev_connection);
|
---|
| 135 |
|
---|
| 136 | if (hc_connection == NULL) {
|
---|
| 137 | return EBADMEM;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | return usb_device_connection_initialize(dev_connection,
|
---|
| 141 | hc_connection->hc_handle, (usb_address_t) 0);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[6865243c] | 144 |
|
---|
| 145 | /** Start a session on the endpoint pipe.
|
---|
| 146 | *
|
---|
| 147 | * A session is something inside what any communication occurs.
|
---|
| 148 | * It is expected that sessions would be started right before the transfer
|
---|
| 149 | * and ended - see usb_endpoint_pipe_end_session() - after the last
|
---|
| 150 | * transfer.
|
---|
| 151 | * The reason for this is that session actually opens some communication
|
---|
| 152 | * channel to the host controller (or to the physical hardware if you
|
---|
| 153 | * wish) and thus it involves acquiring kernel resources.
|
---|
| 154 | * Since they are limited, sessions shall not be longer than strictly
|
---|
| 155 | * necessary.
|
---|
| 156 | *
|
---|
| 157 | * @param pipe Endpoint pipe to start the session on.
|
---|
| 158 | * @return Error code.
|
---|
| 159 | */
|
---|
| 160 | int usb_endpoint_pipe_start_session(usb_endpoint_pipe_t *pipe)
|
---|
| 161 | {
|
---|
[43f698b] | 162 | assert(pipe);
|
---|
| 163 |
|
---|
| 164 | if (pipe->hc_phone >= 0) {
|
---|
| 165 | return EBUSY;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | int phone = devman_device_connect(pipe->wire->hc_handle, 0);
|
---|
| 169 | if (phone < 0) {
|
---|
| 170 | return phone;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | pipe->hc_phone = phone;
|
---|
| 174 |
|
---|
| 175 | return EOK;
|
---|
[6865243c] | 176 | }
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 | /** Ends a session on the endpoint pipe.
|
---|
| 180 | *
|
---|
| 181 | * @see usb_endpoint_pipe_start_session
|
---|
| 182 | *
|
---|
| 183 | * @param pipe Endpoint pipe to end the session on.
|
---|
| 184 | * @return Error code.
|
---|
| 185 | */
|
---|
| 186 | int usb_endpoint_pipe_end_session(usb_endpoint_pipe_t *pipe)
|
---|
| 187 | {
|
---|
[43f698b] | 188 | assert(pipe);
|
---|
| 189 |
|
---|
| 190 | if (pipe->hc_phone < 0) {
|
---|
| 191 | return ENOENT;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[17aca1c] | 194 | int rc = async_hangup(pipe->hc_phone);
|
---|
[43f698b] | 195 | if (rc != EOK) {
|
---|
| 196 | return rc;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | pipe->hc_phone = -1;
|
---|
| 200 |
|
---|
| 201 | return EOK;
|
---|
[6865243c] | 202 | }
|
---|
| 203 |
|
---|
| 204 | /**
|
---|
| 205 | * @}
|
---|
| 206 | */
|
---|