📄 lptio.c
字号:
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (c) 1995-1998 Microsoft Corporation
Module Name:
lptio.c
Abstract:
P1284 Standard Centronic port output function
Notes:
--*/
#include <windows.h>
#include <ceddk.h>
#include <pardbg.h>
#include "1284comm.h"
#include "tmtick.h"
#include "comdef.h"
#include "1284neg.h"
#include "lptio.h"
/* notifications passed in Event of Notification function */
#define CN_RECEIVE 1 /* bytes are available in the input queue */
#define CN_TRANSMIT 2 /* fewer than wOutTrigger bytes still */
/* remain in the output queue waiting */
/* to be transmitted. */
#define CN_EVENT 4 /* an enabled event has occurred */
BYTE BusyWaitPenalty[]={0,0,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3};
#define LPT_CLEAR_MASK 0x40
#define LPT_STROBE_HI 0x0D
#define LPT_STROBE_LO 0x0C
#define LPT_STATUS_BITS 0xF8
#define LPT_BITS_INVERT 0x48
#define LPT_NOTBUSY 0x80
#define LPT_PAPEROUT 0x20
#define LPT_SELECT 0x10
#define LPT_NFAULT 0x08
#define LPT_TimeOut 0x01
/****************************************************************************
* Modification done by Maneesh Gupta
*
* Multiplier used to access the printer registers.
****************************************************************************/
extern unsigned dwPrinterRegMultiplier;
/****************************************************************************
* End of Modification done by Maneesh Gupta
****************************************************************************/
void OutByte(ULONG dataport, BYTE databyte) {
WRITE_PORT_UCHAR((PUCHAR)dataport,databyte);
// Write to the Control port
/****************************************************************************
* Modification done by Maneesh Gupta
*
* Register acces has been modifed for differnt platform with the help of
* dwRegisterMultiplier
****************************************************************************/
dataport+= (2 * dwPrinterRegMultiplier);
/****************************************************************************
* End of Modification done by Maneesh Gupta
****************************************************************************/
databyte = (READ_PORT_UCHAR((PUCHAR)dataport) & LPT_CLEAR_MASK)
| LPT_STROBE_HI;
WRITE_PORT_UCHAR((PUCHAR)dataport,databyte);
databyte = (READ_PORT_UCHAR((PUCHAR)dataport) & LPT_CLEAR_MASK)
| LPT_STROBE_LO;
WRITE_PORT_UCHAR((PUCHAR)dataport,databyte);
}
BYTE CheckStatus(unsigned port) {
BYTE bRet;
BYTE bStatus;
do {
bRet= bStatus= READ_PORT_UCHAR((PUCHAR)port);
} while (bStatus != READ_PORT_UCHAR((PUCHAR)port));
bStatus&= LPT_STATUS_BITS;
bStatus^= LPT_BITS_INVERT;
if (!(bStatus & (LPT_PAPEROUT | LPT_NFAULT)) &&
(bStatus & LPT_SELECT) && (bStatus & LPT_NOTBUSY))
bRet=0;
return bRet;
}
//---------------------------------------------------------------------------
DWORD
SetError
(
BYTE bData
)
// RETURN
// CE_PTO --> printer busy
// CE_OOP --> out of paper
// CE_IOE --> printer error
// CE_DNS --> device not selected
//---------------------------------------------------------------------------
{
// for Centronic, nBusy high means not busy
// no invert needed.
// proecss error conditions
if (bData == 0x30)
{
// This is ECP's way of saying device not selected (in ELPT case)
return (CE_DNS);
}
if ((bData & LPT_PAPEROUT) == LPT_PAPEROUT)
{
return (CE_OOP);
}
if ((bData & LPT_NFAULT) == LPT_NFAULT)
{
return (CE_IOE);
}
if (((bData & LPT_SELECT) ^ LPT_SELECT) == LPT_SELECT)
{
return (CE_DNS);
}
if ((bData & LPT_NOTBUSY) == LPT_NOTBUSY)
{
return (0);
}
else // printer is busy
{
return (CE_PTO);
}
}
//---------------------------------------------------------------------------
BOOL
CheckPrinterStatus
(
PPortInformation PortObj,
unsigned uStatusPort
)
{
BYTE bData;
bData = CheckStatus(uStatusPort);
if (bData)
{
PortObj->pPortData.dwLastError = PortObj->pPortData.dwCommError;
/****************************************************************************
* Modification done by Maneesh Gupta
*
* Register acces has been modifed for differnt platform with the help of
* dwRegisterMultiplier
****************************************************************************/
bData = (BYTE)READ_PORT_UCHAR((PUCHAR)PortObj->ulBase+ 1 * dwPrinterRegMultiplier);
/****************************************************************************
* End of Modification done by Maneesh Gupta
****************************************************************************/
bData &= LPT_STATUS_BITS;
bData ^= LPT_BITS_INVERT;
PortObj->pPortData.dwCommError = SetError(bData);
return (FALSE);
}
else
return (TRUE);
}
//---------------------------------------------------------------------------
// Probe BUSY after data strobes
//---------------------------------------------------------------------------
BOOL GetBusyDelay
(
PPortInformation PortObj,
LPSTR lpData
)
// Caller checks current mode and phase.
{
ULONG uDataPort, uStatusPort;
int ticks;
BYTE i;
// Initialize the LPT Port variables
uDataPort = PortObj->ulBase;
/****************************************************************************
* Modification done by Maneesh Gupta
*
* Register acces has been modifed for differnt platform with the help of
* dwRegisterMultiplier
****************************************************************************/
uStatusPort = uDataPort + 1 * dwPrinterRegMultiplier;
/****************************************************************************
* End of Modification done by Maneesh Gupta
****************************************************************************/
DEBUGMSG(ZONE_MAXIO,(TEXT("[GetBusyDelay]\r\n")));
// We'll start with the best case
PortObj->bWaitBusy = 0;
// We'll test for 200 loops
for (ticks = 200; ticks > 0; ticks--)
{
if (CheckPrinterStatus (PortObj, uStatusPort))
{
OutByte(uDataPort, *lpData ++);
// If we have already calculated a number
// use it. This way the final number should
// be the worst case.
for (i = 0; i < PortObj->bWaitBusy; i++)
READ_PORT_UCHAR((PUCHAR)uStatusPort);
// look for BUSY high, need to make sure printer responds host strobe
// maximum no longer than 20us
for (i = 0; i < MAX_WAIT_BUSY; i++)
{
if (!(READ_PORT_UCHAR((PUCHAR)uStatusPort) & 0x80))
goto bp_break;
// Some very bizarre compiler bug is preventing us
// from using a break here.
}
bp_break:
switch (i)
{
case 0:
case MAX_WAIT_BUSY:
// No adjustment required.
break;
default:
PortObj->bWaitBusy = (BYTE)(PortObj->bWaitBusy + i);
if (PortObj->bWaitBusy > MAX_WAIT_BUSY)
{
// Something probably seriously wrong here...
PortObj->bWaitBusy = MAX_WAIT_BUSY;
// might as well exit
ticks = 0;
}
break;
}
PortObj->pPortData.QOutCount --;
PortObj->pPortData.QOutGet ++;
if (PortObj->pPortData.QOutCount == 0)
{
return (TRUE);
}
}
}
DEBUGMSG(ZONE_IO,(TEXT("[GetBusyDelay] Delay = %Xh\r\n"),PortObj->bWaitBusy));
return (TRUE);
}
//---------------------------------------------------------------------------
// Synchronous printing procedures
//---------------------------------------------------------------------------
BOOL
LPT_WriteFewBytes
(
PPortInformation PortObj,
LPSTR lpData
)
// Caller checks current mode and phase.
{
ULONG uDataPort, uStatusPort;
int ticks;
// int i;
DEBUGMSG(ZONE_IO,(TEXT("[LPT_WriteFewBytes] : Entry\r\n")));
// Initialize the LPT Port variables
uDataPort = PortObj->ulBase;
/****************************************************************************
* Modification done by Maneesh Gupta
*
* Register acces has been modifed for differnt platform with the help of
* dwRegisterMultiplier
****************************************************************************/
uStatusPort = uDataPort + 1 * dwPrinterRegMultiplier;
/****************************************************************************
* End of Modification done by Maneesh Gupta
****************************************************************************/
// If the printer is busy when we enter the loop then we shouldn't wait.
if (CheckPrinterStatus(PortObj, uStatusPort))
{
DEBUGMSG(ZONE_IO,(TEXT("[LPT_WriteFewBytes] : Printer not busy\r\nWriting %u bytes:"),PortObj->pPortData.QOutCount));
// if (PortObj->bWaitBusy == (BYTE)-1)
// return (GetBusyDelay(PortObj, lpData));
for (ticks = LPT_MAX_WRITE; ticks > 0; ticks--)
{
if (CheckPrinterStatus (PortObj, uStatusPort))
{
DEBUGMSG(0,(TEXT("%X "),*lpData));
OutByte(uDataPort, *lpData ++);
PortObj->pPortData.QOutCount --;
PortObj->pPortData.QOutGet ++;
// Wait for the time calculated during
// GetBusyDelay()
// for (i = 0; i < PortObj->bWaitBusy; i++)
// READ_PORT_UCHAR((PUCHAR)uStatusPort);
if (PortObj->pPortData.QOutCount == 0) {
DEBUGMSG(ZONE_IO,(TEXT("\r\n[LPT_WriteFewBytes] : write completed.\r\n")));
return (TRUE);
}
// There is probably a better way to do this!
// ticks -= BusyWaitPenalty[PortObj->bWaitBusy];
}
}
}
DEBUGMSG(ZONE_IO,(TEXT("\r\n[LPT_WriteFewBytes] : %u more bytes.\r\n"),PortObj->pPortData.QOutCount));
return (TRUE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -