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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b272c67a was 77ad86c, checked in by Martin Decky <martin@…>, 13 years ago

cstyle (no change in functionality)

  • Property mode set to 100644
File size: 10.5 KB
RevLine 
[45d105a]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 */
[77ad86c]28
[45d105a]29/** @addtogroup libusbhost
30 * @{
31 */
32/** @file
33 * @brief HCD DDF interface implementation
34 */
[77ad86c]35
[45d105a]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
[cbd568b]43/** Prepare generic usb_transfer_batch and schedule it.
44 * @param fun DDF fun
45 * @param target address and endpoint number.
46 * @param setup_data Data to use in setup stage (Control communication type)
47 * @param in Callback for device to host communication.
48 * @param out Callback for host to device communication.
49 * @param arg Callback parameter.
50 * @param name Communication identifier (for nicer output).
51 * @return Error code.
52 */
[45d105a]53static inline int send_batch(
54 ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
[a837544]55 void *data, size_t size, uint64_t setup_data,
[45d105a]56 usbhc_iface_transfer_in_callback_t in,
57 usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
58{
59 assert(fun);
60 hcd_t *hcd = fun_to_hcd(fun);
61 assert(hcd);
62
[48ae3ef]63 endpoint_t *ep = usb_endpoint_manager_find_ep(&hcd->ep_manager,
[83c3123]64 target.address, target.endpoint, direction);
[45d105a]65 if (ep == NULL) {
66 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
67 target.address, target.endpoint, name);
68 return ENOENT;
69 }
70
71 usb_log_debug2("%s %d:%d %zu(%zu).\n",
72 name, target.address, target.endpoint, size, ep->max_packet_size);
73
74 const size_t bw = bandwidth_count_usb11(
75 ep->speed, ep->transfer_type, size, ep->max_packet_size);
[83c3123]76 /* Check if we have enough bandwidth reserved */
77 if (ep->bandwidth < bw) {
[45d105a]78 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
79 "but only %zu is reserved.\n",
[83c3123]80 ep->address, ep->endpoint, name, bw, ep->bandwidth);
[45d105a]81 return ENOSPC;
82 }
[70fb822]83 if (!hcd->schedule) {
84 usb_log_error("HCD does not implement scheduler.\n");
85 return ENOTSUP;
[45d105a]86 }
87
[70fb822]88 /* No private data and no private data dtor */
89 usb_transfer_batch_t *batch =
[549ff23]90 usb_transfer_batch_create(ep, data, size, setup_data,
[4e9ecf4]91 in, out, arg, fun, NULL, NULL);
[70fb822]92 if (!batch) {
93 return ENOMEM;
[45d105a]94 }
[70fb822]95
[83c3123]96 const int ret = hcd->schedule(hcd, batch);
[45d105a]97 if (ret != EOK)
[549ff23]98 usb_transfer_batch_destroy(batch);
[45d105a]99
100 return ret;
101}
[77ad86c]102
[cbd568b]103/** Calls ep_add_hook upon endpoint registration.
104 * @param ep Endpoint to be registered.
105 * @param arg hcd_t in disguise.
106 * @return Error code.
107 */
[6b6fc232]108static int register_helper(endpoint_t *ep, void *arg)
109{
110 hcd_t *hcd = arg;
111 assert(ep);
112 assert(hcd);
113 if (hcd->ep_add_hook)
114 return hcd->ep_add_hook(hcd, ep);
115 return EOK;
116}
[77ad86c]117
[cbd568b]118/** Calls ep_remove_hook upon endpoint removal.
119 * @param ep Endpoint to be unregistered.
120 * @param arg hcd_t in disguise.
121 */
[6b6fc232]122static void unregister_helper(endpoint_t *ep, void *arg)
123{
124 hcd_t *hcd = arg;
125 assert(ep);
126 assert(hcd);
127 if (hcd->ep_remove_hook)
128 hcd->ep_remove_hook(hcd, ep);
129}
[77ad86c]130
[cbd568b]131/** Calls ep_remove_hook upon endpoint removal. Prints warning.
132 * @param ep Endpoint to be unregistered.
133 * @param arg hcd_t in disguise.
134 */
[6b6fc232]135static void unregister_helper_warn(endpoint_t *ep, void *arg)
136{
137 hcd_t *hcd = arg;
138 assert(ep);
139 assert(hcd);
140 usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
141 ep->address, ep->endpoint, usb_str_direction(ep->direction));
142 if (hcd->ep_remove_hook)
143 hcd->ep_remove_hook(hcd, ep);
144}
[77ad86c]145
[cbd568b]146/** Request address interface function.
[45d105a]147 *
148 * @param[in] fun DDF function that was called.
[cbd568b]149 * @param[in] address Pointer to preferred USB address.
[45d105a]150 * @param[out] address Place to write a new address.
[cbd568b]151 * @param[in] strict Fail if the preferred address is not available.
152 * @param[in] speed Speed to associate with the new default address.
[45d105a]153 * @return Error code.
154 */
155static int request_address(
[67f55e7b]156 ddf_fun_t *fun, usb_address_t *address, bool strict, usb_speed_t speed)
[45d105a]157{
158 assert(fun);
159 hcd_t *hcd = fun_to_hcd(fun);
160 assert(hcd);
161 assert(address);
162
[f37eb84]163 usb_log_debug("Address request: speed: %s, address: %d, strict: %s.\n",
164 usb_str_speed(speed), *address, strict ? "YES" : "NO");
[0cd8089]165 return usb_device_manager_request_address(
[67f55e7b]166 &hcd->dev_manager, address, strict, speed);
[45d105a]167}
[77ad86c]168
[cbd568b]169/** Bind address interface function.
[45d105a]170 *
171 * @param[in] fun DDF function that was called.
172 * @param[in] address Address of the device
173 * @param[in] handle Devman handle of the device driver.
174 * @return Error code.
175 */
176static int bind_address(
[cbd568b]177 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
[45d105a]178{
179 assert(fun);
180 hcd_t *hcd = fun_to_hcd(fun);
181 assert(hcd);
182
183 usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
[0cd8089]184 return usb_device_manager_bind_address(
185 &hcd->dev_manager, address, handle);
[45d105a]186}
[77ad86c]187
[45d105a]188/** Find device handle by address interface function.
189 *
190 * @param[in] fun DDF function that was called.
191 * @param[in] address Address in question.
192 * @param[out] handle Where to store device handle if found.
193 * @return Error code.
194 */
195static int find_by_address(ddf_fun_t *fun, usb_address_t address,
196 devman_handle_t *handle)
197{
198 assert(fun);
199 hcd_t *hcd = fun_to_hcd(fun);
200 assert(hcd);
[4267908]201 return usb_device_manager_get_info_by_address(
202 &hcd->dev_manager, address, handle, NULL);
[45d105a]203}
[77ad86c]204
[cbd568b]205/** Release address interface function.
[45d105a]206 *
207 * @param[in] fun DDF function that was called.
208 * @param[in] address USB address to be released.
209 * @return Error code.
210 */
211static int release_address(ddf_fun_t *fun, usb_address_t address)
212{
213 assert(fun);
214 hcd_t *hcd = fun_to_hcd(fun);
215 assert(hcd);
216 usb_log_debug("Address release %d.\n", address);
[0cd8089]217 usb_device_manager_release_address(&hcd->dev_manager, address);
[6b6fc232]218 usb_endpoint_manager_remove_address(&hcd->ep_manager, address,
219 unregister_helper_warn, hcd);
[45d105a]220 return EOK;
221}
[77ad86c]222
[cbd568b]223/** Register endpoint interface function.
224 * @param fun DDF function.
225 * @param address USB address of the device.
226 * @param endpoint USB endpoint number to be registered.
227 * @param transfer_type Endpoint's transfer type.
228 * @param direction USB communication direction the endpoint is capable of.
229 * @param max_packet_size Maximu size of packets the endpoint accepts.
230 * @param interval Preferred timeout between communication.
231 * @return Error code.
232 */
[45d105a]233static int register_endpoint(
[27736cf]234 ddf_fun_t *fun, usb_address_t address, usb_endpoint_t endpoint,
[45d105a]235 usb_transfer_type_t transfer_type, usb_direction_t direction,
[cbd568b]236 size_t max_packet_size, unsigned interval)
[45d105a]237{
238 assert(fun);
239 hcd_t *hcd = fun_to_hcd(fun);
240 assert(hcd);
241 const size_t size = max_packet_size;
[1b17e37]242 usb_speed_t speed = USB_SPEED_MAX;
243 const int ret = usb_device_manager_get_info_by_address(
[4267908]244 &hcd->dev_manager, address, NULL, &speed);
[1b17e37]245 if (ret != EOK) {
246 return ret;
247 }
[0815000]248
[45d105a]249 usb_log_debug("Register endpoint %d:%d %s-%s %s %zuB %ums.\n",
250 address, endpoint, usb_str_transfer_type(transfer_type),
251 usb_str_direction(direction), usb_str_speed(speed),
252 max_packet_size, interval);
253
[48ae3ef]254 return usb_endpoint_manager_add_ep(&hcd->ep_manager, address, endpoint,
255 direction, transfer_type, speed, max_packet_size, size,
256 register_helper, hcd);
[45d105a]257}
[77ad86c]258
[cbd568b]259/** Unregister endpoint interface function.
260 * @param fun DDF function.
261 * @param address USB address of the endpoint.
262 * @param endpoint USB endpoint number.
263 * @param direction Communication direction of the enpdoint to unregister.
264 * @return Error code.
265 */
[45d105a]266static int unregister_endpoint(
267 ddf_fun_t *fun, usb_address_t address,
268 usb_endpoint_t endpoint, usb_direction_t direction)
269{
270 assert(fun);
271 hcd_t *hcd = fun_to_hcd(fun);
272 assert(hcd);
273 usb_log_debug("Unregister endpoint %d:%d %s.\n",
274 address, endpoint, usb_str_direction(direction));
[48ae3ef]275 return usb_endpoint_manager_remove_ep(&hcd->ep_manager, address,
276 endpoint, direction, unregister_helper, hcd);
[45d105a]277}
[77ad86c]278
[cbd568b]279/** Inbound communication interface function.
280 * @param fun DDF function.
281 * @param target Communication target.
282 * @param setup_data Data to use in setup stage (control transfers).
283 * @param data Pointer to data buffer.
284 * @param size Size of the data buffer.
285 * @param callback Function to call on communication end.
286 * @param arg Argument passed to the callback function.
287 * @return Error code.
288 */
[3822f7c9]289static int usb_read(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
290 uint8_t *data, size_t size, usbhc_iface_transfer_in_callback_t callback,
291 void *arg)
292{
293 return send_batch(fun, target, USB_DIRECTION_IN, data, size,
[a837544]294 setup_data, callback, NULL, arg, "READ");
[3822f7c9]295}
[77ad86c]296
[cbd568b]297/** Outbound communication interface function.
298 * @param fun DDF function.
299 * @param target Communication target.
300 * @param setup_data Data to use in setup stage (control transfers).
301 * @param data Pointer to data buffer.
302 * @param size Size of the data buffer.
303 * @param callback Function to call on communication end.
304 * @param arg Argument passed to the callback function.
305 * @return Error code.
306 */
[3822f7c9]307static int usb_write(ddf_fun_t *fun, usb_target_t target, uint64_t setup_data,
308 const uint8_t *data, size_t size,
309 usbhc_iface_transfer_out_callback_t callback, void *arg)
310{
311 return send_batch(fun, target, USB_DIRECTION_OUT, (uint8_t*)data, size,
[a837544]312 setup_data, NULL, callback, arg, "WRITE");
[45d105a]313}
[77ad86c]314
[cbd568b]315/** usbhc Interface implementation using hcd_t from libusbhost library. */
[45d105a]316usbhc_iface_t hcd_iface = {
317 .request_address = request_address,
318 .bind_address = bind_address,
[02fc5c4]319 .get_handle = find_by_address,
[45d105a]320 .release_address = release_address,
321
322 .register_endpoint = register_endpoint,
323 .unregister_endpoint = unregister_endpoint,
324
[3822f7c9]325 .read = usb_read,
326 .write = usb_write,
[45d105a]327};
[77ad86c]328
[45d105a]329/**
330 * @}
331 */
Note: See TracBrowser for help on using the repository browser.