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