Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
13 KiB

  1. /*++ BUILD Version: 0001
  2. *
  3. * WOW v1.0
  4. *
  5. * Copyright (c) 1991, Microsoft Corporation
  6. *
  7. * WUCOMM.H
  8. * WOW32 16-bit User API support
  9. *
  10. * History:
  11. * Created 07-Mar-1991 by Jeff Parsons (jeffpar)
  12. * Updated Dec-1992 by Craig Jones (v-cjones)
  13. --*/
  14. #include "wowcomm.h"
  15. // these limits set as doc'd in Win3.1 Prog. ref. for OpenComm()
  16. #define NUMCOMS 9 // max avail COM's
  17. #define NUMLPTS 3 // max available LPT's
  18. #define NUMPORTS NUMCOMS+NUMLPTS // max # of entries in PortTab[]
  19. // com port indicies into PortTab[]
  20. #define COM1 0
  21. #define COM2 1
  22. #define COM3 2
  23. #define COM4 3
  24. #define COM5 4
  25. #define COM6 5
  26. #define COM7 6
  27. #define COM8 7
  28. #define COM9 8
  29. #define LPT1 NUMCOMS
  30. #define LPT2 LPT1+1
  31. #define LPT3 LPT1+2
  32. #define AUX COM1
  33. #define PRN LPT1
  34. // DOS comm IRQ assignments
  35. #define IRQ3 3
  36. #define IRQ4 4
  37. #define IRQ5 5
  38. #define IRQ7 7
  39. // LPT assignments a la Win3.1
  40. #define LPTFIRST 0x80 // 0x80 == LPT1
  41. #define LPTLAST LPTFIRST + NUMLPTS - 1 // 0x82 == LPT3
  42. // other useful deinitions & macros
  43. #define COMMASK 0x00FF // strip garbage from idComDev
  44. #define LPTMASK 0x007F // get 0-based LPT #
  45. #define GETLPTID(id) ((id & LPTMASK) + LPT1) // 0x80 LPT to PortTab[] index
  46. #define TABIDTOLPT(id) (id + LPTFIRST - NUMCOMS) // PortTab[] index to LPT 0x80
  47. #define VALIDCOM(id) ((id < NUMCOMS) ? TRUE : FALSE)
  48. #define VALIDLPT(id) (((id >= LPTFIRST) && (id <= LPTLAST)) ? TRUE : FALSE)
  49. #define GETPWOWPTR(id) (VALIDCOM(id) ? PortTab[id].pWOWPort : (VALIDLPT(id) ? PortTab[GETLPTID(id)].pWOWPort : NULL))
  50. #define RM_BIOS_DATA 0x00400000 // bios data real mode seg:0
  51. // for Win3.1 compatibility in EscapeCommFunction() API thunk support
  52. #define RESETDEV 7
  53. #define GETMAXLPT 8
  54. #define GETMAXCOM 9
  55. #define GETBASEIRQ 10
  56. // notifications for EnableCommNotification() support
  57. #define CN_RECEIVE 0x0001
  58. #define CN_TRANSMIT 0x0002
  59. #define CN_EVENT 0x0004
  60. #define CN_RECEIVEHI 0x0100
  61. #define CN_TRANSMITHI 0x0200
  62. #define CN_NOTIFYHI 0x0400
  63. #define WOW_WM_COMMNOTIFY 0x0044
  64. // set all the events that can be masked on NT (a sub-set of Win3.1)
  65. #define EV_NTEVENTS (EV_BREAK | EV_CTS | EV_DSR | EV_ERR | EV_TXEMPTY | \
  66. EV_RLSD | EV_RXCHAR | EV_RXFLAG | EV_RING)
  67. // constants for how Win3.1 expects to see the MSR
  68. #define MSR_DELTAONLY 0x0000000F // strip off MSR state bits
  69. #define MSR_STATEONLY 0x000000F0 // strip off MSR delta bits
  70. #define MSR_DCTS 0x01 // bit for delta CTS
  71. #define MSR_DDSR 0x02 // bit for delta DSR
  72. #define MSR_TERI 0x04 // bit for TERI
  73. #define MSR_DDCD 0x08 // bit for delta DCD
  74. #define MSR_CTS 0x10 // bit for CTS
  75. #define MSR_DSR 0x20 // bit for DSR
  76. #define MSR_RI 0x40 // bit for RI
  77. #define MSR_DCD 0x80 // bit for DCD
  78. // Win3.1 constants for RLSD, CTS, and DSR timeout support
  79. #define CE_RLSDTO 0x0080
  80. #define CE_CTSTO 0x0020
  81. #define CE_DSRTO 0x0040
  82. // constants for the Event Word
  83. #define EV_CTSS 0x00000400 // bit for Win3.1 showing CTS state
  84. #define EV_DSRS 0x00000800 // bit for Win3.1 showing DSR state
  85. #define EV_RLSDS 0x00001000 // bit for Win3.1 showing RLSD state
  86. #define EV_RingTe 0x00002000 // bit for Win3.1 showing RingTe state
  87. #define ERR_XMIT 0x4000 // can't xmit a char Win3.1
  88. #define INFINITE_TIMEOUT 0xFFFF // infinite timeout Win3.1
  89. #define IGNORE_TIMEOUT 0x0000 // Win3.1 ignore RLSD, CTS, & DSR timeouts
  90. #define COMBUF 2 // max. # of bytes we'll queue for WriteComm()
  91. #define MAXCOMNAME 4 // max length of a comm device name
  92. #define MAXCOMNAMENULL MAXCOMNAME+1 // length of a comm device name + NULL
  93. // for 16-bit to 32-bit comm support
  94. typedef struct _WOWPORT {
  95. UINT idComDev; // idComDev returned to app as handle of port
  96. HANDLE h32; // NT file handle used instead of idComDev
  97. HANDLE hREvent; // structure for overlapped reads
  98. CRITICAL_SECTION csWrite; // critsect controls following 4 variables.
  99. PUCHAR pchWriteHead; // oldest byte not yet written to port.
  100. PUCHAR pchWriteTail; // first byte available in buffer.
  101. WORD cbWriteFree; // number of bytes available in write buffer.
  102. WORD cbWritePending; // number of bytes now in WriteFile()
  103. PUCHAR pchWriteBuf; // write buffer
  104. WORD cbWriteBuf; // size of the write buffer. One byte unused.
  105. HANDLE hWriteThread; // thread handle for COM writer.
  106. HANDLE hWriteEvent; // signalled by app thread when empty buffer
  107. // made non-empty to wake up writer thread.
  108. OVERLAPPED olWrite; // Overlapped structure used for writes.
  109. BOOL fWriteDone; // Indicates app thread completed first write.
  110. DWORD cbWritten; // Valid when fWriteDone == TRUE.
  111. DWORD dwThreadID; // app's thread id for crashed/hung app support
  112. DWORD dwErrCode; // most recent error for this idComDev
  113. COMSTAT cs; // struct for error handling
  114. BOOL fChEvt; // TRUE if app set fChEvt in DCB struct
  115. // 16-bit DCB for LPT support only
  116. PDCB16 pdcb16; // save DCB for LPT ports
  117. // for UngetCommChar() support
  118. BOOL fUnGet; // flag specifying an ungot char is pending
  119. UCHAR cUnGet; // ungot char in "buffer" only if fUnGet is set
  120. // for SetCommEventMask()/EnableCommNotification() support
  121. HANDLE hMiThread; // thread handle for Modem interrupt support
  122. BOOL fClose; // flag to close auxiliary threads
  123. // for SetCommEventMask() support only
  124. DWORD dwComDEB16; // DWORD obtained by call to GlobalDosAlloc()
  125. PCOMDEB16 lpComDEB16; // flat address to above
  126. // for XonLim & XoffLim checking in SetCommState
  127. DWORD cbInQ; // Actual size of in Queue set in WU32OpenComm
  128. // for RLSD, CTS, DSR timeout support
  129. WORD RLSDTimeout; // max time in msec to wait for RLSD (0->ignore)
  130. WORD CTSTimeout; // max time in msec to wait for CTS (0->ignore)
  131. WORD DSRTimeout; // max time in msec to wait for DSR (0->ignore)
  132. DWORD QLStackSeg; // Quicklink 1.3 hack See bug #398011
  133. // save the seg val of COMDEB16 in low word, &
  134. // the QuickLink stack selector in the high word
  135. } WOWPORT, *PWOWPORT;
  136. // Table of above structs, one entry needed for each comm port
  137. typedef struct _PORTTAB {
  138. CHAR szPort[MAXCOMNAMENULL]; // port name
  139. PWOWPORT pWOWPort; // pointer to Comm Mapping struct
  140. } PORTTAB, *PPORTTAB;
  141. //
  142. // Macro to calculate the size of chunk to write from the write
  143. // to the filesystem.
  144. //
  145. // This is either the entire pending part of the
  146. // buffer, or, if the buffer wraps, it is the portion
  147. // between the head and the end of the buffer.
  148. //
  149. // In order to keep COMSTAT.cbOutQue moving at a reasonable
  150. // pace, we restrict ourselves to writing at most 1024 bytes
  151. // at a time. This is because ProComm for Windows uses the
  152. // cbOutQue value in displaying its progress, so if we allow
  153. // larger writes it will only update every 5-10k (assuming
  154. // ProComm's default 16k write buffer),
  155. //
  156. #define CALC_COMM_WRITE_SIZE(pwp) \
  157. min(1024, \
  158. (pwp->pchWriteHead < pwp->pchWriteTail) \
  159. ? pwp->pchWriteTail - pwp->pchWriteHead \
  160. : (pwp->pchWriteBuf + pwp->cbWriteBuf) - \
  161. pwp->pchWriteHead \
  162. );
  163. // Win3.1 timesout Tx after approx. 65000 msec (65 sec)
  164. #define WRITE_TIMEOUT 65000
  165. // bitfields of the 16-bit COMSTAT.status
  166. #define W31CS_fCtsHold 0x01
  167. #define W31CS_fDsrHold 0x02
  168. #define W31CS_fRlsdHold 0x04
  169. #define W31CS_fXoffHold 0x08
  170. #define W31CS_fSentHold 0x10
  171. #define W31CS_fEof 0x20
  172. #define W31CS_fTxim 0x40
  173. // Win3.1 Baud Rate constants
  174. #define W31CBR_110 0xFF10
  175. #define W31CBR_300 0xFF11
  176. #define W31CBR_600 0xFF12
  177. #define W31CBR_1200 0xFF13
  178. #define W31CBR_2400 0xFF14
  179. #define W31CBR_4800 0xFF15
  180. #define W31CBR_9600 0xFF16
  181. #define W31CBR_14400 0xFF17
  182. #define W31CBR_19200 0xFF18
  183. #define W31CBR_reserved1 0xFF19
  184. #define W31CBR_reserved2 0xFF1A
  185. #define W31CBR_38400 0xFF1B
  186. #define W31CBR_reserved3 0xFF1C
  187. #define W31CBR_reserved4 0xFF1D
  188. #define W31CBR_reserved5 0xFF1E
  189. #define W31CBR_56000 0xFF1F
  190. // these are defined in Win3.1 windows.h but aren't supported in comm.drv
  191. #define W31CBR_128000 0xFF23
  192. #define W31CBR_256000 0xFF27
  193. // special way to say 115200
  194. #define W31CBR_115200 0xFEFF
  195. // constants for conversions from Win3.1 baud specifications to 32-bit baud
  196. #define W31_DLATCH_110 1047
  197. #define W31_DLATCH_300 384
  198. #define W31_DLATCH_600 192
  199. #define W31_DLATCH_1200 96
  200. #define W31_DLATCH_2400 48
  201. #define W31_DLATCH_4800 24
  202. #define W31_DLATCH_9600 12
  203. #define W31_DLATCH_14400 8
  204. #define W31_DLATCH_19200 6
  205. #define W31_DLATCH_38400 3
  206. #define W31_DLATCH_56000 2
  207. #define W31_DLATCH_115200 1
  208. // Win3.1 flags for DCB structure
  209. #define W31DCB_fBinary 0x0001
  210. #define W31DCB_fRtsDisable 0x0002
  211. #define W31DCB_fParity 0x0004
  212. #define W31DCB_fOutxCtsFlow 0x0008
  213. #define W31DCB_fOutxDsrFlow 0x0010
  214. #define W31DCB_fDummy (0x0020 | 0x0040)
  215. #define W31DCB_fDtrDisable 0x0080
  216. #define W31DCB_fOutX 0x0100
  217. #define W31DCB_fInX 0x0200
  218. #define W31DCB_fPeChar 0x0400
  219. #define W31DCB_fNull 0x0800
  220. #define W31DCB_fChEvt 0x1000
  221. #define W31DCB_fDtrFlow 0x2000
  222. #define W31DCB_fRtsFlow 0x4000
  223. #define W31DCB_fDummy2 0x8000
  224. //+++ DEBUG SUPPORT
  225. #ifdef DEBUG
  226. #define COMMDEBUG(lpszformat) LOGDEBUG(1, lpszformat)
  227. // for watching the modem events
  228. #define DEBUGWATCHMODEMEVENTS(dwE, dwM, dwS, pcE16, pcM16) { \
  229. if(dwS) { \
  230. if((dwE != (DWORD)pcE16) || (dwM != (DWORD)pcM16)) { \
  231. dwE = (DWORD)pcE16; \
  232. dwM = (DWORD)pcM16; \
  233. COMMDEBUG(("\nEvt:0x%4X MSR:0x%2X\n", dwE, dwM)); \
  234. } \
  235. else { \
  236. COMMDEBUG((".")); \
  237. } \
  238. } \
  239. }
  240. // prototype for real-time debug output
  241. void CommIODebug(ULONG fhCommIO, HANDLE hCommIO, LPSZ lpsz, ULONG cb, LPSZ lpszFile);
  242. #else // endif DEBUG
  243. #define COMMDEBUG(lpszFormat)
  244. #define DEBUGWATCHMODEMEVENTS(dwE, dwM, dwS, pcE16, pcM16)
  245. #define CommIODebug(fhCommIO, hCommIO, lpsz, cb, lpszFile)
  246. #endif // endif !DEBUG
  247. //--- DEBUG SUPPORT
  248. // API support function prototypes
  249. ULONG FASTCALL WU32BuildCommDCB(PVDMFRAME pFrame);
  250. ULONG FASTCALL WU32ClearCommBreak(PVDMFRAME pFrame);
  251. ULONG FASTCALL WU32CloseComm(PVDMFRAME pFrame);
  252. ULONG FASTCALL WU32EnableCommNotification(PVDMFRAME pFrame);
  253. ULONG FASTCALL WU32EscapeCommFunction(PVDMFRAME pFrame);
  254. ULONG FASTCALL WU32FlushComm(PVDMFRAME pFrame);
  255. ULONG FASTCALL WU32GetCommError(PVDMFRAME pFrame);
  256. ULONG FASTCALL WU32GetCommEventMask(PVDMFRAME pFrame);
  257. ULONG FASTCALL WU32GetCommState(PVDMFRAME pFrame);
  258. ULONG FASTCALL WU32OpenComm(PVDMFRAME pFrame);
  259. ULONG FASTCALL WU32ReadComm(PVDMFRAME pFrame);
  260. ULONG FASTCALL WU32SetCommBreak(PVDMFRAME pFrame);
  261. ULONG FASTCALL WU32SetCommEventMask(PVDMFRAME pFrame);
  262. ULONG FASTCALL WU32SetCommState(PVDMFRAME pFrame);
  263. ULONG FASTCALL WU32TransmitCommChar(PVDMFRAME pFrame);
  264. ULONG FASTCALL WU32UngetCommChar(PVDMFRAME pFrame);
  265. ULONG FASTCALL WU32WriteComm(PVDMFRAME pFrame);
  266. // prototypes for functions exported to the VDM
  267. BYTE GetCommShadowMSR(WORD idComDev);
  268. HANDLE GetCommHandle(WORD idComDev);
  269. // prototype for crashed/hung app cleanup support
  270. VOID FreeCommSupportResources(DWORD dwThreadID);