Annotation of q_a/samples/ddk/portio/gpdread.c, revision 1.1.1.1

1.1       root        1: // Read a byte from a port using the wkd driver.
                      2: // Robert R. Howell             January 6, 1993
                      3: // Robert B. Nelson (Microsoft) January 12, 1993
                      4: 
                      5: 
                      6: #include <windows.h>
                      7: #include <stdio.h>
                      8: #include <stdlib.h>
                      9: #include <winioctl.h>
                     10: #include "gpioctl.h"        // This defines the IOCTL constants.
                     11: 
                     12: void main(int argc, char ** argv)
                     13: {
                     14: 
                     15:     // The following is returned by IOCTL.  It is true if the read succeeds.
                     16:     BOOL    IoctlResult;
                     17: 
                     18:     // The following parameters are used in the IOCTL call
                     19:     HANDLE  hndFile;            // Handle to device, obtain from CreateFile
                     20:     ULONG   PortNumber;         // Buffer sent to the driver (Port #).
                     21:     union   {
                     22:         ULONG   LongData;
                     23:         USHORT  ShortData;
                     24:         UCHAR   CharData;
                     25:     }   DataBuffer;             // Buffer received from driver (Data).
                     26:     LONG    IoctlCode;
                     27:     ULONG   DataLength;
                     28:     DWORD   ReturnedLength;     // Number of bytes returned
                     29: 
                     30:     // The input buffer is a ULONG containing the port address.  It is
                     31:     // specified as 0, 1, 2, ... relative to the base address set in genport.h
                     32:     // or overridden by the registry.
                     33: 
                     34:     // The port data is returned in the output buffer DataBuffer;
                     35: 
                     36:     if ( argc < 3 || argv[1][0] != '-' ||
                     37:          ( argv[1][1] != 'b' && argv[1][1] != 'w' && argv[1][1] != 'd' ) )
                     38:     {
                     39:         printf("GpdRead -b|-w|-d Port#  A byte (-b), word (-w), or a double\n");
                     40:         printf("                        word (-d) is read from the given port.\n");
                     41:         printf("                        Ports are numbered as 0, 1, ... relative\n");
                     42:         printf("                        to the base port set in the driver or in\n");
                     43:         printf("                        the registry.  The default driver\n");
                     44:         printf("                        uses ports 300h through 303h\n");
                     45:         printf("                        All numbers are read and printed in hex.\n");
                     46:         exit(1);
                     47:     }
                     48: 
                     49:     hndFile = CreateFile(
                     50:                 "\\\\.\\GpdDev",                    // Open the Device "file"
                     51:                 GENERIC_READ,
                     52:                 FILE_SHARE_READ,
                     53:                 NULL,
                     54:                 OPEN_EXISTING,
                     55:                 0,
                     56:                 NULL
                     57:                 );
                     58:                 
                     59:     if (hndFile == INVALID_HANDLE_VALUE)        // Was the device opened?
                     60:     {
                     61:         printf("Unable to open the device.\n");
                     62:         exit(1);
                     63:     }
                     64: 
                     65:     switch (argv[1][1])
                     66:     {
                     67:         case 'b':
                     68:             IoctlCode = IOCTL_GPD_READ_PORT_UCHAR;
                     69:             DataLength = sizeof(DataBuffer.CharData);
                     70:             break;
                     71:             
                     72:         case 'w':
                     73:             IoctlCode = IOCTL_GPD_READ_PORT_USHORT;
                     74:             DataLength = sizeof(DataBuffer.ShortData);
                     75:             break;
                     76: 
                     77:         case 'd':
                     78:             IoctlCode = IOCTL_GPD_READ_PORT_ULONG;
                     79:             DataLength = sizeof(DataBuffer.LongData);
                     80:             break;
                     81:     }
                     82: 
                     83:     sscanf(argv[2], "%x", &PortNumber);         // Get the port number to be read
                     84: 
                     85:     IoctlResult = DeviceIoControl(
                     86:                             hndFile,            // Handle to device
                     87:                             IoctlCode,          // IO Control code for Read
                     88:                             &PortNumber,        // Buffer to driver.
                     89:                             sizeof(PortNumber), // Length of buffer in bytes.
                     90:                             &DataBuffer,        // Buffer from driver.
                     91:                             DataLength,         // Length of buffer in bytes.
                     92:                             &ReturnedLength,    // Bytes placed in DataBuffer.
                     93:                             NULL                // NULL means wait till op. completes.
                     94:                             );
                     95: 
                     96:     if (IoctlResult)                            // Did the IOCTL succeed?
                     97:     {
                     98:         ULONG   Data;
                     99: 
                    100:         if (ReturnedLength != DataLength)
                    101:         {
                    102:             printf(
                    103:                 "Ioctl transfered %d bytes, expected %d bytes\n",
                    104:                 ReturnedLength, DataLength );
                    105:         }
                    106:         
                    107:         switch (ReturnedLength)
                    108:         {
                    109:             case sizeof(UCHAR):
                    110:                 Data = DataBuffer.CharData;
                    111:                 break;
                    112:                 
                    113:             case sizeof(USHORT):
                    114:                 Data = DataBuffer.ShortData;
                    115:                 break;
                    116:     
                    117:             case sizeof(ULONG):
                    118:                 Data = DataBuffer.LongData;
                    119:                 break;
                    120:         }
                    121:         
                    122:         printf("Read from port %x returned %x\n", PortNumber, Data);
                    123:     }
                    124:     else
                    125:     {
                    126:         printf("Ioctl failed with code %ld\n", GetLastError() );
                    127:     }
                    128: 
                    129: 
                    130:     if (!CloseHandle(hndFile))                  // Close the Device "file".
                    131:     {
                    132:         printf("Failed to close device.\n");
                    133:     }
                    134: 
                    135:     exit(0);
                    136: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.