]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LPC1768_IAR/LPCUSB/usbcontrol.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / CORTEX_LPC1768_IAR / LPCUSB / usbcontrol.c
1 /*\r
2         LPCUSB, an USB device driver for LPC microcontrollers   \r
3         Copyright (C) 2006 Bertrik Sikken (bertrik@sikken.nl)\r
4 \r
5         Redistribution and use in source and binary forms, with or without\r
6         modification, are permitted provided that the following conditions are met:\r
7 \r
8         1. Redistributions of source code must retain the above copyright\r
9            notice, this list of conditions and the following disclaimer.\r
10         2. Redistributions in binary form must reproduce the above copyright\r
11            notice, this list of conditions and the following disclaimer in the\r
12            documentation and/or other materials provided with the distribution.\r
13         3. The name of the author may not be used to endorse or promote products\r
14            derived from this software without specific prior written permission.\r
15 \r
16         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
17         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
18         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
19         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
20         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
21         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
22         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
23         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
24         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
25         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
26 */\r
27 \r
28 \r
29 /** @file\r
30         Control transfer handler.\r
31         \r
32         This module handles control transfers and is normally installed on the\r
33         endpoint 0 callback.\r
34         \r
35         Control transfers can be of the following type:\r
36         0 Standard;\r
37         1 Class;\r
38         2 Vendor;\r
39         3 Reserved.\r
40 \r
41         A callback can be installed for each of these control transfers using\r
42         USBRegisterRequestHandler.\r
43         When an OUT request arrives, data is collected in the data store provided\r
44         with the USBRegisterRequestHandler call. When the transfer is done, the\r
45         callback is called.\r
46         When an IN request arrives, the callback is called immediately to either\r
47         put the control transfer data in the data store, or to get a pointer to\r
48         control transfer data. The data is then packetised and sent to the host.\r
49 */\r
50 \r
51 #include "usbdebug.h"\r
52 \r
53 #include "usbstruct.h"\r
54 #include "usbapi.h"\r
55 \r
56 \r
57 \r
58 #define MAX_CONTROL_SIZE        128     /**< maximum total size of control transfer data */\r
59 #define MAX_REQ_HANDLERS        4       /**< standard, class, vendor, reserved */\r
60 \r
61 static TSetupPacket             Setup;  /**< setup packet */\r
62 \r
63 static unsigned char                            *pbData;        /**< pointer to data buffer */\r
64 static int                              iResidue;       /**< remaining bytes in buffer */\r
65 static int                              iLen;           /**< total length of control transfer */\r
66 \r
67 /** Array of installed request handler callbacks */\r
68 static TFnHandleRequest *apfnReqHandlers[4] = {NULL, NULL, NULL, NULL};\r
69 /** Array of installed request data pointers */\r
70 static unsigned char                            *apbDataStore[4] = {NULL, NULL, NULL, NULL};\r
71 \r
72 /**\r
73         Local function to handle a request by calling one of the installed\r
74         request handlers.\r
75                 \r
76         In case of data going from host to device, the data is at *ppbData.\r
77         In case of data going from device to host, the handler can either\r
78         choose to write its data at *ppbData or update the data pointer.\r
79                 \r
80         @param [in]             pSetup          The setup packet\r
81         @param [in,out] *piLen          Pointer to data length\r
82         @param [in,out] ppbData         Data buffer.\r
83 \r
84         @return TRUE if the request was handles successfully\r
85  */\r
86 static BOOL _HandleRequest(TSetupPacket *pSetup, int *piLen, unsigned char **ppbData)\r
87 {\r
88         TFnHandleRequest *pfnHandler;\r
89         int iType;\r
90         \r
91         iType = REQTYPE_GET_TYPE(pSetup->bmRequestType);\r
92         pfnHandler = apfnReqHandlers[iType];\r
93         if (pfnHandler == NULL) {\r
94                 DBG("No handler for reqtype %d\n", iType);\r
95                 return FALSE;\r
96         }\r
97 \r
98         return pfnHandler(pSetup, piLen, ppbData);\r
99 }\r
100 \r
101 \r
102 /**\r
103         Local function to stall the control endpoint\r
104         \r
105         @param [in]     bEPStat Endpoint status\r
106  */\r
107 static void StallControlPipe(unsigned char bEPStat)\r
108 {\r
109         unsigned char   *pb;\r
110         int     i;\r
111 \r
112         USBHwEPStall(0x80, TRUE);\r
113 \r
114 // dump setup packet\r
115         DBG("STALL on [");\r
116         pb = (unsigned char *)&Setup;\r
117         for (i = 0; i < 8; i++) {\r
118                 DBG(" %02x", *pb++);\r
119         }\r
120         DBG("] stat=%x\n", bEPStat);\r
121 }\r
122 \r
123 \r
124 /**\r
125         Sends next chunk of data (possibly 0 bytes) to host\r
126  */\r
127 static void DataIn(void)\r
128 {\r
129         int iChunk;\r
130 \r
131         if( MAX_PACKET_SIZE0 < iResidue )\r
132         {\r
133                 iChunk = MAX_PACKET_SIZE0;\r
134         }\r
135         else\r
136         {\r
137                 iChunk = iResidue;\r
138         }\r
139 \r
140         USBHwEPWrite(0x80, pbData, iChunk);\r
141         pbData += iChunk;\r
142         iResidue -= iChunk;\r
143 }\r
144 \r
145 \r
146 /**\r
147  *      Handles IN/OUT transfers on EP0\r
148  *\r
149  *      @param [in]     bEP             Endpoint address\r
150  *      @param [in]     bEPStat Endpoint status\r
151  */\r
152 void USBHandleControlTransfer(unsigned char bEP, unsigned char bEPStat)\r
153 {\r
154         int iChunk, iType;\r
155 \r
156         if (bEP == 0x00) {\r
157                 // OUT transfer\r
158                 if (bEPStat & EP_STATUS_SETUP) {\r
159                         // setup packet, reset request message state machine\r
160                         USBHwEPRead(0x00, (unsigned char *)&Setup, sizeof(Setup));\r
161                         DBG("S%x", Setup.bRequest);\r
162 \r
163                         // defaults for data pointer and residue\r
164                         iType = REQTYPE_GET_TYPE(Setup.bmRequestType);\r
165                         pbData = apbDataStore[iType];\r
166                         iResidue = Setup.wLength;\r
167                         iLen = Setup.wLength;\r
168 \r
169                         if ((Setup.wLength == 0) ||\r
170                                 (REQTYPE_GET_DIR(Setup.bmRequestType) == REQTYPE_DIR_TO_HOST)) {\r
171                                 // ask installed handler to process request\r
172                                 if (!_HandleRequest(&Setup, &iLen, &pbData)) {\r
173                                         DBG("_HandleRequest1 failed\n");\r
174                                         StallControlPipe(bEPStat);\r
175                                         return;\r
176                                 }\r
177                                 // send smallest of requested and offered length\r
178                                 if( iLen < Setup.wLength )\r
179                                 {\r
180                                         iResidue = iLen;\r
181                                 }\r
182                                 else\r
183                                 {\r
184                                         iResidue = Setup.wLength;\r
185                                 }\r
186 \r
187                                 // send first part (possibly a zero-length status message)\r
188                                 DataIn();\r
189                         }\r
190                 }\r
191                 else {          \r
192                         if (iResidue > 0) {\r
193                                 // store data\r
194                                 iChunk = USBHwEPRead(0x00, pbData, iResidue);\r
195                                 if (iChunk < 0) {\r
196                                         StallControlPipe(bEPStat);\r
197                                         return;\r
198                                 }\r
199                                 pbData += iChunk;\r
200                                 iResidue -= iChunk;\r
201                                 if (iResidue == 0) {\r
202                                         // received all, send data to handler\r
203                                         iType = REQTYPE_GET_TYPE(Setup.bmRequestType);\r
204                                         pbData = apbDataStore[iType];\r
205                                         if (!_HandleRequest(&Setup, &iLen, &pbData)) {\r
206                                                 DBG("_HandleRequest2 failed\n");\r
207                                                 StallControlPipe(bEPStat);\r
208                                                 return;\r
209                                         }\r
210                                         // send status to host\r
211                                         DataIn();\r
212                                 }\r
213                         }\r
214                         else {\r
215                                 // absorb zero-length status message\r
216                                 iChunk = USBHwEPRead(0x00, NULL, 0);\r
217                                 DBG(iChunk > 0 ? "?" : "");\r
218                         }\r
219                 }\r
220         }\r
221         else if (bEP == 0x80) {\r
222                 // IN transfer\r
223                 // send more data if available (possibly a 0-length packet)\r
224                 DataIn();\r
225         }\r
226         else {\r
227                 ASSERT(FALSE);\r
228         }\r
229 }\r
230 \r
231 \r
232 /**\r
233         Registers a callback for handling requests\r
234                 \r
235         @param [in]     iType                   Type of request, e.g. REQTYPE_TYPE_STANDARD\r
236         @param [in]     *pfnHandler             Callback function pointer\r
237         @param [in]     *pbDataStore    Data storage area for this type of request\r
238  */\r
239 void USBRegisterRequestHandler(int iType, TFnHandleRequest *pfnHandler, unsigned char *pbDataStore)\r
240 {\r
241         ASSERT(iType >= 0);\r
242         ASSERT(iType < 4);\r
243         apfnReqHandlers[iType] = pfnHandler;\r
244         apbDataStore[iType] = pbDataStore;\r
245 }\r
246 \r