[b371844] | 1 | /*
|
---|
[6cb58e6] | 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
[b371844] | 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 |
|
---|
[bd8c753d] | 29 | /** @addtogroup drvusbvhc
|
---|
[b371844] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[6cb58e6] | 33 | * Host controller interface implementation.
|
---|
[b371844] | 34 | */
|
---|
| 35 | #include <assert.h>
|
---|
| 36 | #include <errno.h>
|
---|
[4b4c797] | 37 | #include <usb/usb.h>
|
---|
[357a302] | 38 | #include <usb/ddfiface.h>
|
---|
[6cb58e6] | 39 | #include <usb/debug.h>
|
---|
| 40 | #include <usbhc_iface.h>
|
---|
[b371844] | 41 | #include "vhcd.h"
|
---|
[6cb58e6] | 42 |
|
---|
| 43 | #define GET_VHC_DATA(fun) \
|
---|
[56fd7cf] | 44 | ((vhc_data_t *)ddf_dev_data_get(ddf_fun_get_dev(fun)))
|
---|
[6cb58e6] | 45 | #define VHC_DATA(vhc, fun) \
|
---|
| 46 | vhc_data_t *vhc = GET_VHC_DATA(fun); assert(vhc->magic == 0xdeadbeef)
|
---|
| 47 |
|
---|
| 48 | #define UNSUPPORTED(methodname) \
|
---|
| 49 | usb_log_warning("Unsupported interface method `%s()' in %s:%d.\n", \
|
---|
| 50 | methodname, __FILE__, __LINE__)
|
---|
| 51 |
|
---|
| 52 | /** Found free USB address.
|
---|
| 53 | *
|
---|
| 54 | * @param[in] fun Device function the action was invoked on.
|
---|
| 55 | * @param[in] speed Speed of the device that will get this address.
|
---|
| 56 | * @param[out] address Non-null pointer where to store the free address.
|
---|
| 57 | * @return Error code.
|
---|
| 58 | */
|
---|
[67f55e7b] | 59 | static int request_address(ddf_fun_t *fun, usb_address_t *address, bool strict,
|
---|
| 60 | usb_speed_t speed)
|
---|
[c4ba29c7] | 61 | {
|
---|
[6cb58e6] | 62 | VHC_DATA(vhc, fun);
|
---|
[c4ba29c7] | 63 |
|
---|
[67f55e7b] | 64 | assert(address);
|
---|
| 65 | return usb_device_manager_request_address(
|
---|
| 66 | &vhc->dev_manager, address, strict, speed);
|
---|
[c4ba29c7] | 67 | }
|
---|
| 68 |
|
---|
[6cb58e6] | 69 | /** Bind USB address with device devman handle.
|
---|
| 70 | *
|
---|
| 71 | * @param[in] fun Device function the action was invoked on.
|
---|
| 72 | * @param[in] address USB address of the device.
|
---|
| 73 | * @param[in] handle Devman handle of the device.
|
---|
| 74 | * @return Error code.
|
---|
| 75 | */
|
---|
| 76 | static int bind_address(ddf_fun_t *fun,
|
---|
| 77 | usb_address_t address, devman_handle_t handle)
|
---|
[c4ba29c7] | 78 | {
|
---|
[6cb58e6] | 79 | VHC_DATA(vhc, fun);
|
---|
| 80 | usb_log_debug("Binding handle %" PRIun " to address %d.\n",
|
---|
| 81 | handle, address);
|
---|
[0cd8089] | 82 | usb_device_manager_bind_address(&vhc->dev_manager, address, handle);
|
---|
[284c629] | 83 |
|
---|
[6cb58e6] | 84 | return EOK;
|
---|
[284c629] | 85 | }
|
---|
| 86 |
|
---|
[df25ab6] | 87 | /** Find device handle by address interface function.
|
---|
| 88 | *
|
---|
| 89 | * @param[in] fun DDF function that was called.
|
---|
| 90 | * @param[in] address Address in question.
|
---|
| 91 | * @param[out] handle Where to store device handle if found.
|
---|
| 92 | * @return Error code.
|
---|
| 93 | */
|
---|
| 94 | static int find_by_address(ddf_fun_t *fun, usb_address_t address,
|
---|
| 95 | devman_handle_t *handle)
|
---|
| 96 | {
|
---|
| 97 | VHC_DATA(vhc, fun);
|
---|
[4267908] | 98 | return usb_device_manager_get_info_by_address(
|
---|
| 99 | &vhc->dev_manager, address, handle, NULL);
|
---|
[df25ab6] | 100 | }
|
---|
| 101 |
|
---|
[6cb58e6] | 102 | /** Release previously requested address.
|
---|
| 103 | *
|
---|
| 104 | * @param[in] fun Device function the action was invoked on.
|
---|
| 105 | * @param[in] address USB address to be released.
|
---|
| 106 | * @return Error code.
|
---|
| 107 | */
|
---|
| 108 | static int release_address(ddf_fun_t *fun, usb_address_t address)
|
---|
[284c629] | 109 | {
|
---|
[6cb58e6] | 110 | VHC_DATA(vhc, fun);
|
---|
| 111 | usb_log_debug("Releasing address %d...\n", address);
|
---|
[0cd8089] | 112 | usb_device_manager_release_address(&vhc->dev_manager, address);
|
---|
[6cb58e6] | 113 |
|
---|
| 114 | return ENOTSUP;
|
---|
[284c629] | 115 | }
|
---|
| 116 |
|
---|
[6cb58e6] | 117 | /** Register endpoint for bandwidth reservation.
|
---|
| 118 | *
|
---|
| 119 | * @param[in] fun Device function the action was invoked on.
|
---|
| 120 | * @param[in] address USB address of the device.
|
---|
| 121 | * @param[in] speed Endpoint speed (invalid means to use device one).
|
---|
| 122 | * @param[in] endpoint Endpoint number.
|
---|
| 123 | * @param[in] transfer_type USB transfer type.
|
---|
| 124 | * @param[in] direction Endpoint data direction.
|
---|
| 125 | * @param[in] max_packet_size Max packet size of the endpoint.
|
---|
| 126 | * @param[in] interval Polling interval.
|
---|
| 127 | * @return Error code.
|
---|
| 128 | */
|
---|
| 129 | static int register_endpoint(ddf_fun_t *fun,
|
---|
[27736cf] | 130 | usb_address_t address, usb_endpoint_t endpoint,
|
---|
[6cb58e6] | 131 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
---|
| 132 | size_t max_packet_size, unsigned int interval)
|
---|
[284c629] | 133 | {
|
---|
[6cb58e6] | 134 | VHC_DATA(vhc, fun);
|
---|
[284c629] | 135 |
|
---|
[48ae3ef] | 136 | return usb_endpoint_manager_add_ep(&vhc->ep_manager,
|
---|
| 137 | address, endpoint, direction, transfer_type, USB_SPEED_FULL, 1, 0,
|
---|
| 138 | NULL, NULL);
|
---|
[6cb58e6] | 139 |
|
---|
[284c629] | 140 | }
|
---|
| 141 |
|
---|
[6cb58e6] | 142 | /** Unregister endpoint (free some bandwidth reservation).
|
---|
| 143 | *
|
---|
| 144 | * @param[in] fun Device function the action was invoked on.
|
---|
| 145 | * @param[in] address USB address of the device.
|
---|
| 146 | * @param[in] endpoint Endpoint number.
|
---|
| 147 | * @param[in] direction Endpoint data direction.
|
---|
| 148 | * @return Error code.
|
---|
| 149 | */
|
---|
| 150 | static int unregister_endpoint(ddf_fun_t *fun, usb_address_t address,
|
---|
| 151 | usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
[284c629] | 152 | {
|
---|
[6cb58e6] | 153 | VHC_DATA(vhc, fun);
|
---|
[284c629] | 154 |
|
---|
[48ae3ef] | 155 | int rc = usb_endpoint_manager_remove_ep(&vhc->ep_manager,
|
---|
| 156 | address, endpoint, direction, NULL, NULL);
|
---|
[c4ba29c7] | 157 |
|
---|
[6cb58e6] | 158 | return rc;
|
---|
[c4ba29c7] | 159 | }
|
---|
[e779bd3c] | 160 | #if 0
|
---|
[6cb58e6] | 161 | /** Schedule interrupt out transfer.
|
---|
| 162 | *
|
---|
| 163 | * The callback is supposed to be called once the transfer (on the wire) is
|
---|
| 164 | * complete regardless of the outcome.
|
---|
| 165 | * However, the callback could be called only when this function returns
|
---|
| 166 | * with success status (i.e. returns EOK).
|
---|
| 167 | *
|
---|
| 168 | * @param[in] fun Device function the action was invoked on.
|
---|
| 169 | * @param[in] target Target pipe (address and endpoint number) specification.
|
---|
| 170 | * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
|
---|
| 171 | * by the caller).
|
---|
| 172 | * @param[in] size Size of the @p data buffer in bytes.
|
---|
| 173 | * @param[in] callback Callback to be issued once the transfer is complete.
|
---|
| 174 | * @param[in] arg Pass-through argument to the callback.
|
---|
| 175 | * @return Error code.
|
---|
| 176 | */
|
---|
| 177 | static int interrupt_out(ddf_fun_t *fun, usb_target_t target,
|
---|
| 178 | void *data, size_t size,
|
---|
[4317827] | 179 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
[63b4f90] | 180 | {
|
---|
[6cb58e6] | 181 | VHC_DATA(vhc, fun);
|
---|
[c4ba29c7] | 182 |
|
---|
[6cb58e6] | 183 | vhc_transfer_t *transfer = vhc_transfer_create(target.address,
|
---|
| 184 | target.endpoint, USB_DIRECTION_OUT, USB_TRANSFER_INTERRUPT,
|
---|
| 185 | fun, arg);
|
---|
| 186 | if (transfer == NULL) {
|
---|
| 187 | return ENOMEM;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | transfer->data_buffer = data;
|
---|
| 191 | transfer->data_buffer_size = size;
|
---|
| 192 | transfer->callback_out = callback;
|
---|
[c4ba29c7] | 193 |
|
---|
[6cb58e6] | 194 | int rc = vhc_virtdev_add_transfer(vhc, transfer);
|
---|
| 195 | if (rc != EOK) {
|
---|
| 196 | free(transfer);
|
---|
| 197 | return rc;
|
---|
| 198 | }
|
---|
[c4ba29c7] | 199 |
|
---|
| 200 | return EOK;
|
---|
[63b4f90] | 201 | }
|
---|
| 202 |
|
---|
[6cb58e6] | 203 | /** Schedule interrupt in transfer.
|
---|
| 204 | *
|
---|
| 205 | * The callback is supposed to be called once the transfer (on the wire) is
|
---|
| 206 | * complete regardless of the outcome.
|
---|
| 207 | * However, the callback could be called only when this function returns
|
---|
| 208 | * with success status (i.e. returns EOK).
|
---|
| 209 | *
|
---|
| 210 | * @param[in] fun Device function the action was invoked on.
|
---|
| 211 | * @param[in] target Target pipe (address and endpoint number) specification.
|
---|
| 212 | * @param[in] data Buffer where to store the data (in USB endianess,
|
---|
| 213 | * allocated and deallocated by the caller).
|
---|
| 214 | * @param[in] size Size of the @p data buffer in bytes.
|
---|
| 215 | * @param[in] callback Callback to be issued once the transfer is complete.
|
---|
| 216 | * @param[in] arg Pass-through argument to the callback.
|
---|
| 217 | * @return Error code.
|
---|
| 218 | */
|
---|
| 219 | static int interrupt_in(ddf_fun_t *fun, usb_target_t target,
|
---|
| 220 | void *data, size_t size,
|
---|
[4317827] | 221 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
[63b4f90] | 222 | {
|
---|
[6cb58e6] | 223 | VHC_DATA(vhc, fun);
|
---|
| 224 |
|
---|
| 225 | vhc_transfer_t *transfer = vhc_transfer_create(target.address,
|
---|
| 226 | target.endpoint, USB_DIRECTION_IN, USB_TRANSFER_INTERRUPT,
|
---|
| 227 | fun, arg);
|
---|
| 228 | if (transfer == NULL) {
|
---|
| 229 | return ENOMEM;
|
---|
| 230 | }
|
---|
[c4ba29c7] | 231 |
|
---|
[6cb58e6] | 232 | transfer->data_buffer = data;
|
---|
| 233 | transfer->data_buffer_size = size;
|
---|
| 234 | transfer->callback_in = callback;
|
---|
[c4ba29c7] | 235 |
|
---|
[6cb58e6] | 236 | int rc = vhc_virtdev_add_transfer(vhc, transfer);
|
---|
| 237 | if (rc != EOK) {
|
---|
| 238 | free(transfer);
|
---|
| 239 | return rc;
|
---|
| 240 | }
|
---|
[c4ba29c7] | 241 |
|
---|
| 242 | return EOK;
|
---|
[63b4f90] | 243 | }
|
---|
| 244 |
|
---|
[6cb58e6] | 245 | /** Schedule bulk out transfer.
|
---|
| 246 | *
|
---|
| 247 | * The callback is supposed to be called once the transfer (on the wire) is
|
---|
| 248 | * complete regardless of the outcome.
|
---|
| 249 | * However, the callback could be called only when this function returns
|
---|
| 250 | * with success status (i.e. returns EOK).
|
---|
| 251 | *
|
---|
| 252 | * @param[in] fun Device function the action was invoked on.
|
---|
| 253 | * @param[in] target Target pipe (address and endpoint number) specification.
|
---|
| 254 | * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
|
---|
| 255 | * by the caller).
|
---|
| 256 | * @param[in] size Size of the @p data buffer in bytes.
|
---|
| 257 | * @param[in] callback Callback to be issued once the transfer is complete.
|
---|
| 258 | * @param[in] arg Pass-through argument to the callback.
|
---|
| 259 | * @return Error code.
|
---|
| 260 | */
|
---|
| 261 | static int bulk_out(ddf_fun_t *fun, usb_target_t target,
|
---|
[4317827] | 262 | void *data, size_t size,
|
---|
| 263 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
| 264 | {
|
---|
[6cb58e6] | 265 | UNSUPPORTED("bulk_out");
|
---|
| 266 |
|
---|
| 267 | return ENOTSUP;
|
---|
[4317827] | 268 | }
|
---|
| 269 |
|
---|
[6cb58e6] | 270 | /** Schedule bulk in transfer.
|
---|
| 271 | *
|
---|
| 272 | * The callback is supposed to be called once the transfer (on the wire) is
|
---|
| 273 | * complete regardless of the outcome.
|
---|
| 274 | * However, the callback could be called only when this function returns
|
---|
| 275 | * with success status (i.e. returns EOK).
|
---|
| 276 | *
|
---|
| 277 | * @param[in] fun Device function the action was invoked on.
|
---|
| 278 | * @param[in] target Target pipe (address and endpoint number) specification.
|
---|
| 279 | * @param[in] data Buffer where to store the data (in USB endianess,
|
---|
| 280 | * allocated and deallocated by the caller).
|
---|
| 281 | * @param[in] size Size of the @p data buffer in bytes.
|
---|
| 282 | * @param[in] callback Callback to be issued once the transfer is complete.
|
---|
| 283 | * @param[in] arg Pass-through argument to the callback.
|
---|
| 284 | * @return Error code.
|
---|
| 285 | */
|
---|
| 286 | static int bulk_in(ddf_fun_t *fun, usb_target_t target,
|
---|
[4317827] | 287 | void *data, size_t size,
|
---|
| 288 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
| 289 | {
|
---|
[6cb58e6] | 290 | UNSUPPORTED("bulk_in");
|
---|
| 291 |
|
---|
| 292 | return ENOTSUP;
|
---|
[4317827] | 293 | }
|
---|
| 294 |
|
---|
[6cb58e6] | 295 | /** Schedule control write transfer.
|
---|
| 296 | *
|
---|
| 297 | * The callback is supposed to be called once the transfer (on the wire) is
|
---|
| 298 | * complete regardless of the outcome.
|
---|
| 299 | * However, the callback could be called only when this function returns
|
---|
| 300 | * with success status (i.e. returns EOK).
|
---|
| 301 | *
|
---|
| 302 | * @param[in] fun Device function the action was invoked on.
|
---|
| 303 | * @param[in] target Target pipe (address and endpoint number) specification.
|
---|
| 304 | * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
|
---|
| 305 | * and deallocated by the caller).
|
---|
| 306 | * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
|
---|
| 307 | * @param[in] data_buffer Data buffer (in USB endianess, allocated and
|
---|
| 308 | * deallocated by the caller).
|
---|
| 309 | * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
|
---|
| 310 | * @param[in] callback Callback to be issued once the transfer is complete.
|
---|
| 311 | * @param[in] arg Pass-through argument to the callback.
|
---|
| 312 | * @return Error code.
|
---|
| 313 | */
|
---|
[eb1a2f4] | 314 | static int control_write(ddf_fun_t *fun, usb_target_t target,
|
---|
[284c629] | 315 | void *setup_packet, size_t setup_packet_size,
|
---|
[6cb58e6] | 316 | void *data_buffer, size_t data_buffer_size,
|
---|
[284c629] | 317 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
| 318 | {
|
---|
[6cb58e6] | 319 | VHC_DATA(vhc, fun);
|
---|
| 320 |
|
---|
| 321 | vhc_transfer_t *transfer = vhc_transfer_create(target.address,
|
---|
| 322 | target.endpoint, USB_DIRECTION_OUT, USB_TRANSFER_CONTROL,
|
---|
| 323 | fun, arg);
|
---|
| 324 | if (transfer == NULL) {
|
---|
| 325 | return ENOMEM;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | transfer->setup_buffer = setup_packet;
|
---|
| 329 | transfer->setup_buffer_size = setup_packet_size;
|
---|
| 330 | transfer->data_buffer = data_buffer;
|
---|
| 331 | transfer->data_buffer_size = data_buffer_size;
|
---|
| 332 | transfer->callback_out = callback;
|
---|
[284c629] | 333 |
|
---|
[6cb58e6] | 334 | int rc = vhc_virtdev_add_transfer(vhc, transfer);
|
---|
| 335 | if (rc != EOK) {
|
---|
| 336 | free(transfer);
|
---|
| 337 | return rc;
|
---|
| 338 | }
|
---|
[284c629] | 339 |
|
---|
| 340 | return EOK;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
[6cb58e6] | 343 | /** Schedule control read transfer.
|
---|
| 344 | *
|
---|
| 345 | * The callback is supposed to be called once the transfer (on the wire) is
|
---|
| 346 | * complete regardless of the outcome.
|
---|
| 347 | * However, the callback could be called only when this function returns
|
---|
| 348 | * with success status (i.e. returns EOK).
|
---|
| 349 | *
|
---|
| 350 | * @param[in] fun Device function the action was invoked on.
|
---|
| 351 | * @param[in] target Target pipe (address and endpoint number) specification.
|
---|
| 352 | * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
|
---|
| 353 | * and deallocated by the caller).
|
---|
| 354 | * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
|
---|
| 355 | * @param[in] data_buffer Buffer where to store the data (in USB endianess,
|
---|
| 356 | * allocated and deallocated by the caller).
|
---|
| 357 | * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
|
---|
| 358 | * @param[in] callback Callback to be issued once the transfer is complete.
|
---|
| 359 | * @param[in] arg Pass-through argument to the callback.
|
---|
| 360 | * @return Error code.
|
---|
| 361 | */
|
---|
[eb1a2f4] | 362 | static int control_read(ddf_fun_t *fun, usb_target_t target,
|
---|
[284c629] | 363 | void *setup_packet, size_t setup_packet_size,
|
---|
[6cb58e6] | 364 | void *data_buffer, size_t data_buffer_size,
|
---|
[284c629] | 365 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
| 366 | {
|
---|
[6cb58e6] | 367 | VHC_DATA(vhc, fun);
|
---|
[284c629] | 368 |
|
---|
[6cb58e6] | 369 | vhc_transfer_t *transfer = vhc_transfer_create(target.address,
|
---|
| 370 | target.endpoint, USB_DIRECTION_IN, USB_TRANSFER_CONTROL,
|
---|
| 371 | fun, arg);
|
---|
| 372 | if (transfer == NULL) {
|
---|
| 373 | return ENOMEM;
|
---|
[357a302] | 374 | }
|
---|
| 375 |
|
---|
[6cb58e6] | 376 | transfer->setup_buffer = setup_packet;
|
---|
| 377 | transfer->setup_buffer_size = setup_packet_size;
|
---|
| 378 | transfer->data_buffer = data_buffer;
|
---|
| 379 | transfer->data_buffer_size = data_buffer_size;
|
---|
| 380 | transfer->callback_in = callback;
|
---|
[be9cbec] | 381 |
|
---|
[6cb58e6] | 382 | int rc = vhc_virtdev_add_transfer(vhc, transfer);
|
---|
| 383 | if (rc != EOK) {
|
---|
| 384 | free(transfer);
|
---|
| 385 | return rc;
|
---|
[be9cbec] | 386 | }
|
---|
| 387 |
|
---|
| 388 | return EOK;
|
---|
| 389 | }
|
---|
[e779bd3c] | 390 | #endif
|
---|
| 391 | static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_buffer,
|
---|
| 392 | uint8_t *data_buffer, size_t data_buffer_size,
|
---|
| 393 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
| 394 | {
|
---|
| 395 | VHC_DATA(vhc, fun);
|
---|
| 396 |
|
---|
[48ae3ef] | 397 | endpoint_t *ep = usb_endpoint_manager_find_ep(&vhc->ep_manager,
|
---|
[83c3123] | 398 | target.address, target.endpoint, USB_DIRECTION_IN);
|
---|
[e779bd3c] | 399 | if (ep == NULL) {
|
---|
| 400 | return ENOENT;
|
---|
| 401 | }
|
---|
| 402 | const usb_transfer_type_t transfer_type = ep->transfer_type;
|
---|
| 403 |
|
---|
| 404 |
|
---|
| 405 | vhc_transfer_t *transfer = vhc_transfer_create(target.address,
|
---|
| 406 | target.endpoint, USB_DIRECTION_IN, transfer_type,
|
---|
| 407 | fun, arg);
|
---|
| 408 | if (transfer == NULL) {
|
---|
| 409 | return ENOMEM;
|
---|
| 410 | }
|
---|
| 411 | if (transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 412 | transfer->setup_buffer = malloc(sizeof(uint64_t));
|
---|
| 413 | assert(transfer->setup_buffer);
|
---|
| 414 | memcpy(transfer->setup_buffer, &setup_buffer, sizeof(uint64_t));
|
---|
| 415 | transfer->setup_buffer_size = sizeof(uint64_t);
|
---|
| 416 | }
|
---|
| 417 | transfer->data_buffer = data_buffer;
|
---|
| 418 | transfer->data_buffer_size = data_buffer_size;
|
---|
| 419 | transfer->callback_in = callback;
|
---|
| 420 |
|
---|
| 421 | int rc = vhc_virtdev_add_transfer(vhc, transfer);
|
---|
| 422 | if (rc != EOK) {
|
---|
[7d364fb8] | 423 | if (transfer->setup_buffer != NULL) {
|
---|
| 424 | free(transfer->setup_buffer);
|
---|
| 425 | }
|
---|
[e779bd3c] | 426 | free(transfer);
|
---|
| 427 | return rc;
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | return EOK;
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_buffer,
|
---|
| 434 | const uint8_t *data_buffer, size_t data_buffer_size,
|
---|
| 435 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
| 436 | {
|
---|
| 437 | VHC_DATA(vhc, fun);
|
---|
| 438 |
|
---|
[48ae3ef] | 439 | endpoint_t *ep = usb_endpoint_manager_find_ep(&vhc->ep_manager,
|
---|
[83c3123] | 440 | target.address, target.endpoint, USB_DIRECTION_OUT);
|
---|
[e779bd3c] | 441 | if (ep == NULL) {
|
---|
| 442 | return ENOENT;
|
---|
| 443 | }
|
---|
| 444 | const usb_transfer_type_t transfer_type = ep->transfer_type;
|
---|
| 445 |
|
---|
| 446 |
|
---|
| 447 | vhc_transfer_t *transfer = vhc_transfer_create(target.address,
|
---|
| 448 | target.endpoint, USB_DIRECTION_OUT, transfer_type,
|
---|
| 449 | fun, arg);
|
---|
| 450 | if (transfer == NULL) {
|
---|
| 451 | return ENOMEM;
|
---|
| 452 | }
|
---|
| 453 | if (transfer_type == USB_TRANSFER_CONTROL) {
|
---|
| 454 | transfer->setup_buffer = malloc(sizeof(uint64_t));
|
---|
| 455 | assert(transfer->setup_buffer);
|
---|
| 456 | memcpy(transfer->setup_buffer, &setup_buffer, sizeof(uint64_t));
|
---|
| 457 | transfer->setup_buffer_size = sizeof(uint64_t);
|
---|
| 458 | }
|
---|
| 459 | transfer->data_buffer = (void*)data_buffer;
|
---|
| 460 | transfer->data_buffer_size = data_buffer_size;
|
---|
| 461 | transfer->callback_out = callback;
|
---|
| 462 |
|
---|
| 463 | int rc = vhc_virtdev_add_transfer(vhc, transfer);
|
---|
| 464 | if (rc != EOK) {
|
---|
[82a31261] | 465 | free(transfer->setup_buffer);
|
---|
[e779bd3c] | 466 | free(transfer);
|
---|
| 467 | return rc;
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | return EOK;
|
---|
| 471 | }
|
---|
[be9cbec] | 472 |
|
---|
[27ed734c] | 473 | static int tell_address(ddf_fun_t *fun, usb_address_t *address)
|
---|
[be9cbec] | 474 | {
|
---|
[6cb58e6] | 475 | UNSUPPORTED("tell_address");
|
---|
[be9cbec] | 476 |
|
---|
[6cb58e6] | 477 | return ENOTSUP;
|
---|
[be9cbec] | 478 | }
|
---|
| 479 |
|
---|
[eb1a2f4] | 480 | static int usb_iface_get_hc_handle_rh_impl(ddf_fun_t *root_hub_fun,
|
---|
| 481 | devman_handle_t *handle)
|
---|
| 482 | {
|
---|
[6cb58e6] | 483 | VHC_DATA(vhc, root_hub_fun);
|
---|
[eb1a2f4] | 484 |
|
---|
[56fd7cf] | 485 | *handle = ddf_fun_get_handle(vhc->hc_fun);
|
---|
[eb1a2f4] | 486 |
|
---|
| 487 | return EOK;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
[27ed734c] | 490 | static int tell_address_rh(ddf_fun_t *root_hub_fun, usb_address_t *address)
|
---|
[eb1a2f4] | 491 | {
|
---|
[6cb58e6] | 492 | VHC_DATA(vhc, root_hub_fun);
|
---|
[eb1a2f4] | 493 |
|
---|
[56fd7cf] | 494 | devman_handle_t handle = ddf_fun_get_handle(root_hub_fun);
|
---|
[eb1a2f4] | 495 |
|
---|
[6cb58e6] | 496 | usb_log_debug("tell_address_rh(handle=%" PRIun ")\n", handle);
|
---|
[4267908] | 497 | const usb_address_t addr =
|
---|
| 498 | usb_device_manager_find_address(&vhc->dev_manager, handle);
|
---|
[6cb58e6] | 499 | if (addr < 0) {
|
---|
| 500 | return addr;
|
---|
| 501 | } else {
|
---|
| 502 | *address = addr;
|
---|
| 503 | return EOK;
|
---|
| 504 | }
|
---|
[be9cbec] | 505 | }
|
---|
[4317827] | 506 |
|
---|
| 507 | usbhc_iface_t vhc_iface = {
|
---|
[ad104e0] | 508 | .request_address = request_address,
|
---|
[ce687bbe] | 509 | .bind_address = bind_address,
|
---|
[02fc5c4] | 510 | .get_handle = find_by_address,
|
---|
[ad104e0] | 511 | .release_address = release_address,
|
---|
| 512 |
|
---|
[6cb58e6] | 513 | .register_endpoint = register_endpoint,
|
---|
| 514 | .unregister_endpoint = unregister_endpoint,
|
---|
| 515 |
|
---|
[e779bd3c] | 516 | .write = usb_write,
|
---|
| 517 | .read = usb_read,
|
---|
[63b4f90] | 518 | };
|
---|
| 519 |
|
---|
[357a302] | 520 | usb_iface_t vhc_usb_iface = {
|
---|
| 521 | .get_hc_handle = usb_iface_get_hc_handle_hc_impl,
|
---|
[27ed734c] | 522 | .get_my_address = tell_address
|
---|
[357a302] | 523 | };
|
---|
| 524 |
|
---|
[eb1a2f4] | 525 | usb_iface_t rh_usb_iface = {
|
---|
| 526 | .get_hc_handle = usb_iface_get_hc_handle_rh_impl,
|
---|
[27ed734c] | 527 | .get_my_address = tell_address_rh
|
---|
[eb1a2f4] | 528 | };
|
---|
| 529 |
|
---|
[357a302] | 530 |
|
---|
[b371844] | 531 | /**
|
---|
| 532 | * @}
|
---|
| 533 | */
|
---|