1 | /*
|
---|
2 | * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
|
---|
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 | * Virtual base for usb bus implementations.
|
---|
33 | *
|
---|
34 | * The purpose of this structure is to keep information about connected devices
|
---|
35 | * and endpoints, manage available bandwidth and the toggle bit flipping.
|
---|
36 | *
|
---|
37 | * The generic implementation is provided for USB 1 and 2 in usb2_bus.c. Some
|
---|
38 | * details in [OUE]HCI are solved through overriding some functions. XHCI does
|
---|
39 | * not need the bookkeeping functionality, because addresses are managed by HC
|
---|
40 | * itself.
|
---|
41 | */
|
---|
42 | #ifndef LIBUSBHOST_HOST_BUS_H
|
---|
43 | #define LIBUSBHOST_HOST_BUS_H
|
---|
44 |
|
---|
45 | #include <usb/usb.h>
|
---|
46 | #include <usb/request.h>
|
---|
47 |
|
---|
48 | #include <assert.h>
|
---|
49 | #include <fibril_synch.h>
|
---|
50 | #include <stdbool.h>
|
---|
51 |
|
---|
52 | typedef struct hcd hcd_t;
|
---|
53 | typedef struct endpoint endpoint_t;
|
---|
54 | typedef struct bus bus_t;
|
---|
55 | typedef struct ddf_fun ddf_fun_t;
|
---|
56 | typedef struct usb_transfer_batch usb_transfer_batch_t;
|
---|
57 |
|
---|
58 | typedef struct device {
|
---|
59 | /* Device tree keeping */
|
---|
60 | link_t link;
|
---|
61 | list_t devices;
|
---|
62 | fibril_mutex_t guard;
|
---|
63 |
|
---|
64 | /* Associated DDF function, if any */
|
---|
65 | ddf_fun_t *fun;
|
---|
66 |
|
---|
67 | /* Invalid for the roothub device */
|
---|
68 | unsigned port;
|
---|
69 | struct device *hub;
|
---|
70 |
|
---|
71 | /* Transaction translator */
|
---|
72 | usb_tt_address_t tt;
|
---|
73 |
|
---|
74 | /* The following are not set by the library */
|
---|
75 | usb_speed_t speed;
|
---|
76 | usb_address_t address;
|
---|
77 |
|
---|
78 | /* This structure is meant to be extended by overriding. */
|
---|
79 | } device_t;
|
---|
80 |
|
---|
81 | typedef struct {
|
---|
82 | int (*enumerate_device)(bus_t *, hcd_t *, device_t *);
|
---|
83 | int (*remove_device)(bus_t *, hcd_t *, device_t *);
|
---|
84 |
|
---|
85 | int (*online_device)(bus_t *, hcd_t *, device_t *); /**< Optional */
|
---|
86 | int (*offline_device)(bus_t *, hcd_t *, device_t *); /**< Optional */
|
---|
87 |
|
---|
88 | /* The following operations are protected by a bus guard. */
|
---|
89 | endpoint_t *(*create_endpoint)(bus_t *);
|
---|
90 | int (*register_endpoint)(bus_t *, device_t *, endpoint_t *, const usb_endpoint_desc_t *);
|
---|
91 | int (*unregister_endpoint)(bus_t *, endpoint_t *);
|
---|
92 | endpoint_t *(*find_endpoint)(bus_t *, device_t*, usb_target_t, usb_direction_t);
|
---|
93 | void (*destroy_endpoint)(endpoint_t *); /**< Optional */
|
---|
94 | bool (*endpoint_get_toggle)(endpoint_t *); /**< Optional */
|
---|
95 | void (*endpoint_set_toggle)(endpoint_t *, bool); /**< Optional */
|
---|
96 |
|
---|
97 | int (*request_address)(bus_t *, usb_address_t*, bool, usb_speed_t);
|
---|
98 | int (*release_address)(bus_t *, usb_address_t);
|
---|
99 |
|
---|
100 | int (*reset_toggle)(bus_t *, usb_target_t, toggle_reset_mode_t);
|
---|
101 |
|
---|
102 | size_t (*count_bw) (endpoint_t *, size_t);
|
---|
103 |
|
---|
104 | usb_transfer_batch_t *(*create_batch)(bus_t *, endpoint_t *); /**< Optional */
|
---|
105 | void (*destroy_batch)(usb_transfer_batch_t *); /**< Optional */
|
---|
106 | } bus_ops_t;
|
---|
107 |
|
---|
108 | /** Endpoint management structure */
|
---|
109 | typedef struct bus {
|
---|
110 | /* Synchronization of ops */
|
---|
111 | fibril_mutex_t guard;
|
---|
112 |
|
---|
113 | size_t device_size;
|
---|
114 |
|
---|
115 | /* Do not call directly, ops are synchronized. */
|
---|
116 | bus_ops_t ops;
|
---|
117 |
|
---|
118 | /* This structure is meant to be extended by overriding. */
|
---|
119 | } bus_t;
|
---|
120 |
|
---|
121 | void bus_init(bus_t *, size_t);
|
---|
122 | int device_init(device_t *);
|
---|
123 |
|
---|
124 | int device_set_default_name(device_t *);
|
---|
125 |
|
---|
126 | int bus_enumerate_device(bus_t *, hcd_t *, device_t *);
|
---|
127 | int bus_remove_device(bus_t *, hcd_t *, device_t *);
|
---|
128 |
|
---|
129 | int bus_online_device(bus_t *, hcd_t *, device_t *);
|
---|
130 | int bus_offline_device(bus_t *, hcd_t *, device_t *);
|
---|
131 |
|
---|
132 | int bus_add_endpoint(bus_t *, device_t *, const usb_endpoint_desc_t *, endpoint_t **);
|
---|
133 | endpoint_t *bus_find_endpoint(bus_t *, device_t *, usb_target_t, usb_direction_t);
|
---|
134 | int bus_remove_endpoint(bus_t *, endpoint_t *);
|
---|
135 |
|
---|
136 | size_t bus_count_bw(endpoint_t *, size_t);
|
---|
137 |
|
---|
138 | int bus_request_address(bus_t *, usb_address_t *, bool, usb_speed_t);
|
---|
139 | int bus_release_address(bus_t *, usb_address_t);
|
---|
140 |
|
---|
141 |
|
---|
142 | static inline int bus_reserve_default_address(bus_t *bus, usb_speed_t speed) {
|
---|
143 | usb_address_t addr = USB_ADDRESS_DEFAULT;
|
---|
144 |
|
---|
145 | const int r = bus_request_address(bus, &addr, true, speed);
|
---|
146 | assert(addr == USB_ADDRESS_DEFAULT);
|
---|
147 | return r;
|
---|
148 | }
|
---|
149 |
|
---|
150 | static inline int bus_release_default_address(bus_t *bus) {
|
---|
151 | return bus_release_address(bus, USB_ADDRESS_DEFAULT);
|
---|
152 | }
|
---|
153 |
|
---|
154 | int bus_reset_toggle(bus_t *, usb_target_t, bool);
|
---|
155 |
|
---|
156 | #endif
|
---|
157 | /**
|
---|
158 | * @}
|
---|
159 | */
|
---|