| 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 |
|
|---|
| 29 | /** @addtogroup libusbhost
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | *
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <str_error.h>
|
|---|
| 38 | #include <usb_iface.h>
|
|---|
| 39 | #include <usb/debug.h>
|
|---|
| 40 |
|
|---|
| 41 | #include <usb/host/hcd.h>
|
|---|
| 42 |
|
|---|
| 43 | /** Initialize hcd_t structure.
|
|---|
| 44 | * Initializes device and endpoint managers. Sets data and hook pointer to NULL.
|
|---|
| 45 | *
|
|---|
| 46 | * @param hcd hcd_t structure to initialize, non-null.
|
|---|
| 47 | * @param max_speed Maximum supported USB speed (full, high).
|
|---|
| 48 | * @param bandwidth Available bandwidth, passed to endpoint manager.
|
|---|
| 49 | * @param bw_count Bandwidth compute function, passed to endpoint manager.
|
|---|
| 50 | */
|
|---|
| 51 | void hcd_init(hcd_t *hcd, usb_speed_t max_speed, size_t bandwidth,
|
|---|
| 52 | bw_count_func_t bw_count)
|
|---|
| 53 | {
|
|---|
| 54 | assert(hcd);
|
|---|
| 55 | usb_device_manager_init(&hcd->dev_manager, max_speed);
|
|---|
| 56 | usb_endpoint_manager_init(&hcd->ep_manager, bandwidth, bw_count);
|
|---|
| 57 |
|
|---|
| 58 | hcd->private_data = NULL;
|
|---|
| 59 | hcd->schedule = NULL;
|
|---|
| 60 | hcd->ep_add_hook = NULL;
|
|---|
| 61 | hcd->ep_remove_hook = NULL;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /** Prepare generic usb_transfer_batch and schedule it.
|
|---|
| 65 | * @param hcd Host controller driver.
|
|---|
| 66 | * @param fun DDF fun
|
|---|
| 67 | * @param target address and endpoint number.
|
|---|
| 68 | * @param setup_data Data to use in setup stage (Control communication type)
|
|---|
| 69 | * @param in Callback for device to host communication.
|
|---|
| 70 | * @param out Callback for host to device communication.
|
|---|
| 71 | * @param arg Callback parameter.
|
|---|
| 72 | * @param name Communication identifier (for nicer output).
|
|---|
| 73 | * @return Error code.
|
|---|
| 74 | */
|
|---|
| 75 | int hcd_send_batch(
|
|---|
| 76 | hcd_t *hcd, ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
|
|---|
| 77 | void *data, size_t size, uint64_t setup_data,
|
|---|
| 78 | usbhc_iface_transfer_in_callback_t in,
|
|---|
| 79 | usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
|
|---|
| 80 | {
|
|---|
| 81 | assert(hcd);
|
|---|
| 82 |
|
|---|
| 83 | endpoint_t *ep = usb_endpoint_manager_find_ep(&hcd->ep_manager,
|
|---|
| 84 | target.address, target.endpoint, direction);
|
|---|
| 85 | if (ep == NULL) {
|
|---|
| 86 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
|---|
| 87 | target.address, target.endpoint, name);
|
|---|
| 88 | return ENOENT;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
|---|
| 92 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
|---|
| 93 |
|
|---|
| 94 | const size_t bw = bandwidth_count_usb11(
|
|---|
| 95 | ep->speed, ep->transfer_type, size, ep->max_packet_size);
|
|---|
| 96 | /* Check if we have enough bandwidth reserved */
|
|---|
| 97 | if (ep->bandwidth < bw) {
|
|---|
| 98 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
|---|
| 99 | "but only %zu is reserved.\n",
|
|---|
| 100 | ep->address, ep->endpoint, name, bw, ep->bandwidth);
|
|---|
| 101 | return ENOSPC;
|
|---|
| 102 | }
|
|---|
| 103 | if (!hcd->schedule) {
|
|---|
| 104 | usb_log_error("HCD does not implement scheduler.\n");
|
|---|
| 105 | return ENOTSUP;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /* No private data and no private data dtor */
|
|---|
| 109 | usb_transfer_batch_t *batch =
|
|---|
| 110 | usb_transfer_batch_create(ep, data, size, setup_data,
|
|---|
| 111 | in, out, arg, fun, NULL, NULL);
|
|---|
| 112 | if (!batch) {
|
|---|
| 113 | return ENOMEM;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | const int ret = hcd->schedule(hcd, batch);
|
|---|
| 117 | if (ret != EOK)
|
|---|
| 118 | usb_transfer_batch_destroy(batch);
|
|---|
| 119 |
|
|---|
| 120 | return ret;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /**
|
|---|
| 124 | * @}
|
|---|
| 125 | */
|
|---|