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