Android ccid driver


New CCID 1.4. and card movement notification mechanism
Card movement recognition within the ancient occasions

Prior to the mechanism I’ll describe within this blog publish was implemented and used, the only method pcsc-lite detected a card movement (card insertion or removal) ended up being to poll the driver every 200ms to request if your card was present or otherwise within the readers and also to match up against the prior condition.


Every driver (i.e. that old ones) implements the IFDHICCPresence function:

This function returns the status from the card placed within the readers/slot per Lun.


Active polling

The driver will be continuously asking the readers. This really is host CPU “intensive” and can avoid the CPU to use sleep condition to limit its energy consumption. You should use the powertop tool to determine that. This really is BAD.


One other issue would be that the pcscd is informed as much as 200ms following the card status has transformed. Therefore if the application delays for any card insertion you are able to waste as much as 200ms between your card insertion and also the application being informed.

libccid 1.4.

4 days ago I launched a brand new version 1.4. of my CCID driver (libccid).


1.4. – 4 August 2010, Ludovic Rousseau

add support of Kingtrust Multi-Readers, Dectel CI692, Todos CX00,
C3PO LTC36, ACS AET65, Broadcom 5880, Tianyu Wise Card Readers,
Gemalto Hybrid Smartcard Readers
Add support from the SCM SDI 010 again. A minimum of the contact
interface may be used.
Use libusb-1. rather than libusb-.1
add support of TAG_IFD_STOP_POLLING_THREAD and utilisation of the asynchronous libusb API to have the ability to stop a transfer.
Request pcsc-lite 1.6.2 minimum (rather than 1.6.) to possess TAG_IFD_STOP_POLLING_THREAD defined
The O2MICRO OZ776 patch (for OZ776, OZ776_7772, REINER_SCT and BLUDRIVEII_CCID) isn’t any more supported with libusb-1.
properly obtain the IFSC in the ATR (ATR parsing wasn’t always correct)
some minor bugs removed

The modification which will interest us this is actually the support of TAG_IFD_STOP_POLLING_THREAD. This really is used to ensure that pcscd can request the driver to prevent its polling function.

However the support of TAG_IFD_STOP_POLLING_THREAD only has be incorporated in pcsc-lite 1.6.2. For this reason you’ll need pcsc-lite 1.6.2 to compile libccid 1.4..

pcsc-lite 1.6.2

Within 24 hours from the libcid 1.4. release I additionally launched pcsc-lite 1.6.2.


pcsc-lite-1.6.2: Ludovic Rousseau
4 August 2010

implement a “Forced suicide” mechanism. After 3 Control-C with little reaction from pcscd (actually the motorists) we pressure the suicide. Sometimes libusb is blocked inside a type of dead-lock and kill -9 was your best option.
Add support of TAG_IFD_STOP_POLLING_THREAD to request the stop from the driver polling function.
Avoid a division by . Shuts [#312555] “simclist bug in pcsc-lite”
if pcscd is began by libpcsclite then close all file handles except stdin, stdout and stderr to ensure that pcscd doesn’t confiscate assets allotted through the application
just in case of auto exit produce a new session to ensure that Control-C around the application won’t also quit pcscd
src/hotplug_libusb.c: port from libusb-.1 to libusb-1.
default configuration has become $sysconfdir/readers.conf.d
fix crash with empty config dir
another minor enhancements and bug corrections

Card movement notification

So how exactly does the credit card movement notification works together with the CCID protocol?

USB interrupt finish point

Some visitors (although not all) offers an interrupt endpoint in the USB level. This endpoint can be used to signal a card movement towards the host even when the host doesn’t send a CCID command to become informed. The host just needs to browse the interrupt endpoint.

In the CCID specs 1.1 &sect 5.2.3 Interrupt-IN Endpoint:

The interrupt pipe is required for any CCID that supports ICC insertion/removal. It’s optional for any CCID with ICCs which are always placed and aren’t detachable. If there’s an Interrupt-In endpoint, then your RDR_to_PC_NotifySlotChange message is needed and all sorts of other messages are optional.

RDR_to_PC_NotifySlotChange message

The very first byte is bMessageType and it is value is 0×50 showing RDR_to_PC_NotifySlotChange.

The following bytes are bmSlotICCState:

This area is reported on byte granularity.
The dimensions is ( 2 bits * quantity of slots ) put together towards the nearest byte.
Each slot has 2 bits. Minimal significant bit reviews the present condition from the slot (0b = no ICC present, 1b = ICC present). The most important bit reviews if the slot has transformed condition because the last RDR_to_PC_NotifySlotChange message was sent (0b = no change, 1b = change).
If no slot is available for any given location, the area returns 00b in individuals 2 bits.
Example: A 3 slot CCID reviews just one byte using the following format:
Bit = Slot current condition
Bit 1 = Slot transformed status
Bit 2 = Slot 1 current condition
Bit 3 = Slot 1 transformed status
Bit 4 = Slot 2 current condition
Bit 5 = Slot 2 transformed status
Bit 6 = 0b
Bit 7 = 0b

In a nutshell, and when the readers only has one slot, you’ll receive 0×50 0×03 whenever a card is taken away and 0×50 0×02 whenever a card is placed.



Using an interrupt finish point has already been supported in libusb-.1 however i had many issues when utilizing this selection. The primary problem is the fact that libusb-.1 calls are synchronous which is difficult to (cleanly) interrupt a usb_interrupt_read call. This will make it impossible to cleanly steer clear of the driver when pcscd exits.

One hack I made use of is to help make the usb_interrupt_read call timeout after 2 seconds. But it’s still a type of polling, even when the time has become 2 seconds rather than 200 ms.


libusb-1. includes a new API and, particularly, an asynchronous API. While using asynchronous API you’ll be able to cancel a continuing call. This is just what is performed when TAG_IFD_STOP_POLLING_THREAD is asked for by pcscd towards the driver.

But libusb-1. is comparatively new and it has bugs. The most recent stable version 1..8 is not free of bugs and that i found some bugs particularly cases. Bugs happen to be reported towards the libusb project and many already are remedied.


pcscd when combined with my CCID driver has become a great citizen concerning the computer CPU use. All of the internal active polling loops happen to be removed and pcsc-lite has become completely event handled.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>