| 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 |
|
|---|
| 39 | #include "iface.h"
|
|---|
| 40 | #include "hc.h"
|
|---|
| 41 |
|
|---|
| 42 | /** Reserve default address interface function
|
|---|
| 43 | *
|
|---|
| 44 | * @param[in] fun DDF function that was called.
|
|---|
| 45 | * @param[in] speed Speed to associate with the new default address.
|
|---|
| 46 | * @return Error code.
|
|---|
| 47 | */
|
|---|
| 48 | static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
|
|---|
| 49 | {
|
|---|
| 50 | assert(fun);
|
|---|
| 51 | hc_t *hc = fun_to_hc(fun);
|
|---|
| 52 | assert(hc);
|
|---|
| 53 | usb_log_debug("Default address request with speed %d.\n", speed);
|
|---|
| 54 | usb_device_keeper_reserve_default_address(&hc->manager, speed);
|
|---|
| 55 | return EOK;
|
|---|
| 56 | }
|
|---|
| 57 | /*----------------------------------------------------------------------------*/
|
|---|
| 58 | /** Release default address interface function
|
|---|
| 59 | *
|
|---|
| 60 | * @param[in] fun DDF function that was called.
|
|---|
| 61 | * @return Error code.
|
|---|
| 62 | */
|
|---|
| 63 | static int release_default_address(ddf_fun_t *fun)
|
|---|
| 64 | {
|
|---|
| 65 | assert(fun);
|
|---|
| 66 | hc_t *hc = fun_to_hc(fun);
|
|---|
| 67 | assert(hc);
|
|---|
| 68 | usb_log_debug("Default address release.\n");
|
|---|
| 69 | usb_device_keeper_release_default_address(&hc->manager);
|
|---|
| 70 | return EOK;
|
|---|
| 71 | }
|
|---|
| 72 | /*----------------------------------------------------------------------------*/
|
|---|
| 73 | /** Request address interface function
|
|---|
| 74 | *
|
|---|
| 75 | * @param[in] fun DDF function that was called.
|
|---|
| 76 | * @param[in] speed Speed to associate with the new default address.
|
|---|
| 77 | * @param[out] address Place to write a new address.
|
|---|
| 78 | * @return Error code.
|
|---|
| 79 | */
|
|---|
| 80 | static int request_address(
|
|---|
| 81 | ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
|
|---|
| 82 | {
|
|---|
| 83 | assert(fun);
|
|---|
| 84 | hc_t *hc = fun_to_hc(fun);
|
|---|
| 85 | assert(hc);
|
|---|
| 86 | assert(address);
|
|---|
| 87 |
|
|---|
| 88 | usb_log_debug("Address request with speed %d.\n", speed);
|
|---|
| 89 | *address = device_keeper_get_free_address(&hc->manager, speed);
|
|---|
| 90 | usb_log_debug("Address request with result: %d.\n", *address);
|
|---|
| 91 | if (*address <= 0)
|
|---|
| 92 | return *address;
|
|---|
| 93 | return EOK;
|
|---|
| 94 | }
|
|---|
| 95 | /*----------------------------------------------------------------------------*/
|
|---|
| 96 | /** Bind address interface function
|
|---|
| 97 | *
|
|---|
| 98 | * @param[in] fun DDF function that was called.
|
|---|
| 99 | * @param[in] address Address of the device
|
|---|
| 100 | * @param[in] handle Devman handle of the device driver.
|
|---|
| 101 | * @return Error code.
|
|---|
| 102 | */
|
|---|
| 103 | static int bind_address(
|
|---|
| 104 | ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
|
|---|
| 105 | {
|
|---|
| 106 | assert(fun);
|
|---|
| 107 | hc_t *hc = fun_to_hc(fun);
|
|---|
| 108 | assert(hc);
|
|---|
| 109 | usb_log_debug("Address bind %d-%d.\n", address, handle);
|
|---|
| 110 | usb_device_keeper_bind(&hc->manager, address, handle);
|
|---|
| 111 | return EOK;
|
|---|
| 112 | }
|
|---|
| 113 | /*----------------------------------------------------------------------------*/
|
|---|
| 114 | /** Release address interface function
|
|---|
| 115 | *
|
|---|
| 116 | * @param[in] fun DDF function that was called.
|
|---|
| 117 | * @param[in] address USB address to be released.
|
|---|
| 118 | * @return Error code.
|
|---|
| 119 | */
|
|---|
| 120 | static int release_address(ddf_fun_t *fun, usb_address_t address)
|
|---|
| 121 | {
|
|---|
| 122 | assert(fun);
|
|---|
| 123 | hc_t *hc = fun_to_hc(fun);
|
|---|
| 124 | assert(hc);
|
|---|
| 125 | usb_log_debug("Address release %d.\n", address);
|
|---|
| 126 | usb_device_keeper_release(&hc->manager, address);
|
|---|
| 127 | return EOK;
|
|---|
| 128 | }
|
|---|
| 129 | /*----------------------------------------------------------------------------*/
|
|---|
| 130 | /** Interrupt out transaction interface function
|
|---|
| 131 | *
|
|---|
| 132 | * @param[in] fun DDF function that was called.
|
|---|
| 133 | * @param[in] target USB device to write to.
|
|---|
| 134 | * @param[in] max_packet_size maximum size of data packet the device accepts
|
|---|
| 135 | * @param[in] data Source of data.
|
|---|
| 136 | * @param[in] size Size of data source.
|
|---|
| 137 | * @param[in] callback Function to call on transaction completion
|
|---|
| 138 | * @param[in] arg Additional for callback function.
|
|---|
| 139 | * @return Error code.
|
|---|
| 140 | */
|
|---|
| 141 | static int interrupt_out(
|
|---|
| 142 | ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
|
|---|
| 143 | size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
|
|---|
| 144 | {
|
|---|
| 145 | assert(fun);
|
|---|
| 146 | hc_t *hc = fun_to_hc(fun);
|
|---|
| 147 | assert(hc);
|
|---|
| 148 | usb_speed_t speed =
|
|---|
| 149 | usb_device_keeper_get_speed(&hc->manager, target.address);
|
|---|
| 150 |
|
|---|
| 151 | usb_log_debug("Interrupt OUT %d:%d %zu(%zu).\n",
|
|---|
| 152 | target.address, target.endpoint, size, max_packet_size);
|
|---|
| 153 |
|
|---|
| 154 | usb_transfer_batch_t *batch =
|
|---|
| 155 | batch_get(fun, target, USB_TRANSFER_INTERRUPT, max_packet_size,
|
|---|
| 156 | speed, data, size, NULL, 0, NULL, callback, arg, &hc->manager);
|
|---|
| 157 | if (!batch)
|
|---|
| 158 | return ENOMEM;
|
|---|
| 159 | batch_interrupt_out(batch);
|
|---|
| 160 | const int ret = hc_schedule(hc, batch);
|
|---|
| 161 | if (ret != EOK) {
|
|---|
| 162 | batch_dispose(batch);
|
|---|
| 163 | }
|
|---|
| 164 | return ret;
|
|---|
| 165 | }
|
|---|
| 166 | /*----------------------------------------------------------------------------*/
|
|---|
| 167 | /** Interrupt in transaction interface function
|
|---|
| 168 | *
|
|---|
| 169 | * @param[in] fun DDF function that was called.
|
|---|
| 170 | * @param[in] target USB device to write to.
|
|---|
| 171 | * @param[in] max_packet_size maximum size of data packet the device accepts
|
|---|
| 172 | * @param[out] data Data destination.
|
|---|
| 173 | * @param[in] size Size of data source.
|
|---|
| 174 | * @param[in] callback Function to call on transaction completion
|
|---|
| 175 | * @param[in] arg Additional for callback function.
|
|---|
| 176 | * @return Error code.
|
|---|
| 177 | */
|
|---|
| 178 | static int interrupt_in(
|
|---|
| 179 | ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
|
|---|
| 180 | size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
|
|---|
| 181 | {
|
|---|
| 182 | assert(fun);
|
|---|
| 183 | hc_t *hc = fun_to_hc(fun);
|
|---|
| 184 | assert(hc);
|
|---|
| 185 | usb_speed_t speed =
|
|---|
| 186 | usb_device_keeper_get_speed(&hc->manager, target.address);
|
|---|
| 187 | usb_log_debug("Interrupt IN %d:%d %zu(%zu).\n",
|
|---|
| 188 | target.address, target.endpoint, size, max_packet_size);
|
|---|
| 189 |
|
|---|
| 190 | usb_transfer_batch_t *batch =
|
|---|
| 191 | batch_get(fun, target, USB_TRANSFER_INTERRUPT, max_packet_size,
|
|---|
| 192 | speed, data, size, NULL, 0, callback, NULL, arg, &hc->manager);
|
|---|
| 193 | if (!batch)
|
|---|
| 194 | return ENOMEM;
|
|---|
| 195 | batch_interrupt_in(batch);
|
|---|
| 196 | const int ret = hc_schedule(hc, batch);
|
|---|
| 197 | if (ret != EOK) {
|
|---|
| 198 | batch_dispose(batch);
|
|---|
| 199 | }
|
|---|
| 200 | return ret;
|
|---|
| 201 | }
|
|---|
| 202 | /*----------------------------------------------------------------------------*/
|
|---|
| 203 | /** Bulk 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 | */
|
|---|
| 214 | static int bulk_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)
|
|---|
| 217 | {
|
|---|
| 218 | assert(fun);
|
|---|
| 219 | hc_t *hc = fun_to_hc(fun);
|
|---|
| 220 | assert(hc);
|
|---|
| 221 | usb_speed_t speed =
|
|---|
| 222 | usb_device_keeper_get_speed(&hc->manager, target.address);
|
|---|
| 223 |
|
|---|
| 224 | usb_log_debug("Bulk OUT %d:%d %zu(%zu).\n",
|
|---|
| 225 | target.address, target.endpoint, size, max_packet_size);
|
|---|
| 226 |
|
|---|
| 227 | usb_transfer_batch_t *batch =
|
|---|
| 228 | batch_get(fun, target, USB_TRANSFER_BULK, max_packet_size, speed,
|
|---|
| 229 | data, size, NULL, 0, NULL, callback, arg, &hc->manager);
|
|---|
| 230 | if (!batch)
|
|---|
| 231 | return ENOMEM;
|
|---|
| 232 | batch_bulk_out(batch);
|
|---|
| 233 | const int ret = hc_schedule(hc, batch);
|
|---|
| 234 | if (ret != EOK) {
|
|---|
| 235 | batch_dispose(batch);
|
|---|
| 236 | }
|
|---|
| 237 | return ret;
|
|---|
| 238 | }
|
|---|
| 239 | /*----------------------------------------------------------------------------*/
|
|---|
| 240 | /** Bulk in transaction interface function
|
|---|
| 241 | *
|
|---|
| 242 | * @param[in] fun DDF function that was called.
|
|---|
| 243 | * @param[in] target USB device to write to.
|
|---|
| 244 | * @param[in] max_packet_size maximum size of data packet the device accepts
|
|---|
| 245 | * @param[out] data Data destination.
|
|---|
| 246 | * @param[in] size Size of data source.
|
|---|
| 247 | * @param[in] callback Function to call on transaction completion
|
|---|
| 248 | * @param[in] arg Additional for callback function.
|
|---|
| 249 | * @return Error code.
|
|---|
| 250 | */
|
|---|
| 251 | static int bulk_in(
|
|---|
| 252 | ddf_fun_t *fun, usb_target_t target, size_t max_packet_size, void *data,
|
|---|
| 253 | size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
|
|---|
| 254 | {
|
|---|
| 255 | assert(fun);
|
|---|
| 256 | hc_t *hc = fun_to_hc(fun);
|
|---|
| 257 | assert(hc);
|
|---|
| 258 | usb_speed_t speed =
|
|---|
| 259 | usb_device_keeper_get_speed(&hc->manager, target.address);
|
|---|
| 260 | usb_log_debug("Bulk IN %d:%d %zu(%zu).\n",
|
|---|
| 261 | target.address, target.endpoint, size, max_packet_size);
|
|---|
| 262 |
|
|---|
| 263 | usb_transfer_batch_t *batch =
|
|---|
| 264 | batch_get(fun, target, USB_TRANSFER_BULK, max_packet_size, speed,
|
|---|
| 265 | data, size, NULL, 0, callback, NULL, arg, &hc->manager);
|
|---|
| 266 | if (!batch)
|
|---|
| 267 | return ENOMEM;
|
|---|
| 268 | batch_bulk_in(batch);
|
|---|
| 269 | const int ret = hc_schedule(hc, batch);
|
|---|
| 270 | if (ret != EOK) {
|
|---|
| 271 | batch_dispose(batch);
|
|---|
| 272 | }
|
|---|
| 273 | return ret;
|
|---|
| 274 | }
|
|---|
| 275 | /*----------------------------------------------------------------------------*/
|
|---|
| 276 | /** Control write transaction interface function
|
|---|
| 277 | *
|
|---|
| 278 | * @param[in] fun DDF function that was called.
|
|---|
| 279 | * @param[in] target USB device to write to.
|
|---|
| 280 | * @param[in] max_packet_size maximum size of data packet the device accepts.
|
|---|
| 281 | * @param[in] setup_data Data to send with SETUP transfer.
|
|---|
| 282 | * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
|
|---|
| 283 | * @param[in] data Source of data.
|
|---|
| 284 | * @param[in] size Size of data source.
|
|---|
| 285 | * @param[in] callback Function to call on transaction completion.
|
|---|
| 286 | * @param[in] arg Additional for callback function.
|
|---|
| 287 | * @return Error code.
|
|---|
| 288 | */
|
|---|
| 289 | static int control_write(
|
|---|
| 290 | ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
|
|---|
| 291 | void *setup_data, size_t setup_size, void *data, size_t size,
|
|---|
| 292 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
|---|
| 293 | {
|
|---|
| 294 | assert(fun);
|
|---|
| 295 | hc_t *hc = fun_to_hc(fun);
|
|---|
| 296 | assert(hc);
|
|---|
| 297 | usb_speed_t speed =
|
|---|
| 298 | usb_device_keeper_get_speed(&hc->manager, target.address);
|
|---|
| 299 | usb_log_debug("Control WRITE (%d) %d:%d %zu(%zu).\n",
|
|---|
| 300 | speed, target.address, target.endpoint, size, max_packet_size);
|
|---|
| 301 |
|
|---|
| 302 | if (setup_size != 8)
|
|---|
| 303 | return EINVAL;
|
|---|
| 304 |
|
|---|
| 305 | usb_transfer_batch_t *batch =
|
|---|
| 306 | batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size, speed,
|
|---|
| 307 | data, size, setup_data, setup_size, NULL, callback, arg,
|
|---|
| 308 | &hc->manager);
|
|---|
| 309 | if (!batch)
|
|---|
| 310 | return ENOMEM;
|
|---|
| 311 | usb_device_keeper_reset_if_need(&hc->manager, target, setup_data);
|
|---|
| 312 | batch_control_write(batch);
|
|---|
| 313 | const int ret = hc_schedule(hc, batch);
|
|---|
| 314 | if (ret != EOK) {
|
|---|
| 315 | batch_dispose(batch);
|
|---|
| 316 | }
|
|---|
| 317 | return ret;
|
|---|
| 318 | }
|
|---|
| 319 | /*----------------------------------------------------------------------------*/
|
|---|
| 320 | /** Control read transaction interface function
|
|---|
| 321 | *
|
|---|
| 322 | * @param[in] fun DDF function that was called.
|
|---|
| 323 | * @param[in] target USB device to write to.
|
|---|
| 324 | * @param[in] max_packet_size maximum size of data packet the device accepts.
|
|---|
| 325 | * @param[in] setup_data Data to send with SETUP packet.
|
|---|
| 326 | * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
|
|---|
| 327 | * @param[out] data Source of data.
|
|---|
| 328 | * @param[in] size Size of data source.
|
|---|
| 329 | * @param[in] callback Function to call on transaction completion.
|
|---|
| 330 | * @param[in] arg Additional for callback function.
|
|---|
| 331 | * @return Error code.
|
|---|
| 332 | */
|
|---|
| 333 | static int control_read(
|
|---|
| 334 | ddf_fun_t *fun, usb_target_t target, size_t max_packet_size,
|
|---|
| 335 | void *setup_data, size_t setup_size, void *data, size_t size,
|
|---|
| 336 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
|---|
| 337 | {
|
|---|
| 338 | assert(fun);
|
|---|
| 339 | hc_t *hc = fun_to_hc(fun);
|
|---|
| 340 | assert(hc);
|
|---|
| 341 | usb_speed_t speed =
|
|---|
| 342 | usb_device_keeper_get_speed(&hc->manager, target.address);
|
|---|
| 343 |
|
|---|
| 344 | usb_log_debug("Control READ(%d) %d:%d %zu(%zu).\n",
|
|---|
| 345 | speed, target.address, target.endpoint, size, max_packet_size);
|
|---|
| 346 | usb_transfer_batch_t *batch =
|
|---|
| 347 | batch_get(fun, target, USB_TRANSFER_CONTROL, max_packet_size, speed,
|
|---|
| 348 | data, size, setup_data, setup_size, callback, NULL, arg,
|
|---|
| 349 | &hc->manager);
|
|---|
| 350 | if (!batch)
|
|---|
| 351 | return ENOMEM;
|
|---|
| 352 | batch_control_read(batch);
|
|---|
| 353 | const int ret = hc_schedule(hc, batch);
|
|---|
| 354 | if (ret != EOK) {
|
|---|
| 355 | batch_dispose(batch);
|
|---|
| 356 | }
|
|---|
| 357 | return ret;
|
|---|
| 358 | }
|
|---|
| 359 | /*----------------------------------------------------------------------------*/
|
|---|
| 360 | usbhc_iface_t hc_iface = {
|
|---|
| 361 | .reserve_default_address = reserve_default_address,
|
|---|
| 362 | .release_default_address = release_default_address,
|
|---|
| 363 | .request_address = request_address,
|
|---|
| 364 | .bind_address = bind_address,
|
|---|
| 365 | .release_address = release_address,
|
|---|
| 366 |
|
|---|
| 367 | .interrupt_out = interrupt_out,
|
|---|
| 368 | .interrupt_in = interrupt_in,
|
|---|
| 369 |
|
|---|
| 370 | .bulk_out = bulk_out,
|
|---|
| 371 | .bulk_in = bulk_in,
|
|---|
| 372 |
|
|---|
| 373 | .control_write = control_write,
|
|---|
| 374 | .control_read = control_read,
|
|---|
| 375 | };
|
|---|
| 376 | /**
|
|---|
| 377 | * @}
|
|---|
| 378 | */
|
|---|