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