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