source: mainline/uspace/drv/bus/usb/usbhub/port_status.h@ 75eb6735

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

usbhub: Refactor handling of global hub events.

  • Property mode set to 100644
File size: 9.5 KB
Line 
1/*
2 * Copyright (c) 2010 Matus Dekanek
3 * Copyright (c) 2011 Jan Vesely
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/** @addtogroup drvusbhub
30 * @{
31 */
32
33#ifndef HUB_PORT_STATUS_H
34#define HUB_PORT_STATUS_H
35
36#include <bool.h>
37#include <sys/types.h>
38#include <usb/dev/request.h>
39
40/**
41 * structure holding port status and changes flags.
42 * should not be accessed directly, use supplied getter/setter methods.
43 *
44 * For more information refer to table 11-15 in
45 * "Universal Serial Bus Specification Revision 1.1"
46 *
47 */
48typedef uint32_t usb_port_status_t;
49
50/**
51 * structure holding hub status and changes flags.
52 *
53 * For more information refer to table 11.16.2.5 in
54 * "Universal Serial Bus Specification Revision 1.1"
55 *
56 */
57typedef uint32_t usb_hub_status_t;
58// TODO Mind the endiannes, changes are in the first byte of the second word
59// status is int he first byte of the first word
60#define USB_HUB_STATUS_OVER_CURRENT \
61 (1 << (USB_HUB_FEATURE_HUB_OVER_CURRENT))
62#define USB_HUB_STATUS_LOCAL_POWER \
63 (1 << (USB_HUB_FEATURE_HUB_LOCAL_POWER))
64
65#define USB_HUB_STATUS_C_OVER_CURRENT \
66 (1 << (16 + USB_HUB_FEATURE_C_HUB_OVER_CURRENT))
67#define USB_HUB_STATUS_C_LOCAL_POWER \
68 (1 << (16 + USB_HUB_FEATURE_C_HUB_LOCAL_POWER))
69
70/**
71 * set values in request to be it a port status request
72 * @param request
73 * @param port
74 */
75static inline void usb_hub_set_port_status_request(
76 usb_device_request_setup_packet_t *request, uint16_t port)
77{
78 request->index = port;
79 request->request_type = USB_HUB_REQ_TYPE_GET_PORT_STATUS;
80 request->request = USB_HUB_REQUEST_GET_STATUS;
81 request->value = 0;
82 request->length = 4;
83}
84
85/**
86 * create request for usb hub port status
87 * @param port
88 * @return
89 */
90static inline usb_device_request_setup_packet_t *
91 usb_hub_create_port_status_request(uint16_t port)
92{
93 usb_device_request_setup_packet_t *result =
94 malloc(sizeof(usb_device_request_setup_packet_t));
95 usb_hub_set_port_status_request(result, port);
96 return result;
97}
98
99/**
100 * set the device request to be a port feature enable request
101 * @param request
102 * @param port
103 * @param feature_selector
104 */
105static inline void usb_hub_set_enable_port_feature_request(
106 usb_device_request_setup_packet_t *request, uint16_t port,
107 uint16_t feature_selector) {
108 request->index = port;
109 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
110 request->request = USB_HUB_REQUEST_SET_FEATURE;
111 request->value = feature_selector;
112 request->length = 0;
113}
114
115/**
116 * set the device request to be a port feature clear request
117 * @param request
118 * @param port
119 * @param feature_selector
120 */
121static inline void usb_hub_set_disable_port_feature_request(
122 usb_device_request_setup_packet_t *request, uint16_t port,
123 uint16_t feature_selector
124 ) {
125 request->index = port;
126 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
127 request->request = USB_HUB_REQUEST_CLEAR_FEATURE;
128 request->value = feature_selector;
129 request->length = 0;
130}
131
132/**
133 * set the device request to be a port enable request
134 * @param request
135 * @param port
136 */
137static inline void usb_hub_set_enable_port_request(
138 usb_device_request_setup_packet_t *request, uint16_t port
139 ) {
140 request->index = port;
141 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
142 request->request = USB_HUB_REQUEST_SET_FEATURE;
143 request->value = USB_HUB_FEATURE_C_PORT_ENABLE;
144 request->length = 0;
145}
146
147/**
148 * enable specified port
149 * @param port
150 * @return
151 */
152static inline usb_device_request_setup_packet_t *
153usb_hub_create_enable_port_request(uint16_t port) {
154 usb_device_request_setup_packet_t *result =
155 malloc(sizeof (usb_device_request_setup_packet_t));
156 usb_hub_set_enable_port_request(result, port);
157 return result;
158}
159
160/**
161 * set the device request to be a port disable request
162 * @param request
163 * @param port
164 */
165static inline void usb_hub_set_disable_port_request(
166 usb_device_request_setup_packet_t *request, uint16_t port
167 ) {
168 request->index = port;
169 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
170 request->request = USB_HUB_REQUEST_SET_FEATURE;
171 request->value = USB_HUB_FEATURE_C_PORT_SUSPEND;
172 request->length = 0;
173}
174
175/**
176 * disable specified port
177 * @param port
178 * @return
179 */
180static inline usb_device_request_setup_packet_t *
181usb_hub_create_disable_port_request(uint16_t port) {
182 usb_device_request_setup_packet_t *result =
183 malloc(sizeof (usb_device_request_setup_packet_t));
184 usb_hub_set_disable_port_request(result, port);
185 return result;
186}
187
188/**
189 * set the device request to be a port disable request
190 * @param request
191 * @param port
192 */
193static inline void usb_hub_set_reset_port_request(
194 usb_device_request_setup_packet_t *request, uint16_t port
195 ) {
196 request->index = port;
197 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
198 request->request = USB_HUB_REQUEST_SET_FEATURE;
199 request->value = USB_HUB_FEATURE_PORT_RESET;
200 request->length = 0;
201}
202
203/**
204 * disable specified port
205 * @param port
206 * @return
207 */
208static inline usb_device_request_setup_packet_t *
209usb_hub_create_reset_port_request(uint16_t port) {
210 usb_device_request_setup_packet_t *result =
211 malloc(sizeof (usb_device_request_setup_packet_t));
212 usb_hub_set_reset_port_request(result, port);
213 return result;
214}
215
216/**
217 * set the device request to be a port disable request
218 * @param request
219 * @param port
220 */
221static inline void usb_hub_set_power_port_request(
222 usb_device_request_setup_packet_t *request, uint16_t port
223 ) {
224 request->index = port;
225 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
226 request->request = USB_HUB_REQUEST_SET_FEATURE;
227 request->value = USB_HUB_FEATURE_PORT_POWER;
228 request->length = 0;
229}
230
231/**
232 * set the device request to be a port disable request
233 * @param request
234 * @param port
235 */
236static inline void usb_hub_unset_power_port_request(
237 usb_device_request_setup_packet_t *request, uint16_t port
238 ) {
239 request->index = port;
240 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
241 request->request = USB_HUB_REQUEST_CLEAR_FEATURE;
242 request->value = USB_HUB_FEATURE_PORT_POWER;
243 request->length = 0;
244}
245
246/**
247 * get i`th bit of port status
248 *
249 * @param status
250 * @param idx
251 * @return
252 */
253static inline bool usb_port_is_status(usb_port_status_t status, int idx) {
254 return (status & (1 << idx)) != 0;
255}
256
257/**
258 * set i`th bit of port status
259 *
260 * @param status
261 * @param idx
262 * @param value
263 */
264static inline void usb_port_status_set_bit(
265 usb_port_status_t * status, int idx, bool value) {
266 (*status) = value ?
267 ((*status) | (1 << (idx))) :
268 ((*status)&(~(1 << (idx))));
269}
270
271/**
272 * get i`th bit of hub status
273 *
274 * @param status
275 * @param idx
276 * @return
277 */
278static inline bool usb_hub_is_status(usb_hub_status_t status, int idx) {
279 return (status & (1 << idx)) != 0;
280}
281
282/**
283 * set i`th bit of hub status
284 *
285 * @param status
286 * @param idx
287 * @param value
288 */
289static inline void usb_hub_status_set_bit(
290 usb_hub_status_t *status, int idx, bool value) {
291 (*status) = value ?
292 ((*status) | (1 << (idx))) :
293 ((*status)&(~(1 << (idx))));
294}
295
296/**
297 * low speed device on the port indicator
298 *
299 * @param status
300 * @return true if low speed device is attached
301 */
302static inline bool usb_port_low_speed(usb_port_status_t status) {
303 return usb_port_is_status(status, 9);
304}
305
306/**
307 * set low speed device connected bit in port status
308 *
309 * @param status
310 * @param low_speed value of the bit
311 */
312static inline void usb_port_set_low_speed(usb_port_status_t *status, bool low_speed) {
313 usb_port_status_set_bit(status, 9, low_speed);
314}
315
316//high speed device attached
317
318/**
319 * high speed device on the port indicator
320 *
321 * @param status
322 * @return true if high speed device is on port
323 */
324static inline bool usb_port_high_speed(usb_port_status_t status) {
325 return usb_port_is_status(status, 10);
326}
327
328/**
329 * set high speed device bit in port status
330 *
331 * @param status
332 * @param high_speed value of the bit
333 */
334static inline void usb_port_set_high_speed(usb_port_status_t *status, bool high_speed) {
335 usb_port_status_set_bit(status, 10, high_speed);
336}
337
338/**
339 * speed getter for port status
340 *
341 * @param status
342 * @return speed of usb device (for more see usb specification)
343 */
344static inline usb_speed_t usb_port_speed(usb_port_status_t status) {
345 if (usb_port_low_speed(status))
346 return USB_SPEED_LOW;
347 if (usb_port_high_speed(status))
348 return USB_SPEED_HIGH;
349 return USB_SPEED_FULL;
350}
351
352
353
354#endif /* HUB_PORT_STATUS_H */
355/**
356 * @}
357 */
Note: See TracBrowser for help on using the repository browser.