summaryrefslogtreecommitdiff
path: root/lib/libsectok/scio.c
blob: 96104ecd0d3506f1cf0bdbf6ad8c99402ff9f328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* $Id: scio.c,v 1.12 2003/04/02 22:57:51 deraadt Exp $ */

/*
copyright 1997
the regents of the university of michigan
all rights reserved

permission is granted to use, copy, create derivative works 
and redistribute this software and such derivative works 
for any purpose, so long as the name of the university of 
michigan is not used in any advertising or publicity 
pertaining to the use or distribution of this software 
without specific, written prior authorization.  if the 
above copyright notice or any other identification of the 
university of michigan is included in any copy of any 
portion of this software, then the disclaimer below must 
also be included.

this software is provided as is, without representation 
from the university of michigan as to its fitness for any 
purpose, and without warranty by the university of 
michigan of any kind, either express or implied, including 
without limitation the implied warranties of 
merchantability and fitness for a particular purpose. the 
regents of the university of michigan shall not be liable 
for any damages, including special, indirect, incidental, or 
consequential damages, with respect to any claim arising 
out of or in connection with the use of the software, even 
if it has been or is hereafter advised of the possibility of 
such damages.
*/

/*
 * OS dependent part, Unix version
 *
 * Jim Rees, University of Michigan, October 1997
 */

#include <sys/ioctl.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/time.h>
#ifdef _AIX
#include <sys/select.h>
#endif /* _AIX */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>

#include "sectok.h"
#include "sc7816.h"
#include "todos_scrw.h"

#ifndef howmany
#define howmany(x, y) (((x) + ((y) - 1)) / (y))
#endif

static int todos_scfdopen(int ttyn, int fd, int flags, int *ep);
static int todos_sccts(int ttyn);

#ifdef __linux
static char ttynametmpl[] = "/dev/ttyS%01d";
#elif _AIX
static char ttynametmpl[] = "/dev/tty%01d";
#elif __OpenBSD__
static char ttynametmpl[] = "/dev/cua%02d";
#elif __sun
/*static char ttynametmpl[] = "/dev/cua/%c";*/
static char ttynametmpl[] = "/dev/tty%c";
#else
static char ttynametmpl[] = "/dev/tty%02d";
#endif

static struct {
    int fd, flags;
    pid_t pid;
    struct termios tio0, tio1;
} sc[4];

/* global variable */
#ifdef BYTECOUNT
int num_getc, num_putc;
#endif /* BYTECOUNT */

int
todos_scopen(int ttyn, int flags, int *ep)
{
    char ttyname[32];
    int fd, i, oflags;
    pid_t pid;

#ifdef BYTECOUNT
    num_getc = 0;
    num_putc = 0;
#endif /* BYTECOUNT */

#ifdef __sun
    snprintf(ttyname, sizeof ttyname, ttynametmpl, 'a' + ttyn);
#else
    snprintf(ttyname, sizeof ttyname, ttynametmpl, ttyn);
#endif

    
#ifdef DEBUG
    printf ("ttyname=%s\n", ttyname);
#endif /* DEBUG */
    oflags = O_RDWR;
    if (!(flags & SCODCD))
	oflags |= O_NONBLOCK;
    if ((fd = open(ttyname, oflags, 0)) < 0) {
	if (ep)
	    *ep = SCENOTTY;
	return -1;
    }

    if ((ttyn = todos_scfdopen(ttyn, fd, flags, ep)) < 0) {
	close(fd);
	return -1;
    }

    /* Figure out which reader we have */
    if (ioctl(fd, TIOCMGET, &i) < 0) {
	close(fd);
	if (ep)
	    *ep = SCENOTTY;
	return -1;
    }
#ifndef __sun
    /* Todos has RTS wired to RI, so set RTS and see if RI goes high */
    i |= TIOCM_RTS;
    ioctl(fd, TIOCMSET, &i);
    ioctl(fd, TIOCMGET, &i);
#else
    /* Sun serial port is broken, has no RI line */
    i = TIOCM_RI;
#endif
    if (i & TIOCM_RI) {
	/* Todos reader */
	scsleep(20);
	sc[ttyn].flags |= (SCOXCTS | SCOXDTR);
    }

    if (flags & SCODSR) {
	/* Wait for card present */
	while (!todos_sccardpresent(ttyn)) {
	    errno = 0;
	    sleep(1);
	    if (errno == EINTR)
		return -1;
	}
    }

    if (flags & SCOHUP) {
	/* spawn a process to wait for card removal */
	pid = fork();
	if (pid == 0) {
	    /* See if the card is still there */
	    while (todos_sccardpresent(ttyn))
		sleep(1);
	    kill(getppid(), SIGHUP);
	    exit(0);
	}
	sc[ttyn].pid = pid;
    }

    return ttyn;
}

int
todos_scsetflags(int ttyn, int flags, int mask)
{
    int oflags = sc[ttyn].flags;

    sc[ttyn].flags &= ~mask;
    sc[ttyn].flags |= (flags & mask);

    if ((sc[ttyn].flags & SCOINVRT) != (oflags & SCOINVRT)) {
	if (sc[ttyn].flags & SCOINVRT)
	    sc[ttyn].tio1.c_cflag |= PARODD;
	else
	    sc[ttyn].tio1.c_cflag &= ~PARODD;
	tcsetattr(sc[ttyn].fd, TCSADRAIN, &sc[ttyn].tio1);
    }

    return oflags;
}

/* NI: for Linux */
#if (B9600 != 9600)
struct speed_trans {
    speed_t t_val;
    int val;
    char *s;
} speed_trans[] = {
    {0, 0, "a"},
    {B9600, 9600, "a"},
    {B19200, 19200, "a"},
    {B38400, 38400, "a"},
    {B57600, 55928, "a"},
    {B57600, 57600, "a"},
    {B115200, 115200, "a"},
    {-1, -1, NULL}};
#endif /* (B9600 != 9600) */

int
todos_scsetspeed(int ttyn, int speed)
{

#if (B9600 == 9600)
    /* On OpenBSD, B9600 == 9600, and we can use the input argument
       "speed" of this function as an argument to cfset[io]speed(). */ 
#else
    /* On Linux, B9600 != 9600, and we have to translate the input argument
       to speed_t. */
    int i;

    for (i = 0; speed_trans[i].s; i++) 
	if (speed_trans[i].val == speed) break;
    if (speed_trans[i].s == NULL) {
	fprintf (stderr, "scsetspeed() failed : speed %d not supported.  ignore ...\n",
		 speed);
	return 0;
    }
    speed = speed_trans[i].t_val;
#endif    
    cfsetispeed(&sc[ttyn].tio1, speed);
    cfsetospeed(&sc[ttyn].tio1, speed);
    
    return tcsetattr(sc[ttyn].fd, TCSADRAIN, &sc[ttyn].tio1);
}

static int
todos_scfdopen(int ttyn, int fd, int flags, int *ep)
{
    struct termios t;

    /* Get and save the tty state */

    if (tcgetattr(fd, &t) < 0) {
	if (ep)
	    *ep = SCENOTTY;
	return -1;
    }
    sc[ttyn].fd = fd;
    sc[ttyn].tio0 = t;
    sc[ttyn].flags = flags;

    /* Now put the tty in a happy ISO state */

    /* 9600 bps */
    cfsetispeed(&t, B9600);
    cfsetospeed(&t, B9600);

    /* raw 8/E/2 */
    t.c_iflag &= ~(ISTRIP|INLCR|IGNCR|ICRNL|IXON);
    t.c_iflag |= (IGNBRK|IGNPAR);
    t.c_oflag &= ~OPOST;
    t.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
#ifdef CHWFLOW
    t.c_cflag &= ~CHWFLOW;
#endif
#ifdef CRTSCTS
    t.c_cflag &= ~CRTSCTS;
#endif
#ifdef CRTSXOFF
    t.c_cflag &= ~CRTSXOFF;
#endif
    t.c_cflag |= CLOCAL;

    /* 8/E/2 */
    t.c_cflag &= ~(CSIZE | PARODD);
    t.c_cflag |= (CS8 | PARENB | CSTOPB);

    t.c_cc[VMIN] = 1;
    t.c_cc[VTIME] = 0;

    if (tcsetattr(fd, TCSANOW, &t) < 0) {
	if (ep)
	    *ep = SCENOTTY;
	sc[ttyn].fd = -1;
	return -1;
    }
    sc[ttyn].tio1 = t;

    /* The open may or may not have reset the card.  Wait a while then flush
       anything that came in on the port. */
    scsleep(250);
    tcflush(sc[ttyn].fd, TCIFLUSH);

    return ttyn;
}

/* find out if there is a card in the reader */

int
todos_sccardpresent(int ttyn)
{
    return (sc[ttyn].flags & SCOXCTS) ? !todos_sccts(ttyn) : todos_scdsr(ttyn);
}

/* query dsr on the port (usually indicates whether the card is present) */

int
todos_scdsr(int ttyn)
{
    int fd = sc[ttyn].fd;
    int i;

    if (fd < 0 || ioctl(fd, TIOCMGET, &i) < 0)
	return 0;

    return ((i & TIOCM_DSR) ? 1 : 0);
}

static int
todos_sccts(int ttyn)
{
    int fd = sc[ttyn].fd;
    int i;

    if (fd < 0 || ioctl(fd, TIOCMGET, &i) < 0)
	return 0;

    return ((i & TIOCM_CTS) ? 1 : 0);
}

/* raise or lower dtr */

int
todos_scdtr(int ttyn, int cmd)
{
    int fd = sc[ttyn].fd;
    int i;

    if (!todos_sccardpresent(ttyn))
	return -1;

    if (ioctl(fd, TIOCMGET, &i) < 0)
	return -1;

    if (cmd)
	i |= TIOCM_DTR;
    else
	i &= ~TIOCM_DTR;
    i |= TIOCM_RTS;

    return ioctl(fd, TIOCMSET, &i);
}

int
todos_scclose(int ttyn)
{
    int fd = sc[ttyn].fd;

    tcsetattr(fd, TCSANOW, &sc[ttyn].tio0);
    close(fd);
    sc[ttyn].fd = -1;
    if (sc[ttyn].pid) {
	kill(sc[ttyn].pid, SIGTERM);
	sc[ttyn].pid = 0;
    }

#ifdef BYTECOUNT
    printf("#getc=%d, #putc=%d\n", num_getc - num_putc, num_putc);
#endif /* BYTECOUNT */    
    return 0;
}

/*
 * get one byte from the card.
 * wait at most ms msec.  0 for poll, -1 for infinite.
 * return byte in *cp.
 * return 0 or error.
 */

int
scgetc(int ttyn, unsigned char *cp, int ms)
{
    int fd = sc[ttyn].fd;
    fd_set *fdset;
    struct timeval tv, *tvp;

#ifdef BYTECOUNT
    num_getc++;
#endif /* BYTECOUNT */

    fdset = (fd_set *)calloc(howmany(fd + 1, NFDBITS), sizeof(fd_mask));
    if (fdset == NULL)
	return SCENOMEM;
    FD_SET(fd, fdset);

    if (ms == -1)
	tvp = NULL;
    else {
	tv.tv_sec = (ms + 1) / 1000;
	tv.tv_usec = (ms % 1000) * 1000;
	tvp = &tv;
    }

    if (select(fd + 1, fdset, NULL, NULL, tvp) != 1) {
	free(fdset);
	return SCTIMEO;
    }

    if (read(fd, cp, 1) != 1) {
	free(fdset);
	return SCTIMEO;
    }

    if (sc[ttyn].flags & SCOINVRT)
	*cp = todos_scinvert[*cp];

    free(fdset);
    return SCEOK; /* 0 */
}

/* write one byte to the card */

int
scputc(int ttyn, int ic)
{
    int fd = sc[ttyn].fd;
    unsigned char c0, c1;
    int code;

#ifdef BYTECOUNT
    num_putc++;
#endif /* BYTECOUNT */

    c0 = (sc[ttyn].flags & SCOINVRT) ? todos_scinvert[ic] : ic;
    write(fd, &c0, 1);

    /* gobble up the echo */
    code = scgetc(ttyn, &c1, 200);
#ifdef GOBBLEDEBUG
    if (sc[ttyn].flags & SCOINVRT)
	c1 = todos_scinvert[c1];
    if (code)
	printf("failed to gobble\n");
    else if (c0 != c1)
	printf("misgobbled %x != %x\n", c0, c1);
    else
	printf("gobble gobble %x\n", c0);
#endif
    return code;
}

int
scputblk(int ttyn, unsigned char *bp, int n)
{
    int fd = sc[ttyn].fd;
    unsigned char c;

    write(fd, bp, n);
    while (n--)
	scgetc(ttyn, &c, 30);

    return SCEOK;
}

void
scsleep(int ms)
{
    struct timeval tv;

    if (!ms)
	return;
    tv.tv_sec = (ms + 1) / 1000;
    tv.tv_usec = (ms % 1000) * 1000;

    select(0, NULL, NULL, NULL, &tv);
}