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.

6695 lines
192 KiB

  1. /*****************************************************************************
  2. @doc INT EXT
  3. ******************************************************************************
  4. * $ProjectName: $
  5. * $ProjectRevision: $
  6. *-----------------------------------------------------------------------------
  7. * $Source: z:/pr/cmbs0/sw/sccmn50m.ms/rcs/sccmcb.c $
  8. * $Revision: 1.7 $
  9. *-----------------------------------------------------------------------------
  10. * $Author: WFrischauf $
  11. *-----------------------------------------------------------------------------
  12. * History: see EOF
  13. *-----------------------------------------------------------------------------
  14. *
  15. * Copyright 2000 OMNIKEY AG
  16. ******************************************************************************/
  17. #include <stdio.h>
  18. #include "sccmn50m.h"
  19. static ULONG dataRatesSupported[] = {9600,38400};
  20. static ULONG CLKFrequenciesSupported[] = {4000,5000};
  21. /*****************************************************************************
  22. Routine Description:
  23. Arguments:
  24. Return Value:
  25. *****************************************************************************/
  26. NTSTATUS
  27. SCCMN50M_CardPower(PSMARTCARD_EXTENSION pSmartcardExtension)
  28. {
  29. NTSTATUS status = STATUS_SUCCESS;
  30. NTSTATUS DebugStatus = STATUS_SUCCESS;
  31. UCHAR pbAtrBuffer[MAXIMUM_ATR_LENGTH];
  32. UCHAR abSyncAtrBuffer[MAXIMUM_ATR_LENGTH];
  33. ULONG ulAtrLength;
  34. #if DBG
  35. ULONG i;
  36. #endif;
  37. SmartcardDebug(
  38. DEBUG_TRACE,
  39. ( "%s!CardPower: Enter\n",
  40. DRIVER_NAME)
  41. );
  42. #if DBG
  43. switch (pSmartcardExtension->MinorIoControlCode)
  44. {
  45. case SCARD_WARM_RESET:
  46. SmartcardDebug(
  47. DEBUG_ATR,
  48. ( "%s!SCARD_WARM_RESTART\n",
  49. DRIVER_NAME)
  50. );
  51. break;
  52. case SCARD_COLD_RESET:
  53. SmartcardDebug(
  54. DEBUG_ATR,
  55. ( "%s!SCARD_COLD_RESTART\n",
  56. DRIVER_NAME)
  57. );
  58. break;
  59. case SCARD_POWER_DOWN:
  60. SmartcardDebug(
  61. DEBUG_ATR,
  62. ( "%s!SCARD_POWER_DOWN\n",
  63. DRIVER_NAME)
  64. );
  65. break;
  66. }
  67. #endif
  68. //DbgBreakPoint();
  69. switch (pSmartcardExtension->MinorIoControlCode)
  70. {
  71. case SCARD_WARM_RESET:
  72. case SCARD_COLD_RESET:
  73. status = SCCMN50M_PowerOn(pSmartcardExtension,
  74. &ulAtrLength,
  75. pbAtrBuffer,
  76. sizeof(pbAtrBuffer));
  77. if (status != STATUS_SUCCESS)
  78. {
  79. goto ExitReaderPower;
  80. }
  81. pSmartcardExtension->CardCapabilities.ATR.Length = (UCHAR)ulAtrLength;
  82. if (pSmartcardExtension->ReaderExtension->fRawModeNecessary == FALSE)
  83. {
  84. // copy ATR to smart card structure
  85. // the lib needs the ATR for evaluation of the card parameters
  86. MemCpy(pSmartcardExtension->CardCapabilities.ATR.Buffer,
  87. sizeof(pSmartcardExtension->CardCapabilities.ATR.Buffer),
  88. pbAtrBuffer,
  89. ulAtrLength);
  90. pSmartcardExtension->CardCapabilities.ATR.Length = (UCHAR)ulAtrLength;
  91. pSmartcardExtension->ReaderCapabilities.CurrentState = SCARD_NEGOTIABLE;
  92. pSmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  93. status = SmartcardUpdateCardCapabilities(pSmartcardExtension);
  94. if (status != STATUS_SUCCESS)
  95. {
  96. goto ExitReaderPower;
  97. }
  98. // add extra guard time value to card stop bits
  99. pSmartcardExtension->ReaderExtension->CardManConfig.CardStopBits = (UCHAR)(pSmartcardExtension->CardCapabilities.N);
  100. // copy ATR to user space
  101. MemCpy(pSmartcardExtension->IoRequest.ReplyBuffer,
  102. pSmartcardExtension->IoRequest.ReplyBufferLength,
  103. pbAtrBuffer,
  104. ulAtrLength);
  105. *pSmartcardExtension->IoRequest.Information = ulAtrLength;
  106. #if DBG
  107. SmartcardDebug(DEBUG_ATR,
  108. ("%s!ATR : ",
  109. DRIVER_NAME));
  110. for (i = 0;i < ulAtrLength;i++)
  111. {
  112. SmartcardDebug(DEBUG_ATR,
  113. ("%2.2x ",
  114. pSmartcardExtension->CardCapabilities.ATR.Buffer[i]));
  115. }
  116. SmartcardDebug(DEBUG_ATR,("\n"));
  117. #endif
  118. }
  119. else
  120. {
  121. abSyncAtrBuffer[0] = 0x3B;
  122. abSyncAtrBuffer[1] = 0x04;
  123. MemCpy(&abSyncAtrBuffer[2],
  124. sizeof(abSyncAtrBuffer)-2,
  125. pbAtrBuffer,
  126. ulAtrLength);
  127. ulAtrLength += 2;
  128. MemCpy(pSmartcardExtension->CardCapabilities.ATR.Buffer,
  129. sizeof(pSmartcardExtension->CardCapabilities.ATR.Buffer),
  130. abSyncAtrBuffer,
  131. ulAtrLength);
  132. pSmartcardExtension->CardCapabilities.ATR.Length = (UCHAR)(ulAtrLength);
  133. pSmartcardExtension->ReaderCapabilities.CurrentState = SCARD_SPECIFIC;
  134. pSmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_T0;
  135. status = SmartcardUpdateCardCapabilities(pSmartcardExtension);
  136. if (status != STATUS_SUCCESS)
  137. {
  138. goto ExitReaderPower;
  139. }
  140. SmartcardDebug(DEBUG_ATR,("ATR of synchronous smart card : %2.2x %2.2x %2.2x %2.2x\n",
  141. pbAtrBuffer[0],pbAtrBuffer[1],pbAtrBuffer[2],pbAtrBuffer[3]));
  142. pSmartcardExtension->ReaderExtension->SyncParameters.fCardResetRequested = TRUE;
  143. // copy ATR to user space
  144. MemCpy(pSmartcardExtension->IoRequest.ReplyBuffer,
  145. pSmartcardExtension->IoRequest.ReplyBufferLength,
  146. abSyncAtrBuffer,
  147. ulAtrLength);
  148. *pSmartcardExtension->IoRequest.Information = ulAtrLength;
  149. }
  150. break;
  151. case SCARD_POWER_DOWN:
  152. status = SCCMN50M_PowerOff(pSmartcardExtension);
  153. if (status != STATUS_SUCCESS)
  154. {
  155. goto ExitReaderPower;
  156. }
  157. pSmartcardExtension->ReaderCapabilities.CurrentState = SCARD_SWALLOWED;
  158. pSmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  159. break;
  160. }
  161. ExitReaderPower:
  162. SmartcardDebug(
  163. DEBUG_TRACE,
  164. ( "%s!CardPower: Exit %lx\n",
  165. DRIVER_NAME,status)
  166. );
  167. return status;
  168. }
  169. /*****************************************************************************
  170. Routine Description:
  171. Arguments:
  172. Return Value:
  173. *****************************************************************************/
  174. NTSTATUS
  175. SCCMN50M_PowerOn (
  176. IN PSMARTCARD_EXTENSION pSmartcardExtension,
  177. OUT PULONG pulAtrLength,
  178. OUT PUCHAR pbAtrBuffer,
  179. IN ULONG ulAtrBufferSize
  180. )
  181. {
  182. NTSTATUS status;
  183. NTSTATUS DebugStatus;
  184. // We always use 0x80 for reset delay
  185. pSmartcardExtension->ReaderExtension->CardManConfig.ResetDelay = 0x80;
  186. if (SCCMN50M_IsAsynchronousSmartCard(pSmartcardExtension) == TRUE)
  187. {
  188. if (pSmartcardExtension->MinorIoControlCode == SCARD_COLD_RESET)
  189. {
  190. status = SCCMN50M_UseColdWarmResetStrategy(pSmartcardExtension,
  191. pulAtrLength,
  192. pbAtrBuffer,
  193. ulAtrBufferSize,
  194. FALSE);
  195. // if cold reset was not succesfull ,it maybe a SAMOS card with the sensor bug
  196. if (status != STATUS_SUCCESS)
  197. {
  198. status = SCCMN50M_UseColdWarmResetStrategy(pSmartcardExtension,
  199. pulAtrLength,
  200. pbAtrBuffer,
  201. ulAtrBufferSize,
  202. TRUE);
  203. if (status != STATUS_SUCCESS)
  204. {
  205. status = SCCMN50M_UseParsingStrategy(pSmartcardExtension,
  206. pulAtrLength,
  207. pbAtrBuffer,
  208. ulAtrBufferSize);
  209. }
  210. }
  211. }
  212. else
  213. {
  214. status = SCCMN50M_UseColdWarmResetStrategy(pSmartcardExtension,
  215. pulAtrLength,
  216. pbAtrBuffer,
  217. ulAtrBufferSize,
  218. TRUE);
  219. if (status != STATUS_SUCCESS)
  220. {
  221. status = SCCMN50M_UseParsingStrategy(pSmartcardExtension,
  222. pulAtrLength,
  223. pbAtrBuffer,
  224. ulAtrBufferSize);
  225. }
  226. }
  227. }
  228. else
  229. {
  230. SmartcardDebug(DEBUG_ATR,
  231. ("check if synchronous smart card is inserted\n"));
  232. // try to find a synchronous smart card
  233. status = SCCMN50M_UseSyncStrategy(pSmartcardExtension,
  234. pulAtrLength,
  235. pbAtrBuffer,
  236. ulAtrBufferSize);
  237. }
  238. if (status != STATUS_SUCCESS)
  239. {
  240. // smart card not powered
  241. status = STATUS_UNRECOGNIZED_MEDIA;
  242. *pulAtrLength = 0;
  243. DebugStatus = SCCMN50M_PowerOff(pSmartcardExtension);
  244. return status;
  245. }
  246. else
  247. {
  248. // add extra guard time value to card stop bits
  249. pSmartcardExtension->ReaderExtension->CardManConfig.CardStopBits = (UCHAR)(pSmartcardExtension->CardCapabilities.N);
  250. return status;
  251. }
  252. }
  253. /*****************************************************************************
  254. Routine Description:
  255. This routine inverts the buffer
  256. Bit0 -> Bit 7
  257. Bit1 -> Bit 6
  258. Bit2 -> Bit 5
  259. Bit3 -> Bit 4
  260. Bit4 -> Bit 3
  261. Bit5 -> Bit 2
  262. Bit6 -> Bit 1
  263. Bit7 -> Bit 0
  264. Arguments: pbBuffer ... pointer to buffer
  265. ulBufferSize ... size of buffer
  266. Return Value: none
  267. *****************************************************************************/
  268. VOID SCCMN50M_InverseBuffer (
  269. PUCHAR pbBuffer,
  270. ULONG ulBufferSize
  271. )
  272. {
  273. ULONG i;
  274. ULONG j;
  275. ULONG m;
  276. ULONG n;
  277. for (i=0; i<ulBufferSize; i++)
  278. {
  279. n = 0;
  280. for (j=1; j<=8; j++)
  281. {
  282. m = (pbBuffer[i] << j);
  283. m &= 0x00000100;
  284. n |= (m >> (9-j));
  285. }
  286. pbBuffer[i] = (UCHAR)~n;
  287. }
  288. return;
  289. }
  290. /*****************************************************************************
  291. Routine Description:
  292. This function always permforms a cold reset of the smart card.
  293. A SAMOS card with the sensor bug will not be powered by this function.
  294. Arguments:
  295. Return Value:
  296. *****************************************************************************/
  297. NTSTATUS
  298. SCCMN50M_UseParsingStrategy (IN PSMARTCARD_EXTENSION pSmartcardExtension,
  299. OUT PULONG pulAtrLength,
  300. OUT PUCHAR pbAtrBuffer,
  301. IN ULONG ulAtrBufferSize
  302. )
  303. {
  304. NTSTATUS status;
  305. NTSTATUS DebugStatus;
  306. UCHAR ulCardType;
  307. UCHAR ReadBuffer[SCARD_ATR_LENGTH];
  308. UCHAR bAtrBytesRead[SCARD_ATR_LENGTH];
  309. ULONG ulBytesRead;
  310. BOOLEAN fInverseAtr = FALSE;
  311. ULONG ulAtrBufferOffset = 0;
  312. ULONG ulHistoricalBytes;
  313. ULONG ulNextStepBytesToRead;
  314. ULONG ulPrevStepBytesRead;
  315. ULONG i;
  316. BOOLEAN fTDxSent;
  317. BOOLEAN fAtrParsed;
  318. BOOLEAN fOnlyT0 = TRUE;
  319. ULONG ulOldReadTotalTimeoutMultiplier;
  320. // DBGBreakPoint();
  321. // set ReadTotalTimeoutMultiplier to 250ms (9600 * 372/f = initial waiting time)
  322. ulOldReadTotalTimeoutMultiplier = pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier;
  323. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier = 250;
  324. pSmartcardExtension->ReaderExtension->CardManConfig.CardStopBits = 0x02;
  325. for (ulCardType = ASYNC3_CARD;ulCardType <= ASYNC5_CARD;ulCardType++)
  326. {
  327. // power off + resync
  328. status = SCCMN50M_PowerOff(pSmartcardExtension);
  329. if (status != STATUS_SUCCESS)
  330. {
  331. goto ExitUseParsingStrategy;
  332. }
  333. SCCMN50M_ClearCardControlFlags(pSmartcardExtension,ALL_FLAGS);
  334. SCCMN50M_SetSCRControlFlags(pSmartcardExtension,CARD_POWER| IGNORE_PARITY);
  335. if (ulCardType == ASYNC3_CARD)
  336. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_3MHZ);
  337. else
  338. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_5MHZ);
  339. SCCMN50M_SetCardManHeader(pSmartcardExtension,
  340. 0,
  341. 0,
  342. 0,
  343. 2); // TS and T0 expected
  344. SmartcardDebug(DEBUG_ATR,
  345. ("%s!ResetDelay = %d\n",
  346. DRIVER_NAME,
  347. pSmartcardExtension->ReaderExtension->CardManConfig.ResetDelay));
  348. // write config + header
  349. status = SCCMN50M_WriteCardMan(pSmartcardExtension,0,NULL);
  350. if (status != STATUS_SUCCESS)
  351. {
  352. goto ExitUseParsingStrategy;
  353. }
  354. // read state and length + TS + T0
  355. status = SCCMN50M_ReadCardMan(pSmartcardExtension,4,&ulBytesRead,ReadBuffer,sizeof(ReadBuffer));
  356. if (status != STATUS_SUCCESS)
  357. {
  358. continue; // try next card
  359. }
  360. // contents of read buffer
  361. // [0] ... state
  362. // [1] ... length
  363. // [2] ... TS
  364. // [3] ... T0
  365. // TS
  366. if (ReadBuffer[2] == CHAR_INV)
  367. {
  368. fInverseAtr = TRUE;
  369. }
  370. if (fInverseAtr)
  371. SCCMN50M_InverseBuffer(&ReadBuffer[3],1);
  372. ulHistoricalBytes = ReadBuffer[3] & 0x0F;
  373. ulPrevStepBytesRead = 2;
  374. // T0 codes following TA1 - TD1
  375. fAtrParsed = TRUE;
  376. SmartcardDebug(DEBUG_ATR,
  377. ("%s!Step : Bytes to read = 2\n",
  378. DRIVER_NAME));
  379. do
  380. {
  381. ulNextStepBytesToRead = ulPrevStepBytesRead;
  382. fTDxSent = FALSE;
  383. if (ReadBuffer[ulBytesRead - 1 ] & 0x10)
  384. ulNextStepBytesToRead++;
  385. if (ReadBuffer[ulBytesRead - 1 ] & 0x20)
  386. ulNextStepBytesToRead++;
  387. if (ReadBuffer[ulBytesRead - 1 ] & 0x40)
  388. ulNextStepBytesToRead++;
  389. if (ReadBuffer[ulBytesRead - 1 ] & 0x80)
  390. {
  391. ulNextStepBytesToRead++;
  392. fTDxSent = TRUE;
  393. }
  394. if (ulPrevStepBytesRead != 2 &&
  395. ReadBuffer[ulBytesRead -1 ] & 0x0f)
  396. {
  397. fOnlyT0 = FALSE;
  398. }
  399. // -----------------------
  400. // POWER OFF
  401. // -----------------------
  402. // turn power off and get state
  403. status = SCCMN50M_PowerOff(pSmartcardExtension);
  404. if (status != STATUS_SUCCESS)
  405. {
  406. fAtrParsed = FALSE;
  407. goto ExitUseParsingStrategy; // try next card
  408. }
  409. // -----------------------
  410. // POWER ON
  411. // -----------------------
  412. // turn on power flag
  413. SCCMN50M_SetSCRControlFlags(pSmartcardExtension,CARD_POWER| IGNORE_PARITY);
  414. if (ulCardType == ASYNC3_CARD)
  415. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_3MHZ);
  416. else
  417. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_5MHZ);
  418. SCCMN50M_SetCardManHeader(pSmartcardExtension,
  419. 0,
  420. 0,
  421. 0,
  422. (UCHAR)ulNextStepBytesToRead);
  423. // write config + header
  424. status = SCCMN50M_WriteCardMan(pSmartcardExtension,0,NULL);
  425. if (status != STATUS_SUCCESS)
  426. {
  427. fAtrParsed = FALSE;
  428. goto ExitUseParsingStrategy; // try next card
  429. }
  430. // read state and length + TAx,TBx,TCx,TDx
  431. SmartcardDebug(DEBUG_ATR,
  432. ("%s!Step : Bytes to read = %ld\n",
  433. DRIVER_NAME,
  434. ulNextStepBytesToRead));
  435. status = SCCMN50M_ReadCardMan(pSmartcardExtension,2 + ulNextStepBytesToRead,&ulBytesRead,ReadBuffer,sizeof(ReadBuffer));
  436. if (status != STATUS_SUCCESS)
  437. {
  438. fAtrParsed = FALSE;
  439. break; // try next card
  440. }
  441. if (fInverseAtr)
  442. SCCMN50M_InverseBuffer(&ReadBuffer[2],ulBytesRead-2);
  443. MemCpy(bAtrBytesRead,sizeof(bAtrBytesRead),&ReadBuffer[2],ulBytesRead -2);
  444. #if DBG
  445. SmartcardDebug(DEBUG_ATR,
  446. ("%s!read ATR bytes: ",
  447. DRIVER_NAME));
  448. for (i = 0;i < ulBytesRead-2;i++)
  449. SmartcardDebug(DEBUG_ATR,
  450. ("%2.2x ",
  451. bAtrBytesRead[i]));
  452. SmartcardDebug(DEBUG_ATR,("\n"));
  453. #endif
  454. ulPrevStepBytesRead = ulBytesRead - 2;
  455. } while (fTDxSent == TRUE);
  456. // +++++++++++++++++++++++++++++++++++++++
  457. // now we know how long the whole ATR is
  458. // +++++++++++++++++++++++++++++++++++++++
  459. // -----------------------
  460. // POWER OFF
  461. // -----------------------
  462. // turn power off and get state
  463. status = SCCMN50M_PowerOff(pSmartcardExtension);
  464. if (!NT_SUCCESS(status))
  465. {
  466. goto ExitUseParsingStrategy;
  467. }
  468. // -----------------------
  469. // POWER ON
  470. // -----------------------
  471. // turn on power flag
  472. SCCMN50M_SetSCRControlFlags(pSmartcardExtension,CARD_POWER| IGNORE_PARITY);
  473. if (ulCardType == ASYNC3_CARD)
  474. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_3MHZ);
  475. else
  476. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_5MHZ);
  477. // bug fix : old SAMOS cards have a damaged ATR
  478. if (bAtrBytesRead[0] == 0x3b &&
  479. bAtrBytesRead[1] == 0xbf &&
  480. bAtrBytesRead[2] == 0x11 &&
  481. bAtrBytesRead[3] == 0x00 &&
  482. bAtrBytesRead[4] == 0x81 &&
  483. bAtrBytesRead[5] == 0x31 &&
  484. bAtrBytesRead[6] == 0x90 &&
  485. bAtrBytesRead[7] == 0x73 )
  486. {
  487. ulHistoricalBytes = 4;
  488. }
  489. ulNextStepBytesToRead = ulPrevStepBytesRead + ulHistoricalBytes;
  490. if (fOnlyT0 == FALSE)
  491. ulNextStepBytesToRead++; // TCK !
  492. SCCMN50M_SetCardManHeader(pSmartcardExtension,
  493. 0,
  494. 0,
  495. 0,
  496. (UCHAR)ulNextStepBytesToRead);
  497. // write config + header
  498. status = SCCMN50M_WriteCardMan(pSmartcardExtension,0,NULL);
  499. if (status != STATUS_SUCCESS)
  500. {
  501. goto ExitUseParsingStrategy; // try next card
  502. }
  503. // read whole ATR
  504. SmartcardDebug(DEBUG_ATR,
  505. ("%s!Step : Bytes to read = %ld\n",
  506. DRIVER_NAME,
  507. ulNextStepBytesToRead));
  508. status = SCCMN50M_ReadCardMan(pSmartcardExtension,2+ulNextStepBytesToRead,&ulBytesRead,ReadBuffer,sizeof(ReadBuffer));
  509. if (!NT_SUCCESS(status))
  510. {
  511. SmartcardDebug(DEBUG_ATR,
  512. ("%s!Reading of whole ATR failed\n !",
  513. DRIVER_NAME));
  514. continue; // try next card
  515. }
  516. // check ATR
  517. if (ulBytesRead - 2 < MIN_ATR_LEN)
  518. {
  519. status = STATUS_UNRECOGNIZED_MEDIA;
  520. DebugStatus = SCCMN50M_ReadCardMan(pSmartcardExtension,2+ulNextStepBytesToRead,&ulBytesRead,ReadBuffer,sizeof(ReadBuffer));
  521. goto ExitUseParsingStrategy;
  522. }
  523. if (ulBytesRead -2 > ulAtrBufferSize)
  524. {
  525. // the ATR is larger then 33 bytes !!!
  526. status = STATUS_BUFFER_OVERFLOW;
  527. goto ExitUseParsingStrategy;
  528. }
  529. SCCMN50M_CheckAtrModified(pbAtrBuffer,*pulAtrLength);
  530. // pass ATR and ATR length to calling function
  531. MemCpy(pbAtrBuffer,ulAtrBufferSize,&ReadBuffer[2],ulBytesRead -2);
  532. *pulAtrLength = ulBytesRead -2;
  533. if (fInverseAtr)
  534. {
  535. SCCMN50M_SetCardControlFlags(pSmartcardExtension,INVERSE_DATA);
  536. SCCMN50M_InverseBuffer(pbAtrBuffer,*pulAtrLength);
  537. pSmartcardExtension->ReaderExtension->fInverseAtr = TRUE;
  538. }
  539. else
  540. {
  541. pSmartcardExtension->ReaderExtension->fInverseAtr = FALSE;
  542. }
  543. pSmartcardExtension->ReaderExtension->fRawModeNecessary = FALSE;
  544. break;
  545. }
  546. ExitUseParsingStrategy:
  547. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier
  548. = ulOldReadTotalTimeoutMultiplier ;
  549. SCCMN50M_ClearSCRControlFlags(pSmartcardExtension,IGNORE_PARITY);
  550. SCCMN50M_ClearCardManHeader(pSmartcardExtension);
  551. return status;
  552. }
  553. /*****************************************************************************
  554. Routine Description:
  555. This function performs either a cold or a warm reset depending on
  556. the fWarmReset parameter .
  557. Arguments:
  558. Return Value:
  559. *****************************************************************************/
  560. NTSTATUS
  561. SCCMN50M_UseColdWarmResetStrategy (IN PSMARTCARD_EXTENSION pSmartcardExtension,
  562. OUT PULONG pulAtrLength,
  563. OUT PUCHAR pbAtrBuffer,
  564. IN ULONG ulAtrBufferSize,
  565. IN BOOLEAN fWarmReset
  566. )
  567. {
  568. NTSTATUS status;
  569. NTSTATUS DebugStatus;
  570. ULONG ulCardType;
  571. UCHAR bReadBuffer[SCARD_ATR_LENGTH];
  572. ULONG ulBytesRead;
  573. ULONG ulOldReadTotalTimeoutMultiplier;
  574. //DBGBreakPoint();
  575. // set ReadTotalTimeoutMultiplier to 250ms (9600 * 372/f = initial waiting time)
  576. ulOldReadTotalTimeoutMultiplier = pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier;
  577. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier = 250;
  578. if (fWarmReset == FALSE)
  579. {
  580. SCCMN50M_SetCardManHeader(pSmartcardExtension,0,0,COLD_RESET,ATR_LEN_ASYNC);
  581. }
  582. else
  583. {
  584. SCCMN50M_SetCardManHeader(pSmartcardExtension,0,0,0,ATR_LEN_ASYNC);
  585. }
  586. pSmartcardExtension->ReaderExtension->CardManConfig.CardStopBits = 0x02;
  587. for (ulCardType = ASYNC3_CARD;ulCardType <= ASYNC5_CARD;ulCardType++)
  588. {
  589. SCCMN50M_ClearCardControlFlags(pSmartcardExtension,ALL_FLAGS);
  590. SCCMN50M_SetSCRControlFlags(pSmartcardExtension,CARD_POWER| IGNORE_PARITY | CM2_GET_ATR);
  591. if (ulCardType == ASYNC3_CARD)
  592. {
  593. SmartcardDebug(
  594. DEBUG_ATR,
  595. ("%s!ASYNC_3\n",
  596. DRIVER_NAME));
  597. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_3MHZ);
  598. }
  599. else
  600. {
  601. SmartcardDebug(
  602. DEBUG_ATR,
  603. ("%s!ASYN_5\n",
  604. DRIVER_NAME));
  605. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_5MHZ);
  606. }
  607. status = SCCMN50M_ResyncCardManII(pSmartcardExtension);
  608. if (status != STATUS_SUCCESS)
  609. {
  610. goto ExitUseColdWarmResetStrategy;
  611. }
  612. // write config + header
  613. status = SCCMN50M_WriteCardMan(pSmartcardExtension,0,NULL);
  614. if (status != STATUS_SUCCESS)
  615. {
  616. goto ExitUseColdWarmResetStrategy;
  617. }
  618. pSmartcardExtension->ReaderExtension->ToRHConfig = FALSE;
  619. // read state and length
  620. status = SCCMN50M_ReadCardMan(pSmartcardExtension,2,&ulBytesRead,bReadBuffer,sizeof(bReadBuffer));
  621. if (status != STATUS_SUCCESS)
  622. {
  623. continue; // try next card
  624. }
  625. if (bReadBuffer[1] < MIN_ATR_LEN)
  626. {
  627. // read all remaining bytes from the CardMan
  628. DebugStatus = SCCMN50M_ReadCardMan(pSmartcardExtension,bReadBuffer[1],&ulBytesRead,bReadBuffer,sizeof(bReadBuffer));
  629. status = STATUS_UNRECOGNIZED_MEDIA;
  630. goto ExitUseColdWarmResetStrategy;
  631. }
  632. if (bReadBuffer[1] > ulAtrBufferSize)
  633. {
  634. status = STATUS_BUFFER_OVERFLOW;
  635. goto ExitUseColdWarmResetStrategy;
  636. }
  637. // read ATR
  638. status = SCCMN50M_ReadCardMan(pSmartcardExtension,bReadBuffer[1],pulAtrLength,pbAtrBuffer,ulAtrBufferSize);
  639. if (status != STATUS_SUCCESS)
  640. {
  641. continue;
  642. }
  643. switch (pbAtrBuffer[0])
  644. {
  645. case CHAR_INV:
  646. pSmartcardExtension->ReaderExtension->fRawModeNecessary = FALSE;
  647. SCCMN50M_SetCardControlFlags(pSmartcardExtension,INVERSE_DATA);
  648. SCCMN50M_InverseBuffer(pbAtrBuffer,*pulAtrLength);
  649. pSmartcardExtension->ReaderExtension->fInverseAtr = TRUE;
  650. break;
  651. case CHAR_NORM:
  652. pSmartcardExtension->ReaderExtension->fRawModeNecessary = FALSE;
  653. pSmartcardExtension->ReaderExtension->fInverseAtr = FALSE;
  654. break;
  655. default :
  656. status = STATUS_UNRECOGNIZED_MEDIA;
  657. goto ExitUseColdWarmResetStrategy;
  658. break;
  659. }
  660. // the smart card has been powered
  661. SCCMN50M_CheckAtrModified(pbAtrBuffer,*pulAtrLength);
  662. MemCpy(pSmartcardExtension->CardCapabilities.ATR.Buffer,
  663. sizeof(pSmartcardExtension->CardCapabilities.ATR.Buffer),
  664. pbAtrBuffer,
  665. *pulAtrLength);
  666. pSmartcardExtension->CardCapabilities.ATR.Length = (UCHAR)*pulAtrLength;
  667. status = SmartcardUpdateCardCapabilities(pSmartcardExtension);
  668. if (status != STATUS_SUCCESS)
  669. {
  670. SmartcardDebug(DEBUG_ATR,
  671. ("%s!Invalid ATR received\n",
  672. DRIVER_NAME));
  673. goto ExitUseColdWarmResetStrategy;
  674. }
  675. if (SCCMN50M_IsAtrValid(pbAtrBuffer,*pulAtrLength) == FALSE)
  676. {
  677. SmartcardDebug(
  678. DEBUG_ATR,
  679. ("%s!Invalid ATR received\n",
  680. DRIVER_NAME));
  681. status = STATUS_UNRECOGNIZED_MEDIA;
  682. goto ExitUseColdWarmResetStrategy;
  683. }
  684. break;
  685. } // end for
  686. ExitUseColdWarmResetStrategy:
  687. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier
  688. = ulOldReadTotalTimeoutMultiplier ;
  689. SCCMN50M_ClearSCRControlFlags(pSmartcardExtension,CM2_GET_ATR | IGNORE_PARITY);
  690. SCCMN50M_ClearCardManHeader(pSmartcardExtension);
  691. return status;
  692. }
  693. /*****************************************************************************
  694. Routine Description:
  695. This function checks if the received ATR is valid.
  696. Arguments:
  697. Return Value:
  698. *****************************************************************************/
  699. BOOLEAN
  700. SCCMN50M_IsAtrValid(
  701. PUCHAR pbAtrBuffer,
  702. ULONG ulAtrLength
  703. )
  704. {
  705. BOOLEAN fAtrValid = TRUE;
  706. ULONG ulTD1Offset = 0;
  707. BOOLEAN fTD1Transmitted = FALSE;
  708. BOOLEAN fOnlyT0 = FALSE;
  709. BYTE bXor;
  710. ULONG ulHistoricalBytes;
  711. ULONG ulTx2Characters = 0;
  712. ULONG i;
  713. //DBGBreakPoint();
  714. SmartcardDebug(
  715. DEBUG_TRACE,
  716. ( "%s!IsAtrValid : Enter\n",
  717. DRIVER_NAME)
  718. );
  719. // basic checks
  720. if (ulAtrLength < 2 ||
  721. (pbAtrBuffer[0] != 0x3F &&
  722. pbAtrBuffer[0] != 0x3B ) ||
  723. (pbAtrBuffer[1] & 0xF0) == 0x00 )
  724. {
  725. return FALSE;
  726. }
  727. if (pbAtrBuffer[1] & 0x10)
  728. ulTD1Offset++;
  729. if (pbAtrBuffer[1] & 0x20)
  730. ulTD1Offset++;
  731. if (pbAtrBuffer[1] & 0x40)
  732. ulTD1Offset++;
  733. ulHistoricalBytes = pbAtrBuffer[1] & 0x0F;
  734. if (pbAtrBuffer[1] & 0x80) // TD1 in ATR ?
  735. {
  736. fTD1Transmitted = TRUE;
  737. if ((pbAtrBuffer[2 + ulTD1Offset] & 0x0F) == 0x00) // T0 indicated ?
  738. fOnlyT0 = TRUE;
  739. }
  740. else
  741. {
  742. fOnlyT0 = TRUE;
  743. }
  744. if (fOnlyT0 == FALSE)
  745. {
  746. bXor = pbAtrBuffer[1];
  747. for (i=2;i<ulAtrLength;i++)
  748. bXor ^= pbAtrBuffer[i];
  749. if (bXor != 0x00)
  750. fAtrValid = FALSE;
  751. }
  752. else
  753. {
  754. // only T0 protocol is indicated
  755. if (fTD1Transmitted == TRUE)
  756. {
  757. if (pbAtrBuffer[2 + ulTD1Offset] & 0x10)
  758. ulTx2Characters++;
  759. if (pbAtrBuffer[2 + ulTD1Offset] & 0x20)
  760. ulTx2Characters++;
  761. if (pbAtrBuffer[2 + ulTD1Offset] & 0x40)
  762. ulTx2Characters++;
  763. if (ulAtrLength != 2 + ulTD1Offset + 1 + ulTx2Characters + ulHistoricalBytes)
  764. fAtrValid = FALSE;
  765. }
  766. else
  767. {
  768. if (ulAtrLength != 2 + ulTD1Offset + ulHistoricalBytes)
  769. fAtrValid = FALSE;
  770. }
  771. }
  772. SmartcardDebug(
  773. DEBUG_TRACE,
  774. ( "%s!IsAtrValid : Exit %d\n",
  775. DRIVER_NAME,fAtrValid)
  776. );
  777. return fAtrValid;
  778. }
  779. /*****************************************************************************
  780. Routine Description:
  781. This function checks if the received ATR is valid.
  782. Arguments:
  783. Return Value:
  784. *****************************************************************************/
  785. VOID SCCMN50M_CheckAtrModified (
  786. PUCHAR pbBuffer,
  787. ULONG ulBufferSize
  788. )
  789. {
  790. UCHAR bNumberHistoricalBytes;
  791. UCHAR bXorChecksum;
  792. ULONG i;
  793. if (ulBufferSize < 0x09) // mininmum length of a modified ATR
  794. return ; // ATR is ok
  795. // variant 2
  796. if (pbBuffer[0] == 0x3b &&
  797. pbBuffer[1] == 0xbf &&
  798. pbBuffer[2] == 0x11 &&
  799. pbBuffer[3] == 0x00 &&
  800. pbBuffer[4] == 0x81 &&
  801. pbBuffer[5] == 0x31 &&
  802. pbBuffer[6] == 0x90 &&
  803. pbBuffer[7] == 0x73 &&
  804. ulBufferSize == 13 )
  805. {
  806. // correct number of historical bytes
  807. bNumberHistoricalBytes = 4;
  808. pbBuffer[1] &= 0xf0;
  809. pbBuffer[1] |= bNumberHistoricalBytes;
  810. // correct checksum byte
  811. bXorChecksum = pbBuffer[1];
  812. for (i=2;i<ulBufferSize-1;i++)
  813. bXorChecksum ^= pbBuffer[i];
  814. pbBuffer[ulBufferSize -1 ] = bXorChecksum;
  815. SmartcardDebug(DEBUG_ATR,
  816. ("%s!correcting SAMOS ATR (variant 2)\n",
  817. DRIVER_NAME));
  818. }
  819. // variant 1
  820. if (pbBuffer[0] == 0x3b &&
  821. pbBuffer[1] == 0xb4 &&
  822. pbBuffer[2] == 0x11 &&
  823. pbBuffer[3] == 0x00 &&
  824. pbBuffer[4] == 0x81 &&
  825. pbBuffer[5] == 0x31 &&
  826. pbBuffer[6] == 0x90 &&
  827. pbBuffer[7] == 0x73 &&
  828. ulBufferSize == 13 )
  829. {
  830. // correct checksum byte
  831. bXorChecksum = pbBuffer[1];
  832. for (i=2;i<ulBufferSize-1;i++)
  833. bXorChecksum ^= pbBuffer[i];
  834. if (pbBuffer[ulBufferSize -1 ] != bXorChecksum )
  835. {
  836. pbBuffer[ulBufferSize -1 ] = bXorChecksum;
  837. SmartcardDebug(DEBUG_ATR,
  838. ("%s!correcting SAMOS ATR (variant 1)\n",
  839. DRIVER_NAME));
  840. }
  841. }
  842. // variant 3
  843. if (pbBuffer[0] == 0x3b &&
  844. pbBuffer[1] == 0xbf &&
  845. pbBuffer[2] == 0x11 &&
  846. pbBuffer[3] == 0x00 &&
  847. pbBuffer[4] == 0x81 &&
  848. pbBuffer[5] == 0x31 &&
  849. pbBuffer[6] == 0x90 &&
  850. pbBuffer[7] == 0x73 &&
  851. ulBufferSize == 9 )
  852. {
  853. // correct number of historical bytes
  854. bNumberHistoricalBytes = 0;
  855. pbBuffer[1] &= 0xf0;
  856. pbBuffer[1] |= bNumberHistoricalBytes;
  857. // correct checksum byte
  858. bXorChecksum = pbBuffer[1];
  859. for (i=2;i<ulBufferSize-1;i++)
  860. bXorChecksum ^= pbBuffer[i];
  861. pbBuffer[ulBufferSize -1 ] = bXorChecksum;
  862. SmartcardDebug(DEBUG_ATR,
  863. ("%s!correcting SAMOS ATR (variant 3)\n",
  864. DRIVER_NAME));
  865. }
  866. }
  867. /*****************************************************************************
  868. Routine Description:
  869. Arguments:
  870. Return Value:
  871. *****************************************************************************/
  872. NTSTATUS
  873. SCCMN50M_PowerOff (IN PSMARTCARD_EXTENSION pSmartcardExtension )
  874. {
  875. NTSTATUS status;
  876. NTSTATUS DebugStatus = STATUS_SUCCESS;
  877. UCHAR pReadBuffer[2];
  878. ULONG ulBytesRead;
  879. SmartcardDebug(
  880. DEBUG_TRACE,
  881. ( "%s!PowerOff: Enter\n",
  882. DRIVER_NAME)
  883. );
  884. // SCR control bytes
  885. SCCMN50M_ClearSCRControlFlags(pSmartcardExtension,CARD_POWER);
  886. // card control bytes
  887. SCCMN50M_ClearCardControlFlags(pSmartcardExtension,ALL_FLAGS);
  888. // header
  889. SCCMN50M_SetCardManHeader(pSmartcardExtension,0,0,0,1);
  890. // rx length = 1 because we don't want to receive a status
  891. // write config + header
  892. status = SCCMN50M_WriteCardMan(pSmartcardExtension,0,NULL);
  893. if (status != STATUS_SUCCESS)
  894. {
  895. goto ExitSCCMN50M_PowerOff;
  896. }
  897. // CardMan echoes a BRK which is recevied in the read functions
  898. DebugStatus = SCCMN50M_ReadCardMan(pSmartcardExtension,2,&ulBytesRead,pReadBuffer,sizeof(pReadBuffer));
  899. #if 0
  900. if (DebugStatus != STATUS_SUCCESS)
  901. SmartcardDebug(
  902. DEBUG_ERROR,
  903. ( "%s!PowerOffBRK received\n",
  904. DRIVER_NAME)
  905. );
  906. #endif
  907. ExitSCCMN50M_PowerOff:
  908. if (pSmartcardExtension->ReaderExtension->ulOldCardState == POWERED)
  909. pSmartcardExtension->ReaderExtension->ulOldCardState = INSERTED;
  910. SmartcardDebug(
  911. DEBUG_TRACE,
  912. ( "%s!PowerOff: Exit %lx\n",
  913. DRIVER_NAME,status)
  914. );
  915. return status;
  916. }
  917. /*****************************************************************************
  918. Routine Description:
  919. Arguments:
  920. Return Value:
  921. *****************************************************************************/
  922. NTSTATUS
  923. SCCMN50M_Transmit(PSMARTCARD_EXTENSION pSmartcardExtension)
  924. {
  925. NTSTATUS status;
  926. NTSTATUS DebugStatus;
  927. switch (pSmartcardExtension->CardCapabilities.Protocol.Selected)
  928. {
  929. case SCARD_PROTOCOL_RAW:
  930. status = STATUS_INVALID_DEVICE_REQUEST;
  931. break;
  932. case SCARD_PROTOCOL_T0:
  933. status = SCCMN50M_TransmitT0(pSmartcardExtension);
  934. break;
  935. case SCARD_PROTOCOL_T1:
  936. status = SCCMN50M_TransmitT1(pSmartcardExtension);
  937. break;
  938. default:
  939. status = STATUS_INVALID_DEVICE_REQUEST;
  940. break;
  941. }
  942. return status;
  943. }
  944. /*****************************************************************************
  945. Routine Description:
  946. Arguments:
  947. Return Value:
  948. *****************************************************************************/
  949. NTSTATUS
  950. SCCMN50M_TransmitT0(PSMARTCARD_EXTENSION pSmartcardExtension)
  951. {
  952. NTSTATUS status;
  953. NTSTATUS DebugStatus;
  954. UCHAR bWriteBuffer[MIN_BUFFER_SIZE];
  955. UCHAR bReadBuffer [MIN_BUFFER_SIZE];
  956. ULONG ulWriteBufferOffset;
  957. ULONG ulReadBufferOffset;
  958. ULONG ulBytesToWrite;
  959. ULONG ulBytesToRead;
  960. ULONG ulBytesToWriteThisStep;
  961. ULONG ulBytesToReadThisStep;
  962. ULONG ulBytesStillToWrite;
  963. ULONG ulBytesRead;
  964. ULONG ulBytesStillToRead;
  965. BOOLEAN fDataDirectionFromCard;
  966. BYTE bProcedureByte;
  967. BYTE bINS;
  968. BOOLEAN fT0TransferToCard = FALSE;
  969. BOOLEAN fT0TransferFromCard = FALSE;
  970. BOOLEAN fSW1SW2Sent = FALSE;
  971. ULONG ulReadTotalTimeoutMultiplier;
  972. ULONG ulStatBytesRead;
  973. BYTE abStatReadBuffer[2];
  974. //SmartcardDebug(DEBUG_TRACE,("TransmitT0 : Enter\n"));
  975. //
  976. // Let the lib build a T=0 packet
  977. //
  978. pSmartcardExtension->SmartcardRequest.BufferLength = 0; // no bytes additionally needed
  979. status = SmartcardT0Request(pSmartcardExtension);
  980. if (status != STATUS_SUCCESS)
  981. {
  982. //
  983. // This lib detected an error in the data to send.
  984. //
  985. // ------------------------------------------
  986. // ITSEC E2 requirements: clear write buffers
  987. // ------------------------------------------
  988. MemSet(bWriteBuffer,
  989. sizeof(bWriteBuffer),
  990. '\0',
  991. sizeof(bWriteBuffer));
  992. MemSet(pSmartcardExtension->SmartcardRequest.Buffer,
  993. pSmartcardExtension->SmartcardRequest.BufferSize,
  994. '\0',
  995. pSmartcardExtension->SmartcardRequest.BufferSize);
  996. return status;
  997. }
  998. // increase timeout for T0 Transmission
  999. ulReadTotalTimeoutMultiplier = pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier;
  1000. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier =
  1001. pSmartcardExtension->CardCapabilities.T0.WT/1000 + 1500;
  1002. // ##################################
  1003. // TRANSPARENT MODE
  1004. // ##################################
  1005. ulBytesStillToWrite = ulBytesToWrite = T0_HEADER_LEN + pSmartcardExtension->T0.Lc;
  1006. ulBytesStillToRead = ulBytesToRead = pSmartcardExtension->T0.Le;
  1007. if (pSmartcardExtension->T0.Lc)
  1008. fT0TransferToCard = TRUE;
  1009. if (pSmartcardExtension->T0.Le)
  1010. fT0TransferFromCard = TRUE;
  1011. // copy data to the write buffer
  1012. /*
  1013. SmartcardDebug(DEBUG_TRACE,("CLA = %x INS = %x P1 = %x P2 = %X L = %x\n",
  1014. pSmartcardExtension->SmartcardRequest.Buffer[0],
  1015. pSmartcardExtension->SmartcardRequest.Buffer[1],
  1016. pSmartcardExtension->SmartcardRequest.Buffer[2],
  1017. pSmartcardExtension->SmartcardRequest.Buffer[3],
  1018. pSmartcardExtension->SmartcardRequest.Buffer[4]));
  1019. */
  1020. MemCpy(bWriteBuffer,
  1021. sizeof(bWriteBuffer),
  1022. pSmartcardExtension->SmartcardRequest.Buffer,
  1023. ulBytesToWrite);
  1024. bINS = bWriteBuffer[1];
  1025. if (pSmartcardExtension->ReaderExtension->fInverseAtr == TRUE)
  1026. {
  1027. SCCMN50M_InverseBuffer(bWriteBuffer,ulBytesToWrite);
  1028. }
  1029. status = SCCMN50M_EnterTransparentMode(pSmartcardExtension);
  1030. if (status != STATUS_SUCCESS)
  1031. {
  1032. goto ExitTransparentTransmitT0;
  1033. }
  1034. // STEP 1 : write config + header to enter transparent mode
  1035. SCCMN50M_SetCardManHeader(pSmartcardExtension,
  1036. 0, // Tx control
  1037. 0, // Tx length
  1038. 0, // Rx control
  1039. 0); // Rx length
  1040. status = SCCMN50M_WriteCardMan (pSmartcardExtension,
  1041. 0,
  1042. NULL);
  1043. if (NT_ERROR(status))
  1044. {
  1045. goto ExitTransparentTransmitT0;
  1046. }
  1047. pSmartcardExtension->ReaderExtension->fTransparentMode = TRUE;
  1048. // if the inserted card uses inverse convention , we must now switch the COM port
  1049. // to odd parity
  1050. if (pSmartcardExtension->ReaderExtension->fInverseAtr == TRUE)
  1051. {
  1052. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.StopBits = STOP_BITS_2;
  1053. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.Parity = ODD_PARITY;
  1054. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.WordLength = SERIAL_DATABITS_8;
  1055. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_LINE_CONTROL;
  1056. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  1057. &pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl,
  1058. sizeof(SERIAL_LINE_CONTROL));
  1059. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_LINE_CONTROL);
  1060. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  1061. status = SCCMN50M_SerialIo(pSmartcardExtension);
  1062. if (!NT_SUCCESS(status))
  1063. {
  1064. goto ExitTransparentTransmitT0;
  1065. }
  1066. }
  1067. ulWriteBufferOffset = 0;
  1068. ulReadBufferOffset = 0;
  1069. // STEP 2 : write CLA INS P1 P2 Lc
  1070. ulBytesToWriteThisStep = 5;
  1071. status = SCCMN50M_WriteCardMan (pSmartcardExtension,
  1072. ulBytesToWriteThisStep,
  1073. bWriteBuffer+ulWriteBufferOffset);
  1074. if (NT_ERROR(status))
  1075. {
  1076. goto ExitTransparentTransmitT0;
  1077. }
  1078. ulWriteBufferOffset += ulBytesToWriteThisStep;
  1079. ulBytesStillToWrite -= ulBytesToWriteThisStep;
  1080. // STEP 2 : read procedure byte
  1081. do
  1082. {
  1083. do
  1084. {
  1085. pSmartcardExtension->ReaderExtension->ToRHConfig= FALSE;
  1086. status = SCCMN50M_ReadCardMan(pSmartcardExtension,1,&ulBytesRead,&bProcedureByte,sizeof(bProcedureByte));
  1087. if (NT_ERROR(status))
  1088. {
  1089. goto ExitTransparentTransmitT0;
  1090. }
  1091. if (pSmartcardExtension->ReaderExtension->fInverseAtr == TRUE)
  1092. {
  1093. SCCMN50M_InverseBuffer(&bProcedureByte,ulBytesRead);
  1094. }
  1095. //SmartcardDebug(DEBUG_TRACE,("Procedure byte = %x\n",bProcedureByte));
  1096. //SmartcardDebug(DEBUG_TRACE,("waiting time = %x\n",pSmartcardExtension->CardCapabilities.T0.WT));
  1097. if (bProcedureByte == 0x60)
  1098. {
  1099. // ISO 7816-3 :
  1100. // This byte is sent by the card to reset the work waiting time and to anticipate
  1101. // a subsequent procedure byte
  1102. // => we do nothing here
  1103. }
  1104. } while (bProcedureByte == 0x60);
  1105. // check for ACK
  1106. if ((bProcedureByte & 0xFE) == (bINS & 0xFE) )
  1107. {
  1108. if (fT0TransferToCard)
  1109. {
  1110. ulBytesToWriteThisStep = ulBytesStillToWrite;
  1111. status = SCCMN50M_WriteCardMan (pSmartcardExtension,
  1112. ulBytesToWriteThisStep,
  1113. bWriteBuffer+ulWriteBufferOffset);
  1114. if (NT_ERROR(status))
  1115. {
  1116. goto ExitTransparentTransmitT0;
  1117. }
  1118. ulWriteBufferOffset += ulBytesToWriteThisStep;
  1119. ulBytesStillToWrite -= ulBytesToWriteThisStep;
  1120. }
  1121. if (fT0TransferFromCard)
  1122. {
  1123. ulBytesToReadThisStep = ulBytesStillToRead;
  1124. pSmartcardExtension->ReaderExtension->ToRHConfig= FALSE;
  1125. status = SCCMN50M_ReadCardMan(pSmartcardExtension,ulBytesToReadThisStep,&ulBytesRead,bReadBuffer + ulReadBufferOffset,sizeof(bReadBuffer)-ulReadBufferOffset);
  1126. if (NT_ERROR(status))
  1127. {
  1128. goto ExitTransparentTransmitT0;
  1129. }
  1130. if (pSmartcardExtension->ReaderExtension->fInverseAtr == TRUE)
  1131. {
  1132. SCCMN50M_InverseBuffer(bReadBuffer+ulReadBufferOffset,ulBytesRead);
  1133. }
  1134. ulReadBufferOffset += ulBytesRead;
  1135. ulBytesStillToRead -= ulBytesRead;
  1136. }
  1137. }
  1138. // check for NAK
  1139. else if ( (~bProcedureByte & 0xFE) == (bINS & 0xFE))
  1140. {
  1141. if (fT0TransferToCard)
  1142. {
  1143. ulBytesToWriteThisStep = 1;
  1144. status = SCCMN50M_WriteCardMan (pSmartcardExtension,
  1145. ulBytesToWriteThisStep,
  1146. bWriteBuffer+ulWriteBufferOffset);
  1147. if (NT_ERROR(status))
  1148. {
  1149. goto ExitTransparentTransmitT0;
  1150. }
  1151. ulWriteBufferOffset += ulBytesToWriteThisStep;
  1152. ulBytesStillToWrite -= ulBytesToWriteThisStep;
  1153. }
  1154. if (fT0TransferFromCard)
  1155. {
  1156. ulBytesToReadThisStep = 1;
  1157. pSmartcardExtension->ReaderExtension->ToRHConfig= FALSE;
  1158. status = SCCMN50M_ReadCardMan(pSmartcardExtension,ulBytesToReadThisStep,&ulBytesRead,bReadBuffer + ulReadBufferOffset,sizeof(bReadBuffer)-ulReadBufferOffset);
  1159. if (NT_ERROR(status))
  1160. {
  1161. goto ExitTransparentTransmitT0;
  1162. }
  1163. if (pSmartcardExtension->ReaderExtension->fInverseAtr == TRUE)
  1164. {
  1165. SCCMN50M_InverseBuffer(bReadBuffer+ulReadBufferOffset,ulBytesRead);
  1166. }
  1167. ulReadBufferOffset += ulBytesRead;
  1168. ulBytesStillToRead -= ulBytesRead;
  1169. }
  1170. }
  1171. // check for SW1
  1172. else if ( (bProcedureByte > 0x60 && bProcedureByte <= 0x6F) ||
  1173. (bProcedureByte >= 0x90 && bProcedureByte <= 0x9F) )
  1174. {
  1175. pSmartcardExtension->ReaderExtension->ToRHConfig= FALSE;
  1176. bReadBuffer[ulReadBufferOffset] = bProcedureByte;
  1177. ulReadBufferOffset++;
  1178. status = SCCMN50M_ReadCardMan(pSmartcardExtension,1,&ulBytesRead,bReadBuffer+ulReadBufferOffset,sizeof(bReadBuffer)-ulReadBufferOffset);
  1179. if (NT_ERROR(status))
  1180. {
  1181. goto ExitTransparentTransmitT0;
  1182. }
  1183. if (pSmartcardExtension->ReaderExtension->fInverseAtr == TRUE)
  1184. {
  1185. SCCMN50M_InverseBuffer(bReadBuffer+ulReadBufferOffset,ulBytesRead);
  1186. }
  1187. ulReadBufferOffset += ulBytesRead;
  1188. fSW1SW2Sent = TRUE;
  1189. }
  1190. else
  1191. {
  1192. status = STATUS_UNSUCCESSFUL;
  1193. goto ExitTransparentTransmitT0;
  1194. }
  1195. }while (!fSW1SW2Sent);
  1196. // copy received bytes
  1197. MemCpy(pSmartcardExtension->SmartcardReply.Buffer,
  1198. pSmartcardExtension->SmartcardReply.BufferSize,
  1199. bReadBuffer,
  1200. ulReadBufferOffset);
  1201. pSmartcardExtension->SmartcardReply.BufferLength = ulReadBufferOffset;
  1202. // let the lib copy the received bytes to the user buffer
  1203. status = SmartcardT0Reply(pSmartcardExtension);
  1204. if (NT_ERROR(status))
  1205. {
  1206. goto ExitTransparentTransmitT0;
  1207. }
  1208. ExitTransparentTransmitT0:
  1209. // ------------------------------------------
  1210. // ITSEC E2 requirements: clear write buffers
  1211. // ------------------------------------------
  1212. MemSet(bWriteBuffer,
  1213. sizeof(bWriteBuffer),
  1214. '\0',
  1215. sizeof(bWriteBuffer));
  1216. MemSet(pSmartcardExtension->SmartcardRequest.Buffer,
  1217. pSmartcardExtension->SmartcardRequest.BufferSize,
  1218. '\0',
  1219. pSmartcardExtension->SmartcardRequest.BufferSize);
  1220. DebugStatus = SCCMN50M_ExitTransparentMode(pSmartcardExtension);
  1221. pSmartcardExtension->ReaderExtension->fTransparentMode = FALSE;
  1222. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier = ulReadTotalTimeoutMultiplier;
  1223. // to be sure that the new settings take effect
  1224. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant = 250;
  1225. DebugStatus = SCCMN50M_WriteCardMan(pSmartcardExtension,0,NULL);
  1226. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant = DEFAULT_READ_TOTAL_TIMEOUT_CONSTANT;
  1227. if (NT_SUCCESS(DebugStatus))
  1228. {
  1229. DebugStatus = SCCMN50M_ReadCardMan(pSmartcardExtension,2,&ulStatBytesRead,abStatReadBuffer,sizeof(abStatReadBuffer));
  1230. }
  1231. return status;
  1232. }
  1233. /*****************************************************************************
  1234. Routine Description:
  1235. Arguments:
  1236. Return Value:
  1237. *****************************************************************************/
  1238. NTSTATUS
  1239. SCCMN50M_TransmitT1(PSMARTCARD_EXTENSION pSmartcardExtension)
  1240. {
  1241. NTSTATUS status;
  1242. ULONG ulBytesToWrite;
  1243. UCHAR bWriteBuffer [256 + T1_HEADER_LEN + MAX_EDC_LEN];
  1244. UCHAR bReadBuffer [256 + T1_HEADER_LEN + MAX_EDC_LEN];
  1245. ULONG ulBytesRead;
  1246. ULONG ulBytesStillToRead;
  1247. /*
  1248. SmartcardDebug(
  1249. DEBUG_TRACE,
  1250. ("%s!CWT = %ld(ms)\n",
  1251. DRIVER_NAME,
  1252. pSmartcardExtension->CardCapabilities.T1.CWT/1000)
  1253. );
  1254. SmartcardDebug(
  1255. DEBUG_TRACE,
  1256. ("%s!BWT = %ld(ms)\n",
  1257. DRIVER_NAME,
  1258. pSmartcardExtension->CardCapabilities.T1.BWT/1000);
  1259. );
  1260. */
  1261. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant =
  1262. pSmartcardExtension->CardCapabilities.T1.BWT/1000;
  1263. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier =
  1264. pSmartcardExtension->CardCapabilities.T1.CWT/1000;
  1265. /*
  1266. SmartcardDebug(DEBUG_TRACE,("%s!ReadTotalTimeoutConstant = %ld(ms)\n",
  1267. DRIVER_NAME,
  1268. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant));
  1269. SmartcardDebug(DEBUG_TRACE,("%s!ReadTotalTimeoutMultiplier = %ld(ms)\n",
  1270. DRIVER_NAME,
  1271. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier));
  1272. */
  1273. // set T1 protocol flag for CardMan
  1274. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_T1);
  1275. if (pSmartcardExtension->CardCapabilities.T1.EDC == T1_CRC_CHECK)
  1276. {
  1277. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_CRC);
  1278. }
  1279. do
  1280. {
  1281. pSmartcardExtension->SmartcardRequest.BufferLength = 0; // no bytes additionally needed
  1282. status = SmartcardT1Request(pSmartcardExtension);
  1283. if (status != STATUS_SUCCESS)
  1284. {
  1285. goto ExitTransmitT1;
  1286. }
  1287. ulBytesToWrite = pSmartcardExtension->SmartcardRequest.BufferLength;
  1288. SCCMN50M_SetCardManHeader(pSmartcardExtension,
  1289. 0, // Tx conrol
  1290. (UCHAR)ulBytesToWrite, // Tx length
  1291. 0, // Rx control
  1292. T1_HEADER_LEN); // Rx length
  1293. if (sizeof(bWriteBuffer) < ulBytesToWrite)
  1294. {
  1295. status = STATUS_BUFFER_OVERFLOW;
  1296. goto ExitTransmitT1;
  1297. }
  1298. // copy data to the write buffer
  1299. MemCpy(bWriteBuffer,
  1300. sizeof(bWriteBuffer),
  1301. pSmartcardExtension->SmartcardRequest.Buffer,
  1302. ulBytesToWrite);
  1303. // write data to card
  1304. status = SCCMN50M_WriteCardMan (pSmartcardExtension,
  1305. ulBytesToWrite,
  1306. bWriteBuffer);
  1307. if (status == STATUS_SUCCESS)
  1308. {
  1309. // read CardMan Header
  1310. pSmartcardExtension->ReaderExtension->ToRHConfig = FALSE;
  1311. status = SCCMN50M_ReadCardMan(pSmartcardExtension,2,&ulBytesRead,bReadBuffer,sizeof(bReadBuffer));
  1312. if (status == STATUS_SUCCESS)
  1313. {
  1314. ulBytesStillToRead = bReadBuffer[1];
  1315. status = SCCMN50M_ReadCardMan(pSmartcardExtension,
  1316. ulBytesStillToRead,
  1317. &ulBytesRead,
  1318. bReadBuffer,
  1319. sizeof(bReadBuffer));
  1320. if (status == STATUS_SUCCESS)
  1321. {
  1322. if (bReadBuffer[1] == T1_WTX_REQUEST)
  1323. {
  1324. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant =
  1325. (ULONG)(1000 +((pSmartcardExtension->CardCapabilities.T1.BWT*bReadBuffer[3])/1000));
  1326. SmartcardDebug(DEBUG_PROTOCOL,("%s!ReadTotalTimeoutConstant = %ld(ms)\n",
  1327. DRIVER_NAME,
  1328. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant));
  1329. }
  1330. else
  1331. {
  1332. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant =
  1333. pSmartcardExtension->CardCapabilities.T1.BWT/1000;
  1334. SmartcardDebug(DEBUG_PROTOCOL,("%s!ReadTotalTimeoutConstant = %ld(ms)\n",
  1335. DRIVER_NAME,
  1336. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant));
  1337. }
  1338. // copy received bytes
  1339. MemCpy(pSmartcardExtension->SmartcardReply.Buffer,
  1340. pSmartcardExtension->SmartcardReply.BufferSize,
  1341. bReadBuffer,
  1342. ulBytesRead);
  1343. pSmartcardExtension->SmartcardReply.BufferLength = ulBytesRead;
  1344. }
  1345. }
  1346. }
  1347. if (status != STATUS_SUCCESS)
  1348. {
  1349. // reset serial timeout
  1350. SmartcardDebug(
  1351. DEBUG_PROTOCOL,
  1352. ( "%s!reseting timeout constant\n",
  1353. DRIVER_NAME)
  1354. );
  1355. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant =
  1356. pSmartcardExtension->CardCapabilities.T1.BWT/1000;
  1357. SmartcardDebug(DEBUG_PROTOCOL,("%s!ReadTotalTimeoutConstant = %ld(ms)\n",
  1358. DRIVER_NAME,
  1359. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant));
  1360. pSmartcardExtension->SmartcardReply.BufferLength = 0L;
  1361. }
  1362. // bug fix for smclib
  1363. if (pSmartcardExtension->T1.State == T1_IFS_RESPONSE &&
  1364. pSmartcardExtension->T1.OriginalState == T1_I_BLOCK)
  1365. {
  1366. pSmartcardExtension->T1.State = T1_I_BLOCK;
  1367. }
  1368. status = SmartcardT1Reply(pSmartcardExtension);
  1369. }
  1370. while (status == STATUS_MORE_PROCESSING_REQUIRED);
  1371. ExitTransmitT1:
  1372. // ------------------------------------------
  1373. // ITSEC E2 requirements: clear write buffers
  1374. // ------------------------------------------
  1375. MemSet(bWriteBuffer,
  1376. sizeof(bWriteBuffer),
  1377. '\0',
  1378. sizeof(bWriteBuffer));
  1379. MemSet(pSmartcardExtension->SmartcardRequest.Buffer,
  1380. pSmartcardExtension->SmartcardRequest.BufferSize,
  1381. '\0',
  1382. pSmartcardExtension->SmartcardRequest.BufferSize);
  1383. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier =
  1384. DEFAULT_READ_TOTAL_TIMEOUT_MULTIPLIER;
  1385. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant =
  1386. DEFAULT_READ_TOTAL_TIMEOUT_CONSTANT;
  1387. return status;
  1388. }
  1389. /*****************************************************************************
  1390. Routine Description:
  1391. Arguments:
  1392. Return Value:
  1393. *****************************************************************************/
  1394. VOID
  1395. SCCMN50M_InitializeSmartcardExtension(
  1396. IN PSMARTCARD_EXTENSION pSmartcardExtension,
  1397. IN ULONG ulDeviceInstance
  1398. )
  1399. {
  1400. // ==================================
  1401. // Fill the Vendor_Attr structure
  1402. // ==================================
  1403. MemCpy(pSmartcardExtension->VendorAttr.VendorName.Buffer,
  1404. sizeof(pSmartcardExtension->VendorAttr.VendorName.Buffer),
  1405. ATTR_VENDOR_NAME,
  1406. sizeof(ATTR_VENDOR_NAME)
  1407. );
  1408. //
  1409. // Length of vendor name
  1410. //
  1411. pSmartcardExtension->VendorAttr.VendorName.Length = sizeof(ATTR_VENDOR_NAME);
  1412. //
  1413. // Version number
  1414. //
  1415. pSmartcardExtension->VendorAttr.IfdVersion.BuildNumber = IFD_NT_BUILDNUMBER_CARDMAN;
  1416. pSmartcardExtension->VendorAttr.IfdVersion.VersionMinor = IFD_NT_VERSIONMINOR_CARDMAN;
  1417. pSmartcardExtension->VendorAttr.IfdVersion.VersionMajor = IFD_NT_VERSIONMAJOR_CARDMAN;
  1418. MemCpy(pSmartcardExtension->VendorAttr.IfdType.Buffer,
  1419. sizeof(pSmartcardExtension->VendorAttr.IfdType.Buffer),
  1420. ATTR_IFD_TYPE_CM,
  1421. sizeof(ATTR_IFD_TYPE_CM));
  1422. //
  1423. // Length of reader name
  1424. //
  1425. pSmartcardExtension->VendorAttr.IfdType.Length = sizeof(ATTR_IFD_TYPE_CM);
  1426. //
  1427. // Unit number which is zero based
  1428. //
  1429. pSmartcardExtension->VendorAttr.UnitNo = ulDeviceInstance;
  1430. // ================================================
  1431. // Fill the SCARD_READER_CAPABILITIES structure
  1432. // ===============================================
  1433. //
  1434. // Supported protoclols by the reader
  1435. //
  1436. pSmartcardExtension->ReaderCapabilities.SupportedProtocols = SCARD_PROTOCOL_T1 | SCARD_PROTOCOL_T0;
  1437. //
  1438. // Reader type serial, keyboard, ....
  1439. //
  1440. pSmartcardExtension->ReaderCapabilities.ReaderType = SCARD_READER_TYPE_SERIAL;
  1441. //
  1442. // Mechanical characteristics like swallows etc.
  1443. //
  1444. pSmartcardExtension->ReaderCapabilities.MechProperties = 0;
  1445. //
  1446. // Current state of the reader
  1447. //
  1448. pSmartcardExtension->ReaderCapabilities.CurrentState = SCARD_UNKNOWN;
  1449. //
  1450. // Data Rate
  1451. //
  1452. pSmartcardExtension->ReaderCapabilities.DataRate.Default =
  1453. pSmartcardExtension->ReaderCapabilities.DataRate.Max =
  1454. dataRatesSupported[0];
  1455. // reader could support higher data rates
  1456. pSmartcardExtension->ReaderCapabilities.DataRatesSupported.List =
  1457. dataRatesSupported;
  1458. pSmartcardExtension->ReaderCapabilities.DataRatesSupported.Entries =
  1459. sizeof(dataRatesSupported) / sizeof(dataRatesSupported[0]);
  1460. //
  1461. // CLKFrequency
  1462. //
  1463. pSmartcardExtension->ReaderCapabilities.CLKFrequency.Default =
  1464. pSmartcardExtension->ReaderCapabilities.CLKFrequency.Max =
  1465. CLKFrequenciesSupported[0];
  1466. pSmartcardExtension->ReaderCapabilities.CLKFrequenciesSupported.List =
  1467. CLKFrequenciesSupported;
  1468. pSmartcardExtension->ReaderCapabilities.CLKFrequenciesSupported.Entries =
  1469. sizeof(CLKFrequenciesSupported) / sizeof(CLKFrequenciesSupported[0]);
  1470. //pSmartcardExtension->ReaderCapabilities.CLKFrequency.Default = 3571; //3.571 MHz
  1471. //pSmartcardExtension->ReaderCapabilities.CLKFrequency.Max = 3571; //3.571 MHz
  1472. //
  1473. // MaxIFSD
  1474. //
  1475. pSmartcardExtension->ReaderCapabilities.MaxIFSD = ATTR_MAX_IFSD_CARDMAN_II;
  1476. }
  1477. /*****************************************************************************
  1478. Routine Description:
  1479. Arguments:
  1480. Return Value:
  1481. *****************************************************************************/
  1482. VOID
  1483. StrSet(PUCHAR Buffer,ULONG BufferSize,UCHAR Pattern)
  1484. {
  1485. ULONG i;
  1486. for (i=0;i < BufferSize;i++)
  1487. Buffer[i] = Pattern;
  1488. return;
  1489. }
  1490. /*****************************************************************************
  1491. Routine Description:
  1492. Arguments:
  1493. Return Value:
  1494. *****************************************************************************/
  1495. VOID
  1496. StrCpy(PUCHAR pszDestination,ULONG DestinationLen,PUCHAR pszSrc)
  1497. {
  1498. ULONG Len;
  1499. ULONG SrcLen;
  1500. ULONG i;
  1501. StrSet(pszDestination, DestinationLen,'\0');
  1502. SrcLen = StrLen(pszSrc);
  1503. if (DestinationLen - 1 < SrcLen)
  1504. {
  1505. Len = DestinationLen - 1;
  1506. }
  1507. else
  1508. {
  1509. Len = SrcLen;
  1510. }
  1511. for (i = 0; i < Len; i++)
  1512. {
  1513. pszDestination[i] = pszSrc[i];
  1514. if (pszSrc[i] == '\0')
  1515. break;
  1516. }
  1517. return;
  1518. }
  1519. /*****************************************************************************
  1520. Routine Description:
  1521. Arguments:
  1522. Return Value:
  1523. *****************************************************************************/
  1524. ULONG
  1525. StrLen (PUCHAR pszString)
  1526. {
  1527. ULONG Len = 0;
  1528. while ( *(pszString+Len) != '\0')
  1529. Len++;
  1530. return(Len);
  1531. }
  1532. /*****************************************************************************
  1533. Routine Description:
  1534. Arguments:
  1535. Return Value:
  1536. *****************************************************************************/
  1537. VOID
  1538. StrCat(PUCHAR pszDestination,ULONG DestinationLen,PUCHAR pszSrc)
  1539. {
  1540. ULONG Len;
  1541. ULONG i;
  1542. ULONG SrcLen;
  1543. ULONG DestLen;
  1544. SrcLen = StrLen(pszSrc);
  1545. DestLen = StrLen(pszDestination);
  1546. if (StrLen(pszDestination)>=DestinationLen)
  1547. return;
  1548. if ((DestinationLen-DestLen-1) < SrcLen)
  1549. {
  1550. Len = DestinationLen-DestLen-1;
  1551. }
  1552. else
  1553. {
  1554. Len = SrcLen;
  1555. }
  1556. for (i=0; i < Len; i++)
  1557. {
  1558. pszDestination[DestLen+i] = pszSrc[i];
  1559. if (pszSrc[i] == '\0')
  1560. break;
  1561. }
  1562. return;
  1563. }
  1564. /*****************************************************************************
  1565. Routine Description:
  1566. Arguments:
  1567. Return Value:
  1568. *****************************************************************************/
  1569. VOID
  1570. MemSet(PUCHAR pBuffer,
  1571. ULONG ulBufferSize,
  1572. UCHAR ucPattern,
  1573. ULONG ulCount)
  1574. {
  1575. ULONG i;
  1576. for (i=0; i<ulCount;i++)
  1577. {
  1578. if (i >= ulBufferSize)
  1579. break;
  1580. pBuffer[i] = ucPattern;
  1581. }
  1582. return ;
  1583. }
  1584. /*****************************************************************************
  1585. Routine Description:
  1586. Arguments:
  1587. Return Value:
  1588. *****************************************************************************/
  1589. VOID
  1590. MemCpy(PUCHAR pDestination,
  1591. ULONG ulDestinationLen,
  1592. PUCHAR pSource,
  1593. ULONG ulCount)
  1594. {
  1595. ULONG i = 0;
  1596. while ( ulCount-- && ulDestinationLen-- )
  1597. {
  1598. pDestination[i] = pSource[i];
  1599. i++;
  1600. }
  1601. return;
  1602. }
  1603. /*****************************************************************************
  1604. Routine Description:
  1605. Arguments:
  1606. Return Value:
  1607. *****************************************************************************/
  1608. VOID
  1609. SCCMN50M_UpdateCurrentStateThread(
  1610. IN PVOID Context
  1611. )
  1612. {
  1613. PDEVICE_EXTENSION deviceExtension = Context;
  1614. PSMARTCARD_EXTENSION smartcardExtension;
  1615. NTSTATUS status;
  1616. LONG lRetry;
  1617. KIRQL oldIrql;
  1618. LONG ulFailures;
  1619. BOOLEAN fPriorityIncreased;
  1620. LONG lOldPriority;
  1621. SmartcardDebug(DEBUG_DRIVER,
  1622. ("%s!UpdateCurrentStateThread started\n",DRIVER_NAME));
  1623. ulFailures = 0;
  1624. smartcardExtension = &deviceExtension->SmartcardExtension;
  1625. //
  1626. // Increase priority for first loop,
  1627. // because state of card must be known for resource manager
  1628. //
  1629. fPriorityIncreased=TRUE;
  1630. lOldPriority=KeSetPriorityThread(KeGetCurrentThread(),HIGH_PRIORITY);
  1631. do
  1632. {
  1633. KeWaitForSingleObject(&smartcardExtension->ReaderExtension->CardManIOMutex,
  1634. Executive,
  1635. KernelMode,
  1636. FALSE,
  1637. NULL);
  1638. if ( smartcardExtension->ReaderExtension->TimeToTerminateThread )
  1639. {
  1640. KeReleaseMutex(&smartcardExtension->ReaderExtension->CardManIOMutex,FALSE);
  1641. smartcardExtension->ReaderExtension->TimeToTerminateThread = FALSE;
  1642. PsTerminateSystemThread( STATUS_SUCCESS );
  1643. }
  1644. lRetry = 1;
  1645. do
  1646. {
  1647. //SmartcardDebug(DEBUG_TRACE,( "%s!*.",DRIVER_NAME));
  1648. status=SCCMN50M_UpdateCurrentState(smartcardExtension);
  1649. if (NT_SUCCESS(status))
  1650. {
  1651. break;
  1652. }
  1653. else
  1654. {
  1655. lRetry--;
  1656. }
  1657. }
  1658. while (lRetry >= 0);
  1659. if (lRetry < 0)
  1660. {
  1661. ulFailures++;
  1662. if (ulFailures == 1)
  1663. {
  1664. SmartcardDebug(
  1665. DEBUG_DRIVER,
  1666. ( "%s!CardMan removed\n",
  1667. DRIVER_NAME)
  1668. );
  1669. // issue a card removal event if reader has been removed
  1670. if (smartcardExtension->ReaderExtension->ulOldCardState == INSERTED ||
  1671. smartcardExtension->ReaderExtension->ulOldCardState == POWERED )
  1672. {
  1673. SmartcardDebug(
  1674. DEBUG_DRIVER,
  1675. ( "%s!issuing card removal event\n",
  1676. DRIVER_NAME)
  1677. );
  1678. SCCMN50M_CompleteCardTracking(smartcardExtension);
  1679. smartcardExtension->ReaderExtension->SyncParameters.fCardPowerRequested = TRUE;
  1680. smartcardExtension->ReaderExtension->ulNewCardState = REMOVED;
  1681. smartcardExtension->ReaderExtension->ulOldCardState = smartcardExtension->ReaderExtension->ulNewCardState;
  1682. smartcardExtension->ReaderCapabilities.CurrentState = SCARD_ABSENT;
  1683. smartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  1684. smartcardExtension->CardCapabilities.ATR.Length = 0;
  1685. SCCMN50M_ClearCardControlFlags(smartcardExtension,ALL_FLAGS);
  1686. smartcardExtension->ReaderExtension->CardManConfig.CardStopBits = 0;
  1687. smartcardExtension->ReaderExtension->CardManConfig.ResetDelay = 0;
  1688. }
  1689. }
  1690. if (ulFailures == 3)
  1691. {
  1692. // remove the device and terminate this thread
  1693. if (KeReadStateEvent(&deviceExtension->SerialCloseDone) == 0l)
  1694. {
  1695. SmartcardDebug(
  1696. DEBUG_DRIVER,
  1697. ( "%s!closing serial driver\n",
  1698. DRIVER_NAME)
  1699. );
  1700. SCCMN50M_CloseSerialDriver(smartcardExtension->OsData->DeviceObject);
  1701. KeReleaseMutex(&smartcardExtension->ReaderExtension->CardManIOMutex,FALSE);
  1702. smartcardExtension->ReaderExtension->TimeToTerminateThread = FALSE;
  1703. smartcardExtension->ReaderExtension->ThreadObjectPointer = NULL;
  1704. PsTerminateSystemThread( STATUS_SUCCESS );
  1705. }
  1706. }
  1707. }
  1708. else
  1709. {
  1710. ulFailures = 0;
  1711. }
  1712. KeReleaseMutex(&smartcardExtension->ReaderExtension->CardManIOMutex,FALSE);
  1713. if (fPriorityIncreased)
  1714. {
  1715. fPriorityIncreased=FALSE;
  1716. KeSetPriorityThread(KeGetCurrentThread(),lOldPriority);
  1717. //
  1718. // Lower ourselves down just at tad so that we compete a
  1719. // little less.
  1720. //
  1721. KeSetBasePriorityThread(KeGetCurrentThread(),-1);
  1722. }
  1723. //SmartcardDebug(DEBUG_TRACE,( "...#\n"));
  1724. Wait (smartcardExtension,500 * ms_);
  1725. }
  1726. while (TRUE);
  1727. }
  1728. NTSTATUS SCCMN50M_UpdateCurrentState(
  1729. IN PSMARTCARD_EXTENSION smartcardExtension
  1730. )
  1731. {
  1732. NTSTATUS NTStatus;
  1733. UCHAR pbReadBuffer[2];
  1734. ULONG ulBytesRead;
  1735. BOOLEAN fCardStateChanged;
  1736. fCardStateChanged = FALSE;
  1737. SCCMN50M_ClearCardManHeader(smartcardExtension);
  1738. smartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant = 250;
  1739. NTStatus = SCCMN50M_WriteCardMan(smartcardExtension,0,NULL);
  1740. smartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant = DEFAULT_READ_TOTAL_TIMEOUT_CONSTANT;
  1741. if (NT_SUCCESS(NTStatus))
  1742. {
  1743. NTStatus = SCCMN50M_ReadCardMan(smartcardExtension,2,&ulBytesRead,pbReadBuffer,sizeof(pbReadBuffer));
  1744. if (ulBytesRead == 0x02 && // two bytes must have benn received
  1745. (pbReadBuffer[0] & 0x0F) && // at least one version bit must be set
  1746. ((pbReadBuffer[0] & 0x09) == 0x00)) // Bit 0 and Bit 3 must be 0
  1747. {
  1748. if ((pbReadBuffer[0] & 0x04) == 0x04 &&
  1749. (pbReadBuffer[0] & 0x02) == 0x02)
  1750. smartcardExtension->ReaderExtension->ulNewCardState = INSERTED;
  1751. if ((pbReadBuffer[0] & 0x04) == 0x00 &&
  1752. (pbReadBuffer[0] & 0x02) == 0x02)
  1753. smartcardExtension->ReaderExtension->ulNewCardState = REMOVED;
  1754. if ((pbReadBuffer[0] & 0x04) == 0x04 &&
  1755. (pbReadBuffer[0] & 0x02) == 0x00)
  1756. smartcardExtension->ReaderExtension->ulNewCardState = POWERED;
  1757. //SmartcardDebug(DEBUG_TRACE,("old %x ",smartcardExtension->ReaderExtension->ulOldCardState ));
  1758. //SmartcardDebug(DEBUG_TRACE,("new %x\n",smartcardExtension->ReaderExtension->ulNewCardState ));
  1759. if (smartcardExtension->ReaderExtension->ulNewCardState == INSERTED &&
  1760. smartcardExtension->ReaderExtension->ulOldCardState == POWERED )
  1761. {
  1762. // card has been removed and reinserted within 500ms
  1763. fCardStateChanged = TRUE;
  1764. SmartcardDebug(DEBUG_DRIVER,( "%s!Smartcard removed and reinserted\n",DRIVER_NAME));
  1765. smartcardExtension->ReaderExtension->ulOldCardState = REMOVED;
  1766. smartcardExtension->ReaderCapabilities.CurrentState = SCARD_ABSENT;
  1767. smartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  1768. // clear any cardspecific data
  1769. smartcardExtension->CardCapabilities.ATR.Length = 0;
  1770. SCCMN50M_ClearCardControlFlags(smartcardExtension,ALL_FLAGS);
  1771. smartcardExtension->ReaderExtension->CardManConfig.CardStopBits = 0;
  1772. smartcardExtension->ReaderExtension->CardManConfig.ResetDelay = 0;
  1773. }
  1774. if (smartcardExtension->ReaderExtension->ulNewCardState == REMOVED &&
  1775. (smartcardExtension->ReaderExtension->ulOldCardState == UNKNOWN ||
  1776. smartcardExtension->ReaderExtension->ulOldCardState == INSERTED ||
  1777. smartcardExtension->ReaderExtension->ulOldCardState == POWERED ) )
  1778. {
  1779. // card has been removed
  1780. fCardStateChanged = TRUE;
  1781. SmartcardDebug(DEBUG_DRIVER,( "%s!Smartcard removed\n",DRIVER_NAME));
  1782. smartcardExtension->ReaderExtension->ulOldCardState = smartcardExtension->ReaderExtension->ulNewCardState;
  1783. smartcardExtension->ReaderCapabilities.CurrentState = SCARD_ABSENT;
  1784. smartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  1785. // clear any cardspecific data
  1786. smartcardExtension->CardCapabilities.ATR.Length = 0;
  1787. SCCMN50M_ClearCardControlFlags(smartcardExtension,ALL_FLAGS);
  1788. smartcardExtension->ReaderExtension->CardManConfig.CardStopBits = 0;
  1789. smartcardExtension->ReaderExtension->CardManConfig.ResetDelay = 0;
  1790. }
  1791. if (smartcardExtension->ReaderExtension->ulNewCardState == INSERTED &&
  1792. (smartcardExtension->ReaderExtension->ulOldCardState == UNKNOWN ||
  1793. smartcardExtension->ReaderExtension->ulOldCardState == REMOVED ) )
  1794. {
  1795. // card has been inserted
  1796. fCardStateChanged = TRUE;
  1797. SmartcardDebug(DEBUG_DRIVER,( "%s!Smartcard inserted\n",DRIVER_NAME));
  1798. smartcardExtension->ReaderExtension->ulOldCardState = smartcardExtension->ReaderExtension->ulNewCardState;
  1799. smartcardExtension->ReaderCapabilities.CurrentState = SCARD_SWALLOWED;
  1800. smartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  1801. }
  1802. // state after reset of the PC (only for CardMan Power+ possible)
  1803. if (smartcardExtension->ReaderExtension->ulNewCardState == POWERED &&
  1804. smartcardExtension->ReaderExtension->ulOldCardState == UNKNOWN )
  1805. {
  1806. // card has been inserted
  1807. fCardStateChanged = TRUE;
  1808. SmartcardDebug(DEBUG_DRIVER,( "%s!Smartcard inserted (and powered)\n",DRIVER_NAME));
  1809. smartcardExtension->ReaderExtension->ulOldCardState = smartcardExtension->ReaderExtension->ulNewCardState;
  1810. smartcardExtension->ReaderCapabilities.CurrentState = SCARD_SWALLOWED;
  1811. smartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  1812. }
  1813. if (smartcardExtension->ReaderExtension->ulNewCardState == POWERED &&
  1814. smartcardExtension->ReaderExtension->ulOldCardState == INSERTED )
  1815. {
  1816. smartcardExtension->ReaderExtension->ulOldCardState = smartcardExtension->ReaderExtension->ulNewCardState;
  1817. }
  1818. // complete IOCTL_SMARTCARD_IS_ABSENT or IOCTL_SMARTCARD_IS_PRESENT
  1819. if (fCardStateChanged == TRUE &&
  1820. smartcardExtension->OsData->NotificationIrp )
  1821. {
  1822. SCCMN50M_CompleteCardTracking(smartcardExtension);
  1823. }
  1824. }
  1825. }
  1826. return NTStatus;
  1827. }
  1828. /*****************************************************************************
  1829. Routine Description:
  1830. Arguments:
  1831. Return Value:
  1832. *****************************************************************************/
  1833. NTSTATUS Wait (PSMARTCARD_EXTENSION pSmartcardExtension,ULONG ulMilliseconds)
  1834. {
  1835. NTSTATUS status = STATUS_SUCCESS;
  1836. LARGE_INTEGER WaitTime;
  1837. WaitTime = RtlConvertLongToLargeInteger(ulMilliseconds * WAIT_MS);
  1838. KeDelayExecutionThread(KernelMode,FALSE,&WaitTime);
  1839. return status;
  1840. }
  1841. /*****************************************************************************
  1842. Routine Description:
  1843. Arguments:
  1844. Return Value:
  1845. *****************************************************************************/
  1846. VOID
  1847. SCCMN50M_SetSCRControlFlags(
  1848. IN PSMARTCARD_EXTENSION pSmartcardExtension,
  1849. IN UCHAR Flags
  1850. )
  1851. {
  1852. pSmartcardExtension->ReaderExtension->CardManConfig.SCRControl |= Flags;
  1853. }
  1854. /*****************************************************************************
  1855. Routine Description:
  1856. Arguments:
  1857. Return Value:
  1858. *****************************************************************************/
  1859. VOID
  1860. SCCMN50M_ClearSCRControlFlags(
  1861. IN PSMARTCARD_EXTENSION pSmartcardExtension,
  1862. IN UCHAR Flags
  1863. )
  1864. {
  1865. pSmartcardExtension->ReaderExtension->CardManConfig.SCRControl &= ~Flags;
  1866. }
  1867. /*****************************************************************************
  1868. Routine Description:
  1869. Arguments:
  1870. Return Value:
  1871. *****************************************************************************/
  1872. VOID
  1873. SCCMN50M_SetCardControlFlags(
  1874. IN PSMARTCARD_EXTENSION pSmartcardExtension,
  1875. IN UCHAR Flags
  1876. )
  1877. {
  1878. pSmartcardExtension->ReaderExtension->CardManConfig.CardControl |= Flags;
  1879. }
  1880. /*****************************************************************************
  1881. Routine Description:
  1882. Arguments:
  1883. Return Value:
  1884. *****************************************************************************/
  1885. VOID
  1886. SCCMN50M_ClearCardControlFlags(
  1887. IN PSMARTCARD_EXTENSION pSmartcardExtension,
  1888. IN UCHAR Flags
  1889. )
  1890. {
  1891. pSmartcardExtension->ReaderExtension->CardManConfig.CardControl &= ~Flags;
  1892. }
  1893. /*****************************************************************************
  1894. Routine Description:
  1895. Arguments:
  1896. Return Value:
  1897. *****************************************************************************/
  1898. VOID
  1899. SCCMN50M_ClearCardManHeader(
  1900. IN PSMARTCARD_EXTENSION pSmartcardExtension
  1901. )
  1902. {
  1903. pSmartcardExtension->ReaderExtension->CardManHeader.TxControl = 0x00;
  1904. pSmartcardExtension->ReaderExtension->CardManHeader.TxLength = 0x00;
  1905. pSmartcardExtension->ReaderExtension->CardManHeader.RxControl = 0x00;
  1906. pSmartcardExtension->ReaderExtension->CardManHeader.RxLength = 0x00;
  1907. }
  1908. /*****************************************************************************
  1909. Routine Description:
  1910. Arguments:
  1911. Return Value:
  1912. *****************************************************************************/
  1913. VOID
  1914. SCCMN50M_SetCardManHeader(
  1915. IN PSMARTCARD_EXTENSION pSmartcardExtension,
  1916. IN UCHAR TxControl,
  1917. IN UCHAR TxLength,
  1918. IN UCHAR RxControl,
  1919. IN UCHAR RxLength
  1920. )
  1921. {
  1922. pSmartcardExtension->ReaderExtension->CardManHeader.TxControl = TxControl;
  1923. pSmartcardExtension->ReaderExtension->CardManHeader.TxLength = TxLength;
  1924. pSmartcardExtension->ReaderExtension->CardManHeader.RxControl = RxControl;
  1925. pSmartcardExtension->ReaderExtension->CardManHeader.RxLength = RxLength;
  1926. }
  1927. /*****************************************************************************
  1928. Routine Description:
  1929. Arguments:
  1930. Return Value:
  1931. *****************************************************************************/
  1932. NTSTATUS
  1933. SCCMN50M_WriteCardMan (
  1934. IN PSMARTCARD_EXTENSION pSmartcardExtension,
  1935. IN ULONG ulBytesToWrite,
  1936. IN PUCHAR pbWriteBuffer
  1937. )
  1938. {
  1939. NTSTATUS status;
  1940. NTSTATUS DebugStatus;
  1941. PSERIAL_STATUS pSerialStatus;
  1942. // ===============================================
  1943. // Set up timeouts for following read operation
  1944. // ===============================================
  1945. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_TIMEOUTS;
  1946. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  1947. &pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts,
  1948. sizeof(SERIAL_TIMEOUTS));
  1949. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_TIMEOUTS);
  1950. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  1951. /*
  1952. SmartcardDebug(DEBUG_TRACE,("ReadTotalTimeoutMultiplier = %ld\n",
  1953. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier));
  1954. SmartcardDebug(DEBUG_TRACE,("ReadTotalTimeoutConstant = %ld\n",
  1955. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant));
  1956. */
  1957. status = SCCMN50M_SerialIo(pSmartcardExtension);
  1958. // ===============================================
  1959. // write to the CardMan
  1960. // ===============================================
  1961. DebugStatus = SCCMN50M_SetWrite(pSmartcardExtension,ulBytesToWrite,pbWriteBuffer);
  1962. // add pseudoboost (0x00) to write buffer for CardManII
  1963. if (pSmartcardExtension->ReaderExtension->fTransparentMode == FALSE )
  1964. {
  1965. pSmartcardExtension->SmartcardRequest.Buffer[pSmartcardExtension->SmartcardRequest.BufferLength] = 0x00;
  1966. pSmartcardExtension->SmartcardRequest.BufferLength++;
  1967. }
  1968. status = SCCMN50M_SerialIo(pSmartcardExtension);
  1969. // overwrite write buffer with '@'
  1970. RtlFillMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  1971. pSmartcardExtension->SmartcardRequest.BufferLength,
  1972. '@');
  1973. // ===============================================
  1974. // error checking
  1975. // ===============================================
  1976. DebugStatus = SCCMN50M_GetCommStatus(pSmartcardExtension);
  1977. pSerialStatus = (PSERIAL_STATUS) pSmartcardExtension->SmartcardReply.Buffer;
  1978. if (pSerialStatus->Errors || NT_ERROR(status))
  1979. {
  1980. pSmartcardExtension->ReaderExtension->SerialErrors = pSerialStatus->Errors;
  1981. if (!pSmartcardExtension->ReaderExtension->fTransparentMode )
  1982. DebugStatus = SCCMN50M_ResyncCardManII(pSmartcardExtension);
  1983. goto ExitSCCMN50M_WriteCardMan;
  1984. }
  1985. ExitSCCMN50M_WriteCardMan:
  1986. if (status != STATUS_SUCCESS)
  1987. {
  1988. SmartcardDebug(
  1989. DEBUG_TRACE,
  1990. ( "%s!WriteCardMan: Failed, exit %lx\n",
  1991. DRIVER_NAME,status)
  1992. );
  1993. }
  1994. return status;
  1995. }
  1996. /*****************************************************************************
  1997. Routine Description:
  1998. Arguments:
  1999. Return Value:
  2000. *****************************************************************************/
  2001. NTSTATUS SCCMN50M_ResyncCardManI (IN PSMARTCARD_EXTENSION pSmartcardExtension )
  2002. {
  2003. NTSTATUS status;
  2004. // SmartcardDebug(DEBUG_TRACE,("%s!ResyncCardManI: Enter\n",DRIVER_NAME))
  2005. // clear error flags
  2006. pSmartcardExtension->ReaderExtension->SerialErrors = 0;
  2007. // clear any pending errors
  2008. status = SCCMN50M_GetCommStatus(pSmartcardExtension);
  2009. if (!NT_SUCCESS(status))
  2010. {
  2011. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_GetCommStatus failed ! status = %x\n",status))
  2012. goto ExitSCCMN50M_ResyncCardManI;
  2013. }
  2014. // clear COM buffers
  2015. status = SCCMN50M_PurgeComm(pSmartcardExtension);
  2016. if (!NT_SUCCESS(status))
  2017. {
  2018. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_PurgeComm failed ! status = %x\n",status))
  2019. goto ExitSCCMN50M_ResyncCardManI;
  2020. }
  2021. // ####################################################################
  2022. // set break
  2023. if (!pSmartcardExtension->ReaderExtension->fTransparentMode)
  2024. {
  2025. status = SCCMN50M_SetBRK(pSmartcardExtension);
  2026. if (!NT_SUCCESS(status))
  2027. {
  2028. SmartcardDebug(DEBUG_ERROR,("SetBreak failed ! status = %x\n",status))
  2029. goto ExitSCCMN50M_ResyncCardManI;
  2030. }
  2031. }
  2032. // wait 1ms
  2033. Wait(pSmartcardExtension,1 * ms_);
  2034. // clear RTS
  2035. status = SCCMN50M_ClearRTS(pSmartcardExtension);
  2036. if (!NT_SUCCESS(status))
  2037. {
  2038. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_ClearRTS failed ! status = %x\n",status))
  2039. goto ExitSCCMN50M_ResyncCardManI;
  2040. }
  2041. // wait 2ms
  2042. Wait(pSmartcardExtension,2 * ms_);
  2043. // set RTS
  2044. status = SCCMN50M_SetRTS(pSmartcardExtension);
  2045. if (!NT_SUCCESS(status))
  2046. {
  2047. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_SetRTS failed ! status = %x\n",status))
  2048. goto ExitSCCMN50M_ResyncCardManI;
  2049. }
  2050. // wait 1ms
  2051. Wait(pSmartcardExtension,1 * ms_);
  2052. // clear break
  2053. if (!pSmartcardExtension->ReaderExtension->fTransparentMode)
  2054. {
  2055. pSmartcardExtension->ReaderExtension->BreakSet = FALSE;
  2056. status = SCCMN50M_ClearBRK(pSmartcardExtension);
  2057. if (!NT_SUCCESS(status))
  2058. {
  2059. SmartcardDebug(DEBUG_ERROR,("ClearBreak failed ! status = %x\n",status))
  2060. goto ExitSCCMN50M_ResyncCardManI;
  2061. }
  2062. }
  2063. // ####################################################################
  2064. // next write operation must send config data
  2065. pSmartcardExtension->ReaderExtension->NoConfig = FALSE;
  2066. // clear COM buffers
  2067. status = SCCMN50M_PurgeComm(pSmartcardExtension);
  2068. if (!NT_SUCCESS(status))
  2069. {
  2070. goto ExitSCCMN50M_ResyncCardManI;
  2071. }
  2072. // clear any pending errors
  2073. status = SCCMN50M_GetCommStatus(pSmartcardExtension);
  2074. if (!NT_SUCCESS(status))
  2075. {
  2076. goto ExitSCCMN50M_ResyncCardManI;
  2077. }
  2078. ExitSCCMN50M_ResyncCardManI:
  2079. //SmartcardDebug(DEBUG_TRACE,("%s!ResyncCardManI: Exit %lx\n",DRIVER_NAME,status))
  2080. return status;
  2081. }
  2082. NTSTATUS SCCMN50M_ResyncCardManII (IN PSMARTCARD_EXTENSION pSmartcardExtension )
  2083. {
  2084. NTSTATUS status;
  2085. SmartcardDebug(
  2086. DEBUG_TRACE,
  2087. ( "%s!ResyncCardManII: Enter\n",
  2088. DRIVER_NAME)
  2089. );
  2090. // clear error flags
  2091. pSmartcardExtension->ReaderExtension->SerialErrors = 0;
  2092. // clear any pending errors
  2093. status = SCCMN50M_GetCommStatus(pSmartcardExtension);
  2094. if (!NT_SUCCESS(status))
  2095. {
  2096. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_GetCommStatus failed ! status = %x\n",status))
  2097. goto ExitSCCMN50M_ResyncCardManII;
  2098. }
  2099. // clear COM buffers
  2100. status = SCCMN50M_PurgeComm(pSmartcardExtension);
  2101. if (!NT_SUCCESS(status))
  2102. {
  2103. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_PurgeComm failed ! status = %x\n",status))
  2104. goto ExitSCCMN50M_ResyncCardManII;
  2105. }
  2106. // 150 * 0xFE
  2107. RtlFillMemory(pSmartcardExtension->SmartcardRequest.Buffer,150,0xFE);
  2108. pSmartcardExtension->SmartcardRequest.Buffer[150] = 0x00;
  2109. pSmartcardExtension->SmartcardRequest.BufferLength = 151;
  2110. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  2111. pSmartcardExtension->ReaderExtension->SerialIoControlCode = SMARTCARD_WRITE;
  2112. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2113. if (!NT_SUCCESS(status))
  2114. {
  2115. Wait(pSmartcardExtension,2 * ms_);
  2116. // try resync once more
  2117. // clear error flags
  2118. pSmartcardExtension->ReaderExtension->SerialErrors = 0;
  2119. // clear any pending errors
  2120. status = SCCMN50M_GetCommStatus(pSmartcardExtension);
  2121. if (!NT_SUCCESS(status))
  2122. {
  2123. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_GetCommStatus failed ! status = %x\n",status))
  2124. goto ExitSCCMN50M_ResyncCardManII;
  2125. }
  2126. // clear COM buffers
  2127. status = SCCMN50M_PurgeComm(pSmartcardExtension);
  2128. if (!NT_SUCCESS(status))
  2129. {
  2130. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_PurgeComm failed ! status = %x\n",status))
  2131. goto ExitSCCMN50M_ResyncCardManII;
  2132. }
  2133. // 150 * 0xFE
  2134. RtlFillMemory(pSmartcardExtension->SmartcardRequest.Buffer,150,0xFE);
  2135. pSmartcardExtension->SmartcardRequest.Buffer[150] = 0x00;
  2136. pSmartcardExtension->SmartcardRequest.BufferLength = 151;
  2137. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  2138. pSmartcardExtension->ReaderExtension->SerialIoControlCode = SMARTCARD_WRITE;
  2139. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2140. if (status != STATUS_SUCCESS)
  2141. {
  2142. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_SerialIo failed ! status = %x\n",status))
  2143. goto ExitSCCMN50M_ResyncCardManII;
  2144. }
  2145. // normally the second resync command is always successful
  2146. }
  2147. // clear COM buffers
  2148. status = SCCMN50M_PurgeComm(pSmartcardExtension);
  2149. if (!NT_SUCCESS(status))
  2150. {
  2151. goto ExitSCCMN50M_ResyncCardManII;
  2152. }
  2153. // clear any pending errors
  2154. status = SCCMN50M_GetCommStatus(pSmartcardExtension);
  2155. if (!NT_SUCCESS(status))
  2156. {
  2157. goto ExitSCCMN50M_ResyncCardManII;
  2158. }
  2159. ExitSCCMN50M_ResyncCardManII:
  2160. SmartcardDebug(
  2161. DEBUG_TRACE,
  2162. ( "%s!ResyncCardManII: Exit %lx\n",
  2163. DRIVER_NAME,status)
  2164. );
  2165. return status;
  2166. }
  2167. /*****************************************************************************
  2168. Routine Description:
  2169. Arguments:
  2170. Return Value:
  2171. *****************************************************************************/
  2172. NTSTATUS
  2173. SCCMN50M_SerialIo(IN PSMARTCARD_EXTENSION pSmartcardExtension)
  2174. {
  2175. NTSTATUS status;
  2176. IO_STATUS_BLOCK ioStatus;
  2177. KEVENT event;
  2178. PIRP irp;
  2179. PIO_STACK_LOCATION irpNextStack;
  2180. PUCHAR pbRequestBuffer;
  2181. PUCHAR pbReplyBuffer;
  2182. ULONG ulRequestBufferLength;
  2183. ULONG ulReplyBufferLength ;
  2184. //
  2185. // Check if the buffers are large enough
  2186. //
  2187. ASSERT(pSmartcardExtension->SmartcardReply.BufferLength <=
  2188. pSmartcardExtension->SmartcardReply.BufferSize);
  2189. ASSERT(pSmartcardExtension->SmartcardRequest.BufferLength <=
  2190. pSmartcardExtension->SmartcardRequest.BufferSize);
  2191. if (pSmartcardExtension->SmartcardReply.BufferLength >
  2192. pSmartcardExtension->SmartcardReply.BufferSize ||
  2193. pSmartcardExtension->SmartcardRequest.BufferLength >
  2194. pSmartcardExtension->SmartcardRequest.BufferSize)
  2195. {
  2196. SmartcardLogError(pSmartcardExtension->OsData->DeviceObject,
  2197. SCCMN50M_BUFFER_TOO_SMALL,
  2198. NULL,
  2199. 0);
  2200. return STATUS_BUFFER_TOO_SMALL;
  2201. }
  2202. // set pointer and length of request and reply buffer
  2203. ulRequestBufferLength = pSmartcardExtension->SmartcardRequest.BufferLength;
  2204. pbRequestBuffer = (ulRequestBufferLength ? pSmartcardExtension->SmartcardRequest.Buffer : NULL);
  2205. pbReplyBuffer = pSmartcardExtension->SmartcardReply.Buffer;
  2206. ulReplyBufferLength = pSmartcardExtension->SmartcardReply.BufferLength;
  2207. KeInitializeEvent(&event,
  2208. NotificationEvent,
  2209. FALSE);
  2210. //
  2211. // Build irp to be sent to serial driver
  2212. //
  2213. irp = IoBuildDeviceIoControlRequest(pSmartcardExtension->ReaderExtension->SerialIoControlCode,
  2214. pSmartcardExtension->ReaderExtension->AttachedDeviceObject,
  2215. pbRequestBuffer,
  2216. ulRequestBufferLength,
  2217. pbReplyBuffer,
  2218. ulReplyBufferLength,
  2219. FALSE,
  2220. &event,
  2221. &ioStatus);
  2222. ASSERT(irp != NULL);
  2223. if (irp == NULL)
  2224. {
  2225. return STATUS_INSUFFICIENT_RESOURCES;
  2226. }
  2227. irpNextStack = IoGetNextIrpStackLocation(irp);
  2228. switch (pSmartcardExtension->ReaderExtension->SerialIoControlCode)
  2229. {
  2230. //
  2231. // The serial driver transfers data from/to irp->AssociatedIrp.SystemBuffer
  2232. //
  2233. case SMARTCARD_WRITE:
  2234. irpNextStack->MajorFunction = IRP_MJ_WRITE;
  2235. irpNextStack->Parameters.Write.Length = pSmartcardExtension->SmartcardRequest.BufferLength;
  2236. break;
  2237. case SMARTCARD_READ:
  2238. irpNextStack->MajorFunction = IRP_MJ_READ;
  2239. irpNextStack->Parameters.Read.Length = pSmartcardExtension->SmartcardReply.BufferLength;
  2240. break;
  2241. }
  2242. status = IoCallDriver(pSmartcardExtension->ReaderExtension->AttachedDeviceObject,irp);
  2243. if (status == STATUS_PENDING)
  2244. {
  2245. KeWaitForSingleObject(&event,
  2246. Suspended,
  2247. KernelMode,
  2248. FALSE,
  2249. NULL);
  2250. status = ioStatus.Status;
  2251. }
  2252. switch (pSmartcardExtension->ReaderExtension->SerialIoControlCode)
  2253. {
  2254. case SMARTCARD_READ:
  2255. if (status == STATUS_TIMEOUT)
  2256. {
  2257. SmartcardDebug(DEBUG_ERROR,
  2258. ("%s!Timeout while reading from CardMan\n",
  2259. DRIVER_NAME));
  2260. //
  2261. // STATUS_TIMEOUT isn't correctly mapped
  2262. // to a WIN32 error, that's why we change it here
  2263. // to STATUS_IO_TIMEOUT
  2264. //
  2265. status = STATUS_IO_TIMEOUT;
  2266. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  2267. }
  2268. break;
  2269. }
  2270. #if 0
  2271. if (status != STATUS_SUCCESS)
  2272. {
  2273. SmartcardDebug(DEBUG_DRIVER,
  2274. ("%s!SerialIo = %lx\n",
  2275. DRIVER_NAME,
  2276. status));
  2277. }
  2278. #endif
  2279. return status;
  2280. }
  2281. /*****************************************************************************
  2282. Routine Description:
  2283. Arguments:
  2284. Return Value:
  2285. *****************************************************************************/
  2286. NTSTATUS
  2287. SCCMN50M_ReadCardMan (
  2288. IN PSMARTCARD_EXTENSION pSmartcardExtension,
  2289. IN ULONG BytesToRead,
  2290. OUT PULONG pBytesRead,
  2291. IN PUCHAR pReadBuffer,
  2292. IN ULONG ReadBufferSize
  2293. )
  2294. {
  2295. NTSTATUS status;
  2296. NTSTATUS DebugStatus;
  2297. BOOLEAN fRc;
  2298. // check if read buffer is large enough
  2299. ASSERT(BytesToRead <= ReadBufferSize);
  2300. *pBytesRead = 0; // default setting
  2301. DebugStatus = SCCMN50M_SetRead(pSmartcardExtension,BytesToRead);
  2302. //
  2303. // read operation
  2304. //
  2305. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2306. if (status == STATUS_SUCCESS)
  2307. {
  2308. *pBytesRead = pSmartcardExtension->SmartcardReply.BufferLength;
  2309. MemCpy(pReadBuffer,
  2310. ReadBufferSize,
  2311. pSmartcardExtension->SmartcardReply.Buffer,
  2312. pSmartcardExtension->SmartcardReply.BufferLength);
  2313. // overwrite read buffer with '@'
  2314. MemSet(pSmartcardExtension->SmartcardReply.Buffer,
  2315. pSmartcardExtension->SmartcardReply.BufferSize,
  2316. '@',
  2317. pSmartcardExtension->SmartcardReply.BufferLength);
  2318. }
  2319. if (status != STATUS_SUCCESS || SCCMN50M_IOOperationFailed(pSmartcardExtension))
  2320. {
  2321. if (!pSmartcardExtension->ReaderExtension->fTransparentMode)
  2322. {
  2323. DebugStatus = SCCMN50M_ResyncCardManII(pSmartcardExtension);
  2324. }
  2325. goto ExitSCCMN50M_ReadCardMan;
  2326. }
  2327. // *****************************************
  2328. // set CardManII to state RH Config
  2329. // *****************************************
  2330. // don't set CardMan to RH config if there are still bytes to be read
  2331. if (pSmartcardExtension->ReaderExtension->ToRHConfig == TRUE)
  2332. {
  2333. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  2334. pSmartcardExtension->SmartcardRequest.Buffer [0] = 0x00;
  2335. pSmartcardExtension->SmartcardRequest.Buffer [1] = 0x00;
  2336. pSmartcardExtension->SmartcardRequest.Buffer [2] = 0x00;
  2337. pSmartcardExtension->SmartcardRequest.Buffer [3] = 0x00;
  2338. pSmartcardExtension->SmartcardRequest.Buffer [4] = 0x89;
  2339. pSmartcardExtension->SmartcardRequest.BufferLength = 5;
  2340. pSmartcardExtension->ReaderExtension->SerialIoControlCode = SMARTCARD_WRITE;
  2341. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2342. if (status != STATUS_SUCCESS || SCCMN50M_IOOperationFailed(pSmartcardExtension))
  2343. {
  2344. DebugStatus = SCCMN50M_ResyncCardManII(pSmartcardExtension);
  2345. goto ExitSCCMN50M_ReadCardMan;
  2346. }
  2347. }
  2348. ExitSCCMN50M_ReadCardMan:
  2349. // set default value;
  2350. pSmartcardExtension->ReaderExtension->ToRHConfig = TRUE;
  2351. if (status != STATUS_SUCCESS)
  2352. {
  2353. SmartcardDebug(
  2354. DEBUG_TRACE,
  2355. ( "%s!ReadCardMan: Failed, exit %lx\n",
  2356. DRIVER_NAME,status)
  2357. );
  2358. }
  2359. return status;
  2360. }
  2361. /*****************************************************************************
  2362. Routine Description:
  2363. Arguments:
  2364. Return Value:
  2365. *****************************************************************************/
  2366. NTSTATUS
  2367. SCCMN50M_GetCommStatus (
  2368. IN PSMARTCARD_EXTENSION SmartcardExtension
  2369. )
  2370. {
  2371. PSERIAL_READER_CONFIG configData = &SmartcardExtension->ReaderExtension->SerialConfigData;
  2372. NTSTATUS status;
  2373. PUCHAR request = SmartcardExtension->SmartcardRequest.Buffer;
  2374. SmartcardExtension->SmartcardReply.BufferLength = SmartcardExtension->SmartcardReply.BufferSize;
  2375. SmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_GET_COMMSTATUS;
  2376. SmartcardExtension->SmartcardRequest.Buffer = (PUCHAR) &configData->SerialStatus;
  2377. SmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_STATUS);
  2378. status = SCCMN50M_SerialIo(SmartcardExtension);
  2379. //
  2380. // restore pointer to original request buffer
  2381. //
  2382. SmartcardExtension->SmartcardRequest.Buffer = request;
  2383. return status;
  2384. }
  2385. /*****************************************************************************
  2386. Routine Description:
  2387. Arguments:
  2388. Return Value:
  2389. *****************************************************************************/
  2390. BOOLEAN
  2391. SCCMN50M_IOOperationFailed(PSMARTCARD_EXTENSION pSmartcardExtension)
  2392. {
  2393. NTSTATUS DebugStatus;
  2394. PSERIAL_STATUS pSerialStatus;
  2395. DebugStatus = SCCMN50M_GetCommStatus(pSmartcardExtension);
  2396. pSerialStatus = (PSERIAL_STATUS)pSmartcardExtension->SmartcardReply.Buffer;
  2397. if (pSerialStatus->Errors)
  2398. return TRUE;
  2399. else
  2400. return FALSE;
  2401. }
  2402. /*****************************************************************************
  2403. Routine Description:
  2404. Arguments:
  2405. Return Value:
  2406. *****************************************************************************/
  2407. NTSTATUS SCCMN50M_PurgeComm (IN PSMARTCARD_EXTENSION pSmartcardExtension )
  2408. {
  2409. PSERIAL_READER_CONFIG configData = &pSmartcardExtension->ReaderExtension->SerialConfigData;
  2410. NTSTATUS status;
  2411. PUCHAR request = pSmartcardExtension->SmartcardRequest.Buffer;
  2412. pSmartcardExtension->SmartcardReply.BufferLength = pSmartcardExtension->SmartcardReply.BufferSize;
  2413. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_PURGE;
  2414. pSmartcardExtension->SmartcardRequest.Buffer = (PUCHAR) &configData->PurgeMask;
  2415. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(ULONG);
  2416. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2417. //
  2418. // restore pointer to original request buffer
  2419. //
  2420. pSmartcardExtension->SmartcardRequest.Buffer = request;
  2421. // under W2000 & CardMan P+ STATUS_CANCELLED may be returned
  2422. if (status == STATUS_CANCELLED)
  2423. status = STATUS_SUCCESS;
  2424. return status;
  2425. }
  2426. /*****************************************************************************
  2427. Routine Description:
  2428. Arguments:
  2429. Return Value:
  2430. *****************************************************************************/
  2431. NTSTATUS
  2432. SCCMN50M_SetRead(IN PSMARTCARD_EXTENSION pSmartcardExtension,
  2433. IN ULONG ulBytesToRead
  2434. )
  2435. {
  2436. pSmartcardExtension->ReaderExtension->SerialIoControlCode = SMARTCARD_READ;
  2437. pSmartcardExtension->SmartcardRequest.BufferLength = 0;
  2438. pSmartcardExtension->SmartcardReply.BufferLength = ulBytesToRead;
  2439. return STATUS_SUCCESS;
  2440. }
  2441. /*****************************************************************************
  2442. Routine Description:
  2443. Arguments:
  2444. Return Value:
  2445. *****************************************************************************/
  2446. NTSTATUS
  2447. SCCMN50M_SetWrite(IN PSMARTCARD_EXTENSION pSmartcardExtension,
  2448. IN ULONG BytesToWrite,
  2449. IN PUCHAR WriteBuffer
  2450. )
  2451. {
  2452. ULONG Offset = 0;
  2453. pSmartcardExtension->ReaderExtension->SerialIoControlCode = SMARTCARD_WRITE;
  2454. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  2455. if (pSmartcardExtension->ReaderExtension->fTransparentMode == FALSE)
  2456. {
  2457. // send always config string for CardManII, expect we set it manualy
  2458. // to NoConfig = TRUE. (note: only one time)
  2459. if (pSmartcardExtension->ReaderExtension->NoConfig == FALSE)
  2460. {
  2461. MemCpy(pSmartcardExtension->SmartcardRequest.Buffer,
  2462. pSmartcardExtension->SmartcardRequest.BufferSize,
  2463. (PUCHAR)&pSmartcardExtension->ReaderExtension->CardManConfig,
  2464. sizeof(CARDMAN_CONFIG));
  2465. Offset = 4;
  2466. }
  2467. else
  2468. {
  2469. pSmartcardExtension->ReaderExtension->NoConfig = FALSE;
  2470. }
  2471. MemCpy(pSmartcardExtension->SmartcardRequest.Buffer + Offset,
  2472. pSmartcardExtension->SmartcardRequest.BufferSize,
  2473. (PUCHAR)&pSmartcardExtension->ReaderExtension->CardManHeader,
  2474. sizeof(CARDMAN_HEADER));
  2475. Offset+=4;
  2476. }
  2477. if (BytesToWrite != 0)
  2478. {
  2479. MemCpy(pSmartcardExtension->SmartcardRequest.Buffer + Offset,
  2480. pSmartcardExtension->SmartcardRequest.BufferSize,
  2481. WriteBuffer,
  2482. BytesToWrite);
  2483. }
  2484. pSmartcardExtension->SmartcardRequest.BufferLength = Offset + BytesToWrite;
  2485. return STATUS_SUCCESS;
  2486. }
  2487. /*****************************************************************************
  2488. Routine Description:
  2489. Arguments:
  2490. Return Value:
  2491. *****************************************************************************/
  2492. NTSTATUS
  2493. SCCMN50M_StartCardTracking(
  2494. PDEVICE_EXTENSION pDeviceExtension
  2495. )
  2496. {
  2497. NTSTATUS status;
  2498. HANDLE hThread;
  2499. PSMARTCARD_EXTENSION pSmartcardExtension = &pDeviceExtension->SmartcardExtension;
  2500. SmartcardDebug(DEBUG_TRACE,
  2501. ("%s!StartCardTracking: Enter\n",DRIVER_NAME));
  2502. KeWaitForSingleObject(&pSmartcardExtension->ReaderExtension->CardManIOMutex,
  2503. Executive,
  2504. KernelMode,
  2505. FALSE,
  2506. NULL);
  2507. // create thread for updating current state
  2508. status = PsCreateSystemThread(&hThread,
  2509. THREAD_ALL_ACCESS,
  2510. NULL,
  2511. NULL,
  2512. NULL,
  2513. SCCMN50M_UpdateCurrentStateThread,
  2514. pDeviceExtension);
  2515. if (!NT_ERROR(status))
  2516. {
  2517. //
  2518. // We've got the thread. Now get a pointer to it.
  2519. //
  2520. status = ObReferenceObjectByHandle(hThread,
  2521. THREAD_ALL_ACCESS,
  2522. NULL,
  2523. KernelMode,
  2524. &pSmartcardExtension->ReaderExtension->ThreadObjectPointer,
  2525. NULL);
  2526. if (NT_ERROR(status))
  2527. {
  2528. pSmartcardExtension->ReaderExtension->TimeToTerminateThread = TRUE;
  2529. }
  2530. else
  2531. {
  2532. //
  2533. // Now that we have a reference to the thread
  2534. // we can simply close the handle.
  2535. //
  2536. ZwClose(hThread);
  2537. }
  2538. }
  2539. else
  2540. {
  2541. }
  2542. // Release the mutex
  2543. KeReleaseMutex(&pSmartcardExtension->ReaderExtension->CardManIOMutex,
  2544. FALSE);
  2545. SmartcardDebug(DEBUG_TRACE,
  2546. ("%s!StartCardTracking: Exit %lx\n",DRIVER_NAME,status));
  2547. return status;
  2548. }
  2549. /*****************************************************************************
  2550. Routine Description:
  2551. Arguments:
  2552. Return Value:
  2553. *****************************************************************************/
  2554. NTSTATUS
  2555. SCCMN50M_InitCommPort (PSMARTCARD_EXTENSION pSmartcardExtension)
  2556. {
  2557. NTSTATUS status;
  2558. SmartcardDebug(
  2559. DEBUG_TRACE,
  2560. ( "%s!InitCommPort: Enter\n",
  2561. DRIVER_NAME)
  2562. );
  2563. // ===============================
  2564. // clear any pending errors
  2565. // ===============================
  2566. status = SCCMN50M_GetCommStatus(pSmartcardExtension);
  2567. if (!NT_SUCCESS(status))
  2568. {
  2569. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_GetCommStatus failed ! status = %ld\n",status))
  2570. goto ExitInitCommPort;
  2571. }
  2572. // ==============================
  2573. // set baudrate for CardMan
  2574. // ==============================
  2575. pSmartcardExtension->ReaderExtension->SerialConfigData.BaudRate.BaudRate = 38400;
  2576. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_BAUD_RATE;
  2577. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  2578. &pSmartcardExtension->ReaderExtension->SerialConfigData.BaudRate.BaudRate,
  2579. sizeof(SERIAL_BAUD_RATE));
  2580. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_BAUD_RATE);
  2581. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  2582. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2583. if (!NT_SUCCESS(status))
  2584. {
  2585. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_BAUDRATE failed ! status = %ld\n",status))
  2586. goto ExitInitCommPort;
  2587. }
  2588. // ===============================
  2589. // set comm timeouts
  2590. // ===============================
  2591. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadIntervalTimeout = DEFAULT_READ_INTERVAL_TIMEOUT;
  2592. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant = DEFAULT_READ_TOTAL_TIMEOUT_CONSTANT;
  2593. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier = DEFAULT_READ_TOTAL_TIMEOUT_MULTIPLIER;
  2594. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.WriteTotalTimeoutConstant = DEFAULT_WRITE_TOTAL_TIMEOUT_CONSTANT;
  2595. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.WriteTotalTimeoutMultiplier = DEFAULT_WRITE_TOTAL_TIMEOUT_MULTIPLIER;
  2596. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_TIMEOUTS;
  2597. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  2598. &pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts,
  2599. sizeof(SERIAL_TIMEOUTS));
  2600. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_TIMEOUTS);
  2601. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  2602. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2603. if (!NT_SUCCESS(status))
  2604. {
  2605. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_TIMEOUTS failed ! status = %x\n",status))
  2606. goto ExitInitCommPort;
  2607. }
  2608. // ===============================
  2609. // set line control
  2610. // ===============================
  2611. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.StopBits = STOP_BITS_2;
  2612. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.Parity = EVEN_PARITY;
  2613. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.WordLength = 8;
  2614. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_LINE_CONTROL;
  2615. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  2616. &pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl,
  2617. sizeof(SERIAL_LINE_CONTROL));
  2618. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_LINE_CONTROL);
  2619. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  2620. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2621. if (!NT_SUCCESS(status))
  2622. {
  2623. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_LINE_CONTROL failed ! status = %x\n",status))
  2624. goto ExitInitCommPort;
  2625. }
  2626. // ===============================
  2627. // Set handflow
  2628. // ===============================
  2629. pSmartcardExtension->ReaderExtension->SerialConfigData.HandFlow.XonLimit = 0;
  2630. pSmartcardExtension->ReaderExtension->SerialConfigData.HandFlow.XoffLimit = 0;
  2631. pSmartcardExtension->ReaderExtension->SerialConfigData.HandFlow.FlowReplace = 0;
  2632. pSmartcardExtension->ReaderExtension->SerialConfigData.HandFlow.ControlHandShake = SERIAL_ERROR_ABORT | SERIAL_DTR_CONTROL;
  2633. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_HANDFLOW;
  2634. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  2635. &pSmartcardExtension->ReaderExtension->SerialConfigData.HandFlow,
  2636. sizeof(SERIAL_HANDFLOW));
  2637. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_HANDFLOW);
  2638. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  2639. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2640. if (!NT_SUCCESS(status))
  2641. {
  2642. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_HANDFLOW failed ! status = %x\n",status))
  2643. goto ExitInitCommPort;
  2644. }
  2645. // ===============================
  2646. // set purge mask
  2647. // ===============================
  2648. pSmartcardExtension->ReaderExtension->SerialConfigData.PurgeMask =
  2649. SERIAL_PURGE_TXABORT | SERIAL_PURGE_RXABORT |
  2650. SERIAL_PURGE_TXCLEAR | SERIAL_PURGE_RXCLEAR;
  2651. // ===============================
  2652. // set DTR
  2653. // ===============================
  2654. status = SCCMN50M_SetDTR(pSmartcardExtension);
  2655. if (!NT_SUCCESS(status))
  2656. {
  2657. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_DRT failed ! status = %x\n",status))
  2658. goto ExitInitCommPort;
  2659. }
  2660. // ===============================
  2661. // set RTS
  2662. // ===============================
  2663. status = SCCMN50M_SetRTS(pSmartcardExtension);
  2664. if (!NT_SUCCESS(status))
  2665. {
  2666. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_RTS failed ! status = %x\n",status))
  2667. goto ExitInitCommPort;
  2668. }
  2669. ExitInitCommPort:
  2670. SmartcardDebug(
  2671. DEBUG_TRACE,
  2672. ( "%s!InitCommPort: Exit %lx\n",
  2673. DRIVER_NAME,status)
  2674. );
  2675. return status;
  2676. }
  2677. /*****************************************************************************
  2678. Routine Description:
  2679. Arguments:
  2680. Return Value:
  2681. *****************************************************************************/
  2682. NTSTATUS
  2683. SCCMN50M_SetDTR(IN PSMARTCARD_EXTENSION pSmartcardExtension )
  2684. {
  2685. NTSTATUS status;
  2686. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  2687. pSmartcardExtension->SmartcardRequest.BufferLength = 0;
  2688. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_DTR;
  2689. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2690. // under W2000 & CardMan P+ STATUS_CANCELLED may be returned
  2691. if (status == STATUS_CANCELLED)
  2692. status = STATUS_SUCCESS;
  2693. return status;
  2694. }
  2695. /*****************************************************************************
  2696. Routine Description:
  2697. Arguments:
  2698. Return Value:
  2699. *****************************************************************************/
  2700. NTSTATUS
  2701. SCCMN50M_SetRTS(IN PSMARTCARD_EXTENSION pSmartcardExtension )
  2702. {
  2703. NTSTATUS status;
  2704. pSmartcardExtension->SmartcardReply.BufferLength = pSmartcardExtension->SmartcardReply.BufferSize;
  2705. pSmartcardExtension->SmartcardRequest.BufferLength = 0;
  2706. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_RTS;
  2707. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2708. // under W2000 & CardMan P+ STATUS_CANCELLED may be returned
  2709. if (status == STATUS_CANCELLED)
  2710. status = STATUS_SUCCESS;
  2711. return status;
  2712. }
  2713. /*****************************************************************************
  2714. Routine Description:
  2715. Arguments:
  2716. Return Value:
  2717. *****************************************************************************/
  2718. NTSTATUS
  2719. SCCMN50M_InitializeCardMan(IN PSMARTCARD_EXTENSION pSmartcardExtension)
  2720. {
  2721. NTSTATUS status;
  2722. UCHAR pReadBuffer[2];
  2723. ULONG ulBytesRead;
  2724. BOOLEAN fCardManFound = FALSE;
  2725. PREADER_EXTENSION readerExtension = pSmartcardExtension->ReaderExtension;
  2726. ULONG ulRetries;
  2727. SmartcardDebug(
  2728. DEBUG_TRACE,
  2729. ( "%s!InitializeCardMan: Enter\n",
  2730. DRIVER_NAME)
  2731. );
  2732. pSmartcardExtension->ReaderExtension->ulOldCardState = UNKNOWN;
  2733. // ==============================================
  2734. // CardManII
  2735. // ==============================================
  2736. pSmartcardExtension->ReaderExtension->NoConfig = FALSE;
  2737. pSmartcardExtension->ReaderExtension->ToRHConfig = TRUE;
  2738. // This waiting time if necessary for CardMan Power+, because
  2739. // the pnP string may be dumped
  2740. Wait(pSmartcardExtension,200);
  2741. status = SCCMN50M_InitCommPort(pSmartcardExtension);
  2742. if (status != STATUS_SUCCESS)
  2743. goto ExitInitializeCardMan;
  2744. //
  2745. // init CommPort was O.K.
  2746. // now try to find a reader
  2747. //
  2748. // To be sure wait make an additional wait
  2749. Wait(pSmartcardExtension,100);
  2750. status = SCCMN50M_ResyncCardManII(pSmartcardExtension);
  2751. status = SCCMN50M_ResyncCardManII(pSmartcardExtension);
  2752. // no data except config + header
  2753. pSmartcardExtension->ReaderExtension->CardManConfig.SCRControl = XMIT_HANDSHAKE_OFF;
  2754. pSmartcardExtension->ReaderExtension->CardManConfig.CardControl = 0x00;
  2755. pSmartcardExtension->ReaderExtension->CardManConfig.CardStopBits = 0x00;
  2756. pSmartcardExtension->ReaderExtension->CardManConfig.ResetDelay = 0x00;
  2757. pSmartcardExtension->ReaderExtension->CardManHeader.TxControl = 0x00;
  2758. pSmartcardExtension->ReaderExtension->CardManHeader.TxLength = 0x00;
  2759. pSmartcardExtension->ReaderExtension->CardManHeader.RxControl = 0x00;
  2760. pSmartcardExtension->ReaderExtension->CardManHeader.RxLength = 0x00;
  2761. status = SCCMN50M_WriteCardMan(pSmartcardExtension,0,NULL);
  2762. if (status == STATUS_SUCCESS)
  2763. {
  2764. status = SCCMN50M_ReadCardMan(pSmartcardExtension,2,&ulBytesRead,pReadBuffer,sizeof(pReadBuffer));
  2765. if (status == STATUS_SUCCESS &&
  2766. ulBytesRead == 0x02 && // two bytes received
  2767. pReadBuffer[0] >= 0x40 && // at least one version bit must be set
  2768. pReadBuffer[1] == 0x00 &&
  2769. ((pReadBuffer[0] & 0x09) == 0) ) // bit 0 and 3 must be cleared
  2770. {
  2771. pSmartcardExtension->ReaderExtension->ulFWVersion = (pReadBuffer[0] >> 4) * 30 + 120;
  2772. pSmartcardExtension->ReaderExtension->fSPESupported = FALSE;
  2773. SmartcardDebug(
  2774. DEBUG_DRIVER,
  2775. ( "%s!CardMan (FW %ld) found\n",
  2776. DRIVER_NAME,pSmartcardExtension->ReaderExtension->ulFWVersion)
  2777. );
  2778. fCardManFound = TRUE;
  2779. }
  2780. }
  2781. ExitInitializeCardMan:
  2782. if (fCardManFound == TRUE)
  2783. status = STATUS_SUCCESS;
  2784. else
  2785. status = STATUS_UNSUCCESSFUL;
  2786. SmartcardDebug(
  2787. DEBUG_TRACE,
  2788. ( "%s!InitializeCardMan: Exit %lx\n",
  2789. DRIVER_NAME,status)
  2790. );
  2791. return status;
  2792. }
  2793. /*****************************************************************************
  2794. Routine Description:
  2795. Arguments:
  2796. Return Value:
  2797. *****************************************************************************/
  2798. NTSTATUS
  2799. SCCMN50M_EnterTransparentMode (IN PSMARTCARD_EXTENSION pSmartcardExtension)
  2800. {
  2801. NTSTATUS status;
  2802. SmartcardDebug(DEBUG_TRACE,("EnterTransparentMode : enter\n"));
  2803. // Step 1 : Resync CardMan by RTS usage
  2804. status = SCCMN50M_ResyncCardManI(pSmartcardExtension);
  2805. if (status != STATUS_SUCCESS)
  2806. {
  2807. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_ResyncCardManI failed ! status = %ld\n",status))
  2808. goto ExitEnterTransparentMode;
  2809. }
  2810. // Step 2 : set baud rate to 9600
  2811. pSmartcardExtension->ReaderExtension->SerialConfigData.BaudRate.BaudRate = 9600;
  2812. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_BAUD_RATE;
  2813. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  2814. &pSmartcardExtension->ReaderExtension->SerialConfigData.BaudRate.BaudRate,
  2815. sizeof(SERIAL_BAUD_RATE));
  2816. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_BAUD_RATE);
  2817. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  2818. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2819. if (!NT_SUCCESS(status))
  2820. {
  2821. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_BAUDRATE failed ! status = %ld\n",status))
  2822. goto ExitEnterTransparentMode;
  2823. }
  2824. ExitEnterTransparentMode:
  2825. // Step 3 : set ATR and DUMP_BUFFER flags
  2826. // During normal operation these two flags can never be set at the same time
  2827. SCCMN50M_SetSCRControlFlags(pSmartcardExtension,CM2_GET_ATR | TO_STATE_XH);
  2828. SmartcardDebug(DEBUG_TRACE,("EnterTransparentMode : exit\n"));
  2829. return status;
  2830. }
  2831. /*****************************************************************************
  2832. Routine Description:
  2833. Arguments:
  2834. Return Value:
  2835. *****************************************************************************/
  2836. NTSTATUS
  2837. SCCMN50M_ExitTransparentMode (IN PSMARTCARD_EXTENSION pSmartcardExtension)
  2838. {
  2839. NTSTATUS status;
  2840. SmartcardDebug(DEBUG_TRACE,("ExitTransparentMode : enter\n"));
  2841. // ===============================
  2842. // clear any pending errors
  2843. // ===============================
  2844. status = SCCMN50M_GetCommStatus(pSmartcardExtension);
  2845. if (!NT_SUCCESS(status))
  2846. {
  2847. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_GetCommStatus failed ! status = %ld\n",status))
  2848. goto ExitExitTransparentMode;
  2849. }
  2850. // Step 1 : Resync CardMan by RTS usage
  2851. status = SCCMN50M_ResyncCardManI(pSmartcardExtension);
  2852. if (status != STATUS_SUCCESS)
  2853. {
  2854. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_ResyncCardManI failed ! status = %ld\n",status))
  2855. goto ExitExitTransparentMode;
  2856. }
  2857. // Step 2 : set baud rate to 38400
  2858. pSmartcardExtension->ReaderExtension->SerialConfigData.BaudRate.BaudRate = 38400;
  2859. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_BAUD_RATE;
  2860. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  2861. &pSmartcardExtension->ReaderExtension->SerialConfigData.BaudRate.BaudRate,
  2862. sizeof(SERIAL_BAUD_RATE));
  2863. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_BAUD_RATE);
  2864. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  2865. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2866. if (!NT_SUCCESS(status))
  2867. {
  2868. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_BAUDRATE failed ! status = %ld\n",status))
  2869. goto ExitExitTransparentMode;
  2870. }
  2871. // if the inserted card uses inverse convention , we must now switch the COM port
  2872. // back to even parity
  2873. if (pSmartcardExtension->ReaderExtension->fInverseAtr == TRUE)
  2874. {
  2875. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.StopBits = STOP_BITS_2;
  2876. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.Parity = EVEN_PARITY;
  2877. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.WordLength = SERIAL_DATABITS_8;
  2878. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_LINE_CONTROL;
  2879. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  2880. &pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl,
  2881. sizeof(SERIAL_LINE_CONTROL));
  2882. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_LINE_CONTROL);
  2883. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  2884. status = SCCMN50M_SerialIo(pSmartcardExtension);
  2885. if (!NT_SUCCESS(status))
  2886. {
  2887. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_LINE_CONTROL failed ! status = %x\n",status))
  2888. goto ExitExitTransparentMode;
  2889. }
  2890. }
  2891. ExitExitTransparentMode:
  2892. // Step 3 : set ATR and DUMP_BUFFER flags
  2893. // During normal operation these two flags can never be set at the same time
  2894. SCCMN50M_ClearSCRControlFlags(pSmartcardExtension,CM2_GET_ATR | TO_STATE_XH);
  2895. status = SCCMN50M_ResyncCardManII(pSmartcardExtension);
  2896. if (!NT_SUCCESS(status))
  2897. {
  2898. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_ResyncCardManII failed ! status = %x\n",status))
  2899. goto ExitExitTransparentMode;
  2900. }
  2901. SmartcardDebug(DEBUG_TRACE,("ExitTransparentMode : exit\n"));
  2902. return status;
  2903. }
  2904. /*****************************************************************************
  2905. Routine Description:
  2906. Arguments:
  2907. Return Value:
  2908. *****************************************************************************/
  2909. NTSTATUS
  2910. SCCMN50M_ClearRTS(IN PSMARTCARD_EXTENSION SmartcardExtension )
  2911. {
  2912. NTSTATUS status;
  2913. SmartcardExtension->SmartcardReply.BufferLength = SmartcardExtension->SmartcardReply.BufferSize;
  2914. SmartcardExtension->SmartcardRequest.BufferLength = 0;
  2915. SmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_CLR_RTS;
  2916. status = SCCMN50M_SerialIo(SmartcardExtension);
  2917. return status;
  2918. }
  2919. /*****************************************************************************
  2920. Routine Description:
  2921. Arguments:
  2922. Return Value:
  2923. *****************************************************************************/
  2924. NTSTATUS
  2925. SCCMN50M_IoCtlVendor(PSMARTCARD_EXTENSION pSmartcardExtension)
  2926. {
  2927. NTSTATUS status = STATUS_SUCCESS;
  2928. NTSTATUS DebugStatus;
  2929. UCHAR pbAttrBuffer[MAXIMUM_ATR_LENGTH];
  2930. ULONG ulAtrLength;
  2931. SmartcardDebug(
  2932. DEBUG_TRACE,
  2933. ( "%s!IoCtlVendor : Enter\n",
  2934. DRIVER_NAME)
  2935. );
  2936. switch (pSmartcardExtension->MajorIoControlCode)
  2937. {
  2938. case CM_IOCTL_SET_READER_9600_BAUD:
  2939. status = SCCMN50M_SetFl_1Dl_1(pSmartcardExtension);
  2940. break;
  2941. case CM_IOCTL_SET_READER_38400_BAUD:
  2942. status = SCCMN50M_SetFl_1Dl_3(pSmartcardExtension);
  2943. break;
  2944. case CM_IOCTL_CR80S_SAMOS_SET_HIGH_SPEED:
  2945. status = SCCMN50M_SetHighSpeed_CR80S_SAMOS(pSmartcardExtension);
  2946. break;
  2947. case CM_IOCTL_GET_FW_VERSION:
  2948. status = SCCMN50M_GetFWVersion(pSmartcardExtension);
  2949. break;
  2950. case CM_IOCTL_READ_DEVICE_DESCRIPTION:
  2951. status = SCCMN50M_ReadDeviceDescription(pSmartcardExtension);
  2952. break;
  2953. case CM_IOCTL_SET_SYNC_PARAMETERS :
  2954. status = SCCMN50M_SetSyncParameters(pSmartcardExtension);
  2955. break;
  2956. case CM_IOCTL_3WBP_TRANSFER : // for SLE4428
  2957. status = SCCMN50M_Transmit3WBP(pSmartcardExtension);
  2958. break;
  2959. case CM_IOCTL_2WBP_TRANSFER : // for SLE4442
  2960. status = SCCMN50M_Transmit2WBP(pSmartcardExtension);
  2961. break;
  2962. case CM_IOCTL_2WBP_RESET_CARD: // SLE4442 Reset Card
  2963. status = SCCMN50M_ResetCard2WBP(pSmartcardExtension);
  2964. break;
  2965. case CM_IOCTL_SYNC_CARD_POWERON:
  2966. status = SCCMN50M_SyncCardPowerOn(pSmartcardExtension);
  2967. break;
  2968. default:
  2969. status = STATUS_INVALID_DEVICE_REQUEST;
  2970. break;
  2971. }
  2972. SmartcardDebug(
  2973. DEBUG_TRACE,
  2974. ( "%s!IoCtlVendor : Exit %lx\n",
  2975. DRIVER_NAME,status)
  2976. );
  2977. return status;
  2978. }
  2979. /*****************************************************************************
  2980. Routine Description:
  2981. Arguments:
  2982. Return Value:
  2983. *****************************************************************************/
  2984. NTSTATUS
  2985. SCCMN50M_SetFl_1Dl_3(IN PSMARTCARD_EXTENSION pSmartcardExtension )
  2986. {
  2987. NTSTATUS status = STATUS_SUCCESS;
  2988. NTSTATUS DebugStatus;
  2989. SmartcardDebug(DEBUG_TRACE,
  2990. ("%s!SetFl_1Dl_3 Enter\n",
  2991. DRIVER_NAME));
  2992. // check if T=1 active
  2993. if (pSmartcardExtension->CardCapabilities.Protocol.Selected !=
  2994. SCARD_PROTOCOL_T1)
  2995. {
  2996. status = STATUS_CTL_FILE_NOT_SUPPORTED;
  2997. goto ExitSetFl_1Dl_3;
  2998. }
  2999. // Fl=1
  3000. // Dl=3
  3001. // => 38400 Baud for 3.72 MHz
  3002. SCCMN50M_ClearCardControlFlags(pSmartcardExtension,ENABLE_3MHZ | ENABLE_5MHZ |
  3003. ENABLE_3MHZ_FAST | ENABLE_5MHZ_FAST );
  3004. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_3MHZ_FAST);
  3005. ExitSetFl_1Dl_3:
  3006. *pSmartcardExtension->IoRequest.Information = 0L;
  3007. SmartcardDebug(DEBUG_TRACE,
  3008. ("%s!SetFl_1Dl_3 Exit\n",
  3009. DRIVER_NAME));
  3010. return status;
  3011. }
  3012. /*****************************************************************************
  3013. Routine Description:
  3014. Arguments:
  3015. Return Value:
  3016. *****************************************************************************/
  3017. NTSTATUS
  3018. SCCMN50M_SetFl_1Dl_1(IN PSMARTCARD_EXTENSION pSmartcardExtension )
  3019. {
  3020. NTSTATUS status = STATUS_SUCCESS;
  3021. SmartcardDebug(DEBUG_TRACE,
  3022. ("%s!SetFl_1Dl_1 Enter\n",
  3023. DRIVER_NAME));
  3024. // Fl=1
  3025. // Dl=1
  3026. // => 9600 for 3.72 MHz
  3027. SCCMN50M_ClearCardControlFlags(pSmartcardExtension,ENABLE_3MHZ | ENABLE_5MHZ |
  3028. ENABLE_3MHZ_FAST | ENABLE_5MHZ_FAST );
  3029. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_3MHZ);
  3030. *pSmartcardExtension->IoRequest.Information = 0L;
  3031. SmartcardDebug(DEBUG_TRACE,
  3032. ("%s!SetFl_1Dl_1 Exit\n",
  3033. DRIVER_NAME));
  3034. return status;
  3035. }
  3036. /*****************************************************************************
  3037. Routine Description:
  3038. Arguments:
  3039. Return Value:
  3040. *****************************************************************************/
  3041. NTSTATUS
  3042. SCCMN50M_GetFWVersion (IN PSMARTCARD_EXTENSION pSmartcardExtension )
  3043. {
  3044. NTSTATUS status = STATUS_SUCCESS;
  3045. SmartcardDebug(
  3046. DEBUG_TRACE,
  3047. ( "%s!GetFWVersion : Enter\n",
  3048. DRIVER_NAME)
  3049. );
  3050. if (pSmartcardExtension->IoRequest.ReplyBufferLength < sizeof (ULONG))
  3051. {
  3052. status = STATUS_BUFFER_OVERFLOW;
  3053. goto ExitGetFWVersion;
  3054. }
  3055. else
  3056. {
  3057. *(PULONG)(pSmartcardExtension->IoRequest.ReplyBuffer) =
  3058. pSmartcardExtension->ReaderExtension->ulFWVersion;
  3059. }
  3060. ExitGetFWVersion:
  3061. *pSmartcardExtension->IoRequest.Information = sizeof(ULONG);
  3062. SmartcardDebug(
  3063. DEBUG_TRACE,
  3064. ( "%s!GetFWVersion : Exit %lx\n",
  3065. DRIVER_NAME,status)
  3066. );
  3067. return status;
  3068. }
  3069. /*****************************************************************************
  3070. Routine Description:
  3071. Arguments:
  3072. Return Value:
  3073. *****************************************************************************/
  3074. NTSTATUS
  3075. SCCMN50M_ReadDeviceDescription(IN PSMARTCARD_EXTENSION pSmartcardExtension )
  3076. {
  3077. NTSTATUS status = STATUS_SUCCESS;
  3078. SmartcardDebug(
  3079. DEBUG_TRACE,
  3080. ( "%s!ReadDeviceDescription : Enter\n",
  3081. DRIVER_NAME)
  3082. );
  3083. if (pSmartcardExtension->IoRequest.ReplyBufferLength < sizeof(pSmartcardExtension->ReaderExtension->abDeviceDescription))
  3084. {
  3085. status = STATUS_BUFFER_OVERFLOW;
  3086. *pSmartcardExtension->IoRequest.Information = 0L;
  3087. goto ExitReadDeviceDescription;
  3088. }
  3089. else
  3090. {
  3091. if (pSmartcardExtension->ReaderExtension->abDeviceDescription[0] == 0x00 &&
  3092. pSmartcardExtension->ReaderExtension->abDeviceDescription[1] == 0x00 )
  3093. {
  3094. status = SCCMN50M_GetDeviceDescription(pSmartcardExtension);
  3095. }
  3096. if (status == STATUS_SUCCESS)
  3097. {
  3098. StrCpy(pSmartcardExtension->IoRequest.ReplyBuffer,
  3099. pSmartcardExtension->IoRequest.ReplyBufferLength,
  3100. pSmartcardExtension->ReaderExtension->abDeviceDescription);
  3101. *pSmartcardExtension->IoRequest.Information = StrLen(pSmartcardExtension->ReaderExtension->abDeviceDescription) +1;
  3102. }
  3103. else
  3104. {
  3105. MemSet(pSmartcardExtension->ReaderExtension->abDeviceDescription,
  3106. sizeof(pSmartcardExtension->ReaderExtension->abDeviceDescription),
  3107. 0x00,
  3108. sizeof(pSmartcardExtension->ReaderExtension->abDeviceDescription));
  3109. *pSmartcardExtension->IoRequest.Information = 0;
  3110. }
  3111. }
  3112. ExitReadDeviceDescription:
  3113. SmartcardDebug(
  3114. DEBUG_TRACE,
  3115. ( "%s!ReadDeviceDescription : Exit %lx\n",
  3116. DRIVER_NAME,status)
  3117. );
  3118. return status;
  3119. }
  3120. /*****************************************************************************
  3121. Routine Description:
  3122. Arguments:
  3123. Return Value:
  3124. *****************************************************************************/
  3125. NTSTATUS
  3126. SCCMN50M_SetHighSpeed_CR80S_SAMOS (IN PSMARTCARD_EXTENSION pSmartcardExtension )
  3127. {
  3128. NTSTATUS status;
  3129. NTSTATUS DebugStatus;
  3130. UCHAR bReadBuffer[16];
  3131. ULONG ulBytesRead;
  3132. BYTE bCR80S_SAMOS_SET_HIGH_SPEED[4] = {0xFF,0x11,0x94,0x7A};
  3133. ULONG ulAtrLength;
  3134. BYTE bAtr[MAXIMUM_ATR_LENGTH];
  3135. SmartcardDebug(
  3136. DEBUG_TRACE,
  3137. ( "%s!SetHighSpeed_CR80S_SAMOS : Enter\n",
  3138. DRIVER_NAME)
  3139. );
  3140. SCCMN50M_ClearCardControlFlags(pSmartcardExtension,ENABLE_SYN | ENABLE_T0 |
  3141. ENABLE_T1 );
  3142. SCCMN50M_SetCardManHeader(pSmartcardExtension,
  3143. 0, // Tx control
  3144. sizeof(bCR80S_SAMOS_SET_HIGH_SPEED), // Tx length
  3145. 0, // Rx control
  3146. sizeof(bCR80S_SAMOS_SET_HIGH_SPEED)); // Rx length
  3147. status = SCCMN50M_WriteCardMan(pSmartcardExtension,
  3148. sizeof(bCR80S_SAMOS_SET_HIGH_SPEED),
  3149. bCR80S_SAMOS_SET_HIGH_SPEED);
  3150. if (status != STATUS_SUCCESS)
  3151. goto ExitSetHighSpeed;
  3152. pSmartcardExtension->ReaderExtension->ToRHConfig = FALSE;
  3153. status = SCCMN50M_ReadCardMan(pSmartcardExtension,2,&ulBytesRead,bReadBuffer,sizeof(bReadBuffer));
  3154. if (status != STATUS_SUCCESS)
  3155. goto ExitSetHighSpeed;
  3156. if (bReadBuffer[1] > sizeof(bReadBuffer))
  3157. {
  3158. status = STATUS_BUFFER_OVERFLOW;
  3159. goto ExitSetHighSpeed;
  3160. }
  3161. status = SCCMN50M_ReadCardMan(pSmartcardExtension,bReadBuffer[1],&ulBytesRead,bReadBuffer,sizeof(bReadBuffer));
  3162. if (status != STATUS_SUCCESS)
  3163. goto ExitSetHighSpeed;
  3164. // if the card has accepted this string , the string is echoed
  3165. if (bReadBuffer[0] == bCR80S_SAMOS_SET_HIGH_SPEED[0] &&
  3166. bReadBuffer[1] == bCR80S_SAMOS_SET_HIGH_SPEED[1] &&
  3167. bReadBuffer[2] == bCR80S_SAMOS_SET_HIGH_SPEED[2] &&
  3168. bReadBuffer[3] == bCR80S_SAMOS_SET_HIGH_SPEED[3] )
  3169. {
  3170. SCCMN50M_ClearCardControlFlags(pSmartcardExtension,ENABLE_3MHZ | ENABLE_5MHZ |
  3171. ENABLE_3MHZ_FAST | ENABLE_5MHZ_FAST );
  3172. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_5MHZ_FAST);
  3173. }
  3174. else
  3175. {
  3176. DebugStatus = SCCMN50M_PowerOff(pSmartcardExtension);
  3177. DebugStatus = SCCMN50M_PowerOn(pSmartcardExtension,&ulAtrLength,bAtr,sizeof(bAtr));
  3178. status = STATUS_UNSUCCESSFUL;
  3179. }
  3180. ExitSetHighSpeed:
  3181. *pSmartcardExtension->IoRequest.Information = 0L;
  3182. SmartcardDebug(
  3183. DEBUG_TRACE,
  3184. ( "%s!SetHighSpeed_CR80S_SAMOS : Exit %lx\n",
  3185. DRIVER_NAME,status)
  3186. );
  3187. return status;
  3188. }
  3189. /*****************************************************************************
  3190. Routine Description:
  3191. Arguments:
  3192. Return Value:
  3193. *****************************************************************************/
  3194. NTSTATUS
  3195. SCCMN50M_SetBRK(IN PSMARTCARD_EXTENSION pSmartcardExtension )
  3196. {
  3197. NTSTATUS status;
  3198. pSmartcardExtension->SmartcardReply.BufferLength = pSmartcardExtension->SmartcardReply.BufferSize;
  3199. pSmartcardExtension->SmartcardRequest.BufferLength = 0;
  3200. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_BREAK_ON;
  3201. status = SCCMN50M_SerialIo(pSmartcardExtension);
  3202. return status;
  3203. }
  3204. /*****************************************************************************
  3205. Routine Description:
  3206. Arguments:
  3207. Return Value:
  3208. *****************************************************************************/
  3209. NTSTATUS
  3210. SCCMN50M_ClearBRK(IN PSMARTCARD_EXTENSION SmartcardExtension )
  3211. {
  3212. NTSTATUS status;
  3213. SmartcardExtension->SmartcardReply.BufferLength = SmartcardExtension->SmartcardReply.BufferSize;
  3214. SmartcardExtension->SmartcardRequest.BufferLength = 0;
  3215. SmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_BREAK_OFF;
  3216. status = SCCMN50M_SerialIo(SmartcardExtension);
  3217. return status;
  3218. }
  3219. /*****************************************************************************
  3220. Routine Description:
  3221. Arguments:
  3222. Return Value:
  3223. *****************************************************************************/
  3224. NTSTATUS
  3225. SCCMN50M_SetProtocol(PSMARTCARD_EXTENSION pSmartcardExtension )
  3226. {
  3227. NTSTATUS status;
  3228. NTSTATUS DebugStatus;
  3229. ULONG ulNewProtocol;
  3230. UCHAR abPTSRequest[4];
  3231. UCHAR abReadBuffer[6];
  3232. UCHAR abPTSReply [4];
  3233. ULONG ulBytesRead;
  3234. UCHAR bTemp;
  3235. ULONG ulPtsType;
  3236. ULONG ulPTSReplyLength=0;
  3237. ULONG ulStatBytesRead;
  3238. BYTE abStatReadBuffer[2];
  3239. SmartcardDebug(
  3240. DEBUG_TRACE,
  3241. ( "%s!SetProtocol : Enter\n",
  3242. DRIVER_NAME)
  3243. );
  3244. //
  3245. // Check if the card is already in specific state
  3246. // and if the caller wants to have the already selected protocol.
  3247. // We return success if this is the case.
  3248. //
  3249. if ((pSmartcardExtension->CardCapabilities.Protocol.Selected & pSmartcardExtension->MinorIoControlCode))
  3250. {
  3251. status = STATUS_SUCCESS;
  3252. goto ExitSetProtocol;
  3253. }
  3254. ulNewProtocol = pSmartcardExtension->MinorIoControlCode;
  3255. ulPtsType = PTS_TYPE_OPTIMAL;
  3256. // we are not sure if we need this at all
  3257. pSmartcardExtension->CardCapabilities.PtsData.Type = PTS_TYPE_OPTIMAL;
  3258. while (TRUE)
  3259. {
  3260. // set initial character of PTS
  3261. abPTSRequest[0] = 0xFF;
  3262. // set the format character
  3263. if (pSmartcardExtension->CardCapabilities.Protocol.Supported &
  3264. ulNewProtocol &
  3265. SCARD_PROTOCOL_T1)
  3266. {
  3267. // select T=1 and indicate that PTS1 follows
  3268. abPTSRequest[1] = 0x11;
  3269. pSmartcardExtension->CardCapabilities.Protocol.Selected =
  3270. SCARD_PROTOCOL_T1;
  3271. }
  3272. else if (pSmartcardExtension->CardCapabilities.Protocol.Supported &
  3273. ulNewProtocol &
  3274. SCARD_PROTOCOL_T0)
  3275. {
  3276. // select T=1 and indicate that PTS1 follows
  3277. abPTSRequest[1] = 0x10;
  3278. pSmartcardExtension->CardCapabilities.Protocol.Selected =
  3279. SCARD_PROTOCOL_T0;
  3280. }
  3281. else
  3282. {
  3283. status = STATUS_INVALID_DEVICE_REQUEST;
  3284. goto ExitSetProtocol;
  3285. }
  3286. // bug fix :
  3287. // don 't use the suggestion from smclib
  3288. pSmartcardExtension->CardCapabilities.PtsData.Fl =
  3289. pSmartcardExtension->CardCapabilities.Fl;
  3290. pSmartcardExtension->CardCapabilities.PtsData.Dl =
  3291. pSmartcardExtension->CardCapabilities.Dl;
  3292. // CardMan support higher baudrates only for T=1
  3293. // ==> Dl=1
  3294. if (abPTSRequest[1] == 0x10)
  3295. {
  3296. SmartcardDebug(
  3297. DEBUG_PROTOCOL,
  3298. ( "%s!overwriting PTS1 for T=0\n",
  3299. DRIVER_NAME)
  3300. );
  3301. pSmartcardExtension->CardCapabilities.PtsData.Fl = 0x01;
  3302. pSmartcardExtension->CardCapabilities.PtsData.Dl = 0x01;
  3303. }
  3304. if (ulPtsType == PTS_TYPE_DEFAULT)
  3305. {
  3306. SmartcardDebug(
  3307. DEBUG_PROTOCOL,
  3308. ( "%s!overwriting PTS1 with default values\n",
  3309. DRIVER_NAME)
  3310. );
  3311. pSmartcardExtension->CardCapabilities.PtsData.Fl = 0x01;
  3312. pSmartcardExtension->CardCapabilities.PtsData.Dl = 0x01;
  3313. }
  3314. // set pts1 which codes Fl and Dl
  3315. bTemp = (BYTE) (pSmartcardExtension->CardCapabilities.PtsData.Fl << 4 |
  3316. pSmartcardExtension->CardCapabilities.PtsData.Dl);
  3317. SmartcardDebug(
  3318. DEBUG_PROTOCOL,
  3319. ( "%s!PTS1 = %x (suggestion)\n",
  3320. DRIVER_NAME,bTemp)
  3321. );
  3322. switch (bTemp)
  3323. {
  3324. case 0x11:
  3325. // do nothing
  3326. // we support these Fl/Dl parameters
  3327. break;
  3328. case 0x13:
  3329. case 0x94:
  3330. break ;
  3331. case 0x14:
  3332. // let's try it with 38400 baud
  3333. SmartcardDebug(
  3334. DEBUG_PROTOCOL,
  3335. ( "%s!trying 57600 baud\n",DRIVER_NAME)
  3336. );
  3337. // we must correct Fl/Dl
  3338. pSmartcardExtension->CardCapabilities.PtsData.Dl = 0x03;
  3339. pSmartcardExtension->CardCapabilities.PtsData.Fl = 0x01;
  3340. bTemp = (BYTE) (pSmartcardExtension->CardCapabilities.PtsData.Fl << 4 |
  3341. pSmartcardExtension->CardCapabilities.PtsData.Dl);
  3342. break;
  3343. default:
  3344. SmartcardDebug(
  3345. DEBUG_PROTOCOL,
  3346. ( "%s!overwriting PTS1(0x%x)\n",
  3347. DRIVER_NAME,bTemp)
  3348. );
  3349. // we must correct Fl/Dl
  3350. pSmartcardExtension->CardCapabilities.PtsData.Dl = 0x01;
  3351. pSmartcardExtension->CardCapabilities.PtsData.Fl = 0x01;
  3352. bTemp = (BYTE) (pSmartcardExtension->CardCapabilities.PtsData.Fl << 4 |
  3353. pSmartcardExtension->CardCapabilities.PtsData.Dl);
  3354. break;
  3355. }
  3356. abPTSRequest[2] = bTemp;
  3357. // set pck (check character)
  3358. abPTSRequest[3] = (BYTE)(abPTSRequest[0] ^ abPTSRequest[1] ^ abPTSRequest[2]);
  3359. SmartcardDebug(DEBUG_PROTOCOL,("%s!PTS request: 0x%x 0x%x 0x%x 0x%x\n",
  3360. DRIVER_NAME,
  3361. abPTSRequest[0],
  3362. abPTSRequest[1],
  3363. abPTSRequest[2],
  3364. abPTSRequest[3]));
  3365. MemSet(abPTSReply,sizeof(abPTSReply),0x00,sizeof(abPTSReply));
  3366. DebugStatus = SCCMN50M_EnterTransparentMode(pSmartcardExtension);
  3367. // STEP 1 : write config + header to enter transparent mode
  3368. SCCMN50M_SetCardManHeader(pSmartcardExtension,
  3369. 0, // Tx control
  3370. 0, // Tx length
  3371. 0, // Rx control
  3372. 0); // Rx length
  3373. status = SCCMN50M_WriteCardMan (pSmartcardExtension,
  3374. 0,
  3375. NULL);
  3376. if (NT_ERROR(status))
  3377. {
  3378. goto ExitSetProtocol;
  3379. }
  3380. pSmartcardExtension->ReaderExtension->fTransparentMode = TRUE;
  3381. // if the inserted card uses inverse convention , we must now switch the COM port
  3382. // to odd parity
  3383. /*
  3384. if (pSmartcardExtension->ReaderExtension->fInverseAtr == TRUE)
  3385. {
  3386. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.StopBits = STOP_BITS_2;
  3387. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.Parity = ODD_PARITY;
  3388. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.WordLength = SERIAL_DATABITS_8;
  3389. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_LINE_CONTROL;
  3390. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  3391. &pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl,
  3392. sizeof(SERIAL_LINE_CONTROL));
  3393. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_LINE_CONTROL);
  3394. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  3395. status = SCCMN50M_SerialIo(pSmartcardExtension);
  3396. if (!NT_SUCCESS(status))
  3397. {
  3398. goto ExitTransparentTransmitT0;
  3399. }
  3400. }
  3401. */
  3402. SmartcardDebug(
  3403. DEBUG_PROTOCOL,
  3404. ( "%s!writing PTS request\n",
  3405. DRIVER_NAME)
  3406. );
  3407. status = SCCMN50M_WriteCardMan(pSmartcardExtension,
  3408. 4,
  3409. abPTSRequest);
  3410. if (status != STATUS_SUCCESS)
  3411. {
  3412. SmartcardDebug(
  3413. DEBUG_PROTOCOL,
  3414. ( "%s!writing PTS request failed\n",
  3415. DRIVER_NAME)
  3416. );
  3417. goto ExitSetProtocol;
  3418. }
  3419. // read back pts data
  3420. SmartcardDebug(
  3421. DEBUG_PROTOCOL,
  3422. ( "%s!trying to read PTS reply\n",
  3423. DRIVER_NAME)
  3424. );
  3425. // first read CardMan header
  3426. pSmartcardExtension->ReaderExtension->ToRHConfig= FALSE;
  3427. status = SCCMN50M_ReadCardMan(pSmartcardExtension,3,&ulBytesRead,abReadBuffer,sizeof(abReadBuffer));
  3428. if (status != STATUS_SUCCESS &&
  3429. status != STATUS_IO_TIMEOUT )
  3430. {
  3431. SmartcardDebug(
  3432. DEBUG_PROTOCOL,
  3433. ( "%s!reading status failed\n",
  3434. DRIVER_NAME)
  3435. );
  3436. goto ExitSetProtocol;
  3437. }
  3438. ulPTSReplyLength = 3;
  3439. MemCpy(abPTSReply,sizeof(abPTSReply),abReadBuffer,3);
  3440. // check if bit 5 is set
  3441. if (abPTSReply[1] & 0x10)
  3442. {
  3443. pSmartcardExtension->ReaderExtension->ToRHConfig= FALSE;
  3444. status = SCCMN50M_ReadCardMan(pSmartcardExtension,1,&ulBytesRead,abReadBuffer,sizeof(abReadBuffer));
  3445. if (status != STATUS_SUCCESS &&
  3446. status != STATUS_IO_TIMEOUT )
  3447. {
  3448. SmartcardDebug(
  3449. DEBUG_PROTOCOL,
  3450. ( "%s!reading status failed\n",
  3451. DRIVER_NAME)
  3452. );
  3453. goto ExitSetProtocol;
  3454. }
  3455. ulPTSReplyLength += 1;
  3456. MemCpy(&abPTSReply[3],sizeof(abPTSReply)-3,abReadBuffer,1);
  3457. }
  3458. DebugStatus = SCCMN50M_ExitTransparentMode(pSmartcardExtension);
  3459. pSmartcardExtension->ReaderExtension->fTransparentMode = FALSE;
  3460. // to be sure that the new settings take effect
  3461. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant = 250;
  3462. DebugStatus = SCCMN50M_WriteCardMan(pSmartcardExtension,0,NULL);
  3463. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant = DEFAULT_READ_TOTAL_TIMEOUT_CONSTANT;
  3464. if (NT_SUCCESS(DebugStatus))
  3465. {
  3466. DebugStatus = SCCMN50M_ReadCardMan(pSmartcardExtension,2,&ulStatBytesRead,abStatReadBuffer,sizeof(abStatReadBuffer));
  3467. }
  3468. #if DBG
  3469. if (ulPTSReplyLength == 3)
  3470. {
  3471. SmartcardDebug(DEBUG_PROTOCOL,("PTS reply: 0x%x 0x%x 0x%x\n",
  3472. abPTSReply[0],
  3473. abPTSReply[1],
  3474. abPTSReply[2]));
  3475. }
  3476. if (ulPTSReplyLength == 4)
  3477. {
  3478. SmartcardDebug(DEBUG_PROTOCOL,("PTS reply: 0x%x 0x%x 0x%x 0x%x\n",
  3479. abPTSReply[0],
  3480. abPTSReply[1],
  3481. abPTSReply[2],
  3482. abPTSReply[3]));
  3483. }
  3484. #endif
  3485. if (ulPTSReplyLength == 3 &&
  3486. abPTSReply[0] == abPTSRequest[0] &&
  3487. (abPTSReply[1] & 0x7F) == (abPTSRequest[1] & 0x0F) &&
  3488. abPTSReply[2] == (BYTE)(abPTSReply[0] ^ abPTSReply[1]) )
  3489. {
  3490. SmartcardDebug(
  3491. DEBUG_PROTOCOL,
  3492. ( "%s!short PTS reply received\n",
  3493. DRIVER_NAME)
  3494. );
  3495. break;
  3496. }
  3497. if (ulPTSReplyLength == 4 &&
  3498. abPTSReply[0] == abPTSRequest[0] &&
  3499. abPTSReply[1] == abPTSRequest[1] &&
  3500. abPTSReply[2] == abPTSRequest[2] &&
  3501. abPTSReply[3] == abPTSRequest[3])
  3502. {
  3503. SmartcardDebug(
  3504. DEBUG_PROTOCOL,
  3505. ( "%s!PTS request and reply match\n",
  3506. DRIVER_NAME)
  3507. );
  3508. switch (bTemp)
  3509. {
  3510. case 0x11:
  3511. break;
  3512. case 0x13:
  3513. SCCMN50M_ClearCardControlFlags(pSmartcardExtension,ENABLE_3MHZ | ENABLE_5MHZ |
  3514. ENABLE_3MHZ_FAST | ENABLE_5MHZ_FAST );
  3515. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_3MHZ_FAST);
  3516. break ;
  3517. case 0x94:
  3518. SCCMN50M_ClearCardControlFlags(pSmartcardExtension,ENABLE_3MHZ | ENABLE_5MHZ |
  3519. ENABLE_3MHZ_FAST | ENABLE_5MHZ_FAST );
  3520. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_5MHZ_FAST);
  3521. break;
  3522. }
  3523. break;
  3524. }
  3525. if (pSmartcardExtension->CardCapabilities.PtsData.Type !=
  3526. PTS_TYPE_DEFAULT)
  3527. {
  3528. SmartcardDebug(
  3529. DEBUG_PROTOCOL,
  3530. ( "%s!PTS failed : Trying default parameters\n",
  3531. DRIVER_NAME)
  3532. );
  3533. // the card did either not reply or it replied incorrectly
  3534. // so try default valies
  3535. ulPtsType = pSmartcardExtension->CardCapabilities.PtsData.Type = PTS_TYPE_DEFAULT;
  3536. pSmartcardExtension->MinorIoControlCode = SCARD_COLD_RESET;
  3537. status = SCCMN50M_CardPower(pSmartcardExtension);
  3538. continue;
  3539. }
  3540. // the card failed the pts request
  3541. status = STATUS_DEVICE_PROTOCOL_ERROR;
  3542. goto ExitSetProtocol;
  3543. }
  3544. ExitSetProtocol:
  3545. switch (status)
  3546. {
  3547. case STATUS_IO_TIMEOUT:
  3548. pSmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  3549. *pSmartcardExtension->IoRequest.Information = 0;
  3550. break;
  3551. case STATUS_SUCCESS:
  3552. // now indicate that we're in specific mode
  3553. pSmartcardExtension->ReaderCapabilities.CurrentState = SCARD_SPECIFIC;
  3554. // return the selected protocol to the caller
  3555. *(PULONG) pSmartcardExtension->IoRequest.ReplyBuffer =
  3556. pSmartcardExtension->CardCapabilities.Protocol.Selected;
  3557. *pSmartcardExtension->IoRequest.Information =
  3558. sizeof(pSmartcardExtension->CardCapabilities.Protocol.Selected);
  3559. SmartcardDebug(
  3560. DEBUG_PROTOCOL,
  3561. ( "%s!Selected protocol: T=%ld\n",
  3562. DRIVER_NAME,pSmartcardExtension->CardCapabilities.Protocol.Selected-1)
  3563. );
  3564. break;
  3565. default :
  3566. pSmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  3567. *pSmartcardExtension->IoRequest.Information = 0;
  3568. break;
  3569. }
  3570. SmartcardDebug(
  3571. DEBUG_TRACE,
  3572. ( "%s!SetProtocol : Exit %lx\n",
  3573. DRIVER_NAME,status)
  3574. );
  3575. return status;
  3576. }
  3577. /*****************************************************************************
  3578. Routine Description:
  3579. The smart card lib requires to have this function. It is called
  3580. to setup event tracking for card insertion and removal events.
  3581. Arguments:
  3582. pSmartcardExtension - pointer to the smart card data struct.
  3583. Return Value:
  3584. NTSTATUS
  3585. *****************************************************************************/
  3586. NTSTATUS
  3587. SCCMN50M_CardTracking(PSMARTCARD_EXTENSION pSmartcardExtension)
  3588. {
  3589. KIRQL oldIrql;
  3590. SmartcardDebug(
  3591. DEBUG_TRACE,
  3592. ( "%s!CardTracking: Enter\n",
  3593. DRIVER_NAME)
  3594. );
  3595. //
  3596. // Set cancel routine for the notification irp
  3597. //
  3598. IoAcquireCancelSpinLock(&oldIrql);
  3599. IoSetCancelRoutine(pSmartcardExtension->OsData->NotificationIrp,SCCMN50M_Cancel);
  3600. IoReleaseCancelSpinLock(oldIrql);
  3601. //
  3602. // Mark notification irp pending
  3603. //
  3604. IoMarkIrpPending(pSmartcardExtension->OsData->NotificationIrp);
  3605. SmartcardDebug(
  3606. DEBUG_TRACE,
  3607. ( "%s!CardTracking: Exit\n",
  3608. DRIVER_NAME)
  3609. );
  3610. return STATUS_PENDING;
  3611. }
  3612. /*****************************************************************************
  3613. Routine Description:
  3614. Arguments:
  3615. Return Value:
  3616. *****************************************************************************/
  3617. VOID
  3618. SCCMN50M_StopCardTracking(
  3619. IN PDEVICE_EXTENSION pDeviceExtension
  3620. )
  3621. {
  3622. PSMARTCARD_EXTENSION pSmartcardExtension = &pDeviceExtension->SmartcardExtension;
  3623. NTSTATUS status;
  3624. SmartcardDebug(
  3625. DEBUG_TRACE,
  3626. ( "%s!StopCardTracking: Enter\n",
  3627. DRIVER_NAME)
  3628. );
  3629. if (pSmartcardExtension->ReaderExtension->ThreadObjectPointer != NULL)
  3630. {
  3631. // kill thread
  3632. KeWaitForSingleObject(&pSmartcardExtension->ReaderExtension->CardManIOMutex,
  3633. Executive,
  3634. KernelMode,
  3635. FALSE,
  3636. NULL
  3637. );
  3638. pSmartcardExtension->ReaderExtension->TimeToTerminateThread = TRUE;
  3639. KeReleaseMutex(&pSmartcardExtension->ReaderExtension->CardManIOMutex,FALSE);
  3640. //
  3641. // Wait on the thread handle, when the wait is satisfied, the
  3642. // thread has gone away.
  3643. //
  3644. status = KeWaitForSingleObject(
  3645. pSmartcardExtension->ReaderExtension->ThreadObjectPointer,
  3646. Executive,
  3647. KernelMode,
  3648. FALSE,
  3649. NULL
  3650. );
  3651. pSmartcardExtension->ReaderExtension->ThreadObjectPointer = NULL;
  3652. }
  3653. SmartcardDebug(
  3654. DEBUG_TRACE,
  3655. ( "%s!StopCardTracking: Exit %lx\n",
  3656. DRIVER_NAME,
  3657. status)
  3658. );
  3659. }
  3660. /*****************************************************************************
  3661. Routine Description:
  3662. Arguments:
  3663. Return Value:
  3664. *****************************************************************************/
  3665. NTSTATUS
  3666. SCCMN50M_GetDeviceDescription (PSMARTCARD_EXTENSION pSmartcardExtension)
  3667. {
  3668. NTSTATUS status;
  3669. NTSTATUS DebugStatus;
  3670. ULONG ulBytesRead;
  3671. BYTE bByteRead;
  3672. ULONG i,j;
  3673. BYTE abReadBuffer[256];
  3674. ULONG ulPnPStringLength = 0;
  3675. ULONG ulExtend;
  3676. SmartcardDebug(
  3677. DEBUG_TRACE,
  3678. ( "%s!GetDeviceDescriptiong: Enter\n",
  3679. DRIVER_NAME)
  3680. );
  3681. // ===============================
  3682. // clear any pending errors
  3683. // ===============================
  3684. status = SCCMN50M_GetCommStatus(pSmartcardExtension);
  3685. if (!NT_SUCCESS(status))
  3686. {
  3687. SmartcardDebug(DEBUG_ERROR,("SCCMN50M_GetCommStatus failed ! status = %ld\n",status))
  3688. goto ExitGetDeviceDescription;
  3689. }
  3690. // =================================
  3691. // set baudrate for CardMan to 1200
  3692. // =================================
  3693. pSmartcardExtension->ReaderExtension->SerialConfigData.BaudRate.BaudRate = 1200;
  3694. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_BAUD_RATE;
  3695. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  3696. &pSmartcardExtension->ReaderExtension->SerialConfigData.BaudRate.BaudRate,
  3697. sizeof(SERIAL_BAUD_RATE));
  3698. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_BAUD_RATE);
  3699. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  3700. status = SCCMN50M_SerialIo(pSmartcardExtension);
  3701. if (!NT_SUCCESS(status))
  3702. {
  3703. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_BAUDRATE failed ! status = %ld\n",status))
  3704. goto ExitGetDeviceDescription;
  3705. }
  3706. // ===============================
  3707. // set comm timeouts
  3708. // ===============================
  3709. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadIntervalTimeout = DEFAULT_READ_INTERVAL_TIMEOUT;
  3710. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant = DEFAULT_READ_TOTAL_TIMEOUT_CONSTANT + 5000;
  3711. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutMultiplier = DEFAULT_READ_TOTAL_TIMEOUT_MULTIPLIER;
  3712. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.WriteTotalTimeoutConstant = DEFAULT_WRITE_TOTAL_TIMEOUT_CONSTANT;
  3713. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.WriteTotalTimeoutMultiplier = DEFAULT_WRITE_TOTAL_TIMEOUT_MULTIPLIER;
  3714. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_TIMEOUTS;
  3715. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  3716. &pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts,
  3717. sizeof(SERIAL_TIMEOUTS));
  3718. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_TIMEOUTS);
  3719. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  3720. status = SCCMN50M_SerialIo(pSmartcardExtension);
  3721. if (!NT_SUCCESS(status))
  3722. {
  3723. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_TIMEOUTS failed ! status = %x\n",status))
  3724. goto ExitGetDeviceDescription;
  3725. }
  3726. // ===============================
  3727. // set line control
  3728. // ===============================
  3729. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.StopBits = STOP_BITS_2;
  3730. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.Parity = NO_PARITY;
  3731. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.WordLength = 7;
  3732. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_LINE_CONTROL;
  3733. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  3734. &pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl,
  3735. sizeof(SERIAL_LINE_CONTROL));
  3736. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_LINE_CONTROL);
  3737. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  3738. status = SCCMN50M_SerialIo(pSmartcardExtension);
  3739. if (!NT_SUCCESS(status))
  3740. {
  3741. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_LINE_CONTROL failed ! status = %x\n",status))
  3742. goto ExitGetDeviceDescription;
  3743. }
  3744. // ===============================
  3745. // Set handflow
  3746. // ===============================
  3747. pSmartcardExtension->ReaderExtension->SerialConfigData.HandFlow.XonLimit = 0;
  3748. pSmartcardExtension->ReaderExtension->SerialConfigData.HandFlow.XoffLimit = 0;
  3749. pSmartcardExtension->ReaderExtension->SerialConfigData.HandFlow.FlowReplace = 0;
  3750. pSmartcardExtension->ReaderExtension->SerialConfigData.HandFlow.ControlHandShake = SERIAL_ERROR_ABORT | SERIAL_DTR_CONTROL;
  3751. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_HANDFLOW;
  3752. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  3753. &pSmartcardExtension->ReaderExtension->SerialConfigData.HandFlow,
  3754. sizeof(SERIAL_HANDFLOW));
  3755. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_HANDFLOW);
  3756. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  3757. status = SCCMN50M_SerialIo(pSmartcardExtension);
  3758. if (!NT_SUCCESS(status))
  3759. {
  3760. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_HANDFLOW failed ! status = %x\n",status))
  3761. goto ExitGetDeviceDescription;
  3762. }
  3763. // ===============================
  3764. // set purge mask
  3765. // ===============================
  3766. pSmartcardExtension->ReaderExtension->SerialConfigData.PurgeMask =
  3767. SERIAL_PURGE_TXABORT | SERIAL_PURGE_RXABORT |
  3768. SERIAL_PURGE_TXCLEAR | SERIAL_PURGE_RXCLEAR;
  3769. // ===============================
  3770. // clear RTS
  3771. // ===============================
  3772. status = SCCMN50M_ClearRTS(pSmartcardExtension);
  3773. if (!NT_SUCCESS(status))
  3774. {
  3775. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_RTS failed ! status = %x\n",status))
  3776. goto ExitGetDeviceDescription;
  3777. }
  3778. Wait(pSmartcardExtension,1);
  3779. // ===============================
  3780. // set DTR
  3781. // ===============================
  3782. status = SCCMN50M_SetDTR(pSmartcardExtension);
  3783. if (!NT_SUCCESS(status))
  3784. {
  3785. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_DRT failed ! status = %x\n",status))
  3786. goto ExitGetDeviceDescription;
  3787. }
  3788. Wait(pSmartcardExtension,1);
  3789. // ===============================
  3790. // set RTS
  3791. // ===============================
  3792. status = SCCMN50M_SetRTS(pSmartcardExtension);
  3793. if (!NT_SUCCESS(status))
  3794. {
  3795. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_RTS failed ! status = %x\n",status))
  3796. goto ExitGetDeviceDescription;
  3797. }
  3798. i=0;
  3799. while (1)
  3800. {
  3801. pSmartcardExtension->ReaderExtension->ToRHConfig = FALSE;
  3802. status = SCCMN50M_ReadCardMan(pSmartcardExtension,1,&ulBytesRead,&bByteRead,sizeof(bByteRead));
  3803. if (status == STATUS_SUCCESS)
  3804. {
  3805. abReadBuffer[i++] = bByteRead;
  3806. if (bByteRead == 0x29)
  3807. {
  3808. ulPnPStringLength = i;
  3809. break;
  3810. }
  3811. }
  3812. else
  3813. {
  3814. break;
  3815. }
  3816. }
  3817. if (ulPnPStringLength > 11 )
  3818. {
  3819. ulExtend = 0;
  3820. for (i=0;i<ulPnPStringLength;i++)
  3821. {
  3822. if (abReadBuffer[i] == 0x5C)
  3823. ulExtend++;
  3824. if (ulExtend == 4)
  3825. {
  3826. j = 0;
  3827. i++;
  3828. while (i < ulPnPStringLength - 3)
  3829. {
  3830. pSmartcardExtension->ReaderExtension->abDeviceDescription[j] = abReadBuffer[i];
  3831. i++;
  3832. j++;
  3833. }
  3834. SmartcardDebug(
  3835. DEBUG_DRIVER,
  3836. ( "%s!Device=%s\n",
  3837. pSmartcardExtension->ReaderExtension->abDeviceDescription)
  3838. );
  3839. break;
  3840. }
  3841. }
  3842. }
  3843. // ===================
  3844. // restore baud rate
  3845. // ===================
  3846. pSmartcardExtension->ReaderExtension->SerialConfigData.BaudRate.BaudRate = 38400;
  3847. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_BAUD_RATE;
  3848. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  3849. &pSmartcardExtension->ReaderExtension->SerialConfigData.BaudRate.BaudRate,
  3850. sizeof(SERIAL_BAUD_RATE));
  3851. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_BAUD_RATE);
  3852. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  3853. status = SCCMN50M_SerialIo(pSmartcardExtension);
  3854. if (!NT_SUCCESS(status))
  3855. {
  3856. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_BAUDRATE failed ! status = %ld\n",status))
  3857. goto ExitGetDeviceDescription;
  3858. }
  3859. // ====================
  3860. // retore line control
  3861. // ====================
  3862. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.StopBits = STOP_BITS_2;
  3863. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.Parity = EVEN_PARITY;
  3864. pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl.WordLength = 8;
  3865. pSmartcardExtension->ReaderExtension->SerialIoControlCode = IOCTL_SERIAL_SET_LINE_CONTROL;
  3866. RtlCopyMemory(pSmartcardExtension->SmartcardRequest.Buffer,
  3867. &pSmartcardExtension->ReaderExtension->SerialConfigData.LineControl,
  3868. sizeof(SERIAL_LINE_CONTROL));
  3869. pSmartcardExtension->SmartcardRequest.BufferLength = sizeof(SERIAL_LINE_CONTROL);
  3870. pSmartcardExtension->SmartcardReply.BufferLength = 0;
  3871. status = SCCMN50M_SerialIo(pSmartcardExtension);
  3872. if (!NT_SUCCESS(status))
  3873. {
  3874. SmartcardDebug(DEBUG_ERROR,("IOCTL_SERIAL_SET_LINE_CONTROL failed ! status = %x\n",status))
  3875. goto ExitGetDeviceDescription;
  3876. }
  3877. ExitGetDeviceDescription:
  3878. DebugStatus = SCCMN50M_ResyncCardManII(pSmartcardExtension);
  3879. if (status != STATUS_SUCCESS)
  3880. { // map all errors to STATUS_UNSUCCESSFULL;
  3881. status = STATUS_UNSUCCESSFUL;
  3882. }
  3883. SmartcardDebug(
  3884. DEBUG_TRACE,
  3885. ( "%s!GetDeviceDescriptiong: Exit %lx\n",
  3886. DRIVER_NAME,status)
  3887. );
  3888. return status;
  3889. }
  3890. /*****************************************************************************
  3891. Routine Description:
  3892. Arguments:
  3893. Return Value:
  3894. *****************************************************************************/
  3895. NTSTATUS
  3896. SCCMN50M_SetSyncParameters(IN PSMARTCARD_EXTENSION pSmartcardExtension )
  3897. {
  3898. NTSTATUS status = STATUS_SUCCESS;
  3899. SmartcardDebug(
  3900. DEBUG_TRACE,
  3901. ( "%s!SetSyncParameters: Enter\n",
  3902. DRIVER_NAME)
  3903. );
  3904. //DBGBreakPoint();
  3905. pSmartcardExtension->ReaderExtension->SyncParameters.ulProtocol =
  3906. ((PSYNC_PARAMETERS)pSmartcardExtension->IoRequest.RequestBuffer)->ulProtocol;
  3907. pSmartcardExtension->ReaderExtension->SyncParameters.ulStateResetLineWhileReading =
  3908. ((PSYNC_PARAMETERS)pSmartcardExtension->IoRequest.RequestBuffer)->ulStateResetLineWhileReading;
  3909. pSmartcardExtension->ReaderExtension->SyncParameters.ulStateResetLineWhileWriting =
  3910. ((PSYNC_PARAMETERS)pSmartcardExtension->IoRequest.RequestBuffer)->ulStateResetLineWhileWriting;
  3911. pSmartcardExtension->ReaderExtension->SyncParameters.ulWriteDummyClocks =
  3912. ((PSYNC_PARAMETERS)pSmartcardExtension->IoRequest.RequestBuffer)->ulWriteDummyClocks;
  3913. pSmartcardExtension->ReaderExtension->SyncParameters.ulHeaderLen =
  3914. ((PSYNC_PARAMETERS)pSmartcardExtension->IoRequest.RequestBuffer)->ulHeaderLen;
  3915. // Used for the 2 Wire Protocol. We must make a Card reset after Power On
  3916. pSmartcardExtension->ReaderExtension->SyncParameters.fCardResetRequested = TRUE;
  3917. // return length of reply
  3918. *pSmartcardExtension->IoRequest.Information = 0L;
  3919. SmartcardDebug(
  3920. DEBUG_TRACE,
  3921. ( "%s!SetSyncParameters: Exit\n",
  3922. DRIVER_NAME)
  3923. );
  3924. return status;
  3925. }
  3926. /*****************************************************************************
  3927. Routine Description:
  3928. Arguments:
  3929. Return Value:
  3930. *****************************************************************************/
  3931. UCHAR
  3932. SCCMN50M_CalcTxControlByte (IN PSMARTCARD_EXTENSION pSmartcardExtension,
  3933. IN ULONG ulBitsToWrite )
  3934. {
  3935. UCHAR bTxControlByte = 0;
  3936. if (pSmartcardExtension->ReaderExtension->SyncParameters.ulProtocol == SCARD_PROTOCOL_2WBP)
  3937. {
  3938. bTxControlByte = CLOCK_FORCED_2WBP;
  3939. }
  3940. else
  3941. {
  3942. if (ulBitsToWrite >= 255 * 8)
  3943. bTxControlByte |= TRANSMIT_A8;
  3944. if (pSmartcardExtension->ReaderExtension->SyncParameters.ulStateResetLineWhileWriting ==
  3945. SCARD_RESET_LINE_HIGH)
  3946. bTxControlByte |= SYNC_RESET_LINE_HIGH;
  3947. }
  3948. bTxControlByte |= (BYTE)((ulBitsToWrite-1) & 0x00000007);
  3949. return bTxControlByte;
  3950. }
  3951. //---------------------------------------------------------------------------
  3952. //
  3953. //---------------------------------------------------------------------------
  3954. /*****************************************************************************
  3955. Routine Description:
  3956. Arguments:
  3957. Return Value:
  3958. *****************************************************************************/
  3959. UCHAR
  3960. SCCMN50M_CalcTxLengthByte (IN PSMARTCARD_EXTENSION pSmartcardExtension,
  3961. IN ULONG ulBitsToWrite )
  3962. {
  3963. UCHAR bTxLengthByte = 0;
  3964. bTxLengthByte = (BYTE)( ((ulBitsToWrite - 1) >> 3) + 1);
  3965. return bTxLengthByte;
  3966. }
  3967. //---------------------------------------------------------------------------
  3968. //
  3969. //---------------------------------------------------------------------------
  3970. /*****************************************************************************
  3971. Routine Description:
  3972. Arguments:
  3973. Return Value:
  3974. *****************************************************************************/
  3975. UCHAR
  3976. SCCMN50M_CalcRxControlByte (IN PSMARTCARD_EXTENSION pSmartcardExtension,
  3977. IN ULONG ulBitsToRead )
  3978. {
  3979. UCHAR bRxControlByte = 0;
  3980. if (pSmartcardExtension->ReaderExtension->SyncParameters.ulStateResetLineWhileReading ==
  3981. SCARD_RESET_LINE_HIGH)
  3982. bRxControlByte |= SYNC_RESET_LINE_HIGH;
  3983. if (ulBitsToRead == 0)
  3984. {
  3985. ulBitsToRead = pSmartcardExtension->ReaderExtension->SyncParameters.ulWriteDummyClocks;
  3986. bRxControlByte |= SYNC_DUMMY_RECEIVE;
  3987. }
  3988. if (ulBitsToRead > 255 * 8)
  3989. bRxControlByte |= RECEIVE_A8;
  3990. bRxControlByte |= (BYTE)( (ulBitsToRead-1) & 0x00000007);
  3991. return bRxControlByte;
  3992. }
  3993. //----------------------------------------------------------------------------
  3994. //
  3995. //----------------------------------------------------------------------------
  3996. /*****************************************************************************
  3997. Routine Description:
  3998. Arguments:
  3999. Return Value:
  4000. *****************************************************************************/
  4001. UCHAR
  4002. SCCMN50M_CalcRxLengthByte (IN PSMARTCARD_EXTENSION pSmartcardExtension,
  4003. IN ULONG ulBitsToRead )
  4004. {
  4005. UCHAR bRxLengthByte = 0;
  4006. // if (pSmartcardExtension->ReaderExtension->SyncParameters.ulProtocol == SCARD_PROTOCOL_3WBP)
  4007. // {
  4008. if (ulBitsToRead == 0)
  4009. ulBitsToRead = pSmartcardExtension->ReaderExtension->SyncParameters.ulWriteDummyClocks;
  4010. bRxLengthByte = (BYTE)( ((ulBitsToRead - 1) >> 3) + 1);
  4011. // }
  4012. return bRxLengthByte;
  4013. }
  4014. /*****************************************************************************
  4015. Routine Description:
  4016. Arguments:
  4017. Return Value:
  4018. *****************************************************************************/
  4019. NTSTATUS
  4020. SCCMN50M_SyncCardPowerOn (
  4021. IN PSMARTCARD_EXTENSION pSmartcardExtension
  4022. )
  4023. {
  4024. NTSTATUS status;
  4025. UCHAR pbAtrBuffer[MAXIMUM_ATR_LENGTH];
  4026. UCHAR abSyncAtrBuffer[MAXIMUM_ATR_LENGTH];
  4027. ULONG ulAtrLength = 0;
  4028. SmartcardDebug(
  4029. DEBUG_TRACE,
  4030. ( "%s!SyncCardPowerOn: Enter\n",
  4031. DRIVER_NAME)
  4032. );
  4033. status = SCCMN50M_UseSyncStrategy(pSmartcardExtension,
  4034. &ulAtrLength,
  4035. pbAtrBuffer,
  4036. sizeof(pbAtrBuffer));
  4037. abSyncAtrBuffer[0] = 0x3B;
  4038. abSyncAtrBuffer[1] = 0x04;
  4039. MemCpy(&abSyncAtrBuffer[2],
  4040. sizeof(abSyncAtrBuffer)-2,
  4041. pbAtrBuffer,
  4042. ulAtrLength);
  4043. ulAtrLength += 2;
  4044. MemCpy(pSmartcardExtension->CardCapabilities.ATR.Buffer,
  4045. sizeof(pSmartcardExtension->CardCapabilities.ATR.Buffer),
  4046. abSyncAtrBuffer,
  4047. ulAtrLength);
  4048. pSmartcardExtension->CardCapabilities.ATR.Length = (UCHAR)(ulAtrLength);
  4049. pSmartcardExtension->ReaderCapabilities.CurrentState = SCARD_SPECIFIC;
  4050. pSmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_T0;
  4051. pSmartcardExtension->ReaderExtension->SyncParameters.fCardResetRequested = TRUE;
  4052. pSmartcardExtension->ReaderExtension->SyncParameters.fCardPowerRequested = FALSE;
  4053. SmartcardDebug(DEBUG_ATR,("ATR of synchronous smart card : %2.2x %2.2x %2.2x %2.2x\n",
  4054. pbAtrBuffer[0],pbAtrBuffer[1],pbAtrBuffer[2],pbAtrBuffer[3]));
  4055. SmartcardDebug(
  4056. DEBUG_TRACE,
  4057. ( "%s!SyncCardPowerOn: Exit %lx\n",
  4058. DRIVER_NAME,status)
  4059. );
  4060. return status;
  4061. }
  4062. /*****************************************************************************
  4063. Routine Description:
  4064. Arguments:
  4065. Return Value:
  4066. *****************************************************************************/
  4067. NTSTATUS
  4068. SCCMN50M_Transmit2WBP(PSMARTCARD_EXTENSION pSmartcardExtension)
  4069. {
  4070. NTSTATUS status = STATUS_SUCCESS;
  4071. UCHAR bWriteBuffer [128];
  4072. UCHAR bReadBuffer [128];
  4073. UCHAR bTxControlByte;
  4074. UCHAR bTxLengthByte;
  4075. UCHAR bRxControlByte;
  4076. UCHAR bRxLengthByte;
  4077. ULONG ulBytesToWrite;
  4078. ULONG ulBytesToRead;
  4079. ULONG ulBitsToWrite;
  4080. ULONG ulBitsToRead;
  4081. ULONG ulBytesRead;
  4082. // ULONG ulBitsRead;
  4083. ULONG ulBytesStillToRead;
  4084. ULONG ulMaxIFSD;
  4085. PCHAR pbInData;
  4086. SmartcardDebug(
  4087. DEBUG_TRACE,
  4088. ( "%s!Transmit2WBP: Enter\n",
  4089. DRIVER_NAME)
  4090. );
  4091. /*-----------------------------------------------------------------------*/
  4092. /** Power smartcard - if smartcard was removed and reinserted **/
  4093. /*-----------------------------------------------------------------------*/
  4094. if (pSmartcardExtension->ReaderExtension->SyncParameters.fCardPowerRequested == TRUE)
  4095. {
  4096. status = SCCMN50M_SyncCardPowerOn (pSmartcardExtension);
  4097. if (status != STATUS_SUCCESS)
  4098. {
  4099. goto ExitTransmit2WBP;
  4100. }
  4101. }
  4102. pbInData = pSmartcardExtension->IoRequest.RequestBuffer + sizeof(SYNC_TRANSFER);
  4103. ulBitsToWrite = ((PSYNC_TRANSFER)(pSmartcardExtension->IoRequest.RequestBuffer))->ulSyncBitsToWrite;
  4104. ulBitsToRead = ((PSYNC_TRANSFER)(pSmartcardExtension->IoRequest.RequestBuffer))->ulSyncBitsToRead;
  4105. ulBytesToWrite = ulBitsToWrite/8;
  4106. ulBytesToRead = ulBitsToRead/8 + (ulBitsToRead % 8 ? 1 : 0);
  4107. /*-----------------------------------------------------------------------*/
  4108. // check buffer sizes
  4109. /*-----------------------------------------------------------------------*/
  4110. ulMaxIFSD = ATTR_MAX_IFSD_CARDMAN_II;
  4111. if (ulBytesToRead > ulMaxIFSD ||
  4112. ulBytesToRead > pSmartcardExtension->SmartcardReply.BufferSize)
  4113. {
  4114. status = STATUS_BUFFER_OVERFLOW;
  4115. goto ExitTransmit2WBP;
  4116. }
  4117. if (ulBytesToWrite > pSmartcardExtension->SmartcardRequest.BufferSize)
  4118. {
  4119. status = STATUS_BUFFER_OVERFLOW;
  4120. goto ExitTransmit2WBP;
  4121. }
  4122. pSmartcardExtension->SmartcardRequest.BufferLength = ulBytesToWrite+1;
  4123. /*-----------------------------------------------------------------------*/
  4124. // copy data to the Smartcard Request Buffer
  4125. /*-----------------------------------------------------------------------*/
  4126. (pSmartcardExtension->SmartcardRequest.Buffer)[0] = '\x0F';
  4127. MemCpy((pSmartcardExtension->SmartcardRequest.Buffer+1),
  4128. pSmartcardExtension->SmartcardRequest.BufferSize,
  4129. pbInData,
  4130. ulBytesToWrite);
  4131. /*-----------------------------------------------------------------------*/
  4132. // copy data to the write buffer
  4133. /*-----------------------------------------------------------------------*/
  4134. MemCpy((bWriteBuffer),
  4135. sizeof(bWriteBuffer),
  4136. pSmartcardExtension->SmartcardRequest.Buffer,
  4137. (ulBytesToWrite+1));
  4138. /*-----------------------------------------------------------------------*/
  4139. // set SYNC protocol flag for CardMan
  4140. /*-----------------------------------------------------------------------*/
  4141. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_SYN);
  4142. /*-----------------------------------------------------------------------*/
  4143. // Header
  4144. /*-----------------------------------------------------------------------*/
  4145. if (pSmartcardExtension->ReaderExtension->SyncParameters.fCardResetRequested == TRUE)
  4146. {
  4147. status = SCCMN50M_ResetCard2WBP(pSmartcardExtension);
  4148. if (NT_ERROR(status))
  4149. {
  4150. goto ExitTransmit2WBP;
  4151. }
  4152. pSmartcardExtension->ReaderExtension->SyncParameters.fCardResetRequested = FALSE;
  4153. }
  4154. /*-----------------------------------------------------------------------*/
  4155. // 1. Send Carman-Header 4-Byte
  4156. // 2. Send 0x0F, that builds a HIGH-LOW Edge for 4432 CC
  4157. // 3. Send the Data (CC command = 3 Byte)
  4158. /*-----------------------------------------------------------------------*/
  4159. bTxControlByte = SCCMN50M_CalcTxControlByte(pSmartcardExtension,ulBitsToWrite);
  4160. bTxLengthByte = (BYTE)(SCCMN50M_CalcTxLengthByte(pSmartcardExtension,ulBitsToWrite)+1);
  4161. bRxControlByte = 0;
  4162. bRxLengthByte = 0;
  4163. SCCMN50M_SetCardManHeader(pSmartcardExtension,
  4164. bTxControlByte,
  4165. bTxLengthByte,
  4166. bRxControlByte,
  4167. bRxLengthByte);
  4168. /*-----------------------------------------------------------------------*/
  4169. // write data to card
  4170. /*-----------------------------------------------------------------------*/
  4171. status = SCCMN50M_WriteCardMan (pSmartcardExtension,
  4172. (ulBytesToWrite+1),
  4173. bWriteBuffer);
  4174. if (NT_ERROR(status))
  4175. {
  4176. goto ExitTransmit2WBP;
  4177. }
  4178. /*-----------------------------------------------------------------------*/
  4179. // read CardMan Header
  4180. // no Data from CC received
  4181. /*-----------------------------------------------------------------------*/
  4182. pSmartcardExtension->ReaderExtension->ToRHConfig = FALSE;
  4183. status = SCCMN50M_ReadCardMan(pSmartcardExtension,
  4184. 2,
  4185. &ulBytesRead,
  4186. bReadBuffer,
  4187. sizeof(bReadBuffer));
  4188. if (NT_ERROR(status))
  4189. {
  4190. goto ExitTransmit2WBP;
  4191. }
  4192. /*-----------------------------------------------------------------------*/
  4193. // 1. Send Carman-Header 4-Byte
  4194. // 2. Send 0xF0, that builds a LOW-HIGH Edge for 4432 CC
  4195. // 3. Now the receiviing of card-data begins
  4196. /*-----------------------------------------------------------------------*/
  4197. bTxControlByte = SCCMN50M_CalcTxControlByte(pSmartcardExtension, 8);
  4198. bTxLengthByte = SCCMN50M_CalcTxLengthByte(pSmartcardExtension, 8);
  4199. bRxControlByte = SCCMN50M_CalcRxControlByte(pSmartcardExtension,ulBitsToRead);
  4200. bRxLengthByte = SCCMN50M_CalcRxLengthByte(pSmartcardExtension,ulBitsToRead);
  4201. SCCMN50M_SetCardManHeader(pSmartcardExtension,
  4202. bTxControlByte,
  4203. bTxLengthByte,
  4204. bRxControlByte,
  4205. bRxLengthByte);
  4206. /*-----------------------------------------------------------------------*/
  4207. // in this sequnce SCCMN50M_WriteCardMan must not send the Config string.
  4208. // write 0xF0 -> is the trigger to read data from card or start the
  4209. // processing.
  4210. /*-----------------------------------------------------------------------*/
  4211. pSmartcardExtension->ReaderExtension->NoConfig = TRUE;
  4212. status = SCCMN50M_WriteCardMan (pSmartcardExtension,
  4213. 1, // one byte to write
  4214. "\xF0"); // LOW-HIGH - Edge
  4215. if (NT_ERROR(status))
  4216. {
  4217. goto ExitTransmit2WBP;
  4218. }
  4219. /*-----------------------------------------------------------------------*/
  4220. // read CardMan Header
  4221. // Data from CC received will be received
  4222. /*-----------------------------------------------------------------------*/
  4223. pSmartcardExtension->ReaderExtension->ToRHConfig = FALSE;
  4224. status = SCCMN50M_ReadCardMan(pSmartcardExtension,
  4225. 2,
  4226. &ulBytesRead,
  4227. bReadBuffer,
  4228. sizeof(bReadBuffer));
  4229. if (NT_ERROR(status))
  4230. {
  4231. goto ExitTransmit2WBP;
  4232. }
  4233. /*-----------------------------------------------------------------------*/
  4234. // Read the data string
  4235. /*-----------------------------------------------------------------------*/
  4236. ulBytesStillToRead = (ULONG)bReadBuffer[1];
  4237. if (bReadBuffer[0] & RECEIVE_A8)
  4238. ulBytesStillToRead += 256;
  4239. status = SCCMN50M_ReadCardMan(pSmartcardExtension,
  4240. ulBytesStillToRead,
  4241. &ulBytesRead,
  4242. bReadBuffer,
  4243. sizeof(bReadBuffer));
  4244. if (NT_ERROR(status))
  4245. {
  4246. goto ExitTransmit2WBP;
  4247. }
  4248. /*-----------------------------------------------------------------------*/
  4249. /** calculate data length in bits - this value is not used **/
  4250. /*-----------------------------------------------------------------------*/
  4251. // ulBitsRead = ((ulBytesRead-1) * 8) + ((ulBitsToRead-1 ) & 0x00000007) + 1;
  4252. /*-----------------------------------------------------------------------*/
  4253. /** shift the bits in the last byte to the correct position **/
  4254. /*-----------------------------------------------------------------------*/
  4255. bReadBuffer[ulBytesRead-1] >>= (7 - ((ulBitsToRead-1) & 0x00000007));
  4256. /*-----------------------------------------------------------------------*/
  4257. // the first bit of the returned string is lost
  4258. // so we must shift the whole data string one bit left
  4259. // the first bit of the first data byte is lost while reading
  4260. // this bit maybe incorrect
  4261. /*-----------------------------------------------------------------------*/
  4262. SCCMN50M_Shift_Msg(bReadBuffer, ulBytesRead);
  4263. /*-----------------------------------------------------------------------*/
  4264. // copy received bytes to Smartcard Reply Buffer
  4265. /*-----------------------------------------------------------------------*/
  4266. MemCpy(pSmartcardExtension->SmartcardReply.Buffer,
  4267. pSmartcardExtension->SmartcardReply.BufferSize,
  4268. bReadBuffer,
  4269. ulBytesRead);
  4270. pSmartcardExtension->SmartcardReply.BufferLength = ulBytesRead;
  4271. /*-----------------------------------------------------------------------*/
  4272. // copy received bytes to IoReply Buffer
  4273. /*-----------------------------------------------------------------------*/
  4274. MemCpy(pSmartcardExtension->IoRequest.ReplyBuffer,
  4275. pSmartcardExtension->IoRequest.ReplyBufferLength,
  4276. pSmartcardExtension->SmartcardReply.Buffer,
  4277. ulBytesRead);
  4278. *(pSmartcardExtension->IoRequest.Information) = ulBytesRead;
  4279. ExitTransmit2WBP:
  4280. SmartcardDebug(
  4281. DEBUG_TRACE,
  4282. ( "%s!Transmit2WBP: Exit\n",
  4283. DRIVER_NAME,status)
  4284. );
  4285. return status;
  4286. }
  4287. /*****************************************************************************
  4288. Routine Description:
  4289. Arguments:
  4290. Return Value:
  4291. *****************************************************************************/
  4292. VOID
  4293. SCCMN50M_Shift_Msg (PUCHAR pbBuffer,
  4294. ULONG ulMsgLen)
  4295. {
  4296. UCHAR bTmp1, bTmp2;
  4297. int i;
  4298. for (i=(int)ulMsgLen-1; i>=0; i--)
  4299. {
  4300. bTmp1=(BYTE)((pbBuffer[i] >> 7) & 0x01); /* bTmp1 = bit 7 naechstes byte */
  4301. if (i+1 != (int)ulMsgLen)
  4302. {
  4303. bTmp2=(BYTE)((pbBuffer[i+1] << 1) | bTmp1);
  4304. pbBuffer[i+1] = bTmp2;
  4305. }
  4306. }
  4307. pbBuffer[0] = (BYTE)(pbBuffer[0] << 1);
  4308. return;
  4309. }
  4310. /*****************************************************************************
  4311. Routine Description:
  4312. Reset Card 4442
  4313. Arguments:
  4314. Return Value:
  4315. *****************************************************************************/
  4316. NTSTATUS
  4317. SCCMN50M_ResetCard2WBP(IN PSMARTCARD_EXTENSION pSmartcardExtension)
  4318. {
  4319. NTSTATUS status = STATUS_SUCCESS;
  4320. BYTE bBuffer[10];
  4321. ULONG ulBytesRead;
  4322. /*-----------------------------------------------------------------------*/
  4323. // Enter Card Reset
  4324. /*-----------------------------------------------------------------------*/
  4325. SmartcardDebug(
  4326. DEBUG_TRACE,
  4327. ( "%s!ResetCard2WBP: Enter\n",
  4328. DRIVER_NAME)
  4329. );
  4330. /*-----------------------------------------------------------------------*/
  4331. // bTxControlByte = 0;
  4332. // bTxLengthByte = 0;
  4333. // bRxControlByte = RESET_CARD;
  4334. // bRxLengthByte = 5;
  4335. /*-----------------------------------------------------------------------*/
  4336. SCCMN50M_SetCardManHeader(pSmartcardExtension,
  4337. 0, // bTxControlByte
  4338. 0, // bTxLengthByte
  4339. COLD_RESET, // bRxControlByte
  4340. 5); // bRxLengthByte
  4341. /*-----------------------------------------------------------------------*/
  4342. // write data to card
  4343. /*-----------------------------------------------------------------------*/
  4344. status = SCCMN50M_WriteCardMan (pSmartcardExtension,
  4345. 0,
  4346. NULL);
  4347. if (NT_ERROR(status))
  4348. {
  4349. goto ExitResetCard2WBP;
  4350. }
  4351. /*-----------------------------------------------------------------------*/
  4352. // read CardMan Header
  4353. /*-----------------------------------------------------------------------*/
  4354. pSmartcardExtension->ReaderExtension->ToRHConfig = FALSE;
  4355. status = SCCMN50M_ReadCardMan(pSmartcardExtension,
  4356. 2, &ulBytesRead, bBuffer, sizeof(bBuffer));
  4357. if (NT_ERROR(status))
  4358. {
  4359. goto ExitResetCard2WBP;
  4360. }
  4361. if (bBuffer[1] != 5)
  4362. {
  4363. status = !STATUS_SUCCESS;
  4364. goto ExitResetCard2WBP;
  4365. }
  4366. /*-----------------------------------------------------------------------*/
  4367. // read ATR
  4368. /*-----------------------------------------------------------------------*/
  4369. status = SCCMN50M_ReadCardMan(pSmartcardExtension,
  4370. 5, &ulBytesRead, bBuffer, sizeof(bBuffer));
  4371. if (NT_ERROR(status))
  4372. {
  4373. goto ExitResetCard2WBP;
  4374. }
  4375. SmartcardDebug(DEBUG_ATR,("%s!Card Reset ATR : %02x %02x %02x %02x\n",
  4376. DRIVER_NAME,bBuffer[0],bBuffer[1],bBuffer[2],bBuffer[3]));
  4377. ExitResetCard2WBP:
  4378. SmartcardDebug(
  4379. DEBUG_TRACE,
  4380. ( "%s!ResetCard2WBP: Exit %lx\n",
  4381. DRIVER_NAME,status)
  4382. );
  4383. return status;
  4384. }
  4385. /*****************************************************************************
  4386. Routine Description:
  4387. Arguments:
  4388. Return Value:
  4389. *****************************************************************************/
  4390. NTSTATUS
  4391. SCCMN50M_Transmit3WBP(PSMARTCARD_EXTENSION pSmartcardExtension)
  4392. {
  4393. NTSTATUS status = STATUS_SUCCESS;
  4394. UCHAR bWriteBuffer [128];
  4395. UCHAR bReadBuffer [128];
  4396. UCHAR bTxControlByte;
  4397. UCHAR bTxLengthByte;
  4398. UCHAR bRxControlByte;
  4399. UCHAR bRxLengthByte;
  4400. ULONG ulBytesToWrite;
  4401. ULONG ulBytesToRead;
  4402. ULONG ulBitsToWrite;
  4403. ULONG ulBitsToRead;
  4404. ULONG ulBytesRead;
  4405. // ULONG ulBitsRead;
  4406. ULONG ulBytesStillToRead;
  4407. ULONG ulMaxIFSD;
  4408. PCHAR pbInData;
  4409. SmartcardDebug(
  4410. DEBUG_TRACE,
  4411. ( "%s!Transmit3WBP: Enter\n",
  4412. DRIVER_NAME)
  4413. );
  4414. // DBGBreakPoint();
  4415. /*-----------------------------------------------------------------------*/
  4416. /** Power smartcard - if smartcard was removed and reinserted **/
  4417. /*-----------------------------------------------------------------------*/
  4418. if (pSmartcardExtension->ReaderExtension->SyncParameters.fCardPowerRequested == TRUE)
  4419. {
  4420. status = SCCMN50M_SyncCardPowerOn (pSmartcardExtension);
  4421. if (status != STATUS_SUCCESS)
  4422. {
  4423. goto ExitTransmit3WBP;
  4424. }
  4425. }
  4426. pbInData = pSmartcardExtension->IoRequest.RequestBuffer + sizeof(SYNC_TRANSFER);
  4427. ulBitsToWrite = ((PSYNC_TRANSFER)(pSmartcardExtension->IoRequest.RequestBuffer))->ulSyncBitsToWrite;
  4428. ulBitsToRead = ((PSYNC_TRANSFER)(pSmartcardExtension->IoRequest.RequestBuffer))->ulSyncBitsToRead;
  4429. ulBytesToWrite = ulBitsToWrite/8;
  4430. ulBytesToRead = ulBitsToRead/8 + (ulBitsToRead % 8 ? 1 : 0);
  4431. /*-----------------------------------------------------------------------*/
  4432. // check buffer sizes
  4433. /*-----------------------------------------------------------------------*/
  4434. ulMaxIFSD = ATTR_MAX_IFSD_CARDMAN_II;
  4435. if (ulBytesToRead > ulMaxIFSD ||
  4436. ulBytesToRead > pSmartcardExtension->SmartcardReply.BufferSize)
  4437. {
  4438. status = STATUS_BUFFER_OVERFLOW;
  4439. goto ExitTransmit3WBP;
  4440. }
  4441. if (ulBytesToWrite > pSmartcardExtension->SmartcardRequest.BufferSize)
  4442. {
  4443. status = STATUS_BUFFER_OVERFLOW;
  4444. goto ExitTransmit3WBP;
  4445. }
  4446. pSmartcardExtension->SmartcardRequest.BufferLength = ulBytesToWrite;
  4447. /*-----------------------------------------------------------------------*/
  4448. // copy data to the Smartcard Request Buffer
  4449. /*-----------------------------------------------------------------------*/
  4450. MemCpy(pSmartcardExtension->SmartcardRequest.Buffer,
  4451. pSmartcardExtension->SmartcardRequest.BufferSize,
  4452. pbInData,
  4453. ulBytesToWrite);
  4454. /*-----------------------------------------------------------------------*/
  4455. // copy data to the write buffer
  4456. /*-----------------------------------------------------------------------*/
  4457. MemCpy(bWriteBuffer,
  4458. sizeof(bWriteBuffer),
  4459. pSmartcardExtension->SmartcardRequest.Buffer,
  4460. ulBytesToWrite);
  4461. /*-----------------------------------------------------------------------*/
  4462. // set SYNC protocol flag for CardMan
  4463. /*-----------------------------------------------------------------------*/
  4464. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_SYN);
  4465. /*-----------------------------------------------------------------------*/
  4466. // build cardman header
  4467. /*-----------------------------------------------------------------------*/
  4468. bTxControlByte = SCCMN50M_CalcTxControlByte(pSmartcardExtension,ulBitsToWrite);
  4469. bTxLengthByte = SCCMN50M_CalcTxLengthByte(pSmartcardExtension,ulBitsToWrite);
  4470. bRxControlByte = SCCMN50M_CalcRxControlByte(pSmartcardExtension,ulBitsToRead);
  4471. bRxLengthByte = SCCMN50M_CalcRxLengthByte(pSmartcardExtension,ulBitsToRead);
  4472. SCCMN50M_SetCardManHeader(pSmartcardExtension,
  4473. bTxControlByte,
  4474. bTxLengthByte,
  4475. bRxControlByte,
  4476. bRxLengthByte);
  4477. /*-----------------------------------------------------------------------*/
  4478. /** write data to card **/
  4479. /*-----------------------------------------------------------------------*/
  4480. status = SCCMN50M_WriteCardMan (pSmartcardExtension,
  4481. ulBytesToWrite,
  4482. bWriteBuffer);
  4483. if (NT_ERROR(status))
  4484. {
  4485. goto ExitTransmit3WBP;
  4486. }
  4487. /*-----------------------------------------------------------------------*/
  4488. /** read CardMan Header **/
  4489. /*-----------------------------------------------------------------------*/
  4490. pSmartcardExtension->ReaderExtension->ToRHConfig = FALSE;
  4491. status = SCCMN50M_ReadCardMan(pSmartcardExtension,2,&ulBytesRead,bReadBuffer,sizeof(bReadBuffer));
  4492. if (NT_ERROR(status))
  4493. {
  4494. goto ExitTransmit3WBP;
  4495. }
  4496. /*-----------------------------------------------------------------------*/
  4497. // calc data length to receive
  4498. /*-----------------------------------------------------------------------*/
  4499. ulBytesStillToRead = (ULONG)(bReadBuffer[1]);
  4500. if (bReadBuffer[0] & RECEIVE_A8)
  4501. ulBytesStillToRead += 256;
  4502. // read data from card
  4503. status = SCCMN50M_ReadCardMan(pSmartcardExtension,
  4504. ulBytesStillToRead,
  4505. &ulBytesRead,
  4506. bReadBuffer,
  4507. sizeof(bReadBuffer));
  4508. if (NT_ERROR(status))
  4509. {
  4510. goto ExitTransmit3WBP;
  4511. }
  4512. /*-----------------------------------------------------------------------*/
  4513. // calculate data length in bits - this value is not used
  4514. /*-----------------------------------------------------------------------*/
  4515. // ulBitsRead = ((ulBytesRead-1) * 8) + ((ulBitsToRead-1 ) & 0x00000007) + 1;
  4516. /*-----------------------------------------------------------------------*/
  4517. // shift the bits in the last byte to the correct position
  4518. /*-----------------------------------------------------------------------*/
  4519. bReadBuffer[ulBytesRead-1] >>= (7 - ((ulBitsToRead-1) & 0x00000007));
  4520. /*-----------------------------------------------------------------------*/
  4521. // copy received bytes to Smartcard Reply Buffer
  4522. /*-----------------------------------------------------------------------*/
  4523. MemCpy(pSmartcardExtension->SmartcardReply.Buffer,
  4524. pSmartcardExtension->SmartcardReply.BufferSize,
  4525. bReadBuffer,
  4526. ulBytesRead);
  4527. pSmartcardExtension->SmartcardReply.BufferLength = ulBytesRead;
  4528. /*-----------------------------------------------------------------------*/
  4529. // copy received bytes to IoReply Buffer
  4530. // this Memcpy should respond to SmartcardRawReply function
  4531. /*-----------------------------------------------------------------------*/
  4532. MemCpy(pSmartcardExtension->IoRequest.ReplyBuffer,
  4533. pSmartcardExtension->IoRequest.ReplyBufferLength,
  4534. pSmartcardExtension->SmartcardReply.Buffer,
  4535. ulBytesRead);
  4536. *pSmartcardExtension->IoRequest.Information = ulBytesRead;
  4537. ExitTransmit3WBP:
  4538. SmartcardDebug(
  4539. DEBUG_TRACE,
  4540. ( "%s!Transmit3WBP: Exit %lx\n",
  4541. DRIVER_NAME,status)
  4542. );
  4543. return status;
  4544. }
  4545. /*****************************************************************************
  4546. Routine Description:
  4547. This function powers a synchronous smart card.
  4548. Arguments:
  4549. Return Value:
  4550. *****************************************************************************/
  4551. NTSTATUS
  4552. SCCMN50M_UseSyncStrategy (
  4553. IN PSMARTCARD_EXTENSION pSmartcardExtension,
  4554. OUT PULONG pulAtrLength,
  4555. OUT PUCHAR pbAtrBuffer,
  4556. IN ULONG ulAtrBufferSize
  4557. )
  4558. {
  4559. NTSTATUS status;
  4560. NTSTATUS DebugStatus;
  4561. UCHAR bReadBuffer[SCARD_ATR_LENGTH];
  4562. ULONG ulBytesRead;
  4563. SmartcardDebug(
  4564. DEBUG_TRACE,
  4565. ( "%s!UseSyncStrategy: Enter\n",
  4566. DRIVER_NAME)
  4567. );
  4568. //DBGBreakPoint();
  4569. SCCMN50M_SetCardManHeader(pSmartcardExtension,0,0,SYNC_ATR_RX_CONTROL,ATR_LEN_SYNC);
  4570. pSmartcardExtension->ReaderExtension->CardManConfig.ResetDelay = SYNC_RESET_DELAY;
  4571. pSmartcardExtension->ReaderExtension->CardManConfig.CardStopBits = 0x02;
  4572. SCCMN50M_ClearCardControlFlags(pSmartcardExtension,ALL_FLAGS);
  4573. SCCMN50M_SetSCRControlFlags(pSmartcardExtension,CARD_POWER| IGNORE_PARITY );
  4574. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_SYN);
  4575. status = SCCMN50M_ResyncCardManII(pSmartcardExtension);
  4576. if (status != STATUS_SUCCESS)
  4577. {
  4578. goto ExitPowerSynchronousCard;
  4579. }
  4580. // write config + header
  4581. status = SCCMN50M_WriteCardMan(pSmartcardExtension,0,NULL);
  4582. if (status != STATUS_SUCCESS)
  4583. {
  4584. goto ExitPowerSynchronousCard;
  4585. }
  4586. pSmartcardExtension->ReaderExtension->ToRHConfig = FALSE;
  4587. // read state and length
  4588. status = SCCMN50M_ReadCardMan(pSmartcardExtension,2,&ulBytesRead,bReadBuffer,sizeof(bReadBuffer));
  4589. if (status != STATUS_SUCCESS)
  4590. {
  4591. goto ExitPowerSynchronousCard;
  4592. }
  4593. if (bReadBuffer[1] < MIN_ATR_LEN )
  4594. {
  4595. // read all remaining bytes from the CardMan
  4596. DebugStatus = SCCMN50M_ReadCardMan(pSmartcardExtension,bReadBuffer[1],&ulBytesRead,bReadBuffer,sizeof(bReadBuffer));
  4597. status = STATUS_UNRECOGNIZED_MEDIA;
  4598. goto ExitPowerSynchronousCard;
  4599. }
  4600. if (bReadBuffer[1] > ulAtrBufferSize)
  4601. {
  4602. status = STATUS_BUFFER_OVERFLOW;
  4603. goto ExitPowerSynchronousCard;
  4604. }
  4605. // read ATR
  4606. status = SCCMN50M_ReadCardMan(pSmartcardExtension,bReadBuffer[1],pulAtrLength,pbAtrBuffer,ulAtrBufferSize);
  4607. if (status != STATUS_SUCCESS)
  4608. {
  4609. goto ExitPowerSynchronousCard;
  4610. }
  4611. if (pbAtrBuffer[0] == 0x00 ||
  4612. pbAtrBuffer[0] == 0xff )
  4613. {
  4614. status = STATUS_UNRECOGNIZED_MEDIA;
  4615. goto ExitPowerSynchronousCard;
  4616. }
  4617. pSmartcardExtension->ReaderExtension->fRawModeNecessary = TRUE;
  4618. ExitPowerSynchronousCard:
  4619. SmartcardDebug(
  4620. DEBUG_TRACE,
  4621. ( "%s!UseSyncStrategy: Exit %lx\n",
  4622. DRIVER_NAME,status)
  4623. );
  4624. SCCMN50M_ClearSCRControlFlags(pSmartcardExtension,IGNORE_PARITY | CM2_GET_ATR);
  4625. SCCMN50M_ClearCardManHeader(pSmartcardExtension);
  4626. return status;
  4627. }
  4628. /*****************************************************************************
  4629. Routine Description:
  4630. This function checks if the inserted card is a synchronous one
  4631. Arguments:
  4632. Return Value:
  4633. *****************************************************************************/
  4634. BOOLEAN
  4635. SCCMN50M_IsAsynchronousSmartCard(
  4636. IN PSMARTCARD_EXTENSION pSmartcardExtension
  4637. )
  4638. {
  4639. NTSTATUS status;
  4640. UCHAR ReadBuffer[3];
  4641. ULONG ulBytesRead;
  4642. BOOLEAN fIsAsynchronousSmartCard = TRUE;
  4643. UCHAR abATR[33];
  4644. SmartcardDebug(
  4645. DEBUG_TRACE,
  4646. ( "%s!IsAsynchronousSmartcard: Enter \n",
  4647. DRIVER_NAME)
  4648. );
  4649. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant = 200;
  4650. // 3MHz smart card ?
  4651. SCCMN50M_SetCardManHeader(pSmartcardExtension,0,0,0,1);
  4652. SCCMN50M_ClearCardControlFlags(pSmartcardExtension,ALL_FLAGS);
  4653. SCCMN50M_SetSCRControlFlags(pSmartcardExtension,CARD_POWER| IGNORE_PARITY | CM2_GET_ATR);
  4654. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_3MHZ);
  4655. // write config + header
  4656. status = SCCMN50M_WriteCardMan(pSmartcardExtension,0,NULL);
  4657. if (status != STATUS_SUCCESS)
  4658. {
  4659. goto ExitIsAsynchronousSmartCard;
  4660. }
  4661. pSmartcardExtension->ReaderExtension->ToRHConfig = FALSE;
  4662. // read state and length
  4663. status = SCCMN50M_ReadCardMan(pSmartcardExtension,2,&ulBytesRead,ReadBuffer,sizeof(ReadBuffer));
  4664. if (status == STATUS_SUCCESS &&
  4665. ReadBuffer[1] == 0x01 )
  4666. {
  4667. goto ExitIsAsynchronousSmartCard;
  4668. }
  4669. // ---------------------------------------
  4670. // power off card
  4671. // ---------------------------------------
  4672. status = SCCMN50M_PowerOff(pSmartcardExtension);
  4673. if (status != STATUS_SUCCESS)
  4674. {
  4675. goto ExitIsAsynchronousSmartCard;
  4676. }
  4677. // 5MHz smart card ?
  4678. SCCMN50M_SetCardManHeader(pSmartcardExtension,0,0,0,1);
  4679. SCCMN50M_ClearCardControlFlags(pSmartcardExtension,ALL_FLAGS);
  4680. SCCMN50M_SetSCRControlFlags(pSmartcardExtension,CARD_POWER| IGNORE_PARITY | CM2_GET_ATR);
  4681. SCCMN50M_SetCardControlFlags(pSmartcardExtension,ENABLE_5MHZ);
  4682. // write config + header
  4683. status = SCCMN50M_WriteCardMan(pSmartcardExtension,0,NULL);
  4684. if (status != STATUS_SUCCESS)
  4685. {
  4686. goto ExitIsAsynchronousSmartCard;
  4687. }
  4688. pSmartcardExtension->ReaderExtension->ToRHConfig = FALSE;
  4689. // read state and length
  4690. status = SCCMN50M_ReadCardMan(pSmartcardExtension,2,&ulBytesRead,ReadBuffer,sizeof(ReadBuffer));
  4691. if (status == STATUS_SUCCESS &&
  4692. ReadBuffer[1] == 0x01 )
  4693. {
  4694. goto ExitIsAsynchronousSmartCard;
  4695. }
  4696. // now we assume that it is a synchronous smart card
  4697. fIsAsynchronousSmartCard = FALSE;
  4698. // ---------------------------------------
  4699. // power off card
  4700. // ---------------------------------------
  4701. status = SCCMN50M_PowerOff(pSmartcardExtension);
  4702. if (status != STATUS_SUCCESS)
  4703. {
  4704. goto ExitIsAsynchronousSmartCard;
  4705. }
  4706. ExitIsAsynchronousSmartCard:
  4707. pSmartcardExtension->ReaderExtension->SerialConfigData.Timeouts.ReadTotalTimeoutConstant = DEFAULT_READ_TOTAL_TIMEOUT_CONSTANT;
  4708. SmartcardDebug(
  4709. DEBUG_TRACE,
  4710. ( "%s!IsAsynchronousSmartcard: Exit \n",
  4711. DRIVER_NAME)
  4712. );
  4713. return fIsAsynchronousSmartCard;
  4714. }
  4715. /*****************************************************************************
  4716. * History:
  4717. * $Log: sccmcb.c $
  4718. * Revision 1.7 2001/01/22 08:39:41 WFrischauf
  4719. * No comment given
  4720. *
  4721. * Revision 1.6 2000/09/25 10:46:22 WFrischauf
  4722. * No comment given
  4723. *
  4724. * Revision 1.5 2000/08/24 09:05:44 TBruendl
  4725. * No comment given
  4726. *
  4727. * Revision 1.4 2000/08/16 08:24:04 TBruendl
  4728. * warning :uninitialized memory removed
  4729. *
  4730. * Revision 1.3 2000/07/28 09:24:12 TBruendl
  4731. * Changes for OMNIKEY on Whistler CD
  4732. *
  4733. * Revision 1.16 2000/06/27 11:56:28 TBruendl
  4734. * workaraound for SAMOR smart cards with invalid ATR (ITSEC)
  4735. *
  4736. * Revision 1.15 2000/06/08 10:08:47 TBruendl
  4737. * bug fix : warm reset for ScfW
  4738. *
  4739. * Revision 1.14 2000/05/23 09:58:26 TBruendl
  4740. * OMNIKEY 3.0.0.1
  4741. *
  4742. * Revision 1.13 2000/04/13 08:07:22 TBruendl
  4743. * PPS bug fix for SCfW
  4744. *
  4745. * Revision 1.12 2000/04/04 07:52:18 TBruendl
  4746. * problem with the new WfsC fixed
  4747. *
  4748. * Revision 1.11 2000/03/03 09:50:50 TBruendl
  4749. * No comment given
  4750. *
  4751. * Revision 1.10 2000/03/01 09:32:04 TBruendl
  4752. * R02.20.0
  4753. *
  4754. * Revision 1.9 2000/01/04 10:40:33 TBruendl
  4755. * bug fix: status instead of DebugStatus used
  4756. *
  4757. * Revision 1.8 1999/12/16 14:10:16 TBruendl
  4758. * After transparent mode has been left, the status is read from the CardMan to be sure that
  4759. * the new settings are effective.
  4760. *
  4761. * Revision 1.6 1999/12/13 07:55:38 TBruendl
  4762. * Bug fix for P+ druing initialization
  4763. * PTS for 4.9 mhz smartcards added
  4764. *
  4765. * Revision 1.5 1999/11/04 07:53:21 WFrischauf
  4766. * bug fixes due to error reports 2 - 7
  4767. *
  4768. * Revision 1.4 1999/07/12 12:49:04 TBruendl
  4769. * Bug fix: Resync after GET_DEVICE_DESCRIPTION
  4770. * Power On SLE4428
  4771. *
  4772. * Revision 1.3 1999/06/10 09:03:57 TBruendl
  4773. * No comment given
  4774. *
  4775. * Revision 1.2 1999/02/25 10:12:22 TBruendl
  4776. * No comment given
  4777. *
  4778. * Revision 1.1 1999/02/02 13:34:37 TBruendl
  4779. * This is the first release (R01.00) of the IFD handler for CardMan running under NT5.0.
  4780. *
  4781. *
  4782. *****************************************************************************/