source: mainline/uspace/drv/ohci/iface.c@ 4a7a8d4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4a7a8d4 was df25ab6, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

All HC drivers supports "get handle by address"

  • Property mode set to 100644
File size: 15.6 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky, 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 drvusbohci
29 * @{
30 */
31/** @file
32 * @brief OHCI driver hc 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
40#include "iface.h"
41#include "hc.h"
42
43static inline int setup_batch(
44 ddf_fun_t *fun, usb_target_t target, usb_direction_t direction,
45 void *data, size_t size, void * setup_data, size_t setup_size,
46 usbhc_iface_transfer_in_callback_t in,
47 usbhc_iface_transfer_out_callback_t out, void *arg, const char* name,
48 hc_t **hc, usb_transfer_batch_t **batch)
49{
50 assert(hc);
51 assert(batch);
52 assert(fun);
53 *hc = fun_to_hc(fun);
54 assert(*hc);
55
56 size_t res_bw;
57 endpoint_t *ep = hc_get_endpoint(*hc,
58 target.address, target.endpoint, direction, &res_bw);
59 if (ep == NULL) {
60 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
61 target.address, target.endpoint, name);
62 return ENOENT;
63 }
64
65 usb_log_debug("%s %d:%d %zu(%zu).\n",
66 name, target.address, target.endpoint, size, ep->max_packet_size);
67
68 const size_t bw = bandwidth_count_usb11(
69 ep->speed, ep->transfer_type, size, ep->max_packet_size);
70 if (res_bw < bw) {
71 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
72 "but only %zu is reserved.\n",
73 target.address, target.endpoint, name, bw, res_bw);
74 return ENOSPC;
75 }
76
77 *batch = batch_get(
78 fun, ep, data, size, setup_data, setup_size, in, out, arg);
79 if (!*batch)
80 return ENOMEM;
81 return EOK;
82}
83/*----------------------------------------------------------------------------*/
84/** Request address interface function
85 *
86 * @param[in] fun DDF function that was called.
87 * @param[in] speed Speed to associate with the new default address.
88 * @param[out] address Place to write a new address.
89 * @return Error code.
90 */
91static int request_address(
92 ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
93{
94 assert(fun);
95 hc_t *hc = fun_to_hc(fun);
96 assert(hc);
97 assert(address);
98
99 usb_log_debug("Address request with speed %d.\n", speed);
100 *address = device_keeper_get_free_address(&hc->manager, speed);
101 usb_log_debug("Address request with result: %d.\n", *address);
102 if (*address <= 0)
103 return *address;
104 return EOK;
105}
106/*----------------------------------------------------------------------------*/
107/** Bind address interface function
108 *
109 * @param[in] fun DDF function that was called.
110 * @param[in] address Address of the device
111 * @param[in] handle Devman handle of the device driver.
112 * @return Error code.
113 */
114static int bind_address(
115 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
116{
117 assert(fun);
118 hc_t *hc = fun_to_hc(fun);
119 assert(hc);
120 usb_log_debug("Address bind %d-%" PRIun ".\n", address, handle);
121 usb_device_keeper_bind(&hc->manager, address, handle);
122 return EOK;
123}
124
125
126/** Find device handle by address interface function.
127 *
128 * @param[in] fun DDF function that was called.
129 * @param[in] address Address in question.
130 * @param[out] handle Where to store device handle if found.
131 * @return Error code.
132 */
133static int find_by_address(ddf_fun_t *fun, usb_address_t address,
134 devman_handle_t *handle)
135{
136 assert(fun);
137 hc_t *hc = fun_to_hc(fun);
138 assert(hc);
139 bool found =
140 usb_device_keeper_find_by_address(&hc->manager, address, handle);
141 return found ? EOK : ENOENT;
142}
143
144/*----------------------------------------------------------------------------*/
145/** Release address interface function
146 *
147 * @param[in] fun DDF function that was called.
148 * @param[in] address USB address to be released.
149 * @return Error code.
150 */
151static int release_address(ddf_fun_t *fun, usb_address_t address)
152{
153 assert(fun);
154 hc_t *hc = fun_to_hc(fun);
155 assert(hc);
156 usb_log_debug("Address release %d.\n", address);
157 usb_device_keeper_release(&hc->manager, address);
158 return EOK;
159}
160/*----------------------------------------------------------------------------*/
161/** Register endpoint for bandwidth reservation.
162 *
163 * @param[in] fun Device function the action was invoked on.
164 * @param[in] address USB address of the device.
165 * @param[in] ep_speed Endpoint speed (invalid means to use device one).
166 * @param[in] endpoint Endpoint number.
167 * @param[in] transfer_type USB transfer type.
168 * @param[in] direction Endpoint data direction.
169 * @param[in] max_packet_size Max packet size of the endpoint.
170 * @param[in] interval Polling interval.
171 * @return Error code.
172 */
173static int register_endpoint(ddf_fun_t *fun,
174 usb_address_t address, usb_speed_t ep_speed, usb_endpoint_t endpoint,
175 usb_transfer_type_t transfer_type, usb_direction_t direction,
176 size_t max_packet_size, unsigned int interval)
177{
178 hc_t *hc = fun_to_hc(fun);
179 assert(hc);
180
181 usb_speed_t speed = usb_device_keeper_get_speed(&hc->manager, address);
182 if (speed >= USB_SPEED_MAX) {
183 speed = ep_speed;
184 }
185 const size_t size = max_packet_size;
186
187 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
188 address, endpoint, usb_str_transfer_type(transfer_type),
189 usb_str_speed(speed), direction, size, max_packet_size, interval);
190
191 return hc_add_endpoint(hc, address, endpoint, speed, transfer_type,
192 direction, max_packet_size, size, interval);
193}
194/*----------------------------------------------------------------------------*/
195static int unregister_endpoint(
196 ddf_fun_t *fun, usb_address_t address,
197 usb_endpoint_t endpoint, usb_direction_t direction)
198{
199 hc_t *hc = fun_to_hc(fun);
200 assert(hc);
201 usb_log_debug("Unregister endpoint %d:%d %d.\n",
202 address, endpoint, direction);
203 return hc_remove_endpoint(hc, address, endpoint, direction);
204}
205/*----------------------------------------------------------------------------*/
206/** Schedule interrupt out transfer.
207 *
208 * The callback is supposed to be called once the transfer (on the wire) is
209 * complete regardless of the outcome.
210 * However, the callback could be called only when this function returns
211 * with success status (i.e. returns EOK).
212 *
213 * @param[in] fun Device function the action was invoked on.
214 * @param[in] target Target pipe (address and endpoint number) specification.
215 * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
216 * by the caller).
217 * @param[in] size Size of the @p data buffer in bytes.
218 * @param[in] callback Callback to be issued once the transfer is complete.
219 * @param[in] arg Pass-through argument to the callback.
220 * @return Error code.
221 */
222static int interrupt_out(
223 ddf_fun_t *fun, usb_target_t target, void *data,
224 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
225{
226 usb_transfer_batch_t *batch = NULL;
227 hc_t *hc = NULL;
228 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
229 NULL, 0, NULL, callback, arg, "Interrupt OUT", &hc, &batch);
230 if (ret != EOK)
231 return ret;
232 batch_interrupt_out(batch);
233 ret = hc_schedule(hc, batch);
234 if (ret != EOK) {
235 usb_transfer_batch_dispose(batch);
236 }
237 return ret;
238}
239/*----------------------------------------------------------------------------*/
240/** Schedule interrupt in transfer.
241 *
242 * The callback is supposed to be called once the transfer (on the wire) is
243 * complete regardless of the outcome.
244 * However, the callback could be called only when this function returns
245 * with success status (i.e. returns EOK).
246 *
247 * @param[in] fun Device function the action was invoked on.
248 * @param[in] target Target pipe (address and endpoint number) specification.
249 * @param[in] data Buffer where to store the data (in USB endianess,
250 * allocated and deallocated by the caller).
251 * @param[in] size Size of the @p data buffer in bytes.
252 * @param[in] callback Callback to be issued once the transfer is complete.
253 * @param[in] arg Pass-through argument to the callback.
254 * @return Error code.
255 */
256static int interrupt_in(
257 ddf_fun_t *fun, usb_target_t target, void *data,
258 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
259{
260 usb_transfer_batch_t *batch = NULL;
261 hc_t *hc = NULL;
262 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
263 NULL, 0, callback, NULL, arg, "Interrupt IN", &hc, &batch);
264 if (ret != EOK)
265 return ret;
266 batch_interrupt_in(batch);
267 ret = hc_schedule(hc, batch);
268 if (ret != EOK) {
269 usb_transfer_batch_dispose(batch);
270 }
271 return ret;
272}
273/*----------------------------------------------------------------------------*/
274/** Schedule bulk out transfer.
275 *
276 * The callback is supposed to be called once the transfer (on the wire) is
277 * complete regardless of the outcome.
278 * However, the callback could be called only when this function returns
279 * with success status (i.e. returns EOK).
280 *
281 * @param[in] fun Device function the action was invoked on.
282 * @param[in] target Target pipe (address and endpoint number) specification.
283 * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
284 * by the caller).
285 * @param[in] size Size of the @p data buffer in bytes.
286 * @param[in] callback Callback to be issued once the transfer is complete.
287 * @param[in] arg Pass-through argument to the callback.
288 * @return Error code.
289 */
290static int bulk_out(
291 ddf_fun_t *fun, usb_target_t target, void *data,
292 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
293{
294 usb_transfer_batch_t *batch = NULL;
295 hc_t *hc = NULL;
296 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
297 NULL, 0, NULL, callback, arg, "Bulk OUT", &hc, &batch);
298 if (ret != EOK)
299 return ret;
300 batch_bulk_out(batch);
301 ret = hc_schedule(hc, batch);
302 if (ret != EOK) {
303 usb_transfer_batch_dispose(batch);
304 }
305 return ret;
306}
307/*----------------------------------------------------------------------------*/
308/** Schedule bulk in transfer.
309 *
310 * The callback is supposed to be called once the transfer (on the wire) is
311 * complete regardless of the outcome.
312 * However, the callback could be called only when this function returns
313 * with success status (i.e. returns EOK).
314 *
315 * @param[in] fun Device function the action was invoked on.
316 * @param[in] target Target pipe (address and endpoint number) specification.
317 * @param[in] data Buffer where to store the data (in USB endianess,
318 * allocated and deallocated by the caller).
319 * @param[in] size Size of the @p data buffer in bytes.
320 * @param[in] callback Callback to be issued once the transfer is complete.
321 * @param[in] arg Pass-through argument to the callback.
322 * @return Error code.
323 */
324static int bulk_in(
325 ddf_fun_t *fun, usb_target_t target, void *data,
326 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
327{
328 usb_transfer_batch_t *batch = NULL;
329 hc_t *hc = NULL;
330 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
331 NULL, 0, callback, NULL, arg, "Bulk IN", &hc, &batch);
332 if (ret != EOK)
333 return ret;
334 batch_bulk_in(batch);
335 ret = hc_schedule(hc, batch);
336 if (ret != EOK) {
337 usb_transfer_batch_dispose(batch);
338 }
339 return ret;
340}
341/*----------------------------------------------------------------------------*/
342/** Schedule control write transfer.
343 *
344 * The callback is supposed to be called once the transfer (on the wire) is
345 * complete regardless of the outcome.
346 * However, the callback could be called only when this function returns
347 * with success status (i.e. returns EOK).
348 *
349 * @param[in] fun Device function the action was invoked on.
350 * @param[in] target Target pipe (address and endpoint number) specification.
351 * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
352 * and deallocated by the caller).
353 * @param[in] setup_size Size of @p setup_packet buffer in bytes.
354 * @param[in] data_buffer Data buffer (in USB endianess, allocated and
355 * deallocated by the caller).
356 * @param[in] size Size of @p data_buffer buffer in bytes.
357 * @param[in] callback Callback to be issued once the transfer is complete.
358 * @param[in] arg Pass-through argument to the callback.
359 * @return Error code.
360 */
361static int control_write(
362 ddf_fun_t *fun, usb_target_t target,
363 void *setup_data, size_t setup_size, void *data, size_t size,
364 usbhc_iface_transfer_out_callback_t callback, void *arg)
365{
366 usb_transfer_batch_t *batch = NULL;
367 hc_t *hc = NULL;
368 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
369 setup_data, setup_size, NULL, callback, arg, "Control WRITE",
370 &hc, &batch);
371 if (ret != EOK)
372 return ret;
373 usb_endpoint_manager_reset_if_need(&hc->ep_manager, target, setup_data);
374 batch_control_write(batch);
375 ret = hc_schedule(hc, batch);
376 if (ret != EOK) {
377 usb_transfer_batch_dispose(batch);
378 }
379 return ret;
380}
381/*----------------------------------------------------------------------------*/
382/** Schedule control read transfer.
383 *
384 * The callback is supposed to be called once the transfer (on the wire) is
385 * complete regardless of the outcome.
386 * However, the callback could be called only when this function returns
387 * with success status (i.e. returns EOK).
388 *
389 * @param[in] fun Device function the action was invoked on.
390 * @param[in] target Target pipe (address and endpoint number) specification.
391 * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
392 * and deallocated by the caller).
393 * @param[in] setup_size Size of @p setup_packet buffer in bytes.
394 * @param[in] data_buffer Buffer where to store the data (in USB endianess,
395 * allocated and deallocated by the caller).
396 * @param[in] size Size of @p data_buffer buffer in bytes.
397 * @param[in] callback Callback to be issued once the transfer is complete.
398 * @param[in] arg Pass-through argument to the callback.
399 * @return Error code.
400 */
401static int control_read(
402 ddf_fun_t *fun, usb_target_t target,
403 void *setup_data, size_t setup_size, void *data, size_t size,
404 usbhc_iface_transfer_in_callback_t callback, void *arg)
405{
406 usb_transfer_batch_t *batch = NULL;
407 hc_t *hc = NULL;
408 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
409 setup_data, setup_size, callback, NULL, arg, "Control READ",
410 &hc, &batch);
411 if (ret != EOK)
412 return ret;
413 batch_control_read(batch);
414 ret = hc_schedule(hc, batch);
415 if (ret != EOK) {
416 usb_transfer_batch_dispose(batch);
417 }
418 return ret;
419}
420/*----------------------------------------------------------------------------*/
421usbhc_iface_t hc_iface = {
422 .request_address = request_address,
423 .bind_address = bind_address,
424 .find_by_address = find_by_address,
425 .release_address = release_address,
426
427 .register_endpoint = register_endpoint,
428 .unregister_endpoint = unregister_endpoint,
429
430 .interrupt_out = interrupt_out,
431 .interrupt_in = interrupt_in,
432
433 .bulk_out = bulk_out,
434 .bulk_in = bulk_in,
435
436 .control_write = control_write,
437 .control_read = control_read,
438};
439/**
440 * @}
441 */
Note: See TracBrowser for help on using the repository browser.