| 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 | /*----------------------------------------------------------------------------*/
|
|---|
| 46 | static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
|
|---|
| 47 | {
|
|---|
| 48 | assert(fun);
|
|---|
| 49 | uhci_t *hc = fun_to_uhci(fun);
|
|---|
| 50 | assert(hc);
|
|---|
| 51 | usb_log_debug("Default address request with speed %d.\n", speed);
|
|---|
| 52 | device_keeper_reserve_default(&hc->device_manager, speed);
|
|---|
| 53 | return EOK;
|
|---|
| 54 | }
|
|---|
| 55 | /*----------------------------------------------------------------------------*/
|
|---|
| 56 | static int release_default_address(ddf_fun_t *fun)
|
|---|
| 57 | {
|
|---|
| 58 | assert(fun);
|
|---|
| 59 | uhci_t *hc = fun_to_uhci(fun);
|
|---|
| 60 | assert(hc);
|
|---|
| 61 | usb_log_debug("Default address release.\n");
|
|---|
| 62 | device_keeper_release_default(&hc->device_manager);
|
|---|
| 63 | return EOK;
|
|---|
| 64 | }
|
|---|
| 65 | /*----------------------------------------------------------------------------*/
|
|---|
| 66 | static int request_address(ddf_fun_t *fun, usb_speed_t speed,
|
|---|
| 67 | usb_address_t *address)
|
|---|
| 68 | {
|
|---|
| 69 | assert(fun);
|
|---|
| 70 | uhci_t *hc = fun_to_uhci(fun);
|
|---|
| 71 | assert(hc);
|
|---|
| 72 | assert(address);
|
|---|
| 73 |
|
|---|
| 74 | usb_log_debug("Address request with speed %d.\n", speed);
|
|---|
| 75 | *address = device_keeper_request(&hc->device_manager, speed);
|
|---|
| 76 | usb_log_debug("Address request with result: %d.\n", *address);
|
|---|
| 77 | if (*address <= 0)
|
|---|
| 78 | return *address;
|
|---|
| 79 | return EOK;
|
|---|
| 80 | }
|
|---|
| 81 | /*----------------------------------------------------------------------------*/
|
|---|
| 82 | static int bind_address(
|
|---|
| 83 | ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
|
|---|
| 84 | {
|
|---|
| 85 | assert(fun);
|
|---|
| 86 | uhci_t *hc = fun_to_uhci(fun);
|
|---|
| 87 | assert(hc);
|
|---|
| 88 | usb_log_debug("Address bind %d-%d.\n", address, handle);
|
|---|
| 89 | device_keeper_bind(&hc->device_manager, address, handle);
|
|---|
| 90 | return EOK;
|
|---|
| 91 | }
|
|---|
| 92 | /*----------------------------------------------------------------------------*/
|
|---|
| 93 | static int release_address(ddf_fun_t *fun, usb_address_t address)
|
|---|
| 94 | {
|
|---|
| 95 | assert(fun);
|
|---|
| 96 | uhci_t *hc = fun_to_uhci(fun);
|
|---|
| 97 | assert(hc);
|
|---|
| 98 | usb_log_debug("Address release %d.\n", address);
|
|---|
| 99 | device_keeper_release(&hc->device_manager, address);
|
|---|
| 100 | return EOK;
|
|---|
| 101 | }
|
|---|
| 102 | /*----------------------------------------------------------------------------*/
|
|---|
| 103 | static int interrupt_out(ddf_fun_t *fun, usb_target_t target,
|
|---|
| 104 | size_t max_packet_size, void *data, size_t size,
|
|---|
| 105 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
|---|
| 106 | {
|
|---|
| 107 | assert(fun);
|
|---|
| 108 | uhci_t *hc = fun_to_uhci(fun);
|
|---|
| 109 | assert(hc);
|
|---|
| 110 | usb_speed_t speed = device_keeper_speed(&hc->device_manager, target.address);
|
|---|
| 111 |
|
|---|
| 112 | usb_log_debug("Interrupt OUT %d:%d %zu(%zu).\n",
|
|---|
| 113 | target.address, target.endpoint, size, max_packet_size);
|
|---|
| 114 |
|
|---|
| 115 | batch_t *batch = batch_get(fun, target, USB_TRANSFER_INTERRUPT,
|
|---|
| 116 | max_packet_size, speed, data, size, NULL, 0, NULL, callback, arg,
|
|---|
| 117 | &hc->device_manager);
|
|---|
| 118 | if (!batch)
|
|---|
| 119 | return ENOMEM;
|
|---|
| 120 | batch_interrupt_out(batch);
|
|---|
| 121 | return EOK;
|
|---|
| 122 | }
|
|---|
| 123 | /*----------------------------------------------------------------------------*/
|
|---|
| 124 | static int interrupt_in(ddf_fun_t *fun, usb_target_t target,
|
|---|
| 125 | size_t max_packet_size, void *data, size_t size,
|
|---|
| 126 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
|---|
| 127 | {
|
|---|
| 128 | assert(fun);
|
|---|
| 129 | uhci_t *hc = fun_to_uhci(fun);
|
|---|
| 130 | assert(hc);
|
|---|
| 131 | usb_speed_t speed = device_keeper_speed(&hc->device_manager, target.address);
|
|---|
| 132 | usb_log_debug("Interrupt IN %d:%d %zu(%zu).\n",
|
|---|
| 133 | target.address, target.endpoint, size, max_packet_size);
|
|---|
| 134 |
|
|---|
| 135 | batch_t *batch = batch_get(fun, target, USB_TRANSFER_INTERRUPT,
|
|---|
| 136 | max_packet_size, speed, data, size, NULL, 0, callback, NULL, arg,
|
|---|
| 137 | &hc->device_manager);
|
|---|
| 138 | if (!batch)
|
|---|
| 139 | return ENOMEM;
|
|---|
| 140 | batch_interrupt_in(batch);
|
|---|
| 141 | return EOK;
|
|---|
| 142 | }
|
|---|
| 143 | /*----------------------------------------------------------------------------*/
|
|---|
| 144 | static int bulk_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_t *hc = fun_to_uhci(fun);
|
|---|
| 150 | assert(hc);
|
|---|
| 151 | usb_speed_t speed = device_keeper_speed(&hc->device_manager, target.address);
|
|---|
| 152 |
|
|---|
| 153 | usb_log_debug("Bulk OUT %d:%d %zu(%zu).\n",
|
|---|
| 154 | target.address, target.endpoint, size, max_packet_size);
|
|---|
| 155 |
|
|---|
| 156 | batch_t *batch = batch_get(fun, target, USB_TRANSFER_BULK,
|
|---|
| 157 | max_packet_size, speed, data, size, NULL, 0, NULL, callback, arg,
|
|---|
| 158 | &hc->device_manager);
|
|---|
| 159 | if (!batch)
|
|---|
| 160 | return ENOMEM;
|
|---|
| 161 | batch_bulk_out(batch);
|
|---|
| 162 | return EOK;
|
|---|
| 163 | }
|
|---|
| 164 | /*----------------------------------------------------------------------------*/
|
|---|
| 165 | static int bulk_in(ddf_fun_t *fun, usb_target_t target,
|
|---|
| 166 | size_t max_packet_size, void *data, size_t size,
|
|---|
| 167 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
|---|
| 168 | {
|
|---|
| 169 | assert(fun);
|
|---|
| 170 | uhci_t *hc = fun_to_uhci(fun);
|
|---|
| 171 | assert(hc);
|
|---|
| 172 | usb_speed_t speed = device_keeper_speed(&hc->device_manager, target.address);
|
|---|
| 173 | usb_log_debug("Bulk IN %d:%d %zu(%zu).\n",
|
|---|
| 174 | target.address, target.endpoint, size, max_packet_size);
|
|---|
| 175 |
|
|---|
| 176 | batch_t *batch = batch_get(fun, target, USB_TRANSFER_BULK,
|
|---|
| 177 | max_packet_size, speed, data, size, NULL, 0, callback, NULL, arg,
|
|---|
| 178 | &hc->device_manager);
|
|---|
| 179 | if (!batch)
|
|---|
| 180 | return ENOMEM;
|
|---|
| 181 | batch_bulk_in(batch);
|
|---|
| 182 | return EOK;
|
|---|
| 183 | }
|
|---|
| 184 | /*----------------------------------------------------------------------------*/
|
|---|
| 185 | static int control_write(ddf_fun_t *fun, usb_target_t target,
|
|---|
| 186 | size_t max_packet_size,
|
|---|
| 187 | void *setup_data, size_t setup_size, void *data, size_t size,
|
|---|
| 188 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
|---|
| 189 | {
|
|---|
| 190 | assert(fun);
|
|---|
| 191 | uhci_t *hc = fun_to_uhci(fun);
|
|---|
| 192 | assert(hc);
|
|---|
| 193 | usb_speed_t speed = device_keeper_speed(&hc->device_manager, target.address);
|
|---|
| 194 | usb_log_debug("Control WRITE %d:%d %zu(%zu).\n",
|
|---|
| 195 | target.address, target.endpoint, size, max_packet_size);
|
|---|
| 196 |
|
|---|
| 197 | if (setup_size != 8)
|
|---|
| 198 | return EINVAL;
|
|---|
| 199 |
|
|---|
| 200 | batch_t *batch = batch_get(fun, target, USB_TRANSFER_CONTROL,
|
|---|
| 201 | max_packet_size, speed, data, size, setup_data, setup_size,
|
|---|
| 202 | NULL, callback, arg, &hc->device_manager);
|
|---|
| 203 | if (!batch)
|
|---|
| 204 | return ENOMEM;
|
|---|
| 205 | device_keeper_reset_if_need(&hc->device_manager, target, setup_data);
|
|---|
| 206 | batch_control_write(batch);
|
|---|
| 207 | return EOK;
|
|---|
| 208 | }
|
|---|
| 209 | /*----------------------------------------------------------------------------*/
|
|---|
| 210 | static int control_read(ddf_fun_t *fun, usb_target_t target,
|
|---|
| 211 | size_t max_packet_size,
|
|---|
| 212 | void *setup_data, size_t setup_size, void *data, size_t size,
|
|---|
| 213 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
|---|
| 214 | {
|
|---|
| 215 | assert(fun);
|
|---|
| 216 | uhci_t *hc = fun_to_uhci(fun);
|
|---|
| 217 | assert(hc);
|
|---|
| 218 | usb_speed_t speed = device_keeper_speed(&hc->device_manager, target.address);
|
|---|
| 219 |
|
|---|
| 220 | usb_log_debug("Control READ %d:%d %zu(%zu).\n",
|
|---|
| 221 | target.address, target.endpoint, size, max_packet_size);
|
|---|
| 222 | batch_t *batch = batch_get(fun, target, USB_TRANSFER_CONTROL,
|
|---|
| 223 | max_packet_size, speed, data, size, setup_data, setup_size, callback,
|
|---|
| 224 | NULL, arg, &hc->device_manager);
|
|---|
| 225 | if (!batch)
|
|---|
| 226 | return ENOMEM;
|
|---|
| 227 | batch_control_read(batch);
|
|---|
| 228 | return EOK;
|
|---|
| 229 | }
|
|---|
| 230 | /*----------------------------------------------------------------------------*/
|
|---|
| 231 | usbhc_iface_t uhci_iface = {
|
|---|
| 232 | .reserve_default_address = reserve_default_address,
|
|---|
| 233 | .release_default_address = release_default_address,
|
|---|
| 234 | .request_address = request_address,
|
|---|
| 235 | .bind_address = bind_address,
|
|---|
| 236 | .release_address = release_address,
|
|---|
| 237 |
|
|---|
| 238 | .interrupt_out = interrupt_out,
|
|---|
| 239 | .interrupt_in = interrupt_in,
|
|---|
| 240 |
|
|---|
| 241 | .bulk_in = bulk_in,
|
|---|
| 242 | .bulk_out = bulk_out,
|
|---|
| 243 |
|
|---|
| 244 | .control_read = control_read,
|
|---|
| 245 | .control_write = control_write,
|
|---|
| 246 | };
|
|---|
| 247 | /**
|
|---|
| 248 | * @}
|
|---|
| 249 | */
|
|---|