⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 advioreg.c

📁 pc机上经由pci连接的ata和atapi设备驱动
💻 C
📖 第 1 页 / 共 4 页
字号:
                           unsigned int fr, unsigned int sc,
                           unsigned long lba,
                           unsigned int seg, unsigned int off,
                           long numSect, int multiCnt )

{

   // Reset error return data.

   sub_zero_return_data();
   ADP->flg = TRC_FLAG_ATA;
   ADP->ct  = TRC_TYPE_APDI;
   ADP->cmd = cmd;
   ADP->fr1 = fr;
   ADP->sc1 = sc;
   ADP->dh1 = CB_DH_LBA | ADP->devBit;
   // ADP->dc1 = 0x00;     // see sub_setup_command()
   ADP->lbaSize = LBA28;
   ADP->lbaHigh1 = 0L;
   ADP->lbaLow1 = lba;

   // these commands transfer only 1 sector
   if (    ( cmd == CMD_IDENTIFY_DEVICE )
        || ( cmd == CMD_IDENTIFY_DEVICE_PACKET )
        || ( cmd == CMD_READ_BUFFER )
      )
      numSect = 1;

   // only Read Multiple uses multiCnt
   if ( cmd != CMD_READ_MULTIPLE )
      multiCnt = 1;

   ADP->ns  = numSect;
   ADP->mc  = multiCnt;

   return exec_pio_data_in_cmd( seg, off, numSect, multiCnt );
}

//*************************************************************
//
// reg_pio_data_in_lba48() - Easy way to execute a PIO Data In command
//                           using an LBA sector address.
//
//*************************************************************

int reg_pio_data_in_lba48( int cmd,
                           unsigned int fr, unsigned int sc,
                           unsigned long lbahi, unsigned long lbalo,
                           unsigned int seg, unsigned int off,
                           long numSect, int multiCnt )

{

   // Reset error return data.

   sub_zero_return_data();
   ADP->flg = TRC_FLAG_ATA;
   ADP->ct  = TRC_TYPE_APDI;
   ADP->cmd = cmd;
   ADP->fr1 = fr;
   ADP->sc1 = sc;
   ADP->dh1 = CB_DH_LBA | ADP->devBit;
   // ADP->dc1 = 0x00;     // see sub_setup_command()
   ADP->lbaSize = LBA48;
   ADP->lbaHigh1 = lbahi;
   ADP->lbaLow1 = lbalo;

   // only Read Multiple Ext uses multiCnt
   if ( cmd != CMD_READ_MULTIPLE_EXT )
      multiCnt = 1;

   ADP->ns  = numSect;
   ADP->mc  = multiCnt;

   return exec_pio_data_in_cmd( seg, off, numSect, multiCnt );
}

//*************************************************************
//
// exec_pio_data_out_cmd() - Execute a PIO Data Out command.
//
// See ATA-2 Section 9.4, ATA-3 Section 9.4,
// ATA-4 Section 8.7 Figure 11.
//
//*************************************************************

static int exec_pio_data_out_cmd(
                             unsigned int seg, unsigned int off,
                             long numSect, int multiCnt );

static int exec_pio_data_out_cmd(
                             unsigned int seg, unsigned int off,
                             long numSect, int multiCnt )

{
   unsigned char status;
   int loopFlag = 1;
   long wordCnt;
   unsigned int saveSeg = seg;
   unsigned int saveOff = off;

   // mark start of PDO cmd in low level trace

   trc_llt( 0, 0, TRC_LLT_S_PDO );

   // reset Bus Master Error bit

   sub_writeBusMstrStatus( BM_SR_MASK_ERR );

   // Set command time out.

   tmr_set_timeout();

   // Select the drive - call the sub_select function.
   // Quit now if this fails.

   if ( sub_select() )
   {
      sub_trace_command();
      trc_llt( 0, 0, TRC_LLT_E_PDO );
      ADP->reg_drq_block_call_back = 0;
      return 1;
   }

   // Set up all the registers except the command register.

   sub_setup_command();

   // Start the command by setting the Command register.  The drive
   // should immediately set BUSY status.

   pio_outbyte( CB_CMD, ADP->cmd );

   // Waste some time by reading the alternate status a few times.
   // This gives the drive time to set BUSY in the status register on
   // really fast systems.  If we don't do this, a slow drive on a fast
   // system may not set BUSY fast enough and we would think it had
   // completed the command when it really had not even started the
   // command yet.

   DELAY400NS;

   // Poll for not BUSY or time out.
   // Note: No interrupt is generated for the
   // first sector of a write command.  Well...
   // that's not really true we are working with
   // a PCMCIA PC Card ATA device.

   trc_llt( 0, 0, TRC_LLT_PNBSY );
   while ( 1 )
   {
      status = pio_inbyte( CB_ASTAT );
      if ( ( status & CB_STAT_BSY ) == 0 )
         break;
      if ( tmr_chk_timeout() )
      {
         trc_llt( 0, 0, TRC_LLT_TOUT );
         ADP->to = 1;
         ADP->ec = 47;
         trc_llt( 0, ADP->ec, TRC_LLT_ERROR );
         loopFlag = 0;
         break;
      }
   }

   // This loop writes each sector.

   while ( loopFlag )
   {
      // WRITE_LOOP:
      //
      // NOTE NOTE NOTE ...  The primary status register (1f7) MUST NOT be
      // read more than ONCE for each sector transferred!  When the
      // primary status register is read, the drive resets IRQ 14.  The
      // alternate status register (3f6) can be read any number of times.
      // For correct results, transfer the 256 words (REP OUTSW), wait for
      // BSY=0 and then read the primary status register.  AS
      // SOON as BOTH the primary status register has been read AND the
      // last of the 256 words has been written, the drive is allowed to
      // generate the next IRQ 14 (newer and faster drives could generate
      // the next IRQ 14 in 50 microseconds or less).  If the primary
      // status register is read more than once, there is the possibility
      // of a race between the drive and the software and the next IRQ 14
      // could be reset before the system interrupt controller sees it.

      // If BSY=0 and DRQ=1, transfer the data,
      // even if we find out there is an error later.

      if ( ( status & ( CB_STAT_BSY | CB_STAT_DRQ ) ) == CB_STAT_DRQ )
      {

         // increment number of DRQ packets

         ADP->drqPackets ++ ;

         // determine the number of sectors to transfer

         wordCnt = multiCnt ? multiCnt : 1;
         if ( wordCnt > numSect )
            wordCnt = numSect;
         wordCnt = wordCnt * 256;

         // Quit if buffer overrun.
         // If DRQ call back in use:
         // a) Call DRQ block call back function.
         // b) Adjust buffer address.

         if ( ADP->reg_drq_block_call_back )
         {
            if ( ( wordCnt << 1 ) > ADP->reg_buffer_size )
            {
               ADP->ec = 61;
               trc_llt( 0, ADP->ec, TRC_LLT_ERROR );
               break;   // go to WRITE_DONE
            }
            ADP->drqPacketSize = ( wordCnt << 1 );
            (* ADP->reg_drq_block_call_back) ( ADP );
            seg = saveSeg;
            off = saveOff;
         }
         else
         {
            if ( ( ADP->totalBytesXfer + ( wordCnt << 1 ) )
                 > ADP->reg_buffer_size )
            {
               ADP->ec = 61;
               trc_llt( 0, ADP->ec, TRC_LLT_ERROR );
               break;   // go to WRITE_DONE
            }
         }

         // Do the REP OUTSW to write the data for one DRQ block.

         ADP->totalBytesXfer += ( wordCnt << 1 );
         pio_drq_block_out( CB_DATA, seg, off, wordCnt );

         DELAY400NS;    // delay so device can get the status updated

         // Note: The drive should have dropped DATA REQUEST and
         // raised BUSY by now.

         // Decrement the count of sectors to be transferred
         // and increment buffer address.

         numSect = numSect - ( multiCnt ? multiCnt : 1 );
         seg = seg + ( 32 * ( multiCnt ? multiCnt : 1 ) );
      }

      // So was there any error condition?

      if ( status & ( CB_STAT_BSY | CB_STAT_DF | CB_STAT_ERR ) )
      {
         ADP->ec = 41;
         trc_llt( 0, ADP->ec, TRC_LLT_ERROR );
         break;   // go to WRITE_DONE
      }

      // DRQ should have been set -- was it?

      if ( ( status & CB_STAT_DRQ ) == 0 )
      {
         ADP->ec = 42;
         trc_llt( 0, ADP->ec, TRC_LLT_ERROR );
         break;   // go to WRITE_DONE
      }

      // Poll for not BUSY -or- wait for time out.

      reg_poll_not_busy( 45 );

      // Read the primary status register.  In keeping with the rules
      // stated above the primary status register is read only ONCE.

      status = pio_inbyte( CB_STAT );

      // If there was a time out error, go to WRITE_DONE.

      if ( ADP->ec )
         break;   // go to WRITE_DONE

      // If all of the requested sectors have been transferred, make a
      // few more checks before we exit.

      if ( numSect < 1 )
      {
         // Since the drive has transferred all of the sectors without
         // error, the drive MUST not have BUSY, DEVICE FAULT, DATA REQUEST
         // or ERROR status at this time.

         if ( status & ( CB_STAT_BSY | CB_STAT_DF | CB_STAT_DRQ | CB_STAT_ERR ) )
         {
            ADP->ec = 43;
            trc_llt( 0, ADP->ec, TRC_LLT_ERROR );
            break;   // go to WRITE_DONE
         }

         // All sectors have been written without error, go to WRITE_DONE.

         break;   // go to WRITE_DONE

      }

      //
      // This is the end of the write loop.  If we get here, the loop
      // is repeated to write the next sector.  Go back to WRITE_LOOP.

   }

   // read the output registers and trace the command.

   sub_trace_command();

   // BMCR/BMIDE Error=1?

   if ( sub_readBusMstrStatus() & BM_SR_MASK_ERR )
   {
      ADP->ec = 78;                  // yes
      trc_llt( 0, ADP->ec, TRC_LLT_ERROR );
   }

   // WRITE_DONE:

   // mark end of PDO cmd in low level trace

   trc_llt( 0, 0, TRC_LLT_E_PDO );

   // reset reg_drq_block_call_back to NULL (0)

   ADP->reg_drq_block_call_back = 0;

   // All done.  The return values of this function are described in
   // ADVIO.H.

   if ( ADP->ec )
      return 1;
   return 0;
}

//*************************************************************
//
// reg_pio_data_out_chs() - Execute a PIO Data Out command.
//
// See ATA-2 Section 9.4, ATA-3 Section 9.4,
// ATA-4 Section 8.7 Figure 11.
//
//*************************************************************

int reg_pio_data_out_chs( int cmd,
                          unsigned int fr, unsigned int sc,
                          unsigned int cyl, unsigned int head, unsigned int sect,
                          unsigned int seg, unsigned int off,
                          long numSect, int multiCnt )

{

   // Reset error return data.

   sub_zero_return_data();
   ADP->flg = TRC_FLAG_ATA;
   ADP->ct  = TRC_TYPE_APDO;
   ADP->cmd = cmd;
   ADP->fr1 = fr;
   ADP->sc1 = sc;
   ADP->sn1 = sect;
   ADP->cl1 = cyl & 0x00ff;
   ADP->ch1 = ( cyl & 0xff00 ) >> 8;
   ADP->dh1 = ADP->devBit| ( head & 0x0f );
   // ADP->dc1 = 0x00;     // see sub_setup_command()
   ADP->lbaSize = LBACHS;

   // these commands transfer only 1 sector
   if ( cmd == CMD_WRITE_BUFFER )
      numSect = 1;

   // only Write Multiple and CFA Write Multiple W/O Erase uses multCnt
   if (    ( cmd != CMD_WRITE_MULTIPLE )
        && ( cmd != CMD_CFA_WRITE_MULTIPLE_WO_ERASE )
      )
      multiCnt = 1;

   ADP->ns  = numSect;
   ADP->mc  = multiCnt;

   return exec_pio_data_out_cmd( seg, off, numSect, multiCnt );
}

//*************************************************************
//
// reg_pio_data_out_lba28() - Easy way to execute a PIO Data In command
//                            using an LBA sector address.
//
//*************************************************************

int reg_pio_data_out_lba28( int cmd,
                            unsigned int fr, unsigned int sc,
                            unsigned long lba,
                            unsigned int seg, unsigned int off,
                            long numSect, int multiCnt )

{

   // Reset error return data.

   sub_zero_return_data();
   ADP->flg = TRC_FLAG_ATA;
   ADP->ct  = TRC_TYPE_APDO;
   ADP->cmd = cmd;
   ADP->fr1 = fr;
   ADP->sc1 = sc;
   ADP->dh1 = CB_DH_LBA | ADP->devBit;
   // ADP->dc1 = 0x00;     // see sub_setup_command()
   ADP->lbaSize = LBA28;
   ADP->lbaHigh1 = 0;
   ADP->lbaLow1 = lba;

   // these commands transfer only 1 sector
   if ( cmd == CMD_WRITE_BUFFER )
      numSect = 1;

   // only Write Multiple and CFA Write Multiple W/O Erase uses multCnt
   if (    ( cmd != CMD_WRITE_MULTIPLE )
        && ( cmd != CMD_CFA_WRITE_MULTIPLE_WO_ERASE )
      )
      multiCnt = 1;

   ADP->ns  = numSect;
   ADP->mc  = multiCnt;

   return exec_pio_data_out_cmd( seg, off, numSect, multiCnt );
}

//*************************************************************
//
// reg_pio_data_out_lba48() - Easy way to execute a PIO Data In command

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -