]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/dgrp/dgrp_tty.c
tty: Remove tty_hung_up_p() tests from tty drivers' open()
[karo-tx-linux.git] / drivers / staging / dgrp / dgrp_tty.c
1 /*
2  *
3  * Copyright 1999 Digi International (www.digi.com)
4  *     Gene Olson    <Gene_Olson at digi dot com>
5  *     James Puzzo   <jamesp at digi dot com>
6  *     Jeff Randall
7  *     Scott Kilau   <scottk at digi dot com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
16  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17  * PURPOSE.  See the GNU General Public License for more details.
18  *
19  */
20
21 /*
22  *
23  *  Filename:
24  *
25  *     dgrp_tty.c
26  *
27  *  Description:
28  *
29  *     This file implements the tty driver functionality for the
30  *     RealPort driver software.
31  *
32  *  Author:
33  *
34  *     James A. Puzzo
35  *     Ann-Marie Westgate
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  *      forward declarations
54  */
55
56 static void drp_param(struct ch_struct *);
57 static void dgrp_tty_close(struct tty_struct *, struct file *);
58
59 /* ioctl helper functions */
60 static int set_modem_info(struct ch_struct *, unsigned int, unsigned int *);
61 static int get_modem_info(struct ch_struct *, unsigned int *);
62 static void dgrp_set_custom_speed(struct ch_struct *, int);
63 static int dgrp_tty_digigetedelay(struct tty_struct *, int *);
64 static int dgrp_tty_digisetedelay(struct tty_struct *, int *);
65 static int dgrp_send_break(struct ch_struct *, int);
66
67 static ushort  tty_to_ch_flags(struct tty_struct *, char);
68 static tcflag_t ch_to_tty_flags(unsigned short, char);
69
70 static void dgrp_tty_input_start(struct tty_struct *);
71 static void dgrp_tty_input_stop(struct tty_struct *);
72
73 static void drp_wmove(struct ch_struct *, int, void*, int);
74
75 static int dgrp_tty_open(struct tty_struct *, struct file *);
76 static void dgrp_tty_close(struct tty_struct *, struct file *);
77 static int dgrp_tty_write(struct tty_struct *, const unsigned char *, int);
78 static int dgrp_tty_write_room(struct tty_struct *);
79 static void dgrp_tty_flush_buffer(struct tty_struct *);
80 static int dgrp_tty_chars_in_buffer(struct tty_struct *);
81 static int dgrp_tty_ioctl(struct tty_struct *, unsigned int, unsigned long);
82 static void dgrp_tty_set_termios(struct tty_struct *, struct ktermios *);
83 static void dgrp_tty_stop(struct tty_struct *);
84 static void dgrp_tty_start(struct tty_struct *);
85 static void dgrp_tty_throttle(struct tty_struct *);
86 static void dgrp_tty_unthrottle(struct tty_struct *);
87 static void dgrp_tty_hangup(struct tty_struct *);
88 static int dgrp_tty_put_char(struct tty_struct *, unsigned char);
89 static int dgrp_tty_tiocmget(struct tty_struct *);
90 static int dgrp_tty_tiocmset(struct tty_struct *, unsigned int, unsigned int);
91 static int dgrp_tty_send_break(struct tty_struct *, int);
92 static void dgrp_tty_send_xchar(struct tty_struct *, char);
93
94 /*
95  *      tty defines
96  */
97 #define SERIAL_TYPE_NORMAL      1
98 #define SERIAL_TYPE_CALLOUT     2
99 #define SERIAL_TYPE_XPRINT      3
100
101
102 /*
103  *      tty globals/statics
104  */
105
106
107 #define PORTSERVER_DIVIDEND     1843200
108
109 /*
110  *  Default transparent print information.
111  */
112 static struct digi_struct digi_init = {
113         .digi_flags   = DIGI_COOK,      /* Flags                        */
114         .digi_maxcps  = 100,            /* Max CPS                      */
115         .digi_maxchar = 50,             /* Max chars in print queue     */
116         .digi_bufsize = 100,            /* Printer buffer size          */
117         .digi_onlen   = 4,              /* size of printer on string    */
118         .digi_offlen  = 4,              /* size of printer off string   */
119         .digi_onstr   = "\033[5i",      /* ANSI printer on string       */
120         .digi_offstr  = "\033[4i",      /* ANSI printer off string      */
121         .digi_term    = "ansi"          /* default terminal type        */
122 };
123
124 /*
125  *      Define a local default termios struct. All ports will be created
126  *      with this termios initially.
127  *
128  *      This defines a raw port at 9600 baud, 8 data bits, no parity,
129  *      1 stop bit.
130  */
131 static 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 /* Define our tty operations struct */
142 static 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
165 static 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
208 static 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                 { /* fastbaud*/
215                         0,      57600,   76800, 115200,
216                         131657, 153600, 230400, 460800,
217                         921600, 1200,   1800,   2400,
218                         4800,   9600,   19200,  38400 },
219                 { /* fastbaud & CBAUDEX */
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  * drp_param() -- send parameter values to be sent to the node
241  * @ch: channel structure of port to modify
242  *
243  * Interprets the tty and modem changes made by an application
244  * program (by examining the termios structures) and sets up
245  * parameter values to be sent to the node.
246  */
247 static 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          *  If the terminal device is open, use it to set up all tty
261          *  modes and functions.  Otherwise use the printer device.
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                  *  If both devices are open, copy critical line
271                  *  parameters from the tty device to the printer,
272                  *  so that if the tty is closed, the printer will
273                  *  continue without disruption.
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          * Determine if FAST writes can be performed.
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          *  If FAST writes can be performed, and OPOST is on in the
318          *  terminal device, do OPOST handling in the server.
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                 /* add to ch_ocook any processing flags set in the termio */
326                 ch->ch_ocook |= oflag & (OF_OLCUC |
327                                          OF_ONLCR |
328                                          OF_OCRNL |
329                                          OF_ONLRET |
330                                          OF_TABDLY);
331
332                 /*
333                  * the hpux driver clears any flags set in ch_ocook
334                  * from the termios oflag.  It is STILL reported though
335                  * by a TCGETA
336                  */
337
338                 oflag = ch_to_tty_flags(ch->ch_ocook, 'o');
339                 uts->c_oflag &= ~oflag;
340
341         } else {
342                 /* clear the ch->ch_ocook flag */
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          *  Generate channel flags
355          */
356
357         if (C_BAUD(un->un_tty) == B0) {
358                 if (!(ch->ch_flag & CH_BAUD0)) {
359                         /* TODO : the HPUX driver flushes line */
360                         /* TODO : discipline, I assume I don't have to */
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                  * Baud rate mapping.
384                  *
385                  * If FASTBAUD isn't on, we can scan the new baud rate list
386                  * as required.
387                  *
388                  * However, if FASTBAUD is on, we must go to the old
389                  * baud rate mapping that existed many many moons ago,
390                  * for compatibility reasons.
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          *  Generate channel cflags from the termio.
412          */
413
414         ch->ch_cflag = tty_to_ch_flags(un->un_tty, 'c');
415
416         /*
417          *  Generate channel iflags from the termio.
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          *  Generate flow control characters
431          */
432
433         /*
434          * From the POSIX.1 spec (7.1.2.6): "If {_POSIX_VDISABLE}
435          * is defined for the terminal device file, and the value
436          * of one of the changeable special control characters (see
437          * 7.1.1.9) is {_POSIX_VDISABLE}, that function shall be
438          * disabled, that is, no input data shall be recognized as
439          * the disabled special character."
440          *
441          * OK, so we don't ever assign S/DXB XON or XOFF to _POSIX_VDISABLE.
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          * Also, if either c_cc[START] or c_cc[STOP] is set to
454          * _POSIX_VDISABLE, we can't really do software flow
455          * control--in either direction--so we turn it off as
456          * far as S/DXB is concerned.  In essence, if you disable
457          * one, you disable the other too.
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          *  Update xflags.
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          *  Figure effective DCD value.
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          *  Check modem signals
490          */
491
492         dgrp_carrier(ch);
493
494         /*
495          *  Get hardware handshake value.
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          *  Send the changes to the server.
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  * This function is just used as a callback for timeouts
536  * waiting on the ch_sleep flag.
537  */
538 static 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  * Set up our own sleep that can't be cancelled
548  * until our timeout occurs.
549  */
550 static void drp_my_sleep(struct ch_struct *ch)
551 {
552         struct timer_list drp_wakeup_timer;
553         DECLARE_WAITQUEUE(wait, current);
554
555         /*
556          * First make sure we're ready to receive the wakeup.
557          */
558
559         add_wait_queue(&ch->ch_sleep, &wait);
560         current->state = TASK_UNINTERRUPTIBLE;
561
562         /*
563          * Since we are uninterruptible, set a timer to
564          * unset the uninterruptable state in 1 second.
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  * dgrp_tty_open()
582  *
583  * returns:
584  *    -EBUSY    - this is a callout device and the normal device is active
585  *              - there is an error in opening the tty
586  *    -ENODEV   - the channel does not exist
587  *    -EAGAIN   - we are in the middle of hanging up or closing
588  *              - IMMEDIATE_OPEN fails
589  *    -ENXIO or -EAGAIN
590  *              - if the port is outside physical range
591  *    -EINTR    - the open is interrupted
592  *
593  */
594 static 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          * Do some initial checks to see if the node and port exist
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          *  The channel exists.
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          * If the port is in the middle of closing, then block
636          * until it is done, then try again.
637          */
638         retval = wait_event_interruptible(un->un_close_wait,
639                         ((un->un_flag & UN_CLOSING) == 0));
640
641         if (retval)
642                 goto done;
643
644         /*
645          * If the port is in the middle of a reopen after a network disconnect,
646          * wait until it is done, then try again.
647          */
648         retval = wait_event_interruptible(ch->ch_flag_wait,
649                         ((ch->ch_flag & CH_PORT_GONE) == 0));
650
651         if (retval)
652                 goto done;
653
654         /*
655          * If this is a callout device, then just make sure the normal
656          * device isn't being used.
657          */
658
659         if (tty->driver->subtype == SERIAL_TYPE_CALLOUT) {
660                 if (un->un_flag & UN_NORMAL_ACTIVE) {
661                         retval = -EBUSY;
662                         goto done;
663                 } else {
664                         un->un_flag |= UN_CALLOUT_ACTIVE;
665                 }
666         }
667
668         /*
669          *  Loop waiting until the open can be successfully completed.
670          */
671
672         spin_lock_irqsave(&nd->nd_lock, lock_flags);
673
674         nd->nd_tx_work = 1;
675
676         for (;;) {
677                 wait_carrier = 0;
678
679                 /*
680                  * Determine the open type from the flags provided.
681                  */
682
683                 /*
684                  * If the port is not enabled, then exit
685                  */
686                 if (test_bit(TTY_IO_ERROR, &tty->flags)) {
687                         /* there was an error in opening the tty */
688                         if (un->un_flag & UN_CALLOUT_ACTIVE)
689                                 retval = -EBUSY;
690                         else
691                                 un->un_flag |= UN_NORMAL_ACTIVE;
692                         goto unlock;
693                 }
694
695                 if (file->f_flags & O_NONBLOCK) {
696
697                         /*
698                          * if the O_NONBLOCK is set, errors on read and write
699                          * must return -EAGAIN immediately and NOT sleep
700                          * on the waitqs.
701                          */
702                         otype = OTYPE_IMMEDIATE;
703                         delay_error = -EAGAIN;
704
705                 } else if (!OPEN_WAIT_AVAIL(category) ||
706                           (file->f_flags & O_NDELAY) != 0) {
707                         otype = OTYPE_IMMEDIATE;
708                         delay_error = -EBUSY;
709
710                 } else if (!OPEN_WAIT_CARRIER(category) ||
711                           ((ch->ch_digi.digi_flags & DIGI_FORCEDCD) != 0) ||
712                           C_CLOCAL(tty)) {
713                         otype = OTYPE_PERSISTENT;
714                         delay_error = 0;
715
716                 } else {
717                         otype = OTYPE_INCOMING;
718                         delay_error = 0;
719                 }
720
721                 /*
722                  * Handle port currently outside physical port range.
723                  */
724
725                 if (port >= nd->nd_chan_count) {
726                         if (otype == OTYPE_IMMEDIATE) {
727                                 retval = (nd->nd_state == NS_READY) ?
728                                                 -ENXIO : -EAGAIN;
729                                 goto unlock;
730                         }
731                 }
732
733                 /*
734                  *  Handle port not currently open.
735                  */
736
737                 else if (ch->ch_open_count == 0) {
738                         /*
739                          * Return an error when an Incoming Open
740                          * response indicates the port is busy.
741                          */
742
743                         if (ch->ch_open_error != 0 && otype == ch->ch_otype) {
744                                 retval = (ch->ch_open_error <= 2) ?
745                                           delay_error : -ENXIO;
746                                 goto unlock;
747                         }
748
749                         /*
750                          * Fail any new Immediate open if we do not have
751                          * a normal connection to the server.
752                          */
753
754                         if (nd->nd_state != NS_READY &&
755                             otype == OTYPE_IMMEDIATE) {
756                                 retval = -EAGAIN;
757                                 goto unlock;
758                         }
759
760                         /*
761                          * If a Realport open of the correct type has
762                          * succeeded, complete the open.
763                          */
764
765                         if (ch->ch_state == CS_READY && ch->ch_otype == otype)
766                                 break;
767                 }
768
769                 /*
770                  * Handle port already open and active as a device
771                  * of same category.
772                  */
773
774                 else if ((ch->ch_category == category) ||
775                           IS_PRINT(MINOR(tty_devnum(tty)))) {
776                         /*
777                          * Fail if opening the device now would
778                          * violate exclusive use.
779                          */
780                         unf = ch->ch_tun.un_flag | ch->ch_pun.un_flag;
781
782                         if ((file->f_flags & O_EXCL) || (unf & UN_EXCL)) {
783                                 retval = -EBUSY;
784                                 goto unlock;
785                         }
786
787                         /*
788                          * If the open device is in the hangup state, all
789                          * system calls fail except close().
790                          */
791
792                         /* TODO : check on hangup_p calls */
793
794                         if (ch->ch_flag & CH_HANGUP) {
795                                 retval = -ENXIO;
796                                 goto unlock;
797                         }
798
799                         /*
800                          * If the port is ready, and carrier is ignored
801                          * or present, then complete the open.
802                          */
803
804                         if (ch->ch_state == CS_READY &&
805                             (otype != OTYPE_INCOMING ||
806                             ch->ch_flag & CH_VIRT_CD))
807                                 break;
808
809                         wait_carrier = 1;
810                 }
811
812                 /*
813                  *  Handle port active with a different category device.
814                  */
815
816                 else {
817                         if (otype == OTYPE_IMMEDIATE) {
818                                 retval = delay_error;
819                                 goto unlock;
820                         }
821                 }
822
823                 /*
824                  * Wait until conditions change, then take another
825                  * try at the open.
826                  */
827
828                 ch->ch_wait_count[otype]++;
829
830                 if (wait_carrier)
831                         ch->ch_wait_carrier++;
832
833                 /*
834                  * Prepare the task to accept the wakeup, then
835                  * release our locks and release control.
836                  */
837
838                 add_wait_queue(&ch->ch_flag_wait, &wait);
839                 current->state = TASK_INTERRUPTIBLE;
840
841                 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
842
843                 /*
844                  * Give up control, we'll come back if we're
845                  * interrupted or are woken up.
846                  */
847                 schedule();
848                 remove_wait_queue(&ch->ch_flag_wait, &wait);
849
850                 spin_lock_irqsave(&nd->nd_lock, lock_flags);
851
852                 current->state = TASK_RUNNING;
853
854                 ch->ch_wait_count[otype]--;
855
856                 if (wait_carrier)
857                         ch->ch_wait_carrier--;
858
859                 nd->nd_tx_work = 1;
860
861                 if (signal_pending(current)) {
862                         retval = -EINTR;
863                         goto unlock;
864                 }
865         } /* end for(;;) */
866
867         /*
868          *  The open has succeeded.  No turning back.
869          */
870         counts_were_incremented = 1;
871         un->un_open_count++;
872         ch->ch_open_count++;
873
874         /*
875          * Initialize the channel, if it's not already open.
876          */
877
878         if (ch->ch_open_count == 1) {
879                 ch->ch_flag = 0;
880                 ch->ch_inwait = 0;
881                 ch->ch_category = category;
882                 ch->ch_pscan_state = 0;
883
884                 /* TODO : find out what PS-1 bug Gene was referring to */
885                 /* TODO : in the following comment. */
886
887                 ch->ch_send = RR_TX_START | RR_RX_START;  /* PS-1 bug */
888
889                 if (C_CLOCAL(tty) ||
890                     ch->ch_s_mlast & DM_CD ||
891                     ch->ch_digi.digi_flags & DIGI_FORCEDCD)
892                         ch->ch_flag |= CH_VIRT_CD;
893                 else if (OPEN_FORCES_CARRIER(category))
894                         ch->ch_flag |= CH_VIRT_CD;
895
896         }
897
898         /*
899          *  Initialize the unit, if it is not already open.
900          */
901
902         if (un->un_open_count == 1) {
903                 /*
904                  *  Since all terminal options are always sticky in Linux,
905                  *  we don't need the UN_STICKY flag to be handled specially.
906                  */
907                 /* clears all the digi flags, leaves serial flags */
908                 un->un_flag &= ~UN_DIGI_MASK;
909
910                 if (file->f_flags & O_EXCL)
911                         un->un_flag |= UN_EXCL;
912
913                 /* TODO : include "session" and "pgrp" */
914
915                 /*
916                  *  In Linux, all terminal parameters are intended to be sticky.
917                  *  as a result, we "remove" the code which once reset the ports
918                  *  to sane values.
919                  */
920
921                 drp_param(ch);
922
923         }
924
925         un->un_flag |= UN_INITIALIZED;
926
927         retval = 0;
928
929 unlock:
930
931         spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
932
933 done:
934         /*
935          * Linux does a close for every open, even failed ones!
936          */
937         if (!counts_were_incremented) {
938                 un->un_open_count++;
939                 ch->ch_open_count++;
940         }
941
942         if (retval)
943                 dev_err(tty->dev, "tty open bad return (%i)\n", retval);
944
945         return retval;
946 }
947
948
949
950
951 /*
952  * dgrp_tty_close() -- close function for tty_operations
953  */
954 static void dgrp_tty_close(struct tty_struct *tty, struct file *file)
955 {
956         struct ch_struct *ch;
957         struct un_struct *un;
958         struct nd_struct *nd;
959         int     tpos;
960         int     port;
961         int     err = 0;
962         int     s = 0;
963         ulong  waketime;
964         ulong  lock_flags;
965         int sent_printer_offstr = 0;
966
967         port = PORT_NUM(MINOR(tty_devnum(tty)));
968
969         un = tty->driver_data;
970
971         if (!un)
972                 return;
973
974         ch = un->un_ch;
975
976         if (!ch)
977                 return;
978
979         nd = ch->ch_nd;
980
981         if (!nd)
982                 return;
983
984         spin_lock_irqsave(&nd->nd_lock, lock_flags);
985
986
987         /* Used to be on channel basis, now we check on a unit basis. */
988         if (un->un_open_count != 1)
989                 goto unlock;
990
991         /*
992          * OK, its the last close on the unit
993          */
994         un->un_flag |= UN_CLOSING;
995
996         /*
997          * Notify the discipline to only process XON/XOFF characters.
998          */
999         tty->closing = 1;
1000
1001         /*
1002          * Wait for output to drain only if this is
1003          * the last close against the channel
1004          */
1005
1006         if (ch->ch_open_count == 1) {
1007                 /*
1008                  * If its the print device, we need to ensure at all costs that
1009                  * the offstr will fit. If it won't, flush our tbuf.
1010                  */
1011                 if (IS_PRINT(MINOR(tty_devnum(tty))) &&
1012                     (((ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK) <
1013                     ch->ch_digi.digi_offlen))
1014                         ch->ch_tin = ch->ch_tout;
1015
1016                 /*
1017                  * Turn off the printer.  Don't bother checking to see if its
1018                  * IS_PRINT... Since this is the last close the flag is going
1019                  * to be cleared, so we MUST make sure the offstr gets inserted
1020                  * into tbuf.
1021                  */
1022
1023                 if ((ch->ch_flag & CH_PRON) != 0) {
1024                         drp_wmove(ch, 0, ch->ch_digi.digi_offstr,
1025                                   ch->ch_digi.digi_offlen);
1026                         ch->ch_flag &= ~CH_PRON;
1027                         sent_printer_offstr = 1;
1028                 }
1029         }
1030
1031         /*
1032          *  Wait until either the output queue has drained, or we see
1033          *  absolutely no progress for 15 seconds.
1034          */
1035
1036         tpos = ch->ch_s_tpos;
1037
1038         waketime = jiffies + 15 * HZ;
1039
1040         for (;;) {
1041
1042                 /*
1043                  *  Make sure the port still exists.
1044                  */
1045
1046                 if (port >= nd->nd_chan_count) {
1047                         err = 1;
1048                         break;
1049                 }
1050
1051                 if (signal_pending(current)) {
1052                         err = 1;
1053                         break;
1054                 }
1055
1056                 /*
1057                  * If the port is idle (not opened on the server), we have
1058                  * no way of draining/flushing/closing the port on that server.
1059                  * So break out of loop.
1060                  */
1061                 if (ch->ch_state == CS_IDLE)
1062                         break;
1063
1064                 nd->nd_tx_work = 1;
1065
1066                 /*
1067                  *  Exit if the queues for this unit are empty,
1068                  *  and either the other unit is still open or all
1069                  *  data has drained.
1070                  */
1071
1072                 if ((un->un_tty)->ops->chars_in_buffer ?
1073                     ((un->un_tty)->ops->chars_in_buffer)(un->un_tty) == 0 : 1) {
1074
1075                         /*
1076                          * We don't need to wait for a buffer to drain
1077                          * if the other unit is open.
1078                          */
1079
1080                         if (ch->ch_open_count != un->un_open_count)
1081                                 break;
1082
1083                         /*
1084                          *  The wait is complete when all queues are
1085                          *  drained, and any break in progress is complete.
1086                          */
1087
1088                         if (ch->ch_tin == ch->ch_tout &&
1089                             ch->ch_s_tin == ch->ch_s_tpos &&
1090                             (ch->ch_send & RR_TX_BREAK) == 0) {
1091                                 break;
1092                         }
1093                 }
1094
1095                 /*
1096                  * Flush TX data and exit the wait if NDELAY is set,
1097                  * or this is not a DIGI printer, and the close timeout
1098                  * expires.
1099                  */
1100
1101                 if ((file->f_flags & (O_NDELAY | O_NONBLOCK)) ||
1102                     ((long)(jiffies - waketime) >= 0 &&
1103                       (ch->ch_digi.digi_flags & DIGI_PRINTER) == 0)) {
1104
1105                                 /*
1106                                  * If we sent the printer off string, we cannot
1107                                  * flush our internal buffers, or we might lose
1108                                  * the offstr.
1109                                  */
1110                                 if (!sent_printer_offstr)
1111                                         dgrp_tty_flush_buffer(tty);
1112
1113                                 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1114                                 tty_ldisc_flush(tty);
1115                                 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1116                                 break;
1117                 }
1118
1119                 /*
1120                  *  Otherwise take a short nap.
1121                  */
1122
1123                 ch->ch_flag |= CH_DRAIN;
1124
1125                 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1126
1127                 schedule_timeout_interruptible(1);
1128                 s = signal_pending(current);
1129
1130                 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1131
1132                 if (s) {
1133                         /*
1134                          * If we had sent the printer off string, we now have
1135                          * some problems.
1136                          *
1137                          * The system won't let us sleep since we got an error
1138                          * back from sleep, presumably because the user did
1139                          * a ctrl-c...
1140                          * But we need to ensure that the offstr gets sent!
1141                          * Thus, we have to do something else besides sleeping.
1142                          * The plan:
1143                          * 1) Make this task uninterruptable.
1144                          * 2) Set up a timer to go off in 1 sec.
1145                          * 3) Act as tho we just got out of the sleep above.
1146                          *
1147                          * Thankfully, in the real world, this just
1148                          * never happens.
1149                          */
1150
1151                         if (sent_printer_offstr) {
1152                                 spin_unlock_irqrestore(&nd->nd_lock,
1153                                                        lock_flags);
1154                                 drp_my_sleep(ch);
1155                                 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1156                         } else {
1157                                 err = 1;
1158                                 break;
1159                         }
1160                 }
1161
1162                 /*
1163                  *  Restart the wait if any progress is seen.
1164                  */
1165
1166                 if (ch->ch_s_tpos != tpos) {
1167                         tpos = ch->ch_s_tpos;
1168
1169                         /* TODO:  this gives us timeout problems with nist ?? */
1170                         waketime = jiffies + 15 * HZ;
1171                 }
1172         }
1173
1174         /*
1175          *  Close the line discipline
1176          */
1177
1178         /* this is done in tty_io.c */
1179         /* if ((un->un_tty)->ldisc.close)
1180          *      ((un->un_tty)->ldisc.close)(un->un_tty);
1181          */
1182
1183         /* don't do this here */
1184         /* un->un_flag = 0; */
1185
1186         /*
1187          *  Flush the receive buffer on terminal unit close only.
1188          */
1189
1190         if (!IS_PRINT(MINOR(tty_devnum(tty))))
1191                 ch->ch_rout = ch->ch_rin;
1192
1193
1194         /*
1195          * Don't permit the close to happen until we get any pending
1196          * sync request responses.
1197          * There could be other ports depending upon the response as well.
1198          *
1199          * Also, don't permit the close to happen until any parameter
1200          * changes have been sent out from the state machine as well.
1201          * This is required because of a ditty -a race with -HUPCL
1202          * We MUST make sure all channel parameters have been sent to the
1203          * Portserver before sending a close.
1204          */
1205
1206         if ((err != 1) && (ch->ch_state != CS_IDLE)) {
1207                 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1208                 s = wait_event_interruptible(ch->ch_flag_wait,
1209                         ((ch->ch_flag & (CH_WAITING_SYNC | CH_PARAM)) == 0));
1210                 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1211         }
1212
1213         /*
1214          * Cleanup the channel if last unit open.
1215          */
1216
1217         if (ch->ch_open_count == 1) {
1218                 ch->ch_flag = 0;
1219                 ch->ch_category = 0;
1220                 ch->ch_send = 0;
1221                 ch->ch_expect = 0;
1222                 ch->ch_tout = ch->ch_tin;
1223                 /* (un->un_tty)->device = 0; */
1224
1225                 if (ch->ch_state == CS_READY)
1226                         ch->ch_state = CS_SEND_CLOSE;
1227         }
1228
1229         /*
1230          * Send the changes to the server
1231          */
1232         if (ch->ch_state != CS_IDLE) {
1233                 ch->ch_flag |= CH_PARAM;
1234                 wake_up_interruptible(&ch->ch_flag_wait);
1235         }
1236
1237         nd->nd_tx_work = 1;
1238         nd->nd_tx_ready = 1;
1239
1240 unlock:
1241         tty->closing = 0;
1242
1243         if (ch->ch_open_count <= 0)
1244                 dev_info(tty->dev,
1245                          "%s - unexpected value for ch->ch_open_count: %i\n",
1246                          __func__, ch->ch_open_count);
1247         else
1248                 ch->ch_open_count--;
1249
1250         if (un->un_open_count <= 0)
1251                 dev_info(tty->dev,
1252                          "%s - unexpected value for un->un_open_count: %i\n",
1253                          __func__, un->un_open_count);
1254         else
1255                 un->un_open_count--;
1256
1257         un->un_flag &= ~(UN_NORMAL_ACTIVE | UN_CALLOUT_ACTIVE | UN_CLOSING);
1258         if (waitqueue_active(&un->un_close_wait))
1259                 wake_up_interruptible(&un->un_close_wait);
1260
1261         spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1262
1263         return;
1264
1265 }
1266
1267 static void drp_wmove(struct ch_struct *ch, int from_user, void *buf, int count)
1268 {
1269         int n;
1270         int ret = 0;
1271
1272         ch->ch_nd->nd_tx_work = 1;
1273
1274         n = TBUF_MAX - ch->ch_tin;
1275
1276         if (count >= n) {
1277                 if (from_user)
1278                         ret = copy_from_user(ch->ch_tbuf + ch->ch_tin,
1279                                              (void __user *) buf, n);
1280                 else
1281                         memcpy(ch->ch_tbuf + ch->ch_tin, buf, n);
1282
1283                 buf = (char *) buf + n;
1284                 count -= n;
1285                 ch->ch_tin = 0;
1286         }
1287
1288         if (from_user)
1289                 ret = copy_from_user(ch->ch_tbuf + ch->ch_tin,
1290                                      (void __user *) buf, count);
1291         else
1292                 memcpy(ch->ch_tbuf + ch->ch_tin, buf, count);
1293
1294         ch->ch_tin += count;
1295 }
1296
1297
1298 static int dgrp_calculate_txprint_bounds(struct ch_struct *ch, int space,
1299                                          int *un_flag)
1300 {
1301         clock_t tt;
1302         clock_t mt;
1303         unsigned short tmax = 0;
1304
1305         /*
1306          * If the terminal device is busy, reschedule when
1307          * the terminal device becomes idle.
1308          */
1309
1310         if (ch->ch_tun.un_open_count != 0 &&
1311             ch->ch_tun.un_tty->ops->chars_in_buffer &&
1312             ((ch->ch_tun.un_tty->ops->chars_in_buffer)
1313              (ch->ch_tun.un_tty) != 0)) {
1314                 *un_flag = UN_PWAIT;
1315                 return 0;
1316         }
1317
1318         /*
1319          * Assure that whenever there is printer data in the output
1320          * buffer, there always remains enough space after it to
1321          * turn the printer off.
1322          */
1323         space -= ch->ch_digi.digi_offlen;
1324
1325         if (space <= 0) {
1326                 *un_flag = UN_EMPTY;
1327                 return 0;
1328         }
1329
1330         /*
1331          * We measure printer CPS speed by incrementing
1332          * ch_cpstime by (HZ / digi_maxcps) for every
1333          * character we output, restricting output so
1334          * that ch_cpstime never exceeds lbolt.
1335          *
1336          * However if output has not been done for some
1337          * time, lbolt will grow to very much larger than
1338          * ch_cpstime, which would allow essentially
1339          * unlimited amounts of output until ch_cpstime
1340          * finally caught up.   To avoid this, we adjust
1341          * cps_time when necessary so the difference
1342          * between lbolt and ch_cpstime never results
1343          * in sending more than digi_bufsize characters.
1344          *
1345          * This nicely models a printer with an internal
1346          * buffer of digi_bufsize characters.
1347          *
1348          * Get the time between lbolt and ch->ch_cpstime;
1349          */
1350
1351         tt = jiffies - ch->ch_cpstime;
1352
1353         /*
1354          * Compute the time required to send digi_bufsize
1355          * characters.
1356          */
1357
1358         mt = HZ * ch->ch_digi.digi_bufsize / ch->ch_digi.digi_maxcps;
1359
1360         /*
1361          * Compute the number of characters that can be sent
1362          * without violating the time constraint.   If the
1363          * direct calculation of this number is bigger than
1364          * digi_bufsize, limit the number to digi_bufsize,
1365          * and adjust cpstime to match.
1366          */
1367
1368         if ((clock_t)(tt + HZ) > (clock_t)(mt + HZ)) {
1369                 tmax = ch->ch_digi.digi_bufsize;
1370                 ch->ch_cpstime = jiffies - mt;
1371         } else {
1372                 tmax = ch->ch_digi.digi_maxcps * tt / HZ;
1373         }
1374
1375         /*
1376          * If the time constraint now binds, limit the transmit
1377          * count accordingly, and tentatively arrange to be
1378          * rescheduled based on time.
1379          */
1380
1381         if (tmax < space) {
1382                 *un_flag = UN_TIME;
1383                 space = tmax;
1384         }
1385
1386         /*
1387          * Compute the total number of characters we can
1388          * output before the total number of characters known
1389          * to be in the output queue exceeds digi_maxchar.
1390          */
1391
1392         tmax = (ch->ch_digi.digi_maxchar -
1393                 ((ch->ch_tin - ch->ch_tout) & TBUF_MASK) -
1394                 ((ch->ch_s_tin - ch->ch_s_tpos) & 0xffff));
1395
1396
1397         /*
1398          * If the digi_maxchar constraint now holds, limit
1399          * the transmit count accordingly, and arrange to
1400          * be rescheduled when the queue becomes empty.
1401          */
1402
1403         if (space > tmax) {
1404                 *un_flag = UN_EMPTY;
1405                 space = tmax;
1406         }
1407
1408         if (space <= 0)
1409                 *un_flag |= UN_EMPTY;
1410
1411         return space;
1412 }
1413
1414
1415 static int dgrp_tty_write(struct tty_struct *tty,
1416                           const unsigned char *buf,
1417                           int count)
1418 {
1419         struct nd_struct *nd;
1420         struct un_struct *un;
1421         struct ch_struct *ch;
1422         int     space;
1423         int     n;
1424         int     t;
1425         int sendcount;
1426         int un_flag;
1427         ulong lock_flags;
1428
1429         if (tty == NULL)
1430                 return 0;
1431
1432         un = tty->driver_data;
1433         if (!un)
1434                 return 0;
1435
1436         ch = un->un_ch;
1437         if (!ch)
1438                 return 0;
1439
1440         nd = ch->ch_nd;
1441         if (!nd)
1442                 return 0;
1443
1444         /*
1445          * Ignore the request if the channel is not ready.
1446          */
1447         if (ch->ch_state != CS_READY)
1448                 return 0;
1449
1450         spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
1451
1452         /*
1453          * Ignore the request if output is blocked.
1454          */
1455         if ((un->un_flag & (UN_EMPTY | UN_LOW | UN_TIME | UN_PWAIT)) != 0) {
1456                 count = 0;
1457                 goto out;
1458         }
1459
1460         /*
1461          * Also ignore the request if DPA has this port open,
1462          * and is flow controlled on reading more data.
1463          */
1464         if (nd->nd_dpa_debug && nd->nd_dpa_flag & DPA_WAIT_SPACE &&
1465                 nd->nd_dpa_port == MINOR(tty_devnum(ch->ch_tun.un_tty))) {
1466                 count = 0;
1467                 goto out;
1468         }
1469
1470         /*
1471          *      Limit amount we will write to the amount of space
1472          *      available in the channel buffer.
1473          */
1474         sendcount = 0;
1475
1476         space = (ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK;
1477
1478         /*
1479          * Handle the printer device.
1480          */
1481
1482         un_flag = UN_LOW;
1483
1484         if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1485                 clock_t tt;
1486                 clock_t mt;
1487                 unsigned short tmax = 0;
1488
1489                 /*
1490                  * If the terminal device is busy, reschedule when
1491                  * the terminal device becomes idle.
1492                  */
1493
1494                 if (ch->ch_tun.un_open_count != 0 &&
1495                     ((ch->ch_tun.un_tty->ops->chars_in_buffer)
1496                      (ch->ch_tun.un_tty) != 0)) {
1497                         un->un_flag |= UN_PWAIT;
1498                         count = 0;
1499                         goto out;
1500                 }
1501
1502                 /*
1503                  * Assure that whenever there is printer data in the output
1504                  * buffer, there always remains enough space after it to
1505                  * turn the printer off.
1506                  */
1507                 space -= ch->ch_digi.digi_offlen;
1508
1509                 /*
1510                  * Output the printer on string.
1511                  */
1512
1513                 if ((ch->ch_flag & CH_PRON) == 0) {
1514                         space -= ch->ch_digi.digi_onlen;
1515
1516                         if (space < 0) {
1517                                 un->un_flag |= UN_EMPTY;
1518                                 (ch->ch_nd)->nd_tx_work = 1;
1519                                 count = 0;
1520                                 goto out;
1521                         }
1522
1523                         drp_wmove(ch, 0, ch->ch_digi.digi_onstr,
1524                                 ch->ch_digi.digi_onlen);
1525
1526                         ch->ch_flag |= CH_PRON;
1527                 }
1528
1529                 /*
1530                  * We measure printer CPS speed by incrementing
1531                  * ch_cpstime by (HZ / digi_maxcps) for every
1532                  * character we output, restricting output so
1533                  * that ch_cpstime never exceeds lbolt.
1534                  *
1535                  * However if output has not been done for some
1536                  * time, lbolt will grow to very much larger than
1537                  * ch_cpstime, which would allow essentially
1538                  * unlimited amounts of output until ch_cpstime
1539                  * finally caught up.   To avoid this, we adjust
1540                  * cps_time when necessary so the difference
1541                  * between lbolt and ch_cpstime never results
1542                  * in sending more than digi_bufsize characters.
1543                  *
1544                  * This nicely models a printer with an internal
1545                  * buffer of digi_bufsize characters.
1546                  *
1547                  * Get the time between lbolt and ch->ch_cpstime;
1548                  */
1549
1550                 tt = jiffies - ch->ch_cpstime;
1551
1552                 /*
1553                  * Compute the time required to send digi_bufsize
1554                  * characters.
1555                  */
1556
1557                 mt = HZ * ch->ch_digi.digi_bufsize / ch->ch_digi.digi_maxcps;
1558
1559                 /*
1560                  * Compute the number of characters that can be sent
1561                  * without violating the time constraint.   If the
1562                  * direct calculation of this number is bigger than
1563                  * digi_bufsize, limit the number to digi_bufsize,
1564                  * and adjust cpstime to match.
1565                  */
1566
1567                 if ((clock_t)(tt + HZ) > (clock_t)(mt + HZ)) {
1568                         tmax = ch->ch_digi.digi_bufsize;
1569                         ch->ch_cpstime = jiffies - mt;
1570                 } else {
1571                         tmax = ch->ch_digi.digi_maxcps * tt / HZ;
1572                 }
1573
1574                 /*
1575                  * If the time constraint now binds, limit the transmit
1576                  * count accordingly, and tentatively arrange to be
1577                  * rescheduled based on time.
1578                  */
1579
1580                 if (tmax < space) {
1581                         space = tmax;
1582                         un_flag = UN_TIME;
1583                 }
1584
1585                 /*
1586                  * Compute the total number of characters we can
1587                  * output before the total number of characters known
1588                  * to be in the output queue exceeds digi_maxchar.
1589                  */
1590
1591                 tmax = (ch->ch_digi.digi_maxchar -
1592                         ((ch->ch_tin - ch->ch_tout) & TBUF_MASK) -
1593                         ((ch->ch_s_tin - ch->ch_s_tpos) & 0xffff));
1594
1595
1596                 /*
1597                  * If the digi_maxchar constraint now holds, limit
1598                  * the transmit count accordingly, and arrange to
1599                  * be rescheduled when the queue becomes empty.
1600                  */
1601
1602                 if (space > tmax) {
1603                         space = tmax;
1604                         un_flag = UN_EMPTY;
1605                 }
1606
1607         }
1608         /*
1609          * Handle the terminal device.
1610          */
1611         else {
1612
1613                 /*
1614                  * If the printer device is on, turn it off.
1615                  */
1616
1617                 if ((ch->ch_flag & CH_PRON) != 0) {
1618
1619                         space -= ch->ch_digi.digi_offlen;
1620
1621                         drp_wmove(ch, 0, ch->ch_digi.digi_offstr,
1622                                   ch->ch_digi.digi_offlen);
1623
1624                         ch->ch_flag &= ~CH_PRON;
1625                 }
1626         }
1627
1628         /*
1629          *      If space is 0 and its because the ch->tbuf
1630          *      is full, then Linux will handle a callback when queue
1631          *      space becomes available.
1632          *      tty_write returns count = 0
1633          */
1634
1635         if (space <= 0) {
1636                 /* the linux tty_io.c handles this if we return 0 */
1637                 /* if (fp->flags & O_NONBLOCK) return -EAGAIN; */
1638
1639                 un->un_flag |= UN_EMPTY;
1640                 (ch->ch_nd)->nd_tx_work = 1;
1641                 count = 0;
1642                 goto out;
1643         }
1644
1645         count = min(count, space);
1646
1647         if (count > 0) {
1648
1649                 un->un_tbusy++;
1650
1651                 /*
1652                  *      Copy the buffer contents to the ch_tbuf
1653                  *      being careful to wrap around the circular queue
1654                  */
1655
1656                 t = TBUF_MAX - ch->ch_tin;
1657                 n = count;
1658
1659                 if (n >= t) {
1660                         memcpy(ch->ch_tbuf + ch->ch_tin, buf, t);
1661                         if (nd->nd_dpa_debug && nd->nd_dpa_port ==
1662                                 PORT_NUM(MINOR(tty_devnum(un->un_tty))))
1663                                 dgrp_dpa_data(nd, 0, (char *) buf, t);
1664                         buf += t;
1665                         n -= t;
1666                         ch->ch_tin = 0;
1667                         sendcount += n;
1668                 }
1669
1670                 memcpy(ch->ch_tbuf + ch->ch_tin, buf, n);
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, n);
1674                 buf += n;
1675                 ch->ch_tin += n;
1676                 sendcount += n;
1677
1678                 un->un_tbusy--;
1679                 (ch->ch_nd)->nd_tx_work = 1;
1680                 if (ch->ch_edelay != DGRP_RTIME) {
1681                         (ch->ch_nd)->nd_tx_ready = 1;
1682                         wake_up_interruptible(&nd->nd_tx_waitq);
1683                 }
1684         }
1685
1686         ch->ch_txcount += count;
1687
1688         if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1689
1690                 /*
1691                  * Adjust ch_cpstime to account
1692                  * for the characters just output.
1693                  */
1694
1695                 if (sendcount > 0) {
1696                         int cc = HZ * sendcount + ch->ch_cpsrem;
1697
1698                         ch->ch_cpstime += cc / ch->ch_digi.digi_maxcps;
1699                         ch->ch_cpsrem   = cc % ch->ch_digi.digi_maxcps;
1700                 }
1701
1702                 /*
1703                  * If we are now waiting on time, schedule ourself
1704                  * back when we'll be able to send a block of
1705                  * digi_maxchar characters.
1706                  */
1707
1708                 if ((un_flag & UN_TIME) != 0) {
1709                         ch->ch_waketime = (ch->ch_cpstime +
1710                                 (ch->ch_digi.digi_maxchar * HZ /
1711                                 ch->ch_digi.digi_maxcps));
1712                 }
1713         }
1714
1715         /*
1716          * If the printer unit is waiting for completion
1717          * of terminal output, get him going again.
1718          */
1719
1720         if ((ch->ch_pun.un_flag & UN_PWAIT) != 0)
1721                 (ch->ch_nd)->nd_tx_work = 1;
1722
1723 out:
1724         spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
1725
1726         return count;
1727 }
1728
1729
1730 /*
1731  *      Put a character into ch->ch_buf
1732  *
1733  *      - used by the line discipline for OPOST processing
1734  */
1735
1736 static int dgrp_tty_put_char(struct tty_struct *tty, unsigned char new_char)
1737 {
1738         struct un_struct *un;
1739         struct ch_struct *ch;
1740         ulong  lock_flags;
1741         int space;
1742         int retval = 0;
1743
1744         if (tty == NULL)
1745                 return 0;
1746
1747         un = tty->driver_data;
1748         if (!un)
1749                 return 0;
1750
1751         ch = un->un_ch;
1752         if (!ch)
1753                 return 0;
1754
1755         if (ch->ch_state != CS_READY)
1756                 return 0;
1757
1758         spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
1759
1760
1761         /*
1762          *      If space is 0 and its because the ch->tbuf
1763          *      Warn and dump the character, there isn't anything else
1764          *      we can do about it.  David_Fries@digi.com
1765          */
1766
1767         space = (ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK;
1768
1769         un->un_tbusy++;
1770
1771         /*
1772          * Output the printer on string if device is TXPrint.
1773          */
1774         if (IS_PRINT(MINOR(tty_devnum(tty))) && (ch->ch_flag & CH_PRON) == 0) {
1775                 if (space < ch->ch_digi.digi_onlen) {
1776                         un->un_tbusy--;
1777                         goto out;
1778                 }
1779                 space -= ch->ch_digi.digi_onlen;
1780                 drp_wmove(ch, 0, ch->ch_digi.digi_onstr,
1781                           ch->ch_digi.digi_onlen);
1782                 ch->ch_flag |= CH_PRON;
1783         }
1784
1785         /*
1786          * Output the printer off string if device is NOT TXPrint.
1787          */
1788
1789         if (!IS_PRINT(MINOR(tty_devnum(tty))) &&
1790             ((ch->ch_flag & CH_PRON) != 0)) {
1791                 if (space < ch->ch_digi.digi_offlen) {
1792                         un->un_tbusy--;
1793                         goto out;
1794                 }
1795
1796                 space -= ch->ch_digi.digi_offlen;
1797                 drp_wmove(ch, 0, ch->ch_digi.digi_offstr,
1798                           ch->ch_digi.digi_offlen);
1799                 ch->ch_flag &= ~CH_PRON;
1800         }
1801
1802         if (!space) {
1803                 un->un_tbusy--;
1804                 goto out;
1805         }
1806
1807         /*
1808          *      Copy the character to the ch_tbuf being
1809          *      careful to wrap around the circular queue
1810          */
1811         ch->ch_tbuf[ch->ch_tin] = new_char;
1812         ch->ch_tin = (1 + ch->ch_tin) & TBUF_MASK;
1813
1814         if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1815
1816                 /*
1817                  * Adjust ch_cpstime to account
1818                  * for the character just output.
1819                  */
1820
1821                 int cc = HZ + ch->ch_cpsrem;
1822
1823                 ch->ch_cpstime += cc / ch->ch_digi.digi_maxcps;
1824                 ch->ch_cpsrem   = cc % ch->ch_digi.digi_maxcps;
1825
1826                 /*
1827                  * If we are now waiting on time, schedule ourself
1828                  * back when we'll be able to send a block of
1829                  * digi_maxchar characters.
1830                  */
1831
1832                 ch->ch_waketime = (ch->ch_cpstime +
1833                         (ch->ch_digi.digi_maxchar * HZ /
1834                         ch->ch_digi.digi_maxcps));
1835         }
1836
1837
1838         un->un_tbusy--;
1839         (ch->ch_nd)->nd_tx_work = 1;
1840
1841         retval = 1;
1842 out:
1843         spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
1844         return retval;
1845 }
1846
1847
1848
1849 /*
1850  *      Flush TX buffer (make in == out)
1851  *
1852  *      check tty_ioctl.c  -- this is called after TCOFLUSH
1853  */
1854 static void dgrp_tty_flush_buffer(struct tty_struct *tty)
1855 {
1856         struct un_struct *un;
1857         struct ch_struct *ch;
1858
1859         if (!tty)
1860                 return;
1861         un = tty->driver_data;
1862         if (!un)
1863                 return;
1864
1865         ch = un->un_ch;
1866         if (!ch)
1867                 return;
1868
1869         ch->ch_tout = ch->ch_tin;
1870         /* do NOT do this here! */
1871         /* ch->ch_s_tpos = ch->ch_s_tin = 0; */
1872
1873         /* send the flush output command now */
1874         ch->ch_send |= RR_TX_FLUSH;
1875         (ch->ch_nd)->nd_tx_ready = 1;
1876         (ch->ch_nd)->nd_tx_work = 1;
1877         wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
1878
1879         if (waitqueue_active(&tty->write_wait))
1880                 wake_up_interruptible(&tty->write_wait);
1881
1882         tty_wakeup(tty);
1883
1884 }
1885
1886 /*
1887  *      Return space available in Tx buffer
1888  *      count = ( ch->ch_tout - ch->ch_tin ) mod (TBUF_MAX - 1)
1889  */
1890 static int dgrp_tty_write_room(struct tty_struct *tty)
1891 {
1892         struct un_struct *un;
1893         struct ch_struct *ch;
1894         int     count;
1895
1896         if (!tty)
1897                 return 0;
1898
1899         un = tty->driver_data;
1900         if (!un)
1901                 return 0;
1902
1903         ch = un->un_ch;
1904         if (!ch)
1905                 return 0;
1906
1907         count = (ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK;
1908
1909         /* We *MUST* check this, and return 0 if the Printer Unit cannot
1910          * take any more data within its time constraints...  If we don't
1911          * return 0 and the printer has hit it time constraint, the ld will
1912          * call us back doing a put_char, which cannot be rejected!!!
1913          */
1914         if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1915                 int un_flag = 0;
1916                 count = dgrp_calculate_txprint_bounds(ch, count, &un_flag);
1917                 if (count <= 0)
1918                         count = 0;
1919
1920                 ch->ch_pun.un_flag |= un_flag;
1921                 (ch->ch_nd)->nd_tx_work = 1;
1922         }
1923
1924         return count;
1925 }
1926
1927 /*
1928  *      Return number of characters that have not been transmitted yet.
1929  *      chars_in_buffer = ( ch->ch_tin - ch->ch_tout ) mod (TBUF_MAX - 1)
1930  *                      + ( ch->ch_s_tin - ch->ch_s_tout ) mod (0xffff)
1931  *                      = number of characters "in transit"
1932  *
1933  * Remember that sequence number math is always with a sixteen bit
1934  * mask, not the TBUF_MASK.
1935  */
1936
1937 static int dgrp_tty_chars_in_buffer(struct tty_struct *tty)
1938 {
1939         struct un_struct *un;
1940         struct ch_struct *ch;
1941         int     count;
1942         int     count1;
1943
1944         if (!tty)
1945                 return 0;
1946
1947         un = tty->driver_data;
1948         if (!un)
1949                 return 0;
1950
1951         ch = un->un_ch;
1952         if (!ch)
1953                 return 0;
1954
1955         count1 = count = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1956         count += (ch->ch_s_tin - ch->ch_s_tpos) & 0xffff;
1957         /* one for tbuf, one for the PS */
1958
1959         /*
1960          * If we are busy transmitting add 1
1961          */
1962         count += un->un_tbusy;
1963
1964         return count;
1965 }
1966
1967
1968 /*****************************************************************************
1969  *
1970  * Helper applications for dgrp_tty_ioctl()
1971  *
1972  *****************************************************************************
1973  */
1974
1975
1976 /**
1977  * ch_to_tty_flags() -- convert channel flags to termio flags
1978  * @ch_flag: Digi channel flags
1979  * @flagtype: type of ch_flag (iflag, oflag or cflag)
1980  *
1981  * take the channel flags of the specified type and return the
1982  * corresponding termio flag
1983  */
1984 static tcflag_t ch_to_tty_flags(ushort ch_flag, char flagtype)
1985 {
1986         tcflag_t retval = 0;
1987
1988         switch (flagtype) {
1989         case 'i':
1990                 retval = ((ch_flag & IF_IGNBRK) ? IGNBRK : 0)
1991                      | ((ch_flag & IF_BRKINT) ? BRKINT : 0)
1992                      | ((ch_flag & IF_IGNPAR) ? IGNPAR : 0)
1993                      | ((ch_flag & IF_PARMRK) ? PARMRK : 0)
1994                      | ((ch_flag & IF_INPCK) ? INPCK  : 0)
1995                      | ((ch_flag & IF_ISTRIP) ? ISTRIP : 0)
1996                      | ((ch_flag & IF_IXON) ? IXON   : 0)
1997                      | ((ch_flag & IF_IXANY) ? IXANY  : 0)
1998                      | ((ch_flag & IF_IXOFF) ? IXOFF  : 0);
1999                 break;
2000
2001         case 'o':
2002                 retval = ((ch_flag & OF_OLCUC) ? OLCUC : 0)
2003                      | ((ch_flag & OF_ONLCR) ? ONLCR  : 0)
2004                      | ((ch_flag & OF_OCRNL) ? OCRNL  : 0)
2005                      | ((ch_flag & OF_ONOCR) ? ONOCR  : 0)
2006                      | ((ch_flag & OF_ONLRET) ? ONLRET : 0)
2007                   /* | ((ch_flag & OF_OTAB3) ? OFILL  : 0) */
2008                      | ((ch_flag & OF_TABDLY) ? TABDLY : 0);
2009                 break;
2010
2011         case 'c':
2012                 retval = ((ch_flag & CF_CSTOPB) ? CSTOPB : 0)
2013                      | ((ch_flag & CF_CREAD) ? CREAD  : 0)
2014                      | ((ch_flag & CF_PARENB) ? PARENB : 0)
2015                      | ((ch_flag & CF_PARODD) ? PARODD : 0)
2016                      | ((ch_flag & CF_HUPCL) ? HUPCL  : 0);
2017
2018                 switch (ch_flag & CF_CSIZE) {
2019                 case CF_CS5:
2020                         retval |= CS5;
2021                         break;
2022                 case CF_CS6:
2023                         retval |= CS6;
2024                         break;
2025                 case CF_CS7:
2026                         retval |= CS7;
2027                         break;
2028                 case CF_CS8:
2029                         retval |= CS8;
2030                         break;
2031                 default:
2032                         retval |= CS8;
2033                         break;
2034                 }
2035                 break;
2036         case 'x':
2037                 break;
2038         case 'l':
2039                 break;
2040         default:
2041                 return 0;
2042         }
2043
2044         return retval;
2045 }
2046
2047
2048 /**
2049  * tty_to_ch_flags() -- convert termio flags to digi channel flags
2050  * @tty: pointer to a TTY structure holding flag to be converted
2051  * @flagtype: identifies which flag (iflags, oflags, or cflags) should
2052  *                 be converted
2053  *
2054  * take the termio flag of the specified type and return the
2055  * corresponding Digi version of the flags
2056  */
2057 static ushort tty_to_ch_flags(struct tty_struct *tty, char flagtype)
2058 {
2059         ushort retval = 0;
2060         tcflag_t tflag = 0;
2061
2062         switch (flagtype) {
2063         case 'i':
2064                 tflag  = tty->termios.c_iflag;
2065                 retval = (I_IGNBRK(tty) ? IF_IGNBRK : 0)
2066                       | (I_BRKINT(tty) ? IF_BRKINT : 0)
2067                       | (I_IGNPAR(tty) ? IF_IGNPAR : 0)
2068                       | (I_PARMRK(tty) ? IF_PARMRK : 0)
2069                       | (I_INPCK(tty)  ? IF_INPCK  : 0)
2070                       | (I_ISTRIP(tty) ? IF_ISTRIP : 0)
2071                       | (I_IXON(tty)   ? IF_IXON   : 0)
2072                       | (I_IXANY(tty)  ? IF_IXANY  : 0)
2073                       | (I_IXOFF(tty)  ? IF_IXOFF  : 0);
2074                 break;
2075         case 'o':
2076                 tflag  = tty->termios.c_oflag;
2077                 /*
2078                  * If OPOST is set, then do the post processing in the
2079                  * firmware by setting all the processing flags on.
2080                  * If ~OPOST, then make sure we are not doing any
2081                  * output processing!!
2082                  */
2083                 if (!O_OPOST(tty))
2084                         retval = 0;
2085                 else
2086                         retval = (O_OLCUC(tty) ? OF_OLCUC : 0)
2087                              | (O_ONLCR(tty)  ? OF_ONLCR  : 0)
2088                              | (O_OCRNL(tty)  ? OF_OCRNL  : 0)
2089                              | (O_ONOCR(tty)  ? OF_ONOCR  : 0)
2090                              | (O_ONLRET(tty) ? OF_ONLRET : 0)
2091                           /* | (O_OFILL(tty)  ? OF_TAB3   : 0) */
2092                              | (O_TABDLY(tty) ? OF_TABDLY : 0);
2093                 break;
2094         case 'c':
2095                 tflag  = tty->termios.c_cflag;
2096                 retval = (C_CSTOPB(tty) ? CF_CSTOPB : 0)
2097                      | (C_CREAD(tty)  ? CF_CREAD  : 0)
2098                      | (C_PARENB(tty) ? CF_PARENB : 0)
2099                      | (C_PARODD(tty) ? CF_PARODD : 0)
2100                      | (C_HUPCL(tty)  ? CF_HUPCL  : 0);
2101                 switch (C_CSIZE(tty)) {
2102                 case CS8:
2103                         retval |= CF_CS8;
2104                         break;
2105                 case CS7:
2106                         retval |= CF_CS7;
2107                         break;
2108                 case CS6:
2109                         retval |= CF_CS6;
2110                         break;
2111                 case CS5:
2112                         retval |= CF_CS5;
2113                         break;
2114                 default:
2115                         retval |= CF_CS8;
2116                         break;
2117                 }
2118                 break;
2119         case 'x':
2120                 break;
2121         case 'l':
2122                 break;
2123         default:
2124                 return 0;
2125         }
2126
2127         return retval;
2128 }
2129
2130
2131 static int dgrp_tty_send_break(struct tty_struct *tty, int msec)
2132 {
2133         struct un_struct *un;
2134         struct ch_struct *ch;
2135         int ret = -EIO;
2136
2137         if (!tty)
2138                 return ret;
2139
2140         un = tty->driver_data;
2141         if (!un)
2142                 return ret;
2143
2144         ch = un->un_ch;
2145         if (!ch)
2146                 return ret;
2147
2148         dgrp_send_break(ch, msec);
2149         return 0;
2150 }
2151
2152
2153 /*
2154  * This routine sends a break character out the serial port.
2155  *
2156  * duration is in 1/1000's of a second
2157  */
2158 static int dgrp_send_break(struct ch_struct *ch, int msec)
2159 {
2160         ulong x;
2161
2162         wait_event_interruptible(ch->ch_flag_wait,
2163                 ((ch->ch_flag & CH_TX_BREAK) == 0));
2164         ch->ch_break_time += max(msec, 250);
2165         ch->ch_send |= RR_TX_BREAK;
2166         ch->ch_flag |= CH_TX_BREAK;
2167         (ch->ch_nd)->nd_tx_work = 1;
2168
2169         x = (msec * HZ) / 1000;
2170         wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2171
2172         return 0;
2173 }
2174
2175
2176 /*
2177  * Return modem signals to ld.
2178  */
2179 static int dgrp_tty_tiocmget(struct tty_struct *tty)
2180 {
2181         unsigned int mlast;
2182         struct un_struct *un = tty->driver_data;
2183         struct ch_struct *ch;
2184
2185         if (!un)
2186                 return -ENODEV;
2187
2188         ch = un->un_ch;
2189         if (!ch)
2190                 return -ENODEV;
2191
2192         mlast = ((ch->ch_s_mlast & ~(DM_RTS | DM_DTR)) |
2193                 (ch->ch_mout & (DM_RTS | DM_DTR)));
2194
2195         /* defined in /usr/include/asm/termios.h */
2196         mlast =   ((mlast & DM_RTS) ? TIOCM_RTS : 0)
2197                 | ((mlast & DM_DTR) ? TIOCM_DTR : 0)
2198                 | ((mlast & DM_CD)  ? TIOCM_CAR : 0)
2199                 | ((mlast & DM_RI)  ? TIOCM_RNG : 0)
2200                 | ((mlast & DM_DSR) ? TIOCM_DSR : 0)
2201                 | ((mlast & DM_CTS) ? TIOCM_CTS : 0);
2202
2203         return mlast;
2204 }
2205
2206
2207 /*
2208  *      Set modem lines
2209  */
2210 static int dgrp_tty_tiocmset(struct tty_struct *tty,
2211                              unsigned int set, unsigned int clear)
2212 {
2213         ulong lock_flags;
2214         struct un_struct *un = tty->driver_data;
2215         struct ch_struct *ch;
2216
2217         if (!un)
2218                 return -ENODEV;
2219
2220         ch = un->un_ch;
2221         if (!ch)
2222                 return -ENODEV;
2223
2224         if (set & TIOCM_RTS)
2225                 ch->ch_mout |= DM_RTS;
2226
2227         if (set & TIOCM_DTR)
2228                 ch->ch_mout |= DM_DTR;
2229
2230         if (clear & TIOCM_RTS)
2231                 ch->ch_mout &= ~(DM_RTS);
2232
2233         if (clear & TIOCM_DTR)
2234                 ch->ch_mout &= ~(DM_DTR);
2235
2236         spin_lock_irqsave(&(ch->ch_nd)->nd_lock, lock_flags);
2237         ch->ch_flag |= CH_PARAM;
2238         (ch->ch_nd)->nd_tx_work = 1;
2239         wake_up_interruptible(&ch->ch_flag_wait);
2240
2241         spin_unlock_irqrestore(&(ch->ch_nd)->nd_lock, lock_flags);
2242
2243         return 0;
2244 }
2245
2246
2247
2248 /*
2249  *      Get current modem status
2250  */
2251 static int get_modem_info(struct ch_struct *ch, unsigned int *value)
2252 {
2253         unsigned int mlast;
2254
2255         mlast = ((ch->ch_s_mlast & ~(DM_RTS | DM_DTR)) |
2256                 (ch->ch_mout    &  (DM_RTS | DM_DTR)));
2257
2258         /* defined in /usr/include/asm/termios.h */
2259         mlast =   ((mlast & DM_RTS) ? TIOCM_RTS : 0)
2260                 | ((mlast & DM_DTR) ? TIOCM_DTR : 0)
2261                 | ((mlast & DM_CD)  ? TIOCM_CAR : 0)
2262                 | ((mlast & DM_RI)  ? TIOCM_RNG : 0)
2263                 | ((mlast & DM_DSR) ? TIOCM_DSR : 0)
2264                 | ((mlast & DM_CTS) ? TIOCM_CTS : 0);
2265         return put_user(mlast, (unsigned int __user *) value);
2266 }
2267
2268 /*
2269  *      Set modem lines
2270  */
2271 static int set_modem_info(struct ch_struct *ch, unsigned int command,
2272                           unsigned int *value)
2273 {
2274         int error;
2275         unsigned int arg;
2276         int mval = 0;
2277         ulong lock_flags;
2278
2279         error = access_ok(VERIFY_READ, (void __user *) value, sizeof(int));
2280         if (error == 0)
2281                 return -EFAULT;
2282
2283         if (get_user(arg, (unsigned int __user *) value))
2284                 return -EFAULT;
2285         mval |= ((arg & TIOCM_RTS) ? DM_RTS : 0)
2286                 | ((arg & TIOCM_DTR) ? DM_DTR : 0);
2287
2288         switch (command) {
2289         case TIOCMBIS:  /* set flags */
2290                 ch->ch_mout |= mval;
2291                 break;
2292         case TIOCMBIC:  /* clear flags */
2293                 ch->ch_mout &= ~mval;
2294                 break;
2295         case TIOCMSET:
2296                 ch->ch_mout = mval;
2297                 break;
2298         default:
2299                 return -EINVAL;
2300         }
2301
2302         spin_lock_irqsave(&(ch->ch_nd)->nd_lock, lock_flags);
2303
2304         ch->ch_flag |= CH_PARAM;
2305         (ch->ch_nd)->nd_tx_work = 1;
2306         wake_up_interruptible(&ch->ch_flag_wait);
2307
2308         spin_unlock_irqrestore(&(ch->ch_nd)->nd_lock, lock_flags);
2309
2310         return 0;
2311 }
2312
2313
2314 /*
2315  *  Assign the custom baud rate to the channel structure
2316  */
2317 static void dgrp_set_custom_speed(struct ch_struct *ch, int newrate)
2318 {
2319         int testdiv;
2320         int testrate_high;
2321         int testrate_low;
2322
2323         int deltahigh, deltalow;
2324
2325         if (newrate < 0)
2326                 newrate = 0;
2327
2328         /*
2329          * Since the divisor is stored in a 16-bit integer, we make sure
2330          * we don't allow any rates smaller than a 16-bit integer would allow.
2331          * And of course, rates above the dividend won't fly.
2332          */
2333         if (newrate && newrate < ((PORTSERVER_DIVIDEND / 0xFFFF) + 1))
2334                 newrate = ((PORTSERVER_DIVIDEND / 0xFFFF) + 1);
2335         if (newrate && newrate > PORTSERVER_DIVIDEND)
2336                 newrate = PORTSERVER_DIVIDEND;
2337
2338         while (newrate > 0) {
2339                 testdiv = PORTSERVER_DIVIDEND / newrate;
2340
2341                 /*
2342                  * If we try to figure out what rate the PortServer would use
2343                  * with the test divisor, it will be either equal or higher
2344                  * than the requested baud rate.  If we then determine the
2345                  * rate with a divisor one higher, we will get the next lower
2346                  * supported rate below the requested.
2347                  */
2348                 testrate_high = PORTSERVER_DIVIDEND / testdiv;
2349                 testrate_low  = PORTSERVER_DIVIDEND / (testdiv + 1);
2350
2351                 /*
2352                  * If the rate for the requested divisor is correct, just
2353                  * use it and be done.
2354                  */
2355                 if (testrate_high == newrate)
2356                         break;
2357
2358                 /*
2359                  * Otherwise, pick the rate that is closer (i.e. whichever rate
2360                  * has a smaller delta).
2361                  */
2362                 deltahigh = testrate_high - newrate;
2363                 deltalow = newrate - testrate_low;
2364
2365                 if (deltahigh < deltalow)
2366                         newrate = testrate_high;
2367                 else
2368                         newrate = testrate_low;
2369
2370                 break;
2371         }
2372
2373         ch->ch_custom_speed = newrate;
2374
2375         drp_param(ch);
2376
2377         return;
2378 }
2379
2380
2381 /*
2382  # dgrp_tty_digiseta()
2383  *
2384  * Ioctl to set the information from ditty.
2385  *
2386  * NOTE: DIGI_IXON, DSRPACE, DCDPACE, and DTRPACE are unsupported.  JAR 990922
2387  */
2388 static int dgrp_tty_digiseta(struct tty_struct *tty,
2389                              struct digi_struct *new_info)
2390 {
2391         struct un_struct *un = tty->driver_data;
2392         struct ch_struct *ch;
2393
2394         if (!un)
2395                 return -ENODEV;
2396
2397         ch = un->un_ch;
2398         if (!ch)
2399                 return -ENODEV;
2400
2401         if (copy_from_user(&ch->ch_digi, (void __user *) new_info,
2402                            sizeof(struct digi_struct)))
2403                 return -EFAULT;
2404
2405         if ((ch->ch_digi.digi_flags & RTSPACE) ||
2406             (ch->ch_digi.digi_flags & CTSPACE))
2407                 tty->termios.c_cflag |= CRTSCTS;
2408         else
2409                 tty->termios.c_cflag &= ~CRTSCTS;
2410
2411         if (ch->ch_digi.digi_maxcps < 1)
2412                 ch->ch_digi.digi_maxcps = 1;
2413
2414         if (ch->ch_digi.digi_maxcps > 10000)
2415                 ch->ch_digi.digi_maxcps = 10000;
2416
2417         if (ch->ch_digi.digi_bufsize < 10)
2418                 ch->ch_digi.digi_bufsize = 10;
2419
2420         if (ch->ch_digi.digi_maxchar < 1)
2421                 ch->ch_digi.digi_maxchar = 1;
2422
2423         if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2424                 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2425
2426         if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2427                 ch->ch_digi.digi_onlen = DIGI_PLEN;
2428
2429         if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2430                 ch->ch_digi.digi_offlen = DIGI_PLEN;
2431
2432         /* make the changes now */
2433         drp_param(ch);
2434
2435         return 0;
2436 }
2437
2438
2439
2440 /*
2441  * dgrp_tty_digigetedelay()
2442  *
2443  * Ioctl to get the current edelay setting.
2444  *
2445  *
2446  *
2447  */
2448 static int dgrp_tty_digigetedelay(struct tty_struct *tty, int *retinfo)
2449 {
2450         struct un_struct *un;
2451         struct ch_struct *ch;
2452         int tmp;
2453
2454         if (!retinfo)
2455                 return -EFAULT;
2456
2457         if (!tty || tty->magic != TTY_MAGIC)
2458                 return -EFAULT;
2459
2460         un = tty->driver_data;
2461
2462         if (!un)
2463                 return -ENODEV;
2464
2465         ch = un->un_ch;
2466         if (!ch)
2467                 return -ENODEV;
2468
2469         tmp = ch->ch_edelay;
2470
2471         if (copy_to_user((void __user *) retinfo, &tmp, sizeof(*retinfo)))
2472                 return -EFAULT;
2473
2474         return 0;
2475 }
2476
2477
2478 /*
2479  * dgrp_tty_digisetedelay()
2480  *
2481  * Ioctl to set the EDELAY setting
2482  *
2483  */
2484 static int dgrp_tty_digisetedelay(struct tty_struct *tty, int *new_info)
2485 {
2486         struct un_struct *un;
2487         struct ch_struct *ch;
2488         int new_digi;
2489
2490         if (!tty || tty->magic != TTY_MAGIC)
2491                 return -EFAULT;
2492
2493         un = tty->driver_data;
2494
2495         if (!un)
2496                 return -ENODEV;
2497
2498         ch = un->un_ch;
2499         if (!ch)
2500                 return -ENODEV;
2501
2502         if (copy_from_user(&new_digi, (void __user *)new_info, sizeof(int)))
2503                 return -EFAULT;
2504
2505         ch->ch_edelay = new_digi;
2506
2507         /* make the changes now */
2508         drp_param(ch);
2509
2510         return 0;
2511 }
2512
2513
2514 /*
2515  *      The usual assortment of ioctl's
2516  *
2517  *      note:  use tty_check_change to make sure that we are not
2518  *      changing the state of a terminal when we are not a process
2519  *      in the forground.  See tty_io.c
2520  *              rc = tty_check_change(tty);
2521  *              if (rc) return rc;
2522  */
2523 static int dgrp_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2524                           unsigned long arg)
2525 {
2526         struct un_struct *un;
2527         struct ch_struct *ch;
2528         int rc;
2529         struct digiflow_struct   dflow;
2530
2531         if (!tty)
2532                 return -ENODEV;
2533
2534         un = tty->driver_data;
2535         if (!un)
2536                 return -ENODEV;
2537
2538         ch = un->un_ch;
2539         if (!ch)
2540                 return -ENODEV;
2541
2542         switch (cmd) {
2543
2544         /*
2545          * Here are all the standard ioctl's that we MUST implement
2546          */
2547
2548         case TCSBRK:
2549                 /*
2550                  * TCSBRK is SVID version: non-zero arg --> no break
2551                  * this behaviour is exploited by tcdrain().
2552                  *
2553                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2554                  * between 0.25 and 0.5 seconds
2555                  */
2556
2557                 rc = tty_check_change(tty);
2558                 if (rc)
2559                         return rc;
2560                 tty_wait_until_sent(tty, 0);
2561
2562                 if (!arg)
2563                         rc = dgrp_send_break(ch, 250); /* 1/4 second */
2564
2565                 if (dgrp_tty_chars_in_buffer(tty) != 0)
2566                         return -EINTR;
2567
2568                 return 0;
2569
2570         case TCSBRKP:
2571                 /* support for POSIX tcsendbreak()
2572                  *
2573                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2574                  * between 0.25 and 0.5 seconds so we'll ask for something
2575                  * in the middle: 0.375 seconds.
2576                  */
2577                 rc = tty_check_change(tty);
2578                 if (rc)
2579                         return rc;
2580                 tty_wait_until_sent(tty, 0);
2581
2582                 rc = dgrp_send_break(ch, arg ? arg*250 : 250);
2583
2584                 if (dgrp_tty_chars_in_buffer(tty) != 0)
2585                         return -EINTR;
2586                 return 0;
2587
2588         case TIOCSBRK:
2589                 rc = tty_check_change(tty);
2590                 if (rc)
2591                         return rc;
2592                 tty_wait_until_sent(tty, 0);
2593
2594                 /*
2595                  * RealPort doesn't support turning on a break unconditionally.
2596                  * The RealPort device will stop sending a break automatically
2597                  * after the specified time value that we send in.
2598                  */
2599                 rc = dgrp_send_break(ch, 250); /* 1/4 second */
2600
2601                 if (dgrp_tty_chars_in_buffer(tty) != 0)
2602                         return -EINTR;
2603                 return 0;
2604
2605         case TIOCCBRK:
2606                 /*
2607                  * RealPort doesn't support turning off a break unconditionally.
2608                  * The RealPort device will stop sending a break automatically
2609                  * after the specified time value that was sent when turning on
2610                  * the break.
2611                  */
2612                 return 0;
2613
2614         case TIOCMGET:
2615                 rc = access_ok(VERIFY_WRITE, (void __user *) arg,
2616                                  sizeof(unsigned int));
2617                 if (rc == 0)
2618                         return -EFAULT;
2619                 return get_modem_info(ch, (unsigned int *) arg);
2620
2621         case TIOCMBIS:
2622         case TIOCMBIC:
2623         case TIOCMSET:
2624                 return set_modem_info(ch, cmd, (unsigned int *) arg);
2625
2626         /*
2627          * Here are any additional ioctl's that we want to implement
2628          */
2629
2630         case TCFLSH:
2631                 /*
2632                  * The linux tty driver doesn't have a flush
2633                  * input routine for the driver, assuming all backed
2634                  * up data is in the line disc. buffers.  However,
2635                  * we all know that's not the case.  Here, we
2636                  * act on the ioctl, but then lie and say we didn't
2637                  * so the line discipline will process the flush
2638                  * also.
2639                  */
2640                 rc = tty_check_change(tty);
2641                 if (rc)
2642                         return rc;
2643
2644                 switch (arg) {
2645                 case TCIFLUSH:
2646                 case TCIOFLUSH:
2647                         /* only flush input if this is the only open unit */
2648                         if (!IS_PRINT(MINOR(tty_devnum(tty)))) {
2649                                 ch->ch_rout = ch->ch_rin;
2650                                 ch->ch_send |= RR_RX_FLUSH;
2651                                 (ch->ch_nd)->nd_tx_work = 1;
2652                                 (ch->ch_nd)->nd_tx_ready = 1;
2653                                 wake_up_interruptible(
2654                                         &(ch->ch_nd)->nd_tx_waitq);
2655                         }
2656                         if (arg == TCIFLUSH)
2657                                 break;
2658
2659                 case TCOFLUSH: /* flush output, or the receive buffer */
2660                         /*
2661                          * This is handled in the tty_ioctl.c code
2662                          * calling tty_flush_buffer
2663                          */
2664                         break;
2665
2666                 default:
2667                         /* POSIX.1 says return EINVAL if we got a bad arg */
2668                         return -EINVAL;
2669                 }
2670                 /* pretend we didn't recognize this IOCTL */
2671                 return -ENOIOCTLCMD;
2672
2673 #ifdef TIOCGETP
2674         case TIOCGETP:
2675 #endif
2676         /*****************************************
2677         Linux           HPUX            Function
2678         TCSETA          TCSETA          - set the termios
2679         TCSETAF         TCSETAF         - wait for drain first, then set termios
2680         TCSETAW         TCSETAW         - wait for drain,
2681                                         flush the input queue, then set termios
2682         - looking at the tty_ioctl code, these command all call our
2683         tty_set_termios at the driver's end, when a TCSETA* is sent,
2684         it is expecting the tty to have a termio structure,
2685         NOT a termios structure.  These two structures differ in size
2686         and the tty_ioctl code does a conversion before processing them both.
2687         - we should treat the TCSETAW TCSETAF ioctls the same, and let
2688         the tty_ioctl code do the conversion stuff.
2689
2690         TCSETS
2691         TCSETSF         (none)
2692         TCSETSW
2693         - the associated tty structure has a termios structure.
2694         *****************************************/
2695
2696         case TCGETS:
2697         case TCGETA:
2698                 return -ENOIOCTLCMD;
2699
2700         case TCSETAW:
2701         case TCSETAF:
2702         case TCSETSF:
2703         case TCSETSW:
2704                 /*
2705                  * The linux tty driver doesn't have a flush
2706                  * input routine for the driver, assuming all backed
2707                  * up data is in the line disc. buffers.  However,
2708                  * we all know that's not the case.  Here, we
2709                  * act on the ioctl, but then lie and say we didn't
2710                  * so the line discipline will process the flush
2711                  * also.
2712                  */
2713
2714                 /*
2715                  * Also, now that we have TXPrint, we have to check
2716                  * if this is the TXPrint device and the terminal
2717                  * device is open. If so, do NOT run check_change,
2718                  * as the terminal device is ALWAYS the parent.
2719                  */
2720                 if (!IS_PRINT(MINOR(tty_devnum(tty))) ||
2721                     !ch->ch_tun.un_open_count) {
2722                         rc = tty_check_change(tty);
2723                         if (rc)
2724                                 return rc;
2725                 }
2726
2727                 /* wait for all the characters in tbuf to drain */
2728                 tty_wait_until_sent(tty, 0);
2729
2730                 if ((cmd == TCSETSF) || (cmd == TCSETAF)) {
2731                         /* flush the contents of the rbuf queue */
2732                         /* TODO:  check if this is print device? */
2733                         ch->ch_send |= RR_RX_FLUSH;
2734                         (ch->ch_nd)->nd_tx_ready = 1;
2735                         (ch->ch_nd)->nd_tx_work = 1;
2736                         wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2737                         /* do we need to do this?  just to be safe! */
2738                         ch->ch_rout = ch->ch_rin;
2739                 }
2740
2741                 /* pretend we didn't recognize this */
2742                 return -ENOIOCTLCMD;
2743
2744         case TCXONC:
2745                 /*
2746                  * The Linux Line Discipline (LD) would do this for us if we
2747                  * let it, but we have the special firmware options to do this
2748                  * the "right way" regardless of hardware or software flow
2749                  * control so we'll do it outselves instead of letting the LD
2750                  * do it.
2751                  */
2752                 rc = tty_check_change(tty);
2753                 if (rc)
2754                         return rc;
2755
2756                 switch (arg) {
2757                 case TCOON:
2758                         dgrp_tty_start(tty);
2759                         return 0;
2760                 case TCOOFF:
2761                         dgrp_tty_stop(tty);
2762                         return 0;
2763                 case TCION:
2764                         dgrp_tty_input_start(tty);
2765                         return 0;
2766                 case TCIOFF:
2767                         dgrp_tty_input_stop(tty);
2768                         return 0;
2769                 default:
2770                         return -EINVAL;
2771                 }
2772
2773         case DIGI_GETA:
2774                 /* get information for ditty */
2775                 if (copy_to_user((struct digi_struct __user *) arg,
2776                                  &ch->ch_digi, sizeof(struct digi_struct)))
2777                         return -EFAULT;
2778                 break;
2779
2780         case DIGI_SETAW:
2781         case DIGI_SETAF:
2782                 /* wait for all the characters in tbuf to drain */
2783                 tty_wait_until_sent(tty, 0);
2784
2785                 if (cmd == DIGI_SETAF) {
2786                         /* flush the contents of the rbuf queue */
2787                         /* send down a packet with RR_RX_FLUSH set */
2788                         ch->ch_send |= RR_RX_FLUSH;
2789                         (ch->ch_nd)->nd_tx_ready = 1;
2790                         (ch->ch_nd)->nd_tx_work = 1;
2791                         wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2792                         /* do we need to do this?  just to be safe! */
2793                         ch->ch_rout = ch->ch_rin;
2794                 }
2795
2796                 /* pretend we didn't recognize this */
2797                 /* fall-through */
2798
2799         case DIGI_SETA:
2800                 return dgrp_tty_digiseta(tty, (struct digi_struct *) arg);
2801
2802         case DIGI_SEDELAY:
2803                 return dgrp_tty_digisetedelay(tty, (int *) arg);
2804
2805         case DIGI_GEDELAY:
2806                 return dgrp_tty_digigetedelay(tty, (int *) arg);
2807
2808         case DIGI_GETFLOW:
2809         case DIGI_GETAFLOW:
2810                 if (cmd == (DIGI_GETFLOW)) {
2811                         dflow.startc = tty->termios.c_cc[VSTART];
2812                         dflow.stopc = tty->termios.c_cc[VSTOP];
2813                 } else {
2814                         dflow.startc = ch->ch_xxon;
2815                         dflow.stopc = ch->ch_xxoff;
2816                 }
2817
2818                 if (copy_to_user((char __user *)arg, &dflow, sizeof(dflow)))
2819                         return -EFAULT;
2820                 break;
2821
2822         case DIGI_SETFLOW:
2823         case DIGI_SETAFLOW:
2824
2825                 if (copy_from_user(&dflow, (char __user *)arg, sizeof(dflow)))
2826                         return -EFAULT;
2827
2828                 if (cmd == (DIGI_SETFLOW)) {
2829                         tty->termios.c_cc[VSTART] = dflow.startc;
2830                         tty->termios.c_cc[VSTOP] = dflow.stopc;
2831                 } else {
2832                         ch->ch_xxon = dflow.startc;
2833                         ch->ch_xxoff = dflow.stopc;
2834                 }
2835                 break;
2836
2837         case DIGI_GETCUSTOMBAUD:
2838                 if (put_user(ch->ch_custom_speed, (unsigned int __user *) arg))
2839                         return -EFAULT;
2840                 break;
2841
2842         case DIGI_SETCUSTOMBAUD:
2843         {
2844                 int new_rate;
2845
2846                 if (get_user(new_rate, (unsigned int __user *) arg))
2847                         return -EFAULT;
2848                 dgrp_set_custom_speed(ch, new_rate);
2849
2850                 break;
2851         }
2852
2853         default:
2854                 return -ENOIOCTLCMD;
2855         }
2856
2857         return 0;
2858 }
2859
2860 /*
2861  *  This routine allows the tty driver to be notified when
2862  *  the device's termios setting have changed.  Note that we
2863  *  should be prepared to accept the case where old == NULL
2864  *  and try to do something rational.
2865  *
2866  *  So we need to make sure that our copies of ch_oflag,
2867  *  ch_clag, and ch_iflag reflect the tty->termios flags.
2868  */
2869 static void dgrp_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
2870 {
2871         struct ktermios *ts;
2872         struct ch_struct *ch;
2873         struct un_struct *un;
2874
2875         /* seems silly, but we have to check all these! */
2876         if (!tty)
2877                 return;
2878
2879         un = tty->driver_data;
2880         if (!un)
2881                 return;
2882
2883         ts = &tty->termios;
2884
2885         ch = un->un_ch;
2886         if (!ch)
2887                 return;
2888
2889         drp_param(ch);
2890
2891         /* the CLOCAL flag has just been set */
2892         if (!(old->c_cflag & CLOCAL) && C_CLOCAL(tty))
2893                 wake_up_interruptible(&un->un_open_wait);
2894 }
2895
2896
2897 /*
2898  *      Throttle receiving data.  We just set a bit and stop reading
2899  *      data out of the channel buffer.  It will back up and the
2900  *      FEP will do whatever is necessary to stop the far end.
2901  */
2902 static void dgrp_tty_throttle(struct tty_struct *tty)
2903 {
2904         struct ch_struct *ch;
2905
2906         if (!tty)
2907                 return;
2908
2909         ch = ((struct un_struct *) tty->driver_data)->un_ch;
2910         if (!ch)
2911                 return;
2912
2913         ch->ch_flag |= CH_RXSTOP;
2914 }
2915
2916
2917 static void dgrp_tty_unthrottle(struct tty_struct *tty)
2918 {
2919         struct ch_struct *ch;
2920
2921         if (!tty)
2922                 return;
2923
2924         ch = ((struct un_struct *) tty->driver_data)->un_ch;
2925         if (!ch)
2926                 return;
2927
2928         ch->ch_flag &= ~CH_RXSTOP;
2929 }
2930
2931 /*
2932  *      Stop the transmitter
2933  */
2934 static void dgrp_tty_stop(struct tty_struct *tty)
2935 {
2936         struct ch_struct *ch;
2937
2938         if (!tty)
2939                 return;
2940
2941         ch = ((struct un_struct *) tty->driver_data)->un_ch;
2942         if (!ch)
2943                 return;
2944
2945         ch->ch_send |= RR_TX_STOP;
2946         ch->ch_send &= ~RR_TX_START;
2947
2948         /* make the change NOW! */
2949         (ch->ch_nd)->nd_tx_ready = 1;
2950         if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
2951                 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2952 }
2953
2954 /*
2955  *      Start the transmitter
2956  */
2957 static void dgrp_tty_start(struct tty_struct *tty)
2958 {
2959         struct ch_struct *ch;
2960
2961         if (!tty)
2962                 return;
2963
2964         ch = ((struct un_struct *) tty->driver_data)->un_ch;
2965         if (!ch)
2966                 return;
2967
2968         /* TODO: don't do anything if the transmitter is not stopped */
2969
2970         ch->ch_send |= RR_TX_START;
2971         ch->ch_send &= ~RR_TX_STOP;
2972
2973         /* make the change NOW! */
2974         (ch->ch_nd)->nd_tx_ready = 1;
2975         (ch->ch_nd)->nd_tx_work = 1;
2976         if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
2977                 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2978
2979 }
2980
2981 /*
2982  *      Stop the receiver
2983  */
2984 static void dgrp_tty_input_stop(struct tty_struct *tty)
2985 {
2986         struct ch_struct *ch;
2987
2988         if (!tty)
2989                 return;
2990
2991         ch = ((struct un_struct *) tty->driver_data)->un_ch;
2992         if (!ch)
2993                 return;
2994
2995         ch->ch_send |= RR_RX_STOP;
2996         ch->ch_send &= ~RR_RX_START;
2997         (ch->ch_nd)->nd_tx_ready = 1;
2998         if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
2999                 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
3000
3001 }
3002
3003
3004 static void dgrp_tty_send_xchar(struct tty_struct *tty, char c)
3005 {
3006         struct un_struct *un;
3007         struct ch_struct *ch;
3008
3009         if (!tty)
3010                 return;
3011
3012         un = tty->driver_data;
3013         if (!un)
3014                 return;
3015
3016         ch = un->un_ch;
3017         if (!ch)
3018                 return;
3019         if (c == STOP_CHAR(tty))
3020                 ch->ch_send |= RR_RX_STOP;
3021         else if (c == START_CHAR(tty))
3022                 ch->ch_send |= RR_RX_START;
3023
3024         ch->ch_nd->nd_tx_ready = 1;
3025         ch->ch_nd->nd_tx_work = 1;
3026
3027         return;
3028 }
3029
3030
3031 static void dgrp_tty_input_start(struct tty_struct *tty)
3032 {
3033         struct ch_struct *ch;
3034
3035         if (!tty)
3036                 return;
3037
3038         ch = ((struct un_struct *) tty->driver_data)->un_ch;
3039         if (!ch)
3040                 return;
3041
3042         ch->ch_send |= RR_RX_START;
3043         ch->ch_send &= ~RR_RX_STOP;
3044         (ch->ch_nd)->nd_tx_ready = 1;
3045         (ch->ch_nd)->nd_tx_work = 1;
3046         if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
3047                 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
3048
3049 }
3050
3051
3052 /*
3053  *      Hangup the port.  Like a close, but don't wait for output
3054  *      to drain.
3055  *
3056  *      How do we close all the channels that are open?
3057  */
3058 static void dgrp_tty_hangup(struct tty_struct *tty)
3059 {
3060         struct ch_struct *ch;
3061         struct nd_struct *nd;
3062         struct un_struct *un;
3063
3064         if (!tty)
3065                 return;
3066
3067         un = tty->driver_data;
3068         if (!un)
3069                 return;
3070
3071         ch = un->un_ch;
3072         if (!ch)
3073                 return;
3074
3075         nd = ch->ch_nd;
3076
3077         if (C_HUPCL(tty)) {
3078                 /* LOWER DTR */
3079                 ch->ch_mout &= ~DM_DTR;
3080                 /* Don't do this here */
3081                 /* ch->ch_flag |= CH_HANGUP; */
3082                 ch->ch_nd->nd_tx_ready = 1;
3083                 ch->ch_nd->nd_tx_work  = 1;
3084                 if (waitqueue_active(&ch->ch_flag_wait))
3085                         wake_up_interruptible(&ch->ch_flag_wait);
3086         }
3087
3088 }
3089
3090 /************************************************************************/
3091 /*                                                                      */
3092 /*       TTY Initialization/Cleanup Functions                           */
3093 /*                                                                      */
3094 /************************************************************************/
3095
3096 /*
3097  *      Uninitialize the TTY portion of the supplied node.  Free all
3098  *      memory and resources associated with this node.  Do it in reverse
3099  *      allocation order: this might possibly result in less fragmentation
3100  *      of memory, though I don't know this for sure.
3101  */
3102 void
3103 dgrp_tty_uninit(struct nd_struct *nd)
3104 {
3105         unsigned int i;
3106         char id[3];
3107
3108         ID_TO_CHAR(nd->nd_ID, id);
3109
3110         if (nd->nd_ttdriver_flags & SERIAL_TTDRV_REG) {
3111                 tty_unregister_driver(nd->nd_serial_ttdriver);
3112
3113                 kfree(nd->nd_serial_ttdriver->ttys);
3114                 nd->nd_serial_ttdriver->ttys = NULL;
3115
3116                 put_tty_driver(nd->nd_serial_ttdriver);
3117                 nd->nd_ttdriver_flags &= ~SERIAL_TTDRV_REG;
3118         }
3119
3120         if (nd->nd_ttdriver_flags & CALLOUT_TTDRV_REG) {
3121                 tty_unregister_driver(nd->nd_callout_ttdriver);
3122
3123                 kfree(nd->nd_callout_ttdriver->ttys);
3124                 nd->nd_callout_ttdriver->ttys = NULL;
3125
3126                 put_tty_driver(nd->nd_callout_ttdriver);
3127                 nd->nd_ttdriver_flags &= ~CALLOUT_TTDRV_REG;
3128         }
3129
3130         if (nd->nd_ttdriver_flags & XPRINT_TTDRV_REG) {
3131                 tty_unregister_driver(nd->nd_xprint_ttdriver);
3132
3133                 kfree(nd->nd_xprint_ttdriver->ttys);
3134                 nd->nd_xprint_ttdriver->ttys = NULL;
3135
3136                 put_tty_driver(nd->nd_xprint_ttdriver);
3137                 nd->nd_ttdriver_flags &= ~XPRINT_TTDRV_REG;
3138         }
3139         for (i = 0; i < CHAN_MAX; i++)
3140                 tty_port_destroy(&nd->nd_chan[i].port);
3141 }
3142
3143
3144
3145 /*
3146  *     Initialize the TTY portion of the supplied node.
3147  */
3148 int
3149 dgrp_tty_init(struct nd_struct *nd)
3150 {
3151         char id[3];
3152         int  rc;
3153         int  i;
3154
3155         ID_TO_CHAR(nd->nd_ID, id);
3156
3157         /*
3158          *  Initialize the TTDRIVER structures.
3159          */
3160
3161         nd->nd_serial_ttdriver = alloc_tty_driver(CHAN_MAX);
3162         if (!nd->nd_serial_ttdriver)
3163                 return -ENOMEM;
3164
3165         sprintf(nd->nd_serial_name,  "tty_dgrp_%s_", id);
3166
3167         nd->nd_serial_ttdriver->owner = THIS_MODULE;
3168         nd->nd_serial_ttdriver->name = nd->nd_serial_name;
3169         nd->nd_serial_ttdriver->name_base = 0;
3170         nd->nd_serial_ttdriver->major = 0;
3171         nd->nd_serial_ttdriver->minor_start = 0;
3172         nd->nd_serial_ttdriver->type = TTY_DRIVER_TYPE_SERIAL;
3173         nd->nd_serial_ttdriver->subtype = SERIAL_TYPE_NORMAL;
3174         nd->nd_serial_ttdriver->init_termios = DefaultTermios;
3175         nd->nd_serial_ttdriver->driver_name = "dgrp";
3176         nd->nd_serial_ttdriver->flags = (TTY_DRIVER_REAL_RAW |
3177                                          TTY_DRIVER_DYNAMIC_DEV |
3178                                          TTY_DRIVER_HARDWARE_BREAK);
3179
3180         /* The kernel wants space to store pointers to tty_structs. */
3181         nd->nd_serial_ttdriver->ttys =
3182                 kzalloc(CHAN_MAX * sizeof(struct tty_struct *), GFP_KERNEL);
3183         if (!nd->nd_serial_ttdriver->ttys)
3184                 return -ENOMEM;
3185
3186         tty_set_operations(nd->nd_serial_ttdriver, &dgrp_tty_ops);
3187
3188         if (!(nd->nd_ttdriver_flags & SERIAL_TTDRV_REG)) {
3189                 /*
3190                  *   Register tty devices
3191                  */
3192                 rc = tty_register_driver(nd->nd_serial_ttdriver);
3193                 if (rc < 0) {
3194                         /*
3195                          * If errno is EBUSY, this means there are no more
3196                          * slots available to have us auto-majored.
3197                          * (Which is currently supported up to 256)
3198                          *
3199                          * We can still request majors above 256,
3200                          * we just have to do it manually.
3201                          */
3202                         if (rc == -EBUSY) {
3203                                 int i;
3204                                 int max_majors = 1U << (32 - MINORBITS);
3205                                 for (i = 256; i < max_majors; i++) {
3206                                         nd->nd_serial_ttdriver->major = i;
3207                                         rc = tty_register_driver
3208                                                 (nd->nd_serial_ttdriver);
3209                                         if (rc >= 0)
3210                                                 break;
3211                                 }
3212                                 /* Really fail now, since we ran out
3213                                  * of majors to try. */
3214                                 if (i == max_majors)
3215                                         return rc;
3216
3217                         } else {
3218                                 return rc;
3219                         }
3220                 }
3221                 nd->nd_ttdriver_flags |= SERIAL_TTDRV_REG;
3222         }
3223
3224         nd->nd_callout_ttdriver = alloc_tty_driver(CHAN_MAX);
3225         if (!nd->nd_callout_ttdriver)
3226                 return -ENOMEM;
3227
3228         sprintf(nd->nd_callout_name, "cu_dgrp_%s_",  id);
3229
3230         nd->nd_callout_ttdriver->owner = THIS_MODULE;
3231         nd->nd_callout_ttdriver->name = nd->nd_callout_name;
3232         nd->nd_callout_ttdriver->name_base = 0;
3233         nd->nd_callout_ttdriver->major = nd->nd_serial_ttdriver->major;
3234         nd->nd_callout_ttdriver->minor_start = 0x40;
3235         nd->nd_callout_ttdriver->type = TTY_DRIVER_TYPE_SERIAL;
3236         nd->nd_callout_ttdriver->subtype = SERIAL_TYPE_CALLOUT;
3237         nd->nd_callout_ttdriver->init_termios = DefaultTermios;
3238         nd->nd_callout_ttdriver->driver_name = "dgrp";
3239         nd->nd_callout_ttdriver->flags = (TTY_DRIVER_REAL_RAW |
3240                                           TTY_DRIVER_DYNAMIC_DEV |
3241                                           TTY_DRIVER_HARDWARE_BREAK);
3242
3243         /* The kernel wants space to store pointers to tty_structs. */
3244         nd->nd_callout_ttdriver->ttys =
3245                 kzalloc(CHAN_MAX * sizeof(struct tty_struct *), GFP_KERNEL);
3246         if (!nd->nd_callout_ttdriver->ttys)
3247                 return -ENOMEM;
3248
3249         tty_set_operations(nd->nd_callout_ttdriver, &dgrp_tty_ops);
3250
3251         if (dgrp_register_cudevices) {
3252                 if (!(nd->nd_ttdriver_flags & CALLOUT_TTDRV_REG)) {
3253                         /*
3254                          *   Register cu devices
3255                          */
3256                         rc = tty_register_driver(nd->nd_callout_ttdriver);
3257                         if (rc < 0)
3258                                 return rc;
3259                         nd->nd_ttdriver_flags |= CALLOUT_TTDRV_REG;
3260                 }
3261         }
3262
3263
3264         nd->nd_xprint_ttdriver = alloc_tty_driver(CHAN_MAX);
3265         if (!nd->nd_xprint_ttdriver)
3266                 return -ENOMEM;
3267
3268         sprintf(nd->nd_xprint_name,  "pr_dgrp_%s_", id);
3269
3270         nd->nd_xprint_ttdriver->owner = THIS_MODULE;
3271         nd->nd_xprint_ttdriver->name = nd->nd_xprint_name;
3272         nd->nd_xprint_ttdriver->name_base = 0;
3273         nd->nd_xprint_ttdriver->major = nd->nd_serial_ttdriver->major;
3274         nd->nd_xprint_ttdriver->minor_start = 0x80;
3275         nd->nd_xprint_ttdriver->type = TTY_DRIVER_TYPE_SERIAL;
3276         nd->nd_xprint_ttdriver->subtype = SERIAL_TYPE_XPRINT;
3277         nd->nd_xprint_ttdriver->init_termios = DefaultTermios;
3278         nd->nd_xprint_ttdriver->driver_name = "dgrp";
3279         nd->nd_xprint_ttdriver->flags = (TTY_DRIVER_REAL_RAW |
3280                                          TTY_DRIVER_DYNAMIC_DEV |
3281                                          TTY_DRIVER_HARDWARE_BREAK);
3282
3283         /* The kernel wants space to store pointers to tty_structs. */
3284         nd->nd_xprint_ttdriver->ttys =
3285                 kzalloc(CHAN_MAX * sizeof(struct tty_struct *), GFP_KERNEL);
3286         if (!nd->nd_xprint_ttdriver->ttys)
3287                 return -ENOMEM;
3288
3289         tty_set_operations(nd->nd_xprint_ttdriver, &dgrp_tty_ops);
3290
3291         if (dgrp_register_prdevices) {
3292                 if (!(nd->nd_ttdriver_flags & XPRINT_TTDRV_REG)) {
3293                         /*
3294                          *   Register transparent print devices
3295                          */
3296                         rc = tty_register_driver(nd->nd_xprint_ttdriver);
3297                         if (rc < 0)
3298                                 return rc;
3299                         nd->nd_ttdriver_flags |= XPRINT_TTDRV_REG;
3300                 }
3301         }
3302
3303         for (i = 0; i < CHAN_MAX; i++) {
3304                 struct ch_struct *ch = nd->nd_chan + i;
3305
3306                 ch->ch_nd = nd;
3307                 ch->ch_digi = digi_init;
3308                 ch->ch_edelay = 100;
3309                 ch->ch_custom_speed = 0;
3310                 ch->ch_portnum = i;
3311                 ch->ch_tun.un_ch = ch;
3312                 ch->ch_pun.un_ch = ch;
3313                 ch->ch_tun.un_type = SERIAL_TYPE_NORMAL;
3314                 ch->ch_pun.un_type = SERIAL_TYPE_XPRINT;
3315
3316                 init_waitqueue_head(&(ch->ch_flag_wait));
3317                 init_waitqueue_head(&(ch->ch_sleep));
3318
3319                 init_waitqueue_head(&(ch->ch_tun.un_open_wait));
3320                 init_waitqueue_head(&(ch->ch_tun.un_close_wait));
3321
3322                 init_waitqueue_head(&(ch->ch_pun.un_open_wait));
3323                 init_waitqueue_head(&(ch->ch_pun.un_close_wait));
3324                 tty_port_init(&ch->port);
3325         }
3326         return 0;
3327 }