home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Devices / DriverGestalt Demo / DriverGestalt_Demo.c < prev   
Encoding:
Text File  |  1997-01-28  |  8.8 KB  |  361 lines  |  [TEXT/BOBO]

  1. /*/*************************************************************************
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Macintosh Disk Driver Gestalt Sample
  5. **
  6. **    by Vinne Moscaritolo, <vinnie@apple.com>
  7. **    Apple Developer Technical Support
  8. **
  9. **  Created from as much sample code I could find.
  10. **  with much help from  Martin Minow
  11. **
  12. **  " Good Artists Imitate, Great Artist Steal "
  13. **
  14. **    File:        DriverGestalt Demo.c
  15. **
  16. **    Copyright © 1996 Apple Computer, Inc.
  17. **    All rights reserved.
  18. **
  19. **    You may incorporate this sample code into your applications without
  20. **    restriction, though the sample code has been provided "AS IS" and the
  21. **    responsibility for its operation is 100% yours.  However, what you are
  22. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  23. **    after having made changes. If you're going to re-distribute the source,
  24. **    we require that you make it clear in the source that the code was
  25. **    descended from Apple Sample Code, but that you've made changes.
  26. **
  27. **************************************************************************/
  28.  
  29.  
  30.  
  31. //------------------------------------------------------------------------------
  32. ------
  33. #pragma mark Includes
  34. //------------------------------------------------------------------------------
  35. ------
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38.  
  39. #include <DriverGestalt.h>
  40.  
  41. //------------------------------------------------------------------------------
  42. ------
  43. #pragma mark Defines
  44. //------------------------------------------------------------------------------
  45. ------
  46.  
  47. #define CLEAR(what) do {                        \
  48.         register Ptr        _ptr = (Ptr) &what;    \
  49.         register Size        _len = sizeof what;    \
  50.         for (; _len > 0; --_len)
  51.     \
  52.             *_ptr++ = 0;
  53.     \
  54.     } while (0)
  55.  
  56.  
  57. //------------------------------------------------------------------------------
  58. ------
  59. #pragma mark DriverGestalt Info & Structures
  60. //------------------------------------------------------------------------------
  61. ------
  62.  
  63. enum {
  64.     kdgFlush                    = 'flus'    /*
  65. can disk driver flush */
  66. };
  67.  
  68. struct DriverGestaltFlushResponse {
  69.     Boolean                            canFlush;
  70.     Boolean                            needsFlush;
  71.     UInt8                            pad[2];
  72. };
  73. typedef struct DriverGestaltFlushResponse DriverGestaltFlushResponse;
  74.  
  75. typedef struct DriverGestaltSyncResponse DriverGestaltSyncResponse;
  76.  
  77. struct DriverGestaltVersResponse
  78. {
  79.     NumVersion driverVersion;
  80. };
  81. typedef struct DriverGestaltVersResponse DriverGestaltVersResponse;
  82.  
  83.  
  84. union DriverGestaltInfo
  85. {
  86.     DriverGestaltSyncResponse        sync;
  87.     DriverGestaltBootResponse        boot;
  88.     DriverGestaltDevTResponse        devt;
  89.     DriverGestaltIntfResponse        intf;
  90.     DriverGestaltVersResponse        vers;
  91.     DriverGestaltEjectResponse        ejec;
  92.     DriverGestaltPowerResponse        powr;
  93.     DriverGestaltFlushResponse        flus;
  94.     UInt32                            i;
  95. };
  96. typedef union DriverGestaltInfo DriverGestaltInfo;
  97.  
  98. //------------------------------------------------------------------------------
  99. ------
  100. #pragma mark Prototypes
  101. //------------------------------------------------------------------------------
  102. ------
  103.  
  104.  
  105. void         DisplayAllDriveInfo (void);
  106.  
  107. void         DisplayDriverGestaltInfo    (short refNum, short device);
  108.  
  109. OSErr         DoDriverGestalt        (short
  110.     driverRefnum,
  111.                                  short
  112.             driveNumber,
  113.                                 OSType
  114.             gestaltSelector,
  115.  
  116.     DriverGestaltInfo    *response );
  117.  
  118. char*        DrvrRefToName        (short refNum);
  119. void         PrintNumVersion        (char *label, NumVersion version );
  120.  
  121.  
  122. // ---------------------------------------------------------------------------
  123. void     main     (void)
  124. // ---------------------------------------------------------------------------
  125. {
  126.     DisplayAllDriveInfo();
  127. }
  128.  
  129.  
  130. // ---------------------------------------------------------------------------
  131. void         DisplayAllDriveInfo (void)
  132. // ---------------------------------------------------------------------------
  133. //
  134. //  Display information ATA Driver GestaltInfo
  135. //
  136. {
  137.     register DrvQEl        *drvQElPtr;
  138.     char*                drvrNamePtr;
  139.     short                notused;
  140.     OSErr                status;
  141.  
  142.  
  143. #define DQEL    (*drvQElPtr)
  144.     drvQElPtr = (DrvQEl *) GetDrvQHdr()->qHead;
  145.  
  146.     printf("Disk Drive Queue Information:\n\n");
  147.     while (drvQElPtr != NULL) {
  148.  
  149.         drvrNamePtr = DrvrRefToName(DQEL.dQRefNum);
  150.  
  151.         printf("Drive: %d  %#s (%d) ",
  152.                 (int) DQEL.dQDrive,
  153. drvrNamePtr,DQEL.dQRefNum);
  154.  
  155.         switch (DQEL.qType) {
  156.         case 0:
  157.             printf(
  158.                 "%lu blocks (small drive)\n",
  159.                 (unsigned long) DQEL.dQDrvSz
  160.             );
  161.             break;
  162.         case 1:
  163.             printf(
  164.                 "%lu blocks\n",
  165.                 ((unsigned long) DQEL.dQDrvSz)
  166.                 + (((unsigned long) DQEL.dQDrvSz2) * 0x10000L)
  167.             );
  168.             break;
  169.         case 3:
  170.             printf(
  171.                 "%lu blocks (MFS present)\n",
  172.                 ((unsigned long) DQEL.dQDrvSz)
  173.             );
  174.             break;
  175.         default:
  176.             printf(
  177.                 "%lu %lu (unknown queue type)\n",
  178.                 ((unsigned long) DQEL.dQDrvSz),
  179.                 ((unsigned long) DQEL.dQDrvSz2)
  180.             );
  181.             break;
  182.         }
  183.  
  184. // Open driver
  185.         status = OpenDriver((ConstStr255Param)drvrNamePtr, ¬used);
  186.         if (status != noErr) {
  187.             printf("Cannot open %#s Error: %d\n", (int) status);
  188.         exit(EXIT_FAILURE);
  189.         }
  190.  
  191.         DisplayDriverGestaltInfo(DQEL.dQRefNum, DQEL.dQDrive);
  192.         drvQElPtr = (DrvQEl *) DQEL.qLink;
  193.     }
  194. #undef DQEL
  195. }
  196.  
  197.  
  198.  
  199.  
  200. // ---------------------------------------------------------------------------
  201. void         DisplayDriverGestaltInfo    (short refNum, short driveNum)
  202. // ---------------------------------------------------------------------------
  203. //
  204. //  Display DriverGestalt information.
  205. //
  206. {
  207.         OSErr                status;
  208.         DriverGestaltInfo    response;
  209.  
  210. // display driver version
  211.         status = DoDriverGestalt(refNum,driveNum, kdgVersion,
  212. &response);
  213.         if (status == noErr)
  214.             PrintNumVersion("\t'vers' - version:",
  215. response.vers.driverVersion);
  216.  
  217.  
  218. // display device type
  219.         status = DoDriverGestalt(refNum, driveNum, kdgDeviceType,
  220. &response);
  221.         if (status == noErr)
  222.             printf("\t'devt' - Device:\t'%.4s'\n",
  223.                 (char *) &response.devt.deviceType
  224.             );
  225.  
  226.  
  227. // display API info
  228.         status = DoDriverGestalt(refNum, driveNum, kdgInterface,
  229. &response);
  230.         if (status == noErr)
  231.             printf("\t'intf' - Interface:\t'%.4s'\n",
  232.                 (char *) &response.intf.interfaceType
  233.             );
  234.  
  235. // display boot record
  236.         status = DoDriverGestalt(refNum, driveNum, kdgBoot, &response);
  237.         if (status == noErr)
  238.             printf("\t'boot' - Boot PRAM:\t%08lx\n",
  239.                 (* ((unsigned long *) &response))
  240.             );
  241.  
  242. // display sync info
  243.         status = DoDriverGestalt(refNum, driveNum,  kdgSync,
  244. &response);
  245.         if (status == noErr)
  246.                     printf("\t'sync' - Driver is %s \n",
  247.  
  248.     (response.sync.behavesSynchronously)?"synchronous":"asynchronous");
  249.  
  250.  
  251. // display driver flush capablities
  252.         status = DoDriverGestalt(refNum, driveNum, kdgFlush,
  253. &response);
  254.         if (status == noErr)
  255.             printf("\t'flus' - Driver %s flush \n",
  256.  
  257.     (response.flus.canFlush)?"supports":"doesn't support");
  258.  
  259. // display power info
  260.         status = DoDriverGestalt(refNum, driveNum,
  261. kdgSupportsPowerCtl, &response);
  262.         if (status == noErr)  {
  263.                     printf("\t'psup' - Driver %s power
  264. switching\n",
  265.  
  266.     (response.powr.powerValue)?"supports":"doesn't support");
  267.  
  268.         status = DoDriverGestalt(refNum, driveNum, kdgMin3VPower,
  269. &response);
  270.         if (status == noErr)
  271.             printf("\t'pmx3' - 3.3V power consumption %d
  272. microWatts \n",
  273.                 (char *) &response.powr.powerValue);
  274.     }
  275.  
  276.  
  277.     printf("\n");
  278. }
  279.  
  280.  
  281. // ---------------------------------------------------------------------------
  282. OSErr         DoDriverGestalt        (short
  283.     driverRefnum,
  284.                                  short
  285.             driveNumber,
  286.                                 OSType
  287.             gestaltSelector,
  288.  
  289.     DriverGestaltInfo    *response )
  290. // ---------------------------------------------------------------------------
  291. //
  292. //  Do Driver Gestalt Call
  293. //
  294. {
  295.         DriverGestaltParam        pb;
  296.         OSErr                    status;
  297.  
  298.         CLEAR(pb);
  299.         pb.ioVRefNum     = driveNumber;
  300.         pb.ioCRefNum    = driverRefnum;
  301.         pb.csCode         = kDriverGestaltCode;
  302.         pb.driverGestaltSelector = gestaltSelector;
  303.  
  304.         status = PBStatusSync((ParmBlkPtr) &pb);
  305.  
  306.         if (status == noErr)
  307.             response->i  = pb.driverGestaltResponse;
  308. //        else printf("error %d\n",status);
  309.         return (status);
  310. }
  311.  
  312. // ---------------------------------------------------------------------------
  313. char*        DrvrRefToName(short refNum)
  314. // ---------------------------------------------------------------------------
  315. //
  316. //  lookup driver name in table
  317. //
  318.  
  319. {
  320.         AuxDCEHandle*        UTable  = (AuxDCEHandle*)
  321. LMGetUTableBase();
  322.         DCtlPtr                dctl;
  323.         Ptr                    p;
  324.  
  325.         if(!refNum) return ((char*) "\p<none>");
  326.  
  327.         dctl = (DCtlPtr) *UTable[~refNum];
  328.         p      =  dctl->dCtlDriver;
  329.         if( dctl->dCtlFlags  & 0x0040) p = (void*) *p;
  330.  
  331.         return  ( p?(char*) (p+18):(char*)"\p-Purged-");
  332. }
  333.  
  334. // ---------------------------------------------------------------------------
  335. void         PrintNumVersion        (char *label, NumVersion version )
  336. // ---------------------------------------------------------------------------
  337. //
  338. //    Decode version number info
  339. //
  340. {
  341.     char            *stage;
  342.  
  343.     switch (version.stage) {
  344.     case developStage:    stage = "d";        break;
  345.     case alphaStage:    stage = "a";        break;
  346.     case betaStage:        stage = "b";        break;
  347.     case finalStage:    stage = "";            break;
  348.     default:            stage = "?";        break;
  349.  
  350.     }
  351.     printf("%s %d.%d.%d",
  352.         label,
  353.         version.majorRev,
  354.         (version.minorAndBugRev>>4), (version.minorAndBugRev & 0xf),
  355.         stage);
  356.     if(version.stage != finalStage) printf(".%d",version.nonRelRev);
  357.  
  358.     printf(" (hex %08lx)\n",    (* ((unsigned long *) &version)));
  359. }
  360.  
  361.