[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 | */
|
---|
| 28 | /** @addtogroup libusbhost
|
---|
| 29 | * @{
|
---|
| 30 | */
|
---|
| 31 | /** @file
|
---|
| 32 | * @brief HCD DDF interface implementation
|
---|
| 33 | */
|
---|
| 34 | #include <ddf/driver.h>
|
---|
| 35 | #include <errno.h>
|
---|
| 36 |
|
---|
| 37 | #include <usb/debug.h>
|
---|
| 38 | #include <usb/host/endpoint.h>
|
---|
| 39 | #include <usb/host/hcd.h>
|
---|
| 40 |
|
---|
[cbd568b] | 41 | /** Prepare generic usb_transfer_batch and schedule it.
|
---|
| 42 | * @param fun DDF fun
|
---|
| 43 | * @param target address and endpoint number.
|
---|
| 44 | * @param setup_data Data to use in setup stage (Control communication type)
|
---|
| 45 | * @param in Callback for device to host communication.
|
---|
| 46 | * @param out Callback for host to device communication.
|
---|
| 47 | * @param arg Callback parameter.
|
---|
| 48 | * @param name Communication identifier (for nicer output).
|
---|
| 49 | * @return Error code.
|
---|
| 50 | */
|
---|
[45d105a] | 51 | static inline int send_batch(
|
---|
| 52 | ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
|
---|
[a837544] | 53 | void *data, size_t size, uint64_t setup_data,
|
---|
[45d105a] | 54 | usbhc_iface_transfer_in_callback_t in,
|
---|
| 55 | usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
|
---|
| 56 | {
|
---|
| 57 | assert(fun);
|
---|
| 58 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 59 | assert(hcd);
|
---|
| 60 |
|
---|
[48ae3ef] | 61 | endpoint_t *ep = usb_endpoint_manager_find_ep(&hcd->ep_manager,
|
---|
[83c3123] | 62 | target.address, target.endpoint, direction);
|
---|
[45d105a] | 63 | if (ep == NULL) {
|
---|
| 64 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
| 65 | target.address, target.endpoint, name);
|
---|
| 66 | return ENOENT;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
---|
| 70 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
| 71 |
|
---|
| 72 | const size_t bw = bandwidth_count_usb11(
|
---|
| 73 | ep->speed, ep->transfer_type, size, ep->max_packet_size);
|
---|
[83c3123] | 74 | /* Check if we have enough bandwidth reserved */
|
---|
| 75 | if (ep->bandwidth < bw) {
|
---|
[45d105a] | 76 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
| 77 | "but only %zu is reserved.\n",
|
---|
[83c3123] | 78 | ep->address, ep->endpoint, name, bw, ep->bandwidth);
|
---|
[45d105a] | 79 | return ENOSPC;
|
---|
| 80 | }
|
---|
[70fb822] | 81 | if (!hcd->schedule) {
|
---|
| 82 | usb_log_error("HCD does not implement scheduler.\n");
|
---|
| 83 | return ENOTSUP;
|
---|
[45d105a] | 84 | }
|
---|
| 85 |
|
---|
[70fb822] | 86 | /* No private data and no private data dtor */
|
---|
| 87 | usb_transfer_batch_t *batch =
|
---|
[549ff23] | 88 | usb_transfer_batch_create(ep, data, size, setup_data,
|
---|
[4e9ecf4] | 89 | in, out, arg, fun, NULL, NULL);
|
---|
[70fb822] | 90 | if (!batch) {
|
---|
| 91 | return ENOMEM;
|
---|
[45d105a] | 92 | }
|
---|
[70fb822] | 93 |
|
---|
[83c3123] | 94 | const int ret = hcd->schedule(hcd, batch);
|
---|
[45d105a] | 95 | if (ret != EOK)
|
---|
[549ff23] | 96 | usb_transfer_batch_destroy(batch);
|
---|
[45d105a] | 97 |
|
---|
| 98 | return ret;
|
---|
| 99 | }
|
---|
| 100 | /*----------------------------------------------------------------------------*/
|
---|
[cbd568b] | 101 | /** Calls ep_add_hook upon endpoint registration.
|
---|
| 102 | * @param ep Endpoint to be registered.
|
---|
| 103 | * @param arg hcd_t in disguise.
|
---|
| 104 | * @return Error code.
|
---|
| 105 | */
|
---|
[6b6fc232] | 106 | static int register_helper(endpoint_t *ep, void *arg)
|
---|
| 107 | {
|
---|
| 108 | hcd_t *hcd = arg;
|
---|
| 109 | assert(ep);
|
---|
| 110 | assert(hcd);
|
---|
| 111 | if (hcd->ep_add_hook)
|
---|
| 112 | return hcd->ep_add_hook(hcd, ep);
|
---|
| 113 | return EOK;
|
---|
| 114 | }
|
---|
| 115 | /*----------------------------------------------------------------------------*/
|
---|
[cbd568b] | 116 | /** Calls ep_remove_hook upon endpoint removal.
|
---|
| 117 | * @param ep Endpoint to be unregistered.
|
---|
| 118 | * @param arg hcd_t in disguise.
|
---|
| 119 | */
|
---|
[6b6fc232] | 120 | static void unregister_helper(endpoint_t *ep, void *arg)
|
---|
| 121 | {
|
---|
| 122 | hcd_t *hcd = arg;
|
---|
| 123 | assert(ep);
|
---|
| 124 | assert(hcd);
|
---|
| 125 | if (hcd->ep_remove_hook)
|
---|
| 126 | hcd->ep_remove_hook(hcd, ep);
|
---|
| 127 | }
|
---|
| 128 | /*----------------------------------------------------------------------------*/
|
---|
[cbd568b] | 129 | /** Calls ep_remove_hook upon endpoint removal. Prints warning.
|
---|
| 130 | * @param ep Endpoint to be unregistered.
|
---|
| 131 | * @param arg hcd_t in disguise.
|
---|
| 132 | */
|
---|
[6b6fc232] | 133 | static void unregister_helper_warn(endpoint_t *ep, void *arg)
|
---|
| 134 | {
|
---|
| 135 | hcd_t *hcd = arg;
|
---|
| 136 | assert(ep);
|
---|
| 137 | assert(hcd);
|
---|
| 138 | usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
|
---|
| 139 | ep->address, ep->endpoint, usb_str_direction(ep->direction));
|
---|
| 140 | if (hcd->ep_remove_hook)
|
---|
| 141 | hcd->ep_remove_hook(hcd, ep);
|
---|
| 142 | }
|
---|
| 143 | /*----------------------------------------------------------------------------*/
|
---|
[cbd568b] | 144 | /** Request address interface function.
|
---|
[45d105a] | 145 | *
|
---|
| 146 | * @param[in] fun DDF function that was called.
|
---|
[cbd568b] | 147 | * @param[in] address Pointer to preferred USB address.
|
---|
[45d105a] | 148 | * @param[out] address Place to write a new address.
|
---|
[cbd568b] | 149 | * @param[in] strict Fail if the preferred address is not available.
|
---|
| 150 | * @param[in] speed Speed to associate with the new default address.
|
---|
[45d105a] | 151 | * @return Error code.
|
---|
| 152 | */
|
---|
| 153 | static int request_address(
|
---|
[67f55e7b] | 154 | ddf_fun_t *fun, usb_address_t *address, bool strict, usb_speed_t speed)
|
---|
[45d105a] | 155 | {
|
---|
| 156 | assert(fun);
|
---|
| 157 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 158 | assert(hcd);
|
---|
| 159 | assert(address);
|
---|
| 160 |
|
---|
[f37eb84] | 161 | usb_log_debug("Address request: speed: %s, address: %d, strict: %s.\n",
|
---|
| 162 | usb_str_speed(speed), *address, strict ? "YES" : "NO");
|
---|
[0cd8089] | 163 | return usb_device_manager_request_address(
|
---|
[67f55e7b] | 164 | &hcd->dev_manager, address, strict, speed);
|
---|
[45d105a] | 165 | }
|
---|
| 166 | /*----------------------------------------------------------------------------*/
|
---|
[cbd568b] | 167 | /** Bind address interface function.
|
---|
[45d105a] | 168 | *
|
---|
| 169 | * @param[in] fun DDF function that was called.
|
---|
| 170 | * @param[in] address Address of the device
|
---|
| 171 | * @param[in] handle Devman handle of the device driver.
|
---|
| 172 | * @return Error code.
|
---|
| 173 | */
|
---|
| 174 | static int bind_address(
|
---|
[cbd568b] | 175 | ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
|
---|
[45d105a] | 176 | {
|
---|
| 177 | assert(fun);
|
---|
| 178 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 179 | assert(hcd);
|
---|
| 180 |
|
---|
| 181 | usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
|
---|
[0cd8089] | 182 | return usb_device_manager_bind_address(
|
---|
| 183 | &hcd->dev_manager, address, handle);
|
---|
[45d105a] | 184 | }
|
---|
| 185 | /*----------------------------------------------------------------------------*/
|
---|
| 186 | /** Find device handle by address interface function.
|
---|
| 187 | *
|
---|
| 188 | * @param[in] fun DDF function that was called.
|
---|
| 189 | * @param[in] address Address in question.
|
---|
| 190 | * @param[out] handle Where to store device handle if found.
|
---|
| 191 | * @return Error code.
|
---|
| 192 | */
|
---|
| 193 | static int find_by_address(ddf_fun_t *fun, usb_address_t address,
|
---|
| 194 | devman_handle_t *handle)
|
---|
| 195 | {
|
---|
| 196 | assert(fun);
|
---|
| 197 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 198 | assert(hcd);
|
---|
[4267908] | 199 | return usb_device_manager_get_info_by_address(
|
---|
| 200 | &hcd->dev_manager, address, handle, NULL);
|
---|
[45d105a] | 201 | }
|
---|
| 202 | /*----------------------------------------------------------------------------*/
|
---|
[cbd568b] | 203 | /** Release address interface function.
|
---|
[45d105a] | 204 | *
|
---|
| 205 | * @param[in] fun DDF function that was called.
|
---|
| 206 | * @param[in] address USB address to be released.
|
---|
| 207 | * @return Error code.
|
---|
| 208 | */
|
---|
| 209 | static int release_address(ddf_fun_t *fun, usb_address_t address)
|
---|
| 210 | {
|
---|
| 211 | assert(fun);
|
---|
| 212 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 213 | assert(hcd);
|
---|
| 214 | usb_log_debug("Address release %d.\n", address);
|
---|
[0cd8089] | 215 | usb_device_manager_release_address(&hcd->dev_manager, address);
|
---|
[6b6fc232] | 216 | usb_endpoint_manager_remove_address(&hcd->ep_manager, address,
|
---|
| 217 | unregister_helper_warn, hcd);
|
---|
[45d105a] | 218 | return EOK;
|
---|
| 219 | }
|
---|
| 220 | /*----------------------------------------------------------------------------*/
|
---|
[cbd568b] | 221 | /** Register endpoint interface function.
|
---|
| 222 | * @param fun DDF function.
|
---|
| 223 | * @param address USB address of the device.
|
---|
| 224 | * @param endpoint USB endpoint number to be registered.
|
---|
| 225 | * @param transfer_type Endpoint's transfer type.
|
---|
| 226 | * @param direction USB communication direction the endpoint is capable of.
|
---|
| 227 | * @param max_packet_size Maximu size of packets the endpoint accepts.
|
---|
| 228 | * @param interval Preferred timeout between communication.
|
---|
| 229 | * @return Error code.
|
---|
| 230 | */
|
---|
[45d105a] | 231 | static int register_endpoint(
|
---|
[27736cf] | 232 | ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
|
---|
[45d105a] | 233 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
---|
[cbd568b] | 234 | size_t max_packet_size, unsigned interval)
|
---|
[45d105a] | 235 | {
|
---|
| 236 | assert(fun);
|
---|
| 237 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 238 | assert(hcd);
|
---|
| 239 | const size_t size = max_packet_size;
|
---|
[1b17e37] | 240 | usb_speed_t speed = USB_SPEED_MAX;
|
---|
| 241 | const int ret = usb_device_manager_get_info_by_address(
|
---|
[4267908] | 242 | &hcd->dev_manager, address, NULL, &speed);
|
---|
[1b17e37] | 243 | if (ret != EOK) {
|
---|
| 244 | return ret;
|
---|
| 245 | }
|
---|
[0815000] | 246 |
|
---|
[45d105a] | 247 | usb_log_debug("Register endpoint %d:%d %s-%s %s %zuB %ums.\n",
|
---|
| 248 | address, endpoint, usb_str_transfer_type(transfer_type),
|
---|
| 249 | usb_str_direction(direction), usb_str_speed(speed),
|
---|
| 250 | max_packet_size, interval);
|
---|
| 251 |
|
---|
[48ae3ef] | 252 | return usb_endpoint_manager_add_ep(&hcd->ep_manager, address, endpoint,
|
---|
| 253 | direction, transfer_type, speed, max_packet_size, size,
|
---|
| 254 | register_helper, hcd);
|
---|
[45d105a] | 255 | }
|
---|
| 256 | /*----------------------------------------------------------------------------*/
|
---|
[cbd568b] | 257 | /** Unregister endpoint interface function.
|
---|
| 258 | * @param fun DDF function.
|
---|
| 259 | * @param address USB address of the endpoint.
|
---|
| 260 | * @param endpoint USB endpoint number.
|
---|
| 261 | * @param direction Communication direction of the enpdoint to unregister.
|
---|
| 262 | * @return Error code.
|
---|
| 263 | */
|
---|
[45d105a] | 264 | static int unregister_endpoint(
|
---|
| 265 | ddf_fun_t *fun, usb_address_t address,
|
---|
| 266 | usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
| 267 | {
|
---|
| 268 | assert(fun);
|
---|
| 269 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 270 | assert(hcd);
|
---|
| 271 | usb_log_debug("Unregister endpoint %d:%d %s.\n",
|
---|
| 272 | address, endpoint, usb_str_direction(direction));
|
---|
[48ae3ef] | 273 | return usb_endpoint_manager_remove_ep(&hcd->ep_manager, address,
|
---|
| 274 | endpoint, direction, unregister_helper, hcd);
|
---|
[45d105a] | 275 | }
|
---|
| 276 | /*----------------------------------------------------------------------------*/
|
---|
[cbd568b] | 277 | /** Inbound communication interface function.
|
---|
| 278 | * @param fun DDF function.
|
---|
| 279 | * @param target Communication target.
|
---|
| 280 | * @param setup_data Data to use in setup stage (control transfers).
|
---|
| 281 | * @param data Pointer to data buffer.
|
---|
| 282 | * @param size Size of the data buffer.
|
---|
| 283 | * @param callback Function to call on communication end.
|
---|
| 284 | * @param arg Argument passed to the callback function.
|
---|
| 285 | * @return Error code.
|
---|
| 286 | */
|
---|
[3822f7c9] | 287 | static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
|
---|
| 288 | uint8_t *data, size_t size, usbhc_iface_transfer_in_callback_t callback,
|
---|
| 289 | void *arg)
|
---|
| 290 | {
|
---|
| 291 | return send_batch(fun, target, USB_DIRECTION_IN, data, size,
|
---|
[a837544] | 292 | setup_data, callback, NULL, arg, "READ");
|
---|
[3822f7c9] | 293 | }
|
---|
| 294 | /*----------------------------------------------------------------------------*/
|
---|
[cbd568b] | 295 | /** Outbound communication interface function.
|
---|
| 296 | * @param fun DDF function.
|
---|
| 297 | * @param target Communication target.
|
---|
| 298 | * @param setup_data Data to use in setup stage (control transfers).
|
---|
| 299 | * @param data Pointer to data buffer.
|
---|
| 300 | * @param size Size of the data buffer.
|
---|
| 301 | * @param callback Function to call on communication end.
|
---|
| 302 | * @param arg Argument passed to the callback function.
|
---|
| 303 | * @return Error code.
|
---|
| 304 | */
|
---|
[3822f7c9] | 305 | static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
|
---|
| 306 | const uint8_t *data, size_t size,
|
---|
| 307 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
| 308 | {
|
---|
| 309 | return send_batch(fun, target, USB_DIRECTION_OUT, (uint8_t*)data, size,
|
---|
[a837544] | 310 | setup_data, NULL, callback, arg, "WRITE");
|
---|
[45d105a] | 311 | }
|
---|
| 312 | /*----------------------------------------------------------------------------*/
|
---|
[cbd568b] | 313 | /** usbhc Interface implementation using hcd_t from libusbhost library. */
|
---|
[45d105a] | 314 | usbhc_iface_t hcd_iface = {
|
---|
| 315 | .request_address = request_address,
|
---|
| 316 | .bind_address = bind_address,
|
---|
[02fc5c4] | 317 | .get_handle = find_by_address,
|
---|
[45d105a] | 318 | .release_address = release_address,
|
---|
| 319 |
|
---|
| 320 | .register_endpoint = register_endpoint,
|
---|
| 321 | .unregister_endpoint = unregister_endpoint,
|
---|
| 322 |
|
---|
[3822f7c9] | 323 | .read = usb_read,
|
---|
| 324 | .write = usb_write,
|
---|
[45d105a] | 325 | };
|
---|
| 326 | /**
|
---|
| 327 | * @}
|
---|
| 328 | */
|
---|