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