]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D3x_Xplained_IAR/AtmelFiles/usb/common/core/USBRequests.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D3x_Xplained_IAR / AtmelFiles / usb / common / core / USBRequests.c
1 /* ----------------------------------------------------------------------------\r
2  *         ATMEL Microcontroller Software Support \r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2010, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\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
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\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
16  *\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
28  */\r
29 \r
30 /** \file\r
31  *  \section Purpose\r
32  * \r
33  *    Implements for USB requests described by the USB specification.\r
34  */\r
35 \r
36 /** \addtogroup usb_request\r
37  * @{\r
38  */\r
39 \r
40 /*------------------------------------------------------------------------------\r
41  *         Headers\r
42  *------------------------------------------------------------------------------*/\r
43      \r
44 #include <USBRequests.h>\r
45 \r
46 /*------------------------------------------------------------------------------\r
47  *         Exported functions\r
48  *------------------------------------------------------------------------------*/\r
49 \r
50 /**\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
54  */\r
55 extern uint8_t USBGenericRequest_GetType(const USBGenericRequest *request)\r
56 {\r
57     return ((request->bmRequestType >> 5) & 0x3);\r
58 }\r
59 \r
60 /**\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
65  */\r
66 uint8_t USBGenericRequest_GetRequest(const USBGenericRequest *request)\r
67 {\r
68     return request->bRequest;\r
69 }\r
70 \r
71 /**\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
75  */\r
76 uint16_t USBGenericRequest_GetValue(const USBGenericRequest *request)\r
77 {\r
78     return request->wValue;\r
79 }\r
80 \r
81 /**\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
85  */\r
86 uint16_t USBGenericRequest_GetIndex(const USBGenericRequest *request)\r
87 {\r
88     return request->wIndex;\r
89 }\r
90 \r
91 /**\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
95  */\r
96 uint16_t USBGenericRequest_GetLength(const USBGenericRequest *request)\r
97 {\r
98     return request->wLength;\r
99 }\r
100 \r
101 /**\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
105  */\r
106 uint8_t USBGenericRequest_GetEndpointNumber(\r
107     const USBGenericRequest *request)\r
108 {\r
109     return USBGenericRequest_GetIndex(request) & 0xF;\r
110 }\r
111 \r
112 /**\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
117  */\r
118 uint8_t USBGenericRequest_GetRecipient(const USBGenericRequest *request)\r
119 {\r
120     /* Recipient is in bits [0..4] of the bmRequestType field */\r
121     return request->bmRequestType & 0xF;\r
122 }\r
123 \r
124 /**\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
129  */\r
130 uint8_t USBGenericRequest_GetDirection(const USBGenericRequest *request)\r
131 {\r
132     /* Transfer direction is located in bit D7 of the bmRequestType field */\r
133     if ((request->bmRequestType & 0x80) != 0) {\r
134 \r
135         return USBGenericRequest_IN;\r
136     }\r
137     else {\r
138 \r
139         return USBGenericRequest_OUT;\r
140     }\r
141 }\r
142 \r
143 \r
144 /**\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
149  */\r
150 uint8_t USBGetDescriptorRequest_GetDescriptorType(\r
151     const USBGenericRequest *request)\r
152 {\r
153     /* Requested descriptor type is in the high-byte of the wValue field */\r
154     return (USBGenericRequest_GetValue(request) >> 8) & 0xFF;\r
155 }\r
156 \r
157 /**\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
162  */\r
163 uint8_t USBGetDescriptorRequest_GetDescriptorIndex(\r
164     const USBGenericRequest *request)\r
165 {\r
166     /* Requested descriptor index if in the low byte of the wValue field */\r
167     return USBGenericRequest_GetValue(request) & 0xFF;\r
168 }\r
169 \r
170 \r
171 /**\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
176  */\r
177 uint8_t USBSetAddressRequest_GetAddress(const USBGenericRequest *request)\r
178 {\r
179     return USBGenericRequest_GetValue(request) & 0x7F;\r
180 }\r
181 \r
182 \r
183 /**\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
188  */\r
189 uint8_t USBSetConfigurationRequest_GetConfiguration(\r
190     const USBGenericRequest *request)\r
191 {\r
192     return USBGenericRequest_GetValue(request);\r
193 }\r
194 \r
195 \r
196 /**\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
201  */\r
202 uint8_t USBInterfaceRequest_GetInterface(const USBGenericRequest *request)\r
203 {\r
204     return (USBGenericRequest_GetIndex(request) & 0xFF);\r
205 }\r
206 \r
207 /**\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
212  */\r
213 uint8_t USBInterfaceRequest_GetAlternateSetting(\r
214     const USBGenericRequest *request)\r
215 {\r
216     return (USBGenericRequest_GetValue(request) & 0xFF);\r
217 }\r
218 \r
219 \r
220 /**\r
221  *  Returns the feature selector of a given CLEAR_FEATURE or SET_FEATURE\r
222  *  request.\r
223  *  \param request Pointer to a USBGenericRequest instance.\r
224  *  \return Feature selector.\r
225  */\r
226 uint8_t USBFeatureRequest_GetFeatureSelector(\r
227     const USBGenericRequest *request)\r
228 {\r
229     return USBGenericRequest_GetValue(request);\r
230 }\r
231 \r
232 /**\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
237  */\r
238 uint8_t USBFeatureRequest_GetTestSelector(\r
239     const USBGenericRequest *request)\r
240 {\r
241     return (USBGenericRequest_GetIndex(request) >> 8) & 0xFF;\r
242 }\r
243 \r
244 /**@}*/\r