-
Notifications
You must be signed in to change notification settings - Fork 2
/
net_comx.c
1285 lines (1077 loc) · 28.8 KB
/
net_comx.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) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// net_comx.c
#include <dos.h>
#include <dpmi.h>
#define NUM_COM_PORTS 2
#define ERR_TTY_LINE_STATUS -1
#define ERR_TTY_MODEM_STATUS -2
#define ERR_TTY_NODATA -3
#define QUEUESIZE 8192
#define QUEUEMASK (QUEUESIZE - 1)
typedef struct
{
volatile int head;
volatile int tail;
volatile byte data[QUEUESIZE];
} queue;
#define FULL(q) (q.head == ((q.tail-1) & QUEUEMASK))
#define EMPTY(q) (q.tail == q.head)
#define ENQUEUE(q,b) (q.data[q.head] = b, q.head = (q.head + 1) & QUEUEMASK)
#define DEQUEUE(q,b) (b = q.data[q.tail], q.tail = (q.tail + 1) & QUEUEMASK)
extern cvar_t config_com_port;
extern cvar_t config_com_irq;
extern cvar_t config_com_baud;
extern cvar_t config_com_modem;
extern cvar_t config_modem_dialtype;
extern cvar_t config_modem_clear;
extern cvar_t config_modem_init;
extern cvar_t config_modem_hangup;
extern int m_return_state;
extern int m_state;
extern qboolean m_return_onerror;
extern char m_return_reason[32];
// 8250, 16550 definitions
#define TRANSMIT_HOLDING_REGISTER 0x00
#define RECEIVE_BUFFER_REGISTER 0x00
#define INTERRUPT_ENABLE_REGISTER 0x01
#define IER_RX_DATA_READY 0x01
#define IER_TX_HOLDING_REGISTER_EMPTY 0x02
#define IER_LINE_STATUS 0x04
#define IER_MODEM_STATUS 0x08
#define INTERRUPT_ID_REGISTER 0x02
#define IIR_MODEM_STATUS_INTERRUPT 0x00
#define IIR_TX_HOLDING_REGISTER_INTERRUPT 0x02
#define IIR_RX_DATA_READY_INTERRUPT 0x04
#define IIR_LINE_STATUS_INTERRUPT 0x06
#define IIR_FIFO_TIMEOUT 0x0c
#define IIR_FIFO_ENABLED 0xc0
#define FIFO_CONTROL_REGISTER 0x02
#define FCR_FIFO_ENABLE 0x01
#define FCR_RCVR_FIFO_RESET 0x02
#define FCR_XMIT_FIFO_RESET 0x04
#define FCR_TRIGGER_01 0x00
#define FCR_TRIGGER_04 0x40
#define FCR_TRIGGER_08 0x80
#define FCR_TRIGGER_16 0xc0
#define LINE_CONTROL_REGISTER 0x03
#define LCR_DATA_BITS_5 0x00
#define LCR_DATA_BITS_6 0x01
#define LCR_DATA_BITS_7 0x02
#define LCR_DATA_BITS_8 0x03
#define LCR_STOP_BITS_1 0x00
#define LCR_STOP_BITS_2 0x04
#define LCR_PARITY_NONE 0x00
#define LCR_PARITY_ODD 0x08
#define LCR_PARITY_EVEN 0x18
#define LCR_PARITY_MARK 0x28
#define LCR_PARITY_SPACE 0x38
#define LCR_SET_BREAK 0x40
#define LCR_DLAB 0x80
#define MODEM_CONTROL_REGISTER 0x04
#define MCR_DTR 0x01
#define MCR_RTS 0x02
#define MCR_OUT1 0x04
#define MCR_OUT2 0x08
#define MCR_LOOPBACK 0x10
#define LINE_STATUS_REGISTER 0x05
#define LSR_DATA_READY 0x01
#define LSR_OVERRUN_ERROR 0x02
#define LSR_PARITY_ERROR 0x04
#define LSR_FRAMING_ERROR 0x08
#define LSR_BREAK_DETECT 0x10
#define LSR_TRANSMITTER_BUFFER_EMPTY 0x20
#define LSR_TRANSMITTER_EMPTY 0x40
#define LSR_FIFO_DIRTY 0x80
#define MODEM_STATUS_REGISTER 0x06
#define MSR_DELTA_CTS 0x01
#define MSR_DELTA_DSR 0x02
#define MSR_DELTA_RI 0x04
#define MSR_DELTA_CD 0x08
#define MSR_CTS 0x10
#define MSR_DSR 0x20
#define MSR_RI 0x40
#define MSR_CD 0x80
#define DIVISOR_LATCH_LOW 0x00
#define DIVISOR_LATCH_HIGH 0x01
#define MODEM_STATUS_MASK (MSR_CTS | MSR_DSR | MSR_CD)
#define UART_AUTO 0
#define UART_8250 1
#define UART_16550 2
static int ISA_uarts[] = {0x3f8,0x2f8,0x3e8,0x2e8};
static int ISA_IRQs[] = {4,3,4,3};
typedef struct ComPort_s
{
struct ComPort_s *next;
_go32_dpmi_seginfo protectedModeInfo;
_go32_dpmi_seginfo protectedModeSaveInfo;
int uart;
volatile byte modemStatus;
byte modemStatusIgnore;
byte lineStatus;
byte bufferUsed;
qboolean enabled;
volatile qboolean statusUpdated;
qboolean useModem;
qboolean modemInitialized;
qboolean modemRang;
qboolean modemConnected;
queue inputQueue;
queue outputQueue;
char clear[16];
char startup[32];
char shutdown[16];
char buffer[128];
PollProcedure poll;
double timestamp;
byte uartType;
byte irq;
byte baudBits;
byte lineControl;
byte portNumber;
char dialType;
char name[4];
} ComPort;
ComPort *portList = NULL;
ComPort *handleToPort [NUM_COM_PORTS];
static int Modem_Command(ComPort *p, char *commandString);
static char *Modem_Response(ComPort *p);
static void Modem_Hangup(ComPort *p);
int TTY_Init(void);
void TTY_Shutdown(void);
int TTY_Open(int serialPortNumber);
void TTY_Close(int handle);
int TTY_ReadByte(int handle);
int TTY_WriteByte(int handle, byte data);
void TTY_Flush(int handle);
int TTY_Connect(int handle, char *host);
void TTY_Disconnect(int handle);
qboolean TTY_CheckForConnection(int handle);
qboolean TTY_IsEnabled(int serialPortNumber);
qboolean TTY_IsModem(int serialPortNumber);
qboolean TTY_OutputQueueIsEmpty(int handle);
static void ISR_8250 (ComPort *p)
{
byte source = 0;
byte b;
disable();
while((source = inportb (p->uart + INTERRUPT_ID_REGISTER) & 0x07) != 1)
{
switch (source)
{
case IIR_RX_DATA_READY_INTERRUPT:
b = inportb (p->uart + RECEIVE_BUFFER_REGISTER);
if (! FULL(p->inputQueue))
{
ENQUEUE (p->inputQueue, b);
}
else
{
p->lineStatus |= LSR_OVERRUN_ERROR;
p->statusUpdated = true;
}
break;
case IIR_TX_HOLDING_REGISTER_INTERRUPT:
if (! EMPTY(p->outputQueue))
{
DEQUEUE (p->outputQueue, b);
outportb (p->uart + TRANSMIT_HOLDING_REGISTER, b);
}
break;
case IIR_MODEM_STATUS_INTERRUPT:
p->modemStatus = (inportb (p->uart + MODEM_STATUS_REGISTER) & MODEM_STATUS_MASK) | p->modemStatusIgnore;
p->statusUpdated = true;
break;
case IIR_LINE_STATUS_INTERRUPT:
p->lineStatus = inportb (p->uart + LINE_STATUS_REGISTER);
p->statusUpdated = true;
break;
}
source = inportb (p->uart + INTERRUPT_ID_REGISTER) & 0x07;
}
outportb (0x20, 0x20);
}
static void COM1_ISR_8250 (void)
{
ISR_8250 (handleToPort[0]);
}
static void COM2_ISR_8250 (void)
{
ISR_8250 (handleToPort[1]);
}
static void ISR_16550 (ComPort *p)
{
int count;
byte source;
byte b;
disable();
while((source = inportb (p->uart + INTERRUPT_ID_REGISTER) & 0x07) != 1)
{
switch (source)
{
case IIR_RX_DATA_READY_INTERRUPT:
do
{
b = inportb (p->uart + RECEIVE_BUFFER_REGISTER);
if (!FULL(p->inputQueue))
{
ENQUEUE (p->inputQueue, b);
}
else
{
p->lineStatus |= LSR_OVERRUN_ERROR;
p->statusUpdated = true;
}
} while (inportb (p->uart + LINE_STATUS_REGISTER) & LSR_DATA_READY);
break;
case IIR_TX_HOLDING_REGISTER_INTERRUPT:
count = 16;
while ((! EMPTY(p->outputQueue)) && count--)
{
DEQUEUE (p->outputQueue, b);
outportb (p->uart + TRANSMIT_HOLDING_REGISTER, b);
}
break;
case IIR_MODEM_STATUS_INTERRUPT:
p->modemStatus = (inportb (p->uart + MODEM_STATUS_REGISTER) & MODEM_STATUS_MASK) | p->modemStatusIgnore;
p->statusUpdated = true;
break;
case IIR_LINE_STATUS_INTERRUPT:
p->lineStatus = inportb (p->uart + LINE_STATUS_REGISTER);
p->statusUpdated = true;
break;
}
source = inportb (p->uart + INTERRUPT_ID_REGISTER) & 0x07;
}
// check for lost IIR_TX_HOLDING_REGISTER_INTERRUPT on 16550a!
if (inportb (p->uart + LINE_STATUS_REGISTER ) & LSR_TRANSMITTER_EMPTY)
{
count = 16;
while ((! EMPTY(p->outputQueue)) && count--)
{
DEQUEUE (p->outputQueue, b);
outportb (p->uart + TRANSMIT_HOLDING_REGISTER, b);
}
}
outportb (0x20, 0x20);
}
static void COM1_ISR_16550 (void)
{
ISR_16550 (handleToPort[0]);
}
static void COM2_ISR_16550 (void)
{
ISR_16550 (handleToPort[1]);
}
void TTY_GetComPortConfig (int portNumber, int *port, int *irq, int *baud, qboolean *useModem)
{
ComPort *p;
p = handleToPort[portNumber];
*port = p->uart;
*irq = p->irq;
*baud = 115200 / p->baudBits;
*useModem = p->useModem;
}
void TTY_SetComPortConfig (int portNumber, int port, int irq, int baud, qboolean useModem)
{
ComPort *p;
float temp;
if (useModem)
{
if (baud == 14400)
baud = 19200;
if (baud == 28800)
baud = 38400;
}
p = handleToPort[portNumber];
p->uart = port;
p->irq = irq;
p->baudBits = 115200 / baud;
p->useModem = useModem;
if (useModem)
temp = 1.0;
else
temp = 0.0;
Cvar_SetValue ("_config_com_port", (float)port);
Cvar_SetValue ("_config_com_irq", (float)irq);
Cvar_SetValue ("_config_com_baud", (float)baud);
Cvar_SetValue ("_config_com_modem", temp);
}
void TTY_GetModemConfig (int portNumber, char *dialType, char *clear, char *init, char *hangup)
{
ComPort *p;
p = handleToPort[portNumber];
*dialType = p->dialType;
Q_strcpy(clear, p->clear);
Q_strcpy(init, p->startup);
Q_strcpy(hangup, p->shutdown);
}
void TTY_SetModemConfig (int portNumber, char *dialType, char *clear, char *init, char *hangup)
{
ComPort *p;
p = handleToPort[portNumber];
p->dialType = dialType[0];
Q_strcpy(p->clear, clear);
Q_strcpy(p->startup, init);
Q_strcpy(p->shutdown, hangup);
p->modemInitialized = false;
Cvar_Set ("_config_modem_dialtype", dialType);
Cvar_Set ("_config_modem_clear", clear);
Cvar_Set ("_config_modem_init", init);
Cvar_Set ("_config_modem_hangup", hangup);
}
static void ResetComPortConfig (ComPort *p)
{
p->useModem = false;
p->uartType = UART_AUTO;
p->uart = ISA_uarts[p->portNumber];
p->irq = ISA_IRQs[p->portNumber];
p->modemStatusIgnore = MSR_CD | MSR_CTS | MSR_DSR;
p->baudBits = 115200 / 57600;
p->lineControl = LCR_DATA_BITS_8 | LCR_STOP_BITS_1 | LCR_PARITY_NONE;
Q_strcpy(p->clear, "ATZ");
Q_strcpy(p->startup, "");
Q_strcpy(p->shutdown, "AT H");
p->modemRang = false;
p->modemConnected = false;
p->statusUpdated = false;
p->outputQueue.head = p->outputQueue.tail = 0;
p->inputQueue.head = p->inputQueue.tail = 0;
}
static void ComPort_Enable(ComPort *p)
{
void (*isr)(void);
int n;
byte b;
if (p->enabled)
{
Con_Printf("Already enabled\n");
return;
}
// disable all UART interrupts
outportb (p->uart + INTERRUPT_ENABLE_REGISTER, 0);
// clear out any buffered uncoming data
while((inportb (p->uart + LINE_STATUS_REGISTER)) & LSR_DATA_READY)
inportb (p->uart + RECEIVE_BUFFER_REGISTER);
// get the current line and modem status
p->modemStatus = (inportb (p->uart + MODEM_STATUS_REGISTER) & MODEM_STATUS_MASK) | p->modemStatusIgnore;
p->lineStatus = inportb (p->uart + LINE_STATUS_REGISTER);
// clear any UART interrupts
do
{
n = inportb (p->uart + INTERRUPT_ID_REGISTER) & 7;
if (n == IIR_RX_DATA_READY_INTERRUPT)
inportb (p->uart + RECEIVE_BUFFER_REGISTER);
} while (!(n & 1));
if (p->uartType == UART_AUTO)
{
outportb (p->uart + FIFO_CONTROL_REGISTER, FCR_FIFO_ENABLE);
b = inportb (p->uart + INTERRUPT_ID_REGISTER);
if ((b & IIR_FIFO_ENABLED) == IIR_FIFO_ENABLED)
p->uartType = UART_16550;
else
p->uartType = UART_8250;
}
// save the old interrupt handler
_go32_dpmi_get_protected_mode_interrupt_vector(p->irq + 8, &p->protectedModeSaveInfo);
if (p->uartType == UART_8250)
{
outportb (p->uart + FIFO_CONTROL_REGISTER, 0);
if (p == handleToPort[0])
isr = COM1_ISR_8250;
else
isr = COM2_ISR_8250;
}
else
{
outportb (p->uart + FIFO_CONTROL_REGISTER, FCR_FIFO_ENABLE | FCR_RCVR_FIFO_RESET | FCR_XMIT_FIFO_RESET | FCR_TRIGGER_08);
if (p == handleToPort[0])
isr = COM1_ISR_16550;
else
isr = COM2_ISR_16550;
}
p->protectedModeInfo.pm_offset = (int)isr;
n = _go32_dpmi_allocate_iret_wrapper(&p->protectedModeInfo);
if (n)
{
Con_Printf("serial: protected mode callback allocation failed\n");
return;
}
// disable interrupts at the processor
disable();
// install our interrupt handlers now
_go32_dpmi_set_protected_mode_interrupt_vector(p->irq + 8, &p->protectedModeInfo);
// enable our interrupt at the PIC
outportb (0x21, inportb (0x21) & ~(1<<p->irq));
// enable interrupts at the processor
enable();
// enable interrupts at the PIC
outportb (0x20, 0xc2);
// set baud rate & line control
outportb (p->uart + LINE_CONTROL_REGISTER, LCR_DLAB | p->lineControl);
outportb (p->uart, p->baudBits);
outportb (p->uart + 1, 0);
outportb (p->uart + LINE_CONTROL_REGISTER, p->lineControl);
// set modem control register & enable uart interrupt generation
outportb(p->uart + MODEM_CONTROL_REGISTER, MCR_OUT2 | MCR_RTS | MCR_DTR);
// enable the individual interrupts at the uart
outportb (p->uart + INTERRUPT_ENABLE_REGISTER, IER_RX_DATA_READY | IER_TX_HOLDING_REGISTER_EMPTY | IER_LINE_STATUS | IER_MODEM_STATUS);
p->enabled = true;
}
static void ComPort_Disable(ComPort *p)
{
if (!p->enabled)
{
Con_Printf("Already disabled\n");
return;
}
// disable interrupts at the uart
outportb (p->uart + INTERRUPT_ENABLE_REGISTER, 0);
// disable our interrupt at the PIC
outportb (0x21, inportb (0x21) | (1<<p->irq));
// disable interrupts at the processor
disable();
// restore the old interrupt handler
_go32_dpmi_set_protected_mode_interrupt_vector(p->irq + 8, &p->protectedModeSaveInfo);
_go32_dpmi_free_iret_wrapper(&p->protectedModeInfo);
// enable interrupts at the processor
enable();
p->enabled = false;
}
static int CheckStatus (ComPort *p)
{
int ret = 0;
if (p->statusUpdated)
{
p->statusUpdated = false;
if (p->lineStatus & (LSR_OVERRUN_ERROR | LSR_PARITY_ERROR | LSR_FRAMING_ERROR | LSR_BREAK_DETECT))
{
if (p->lineStatus & LSR_OVERRUN_ERROR)
Con_DPrintf ("Serial overrun error\n");
if (p->lineStatus & LSR_PARITY_ERROR)
Con_DPrintf ("Serial parity error\n");
if (p->lineStatus & LSR_FRAMING_ERROR)
Con_DPrintf ("Serial framing error\n");
if (p->lineStatus & LSR_BREAK_DETECT)
Con_DPrintf ("Serial break detect\n");
ret = ERR_TTY_LINE_STATUS;
}
if ((p->modemStatus & MODEM_STATUS_MASK) != MODEM_STATUS_MASK)
{
if (!(p->modemStatus & MSR_CTS))
Con_Printf ("Serial lost CTS\n");
if (!(p->modemStatus & MSR_DSR))
Con_Printf ("Serial lost DSR\n");
if (!(p->modemStatus & MSR_CD))
Con_Printf ("Serial lost Carrier\n");
ret = ERR_TTY_MODEM_STATUS;
}
}
return ret;
}
static void Modem_Init(ComPort *p)
{
double start;
char *response;
Con_Printf ("Initializing modem...\n");
// write 0 to MCR, wait 1/2 sec, then write the real value back again
// I got this from the guys at head-to-head who say it's necessary.
outportb(p->uart + MODEM_CONTROL_REGISTER, 0);
start = Sys_FloatTime();
while ((Sys_FloatTime() - start) < 0.5)
;
outportb(p->uart + MODEM_CONTROL_REGISTER, MCR_OUT2 | MCR_RTS | MCR_DTR);
start = Sys_FloatTime();
while ((Sys_FloatTime() - start) < 0.25)
;
if (*p->clear)
{
Modem_Command (p, p->clear);
start = Sys_FloatTime();
while(1)
{
if ((Sys_FloatTime() - start) > 3.0)
{
Con_Printf("No response - clear failed\n");
p->enabled = false;
goto failed;
}
response = Modem_Response(p);
if (!response)
continue;
if (Q_strncmp(response, "OK", 2) == 0)
break;
if (Q_strncmp(response, "ERROR", 5) == 0)
{
p->enabled = false;
goto failed;
}
}
}
if (*p->startup)
{
Modem_Command (p, p->startup);
start = Sys_FloatTime();
while(1)
{
if ((Sys_FloatTime() - start) > 3.0)
{
Con_Printf("No response - init failed\n");
p->enabled = false;
goto failed;
}
response = Modem_Response(p);
if (!response)
continue;
if (Q_strncmp(response, "OK", 2) == 0)
break;
if (Q_strncmp(response, "ERROR", 5) == 0)
{
p->enabled = false;
goto failed;
}
}
}
p->modemInitialized = true;
return;
failed:
if (m_return_onerror)
{
key_dest = key_menu;
m_state = m_return_state;
m_return_onerror = false;
Q_strcpy(m_return_reason, "Initialization Failed");
}
return;
}
void TTY_Enable(int handle)
{
ComPort *p;
p = handleToPort [handle];
if (p->enabled)
return;
ComPort_Enable(p);
if (p->useModem && !p->modemInitialized)
Modem_Init (p);
}
int TTY_Open(int serialPortNumber)
{
return serialPortNumber;
}
void TTY_Close(int handle)
{
ComPort *p;
double startTime;
p = handleToPort [handle];
startTime = Sys_FloatTime();
while ((Sys_FloatTime() - startTime) < 1.0)
if (EMPTY(p->outputQueue))
break;
if (p->useModem)
{
if (p->modemConnected)
Modem_Hangup(p);
}
}
int TTY_ReadByte(int handle)
{
int ret;
ComPort *p;
p = handleToPort [handle];
if ((ret = CheckStatus (p)) != 0)
return ret;
if (EMPTY (p->inputQueue))
return ERR_TTY_NODATA;
DEQUEUE (p->inputQueue, ret);
return (ret & 0xff);
}
int TTY_WriteByte(int handle, byte data)
{
ComPort *p;
p = handleToPort [handle];
if (FULL(p->outputQueue))
return -1;
ENQUEUE (p->outputQueue, data);
return 0;
}
void TTY_Flush(int handle)
{
byte b;
ComPort *p;
p = handleToPort [handle];
if (inportb (p->uart + LINE_STATUS_REGISTER ) & LSR_TRANSMITTER_EMPTY)
{
DEQUEUE (p->outputQueue, b);
outportb(p->uart, b);
}
}
int TTY_Connect(int handle, char *host)
{
double start;
ComPort *p;
char *response = NULL;
keydest_t save_key_dest;
byte dialstring[64];
byte b;
p = handleToPort[handle];
if ((p->modemStatus & MODEM_STATUS_MASK) != MODEM_STATUS_MASK)
{
Con_Printf ("Serial: line not ready (");
if ((p->modemStatus & MSR_CTS) == 0)
Con_Printf(" CTS");
if ((p->modemStatus & MSR_DSR) == 0)
Con_Printf(" DSR");
if ((p->modemStatus & MSR_CD) == 0)
Con_Printf(" CD");
Con_Printf(" )");
return -1;
}
// discard any scraps in the input buffer
while (! EMPTY (p->inputQueue))
DEQUEUE (p->inputQueue, b);
CheckStatus (p);
if (p->useModem)
{
save_key_dest = key_dest;
key_dest = key_console;
key_count = -2;
Con_Printf ("Dialing...\n");
sprintf(dialstring, "AT D%c %s\r", p->dialType, host);
Modem_Command (p, dialstring);
start = Sys_FloatTime();
while(1)
{
if ((Sys_FloatTime() - start) > 60.0)
{
Con_Printf("Dialing failure!\n");
break;
}
Sys_SendKeyEvents ();
if (key_count == 0)
{
if (key_lastpress != K_ESCAPE)
{
key_count = -2;
continue;
}
Con_Printf("Aborting...\n");
while ((Sys_FloatTime() - start) < 5.0)
;
disable();
p->outputQueue.head = p->outputQueue.tail = 0;
p->inputQueue.head = p->inputQueue.tail = 0;
outportb(p->uart + MODEM_CONTROL_REGISTER, inportb(p->uart + MODEM_CONTROL_REGISTER) & ~MCR_DTR);
enable();
start = Sys_FloatTime();
while ((Sys_FloatTime() - start) < 0.75)
;
outportb(p->uart + MODEM_CONTROL_REGISTER, inportb(p->uart + MODEM_CONTROL_REGISTER) | MCR_DTR);
response = "Aborted";
break;
}
response = Modem_Response(p);
if (!response)
continue;
if (Q_strncmp(response, "CONNECT", 7) == 0)
{
disable();
p->modemRang = true;
p->modemConnected = true;
p->outputQueue.head = p->outputQueue.tail = 0;
p->inputQueue.head = p->inputQueue.tail = 0;
enable();
key_dest = save_key_dest;
key_count = 0;
m_return_onerror = false;
return 0;
}
if (Q_strncmp(response, "NO CARRIER", 10) == 0)
break;
if (Q_strncmp(response, "NO DIALTONE", 11) == 0)
break;
if (Q_strncmp(response, "NO DIAL TONE", 12) == 0)
break;
if (Q_strncmp(response, "NO ANSWER", 9) == 0)
break;
if (Q_strncmp(response, "BUSY", 4) == 0)
break;
if (Q_strncmp(response, "ERROR", 5) == 0)
break;
}
key_dest = save_key_dest;
key_count = 0;
if (m_return_onerror)
{
key_dest = key_menu;
m_state = m_return_state;
m_return_onerror = false;
Q_strncpy(m_return_reason, response, 31);
}
return -1;
}
m_return_onerror = false;
return 0;
}
void TTY_Disconnect(int handle)
{
ComPort *p;
p = handleToPort[handle];
if (p->useModem && p->modemConnected)
Modem_Hangup(p);
}
qboolean TTY_CheckForConnection(int handle)
{
ComPort *p;
p = handleToPort[handle];
CheckStatus (p);
if (p->useModem)
{
if (!p->modemRang)
{
if (!Modem_Response(p))
return false;
if (Q_strncmp(p->buffer, "RING", 4) == 0)
{
Modem_Command (p, "ATA");
p->modemRang = true;
p->timestamp = net_time;
}
return false;
}
if (!p->modemConnected)
{
if ((net_time - p->timestamp) > 35.0)
{
Con_Printf("Unable to establish modem connection\n");
p->modemRang = false;
return false;
}
if (!Modem_Response(p))
return false;
if (Q_strncmp (p->buffer, "CONNECT", 7) != 0)
return false;
disable();
p->modemConnected = true;
p->outputQueue.head = p->outputQueue.tail = 0;
p->inputQueue.head = p->inputQueue.tail = 0;
enable();
Con_Printf("Modem Connect\n");
return true;
}
return true;
}
// direct connect case
if (EMPTY (p->inputQueue))
return false;
return true;
}
qboolean TTY_IsEnabled(int serialPortNumber)
{
return handleToPort[serialPortNumber]->enabled;
}
qboolean TTY_IsModem(int serialPortNumber)
{
return handleToPort[serialPortNumber]->useModem;
}
qboolean TTY_OutputQueueIsEmpty(int handle)
{
return EMPTY(handleToPort[handle]->outputQueue);
}
void Com_f (void)
{
ComPort *p;
int portNumber;
int i;
int n;
// first, determine which port they're messing with
portNumber = Q_atoi(Cmd_Argv (0) + 3) - 1;
if (portNumber > 1)
return;
p = handleToPort[portNumber];
if (Cmd_Argc() == 1)
{
Con_Printf("Settings for COM%i\n", portNumber + 1);
Con_Printf("enabled: %s\n", p->enabled ? "true" : "false");
Con_Printf("uart: ");
if (p->uartType == UART_AUTO)
Con_Printf("auto\n");
else if (p->uartType == UART_8250)
Con_Printf("8250\n");
else
Con_Printf("16550\n");
Con_Printf("port: %x\n", p->uart);
Con_Printf("irq: %i\n", p->irq);
Con_Printf("baud: %i\n", 115200 / p->baudBits);
Con_Printf("CTS: %s\n", (p->modemStatusIgnore & MSR_CTS) ? "ignored" : "honored");
Con_Printf("DSR: %s\n", (p->modemStatusIgnore & MSR_DSR) ? "ignored" : "honored");
Con_Printf("CD: %s\n", (p->modemStatusIgnore & MSR_CD) ? "ignored" : "honored");
if (p->useModem)
{
Con_Printf("type: Modem\n");
Con_Printf("clear: %s\n", p->clear);
Con_Printf("startup: %s\n", p->startup);
Con_Printf("shutdown: %s\n", p->shutdown);
}
else
Con_Printf("type: Direct connect\n");
return;
}
if (Cmd_CheckParm ("disable"))
{
if (p->enabled)
ComPort_Disable(p);
p->modemInitialized = false;
return;
}