home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Examples / DriverKit / SCSITape / PostLoad.tproj / PostLoad.m < prev   
Encoding:
Text File  |  1994-07-13  |  2.9 KB  |  124 lines

  1. /*
  2.  *      Copyright (c) 1993 NeXT Computer, Inc.  All rights reserved. 
  3.  *
  4.  * Post-Load command for SCSITape.   Creates device nodes /dev/rst<n>,
  5.  * /dev/nrst<n>, /dev/rxt<n>, and /dev/nrxt<n> for up to 4 SCSI tape
  6.  * units.
  7.  *
  8.  * HISTORY
  9.  * 20-Apr-93    Phillip Dibner at NeXT
  10.  *      Created. 
  11.  */
  12.  
  13. #import <driverkit/IODeviceMaster.h>
  14. #import <driverkit/IODevice.h>
  15. #import "SCSITapeTypes.h"
  16. #import <errno.h>
  17. #import <libc.h>
  18.  
  19.  
  20. #define PATH_NAME_SIZE 10
  21. #define DEV_STRING "/dev/"
  22. #define INSTANCE_STRING "Instance="
  23. #define ST_INIT_ERR_STRING "Error initializing SCSI Tape driver"
  24. #define NTAPE_NAMES 4
  25.  
  26. #define DEV_MOD 020666
  27. #define DEV_UMASK 0
  28.  
  29. char path [PATH_NAME_SIZE];
  30. char *scsiTapeNames[] = {"rst", "nrst", "rxt", "nrxt"};
  31. int scsiTapeDevFlags[] = {0, 1, 2, 3}; /* bit 0 = no rewind, bit 1 = Exabyte */
  32.  
  33. int
  34. main(int argc, char **argv)
  35. {
  36.     IOReturn            ret = IO_R_INVALID;
  37.     IOObjectNumber        tag;
  38.     IOString            kind;
  39.     int                major, minor, iUnit, iRet = 0;
  40.     unsigned int        count = 1;
  41.     IODeviceMaster        *devMaster;
  42.     int                i;
  43.  
  44.     devMaster = [IODeviceMaster new];
  45.  
  46.     for (iUnit = 0; iUnit < NST; iUnit ++) {
  47.     bzero (path, PATH_NAME_SIZE);
  48.     sprintf (path, "%s%s%d", DEV_STRING, "st", iUnit);
  49.  
  50.     /*
  51.      * Find this instance of the SCSI Tape driver
  52.      */
  53.  
  54.     ret = [devMaster lookUpByDeviceName: path + strlen (DEV_STRING)
  55.         objectNumber: &tag 
  56.         deviceKind: &kind];
  57.  
  58. #ifdef DEBUG
  59. printf (" unit %d, obj name %s\n", iUnit, path);
  60. printf (" ret = %d\n", ret);
  61. #endif DEBUG
  62.  
  63.     /*
  64.      * Special work to do for the first SCSITape object
  65.      */
  66.     if (iUnit == 0) {
  67.  
  68.         /*
  69.          * If we couldn't find the first one, we will return failure.
  70.          */
  71.         if (ret != IO_R_SUCCESS) {
  72.         printf ("%s: couldn't find driver. Returned %d\n",
  73.             ST_INIT_ERR_STRING, ret);
  74.         iRet = -1;
  75.         } 
  76.  
  77.         /*
  78.          * Query the object for its major device number
  79.          */
  80.         major = -1;
  81.         ret = [devMaster getIntValues:&major 
  82.         forParameter:"IOMajorDevice" objectNumber:tag
  83.         count:&count];
  84.         if (ret != IO_R_SUCCESS) {
  85.         printf ("%s: couldn't get major number:  Returned %d.\n",
  86.             ST_INIT_ERR_STRING, ret);
  87.         iRet = -1;
  88.         }
  89.     }
  90.  
  91.     /*
  92.      * Remove old devs and create new ones for this unit.
  93.      */
  94.     for (i = 0; i < NTAPE_NAMES; i++) {
  95.  
  96.         bzero (path, PATH_NAME_SIZE);
  97.         sprintf (path, "%s%s%d", DEV_STRING, scsiTapeNames [i], iUnit);
  98.  
  99.         if (unlink (path)) {
  100.         if (errno != ENOENT) {
  101.             printf ("%s: could not delete old %s.  Errno is %d\n",
  102.             ST_INIT_ERR_STRING, path, errno);
  103.             iRet = -1; 
  104.         }
  105.         }
  106.  
  107.  
  108.         /*
  109.          * If we found this object, create new device nodes
  110.          */
  111.         if (ret == IO_R_SUCCESS) {
  112.         minor = (iUnit << 3) | scsiTapeDevFlags [i];
  113.  
  114.         umask (DEV_UMASK);
  115.         if (mknod(path, DEV_MOD, (major << 8) | minor)) {
  116.             printf ("%s: could not create %s.  Errno is %d\n",
  117.             ST_INIT_ERR_STRING, path, errno);
  118.             iRet = -1;
  119.         }
  120.         }
  121.     }
  122.     } /* for iUnit */
  123.     return iRet;
  124. }