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 | * Device keeper structure and functions.
|
---|
33 | *
|
---|
34 | * Typical USB host controller needs to keep track of various settings for
|
---|
35 | * each device that is connected to it.
|
---|
36 | * State of toggle bit, device speed etc. etc.
|
---|
37 | * This structure shall simplify the management.
|
---|
38 | */
|
---|
39 | #ifndef LIBUSBHOST_HOST_USB_ENDPOINT_MANAGER_H
|
---|
40 | #define LIBUSBHOST_HOST_USB_ENDPOINT_MANAGER_H
|
---|
41 |
|
---|
42 | #include <usb/host/endpoint.h>
|
---|
43 | #include <usb/usb.h>
|
---|
44 |
|
---|
45 | #include <adt/list.h>
|
---|
46 | #include <fibril_synch.h>
|
---|
47 | #include <stdbool.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /** Bytes per second in FULL SPEED */
|
---|
51 | #define BANDWIDTH_TOTAL_USB11 (12000000 / 8)
|
---|
52 | /** 90% of total bandwidth is available for periodic transfers */
|
---|
53 | #define BANDWIDTH_AVAILABLE_USB11 ((BANDWIDTH_TOTAL_USB11 / 10) * 9)
|
---|
54 |
|
---|
55 | //TODO: Implement
|
---|
56 | #define BANDWIDTH_AVAILABLE_USB20 1
|
---|
57 |
|
---|
58 | typedef size_t (*bw_count_func_t)(usb_speed_t, usb_transfer_type_t, size_t, size_t);
|
---|
59 | typedef void (*ep_remove_callback_t)(endpoint_t *, void *);
|
---|
60 | typedef int (*ep_add_callback_t)(endpoint_t *, void *);
|
---|
61 |
|
---|
62 | /** Endpoint management structure */
|
---|
63 | typedef struct usb_bus {
|
---|
64 | struct {
|
---|
65 | usb_speed_t speed; /**< Device speed */
|
---|
66 | bool occupied; /**< The address is in use. */
|
---|
67 | list_t endpoint_list; /**< Store endpoint_t instances */
|
---|
68 | } devices[USB_ADDRESS_COUNT];
|
---|
69 | /** Prevents races accessing lists */
|
---|
70 | fibril_mutex_t guard;
|
---|
71 | /** Size of the bandwidth pool */
|
---|
72 | size_t free_bw;
|
---|
73 | /** Use this function to count bw required by EP */
|
---|
74 | bw_count_func_t bw_count;
|
---|
75 | /** Maximum speed allowed. */
|
---|
76 | usb_speed_t max_speed;
|
---|
77 | /** The last reserved address */
|
---|
78 | usb_address_t last_address;
|
---|
79 | } usb_bus_t;
|
---|
80 |
|
---|
81 |
|
---|
82 | size_t bandwidth_count_usb11(usb_speed_t speed, usb_transfer_type_t type,
|
---|
83 | size_t size, size_t max_packet_size);
|
---|
84 | size_t bandwidth_count_usb20(usb_speed_t speed, usb_transfer_type_t type,
|
---|
85 | size_t size, size_t max_packet_size);
|
---|
86 |
|
---|
87 | int usb_bus_init(usb_bus_t *instance,
|
---|
88 | size_t available_bandwidth, bw_count_func_t bw_count, usb_speed_t max_speed);
|
---|
89 |
|
---|
90 | int usb_bus_register_ep(usb_bus_t *instance, endpoint_t *ep, size_t data_size);
|
---|
91 |
|
---|
92 | int usb_bus_unregister_ep(usb_bus_t *instance, endpoint_t *ep);
|
---|
93 |
|
---|
94 | endpoint_t * usb_bus_find_ep(usb_bus_t *instance,
|
---|
95 | usb_address_t address, usb_endpoint_t ep, usb_direction_t direction);
|
---|
96 |
|
---|
97 | int usb_bus_add_ep(usb_bus_t *instance,
|
---|
98 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
|
---|
99 | usb_transfer_type_t type, size_t max_packet_size, unsigned packets,
|
---|
100 | size_t data_size, ep_add_callback_t callback, void *arg,
|
---|
101 | usb_address_t tt_address, unsigned tt_port);
|
---|
102 |
|
---|
103 | int usb_bus_remove_ep(usb_bus_t *instance,
|
---|
104 | usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
|
---|
105 | ep_remove_callback_t callback, void *arg);
|
---|
106 |
|
---|
107 | int usb_bus_reset_toggle(usb_bus_t *instance, usb_target_t target, bool all);
|
---|
108 |
|
---|
109 | int usb_bus_remove_address(usb_bus_t *instance,
|
---|
110 | usb_address_t address, ep_remove_callback_t callback, void *arg);
|
---|
111 |
|
---|
112 | int usb_bus_request_address(usb_bus_t *instance,
|
---|
113 | usb_address_t *address, bool strict, usb_speed_t speed);
|
---|
114 |
|
---|
115 | int usb_bus_get_speed(usb_bus_t *instance,
|
---|
116 | usb_address_t address, usb_speed_t *speed);
|
---|
117 | #endif
|
---|
118 | /**
|
---|
119 | * @}
|
---|
120 | */
|
---|