1 /* ----------------------------------------------------------------------------
\r
2 * ATMEL Microcontroller Software Support
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2010, Atmel Corporation
\r
6 * All rights reserved.
\r
8 * Redistribution and use in source and binary forms, with or without
\r
9 * modification, are permitted provided that the following conditions are met:
\r
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\r
14 * Atmel's name may not be used to endorse or promote products derived from
\r
15 * this software without specific prior written permission.
\r
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
\r
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
\r
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
27 * ----------------------------------------------------------------------------
\r
33 * Implements for USB requests described by the USB specification.
\r
36 /** \addtogroup usb_request
\r
40 /*------------------------------------------------------------------------------
\r
42 *------------------------------------------------------------------------------*/
\r
44 #include <USBRequests.h>
\r
46 /*------------------------------------------------------------------------------
\r
47 * Exported functions
\r
48 *------------------------------------------------------------------------------*/
\r
51 * Returns the type of the given request.
\r
52 * \param request Pointer to a USBGenericRequest instance.
\r
53 * \return "USB Request Types"
\r
55 extern uint8_t USBGenericRequest_GetType(const USBGenericRequest *request)
\r
57 return ((request->bmRequestType >> 5) & 0x3);
\r
61 * Returns the request code of the given request.
\r
62 * \param request Pointer to a USBGenericRequest instance.
\r
63 * \return Request code.
\r
64 * \sa "USB Request Codes"
\r
66 uint8_t USBGenericRequest_GetRequest(const USBGenericRequest *request)
\r
68 return request->bRequest;
\r
72 * Returns the wValue field of the given request.
\r
73 * \param request - Pointer to a USBGenericRequest instance.
\r
74 * \return Request value.
\r
76 uint16_t USBGenericRequest_GetValue(const USBGenericRequest *request)
\r
78 return request->wValue;
\r
82 * Returns the wIndex field of the given request.
\r
83 * \param request Pointer to a USBGenericRequest instance.
\r
84 * \return Request index;
\r
86 uint16_t USBGenericRequest_GetIndex(const USBGenericRequest *request)
\r
88 return request->wIndex;
\r
92 * Returns the expected length of the data phase following a request.
\r
93 * \param request Pointer to a USBGenericRequest instance.
\r
94 * \return Length of data phase.
\r
96 uint16_t USBGenericRequest_GetLength(const USBGenericRequest *request)
\r
98 return request->wLength;
\r
102 * Returns the endpoint number targetted by a given request.
\r
103 * \param request Pointer to a USBGenericRequest instance.
\r
104 * \return Endpoint number.
\r
106 uint8_t USBGenericRequest_GetEndpointNumber(
\r
107 const USBGenericRequest *request)
\r
109 return USBGenericRequest_GetIndex(request) & 0xF;
\r
113 * Returns the intended recipient of a given request.
\r
114 * \param request Pointer to a USBGenericRequest instance.
\r
115 * \return Request recipient.
\r
116 * \sa "USB Request Recipients"
\r
118 uint8_t USBGenericRequest_GetRecipient(const USBGenericRequest *request)
\r
120 /* Recipient is in bits [0..4] of the bmRequestType field */
\r
121 return request->bmRequestType & 0xF;
\r
125 * Returns the direction of the data transfer following the given request.
\r
126 * \param request Pointer to a USBGenericRequest instance.
\r
127 * \return Transfer direction.
\r
128 * \sa "USB Request Directions"
\r
130 uint8_t USBGenericRequest_GetDirection(const USBGenericRequest *request)
\r
132 /* Transfer direction is located in bit D7 of the bmRequestType field */
\r
133 if ((request->bmRequestType & 0x80) != 0) {
\r
135 return USBGenericRequest_IN;
\r
139 return USBGenericRequest_OUT;
\r
145 * Returns the type of the descriptor requested by the host given the
\r
146 * corresponding GET_DESCRIPTOR request.
\r
147 * \param request Pointer to a USBGenericDescriptor instance.
\r
148 * \return Type of the requested descriptor.
\r
150 uint8_t USBGetDescriptorRequest_GetDescriptorType(
\r
151 const USBGenericRequest *request)
\r
153 /* Requested descriptor type is in the high-byte of the wValue field */
\r
154 return (USBGenericRequest_GetValue(request) >> 8) & 0xFF;
\r
158 * Returns the index of the requested descriptor, given the corresponding
\r
159 * GET_DESCRIPTOR request.
\r
160 * \param request Pointer to a USBGenericDescriptor instance.
\r
161 * \return Index of the requested descriptor.
\r
163 uint8_t USBGetDescriptorRequest_GetDescriptorIndex(
\r
164 const USBGenericRequest *request)
\r
166 /* Requested descriptor index if in the low byte of the wValue field */
\r
167 return USBGenericRequest_GetValue(request) & 0xFF;
\r
172 * Returns the address that the device must take in response to a
\r
173 * SET_ADDRESS request.
\r
174 * \param request Pointer to a USBGenericRequest instance.
\r
175 * \return New device address.
\r
177 uint8_t USBSetAddressRequest_GetAddress(const USBGenericRequest *request)
\r
179 return USBGenericRequest_GetValue(request) & 0x7F;
\r
184 * Returns the number of the configuration that should be set in response
\r
185 * to the given SET_CONFIGURATION request.
\r
186 * \param request Pointer to a USBGenericRequest instance.
\r
187 * \return Number of the requested configuration.
\r
189 uint8_t USBSetConfigurationRequest_GetConfiguration(
\r
190 const USBGenericRequest *request)
\r
192 return USBGenericRequest_GetValue(request);
\r
197 * Indicates which interface is targetted by a GET_INTERFACE or
\r
198 * SET_INTERFACE request.
\r
199 * \param request Pointer to a USBGenericRequest instance.
\r
200 * \return Interface number.
\r
202 uint8_t USBInterfaceRequest_GetInterface(const USBGenericRequest *request)
\r
204 return (USBGenericRequest_GetIndex(request) & 0xFF);
\r
208 * Indicates the new alternate setting that the interface targetted by a
\r
209 * SET_INTERFACE request should use.
\r
210 * \param request Pointer to a USBGenericRequest instance.
\r
211 * \return New active setting for the interface.
\r
213 uint8_t USBInterfaceRequest_GetAlternateSetting(
\r
214 const USBGenericRequest *request)
\r
216 return (USBGenericRequest_GetValue(request) & 0xFF);
\r
221 * Returns the feature selector of a given CLEAR_FEATURE or SET_FEATURE
\r
223 * \param request Pointer to a USBGenericRequest instance.
\r
224 * \return Feature selector.
\r
226 uint8_t USBFeatureRequest_GetFeatureSelector(
\r
227 const USBGenericRequest *request)
\r
229 return USBGenericRequest_GetValue(request);
\r
233 * Indicates the test that the device must undertake following a
\r
234 * SET_FEATURE request.
\r
235 * \param request Pointer to a USBGenericRequest instance.
\r
236 * \return Test selector.
\r
238 uint8_t USBFeatureRequest_GetTestSelector(
\r
239 const USBGenericRequest *request)
\r
241 return (USBGenericRequest_GetIndex(request) >> 8) & 0xFF;
\r