-
Notifications
You must be signed in to change notification settings - Fork 3
/
unixware-dev.c
1197 lines (1014 loc) · 22.2 KB
/
unixware-dev.c
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Copyright (C) Cyclades Corporation, 1999-1999. All rights reserved.
*
*
* dev.c
* Unix Pty Device routines
*
* History
* 08/17/1999 V.1.0.0 Initial revision
*
*/
/*
* Argh => sys/stream.h needs this # define, and it nust be before types.h,
* and undefined before signal.h
*/
# define _KERNEL
/* Open / stat includes */
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
/* Errno */
# include <stdio.h>
# include <errno.h>
# include <string.h>
/* ptsname */
# include <stdlib.h>
# include <sys/ptms.h>
/* Termio */
# ifdef USE_TERMIO
# include <termio.h>
# endif
/* Termios */
# ifdef USE_TERMIOS
# include <termios.h>
# endif
/* STREAMS ioctl */
# include <sys/types.h>
# include <stropts.h>
# include <sys/stream.h>
# undef _KERNEL
/* Tiocsignal */
# include <signal.h>
# define _TSR_DEV_
#include "inc/cyclades-ser-cli.h"
#include "inc/system.h"
#include "inc/tsrio.h"
#include "inc/telnet.h"
#include "inc/dev.h"
#include "inc/port_speed.h"
#ifdef TSR_MEASURE
#include "inc/tsrmeasure.h"
#endif
/*
* Streams message routines
*/
void parse_message(unsigned char type, char *buf, int size);
void parse_msgflush(int queues);
/*
* Termio / Termios routines
*/
void parse_ioctl(int ioctype, void *iocdata);
void parse_termios(int mode, struct termios *tp);
void parse_termio(int mode, struct termio *tp);
void parse_break(int *interval);
void parse_iocflush(int *queues);
int termio_to_portmodes(struct termio *tp);
void termio_to_portconfig(struct termio *tp, struct portconfig *pcp);
int termios_to_portmodes(struct termios *tp);
void termios_to_portconfig(struct termios *tp, struct portconfig *pcp);
void portconfig_to_termios(struct portconfig *pcp, struct termios *tp);
/*
* Debug routines
*/
void print_msg(int type, unsigned char *buf, int size);
char *ioctl_name(int type, void *arg);
/*
* Internal Variables
*/
char P_sname[NAMESIZE];
int Lasttype;
char Ctlbuf[16], Databuf[DEV_MAXIOSZ];
struct strpeek Message;
/*
* STREAMS Pseudo-TTY Device Stuff
*/
# define PTY_DEVICE "/dev/ptmx"
int
dev_getaddr(char *dname)
{
int fd;
int mode;
char *stty;
int found;
struct stat statb;
if (lstat(dname, &statb) >= 0) { /* File exists */
if (S_ISLNK(statb.st_mode)) {
sysmessage(MSG_WARNING, "Removing old sym-link \"%s\".\n", dname);
unlink(dname);
}
else if (!S_ISCHR(statb.st_mode)) {
sysmessage(MSG_ERR, "%s already exists\n", dname);
return (E_PARMINVAL);
}
}
else if (errno != ENOENT) { /* generic stat error */
sysmessage(MSG_ERR, "Can't lstat %s : %s\n", dname, strerror(errno));
return (E_FILEIO);
}
mode = O_RDWR;
# ifdef USE_POSIX_NONBLOCK
mode |= O_NONBLOCK;
# elif defined USE_STD_NDELAY
mode |= O_NDELAY;
# endif
/*
* Warning: most PTY implementation puts master side as controlling terminal if
* O_NOCTTY is not set !!!
*/
mode |= O_NOCTTY;
if ((fd = open(PTY_DEVICE, mode)) < 0) {
sysmessage(MSG_ERR, "Can't open ptmx : %s\n", strerror(errno));
return (E_FILEIO);
}
# ifdef USE_FIONBIO
mode = 1;
if (ioctl(fd, FIONBIO, &mode) == -1) {
sysmessage(MSG_ERR,
"Can't set non-block on maste: pty : %s\n",
strerror(errno));
close(fd);
return (E_FILEIO);
}
# endif
if (ioctl(fd, I_PUSH, "pckt") == -1) {
sysmessage(MSG_ERR, "Can't push module: %s\n", strerror(errno));
(void) close(fd);
return (E_FILEIO);
}
found = FALSE;
if ((stty = ptsname(fd)) != (char *) NULL) {
if (unlockpt(fd) >= 0) {
if (link(stty, dname) == 0) {
found = TRUE;
sysmessage(MSG_NOTICE, "Using %s pseudo-tty \n", stty);
}
else {
sysmessage(MSG_ERR, "Can't link dev : %s\n", strerror(errno));
}
}
else {
sysmessage(MSG_ERR, "Can't unlock ptmx : %s\n", strerror(errno));
}
}
else {
sysmessage(MSG_ERR, "Can't get ptmx ptsname\n");
}
if (found == FALSE) {
(void) close(fd);
return (E_FILEIO);
}
P_mfd = fd;
strcpy(P_sname, stty);
strcpy(P_devname, dname);
return (E_NORMAL);
}
void
dev_free(void)
{
(void) close(P_sfd);
P_sfd = -1;
return;
}
int
dev_init(int iosize, int devmodem, int closemode, struct buffer *ibp,
struct buffer *obp, struct comport *cp)
{
int ret;
Pty.portmodes = 0;
if (devmodem == DEV_LOCAL) {
Pty.portmodes = PORT_CLOCAL;
}
if (closemode == CLOSE_HANG) {
Pty.portmodes |= PORT_HUPCL;
}
Pty.portmodes |= PORT_IGNBRK | PORT_IGNPAR;
Pty.iosize = iosize;
Pty.inbuff = ibp;
Pty.outbuff = obp;
Pty.comport = cp;
return (E_NORMAL);
}
int
dev_config(void)
{
int sfd;
struct termios tios;
struct portconfig *pcp = &Pty.comport->portconfig;
int modes = Pty.portmodes;
struct strbuf *ctlmsg = &Message.ctlbuf;
struct strbuf *datamsg = &Message.databuf;
int flags = 0;
sysmessage(MSG_NOTICE, "Opening %s pseudo-tty \n", P_sname);
if ((sfd = open(P_sname, O_RDWR | O_NOCTTY)) == -1) {
sysmessage(MSG_ERR, "Can't open slave device : %s\n",
strerror(errno));
return (E_FILEIO);
}
if (ioctl(sfd, I_PUSH, "ptem") == -1) {
sysmessage(MSG_ERR, "Can't ioctl tcgeta: %s\n", strerror(errno));
(void) close(sfd);
return (E_FILEIO);
}
if (ioctl(sfd, I_PUSH, "ldterm") == -1) {
sysmessage(MSG_ERR, "Can't push ldterm : %s\n", strerror(errno));
(void) close(sfd);
return (E_FILEIO);
}
memset((void *) &tios, 0, sizeof(struct termios));
portconfig_to_termios(pcp, &tios);
tios.c_cflag |= CREAD;
tios.c_lflag |= NOFLSH;
/* PTY modes */
if (modes & PORT_HUPCL) {
tios.c_cflag |= HUPCL;
}
if (modes & PORT_CLOCAL) {
tios.c_cflag |= CLOCAL;
}
if (modes & PORT_IGNBRK) {
tios.c_iflag |= IGNBRK;
}
if (modes & PORT_IGNPAR) {
tios.c_iflag |= IGNPAR;
}
if (tcsetattr(sfd, TCSANOW, &tios) == -1) {
sysmessage(MSG_ERR, "Can't set termios : %s\n", strerror(errno));
(void) close(sfd);
return (E_FILEIO);
}
P_sfd = sfd;
ctlmsg->buf = Ctlbuf;
ctlmsg->maxlen = 1;
datamsg->buf = Databuf;
datamsg->maxlen = Pty.iosize;
(void) getmsg(P_mfd, ctlmsg, datamsg, &flags);
return (E_NORMAL);
}
int
dev_closeslave(void)
{
if (Pty.state == PTY_OPER && P_sfd != -1) {
sysmessage(MSG_NOTICE, "Closing %s pseudo-tty \n", P_sname);
(void) close(P_sfd);
P_sfd = -1;
}
return (E_NORMAL);
}
int
dev_probe(void)
{
int ctllen, datalen;
int retc;
int clocal = 0;
int retmsg;
unsigned char type;
struct strbuf *ctlmsg = &Message.ctlbuf;
struct strbuf *datamsg = &Message.databuf;
struct iocblk *iocp;
int ioctype;
unsigned char *iocdata;
struct termio *tio;
struct termios *tios;
ctlmsg->buf = Ctlbuf;
ctlmsg->maxlen = 1;
datamsg->buf = Databuf;
datamsg->maxlen = Pty.iosize;
Message.flags = 0;
if ((retc = ioctl(P_mfd, I_PEEK, (void *) &Message)) == -1) {
sysmessage(MSG_ERR,
"Can't get a message from master pty: %s\n",
strerror(errno));
return (retc);
}
ctllen = ctlmsg->len;
datalen = datamsg->len;
type = (unsigned char) ctlmsg->buf[0];
if (Debug > 2) {
sysmessage(MSG_DEBUG,
"PROBE: Ctl: %d bytes, Type: %d, Data: %d bytes, flags: %08X\n",
ctllen, type, datalen, Message.flags);
}
switch (type) {
case M_DATA:
if (datalen != 0) {
retmsg = PROBE_DATA; /* M_DATA > 0 */
}
else {
retmsg = PROBE_EOF; /* M_DATA == 0 */
}
break;
case M_FLUSH:
retmsg = PROBE_FLUSH; /* M_FLUSH */
break;
case M_IOCTL:
retmsg = PROBE_GENERIC; /* Generic M_IOCTL */
iocp = (struct iocblk *) &datamsg->buf[0];
ioctype = iocp->ioc_cmd;
iocdata = (unsigned char *) iocp + sizeof(struct iocblk);
switch (ioctype) {
case TCSETA:
case TCSETAW:
case TCSETAF:
tio = (struct termio *) iocdata;
if (tio->c_cflag & CLOCAL) {
retmsg = PROBE_CLOCAL; /* CLOCAL ON */
}
break;
case TCSETS:
case TCSETSW:
case TCSETSF:
tios = (struct termios *) iocdata;
if (tios->c_cflag & CLOCAL) {
retmsg = PROBE_CLOCAL; /* CLOCAL ON */
}
break;
default:
break;
}
}
if (Debug > 1) {
sysmessage(MSG_DEBUG, "PROBE: msg %d\n", retmsg);
}
return (retmsg);
}
int
dev_getdata(void)
{
int ctllen, datalen;
int retc;
int flags = 0;
struct strbuf *ctlmsg = &Message.ctlbuf;
struct strbuf *datamsg = &Message.databuf;
unsigned char type;
ctlmsg->buf = Ctlbuf;
ctlmsg->maxlen = 1;
datamsg->buf = Databuf;
datamsg->maxlen = Pty.iosize;
if ((retc = getmsg(P_mfd, ctlmsg, datamsg, &flags)) == -1) {
sysmessage(MSG_ERR,
"Can't get a message from master pty: %s\n",
strerror(errno));
# ifdef TSR_MEASURE
devnreads++;
# endif
return (retc);
}
ctllen = ctlmsg->len;
datalen = datamsg->len;
if (ctllen == -1) {
type = Lasttype;
}
else {
Lasttype = type = (unsigned char) ctlmsg->buf[0];
}
if (Debug > 2) {
sysmessage(MSG_DEBUG,
" DATA: Ctl: %d bytes, Type: %d, Data: %d bytes, flags: %08X\n",
ctllen, type, datalen, flags);
}
parse_message(type, datamsg->buf, datalen);
return (0);
}
int
dev_putdata(struct buffer *bp)
{
struct pty *pty = &Pty;
int ret;
int size;
int frombuf;
while (bp->b_hold) {
size = min(bp->b_hold, pty->iosize);
if ((ret = write(P_mfd, bp->b_rem, size)) == -1) {
if (errno == EAGAIN) {
ret = 0;
}
else {
sysmessage(MSG_ERR,
"Can't write on master pty: %s\n",
strerror(errno));
}
# ifdef TSR_MEASURE
devnwrites++;
# endif
return (ret);
}
# ifdef TSR_MEASURE
devwrites++;
devwbytes += ret;
# endif
FORWARD_BUFFER(bp, ret);
}
if (bp->b_hold == 0) {
RESET_BUFFER(bp);
}
return (0);
}
void
dev_interrupt(void)
{
if (ioctl(P_mfd, TIOCSIGNAL, SIGINT) == -1) {
sysmessage(MSG_ERR,
"Can't send SIGINT to slave device : %s\n",
strerror(errno));
}
}
void
dev_hangup(void)
{
if (ioctl(P_mfd, TIOCSIGNAL, SIGHUP) == -1) {
sysmessage(MSG_ERR,
"Can't send SIGHUP to slave device : %s\n",
strerror(errno));
}
}
/*
* Streams message routines
*/
void
parse_message(unsigned char type, char *buf, int size)
{
char *msgt;
int i;
struct iocblk *iocp;
struct buffer *bp = Pty.inbuff;
print_msg(type, (unsigned char *) buf, size);
if (size != 0) {
switch (Pty.state) {
case PTY_CLOSED:
case PTY_OPERRONLY:
SET_EVENT(EV_UP, EV_UPOPEN, 0, 0);
break;
}
}
switch (type) {
case M_DATA:
# ifdef TSR_MEASURE
devreads++;
devrbytes += size;
# endif
if (size == 0) {
SET_EVENT(EV_UP, EV_UPCLOSE, 0, 0);
}
else {
COPY_TO_BUFFER(bp, buf, size);
SET_EVENT(EV_UP, EV_UPDATA, 0, 0);
}
break;
case M_IOCTL:
iocp = (struct iocblk *) &buf[0];
parse_ioctl(iocp->ioc_cmd, (void *) &buf[sizeof(struct iocblk)]);
break;
case M_FLUSH:
parse_msgflush((int) buf[0]);
break;
default:
sysmessage(MSG_WARNING, "Unsupported stream message: %d\n", type);
print_msg(type, (unsigned char *) buf, size);
break;
}
}
void
parse_msgflush(int queues)
{
int mode;
switch (queues) {
case FLUSHR:
mode = OPFLUSH_IN;
break;
case FLUSHW:
mode = OPFLUSH_OUT;
break;
case FLUSHRW:
default:
mode = OPFLUSH_IO;
break;
}
SET_EVENT(EV_UP, EV_UPFLUSH, (void *) &mode, sizeof(int));
}
/*
* Termio / Termios routines
*/
void
parse_ioctl(int ioctype, void *iocdata)
{
switch (ioctype) {
case TCSETA:
parse_termio(ioctype, (struct termio *) iocdata);
break;
case TCSETAW:
parse_termio(ioctype, (struct termio *) iocdata);
break;
case TCSETAF:
parse_termio(ioctype, (struct termio *) iocdata);
break;
case TCSBRK:
parse_break((int *) iocdata);
break;
case TCFLSH:
parse_iocflush((int *) iocdata);
break;
case TCSETS:
parse_termios(ioctype, (struct termios *) iocdata);
break;
case TCSETSW:
parse_termios(ioctype, (struct termios *) iocdata);
break;
case TCSETSF:
parse_termios(ioctype, (struct termios *) iocdata);
break;
default:
sysmessage(MSG_WARNING, "Unsupported ioctl: %c %d\n",
(ioctype & IOCTYPE) >> 8, ioctype & 0xFF);
}
}
void
parse_termio(int mode, struct termio *tp)
{
struct iocontrol *iocp = &Pty.iocontrol;
struct portconfig *pcp = &iocp->io_portconfig;
switch (mode) {
case TCSETA:
iocp->io_oper = OP_SETNOW;
break;
case TCSETAW:
iocp->io_oper = OP_SETWAIT;
break;
case TCSETAF:
iocp->io_oper = OP_SETFLUSH;
break;
}
Pty.portmodes = termio_to_portmodes(tp);
memset((void *) pcp, 0, sizeof(struct portconfig));
termio_to_portconfig(tp, pcp);
SET_EVENT(EV_UP, EV_UPCONTROL, 0, 0);
}
void
parse_termios(int mode, struct termios *tp)
{
struct iocontrol *iocp = &Pty.iocontrol;
struct portconfig *pcp = &iocp->io_portconfig;
switch (mode) {
case TCSETS:
iocp->io_oper = OP_SETNOW;
break;
case TCSETSW:
iocp->io_oper = OP_SETWAIT;
break;
case TCSETSF:
iocp->io_oper = OP_SETFLUSH;
break;
}
Pty.portmodes = termios_to_portmodes(tp);
memset((void *) pcp, 0, sizeof(struct portconfig));
termios_to_portconfig(tp, pcp);
SET_EVENT(EV_UP, EV_UPCONTROL, 0, 0);
}
void
parse_break(int *interval)
{
struct iocontrol *iocp = &Pty.iocontrol;
iocp->io_oper = OP_SENDBREAK;
iocp->io_arg = *interval;
SET_EVENT(EV_UP, EV_UPCONTROL, 0, 0);
}
void
parse_iocflush(int *queues)
{
struct iocontrol *iocp = &Pty.iocontrol;
iocp->io_oper = OP_FLUSH;
switch (*queues) {
case TCIFLUSH:
iocp->io_arg = OPFLUSH_IN;
break;
case TCOFLUSH:
iocp->io_arg = OPFLUSH_OUT;
break;
case TCIOFLUSH:
iocp->io_arg = OPFLUSH_IO;
break;
}
SET_EVENT(EV_UP, EV_UPCONTROL, 0, 0);
}
int
termio_to_portmodes(struct termio *tp)
{
int portmodes;
if (tp->c_iflag & IGNBRK) {
portmodes |= PORT_IGNBRK;
}
else {
portmodes &= ~PORT_IGNBRK;
}
if (tp->c_iflag & BRKINT) {
portmodes |= PORT_BRKINT;
}
else {
portmodes &= ~PORT_BRKINT;
}
if (tp->c_iflag & IGNPAR) {
portmodes |= PORT_IGNPAR;
}
else {
portmodes &= ~PORT_IGNPAR;
}
if (tp->c_iflag & PARMRK) {
portmodes |= PORT_PARMRK;
}
else {
portmodes &= ~PORT_PARMRK;
}
if (tp->c_cflag & CLOCAL) {
portmodes |= PORT_CLOCAL;
}
else {
portmodes &= ~PORT_CLOCAL;
}
if (tp->c_cflag & HUPCL) {
portmodes |= PORT_HUPCL;
}
else {
portmodes &= ~PORT_HUPCL;
}
return (portmodes);
}
void
termio_to_portconfig(struct termio *tp, struct portconfig *pcp)
{
/* Speed */
pcp->speed = baud_index_to_int(tp->c_cflag & CBAUD);
if (pcp->speed == -1)
pcp->speed = 38400;
/* Datasize */
switch (tp->c_cflag & CSIZE) {
case CS5:
pcp->datasize = 5;
break;
case CS6:
pcp->datasize = 6;
break;
case CS7:
pcp->datasize = 7;
break;
case CS8:
default:
pcp->datasize = 8;
break;
}
/* Stopsize */
if (tp->c_cflag & CSTOPB) {
pcp->stopsize = COM_SSIZE_TWO;
}
else {
pcp->stopsize = COM_SSIZE_ONE;
}
/* Parity */
if (tp->c_cflag & PARENB) {
if (tp->c_cflag & PARODD) {
pcp->parity = COM_PARITY_ODD;
}
else {
pcp->parity = COM_PARITY_EVEN;
}
}
else {
pcp->parity = COM_PARITY_NONE;
}
/* Flow Control */
if (tp->c_iflag & IXON) {
pcp->flowc = COM_FLOW_SOFT;
}
else { /* Warning, assumes hardware flow control */
pcp->flowc = COM_FLOW_HARD;
}
}
int
termios_to_portmodes(struct termios *tp)
{
int portmodes;
if (tp->c_iflag & IGNBRK) {
portmodes |= PORT_IGNBRK;
}
else {
portmodes &= ~PORT_IGNBRK;
}
if (tp->c_iflag & BRKINT) {
portmodes |= PORT_BRKINT;
}
else {
portmodes &= ~PORT_BRKINT;
}
if (tp->c_iflag & IGNPAR) {
portmodes |= PORT_IGNPAR;
}
else {
portmodes &= ~PORT_IGNPAR;
}
if (tp->c_iflag & PARMRK) {
portmodes |= PORT_PARMRK;
}
else {
portmodes &= ~PORT_PARMRK;
}
if (tp->c_cflag & CLOCAL) {
portmodes |= PORT_CLOCAL;
}
else {
portmodes &= ~PORT_CLOCAL;
}
if (tp->c_cflag & HUPCL) {
portmodes |= PORT_HUPCL;
}
else {
portmodes &= ~PORT_HUPCL;
}
return (portmodes);
}
void
termios_to_portconfig(struct termios *tp, struct portconfig *pcp)
{
/* Speed */
speed_t speed = cfgetospeed(tp);
pcp->speed = baud_index_to_int(speed);
if (pcp->speed == -1)
pcp->speed = 38400;
/* Datasize */
switch (tp->c_cflag & CSIZE) {
case CS5:
pcp->datasize = 5;
break;
case CS6:
pcp->datasize = 6;
break;
case CS7:
pcp->datasize = 7;
break;
case CS8:
default:
pcp->datasize = 8;
break;
}
/* Stopsize */
if (tp->c_cflag & CSTOPB) {
pcp->stopsize = COM_SSIZE_TWO;
}
else {
pcp->stopsize = COM_SSIZE_ONE;
}
/* Parity */
if (tp->c_cflag & PARENB) {
if (tp->c_cflag & PARODD) {
pcp->parity = COM_PARITY_ODD;
}
else {
pcp->parity = COM_PARITY_EVEN;
}
}
else {
pcp->parity = COM_PARITY_NONE;
}
/* Flow Control */
if (tp->c_iflag & IXON) {
pcp->flowc = COM_FLOW_SOFT;
}
else { /* Warning, assumes hardware flow control */
pcp->flowc = COM_FLOW_HARD;
}
}
/* Termios must be clean */
void
portconfig_to_termios(struct portconfig *pcp, struct termios *tp)
{
/* Speed */
speed_t speed = int_to_baud_index(pcp->speed);
cfsetospeed(tp, (speed_t) speed);
cfsetispeed(tp, (speed_t) B0);
/* Datasize */
switch (pcp->datasize) {
case 5:
tp->c_cflag |= CS5;
break;
case 6:
tp->c_cflag |= CS6;
break;
case 7:
tp->c_cflag |= CS7;
break;
case 8:
tp->c_cflag |= CS8;
break;
}
/* Stopsize */
if (pcp->stopsize == COM_SSIZE_TWO) {
tp->c_cflag |= CSTOPB;
} /* else one stop bit */
/* Parity */
switch (pcp->parity) {
case COM_PARITY_EVEN:
tp->c_cflag |= PARENB;
break;
case COM_PARITY_ODD:
tp->c_cflag |= PARENB | PARODD;
break;
case COM_PARITY_NONE:
default:
break;
}
/* Flow Control */
switch (pcp->flowc) {
case COM_FLOW_SOFT:
tp->c_iflag |= IXON;
break;
default:
break;
}
}
/*