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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
|
/*
* Mach Operating System
* Copyright (c) 1993-1988 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/*
* Author: David B. Golub, Carnegie Mellon University
* Date: 8/88
*
* TTY io.
* Compatibility with old TTY device drivers.
*/
#include <mach/kern_return.h>
#include <mach/mig_errors.h>
#include <mach/vm_param.h>
#include <machine/machspl.h> /* spl definitions */
#include <ipc/ipc_port.h>
#include <kern/lock.h>
#include <kern/queue.h>
#include <vm/vm_map.h>
#include <vm/vm_kern.h>
#include <device/device_types.h>
#include <device/io_req.h>
#include <device/ds_routines.h>
#include <device/device_reply.user.h>
#include <device/tty.h>
/* If you change these, check that tty_outq_size and tty_inq_size
* is greater than largest tthiwat entry.
*/
short tthiwat[NSPEEDS] =
{ 100,100,100,100,100,100,100,200,200,400,400,400,650,650,1300,2000,
2000,2000 };
short ttlowat[NSPEEDS] =
{ 30, 30, 30, 30, 30, 30, 30, 50, 50,120,120,120,125,125, 125, 125,
125,125 };
/*
* forward declarations
*/
void queue_delayed_reply(
queue_t, io_req_t, boolean_t (*)(io_req_t));
void tty_output(struct tty *);
void tty_flush(struct tty *, int);
boolean_t char_open_done(io_req_t);
boolean_t char_read_done(io_req_t);
boolean_t char_write_done(io_req_t);
void ttstart(struct tty *tp);
/*
* Fake 'line discipline' switch for the benefit of old code
* that wants to call through it.
*/
struct ldisc_switch linesw[] = {
{
char_read,
char_write,
ttyinput,
ttymodem,
tty_output
}
};
/*
* Sizes for input and output circular buffers.
*/
int tty_inq_size = 4096; /* big nuf */
int tty_outq_size = 2048; /* Must be bigger that tthiwat */
int pdma_default = 1; /* turn pseudo dma on by default */
/*
* compute pseudo-dma tables
*/
int pdma_timeouts[NSPEEDS]; /* how many ticks in timeout */
int pdma_water_mark[NSPEEDS];
void chario_init(void)
{
/* the basic idea with the timeouts is two allow enough
time for a character to show up if data is coming in at full data rate
plus a little slack. 2 ticks is considered slack
Below 300 baud we just glob a character at a time */
#define _PR(x) ((hz/x) + 2)
int i;
for (i = B0; i < B300; i++)
pdma_timeouts[i] = 0;
pdma_timeouts[B300] = _PR(30);
pdma_timeouts[B600] = _PR(60);
pdma_timeouts[B1200] = _PR(120);
pdma_timeouts[B1800] = _PR(180);
pdma_timeouts[B2400] = _PR(240);
pdma_timeouts[B4800] = _PR(480);
pdma_timeouts[B9600] = _PR(960);
pdma_timeouts[EXTA] = _PR(1440); /* >14400 baud */
pdma_timeouts[EXTB] = _PR(1920); /* >19200 baud */
pdma_timeouts[B57600] = _PR(5760);
pdma_timeouts[B115200] = _PR(11520);
for (i = B0; i < B300; i++)
pdma_water_mark[i] = 0;
/* for the slow speeds, we try to buffer 0.02 of the baud rate
(20% of the character rate). For the faster lines,
we try to buffer 1/2 the input queue size */
#undef _PR
#define _PR(x) (0.20 * x)
pdma_water_mark[B300] = _PR(120);
pdma_water_mark[B600] = _PR(120);
pdma_water_mark[B1200] = _PR(120);
pdma_water_mark[B1800] = _PR(180);
pdma_water_mark[B2400] = _PR(240);
pdma_water_mark[B4800] = _PR(480);
i = tty_inq_size/2;
pdma_water_mark[B9600] = i;
pdma_water_mark[EXTA] = i; /* >14400 baud */
pdma_water_mark[EXTB] = i; /* >19200 baud */
pdma_water_mark[B57600] = i;
pdma_water_mark[B115200] = i;
return;
}
/*
* Open TTY, waiting for CARR_ON.
* No locks may be held.
* May run on any CPU.
*/
io_return_t char_open(
int dev,
struct tty * tp,
dev_mode_t mode,
io_req_t ior)
{
spl_t s;
io_return_t rc = D_SUCCESS;
s = spltty();
simple_lock(&tp->t_lock);
tp->t_dev = dev;
if (tp->t_mctl)
(*tp->t_mctl)(tp, TM_DTR, DMSET);
if (pdma_default)
tp->t_state |= TS_MIN;
if ((tp->t_state & TS_CARR_ON) == 0) {
/*
* No carrier.
*/
if (mode & D_NODELAY) {
tp->t_state |= TS_ONDELAY;
}
else {
/*
* Don`t return from open until carrier detected.
*/
tp->t_state |= TS_WOPEN;
ior->io_dev_ptr = (char *)tp;
queue_delayed_reply(&tp->t_delayed_open, ior, char_open_done);
rc = D_IO_QUEUED;
goto out;
}
}
tp->t_state |= TS_ISOPEN;
if (tp->t_mctl)
(*tp->t_mctl)(tp, TM_RTS, DMBIS);
out:
simple_unlock(&tp->t_lock);
splx(s);
return rc;
}
/*
* Retry wait for CARR_ON for open.
* No locks may be held.
* May run on any CPU.
*/
boolean_t char_open_done(
io_req_t ior)
{
register struct tty *tp = (struct tty *)ior->io_dev_ptr;
spl_t s = spltty();
simple_lock(&tp->t_lock);
if ((tp->t_state & TS_ISOPEN) == 0) {
queue_delayed_reply(&tp->t_delayed_open, ior, char_open_done);
simple_unlock(&tp->t_lock);
splx(s);
return FALSE;
}
tp->t_state |= TS_ISOPEN;
tp->t_state &= ~TS_WOPEN;
if (tp->t_mctl)
(*tp->t_mctl)(tp, TM_RTS, DMBIS);
simple_unlock(&tp->t_lock);
splx(s);
ior->io_error = D_SUCCESS;
(void) ds_open_done(ior);
return TRUE;
}
boolean_t tty_close_open_reply(
io_req_t ior)
{
ior->io_error = D_DEVICE_DOWN;
(void) ds_open_done(ior);
return TRUE;
}
/*
* Write to TTY.
* No locks may be held.
* Calls device start routine; must already be on master if
* device needs to run on master.
*/
io_return_t char_write(
register struct tty * tp,
register io_req_t ior)
{
spl_t s;
register int count;
register char *data;
vm_offset_t addr;
io_return_t rc = D_SUCCESS;
data = ior->io_data;
count = ior->io_count;
if (count == 0)
return rc;
if (!(ior->io_op & IO_INBAND)) {
/*
* Copy out-of-line data into kernel address space.
* Since data is copied as page list, it will be
* accessible.
*/
vm_map_copy_t copy = (vm_map_copy_t) data;
kern_return_t kr;
kr = vm_map_copyout(device_io_map, &addr, copy);
if (kr != KERN_SUCCESS)
return kr;
data = (char *) addr;
}
/*
* Check for tty operating.
*/
s = spltty();
simple_lock(&tp->t_lock);
if ((tp->t_state & TS_CARR_ON) == 0) {
if ((tp->t_state & TS_ONDELAY) == 0) {
/*
* No delayed writes - tell caller that device is down
*/
rc = D_IO_ERROR;
goto out;
}
if (ior->io_mode & D_NOWAIT) {
rc = D_WOULD_BLOCK;
goto out;
}
}
/*
* Copy data into the output buffer.
* Report the amount not copied.
*/
ior->io_residual = b_to_q(data, count, &tp->t_outq);
/*
* Start hardware output.
*/
tp->t_state &= ~TS_TTSTOP;
tty_output(tp);
if (tp->t_outq.c_cc > TTHIWAT(tp) ||
(tp->t_state & TS_CARR_ON) == 0) {
/*
* Do not send reply until some characters have been sent.
*/
ior->io_dev_ptr = (char *)tp;
queue_delayed_reply(&tp->t_delayed_write, ior, char_write_done);
rc = D_IO_QUEUED;
}
out:
simple_unlock(&tp->t_lock);
splx(s);
if (!(ior->io_op & IO_INBAND))
(void) vm_deallocate(device_io_map, addr, ior->io_count);
return rc;
}
/*
* Retry wait for output queue emptied, for write.
* No locks may be held.
* May run on any CPU.
*/
boolean_t char_write_done(
register io_req_t ior)
{
register struct tty *tp = (struct tty *)ior->io_dev_ptr;
register spl_t s = spltty();
simple_lock(&tp->t_lock);
if (tp->t_outq.c_cc > TTHIWAT(tp) ||
(tp->t_state & TS_CARR_ON) == 0) {
queue_delayed_reply(&tp->t_delayed_write, ior, char_write_done);
simple_unlock(&tp->t_lock);
splx(s);
return FALSE;
}
simple_unlock(&tp->t_lock);
splx(s);
if (IP_VALID(ior->io_reply_port)) {
(void) (*((ior->io_op & IO_INBAND) ?
ds_device_write_reply_inband :
ds_device_write_reply))(ior->io_reply_port,
ior->io_reply_port_type,
ior->io_error,
(int) (ior->io_total -
ior->io_residual));
}
mach_device_deallocate(ior->io_device);
return TRUE;
}
boolean_t tty_close_write_reply(
register io_req_t ior)
{
ior->io_residual = ior->io_count;
ior->io_error = D_DEVICE_DOWN;
(void) ds_write_done(ior);
return TRUE;
}
/*
* Read from TTY.
* No locks may be held.
* May run on any CPU - does not talk to device driver.
*/
io_return_t char_read(
register struct tty *tp,
register io_req_t ior)
{
spl_t s;
kern_return_t rc;
/*
* Allocate memory for read buffer.
*/
rc = device_read_alloc(ior, (vm_size_t)ior->io_count);
if (rc != KERN_SUCCESS)
return rc;
s = spltty();
simple_lock(&tp->t_lock);
if ((tp->t_state & TS_CARR_ON) == 0) {
if ((tp->t_state & TS_ONDELAY) == 0) {
/*
* No delayed writes - tell caller that device is down
*/
rc = D_IO_ERROR;
goto out;
}
if (ior->io_mode & D_NOWAIT) {
rc = D_WOULD_BLOCK;
goto out;
}
}
if (tp->t_inq.c_cc <= 0 ||
(tp->t_state & TS_CARR_ON) == 0) {
ior->io_dev_ptr = (char *)tp;
queue_delayed_reply(&tp->t_delayed_read, ior, char_read_done);
rc = D_IO_QUEUED;
goto out;
}
ior->io_residual = ior->io_count - q_to_b(&tp->t_inq,
ior->io_data,
(int)ior->io_count);
if (tp->t_state & TS_RTS_DOWN) {
(*tp->t_mctl)(tp, TM_RTS, DMBIS);
tp->t_state &= ~TS_RTS_DOWN;
}
out:
simple_unlock(&tp->t_lock);
splx(s);
return rc;
}
/*
* Retry wait for characters, for read.
* No locks may be held.
* May run on any CPU - does not talk to device driver.
*/
boolean_t char_read_done(
register io_req_t ior)
{
register struct tty *tp = (struct tty *)ior->io_dev_ptr;
register spl_t s = spltty();
simple_lock(&tp->t_lock);
if (tp->t_inq.c_cc <= 0 ||
(tp->t_state & TS_CARR_ON) == 0) {
queue_delayed_reply(&tp->t_delayed_read, ior, char_read_done);
simple_unlock(&tp->t_lock);
splx(s);
return FALSE;
}
ior->io_residual = ior->io_count - q_to_b(&tp->t_inq,
ior->io_data,
(int)ior->io_count);
if (tp->t_state & TS_RTS_DOWN) {
(*tp->t_mctl)(tp, TM_RTS, DMBIS);
tp->t_state &= ~TS_RTS_DOWN;
}
simple_unlock(&tp->t_lock);
splx(s);
(void) ds_read_done(ior);
return TRUE;
}
boolean_t tty_close_read_reply(
register io_req_t ior)
{
ior->io_residual = ior->io_count;
ior->io_error = D_DEVICE_DOWN;
(void) ds_read_done(ior);
return TRUE;
}
/*
* Close the tty.
* Tty must be locked (at spltty).
* Iff modem control should run on master.
*/
void ttyclose(
register struct tty *tp)
{
register io_req_t ior;
/*
* Flush the read and write queues. Signal
* the open queue so that those waiting for open
* to complete will see that the tty is closed.
*/
while ((ior = (io_req_t)dequeue_head(&tp->t_delayed_read)) != 0) {
ior->io_done = tty_close_read_reply;
iodone(ior);
}
while ((ior = (io_req_t)dequeue_head(&tp->t_delayed_write)) != 0) {
ior->io_done = tty_close_write_reply;
iodone(ior);
}
while ((ior = (io_req_t)dequeue_head(&tp->t_delayed_open)) != 0) {
ior->io_done = tty_close_open_reply;
iodone(ior);
}
/* Close down modem */
if (tp->t_mctl) {
(*tp->t_mctl)(tp, TM_BRK|TM_RTS, DMBIC);
if ((tp->t_state&(TS_HUPCLS|TS_WOPEN)) || (tp->t_state&TS_ISOPEN)==0)
(*tp->t_mctl)(tp, TM_HUP, DMSET);
}
/* only save buffering bit, and carrier */
tp->t_state = tp->t_state & (TS_MIN|TS_CARR_ON);
}
/*
* Port-death routine to clean up reply messages.
*/
boolean_t
tty_queue_clean(
queue_t q,
ipc_port_t port,
boolean_t (*routine)(io_req_t) )
{
register io_req_t ior;
ior = (io_req_t)queue_first(q);
while (!queue_end(q, (queue_entry_t)ior)) {
if (ior->io_reply_port == port) {
remqueue(q, (queue_entry_t)ior);
ior->io_done = routine;
iodone(ior);
return TRUE;
}
ior = ior->io_next;
}
return FALSE;
}
/*
* Handle port-death (dead reply port) for tty.
* No locks may be held.
* May run on any CPU.
*/
boolean_t
tty_portdeath(
struct tty * tp,
ipc_port_t port)
{
register spl_t spl = spltty();
register boolean_t result;
simple_lock(&tp->t_lock);
/*
* The queues may never have been initialized
*/
if (tp->t_delayed_read.next == 0) {
result = FALSE;
}
else {
result =
tty_queue_clean(&tp->t_delayed_read, port,
tty_close_read_reply)
|| tty_queue_clean(&tp->t_delayed_write, port,
tty_close_write_reply)
|| tty_queue_clean(&tp->t_delayed_open, port,
tty_close_open_reply);
}
simple_unlock(&tp->t_lock);
splx(spl);
return result;
}
/*
* Get TTY status.
* No locks may be held.
* May run on any CPU.
*/
io_return_t tty_get_status(
register struct tty *tp,
dev_flavor_t flavor,
int * data, /* pointer to OUT array */
natural_t *count) /* out */
{
spl_t s;
switch (flavor) {
case TTY_STATUS:
{
register struct tty_status *tsp =
(struct tty_status *) data;
if (*count < TTY_STATUS_COUNT)
return (D_INVALID_OPERATION);
s = spltty();
simple_lock(&tp->t_lock);
tsp->tt_ispeed = tp->t_ispeed;
tsp->tt_ospeed = tp->t_ospeed;
tsp->tt_breakc = tp->t_breakc;
tsp->tt_flags = tp->t_flags;
if (tp->t_state & TS_HUPCLS)
tsp->tt_flags |= TF_HUPCLS;
simple_unlock(&tp->t_lock);
splx(s);
*count = TTY_STATUS_COUNT;
break;
}
default:
return D_INVALID_OPERATION;
}
return D_SUCCESS;
}
/*
* Set TTY status.
* No locks may be held.
* Calls device start or stop routines; must already be on master if
* device needs to run on master.
*/
io_return_t tty_set_status(
register struct tty *tp,
dev_flavor_t flavor,
int * data,
natural_t count)
{
int s;
switch (flavor) {
case TTY_FLUSH:
{
register int flags;
if (count < TTY_FLUSH_COUNT)
return D_INVALID_OPERATION;
flags = *data;
if (flags == 0)
flags = D_READ | D_WRITE;
s = spltty();
simple_lock(&tp->t_lock);
tty_flush(tp, flags);
simple_unlock(&tp->t_lock);
splx(s);
break;
}
case TTY_STOP:
/* stop output */
s = spltty();
simple_lock(&tp->t_lock);
if ((tp->t_state & TS_TTSTOP) == 0) {
tp->t_state |= TS_TTSTOP;
(*tp->t_stop)(tp, 0);
}
simple_unlock(&tp->t_lock);
splx(s);
break;
case TTY_START:
/* start output */
s = spltty();
simple_lock(&tp->t_lock);
if (tp->t_state & TS_TTSTOP) {
tp->t_state &= ~TS_TTSTOP;
tty_output(tp);
}
simple_unlock(&tp->t_lock);
splx(s);
break;
case TTY_STATUS:
/* set special characters and speed */
{
register struct tty_status *tsp;
if (count < TTY_STATUS_COUNT)
return D_INVALID_OPERATION;
tsp = (struct tty_status *)data;
if (tsp->tt_ispeed < 0 ||
tsp->tt_ispeed >= NSPEEDS ||
tsp->tt_ospeed < 0 ||
tsp->tt_ospeed >= NSPEEDS)
{
return D_INVALID_OPERATION;
}
s = spltty();
simple_lock(&tp->t_lock);
tp->t_ispeed = tsp->tt_ispeed;
tp->t_ospeed = tsp->tt_ospeed;
tp->t_breakc = tsp->tt_breakc;
tp->t_flags = tsp->tt_flags & ~TF_HUPCLS;
if (tsp->tt_flags & TF_HUPCLS)
tp->t_state |= TS_HUPCLS;
simple_unlock(&tp->t_lock);
splx(s);
break;
}
default:
return D_INVALID_OPERATION;
}
return D_SUCCESS;
}
/*
* [internal]
* Queue IOR on reply queue, to wait for TTY operation.
* TTY must be locked (at spltty).
*/
void queue_delayed_reply(
queue_t qh,
io_req_t ior,
boolean_t (*io_done)(io_req_t) )
{
ior->io_done = io_done;
enqueue_tail(qh, (queue_entry_t)ior);
}
/*
* Retry delayed IO operations for TTY.
* TTY containing queue must be locked (at spltty).
*/
void tty_queue_completion(
register queue_t qh)
{
register io_req_t ior;
while ((ior = (io_req_t)dequeue_head(qh)) != 0) {
iodone(ior);
}
}
/*
* Set the default special characters.
* Since this routine is called whenever a tty has never been opened,
* we can initialize the queues here.
*/
void ttychars(
register struct tty *tp)
{
if ((tp->t_flags & TS_INIT) == 0) {
/*
* Initialize queues
*/
queue_init(&tp->t_delayed_open);
queue_init(&tp->t_delayed_read);
queue_init(&tp->t_delayed_write);
/*
* Initialize character buffers
*/
cb_alloc(&tp->t_inq, tty_inq_size);
/* if we might do modem flow control */
if (tp->t_mctl && tp->t_inq.c_hog > 30)
tp->t_inq.c_hog -= 30;
cb_alloc(&tp->t_outq, tty_outq_size);
/*
* Mark initialized
*/
tp->t_state |= TS_INIT;
}
tp->t_breakc = 0;
}
/*
* Flush all TTY queues.
* Called at spltty, tty already locked.
* Calls device STOP routine; must already be on master if
* device needs to run on master.
*/
void tty_flush(
register struct tty *tp,
int rw)
{
if (rw & D_READ) {
cb_clear(&tp->t_inq);
tty_queue_completion(&tp->t_delayed_read);
}
if (rw & D_WRITE) {
tp->t_state &= ~TS_TTSTOP;
(*tp->t_stop)(tp, rw);
cb_clear(&tp->t_outq);
tty_queue_completion(&tp->t_delayed_write);
}
}
/*
* Restart character output after a delay timeout.
* Calls device start routine - must be on master CPU.
*
* Timeout routines are called only on master CPU.
* What if device runs on a different CPU?
*/
void ttrstrt(
register struct tty *tp)
{
register spl_t s;
s = spltty();
simple_lock(&tp->t_lock);
tp->t_state &= ~TS_TIMEOUT;
ttstart (tp);
simple_unlock(&tp->t_lock);
splx(s);
}
/*
* Start output on the typewriter. It is used from the top half
* after some characters have been put on the output queue,
* from the interrupt routine to transmit the next
* character, and after a timeout has finished.
*
* Called at spltty, tty already locked.
* Must be on master CPU if device runs on master.
*/
void ttstart(tp)
register struct tty *tp;
{
if ((tp->t_state & (TS_TIMEOUT|TS_TTSTOP|TS_BUSY)) == 0) {
/*
* Start up the hardware again
*/
(*tp->t_start)(tp);
/*
* Wake up those waiting for write completion.
*/
if (tp->t_outq.c_cc <= TTLOWAT(tp))
tty_queue_completion(&tp->t_delayed_write);
}
}
/*
* Start character output, if the device is not busy or
* stopped or waiting for a timeout.
*
* Called at spltty, tty already locked.
* Must be on master CPU if device runs on master.
*/
void tty_output(
register struct tty *tp)
{
if ((tp->t_state & (TS_TIMEOUT|TS_TTSTOP|TS_BUSY)) == 0) {
/*
* Not busy. Start output.
*/
(*tp->t_start)(tp);
/*
* Wake up those waiting for write completion.
*/
if (tp->t_outq.c_cc <= TTLOWAT(tp))
tty_queue_completion(&tp->t_delayed_write);
}
}
/*
* Send any buffered recvd chars up to user
*/
void ttypush(
register struct tty *tp)
{
spl_t s = spltty();
register int state;
simple_lock(&tp->t_lock);
/*
The pdma timeout has gone off.
If no character has been received since the timeout
was set, push any pending characters up.
If any characters were received in the last interval
then just reset the timeout and the character received bit.
*/
state = tp->t_state;
if (state & TS_MIN_TO)
{
if (state & TS_MIN_TO_RCV)
{ /* a character was received */
tp->t_state = state & ~TS_MIN_TO_RCV;
timeout(ttypush,tp,pdma_timeouts[tp->t_ispeed]);
}
else
{
tp->t_state = state & ~TS_MIN_TO;
if (tp->t_inq.c_cc) /* pending characters */
tty_queue_completion(&tp->t_delayed_read);
}
}
else
{
tp->t_state = state & ~TS_MIN_TO_RCV;/* sanity */
}
simple_unlock(&tp->t_lock);
splx(s);
}
/*
* Put input character on input queue.
*
* Called at spltty, tty already locked.
*/
void ttyinput(
unsigned int c,
struct tty *tp)
{
if (tp->t_inq.c_cc >= tp->t_inq.c_hog) {
/*
* Do not want to overflow input queue
*/
if (tp->t_mctl) {
(*tp->t_mctl)(tp, TM_RTS, DMBIC);
tp->t_state |= TS_RTS_DOWN;
}
tty_queue_completion(&tp->t_delayed_read);
return;
}
c &= 0xff;
(void) putc(c, &tp->t_inq);
if ((tp->t_state & TS_MIN) == 0 ||
tp->t_inq.c_cc > pdma_water_mark[tp->t_ispeed])
{
/*
* No input buffering, or input minimum exceeded.
* Grab a request from input queue and queue it
* to io_done thread.
*/
if (tp->t_state & TS_MIN_TO) {
tp->t_state &= ~(TS_MIN_TO|TS_MIN_TO_RCV);
untimeout(ttypush, tp);
}
tty_queue_completion(&tp->t_delayed_read);
}
else {
/*
* Not enough characters.
* If no timeout is set, initiate the timeout
* Otherwise set the character received during timeout interval
* flag.
* One alternative approach would be just to reset the timeout
* into the future, but this involves making a timeout/untimeout
* call on every character.
*/
register int ptime = pdma_timeouts[tp->t_ispeed];
if (ptime > 0)
{
if ((tp->t_state & TS_MIN_TO) == 0)
{
tp->t_state |= TS_MIN_TO;
timeout(ttypush, tp, ptime);
}
else
{
tp->t_state |= TS_MIN_TO_RCV;
}
}
}
}
/*
* Put many characters on input queue.
*
* Called at spltty, tty already locked.
*/
void ttyinput_many(
struct tty *tp,
unsigned char *chars,
int count)
{
/*
* Do not want to overflow input queue
*/
if (tp->t_inq.c_cc < tp->t_inq.c_hog)
count -= b_to_q( chars, count, &tp->t_inq);
tty_queue_completion(&tp->t_delayed_read);
}
/*
* Handle modem control transition on a tty.
* Flag indicates new state of carrier.
* Returns FALSE if the line should be turned off.
*
* Called at spltty, tty already locked.
*/
boolean_t ttymodem(
struct tty * tp,
boolean_t carrier_up)
{
if ((tp->t_state&TS_WOPEN) == 0 && (tp->t_flags & MDMBUF)) {
/*
* Flow control by carrier. Carrier down stops
* output; carrier up restarts output.
*/
if (carrier_up) {
tp->t_state &= ~TS_TTSTOP;
tty_output(tp);
}
else if ((tp->t_state&TS_TTSTOP) == 0) {
tp->t_state |= TS_TTSTOP;
(*tp->t_stop)(tp, 0);
}
}
else if (carrier_up) {
/*
* Carrier now on.
*/
tp->t_state |= TS_CARR_ON;
tt_open_wakeup(tp);
}
else {
/*
* Lost carrier.
*/
tp->t_state &= ~TS_CARR_ON;
if (tp->t_state & TS_ISOPEN &&
(tp->t_flags & NOHANG) == 0)
{
/*
* Hang up TTY if carrier drops.
* Need to alert users, somehow...
*/
tty_flush(tp, D_READ|D_WRITE);
return FALSE;
}
}
return TRUE;
}
/*
* Similarly, handle transitions on the ClearToSend
* signal. Nowadays, it is used by many modems as
* a flow-control device: they turn it down to stop
* us from sending more chars. We do the same with
* the RequestToSend signal. [Yes, that is exactly
* why those signals are defined in the standard.]
*
* Tty must be locked and on master.
*/
tty_cts(
struct tty * tp,
boolean_t cts_up)
{
if (tp->t_state & TS_ISOPEN){
if (cts_up) {
tp->t_state &= ~(TS_TTSTOP|TS_BUSY);
tty_output(tp);
} else {
tp->t_state |= (TS_TTSTOP|TS_BUSY);
(*tp->t_stop)(tp, D_WRITE);
}
}
}
|