xref: /OK3568_Linux_fs/kernel/Documentation/driver-api/serial/serial-iso7816.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun=============================
2*4882a593SmuzhiyunISO7816 Serial Communications
3*4882a593Smuzhiyun=============================
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun1. Introduction
6*4882a593Smuzhiyun===============
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun  ISO/IEC7816 is a series of standards specifying integrated circuit cards (ICC)
9*4882a593Smuzhiyun  also known as smart cards.
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun2. Hardware-related considerations
12*4882a593Smuzhiyun==================================
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun  Some CPUs/UARTs (e.g., Microchip AT91) contain a built-in mode capable of
15*4882a593Smuzhiyun  handling communication with a smart card.
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun  For these microcontrollers, the Linux driver should be made capable of
18*4882a593Smuzhiyun  working in both modes, and proper ioctls (see later) should be made
19*4882a593Smuzhiyun  available at user-level to allow switching from one mode to the other, and
20*4882a593Smuzhiyun  vice versa.
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun3. Data Structures Already Available in the Kernel
23*4882a593Smuzhiyun==================================================
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun  The Linux kernel provides the serial_iso7816 structure (see [1]) to handle
26*4882a593Smuzhiyun  ISO7816 communications. This data structure is used to set and configure
27*4882a593Smuzhiyun  ISO7816 parameters in ioctls.
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun  Any driver for devices capable of working both as RS232 and ISO7816 should
30*4882a593Smuzhiyun  implement the iso7816_config callback in the uart_port structure. The
31*4882a593Smuzhiyun  serial_core calls iso7816_config to do the device specific part in response
32*4882a593Smuzhiyun  to TIOCGISO7816 and TIOCSISO7816 ioctls (see below). The iso7816_config
33*4882a593Smuzhiyun  callback receives a pointer to struct serial_iso7816.
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun4. Usage from user-level
36*4882a593Smuzhiyun========================
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun  From user-level, ISO7816 configuration can be get/set using the previous
39*4882a593Smuzhiyun  ioctls. For instance, to set ISO7816 you can use the following code::
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun	#include <linux/serial.h>
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun	/* Include definition for ISO7816 ioctls: TIOCSISO7816 and TIOCGISO7816 */
44*4882a593Smuzhiyun	#include <sys/ioctl.h>
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun	/* Open your specific device (e.g., /dev/mydevice): */
47*4882a593Smuzhiyun	int fd = open ("/dev/mydevice", O_RDWR);
48*4882a593Smuzhiyun	if (fd < 0) {
49*4882a593Smuzhiyun		/* Error handling. See errno. */
50*4882a593Smuzhiyun	}
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun	struct serial_iso7816 iso7816conf;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun	/* Reserved fields as to be zeroed */
55*4882a593Smuzhiyun	memset(&iso7816conf, 0, sizeof(iso7816conf));
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun	/* Enable ISO7816 mode: */
58*4882a593Smuzhiyun	iso7816conf.flags |= SER_ISO7816_ENABLED;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun	/* Select the protocol: */
61*4882a593Smuzhiyun	/* T=0 */
62*4882a593Smuzhiyun	iso7816conf.flags |= SER_ISO7816_T(0);
63*4882a593Smuzhiyun	/* or T=1 */
64*4882a593Smuzhiyun	iso7816conf.flags |= SER_ISO7816_T(1);
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun	/* Set the guard time: */
67*4882a593Smuzhiyun	iso7816conf.tg = 2;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun	/* Set the clock frequency*/
70*4882a593Smuzhiyun	iso7816conf.clk = 3571200;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun	/* Set transmission factors: */
73*4882a593Smuzhiyun	iso7816conf.sc_fi = 372;
74*4882a593Smuzhiyun	iso7816conf.sc_di = 1;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun	if (ioctl(fd_usart, TIOCSISO7816, &iso7816conf) < 0) {
77*4882a593Smuzhiyun		/* Error handling. See errno. */
78*4882a593Smuzhiyun	}
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun	/* Use read() and write() syscalls here... */
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun	/* Close the device when finished: */
83*4882a593Smuzhiyun	if (close (fd) < 0) {
84*4882a593Smuzhiyun		/* Error handling. See errno. */
85*4882a593Smuzhiyun	}
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun5. References
88*4882a593Smuzhiyun=============
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun [1]    include/uapi/linux/serial.h
91