port_status.h

00001 /*
00002  * Copyright (c) 2010 Matus Dekanek
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00032 #ifndef HUB_PORT_STATUS_H
00033 #define HUB_PORT_STATUS_H
00034 
00035 #include <bool.h>
00036 #include <sys/types.h>
00037 #include <usb/dev/request.h>
00038 #include "usbhub_private.h"
00039 
00048 typedef uint32_t usb_port_status_t;
00049 
00058 typedef uint32_t usb_hub_status_t;
00059 
00065 static inline void usb_hub_set_port_status_request(
00066     usb_device_request_setup_packet_t *request, uint16_t port) {
00067         request->index = port;
00068         request->request_type = USB_HUB_REQ_TYPE_GET_PORT_STATUS;
00069         request->request = USB_HUB_REQUEST_GET_STATUS;
00070         request->value = 0;
00071         request->length = 4;
00072 }
00073 
00079 static inline void usb_hub_set_hub_status_request(
00080     usb_device_request_setup_packet_t *request) {
00081         request->index = 0;
00082         request->request_type = USB_HUB_REQ_TYPE_GET_HUB_STATUS;
00083         request->request = USB_HUB_REQUEST_GET_STATUS;
00084         request->value = 0;
00085         request->length = 4;
00086 }
00087 
00093 static inline usb_device_request_setup_packet_t *
00094 usb_hub_create_port_status_request(uint16_t port) {
00095         usb_device_request_setup_packet_t *result =
00096             malloc(sizeof (usb_device_request_setup_packet_t));
00097         usb_hub_set_port_status_request(result, port);
00098         return result;
00099 }
00100 
00107 static inline void usb_hub_set_enable_port_feature_request(
00108     usb_device_request_setup_packet_t *request, uint16_t port,
00109     uint16_t feature_selector) {
00110         request->index = port;
00111         request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
00112         request->request = USB_HUB_REQUEST_SET_FEATURE;
00113         request->value = feature_selector;
00114         request->length = 0;
00115 }
00116 
00123 static inline void usb_hub_set_disable_port_feature_request(
00124     usb_device_request_setup_packet_t *request, uint16_t port,
00125     uint16_t feature_selector
00126     ) {
00127         request->index = port;
00128         request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
00129         request->request = USB_HUB_REQUEST_CLEAR_FEATURE;
00130         request->value = feature_selector;
00131         request->length = 0;
00132 }
00133 
00139 static inline void usb_hub_set_enable_port_request(
00140     usb_device_request_setup_packet_t *request, uint16_t port
00141     ) {
00142         request->index = port;
00143         request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
00144         request->request = USB_HUB_REQUEST_SET_FEATURE;
00145         request->value = USB_HUB_FEATURE_C_PORT_ENABLE;
00146         request->length = 0;
00147 }
00148 
00154 static inline usb_device_request_setup_packet_t *
00155 usb_hub_create_enable_port_request(uint16_t port) {
00156         usb_device_request_setup_packet_t *result =
00157             malloc(sizeof (usb_device_request_setup_packet_t));
00158         usb_hub_set_enable_port_request(result, port);
00159         return result;
00160 }
00161 
00167 static inline void usb_hub_set_disable_port_request(
00168     usb_device_request_setup_packet_t *request, uint16_t port
00169     ) {
00170         request->index = port;
00171         request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
00172         request->request = USB_HUB_REQUEST_SET_FEATURE;
00173         request->value = USB_HUB_FEATURE_C_PORT_SUSPEND;
00174         request->length = 0;
00175 }
00176 
00182 static inline usb_device_request_setup_packet_t *
00183 usb_hub_create_disable_port_request(uint16_t port) {
00184         usb_device_request_setup_packet_t *result =
00185             malloc(sizeof (usb_device_request_setup_packet_t));
00186         usb_hub_set_disable_port_request(result, port);
00187         return result;
00188 }
00189 
00195 static inline void usb_hub_set_reset_port_request(
00196     usb_device_request_setup_packet_t *request, uint16_t port
00197     ) {
00198         request->index = port;
00199         request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
00200         request->request = USB_HUB_REQUEST_SET_FEATURE;
00201         request->value = USB_HUB_FEATURE_PORT_RESET;
00202         request->length = 0;
00203 }
00204 
00210 static inline usb_device_request_setup_packet_t *
00211 usb_hub_create_reset_port_request(uint16_t port) {
00212         usb_device_request_setup_packet_t *result =
00213             malloc(sizeof (usb_device_request_setup_packet_t));
00214         usb_hub_set_reset_port_request(result, port);
00215         return result;
00216 }
00217 
00223 static inline void usb_hub_set_power_port_request(
00224     usb_device_request_setup_packet_t *request, uint16_t port
00225     ) {
00226         request->index = port;
00227         request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
00228         request->request = USB_HUB_REQUEST_SET_FEATURE;
00229         request->value = USB_HUB_FEATURE_PORT_POWER;
00230         request->length = 0;
00231 }
00232 
00238 static inline void usb_hub_unset_power_port_request(
00239     usb_device_request_setup_packet_t *request, uint16_t port
00240     ) {
00241         request->index = port;
00242         request->request_type = USB_HUB_REQ_TYPE_SET_PORT_FEATURE;
00243         request->request = USB_HUB_REQUEST_CLEAR_FEATURE;
00244         request->value = USB_HUB_FEATURE_PORT_POWER;
00245         request->length = 0;
00246 }
00247 
00255 static inline bool usb_port_is_status(usb_port_status_t status, int idx) {
00256         return (status & (1 << idx)) != 0;
00257 }
00258 
00266 static inline void usb_port_status_set_bit(
00267     usb_port_status_t * status, int idx, bool value) {
00268         (*status) = value ?
00269             ((*status) | (1 << (idx))) :
00270             ((*status)&(~(1 << (idx))));
00271 }
00272 
00280 static inline bool usb_hub_is_status(usb_hub_status_t status, int idx) {
00281         return (status & (1 << idx)) != 0;
00282 }
00283 
00291 static inline void usb_hub_status_set_bit(
00292     usb_hub_status_t *status, int idx, bool value) {
00293         (*status) = value ?
00294             ((*status) | (1 << (idx))) :
00295             ((*status)&(~(1 << (idx))));
00296 }
00297 
00304 static inline bool usb_port_low_speed(usb_port_status_t status) {
00305         return usb_port_is_status(status, 9);
00306 }
00307 
00314 static inline void usb_port_set_low_speed(usb_port_status_t *status, bool low_speed) {
00315         usb_port_status_set_bit(status, 9, low_speed);
00316 }
00317 
00318 //high speed device attached
00319 
00326 static inline bool usb_port_high_speed(usb_port_status_t status) {
00327         return usb_port_is_status(status, 10);
00328 }
00329 
00336 static inline void usb_port_set_high_speed(usb_port_status_t *status, bool high_speed) {
00337         usb_port_status_set_bit(status, 10, high_speed);
00338 }
00339 
00346 static inline usb_speed_t usb_port_speed(usb_port_status_t status) {
00347         if (usb_port_low_speed(status))
00348                 return USB_SPEED_LOW;
00349         if (usb_port_high_speed(status))
00350                 return USB_SPEED_HIGH;
00351         return USB_SPEED_FULL;
00352 }
00353 
00354 
00355 
00356 #endif  /* HUB_PORT_STATUS_H */
00357 

Generated on Thu Jun 2 07:45:44 2011 for HelenOS/USB by  doxygen 1.4.7