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