source: mainline/uspace/drv/ohci/iface.c@ c6394aa

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

Merge development/ changes

  • Property mode set to 100644
File size: 17.5 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 = usb_endpoint_manager_get_ep(&(*hc)->ep_manager,
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 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 name, target.address, target.endpoint, bw, res_bw);
71 return ENOSPC;
72 }
73 usb_log_debug("%s %d:%d %zu(%zu).\n",
74 name, target.address, target.endpoint, size, ep->max_packet_size);
75
76 assert(ep->speed ==
77 usb_device_keeper_get_speed(&(*hc)->manager, target.address));
78// assert(ep->max_packet_size == max_packet_size);
79// assert(ep->transfer_type == USB_TRANSFER_CONTROL);
80
81 *batch =
82 batch_get(fun, ep, data, size, setup_data, setup_size,
83 in, out, arg);
84 if (!batch)
85 return ENOMEM;
86 return EOK;
87}
88
89
90/** Reserve default address interface function
91 *
92 * @param[in] fun DDF function that was called.
93 * @param[in] speed Speed to associate with the new default address.
94 * @return Error code.
95 */
96static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
97{
98 assert(fun);
99 hc_t *hc = fun_to_hc(fun);
100 assert(hc);
101 usb_log_debug("Default address request with speed %d.\n", speed);
102 usb_device_keeper_reserve_default_address(&hc->manager, speed);
103 return EOK;
104#if 0
105 endpoint_t *ep = malloc(sizeof(endpoint_t));
106 if (ep == NULL)
107 return ENOMEM;
108 const size_t max_packet_size = speed == USB_SPEED_LOW ? 8 : 64;
109 endpoint_init(ep, USB_TRANSFER_CONTROL, speed, max_packet_size);
110 int ret;
111try_retgister:
112 ret = usb_endpoint_manager_register_ep(&hc->ep_manager,
113 USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH, ep, endpoint_destroy, 0);
114 if (ret == EEXISTS) {
115 async_usleep(1000);
116 goto try_retgister;
117 }
118 if (ret != EOK) {
119 endpoint_destroy(ep);
120 }
121 return ret;
122#endif
123}
124/*----------------------------------------------------------------------------*/
125/** Release default address interface function
126 *
127 * @param[in] fun DDF function that was called.
128 * @return Error code.
129 */
130static int release_default_address(ddf_fun_t *fun)
131{
132 assert(fun);
133 hc_t *hc = fun_to_hc(fun);
134 assert(hc);
135 usb_log_debug("Default address release.\n");
136// return usb_endpoint_manager_unregister_ep(&hc->ep_manager,
137// USB_ADDRESS_DEFAULT, 0, USB_DIRECTION_BOTH);
138 usb_device_keeper_release_default_address(&hc->manager);
139 return EOK;
140}
141/*----------------------------------------------------------------------------*/
142/** Request address interface function
143 *
144 * @param[in] fun DDF function that was called.
145 * @param[in] speed Speed to associate with the new default address.
146 * @param[out] address Place to write a new address.
147 * @return Error code.
148 */
149static int request_address(
150 ddf_fun_t *fun, usb_speed_t speed, usb_address_t *address)
151{
152 assert(fun);
153 hc_t *hc = fun_to_hc(fun);
154 assert(hc);
155 assert(address);
156
157 usb_log_debug("Address request with speed %d.\n", speed);
158 *address = device_keeper_get_free_address(&hc->manager, speed);
159 usb_log_debug("Address request with result: %d.\n", *address);
160 if (*address <= 0)
161 return *address;
162 return EOK;
163}
164/*----------------------------------------------------------------------------*/
165/** Bind address interface function
166 *
167 * @param[in] fun DDF function that was called.
168 * @param[in] address Address of the device
169 * @param[in] handle Devman handle of the device driver.
170 * @return Error code.
171 */
172static int bind_address(
173 ddf_fun_t *fun, usb_address_t address, devman_handle_t handle)
174{
175 assert(fun);
176 hc_t *hc = fun_to_hc(fun);
177 assert(hc);
178 usb_log_debug("Address bind %d-%d.\n", address, handle);
179 usb_device_keeper_bind(&hc->manager, address, handle);
180 return EOK;
181}
182/*----------------------------------------------------------------------------*/
183/** Release address interface function
184 *
185 * @param[in] fun DDF function that was called.
186 * @param[in] address USB address to be released.
187 * @return Error code.
188 */
189static int release_address(ddf_fun_t *fun, usb_address_t address)
190{
191 assert(fun);
192 hc_t *hc = fun_to_hc(fun);
193 assert(hc);
194 usb_log_debug("Address release %d.\n", address);
195 usb_device_keeper_release(&hc->manager, address);
196 return EOK;
197}
198/*----------------------------------------------------------------------------*/
199/** Register endpoint for bandwidth reservation.
200 *
201 * @param[in] fun Device function the action was invoked on.
202 * @param[in] address USB address of the device.
203 * @param[in] ep_speed Endpoint speed (invalid means to use device one).
204 * @param[in] endpoint Endpoint number.
205 * @param[in] transfer_type USB transfer type.
206 * @param[in] direction Endpoint data direction.
207 * @param[in] max_packet_size Max packet size of the endpoint.
208 * @param[in] interval Polling interval.
209 * @return Error code.
210 */
211static int register_endpoint(ddf_fun_t *fun,
212 usb_address_t address, usb_speed_t ep_speed, usb_endpoint_t endpoint,
213 usb_transfer_type_t transfer_type, usb_direction_t direction,
214 size_t max_packet_size, unsigned int interval)
215{
216 hc_t *hc = fun_to_hc(fun);
217 assert(hc);
218 if (address == hc->rh.address)
219 return EOK;
220 usb_speed_t speed = usb_device_keeper_get_speed(&hc->manager, address);
221 if (speed >= USB_SPEED_MAX) {
222 speed = ep_speed;
223 }
224 const size_t size =
225 (transfer_type == USB_TRANSFER_INTERRUPT
226 || transfer_type == USB_TRANSFER_ISOCHRONOUS) ?
227 max_packet_size : 0;
228 int ret;
229
230 endpoint_t *ep = malloc(sizeof(endpoint_t));
231 if (ep == NULL)
232 return ENOMEM;
233 ret = endpoint_init(ep, address, endpoint, direction,
234 transfer_type, speed, max_packet_size);
235 if (ret != EOK) {
236 free(ep);
237 return ret;
238 }
239
240 usb_log_debug("Register endpoint %d:%d %s %s(%d) %zu(%zu) %u.\n",
241 address, endpoint, usb_str_transfer_type(transfer_type),
242 usb_str_speed(speed), direction, size, max_packet_size, interval);
243
244 ret = usb_endpoint_manager_register_ep(&hc->ep_manager, ep, size);
245 if (ret != EOK) {
246 endpoint_destroy(ep);
247 } else {
248 usb_device_keeper_add_ep(&hc->manager, address, ep);
249 }
250 return ret;
251}
252/*----------------------------------------------------------------------------*/
253static int unregister_endpoint(
254 ddf_fun_t *fun, usb_address_t address,
255 usb_endpoint_t endpoint, usb_direction_t direction)
256{
257 hc_t *hc = fun_to_hc(fun);
258 assert(hc);
259 usb_log_debug("Unregister endpoint %d:%d %d.\n",
260 address, endpoint, direction);
261 endpoint_t *ep = usb_endpoint_manager_get_ep(&hc->ep_manager,
262 address, endpoint, direction, NULL);
263 if (ep != NULL) {
264 usb_device_keeper_del_ep(&hc->manager, address, ep);
265 }
266 return usb_endpoint_manager_unregister_ep(&hc->ep_manager, address,
267 endpoint, direction);
268}
269/*----------------------------------------------------------------------------*/
270/** Schedule interrupt out transfer.
271 *
272 * The callback is supposed to be called once the transfer (on the wire) is
273 * complete regardless of the outcome.
274 * However, the callback could be called only when this function returns
275 * with success status (i.e. returns EOK).
276 *
277 * @param[in] fun Device function the action was invoked on.
278 * @param[in] target Target pipe (address and endpoint number) specification.
279 * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
280 * by the caller).
281 * @param[in] size Size of the @p data buffer in bytes.
282 * @param[in] callback Callback to be issued once the transfer is complete.
283 * @param[in] arg Pass-through argument to the callback.
284 * @return Error code.
285 */
286static int interrupt_out(
287 ddf_fun_t *fun, usb_target_t target, void *data,
288 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
289{
290 usb_transfer_batch_t *batch = NULL;
291 hc_t *hc = NULL;
292 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
293 NULL, 0, NULL, callback, arg, "Interrupt OUT", &hc, &batch);
294 if (ret != EOK)
295 return ret;
296 batch_interrupt_out(batch);
297 ret = hc_schedule(hc, batch);
298 if (ret != EOK) {
299 batch_dispose(batch);
300 }
301 return ret;
302}
303/*----------------------------------------------------------------------------*/
304/** Schedule interrupt in transfer.
305 *
306 * The callback is supposed to be called once the transfer (on the wire) is
307 * complete regardless of the outcome.
308 * However, the callback could be called only when this function returns
309 * with success status (i.e. returns EOK).
310 *
311 * @param[in] fun Device function the action was invoked on.
312 * @param[in] target Target pipe (address and endpoint number) specification.
313 * @param[in] data Buffer where to store the data (in USB endianess,
314 * allocated and deallocated by the caller).
315 * @param[in] size Size of the @p data buffer in bytes.
316 * @param[in] callback Callback to be issued once the transfer is complete.
317 * @param[in] arg Pass-through argument to the callback.
318 * @return Error code.
319 */
320static int interrupt_in(
321 ddf_fun_t *fun, usb_target_t target, void *data,
322 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
323{
324 usb_transfer_batch_t *batch = NULL;
325 hc_t *hc = NULL;
326 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
327 NULL, 0, callback, NULL, arg, "Interrupt IN", &hc, &batch);
328 if (ret != EOK)
329 return ret;
330 batch_interrupt_in(batch);
331 ret = hc_schedule(hc, batch);
332 if (ret != EOK) {
333 batch_dispose(batch);
334 }
335 return ret;
336}
337/*----------------------------------------------------------------------------*/
338/** Schedule bulk out transfer.
339 *
340 * The callback is supposed to be called once the transfer (on the wire) is
341 * complete regardless of the outcome.
342 * However, the callback could be called only when this function returns
343 * with success status (i.e. returns EOK).
344 *
345 * @param[in] fun Device function the action was invoked on.
346 * @param[in] target Target pipe (address and endpoint number) specification.
347 * @param[in] data Data to be sent (in USB endianess, allocated and deallocated
348 * by the caller).
349 * @param[in] size Size of the @p data buffer in bytes.
350 * @param[in] callback Callback to be issued once the transfer is complete.
351 * @param[in] arg Pass-through argument to the callback.
352 * @return Error code.
353 */
354static int bulk_out(
355 ddf_fun_t *fun, usb_target_t target, void *data,
356 size_t size, usbhc_iface_transfer_out_callback_t callback, void *arg)
357{
358 usb_transfer_batch_t *batch = NULL;
359 hc_t *hc = NULL;
360 int ret = setup_batch(fun, target, USB_DIRECTION_OUT, data, size,
361 NULL, 0, NULL, callback, arg, "Bulk OUT", &hc, &batch);
362 if (ret != EOK)
363 return ret;
364 batch_bulk_out(batch);
365 ret = hc_schedule(hc, batch);
366 if (ret != EOK) {
367 batch_dispose(batch);
368 }
369 return ret;
370}
371/*----------------------------------------------------------------------------*/
372/** Schedule bulk in transfer.
373 *
374 * The callback is supposed to be called once the transfer (on the wire) is
375 * complete regardless of the outcome.
376 * However, the callback could be called only when this function returns
377 * with success status (i.e. returns EOK).
378 *
379 * @param[in] fun Device function the action was invoked on.
380 * @param[in] target Target pipe (address and endpoint number) specification.
381 * @param[in] data Buffer where to store the data (in USB endianess,
382 * allocated and deallocated by the caller).
383 * @param[in] size Size of the @p data buffer in bytes.
384 * @param[in] callback Callback to be issued once the transfer is complete.
385 * @param[in] arg Pass-through argument to the callback.
386 * @return Error code.
387 */
388static int bulk_in(
389 ddf_fun_t *fun, usb_target_t target, void *data,
390 size_t size, usbhc_iface_transfer_in_callback_t callback, void *arg)
391{
392 usb_transfer_batch_t *batch = NULL;
393 hc_t *hc = NULL;
394 int ret = setup_batch(fun, target, USB_DIRECTION_IN, data, size,
395 NULL, 0, callback, NULL, arg, "Bulk IN", &hc, &batch);
396 if (ret != EOK)
397 return ret;
398 batch_bulk_in(batch);
399 ret = hc_schedule(hc, batch);
400 if (ret != EOK) {
401 batch_dispose(batch);
402 }
403 return ret;
404}
405/*----------------------------------------------------------------------------*/
406/** Schedule control write transfer.
407 *
408 * The callback is supposed to be called once the transfer (on the wire) is
409 * complete regardless of the outcome.
410 * However, the callback could be called only when this function returns
411 * with success status (i.e. returns EOK).
412 *
413 * @param[in] fun Device function the action was invoked on.
414 * @param[in] target Target pipe (address and endpoint number) specification.
415 * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
416 * and deallocated by the caller).
417 * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
418 * @param[in] data_buffer Data buffer (in USB endianess, allocated and
419 * deallocated by the caller).
420 * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
421 * @param[in] callback Callback to be issued once the transfer is complete.
422 * @param[in] arg Pass-through argument to the callback.
423 * @return Error code.
424 */
425static int control_write(
426 ddf_fun_t *fun, usb_target_t target,
427 void *setup_data, size_t setup_size, void *data, size_t size,
428 usbhc_iface_transfer_out_callback_t callback, void *arg)
429{
430 usb_transfer_batch_t *batch = NULL;
431 hc_t *hc = NULL;
432 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
433 setup_data, setup_size, NULL, callback, arg, "Control WRITE",
434 &hc, &batch);
435 if (ret != EOK)
436 return ret;
437 usb_device_keeper_reset_if_need(&hc->manager, target, setup_data);
438 batch_control_write(batch);
439 ret = hc_schedule(hc, batch);
440 if (ret != EOK) {
441 batch_dispose(batch);
442 }
443 return ret;
444}
445/*----------------------------------------------------------------------------*/
446/** Schedule control read transfer.
447 *
448 * The callback is supposed to be called once the transfer (on the wire) is
449 * complete regardless of the outcome.
450 * However, the callback could be called only when this function returns
451 * with success status (i.e. returns EOK).
452 *
453 * @param[in] fun Device function the action was invoked on.
454 * @param[in] target Target pipe (address and endpoint number) specification.
455 * @param[in] setup_packet Setup packet buffer (in USB endianess, allocated
456 * and deallocated by the caller).
457 * @param[in] setup_packet_size Size of @p setup_packet buffer in bytes.
458 * @param[in] data_buffer Buffer where to store the data (in USB endianess,
459 * allocated and deallocated by the caller).
460 * @param[in] data_buffer_size Size of @p data_buffer buffer in bytes.
461 * @param[in] callback Callback to be issued once the transfer is complete.
462 * @param[in] arg Pass-through argument to the callback.
463 * @return Error code.
464 */
465static int control_read(
466 ddf_fun_t *fun, usb_target_t target,
467 void *setup_data, size_t setup_size, void *data, size_t size,
468 usbhc_iface_transfer_in_callback_t callback, void *arg)
469{
470 usb_transfer_batch_t *batch = NULL;
471 hc_t *hc = NULL;
472 int ret = setup_batch(fun, target, USB_DIRECTION_BOTH, data, size,
473 setup_data, setup_size, callback, NULL, arg, "Control READ",
474 &hc, &batch);
475 if (ret != EOK)
476 return ret;
477 batch_control_read(batch);
478 ret = hc_schedule(hc, batch);
479 if (ret != EOK) {
480 batch_dispose(batch);
481 }
482 return ret;
483}
484/*----------------------------------------------------------------------------*/
485usbhc_iface_t hc_iface = {
486 .reserve_default_address = reserve_default_address,
487 .release_default_address = release_default_address,
488 .request_address = request_address,
489 .bind_address = bind_address,
490 .release_address = release_address,
491
492 .register_endpoint = register_endpoint,
493 .unregister_endpoint = unregister_endpoint,
494
495 .interrupt_out = interrupt_out,
496 .interrupt_in = interrupt_in,
497
498 .bulk_out = bulk_out,
499 .bulk_in = bulk_in,
500
501 .control_write = control_write,
502 .control_read = control_read,
503};
504/**
505 * @}
506 */
Note: See TracBrowser for help on using the repository browser.