| 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 | /** Request address interface function.
|
|---|
| 45 | *
|
|---|
| 46 | * @param[in] fun DDF function that was called.
|
|---|
| 47 | * @param[in] address Pointer to preferred USB address.
|
|---|
| 48 | * @param[out] address Place to write a new address.
|
|---|
| 49 | * @param[in] strict Fail if the preferred address is not available.
|
|---|
| 50 | * @param[in] speed Speed to associate with the new default address.
|
|---|
| 51 | * @return Error code.
|
|---|
| 52 | */
|
|---|
| 53 | static int request_address(
|
|---|
| 54 | ddf_fun_t *fun, usb_address_t *address, bool strict, usb_speed_t speed)
|
|---|
| 55 | {
|
|---|
| 56 | assert(fun);
|
|---|
| 57 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 58 | assert(hcd);
|
|---|
| 59 | assert(address);
|
|---|
| 60 | usb_log_debug("Address request: speed: %s, address: %d, strict: %s.\n",
|
|---|
| 61 | usb_str_speed(speed), *address, strict ? "YES" : "NO");
|
|---|
| 62 | return usb_device_manager_request_address(
|
|---|
| 63 | &hcd->dev_manager, address, strict, speed);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /** Bind address interface function.
|
|---|
| 67 | *
|
|---|
| 68 | * @param[in] fun DDF function that was called.
|
|---|
| 69 | * @param[in] address Address of the device
|
|---|
| 70 | * @param[in] handle Devman handle of the device driver.
|
|---|
| 71 | * @return Error code.
|
|---|
| 72 | */
|
|---|
| 73 | static int bind_address(
|
|---|
| 74 | ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
|
|---|
| 75 | {
|
|---|
| 76 | assert(fun);
|
|---|
| 77 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 78 | assert(hcd);
|
|---|
| 79 |
|
|---|
| 80 | usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
|
|---|
| 81 | return usb_device_manager_bind_address(
|
|---|
| 82 | &hcd->dev_manager, address, handle);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /** Find device handle by address interface function.
|
|---|
| 86 | *
|
|---|
| 87 | * @param[in] fun DDF function that was called.
|
|---|
| 88 | * @param[in] address Address in question.
|
|---|
| 89 | * @param[out] handle Where to store device handle if found.
|
|---|
| 90 | * @return Error code.
|
|---|
| 91 | */
|
|---|
| 92 | static int find_by_address(ddf_fun_t *fun, usb_address_t address,
|
|---|
| 93 | devman_handle_t *handle)
|
|---|
| 94 | {
|
|---|
| 95 | assert(fun);
|
|---|
| 96 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 97 | assert(hcd);
|
|---|
| 98 | return usb_device_manager_get_info_by_address(
|
|---|
| 99 | &hcd->dev_manager, address, handle, NULL);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | /** Release address interface function.
|
|---|
| 103 | *
|
|---|
| 104 | * @param[in] fun DDF function that was called.
|
|---|
| 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)
|
|---|
| 109 | {
|
|---|
| 110 | assert(fun);
|
|---|
| 111 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 112 | usb_log_debug("Address release %d.\n", address);
|
|---|
| 113 | return hcd_release_address(hcd, address);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /** Register endpoint interface function.
|
|---|
| 117 | * @param fun DDF function.
|
|---|
| 118 | * @param address USB address of the device.
|
|---|
| 119 | * @param endpoint USB endpoint number to be registered.
|
|---|
| 120 | * @param transfer_type Endpoint's transfer type.
|
|---|
| 121 | * @param direction USB communication direction the endpoint is capable of.
|
|---|
| 122 | * @param max_packet_size Maximu size of packets the endpoint accepts.
|
|---|
| 123 | * @param interval Preferred timeout between communication.
|
|---|
| 124 | * @return Error code.
|
|---|
| 125 | */
|
|---|
| 126 | static int register_endpoint(
|
|---|
| 127 | ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
|
|---|
| 128 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
|---|
| 129 | size_t max_packet_size, unsigned interval)
|
|---|
| 130 | {
|
|---|
| 131 | assert(fun);
|
|---|
| 132 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 133 | assert(hcd);
|
|---|
| 134 | const size_t size = max_packet_size;
|
|---|
| 135 | const usb_target_t target = {{.address = address, .endpoint = endpoint}};
|
|---|
| 136 |
|
|---|
| 137 | usb_log_debug("Register endpoint %d:%d %s-%s %zuB %ums.\n",
|
|---|
| 138 | address, endpoint, usb_str_transfer_type(transfer_type),
|
|---|
| 139 | usb_str_direction(direction), max_packet_size, interval);
|
|---|
| 140 |
|
|---|
| 141 | return hcd_add_ep(hcd, target, direction, transfer_type,
|
|---|
| 142 | max_packet_size, size);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /** Unregister endpoint interface function.
|
|---|
| 146 | * @param fun DDF function.
|
|---|
| 147 | * @param address USB address of the endpoint.
|
|---|
| 148 | * @param endpoint USB endpoint number.
|
|---|
| 149 | * @param direction Communication direction of the enpdoint to unregister.
|
|---|
| 150 | * @return Error code.
|
|---|
| 151 | */
|
|---|
| 152 | static int unregister_endpoint(
|
|---|
| 153 | ddf_fun_t *fun, usb_address_t address,
|
|---|
| 154 | usb_endpoint_t endpoint, usb_direction_t direction)
|
|---|
| 155 | {
|
|---|
| 156 | assert(fun);
|
|---|
| 157 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 158 | assert(hcd);
|
|---|
| 159 | const usb_target_t target = {{.address = address, .endpoint = endpoint}};
|
|---|
| 160 | usb_log_debug("Unregister endpoint %d:%d %s.\n",
|
|---|
| 161 | address, endpoint, usb_str_direction(direction));
|
|---|
| 162 | return hcd_remove_ep(hcd, target, direction);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /** Inbound communication interface function.
|
|---|
| 166 | * @param fun DDF function.
|
|---|
| 167 | * @param target Communication target.
|
|---|
| 168 | * @param setup_data Data to use in setup stage (control transfers).
|
|---|
| 169 | * @param data Pointer to data buffer.
|
|---|
| 170 | * @param size Size of the data buffer.
|
|---|
| 171 | * @param callback Function to call on communication end.
|
|---|
| 172 | * @param arg Argument passed to the callback function.
|
|---|
| 173 | * @return Error code.
|
|---|
| 174 | */
|
|---|
| 175 | static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
|
|---|
| 176 | uint8_t *data, size_t size, usbhc_iface_transfer_in_callback_t callback,
|
|---|
| 177 | void *arg)
|
|---|
| 178 | {
|
|---|
| 179 | return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)), target,
|
|---|
| 180 | USB_DIRECTION_IN, data, size, setup_data, callback, NULL, arg,
|
|---|
| 181 | "READ");
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /** Outbound communication interface function.
|
|---|
| 185 | * @param fun DDF function.
|
|---|
| 186 | * @param target Communication target.
|
|---|
| 187 | * @param setup_data Data to use in setup stage (control transfers).
|
|---|
| 188 | * @param data Pointer to data buffer.
|
|---|
| 189 | * @param size Size of the data buffer.
|
|---|
| 190 | * @param callback Function to call on communication end.
|
|---|
| 191 | * @param arg Argument passed to the callback function.
|
|---|
| 192 | * @return Error code.
|
|---|
| 193 | */
|
|---|
| 194 | static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
|
|---|
| 195 | const uint8_t *data, size_t size,
|
|---|
| 196 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
|---|
| 197 | {
|
|---|
| 198 | return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)),
|
|---|
| 199 | target, USB_DIRECTION_OUT, (uint8_t*)data, size, setup_data, NULL,
|
|---|
| 200 | callback, arg, "WRITE");
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /** usbhc Interface implementation using hcd_t from libusbhost library. */
|
|---|
| 204 | usbhc_iface_t hcd_iface = {
|
|---|
| 205 | .request_address = request_address,
|
|---|
| 206 | .bind_address = bind_address,
|
|---|
| 207 | .get_handle = find_by_address,
|
|---|
| 208 | .release_address = release_address,
|
|---|
| 209 |
|
|---|
| 210 | .register_endpoint = register_endpoint,
|
|---|
| 211 | .unregister_endpoint = unregister_endpoint,
|
|---|
| 212 |
|
|---|
| 213 | .read = usb_read,
|
|---|
| 214 | .write = usb_write,
|
|---|
| 215 | };
|
|---|
| 216 |
|
|---|
| 217 | /**
|
|---|
| 218 | * @}
|
|---|
| 219 | */
|
|---|