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