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 | /** @addtogroup libusbhost
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * @brief HCD DDF interface implementation
|
---|
33 | */
|
---|
34 | #include <ddf/driver.h>
|
---|
35 | #include <errno.h>
|
---|
36 |
|
---|
37 | #include <usb/debug.h>
|
---|
38 | #include <usb/host/endpoint.h>
|
---|
39 | #include <usb/host/hcd.h>
|
---|
40 |
|
---|
41 | static inline int send_batch(
|
---|
42 | ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
|
---|
43 | void *data, size_t size, void * setup_data, size_t setup_size,
|
---|
44 | usbhc_iface_transfer_in_callback_t in,
|
---|
45 | usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
|
---|
46 | {
|
---|
47 | assert(fun);
|
---|
48 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
49 | assert(hcd);
|
---|
50 |
|
---|
51 | int ret;
|
---|
52 |
|
---|
53 | size_t res_bw;
|
---|
54 | endpoint_t *ep = usb_endpoint_manager_get_ep(&hcd->ep_manager,
|
---|
55 | target.address, target.endpoint, direction, &res_bw);
|
---|
56 | if (ep == NULL) {
|
---|
57 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
58 | target.address, target.endpoint, name);
|
---|
59 | return ENOENT;
|
---|
60 | }
|
---|
61 |
|
---|
62 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
---|
63 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
64 |
|
---|
65 | const size_t bw = bandwidth_count_usb11(
|
---|
66 | ep->speed, ep->transfer_type, size, ep->max_packet_size);
|
---|
67 | if (res_bw < bw) {
|
---|
68 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
69 | "but only %zu is reserved.\n",
|
---|
70 | target.address, target.endpoint, name, bw, res_bw);
|
---|
71 | return ENOSPC;
|
---|
72 | }
|
---|
73 | if (!hcd->schedule) {
|
---|
74 | usb_log_error("HCD does not implement scheduler.\n");
|
---|
75 | return ENOTSUP;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /* No private data and no private data dtor */
|
---|
79 | usb_transfer_batch_t *batch =
|
---|
80 | usb_transfer_batch_get(ep, data, size, setup_data, setup_size,
|
---|
81 | in, out, arg, fun, NULL, NULL);
|
---|
82 | if (!batch) {
|
---|
83 | return ENOMEM;
|
---|
84 | }
|
---|
85 |
|
---|
86 | ret = hcd->schedule(hcd, batch);
|
---|
87 | if (ret != EOK)
|
---|
88 | usb_transfer_batch_dispose(batch);
|
---|
89 |
|
---|
90 | return ret;
|
---|
91 | }
|
---|
92 | /*----------------------------------------------------------------------------*/
|
---|
93 | /** Request address interface function
|
---|
94 | *
|
---|
95 | * @param[in] fun DDF function that was called.
|
---|
96 | * @param[in] speed Speed to associate with the new default address.
|
---|
97 | * @param[out] address Place to write a new address.
|
---|
98 | * @return Error code.
|
---|
99 | */
|
---|
100 | static int request_address(
|
---|
101 | ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
|
---|
102 | {
|
---|
103 | assert(fun);
|
---|
104 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
105 | assert(hcd);
|
---|
106 | assert(address);
|
---|
107 |
|
---|
108 | usb_log_debug("Address request speed: %s.\n", usb_str_speed(speed));
|
---|
109 | *address = device_keeper_get_free_address(&hcd->dev_manager, speed);
|
---|
110 | usb_log_debug("Address request with result: %d.\n", *address);
|
---|
111 | if (*address <= 0)
|
---|
112 | return *address;
|
---|
113 | return EOK;
|
---|
114 | }
|
---|
115 | /*----------------------------------------------------------------------------*/
|
---|
116 | /** Bind address interface function
|
---|
117 | *
|
---|
118 | * @param[in] fun DDF function that was called.
|
---|
119 | * @param[in] address Address of the device
|
---|
120 | * @param[in] handle Devman handle of the device driver.
|
---|
121 | * @return Error code.
|
---|
122 | */
|
---|
123 | static int bind_address(
|
---|
124 | ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
|
---|
125 | {
|
---|
126 | assert(fun);
|
---|
127 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
128 | assert(hcd);
|
---|
129 |
|
---|
130 | usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
|
---|
131 | usb_device_keeper_bind(&hcd->dev_manager, address, handle);
|
---|
132 | return EOK;
|
---|
133 | }
|
---|
134 | /*----------------------------------------------------------------------------*/
|
---|
135 | /** Find device handle by address interface function.
|
---|
136 | *
|
---|
137 | * @param[in] fun DDF function that was called.
|
---|
138 | * @param[in] address Address in question.
|
---|
139 | * @param[out] handle Where to store device handle if found.
|
---|
140 | * @return Error code.
|
---|
141 | */
|
---|
142 | static int find_by_address(ddf_fun_t *fun, usb_address_t address,
|
---|
143 | devman_handle_t *handle)
|
---|
144 | {
|
---|
145 | assert(fun);
|
---|
146 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
147 | assert(hcd);
|
---|
148 | const bool found =
|
---|
149 | usb_device_keeper_find_by_address(&hcd->dev_manager, address, handle);
|
---|
150 | return found ? EOK : ENOENT;
|
---|
151 | }
|
---|
152 | /*----------------------------------------------------------------------------*/
|
---|
153 | /** Release address interface function
|
---|
154 | *
|
---|
155 | * @param[in] fun DDF function that was called.
|
---|
156 | * @param[in] address USB address to be released.
|
---|
157 | * @return Error code.
|
---|
158 | */
|
---|
159 | static int release_address(ddf_fun_t *fun, usb_address_t address)
|
---|
160 | {
|
---|
161 | assert(fun);
|
---|
162 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
163 | assert(hcd);
|
---|
164 | usb_log_debug("Address release %d.\n", address);
|
---|
165 | usb_device_keeper_release(&hcd->dev_manager, address);
|
---|
166 | return EOK;
|
---|
167 | }
|
---|
168 | /*----------------------------------------------------------------------------*/
|
---|
169 | static int register_endpoint(
|
---|
170 | ddf_fun_t *fun, usb_address_t address, usb_speed_t ep_speed,
|
---|
171 | usb_endpoint_t endpoint,
|
---|
172 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
---|
173 | size_t max_packet_size, unsigned int interval)
|
---|
174 | {
|
---|
175 | assert(fun);
|
---|
176 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
177 | assert(hcd);
|
---|
178 | const size_t size = max_packet_size;
|
---|
179 | /* Default address is not bound or registered,
|
---|
180 | * thus it does not provide speed info. */
|
---|
181 | const usb_speed_t speed = (address == 0) ? ep_speed :
|
---|
182 | usb_device_keeper_get_speed(&hcd->dev_manager, address);
|
---|
183 |
|
---|
184 | usb_log_debug("Register endpoint %d:%d %s-%s %s %zuB %ums.\n",
|
---|
185 | address, endpoint, usb_str_transfer_type(transfer_type),
|
---|
186 | usb_str_direction(direction), usb_str_speed(speed),
|
---|
187 | max_packet_size, interval);
|
---|
188 |
|
---|
189 | endpoint_t *ep = endpoint_get(
|
---|
190 | address, endpoint, direction, transfer_type, speed, max_packet_size);
|
---|
191 | if (!ep)
|
---|
192 | return ENOMEM;
|
---|
193 | int ret = EOK;
|
---|
194 |
|
---|
195 | if (hcd->ep_add_hook) {
|
---|
196 | ret = hcd->ep_add_hook(hcd, ep);
|
---|
197 | }
|
---|
198 | if (ret != EOK) {
|
---|
199 | endpoint_destroy(ep);
|
---|
200 | return ret;
|
---|
201 | }
|
---|
202 |
|
---|
203 | ret = usb_endpoint_manager_register_ep(&hcd->ep_manager, ep, size);
|
---|
204 | if (ret != EOK) {
|
---|
205 | endpoint_destroy(ep);
|
---|
206 | }
|
---|
207 | return ret;
|
---|
208 | }
|
---|
209 | /*----------------------------------------------------------------------------*/
|
---|
210 | static int unregister_endpoint(
|
---|
211 | ddf_fun_t *fun, usb_address_t address,
|
---|
212 | usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
213 | {
|
---|
214 | assert(fun);
|
---|
215 | hcd_t *hcd = fun_to_hcd(fun);
|
---|
216 | assert(hcd);
|
---|
217 | usb_log_debug("Unregister endpoint %d:%d %s.\n",
|
---|
218 | address, endpoint, usb_str_direction(direction));
|
---|
219 | return usb_endpoint_manager_unregister_ep(&hcd->ep_manager, address,
|
---|
220 | endpoint, direction);
|
---|
221 | }
|
---|
222 | /*----------------------------------------------------------------------------*/
|
---|
223 | /** Interrupt out transaction interface function
|
---|
224 | *
|
---|
225 | * @param[in] fun DDF function that was called.
|
---|
226 | * @param[in] target USB device to write to.
|
---|
227 | * @param[in] data Source of data.
|
---|
228 | * @param[in] size Size of data source.
|
---|
229 | * @param[in] callback Function to call on transaction completion
|
---|
230 | * @param[in] arg Additional for callback function.
|
---|
231 | * @return Error code.
|
---|
232 | */
|
---|
233 | static int interrupt_out(
|
---|
234 | ddf_fun_t *fun, usb_target_t target, void *data,
|
---|
235 | size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
236 | {
|
---|
237 | return send_batch(fun, target, USB_DIRECTION_OUT, data, size,
|
---|
238 | NULL, 0, NULL, callback, arg, "Interrupt OUT");
|
---|
239 | }
|
---|
240 | /*----------------------------------------------------------------------------*/
|
---|
241 | /** Interrupt in transaction interface function
|
---|
242 | *
|
---|
243 | * @param[in] fun DDF function that was called.
|
---|
244 | * @param[in] target USB device to write to.
|
---|
245 | * @param[out] data Data destination.
|
---|
246 | * @param[in] size Size of data source.
|
---|
247 | * @param[in] callback Function to call on transaction completion
|
---|
248 | * @param[in] arg Additional for callback function.
|
---|
249 | * @return Error code.
|
---|
250 | */
|
---|
251 | static int interrupt_in(
|
---|
252 | ddf_fun_t *fun, usb_target_t target, void *data,
|
---|
253 | size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
254 | {
|
---|
255 | return send_batch(fun, target, USB_DIRECTION_IN, data, size,
|
---|
256 | NULL, 0, callback, NULL, arg, "Interrupt IN");
|
---|
257 | }
|
---|
258 | /*----------------------------------------------------------------------------*/
|
---|
259 | /** Bulk out transaction interface function
|
---|
260 | *
|
---|
261 | * @param[in] fun DDF function that was called.
|
---|
262 | * @param[in] target USB device to write to.
|
---|
263 | * @param[in] data Source of data.
|
---|
264 | * @param[in] size Size of data source.
|
---|
265 | * @param[in] callback Function to call on transaction completion
|
---|
266 | * @param[in] arg Additional for callback function.
|
---|
267 | * @return Error code.
|
---|
268 | */
|
---|
269 | static int bulk_out(
|
---|
270 | ddf_fun_t *fun, usb_target_t target, void *data,
|
---|
271 | size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
272 | {
|
---|
273 | return send_batch(fun, target, USB_DIRECTION_OUT, data, size,
|
---|
274 | NULL, 0, NULL, callback, arg, "Bulk OUT");
|
---|
275 | }
|
---|
276 | /*----------------------------------------------------------------------------*/
|
---|
277 | /** Bulk in transaction interface function
|
---|
278 | *
|
---|
279 | * @param[in] fun DDF function that was called.
|
---|
280 | * @param[in] target USB device to write to.
|
---|
281 | * @param[out] data Data destination.
|
---|
282 | * @param[in] size Size of data source.
|
---|
283 | * @param[in] callback Function to call on transaction completion
|
---|
284 | * @param[in] arg Additional for callback function.
|
---|
285 | * @return Error code.
|
---|
286 | */
|
---|
287 | static int bulk_in(
|
---|
288 | ddf_fun_t *fun, usb_target_t target, void *data,
|
---|
289 | size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
290 | {
|
---|
291 | return send_batch(fun, target, USB_DIRECTION_IN, data, size,
|
---|
292 | NULL, 0, callback, NULL, arg, "Bulk IN");
|
---|
293 | }
|
---|
294 | /*----------------------------------------------------------------------------*/
|
---|
295 | /** Control write transaction interface function
|
---|
296 | *
|
---|
297 | * @param[in] fun DDF function that was called.
|
---|
298 | * @param[in] target USB device to write to.
|
---|
299 | * @param[in] setup_data Data to send with SETUP transfer.
|
---|
300 | * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
|
---|
301 | * @param[in] data Source of data.
|
---|
302 | * @param[in] size Size of data source.
|
---|
303 | * @param[in] callback Function to call on transaction completion.
|
---|
304 | * @param[in] arg Additional for callback function.
|
---|
305 | * @return Error code.
|
---|
306 | */
|
---|
307 | static int control_write(
|
---|
308 | ddf_fun_t *fun, usb_target_t target,
|
---|
309 | void *setup_data, size_t setup_size, void *data, size_t size,
|
---|
310 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
311 | {
|
---|
312 | return send_batch(fun, target, USB_DIRECTION_BOTH, data, size,
|
---|
313 | setup_data, setup_size, NULL, callback, arg, "Control WRITE");
|
---|
314 | }
|
---|
315 | /*----------------------------------------------------------------------------*/
|
---|
316 | /** Control read transaction interface function
|
---|
317 | *
|
---|
318 | * @param[in] fun DDF function that was called.
|
---|
319 | * @param[in] target USB device to write to.
|
---|
320 | * @param[in] setup_data Data to send with SETUP packet.
|
---|
321 | * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
|
---|
322 | * @param[out] data Source of data.
|
---|
323 | * @param[in] size Size of data source.
|
---|
324 | * @param[in] callback Function to call on transaction completion.
|
---|
325 | * @param[in] arg Additional for callback function.
|
---|
326 | * @return Error code.
|
---|
327 | */
|
---|
328 | static int control_read(
|
---|
329 | ddf_fun_t *fun, usb_target_t target,
|
---|
330 | void *setup_data, size_t setup_size, void *data, size_t size,
|
---|
331 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
332 | {
|
---|
333 | return send_batch(fun, target, USB_DIRECTION_BOTH, data, size,
|
---|
334 | setup_data, setup_size, callback, NULL, arg, "Control READ");
|
---|
335 | }
|
---|
336 | /*----------------------------------------------------------------------------*/
|
---|
337 | usbhc_iface_t hcd_iface = {
|
---|
338 | .request_address = request_address,
|
---|
339 | .bind_address = bind_address,
|
---|
340 | .find_by_address = find_by_address,
|
---|
341 | .release_address = release_address,
|
---|
342 |
|
---|
343 | .register_endpoint = register_endpoint,
|
---|
344 | .unregister_endpoint = unregister_endpoint,
|
---|
345 |
|
---|
346 | .interrupt_out = interrupt_out,
|
---|
347 | .interrupt_in = interrupt_in,
|
---|
348 |
|
---|
349 | .bulk_out = bulk_out,
|
---|
350 | .bulk_in = bulk_in,
|
---|
351 |
|
---|
352 | .control_write = control_write,
|
---|
353 | .control_read = control_read,
|
---|
354 | };
|
---|
355 | /**
|
---|
356 | * @}
|
---|
357 | */
|
---|