home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 February / Chip_2004-02_cd1.bin / zkuste / konfig / download / msic / Help / Int / MiTeC_USB.int < prev    next >
Text File  |  2003-08-26  |  10KB  |  272 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {         MiTeC System Information Component            }
  4. {                USB Interface                          }
  5. {           version 8.3 for Delphi 5,6,7                }
  6. {                                                       }
  7. {       Copyright ⌐ 1997,2003 Michal Mutl               }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11.  
  12. {$INCLUDE MITEC_DEF.INC}
  13.  
  14. unit MiTeC_USB;
  15.  
  16. interface
  17.  
  18. uses SysUtils, Windows, MiTeC_WinIOCTL;
  19.  
  20. const
  21.   FILE_DEVICE_USB = FILE_DEVICE_UNKNOWN;
  22.   USB_IOCTL_INTERNAL_INDEX = $0000;
  23.   USB_IOCTL_INDEX = $00ff;
  24.  
  25.   IOCTL_USB_GET_NODE_INFORMATION   = ((FILE_DEVICE_USB shl 16) or
  26.                                                ((USB_IOCTL_INDEX+3) shl 2) or
  27.                                                METHOD_BUFFERED or
  28.                                                (FILE_ANY_ACCESS shl 14));
  29.  
  30.   IOCTL_USB_GET_NODE_CONNECTION_INFORMATION       = ((FILE_DEVICE_USB shl 16) or
  31.                                                 ((USB_IOCTL_INDEX+4) shl 2) or
  32.                                                 METHOD_BUFFERED or
  33.                                                 (FILE_ANY_ACCESS shl 14));
  34.  
  35.   IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION   = ((FILE_DEVICE_USB shl 16) or
  36.                                                 ((USB_IOCTL_INDEX+5) shl 2) or
  37.                                                 METHOD_BUFFERED or
  38.                                                 (FILE_ANY_ACCESS shl 14));
  39.  
  40.   IOCTL_USB_GET_NODE_CONNECTION_NAME     = ((FILE_DEVICE_USB shl 16) or
  41.                                                 ((USB_IOCTL_INDEX+6) shl 2) or
  42.                                                 METHOD_BUFFERED or
  43.                                                 (FILE_ANY_ACCESS shl 14));
  44.  
  45.   IOCTL_USB_DIAG_IGNORE_HUBS_ON   = ((FILE_DEVICE_USB shl 16) or
  46.                                                 ((USB_IOCTL_INDEX+7) shl 2) or
  47.                                                 METHOD_BUFFERED or
  48.                                                 (FILE_ANY_ACCESS shl 14));
  49.  
  50.   IOCTL_USB_DIAG_IGNORE_HUBS_OFF  = ((FILE_DEVICE_USB shl 16) or
  51.                                                 ((USB_IOCTL_INDEX+8) shl 2) or
  52.                                                 METHOD_BUFFERED or
  53.                                                 (FILE_ANY_ACCESS shl 14));
  54.  
  55.   IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME  = ((FILE_DEVICE_USB shl 16) or
  56.                                                 ((USB_IOCTL_INDEX+9) shl 2) or
  57.                                                 METHOD_BUFFERED or
  58.                                                 (FILE_ANY_ACCESS shl 14));
  59.  
  60.   IOCTL_USB_HCD_GET_STATS_1          = ((FILE_DEVICE_USB shl 16) or
  61.                                                 ((USB_IOCTL_INDEX) shl 2) or
  62.                                                 METHOD_BUFFERED or
  63.                                                 (FILE_ANY_ACCESS shl 14));
  64.  
  65.   IOCTL_USB_HCD_GET_STATS_2          = ((FILE_DEVICE_USB shl 16) or
  66.                                                 ((USB_IOCTL_INDEX+11) shl 2) or
  67.                                                 METHOD_BUFFERED or
  68.                                                 (FILE_ANY_ACCESS shl 14));
  69.  
  70.   IOCTL_USB_DIAGNOSTIC_MODE_ON   = ((FILE_DEVICE_USB shl 16) or
  71.                                                 ((USB_IOCTL_INDEX+1) shl 2) or
  72.                                                 METHOD_BUFFERED or
  73.                                                 (FILE_ANY_ACCESS shl 14));
  74.  
  75.   IOCTL_USB_DIAGNOSTIC_MODE_OFF  = ((FILE_DEVICE_USB shl 16) or
  76.                                                 ((USB_IOCTL_INDEX+2) shl 2) or
  77.                                                 METHOD_BUFFERED or
  78.                                                 (FILE_ANY_ACCESS shl 14));
  79.  
  80.   IOCTL_USB_GET_ROOT_HUB_NAME  = ((FILE_DEVICE_USB shl 16) or
  81.                                                 ((USB_IOCTL_INDEX+3) shl 2) or
  82.                                                 METHOD_BUFFERED or
  83.                                                 (FILE_ANY_ACCESS shl 14));
  84.  
  85.   IOCTL_GET_HCD_DRIVERKEY_NAME = ((FILE_DEVICE_USB shl 16) or
  86.                                                 ((USB_IOCTL_INDEX+10) shl 2) or
  87.                                                 METHOD_BUFFERED or
  88.                                                 (FILE_ANY_ACCESS shl 14));
  89.  
  90.   USB_DEVICE_DESCRIPTOR_TYPE                = $01;
  91.   USB_CONFIGURATION_DESCRIPTOR_TYPE         = $02;
  92.   USB_STRING_DESCRIPTOR_TYPE                = $03;
  93.   USB_INTERFACE_DESCRIPTOR_TYPE             = $04;
  94.   USB_ENDPOINT_DESCRIPTOR_TYPE              = $05;
  95.  
  96.   USB_REQUEST_GET_STATUS                    = $00;
  97.   USB_REQUEST_CLEAR_FEATURE                 = $01;
  98.  
  99.   USB_REQUEST_SET_FEATURE                   = $03;
  100.  
  101.   USB_REQUEST_SET_ADDRESS                   = $05;
  102.   USB_REQUEST_GET_DESCRIPTOR                = $06;
  103.   USB_REQUEST_SET_DESCRIPTOR                = $07;
  104.   USB_REQUEST_GET_CONFIGURATION             = $08;
  105.   USB_REQUEST_SET_CONFIGURATION             = $09;
  106.   USB_REQUEST_GET_INTERFACE                 = $0A;
  107.   USB_REQUEST_SET_INTERFACE                 = $0B;
  108.   USB_REQUEST_SYNC_FRAME                    = $0C;
  109.  
  110.  
  111.  
  112. type
  113.   TUSBHubNode = (usbhnHub,usbhnMIParent);
  114.  
  115.   TUnicodeName = record
  116.     Length: DWORD;
  117.     UnicodeName: array[0..255] of Byte;
  118.   end;
  119.  
  120.   TNodeType = record
  121.     ConnectionIndex: DWORD;
  122.     Length: DWORD;
  123.     UnicodeName: array[0..255] of byte
  124.   end;
  125.  
  126.   TSetupPacket = record
  127.     bmRequest: Byte;
  128.     bRequest: Byte;
  129.     wValue: array[0..1] of Byte;
  130.     wIndex: array[0..1] of Byte;
  131.     wLength: array[0..1] of Byte;
  132.   end;
  133.  
  134.   TDescriptorRequest = record
  135.     ConnectionIndex: DWORD;
  136.     SetupPacket: TSetupPacket;
  137.     ConfigurationDescriptor: array[0..2047] of Byte;
  138.   end;
  139.  
  140.   TDeviceDescriptor = record
  141.     Length: Byte;
  142.     DescriptorType: Byte;
  143.     USBSpec: array[0..1] of Byte;
  144.     DeviceClass: Byte;
  145.     DeviceSubClass: Byte;
  146.     DeviceProtocol: Byte;
  147.     MaxPacketSize0: Byte;
  148.     VendorID: array[0..1] of Byte;
  149.     ProductID: array[0..1] of Byte;
  150.     DeviceRevision: array[0..1] of byte;
  151.     ManufacturerStringIndex: byte;
  152.     ProductStringIndex: Byte;
  153.     SerialNumberStringIndex: Byte;
  154.     ConfigurationCount: Byte;
  155.   end;
  156.  
  157.   THubDescriptor = record
  158.     Length: Byte;
  159.     HubType: Byte;
  160.     PortCount: Byte;
  161.     Characteristics: array[0..1] of Byte;
  162.     PowerOnToPowerGood: Byte;
  163.     HubControlCurrent: Byte;
  164.     RemoveAndPowerMask: array[0..63] of Byte;
  165.   end;
  166.  
  167.   TEndPointDescriptor = record
  168.     Length: Byte;
  169.     DescriptorType: Byte;
  170.     EndpointAddress: Byte;
  171.     Attributes: Byte;
  172.     MaxPacketSize: WORD;
  173.     PollingInterval: Byte;
  174.   end;
  175.  
  176.   TNodeInformation = record
  177.     NodeType: DWORD;
  178.     NodeDescriptor: THubDescriptor;
  179.     HubIsBusPowered: Byte;
  180.   end;
  181.  
  182.   TUSBPipeInfo = record
  183.     EndPointDescriptor: TEndpointDescriptor;
  184.     ScheduleOffset: DWORD;
  185.   end;
  186.  
  187.   TNodeConnectionInformation = record
  188.     ConnectionIndex: DWORD;
  189.     ThisDevice: TDeviceDescriptor;
  190.     CurrentConfiguration: Byte;
  191.     LowSpeed: Byte;
  192.     DeviceIsHub: Byte;
  193.     DeviceAddress: array[0..1] of Byte;
  194.     NumberOfOpenEndPoints: array[0..3] of Byte;
  195.     ThisConnectionStatus: array[0..3] of Byte;
  196.     PipeList: array[0..31] of TUSBPipeInfo;
  197.   end;
  198.  
  199.   TCollectedDeviceData = record
  200.     DeviceType: DWORD;
  201.     DeviceHandle: DWORD;
  202.     ConnectionData: TNodeConnectionInformation;
  203.     NodeData: TNodeInformation;
  204.   end;
  205.  
  206.   TUSBClass =     (usbReserved, usbAudio, usbCommunications, usbHID, usbMonitor,
  207.                  usbPhysicalIntf, usbPower, usbPrinter, usbStorage, usbHub, usbVendorSpec,
  208.                  // illegal values
  209.                  usbExternalHub, usbHostController, usbUnknown, usbError);
  210.  
  211.   TUSBDevice = record
  212.     Port: DWORD;
  213.     DeviceAddress: DWORD;
  214.     Manufacturer,
  215.     Product,
  216.     Serial: shortstring;
  217.     ConnectionStatus: Byte;
  218.     MaxPower: WORD;
  219.     MajorVersion,
  220.     MinorVersion: Byte;
  221.   end;
  222.  
  223.   TUSBNode = record
  224.     ConnectionName: shortstring;
  225.     Keyname: ShortString;
  226.     USBClass: TUSBClass;
  227.     ParentIndex: Integer;
  228.     Level: Integer;
  229.     USBDevice: TUSBDevice;
  230.   end;
  231.  
  232.   TUSBNodes = array of TUSBNode;
  233.  
  234. const
  235.   ClassNames: array[0..14] of string = (
  236.     'Reserved',
  237.         'Audio',
  238.         'Communications',
  239.         'Human interface',
  240.         'Monitor',
  241.         'Physical interface',
  242.         'Power',
  243.         'Printer',
  244.         'Storage',
  245.         'Root Hub',
  246.         'Vendor Specific',
  247.         'External Hub',
  248.         'Host Controller',
  249.         '<error>',
  250.         '<unknown>'
  251.         );
  252.  
  253.   ConnectionStates: array[0..5] of string = (
  254.     'No device connected',
  255.         'Device connected',
  256.         'Device FAILED enumeration',
  257.         'Device general FAILURE',
  258.         'Device caused overcurrent',
  259.         'Not enough power for device');
  260.  
  261. function GetDeviceName1(hDevice: THandle; IOCTL_Code: DWORD): string;
  262. function GetDeviceName2(hDevice: THandle; PortIndex: DWORD; IOCTL_Code: DWORD): string;
  263. function GetNodeInformation(hDevice: THandle; var Information: TNodeInformation): Integer;
  264. function GetNodeConnection(hDevice: THandle; PortIndex: DWORD; var Connection: TNodeConnectionInformation): Integer;
  265. function GetStringDescriptor(HubHandle: THandle; PortIndex: DWORD; var LanguageID: Word; Index: Byte; var Str: shortstring): integer;
  266. function GetConfigurationDescriptor(HubHandle: THandle; PortIndex: DWORD; var MaxPower: WORD; var ClassID: integer): Integer;
  267. function GetPortData(hRootHub: THandle; SA: TSecurityAttributes; PortCount: Byte; ParentIndex: integer; Level: Integer; var USBNodes: TUSBNodes): Integer;
  268. procedure EnumUSBDevices(var USBNodes: TUSBNodes);
  269.  
  270. implementation
  271.  
  272.