3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  31 #include "ConfigDescriptor.h" 
  33 uint8_t PrinterInterfaceNumber
; 
  34 uint8_t PrinterAltSetting
; 
  37 uint8_t ProcessConfigurationDescriptor(void) 
  39         uint8_t  ConfigDescriptorData
[512]; 
  40         uint8_t* CurrConfigLocation 
= ConfigDescriptorData
; 
  41         uint16_t CurrConfigBytesRem
; 
  42         uint8_t  FoundEndpoints 
= 0; 
  44         /* Retrieve the entire configuration descriptor into the allocated buffer */ 
  45         switch (USB_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem
, ConfigDescriptorData
, sizeof(ConfigDescriptorData
))) 
  47                 case HOST_GETCONFIG_Successful
: 
  49                 case HOST_GETCONFIG_InvalidData
: 
  50                         return InvalidConfigDataReturned
; 
  51                 case HOST_GETCONFIG_BuffOverflow
: 
  52                         return DescriptorTooLarge
; 
  57         /* Get the printer interface from the configuration descriptor */ 
  58         if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, DComp_NextBidirectionalPrinterInterface
)) 
  60                 /* Descriptor not found, error out */ 
  61                 return NoInterfaceFound
; 
  64         PrinterInterfaceNumber 
= DESCRIPTOR_CAST(CurrConfigLocation
, USB_Descriptor_Interface_t
).InterfaceNumber
; 
  65         PrinterAltSetting      
= DESCRIPTOR_CAST(CurrConfigLocation
, USB_Descriptor_Interface_t
).AlternateSetting
; 
  67         /* Get the IN and OUT data endpoints for the printer interface */ 
  68         while (FoundEndpoints 
!= ((1 << PRINTER_DATA_OUT_PIPE
) | (1 << PRINTER_DATA_IN_PIPE
))) 
  70                 /* Fetch the next bulk endpoint from the current printer interface */ 
  71                 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, DComp_NextPrinterInterfaceBulkDataEndpoint
)) 
  73                         /* Descriptor not found, error out */ 
  74                         return NoEndpointFound
; 
  77                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
); 
  79                 /* Check if the endpoint is a bulk IN or bulk OUT endpoint, set appropriate globals */ 
  80                 if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
  82                         /* Configure the data IN pipe */ 
  83                         Pipe_ConfigurePipe(PRINTER_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
, 
  84                                            EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, 
  87                         Pipe_SetInfiniteINRequests(); 
  89                         /* Set the flag indicating that the data IN pipe has been found */ 
  90                         FoundEndpoints 
|= (1 << PRINTER_DATA_IN_PIPE
); 
  94                         /* Configure the data OUT pipe */ 
  95                         Pipe_ConfigurePipe(PRINTER_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
, 
  96                                            EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, 
  99                         /* Set the flag indicating that the data OUT pipe has been found */ 
 100                         FoundEndpoints 
|= (1 << PRINTER_DATA_OUT_PIPE
); 
 104         /* Valid data found, return success */ 
 105         return SuccessfulConfigRead
; 
 108 uint8_t DComp_NextBidirectionalPrinterInterface(void* CurrentDescriptor
) 
 110         /* PURPOSE: Find next bidirectional protocol printer class interface descriptor */ 
 112         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 114                 /* Check the descriptor class and protocol, break out if correct class/protocol interface found */ 
 115                 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class    
== PRINTER_CLASS
)    && 
 116                     (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass 
== PRINTER_SUBCLASS
) && 
 117                         (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol 
== PRINTER_PROTOCOL
)) 
 119                         return DESCRIPTOR_SEARCH_Found
; 
 123         return DESCRIPTOR_SEARCH_NotFound
; 
 126 uint8_t DComp_NextPrinterInterfaceBulkDataEndpoint(void* CurrentDescriptor
) 
 128         /* PURPOSE: Find next interface bulk endpoint descriptor before next interface descriptor */ 
 130         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
) 
 132                 uint8_t EndpointType 
= (DESCRIPTOR_CAST(CurrentDescriptor
, 
 133                                                         USB_Descriptor_Endpoint_t
).Attributes 
& EP_TYPE_MASK
); 
 135                 /* Check the endpoint type, break out if correct BULK type endpoint found */ 
 136                 if (EndpointType 
== EP_TYPE_BULK
) 
 137                   return DESCRIPTOR_SEARCH_Found
; 
 139         else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 141                 return DESCRIPTOR_SEARCH_Fail
; 
 144         return DESCRIPTOR_SEARCH_NotFound
;