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

3097 lines
107 KiB

  1. /*****************************************************************************
  2. @doc INT EXT
  3. ******************************************************************************
  4. * $ProjectName: $
  5. * $ProjectRevision: $
  6. *-----------------------------------------------------------------------------
  7. * $Source: z:/pr/cmbp0/sw/cmbp0.ms/rcs/cmbp0scr.c $
  8. * $Revision: 1.7 $
  9. *-----------------------------------------------------------------------------
  10. * $Author: WFrischauf $
  11. *-----------------------------------------------------------------------------
  12. * History: see EOF
  13. *-----------------------------------------------------------------------------
  14. *
  15. * Copyright 2000 OMNIKEY AG
  16. ******************************************************************************/
  17. #include <cmbp0wdm.h>
  18. #include <cmbp0scr.h>
  19. #include <cmbp0log.h>
  20. // this is a FI / Fi assignment
  21. const ULONG Fi[] = { 372, 372, 588, 744, 1116, 1488, 1860, 372,
  22. 372, 512, 768, 1024, 1536, 2048, 372, 372};
  23. // this is a DI / Di assignment
  24. const ULONG Di[] = { 1, 1, 2, 4, 8, 16, 32, 1,
  25. 12, 20, 1, 1, 1, 1, 1, 1};
  26. /*****************************************************************************
  27. CMMOB_CorrectAtr
  28. Routine Description:
  29. This function checks if the received ATR is valid.
  30. Arguments:
  31. Return Value:
  32. *****************************************************************************/
  33. VOID CMMOB_CorrectAtr(
  34. PUCHAR pbBuffer,
  35. ULONG ulBufferSize
  36. )
  37. {
  38. UCHAR bNumberHistoricalBytes;
  39. UCHAR bXorChecksum;
  40. ULONG i;
  41. if (ulBufferSize < 0x09) // mininmum length of a modified ATR
  42. return ; // ATR is ok
  43. // variant 1
  44. if (pbBuffer[0] == 0x3b &&
  45. pbBuffer[1] == 0xb4 &&
  46. pbBuffer[2] == 0x11 &&
  47. pbBuffer[3] == 0x00 &&
  48. pbBuffer[4] == 0x81 &&
  49. pbBuffer[5] == 0x31 &&
  50. pbBuffer[6] == 0x90 &&
  51. pbBuffer[7] == 0x73 &&
  52. ulBufferSize == 13 ) {
  53. // correct checksum byte
  54. bXorChecksum = pbBuffer[1];
  55. for (i=2;i<ulBufferSize-1;i++)
  56. bXorChecksum ^= pbBuffer[i];
  57. if (pbBuffer[ulBufferSize -1 ] != bXorChecksum ) {
  58. pbBuffer[ulBufferSize -1 ] = bXorChecksum;
  59. SmartcardDebug(DEBUG_ATR,
  60. ("%s!CorrectAtr: Correcting SAMOS ATR (variant 1)\n", DRIVER_NAME));
  61. }
  62. }
  63. // variant 2
  64. if (pbBuffer[0] == 0x3b &&
  65. pbBuffer[1] == 0xbf &&
  66. pbBuffer[2] == 0x11 &&
  67. pbBuffer[3] == 0x00 &&
  68. pbBuffer[4] == 0x81 &&
  69. pbBuffer[5] == 0x31 &&
  70. pbBuffer[6] == 0x90 &&
  71. pbBuffer[7] == 0x73 &&
  72. ulBufferSize == 13 ) {
  73. // correct number of historical bytes
  74. bNumberHistoricalBytes = 4;
  75. pbBuffer[1] &= 0xf0;
  76. pbBuffer[1] |= bNumberHistoricalBytes;
  77. // correct checksum byte
  78. bXorChecksum = pbBuffer[1];
  79. for (i=2;i<ulBufferSize-1;i++)
  80. bXorChecksum ^= pbBuffer[i];
  81. pbBuffer[ulBufferSize -1 ] = bXorChecksum;
  82. SmartcardDebug(DEBUG_ATR,
  83. ("%s!CorrectAtr: Correcting SAMOS ATR (variant 2)\n", DRIVER_NAME));
  84. }
  85. // variant 3
  86. if (pbBuffer[0] == 0x3b &&
  87. pbBuffer[1] == 0xbf &&
  88. pbBuffer[2] == 0x11 &&
  89. pbBuffer[3] == 0x00 &&
  90. pbBuffer[4] == 0x81 &&
  91. pbBuffer[5] == 0x31 &&
  92. pbBuffer[6] == 0x90 &&
  93. pbBuffer[7] == 0x73 &&
  94. ulBufferSize == 9 ) {
  95. // correct number of historical bytes
  96. bNumberHistoricalBytes = 0;
  97. pbBuffer[1] &= 0xf0;
  98. pbBuffer[1] |= bNumberHistoricalBytes;
  99. // correct checksum byte
  100. bXorChecksum = pbBuffer[1];
  101. for (i=2;i<ulBufferSize-1;i++)
  102. bXorChecksum ^= pbBuffer[i];
  103. pbBuffer[ulBufferSize -1 ] = bXorChecksum;
  104. SmartcardDebug(DEBUG_ATR,
  105. ("%s!CorrectAtr: Correcting SAMOS ATR (variant 3)\n",DRIVER_NAME));
  106. }
  107. }
  108. /*****************************************************************************
  109. CMMOB_CardPower:
  110. callback handler for SMCLIB RDF_CARD_POWER
  111. Arguments:
  112. SmartcardExtension context of call
  113. Return Value:
  114. STATUS_SUCCESS
  115. STATUS_NO_MEDIA
  116. STATUS_TIMEOUT
  117. STATUS_BUFFER_TOO_SMALL
  118. ******************************************************************************/
  119. NTSTATUS CMMOB_CardPower (
  120. PSMARTCARD_EXTENSION SmartcardExtension
  121. )
  122. {
  123. NTSTATUS NTStatus = STATUS_SUCCESS;
  124. PREADER_EXTENSION ReaderExtension;
  125. UCHAR pbAtrBuffer[MAXIMUM_ATR_LENGTH];
  126. ULONG ulAtrLength;
  127. BOOLEAN fMaxWaitTime=FALSE;
  128. KIRQL irql;
  129. #if DBG || DEBUG
  130. static PCHAR request[] = { "PowerDown", "ColdReset", "WarmReset"};
  131. #endif
  132. SmartcardDebug(DEBUG_TRACE,
  133. ("%s!CardPower: Enter, Request = %s\n",
  134. DRIVER_NAME,request[SmartcardExtension->MinorIoControlCode]));
  135. ReaderExtension = SmartcardExtension->ReaderExtension;
  136. ReaderExtension->fTActive=TRUE;
  137. NTStatus = CMMOB_SetFlags1(ReaderExtension);
  138. if (NTStatus != STATUS_SUCCESS) {
  139. goto ExitCardPower;
  140. }
  141. switch (SmartcardExtension->MinorIoControlCode) {
  142. case SCARD_WARM_RESET:
  143. case SCARD_COLD_RESET:
  144. // try asynchronous cards first
  145. // because some asynchronous cards
  146. // do not return 0xFF in the first byte
  147. NTStatus = CMMOB_PowerOnCard(SmartcardExtension,
  148. pbAtrBuffer,
  149. fMaxWaitTime,
  150. &ulAtrLength);
  151. if (NTStatus != STATUS_SUCCESS) {
  152. // try a second time, with maximum waiting time
  153. fMaxWaitTime=TRUE;
  154. NTStatus = CMMOB_PowerOnCard(SmartcardExtension,
  155. pbAtrBuffer,
  156. fMaxWaitTime,
  157. &ulAtrLength);
  158. }
  159. if (NTStatus != STATUS_SUCCESS) {
  160. goto ExitCardPower;
  161. }
  162. // correct ATR in case of old Samos cards, with wrong number of historical bytes / checksum
  163. CMMOB_CorrectAtr(pbAtrBuffer, ulAtrLength);
  164. if (ReaderExtension->CardParameters.fSynchronousCard == FALSE) {
  165. // copy ATR to smart card structure
  166. // the lib needs the ATR for evaluation of the card parameters
  167. RtlCopyBytes((PVOID)SmartcardExtension->CardCapabilities.ATR.Buffer,
  168. (PVOID)pbAtrBuffer,
  169. ulAtrLength);
  170. KeAcquireSpinLock(&SmartcardExtension->OsData->SpinLock,
  171. &irql);
  172. SmartcardExtension->CardCapabilities.ATR.Length = (UCHAR)ulAtrLength;
  173. SmartcardExtension->ReaderCapabilities.CurrentState = SCARD_NEGOTIABLE;
  174. SmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  175. KeReleaseSpinLock(&SmartcardExtension->OsData->SpinLock,
  176. irql);
  177. NTStatus = SmartcardUpdateCardCapabilities(SmartcardExtension);
  178. if (NTStatus != STATUS_SUCCESS) {
  179. if (!fMaxWaitTime) {
  180. // try a second time, with maximum waiting time
  181. fMaxWaitTime=TRUE;
  182. NTStatus = CMMOB_PowerOnCard(SmartcardExtension,
  183. pbAtrBuffer,
  184. fMaxWaitTime,
  185. &ulAtrLength);
  186. if (NTStatus != STATUS_SUCCESS) {
  187. goto ExitCardPower;
  188. }
  189. RtlCopyBytes((PVOID)SmartcardExtension->CardCapabilities.ATR.Buffer,
  190. (PVOID)pbAtrBuffer,
  191. ulAtrLength);
  192. KeAcquireSpinLock(&SmartcardExtension->OsData->SpinLock,
  193. &irql);
  194. SmartcardExtension->CardCapabilities.ATR.Length = (UCHAR)ulAtrLength;
  195. SmartcardExtension->ReaderCapabilities.CurrentState = SCARD_NEGOTIABLE;
  196. SmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  197. KeReleaseSpinLock(&SmartcardExtension->OsData->SpinLock,
  198. irql);
  199. NTStatus = SmartcardUpdateCardCapabilities(SmartcardExtension);
  200. if (NTStatus != STATUS_SUCCESS) {
  201. goto ExitCardPower;
  202. }
  203. } else
  204. goto ExitCardPower;
  205. }
  206. // -----------------------
  207. // set parameters
  208. // -----------------------
  209. if (SmartcardExtension->CardCapabilities.N != 0xff) {
  210. // 0 <= N <= 254
  211. ReaderExtension->CardParameters.bStopBits = 2 + SmartcardExtension->CardCapabilities.N;
  212. } else {
  213. // N = 255
  214. if (SmartcardExtension->CardCapabilities.Protocol.Selected & SCARD_PROTOCOL_T0) {
  215. // 12 etu for T=0;
  216. ReaderExtension->CardParameters.bStopBits = 2;
  217. } else {
  218. // 11 etu for T=1
  219. ReaderExtension->CardParameters.bStopBits = 1;
  220. }
  221. }
  222. if (SmartcardExtension->CardCapabilities.InversConvention) {
  223. ReaderExtension->CardParameters.fInversRevers = TRUE;
  224. SmartcardDebug(DEBUG_ATR,
  225. ("%s!Card with invers convention !\n",DRIVER_NAME ));
  226. }
  227. CMMOB_SetCardParameters (ReaderExtension);
  228. #if DBG
  229. {
  230. ULONG i;
  231. SmartcardDebug(DEBUG_ATR,("%s!ATR : ",DRIVER_NAME));
  232. for (i = 0;i < ulAtrLength;i++)
  233. SmartcardDebug(DEBUG_ATR,("%2.2x ",SmartcardExtension->CardCapabilities.ATR.Buffer[i]));
  234. SmartcardDebug(DEBUG_ATR,("\n"));
  235. }
  236. #endif
  237. } else {
  238. SmartcardExtension->CardCapabilities.ATR.Buffer[0] = 0x3B;
  239. SmartcardExtension->CardCapabilities.ATR.Buffer[1] = 0x04;
  240. RtlCopyBytes((PVOID)&SmartcardExtension->CardCapabilities.ATR.Buffer[2],
  241. (PVOID)pbAtrBuffer,
  242. ulAtrLength);
  243. ulAtrLength += 2;
  244. SmartcardExtension->CardCapabilities.ATR.Length = (UCHAR)ulAtrLength;
  245. KeAcquireSpinLock(&SmartcardExtension->OsData->SpinLock,
  246. &irql);
  247. SmartcardExtension->ReaderCapabilities.CurrentState = SCARD_SPECIFIC;
  248. SmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_T0;
  249. KeReleaseSpinLock(&SmartcardExtension->OsData->SpinLock,
  250. irql);
  251. NTStatus = SmartcardUpdateCardCapabilities(SmartcardExtension);
  252. if (NTStatus != STATUS_SUCCESS) {
  253. goto ExitCardPower;
  254. }
  255. SmartcardDebug(DEBUG_ATR,("ATR of synchronous smart card : %2.2x %2.2x %2.2x %2.2x\n",
  256. pbAtrBuffer[0],pbAtrBuffer[1],pbAtrBuffer[2],pbAtrBuffer[3]));
  257. }
  258. // copy ATR to user space
  259. if (SmartcardExtension->IoRequest.ReplyBufferLength >=
  260. SmartcardExtension->CardCapabilities.ATR.Length) {
  261. RtlCopyBytes((PVOID)SmartcardExtension->IoRequest.ReplyBuffer,
  262. (PVOID)SmartcardExtension->CardCapabilities.ATR.Buffer,
  263. SmartcardExtension->CardCapabilities.ATR.Length);
  264. *SmartcardExtension->IoRequest.Information = SmartcardExtension->CardCapabilities.ATR.Length;
  265. } else {
  266. NTStatus = STATUS_BUFFER_TOO_SMALL;
  267. *SmartcardExtension->IoRequest.Information = 0;
  268. }
  269. break;
  270. case SCARD_POWER_DOWN:
  271. NTStatus = CMMOB_PowerOffCard(SmartcardExtension);
  272. if (NTStatus != STATUS_SUCCESS) {
  273. goto ExitCardPower;
  274. }
  275. KeAcquireSpinLock(&SmartcardExtension->OsData->SpinLock,
  276. &irql);
  277. SmartcardExtension->ReaderCapabilities.CurrentState = SCARD_SWALLOWED;
  278. SmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  279. KeReleaseSpinLock(&SmartcardExtension->OsData->SpinLock,
  280. irql);
  281. break;
  282. }
  283. ExitCardPower:
  284. ReaderExtension->fTActive=FALSE;
  285. CMMOB_SetFlags1(ReaderExtension);
  286. SmartcardDebug(DEBUG_TRACE,
  287. ("%s!CardPower: Exit %X\n",DRIVER_NAME,NTStatus ));
  288. return( NTStatus );
  289. }
  290. /*****************************************************************************
  291. Routine Description:
  292. CMMOB_PowerOnCard
  293. Arguments:
  294. Return Value:
  295. *****************************************************************************/
  296. NTSTATUS CMMOB_PowerOnCard (
  297. IN PSMARTCARD_EXTENSION SmartcardExtension,
  298. IN PUCHAR pbATR,
  299. IN BOOLEAN fMaxWaitTime,
  300. OUT PULONG pulATRLength
  301. )
  302. {
  303. NTSTATUS NTStatus = STATUS_SUCCESS;
  304. KTIMER TimerWait;
  305. UCHAR bPowerCmd;
  306. ULONG ulCardType;
  307. ULONG ulBytesReceived;
  308. UCHAR bFirstByte;
  309. LONG lWaitTime;
  310. LARGE_INTEGER liWaitTime;
  311. BOOLEAN fTimeExpired;
  312. SmartcardDebug(DEBUG_TRACE,
  313. ("%s!PowerOnCard: Enter\n",DRIVER_NAME));
  314. SmartcardExtension->ReaderExtension->CardParameters.bStopBits=2;
  315. SmartcardExtension->ReaderExtension->CardParameters.fSynchronousCard=FALSE;
  316. SmartcardExtension->ReaderExtension->CardParameters.fInversRevers=FALSE;
  317. SmartcardExtension->ReaderExtension->CardParameters.bClockFrequency=4;
  318. SmartcardExtension->ReaderExtension->CardParameters.fT0Mode=FALSE;
  319. SmartcardExtension->ReaderExtension->CardParameters.fT0Write=FALSE;
  320. SmartcardExtension->ReaderExtension->fReadCIS = FALSE;
  321. // reset the state machine of the reader
  322. NTStatus = CMMOB_ResetReader (SmartcardExtension->ReaderExtension);
  323. if (NTStatus!=STATUS_SUCCESS)
  324. goto ExitPowerOnCard;
  325. if (CMMOB_CardInserted (SmartcardExtension->ReaderExtension)) {
  326. //initialize Timer
  327. KeInitializeTimer(&TimerWait);
  328. //we have to differentiate between cold and warm reset
  329. if (SmartcardExtension->MinorIoControlCode == SCARD_WARM_RESET &&
  330. CMMOB_CardPowered (SmartcardExtension->ReaderExtension)) {
  331. //warm reset
  332. bPowerCmd=CMD_POWERON_WARM;
  333. } else {
  334. //cold reset
  335. bPowerCmd=CMD_POWERON_COLD;
  336. //if card is powerde we have to turn it off for cold reset
  337. if (CMMOB_CardPowered (SmartcardExtension->ReaderExtension))
  338. CMMOB_PowerOffCard (SmartcardExtension);
  339. }
  340. #define MAX_CARD_TYPE 2
  341. for (ulCardType = 0; ulCardType < MAX_CARD_TYPE; ulCardType++) {
  342. switch (ulCardType) {
  343. case 0:
  344. // BaudRate divider 372 - 1
  345. SmartcardExtension->ReaderExtension->CardParameters.bBaudRateHigh = 0x01;
  346. SmartcardExtension->ReaderExtension->CardParameters.bBaudRateLow = 0x73;
  347. if (fMaxWaitTime)
  348. lWaitTime=1000;
  349. else
  350. lWaitTime=100;
  351. SmartcardDebug(DEBUG_ATR,
  352. ("%s!trying 3.57 Mhz smart card, waiting time %ims\n",DRIVER_NAME,lWaitTime));
  353. break;
  354. case 1:
  355. // BaudRate divider 512 - 1
  356. SmartcardExtension->ReaderExtension->CardParameters.bBaudRateHigh = 0x01;
  357. SmartcardExtension->ReaderExtension->CardParameters.bBaudRateLow = 0xFF;
  358. if (fMaxWaitTime)
  359. lWaitTime=1400;
  360. else
  361. lWaitTime=140;
  362. SmartcardDebug(DEBUG_ATR,
  363. ("%s!trying 4.92 Mhz smart card, waiting time %ims\n",DRIVER_NAME,lWaitTime));
  364. break;
  365. }
  366. //set baud rate
  367. NTStatus=CMMOB_SetCardParameters(SmartcardExtension->ReaderExtension);
  368. if (NTStatus!=STATUS_SUCCESS)
  369. goto ExitPowerOnCard;
  370. //issue power on command
  371. NTStatus=CMMOB_WriteRegister(SmartcardExtension->ReaderExtension,
  372. ADDR_WRITEREG_FLAGS0, bPowerCmd);
  373. if (NTStatus!=STATUS_SUCCESS)
  374. goto ExitPowerOnCard;
  375. // maximum wait for power on first byte 100 ms
  376. liWaitTime = RtlConvertLongToLargeInteger(100L * -10000L);
  377. KeSetTimer(&TimerWait,liWaitTime,NULL);
  378. do {
  379. fTimeExpired = KeReadStateTimer(&TimerWait);
  380. NTStatus=CMMOB_BytesReceived (SmartcardExtension->ReaderExtension,&ulBytesReceived);
  381. if (NTStatus!=STATUS_SUCCESS)
  382. goto ExitPowerOnCard;
  383. // wait 1 ms, so that processor is not blocked
  384. SysDelay(1);
  385. }
  386. while (fTimeExpired==FALSE && ulBytesReceived == 0x00);
  387. if (fTimeExpired) {
  388. NTStatus = STATUS_IO_TIMEOUT;
  389. } else {
  390. ULONG ulBytesReceivedPrevious;
  391. KeCancelTimer(&TimerWait);
  392. // maximum wait for power on last byte 1 s for 3.58 card
  393. // and 1.4 seconds for 4.91 cards
  394. liWaitTime = RtlConvertLongToLargeInteger(lWaitTime * -10000L);
  395. do {
  396. KeSetTimer(&TimerWait,liWaitTime,NULL);
  397. NTStatus=CMMOB_BytesReceived (SmartcardExtension->ReaderExtension,&ulBytesReceivedPrevious);
  398. if (NTStatus!=STATUS_SUCCESS)
  399. goto ExitPowerOnCard;
  400. do {
  401. fTimeExpired = KeReadStateTimer(&TimerWait);
  402. NTStatus=CMMOB_BytesReceived (SmartcardExtension->ReaderExtension,&ulBytesReceived);
  403. if (NTStatus!=STATUS_SUCCESS)
  404. goto ExitPowerOnCard;
  405. // wait 1 ms, so that processor is not blocked
  406. SysDelay(1);
  407. }
  408. while (fTimeExpired==FALSE && ulBytesReceivedPrevious == ulBytesReceived);
  409. if (!fTimeExpired) {
  410. KeCancelTimer(&TimerWait);
  411. }
  412. }
  413. while (!fTimeExpired);
  414. //now we should have received an ATR
  415. NTStatus=CMMOB_ResetReader (SmartcardExtension->ReaderExtension);
  416. if (NTStatus!=STATUS_SUCCESS)
  417. goto ExitPowerOnCard;
  418. NTStatus=CMMOB_ReadBuffer(SmartcardExtension->ReaderExtension, 0, 1, &bFirstByte);
  419. if (NTStatus!=STATUS_SUCCESS)
  420. goto ExitPowerOnCard;
  421. if ((bFirstByte != 0x3B &&
  422. bFirstByte != 0x03 )||
  423. ulBytesReceived > MAXIMUM_ATR_LENGTH) {
  424. NTStatus=STATUS_UNRECOGNIZED_MEDIA;
  425. } else {
  426. pbATR[0]=bFirstByte;
  427. NTStatus=CMMOB_ReadBuffer(SmartcardExtension->ReaderExtension,
  428. 1, ulBytesReceived, &pbATR[1]);
  429. if (NTStatus!=STATUS_SUCCESS)
  430. goto ExitPowerOnCard;
  431. *pulATRLength = ulBytesReceived;
  432. // success leave the loop
  433. break;
  434. }
  435. }
  436. // if not the last time in the loop power off
  437. // the card to get a well defined condition
  438. // (after the last pass the power off is
  439. // done outside the loop if necessary)
  440. if (ulCardType < MAX_CARD_TYPE-1) {
  441. CMMOB_PowerOffCard(SmartcardExtension);
  442. }
  443. }
  444. } else {
  445. NTStatus=STATUS_NO_MEDIA;
  446. }
  447. ExitPowerOnCard:
  448. if (NTStatus!=STATUS_SUCCESS) {
  449. if (NTStatus != STATUS_NO_MEDIA) {
  450. NTStatus = STATUS_UNRECOGNIZED_MEDIA;
  451. }
  452. CMMOB_PowerOffCard(SmartcardExtension);
  453. }
  454. SmartcardDebug(DEBUG_TRACE,
  455. ("%s!PowerOnCard: Exit %lx\n",DRIVER_NAME,NTStatus));
  456. return NTStatus;
  457. }
  458. /*****************************************************************************
  459. Routine Description:
  460. CMMOB_PowerOffCard
  461. Arguments:
  462. Return Value:
  463. *****************************************************************************/
  464. NTSTATUS CMMOB_PowerOffCard (
  465. IN PSMARTCARD_EXTENSION SmartcardExtension
  466. )
  467. {
  468. NTSTATUS NTStatus = STATUS_SUCCESS;
  469. BYTE* pbRegsBase;
  470. KTIMER TimerWait;
  471. LARGE_INTEGER liWaitTime;
  472. BOOLEAN fTimeExpired;
  473. SmartcardDebug(DEBUG_TRACE,
  474. ("%s!PowerOffCard: Enter\n",DRIVER_NAME));
  475. NTStatus = CMMOB_ResetReader (SmartcardExtension->ReaderExtension);
  476. if (NTStatus!=STATUS_SUCCESS)
  477. goto ExitPowerOffCard;
  478. pbRegsBase=SmartcardExtension->ReaderExtension->pbRegsBase;
  479. if (CMMOB_CardInserted (SmartcardExtension->ReaderExtension)) {
  480. // set card state for update thread
  481. // otherwise a card removal/insertion would be recognized
  482. if (SmartcardExtension->ReaderExtension->ulOldCardState == POWERED)
  483. SmartcardExtension->ReaderExtension->ulOldCardState = INSERTED;
  484. //issue power off command
  485. CMMOB_WriteRegister(SmartcardExtension->ReaderExtension,
  486. ADDR_WRITEREG_FLAGS0, CMD_POWEROFF);
  487. KeInitializeTimer(&TimerWait);
  488. // maximum wait for power down 1 second
  489. liWaitTime = RtlConvertLongToLargeInteger(1000L * -10000L);
  490. KeSetTimer(&TimerWait,liWaitTime,NULL);
  491. do {
  492. fTimeExpired = KeReadStateTimer(&TimerWait);
  493. // wait 1 ms, so that processor is not blocked
  494. SysDelay(1);
  495. }
  496. while (fTimeExpired==FALSE && CMMOB_CardPowered (SmartcardExtension->ReaderExtension));
  497. if (fTimeExpired) {
  498. NTStatus = STATUS_IO_TIMEOUT;
  499. } else {
  500. KeCancelTimer(&TimerWait);
  501. }
  502. } else {
  503. NTStatus=STATUS_NO_MEDIA;
  504. }
  505. ExitPowerOffCard:
  506. CMMOB_ResetReader (SmartcardExtension->ReaderExtension);
  507. SmartcardDebug(DEBUG_TRACE,
  508. ("%s!PowerOffCard: Exit %lx\n",DRIVER_NAME,NTStatus));
  509. return NTStatus;
  510. }
  511. /*****************************************************************************
  512. CMMOB_SetProtocol:
  513. callback handler for SMCLIB RDF_SET_PROTOCOL
  514. Arguments:
  515. SmartcardExtension context of call
  516. Return Value:
  517. STATUS_SUCCESS
  518. STATUS_NO_MEDIA
  519. STATUS_TIMEOUT
  520. STATUS_BUFFER_TOO_SMALL
  521. STATUS_INVALID_DEVICE_STATE
  522. STATUS_INVALID_DEVICE_REQUEST
  523. ******************************************************************************/
  524. NTSTATUS CMMOB_SetProtocol(
  525. PSMARTCARD_EXTENSION SmartcardExtension
  526. )
  527. {
  528. NTSTATUS NTStatus;
  529. PREADER_EXTENSION ReaderExtension;
  530. USHORT usSCLibProtocol;
  531. UCHAR abPTSRequest[4];
  532. UCHAR abPTSReply [4];
  533. ULONG ulBytesRead;
  534. ULONG ulBaudRateDivider;
  535. ULONG ulWaitTime;
  536. UCHAR bTemp;
  537. ULONG i;
  538. KIRQL irql;
  539. SmartcardDebug(DEBUG_TRACE,
  540. ("%s!SetProtocol: Enter\n",DRIVER_NAME ));
  541. ReaderExtension = SmartcardExtension->ReaderExtension;
  542. ReaderExtension->fTActive=TRUE;
  543. NTStatus = CMMOB_SetFlags1(ReaderExtension);
  544. if (NTStatus != STATUS_SUCCESS) {
  545. goto ExitSetProtocol;
  546. }
  547. NTStatus = STATUS_PENDING;
  548. usSCLibProtocol = ( USHORT )( SmartcardExtension->MinorIoControlCode );
  549. //
  550. // check card insertion
  551. //
  552. KeAcquireSpinLock(&SmartcardExtension->OsData->SpinLock,
  553. &irql);
  554. if (SmartcardExtension->ReaderCapabilities.CurrentState == SCARD_ABSENT) {
  555. NTStatus = STATUS_NO_MEDIA;
  556. } else {
  557. //
  558. // Check if the card is already in specific state and if the caller
  559. // wants to have the selected protocol
  560. //
  561. if (SmartcardExtension->ReaderCapabilities.CurrentState == SCARD_SPECIFIC) {
  562. if (SmartcardExtension->CardCapabilities.Protocol.Selected == usSCLibProtocol) {
  563. NTStatus = STATUS_SUCCESS;
  564. }
  565. }
  566. }
  567. KeReleaseSpinLock(&SmartcardExtension->OsData->SpinLock,
  568. irql);
  569. if (NTStatus == STATUS_PENDING) {
  570. //
  571. // reset the state machine of the reader
  572. //
  573. NTStatus=CMMOB_ResetReader(ReaderExtension);
  574. if (NTStatus==STATUS_SUCCESS) {
  575. // try 2 times,
  576. // 0 - optimal
  577. // 1 - default
  578. for (i=0; i<2; i++) {
  579. // set initial character of PTS
  580. abPTSRequest[0] = 0xFF;
  581. // set the format character (PTS0)
  582. if (SmartcardExtension->CardCapabilities.Protocol.Supported &
  583. usSCLibProtocol & SCARD_PROTOCOL_T1) {
  584. // select T=1 and indicate that PTS1 follows
  585. abPTSRequest[1] = 0x11;
  586. SmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_T1;
  587. } else if (SmartcardExtension->CardCapabilities.Protocol.Supported &
  588. usSCLibProtocol & SCARD_PROTOCOL_T0) {
  589. // select T=0 and indicate that PTS1 follows
  590. abPTSRequest[1] = 0x10;
  591. SmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_T0;
  592. } else {
  593. // we do not support other protocols
  594. NTStatus = STATUS_INVALID_DEVICE_REQUEST;
  595. goto ExitSetProtocol;
  596. }
  597. if (i==0) {
  598. // optimal
  599. bTemp = (BYTE) (SmartcardExtension->CardCapabilities.PtsData.Fl << 4 |
  600. SmartcardExtension->CardCapabilities.PtsData.Dl);
  601. SmartcardDebug(DEBUG_PROTOCOL,
  602. ("%s! from library suggested PTS1(0x%x)\n",DRIVER_NAME,bTemp));
  603. SmartcardExtension->CardCapabilities.PtsData.Type = PTS_TYPE_OPTIMAL;
  604. SmartcardExtension->CardCapabilities.PtsData.Fl=SmartcardExtension->CardCapabilities.Fl;
  605. SmartcardExtension->CardCapabilities.PtsData.Dl=SmartcardExtension->CardCapabilities.Dl;
  606. } else {
  607. // default
  608. // we don�t know if it is correct to set 4.91Mhz cards to 0x11
  609. // but we have no card to try now
  610. SmartcardExtension->CardCapabilities.PtsData.Type = PTS_TYPE_DEFAULT;
  611. SmartcardExtension->CardCapabilities.PtsData.Fl=1;
  612. SmartcardExtension->CardCapabilities.PtsData.Dl=1;
  613. }
  614. bTemp = (BYTE) (SmartcardExtension->CardCapabilities.PtsData.Fl << 4 |
  615. SmartcardExtension->CardCapabilities.PtsData.Dl);
  616. SmartcardDebug(DEBUG_PROTOCOL,
  617. ("%s! trying PTS1(0x%x)\n",DRIVER_NAME,bTemp));
  618. switch (SmartcardExtension->CardCapabilities.PtsData.Fl) {
  619. case 1:
  620. // here we can handle all baudrates
  621. break;
  622. case 2:
  623. case 3:
  624. if (SmartcardExtension->CardCapabilities.PtsData.Dl == 1) {
  625. SmartcardDebug(DEBUG_PROTOCOL,
  626. ("%s! overwriting PTS1(0x%x)\n",DRIVER_NAME,bTemp));
  627. // we must correct Fl/Dl
  628. SmartcardExtension->CardCapabilities.PtsData.Dl = 0x01;
  629. SmartcardExtension->CardCapabilities.PtsData.Fl = 0x01;
  630. }
  631. break;
  632. case 4:
  633. case 5:
  634. case 6:
  635. if (SmartcardExtension->CardCapabilities.PtsData.Dl == 1 ||
  636. SmartcardExtension->CardCapabilities.PtsData.Dl == 2) {
  637. SmartcardDebug(DEBUG_PROTOCOL,
  638. ("%s! overwriting PTS1(0x%x)\n",DRIVER_NAME,bTemp));
  639. // we must correct Fl/Dl
  640. SmartcardExtension->CardCapabilities.PtsData.Dl = 0x01;
  641. SmartcardExtension->CardCapabilities.PtsData.Fl = 0x01;
  642. }
  643. break;
  644. case 9:
  645. // here we can handle all baudrates
  646. break;
  647. case 10:
  648. case 11:
  649. if (SmartcardExtension->CardCapabilities.PtsData.Dl == 1) {
  650. SmartcardDebug(DEBUG_PROTOCOL,
  651. ("%s! overwriting PTS1(0x%x)\n",DRIVER_NAME,bTemp));
  652. // we must correct Fl/Dl
  653. SmartcardExtension->CardCapabilities.PtsData.Dl = 0x01;
  654. SmartcardExtension->CardCapabilities.PtsData.Fl = 0x09;
  655. }
  656. break;
  657. case 12:
  658. case 13:
  659. if (SmartcardExtension->CardCapabilities.PtsData.Dl == 1 ||
  660. SmartcardExtension->CardCapabilities.PtsData.Dl == 2) {
  661. SmartcardDebug(DEBUG_PROTOCOL,
  662. ("%s! overwriting PTS1(0x%x)\n",DRIVER_NAME,bTemp));
  663. // we must correct Fl/Dl
  664. SmartcardExtension->CardCapabilities.PtsData.Dl = 0x01;
  665. SmartcardExtension->CardCapabilities.PtsData.Fl = 0x09;
  666. }
  667. break;
  668. default:
  669. // this are the RFUs
  670. SmartcardDebug(DEBUG_PROTOCOL,
  671. ("%s! overwriting PTS1(0x%x)\n",DRIVER_NAME,bTemp));
  672. // we must correct Fl/Dl
  673. SmartcardExtension->CardCapabilities.PtsData.Dl = 0x01;
  674. SmartcardExtension->CardCapabilities.PtsData.Fl = 0x01;
  675. break;
  676. }
  677. // set PTS1 with codes Fl and Dl
  678. abPTSRequest[2] = (BYTE) (SmartcardExtension->CardCapabilities.PtsData.Fl << 4 |
  679. SmartcardExtension->CardCapabilities.PtsData.Dl);
  680. // set PCK (check character)
  681. abPTSRequest[3] = (BYTE)(abPTSRequest[0] ^ abPTSRequest[1] ^ abPTSRequest[2]);
  682. if (ReaderExtension->CardParameters.fInversRevers) {
  683. SmartcardDebug(DEBUG_PROTOCOL,
  684. ("%s! PTS request for InversConvention\n",DRIVER_NAME));
  685. CMMOB_InverseBuffer (abPTSRequest,4);
  686. }
  687. #if DBG
  688. {
  689. ULONG k;
  690. SmartcardDebug(DEBUG_PROTOCOL,("%s! writing PTS request: ",DRIVER_NAME));
  691. for (k = 0;k < 4;k++)
  692. SmartcardDebug(DEBUG_PROTOCOL,("%2.2x ",abPTSRequest[k]));
  693. SmartcardDebug(DEBUG_PROTOCOL,("\n"));
  694. }
  695. #endif
  696. NTStatus = CMMOB_WriteT1(ReaderExtension,4,abPTSRequest);
  697. if (NTStatus != STATUS_SUCCESS) {
  698. SmartcardDebug(DEBUG_ERROR,
  699. ("%s! writing PTS request failed\n", DRIVER_NAME));
  700. goto ExitSetProtocol;
  701. }
  702. // read back PTS data
  703. ulWaitTime=1000;
  704. if (SmartcardExtension->CardCapabilities.PtsData.Fl >= 8)
  705. ulWaitTime=1400;
  706. NTStatus = CMMOB_ReadT1(ReaderExtension,4,
  707. ulWaitTime,ulWaitTime,
  708. abPTSReply,&ulBytesRead);
  709. // in case of an short PTS reply an timeout will occur,
  710. // but that's not the standard case
  711. if (NTStatus != STATUS_SUCCESS && NTStatus != STATUS_IO_TIMEOUT) {
  712. SmartcardDebug(DEBUG_ERROR,
  713. ("%s! reading PTS reply: failed\n",DRIVER_NAME));
  714. goto ExitSetProtocol;
  715. }
  716. #if DBG
  717. {
  718. ULONG k;
  719. SmartcardDebug(DEBUG_PROTOCOL,("%s! reading PTS reply: ",DRIVER_NAME));
  720. for (k = 0;k < ulBytesRead;k++)
  721. SmartcardDebug(DEBUG_PROTOCOL,("%2.2x ",abPTSReply[k]));
  722. SmartcardDebug(DEBUG_PROTOCOL,("\n"));
  723. }
  724. #endif
  725. if (ulBytesRead == 4 &&
  726. abPTSReply[0] == abPTSRequest[0] &&
  727. abPTSReply[1] == abPTSRequest[1] &&
  728. abPTSReply[2] == abPTSRequest[2] &&
  729. abPTSReply[3] == abPTSRequest[3] ) {
  730. SmartcardDebug(DEBUG_PROTOCOL,
  731. ("%s! PTS request and reply match\n",DRIVER_NAME));
  732. if ((SmartcardExtension->CardCapabilities.PtsData.Fl >= 3 &&
  733. SmartcardExtension->CardCapabilities.PtsData.Fl < 8) ||
  734. (SmartcardExtension->CardCapabilities.PtsData.Fl >= 11 &&
  735. SmartcardExtension->CardCapabilities.PtsData.Fl < 16)) {
  736. ReaderExtension->CardParameters.bClockFrequency=8;
  737. }
  738. ulBaudRateDivider = Fi[SmartcardExtension->CardCapabilities.PtsData.Fl] /
  739. Di[SmartcardExtension->CardCapabilities.PtsData.Dl];
  740. // decrease by 1, because these values have to be written to CardMan
  741. ulBaudRateDivider--;
  742. if (ulBaudRateDivider < 512) {
  743. ReaderExtension->CardParameters.bBaudRateLow=(UCHAR)(ulBaudRateDivider & 0xFF);
  744. if (ulBaudRateDivider>255) {
  745. ReaderExtension->CardParameters.bBaudRateHigh=1;
  746. } else {
  747. ReaderExtension->CardParameters.bBaudRateHigh=0;
  748. }
  749. NTStatus = CMMOB_SetCardParameters (ReaderExtension);
  750. if (NTStatus == STATUS_SUCCESS) {
  751. //
  752. // we had success, leave the loop
  753. //
  754. break;
  755. }
  756. }
  757. }
  758. if (ulBytesRead == 3 &&
  759. abPTSReply[0] == abPTSRequest[0] &&
  760. (abPTSReply[1] & 0x7F) == (abPTSRequest[1] & 0x0F) &&
  761. abPTSReply[2] == (BYTE)(abPTSReply[0] ^ abPTSReply[1] )) {
  762. SmartcardDebug(DEBUG_PROTOCOL,
  763. ("%s! Short PTS reply received\n",DRIVER_NAME));
  764. if (SmartcardExtension->CardCapabilities.PtsData.Fl >= 9) {
  765. ulBaudRateDivider = 512;
  766. } else {
  767. ulBaudRateDivider = 372;
  768. }
  769. // decrease by 1, because these values have to be written to CardMan
  770. ulBaudRateDivider--;
  771. NTStatus = CMMOB_SetCardParameters (ReaderExtension);
  772. if (NTStatus == STATUS_SUCCESS) {
  773. //
  774. // we had success, leave the loop
  775. //
  776. break;
  777. }
  778. }
  779. if (i==0) {
  780. // this was the first try
  781. // we have a second with default values
  782. SmartcardDebug(DEBUG_PROTOCOL,
  783. ("%s! PTS failed : Trying default parameters\n",DRIVER_NAME));
  784. // the card did either not reply or it replied incorrectly
  785. // so try default values
  786. SmartcardExtension->MinorIoControlCode = SCARD_COLD_RESET;
  787. NTStatus = CMMOB_CardPower(SmartcardExtension);
  788. } else {
  789. // the card failed the PTS request
  790. NTStatus = STATUS_DEVICE_PROTOCOL_ERROR;
  791. }
  792. }
  793. }
  794. }
  795. ExitSetProtocol:
  796. //
  797. // if protocol selection failed, prevent from calling invalid protocols
  798. //
  799. KeAcquireSpinLock(&SmartcardExtension->OsData->SpinLock,
  800. &irql);
  801. if (NTStatus==STATUS_SUCCESS) {
  802. SmartcardExtension->ReaderCapabilities.CurrentState = SCARD_SPECIFIC;
  803. } else {
  804. SmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  805. }
  806. KeReleaseSpinLock(&SmartcardExtension->OsData->SpinLock,
  807. irql);
  808. //
  809. // Return the selected protocol to the caller.
  810. //
  811. *(PULONG) (SmartcardExtension->IoRequest.ReplyBuffer) = SmartcardExtension->CardCapabilities.Protocol.Selected;
  812. *SmartcardExtension->IoRequest.Information = sizeof( ULONG );
  813. ReaderExtension->fTActive=FALSE;
  814. CMMOB_SetFlags1(ReaderExtension);
  815. SmartcardDebug(DEBUG_TRACE,
  816. ("%s!SetProtocol: Exit %X\n",DRIVER_NAME,NTStatus ));
  817. return( NTStatus );
  818. }
  819. /*****************************************************************************
  820. CMMOB_Transmit:
  821. callback handler for SMCLIB RDF_TRANSMIT
  822. Arguments:
  823. SmartcardExtension context of call
  824. Return Value:
  825. STATUS_SUCCESS
  826. STATUS_NO_MEDIA
  827. STATUS_TIMEOUT
  828. STATUS_INVALID_DEVICE_REQUEST
  829. ******************************************************************************/
  830. NTSTATUS CMMOB_Transmit (
  831. PSMARTCARD_EXTENSION SmartcardExtension
  832. )
  833. {
  834. NTSTATUS NTStatus = STATUS_SUCCESS;
  835. SmartcardDebug(DEBUG_TRACE,
  836. ("%s!Transmit: Enter\n",DRIVER_NAME ));
  837. //
  838. // dispatch on the selected protocol
  839. //
  840. switch (SmartcardExtension->CardCapabilities.Protocol.Selected) {
  841. case SCARD_PROTOCOL_T0:
  842. NTStatus = CMMOB_TransmitT0(SmartcardExtension);
  843. break;
  844. case SCARD_PROTOCOL_T1:
  845. NTStatus = CMMOB_TransmitT1(SmartcardExtension);
  846. break;
  847. /*
  848. case SCARD_PROTOCOL_RAW:
  849. break;
  850. */
  851. default:
  852. NTStatus = STATUS_INVALID_DEVICE_REQUEST;
  853. break;
  854. }
  855. SmartcardDebug(DEBUG_TRACE,
  856. ("%s!Transmit: Exit %X\n",DRIVER_NAME,NTStatus ));
  857. return( NTStatus );
  858. }
  859. /*****************************************************************************
  860. CMMOB_TransmitT0:
  861. callback handler for SMCLIB RDF_TRANSMIT
  862. Arguments:
  863. SmartcardExtension context of call
  864. Return Value:
  865. STATUS_SUCCESS
  866. STATUS_NO_MEDIA
  867. STATUS_TIMEOUT
  868. STATUS_INVALID_DEVICE_REQUEST
  869. ******************************************************************************/
  870. NTSTATUS CMMOB_TransmitT0 (
  871. PSMARTCARD_EXTENSION SmartcardExtension
  872. )
  873. {
  874. NTSTATUS NTStatus;
  875. UCHAR abWriteBuffer[MIN_BUFFER_SIZE];
  876. UCHAR abReadBuffer[MIN_BUFFER_SIZE];
  877. ULONG ulBytesToWrite; //length written to card
  878. ULONG ulBytesToReceive; //length expected from card
  879. ULONG ulBytesToRead; //length expected from reader
  880. ULONG ulBytesRead; //length received from reader
  881. //(without length written)
  882. ULONG ulCWTWaitTime;
  883. BOOLEAN fDataSent; //data longer than T0_HEADER
  884. SmartcardExtension->ReaderExtension->fTActive=TRUE;
  885. NTStatus = CMMOB_SetFlags1(SmartcardExtension->ReaderExtension);
  886. if (NTStatus != STATUS_SUCCESS) {
  887. goto ExitTransmitT0;
  888. }
  889. // reset the state machine of the reader
  890. NTStatus = CMMOB_ResetReader (SmartcardExtension->ReaderExtension);
  891. if (NTStatus!=STATUS_SUCCESS) {
  892. // there must be severe error
  893. goto ExitTransmitT0;
  894. }
  895. // set T0 mode
  896. SmartcardExtension->ReaderExtension->CardParameters.fT0Mode=TRUE;
  897. // increase timeout for T0 Transmission
  898. ulCWTWaitTime = SmartcardExtension->CardCapabilities.T0.WT/1000 + 1500;
  899. //
  900. // Let the lib build a T=0 packet
  901. //
  902. // no bytes additionally needed
  903. SmartcardExtension->SmartcardRequest.BufferLength = 0;
  904. NTStatus = SmartcardT0Request(SmartcardExtension);
  905. if (NTStatus != STATUS_SUCCESS) {
  906. // the lib detected an error in the data to send.
  907. goto ExitTransmitT0;
  908. }
  909. // copy data to the write buffer
  910. ulBytesToWrite = T0_HEADER_LEN + SmartcardExtension->T0.Lc;
  911. RtlCopyMemory(abWriteBuffer,SmartcardExtension->SmartcardRequest.Buffer,ulBytesToWrite);
  912. ulBytesToReceive = SmartcardExtension->T0.Le;
  913. #if DBG
  914. {
  915. ULONG i;
  916. SmartcardDebug(DEBUG_PROTOCOL,("%s!TransmitT0: Request ",DRIVER_NAME));
  917. for (i = 0;i < ulBytesToWrite;i++)
  918. SmartcardDebug(DEBUG_PROTOCOL,("%2.2x ",abWriteBuffer[i]));
  919. SmartcardDebug(DEBUG_PROTOCOL,("\n"));
  920. }
  921. #endif
  922. // set T0 write flag correctly
  923. if (ulBytesToReceive == 0) {
  924. SmartcardExtension->ReaderExtension->CardParameters.fT0Write=TRUE;
  925. } else {
  926. SmartcardExtension->ReaderExtension->CardParameters.fT0Write=FALSE;
  927. }
  928. NTStatus=CMMOB_SetCardParameters(SmartcardExtension->ReaderExtension);
  929. if (NTStatus != STATUS_SUCCESS)
  930. goto ExitTransmitT0;
  931. NTStatus = CMMOB_WriteT0 (SmartcardExtension->ReaderExtension,
  932. ulBytesToWrite,
  933. ulBytesToReceive,
  934. abWriteBuffer);
  935. if (NTStatus != STATUS_SUCCESS) {
  936. goto ExitTransmitT0;
  937. }
  938. // bytes to write + answer + SW2
  939. if ( (MAXULONG - ulBytesToWrite < ulBytesToReceive + 1) ||
  940. (ulBytesToReceive == MAXULONG)) {
  941. NTStatus = STATUS_BUFFER_OVERFLOW;
  942. goto ExitTransmitT0;
  943. }
  944. ulBytesToRead = ulBytesToWrite + ulBytesToReceive + 1;
  945. if (ulBytesToRead > MIN_BUFFER_SIZE) {
  946. NTStatus = STATUS_BUFFER_OVERFLOW;
  947. goto ExitTransmitT0;
  948. }
  949. NTStatus = CMMOB_ReadT0 (SmartcardExtension->ReaderExtension,
  950. ulBytesToRead,
  951. ulBytesToWrite,
  952. ulCWTWaitTime,
  953. abReadBuffer,
  954. &ulBytesRead,
  955. &fDataSent);
  956. #if DBG
  957. {
  958. ULONG i;
  959. SmartcardDebug(DEBUG_PROTOCOL,("%s!TransmitT0: Reply ",DRIVER_NAME));
  960. for (i = 0;i < ulBytesRead;i++)
  961. SmartcardDebug(DEBUG_PROTOCOL,("%2.2x ",abReadBuffer[i]));
  962. SmartcardDebug(DEBUG_PROTOCOL,("\n"));
  963. }
  964. #endif
  965. if (NTStatus != STATUS_SUCCESS) {
  966. SmartcardDebug(DEBUG_PROTOCOL,("%s!TransmitT0: Read failed!\n",DRIVER_NAME));
  967. goto ExitTransmitT0;
  968. }
  969. // copy received bytes
  970. if (ulBytesRead <= SmartcardExtension->SmartcardReply.BufferSize) {
  971. RtlCopyBytes((PVOID)SmartcardExtension->SmartcardReply.Buffer,
  972. (PVOID) abReadBuffer,
  973. ulBytesRead);
  974. SmartcardExtension->SmartcardReply.BufferLength = ulBytesRead;
  975. } else {
  976. NTStatus=STATUS_BUFFER_OVERFLOW;
  977. goto ExitTransmitT0;
  978. }
  979. // let the lib copy the received bytes to the user buffer
  980. NTStatus = SmartcardT0Reply(SmartcardExtension);
  981. if (NTStatus != STATUS_SUCCESS) {
  982. goto ExitTransmitT0;
  983. }
  984. ExitTransmitT0:
  985. // ------------------------------------------
  986. // ITSEC E2 requirements: clear write buffers
  987. // ------------------------------------------
  988. RtlFillMemory((PVOID)abWriteBuffer,sizeof(abWriteBuffer),0x00);
  989. RtlFillMemory((PVOID)SmartcardExtension->SmartcardRequest.Buffer,
  990. SmartcardExtension->SmartcardRequest.BufferSize,0x00);
  991. // set T0 mode back
  992. SmartcardExtension->ReaderExtension->CardParameters.fT0Mode=FALSE;
  993. SmartcardExtension->ReaderExtension->CardParameters.fT0Write=FALSE;
  994. CMMOB_SetCardParameters(SmartcardExtension->ReaderExtension);
  995. SmartcardExtension->ReaderExtension->fTActive=FALSE;
  996. CMMOB_SetFlags1(SmartcardExtension->ReaderExtension);
  997. return NTStatus;
  998. }
  999. /*****************************************************************************
  1000. CMMOB_TransmitT1:
  1001. callback handler for SMCLIB RDF_TRANSMIT
  1002. Arguments:
  1003. SmartcardExtension context of call
  1004. Return Value:
  1005. STATUS_SUCCESS
  1006. STATUS_NO_MEDIA
  1007. STATUS_TIMEOUT
  1008. STATUS_INVALID_DEVICE_REQUEST
  1009. ******************************************************************************/
  1010. NTSTATUS CMMOB_TransmitT1 (
  1011. PSMARTCARD_EXTENSION SmartcardExtension
  1012. )
  1013. {
  1014. NTSTATUS NTStatus;
  1015. UCHAR abReadBuffer[CMMOB_MAXBUFFER];
  1016. LONG lBytesToRead;
  1017. ULONG ulBytesRead;
  1018. ULONG ulCurrentWaitTime;
  1019. ULONG ulCWTWaitTime;
  1020. ULONG ulBWTWaitTime;
  1021. ULONG ulWTXWaitTime;
  1022. ULONG ulTemp;
  1023. SmartcardDebug(DEBUG_PROTOCOL,
  1024. ("%s!TransmitT1 CWT = %ld(ms)\n",DRIVER_NAME,
  1025. SmartcardExtension->CardCapabilities.T1.CWT/1000));
  1026. SmartcardDebug(DEBUG_PROTOCOL,
  1027. ("%s!TransmitT1 BWT = %ld(ms)\n",DRIVER_NAME,
  1028. SmartcardExtension->CardCapabilities.T1.BWT/1000));
  1029. ulCWTWaitTime = (ULONG)(100 + 32*(SmartcardExtension->CardCapabilities.T1.CWT/1000));
  1030. ulBWTWaitTime = (ULONG)(1000 + SmartcardExtension->CardCapabilities.T1.BWT/1000);
  1031. ulWTXWaitTime = 0;
  1032. SmartcardExtension->ReaderExtension->fTActive=TRUE;
  1033. NTStatus = CMMOB_SetFlags1(SmartcardExtension->ReaderExtension);
  1034. if (NTStatus != STATUS_SUCCESS) {
  1035. goto ExitTransmitT1;
  1036. }
  1037. // reset the state machine of the reader
  1038. NTStatus = CMMOB_ResetReader (SmartcardExtension->ReaderExtension);
  1039. if (NTStatus!=STATUS_SUCCESS) {
  1040. // there must be severe error
  1041. goto ExitTransmitT1;
  1042. }
  1043. do {
  1044. // no bytes additionally needed
  1045. SmartcardExtension->SmartcardRequest.BufferLength = 0;
  1046. NTStatus = SmartcardT1Request(SmartcardExtension);
  1047. if (NTStatus != STATUS_SUCCESS) {
  1048. // this should never happen, so we return immediately
  1049. goto ExitTransmitT1;
  1050. }
  1051. #if DBG
  1052. {
  1053. ULONG i;
  1054. SmartcardDebug(DEBUG_PROTOCOL,("%s!TransmitT1: Request ",DRIVER_NAME));
  1055. for (i = 0;i < SmartcardExtension->SmartcardRequest.BufferLength;i++)
  1056. SmartcardDebug(DEBUG_PROTOCOL,("%2.2x ",SmartcardExtension->SmartcardRequest.Buffer[i]));
  1057. SmartcardDebug(DEBUG_PROTOCOL,("\n"));
  1058. }
  1059. #endif
  1060. // write to the reader
  1061. NTStatus = CMMOB_WriteT1 (SmartcardExtension->ReaderExtension,
  1062. SmartcardExtension->SmartcardRequest.BufferLength,
  1063. SmartcardExtension->SmartcardRequest.Buffer);
  1064. if (NTStatus == STATUS_SUCCESS) {
  1065. if (ulWTXWaitTime == 0 ) { // use BWT
  1066. /*
  1067. SmartcardDebug(DEBUG_TRACE,
  1068. ("%s!ulCurrentWaitTime = %ld\n",DRIVER_NAME,ulCurrentWaitTime));
  1069. */
  1070. ulCurrentWaitTime = ulBWTWaitTime;
  1071. } else { // use WTX time
  1072. /*
  1073. SmartcardDebug(DEBUG_TRACE,
  1074. ("%s!ulCurrentWaitTime = %ld\n",DRIVER_NAME,ulWTXWaitTime));
  1075. */
  1076. ulCurrentWaitTime = ulWTXWaitTime;
  1077. }
  1078. if (SmartcardExtension->CardCapabilities.T1.EDC == T1_CRC_CHECK) {
  1079. // in case of card with CRC check read reply + 5 bytes
  1080. // a negative value indicates a relative number of bytes to read
  1081. lBytesToRead=-5;
  1082. } else {
  1083. // in case of card with CRC check read reply + 4 bytes
  1084. // a negative value indicates a relative number of bytes to read
  1085. lBytesToRead=-4;
  1086. }
  1087. NTStatus = CMMOB_ReadT1(SmartcardExtension->ReaderExtension,lBytesToRead,
  1088. ulCurrentWaitTime,ulCWTWaitTime,abReadBuffer,&ulBytesRead);
  1089. if (NTStatus == STATUS_SUCCESS) {
  1090. if (abReadBuffer[1] == T1_WTX_REQUEST) {
  1091. ulWTXWaitTime = (ULONG)(1000 +((SmartcardExtension->CardCapabilities.T1.BWT*abReadBuffer[3])/1000));
  1092. SmartcardDebug(DEBUG_PROTOCOL,
  1093. ("%s!TransmitT1 WTX = %ld(ms)\n",DRIVER_NAME,ulWTXWaitTime));
  1094. } else {
  1095. ulWTXWaitTime = 0;
  1096. }
  1097. #if DBG
  1098. {
  1099. ULONG i;
  1100. SmartcardDebug(DEBUG_PROTOCOL,("%s!TransmitT1: Reply ",DRIVER_NAME));
  1101. for (i = 0;i < ulBytesRead;i++)
  1102. SmartcardDebug(DEBUG_PROTOCOL,("%2.2x ",abReadBuffer[i]));
  1103. SmartcardDebug(DEBUG_PROTOCOL,("\n"));
  1104. }
  1105. #endif
  1106. // copy received bytes
  1107. if (ulBytesRead <= SmartcardExtension->SmartcardReply.BufferSize) {
  1108. RtlCopyBytes((PVOID)SmartcardExtension->SmartcardReply.Buffer,
  1109. (PVOID)abReadBuffer,
  1110. ulBytesRead);
  1111. SmartcardExtension->SmartcardReply.BufferLength = ulBytesRead;
  1112. } else {
  1113. NTStatus=STATUS_BUFFER_OVERFLOW;
  1114. goto ExitTransmitT1;
  1115. }
  1116. }
  1117. }
  1118. if (NTStatus != STATUS_SUCCESS) {
  1119. SmartcardExtension->SmartcardReply.BufferLength = 0L;
  1120. }
  1121. // bug fix for smclib
  1122. if (SmartcardExtension->T1.State == T1_IFS_RESPONSE &&
  1123. SmartcardExtension->T1.OriginalState == T1_I_BLOCK) {
  1124. SmartcardExtension->T1.State = T1_I_BLOCK;
  1125. }
  1126. NTStatus = SmartcardT1Reply(SmartcardExtension);
  1127. }
  1128. while (NTStatus == STATUS_MORE_PROCESSING_REQUIRED);
  1129. ExitTransmitT1:
  1130. // ------------------------------------------
  1131. // ITSEC E2 requirements: clear write buffers
  1132. // ------------------------------------------
  1133. RtlFillMemory((PVOID)SmartcardExtension->SmartcardRequest.Buffer,
  1134. SmartcardExtension->SmartcardRequest.BufferSize,0x00);
  1135. SmartcardExtension->ReaderExtension->fTActive=FALSE;
  1136. CMMOB_SetFlags1(SmartcardExtension->ReaderExtension);
  1137. return NTStatus;
  1138. }
  1139. /*****************************************************************************
  1140. CMMOB_IoCtlVendor:
  1141. Performs generic callbacks to the reader
  1142. Arguments:
  1143. SmartcardExtension context of the call
  1144. Return Value:
  1145. STATUS_SUCCESS
  1146. ******************************************************************************/
  1147. NTSTATUS CMMOB_IoCtlVendor(
  1148. PSMARTCARD_EXTENSION SmartcardExtension
  1149. )
  1150. {
  1151. NTSTATUS NTStatus=STATUS_SUCCESS;
  1152. PIRP Irp;
  1153. PIO_STACK_LOCATION IrpStack;
  1154. SmartcardDebug(DEBUG_TRACE,
  1155. ("%s!IoCtlVendor: Enter\n",DRIVER_NAME ));
  1156. //
  1157. // get pointer to current IRP stack location
  1158. //
  1159. Irp = SmartcardExtension->OsData->CurrentIrp;
  1160. IrpStack = IoGetCurrentIrpStackLocation( Irp );
  1161. Irp->IoStatus.Information = 0;
  1162. //
  1163. // dispatch IOCTL
  1164. //
  1165. switch (IrpStack->Parameters.DeviceIoControl.IoControlCode) {
  1166. case CM_IOCTL_GET_FW_VERSION:
  1167. NTStatus = CMMOB_GetFWVersion(SmartcardExtension);
  1168. break;
  1169. case CM_IOCTL_CR80S_SAMOS_SET_HIGH_SPEED:
  1170. NTStatus = CMMOB_SetHighSpeed_CR80S_SAMOS(SmartcardExtension);
  1171. break;
  1172. case CM_IOCTL_SET_READER_9600_BAUD:
  1173. NTStatus = CMMOB_SetReader_9600Baud(SmartcardExtension);
  1174. break;
  1175. case CM_IOCTL_SET_READER_38400_BAUD:
  1176. NTStatus = CMMOB_SetReader_38400Baud(SmartcardExtension);
  1177. break;
  1178. case CM_IOCTL_READ_DEVICE_DESCRIPTION:
  1179. NTStatus = CMMOB_ReadDeviceDescription(SmartcardExtension);
  1180. break;
  1181. default:
  1182. NTStatus = STATUS_INVALID_DEVICE_REQUEST;
  1183. break;
  1184. }
  1185. //
  1186. // set NTStatus of the packet
  1187. //
  1188. Irp->IoStatus.Status = NTStatus;
  1189. SmartcardDebug(DEBUG_TRACE,
  1190. ("%s!IoCtlVendor: Exit %X\n",DRIVER_NAME,NTStatus ));
  1191. return( NTStatus );
  1192. }
  1193. /*****************************************************************************
  1194. Routine Description:
  1195. Arguments:
  1196. Return Value: STATUS_UNSUCCESSFUL
  1197. STATUS_SUCCESS
  1198. *****************************************************************************/
  1199. NTSTATUS CMMOB_SetReader_9600Baud (
  1200. IN PSMARTCARD_EXTENSION SmartcardExtension
  1201. )
  1202. {
  1203. NTSTATUS NTStatus = STATUS_SUCCESS;
  1204. KIRQL irql;
  1205. SmartcardDebug(DEBUG_TRACE,
  1206. ("%s!SetReader_9600Baud: Enter\n",DRIVER_NAME));
  1207. // check if card is already in specific mode
  1208. KeAcquireSpinLock(&SmartcardExtension->OsData->SpinLock,
  1209. &irql);
  1210. if (SmartcardExtension->ReaderCapabilities.CurrentState != SCARD_SPECIFIC) {
  1211. KeReleaseSpinLock(&SmartcardExtension->OsData->SpinLock,
  1212. irql);
  1213. NTStatus = STATUS_INVALID_DEVICE_REQUEST;
  1214. goto ExitSetReader9600;
  1215. }
  1216. KeReleaseSpinLock(&SmartcardExtension->OsData->SpinLock,
  1217. irql);
  1218. // set 9600 Baud for 3.58 MHz
  1219. SmartcardExtension->ReaderExtension->CardParameters.bBaudRateHigh=0x01;
  1220. SmartcardExtension->ReaderExtension->CardParameters.bBaudRateLow=0x73;
  1221. NTStatus = CMMOB_SetCardParameters (SmartcardExtension->ReaderExtension);
  1222. ExitSetReader9600:
  1223. *SmartcardExtension->IoRequest.Information = 0L;
  1224. SmartcardDebug(DEBUG_TRACE,
  1225. ("%s!SetReader_9600Baud: Exit %lx\n",DRIVER_NAME,NTStatus));
  1226. return(NTStatus);
  1227. }
  1228. /*****************************************************************************
  1229. Routine Description:
  1230. Arguments:
  1231. Return Value: STATUS_UNSUCCESSFUL
  1232. STATUS_SUCCESS
  1233. *****************************************************************************/
  1234. NTSTATUS CMMOB_SetReader_38400Baud (
  1235. IN PSMARTCARD_EXTENSION SmartcardExtension
  1236. )
  1237. {
  1238. NTSTATUS NTStatus = STATUS_SUCCESS;
  1239. KIRQL irql;
  1240. SmartcardDebug(DEBUG_TRACE,
  1241. ("%s!SetReader_38400Baud: Enter\n",DRIVER_NAME));
  1242. // check if card is already in specific mode
  1243. KeAcquireSpinLock(&SmartcardExtension->OsData->SpinLock,
  1244. &irql);
  1245. if (SmartcardExtension->ReaderCapabilities.CurrentState != SCARD_SPECIFIC) {
  1246. KeReleaseSpinLock(&SmartcardExtension->OsData->SpinLock,
  1247. irql);
  1248. NTStatus = STATUS_INVALID_DEVICE_REQUEST;
  1249. goto ExitSetReader38400;
  1250. }
  1251. KeReleaseSpinLock(&SmartcardExtension->OsData->SpinLock,
  1252. irql);
  1253. // set 384000 Baud for 3.58 MHz card
  1254. SmartcardExtension->ReaderExtension->CardParameters.bBaudRateHigh=0x00;
  1255. SmartcardExtension->ReaderExtension->CardParameters.bBaudRateLow=0x5D;
  1256. NTStatus = CMMOB_SetCardParameters (SmartcardExtension->ReaderExtension);
  1257. ExitSetReader38400:
  1258. *SmartcardExtension->IoRequest.Information = 0L;
  1259. SmartcardDebug(DEBUG_TRACE,
  1260. ("%s!SetReader_38400Baud: Exit %lx\n",DRIVER_NAME,NTStatus));
  1261. return(NTStatus);
  1262. }
  1263. /*****************************************************************************
  1264. Routine Description:
  1265. Arguments:
  1266. Return Value: STATUS_UNSUCCESSFUL
  1267. STATUS_SUCCESS
  1268. *****************************************************************************/
  1269. NTSTATUS CMMOB_SetHighSpeed_CR80S_SAMOS (
  1270. IN PSMARTCARD_EXTENSION SmartcardExtension
  1271. )
  1272. {
  1273. NTSTATUS NTStatus;
  1274. UCHAR abCR80S_SAMOS_SET_HIGH_SPEED[4] = {0xFF,0x11,0x94,0x7A};
  1275. SmartcardDebug(DEBUG_TRACE,
  1276. ("%s!SetHighSpeed_CR80S_SAMOS: Enter\n",DRIVER_NAME));
  1277. NTStatus = CMMOB_SetSpeed (SmartcardExtension,
  1278. abCR80S_SAMOS_SET_HIGH_SPEED);
  1279. SmartcardDebug(DEBUG_TRACE,
  1280. ("%s!SetHighSpeed_CR80S_SAMOS: Exit %lx\n",DRIVER_NAME,NTStatus));
  1281. return(NTStatus);
  1282. }
  1283. /*****************************************************************************
  1284. Routine Description:
  1285. Arguments:
  1286. Return Value: STATUS_UNSUCCESSFUL
  1287. STATUS_SUCCESS
  1288. *****************************************************************************/
  1289. NTSTATUS CMMOB_SetSpeed (
  1290. IN PSMARTCARD_EXTENSION SmartcardExtension,
  1291. IN PUCHAR abFIDICommand
  1292. )
  1293. {
  1294. NTSTATUS NTStatus;
  1295. NTSTATUS DebugStatus;
  1296. UCHAR abReadBuffer[16];
  1297. ULONG ulBytesRead;
  1298. ULONG ulWaitTime;
  1299. SmartcardDebug(DEBUG_TRACE,
  1300. ("%s!SetSpeed: Enter\n",DRIVER_NAME));
  1301. SmartcardExtension->ReaderExtension->fTActive=TRUE;
  1302. NTStatus = CMMOB_SetFlags1(SmartcardExtension->ReaderExtension);
  1303. if (NTStatus != STATUS_SUCCESS) {
  1304. goto ExitSetSpeed;
  1305. }
  1306. #if DBG
  1307. {
  1308. ULONG k;
  1309. SmartcardDebug(DEBUG_PROTOCOL,("%s!SetSpeed: writing: ",DRIVER_NAME));
  1310. for (k = 0;k < 4;k++)
  1311. SmartcardDebug(DEBUG_PROTOCOL,("%2.2x ",abFIDICommand[k]));
  1312. SmartcardDebug(DEBUG_PROTOCOL,("\n"));
  1313. }
  1314. #endif
  1315. NTStatus = CMMOB_WriteT1(SmartcardExtension->ReaderExtension,4,
  1316. abFIDICommand);
  1317. if (NTStatus != STATUS_SUCCESS) {
  1318. SmartcardDebug(DEBUG_ERROR,
  1319. ("%s!SetSpeed: writing high speed command failed\n",DRIVER_NAME));
  1320. goto ExitSetSpeed;
  1321. }
  1322. // read back pts data
  1323. // maximim initial waiting time is 9600 * etu
  1324. // clock divider of this card 512 => 1.4 sec is sufficient
  1325. ulWaitTime = 1400;
  1326. NTStatus = CMMOB_ReadT1(SmartcardExtension->ReaderExtension,4,
  1327. ulWaitTime,ulWaitTime,abReadBuffer,&ulBytesRead);
  1328. SmartcardDebug(DEBUG_PROTOCOL,
  1329. ("%s!SetSpeed: reading echo: ",DRIVER_NAME));
  1330. if (NTStatus != STATUS_SUCCESS) {
  1331. SmartcardDebug(DEBUG_PROTOCOL,("failed\n"));
  1332. goto ExitSetSpeed;
  1333. }
  1334. #if DBG
  1335. {
  1336. ULONG k;
  1337. for (k = 0;k < ulBytesRead;k++)
  1338. SmartcardDebug(DEBUG_PROTOCOL,("%2.2x ",abReadBuffer[k]));
  1339. SmartcardDebug(DEBUG_PROTOCOL,("\n"));
  1340. }
  1341. #endif
  1342. // if the card has accepted this string , the string is echoed
  1343. if (abReadBuffer[0] == abFIDICommand[0] &&
  1344. abReadBuffer[1] == abFIDICommand[1] &&
  1345. abReadBuffer[2] == abFIDICommand[2] &&
  1346. abReadBuffer[3] == abFIDICommand[3] ) {
  1347. SmartcardExtension->ReaderExtension->CardParameters.bBaudRateLow=63;
  1348. SmartcardExtension->ReaderExtension->CardParameters.bBaudRateHigh=0;
  1349. NTStatus = CMMOB_SetCardParameters (SmartcardExtension->ReaderExtension);
  1350. } else {
  1351. SmartcardExtension->MinorIoControlCode = SCARD_COLD_RESET;
  1352. CMMOB_CardPower(SmartcardExtension);
  1353. NTStatus = STATUS_UNSUCCESSFUL;
  1354. }
  1355. ExitSetSpeed:
  1356. *SmartcardExtension->IoRequest.Information = 0L;
  1357. if (NTStatus != STATUS_SUCCESS) {
  1358. NTStatus = STATUS_UNSUCCESSFUL;
  1359. }
  1360. SmartcardExtension->ReaderExtension->fTActive=FALSE;
  1361. CMMOB_SetFlags1(SmartcardExtension->ReaderExtension);
  1362. SmartcardDebug(DEBUG_TRACE,
  1363. ("%s!SetSpeed: Exit %lx\n",DRIVER_NAME,NTStatus));
  1364. return NTStatus;
  1365. }
  1366. /*****************************************************************************
  1367. Routine Description:
  1368. This function always returns 'CardManMobile'.
  1369. Arguments: pointer to SMARTCARD_EXTENSION
  1370. Return Value: NT status
  1371. *****************************************************************************/
  1372. NTSTATUS CMMOB_ReadDeviceDescription(
  1373. IN PSMARTCARD_EXTENSION SmartcardExtension
  1374. )
  1375. {
  1376. NTSTATUS NTStatus = STATUS_SUCCESS;
  1377. BYTE abDeviceDescription[] = "CardManMobile";
  1378. SmartcardDebug(DEBUG_TRACE,
  1379. ("%s!ReadDeviceDescription : Enter\n",DRIVER_NAME));
  1380. if (SmartcardExtension->IoRequest.ReplyBufferLength < sizeof(abDeviceDescription)) {
  1381. NTStatus = STATUS_BUFFER_OVERFLOW;
  1382. *SmartcardExtension->IoRequest.Information = 0L;
  1383. goto ExitReadDeviceDescription;
  1384. } else {
  1385. RtlCopyBytes((PVOID)SmartcardExtension->IoRequest.ReplyBuffer,
  1386. (PVOID)abDeviceDescription,sizeof(abDeviceDescription));
  1387. *SmartcardExtension->IoRequest.Information = sizeof(abDeviceDescription);
  1388. }
  1389. ExitReadDeviceDescription:
  1390. SmartcardDebug(DEBUG_TRACE,
  1391. ("%s!ReadDeviceDescription : Exit %lx\n",DRIVER_NAME,NTStatus));
  1392. return NTStatus;
  1393. }
  1394. /*****************************************************************************
  1395. Routine Description:
  1396. Arguments:
  1397. Return Value:
  1398. *****************************************************************************/
  1399. NTSTATUS CMMOB_GetFWVersion (
  1400. IN PSMARTCARD_EXTENSION SmartcardExtension
  1401. )
  1402. {
  1403. NTSTATUS NTStatus = STATUS_SUCCESS;
  1404. SmartcardDebug(DEBUG_TRACE,
  1405. ("%s!GetFWVersion : Enter\n",DRIVER_NAME));
  1406. if (SmartcardExtension->IoRequest.ReplyBufferLength < sizeof (ULONG)) {
  1407. NTStatus = STATUS_BUFFER_OVERFLOW;
  1408. *SmartcardExtension->IoRequest.Information = 0;
  1409. } else {
  1410. *(PULONG)(SmartcardExtension->IoRequest.ReplyBuffer) =
  1411. SmartcardExtension->ReaderExtension->ulFWVersion;
  1412. *SmartcardExtension->IoRequest.Information = sizeof(ULONG);
  1413. }
  1414. SmartcardDebug(DEBUG_TRACE,
  1415. ("%s!GetFWVersion : Exit %lx\n",DRIVER_NAME,NTStatus));
  1416. return NTStatus;
  1417. }
  1418. /*****************************************************************************
  1419. CMMOB_CardTracking:
  1420. callback handler for SMCLIB RDF_CARD_TRACKING. the requested event was
  1421. validated by the smclib (i.e. a card removal request will only be passed
  1422. if a card is present).
  1423. for a win95 build STATUS_PENDING will be returned without any other action.
  1424. for NT the cancel routine for the irp will be set to the drivers cancel
  1425. routine.
  1426. Arguments:
  1427. SmartcardExtension context of call
  1428. Return Value:
  1429. STATUS_PENDING
  1430. ******************************************************************************/
  1431. NTSTATUS CMMOB_CardTracking(
  1432. PSMARTCARD_EXTENSION SmartcardExtension
  1433. )
  1434. {
  1435. KIRQL CurrentIrql;
  1436. SmartcardDebug(DEBUG_TRACE,
  1437. ("%s!CardTracking: Enter\n",DRIVER_NAME ));
  1438. //
  1439. // set cancel routine
  1440. //
  1441. IoAcquireCancelSpinLock( &CurrentIrql );
  1442. IoSetCancelRoutine(SmartcardExtension->OsData->NotificationIrp,
  1443. CMMOB_CancelCardTracking);
  1444. IoReleaseCancelSpinLock( CurrentIrql );
  1445. //
  1446. // Mark notification irp pending
  1447. //
  1448. IoMarkIrpPending(SmartcardExtension->OsData->NotificationIrp);
  1449. SmartcardDebug(DEBUG_TRACE,
  1450. ("%s!CardTracking: Exit\n",DRIVER_NAME ));
  1451. return( STATUS_PENDING );
  1452. }
  1453. /*****************************************************************************
  1454. CMMOB_CompleteCardTracking:
  1455. finishes a pending tracking request if the device will be unloaded
  1456. Arguments:
  1457. DeviceObject context of the request
  1458. NTStatus NTStatus to report to the calling process
  1459. Return Value:
  1460. ******************************************************************************/
  1461. VOID CMMOB_CompleteCardTracking(
  1462. PSMARTCARD_EXTENSION SmartcardExtension
  1463. )
  1464. {
  1465. KIRQL ioIrql, keIrql;
  1466. PIRP NotificationIrp;
  1467. IoAcquireCancelSpinLock(&ioIrql);
  1468. KeAcquireSpinLock(&SmartcardExtension->OsData->SpinLock,
  1469. &keIrql);
  1470. NotificationIrp = SmartcardExtension->OsData->NotificationIrp;
  1471. SmartcardExtension->OsData->NotificationIrp = NULL;
  1472. KeReleaseSpinLock(&SmartcardExtension->OsData->SpinLock,
  1473. keIrql);
  1474. if (NotificationIrp!=NULL) {
  1475. IoSetCancelRoutine(NotificationIrp, NULL);
  1476. }
  1477. IoReleaseCancelSpinLock(ioIrql);
  1478. if (NotificationIrp!=NULL) {
  1479. //finish the request
  1480. if (NotificationIrp->Cancel) {
  1481. NotificationIrp->IoStatus.Status = STATUS_CANCELLED;
  1482. } else {
  1483. NotificationIrp->IoStatus.Status = STATUS_SUCCESS;
  1484. }
  1485. NotificationIrp->IoStatus.Information = 0;
  1486. SmartcardDebug(DEBUG_DRIVER,
  1487. ("%s!CompleteCardTracking: Completing Irp %lx Status=%lx\n",
  1488. DRIVER_NAME, NotificationIrp,NotificationIrp->IoStatus.Status));
  1489. IoCompleteRequest(NotificationIrp, IO_NO_INCREMENT );
  1490. }
  1491. }
  1492. /*****************************************************************************
  1493. CMMOB_CancelCardTracking
  1494. This routine is called by the I/O system
  1495. when the irp should be cancelled
  1496. Arguments:
  1497. DeviceObject - Pointer to device object for this miniport
  1498. Irp - IRP involved.
  1499. Return Value:
  1500. STATUS_CANCELLED
  1501. ******************************************************************************/
  1502. NTSTATUS CMMOB_CancelCardTracking(
  1503. IN PDEVICE_OBJECT DeviceObject,
  1504. IN PIRP Irp
  1505. )
  1506. {
  1507. PDEVICE_EXTENSION DeviceExtension = DeviceObject->DeviceExtension;
  1508. PSMARTCARD_EXTENSION SmartcardExtension = &DeviceExtension->SmartcardExtension;
  1509. SmartcardDebug(DEBUG_TRACE,
  1510. ("%s!CancelCardTracking: Enter\n",DRIVER_NAME));
  1511. ASSERT(Irp == SmartcardExtension->OsData->NotificationIrp);
  1512. IoReleaseCancelSpinLock(Irp->CancelIrql);
  1513. CMMOB_CompleteCardTracking(SmartcardExtension);
  1514. SmartcardDebug(DEBUG_TRACE,
  1515. ("%s!CancelCardTracking: Exit\n",DRIVER_NAME));
  1516. return STATUS_CANCELLED;
  1517. }
  1518. /*****************************************************************************
  1519. CMMOB_StartCardTracking:
  1520. Arguments:
  1521. DeviceObject context of call
  1522. Return Value:
  1523. STATUS_SUCCESS
  1524. NTStatus returned by LowLevel routines
  1525. ******************************************************************************/
  1526. NTSTATUS CMMOB_StartCardTracking(
  1527. IN PDEVICE_OBJECT DeviceObject
  1528. )
  1529. {
  1530. NTSTATUS NTStatus = STATUS_SUCCESS;
  1531. HANDLE hThread;
  1532. PDEVICE_EXTENSION DeviceExtension;
  1533. PSMARTCARD_EXTENSION SmartcardExtension;
  1534. DeviceExtension = DeviceObject->DeviceExtension;
  1535. SmartcardExtension = &DeviceExtension->SmartcardExtension;
  1536. SmartcardDebug(DEBUG_TRACE,
  1537. ( "%s!StartCardTracking: Enter\n",DRIVER_NAME));
  1538. SmartcardDebug(DEBUG_DRIVER,
  1539. ( "%s!StartCardTracking: IRQL %i\n",DRIVER_NAME,KeGetCurrentIrql()));
  1540. KeWaitForSingleObject(&SmartcardExtension->ReaderExtension->CardManIOMutex,
  1541. Executive,
  1542. KernelMode,
  1543. FALSE,
  1544. NULL);
  1545. // settings for thread synchronization
  1546. SmartcardExtension->ReaderExtension->fTerminateUpdateThread = FALSE;
  1547. // create thread for updating current state
  1548. NTStatus = PsCreateSystemThread(&hThread,
  1549. THREAD_ALL_ACCESS,
  1550. NULL,
  1551. NULL,
  1552. NULL,
  1553. CMMOB_UpdateCurrentStateThread,
  1554. DeviceObject);
  1555. if (NT_SUCCESS(NTStatus)) {
  1556. //
  1557. // We've got the thread. Now get a pointer to it.
  1558. //
  1559. NTStatus = ObReferenceObjectByHandle(hThread,
  1560. THREAD_ALL_ACCESS,
  1561. NULL,
  1562. KernelMode,
  1563. &SmartcardExtension->ReaderExtension->ThreadObjectPointer,
  1564. NULL);
  1565. if (NT_ERROR(NTStatus)) {
  1566. ZwClose(hThread);
  1567. SmartcardExtension->ReaderExtension->fTerminateUpdateThread = TRUE;
  1568. } else {
  1569. //
  1570. // Now that we have a reference to the thread
  1571. // we can simply close the handle.
  1572. //
  1573. ZwClose(hThread);
  1574. SmartcardExtension->ReaderExtension->fUpdateThreadRunning = TRUE;
  1575. }
  1576. }
  1577. SmartcardDebug(DEBUG_DRIVER,
  1578. ("%s!-----------------------------------------------------------\n",DRIVER_NAME));
  1579. SmartcardDebug(DEBUG_DRIVER,
  1580. ("%s!STARTING THREAD\n",DRIVER_NAME));
  1581. SmartcardDebug(DEBUG_DRIVER,
  1582. ("%s!-----------------------------------------------------------\n",DRIVER_NAME));
  1583. KeReleaseMutex(&SmartcardExtension->ReaderExtension->CardManIOMutex,FALSE);
  1584. SmartcardDebug(DEBUG_TRACE,
  1585. ( "%s!CMMOB_StartCardTracking: Exit %lx\n",DRIVER_NAME,NTStatus));
  1586. return NTStatus;
  1587. }
  1588. /*****************************************************************************
  1589. CMMOB_StopCardTracking:
  1590. Arguments:
  1591. DeviceObject context of call
  1592. Return Value:
  1593. ******************************************************************************/
  1594. VOID CMMOB_StopCardTracking(
  1595. IN PDEVICE_OBJECT DeviceObject
  1596. )
  1597. {
  1598. PDEVICE_EXTENSION DeviceExtension;
  1599. PSMARTCARD_EXTENSION SmartcardExtension;
  1600. SmartcardDebug(DEBUG_TRACE,
  1601. ( "%s!StopCardTracking: Enter\n",DRIVER_NAME));
  1602. SmartcardDebug(DEBUG_DRIVER,
  1603. ( "%s!StopCardTracking: IRQL %i\n",DRIVER_NAME,KeGetCurrentIrql()));
  1604. DeviceExtension = DeviceObject->DeviceExtension;
  1605. SmartcardExtension = &DeviceExtension->SmartcardExtension;
  1606. if (SmartcardExtension->ReaderExtension->fUpdateThreadRunning) {
  1607. // kill thread
  1608. KeWaitForSingleObject(&SmartcardExtension->ReaderExtension->CardManIOMutex,
  1609. Executive,
  1610. KernelMode,
  1611. FALSE,
  1612. NULL );
  1613. SmartcardExtension->ReaderExtension->fTerminateUpdateThread = TRUE;
  1614. KeReleaseMutex(&SmartcardExtension->ReaderExtension->CardManIOMutex,FALSE);
  1615. //
  1616. // Wait on the thread handle, when the wait is satisfied, the
  1617. // thread has gone away.
  1618. //
  1619. KeWaitForSingleObject(SmartcardExtension->ReaderExtension->ThreadObjectPointer,
  1620. Executive,
  1621. KernelMode,
  1622. FALSE,
  1623. NULL);
  1624. }
  1625. SmartcardDebug(DEBUG_TRACE,
  1626. ( "%s!StopCardTracking: Exit\n",DRIVER_NAME));
  1627. return;
  1628. }
  1629. /*****************************************************************************
  1630. CMMOB_UpdateCurrentStateThread:
  1631. Arguments:
  1632. DeviceObject context of call
  1633. Return Value:
  1634. ******************************************************************************/
  1635. VOID CMMOB_UpdateCurrentStateThread(
  1636. IN PVOID Context
  1637. )
  1638. {
  1639. NTSTATUS NTStatus = STATUS_SUCCESS;
  1640. PDEVICE_OBJECT DeviceObject = Context;
  1641. PDEVICE_EXTENSION DeviceExtension;
  1642. PSMARTCARD_EXTENSION SmartcardExtension;
  1643. ULONG ulInterval;
  1644. DeviceExtension = DeviceObject->DeviceExtension;
  1645. SmartcardExtension = &DeviceExtension->SmartcardExtension;
  1646. KeWaitForSingleObject(&DeviceExtension->CanRunUpdateThread,
  1647. Executive,
  1648. KernelMode,
  1649. FALSE,
  1650. NULL);
  1651. SmartcardDebug(DEBUG_DRIVER,
  1652. ( "%s!UpdateCurrentStateThread: started\n",DRIVER_NAME));
  1653. while (TRUE) {
  1654. // every 500 ms the NTStatus request is sent
  1655. ulInterval = 500;
  1656. KeWaitForSingleObject(&SmartcardExtension->ReaderExtension->CardManIOMutex,
  1657. Executive,
  1658. KernelMode,
  1659. FALSE,
  1660. NULL);
  1661. if (SmartcardExtension->ReaderExtension->fTerminateUpdateThread) {
  1662. SmartcardDebug(DEBUG_DRIVER,
  1663. ("%s!-----------------------------------------------------------\n",DRIVER_NAME));
  1664. SmartcardDebug(DEBUG_DRIVER,
  1665. ("%s!UpdateCurrentStateThread: STOPPING THREAD\n",DRIVER_NAME));
  1666. SmartcardDebug(DEBUG_DRIVER,
  1667. ("%s!-----------------------------------------------------------\n",DRIVER_NAME));
  1668. KeReleaseMutex(&SmartcardExtension->ReaderExtension->CardManIOMutex,FALSE);
  1669. SmartcardExtension->ReaderExtension->fUpdateThreadRunning = FALSE;
  1670. PsTerminateSystemThread( STATUS_SUCCESS );
  1671. }
  1672. //
  1673. // get current card state
  1674. //
  1675. NTStatus = CMMOB_UpdateCurrentState(SmartcardExtension);
  1676. if (NTStatus == STATUS_DEVICE_DATA_ERROR) {
  1677. SmartcardDebug(DEBUG_DRIVER,
  1678. ("%s!UpdateCurrentStateThread: setting update interval to 1ms\n",DRIVER_NAME));
  1679. ulInterval = 1;
  1680. } else if (NTStatus != STATUS_SUCCESS &&
  1681. NTStatus != STATUS_NO_SUCH_DEVICE) {
  1682. SmartcardDebug(DEBUG_DRIVER,
  1683. ("%s!UpdateCurrentStateThread: UpdateCurrentState failed!\n",DRIVER_NAME));
  1684. }
  1685. KeReleaseMutex(&SmartcardExtension->ReaderExtension->CardManIOMutex,FALSE);
  1686. SysDelay (ulInterval);
  1687. }
  1688. }
  1689. /*****************************************************************************
  1690. CMMOB_UpdateCurrentState:
  1691. Arguments:
  1692. DeviceObject context of call
  1693. Return Value:
  1694. ******************************************************************************/
  1695. NTSTATUS CMMOB_UpdateCurrentState(
  1696. IN PSMARTCARD_EXTENSION SmartcardExtension
  1697. )
  1698. {
  1699. NTSTATUS NTStatus = STATUS_SUCCESS;
  1700. BOOL fCardStateChanged = FALSE;
  1701. KIRQL irql;
  1702. //
  1703. // get card state from cardman
  1704. //
  1705. NTStatus = CMMOB_ResetReader(SmartcardExtension->ReaderExtension);
  1706. if (NTStatus == STATUS_SUCCESS ||
  1707. NTStatus == STATUS_NO_SUCH_DEVICE) {
  1708. if (NTStatus == STATUS_SUCCESS) {
  1709. if (CMMOB_CardInserted(SmartcardExtension->ReaderExtension)) {
  1710. if (CMMOB_CardPowered(SmartcardExtension->ReaderExtension))
  1711. SmartcardExtension->ReaderExtension->ulNewCardState = POWERED;
  1712. else
  1713. SmartcardExtension->ReaderExtension->ulNewCardState = INSERTED;
  1714. } else
  1715. SmartcardExtension->ReaderExtension->ulNewCardState = REMOVED;
  1716. } else
  1717. SmartcardExtension->ReaderExtension->ulNewCardState = REMOVED;
  1718. KeAcquireSpinLock(&SmartcardExtension->OsData->SpinLock,
  1719. &irql);
  1720. if (SmartcardExtension->ReaderExtension->ulNewCardState == INSERTED &&
  1721. SmartcardExtension->ReaderExtension->ulOldCardState == POWERED ) {
  1722. // card has been removed and reinserted
  1723. SmartcardExtension->ReaderExtension->ulNewCardState = REMOVED;
  1724. }
  1725. if ((SmartcardExtension->ReaderExtension->ulNewCardState == INSERTED &&
  1726. (SmartcardExtension->ReaderExtension->ulOldCardState == UNKNOWN ||
  1727. SmartcardExtension->ReaderExtension->ulOldCardState == REMOVED )) ||
  1728. (SmartcardExtension->ReaderExtension->ulNewCardState == POWERED &&
  1729. SmartcardExtension->ReaderExtension->ulOldCardState == UNKNOWN )) {
  1730. // card has been inserted
  1731. SmartcardDebug(DEBUG_DRIVER,
  1732. ("%s!UpdateCurrentState: smartcard inserted\n",DRIVER_NAME));
  1733. SmartcardExtension->ReaderExtension->ulOldCardState = SmartcardExtension->ReaderExtension->ulNewCardState;
  1734. SmartcardExtension->ReaderCapabilities.CurrentState = SCARD_SWALLOWED;
  1735. SmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  1736. fCardStateChanged = TRUE;
  1737. }
  1738. if (SmartcardExtension->ReaderExtension->ulNewCardState == REMOVED &&
  1739. (SmartcardExtension->ReaderExtension->ulOldCardState == UNKNOWN ||
  1740. SmartcardExtension->ReaderExtension->ulOldCardState == INSERTED ||
  1741. SmartcardExtension->ReaderExtension->ulOldCardState == POWERED )) {
  1742. // card has been removed
  1743. SmartcardDebug(DEBUG_DRIVER,
  1744. ("%s!UpdateCurrentState: smartcard removed\n",DRIVER_NAME));
  1745. SmartcardExtension->ReaderExtension->ulOldCardState = SmartcardExtension->ReaderExtension->ulNewCardState;
  1746. SmartcardExtension->ReaderCapabilities.CurrentState = SCARD_ABSENT;
  1747. SmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
  1748. fCardStateChanged = TRUE;
  1749. // clear any cardspecific data
  1750. SmartcardExtension->CardCapabilities.ATR.Length = 0;
  1751. RtlFillMemory((PVOID)&SmartcardExtension->ReaderExtension->CardParameters,
  1752. sizeof(CARD_PARAMETERS), 0x00);
  1753. }
  1754. KeReleaseSpinLock(&SmartcardExtension->OsData->SpinLock,
  1755. irql);
  1756. // complete IOCTL_SMARTCARD_IS_ABSENT or IOCTL_SMARTCARD_IS_PRESENT
  1757. if (fCardStateChanged == TRUE &&
  1758. SmartcardExtension->OsData->NotificationIrp ) {
  1759. SmartcardDebug(DEBUG_DRIVER,("%s!UpdateCurrentState: completing IRP\n",DRIVER_NAME));
  1760. CMMOB_CompleteCardTracking(SmartcardExtension);
  1761. }
  1762. }
  1763. return NTStatus;
  1764. }
  1765. /*****************************************************************************
  1766. CMMOB_ResetReader:
  1767. Resets the reader
  1768. Arguments:
  1769. ReaderExtension context of the call
  1770. Return Value:
  1771. none
  1772. ******************************************************************************/
  1773. NTSTATUS CMMOB_ResetReader(
  1774. PREADER_EXTENSION ReaderExtension
  1775. )
  1776. {
  1777. NTSTATUS NTStatus;
  1778. BOOLEAN fToggle;
  1779. UCHAR bFlags1;
  1780. NTStatus = CMMOB_WriteRegister(ReaderExtension,ADDR_WRITEREG_FLAGS0,CMD_RESET_SM);
  1781. if (NTStatus != STATUS_SUCCESS)
  1782. return NTStatus;
  1783. // check for reader presence
  1784. bFlags1 = ReaderExtension->bPreviousFlags1;
  1785. bFlags1 |= FLAG_CHECK_PRESENCE;
  1786. NTStatus = CMMOB_WriteRegister(ReaderExtension, ADDR_WRITEREG_FLAGS1, bFlags1);
  1787. // don't check for status because
  1788. // we have to set back fCheckPresence for proper working
  1789. fToggle = CMMOB_GetReceiveFlag(ReaderExtension);
  1790. bFlags1 = ReaderExtension->bPreviousFlags1;
  1791. NTStatus = CMMOB_WriteRegister(ReaderExtension, ADDR_WRITEREG_FLAGS1, bFlags1);
  1792. if (NTStatus != STATUS_SUCCESS)
  1793. return NTStatus;
  1794. if (fToggle == CMMOB_GetReceiveFlag(ReaderExtension)) {
  1795. SmartcardDebug(DEBUG_DRIVER,
  1796. ("%s!ResetReader: CardMan Mobile removed!\n",DRIVER_NAME));
  1797. return STATUS_NO_SUCH_DEVICE;
  1798. }
  1799. return NTStatus;
  1800. }
  1801. /*****************************************************************************
  1802. CMMOB_BytesReceived:
  1803. Reads how many bytes are already received from the card by the reader
  1804. Arguments:
  1805. ReaderExtension context of the call
  1806. Return Value:
  1807. NTStatus
  1808. ******************************************************************************/
  1809. NTSTATUS CMMOB_BytesReceived(
  1810. PREADER_EXTENSION ReaderExtension,
  1811. PULONG pulBytesReceived
  1812. )
  1813. {
  1814. NTSTATUS NTStatus = STATUS_SUCCESS;
  1815. ULONG ulBytesReceived;
  1816. ULONG ulBytesReceivedCheck;
  1817. UCHAR bReg;
  1818. /*
  1819. SmartcardDebug(DEBUG_TRACE,
  1820. ( "%s!BytesReceived Enter\n",DRIVER_NAME));
  1821. */
  1822. *pulBytesReceived=0;
  1823. if (CMMOB_GetReceiveFlag(ReaderExtension) ||
  1824. ReaderExtension->CardParameters.fT0Mode) {
  1825. do {
  1826. NTStatus=CMMOB_ReadRegister(ReaderExtension,ADDR_READREG_BYTES_RECEIVED,&bReg);
  1827. if (NTStatus!=STATUS_SUCCESS)
  1828. return NTStatus;
  1829. ulBytesReceived=bReg;
  1830. NTStatus=CMMOB_ReadRegister(ReaderExtension,ADDR_READREG_FLAGS0,&bReg);
  1831. if (NTStatus!=STATUS_SUCCESS)
  1832. return NTStatus;
  1833. if ((bReg & FLAG_BYTES_RECEIVED_B9) == FLAG_BYTES_RECEIVED_B9) {
  1834. ulBytesReceived+=0x100;
  1835. }
  1836. NTStatus=CMMOB_ReadRegister(ReaderExtension,ADDR_READREG_BYTES_RECEIVED,&bReg);
  1837. if (NTStatus!=STATUS_SUCCESS)
  1838. return NTStatus;
  1839. ulBytesReceivedCheck=bReg;
  1840. NTStatus=CMMOB_ReadRegister(ReaderExtension,ADDR_READREG_FLAGS0,&bReg);
  1841. if (NTStatus!=STATUS_SUCCESS)
  1842. return NTStatus;
  1843. if ((bReg & FLAG_BYTES_RECEIVED_B9) == FLAG_BYTES_RECEIVED_B9) {
  1844. ulBytesReceivedCheck+=0x100;
  1845. }
  1846. }
  1847. while (ulBytesReceived!=ulBytesReceivedCheck);
  1848. *pulBytesReceived=ulBytesReceived;
  1849. }
  1850. /*
  1851. SmartcardDebug(DEBUG_TRACE,
  1852. ( "%s!BytesReceived Exit\n",DRIVER_NAME));
  1853. */
  1854. return NTStatus;
  1855. }
  1856. /*****************************************************************************
  1857. CMMOB_SetFlags1:
  1858. Sets register Flags1
  1859. Arguments:
  1860. ReaderExtension context of the call
  1861. Return Value:
  1862. none
  1863. ******************************************************************************/
  1864. NTSTATUS CMMOB_SetFlags1 (
  1865. PREADER_EXTENSION ReaderExtension
  1866. )
  1867. {
  1868. NTSTATUS NTStatus = STATUS_SUCCESS;
  1869. UCHAR bFlags1;
  1870. bFlags1 = ReaderExtension->CardParameters.bBaudRateHigh;
  1871. if (ReaderExtension->CardParameters.fInversRevers)
  1872. bFlags1 |= FLAG_INVERS_PARITY;
  1873. if (ReaderExtension->CardParameters.bClockFrequency==8)
  1874. bFlags1 |= FLAG_CLOCK_8MHZ;
  1875. if (ReaderExtension->CardParameters.fT0Write)
  1876. bFlags1 |= FLAG_T0_WRITE;
  1877. if (ReaderExtension->bAddressHigh == 1)
  1878. bFlags1 |= FLAG_BUFFER_ADDR_B9;
  1879. if (ReaderExtension->fTActive)
  1880. bFlags1 |= FLAG_TACTIVE;
  1881. if (ReaderExtension->fReadCIS)
  1882. bFlags1 |= FLAG_READ_CIS;
  1883. ReaderExtension->bPreviousFlags1=bFlags1;
  1884. NTStatus = CMMOB_WriteRegister(ReaderExtension, ADDR_WRITEREG_FLAGS1, bFlags1);
  1885. return NTStatus;
  1886. }
  1887. /*****************************************************************************
  1888. CMMOB_SetCardParameters:
  1889. Sets card parameters (baudrate, stopbits)
  1890. Arguments:
  1891. ReaderExtension context of the call
  1892. Return Value:
  1893. none
  1894. ******************************************************************************/
  1895. NTSTATUS CMMOB_SetCardParameters (
  1896. PREADER_EXTENSION ReaderExtension
  1897. )
  1898. {
  1899. NTSTATUS NTStatus = STATUS_SUCCESS;
  1900. NTStatus = CMMOB_SetFlags1 (ReaderExtension);
  1901. if (NTStatus!=STATUS_SUCCESS)
  1902. return NTStatus;
  1903. NTStatus = CMMOB_WriteRegister(ReaderExtension, ADDR_WRITEREG_BAUDRATE,
  1904. ReaderExtension->CardParameters.bBaudRateLow);
  1905. if (NTStatus!=STATUS_SUCCESS)
  1906. return NTStatus;
  1907. NTStatus = CMMOB_WriteRegister(ReaderExtension, ADDR_WRITEREG_STOPBITS,
  1908. ReaderExtension->CardParameters.bStopBits);
  1909. return NTStatus;
  1910. }
  1911. /*****************************************************************************
  1912. CMMOB_CardInserted:
  1913. Sets card parameters (baudrate, stopbits)
  1914. Arguments:
  1915. ReaderExtension context of the call
  1916. Return Value:
  1917. TRUE if card is inserted
  1918. ******************************************************************************/
  1919. BOOLEAN CMMOB_CardInserted(
  1920. IN PREADER_EXTENSION ReaderExtension
  1921. )
  1922. {
  1923. NTSTATUS NTStatus=STATUS_SUCCESS;
  1924. UCHAR bReg;
  1925. NTStatus=CMMOB_ReadRegister(ReaderExtension,ADDR_READREG_FLAGS0,&bReg);
  1926. if (NTStatus!=STATUS_SUCCESS)
  1927. return FALSE;
  1928. /*
  1929. SmartcardDebug(DEBUG_TRACE,
  1930. ("%s!CardInserted: ReadReg Flags0 = %x\n",DRIVER_NAME, (ULONG)bReg));
  1931. */
  1932. if ((bReg & FLAG_INSERTED)==FLAG_INSERTED) {
  1933. return TRUE;
  1934. }
  1935. return FALSE;
  1936. }
  1937. /*****************************************************************************
  1938. CMMOB_CardPowered:
  1939. Sets card parameters (baudrate, stopbits)
  1940. Arguments:
  1941. ReaderExtension context of the call
  1942. Return Value:
  1943. TRUE if card is powered
  1944. ******************************************************************************/
  1945. BOOLEAN CMMOB_CardPowered(
  1946. IN PREADER_EXTENSION ReaderExtension
  1947. )
  1948. {
  1949. NTSTATUS NTStatus=STATUS_SUCCESS;
  1950. UCHAR bReg;
  1951. NTStatus=CMMOB_ReadRegister(ReaderExtension,ADDR_READREG_FLAGS0,&bReg);
  1952. if (NTStatus!=STATUS_SUCCESS)
  1953. return FALSE;
  1954. if ((bReg & FLAG_POWERED)==FLAG_POWERED) {
  1955. return TRUE;
  1956. }
  1957. return FALSE;
  1958. }
  1959. /*****************************************************************************
  1960. CMMOB_ProcedureReceived:
  1961. Arguments:
  1962. ReaderExtension context of the call
  1963. Return Value:
  1964. TRUE if a procedure byte has been received
  1965. ******************************************************************************/
  1966. BOOLEAN CMMOB_ProcedureReceived(
  1967. IN PREADER_EXTENSION ReaderExtension
  1968. )
  1969. {
  1970. NTSTATUS NTStatus=STATUS_SUCCESS;
  1971. UCHAR bReg;
  1972. NTStatus=CMMOB_ReadRegister(ReaderExtension,ADDR_READREG_FLAGS1,&bReg);
  1973. if (NTStatus!=STATUS_SUCCESS)
  1974. return FALSE;
  1975. if ((bReg & FLAG_NOPROCEDURE_RECEIVED)!=FLAG_NOPROCEDURE_RECEIVED) {
  1976. return TRUE;
  1977. }
  1978. return FALSE;
  1979. }
  1980. /*****************************************************************************
  1981. CMMOB_GetReceiveFlag:
  1982. Arguments:
  1983. ReaderExtension context of the call
  1984. Return Value:
  1985. TRUE if a receive flag is set
  1986. ******************************************************************************/
  1987. BOOLEAN CMMOB_GetReceiveFlag(
  1988. IN PREADER_EXTENSION ReaderExtension
  1989. )
  1990. {
  1991. NTSTATUS NTStatus=STATUS_SUCCESS;
  1992. UCHAR bReg;
  1993. NTStatus=CMMOB_ReadRegister(ReaderExtension,ADDR_READREG_FLAGS0,&bReg);
  1994. /*
  1995. SmartcardDebug(DEBUG_TRACE,
  1996. ("%s!GetReceiveFlag: ReadReg Flags0 = %x\n",DRIVER_NAME, (ULONG)bReg));
  1997. */
  1998. if (NTStatus!=STATUS_SUCCESS)
  1999. return FALSE;
  2000. if ((bReg & FLAG_RECEIVE)==FLAG_RECEIVE) {
  2001. return TRUE;
  2002. }
  2003. return FALSE;
  2004. }
  2005. /*****************************************************************************
  2006. CMMOB_GetProcedureByte:
  2007. Reads how many bytes are already received from the card by the reader
  2008. Arguments:
  2009. ReaderExtension context of the call
  2010. Return Value:
  2011. NTStatus
  2012. ******************************************************************************/
  2013. NTSTATUS CMMOB_GetProcedureByte(
  2014. IN PREADER_EXTENSION ReaderExtension,
  2015. OUT PUCHAR pbProcedureByte
  2016. )
  2017. {
  2018. NTSTATUS NTStatus = STATUS_SUCCESS;
  2019. UCHAR bReg;
  2020. UCHAR bRegPrevious;
  2021. do {
  2022. NTStatus=CMMOB_ReadRegister(ReaderExtension,ADDR_READREG_LASTPROCEDURE_T0,&bRegPrevious);
  2023. if (NTStatus!=STATUS_SUCCESS)
  2024. return NTStatus;
  2025. NTStatus=CMMOB_ReadRegister(ReaderExtension,ADDR_READREG_LASTPROCEDURE_T0,&bReg);
  2026. if (NTStatus!=STATUS_SUCCESS)
  2027. return NTStatus;
  2028. }
  2029. while (bReg!=bRegPrevious);
  2030. *pbProcedureByte=bReg;
  2031. return NTStatus;
  2032. }
  2033. /*****************************************************************************
  2034. CMMOB_WriteT0:
  2035. Writes T0 request to card
  2036. Arguments:
  2037. ReaderExtension context of the call
  2038. Return Value:
  2039. NT STATUS
  2040. ******************************************************************************/
  2041. NTSTATUS CMMOB_WriteT0(
  2042. IN PREADER_EXTENSION ReaderExtension,
  2043. IN ULONG ulBytesToWrite,
  2044. IN ULONG ulBytesToReceive,
  2045. IN PUCHAR pbData
  2046. )
  2047. {
  2048. NTSTATUS NTStatus = STATUS_SUCCESS;
  2049. UCHAR bFlags0;
  2050. UCHAR bReg;
  2051. if (ulBytesToWrite > CMMOB_MAXBUFFER) {
  2052. NTStatus = STATUS_BUFFER_OVERFLOW;
  2053. return NTStatus;
  2054. }
  2055. // dummy read, to reset flag procedure received
  2056. NTStatus=CMMOB_ReadRegister(ReaderExtension,ADDR_READREG_FLAGS1,&bReg);
  2057. NTStatus = CMMOB_WriteBuffer(ReaderExtension,ulBytesToWrite,pbData);
  2058. if (NTStatus != STATUS_SUCCESS)
  2059. return NTStatus;
  2060. // write instruction byte to register
  2061. NTStatus = CMMOB_WriteRegister(ReaderExtension,ADDR_WRITEREG_PROCEDURE_T0,pbData[1]);
  2062. if (NTStatus != STATUS_SUCCESS)
  2063. return NTStatus;
  2064. // write message length
  2065. NTStatus = CMMOB_WriteRegister(ReaderExtension,ADDR_WRITEREG_MESSAGE_LENGTH,
  2066. (UCHAR)((ulBytesToWrite+ulBytesToReceive) & 0xFF));
  2067. if (NTStatus != STATUS_SUCCESS)
  2068. return NTStatus;
  2069. if ((ulBytesToWrite+ulBytesToReceive) > 0xFF) {
  2070. bFlags0=1;
  2071. } else {
  2072. bFlags0=0;
  2073. }
  2074. bFlags0 |= CMD_WRITE_T0;
  2075. NTStatus = CMMOB_WriteRegister(ReaderExtension,ADDR_WRITEREG_FLAGS0,bFlags0);
  2076. return NTStatus;
  2077. }
  2078. /*****************************************************************************
  2079. CMMOB_WriteT1:
  2080. Writes T1 request to card
  2081. Arguments:
  2082. ReaderExtension context of the call
  2083. Return Value:
  2084. NT STATUS
  2085. ******************************************************************************/
  2086. NTSTATUS CMMOB_WriteT1(
  2087. IN PREADER_EXTENSION ReaderExtension,
  2088. IN ULONG ulBytesToWrite,
  2089. IN PUCHAR pbData
  2090. )
  2091. {
  2092. NTSTATUS NTStatus = STATUS_SUCCESS;
  2093. UCHAR bFlags0;
  2094. if (ulBytesToWrite > CMMOB_MAXBUFFER) {
  2095. NTStatus = STATUS_BUFFER_OVERFLOW;
  2096. return NTStatus;
  2097. }
  2098. NTStatus = CMMOB_WriteBuffer(ReaderExtension,ulBytesToWrite,pbData);
  2099. if (NTStatus != STATUS_SUCCESS)
  2100. return NTStatus;
  2101. NTStatus = CMMOB_WriteRegister(ReaderExtension,ADDR_WRITEREG_MESSAGE_LENGTH,
  2102. (UCHAR)(ulBytesToWrite & 0xFF));
  2103. if (NTStatus != STATUS_SUCCESS)
  2104. return NTStatus;
  2105. if (ulBytesToWrite > 0xFF) {
  2106. bFlags0=1;
  2107. } else {
  2108. bFlags0=0;
  2109. }
  2110. bFlags0 |= CMD_WRITE_T1;
  2111. NTStatus = CMMOB_WriteRegister(ReaderExtension,ADDR_WRITEREG_FLAGS0,bFlags0);
  2112. return NTStatus;
  2113. }
  2114. /*****************************************************************************
  2115. CMMOB_ReadT0:
  2116. Reads T0 reply from card
  2117. Arguments:
  2118. ReaderExtension context of the call
  2119. Return Value:
  2120. NT STATUS
  2121. ******************************************************************************/
  2122. NTSTATUS CMMOB_ReadT0(
  2123. IN PREADER_EXTENSION ReaderExtension,
  2124. IN ULONG ulBytesToRead,
  2125. IN ULONG ulBytesSent,
  2126. IN ULONG ulCWT,
  2127. OUT PUCHAR pbData,
  2128. OUT PULONG pulBytesRead,
  2129. OUT PBOOLEAN pfDataSent
  2130. )
  2131. {
  2132. NTSTATUS NTStatus = STATUS_SUCCESS;
  2133. KTIMER TimerWait;
  2134. ULONG ulBytesReceived;
  2135. ULONG ulBytesReceivedPrevious;
  2136. LARGE_INTEGER liWaitTime;
  2137. BOOLEAN fTimeExpired;
  2138. BOOLEAN fProcedureReceived;
  2139. BOOLEAN fTransmissionFinished;
  2140. UCHAR bProcedureByte=0;
  2141. SmartcardDebug(DEBUG_TRACE,
  2142. ("%s!ReadT0: Enter BytesToRead = %li\n",DRIVER_NAME,ulBytesToRead));
  2143. //initialize Timer
  2144. KeInitializeTimer(&TimerWait);
  2145. liWaitTime = RtlConvertLongToLargeInteger(ulCWT * -10000L);
  2146. *pulBytesRead = 0;
  2147. *pfDataSent = FALSE;
  2148. do {
  2149. KeSetTimer(&TimerWait,liWaitTime,NULL);
  2150. NTStatus=CMMOB_BytesReceived (ReaderExtension,&ulBytesReceivedPrevious);
  2151. if (NTStatus!=STATUS_SUCCESS)
  2152. goto ExitReadT0;
  2153. do {
  2154. fTimeExpired = KeReadStateTimer(&TimerWait);
  2155. fTransmissionFinished=CMMOB_GetReceiveFlag(ReaderExtension);
  2156. fProcedureReceived=CMMOB_ProcedureReceived(ReaderExtension);
  2157. NTStatus=CMMOB_BytesReceived (ReaderExtension,&ulBytesReceived);
  2158. if (NTStatus!=STATUS_SUCCESS)
  2159. goto ExitReadT0;
  2160. // wait 1 ms, so that processor is not blocked
  2161. SysDelay(1);
  2162. }
  2163. while (fTimeExpired==FALSE &&
  2164. fProcedureReceived==FALSE &&
  2165. ulBytesReceivedPrevious == ulBytesReceived &&
  2166. fTransmissionFinished==FALSE);
  2167. if (fProcedureReceived) {
  2168. NTStatus=CMMOB_GetProcedureByte (ReaderExtension,&bProcedureByte);
  2169. if (NTStatus!=STATUS_SUCCESS)
  2170. goto ExitReadT0;
  2171. // check for SW1
  2172. if (ReaderExtension->CardParameters.fInversRevers) {
  2173. CMMOB_InverseBuffer(&bProcedureByte,1);
  2174. }
  2175. }
  2176. if (!fTimeExpired) {
  2177. KeCancelTimer(&TimerWait);
  2178. }
  2179. #ifdef DBG
  2180. else {
  2181. SmartcardDebug(DEBUG_PROTOCOL,( "%s!----------------------------------------------\n",DRIVER_NAME));
  2182. SmartcardDebug(DEBUG_PROTOCOL,( "%s!Read T0 timed out\n",DRIVER_NAME));
  2183. SmartcardDebug(DEBUG_PROTOCOL,( "%s!----------------------------------------------\n",DRIVER_NAME));
  2184. }
  2185. #endif
  2186. }
  2187. while (fTimeExpired==FALSE &&
  2188. fTransmissionFinished==FALSE);
  2189. // read once more ulBytesReceived
  2190. // this value could have changed in the meantime
  2191. NTStatus=CMMOB_BytesReceived (ReaderExtension,&ulBytesReceived);
  2192. if (NTStatus!=STATUS_SUCCESS)
  2193. goto ExitReadT0;
  2194. SmartcardDebug(DEBUG_PROTOCOL,
  2195. ("%s!ReadT0: BytesReceived = %li\n",DRIVER_NAME,ulBytesReceived));
  2196. //now we should have received a reply
  2197. NTStatus=CMMOB_ResetReader (ReaderExtension);
  2198. if (NTStatus!=STATUS_SUCCESS)
  2199. goto ExitReadT0;
  2200. // check for valid SW1
  2201. if ((bProcedureByte > 0x60 && bProcedureByte <= 0x6F) ||
  2202. (bProcedureByte >= 0x90 && bProcedureByte <= 0x9F)) {
  2203. if (ReaderExtension->CardParameters.fInversRevers) {
  2204. CMMOB_InverseBuffer(&bProcedureByte,1);
  2205. }
  2206. if (ulBytesReceived > ulBytesSent) {
  2207. if (ulBytesReceived - ulBytesSent > MIN_BUFFER_SIZE) {
  2208. NTStatus = STATUS_BUFFER_OVERFLOW;
  2209. goto ExitReadT0;
  2210. }
  2211. NTStatus=CMMOB_ReadBuffer(ReaderExtension, ulBytesSent,
  2212. ulBytesReceived-ulBytesSent, pbData);
  2213. if (NTStatus==STATUS_SUCCESS) {
  2214. // we have to insert the procedure byte (SW1)
  2215. pbData[ulBytesReceived-ulBytesSent]=pbData[ulBytesReceived-ulBytesSent-1];
  2216. pbData[ulBytesReceived-ulBytesSent-1]=bProcedureByte;
  2217. *pulBytesRead=ulBytesReceived-ulBytesSent+1;
  2218. }
  2219. if (ulBytesSent > T0_HEADER_LEN) {
  2220. *pfDataSent = TRUE;
  2221. }
  2222. } else {
  2223. if (ulBytesReceived > T0_HEADER_LEN) {
  2224. // it seems not all bytes were accepted by the card
  2225. // but we got SW1 SW2 - return only SW1 SW2
  2226. pbData[0]=bProcedureByte;
  2227. NTStatus=CMMOB_ReadBuffer(ReaderExtension, ulBytesReceived-1,
  2228. 1, &pbData[1]);
  2229. *pulBytesRead=2;
  2230. } else {
  2231. NTStatus = STATUS_IO_TIMEOUT;
  2232. }
  2233. }
  2234. } else {
  2235. NTStatus = STATUS_IO_TIMEOUT;
  2236. }
  2237. ExitReadT0:
  2238. SmartcardDebug(DEBUG_TRACE,
  2239. ("%s!ReadT0: Exit\n",DRIVER_NAME ));
  2240. return NTStatus;
  2241. }
  2242. /*****************************************************************************
  2243. CMMOB_ReadT1:
  2244. Reads T1 reply from card
  2245. Arguments:
  2246. ReaderExtension context of the call
  2247. Return Value:
  2248. NT STATUS
  2249. ******************************************************************************/
  2250. NTSTATUS CMMOB_ReadT1(
  2251. IN PREADER_EXTENSION ReaderExtension,
  2252. IN LONG lBytesToRead,
  2253. IN ULONG ulBWT,
  2254. IN ULONG ulCWT,
  2255. OUT PUCHAR pbData,
  2256. OUT PULONG pulBytesRead
  2257. )
  2258. // a negative value of ulBytesToRead indicates a relative number of bytes to read
  2259. {
  2260. NTSTATUS NTStatus = STATUS_SUCCESS;
  2261. KTIMER TimerWait;
  2262. ULONG ulBytesReceived;
  2263. LARGE_INTEGER liWaitTime;
  2264. BOOLEAN fTimeExpired;
  2265. ULONG ulBytesReceivedPrevious;
  2266. /*
  2267. SmartcardDebug(DEBUG_TRACE,
  2268. ("%s!ReadT1: Enter\n",DRIVER_NAME ));
  2269. */
  2270. //initialize Timer
  2271. KeInitializeTimer(&TimerWait);
  2272. *pulBytesRead = 0;
  2273. // first wait BWT (block waiting time)
  2274. liWaitTime = RtlConvertLongToLargeInteger(ulBWT * -10000L);
  2275. do {
  2276. KeSetTimer(&TimerWait,liWaitTime,NULL);
  2277. NTStatus=CMMOB_BytesReceived (ReaderExtension,&ulBytesReceivedPrevious);
  2278. if (NTStatus!=STATUS_SUCCESS)
  2279. goto ExitReadT1;
  2280. do {
  2281. fTimeExpired = KeReadStateTimer(&TimerWait);
  2282. NTStatus=CMMOB_BytesReceived (ReaderExtension,&ulBytesReceived);
  2283. if (NTStatus!=STATUS_SUCCESS)
  2284. goto ExitReadT1;
  2285. // wait 1 ms, so that processor is not blocked
  2286. SysDelay(1);
  2287. // make an adjustment of lBytesToRead (only one time)
  2288. if (lBytesToRead<= 0 && ulBytesReceived >= 3) {
  2289. // get number of bytes to receive from reader
  2290. UCHAR bReg;
  2291. UCHAR bRegPrevious;
  2292. lBytesToRead = -lBytesToRead;
  2293. do {
  2294. NTStatus=CMMOB_ReadRegister(ReaderExtension,ADDR_READREG_BYTESTORECEIVE_T1,&bRegPrevious);
  2295. if (NTStatus!=STATUS_SUCCESS)
  2296. goto ExitReadT1;
  2297. NTStatus=CMMOB_ReadRegister(ReaderExtension,ADDR_READREG_BYTESTORECEIVE_T1,&bReg);
  2298. if (NTStatus!=STATUS_SUCCESS)
  2299. goto ExitReadT1;
  2300. }
  2301. while (bReg!=bRegPrevious);
  2302. lBytesToRead += bReg;
  2303. }
  2304. }
  2305. while (fTimeExpired==FALSE &&
  2306. ulBytesReceivedPrevious == ulBytesReceived &&
  2307. (lBytesToRead<=0 || ulBytesReceived!=(ULONG)lBytesToRead));
  2308. if (!fTimeExpired) {
  2309. KeCancelTimer(&TimerWait);
  2310. liWaitTime = RtlConvertLongToLargeInteger(ulCWT * -10000L);
  2311. // now wait only CWT (character waiting time)
  2312. }
  2313. #ifdef DBG
  2314. else {
  2315. SmartcardDebug(DEBUG_PROTOCOL,( "%s!----------------------------------------------\n",DRIVER_NAME));
  2316. SmartcardDebug(DEBUG_PROTOCOL,( "%s!Read T1 timed out\n",DRIVER_NAME));
  2317. SmartcardDebug(DEBUG_PROTOCOL,( "%s!----------------------------------------------\n",DRIVER_NAME));
  2318. }
  2319. #endif
  2320. }
  2321. while (!fTimeExpired &&
  2322. (lBytesToRead<=0 || ulBytesReceived!=(ULONG)lBytesToRead));
  2323. //now we should have received a reply
  2324. NTStatus=CMMOB_ResetReader (ReaderExtension);
  2325. if (NTStatus!=STATUS_SUCCESS)
  2326. goto ExitReadT1;
  2327. if (ulBytesReceived > CMMOB_MAXBUFFER) {
  2328. NTStatus = STATUS_BUFFER_OVERFLOW;
  2329. }
  2330. if (ulBytesReceived==(ULONG)lBytesToRead && lBytesToRead > 0) {
  2331. NTStatus=CMMOB_ReadBuffer(ReaderExtension, 0, (ULONG)lBytesToRead, pbData);
  2332. if (NTStatus==STATUS_SUCCESS) {
  2333. *pulBytesRead=(ULONG)lBytesToRead;
  2334. }
  2335. } else {
  2336. NTStatus=CMMOB_ReadBuffer(ReaderExtension, 0, ulBytesReceived, pbData);
  2337. if (NTStatus==STATUS_SUCCESS) {
  2338. *pulBytesRead=ulBytesReceived;
  2339. }
  2340. NTStatus = STATUS_IO_TIMEOUT;
  2341. }
  2342. ExitReadT1:
  2343. /*
  2344. SmartcardDebug(DEBUG_TRACE,
  2345. ("%s!ReadT1: Exit\n",DRIVER_NAME ));
  2346. */
  2347. return NTStatus;
  2348. }
  2349. /*****************************************************************************
  2350. CMMOB_ReadRegister:
  2351. Sets card parameters (baudrate, stopbits)
  2352. Arguments:
  2353. ReaderExtension context of the call
  2354. Return Value:
  2355. NT STATUS
  2356. ******************************************************************************/
  2357. NTSTATUS CMMOB_ReadRegister(
  2358. IN PREADER_EXTENSION ReaderExtension,
  2359. IN USHORT usAddress,
  2360. OUT PUCHAR pbData
  2361. )
  2362. {
  2363. *pbData = READ_PORT_UCHAR(ReaderExtension->pbRegsBase+usAddress);
  2364. return STATUS_SUCCESS;
  2365. }
  2366. /*****************************************************************************
  2367. CMMOB_WriteRegister:
  2368. Sets card parameters (baudrate, stopbits)
  2369. Arguments:
  2370. ReaderExtension context of the call
  2371. Return Value:
  2372. NT STATUS
  2373. ******************************************************************************/
  2374. NTSTATUS CMMOB_WriteRegister(
  2375. IN PREADER_EXTENSION ReaderExtension,
  2376. IN USHORT usAddress,
  2377. IN UCHAR bData
  2378. )
  2379. {
  2380. WRITE_PORT_UCHAR(ReaderExtension->pbRegsBase+usAddress,bData);
  2381. return STATUS_SUCCESS;
  2382. }
  2383. /*****************************************************************************
  2384. CMMOB_ReadBuffer:
  2385. Sets card parameters (baudrate, stopbits)
  2386. Arguments:
  2387. ReaderExtension context of the call
  2388. Return Value:
  2389. NT STATUS
  2390. ******************************************************************************/
  2391. NTSTATUS CMMOB_ReadBuffer(
  2392. IN PREADER_EXTENSION ReaderExtension,
  2393. IN ULONG ulOffset,
  2394. IN ULONG ulLength,
  2395. OUT PUCHAR pbData
  2396. )
  2397. {
  2398. NTSTATUS NTStatus = STATUS_SUCCESS;
  2399. ULONG i;
  2400. if ((ulOffset & 0x100) == 0x100) {
  2401. ReaderExtension->bAddressHigh=1;
  2402. } else {
  2403. ReaderExtension->bAddressHigh=0;
  2404. }
  2405. NTStatus = CMMOB_SetFlags1(ReaderExtension);
  2406. if (NTStatus!=STATUS_SUCCESS) {
  2407. goto ExitReadBuffer;
  2408. }
  2409. for (i=0; i<ulLength; i++) {
  2410. WRITE_PORT_UCHAR(ReaderExtension->pbRegsBase+ADDR_WRITEREG_BUFFER_ADDR,
  2411. (BYTE)((ulOffset+i)&0xFF));
  2412. // because we are counting up in a loop we have to set
  2413. // bit 9 of address only once
  2414. if (ulOffset+i == 0x100) {
  2415. ReaderExtension->bAddressHigh=1;
  2416. NTStatus = CMMOB_SetFlags1(ReaderExtension);
  2417. if (NTStatus!=STATUS_SUCCESS) {
  2418. goto ExitReadBuffer;
  2419. }
  2420. }
  2421. *(pbData+i)=READ_PORT_UCHAR(ReaderExtension->pbRegsBase+ADDR_WRITEREG_BUFFER_DATA);
  2422. // erase buffer - required for certification
  2423. WRITE_PORT_UCHAR(ReaderExtension->pbRegsBase+ADDR_WRITEREG_BUFFER_DATA,0);
  2424. }
  2425. ExitReadBuffer:
  2426. return NTStatus;
  2427. }
  2428. /*****************************************************************************
  2429. CMMOB_WriteBuffer:
  2430. Sets card parameters (baudrate, stopbits)
  2431. Arguments:
  2432. ReaderExtension context of the call
  2433. Return Value:
  2434. NT STATUS
  2435. ******************************************************************************/
  2436. NTSTATUS CMMOB_WriteBuffer(
  2437. IN PREADER_EXTENSION ReaderExtension,
  2438. IN ULONG ulLength,
  2439. IN PUCHAR pbData
  2440. )
  2441. {
  2442. NTSTATUS NTStatus = STATUS_SUCCESS;
  2443. ULONG i;
  2444. ReaderExtension->bAddressHigh=0;
  2445. NTStatus = CMMOB_SetFlags1(ReaderExtension);
  2446. if (NTStatus!=STATUS_SUCCESS) {
  2447. goto ExitWriteBuffer;
  2448. }
  2449. for (i=0; i<ulLength; i++) {
  2450. WRITE_PORT_UCHAR(ReaderExtension->pbRegsBase+ADDR_WRITEREG_BUFFER_ADDR,
  2451. (BYTE)(i & 0xFF));
  2452. // because we are counting up in a loop we have to set
  2453. // bit 9 of address only once
  2454. if (i == 0x100) {
  2455. ReaderExtension->bAddressHigh=1;
  2456. NTStatus = CMMOB_SetFlags1(ReaderExtension);
  2457. if (NTStatus!=STATUS_SUCCESS) {
  2458. goto ExitWriteBuffer;
  2459. }
  2460. }
  2461. WRITE_PORT_UCHAR(ReaderExtension->pbRegsBase+ADDR_WRITEREG_BUFFER_DATA,*(pbData+i));
  2462. }
  2463. ExitWriteBuffer:
  2464. return NTStatus;
  2465. }
  2466. /*****************************************************************************
  2467. Routine Description:
  2468. This routine inverts the buffer
  2469. Bit0 -> Bit 7
  2470. Bit1 -> Bit 6
  2471. Bit2 -> Bit 5
  2472. Bit3 -> Bit 4
  2473. Bit4 -> Bit 3
  2474. Bit5 -> Bit 2
  2475. Bit6 -> Bit 1
  2476. Bit7 -> Bit 0
  2477. Arguments: pbBuffer ... pointer to buffer
  2478. ulBufferSize ... size of buffer
  2479. Return Value: none
  2480. *****************************************************************************/
  2481. VOID CMMOB_InverseBuffer (
  2482. PUCHAR pbBuffer,
  2483. ULONG ulBufferSize
  2484. )
  2485. {
  2486. ULONG i,j;
  2487. UCHAR bRevers;
  2488. UCHAR bTemp;
  2489. for (i=0; i<ulBufferSize; i++) {
  2490. bRevers = 0;
  2491. for (j=0; j<8; j++) {
  2492. bTemp = pbBuffer[i] << j;
  2493. bTemp &= 0x80;
  2494. bRevers |= bTemp >> (7-j);
  2495. }
  2496. pbBuffer[i] = ~bRevers;
  2497. }
  2498. return;
  2499. }
  2500. /*****************************************************************************
  2501. * History:
  2502. * $Log: cmbp0scr.c $
  2503. * Revision 1.7 2001/01/22 07:12:36 WFrischauf
  2504. * No comment given
  2505. *
  2506. * Revision 1.6 2000/09/25 14:24:31 WFrischauf
  2507. * No comment given
  2508. *
  2509. * Revision 1.5 2000/08/24 09:05:13 TBruendl
  2510. * No comment given
  2511. *
  2512. * Revision 1.4 2000/08/09 12:45:57 WFrischauf
  2513. * No comment given
  2514. *
  2515. * Revision 1.3 2000/07/27 13:53:03 WFrischauf
  2516. * No comment given
  2517. *
  2518. *
  2519. ******************************************************************************/