home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cdisk.zip / INIT / INIT.C
Text File  |  1992-07-06  |  6KB  |  203 lines

  1. /* Ex.INIT section, combination ISA and MicroChannel bus driver
  2.    
  3.    This driver is loaded in the config.sys file with the DEVICE=
  4.    statement. For ISA configuration, the first parameter to the
  5.    "DEVICE=" is the base port address. The next parameter is the
  6.    board base address. All numbers are in hex. For Micro Channel
  7.    configuration, the board address and port address are read
  8.    from the board POS regs.
  9. */
  10.  
  11. PHYSADDR    board_address;     /* base board address            */
  12. USHORT      port_address;      /* base port address             */
  13. USHORT      bus = 0;           /* default ISA bus               */
  14. REQBLK      ABIOS_r_blk;       /* ABIOS request block           */
  15. LIDBLK      ABIOS_l_blk;       /* ABIOS LID block               */
  16. USHORT      lid_blk_size;      /* size of LID block             */
  17. CARD        card[MAX_NUM_SLOTS+1];/* array for IDs and POS reg  */
  18. CARD        *pcard;            /* pointer to card array         */
  19. USHORT      matches = 0;       /* match flag for card ID        */
  20. USHORT      port1,port2;       /* temp variables for addr calc  */
  21.  
  22. char     NoMatchMsg[]  = " no match for DESIRED card ID found.\r\n";
  23. char     MainMsgMCA[]  = "\r\nOS/2 Micro Channel (tm) Device
  24. Driver installed.\r\n";
  25. char     MainMsg[] = "\r\nOS/2 ISA Device Driver installed.\r\n";
  26.  
  27. /* prototypes */
  28.  
  29. int      hex2bin(char);
  30. USHORT   get_POS();
  31. UCHAR    get_pos_data();
  32. .
  33. .
  34. * Device Driver Strategy Section Here *
  35. .
  36. .
  37. int  hex2bin(char c)
  38. {
  39.     if(c < 0x3a)
  40.         return (c - 48);
  41.     else
  42.         return (( c & 0xdf) - 55);
  43. }
  44.  
  45. USHORT get_POS(USHORT slot_num,USHORT far *card_ID,
  46.        UCHAR far *pos_regs)
  47. {
  48. USHORT rc, i, lid;
  49.     
  50.     if (GetLIDEntry(0x10, 0, 1, &lid))    /* get LID for POS   */
  51.         return (1);
  52.  
  53.     /* Get the size of the LID request block */
  54.  
  55.     ABIOS_l_blk.f_parms.req_blk_len = sizeof(struct lid_block_def);
  56.     ABIOS_l_blk.f_parms.LID = lid;
  57.     ABIOS_l_blk.f_parms.unit = 0;;
  58.     ABIOS_l_blk.f_parms.function = GET_LID_BLOCK_SIZE;
  59.     ABIOS_l_blk.f_parms.ret_code = 0x5a5a;
  60.     ABIOS_l_blk.f_parms.time_out = 0;
  61.  
  62.     if (ABIOSCall(lid,0,(void far *)&ABIOS_l_blk))
  63.         return (1);
  64.  
  65.     lid_blk_size = ABIOS_l_blk.s_parms.blk_size; /* Get blk siz*/
  66.  
  67.     /* Fill POS regs and card ID with FF in case               */
  68.  
  69.     *card_ID = 0xFFFF;
  70.     for (i=0; i<NUM_POS_BYTES; i++) { pos_regs[i] = 0x00; }; 
  71.  
  72.     /* Get the POS registers and card ID for the commanded slot*/
  73.  
  74.     ABIOS_r_blk.f_parms.req_blk_len = lid_blk_size;
  75.     ABIOS_r_blk.f_parms.LID = lid;
  76.     ABIOS_r_blk.f_parms.unit = 0;;
  77.     ABIOS_r_blk.f_parms.function = READ_POS_REGS_CARD;
  78.     ABIOS_r_blk.f_parms.ret_code = 0x5a5a;
  79.     ABIOS_r_blk.f_parms.time_out = 0;
  80.     
  81.     ABIOS_r_blk.s_parms.slot_num = (UCHAR)slot_num & 0x0F;
  82.     ABIOS_r_blk.s_parms.pos_buf = (void far *)pos_regs;
  83.     ABIOS_r_blk.s_parms.card_ID = 0xFFFF;
  84.     
  85.     if (ABIOSCall(lid,0,(void far *)&ABIOS_r_blk))
  86.        rc = 1;
  87.      else {                                       /* Else      */
  88.        *card_ID = ABIOS_r_blk.s_parms.card_ID;   /* Setcard    */
  89.        rc = 0;
  90.       }
  91.     FreeLIDEntry(lid);
  92.     return(rc);
  93.     
  94. }
  95.  
  96. UCHAR get_pos_data (int slot, int reg)
  97. {
  98.    UCHAR pos;
  99.    CARD *cptr;
  100.  
  101.    cptr = &card[slot-1];            /* set ptr to beg of array */
  102.    if (reg == 0)                    /* card ID                 */
  103.       pos = LOUSHORT(cptr->card_ID);
  104.    else
  105.      if ( reg == 1)
  106.       pos = HIUSHORT(cptr->card_ID);
  107.    else
  108.       pos = cptr->pos_regs[reg-2];  /* POS data register       */
  109.    return (pos);
  110. }
  111.  
  112. /* Device Initialization Routine */
  113.  
  114. int Init(PREQPACKET rp)
  115. {
  116.     USHORT lid;
  117.  
  118.     register char far *p;
  119.  
  120.     /* store DevHlp entry point */
  121.  
  122.     DevHlp = rp->s.Init.DevHlp;/* save DevHlp entry point      */
  123.  
  124.     if (!(GetLIDEntry(0x10, 0, 1, &lid))){/* get LID for POS   */
  125.       FreeLIDEntry(lid);
  126.  
  127.       /* Micro Channel (tm) setup section */
  128.  
  129.       bus = 1;                      /* Micro Channel bus       */
  130.  
  131.       /*    Get the POS data and card ID for each of 8 slots   */
  132.  
  133.       for (i=0;i <= MAX_NUM_SLOTS; i++) 
  134.          get_POS(i+1,(FARPOINTER)&card[i].card_ID,
  135.             (FARPOINTER)card[i].pos_regs);
  136.  
  137.       matches = 0;
  138.       for (i=0, pcard = card; i <= MAX_NUM_SLOTS; i++, pcard++){
  139.          if (pcard->card_ID == DESIRED_ID) { 
  140.             matches = 1;
  141.             break;
  142.             }
  143.          }
  144.  
  145.       if (matches == 0) {           /* no matches found        */
  146.          DosPutMessage(1, 8, devhdr.DHname);
  147.          DosPutMessage(1,strlen(NoMatchMsg),NoMatchMsg);
  148.          rp->s.InitExit.finalCS = (OFF) 0;
  149.          rp->s.InitExit.finalDS = (OFF) 0;
  150.          return (RPDONE | RPERR | ERROR_BAD_COMMAND);
  151.          }
  152.  
  153.       /* calculate the board address from the POS regs */
  154.  
  155.       board_address = ((unsigned long) get_pos_data(i+1, 4)
  156.       << 16) | ((unsigned long)(get_pos_data(i+1, 3) & 1) << 15);
  157.  
  158.       /* calculate the port address from the POS regs data */
  159.  
  160.       port1 = (get_pos_data(i+1, 3) << 8) & 0xf800;
  161.       port2 = (get_pos_data(i+1, 2) << 3) & 0x07e0;
  162.       port_address = (port1 | port2);
  163.  
  164.       }
  165.    else
  166.       {
  167.       /* ISA bus setup */
  168.       bus = 0;                      /* ISA bus                 */
  169.  
  170.     /* get parameters, port addr and base mem addr */
  171.  
  172.       for (p = rp->s.Init.args; *p && *p != ' ';++p);/* sk name*/
  173.       for (; *p == ' '; ++p);       /* skip blanks after name  */
  174.       if (*p)
  175.        {
  176.        port_address = 0;
  177.        board_address=0;           /* i/o port address          */
  178.        for (; *p != ' '; ++p)     /* get port address          */
  179.        port_address = (port_address << 4) + (hex2bin(*p));
  180.        for (; *p == ' '; ++p);    /* skip blanks after address */
  181.        for (; *p != '\0'; ++p)    /* get board address         */
  182.        board_address = (board_address << 4) + (hex2bin(*p));
  183.        }
  184.     }
  185.  
  186.     if (bus)
  187.        DosPutMessage(1,strlen(MainMsgMCA),MainMsgMCA);
  188.     else
  189.        DosPutMessage(1,strlen(MainMsg),MainMsg);
  190.  
  191.        /* send back our end values to os/2 */
  192.         
  193.     if (SegLimit(HIUSHORT((void far *) Init),
  194.         &rp->s.InitExit.finalCS) ||
  195.         SegLimit(HIUSHORT((void far *) MainMsg),
  196.         &rp->s.InitExit.finalDS))
  197.         Abort();
  198.  
  199.     return (RPDONE);
  200. }
  201.  
  202.  
  203.