Source code of Windows XP (NT5)
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.

803 lines
30 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. * Returns: TRUE - on success
  113. * FALSE - on failure
  114. *
  115. *
  116. *****************************************************************************/
  117. BOOLEAN
  118. NdisToFirPacket(
  119. IN PIR_DEVICE pIrDev,
  120. IN PNDIS_PACKET pPacket,
  121. OUT PUCHAR pIrPacketBuf,
  122. ULONG IrPacketBufLen,
  123. IN PUCHAR pContigPacketBuf,
  124. OUT PULONG pIrPacketLen
  125. )
  126. {
  127. PNDIS_BUFFER pNdisBuf;
  128. ULONG ndisPacketBytes = 0;
  129. ULONG I_fieldBytes, ndisPacketLen;
  130. FAST_IR_FCS_TYPE fcs;
  131. FAST_IR_FCS_TYPE UNALIGNED *pfcs;
  132. ULONG i, TotalBytes, EscSize;
  133. PSTIR4200_FRAME_HEADER pFrameHeader = (PSTIR4200_FRAME_HEADER)pIrPacketBuf;
  134. PUCHAR pIrPacketBufFrame = pIrPacketBuf + sizeof(STIR4200_FRAME_HEADER);
  135. /***********************************************/
  136. /* Get the packet's entire length and its */
  137. /* first NDIS buffer */
  138. /***********************************************/
  139. NdisQueryPacket( pPacket, NULL, NULL, &pNdisBuf, (UINT*)&ndisPacketLen );
  140. /***********************************************/
  141. /* Make sure that the packet is big enough */
  142. /* to be legal. It consists of an A, C, and */
  143. /* variable-length I field. */
  144. /***********************************************/
  145. if( ndisPacketLen < IRDA_A_C_TOTAL_SIZE )
  146. {
  147. DEBUGMSG(DBG_ERR, (" NdisToFirPacket(): Packet is too small\n"));
  148. return FALSE;
  149. }
  150. else
  151. {
  152. I_fieldBytes = ndisPacketLen - IRDA_A_C_TOTAL_SIZE;
  153. }
  154. /***********************************************/
  155. /* Make sure that we won't overwrite our */
  156. /* contiguous buffer */
  157. /***********************************************/
  158. if( (ndisPacketLen > MAX_TOTAL_SIZE_WITH_ALL_HEADERS) ||
  159. (MAX_POSSIBLE_IR_PACKET_SIZE_FOR_DATA(I_fieldBytes) > IrPacketBufLen) )
  160. {
  161. /***********************************************/
  162. /* The packet is too large. Tell the caller */
  163. /* to retry with a packet size large enough */
  164. /* to get past this stage next time. */
  165. /***********************************************/
  166. DEBUGMSG(DBG_ERR, (" NdisToFirPacket(): Packet is too big\n"));
  167. return FALSE;
  168. }
  169. /***********************************************/
  170. /* Read the NDIS packet into a contiguous */
  171. /* buffer. We have to do this in two steps */
  172. /* so that we can compute the FCS BEFORE */
  173. /* applying escape-byte transparency. */
  174. /***********************************************/
  175. while( pNdisBuf )
  176. {
  177. UCHAR *bufData;
  178. ULONG bufLen;
  179. #if defined(LEGACY_NDIS5)
  180. NdisQueryBuffer( pNdisBuf, (PVOID*)&bufData, (UINT*)&bufLen );
  181. #else
  182. NdisQueryBufferSafe( pNdisBuf, (PVOID*)&bufData, (UINT*)&bufLen, NormalPagePriority );
  183. if (bufData == NULL)
  184. {
  185. DEBUGMSG(DBG_ERR, (" NdisToFirPacket(): Could not query buffer\n"));
  186. return FALSE;
  187. }
  188. #endif
  189. if( (ndisPacketBytes + bufLen) > ndisPacketLen )
  190. {
  191. //
  192. // Packet was corrupt -- it misreported its size.
  193. //
  194. DEBUGMSG(DBG_ERR, (" NdisToFirPacket(): Packet is corrupt\n"));
  195. return FALSE;
  196. }
  197. NdisMoveMemory( (PVOID)(pContigPacketBuf + ndisPacketBytes), (PVOID)bufData, bufLen );
  198. ndisPacketBytes += bufLen;
  199. NdisGetNextBuffer( pNdisBuf, &pNdisBuf );
  200. }
  201. /***********************************************/
  202. /* Do sanity check on length of packet... */
  203. /***********************************************/
  204. if( ndisPacketBytes != ndisPacketLen )
  205. {
  206. //
  207. // Packet was corrupt -- it misreported its size.
  208. //
  209. DEBUGMSG(DBG_ERR, (" NdisToFirPacket(): Packet is corrupt\n"));
  210. return FALSE;
  211. }
  212. /***********************************************/
  213. /* Compute the FCS on the packet BEFORE */
  214. /* applying transparency fixups. The FCS */
  215. /* also must be sent using ESC-char */
  216. /* transparency. */
  217. /***********************************************/
  218. fcs = ComputeFCS32( pContigPacketBuf, ndisPacketBytes );
  219. /***********************************************/
  220. /* Add FCS to packet... */
  221. /***********************************************/
  222. pfcs = (FAST_IR_FCS_TYPE *)&pContigPacketBuf[ndisPacketBytes];
  223. *pfcs = fcs;
  224. /***********************************************/
  225. /* Build the STIr4200 FIR frame. */
  226. /***********************************************/
  227. /***********************************************/
  228. /* Add preamble... */
  229. /***********************************************/
  230. memset( pIrPacketBufFrame, STIR4200_FIR_PREAMBLE, STIR4200_FIR_PREAMBLE_SIZ );
  231. /***********************************************/
  232. /* Add BOF's... */
  233. /***********************************************/
  234. memset( &pIrPacketBufFrame[STIR4200_FIR_PREAMBLE_SIZ], STIR4200_FIR_BOF, STIR4200_FIR_BOF_SIZ );
  235. /***********************************************/
  236. /* Escape A, C, I & CRC fields of packet... */
  237. /***********************************************/
  238. EscSize = ndisPacketBytes + FAST_IR_FCS_SIZE;
  239. for( i = 0, TotalBytes = STIR4200_FIR_PREAMBLE_SIZ + STIR4200_FIR_BOF_SIZ; i < EscSize; i++ )
  240. {
  241. UCHAR c;
  242. switch( c = pContigPacketBuf[i] )
  243. {
  244. case STIR4200_FIR_ESC_CHAR:
  245. pIrPacketBufFrame[TotalBytes++] = STIR4200_FIR_ESC_CHAR;
  246. pIrPacketBufFrame[TotalBytes++] = STIR4200_FIR_ESC_DATA_7D;
  247. break;
  248. case STIR4200_FIR_BOF: // BOF = EOF too
  249. pIrPacketBufFrame[TotalBytes++] = STIR4200_FIR_ESC_CHAR;
  250. pIrPacketBufFrame[TotalBytes++] = STIR4200_FIR_ESC_DATA_7E;
  251. break;
  252. case STIR4200_FIR_PREAMBLE:
  253. pIrPacketBufFrame[TotalBytes++] = STIR4200_FIR_ESC_CHAR;
  254. pIrPacketBufFrame[TotalBytes++] = STIR4200_FIR_ESC_DATA_7F;
  255. break;
  256. default:
  257. pIrPacketBufFrame[TotalBytes++] = c;
  258. }
  259. }
  260. /***********************************************/
  261. /* Add EOF's... */
  262. /***********************************************/
  263. memset( &pIrPacketBufFrame[TotalBytes], STIR4200_FIR_EOF, STIR4200_FIR_EOF_SIZ );
  264. /***********************************************/
  265. /* Add in STIr4200 header... */
  266. /***********************************************/
  267. TotalBytes += STIR4200_FIR_EOF_SIZ;
  268. pFrameHeader->id1 = STIR4200_HEADERID_BYTE1;
  269. pFrameHeader->id2 = STIR4200_HEADERID_BYTE2;
  270. pFrameHeader->sizlsb = LOBYTE(TotalBytes);
  271. pFrameHeader->sizmsb = HIBYTE(TotalBytes);
  272. /***********************************************/
  273. /* Calc size packet w/escaped data... */
  274. /***********************************************/
  275. *pIrPacketLen = TotalBytes + sizeof(STIR4200_FRAME_HEADER);
  276. return TRUE;
  277. }
  278. /*****************************************************************************
  279. *
  280. * Function: NdisToMirPacket
  281. *
  282. * Synopsis: convert an NDIS packet to a Mir IR packet
  283. *
  284. * Write the IR packet into the provided buffer and report
  285. * its actual size.
  286. *
  287. * Arguments: pIrDev - pointer to device instance
  288. * pPacket - NDIS packet to convert
  289. * pIrPacketBuf - output buffer
  290. * IrPacketBufLen - output buffer size
  291. * pContigPacketBuf - temporary staging buffer
  292. * pIrPacketLen - lenght of the converted data
  293. *
  294. * Returns: TRUE - on success
  295. * FALSE - on failure
  296. *
  297. *
  298. *****************************************************************************/
  299. BOOLEAN
  300. NdisToMirPacket(
  301. IN PIR_DEVICE pIrDev,
  302. IN PNDIS_PACKET pPacket,
  303. OUT PUCHAR pIrPacketBuf,
  304. ULONG IrPacketBufLen,
  305. IN PUCHAR pContigPacketBuf,
  306. OUT PULONG pIrPacketLen
  307. )
  308. {
  309. PNDIS_BUFFER pNdisBuf;
  310. ULONG ndisPacketBytes = 0;
  311. ULONG I_fieldBytes, ndisPacketLen;
  312. MEDIUM_IR_FCS_TYPE fcs;
  313. MEDIUM_IR_FCS_TYPE UNALIGNED *pfcs;
  314. ULONG i, TotalBytes, EscSize;
  315. PSTIR4200_FRAME_HEADER pFrameHeader = (PSTIR4200_FRAME_HEADER)pIrPacketBuf;
  316. PUCHAR pIrPacketBufFrame = pIrPacketBuf + sizeof(STIR4200_FRAME_HEADER);
  317. /***********************************************/
  318. /* Get the packet's entire length and its */
  319. /* first NDIS buffer */
  320. /***********************************************/
  321. NdisQueryPacket( pPacket, NULL, NULL, &pNdisBuf, (UINT*)&ndisPacketLen );
  322. /***********************************************/
  323. /* Make sure that the packet is big enough */
  324. /* to be legal. It consists of an A, C, and */
  325. /* variable-length I field. */
  326. /***********************************************/
  327. if( ndisPacketLen < IRDA_A_C_TOTAL_SIZE )
  328. {
  329. DEBUGMSG(DBG_ERR, (" NdisToMirPacket(): Packet too small\n"));
  330. return FALSE;
  331. }
  332. else
  333. {
  334. I_fieldBytes = ndisPacketLen - IRDA_A_C_TOTAL_SIZE;
  335. }
  336. /***********************************************/
  337. /* Make sure that we won't overwrite our */
  338. /* contiguous buffer */
  339. /***********************************************/
  340. if( (ndisPacketLen > MAX_TOTAL_SIZE_WITH_ALL_HEADERS) ||
  341. (MAX_POSSIBLE_IR_PACKET_SIZE_FOR_DATA(I_fieldBytes) > IrPacketBufLen) )
  342. {
  343. /***********************************************/
  344. /* The packet is too large. Tell the caller */
  345. /* to retry with a packet size large enough */
  346. /* to get past this stage next time. */
  347. /***********************************************/
  348. DEBUGMSG(DBG_ERR, (" NdisToMirPacket(): Packet too big\n"));
  349. return FALSE;
  350. }
  351. /***********************************************/
  352. /* Read the NDIS packet into a contiguous */
  353. /* buffer. We have to do this in two steps */
  354. /* so that we can compute the FCS BEFORE */
  355. /* applying escape-byte transparency. */
  356. /***********************************************/
  357. while( pNdisBuf )
  358. {
  359. UCHAR *bufData;
  360. ULONG bufLen;
  361. #if defined(LEGACY_NDIS5)
  362. NdisQueryBuffer( pNdisBuf, (PVOID*)&bufData, (UINT*)&bufLen );
  363. #else
  364. NdisQueryBufferSafe( pNdisBuf, (PVOID*)&bufData, (UINT*)&bufLen, NormalPagePriority );
  365. if (bufData == NULL)
  366. {
  367. DEBUGMSG(DBG_ERR, (" NdisToMirPacket(): Could not query buffer\n"));
  368. return FALSE;
  369. }
  370. #endif
  371. if( ndisPacketBytes + bufLen > ndisPacketLen )
  372. {
  373. //
  374. // Packet was corrupt -- it misreported its size.
  375. //
  376. DEBUGMSG(DBG_ERR, (" NdisToMirPacket(): Packet is corrupt\n"));
  377. *pIrPacketLen = 0;
  378. return FALSE;
  379. }
  380. NdisMoveMemory( (PVOID)(pContigPacketBuf + ndisPacketBytes), (PVOID)bufData, bufLen );
  381. ndisPacketBytes += bufLen;
  382. NdisGetNextBuffer( pNdisBuf, &pNdisBuf );
  383. }
  384. /***********************************************/
  385. /* Do sanity check on length of packet... */
  386. /***********************************************/
  387. if( ndisPacketBytes != ndisPacketLen )
  388. {
  389. //
  390. // Packet was corrupt -- it misreported its size.
  391. //
  392. DEBUGMSG(DBG_ERR, (" NdisToMirPacket(): Packet is corrupt\n"));
  393. return FALSE;
  394. }
  395. /***********************************************/
  396. /* Compute the FCS on the packet BEFORE */
  397. /* applying transparency fixups. The FCS */
  398. /* also must be sent using ESC-char */
  399. /* transparency. */
  400. /***********************************************/
  401. fcs = ComputeFCS16( pContigPacketBuf, ndisPacketBytes );
  402. /***********************************************/
  403. /* Add FCS to packet... */
  404. /***********************************************/
  405. pfcs = (MEDIUM_IR_FCS_TYPE *)&pContigPacketBuf[ndisPacketBytes];
  406. *pfcs = fcs;
  407. /***********************************************/
  408. /* Build the STIr4200 MIR frame. */
  409. /***********************************************/
  410. /***********************************************/
  411. /* Add BOF's... */
  412. /***********************************************/
  413. memset( pIrPacketBufFrame, STIR4200_MIR_BOF, STIR4200_MIR_BOF_SIZ );
  414. /***********************************************/
  415. /* Escape A, C, I & CRC fields of packet... */
  416. /***********************************************/
  417. EscSize = ndisPacketBytes + MEDIUM_IR_FCS_SIZE;
  418. for( i = 0, TotalBytes = STIR4200_MIR_BOF_SIZ; i < EscSize; i++ )
  419. {
  420. UCHAR c;
  421. switch( c = pContigPacketBuf[i] )
  422. {
  423. case STIR4200_MIR_ESC_CHAR:
  424. pIrPacketBufFrame[TotalBytes++] = STIR4200_MIR_ESC_CHAR;
  425. pIrPacketBufFrame[TotalBytes++] = STIR4200_MIR_ESC_DATA_7D;
  426. break;
  427. case STIR4200_MIR_BOF: // BOF = EOF too
  428. pIrPacketBufFrame[TotalBytes++] = STIR4200_MIR_ESC_CHAR;
  429. pIrPacketBufFrame[TotalBytes++] = STIR4200_MIR_ESC_DATA_7E;
  430. break;
  431. default:
  432. pIrPacketBufFrame[TotalBytes++] = c;
  433. }
  434. }
  435. /***********************************************/
  436. /* Add EOF's... */
  437. /***********************************************/
  438. memset( &pIrPacketBufFrame[TotalBytes], STIR4200_MIR_EOF, STIR4200_MIR_EOF_SIZ );
  439. /***********************************************/
  440. /* Add in STIr4200 header... */
  441. /***********************************************/
  442. TotalBytes += STIR4200_MIR_EOF_SIZ;
  443. pFrameHeader->id1 = STIR4200_HEADERID_BYTE1;
  444. pFrameHeader->id2 = STIR4200_HEADERID_BYTE2;
  445. pFrameHeader->sizlsb = LOBYTE(TotalBytes);
  446. pFrameHeader->sizmsb = HIBYTE(TotalBytes);
  447. /***********************************************/
  448. /* Calc size packet w/escaped data... */
  449. /***********************************************/
  450. *pIrPacketLen = TotalBytes + sizeof(STIR4200_FRAME_HEADER);
  451. return TRUE;
  452. }
  453. /*****************************************************************************
  454. *
  455. * Function: NdisToSirPacket
  456. *
  457. * Synopsis: convert an NDIS packet to a Sir IR packet
  458. *
  459. * Write the IR packet into the provided buffer and report
  460. * its actual size.
  461. *
  462. * Arguments: pIrDev - pointer to device instance
  463. * pPacket - NDIS packet to convert
  464. * pIrPacketBuf - output buffer
  465. * IrPacketBufLen - output buffer size
  466. * pContigPacketBuf - temporary staging buffer
  467. * pIrPacketLen - lenght of the converted data
  468. *
  469. * Returns: TRUE - on success
  470. * FALSE - on failure
  471. *
  472. *
  473. *****************************************************************************/
  474. BOOLEAN
  475. NdisToSirPacket(
  476. IN PIR_DEVICE pIrDev,
  477. IN PNDIS_PACKET pPacket,
  478. OUT PUCHAR pIrPacketBuf,
  479. ULONG IrPacketBufLen,
  480. IN PUCHAR pContigPacketBuf,
  481. OUT PULONG pIrPacketLen
  482. )
  483. {
  484. PNDIS_BUFFER pNdisBuf;
  485. ULONG i, ndisPacketBytes = 0;
  486. ULONG I_fieldBytes, totalBytes = 0;
  487. ULONG ndisPacketLen, numExtraBOFs;
  488. SLOW_IR_FCS_TYPE fcs, tmpfcs;
  489. UCHAR fcsBuf[SLOW_IR_FCS_SIZE * 2];
  490. ULONG fcsLen = 0;
  491. PNDIS_IRDA_PACKET_INFO pPacketInfo = GetPacketInfo(pPacket);
  492. UCHAR nextChar;
  493. PSTIR4200_FRAME_HEADER pFrameHeader = (PSTIR4200_FRAME_HEADER)pIrPacketBuf;
  494. PUCHAR pIrPacketBufFrame = pIrPacketBuf + sizeof(STIR4200_FRAME_HEADER);
  495. /***********************************************/
  496. /* Get the packet's entire length and its */
  497. /* first NDIS buffer */
  498. /***********************************************/
  499. NdisQueryPacket( pPacket, NULL, NULL, &pNdisBuf, (UINT*)&ndisPacketLen );
  500. /***********************************************/
  501. /* Make sure that the packet is big enough */
  502. /* to be legal. It consists of an A, C, and */
  503. /* variable-length I field. */
  504. /***********************************************/
  505. if( ndisPacketLen < IRDA_A_C_TOTAL_SIZE )
  506. {
  507. DEBUGMSG(DBG_ERR, (" NdisToSirPacket(): Packet is too small\n"));
  508. return FALSE;
  509. }
  510. else
  511. {
  512. I_fieldBytes = ndisPacketLen - IRDA_A_C_TOTAL_SIZE;
  513. }
  514. /***********************************************/
  515. /* Make sure that we won't overwrite our */
  516. /* contiguous buffer. Make sure that the */
  517. /* passed-in buffer can accomodate this */
  518. /* packet's data no matter how much it */
  519. /* grows through adding ESC-sequences, etc. */
  520. /***********************************************/
  521. if( (ndisPacketLen > MAX_TOTAL_SIZE_WITH_ALL_HEADERS) ||
  522. (MAX_POSSIBLE_IR_PACKET_SIZE_FOR_DATA(I_fieldBytes) > IrPacketBufLen) )
  523. {
  524. //
  525. // Packet is too big
  526. //
  527. DEBUGMSG(DBG_ERR, (" NdisToSirPacket(): Packet is too big\n"));
  528. return FALSE;
  529. }
  530. /***********************************************/
  531. /* First, read the NDIS packet into a */
  532. /* contiguous buffer. We have to do this in */
  533. /* two steps so that we can compute the FCS */
  534. /* BEFORE applying escape-byte */
  535. /* transparency. */
  536. /***********************************************/
  537. while( pNdisBuf )
  538. {
  539. UCHAR *bufData;
  540. ULONG bufLen;
  541. #if defined(LEGACY_NDIS5)
  542. NdisQueryBuffer( pNdisBuf, (PVOID*)&bufData, (UINT*)&bufLen );
  543. #else
  544. NdisQueryBufferSafe( pNdisBuf, (PVOID*)&bufData, (UINT*)&bufLen, NormalPagePriority );
  545. if (bufData == NULL)
  546. {
  547. DEBUGMSG(DBG_ERR, (" NdisToSirPacket(): Could not query buffer\n"));
  548. return FALSE;
  549. }
  550. #endif
  551. if( (ndisPacketBytes + bufLen) > ndisPacketLen )
  552. {
  553. //
  554. // Packet was corrupt -- it misreported its size.
  555. //
  556. DEBUGMSG(DBG_ERR, (" NdisToSirPacket(): Packet is corrupt\n"));
  557. return FALSE;
  558. }
  559. NdisMoveMemory( (PVOID)(pContigPacketBuf + ndisPacketBytes), (PVOID)bufData, bufLen );
  560. ndisPacketBytes += bufLen;
  561. NdisGetNextBuffer( pNdisBuf, &pNdisBuf );
  562. }
  563. /***********************************************/
  564. /* Do sanity check on length of packet... */
  565. /***********************************************/
  566. if( ndisPacketBytes != ndisPacketLen )
  567. {
  568. //
  569. // Packet was corrupt -- it misreported its size.
  570. //
  571. DEBUGMSG(DBG_ERR, (" NdisToSirPacket(): Packet is corrupt\n"));
  572. return FALSE;
  573. }
  574. /***********************************************/
  575. /* Compute the FCS on the packet BEFORE */
  576. /* applying transparency fixups. The FCS */
  577. /* also must be sent using ESC-char */
  578. /* transparency, so figure out how large */
  579. /* the fcs will really be. */
  580. /***********************************************/
  581. fcs = ComputeFCS16( pContigPacketBuf, ndisPacketBytes );
  582. for( i = 0, tmpfcs = fcs, fcsLen = 0; i < SLOW_IR_FCS_SIZE; tmpfcs >>= 8, i++ )
  583. {
  584. UCHAR fcsbyte = tmpfcs & 0x00ff;
  585. switch( fcsbyte )
  586. {
  587. case SLOW_IR_BOF:
  588. case SLOW_IR_EOF:
  589. case SLOW_IR_ESC:
  590. fcsBuf[fcsLen++] = SLOW_IR_ESC;
  591. fcsBuf[fcsLen++] = fcsbyte ^ SLOW_IR_ESC_COMP;
  592. break;
  593. default:
  594. fcsBuf[fcsLen++] = fcsbyte;
  595. break;
  596. }
  597. }
  598. /***********************************************/
  599. /* Now begin building the IR frame. */
  600. /* */
  601. /* This is the final format: */
  602. /* */
  603. /* BOF (1) */
  604. /* extra BOFs ... */
  605. /* NdisMediumIrda packet (from NDIS): */
  606. /* Address (1) */
  607. /* Control (1) */
  608. /* FCS (2) */
  609. /* EOF (1) */
  610. /* */
  611. /* Prepend BOFs (extra BOFs + 1 actual BOF) */
  612. /***********************************************/
  613. numExtraBOFs = pPacketInfo->ExtraBOFs;
  614. if( numExtraBOFs > MAX_NUM_EXTRA_BOFS )
  615. {
  616. numExtraBOFs = MAX_NUM_EXTRA_BOFS;
  617. }
  618. for( i = totalBytes = 0; i < numExtraBOFs; i++ )
  619. {
  620. *(SLOW_IR_BOF_TYPE*)(pIrPacketBufFrame + totalBytes) = SLOW_IR_EXTRA_BOF;
  621. totalBytes += SLOW_IR_EXTRA_BOF_SIZE;
  622. }
  623. *(SLOW_IR_BOF_TYPE*)(pIrPacketBufFrame + totalBytes) = SLOW_IR_BOF;
  624. totalBytes += SLOW_IR_BOF_SIZE;
  625. /***********************************************/
  626. /* Copy the NDIS packet from our contiguous */
  627. /* buffer, applying escape-char */
  628. /* transparency. */
  629. /***********************************************/
  630. for( i = 0; i < ndisPacketBytes; i++ )
  631. {
  632. nextChar = pContigPacketBuf[i];
  633. switch( nextChar )
  634. {
  635. case SLOW_IR_BOF:
  636. case SLOW_IR_EOF:
  637. case SLOW_IR_ESC:
  638. pIrPacketBufFrame[totalBytes++] = SLOW_IR_ESC;
  639. pIrPacketBufFrame[totalBytes++] = nextChar ^ SLOW_IR_ESC_COMP;
  640. break;
  641. default:
  642. pIrPacketBufFrame[totalBytes++] = nextChar;
  643. break;
  644. }
  645. }
  646. /***********************************************/
  647. /* Add FCS, EOF. */
  648. /***********************************************/
  649. NdisMoveMemory( (PVOID)(pIrPacketBufFrame + totalBytes), (PVOID)fcsBuf, fcsLen );
  650. totalBytes += fcsLen;
  651. *(SLOW_IR_EOF_TYPE*)(pIrPacketBufFrame + totalBytes) = (UCHAR)SLOW_IR_EOF;
  652. totalBytes += SLOW_IR_EOF_SIZE;
  653. /***********************************************/
  654. /* Add in STIr4200 header... */
  655. /***********************************************/
  656. pFrameHeader->id1 = STIR4200_HEADERID_BYTE1;
  657. pFrameHeader->id2 = STIR4200_HEADERID_BYTE2;
  658. pFrameHeader->sizlsb = LOBYTE(totalBytes);
  659. pFrameHeader->sizmsb = HIBYTE(totalBytes);
  660. *pIrPacketLen = totalBytes + sizeof(STIR4200_FRAME_HEADER);
  661. return TRUE;
  662. }
  663. /*****************************************************************************
  664. *
  665. * Function: ComputeFCS16
  666. *
  667. * Synopsis: Calculates the 16 bit CRC.
  668. *
  669. * Arguments: pData - pointer to data buffer
  670. * DataLen - length of data buffer
  671. *
  672. * Returns: Calculated CRC
  673. *
  674. * Notes:
  675. *
  676. *****************************************************************************/
  677. USHORT
  678. ComputeFCS16(
  679. IN PUCHAR pData,
  680. UINT DataLen
  681. )
  682. {
  683. USHORT fcs = 0xffff;
  684. UINT i;
  685. for( i = 0; i < DataLen; i++ )
  686. {
  687. fcs = (fcs >> 8) ^ fcsTable16[(fcs ^ *pData++)& 0xff];
  688. }
  689. fcs = ~fcs;
  690. return fcs;
  691. }
  692. /*****************************************************************************
  693. *
  694. * Function: ComputeFCS32
  695. *
  696. * Synopsis: Calculates the 32 bit CRC.
  697. *
  698. * Arguments: pData - pointer to data buffer
  699. * DataLen - length of data buffer
  700. *
  701. * Returns: Calculated CRC
  702. *
  703. * Notes:
  704. *
  705. *****************************************************************************/
  706. #define USE_FASTER_CRC32 1
  707. ULONG
  708. ComputeFCS32(
  709. IN PUCHAR pData,
  710. ULONG DataLen
  711. )
  712. {
  713. #ifdef USE_FASTER_CRC32
  714. #define UPDC32(octet, crc) (crc_32_tab[((crc) ^ ((UCHAR)octet)) & 0xff] ^ ((crc) >> 8))
  715. ULONG crc;
  716. crc = 0xFFFFFFFF;
  717. for( ; DataLen; --DataLen, ++pData )
  718. {
  719. crc = (fcsTable32[((crc) ^ (*pData)) & 0xff] ^ ((crc) >> 8));
  720. }
  721. return ~crc;
  722. #else
  723. ULONG crc, temp1, temp2;
  724. crc = 0xFFFFFFFF;
  725. while( DataLen-- != 0 )
  726. {
  727. temp1 = (crc >> 8) & 0x00FFFFFFL;
  728. temp2 = fcsTable32[((int)crc ^ *pData++) & 0xff];
  729. crc = temp1 ^ temp2;
  730. }
  731. return crc;
  732. #endif
  733. }