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.

458 lines
12 KiB

  1. /*++
  2. Copyright (c) 1999 SCM Microsystems, Inc.
  3. Module Name:
  4. common.h
  5. Abstract:
  6. Constants, structures, macro etc.. for STC USB WDM
  7. Revision History:
  8. PP 01/20/1999 Initial Version
  9. --*/
  10. #if !defined( __COMMON_H__ )
  11. #define __COMMON_H__
  12. #include <ntstatus.h>
  13. #include <wdm.h>
  14. #include <usbdi.h>
  15. #include <usbdlib.h>
  16. #include <usb100.h>
  17. #include <DEVIOCTL.H>
  18. #define DRIVER_NAME "STCUSB"
  19. #include "SMCLIB.h"
  20. #include "WINSMCRD.h"
  21. //
  22. // Constants -----------------------------------------------------------------
  23. //
  24. #define SMARTCARD_POOL_TAG '4SCS'
  25. #define STCUSB_VENDOR_NAME "SCM Microsystems"
  26. #define STCUSB_PRODUCT_NAME "STCUSB"
  27. #define MAX_READ_REGISTER_BUFFER_SIZE 18
  28. #define STCUSB_MAX_DEVICE 16
  29. #define USB_WRITE_RETRIES 2
  30. #define IOCTL_WRITE_STC_REGISTER SCARD_CTL_CODE(0x800)
  31. #define IOCTL_READ_STC_REGISTER SCARD_CTL_CODE(0x801)
  32. #define IOCTL_WRITE_STC_DATA SCARD_CTL_CODE(0x802)
  33. #define IOCTL_READ_STC_DATA SCARD_CTL_CODE(0x803)
  34. #define POLLING_PERIOD 500
  35. #define CLA_IDX 0
  36. #define INS_IDX 1
  37. #define P1_IDX 2
  38. #define P2_IDX 3
  39. #define P3_IDX 4
  40. #define ISO_OUT TRUE
  41. #define ISO_IN !ISO_OUT
  42. #define NAD_IDX 0x00
  43. #define PCB_IDX 0x01
  44. #define LEN_IDX 0x02
  45. #define DATA_IDX 0x03
  46. #define PROLOGUE_LEN 0x03
  47. #define EPILOGUE_LEN 0x01
  48. #define OSC 16000
  49. #define FREQ 3580
  50. #define CYC_TO_MS( cyc ) ((ULONG)( cyc / FREQ ))
  51. // register addresses
  52. #define ADR_ETULENGTH15 0x00
  53. #define ADR_ETULENGTH7 0x01
  54. #define ADR_CGT8 0x02
  55. #define ADR_CGT7 0x03
  56. #define ADR_CWT31 0x04
  57. #define ADR_CWT23 0x05
  58. #define ADR_CWT15 0x06
  59. #define ADR_CWT7 0x07
  60. #define ADR_BGT8 0x08
  61. #define ADR_BGT7 0x09
  62. #define ADR_BWT31 0x0A
  63. #define ADR_BWT23 0x0B
  64. #define ADR_BWT15 0x0C
  65. #define ADR_BWT7 0x0D
  66. #define ADR_TCON 0x0E
  67. #define ADR_UART_CONTROL 0x0F
  68. #define ADR_FIFO_CONFIG 0x10
  69. #define ADR_INT_CONTROL 0x11
  70. #define ADR_INT_STATUS 0x12
  71. #define ADR_DATA 0x13
  72. #define ADR_IO_CONFIG 0x14
  73. #define ADR_SC_CONTROL 0x15
  74. #define ADR_CLOCK_CONTROL 0x16
  75. // clock control register
  76. #define M_CKE 0x01
  77. #define M_OEN 0x02
  78. // ETU length register
  79. #define M_ETU_RST 0x80
  80. #define M_DIV 0x30
  81. #define M_DIV1 0x20
  82. #define M_DIV0 0x10
  83. #define M_ETUH 0x0F
  84. #define M_ETUL 0xFF
  85. // CGT length register
  86. #define M_CGTH 0x01
  87. #define M_CGTL 0XFF
  88. // BGT length register
  89. #define M_BGTH 0x01
  90. #define M_BGTL 0xFF
  91. // CWT register
  92. #define M_CWT4 0xFF
  93. #define M_CWT3 0xFF
  94. #define M_CWT2 0xFF
  95. #define M_CWT1 0xFF
  96. // TCON register
  97. #define M_MGT 0x80
  98. #define M_MWT 0x40
  99. #define M_WTR 0x04
  100. #define M_GT 0x02
  101. #define M_WT 0x01
  102. // UART control register
  103. #define M_UEN 0x40
  104. #define M_UART_RST 0x20
  105. #define M_CONV 0x10
  106. #define M_TS 0x08
  107. #define M_PE 0x04
  108. #define M_R 0x03
  109. // FIFO config register
  110. #define M_RFP 0x80
  111. #define M_LD 0x0F
  112. // INT control register
  113. #define M_SSL 0x20
  114. #define M_DRM 0x10
  115. #define M_DSM 0x08
  116. #define M_WTE 0x04
  117. #define M_SIM 0x02
  118. #define M_MEM 0x01
  119. #define M_DRM_MEM 0x11
  120. // INT status register
  121. #define M_FNE 0x80
  122. #define M_FE 0x40
  123. #define M_OE 0x20
  124. #define M_DR 0x10
  125. #define M_TRE 0x08
  126. #define M_WTOVF 0x04
  127. #define M_SENSE 0x02
  128. #define M_MOV 0x01
  129. // SMART card interface
  130. #define M_ALT1 0x20
  131. #define M_ALT2 0x10
  132. #define M_ALT0 0x08
  133. #define M_SDE 0x04
  134. #define M_SL 0x02
  135. #define M_SD 0x01
  136. // SMART card control register
  137. #define M_IO 0x80
  138. #define M_VCE 0x40
  139. #define M_SC_RST 0x20
  140. #define M_SCE 0x10
  141. #define M_SCK 0x08
  142. #define M_C8 0x04
  143. #define M_C4 0x02
  144. #define M_VPE 0x01
  145. // Nad
  146. #define HOST_TO_STC1 0x12
  147. #define HOST_TO_STC2 0x52
  148. #define HOST_TO_ICC1 0x02
  149. #define HOST_TO_ICC2 0x42
  150. #define STC1_TO_HOST 0x21
  151. #define STC2_TO_HOST 0x25
  152. #define ICC1_TO_HOST 0x20
  153. #define ICC2_TO_HOST 0x24
  154. // PCB
  155. #define PCB 0x00
  156. #define CLA_READ_REGISTER 0x00
  157. #define INS_READ_REGISTER 0xB0
  158. #define CLA_WRITE_REGISTER 0x00
  159. #define INS_WRITE_REGISTER 0xD0
  160. #define CLA_READ_FIRMWARE_REVISION 0x00
  161. #define INS_READ_FIRMWARE_REVISION 0xB1
  162. #define PCB_DEFAULT 0x00
  163. #define TLV_BUFFER_SIZE 0x20
  164. #define ATR_SIZE 0x40 // TS + 32 + SW + PROLOGUE + EPILOGUE...
  165. #define MAX_T1_BLOCK_SIZE 270
  166. // ATR interface byte coding in TS
  167. #define TAx 0x01
  168. #define TBx 0x02
  169. #define TCx 0x04
  170. #define TDx 0x08
  171. #define FREQ_DIV 1 // 3,58 MHz XTAL -> SC Clock = 3.58MHz
  172. //#define FREQ_DIV 0x08 /* 30MHz XTAL -> SC Clock = 3.75MHz */
  173. #define PROTOCOL_TO 0
  174. #define PROTOCOL_T1 1
  175. #define PROTOCOL_T14 14
  176. #define PROTOCOL_T15 15
  177. #define STC_READ_TIMEOUT 1000
  178. // max. of communication errors while polling the device
  179. #define ERROR_COUNTER_TRESHOLD 5
  180. //
  181. // Define the facility codes
  182. //
  183. #define FACILITY_SCARD 0x10
  184. #define FACILITY_RPC_STUBS 0x3
  185. #define FACILITY_RPC_RUNTIME 0x2
  186. #define FACILITY_IO_ERROR_CODE 0x4
  187. //
  188. // Define the severity codes
  189. //
  190. #define STATUS_SEVERITY_WARNING 0x2
  191. #define STATUS_SEVERITY_SUCCESS 0x0
  192. #define STATUS_SEVERITY_INFORMATIONAL 0x1
  193. #define STATUS_SEVERITY_ERROR 0x3
  194. //
  195. // MessageId: STCUSB_NO_DEVICE_FOUND
  196. //
  197. // MessageText:
  198. //
  199. // No USB smart card reader found in the system.
  200. //
  201. #define STCUSB_NO_DEVICE_FOUND ((NTSTATUS)0xC0100001L)
  202. //
  203. // MessageId: STCUSB_CANT_INITIALIZE_READER
  204. //
  205. // MessageText:
  206. //
  207. // The attached reader is not working properly.
  208. //
  209. #define STCUSB_CANT_INITIALIZE_READER ((NTSTATUS)0xC0100002L)
  210. //
  211. // MessageId: STCUSB_INSUFFICIENT_RESOURCES
  212. //
  213. // MessageText:
  214. //
  215. // Insufficient system resources to start device.
  216. //
  217. #define STCUSB_INSUFFICIENT_RESOURCES ((NTSTATUS)0xC0100003L)
  218. //
  219. // MessageId: STCUSB_ERROR_CLAIM_RESOURCES
  220. //
  221. // MessageText:
  222. //
  223. // Resources can not be claimed or an resource conflict exists.
  224. //
  225. #define STCUSB_ERROR_CLAIM_RESOURCES ((NTSTATUS)0xC0100006L)
  226. //
  227. // MessageId: STCUSB_NO_MEMORY
  228. //
  229. // MessageText:
  230. //
  231. // The system does not have enough memory.
  232. //
  233. #define STCUSB_NO_MEMORY ((NTSTATUS)0xC0040007L)
  234. //
  235. // MessageId: STCUSB_TOO_MANY_ERRORS
  236. //
  237. // MessageText:
  238. //
  239. // The error treshold while detecting a smart card was reached.
  240. //
  241. #define STCUSB_TOO_MANY_ERRORS ((NTSTATUS)0xC0040008L)
  242. //
  243. // MessageId: STCUSB_USB_MSG
  244. //
  245. // MessageText:
  246. //
  247. // SmartCard USB Driver : %2.
  248. //
  249. #define STCUSB_USB_MSG ((NTSTATUS)0x40100010L)
  250. //
  251. // MessageId: STCUSB_USB_ERROR
  252. //
  253. // MessageText:
  254. //
  255. // SmartCard USB Driver : %2.
  256. //
  257. #define STCUSB_USB_ERROR ((NTSTATUS)0xC0100011L)
  258. //
  259. // MessageId: STCUSB_TIMEOUT
  260. //
  261. // MessageText:
  262. //
  263. // SmartCard Reader TIMEOUT : %2.
  264. //
  265. #define STCUSB_TIMEOUT ((NTSTATUS)0xC00000B5L)
  266. //
  267. // Macros --------------------------------------------------------------------
  268. //
  269. #define SysCompareMemory( p1, p2, Len ) ( RtlCompareMemory( p1,p2, Len ) != Len )
  270. #define SysCopyMemory( pDest, pSrc, Len ) RtlCopyMemory( pDest, pSrc, Len )
  271. #define SysFillMemory( pDest, Value, Len ) RtlFillMemory( pDest, Len, Value )
  272. //
  273. // Structures ----------------------------------------------------------------
  274. //
  275. typedef struct _DEVICE_EXTENSION
  276. {
  277. // The PDO that we are attached to
  278. PDEVICE_OBJECT AttachedPDO;
  279. // Our PnP device name
  280. UNICODE_STRING DeviceName;
  281. // Current number of io-requests
  282. LONG IoCount;
  283. // Used to access IoCount;
  284. KSPIN_LOCK SpinLock;
  285. // Used to signal that the device has been removed
  286. //KEVENT ReaderRemoved;
  287. // Used to signal that the reader is able to process reqeusts
  288. KEVENT ReaderStarted;
  289. // Used to signal the the reader has been closed
  290. LONG ReaderOpen;
  291. // Used to synchonize the polling thread
  292. KMUTEX hMutex;
  293. // Used to keep track of the current power state the reader is in
  294. LONG PowerState;
  295. // configuration handle for the configuration the device is currently in use
  296. USBD_CONFIGURATION_HANDLE ConfigurationHandle;
  297. // ptr to the USB device descriptor for this device
  298. PUSB_DEVICE_DESCRIPTOR DeviceDescriptor;
  299. // we support up to one interface
  300. PUSBD_INTERFACE_INFORMATION Interface;
  301. // poll thread relevant data
  302. KEVENT FinishPollThread;
  303. KEVENT PollThreadStopped;
  304. PIO_WORKITEM PollWorkItem;
  305. SMARTCARD_EXTENSION SmartcardExtension;
  306. } DEVICE_EXTENSION, *PDEVICE_EXTENSION;
  307. typedef enum _READER_POWER_STATE {
  308. PowerReaderUnspecified = 0,
  309. PowerReaderWorking,
  310. PowerReaderOff
  311. } READER_POWER_STATE, *PREADER_POWER_STATE;
  312. typedef struct _READER_EXTENSION {
  313. UCHAR Device;
  314. // Software revision ID of the firmware.
  315. UCHAR FirmwareMajor, FirmwareMinor;
  316. BOOLEAN CardPresent;
  317. // Current reader power state.
  318. READER_POWER_STATE ReaderPowerState;
  319. // read timeout in ms
  320. ULONG ReadTimeout;
  321. PDEVICE_OBJECT DeviceObject;
  322. UCHAR ucReadBuffer[MIN_BUFFER_SIZE];
  323. ULONG ulReadBufferLen;
  324. // counter for communication errors while polling the reader
  325. ULONG ErrorCounter;
  326. // pre-allocated buffer to perform read/write in extension
  327. PUCHAR pExtBuffer; // added CB_09/02/01
  328. // pre-allocated URB in extension to be used in UsbRead and UsbWrite CB_09/02/01
  329. PURB pUrb; // added CB_09/02/01
  330. // Priority of the KeThread.... to support low PC ressources
  331. LONG Chosen_Priority;
  332. } READER_EXTENSION, *PREADER_EXTENSION;
  333. #define SIZEOF_READER_EXTENSION ( sizeof( READER_EXTENSION ))
  334. typedef struct _STC_REGISTER
  335. {
  336. UCHAR Register;
  337. UCHAR Size;
  338. ULONG Value;
  339. } STC_REGISTER, *PSTC_REGISTER;
  340. //
  341. // wrapper ------------------------------------------------------------------
  342. //
  343. #define IFReadSTCRegister UsbReadSTCRegister
  344. #define IFReadSTCData UsbReadSTCData
  345. #define IFWriteSTCRegister UsbWriteSTCRegister
  346. #define IFWriteSTCData UsbWriteSTCData
  347. #endif // __COMMON_H__
  348. // ------------------------------- END OF FILE -------------------------------