Fix PrinterHost demo so that it will only enumerate printers with Bidirectional proto...
authorDean Camera <dean@fourwalledcubicle.com>
Sun, 19 Jul 2009 09:36:16 +0000 (09:36 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Sun, 19 Jul 2009 09:36:16 +0000 (09:36 +0000)
Demos/Host/Incomplete/PrinterHost/ConfigDescriptor.c
Demos/Host/Incomplete/PrinterHost/ConfigDescriptor.h
Demos/Host/Incomplete/PrinterHost/PrinterHost.c
Demos/Host/Incomplete/PrinterHost/PrinterHost.h

index ed4bdb9..67a0fa7 100644 (file)
 \r
 #include "ConfigDescriptor.h"\r
 \r
+uint8_t PrinterInterfaceNumber;\r
+uint8_t PrinterAltSetting;\r
+\r
+\r
 uint8_t ProcessConfigurationDescriptor(void)\r
 {\r
        uint8_t* ConfigDescriptorData;\r
        uint16_t ConfigDescriptorSize;\r
        uint8_t  ErrorCode;\r
        uint8_t  FoundEndpoints = 0;\r
-       uint8_t  FoundEndpointMask;\r
        \r
        /* Get Configuration Descriptor size from the device */\r
        if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)\r
@@ -58,31 +61,17 @@ uint8_t ProcessConfigurationDescriptor(void)
        \r
        /* Get the printer interface from the configuration descriptor */\r
        if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,\r
-                                                  NextPrinterInterface)))\r
+                                                  NextBidirectionalPrinterInterface)))\r
        {\r
                /* Descriptor not found, error out */\r
                return NoInterfaceFound;\r
        }\r
-\r
-       /* Get the printer's communication protocol */\r
-       PrinterProtocol = DESCRIPTOR_CAST(ConfigDescriptorData, USB_Descriptor_Interface_t).Protocol;\r
        \r
-       /* Determine what endpoints to look for from the protocol */\r
-       switch (PrinterProtocol)\r
-       {\r
-               case PROTOCOL_UNIDIRECTIONAL:\r
-                       FoundEndpointMask = (1 << PRINTER_DATA_OUT_PIPE);\r
-                       break;\r
-               case PROTOCOL_BIDIRECTIONAL:\r
-               case PROTOCOL_IEEE1284:\r
-                       FoundEndpointMask = ((1 << PRINTER_DATA_OUT_PIPE) | (1 << PRINTER_DATA_IN_PIPE));\r
-                       break;\r
-               default:\r
-                       return NoInterfaceFound;\r
-       }\r
+       PrinterInterfaceNumber = DESCRIPTOR_CAST(ConfigDescriptorData, USB_Descriptor_Interface_t).InterfaceNumber;\r
+       PrinterAltSetting      = DESCRIPTOR_CAST(ConfigDescriptorData, USB_Descriptor_Interface_t).AlternateSetting;\r
 \r
        /* Get the IN and OUT data endpoints for the mass storage interface */\r
-       while (FoundEndpoints != FoundEndpointMask)\r
+       while (FoundEndpoints != ((1 << PRINTER_DATA_OUT_PIPE) | (1 << PRINTER_DATA_IN_PIPE)))\r
        {\r
                /* Fetch the next bulk endpoint from the current printer interface */\r
                if ((ErrorCode = USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,\r
@@ -123,15 +112,16 @@ uint8_t ProcessConfigurationDescriptor(void)
        return SuccessfulConfigRead;\r
 }\r
 \r
-uint8_t NextPrinterInterface(void* CurrentDescriptor)\r
+uint8_t NextBidirectionalPrinterInterface(void* CurrentDescriptor)\r
 {\r
-       /* PURPOSE: Find next mass storage class interface descriptor */\r
+       /* PURPOSE: Find next Bidirectional protocol printer class interface descriptor */\r
 \r
        if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)\r
        {\r
                /* Check the descriptor class and protocol, break out if correct class/protocol interface found */\r
                if ((DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Class    == PRINTER_CLASS)    &&\r
-                   (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == PRINTER_SUBCLASS))\r
+                   (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).SubClass == PRINTER_SUBCLASS) &&\r
+                       (DESCRIPTOR_CAST(CurrentDescriptor, USB_Descriptor_Interface_t).Protocol == PROTOCOL_BIDIRECTIONAL))\r
                {\r
                        return DESCRIPTOR_SEARCH_Found;\r
                }\r
index 0a8dbe9..bbe2372 100644 (file)
                        NoInterfaceFound                     = 4,\r
                        NoEndpointFound                      = 5,\r
                };\r
+       \r
+       /* External Variables: */\r
+               uint8_t PrinterInterfaceNumber;\r
+               uint8_t PrinterAltSetting;\r
 \r
        /* Function Prototypes: */\r
                uint8_t ProcessConfigurationDescriptor(void);   \r
 \r
-               uint8_t NextPrinterInterface(void* CurrentDescriptor);\r
+               uint8_t NextBidirectionalPrinterInterface(void* CurrentDescriptor);\r
                uint8_t NextInterfaceBulkDataEndpoint(void* CurrentDescriptor);\r
 \r
 #endif\r
index 80fca25..643f919 100644 (file)
@@ -36,8 +36,6 @@
 \r
 #include "PrinterHost.h"\r
 \r
-uint8_t PrinterProtocol;\r
-\r
 \r
 int main(void)\r
 {\r
@@ -150,12 +148,37 @@ void USB_Printer_Host(void)
                                USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
                                break;\r
                        }\r
-\r
+                       \r
+                       /* Some printers use alternate settings to determine the communication protocol used - if so, send a SetInterface\r
+                        * request to switch to the interface alternate setting with the Bidirection protocol */\r
+                       if (PrinterAltSetting)\r
+                       {\r
+                               USB_ControlRequest = (USB_Request_Header_t)\r
+                                       {\r
+                                               bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),\r
+                                               bRequest:      REQ_SetInterface,\r
+                                               wValue:        PrinterAltSetting,\r
+                                               wIndex:        PrinterInterfaceNumber,\r
+                                               wLength:       0,\r
+                                       };\r
+                                       \r
+                               if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)\r
+                               {\r
+                                       puts_P(PSTR("Control Error (Set Interface).\r\n"));\r
+                                       printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);\r
+\r
+                                       /* Indicate error via status LEDs */\r
+                                       LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+\r
+                                       /* Wait until USB device disconnected */\r
+                                       USB_HostState = HOST_STATE_WaitForDeviceRemoval;\r
+                                       break;                                  \r
+                               }\r
+                       }\r
+                       \r
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                case HOST_STATE_Configured:\r
-                       printf_P(PSTR("Printer Protocol: %d\r\n"), PrinterProtocol);\r
-               \r
                        puts_P(PSTR("Retrieving Device ID...\r\n"));\r
                \r
                        Device_ID_String_t DeviceIDString;\r
index 46427b3..c0bbebf 100644 (file)
@@ -62,9 +62,6 @@
        \r
                /** LED mask for the library LED driver, to indicate that the USB interface is busy. */\r
                #define LEDMASK_USB_BUSY         (LEDS_LED2)\r
-\r
-       /* External Variables: */\r
-               extern uint8_t PrinterProtocol;\r
        \r
        /* Function Prototypes: */\r
                void EVENT_USB_DeviceAttached(void);\r