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