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/sched.h>
44 #include "dgrp_common.h"
46 #ifndef _POSIX_VDISABLE
47 #define _POSIX_VDISABLE ('\0')
51 * forward declarations
54 static void drp_param(struct ch_struct *);
55 static void dgrp_tty_close(struct tty_struct *, struct file *);
57 /* ioctl helper functions */
58 static int set_modem_info(struct ch_struct *, unsigned int, unsigned int *);
59 static int get_modem_info(struct ch_struct *, unsigned int *);
60 static void dgrp_set_custom_speed(struct ch_struct *, int);
61 static int dgrp_tty_digigetedelay(struct tty_struct *, int *);
62 static int dgrp_tty_digisetedelay(struct tty_struct *, int *);
63 static int dgrp_send_break(struct ch_struct *, int);
65 static ushort tty_to_ch_flags(struct tty_struct *, char);
66 static tcflag_t ch_to_tty_flags(unsigned short, char);
68 static void dgrp_tty_input_start(struct tty_struct *);
69 static void dgrp_tty_input_stop(struct tty_struct *);
71 static void drp_wmove(struct ch_struct *, int, void*, int);
73 static int dgrp_tty_open(struct tty_struct *, struct file *);
74 static void dgrp_tty_close(struct tty_struct *, struct file *);
75 static int dgrp_tty_write(struct tty_struct *, const unsigned char *, int);
76 static int dgrp_tty_write_room(struct tty_struct *);
77 static void dgrp_tty_flush_buffer(struct tty_struct *);
78 static int dgrp_tty_chars_in_buffer(struct tty_struct *);
79 static int dgrp_tty_ioctl(struct tty_struct *, unsigned int, unsigned long);
80 static void dgrp_tty_set_termios(struct tty_struct *, struct ktermios *);
81 static void dgrp_tty_stop(struct tty_struct *);
82 static void dgrp_tty_start(struct tty_struct *);
83 static void dgrp_tty_throttle(struct tty_struct *);
84 static void dgrp_tty_unthrottle(struct tty_struct *);
85 static void dgrp_tty_hangup(struct tty_struct *);
86 static int dgrp_tty_put_char(struct tty_struct *, unsigned char);
87 static int dgrp_tty_tiocmget(struct tty_struct *);
88 static int dgrp_tty_tiocmset(struct tty_struct *, unsigned int, unsigned int);
89 static int dgrp_tty_send_break(struct tty_struct *, int);
90 static void dgrp_tty_send_xchar(struct tty_struct *, char);
95 #define SERIAL_TYPE_NORMAL 1
96 #define SERIAL_TYPE_CALLOUT 2
97 #define SERIAL_TYPE_XPRINT 3
101 * tty globals/statics
105 #define PORTSERVER_DIVIDEND 1843200
108 * Default transparent print information.
110 static struct digi_struct digi_init = {
111 .digi_flags = DIGI_COOK, /* Flags */
112 .digi_maxcps = 100, /* Max CPS */
113 .digi_maxchar = 50, /* Max chars in print queue */
114 .digi_bufsize = 100, /* Printer buffer size */
115 .digi_onlen = 4, /* size of printer on string */
116 .digi_offlen = 4, /* size of printer off string */
117 .digi_onstr = "\033[5i", /* ANSI printer on string */
118 .digi_offstr = "\033[4i", /* ANSI printer off string */
119 .digi_term = "ansi" /* default terminal type */
123 * Define a local default termios struct. All ports will be created
124 * with this termios initially.
126 * This defines a raw port at 9600 baud, 8 data bits, no parity,
129 static struct ktermios DefaultTermios = {
130 .c_iflag = (ICRNL | IXON),
131 .c_oflag = (OPOST | ONLCR),
132 .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
133 .c_lflag = (ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL
139 /* Define our tty operations struct */
140 static const struct tty_operations dgrp_tty_ops = {
141 .open = dgrp_tty_open,
142 .close = dgrp_tty_close,
143 .write = dgrp_tty_write,
144 .write_room = dgrp_tty_write_room,
145 .flush_buffer = dgrp_tty_flush_buffer,
146 .chars_in_buffer = dgrp_tty_chars_in_buffer,
148 .ioctl = dgrp_tty_ioctl,
149 .set_termios = dgrp_tty_set_termios,
150 .stop = dgrp_tty_stop,
151 .start = dgrp_tty_start,
152 .throttle = dgrp_tty_throttle,
153 .unthrottle = dgrp_tty_unthrottle,
154 .hangup = dgrp_tty_hangup,
155 .put_char = dgrp_tty_put_char,
156 .tiocmget = dgrp_tty_tiocmget,
157 .tiocmset = dgrp_tty_tiocmset,
158 .break_ctl = dgrp_tty_send_break,
159 .send_xchar = dgrp_tty_send_xchar
163 static int calc_baud_rate(struct un_struct *un)
173 static struct baud_rates baud_rates[] = {
196 brate = C_BAUD(un->un_tty);
198 for (i = 0; baud_rates[i].rate; i++) {
199 if (baud_rates[i].cflag == brate)
203 return baud_rates[i].rate;
206 static int calc_fastbaud_rate(struct un_struct *un, struct ktermios *uts)
211 ulong bauds[2][16] = {
213 0, 57600, 76800, 115200,
214 131657, 153600, 230400, 460800,
215 921600, 1200, 1800, 2400,
216 4800, 9600, 19200, 38400 },
217 { /* fastbaud & CBAUDEX */
218 0, 57600, 115200, 230400,
219 460800, 150, 200, 921600,
220 600, 1200, 1800, 2400,
221 4800, 9600, 19200, 38400 }
224 brate = C_BAUD(un->un_tty) & 0xff;
226 i = (uts->c_cflag & CBAUDEX) ? 1 : 0;
229 if ((i >= 0) && (i < 2) && (brate >= 0) && (brate < 16))
230 brate = bauds[i][brate];
238 * drp_param() -- send parameter values to be sent to the node
239 * @ch: channel structure of port to modify
241 * Interprets the tty and modem changes made by an application
242 * program (by examining the termios structures) and sets up
243 * parameter values to be sent to the node.
245 static void drp_param(struct ch_struct *ch)
247 struct nd_struct *nd;
248 struct un_struct *un;
253 struct ktermios *tts, *pts, *uts;
258 * If the terminal device is open, use it to set up all tty
259 * modes and functions. Otherwise use the printer device.
262 if (ch->ch_tun.un_open_count) {
265 tts = &ch->ch_tun.un_tty->termios;
268 * If both devices are open, copy critical line
269 * parameters from the tty device to the printer,
270 * so that if the tty is closed, the printer will
271 * continue without disruption.
274 if (ch->ch_pun.un_open_count) {
276 pts = &ch->ch_pun.un_tty->termios;
279 (pts->c_cflag ^ tts->c_cflag) &
280 (CBAUD | CSIZE | CSTOPB | CREAD | PARENB |
281 PARODD | HUPCL | CLOCAL);
284 (pts->c_iflag ^ tts->c_iflag) &
285 (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK |
286 ISTRIP | IXON | IXANY | IXOFF);
288 pts->c_cc[VSTART] = tts->c_cc[VSTART];
289 pts->c_cc[VSTOP] = tts->c_cc[VSTOP];
291 } else if (ch->ch_pun.un_open_count == 0) {
292 pr_warn("%s - ch_pun.un_open_count shouldn't be 0\n",
299 uts = &un->un_tty->termios;
302 * Determine if FAST writes can be performed.
305 if ((ch->ch_digi.digi_flags & DIGI_COOK) != 0 &&
306 (ch->ch_tun.un_open_count != 0) &&
307 !((un->un_tty)->ldisc->ops->flags & LDISC_FLAG_DEFINED) &&
308 !(L_XCASE(un->un_tty))) {
309 ch->ch_flag |= CH_FAST_WRITE;
311 ch->ch_flag &= ~CH_FAST_WRITE;
315 * If FAST writes can be performed, and OPOST is on in the
316 * terminal device, do OPOST handling in the server.
319 if ((ch->ch_flag & CH_FAST_WRITE) &&
320 O_OPOST(un->un_tty) != 0) {
321 int oflag = tty_to_ch_flags(un->un_tty, 'o');
323 /* add to ch_ocook any processing flags set in the termio */
324 ch->ch_ocook |= oflag & (OF_OLCUC |
331 * the hpux driver clears any flags set in ch_ocook
332 * from the termios oflag. It is STILL reported though
336 oflag = ch_to_tty_flags(ch->ch_ocook, 'o');
337 uts->c_oflag &= ~oflag;
340 /* clear the ch->ch_ocook flag */
341 int oflag = ch_to_tty_flags(ch->ch_ocook, 'o');
342 uts->c_oflag |= oflag;
346 ch->ch_oflag = ch->ch_ocook;
349 ch->ch_flag &= ~CH_FAST_READ;
352 * Generate channel flags
355 if (C_BAUD(un->un_tty) == B0) {
356 if (!(ch->ch_flag & CH_BAUD0)) {
357 /* TODO : the HPUX driver flushes line */
358 /* TODO : discipline, I assume I don't have to */
360 ch->ch_tout = ch->ch_tin;
361 ch->ch_rout = ch->ch_rin;
363 ch->ch_break_time = 0;
365 ch->ch_send |= RR_TX_FLUSH | RR_RX_FLUSH;
367 ch->ch_mout &= ~(DM_DTR | DM_RTS);
369 ch->ch_flag |= CH_BAUD0;
371 } else if (ch->ch_custom_speed) {
372 ch->ch_brate = PORTSERVER_DIVIDEND / ch->ch_custom_speed ;
374 if (ch->ch_flag & CH_BAUD0) {
375 ch->ch_mout |= DM_DTR | DM_RTS;
377 ch->ch_flag &= ~CH_BAUD0;
383 * If FASTBAUD isn't on, we can scan the new baud rate list
386 * However, if FASTBAUD is on, we must go to the old
387 * baud rate mapping that existed many many moons ago,
388 * for compatibility reasons.
391 if (!(ch->ch_digi.digi_flags & DIGI_FAST))
392 brate = calc_baud_rate(un);
394 brate = calc_fastbaud_rate(un, uts);
399 ch->ch_brate = PORTSERVER_DIVIDEND / brate;
401 if (ch->ch_flag & CH_BAUD0) {
402 ch->ch_mout |= DM_DTR | DM_RTS;
404 ch->ch_flag &= ~CH_BAUD0;
409 * Generate channel cflags from the termio.
412 ch->ch_cflag = tty_to_ch_flags(un->un_tty, 'c');
415 * Generate channel iflags from the termio.
418 iflag = (int) tty_to_ch_flags(un->un_tty, 'i');
420 if (START_CHAR(un->un_tty) == _POSIX_VDISABLE ||
421 STOP_CHAR(un->un_tty) == _POSIX_VDISABLE) {
422 iflag &= ~(IF_IXON | IF_IXANY | IF_IXOFF);
425 ch->ch_iflag = iflag;
428 * Generate flow control characters
432 * From the POSIX.1 spec (7.1.2.6): "If {_POSIX_VDISABLE}
433 * is defined for the terminal device file, and the value
434 * of one of the changable special control characters (see
435 * 7.1.1.9) is {_POSIX_VDISABLE}, that function shall be
436 * disabled, that is, no input data shall be recognized as
437 * the disabled special character."
439 * OK, so we don't ever assign S/DXB XON or XOFF to _POSIX_VDISABLE.
442 if (uts->c_cc[VSTART] != _POSIX_VDISABLE)
443 ch->ch_xon = uts->c_cc[VSTART];
444 if (uts->c_cc[VSTOP] != _POSIX_VDISABLE)
445 ch->ch_xoff = uts->c_cc[VSTOP];
447 ch->ch_lnext = (uts->c_cc[VLNEXT] == _POSIX_VDISABLE ? 0 :
451 * Also, if either c_cc[START] or c_cc[STOP] is set to
452 * _POSIX_VDISABLE, we can't really do software flow
453 * control--in either direction--so we turn it off as
454 * far as S/DXB is concerned. In essence, if you disable
455 * one, you disable the other too.
457 if ((uts->c_cc[VSTART] == _POSIX_VDISABLE) ||
458 (uts->c_cc[VSTOP] == _POSIX_VDISABLE))
459 ch->ch_iflag &= ~(IF_IXOFF | IF_IXON);
467 if (ch->ch_digi.digi_flags & DIGI_AIXON)
470 if ((ch->ch_xxon == _POSIX_VDISABLE) ||
471 (ch->ch_xxoff == _POSIX_VDISABLE))
474 ch->ch_xflag = xflag;
478 * Figure effective DCD value.
481 if (C_CLOCAL(un->un_tty))
482 ch->ch_flag |= CH_CLOCAL;
484 ch->ch_flag &= ~CH_CLOCAL;
487 * Check modem signals
493 * Get hardware handshake value.
498 if (C_CRTSCTS(un->un_tty))
499 mflow |= (DM_RTS | DM_CTS);
501 if (ch->ch_digi.digi_flags & RTSPACE)
504 if (ch->ch_digi.digi_flags & DTRPACE)
507 if (ch->ch_digi.digi_flags & CTSPACE)
510 if (ch->ch_digi.digi_flags & DSRPACE)
513 if (ch->ch_digi.digi_flags & DCDPACE)
516 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
517 mflow |= DM_RTS_TOGGLE;
519 ch->ch_mflow = mflow;
522 * Send the changes to the server.
525 ch->ch_flag |= CH_PARAM;
526 (ch->ch_nd)->nd_tx_work = 1;
528 if (waitqueue_active(&ch->ch_flag_wait))
529 wake_up_interruptible(&ch->ch_flag_wait);
533 * This function is just used as a callback for timeouts
534 * waiting on the ch_sleep flag.
536 static void wake_up_drp_sleep_timer(unsigned long ptr)
538 struct ch_struct *ch = (struct ch_struct *) ptr;
540 wake_up(&ch->ch_sleep);
545 * Set up our own sleep that can't be cancelled
546 * until our timeout occurs.
548 static void drp_my_sleep(struct ch_struct *ch)
550 struct timer_list drp_wakeup_timer;
551 DECLARE_WAITQUEUE(wait, current);
554 * First make sure we're ready to receive the wakeup.
557 add_wait_queue(&ch->ch_sleep, &wait);
558 current->state = TASK_UNINTERRUPTIBLE;
561 * Since we are uninterruptible, set a timer to
562 * unset the uninterruptable state in 1 second.
565 init_timer(&drp_wakeup_timer);
566 drp_wakeup_timer.function = wake_up_drp_sleep_timer;
567 drp_wakeup_timer.data = (unsigned long) ch;
568 drp_wakeup_timer.expires = jiffies + (1 * HZ);
569 add_timer(&drp_wakeup_timer);
573 del_timer(&drp_wakeup_timer);
575 remove_wait_queue(&ch->ch_sleep, &wait);
582 * -EBUSY - this is a callout device and the normal device is active
583 * - there is an error in opening the tty
584 * -ENODEV - the channel does not exist
585 * -EAGAIN - we are in the middle of hanging up or closing
586 * - IMMEDIATE_OPEN fails
588 * - if the port is outside physical range
589 * -EINTR - the open is interrupted
592 static int dgrp_tty_open(struct tty_struct *tty, struct file *file)
595 struct nd_struct *nd;
596 struct ch_struct *ch;
597 struct un_struct *un;
604 int counts_were_incremented = 0;
606 DECLARE_WAITQUEUE(wait, current);
609 * Do some initial checks to see if the node and port exist
612 nd = nd_struct_get(MAJOR(tty_devnum(tty)));
613 port = PORT_NUM(MINOR(tty_devnum(tty)));
614 category = OPEN_CATEGORY(MINOR(tty_devnum(tty)));
619 if (port >= CHAN_MAX)
623 * The channel exists.
626 ch = nd->nd_chan + port;
628 un = IS_PRINT(MINOR(tty_devnum(tty))) ? &ch->ch_pun : &ch->ch_tun;
630 tty->driver_data = un;
633 * If we are in the middle of hanging up,
634 * then return an error
636 if (tty_hung_up_p(file)) {
637 retval = ((un->un_flag & UN_HUP_NOTIFY) ?
638 -EAGAIN : -ERESTARTSYS);
643 * If the port is in the middle of closing, then block
644 * until it is done, then try again.
646 retval = wait_event_interruptible(un->un_close_wait,
647 ((un->un_flag & UN_CLOSING) == 0));
653 * If the port is in the middle of a reopen after a network disconnect,
654 * wait until it is done, then try again.
656 retval = wait_event_interruptible(ch->ch_flag_wait,
657 ((ch->ch_flag & CH_PORT_GONE) == 0));
663 * If this is a callout device, then just make sure the normal
664 * device isn't being used.
667 if (tty->driver->subtype == SERIAL_TYPE_CALLOUT) {
668 if (un->un_flag & UN_NORMAL_ACTIVE) {
672 un->un_flag |= UN_CALLOUT_ACTIVE;
677 * Loop waiting until the open can be successfully completed.
680 spin_lock_irqsave(&nd->nd_lock, lock_flags);
688 * Determine the open type from the flags provided.
692 * If the port is not enabled, then exit
694 if (test_bit(TTY_IO_ERROR, &tty->flags)) {
695 /* there was an error in opening the tty */
696 if (un->un_flag & UN_CALLOUT_ACTIVE)
699 un->un_flag |= UN_NORMAL_ACTIVE;
703 if (file->f_flags & O_NONBLOCK) {
706 * if the O_NONBLOCK is set, errors on read and write
707 * must return -EAGAIN immediately and NOT sleep
710 otype = OTYPE_IMMEDIATE;
711 delay_error = -EAGAIN;
713 } else if (!OPEN_WAIT_AVAIL(category) ||
714 (file->f_flags & O_NDELAY) != 0) {
715 otype = OTYPE_IMMEDIATE;
716 delay_error = -EBUSY;
718 } else if (!OPEN_WAIT_CARRIER(category) ||
719 ((ch->ch_digi.digi_flags & DIGI_FORCEDCD) != 0) ||
721 otype = OTYPE_PERSISTENT;
725 otype = OTYPE_INCOMING;
730 * Handle port currently outside physical port range.
733 if (port >= nd->nd_chan_count) {
734 if (otype == OTYPE_IMMEDIATE) {
735 retval = (nd->nd_state == NS_READY) ?
742 * Handle port not currently open.
745 else if (ch->ch_open_count == 0) {
747 * Return an error when an Incoming Open
748 * response indicates the port is busy.
751 if (ch->ch_open_error != 0 && otype == ch->ch_otype) {
752 retval = (ch->ch_open_error <= 2) ?
753 delay_error : -ENXIO ;
758 * Fail any new Immediate open if we do not have
759 * a normal connection to the server.
762 if (nd->nd_state != NS_READY &&
763 otype == OTYPE_IMMEDIATE) {
769 * If a Realport open of the correct type has
770 * succeeded, complete the open.
773 if (ch->ch_state == CS_READY && ch->ch_otype == otype)
778 * Handle port already open and active as a device
782 else if ((ch->ch_category == category) ||
783 IS_PRINT(MINOR(tty_devnum(tty)))) {
785 * Fail if opening the device now would
786 * violate exclusive use.
788 unf = ch->ch_tun.un_flag | ch->ch_pun.un_flag;
790 if ((file->f_flags & O_EXCL) || (unf & UN_EXCL)) {
796 * If the open device is in the hangup state, all
797 * system calls fail except close().
800 /* TODO : check on hangup_p calls */
802 if (ch->ch_flag & CH_HANGUP) {
808 * If the port is ready, and carrier is ignored
809 * or present, then complete the open.
812 if (ch->ch_state == CS_READY &&
813 (otype != OTYPE_INCOMING ||
814 ch->ch_flag & CH_VIRT_CD))
821 * Handle port active with a different category device.
825 if (otype == OTYPE_IMMEDIATE) {
826 retval = delay_error;
832 * Wait until conditions change, then take another
836 ch->ch_wait_count[otype]++;
839 ch->ch_wait_carrier++;
842 * Prepare the task to accept the wakeup, then
843 * release our locks and release control.
846 add_wait_queue(&ch->ch_flag_wait, &wait);
847 current->state = TASK_INTERRUPTIBLE;
849 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
852 * Give up control, we'll come back if we're
853 * interrupted or are woken up.
856 remove_wait_queue(&ch->ch_flag_wait, &wait);
858 spin_lock_irqsave(&nd->nd_lock, lock_flags);
860 current->state = TASK_RUNNING;
862 ch->ch_wait_count[otype]--;
865 ch->ch_wait_carrier--;
869 if (signal_pending(current)) {
876 * The open has succeeded. No turning back.
878 counts_were_incremented = 1;
883 * Initialize the channel, if it's not already open.
886 if (ch->ch_open_count == 1) {
889 ch->ch_category = category;
890 ch->ch_pscan_state = 0;
892 /* TODO : find out what PS-1 bug Gene was referring to */
893 /* TODO : in the following comment. */
895 ch->ch_send = RR_TX_START | RR_RX_START; /* PS-1 bug */
898 ch->ch_s_mlast & DM_CD ||
899 ch->ch_digi.digi_flags & DIGI_FORCEDCD)
900 ch->ch_flag |= CH_VIRT_CD;
901 else if (OPEN_FORCES_CARRIER(category))
902 ch->ch_flag |= CH_VIRT_CD;
907 * Initialize the unit, if it is not already open.
910 if (un->un_open_count == 1) {
912 * Since all terminal options are always sticky in Linux,
913 * we don't need the UN_STICKY flag to be handled specially.
915 /* clears all the digi flags, leaves serial flags */
916 un->un_flag &= ~UN_DIGI_MASK;
918 if (file->f_flags & O_EXCL)
919 un->un_flag |= UN_EXCL;
921 /* TODO : include "session" and "pgrp" */
924 * In Linux, all terminal parameters are intended to be sticky.
925 * as a result, we "remove" the code which once reset the ports
933 un->un_flag |= UN_INITIALIZED;
939 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
943 * Linux does a close for every open, even failed ones!
945 if (!counts_were_incremented) {
951 dev_err(tty->dev, "tty open bad return (%i)\n", retval);
960 * dgrp_tty_close() -- close function for tty_operations
962 static void dgrp_tty_close(struct tty_struct *tty, struct file *file)
964 struct ch_struct *ch;
965 struct un_struct *un;
966 struct nd_struct *nd;
973 int sent_printer_offstr = 0;
975 port = PORT_NUM(MINOR(tty_devnum(tty)));
977 un = tty->driver_data;
992 spin_lock_irqsave(&nd->nd_lock, lock_flags);
995 /* Used to be on channel basis, now we check on a unit basis. */
996 if (un->un_open_count != 1)
1000 * OK, its the last close on the unit
1002 un->un_flag |= UN_CLOSING;
1005 * Notify the discipline to only process XON/XOFF characters.
1010 * Wait for output to drain only if this is
1011 * the last close against the channel
1014 if (ch->ch_open_count == 1) {
1016 * If its the print device, we need to ensure at all costs that
1017 * the offstr will fit. If it won't, flush our tbuf.
1019 if (IS_PRINT(MINOR(tty_devnum(tty))) &&
1020 (((ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK) <
1021 ch->ch_digi.digi_offlen))
1022 ch->ch_tin = ch->ch_tout;
1025 * Turn off the printer. Don't bother checking to see if its
1026 * IS_PRINT... Since this is the last close the flag is going
1027 * to be cleared, so we MUST make sure the offstr gets inserted
1031 if ((ch->ch_flag & CH_PRON) != 0) {
1032 drp_wmove(ch, 0, ch->ch_digi.digi_offstr,
1033 ch->ch_digi.digi_offlen);
1034 ch->ch_flag &= ~CH_PRON;
1035 sent_printer_offstr = 1;
1040 * Wait until either the output queue has drained, or we see
1041 * absolutely no progress for 15 seconds.
1044 tpos = ch->ch_s_tpos;
1046 waketime = jiffies + 15 * HZ;
1051 * Make sure the port still exists.
1054 if (port >= nd->nd_chan_count) {
1059 if (signal_pending(current)) {
1065 * If the port is idle (not opened on the server), we have
1066 * no way of draining/flushing/closing the port on that server.
1067 * So break out of loop.
1069 if (ch->ch_state == CS_IDLE)
1075 * Exit if the queues for this unit are empty,
1076 * and either the other unit is still open or all
1080 if ((un->un_tty)->ops->chars_in_buffer ?
1081 ((un->un_tty)->ops->chars_in_buffer)(un->un_tty) == 0 : 1) {
1084 * We don't need to wait for a buffer to drain
1085 * if the other unit is open.
1088 if (ch->ch_open_count != un->un_open_count)
1092 * The wait is complete when all queues are
1093 * drained, and any break in progress is complete.
1096 if (ch->ch_tin == ch->ch_tout &&
1097 ch->ch_s_tin == ch->ch_s_tpos &&
1098 (ch->ch_send & RR_TX_BREAK) == 0) {
1104 * Flush TX data and exit the wait if NDELAY is set,
1105 * or this is not a DIGI printer, and the close timeout
1109 if ((file->f_flags & (O_NDELAY | O_NONBLOCK)) ||
1110 ((long)(jiffies - waketime) >= 0 &&
1111 (ch->ch_digi.digi_flags & DIGI_PRINTER) == 0)) {
1114 * If we sent the printer off string, we cannot
1115 * flush our internal buffers, or we might lose
1118 if (!sent_printer_offstr)
1119 dgrp_tty_flush_buffer(tty);
1121 tty_ldisc_flush(tty);
1126 * Otherwise take a short nap.
1129 ch->ch_flag |= CH_DRAIN;
1131 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1133 schedule_timeout_interruptible(1);
1134 s = signal_pending(current);
1136 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1140 * If we had sent the printer off string, we now have
1143 * The system won't let us sleep since we got an error
1144 * back from sleep, presumably because the user did
1146 * But we need to ensure that the offstr gets sent!
1147 * Thus, we have to do something else besides sleeping.
1149 * 1) Make this task uninterruptable.
1150 * 2) Set up a timer to go off in 1 sec.
1151 * 3) Act as tho we just got out of the sleep above.
1153 * Thankfully, in the real world, this just
1157 if (sent_printer_offstr) {
1158 spin_unlock_irqrestore(&nd->nd_lock,
1161 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1169 * Restart the wait if any progress is seen.
1172 if (ch->ch_s_tpos != tpos) {
1173 tpos = ch->ch_s_tpos;
1175 /* TODO: this gives us timeout problems with nist ?? */
1176 waketime = jiffies + 15 * HZ;
1181 * Close the line discipline
1184 /* this is done in tty_io.c */
1185 /* if ((un->un_tty)->ldisc.close)
1186 * ((un->un_tty)->ldisc.close)(un->un_tty);
1189 /* don't do this here */
1190 /* un->un_flag = 0; */
1193 * Flush the receive buffer on terminal unit close only.
1196 if (!IS_PRINT(MINOR(tty_devnum(tty))))
1197 ch->ch_rout = ch->ch_rin;
1201 * Don't permit the close to happen until we get any pending
1202 * sync request responses.
1203 * There could be other ports depending upon the response as well.
1205 * Also, don't permit the close to happen until any parameter
1206 * changes have been sent out from the state machine as well.
1207 * This is required because of a ditty -a race with -HUPCL
1208 * We MUST make sure all channel parameters have been sent to the
1209 * Portserver before sending a close.
1212 if ((err != 1) && (ch->ch_state != CS_IDLE)) {
1213 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1214 s = wait_event_interruptible(ch->ch_flag_wait,
1215 ((ch->ch_flag & (CH_WAITING_SYNC | CH_PARAM)) == 0));
1216 spin_lock_irqsave(&nd->nd_lock, lock_flags);
1220 * Cleanup the channel if last unit open.
1223 if (ch->ch_open_count == 1) {
1225 ch->ch_category = 0;
1228 ch->ch_tout = ch->ch_tin;
1229 /* (un->un_tty)->device = 0; */
1231 if (ch->ch_state == CS_READY)
1232 ch->ch_state = CS_SEND_CLOSE;
1236 * Send the changes to the server
1238 if (ch->ch_state != CS_IDLE) {
1239 ch->ch_flag |= CH_PARAM;
1240 wake_up_interruptible(&ch->ch_flag_wait);
1244 nd->nd_tx_ready = 1;
1249 if (ch->ch_open_count <= 0)
1251 "%s - unexpected value for ch->ch_open_count: %i\n",
1252 __func__, ch->ch_open_count);
1254 ch->ch_open_count--;
1256 if (un->un_open_count <= 0)
1258 "%s - unexpected value for un->un_open_count: %i\n",
1259 __func__, un->un_open_count);
1261 un->un_open_count--;
1263 un->un_flag &= ~(UN_NORMAL_ACTIVE | UN_CALLOUT_ACTIVE | UN_CLOSING);
1264 if (waitqueue_active(&un->un_close_wait))
1265 wake_up_interruptible(&un->un_close_wait);
1267 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
1273 static void drp_wmove(struct ch_struct *ch, int from_user, void *buf, int count)
1278 ch->ch_nd->nd_tx_work = 1;
1280 n = TBUF_MAX - ch->ch_tin;
1284 ret = copy_from_user(ch->ch_tbuf + ch->ch_tin,
1285 (void __user *) buf, n);
1287 memcpy(ch->ch_tbuf + ch->ch_tin, buf, n);
1289 buf = (char *) buf + n;
1295 ret = copy_from_user(ch->ch_tbuf + ch->ch_tin,
1296 (void __user *) buf, count);
1298 memcpy(ch->ch_tbuf + ch->ch_tin, buf, count);
1300 ch->ch_tin += count;
1304 static int dgrp_calculate_txprint_bounds(struct ch_struct *ch, int space,
1309 unsigned short tmax = 0;
1312 * If the terminal device is busy, reschedule when
1313 * the terminal device becomes idle.
1316 if (ch->ch_tun.un_open_count != 0 &&
1317 ch->ch_tun.un_tty->ops->chars_in_buffer &&
1318 ((ch->ch_tun.un_tty->ops->chars_in_buffer)(ch->ch_tun.un_tty) != 0)) {
1319 *un_flag = UN_PWAIT;
1324 * Assure that whenever there is printer data in the output
1325 * buffer, there always remains enough space after it to
1326 * turn the printer off.
1328 space -= ch->ch_digi.digi_offlen;
1331 *un_flag = UN_EMPTY;
1336 * We measure printer CPS speed by incrementing
1337 * ch_cpstime by (HZ / digi_maxcps) for every
1338 * character we output, restricting output so
1339 * that ch_cpstime never exceeds lbolt.
1341 * However if output has not been done for some
1342 * time, lbolt will grow to very much larger than
1343 * ch_cpstime, which would allow essentially
1344 * unlimited amounts of output until ch_cpstime
1345 * finally caught up. To avoid this, we adjust
1346 * cps_time when necessary so the difference
1347 * between lbolt and ch_cpstime never results
1348 * in sending more than digi_bufsize characters.
1350 * This nicely models a printer with an internal
1351 * buffer of digi_bufsize characters.
1353 * Get the time between lbolt and ch->ch_cpstime;
1356 tt = jiffies - ch->ch_cpstime;
1359 * Compute the time required to send digi_bufsize
1363 mt = HZ * ch->ch_digi.digi_bufsize / ch->ch_digi.digi_maxcps;
1366 * Compute the number of characters that can be sent
1367 * without violating the time constraint. If the
1368 * direct calculation of this number is bigger than
1369 * digi_bufsize, limit the number to digi_bufsize,
1370 * and adjust cpstime to match.
1373 if ((clock_t)(tt + HZ) > (clock_t)(mt + HZ)) {
1374 tmax = ch->ch_digi.digi_bufsize;
1375 ch->ch_cpstime = jiffies - mt;
1377 tmax = ch->ch_digi.digi_maxcps * tt / HZ;
1381 * If the time constraint now binds, limit the transmit
1382 * count accordingly, and tentatively arrange to be
1383 * rescheduled based on time.
1392 * Compute the total number of characters we can
1393 * output before the total number of characters known
1394 * to be in the output queue exceeds digi_maxchar.
1397 tmax = (ch->ch_digi.digi_maxchar -
1398 ((ch->ch_tin - ch->ch_tout) & TBUF_MASK) -
1399 ((ch->ch_s_tin - ch->ch_s_tpos) & 0xffff));
1403 * If the digi_maxchar constraint now holds, limit
1404 * the transmit count accordingly, and arrange to
1405 * be rescheduled when the queue becomes empty.
1409 *un_flag = UN_EMPTY;
1414 *un_flag |= UN_EMPTY;
1420 static int dgrp_tty_write(struct tty_struct *tty,
1421 const unsigned char *buf,
1424 struct nd_struct *nd;
1425 struct un_struct *un;
1426 struct ch_struct *ch;
1437 un = tty->driver_data;
1450 * Ignore the request if the channel is not ready.
1452 if (ch->ch_state != CS_READY)
1455 spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
1458 * Ignore the request if output is blocked.
1460 if ((un->un_flag & (UN_EMPTY | UN_LOW | UN_TIME | UN_PWAIT)) != 0) {
1466 * Also ignore the request if DPA has this port open,
1467 * and is flow controlled on reading more data.
1469 if (nd->nd_dpa_debug && nd->nd_dpa_flag & DPA_WAIT_SPACE &&
1470 nd->nd_dpa_port == MINOR(tty_devnum(ch->ch_tun.un_tty))) {
1476 * Limit amount we will write to the amount of space
1477 * available in the channel buffer.
1481 space = (ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK;
1484 * Handle the printer device.
1489 if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1492 unsigned short tmax = 0;
1495 * If the terminal device is busy, reschedule when
1496 * the terminal device becomes idle.
1499 if (ch->ch_tun.un_open_count != 0 &&
1500 ((ch->ch_tun.un_tty->ops->chars_in_buffer)(ch->ch_tun.un_tty) != 0)) {
1501 un->un_flag |= UN_PWAIT;
1507 * Assure that whenever there is printer data in the output
1508 * buffer, there always remains enough space after it to
1509 * turn the printer off.
1511 space -= ch->ch_digi.digi_offlen;
1514 * Output the printer on string.
1517 if ((ch->ch_flag & CH_PRON) == 0) {
1518 space -= ch->ch_digi.digi_onlen;
1521 un->un_flag |= UN_EMPTY;
1522 (ch->ch_nd)->nd_tx_work = 1;
1527 drp_wmove(ch, 0, ch->ch_digi.digi_onstr,
1528 ch->ch_digi.digi_onlen);
1530 ch->ch_flag |= CH_PRON;
1534 * We measure printer CPS speed by incrementing
1535 * ch_cpstime by (HZ / digi_maxcps) for every
1536 * character we output, restricting output so
1537 * that ch_cpstime never exceeds lbolt.
1539 * However if output has not been done for some
1540 * time, lbolt will grow to very much larger than
1541 * ch_cpstime, which would allow essentially
1542 * unlimited amounts of output until ch_cpstime
1543 * finally caught up. To avoid this, we adjust
1544 * cps_time when necessary so the difference
1545 * between lbolt and ch_cpstime never results
1546 * in sending more than digi_bufsize characters.
1548 * This nicely models a printer with an internal
1549 * buffer of digi_bufsize characters.
1551 * Get the time between lbolt and ch->ch_cpstime;
1554 tt = jiffies - ch->ch_cpstime;
1557 * Compute the time required to send digi_bufsize
1561 mt = HZ * ch->ch_digi.digi_bufsize / ch->ch_digi.digi_maxcps;
1564 * Compute the number of characters that can be sent
1565 * without violating the time constraint. If the
1566 * direct calculation of this number is bigger than
1567 * digi_bufsize, limit the number to digi_bufsize,
1568 * and adjust cpstime to match.
1571 if ((clock_t)(tt + HZ) > (clock_t)(mt + HZ)) {
1572 tmax = ch->ch_digi.digi_bufsize;
1573 ch->ch_cpstime = jiffies - mt;
1575 tmax = ch->ch_digi.digi_maxcps * tt / HZ;
1579 * If the time constraint now binds, limit the transmit
1580 * count accordingly, and tentatively arrange to be
1581 * rescheduled based on time.
1590 * Compute the total number of characters we can
1591 * output before the total number of characters known
1592 * to be in the output queue exceeds digi_maxchar.
1595 tmax = (ch->ch_digi.digi_maxchar -
1596 ((ch->ch_tin - ch->ch_tout) & TBUF_MASK) -
1597 ((ch->ch_s_tin - ch->ch_s_tpos) & 0xffff));
1601 * If the digi_maxchar constraint now holds, limit
1602 * the transmit count accordingly, and arrange to
1603 * be rescheduled when the queue becomes empty.
1613 * Handle the terminal device.
1618 * If the printer device is on, turn it off.
1621 if ((ch->ch_flag & CH_PRON) != 0) {
1623 space -= ch->ch_digi.digi_offlen;
1625 drp_wmove(ch, 0, ch->ch_digi.digi_offstr,
1626 ch->ch_digi.digi_offlen);
1628 ch->ch_flag &= ~CH_PRON;
1633 * If space is 0 and its because the ch->tbuf
1634 * is full, then Linux will handle a callback when queue
1635 * space becomes available.
1636 * tty_write returns count = 0
1640 /* the linux tty_io.c handles this if we return 0 */
1641 /* if (fp->flags & O_NONBLOCK) return -EAGAIN; */
1643 un->un_flag |= UN_EMPTY;
1644 (ch->ch_nd)->nd_tx_work = 1;
1649 count = min(count, space);
1656 * Copy the buffer contents to the ch_tbuf
1657 * being careful to wrap around the circular queue
1660 t = TBUF_MAX - ch->ch_tin;
1664 memcpy(ch->ch_tbuf + ch->ch_tin, buf, t);
1665 if (nd->nd_dpa_debug && nd->nd_dpa_port == PORT_NUM(MINOR(tty_devnum(un->un_tty))))
1666 dgrp_dpa_data(nd, 0, (char *) buf, t);
1673 memcpy(ch->ch_tbuf + ch->ch_tin, buf, n);
1674 if (nd->nd_dpa_debug && nd->nd_dpa_port == PORT_NUM(MINOR(tty_devnum(un->un_tty))))
1675 dgrp_dpa_data(nd, 0, (char *) buf, n);
1681 (ch->ch_nd)->nd_tx_work = 1;
1682 if (ch->ch_edelay != DGRP_RTIME) {
1683 (ch->ch_nd)->nd_tx_ready = 1;
1684 wake_up_interruptible(&nd->nd_tx_waitq);
1688 ch->ch_txcount += count;
1690 if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1693 * Adjust ch_cpstime to account
1694 * for the characters just output.
1697 if (sendcount > 0) {
1698 int cc = HZ * sendcount + ch->ch_cpsrem;
1700 ch->ch_cpstime += cc / ch->ch_digi.digi_maxcps;
1701 ch->ch_cpsrem = cc % ch->ch_digi.digi_maxcps;
1705 * If we are now waiting on time, schedule ourself
1706 * back when we'll be able to send a block of
1707 * digi_maxchar characters.
1710 if ((un_flag & UN_TIME) != 0) {
1711 ch->ch_waketime = (ch->ch_cpstime +
1712 (ch->ch_digi.digi_maxchar * HZ /
1713 ch->ch_digi.digi_maxcps));
1718 * If the printer unit is waiting for completion
1719 * of terminal output, get him going again.
1722 if ((ch->ch_pun.un_flag & UN_PWAIT) != 0)
1723 (ch->ch_nd)->nd_tx_work = 1;
1726 spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
1733 * Put a character into ch->ch_buf
1735 * - used by the line discipline for OPOST processing
1738 static int dgrp_tty_put_char(struct tty_struct *tty, unsigned char new_char)
1740 struct un_struct *un;
1741 struct ch_struct *ch;
1749 un = tty->driver_data;
1757 if (ch->ch_state != CS_READY)
1760 spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
1764 * If space is 0 and its because the ch->tbuf
1765 * Warn and dump the character, there isn't anything else
1766 * we can do about it. David_Fries@digi.com
1769 space = (ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK;
1774 * Output the printer on string if device is TXPrint.
1776 if (IS_PRINT(MINOR(tty_devnum(tty))) && (ch->ch_flag & CH_PRON) == 0) {
1777 if (space < ch->ch_digi.digi_onlen) {
1781 space -= ch->ch_digi.digi_onlen;
1782 drp_wmove(ch, 0, ch->ch_digi.digi_onstr,
1783 ch->ch_digi.digi_onlen);
1784 ch->ch_flag |= CH_PRON;
1788 * Output the printer off string if device is NOT TXPrint.
1791 if (!IS_PRINT(MINOR(tty_devnum(tty))) &&
1792 ((ch->ch_flag & CH_PRON) != 0)) {
1793 if (space < ch->ch_digi.digi_offlen) {
1798 space -= ch->ch_digi.digi_offlen;
1799 drp_wmove(ch, 0, ch->ch_digi.digi_offstr,
1800 ch->ch_digi.digi_offlen);
1801 ch->ch_flag &= ~CH_PRON;
1810 * Copy the character to the ch_tbuf being
1811 * careful to wrap around the circular queue
1813 ch->ch_tbuf[ch->ch_tin] = new_char;
1814 ch->ch_tin = (1 + ch->ch_tin) & TBUF_MASK;
1816 if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1819 * Adjust ch_cpstime to account
1820 * for the character just output.
1823 int cc = HZ + ch->ch_cpsrem;
1825 ch->ch_cpstime += cc / ch->ch_digi.digi_maxcps;
1826 ch->ch_cpsrem = cc % ch->ch_digi.digi_maxcps;
1829 * If we are now waiting on time, schedule ourself
1830 * back when we'll be able to send a block of
1831 * digi_maxchar characters.
1834 ch->ch_waketime = (ch->ch_cpstime +
1835 (ch->ch_digi.digi_maxchar * HZ /
1836 ch->ch_digi.digi_maxcps));
1841 (ch->ch_nd)->nd_tx_work = 1;
1845 spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
1852 * Flush TX buffer (make in == out)
1854 * check tty_ioctl.c -- this is called after TCOFLUSH
1856 static void dgrp_tty_flush_buffer(struct tty_struct *tty)
1858 struct un_struct *un;
1859 struct ch_struct *ch;
1863 un = tty->driver_data;
1871 ch->ch_tout = ch->ch_tin;
1872 /* do NOT do this here! */
1873 /* ch->ch_s_tpos = ch->ch_s_tin = 0; */
1875 /* send the flush output command now */
1876 ch->ch_send |= RR_TX_FLUSH;
1877 (ch->ch_nd)->nd_tx_ready = 1;
1878 (ch->ch_nd)->nd_tx_work = 1;
1879 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
1881 if (waitqueue_active(&tty->write_wait))
1882 wake_up_interruptible(&tty->write_wait);
1889 * Return space available in Tx buffer
1890 * count = ( ch->ch_tout - ch->ch_tin ) mod (TBUF_MAX - 1)
1892 static int dgrp_tty_write_room(struct tty_struct *tty)
1894 struct un_struct *un;
1895 struct ch_struct *ch;
1901 un = tty->driver_data;
1909 count = (ch->ch_tout - ch->ch_tin - 1) & TBUF_MASK;
1911 /* We *MUST* check this, and return 0 if the Printer Unit cannot
1912 * take any more data within its time constraints... If we don't
1913 * return 0 and the printer has hit it time constraint, the ld will
1914 * call us back doing a put_char, which cannot be rejected!!!
1916 if (IS_PRINT(MINOR(tty_devnum(tty)))) {
1918 count = dgrp_calculate_txprint_bounds(ch, count, &un_flag);
1922 ch->ch_pun.un_flag |= un_flag;
1923 (ch->ch_nd)->nd_tx_work = 1;
1930 * Return number of characters that have not been transmitted yet.
1931 * chars_in_buffer = ( ch->ch_tin - ch->ch_tout ) mod (TBUF_MAX - 1)
1932 * + ( ch->ch_s_tin - ch->ch_s_tout ) mod (0xffff)
1933 * = number of characters "in transit"
1935 * Remember that sequence number math is always with a sixteen bit
1936 * mask, not the TBUF_MASK.
1939 static int dgrp_tty_chars_in_buffer(struct tty_struct *tty)
1941 struct un_struct *un;
1942 struct ch_struct *ch;
1949 un = tty->driver_data;
1957 count1 = count = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1958 count += (ch->ch_s_tin - ch->ch_s_tpos) & 0xffff;
1959 /* one for tbuf, one for the PS */
1962 * If we are busy transmitting add 1
1964 count += un->un_tbusy;
1970 /*****************************************************************************
1972 * Helper applications for dgrp_tty_ioctl()
1974 *****************************************************************************
1979 * ch_to_tty_flags() -- convert channel flags to termio flags
1980 * @ch_flag: Digi channel flags
1981 * @flagtype: type of ch_flag (iflag, oflag or cflag)
1983 * take the channel flags of the specified type and return the
1984 * corresponding termio flag
1986 static tcflag_t ch_to_tty_flags(ushort ch_flag, char flagtype)
1988 tcflag_t retval = 0;
1992 retval = ((ch_flag & IF_IGNBRK) ? IGNBRK : 0)
1993 | ((ch_flag & IF_BRKINT) ? BRKINT : 0)
1994 | ((ch_flag & IF_IGNPAR) ? IGNPAR : 0)
1995 | ((ch_flag & IF_PARMRK) ? PARMRK : 0)
1996 | ((ch_flag & IF_INPCK) ? INPCK : 0)
1997 | ((ch_flag & IF_ISTRIP) ? ISTRIP : 0)
1998 | ((ch_flag & IF_IXON) ? IXON : 0)
1999 | ((ch_flag & IF_IXANY) ? IXANY : 0)
2000 | ((ch_flag & IF_IXOFF) ? IXOFF : 0);
2004 retval = ((ch_flag & OF_OLCUC) ? OLCUC : 0)
2005 | ((ch_flag & OF_ONLCR) ? ONLCR : 0)
2006 | ((ch_flag & OF_OCRNL) ? OCRNL : 0)
2007 | ((ch_flag & OF_ONOCR) ? ONOCR : 0)
2008 | ((ch_flag & OF_ONLRET) ? ONLRET : 0)
2009 /* | ((ch_flag & OF_OTAB3) ? OFILL : 0) */
2010 | ((ch_flag & OF_TABDLY) ? TABDLY : 0);
2014 retval = ((ch_flag & CF_CSTOPB) ? CSTOPB : 0)
2015 | ((ch_flag & CF_CREAD) ? CREAD : 0)
2016 | ((ch_flag & CF_PARENB) ? PARENB : 0)
2017 | ((ch_flag & CF_PARODD) ? PARODD : 0)
2018 | ((ch_flag & CF_HUPCL) ? HUPCL : 0);
2020 switch (ch_flag & CF_CSIZE) {
2051 * tty_to_ch_flags() -- convert termio flags to digi channel flags
2052 * @tty: pointer to a TTY structure holding flag to be converted
2053 * @flagtype: identifies which flag (iflags, oflags, or cflags) should
2056 * take the termio flag of the specified type and return the
2057 * corresponding Digi version of the flags
2059 static ushort tty_to_ch_flags(struct tty_struct *tty, char flagtype)
2066 tflag = tty->termios.c_iflag;
2067 retval = (I_IGNBRK(tty) ? IF_IGNBRK : 0)
2068 | (I_BRKINT(tty) ? IF_BRKINT : 0)
2069 | (I_IGNPAR(tty) ? IF_IGNPAR : 0)
2070 | (I_PARMRK(tty) ? IF_PARMRK : 0)
2071 | (I_INPCK(tty) ? IF_INPCK : 0)
2072 | (I_ISTRIP(tty) ? IF_ISTRIP : 0)
2073 | (I_IXON(tty) ? IF_IXON : 0)
2074 | (I_IXANY(tty) ? IF_IXANY : 0)
2075 | (I_IXOFF(tty) ? IF_IXOFF : 0);
2078 tflag = tty->termios.c_oflag;
2080 * If OPOST is set, then do the post processing in the
2081 * firmware by setting all the processing flags on.
2082 * If ~OPOST, then make sure we are not doing any
2083 * output processing!!
2088 retval = (O_OLCUC(tty) ? OF_OLCUC : 0)
2089 | (O_ONLCR(tty) ? OF_ONLCR : 0)
2090 | (O_OCRNL(tty) ? OF_OCRNL : 0)
2091 | (O_ONOCR(tty) ? OF_ONOCR : 0)
2092 | (O_ONLRET(tty) ? OF_ONLRET : 0)
2093 /* | (O_OFILL(tty) ? OF_TAB3 : 0) */
2094 | (O_TABDLY(tty) ? OF_TABDLY : 0);
2097 tflag = tty->termios.c_cflag;
2098 retval = (C_CSTOPB(tty) ? CF_CSTOPB : 0)
2099 | (C_CREAD(tty) ? CF_CREAD : 0)
2100 | (C_PARENB(tty) ? CF_PARENB : 0)
2101 | (C_PARODD(tty) ? CF_PARODD : 0)
2102 | (C_HUPCL(tty) ? CF_HUPCL : 0);
2103 switch (C_CSIZE(tty)) {
2133 static int dgrp_tty_send_break(struct tty_struct *tty, int msec)
2135 struct un_struct *un;
2136 struct ch_struct *ch;
2142 un = tty->driver_data;
2150 dgrp_send_break(ch, msec);
2156 * This routine sends a break character out the serial port.
2158 * duration is in 1/1000's of a second
2160 static int dgrp_send_break(struct ch_struct *ch, int msec)
2164 wait_event_interruptible(ch->ch_flag_wait,
2165 ((ch->ch_flag & CH_TX_BREAK) == 0));
2166 ch->ch_break_time += max(msec, 250);
2167 ch->ch_send |= RR_TX_BREAK;
2168 ch->ch_flag |= CH_TX_BREAK;
2169 (ch->ch_nd)->nd_tx_work = 1;
2171 x = (msec * HZ) / 1000;
2172 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2179 * Return modem signals to ld.
2181 static int dgrp_tty_tiocmget(struct tty_struct *tty)
2184 struct un_struct *un = tty->driver_data;
2185 struct ch_struct *ch;
2194 mlast = ((ch->ch_s_mlast & ~(DM_RTS | DM_DTR)) |
2195 (ch->ch_mout & (DM_RTS | DM_DTR)));
2197 /* defined in /usr/include/asm/termios.h */
2198 mlast = ((mlast & DM_RTS) ? TIOCM_RTS : 0)
2199 | ((mlast & DM_DTR) ? TIOCM_DTR : 0)
2200 | ((mlast & DM_CD) ? TIOCM_CAR : 0)
2201 | ((mlast & DM_RI) ? TIOCM_RNG : 0)
2202 | ((mlast & DM_DSR) ? TIOCM_DSR : 0)
2203 | ((mlast & DM_CTS) ? TIOCM_CTS : 0);
2212 static int dgrp_tty_tiocmset(struct tty_struct *tty,
2213 unsigned int set, unsigned int clear)
2216 struct un_struct *un = tty->driver_data;
2217 struct ch_struct *ch;
2226 if (set & TIOCM_RTS)
2227 ch->ch_mout |= DM_RTS;
2229 if (set & TIOCM_DTR)
2230 ch->ch_mout |= DM_DTR;
2232 if (clear & TIOCM_RTS)
2233 ch->ch_mout &= ~(DM_RTS);
2235 if (clear & TIOCM_DTR)
2236 ch->ch_mout &= ~(DM_DTR);
2238 spin_lock_irqsave(&(ch->ch_nd)->nd_lock, lock_flags);
2239 ch->ch_flag |= CH_PARAM;
2240 (ch->ch_nd)->nd_tx_work = 1;
2241 wake_up_interruptible(&ch->ch_flag_wait);
2243 spin_unlock_irqrestore(&(ch->ch_nd)->nd_lock, lock_flags);
2251 * Get current modem status
2253 static int get_modem_info(struct ch_struct *ch, unsigned int *value)
2257 mlast = ((ch->ch_s_mlast & ~(DM_RTS | DM_DTR)) |
2258 (ch->ch_mout & (DM_RTS | DM_DTR)));
2260 /* defined in /usr/include/asm/termios.h */
2261 mlast = ((mlast & DM_RTS) ? TIOCM_RTS : 0)
2262 | ((mlast & DM_DTR) ? TIOCM_DTR : 0)
2263 | ((mlast & DM_CD) ? TIOCM_CAR : 0)
2264 | ((mlast & DM_RI) ? TIOCM_RNG : 0)
2265 | ((mlast & DM_DSR) ? TIOCM_DSR : 0)
2266 | ((mlast & DM_CTS) ? TIOCM_CTS : 0);
2267 put_user(mlast, (unsigned int __user *) value);
2275 static int set_modem_info(struct ch_struct *ch, unsigned int command,
2276 unsigned int *value)
2283 error = access_ok(VERIFY_READ, (void __user *) value, sizeof(int));
2287 get_user(arg, (unsigned int __user *) value);
2288 mval |= ((arg & TIOCM_RTS) ? DM_RTS : 0)
2289 | ((arg & TIOCM_DTR) ? DM_DTR : 0);
2292 case TIOCMBIS: /* set flags */
2293 ch->ch_mout |= mval;
2295 case TIOCMBIC: /* clear flags */
2296 ch->ch_mout &= ~mval;
2305 spin_lock_irqsave(&(ch->ch_nd)->nd_lock, lock_flags);
2307 ch->ch_flag |= CH_PARAM;
2308 (ch->ch_nd)->nd_tx_work = 1;
2309 wake_up_interruptible(&ch->ch_flag_wait);
2311 spin_unlock_irqrestore(&(ch->ch_nd)->nd_lock, lock_flags);
2318 * Assign the custom baud rate to the channel structure
2320 static void dgrp_set_custom_speed(struct ch_struct *ch, int newrate)
2326 int deltahigh, deltalow;
2332 * Since the divisor is stored in a 16-bit integer, we make sure
2333 * we don't allow any rates smaller than a 16-bit integer would allow.
2334 * And of course, rates above the dividend won't fly.
2336 if (newrate && newrate < ((PORTSERVER_DIVIDEND / 0xFFFF) + 1))
2337 newrate = ((PORTSERVER_DIVIDEND / 0xFFFF) + 1);
2338 if (newrate && newrate > PORTSERVER_DIVIDEND)
2339 newrate = PORTSERVER_DIVIDEND;
2341 while (newrate > 0) {
2342 testdiv = PORTSERVER_DIVIDEND / newrate;
2345 * If we try to figure out what rate the PortServer would use
2346 * with the test divisor, it will be either equal or higher
2347 * than the requested baud rate. If we then determine the
2348 * rate with a divisor one higher, we will get the next lower
2349 * supported rate below the requested.
2351 testrate_high = PORTSERVER_DIVIDEND / testdiv;
2352 testrate_low = PORTSERVER_DIVIDEND / (testdiv + 1);
2355 * If the rate for the requested divisor is correct, just
2356 * use it and be done.
2358 if (testrate_high == newrate)
2362 * Otherwise, pick the rate that is closer (i.e. whichever rate
2363 * has a smaller delta).
2365 deltahigh = testrate_high - newrate;
2366 deltalow = newrate - testrate_low;
2368 if (deltahigh < deltalow)
2369 newrate = testrate_high;
2371 newrate = testrate_low;
2376 ch->ch_custom_speed = newrate;
2385 # dgrp_tty_digiseta()
2387 * Ioctl to set the information from ditty.
2389 * NOTE: DIGI_IXON, DSRPACE, DCDPACE, and DTRPACE are unsupported. JAR 990922
2391 static int dgrp_tty_digiseta(struct tty_struct *tty,
2392 struct digi_struct *new_info)
2394 struct un_struct *un = tty->driver_data;
2395 struct ch_struct *ch;
2404 if (copy_from_user(&ch->ch_digi, (void __user *) new_info,
2405 sizeof(struct digi_struct)))
2408 if ((ch->ch_digi.digi_flags & RTSPACE) ||
2409 (ch->ch_digi.digi_flags & CTSPACE))
2410 tty->termios.c_cflag |= CRTSCTS;
2412 tty->termios.c_cflag &= ~CRTSCTS;
2414 if (ch->ch_digi.digi_maxcps < 1)
2415 ch->ch_digi.digi_maxcps = 1;
2417 if (ch->ch_digi.digi_maxcps > 10000)
2418 ch->ch_digi.digi_maxcps = 10000;
2420 if (ch->ch_digi.digi_bufsize < 10)
2421 ch->ch_digi.digi_bufsize = 10;
2423 if (ch->ch_digi.digi_maxchar < 1)
2424 ch->ch_digi.digi_maxchar = 1;
2426 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2427 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2429 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2430 ch->ch_digi.digi_onlen = DIGI_PLEN;
2432 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2433 ch->ch_digi.digi_offlen = DIGI_PLEN;
2435 /* make the changes now */
2444 * dgrp_tty_digigetedelay()
2446 * Ioctl to get the current edelay setting.
2451 static int dgrp_tty_digigetedelay(struct tty_struct *tty, int *retinfo)
2453 struct un_struct *un;
2454 struct ch_struct *ch;
2460 if (!tty || tty->magic != TTY_MAGIC)
2463 un = tty->driver_data;
2472 tmp = ch->ch_edelay;
2474 if (copy_to_user((void __user *) retinfo, &tmp, sizeof(*retinfo)))
2482 * dgrp_tty_digisetedelay()
2484 * Ioctl to set the EDELAY setting
2487 static int dgrp_tty_digisetedelay(struct tty_struct *tty, int *new_info)
2489 struct un_struct *un;
2490 struct ch_struct *ch;
2493 if (!tty || tty->magic != TTY_MAGIC)
2496 un = tty->driver_data;
2505 if (copy_from_user(&new_digi, (void __user *)new_info, sizeof(int)))
2508 ch->ch_edelay = new_digi;
2510 /* make the changes now */
2518 * The usual assortment of ioctl's
2520 * note: use tty_check_change to make sure that we are not
2521 * changing the state of a terminal when we are not a process
2522 * in the forground. See tty_io.c
2523 * rc = tty_check_change(tty);
2524 * if (rc) return rc;
2526 static int dgrp_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2529 struct un_struct *un;
2530 struct ch_struct *ch;
2532 struct digiflow_struct dflow;
2537 un = tty->driver_data;
2548 * Here are all the standard ioctl's that we MUST implement
2553 * TCSBRK is SVID version: non-zero arg --> no break
2554 * this behaviour is exploited by tcdrain().
2556 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2557 * between 0.25 and 0.5 seconds
2560 rc = tty_check_change(tty);
2563 tty_wait_until_sent(tty, 0);
2566 rc = dgrp_send_break(ch, 250); /* 1/4 second */
2568 if (dgrp_tty_chars_in_buffer(tty) != 0)
2574 /* support for POSIX tcsendbreak()
2576 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2577 * between 0.25 and 0.5 seconds so we'll ask for something
2578 * in the middle: 0.375 seconds.
2580 rc = tty_check_change(tty);
2583 tty_wait_until_sent(tty, 0);
2585 rc = dgrp_send_break(ch, arg ? arg*250 : 250);
2587 if (dgrp_tty_chars_in_buffer(tty) != 0)
2592 rc = tty_check_change(tty);
2595 tty_wait_until_sent(tty, 0);
2598 * RealPort doesn't support turning on a break unconditionally.
2599 * The RealPort device will stop sending a break automatically
2600 * after the specified time value that we send in.
2602 rc = dgrp_send_break(ch, 250); /* 1/4 second */
2604 if (dgrp_tty_chars_in_buffer(tty) != 0)
2610 * RealPort doesn't support turning off a break unconditionally.
2611 * The RealPort device will stop sending a break automatically
2612 * after the specified time value that was sent when turning on
2618 rc = access_ok(VERIFY_WRITE, (void __user *) arg,
2622 put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
2626 get_user(arg, (unsigned long __user *) arg);
2627 tty->termios.c_cflag =
2628 ((tty->termios.c_cflag & ~CLOCAL) |
2629 (arg ? CLOCAL : 0));
2633 rc = access_ok(VERIFY_WRITE, (void __user *) arg,
2634 sizeof(unsigned int));
2637 return get_modem_info(ch, (unsigned int *) arg);
2642 return set_modem_info(ch, cmd, (unsigned int *) arg);
2645 * Here are any additional ioctl's that we want to implement
2650 * The linux tty driver doesn't have a flush
2651 * input routine for the driver, assuming all backed
2652 * up data is in the line disc. buffers. However,
2653 * we all know that's not the case. Here, we
2654 * act on the ioctl, but then lie and say we didn't
2655 * so the line discipline will process the flush
2658 rc = tty_check_change(tty);
2665 /* only flush input if this is the only open unit */
2666 if (!IS_PRINT(MINOR(tty_devnum(tty)))) {
2667 ch->ch_rout = ch->ch_rin;
2668 ch->ch_send |= RR_RX_FLUSH;
2669 (ch->ch_nd)->nd_tx_work = 1;
2670 (ch->ch_nd)->nd_tx_ready = 1;
2671 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2673 if (arg == TCIFLUSH)
2676 case TCOFLUSH: /* flush output, or the receive buffer */
2678 * This is handled in the tty_ioctl.c code
2679 * calling tty_flush_buffer
2684 /* POSIX.1 says return EINVAL if we got a bad arg */
2687 /* pretend we didn't recognize this IOCTL */
2688 return -ENOIOCTLCMD;
2693 /*****************************************
2695 TCSETA TCSETA - set the termios
2696 TCSETAF TCSETAF - wait for drain first, then set termios
2697 TCSETAW TCSETAW - wait for drain, flush the input queue, then set termios
2698 - looking at the tty_ioctl code, these command all call our
2699 tty_set_termios at the driver's end, when a TCSETA* is sent,
2700 it is expecting the tty to have a termio structure,
2701 NOT a termios stucture. These two structures differ in size
2702 and the tty_ioctl code does a conversion before processing them both.
2703 - we should treat the TCSETAW TCSETAF ioctls the same, and let
2704 the tty_ioctl code do the conversion stuff.
2709 - the associated tty structure has a termios structure.
2710 *****************************************/
2714 return -ENOIOCTLCMD;
2721 * The linux tty driver doesn't have a flush
2722 * input routine for the driver, assuming all backed
2723 * up data is in the line disc. buffers. However,
2724 * we all know that's not the case. Here, we
2725 * act on the ioctl, but then lie and say we didn't
2726 * so the line discipline will process the flush
2731 * Also, now that we have TXPrint, we have to check
2732 * if this is the TXPrint device and the terminal
2733 * device is open. If so, do NOT run check_change,
2734 * as the terminal device is ALWAYS the parent.
2736 if (!IS_PRINT(MINOR(tty_devnum(tty))) ||
2737 !ch->ch_tun.un_open_count) {
2738 rc = tty_check_change(tty);
2743 /* wait for all the characters in tbuf to drain */
2744 tty_wait_until_sent(tty, 0);
2746 if ((cmd == TCSETSF) || (cmd == TCSETAF)) {
2747 /* flush the contents of the rbuf queue */
2748 /* TODO: check if this is print device? */
2749 ch->ch_send |= RR_RX_FLUSH;
2750 (ch->ch_nd)->nd_tx_ready = 1;
2751 (ch->ch_nd)->nd_tx_work = 1;
2752 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2753 /* do we need to do this? just to be safe! */
2754 ch->ch_rout = ch->ch_rin;
2757 /* pretend we didn't recognize this */
2758 return -ENOIOCTLCMD;
2762 * The Linux Line Discipline (LD) would do this for us if we
2763 * let it, but we have the special firmware options to do this
2764 * the "right way" regardless of hardware or software flow
2765 * control so we'll do it outselves instead of letting the LD
2768 rc = tty_check_change(tty);
2774 dgrp_tty_start(tty);
2780 dgrp_tty_input_start(tty);
2783 dgrp_tty_input_stop(tty);
2790 /* get information for ditty */
2791 if (copy_to_user((struct digi_struct __user *) arg,
2792 &ch->ch_digi, sizeof(struct digi_struct)))
2798 /* wait for all the characters in tbuf to drain */
2799 tty_wait_until_sent(tty, 0);
2801 if (cmd == DIGI_SETAF) {
2802 /* flush the contents of the rbuf queue */
2803 /* send down a packet with RR_RX_FLUSH set */
2804 ch->ch_send |= RR_RX_FLUSH;
2805 (ch->ch_nd)->nd_tx_ready = 1;
2806 (ch->ch_nd)->nd_tx_work = 1;
2807 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2808 /* do we need to do this? just to be safe! */
2809 ch->ch_rout = ch->ch_rin;
2812 /* pretend we didn't recognize this */
2815 return dgrp_tty_digiseta(tty, (struct digi_struct *) arg);
2818 return dgrp_tty_digisetedelay(tty, (int *) arg);
2821 return dgrp_tty_digigetedelay(tty, (int *) arg);
2825 if (cmd == (DIGI_GETFLOW)) {
2826 dflow.startc = tty->termios.c_cc[VSTART];
2827 dflow.stopc = tty->termios.c_cc[VSTOP];
2829 dflow.startc = ch->ch_xxon;
2830 dflow.stopc = ch->ch_xxoff;
2833 if (copy_to_user((char __user *)arg, &dflow, sizeof(dflow)))
2840 if (copy_from_user(&dflow, (char __user *)arg, sizeof(dflow)))
2843 if (cmd == (DIGI_SETFLOW)) {
2844 tty->termios.c_cc[VSTART] = dflow.startc;
2845 tty->termios.c_cc[VSTOP] = dflow.stopc;
2847 ch->ch_xxon = dflow.startc;
2848 ch->ch_xxoff = dflow.stopc;
2852 case DIGI_GETCUSTOMBAUD:
2853 rc = access_ok(VERIFY_WRITE, (void __user *) arg, sizeof(int));
2856 put_user(ch->ch_custom_speed, (unsigned int __user *) arg);
2859 case DIGI_SETCUSTOMBAUD:
2863 get_user(new_rate, (unsigned int __user *) arg);
2864 dgrp_set_custom_speed(ch, new_rate);
2870 return -ENOIOCTLCMD;
2877 * This routine allows the tty driver to be notified when
2878 * the device's termios setting have changed. Note that we
2879 * should be prepared to accept the case where old == NULL
2880 * and try to do something rational.
2882 * So we need to make sure that our copies of ch_oflag,
2883 * ch_clag, and ch_iflag reflect the tty->termios flags.
2885 static void dgrp_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
2887 struct ktermios *ts;
2888 struct ch_struct *ch;
2889 struct un_struct *un;
2891 /* seems silly, but we have to check all these! */
2895 un = tty->driver_data;
2907 /* the CLOCAL flag has just been set */
2908 if (!(old->c_cflag & CLOCAL) && C_CLOCAL(tty))
2909 wake_up_interruptible(&un->un_open_wait);
2914 * Throttle receiving data. We just set a bit and stop reading
2915 * data out of the channel buffer. It will back up and the
2916 * FEP will do whatever is necessary to stop the far end.
2918 static void dgrp_tty_throttle(struct tty_struct *tty)
2920 struct ch_struct *ch;
2925 ch = ((struct un_struct *) tty->driver_data)->un_ch;
2929 ch->ch_flag |= CH_RXSTOP;
2933 static void dgrp_tty_unthrottle(struct tty_struct *tty)
2935 struct ch_struct *ch;
2940 ch = ((struct un_struct *) tty->driver_data)->un_ch;
2944 ch->ch_flag &= ~CH_RXSTOP;
2948 * Stop the transmitter
2950 static void dgrp_tty_stop(struct tty_struct *tty)
2952 struct ch_struct *ch;
2957 ch = ((struct un_struct *) tty->driver_data)->un_ch;
2961 ch->ch_send |= RR_TX_STOP;
2962 ch->ch_send &= ~RR_TX_START;
2964 /* make the change NOW! */
2965 (ch->ch_nd)->nd_tx_ready = 1;
2966 if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
2967 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
2971 * Start the transmitter
2973 static void dgrp_tty_start(struct tty_struct *tty)
2975 struct ch_struct *ch;
2980 ch = ((struct un_struct *) tty->driver_data)->un_ch;
2984 /* TODO: don't do anything if the transmitter is not stopped */
2986 ch->ch_send |= RR_TX_START;
2987 ch->ch_send &= ~RR_TX_STOP;
2989 /* make the change NOW! */
2990 (ch->ch_nd)->nd_tx_ready = 1;
2991 (ch->ch_nd)->nd_tx_work = 1;
2992 if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
2993 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
3000 static void dgrp_tty_input_stop(struct tty_struct *tty)
3002 struct ch_struct *ch;
3007 ch = ((struct un_struct *) tty->driver_data)->un_ch;
3011 ch->ch_send |= RR_RX_STOP;
3012 ch->ch_send &= ~RR_RX_START;
3013 (ch->ch_nd)->nd_tx_ready = 1;
3014 if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
3015 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
3020 static void dgrp_tty_send_xchar(struct tty_struct *tty, char c)
3022 struct un_struct *un;
3023 struct ch_struct *ch;
3028 un = tty->driver_data;
3035 if (c == STOP_CHAR(tty))
3036 ch->ch_send |= RR_RX_STOP;
3037 else if (c == START_CHAR(tty))
3038 ch->ch_send |= RR_RX_START;
3040 ch->ch_nd->nd_tx_ready = 1;
3041 ch->ch_nd->nd_tx_work = 1;
3047 static void dgrp_tty_input_start(struct tty_struct *tty)
3049 struct ch_struct *ch;
3054 ch = ((struct un_struct *) tty->driver_data)->un_ch;
3058 ch->ch_send |= RR_RX_START;
3059 ch->ch_send &= ~RR_RX_STOP;
3060 (ch->ch_nd)->nd_tx_ready = 1;
3061 (ch->ch_nd)->nd_tx_work = 1;
3062 if (waitqueue_active(&(ch->ch_nd)->nd_tx_waitq))
3063 wake_up_interruptible(&(ch->ch_nd)->nd_tx_waitq);
3069 * Hangup the port. Like a close, but don't wait for output
3072 * How do we close all the channels that are open?
3074 static void dgrp_tty_hangup(struct tty_struct *tty)
3076 struct ch_struct *ch;
3077 struct nd_struct *nd;
3078 struct un_struct *un;
3083 un = tty->driver_data;
3095 ch->ch_mout &= ~DM_DTR;
3096 /* Don't do this here */
3097 /* ch->ch_flag |= CH_HANGUP; */
3098 ch->ch_nd->nd_tx_ready = 1;
3099 ch->ch_nd->nd_tx_work = 1;
3100 if (waitqueue_active(&ch->ch_flag_wait))
3101 wake_up_interruptible(&ch->ch_flag_wait);
3106 /************************************************************************/
3108 /* TTY Initialization/Cleanup Functions */
3110 /************************************************************************/
3113 * Uninitialize the TTY portion of the supplied node. Free all
3114 * memory and resources associated with this node. Do it in reverse
3115 * allocation order: this might possibly result in less fragmentation
3116 * of memory, though I don't know this for sure.
3119 dgrp_tty_uninit(struct nd_struct *nd)
3123 ID_TO_CHAR(nd->nd_ID, id);
3125 if (nd->nd_ttdriver_flags & SERIAL_TTDRV_REG) {
3126 tty_unregister_driver(nd->nd_serial_ttdriver);
3128 kfree(nd->nd_serial_ttdriver->ttys);
3129 nd->nd_serial_ttdriver->ttys = NULL;
3131 put_tty_driver(nd->nd_serial_ttdriver);
3132 nd->nd_ttdriver_flags &= ~SERIAL_TTDRV_REG;
3135 if (nd->nd_ttdriver_flags & CALLOUT_TTDRV_REG) {
3136 tty_unregister_driver(nd->nd_callout_ttdriver);
3138 kfree(nd->nd_callout_ttdriver->ttys);
3139 nd->nd_callout_ttdriver->ttys = NULL;
3141 put_tty_driver(nd->nd_callout_ttdriver);
3142 nd->nd_ttdriver_flags &= ~CALLOUT_TTDRV_REG;
3145 if (nd->nd_ttdriver_flags & XPRINT_TTDRV_REG) {
3146 tty_unregister_driver(nd->nd_xprint_ttdriver);
3148 kfree(nd->nd_xprint_ttdriver->ttys);
3149 nd->nd_xprint_ttdriver->ttys = NULL;
3151 put_tty_driver(nd->nd_xprint_ttdriver);
3152 nd->nd_ttdriver_flags &= ~XPRINT_TTDRV_REG;
3159 * Initialize the TTY portion of the supplied node.
3162 dgrp_tty_init(struct nd_struct *nd)
3168 ID_TO_CHAR(nd->nd_ID, id);
3171 * Initialize the TTDRIVER structures.
3174 nd->nd_serial_ttdriver = alloc_tty_driver(CHAN_MAX);
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(nd->nd_serial_ttdriver);
3221 /* Really fail now, since we ran out
3222 * of majors to try. */
3223 if (i == max_majors)
3230 nd->nd_ttdriver_flags |= SERIAL_TTDRV_REG;
3233 nd->nd_callout_ttdriver = alloc_tty_driver(CHAN_MAX);
3234 sprintf(nd->nd_callout_name, "cu_dgrp_%s_", id);
3236 nd->nd_callout_ttdriver->owner = THIS_MODULE;
3237 nd->nd_callout_ttdriver->name = nd->nd_callout_name;
3238 nd->nd_callout_ttdriver->name_base = 0;
3239 nd->nd_callout_ttdriver->major = nd->nd_serial_ttdriver->major;
3240 nd->nd_callout_ttdriver->minor_start = 0x40;
3241 nd->nd_callout_ttdriver->type = TTY_DRIVER_TYPE_SERIAL;
3242 nd->nd_callout_ttdriver->subtype = SERIAL_TYPE_CALLOUT;
3243 nd->nd_callout_ttdriver->init_termios = DefaultTermios;
3244 nd->nd_callout_ttdriver->driver_name = "dgrp";
3245 nd->nd_callout_ttdriver->flags = (TTY_DRIVER_REAL_RAW |
3246 TTY_DRIVER_DYNAMIC_DEV |
3247 TTY_DRIVER_HARDWARE_BREAK);
3249 /* The kernel wants space to store pointers to tty_structs. */
3250 nd->nd_callout_ttdriver->ttys =
3251 kzalloc(CHAN_MAX * sizeof(struct tty_struct *), GFP_KERNEL);
3252 if (!nd->nd_callout_ttdriver->ttys)
3255 tty_set_operations(nd->nd_callout_ttdriver, &dgrp_tty_ops);
3257 if (dgrp_register_cudevices) {
3258 if (!(nd->nd_ttdriver_flags & CALLOUT_TTDRV_REG)) {
3260 * Register cu devices
3262 rc = tty_register_driver(nd->nd_callout_ttdriver);
3265 nd->nd_ttdriver_flags |= CALLOUT_TTDRV_REG;
3270 nd->nd_xprint_ttdriver = alloc_tty_driver(CHAN_MAX);
3271 sprintf(nd->nd_xprint_name, "pr_dgrp_%s_", id);
3273 nd->nd_xprint_ttdriver->owner = THIS_MODULE;
3274 nd->nd_xprint_ttdriver->name = nd->nd_xprint_name;
3275 nd->nd_xprint_ttdriver->name_base = 0;
3276 nd->nd_xprint_ttdriver->major = nd->nd_serial_ttdriver->major;
3277 nd->nd_xprint_ttdriver->minor_start = 0x80;
3278 nd->nd_xprint_ttdriver->type = TTY_DRIVER_TYPE_SERIAL;
3279 nd->nd_xprint_ttdriver->subtype = SERIAL_TYPE_XPRINT;
3280 nd->nd_xprint_ttdriver->init_termios = DefaultTermios;
3281 nd->nd_xprint_ttdriver->driver_name = "dgrp";
3282 nd->nd_xprint_ttdriver->flags = (TTY_DRIVER_REAL_RAW |
3283 TTY_DRIVER_DYNAMIC_DEV |
3284 TTY_DRIVER_HARDWARE_BREAK);
3286 /* The kernel wants space to store pointers to tty_structs. */
3287 nd->nd_xprint_ttdriver->ttys =
3288 kzalloc(CHAN_MAX * sizeof(struct tty_struct *), GFP_KERNEL);
3289 if (!nd->nd_xprint_ttdriver->ttys)
3292 tty_set_operations(nd->nd_xprint_ttdriver, &dgrp_tty_ops);
3294 if (dgrp_register_prdevices) {
3295 if (!(nd->nd_ttdriver_flags & XPRINT_TTDRV_REG)) {
3297 * Register transparent print devices
3299 rc = tty_register_driver(nd->nd_xprint_ttdriver);
3302 nd->nd_ttdriver_flags |= XPRINT_TTDRV_REG;
3306 for (i = 0; i < CHAN_MAX; i++) {
3307 struct ch_struct *ch = nd->nd_chan + i;
3310 ch->ch_digi = digi_init;
3311 ch->ch_edelay = 100;
3312 ch->ch_custom_speed = 0;
3314 ch->ch_tun.un_ch = ch;
3315 ch->ch_pun.un_ch = ch;
3316 ch->ch_tun.un_type = SERIAL_TYPE_NORMAL;
3317 ch->ch_pun.un_type = SERIAL_TYPE_XPRINT;
3319 init_waitqueue_head(&(ch->ch_flag_wait));
3320 init_waitqueue_head(&(ch->ch_sleep));
3322 init_waitqueue_head(&(ch->ch_tun.un_open_wait));
3323 init_waitqueue_head(&(ch->ch_tun.un_close_wait));
3325 init_waitqueue_head(&(ch->ch_pun.un_open_wait));
3326 init_waitqueue_head(&(ch->ch_pun.un_close_wait));
3327 tty_port_init(&ch->port);
3328 tty_port_init(&ch->port);