[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 |
|
---|
| 41 | static inline int send_batch(
|
---|
| 42 | ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
|
---|
| 43 | void *data, size_t size, void * setup_data, size_t setup_size,
|
---|
| 44 | usbhc_iface_transfer_in_callback_t in,
|
---|
| 45 | usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
|
---|
| 46 | {
|
---|
| 47 | assert(fun);
|
---|
| 48 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 49 | assert(hcd);
|
---|
| 50 |
|
---|
| 51 | int ret;
|
---|
| 52 |
|
---|
| 53 | size_t res_bw;
|
---|
| 54 | endpoint_t *ep = usb_endpoint_manager_get_ep(&hcd->ep_manager,
|
---|
| 55 | target.address, target.endpoint, direction, &res_bw);
|
---|
| 56 | if (ep == NULL) {
|
---|
| 57 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
| 58 | target.address, target.endpoint, name);
|
---|
| 59 | return ENOENT;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
---|
| 63 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
| 64 |
|
---|
| 65 | const size_t bw = bandwidth_count_usb11(
|
---|
| 66 | ep->speed, ep->transfer_type, size, ep->max_packet_size);
|
---|
| 67 | if (res_bw < bw) {
|
---|
| 68 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
| 69 | "but only %zu is reserved.\n",
|
---|
| 70 | target.address, target.endpoint, name, bw, res_bw);
|
---|
| 71 | return ENOSPC;
|
---|
| 72 | }
|
---|
| 73 | usb_transfer_batch_t *batch = malloc(sizeof(usb_transfer_batch_t));
|
---|
| 74 | if (!batch) {
|
---|
| 75 | ret = ENOMEM;
|
---|
| 76 | goto out;
|
---|
| 77 |
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | usb_transfer_batch_init(batch, ep, data, NULL, size, setup_data,
|
---|
| 81 | setup_size, in, out, arg, fun, NULL, hcd->batch_private_dtor);
|
---|
| 82 | if (hcd->batch_private_ctor) {
|
---|
| 83 | batch->private_data = hcd->batch_private_ctor(batch);
|
---|
| 84 | if (!batch->private_data) {
|
---|
| 85 | ret = ENOMEM;
|
---|
| 86 | goto out;
|
---|
| 87 | }
|
---|
| 88 | } else {
|
---|
| 89 | usb_log_warning("Missing batch_private_data constructor!\n");
|
---|
| 90 | }
|
---|
| 91 | if (hcd->schedule) {
|
---|
| 92 | ret = hcd->schedule(hcd, batch);
|
---|
| 93 | } else {
|
---|
| 94 | usb_log_error("HCD does not implement scheduler.\n");
|
---|
| 95 | ret = ENOTSUP;
|
---|
| 96 | }
|
---|
| 97 | out:
|
---|
| 98 | if (ret != EOK)
|
---|
| 99 | usb_transfer_batch_dispose(batch);
|
---|
| 100 |
|
---|
| 101 | return ret;
|
---|
| 102 | }
|
---|
| 103 | /*----------------------------------------------------------------------------*/
|
---|
| 104 | /** Request address interface function
|
---|
| 105 | *
|
---|
| 106 | * @param[in] fun DDF function that was called.
|
---|
| 107 | * @param[in] speed Speed to associate with the new default address.
|
---|
| 108 | * @param[out] address Place to write a new address.
|
---|
| 109 | * @return Error code.
|
---|
| 110 | */
|
---|
| 111 | static int request_address(
|
---|
| 112 | ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
|
---|
| 113 | {
|
---|
| 114 | assert(fun);
|
---|
| 115 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 116 | assert(hcd);
|
---|
| 117 | assert(address);
|
---|
| 118 |
|
---|
| 119 | usb_log_debug("Address request with speed %d.\n", speed);
|
---|
| 120 | *address = device_keeper_get_free_address(&hcd->dev_manager, speed);
|
---|
| 121 | usb_log_debug("Address request with result: %d.\n", *address);
|
---|
| 122 | if (*address <= 0)
|
---|
| 123 | return *address;
|
---|
| 124 | return EOK;
|
---|
| 125 | }
|
---|
| 126 | /*----------------------------------------------------------------------------*/
|
---|
| 127 | /** Bind address interface function
|
---|
| 128 | *
|
---|
| 129 | * @param[in] fun DDF function that was called.
|
---|
| 130 | * @param[in] address Address of the device
|
---|
| 131 | * @param[in] handle Devman handle of the device driver.
|
---|
| 132 | * @return Error code.
|
---|
| 133 | */
|
---|
| 134 | static int bind_address(
|
---|
| 135 | ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
|
---|
| 136 | {
|
---|
| 137 | assert(fun);
|
---|
| 138 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 139 | assert(hcd);
|
---|
| 140 |
|
---|
| 141 | usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
|
---|
| 142 | usb_device_keeper_bind(&hcd->dev_manager, address, handle);
|
---|
| 143 | return EOK;
|
---|
| 144 | }
|
---|
| 145 | /*----------------------------------------------------------------------------*/
|
---|
| 146 | /** Find device handle by address interface function.
|
---|
| 147 | *
|
---|
| 148 | * @param[in] fun DDF function that was called.
|
---|
| 149 | * @param[in] address Address in question.
|
---|
| 150 | * @param[out] handle Where to store device handle if found.
|
---|
| 151 | * @return Error code.
|
---|
| 152 | */
|
---|
| 153 | static int find_by_address(ddf_fun_t *fun, usb_address_t address,
|
---|
| 154 | devman_handle_t *handle)
|
---|
| 155 | {
|
---|
| 156 | assert(fun);
|
---|
| 157 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 158 | assert(hcd);
|
---|
| 159 | const bool found =
|
---|
| 160 | usb_device_keeper_find_by_address(&hcd->dev_manager, address, handle);
|
---|
| 161 | return found ? EOK : ENOENT;
|
---|
| 162 | }
|
---|
| 163 | /*----------------------------------------------------------------------------*/
|
---|
| 164 | /** Release address interface function
|
---|
| 165 | *
|
---|
| 166 | * @param[in] fun DDF function that was called.
|
---|
| 167 | * @param[in] address USB address to be released.
|
---|
| 168 | * @return Error code.
|
---|
| 169 | */
|
---|
| 170 | static int release_address(ddf_fun_t *fun, usb_address_t address)
|
---|
| 171 | {
|
---|
| 172 | assert(fun);
|
---|
| 173 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 174 | assert(hcd);
|
---|
| 175 | usb_log_debug("Address release %d.\n", address);
|
---|
| 176 | usb_device_keeper_release(&hcd->dev_manager, address);
|
---|
| 177 | return EOK;
|
---|
| 178 | }
|
---|
| 179 | /*----------------------------------------------------------------------------*/
|
---|
| 180 | static int register_endpoint(
|
---|
| 181 | ddf_fun_t *fun, usb_address_t address, usb_speed_t ep_speed,
|
---|
| 182 | usb_endpoint_t endpoint,
|
---|
| 183 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
---|
| 184 | size_t max_packet_size, unsigned int interval)
|
---|
| 185 | {
|
---|
| 186 | assert(fun);
|
---|
| 187 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 188 | assert(hcd);
|
---|
| 189 | const size_t size = max_packet_size;
|
---|
| 190 | usb_speed_t speed =
|
---|
| 191 | usb_device_keeper_get_speed(&hcd->dev_manager, address);
|
---|
| 192 | if (speed >= USB_SPEED_MAX) {
|
---|
| 193 | speed = ep_speed;
|
---|
| 194 | }
|
---|
| 195 | usb_log_debug("Register endpoint %d:%d %s-%s %s %zuB %ums.\n",
|
---|
| 196 | address, endpoint, usb_str_transfer_type(transfer_type),
|
---|
| 197 | usb_str_direction(direction), usb_str_speed(speed),
|
---|
| 198 | max_packet_size, interval);
|
---|
| 199 |
|
---|
| 200 | return usb_endpoint_manager_add_ep(&hcd->ep_manager, address, endpoint,
|
---|
| 201 | direction, transfer_type, speed, max_packet_size, size);
|
---|
| 202 | }
|
---|
| 203 | /*----------------------------------------------------------------------------*/
|
---|
| 204 | static int unregister_endpoint(
|
---|
| 205 | ddf_fun_t *fun, usb_address_t address,
|
---|
| 206 | usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
| 207 | {
|
---|
| 208 | assert(fun);
|
---|
| 209 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 210 | assert(hcd);
|
---|
| 211 | usb_log_debug("Unregister endpoint %d:%d %s.\n",
|
---|
| 212 | address, endpoint, usb_str_direction(direction));
|
---|
| 213 | return usb_endpoint_manager_unregister_ep(&hcd->ep_manager, address,
|
---|
| 214 | endpoint, direction);
|
---|
| 215 | }
|
---|
| 216 | /*----------------------------------------------------------------------------*/
|
---|
| 217 | /** Interrupt out transaction interface function
|
---|
| 218 | *
|
---|
| 219 | * @param[in] fun DDF function that was called.
|
---|
| 220 | * @param[in] target USB device to write to.
|
---|
| 221 | * @param[in] data Source of data.
|
---|
| 222 | * @param[in] size Size of data source.
|
---|
| 223 | * @param[in] callback Function to call on transaction completion
|
---|
| 224 | * @param[in] arg Additional for callback function.
|
---|
| 225 | * @return Error code.
|
---|
| 226 | */
|
---|
| 227 | static int interrupt_out(
|
---|
| 228 | ddf_fun_t *fun, usb_target_t target, void *data,
|
---|
| 229 | size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
| 230 | {
|
---|
| 231 | return send_batch(fun, target, USB_DIRECTION_OUT, data, size,
|
---|
| 232 | NULL, 0, NULL, callback, arg, "Interrupt OUT");
|
---|
| 233 | }
|
---|
| 234 | /*----------------------------------------------------------------------------*/
|
---|
| 235 | /** Interrupt in transaction interface function
|
---|
| 236 | *
|
---|
| 237 | * @param[in] fun DDF function that was called.
|
---|
| 238 | * @param[in] target USB device to write to.
|
---|
| 239 | * @param[out] data Data destination.
|
---|
| 240 | * @param[in] size Size of data source.
|
---|
| 241 | * @param[in] callback Function to call on transaction completion
|
---|
| 242 | * @param[in] arg Additional for callback function.
|
---|
| 243 | * @return Error code.
|
---|
| 244 | */
|
---|
| 245 | static int interrupt_in(
|
---|
| 246 | ddf_fun_t *fun, usb_target_t target, void *data,
|
---|
| 247 | size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
| 248 | {
|
---|
| 249 | return send_batch(fun, target, USB_DIRECTION_IN, data, size,
|
---|
| 250 | NULL, 0, callback, NULL, arg, "Interrupt IN");
|
---|
| 251 | }
|
---|
| 252 | /*----------------------------------------------------------------------------*/
|
---|
| 253 | /** Bulk out transaction interface function
|
---|
| 254 | *
|
---|
| 255 | * @param[in] fun DDF function that was called.
|
---|
| 256 | * @param[in] target USB device to write to.
|
---|
| 257 | * @param[in] data Source of data.
|
---|
| 258 | * @param[in] size Size of data source.
|
---|
| 259 | * @param[in] callback Function to call on transaction completion
|
---|
| 260 | * @param[in] arg Additional for callback function.
|
---|
| 261 | * @return Error code.
|
---|
| 262 | */
|
---|
| 263 | static int bulk_out(
|
---|
| 264 | ddf_fun_t *fun, usb_target_t target, void *data,
|
---|
| 265 | size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
| 266 | {
|
---|
| 267 | return send_batch(fun, target, USB_DIRECTION_OUT, data, size,
|
---|
| 268 | NULL, 0, NULL, callback, arg, "Bulk OUT");
|
---|
| 269 | }
|
---|
| 270 | /*----------------------------------------------------------------------------*/
|
---|
| 271 | /** Bulk in transaction interface function
|
---|
| 272 | *
|
---|
| 273 | * @param[in] fun DDF function that was called.
|
---|
| 274 | * @param[in] target USB device to write to.
|
---|
| 275 | * @param[out] data Data destination.
|
---|
| 276 | * @param[in] size Size of data source.
|
---|
| 277 | * @param[in] callback Function to call on transaction completion
|
---|
| 278 | * @param[in] arg Additional for callback function.
|
---|
| 279 | * @return Error code.
|
---|
| 280 | */
|
---|
| 281 | static int bulk_in(
|
---|
| 282 | ddf_fun_t *fun, usb_target_t target, void *data,
|
---|
| 283 | size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
| 284 | {
|
---|
| 285 | return send_batch(fun, target, USB_DIRECTION_IN, data, size,
|
---|
| 286 | NULL, 0, callback, NULL, arg, "Bulk IN");
|
---|
| 287 | }
|
---|
| 288 | /*----------------------------------------------------------------------------*/
|
---|
| 289 | /** Control write transaction interface function
|
---|
| 290 | *
|
---|
| 291 | * @param[in] fun DDF function that was called.
|
---|
| 292 | * @param[in] target USB device to write to.
|
---|
| 293 | * @param[in] setup_data Data to send with SETUP transfer.
|
---|
| 294 | * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
|
---|
| 295 | * @param[in] data Source of data.
|
---|
| 296 | * @param[in] size Size of data source.
|
---|
| 297 | * @param[in] callback Function to call on transaction completion.
|
---|
| 298 | * @param[in] arg Additional for callback function.
|
---|
| 299 | * @return Error code.
|
---|
| 300 | */
|
---|
| 301 | static int control_write(
|
---|
| 302 | ddf_fun_t *fun, usb_target_t target,
|
---|
| 303 | void *setup_data, size_t setup_size, void *data, size_t size,
|
---|
| 304 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
| 305 | {
|
---|
| 306 | return send_batch(fun, target, USB_DIRECTION_BOTH, data, size,
|
---|
| 307 | setup_data, setup_size, NULL, callback, arg, "Control WRITE");
|
---|
| 308 | // usb_endpoint_manager_reset_if_need(&hc->ep_manager, target, setup_data);
|
---|
| 309 | }
|
---|
| 310 | /*----------------------------------------------------------------------------*/
|
---|
| 311 | /** Control read transaction interface function
|
---|
| 312 | *
|
---|
| 313 | * @param[in] fun DDF function that was called.
|
---|
| 314 | * @param[in] target USB device to write to.
|
---|
| 315 | * @param[in] setup_data Data to send with SETUP packet.
|
---|
| 316 | * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
|
---|
| 317 | * @param[out] data Source of data.
|
---|
| 318 | * @param[in] size Size of data source.
|
---|
| 319 | * @param[in] callback Function to call on transaction completion.
|
---|
| 320 | * @param[in] arg Additional for callback function.
|
---|
| 321 | * @return Error code.
|
---|
| 322 | */
|
---|
| 323 | static int control_read(
|
---|
| 324 | ddf_fun_t *fun, usb_target_t target,
|
---|
| 325 | void *setup_data, size_t setup_size, void *data, size_t size,
|
---|
| 326 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
| 327 | {
|
---|
| 328 | return send_batch(fun, target, USB_DIRECTION_BOTH, data, size,
|
---|
| 329 | setup_data, setup_size, callback, NULL, arg, "Control READ");
|
---|
| 330 | }
|
---|
| 331 | /*----------------------------------------------------------------------------*/
|
---|
| 332 | usbhc_iface_t hcd_iface = {
|
---|
| 333 | .request_address = request_address,
|
---|
| 334 | .bind_address = bind_address,
|
---|
| 335 | .find_by_address = find_by_address,
|
---|
| 336 | .release_address = release_address,
|
---|
| 337 |
|
---|
| 338 | .register_endpoint = register_endpoint,
|
---|
| 339 | .unregister_endpoint = unregister_endpoint,
|
---|
| 340 |
|
---|
| 341 | .interrupt_out = interrupt_out,
|
---|
| 342 | .interrupt_in = interrupt_in,
|
---|
| 343 |
|
---|
| 344 | .bulk_out = bulk_out,
|
---|
| 345 | .bulk_in = bulk_in,
|
---|
| 346 |
|
---|
| 347 | .control_write = control_write,
|
---|
| 348 | .control_read = control_read,
|
---|
| 349 | };
|
---|
| 350 | /**
|
---|
| 351 | * @}
|
---|
| 352 | */
|
---|