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>
7 * Scott Kilau <scottk at digi dot com>
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)
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.
29 * This file implements the tty driver functionality for the
30 * RealPort driver software.
39 #include <linux/slab.h>
40 #include <linux/tty.h>
41 #include <linux/tty_flip.h>
42 #include <linux/device.h>
43 #include <linux/sched.h>
44 #include <linux/uaccess.h>
46 #include "dgrp_common.h"
48 #ifndef _POSIX_VDISABLE
49 #define _POSIX_VDISABLE ('\0')
53 * forward declarations
56 static void drp_param(struct ch_struct *);
57 static void dgrp_tty_close(struct tty_struct *, struct file *);
59 /* ioctl helper functions */
60 static int set_modem_info(struct ch_struct *, unsigned int, unsigned int *);
61 static int get_modem_info(struct ch_struct *, unsigned int *);
62 static void dgrp_set_custom_speed(struct ch_struct *, int);
63 static int dgrp_tty_digigetedelay(struct tty_struct *, int *);
64 static int dgrp_tty_digisetedelay(struct tty_struct *, int *);
65 static int dgrp_send_break(struct ch_struct *, int);
67 static ushort tty_to_ch_flags(struct tty_struct *, char);
68 static tcflag_t ch_to_tty_flags(unsigned short, char);
70 static void dgrp_tty_input_start(struct tty_struct *);
71 static void dgrp_tty_input_stop(struct tty_struct *);
73 static void drp_wmove(struct ch_struct *, int, void*, int);
75 static int dgrp_tty_open(struct tty_struct *, struct file *);
76 static void dgrp_tty_close(struct tty_struct *, struct file *);
77 static int dgrp_tty_write(struct tty_struct *, const unsigned char *, int);
78 static int dgrp_tty_write_room(struct tty_struct *);
79 static void dgrp_tty_flush_buffer(struct tty_struct *);
80 static int dgrp_tty_chars_in_buffer(struct tty_struct *);
81 static int dgrp_tty_ioctl(struct tty_struct *, unsigned int, unsigned long);
82 static void dgrp_tty_set_termios(struct tty_struct *, struct ktermios *);
83 static void dgrp_tty_stop(struct tty_struct *);
84 static void dgrp_tty_start(struct tty_struct *);
85 static void dgrp_tty_throttle(struct tty_struct *);
86 static void dgrp_tty_unthrottle(struct tty_struct *);
87 static void dgrp_tty_hangup(struct tty_struct *);
88 static int dgrp_tty_put_char(struct tty_struct *, unsigned char);
89 static int dgrp_tty_tiocmget(struct tty_struct *);
90 static int dgrp_tty_tiocmset(struct tty_struct *, unsigned int, unsigned int);
91 static int dgrp_tty_send_break(struct tty_struct *, int);
92 static void dgrp_tty_send_xchar(struct tty_struct *, char);
97 #define SERIAL_TYPE_NORMAL 1
98 #define SERIAL_TYPE_CALLOUT 2
99 #define SERIAL_TYPE_XPRINT 3
103 * tty globals/statics
107 #define PORTSERVER_DIVIDEND 1843200
110 * Default transparent print information.
112 static struct digi_struct digi_init = {
113 .digi_flags = DIGI_COOK, /* Flags */
114 .digi_maxcps = 100, /* Max CPS */
115 .digi_maxchar = 50, /* Max chars in print queue */
116 .digi_bufsize = 100, /* Printer buffer size */
117 .digi_onlen = 4, /* size of printer on string */
118 .digi_offlen = 4, /* size of printer off string */
119 .digi_onstr = "\033[5i", /* ANSI printer on string */
120 .digi_offstr = "\033[4i", /* ANSI printer off string */
121 .digi_term = "ansi" /* default terminal type */
125 * Define a local default termios struct. All ports will be created
126 * with this termios initially.
128 * This defines a raw port at 9600 baud, 8 data bits, no parity,
131 static struct ktermios DefaultTermios = {
132 .c_iflag = (ICRNL | IXON),
133 .c_oflag = (OPOST | ONLCR),
134 .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
135 .c_lflag = (ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL
141 /* Define our tty operations struct */
142 static const struct tty_operations dgrp_tty_ops = {
143 .open = dgrp_tty_open,
144 .close = dgrp_tty_close,
145 .write = dgrp_tty_write,
146 .write_room = dgrp_tty_write_room,
147 .flush_buffer = dgrp_tty_flush_buffer,
148 .chars_in_buffer = dgrp_tty_chars_in_buffer,
150 .ioctl = dgrp_tty_ioctl,
151 .set_termios = dgrp_tty_set_termios,
152 .stop = dgrp_tty_stop,
153 .start = dgrp_tty_start,
154 .throttle = dgrp_tty_throttle,
155 .unthrottle = dgrp_tty_unthrottle,
156 .hangup = dgrp_tty_hangup,
157 .put_char = dgrp_tty_put_char,
158 .tiocmget = dgrp_tty_tiocmget,
159 .tiocmset = dgrp_tty_tiocmset,
160 .break_ctl = dgrp_tty_send_break,
161 .send_xchar = dgrp_tty_send_xchar
165 static int calc_baud_rate(struct un_struct *un)
175 static struct baud_rates baud_rates[] = {
198 brate = C_BAUD(un->un_tty);
200 for (i = 0; baud_rates[i].rate; i++) {
201 if (baud_rates[i].cflag == brate)
205 return baud_rates[i].rate;
208 static int calc_fastbaud_rate(struct un_struct *un, struct ktermios *uts)
213 ulong bauds[2][16] = {
215 0, 57600, 76800, 115200,
216 131657, 153600, 230400, 460800,
217 921600, 1200, 1800, 2400,
218 4800, 9600, 19200, 38400 },
219 { /* fastbaud & CBAUDEX */
220 0, 57600, 115200, 230400,
221 460800, 150, 200, 921600,
222 600, 1200, 1800, 2400,
223 4800, 9600, 19200, 38400 }
226 brate = C_BAUD(un->un_tty) & 0xff;
228 i = (uts->c_cflag & CBAUDEX) ? 1 : 0;
231 if ((i >= 0) && (i < 2) && (brate >= 0) && (brate < 16))
232 brate = bauds[i][brate];
240 * drp_param() -- send parameter values to be sent to the node
241 * @ch: channel structure of port to modify
243 * Interprets the tty and modem changes made by an application
244 * program (by examining the termios structures) and sets up
245 * parameter values to be sent to the node.
247 static void drp_param(struct ch_struct *ch)
249 struct nd_struct *nd;
250 struct un_struct *un;
255 struct ktermios *tts, *pts, *uts;
260 * If the terminal device is open, use it to set up all tty
261 * modes and functions. Otherwise use the printer device.
264 if (ch->ch_tun.un_open_count) {
267 tts = &ch->ch_tun.un_tty->termios;
270 * If both devices are open, copy critical line
271 * parameters from the tty device to the printer,
272 * so that if the tty is closed, the printer will
273 * continue without disruption.
276 if (ch->ch_pun.un_open_count) {
278 pts = &ch->ch_pun.un_tty->termios;
281 (pts->c_cflag ^ tts->c_cflag) &
282 (CBAUD | CSIZE | CSTOPB | CREAD | PARENB |
283 PARODD | HUPCL | CLOCAL);
286 (pts->c_iflag ^ tts->c_iflag) &
287 (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK |
288 ISTRIP | IXON | IXANY | IXOFF);
290 pts->c_cc[VSTART] = tts->c_cc[VSTART];
291 pts->c_cc[VSTOP] = tts->c_cc[VSTOP];
293 } else if (ch->ch_pun.un_open_count == 0) {
294 pr_warn("%s - ch_pun.un_open_count shouldn't be 0\n",
301 uts = &un->un_tty->termios;
304 * Determine if FAST writes can be performed.
307 if ((ch->ch_digi.digi_flags & DIGI_COOK) != 0 &&
308 (ch->ch_tun.un_open_count != 0) &&
309 !((un->un_tty)->ldisc->ops->flags & LDISC_FLAG_DEFINED) &&
310 !(L_XCASE(un->un_tty))) {
311 ch->ch_flag |= CH_FAST_WRITE;
313 ch->ch_flag &= ~CH_FAST_WRITE;
317 * If FAST writes can be performed, and OPOST is on in the
318 * terminal device, do OPOST handling in the server.
321 if ((ch->ch_flag & CH_FAST_WRITE) &&
322 O_OPOST(un->un_tty) != 0) {
323 int oflag = tty_to_ch_flags(un->un_tty, 'o');
325 /* add to ch_ocook any processing flags set in the termio */
326 ch->ch_ocook |= oflag & (OF_OLCUC |
333 * the hpux driver clears any flags set in ch_ocook
334 * from the termios oflag. It is STILL reported though
338 oflag = ch_to_tty_flags(ch->ch_ocook, 'o');
339 uts->c_oflag &= ~oflag;
342 /* clear the ch->ch_ocook flag */
343 int oflag = ch_to_tty_flags(ch->ch_ocook, 'o');
344 uts->c_oflag |= oflag;
348 ch->ch_oflag = ch->ch_ocook;
351 ch->ch_flag &= ~CH_FAST_READ;
354 * Generate channel flags
357 if (C_BAUD(un->un_tty) == B0) {
358 if (!(ch->ch_flag & CH_BAUD0)) {
359 /* TODO : the HPUX driver flushes line */
360 /* TODO : discipline, I assume I don't have to */
362 ch->ch_tout = ch->ch_tin;
363 ch->ch_rout = ch->ch_rin;
365 ch->ch_break_time = 0;
367 ch->ch_send |= RR_TX_FLUSH | RR_RX_FLUSH;
369 ch->ch_mout &= ~(DM_DTR | DM_RTS);
371 ch->ch_flag |= CH_BAUD0;
373 } else if (ch->ch_custom_speed) {
374 ch->ch_brate = PORTSERVER_DIVIDEND / ch->ch_custom_speed;
376 if (ch->ch_flag & CH_BAUD0) {
377 ch->ch_mout |= DM_DTR | DM_RTS;
379 ch->ch_flag &= ~CH_BAUD0;
385 * If FASTBAUD isn't on, we can scan the new baud rate list
388 * However, if FASTBAUD is on, we must go to the old
389 * baud rate mapping that existed many many moons ago,
390 * for compatibility reasons.
393 if (!(ch->ch_digi.digi_flags & DIGI_FAST))
394 brate = calc_baud_rate(un);
396 brate = calc_fastbaud_rate(un, uts);
401 ch->ch_brate = PORTSERVER_DIVIDEND / brate;
403 if (ch->ch_flag & CH_BAUD0) {
404 ch->ch_mout |= DM_DTR | DM_RTS;
406 ch->ch_flag &= ~CH_BAUD0;
411 * Generate channel cflags from the termio.
414 ch->ch_cflag = tty_to_ch_flags(un->un_tty, 'c');
417 * Generate channel iflags from the termio.
420 iflag = (int) tty_to_ch_flags(un->un_tty, 'i');
422 if (START_CHAR(un->un_tty) == _POSIX_VDISABLE ||
423 STOP_CHAR(un->un_tty) == _POSIX_VDISABLE) {
424 iflag &= ~(IF_IXON | IF_IXANY | IF_IXOFF);
427 ch->ch_iflag = iflag;
430 * Generate flow control characters
434 * From the POSIX.1 spec (7.1.2.6): "If {_POSIX_VDISABLE}
435 * is defined for the terminal device file, and the value
436 * of one of the changeable special control characters (see
437 * 7.1.1.9) is {_POSIX_VDISABLE}, that function shall be
438 * disabled, that is, no input data shall be recognized as
439 * the disabled special character."
441 * OK, so we don't ever assign S/DXB XON or XOFF to _POSIX_VDISABLE.
444 if (uts->c_cc[VSTART] != _POSIX_VDISABLE)
445 ch->ch_xon = uts->c_cc[VSTART];
446 if (uts->c_cc[VSTOP] != _POSIX_VDISABLE)
447 ch->ch_xoff = uts->c_cc[VSTOP];
449 ch->ch_lnext = (uts->c_cc[VLNEXT] == _POSIX_VDISABLE ? 0 :
453 * Also, if either c_cc[START] or c_cc[STOP] is set to
454 * _POSIX_VDISABLE, we can't really do software flow
455 * control--in either direction--so we turn it off as
456 * far as S/DXB is concerned. In essence, if you disable
457 * one, you disable the other too.
459 if ((uts->c_cc[VSTART] == _POSIX_VDISABLE) ||
460 (uts->c_cc[VSTOP] == _POSIX_VDISABLE))
461 ch->ch_iflag &= ~(IF_IXOFF | IF_IXON);
469 if (ch->ch_digi.digi_flags & DIGI_AIXON)
472 if ((ch->ch_xxon == _POSIX_VDISABLE) ||
473 (ch->ch_xxoff == _POSIX_VDISABLE))
476 ch->ch_xflag = xflag;
480 * Figure effective DCD value.
483 if (C_CLOCAL(un->un_tty))
484 ch->ch_flag |= CH_CLOCAL;
486 ch->ch_flag &= ~CH_CLOCAL;
489 * Check modem signals
495 * Get hardware handshake value.
500 if (C_CRTSCTS(un->un_tty))
501 mflow |= (DM_RTS | DM_CTS);
503 if (ch->ch_digi.digi_flags & RTSPACE)
506 if (ch->ch_digi.digi_flags & DTRPACE)
509 if (ch->ch_digi.digi_flags & CTSPACE)
512 if (ch->ch_digi.digi_flags & DSRPACE)
515 if (ch->ch_digi.digi_flags & DCDPACE)
518 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
519 mflow |= DM_RTS_TOGGLE;
521 ch->ch_mflow = mflow;
524 * Send the changes to the server.
527 ch->ch_flag |= CH_PARAM;
528 (ch->ch_nd)->nd_tx_work = 1;
530 if (waitqueue_active(&ch->ch_flag_wait))
531 wake_up_interruptible(&ch->ch_flag_wait);
535 * This function is just used as a callback for timeouts
536 * waiting on the ch_sleep flag.
538 static void wake_up_drp_sleep_timer(unsigned long ptr)
540 struct ch_struct *ch = (struct ch_struct *) ptr;
542 wake_up(&ch->ch_sleep);
547 * Set up our own sleep that can't be cancelled
548 * until our timeout occurs.
550 static void drp_my_sleep(struct ch_struct *ch)
552 struct timer_list drp_wakeup_timer;
553 DECLARE_WAITQUEUE(wait, current);
556 * First make sure we're ready to receive the wakeup.
559 add_wait_queue(&ch->ch_sleep, &wait);
560 current->state = TASK_UNINTERRUPTIBLE;
563 * Since we are uninterruptible, set a timer to
564 * unset the uninterruptable state in 1 second.
567 init_timer(&drp_wakeup_timer);
568 drp_wakeup_timer.function = wake_up_drp_sleep_timer;
569 drp_wakeup_timer.data = (unsigned long) ch;
570 drp_wakeup_timer.expires = jiffies + (1 * HZ);
571 add_timer(&drp_wakeup_timer);
575 del_timer(&drp_wakeup_timer);
577 remove_wait_queue(&ch->ch_sleep, &wait);
584 * -EBUSY - this is a callout device and the normal device is active
585 * - there is an error in opening the tty
586 * -ENODEV - the channel does not exist
587 * -EAGAIN - we are in the middle of hanging up or closing
588 * - IMMEDIATE_OPEN fails
590 * - if the port is outside physical range
591 * -EINTR - the open is interrupted
594 static int dgrp_tty_open(struct tty_struct *tty, struct file *file)
597 struct nd_struct *nd;
598 struct ch_struct *ch;
599 struct un_struct *un;
606 int counts_were_incremented = 0;
608 DECLARE_WAITQUEUE(wait, current);
611 * Do some initial checks to see if the node and port exist
614 nd = nd_struct_get(MAJOR(tty_devnum(tty)));
615 port = PORT_NUM(MINOR(tty_devnum(tty)));
616 category = OPEN_CATEGORY(MINOR(tty_devnum(tty)));
621 if (port >= CHAN_MAX)
625 * The channel exists.
628 ch = nd->nd_chan + port;
630 un = IS_PRINT(MINOR(tty_devnum(tty))) ? &ch->ch_pun : &ch->ch_tun;
632 tty->driver_data = un;
635 * If we are in the middle of hanging up,
636 * then return an error
638 if (tty_hung_up_p(file)) {
639 retval = ((un->un_flag & UN_HUP_NOTIFY) ?
640 -EAGAIN : -ERESTARTSYS);
645 * If the port is in the middle of closing, then block
646 * until it is done, then try again.
648 retval = wait_event_interruptible(un->un_close_wait,
649 ((un->un_flag & UN_CLOSING) == 0));
655 * If the port is in the middle of a reopen after a network disconnect,
656 * wait until it is done, then try again.
658 retval = wait_event_interruptible(ch->ch_flag_wait,
659 ((ch->ch_flag & CH_PORT_GONE) == 0));
665 * If this is a callout device, then just make sure the normal
666 * device isn't being used.
669 if (tty->driver->subtype == SERIAL_TYPE_CALLOUT) {
670 if (un->un_flag & UN_NORMAL_ACTIVE) {
674 un->un_flag |= UN_CALLOUT_ACTIVE;
679 * Loop waiting until the open can be successfully completed.
682 spin_lock_irqsave(&nd->nd_lock, lock_flags);
690 * Determine the open type from the flags provided.
694 * If the port is not enabled, then exit
696 if (test_bit(TTY_IO_ERROR, &tty->flags)) {
697 /* there was an error in opening the tty */
698 if (un->un_flag & UN_CALLOUT_ACTIVE)
701 un->un_flag |= UN_NORMAL_ACTIVE;
705 if (file->f_flags & O_NONBLOCK) {
708 * if the O_NONBLOCK is set, errors on read and write
709 * must return -EAGAIN immediately and NOT sleep
712 otype = OTYPE_IMMEDIATE;
713 delay_error = -EAGAIN;
715 } else if (!OPEN_WAIT_AVAIL(category) ||
716 (file->f_flags & O_NDELAY) != 0) {
717 otype = OTYPE_IMMEDIATE;
718 delay_error = -EBUSY;
720 } else if (!OPEN_WAIT_CARRIER(category) ||
721 ((ch->ch_digi.digi_flags & DIGI_FORCEDCD) != 0) ||
723 otype = OTYPE_PERSISTENT;
727 otype = OTYPE_INCOMING;
732 * Handle port currently outside physical port range.
735 if (port >= nd->nd_chan_count) {
736 if (otype == OTYPE_IMMEDIATE) {
737 retval = (nd->nd_state == NS_READY) ?
744 * Handle port not currently open.
747 else if (ch->ch_open_count == 0) {
749 * Return an error when an Incoming Open
750 * response indicates the port is busy.
753 if (ch->ch_open_error != 0 && otype == ch->ch_otype) {
754 retval = (ch->ch_open_error <= 2) ?
755 delay_error : -ENXIO;
760 * Fail any new Immediate open if we do not have
761 * a normal connection to the server.
764 if (nd->nd_state != NS_READY &&
765 otype == OTYPE_IMMEDIATE) {
771 * If a Realport open of the correct type has
772 * succeeded, complete the open.
775 if (ch->ch_state == CS_READY && ch->ch_otype == otype)
780 * Handle port already open and active as a device
784 else if ((ch->ch_category == category) ||
785 IS_PRINT(MINOR(tty_devnum(tty)))) {
787 * Fail if opening the device now would
788 * violate exclusive use.
790 unf = ch->ch_tun.un_flag | ch->ch_pun.un_flag;
792 if ((file->f_flags & O_EXCL) || (unf & UN_EXCL)) {
798 * If the open device is in the hangup state, all
799 * system calls fail except close().
802 /* TODO : check on hangup_p calls */
804 if (ch->ch_flag & CH_HANGUP) {
810 * If the port is ready, and carrier is ignored
811 * or present, then complete the open.
814 if (ch->ch_state == CS_READY &&
815 (otype != OTYPE_INCOMING ||
816 ch->ch_flag & CH_VIRT_CD))
823 * Handle port active with a different category device.
827 if (otype == OTYPE_IMMEDIATE) {
828 retval = delay_error;
834 * Wait until conditions change, then take another
838 ch->ch_wait_count[otype]++;
841 ch->ch_wait_carrier++;
844 * Prepare the task to accept the wakeup, then
845 * release our locks and release control.
848 add_wait_queue(&ch->ch_flag_wait, &wait);
849 current->state = TASK_INTERRUPTIBLE;
851 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
854 * Give up control, we'll come back if we're
855 * interrupted or are woken up.
858 remove_wait_queue(&ch->ch_flag_wait, &wait);
860 spin_lock_irqsave(&nd->nd_lock, lock_flags);
862 current->state = TASK_RUNNING;
864 ch->ch_wait_count[otype]--;
867 ch->ch_wait_carrier--;
871 if (signal_pending(current)) {
878 * The open has succeeded. No turning back.
880 counts_were_incremented = 1;
885 * Initialize the channel, if it's not already open.
888 if (ch->ch_open_count == 1) {
891 ch->ch_category = category;
892 ch->ch_pscan_state = 0;
894 /* TODO : find out what PS-1 bug Gene was referring to */
895 /* TODO : in the following comment. */
897 ch->ch_send = RR_TX_START | RR_RX_START; /* PS-1 bug */
900 ch->ch_s_mlast & DM_CD ||
901 ch->ch_digi.digi_flags & DIGI_FORCEDCD)
902 ch->ch_flag |= CH_VIRT_CD;
903 else if (OPEN_FORCES_CARRIER(category))
904 ch->ch_flag |= CH_VIRT_CD;
909 * Initialize the unit, if it is not already open.
912 if (un->un_open_count == 1) {
914 * Since all terminal options are always sticky in Linux,
915 * we don't need the UN_STICKY flag to be handled specially.
917 /* clears all the digi flags, leaves serial flags */
918 un->un_flag &= ~UN_DIGI_MASK;
920 if (file->f_flags & O_EXCL)
921 un->un_flag |= UN_EXCL;
923 /* TODO : include "session" and "pgrp" */
926 * In Linux, all terminal parameters are intended to be sticky.
927 * as a result, we "remove" the code which once reset the ports
935 un->un_flag |= UN_INITIALIZED;
941 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
945 * Linux does a close for every open, even failed ones!
947 if (!counts_were_incremented) {
953 dev_err(tty->dev, "tty open bad return (%i)\n", retval);
962 * dgrp_tty_close() -- close function for tty_operations
964 static void dgrp_tty_close(struct tty_struct *tty, struct file *file)
966 struct ch_struct *ch;
967 struct un_struct *un;
968 struct nd_struct *nd;
975 int sent_printer_offstr = 0;
977 port = PORT_NUM(MINOR(tty_devnum(tty)));
979 un = tty->driver_data;
994 spin_lock_irqsave(&nd->nd_lock, lock_flags);
997 /* Used to be on channel basis, now we check on a unit basis. */
998 if (un->un_open_count != 1)
1002 * OK, its the last close on the unit
1004 un->un_flag |= UN_CLOSING;
1007 * Notify the discipline to only process XON/XOFF characters.
1012 * Wait for output to drain only if this is
1013 * the last close against the channel
1016 if (ch->ch_open_count == 1) {
1018 * If its the print device, we need to ensure at all costs that
1019 * the offstr will fit. If it won't, flush our tbuf.
1021 if (IS_PRINT(MINOR(tty_devnum(tty))) &&
1022 (((ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK) <
1023 ch->ch_digi.digi_offlen))
1024 ch->ch_tin = ch->ch_tout;
1027 * Turn off the printer. Don't bother checking to see if its
1028 * IS_PRINT... Since this is the last close the flag is going
1029 * to be cleared, so we MUST make sure the offstr gets inserted
1033 if ((ch->ch_flag & CH_PRON) != 0) {
1034 drp_wmove(ch, 0, ch->ch_digi.digi_offstr,
1035 ch->ch_digi.digi_offlen);
1036 ch->ch_flag &= ~CH_PRON;
1037 sent_printer_offstr = 1;
1042 * Wait until either the output queue has drained, or we see
1043 * absolutely no progress for 15 seconds.
1046 tpos = ch->ch_s_tpos;
1048 waketime = jiffies + 15 * HZ;
1053 * Make sure the port still exists.
1056 if (port >= nd->nd_chan_count) {
1061 if (signal_pending(current)) {
1067 * If the port is idle (not opened on the server), we have
1068 * no way of draining/flushing/closing the port on that server.
1069 * So break out of loop.
1071 if (ch->ch_state == CS_IDLE)
1077 * Exit if the queues for this unit are empty,
1078 * and either the other unit is still open or all
1082 if ((un->un_tty)->ops->chars_in_buffer ?
1083 ((un->un_tty)->ops->chars_in_buffer)(un->un_tty) == 0 : 1) {
1086 * We don't need to wait for a buffer to drain
1087 * if the other unit is open.
1090 if (ch->ch_open_count != un->un_open_count)
1094 * The wait is complete when all queues are
1095 * drained, and any break in progress is complete.
1098 if (ch->ch_tin == ch->ch_tout &&
1099 ch->ch_s_tin == ch->ch_s_tpos &&
1100 (ch->ch_send & RR_TX_BREAK) == 0) {
1106 * Flush TX data and exit the wait if NDELAY is set,
1107 * or this is not a DIGI printer, and the close timeout
1111 if ((file->f_flags & (O_NDELAY | O_NONBLOCK)) ||
1112 ((long)(jiffies - waketime) >= 0 &&
1113 (ch->ch_digi.digi_flags & DIGI_PRINTER) == 0)) {
1116 * If we sent the printer off string, we cannot
1117 * flush our internal buffers, or we might lose
1120 if (!sent_printer_offstr)
1121 dgrp_tty_flush_buffer(tty);
1123 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1124 tty_ldisc_flush(tty);
1125 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1130 * Otherwise take a short nap.
1133 ch->ch_flag |= CH_DRAIN;
1135 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1137 schedule_timeout_interruptible(1);
1138 s = signal_pending(current);
1140 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1144 * If we had sent the printer off string, we now have
1147 * The system won't let us sleep since we got an error
1148 * back from sleep, presumably because the user did
1150 * But we need to ensure that the offstr gets sent!
1151 * Thus, we have to do something else besides sleeping.
1153 * 1) Make this task uninterruptable.
1154 * 2) Set up a timer to go off in 1 sec.
1155 * 3) Act as tho we just got out of the sleep above.
1157 * Thankfully, in the real world, this just
1161 if (sent_printer_offstr) {
1162 spin_unlock_irqrestore(&nd->nd_lock,
1165 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1173 * Restart the wait if any progress is seen.
1176 if (ch->ch_s_tpos != tpos) {
1177 tpos = ch->ch_s_tpos;
1179 /* TODO: this gives us timeout problems with nist ?? */
1180 waketime = jiffies + 15 * HZ;
1185 * Close the line discipline
1188 /* this is done in tty_io.c */
1189 /* if ((un->un_tty)->ldisc.close)
1190 * ((un->un_tty)->ldisc.close)(un->un_tty);
1193 /* don't do this here */
1194 /* un->un_flag = 0; */
1197 * Flush the receive buffer on terminal unit close only.
1200 if (!IS_PRINT(MINOR(tty_devnum(tty))))
1201 ch->ch_rout = ch->ch_rin;
1205 * Don't permit the close to happen until we get any pending
1206 * sync request responses.
1207 * There could be other ports depending upon the response as well.
1209 * Also, don't permit the close to happen until any parameter
1210 * changes have been sent out from the state machine as well.
1211 * This is required because of a ditty -a race with -HUPCL
1212 * We MUST make sure all channel parameters have been sent to the
1213 * Portserver before sending a close.
1216 if ((err != 1) && (ch->ch_state != CS_IDLE)) {
1217 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1218 s = wait_event_interruptible(ch->ch_flag_wait,
1219 ((ch->ch_flag & (CH_WAITING_SYNC | CH_PARAM)) == 0));
1220 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1224 * Cleanup the channel if last unit open.
1227 if (ch->ch_open_count == 1) {
1229 ch->ch_category = 0;
1232 ch->ch_tout = ch->ch_tin;
1233 /* (un->un_tty)->device = 0; */
1235 if (ch->ch_state == CS_READY)
1236 ch->ch_state = CS_SEND_CLOSE;
1240 * Send the changes to the server
1242 if (ch->ch_state != CS_IDLE) {
1243 ch->ch_flag |= CH_PARAM;
1244 wake_up_interruptible(&ch->ch_flag_wait);
1248 nd->nd_tx_ready = 1;
1253 if (ch->ch_open_count <= 0)
1255 "%s - unexpected value for ch->ch_open_count: %i\n",
1256 __func__, ch->ch_open_count);
1258 ch->ch_open_count--;
1260 if (un->un_open_count <= 0)
1262 "%s - unexpected value for un->un_open_count: %i\n",
1263 __func__, un->un_open_count);
1265 un->un_open_count--;
1267 un->un_flag &= ~(UN_NORMAL_ACTIVE | UN_CALLOUT_ACTIVE | UN_CLOSING);
1268 if (waitqueue_active(&un->un_close_wait))
1269 wake_up_interruptible(&un->un_close_wait);
1271 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1277 static void drp_wmove(struct ch_struct *ch, int from_user, void *buf, int count)
1282 ch->ch_nd->nd_tx_work = 1;
1284 n = TBUF_MAX - ch->ch_tin;
1288 ret = copy_from_user(ch->ch_tbuf + ch->ch_tin,
1289 (void __user *) buf, n);
1291 memcpy(ch->ch_tbuf + ch->ch_tin, buf, n);
1293 buf = (char *) buf + n;
1299 ret = copy_from_user(ch->ch_tbuf + ch->ch_tin,
1300 (void __user *) buf, count);
1302 memcpy(ch->ch_tbuf + ch->ch_tin, buf, count);
1304 ch->ch_tin += count;
1308 static int dgrp_calculate_txprint_bounds(struct ch_struct *ch, int space,
1313 unsigned short tmax = 0;
1316 * If the terminal device is busy, reschedule when
1317 * the terminal device becomes idle.
1320 if (ch->ch_tun.un_open_count != 0 &&
1321 ch->ch_tun.un_tty->ops->chars_in_buffer &&
1322 ((ch->ch_tun.un_tty->ops->chars_in_buffer)
1323 (ch->ch_tun.un_tty) != 0)) {
1324 *un_flag = UN_PWAIT;
1329 * Assure that whenever there is printer data in the output
1330 * buffer, there always remains enough space after it to
1331 * turn the printer off.
1333 space -= ch->ch_digi.digi_offlen;
1336 *un_flag = UN_EMPTY;
1341 * We measure printer CPS speed by incrementing
1342 * ch_cpstime by (HZ / digi_maxcps) for every
1343 * character we output, restricting output so
1344 * that ch_cpstime never exceeds lbolt.
1346 * However if output has not been done for some
1347 * time, lbolt will grow to very much larger than
1348 * ch_cpstime, which would allow essentially
1349 * unlimited amounts of output until ch_cpstime
1350 * finally caught up. To avoid this, we adjust
1351 * cps_time when necessary so the difference
1352 * between lbolt and ch_cpstime never results
1353 * in sending more than digi_bufsize characters.
1355 * This nicely models a printer with an internal
1356 * buffer of digi_bufsize characters.
1358 * Get the time between lbolt and ch->ch_cpstime;
1361 tt = jiffies - ch->ch_cpstime;
1364 * Compute the time required to send digi_bufsize
1368 mt = HZ * ch->ch_digi.digi_bufsize / ch->ch_digi.digi_maxcps;
1371 * Compute the number of characters that can be sent
1372 * without violating the time constraint. If the
1373 * direct calculation of this number is bigger than
1374 * digi_bufsize, limit the number to digi_bufsize,
1375 * and adjust cpstime to match.
1378 if ((clock_t)(tt + HZ) > (clock_t)(mt + HZ)) {
1379 tmax = ch->ch_digi.digi_bufsize;
1380 ch->ch_cpstime = jiffies - mt;
1382 tmax = ch->ch_digi.digi_maxcps * tt / HZ;
1386 * If the time constraint now binds, limit the transmit
1387 * count accordingly, and tentatively arrange to be
1388 * rescheduled based on time.
1397 * Compute the total number of characters we can
1398 * output before the total number of characters known
1399 * to be in the output queue exceeds digi_maxchar.
1402 tmax = (ch->ch_digi.digi_maxchar -
1403 ((ch->ch_tin - ch->ch_tout) & TBUF_MASK) -
1404 ((ch->ch_s_tin - ch->ch_s_tpos) & 0xffff));
1408 * If the digi_maxchar constraint now holds, limit
1409 * the transmit count accordingly, and arrange to
1410 * be rescheduled when the queue becomes empty.
1414 *un_flag = UN_EMPTY;
1419 *un_flag |= UN_EMPTY;
1425 static int dgrp_tty_write(struct tty_struct *tty,
1426 const unsigned char *buf,
1429 struct nd_struct *nd;
1430 struct un_struct *un;
1431 struct ch_struct *ch;
1442 un = tty->driver_data;
1455 * Ignore the request if the channel is not ready.
1457 if (ch->ch_state != CS_READY)
1460 spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
1463 * Ignore the request if output is blocked.
1465 if ((un->un_flag & (UN_EMPTY | UN_LOW | UN_TIME | UN_PWAIT)) != 0) {
1471 * Also ignore the request if DPA has this port open,
1472 * and is flow controlled on reading more data.
1474 if (nd->nd_dpa_debug && nd->nd_dpa_flag & DPA_WAIT_SPACE &&
1475 nd->nd_dpa_port == MINOR(tty_devnum(ch->ch_tun.un_tty))) {
1481 * Limit amount we will write to the amount of space
1482 * available in the channel buffer.
1486 space = (ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK;
1489 * Handle the printer device.
1494 if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1497 unsigned short tmax = 0;
1500 * If the terminal device is busy, reschedule when
1501 * the terminal device becomes idle.
1504 if (ch->ch_tun.un_open_count != 0 &&
1505 ((ch->ch_tun.un_tty->ops->chars_in_buffer)
1506 (ch->ch_tun.un_tty) != 0)) {
1507 un->un_flag |= UN_PWAIT;
1513 * Assure that whenever there is printer data in the output
1514 * buffer, there always remains enough space after it to
1515 * turn the printer off.
1517 space -= ch->ch_digi.digi_offlen;
1520 * Output the printer on string.
1523 if ((ch->ch_flag & CH_PRON) == 0) {
1524 space -= ch->ch_digi.digi_onlen;
1527 un->un_flag |= UN_EMPTY;
1528 (ch->ch_nd)->nd_tx_work = 1;
1533 drp_wmove(ch, 0, ch->ch_digi.digi_onstr,
1534 ch->ch_digi.digi_onlen);
1536 ch->ch_flag |= CH_PRON;
1540 * We measure printer CPS speed by incrementing
1541 * ch_cpstime by (HZ / digi_maxcps) for every
1542 * character we output, restricting output so
1543 * that ch_cpstime never exceeds lbolt.
1545 * However if output has not been done for some
1546 * time, lbolt will grow to very much larger than
1547 * ch_cpstime, which would allow essentially
1548 * unlimited amounts of output until ch_cpstime
1549 * finally caught up. To avoid this, we adjust
1550 * cps_time when necessary so the difference
1551 * between lbolt and ch_cpstime never results
1552 * in sending more than digi_bufsize characters.
1554 * This nicely models a printer with an internal
1555 * buffer of digi_bufsize characters.
1557 * Get the time between lbolt and ch->ch_cpstime;
1560 tt = jiffies - ch->ch_cpstime;
1563 * Compute the time required to send digi_bufsize
1567 mt = HZ * ch->ch_digi.digi_bufsize / ch->ch_digi.digi_maxcps;
1570 * Compute the number of characters that can be sent
1571 * without violating the time constraint. If the
1572 * direct calculation of this number is bigger than
1573 * digi_bufsize, limit the number to digi_bufsize,
1574 * and adjust cpstime to match.
1577 if ((clock_t)(tt + HZ) > (clock_t)(mt + HZ)) {
1578 tmax = ch->ch_digi.digi_bufsize;
1579 ch->ch_cpstime = jiffies - mt;
1581 tmax = ch->ch_digi.digi_maxcps * tt / HZ;
1585 * If the time constraint now binds, limit the transmit
1586 * count accordingly, and tentatively arrange to be
1587 * rescheduled based on time.
1596 * Compute the total number of characters we can
1597 * output before the total number of characters known
1598 * to be in the output queue exceeds digi_maxchar.
1601 tmax = (ch->ch_digi.digi_maxchar -
1602 ((ch->ch_tin - ch->ch_tout) & TBUF_MASK) -
1603 ((ch->ch_s_tin - ch->ch_s_tpos) & 0xffff));
1607 * If the digi_maxchar constraint now holds, limit
1608 * the transmit count accordingly, and arrange to
1609 * be rescheduled when the queue becomes empty.
1619 * Handle the terminal device.
1624 * If the printer device is on, turn it off.
1627 if ((ch->ch_flag & CH_PRON) != 0) {
1629 space -= ch->ch_digi.digi_offlen;
1631 drp_wmove(ch, 0, ch->ch_digi.digi_offstr,
1632 ch->ch_digi.digi_offlen);
1634 ch->ch_flag &= ~CH_PRON;
1639 * If space is 0 and its because the ch->tbuf
1640 * is full, then Linux will handle a callback when queue
1641 * space becomes available.
1642 * tty_write returns count = 0
1646 /* the linux tty_io.c handles this if we return 0 */
1647 /* if (fp->flags & O_NONBLOCK) return -EAGAIN; */
1649 un->un_flag |= UN_EMPTY;
1650 (ch->ch_nd)->nd_tx_work = 1;
1655 count = min(count, space);
1662 * Copy the buffer contents to the ch_tbuf
1663 * being careful to wrap around the circular queue
1666 t = TBUF_MAX - ch->ch_tin;
1670 memcpy(ch->ch_tbuf + ch->ch_tin, buf, t);
1671 if (nd->nd_dpa_debug && nd->nd_dpa_port ==
1672 PORT_NUM(MINOR(tty_devnum(un->un_tty))))
1673 dgrp_dpa_data(nd, 0, (char *) buf, t);
1680 memcpy(ch->ch_tbuf + ch->ch_tin, buf, n);
1681 if (nd->nd_dpa_debug && nd->nd_dpa_port ==
1682 PORT_NUM(MINOR(tty_devnum(un->un_tty))))
1683 dgrp_dpa_data(nd, 0, (char *) buf, n);
1689 (ch->ch_nd)->nd_tx_work = 1;
1690 if (ch->ch_edelay != DGRP_RTIME) {
1691 (ch->ch_nd)->nd_tx_ready = 1;
1692 wake_up_interruptible(&nd->nd_tx_waitq);
1696 ch->ch_txcount += count;
1698 if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1701 * Adjust ch_cpstime to account
1702 * for the characters just output.
1705 if (sendcount > 0) {
1706 int cc = HZ * sendcount + ch->ch_cpsrem;
1708 ch->ch_cpstime += cc / ch->ch_digi.digi_maxcps;
1709 ch->ch_cpsrem = cc % ch->ch_digi.digi_maxcps;
1713 * If we are now waiting on time, schedule ourself
1714 * back when we'll be able to send a block of
1715 * digi_maxchar characters.
1718 if ((un_flag & UN_TIME) != 0) {
1719 ch->ch_waketime = (ch->ch_cpstime +
1720 (ch->ch_digi.digi_maxchar * HZ /
1721 ch->ch_digi.digi_maxcps));
1726 * If the printer unit is waiting for completion
1727 * of terminal output, get him going again.
1730 if ((ch->ch_pun.un_flag & UN_PWAIT) != 0)
1731 (ch->ch_nd)->nd_tx_work = 1;
1734 spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
1741 * Put a character into ch->ch_buf
1743 * - used by the line discipline for OPOST processing
1746 static int dgrp_tty_put_char(struct tty_struct *tty, unsigned char new_char)
1748 struct un_struct *un;
1749 struct ch_struct *ch;
1757 un = tty->driver_data;
1765 if (ch->ch_state != CS_READY)
1768 spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
1772 * If space is 0 and its because the ch->tbuf
1773 * Warn and dump the character, there isn't anything else
1777 space = (ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK;
1782 * Output the printer on string if device is TXPrint.
1784 if (IS_PRINT(MINOR(tty_devnum(tty))) && (ch->ch_flag & CH_PRON) == 0) {
1785 if (space < ch->ch_digi.digi_onlen) {
1789 space -= ch->ch_digi.digi_onlen;
1790 drp_wmove(ch, 0, ch->ch_digi.digi_onstr,
1791 ch->ch_digi.digi_onlen);
1792 ch->ch_flag |= CH_PRON;
1796 * Output the printer off string if device is NOT TXPrint.
1799 if (!IS_PRINT(MINOR(tty_devnum(tty))) &&
1800 ((ch->ch_flag & CH_PRON) != 0)) {
1801 if (space < ch->ch_digi.digi_offlen) {
1806 space -= ch->ch_digi.digi_offlen;
1807 drp_wmove(ch, 0, ch->ch_digi.digi_offstr,
1808 ch->ch_digi.digi_offlen);
1809 ch->ch_flag &= ~CH_PRON;
1818 * Copy the character to the ch_tbuf being
1819 * careful to wrap around the circular queue
1821 ch->ch_tbuf[ch->ch_tin] = new_char;
1822 ch->ch_tin = (1 + ch->ch_tin) & TBUF_MASK;
1824 if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1827 * Adjust ch_cpstime to account
1828 * for the character just output.
1831 int cc = HZ + ch->ch_cpsrem;
1833 ch->ch_cpstime += cc / ch->ch_digi.digi_maxcps;
1834 ch->ch_cpsrem = cc % ch->ch_digi.digi_maxcps;
1837 * If we are now waiting on time, schedule ourself
1838 * back when we'll be able to send a block of
1839 * digi_maxchar characters.
1842 ch->ch_waketime = (ch->ch_cpstime +
1843 (ch->ch_digi.digi_maxchar * HZ /
1844 ch->ch_digi.digi_maxcps));
1849 (ch->ch_nd)->nd_tx_work = 1;
1853 spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
1860 * Flush TX buffer (make in == out)
1862 * check tty_ioctl.c -- this is called after TCOFLUSH
1864 static void dgrp_tty_flush_buffer(struct tty_struct *tty)
1866 struct un_struct *un;
1867 struct ch_struct *ch;
1871 un = tty->driver_data;
1879 ch->ch_tout = ch->ch_tin;
1880 /* do NOT do this here! */
1881 /* ch->ch_s_tpos = ch->ch_s_tin = 0; */
1883 /* send the flush output command now */
1884 ch->ch_send |= RR_TX_FLUSH;
1885 (ch->ch_nd)->nd_tx_ready = 1;
1886 (ch->ch_nd)->nd_tx_work = 1;
1887 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
1889 if (waitqueue_active(&tty->write_wait))
1890 wake_up_interruptible(&tty->write_wait);
1897 * Return space available in Tx buffer
1898 * count = ( ch->ch_tout - ch->ch_tin ) mod (TBUF_MAX - 1)
1900 static int dgrp_tty_write_room(struct tty_struct *tty)
1902 struct un_struct *un;
1903 struct ch_struct *ch;
1909 un = tty->driver_data;
1917 count = (ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK;
1919 /* We *MUST* check this, and return 0 if the Printer Unit cannot
1920 * take any more data within its time constraints... If we don't
1921 * return 0 and the printer has hit it time constraint, the ld will
1922 * call us back doing a put_char, which cannot be rejected!!!
1924 if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1926 count = dgrp_calculate_txprint_bounds(ch, count, &un_flag);
1930 ch->ch_pun.un_flag |= un_flag;
1931 (ch->ch_nd)->nd_tx_work = 1;
1938 * Return number of characters that have not been transmitted yet.
1939 * chars_in_buffer = ( ch->ch_tin - ch->ch_tout ) mod (TBUF_MAX - 1)
1940 * + ( ch->ch_s_tin - ch->ch_s_tout ) mod (0xffff)
1941 * = number of characters "in transit"
1943 * Remember that sequence number math is always with a sixteen bit
1944 * mask, not the TBUF_MASK.
1947 static int dgrp_tty_chars_in_buffer(struct tty_struct *tty)
1949 struct un_struct *un;
1950 struct ch_struct *ch;
1957 un = tty->driver_data;
1965 count1 = count = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1966 count += (ch->ch_s_tin - ch->ch_s_tpos) & 0xffff;
1967 /* one for tbuf, one for the PS */
1970 * If we are busy transmitting add 1
1972 count += un->un_tbusy;
1978 /*****************************************************************************
1980 * Helper applications for dgrp_tty_ioctl()
1982 *****************************************************************************
1987 * ch_to_tty_flags() -- convert channel flags to termio flags
1988 * @ch_flag: Digi channel flags
1989 * @flagtype: type of ch_flag (iflag, oflag or cflag)
1991 * take the channel flags of the specified type and return the
1992 * corresponding termio flag
1994 static tcflag_t ch_to_tty_flags(ushort ch_flag, char flagtype)
1996 tcflag_t retval = 0;
2000 retval = ((ch_flag & IF_IGNBRK) ? IGNBRK : 0)
2001 | ((ch_flag & IF_BRKINT) ? BRKINT : 0)
2002 | ((ch_flag & IF_IGNPAR) ? IGNPAR : 0)
2003 | ((ch_flag & IF_PARMRK) ? PARMRK : 0)
2004 | ((ch_flag & IF_INPCK) ? INPCK : 0)
2005 | ((ch_flag & IF_ISTRIP) ? ISTRIP : 0)
2006 | ((ch_flag & IF_IXON) ? IXON : 0)
2007 | ((ch_flag & IF_IXANY) ? IXANY : 0)
2008 | ((ch_flag & IF_IXOFF) ? IXOFF : 0);
2012 retval = ((ch_flag & OF_OLCUC) ? OLCUC : 0)
2013 | ((ch_flag & OF_ONLCR) ? ONLCR : 0)
2014 | ((ch_flag & OF_OCRNL) ? OCRNL : 0)
2015 | ((ch_flag & OF_ONOCR) ? ONOCR : 0)
2016 | ((ch_flag & OF_ONLRET) ? ONLRET : 0)
2017 /* | ((ch_flag & OF_OTAB3) ? OFILL : 0) */
2018 | ((ch_flag & OF_TABDLY) ? TABDLY : 0);
2022 retval = ((ch_flag & CF_CSTOPB) ? CSTOPB : 0)
2023 | ((ch_flag & CF_CREAD) ? CREAD : 0)
2024 | ((ch_flag & CF_PARENB) ? PARENB : 0)
2025 | ((ch_flag & CF_PARODD) ? PARODD : 0)
2026 | ((ch_flag & CF_HUPCL) ? HUPCL : 0);
2028 switch (ch_flag & CF_CSIZE) {
2059 * tty_to_ch_flags() -- convert termio flags to digi channel flags
2060 * @tty: pointer to a TTY structure holding flag to be converted
2061 * @flagtype: identifies which flag (iflags, oflags, or cflags) should
2064 * take the termio flag of the specified type and return the
2065 * corresponding Digi version of the flags
2067 static ushort tty_to_ch_flags(struct tty_struct *tty, char flagtype)
2074 tflag = tty->termios.c_iflag;
2075 retval = (I_IGNBRK(tty) ? IF_IGNBRK : 0)
2076 | (I_BRKINT(tty) ? IF_BRKINT : 0)
2077 | (I_IGNPAR(tty) ? IF_IGNPAR : 0)
2078 | (I_PARMRK(tty) ? IF_PARMRK : 0)
2079 | (I_INPCK(tty) ? IF_INPCK : 0)
2080 | (I_ISTRIP(tty) ? IF_ISTRIP : 0)
2081 | (I_IXON(tty) ? IF_IXON : 0)
2082 | (I_IXANY(tty) ? IF_IXANY : 0)
2083 | (I_IXOFF(tty) ? IF_IXOFF : 0);
2086 tflag = tty->termios.c_oflag;
2088 * If OPOST is set, then do the post processing in the
2089 * firmware by setting all the processing flags on.
2090 * If ~OPOST, then make sure we are not doing any
2091 * output processing!!
2096 retval = (O_OLCUC(tty) ? OF_OLCUC : 0)
2097 | (O_ONLCR(tty) ? OF_ONLCR : 0)
2098 | (O_OCRNL(tty) ? OF_OCRNL : 0)
2099 | (O_ONOCR(tty) ? OF_ONOCR : 0)
2100 | (O_ONLRET(tty) ? OF_ONLRET : 0)
2101 /* | (O_OFILL(tty) ? OF_TAB3 : 0) */
2102 | (O_TABDLY(tty) ? OF_TABDLY : 0);
2105 tflag = tty->termios.c_cflag;
2106 retval = (C_CSTOPB(tty) ? CF_CSTOPB : 0)
2107 | (C_CREAD(tty) ? CF_CREAD : 0)
2108 | (C_PARENB(tty) ? CF_PARENB : 0)
2109 | (C_PARODD(tty) ? CF_PARODD : 0)
2110 | (C_HUPCL(tty) ? CF_HUPCL : 0);
2111 switch (C_CSIZE(tty)) {
2141 static int dgrp_tty_send_break(struct tty_struct *tty, int msec)
2143 struct un_struct *un;
2144 struct ch_struct *ch;
2150 un = tty->driver_data;
2158 dgrp_send_break(ch, msec);
2164 * This routine sends a break character out the serial port.
2166 * duration is in 1/1000's of a second
2168 static int dgrp_send_break(struct ch_struct *ch, int msec)
2172 wait_event_interruptible(ch->ch_flag_wait,
2173 ((ch->ch_flag & CH_TX_BREAK) == 0));
2174 ch->ch_break_time += max(msec, 250);
2175 ch->ch_send |= RR_TX_BREAK;
2176 ch->ch_flag |= CH_TX_BREAK;
2177 (ch->ch_nd)->nd_tx_work = 1;
2179 x = (msec * HZ) / 1000;
2180 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2187 * Return modem signals to ld.
2189 static int dgrp_tty_tiocmget(struct tty_struct *tty)
2192 struct un_struct *un = tty->driver_data;
2193 struct ch_struct *ch;
2202 mlast = ((ch->ch_s_mlast & ~(DM_RTS | DM_DTR)) |
2203 (ch->ch_mout & (DM_RTS | DM_DTR)));
2205 /* defined in /usr/include/asm/termios.h */
2206 mlast = ((mlast & DM_RTS) ? TIOCM_RTS : 0)
2207 | ((mlast & DM_DTR) ? TIOCM_DTR : 0)
2208 | ((mlast & DM_CD) ? TIOCM_CAR : 0)
2209 | ((mlast & DM_RI) ? TIOCM_RNG : 0)
2210 | ((mlast & DM_DSR) ? TIOCM_DSR : 0)
2211 | ((mlast & DM_CTS) ? TIOCM_CTS : 0);
2220 static int dgrp_tty_tiocmset(struct tty_struct *tty,
2221 unsigned int set, unsigned int clear)
2224 struct un_struct *un = tty->driver_data;
2225 struct ch_struct *ch;
2234 if (set & TIOCM_RTS)
2235 ch->ch_mout |= DM_RTS;
2237 if (set & TIOCM_DTR)
2238 ch->ch_mout |= DM_DTR;
2240 if (clear & TIOCM_RTS)
2241 ch->ch_mout &= ~(DM_RTS);
2243 if (clear & TIOCM_DTR)
2244 ch->ch_mout &= ~(DM_DTR);
2246 spin_lock_irqsave(&(ch->ch_nd)->nd_lock, lock_flags);
2247 ch->ch_flag |= CH_PARAM;
2248 (ch->ch_nd)->nd_tx_work = 1;
2249 wake_up_interruptible(&ch->ch_flag_wait);
2251 spin_unlock_irqrestore(&(ch->ch_nd)->nd_lock, lock_flags);
2259 * Get current modem status
2261 static int get_modem_info(struct ch_struct *ch, unsigned int *value)
2265 mlast = ((ch->ch_s_mlast & ~(DM_RTS | DM_DTR)) |
2266 (ch->ch_mout & (DM_RTS | DM_DTR)));
2268 /* defined in /usr/include/asm/termios.h */
2269 mlast = ((mlast & DM_RTS) ? TIOCM_RTS : 0)
2270 | ((mlast & DM_DTR) ? TIOCM_DTR : 0)
2271 | ((mlast & DM_CD) ? TIOCM_CAR : 0)
2272 | ((mlast & DM_RI) ? TIOCM_RNG : 0)
2273 | ((mlast & DM_DSR) ? TIOCM_DSR : 0)
2274 | ((mlast & DM_CTS) ? TIOCM_CTS : 0);
2275 return put_user(mlast, (unsigned int __user *) value);
2281 static int set_modem_info(struct ch_struct *ch, unsigned int command,
2282 unsigned int *value)
2289 error = access_ok(VERIFY_READ, (void __user *) value, sizeof(int));
2293 if (get_user(arg, (unsigned int __user *) value))
2295 mval |= ((arg & TIOCM_RTS) ? DM_RTS : 0)
2296 | ((arg & TIOCM_DTR) ? DM_DTR : 0);
2299 case TIOCMBIS: /* set flags */
2300 ch->ch_mout |= mval;
2302 case TIOCMBIC: /* clear flags */
2303 ch->ch_mout &= ~mval;
2312 spin_lock_irqsave(&(ch->ch_nd)->nd_lock, lock_flags);
2314 ch->ch_flag |= CH_PARAM;
2315 (ch->ch_nd)->nd_tx_work = 1;
2316 wake_up_interruptible(&ch->ch_flag_wait);
2318 spin_unlock_irqrestore(&(ch->ch_nd)->nd_lock, lock_flags);
2325 * Assign the custom baud rate to the channel structure
2327 static void dgrp_set_custom_speed(struct ch_struct *ch, int newrate)
2333 int deltahigh, deltalow;
2339 * Since the divisor is stored in a 16-bit integer, we make sure
2340 * we don't allow any rates smaller than a 16-bit integer would allow.
2341 * And of course, rates above the dividend won't fly.
2343 if (newrate && newrate < ((PORTSERVER_DIVIDEND / 0xFFFF) + 1))
2344 newrate = ((PORTSERVER_DIVIDEND / 0xFFFF) + 1);
2345 if (newrate && newrate > PORTSERVER_DIVIDEND)
2346 newrate = PORTSERVER_DIVIDEND;
2348 while (newrate > 0) {
2349 testdiv = PORTSERVER_DIVIDEND / newrate;
2352 * If we try to figure out what rate the PortServer would use
2353 * with the test divisor, it will be either equal or higher
2354 * than the requested baud rate. If we then determine the
2355 * rate with a divisor one higher, we will get the next lower
2356 * supported rate below the requested.
2358 testrate_high = PORTSERVER_DIVIDEND / testdiv;
2359 testrate_low = PORTSERVER_DIVIDEND / (testdiv + 1);
2362 * If the rate for the requested divisor is correct, just
2363 * use it and be done.
2365 if (testrate_high == newrate)
2369 * Otherwise, pick the rate that is closer (i.e. whichever rate
2370 * has a smaller delta).
2372 deltahigh = testrate_high - newrate;
2373 deltalow = newrate - testrate_low;
2375 if (deltahigh < deltalow)
2376 newrate = testrate_high;
2378 newrate = testrate_low;
2383 ch->ch_custom_speed = newrate;
2392 # dgrp_tty_digiseta()
2394 * Ioctl to set the information from ditty.
2396 * NOTE: DIGI_IXON, DSRPACE, DCDPACE, and DTRPACE are unsupported. JAR 990922
2398 static int dgrp_tty_digiseta(struct tty_struct *tty,
2399 struct digi_struct *new_info)
2401 struct un_struct *un = tty->driver_data;
2402 struct ch_struct *ch;
2411 if (copy_from_user(&ch->ch_digi, (void __user *) new_info,
2412 sizeof(struct digi_struct)))
2415 if ((ch->ch_digi.digi_flags & RTSPACE) ||
2416 (ch->ch_digi.digi_flags & CTSPACE))
2417 tty->termios.c_cflag |= CRTSCTS;
2419 tty->termios.c_cflag &= ~CRTSCTS;
2421 if (ch->ch_digi.digi_maxcps < 1)
2422 ch->ch_digi.digi_maxcps = 1;
2424 if (ch->ch_digi.digi_maxcps > 10000)
2425 ch->ch_digi.digi_maxcps = 10000;
2427 if (ch->ch_digi.digi_bufsize < 10)
2428 ch->ch_digi.digi_bufsize = 10;
2430 if (ch->ch_digi.digi_maxchar < 1)
2431 ch->ch_digi.digi_maxchar = 1;
2433 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2434 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2436 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2437 ch->ch_digi.digi_onlen = DIGI_PLEN;
2439 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2440 ch->ch_digi.digi_offlen = DIGI_PLEN;
2442 /* make the changes now */
2451 * dgrp_tty_digigetedelay()
2453 * Ioctl to get the current edelay setting.
2458 static int dgrp_tty_digigetedelay(struct tty_struct *tty, int *retinfo)
2460 struct un_struct *un;
2461 struct ch_struct *ch;
2467 if (!tty || tty->magic != TTY_MAGIC)
2470 un = tty->driver_data;
2479 tmp = ch->ch_edelay;
2481 if (copy_to_user((void __user *) retinfo, &tmp, sizeof(*retinfo)))
2489 * dgrp_tty_digisetedelay()
2491 * Ioctl to set the EDELAY setting
2494 static int dgrp_tty_digisetedelay(struct tty_struct *tty, int *new_info)
2496 struct un_struct *un;
2497 struct ch_struct *ch;
2500 if (!tty || tty->magic != TTY_MAGIC)
2503 un = tty->driver_data;
2512 if (copy_from_user(&new_digi, (void __user *)new_info, sizeof(int)))
2515 ch->ch_edelay = new_digi;
2517 /* make the changes now */
2525 * The usual assortment of ioctl's
2527 * note: use tty_check_change to make sure that we are not
2528 * changing the state of a terminal when we are not a process
2529 * in the forground. See tty_io.c
2530 * rc = tty_check_change(tty);
2531 * if (rc) return rc;
2533 static int dgrp_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2536 struct un_struct *un;
2537 struct ch_struct *ch;
2539 struct digiflow_struct dflow;
2544 un = tty->driver_data;
2555 * Here are all the standard ioctl's that we MUST implement
2560 * TCSBRK is SVID version: non-zero arg --> no break
2561 * this behaviour is exploited by tcdrain().
2563 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2564 * between 0.25 and 0.5 seconds
2567 rc = tty_check_change(tty);
2570 tty_wait_until_sent(tty, 0);
2573 rc = dgrp_send_break(ch, 250); /* 1/4 second */
2575 if (dgrp_tty_chars_in_buffer(tty) != 0)
2581 /* support for POSIX tcsendbreak()
2583 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2584 * between 0.25 and 0.5 seconds so we'll ask for something
2585 * in the middle: 0.375 seconds.
2587 rc = tty_check_change(tty);
2590 tty_wait_until_sent(tty, 0);
2592 rc = dgrp_send_break(ch, arg ? arg*250 : 250);
2594 if (dgrp_tty_chars_in_buffer(tty) != 0)
2599 rc = tty_check_change(tty);
2602 tty_wait_until_sent(tty, 0);
2605 * RealPort doesn't support turning on a break unconditionally.
2606 * The RealPort device will stop sending a break automatically
2607 * after the specified time value that we send in.
2609 rc = dgrp_send_break(ch, 250); /* 1/4 second */
2611 if (dgrp_tty_chars_in_buffer(tty) != 0)
2617 * RealPort doesn't support turning off a break unconditionally.
2618 * The RealPort device will stop sending a break automatically
2619 * after the specified time value that was sent when turning on
2625 rc = access_ok(VERIFY_WRITE, (void __user *) arg,
2626 sizeof(unsigned int));
2629 return get_modem_info(ch, (unsigned int *) arg);
2634 return set_modem_info(ch, cmd, (unsigned int *) arg);
2637 * Here are any additional ioctl's that we want to implement
2642 * The linux tty driver doesn't have a flush
2643 * input routine for the driver, assuming all backed
2644 * up data is in the line disc. buffers. However,
2645 * we all know that's not the case. Here, we
2646 * act on the ioctl, but then lie and say we didn't
2647 * so the line discipline will process the flush
2650 rc = tty_check_change(tty);
2657 /* only flush input if this is the only open unit */
2658 if (!IS_PRINT(MINOR(tty_devnum(tty)))) {
2659 ch->ch_rout = ch->ch_rin;
2660 ch->ch_send |= RR_RX_FLUSH;
2661 (ch->ch_nd)->nd_tx_work = 1;
2662 (ch->ch_nd)->nd_tx_ready = 1;
2663 wake_up_interruptible(
2664 &(ch->ch_nd)->nd_tx_waitq);
2666 if (arg == TCIFLUSH)
2669 case TCOFLUSH: /* flush output, or the receive buffer */
2671 * This is handled in the tty_ioctl.c code
2672 * calling tty_flush_buffer
2677 /* POSIX.1 says return EINVAL if we got a bad arg */
2680 /* pretend we didn't recognize this IOCTL */
2681 return -ENOIOCTLCMD;
2686 /*****************************************
2688 TCSETA TCSETA - set the termios
2689 TCSETAF TCSETAF - wait for drain first, then set termios
2690 TCSETAW TCSETAW - wait for drain,
2691 flush the input queue, then set termios
2692 - looking at the tty_ioctl code, these command all call our
2693 tty_set_termios at the driver's end, when a TCSETA* is sent,
2694 it is expecting the tty to have a termio structure,
2695 NOT a termios structure. These two structures differ in size
2696 and the tty_ioctl code does a conversion before processing them both.
2697 - we should treat the TCSETAW TCSETAF ioctls the same, and let
2698 the tty_ioctl code do the conversion stuff.
2703 - the associated tty structure has a termios structure.
2704 *****************************************/
2708 return -ENOIOCTLCMD;
2715 * The linux tty driver doesn't have a flush
2716 * input routine for the driver, assuming all backed
2717 * up data is in the line disc. buffers. However,
2718 * we all know that's not the case. Here, we
2719 * act on the ioctl, but then lie and say we didn't
2720 * so the line discipline will process the flush
2725 * Also, now that we have TXPrint, we have to check
2726 * if this is the TXPrint device and the terminal
2727 * device is open. If so, do NOT run check_change,
2728 * as the terminal device is ALWAYS the parent.
2730 if (!IS_PRINT(MINOR(tty_devnum(tty))) ||
2731 !ch->ch_tun.un_open_count) {
2732 rc = tty_check_change(tty);
2737 /* wait for all the characters in tbuf to drain */
2738 tty_wait_until_sent(tty, 0);
2740 if ((cmd == TCSETSF) || (cmd == TCSETAF)) {
2741 /* flush the contents of the rbuf queue */
2742 /* TODO: check if this is print device? */
2743 ch->ch_send |= RR_RX_FLUSH;
2744 (ch->ch_nd)->nd_tx_ready = 1;
2745 (ch->ch_nd)->nd_tx_work = 1;
2746 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2747 /* do we need to do this? just to be safe! */
2748 ch->ch_rout = ch->ch_rin;
2751 /* pretend we didn't recognize this */
2752 return -ENOIOCTLCMD;
2756 * The Linux Line Discipline (LD) would do this for us if we
2757 * let it, but we have the special firmware options to do this
2758 * the "right way" regardless of hardware or software flow
2759 * control so we'll do it outselves instead of letting the LD
2762 rc = tty_check_change(tty);
2768 dgrp_tty_start(tty);
2774 dgrp_tty_input_start(tty);
2777 dgrp_tty_input_stop(tty);
2784 /* get information for ditty */
2785 if (copy_to_user((struct digi_struct __user *) arg,
2786 &ch->ch_digi, sizeof(struct digi_struct)))
2792 /* wait for all the characters in tbuf to drain */
2793 tty_wait_until_sent(tty, 0);
2795 if (cmd == DIGI_SETAF) {
2796 /* flush the contents of the rbuf queue */
2797 /* send down a packet with RR_RX_FLUSH set */
2798 ch->ch_send |= RR_RX_FLUSH;
2799 (ch->ch_nd)->nd_tx_ready = 1;
2800 (ch->ch_nd)->nd_tx_work = 1;
2801 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2802 /* do we need to do this? just to be safe! */
2803 ch->ch_rout = ch->ch_rin;
2806 /* pretend we didn't recognize this */
2810 return dgrp_tty_digiseta(tty, (struct digi_struct *) arg);
2813 return dgrp_tty_digisetedelay(tty, (int *) arg);
2816 return dgrp_tty_digigetedelay(tty, (int *) arg);
2820 if (cmd == (DIGI_GETFLOW)) {
2821 dflow.startc = tty->termios.c_cc[VSTART];
2822 dflow.stopc = tty->termios.c_cc[VSTOP];
2824 dflow.startc = ch->ch_xxon;
2825 dflow.stopc = ch->ch_xxoff;
2828 if (copy_to_user((char __user *)arg, &dflow, sizeof(dflow)))
2835 if (copy_from_user(&dflow, (char __user *)arg, sizeof(dflow)))
2838 if (cmd == (DIGI_SETFLOW)) {
2839 tty->termios.c_cc[VSTART] = dflow.startc;
2840 tty->termios.c_cc[VSTOP] = dflow.stopc;
2842 ch->ch_xxon = dflow.startc;
2843 ch->ch_xxoff = dflow.stopc;
2847 case DIGI_GETCUSTOMBAUD:
2848 if (put_user(ch->ch_custom_speed, (unsigned int __user *) arg))
2852 case DIGI_SETCUSTOMBAUD:
2856 if (get_user(new_rate, (unsigned int __user *) arg))
2858 dgrp_set_custom_speed(ch, new_rate);
2864 return -ENOIOCTLCMD;
2871 * This routine allows the tty driver to be notified when
2872 * the device's termios setting have changed. Note that we
2873 * should be prepared to accept the case where old == NULL
2874 * and try to do something rational.
2876 * So we need to make sure that our copies of ch_oflag,
2877 * ch_clag, and ch_iflag reflect the tty->termios flags.
2879 static void dgrp_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
2881 struct ktermios *ts;
2882 struct ch_struct *ch;
2883 struct un_struct *un;
2885 /* seems silly, but we have to check all these! */
2889 un = tty->driver_data;
2901 /* the CLOCAL flag has just been set */
2902 if (!(old->c_cflag & CLOCAL) && C_CLOCAL(tty))
2903 wake_up_interruptible(&un->un_open_wait);
2908 * Throttle receiving data. We just set a bit and stop reading
2909 * data out of the channel buffer. It will back up and the
2910 * FEP will do whatever is necessary to stop the far end.
2912 static void dgrp_tty_throttle(struct tty_struct *tty)
2914 struct ch_struct *ch;
2919 ch = ((struct un_struct *) tty->driver_data)->un_ch;
2923 ch->ch_flag |= CH_RXSTOP;
2927 static void dgrp_tty_unthrottle(struct tty_struct *tty)
2929 struct ch_struct *ch;
2934 ch = ((struct un_struct *) tty->driver_data)->un_ch;
2938 ch->ch_flag &= ~CH_RXSTOP;
2942 * Stop the transmitter
2944 static void dgrp_tty_stop(struct tty_struct *tty)
2946 struct ch_struct *ch;
2951 ch = ((struct un_struct *) tty->driver_data)->un_ch;
2955 ch->ch_send |= RR_TX_STOP;
2956 ch->ch_send &= ~RR_TX_START;
2958 /* make the change NOW! */
2959 (ch->ch_nd)->nd_tx_ready = 1;
2960 if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
2961 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2965 * Start the transmitter
2967 static void dgrp_tty_start(struct tty_struct *tty)
2969 struct ch_struct *ch;
2974 ch = ((struct un_struct *) tty->driver_data)->un_ch;
2978 /* TODO: don't do anything if the transmitter is not stopped */
2980 ch->ch_send |= RR_TX_START;
2981 ch->ch_send &= ~RR_TX_STOP;
2983 /* make the change NOW! */
2984 (ch->ch_nd)->nd_tx_ready = 1;
2985 (ch->ch_nd)->nd_tx_work = 1;
2986 if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
2987 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2994 static void dgrp_tty_input_stop(struct tty_struct *tty)
2996 struct ch_struct *ch;
3001 ch = ((struct un_struct *) tty->driver_data)->un_ch;
3005 ch->ch_send |= RR_RX_STOP;
3006 ch->ch_send &= ~RR_RX_START;
3007 (ch->ch_nd)->nd_tx_ready = 1;
3008 if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
3009 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
3014 static void dgrp_tty_send_xchar(struct tty_struct *tty, char c)
3016 struct un_struct *un;
3017 struct ch_struct *ch;
3022 un = tty->driver_data;
3029 if (c == STOP_CHAR(tty))
3030 ch->ch_send |= RR_RX_STOP;
3031 else if (c == START_CHAR(tty))
3032 ch->ch_send |= RR_RX_START;
3034 ch->ch_nd->nd_tx_ready = 1;
3035 ch->ch_nd->nd_tx_work = 1;
3041 static void dgrp_tty_input_start(struct tty_struct *tty)
3043 struct ch_struct *ch;
3048 ch = ((struct un_struct *) tty->driver_data)->un_ch;
3052 ch->ch_send |= RR_RX_START;
3053 ch->ch_send &= ~RR_RX_STOP;
3054 (ch->ch_nd)->nd_tx_ready = 1;
3055 (ch->ch_nd)->nd_tx_work = 1;
3056 if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
3057 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
3063 * Hangup the port. Like a close, but don't wait for output
3066 * How do we close all the channels that are open?
3068 static void dgrp_tty_hangup(struct tty_struct *tty)
3070 struct ch_struct *ch;
3071 struct nd_struct *nd;
3072 struct un_struct *un;
3077 un = tty->driver_data;
3089 ch->ch_mout &= ~DM_DTR;
3090 /* Don't do this here */
3091 /* ch->ch_flag |= CH_HANGUP; */
3092 ch->ch_nd->nd_tx_ready = 1;
3093 ch->ch_nd->nd_tx_work = 1;
3094 if (waitqueue_active(&ch->ch_flag_wait))
3095 wake_up_interruptible(&ch->ch_flag_wait);
3100 /************************************************************************/
3102 /* TTY Initialization/Cleanup Functions */
3104 /************************************************************************/
3107 * Uninitialize the TTY portion of the supplied node. Free all
3108 * memory and resources associated with this node. Do it in reverse
3109 * allocation order: this might possibly result in less fragmentation
3110 * of memory, though I don't know this for sure.
3113 dgrp_tty_uninit(struct nd_struct *nd)
3118 ID_TO_CHAR(nd->nd_ID, id);
3120 if (nd->nd_ttdriver_flags & SERIAL_TTDRV_REG) {
3121 tty_unregister_driver(nd->nd_serial_ttdriver);
3123 kfree(nd->nd_serial_ttdriver->ttys);
3124 nd->nd_serial_ttdriver->ttys = NULL;
3126 put_tty_driver(nd->nd_serial_ttdriver);
3127 nd->nd_ttdriver_flags &= ~SERIAL_TTDRV_REG;
3130 if (nd->nd_ttdriver_flags & CALLOUT_TTDRV_REG) {
3131 tty_unregister_driver(nd->nd_callout_ttdriver);
3133 kfree(nd->nd_callout_ttdriver->ttys);
3134 nd->nd_callout_ttdriver->ttys = NULL;
3136 put_tty_driver(nd->nd_callout_ttdriver);
3137 nd->nd_ttdriver_flags &= ~CALLOUT_TTDRV_REG;
3140 if (nd->nd_ttdriver_flags & XPRINT_TTDRV_REG) {
3141 tty_unregister_driver(nd->nd_xprint_ttdriver);
3143 kfree(nd->nd_xprint_ttdriver->ttys);
3144 nd->nd_xprint_ttdriver->ttys = NULL;
3146 put_tty_driver(nd->nd_xprint_ttdriver);
3147 nd->nd_ttdriver_flags &= ~XPRINT_TTDRV_REG;
3149 for (i = 0; i < CHAN_MAX; i++)
3150 tty_port_destroy(&nd->nd_chan[i].port);
3156 * Initialize the TTY portion of the supplied node.
3159 dgrp_tty_init(struct nd_struct *nd)
3165 ID_TO_CHAR(nd->nd_ID, id);
3168 * Initialize the TTDRIVER structures.
3171 nd->nd_serial_ttdriver = alloc_tty_driver(CHAN_MAX);
3172 if (!nd->nd_serial_ttdriver)
3175 sprintf(nd->nd_serial_name, "tty_dgrp_%s_", id);
3177 nd->nd_serial_ttdriver->owner = THIS_MODULE;
3178 nd->nd_serial_ttdriver->name = nd->nd_serial_name;
3179 nd->nd_serial_ttdriver->name_base = 0;
3180 nd->nd_serial_ttdriver->major = 0;
3181 nd->nd_serial_ttdriver->minor_start = 0;
3182 nd->nd_serial_ttdriver->type = TTY_DRIVER_TYPE_SERIAL;
3183 nd->nd_serial_ttdriver->subtype = SERIAL_TYPE_NORMAL;
3184 nd->nd_serial_ttdriver->init_termios = DefaultTermios;
3185 nd->nd_serial_ttdriver->driver_name = "dgrp";
3186 nd->nd_serial_ttdriver->flags = (TTY_DRIVER_REAL_RAW |
3187 TTY_DRIVER_DYNAMIC_DEV |
3188 TTY_DRIVER_HARDWARE_BREAK);
3190 /* The kernel wants space to store pointers to tty_structs. */
3191 nd->nd_serial_ttdriver->ttys =
3192 kzalloc(CHAN_MAX * sizeof(struct tty_struct *), GFP_KERNEL);
3193 if (!nd->nd_serial_ttdriver->ttys)
3196 tty_set_operations(nd->nd_serial_ttdriver, &dgrp_tty_ops);
3198 if (!(nd->nd_ttdriver_flags & SERIAL_TTDRV_REG)) {
3200 * Register tty devices
3202 rc = tty_register_driver(nd->nd_serial_ttdriver);
3205 * If errno is EBUSY, this means there are no more
3206 * slots available to have us auto-majored.
3207 * (Which is currently supported up to 256)
3209 * We can still request majors above 256,
3210 * we just have to do it manually.
3214 int max_majors = 1U << (32 - MINORBITS);
3215 for (i = 256; i < max_majors; i++) {
3216 nd->nd_serial_ttdriver->major = i;
3217 rc = tty_register_driver
3218 (nd->nd_serial_ttdriver);
3222 /* Really fail now, since we ran out
3223 * of majors to try. */
3224 if (i == max_majors)
3231 nd->nd_ttdriver_flags |= SERIAL_TTDRV_REG;
3234 nd->nd_callout_ttdriver = alloc_tty_driver(CHAN_MAX);
3235 if (!nd->nd_callout_ttdriver)
3238 sprintf(nd->nd_callout_name, "cu_dgrp_%s_", id);
3240 nd->nd_callout_ttdriver->owner = THIS_MODULE;
3241 nd->nd_callout_ttdriver->name = nd->nd_callout_name;
3242 nd->nd_callout_ttdriver->name_base = 0;
3243 nd->nd_callout_ttdriver->major = nd->nd_serial_ttdriver->major;
3244 nd->nd_callout_ttdriver->minor_start = 0x40;
3245 nd->nd_callout_ttdriver->type = TTY_DRIVER_TYPE_SERIAL;
3246 nd->nd_callout_ttdriver->subtype = SERIAL_TYPE_CALLOUT;
3247 nd->nd_callout_ttdriver->init_termios = DefaultTermios;
3248 nd->nd_callout_ttdriver->driver_name = "dgrp";
3249 nd->nd_callout_ttdriver->flags = (TTY_DRIVER_REAL_RAW |
3250 TTY_DRIVER_DYNAMIC_DEV |
3251 TTY_DRIVER_HARDWARE_BREAK);
3253 /* The kernel wants space to store pointers to tty_structs. */
3254 nd->nd_callout_ttdriver->ttys =
3255 kzalloc(CHAN_MAX * sizeof(struct tty_struct *), GFP_KERNEL);
3256 if (!nd->nd_callout_ttdriver->ttys)
3259 tty_set_operations(nd->nd_callout_ttdriver, &dgrp_tty_ops);
3261 if (dgrp_register_cudevices) {
3262 if (!(nd->nd_ttdriver_flags & CALLOUT_TTDRV_REG)) {
3264 * Register cu devices
3266 rc = tty_register_driver(nd->nd_callout_ttdriver);
3269 nd->nd_ttdriver_flags |= CALLOUT_TTDRV_REG;
3274 nd->nd_xprint_ttdriver = alloc_tty_driver(CHAN_MAX);
3275 if (!nd->nd_xprint_ttdriver)
3278 sprintf(nd->nd_xprint_name, "pr_dgrp_%s_", id);
3280 nd->nd_xprint_ttdriver->owner = THIS_MODULE;
3281 nd->nd_xprint_ttdriver->name = nd->nd_xprint_name;
3282 nd->nd_xprint_ttdriver->name_base = 0;
3283 nd->nd_xprint_ttdriver->major = nd->nd_serial_ttdriver->major;
3284 nd->nd_xprint_ttdriver->minor_start = 0x80;
3285 nd->nd_xprint_ttdriver->type = TTY_DRIVER_TYPE_SERIAL;
3286 nd->nd_xprint_ttdriver->subtype = SERIAL_TYPE_XPRINT;
3287 nd->nd_xprint_ttdriver->init_termios = DefaultTermios;
3288 nd->nd_xprint_ttdriver->driver_name = "dgrp";
3289 nd->nd_xprint_ttdriver->flags = (TTY_DRIVER_REAL_RAW |
3290 TTY_DRIVER_DYNAMIC_DEV |
3291 TTY_DRIVER_HARDWARE_BREAK);
3293 /* The kernel wants space to store pointers to tty_structs. */
3294 nd->nd_xprint_ttdriver->ttys =
3295 kzalloc(CHAN_MAX * sizeof(struct tty_struct *), GFP_KERNEL);
3296 if (!nd->nd_xprint_ttdriver->ttys)
3299 tty_set_operations(nd->nd_xprint_ttdriver, &dgrp_tty_ops);
3301 if (dgrp_register_prdevices) {
3302 if (!(nd->nd_ttdriver_flags & XPRINT_TTDRV_REG)) {
3304 * Register transparent print devices
3306 rc = tty_register_driver(nd->nd_xprint_ttdriver);
3309 nd->nd_ttdriver_flags |= XPRINT_TTDRV_REG;
3313 for (i = 0; i < CHAN_MAX; i++) {
3314 struct ch_struct *ch = nd->nd_chan + i;
3317 ch->ch_digi = digi_init;
3318 ch->ch_edelay = 100;
3319 ch->ch_custom_speed = 0;
3321 ch->ch_tun.un_ch = ch;
3322 ch->ch_pun.un_ch = ch;
3323 ch->ch_tun.un_type = SERIAL_TYPE_NORMAL;
3324 ch->ch_pun.un_type = SERIAL_TYPE_XPRINT;
3326 init_waitqueue_head(&(ch->ch_flag_wait));
3327 init_waitqueue_head(&(ch->ch_sleep));
3329 init_waitqueue_head(&(ch->ch_tun.un_open_wait));
3330 init_waitqueue_head(&(ch->ch_tun.un_close_wait));
3332 init_waitqueue_head(&(ch->ch_pun.un_open_wait));
3333 init_waitqueue_head(&(ch->ch_pun.un_close_wait));
3334 tty_port_init(&ch->port);