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

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

usbhc: Export IPC wrapper instead of IPC call numbers.

Hide IPC protocol in one file, instead of multiple client implementations.
Rename 'find_by_address' ⇒ 'get_handle'

  • Property mode set to 100644
File size: 8.9 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_find_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_create(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_destroy(batch);
87
88 return ret;
89}
90/*----------------------------------------------------------------------------*/
91static int register_helper(endpoint_t *ep, void *arg)
92{
93 hcd_t *hcd = arg;
94 assert(ep);
95 assert(hcd);
96 if (hcd->ep_add_hook)
97 return hcd->ep_add_hook(hcd, ep);
98 return EOK;
99}
100/*----------------------------------------------------------------------------*/
101static void unregister_helper(endpoint_t *ep, void *arg)
102{
103 hcd_t *hcd = arg;
104 assert(ep);
105 assert(hcd);
106 if (hcd->ep_remove_hook)
107 hcd->ep_remove_hook(hcd, ep);
108}
109/*----------------------------------------------------------------------------*/
110static void unregister_helper_warn(endpoint_t *ep, void *arg)
111{
112 hcd_t *hcd = arg;
113 assert(ep);
114 assert(hcd);
115 usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
116 ep->address, ep->endpoint, usb_str_direction(ep->direction));
117 if (hcd->ep_remove_hook)
118 hcd->ep_remove_hook(hcd, ep);
119}
120/*----------------------------------------------------------------------------*/
121/** Request address interface function
122 *
123 * @param[in] fun DDF function that was called.
124 * @param[in] speed Speed to associate with the new default address.
125 * @param[out] address Place to write a new address.
126 * @return Error code.
127 */
128static int request_address(
129 ddf_fun_t *fun, usb_address_t *address, bool strict, usb_speed_t speed)
130{
131 assert(fun);
132 hcd_t *hcd = fun_to_hcd(fun);
133 assert(hcd);
134 assert(address);
135
136 usb_log_debug("Address request: speed: %s, address: %d, strict: %s.\n",
137 usb_str_speed(speed), *address, strict ? "YES" : "NO");
138 return usb_device_manager_request_address(
139 &hcd->dev_manager, address, strict, speed);
140}
141/*----------------------------------------------------------------------------*/
142/** Bind address interface function
143 *
144 * @param[in] fun DDF function that was called.
145 * @param[in] address Address of the device
146 * @param[in] handle Devman handle of the device driver.
147 * @return Error code.
148 */
149static int bind_address(
150 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
151{
152 assert(fun);
153 hcd_t *hcd = fun_to_hcd(fun);
154 assert(hcd);
155
156 usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
157 return usb_device_manager_bind_address(
158 &hcd->dev_manager, address, handle);
159}
160/*----------------------------------------------------------------------------*/
161/** Find device handle by address interface function.
162 *
163 * @param[in] fun DDF function that was called.
164 * @param[in] address Address in question.
165 * @param[out] handle Where to store device handle if found.
166 * @return Error code.
167 */
168static int find_by_address(ddf_fun_t *fun, usb_address_t address,
169 devman_handle_t *handle)
170{
171 assert(fun);
172 hcd_t *hcd = fun_to_hcd(fun);
173 assert(hcd);
174 return usb_device_manager_get_info_by_address(
175 &hcd->dev_manager, address, handle, NULL);
176}
177/*----------------------------------------------------------------------------*/
178/** Release address interface function
179 *
180 * @param[in] fun DDF function that was called.
181 * @param[in] address USB address to be released.
182 * @return Error code.
183 */
184static int release_address(ddf_fun_t *fun, usb_address_t address)
185{
186 assert(fun);
187 hcd_t *hcd = fun_to_hcd(fun);
188 assert(hcd);
189 usb_log_debug("Address release %d.\n", address);
190 usb_device_manager_release_address(&hcd->dev_manager, address);
191 usb_endpoint_manager_remove_address(&hcd->ep_manager, address,
192 unregister_helper_warn, hcd);
193 return EOK;
194}
195/*----------------------------------------------------------------------------*/
196static int register_endpoint(
197 ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
198 usb_transfer_type_t transfer_type, usb_direction_t direction,
199 size_t max_packet_size, unsigned int interval)
200{
201 assert(fun);
202 hcd_t *hcd = fun_to_hcd(fun);
203 assert(hcd);
204 const size_t size = max_packet_size;
205 usb_speed_t speed = USB_SPEED_MAX;
206 const int ret = usb_device_manager_get_info_by_address(
207 &hcd->dev_manager, address, NULL, &speed);
208 if (ret != EOK) {
209 return ret;
210 }
211
212 usb_log_debug("Register endpoint %d:%d %s-%s %s %zuB %ums.\n",
213 address, endpoint, usb_str_transfer_type(transfer_type),
214 usb_str_direction(direction), usb_str_speed(speed),
215 max_packet_size, interval);
216
217 return usb_endpoint_manager_add_ep(&hcd->ep_manager, address, endpoint,
218 direction, transfer_type, speed, max_packet_size, size,
219 register_helper, hcd);
220}
221/*----------------------------------------------------------------------------*/
222static int unregister_endpoint(
223 ddf_fun_t *fun, usb_address_t address,
224 usb_endpoint_t endpoint, usb_direction_t direction)
225{
226 assert(fun);
227 hcd_t *hcd = fun_to_hcd(fun);
228 assert(hcd);
229 usb_log_debug("Unregister endpoint %d:%d %s.\n",
230 address, endpoint, usb_str_direction(direction));
231 return usb_endpoint_manager_remove_ep(&hcd->ep_manager, address,
232 endpoint, direction, unregister_helper, hcd);
233}
234/*----------------------------------------------------------------------------*/
235static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
236 uint8_t *data, size_t size, usbhc_iface_transfer_in_callback_t callback,
237 void *arg)
238{
239 return send_batch(fun, target, USB_DIRECTION_IN, data, size,
240 setup_data, callback, NULL, arg, "READ");
241}
242/*----------------------------------------------------------------------------*/
243static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
244 const uint8_t *data, size_t size,
245 usbhc_iface_transfer_out_callback_t callback, void *arg)
246{
247 return send_batch(fun, target, USB_DIRECTION_OUT, (uint8_t*)data, size,
248 setup_data, NULL, callback, arg, "WRITE");
249}
250/*----------------------------------------------------------------------------*/
251usbhc_iface_t hcd_iface = {
252 .request_address = request_address,
253 .bind_address = bind_address,
254 .get_handle = find_by_address,
255 .release_address = release_address,
256
257 .register_endpoint = register_endpoint,
258 .unregister_endpoint = unregister_endpoint,
259
260 .read = usb_read,
261 .write = usb_write,
262};
263/**
264 * @}
265 */
Note: See TracBrowser for help on using the repository browser.