Chapter 9 of the USB specification 2.0 defines a set of standard requests which have to be implemented by all devices. Since most class drivers treat those requests in the standard way, the USB framework provides a way to easily do that.
USBDDriver_RequestHandler handles the standard requests in an appropriate way. It can answer the following commands:
Simply using this standard request handler enables a device to be enumerated correctly.
The GET_DESCRIPTOR request is used by the host to retrieve information about the device by means of several descriptors.
The standard request handler simply sends the corresponding descriptor to the host. How these descriptors are provided to the function is discussed in Structures.
Whenever the host wants to change the device state from Default to Address, or vice-versa, it sends a SET_ADDRESS request. The wValue field contains the new address of the device; if it is null, then the device returns to the Default state.
The USBD_SetAddress function is called to perform this operation. Note that a zero-length packet must be sent prior to doing that, to acknowledge the SETUP transfer.
The SET_CONFIGURATION request makes it possible for the host to select between one or more configurations for the device. GET_CONFIGURATION is used to retrieve the currently selected one.
Those two requests are handled in a very basic way by USBDDriver_RequestHandler() : it assumes that the device has only one configuration. Therefore, the SET_CONFIGURATION request is simply acknowledged with a zero-length packet, and GET_CONFIGURATION is answered with either 0 or 1. If the user application needs more than one configuration, it will be the duty of the class driver handler to service those requests.
In addition, when the SET_CONFIGURATION request causes the device to enter the Configured state, the standard request handler calls the USBD_ConfigureEndpoint method for each endpoint used by the device;
Several features of a device can either be activated or deactivated by the USB host:
The USBDDriver_RequestHandler method answers a Halt state operation by calling the USBD_Halt method on the endpoint with the request.
Several pieces of information must be known to the USBDDriver_RequestHandler to be able to process some SETUP commands. For example, all the descriptors (configuration, etc.) used by the device are needed since they must be sent to the host when a GET_DESCRIPTOR is received.
The USBGenericRequest structure is a "standard USB class driver" object used to hold the required information. It must be passed as an argument to the USBDDriver_RequestHandler method. Another structure, USBDDriverDescriptors, is used to store the descriptors list.
The NewRequest callback is used to notify the user application that a new SETUP request has been received. SETUP request can either be class-specific or standard.
The correct way to handle incoming requests is to first process class-specific requests using a class handler. For example, a Mass Storage implementation will define the NewRequest callback to call MSDDriver_RequestHandler. This function will handle the necessary requests, and forward the rest to USBDDriver_RequestHandler.
If a request cannot be processed, USBDDriver_RequestHandler will STALL control endpoint 0.