Fix PrinterHost demo so that it will only enumerate printers with Bidirectional protocol encapsulation. Change enumeration code to automatically select the correct alternate setting for the printer interface to select the bidirectional protocol.

pull/1469/head
Dean Camera 16 years ago
parent 9d2613d908
commit d3fb6273aa

@ -30,13 +30,16 @@
#include "ConfigDescriptor.h" #include "ConfigDescriptor.h"
uint8_t PrinterInterfaceNumber;
uint8_t PrinterAltSetting;
uint8_t ProcessConfigurationDescriptor(void) uint8_t ProcessConfigurationDescriptor(void)
{ {
uint8_t* ConfigDescriptorData; uint8_t* ConfigDescriptorData;
uint16_t ConfigDescriptorSize; uint16_t ConfigDescriptorSize;
uint8_t ErrorCode; uint8_t ErrorCode;
uint8_t FoundEndpoints = 0; uint8_t FoundEndpoints = 0;
uint8_t FoundEndpointMask;
/* Get Configuration Descriptor size from the device */ /* Get Configuration Descriptor size from the device */
if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful) if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
@ -58,31 +61,17 @@ uint8_t ProcessConfigurationDescriptor(void)
/* Get the printer interface from the configuration descriptor */ /* Get the printer interface from the configuration descriptor */
if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
NextPrinterInterface))) NextBidirectionalPrinterInterface)))
{ {
/* Descriptor not found, error out */ /* Descriptor not found, error out */
return NoInterfaceFound; return NoInterfaceFound;
} }
/* Get the printer's communication protocol */ PrinterInterfaceNumber = DESCRIPTOR_CAST(ConfigDescriptorData, USB_Descriptor_Interface_t).InterfaceNumber;
PrinterProtocol = DESCRIPTOR_CAST(ConfigDescriptorData, USB_Descriptor_Interface_t).Protocol; PrinterAltSetting = DESCRIPTOR_CAST(ConfigDescriptorData, USB_Descriptor_Interface_t).AlternateSetting;
/* Determine what endpoints to look for from the protocol */
switch (PrinterProtocol)
{
case PROTOCOL_UNIDIRECTIONAL:
FoundEndpointMask = (1 << PRINTER_DATA_OUT_PIPE);
break;
case PROTOCOL_BIDIRECTIONAL:
case PROTOCOL_IEEE1284:
FoundEndpointMask = ((1 << PRINTER_DATA_OUT_PIPE) | (1 << PRINTER_DATA_IN_PIPE));
break;
default:
return NoInterfaceFound;
}
/* Get the IN and OUT data endpoints for the mass storage interface */ /* Get the IN and OUT data endpoints for the mass storage interface */
while (FoundEndpoints != FoundEndpointMask) while (FoundEndpoints != ((1 << PRINTER_DATA_OUT_PIPE) | (1 << PRINTER_DATA_IN_PIPE)))
{ {
/* Fetch the next bulk endpoint from the current printer interface */ /* Fetch the next bulk endpoint from the current printer interface */
if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
@ -123,15 +112,16 @@ uint8_t ProcessConfigurationDescriptor(void)
return SuccessfulConfigRead; return SuccessfulConfigRead;
} }
uint8_t NextPrinterInterface(void* CurrentDescriptor) uint8_t NextBidirectionalPrinterInterface(void* CurrentDescriptor)
{ {
/* PURPOSE: Find next mass storage class interface descriptor */ /* PURPOSE: Find next Bidirectional protocol printer class interface descriptor */
if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface) if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
{ {
/* Check the descriptor class and protocol, break out if correct class/protocol interface found */ /* Check the descriptor class and protocol, break out if correct class/protocol interface found */
if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == PRINTER_CLASS) && if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class == PRINTER_CLASS) &&
(DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == PRINTER_SUBCLASS)) (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == PRINTER_SUBCLASS) &&
(DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == PROTOCOL_BIDIRECTIONAL))
{ {
return DESCRIPTOR_SEARCH_Found; return DESCRIPTOR_SEARCH_Found;
} }

@ -56,10 +56,14 @@
NoEndpointFound = 5, NoEndpointFound = 5,
}; };
/* External Variables: */
uint8_t PrinterInterfaceNumber;
uint8_t PrinterAltSetting;
/* Function Prototypes: */ /* Function Prototypes: */
uint8_t ProcessConfigurationDescriptor(void); uint8_t ProcessConfigurationDescriptor(void);
uint8_t NextPrinterInterface(void* CurrentDescriptor); uint8_t NextBidirectionalPrinterInterface(void* CurrentDescriptor);
uint8_t NextInterfaceBulkDataEndpoint(void* CurrentDescriptor); uint8_t NextInterfaceBulkDataEndpoint(void* CurrentDescriptor);
#endif #endif

@ -36,8 +36,6 @@
#include "PrinterHost.h" #include "PrinterHost.h"
uint8_t PrinterProtocol;
int main(void) int main(void)
{ {
@ -151,11 +149,36 @@ void USB_Printer_Host(void)
break; break;
} }
/* Some printers use alternate settings to determine the communication protocol used - if so, send a SetInterface
* request to switch to the interface alternate setting with the Bidirection protocol */
if (PrinterAltSetting)
{
USB_ControlRequest = (USB_Request_Header_t)
{
bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),
bRequest: REQ_SetInterface,
wValue: PrinterAltSetting,
wIndex: PrinterInterfaceNumber,
wLength: 0,
};
if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
{
puts_P(PSTR("Control Error (Set Interface).\r\n"));
printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
/* Indicate error via status LEDs */
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
/* Wait until USB device disconnected */
USB_HostState = HOST_STATE_WaitForDeviceRemoval;
break;
}
}
USB_HostState = HOST_STATE_Configured; USB_HostState = HOST_STATE_Configured;
break; break;
case HOST_STATE_Configured: case HOST_STATE_Configured:
printf_P(PSTR("Printer Protocol: %d\r\n"), PrinterProtocol);
puts_P(PSTR("Retrieving Device ID...\r\n")); puts_P(PSTR("Retrieving Device ID...\r\n"));
Device_ID_String_t DeviceIDString; Device_ID_String_t DeviceIDString;

@ -63,9 +63,6 @@
/** LED mask for the library LED driver, to indicate that the USB interface is busy. */ /** LED mask for the library LED driver, to indicate that the USB interface is busy. */
#define LEDMASK_USB_BUSY (LEDS_LED2) #define LEDMASK_USB_BUSY (LEDS_LED2)
/* External Variables: */
extern uint8_t PrinterProtocol;
/* Function Prototypes: */ /* Function Prototypes: */
void EVENT_USB_DeviceAttached(void); void EVENT_USB_DeviceAttached(void);
void EVENT_USB_DeviceUnattached(void); void EVENT_USB_DeviceUnattached(void);

Loading…
Cancel
Save