1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky, 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 drvusbohci
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * @brief OHCI driver hc 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 |
|
---|
40 | #include "iface.h"
|
---|
41 | #include "hc.h"
|
---|
42 |
|
---|
43 | static inline int setup_batch(
|
---|
44 | ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
|
---|
45 | void *data, size_t size, void * setup_data, size_t setup_size,
|
---|
46 | usbhc_iface_transfer_in_callback_t in,
|
---|
47 | usbhc_iface_transfer_out_callback_t out, void *arg, const char* name,
|
---|
48 | hc_t **hc, usb_transfer_batch_t **batch)
|
---|
49 | {
|
---|
50 | assert(hc);
|
---|
51 | assert(batch);
|
---|
52 | assert(fun);
|
---|
53 | *hc = fun_to_hc(fun);
|
---|
54 | assert(*hc);
|
---|
55 |
|
---|
56 | size_t res_bw;
|
---|
57 | endpoint_t *ep = usb_endpoint_manager_get_ep(&(*hc)->ep_manager,
|
---|
58 | target.address, target.endpoint, direction, &res_bw);
|
---|
59 | if (ep == NULL) {
|
---|
60 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
61 | target.address, target.endpoint, name);
|
---|
62 | return ENOENT;
|
---|
63 | }
|
---|
64 |
|
---|
65 | usb_log_debug("%s %d:%d %zu(%zu).\n",
|
---|
66 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
67 |
|
---|
68 | const size_t bw = bandwidth_count_usb11(
|
---|
69 | ep->speed, ep->transfer_type, size, ep->max_packet_size);
|
---|
70 | if (res_bw < bw) {
|
---|
71 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
72 | "but only %zu is reserved.\n",
|
---|
73 | target.address, target.endpoint, name, bw, res_bw);
|
---|
74 | return ENOSPC;
|
---|
75 | }
|
---|
76 |
|
---|
77 | *batch = batch_get(
|
---|
78 | fun, ep, data, size, setup_data, setup_size, in, out, arg);
|
---|
79 | if (!*batch)
|
---|
80 | return ENOMEM;
|
---|
81 | return EOK;
|
---|
82 | }
|
---|
83 | /*----------------------------------------------------------------------------*/
|
---|
84 | /** Request address interface function
|
---|
85 | *
|
---|
86 | * @param[in] fun DDF function that was called.
|
---|
87 | * @param[in] speed Speed to associate with the new default address.
|
---|
88 | * @param[out] address Place to write a new address.
|
---|
89 | * @return Error code.
|
---|
90 | */
|
---|
91 | static int request_address(
|
---|
92 | ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
|
---|
93 | {
|
---|
94 | assert(fun);
|
---|
95 | hc_t *hc = fun_to_hc(fun);
|
---|
96 | assert(hc);
|
---|
97 | assert(address);
|
---|
98 |
|
---|
99 | usb_log_debug("Address request with speed %d.\n", speed);
|
---|
100 | *address = device_keeper_get_free_address(&hc->manager, speed);
|
---|
101 | usb_log_debug("Address request with result: %d.\n", *address);
|
---|
102 | if (*address <= 0)
|
---|
103 | return *address;
|
---|
104 | return EOK;
|
---|
105 | }
|
---|
106 | /*----------------------------------------------------------------------------*/
|
---|
107 | /** Bind address interface function
|
---|
108 | *
|
---|
109 | * @param[in] fun DDF function that was called.
|
---|
110 | * @param[in] address Address of the device
|
---|
111 | * @param[in] handle Devman handle of the device driver.
|
---|
112 | * @return Error code.
|
---|
113 | */
|
---|
114 | static int bind_address(
|
---|
115 | ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
|
---|
116 | {
|
---|
117 | assert(fun);
|
---|
118 | hc_t *hc = fun_to_hc(fun);
|
---|
119 | assert(hc);
|
---|
120 | usb_log_debug("Address bind %d-%d.\n", address, handle);
|
---|
121 | usb_device_keeper_bind(&hc->manager, address, handle);
|
---|
122 | return EOK;
|
---|
123 | }
|
---|
124 | /*----------------------------------------------------------------------------*/
|
---|
125 | /** Release address interface function
|
---|
126 | *
|
---|
127 | * @param[in] fun DDF function that was called.
|
---|
128 | * @param[in] address USB address to be released.
|
---|
129 | * @return Error code.
|
---|
130 | */
|
---|
131 | static int release_address(ddf_fun_t *fun, usb_address_t address)
|
---|
132 | {
|
---|
133 | assert(fun);
|
---|
134 | hc_t *hc = fun_to_hc(fun);
|
---|
135 | assert(hc);
|
---|
136 | usb_log_debug("Address release %d.\n", address);
|
---|
137 | usb_device_keeper_release(&hc->manager, address);
|
---|
138 | return EOK;
|
---|
139 | }
|
---|
140 | /*----------------------------------------------------------------------------*/
|
---|
141 | /** Register endpoint for bandwidth reservation.
|
---|
142 | *
|
---|
143 | * @param[in] fun Device function the action was invoked on.
|
---|
144 | * @param[in] address USB address of the device.
|
---|
145 | * @param[in] ep_speed Endpoint speed (invalid means to use device one).
|
---|
146 | * @param[in] endpoint Endpoint number.
|
---|
147 | * @param[in] transfer_type USB transfer type.
|
---|
148 | * @param[in] direction Endpoint data direction.
|
---|
149 | * @param[in] max_packet_size Max packet size of the endpoint.
|
---|
150 | * @param[in] interval Polling interval.
|
---|
151 | * @return Error code.
|
---|
152 | */
|
---|
153 | static int register_endpoint(ddf_fun_t *fun,
|
---|
154 | usb_address_t address, usb_speed_t ep_speed, usb_endpoint_t endpoint,
|
---|
155 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
---|
156 | size_t max_packet_size, unsigned int interval)
|
---|
157 | {
|
---|
158 | hc_t *hc = fun_to_hc(fun);
|
---|
159 | assert(hc);
|
---|
160 |
|
---|
161 | usb_speed_t speed = usb_device_keeper_get_speed(&hc->manager, address);
|
---|
162 | if (speed >= USB_SPEED_MAX) {
|
---|
163 | speed = ep_speed;
|
---|
164 | }
|
---|
165 | const size_t size = max_packet_size;
|
---|
166 | int ret;
|
---|
167 |
|
---|
168 | usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
|
---|
169 | address, endpoint, usb_str_transfer_type(transfer_type),
|
---|
170 | usb_str_speed(speed), direction, size, max_packet_size, interval);
|
---|
171 |
|
---|
172 | endpoint_t *ep = malloc(sizeof(endpoint_t));
|
---|
173 | if (ep == NULL)
|
---|
174 | return ENOMEM;
|
---|
175 | ret = endpoint_init(ep, address, endpoint, direction,
|
---|
176 | transfer_type, speed, max_packet_size);
|
---|
177 | if (ret != EOK) {
|
---|
178 | free(ep);
|
---|
179 | return ret;
|
---|
180 | }
|
---|
181 |
|
---|
182 | ret = usb_endpoint_manager_register_ep(&hc->ep_manager, ep, size);
|
---|
183 | if (ret != EOK) {
|
---|
184 | endpoint_destroy(ep);
|
---|
185 | }
|
---|
186 | return ret;
|
---|
187 | }
|
---|
188 | /*----------------------------------------------------------------------------*/
|
---|
189 | static int unregister_endpoint(
|
---|
190 | ddf_fun_t *fun, usb_address_t address,
|
---|
191 | usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
192 | {
|
---|
193 | hc_t *hc = fun_to_hc(fun);
|
---|
194 | assert(hc);
|
---|
195 | usb_log_debug("Unregister endpoint %d:%d %d.\n",
|
---|
196 | address, endpoint, direction);
|
---|
197 | return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
|
---|
198 | endpoint, direction);
|
---|
199 | }
|
---|
200 | /*----------------------------------------------------------------------------*/
|
---|
201 | /** Schedule interrupt out transfer.
|
---|
202 | *
|
---|
203 | * The callback is supposed to be called once the transfer (on the wire) is
|
---|
204 | * complete regardless of the outcome.
|
---|
205 | * However, the callback could be called only when this function returns
|
---|
206 | * with success status (i.e. returns EOK).
|
---|
207 | *
|
---|
208 | * @param[in] fun Device function the action was invoked on.
|
---|
209 | * @param[in] target Target pipe (address and endpoint number) specification.
|
---|
210 | * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
|
---|
211 | * by the caller).
|
---|
212 | * @param[in] size Size of the @p data buffer in bytes.
|
---|
213 | * @param[in] callback Callback to be issued once the transfer is complete.
|
---|
214 | * @param[in] arg Pass-through argument to the callback.
|
---|
215 | * @return Error code.
|
---|
216 | */
|
---|
217 | static int interrupt_out(
|
---|
218 | ddf_fun_t *fun, usb_target_t target, void *data,
|
---|
219 | size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
220 | {
|
---|
221 | usb_transfer_batch_t *batch = NULL;
|
---|
222 | hc_t *hc = NULL;
|
---|
223 | int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
|
---|
224 | NULL, 0, NULL, callback, arg, "Interrupt OUT", &hc, &batch);
|
---|
225 | if (ret != EOK)
|
---|
226 | return ret;
|
---|
227 | batch_interrupt_out(batch);
|
---|
228 | ret = hc_schedule(hc, batch);
|
---|
229 | if (ret != EOK) {
|
---|
230 | batch_dispose(batch);
|
---|
231 | }
|
---|
232 | return ret;
|
---|
233 | }
|
---|
234 | /*----------------------------------------------------------------------------*/
|
---|
235 | /** Schedule interrupt in transfer.
|
---|
236 | *
|
---|
237 | * The callback is supposed to be called once the transfer (on the wire) is
|
---|
238 | * complete regardless of the outcome.
|
---|
239 | * However, the callback could be called only when this function returns
|
---|
240 | * with success status (i.e. returns EOK).
|
---|
241 | *
|
---|
242 | * @param[in] fun Device function the action was invoked on.
|
---|
243 | * @param[in] target Target pipe (address and endpoint number) specification.
|
---|
244 | * @param[in] data Buffer where to store the data (in USB endianess,
|
---|
245 | * allocated and deallocated by the caller).
|
---|
246 | * @param[in] size Size of the @p data buffer in bytes.
|
---|
247 | * @param[in] callback Callback to be issued once the transfer is complete.
|
---|
248 | * @param[in] arg Pass-through argument to the callback.
|
---|
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 | usb_transfer_batch_t *batch = NULL;
|
---|
256 | hc_t *hc = NULL;
|
---|
257 | int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
|
---|
258 | NULL, 0, callback, NULL, arg, "Interrupt IN", &hc, &batch);
|
---|
259 | if (ret != EOK)
|
---|
260 | return ret;
|
---|
261 | batch_interrupt_in(batch);
|
---|
262 | ret = hc_schedule(hc, batch);
|
---|
263 | if (ret != EOK) {
|
---|
264 | batch_dispose(batch);
|
---|
265 | }
|
---|
266 | return ret;
|
---|
267 | }
|
---|
268 | /*----------------------------------------------------------------------------*/
|
---|
269 | /** Schedule bulk out transfer.
|
---|
270 | *
|
---|
271 | * The callback is supposed to be called once the transfer (on the wire) is
|
---|
272 | * complete regardless of the outcome.
|
---|
273 | * However, the callback could be called only when this function returns
|
---|
274 | * with success status (i.e. returns EOK).
|
---|
275 | *
|
---|
276 | * @param[in] fun Device function the action was invoked on.
|
---|
277 | * @param[in] target Target pipe (address and endpoint number) specification.
|
---|
278 | * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
|
---|
279 | * by the caller).
|
---|
280 | * @param[in] size Size of the @p data buffer in bytes.
|
---|
281 | * @param[in] callback Callback to be issued once the transfer is complete.
|
---|
282 | * @param[in] arg Pass-through argument to the callback.
|
---|
283 | * @return Error code.
|
---|
284 | */
|
---|
285 | static int bulk_out(
|
---|
286 | ddf_fun_t *fun, usb_target_t target, void *data,
|
---|
287 | size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
288 | {
|
---|
289 | usb_transfer_batch_t *batch = NULL;
|
---|
290 | hc_t *hc = NULL;
|
---|
291 | int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
|
---|
292 | NULL, 0, NULL, callback, arg, "Bulk OUT", &hc, &batch);
|
---|
293 | if (ret != EOK)
|
---|
294 | return ret;
|
---|
295 | batch_bulk_out(batch);
|
---|
296 | ret = hc_schedule(hc, batch);
|
---|
297 | if (ret != EOK) {
|
---|
298 | batch_dispose(batch);
|
---|
299 | }
|
---|
300 | return ret;
|
---|
301 | }
|
---|
302 | /*----------------------------------------------------------------------------*/
|
---|
303 | /** Schedule bulk in transfer.
|
---|
304 | *
|
---|
305 | * The callback is supposed to be called once the transfer (on the wire) is
|
---|
306 | * complete regardless of the outcome.
|
---|
307 | * However, the callback could be called only when this function returns
|
---|
308 | * with success status (i.e. returns EOK).
|
---|
309 | *
|
---|
310 | * @param[in] fun Device function the action was invoked on.
|
---|
311 | * @param[in] target Target pipe (address and endpoint number) specification.
|
---|
312 | * @param[in] data Buffer where to store the data (in USB endianess,
|
---|
313 | * allocated and deallocated by the caller).
|
---|
314 | * @param[in] size Size of the @p data buffer in bytes.
|
---|
315 | * @param[in] callback Callback to be issued once the transfer is complete.
|
---|
316 | * @param[in] arg Pass-through argument to the callback.
|
---|
317 | * @return Error code.
|
---|
318 | */
|
---|
319 | static int bulk_in(
|
---|
320 | ddf_fun_t *fun, usb_target_t target, void *data,
|
---|
321 | size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
322 | {
|
---|
323 | usb_transfer_batch_t *batch = NULL;
|
---|
324 | hc_t *hc = NULL;
|
---|
325 | int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
|
---|
326 | NULL, 0, callback, NULL, arg, "Bulk IN", &hc, &batch);
|
---|
327 | if (ret != EOK)
|
---|
328 | return ret;
|
---|
329 | batch_bulk_in(batch);
|
---|
330 | ret = hc_schedule(hc, batch);
|
---|
331 | if (ret != EOK) {
|
---|
332 | batch_dispose(batch);
|
---|
333 | }
|
---|
334 | return ret;
|
---|
335 | }
|
---|
336 | /*----------------------------------------------------------------------------*/
|
---|
337 | /** Schedule control write transfer.
|
---|
338 | *
|
---|
339 | * The callback is supposed to be called once the transfer (on the wire) is
|
---|
340 | * complete regardless of the outcome.
|
---|
341 | * However, the callback could be called only when this function returns
|
---|
342 | * with success status (i.e. returns EOK).
|
---|
343 | *
|
---|
344 | * @param[in] fun Device function the action was invoked on.
|
---|
345 | * @param[in] target Target pipe (address and endpoint number) specification.
|
---|
346 | * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
|
---|
347 | * and deallocated by the caller).
|
---|
348 | * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
|
---|
349 | * @param[in] data_buffer Data buffer (in USB endianess, allocated and
|
---|
350 | * deallocated by the caller).
|
---|
351 | * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
|
---|
352 | * @param[in] callback Callback to be issued once the transfer is complete.
|
---|
353 | * @param[in] arg Pass-through argument to the callback.
|
---|
354 | * @return Error code.
|
---|
355 | */
|
---|
356 | static int control_write(
|
---|
357 | ddf_fun_t *fun, usb_target_t target,
|
---|
358 | void *setup_data, size_t setup_size, void *data, size_t size,
|
---|
359 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
360 | {
|
---|
361 | usb_transfer_batch_t *batch = NULL;
|
---|
362 | hc_t *hc = NULL;
|
---|
363 | int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
|
---|
364 | setup_data, setup_size, NULL, callback, arg, "Control WRITE",
|
---|
365 | &hc, &batch);
|
---|
366 | if (ret != EOK)
|
---|
367 | return ret;
|
---|
368 | usb_endpoint_manager_reset_if_need(&hc->ep_manager, target, setup_data);
|
---|
369 | batch_control_write(batch);
|
---|
370 | ret = hc_schedule(hc, batch);
|
---|
371 | if (ret != EOK) {
|
---|
372 | batch_dispose(batch);
|
---|
373 | }
|
---|
374 | return ret;
|
---|
375 | }
|
---|
376 | /*----------------------------------------------------------------------------*/
|
---|
377 | /** Schedule control read transfer.
|
---|
378 | *
|
---|
379 | * The callback is supposed to be called once the transfer (on the wire) is
|
---|
380 | * complete regardless of the outcome.
|
---|
381 | * However, the callback could be called only when this function returns
|
---|
382 | * with success status (i.e. returns EOK).
|
---|
383 | *
|
---|
384 | * @param[in] fun Device function the action was invoked on.
|
---|
385 | * @param[in] target Target pipe (address and endpoint number) specification.
|
---|
386 | * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
|
---|
387 | * and deallocated by the caller).
|
---|
388 | * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
|
---|
389 | * @param[in] data_buffer Buffer where to store the data (in USB endianess,
|
---|
390 | * allocated and deallocated by the caller).
|
---|
391 | * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
|
---|
392 | * @param[in] callback Callback to be issued once the transfer is complete.
|
---|
393 | * @param[in] arg Pass-through argument to the callback.
|
---|
394 | * @return Error code.
|
---|
395 | */
|
---|
396 | static int control_read(
|
---|
397 | ddf_fun_t *fun, usb_target_t target,
|
---|
398 | void *setup_data, size_t setup_size, void *data, size_t size,
|
---|
399 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
400 | {
|
---|
401 | usb_transfer_batch_t *batch = NULL;
|
---|
402 | hc_t *hc = NULL;
|
---|
403 | int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
|
---|
404 | setup_data, setup_size, callback, NULL, arg, "Control READ",
|
---|
405 | &hc, &batch);
|
---|
406 | if (ret != EOK)
|
---|
407 | return ret;
|
---|
408 | batch_control_read(batch);
|
---|
409 | ret = hc_schedule(hc, batch);
|
---|
410 | if (ret != EOK) {
|
---|
411 | batch_dispose(batch);
|
---|
412 | }
|
---|
413 | return ret;
|
---|
414 | }
|
---|
415 | /*----------------------------------------------------------------------------*/
|
---|
416 | usbhc_iface_t hc_iface = {
|
---|
417 | .request_address = request_address,
|
---|
418 | .bind_address = bind_address,
|
---|
419 | .release_address = release_address,
|
---|
420 |
|
---|
421 | .register_endpoint = register_endpoint,
|
---|
422 | .unregister_endpoint = unregister_endpoint,
|
---|
423 |
|
---|
424 | .interrupt_out = interrupt_out,
|
---|
425 | .interrupt_in = interrupt_in,
|
---|
426 |
|
---|
427 | .bulk_out = bulk_out,
|
---|
428 | .bulk_in = bulk_in,
|
---|
429 |
|
---|
430 | .control_write = control_write,
|
---|
431 | .control_read = control_read,
|
---|
432 | };
|
---|
433 | /**
|
---|
434 | * @}
|
---|
435 | */
|
---|