comparison doc/Serial-SIM-readers @ 47:b0cf75d0bb2d

doc/Serial-SIM-readers article written
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 21 Mar 2021 04:32:18 +0000
parents
children
comparison
equal deleted inserted replaced
46:e2ef4b8e4136 47:b0cf75d0bb2d
1 A serial SIM reader is a very simple hardware gadget consisting of just the
2 following:
3
4 * An always-on power supply (voltage determined by hw design) connected to SIM
5 VCC contact;
6
7 * An always-on fixed-frequency clock fed to SIM CLK contact;
8
9 * SIM I/O line connected to both TxD and RxD of a general-purpose UART, with
10 UART TxD driving SIM I/O by way of an open collector or open drain buffer;
11
12 * SIM RST line controlled by one of the modem control lines, either DTR or RTS.
13
14 In order to successfully communicate with the SIM, the UART baud rate needs to
15 be set to clock_freq/372, where clock_freq is the clock frequency fed to SIM CLK
16 contact, determined by the crystal oscillator used in the design of the serial
17 SIM reader gadget. This baud rate must be used initially for receiving ATR and
18 performing PTS, and it can also be used for the entire card session if the user
19 is content with the default slow speed - however, if the SIM card supports speed
20 enhancement (almost all cards do), this speed enhancement feature can be
21 supported with serial SIM readers if the UART baud rate can be switched under
22 software control from clock_freq/372 to clock_freq/64.
23
24 I (Mother Mychaela) am a very late comer to this game, thus I am not able to
25 speak with authority regarding the history of how serial SIM readers came about.
26 However, my understanding of this history is that early serial SIM readers were
27 RS-232 serial rather than USB-serial, thus they were constrained to use only
28 standard RS-232 baud rates like 9600 bps. And even when "serial" SIM readers
29 are really USB-serial, with a USB UART chip built directly into the custom
30 gadget, most people are still used to standard RS-232 baud rates (and the most
31 important OSes still make it unreasonably difficult to use non-standard baud
32 rates on USB-serial and other ports that are capable of such), thus the classic
33 baud rate of 9600 bps has stuck.
34
35 When a serial SIM reader gadget is designed to operate at 9600 baud, the crystal
36 oscillator frequency should ideally be 3.571200 MHz: 9600 * 372 = 3571200.
37 However, most real-life serial SIM readers out there have been built in a "race
38 to the bottom" fashion in terms of cost, and they typically feature crystals of
39 some frequency that is close to the ideal one, but not exactly - apparently
40 crystals of other nearby frequencies are cheaper than getting one for the ideal
41 frequency of 3.571200 MHz. (As a practical example, the USB-serial SIM reader
42 I bought on ebay in 2021-03 features a 3.579545 MHz crystal.) These serial SIM
43 readers with slightly-off crystals exploit the baud rate error tolerance that is
44 inherent in all UART interfaces.
45
46 The use of 3.571200 MHz (or close to this frequency) clocks in USB-serial SIM
47 reader designs is a case of conservative thinking, as opposed to maximizing
48 speed. The highest clock frequency allowed by SIM card specs is 5 MHz for
49 class A (5.0 V) or 4 MHz for classes B and C (3.0 V and 1.8 V), thus if someone
50 sought maximum SIM communication speed, they could use a higher frequency clock
51 (for example, the same 4.8 MHz clock that is used in the USB CCID reader on
52 which I measured it with an oscilloscope, HID Omnikey 3121), use a high-quality
53 USB-serial chip with good support for non-standard baud rates, and design the
54 software around the resulting non-standard baud rates, including baud rate
55 switching for SIM speed enhancement. If anyone has ever made such a USB-serial
56 SIM reader, our tools are ready to support it - but I don't know if anyone has
57 ever built such a gadget.
58
59 In case of serial SIM readers with 3.571200 MHz (or nearby) clocks, it is my
60 (Mychaela's) guess that their designers probably never considered the
61 possibility of using SIM speed enhancement, and that most programmers who write
62 software tools for such readers don't consider it either, resigning themselves
63 to just the default slow speed mode of F=372 D=1. However, if the SIM card
64 supports F=512 D=8 speed enhancement (almost all cards do) and this mode is
65 activated via PTS procedure, the baud rate becomes 55800 bps. This baud rate
66 is non-standard - however, the USB-serial chip in the SIM reader gadget I got
67 from ebay supports it just fine. (The USB-serial chip in this particular reader
68 is a PL2303, so I initially thought that it wouldn't support non-standard baud
69 rates - however, a closer investigation revealed that the popular notion of
70 these chips not supporting non-standard baud rates is actually a myth: PL2303
71 chips support non-standard baud rates just fine, instead this support was
72 lacking in earlier versions of the Linux kernel pl2303 driver.)
73
74 Our FC SIM tools back end for serial SIM readers supports 55800 baud speed
75 enhancement mode whenever the UART and its Linux kernel driver support it, and
76 we also support even higher speed enhancement modes: if the UART can handle
77 111600 baud, we can use F=512 D=16 mode, and if the UART can handle 223200 baud,
78 we can use F=512 D=32 mode which most newer SIM cards support. And yes, the
79 PL2303 USB-serial chip in the SIM reader I got from ebay supports this fastest
80 223200 baud mode just fine.
81
82 Using fc-simtool with serial SIM readers
83 ========================================
84
85 The most basic usage is as follows:
86
87 fc-simtool -d /dev/ttyXXX
88
89 If the serial SIM reader uses a 3.571200 MHz (or nearby frequency) crystal and
90 you are not going for speed enhancement (i.e., if you are sticking with 9600
91 baud for the entire card session), then no further options are needed.
92
93 If the clock frequency stays at 3.571200 MHz (or nearby) but you do wish to use
94 speed enhancement, add one of the following options:
95
96 -e1 if your UART can handle 55800 bps;
97 -e2 if your UART can handle 55800 and 111600 bps;
98 -e4 if your UART can handle 55800, 111600 and 223200 bps.
99
100 The actual selected mode will be based both on the UART capabilities declared
101 with the -e option and on the SIM card capabilities indicated in the ATR TA1
102 byte.
103
104 If your serial SIM reader uses a different clock frequency such that you need to
105 use baud rates other than 9600 and 55800 bps for standard and speed-enhanced
106 modes, respectively, then you need to use the more general -b option instead of
107 -e, taking one of the following forms:
108
109 -b base_baud
110 -b base_baud,spenh_baud
111 -b base_baud,spenh_baud,spenh_max
112
113 base_baud is the initial baud rate to use for receiving ATR; if no further
114 comma-separated parameters are specified, then this initial baud rate persists
115 for the entire card session without any speed enhancement modes. spenh_baud is
116 the baud rate to use for F=512 D=8 speed enhancement mode. spenh_max must be
117 1, 2, 4 or 8 (the default is 1); it is the maximum further multiplier that can
118 be applied to spenh_baud, if any.