[45d105a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
---|
| 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 | */
|
---|
[77ad86c] | 28 |
|
---|
[45d105a] | 29 | /** @addtogroup libusbhost
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief HCD DDF interface implementation
|
---|
| 34 | */
|
---|
[77ad86c] | 35 |
|
---|
[45d105a] | 36 | #include <ddf/driver.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 |
|
---|
| 39 | #include <usb/debug.h>
|
---|
| 40 | #include <usb/host/endpoint.h>
|
---|
| 41 | #include <usb/host/hcd.h>
|
---|
[d8cdf39e] | 42 | #include "ddf_helpers.h"
|
---|
[45d105a] | 43 |
|
---|
[cbd568b] | 44 | /** Calls ep_remove_hook upon endpoint removal. Prints warning.
|
---|
| 45 | * @param ep Endpoint to be unregistered.
|
---|
| 46 | * @param arg hcd_t in disguise.
|
---|
| 47 | */
|
---|
[6b6fc232] | 48 | static void unregister_helper_warn(endpoint_t *ep, void *arg)
|
---|
| 49 | {
|
---|
| 50 | hcd_t *hcd = arg;
|
---|
| 51 | assert(ep);
|
---|
| 52 | assert(hcd);
|
---|
| 53 | usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
|
---|
| 54 | ep->address, ep->endpoint, usb_str_direction(ep->direction));
|
---|
| 55 | if (hcd->ep_remove_hook)
|
---|
| 56 | hcd->ep_remove_hook(hcd, ep);
|
---|
| 57 | }
|
---|
[77ad86c] | 58 |
|
---|
[cbd568b] | 59 | /** Request address interface function.
|
---|
[45d105a] | 60 | *
|
---|
| 61 | * @param[in] fun DDF function that was called.
|
---|
[cbd568b] | 62 | * @param[in] address Pointer to preferred USB address.
|
---|
[45d105a] | 63 | * @param[out] address Place to write a new address.
|
---|
[cbd568b] | 64 | * @param[in] strict Fail if the preferred address is not available.
|
---|
| 65 | * @param[in] speed Speed to associate with the new default address.
|
---|
[45d105a] | 66 | * @return Error code.
|
---|
| 67 | */
|
---|
| 68 | static int request_address(
|
---|
[67f55e7b] | 69 | ddf_fun_t *fun, usb_address_t *address, bool strict, usb_speed_t speed)
|
---|
[45d105a] | 70 | {
|
---|
| 71 | assert(fun);
|
---|
[d8cdf39e] | 72 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
[45d105a] | 73 | assert(hcd);
|
---|
| 74 | assert(address);
|
---|
| 75 |
|
---|
[f37eb84] | 76 | usb_log_debug("Address request: speed: %s, address: %d, strict: %s.\n",
|
---|
| 77 | usb_str_speed(speed), *address, strict ? "YES" : "NO");
|
---|
[0cd8089] | 78 | return usb_device_manager_request_address(
|
---|
[67f55e7b] | 79 | &hcd->dev_manager, address, strict, speed);
|
---|
[45d105a] | 80 | }
|
---|
[77ad86c] | 81 |
|
---|
[cbd568b] | 82 | /** Bind address interface function.
|
---|
[45d105a] | 83 | *
|
---|
| 84 | * @param[in] fun DDF function that was called.
|
---|
| 85 | * @param[in] address Address of the device
|
---|
| 86 | * @param[in] handle Devman handle of the device driver.
|
---|
| 87 | * @return Error code.
|
---|
| 88 | */
|
---|
| 89 | static int bind_address(
|
---|
[cbd568b] | 90 | ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
|
---|
[45d105a] | 91 | {
|
---|
| 92 | assert(fun);
|
---|
[d8cdf39e] | 93 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
[45d105a] | 94 | assert(hcd);
|
---|
| 95 |
|
---|
| 96 | usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
|
---|
[0cd8089] | 97 | return usb_device_manager_bind_address(
|
---|
| 98 | &hcd->dev_manager, address, handle);
|
---|
[45d105a] | 99 | }
|
---|
[77ad86c] | 100 |
|
---|
[45d105a] | 101 | /** Find device handle by address interface function.
|
---|
| 102 | *
|
---|
| 103 | * @param[in] fun DDF function that was called.
|
---|
| 104 | * @param[in] address Address in question.
|
---|
| 105 | * @param[out] handle Where to store device handle if found.
|
---|
| 106 | * @return Error code.
|
---|
| 107 | */
|
---|
| 108 | static int find_by_address(ddf_fun_t *fun, usb_address_t address,
|
---|
| 109 | devman_handle_t *handle)
|
---|
| 110 | {
|
---|
| 111 | assert(fun);
|
---|
[d8cdf39e] | 112 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
[45d105a] | 113 | assert(hcd);
|
---|
[4267908] | 114 | return usb_device_manager_get_info_by_address(
|
---|
| 115 | &hcd->dev_manager, address, handle, NULL);
|
---|
[45d105a] | 116 | }
|
---|
[77ad86c] | 117 |
|
---|
[cbd568b] | 118 | /** Release address interface function.
|
---|
[45d105a] | 119 | *
|
---|
| 120 | * @param[in] fun DDF function that was called.
|
---|
| 121 | * @param[in] address USB address to be released.
|
---|
| 122 | * @return Error code.
|
---|
| 123 | */
|
---|
| 124 | static int release_address(ddf_fun_t *fun, usb_address_t address)
|
---|
| 125 | {
|
---|
| 126 | assert(fun);
|
---|
[d8cdf39e] | 127 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
[45d105a] | 128 | assert(hcd);
|
---|
| 129 | usb_log_debug("Address release %d.\n", address);
|
---|
[6b6fc232] | 130 | usb_endpoint_manager_remove_address(&hcd->ep_manager, address,
|
---|
| 131 | unregister_helper_warn, hcd);
|
---|
[5994cc3] | 132 | usb_device_manager_release_address(&hcd->dev_manager, address);
|
---|
[45d105a] | 133 | return EOK;
|
---|
| 134 | }
|
---|
[77ad86c] | 135 |
|
---|
[cbd568b] | 136 | /** Register endpoint interface function.
|
---|
| 137 | * @param fun DDF function.
|
---|
| 138 | * @param address USB address of the device.
|
---|
| 139 | * @param endpoint USB endpoint number to be registered.
|
---|
| 140 | * @param transfer_type Endpoint's transfer type.
|
---|
| 141 | * @param direction USB communication direction the endpoint is capable of.
|
---|
| 142 | * @param max_packet_size Maximu size of packets the endpoint accepts.
|
---|
| 143 | * @param interval Preferred timeout between communication.
|
---|
| 144 | * @return Error code.
|
---|
| 145 | */
|
---|
[45d105a] | 146 | static int register_endpoint(
|
---|
[27736cf] | 147 | ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
|
---|
[45d105a] | 148 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
---|
[cbd568b] | 149 | size_t max_packet_size, unsigned interval)
|
---|
[45d105a] | 150 | {
|
---|
| 151 | assert(fun);
|
---|
[d8cdf39e] | 152 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
[45d105a] | 153 | assert(hcd);
|
---|
| 154 | const size_t size = max_packet_size;
|
---|
[0816c2e] | 155 | const usb_target_t target = {{.address = address, .endpoint = endpoint}};
|
---|
| 156 |
|
---|
| 157 | usb_log_debug("Register endpoint %d:%d %s-%s %zuB %ums.\n",
|
---|
[45d105a] | 158 | address, endpoint, usb_str_transfer_type(transfer_type),
|
---|
[0816c2e] | 159 | usb_str_direction(direction), max_packet_size, interval);
|
---|
[45d105a] | 160 |
|
---|
[0816c2e] | 161 | return hcd_add_ep(hcd, target, direction, transfer_type,
|
---|
| 162 | max_packet_size, size);
|
---|
[45d105a] | 163 | }
|
---|
[77ad86c] | 164 |
|
---|
[cbd568b] | 165 | /** Unregister endpoint interface function.
|
---|
| 166 | * @param fun DDF function.
|
---|
| 167 | * @param address USB address of the endpoint.
|
---|
| 168 | * @param endpoint USB endpoint number.
|
---|
| 169 | * @param direction Communication direction of the enpdoint to unregister.
|
---|
| 170 | * @return Error code.
|
---|
| 171 | */
|
---|
[45d105a] | 172 | static int unregister_endpoint(
|
---|
| 173 | ddf_fun_t *fun, usb_address_t address,
|
---|
| 174 | usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
| 175 | {
|
---|
| 176 | assert(fun);
|
---|
[d8cdf39e] | 177 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
[45d105a] | 178 | assert(hcd);
|
---|
[0816c2e] | 179 | const usb_target_t target = {{.address = address, .endpoint = endpoint}};
|
---|
[45d105a] | 180 | usb_log_debug("Unregister endpoint %d:%d %s.\n",
|
---|
| 181 | address, endpoint, usb_str_direction(direction));
|
---|
[0816c2e] | 182 | return hcd_remove_ep(hcd, target, direction);
|
---|
[45d105a] | 183 | }
|
---|
[77ad86c] | 184 |
|
---|
[cbd568b] | 185 | /** Inbound communication interface function.
|
---|
| 186 | * @param fun DDF function.
|
---|
| 187 | * @param target Communication target.
|
---|
| 188 | * @param setup_data Data to use in setup stage (control transfers).
|
---|
| 189 | * @param data Pointer to data buffer.
|
---|
| 190 | * @param size Size of the data buffer.
|
---|
| 191 | * @param callback Function to call on communication end.
|
---|
| 192 | * @param arg Argument passed to the callback function.
|
---|
| 193 | * @return Error code.
|
---|
| 194 | */
|
---|
[3822f7c9] | 195 | static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
|
---|
| 196 | uint8_t *data, size_t size, usbhc_iface_transfer_in_callback_t callback,
|
---|
| 197 | void *arg)
|
---|
| 198 | {
|
---|
[d8cdf39e] | 199 | return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)), target, USB_DIRECTION_IN,
|
---|
[d9b2c73] | 200 | data, size, setup_data, callback, NULL, arg, "READ");
|
---|
[3822f7c9] | 201 | }
|
---|
[77ad86c] | 202 |
|
---|
[cbd568b] | 203 | /** Outbound communication interface function.
|
---|
| 204 | * @param fun DDF function.
|
---|
| 205 | * @param target Communication target.
|
---|
| 206 | * @param setup_data Data to use in setup stage (control transfers).
|
---|
| 207 | * @param data Pointer to data buffer.
|
---|
| 208 | * @param size Size of the data buffer.
|
---|
| 209 | * @param callback Function to call on communication end.
|
---|
| 210 | * @param arg Argument passed to the callback function.
|
---|
| 211 | * @return Error code.
|
---|
| 212 | */
|
---|
[3822f7c9] | 213 | static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
|
---|
| 214 | const uint8_t *data, size_t size,
|
---|
| 215 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
| 216 | {
|
---|
[d8cdf39e] | 217 | return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)),
|
---|
| 218 | target, USB_DIRECTION_OUT, (uint8_t*)data, size, setup_data, NULL,
|
---|
| 219 | callback, arg, "WRITE");
|
---|
[45d105a] | 220 | }
|
---|
[77ad86c] | 221 |
|
---|
[cbd568b] | 222 | /** usbhc Interface implementation using hcd_t from libusbhost library. */
|
---|
[45d105a] | 223 | usbhc_iface_t hcd_iface = {
|
---|
| 224 | .request_address = request_address,
|
---|
| 225 | .bind_address = bind_address,
|
---|
[02fc5c4] | 226 | .get_handle = find_by_address,
|
---|
[45d105a] | 227 | .release_address = release_address,
|
---|
| 228 |
|
---|
| 229 | .register_endpoint = register_endpoint,
|
---|
| 230 | .unregister_endpoint = unregister_endpoint,
|
---|
| 231 |
|
---|
[3822f7c9] | 232 | .read = usb_read,
|
---|
| 233 | .write = usb_write,
|
---|
[45d105a] | 234 | };
|
---|
[77ad86c] | 235 |
|
---|
[45d105a] | 236 | /**
|
---|
| 237 | * @}
|
---|
| 238 | */
|
---|