c5509_usb_reqhndlr.c
上传用户:dahaojd
上传日期:2008-01-29
资源大小:14357k
文件大小:11k
- /*
- * Copyright 2003 by Texas Instruments Incorporated.
- * All rights reserved. Property of Texas Instruments Incorporated.
- * Restricted rights to use, duplicate or disclose this code are
- * granted through contract.
- *
- */
- /* "@(#) DDK 1.10.00.23 07-02-03 (ddk-b12)" */
- /*
- * ======== c5509_usb_reqhndlr.c ========
- * This file defines USB Chapter 9 default device request handlers.
- */
- #include <std.h>
- #include <string.h>
- #include <csl.h>
- #include <csl_usb.h>
- #include <_c5509_usb.h>
- #include <c5509_usb.h>
- /*
- * data buffer to send/receive status data to host.
- * first 2 bytes for xfer byte count next 8 bytes are for usb data
- */
- static Uint16 genPurposeBuffer[5];
- /*
- * USB requests handlers
- */
- static C5509_USB_UsbReqRet usbReqGetDescriptor();
- static C5509_USB_UsbReqRet usbReqSetAddress();
- static C5509_USB_UsbReqRet usbReqSetConfiguration();
- static C5509_USB_UsbReqRet usbReqGetInterface();
- static C5509_USB_UsbReqRet usbReqGetConfiguration();
- static C5509_USB_UsbReqRet usbReqGetStatus();
- static C5509_USB_UsbReqRet usbReqClearSetFeature();
- static C5509_USB_UsbReqRet usbReqSetInterface();
- /*
- * ======== standard USB Request Table ========
- * USB control endpoint 0 parses through this table and pass the
- * appropriate default handler via callback to application.
- */
- _C5509_USB_UsbRequestStruct _C5509_USB_usbReqTable[] = {
- { _C5509_USB_REQUEST_GET_STATUS , usbReqGetStatus },
- { _C5509_USB_REQUEST_CLEAR_FEATURE , usbReqClearSetFeature },
- { _C5509_USB_REQUEST_SET_FEATURE , usbReqClearSetFeature },
- { _C5509_USB_REQUEST_SET_ADDRESS , usbReqSetAddress },
- { _C5509_USB_REQUEST_GET_DESCRIPTOR , usbReqGetDescriptor },
- { _C5509_USB_REQUEST_SET_DESCRIPTOR , _C5509_USB_usbReqUnknown },
- { _C5509_USB_REQUEST_GET_CONFIGURATION , usbReqGetConfiguration },
- { _C5509_USB_REQUEST_SET_CONFIGURATION , usbReqSetConfiguration },
- { _C5509_USB_REQUEST_GET_INTERFACE , usbReqGetInterface },
- { _C5509_USB_REQUEST_SET_INTERFACE , usbReqSetInterface },
- { _C5509_USB_REQUEST_SYNC_FRAME , _C5509_USB_usbReqUnknown },
- { 0, NULL } /* table must terminate with a NULL member */
- };
- /*
- * ======== usbReqSetAddress ========
- * set device address request
- */
- static C5509_USB_UsbReqRet usbReqSetAddress()
- {
- /*
- * set new device address sent in
- * the wValue field of the setup packet
- */
- USB_setDevAddr(USB0, (Uint16)(_C5509_USB_usbSetup.wValue));
-
- return( C5509_USB_REQUEST_SEND_ACK );
- }
- /*
- * ======== usbReqSetConfiguration ========
- * Set/clear active configuration of the USB device
- */
- static C5509_USB_UsbReqRet usbReqSetConfiguration()
- {
- C5509_USB_UsbReqRet retStat;
- /* single configuration supported only */
- if((_C5509_USB_usbSetup.wValue ==0) || (_C5509_USB_usbSetup.wValue ==1)) {
- _C5509_USB_devObj.stateInfo.usbCurConfig = _C5509_USB_usbSetup.wValue;
-
- retStat = C5509_USB_REQUEST_SEND_ACK;
- }
- else {
-
- retStat = C5509_USB_REQUEST_STALL;
- }
- return (retStat);
- }
- /*
- * ======== usbReqSetInterface ========
- * Set/clear active interface of the USB device
- */
- static C5509_USB_UsbReqRet usbReqSetInterface()
- {
- C5509_USB_UsbReqRet retStat = C5509_USB_REQUEST_STALL;
- /*
- * decode the requested feature
- * only Interface 0 and single configuration supported
- */
- if((_C5509_USB_usbSetup.wIndex == 0) &&
- (_C5509_USB_devObj.stateInfo.usbCurConfig == 1)) {
- if((_C5509_USB_usbSetup.wValue==0) || (_C5509_USB_usbSetup.wValue==1)) {
- _C5509_USB_devObj.stateInfo.usbCurAltSet = _C5509_USB_usbSetup.wValue;
- retStat = C5509_USB_REQUEST_SEND_ACK;
- }
- }
- return( retStat );
- }
- /*
- * ======== usbReqClearSetFeature ========
- * Clear or Set standard device features
- */
- static C5509_USB_UsbReqRet usbReqClearSetFeature()
- {
- USB_EpHandle hEPx;
- Uint16 endpt;
- C5509_USB_UsbReqRet retStat = C5509_USB_REQUEST_SEND_ACK;
- USB_Boolean bFeature = USB_TRUE; /* USB_TRUE, set feature */
- if( _C5509_USB_devObj.lastRequest == _C5509_USB_REQUEST_CLEAR_FEATURE ){
- /* clear or set feature?*/
- bFeature = USB_FALSE;
- }
-
- /* decode the requested feature */
- switch (_C5509_USB_usbSetup.wValue) {
- case _C5509_USB_FEATURE_ENDPOINT_STALL:
-
- /* retrieve the endpoint number */
- endpt = (_C5509_USB_usbSetup.wIndex) & 0xFF;
-
- /* retrieve the handle associated with the endpoint */
- hEPx = USB_epNumToHandle( USB0, endpt );
-
- if (bFeature) {
- /* stall endpoint */
- USB_stallEndpt( hEPx );
- }
- else {
- /* clear stall of the endpoint */
- USB_clearEndptStall(hEPx);
- }
- break;
-
- case _C5509_USB_FEATURE_REMOTE_WAKEUP:
-
- /* disable / enable remote wakeup */
- USB_setRemoteWakeup(USB0, bFeature);
- break;
-
- default:
-
- retStat = C5509_USB_REQUEST_STALL;
-
- } /* end switch */
- return(retStat);
- }
- /*
- * ======== usbReqGetStatus ========
- * Handle standard device request GET_STATUS
- */
- static C5509_USB_UsbReqRet usbReqGetStatus()
- {
- USB_EpHandle hEPx;
- Uint16 endpt;
-
- switch (_C5509_USB_usbSetup.bmRequestType - 0x80) {
- case 0: /* return Device Status */
-
- genPurposeBuffer[1] =
- (((Uint16)USB_getRemoteWakeupStat(USB0) ) << 1) |
- _C5509_USB_devObj.stateInfo.usbCurDev;
- break;
-
- case 1: /* return Interface status */
-
- genPurposeBuffer[1] = _C5509_USB_devObj.stateInfo.usbCurIntrfc;
- break;
-
- case 2: /* return Endpoint status */
-
- endpt = (_C5509_USB_usbSetup.wIndex) & 0xFF;
- hEPx = USB_epNumToHandle( USB0, endpt );
- genPurposeBuffer[1] = (Uint16)USB_getEndptStall(hEPx);
- break;
-
- default:
- /*
- * Unsupported feature.
- */
- return (C5509_USB_REQUEST_STALL);
- }
-
- /* post transaction to send the requested data to host */
- USB_postTransaction( &_C5509_USB_usbEpObjIn0, 2,
- genPurposeBuffer, USB_IOFLAG_NONE);
-
- return ( C5509_USB_REQUEST_GET_ACK );
- }
- /*
- * ======== usbReqGetConfigurationInterface ========
- * Return current device configuration value
- */
- static C5509_USB_UsbReqRet usbReqGetConfiguration()
- {
- /* Send the current Configuration Value */
- genPurposeBuffer[1] = _C5509_USB_devObj.stateInfo.usbCurConfig;
-
- USB_postTransaction( &_C5509_USB_usbEpObjIn0, 1, genPurposeBuffer,
- USB_IOFLAG_NONE | USB_IOFLAG_NOSHORT );
- return( C5509_USB_REQUEST_GET_ACK );
- }
- /*
- * ======== usbReqGetConfigurationInterface ========
- * Return current interface alternate set value
- */
- static C5509_USB_UsbReqRet usbReqGetInterface()
- {
-
- /* Send the current Interface alternate set Value */
- genPurposeBuffer[1] = _C5509_USB_devObj.stateInfo.usbCurAltSet;
-
- USB_postTransaction( &_C5509_USB_usbEpObjIn0, 1, genPurposeBuffer,
- USB_IOFLAG_NONE | USB_IOFLAG_NOSHORT );
- return( C5509_USB_REQUEST_GET_ACK );
- }
- /*
- * ======== usbReqGetDescriptor ========
- * Return requested descriptor
- */
- static C5509_USB_UsbReqRet usbReqGetDescriptor( )
- {
- Uint16 dval;
- Uint16 inoutFlag = USB_IOFLAG_NONE ;
- Bool setInitDone = FALSE;
- String * strDesc;
- Void * dataPtr;
- C5509_USB_ChanHandle chan;
- Int i;
- switch (_C5509_USB_usbSetup.wValue >> 8) {
- case C5509_USB_DESCRIPTOR_DEVICE: /* send device descriptor */
-
- dataPtr = _C5509_USB_devParams->deviceConfig->deviceDesc;
- dval = ((Uint16*)dataPtr)[1] & 0xFF;
- break;
- case C5509_USB_DESCRIPTOR_CONFIG: /* send config descriptor */
-
- /* config descriptor is made of linked lists */
- inoutFlag = ( USB_IOFLAG_LNK | USB_IOFLAG_CAT );
- dataPtr = _C5509_USB_devParams->ifcConfig->usbConfig;
- dval = _C5509_USB_devParams->ifcConfig->usbConfig->pBuffer[2];
- if ((dval == _C5509_USB_usbSetup.wLength) ||
- ((_C5509_USB_usbSetup.wLength % _C5509_USB_EP0RSVDSIZE) == 0 )) {
- inoutFlag |= USB_IOFLAG_NOSHORT;
- }
-
- setInitDone = TRUE;
- break;
- case C5509_USB_DESCRIPTOR_STRING: /* send string descriptor */
- if( (_C5509_USB_usbSetup.wValue & 0xFF) == 0) {
- /* LANGID Language Codes */
-
- dataPtr = _C5509_USB_devParams->deviceConfig->stringDescLangId;
- dval = ((Uint16*)dataPtr)[1] & 0xFF;
- }
- else {
- /* strlen of the string requested */
- strDesc = _C5509_USB_devParams->deviceConfig->stringDesc;
- dval = strlen( strDesc[_C5509_USB_usbSetup.wValue & 0xFF] ) *
- 2 - 2 ;
-
- /* select the smaller of the two */
- dval = (dval < _C5509_USB_usbSetup.wLength) ? dval :
- _C5509_USB_usbSetup.wLength;
-
- /*
- * Insert descriptor type and length in the
- * first two bytes of string dscriptor
- */
- strDesc[_C5509_USB_usbSetup.wValue & 0xFF][1] =
- (C5509_USB_DESCRIPTOR_STRING<<8) | dval;
-
- dataPtr = &strDesc[_C5509_USB_usbSetup.wValue & 0xFF][0];
- }
- break;
-
- default:
- return(C5509_USB_REQUEST_STALL);
- }
- /* select the smaller of two */
- dval = (dval < _C5509_USB_usbSetup.wLength) ? dval :
- _C5509_USB_usbSetup.wLength;
- USB_postTransaction(&_C5509_USB_usbEpObjIn0, dval, dataPtr, inoutFlag);
-
- /*
- * set _C5509_USB_devObj.busConnected = TRUE if configuration descriptor is
- * sent to host.
- */
- _C5509_USB_devObj.busConnected = setInitDone;
-
- if (setInitDone == TRUE) {
- /*
- * Call registered connect callback function to notify application
- * thread(s) that we are now ready to communicate over USB.
- */
- for (i = _C5509_USB_NUMEPSRSVD; i < _C5509_USB_ENDPTNUMS; i++) {
- if ((chan = _C5509_USB_devObj.chans[i]) != NULL) {
- if (chan->fxnConnect) {
- /* Call registered connect callback function */
- chan->fxnConnect(chan->argConnect);
- }
- }
- }
- }
- return(C5509_USB_REQUEST_GET_ACK);
- }