Subversion Repositories NaviCtrl

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
2
* File Name          : usb_core.c
3
* Author             : MCD Application Team
4
* Date First Issued  : 10/27/2003 : V1.0
5
* Description        : Standard protocol processing (USB v2.0)
6
********************************************************************************
7
* History:
8
* 09/18/2006 : V3.0
9
* 09/01/2006 : V2.0
10
* 10/27/2003 : V1.0
11
********************************************************************************
12
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
14
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
15
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
16
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
17
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18
*******************************************************************************/
19
 
20
/* Includes ------------------------------------------------------------------*/
21
#include "usb_lib.h"
22
#include "usb_mem.h"
23
#include "usb_conf.h"
24
/* Private typedef -----------------------------------------------------------*/
25
/* Private define ------------------------------------------------------------*/
26
#define ValBit(VAR,Place)    (VAR & (1<<Place))
27
#define SetBit(VAR,Place)    ( VAR |= (1<<Place) )
28
#define ClrBit(VAR,Place)    ( VAR &= ((1<<Place)^255) )
29
 
30
#define Send0LengthData() {     \
31
        _SetEPTxCount(ENDP0, 0);        \
32
        vSetEPTxStatus(EP_TX_VALID);    \
33
        }
34
 
35
#define vSetEPRxStatus(st)      (SaveRState = st)
36
#define vSetEPTxStatus(st)      (SaveTState = st)
37
 
38
#define USB_StatusIn()  Send0LengthData()
39
#define USB_StatusOut() vSetEPRxStatus(EP_RX_VALID)
40
 
41
#define StatusInfo0 StatusInfo.bw.bb1   /* Reverse bb0 & bb1 */
42
#define StatusInfo1 StatusInfo.bw.bb0
43
/* Private macro -------------------------------------------------------------*/
44
/* Private variables ---------------------------------------------------------*/
45
u16_u8 StatusInfo;
46
/* Private function prototypes -----------------------------------------------*/
47
/* Private functions ---------------------------------------------------------*/
48
 
49
/*******************************************************************************
50
* Function Name  : Standard_GetConfiguration
51
* Description    : Return the current configuration variable address
52
* Input          : Length - How many bytes are needed.
53
* Output         : If "Length" is 0, return the length of configuration value
54
                   If "Length" is not 0, return the data buffer address.
55
* Return         : Return 1 , if the request is invalid when "Length" is 0
56
                   Return "Buffer" if the "Length" is not 0
57
*******************************************************************************/
58
u8 *Standard_GetConfiguration(u16 Length)
59
{
60
  if (Length == 0){
61
    pInformation->Ctrl_Info.Usb_wLength = sizeof(pInformation->Current_Configuration);
62
                return 0;
63
  }
64
        pUser_Standard_Requests->User_GetConfiguration();
65
        return (u8 *)&pInformation->Current_Configuration;
66
}
67
/*******************************************************************************
68
* Function Name  : Standard_SetConfiguration
69
* Description    : This routine is called to set the configuration value
70
                   Then each class should configure device themself
71
* Input          :
72
* Output         :
73
* Return         : Return USB_SUCCESS, if the request is performed
74
                   Return USB_UNSUPPORT, if the request is invalid
75
*******************************************************************************/
76
RESULT Standard_SetConfiguration(void)
77
{
78
 
79
        if (pInformation->USBwValue0 <= Device_Table.Total_Configuration && pInformation->USBwValue1==0
80
            && pInformation->USBwIndex==0) /*call Back usb spec 2.0*/
81
        {
82
                pInformation->Current_Configuration = pInformation->USBwValue0;
83
                pUser_Standard_Requests->User_SetConfiguration();
84
                return USB_SUCCESS;
85
        }
86
        else
87
        return USB_UNSUPPORT;
88
}
89
/*******************************************************************************
90
* Function Name  : Standard_GetInterface
91
* Description    : Return the Alternate Setting of the current interface
92
* Input          : Length - How many bytes are needed
93
* Output         : If "Length" is 0, return the length of interface value
94
                   If "Length" is not 0, return the data buffer address
95
* Return         : Return 0, if the request is invalid when "Length" is 0
96
                   Return "Buffer" if the "Length" is not 0
97
*******************************************************************************/
98
u8 *Standard_GetInterface(u16 Length)
99
{
100
  if (Length == 0){
101
        pInformation->Ctrl_Info.Usb_wLength =   sizeof(pInformation->Current_AlternateSetting);
102
    return 0;
103
  }
104
        pUser_Standard_Requests->User_GetInterface();
105
        return (u8 *)&pInformation->Current_AlternateSetting;
106
}
107
/*******************************************************************************
108
* Function Name  : Standard_SetInterface
109
* Description    : This routine is called to set the interface
110
                   Then each class should configure the interface them self
111
* Input          :
112
* Output         :
113
* Return         : Return USB_SUCCESS, if the request is performed
114
                   Return USB_UNSUPPORT, if the request is invalid
115
*******************************************************************************/
116
RESULT Standard_SetInterface(void)
117
{
118
        RESULT                  Re;
119
        /*Test if the specified Interface and Alternate Setting are supported by the application Firmware*/
120
        Re = (*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0,pInformation->USBwValue0);
121
 
122
        if(pInformation->Current_Configuration!=0 )
123
        {
124
                if (Re!= USB_SUCCESS || pInformation->USBwIndex1!=0 || pInformation->USBwValue1!=0)
125
                                return  USB_UNSUPPORT;
126
                else if ( Re == USB_SUCCESS)
127
                {
128
                 pUser_Standard_Requests->User_SetInterface();
129
                 pInformation->Current_Interface = pInformation->USBwIndex0;
130
                 pInformation->Current_AlternateSetting = pInformation->USBwValue0;
131
                 return USB_SUCCESS;
132
                }
133
 
134
        }
135
 
136
 return USB_UNSUPPORT;
137
}
138
/*******************************************************************************
139
* Function Name  : Standard_GetStatus
140
* Description    : Copy the device request data to "StatusInfo buffer"
141
* Input          : Length - How many bytes are needed
142
* Output         : If "Length" is 0, return the length of status data
143
                   If "Length" is not 0, return number of bytes have been copied
144
* Return         : Return 0, if the request is at end of data block,
145
                   or is invalid when "Length" is 0
146
*******************************************************************************/
147
u8 *Standard_GetStatus(u16 Length)
148
{
149
  if (Length == 0){
150
          pInformation->Ctrl_Info.Usb_wLength =2;
151
                return 0;
152
  }
153
 
154
        StatusInfo.w = 0;
155
                                /* Reset Status Information */
156
 
157
        if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) {/*Get Device Status */
158
                u8      Feature = pInformation->Current_Feature;
159
 
160
                if (ValBit(Feature, 5))
161
                        SetBit(StatusInfo0, 1); /* Remote Wakeup enabled */
162
 
163
 
164
                if (ValBit(Feature, 6))
165
                        SetBit(StatusInfo0, 0);  /* Self-powered */
166
                else
167
                        ClrBit(StatusInfo0, 0); /* Bus-powered */
168
        }
169
        else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))/*Interface Status*/
170
                        return (u8 *)&StatusInfo;
171
 
172
        else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) {/*Get EndPoint Status*/
173
                u8      Related_Endpoint;
174
                u8      wIndex0 = pInformation->USBwIndex0;
175
 
176
                Related_Endpoint = (wIndex0 & 0x0f);
177
                        if (ValBit(wIndex0, 7)) {
178
                                /* IN endpoint */
179
                                if (_GetTxStallStatus( Related_Endpoint ))
180
                                SetBit(StatusInfo0, 0); /* IN Endpoint stalled */
181
                        }
182
                        else {
183
                                /* OUT endpoint */
184
                                if (_GetRxStallStatus( Related_Endpoint ))
185
                                SetBit(StatusInfo0, 0); /* OUT Endpoint stalled */
186
                        }
187
 
188
        }
189
        else
190
                return NULL;
191
        pUser_Standard_Requests->User_GetStatus();
192
        return (u8 *)&StatusInfo;
193
}
194
/*******************************************************************************
195
* Function Name  : Standard_ClearFeature
196
* Description    : Clear or disable a specific feature
197
* Input          :
198
* Output         :
199
* Return         :
200
*******************************************************************************/
201
RESULT Standard_ClearFeature(void)
202
{
203
 
204
        u32     Type_Rec = Type_Recipient;
205
        u32     Status;
206
 
207
 
208
        if ( Type_Rec == (STANDARD_REQUEST | DEVICE_RECIPIENT) ) {/*Device Clear Feature*/
209
                ClrBit(pInformation->Current_Feature, 5);
210
                return USB_SUCCESS;
211
        }
212
        else if ( Type_Rec == (STANDARD_REQUEST | ENDPOINT_RECIPIENT) ) {/*EndPoint Clear Feature*/
213
                DEVICE* pDev;
214
                u32     Related_Endpoint;
215
                u32     wIndex0;
216
                u32     rEP;
217
 
218
                if (pInformation->USBwValue != ENDPOINT_STALL || pInformation->USBwIndex1!=0)
219
                        return USB_UNSUPPORT;
220
 
221
                pDev = &Device_Table;
222
                wIndex0 = pInformation->USBwIndex0;
223
                rEP = wIndex0 & ~0x80;
224
                Related_Endpoint = ENDP0 + rEP;
225
 
226
                if (ValBit(pInformation->USBwIndex0, 7)) Status =_GetEPTxStatus(Related_Endpoint);/*Get Status of endpoint & stall the request if
227
                                                                                                      the related_ENdpoint is Disabled*/
228
                else Status =_GetEPRxStatus(Related_Endpoint);
229
 
230
                if (rEP >= pDev->Total_Endpoint || Status==0 || pInformation->Current_Configuration==0)
231
                        return USB_UNSUPPORT;
232
 
233
 
234
                if (wIndex0 & 0x80) {           /* IN endpoint */
235
                  if (_GetTxStallStatus(Related_Endpoint )){
236
                                ClearDTOG_TX(Related_Endpoint);
237
                                SetEPTxStatus(Related_Endpoint, EP_TX_VALID);
238
                  }
239
                }
240
                else {                                                  /* OUT endpoint */
241
                        if (_GetRxStallStatus(Related_Endpoint)){
242
                                if (Related_Endpoint == ENDP0) {
243
                                        /* After clear the STALL, enable the default endpoint receiver */
244
                                        SetEPRxCount(Related_Endpoint, Device_Property.MaxPacketSize);
245
                                        _SetEPRxStatus(Related_Endpoint, EP_RX_VALID);
246
                                }
247
                                else
248
                                {
249
                                        ClearDTOG_RX(Related_Endpoint);
250
                                        _SetEPRxStatus(Related_Endpoint, EP_RX_VALID);
251
                                }
252
                        }
253
                }
254
                pUser_Standard_Requests->User_ClearFeature();
255
                return USB_SUCCESS;
256
        }
257
 
258
        return USB_UNSUPPORT;
259
}
260
/*******************************************************************************
261
* Function Name  : Standard_SetEndPointFeature
262
* Description    : Set or enable a specific feature of EndPoint
263
* Input          :
264
* Output         :
265
* Return         :
266
*******************************************************************************/
267
RESULT Standard_SetEndPointFeature(void)
268
{
269
        u32    wIndex0;
270
        u32    Related_Endpoint;
271
        u32    rEP;
272
        u32   Status;
273
 
274
                wIndex0 = pInformation->USBwIndex0;
275
                rEP = wIndex0 & ~0x80;
276
                Related_Endpoint = ENDP0 + rEP;
277
 
278
                if (ValBit(pInformation->USBwIndex0, 7)) Status =_GetEPTxStatus(Related_Endpoint);/* get Status of endpoint & stall the request if
279
                                                                                                    the related_ENdpoint is Disabled*/
280
                else Status =_GetEPRxStatus(Related_Endpoint);
281
 
282
 
283
 
284
                if (Related_Endpoint >= Device_Table.Total_Endpoint || pInformation->USBwValue !=0 || Status==0 ||
285
                        pInformation->Current_Configuration==0 /*&& Related_Endpoint!=ENDP0)*/)
286
                        return USB_UNSUPPORT;
287
                else {
288
 
289
                        if (wIndex0 & 0x80) {           /* IN endpoint */
290
                                _SetEPTxStatus(Related_Endpoint, EP_TX_STALL);
291
                        }
292
 
293
                        else {                          /* OUT endpoint */
294
                                _SetEPRxStatus(Related_Endpoint, EP_RX_STALL);
295
                        }
296
                }
297
        pUser_Standard_Requests->User_SetEndPointFeature();
298
        return USB_SUCCESS;
299
}
300
/*******************************************************************************
301
* Function Name  : Standard_SetDeviceFeature
302
* Description    : Set or enable a specific feature of Device
303
* Input          :
304
* Output         :
305
* Return         :
306
*******************************************************************************/
307
RESULT Standard_SetDeviceFeature(void)
308
{
309
                        SetBit(pInformation->Current_Feature, 5);
310
                        pUser_Standard_Requests->User_SetDeviceFeature();
311
                        return USB_SUCCESS;
312
 
313
}
314
 
315
/*******************************************************************************
316
* Function Name  : Standard_GetDescriptorData
317
* Description    : Standard_GetDescriptorData is used for descriptors transfer
318
*                : This routine is used for the descriptors resident in Flash or RAM
319
                  pDesc can be in either Flash or RAM
320
                  The purpose of this routine is to have a versatile way to response
321
                  descriptors request. It allows user to generate certain descriptors
322
                  with software or read descriptors from external storage part by part.
323
* Input          : Length - Length of the data in this transfer
324
                   pDesc - A pointer points to descriptor struct.
325
                          The structure gives the initial address of the
326
                          descriptor and its original size
327
* Output         : Address of a part of the descriptor pointed by the Usb_wOffset
328
                   The buffer pointed by this address contains at least Length bytes
329
* Return         :
330
*******************************************************************************/
331
 
332
u8 *Standard_GetDescriptorData(u16 Length, ONE_DESCRIPTOR *pDesc)
333
{
334
        u32      wOffset;
335
 
336
        wOffset = pInformation->Ctrl_Info.Usb_wOffset;
337
        if (Length == 0) {
338
                pInformation->Ctrl_Info.Usb_wLength = pDesc->Descriptor_Size - wOffset;
339
                return 0;
340
        }
341
 
342
        return pDesc->Descriptor + wOffset;
343
}
344
/*******************************************************************************
345
* Function Name  : DataStageOut
346
* Description    : Data stage of a Control Write Transfer
347
* Input          :
348
* Output         :
349
* Return         :
350
*******************************************************************************/
351
void DataStageOut(void)
352
{
353
        ENDPOINT_INFO   *pEPinfo = &pInformation->Ctrl_Info;
354
        u32     save_rLength;
355
 
356
        save_rLength = pEPinfo->Usb_rLength;
357
 
358
        if (pEPinfo->CopyData && save_rLength) {
359
                u8 *Buffer;
360
                u32 Length;
361
 
362
                Length = pEPinfo->PacketSize;
363
                if (Length > save_rLength)
364
                        Length = save_rLength;
365
 
366
                Buffer = (*pEPinfo->CopyData)(Length);
367
                pEPinfo->Usb_rLength -= Length;
368
                pEPinfo->Usb_rOffset += Length;
369
 
370
                PMAToUserBufferCopy(Buffer,GetEPRxAddr(ENDP0), Length);
371
        }
372
 
373
        if(pEPinfo->Usb_rLength !=0){
374
                vSetEPRxStatus(EP_RX_VALID);/* re-enable for next data reception */
375
                SetEPTxCount(ENDP0, 0);
376
                vSetEPTxStatus(EP_TX_VALID);/* Expect the host to abort the data OUT stage */
377
        }
378
/* Set the next State*/
379
        if (pEPinfo->Usb_rLength >= pEPinfo->PacketSize)
380
                pInformation->ControlState = OUT_DATA;
381
        else
382
                {
383
                        if (pEPinfo->Usb_rLength >0)
384
                                pInformation->ControlState = LAST_OUT_DATA;
385
                        else if (pEPinfo->Usb_rLength == 0)
386
                                {
387
                                pInformation->ControlState = WAIT_STATUS_IN;
388
                                USB_StatusIn();
389
                                }
390
                }
391
 
392
}
393
/*******************************************************************************
394
* Function Name  : DataStageIn
395
* Description    : Data stage of a Control Read Transfer
396
* Input          :
397
* Output         :
398
* Return         :
399
*******************************************************************************/
400
void DataStageIn(void)
401
{
402
        ENDPOINT_INFO   *pEPinfo = &pInformation->Ctrl_Info;
403
        u32     save_wLength = pEPinfo->Usb_wLength;
404
        u32     ControlState = pInformation->ControlState;
405
 
406
        u8      *DataBuffer;
407
        u32     Length;
408
 
409
        if ((save_wLength == 0) && (ControlState == LAST_IN_DATA))
410
        { /* no more data to send so STALL the TX Status*/
411
                ControlState = WAIT_STATUS_OUT;
412
                vSetEPTxStatus(EP_TX_STALL);
413
                goto Expect_Status_Out;
414
        }
415
 
416
        Length = pEPinfo->PacketSize;
417
        ControlState = (save_wLength <= Length) ? LAST_IN_DATA : IN_DATA;
418
 
419
        if (Length > save_wLength)
420
                Length = save_wLength;
421
 
422
        DataBuffer = (*pEPinfo->CopyData)(Length);
423
 
424
        UserToPMABufferCopy(DataBuffer,GetEPTxAddr(ENDP0),Length);
425
 
426
        SetEPTxCount(ENDP0, Length);
427
 
428
        pEPinfo->Usb_wLength -= Length;
429
        pEPinfo->Usb_wOffset += Length;
430
        vSetEPTxStatus(EP_TX_VALID);
431
 
432
        USB_StatusOut();/* Expect the host to abort the data IN stage */
433
 
434
 
435
Expect_Status_Out:
436
        pInformation->ControlState = ControlState;
437
}
438
/*******************************************************************************
439
* Function Name  : NoData_Setup0
440
* Description    : Proceed the processing of setup request without data stage
441
* Input          : None
442
* Output         : None
443
* Return         : None
444
*******************************************************************************/
445
void NoData_Setup0(void)
446
{
447
        RESULT  Result = USB_UNSUPPORT;
448
        u32     RequestNo = pInformation->USBbRequest;
449
        u32     ControlState;
450
 
451
        if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) {/* Device Request*/
452
 
453
                /* SET_CONFIGURATION*/
454
                if (RequestNo == SET_CONFIGURATION)
455
                        Result = Standard_SetConfiguration();
456
 
457
                /*SET ADDRESS*/
458
                else if (RequestNo == SET_ADDRESS){
459
                        if(pInformation->USBwValue0 > 127 || pInformation->USBwValue1!=0
460
                                ||pInformation->USBwIndex!=0 || pInformation->Current_Configuration!=0) /* Device Address should be 127 or less*/
461
                                                                                          /*call Beck spec USB 2.0*/
462
                                {              
463
                                ControlState = STALLED;
464
                                goto exit_NoData_Setup0;
465
                                }
466
                        else Result = USB_SUCCESS;
467
                        }
468
                /*SET FEATURE for Device*/
469
                else if (RequestNo == SET_FEATURE){
470
                                if (pInformation->USBwValue0==DEVICE_REMOTE_WAKEUP && pInformation->USBwIndex==0 && ValBit(pInformation->Current_Feature, 5))
471
                                Result = Standard_SetDeviceFeature();
472
                                else
473
                                Result = USB_UNSUPPORT;
474
                        }
475
                /*Clear FEATURE for Device */
476
                else if (RequestNo == CLEAR_FEATURE){
477
                        if (pInformation->USBwValue0==DEVICE_REMOTE_WAKEUP && pInformation->USBwIndex==0 && ValBit(pInformation->Current_Feature, 5))
478
                        Result = Standard_ClearFeature();
479
                        else
480
                        Result = USB_UNSUPPORT;
481
                }
482
 
483
         }
484
 
485
        /* Interface Request*/
486
 
487
        else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) { 
488
 
489
                        /*SET INTERFACE*/
490
                        if (RequestNo == SET_INTERFACE)
491
                                Result = Standard_SetInterface();
492
        }
493
 
494
 
495
        /* EndPoint Request*/
496
 
497
        else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) {
498
                        /*CLEAR FEATURE for EndPoint*/
499
                        if (RequestNo == CLEAR_FEATURE)
500
                         Result = Standard_ClearFeature();
501
                        /* SET FEATURE for EndPoint*/
502
                         else if (RequestNo == SET_FEATURE){
503
                                Result = Standard_SetEndPointFeature();
504
                        }
505
        }
506
        else
507
                        Result = USB_UNSUPPORT;
508
 
509
 
510
        if (Result != USB_SUCCESS) {
511
                    Result = (*pProperty->Class_NoData_Setup)(RequestNo);
512
                    if (Result == USB_NOT_READY) {
513
                    ControlState = PAUSE;
514
                    goto exit_NoData_Setup0;
515
                }
516
        }
517
 
518
        if (Result != USB_SUCCESS) {
519
                ControlState = STALLED;
520
                goto exit_NoData_Setup0;
521
        }
522
 
523
        ControlState = WAIT_STATUS_IN;/* After no data stage SETUP */
524
 
525
        USB_StatusIn();
526
 
527
exit_NoData_Setup0:
528
        pInformation->ControlState = ControlState;
529
        return;
530
}
531
/*******************************************************************************
532
* Function Name  : Data_Setup0
533
* Description    : Proceed the processing of setup request with data stage
534
* Input          :
535
* Output         :
536
* Return         :
537
*******************************************************************************/
538
void Data_Setup0(void)
539
{
540
        u8      *(*CopyRoutine)(u16);
541
        RESULT  Result;
542
        u32 Request_No = pInformation->USBbRequest;
543
 
544
        u32 Related_Endpoint,Reserved;
545
        u32     wOffset,Status;
546
 
547
 
548
 
549
        CopyRoutine = NULL;
550
        wOffset = 0;
551
 
552
        if (Request_No == GET_DESCRIPTOR) {
553
                if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) {
554
                        u8 wValue1 = pInformation->USBwValue1;
555
                        if (wValue1 == DEVICE_DESCRIPTOR)
556
                                CopyRoutine = pProperty->GetDeviceDescriptor;
557
                        else if (wValue1 == CONFIG_DESCRIPTOR)
558
                                CopyRoutine = pProperty->GetConfigDescriptor;
559
                        else if (wValue1 == STRING_DESCRIPTOR) {
560
                                CopyRoutine = pProperty->GetStringDescriptor;
561
                        }               /* End of GET_DESCRIPTOR */
562
                }
563
        }
564
 
565
        /*GET STATUS*/
566
        else if (Request_No == GET_STATUS && pInformation->USBwValue==0 && pInformation->USBwLength == 0x0002 && pInformation->USBwIndex1==0) {
567
                        /* GET STATUS for Device*/
568
                if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT) && pInformation->USBwIndex==0) {
569
                        CopyRoutine = Standard_GetStatus;
570
                }
571
 
572
                        /* GET STATUS for Interface*/
573
                else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)){
574
                        if ((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0,0)==USB_SUCCESS && pInformation->Current_Configuration!=0)
575
                                         CopyRoutine = Standard_GetStatus;
576
                }
577
 
578
                        /* GET STATUS for EndPoint*/
579
                else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)){
580
 
581
                        Related_Endpoint = (pInformation->USBwIndex0 & 0x0f);
582
                        Reserved= pInformation->USBwIndex0 & 0x70;
583
 
584
                        if (ValBit(pInformation->USBwIndex0, 7)) Status =_GetEPTxStatus(Related_Endpoint);/*Get Status of endpoint & stall the request if
585
                                                                                                            the related_ENdpoint is Disabled*/
586
                        else Status =_GetEPRxStatus(Related_Endpoint);
587
 
588
 
589
 
590
                        if(Related_Endpoint < Device_Table.Total_Endpoint && Reserved==0 && Status != 0)
591
                                        CopyRoutine = Standard_GetStatus;
592
                }
593
 
594
 
595
        }
596
                /*GET CONFIGURATION*/
597
        else if (Request_No == GET_CONFIGURATION) {
598
                if ( Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT) )
599
                        CopyRoutine = Standard_GetConfiguration;
600
        }
601
                /*GET INTERFACE*/
602
        else if (Request_No == GET_INTERFACE) {
603
                if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT) && pInformation->Current_Configuration!=0
604
                   && pInformation->USBwValue==0 && pInformation->USBwIndex1==0 && pInformation->USBwLength == 0x0001
605
                   && (*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0,0)==USB_SUCCESS)                      
606
                        CopyRoutine = Standard_GetInterface;
607
 
608
        }
609
 
610
 
611
        if (CopyRoutine) {
612
                pInformation->Ctrl_Info.Usb_wOffset = wOffset;
613
                pInformation->Ctrl_Info.CopyData = CopyRoutine;
614
                /* sb in the original the cast to word was directly */
615
                /* now the cast is made step by step */
616
                (*CopyRoutine)(0);
617
                Result = USB_SUCCESS;
618
        }
619
       else {
620
                Result = (*pProperty->Class_Data_Setup)(pInformation->USBbRequest);
621
                if (Result == USB_NOT_READY) {
622
                        pInformation->ControlState = PAUSE;
623
                        return;
624
                }
625
        }
626
 
627
        if (pInformation->Ctrl_Info.Usb_wLength == 0xFFFF) {    /* Data is not ready, wait it */
628
                pInformation->ControlState = PAUSE;
629
                return;
630
        }
631
        if (Result == USB_UNSUPPORT || pInformation->Ctrl_Info.Usb_wLength == 0) {
632
                /* Unsupported request */
633
                pInformation->ControlState = STALLED;
634
                return;
635
        }
636
 
637
 
638
        if (ValBit(pInformation->USBbmRequestType, 7)) {
639
                /* Device ==> Host */
640
                u32     wLength = pInformation->USBwLength;
641
 
642
                /* Restrict the data length to be the one host asks */
643
                if (pInformation->Ctrl_Info.Usb_wLength > wLength)
644
                        pInformation->Ctrl_Info.Usb_wLength = wLength;
645
 
646
                pInformation->Ctrl_Info.PacketSize = pProperty->MaxPacketSize;
647
                DataStageIn();
648
        }
649
        else {
650
                pInformation->ControlState = OUT_DATA;
651
                vSetEPRxStatus(EP_RX_VALID);/* enable for next data reception */       
652
        }
653
 
654
        return;
655
} /* Data_Setup0 */
656
/*******************************************************************************
657
* Function Name  : Setup0_Process
658
* Description    : Get the device request data and dispatch to individual process
659
* Input          :
660
* Output         :
661
* Return         :
662
*******************************************************************************/
663
u8 Setup0_Process(void)
664
{
665
#ifdef STR7xx /* STR7xx family*/
666
  union {
667
        u8*     b;
668
        u16*    w;
669
        } pBuf;
670
  pBuf.b = PMAAddr + (u8 *)(_GetEPRxAddr(ENDP0)*2); /* *2 for 32 bits addr */
671
 
672
  if (pInformation->ControlState != PAUSE) {
673
        pInformation->USBbmRequestType = *pBuf.b++;     /* bmRequestType */
674
        pInformation->USBbRequest               = *pBuf.b++;    /* bRequest */
675
        pBuf.w++;  /* word not accessed because of 32 bits addressing */
676
        pInformation->USBwValue         = ByteSwap(*pBuf.w++);  /* wValue */
677
        pBuf.w++;  /* word not accessed because of 32 bits addressing */
678
        pInformation->USBwIndex         = ByteSwap(*pBuf.w++);  /* wIndex */
679
        pBuf.w++;  /* word not accessed because of 32 bits addressing */
680
        pInformation->USBwLength                = *pBuf.w;      /* wLength */
681
  }
682
#endif/* End of STR7xx family*/
683
 
684
#ifdef STR91x /* STR91x family*/
685
  u16*    pBuf;
686
 
687
  pBuf= (u16 *)(GetEPRxAddr(ENDP0)+PMAAddr);
688
  if (pInformation->ControlState != PAUSE)
689
  {
690
    pInformation->USBbmRequestType = (*pBuf)&0xFF; /* bmRequestType */
691
    pInformation->USBbRequest = ((*pBuf)&0xFF00)>>8; /* bRequest */
692
    pInformation->USBwValue = ByteSwap(*(pBuf+1)); /* wValue */
693
    pInformation->USBwIndex = ByteSwap(*(pBuf+2)); /* wIndex */
694
    pInformation->USBwLength = *(pBuf+3);  /* wLength */
695
  }
696
#endif /* End of STR91x family*/
697
  pInformation->ControlState = SETTING_UP;
698
  if (pInformation->USBwLength == 0)
699
  {
700
    /* Setup with no data stage */
701
    NoData_Setup0();
702
  }
703
  else
704
  {
705
    /* Setup with data stage */
706
    Data_Setup0();
707
  }
708
  return Post0_Process();
709
}
710
/*******************************************************************************
711
* Function Name  : In0_Process
712
* Description    : Process the IN token on all default endpoint
713
* Input          :
714
* Output         :
715
* Return         :
716
*******************************************************************************/
717
u8 In0_Process(void)
718
{
719
        u32     ControlState = pInformation->ControlState;
720
 
721
        if (ControlState == IN_DATA || ControlState == LAST_IN_DATA)
722
        {
723
                DataStageIn();
724
                ControlState = pInformation->ControlState; /* may be changed outside the function */
725
        }
726
 
727
        else if (ControlState == WAIT_STATUS_IN) {
728
                if (pInformation->USBbRequest == SET_ADDRESS &&
729
                                Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT) )
730
                {
731
                        SetDeviceAddress(pInformation->USBwValue0);
732
                        pUser_Standard_Requests->User_SetDeviceAddress();
733
                }
734
                (*pProperty->Process_Status_IN)();
735
                ControlState = STALLED;
736
        }
737
 
738
        else
739
        ControlState = STALLED;
740
 
741
        pInformation->ControlState = ControlState;
742
 
743
        return Post0_Process();
744
}
745
/*******************************************************************************
746
* Function Name  : Out0_Process
747
* Description    : Process the OUT token on all default endpoint
748
* Input          :
749
* Output         :
750
* Return         :
751
*******************************************************************************/
752
u8 Out0_Process(void)
753
{
754
        u32     ControlState = pInformation->ControlState;
755
 
756
        if(ControlState == OUT_DATA || ControlState == LAST_OUT_DATA)
757
                DataStageOut();
758
 
759
        else if (ControlState == WAIT_STATUS_OUT) {
760
                (*pProperty->Process_Status_OUT)();    
761
                ControlState = STALLED;
762
                }
763
 
764
        else if (ControlState == IN_DATA || ControlState == LAST_IN_DATA) {
765
                /* host aborts the transfer before finish */
766
                                ControlState = STALLED;
767
        }
768
 
769
 
770
        /* Unexpect state, STALL the endpoint */
771
        else {
772
 
773
                ControlState = STALLED;
774
        }
775
 
776
        pInformation->ControlState = ControlState;
777
 
778
        return Post0_Process();
779
} /* Out0_Process */
780
 
781
/*******************************************************************************
782
* Function Name  : Post0_Process
783
* Description    :
784
* Input          :
785
* Output         :
786
* Return         :
787
*******************************************************************************/
788
u8 Post0_Process(void)
789
{
790
        SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
791
 
792
        if (pInformation->ControlState == STALLED) {
793
                vSetEPRxStatus(EP_RX_STALL);
794
                vSetEPTxStatus(EP_TX_STALL);
795
        }
796
 
797
        return (pInformation->ControlState == PAUSE);
798
}
799
/*******************************************************************************
800
* Function Name  : SetDeviceAddress
801
* Description    : set the device and all the used Endpoints addresses
802
* Input          : Val : device adress
803
* Output         :
804
* Return         :
805
*******************************************************************************/
806
void SetDeviceAddress(u8 Val)
807
{
808
  u32 i;
809
  u32   nEP = Device_Table.Total_Endpoint;
810
 
811
        /* set address in every used endpoint */
812
        for(i=0;i<nEP;i++)
813
        {
814
                _SetEPAddress((u8)i, (u8)i);
815
        } /* for */
816
        _SetDADDR(Val|DADDR_EF); /* set device address and enable function */
817
 
818
}
819
/*******************************************************************************
820
* Function Name  : NOP_Process
821
* Description    : no operation function.
822
* Input          :
823
* Output         :
824
* Return         :
825
*******************************************************************************/
826
void NOP_Process(void)
827
{
828
}
829
/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/
830
 
831