| 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 | /** Release address interface function | 
|---|
| 126 | * | 
|---|
| 127 | * @param[in] fun DDF function that was called. | 
|---|
| 128 | * @param[in] address USB address to be released. | 
|---|
| 129 | * @return Error code. | 
|---|
| 130 | */ | 
|---|
| 131 | static int release_address(ddf_fun_t *fun, usb_address_t address) | 
|---|
| 132 | { | 
|---|
| 133 | assert(fun); | 
|---|
| 134 | hc_t *hc = fun_to_hc(fun); | 
|---|
| 135 | assert(hc); | 
|---|
| 136 | usb_log_debug("Address release %d.\n", address); | 
|---|
| 137 | usb_device_keeper_release(&hc->manager, address); | 
|---|
| 138 | return EOK; | 
|---|
| 139 | } | 
|---|
| 140 | /*----------------------------------------------------------------------------*/ | 
|---|
| 141 | static int register_endpoint( | 
|---|
| 142 | ddf_fun_t *fun, usb_address_t address, usb_speed_t ep_speed, | 
|---|
| 143 | usb_endpoint_t endpoint, | 
|---|
| 144 | usb_transfer_type_t transfer_type, usb_direction_t direction, | 
|---|
| 145 | size_t max_packet_size, unsigned int interval) | 
|---|
| 146 | { | 
|---|
| 147 | hc_t *hc = fun_to_hc(fun); | 
|---|
| 148 | assert(hc); | 
|---|
| 149 | const size_t size = max_packet_size; | 
|---|
| 150 | usb_speed_t speed = usb_device_keeper_get_speed(&hc->manager, address); | 
|---|
| 151 | if (speed >= USB_SPEED_MAX) { | 
|---|
| 152 | speed = ep_speed; | 
|---|
| 153 | } | 
|---|
| 154 | usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n", | 
|---|
| 155 | address, endpoint, usb_str_transfer_type(transfer_type), | 
|---|
| 156 | usb_str_speed(speed), direction, size, max_packet_size, interval); | 
|---|
| 157 |  | 
|---|
| 158 | return usb_endpoint_manager_add_ep(&hc->ep_manager, address, endpoint, | 
|---|
| 159 | direction, transfer_type, speed, max_packet_size, size); | 
|---|
| 160 | } | 
|---|
| 161 | /*----------------------------------------------------------------------------*/ | 
|---|
| 162 | static int unregister_endpoint( | 
|---|
| 163 | ddf_fun_t *fun, usb_address_t address, | 
|---|
| 164 | usb_endpoint_t endpoint, usb_direction_t direction) | 
|---|
| 165 | { | 
|---|
| 166 | hc_t *hc = fun_to_hc(fun); | 
|---|
| 167 | assert(hc); | 
|---|
| 168 | usb_log_debug("Unregister endpoint %d:%d %d.\n", | 
|---|
| 169 | address, endpoint, direction); | 
|---|
| 170 | return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address, | 
|---|
| 171 | endpoint, direction); | 
|---|
| 172 | } | 
|---|
| 173 | /*----------------------------------------------------------------------------*/ | 
|---|
| 174 | /** Interrupt out transaction interface function | 
|---|
| 175 | * | 
|---|
| 176 | * @param[in] fun DDF function that was called. | 
|---|
| 177 | * @param[in] target USB device to write to. | 
|---|
| 178 | * @param[in] data Source of data. | 
|---|
| 179 | * @param[in] size Size of data source. | 
|---|
| 180 | * @param[in] callback Function to call on transaction completion | 
|---|
| 181 | * @param[in] arg Additional for callback function. | 
|---|
| 182 | * @return Error code. | 
|---|
| 183 | */ | 
|---|
| 184 | static int interrupt_out( | 
|---|
| 185 | ddf_fun_t *fun, usb_target_t target, void *data, | 
|---|
| 186 | size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg) | 
|---|
| 187 | { | 
|---|
| 188 | usb_transfer_batch_t *batch = NULL; | 
|---|
| 189 | hc_t *hc = NULL; | 
|---|
| 190 | int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size, | 
|---|
| 191 | NULL, 0, NULL, callback, arg, "Interrupt OUT", &hc, &batch); | 
|---|
| 192 | if (ret != EOK) | 
|---|
| 193 | return ret; | 
|---|
| 194 | batch_interrupt_out(batch); | 
|---|
| 195 | ret = hc_schedule(hc, batch); | 
|---|
| 196 | if (ret != EOK) { | 
|---|
| 197 | usb_transfer_batch_dispose(batch); | 
|---|
| 198 | } | 
|---|
| 199 | return ret; | 
|---|
| 200 | } | 
|---|
| 201 | /*----------------------------------------------------------------------------*/ | 
|---|
| 202 | /** Interrupt in transaction interface function | 
|---|
| 203 | * | 
|---|
| 204 | * @param[in] fun DDF function that was called. | 
|---|
| 205 | * @param[in] target USB device to write to. | 
|---|
| 206 | * @param[out] data Data destination. | 
|---|
| 207 | * @param[in] size Size of data source. | 
|---|
| 208 | * @param[in] callback Function to call on transaction completion | 
|---|
| 209 | * @param[in] arg Additional for callback function. | 
|---|
| 210 | * @return Error code. | 
|---|
| 211 | */ | 
|---|
| 212 | static int interrupt_in( | 
|---|
| 213 | ddf_fun_t *fun, usb_target_t target, void *data, | 
|---|
| 214 | size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg) | 
|---|
| 215 | { | 
|---|
| 216 | usb_transfer_batch_t *batch = NULL; | 
|---|
| 217 | hc_t *hc = NULL; | 
|---|
| 218 | int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size, | 
|---|
| 219 | NULL, 0, callback, NULL, arg, "Interrupt IN", &hc, &batch); | 
|---|
| 220 | if (ret != EOK) | 
|---|
| 221 | return ret; | 
|---|
| 222 | batch_interrupt_in(batch); | 
|---|
| 223 | ret = hc_schedule(hc, batch); | 
|---|
| 224 | if (ret != EOK) { | 
|---|
| 225 | usb_transfer_batch_dispose(batch); | 
|---|
| 226 | } | 
|---|
| 227 | return ret; | 
|---|
| 228 | } | 
|---|
| 229 | /*----------------------------------------------------------------------------*/ | 
|---|
| 230 | /** Bulk out transaction interface function | 
|---|
| 231 | * | 
|---|
| 232 | * @param[in] fun DDF function that was called. | 
|---|
| 233 | * @param[in] target USB device to write to. | 
|---|
| 234 | * @param[in] data Source of data. | 
|---|
| 235 | * @param[in] size Size of data source. | 
|---|
| 236 | * @param[in] callback Function to call on transaction completion | 
|---|
| 237 | * @param[in] arg Additional for callback function. | 
|---|
| 238 | * @return Error code. | 
|---|
| 239 | */ | 
|---|
| 240 | static int bulk_out( | 
|---|
| 241 | ddf_fun_t *fun, usb_target_t target, void *data, | 
|---|
| 242 | size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg) | 
|---|
| 243 | { | 
|---|
| 244 | usb_transfer_batch_t *batch = NULL; | 
|---|
| 245 | hc_t *hc = NULL; | 
|---|
| 246 | int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size, | 
|---|
| 247 | NULL, 0, NULL, callback, arg, "Bulk OUT", &hc, &batch); | 
|---|
| 248 | if (ret != EOK) | 
|---|
| 249 | return ret; | 
|---|
| 250 | batch_bulk_out(batch); | 
|---|
| 251 | ret = hc_schedule(hc, batch); | 
|---|
| 252 | if (ret != EOK) { | 
|---|
| 253 | usb_transfer_batch_dispose(batch); | 
|---|
| 254 | } | 
|---|
| 255 | return ret; | 
|---|
| 256 | } | 
|---|
| 257 | /*----------------------------------------------------------------------------*/ | 
|---|
| 258 | /** Bulk in transaction interface function | 
|---|
| 259 | * | 
|---|
| 260 | * @param[in] fun DDF function that was called. | 
|---|
| 261 | * @param[in] target USB device to write to. | 
|---|
| 262 | * @param[out] data Data destination. | 
|---|
| 263 | * @param[in] size Size of data source. | 
|---|
| 264 | * @param[in] callback Function to call on transaction completion | 
|---|
| 265 | * @param[in] arg Additional for callback function. | 
|---|
| 266 | * @return Error code. | 
|---|
| 267 | */ | 
|---|
| 268 | static int bulk_in( | 
|---|
| 269 | ddf_fun_t *fun, usb_target_t target, void *data, | 
|---|
| 270 | size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg) | 
|---|
| 271 | { | 
|---|
| 272 | usb_transfer_batch_t *batch = NULL; | 
|---|
| 273 | hc_t *hc = NULL; | 
|---|
| 274 | int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size, | 
|---|
| 275 | NULL, 0, callback, NULL, arg, "Bulk IN", &hc, &batch); | 
|---|
| 276 | if (ret != EOK) | 
|---|
| 277 | return ret; | 
|---|
| 278 | batch_bulk_in(batch); | 
|---|
| 279 | ret = hc_schedule(hc, batch); | 
|---|
| 280 | if (ret != EOK) { | 
|---|
| 281 | usb_transfer_batch_dispose(batch); | 
|---|
| 282 | } | 
|---|
| 283 | return ret; | 
|---|
| 284 | } | 
|---|
| 285 | /*----------------------------------------------------------------------------*/ | 
|---|
| 286 | /** Control write transaction interface function | 
|---|
| 287 | * | 
|---|
| 288 | * @param[in] fun DDF function that was called. | 
|---|
| 289 | * @param[in] target USB device to write to. | 
|---|
| 290 | * @param[in] setup_data Data to send with SETUP transfer. | 
|---|
| 291 | * @param[in] setup_size Size of data to send with SETUP transfer (always 8B). | 
|---|
| 292 | * @param[in] data Source of data. | 
|---|
| 293 | * @param[in] size Size of data source. | 
|---|
| 294 | * @param[in] callback Function to call on transaction completion. | 
|---|
| 295 | * @param[in] arg Additional for callback function. | 
|---|
| 296 | * @return Error code. | 
|---|
| 297 | */ | 
|---|
| 298 | static int control_write( | 
|---|
| 299 | ddf_fun_t *fun, usb_target_t target, | 
|---|
| 300 | void *setup_data, size_t setup_size, void *data, size_t size, | 
|---|
| 301 | usbhc_iface_transfer_out_callback_t callback, void *arg) | 
|---|
| 302 | { | 
|---|
| 303 | usb_transfer_batch_t *batch = NULL; | 
|---|
| 304 | hc_t *hc = NULL; | 
|---|
| 305 | int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size, | 
|---|
| 306 | setup_data, setup_size, NULL, callback, arg, "Control WRITE", | 
|---|
| 307 | &hc, &batch); | 
|---|
| 308 | if (ret != EOK) | 
|---|
| 309 | return ret; | 
|---|
| 310 | usb_endpoint_manager_reset_if_need(&hc->ep_manager, target, setup_data); | 
|---|
| 311 | batch_control_write(batch); | 
|---|
| 312 | ret = hc_schedule(hc, batch); | 
|---|
| 313 | if (ret != EOK) { | 
|---|
| 314 | usb_transfer_batch_dispose(batch); | 
|---|
| 315 | } | 
|---|
| 316 | return ret; | 
|---|
| 317 | } | 
|---|
| 318 | /*----------------------------------------------------------------------------*/ | 
|---|
| 319 | /** Control read transaction interface function | 
|---|
| 320 | * | 
|---|
| 321 | * @param[in] fun DDF function that was called. | 
|---|
| 322 | * @param[in] target USB device to write to. | 
|---|
| 323 | * @param[in] setup_data Data to send with SETUP packet. | 
|---|
| 324 | * @param[in] setup_size Size of data to send with SETUP packet (should be 8B). | 
|---|
| 325 | * @param[out] data Source of data. | 
|---|
| 326 | * @param[in] size Size of data source. | 
|---|
| 327 | * @param[in] callback Function to call on transaction completion. | 
|---|
| 328 | * @param[in] arg Additional for callback function. | 
|---|
| 329 | * @return Error code. | 
|---|
| 330 | */ | 
|---|
| 331 | static int control_read( | 
|---|
| 332 | ddf_fun_t *fun, usb_target_t target, | 
|---|
| 333 | void *setup_data, size_t setup_size, void *data, size_t size, | 
|---|
| 334 | usbhc_iface_transfer_in_callback_t callback, void *arg) | 
|---|
| 335 | { | 
|---|
| 336 | usb_transfer_batch_t *batch = NULL; | 
|---|
| 337 | hc_t *hc = NULL; | 
|---|
| 338 | int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size, | 
|---|
| 339 | setup_data, setup_size, callback, NULL, arg, "Control READ", | 
|---|
| 340 | &hc, &batch); | 
|---|
| 341 | if (ret != EOK) | 
|---|
| 342 | return ret; | 
|---|
| 343 | batch_control_read(batch); | 
|---|
| 344 | ret = hc_schedule(hc, batch); | 
|---|
| 345 | if (ret != EOK) { | 
|---|
| 346 | usb_transfer_batch_dispose(batch); | 
|---|
| 347 | } | 
|---|
| 348 | return ret; | 
|---|
| 349 | } | 
|---|
| 350 | /*----------------------------------------------------------------------------*/ | 
|---|
| 351 | usbhc_iface_t hc_iface = { | 
|---|
| 352 | .request_address = request_address, | 
|---|
| 353 | .bind_address = bind_address, | 
|---|
| 354 | .release_address = release_address, | 
|---|
| 355 |  | 
|---|
| 356 | .register_endpoint = register_endpoint, | 
|---|
| 357 | .unregister_endpoint = unregister_endpoint, | 
|---|
| 358 |  | 
|---|
| 359 | .interrupt_out = interrupt_out, | 
|---|
| 360 | .interrupt_in = interrupt_in, | 
|---|
| 361 |  | 
|---|
| 362 | .bulk_out = bulk_out, | 
|---|
| 363 | .bulk_in = bulk_in, | 
|---|
| 364 |  | 
|---|
| 365 | .control_write = control_write, | 
|---|
| 366 | .control_read = control_read, | 
|---|
| 367 | }; | 
|---|
| 368 | /** | 
|---|
| 369 | * @} | 
|---|
| 370 | */ | 
|---|