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#include <linux/slab.h>
40#include <linux/tty.h>
41#include <linux/tty_flip.h>
42#include <linux/device.h>
43#include <linux/sched.h>
44#include <linux/uaccess.h>
45
46#include "dgrp_common.h"
47
48#ifndef _POSIX_VDISABLE
49#define _POSIX_VDISABLE ('\0')
50#endif
51
52
53
54
55
56static void drp_param(struct ch_struct *);
57static void dgrp_tty_close(struct tty_struct *, struct file *);
58
59
60static int set_modem_info(struct ch_struct *, unsigned int, unsigned int *);
61static int get_modem_info(struct ch_struct *, unsigned int *);
62static void dgrp_set_custom_speed(struct ch_struct *, int);
63static int dgrp_tty_digigetedelay(struct tty_struct *, int *);
64static int dgrp_tty_digisetedelay(struct tty_struct *, int *);
65static int dgrp_send_break(struct ch_struct *, int);
66
67static ushort tty_to_ch_flags(struct tty_struct *, char);
68static tcflag_t ch_to_tty_flags(unsigned short, char);
69
70static void dgrp_tty_input_start(struct tty_struct *);
71static void dgrp_tty_input_stop(struct tty_struct *);
72
73static void drp_wmove(struct ch_struct *, int, void*, int);
74
75static int dgrp_tty_open(struct tty_struct *, struct file *);
76static void dgrp_tty_close(struct tty_struct *, struct file *);
77static int dgrp_tty_write(struct tty_struct *, const unsigned char *, int);
78static int dgrp_tty_write_room(struct tty_struct *);
79static void dgrp_tty_flush_buffer(struct tty_struct *);
80static int dgrp_tty_chars_in_buffer(struct tty_struct *);
81static int dgrp_tty_ioctl(struct tty_struct *, unsigned int, unsigned long);
82static void dgrp_tty_set_termios(struct tty_struct *, struct ktermios *);
83static void dgrp_tty_stop(struct tty_struct *);
84static void dgrp_tty_start(struct tty_struct *);
85static void dgrp_tty_throttle(struct tty_struct *);
86static void dgrp_tty_unthrottle(struct tty_struct *);
87static void dgrp_tty_hangup(struct tty_struct *);
88static int dgrp_tty_put_char(struct tty_struct *, unsigned char);
89static int dgrp_tty_tiocmget(struct tty_struct *);
90static int dgrp_tty_tiocmset(struct tty_struct *, unsigned int, unsigned int);
91static int dgrp_tty_send_break(struct tty_struct *, int);
92static void dgrp_tty_send_xchar(struct tty_struct *, char);
93
94
95
96
97#define SERIAL_TYPE_NORMAL 1
98#define SERIAL_TYPE_CALLOUT 2
99#define SERIAL_TYPE_XPRINT 3
100
101
102
103
104
105
106
107#define PORTSERVER_DIVIDEND 1843200
108
109
110
111
112static struct digi_struct digi_init = {
113 .digi_flags = DIGI_COOK,
114 .digi_maxcps = 100,
115 .digi_maxchar = 50,
116 .digi_bufsize = 100,
117 .digi_onlen = 4,
118 .digi_offlen = 4,
119 .digi_onstr = "\033[5i",
120 .digi_offstr = "\033[4i",
121 .digi_term = "ansi"
122};
123
124
125
126
127
128
129
130
131static struct ktermios DefaultTermios = {
132 .c_iflag = (ICRNL | IXON),
133 .c_oflag = (OPOST | ONLCR),
134 .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
135 .c_lflag = (ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL
136 | ECHOKE | IEXTEN),
137 .c_cc = INIT_C_CC,
138 .c_line = 0,
139};
140
141
142static const struct tty_operations dgrp_tty_ops = {
143 .open = dgrp_tty_open,
144 .close = dgrp_tty_close,
145 .write = dgrp_tty_write,
146 .write_room = dgrp_tty_write_room,
147 .flush_buffer = dgrp_tty_flush_buffer,
148 .chars_in_buffer = dgrp_tty_chars_in_buffer,
149 .flush_chars = NULL,
150 .ioctl = dgrp_tty_ioctl,
151 .set_termios = dgrp_tty_set_termios,
152 .stop = dgrp_tty_stop,
153 .start = dgrp_tty_start,
154 .throttle = dgrp_tty_throttle,
155 .unthrottle = dgrp_tty_unthrottle,
156 .hangup = dgrp_tty_hangup,
157 .put_char = dgrp_tty_put_char,
158 .tiocmget = dgrp_tty_tiocmget,
159 .tiocmset = dgrp_tty_tiocmset,
160 .break_ctl = dgrp_tty_send_break,
161 .send_xchar = dgrp_tty_send_xchar
162};
163
164
165static int calc_baud_rate(struct un_struct *un)
166{
167 int i;
168 int brate;
169
170 struct baud_rates {
171 unsigned int rate;
172 unsigned int cflag;
173 };
174
175 static struct baud_rates baud_rates[] = {
176 { 921600, B921600 },
177 { 460800, B460800 },
178 { 230400, B230400 },
179 { 115200, B115200 },
180 { 57600, B57600 },
181 { 38400, B38400 },
182 { 19200, B19200 },
183 { 9600, B9600 },
184 { 4800, B4800 },
185 { 2400, B2400 },
186 { 1200, B1200 },
187 { 600, B600 },
188 { 300, B300 },
189 { 200, B200 },
190 { 150, B150 },
191 { 134, B134 },
192 { 110, B110 },
193 { 75, B75 },
194 { 50, B50 },
195 { 0, B9600 }
196 };
197
198 brate = C_BAUD(un->un_tty);
199
200 for (i = 0; baud_rates[i].rate; i++) {
201 if (baud_rates[i].cflag == brate)
202 break;
203 }
204
205 return baud_rates[i].rate;
206}
207
208static int calc_fastbaud_rate(struct un_struct *un, struct ktermios *uts)
209{
210 int i;
211 int brate;
212
213 ulong bauds[2][16] = {
214 {
215 0, 57600, 76800, 115200,
216 131657, 153600, 230400, 460800,
217 921600, 1200, 1800, 2400,
218 4800, 9600, 19200, 38400 },
219 {
220 0, 57600, 115200, 230400,
221 460800, 150, 200, 921600,
222 600, 1200, 1800, 2400,
223 4800, 9600, 19200, 38400 }
224 };
225
226 brate = C_BAUD(un->un_tty) & 0xff;
227
228 i = (uts->c_cflag & CBAUDEX) ? 1 : 0;
229
230
231 if ((i >= 0) && (i < 2) && (brate >= 0) && (brate < 16))
232 brate = bauds[i][brate];
233 else
234 brate = 0;
235
236 return brate;
237}
238
239
240
241
242
243
244
245
246
247static void drp_param(struct ch_struct *ch)
248{
249 struct nd_struct *nd;
250 struct un_struct *un;
251 int brate;
252 int mflow;
253 int xflag;
254 int iflag;
255 struct ktermios *tts, *pts, *uts;
256
257 nd = ch->ch_nd;
258
259
260
261
262
263
264 if (ch->ch_tun.un_open_count) {
265
266 un = &ch->ch_tun;
267 tts = &ch->ch_tun.un_tty->termios;
268
269
270
271
272
273
274
275
276 if (ch->ch_pun.un_open_count) {
277
278 pts = &ch->ch_pun.un_tty->termios;
279
280 pts->c_cflag ^=
281 (pts->c_cflag ^ tts->c_cflag) &
282 (CBAUD | CSIZE | CSTOPB | CREAD | PARENB |
283 PARODD | HUPCL | CLOCAL);
284
285 pts->c_iflag ^=
286 (pts->c_iflag ^ tts->c_iflag) &
287 (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK |
288 ISTRIP | IXON | IXANY | IXOFF);
289
290 pts->c_cc[VSTART] = tts->c_cc[VSTART];
291 pts->c_cc[VSTOP] = tts->c_cc[VSTOP];
292 }
293 } else if (ch->ch_pun.un_open_count == 0) {
294 pr_warn("%s - ch_pun.un_open_count shouldn't be 0\n",
295 __func__);
296 return;
297 } else {
298 un = &ch->ch_pun;
299 }
300
301 uts = &un->un_tty->termios;
302
303
304
305
306
307 if ((ch->ch_digi.digi_flags & DIGI_COOK) != 0 &&
308 (ch->ch_tun.un_open_count != 0) &&
309 !((un->un_tty)->ldisc->ops->flags & LDISC_FLAG_DEFINED) &&
310 !(L_XCASE(un->un_tty))) {
311 ch->ch_flag |= CH_FAST_WRITE;
312 } else {
313 ch->ch_flag &= ~CH_FAST_WRITE;
314 }
315
316
317
318
319
320
321 if ((ch->ch_flag & CH_FAST_WRITE) &&
322 O_OPOST(un->un_tty) != 0) {
323 int oflag = tty_to_ch_flags(un->un_tty, 'o');
324
325
326 ch->ch_ocook |= oflag & (OF_OLCUC |
327 OF_ONLCR |
328 OF_OCRNL |
329 OF_ONLRET |
330 OF_TABDLY);
331
332
333
334
335
336
337
338 oflag = ch_to_tty_flags(ch->ch_ocook, 'o');
339 uts->c_oflag &= ~oflag;
340
341 } else {
342
343 int oflag = ch_to_tty_flags(ch->ch_ocook, 'o');
344 uts->c_oflag |= oflag;
345 ch->ch_ocook = 0;
346 }
347
348 ch->ch_oflag = ch->ch_ocook;
349
350
351 ch->ch_flag &= ~CH_FAST_READ;
352
353
354
355
356
357 if (C_BAUD(un->un_tty) == B0) {
358 if (!(ch->ch_flag & CH_BAUD0)) {
359
360
361
362 ch->ch_tout = ch->ch_tin;
363 ch->ch_rout = ch->ch_rin;
364
365 ch->ch_break_time = 0;
366
367 ch->ch_send |= RR_TX_FLUSH | RR_RX_FLUSH;
368
369 ch->ch_mout &= ~(DM_DTR | DM_RTS);
370
371 ch->ch_flag |= CH_BAUD0;
372 }
373 } else if (ch->ch_custom_speed) {
374 ch->ch_brate = PORTSERVER_DIVIDEND / ch->ch_custom_speed;
375
376 if (ch->ch_flag & CH_BAUD0) {
377 ch->ch_mout |= DM_DTR | DM_RTS;
378
379 ch->ch_flag &= ~CH_BAUD0;
380 }
381 } else {
382
383
384
385
386
387
388
389
390
391
392
393 if (!(ch->ch_digi.digi_flags & DIGI_FAST))
394 brate = calc_baud_rate(un);
395 else
396 brate = calc_fastbaud_rate(un, uts);
397
398 if (brate == 0)
399 brate = 9600;
400
401 ch->ch_brate = PORTSERVER_DIVIDEND / brate;
402
403 if (ch->ch_flag & CH_BAUD0) {
404 ch->ch_mout |= DM_DTR | DM_RTS;
405
406 ch->ch_flag &= ~CH_BAUD0;
407 }
408 }
409
410
411
412
413
414 ch->ch_cflag = tty_to_ch_flags(un->un_tty, 'c');
415
416
417
418
419
420 iflag = (int) tty_to_ch_flags(un->un_tty, 'i');
421
422 if (START_CHAR(un->un_tty) == _POSIX_VDISABLE ||
423 STOP_CHAR(un->un_tty) == _POSIX_VDISABLE) {
424 iflag &= ~(IF_IXON | IF_IXANY | IF_IXOFF);
425 }
426
427 ch->ch_iflag = iflag;
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444 if (uts->c_cc[VSTART] != _POSIX_VDISABLE)
445 ch->ch_xon = uts->c_cc[VSTART];
446 if (uts->c_cc[VSTOP] != _POSIX_VDISABLE)
447 ch->ch_xoff = uts->c_cc[VSTOP];
448
449 ch->ch_lnext = (uts->c_cc[VLNEXT] == _POSIX_VDISABLE ? 0 :
450 uts->c_cc[VLNEXT]);
451
452
453
454
455
456
457
458
459 if ((uts->c_cc[VSTART] == _POSIX_VDISABLE) ||
460 (uts->c_cc[VSTOP] == _POSIX_VDISABLE))
461 ch->ch_iflag &= ~(IF_IXOFF | IF_IXON);
462
463
464
465
466
467 xflag = 0;
468
469 if (ch->ch_digi.digi_flags & DIGI_AIXON)
470 xflag = XF_XIXON;
471
472 if ((ch->ch_xxon == _POSIX_VDISABLE) ||
473 (ch->ch_xxoff == _POSIX_VDISABLE))
474 xflag &= ~XF_XIXON;
475
476 ch->ch_xflag = xflag;
477
478
479
480
481
482
483 if (C_CLOCAL(un->un_tty))
484 ch->ch_flag |= CH_CLOCAL;
485 else
486 ch->ch_flag &= ~CH_CLOCAL;
487
488
489
490
491
492 dgrp_carrier(ch);
493
494
495
496
497
498 mflow = 0;
499
500 if (C_CRTSCTS(un->un_tty))
501 mflow |= (DM_RTS | DM_CTS);
502
503 if (ch->ch_digi.digi_flags & RTSPACE)
504 mflow |= DM_RTS;
505
506 if (ch->ch_digi.digi_flags & DTRPACE)
507 mflow |= DM_DTR;
508
509 if (ch->ch_digi.digi_flags & CTSPACE)
510 mflow |= DM_CTS;
511
512 if (ch->ch_digi.digi_flags & DSRPACE)
513 mflow |= DM_DSR;
514
515 if (ch->ch_digi.digi_flags & DCDPACE)
516 mflow |= DM_CD;
517
518 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
519 mflow |= DM_RTS_TOGGLE;
520
521 ch->ch_mflow = mflow;
522
523
524
525
526
527 ch->ch_flag |= CH_PARAM;
528 (ch->ch_nd)->nd_tx_work = 1;
529
530 if (waitqueue_active(&ch->ch_flag_wait))
531 wake_up_interruptible(&ch->ch_flag_wait);
532}
533
534
535
536
537
538static void wake_up_drp_sleep_timer(unsigned long ptr)
539{
540 struct ch_struct *ch = (struct ch_struct *) ptr;
541 if (ch)
542 wake_up(&ch->ch_sleep);
543}
544
545
546
547
548
549
550static void drp_my_sleep(struct ch_struct *ch)
551{
552 struct timer_list drp_wakeup_timer;
553 DECLARE_WAITQUEUE(wait, current);
554
555
556
557
558
559 add_wait_queue(&ch->ch_sleep, &wait);
560 current->state = TASK_UNINTERRUPTIBLE;
561
562
563
564
565
566
567 init_timer(&drp_wakeup_timer);
568 drp_wakeup_timer.function = wake_up_drp_sleep_timer;
569 drp_wakeup_timer.data = (unsigned long) ch;
570 drp_wakeup_timer.expires = jiffies + (1 * HZ);
571 add_timer(&drp_wakeup_timer);
572
573 schedule();
574
575 del_timer(&drp_wakeup_timer);
576
577 remove_wait_queue(&ch->ch_sleep, &wait);
578}
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594static int dgrp_tty_open(struct tty_struct *tty, struct file *file)
595{
596 int retval = 0;
597 struct nd_struct *nd;
598 struct ch_struct *ch;
599 struct un_struct *un;
600 int port;
601 int delay_error;
602 int otype;
603 int unf;
604 int wait_carrier;
605 int category;
606 int counts_were_incremented = 0;
607 ulong lock_flags;
608 DECLARE_WAITQUEUE(wait, current);
609
610
611
612
613
614 nd = nd_struct_get(MAJOR(tty_devnum(tty)));
615 port = PORT_NUM(MINOR(tty_devnum(tty)));
616 category = OPEN_CATEGORY(MINOR(tty_devnum(tty)));
617
618 if (!nd)
619 return -ENODEV;
620
621 if (port >= CHAN_MAX)
622 return -ENODEV;
623
624
625
626
627
628 ch = nd->nd_chan + port;
629
630 un = IS_PRINT(MINOR(tty_devnum(tty))) ? &ch->ch_pun : &ch->ch_tun;
631 un->un_tty = tty;
632 tty->driver_data = un;
633
634
635
636
637
638 if (tty_hung_up_p(file)) {
639 retval = ((un->un_flag & UN_HUP_NOTIFY) ?
640 -EAGAIN : -ERESTARTSYS);
641 goto done;
642 }
643
644
645
646
647
648 retval = wait_event_interruptible(un->un_close_wait,
649 ((un->un_flag & UN_CLOSING) == 0));
650
651 if (retval)
652 goto done;
653
654
655
656
657
658 retval = wait_event_interruptible(ch->ch_flag_wait,
659 ((ch->ch_flag & CH_PORT_GONE) == 0));
660
661 if (retval)
662 goto done;
663
664
665
666
667
668
669 if (tty->driver->subtype == SERIAL_TYPE_CALLOUT) {
670 if (un->un_flag & UN_NORMAL_ACTIVE) {
671 retval = -EBUSY;
672 goto done;
673 } else {
674 un->un_flag |= UN_CALLOUT_ACTIVE;
675 }
676 }
677
678
679
680
681
682 spin_lock_irqsave(&nd->nd_lock, lock_flags);
683
684 nd->nd_tx_work = 1;
685
686 for (;;) {
687 wait_carrier = 0;
688
689
690
691
692
693
694
695
696 if (test_bit(TTY_IO_ERROR, &tty->flags)) {
697
698 if (un->un_flag & UN_CALLOUT_ACTIVE)
699 retval = -EBUSY;
700 else
701 un->un_flag |= UN_NORMAL_ACTIVE;
702 goto unlock;
703 }
704
705 if (file->f_flags & O_NONBLOCK) {
706
707
708
709
710
711
712 otype = OTYPE_IMMEDIATE;
713 delay_error = -EAGAIN;
714
715 } else if (!OPEN_WAIT_AVAIL(category) ||
716 (file->f_flags & O_NDELAY) != 0) {
717 otype = OTYPE_IMMEDIATE;
718 delay_error = -EBUSY;
719
720 } else if (!OPEN_WAIT_CARRIER(category) ||
721 ((ch->ch_digi.digi_flags & DIGI_FORCEDCD) != 0) ||
722 C_CLOCAL(tty)) {
723 otype = OTYPE_PERSISTENT;
724 delay_error = 0;
725
726 } else {
727 otype = OTYPE_INCOMING;
728 delay_error = 0;
729 }
730
731
732
733
734
735 if (port >= nd->nd_chan_count) {
736 if (otype == OTYPE_IMMEDIATE) {
737 retval = (nd->nd_state == NS_READY) ?
738 -ENXIO : -EAGAIN;
739 goto unlock;
740 }
741 }
742
743
744
745
746
747 else if (ch->ch_open_count == 0) {
748
749
750
751
752
753 if (ch->ch_open_error != 0 && otype == ch->ch_otype) {
754 retval = (ch->ch_open_error <= 2) ?
755 delay_error : -ENXIO;
756 goto unlock;
757 }
758
759
760
761
762
763
764 if (nd->nd_state != NS_READY &&
765 otype == OTYPE_IMMEDIATE) {
766 retval = -EAGAIN;
767 goto unlock;
768 }
769
770
771
772
773
774
775 if (ch->ch_state == CS_READY && ch->ch_otype == otype)
776 break;
777 }
778
779
780
781
782
783
784 else if ((ch->ch_category == category) ||
785 IS_PRINT(MINOR(tty_devnum(tty)))) {
786
787
788
789
790 unf = ch->ch_tun.un_flag | ch->ch_pun.un_flag;
791
792 if ((file->f_flags & O_EXCL) || (unf & UN_EXCL)) {
793 retval = -EBUSY;
794 goto unlock;
795 }
796
797
798
799
800
801
802
803
804 if (ch->ch_flag & CH_HANGUP) {
805 retval = -ENXIO;
806 goto unlock;
807 }
808
809
810
811
812
813
814 if (ch->ch_state == CS_READY &&
815 (otype != OTYPE_INCOMING ||
816 ch->ch_flag & CH_VIRT_CD))
817 break;
818
819 wait_carrier = 1;
820 }
821
822
823
824
825
826 else {
827 if (otype == OTYPE_IMMEDIATE) {
828 retval = delay_error;
829 goto unlock;
830 }
831 }
832
833
834
835
836
837
838 ch->ch_wait_count[otype]++;
839
840 if (wait_carrier)
841 ch->ch_wait_carrier++;
842
843
844
845
846
847
848 add_wait_queue(&ch->ch_flag_wait, &wait);
849 current->state = TASK_INTERRUPTIBLE;
850
851 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
852
853
854
855
856
857 schedule();
858 remove_wait_queue(&ch->ch_flag_wait, &wait);
859
860 spin_lock_irqsave(&nd->nd_lock, lock_flags);
861
862 current->state = TASK_RUNNING;
863
864 ch->ch_wait_count[otype]--;
865
866 if (wait_carrier)
867 ch->ch_wait_carrier--;
868
869 nd->nd_tx_work = 1;
870
871 if (signal_pending(current)) {
872 retval = -EINTR;
873 goto unlock;
874 }
875 }
876
877
878
879
880 counts_were_incremented = 1;
881 un->un_open_count++;
882 ch->ch_open_count++;
883
884
885
886
887
888 if (ch->ch_open_count == 1) {
889 ch->ch_flag = 0;
890 ch->ch_inwait = 0;
891 ch->ch_category = category;
892 ch->ch_pscan_state = 0;
893
894
895
896
897 ch->ch_send = RR_TX_START | RR_RX_START;
898
899 if (C_CLOCAL(tty) ||
900 ch->ch_s_mlast & DM_CD ||
901 ch->ch_digi.digi_flags & DIGI_FORCEDCD)
902 ch->ch_flag |= CH_VIRT_CD;
903 else if (OPEN_FORCES_CARRIER(category))
904 ch->ch_flag |= CH_VIRT_CD;
905
906 }
907
908
909
910
911
912 if (un->un_open_count == 1) {
913
914
915
916
917
918 un->un_flag &= ~UN_DIGI_MASK;
919
920 if (file->f_flags & O_EXCL)
921 un->un_flag |= UN_EXCL;
922
923
924
925
926
927
928
929
930
931 drp_param(ch);
932
933 }
934
935 un->un_flag |= UN_INITIALIZED;
936
937 retval = 0;
938
939unlock:
940
941 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
942
943done:
944
945
946
947 if (!counts_were_incremented) {
948 un->un_open_count++;
949 ch->ch_open_count++;
950 }
951
952 if (retval)
953 dev_err(tty->dev, "tty open bad return (%i)\n", retval);
954
955 return retval;
956}
957
958
959
960
961
962
963
964static void dgrp_tty_close(struct tty_struct *tty, struct file *file)
965{
966 struct ch_struct *ch;
967 struct un_struct *un;
968 struct nd_struct *nd;
969 int tpos;
970 int port;
971 int err = 0;
972 int s = 0;
973 ulong waketime;
974 ulong lock_flags;
975 int sent_printer_offstr = 0;
976
977 port = PORT_NUM(MINOR(tty_devnum(tty)));
978
979 un = tty->driver_data;
980
981 if (!un)
982 return;
983
984 ch = un->un_ch;
985
986 if (!ch)
987 return;
988
989 nd = ch->ch_nd;
990
991 if (!nd)
992 return;
993
994 spin_lock_irqsave(&nd->nd_lock, lock_flags);
995
996
997
998 if (un->un_open_count != 1)
999 goto unlock;
1000
1001
1002
1003
1004 un->un_flag |= UN_CLOSING;
1005
1006
1007
1008
1009 tty->closing = 1;
1010
1011
1012
1013
1014
1015
1016 if (ch->ch_open_count == 1) {
1017
1018
1019
1020
1021 if (IS_PRINT(MINOR(tty_devnum(tty))) &&
1022 (((ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK) <
1023 ch->ch_digi.digi_offlen))
1024 ch->ch_tin = ch->ch_tout;
1025
1026
1027
1028
1029
1030
1031
1032
1033 if ((ch->ch_flag & CH_PRON) != 0) {
1034 drp_wmove(ch, 0, ch->ch_digi.digi_offstr,
1035 ch->ch_digi.digi_offlen);
1036 ch->ch_flag &= ~CH_PRON;
1037 sent_printer_offstr = 1;
1038 }
1039 }
1040
1041
1042
1043
1044
1045
1046 tpos = ch->ch_s_tpos;
1047
1048 waketime = jiffies + 15 * HZ;
1049
1050 for (;;) {
1051
1052
1053
1054
1055
1056 if (port >= nd->nd_chan_count) {
1057 err = 1;
1058 break;
1059 }
1060
1061 if (signal_pending(current)) {
1062 err = 1;
1063 break;
1064 }
1065
1066
1067
1068
1069
1070
1071 if (ch->ch_state == CS_IDLE)
1072 break;
1073
1074 nd->nd_tx_work = 1;
1075
1076
1077
1078
1079
1080
1081
1082 if ((un->un_tty)->ops->chars_in_buffer ?
1083 ((un->un_tty)->ops->chars_in_buffer)(un->un_tty) == 0 : 1) {
1084
1085
1086
1087
1088
1089
1090 if (ch->ch_open_count != un->un_open_count)
1091 break;
1092
1093
1094
1095
1096
1097
1098 if (ch->ch_tin == ch->ch_tout &&
1099 ch->ch_s_tin == ch->ch_s_tpos &&
1100 (ch->ch_send & RR_TX_BREAK) == 0) {
1101 break;
1102 }
1103 }
1104
1105
1106
1107
1108
1109
1110
1111 if ((file->f_flags & (O_NDELAY | O_NONBLOCK)) ||
1112 ((long)(jiffies - waketime) >= 0 &&
1113 (ch->ch_digi.digi_flags & DIGI_PRINTER) == 0)) {
1114
1115
1116
1117
1118
1119
1120 if (!sent_printer_offstr)
1121 dgrp_tty_flush_buffer(tty);
1122
1123 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1124 tty_ldisc_flush(tty);
1125 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1126 break;
1127 }
1128
1129
1130
1131
1132
1133 ch->ch_flag |= CH_DRAIN;
1134
1135 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1136
1137 schedule_timeout_interruptible(1);
1138 s = signal_pending(current);
1139
1140 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1141
1142 if (s) {
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161 if (sent_printer_offstr) {
1162 spin_unlock_irqrestore(&nd->nd_lock,
1163 lock_flags);
1164 drp_my_sleep(ch);
1165 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1166 } else {
1167 err = 1;
1168 break;
1169 }
1170 }
1171
1172
1173
1174
1175
1176 if (ch->ch_s_tpos != tpos) {
1177 tpos = ch->ch_s_tpos;
1178
1179
1180 waketime = jiffies + 15 * HZ;
1181 }
1182 }
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200 if (!IS_PRINT(MINOR(tty_devnum(tty))))
1201 ch->ch_rout = ch->ch_rin;
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216 if ((err != 1) && (ch->ch_state != CS_IDLE)) {
1217 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1218 s = wait_event_interruptible(ch->ch_flag_wait,
1219 ((ch->ch_flag & (CH_WAITING_SYNC | CH_PARAM)) == 0));
1220 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1221 }
1222
1223
1224
1225
1226
1227 if (ch->ch_open_count == 1) {
1228 ch->ch_flag = 0;
1229 ch->ch_category = 0;
1230 ch->ch_send = 0;
1231 ch->ch_expect = 0;
1232 ch->ch_tout = ch->ch_tin;
1233
1234
1235 if (ch->ch_state == CS_READY)
1236 ch->ch_state = CS_SEND_CLOSE;
1237 }
1238
1239
1240
1241
1242 if (ch->ch_state != CS_IDLE) {
1243 ch->ch_flag |= CH_PARAM;
1244 wake_up_interruptible(&ch->ch_flag_wait);
1245 }
1246
1247 nd->nd_tx_work = 1;
1248 nd->nd_tx_ready = 1;
1249
1250unlock:
1251 tty->closing = 0;
1252
1253 if (ch->ch_open_count <= 0)
1254 dev_info(tty->dev,
1255 "%s - unexpected value for ch->ch_open_count: %i\n",
1256 __func__, ch->ch_open_count);
1257 else
1258 ch->ch_open_count--;
1259
1260 if (un->un_open_count <= 0)
1261 dev_info(tty->dev,
1262 "%s - unexpected value for un->un_open_count: %i\n",
1263 __func__, un->un_open_count);
1264 else
1265 un->un_open_count--;
1266
1267 un->un_flag &= ~(UN_NORMAL_ACTIVE | UN_CALLOUT_ACTIVE | UN_CLOSING);
1268 if (waitqueue_active(&un->un_close_wait))
1269 wake_up_interruptible(&un->un_close_wait);
1270
1271 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1272
1273 return;
1274
1275}
1276
1277static void drp_wmove(struct ch_struct *ch, int from_user, void *buf, int count)
1278{
1279 int n;
1280 int ret = 0;
1281
1282 ch->ch_nd->nd_tx_work = 1;
1283
1284 n = TBUF_MAX - ch->ch_tin;
1285
1286 if (count >= n) {
1287 if (from_user)
1288 ret = copy_from_user(ch->ch_tbuf + ch->ch_tin,
1289 (void __user *) buf, n);
1290 else
1291 memcpy(ch->ch_tbuf + ch->ch_tin, buf, n);
1292
1293 buf = (char *) buf + n;
1294 count -= n;
1295 ch->ch_tin = 0;
1296 }
1297
1298 if (from_user)
1299 ret = copy_from_user(ch->ch_tbuf + ch->ch_tin,
1300 (void __user *) buf, count);
1301 else
1302 memcpy(ch->ch_tbuf + ch->ch_tin, buf, count);
1303
1304 ch->ch_tin += count;
1305}
1306
1307
1308static int dgrp_calculate_txprint_bounds(struct ch_struct *ch, int space,
1309 int *un_flag)
1310{
1311 clock_t tt;
1312 clock_t mt;
1313 unsigned short tmax = 0;
1314
1315
1316
1317
1318
1319
1320 if (ch->ch_tun.un_open_count != 0 &&
1321 ch->ch_tun.un_tty->ops->chars_in_buffer &&
1322 ((ch->ch_tun.un_tty->ops->chars_in_buffer)
1323 (ch->ch_tun.un_tty) != 0)) {
1324 *un_flag = UN_PWAIT;
1325 return 0;
1326 }
1327
1328
1329
1330
1331
1332
1333 space -= ch->ch_digi.digi_offlen;
1334
1335 if (space <= 0) {
1336 *un_flag = UN_EMPTY;
1337 return 0;
1338 }
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361 tt = jiffies - ch->ch_cpstime;
1362
1363
1364
1365
1366
1367
1368 mt = HZ * ch->ch_digi.digi_bufsize / ch->ch_digi.digi_maxcps;
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378 if ((clock_t)(tt + HZ) > (clock_t)(mt + HZ)) {
1379 tmax = ch->ch_digi.digi_bufsize;
1380 ch->ch_cpstime = jiffies - mt;
1381 } else {
1382 tmax = ch->ch_digi.digi_maxcps * tt / HZ;
1383 }
1384
1385
1386
1387
1388
1389
1390
1391 if (tmax < space) {
1392 *un_flag = UN_TIME;
1393 space = tmax;
1394 }
1395
1396
1397
1398
1399
1400
1401
1402 tmax = (ch->ch_digi.digi_maxchar -
1403 ((ch->ch_tin - ch->ch_tout) & TBUF_MASK) -
1404 ((ch->ch_s_tin - ch->ch_s_tpos) & 0xffff));
1405
1406
1407
1408
1409
1410
1411
1412
1413 if (space > tmax) {
1414 *un_flag = UN_EMPTY;
1415 space = tmax;
1416 }
1417
1418 if (space <= 0)
1419 *un_flag |= UN_EMPTY;
1420
1421 return space;
1422}
1423
1424
1425static int dgrp_tty_write(struct tty_struct *tty,
1426 const unsigned char *buf,
1427 int count)
1428{
1429 struct nd_struct *nd;
1430 struct un_struct *un;
1431 struct ch_struct *ch;
1432 int space;
1433 int n;
1434 int t;
1435 int sendcount;
1436 int un_flag;
1437 ulong lock_flags;
1438
1439 if (tty == NULL)
1440 return 0;
1441
1442 un = tty->driver_data;
1443 if (!un)
1444 return 0;
1445
1446 ch = un->un_ch;
1447 if (!ch)
1448 return 0;
1449
1450 nd = ch->ch_nd;
1451 if (!nd)
1452 return 0;
1453
1454
1455
1456
1457 if (ch->ch_state != CS_READY)
1458 return 0;
1459
1460 spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
1461
1462
1463
1464
1465 if ((un->un_flag & (UN_EMPTY | UN_LOW | UN_TIME | UN_PWAIT)) != 0) {
1466 count = 0;
1467 goto out;
1468 }
1469
1470
1471
1472
1473
1474 if (nd->nd_dpa_debug && nd->nd_dpa_flag & DPA_WAIT_SPACE &&
1475 nd->nd_dpa_port == MINOR(tty_devnum(ch->ch_tun.un_tty))) {
1476 count = 0;
1477 goto out;
1478 }
1479
1480
1481
1482
1483
1484 sendcount = 0;
1485
1486 space = (ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK;
1487
1488
1489
1490
1491
1492 un_flag = UN_LOW;
1493
1494 if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1495 clock_t tt;
1496 clock_t mt;
1497 unsigned short tmax = 0;
1498
1499
1500
1501
1502
1503
1504 if (ch->ch_tun.un_open_count != 0 &&
1505 ((ch->ch_tun.un_tty->ops->chars_in_buffer)
1506 (ch->ch_tun.un_tty) != 0)) {
1507 un->un_flag |= UN_PWAIT;
1508 count = 0;
1509 goto out;
1510 }
1511
1512
1513
1514
1515
1516
1517 space -= ch->ch_digi.digi_offlen;
1518
1519
1520
1521
1522
1523 if ((ch->ch_flag & CH_PRON) == 0) {
1524 space -= ch->ch_digi.digi_onlen;
1525
1526 if (space < 0) {
1527 un->un_flag |= UN_EMPTY;
1528 (ch->ch_nd)->nd_tx_work = 1;
1529 count = 0;
1530 goto out;
1531 }
1532
1533 drp_wmove(ch, 0, ch->ch_digi.digi_onstr,
1534 ch->ch_digi.digi_onlen);
1535
1536 ch->ch_flag |= CH_PRON;
1537 }
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560 tt = jiffies - ch->ch_cpstime;
1561
1562
1563
1564
1565
1566
1567 mt = HZ * ch->ch_digi.digi_bufsize / ch->ch_digi.digi_maxcps;
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577 if ((clock_t)(tt + HZ) > (clock_t)(mt + HZ)) {
1578 tmax = ch->ch_digi.digi_bufsize;
1579 ch->ch_cpstime = jiffies - mt;
1580 } else {
1581 tmax = ch->ch_digi.digi_maxcps * tt / HZ;
1582 }
1583
1584
1585
1586
1587
1588
1589
1590 if (tmax < space) {
1591 space = tmax;
1592 un_flag = UN_TIME;
1593 }
1594
1595
1596
1597
1598
1599
1600
1601 tmax = (ch->ch_digi.digi_maxchar -
1602 ((ch->ch_tin - ch->ch_tout) & TBUF_MASK) -
1603 ((ch->ch_s_tin - ch->ch_s_tpos) & 0xffff));
1604
1605
1606
1607
1608
1609
1610
1611
1612 if (space > tmax) {
1613 space = tmax;
1614 un_flag = UN_EMPTY;
1615 }
1616
1617 }
1618
1619
1620
1621 else {
1622
1623
1624
1625
1626
1627 if ((ch->ch_flag & CH_PRON) != 0) {
1628
1629 space -= ch->ch_digi.digi_offlen;
1630
1631 drp_wmove(ch, 0, ch->ch_digi.digi_offstr,
1632 ch->ch_digi.digi_offlen);
1633
1634 ch->ch_flag &= ~CH_PRON;
1635 }
1636 }
1637
1638
1639
1640
1641
1642
1643
1644
1645 if (space <= 0) {
1646
1647
1648
1649 un->un_flag |= UN_EMPTY;
1650 (ch->ch_nd)->nd_tx_work = 1;
1651 count = 0;
1652 goto out;
1653 }
1654
1655 count = min(count, space);
1656
1657 if (count > 0) {
1658
1659 un->un_tbusy++;
1660
1661
1662
1663
1664
1665
1666 t = TBUF_MAX - ch->ch_tin;
1667 n = count;
1668
1669 if (n >= t) {
1670 memcpy(ch->ch_tbuf + ch->ch_tin, buf, t);
1671 if (nd->nd_dpa_debug && nd->nd_dpa_port ==
1672 PORT_NUM(MINOR(tty_devnum(un->un_tty))))
1673 dgrp_dpa_data(nd, 0, (char *) buf, t);
1674 buf += t;
1675 n -= t;
1676 ch->ch_tin = 0;
1677 sendcount += n;
1678 }
1679
1680 memcpy(ch->ch_tbuf + ch->ch_tin, buf, n);
1681 if (nd->nd_dpa_debug && nd->nd_dpa_port ==
1682 PORT_NUM(MINOR(tty_devnum(un->un_tty))))
1683 dgrp_dpa_data(nd, 0, (char *) buf, n);
1684 buf += n;
1685 ch->ch_tin += n;
1686 sendcount += n;
1687
1688 un->un_tbusy--;
1689 (ch->ch_nd)->nd_tx_work = 1;
1690 if (ch->ch_edelay != DGRP_RTIME) {
1691 (ch->ch_nd)->nd_tx_ready = 1;
1692 wake_up_interruptible(&nd->nd_tx_waitq);
1693 }
1694 }
1695
1696 ch->ch_txcount += count;
1697
1698 if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1699
1700
1701
1702
1703
1704
1705 if (sendcount > 0) {
1706 int cc = HZ * sendcount + ch->ch_cpsrem;
1707
1708 ch->ch_cpstime += cc / ch->ch_digi.digi_maxcps;
1709 ch->ch_cpsrem = cc % ch->ch_digi.digi_maxcps;
1710 }
1711
1712
1713
1714
1715
1716
1717
1718 if ((un_flag & UN_TIME) != 0) {
1719 ch->ch_waketime = (ch->ch_cpstime +
1720 (ch->ch_digi.digi_maxchar * HZ /
1721 ch->ch_digi.digi_maxcps));
1722 }
1723 }
1724
1725
1726
1727
1728
1729
1730 if ((ch->ch_pun.un_flag & UN_PWAIT) != 0)
1731 (ch->ch_nd)->nd_tx_work = 1;
1732
1733out:
1734 spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
1735
1736 return count;
1737}
1738
1739
1740
1741
1742
1743
1744
1745
1746static int dgrp_tty_put_char(struct tty_struct *tty, unsigned char new_char)
1747{
1748 struct un_struct *un;
1749 struct ch_struct *ch;
1750 ulong lock_flags;
1751 int space;
1752 int retval = 0;
1753
1754 if (tty == NULL)
1755 return 0;
1756
1757 un = tty->driver_data;
1758 if (!un)
1759 return 0;
1760
1761 ch = un->un_ch;
1762 if (!ch)
1763 return 0;
1764
1765 if (ch->ch_state != CS_READY)
1766 return 0;
1767
1768 spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
1769
1770
1771
1772
1773
1774
1775
1776
1777 space = (ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK;
1778
1779 un->un_tbusy++;
1780
1781
1782
1783
1784 if (IS_PRINT(MINOR(tty_devnum(tty))) && (ch->ch_flag & CH_PRON) == 0) {
1785 if (space < ch->ch_digi.digi_onlen) {
1786 un->un_tbusy--;
1787 goto out;
1788 }
1789 space -= ch->ch_digi.digi_onlen;
1790 drp_wmove(ch, 0, ch->ch_digi.digi_onstr,
1791 ch->ch_digi.digi_onlen);
1792 ch->ch_flag |= CH_PRON;
1793 }
1794
1795
1796
1797
1798
1799 if (!IS_PRINT(MINOR(tty_devnum(tty))) &&
1800 ((ch->ch_flag & CH_PRON) != 0)) {
1801 if (space < ch->ch_digi.digi_offlen) {
1802 un->un_tbusy--;
1803 goto out;
1804 }
1805
1806 space -= ch->ch_digi.digi_offlen;
1807 drp_wmove(ch, 0, ch->ch_digi.digi_offstr,
1808 ch->ch_digi.digi_offlen);
1809 ch->ch_flag &= ~CH_PRON;
1810 }
1811
1812 if (!space) {
1813 un->un_tbusy--;
1814 goto out;
1815 }
1816
1817
1818
1819
1820
1821 ch->ch_tbuf[ch->ch_tin] = new_char;
1822 ch->ch_tin = (1 + ch->ch_tin) & TBUF_MASK;
1823
1824 if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1825
1826
1827
1828
1829
1830
1831 int cc = HZ + ch->ch_cpsrem;
1832
1833 ch->ch_cpstime += cc / ch->ch_digi.digi_maxcps;
1834 ch->ch_cpsrem = cc % ch->ch_digi.digi_maxcps;
1835
1836
1837
1838
1839
1840
1841
1842 ch->ch_waketime = (ch->ch_cpstime +
1843 (ch->ch_digi.digi_maxchar * HZ /
1844 ch->ch_digi.digi_maxcps));
1845 }
1846
1847
1848 un->un_tbusy--;
1849 (ch->ch_nd)->nd_tx_work = 1;
1850
1851 retval = 1;
1852out:
1853 spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
1854 return retval;
1855}
1856
1857
1858
1859
1860
1861
1862
1863
1864static void dgrp_tty_flush_buffer(struct tty_struct *tty)
1865{
1866 struct un_struct *un;
1867 struct ch_struct *ch;
1868
1869 if (!tty)
1870 return;
1871 un = tty->driver_data;
1872 if (!un)
1873 return;
1874
1875 ch = un->un_ch;
1876 if (!ch)
1877 return;
1878
1879 ch->ch_tout = ch->ch_tin;
1880
1881
1882
1883
1884 ch->ch_send |= RR_TX_FLUSH;
1885 (ch->ch_nd)->nd_tx_ready = 1;
1886 (ch->ch_nd)->nd_tx_work = 1;
1887 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
1888
1889 if (waitqueue_active(&tty->write_wait))
1890 wake_up_interruptible(&tty->write_wait);
1891
1892 tty_wakeup(tty);
1893
1894}
1895
1896
1897
1898
1899
1900static int dgrp_tty_write_room(struct tty_struct *tty)
1901{
1902 struct un_struct *un;
1903 struct ch_struct *ch;
1904 int count;
1905
1906 if (!tty)
1907 return 0;
1908
1909 un = tty->driver_data;
1910 if (!un)
1911 return 0;
1912
1913 ch = un->un_ch;
1914 if (!ch)
1915 return 0;
1916
1917 count = (ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK;
1918
1919
1920
1921
1922
1923
1924 if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1925 int un_flag = 0;
1926 count = dgrp_calculate_txprint_bounds(ch, count, &un_flag);
1927 if (count <= 0)
1928 count = 0;
1929
1930 ch->ch_pun.un_flag |= un_flag;
1931 (ch->ch_nd)->nd_tx_work = 1;
1932 }
1933
1934 return count;
1935}
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947static int dgrp_tty_chars_in_buffer(struct tty_struct *tty)
1948{
1949 struct un_struct *un;
1950 struct ch_struct *ch;
1951 int count;
1952 int count1;
1953
1954 if (!tty)
1955 return 0;
1956
1957 un = tty->driver_data;
1958 if (!un)
1959 return 0;
1960
1961 ch = un->un_ch;
1962 if (!ch)
1963 return 0;
1964
1965 count1 = count = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1966 count += (ch->ch_s_tin - ch->ch_s_tpos) & 0xffff;
1967
1968
1969
1970
1971
1972 count += un->un_tbusy;
1973
1974 return count;
1975}
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994static tcflag_t ch_to_tty_flags(ushort ch_flag, char flagtype)
1995{
1996 tcflag_t retval = 0;
1997
1998 switch (flagtype) {
1999 case 'i':
2000 retval = ((ch_flag & IF_IGNBRK) ? IGNBRK : 0)
2001 | ((ch_flag & IF_BRKINT) ? BRKINT : 0)
2002 | ((ch_flag & IF_IGNPAR) ? IGNPAR : 0)
2003 | ((ch_flag & IF_PARMRK) ? PARMRK : 0)
2004 | ((ch_flag & IF_INPCK) ? INPCK : 0)
2005 | ((ch_flag & IF_ISTRIP) ? ISTRIP : 0)
2006 | ((ch_flag & IF_IXON) ? IXON : 0)
2007 | ((ch_flag & IF_IXANY) ? IXANY : 0)
2008 | ((ch_flag & IF_IXOFF) ? IXOFF : 0);
2009 break;
2010
2011 case 'o':
2012 retval = ((ch_flag & OF_OLCUC) ? OLCUC : 0)
2013 | ((ch_flag & OF_ONLCR) ? ONLCR : 0)
2014 | ((ch_flag & OF_OCRNL) ? OCRNL : 0)
2015 | ((ch_flag & OF_ONOCR) ? ONOCR : 0)
2016 | ((ch_flag & OF_ONLRET) ? ONLRET : 0)
2017
2018 | ((ch_flag & OF_TABDLY) ? TABDLY : 0);
2019 break;
2020
2021 case 'c':
2022 retval = ((ch_flag & CF_CSTOPB) ? CSTOPB : 0)
2023 | ((ch_flag & CF_CREAD) ? CREAD : 0)
2024 | ((ch_flag & CF_PARENB) ? PARENB : 0)
2025 | ((ch_flag & CF_PARODD) ? PARODD : 0)
2026 | ((ch_flag & CF_HUPCL) ? HUPCL : 0);
2027
2028 switch (ch_flag & CF_CSIZE) {
2029 case CF_CS5:
2030 retval |= CS5;
2031 break;
2032 case CF_CS6:
2033 retval |= CS6;
2034 break;
2035 case CF_CS7:
2036 retval |= CS7;
2037 break;
2038 case CF_CS8:
2039 retval |= CS8;
2040 break;
2041 default:
2042 retval |= CS8;
2043 break;
2044 }
2045 break;
2046 case 'x':
2047 break;
2048 case 'l':
2049 break;
2050 default:
2051 return 0;
2052 }
2053
2054 return retval;
2055}
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067static ushort tty_to_ch_flags(struct tty_struct *tty, char flagtype)
2068{
2069 ushort retval = 0;
2070 tcflag_t tflag = 0;
2071
2072 switch (flagtype) {
2073 case 'i':
2074 tflag = tty->termios.c_iflag;
2075 retval = (I_IGNBRK(tty) ? IF_IGNBRK : 0)
2076 | (I_BRKINT(tty) ? IF_BRKINT : 0)
2077 | (I_IGNPAR(tty) ? IF_IGNPAR : 0)
2078 | (I_PARMRK(tty) ? IF_PARMRK : 0)
2079 | (I_INPCK(tty) ? IF_INPCK : 0)
2080 | (I_ISTRIP(tty) ? IF_ISTRIP : 0)
2081 | (I_IXON(tty) ? IF_IXON : 0)
2082 | (I_IXANY(tty) ? IF_IXANY : 0)
2083 | (I_IXOFF(tty) ? IF_IXOFF : 0);
2084 break;
2085 case 'o':
2086 tflag = tty->termios.c_oflag;
2087
2088
2089
2090
2091
2092
2093 if (!O_OPOST(tty))
2094 retval = 0;
2095 else
2096 retval = (O_OLCUC(tty) ? OF_OLCUC : 0)
2097 | (O_ONLCR(tty) ? OF_ONLCR : 0)
2098 | (O_OCRNL(tty) ? OF_OCRNL : 0)
2099 | (O_ONOCR(tty) ? OF_ONOCR : 0)
2100 | (O_ONLRET(tty) ? OF_ONLRET : 0)
2101
2102 | (O_TABDLY(tty) ? OF_TABDLY : 0);
2103 break;
2104 case 'c':
2105 tflag = tty->termios.c_cflag;
2106 retval = (C_CSTOPB(tty) ? CF_CSTOPB : 0)
2107 | (C_CREAD(tty) ? CF_CREAD : 0)
2108 | (C_PARENB(tty) ? CF_PARENB : 0)
2109 | (C_PARODD(tty) ? CF_PARODD : 0)
2110 | (C_HUPCL(tty) ? CF_HUPCL : 0);
2111 switch (C_CSIZE(tty)) {
2112 case CS8:
2113 retval |= CF_CS8;
2114 break;
2115 case CS7:
2116 retval |= CF_CS7;
2117 break;
2118 case CS6:
2119 retval |= CF_CS6;
2120 break;
2121 case CS5:
2122 retval |= CF_CS5;
2123 break;
2124 default:
2125 retval |= CF_CS8;
2126 break;
2127 }
2128 break;
2129 case 'x':
2130 break;
2131 case 'l':
2132 break;
2133 default:
2134 return 0;
2135 }
2136
2137 return retval;
2138}
2139
2140
2141static int dgrp_tty_send_break(struct tty_struct *tty, int msec)
2142{
2143 struct un_struct *un;
2144 struct ch_struct *ch;
2145 int ret = -EIO;
2146
2147 if (!tty)
2148 return ret;
2149
2150 un = tty->driver_data;
2151 if (!un)
2152 return ret;
2153
2154 ch = un->un_ch;
2155 if (!ch)
2156 return ret;
2157
2158 dgrp_send_break(ch, msec);
2159 return 0;
2160}
2161
2162
2163
2164
2165
2166
2167
2168static int dgrp_send_break(struct ch_struct *ch, int msec)
2169{
2170 ulong x;
2171
2172 wait_event_interruptible(ch->ch_flag_wait,
2173 ((ch->ch_flag & CH_TX_BREAK) == 0));
2174 ch->ch_break_time += max(msec, 250);
2175 ch->ch_send |= RR_TX_BREAK;
2176 ch->ch_flag |= CH_TX_BREAK;
2177 (ch->ch_nd)->nd_tx_work = 1;
2178
2179 x = (msec * HZ) / 1000;
2180 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2181
2182 return 0;
2183}
2184
2185
2186
2187
2188
2189static int dgrp_tty_tiocmget(struct tty_struct *tty)
2190{
2191 unsigned int mlast;
2192 struct un_struct *un = tty->driver_data;
2193 struct ch_struct *ch;
2194
2195 if (!un)
2196 return -ENODEV;
2197
2198 ch = un->un_ch;
2199 if (!ch)
2200 return -ENODEV;
2201
2202 mlast = ((ch->ch_s_mlast & ~(DM_RTS | DM_DTR)) |
2203 (ch->ch_mout & (DM_RTS | DM_DTR)));
2204
2205
2206 mlast = ((mlast & DM_RTS) ? TIOCM_RTS : 0)
2207 | ((mlast & DM_DTR) ? TIOCM_DTR : 0)
2208 | ((mlast & DM_CD) ? TIOCM_CAR : 0)
2209 | ((mlast & DM_RI) ? TIOCM_RNG : 0)
2210 | ((mlast & DM_DSR) ? TIOCM_DSR : 0)
2211 | ((mlast & DM_CTS) ? TIOCM_CTS : 0);
2212
2213 return mlast;
2214}
2215
2216
2217
2218
2219
2220static int dgrp_tty_tiocmset(struct tty_struct *tty,
2221 unsigned int set, unsigned int clear)
2222{
2223 ulong lock_flags;
2224 struct un_struct *un = tty->driver_data;
2225 struct ch_struct *ch;
2226
2227 if (!un)
2228 return -ENODEV;
2229
2230 ch = un->un_ch;
2231 if (!ch)
2232 return -ENODEV;
2233
2234 if (set & TIOCM_RTS)
2235 ch->ch_mout |= DM_RTS;
2236
2237 if (set & TIOCM_DTR)
2238 ch->ch_mout |= DM_DTR;
2239
2240 if (clear & TIOCM_RTS)
2241 ch->ch_mout &= ~(DM_RTS);
2242
2243 if (clear & TIOCM_DTR)
2244 ch->ch_mout &= ~(DM_DTR);
2245
2246 spin_lock_irqsave(&(ch->ch_nd)->nd_lock, lock_flags);
2247 ch->ch_flag |= CH_PARAM;
2248 (ch->ch_nd)->nd_tx_work = 1;
2249 wake_up_interruptible(&ch->ch_flag_wait);
2250
2251 spin_unlock_irqrestore(&(ch->ch_nd)->nd_lock, lock_flags);
2252
2253 return 0;
2254}
2255
2256
2257
2258
2259
2260
2261static int get_modem_info(struct ch_struct *ch, unsigned int *value)
2262{
2263 unsigned int mlast;
2264
2265 mlast = ((ch->ch_s_mlast & ~(DM_RTS | DM_DTR)) |
2266 (ch->ch_mout & (DM_RTS | DM_DTR)));
2267
2268
2269 mlast = ((mlast & DM_RTS) ? TIOCM_RTS : 0)
2270 | ((mlast & DM_DTR) ? TIOCM_DTR : 0)
2271 | ((mlast & DM_CD) ? TIOCM_CAR : 0)
2272 | ((mlast & DM_RI) ? TIOCM_RNG : 0)
2273 | ((mlast & DM_DSR) ? TIOCM_DSR : 0)
2274 | ((mlast & DM_CTS) ? TIOCM_CTS : 0);
2275 return put_user(mlast, (unsigned int __user *) value);
2276}
2277
2278
2279
2280
2281static int set_modem_info(struct ch_struct *ch, unsigned int command,
2282 unsigned int *value)
2283{
2284 int error;
2285 unsigned int arg;
2286 int mval = 0;
2287 ulong lock_flags;
2288
2289 error = access_ok(VERIFY_READ, (void __user *) value, sizeof(int));
2290 if (error == 0)
2291 return -EFAULT;
2292
2293 if (get_user(arg, (unsigned int __user *) value))
2294 return -EFAULT;
2295 mval |= ((arg & TIOCM_RTS) ? DM_RTS : 0)
2296 | ((arg & TIOCM_DTR) ? DM_DTR : 0);
2297
2298 switch (command) {
2299 case TIOCMBIS:
2300 ch->ch_mout |= mval;
2301 break;
2302 case TIOCMBIC:
2303 ch->ch_mout &= ~mval;
2304 break;
2305 case TIOCMSET:
2306 ch->ch_mout = mval;
2307 break;
2308 default:
2309 return -EINVAL;
2310 }
2311
2312 spin_lock_irqsave(&(ch->ch_nd)->nd_lock, lock_flags);
2313
2314 ch->ch_flag |= CH_PARAM;
2315 (ch->ch_nd)->nd_tx_work = 1;
2316 wake_up_interruptible(&ch->ch_flag_wait);
2317
2318 spin_unlock_irqrestore(&(ch->ch_nd)->nd_lock, lock_flags);
2319
2320 return 0;
2321}
2322
2323
2324
2325
2326
2327static void dgrp_set_custom_speed(struct ch_struct *ch, int newrate)
2328{
2329 int testdiv;
2330 int testrate_high;
2331 int testrate_low;
2332
2333 int deltahigh, deltalow;
2334
2335 if (newrate < 0)
2336 newrate = 0;
2337
2338
2339
2340
2341
2342
2343 if (newrate && newrate < ((PORTSERVER_DIVIDEND / 0xFFFF) + 1))
2344 newrate = ((PORTSERVER_DIVIDEND / 0xFFFF) + 1);
2345 if (newrate && newrate > PORTSERVER_DIVIDEND)
2346 newrate = PORTSERVER_DIVIDEND;
2347
2348 while (newrate > 0) {
2349 testdiv = PORTSERVER_DIVIDEND / newrate;
2350
2351
2352
2353
2354
2355
2356
2357
2358 testrate_high = PORTSERVER_DIVIDEND / testdiv;
2359 testrate_low = PORTSERVER_DIVIDEND / (testdiv + 1);
2360
2361
2362
2363
2364
2365 if (testrate_high == newrate)
2366 break;
2367
2368
2369
2370
2371
2372 deltahigh = testrate_high - newrate;
2373 deltalow = newrate - testrate_low;
2374
2375 if (deltahigh < deltalow)
2376 newrate = testrate_high;
2377 else
2378 newrate = testrate_low;
2379
2380 break;
2381 }
2382
2383 ch->ch_custom_speed = newrate;
2384
2385 drp_param(ch);
2386
2387 return;
2388}
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398static int dgrp_tty_digiseta(struct tty_struct *tty,
2399 struct digi_struct *new_info)
2400{
2401 struct un_struct *un = tty->driver_data;
2402 struct ch_struct *ch;
2403
2404 if (!un)
2405 return -ENODEV;
2406
2407 ch = un->un_ch;
2408 if (!ch)
2409 return -ENODEV;
2410
2411 if (copy_from_user(&ch->ch_digi, (void __user *) new_info,
2412 sizeof(struct digi_struct)))
2413 return -EFAULT;
2414
2415 if ((ch->ch_digi.digi_flags & RTSPACE) ||
2416 (ch->ch_digi.digi_flags & CTSPACE))
2417 tty->termios.c_cflag |= CRTSCTS;
2418 else
2419 tty->termios.c_cflag &= ~CRTSCTS;
2420
2421 if (ch->ch_digi.digi_maxcps < 1)
2422 ch->ch_digi.digi_maxcps = 1;
2423
2424 if (ch->ch_digi.digi_maxcps > 10000)
2425 ch->ch_digi.digi_maxcps = 10000;
2426
2427 if (ch->ch_digi.digi_bufsize < 10)
2428 ch->ch_digi.digi_bufsize = 10;
2429
2430 if (ch->ch_digi.digi_maxchar < 1)
2431 ch->ch_digi.digi_maxchar = 1;
2432
2433 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2434 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2435
2436 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2437 ch->ch_digi.digi_onlen = DIGI_PLEN;
2438
2439 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2440 ch->ch_digi.digi_offlen = DIGI_PLEN;
2441
2442
2443 drp_param(ch);
2444
2445 return 0;
2446}
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458static int dgrp_tty_digigetedelay(struct tty_struct *tty, int *retinfo)
2459{
2460 struct un_struct *un;
2461 struct ch_struct *ch;
2462 int tmp;
2463
2464 if (!retinfo)
2465 return -EFAULT;
2466
2467 if (!tty || tty->magic != TTY_MAGIC)
2468 return -EFAULT;
2469
2470 un = tty->driver_data;
2471
2472 if (!un)
2473 return -ENODEV;
2474
2475 ch = un->un_ch;
2476 if (!ch)
2477 return -ENODEV;
2478
2479 tmp = ch->ch_edelay;
2480
2481 if (copy_to_user((void __user *) retinfo, &tmp, sizeof(*retinfo)))
2482 return -EFAULT;
2483
2484 return 0;
2485}
2486
2487
2488
2489
2490
2491
2492
2493
2494static int dgrp_tty_digisetedelay(struct tty_struct *tty, int *new_info)
2495{
2496 struct un_struct *un;
2497 struct ch_struct *ch;
2498 int new_digi;
2499
2500 if (!tty || tty->magic != TTY_MAGIC)
2501 return -EFAULT;
2502
2503 un = tty->driver_data;
2504
2505 if (!un)
2506 return -ENODEV;
2507
2508 ch = un->un_ch;
2509 if (!ch)
2510 return -ENODEV;
2511
2512 if (copy_from_user(&new_digi, (void __user *)new_info, sizeof(int)))
2513 return -EFAULT;
2514
2515 ch->ch_edelay = new_digi;
2516
2517
2518 drp_param(ch);
2519
2520 return 0;
2521}
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533static int dgrp_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2534 unsigned long arg)
2535{
2536 struct un_struct *un;
2537 struct ch_struct *ch;
2538 int rc;
2539 struct digiflow_struct dflow;
2540
2541 if (!tty)
2542 return -ENODEV;
2543
2544 un = tty->driver_data;
2545 if (!un)
2546 return -ENODEV;
2547
2548 ch = un->un_ch;
2549 if (!ch)
2550 return -ENODEV;
2551
2552 switch (cmd) {
2553
2554
2555
2556
2557
2558 case TCSBRK:
2559
2560
2561
2562
2563
2564
2565
2566
2567 rc = tty_check_change(tty);
2568 if (rc)
2569 return rc;
2570 tty_wait_until_sent(tty, 0);
2571
2572 if (!arg)
2573 rc = dgrp_send_break(ch, 250);
2574
2575 if (dgrp_tty_chars_in_buffer(tty) != 0)
2576 return -EINTR;
2577
2578 return 0;
2579
2580 case TCSBRKP:
2581
2582
2583
2584
2585
2586
2587 rc = tty_check_change(tty);
2588 if (rc)
2589 return rc;
2590 tty_wait_until_sent(tty, 0);
2591
2592 rc = dgrp_send_break(ch, arg ? arg*250 : 250);
2593
2594 if (dgrp_tty_chars_in_buffer(tty) != 0)
2595 return -EINTR;
2596 return 0;
2597
2598 case TIOCSBRK:
2599 rc = tty_check_change(tty);
2600 if (rc)
2601 return rc;
2602 tty_wait_until_sent(tty, 0);
2603
2604
2605
2606
2607
2608
2609 rc = dgrp_send_break(ch, 250);
2610
2611 if (dgrp_tty_chars_in_buffer(tty) != 0)
2612 return -EINTR;
2613 return 0;
2614
2615 case TIOCCBRK:
2616
2617
2618
2619
2620
2621
2622 return 0;
2623
2624 case TIOCMGET:
2625 rc = access_ok(VERIFY_WRITE, (void __user *) arg,
2626 sizeof(unsigned int));
2627 if (rc == 0)
2628 return -EFAULT;
2629 return get_modem_info(ch, (unsigned int *) arg);
2630
2631 case TIOCMBIS:
2632 case TIOCMBIC:
2633 case TIOCMSET:
2634 return set_modem_info(ch, cmd, (unsigned int *) arg);
2635
2636
2637
2638
2639
2640 case TCFLSH:
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650 rc = tty_check_change(tty);
2651 if (rc)
2652 return rc;
2653
2654 switch (arg) {
2655 case TCIFLUSH:
2656 case TCIOFLUSH:
2657
2658 if (!IS_PRINT(MINOR(tty_devnum(tty)))) {
2659 ch->ch_rout = ch->ch_rin;
2660 ch->ch_send |= RR_RX_FLUSH;
2661 (ch->ch_nd)->nd_tx_work = 1;
2662 (ch->ch_nd)->nd_tx_ready = 1;
2663 wake_up_interruptible(
2664 &(ch->ch_nd)->nd_tx_waitq);
2665 }
2666 if (arg == TCIFLUSH)
2667 break;
2668
2669 case TCOFLUSH:
2670
2671
2672
2673
2674 break;
2675
2676 default:
2677
2678 return -EINVAL;
2679 }
2680
2681 return -ENOIOCTLCMD;
2682
2683#ifdef TIOCGETP
2684 case TIOCGETP:
2685#endif
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706 case TCGETS:
2707 case TCGETA:
2708 return -ENOIOCTLCMD;
2709
2710 case TCSETAW:
2711 case TCSETAF:
2712 case TCSETSF:
2713 case TCSETSW:
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730 if (!IS_PRINT(MINOR(tty_devnum(tty))) ||
2731 !ch->ch_tun.un_open_count) {
2732 rc = tty_check_change(tty);
2733 if (rc)
2734 return rc;
2735 }
2736
2737
2738 tty_wait_until_sent(tty, 0);
2739
2740 if ((cmd == TCSETSF) || (cmd == TCSETAF)) {
2741
2742
2743 ch->ch_send |= RR_RX_FLUSH;
2744 (ch->ch_nd)->nd_tx_ready = 1;
2745 (ch->ch_nd)->nd_tx_work = 1;
2746 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2747
2748 ch->ch_rout = ch->ch_rin;
2749 }
2750
2751
2752 return -ENOIOCTLCMD;
2753
2754 case TCXONC:
2755
2756
2757
2758
2759
2760
2761
2762 rc = tty_check_change(tty);
2763 if (rc)
2764 return rc;
2765
2766 switch (arg) {
2767 case TCOON:
2768 dgrp_tty_start(tty);
2769 return 0;
2770 case TCOOFF:
2771 dgrp_tty_stop(tty);
2772 return 0;
2773 case TCION:
2774 dgrp_tty_input_start(tty);
2775 return 0;
2776 case TCIOFF:
2777 dgrp_tty_input_stop(tty);
2778 return 0;
2779 default:
2780 return -EINVAL;
2781 }
2782
2783 case DIGI_GETA:
2784
2785 if (copy_to_user((struct digi_struct __user *) arg,
2786 &ch->ch_digi, sizeof(struct digi_struct)))
2787 return -EFAULT;
2788 break;
2789
2790 case DIGI_SETAW:
2791 case DIGI_SETAF:
2792
2793 tty_wait_until_sent(tty, 0);
2794
2795 if (cmd == DIGI_SETAF) {
2796
2797
2798 ch->ch_send |= RR_RX_FLUSH;
2799 (ch->ch_nd)->nd_tx_ready = 1;
2800 (ch->ch_nd)->nd_tx_work = 1;
2801 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2802
2803 ch->ch_rout = ch->ch_rin;
2804 }
2805
2806
2807
2808
2809 case DIGI_SETA:
2810 return dgrp_tty_digiseta(tty, (struct digi_struct *) arg);
2811
2812 case DIGI_SEDELAY:
2813 return dgrp_tty_digisetedelay(tty, (int *) arg);
2814
2815 case DIGI_GEDELAY:
2816 return dgrp_tty_digigetedelay(tty, (int *) arg);
2817
2818 case DIGI_GETFLOW:
2819 case DIGI_GETAFLOW:
2820 if (cmd == (DIGI_GETFLOW)) {
2821 dflow.startc = tty->termios.c_cc[VSTART];
2822 dflow.stopc = tty->termios.c_cc[VSTOP];
2823 } else {
2824 dflow.startc = ch->ch_xxon;
2825 dflow.stopc = ch->ch_xxoff;
2826 }
2827
2828 if (copy_to_user((char __user *)arg, &dflow, sizeof(dflow)))
2829 return -EFAULT;
2830 break;
2831
2832 case DIGI_SETFLOW:
2833 case DIGI_SETAFLOW:
2834
2835 if (copy_from_user(&dflow, (char __user *)arg, sizeof(dflow)))
2836 return -EFAULT;
2837
2838 if (cmd == (DIGI_SETFLOW)) {
2839 tty->termios.c_cc[VSTART] = dflow.startc;
2840 tty->termios.c_cc[VSTOP] = dflow.stopc;
2841 } else {
2842 ch->ch_xxon = dflow.startc;
2843 ch->ch_xxoff = dflow.stopc;
2844 }
2845 break;
2846
2847 case DIGI_GETCUSTOMBAUD:
2848 if (put_user(ch->ch_custom_speed, (unsigned int __user *) arg))
2849 return -EFAULT;
2850 break;
2851
2852 case DIGI_SETCUSTOMBAUD:
2853 {
2854 int new_rate;
2855
2856 if (get_user(new_rate, (unsigned int __user *) arg))
2857 return -EFAULT;
2858 dgrp_set_custom_speed(ch, new_rate);
2859
2860 break;
2861 }
2862
2863 default:
2864 return -ENOIOCTLCMD;
2865 }
2866
2867 return 0;
2868}
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879static void dgrp_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
2880{
2881 struct ktermios *ts;
2882 struct ch_struct *ch;
2883 struct un_struct *un;
2884
2885
2886 if (!tty)
2887 return;
2888
2889 un = tty->driver_data;
2890 if (!un)
2891 return;
2892
2893 ts = &tty->termios;
2894
2895 ch = un->un_ch;
2896 if (!ch)
2897 return;
2898
2899 drp_param(ch);
2900
2901
2902 if (!(old->c_cflag & CLOCAL) && C_CLOCAL(tty))
2903 wake_up_interruptible(&un->un_open_wait);
2904}
2905
2906
2907
2908
2909
2910
2911
2912static void dgrp_tty_throttle(struct tty_struct *tty)
2913{
2914 struct ch_struct *ch;
2915
2916 if (!tty)
2917 return;
2918
2919 ch = ((struct un_struct *) tty->driver_data)->un_ch;
2920 if (!ch)
2921 return;
2922
2923 ch->ch_flag |= CH_RXSTOP;
2924}
2925
2926
2927static void dgrp_tty_unthrottle(struct tty_struct *tty)
2928{
2929 struct ch_struct *ch;
2930
2931 if (!tty)
2932 return;
2933
2934 ch = ((struct un_struct *) tty->driver_data)->un_ch;
2935 if (!ch)
2936 return;
2937
2938 ch->ch_flag &= ~CH_RXSTOP;
2939}
2940
2941
2942
2943
2944static void dgrp_tty_stop(struct tty_struct *tty)
2945{
2946 struct ch_struct *ch;
2947
2948 if (!tty)
2949 return;
2950
2951 ch = ((struct un_struct *) tty->driver_data)->un_ch;
2952 if (!ch)
2953 return;
2954
2955 ch->ch_send |= RR_TX_STOP;
2956 ch->ch_send &= ~RR_TX_START;
2957
2958
2959 (ch->ch_nd)->nd_tx_ready = 1;
2960 if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
2961 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2962}
2963
2964
2965
2966
2967static void dgrp_tty_start(struct tty_struct *tty)
2968{
2969 struct ch_struct *ch;
2970
2971 if (!tty)
2972 return;
2973
2974 ch = ((struct un_struct *) tty->driver_data)->un_ch;
2975 if (!ch)
2976 return;
2977
2978
2979
2980 ch->ch_send |= RR_TX_START;
2981 ch->ch_send &= ~RR_TX_STOP;
2982
2983
2984 (ch->ch_nd)->nd_tx_ready = 1;
2985 (ch->ch_nd)->nd_tx_work = 1;
2986 if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
2987 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2988
2989}
2990
2991
2992
2993
2994static void dgrp_tty_input_stop(struct tty_struct *tty)
2995{
2996 struct ch_struct *ch;
2997
2998 if (!tty)
2999 return;
3000
3001 ch = ((struct un_struct *) tty->driver_data)->un_ch;
3002 if (!ch)
3003 return;
3004
3005 ch->ch_send |= RR_RX_STOP;
3006 ch->ch_send &= ~RR_RX_START;
3007 (ch->ch_nd)->nd_tx_ready = 1;
3008 if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
3009 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
3010
3011}
3012
3013
3014static void dgrp_tty_send_xchar(struct tty_struct *tty, char c)
3015{
3016 struct un_struct *un;
3017 struct ch_struct *ch;
3018
3019 if (!tty)
3020 return;
3021
3022 un = tty->driver_data;
3023 if (!un)
3024 return;
3025
3026 ch = un->un_ch;
3027 if (!ch)
3028 return;
3029 if (c == STOP_CHAR(tty))
3030 ch->ch_send |= RR_RX_STOP;
3031 else if (c == START_CHAR(tty))
3032 ch->ch_send |= RR_RX_START;
3033
3034 ch->ch_nd->nd_tx_ready = 1;
3035 ch->ch_nd->nd_tx_work = 1;
3036
3037 return;
3038}
3039
3040
3041static void dgrp_tty_input_start(struct tty_struct *tty)
3042{
3043 struct ch_struct *ch;
3044
3045 if (!tty)
3046 return;
3047
3048 ch = ((struct un_struct *) tty->driver_data)->un_ch;
3049 if (!ch)
3050 return;
3051
3052 ch->ch_send |= RR_RX_START;
3053 ch->ch_send &= ~RR_RX_STOP;
3054 (ch->ch_nd)->nd_tx_ready = 1;
3055 (ch->ch_nd)->nd_tx_work = 1;
3056 if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
3057 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
3058
3059}
3060
3061
3062
3063
3064
3065
3066
3067
3068static void dgrp_tty_hangup(struct tty_struct *tty)
3069{
3070 struct ch_struct *ch;
3071 struct nd_struct *nd;
3072 struct un_struct *un;
3073
3074 if (!tty)
3075 return;
3076
3077 un = tty->driver_data;
3078 if (!un)
3079 return;
3080
3081 ch = un->un_ch;
3082 if (!ch)
3083 return;
3084
3085 nd = ch->ch_nd;
3086
3087 if (C_HUPCL(tty)) {
3088
3089 ch->ch_mout &= ~DM_DTR;
3090
3091
3092 ch->ch_nd->nd_tx_ready = 1;
3093 ch->ch_nd->nd_tx_work = 1;
3094 if (waitqueue_active(&ch->ch_flag_wait))
3095 wake_up_interruptible(&ch->ch_flag_wait);
3096 }
3097
3098}
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112void
3113dgrp_tty_uninit(struct nd_struct *nd)
3114{
3115 unsigned int i;
3116 char id[3];
3117
3118 ID_TO_CHAR(nd->nd_ID, id);
3119
3120 if (nd->nd_ttdriver_flags & SERIAL_TTDRV_REG) {
3121 tty_unregister_driver(nd->nd_serial_ttdriver);
3122
3123 kfree(nd->nd_serial_ttdriver->ttys);
3124 nd->nd_serial_ttdriver->ttys = NULL;
3125
3126 put_tty_driver(nd->nd_serial_ttdriver);
3127 nd->nd_ttdriver_flags &= ~SERIAL_TTDRV_REG;
3128 }
3129
3130 if (nd->nd_ttdriver_flags & CALLOUT_TTDRV_REG) {
3131 tty_unregister_driver(nd->nd_callout_ttdriver);
3132
3133 kfree(nd->nd_callout_ttdriver->ttys);
3134 nd->nd_callout_ttdriver->ttys = NULL;
3135
3136 put_tty_driver(nd->nd_callout_ttdriver);
3137 nd->nd_ttdriver_flags &= ~CALLOUT_TTDRV_REG;
3138 }
3139
3140 if (nd->nd_ttdriver_flags & XPRINT_TTDRV_REG) {
3141 tty_unregister_driver(nd->nd_xprint_ttdriver);
3142
3143 kfree(nd->nd_xprint_ttdriver->ttys);
3144 nd->nd_xprint_ttdriver->ttys = NULL;
3145
3146 put_tty_driver(nd->nd_xprint_ttdriver);
3147 nd->nd_ttdriver_flags &= ~XPRINT_TTDRV_REG;
3148 }
3149 for (i = 0; i < CHAN_MAX; i++)
3150 tty_port_destroy(&nd->nd_chan[i].port);
3151}
3152
3153
3154
3155
3156
3157
3158int
3159dgrp_tty_init(struct nd_struct *nd)
3160{
3161 char id[3];
3162 int rc;
3163 int i;
3164
3165 ID_TO_CHAR(nd->nd_ID, id);
3166
3167
3168
3169
3170
3171 nd->nd_serial_ttdriver = alloc_tty_driver(CHAN_MAX);
3172 if (!nd->nd_serial_ttdriver)
3173 return -ENOMEM;
3174
3175 sprintf(nd->nd_serial_name, "tty_dgrp_%s_", id);
3176
3177 nd->nd_serial_ttdriver->owner = THIS_MODULE;
3178 nd->nd_serial_ttdriver->name = nd->nd_serial_name;
3179 nd->nd_serial_ttdriver->name_base = 0;
3180 nd->nd_serial_ttdriver->major = 0;
3181 nd->nd_serial_ttdriver->minor_start = 0;
3182 nd->nd_serial_ttdriver->type = TTY_DRIVER_TYPE_SERIAL;
3183 nd->nd_serial_ttdriver->subtype = SERIAL_TYPE_NORMAL;
3184 nd->nd_serial_ttdriver->init_termios = DefaultTermios;
3185 nd->nd_serial_ttdriver->driver_name = "dgrp";
3186 nd->nd_serial_ttdriver->flags = (TTY_DRIVER_REAL_RAW |
3187 TTY_DRIVER_DYNAMIC_DEV |
3188 TTY_DRIVER_HARDWARE_BREAK);
3189
3190
3191 nd->nd_serial_ttdriver->ttys =
3192 kzalloc(CHAN_MAX * sizeof(struct tty_struct *), GFP_KERNEL);
3193 if (!nd->nd_serial_ttdriver->ttys)
3194 return -ENOMEM;
3195
3196 tty_set_operations(nd->nd_serial_ttdriver, &dgrp_tty_ops);
3197
3198 if (!(nd->nd_ttdriver_flags & SERIAL_TTDRV_REG)) {
3199
3200
3201
3202 rc = tty_register_driver(nd->nd_serial_ttdriver);
3203 if (rc < 0) {
3204
3205
3206
3207
3208
3209
3210
3211
3212 if (rc == -EBUSY) {
3213 int i;
3214 int max_majors = 1U << (32 - MINORBITS);
3215 for (i = 256; i < max_majors; i++) {
3216 nd->nd_serial_ttdriver->major = i;
3217 rc = tty_register_driver
3218 (nd->nd_serial_ttdriver);
3219 if (rc >= 0)
3220 break;
3221 }
3222
3223
3224 if (i == max_majors)
3225 return rc;
3226
3227 } else {
3228 return rc;
3229 }
3230 }
3231 nd->nd_ttdriver_flags |= SERIAL_TTDRV_REG;
3232 }
3233
3234 nd->nd_callout_ttdriver = alloc_tty_driver(CHAN_MAX);
3235 if (!nd->nd_callout_ttdriver)
3236 return -ENOMEM;
3237
3238 sprintf(nd->nd_callout_name, "cu_dgrp_%s_", id);
3239
3240 nd->nd_callout_ttdriver->owner = THIS_MODULE;
3241 nd->nd_callout_ttdriver->name = nd->nd_callout_name;
3242 nd->nd_callout_ttdriver->name_base = 0;
3243 nd->nd_callout_ttdriver->major = nd->nd_serial_ttdriver->major;
3244 nd->nd_callout_ttdriver->minor_start = 0x40;
3245 nd->nd_callout_ttdriver->type = TTY_DRIVER_TYPE_SERIAL;
3246 nd->nd_callout_ttdriver->subtype = SERIAL_TYPE_CALLOUT;
3247 nd->nd_callout_ttdriver->init_termios = DefaultTermios;
3248 nd->nd_callout_ttdriver->driver_name = "dgrp";
3249 nd->nd_callout_ttdriver->flags = (TTY_DRIVER_REAL_RAW |
3250 TTY_DRIVER_DYNAMIC_DEV |
3251 TTY_DRIVER_HARDWARE_BREAK);
3252
3253
3254 nd->nd_callout_ttdriver->ttys =
3255 kzalloc(CHAN_MAX * sizeof(struct tty_struct *), GFP_KERNEL);
3256 if (!nd->nd_callout_ttdriver->ttys)
3257 return -ENOMEM;
3258
3259 tty_set_operations(nd->nd_callout_ttdriver, &dgrp_tty_ops);
3260
3261 if (dgrp_register_cudevices) {
3262 if (!(nd->nd_ttdriver_flags & CALLOUT_TTDRV_REG)) {
3263
3264
3265
3266 rc = tty_register_driver(nd->nd_callout_ttdriver);
3267 if (rc < 0)
3268 return rc;
3269 nd->nd_ttdriver_flags |= CALLOUT_TTDRV_REG;
3270 }
3271 }
3272
3273
3274 nd->nd_xprint_ttdriver = alloc_tty_driver(CHAN_MAX);
3275 if (!nd->nd_xprint_ttdriver)
3276 return -ENOMEM;
3277
3278 sprintf(nd->nd_xprint_name, "pr_dgrp_%s_", id);
3279
3280 nd->nd_xprint_ttdriver->owner = THIS_MODULE;
3281 nd->nd_xprint_ttdriver->name = nd->nd_xprint_name;
3282 nd->nd_xprint_ttdriver->name_base = 0;
3283 nd->nd_xprint_ttdriver->major = nd->nd_serial_ttdriver->major;
3284 nd->nd_xprint_ttdriver->minor_start = 0x80;
3285 nd->nd_xprint_ttdriver->type = TTY_DRIVER_TYPE_SERIAL;
3286 nd->nd_xprint_ttdriver->subtype = SERIAL_TYPE_XPRINT;
3287 nd->nd_xprint_ttdriver->init_termios = DefaultTermios;
3288 nd->nd_xprint_ttdriver->driver_name = "dgrp";
3289 nd->nd_xprint_ttdriver->flags = (TTY_DRIVER_REAL_RAW |
3290 TTY_DRIVER_DYNAMIC_DEV |
3291 TTY_DRIVER_HARDWARE_BREAK);
3292
3293
3294 nd->nd_xprint_ttdriver->ttys =
3295 kzalloc(CHAN_MAX * sizeof(struct tty_struct *), GFP_KERNEL);
3296 if (!nd->nd_xprint_ttdriver->ttys)
3297 return -ENOMEM;
3298
3299 tty_set_operations(nd->nd_xprint_ttdriver, &dgrp_tty_ops);
3300
3301 if (dgrp_register_prdevices) {
3302 if (!(nd->nd_ttdriver_flags & XPRINT_TTDRV_REG)) {
3303
3304
3305
3306 rc = tty_register_driver(nd->nd_xprint_ttdriver);
3307 if (rc < 0)
3308 return rc;
3309 nd->nd_ttdriver_flags |= XPRINT_TTDRV_REG;
3310 }
3311 }
3312
3313 for (i = 0; i < CHAN_MAX; i++) {
3314 struct ch_struct *ch = nd->nd_chan + i;
3315
3316 ch->ch_nd = nd;
3317 ch->ch_digi = digi_init;
3318 ch->ch_edelay = 100;
3319 ch->ch_custom_speed = 0;
3320 ch->ch_portnum = i;
3321 ch->ch_tun.un_ch = ch;
3322 ch->ch_pun.un_ch = ch;
3323 ch->ch_tun.un_type = SERIAL_TYPE_NORMAL;
3324 ch->ch_pun.un_type = SERIAL_TYPE_XPRINT;
3325
3326 init_waitqueue_head(&(ch->ch_flag_wait));
3327 init_waitqueue_head(&(ch->ch_sleep));
3328
3329 init_waitqueue_head(&(ch->ch_tun.un_open_wait));
3330 init_waitqueue_head(&(ch->ch_tun.un_close_wait));
3331
3332 init_waitqueue_head(&(ch->ch_pun.un_open_wait));
3333 init_waitqueue_head(&(ch->ch_pun.un_close_wait));
3334 tty_port_init(&ch->port);
3335 }
3336 return 0;
3337}
3338