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