blob: d71af86660c5239f89fbe91bef366f11df17f3a6 [file] [log] [blame]
Christina Quastdb7b1ab2015-03-03 12:34:36 +01001/* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2008, Atmel Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * Atmel's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ----------------------------------------------------------------------------
28 */
29
30//------------------------------------------------------------------------------
31/// \unit
32///
33/// !Purpose
34///
35/// Definition of methods for using a CCID device driver.
36///
37/// !Usage
38///
39/// -# CCIDDriver_Initialize
40/// -# CCID_Read
41/// -# CCID_Write
42/// -# CCID_SmartCardRequest
43/// -# CCID_Insertion
44/// -# CCID_Removal
45/// -# RDRtoPCHardwareError
46//------------------------------------------------------------------------------
47
48#ifndef CCID_DRIVER_H
49#define CCID_DRIVER_H
50
51// FIXME: This is dirty! Typedef copied from USBD.h
52typedef void (*TransferCallback)(void *pArg,
53 uint8_t status,
54 uint32_t transferred,
55 uint32_t remaining);
56
57
58
59/// For reference, the absolute maximum block size
60/// for a TPDU T=0 block is 260 bytes (5 bytes command; 255 bytes data), or
61/// for a TPDU T=1 block is 259 bytes, or
62/// for a short APDU T=1 block is 261 bytes, or
63/// for an extended APDU T=1 block is 65544 bytes.
64#define ABDATA_SIZE 260
65
66/// define protocol T=0
67#define PROTOCOL_TO 0
68/// define protocol T=1
69#define PROTOCOL_T1 1
70
71/// define for dwFeatures see Table 5.1-1 Smart Card Device Class Descriptors
72/// No special characteristics
73#define CCID_FEATURES_NADA 0x00000000
74/// Automatic parameter configuration based on ATR data
75#define CCID_FEATURES_AUTO_PCONF 0x00000002
76/// Automatic activation of ICC on inserting
77#define CCID_FEATURES_AUTO_ACTIV 0x00000004
78/// Automatic ICC voltage selection
79#define CCID_FEATURES_AUTO_VOLT 0x00000008
80/// Automatic ICC clock frequency change according to active parameters provided
81/// by the Host or self determined
82#define CCID_FEATURES_AUTO_CLOCK 0x00000010
83/// Automatic baud rate change according to active parameters provided by the
84/// Host or self determined
85#define CCID_FEATURES_AUTO_BAUD 0x00000020
86/// Automatic parameters negotiation made by the CCID (use of warm or cold
87/// resets or PPS according to a manufacturer proprietary algorithm to select
88/// the communication parameters with the ICC)
89#define CCID_FEATURES_AUTO_PNEGO 0x00000040
90/// Automatic PPS made by the CCID according to the active parameters
91#define CCID_FEATURES_AUTO_PPS 0x00000080
92/// CCID can set ICC in clock stop mode
93#define CCID_FEATURES_ICCSTOP 0x00000100
94/// NAD value other than 00 accepted (T=1 protocol in use)
95#define CCID_FEATURES_NAD 0x00000200
96/// Automatic IFSD exchange as first exchange (T=1 protocol in use)
97#define CCID_FEATURES_AUTO_IFSD 0x00000400
98/// TPDU level exchanges with CCID
99#define CCID_FEATURES_EXC_TPDU 0x00010000
100/// Short APDU level exchange with CCID
101#define CCID_FEATURES_EXC_SAPDU 0x00020000
102/// Short and Extended APDU level exchange with CCID
103#define CCID_FEATURES_EXC_APDU 0x00040000
104/// USB Wake up signaling supported on card insertion and removal
105#define CCID_FEATURES_WAKEUP 0x00100000
106
107//------------------------------------------------------------------------------
108// Types
109//------------------------------------------------------------------------------
110
111/// Bulk CCID Message header structure
112typedef struct
113{
114 unsigned char bMessageType;
115 /// Message-specific data length
116 unsigned long wLength;
117 /// Identifies the slot number for this command
118 unsigned char bSlot;
119 /// Sequence number for command.
120 unsigned char bSeq;
121 /// Slot status register
122 unsigned char bStatus;
123 /// Slot error
124 unsigned char bError;
125 /// specific register
126 unsigned char bSpecific;
127 /// Data block sent to the CCID.
128 unsigned char abData[ABDATA_SIZE];
129 unsigned char bSizeToSend;
130} __attribute__ ((packed)) S_ccid_bulk_in_header;
131
132/// 6.1 Bulk Transfers
133typedef struct
134{
135 unsigned char bMessageType;
136 /// Message-specific data length
137 unsigned long wLength;
138 /// Identifies the slot number for this command
139 unsigned char bSlot;
140 /// Sequence number for command.
141 unsigned char bSeq;
142 /// specific register
143 unsigned char bSpecific_0;
144 unsigned char bSpecific_1;
145 unsigned char bSpecific_2;
146 /// Application Protocol Data Unit
147 unsigned char APDU[ABDATA_SIZE];
148} __attribute__ ((packed)) S_ccid_bulk_out_header;
149
150
151/// 6.1.11.2 PIN Verification Data Structure
152typedef struct
153{
154 /// Number of seconds.
155 unsigned char bTimerOut;
156 /// Several parameters for the PIN format options
157 unsigned char bmFormatString;
158 /// Define the length of the PIN to present in the APDU command
159 unsigned char bmPINBlockString;
160 /// Allows the length PIN insertion in the APDU command
161 unsigned char bmPINLengthFormat;
162 /// Minimum PIN size in digit and Maximum PIN size in digit
163 unsigned char wPINMaxExtraDigit;
164 /// The value is a bit wise OR operation.
165 unsigned char bEntryValidationCondition;
166 /// Number of messages to display for the PIN modify command
167 unsigned char bNumberMessage;
168 /// Language used to display the messages.
169 unsigned char wLangId;
170 /// Message index in the Reader message table
171 unsigned char bMsgIndex;
172 /// T=1 I-block prologue field to use
173 unsigned char bTeoPrologue[3];
174 /// APDU to send to the ICC
175 unsigned char abPINApdu[255];
176}__attribute__ ((packed)) S_ccid_PIN_Verification;
177
178
179/// 6.1.11.7 PIN Modification Data Structure
180typedef struct
181{
182 /// Number of seconds. If 00h then CCID default value is used.
183 unsigned char bTimeOut;
184 /// Several parameters for the PIN format options (defined in § 6.1.11.4)
185 unsigned char bmFormatString4;
186 /// Define the length of the PIN to present in the APDU command
187 unsigned char bmPINBlockString;
188 /// Allows the length PIN insertion in the APDU command (defined in § 6.1.11.6)
189 unsigned char bmPinLengthFormat;
190 /// Insertion position offset in byte for the current PIN
191 unsigned char bInsertionOffsetOld;
192 /// Insertion position offset in byte for the new PIN
193 unsigned char bInsertionOffsetNew;
194 /// XXYYh
195 /// XX: Minimum PIN size in digit
196 /// YY: Maximum PIN size in digit
197 unsigned char wPINMaxExtraDigit;
198 /// 00h,01h,02h,03h
199 /// Indicates if a confirmation is requested before acceptance of a new PIN (meaning that the user has to enter this new PIN twice before it is accepted)
200 /// Indicates if the current PIN must be entered and set in the same APDU field of not.
201 unsigned char bConfirmPIN;
202 /// The value is a bit wise OR operation.
203 /// 01h Max size reached
204 /// 02h Validation key pressed
205 /// 04h Timeout occurred
206 unsigned char bEntryValidationCondition;
207 /// 00h,01h,02h,03h,or FFh
208 /// Number of messages to display for the PIN modify command.
209 unsigned char bNumberMessage;
210 /// Language used to display the messages. The 16 bit
211 unsigned char wLangId;
212 /// Message index in the Reader message table (should be 00h or 01h).
213 unsigned char bMsgIndex1;
214 /// Message index in the Reader message table (should be 01h or 02h).
215 unsigned char bMsgIndex2;
216 /// Message index in the Reader message table (should be 02h).
217 unsigned char bMsgIndex3;
218 /// T=1 I-block prologue field to use. Significant only if protocol in use is T=1.
219 unsigned char bTeoPrologue[3];
220 /// Byte array APDU to send to the ICC
221 unsigned char abPINApdu[255];
222}__attribute__ ((packed)) S_ccid_PIN_Modification;
223
224/// Protocol Data Structure for Protocol T=0 (bProtocolNum=0, dwLength=00000005h)
225typedef struct
226{
227 /// B7-4 – FI – Index into the table 7 in ISO/IEC 7816-3:1997 selecting a
228 /// clock rate conversion factor
229 /// B3-0 – DI - Index into the table 8 in ISO/IEC 7816-3:1997 selecting a
230 /// baud rate conversion factor
231 unsigned char bmFindexDindex;
232 /// For T=0 ,B0 – 0b, B7-2 – 000000b
233 /// B1 – Convention used (b1=0 for direct, b1=1 for inverse)
234 unsigned char bmTCCKST0; // 0 to 2
235 /// Extra Guardtime between two characters. Add 0 to 254 etu to the normal
236 /// guardtime of 12etu. FFh is the same as 00h.
237 unsigned char bGuardTimeT0; // 0 to FF
238 /// WI for T=0 used to define WWT
239 unsigned char bWaitingIntegerT0; // 0 to FF
240 /// ICC Clock Stop Support
241 /// 00 = Stopping the Clock is not allowed
242 /// 01 = Stop with Clock signal Low
243 /// 02 = Stop with Clock signal High
244 /// 03 = Stop with Clock either High or Low
245 unsigned char bClockStop; // 0 to 3
246} __attribute__ ((packed)) S_ccid_protocol_t0;
247
248
249/// Protocol Data Structure for Protocol T=1 (bProtocolNum=1, dwLength=00000007h)
250typedef struct
251{
252 /// B7-4 – FI – Index into the table 7 in ISO/IEC 7816-3:1997 selecting a
253 /// clock rate conversion factor
254 /// B3-0 – DI - Index into the table 8 in ISO/IEC 7816-3:1997 selecting a
255 /// baud rate conversion factor
256 unsigned char bmFindexDindex;
257 /// For T=1, B7-2 – 000100b
258 /// B0 – Checksum type (b0=0 for LRC, b0=1 for CRC
259 /// B1 – Convention used (b1=0 for direct, b1=1 for inverse)
260 unsigned char bmTCCKST1; // 10h, 11h, 12h, 13h
261 /// Extra Guardtime (0 to 254 etu between two characters).
262 /// If value is FFh, then guardtime is reduced by 1.
263 unsigned char bGuardTimeT1; // 0 to FF
264 /// B7-4 = BWI
265 /// B3-0 = CWI
266 unsigned char bmWaitingIntegersT1; // 0 to 9
267 /// ICC Clock Stop Support
268 /// 00 = Stopping the Clock is not allowed
269 /// 01 = Stop with Clock signal Low
270 /// 02 = Stop with Clock signal High
271 /// 03 = Stop with Clock either High or Low
272 unsigned char bClockStop; // 0 to 3
273 /// Size of negotiated IFSC
274 unsigned char bIFSC; // 0 to FE
275 /// Nad value used by CCID
276 unsigned char bNadValue; // 0 to FF
277} __attribute__ ((packed)) S_ccid_protocol_t1;
278
279
280/// Identifies the length of type of subordinate descriptors of a CCID device
281/// Table 5.1-1 Smart Card Device Class descriptors
282typedef struct
283{
284 /// Size of this descriptor, in bytes.
285 unsigned char bLength;
286 /// Functional Descriptor type
287 unsigned char bDescriptorType;
288 /// Integrated Circuit(s) Cards Interface Devices (CCID) Specification
289 /// Release Number
290 unsigned short bcdCCID;
291 /// Index of the highest available slot. An USB-ICC is regarded as a single
292 /// slot CCID.
293 unsigned char bMaxSlotIndex;
294 /// This value indicates what voltages the CCID can supply to its slots.
295 /// It is a bitwise OR operation performed on the following values:
296 /// - 01h 5.0V
297 /// - 02h 3.0V
298 /// - 04h 1.8V
299 /// Other bits are RFU.
300 unsigned char bVoltageSupport;
301 /// RRRR –Upper Word- is RFU = 0000h
302 /// PPPP –Lower Word- Encodes the supported protocol types. A ‘1’ in a given
303 /// bit position indicates support for the associated ISO protocol.
304 /// 0001h = Protocol T=0
305 /// 0002h = Protocol T=1
306 /// All other bits are reserved and must be set to zero. The field is
307 /// intended to correspond to the PCSC specification definitions.
308 unsigned long dwProtocols;
309 /// Default ICC clock frequency in KHz. This is an integer value.
310 unsigned long dwDefaultClock;
311 /// Maximum supported ICC clock frequency in KHz. This is an integer value.
312 unsigned long dwMaximumClock;
313 /// The number of clock frequencies that are supported by the CCID. If the
314 /// value is 00h, the supported clock frequencies are assumed to be the
315 /// default clock frequency defined by dwDefaultClock and the maximum clock
316 /// frequency defined by dwMaximumClock.
317 unsigned char bNumClockSupported;
318 /// Default ICC I/O data rate in bps. This is an integer value
319 unsigned long dwDataRate;
320 /// Maximum supported ICC I/O data rate in bps
321 unsigned long dwMaxDataRate;
322 /// The number of data rates that are supported by the CCID.
323 unsigned char bNumDataRatesSupported;
324 /// Indicates the maximum IFSD supported by CCID for protocol T=1.
325 unsigned long dwMaxIFSD;
326 /// - RRRR-Upper Word- is RFU = 0000h
327 /// - PPPP-Lower Word- encodes the supported protocol types. A ‘1’ in a given
328 /// bit position indicates support for the associated protocol.
329 /// 0001h indicates support for the 2-wire protocol 1
330 /// 0002h indicates support for the 3-wire protocol 1
331 /// 0004h indicates support for the I2C protocol 1
332 /// All other values are outside of this specification, and must be handled
333 /// by vendor-supplied drivers.
334 unsigned long dwSynchProtocols;
335 /// The value is a bitwise OR operation performed on the following values:
336 /// - 00000000h No special characteristics
337 /// - 00000001h Card accept mechanism 2
338 /// - 00000002h Card ejection mechanism 2
339 /// - 00000004h Card capture mechanism 2
340 /// - 00000008h Card lock/unlock mechanism
341 unsigned long dwMechanical;
342 /// This value indicates what intelligent features the CCID has.
343 unsigned long dwFeatures;
344 /// For extended APDU level the value shall be between 261 + 10 (header) and
345 /// 65544 +10, otherwise the minimum value is the wMaxPacketSize of the
346 /// Bulk-OUT endpoint.
347 unsigned long dwMaxCCIDMessageLength;
348 /// Significant only for CCID that offers an APDU level for exchanges.
349 unsigned char bClassGetResponse;
350 /// Significant only for CCID that offers an extended APDU level for exchanges.
351 unsigned char bClassEnvelope;
352 /// Number of lines and characters for the LCD display used to send messages for PIN entry.
353 unsigned short wLcdLayout;
354 /// This value indicates what PIN support features the CCID has.
355 unsigned char bPINSupport;
356 /// Maximum number of slots which can be simultaneously busy.
357 unsigned char bMaxCCIDBusySlots;
358
359} __attribute__ ((packed)) CCIDDescriptor;
360
361//------------------------------------------------------------------------------
362// Exported functions
363//------------------------------------------------------------------------------
364
365extern unsigned char RDRtoPCHardwareError( unsigned char bSlot,
366 unsigned char bSeq,
367 unsigned char bHardwareErrorCode );
368
369/*
370#if !defined(NOAUTOCALLBACK)
371extern void USBDCallbacks_RequestReceived(const USBGenericRequest *request);
372#endif
373*/
374extern void CCID_SmartCardRequest( void );
375extern void CCIDDriver_Initialize( void );
376extern unsigned char CCID_Read(void *pBuffer,
377 unsigned int dLength,
378 TransferCallback fCallback,
379 void *pArgument);
380extern unsigned char CCID_Write(void *pBuffer,
381 unsigned int dLength,
382 TransferCallback fCallback,
383 void *pArgument);
384extern unsigned char CCID_Insertion( void );
385extern unsigned char CCID_Removal( void );
386
387#endif //#ifndef CCID_DRIVER_H
388