[45d105a] | 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 | */
|
---|
[77ad86c] | 28 |
|
---|
[45d105a] | 29 | /** @addtogroup libusbhost
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief HCD DDF interface implementation
|
---|
| 34 | */
|
---|
[77ad86c] | 35 |
|
---|
[45d105a] | 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>
|
---|
[d8cdf39e] | 42 | #include "ddf_helpers.h"
|
---|
[45d105a] | 43 |
|
---|
[cbd568b] | 44 | /** Request address interface function.
|
---|
[45d105a] | 45 | *
|
---|
| 46 | * @param[in] fun DDF function that was called.
|
---|
[cbd568b] | 47 | * @param[in] address Pointer to preferred USB address.
|
---|
[45d105a] | 48 | * @param[out] address Place to write a new address.
|
---|
[cbd568b] | 49 | * @param[in] strict Fail if the preferred address is not available.
|
---|
| 50 | * @param[in] speed Speed to associate with the new default address.
|
---|
[45d105a] | 51 | * @return Error code.
|
---|
| 52 | */
|
---|
| 53 | static int request_address(
|
---|
[67f55e7b] | 54 | ddf_fun_t *fun, usb_address_t *address, bool strict, usb_speed_t speed)
|
---|
[45d105a] | 55 | {
|
---|
| 56 | assert(fun);
|
---|
[d8cdf39e] | 57 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
[45d105a] | 58 | assert(hcd);
|
---|
| 59 | assert(address);
|
---|
[f37eb84] | 60 | usb_log_debug("Address request: speed: %s, address: %d, strict: %s.\n",
|
---|
| 61 | usb_str_speed(speed), *address, strict ? "YES" : "NO");
|
---|
[0cd8089] | 62 | return usb_device_manager_request_address(
|
---|
[67f55e7b] | 63 | &hcd->dev_manager, address, strict, speed);
|
---|
[45d105a] | 64 | }
|
---|
[77ad86c] | 65 |
|
---|
[cbd568b] | 66 | /** Bind address interface function.
|
---|
[45d105a] | 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(
|
---|
[cbd568b] | 74 | ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
|
---|
[45d105a] | 75 | {
|
---|
| 76 | assert(fun);
|
---|
[d8cdf39e] | 77 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
[45d105a] | 78 | assert(hcd);
|
---|
| 79 |
|
---|
| 80 | usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
|
---|
[0cd8089] | 81 | return usb_device_manager_bind_address(
|
---|
| 82 | &hcd->dev_manager, address, handle);
|
---|
[45d105a] | 83 | }
|
---|
[77ad86c] | 84 |
|
---|
[45d105a] | 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);
|
---|
[d8cdf39e] | 96 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
[45d105a] | 97 | assert(hcd);
|
---|
[4267908] | 98 | return usb_device_manager_get_info_by_address(
|
---|
| 99 | &hcd->dev_manager, address, handle, NULL);
|
---|
[45d105a] | 100 | }
|
---|
[77ad86c] | 101 |
|
---|
[cbd568b] | 102 | /** Release address interface function.
|
---|
[45d105a] | 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);
|
---|
[d8cdf39e] | 111 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
[45d105a] | 112 | usb_log_debug("Address release %d.\n", address);
|
---|
[b4c1c95] | 113 | return hcd_release_address(hcd, address);
|
---|
[45d105a] | 114 | }
|
---|
[77ad86c] | 115 |
|
---|
[cbd568b] | 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 | */
|
---|
[45d105a] | 126 | static int register_endpoint(
|
---|
[27736cf] | 127 | ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
|
---|
[45d105a] | 128 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
---|
[cbd568b] | 129 | size_t max_packet_size, unsigned interval)
|
---|
[45d105a] | 130 | {
|
---|
| 131 | assert(fun);
|
---|
[d8cdf39e] | 132 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
[45d105a] | 133 | assert(hcd);
|
---|
| 134 | const size_t size = max_packet_size;
|
---|
[0816c2e] | 135 | const usb_target_t target = {{.address = address, .endpoint = endpoint}};
|
---|
| 136 |
|
---|
| 137 | usb_log_debug("Register endpoint %d:%d %s-%s %zuB %ums.\n",
|
---|
[45d105a] | 138 | address, endpoint, usb_str_transfer_type(transfer_type),
|
---|
[0816c2e] | 139 | usb_str_direction(direction), max_packet_size, interval);
|
---|
[45d105a] | 140 |
|
---|
[0816c2e] | 141 | return hcd_add_ep(hcd, target, direction, transfer_type,
|
---|
| 142 | max_packet_size, size);
|
---|
[45d105a] | 143 | }
|
---|
[77ad86c] | 144 |
|
---|
[cbd568b] | 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 | */
|
---|
[45d105a] | 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);
|
---|
[d8cdf39e] | 157 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
[45d105a] | 158 | assert(hcd);
|
---|
[0816c2e] | 159 | const usb_target_t target = {{.address = address, .endpoint = endpoint}};
|
---|
[45d105a] | 160 | usb_log_debug("Unregister endpoint %d:%d %s.\n",
|
---|
| 161 | address, endpoint, usb_str_direction(direction));
|
---|
[0816c2e] | 162 | return hcd_remove_ep(hcd, target, direction);
|
---|
[45d105a] | 163 | }
|
---|
[77ad86c] | 164 |
|
---|
[cbd568b] | 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 | */
|
---|
[3822f7c9] | 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 | {
|
---|
[b4c1c95] | 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");
|
---|
[3822f7c9] | 182 | }
|
---|
[77ad86c] | 183 |
|
---|
[cbd568b] | 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 | */
|
---|
[3822f7c9] | 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 | {
|
---|
[d8cdf39e] | 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");
|
---|
[45d105a] | 201 | }
|
---|
[77ad86c] | 202 |
|
---|
[cbd568b] | 203 | /** usbhc Interface implementation using hcd_t from libusbhost library. */
|
---|
[45d105a] | 204 | usbhc_iface_t hcd_iface = {
|
---|
| 205 | .request_address = request_address,
|
---|
| 206 | .bind_address = bind_address,
|
---|
[02fc5c4] | 207 | .get_handle = find_by_address,
|
---|
[45d105a] | 208 | .release_address = release_address,
|
---|
| 209 |
|
---|
| 210 | .register_endpoint = register_endpoint,
|
---|
| 211 | .unregister_endpoint = unregister_endpoint,
|
---|
| 212 |
|
---|
[3822f7c9] | 213 | .read = usb_read,
|
---|
| 214 | .write = usb_write,
|
---|
[45d105a] | 215 | };
|
---|
[77ad86c] | 216 |
|
---|
[45d105a] | 217 | /**
|
---|
| 218 | * @}
|
---|
| 219 | */
|
---|