source: mainline/uspace/lib/usbhost/src/iface.c@ 7265558

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7265558 was 83c3123, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

libusbhost: Store bandwidth in endpoint structure.

Remove redundant node_t tructure and use endpoint_t directly.

  • Property mode set to 100644
File size: 8.4 KB
Line 
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
41static inline int send_batch(
42 ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
43 void *data, size_t size, uint64_t setup_data,
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 endpoint_t *ep = usb_endpoint_manager_get_ep(&hcd->ep_manager,
52 target.address, target.endpoint, direction);
53 if (ep == NULL) {
54 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
55 target.address, target.endpoint, name);
56 return ENOENT;
57 }
58
59 usb_log_debug2("%s %d:%d %zu(%zu).\n",
60 name, target.address, target.endpoint, size, ep->max_packet_size);
61
62 const size_t bw = bandwidth_count_usb11(
63 ep->speed, ep->transfer_type, size, ep->max_packet_size);
64 /* Check if we have enough bandwidth reserved */
65 if (ep->bandwidth < bw) {
66 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
67 "but only %zu is reserved.\n",
68 ep->address, ep->endpoint, name, bw, ep->bandwidth);
69 return ENOSPC;
70 }
71 if (!hcd->schedule) {
72 usb_log_error("HCD does not implement scheduler.\n");
73 return ENOTSUP;
74 }
75
76 /* No private data and no private data dtor */
77 usb_transfer_batch_t *batch =
78 usb_transfer_batch_get(ep, data, size, setup_data,
79 in, out, arg, fun, NULL, NULL);
80 if (!batch) {
81 return ENOMEM;
82 }
83
84 const int ret = hcd->schedule(hcd, batch);
85 if (ret != EOK)
86 usb_transfer_batch_dispose(batch);
87
88 return ret;
89}
90/*----------------------------------------------------------------------------*/
91/** Request address interface function
92 *
93 * @param[in] fun DDF function that was called.
94 * @param[in] speed Speed to associate with the new default address.
95 * @param[out] address Place to write a new address.
96 * @return Error code.
97 */
98static int request_address(
99 ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
100{
101 assert(fun);
102 hcd_t *hcd = fun_to_hcd(fun);
103 assert(hcd);
104 assert(address);
105
106 usb_log_debug("Address request speed: %s.\n", usb_str_speed(speed));
107 *address =
108 usb_device_manager_get_free_address(&hcd->dev_manager, speed);
109 usb_log_debug("Address request with result: %d.\n", *address);
110 if (*address <= 0)
111 return *address;
112 return EOK;
113}
114/*----------------------------------------------------------------------------*/
115/** Bind address interface function
116 *
117 * @param[in] fun DDF function that was called.
118 * @param[in] address Address of the device
119 * @param[in] handle Devman handle of the device driver.
120 * @return Error code.
121 */
122static int bind_address(
123 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
124{
125 assert(fun);
126 hcd_t *hcd = fun_to_hcd(fun);
127 assert(hcd);
128
129 usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
130 usb_device_manager_bind(&hcd->dev_manager, address, handle);
131 return EOK;
132}
133/*----------------------------------------------------------------------------*/
134/** Find device handle by address interface function.
135 *
136 * @param[in] fun DDF function that was called.
137 * @param[in] address Address in question.
138 * @param[out] handle Where to store device handle if found.
139 * @return Error code.
140 */
141static int find_by_address(ddf_fun_t *fun, usb_address_t address,
142 devman_handle_t *handle)
143{
144 assert(fun);
145 hcd_t *hcd = fun_to_hcd(fun);
146 assert(hcd);
147 return usb_device_manager_get_info_by_address(
148 &hcd->dev_manager, address, handle, NULL);
149}
150/*----------------------------------------------------------------------------*/
151/** Release address interface function
152 *
153 * @param[in] fun DDF function that was called.
154 * @param[in] address USB address to be released.
155 * @return Error code.
156 */
157static int release_address(ddf_fun_t *fun, usb_address_t address)
158{
159 assert(fun);
160 hcd_t *hcd = fun_to_hcd(fun);
161 assert(hcd);
162 usb_log_debug("Address release %d.\n", address);
163 usb_device_manager_release(&hcd->dev_manager, address);
164 return EOK;
165}
166/*----------------------------------------------------------------------------*/
167static int register_endpoint(
168 ddf_fun_t *fun, usb_address_t address, usb_speed_t ep_speed,
169 usb_endpoint_t endpoint,
170 usb_transfer_type_t transfer_type, usb_direction_t direction,
171 size_t max_packet_size, unsigned int interval)
172{
173 assert(fun);
174 hcd_t *hcd = fun_to_hcd(fun);
175 assert(hcd);
176 const size_t size = max_packet_size;
177 /* Default address is not bound or registered,
178 * thus it does not provide speed info. */
179 usb_speed_t speed = ep_speed;
180 /* NOTE The function will return EINVAL and won't
181 * touch speed variable for default address */
182 usb_device_manager_get_info_by_address(
183 &hcd->dev_manager, address, NULL, &speed);
184
185 usb_log_debug("Register endpoint %d:%d %s-%s %s %zuB %ums.\n",
186 address, endpoint, usb_str_transfer_type(transfer_type),
187 usb_str_direction(direction), usb_str_speed(speed),
188 max_packet_size, interval);
189
190 endpoint_t *ep =
191 endpoint_create(address, endpoint, direction, transfer_type,
192 speed, max_packet_size, 0);
193 if (!ep)
194 return ENOMEM;
195
196 if (hcd->ep_add_hook) {
197 const int ret = hcd->ep_add_hook(hcd, ep);
198 if (ret != EOK) {
199 endpoint_destroy(ep);
200 return ret;
201 }
202 }
203
204 const int ret =
205 usb_endpoint_manager_register_ep(&hcd->ep_manager, ep, size);
206 if (ret != EOK) {
207 endpoint_destroy(ep);
208 }
209 return ret;
210}
211/*----------------------------------------------------------------------------*/
212static int unregister_endpoint(
213 ddf_fun_t *fun, usb_address_t address,
214 usb_endpoint_t endpoint, usb_direction_t direction)
215{
216 assert(fun);
217 hcd_t *hcd = fun_to_hcd(fun);
218 assert(hcd);
219 usb_log_debug("Unregister endpoint %d:%d %s.\n",
220 address, endpoint, usb_str_direction(direction));
221 return usb_endpoint_manager_unregister_ep(&hcd->ep_manager, address,
222 endpoint, direction);
223}
224/*----------------------------------------------------------------------------*/
225static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
226 uint8_t *data, size_t size, usbhc_iface_transfer_in_callback_t callback,
227 void *arg)
228{
229 return send_batch(fun, target, USB_DIRECTION_IN, data, size,
230 setup_data, callback, NULL, arg, "READ");
231}
232/*----------------------------------------------------------------------------*/
233static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
234 const uint8_t *data, size_t size,
235 usbhc_iface_transfer_out_callback_t callback, void *arg)
236{
237 return send_batch(fun, target, USB_DIRECTION_OUT, (uint8_t*)data, size,
238 setup_data, NULL, callback, arg, "WRITE");
239}
240/*----------------------------------------------------------------------------*/
241usbhc_iface_t hcd_iface = {
242 .request_address = request_address,
243 .bind_address = bind_address,
244 .find_by_address = find_by_address,
245 .release_address = release_address,
246
247 .register_endpoint = register_endpoint,
248 .unregister_endpoint = unregister_endpoint,
249
250 .read = usb_read,
251 .write = usb_write,
252};
253/**
254 * @}
255 */
Note: See TracBrowser for help on using the repository browser.