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

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

libusbhost: Streamline new hcd arch. UHCI: follow changes in libusbhost

Add endpoint registration hook.
Remove batch private data dtor from hcd_t (it should be set during initialization)

  • Property mode set to 100644
File size: 12.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, void * setup_data, size_t setup_size,
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 int ret;
52
53 size_t res_bw;
54 endpoint_t *ep = usb_endpoint_manager_get_ep(&hcd->ep_manager,
55 target.address, target.endpoint, direction, &res_bw);
56 if (ep == NULL) {
57 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
58 target.address, target.endpoint, name);
59 return ENOENT;
60 }
61
62 usb_log_debug2("%s %d:%d %zu(%zu).\n",
63 name, target.address, target.endpoint, size, ep->max_packet_size);
64
65 const size_t bw = bandwidth_count_usb11(
66 ep->speed, ep->transfer_type, size, ep->max_packet_size);
67 if (res_bw < bw) {
68 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
69 "but only %zu is reserved.\n",
70 target.address, target.endpoint, name, bw, res_bw);
71 return ENOSPC;
72 }
73 usb_transfer_batch_t *batch = malloc(sizeof(usb_transfer_batch_t));
74 if (!batch) {
75 ret = ENOMEM;
76 goto out;
77
78 }
79
80 /* No private data and no private data_dtor, these should be set by
81 * batch_init_hook*/
82 usb_transfer_batch_init(batch, ep, data, NULL, size, setup_data,
83 setup_size, in, out, arg, fun, NULL, NULL);
84 if (hcd->batch_init_hook) {
85 ret = hcd->batch_init_hook(batch);
86 if (ret != EOK)
87 goto out;
88 } else {
89 usb_log_warning("Missing batch_private_data constructor!\n");
90 }
91 if (hcd->schedule) {
92 ret = hcd->schedule(hcd, batch);
93 } else {
94 usb_log_error("HCD does not implement scheduler.\n");
95 ret = ENOTSUP;
96 }
97out:
98 if (ret != EOK)
99 usb_transfer_batch_dispose(batch);
100
101 return ret;
102}
103/*----------------------------------------------------------------------------*/
104/** Request address interface function
105 *
106 * @param[in] fun DDF function that was called.
107 * @param[in] speed Speed to associate with the new default address.
108 * @param[out] address Place to write a new address.
109 * @return Error code.
110 */
111static int request_address(
112 ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
113{
114 assert(fun);
115 hcd_t *hcd = fun_to_hcd(fun);
116 assert(hcd);
117 assert(address);
118
119 usb_log_debug("Address request speed: %s.\n", usb_str_speed(speed));
120 *address = device_keeper_get_free_address(&hcd->dev_manager, speed);
121 usb_log_debug("Address request with result: %d.\n", *address);
122 if (*address <= 0)
123 return *address;
124 return EOK;
125}
126/*----------------------------------------------------------------------------*/
127/** Bind address interface function
128 *
129 * @param[in] fun DDF function that was called.
130 * @param[in] address Address of the device
131 * @param[in] handle Devman handle of the device driver.
132 * @return Error code.
133 */
134static int bind_address(
135 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
136{
137 assert(fun);
138 hcd_t *hcd = fun_to_hcd(fun);
139 assert(hcd);
140
141 usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
142 usb_device_keeper_bind(&hcd->dev_manager, address, handle);
143 return EOK;
144}
145/*----------------------------------------------------------------------------*/
146/** Find device handle by address interface function.
147 *
148 * @param[in] fun DDF function that was called.
149 * @param[in] address Address in question.
150 * @param[out] handle Where to store device handle if found.
151 * @return Error code.
152 */
153static int find_by_address(ddf_fun_t *fun, usb_address_t address,
154 devman_handle_t *handle)
155{
156 assert(fun);
157 hcd_t *hcd = fun_to_hcd(fun);
158 assert(hcd);
159 const bool found =
160 usb_device_keeper_find_by_address(&hcd->dev_manager, address, handle);
161 return found ? EOK : ENOENT;
162}
163/*----------------------------------------------------------------------------*/
164/** Release address interface function
165 *
166 * @param[in] fun DDF function that was called.
167 * @param[in] address USB address to be released.
168 * @return Error code.
169 */
170static int release_address(ddf_fun_t *fun, usb_address_t address)
171{
172 assert(fun);
173 hcd_t *hcd = fun_to_hcd(fun);
174 assert(hcd);
175 usb_log_debug("Address release %d.\n", address);
176 usb_device_keeper_release(&hcd->dev_manager, address);
177 return EOK;
178}
179/*----------------------------------------------------------------------------*/
180static int register_endpoint(
181 ddf_fun_t *fun, usb_address_t address, usb_speed_t ep_speed,
182 usb_endpoint_t endpoint,
183 usb_transfer_type_t transfer_type, usb_direction_t direction,
184 size_t max_packet_size, unsigned int interval)
185{
186 assert(fun);
187 hcd_t *hcd = fun_to_hcd(fun);
188 assert(hcd);
189 const size_t size = max_packet_size;
190 usb_speed_t speed =
191 usb_device_keeper_get_speed(&hcd->dev_manager, address);
192 if (speed >= USB_SPEED_MAX) {
193 // Does this happen?
194 speed = ep_speed;
195 }
196 usb_log_debug("Register endpoint %d:%d %s-%s %s %zuB %ums.\n",
197 address, endpoint, usb_str_transfer_type(transfer_type),
198 usb_str_direction(direction), usb_str_speed(speed),
199 max_packet_size, interval);
200
201 endpoint_t *ep = endpoint_get(
202 address, endpoint, direction, transfer_type, speed, max_packet_size);
203 if (!ep)
204 return ENOMEM;
205 int ret = EOK;
206 if (hcd->ep_add_hook) {
207 ret = hcd->ep_add_hook(ep);
208 }
209 if (ret != EOK) {
210 endpoint_destroy(ep);
211 return ret;
212 }
213
214 ret = usb_endpoint_manager_register_ep(&hcd->ep_manager, ep, size);
215 if (ret != EOK) {
216 endpoint_destroy(ep);
217 }
218 return ret;
219}
220/*----------------------------------------------------------------------------*/
221static int unregister_endpoint(
222 ddf_fun_t *fun, usb_address_t address,
223 usb_endpoint_t endpoint, usb_direction_t direction)
224{
225 assert(fun);
226 hcd_t *hcd = fun_to_hcd(fun);
227 assert(hcd);
228 usb_log_debug("Unregister endpoint %d:%d %s.\n",
229 address, endpoint, usb_str_direction(direction));
230 return usb_endpoint_manager_unregister_ep(&hcd->ep_manager, address,
231 endpoint, direction);
232}
233/*----------------------------------------------------------------------------*/
234/** Interrupt out transaction interface function
235 *
236 * @param[in] fun DDF function that was called.
237 * @param[in] target USB device to write to.
238 * @param[in] data Source of data.
239 * @param[in] size Size of data source.
240 * @param[in] callback Function to call on transaction completion
241 * @param[in] arg Additional for callback function.
242 * @return Error code.
243 */
244static int interrupt_out(
245 ddf_fun_t *fun, usb_target_t target, void *data,
246 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
247{
248 return send_batch(fun, target, USB_DIRECTION_OUT, data, size,
249 NULL, 0, NULL, callback, arg, "Interrupt OUT");
250}
251/*----------------------------------------------------------------------------*/
252/** Interrupt in transaction interface function
253 *
254 * @param[in] fun DDF function that was called.
255 * @param[in] target USB device to write to.
256 * @param[out] data Data destination.
257 * @param[in] size Size of data source.
258 * @param[in] callback Function to call on transaction completion
259 * @param[in] arg Additional for callback function.
260 * @return Error code.
261 */
262static int interrupt_in(
263 ddf_fun_t *fun, usb_target_t target, void *data,
264 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
265{
266 return send_batch(fun, target, USB_DIRECTION_IN, data, size,
267 NULL, 0, callback, NULL, arg, "Interrupt IN");
268}
269/*----------------------------------------------------------------------------*/
270/** Bulk out transaction interface function
271 *
272 * @param[in] fun DDF function that was called.
273 * @param[in] target USB device to write to.
274 * @param[in] data Source of data.
275 * @param[in] size Size of data source.
276 * @param[in] callback Function to call on transaction completion
277 * @param[in] arg Additional for callback function.
278 * @return Error code.
279 */
280static int bulk_out(
281 ddf_fun_t *fun, usb_target_t target, void *data,
282 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
283{
284 return send_batch(fun, target, USB_DIRECTION_OUT, data, size,
285 NULL, 0, NULL, callback, arg, "Bulk OUT");
286}
287/*----------------------------------------------------------------------------*/
288/** Bulk in transaction interface function
289 *
290 * @param[in] fun DDF function that was called.
291 * @param[in] target USB device to write to.
292 * @param[out] data Data destination.
293 * @param[in] size Size of data source.
294 * @param[in] callback Function to call on transaction completion
295 * @param[in] arg Additional for callback function.
296 * @return Error code.
297 */
298static int bulk_in(
299 ddf_fun_t *fun, usb_target_t target, void *data,
300 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
301{
302 return send_batch(fun, target, USB_DIRECTION_IN, data, size,
303 NULL, 0, callback, NULL, arg, "Bulk IN");
304}
305/*----------------------------------------------------------------------------*/
306/** Control write transaction interface function
307 *
308 * @param[in] fun DDF function that was called.
309 * @param[in] target USB device to write to.
310 * @param[in] setup_data Data to send with SETUP transfer.
311 * @param[in] setup_size Size of data to send with SETUP transfer (always 8B).
312 * @param[in] data Source of data.
313 * @param[in] size Size of data source.
314 * @param[in] callback Function to call on transaction completion.
315 * @param[in] arg Additional for callback function.
316 * @return Error code.
317 */
318static int control_write(
319 ddf_fun_t *fun, usb_target_t target,
320 void *setup_data, size_t setup_size, void *data, size_t size,
321 usbhc_iface_transfer_out_callback_t callback, void *arg)
322{
323 return send_batch(fun, target, USB_DIRECTION_BOTH, data, size,
324 setup_data, setup_size, NULL, callback, arg, "Control WRITE");
325}
326/*----------------------------------------------------------------------------*/
327/** Control read transaction interface function
328 *
329 * @param[in] fun DDF function that was called.
330 * @param[in] target USB device to write to.
331 * @param[in] setup_data Data to send with SETUP packet.
332 * @param[in] setup_size Size of data to send with SETUP packet (should be 8B).
333 * @param[out] data Source of data.
334 * @param[in] size Size of data source.
335 * @param[in] callback Function to call on transaction completion.
336 * @param[in] arg Additional for callback function.
337 * @return Error code.
338 */
339static int control_read(
340 ddf_fun_t *fun, usb_target_t target,
341 void *setup_data, size_t setup_size, void *data, size_t size,
342 usbhc_iface_transfer_in_callback_t callback, void *arg)
343{
344 return send_batch(fun, target, USB_DIRECTION_BOTH, data, size,
345 setup_data, setup_size, callback, NULL, arg, "Control READ");
346}
347/*----------------------------------------------------------------------------*/
348usbhc_iface_t hcd_iface = {
349 .request_address = request_address,
350 .bind_address = bind_address,
351 .find_by_address = find_by_address,
352 .release_address = release_address,
353
354 .register_endpoint = register_endpoint,
355 .unregister_endpoint = unregister_endpoint,
356
357 .interrupt_out = interrupt_out,
358 .interrupt_in = interrupt_in,
359
360 .bulk_out = bulk_out,
361 .bulk_in = bulk_in,
362
363 .control_write = control_write,
364 .control_read = control_read,
365};
366/**
367 * @}
368 */
Note: See TracBrowser for help on using the repository browser.