source: mainline/uspace/drv/bus/usb/usbhub/port_status.h@ 193da9d6

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

usbhub: Rename usbhub_private.h ⇒ utils.h.

Add error checks, comments, and slight refactoring.

  • 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 * should not be accessed directly, use supplied getter/setter methods.
53 *
54 * For more information refer to table 11.16.2.5 in
55 * "Universal Serial Bus Specification Revision 1.1"
56 *
57 */
58typedef uint32_t usb_hub_status_t;
59
60/**
61 * set values in request to be it a port status request
62 * @param request
63 * @param port
64 */
65static inline void usb_hub_set_port_status_request(
66 usb_device_request_setup_packet_t *request, uint16_t port) {
67 request->index = port;
68 request->request_type = USB_HUB_REQ_TYPE_GET_PORT_STATUS;
69 request->request = USB_HUB_REQUEST_GET_STATUS;
70 request->value = 0;
71 request->length = 4;
72}
73
74/**
75 * set values in request to be it a port status request
76 * @param request
77 * @param port
78 */
79static inline void usb_hub_set_hub_status_request(
80 usb_device_request_setup_packet_t *request) {
81 request->index = 0;
82 request->request_type = USB_HUB_REQ_TYPE_GET_HUB_STATUS;
83 request->request = USB_HUB_REQUEST_GET_STATUS;
84 request->value = 0;
85 request->length = 4;
86}
87
88/**
89 * create request for usb hub port status
90 * @param port
91 * @return
92 */
93static inline usb_device_request_setup_packet_t *
94usb_hub_create_port_status_request(uint16_t port) {
95 usb_device_request_setup_packet_t *result =
96 malloc(sizeof (usb_device_request_setup_packet_t));
97 usb_hub_set_port_status_request(result, port);
98 return result;
99}
100
101/**
102 * set the device request to be a port feature enable request
103 * @param request
104 * @param port
105 * @param feature_selector
106 */
107static inline void usb_hub_set_enable_port_feature_request(
108 usb_device_request_setup_packet_t *request, uint16_t port,
109 uint16_t feature_selector) {
110 request->index = port;
111 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
112 request->request = USB_HUB_REQUEST_SET_FEATURE;
113 request->value = feature_selector;
114 request->length = 0;
115}
116
117/**
118 * set the device request to be a port feature clear request
119 * @param request
120 * @param port
121 * @param feature_selector
122 */
123static inline void usb_hub_set_disable_port_feature_request(
124 usb_device_request_setup_packet_t *request, uint16_t port,
125 uint16_t feature_selector
126 ) {
127 request->index = port;
128 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
129 request->request = USB_HUB_REQUEST_CLEAR_FEATURE;
130 request->value = feature_selector;
131 request->length = 0;
132}
133
134/**
135 * set the device request to be a port enable request
136 * @param request
137 * @param port
138 */
139static inline void usb_hub_set_enable_port_request(
140 usb_device_request_setup_packet_t *request, uint16_t port
141 ) {
142 request->index = port;
143 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
144 request->request = USB_HUB_REQUEST_SET_FEATURE;
145 request->value = USB_HUB_FEATURE_C_PORT_ENABLE;
146 request->length = 0;
147}
148
149/**
150 * enable specified port
151 * @param port
152 * @return
153 */
154static inline usb_device_request_setup_packet_t *
155usb_hub_create_enable_port_request(uint16_t port) {
156 usb_device_request_setup_packet_t *result =
157 malloc(sizeof (usb_device_request_setup_packet_t));
158 usb_hub_set_enable_port_request(result, port);
159 return result;
160}
161
162/**
163 * set the device request to be a port disable request
164 * @param request
165 * @param port
166 */
167static inline void usb_hub_set_disable_port_request(
168 usb_device_request_setup_packet_t *request, uint16_t port
169 ) {
170 request->index = port;
171 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
172 request->request = USB_HUB_REQUEST_SET_FEATURE;
173 request->value = USB_HUB_FEATURE_C_PORT_SUSPEND;
174 request->length = 0;
175}
176
177/**
178 * disable specified port
179 * @param port
180 * @return
181 */
182static inline usb_device_request_setup_packet_t *
183usb_hub_create_disable_port_request(uint16_t port) {
184 usb_device_request_setup_packet_t *result =
185 malloc(sizeof (usb_device_request_setup_packet_t));
186 usb_hub_set_disable_port_request(result, port);
187 return result;
188}
189
190/**
191 * set the device request to be a port disable request
192 * @param request
193 * @param port
194 */
195static inline void usb_hub_set_reset_port_request(
196 usb_device_request_setup_packet_t *request, uint16_t port
197 ) {
198 request->index = port;
199 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
200 request->request = USB_HUB_REQUEST_SET_FEATURE;
201 request->value = USB_HUB_FEATURE_PORT_RESET;
202 request->length = 0;
203}
204
205/**
206 * disable specified port
207 * @param port
208 * @return
209 */
210static inline usb_device_request_setup_packet_t *
211usb_hub_create_reset_port_request(uint16_t port) {
212 usb_device_request_setup_packet_t *result =
213 malloc(sizeof (usb_device_request_setup_packet_t));
214 usb_hub_set_reset_port_request(result, port);
215 return result;
216}
217
218/**
219 * set the device request to be a port disable request
220 * @param request
221 * @param port
222 */
223static inline void usb_hub_set_power_port_request(
224 usb_device_request_setup_packet_t *request, uint16_t port
225 ) {
226 request->index = port;
227 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
228 request->request = USB_HUB_REQUEST_SET_FEATURE;
229 request->value = USB_HUB_FEATURE_PORT_POWER;
230 request->length = 0;
231}
232
233/**
234 * set the device request to be a port disable request
235 * @param request
236 * @param port
237 */
238static inline void usb_hub_unset_power_port_request(
239 usb_device_request_setup_packet_t *request, uint16_t port
240 ) {
241 request->index = port;
242 request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
243 request->request = USB_HUB_REQUEST_CLEAR_FEATURE;
244 request->value = USB_HUB_FEATURE_PORT_POWER;
245 request->length = 0;
246}
247
248/**
249 * get i`th bit of port status
250 *
251 * @param status
252 * @param idx
253 * @return
254 */
255static inline bool usb_port_is_status(usb_port_status_t status, int idx) {
256 return (status & (1 << idx)) != 0;
257}
258
259/**
260 * set i`th bit of port status
261 *
262 * @param status
263 * @param idx
264 * @param value
265 */
266static inline void usb_port_status_set_bit(
267 usb_port_status_t * status, int idx, bool value) {
268 (*status) = value ?
269 ((*status) | (1 << (idx))) :
270 ((*status)&(~(1 << (idx))));
271}
272
273/**
274 * get i`th bit of hub status
275 *
276 * @param status
277 * @param idx
278 * @return
279 */
280static inline bool usb_hub_is_status(usb_hub_status_t status, int idx) {
281 return (status & (1 << idx)) != 0;
282}
283
284/**
285 * set i`th bit of hub status
286 *
287 * @param status
288 * @param idx
289 * @param value
290 */
291static inline void usb_hub_status_set_bit(
292 usb_hub_status_t *status, int idx, bool value) {
293 (*status) = value ?
294 ((*status) | (1 << (idx))) :
295 ((*status)&(~(1 << (idx))));
296}
297
298/**
299 * low speed device on the port indicator
300 *
301 * @param status
302 * @return true if low speed device is attached
303 */
304static inline bool usb_port_low_speed(usb_port_status_t status) {
305 return usb_port_is_status(status, 9);
306}
307
308/**
309 * set low speed device connected bit in port status
310 *
311 * @param status
312 * @param low_speed value of the bit
313 */
314static inline void usb_port_set_low_speed(usb_port_status_t *status, bool low_speed) {
315 usb_port_status_set_bit(status, 9, low_speed);
316}
317
318//high speed device attached
319
320/**
321 * high speed device on the port indicator
322 *
323 * @param status
324 * @return true if high speed device is on port
325 */
326static inline bool usb_port_high_speed(usb_port_status_t status) {
327 return usb_port_is_status(status, 10);
328}
329
330/**
331 * set high speed device bit in port status
332 *
333 * @param status
334 * @param high_speed value of the bit
335 */
336static inline void usb_port_set_high_speed(usb_port_status_t *status, bool high_speed) {
337 usb_port_status_set_bit(status, 10, high_speed);
338}
339
340/**
341 * speed getter for port status
342 *
343 * @param status
344 * @return speed of usb device (for more see usb specification)
345 */
346static inline usb_speed_t usb_port_speed(usb_port_status_t status) {
347 if (usb_port_low_speed(status))
348 return USB_SPEED_LOW;
349 if (usb_port_high_speed(status))
350 return USB_SPEED_HIGH;
351 return USB_SPEED_FULL;
352}
353
354
355
356#endif /* HUB_PORT_STATUS_H */
357
358/**
359 * @}
360 */
Note: See TracBrowser for help on using the repository browser.