| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 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 |
|
|---|
| 29 | /** @addtogroup libusbhost
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief HCD DDF interface implementation
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <ddf/driver.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 |
|
|---|
| 39 | #include <usb/debug.h>
|
|---|
| 40 | #include <usb/host/endpoint.h>
|
|---|
| 41 | #include <usb/host/hcd.h>
|
|---|
| 42 | #include "ddf_helpers.h"
|
|---|
| 43 |
|
|---|
| 44 | /** Find device handle by address interface function.
|
|---|
| 45 | *
|
|---|
| 46 | * @param[in] fun DDF function that was called.
|
|---|
| 47 | * @param[in] address Address in question.
|
|---|
| 48 | * @param[out] handle Where to store device handle if found.
|
|---|
| 49 | * @return Error code.
|
|---|
| 50 | */
|
|---|
| 51 | static int find_by_address(ddf_fun_t *fun, usb_address_t address,
|
|---|
| 52 | devman_handle_t *handle)
|
|---|
| 53 | {
|
|---|
| 54 | assert(fun);
|
|---|
| 55 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 56 | assert(hcd);
|
|---|
| 57 | return usb_device_manager_get_info_by_address(
|
|---|
| 58 | &hcd->dev_manager, address, handle, NULL);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /** Register endpoint interface function.
|
|---|
| 62 | * @param fun DDF function.
|
|---|
| 63 | * @param address USB address of the device.
|
|---|
| 64 | * @param endpoint USB endpoint number to be registered.
|
|---|
| 65 | * @param transfer_type Endpoint's transfer type.
|
|---|
| 66 | * @param direction USB communication direction the endpoint is capable of.
|
|---|
| 67 | * @param max_packet_size Maximu size of packets the endpoint accepts.
|
|---|
| 68 | * @param interval Preferred timeout between communication.
|
|---|
| 69 | * @return Error code.
|
|---|
| 70 | */
|
|---|
| 71 | static int register_endpoint(
|
|---|
| 72 | ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
|
|---|
| 73 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
|---|
| 74 | size_t max_packet_size, unsigned interval)
|
|---|
| 75 | {
|
|---|
| 76 | assert(fun);
|
|---|
| 77 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 78 | assert(hcd);
|
|---|
| 79 | const size_t size = max_packet_size;
|
|---|
| 80 | const usb_target_t target = {{.address = address, .endpoint = endpoint}};
|
|---|
| 81 |
|
|---|
| 82 | usb_log_debug("Register endpoint %d:%d %s-%s %zuB %ums.\n",
|
|---|
| 83 | address, endpoint, usb_str_transfer_type(transfer_type),
|
|---|
| 84 | usb_str_direction(direction), max_packet_size, interval);
|
|---|
| 85 |
|
|---|
| 86 | return hcd_add_ep(hcd, target, direction, transfer_type,
|
|---|
| 87 | max_packet_size, size);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /** Unregister endpoint interface function.
|
|---|
| 91 | * @param fun DDF function.
|
|---|
| 92 | * @param address USB address of the endpoint.
|
|---|
| 93 | * @param endpoint USB endpoint number.
|
|---|
| 94 | * @param direction Communication direction of the enpdoint to unregister.
|
|---|
| 95 | * @return Error code.
|
|---|
| 96 | */
|
|---|
| 97 | static int unregister_endpoint(
|
|---|
| 98 | ddf_fun_t *fun, usb_address_t address,
|
|---|
| 99 | usb_endpoint_t endpoint, usb_direction_t direction)
|
|---|
| 100 | {
|
|---|
| 101 | assert(fun);
|
|---|
| 102 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 103 | assert(hcd);
|
|---|
| 104 | const usb_target_t target = {{.address = address, .endpoint = endpoint}};
|
|---|
| 105 | usb_log_debug("Unregister endpoint %d:%d %s.\n",
|
|---|
| 106 | address, endpoint, usb_str_direction(direction));
|
|---|
| 107 | return hcd_remove_ep(hcd, target, direction);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /** Inbound communication interface function.
|
|---|
| 111 | * @param fun DDF function.
|
|---|
| 112 | * @param target Communication target.
|
|---|
| 113 | * @param setup_data Data to use in setup stage (control transfers).
|
|---|
| 114 | * @param data Pointer to data buffer.
|
|---|
| 115 | * @param size Size of the data buffer.
|
|---|
| 116 | * @param callback Function to call on communication end.
|
|---|
| 117 | * @param arg Argument passed to the callback function.
|
|---|
| 118 | * @return Error code.
|
|---|
| 119 | */
|
|---|
| 120 | static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
|
|---|
| 121 | uint8_t *data, size_t size, usbhc_iface_transfer_in_callback_t callback,
|
|---|
| 122 | void *arg)
|
|---|
| 123 | {
|
|---|
| 124 | return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)), target,
|
|---|
| 125 | USB_DIRECTION_IN, data, size, setup_data, callback, NULL, arg,
|
|---|
| 126 | "READ");
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /** Outbound communication interface function.
|
|---|
| 130 | * @param fun DDF function.
|
|---|
| 131 | * @param target Communication target.
|
|---|
| 132 | * @param setup_data Data to use in setup stage (control transfers).
|
|---|
| 133 | * @param data Pointer to data buffer.
|
|---|
| 134 | * @param size Size of the data buffer.
|
|---|
| 135 | * @param callback Function to call on communication end.
|
|---|
| 136 | * @param arg Argument passed to the callback function.
|
|---|
| 137 | * @return Error code.
|
|---|
| 138 | */
|
|---|
| 139 | static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
|
|---|
| 140 | const uint8_t *data, size_t size,
|
|---|
| 141 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
|---|
| 142 | {
|
|---|
| 143 | return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)),
|
|---|
| 144 | target, USB_DIRECTION_OUT, (uint8_t*)data, size, setup_data, NULL,
|
|---|
| 145 | callback, arg, "WRITE");
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /** usbhc Interface implementation using hcd_t from libusbhost library. */
|
|---|
| 149 | usbhc_iface_t hcd_iface = {
|
|---|
| 150 | .get_handle = find_by_address,
|
|---|
| 151 |
|
|---|
| 152 | .register_endpoint = register_endpoint,
|
|---|
| 153 | .unregister_endpoint = unregister_endpoint,
|
|---|
| 154 |
|
|---|
| 155 | .read = usb_read,
|
|---|
| 156 | .write = usb_write,
|
|---|
| 157 | };
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * @}
|
|---|
| 161 | */
|
|---|