[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,
|
---|
[a837544] | 43 | void *data, size_t size, uint64_t setup_data,
|
---|
[45d105a] | 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 |
|
---|
[48ae3ef] | 51 | endpoint_t *ep = usb_endpoint_manager_find_ep(&hcd->ep_manager,
|
---|
[83c3123] | 52 | target.address, target.endpoint, direction);
|
---|
[45d105a] | 53 | if (ep == NULL) {
|
---|
| 54 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
| 55 | target.address, target.endpoint, name);
|
---|
| 56 | return ENOENT;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
---|
| 60 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
| 61 |
|
---|
| 62 | const size_t bw = bandwidth_count_usb11(
|
---|
| 63 | ep->speed, ep->transfer_type, size, ep->max_packet_size);
|
---|
[83c3123] | 64 | /* Check if we have enough bandwidth reserved */
|
---|
| 65 | if (ep->bandwidth < bw) {
|
---|
[45d105a] | 66 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
| 67 | "but only %zu is reserved.\n",
|
---|
[83c3123] | 68 | ep->address, ep->endpoint, name, bw, ep->bandwidth);
|
---|
[45d105a] | 69 | return ENOSPC;
|
---|
| 70 | }
|
---|
[70fb822] | 71 | if (!hcd->schedule) {
|
---|
| 72 | usb_log_error("HCD does not implement scheduler.\n");
|
---|
| 73 | return ENOTSUP;
|
---|
[45d105a] | 74 | }
|
---|
| 75 |
|
---|
[70fb822] | 76 | /* No private data and no private data dtor */
|
---|
| 77 | usb_transfer_batch_t *batch =
|
---|
[549ff23] | 78 | usb_transfer_batch_create(ep, data, size, setup_data,
|
---|
[4e9ecf4] | 79 | in, out, arg, fun, NULL, NULL);
|
---|
[70fb822] | 80 | if (!batch) {
|
---|
| 81 | return ENOMEM;
|
---|
[45d105a] | 82 | }
|
---|
[70fb822] | 83 |
|
---|
[83c3123] | 84 | const int ret = hcd->schedule(hcd, batch);
|
---|
[45d105a] | 85 | if (ret != EOK)
|
---|
[549ff23] | 86 | usb_transfer_batch_destroy(batch);
|
---|
[45d105a] | 87 |
|
---|
| 88 | return ret;
|
---|
| 89 | }
|
---|
| 90 | /*----------------------------------------------------------------------------*/
|
---|
| 91 | /** Request address interface function
|
---|
| 92 | *
|
---|
| 93 | * @param[in] fun DDF function that was called.
|
---|
| 94 | * @param[in] speed Speed to associate with the new default address.
|
---|
| 95 | * @param[out] address Place to write a new address.
|
---|
| 96 | * @return Error code.
|
---|
| 97 | */
|
---|
| 98 | static int request_address(
|
---|
[67f55e7b] | 99 | ddf_fun_t *fun, usb_address_t *address, bool strict, usb_speed_t speed)
|
---|
[45d105a] | 100 | {
|
---|
| 101 | assert(fun);
|
---|
| 102 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 103 | assert(hcd);
|
---|
| 104 | assert(address);
|
---|
| 105 |
|
---|
[f37eb84] | 106 | usb_log_debug("Address request: speed: %s, address: %d, strict: %s.\n",
|
---|
| 107 | usb_str_speed(speed), *address, strict ? "YES" : "NO");
|
---|
[0cd8089] | 108 | return usb_device_manager_request_address(
|
---|
[67f55e7b] | 109 | &hcd->dev_manager, address, strict, speed);
|
---|
[45d105a] | 110 | }
|
---|
| 111 | /*----------------------------------------------------------------------------*/
|
---|
| 112 | /** Bind address interface function
|
---|
| 113 | *
|
---|
| 114 | * @param[in] fun DDF function that was called.
|
---|
| 115 | * @param[in] address Address of the device
|
---|
| 116 | * @param[in] handle Devman handle of the device driver.
|
---|
| 117 | * @return Error code.
|
---|
| 118 | */
|
---|
| 119 | static int bind_address(
|
---|
| 120 | ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
|
---|
| 121 | {
|
---|
| 122 | assert(fun);
|
---|
| 123 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 124 | assert(hcd);
|
---|
| 125 |
|
---|
| 126 | usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
|
---|
[0cd8089] | 127 | return usb_device_manager_bind_address(
|
---|
| 128 | &hcd->dev_manager, address, handle);
|
---|
[45d105a] | 129 | }
|
---|
| 130 | /*----------------------------------------------------------------------------*/
|
---|
| 131 | /** Find device handle by address interface function.
|
---|
| 132 | *
|
---|
| 133 | * @param[in] fun DDF function that was called.
|
---|
| 134 | * @param[in] address Address in question.
|
---|
| 135 | * @param[out] handle Where to store device handle if found.
|
---|
| 136 | * @return Error code.
|
---|
| 137 | */
|
---|
| 138 | static int find_by_address(ddf_fun_t *fun, usb_address_t address,
|
---|
| 139 | devman_handle_t *handle)
|
---|
| 140 | {
|
---|
| 141 | assert(fun);
|
---|
| 142 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 143 | assert(hcd);
|
---|
[4267908] | 144 | return usb_device_manager_get_info_by_address(
|
---|
| 145 | &hcd->dev_manager, address, handle, NULL);
|
---|
[45d105a] | 146 | }
|
---|
| 147 | /*----------------------------------------------------------------------------*/
|
---|
| 148 | /** Release address interface function
|
---|
| 149 | *
|
---|
| 150 | * @param[in] fun DDF function that was called.
|
---|
| 151 | * @param[in] address USB address to be released.
|
---|
| 152 | * @return Error code.
|
---|
| 153 | */
|
---|
| 154 | static int release_address(ddf_fun_t *fun, usb_address_t address)
|
---|
| 155 | {
|
---|
| 156 | assert(fun);
|
---|
| 157 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 158 | assert(hcd);
|
---|
| 159 | usb_log_debug("Address release %d.\n", address);
|
---|
[0cd8089] | 160 | usb_device_manager_release_address(&hcd->dev_manager, address);
|
---|
[45d105a] | 161 | return EOK;
|
---|
| 162 | }
|
---|
| 163 | /*----------------------------------------------------------------------------*/
|
---|
[48ae3ef] | 164 | static int register_helper(endpoint_t *ep, void *arg)
|
---|
| 165 | {
|
---|
| 166 | hcd_t *hcd = arg;
|
---|
| 167 | assert(ep);
|
---|
| 168 | assert(hcd);
|
---|
| 169 | if (hcd->ep_add_hook)
|
---|
| 170 | return hcd->ep_add_hook(hcd, ep);
|
---|
| 171 | return EOK;
|
---|
| 172 | }
|
---|
| 173 | /*----------------------------------------------------------------------------*/
|
---|
| 174 | static void unregister_helper(endpoint_t *ep, void *arg)
|
---|
| 175 | {
|
---|
| 176 | hcd_t *hcd = arg;
|
---|
| 177 | assert(ep);
|
---|
| 178 | assert(hcd);
|
---|
| 179 | if (hcd->ep_remove_hook)
|
---|
| 180 | hcd->ep_remove_hook(hcd, ep);
|
---|
| 181 | }
|
---|
| 182 | /*----------------------------------------------------------------------------*/
|
---|
[45d105a] | 183 | static int register_endpoint(
|
---|
[27736cf] | 184 | ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
|
---|
[45d105a] | 185 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
---|
| 186 | size_t max_packet_size, unsigned int interval)
|
---|
| 187 | {
|
---|
| 188 | assert(fun);
|
---|
| 189 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 190 | assert(hcd);
|
---|
| 191 | const size_t size = max_packet_size;
|
---|
[1b17e37] | 192 | usb_speed_t speed = USB_SPEED_MAX;
|
---|
| 193 | const int ret = usb_device_manager_get_info_by_address(
|
---|
[4267908] | 194 | &hcd->dev_manager, address, NULL, &speed);
|
---|
[1b17e37] | 195 | if (ret != EOK) {
|
---|
| 196 | return ret;
|
---|
| 197 | }
|
---|
[0815000] | 198 |
|
---|
[45d105a] | 199 | usb_log_debug("Register endpoint %d:%d %s-%s %s %zuB %ums.\n",
|
---|
| 200 | address, endpoint, usb_str_transfer_type(transfer_type),
|
---|
| 201 | usb_str_direction(direction), usb_str_speed(speed),
|
---|
| 202 | max_packet_size, interval);
|
---|
| 203 |
|
---|
[48ae3ef] | 204 | return usb_endpoint_manager_add_ep(&hcd->ep_manager, address, endpoint,
|
---|
| 205 | direction, transfer_type, speed, max_packet_size, size,
|
---|
| 206 | register_helper, hcd);
|
---|
[45d105a] | 207 | }
|
---|
| 208 | /*----------------------------------------------------------------------------*/
|
---|
| 209 | static int unregister_endpoint(
|
---|
| 210 | ddf_fun_t *fun, usb_address_t address,
|
---|
| 211 | usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
| 212 | {
|
---|
| 213 | assert(fun);
|
---|
| 214 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 215 | assert(hcd);
|
---|
| 216 | usb_log_debug("Unregister endpoint %d:%d %s.\n",
|
---|
| 217 | address, endpoint, usb_str_direction(direction));
|
---|
[48ae3ef] | 218 | return usb_endpoint_manager_remove_ep(&hcd->ep_manager, address,
|
---|
| 219 | endpoint, direction, unregister_helper, hcd);
|
---|
[45d105a] | 220 | }
|
---|
| 221 | /*----------------------------------------------------------------------------*/
|
---|
[3822f7c9] | 222 | static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
|
---|
| 223 | uint8_t *data, size_t size, usbhc_iface_transfer_in_callback_t callback,
|
---|
| 224 | void *arg)
|
---|
| 225 | {
|
---|
| 226 | return send_batch(fun, target, USB_DIRECTION_IN, data, size,
|
---|
[a837544] | 227 | setup_data, callback, NULL, arg, "READ");
|
---|
[3822f7c9] | 228 | }
|
---|
| 229 | /*----------------------------------------------------------------------------*/
|
---|
| 230 | static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
|
---|
| 231 | const uint8_t *data, size_t size,
|
---|
| 232 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
| 233 | {
|
---|
| 234 | return send_batch(fun, target, USB_DIRECTION_OUT, (uint8_t*)data, size,
|
---|
[a837544] | 235 | setup_data, NULL, callback, arg, "WRITE");
|
---|
[45d105a] | 236 | }
|
---|
| 237 | /*----------------------------------------------------------------------------*/
|
---|
| 238 | usbhc_iface_t hcd_iface = {
|
---|
| 239 | .request_address = request_address,
|
---|
| 240 | .bind_address = bind_address,
|
---|
| 241 | .find_by_address = find_by_address,
|
---|
| 242 | .release_address = release_address,
|
---|
| 243 |
|
---|
| 244 | .register_endpoint = register_endpoint,
|
---|
| 245 | .unregister_endpoint = unregister_endpoint,
|
---|
| 246 |
|
---|
[3822f7c9] | 247 | .read = usb_read,
|
---|
| 248 | .write = usb_write,
|
---|
[45d105a] | 249 | };
|
---|
| 250 | /**
|
---|
| 251 | * @}
|
---|
| 252 | */
|
---|