Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

852 lines
25 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1996 - 1999
  3. Module Name:
  4. smcioctl.c
  5. Abstract:
  6. This module handles all IOCTL requests to the smart card reader.
  7. Environment:
  8. Kernel mode only.
  9. Notes:
  10. This module is shared by Windows NT and Windows 9x
  11. Revision History:
  12. - Created June 1997 by Klaus Schutz
  13. --*/
  14. #define _ISO_TABLES_
  15. #ifndef SMCLIB_VXD
  16. #ifndef SMCLIB_CE
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <ntddk.h>
  21. #endif
  22. #endif
  23. #include "smclib.h"
  24. #define IOCTL_SMARTCARD_DEBUG SCARD_CTL_CODE(98)
  25. #define CheckUserBuffer(_len_) \
  26. if (SmartcardExtension->IoRequest.ReplyBuffer == NULL || \
  27. SmartcardExtension->IoRequest.ReplyBufferLength < (_len_)) { \
  28. status = STATUS_BUFFER_TOO_SMALL; \
  29. break; \
  30. }
  31. #define CheckMinCardStatus(_status_) \
  32. if (SmartcardExtension->ReaderCapabilities.CurrentState < (_status_)) { \
  33. status = STATUS_INVALID_DEVICE_STATE; \
  34. break; \
  35. }
  36. #define ReturnULong(_value_) \
  37. { \
  38. CheckUserBuffer(sizeof(ULONG)) \
  39. *(PULONG) SmartcardExtension->IoRequest.ReplyBuffer = (_value_); \
  40. *SmartcardExtension->IoRequest.Information = sizeof(ULONG); \
  41. }
  42. #define ReturnUChar(_value_) \
  43. { \
  44. CheckUserBuffer(sizeof(UCHAR)) \
  45. *(PUCHAR) SmartcardExtension->IoRequest.ReplyBuffer = (_value_); \
  46. *SmartcardExtension->IoRequest.Information = sizeof(UCHAR); \
  47. }
  48. #define DIM(_array_) (sizeof(_array_) / sizeof(_array_[0]))
  49. PTCHAR
  50. MapIoControlCodeToString(
  51. ULONG IoControlCode
  52. )
  53. {
  54. ULONG i;
  55. static struct {
  56. ULONG IoControlCode;
  57. PTCHAR String;
  58. } IoControlList[] = {
  59. IOCTL_SMARTCARD_POWER, TEXT("POWER"),
  60. IOCTL_SMARTCARD_GET_ATTRIBUTE, TEXT("GET_ATTRIBUTE"),
  61. IOCTL_SMARTCARD_SET_ATTRIBUTE, TEXT("SET_ATTRIBUTE"),
  62. IOCTL_SMARTCARD_CONFISCATE, TEXT("CONFISCATE"),
  63. IOCTL_SMARTCARD_TRANSMIT, TEXT("TRANSMIT"),
  64. IOCTL_SMARTCARD_EJECT, TEXT("EJECT"),
  65. IOCTL_SMARTCARD_SWALLOW, TEXT("SWALLOW"),
  66. IOCTL_SMARTCARD_IS_PRESENT, TEXT("IS_PRESENT"),
  67. IOCTL_SMARTCARD_IS_ABSENT, TEXT("IS_ABSENT"),
  68. IOCTL_SMARTCARD_SET_PROTOCOL, TEXT("SET_PROTOCOL"),
  69. IOCTL_SMARTCARD_GET_STATE, TEXT("GET_STATE"),
  70. IOCTL_SMARTCARD_GET_LAST_ERROR, TEXT("GET_LAST_ERROR")
  71. };
  72. for (i = 0; i < DIM(IoControlList); i++) {
  73. if (IoControlCode == IoControlList[i].IoControlCode) {
  74. return IoControlList[i].String;
  75. }
  76. }
  77. return TEXT("*** UNKNOWN ***");
  78. }
  79. NTSTATUS
  80. SmartcardDeviceIoControl(
  81. PSMARTCARD_EXTENSION SmartcardExtension
  82. )
  83. /*++
  84. Routine Description:
  85. This routine handles the smart card lib specific io control requests.
  86. The driver has to call the function from the driver's io control request.
  87. It checks the parameters of the call and depending on the type of
  88. the call returns the requested value or calls the driver in order
  89. to perform an operation like POWER or TRANSMIT.
  90. NOTE: This function is used by Windows NT and VxD driver
  91. Arguments:
  92. SmartcardExtension - The pointer to the smart card data struct
  93. Return Value:
  94. NTSTATUS value
  95. --*/
  96. {
  97. NTSTATUS status = STATUS_SUCCESS;
  98. #ifdef SMCLIB_NT
  99. KIRQL Irql;
  100. #endif
  101. switch (SmartcardExtension->MajorIoControlCode) {
  102. #if DEBUG
  103. ULONG CurrentDebugLevel, bytesTransferred;
  104. #endif
  105. PSCARD_IO_REQUEST scardIoRequest;
  106. case IOCTL_SMARTCARD_GET_ATTRIBUTE:
  107. //
  108. // Please refer to the Interoperrability standard for ICC
  109. //
  110. switch (SmartcardExtension->MinorIoControlCode) {
  111. case SCARD_ATTR_VENDOR_NAME:
  112. CheckUserBuffer(SmartcardExtension->VendorAttr.VendorName.Length);
  113. RtlCopyMemory(
  114. SmartcardExtension->IoRequest.ReplyBuffer,
  115. SmartcardExtension->VendorAttr.VendorName.Buffer,
  116. SmartcardExtension->VendorAttr.VendorName.Length
  117. );
  118. *SmartcardExtension->IoRequest.Information =
  119. SmartcardExtension->VendorAttr.VendorName.Length;
  120. break;
  121. case SCARD_ATTR_VENDOR_IFD_TYPE:
  122. CheckUserBuffer(SmartcardExtension->VendorAttr.IfdType.Length);
  123. RtlCopyMemory(
  124. SmartcardExtension->IoRequest.ReplyBuffer,
  125. SmartcardExtension->VendorAttr.IfdType.Buffer,
  126. SmartcardExtension->VendorAttr.IfdType.Length
  127. );
  128. *SmartcardExtension->IoRequest.Information =
  129. SmartcardExtension->VendorAttr.IfdType.Length;
  130. break;
  131. case SCARD_ATTR_VENDOR_IFD_VERSION:
  132. ReturnULong(
  133. SmartcardExtension->VendorAttr.IfdVersion.BuildNumber |
  134. SmartcardExtension->VendorAttr.IfdVersion.VersionMinor << 16 |
  135. SmartcardExtension->VendorAttr.IfdVersion.VersionMajor << 24
  136. );
  137. break;
  138. case SCARD_ATTR_VENDOR_IFD_SERIAL_NO:
  139. if (SmartcardExtension->VendorAttr.IfdSerialNo.Length == 0) {
  140. status = STATUS_NOT_SUPPORTED;
  141. } else {
  142. CheckUserBuffer(SmartcardExtension->VendorAttr.IfdSerialNo.Length);
  143. RtlCopyMemory(
  144. SmartcardExtension->IoRequest.ReplyBuffer,
  145. SmartcardExtension->VendorAttr.IfdSerialNo.Buffer,
  146. SmartcardExtension->VendorAttr.IfdSerialNo.Length
  147. );
  148. *SmartcardExtension->IoRequest.Information =
  149. SmartcardExtension->VendorAttr.IfdSerialNo.Length;
  150. }
  151. break;
  152. case SCARD_ATTR_DEVICE_UNIT:
  153. // Return the unit number of this device
  154. ReturnULong(SmartcardExtension->VendorAttr.UnitNo);
  155. break;
  156. case SCARD_ATTR_CHANNEL_ID:
  157. //
  158. // Return reader type / channel id in form
  159. // 0xDDDDCCCC where D is reader type and C is channel number
  160. //
  161. ReturnULong(
  162. SmartcardExtension->ReaderCapabilities.ReaderType << 16l |
  163. SmartcardExtension->ReaderCapabilities.Channel
  164. );
  165. break;
  166. case SCARD_ATTR_CHARACTERISTICS:
  167. // Return mechanical characteristics of the reader
  168. ReturnULong(
  169. SmartcardExtension->ReaderCapabilities.MechProperties
  170. )
  171. break;
  172. case SCARD_ATTR_CURRENT_PROTOCOL_TYPE:
  173. // Return the currently selected protocol
  174. CheckMinCardStatus(SCARD_NEGOTIABLE);
  175. ReturnULong(
  176. SmartcardExtension->CardCapabilities.Protocol.Selected
  177. );
  178. break;
  179. case SCARD_ATTR_CURRENT_CLK:
  180. //
  181. // Return the current ICC clock freq. encoded as little
  182. // endian integer value (3.58 MHZ is 3580)
  183. //
  184. CheckMinCardStatus(SCARD_NEGOTIABLE);
  185. if(SmartcardExtension->CardCapabilities.PtsData.CLKFrequency) {
  186. ReturnULong(SmartcardExtension->CardCapabilities.PtsData.CLKFrequency);
  187. } else {
  188. ReturnULong(SmartcardExtension->ReaderCapabilities.CLKFrequency.Default);
  189. }
  190. break;
  191. case SCARD_ATTR_CURRENT_F:
  192. // Return the current F value encoded as little endian integer
  193. CheckMinCardStatus(SCARD_NEGOTIABLE);
  194. ASSERT(SmartcardExtension->CardCapabilities.Fl <
  195. DIM(ClockRateConversion));
  196. ReturnULong(
  197. SmartcardExtension->CardCapabilities.ClockRateConversion[
  198. SmartcardExtension->CardCapabilities.Fl
  199. ].F
  200. );
  201. break;
  202. case SCARD_ATTR_CURRENT_D:
  203. //
  204. // Return the current D value encoded as little endian integer
  205. // in units of 1/64. So return 1 if D is 1/64.
  206. //
  207. CheckMinCardStatus(SCARD_NEGOTIABLE);
  208. ASSERT(
  209. SmartcardExtension->CardCapabilities.Dl <
  210. DIM(BitRateAdjustment)
  211. );
  212. ASSERT(
  213. SmartcardExtension->CardCapabilities.BitRateAdjustment[
  214. SmartcardExtension->CardCapabilities.Dl
  215. ].DDivisor != 0
  216. );
  217. //
  218. // Check the current value of Dl.
  219. // It should definitely not be greater than the array bounds
  220. // and the value in the array is not allowed to be zero
  221. //
  222. if (SmartcardExtension->CardCapabilities.Dl >=
  223. DIM(BitRateAdjustment) ||
  224. SmartcardExtension->CardCapabilities.BitRateAdjustment[
  225. SmartcardExtension->CardCapabilities.Dl
  226. ].DDivisor == 0) {
  227. status = STATUS_UNRECOGNIZED_MEDIA;
  228. break;
  229. }
  230. ReturnULong(
  231. SmartcardExtension->CardCapabilities.BitRateAdjustment[
  232. SmartcardExtension->CardCapabilities.Dl
  233. ].DNumerator /
  234. SmartcardExtension->CardCapabilities.BitRateAdjustment[
  235. SmartcardExtension->CardCapabilities.Dl
  236. ].DDivisor
  237. );
  238. break;
  239. case SCARD_ATTR_CURRENT_W:
  240. // Return the work waiting time (integer) for T=0
  241. CheckMinCardStatus(SCARD_NEGOTIABLE);
  242. ReturnULong(SmartcardExtension->CardCapabilities.T0.WI);
  243. break;
  244. case SCARD_ATTR_CURRENT_N:
  245. // Return extra guard time
  246. CheckMinCardStatus(SCARD_NEGOTIABLE);
  247. ReturnULong(SmartcardExtension->CardCapabilities.N);
  248. break;
  249. case SCARD_ATTR_CURRENT_IFSC:
  250. // Return the current information field size card
  251. CheckMinCardStatus(SCARD_NEGOTIABLE);
  252. if (SmartcardExtension->T1.IFSC) {
  253. ReturnULong(SmartcardExtension->T1.IFSC);
  254. } else {
  255. ReturnULong(SmartcardExtension->CardCapabilities.T1.IFSC);
  256. }
  257. break;
  258. case SCARD_ATTR_CURRENT_IFSD:
  259. // Return the current information field size card
  260. CheckMinCardStatus(SCARD_NEGOTIABLE);
  261. if (SmartcardExtension->T1.IFSD) {
  262. ReturnULong(SmartcardExtension->T1.IFSD);
  263. } else {
  264. ReturnULong(SmartcardExtension->ReaderCapabilities.MaxIFSD);
  265. }
  266. break;
  267. case SCARD_ATTR_CURRENT_BWT:
  268. // Return the current block waiting time for T=1
  269. CheckMinCardStatus(SCARD_NEGOTIABLE);
  270. ReturnULong(SmartcardExtension->CardCapabilities.T1.BWI);
  271. break;
  272. case SCARD_ATTR_CURRENT_CWT:
  273. // Return the current character waiting time for T=1
  274. CheckMinCardStatus(SCARD_NEGOTIABLE);
  275. ReturnULong(SmartcardExtension->CardCapabilities.T1.CWI);
  276. break;
  277. case SCARD_ATTR_CURRENT_EBC_ENCODING:
  278. // Return the current error checking method
  279. CheckMinCardStatus(SCARD_NEGOTIABLE);
  280. ReturnULong(SmartcardExtension->CardCapabilities.T1.EDC);
  281. break;
  282. case SCARD_ATTR_DEFAULT_CLK:
  283. ReturnULong(
  284. SmartcardExtension->ReaderCapabilities.CLKFrequency.Default
  285. );
  286. break;
  287. case SCARD_ATTR_MAX_CLK:
  288. ReturnULong(
  289. SmartcardExtension->ReaderCapabilities.CLKFrequency.Max
  290. );
  291. break;
  292. case SCARD_ATTR_DEFAULT_DATA_RATE:
  293. ReturnULong(
  294. SmartcardExtension->ReaderCapabilities.DataRate.Default
  295. );
  296. break;
  297. case SCARD_ATTR_MAX_DATA_RATE:
  298. ReturnULong(
  299. SmartcardExtension->ReaderCapabilities.DataRate.Max
  300. );
  301. break;
  302. case SCARD_ATTR_ATR_STRING:
  303. // Return ATR of currently inserted card
  304. CheckUserBuffer(MAXIMUM_ATR_LENGTH);
  305. CheckMinCardStatus(SCARD_NEGOTIABLE);
  306. RtlCopyMemory(
  307. SmartcardExtension->IoRequest.ReplyBuffer,
  308. SmartcardExtension->CardCapabilities.ATR.Buffer,
  309. SmartcardExtension->CardCapabilities.ATR.Length
  310. );
  311. *SmartcardExtension->IoRequest.Information =
  312. SmartcardExtension->CardCapabilities.ATR.Length;
  313. break;
  314. case SCARD_ATTR_ICC_TYPE_PER_ATR:
  315. //
  316. // Return ICC type, based on ATR.
  317. // We currently support only T=0 and T=1, so return
  318. // 1 for those protocols otherwise 0 (unknown ICC type)
  319. //
  320. CheckMinCardStatus(SCARD_NEGOTIABLE);
  321. ReturnUChar(
  322. ((SmartcardExtension->CardCapabilities.Protocol.Selected &
  323. (SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)) ? 1 : 0)
  324. );
  325. break;
  326. case SCARD_ATTR_ICC_PRESENCE:
  327. // Return the status of the card
  328. AccessUnsafeData(&Irql);
  329. switch (SmartcardExtension->ReaderCapabilities.CurrentState) {
  330. case SCARD_UNKNOWN:
  331. status = STATUS_INVALID_DEVICE_STATE;
  332. break;
  333. case SCARD_ABSENT:
  334. ReturnUChar(0);
  335. break;
  336. case SCARD_PRESENT:
  337. ReturnUChar(1);
  338. break;
  339. default:
  340. ReturnUChar(2);
  341. break;
  342. }
  343. EndAccessUnsafeData(Irql);
  344. break;
  345. case SCARD_ATTR_ICC_INTERFACE_STATUS:
  346. // Return if card contacts are active
  347. ReturnUChar(
  348. (SmartcardExtension->ReaderCapabilities.CurrentState >=
  349. SCARD_SWALLOWED ? (UCHAR) -1 : 0)
  350. );
  351. break;
  352. case SCARD_ATTR_PROTOCOL_TYPES:
  353. ReturnULong(
  354. SmartcardExtension->ReaderCapabilities.SupportedProtocols
  355. );
  356. break;
  357. case SCARD_ATTR_MAX_IFSD:
  358. ReturnULong(
  359. SmartcardExtension->ReaderCapabilities.MaxIFSD
  360. );
  361. break;
  362. case SCARD_ATTR_POWER_MGMT_SUPPORT:
  363. ReturnULong(
  364. SmartcardExtension->ReaderCapabilities.PowerMgmtSupport
  365. );
  366. break;
  367. default:
  368. status = STATUS_NOT_SUPPORTED;
  369. break;
  370. }
  371. break;
  372. case IOCTL_SMARTCARD_SET_ATTRIBUTE:
  373. switch (SmartcardExtension->MinorIoControlCode) {
  374. case SCARD_ATTR_SUPRESS_T1_IFS_REQUEST:
  375. //
  376. // The card does not support ifs request, so
  377. // we turn off ifs negotiation
  378. //
  379. SmartcardExtension->T1.State = T1_START;
  380. break;
  381. default:
  382. status = STATUS_NOT_SUPPORTED;
  383. break;
  384. }
  385. break;
  386. #if defined(DEBUG) && defined(SMCLIB_NT)
  387. case IOCTL_SMARTCARD_GET_PERF_CNTR:
  388. switch (SmartcardExtension->MinorIoControlCode) {
  389. case SCARD_PERF_NUM_TRANSMISSIONS:
  390. ReturnULong(SmartcardExtension->PerfInfo->NumTransmissions);
  391. break;
  392. case SCARD_PERF_BYTES_TRANSMITTED:
  393. ReturnULong(
  394. SmartcardExtension->PerfInfo->BytesSent +
  395. SmartcardExtension->PerfInfo->BytesReceived
  396. );
  397. break;
  398. case SCARD_PERF_TRANSMISSION_TIME:
  399. ReturnULong(
  400. (ULONG) (SmartcardExtension->PerfInfo->IoTickCount.QuadPart *
  401. KeQueryTimeIncrement() /
  402. 10)
  403. );
  404. break;
  405. }
  406. break;
  407. #endif
  408. case IOCTL_SMARTCARD_CONFISCATE:
  409. if (SmartcardExtension->ReaderFunction[RDF_CARD_CONFISCATE] == NULL) {
  410. status = STATUS_NOT_SUPPORTED;
  411. break;
  412. }
  413. status = SmartcardExtension->ReaderFunction[RDF_CARD_CONFISCATE](
  414. SmartcardExtension
  415. );
  416. break;
  417. case IOCTL_SMARTCARD_EJECT:
  418. if (SmartcardExtension->ReaderFunction[RDF_CARD_EJECT] == NULL) {
  419. status = STATUS_NOT_SUPPORTED;
  420. break;
  421. }
  422. status = SmartcardExtension->ReaderFunction[RDF_CARD_EJECT](
  423. SmartcardExtension
  424. );
  425. break;
  426. #ifdef SMCLIB_VXD
  427. case IOCTL_SMARTCARD_GET_LAST_ERROR:
  428. //
  429. // Return error of the last overlapped operation
  430. // Used for Windows VxD's that can't return the
  431. // error code within IoComplete like NT can
  432. //
  433. ReturnULong(SmartcardExtension->LastError);
  434. break;
  435. #endif
  436. case IOCTL_SMARTCARD_GET_STATE:
  437. // Return current state of the smartcard
  438. CheckUserBuffer(sizeof(ULONG));
  439. AccessUnsafeData(&Irql);
  440. *(PULONG) SmartcardExtension->IoRequest.ReplyBuffer =
  441. SmartcardExtension->ReaderCapabilities.CurrentState;
  442. *SmartcardExtension->IoRequest.Information =
  443. sizeof(ULONG);
  444. EndAccessUnsafeData(Irql);
  445. break;
  446. case IOCTL_SMARTCARD_POWER:
  447. if (SmartcardExtension->ReaderFunction[RDF_CARD_POWER] == NULL) {
  448. status = STATUS_NOT_SUPPORTED;
  449. break;
  450. }
  451. // Check if a card is present
  452. if (SmartcardExtension->ReaderCapabilities.CurrentState <=
  453. SCARD_ABSENT) {
  454. status = STATUS_INVALID_DEVICE_STATE;
  455. break;
  456. }
  457. // Initialize the card capabilities struct
  458. SmartcardInitializeCardCapabilities(
  459. SmartcardExtension
  460. );
  461. switch (SmartcardExtension->MinorIoControlCode) {
  462. case SCARD_COLD_RESET:
  463. case SCARD_WARM_RESET:
  464. CheckUserBuffer(MAXIMUM_ATR_LENGTH);
  465. case SCARD_POWER_DOWN:
  466. status = SmartcardExtension->ReaderFunction[RDF_CARD_POWER](
  467. SmartcardExtension
  468. );
  469. break;
  470. default:
  471. status = STATUS_INVALID_DEVICE_REQUEST;
  472. break;
  473. }
  474. break;
  475. case IOCTL_SMARTCARD_SET_PROTOCOL:
  476. //
  477. // Since we return the selected protocol, the return buffer
  478. // must be large enough to hold the result
  479. //
  480. CheckUserBuffer(sizeof(ULONG));
  481. // Set the protocol to be used with the current card
  482. if (SmartcardExtension->ReaderFunction[RDF_SET_PROTOCOL] == NULL) {
  483. status = STATUS_NOT_SUPPORTED;
  484. break;
  485. }
  486. // Check if we're already in specific state
  487. if (SmartcardExtension->ReaderCapabilities.CurrentState == SCARD_SPECIFIC &&
  488. (SmartcardExtension->CardCapabilities.Protocol.Selected &
  489. SmartcardExtension->MinorIoControlCode)) {
  490. status = STATUS_SUCCESS;
  491. break;
  492. }
  493. // Check if a card is present and not already in specific mode
  494. if (SmartcardExtension->ReaderCapabilities.CurrentState <=
  495. SCARD_ABSENT) {
  496. status = STATUS_INVALID_DEVICE_STATE;
  497. break;
  498. }
  499. // We only check the ATR when the user selects T=0 or T=1
  500. if (SmartcardExtension->MinorIoControlCode & (SCARD_PROTOCOL_Tx)) {
  501. if (SmartcardExtension->MinorIoControlCode & SCARD_PROTOCOL_DEFAULT) {
  502. // Select default PTS values
  503. SmartcardExtension->CardCapabilities.PtsData.Type = PTS_TYPE_DEFAULT;
  504. } else {
  505. // Select best possible PTS data
  506. SmartcardExtension->CardCapabilities.PtsData.Type = PTS_TYPE_OPTIMAL;
  507. }
  508. // Evaluate ATR
  509. status = SmartcardUpdateCardCapabilities(SmartcardExtension);
  510. } else {
  511. // caller doesn't want neither T=0 nor T=1 -> force callback
  512. status = STATUS_UNRECOGNIZED_MEDIA;
  513. }
  514. if (status == STATUS_UNRECOGNIZED_MEDIA &&
  515. SmartcardExtension->ReaderFunction[RDF_ATR_PARSE] != NULL) {
  516. // let the driver evaluate the ATR, since we don't know it
  517. status = SmartcardExtension->ReaderFunction[RDF_ATR_PARSE](
  518. SmartcardExtension
  519. );
  520. }
  521. if (status != STATUS_SUCCESS) {
  522. // Evaluation of the ATR failed It doesn't make sense to continue
  523. break;
  524. }
  525. // Check if card is now in the right status
  526. if (SmartcardExtension->ReaderCapabilities.CurrentState <
  527. SCARD_NEGOTIABLE) {
  528. status = STATUS_INVALID_DEVICE_STATE;
  529. break;
  530. }
  531. //
  532. // Check if the user tries to select a protocol that
  533. // the card doesn't support
  534. //
  535. if ((SmartcardExtension->CardCapabilities.Protocol.Supported &
  536. SmartcardExtension->MinorIoControlCode) == 0) {
  537. //
  538. // Since the card does not support the request protocol
  539. // we need to set back any automatic seletions done by
  540. // SmartcardUpdateCardCapabilities()
  541. //
  542. SmartcardExtension->ReaderCapabilities.CurrentState = SCARD_NEGOTIABLE;
  543. SmartcardExtension->CardCapabilities.Protocol.Selected = 0;
  544. status = STATUS_NOT_SUPPORTED;
  545. break;
  546. }
  547. status = SmartcardExtension->ReaderFunction[RDF_SET_PROTOCOL](
  548. SmartcardExtension
  549. );
  550. break;
  551. case IOCTL_SMARTCARD_TRANSMIT:
  552. if (SmartcardExtension->ReaderFunction[RDF_TRANSMIT] == NULL) {
  553. status = STATUS_NOT_SUPPORTED;
  554. break;
  555. }
  556. //
  557. // Check if card is in the right status
  558. //
  559. if (SmartcardExtension->ReaderCapabilities.CurrentState !=
  560. SCARD_SPECIFIC) {
  561. status = STATUS_INVALID_DEVICE_STATE;
  562. break;
  563. }
  564. if (SmartcardExtension->IoRequest.RequestBufferLength <
  565. sizeof(SCARD_IO_REQUEST)) {
  566. status = STATUS_INVALID_PARAMETER;
  567. break;
  568. }
  569. //
  570. // Check if the requested io-protocol matches
  571. // the prev. seleced protocol
  572. //
  573. scardIoRequest = (PSCARD_IO_REQUEST)
  574. SmartcardExtension->IoRequest.RequestBuffer;
  575. if (scardIoRequest->dwProtocol !=
  576. SmartcardExtension->CardCapabilities.Protocol.Selected) {
  577. status = STATUS_INVALID_DEVICE_STATE;
  578. break;
  579. }
  580. SmartcardExtension->SmartcardRequest.BufferLength = 0;
  581. #if defined(DEBUG) && defined(SMCLIB_NT)
  582. SmartcardExtension->PerfInfo->NumTransmissions += 1;
  583. if (SmartcardExtension->IoRequest.RequestBufferLength >=
  584. sizeof(SCARD_IO_REQUEST)) {
  585. bytesTransferred =
  586. SmartcardExtension->IoRequest.RequestBufferLength -
  587. sizeof(SCARD_IO_REQUEST);
  588. SmartcardExtension->PerfInfo->BytesSent +=
  589. bytesTransferred;
  590. }
  591. KeQueryTickCount(&SmartcardExtension->PerfInfo->TickStart);
  592. #endif
  593. status = SmartcardExtension->ReaderFunction[RDF_TRANSMIT](
  594. SmartcardExtension
  595. );
  596. #if defined(DEBUG) && defined(SMCLIB_NT)
  597. KeQueryTickCount(&SmartcardExtension->PerfInfo->TickEnd);
  598. if (*SmartcardExtension->IoRequest.Information >=
  599. sizeof(SCARD_IO_REQUEST)) {
  600. SmartcardExtension->PerfInfo->BytesReceived +=
  601. *SmartcardExtension->IoRequest.Information -
  602. sizeof(SCARD_IO_REQUEST);
  603. bytesTransferred +=
  604. *SmartcardExtension->IoRequest.Information -
  605. sizeof(SCARD_IO_REQUEST);
  606. }
  607. SmartcardExtension->PerfInfo->IoTickCount.QuadPart +=
  608. SmartcardExtension->PerfInfo->TickEnd.QuadPart -
  609. SmartcardExtension->PerfInfo->TickStart.QuadPart;
  610. if (FALSE) {
  611. ULONG timeInMilliSec = (ULONG)
  612. ((SmartcardExtension->PerfInfo->TickEnd.QuadPart -
  613. SmartcardExtension->PerfInfo->TickStart.QuadPart) *
  614. KeQueryTimeIncrement() /
  615. 10000);
  616. // check for a transferrate of < 400 bps
  617. if (status == STATUS_SUCCESS &&
  618. timeInMilliSec > 0 &&
  619. bytesTransferred * 5 < timeInMilliSec * 2) {
  620. SmartcardDebug(
  621. DEBUG_PERF,
  622. ("%s!SmartcardDeviceControl: Datarate for reader %*s was %3ld Baud (%3ld)\n",
  623. DRIVER_NAME,
  624. SmartcardExtension->VendorAttr.VendorName.Length,
  625. SmartcardExtension->VendorAttr.VendorName.Buffer,
  626. bytesTransferred * 1000 / timeInMilliSec,
  627. bytesTransferred)
  628. );
  629. }
  630. }
  631. #endif
  632. break;
  633. case IOCTL_SMARTCARD_SWALLOW:
  634. if (SmartcardExtension->ReaderFunction[RDF_READER_SWALLOW] == NULL) {
  635. status = STATUS_NOT_SUPPORTED;
  636. break;
  637. }
  638. status = SmartcardExtension->ReaderFunction[RDF_READER_SWALLOW](
  639. SmartcardExtension
  640. );
  641. break;
  642. #if DEBUG
  643. case IOCTL_SMARTCARD_DEBUG:
  644. //
  645. // Toggle debug bit
  646. //
  647. CurrentDebugLevel =
  648. SmartcardGetDebugLevel();
  649. SmartcardSetDebugLevel(
  650. SmartcardExtension->MinorIoControlCode ^ CurrentDebugLevel
  651. );
  652. break;
  653. #endif
  654. default:
  655. //
  656. // check if the bit for a vendor ioctl is set and if the driver
  657. // has registered a callback function
  658. //
  659. if ((SmartcardExtension->MajorIoControlCode & CTL_CODE(0, 2048, 0, 0)) == 0 ||
  660. SmartcardExtension->ReaderFunction[RDF_IOCTL_VENDOR] == NULL) {
  661. status = STATUS_INVALID_DEVICE_REQUEST;
  662. } else {
  663. //
  664. // Call the driver if it has registered a callback for vendor calls
  665. //
  666. status = SmartcardExtension->ReaderFunction[RDF_IOCTL_VENDOR](
  667. SmartcardExtension
  668. );
  669. }
  670. break;
  671. } // end switch
  672. return status;
  673. }