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
|
/*
Copyright (C) 1995, 1996, 1998, 1999 Free Software Foundation, Inc.
Written by Michael I. Bushnell, p/BSG.
This file is part of the GNU Hurd.
The GNU Hurd 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, or (at
your option) any later version.
The GNU Hurd 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, USA. */
/* Avoid defenition of the baud rates from <ternios.h> at a later time. */
#include <termios.h>
/* And undefine the baud rates to avoid warnings from
<device/tty_status.h>. */
#undef B50
#undef B75
#undef B110
#undef B134
#undef B150
#undef B200
#undef B300
#undef B600
#undef B1200
#undef B1800
#undef B2400
#undef B4800
#undef B9600
#undef B19200
#undef B38400
#undef B57600
#undef B115200
#undef EXTA
#undef EXTB
#include <assert.h>
#include <errno.h>
#include <error.h>
#include <string.h>
#include <device/device.h>
#include <device/device_request.h>
#include <device/tty_status.h>
#include <cthreads.h>
#include <hurd.h>
#include <hurd/ports.h>
#include "term.h"
/* This flag is set if there is an outstanding device_write. */
static int output_pending;
/* This flag is set if there is an outstanding device_read. */
static int input_pending;
/* Tell the status of any pending open */
static enum
{
NOTPENDING, /* no open is pending */
INITIAL, /* initial open of device pending */
FAKE, /* open pending to block on dtr */
} open_pending;
static char pending_output[IO_INBAND_MAX];
static int npending_output;
static struct port_class *phys_reply_class;
/* The Mach device_t representing the terminal. */
static device_t phys_device = MACH_PORT_NULL;
/* The ports we get replies on for device calls. */
static mach_port_t phys_reply_writes = MACH_PORT_NULL;
static mach_port_t phys_reply = MACH_PORT_NULL;
/* The port-info structs. */
static struct port_info *phys_reply_writes_pi;
static struct port_info *phys_reply_pi;
static device_t device_master;
static int output_stopped;
/* XXX Mask that omits high bits we are currently not supposed to pass
through. */
static int char_size_mask_xxx = 0xff;
/* Forward */
static void devio_desert_dtr ();
static void init_devio (void) __attribute__ ((constructor));
static void
init_devio ()
{
mach_port_t host_priv;
error_t err;
err = get_privileged_ports (&host_priv, &device_master);
if (err)
error (1, err, "Getting priviliged ports");
mach_port_deallocate (mach_task_self (), host_priv);
phys_reply_class = ports_create_class (0, 0);
}
/* XXX Convert a real speed to a bogus Mach speed. Return
-1 if the real speed was bogus, else 0. */
static int
real_speed_to_bogus_speed (int rspeed, int *bspeed)
{
switch (rspeed)
{
case 0:
*bspeed = B0;
break;
case 50:
*bspeed = B50;
break;
case 75:
*bspeed = B75;
break;
case 110:
*bspeed = B110;
break;
case 134:
*bspeed = B134;
break;
case 150:
*bspeed = B150;
break;
case 200:
*bspeed = B200;
break;
case 300:
*bspeed = B300;
break;
case 600:
*bspeed = B600;
break;
case 1200:
*bspeed = B1200;
break;
case 1800:
*bspeed = B1800;
break;
case 2400:
*bspeed = B2400;
break;
case 4800:
*bspeed = B4800;
break;
case 9600:
*bspeed = B9600;
break;
case 19200:
*bspeed = EXTA;
break;
case 38400:
*bspeed = EXTB;
break;
default:
return -1;
}
return 0;
}
/* XXX Convert a bogus speed to a real speed. */
static int
bogus_speed_to_real_speed (int bspeed)
{
switch (bspeed)
{
case B0:
default:
return 0;
case B50:
return 50;
case B75:
return 75;
case B110:
return 110;
case B134:
return 134;
case B150:
return 150;
case B200:
return 200;
case B300:
return 300;
case B600:
return 600;
case B1200:
return 1200;
case B1800:
return 1800;
case B2400:
return 2400;
case B4800:
return 4800;
case B9600:
return 9600;
case EXTA:
return 19200;
case EXTB:
return 38400;
}
}
/* If there are characters on the output queue and no
pending output requests, then send them. */
static void
devio_start_output ()
{
char *cp;
int size;
error_t err;
size = qsize (outputq);
if (!size || output_pending || (termflags & USER_OUTPUT_SUSP))
return;
if (output_stopped)
{
device_set_status (phys_device, TTY_START, 0, 0);
output_stopped = 0;
}
/* Copy characters onto PENDING_OUTPUT, not bothering
those already there. */
if (size + npending_output > IO_INBAND_MAX)
size = IO_INBAND_MAX - npending_output;
cp = pending_output + npending_output;
npending_output += size;
while (size--)
*cp++ = dequeue (outputq);
/* Submit all the outstanding characters to the device. */
/* The D_NOWAIT flag does not, in fact, prevent blocks. Instead,
it merely causes D_WOULD_BLOCK errors when carrier is down...
whee.... */
err = device_write_request_inband (phys_device, phys_reply_writes, D_NOWAIT,
0, pending_output, npending_output);
if (err == MACH_SEND_INVALID_DEST)
devio_desert_dtr ();
else if (!err)
output_pending = 1;
}
error_t
device_write_reply_inband (mach_port_t replypt,
error_t return_code,
int amount)
{
if (replypt != phys_reply_writes)
return EOPNOTSUPP;
mutex_lock (&global_lock);
output_pending = 0;
if (return_code == 0)
{
if (amount >= npending_output)
{
npending_output = 0;
condition_broadcast (outputq->wait);
}
else
{
/* Copy the characters that didn't get output
to the front of the array. */
npending_output -= amount;
memmove (pending_output, pending_output + amount, npending_output);
}
devio_start_output ();
}
else if (return_code == D_WOULD_BLOCK)
/* Carrier has dropped. */
devio_desert_dtr ();
else
devio_start_output ();
mutex_unlock (&global_lock);
return 0;
}
error_t
device_read_reply_inband (mach_port_t replypt,
error_t error_code,
char *data,
u_int datalen)
{
int i, flush;
error_t err;
if (replypt != phys_reply)
return EOPNOTSUPP;
mutex_lock (&global_lock);
input_pending = 0;
if (!error_code && (termstate.c_cflag & CREAD))
for (i = 0; i < datalen; i++)
{
int c = data[i];
/* XXX Mach only supports 8-bit channels; this munges things
to account for the reality. */
c &= char_size_mask_xxx;
flush = input_character (c);
if (flush)
break;
}
else if (error_code == D_WOULD_BLOCK)
{
devio_desert_dtr ();
mutex_unlock (&global_lock);
return 0;
}
/* D_NOWAIT does not actually prevent blocks; it merely causes
D_WOULD_BLOCK errors when carrier drops. */
err = device_read_request_inband (phys_device, phys_reply, D_NOWAIT,
0, vm_page_size);
if (err)
devio_desert_dtr ();
else
input_pending = 1;
mutex_unlock (&global_lock);
return 0;
}
static void
devio_set_break ()
{
device_set_status (phys_device, TTY_SET_BREAK, 0, 0);
}
static void
devio_clear_break ()
{
device_set_status (phys_device, TTY_CLEAR_BREAK, 0, 0);
}
static void
devio_abandon_physical_output ()
{
int val = D_WRITE;
/* If this variable is clear, then carrier is gone, so we
have nothing to do. */
if (!phys_reply_writes_pi)
return;
mach_port_deallocate (mach_task_self (), phys_reply_writes);
ports_reallocate_port (phys_reply_writes_pi);
phys_reply_writes = ports_get_right (phys_reply_writes_pi);
mach_port_insert_right (mach_task_self (), phys_reply_writes,
phys_reply_writes, MACH_MSG_TYPE_MAKE_SEND);
device_set_status (phys_device, TTY_FLUSH, &val, TTY_FLUSH_COUNT);
npending_output = 0;
output_pending = 0;
}
static void
devio_suspend_physical_output ()
{
if (!output_stopped)
{
device_set_status (phys_device, TTY_STOP, 0, 0);
output_stopped = 1;
}
}
static void
devio_notice_input_flushed ()
{
}
static int
devio_pending_output_size ()
{
/* Unfortunately, there's no way to get the amount back from Mach
that has actually been written from this... */
return npending_output;
}
/* Do this the first time the device is to be opened */
static error_t
initial_open ()
{
error_t err;
assert (open_pending != FAKE);
/* Nothing to do */
if (open_pending == INITIAL)
return 0;
assert (phys_device == MACH_PORT_NULL);
assert (phys_reply == MACH_PORT_NULL);
assert (phys_reply_pi == 0);
err = ports_create_port (phys_reply_class, term_bucket,
sizeof (struct port_info), &phys_reply_pi);
if (err)
return err;
phys_reply = ports_get_right (phys_reply_pi);
mach_port_insert_right (mach_task_self (), phys_reply, phys_reply,
MACH_MSG_TYPE_MAKE_SEND);
err = device_open_request (device_master, phys_reply,
D_READ|D_WRITE, pterm_name);
if (err)
{
mach_port_deallocate (mach_task_self (), phys_reply);
phys_reply = MACH_PORT_NULL;
ports_port_deref (phys_reply_pi);
phys_reply_pi = 0;
}
else
open_pending = INITIAL;
return err;
}
static void
devio_desert_dtr ()
{
int bits;
/* Turn off DTR. */
bits = TM_HUP;
device_set_status (phys_device, TTY_MODEM,
(dev_status_t) &bits, TTY_MODEM_COUNT);
report_carrier_off ();
}
static error_t
devio_assert_dtr ()
{
error_t err;
/* The first time is special. */
if (phys_device == MACH_PORT_NULL)
return initial_open ();
/* Schedule a fake open to wait for DTR, unless one is already
happening. */
assert (open_pending != INITIAL);
if (open_pending == FAKE)
return 0;
err = device_open_request (device_master, phys_reply,
D_READ|D_WRITE, pterm_name);
if (err)
return err;
open_pending = FAKE;
return 0;
}
kern_return_t
device_open_reply (mach_port_t replyport,
int returncode,
mach_port_t device)
{
struct tty_status ttystat;
int count = TTY_STATUS_COUNT;
error_t err = 0;
if (replyport != phys_reply)
return EOPNOTSUPP;
mutex_lock (&global_lock);
assert (open_pending != NOTPENDING);
if (open_pending == INITIAL)
{
/* Special handling for the first open */
if (returncode != 0)
{
/* Note that DEVICE is total garbage (not a real port name at all!)
in this case. */
if (returncode == D_NO_SUCH_DEVICE)
/* Record that the device does not exist. */
termflags |= NO_DEVICE;
/* Bogus. */
report_carrier_on ();
report_carrier_off ();
mach_port_deallocate (mach_task_self (), phys_reply);
phys_reply = MACH_PORT_NULL;
ports_port_deref (phys_reply_pi);
phys_reply_pi = 0;
open_pending = NOTPENDING;
mutex_unlock (&global_lock);
return 0;
}
assert (phys_device == MACH_PORT_NULL);
assert (phys_reply_writes == MACH_PORT_NULL);
assert (phys_reply_writes_pi == 0);
phys_device = device;
err = ports_create_port (phys_reply_class, term_bucket,
sizeof (struct port_info),
&phys_reply_writes_pi);
if (err)
{
open_pending = NOTPENDING;
mutex_unlock (&global_lock);
return err;
}
phys_reply_writes = ports_get_right (phys_reply_writes_pi);
mach_port_insert_right (mach_task_self (), phys_reply_writes,
phys_reply_writes, MACH_MSG_TYPE_MAKE_SEND);
/* Schedule our first read */
err = device_read_request_inband (phys_device, phys_reply, D_NOWAIT,
0, vm_page_size);
input_pending = 1;
}
else
{
/* This was a fake open, only for the sake of assert DTR. */
device_close (device);
mach_port_deallocate (mach_task_self (), device);
}
device_get_status (phys_device, TTY_STATUS,
(dev_status_t)&ttystat, &count);
ttystat.tt_breakc = 0;
ttystat.tt_flags = TF_ANYP | TF_LITOUT | TF_NOHANG | TF_HUPCLS;
device_set_status (phys_device, TTY_STATUS,
(dev_status_t)&ttystat, TTY_STATUS_COUNT);
report_carrier_on ();
if (err)
devio_desert_dtr ();
open_pending = NOTPENDING;
mutex_unlock (&global_lock);
return 0;
}
/* Adjust physical state on the basis of the terminal state.
Where it isn't possible, mutate terminal state to match
reality. */
static void
devio_set_bits ()
{
if (!(termstate.c_cflag & CIGNORE) && phys_device != MACH_PORT_NULL)
{
struct tty_status ttystat;
int cnt = TTY_STATUS_COUNT;
/* Find the current state. */
device_get_status (phys_device, TTY_STATUS, (dev_status_t) &ttystat, &cnt);
if (termstate.__ispeed)
real_speed_to_bogus_speed (termstate.__ispeed, &ttystat.tt_ispeed);
if (termstate.__ospeed)
real_speed_to_bogus_speed (termstate.__ospeed, &ttystat.tt_ospeed);
/* Try and set it. */
device_set_status (phys_device, TTY_STATUS,
(dev_status_t) &ttystat, TTY_STATUS_COUNT);
/* And now make termstate match reality. */
cnt = TTY_STATUS_COUNT;
device_get_status (phys_device, TTY_STATUS, (dev_status_t) &ttystat, &cnt);
termstate.__ispeed = bogus_speed_to_real_speed (ttystat.tt_ispeed);
termstate.__ospeed = bogus_speed_to_real_speed (ttystat.tt_ospeed);
/* Mach forces us to use the normal stop bit convention:
two bits at 110 bps; 1 bit otherwise. */
if (termstate.__ispeed == 110)
termstate.c_cflag |= CSTOPB;
else
termstate.c_cflag &= ~CSTOPB;
/* Figure out how to munge input, since we are unable to actually
affect what the hardware does. */
switch (termstate.c_cflag & CSIZE)
{
case CS5:
char_size_mask_xxx = 0x1f;
break;
case CS6:
char_size_mask_xxx = 0x3f;
break;
case CS7:
char_size_mask_xxx = 0x7f;
break;
case CS8:
default:
char_size_mask_xxx = 0xff;
break;
}
if (termstate.c_cflag & PARENB)
char_size_mask_xxx |= 0x80;
}
}
static void
devio_mdmctl (int how, int bits)
{
int oldbits, newbits;
int cnt;
if ((how == MDMCTL_BIS) || (how == MDMCTL_BIC))
{
cnt = TTY_MODEM_COUNT;
device_get_status (phys_device, TTY_MODEM,
(dev_status_t) &oldbits, &cnt);
if (cnt < TTY_MODEM_COUNT)
oldbits = 0; /* what else can we do? */
}
if (how == MDMCTL_BIS)
newbits = (oldbits | bits);
else if (how == MDMCTL_BIC)
newbits = (oldbits &= ~bits);
else
newbits = bits;
device_set_status (phys_device, TTY_MODEM,
(dev_status_t) &newbits, TTY_MODEM_COUNT);
}
static int
devio_mdmstate ()
{
int bits, cnt;
cnt = TTY_MODEM_COUNT;
device_get_status (phys_device, TTY_MODEM, (dev_status_t) &bits, &cnt);
if (cnt != TTY_MODEM_COUNT)
return 0;
else
return bits;
}
/* Unused stubs */
kern_return_t
device_read_reply (mach_port_t port,
kern_return_t retcode,
io_buf_ptr_t data,
mach_msg_type_number_t cnt)
{
return EOPNOTSUPP;
}
kern_return_t
device_write_reply (mach_port_t replyport,
kern_return_t retcode,
int written)
{
return EOPNOTSUPP;
}
error_t
ports_do_mach_notify_send_once (mach_port_t notify)
{
error_t err;
mutex_lock (&global_lock);
if (notify == phys_reply_writes)
{
err = 0;
devio_start_output ();
}
else if (notify == phys_reply)
{
if (input_pending)
{
/* xxx */
char msg[] = "Term input check happened\r\n";
int foo;
device_write_inband (phys_device, 0, 0, msg, sizeof msg, &foo);
/* end xxx */
input_pending = 0;
err = device_read_request_inband (phys_device, phys_reply,
D_NOWAIT, 0, vm_page_size);
if (err)
devio_desert_dtr ();
else
input_pending = 1;
}
else if (open_pending != NOTPENDING)
{
open_pending = NOTPENDING;
report_carrier_on ();
report_carrier_off ();
mach_port_deallocate (mach_task_self (), phys_reply);
phys_reply = MACH_PORT_NULL;
ports_port_deref (phys_reply_pi);
phys_reply_pi = 0;
}
err = 0;
}
else
err = EOPNOTSUPP;
mutex_unlock (&global_lock);
return err;
}
struct bottomhalf devio_bottom =
{
devio_start_output,
devio_set_break,
devio_clear_break,
devio_abandon_physical_output,
devio_suspend_physical_output,
devio_pending_output_size,
devio_notice_input_flushed,
devio_assert_dtr,
devio_desert_dtr,
devio_set_bits,
devio_mdmctl,
devio_mdmstate,
};
|