[c5d61ae6] | 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 | * General communication between device drivers and host controller driver.
|
---|
| 34 | */
|
---|
| 35 | #include <devman.h>
|
---|
| 36 | #include <async.h>
|
---|
| 37 | #include <usb_iface.h>
|
---|
| 38 | #include <usb/usbdevice.h>
|
---|
[eb1a2f4] | 39 | #include <usb/debug.h>
|
---|
[c5d61ae6] | 40 | #include <errno.h>
|
---|
[eb1a2f4] | 41 | #include <assert.h>
|
---|
[c5d61ae6] | 42 |
|
---|
| 43 | /** Find host controller handle that is ancestor of given device.
|
---|
| 44 | *
|
---|
| 45 | * @param[in] device_handle Device devman handle.
|
---|
| 46 | * @param[out] hc_handle Where to store handle of host controller
|
---|
| 47 | * controlling device with @p device_handle handle.
|
---|
| 48 | * @return Error code.
|
---|
| 49 | */
|
---|
| 50 | int usb_hc_find(devman_handle_t device_handle, devman_handle_t *hc_handle)
|
---|
| 51 | {
|
---|
| 52 | int parent_phone = devman_parent_device_connect(device_handle,
|
---|
| 53 | IPC_FLAG_BLOCKING);
|
---|
| 54 | if (parent_phone < 0) {
|
---|
| 55 | return parent_phone;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | devman_handle_t h;
|
---|
[eb1a2f4] | 59 | usb_log_debug("asking for HC handle (my handle is %zu).\n", device_handle);
|
---|
[c5d61ae6] | 60 | int rc = async_req_1_1(parent_phone, DEV_IFACE_ID(USB_DEV_IFACE),
|
---|
| 61 | IPC_M_USB_GET_HOST_CONTROLLER_HANDLE, &h);
|
---|
| 62 |
|
---|
| 63 | async_hangup(parent_phone);
|
---|
| 64 |
|
---|
| 65 | if (rc != EOK) {
|
---|
| 66 | return rc;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | if (hc_handle != NULL) {
|
---|
| 70 | *hc_handle = h;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | return EOK;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /** Initialize connection to USB host controller.
|
---|
| 77 | *
|
---|
| 78 | * @param connection Connection to be initialized.
|
---|
| 79 | * @param device Device connecting to the host controller.
|
---|
| 80 | * @return Error code.
|
---|
| 81 | */
|
---|
| 82 | int usb_hc_connection_initialize_from_device(usb_hc_connection_t *connection,
|
---|
[eb1a2f4] | 83 | ddf_dev_t *device)
|
---|
[c5d61ae6] | 84 | {
|
---|
| 85 | assert(connection);
|
---|
| 86 |
|
---|
| 87 | if (device == NULL) {
|
---|
| 88 | return EBADMEM;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | devman_handle_t hc_handle;
|
---|
| 92 | int rc = usb_hc_find(device->handle, &hc_handle);
|
---|
| 93 | if (rc != EOK) {
|
---|
| 94 | return rc;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | rc = usb_hc_connection_initialize(connection, hc_handle);
|
---|
| 98 |
|
---|
| 99 | return rc;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /** Manually initialize connection to USB host controller.
|
---|
| 103 | *
|
---|
| 104 | * @param connection Connection to be initialized.
|
---|
| 105 | * @param hc_handle Devman handle of the host controller.
|
---|
| 106 | * @return Error code.
|
---|
| 107 | */
|
---|
| 108 | int usb_hc_connection_initialize(usb_hc_connection_t *connection,
|
---|
| 109 | devman_handle_t hc_handle)
|
---|
| 110 | {
|
---|
| 111 | assert(connection);
|
---|
| 112 |
|
---|
| 113 | connection->hc_handle = hc_handle;
|
---|
| 114 | connection->hc_phone = -1;
|
---|
| 115 |
|
---|
| 116 | return EOK;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /** Open connection to host controller.
|
---|
| 120 | *
|
---|
| 121 | * @param connection Connection to the host controller.
|
---|
| 122 | * @return Error code.
|
---|
| 123 | */
|
---|
| 124 | int usb_hc_connection_open(usb_hc_connection_t *connection)
|
---|
| 125 | {
|
---|
| 126 | assert(connection);
|
---|
| 127 |
|
---|
| 128 | if (usb_hc_connection_is_opened(connection)) {
|
---|
| 129 | return EBUSY;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | int phone = devman_device_connect(connection->hc_handle, 0);
|
---|
| 133 | if (phone < 0) {
|
---|
| 134 | return phone;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | connection->hc_phone = phone;
|
---|
| 138 |
|
---|
| 139 | return EOK;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /** Tells whether connection to host controller is opened.
|
---|
| 143 | *
|
---|
| 144 | * @param connection Connection to the host controller.
|
---|
| 145 | * @return Whether connection is opened.
|
---|
| 146 | */
|
---|
| 147 | bool usb_hc_connection_is_opened(const usb_hc_connection_t *connection)
|
---|
| 148 | {
|
---|
| 149 | assert(connection);
|
---|
| 150 |
|
---|
| 151 | return (connection->hc_phone >= 0);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /** Close connection to the host controller.
|
---|
| 155 | *
|
---|
| 156 | * @param connection Connection to the host controller.
|
---|
| 157 | * @return Error code.
|
---|
| 158 | */
|
---|
| 159 | int usb_hc_connection_close(usb_hc_connection_t *connection)
|
---|
| 160 | {
|
---|
| 161 | assert(connection);
|
---|
| 162 |
|
---|
| 163 | if (!usb_hc_connection_is_opened(connection)) {
|
---|
| 164 | return ENOENT;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | int rc = async_hangup(connection->hc_phone);
|
---|
| 168 | if (rc != EOK) {
|
---|
| 169 | return rc;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | connection->hc_phone = -1;
|
---|
| 173 |
|
---|
| 174 | return EOK;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /**
|
---|
| 178 | * @}
|
---|
| 179 | */
|
---|