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.

815 lines
31 KiB

  1. /**************************************************************************************************************************
  2. * CONVERT.C SigmaTel STIR4200 format conversion (NDIS->IR, IR->NDIS) module
  3. **************************************************************************************************************************
  4. * (C) Unpublished Copyright of Sigmatel, Inc. All Rights Reserved.
  5. *
  6. *
  7. * Created: 04/06/2000
  8. * Version 0.9
  9. * Edited: 05/12/2000
  10. * Version 0.94
  11. *
  12. *
  13. **************************************************************************************************************************/
  14. #define DOBREAKS // enable debug breaks
  15. #include <ndis.h>
  16. #include <ntddndis.h> // defines OID's
  17. #include <usbdi.h>
  18. #include <usbdlib.h>
  19. #include "debug.h"
  20. #include "ircommon.h"
  21. #include "irndis.h"
  22. #include "stir4200.h"
  23. //
  24. // Tables for CRC calculations
  25. //
  26. static const USHORT fcsTable16[256] =
  27. {
  28. 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  29. 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  30. 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  31. 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  32. 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  33. 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  34. 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  35. 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  36. 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  37. 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  38. 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  39. 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  40. 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  41. 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  42. 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  43. 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  44. 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  45. 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  46. 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  47. 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  48. 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  49. 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  50. 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  51. 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  52. 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  53. 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  54. 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  55. 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  56. 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  57. 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  58. 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  59. 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  60. };
  61. static const ULONG fcsTable32[256] =
  62. {
  63. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
  64. 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
  65. 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
  66. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
  67. 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
  68. 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
  69. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
  70. 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
  71. 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
  72. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
  73. 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
  74. 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
  75. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
  76. 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
  77. 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  78. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
  79. 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
  80. 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
  81. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
  82. 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
  83. 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
  84. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
  85. 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
  86. 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
  87. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
  88. 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
  89. 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
  90. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
  91. 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
  92. 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  93. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
  94. 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
  95. };
  96. /*****************************************************************************
  97. *
  98. * Function: NdisToFirPacket
  99. *
  100. * Synopsis: convert an NDIS packet to a Fir IR packet
  101. *
  102. * Write the IR packet into the provided buffer and report
  103. * its actual size.
  104. *
  105. * Arguments: pIrDev - pointer to device instance
  106. * pPacket - NDIS packet to convert
  107. * pIrPacketBuf - output buffer
  108. * IrPacketBufLen - output buffer size
  109. * pContigPacketBuf - temporary staging buffer
  110. * pIrPacketLen - lenght of the converted data
  111. *
  112. * MS Security bug #533243
  113. * Note: size of pContigPacketBuf staging buffer is:
  114. * MAX_TOTAL_SIZE_WITH_ALL_HEADERS + FAST_IR_FCS_SIZE
  115. *
  116. * Returns: TRUE - on success
  117. * FALSE - on failure
  118. *
  119. *
  120. *****************************************************************************/
  121. BOOLEAN
  122. NdisToFirPacket(
  123. IN PIR_DEVICE pIrDev,
  124. IN PNDIS_PACKET pPacket,
  125. OUT PUCHAR pIrPacketBuf,
  126. ULONG IrPacketBufLen,
  127. IN PUCHAR pContigPacketBuf,
  128. OUT PULONG pIrPacketLen
  129. )
  130. {
  131. PNDIS_BUFFER pNdisBuf;
  132. ULONG ndisPacketBytes = 0;
  133. ULONG I_fieldBytes, ndisPacketLen;
  134. FAST_IR_FCS_TYPE fcs;
  135. FAST_IR_FCS_TYPE UNALIGNED *pfcs;
  136. ULONG i, TotalBytes, EscSize;
  137. PSTIR4200_FRAME_HEADER pFrameHeader = (PSTIR4200_FRAME_HEADER)pIrPacketBuf;
  138. PUCHAR pIrPacketBufFrame = pIrPacketBuf + sizeof(STIR4200_FRAME_HEADER);
  139. /***********************************************/
  140. /* Get the packet's entire length and its */
  141. /* first NDIS buffer */
  142. /***********************************************/
  143. NdisQueryPacket( pPacket, NULL, NULL, &pNdisBuf, (UINT*)&ndisPacketLen );
  144. /***********************************************/
  145. /* Make sure that the packet is big enough */
  146. /* to be legal. It consists of an A, C, and */
  147. /* variable-length I field. */
  148. /***********************************************/
  149. if( ndisPacketLen < IRDA_A_C_TOTAL_SIZE )
  150. {
  151. DEBUGMSG(DBG_ERR, (" NdisToFirPacket(): Packet is too small\n"));
  152. return FALSE;
  153. }
  154. else
  155. {
  156. I_fieldBytes = ndisPacketLen - IRDA_A_C_TOTAL_SIZE;
  157. }
  158. /***********************************************/
  159. /* Make sure that we won't overwrite our */
  160. /* contiguous buffer */
  161. /***********************************************/
  162. if( (ndisPacketLen > MAX_TOTAL_SIZE_WITH_ALL_HEADERS) ||
  163. (MAX_POSSIBLE_IR_PACKET_SIZE_FOR_DATA(I_fieldBytes) > IrPacketBufLen) )
  164. {
  165. /***********************************************/
  166. /* The packet is too large. Tell the caller */
  167. /* to retry with a packet size large enough */
  168. /* to get past this stage next time. */
  169. /***********************************************/
  170. DEBUGMSG(DBG_ERR, (" NdisToFirPacket(): Packet is too big\n"));
  171. return FALSE;
  172. }
  173. /***********************************************/
  174. /* Read the NDIS packet into a contiguous */
  175. /* buffer. We have to do this in two steps */
  176. /* so that we can compute the FCS BEFORE */
  177. /* applying escape-byte transparency. */
  178. /***********************************************/
  179. while( pNdisBuf )
  180. {
  181. UCHAR *bufData;
  182. ULONG bufLen;
  183. #if defined(LEGACY_NDIS5)
  184. NdisQueryBuffer( pNdisBuf, (PVOID*)&bufData, (UINT*)&bufLen );
  185. #else
  186. NdisQueryBufferSafe( pNdisBuf, (PVOID*)&bufData, (UINT*)&bufLen, NormalPagePriority );
  187. if (bufData == NULL)
  188. {
  189. DEBUGMSG(DBG_ERR, (" NdisToFirPacket(): Could not query buffer\n"));
  190. return FALSE;
  191. }
  192. #endif
  193. if( (ndisPacketBytes + bufLen) > ndisPacketLen )
  194. {
  195. //
  196. // Packet was corrupt -- it misreported its size.
  197. //
  198. DEBUGMSG(DBG_ERR, (" NdisToFirPacket(): Packet is corrupt\n"));
  199. return FALSE;
  200. }
  201. NdisMoveMemory( (PVOID)(pContigPacketBuf + ndisPacketBytes), (PVOID)bufData, bufLen );
  202. ndisPacketBytes += bufLen;
  203. NdisGetNextBuffer( pNdisBuf, &pNdisBuf );
  204. }
  205. /***********************************************/
  206. /* Do sanity check on length of packet... */
  207. /***********************************************/
  208. if( ndisPacketBytes != ndisPacketLen )
  209. {
  210. //
  211. // Packet was corrupt -- it misreported its size.
  212. //
  213. DEBUGMSG(DBG_ERR, (" NdisToFirPacket(): Packet is corrupt\n"));
  214. return FALSE;
  215. }
  216. /***********************************************/
  217. /* Compute the FCS on the packet BEFORE */
  218. /* applying transparency fixups. The FCS */
  219. /* also must be sent using ESC-char */
  220. /* transparency. */
  221. /***********************************************/
  222. fcs = ComputeFCS32( pContigPacketBuf, ndisPacketBytes );
  223. /***********************************************/
  224. /* Add FCS to packet... */
  225. /***********************************************/
  226. pfcs = (FAST_IR_FCS_TYPE *)&pContigPacketBuf[ndisPacketBytes];
  227. *pfcs = fcs;
  228. /***********************************************/
  229. /* Build the STIr4200 FIR frame. */
  230. /***********************************************/
  231. /***********************************************/
  232. /* Add preamble... */
  233. /***********************************************/
  234. memset( pIrPacketBufFrame, STIR4200_FIR_PREAMBLE, STIR4200_FIR_PREAMBLE_SIZ );
  235. /***********************************************/
  236. /* Add BOF's... */
  237. /***********************************************/
  238. memset( &pIrPacketBufFrame[STIR4200_FIR_PREAMBLE_SIZ], STIR4200_FIR_BOF, STIR4200_FIR_BOF_SIZ );
  239. /***********************************************/
  240. /* Escape A, C, I & CRC fields of packet... */
  241. /***********************************************/
  242. EscSize = ndisPacketBytes + FAST_IR_FCS_SIZE;
  243. for( i = 0, TotalBytes = STIR4200_FIR_PREAMBLE_SIZ + STIR4200_FIR_BOF_SIZ; i < EscSize; i++ )
  244. {
  245. UCHAR c;
  246. switch( c = pContigPacketBuf[i] )
  247. {
  248. case STIR4200_FIR_ESC_CHAR:
  249. pIrPacketBufFrame[TotalBytes++] = STIR4200_FIR_ESC_CHAR;
  250. pIrPacketBufFrame[TotalBytes++] = STIR4200_FIR_ESC_DATA_7D;
  251. break;
  252. case STIR4200_FIR_BOF: // BOF = EOF too
  253. pIrPacketBufFrame[TotalBytes++] = STIR4200_FIR_ESC_CHAR;
  254. pIrPacketBufFrame[TotalBytes++] = STIR4200_FIR_ESC_DATA_7E;
  255. break;
  256. case STIR4200_FIR_PREAMBLE:
  257. pIrPacketBufFrame[TotalBytes++] = STIR4200_FIR_ESC_CHAR;
  258. pIrPacketBufFrame[TotalBytes++] = STIR4200_FIR_ESC_DATA_7F;
  259. break;
  260. default:
  261. pIrPacketBufFrame[TotalBytes++] = c;
  262. }
  263. }
  264. /***********************************************/
  265. /* Add EOF's... */
  266. /***********************************************/
  267. memset( &pIrPacketBufFrame[TotalBytes], STIR4200_FIR_EOF, STIR4200_FIR_EOF_SIZ );
  268. /***********************************************/
  269. /* Add in STIr4200 header... */
  270. /***********************************************/
  271. TotalBytes += STIR4200_FIR_EOF_SIZ;
  272. pFrameHeader->id1 = STIR4200_HEADERID_BYTE1;
  273. pFrameHeader->id2 = STIR4200_HEADERID_BYTE2;
  274. pFrameHeader->sizlsb = LOBYTE(TotalBytes);
  275. pFrameHeader->sizmsb = HIBYTE(TotalBytes);
  276. /***********************************************/
  277. /* Calc size packet w/escaped data... */
  278. /***********************************************/
  279. *pIrPacketLen = TotalBytes + sizeof(STIR4200_FRAME_HEADER);
  280. return TRUE;
  281. }
  282. /*****************************************************************************
  283. *
  284. * Function: NdisToMirPacket
  285. *
  286. * Synopsis: convert an NDIS packet to a Mir IR packet
  287. *
  288. * Write the IR packet into the provided buffer and report
  289. * its actual size.
  290. *
  291. * Arguments: pIrDev - pointer to device instance
  292. * pPacket - NDIS packet to convert
  293. * pIrPacketBuf - output buffer
  294. * IrPacketBufLen - output buffer size
  295. * pContigPacketBuf - temporary staging buffer
  296. * pIrPacketLen - lenght of the converted data
  297. *
  298. * MS Security bug #533243
  299. * Note: size of pContigPacketBuf staging buffer is:
  300. * MAX_TOTAL_SIZE_WITH_ALL_HEADERS + FAST_IR_FCS_SIZE
  301. *
  302. * Returns: TRUE - on success
  303. * FALSE - on failure
  304. *
  305. *
  306. *****************************************************************************/
  307. BOOLEAN
  308. NdisToMirPacket(
  309. IN PIR_DEVICE pIrDev,
  310. IN PNDIS_PACKET pPacket,
  311. OUT PUCHAR pIrPacketBuf,
  312. ULONG IrPacketBufLen,
  313. IN PUCHAR pContigPacketBuf,
  314. OUT PULONG pIrPacketLen
  315. )
  316. {
  317. PNDIS_BUFFER pNdisBuf;
  318. ULONG ndisPacketBytes = 0;
  319. ULONG I_fieldBytes, ndisPacketLen;
  320. MEDIUM_IR_FCS_TYPE fcs;
  321. MEDIUM_IR_FCS_TYPE UNALIGNED *pfcs;
  322. ULONG i, TotalBytes, EscSize;
  323. PSTIR4200_FRAME_HEADER pFrameHeader = (PSTIR4200_FRAME_HEADER)pIrPacketBuf;
  324. PUCHAR pIrPacketBufFrame = pIrPacketBuf + sizeof(STIR4200_FRAME_HEADER);
  325. /***********************************************/
  326. /* Get the packet's entire length and its */
  327. /* first NDIS buffer */
  328. /***********************************************/
  329. NdisQueryPacket( pPacket, NULL, NULL, &pNdisBuf, (UINT*)&ndisPacketLen );
  330. /***********************************************/
  331. /* Make sure that the packet is big enough */
  332. /* to be legal. It consists of an A, C, and */
  333. /* variable-length I field. */
  334. /***********************************************/
  335. if( ndisPacketLen < IRDA_A_C_TOTAL_SIZE )
  336. {
  337. DEBUGMSG(DBG_ERR, (" NdisToMirPacket(): Packet too small\n"));
  338. return FALSE;
  339. }
  340. else
  341. {
  342. I_fieldBytes = ndisPacketLen - IRDA_A_C_TOTAL_SIZE;
  343. }
  344. /***********************************************/
  345. /* Make sure that we won't overwrite our */
  346. /* contiguous buffer */
  347. /***********************************************/
  348. if( (ndisPacketLen > MAX_TOTAL_SIZE_WITH_ALL_HEADERS) ||
  349. (MAX_POSSIBLE_IR_PACKET_SIZE_FOR_DATA(I_fieldBytes) > IrPacketBufLen) )
  350. {
  351. /***********************************************/
  352. /* The packet is too large. Tell the caller */
  353. /* to retry with a packet size large enough */
  354. /* to get past this stage next time. */
  355. /***********************************************/
  356. DEBUGMSG(DBG_ERR, (" NdisToMirPacket(): Packet too big\n"));
  357. return FALSE;
  358. }
  359. /***********************************************/
  360. /* Read the NDIS packet into a contiguous */
  361. /* buffer. We have to do this in two steps */
  362. /* so that we can compute the FCS BEFORE */
  363. /* applying escape-byte transparency. */
  364. /***********************************************/
  365. while( pNdisBuf )
  366. {
  367. UCHAR *bufData;
  368. ULONG bufLen;
  369. #if defined(LEGACY_NDIS5)
  370. NdisQueryBuffer( pNdisBuf, (PVOID*)&bufData, (UINT*)&bufLen );
  371. #else
  372. NdisQueryBufferSafe( pNdisBuf, (PVOID*)&bufData, (UINT*)&bufLen, NormalPagePriority );
  373. if (bufData == NULL)
  374. {
  375. DEBUGMSG(DBG_ERR, (" NdisToMirPacket(): Could not query buffer\n"));
  376. return FALSE;
  377. }
  378. #endif
  379. if( ndisPacketBytes + bufLen > ndisPacketLen )
  380. {
  381. //
  382. // Packet was corrupt -- it misreported its size.
  383. //
  384. DEBUGMSG(DBG_ERR, (" NdisToMirPacket(): Packet is corrupt\n"));
  385. *pIrPacketLen = 0;
  386. return FALSE;
  387. }
  388. NdisMoveMemory( (PVOID)(pContigPacketBuf + ndisPacketBytes), (PVOID)bufData, bufLen );
  389. ndisPacketBytes += bufLen;
  390. NdisGetNextBuffer( pNdisBuf, &pNdisBuf );
  391. }
  392. /***********************************************/
  393. /* Do sanity check on length of packet... */
  394. /***********************************************/
  395. if( ndisPacketBytes != ndisPacketLen )
  396. {
  397. //
  398. // Packet was corrupt -- it misreported its size.
  399. //
  400. DEBUGMSG(DBG_ERR, (" NdisToMirPacket(): Packet is corrupt\n"));
  401. return FALSE;
  402. }
  403. /***********************************************/
  404. /* Compute the FCS on the packet BEFORE */
  405. /* applying transparency fixups. The FCS */
  406. /* also must be sent using ESC-char */
  407. /* transparency. */
  408. /***********************************************/
  409. fcs = ComputeFCS16( pContigPacketBuf, ndisPacketBytes );
  410. /***********************************************/
  411. /* Add FCS to packet... */
  412. /***********************************************/
  413. pfcs = (MEDIUM_IR_FCS_TYPE *)&pContigPacketBuf[ndisPacketBytes];
  414. *pfcs = fcs;
  415. /***********************************************/
  416. /* Build the STIr4200 MIR frame. */
  417. /***********************************************/
  418. /***********************************************/
  419. /* Add BOF's... */
  420. /***********************************************/
  421. memset( pIrPacketBufFrame, STIR4200_MIR_BOF, STIR4200_MIR_BOF_SIZ );
  422. /***********************************************/
  423. /* Escape A, C, I & CRC fields of packet... */
  424. /***********************************************/
  425. EscSize = ndisPacketBytes + MEDIUM_IR_FCS_SIZE;
  426. for( i = 0, TotalBytes = STIR4200_MIR_BOF_SIZ; i < EscSize; i++ )
  427. {
  428. UCHAR c;
  429. switch( c = pContigPacketBuf[i] )
  430. {
  431. case STIR4200_MIR_ESC_CHAR:
  432. pIrPacketBufFrame[TotalBytes++] = STIR4200_MIR_ESC_CHAR;
  433. pIrPacketBufFrame[TotalBytes++] = STIR4200_MIR_ESC_DATA_7D;
  434. break;
  435. case STIR4200_MIR_BOF: // BOF = EOF too
  436. pIrPacketBufFrame[TotalBytes++] = STIR4200_MIR_ESC_CHAR;
  437. pIrPacketBufFrame[TotalBytes++] = STIR4200_MIR_ESC_DATA_7E;
  438. break;
  439. default:
  440. pIrPacketBufFrame[TotalBytes++] = c;
  441. }
  442. }
  443. /***********************************************/
  444. /* Add EOF's... */
  445. /***********************************************/
  446. memset( &pIrPacketBufFrame[TotalBytes], STIR4200_MIR_EOF, STIR4200_MIR_EOF_SIZ );
  447. /***********************************************/
  448. /* Add in STIr4200 header... */
  449. /***********************************************/
  450. TotalBytes += STIR4200_MIR_EOF_SIZ;
  451. pFrameHeader->id1 = STIR4200_HEADERID_BYTE1;
  452. pFrameHeader->id2 = STIR4200_HEADERID_BYTE2;
  453. pFrameHeader->sizlsb = LOBYTE(TotalBytes);
  454. pFrameHeader->sizmsb = HIBYTE(TotalBytes);
  455. /***********************************************/
  456. /* Calc size packet w/escaped data... */
  457. /***********************************************/
  458. *pIrPacketLen = TotalBytes + sizeof(STIR4200_FRAME_HEADER);
  459. return TRUE;
  460. }
  461. /*****************************************************************************
  462. *
  463. * Function: NdisToSirPacket
  464. *
  465. * Synopsis: convert an NDIS packet to a Sir IR packet
  466. *
  467. * Write the IR packet into the provided buffer and report
  468. * its actual size.
  469. *
  470. * Arguments: pIrDev - pointer to device instance
  471. * pPacket - NDIS packet to convert
  472. * pIrPacketBuf - output buffer
  473. * IrPacketBufLen - output buffer size
  474. * pContigPacketBuf - temporary staging buffer
  475. * pIrPacketLen - lenght of the converted data
  476. *
  477. * MS Security bug #533243
  478. * Note: size of pContigPacketBuf staging buffer is:
  479. * MAX_TOTAL_SIZE_WITH_ALL_HEADERS + FAST_IR_FCS_SIZE
  480. *
  481. * Returns: TRUE - on success
  482. * FALSE - on failure
  483. *
  484. *
  485. *****************************************************************************/
  486. BOOLEAN
  487. NdisToSirPacket(
  488. IN PIR_DEVICE pIrDev,
  489. IN PNDIS_PACKET pPacket,
  490. OUT PUCHAR pIrPacketBuf,
  491. ULONG IrPacketBufLen,
  492. IN PUCHAR pContigPacketBuf,
  493. OUT PULONG pIrPacketLen
  494. )
  495. {
  496. PNDIS_BUFFER pNdisBuf;
  497. ULONG i, ndisPacketBytes = 0;
  498. ULONG I_fieldBytes, totalBytes = 0;
  499. ULONG ndisPacketLen, numExtraBOFs;
  500. SLOW_IR_FCS_TYPE fcs, tmpfcs;
  501. UCHAR fcsBuf[SLOW_IR_FCS_SIZE * 2];
  502. ULONG fcsLen = 0;
  503. PNDIS_IRDA_PACKET_INFO pPacketInfo = GetPacketInfo(pPacket);
  504. UCHAR nextChar;
  505. PSTIR4200_FRAME_HEADER pFrameHeader = (PSTIR4200_FRAME_HEADER)pIrPacketBuf;
  506. PUCHAR pIrPacketBufFrame = pIrPacketBuf + sizeof(STIR4200_FRAME_HEADER);
  507. /***********************************************/
  508. /* Get the packet's entire length and its */
  509. /* first NDIS buffer */
  510. /***********************************************/
  511. NdisQueryPacket( pPacket, NULL, NULL, &pNdisBuf, (UINT*)&ndisPacketLen );
  512. /***********************************************/
  513. /* Make sure that the packet is big enough */
  514. /* to be legal. It consists of an A, C, and */
  515. /* variable-length I field. */
  516. /***********************************************/
  517. if( ndisPacketLen < IRDA_A_C_TOTAL_SIZE )
  518. {
  519. DEBUGMSG(DBG_ERR, (" NdisToSirPacket(): Packet is too small\n"));
  520. return FALSE;
  521. }
  522. else
  523. {
  524. I_fieldBytes = ndisPacketLen - IRDA_A_C_TOTAL_SIZE;
  525. }
  526. /***********************************************/
  527. /* Make sure that we won't overwrite our */
  528. /* contiguous buffer. Make sure that the */
  529. /* passed-in buffer can accomodate this */
  530. /* packet's data no matter how much it */
  531. /* grows through adding ESC-sequences, etc. */
  532. /***********************************************/
  533. if( (ndisPacketLen > MAX_TOTAL_SIZE_WITH_ALL_HEADERS) ||
  534. (MAX_POSSIBLE_IR_PACKET_SIZE_FOR_DATA(I_fieldBytes) > IrPacketBufLen) )
  535. {
  536. //
  537. // Packet is too big
  538. //
  539. DEBUGMSG(DBG_ERR, (" NdisToSirPacket(): Packet is too big\n"));
  540. return FALSE;
  541. }
  542. /***********************************************/
  543. /* First, read the NDIS packet into a */
  544. /* contiguous buffer. We have to do this in */
  545. /* two steps so that we can compute the FCS */
  546. /* BEFORE applying escape-byte */
  547. /* transparency. */
  548. /***********************************************/
  549. while( pNdisBuf )
  550. {
  551. UCHAR *bufData;
  552. ULONG bufLen;
  553. #if defined(LEGACY_NDIS5)
  554. NdisQueryBuffer( pNdisBuf, (PVOID*)&bufData, (UINT*)&bufLen );
  555. #else
  556. NdisQueryBufferSafe( pNdisBuf, (PVOID*)&bufData, (UINT*)&bufLen, NormalPagePriority );
  557. if (bufData == NULL)
  558. {
  559. DEBUGMSG(DBG_ERR, (" NdisToSirPacket(): Could not query buffer\n"));
  560. return FALSE;
  561. }
  562. #endif
  563. if( (ndisPacketBytes + bufLen) > ndisPacketLen )
  564. {
  565. //
  566. // Packet was corrupt -- it misreported its size.
  567. //
  568. DEBUGMSG(DBG_ERR, (" NdisToSirPacket(): Packet is corrupt\n"));
  569. return FALSE;
  570. }
  571. NdisMoveMemory( (PVOID)(pContigPacketBuf + ndisPacketBytes), (PVOID)bufData, bufLen );
  572. ndisPacketBytes += bufLen;
  573. NdisGetNextBuffer( pNdisBuf, &pNdisBuf );
  574. }
  575. /***********************************************/
  576. /* Do sanity check on length of packet... */
  577. /***********************************************/
  578. if( ndisPacketBytes != ndisPacketLen )
  579. {
  580. //
  581. // Packet was corrupt -- it misreported its size.
  582. //
  583. DEBUGMSG(DBG_ERR, (" NdisToSirPacket(): Packet is corrupt\n"));
  584. return FALSE;
  585. }
  586. /***********************************************/
  587. /* Compute the FCS on the packet BEFORE */
  588. /* applying transparency fixups. The FCS */
  589. /* also must be sent using ESC-char */
  590. /* transparency, so figure out how large */
  591. /* the fcs will really be. */
  592. /***********************************************/
  593. fcs = ComputeFCS16( pContigPacketBuf, ndisPacketBytes );
  594. for( i = 0, tmpfcs = fcs, fcsLen = 0; i < SLOW_IR_FCS_SIZE; tmpfcs >>= 8, i++ )
  595. {
  596. UCHAR fcsbyte = tmpfcs & 0x00ff;
  597. switch( fcsbyte )
  598. {
  599. case SLOW_IR_BOF:
  600. case SLOW_IR_EOF:
  601. case SLOW_IR_ESC:
  602. fcsBuf[fcsLen++] = SLOW_IR_ESC;
  603. fcsBuf[fcsLen++] = fcsbyte ^ SLOW_IR_ESC_COMP;
  604. break;
  605. default:
  606. fcsBuf[fcsLen++] = fcsbyte;
  607. break;
  608. }
  609. }
  610. /***********************************************/
  611. /* Now begin building the IR frame. */
  612. /* */
  613. /* This is the final format: */
  614. /* */
  615. /* BOF (1) */
  616. /* extra BOFs ... */
  617. /* NdisMediumIrda packet (from NDIS): */
  618. /* Address (1) */
  619. /* Control (1) */
  620. /* FCS (2) */
  621. /* EOF (1) */
  622. /* */
  623. /* Prepend BOFs (extra BOFs + 1 actual BOF) */
  624. /***********************************************/
  625. numExtraBOFs = pPacketInfo->ExtraBOFs;
  626. if( numExtraBOFs > MAX_NUM_EXTRA_BOFS )
  627. {
  628. numExtraBOFs = MAX_NUM_EXTRA_BOFS;
  629. }
  630. for( i = totalBytes = 0; i < numExtraBOFs; i++ )
  631. {
  632. *(SLOW_IR_BOF_TYPE*)(pIrPacketBufFrame + totalBytes) = SLOW_IR_EXTRA_BOF;
  633. totalBytes += SLOW_IR_EXTRA_BOF_SIZE;
  634. }
  635. *(SLOW_IR_BOF_TYPE*)(pIrPacketBufFrame + totalBytes) = SLOW_IR_BOF;
  636. totalBytes += SLOW_IR_BOF_SIZE;
  637. /***********************************************/
  638. /* Copy the NDIS packet from our contiguous */
  639. /* buffer, applying escape-char */
  640. /* transparency. */
  641. /***********************************************/
  642. for( i = 0; i < ndisPacketBytes; i++ )
  643. {
  644. nextChar = pContigPacketBuf[i];
  645. switch( nextChar )
  646. {
  647. case SLOW_IR_BOF:
  648. case SLOW_IR_EOF:
  649. case SLOW_IR_ESC:
  650. pIrPacketBufFrame[totalBytes++] = SLOW_IR_ESC;
  651. pIrPacketBufFrame[totalBytes++] = nextChar ^ SLOW_IR_ESC_COMP;
  652. break;
  653. default:
  654. pIrPacketBufFrame[totalBytes++] = nextChar;
  655. break;
  656. }
  657. }
  658. /***********************************************/
  659. /* Add FCS, EOF. */
  660. /***********************************************/
  661. NdisMoveMemory( (PVOID)(pIrPacketBufFrame + totalBytes), (PVOID)fcsBuf, fcsLen );
  662. totalBytes += fcsLen;
  663. *(SLOW_IR_EOF_TYPE*)(pIrPacketBufFrame + totalBytes) = (UCHAR)SLOW_IR_EOF;
  664. totalBytes += SLOW_IR_EOF_SIZE;
  665. /***********************************************/
  666. /* Add in STIr4200 header... */
  667. /***********************************************/
  668. pFrameHeader->id1 = STIR4200_HEADERID_BYTE1;
  669. pFrameHeader->id2 = STIR4200_HEADERID_BYTE2;
  670. pFrameHeader->sizlsb = LOBYTE(totalBytes);
  671. pFrameHeader->sizmsb = HIBYTE(totalBytes);
  672. *pIrPacketLen = totalBytes + sizeof(STIR4200_FRAME_HEADER);
  673. return TRUE;
  674. }
  675. /*****************************************************************************
  676. *
  677. * Function: ComputeFCS16
  678. *
  679. * Synopsis: Calculates the 16 bit CRC.
  680. *
  681. * Arguments: pData - pointer to data buffer
  682. * DataLen - length of data buffer
  683. *
  684. * Returns: Calculated CRC
  685. *
  686. * Notes:
  687. *
  688. *****************************************************************************/
  689. USHORT
  690. ComputeFCS16(
  691. IN PUCHAR pData,
  692. UINT DataLen
  693. )
  694. {
  695. USHORT fcs = 0xffff;
  696. UINT i;
  697. for( i = 0; i < DataLen; i++ )
  698. {
  699. fcs = (fcs >> 8) ^ fcsTable16[(fcs ^ *pData++)& 0xff];
  700. }
  701. fcs = ~fcs;
  702. return fcs;
  703. }
  704. /*****************************************************************************
  705. *
  706. * Function: ComputeFCS32
  707. *
  708. * Synopsis: Calculates the 32 bit CRC.
  709. *
  710. * Arguments: pData - pointer to data buffer
  711. * DataLen - length of data buffer
  712. *
  713. * Returns: Calculated CRC
  714. *
  715. * Notes:
  716. *
  717. *****************************************************************************/
  718. #define USE_FASTER_CRC32 1
  719. ULONG
  720. ComputeFCS32(
  721. IN PUCHAR pData,
  722. ULONG DataLen
  723. )
  724. {
  725. #ifdef USE_FASTER_CRC32
  726. #define UPDC32(octet, crc) (crc_32_tab[((crc) ^ ((UCHAR)octet)) & 0xff] ^ ((crc) >> 8))
  727. ULONG crc;
  728. crc = 0xFFFFFFFF;
  729. for( ; DataLen; --DataLen, ++pData )
  730. {
  731. crc = (fcsTable32[((crc) ^ (*pData)) & 0xff] ^ ((crc) >> 8));
  732. }
  733. return ~crc;
  734. #else
  735. ULONG crc, temp1, temp2;
  736. crc = 0xFFFFFFFF;
  737. while( DataLen-- != 0 )
  738. {
  739. temp1 = (crc >> 8) & 0x00FFFFFFL;
  740. temp2 = fcsTable32[((int)crc ^ *pData++) & 0xff];
  741. crc = temp1 ^ temp2;
  742. }
  743. return crc;
  744. #endif
  745. }