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 | *
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <usb_iface.h>
|
---|
39 | #include <usb/debug.h>
|
---|
40 |
|
---|
41 | #include <usb/host/hcd.h>
|
---|
42 |
|
---|
43 | /** Initialize hcd_t structure.
|
---|
44 | * Initializes device and endpoint managers. Sets data and hook pointer to NULL.
|
---|
45 | *
|
---|
46 | * @param hcd hcd_t structure to initialize, non-null.
|
---|
47 | * @param max_speed Maximum supported USB speed (full, high).
|
---|
48 | * @param bandwidth Available bandwidth, passed to endpoint manager.
|
---|
49 | * @param bw_count Bandwidth compute function, passed to endpoint manager.
|
---|
50 | */
|
---|
51 | void hcd_init(hcd_t *hcd, usb_speed_t max_speed, size_t bandwidth,
|
---|
52 | bw_count_func_t bw_count)
|
---|
53 | {
|
---|
54 | assert(hcd);
|
---|
55 | usb_device_manager_init(&hcd->dev_manager, max_speed);
|
---|
56 | usb_endpoint_manager_init(&hcd->ep_manager, bandwidth, bw_count);
|
---|
57 | list_initialize(&hcd->devices);
|
---|
58 |
|
---|
59 | hcd->private_data = NULL;
|
---|
60 | hcd->schedule = NULL;
|
---|
61 | hcd->ep_add_hook = NULL;
|
---|
62 | hcd->ep_remove_hook = NULL;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** Prepare generic usb_transfer_batch and schedule it.
|
---|
66 | * @param hcd Host controller driver.
|
---|
67 | * @param fun DDF fun
|
---|
68 | * @param target address and endpoint number.
|
---|
69 | * @param setup_data Data to use in setup stage (Control communication type)
|
---|
70 | * @param in Callback for device to host communication.
|
---|
71 | * @param out Callback for host to device communication.
|
---|
72 | * @param arg Callback parameter.
|
---|
73 | * @param name Communication identifier (for nicer output).
|
---|
74 | * @return Error code.
|
---|
75 | */
|
---|
76 | int hcd_send_batch(
|
---|
77 | hcd_t *hcd, ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
|
---|
78 | void *data, size_t size, uint64_t setup_data,
|
---|
79 | usbhc_iface_transfer_in_callback_t in,
|
---|
80 | usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
|
---|
81 | {
|
---|
82 | assert(hcd);
|
---|
83 |
|
---|
84 | endpoint_t *ep = usb_endpoint_manager_find_ep(&hcd->ep_manager,
|
---|
85 | target.address, target.endpoint, direction);
|
---|
86 | if (ep == NULL) {
|
---|
87 | usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
|
---|
88 | target.address, target.endpoint, name);
|
---|
89 | return ENOENT;
|
---|
90 | }
|
---|
91 |
|
---|
92 | usb_log_debug2("%s %d:%d %zu(%zu).\n",
|
---|
93 | name, target.address, target.endpoint, size, ep->max_packet_size);
|
---|
94 |
|
---|
95 | const size_t bw = bandwidth_count_usb11(
|
---|
96 | ep->speed, ep->transfer_type, size, ep->max_packet_size);
|
---|
97 | /* Check if we have enough bandwidth reserved */
|
---|
98 | if (ep->bandwidth < bw) {
|
---|
99 | usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
|
---|
100 | "but only %zu is reserved.\n",
|
---|
101 | ep->address, ep->endpoint, name, bw, ep->bandwidth);
|
---|
102 | return ENOSPC;
|
---|
103 | }
|
---|
104 | if (!hcd->schedule) {
|
---|
105 | usb_log_error("HCD does not implement scheduler.\n");
|
---|
106 | return ENOTSUP;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /* No private data and no private data dtor */
|
---|
110 | usb_transfer_batch_t *batch =
|
---|
111 | usb_transfer_batch_create(ep, data, size, setup_data,
|
---|
112 | in, out, arg, fun, NULL, NULL);
|
---|
113 | if (!batch) {
|
---|
114 | return ENOMEM;
|
---|
115 | }
|
---|
116 |
|
---|
117 | const int ret = hcd->schedule(hcd, batch);
|
---|
118 | if (ret != EOK)
|
---|
119 | usb_transfer_batch_destroy(batch);
|
---|
120 |
|
---|
121 | return ret;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * @}
|
---|
126 | */
|
---|