blob: f8a1438ae1da4b5bc93d8971b701a93a88b1da85 [file] [log] [blame]
Harald Weltea40c8e52019-09-27 19:22:34 +02001/* Card (ICC) UART driver for simple serial readers attached to tty.
2 * This allows you to use the CCID core in Linux userspace against a serial reader */
3
Harald Weltea67be5f2020-09-03 10:04:36 +02004/* (C) 2019-2020 by Harald Welte <laforge@gnumonks.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19 */
20
Harald Weltea40c8e52019-09-27 19:22:34 +020021#include <unistd.h>
22#include <termios.h>
23#include <errno.h>
24#include <sys/ioctl.h>
25
26#include <osmocom/core/select.h>
27#include <osmocom/core/serial.h>
28#include <osmocom/core/utils.h>
29
30#include "cuart.h"
31#include "utils_ringbuffer.h"
32
33/***********************************************************************
34 * low-level helper routines
35 ***********************************************************************/
36
37static int _init_uart(int fd)
38{
39 struct termios tio;
40 int rc;
41
42 rc = tcgetattr(fd, &tio);
43 if (rc < 0) {
44 perror("tcgetattr()");
45 return -EIO;
46 }
47
48 tio.c_iflag = 0;
49 tio.c_oflag = 0;
50 tio.c_lflag = 0;
51 tio.c_cflag = CREAD | CLOCAL | CSTOPB | PARENB | CS8 | B9600;
52
53 rc = tcsetattr(fd, TCSANOW, &tio);
54 if (rc < 0) {
55 perror("tcsetattr()");
56 return -EIO;
57 }
58
59 return 0;
60}
61
62static void _set_dtr(int fd, bool dtr)
63{
64 int status, rc;
65
66 rc = ioctl(fd, TIOCMGET, &status);
67 OSMO_ASSERT(rc == 0);
68 if (dtr) /* set DTR */
69 status |= TIOCM_DTR;
70 else
71 status &= ~TIOCM_DTR;
72 rc = ioctl(fd, TIOCMSET, &status);
73 OSMO_ASSERT(rc == 0);
74}
75
76static void _set_rts(int fd, bool rts)
77{
78 int status, rc;
79
80 rc = ioctl(fd, TIOCMGET, &status);
81 OSMO_ASSERT(rc == 0);
82 if (rts) /* set RTS */
83 status |= TIOCM_RTS;
84 else
85 status &= ~TIOCM_RTS;
86 rc = ioctl(fd, TIOCMSET, &status);
87 OSMO_ASSERT(rc == 0);
88}
89
90static int read_timeout(int fd, uint8_t *out, size_t len, unsigned long timeout_ms)
91{
92 struct timeval tv = { .tv_sec = timeout_ms / 1000, .tv_usec = (timeout_ms % 1000) * 1000 };
93 fd_set rd_set;
94 int rc;
95
96 FD_ZERO(&rd_set);
97 FD_SET(fd, &rd_set);
98 rc = select(fd+1, &rd_set, NULL, NULL, &tv);
99 if (rc == 0)
100 return -ETIMEDOUT;
101 else if (rc < 0)
102 return rc;
103
104 return read(fd, out, len);
105}
106
107static int _flush(int fd)
108{
109#if 0
110 uint8_t buf[1];
111 int rc;
112
113 while (1) {
114 rc = read_timeout(fd, buf, sizeof(buf), 10);
115 if (rc == -ETIMEDOUT)
116 return 0;
117 else if (rc < 0)
118 return rc;
119 }
120#else
121 return tcflush(fd, TCIFLUSH);
122#endif
123}
124
125/***********************************************************************
126 * Interface with card_uart (cuart) core
127 ***********************************************************************/
128
129/* forward-declaration */
130static struct card_uart_driver tty_uart_driver;
131static int tty_uart_close(struct card_uart *cuart);
132
133static int tty_uart_fd_cb(struct osmo_fd *ofd, unsigned int what)
134{
135 struct card_uart *cuart = (struct card_uart *) ofd->data;
136 uint8_t buf[256];
137 int rc;
138
139 if (what & OSMO_FD_READ) {
140 int i;
141 /* read any pending bytes and feed them into ring buffer */
142 rc = read(ofd->fd, buf, sizeof(buf));
143 OSMO_ASSERT(rc > 0);
144 for (i = 0; i < rc; i++) {
Harald Welte18ec3222019-09-28 21:41:59 +0200145#ifndef CREAD_ACTUALLY_WORKS
Harald Weltea40c8e52019-09-27 19:22:34 +0200146 /* work-around for https://bugzilla.kernel.org/show_bug.cgi?id=205033 */
Harald Welte18ec3222019-09-28 21:41:59 +0200147 if (cuart->tx_busy) {
148 if (cuart->u.tty.rx_count_during_tx < cuart->u.tty.tx_buf_len) {
149 /* FIXME: compare! */
150 cuart->u.tty.rx_count_during_tx += 1;
151 if (cuart->u.tty.rx_count_during_tx == cuart->u.tty.tx_buf_len) {
152 cuart->tx_busy = false;
153 card_uart_notification(cuart, CUART_E_TX_COMPLETE,
154 (void *)cuart->u.tty.tx_buf);
155 }
156 continue;
157 }
158 }
159#endif
Harald Weltea40c8e52019-09-27 19:22:34 +0200160 if (!cuart->rx_enabled)
161 continue;
162
Harald Welte7b64fc02019-10-09 20:49:35 +0200163 card_uart_wtime_restart(cuart);
164
Harald Weltea40c8e52019-09-27 19:22:34 +0200165 if (cuart->rx_threshold == 1) {
166 /* bypass ringbuffer and report byte directly */
167 card_uart_notification(cuart, CUART_E_RX_SINGLE, &buf[i]);
168 } else {
169 /* go via ringbuffer and notify only after threshold */
170 ringbuffer_put(&cuart->u.tty.rx_ringbuf, buf[i]);
171 if (ringbuffer_num(&cuart->u.tty.rx_ringbuf) >= cuart->rx_threshold)
172 card_uart_notification(cuart, CUART_E_RX_COMPLETE, NULL);
173 }
174 }
175 }
176 if (what & OSMO_FD_WRITE) {
177 unsigned int to_tx;
178 OSMO_ASSERT(cuart->u.tty.tx_buf_len > cuart->u.tty.tx_index);
179 /* push as many pending transmit bytes as possible */
180 to_tx = cuart->u.tty.tx_buf_len - cuart->u.tty.tx_index;
181 rc = write(ofd->fd, cuart->u.tty.tx_buf + cuart->u.tty.tx_index, to_tx);
182 OSMO_ASSERT(rc > 0);
183 cuart->u.tty.tx_index += rc;
184
185 /* if no more bytes to transmit, disable OSMO_FD_WRITE */
186 if (cuart->u.tty.tx_index >= cuart->u.tty.tx_buf_len) {
Harald Welte4c769e92020-10-18 22:33:59 +0200187 ofd->when &= ~OSMO_FD_WRITE;
Harald Welte18ec3222019-09-28 21:41:59 +0200188#ifndef CREAD_ACTUALLY_WORKS
189 /* don't immediately notify user; first wait for characters to be received */
190#else
Harald Weltea40c8e52019-09-27 19:22:34 +0200191 /* ensure everything is written (tx queue/fifo drained) */
192 tcdrain(cuart->u.tty.ofd.fd);
Harald Weltea40c8e52019-09-27 19:22:34 +0200193 cuart->tx_busy = false;
194 /* notify */
195 card_uart_notification(cuart, CUART_E_TX_COMPLETE, (void *)cuart->u.tty.tx_buf);
Harald Welte18ec3222019-09-28 21:41:59 +0200196#endif
Harald Weltea40c8e52019-09-27 19:22:34 +0200197 }
198 }
199 return 0;
200}
201
202static int tty_uart_open(struct card_uart *cuart, const char *device_name)
203{
204 int rc;
205
206 rc = ringbuffer_init(&cuart->u.tty.rx_ringbuf, cuart->u.tty.rx_buf, sizeof(cuart->u.tty.rx_buf));
207 if (rc < 0)
208 return rc;
209
210 cuart->u.tty.ofd.fd = -1;
211 rc = osmo_serial_init(device_name, B9600);
212 if (rc < 0)
213 return rc;
214
Harald Welte4c769e92020-10-18 22:33:59 +0200215 osmo_fd_setup(&cuart->u.tty.ofd, rc, OSMO_FD_READ, tty_uart_fd_cb, cuart, 0);
Harald Weltea40c8e52019-09-27 19:22:34 +0200216 cuart->u.tty.baudrate = B9600;
217
218 rc = _init_uart(cuart->u.tty.ofd.fd);
219 if (rc < 0) {
220 tty_uart_close(cuart);
221 return rc;
222 }
223
224 _flush(cuart->u.tty.ofd.fd);
225
226 osmo_fd_register(&cuart->u.tty.ofd);
227
228 return 0;
229}
230
231static int tty_uart_close(struct card_uart *cuart)
232{
233 OSMO_ASSERT(cuart->driver == &tty_uart_driver);
234 if (cuart->u.tty.ofd.fd != -1) {
235 osmo_fd_unregister(&cuart->u.tty.ofd);
236 close(cuart->u.tty.ofd.fd);
237 cuart->u.tty.ofd.fd = -1;
238 }
239 return 0;
240}
241
Harald Welte02dd9112019-10-01 08:55:29 +0200242static int tty_uart_async_tx(struct card_uart *cuart, const uint8_t *data, size_t len)
Harald Weltea40c8e52019-09-27 19:22:34 +0200243{
244 OSMO_ASSERT(cuart->driver == &tty_uart_driver);
245
246 cuart->u.tty.tx_buf = data;
247 cuart->u.tty.tx_buf_len = len;
Harald Welte18ec3222019-09-28 21:41:59 +0200248 cuart->u.tty.tx_index = 0;
249 cuart->u.tty.rx_count_during_tx = 0;
Harald Weltea40c8e52019-09-27 19:22:34 +0200250 cuart->tx_busy = true;
251 cuart->u.tty.ofd.when |= OSMO_FD_WRITE;
252
253 return 0;
254}
255
256static int tty_uart_async_rx(struct card_uart *cuart, uint8_t *data, size_t len)
257{
258 int rc, i;
259 OSMO_ASSERT(cuart->driver == &tty_uart_driver);
260
261 /* FIXME: actually do this asynchronously */
262 for (i = 0; i < len; i++) {
263 rc = ringbuffer_get(&cuart->u.tty.rx_ringbuf, &data[i]);
264 if (rc < 0)
265 return i;
266 }
267
268 return i;
269}
270
Harald Welte9700d392019-10-09 20:17:28 +0200271static int tty_uart_ctrl(struct card_uart *cuart, enum card_uart_ctl ctl, int arg)
Harald Weltea40c8e52019-09-27 19:22:34 +0200272{
273 struct termios tio;
274 int rc;
275
276 switch (ctl) {
277 case CUART_CTL_RX:
278 rc = tcgetattr(cuart->u.tty.ofd.fd, &tio);
279 if (rc < 0) {
280 perror("tcgetattr()");
281 return -EIO;
282 }
283 /* We do our best here, but lots of [USB] serial drivers seem to ignore
284 * CREAD, see https://bugzilla.kernel.org/show_bug.cgi?id=205033 */
Harald Welte7b64fc02019-10-09 20:49:35 +0200285 if (arg) {
Harald Weltea40c8e52019-09-27 19:22:34 +0200286 tio.c_cflag |= CREAD;
Harald Welte7b64fc02019-10-09 20:49:35 +0200287 card_uart_wtime_restart(cuart);
288 } else
Harald Weltea40c8e52019-09-27 19:22:34 +0200289 tio.c_cflag &= ~CREAD;
290 rc = tcsetattr(cuart->u.tty.ofd.fd, TCSANOW, &tio);
291 if (rc < 0) {
292 perror("tcsetattr()");
293 return -EIO;
294 }
295 break;
296 case CUART_CTL_RST:
Harald Welte9700d392019-10-09 20:17:28 +0200297 _set_rts(cuart->u.tty.ofd.fd, arg ? true : false);
298 if (arg)
Harald Weltea40c8e52019-09-27 19:22:34 +0200299 _flush(cuart->u.tty.ofd.fd);
300 break;
Harald Welte7b64fc02019-10-09 20:49:35 +0200301 case CUART_CTL_WTIME:
302 /* no driver-specific handling of this */
303 break;
Eric Wild70f36912020-04-03 22:29:02 +0200304 case CUART_CTL_POWER_5V0:
305 case CUART_CTL_POWER_3V0:
306 case CUART_CTL_POWER_1V8:
Harald Weltea40c8e52019-09-27 19:22:34 +0200307 case CUART_CTL_CLOCK:
308 default:
309 return -EINVAL;
310 }
311 return 0;
312}
313
314static const struct card_uart_ops tty_uart_ops = {
315 .open = tty_uart_open,
316 .close = tty_uart_close,
317 .async_tx = tty_uart_async_tx,
318 .async_rx = tty_uart_async_rx,
319 .ctrl = tty_uart_ctrl,
320};
321
322static struct card_uart_driver tty_uart_driver = {
323 .name = "tty",
324 .ops = &tty_uart_ops,
325};
326
327static __attribute__((constructor)) void on_dso_load_cuart_tty(void)
328{
329 card_uart_driver_register(&tty_uart_driver);
330}