2 * Copyright 2003 Digi International (www.digi.com)
3 * Scott H Kilau <Scott_Kilau at digi dot com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * NOTE TO LINUX KERNEL HACKERS: DO NOT REFORMAT THIS CODE!
22 * This is shared code between Digi's CVS archive and the
23 * Linux Kernel sources.
24 * Changing the source just for reformatting needlessly breaks
25 * our CVS diff history.
27 * Send any bug fixes/changes to: Eng.Linux at digi dot com.
31 /************************************************************************
33 * This file implements the tty driver functionality for the
34 * Neo and ClassicBoard PCI based product lines.
36 ************************************************************************
40 #include <linux/kernel.h>
41 #include <linux/sched.h> /* For jiffies, task states */
42 #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
43 #include <linux/module.h>
44 #include <linux/ctype.h>
45 #include <linux/tty.h>
46 #include <linux/tty_flip.h>
47 #include <linux/serial_reg.h>
48 #include <linux/slab.h>
49 #include <linux/delay.h> /* For udelay */
50 #include <asm/uaccess.h> /* For copy_from_user/copy_to_user */
51 #include <linux/pci.h>
53 #include "dgnc_driver.h"
55 #include "dgnc_types.h"
56 #include "dgnc_trace.h"
59 #include "dpacompat.h"
60 #include "dgnc_sysfs.h"
62 #define init_MUTEX(sem) sema_init(sem, 1)
63 #define DECLARE_MUTEX(name) \
64 struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
69 static struct dgnc_board *dgnc_BoardsByMajor[256];
70 static uchar *dgnc_TmpWriteBuf = NULL;
71 static DECLARE_MUTEX(dgnc_TmpWriteSem);
74 * Default transparent print information.
76 static struct digi_t dgnc_digi_init = {
77 .digi_flags = DIGI_COOK, /* Flags */
78 .digi_maxcps = 100, /* Max CPS */
79 .digi_maxchar = 50, /* Max chars in print queue */
80 .digi_bufsize = 100, /* Printer buffer size */
81 .digi_onlen = 4, /* size of printer on string */
82 .digi_offlen = 4, /* size of printer off string */
83 .digi_onstr = "\033[5i", /* ANSI printer on string ] */
84 .digi_offstr = "\033[4i", /* ANSI printer off string ] */
85 .digi_term = "ansi" /* default terminal type */
90 * Define a local default termios struct. All ports will be created
91 * with this termios initially.
93 * This defines a raw port at 9600 baud, 8 data bits, no parity,
96 static struct ktermios DgncDefaultTermios =
98 .c_iflag = (DEFAULT_IFLAGS), /* iflags */
99 .c_oflag = (DEFAULT_OFLAGS), /* oflags */
100 .c_cflag = (DEFAULT_CFLAGS), /* cflags */
101 .c_lflag = (DEFAULT_LFLAGS), /* lflags */
107 /* Our function prototypes */
108 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
109 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
110 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch);
111 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
112 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo);
113 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info);
114 static int dgnc_tty_write_room(struct tty_struct *tty);
115 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
116 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
117 static void dgnc_tty_start(struct tty_struct *tty);
118 static void dgnc_tty_stop(struct tty_struct *tty);
119 static void dgnc_tty_throttle(struct tty_struct *tty);
120 static void dgnc_tty_unthrottle(struct tty_struct *tty);
121 static void dgnc_tty_flush_chars(struct tty_struct *tty);
122 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
123 static void dgnc_tty_hangup(struct tty_struct *tty);
124 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value);
125 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value);
126 static int dgnc_tty_tiocmget(struct tty_struct *tty);
127 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear);
128 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
129 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
130 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf, int count);
131 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios);
132 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
135 static const struct tty_operations dgnc_tty_ops = {
136 .open = dgnc_tty_open,
137 .close = dgnc_tty_close,
138 .write = dgnc_tty_write,
139 .write_room = dgnc_tty_write_room,
140 .flush_buffer = dgnc_tty_flush_buffer,
141 .chars_in_buffer = dgnc_tty_chars_in_buffer,
142 .flush_chars = dgnc_tty_flush_chars,
143 .ioctl = dgnc_tty_ioctl,
144 .set_termios = dgnc_tty_set_termios,
145 .stop = dgnc_tty_stop,
146 .start = dgnc_tty_start,
147 .throttle = dgnc_tty_throttle,
148 .unthrottle = dgnc_tty_unthrottle,
149 .hangup = dgnc_tty_hangup,
150 .put_char = dgnc_tty_put_char,
151 .tiocmget = dgnc_tty_tiocmget,
152 .tiocmset = dgnc_tty_tiocmset,
153 .break_ctl = dgnc_tty_send_break,
154 .wait_until_sent = dgnc_tty_wait_until_sent,
155 .send_xchar = dgnc_tty_send_xchar
158 /************************************************************************
160 * TTY Initialization/Cleanup Functions
162 ************************************************************************/
167 * Initialize any global tty related data before we download any boards.
169 int dgnc_tty_preinit(void)
172 * Allocate a buffer for doing the copy from user space to
173 * kernel space in dgnc_write(). We only use one buffer and
174 * control access to it with a semaphore. If we are paging, we
175 * are already in trouble so one buffer won't hurt much anyway.
177 * We are okay to sleep in the malloc, as this routine
178 * is only called during module load, (not in interrupt context),
179 * and with no locks held.
181 dgnc_TmpWriteBuf = kmalloc(WRITEBUFLEN, GFP_KERNEL);
183 if (!dgnc_TmpWriteBuf) {
184 DPR_INIT(("unable to allocate tmp write buf"));
193 * dgnc_tty_register()
195 * Init the tty subsystem for this board.
197 int dgnc_tty_register(struct dgnc_board *brd)
201 DPR_INIT(("tty_register start\n"));
203 memset(&brd->SerialDriver, 0, sizeof(brd->SerialDriver));
204 memset(&brd->PrintDriver, 0, sizeof(brd->PrintDriver));
206 brd->SerialDriver.magic = TTY_DRIVER_MAGIC;
208 snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgnc_%d_", brd->boardnum);
210 brd->SerialDriver.name = brd->SerialName;
211 brd->SerialDriver.name_base = 0;
212 brd->SerialDriver.major = 0;
213 brd->SerialDriver.minor_start = 0;
214 brd->SerialDriver.num = brd->maxports;
215 brd->SerialDriver.type = TTY_DRIVER_TYPE_SERIAL;
216 brd->SerialDriver.subtype = SERIAL_TYPE_NORMAL;
217 brd->SerialDriver.init_termios = DgncDefaultTermios;
218 brd->SerialDriver.driver_name = DRVSTR;
219 brd->SerialDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
222 * The kernel wants space to store pointers to
223 * tty_struct's and termios's.
225 brd->SerialDriver.ttys = kzalloc(brd->maxports * sizeof(*brd->SerialDriver.ttys), GFP_KERNEL);
226 if (!brd->SerialDriver.ttys)
229 kref_init(&brd->SerialDriver.kref);
230 brd->SerialDriver.termios = kzalloc(brd->maxports * sizeof(*brd->SerialDriver.termios), GFP_KERNEL);
231 if (!brd->SerialDriver.termios)
235 * Entry points for driver. Called by the kernel from
236 * tty_io.c and n_tty.c.
238 tty_set_operations(&brd->SerialDriver, &dgnc_tty_ops);
240 if (!brd->dgnc_Major_Serial_Registered) {
241 /* Register tty devices */
242 rc = tty_register_driver(&brd->SerialDriver);
244 APR(("Can't register tty device (%d)\n", rc));
247 brd->dgnc_Major_Serial_Registered = TRUE;
251 * If we're doing transparent print, we have to do all of the above
252 * again, separately so we don't get the LD confused about what major
253 * we are when we get into the dgnc_tty_open() routine.
255 brd->PrintDriver.magic = TTY_DRIVER_MAGIC;
256 snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
258 brd->PrintDriver.name = brd->PrintName;
259 brd->PrintDriver.name_base = 0;
260 brd->PrintDriver.major = brd->SerialDriver.major;
261 brd->PrintDriver.minor_start = 0x80;
262 brd->PrintDriver.num = brd->maxports;
263 brd->PrintDriver.type = TTY_DRIVER_TYPE_SERIAL;
264 brd->PrintDriver.subtype = SERIAL_TYPE_NORMAL;
265 brd->PrintDriver.init_termios = DgncDefaultTermios;
266 brd->PrintDriver.driver_name = DRVSTR;
267 brd->PrintDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
270 * The kernel wants space to store pointers to
271 * tty_struct's and termios's. Must be separated from
272 * the Serial Driver so we don't get confused
274 brd->PrintDriver.ttys = kzalloc(brd->maxports * sizeof(*brd->PrintDriver.ttys), GFP_KERNEL);
275 if (!brd->PrintDriver.ttys)
277 kref_init(&brd->PrintDriver.kref);
278 brd->PrintDriver.termios = kzalloc(brd->maxports * sizeof(*brd->PrintDriver.termios), GFP_KERNEL);
279 if (!brd->PrintDriver.termios)
283 * Entry points for driver. Called by the kernel from
284 * tty_io.c and n_tty.c.
286 tty_set_operations(&brd->PrintDriver, &dgnc_tty_ops);
288 if (!brd->dgnc_Major_TransparentPrint_Registered) {
289 /* Register Transparent Print devices */
290 rc = tty_register_driver(&brd->PrintDriver);
292 APR(("Can't register Transparent Print device (%d)\n", rc));
295 brd->dgnc_Major_TransparentPrint_Registered = TRUE;
298 dgnc_BoardsByMajor[brd->SerialDriver.major] = brd;
299 brd->dgnc_Serial_Major = brd->SerialDriver.major;
300 brd->dgnc_TransparentPrint_Major = brd->PrintDriver.major;
302 DPR_INIT(("DGNC REGISTER TTY: MAJOR: %d\n", brd->SerialDriver.major));
311 * Init the tty subsystem. Called once per board after board has been
312 * downloaded and init'ed.
314 int dgnc_tty_init(struct dgnc_board *brd)
318 struct channel_t *ch;
323 DPR_INIT(("dgnc_tty_init start\n"));
326 * Initialize board structure elements.
329 vaddr = brd->re_map_membase;
331 brd->nasync = brd->maxports;
334 * Allocate channel memory that might not have been allocated
335 * when the driver was first loaded.
337 for (i = 0; i < brd->nasync; i++) {
338 if (!brd->channels[i]) {
341 * Okay to malloc with GFP_KERNEL, we are not at
342 * interrupt context, and there are no locks held.
344 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]), GFP_KERNEL);
345 if (!brd->channels[i]) {
346 DPR_CORE(("%s:%d Unable to allocate memory for channel struct\n",
347 __FILE__, __LINE__));
352 ch = brd->channels[0];
353 vaddr = brd->re_map_membase;
355 /* Set up channel variables */
356 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
358 if (!brd->channels[i])
361 DGNC_SPINLOCK_INIT(ch->ch_lock);
363 /* Store all our magic numbers */
364 ch->magic = DGNC_CHANNEL_MAGIC;
365 ch->ch_tun.magic = DGNC_UNIT_MAGIC;
366 ch->ch_tun.un_ch = ch;
367 ch->ch_tun.un_type = DGNC_SERIAL;
368 ch->ch_tun.un_dev = i;
370 ch->ch_pun.magic = DGNC_UNIT_MAGIC;
371 ch->ch_pun.un_ch = ch;
372 ch->ch_pun.un_type = DGNC_PRINT;
373 ch->ch_pun.un_dev = i + 128;
375 if (brd->bd_uart_offset == 0x200)
376 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
378 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
382 ch->ch_digi = dgnc_digi_init;
384 /* .25 second delay */
385 ch->ch_close_delay = 250;
387 init_waitqueue_head(&ch->ch_flags_wait);
388 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
389 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
390 init_waitqueue_head(&ch->ch_sniff_wait);
393 struct device *classp;
394 classp = tty_register_device(&brd->SerialDriver, i,
395 &(ch->ch_bd->pdev->dev));
396 ch->ch_tun.un_sysfs = classp;
397 dgnc_create_tty_sysfs(&ch->ch_tun, classp);
399 classp = tty_register_device(&brd->PrintDriver, i,
400 &(ch->ch_bd->pdev->dev));
401 ch->ch_pun.un_sysfs = classp;
402 dgnc_create_tty_sysfs(&ch->ch_pun, classp);
407 DPR_INIT(("dgnc_tty_init finish\n"));
414 * dgnc_tty_post_uninit()
416 * UnInitialize any global tty related data.
418 void dgnc_tty_post_uninit(void)
420 kfree(dgnc_TmpWriteBuf);
421 dgnc_TmpWriteBuf = NULL;
428 * Uninitialize the TTY portion of this driver. Free all memory and
431 void dgnc_tty_uninit(struct dgnc_board *brd)
435 if (brd->dgnc_Major_Serial_Registered) {
436 dgnc_BoardsByMajor[brd->SerialDriver.major] = NULL;
437 brd->dgnc_Serial_Major = 0;
438 for (i = 0; i < brd->nasync; i++) {
439 dgnc_remove_tty_sysfs(brd->channels[i]->ch_tun.un_sysfs);
440 tty_unregister_device(&brd->SerialDriver, i);
442 tty_unregister_driver(&brd->SerialDriver);
443 brd->dgnc_Major_Serial_Registered = FALSE;
446 if (brd->dgnc_Major_TransparentPrint_Registered) {
447 dgnc_BoardsByMajor[brd->PrintDriver.major] = NULL;
448 brd->dgnc_TransparentPrint_Major = 0;
449 for (i = 0; i < brd->nasync; i++) {
450 dgnc_remove_tty_sysfs(brd->channels[i]->ch_pun.un_sysfs);
451 tty_unregister_device(&brd->PrintDriver, i);
453 tty_unregister_driver(&brd->PrintDriver);
454 brd->dgnc_Major_TransparentPrint_Registered = FALSE;
457 kfree(brd->SerialDriver.ttys);
458 brd->SerialDriver.ttys = NULL;
459 kfree(brd->PrintDriver.ttys);
460 brd->PrintDriver.ttys = NULL;
464 #define TMPBUFLEN (1024)
467 * dgnc_sniff - Dump data out to the "sniff" buffer if the
468 * proc sniff file is opened...
470 void dgnc_sniff_nowait_nolock(struct channel_t *ch, uchar *text, uchar *buf, int len)
478 char tmpbuf[TMPBUFLEN];
482 /* Leave if sniff not open */
483 if (!(ch->ch_sniff_flags & SNIFF_OPEN))
486 do_gettimeofday(&tv);
488 /* Create our header for data dump */
489 p += sprintf(p, "<%ld %ld><%s><", tv.tv_sec, tv.tv_usec, text);
490 tmpbuflen = p - tmpbuf;
495 for (i = 0; i < len && tmpbuflen < (TMPBUFLEN - 4); i++) {
496 p += sprintf(p, "%02x ", *buf);
498 tmpbuflen = p - tmpbuf;
501 if (tmpbuflen < (TMPBUFLEN - 4)) {
503 p += sprintf(p - 1, "%s\n", ">");
505 p += sprintf(p, "%s\n", ">");
511 nbuf = strlen(tmpbuf);
515 * Loop while data remains.
517 while (nbuf > 0 && ch->ch_sniff_buf) {
519 * Determine the amount of available space left in the
520 * buffer. If there's none, wait until some appears.
522 n = (ch->ch_sniff_out - ch->ch_sniff_in - 1) & SNIFF_MASK;
525 * If there is no space left to write to in our sniff buffer,
526 * we have no choice but to drop the data.
527 * We *cannot* sleep here waiting for space, because this
528 * function was probably called by the interrupt/timer routines!
534 * Copy as much data as will fit.
540 r = SNIFF_MAX - ch->ch_sniff_in;
543 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, r);
551 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, n);
553 ch->ch_sniff_in += n;
558 * Wakeup any thread waiting for data
560 if (ch->ch_sniff_flags & SNIFF_WAIT_DATA) {
561 ch->ch_sniff_flags &= ~SNIFF_WAIT_DATA;
562 wake_up_interruptible(&ch->ch_sniff_wait);
567 * If the user sent us too much data to push into our tmpbuf,
568 * we need to keep looping around on all the data.
575 } while (too_much_data);
579 /*=======================================================================
581 * dgnc_wmove - Write data to transmit queue.
583 * ch - Pointer to channel structure.
584 * buf - Poiter to characters to be moved.
585 * n - Number of characters to move.
587 *=======================================================================*/
588 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
593 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
596 head = ch->ch_w_head & WQUEUEMASK;
599 * If the write wraps over the top of the circular buffer,
600 * move the portion up to the wrap point, and reset the
601 * pointers to the bottom.
603 remain = WQUEUESIZE - head;
607 memcpy(ch->ch_wqueue + head, buf, remain);
617 memcpy(ch->ch_wqueue + head, buf, remain);
622 ch->ch_w_head = head;
628 /*=======================================================================
630 * dgnc_input - Process received data.
632 * ch - Pointer to channel structure.
634 *=======================================================================*/
635 void dgnc_input(struct channel_t *ch)
637 struct dgnc_board *bd;
638 struct tty_struct *tp;
639 struct tty_ldisc *ld;
651 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
654 tp = ch->ch_tun.un_tty;
657 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
660 DGNC_LOCK(ch->ch_lock, lock_flags);
663 * Figure the number of characters in the buffer.
664 * Exit immediately if none.
667 head = ch->ch_r_head & rmask;
668 tail = ch->ch_r_tail & rmask;
669 data_len = (head - tail) & rmask;
672 DGNC_UNLOCK(ch->ch_lock, lock_flags);
676 DPR_READ(("dgnc_input start\n"));
679 * If the device is not open, or CREAD is off,
680 * flush input data and return immediately.
682 if (!tp || (tp->magic != TTY_MAGIC) || !(ch->ch_tun.un_flags & UN_ISOPEN) ||
683 !(tp->termios.c_cflag & CREAD) || (ch->ch_tun.un_flags & UN_CLOSING)) {
685 DPR_READ(("input. dropping %d bytes on port %d...\n", data_len, ch->ch_portnum));
686 DPR_READ(("input. tp: %p tp->magic: %x MAGIC:%x ch flags: %x\n",
687 tp, tp ? tp->magic : 0, TTY_MAGIC, ch->ch_tun.un_flags));
689 ch->ch_r_head = tail;
691 /* Force queue flow control to be released, if needed */
692 dgnc_check_queue_flow_control(ch);
694 DGNC_UNLOCK(ch->ch_lock, lock_flags);
699 * If we are throttled, simply don't read any data.
701 if (ch->ch_flags & CH_FORCED_STOPI) {
702 DGNC_UNLOCK(ch->ch_lock, lock_flags);
703 DPR_READ(("Port %d throttled, not reading any data. head: %x tail: %x\n",
704 ch->ch_portnum, head, tail));
708 DPR_READ(("dgnc_input start 2\n"));
710 flip_len = TTY_FLIPBUF_SIZE;
712 /* Chop down the length, if needed */
713 len = min(data_len, flip_len);
714 len = min(len, (N_TTY_BUF_SIZE - 1));
716 ld = tty_ldisc_ref(tp);
720 * If the DONT_FLIP flag is on, don't flush our buffer, and act
721 * like the ld doesn't have any space to put the data right now.
723 if (test_bit(TTY_DONT_FLIP, &tp->flags))
728 * If we were unable to get a reference to the ld,
729 * don't flush our buffer, and act like the ld doesn't
730 * have any space to put the data right now.
736 * If ld doesn't have a pointer to a receive_buf function,
737 * flush the data, then act like the ld doesn't have any
738 * space to put the data right now.
740 if (!ld->ops->receive_buf) {
741 ch->ch_r_head = ch->ch_r_tail;
747 DGNC_UNLOCK(ch->ch_lock, lock_flags);
754 * The tty layer in the kernel has changed in 2.6.16+.
756 * The flip buffers in the tty structure are no longer exposed,
757 * and probably will be going away eventually.
759 * If we are completely raw, we don't need to go through a lot
760 * of the tty layers that exist.
761 * In this case, we take the shortest and fastest route we
762 * can to relay the data to the user.
764 * On the other hand, if we are not raw, we need to go through
765 * the new 2.6.16+ tty layer, which has its API more well defined.
767 len = tty_buffer_request_room(tp->port, len);
771 * n now contains the most amount of data we can copy,
772 * bounded either by how much the Linux tty layer can handle,
773 * or the amount of data the card actually has pending...
776 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
783 * If conditions are such that ld needs to see all
784 * UART errors, we will have to walk each character
785 * and error byte and send them to the buffer one at
788 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
789 for (i = 0; i < s; i++) {
790 if (*(ch->ch_equeue + tail + i) & UART_LSR_BI)
791 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_BREAK);
792 else if (*(ch->ch_equeue + tail + i) & UART_LSR_PE)
793 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_PARITY);
794 else if (*(ch->ch_equeue + tail + i) & UART_LSR_FE)
795 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_FRAME);
797 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_NORMAL);
800 tty_insert_flip_string(tp->port, ch->ch_rqueue + tail, s);
803 dgnc_sniff_nowait_nolock(ch, "USER READ", ch->ch_rqueue + tail, s);
807 /* Flip queue if needed */
811 ch->ch_r_tail = tail & rmask;
812 ch->ch_e_tail = tail & rmask;
813 dgnc_check_queue_flow_control(ch);
814 DGNC_UNLOCK(ch->ch_lock, lock_flags);
816 /* Tell the tty layer its okay to "eat" the data now */
817 tty_flip_buffer_push(tp->port);
822 DPR_READ(("dgnc_input - finish\n"));
826 /************************************************************************
827 * Determines when CARRIER changes state and takes appropriate
829 ************************************************************************/
830 void dgnc_carrier(struct channel_t *ch)
832 struct dgnc_board *bd;
834 int virt_carrier = 0;
835 int phys_carrier = 0;
837 DPR_CARR(("dgnc_carrier called...\n"));
839 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
844 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
847 if (ch->ch_mistat & UART_MSR_DCD) {
848 DPR_CARR(("mistat: %x D_CD: %x\n", ch->ch_mistat, ch->ch_mistat & UART_MSR_DCD));
852 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
855 if (ch->ch_c_cflag & CLOCAL)
858 DPR_CARR(("DCD: physical: %d virt: %d\n", phys_carrier, virt_carrier));
861 * Test for a VIRTUAL carrier transition to HIGH.
863 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
866 * When carrier rises, wake any threads waiting
867 * for carrier in the open routine.
870 DPR_CARR(("carrier: virt DCD rose\n"));
872 if (waitqueue_active(&(ch->ch_flags_wait)))
873 wake_up_interruptible(&ch->ch_flags_wait);
877 * Test for a PHYSICAL carrier transition to HIGH.
879 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
882 * When carrier rises, wake any threads waiting
883 * for carrier in the open routine.
886 DPR_CARR(("carrier: physical DCD rose\n"));
888 if (waitqueue_active(&(ch->ch_flags_wait)))
889 wake_up_interruptible(&ch->ch_flags_wait);
893 * Test for a PHYSICAL transition to low, so long as we aren't
894 * currently ignoring physical transitions (which is what "virtual
895 * carrier" indicates).
897 * The transition of the virtual carrier to low really doesn't
898 * matter... it really only means "ignore carrier state", not
899 * "make pretend that carrier is there".
901 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
906 * When carrier drops:
908 * Drop carrier on all open units.
910 * Flush queues, waking up any task waiting in the
913 * Send a hangup to the control terminal.
915 * Enable all select calls.
917 if (waitqueue_active(&(ch->ch_flags_wait)))
918 wake_up_interruptible(&ch->ch_flags_wait);
920 if (ch->ch_tun.un_open_count > 0) {
921 DPR_CARR(("Sending tty hangup\n"));
922 tty_hangup(ch->ch_tun.un_tty);
925 if (ch->ch_pun.un_open_count > 0) {
926 DPR_CARR(("Sending pr hangup\n"));
927 tty_hangup(ch->ch_pun.un_tty);
932 * Make sure that our cached values reflect the current reality.
934 if (virt_carrier == 1)
935 ch->ch_flags |= CH_FCAR;
937 ch->ch_flags &= ~CH_FCAR;
939 if (phys_carrier == 1)
940 ch->ch_flags |= CH_CD;
942 ch->ch_flags &= ~CH_CD;
946 * Assign the custom baud rate to the channel structure
948 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
957 ch->ch_custom_speed = 0;
962 * Since the divisor is stored in a 16-bit integer, we make sure
963 * we don't allow any rates smaller than a 16-bit integer would allow.
964 * And of course, rates above the dividend won't fly.
966 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
967 newrate = ((ch->ch_bd->bd_dividend / 0xFFFF) + 1);
969 if (newrate && newrate > ch->ch_bd->bd_dividend)
970 newrate = ch->ch_bd->bd_dividend;
973 testdiv = ch->ch_bd->bd_dividend / newrate;
976 * If we try to figure out what rate the board would use
977 * with the test divisor, it will be either equal or higher
978 * than the requested baud rate. If we then determine the
979 * rate with a divisor one higher, we will get the next lower
980 * supported rate below the requested.
982 testrate_high = ch->ch_bd->bd_dividend / testdiv;
983 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
986 * If the rate for the requested divisor is correct, just
987 * use it and be done.
989 if (testrate_high != newrate) {
991 * Otherwise, pick the rate that is closer (i.e. whichever rate
992 * has a smaller delta).
994 deltahigh = testrate_high - newrate;
995 deltalow = newrate - testrate_low;
997 if (deltahigh < deltalow)
998 newrate = testrate_high;
1000 newrate = testrate_low;
1004 ch->ch_custom_speed = newrate;
1008 void dgnc_check_queue_flow_control(struct channel_t *ch)
1012 /* Store how much space we have left in the queue */
1013 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
1015 qleft += RQUEUEMASK + 1;
1018 * Check to see if we should enforce flow control on our queue because
1019 * the ld (or user) isn't reading data out of our queue fast enuf.
1021 * NOTE: This is done based on what the current flow control of the
1024 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
1025 * This will cause the UART's FIFO to back up, and force
1026 * the RTS signal to be dropped.
1027 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
1028 * the other side, in hopes it will stop sending data to us.
1029 * 3) NONE - Nothing we can do. We will simply drop any extra data
1030 * that gets sent into us when the queue fills up.
1034 if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
1035 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
1036 ch->ch_bd->bd_ops->disable_receiver(ch);
1037 ch->ch_flags |= (CH_RECEIVER_OFF);
1038 DPR_READ(("Internal queue hit hilevel mark (%d)! Turning off interrupts.\n",
1043 else if (ch->ch_c_iflag & IXOFF) {
1044 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
1045 ch->ch_bd->bd_ops->send_stop_character(ch);
1046 ch->ch_stops_sent++;
1047 DPR_READ(("Sending stop char! Times sent: %x\n", ch->ch_stops_sent));
1052 /* Empty... Can't do anything about the impending overflow... */
1057 * Check to see if we should unenforce flow control because
1058 * ld (or user) finally read enuf data out of our queue.
1060 * NOTE: This is done based on what the current flow control of the
1063 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
1064 * This will cause the UART's FIFO to raise RTS back up,
1065 * which will allow the other side to start sending data again.
1066 * 2) SWFLOW (IXOFF) - Send a start character to
1067 * the other side, so it will start sending data to us again.
1068 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
1069 * other side, we don't need to do anything now.
1071 if (qleft > (RQUEUESIZE / 2)) {
1073 if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
1074 if (ch->ch_flags & CH_RECEIVER_OFF) {
1075 ch->ch_bd->bd_ops->enable_receiver(ch);
1076 ch->ch_flags &= ~(CH_RECEIVER_OFF);
1077 DPR_READ(("Internal queue hit lowlevel mark (%d)! Turning on interrupts.\n",
1082 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
1083 ch->ch_stops_sent = 0;
1084 ch->ch_bd->bd_ops->send_start_character(ch);
1085 DPR_READ(("Sending start char!\n"));
1089 /* Nothing needed. */
1095 void dgnc_wakeup_writes(struct channel_t *ch)
1100 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1103 DGNC_LOCK(ch->ch_lock, lock_flags);
1106 * If channel now has space, wake up anyone waiting on the condition.
1108 qlen = ch->ch_w_head - ch->ch_w_tail;
1112 if (qlen >= (WQUEUESIZE - 256)) {
1113 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1117 if (ch->ch_tun.un_flags & UN_ISOPEN) {
1118 if ((ch->ch_tun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1119 ch->ch_tun.un_tty->ldisc->ops->write_wakeup)
1121 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1122 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
1123 DGNC_LOCK(ch->ch_lock, lock_flags);
1126 wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
1129 * If unit is set to wait until empty, check to make sure
1130 * the queue AND FIFO are both empty.
1132 if (ch->ch_tun.un_flags & UN_EMPTY) {
1133 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
1134 ch->ch_tun.un_flags &= ~(UN_EMPTY);
1137 * If RTS Toggle mode is on, whenever
1138 * the queue and UART is empty, keep RTS low.
1140 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1141 ch->ch_mostat &= ~(UART_MCR_RTS);
1142 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1146 * If DTR Toggle mode is on, whenever
1147 * the queue and UART is empty, keep DTR low.
1149 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1150 ch->ch_mostat &= ~(UART_MCR_DTR);
1151 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1156 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
1159 if (ch->ch_pun.un_flags & UN_ISOPEN) {
1160 if ((ch->ch_pun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1161 ch->ch_pun.un_tty->ldisc->ops->write_wakeup)
1163 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1164 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
1165 DGNC_LOCK(ch->ch_lock, lock_flags);
1168 wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
1171 * If unit is set to wait until empty, check to make sure
1172 * the queue AND FIFO are both empty.
1174 if (ch->ch_pun.un_flags & UN_EMPTY) {
1175 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
1176 ch->ch_pun.un_flags &= ~(UN_EMPTY);
1180 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1183 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1188 /************************************************************************
1190 * TTY Entry points and helper functions
1192 ************************************************************************/
1198 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
1200 struct dgnc_board *brd;
1201 struct channel_t *ch;
1210 major = MAJOR(tty_devnum(tty));
1211 minor = MINOR(tty_devnum(tty));
1216 /* Get board pointer from our array of majors we have allocated */
1217 brd = dgnc_BoardsByMajor[major];
1222 * If board is not yet up to a state of READY, go to
1223 * sleep waiting for it to happen or they cancel the open.
1225 rc = wait_event_interruptible(brd->state_wait,
1226 (brd->state & BOARD_READY));
1231 DGNC_LOCK(brd->bd_lock, lock_flags);
1233 /* If opened device is greater than our number of ports, bail. */
1234 if (PORT_NUM(minor) > brd->nasync) {
1235 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1239 ch = brd->channels[PORT_NUM(minor)];
1241 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1245 /* Drop board lock */
1246 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1248 /* Grab channel lock */
1249 DGNC_LOCK(ch->ch_lock, lock_flags);
1251 /* Figure out our type */
1252 if (!IS_PRINT(minor)) {
1253 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1254 un->un_type = DGNC_SERIAL;
1255 } else if (IS_PRINT(minor)) {
1256 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1257 un->un_type = DGNC_PRINT;
1259 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1260 DPR_OPEN(("%d Unknown TYPE!\n", __LINE__));
1265 * If the port is still in a previous open, and in a state
1266 * where we simply cannot safely keep going, wait until the
1269 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1271 rc = wait_event_interruptible(ch->ch_flags_wait, ((ch->ch_flags & CH_OPENING) == 0));
1273 /* If ret is non-zero, user ctrl-c'ed us */
1275 DPR_OPEN(("%d User ctrl c'ed\n", __LINE__));
1280 * If either unit is in the middle of the fragile part of close,
1281 * we just cannot touch the channel safely.
1282 * Go to sleep, knowing that when the channel can be
1283 * touched safely, the close routine will signal the
1284 * ch_flags_wait to wake us back up.
1286 rc = wait_event_interruptible(ch->ch_flags_wait,
1287 (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING) == 0));
1289 /* If ret is non-zero, user ctrl-c'ed us */
1291 DPR_OPEN(("%d User ctrl c'ed\n", __LINE__));
1295 DGNC_LOCK(ch->ch_lock, lock_flags);
1298 /* Store our unit into driver_data, so we always have it available. */
1299 tty->driver_data = un;
1301 DPR_OPEN(("Open called. MAJOR: %d MINOR:%d PORT_NUM: %x unit: %p NAME: %s\n",
1302 MAJOR(tty_devnum(tty)), MINOR(tty_devnum(tty)), PORT_NUM(minor), un, brd->name));
1304 DPR_OPEN(("%d: tflag=%x pflag=%x\n", __LINE__, ch->ch_tun.un_flags, ch->ch_pun.un_flags));
1309 if (!(un->un_flags & UN_ISOPEN)) {
1310 /* Store important variables. */
1313 /* Maybe do something here to the TTY struct as well? */
1318 * Allocate channel buffers for read/write/error.
1319 * Set flag, so we don't get trounced on.
1321 ch->ch_flags |= (CH_OPENING);
1323 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1324 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1327 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1329 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1331 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1333 DGNC_LOCK(ch->ch_lock, lock_flags);
1335 ch->ch_flags &= ~(CH_OPENING);
1336 wake_up_interruptible(&ch->ch_flags_wait);
1339 * Initialize if neither terminal or printer is open.
1341 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1343 DPR_OPEN(("dgnc_open: initializing channel in open...\n"));
1346 * Flush input queues.
1348 ch->ch_r_head = ch->ch_r_tail = 0;
1349 ch->ch_e_head = ch->ch_e_tail = 0;
1350 ch->ch_w_head = ch->ch_w_tail = 0;
1352 brd->bd_ops->flush_uart_write(ch);
1353 brd->bd_ops->flush_uart_read(ch);
1356 ch->ch_cached_lsr = 0;
1357 ch->ch_stop_sending_break = 0;
1358 ch->ch_stops_sent = 0;
1360 ch->ch_c_cflag = tty->termios.c_cflag;
1361 ch->ch_c_iflag = tty->termios.c_iflag;
1362 ch->ch_c_oflag = tty->termios.c_oflag;
1363 ch->ch_c_lflag = tty->termios.c_lflag;
1364 ch->ch_startc = tty->termios.c_cc[VSTART];
1365 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1368 * Bring up RTS and DTR...
1369 * Also handle RTS or DTR toggle if set.
1371 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1372 ch->ch_mostat |= (UART_MCR_RTS);
1373 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1374 ch->ch_mostat |= (UART_MCR_DTR);
1376 /* Tell UART to init itself */
1377 brd->bd_ops->uart_init(ch);
1381 * Run param in case we changed anything
1383 brd->bd_ops->param(tty);
1388 * follow protocol for opening port
1391 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1393 rc = dgnc_block_til_ready(tty, file, ch);
1396 DPR_OPEN(("dgnc_tty_open returning after dgnc_block_til_ready "
1399 /* No going back now, increment our unit and channel counters */
1400 DGNC_LOCK(ch->ch_lock, lock_flags);
1401 ch->ch_open_count++;
1402 un->un_open_count++;
1403 un->un_flags |= (UN_ISOPEN);
1404 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1406 DPR_OPEN(("dgnc_tty_open finished\n"));
1412 * dgnc_block_til_ready()
1414 * Wait for DCD, if needed.
1416 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch)
1419 struct un_t *un = NULL;
1422 int sleep_on_un_flags = 0;
1424 if (!tty || tty->magic != TTY_MAGIC || !file || !ch || ch->magic != DGNC_CHANNEL_MAGIC) {
1428 un = tty->driver_data;
1429 if (!un || un->magic != DGNC_UNIT_MAGIC)
1432 DPR_OPEN(("dgnc_block_til_ready - before block.\n"));
1434 DGNC_LOCK(ch->ch_lock, lock_flags);
1441 sleep_on_un_flags = 0;
1444 * If board has failed somehow during our sleep, bail with error.
1446 if (ch->ch_bd->state == BOARD_FAILED) {
1451 /* If tty was hung up, break out of loop and set error. */
1452 if (tty_hung_up_p(file)) {
1458 * If either unit is in the middle of the fragile part of close,
1459 * we just cannot touch the channel safely.
1460 * Go back to sleep, knowing that when the channel can be
1461 * touched safely, the close routine will signal the
1462 * ch_wait_flags to wake us back up.
1464 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING)) {
1467 * Our conditions to leave cleanly and happily:
1468 * 1) NONBLOCKING on the tty is set.
1470 * 3) DCD (fake or real) is active.
1473 if (file->f_flags & O_NONBLOCK)
1476 if (tty->flags & (1 << TTY_IO_ERROR)) {
1481 if (ch->ch_flags & CH_CD) {
1482 DPR_OPEN(("%d: ch_flags: %x\n", __LINE__, ch->ch_flags));
1486 if (ch->ch_flags & CH_FCAR) {
1487 DPR_OPEN(("%d: ch_flags: %x\n", __LINE__, ch->ch_flags));
1491 sleep_on_un_flags = 1;
1495 * If there is a signal pending, the user probably
1496 * interrupted (ctrl-c) us.
1497 * Leave loop with error set.
1499 if (signal_pending(current)) {
1500 DPR_OPEN(("%d: signal pending...\n", __LINE__));
1501 retval = -ERESTARTSYS;
1505 DPR_OPEN(("dgnc_block_til_ready - blocking.\n"));
1508 * Store the flags before we let go of channel lock
1510 if (sleep_on_un_flags)
1511 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1513 old_flags = ch->ch_flags;
1516 * Let go of channel lock before calling schedule.
1517 * Our poller will get any FEP events and wake us up when DCD
1518 * eventually goes active.
1521 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1523 DPR_OPEN(("Going to sleep on %s flags...\n",
1524 (sleep_on_un_flags ? "un" : "ch")));
1527 * Wait for something in the flags to change from the current value.
1529 if (sleep_on_un_flags)
1530 retval = wait_event_interruptible(un->un_flags_wait,
1531 (old_flags != (ch->ch_tun.un_flags | ch->ch_pun.un_flags)));
1533 retval = wait_event_interruptible(ch->ch_flags_wait,
1534 (old_flags != ch->ch_flags));
1536 DPR_OPEN(("After sleep... retval: %x\n", retval));
1539 * We got woken up for some reason.
1540 * Before looping around, grab our channel lock.
1542 DGNC_LOCK(ch->ch_lock, lock_flags);
1547 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1549 DPR_OPEN(("dgnc_block_til_ready - after blocking.\n"));
1552 DPR_OPEN(("dgnc_block_til_ready - done. error. retval: %x\n", retval));
1556 DPR_OPEN(("dgnc_block_til_ready - done no error. jiffies: %lu\n", jiffies));
1565 * Hangup the port. Like a close, but don't wait for output to drain.
1567 static void dgnc_tty_hangup(struct tty_struct *tty)
1571 if (!tty || tty->magic != TTY_MAGIC)
1574 un = tty->driver_data;
1575 if (!un || un->magic != DGNC_UNIT_MAGIC)
1578 DPR_CLOSE(("dgnc_hangup called. ch->ch_open_count: %d un->un_open_count: %d\n",
1579 un->un_ch->ch_open_count, un->un_open_count));
1581 /* flush the transmit queues */
1582 dgnc_tty_flush_buffer(tty);
1584 DPR_CLOSE(("dgnc_hangup finished. ch->ch_open_count: %d un->un_open_count: %d\n",
1585 un->un_ch->ch_open_count, un->un_open_count));
1593 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1595 struct ktermios *ts;
1596 struct dgnc_board *bd;
1597 struct channel_t *ch;
1602 if (!tty || tty->magic != TTY_MAGIC)
1605 un = tty->driver_data;
1606 if (!un || un->magic != DGNC_UNIT_MAGIC)
1610 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1614 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1619 DPR_CLOSE(("Close called\n"));
1621 DGNC_LOCK(ch->ch_lock, lock_flags);
1624 * Determine if this is the last close or not - and if we agree about
1625 * which type of close it is with the Line Discipline
1627 if ((tty->count == 1) && (un->un_open_count != 1)) {
1629 * Uh, oh. tty->count is 1, which means that the tty
1630 * structure will be freed. un_open_count should always
1631 * be one in these conditions. If it's greater than
1632 * one, we've got real problems, since it means the
1633 * serial port won't be shutdown.
1635 APR(("tty->count is 1, un open count is %d\n", un->un_open_count));
1636 un->un_open_count = 1;
1639 if (--un->un_open_count < 0) {
1640 APR(("bad serial port open count of %d\n", un->un_open_count));
1641 un->un_open_count = 0;
1644 ch->ch_open_count--;
1646 if (ch->ch_open_count && un->un_open_count) {
1647 DPR_CLOSE(("dgnc_tty_close: not last close ch: %d un:%d\n",
1648 ch->ch_open_count, un->un_open_count));
1650 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1654 /* OK, its the last close on the unit */
1655 DPR_CLOSE(("dgnc_tty_close - last close on unit procedures\n"));
1657 un->un_flags |= UN_CLOSING;
1663 * Only officially close channel if count is 0 and
1664 * DIGI_PRINTER bit is not set.
1666 if ((ch->ch_open_count == 0) && !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1668 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1671 * turn off print device when closing print device.
1673 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1674 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1675 (int) ch->ch_digi.digi_offlen);
1676 ch->ch_flags &= ~CH_PRON;
1679 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1680 /* wait for output to drain */
1681 /* This will also return if we take an interrupt */
1683 DPR_CLOSE(("Calling wait_for_drain\n"));
1684 rc = bd->bd_ops->drain(tty, 0);
1686 DPR_CLOSE(("After calling wait_for_drain\n"));
1689 DPR_BASIC(("dgnc_tty_close - bad return: %d ", rc));
1691 dgnc_tty_flush_buffer(tty);
1692 tty_ldisc_flush(tty);
1694 DGNC_LOCK(ch->ch_lock, lock_flags);
1699 * If we have HUPCL set, lower DTR and RTS
1701 if (ch->ch_c_cflag & HUPCL) {
1702 DPR_CLOSE(("Close. HUPCL set, dropping DTR/RTS\n"));
1705 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1706 bd->bd_ops->assert_modem_signals(ch);
1709 * Go to sleep to ensure RTS/DTR
1710 * have been dropped for modems to see it.
1712 if (ch->ch_close_delay) {
1713 DPR_CLOSE(("Close. Sleeping for RTS/DTR drop\n"));
1715 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1716 dgnc_ms_sleep(ch->ch_close_delay);
1717 DGNC_LOCK(ch->ch_lock, lock_flags);
1719 DPR_CLOSE(("Close. After sleeping for RTS/DTR drop\n"));
1723 ch->ch_old_baud = 0;
1725 /* Turn off UART interrupts for this port */
1726 ch->ch_bd->bd_ops->uart_off(ch);
1729 * turn off print device when closing print device.
1731 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1732 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1733 (int) ch->ch_digi.digi_offlen);
1734 ch->ch_flags &= ~CH_PRON;
1739 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1741 DPR_CLOSE(("Close. Doing wakeups\n"));
1742 wake_up_interruptible(&ch->ch_flags_wait);
1743 wake_up_interruptible(&un->un_flags_wait);
1745 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1747 DPR_BASIC(("dgnc_tty_close - complete\n"));
1752 * dgnc_tty_chars_in_buffer()
1754 * Return number of characters that have not been transmitted yet.
1756 * This routine is used by the line discipline to determine if there
1757 * is data waiting to be transmitted/drained/flushed or not.
1759 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1761 struct channel_t *ch = NULL;
1762 struct un_t *un = NULL;
1767 ulong lock_flags = 0;
1772 un = tty->driver_data;
1773 if (!un || un->magic != DGNC_UNIT_MAGIC)
1777 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1780 DGNC_LOCK(ch->ch_lock, lock_flags);
1783 thead = ch->ch_w_head & tmask;
1784 ttail = ch->ch_w_tail & tmask;
1786 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1788 if (ttail == thead) {
1792 chars = thead - ttail;
1794 chars = thead - ttail + WQUEUESIZE;
1797 DPR_WRITE(("dgnc_tty_chars_in_buffer. Port: %x - %d (head: %d tail: %d)\n",
1798 ch->ch_portnum, chars, thead, ttail));
1807 * Reduces bytes_available to the max number of characters
1808 * that can be sent currently given the maxcps value, and
1809 * returns the new bytes_available. This only affects printer
1812 static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1814 struct channel_t *ch = NULL;
1815 struct un_t *un = NULL;
1818 return bytes_available;
1820 un = tty->driver_data;
1821 if (!un || un->magic != DGNC_UNIT_MAGIC)
1822 return bytes_available;
1825 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1826 return bytes_available;
1829 * If its not the Transparent print device, return
1830 * the full data amount.
1832 if (un->un_type != DGNC_PRINT)
1833 return bytes_available;
1835 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1837 unsigned long current_time = jiffies;
1838 unsigned long buffer_time = current_time +
1839 (HZ * ch->ch_digi.digi_bufsize) / ch->ch_digi.digi_maxcps;
1841 if (ch->ch_cpstime < current_time) {
1842 /* buffer is empty */
1843 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1844 cps_limit = ch->ch_digi.digi_bufsize;
1845 } else if (ch->ch_cpstime < buffer_time) {
1846 /* still room in the buffer */
1847 cps_limit = ((buffer_time - ch->ch_cpstime) * ch->ch_digi.digi_maxcps) / HZ;
1849 /* no room in the buffer */
1853 bytes_available = min(cps_limit, bytes_available);
1856 return bytes_available;
1861 * dgnc_tty_write_room()
1863 * Return space available in Tx buffer
1865 static int dgnc_tty_write_room(struct tty_struct *tty)
1867 struct channel_t *ch = NULL;
1868 struct un_t *un = NULL;
1873 ulong lock_flags = 0;
1875 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1878 un = tty->driver_data;
1879 if (!un || un->magic != DGNC_UNIT_MAGIC)
1883 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1886 DGNC_LOCK(ch->ch_lock, lock_flags);
1889 head = (ch->ch_w_head) & tmask;
1890 tail = (ch->ch_w_tail) & tmask;
1892 ret = tail - head - 1;
1896 /* Limit printer to maxcps */
1897 ret = dgnc_maxcps_room(tty, ret);
1900 * If we are printer device, leave space for
1901 * possibly both the on and off strings.
1903 if (un->un_type == DGNC_PRINT) {
1904 if (!(ch->ch_flags & CH_PRON))
1905 ret -= ch->ch_digi.digi_onlen;
1906 ret -= ch->ch_digi.digi_offlen;
1908 if (ch->ch_flags & CH_PRON)
1909 ret -= ch->ch_digi.digi_offlen;
1915 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1917 DPR_WRITE(("dgnc_tty_write_room - %d tail: %d head: %d\n", ret, tail, head));
1924 * dgnc_tty_put_char()
1926 * Put a character into ch->ch_buf
1928 * - used by the line discipline for OPOST processing
1930 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1933 * Simply call tty_write.
1935 DPR_WRITE(("dgnc_tty_put_char called\n"));
1936 dgnc_tty_write(tty, &c, 1);
1944 * Take data from the user or kernel and send it out to the FEP.
1945 * In here exists all the Transparent Print magic as well.
1947 static int dgnc_tty_write(struct tty_struct *tty,
1948 const unsigned char *buf, int count)
1950 struct channel_t *ch = NULL;
1951 struct un_t *un = NULL;
1952 int bufcount = 0, n = 0;
1961 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1964 un = tty->driver_data;
1965 if (!un || un->magic != DGNC_UNIT_MAGIC)
1969 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1975 DPR_WRITE(("dgnc_tty_write: Port: %x tty=%p user=%d len=%d\n",
1976 ch->ch_portnum, tty, from_user, count));
1979 * Store original amount of characters passed in.
1980 * This helps to figure out if we should ask the FEP
1981 * to send us an event when it has more space available.
1985 DGNC_LOCK(ch->ch_lock, lock_flags);
1987 /* Get our space available for the channel from the board */
1989 head = (ch->ch_w_head) & tmask;
1990 tail = (ch->ch_w_tail) & tmask;
1992 bufcount = tail - head - 1;
1994 bufcount += WQUEUESIZE;
1996 DPR_WRITE(("%d: bufcount: %x count: %x tail: %x head: %x tmask: %x\n",
1997 __LINE__, bufcount, count, tail, head, tmask));
2000 * Limit printer output to maxcps overall, with bursts allowed
2001 * up to bufsize characters.
2003 bufcount = dgnc_maxcps_room(tty, bufcount);
2006 * Take minimum of what the user wants to send, and the
2007 * space available in the FEP buffer.
2009 count = min(count, bufcount);
2012 * Bail if no space left.
2015 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2020 * Output the printer ON string, if we are in terminal mode, but
2021 * need to be in printer mode.
2023 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
2024 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
2025 (int) ch->ch_digi.digi_onlen);
2026 head = (ch->ch_w_head) & tmask;
2027 ch->ch_flags |= CH_PRON;
2031 * On the other hand, output the printer OFF string, if we are
2032 * currently in printer mode, but need to output to the terminal.
2034 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
2035 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
2036 (int) ch->ch_digi.digi_offlen);
2037 head = (ch->ch_w_head) & tmask;
2038 ch->ch_flags &= ~CH_PRON;
2042 * If there is nothing left to copy, or I can't handle any more data, leave.
2045 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2051 count = min(count, WRITEBUFLEN);
2053 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2056 * If data is coming from user space, copy it into a temporary
2057 * buffer so we don't get swapped out while doing the copy to
2060 /* we're allowed to block if it's from_user */
2061 if (down_interruptible(&dgnc_TmpWriteSem))
2065 * copy_from_user() returns the number
2066 * of bytes that could *NOT* be copied.
2068 count -= copy_from_user(dgnc_TmpWriteBuf, (const uchar __user *) buf, count);
2071 up(&dgnc_TmpWriteSem);
2075 DGNC_LOCK(ch->ch_lock, lock_flags);
2077 buf = dgnc_TmpWriteBuf;
2084 * If the write wraps over the top of the circular buffer,
2085 * move the portion up to the wrap point, and reset the
2086 * pointers to the bottom.
2088 remain = WQUEUESIZE - head;
2092 memcpy(ch->ch_wqueue + head, buf, remain);
2093 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
2100 * Move rest of data.
2103 memcpy(ch->ch_wqueue + head, buf, remain);
2104 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
2110 ch->ch_w_head = head;
2115 * If this is the print device, and the
2116 * printer is still on, we need to turn it
2117 * off before going idle.
2119 if (count == orig_count) {
2120 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
2122 ch->ch_w_head = head;
2123 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
2124 (int) ch->ch_digi.digi_offlen);
2125 head = (ch->ch_w_head) & tmask;
2126 ch->ch_flags &= ~CH_PRON;
2131 /* Update printer buffer empty time. */
2132 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
2133 && (ch->ch_digi.digi_bufsize > 0)) {
2134 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
2138 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2139 up(&dgnc_TmpWriteSem);
2141 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2144 DPR_WRITE(("Write finished - Write %d bytes of %d.\n", count, orig_count));
2148 * Channel lock is grabbed and then released
2149 * inside this routine.
2151 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
2159 * Return modem signals to ld.
2162 static int dgnc_tty_tiocmget(struct tty_struct *tty)
2164 struct channel_t *ch;
2170 if (!tty || tty->magic != TTY_MAGIC)
2173 un = tty->driver_data;
2174 if (!un || un->magic != DGNC_UNIT_MAGIC)
2178 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2181 DPR_IOCTL(("dgnc_tty_tiocmget start\n"));
2183 DGNC_LOCK(ch->ch_lock, lock_flags);
2185 mstat = (ch->ch_mostat | ch->ch_mistat);
2187 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2191 if (mstat & UART_MCR_DTR)
2192 result |= TIOCM_DTR;
2193 if (mstat & UART_MCR_RTS)
2194 result |= TIOCM_RTS;
2195 if (mstat & UART_MSR_CTS)
2196 result |= TIOCM_CTS;
2197 if (mstat & UART_MSR_DSR)
2198 result |= TIOCM_DSR;
2199 if (mstat & UART_MSR_RI)
2201 if (mstat & UART_MSR_DCD)
2204 DPR_IOCTL(("dgnc_tty_tiocmget finish\n"));
2211 * dgnc_tty_tiocmset()
2213 * Set modem signals, called by ld.
2216 static int dgnc_tty_tiocmset(struct tty_struct *tty,
2217 unsigned int set, unsigned int clear)
2219 struct dgnc_board *bd;
2220 struct channel_t *ch;
2225 if (!tty || tty->magic != TTY_MAGIC)
2228 un = tty->driver_data;
2229 if (!un || un->magic != DGNC_UNIT_MAGIC)
2233 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2237 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2240 DPR_IOCTL(("dgnc_tty_tiocmset start\n"));
2243 DGNC_LOCK(ch->ch_lock, lock_flags);
2245 if (set & TIOCM_RTS)
2246 ch->ch_mostat |= UART_MCR_RTS;
2248 if (set & TIOCM_DTR)
2249 ch->ch_mostat |= UART_MCR_DTR;
2251 if (clear & TIOCM_RTS)
2252 ch->ch_mostat &= ~(UART_MCR_RTS);
2254 if (clear & TIOCM_DTR)
2255 ch->ch_mostat &= ~(UART_MCR_DTR);
2257 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2259 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2261 DPR_IOCTL(("dgnc_tty_tiocmset finish\n"));
2268 * dgnc_tty_send_break()
2270 * Send a Break, called by ld.
2272 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
2274 struct dgnc_board *bd;
2275 struct channel_t *ch;
2280 if (!tty || tty->magic != TTY_MAGIC)
2283 un = tty->driver_data;
2284 if (!un || un->magic != DGNC_UNIT_MAGIC)
2288 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2292 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2306 DPR_IOCTL(("dgnc_tty_send_break start 1. %lx\n", jiffies));
2308 DGNC_LOCK(ch->ch_lock, lock_flags);
2310 ch->ch_bd->bd_ops->send_break(ch, msec);
2312 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2314 DPR_IOCTL(("dgnc_tty_send_break finish\n"));
2322 * dgnc_tty_wait_until_sent()
2324 * wait until data has been transmitted, called by ld.
2326 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
2328 struct dgnc_board *bd;
2329 struct channel_t *ch;
2333 if (!tty || tty->magic != TTY_MAGIC)
2336 un = tty->driver_data;
2337 if (!un || un->magic != DGNC_UNIT_MAGIC)
2341 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2345 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2348 rc = bd->bd_ops->drain(tty, 0);
2350 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
2360 * send a high priority character, called by ld.
2362 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
2364 struct dgnc_board *bd;
2365 struct channel_t *ch;
2369 if (!tty || tty->magic != TTY_MAGIC)
2372 un = tty->driver_data;
2373 if (!un || un->magic != DGNC_UNIT_MAGIC)
2377 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2381 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2384 DPR_IOCTL(("dgnc_tty_send_xchar start\n"));
2385 printk("dgnc_tty_send_xchar start\n");
2387 DGNC_LOCK(ch->ch_lock, lock_flags);
2388 bd->bd_ops->send_immediate_char(ch, c);
2389 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2391 DPR_IOCTL(("dgnc_tty_send_xchar finish\n"));
2392 printk("dgnc_tty_send_xchar finish\n");
2400 * Return modem signals to ld.
2402 static inline int dgnc_get_mstat(struct channel_t *ch)
2404 unsigned char mstat;
2408 DPR_IOCTL(("dgnc_getmstat start\n"));
2410 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2413 DGNC_LOCK(ch->ch_lock, lock_flags);
2415 mstat = (ch->ch_mostat | ch->ch_mistat);
2417 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2421 if (mstat & UART_MCR_DTR)
2422 result |= TIOCM_DTR;
2423 if (mstat & UART_MCR_RTS)
2424 result |= TIOCM_RTS;
2425 if (mstat & UART_MSR_CTS)
2426 result |= TIOCM_CTS;
2427 if (mstat & UART_MSR_DSR)
2428 result |= TIOCM_DSR;
2429 if (mstat & UART_MSR_RI)
2431 if (mstat & UART_MSR_DCD)
2434 DPR_IOCTL(("dgnc_getmstat finish\n"));
2442 * Return modem signals to ld.
2444 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value)
2449 DPR_IOCTL(("dgnc_get_modem_info start\n"));
2451 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2454 result = dgnc_get_mstat(ch);
2459 rc = put_user(result, value);
2461 DPR_IOCTL(("dgnc_get_modem_info finish\n"));
2467 * dgnc_set_modem_info()
2469 * Set modem signals, called by ld.
2471 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value)
2473 struct dgnc_board *bd;
2474 struct channel_t *ch;
2477 unsigned int arg = 0;
2480 if (!tty || tty->magic != TTY_MAGIC)
2483 un = tty->driver_data;
2484 if (!un || un->magic != DGNC_UNIT_MAGIC)
2488 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2492 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2497 DPR_IOCTL(("dgnc_set_modem_info() start\n"));
2499 ret = get_user(arg, value);
2505 if (arg & TIOCM_RTS)
2506 ch->ch_mostat |= UART_MCR_RTS;
2508 if (arg & TIOCM_DTR)
2509 ch->ch_mostat |= UART_MCR_DTR;
2514 if (arg & TIOCM_RTS)
2515 ch->ch_mostat &= ~(UART_MCR_RTS);
2517 if (arg & TIOCM_DTR)
2518 ch->ch_mostat &= ~(UART_MCR_DTR);
2524 if (arg & TIOCM_RTS)
2525 ch->ch_mostat |= UART_MCR_RTS;
2527 ch->ch_mostat &= ~(UART_MCR_RTS);
2529 if (arg & TIOCM_DTR)
2530 ch->ch_mostat |= UART_MCR_DTR;
2532 ch->ch_mostat &= ~(UART_MCR_DTR);
2540 DGNC_LOCK(ch->ch_lock, lock_flags);
2542 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2544 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2546 DPR_IOCTL(("dgnc_set_modem_info finish\n"));
2553 * dgnc_tty_digigeta()
2555 * Ioctl to get the information for ditty.
2560 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo)
2562 struct channel_t *ch;
2570 if (!tty || tty->magic != TTY_MAGIC)
2573 un = tty->driver_data;
2574 if (!un || un->magic != DGNC_UNIT_MAGIC)
2578 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2581 memset(&tmp, 0, sizeof(tmp));
2583 DGNC_LOCK(ch->ch_lock, lock_flags);
2584 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2585 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2587 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2595 * dgnc_tty_digiseta()
2597 * Ioctl to set the information for ditty.
2602 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info)
2604 struct dgnc_board *bd;
2605 struct channel_t *ch;
2607 struct digi_t new_digi;
2610 DPR_IOCTL(("DIGI_SETA start\n"));
2612 if (!tty || tty->magic != TTY_MAGIC)
2615 un = tty->driver_data;
2616 if (!un || un->magic != DGNC_UNIT_MAGIC)
2620 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2624 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2627 if (copy_from_user(&new_digi, new_info, sizeof(new_digi))) {
2628 DPR_IOCTL(("DIGI_SETA failed copy_from_user\n"));
2632 DGNC_LOCK(ch->ch_lock, lock_flags);
2635 * Handle transistions to and from RTS Toggle.
2637 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2638 ch->ch_mostat &= ~(UART_MCR_RTS);
2639 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2640 ch->ch_mostat |= (UART_MCR_RTS);
2643 * Handle transistions to and from DTR Toggle.
2645 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2646 ch->ch_mostat &= ~(UART_MCR_DTR);
2647 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2648 ch->ch_mostat |= (UART_MCR_DTR);
2650 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2652 if (ch->ch_digi.digi_maxcps < 1)
2653 ch->ch_digi.digi_maxcps = 1;
2655 if (ch->ch_digi.digi_maxcps > 10000)
2656 ch->ch_digi.digi_maxcps = 10000;
2658 if (ch->ch_digi.digi_bufsize < 10)
2659 ch->ch_digi.digi_bufsize = 10;
2661 if (ch->ch_digi.digi_maxchar < 1)
2662 ch->ch_digi.digi_maxchar = 1;
2664 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2665 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2667 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2668 ch->ch_digi.digi_onlen = DIGI_PLEN;
2670 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2671 ch->ch_digi.digi_offlen = DIGI_PLEN;
2673 ch->ch_bd->bd_ops->param(tty);
2675 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2677 DPR_IOCTL(("DIGI_SETA finish\n"));
2684 * dgnc_set_termios()
2686 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2688 struct dgnc_board *bd;
2689 struct channel_t *ch;
2691 unsigned long lock_flags;
2693 if (!tty || tty->magic != TTY_MAGIC)
2696 un = tty->driver_data;
2697 if (!un || un->magic != DGNC_UNIT_MAGIC)
2701 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2705 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2708 DGNC_LOCK(ch->ch_lock, lock_flags);
2710 ch->ch_c_cflag = tty->termios.c_cflag;
2711 ch->ch_c_iflag = tty->termios.c_iflag;
2712 ch->ch_c_oflag = tty->termios.c_oflag;
2713 ch->ch_c_lflag = tty->termios.c_lflag;
2714 ch->ch_startc = tty->termios.c_cc[VSTART];
2715 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2717 ch->ch_bd->bd_ops->param(tty);
2720 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2724 static void dgnc_tty_throttle(struct tty_struct *tty)
2726 struct channel_t *ch;
2728 ulong lock_flags = 0;
2730 if (!tty || tty->magic != TTY_MAGIC)
2733 un = tty->driver_data;
2734 if (!un || un->magic != DGNC_UNIT_MAGIC)
2738 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2741 DPR_IOCTL(("dgnc_tty_throttle start\n"));
2743 DGNC_LOCK(ch->ch_lock, lock_flags);
2745 ch->ch_flags |= (CH_FORCED_STOPI);
2747 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2749 DPR_IOCTL(("dgnc_tty_throttle finish\n"));
2753 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2755 struct channel_t *ch;
2759 if (!tty || tty->magic != TTY_MAGIC)
2762 un = tty->driver_data;
2763 if (!un || un->magic != DGNC_UNIT_MAGIC)
2767 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2770 DPR_IOCTL(("dgnc_tty_unthrottle start\n"));
2772 DGNC_LOCK(ch->ch_lock, lock_flags);
2774 ch->ch_flags &= ~(CH_FORCED_STOPI);
2776 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2778 DPR_IOCTL(("dgnc_tty_unthrottle finish\n"));
2782 static void dgnc_tty_start(struct tty_struct *tty)
2784 struct dgnc_board *bd;
2785 struct channel_t *ch;
2789 if (!tty || tty->magic != TTY_MAGIC)
2792 un = tty->driver_data;
2793 if (!un || un->magic != DGNC_UNIT_MAGIC)
2797 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2801 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2804 DPR_IOCTL(("dgcn_tty_start start\n"));
2806 DGNC_LOCK(ch->ch_lock, lock_flags);
2808 ch->ch_flags &= ~(CH_FORCED_STOP);
2810 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2812 DPR_IOCTL(("dgnc_tty_start finish\n"));
2816 static void dgnc_tty_stop(struct tty_struct *tty)
2818 struct dgnc_board *bd;
2819 struct channel_t *ch;
2823 if (!tty || tty->magic != TTY_MAGIC)
2826 un = tty->driver_data;
2827 if (!un || un->magic != DGNC_UNIT_MAGIC)
2831 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2835 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2838 DPR_IOCTL(("dgnc_tty_stop start\n"));
2840 DGNC_LOCK(ch->ch_lock, lock_flags);
2842 ch->ch_flags |= (CH_FORCED_STOP);
2844 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2846 DPR_IOCTL(("dgnc_tty_stop finish\n"));
2851 * dgnc_tty_flush_chars()
2853 * Flush the cook buffer
2855 * Note to self, and any other poor souls who venture here:
2857 * flush in this case DOES NOT mean dispose of the data.
2858 * instead, it means "stop buffering and send it if you
2859 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2861 * It is also always called in interrupt context - JAR 8-Sept-99
2863 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2865 struct dgnc_board *bd;
2866 struct channel_t *ch;
2870 if (!tty || tty->magic != TTY_MAGIC)
2873 un = tty->driver_data;
2874 if (!un || un->magic != DGNC_UNIT_MAGIC)
2878 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2882 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2885 DPR_IOCTL(("dgnc_tty_flush_chars start\n"));
2887 DGNC_LOCK(ch->ch_lock, lock_flags);
2889 /* Do something maybe here */
2891 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2893 DPR_IOCTL(("dgnc_tty_flush_chars finish\n"));
2899 * dgnc_tty_flush_buffer()
2901 * Flush Tx buffer (make in == out)
2903 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2905 struct channel_t *ch;
2909 if (!tty || tty->magic != TTY_MAGIC)
2912 un = tty->driver_data;
2913 if (!un || un->magic != DGNC_UNIT_MAGIC)
2917 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2920 DPR_IOCTL(("dgnc_tty_flush_buffer on port: %d start\n", ch->ch_portnum));
2922 DGNC_LOCK(ch->ch_lock, lock_flags);
2924 ch->ch_flags &= ~CH_STOP;
2926 /* Flush our write queue */
2927 ch->ch_w_head = ch->ch_w_tail;
2929 /* Flush UARTs transmit FIFO */
2930 ch->ch_bd->bd_ops->flush_uart_write(ch);
2932 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2933 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2934 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2936 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2937 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2938 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2941 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2943 DPR_IOCTL(("dgnc_tty_flush_buffer finish\n"));
2948 /*****************************************************************************
2950 * The IOCTL function and all of its helpers
2952 *****************************************************************************/
2957 * The usual assortment of ioctl's
2959 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2962 struct dgnc_board *bd;
2963 struct channel_t *ch;
2967 void __user *uarg = (void __user *) arg;
2969 if (!tty || tty->magic != TTY_MAGIC)
2972 un = tty->driver_data;
2973 if (!un || un->magic != DGNC_UNIT_MAGIC)
2977 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2981 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2984 DPR_IOCTL(("dgnc_tty_ioctl start on port %d - cmd %s (%x), arg %lx\n",
2985 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
2987 DGNC_LOCK(ch->ch_lock, lock_flags);
2989 if (un->un_open_count <= 0) {
2990 DPR_BASIC(("dgnc_tty_ioctl - unit not open.\n"));
2991 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2997 /* Here are all the standard ioctl's that we MUST implement */
3001 * TCSBRK is SVID version: non-zero arg --> no break
3002 * this behaviour is exploited by tcdrain().
3004 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3005 * between 0.25 and 0.5 seconds so we'll ask for something
3006 * in the middle: 0.375 seconds.
3008 rc = tty_check_change(tty);
3009 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3013 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3016 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3020 DGNC_LOCK(ch->ch_lock, lock_flags);
3022 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP)) {
3023 ch->ch_bd->bd_ops->send_break(ch, 250);
3026 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3028 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3029 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3035 /* support for POSIX tcsendbreak()
3036 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3037 * between 0.25 and 0.5 seconds so we'll ask for something
3038 * in the middle: 0.375 seconds.
3040 rc = tty_check_change(tty);
3041 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3045 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3047 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3051 DGNC_LOCK(ch->ch_lock, lock_flags);
3053 ch->ch_bd->bd_ops->send_break(ch, 250);
3055 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3057 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3058 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3063 rc = tty_check_change(tty);
3064 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3068 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3070 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3074 DGNC_LOCK(ch->ch_lock, lock_flags);
3076 ch->ch_bd->bd_ops->send_break(ch, 250);
3078 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3080 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3081 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3087 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3092 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3094 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
3099 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3100 rc = get_user(arg, (unsigned long __user *) arg);
3104 DGNC_LOCK(ch->ch_lock, lock_flags);
3105 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
3106 ch->ch_bd->bd_ops->param(tty);
3107 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3112 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3113 return dgnc_get_modem_info(ch, uarg);
3118 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3119 return dgnc_set_modem_info(tty, cmd, uarg);
3122 * Here are any additional ioctl's that we want to implement
3127 * The linux tty driver doesn't have a flush
3128 * input routine for the driver, assuming all backed
3129 * up data is in the line disc. buffers. However,
3130 * we all know that's not the case. Here, we
3131 * act on the ioctl, but then lie and say we didn't
3132 * so the line discipline will process the flush
3135 rc = tty_check_change(tty);
3137 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3141 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
3142 ch->ch_r_head = ch->ch_r_tail;
3143 ch->ch_bd->bd_ops->flush_uart_read(ch);
3144 /* Force queue flow control to be released, if needed */
3145 dgnc_check_queue_flow_control(ch);
3148 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
3149 if (!(un->un_type == DGNC_PRINT)) {
3150 ch->ch_w_head = ch->ch_w_tail;
3151 ch->ch_bd->bd_ops->flush_uart_write(ch);
3153 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
3154 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
3155 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
3158 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
3159 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
3160 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
3166 /* pretend we didn't recognize this IOCTL */
3167 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3168 return -ENOIOCTLCMD;
3172 * The linux tty driver doesn't have a flush
3173 * input routine for the driver, assuming all backed
3174 * up data is in the line disc. buffers. However,
3175 * we all know that's not the case. Here, we
3176 * act on the ioctl, but then lie and say we didn't
3177 * so the line discipline will process the flush
3180 if (cmd == TCSETSF) {
3182 ch->ch_flags &= ~CH_STOP;
3183 ch->ch_r_head = ch->ch_r_tail;
3184 ch->ch_bd->bd_ops->flush_uart_read(ch);
3185 /* Force queue flow control to be released, if needed */
3186 dgnc_check_queue_flow_control(ch);
3189 /* now wait for all the output to drain */
3190 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3191 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3193 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d\n", rc));
3197 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3198 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3200 /* pretend we didn't recognize this */
3201 return -ENOIOCTLCMD;
3205 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3206 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3208 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3212 /* pretend we didn't recognize this */
3213 return -ENOIOCTLCMD;
3216 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3217 /* Make the ld do it */
3218 return -ENOIOCTLCMD;
3221 /* get information for ditty */
3222 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3223 return dgnc_tty_digigeta(tty, uarg);
3228 /* set information for ditty */
3229 if (cmd == (DIGI_SETAW)) {
3231 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3232 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3234 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3237 DGNC_LOCK(ch->ch_lock, lock_flags);
3239 tty_ldisc_flush(tty);
3244 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3245 return dgnc_tty_digiseta(tty, uarg);
3250 /* Let go of locks when accessing user space, could sleep */
3251 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3252 rc = get_user(loopback, (unsigned int __user *) arg);
3255 DGNC_LOCK(ch->ch_lock, lock_flags);
3257 /* Enable/disable internal loopback for this port */
3259 ch->ch_flags |= CH_LOOPBACK;
3261 ch->ch_flags &= ~(CH_LOOPBACK);
3263 ch->ch_bd->bd_ops->param(tty);
3264 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3268 case DIGI_GETCUSTOMBAUD:
3269 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3270 rc = put_user(ch->ch_custom_speed, (unsigned int __user *) arg);
3273 case DIGI_SETCUSTOMBAUD:
3276 /* Let go of locks when accessing user space, could sleep */
3277 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3278 rc = get_user(new_rate, (int __user *) arg);
3281 DGNC_LOCK(ch->ch_lock, lock_flags);
3282 dgnc_set_custom_speed(ch, new_rate);
3283 ch->ch_bd->bd_ops->param(tty);
3284 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3289 * This ioctl allows insertion of a character into the front
3290 * of any pending data to be transmitted.
3292 * This ioctl is to satify the "Send Character Immediate"
3293 * call that the RealPort protocol spec requires.
3295 case DIGI_REALPORT_SENDIMMEDIATE:
3298 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3299 rc = get_user(c, (unsigned char __user *) arg);
3302 DGNC_LOCK(ch->ch_lock, lock_flags);
3303 ch->ch_bd->bd_ops->send_immediate_char(ch, c);
3304 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3309 * This ioctl returns all the current counts for the port.
3311 * This ioctl is to satify the "Line Error Counters"
3312 * call that the RealPort protocol spec requires.
3314 case DIGI_REALPORT_GETCOUNTERS:
3316 struct digi_getcounter buf;
3318 buf.norun = ch->ch_err_overrun;
3319 buf.noflow = 0; /* The driver doesn't keep this stat */
3320 buf.nframe = ch->ch_err_frame;
3321 buf.nparity = ch->ch_err_parity;
3322 buf.nbreak = ch->ch_err_break;
3323 buf.rbytes = ch->ch_rxcount;
3324 buf.tbytes = ch->ch_txcount;
3326 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3328 if (copy_to_user(uarg, &buf, sizeof(buf)))
3335 * This ioctl returns all current events.
3337 * This ioctl is to satify the "Event Reporting"
3338 * call that the RealPort protocol spec requires.
3340 case DIGI_REALPORT_GETEVENTS:
3342 unsigned int events = 0;
3344 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
3345 if (ch->ch_flags & CH_BREAK_SENDING)
3347 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP))
3348 events |= (EV_OPU | EV_OPS);
3350 if ((ch->ch_flags & CH_STOPI) || (ch->ch_flags & CH_FORCED_STOPI)) {
3351 events |= (EV_IPU | EV_IPS);
3354 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3355 rc = put_user(events, (unsigned int __user *) arg);
3360 * This ioctl returns TOUT and TIN counters based
3361 * upon the values passed in by the RealPort Server.
3362 * It also passes back whether the UART Transmitter is
3365 case DIGI_REALPORT_GETBUFFERS:
3367 struct digi_getbuffer buf;
3371 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3374 * Get data from user first.
3376 if (copy_from_user(&buf, uarg, sizeof(buf)))
3379 DGNC_LOCK(ch->ch_lock, lock_flags);
3382 * Figure out how much data is in our RX and TX queues.
3384 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
3385 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
3388 * Is the UART empty? Add that value to whats in our TX queue.
3390 count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
3393 * Figure out how much data the RealPort Server believes should
3394 * be in our TX queue.
3396 tdist = (buf.tIn - buf.tOut) & 0xffff;
3399 * If we have more data than the RealPort Server believes we
3400 * should have, reduce our count to its amount.
3402 * This count difference CAN happen because the Linux LD can
3403 * insert more characters into our queue for OPOST processing
3404 * that the RealPort Server doesn't know about.
3406 if (buf.txbuf > tdist)
3410 * Report whether our queue and UART TX are completely empty.
3417 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3419 if (copy_to_user(uarg, &buf, sizeof(buf)))
3425 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3427 DPR_IOCTL(("dgnc_tty_ioctl - in default\n"));
3428 DPR_IOCTL(("dgnc_tty_ioctl end - cmd %s (%x), arg %lx\n",
3429 dgnc_ioctl_name(cmd), cmd, arg));
3431 return -ENOIOCTLCMD;
3434 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3436 DPR_IOCTL(("dgnc_tty_ioctl end - cmd %s (%x), arg %lx\n",
3437 dgnc_ioctl_name(cmd), cmd, arg));