c5509_usb_ctrl.c
上传用户:dahaojd
上传日期:2008-01-29
资源大小:14357k
文件大小:8k
- /*
- * 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_ctrl.c ========
- * This file implements control endpoint 0 handler.
- */
- #include <std.h>
- #include <hwi.h>
- #include <csl.h>
- #include <csl_usb.h>
- #include <c5509_usb.h>
- #include <_c5509_usb.h>
-
- /*
- * Data structure to hold USB setup packet.
- * Used by USB CSL USB_getSetupPacket() Fxn.
- */
- USB_SetupStruct _C5509_USB_usbSetup = {0, 0, 0, 0, 0, 0};
- /*
- * ======== standard USB Request Table ========
- * USB control endpoint 0 handler parses through this table. It calls
- * back to application with the default handler that matches the request
- * sent by the host.
- * This table is declared in file c5509_usb_reqhndlr.c
- */
- extern _C5509_USB_UsbRequestStruct _C5509_USB_usbReqTable[];
-
- /*
- * ======== _C5509_USB_usbReqUnknown ========
- * Respond to unknown requests. By default, it returns C5509_USB_REQUEST_STALL
- * to stall endpoint 0.
- */
- C5509_USB_UsbReqRet _C5509_USB_usbReqUnknown()
- {
- C5509_USB_UsbReqRet retStat = C5509_USB_REQUEST_DONE;
- if (_C5509_USB_usbSetup.New) {
- /*
- * The request is either not known or not supported.
- * Request the usb control endpoint handler
- * to stall the endpoint
- */
- retStat = C5509_USB_REQUEST_STALL;
- }
- return(retStat);
- }
- /*
- * initialize the request handler function ptr to _C5509_USB_usbReqUnknown().
- */
- C5509_USB_UsbReqHandler fpRequestHandler = _C5509_USB_usbReqUnknown;
- /*
- * ======== C5509_USB_resetEventHandler ========
- * Host requests device to reset
- * This function free all packets and complet IOM_FLUSH for all
- * channels. It then re-init usb module, set device state to default.
- */
- Void C5509_USB_resetEventHandler()
- {
- Uns imask;
- /* stop all data transfer activities */
- USB_abortAllTransaction(USB0);
-
- /* free all packets and complete IOM_FLUSH for all channels */
- imask = HWI_disable();
- _C5509_USB_freeAllPackets();
- HWI_restore(imask);
-
- /* re-init usb module, set state to default */
- _C5509_USB_reInitUsb();
- }
- /*
- * ======== C5509_USB_suspendEventHandler ========
- * Host requests device to suspend
- */
- Void C5509_USB_suspendEventHandler(){
- /*
- * we do nothing here
- * application can extend the functionality
- */
- }
- /*
- * ======== control endpoint 0 handler ========
- */
- Void _C5509_USB_usbCtrlHandler()
- {
- C5509_USB_UsbReqRet reqHandlerRet = C5509_USB_REQUEST_DONE;
- USB_EVENT_MASK usbCtrlEvents;
- USB_EpHandle hEp0In = &_C5509_USB_usbEpObjIn0;
- USB_EpHandle hEp0Out = &_C5509_USB_usbEpObjOut0;
-
- /* find out the control endpoint EP0 event */
- usbCtrlEvents = (USB_getEvents(hEp0Out) | USB_getEvents(hEp0In));
-
- /*
- * if the USB reset request received, abort all endpoint activities
- * and reconfigure the USB module
- */
- if (usbCtrlEvents & USB_EVENT_RESET) { /* USB RESET event */
-
- /*
- * host is requesting reset.
- */
- if (_C5509_USB_devParams->deviceConfig->eventHandler != NULL) {
- /* Call configured event handler */
- _C5509_USB_devParams->deviceConfig->eventHandler(USB_EVENT_RESET,
- C5509_USB_resetEventHandler);
- }
- else { /* use default handler */
- C5509_USB_resetEventHandler();
- }
- }
-
- if (usbCtrlEvents & USB_EVENT_SUSPEND) { /* USB SUSPEND event */
-
- /*
- * host is requesting suspend
- */
- if (_C5509_USB_devParams->deviceConfig->eventHandler != NULL) {
- _C5509_USB_devParams->deviceConfig->eventHandler(USB_EVENT_SUSPEND,
- C5509_USB_suspendEventHandler );
- }
- else {
- C5509_USB_suspendEventHandler(); /* call default handler */
- }
- }
-
- /*
- * If the event is a setup packet received event then read the setup
- * packet, and lookup the usbReqTable[] and user defined table for
- * the appropriate request handler
- */
- if ((usbCtrlEvents & USB_EVENT_SETUP) == USB_EVENT_SETUP) {
- /*
- * read the setup packet, if something wrong with setup packet
- * then stall the control endpints
- */
- if ( USB_getSetupPacket( USB0, &_C5509_USB_usbSetup) == USB_FALSE) {
- reqHandlerRet = C5509_USB_REQUEST_STALL;
- }
- else {
- /*
- * Get last request from USB setup packet..
- * See USB 1.x spec Ch 9 for data formats.
- */
- _C5509_USB_devObj.lastRequest =
- ((_C5509_USB_usbSetup.bmRequestType & 0xC0) <<8) |
- _C5509_USB_usbSetup.bRequest;
- /*
- * lookup the USB request handler
- */
- fpRequestHandler =
- _C5509_USB_usbLookupReqHandler(_C5509_USB_devObj.lastRequest);
-
- /*
- * pass the default request handler,
- * notify application that there is a setup event
- * application can extend the functionality
- * or override the default request handler
- */
- if (_C5509_USB_devParams->deviceConfig->setupEventHandler !=NULL) {
- reqHandlerRet =
- _C5509_USB_devParams->deviceConfig->setupEventHandler(
- _C5509_USB_devObj.lastRequest,
- fpRequestHandler, &_C5509_USB_usbSetup);
- }
- else {
- reqHandlerRet = fpRequestHandler(); /* call defualt handler */
- }
- }
-
- } /* end of if setup event received */
-
- /*
- * based on the return value from the called request handler routine
- * send ACK, stall endpoint, or do nothing.
- */
- switch (reqHandlerRet) {
- /*
- * the request handler routine successfully completed the task,
- * send a 0-byte ACK
- */
- case C5509_USB_REQUEST_SEND_ACK :
- USB_postTransaction( hEp0In, 0, NULL, USB_IOFLAG_NONE);
- break;
-
- case C5509_USB_REQUEST_DATA_OUT :
- break;
-
- /*
- * the request handler routine successfully completed the task,
- * get a 0-byte ACK
- */
- case C5509_USB_REQUEST_GET_ACK :
- USB_postTransaction( hEp0Out, 0, NULL, USB_IOFLAG_NONE);
- break;
- case C5509_USB_REQUEST_DATA_IN :
- break;
-
- /*
- * the request handler does not know what to do with the setup
- * packet, stall the control endpoints
- */
- case C5509_USB_REQUEST_STALL :
- USB_stallEndpt(hEp0Out);
- USB_stallEndpt(hEp0In);
- break;
-
- case C5509_USB_REQUEST_DONE :
- break;
- default:
- fpRequestHandler = _C5509_USB_usbReqUnknown;
- }
- /* clear the flags if a new setup packet is received */
- if (_C5509_USB_usbSetup.New) {
- _C5509_USB_usbSetup.New = 0;
- }
- }
- /*
- * ======== _C5509_USB_usbLookupReqHandler ========
- * This function parse through the usbReqTable and
- * pickup the address of that particular request handler
- * request: the first 2 bytes of USB setup packet
- */
- C5509_USB_UsbReqHandler _C5509_USB_usbLookupReqHandler(Uint16 request)
- {
- Uint16 i;
-
- /* parse thru the end of request handler table */
- for (i=0; _C5509_USB_usbReqTable[i].usbReqHandler != NULL; i++) {
- /* if request handler exists
- * return a pointer to the request handler routine
- */
- if(_C5509_USB_usbReqTable[i].request == request) {
- return (_C5509_USB_usbReqTable[i].usbReqHandler);
- }
- }
-
- /*
- * if request handler does not exist,
- * return a pointer to the usbReqUnknown routine
- */
- return (_C5509_USB_usbReqUnknown);
- }