[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 |
|
---|
| 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 | }
|
---|
[70fb822] | 73 | if (!hcd->schedule) {
|
---|
| 74 | usb_log_error("HCD does not implement scheduler.\n");
|
---|
| 75 | return ENOTSUP;
|
---|
[45d105a] | 76 | }
|
---|
| 77 |
|
---|
[70fb822] | 78 | /* No private data and no private data dtor */
|
---|
| 79 | usb_transfer_batch_t *batch =
|
---|
[a00ac07] | 80 | usb_transfer_batch_get(ep, data, size, setup_data,
|
---|
[4e9ecf4] | 81 | in, out, arg, fun, NULL, NULL);
|
---|
[70fb822] | 82 | if (!batch) {
|
---|
| 83 | return ENOMEM;
|
---|
[45d105a] | 84 | }
|
---|
[70fb822] | 85 |
|
---|
| 86 | ret = hcd->schedule(hcd, batch);
|
---|
[45d105a] | 87 | if (ret != EOK)
|
---|
| 88 | usb_transfer_batch_dispose(batch);
|
---|
| 89 |
|
---|
| 90 | return ret;
|
---|
| 91 | }
|
---|
| 92 | /*----------------------------------------------------------------------------*/
|
---|
| 93 | /** Request address interface function
|
---|
| 94 | *
|
---|
| 95 | * @param[in] fun DDF function that was called.
|
---|
| 96 | * @param[in] speed Speed to associate with the new default address.
|
---|
| 97 | * @param[out] address Place to write a new address.
|
---|
| 98 | * @return Error code.
|
---|
| 99 | */
|
---|
| 100 | static int request_address(
|
---|
| 101 | ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
|
---|
| 102 | {
|
---|
| 103 | assert(fun);
|
---|
| 104 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 105 | assert(hcd);
|
---|
| 106 | assert(address);
|
---|
| 107 |
|
---|
[df8f3fa] | 108 | usb_log_debug("Address request speed: %s.\n", usb_str_speed(speed));
|
---|
[8b54fe6] | 109 | *address =
|
---|
| 110 | usb_device_manager_get_free_address(&hcd->dev_manager, speed);
|
---|
[45d105a] | 111 | usb_log_debug("Address request with result: %d.\n", *address);
|
---|
| 112 | if (*address <= 0)
|
---|
| 113 | return *address;
|
---|
| 114 | return EOK;
|
---|
| 115 | }
|
---|
| 116 | /*----------------------------------------------------------------------------*/
|
---|
| 117 | /** Bind address interface function
|
---|
| 118 | *
|
---|
| 119 | * @param[in] fun DDF function that was called.
|
---|
| 120 | * @param[in] address Address of the device
|
---|
| 121 | * @param[in] handle Devman handle of the device driver.
|
---|
| 122 | * @return Error code.
|
---|
| 123 | */
|
---|
| 124 | static int bind_address(
|
---|
| 125 | ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
|
---|
| 126 | {
|
---|
| 127 | assert(fun);
|
---|
| 128 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 129 | assert(hcd);
|
---|
| 130 |
|
---|
| 131 | usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
|
---|
[8b54fe6] | 132 | usb_device_manager_bind(&hcd->dev_manager, address, handle);
|
---|
[45d105a] | 133 | return EOK;
|
---|
| 134 | }
|
---|
| 135 | /*----------------------------------------------------------------------------*/
|
---|
| 136 | /** Find device handle by address interface function.
|
---|
| 137 | *
|
---|
| 138 | * @param[in] fun DDF function that was called.
|
---|
| 139 | * @param[in] address Address in question.
|
---|
| 140 | * @param[out] handle Where to store device handle if found.
|
---|
| 141 | * @return Error code.
|
---|
| 142 | */
|
---|
| 143 | static int find_by_address(ddf_fun_t *fun, usb_address_t address,
|
---|
| 144 | devman_handle_t *handle)
|
---|
| 145 | {
|
---|
| 146 | assert(fun);
|
---|
| 147 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 148 | assert(hcd);
|
---|
| 149 | const bool found =
|
---|
[8b54fe6] | 150 | usb_device_manager_find_by_address(&hcd->dev_manager, address, handle);
|
---|
[45d105a] | 151 | return found ? EOK : ENOENT;
|
---|
| 152 | }
|
---|
| 153 | /*----------------------------------------------------------------------------*/
|
---|
| 154 | /** Release address interface function
|
---|
| 155 | *
|
---|
| 156 | * @param[in] fun DDF function that was called.
|
---|
| 157 | * @param[in] address USB address to be released.
|
---|
| 158 | * @return Error code.
|
---|
| 159 | */
|
---|
| 160 | static int release_address(ddf_fun_t *fun, usb_address_t address)
|
---|
| 161 | {
|
---|
| 162 | assert(fun);
|
---|
| 163 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 164 | assert(hcd);
|
---|
| 165 | usb_log_debug("Address release %d.\n", address);
|
---|
[8b54fe6] | 166 | usb_device_manager_release(&hcd->dev_manager, address);
|
---|
[45d105a] | 167 | return EOK;
|
---|
| 168 | }
|
---|
| 169 | /*----------------------------------------------------------------------------*/
|
---|
| 170 | static int register_endpoint(
|
---|
| 171 | ddf_fun_t *fun, usb_address_t address, usb_speed_t ep_speed,
|
---|
| 172 | usb_endpoint_t endpoint,
|
---|
| 173 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
---|
| 174 | size_t max_packet_size, unsigned int interval)
|
---|
| 175 | {
|
---|
| 176 | assert(fun);
|
---|
| 177 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 178 | assert(hcd);
|
---|
| 179 | const size_t size = max_packet_size;
|
---|
[0815000] | 180 | /* Default address is not bound or registered,
|
---|
| 181 | * thus it does not provide speed info. */
|
---|
| 182 | const usb_speed_t speed = (address == 0) ? ep_speed :
|
---|
[8b54fe6] | 183 | usb_device_manager_get_speed(&hcd->dev_manager, address);
|
---|
[0815000] | 184 |
|
---|
[45d105a] | 185 | usb_log_debug("Register endpoint %d:%d %s-%s %s %zuB %ums.\n",
|
---|
| 186 | address, endpoint, usb_str_transfer_type(transfer_type),
|
---|
| 187 | usb_str_direction(direction), usb_str_speed(speed),
|
---|
| 188 | max_packet_size, interval);
|
---|
| 189 |
|
---|
[1a02517] | 190 | endpoint_t *ep = endpoint_get(
|
---|
| 191 | address, endpoint, direction, transfer_type, speed, max_packet_size);
|
---|
| 192 | if (!ep)
|
---|
| 193 | return ENOMEM;
|
---|
| 194 | int ret = EOK;
|
---|
[23b0fe8] | 195 |
|
---|
[1a02517] | 196 | if (hcd->ep_add_hook) {
|
---|
[23b0fe8] | 197 | ret = hcd->ep_add_hook(hcd, ep);
|
---|
[1a02517] | 198 | }
|
---|
| 199 | if (ret != EOK) {
|
---|
| 200 | endpoint_destroy(ep);
|
---|
| 201 | return ret;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | ret = usb_endpoint_manager_register_ep(&hcd->ep_manager, ep, size);
|
---|
| 205 | if (ret != EOK) {
|
---|
| 206 | endpoint_destroy(ep);
|
---|
| 207 | }
|
---|
| 208 | return ret;
|
---|
[45d105a] | 209 | }
|
---|
| 210 | /*----------------------------------------------------------------------------*/
|
---|
| 211 | static int unregister_endpoint(
|
---|
| 212 | ddf_fun_t *fun, usb_address_t address,
|
---|
| 213 | usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
| 214 | {
|
---|
| 215 | assert(fun);
|
---|
| 216 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
| 217 | assert(hcd);
|
---|
| 218 | usb_log_debug("Unregister endpoint %d:%d %s.\n",
|
---|
| 219 | address, endpoint, usb_str_direction(direction));
|
---|
| 220 | return usb_endpoint_manager_unregister_ep(&hcd->ep_manager, address,
|
---|
| 221 | endpoint, direction);
|
---|
| 222 | }
|
---|
| 223 | /*----------------------------------------------------------------------------*/
|
---|
[3822f7c9] | 224 | static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
|
---|
| 225 | uint8_t *data, size_t size, usbhc_iface_transfer_in_callback_t callback,
|
---|
| 226 | void *arg)
|
---|
| 227 | {
|
---|
| 228 | return send_batch(fun, target, USB_DIRECTION_IN, data, size,
|
---|
[a837544] | 229 | setup_data, callback, NULL, arg, "READ");
|
---|
[3822f7c9] | 230 | }
|
---|
| 231 | /*----------------------------------------------------------------------------*/
|
---|
| 232 | static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
|
---|
| 233 | const uint8_t *data, size_t size,
|
---|
| 234 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
| 235 | {
|
---|
| 236 | return send_batch(fun, target, USB_DIRECTION_OUT, (uint8_t*)data, size,
|
---|
[a837544] | 237 | setup_data, NULL, callback, arg, "WRITE");
|
---|
[45d105a] | 238 | }
|
---|
| 239 | /*----------------------------------------------------------------------------*/
|
---|
| 240 | usbhc_iface_t hcd_iface = {
|
---|
| 241 | .request_address = request_address,
|
---|
| 242 | .bind_address = bind_address,
|
---|
| 243 | .find_by_address = find_by_address,
|
---|
| 244 | .release_address = release_address,
|
---|
| 245 |
|
---|
| 246 | .register_endpoint = register_endpoint,
|
---|
| 247 | .unregister_endpoint = unregister_endpoint,
|
---|
| 248 |
|
---|
[3822f7c9] | 249 | .read = usb_read,
|
---|
| 250 | .write = usb_write,
|
---|
[45d105a] | 251 | };
|
---|
| 252 | /**
|
---|
| 253 | * @}
|
---|
| 254 | */
|
---|