[4317827] | 1 | /*
|
---|
[c56dbe0] | 2 | * Copyright (c) 2011 Vojtech Horky, Jan Vesely
|
---|
[4317827] | 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 | */
|
---|
[17ceb72] | 28 | /** @addtogroup drvusbuhcihc
|
---|
[c56dbe0] | 29 | * @{
|
---|
| 30 | */
|
---|
| 31 | /** @file
|
---|
[17ceb72] | 32 | * @brief UHCI driver hc interface implementation
|
---|
[c56dbe0] | 33 | */
|
---|
[eb1a2f4] | 34 | #include <ddf/driver.h>
|
---|
[1c6a45f] | 35 | #include <errno.h>
|
---|
[c56dbe0] | 36 |
|
---|
[1669a73] | 37 | #include <usb/debug.h>
|
---|
[8dc762e0] | 38 | #include <usb/host/endpoint.h>
|
---|
[1669a73] | 39 |
|
---|
[3515533] | 40 | #include "iface.h"
|
---|
[c01cd32] | 41 | #include "hc.h"
|
---|
[b4875e67] | 42 |
|
---|
[a7e2f0d] | 43 | /** Reserve default address interface function
|
---|
| 44 | *
|
---|
| 45 | * @param[in] fun DDF function that was called.
|
---|
| 46 | * @param[in] speed Speed to associate with the new default address.
|
---|
| 47 | * @return Error code.
|
---|
| 48 | */
|
---|
[eb1a2f4] | 49 | static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
|
---|
[86b39f7e] | 50 | {
|
---|
[eb1a2f4] | 51 | assert(fun);
|
---|
[c01cd32] | 52 | hc_t *hc = fun_to_hc(fun);
|
---|
[86b39f7e] | 53 | assert(hc);
|
---|
[1a93bb0] | 54 | usb_log_debug("Default address request with speed %d.\n", speed);
|
---|
[62ed5bc] | 55 | usb_device_keeper_reserve_default_address(&hc->manager, speed);
|
---|
[86b39f7e] | 56 | return EOK;
|
---|
[fb8927d] | 57 | #if 0
|
---|
| 58 | endpoint_t *ep = malloc(sizeof(endpoint_t));
|
---|
| 59 | if (ep == NULL)
|
---|
| 60 | return ENOMEM;
|
---|
| 61 | const size_t max_packet_size = speed == USB_SPEED_LOW ? 8 : 64;
|
---|
| 62 | endpoint_init(ep, USB_TRANSFER_CONTROL, speed, max_packet_size);
|
---|
| 63 | int ret;
|
---|
| 64 | try_retgister:
|
---|
| 65 | ret = usb_endpoint_manager_register_ep(&hc->ep_manager,
|
---|
| 66 | USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH, ep, endpoint_destroy, 0);
|
---|
| 67 | if (ret == EEXISTS) {
|
---|
| 68 | async_usleep(1000);
|
---|
| 69 | goto try_retgister;
|
---|
| 70 | }
|
---|
| 71 | if (ret != EOK) {
|
---|
| 72 | endpoint_destroy(ep);
|
---|
| 73 | }
|
---|
| 74 | return ret;
|
---|
| 75 | #endif
|
---|
[86b39f7e] | 76 | }
|
---|
| 77 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 78 | /** Release default address interface function
|
---|
| 79 | *
|
---|
| 80 | * @param[in] fun DDF function that was called.
|
---|
| 81 | * @return Error code.
|
---|
| 82 | */
|
---|
[eb1a2f4] | 83 | static int release_default_address(ddf_fun_t *fun)
|
---|
[86b39f7e] | 84 | {
|
---|
[eb1a2f4] | 85 | assert(fun);
|
---|
[c01cd32] | 86 | hc_t *hc = fun_to_hc(fun);
|
---|
[86b39f7e] | 87 | assert(hc);
|
---|
[1a93bb0] | 88 | usb_log_debug("Default address release.\n");
|
---|
[fb8927d] | 89 | // return usb_endpoint_manager_unregister_ep(&hc->ep_manager,
|
---|
| 90 | // USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH);
|
---|
[62ed5bc] | 91 | usb_device_keeper_release_default_address(&hc->manager);
|
---|
[86b39f7e] | 92 | return EOK;
|
---|
| 93 | }
|
---|
| 94 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 95 | /** Request address interface function
|
---|
| 96 | *
|
---|
| 97 | * @param[in] fun DDF function that was called.
|
---|
| 98 | * @param[in] speed Speed to associate with the new default address.
|
---|
| 99 | * @param[out] address Place to write a new address.
|
---|
| 100 | * @return Error code.
|
---|
| 101 | */
|
---|
[b9fa0a9] | 102 | static int request_address(
|
---|
| 103 | ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
|
---|
[86b39f7e] | 104 | {
|
---|
[eb1a2f4] | 105 | assert(fun);
|
---|
[c01cd32] | 106 | hc_t *hc = fun_to_hc(fun);
|
---|
[86b39f7e] | 107 | assert(hc);
|
---|
[1a93bb0] | 108 | assert(address);
|
---|
| 109 |
|
---|
| 110 | usb_log_debug("Address request with speed %d.\n", speed);
|
---|
[62ed5bc] | 111 | *address = device_keeper_get_free_address(&hc->manager, speed);
|
---|
[1a93bb0] | 112 | usb_log_debug("Address request with result: %d.\n", *address);
|
---|
[86b39f7e] | 113 | if (*address <= 0)
|
---|
[1c6a45f] | 114 | return *address;
|
---|
[86b39f7e] | 115 | return EOK;
|
---|
| 116 | }
|
---|
| 117 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 118 | /** Bind address interface function
|
---|
| 119 | *
|
---|
| 120 | * @param[in] fun DDF function that was called.
|
---|
| 121 | * @param[in] address Address of the device
|
---|
| 122 | * @param[in] handle Devman handle of the device driver.
|
---|
| 123 | * @return Error code.
|
---|
| 124 | */
|
---|
[86b39f7e] | 125 | static int bind_address(
|
---|
[eb1a2f4] | 126 | ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
|
---|
[86b39f7e] | 127 | {
|
---|
[eb1a2f4] | 128 | assert(fun);
|
---|
[c01cd32] | 129 | hc_t *hc = fun_to_hc(fun);
|
---|
[86b39f7e] | 130 | assert(hc);
|
---|
[1a93bb0] | 131 | usb_log_debug("Address bind %d-%d.\n", address, handle);
|
---|
[62ed5bc] | 132 | usb_device_keeper_bind(&hc->manager, address, handle);
|
---|
[86b39f7e] | 133 | return EOK;
|
---|
| 134 | }
|
---|
| 135 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 136 | /** Release address interface function
|
---|
| 137 | *
|
---|
| 138 | * @param[in] fun DDF function that was called.
|
---|
| 139 | * @param[in] address USB address to be released.
|
---|
| 140 | * @return Error code.
|
---|
| 141 | */
|
---|
[eb1a2f4] | 142 | static int release_address(ddf_fun_t *fun, usb_address_t address)
|
---|
[86b39f7e] | 143 | {
|
---|
[eb1a2f4] | 144 | assert(fun);
|
---|
[c01cd32] | 145 | hc_t *hc = fun_to_hc(fun);
|
---|
[86b39f7e] | 146 | assert(hc);
|
---|
[1a93bb0] | 147 | usb_log_debug("Address release %d.\n", address);
|
---|
[62ed5bc] | 148 | usb_device_keeper_release(&hc->manager, address);
|
---|
[86b39f7e] | 149 | return EOK;
|
---|
[4317827] | 150 | }
|
---|
[3515533] | 151 | /*----------------------------------------------------------------------------*/
|
---|
[8870230] | 152 | static int register_endpoint(
|
---|
| 153 | ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
|
---|
| 154 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
---|
| 155 | size_t max_packet_size, unsigned int interval)
|
---|
| 156 | {
|
---|
[a1313b8c] | 157 | hc_t *hc = fun_to_hc(fun);
|
---|
| 158 | assert(hc);
|
---|
| 159 | const usb_speed_t speed =
|
---|
| 160 | usb_device_keeper_get_speed(&hc->manager, address);
|
---|
[8dc762e0] | 161 | const size_t size =
|
---|
| 162 | (transfer_type == USB_TRANSFER_INTERRUPT
|
---|
| 163 | || transfer_type == USB_TRANSFER_ISOCHRONOUS) ?
|
---|
| 164 | max_packet_size : 0;
|
---|
[1e70157] | 165 | int ret;
|
---|
[6ce42e85] | 166 |
|
---|
| 167 | endpoint_t *ep = malloc(sizeof(endpoint_t));
|
---|
| 168 | if (ep == NULL)
|
---|
| 169 | return ENOMEM;
|
---|
[0aae4a6a] | 170 | ret = endpoint_init(ep, address, endpoint, direction,
|
---|
| 171 | transfer_type, speed, max_packet_size);
|
---|
[1e70157] | 172 | if (ret != EOK) {
|
---|
| 173 | free(ep);
|
---|
| 174 | return ret;
|
---|
| 175 | }
|
---|
[a1313b8c] | 176 |
|
---|
| 177 | usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
|
---|
| 178 | address, endpoint, usb_str_transfer_type(transfer_type),
|
---|
| 179 | usb_str_speed(speed), direction, size, max_packet_size, interval);
|
---|
[6ce42e85] | 180 |
|
---|
[1e70157] | 181 | ret = usb_endpoint_manager_register_ep(&hc->ep_manager,
|
---|
[8dc762e0] | 182 | address, endpoint, direction, ep, size);
|
---|
[6ce42e85] | 183 | if (ret != EOK) {
|
---|
| 184 | endpoint_destroy(ep);
|
---|
[1e70157] | 185 | } else {
|
---|
[f567bcf] | 186 | usb_device_keeper_add_ep(&hc->manager, address, ep);
|
---|
[6ce42e85] | 187 | }
|
---|
| 188 | return ret;
|
---|
[8870230] | 189 | }
|
---|
| 190 | /*----------------------------------------------------------------------------*/
|
---|
| 191 | static int unregister_endpoint(
|
---|
| 192 | ddf_fun_t *fun, usb_address_t address,
|
---|
| 193 | usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
| 194 | {
|
---|
[a1313b8c] | 195 | hc_t *hc = fun_to_hc(fun);
|
---|
| 196 | assert(hc);
|
---|
| 197 | usb_log_debug("Unregister endpoint %d:%d %d.\n",
|
---|
| 198 | address, endpoint, direction);
|
---|
[6ce42e85] | 199 | return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
|
---|
| 200 | endpoint, direction);
|
---|
[8870230] | 201 | }
|
---|
| 202 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 203 | /** Interrupt out transaction interface function
|
---|
| 204 | *
|
---|
| 205 | * @param[in] fun DDF function that was called.
|
---|
| 206 | * @param[in] target USB device to write to.
|
---|
| 207 | * @param[in] max_packet_size maximum size of data packet the device accepts
|
---|
| 208 | * @param[in] data Source of data.
|
---|
| 209 | * @param[in] size Size of data source.
|
---|
| 210 | * @param[in] callback Function to call on transaction completion
|
---|
| 211 | * @param[in] arg Additional for callback function.
|
---|
| 212 | * @return Error code.
|
---|
| 213 | */
|
---|
[b9fa0a9] | 214 | static int interrupt_out(
|
---|
| 215 | ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
|
---|
| 216 | size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
[4317827] | 217 | {
|
---|
[1a93bb0] | 218 | assert(fun);
|
---|
[c01cd32] | 219 | hc_t *hc = fun_to_hc(fun);
|
---|
[1a93bb0] | 220 | assert(hc);
|
---|
[fe10e72] | 221 |
|
---|
[48563a3] | 222 | usb_log_debug("Interrupt OUT %d:%d %zu(%zu).\n",
|
---|
| 223 | target.address, target.endpoint, size, max_packet_size);
|
---|
| 224 |
|
---|
[391d55b] | 225 | size_t res_bw;
|
---|
[8dc762e0] | 226 | endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
|
---|
[391d55b] | 227 | target.address, target.endpoint, USB_DIRECTION_OUT, &res_bw);
|
---|
[6ce42e85] | 228 | if (ep == NULL) {
|
---|
| 229 | usb_log_error("Endpoint(%d:%d) not registered for INT OUT.\n",
|
---|
| 230 | target.address, target.endpoint);
|
---|
| 231 | return ENOENT;
|
---|
| 232 | }
|
---|
[391d55b] | 233 | const size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
|
---|
| 234 | size, ep->max_packet_size);
|
---|
| 235 | if (res_bw < bw)
|
---|
| 236 | {
|
---|
| 237 | usb_log_error("Endpoint(%d:%d) INT IN needs %zu bw "
|
---|
| 238 | "but only %zu is reserved.\n",
|
---|
| 239 | target.address, target.endpoint, bw, res_bw);
|
---|
| 240 | return ENOENT;
|
---|
| 241 | }
|
---|
[6ce42e85] | 242 | assert(ep->speed ==
|
---|
| 243 | usb_device_keeper_get_speed(&hc->manager, target.address));
|
---|
| 244 | assert(ep->max_packet_size == max_packet_size);
|
---|
| 245 | assert(ep->transfer_type == USB_TRANSFER_INTERRUPT);
|
---|
| 246 |
|
---|
[b9fa0a9] | 247 | usb_transfer_batch_t *batch =
|
---|
[6ce42e85] | 248 | batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
|
---|
[4ca18ae] | 249 | ep->speed, data, size, NULL, 0, NULL, callback, arg, ep);
|
---|
[83c439c] | 250 | if (!batch)
|
---|
[7e62b62] | 251 | return ENOMEM;
|
---|
[5620bd4] | 252 | batch_interrupt_out(batch);
|
---|
[c01cd32] | 253 | const int ret = hc_schedule(hc, batch);
|
---|
[3bd96bb] | 254 | if (ret != EOK) {
|
---|
| 255 | batch_dispose(batch);
|
---|
| 256 | }
|
---|
[b9fa0a9] | 257 | return ret;
|
---|
[4317827] | 258 | }
|
---|
[3515533] | 259 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 260 | /** Interrupt in transaction interface function
|
---|
| 261 | *
|
---|
| 262 | * @param[in] fun DDF function that was called.
|
---|
| 263 | * @param[in] target USB device to write to.
|
---|
| 264 | * @param[in] max_packet_size maximum size of data packet the device accepts
|
---|
| 265 | * @param[out] data Data destination.
|
---|
| 266 | * @param[in] size Size of data source.
|
---|
| 267 | * @param[in] callback Function to call on transaction completion
|
---|
| 268 | * @param[in] arg Additional for callback function.
|
---|
| 269 | * @return Error code.
|
---|
| 270 | */
|
---|
[b9fa0a9] | 271 | static int interrupt_in(
|
---|
| 272 | ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
|
---|
| 273 | size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
[4317827] | 274 | {
|
---|
[1a93bb0] | 275 | assert(fun);
|
---|
[c01cd32] | 276 | hc_t *hc = fun_to_hc(fun);
|
---|
[1a93bb0] | 277 | assert(hc);
|
---|
[6ce42e85] | 278 |
|
---|
[48563a3] | 279 | usb_log_debug("Interrupt IN %d:%d %zu(%zu).\n",
|
---|
| 280 | target.address, target.endpoint, size, max_packet_size);
|
---|
[fe10e72] | 281 |
|
---|
[391d55b] | 282 | size_t res_bw;
|
---|
[8dc762e0] | 283 | endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
|
---|
[391d55b] | 284 | target.address, target.endpoint, USB_DIRECTION_IN, &res_bw);
|
---|
[6ce42e85] | 285 | if (ep == NULL) {
|
---|
| 286 | usb_log_error("Endpoint(%d:%d) not registered for INT IN.\n",
|
---|
[391d55b] | 287 | target.address, target.endpoint);
|
---|
[6ce42e85] | 288 | return ENOENT;
|
---|
| 289 | }
|
---|
[391d55b] | 290 | const size_t bw = bandwidth_count_usb11(ep->speed, ep->transfer_type,
|
---|
| 291 | size, ep->max_packet_size);
|
---|
| 292 | if (res_bw < bw)
|
---|
| 293 | {
|
---|
| 294 | usb_log_error("Endpoint(%d:%d) INT IN needs %zu bw "
|
---|
| 295 | "but only %zu bw is reserved.\n",
|
---|
| 296 | target.address, target.endpoint, bw, res_bw);
|
---|
| 297 | return ENOENT;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[6ce42e85] | 300 | assert(ep->speed ==
|
---|
| 301 | usb_device_keeper_get_speed(&hc->manager, target.address));
|
---|
| 302 | assert(ep->max_packet_size == max_packet_size);
|
---|
| 303 | assert(ep->transfer_type == USB_TRANSFER_INTERRUPT);
|
---|
| 304 |
|
---|
[b9fa0a9] | 305 | usb_transfer_batch_t *batch =
|
---|
[6ce42e85] | 306 | batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
|
---|
[4ca18ae] | 307 | ep->speed, data, size, NULL, 0, callback, NULL, arg, ep);
|
---|
[83c439c] | 308 | if (!batch)
|
---|
[7e62b62] | 309 | return ENOMEM;
|
---|
[5620bd4] | 310 | batch_interrupt_in(batch);
|
---|
[c01cd32] | 311 | const int ret = hc_schedule(hc, batch);
|
---|
[3bd96bb] | 312 | if (ret != EOK) {
|
---|
| 313 | batch_dispose(batch);
|
---|
| 314 | }
|
---|
[b9fa0a9] | 315 | return ret;
|
---|
[4317827] | 316 | }
|
---|
[3515533] | 317 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 318 | /** Bulk out transaction interface function
|
---|
| 319 | *
|
---|
| 320 | * @param[in] fun DDF function that was called.
|
---|
| 321 | * @param[in] target USB device to write to.
|
---|
| 322 | * @param[in] max_packet_size maximum size of data packet the device accepts
|
---|
| 323 | * @param[in] data Source of data.
|
---|
| 324 | * @param[in] size Size of data source.
|
---|
| 325 | * @param[in] callback Function to call on transaction completion
|
---|
| 326 | * @param[in] arg Additional for callback function.
|
---|
| 327 | * @return Error code.
|
---|
| 328 | */
|
---|
[b9fa0a9] | 329 | static int bulk_out(
|
---|
| 330 | ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
|
---|
| 331 | size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
[0e06a14] | 332 | {
|
---|
| 333 | assert(fun);
|
---|
[c01cd32] | 334 | hc_t *hc = fun_to_hc(fun);
|
---|
[0e06a14] | 335 | assert(hc);
|
---|
| 336 |
|
---|
| 337 | usb_log_debug("Bulk OUT %d:%d %zu(%zu).\n",
|
---|
| 338 | target.address, target.endpoint, size, max_packet_size);
|
---|
| 339 |
|
---|
[8dc762e0] | 340 | endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
|
---|
[6ce42e85] | 341 | target.address, target.endpoint, USB_DIRECTION_OUT, NULL);
|
---|
| 342 | if (ep == NULL) {
|
---|
| 343 | usb_log_error("Endpoint(%d:%d) not registered for BULK OUT.\n",
|
---|
| 344 | target.address, target.endpoint);
|
---|
| 345 | return ENOENT;
|
---|
| 346 | }
|
---|
| 347 | assert(ep->speed ==
|
---|
| 348 | usb_device_keeper_get_speed(&hc->manager, target.address));
|
---|
| 349 | assert(ep->max_packet_size == max_packet_size);
|
---|
| 350 | assert(ep->transfer_type == USB_TRANSFER_BULK);
|
---|
| 351 |
|
---|
[b9fa0a9] | 352 | usb_transfer_batch_t *batch =
|
---|
[6ce42e85] | 353 | batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
|
---|
[4ca18ae] | 354 | ep->speed, data, size, NULL, 0, NULL, callback, arg, ep);
|
---|
[0e06a14] | 355 | if (!batch)
|
---|
| 356 | return ENOMEM;
|
---|
[5620bd4] | 357 | batch_bulk_out(batch);
|
---|
[c01cd32] | 358 | const int ret = hc_schedule(hc, batch);
|
---|
[3bd96bb] | 359 | if (ret != EOK) {
|
---|
| 360 | batch_dispose(batch);
|
---|
| 361 | }
|
---|
[b9fa0a9] | 362 | return ret;
|
---|
[0e06a14] | 363 | }
|
---|
| 364 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 365 | /** Bulk in transaction interface function
|
---|
| 366 | *
|
---|
| 367 | * @param[in] fun DDF function that was called.
|
---|
| 368 | * @param[in] target USB device to write to.
|
---|
| 369 | * @param[in] max_packet_size maximum size of data packet the device accepts
|
---|
| 370 | * @param[out] data Data destination.
|
---|
| 371 | * @param[in] size Size of data source.
|
---|
| 372 | * @param[in] callback Function to call on transaction completion
|
---|
| 373 | * @param[in] arg Additional for callback function.
|
---|
| 374 | * @return Error code.
|
---|
| 375 | */
|
---|
[b9fa0a9] | 376 | static int bulk_in(
|
---|
| 377 | ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
|
---|
| 378 | size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
[0e06a14] | 379 | {
|
---|
| 380 | assert(fun);
|
---|
[c01cd32] | 381 | hc_t *hc = fun_to_hc(fun);
|
---|
[0e06a14] | 382 | assert(hc);
|
---|
| 383 | usb_log_debug("Bulk IN %d:%d %zu(%zu).\n",
|
---|
| 384 | target.address, target.endpoint, size, max_packet_size);
|
---|
| 385 |
|
---|
[8dc762e0] | 386 | endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
|
---|
[6ce42e85] | 387 | target.address, target.endpoint, USB_DIRECTION_IN, NULL);
|
---|
| 388 | if (ep == NULL) {
|
---|
| 389 | usb_log_error("Endpoint(%d:%d) not registered for BULK IN.\n",
|
---|
| 390 | target.address, target.endpoint);
|
---|
| 391 | return ENOENT;
|
---|
| 392 | }
|
---|
| 393 | assert(ep->speed ==
|
---|
| 394 | usb_device_keeper_get_speed(&hc->manager, target.address));
|
---|
| 395 | assert(ep->max_packet_size == max_packet_size);
|
---|
| 396 | assert(ep->transfer_type == USB_TRANSFER_BULK);
|
---|
| 397 |
|
---|
[b9fa0a9] | 398 | usb_transfer_batch_t *batch =
|
---|
[1e70157] | 399 | batch_get(fun, target, ep->transfer_type, ep->max_packet_size,
|
---|
[4ca18ae] | 400 | ep->speed, data, size, NULL, 0, callback, NULL, arg, ep);
|
---|
[0e06a14] | 401 | if (!batch)
|
---|
| 402 | return ENOMEM;
|
---|
[5620bd4] | 403 | batch_bulk_in(batch);
|
---|
[c01cd32] | 404 | const int ret = hc_schedule(hc, batch);
|
---|
[3bd96bb] | 405 | if (ret != EOK) {
|
---|
| 406 | batch_dispose(batch);
|
---|
| 407 | }
|
---|
[b9fa0a9] | 408 | return ret;
|
---|
[0e06a14] | 409 | }
|
---|
| 410 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 411 | /** Control write transaction interface function
|
---|
| 412 | *
|
---|
| 413 | * @param[in] fun DDF function that was called.
|
---|
| 414 | * @param[in] target USB device to write to.
|
---|
| 415 | * @param[in] max_packet_size maximum size of data packet the device accepts.
|
---|
[c61338a] | 416 | * @param[in] setup_data Data to send with SETUP transfer.
|
---|
| 417 | * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
|
---|
[a7e2f0d] | 418 | * @param[in] data Source of data.
|
---|
| 419 | * @param[in] size Size of data source.
|
---|
| 420 | * @param[in] callback Function to call on transaction completion.
|
---|
| 421 | * @param[in] arg Additional for callback function.
|
---|
| 422 | * @return Error code.
|
---|
| 423 | */
|
---|
[b9fa0a9] | 424 | static int control_write(
|
---|
| 425 | ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
|
---|
[a72620d] | 426 | void *setup_data, size_t setup_size, void *data, size_t size,
|
---|
| 427 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
| 428 | {
|
---|
[1a93bb0] | 429 | assert(fun);
|
---|
[c01cd32] | 430 | hc_t *hc = fun_to_hc(fun);
|
---|
[1a93bb0] | 431 | assert(hc);
|
---|
[b9fa0a9] | 432 | usb_speed_t speed =
|
---|
| 433 | usb_device_keeper_get_speed(&hc->manager, target.address);
|
---|
[71b6e92] | 434 | usb_log_debug("Control WRITE (%d) %d:%d %zu(%zu).\n",
|
---|
| 435 | speed, target.address, target.endpoint, size, max_packet_size);
|
---|
[8dc762e0] | 436 | endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
|
---|
[6ce42e85] | 437 | target.address, target.endpoint, USB_DIRECTION_BOTH, NULL);
|
---|
| 438 | if (ep == NULL) {
|
---|
| 439 | usb_log_warning("Endpoint(%d:%d) not registered for CONTROL.\n",
|
---|
| 440 | target.address, target.endpoint);
|
---|
| 441 | }
|
---|
[f241b05b] | 442 |
|
---|
[5620bd4] | 443 | if (setup_size != 8)
|
---|
| 444 | return EINVAL;
|
---|
| 445 |
|
---|
[b9fa0a9] | 446 | usb_transfer_batch_t *batch =
|
---|
| 447 | batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size, speed,
|
---|
[4ca18ae] | 448 | data, size, setup_data, setup_size, NULL, callback, arg, ep);
|
---|
[83c439c] | 449 | if (!batch)
|
---|
[f241b05b] | 450 | return ENOMEM;
|
---|
[62ed5bc] | 451 | usb_device_keeper_reset_if_need(&hc->manager, target, setup_data);
|
---|
[83c439c] | 452 | batch_control_write(batch);
|
---|
[c01cd32] | 453 | const int ret = hc_schedule(hc, batch);
|
---|
[3bd96bb] | 454 | if (ret != EOK) {
|
---|
| 455 | batch_dispose(batch);
|
---|
| 456 | }
|
---|
[b9fa0a9] | 457 | return ret;
|
---|
[a72620d] | 458 | }
|
---|
| 459 | /*----------------------------------------------------------------------------*/
|
---|
[a7e2f0d] | 460 | /** Control read transaction interface function
|
---|
| 461 | *
|
---|
| 462 | * @param[in] fun DDF function that was called.
|
---|
| 463 | * @param[in] target USB device to write to.
|
---|
| 464 | * @param[in] max_packet_size maximum size of data packet the device accepts.
|
---|
| 465 | * @param[in] setup_data Data to send with SETUP packet.
|
---|
| 466 | * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
|
---|
| 467 | * @param[out] data Source of data.
|
---|
| 468 | * @param[in] size Size of data source.
|
---|
| 469 | * @param[in] callback Function to call on transaction completion.
|
---|
| 470 | * @param[in] arg Additional for callback function.
|
---|
| 471 | * @return Error code.
|
---|
| 472 | */
|
---|
[b9fa0a9] | 473 | static int control_read(
|
---|
| 474 | ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
|
---|
[a72620d] | 475 | void *setup_data, size_t setup_size, void *data, size_t size,
|
---|
| 476 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
| 477 | {
|
---|
[1a93bb0] | 478 | assert(fun);
|
---|
[c01cd32] | 479 | hc_t *hc = fun_to_hc(fun);
|
---|
[1a93bb0] | 480 | assert(hc);
|
---|
[b9fa0a9] | 481 | usb_speed_t speed =
|
---|
| 482 | usb_device_keeper_get_speed(&hc->manager, target.address);
|
---|
[f241b05b] | 483 |
|
---|
[71b6e92] | 484 | usb_log_debug("Control READ(%d) %d:%d %zu(%zu).\n",
|
---|
| 485 | speed, target.address, target.endpoint, size, max_packet_size);
|
---|
[8dc762e0] | 486 | endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
|
---|
[6ce42e85] | 487 | target.address, target.endpoint, USB_DIRECTION_BOTH, NULL);
|
---|
| 488 | if (ep == NULL) {
|
---|
| 489 | usb_log_warning("Endpoint(%d:%d) not registered for CONTROL.\n",
|
---|
| 490 | target.address, target.endpoint);
|
---|
| 491 | }
|
---|
[b9fa0a9] | 492 | usb_transfer_batch_t *batch =
|
---|
| 493 | batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size, speed,
|
---|
[4ca18ae] | 494 | data, size, setup_data, setup_size, callback, NULL, arg, ep);
|
---|
[83c439c] | 495 | if (!batch)
|
---|
[f241b05b] | 496 | return ENOMEM;
|
---|
[83c439c] | 497 | batch_control_read(batch);
|
---|
[c01cd32] | 498 | const int ret = hc_schedule(hc, batch);
|
---|
[3bd96bb] | 499 | if (ret != EOK) {
|
---|
| 500 | batch_dispose(batch);
|
---|
| 501 | }
|
---|
[b9fa0a9] | 502 | return ret;
|
---|
[a72620d] | 503 | }
|
---|
[9e80904] | 504 | /*----------------------------------------------------------------------------*/
|
---|
[c01cd32] | 505 | usbhc_iface_t hc_iface = {
|
---|
[86b39f7e] | 506 | .reserve_default_address = reserve_default_address,
|
---|
| 507 | .release_default_address = release_default_address,
|
---|
| 508 | .request_address = request_address,
|
---|
| 509 | .bind_address = bind_address,
|
---|
| 510 | .release_address = release_address,
|
---|
[3515533] | 511 |
|
---|
[8870230] | 512 | .register_endpoint = register_endpoint,
|
---|
| 513 | .unregister_endpoint = unregister_endpoint,
|
---|
| 514 |
|
---|
[4317827] | 515 | .interrupt_out = interrupt_out,
|
---|
| 516 | .interrupt_in = interrupt_in,
|
---|
[3515533] | 517 |
|
---|
[0e06a14] | 518 | .bulk_out = bulk_out,
|
---|
[1c6a45f] | 519 | .bulk_in = bulk_in,
|
---|
[0e06a14] | 520 |
|
---|
[a72620d] | 521 | .control_write = control_write,
|
---|
[1c6a45f] | 522 | .control_read = control_read,
|
---|
[4317827] | 523 | };
|
---|
[c56dbe0] | 524 | /**
|
---|
| 525 | * @}
|
---|
| 526 | */
|
---|