00001 /* ---------------------------------------------------------------------------- */ 00002 /* Atmel Microcontroller Software Support */ 00003 /* SAM Software Package License */ 00004 /* ---------------------------------------------------------------------------- */ 00005 /* Copyright (c) 2015, Atmel Corporation */ 00006 /* */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without */ 00010 /* modification, are permitted provided that the following condition is met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, */ 00013 /* this list of conditions and the disclaimer below. */ 00014 /* */ 00015 /* Atmel's name may not be used to endorse or promote products derived from */ 00016 /* this software without specific prior written permission. */ 00017 /* */ 00018 /* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR */ 00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ 00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE */ 00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, */ 00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ 00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */ 00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ 00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ 00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */ 00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 00028 /* ---------------------------------------------------------------------------- */ 00029 00030 /** \file 00031 * \section Purpose 00032 * 00033 * Implements for USB requests described by the USB specification. 00034 */ 00035 00036 /** \addtogroup usb_request 00037 * @{ 00038 */ 00039 00040 /*------------------------------------------------------------------------------ 00041 * Headers 00042 *------------------------------------------------------------------------------*/ 00043 #include <USBRequests.h> 00044 00045 /*------------------------------------------------------------------------------ 00046 * Exported functions 00047 *------------------------------------------------------------------------------*/ 00048 00049 /** 00050 * Returns the type of the given request. 00051 * \param request Pointer to a USBGenericRequest instance. 00052 * \return "USB Request Types" 00053 */ 00054 extern uint8_t USBGenericRequest_GetType(const USBGenericRequest *request) 00055 { 00056 return ((request->bmRequestType >> 5) & 0x3); 00057 } 00058 00059 /** 00060 * Returns the request code of the given request. 00061 * \param request Pointer to a USBGenericRequest instance. 00062 * \return Request code. 00063 * \sa "USB Request Codes" 00064 */ 00065 uint8_t USBGenericRequest_GetRequest(const USBGenericRequest *request) 00066 { 00067 return request->bRequest; 00068 } 00069 00070 /** 00071 * Returns the wValue field of the given request. 00072 * \param request - Pointer to a USBGenericRequest instance. 00073 * \return Request value. 00074 */ 00075 uint16_t USBGenericRequest_GetValue(const USBGenericRequest *request) 00076 { 00077 return request->wValue; 00078 } 00079 00080 /** 00081 * Returns the wIndex field of the given request. 00082 * \param request Pointer to a USBGenericRequest instance. 00083 * \return Request index; 00084 */ 00085 uint16_t USBGenericRequest_GetIndex(const USBGenericRequest *request) 00086 { 00087 return request->wIndex; 00088 } 00089 00090 /** 00091 * Returns the expected length of the data phase following a request. 00092 * \param request Pointer to a USBGenericRequest instance. 00093 * \return Length of data phase. 00094 */ 00095 uint16_t USBGenericRequest_GetLength(const USBGenericRequest *request) 00096 { 00097 return request->wLength; 00098 } 00099 00100 /** 00101 * Returns the endpoint number targeted by a given request. 00102 * \param request Pointer to a USBGenericRequest instance. 00103 * \return Endpoint number. 00104 */ 00105 uint8_t USBGenericRequest_GetEndpointNumber( 00106 const USBGenericRequest *request) 00107 { 00108 return USBGenericRequest_GetIndex(request) & 0xF; 00109 } 00110 00111 /** 00112 * Returns the intended recipient of a given request. 00113 * \param request Pointer to a USBGenericRequest instance. 00114 * \return Request recipient. 00115 * \sa "USB Request Recipients" 00116 */ 00117 uint8_t USBGenericRequest_GetRecipient(const USBGenericRequest *request) 00118 { 00119 /* Recipient is in bits [0..4] of the bmRequestType field */ 00120 return request->bmRequestType & 0xF; 00121 } 00122 00123 /** 00124 * Returns the direction of the data transfer following the given request. 00125 * \param request Pointer to a USBGenericRequest instance. 00126 * \return Transfer direction. 00127 * \sa "USB Request Directions" 00128 */ 00129 uint8_t USBGenericRequest_GetDirection(const USBGenericRequest *request) 00130 { 00131 /* Transfer direction is located in bit D7 of the bmRequestType field */ 00132 if ((request->bmRequestType & 0x80) != 0) 00133 return USBGenericRequest_IN; 00134 else 00135 return USBGenericRequest_OUT; 00136 } 00137 00138 00139 /** 00140 * Returns the type of the descriptor requested by the host given the 00141 * corresponding GET_DESCRIPTOR request. 00142 * \param request Pointer to a USBGenericDescriptor instance. 00143 * \return Type of the requested descriptor. 00144 */ 00145 uint8_t USBGetDescriptorRequest_GetDescriptorType( 00146 const USBGenericRequest *request) 00147 { 00148 /* Requested descriptor type is in the high-byte of the wValue field */ 00149 return (USBGenericRequest_GetValue(request) >> 8) & 0xFF; 00150 } 00151 00152 /** 00153 * Returns the index of the requested descriptor, given the corresponding 00154 * GET_DESCRIPTOR request. 00155 * \param request Pointer to a USBGenericDescriptor instance. 00156 * \return Index of the requested descriptor. 00157 */ 00158 uint8_t USBGetDescriptorRequest_GetDescriptorIndex( 00159 const USBGenericRequest *request) 00160 { 00161 /* Requested descriptor index if in the low byte of the wValue field */ 00162 return USBGenericRequest_GetValue(request) & 0xFF; 00163 } 00164 00165 00166 /** 00167 * Returns the address that the device must take in response to a 00168 * SET_ADDRESS request. 00169 * \param request Pointer to a USBGenericRequest instance. 00170 * \return New device address. 00171 */ 00172 uint8_t USBSetAddressRequest_GetAddress(const USBGenericRequest *request) 00173 { 00174 return USBGenericRequest_GetValue(request) & 0x7F; 00175 } 00176 00177 00178 /** 00179 * Returns the number of the configuration that should be set in response 00180 * to the given SET_CONFIGURATION request. 00181 * \param request Pointer to a USBGenericRequest instance. 00182 * \return Number of the requested configuration. 00183 */ 00184 uint8_t USBSetConfigurationRequest_GetConfiguration( 00185 const USBGenericRequest *request) 00186 { 00187 return USBGenericRequest_GetValue(request); 00188 } 00189 00190 00191 /** 00192 * Indicates which interface is targeted by a GET_INTERFACE or 00193 * SET_INTERFACE request. 00194 * \param request Pointer to a USBGenericRequest instance. 00195 * \return Interface number. 00196 */ 00197 uint8_t USBInterfaceRequest_GetInterface(const USBGenericRequest *request) 00198 { 00199 return (USBGenericRequest_GetIndex(request) & 0xFF); 00200 } 00201 00202 /** 00203 * Indicates the new alternate setting that the interface targeted by a 00204 * SET_INTERFACE request should use. 00205 * \param request Pointer to a USBGenericRequest instance. 00206 * \return New active setting for the interface. 00207 */ 00208 uint8_t USBInterfaceRequest_GetAlternateSetting( 00209 const USBGenericRequest *request) 00210 { 00211 return (USBGenericRequest_GetValue(request) & 0xFF); 00212 } 00213 00214 00215 /** 00216 * Returns the feature selector of a given CLEAR_FEATURE or SET_FEATURE 00217 * request. 00218 * \param request Pointer to a USBGenericRequest instance. 00219 * \return Feature selector. 00220 */ 00221 uint8_t USBFeatureRequest_GetFeatureSelector( 00222 const USBGenericRequest *request) 00223 { 00224 return USBGenericRequest_GetValue(request); 00225 } 00226 00227 /** 00228 * Indicates the test that the device must undertake following a 00229 * SET_FEATURE request. 00230 * \param request Pointer to a USBGenericRequest instance. 00231 * \return Test selector. 00232 */ 00233 uint8_t USBFeatureRequest_GetTestSelector( 00234 const USBGenericRequest *request) 00235 { 00236 return (USBGenericRequest_GetIndex(request) >> 8) & 0xFF; 00237 } 00238 00239 /**@}*/