OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [CORTEX_LPC1768_IAR/] [LPCUSB/] [usbcontrol.c] - Blame information for rev 581

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.