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

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

libusbhost: Remove fun_to_hcd helper.

Use dev_to_hcd instead.

  • Property mode set to 100644
File size: 8.8 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
29/** @addtogroup libusbhost
30 * @{
31 */
32/** @file
33 * @brief HCD DDF interface implementation
34 */
35
36#include <ddf/driver.h>
37#include <errno.h>
38
39#include <usb/debug.h>
40#include <usb/host/endpoint.h>
41#include <usb/host/hcd.h>
42#include "ddf_helpers.h"
43
44/** Calls ep_add_hook upon endpoint registration.
45 * @param ep Endpoint to be registered.
46 * @param arg hcd_t in disguise.
47 * @return Error code.
48 */
49static int register_helper(endpoint_t *ep, void *arg)
50{
51 hcd_t *hcd = arg;
52 assert(ep);
53 assert(hcd);
54 if (hcd->ep_add_hook)
55 return hcd->ep_add_hook(hcd, ep);
56 return EOK;
57}
58
59/** Calls ep_remove_hook upon endpoint removal.
60 * @param ep Endpoint to be unregistered.
61 * @param arg hcd_t in disguise.
62 */
63static void unregister_helper(endpoint_t *ep, void *arg)
64{
65 hcd_t *hcd = arg;
66 assert(ep);
67 assert(hcd);
68 if (hcd->ep_remove_hook)
69 hcd->ep_remove_hook(hcd, ep);
70}
71
72/** Calls ep_remove_hook upon endpoint removal. Prints warning.
73 * @param ep Endpoint to be unregistered.
74 * @param arg hcd_t in disguise.
75 */
76static void unregister_helper_warn(endpoint_t *ep, void *arg)
77{
78 hcd_t *hcd = arg;
79 assert(ep);
80 assert(hcd);
81 usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
82 ep->address, ep->endpoint, usb_str_direction(ep->direction));
83 if (hcd->ep_remove_hook)
84 hcd->ep_remove_hook(hcd, ep);
85}
86
87/** Request address interface function.
88 *
89 * @param[in] fun DDF function that was called.
90 * @param[in] address Pointer to preferred USB address.
91 * @param[out] address Place to write a new address.
92 * @param[in] strict Fail if the preferred address is not available.
93 * @param[in] speed Speed to associate with the new default address.
94 * @return Error code.
95 */
96static int request_address(
97 ddf_fun_t *fun, usb_address_t *address, bool strict, usb_speed_t speed)
98{
99 assert(fun);
100 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
101 assert(hcd);
102 assert(address);
103
104 usb_log_debug("Address request: speed: %s, address: %d, strict: %s.\n",
105 usb_str_speed(speed), *address, strict ? "YES" : "NO");
106 return usb_device_manager_request_address(
107 &hcd->dev_manager, address, strict, speed);
108}
109
110/** Bind address interface function.
111 *
112 * @param[in] fun DDF function that was called.
113 * @param[in] address Address of the device
114 * @param[in] handle Devman handle of the device driver.
115 * @return Error code.
116 */
117static int bind_address(
118 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
119{
120 assert(fun);
121 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
122 assert(hcd);
123
124 usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
125 return usb_device_manager_bind_address(
126 &hcd->dev_manager, address, handle);
127}
128
129/** Find device handle by address interface function.
130 *
131 * @param[in] fun DDF function that was called.
132 * @param[in] address Address in question.
133 * @param[out] handle Where to store device handle if found.
134 * @return Error code.
135 */
136static int find_by_address(ddf_fun_t *fun, usb_address_t address,
137 devman_handle_t *handle)
138{
139 assert(fun);
140 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
141 assert(hcd);
142 return usb_device_manager_get_info_by_address(
143 &hcd->dev_manager, address, handle, NULL);
144}
145
146/** Release address interface function.
147 *
148 * @param[in] fun DDF function that was called.
149 * @param[in] address USB address to be released.
150 * @return Error code.
151 */
152static int release_address(ddf_fun_t *fun, usb_address_t address)
153{
154 assert(fun);
155 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
156 assert(hcd);
157 usb_log_debug("Address release %d.\n", address);
158 usb_endpoint_manager_remove_address(&hcd->ep_manager, address,
159 unregister_helper_warn, hcd);
160 usb_device_manager_release_address(&hcd->dev_manager, address);
161 return EOK;
162}
163
164/** Register endpoint interface function.
165 * @param fun DDF function.
166 * @param address USB address of the device.
167 * @param endpoint USB endpoint number to be registered.
168 * @param transfer_type Endpoint's transfer type.
169 * @param direction USB communication direction the endpoint is capable of.
170 * @param max_packet_size Maximu size of packets the endpoint accepts.
171 * @param interval Preferred timeout between communication.
172 * @return Error code.
173 */
174static int register_endpoint(
175 ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
176 usb_transfer_type_t transfer_type, usb_direction_t direction,
177 size_t max_packet_size, unsigned interval)
178{
179 assert(fun);
180 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
181 assert(hcd);
182 const size_t size = max_packet_size;
183 usb_speed_t speed = USB_SPEED_MAX;
184 const int ret = usb_device_manager_get_info_by_address(
185 &hcd->dev_manager, address, NULL, &speed);
186 if (ret != EOK) {
187 return ret;
188 }
189
190 usb_log_debug("Register endpoint %d:%d %s-%s %s %zuB %ums.\n",
191 address, endpoint, usb_str_transfer_type(transfer_type),
192 usb_str_direction(direction), usb_str_speed(speed),
193 max_packet_size, interval);
194
195 return usb_endpoint_manager_add_ep(&hcd->ep_manager, address, endpoint,
196 direction, transfer_type, speed, max_packet_size, size,
197 register_helper, hcd);
198}
199
200/** Unregister endpoint interface function.
201 * @param fun DDF function.
202 * @param address USB address of the endpoint.
203 * @param endpoint USB endpoint number.
204 * @param direction Communication direction of the enpdoint to unregister.
205 * @return Error code.
206 */
207static int unregister_endpoint(
208 ddf_fun_t *fun, usb_address_t address,
209 usb_endpoint_t endpoint, usb_direction_t direction)
210{
211 assert(fun);
212 hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
213 assert(hcd);
214 usb_log_debug("Unregister endpoint %d:%d %s.\n",
215 address, endpoint, usb_str_direction(direction));
216 return usb_endpoint_manager_remove_ep(&hcd->ep_manager, address,
217 endpoint, direction, unregister_helper, hcd);
218}
219
220/** Inbound communication interface function.
221 * @param fun DDF function.
222 * @param target Communication target.
223 * @param setup_data Data to use in setup stage (control transfers).
224 * @param data Pointer to data buffer.
225 * @param size Size of the data buffer.
226 * @param callback Function to call on communication end.
227 * @param arg Argument passed to the callback function.
228 * @return Error code.
229 */
230static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
231 uint8_t *data, size_t size, usbhc_iface_transfer_in_callback_t callback,
232 void *arg)
233{
234 return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)), target, USB_DIRECTION_IN,
235 data, size, setup_data, callback, NULL, arg, "READ");
236}
237
238/** Outbound communication interface function.
239 * @param fun DDF function.
240 * @param target Communication target.
241 * @param setup_data Data to use in setup stage (control transfers).
242 * @param data Pointer to data buffer.
243 * @param size Size of the data buffer.
244 * @param callback Function to call on communication end.
245 * @param arg Argument passed to the callback function.
246 * @return Error code.
247 */
248static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
249 const uint8_t *data, size_t size,
250 usbhc_iface_transfer_out_callback_t callback, void *arg)
251{
252 return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)),
253 target, USB_DIRECTION_OUT, (uint8_t*)data, size, setup_data, NULL,
254 callback, arg, "WRITE");
255}
256
257/** usbhc Interface implementation using hcd_t from libusbhost library. */
258usbhc_iface_t hcd_iface = {
259 .request_address = request_address,
260 .bind_address = bind_address,
261 .get_handle = find_by_address,
262 .release_address = release_address,
263
264 .register_endpoint = register_endpoint,
265 .unregister_endpoint = unregister_endpoint,
266
267 .read = usb_read,
268 .write = usb_write,
269};
270
271/**
272 * @}
273 */
Note: See TracBrowser for help on using the repository browser.