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 if (dgnc_TmpWriteBuf) {
421 kfree(dgnc_TmpWriteBuf);
422 dgnc_TmpWriteBuf = NULL;
430 * Uninitialize the TTY portion of this driver. Free all memory and
433 void dgnc_tty_uninit(struct dgnc_board *brd)
437 if (brd->dgnc_Major_Serial_Registered) {
438 dgnc_BoardsByMajor[brd->SerialDriver.major] = NULL;
439 brd->dgnc_Serial_Major = 0;
440 for (i = 0; i < brd->nasync; i++) {
441 dgnc_remove_tty_sysfs(brd->channels[i]->ch_tun.un_sysfs);
442 tty_unregister_device(&brd->SerialDriver, i);
444 tty_unregister_driver(&brd->SerialDriver);
445 brd->dgnc_Major_Serial_Registered = FALSE;
448 if (brd->dgnc_Major_TransparentPrint_Registered) {
449 dgnc_BoardsByMajor[brd->PrintDriver.major] = NULL;
450 brd->dgnc_TransparentPrint_Major = 0;
451 for (i = 0; i < brd->nasync; i++) {
452 dgnc_remove_tty_sysfs(brd->channels[i]->ch_pun.un_sysfs);
453 tty_unregister_device(&brd->PrintDriver, i);
455 tty_unregister_driver(&brd->PrintDriver);
456 brd->dgnc_Major_TransparentPrint_Registered = FALSE;
459 if (brd->SerialDriver.ttys) {
460 kfree(brd->SerialDriver.ttys);
461 brd->SerialDriver.ttys = NULL;
463 if (brd->PrintDriver.ttys) {
464 kfree(brd->PrintDriver.ttys);
465 brd->PrintDriver.ttys = NULL;
470 #define TMPBUFLEN (1024)
473 * dgnc_sniff - Dump data out to the "sniff" buffer if the
474 * proc sniff file is opened...
476 void dgnc_sniff_nowait_nolock(struct channel_t *ch, uchar *text, uchar *buf, int len)
484 char tmpbuf[TMPBUFLEN];
488 /* Leave if sniff not open */
489 if (!(ch->ch_sniff_flags & SNIFF_OPEN))
492 do_gettimeofday(&tv);
494 /* Create our header for data dump */
495 p += sprintf(p, "<%ld %ld><%s><", tv.tv_sec, tv.tv_usec, text);
496 tmpbuflen = p - tmpbuf;
501 for (i = 0; i < len && tmpbuflen < (TMPBUFLEN - 4); i++) {
502 p += sprintf(p, "%02x ", *buf);
504 tmpbuflen = p - tmpbuf;
507 if (tmpbuflen < (TMPBUFLEN - 4)) {
509 p += sprintf(p - 1, "%s\n", ">");
511 p += sprintf(p, "%s\n", ">");
517 nbuf = strlen(tmpbuf);
521 * Loop while data remains.
523 while (nbuf > 0 && ch->ch_sniff_buf) {
525 * Determine the amount of available space left in the
526 * buffer. If there's none, wait until some appears.
528 n = (ch->ch_sniff_out - ch->ch_sniff_in - 1) & SNIFF_MASK;
531 * If there is no space left to write to in our sniff buffer,
532 * we have no choice but to drop the data.
533 * We *cannot* sleep here waiting for space, because this
534 * function was probably called by the interrupt/timer routines!
540 * Copy as much data as will fit.
546 r = SNIFF_MAX - ch->ch_sniff_in;
549 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, r);
557 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, n);
559 ch->ch_sniff_in += n;
564 * Wakeup any thread waiting for data
566 if (ch->ch_sniff_flags & SNIFF_WAIT_DATA) {
567 ch->ch_sniff_flags &= ~SNIFF_WAIT_DATA;
568 wake_up_interruptible(&ch->ch_sniff_wait);
573 * If the user sent us too much data to push into our tmpbuf,
574 * we need to keep looping around on all the data.
581 } while (too_much_data);
585 /*=======================================================================
587 * dgnc_wmove - Write data to transmit queue.
589 * ch - Pointer to channel structure.
590 * buf - Poiter to characters to be moved.
591 * n - Number of characters to move.
593 *=======================================================================*/
594 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
599 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
602 head = ch->ch_w_head & WQUEUEMASK;
605 * If the write wraps over the top of the circular buffer,
606 * move the portion up to the wrap point, and reset the
607 * pointers to the bottom.
609 remain = WQUEUESIZE - head;
613 memcpy(ch->ch_wqueue + head, buf, remain);
623 memcpy(ch->ch_wqueue + head, buf, remain);
628 ch->ch_w_head = head;
634 /*=======================================================================
636 * dgnc_input - Process received data.
638 * ch - Pointer to channel structure.
640 *=======================================================================*/
641 void dgnc_input(struct channel_t *ch)
643 struct dgnc_board *bd;
644 struct tty_struct *tp;
645 struct tty_ldisc *ld;
657 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
660 tp = ch->ch_tun.un_tty;
663 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
666 DGNC_LOCK(ch->ch_lock, lock_flags);
669 * Figure the number of characters in the buffer.
670 * Exit immediately if none.
673 head = ch->ch_r_head & rmask;
674 tail = ch->ch_r_tail & rmask;
675 data_len = (head - tail) & rmask;
678 DGNC_UNLOCK(ch->ch_lock, lock_flags);
682 DPR_READ(("dgnc_input start\n"));
685 * If the device is not open, or CREAD is off,
686 * flush input data and return immediately.
688 if (!tp || (tp->magic != TTY_MAGIC) || !(ch->ch_tun.un_flags & UN_ISOPEN) ||
689 !(tp->termios.c_cflag & CREAD) || (ch->ch_tun.un_flags & UN_CLOSING)) {
691 DPR_READ(("input. dropping %d bytes on port %d...\n", data_len, ch->ch_portnum));
692 DPR_READ(("input. tp: %p tp->magic: %x MAGIC:%x ch flags: %x\n",
693 tp, tp ? tp->magic : 0, TTY_MAGIC, ch->ch_tun.un_flags));
695 ch->ch_r_head = tail;
697 /* Force queue flow control to be released, if needed */
698 dgnc_check_queue_flow_control(ch);
700 DGNC_UNLOCK(ch->ch_lock, lock_flags);
705 * If we are throttled, simply don't read any data.
707 if (ch->ch_flags & CH_FORCED_STOPI) {
708 DGNC_UNLOCK(ch->ch_lock, lock_flags);
709 DPR_READ(("Port %d throttled, not reading any data. head: %x tail: %x\n",
710 ch->ch_portnum, head, tail));
714 DPR_READ(("dgnc_input start 2\n"));
716 flip_len = TTY_FLIPBUF_SIZE;
718 /* Chop down the length, if needed */
719 len = min(data_len, flip_len);
720 len = min(len, (N_TTY_BUF_SIZE - 1));
722 ld = tty_ldisc_ref(tp);
726 * If the DONT_FLIP flag is on, don't flush our buffer, and act
727 * like the ld doesn't have any space to put the data right now.
729 if (test_bit(TTY_DONT_FLIP, &tp->flags))
734 * If we were unable to get a reference to the ld,
735 * don't flush our buffer, and act like the ld doesn't
736 * have any space to put the data right now.
742 * If ld doesn't have a pointer to a receive_buf function,
743 * flush the data, then act like the ld doesn't have any
744 * space to put the data right now.
746 if (!ld->ops->receive_buf) {
747 ch->ch_r_head = ch->ch_r_tail;
753 DGNC_UNLOCK(ch->ch_lock, lock_flags);
760 * The tty layer in the kernel has changed in 2.6.16+.
762 * The flip buffers in the tty structure are no longer exposed,
763 * and probably will be going away eventually.
765 * If we are completely raw, we don't need to go through a lot
766 * of the tty layers that exist.
767 * In this case, we take the shortest and fastest route we
768 * can to relay the data to the user.
770 * On the other hand, if we are not raw, we need to go through
771 * the new 2.6.16+ tty layer, which has its API more well defined.
773 len = tty_buffer_request_room(tp->port, len);
777 * n now contains the most amount of data we can copy,
778 * bounded either by how much the Linux tty layer can handle,
779 * or the amount of data the card actually has pending...
782 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
789 * If conditions are such that ld needs to see all
790 * UART errors, we will have to walk each character
791 * and error byte and send them to the buffer one at
794 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
795 for (i = 0; i < s; i++) {
796 if (*(ch->ch_equeue + tail + i) & UART_LSR_BI)
797 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_BREAK);
798 else if (*(ch->ch_equeue + tail + i) & UART_LSR_PE)
799 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_PARITY);
800 else if (*(ch->ch_equeue + tail + i) & UART_LSR_FE)
801 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_FRAME);
803 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_NORMAL);
806 tty_insert_flip_string(tp->port, ch->ch_rqueue + tail, s);
809 dgnc_sniff_nowait_nolock(ch, "USER READ", ch->ch_rqueue + tail, s);
813 /* Flip queue if needed */
817 ch->ch_r_tail = tail & rmask;
818 ch->ch_e_tail = tail & rmask;
819 dgnc_check_queue_flow_control(ch);
820 DGNC_UNLOCK(ch->ch_lock, lock_flags);
822 /* Tell the tty layer its okay to "eat" the data now */
823 tty_flip_buffer_push(tp->port);
828 DPR_READ(("dgnc_input - finish\n"));
832 /************************************************************************
833 * Determines when CARRIER changes state and takes appropriate
835 ************************************************************************/
836 void dgnc_carrier(struct channel_t *ch)
838 struct dgnc_board *bd;
840 int virt_carrier = 0;
841 int phys_carrier = 0;
843 DPR_CARR(("dgnc_carrier called...\n"));
845 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
850 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
853 if (ch->ch_mistat & UART_MSR_DCD) {
854 DPR_CARR(("mistat: %x D_CD: %x\n", ch->ch_mistat, ch->ch_mistat & UART_MSR_DCD));
858 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
861 if (ch->ch_c_cflag & CLOCAL)
864 DPR_CARR(("DCD: physical: %d virt: %d\n", phys_carrier, virt_carrier));
867 * Test for a VIRTUAL carrier transition to HIGH.
869 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
872 * When carrier rises, wake any threads waiting
873 * for carrier in the open routine.
876 DPR_CARR(("carrier: virt DCD rose\n"));
878 if (waitqueue_active(&(ch->ch_flags_wait)))
879 wake_up_interruptible(&ch->ch_flags_wait);
883 * Test for a PHYSICAL carrier transition to HIGH.
885 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
888 * When carrier rises, wake any threads waiting
889 * for carrier in the open routine.
892 DPR_CARR(("carrier: physical DCD rose\n"));
894 if (waitqueue_active(&(ch->ch_flags_wait)))
895 wake_up_interruptible(&ch->ch_flags_wait);
899 * Test for a PHYSICAL transition to low, so long as we aren't
900 * currently ignoring physical transitions (which is what "virtual
901 * carrier" indicates).
903 * The transition of the virtual carrier to low really doesn't
904 * matter... it really only means "ignore carrier state", not
905 * "make pretend that carrier is there".
907 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
912 * When carrier drops:
914 * Drop carrier on all open units.
916 * Flush queues, waking up any task waiting in the
919 * Send a hangup to the control terminal.
921 * Enable all select calls.
923 if (waitqueue_active(&(ch->ch_flags_wait)))
924 wake_up_interruptible(&ch->ch_flags_wait);
926 if (ch->ch_tun.un_open_count > 0) {
927 DPR_CARR(("Sending tty hangup\n"));
928 tty_hangup(ch->ch_tun.un_tty);
931 if (ch->ch_pun.un_open_count > 0) {
932 DPR_CARR(("Sending pr hangup\n"));
933 tty_hangup(ch->ch_pun.un_tty);
938 * Make sure that our cached values reflect the current reality.
940 if (virt_carrier == 1)
941 ch->ch_flags |= CH_FCAR;
943 ch->ch_flags &= ~CH_FCAR;
945 if (phys_carrier == 1)
946 ch->ch_flags |= CH_CD;
948 ch->ch_flags &= ~CH_CD;
952 * Assign the custom baud rate to the channel structure
954 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
963 ch->ch_custom_speed = 0;
968 * Since the divisor is stored in a 16-bit integer, we make sure
969 * we don't allow any rates smaller than a 16-bit integer would allow.
970 * And of course, rates above the dividend won't fly.
972 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
973 newrate = ((ch->ch_bd->bd_dividend / 0xFFFF) + 1);
975 if (newrate && newrate > ch->ch_bd->bd_dividend)
976 newrate = ch->ch_bd->bd_dividend;
979 testdiv = ch->ch_bd->bd_dividend / newrate;
982 * If we try to figure out what rate the board would use
983 * with the test divisor, it will be either equal or higher
984 * than the requested baud rate. If we then determine the
985 * rate with a divisor one higher, we will get the next lower
986 * supported rate below the requested.
988 testrate_high = ch->ch_bd->bd_dividend / testdiv;
989 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
992 * If the rate for the requested divisor is correct, just
993 * use it and be done.
995 if (testrate_high != newrate) {
997 * Otherwise, pick the rate that is closer (i.e. whichever rate
998 * has a smaller delta).
1000 deltahigh = testrate_high - newrate;
1001 deltalow = newrate - testrate_low;
1003 if (deltahigh < deltalow)
1004 newrate = testrate_high;
1006 newrate = testrate_low;
1010 ch->ch_custom_speed = newrate;
1014 void dgnc_check_queue_flow_control(struct channel_t *ch)
1018 /* Store how much space we have left in the queue */
1019 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
1021 qleft += RQUEUEMASK + 1;
1024 * Check to see if we should enforce flow control on our queue because
1025 * the ld (or user) isn't reading data out of our queue fast enuf.
1027 * NOTE: This is done based on what the current flow control of the
1030 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
1031 * This will cause the UART's FIFO to back up, and force
1032 * the RTS signal to be dropped.
1033 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
1034 * the other side, in hopes it will stop sending data to us.
1035 * 3) NONE - Nothing we can do. We will simply drop any extra data
1036 * that gets sent into us when the queue fills up.
1040 if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
1041 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
1042 ch->ch_bd->bd_ops->disable_receiver(ch);
1043 ch->ch_flags |= (CH_RECEIVER_OFF);
1044 DPR_READ(("Internal queue hit hilevel mark (%d)! Turning off interrupts.\n",
1049 else if (ch->ch_c_iflag & IXOFF) {
1050 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
1051 ch->ch_bd->bd_ops->send_stop_character(ch);
1052 ch->ch_stops_sent++;
1053 DPR_READ(("Sending stop char! Times sent: %x\n", ch->ch_stops_sent));
1058 /* Empty... Can't do anything about the impending overflow... */
1063 * Check to see if we should unenforce flow control because
1064 * ld (or user) finally read enuf data out of our queue.
1066 * NOTE: This is done based on what the current flow control of the
1069 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
1070 * This will cause the UART's FIFO to raise RTS back up,
1071 * which will allow the other side to start sending data again.
1072 * 2) SWFLOW (IXOFF) - Send a start character to
1073 * the other side, so it will start sending data to us again.
1074 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
1075 * other side, we don't need to do anything now.
1077 if (qleft > (RQUEUESIZE / 2)) {
1079 if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
1080 if (ch->ch_flags & CH_RECEIVER_OFF) {
1081 ch->ch_bd->bd_ops->enable_receiver(ch);
1082 ch->ch_flags &= ~(CH_RECEIVER_OFF);
1083 DPR_READ(("Internal queue hit lowlevel mark (%d)! Turning on interrupts.\n",
1088 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
1089 ch->ch_stops_sent = 0;
1090 ch->ch_bd->bd_ops->send_start_character(ch);
1091 DPR_READ(("Sending start char!\n"));
1095 /* Nothing needed. */
1101 void dgnc_wakeup_writes(struct channel_t *ch)
1106 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1109 DGNC_LOCK(ch->ch_lock, lock_flags);
1112 * If channel now has space, wake up anyone waiting on the condition.
1114 qlen = ch->ch_w_head - ch->ch_w_tail;
1118 if (qlen >= (WQUEUESIZE - 256)) {
1119 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1123 if (ch->ch_tun.un_flags & UN_ISOPEN) {
1124 if ((ch->ch_tun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1125 ch->ch_tun.un_tty->ldisc->ops->write_wakeup)
1127 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1128 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
1129 DGNC_LOCK(ch->ch_lock, lock_flags);
1132 wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
1135 * If unit is set to wait until empty, check to make sure
1136 * the queue AND FIFO are both empty.
1138 if (ch->ch_tun.un_flags & UN_EMPTY) {
1139 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
1140 ch->ch_tun.un_flags &= ~(UN_EMPTY);
1143 * If RTS Toggle mode is on, whenever
1144 * the queue and UART is empty, keep RTS low.
1146 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1147 ch->ch_mostat &= ~(UART_MCR_RTS);
1148 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1152 * If DTR Toggle mode is on, whenever
1153 * the queue and UART is empty, keep DTR low.
1155 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1156 ch->ch_mostat &= ~(UART_MCR_DTR);
1157 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1162 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
1165 if (ch->ch_pun.un_flags & UN_ISOPEN) {
1166 if ((ch->ch_pun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1167 ch->ch_pun.un_tty->ldisc->ops->write_wakeup)
1169 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1170 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
1171 DGNC_LOCK(ch->ch_lock, lock_flags);
1174 wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
1177 * If unit is set to wait until empty, check to make sure
1178 * the queue AND FIFO are both empty.
1180 if (ch->ch_pun.un_flags & UN_EMPTY) {
1181 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
1182 ch->ch_pun.un_flags &= ~(UN_EMPTY);
1186 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1189 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1194 /************************************************************************
1196 * TTY Entry points and helper functions
1198 ************************************************************************/
1204 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
1206 struct dgnc_board *brd;
1207 struct channel_t *ch;
1216 major = MAJOR(tty_devnum(tty));
1217 minor = MINOR(tty_devnum(tty));
1222 /* Get board pointer from our array of majors we have allocated */
1223 brd = dgnc_BoardsByMajor[major];
1228 * If board is not yet up to a state of READY, go to
1229 * sleep waiting for it to happen or they cancel the open.
1231 rc = wait_event_interruptible(brd->state_wait,
1232 (brd->state & BOARD_READY));
1237 DGNC_LOCK(brd->bd_lock, lock_flags);
1239 /* If opened device is greater than our number of ports, bail. */
1240 if (PORT_NUM(minor) > brd->nasync) {
1241 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1245 ch = brd->channels[PORT_NUM(minor)];
1247 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1251 /* Drop board lock */
1252 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1254 /* Grab channel lock */
1255 DGNC_LOCK(ch->ch_lock, lock_flags);
1257 /* Figure out our type */
1258 if (!IS_PRINT(minor)) {
1259 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1260 un->un_type = DGNC_SERIAL;
1261 } else if (IS_PRINT(minor)) {
1262 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1263 un->un_type = DGNC_PRINT;
1265 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1266 DPR_OPEN(("%d Unknown TYPE!\n", __LINE__));
1271 * If the port is still in a previous open, and in a state
1272 * where we simply cannot safely keep going, wait until the
1275 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1277 rc = wait_event_interruptible(ch->ch_flags_wait, ((ch->ch_flags & CH_OPENING) == 0));
1279 /* If ret is non-zero, user ctrl-c'ed us */
1281 DPR_OPEN(("%d User ctrl c'ed\n", __LINE__));
1286 * If either unit is in the middle of the fragile part of close,
1287 * we just cannot touch the channel safely.
1288 * Go to sleep, knowing that when the channel can be
1289 * touched safely, the close routine will signal the
1290 * ch_flags_wait to wake us back up.
1292 rc = wait_event_interruptible(ch->ch_flags_wait,
1293 (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING) == 0));
1295 /* If ret is non-zero, user ctrl-c'ed us */
1297 DPR_OPEN(("%d User ctrl c'ed\n", __LINE__));
1301 DGNC_LOCK(ch->ch_lock, lock_flags);
1304 /* Store our unit into driver_data, so we always have it available. */
1305 tty->driver_data = un;
1307 DPR_OPEN(("Open called. MAJOR: %d MINOR:%d PORT_NUM: %x unit: %p NAME: %s\n",
1308 MAJOR(tty_devnum(tty)), MINOR(tty_devnum(tty)), PORT_NUM(minor), un, brd->name));
1310 DPR_OPEN(("%d: tflag=%x pflag=%x\n", __LINE__, ch->ch_tun.un_flags, ch->ch_pun.un_flags));
1315 if (!(un->un_flags & UN_ISOPEN)) {
1316 /* Store important variables. */
1319 /* Maybe do something here to the TTY struct as well? */
1324 * Allocate channel buffers for read/write/error.
1325 * Set flag, so we don't get trounced on.
1327 ch->ch_flags |= (CH_OPENING);
1329 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1330 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1333 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1335 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1337 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1339 DGNC_LOCK(ch->ch_lock, lock_flags);
1341 ch->ch_flags &= ~(CH_OPENING);
1342 wake_up_interruptible(&ch->ch_flags_wait);
1345 * Initialize if neither terminal or printer is open.
1347 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1349 DPR_OPEN(("dgnc_open: initializing channel in open...\n"));
1352 * Flush input queues.
1354 ch->ch_r_head = ch->ch_r_tail = 0;
1355 ch->ch_e_head = ch->ch_e_tail = 0;
1356 ch->ch_w_head = ch->ch_w_tail = 0;
1358 brd->bd_ops->flush_uart_write(ch);
1359 brd->bd_ops->flush_uart_read(ch);
1362 ch->ch_cached_lsr = 0;
1363 ch->ch_stop_sending_break = 0;
1364 ch->ch_stops_sent = 0;
1366 ch->ch_c_cflag = tty->termios.c_cflag;
1367 ch->ch_c_iflag = tty->termios.c_iflag;
1368 ch->ch_c_oflag = tty->termios.c_oflag;
1369 ch->ch_c_lflag = tty->termios.c_lflag;
1370 ch->ch_startc = tty->termios.c_cc[VSTART];
1371 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1374 * Bring up RTS and DTR...
1375 * Also handle RTS or DTR toggle if set.
1377 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1378 ch->ch_mostat |= (UART_MCR_RTS);
1379 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1380 ch->ch_mostat |= (UART_MCR_DTR);
1382 /* Tell UART to init itself */
1383 brd->bd_ops->uart_init(ch);
1387 * Run param in case we changed anything
1389 brd->bd_ops->param(tty);
1394 * follow protocol for opening port
1397 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1399 rc = dgnc_block_til_ready(tty, file, ch);
1402 DPR_OPEN(("dgnc_tty_open returning after dgnc_block_til_ready "
1405 /* No going back now, increment our unit and channel counters */
1406 DGNC_LOCK(ch->ch_lock, lock_flags);
1407 ch->ch_open_count++;
1408 un->un_open_count++;
1409 un->un_flags |= (UN_ISOPEN);
1410 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1412 DPR_OPEN(("dgnc_tty_open finished\n"));
1418 * dgnc_block_til_ready()
1420 * Wait for DCD, if needed.
1422 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch)
1425 struct un_t *un = NULL;
1428 int sleep_on_un_flags = 0;
1430 if (!tty || tty->magic != TTY_MAGIC || !file || !ch || ch->magic != DGNC_CHANNEL_MAGIC) {
1434 un = tty->driver_data;
1435 if (!un || un->magic != DGNC_UNIT_MAGIC)
1438 DPR_OPEN(("dgnc_block_til_ready - before block.\n"));
1440 DGNC_LOCK(ch->ch_lock, lock_flags);
1447 sleep_on_un_flags = 0;
1450 * If board has failed somehow during our sleep, bail with error.
1452 if (ch->ch_bd->state == BOARD_FAILED) {
1457 /* If tty was hung up, break out of loop and set error. */
1458 if (tty_hung_up_p(file)) {
1464 * If either unit is in the middle of the fragile part of close,
1465 * we just cannot touch the channel safely.
1466 * Go back to sleep, knowing that when the channel can be
1467 * touched safely, the close routine will signal the
1468 * ch_wait_flags to wake us back up.
1470 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING)) {
1473 * Our conditions to leave cleanly and happily:
1474 * 1) NONBLOCKING on the tty is set.
1476 * 3) DCD (fake or real) is active.
1479 if (file->f_flags & O_NONBLOCK)
1482 if (tty->flags & (1 << TTY_IO_ERROR)) {
1487 if (ch->ch_flags & CH_CD) {
1488 DPR_OPEN(("%d: ch_flags: %x\n", __LINE__, ch->ch_flags));
1492 if (ch->ch_flags & CH_FCAR) {
1493 DPR_OPEN(("%d: ch_flags: %x\n", __LINE__, ch->ch_flags));
1497 sleep_on_un_flags = 1;
1501 * If there is a signal pending, the user probably
1502 * interrupted (ctrl-c) us.
1503 * Leave loop with error set.
1505 if (signal_pending(current)) {
1506 DPR_OPEN(("%d: signal pending...\n", __LINE__));
1507 retval = -ERESTARTSYS;
1511 DPR_OPEN(("dgnc_block_til_ready - blocking.\n"));
1514 * Store the flags before we let go of channel lock
1516 if (sleep_on_un_flags)
1517 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1519 old_flags = ch->ch_flags;
1522 * Let go of channel lock before calling schedule.
1523 * Our poller will get any FEP events and wake us up when DCD
1524 * eventually goes active.
1527 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1529 DPR_OPEN(("Going to sleep on %s flags...\n",
1530 (sleep_on_un_flags ? "un" : "ch")));
1533 * Wait for something in the flags to change from the current value.
1535 if (sleep_on_un_flags)
1536 retval = wait_event_interruptible(un->un_flags_wait,
1537 (old_flags != (ch->ch_tun.un_flags | ch->ch_pun.un_flags)));
1539 retval = wait_event_interruptible(ch->ch_flags_wait,
1540 (old_flags != ch->ch_flags));
1542 DPR_OPEN(("After sleep... retval: %x\n", retval));
1545 * We got woken up for some reason.
1546 * Before looping around, grab our channel lock.
1548 DGNC_LOCK(ch->ch_lock, lock_flags);
1553 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1555 DPR_OPEN(("dgnc_block_til_ready - after blocking.\n"));
1558 DPR_OPEN(("dgnc_block_til_ready - done. error. retval: %x\n", retval));
1562 DPR_OPEN(("dgnc_block_til_ready - done no error. jiffies: %lu\n", jiffies));
1571 * Hangup the port. Like a close, but don't wait for output to drain.
1573 static void dgnc_tty_hangup(struct tty_struct *tty)
1577 if (!tty || tty->magic != TTY_MAGIC)
1580 un = tty->driver_data;
1581 if (!un || un->magic != DGNC_UNIT_MAGIC)
1584 DPR_CLOSE(("dgnc_hangup called. ch->ch_open_count: %d un->un_open_count: %d\n",
1585 un->un_ch->ch_open_count, un->un_open_count));
1587 /* flush the transmit queues */
1588 dgnc_tty_flush_buffer(tty);
1590 DPR_CLOSE(("dgnc_hangup finished. ch->ch_open_count: %d un->un_open_count: %d\n",
1591 un->un_ch->ch_open_count, un->un_open_count));
1599 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1601 struct ktermios *ts;
1602 struct dgnc_board *bd;
1603 struct channel_t *ch;
1608 if (!tty || tty->magic != TTY_MAGIC)
1611 un = tty->driver_data;
1612 if (!un || un->magic != DGNC_UNIT_MAGIC)
1616 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1620 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1625 DPR_CLOSE(("Close called\n"));
1627 DGNC_LOCK(ch->ch_lock, lock_flags);
1630 * Determine if this is the last close or not - and if we agree about
1631 * which type of close it is with the Line Discipline
1633 if ((tty->count == 1) && (un->un_open_count != 1)) {
1635 * Uh, oh. tty->count is 1, which means that the tty
1636 * structure will be freed. un_open_count should always
1637 * be one in these conditions. If it's greater than
1638 * one, we've got real problems, since it means the
1639 * serial port won't be shutdown.
1641 APR(("tty->count is 1, un open count is %d\n", un->un_open_count));
1642 un->un_open_count = 1;
1645 if (--un->un_open_count < 0) {
1646 APR(("bad serial port open count of %d\n", un->un_open_count));
1647 un->un_open_count = 0;
1650 ch->ch_open_count--;
1652 if (ch->ch_open_count && un->un_open_count) {
1653 DPR_CLOSE(("dgnc_tty_close: not last close ch: %d un:%d\n",
1654 ch->ch_open_count, un->un_open_count));
1656 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1660 /* OK, its the last close on the unit */
1661 DPR_CLOSE(("dgnc_tty_close - last close on unit procedures\n"));
1663 un->un_flags |= UN_CLOSING;
1669 * Only officially close channel if count is 0 and
1670 * DIGI_PRINTER bit is not set.
1672 if ((ch->ch_open_count == 0) && !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1674 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1677 * turn off print device when closing print device.
1679 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1680 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1681 (int) ch->ch_digi.digi_offlen);
1682 ch->ch_flags &= ~CH_PRON;
1685 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1686 /* wait for output to drain */
1687 /* This will also return if we take an interrupt */
1689 DPR_CLOSE(("Calling wait_for_drain\n"));
1690 rc = bd->bd_ops->drain(tty, 0);
1692 DPR_CLOSE(("After calling wait_for_drain\n"));
1695 DPR_BASIC(("dgnc_tty_close - bad return: %d ", rc));
1697 dgnc_tty_flush_buffer(tty);
1698 tty_ldisc_flush(tty);
1700 DGNC_LOCK(ch->ch_lock, lock_flags);
1705 * If we have HUPCL set, lower DTR and RTS
1707 if (ch->ch_c_cflag & HUPCL) {
1708 DPR_CLOSE(("Close. HUPCL set, dropping DTR/RTS\n"));
1711 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1712 bd->bd_ops->assert_modem_signals(ch);
1715 * Go to sleep to ensure RTS/DTR
1716 * have been dropped for modems to see it.
1718 if (ch->ch_close_delay) {
1719 DPR_CLOSE(("Close. Sleeping for RTS/DTR drop\n"));
1721 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1722 dgnc_ms_sleep(ch->ch_close_delay);
1723 DGNC_LOCK(ch->ch_lock, lock_flags);
1725 DPR_CLOSE(("Close. After sleeping for RTS/DTR drop\n"));
1729 ch->ch_old_baud = 0;
1731 /* Turn off UART interrupts for this port */
1732 ch->ch_bd->bd_ops->uart_off(ch);
1735 * turn off print device when closing print device.
1737 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1738 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1739 (int) ch->ch_digi.digi_offlen);
1740 ch->ch_flags &= ~CH_PRON;
1745 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1747 DPR_CLOSE(("Close. Doing wakeups\n"));
1748 wake_up_interruptible(&ch->ch_flags_wait);
1749 wake_up_interruptible(&un->un_flags_wait);
1751 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1753 DPR_BASIC(("dgnc_tty_close - complete\n"));
1758 * dgnc_tty_chars_in_buffer()
1760 * Return number of characters that have not been transmitted yet.
1762 * This routine is used by the line discipline to determine if there
1763 * is data waiting to be transmitted/drained/flushed or not.
1765 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1767 struct channel_t *ch = NULL;
1768 struct un_t *un = NULL;
1773 ulong lock_flags = 0;
1778 un = tty->driver_data;
1779 if (!un || un->magic != DGNC_UNIT_MAGIC)
1783 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1786 DGNC_LOCK(ch->ch_lock, lock_flags);
1789 thead = ch->ch_w_head & tmask;
1790 ttail = ch->ch_w_tail & tmask;
1792 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1794 if (ttail == thead) {
1798 chars = thead - ttail;
1800 chars = thead - ttail + WQUEUESIZE;
1803 DPR_WRITE(("dgnc_tty_chars_in_buffer. Port: %x - %d (head: %d tail: %d)\n",
1804 ch->ch_portnum, chars, thead, ttail));
1813 * Reduces bytes_available to the max number of characters
1814 * that can be sent currently given the maxcps value, and
1815 * returns the new bytes_available. This only affects printer
1818 static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1820 struct channel_t *ch = NULL;
1821 struct un_t *un = NULL;
1824 return bytes_available;
1826 un = tty->driver_data;
1827 if (!un || un->magic != DGNC_UNIT_MAGIC)
1828 return bytes_available;
1831 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1832 return bytes_available;
1835 * If its not the Transparent print device, return
1836 * the full data amount.
1838 if (un->un_type != DGNC_PRINT)
1839 return bytes_available;
1841 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1843 unsigned long current_time = jiffies;
1844 unsigned long buffer_time = current_time +
1845 (HZ * ch->ch_digi.digi_bufsize) / ch->ch_digi.digi_maxcps;
1847 if (ch->ch_cpstime < current_time) {
1848 /* buffer is empty */
1849 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1850 cps_limit = ch->ch_digi.digi_bufsize;
1851 } else if (ch->ch_cpstime < buffer_time) {
1852 /* still room in the buffer */
1853 cps_limit = ((buffer_time - ch->ch_cpstime) * ch->ch_digi.digi_maxcps) / HZ;
1855 /* no room in the buffer */
1859 bytes_available = min(cps_limit, bytes_available);
1862 return bytes_available;
1867 * dgnc_tty_write_room()
1869 * Return space available in Tx buffer
1871 static int dgnc_tty_write_room(struct tty_struct *tty)
1873 struct channel_t *ch = NULL;
1874 struct un_t *un = NULL;
1879 ulong lock_flags = 0;
1881 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1884 un = tty->driver_data;
1885 if (!un || un->magic != DGNC_UNIT_MAGIC)
1889 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1892 DGNC_LOCK(ch->ch_lock, lock_flags);
1895 head = (ch->ch_w_head) & tmask;
1896 tail = (ch->ch_w_tail) & tmask;
1898 ret = tail - head - 1;
1902 /* Limit printer to maxcps */
1903 ret = dgnc_maxcps_room(tty, ret);
1906 * If we are printer device, leave space for
1907 * possibly both the on and off strings.
1909 if (un->un_type == DGNC_PRINT) {
1910 if (!(ch->ch_flags & CH_PRON))
1911 ret -= ch->ch_digi.digi_onlen;
1912 ret -= ch->ch_digi.digi_offlen;
1914 if (ch->ch_flags & CH_PRON)
1915 ret -= ch->ch_digi.digi_offlen;
1921 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1923 DPR_WRITE(("dgnc_tty_write_room - %d tail: %d head: %d\n", ret, tail, head));
1930 * dgnc_tty_put_char()
1932 * Put a character into ch->ch_buf
1934 * - used by the line discipline for OPOST processing
1936 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1939 * Simply call tty_write.
1941 DPR_WRITE(("dgnc_tty_put_char called\n"));
1942 dgnc_tty_write(tty, &c, 1);
1950 * Take data from the user or kernel and send it out to the FEP.
1951 * In here exists all the Transparent Print magic as well.
1953 static int dgnc_tty_write(struct tty_struct *tty,
1954 const unsigned char *buf, int count)
1956 struct channel_t *ch = NULL;
1957 struct un_t *un = NULL;
1958 int bufcount = 0, n = 0;
1967 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1970 un = tty->driver_data;
1971 if (!un || un->magic != DGNC_UNIT_MAGIC)
1975 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1981 DPR_WRITE(("dgnc_tty_write: Port: %x tty=%p user=%d len=%d\n",
1982 ch->ch_portnum, tty, from_user, count));
1985 * Store original amount of characters passed in.
1986 * This helps to figure out if we should ask the FEP
1987 * to send us an event when it has more space available.
1991 DGNC_LOCK(ch->ch_lock, lock_flags);
1993 /* Get our space available for the channel from the board */
1995 head = (ch->ch_w_head) & tmask;
1996 tail = (ch->ch_w_tail) & tmask;
1998 bufcount = tail - head - 1;
2000 bufcount += WQUEUESIZE;
2002 DPR_WRITE(("%d: bufcount: %x count: %x tail: %x head: %x tmask: %x\n",
2003 __LINE__, bufcount, count, tail, head, tmask));
2006 * Limit printer output to maxcps overall, with bursts allowed
2007 * up to bufsize characters.
2009 bufcount = dgnc_maxcps_room(tty, bufcount);
2012 * Take minimum of what the user wants to send, and the
2013 * space available in the FEP buffer.
2015 count = min(count, bufcount);
2018 * Bail if no space left.
2021 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2026 * Output the printer ON string, if we are in terminal mode, but
2027 * need to be in printer mode.
2029 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
2030 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
2031 (int) ch->ch_digi.digi_onlen);
2032 head = (ch->ch_w_head) & tmask;
2033 ch->ch_flags |= CH_PRON;
2037 * On the other hand, output the printer OFF string, if we are
2038 * currently in printer mode, but need to output to the terminal.
2040 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
2041 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
2042 (int) ch->ch_digi.digi_offlen);
2043 head = (ch->ch_w_head) & tmask;
2044 ch->ch_flags &= ~CH_PRON;
2048 * If there is nothing left to copy, or I can't handle any more data, leave.
2051 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2057 count = min(count, WRITEBUFLEN);
2059 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2062 * If data is coming from user space, copy it into a temporary
2063 * buffer so we don't get swapped out while doing the copy to
2066 /* we're allowed to block if it's from_user */
2067 if (down_interruptible(&dgnc_TmpWriteSem))
2071 * copy_from_user() returns the number
2072 * of bytes that could *NOT* be copied.
2074 count -= copy_from_user(dgnc_TmpWriteBuf, (const uchar __user *) buf, count);
2077 up(&dgnc_TmpWriteSem);
2081 DGNC_LOCK(ch->ch_lock, lock_flags);
2083 buf = dgnc_TmpWriteBuf;
2090 * If the write wraps over the top of the circular buffer,
2091 * move the portion up to the wrap point, and reset the
2092 * pointers to the bottom.
2094 remain = WQUEUESIZE - head;
2098 memcpy(ch->ch_wqueue + head, buf, remain);
2099 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
2106 * Move rest of data.
2109 memcpy(ch->ch_wqueue + head, buf, remain);
2110 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
2116 ch->ch_w_head = head;
2121 * If this is the print device, and the
2122 * printer is still on, we need to turn it
2123 * off before going idle.
2125 if (count == orig_count) {
2126 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
2128 ch->ch_w_head = head;
2129 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
2130 (int) ch->ch_digi.digi_offlen);
2131 head = (ch->ch_w_head) & tmask;
2132 ch->ch_flags &= ~CH_PRON;
2137 /* Update printer buffer empty time. */
2138 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
2139 && (ch->ch_digi.digi_bufsize > 0)) {
2140 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
2144 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2145 up(&dgnc_TmpWriteSem);
2147 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2150 DPR_WRITE(("Write finished - Write %d bytes of %d.\n", count, orig_count));
2154 * Channel lock is grabbed and then released
2155 * inside this routine.
2157 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
2165 * Return modem signals to ld.
2168 static int dgnc_tty_tiocmget(struct tty_struct *tty)
2170 struct channel_t *ch;
2176 if (!tty || tty->magic != TTY_MAGIC)
2179 un = tty->driver_data;
2180 if (!un || un->magic != DGNC_UNIT_MAGIC)
2184 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2187 DPR_IOCTL(("dgnc_tty_tiocmget start\n"));
2189 DGNC_LOCK(ch->ch_lock, lock_flags);
2191 mstat = (ch->ch_mostat | ch->ch_mistat);
2193 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2197 if (mstat & UART_MCR_DTR)
2198 result |= TIOCM_DTR;
2199 if (mstat & UART_MCR_RTS)
2200 result |= TIOCM_RTS;
2201 if (mstat & UART_MSR_CTS)
2202 result |= TIOCM_CTS;
2203 if (mstat & UART_MSR_DSR)
2204 result |= TIOCM_DSR;
2205 if (mstat & UART_MSR_RI)
2207 if (mstat & UART_MSR_DCD)
2210 DPR_IOCTL(("dgnc_tty_tiocmget finish\n"));
2217 * dgnc_tty_tiocmset()
2219 * Set modem signals, called by ld.
2222 static int dgnc_tty_tiocmset(struct tty_struct *tty,
2223 unsigned int set, unsigned int clear)
2225 struct dgnc_board *bd;
2226 struct channel_t *ch;
2231 if (!tty || tty->magic != TTY_MAGIC)
2234 un = tty->driver_data;
2235 if (!un || un->magic != DGNC_UNIT_MAGIC)
2239 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2243 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2246 DPR_IOCTL(("dgnc_tty_tiocmset start\n"));
2249 DGNC_LOCK(ch->ch_lock, lock_flags);
2251 if (set & TIOCM_RTS)
2252 ch->ch_mostat |= UART_MCR_RTS;
2254 if (set & TIOCM_DTR)
2255 ch->ch_mostat |= UART_MCR_DTR;
2257 if (clear & TIOCM_RTS)
2258 ch->ch_mostat &= ~(UART_MCR_RTS);
2260 if (clear & TIOCM_DTR)
2261 ch->ch_mostat &= ~(UART_MCR_DTR);
2263 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2265 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2267 DPR_IOCTL(("dgnc_tty_tiocmset finish\n"));
2274 * dgnc_tty_send_break()
2276 * Send a Break, called by ld.
2278 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
2280 struct dgnc_board *bd;
2281 struct channel_t *ch;
2286 if (!tty || tty->magic != TTY_MAGIC)
2289 un = tty->driver_data;
2290 if (!un || un->magic != DGNC_UNIT_MAGIC)
2294 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2298 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2312 DPR_IOCTL(("dgnc_tty_send_break start 1. %lx\n", jiffies));
2314 DGNC_LOCK(ch->ch_lock, lock_flags);
2316 ch->ch_bd->bd_ops->send_break(ch, msec);
2318 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2320 DPR_IOCTL(("dgnc_tty_send_break finish\n"));
2328 * dgnc_tty_wait_until_sent()
2330 * wait until data has been transmitted, called by ld.
2332 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
2334 struct dgnc_board *bd;
2335 struct channel_t *ch;
2339 if (!tty || tty->magic != TTY_MAGIC)
2342 un = tty->driver_data;
2343 if (!un || un->magic != DGNC_UNIT_MAGIC)
2347 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2351 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2354 rc = bd->bd_ops->drain(tty, 0);
2356 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
2366 * send a high priority character, called by ld.
2368 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
2370 struct dgnc_board *bd;
2371 struct channel_t *ch;
2375 if (!tty || tty->magic != TTY_MAGIC)
2378 un = tty->driver_data;
2379 if (!un || un->magic != DGNC_UNIT_MAGIC)
2383 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2387 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2390 DPR_IOCTL(("dgnc_tty_send_xchar start\n"));
2391 printk("dgnc_tty_send_xchar start\n");
2393 DGNC_LOCK(ch->ch_lock, lock_flags);
2394 bd->bd_ops->send_immediate_char(ch, c);
2395 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2397 DPR_IOCTL(("dgnc_tty_send_xchar finish\n"));
2398 printk("dgnc_tty_send_xchar finish\n");
2406 * Return modem signals to ld.
2408 static inline int dgnc_get_mstat(struct channel_t *ch)
2410 unsigned char mstat;
2414 DPR_IOCTL(("dgnc_getmstat start\n"));
2416 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2419 DGNC_LOCK(ch->ch_lock, lock_flags);
2421 mstat = (ch->ch_mostat | ch->ch_mistat);
2423 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2427 if (mstat & UART_MCR_DTR)
2428 result |= TIOCM_DTR;
2429 if (mstat & UART_MCR_RTS)
2430 result |= TIOCM_RTS;
2431 if (mstat & UART_MSR_CTS)
2432 result |= TIOCM_CTS;
2433 if (mstat & UART_MSR_DSR)
2434 result |= TIOCM_DSR;
2435 if (mstat & UART_MSR_RI)
2437 if (mstat & UART_MSR_DCD)
2440 DPR_IOCTL(("dgnc_getmstat finish\n"));
2448 * Return modem signals to ld.
2450 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value)
2455 DPR_IOCTL(("dgnc_get_modem_info start\n"));
2457 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2460 result = dgnc_get_mstat(ch);
2465 rc = put_user(result, value);
2467 DPR_IOCTL(("dgnc_get_modem_info finish\n"));
2473 * dgnc_set_modem_info()
2475 * Set modem signals, called by ld.
2477 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value)
2479 struct dgnc_board *bd;
2480 struct channel_t *ch;
2483 unsigned int arg = 0;
2486 if (!tty || tty->magic != TTY_MAGIC)
2489 un = tty->driver_data;
2490 if (!un || un->magic != DGNC_UNIT_MAGIC)
2494 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2498 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2503 DPR_IOCTL(("dgnc_set_modem_info() start\n"));
2505 ret = get_user(arg, value);
2511 if (arg & TIOCM_RTS)
2512 ch->ch_mostat |= UART_MCR_RTS;
2514 if (arg & TIOCM_DTR)
2515 ch->ch_mostat |= UART_MCR_DTR;
2520 if (arg & TIOCM_RTS)
2521 ch->ch_mostat &= ~(UART_MCR_RTS);
2523 if (arg & TIOCM_DTR)
2524 ch->ch_mostat &= ~(UART_MCR_DTR);
2530 if (arg & TIOCM_RTS)
2531 ch->ch_mostat |= UART_MCR_RTS;
2533 ch->ch_mostat &= ~(UART_MCR_RTS);
2535 if (arg & TIOCM_DTR)
2536 ch->ch_mostat |= UART_MCR_DTR;
2538 ch->ch_mostat &= ~(UART_MCR_DTR);
2546 DGNC_LOCK(ch->ch_lock, lock_flags);
2548 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2550 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2552 DPR_IOCTL(("dgnc_set_modem_info finish\n"));
2559 * dgnc_tty_digigeta()
2561 * Ioctl to get the information for ditty.
2566 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo)
2568 struct channel_t *ch;
2576 if (!tty || tty->magic != TTY_MAGIC)
2579 un = tty->driver_data;
2580 if (!un || un->magic != DGNC_UNIT_MAGIC)
2584 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2587 memset(&tmp, 0, sizeof(tmp));
2589 DGNC_LOCK(ch->ch_lock, lock_flags);
2590 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2591 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2593 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2601 * dgnc_tty_digiseta()
2603 * Ioctl to set the information for ditty.
2608 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info)
2610 struct dgnc_board *bd;
2611 struct channel_t *ch;
2613 struct digi_t new_digi;
2616 DPR_IOCTL(("DIGI_SETA start\n"));
2618 if (!tty || tty->magic != TTY_MAGIC)
2621 un = tty->driver_data;
2622 if (!un || un->magic != DGNC_UNIT_MAGIC)
2626 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2630 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2633 if (copy_from_user(&new_digi, new_info, sizeof(new_digi))) {
2634 DPR_IOCTL(("DIGI_SETA failed copy_from_user\n"));
2638 DGNC_LOCK(ch->ch_lock, lock_flags);
2641 * Handle transistions to and from RTS Toggle.
2643 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2644 ch->ch_mostat &= ~(UART_MCR_RTS);
2645 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2646 ch->ch_mostat |= (UART_MCR_RTS);
2649 * Handle transistions to and from DTR Toggle.
2651 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2652 ch->ch_mostat &= ~(UART_MCR_DTR);
2653 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2654 ch->ch_mostat |= (UART_MCR_DTR);
2656 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2658 if (ch->ch_digi.digi_maxcps < 1)
2659 ch->ch_digi.digi_maxcps = 1;
2661 if (ch->ch_digi.digi_maxcps > 10000)
2662 ch->ch_digi.digi_maxcps = 10000;
2664 if (ch->ch_digi.digi_bufsize < 10)
2665 ch->ch_digi.digi_bufsize = 10;
2667 if (ch->ch_digi.digi_maxchar < 1)
2668 ch->ch_digi.digi_maxchar = 1;
2670 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2671 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2673 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2674 ch->ch_digi.digi_onlen = DIGI_PLEN;
2676 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2677 ch->ch_digi.digi_offlen = DIGI_PLEN;
2679 ch->ch_bd->bd_ops->param(tty);
2681 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2683 DPR_IOCTL(("DIGI_SETA finish\n"));
2690 * dgnc_set_termios()
2692 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2694 struct dgnc_board *bd;
2695 struct channel_t *ch;
2697 unsigned long lock_flags;
2699 if (!tty || tty->magic != TTY_MAGIC)
2702 un = tty->driver_data;
2703 if (!un || un->magic != DGNC_UNIT_MAGIC)
2707 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2711 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2714 DGNC_LOCK(ch->ch_lock, lock_flags);
2716 ch->ch_c_cflag = tty->termios.c_cflag;
2717 ch->ch_c_iflag = tty->termios.c_iflag;
2718 ch->ch_c_oflag = tty->termios.c_oflag;
2719 ch->ch_c_lflag = tty->termios.c_lflag;
2720 ch->ch_startc = tty->termios.c_cc[VSTART];
2721 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2723 ch->ch_bd->bd_ops->param(tty);
2726 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2730 static void dgnc_tty_throttle(struct tty_struct *tty)
2732 struct channel_t *ch;
2734 ulong lock_flags = 0;
2736 if (!tty || tty->magic != TTY_MAGIC)
2739 un = tty->driver_data;
2740 if (!un || un->magic != DGNC_UNIT_MAGIC)
2744 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2747 DPR_IOCTL(("dgnc_tty_throttle start\n"));
2749 DGNC_LOCK(ch->ch_lock, lock_flags);
2751 ch->ch_flags |= (CH_FORCED_STOPI);
2753 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2755 DPR_IOCTL(("dgnc_tty_throttle finish\n"));
2759 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2761 struct channel_t *ch;
2765 if (!tty || tty->magic != TTY_MAGIC)
2768 un = tty->driver_data;
2769 if (!un || un->magic != DGNC_UNIT_MAGIC)
2773 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2776 DPR_IOCTL(("dgnc_tty_unthrottle start\n"));
2778 DGNC_LOCK(ch->ch_lock, lock_flags);
2780 ch->ch_flags &= ~(CH_FORCED_STOPI);
2782 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2784 DPR_IOCTL(("dgnc_tty_unthrottle finish\n"));
2788 static void dgnc_tty_start(struct tty_struct *tty)
2790 struct dgnc_board *bd;
2791 struct channel_t *ch;
2795 if (!tty || tty->magic != TTY_MAGIC)
2798 un = tty->driver_data;
2799 if (!un || un->magic != DGNC_UNIT_MAGIC)
2803 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2807 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2810 DPR_IOCTL(("dgcn_tty_start start\n"));
2812 DGNC_LOCK(ch->ch_lock, lock_flags);
2814 ch->ch_flags &= ~(CH_FORCED_STOP);
2816 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2818 DPR_IOCTL(("dgnc_tty_start finish\n"));
2822 static void dgnc_tty_stop(struct tty_struct *tty)
2824 struct dgnc_board *bd;
2825 struct channel_t *ch;
2829 if (!tty || tty->magic != TTY_MAGIC)
2832 un = tty->driver_data;
2833 if (!un || un->magic != DGNC_UNIT_MAGIC)
2837 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2841 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2844 DPR_IOCTL(("dgnc_tty_stop start\n"));
2846 DGNC_LOCK(ch->ch_lock, lock_flags);
2848 ch->ch_flags |= (CH_FORCED_STOP);
2850 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2852 DPR_IOCTL(("dgnc_tty_stop finish\n"));
2857 * dgnc_tty_flush_chars()
2859 * Flush the cook buffer
2861 * Note to self, and any other poor souls who venture here:
2863 * flush in this case DOES NOT mean dispose of the data.
2864 * instead, it means "stop buffering and send it if you
2865 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2867 * It is also always called in interrupt context - JAR 8-Sept-99
2869 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2871 struct dgnc_board *bd;
2872 struct channel_t *ch;
2876 if (!tty || tty->magic != TTY_MAGIC)
2879 un = tty->driver_data;
2880 if (!un || un->magic != DGNC_UNIT_MAGIC)
2884 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2888 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2891 DPR_IOCTL(("dgnc_tty_flush_chars start\n"));
2893 DGNC_LOCK(ch->ch_lock, lock_flags);
2895 /* Do something maybe here */
2897 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2899 DPR_IOCTL(("dgnc_tty_flush_chars finish\n"));
2905 * dgnc_tty_flush_buffer()
2907 * Flush Tx buffer (make in == out)
2909 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2911 struct channel_t *ch;
2915 if (!tty || tty->magic != TTY_MAGIC)
2918 un = tty->driver_data;
2919 if (!un || un->magic != DGNC_UNIT_MAGIC)
2923 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2926 DPR_IOCTL(("dgnc_tty_flush_buffer on port: %d start\n", ch->ch_portnum));
2928 DGNC_LOCK(ch->ch_lock, lock_flags);
2930 ch->ch_flags &= ~CH_STOP;
2932 /* Flush our write queue */
2933 ch->ch_w_head = ch->ch_w_tail;
2935 /* Flush UARTs transmit FIFO */
2936 ch->ch_bd->bd_ops->flush_uart_write(ch);
2938 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2939 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2940 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2942 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2943 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2944 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2947 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2949 DPR_IOCTL(("dgnc_tty_flush_buffer finish\n"));
2954 /*****************************************************************************
2956 * The IOCTL function and all of its helpers
2958 *****************************************************************************/
2963 * The usual assortment of ioctl's
2965 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2968 struct dgnc_board *bd;
2969 struct channel_t *ch;
2973 void __user *uarg = (void __user *) arg;
2975 if (!tty || tty->magic != TTY_MAGIC)
2978 un = tty->driver_data;
2979 if (!un || un->magic != DGNC_UNIT_MAGIC)
2983 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2987 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2990 DPR_IOCTL(("dgnc_tty_ioctl start on port %d - cmd %s (%x), arg %lx\n",
2991 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
2993 DGNC_LOCK(ch->ch_lock, lock_flags);
2995 if (un->un_open_count <= 0) {
2996 DPR_BASIC(("dgnc_tty_ioctl - unit not open.\n"));
2997 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3003 /* Here are all the standard ioctl's that we MUST implement */
3007 * TCSBRK is SVID version: non-zero arg --> no break
3008 * this behaviour is exploited by tcdrain().
3010 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3011 * between 0.25 and 0.5 seconds so we'll ask for something
3012 * in the middle: 0.375 seconds.
3014 rc = tty_check_change(tty);
3015 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3019 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3022 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3026 DGNC_LOCK(ch->ch_lock, lock_flags);
3028 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP)) {
3029 ch->ch_bd->bd_ops->send_break(ch, 250);
3032 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3034 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3035 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3041 /* support for POSIX tcsendbreak()
3042 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3043 * between 0.25 and 0.5 seconds so we'll ask for something
3044 * in the middle: 0.375 seconds.
3046 rc = tty_check_change(tty);
3047 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3051 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3053 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3057 DGNC_LOCK(ch->ch_lock, lock_flags);
3059 ch->ch_bd->bd_ops->send_break(ch, 250);
3061 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3063 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3064 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3069 rc = tty_check_change(tty);
3070 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3074 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3076 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3080 DGNC_LOCK(ch->ch_lock, lock_flags);
3082 ch->ch_bd->bd_ops->send_break(ch, 250);
3084 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3086 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3087 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3093 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3098 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3100 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
3105 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3106 rc = get_user(arg, (unsigned long __user *) arg);
3110 DGNC_LOCK(ch->ch_lock, lock_flags);
3111 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
3112 ch->ch_bd->bd_ops->param(tty);
3113 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3118 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3119 return dgnc_get_modem_info(ch, uarg);
3124 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3125 return dgnc_set_modem_info(tty, cmd, uarg);
3128 * Here are any additional ioctl's that we want to implement
3133 * The linux tty driver doesn't have a flush
3134 * input routine for the driver, assuming all backed
3135 * up data is in the line disc. buffers. However,
3136 * we all know that's not the case. Here, we
3137 * act on the ioctl, but then lie and say we didn't
3138 * so the line discipline will process the flush
3141 rc = tty_check_change(tty);
3143 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3147 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
3148 ch->ch_r_head = ch->ch_r_tail;
3149 ch->ch_bd->bd_ops->flush_uart_read(ch);
3150 /* Force queue flow control to be released, if needed */
3151 dgnc_check_queue_flow_control(ch);
3154 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
3155 if (!(un->un_type == DGNC_PRINT)) {
3156 ch->ch_w_head = ch->ch_w_tail;
3157 ch->ch_bd->bd_ops->flush_uart_write(ch);
3159 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
3160 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
3161 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
3164 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
3165 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
3166 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
3172 /* pretend we didn't recognize this IOCTL */
3173 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3174 return -ENOIOCTLCMD;
3178 * The linux tty driver doesn't have a flush
3179 * input routine for the driver, assuming all backed
3180 * up data is in the line disc. buffers. However,
3181 * we all know that's not the case. Here, we
3182 * act on the ioctl, but then lie and say we didn't
3183 * so the line discipline will process the flush
3186 if (cmd == TCSETSF) {
3188 ch->ch_flags &= ~CH_STOP;
3189 ch->ch_r_head = ch->ch_r_tail;
3190 ch->ch_bd->bd_ops->flush_uart_read(ch);
3191 /* Force queue flow control to be released, if needed */
3192 dgnc_check_queue_flow_control(ch);
3195 /* now wait for all the output to drain */
3196 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3197 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3199 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d\n", rc));
3203 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3204 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3206 /* pretend we didn't recognize this */
3207 return -ENOIOCTLCMD;
3211 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3212 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3214 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3218 /* pretend we didn't recognize this */
3219 return -ENOIOCTLCMD;
3222 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3223 /* Make the ld do it */
3224 return -ENOIOCTLCMD;
3227 /* get information for ditty */
3228 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3229 return dgnc_tty_digigeta(tty, uarg);
3234 /* set information for ditty */
3235 if (cmd == (DIGI_SETAW)) {
3237 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3238 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3240 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3243 DGNC_LOCK(ch->ch_lock, lock_flags);
3245 tty_ldisc_flush(tty);
3250 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3251 return dgnc_tty_digiseta(tty, uarg);
3256 /* Let go of locks when accessing user space, could sleep */
3257 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3258 rc = get_user(loopback, (unsigned int __user *) arg);
3261 DGNC_LOCK(ch->ch_lock, lock_flags);
3263 /* Enable/disable internal loopback for this port */
3265 ch->ch_flags |= CH_LOOPBACK;
3267 ch->ch_flags &= ~(CH_LOOPBACK);
3269 ch->ch_bd->bd_ops->param(tty);
3270 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3274 case DIGI_GETCUSTOMBAUD:
3275 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3276 rc = put_user(ch->ch_custom_speed, (unsigned int __user *) arg);
3279 case DIGI_SETCUSTOMBAUD:
3282 /* Let go of locks when accessing user space, could sleep */
3283 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3284 rc = get_user(new_rate, (int __user *) arg);
3287 DGNC_LOCK(ch->ch_lock, lock_flags);
3288 dgnc_set_custom_speed(ch, new_rate);
3289 ch->ch_bd->bd_ops->param(tty);
3290 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3295 * This ioctl allows insertion of a character into the front
3296 * of any pending data to be transmitted.
3298 * This ioctl is to satify the "Send Character Immediate"
3299 * call that the RealPort protocol spec requires.
3301 case DIGI_REALPORT_SENDIMMEDIATE:
3304 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3305 rc = get_user(c, (unsigned char __user *) arg);
3308 DGNC_LOCK(ch->ch_lock, lock_flags);
3309 ch->ch_bd->bd_ops->send_immediate_char(ch, c);
3310 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3315 * This ioctl returns all the current counts for the port.
3317 * This ioctl is to satify the "Line Error Counters"
3318 * call that the RealPort protocol spec requires.
3320 case DIGI_REALPORT_GETCOUNTERS:
3322 struct digi_getcounter buf;
3324 buf.norun = ch->ch_err_overrun;
3325 buf.noflow = 0; /* The driver doesn't keep this stat */
3326 buf.nframe = ch->ch_err_frame;
3327 buf.nparity = ch->ch_err_parity;
3328 buf.nbreak = ch->ch_err_break;
3329 buf.rbytes = ch->ch_rxcount;
3330 buf.tbytes = ch->ch_txcount;
3332 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3334 if (copy_to_user(uarg, &buf, sizeof(buf)))
3341 * This ioctl returns all current events.
3343 * This ioctl is to satify the "Event Reporting"
3344 * call that the RealPort protocol spec requires.
3346 case DIGI_REALPORT_GETEVENTS:
3348 unsigned int events = 0;
3350 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
3351 if (ch->ch_flags & CH_BREAK_SENDING)
3353 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP))
3354 events |= (EV_OPU | EV_OPS);
3356 if ((ch->ch_flags & CH_STOPI) || (ch->ch_flags & CH_FORCED_STOPI)) {
3357 events |= (EV_IPU | EV_IPS);
3360 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3361 rc = put_user(events, (unsigned int __user *) arg);
3366 * This ioctl returns TOUT and TIN counters based
3367 * upon the values passed in by the RealPort Server.
3368 * It also passes back whether the UART Transmitter is
3371 case DIGI_REALPORT_GETBUFFERS:
3373 struct digi_getbuffer buf;
3377 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3380 * Get data from user first.
3382 if (copy_from_user(&buf, uarg, sizeof(buf)))
3385 DGNC_LOCK(ch->ch_lock, lock_flags);
3388 * Figure out how much data is in our RX and TX queues.
3390 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
3391 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
3394 * Is the UART empty? Add that value to whats in our TX queue.
3396 count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
3399 * Figure out how much data the RealPort Server believes should
3400 * be in our TX queue.
3402 tdist = (buf.tIn - buf.tOut) & 0xffff;
3405 * If we have more data than the RealPort Server believes we
3406 * should have, reduce our count to its amount.
3408 * This count difference CAN happen because the Linux LD can
3409 * insert more characters into our queue for OPOST processing
3410 * that the RealPort Server doesn't know about.
3412 if (buf.txbuf > tdist)
3416 * Report whether our queue and UART TX are completely empty.
3423 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3425 if (copy_to_user(uarg, &buf, sizeof(buf)))
3431 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3433 DPR_IOCTL(("dgnc_tty_ioctl - in default\n"));
3434 DPR_IOCTL(("dgnc_tty_ioctl end - cmd %s (%x), arg %lx\n",
3435 dgnc_ioctl_name(cmd), cmd, arg));
3437 return -ENOIOCTLCMD;
3440 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3442 DPR_IOCTL(("dgnc_tty_ioctl end - cmd %s (%x), arg %lx\n",
3443 dgnc_ioctl_name(cmd), cmd, arg));