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!
541 * Copy as much data as will fit.
547 r = SNIFF_MAX - ch->ch_sniff_in;
550 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, r);
558 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, n);
560 ch->ch_sniff_in += n;
565 * Wakeup any thread waiting for data
567 if (ch->ch_sniff_flags & SNIFF_WAIT_DATA) {
568 ch->ch_sniff_flags &= ~SNIFF_WAIT_DATA;
569 wake_up_interruptible(&ch->ch_sniff_wait);
574 * If the user sent us too much data to push into our tmpbuf,
575 * we need to keep looping around on all the data.
582 } while (too_much_data);
586 /*=======================================================================
588 * dgnc_wmove - Write data to transmit queue.
590 * ch - Pointer to channel structure.
591 * buf - Poiter to characters to be moved.
592 * n - Number of characters to move.
594 *=======================================================================*/
595 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
600 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
603 head = ch->ch_w_head & WQUEUEMASK;
606 * If the write wraps over the top of the circular buffer,
607 * move the portion up to the wrap point, and reset the
608 * pointers to the bottom.
610 remain = WQUEUESIZE - head;
614 memcpy(ch->ch_wqueue + head, buf, remain);
624 memcpy(ch->ch_wqueue + head, buf, remain);
629 ch->ch_w_head = head;
635 /*=======================================================================
637 * dgnc_input - Process received data.
639 * ch - Pointer to channel structure.
641 *=======================================================================*/
642 void dgnc_input(struct channel_t *ch)
644 struct dgnc_board *bd;
645 struct tty_struct *tp;
646 struct tty_ldisc *ld;
658 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
661 tp = ch->ch_tun.un_tty;
664 if(!bd || bd->magic != DGNC_BOARD_MAGIC)
667 DGNC_LOCK(ch->ch_lock, lock_flags);
670 * Figure the number of characters in the buffer.
671 * Exit immediately if none.
674 head = ch->ch_r_head & rmask;
675 tail = ch->ch_r_tail & rmask;
676 data_len = (head - tail) & rmask;
679 DGNC_UNLOCK(ch->ch_lock, lock_flags);
683 DPR_READ(("dgnc_input start\n"));
686 * If the device is not open, or CREAD is off,
687 * flush input data and return immediately.
689 if (!tp || (tp->magic != TTY_MAGIC) || !(ch->ch_tun.un_flags & UN_ISOPEN) ||
690 !(tp->termios.c_cflag & CREAD) || (ch->ch_tun.un_flags & UN_CLOSING)) {
692 DPR_READ(("input. dropping %d bytes on port %d...\n", data_len, ch->ch_portnum));
693 DPR_READ(("input. tp: %p tp->magic: %x MAGIC:%x ch flags: %x\n",
694 tp, tp ? tp->magic : 0, TTY_MAGIC, ch->ch_tun.un_flags));
696 ch->ch_r_head = tail;
698 /* Force queue flow control to be released, if needed */
699 dgnc_check_queue_flow_control(ch);
701 DGNC_UNLOCK(ch->ch_lock, lock_flags);
706 * If we are throttled, simply don't read any data.
708 if (ch->ch_flags & CH_FORCED_STOPI) {
709 DGNC_UNLOCK(ch->ch_lock, lock_flags);
710 DPR_READ(("Port %d throttled, not reading any data. head: %x tail: %x\n",
711 ch->ch_portnum, head, tail));
715 DPR_READ(("dgnc_input start 2\n"));
717 flip_len = TTY_FLIPBUF_SIZE;
719 /* Chop down the length, if needed */
720 len = min(data_len, flip_len);
721 len = min(len, (N_TTY_BUF_SIZE - 1));
723 ld = tty_ldisc_ref(tp);
727 * If the DONT_FLIP flag is on, don't flush our buffer, and act
728 * like the ld doesn't have any space to put the data right now.
730 if (test_bit(TTY_DONT_FLIP, &tp->flags))
735 * If we were unable to get a reference to the ld,
736 * don't flush our buffer, and act like the ld doesn't
737 * have any space to put the data right now.
743 * If ld doesn't have a pointer to a receive_buf function,
744 * flush the data, then act like the ld doesn't have any
745 * space to put the data right now.
747 if (!ld->ops->receive_buf) {
748 ch->ch_r_head = ch->ch_r_tail;
754 DGNC_UNLOCK(ch->ch_lock, lock_flags);
761 * The tty layer in the kernel has changed in 2.6.16+.
763 * The flip buffers in the tty structure are no longer exposed,
764 * and probably will be going away eventually.
766 * If we are completely raw, we don't need to go through a lot
767 * of the tty layers that exist.
768 * In this case, we take the shortest and fastest route we
769 * can to relay the data to the user.
771 * On the other hand, if we are not raw, we need to go through
772 * the new 2.6.16+ tty layer, which has its API more well defined.
774 len = tty_buffer_request_room(tp->port, len);
778 * n now contains the most amount of data we can copy,
779 * bounded either by how much the Linux tty layer can handle,
780 * or the amount of data the card actually has pending...
783 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
790 * If conditions are such that ld needs to see all
791 * UART errors, we will have to walk each character
792 * and error byte and send them to the buffer one at
795 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
796 for (i = 0; i < s; i++) {
797 if (*(ch->ch_equeue + tail + i) & UART_LSR_BI)
798 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_BREAK);
799 else if (*(ch->ch_equeue + tail + i) & UART_LSR_PE)
800 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_PARITY);
801 else if (*(ch->ch_equeue + tail + i) & UART_LSR_FE)
802 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_FRAME);
804 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_NORMAL);
808 tty_insert_flip_string(tp->port, ch->ch_rqueue + tail, s);
811 dgnc_sniff_nowait_nolock(ch, "USER READ", ch->ch_rqueue + tail, s);
815 /* Flip queue if needed */
819 ch->ch_r_tail = tail & rmask;
820 ch->ch_e_tail = tail & rmask;
821 dgnc_check_queue_flow_control(ch);
822 DGNC_UNLOCK(ch->ch_lock, lock_flags);
824 /* Tell the tty layer its okay to "eat" the data now */
825 tty_flip_buffer_push(tp->port);
830 DPR_READ(("dgnc_input - finish\n"));
834 /************************************************************************
835 * Determines when CARRIER changes state and takes appropriate
837 ************************************************************************/
838 void dgnc_carrier(struct channel_t *ch)
840 struct dgnc_board *bd;
842 int virt_carrier = 0;
843 int phys_carrier = 0;
845 DPR_CARR(("dgnc_carrier called...\n"));
847 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
852 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
855 if (ch->ch_mistat & UART_MSR_DCD) {
856 DPR_CARR(("mistat: %x D_CD: %x\n", ch->ch_mistat, ch->ch_mistat & UART_MSR_DCD));
860 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD) {
864 if (ch->ch_c_cflag & CLOCAL) {
869 DPR_CARR(("DCD: physical: %d virt: %d\n", phys_carrier, virt_carrier));
872 * Test for a VIRTUAL carrier transition to HIGH.
874 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
877 * When carrier rises, wake any threads waiting
878 * for carrier in the open routine.
881 DPR_CARR(("carrier: virt DCD rose\n"));
883 if (waitqueue_active(&(ch->ch_flags_wait)))
884 wake_up_interruptible(&ch->ch_flags_wait);
888 * Test for a PHYSICAL carrier transition to HIGH.
890 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
893 * When carrier rises, wake any threads waiting
894 * for carrier in the open routine.
897 DPR_CARR(("carrier: physical DCD rose\n"));
899 if (waitqueue_active(&(ch->ch_flags_wait)))
900 wake_up_interruptible(&ch->ch_flags_wait);
904 * Test for a PHYSICAL transition to low, so long as we aren't
905 * currently ignoring physical transitions (which is what "virtual
906 * carrier" indicates).
908 * The transition of the virtual carrier to low really doesn't
909 * matter... it really only means "ignore carrier state", not
910 * "make pretend that carrier is there".
912 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
917 * When carrier drops:
919 * Drop carrier on all open units.
921 * Flush queues, waking up any task waiting in the
924 * Send a hangup to the control terminal.
926 * Enable all select calls.
928 if (waitqueue_active(&(ch->ch_flags_wait)))
929 wake_up_interruptible(&ch->ch_flags_wait);
931 if (ch->ch_tun.un_open_count > 0) {
932 DPR_CARR(("Sending tty hangup\n"));
933 tty_hangup(ch->ch_tun.un_tty);
936 if (ch->ch_pun.un_open_count > 0) {
937 DPR_CARR(("Sending pr hangup\n"));
938 tty_hangup(ch->ch_pun.un_tty);
943 * Make sure that our cached values reflect the current reality.
945 if (virt_carrier == 1)
946 ch->ch_flags |= CH_FCAR;
948 ch->ch_flags &= ~CH_FCAR;
950 if (phys_carrier == 1)
951 ch->ch_flags |= CH_CD;
953 ch->ch_flags &= ~CH_CD;
957 * Assign the custom baud rate to the channel structure
959 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
968 ch->ch_custom_speed = 0;
973 * Since the divisor is stored in a 16-bit integer, we make sure
974 * we don't allow any rates smaller than a 16-bit integer would allow.
975 * And of course, rates above the dividend won't fly.
977 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
978 newrate = ((ch->ch_bd->bd_dividend / 0xFFFF) + 1);
980 if (newrate && newrate > ch->ch_bd->bd_dividend)
981 newrate = ch->ch_bd->bd_dividend;
984 testdiv = ch->ch_bd->bd_dividend / newrate;
987 * If we try to figure out what rate the board would use
988 * with the test divisor, it will be either equal or higher
989 * than the requested baud rate. If we then determine the
990 * rate with a divisor one higher, we will get the next lower
991 * supported rate below the requested.
993 testrate_high = ch->ch_bd->bd_dividend / testdiv;
994 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
997 * If the rate for the requested divisor is correct, just
998 * use it and be done.
1000 if (testrate_high != newrate) {
1002 * Otherwise, pick the rate that is closer (i.e. whichever rate
1003 * has a smaller delta).
1005 deltahigh = testrate_high - newrate;
1006 deltalow = newrate - testrate_low;
1008 if (deltahigh < deltalow) {
1009 newrate = testrate_high;
1011 newrate = testrate_low;
1016 ch->ch_custom_speed = newrate;
1020 void dgnc_check_queue_flow_control(struct channel_t *ch)
1024 /* Store how much space we have left in the queue */
1025 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
1027 qleft += RQUEUEMASK + 1;
1030 * Check to see if we should enforce flow control on our queue because
1031 * the ld (or user) isn't reading data out of our queue fast enuf.
1033 * NOTE: This is done based on what the current flow control of the
1036 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
1037 * This will cause the UART's FIFO to back up, and force
1038 * the RTS signal to be dropped.
1039 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
1040 * the other side, in hopes it will stop sending data to us.
1041 * 3) NONE - Nothing we can do. We will simply drop any extra data
1042 * that gets sent into us when the queue fills up.
1046 if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
1047 if(!(ch->ch_flags & CH_RECEIVER_OFF)) {
1048 ch->ch_bd->bd_ops->disable_receiver(ch);
1049 ch->ch_flags |= (CH_RECEIVER_OFF);
1050 DPR_READ(("Internal queue hit hilevel mark (%d)! Turning off interrupts.\n",
1055 else if (ch->ch_c_iflag & IXOFF) {
1056 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
1057 ch->ch_bd->bd_ops->send_stop_character(ch);
1058 ch->ch_stops_sent++;
1059 DPR_READ(("Sending stop char! Times sent: %x\n", ch->ch_stops_sent));
1064 /* Empty... Can't do anything about the impending overflow... */
1069 * Check to see if we should unenforce flow control because
1070 * ld (or user) finally read enuf data out of our queue.
1072 * NOTE: This is done based on what the current flow control of the
1075 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
1076 * This will cause the UART's FIFO to raise RTS back up,
1077 * which will allow the other side to start sending data again.
1078 * 2) SWFLOW (IXOFF) - Send a start character to
1079 * the other side, so it will start sending data to us again.
1080 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
1081 * other side, we don't need to do anything now.
1083 if (qleft > (RQUEUESIZE / 2)) {
1085 if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
1086 if (ch->ch_flags & CH_RECEIVER_OFF) {
1087 ch->ch_bd->bd_ops->enable_receiver(ch);
1088 ch->ch_flags &= ~(CH_RECEIVER_OFF);
1089 DPR_READ(("Internal queue hit lowlevel mark (%d)! Turning on interrupts.\n",
1094 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
1095 ch->ch_stops_sent = 0;
1096 ch->ch_bd->bd_ops->send_start_character(ch);
1097 DPR_READ(("Sending start char!\n"));
1101 /* Nothing needed. */
1107 void dgnc_wakeup_writes(struct channel_t *ch)
1112 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1115 DGNC_LOCK(ch->ch_lock, lock_flags);
1118 * If channel now has space, wake up anyone waiting on the condition.
1120 qlen = ch->ch_w_head - ch->ch_w_tail;
1124 if (qlen >= (WQUEUESIZE - 256)) {
1125 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1129 if (ch->ch_tun.un_flags & UN_ISOPEN) {
1130 if ((ch->ch_tun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1131 ch->ch_tun.un_tty->ldisc->ops->write_wakeup)
1133 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1134 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
1135 DGNC_LOCK(ch->ch_lock, lock_flags);
1138 wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
1141 * If unit is set to wait until empty, check to make sure
1142 * the queue AND FIFO are both empty.
1144 if (ch->ch_tun.un_flags & UN_EMPTY) {
1145 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
1146 ch->ch_tun.un_flags &= ~(UN_EMPTY);
1149 * If RTS Toggle mode is on, whenever
1150 * the queue and UART is empty, keep RTS low.
1152 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1153 ch->ch_mostat &= ~(UART_MCR_RTS);
1154 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1158 * If DTR Toggle mode is on, whenever
1159 * the queue and UART is empty, keep DTR low.
1161 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1162 ch->ch_mostat &= ~(UART_MCR_DTR);
1163 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1168 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
1171 if (ch->ch_pun.un_flags & UN_ISOPEN) {
1172 if ((ch->ch_pun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1173 ch->ch_pun.un_tty->ldisc->ops->write_wakeup)
1175 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1176 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
1177 DGNC_LOCK(ch->ch_lock, lock_flags);
1180 wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
1183 * If unit is set to wait until empty, check to make sure
1184 * the queue AND FIFO are both empty.
1186 if (ch->ch_pun.un_flags & UN_EMPTY) {
1187 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
1188 ch->ch_pun.un_flags &= ~(UN_EMPTY);
1192 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1195 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1200 /************************************************************************
1202 * TTY Entry points and helper functions
1204 ************************************************************************/
1210 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
1212 struct dgnc_board *brd;
1213 struct channel_t *ch;
1222 major = MAJOR(tty_devnum(tty));
1223 minor = MINOR(tty_devnum(tty));
1229 /* Get board pointer from our array of majors we have allocated */
1230 brd = dgnc_BoardsByMajor[major];
1236 * If board is not yet up to a state of READY, go to
1237 * sleep waiting for it to happen or they cancel the open.
1239 rc = wait_event_interruptible(brd->state_wait,
1240 (brd->state & BOARD_READY));
1246 DGNC_LOCK(brd->bd_lock, lock_flags);
1248 /* If opened device is greater than our number of ports, bail. */
1249 if (PORT_NUM(minor) > brd->nasync) {
1250 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1254 ch = brd->channels[PORT_NUM(minor)];
1256 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1260 /* Drop board lock */
1261 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1263 /* Grab channel lock */
1264 DGNC_LOCK(ch->ch_lock, lock_flags);
1266 /* Figure out our type */
1267 if (!IS_PRINT(minor)) {
1268 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1269 un->un_type = DGNC_SERIAL;
1271 else if (IS_PRINT(minor)) {
1272 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1273 un->un_type = DGNC_PRINT;
1276 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1277 DPR_OPEN(("%d Unknown TYPE!\n", __LINE__));
1282 * If the port is still in a previous open, and in a state
1283 * where we simply cannot safely keep going, wait until the
1286 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1288 rc = wait_event_interruptible(ch->ch_flags_wait, ((ch->ch_flags & CH_OPENING) == 0));
1290 /* If ret is non-zero, user ctrl-c'ed us */
1292 DPR_OPEN(("%d User ctrl c'ed\n", __LINE__));
1297 * If either unit is in the middle of the fragile part of close,
1298 * we just cannot touch the channel safely.
1299 * Go to sleep, knowing that when the channel can be
1300 * touched safely, the close routine will signal the
1301 * ch_flags_wait to wake us back up.
1303 rc = wait_event_interruptible(ch->ch_flags_wait,
1304 (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING) == 0));
1306 /* If ret is non-zero, user ctrl-c'ed us */
1308 DPR_OPEN(("%d User ctrl c'ed\n", __LINE__));
1312 DGNC_LOCK(ch->ch_lock, lock_flags);
1315 /* Store our unit into driver_data, so we always have it available. */
1316 tty->driver_data = un;
1318 DPR_OPEN(("Open called. MAJOR: %d MINOR:%d PORT_NUM: %x unit: %p NAME: %s\n",
1319 MAJOR(tty_devnum(tty)), MINOR(tty_devnum(tty)), PORT_NUM(minor), un, brd->name));
1321 DPR_OPEN(("%d: tflag=%x pflag=%x\n", __LINE__, ch->ch_tun.un_flags, ch->ch_pun.un_flags));
1326 if (!(un->un_flags & UN_ISOPEN)) {
1327 /* Store important variables. */
1330 /* Maybe do something here to the TTY struct as well? */
1335 * Allocate channel buffers for read/write/error.
1336 * Set flag, so we don't get trounced on.
1338 ch->ch_flags |= (CH_OPENING);
1340 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1341 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1344 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1346 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1348 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1350 DGNC_LOCK(ch->ch_lock, lock_flags);
1352 ch->ch_flags &= ~(CH_OPENING);
1353 wake_up_interruptible(&ch->ch_flags_wait);
1356 * Initialize if neither terminal or printer is open.
1358 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1360 DPR_OPEN(("dgnc_open: initializing channel in open...\n"));
1363 * Flush input queues.
1365 ch->ch_r_head = ch->ch_r_tail = 0;
1366 ch->ch_e_head = ch->ch_e_tail = 0;
1367 ch->ch_w_head = ch->ch_w_tail = 0;
1369 brd->bd_ops->flush_uart_write(ch);
1370 brd->bd_ops->flush_uart_read(ch);
1373 ch->ch_cached_lsr = 0;
1374 ch->ch_stop_sending_break = 0;
1375 ch->ch_stops_sent = 0;
1377 ch->ch_c_cflag = tty->termios.c_cflag;
1378 ch->ch_c_iflag = tty->termios.c_iflag;
1379 ch->ch_c_oflag = tty->termios.c_oflag;
1380 ch->ch_c_lflag = tty->termios.c_lflag;
1381 ch->ch_startc = tty->termios.c_cc[VSTART];
1382 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1385 * Bring up RTS and DTR...
1386 * Also handle RTS or DTR toggle if set.
1388 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1389 ch->ch_mostat |= (UART_MCR_RTS);
1390 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1391 ch->ch_mostat |= (UART_MCR_DTR);
1393 /* Tell UART to init itself */
1394 brd->bd_ops->uart_init(ch);
1398 * Run param in case we changed anything
1400 brd->bd_ops->param(tty);
1405 * follow protocol for opening port
1408 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1410 rc = dgnc_block_til_ready(tty, file, ch);
1413 DPR_OPEN(("dgnc_tty_open returning after dgnc_block_til_ready "
1417 /* No going back now, increment our unit and channel counters */
1418 DGNC_LOCK(ch->ch_lock, lock_flags);
1419 ch->ch_open_count++;
1420 un->un_open_count++;
1421 un->un_flags |= (UN_ISOPEN);
1422 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1424 DPR_OPEN(("dgnc_tty_open finished\n"));
1430 * dgnc_block_til_ready()
1432 * Wait for DCD, if needed.
1434 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch)
1437 struct un_t *un = NULL;
1440 int sleep_on_un_flags = 0;
1442 if (!tty || tty->magic != TTY_MAGIC || !file || !ch || ch->magic != DGNC_CHANNEL_MAGIC) {
1446 un = tty->driver_data;
1447 if (!un || un->magic != DGNC_UNIT_MAGIC) {
1451 DPR_OPEN(("dgnc_block_til_ready - before block.\n"));
1453 DGNC_LOCK(ch->ch_lock, lock_flags);
1460 sleep_on_un_flags = 0;
1463 * If board has failed somehow during our sleep, bail with error.
1465 if (ch->ch_bd->state == BOARD_FAILED) {
1470 /* If tty was hung up, break out of loop and set error. */
1471 if (tty_hung_up_p(file)) {
1477 * If either unit is in the middle of the fragile part of close,
1478 * we just cannot touch the channel safely.
1479 * Go back to sleep, knowing that when the channel can be
1480 * touched safely, the close routine will signal the
1481 * ch_wait_flags to wake us back up.
1483 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING)) {
1486 * Our conditions to leave cleanly and happily:
1487 * 1) NONBLOCKING on the tty is set.
1489 * 3) DCD (fake or real) is active.
1492 if (file->f_flags & O_NONBLOCK) {
1496 if (tty->flags & (1 << TTY_IO_ERROR)) {
1501 if (ch->ch_flags & CH_CD) {
1502 DPR_OPEN(("%d: ch_flags: %x\n", __LINE__, ch->ch_flags));
1506 if (ch->ch_flags & CH_FCAR) {
1507 DPR_OPEN(("%d: ch_flags: %x\n", __LINE__, ch->ch_flags));
1512 sleep_on_un_flags = 1;
1516 * If there is a signal pending, the user probably
1517 * interrupted (ctrl-c) us.
1518 * Leave loop with error set.
1520 if (signal_pending(current)) {
1521 DPR_OPEN(("%d: signal pending...\n", __LINE__));
1522 retval = -ERESTARTSYS;
1526 DPR_OPEN(("dgnc_block_til_ready - blocking.\n"));
1529 * Store the flags before we let go of channel lock
1531 if (sleep_on_un_flags)
1532 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1534 old_flags = ch->ch_flags;
1537 * Let go of channel lock before calling schedule.
1538 * Our poller will get any FEP events and wake us up when DCD
1539 * eventually goes active.
1542 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1544 DPR_OPEN(("Going to sleep on %s flags...\n",
1545 (sleep_on_un_flags ? "un" : "ch")));
1548 * Wait for something in the flags to change from the current value.
1550 if (sleep_on_un_flags) {
1551 retval = wait_event_interruptible(un->un_flags_wait,
1552 (old_flags != (ch->ch_tun.un_flags | ch->ch_pun.un_flags)));
1555 retval = wait_event_interruptible(ch->ch_flags_wait,
1556 (old_flags != ch->ch_flags));
1559 DPR_OPEN(("After sleep... retval: %x\n", retval));
1562 * We got woken up for some reason.
1563 * Before looping around, grab our channel lock.
1565 DGNC_LOCK(ch->ch_lock, lock_flags);
1570 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1572 DPR_OPEN(("dgnc_block_til_ready - after blocking.\n"));
1575 DPR_OPEN(("dgnc_block_til_ready - done. error. retval: %x\n", retval));
1579 DPR_OPEN(("dgnc_block_til_ready - done no error. jiffies: %lu\n", jiffies));
1588 * Hangup the port. Like a close, but don't wait for output to drain.
1590 static void dgnc_tty_hangup(struct tty_struct *tty)
1594 if (!tty || tty->magic != TTY_MAGIC)
1597 un = tty->driver_data;
1598 if (!un || un->magic != DGNC_UNIT_MAGIC)
1601 DPR_CLOSE(("dgnc_hangup called. ch->ch_open_count: %d un->un_open_count: %d\n",
1602 un->un_ch->ch_open_count, un->un_open_count));
1604 /* flush the transmit queues */
1605 dgnc_tty_flush_buffer(tty);
1607 DPR_CLOSE(("dgnc_hangup finished. ch->ch_open_count: %d un->un_open_count: %d\n",
1608 un->un_ch->ch_open_count, un->un_open_count));
1616 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1618 struct ktermios *ts;
1619 struct dgnc_board *bd;
1620 struct channel_t *ch;
1625 if (!tty || tty->magic != TTY_MAGIC)
1628 un = tty->driver_data;
1629 if (!un || un->magic != DGNC_UNIT_MAGIC)
1633 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1637 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1642 DPR_CLOSE(("Close called\n"));
1644 DGNC_LOCK(ch->ch_lock, lock_flags);
1647 * Determine if this is the last close or not - and if we agree about
1648 * which type of close it is with the Line Discipline
1650 if ((tty->count == 1) && (un->un_open_count != 1)) {
1652 * Uh, oh. tty->count is 1, which means that the tty
1653 * structure will be freed. un_open_count should always
1654 * be one in these conditions. If it's greater than
1655 * one, we've got real problems, since it means the
1656 * serial port won't be shutdown.
1658 APR(("tty->count is 1, un open count is %d\n", un->un_open_count));
1659 un->un_open_count = 1;
1662 if (--un->un_open_count < 0) {
1663 APR(("bad serial port open count of %d\n", un->un_open_count));
1664 un->un_open_count = 0;
1667 ch->ch_open_count--;
1669 if (ch->ch_open_count && un->un_open_count) {
1670 DPR_CLOSE(("dgnc_tty_close: not last close ch: %d un:%d\n",
1671 ch->ch_open_count, un->un_open_count));
1673 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1677 /* OK, its the last close on the unit */
1678 DPR_CLOSE(("dgnc_tty_close - last close on unit procedures\n"));
1680 un->un_flags |= UN_CLOSING;
1686 * Only officially close channel if count is 0 and
1687 * DIGI_PRINTER bit is not set.
1689 if ((ch->ch_open_count == 0) && !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1691 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1694 * turn off print device when closing print device.
1696 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON) ) {
1697 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1698 (int) ch->ch_digi.digi_offlen);
1699 ch->ch_flags &= ~CH_PRON;
1702 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1703 /* wait for output to drain */
1704 /* This will also return if we take an interrupt */
1706 DPR_CLOSE(("Calling wait_for_drain\n"));
1707 rc = bd->bd_ops->drain(tty, 0);
1709 DPR_CLOSE(("After calling wait_for_drain\n"));
1712 DPR_BASIC(("dgnc_tty_close - bad return: %d ", rc));
1715 dgnc_tty_flush_buffer(tty);
1716 tty_ldisc_flush(tty);
1718 DGNC_LOCK(ch->ch_lock, lock_flags);
1723 * If we have HUPCL set, lower DTR and RTS
1725 if (ch->ch_c_cflag & HUPCL) {
1726 DPR_CLOSE(("Close. HUPCL set, dropping DTR/RTS\n"));
1729 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1730 bd->bd_ops->assert_modem_signals(ch);
1733 * Go to sleep to ensure RTS/DTR
1734 * have been dropped for modems to see it.
1736 if (ch->ch_close_delay) {
1737 DPR_CLOSE(("Close. Sleeping for RTS/DTR drop\n"));
1739 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1740 dgnc_ms_sleep(ch->ch_close_delay);
1741 DGNC_LOCK(ch->ch_lock, lock_flags);
1743 DPR_CLOSE(("Close. After sleeping for RTS/DTR drop\n"));
1747 ch->ch_old_baud = 0;
1749 /* Turn off UART interrupts for this port */
1750 ch->ch_bd->bd_ops->uart_off(ch);
1754 * turn off print device when closing print device.
1756 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON) ) {
1757 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1758 (int) ch->ch_digi.digi_offlen);
1759 ch->ch_flags &= ~CH_PRON;
1764 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1766 DPR_CLOSE(("Close. Doing wakeups\n"));
1767 wake_up_interruptible(&ch->ch_flags_wait);
1768 wake_up_interruptible(&un->un_flags_wait);
1770 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1772 DPR_BASIC(("dgnc_tty_close - complete\n"));
1777 * dgnc_tty_chars_in_buffer()
1779 * Return number of characters that have not been transmitted yet.
1781 * This routine is used by the line discipline to determine if there
1782 * is data waiting to be transmitted/drained/flushed or not.
1784 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1786 struct channel_t *ch = NULL;
1787 struct un_t *un = NULL;
1792 ulong lock_flags = 0;
1797 un = tty->driver_data;
1798 if (!un || un->magic != DGNC_UNIT_MAGIC)
1802 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1805 DGNC_LOCK(ch->ch_lock, lock_flags);
1808 thead = ch->ch_w_head & tmask;
1809 ttail = ch->ch_w_tail & tmask;
1811 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1813 if (ttail == thead) {
1817 chars = thead - ttail;
1819 chars = thead - ttail + WQUEUESIZE;
1822 DPR_WRITE(("dgnc_tty_chars_in_buffer. Port: %x - %d (head: %d tail: %d)\n",
1823 ch->ch_portnum, chars, thead, ttail));
1832 * Reduces bytes_available to the max number of characters
1833 * that can be sent currently given the maxcps value, and
1834 * returns the new bytes_available. This only affects printer
1837 static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1839 struct channel_t *ch = NULL;
1840 struct un_t *un = NULL;
1843 return bytes_available;
1845 un = tty->driver_data;
1846 if (!un || un->magic != DGNC_UNIT_MAGIC)
1847 return bytes_available;
1850 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1851 return bytes_available;
1854 * If its not the Transparent print device, return
1855 * the full data amount.
1857 if (un->un_type != DGNC_PRINT)
1858 return bytes_available;
1860 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0 ) {
1862 unsigned long current_time = jiffies;
1863 unsigned long buffer_time = current_time +
1864 (HZ * ch->ch_digi.digi_bufsize) / ch->ch_digi.digi_maxcps;
1866 if (ch->ch_cpstime < current_time) {
1867 /* buffer is empty */
1868 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1869 cps_limit = ch->ch_digi.digi_bufsize;
1871 else if (ch->ch_cpstime < buffer_time) {
1872 /* still room in the buffer */
1873 cps_limit = ((buffer_time - ch->ch_cpstime) * ch->ch_digi.digi_maxcps) / HZ;
1876 /* no room in the buffer */
1880 bytes_available = min(cps_limit, bytes_available);
1883 return bytes_available;
1888 * dgnc_tty_write_room()
1890 * Return space available in Tx buffer
1892 static int dgnc_tty_write_room(struct tty_struct *tty)
1894 struct channel_t *ch = NULL;
1895 struct un_t *un = NULL;
1900 ulong lock_flags = 0;
1902 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1905 un = tty->driver_data;
1906 if (!un || un->magic != DGNC_UNIT_MAGIC)
1910 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1913 DGNC_LOCK(ch->ch_lock, lock_flags);
1916 head = (ch->ch_w_head) & tmask;
1917 tail = (ch->ch_w_tail) & tmask;
1919 ret = tail - head - 1;
1923 /* Limit printer to maxcps */
1924 ret = dgnc_maxcps_room(tty, ret);
1927 * If we are printer device, leave space for
1928 * possibly both the on and off strings.
1930 if (un->un_type == DGNC_PRINT) {
1931 if (!(ch->ch_flags & CH_PRON))
1932 ret -= ch->ch_digi.digi_onlen;
1933 ret -= ch->ch_digi.digi_offlen;
1936 if (ch->ch_flags & CH_PRON)
1937 ret -= ch->ch_digi.digi_offlen;
1943 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1945 DPR_WRITE(("dgnc_tty_write_room - %d tail: %d head: %d\n", ret, tail, head));
1952 * dgnc_tty_put_char()
1954 * Put a character into ch->ch_buf
1956 * - used by the line discipline for OPOST processing
1958 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1961 * Simply call tty_write.
1963 DPR_WRITE(("dgnc_tty_put_char called\n"));
1964 dgnc_tty_write(tty, &c, 1);
1972 * Take data from the user or kernel and send it out to the FEP.
1973 * In here exists all the Transparent Print magic as well.
1975 static int dgnc_tty_write(struct tty_struct *tty,
1976 const unsigned char *buf, int count)
1978 struct channel_t *ch = NULL;
1979 struct un_t *un = NULL;
1980 int bufcount = 0, n = 0;
1989 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1992 un = tty->driver_data;
1993 if (!un || un->magic != DGNC_UNIT_MAGIC)
1997 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2003 DPR_WRITE(("dgnc_tty_write: Port: %x tty=%p user=%d len=%d\n",
2004 ch->ch_portnum, tty, from_user, count));
2007 * Store original amount of characters passed in.
2008 * This helps to figure out if we should ask the FEP
2009 * to send us an event when it has more space available.
2013 DGNC_LOCK(ch->ch_lock, lock_flags);
2015 /* Get our space available for the channel from the board */
2017 head = (ch->ch_w_head) & tmask;
2018 tail = (ch->ch_w_tail) & tmask;
2020 bufcount = tail - head - 1;
2022 bufcount += WQUEUESIZE;
2024 DPR_WRITE(("%d: bufcount: %x count: %x tail: %x head: %x tmask: %x\n",
2025 __LINE__, bufcount, count, tail, head, tmask));
2028 * Limit printer output to maxcps overall, with bursts allowed
2029 * up to bufsize characters.
2031 bufcount = dgnc_maxcps_room(tty, bufcount);
2034 * Take minimum of what the user wants to send, and the
2035 * space available in the FEP buffer.
2037 count = min(count, bufcount);
2040 * Bail if no space left.
2043 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2048 * Output the printer ON string, if we are in terminal mode, but
2049 * need to be in printer mode.
2051 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
2052 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
2053 (int) ch->ch_digi.digi_onlen);
2054 head = (ch->ch_w_head) & tmask;
2055 ch->ch_flags |= CH_PRON;
2059 * On the other hand, output the printer OFF string, if we are
2060 * currently in printer mode, but need to output to the terminal.
2062 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
2063 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
2064 (int) ch->ch_digi.digi_offlen);
2065 head = (ch->ch_w_head) & tmask;
2066 ch->ch_flags &= ~CH_PRON;
2070 * If there is nothing left to copy, or I can't handle any more data, leave.
2073 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2079 count = min(count, WRITEBUFLEN);
2081 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2084 * If data is coming from user space, copy it into a temporary
2085 * buffer so we don't get swapped out while doing the copy to
2088 /* we're allowed to block if it's from_user */
2089 if (down_interruptible(&dgnc_TmpWriteSem)) {
2094 * copy_from_user() returns the number
2095 * of bytes that could *NOT* be copied.
2097 count -= copy_from_user(dgnc_TmpWriteBuf, (const uchar __user *) buf, count);
2100 up(&dgnc_TmpWriteSem);
2104 DGNC_LOCK(ch->ch_lock, lock_flags);
2106 buf = dgnc_TmpWriteBuf;
2113 * If the write wraps over the top of the circular buffer,
2114 * move the portion up to the wrap point, and reset the
2115 * pointers to the bottom.
2117 remain = WQUEUESIZE - head;
2121 memcpy(ch->ch_wqueue + head, buf, remain);
2122 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
2129 * Move rest of data.
2132 memcpy(ch->ch_wqueue + head, buf, remain);
2133 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
2139 ch->ch_w_head = head;
2144 * If this is the print device, and the
2145 * printer is still on, we need to turn it
2146 * off before going idle.
2148 if (count == orig_count) {
2149 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
2151 ch->ch_w_head = head;
2152 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
2153 (int) ch->ch_digi.digi_offlen);
2154 head = (ch->ch_w_head) & tmask;
2155 ch->ch_flags &= ~CH_PRON;
2160 /* Update printer buffer empty time. */
2161 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
2162 && (ch->ch_digi.digi_bufsize > 0)) {
2163 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
2167 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2168 up(&dgnc_TmpWriteSem);
2170 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2173 DPR_WRITE(("Write finished - Write %d bytes of %d.\n", count, orig_count));
2177 * Channel lock is grabbed and then released
2178 * inside this routine.
2180 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
2188 * Return modem signals to ld.
2191 static int dgnc_tty_tiocmget(struct tty_struct *tty)
2193 struct channel_t *ch;
2199 if (!tty || tty->magic != TTY_MAGIC)
2202 un = tty->driver_data;
2203 if (!un || un->magic != DGNC_UNIT_MAGIC)
2207 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2210 DPR_IOCTL(("dgnc_tty_tiocmget start\n"));
2212 DGNC_LOCK(ch->ch_lock, lock_flags);
2214 mstat = (ch->ch_mostat | ch->ch_mistat);
2216 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2220 if (mstat & UART_MCR_DTR)
2221 result |= TIOCM_DTR;
2222 if (mstat & UART_MCR_RTS)
2223 result |= TIOCM_RTS;
2224 if (mstat & UART_MSR_CTS)
2225 result |= TIOCM_CTS;
2226 if (mstat & UART_MSR_DSR)
2227 result |= TIOCM_DSR;
2228 if (mstat & UART_MSR_RI)
2230 if (mstat & UART_MSR_DCD)
2233 DPR_IOCTL(("dgnc_tty_tiocmget finish\n"));
2240 * dgnc_tty_tiocmset()
2242 * Set modem signals, called by ld.
2245 static int dgnc_tty_tiocmset(struct tty_struct *tty,
2246 unsigned int set, unsigned int clear)
2248 struct dgnc_board *bd;
2249 struct channel_t *ch;
2254 if (!tty || tty->magic != TTY_MAGIC)
2257 un = tty->driver_data;
2258 if (!un || un->magic != DGNC_UNIT_MAGIC)
2262 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2266 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2269 DPR_IOCTL(("dgnc_tty_tiocmset start\n"));
2272 DGNC_LOCK(ch->ch_lock, lock_flags);
2274 if (set & TIOCM_RTS) {
2275 ch->ch_mostat |= UART_MCR_RTS;
2278 if (set & TIOCM_DTR) {
2279 ch->ch_mostat |= UART_MCR_DTR;
2282 if (clear & TIOCM_RTS) {
2283 ch->ch_mostat &= ~(UART_MCR_RTS);
2286 if (clear & TIOCM_DTR) {
2287 ch->ch_mostat &= ~(UART_MCR_DTR);
2290 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2292 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2294 DPR_IOCTL(("dgnc_tty_tiocmset finish\n"));
2301 * dgnc_tty_send_break()
2303 * Send a Break, called by ld.
2305 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
2307 struct dgnc_board *bd;
2308 struct channel_t *ch;
2313 if (!tty || tty->magic != TTY_MAGIC)
2316 un = tty->driver_data;
2317 if (!un || un->magic != DGNC_UNIT_MAGIC)
2321 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2325 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2339 DPR_IOCTL(("dgnc_tty_send_break start 1. %lx\n", jiffies));
2341 DGNC_LOCK(ch->ch_lock, lock_flags);
2343 ch->ch_bd->bd_ops->send_break(ch, msec);
2345 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2347 DPR_IOCTL(("dgnc_tty_send_break finish\n"));
2355 * dgnc_tty_wait_until_sent()
2357 * wait until data has been transmitted, called by ld.
2359 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
2361 struct dgnc_board *bd;
2362 struct channel_t *ch;
2366 if (!tty || tty->magic != TTY_MAGIC)
2369 un = tty->driver_data;
2370 if (!un || un->magic != DGNC_UNIT_MAGIC)
2374 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2378 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2381 rc = bd->bd_ops->drain(tty, 0);
2383 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
2393 * send a high priority character, called by ld.
2395 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
2397 struct dgnc_board *bd;
2398 struct channel_t *ch;
2402 if (!tty || tty->magic != TTY_MAGIC)
2405 un = tty->driver_data;
2406 if (!un || un->magic != DGNC_UNIT_MAGIC)
2410 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2414 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2417 DPR_IOCTL(("dgnc_tty_send_xchar start\n"));
2418 printk("dgnc_tty_send_xchar start\n");
2420 DGNC_LOCK(ch->ch_lock, lock_flags);
2421 bd->bd_ops->send_immediate_char(ch, c);
2422 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2424 DPR_IOCTL(("dgnc_tty_send_xchar finish\n"));
2425 printk("dgnc_tty_send_xchar finish\n");
2433 * Return modem signals to ld.
2435 static inline int dgnc_get_mstat(struct channel_t *ch)
2437 unsigned char mstat;
2441 DPR_IOCTL(("dgnc_getmstat start\n"));
2443 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2446 DGNC_LOCK(ch->ch_lock, lock_flags);
2448 mstat = (ch->ch_mostat | ch->ch_mistat);
2450 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2454 if (mstat & UART_MCR_DTR)
2455 result |= TIOCM_DTR;
2456 if (mstat & UART_MCR_RTS)
2457 result |= TIOCM_RTS;
2458 if (mstat & UART_MSR_CTS)
2459 result |= TIOCM_CTS;
2460 if (mstat & UART_MSR_DSR)
2461 result |= TIOCM_DSR;
2462 if (mstat & UART_MSR_RI)
2464 if (mstat & UART_MSR_DCD)
2467 DPR_IOCTL(("dgnc_getmstat finish\n"));
2475 * Return modem signals to ld.
2477 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value)
2482 DPR_IOCTL(("dgnc_get_modem_info start\n"));
2484 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2487 result = dgnc_get_mstat(ch);
2492 rc = put_user(result, value);
2494 DPR_IOCTL(("dgnc_get_modem_info finish\n"));
2500 * dgnc_set_modem_info()
2502 * Set modem signals, called by ld.
2504 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value)
2506 struct dgnc_board *bd;
2507 struct channel_t *ch;
2510 unsigned int arg = 0;
2513 if (!tty || tty->magic != TTY_MAGIC)
2516 un = tty->driver_data;
2517 if (!un || un->magic != DGNC_UNIT_MAGIC)
2521 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2525 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2530 DPR_IOCTL(("dgnc_set_modem_info() start\n"));
2532 ret = get_user(arg, value);
2538 if (arg & TIOCM_RTS) {
2539 ch->ch_mostat |= UART_MCR_RTS;
2542 if (arg & TIOCM_DTR) {
2543 ch->ch_mostat |= UART_MCR_DTR;
2549 if (arg & TIOCM_RTS) {
2550 ch->ch_mostat &= ~(UART_MCR_RTS);
2553 if (arg & TIOCM_DTR) {
2554 ch->ch_mostat &= ~(UART_MCR_DTR);
2561 if (arg & TIOCM_RTS) {
2562 ch->ch_mostat |= UART_MCR_RTS;
2565 ch->ch_mostat &= ~(UART_MCR_RTS);
2568 if (arg & TIOCM_DTR) {
2569 ch->ch_mostat |= UART_MCR_DTR;
2572 ch->ch_mostat &= ~(UART_MCR_DTR);
2581 DGNC_LOCK(ch->ch_lock, lock_flags);
2583 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2585 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2587 DPR_IOCTL(("dgnc_set_modem_info finish\n"));
2594 * dgnc_tty_digigeta()
2596 * Ioctl to get the information for ditty.
2601 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo)
2603 struct channel_t *ch;
2611 if (!tty || tty->magic != TTY_MAGIC)
2614 un = tty->driver_data;
2615 if (!un || un->magic != DGNC_UNIT_MAGIC)
2619 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2622 memset(&tmp, 0, sizeof(tmp));
2624 DGNC_LOCK(ch->ch_lock, lock_flags);
2625 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2626 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2628 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2636 * dgnc_tty_digiseta()
2638 * Ioctl to set the information for ditty.
2643 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info)
2645 struct dgnc_board *bd;
2646 struct channel_t *ch;
2648 struct digi_t new_digi;
2651 DPR_IOCTL(("DIGI_SETA start\n"));
2653 if (!tty || tty->magic != TTY_MAGIC)
2656 un = tty->driver_data;
2657 if (!un || un->magic != DGNC_UNIT_MAGIC)
2661 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2665 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2668 if (copy_from_user(&new_digi, new_info, sizeof(new_digi))) {
2669 DPR_IOCTL(("DIGI_SETA failed copy_from_user\n"));
2673 DGNC_LOCK(ch->ch_lock, lock_flags);
2676 * Handle transistions to and from RTS Toggle.
2678 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2679 ch->ch_mostat &= ~(UART_MCR_RTS);
2680 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2681 ch->ch_mostat |= (UART_MCR_RTS);
2684 * Handle transistions to and from DTR Toggle.
2686 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2687 ch->ch_mostat &= ~(UART_MCR_DTR);
2688 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2689 ch->ch_mostat |= (UART_MCR_DTR);
2691 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2693 if (ch->ch_digi.digi_maxcps < 1)
2694 ch->ch_digi.digi_maxcps = 1;
2696 if (ch->ch_digi.digi_maxcps > 10000)
2697 ch->ch_digi.digi_maxcps = 10000;
2699 if (ch->ch_digi.digi_bufsize < 10)
2700 ch->ch_digi.digi_bufsize = 10;
2702 if (ch->ch_digi.digi_maxchar < 1)
2703 ch->ch_digi.digi_maxchar = 1;
2705 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2706 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2708 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2709 ch->ch_digi.digi_onlen = DIGI_PLEN;
2711 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2712 ch->ch_digi.digi_offlen = DIGI_PLEN;
2714 ch->ch_bd->bd_ops->param(tty);
2716 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2718 DPR_IOCTL(("DIGI_SETA finish\n"));
2725 * dgnc_set_termios()
2727 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2729 struct dgnc_board *bd;
2730 struct channel_t *ch;
2732 unsigned long lock_flags;
2734 if (!tty || tty->magic != TTY_MAGIC)
2737 un = tty->driver_data;
2738 if (!un || un->magic != DGNC_UNIT_MAGIC)
2742 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2746 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2749 DGNC_LOCK(ch->ch_lock, lock_flags);
2751 ch->ch_c_cflag = tty->termios.c_cflag;
2752 ch->ch_c_iflag = tty->termios.c_iflag;
2753 ch->ch_c_oflag = tty->termios.c_oflag;
2754 ch->ch_c_lflag = tty->termios.c_lflag;
2755 ch->ch_startc = tty->termios.c_cc[VSTART];
2756 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2758 ch->ch_bd->bd_ops->param(tty);
2761 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2765 static void dgnc_tty_throttle(struct tty_struct *tty)
2767 struct channel_t *ch;
2769 ulong lock_flags = 0;
2771 if (!tty || tty->magic != TTY_MAGIC)
2774 un = tty->driver_data;
2775 if (!un || un->magic != DGNC_UNIT_MAGIC)
2779 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2782 DPR_IOCTL(("dgnc_tty_throttle start\n"));
2784 DGNC_LOCK(ch->ch_lock, lock_flags);
2786 ch->ch_flags |= (CH_FORCED_STOPI);
2788 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2790 DPR_IOCTL(("dgnc_tty_throttle finish\n"));
2794 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2796 struct channel_t *ch;
2800 if (!tty || tty->magic != TTY_MAGIC)
2803 un = tty->driver_data;
2804 if (!un || un->magic != DGNC_UNIT_MAGIC)
2808 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2811 DPR_IOCTL(("dgnc_tty_unthrottle start\n"));
2813 DGNC_LOCK(ch->ch_lock, lock_flags);
2815 ch->ch_flags &= ~(CH_FORCED_STOPI);
2817 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2819 DPR_IOCTL(("dgnc_tty_unthrottle finish\n"));
2823 static void dgnc_tty_start(struct tty_struct *tty)
2825 struct dgnc_board *bd;
2826 struct channel_t *ch;
2830 if (!tty || tty->magic != TTY_MAGIC)
2833 un = tty->driver_data;
2834 if (!un || un->magic != DGNC_UNIT_MAGIC)
2838 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2842 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2845 DPR_IOCTL(("dgcn_tty_start start\n"));
2847 DGNC_LOCK(ch->ch_lock, lock_flags);
2849 ch->ch_flags &= ~(CH_FORCED_STOP);
2851 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2853 DPR_IOCTL(("dgnc_tty_start finish\n"));
2857 static void dgnc_tty_stop(struct tty_struct *tty)
2859 struct dgnc_board *bd;
2860 struct channel_t *ch;
2864 if (!tty || tty->magic != TTY_MAGIC)
2867 un = tty->driver_data;
2868 if (!un || un->magic != DGNC_UNIT_MAGIC)
2872 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2876 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2879 DPR_IOCTL(("dgnc_tty_stop start\n"));
2881 DGNC_LOCK(ch->ch_lock, lock_flags);
2883 ch->ch_flags |= (CH_FORCED_STOP);
2885 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2887 DPR_IOCTL(("dgnc_tty_stop finish\n"));
2892 * dgnc_tty_flush_chars()
2894 * Flush the cook buffer
2896 * Note to self, and any other poor souls who venture here:
2898 * flush in this case DOES NOT mean dispose of the data.
2899 * instead, it means "stop buffering and send it if you
2900 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2902 * It is also always called in interrupt context - JAR 8-Sept-99
2904 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2906 struct dgnc_board *bd;
2907 struct channel_t *ch;
2911 if (!tty || tty->magic != TTY_MAGIC)
2914 un = tty->driver_data;
2915 if (!un || un->magic != DGNC_UNIT_MAGIC)
2919 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2923 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2926 DPR_IOCTL(("dgnc_tty_flush_chars start\n"));
2928 DGNC_LOCK(ch->ch_lock, lock_flags);
2930 /* Do something maybe here */
2932 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2934 DPR_IOCTL(("dgnc_tty_flush_chars finish\n"));
2940 * dgnc_tty_flush_buffer()
2942 * Flush Tx buffer (make in == out)
2944 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2946 struct channel_t *ch;
2950 if (!tty || tty->magic != TTY_MAGIC)
2953 un = tty->driver_data;
2954 if (!un || un->magic != DGNC_UNIT_MAGIC)
2958 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2961 DPR_IOCTL(("dgnc_tty_flush_buffer on port: %d start\n", ch->ch_portnum));
2963 DGNC_LOCK(ch->ch_lock, lock_flags);
2965 ch->ch_flags &= ~CH_STOP;
2967 /* Flush our write queue */
2968 ch->ch_w_head = ch->ch_w_tail;
2970 /* Flush UARTs transmit FIFO */
2971 ch->ch_bd->bd_ops->flush_uart_write(ch);
2973 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2974 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2975 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2977 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2978 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2979 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2982 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2984 DPR_IOCTL(("dgnc_tty_flush_buffer finish\n"));
2989 /*****************************************************************************
2991 * The IOCTL function and all of its helpers
2993 *****************************************************************************/
2998 * The usual assortment of ioctl's
3000 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
3003 struct dgnc_board *bd;
3004 struct channel_t *ch;
3008 void __user *uarg = (void __user *) arg;
3010 if (!tty || tty->magic != TTY_MAGIC)
3013 un = tty->driver_data;
3014 if (!un || un->magic != DGNC_UNIT_MAGIC)
3018 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
3022 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
3025 DPR_IOCTL(("dgnc_tty_ioctl start on port %d - cmd %s (%x), arg %lx\n",
3026 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3028 DGNC_LOCK(ch->ch_lock, lock_flags);
3030 if (un->un_open_count <= 0) {
3031 DPR_BASIC(("dgnc_tty_ioctl - unit not open.\n"));
3032 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3038 /* Here are all the standard ioctl's that we MUST implement */
3042 * TCSBRK is SVID version: non-zero arg --> no break
3043 * this behaviour is exploited by tcdrain().
3045 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3046 * between 0.25 and 0.5 seconds so we'll ask for something
3047 * in the middle: 0.375 seconds.
3049 rc = tty_check_change(tty);
3050 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3055 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3058 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3062 DGNC_LOCK(ch->ch_lock, lock_flags);
3064 if(((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP)) {
3065 ch->ch_bd->bd_ops->send_break(ch, 250);
3068 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3070 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3071 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3077 /* support for POSIX tcsendbreak()
3078 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3079 * between 0.25 and 0.5 seconds so we'll ask for something
3080 * in the middle: 0.375 seconds.
3082 rc = tty_check_change(tty);
3083 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3088 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3090 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3094 DGNC_LOCK(ch->ch_lock, lock_flags);
3096 ch->ch_bd->bd_ops->send_break(ch, 250);
3098 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3100 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3101 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3106 rc = tty_check_change(tty);
3107 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3112 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3114 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3118 DGNC_LOCK(ch->ch_lock, lock_flags);
3120 ch->ch_bd->bd_ops->send_break(ch, 250);
3122 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3124 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3125 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3131 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3136 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3138 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
3143 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3144 rc = get_user(arg, (unsigned long __user *) arg);
3148 DGNC_LOCK(ch->ch_lock, lock_flags);
3149 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
3150 ch->ch_bd->bd_ops->param(tty);
3151 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3156 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3157 return dgnc_get_modem_info(ch, uarg);
3162 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3163 return dgnc_set_modem_info(tty, cmd, uarg);
3166 * Here are any additional ioctl's that we want to implement
3171 * The linux tty driver doesn't have a flush
3172 * input routine for the driver, assuming all backed
3173 * up data is in the line disc. buffers. However,
3174 * we all know that's not the case. Here, we
3175 * act on the ioctl, but then lie and say we didn't
3176 * so the line discipline will process the flush
3179 rc = tty_check_change(tty);
3181 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3185 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
3186 ch->ch_r_head = ch->ch_r_tail;
3187 ch->ch_bd->bd_ops->flush_uart_read(ch);
3188 /* Force queue flow control to be released, if needed */
3189 dgnc_check_queue_flow_control(ch);
3192 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
3193 if (!(un->un_type == DGNC_PRINT)) {
3194 ch->ch_w_head = ch->ch_w_tail;
3195 ch->ch_bd->bd_ops->flush_uart_write(ch);
3197 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
3198 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
3199 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
3202 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
3203 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
3204 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
3210 /* pretend we didn't recognize this IOCTL */
3211 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3212 return -ENOIOCTLCMD;
3216 * The linux tty driver doesn't have a flush
3217 * input routine for the driver, assuming all backed
3218 * up data is in the line disc. buffers. However,
3219 * we all know that's not the case. Here, we
3220 * act on the ioctl, but then lie and say we didn't
3221 * so the line discipline will process the flush
3224 if (cmd == TCSETSF) {
3226 ch->ch_flags &= ~CH_STOP;
3227 ch->ch_r_head = ch->ch_r_tail;
3228 ch->ch_bd->bd_ops->flush_uart_read(ch);
3229 /* Force queue flow control to be released, if needed */
3230 dgnc_check_queue_flow_control(ch);
3233 /* now wait for all the output to drain */
3234 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3235 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3237 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d\n", rc));
3241 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3242 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3244 /* pretend we didn't recognize this */
3245 return -ENOIOCTLCMD;
3249 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3250 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3252 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3256 /* pretend we didn't recognize this */
3257 return -ENOIOCTLCMD;
3260 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3261 /* Make the ld do it */
3262 return -ENOIOCTLCMD;
3265 /* get information for ditty */
3266 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3267 return dgnc_tty_digigeta(tty, uarg);
3272 /* set information for ditty */
3273 if (cmd == (DIGI_SETAW)) {
3275 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3276 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3278 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3281 DGNC_LOCK(ch->ch_lock, lock_flags);
3284 tty_ldisc_flush(tty);
3289 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3290 return dgnc_tty_digiseta(tty, uarg);
3295 /* Let go of locks when accessing user space, could sleep */
3296 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3297 rc = get_user(loopback, (unsigned int __user *) arg);
3300 DGNC_LOCK(ch->ch_lock, lock_flags);
3302 /* Enable/disable internal loopback for this port */
3304 ch->ch_flags |= CH_LOOPBACK;
3306 ch->ch_flags &= ~(CH_LOOPBACK);
3308 ch->ch_bd->bd_ops->param(tty);
3309 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3313 case DIGI_GETCUSTOMBAUD:
3314 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3315 rc = put_user(ch->ch_custom_speed, (unsigned int __user *) arg);
3318 case DIGI_SETCUSTOMBAUD:
3321 /* Let go of locks when accessing user space, could sleep */
3322 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3323 rc = get_user(new_rate, (int __user *) arg);
3326 DGNC_LOCK(ch->ch_lock, lock_flags);
3327 dgnc_set_custom_speed(ch, new_rate);
3328 ch->ch_bd->bd_ops->param(tty);
3329 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3334 * This ioctl allows insertion of a character into the front
3335 * of any pending data to be transmitted.
3337 * This ioctl is to satify the "Send Character Immediate"
3338 * call that the RealPort protocol spec requires.
3340 case DIGI_REALPORT_SENDIMMEDIATE:
3343 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3344 rc = get_user(c, (unsigned char __user *) arg);
3347 DGNC_LOCK(ch->ch_lock, lock_flags);
3348 ch->ch_bd->bd_ops->send_immediate_char(ch, c);
3349 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3354 * This ioctl returns all the current counts for the port.
3356 * This ioctl is to satify the "Line Error Counters"
3357 * call that the RealPort protocol spec requires.
3359 case DIGI_REALPORT_GETCOUNTERS:
3361 struct digi_getcounter buf;
3363 buf.norun = ch->ch_err_overrun;
3364 buf.noflow = 0; /* The driver doesn't keep this stat */
3365 buf.nframe = ch->ch_err_frame;
3366 buf.nparity = ch->ch_err_parity;
3367 buf.nbreak = ch->ch_err_break;
3368 buf.rbytes = ch->ch_rxcount;
3369 buf.tbytes = ch->ch_txcount;
3371 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3373 if (copy_to_user(uarg, &buf, sizeof(buf))) {
3380 * This ioctl returns all current events.
3382 * This ioctl is to satify the "Event Reporting"
3383 * call that the RealPort protocol spec requires.
3385 case DIGI_REALPORT_GETEVENTS:
3387 unsigned int events = 0;
3389 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
3390 if (ch->ch_flags & CH_BREAK_SENDING)
3392 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP)) {
3393 events |= (EV_OPU | EV_OPS);
3395 if ((ch->ch_flags & CH_STOPI) || (ch->ch_flags & CH_FORCED_STOPI)) {
3396 events |= (EV_IPU | EV_IPS);
3399 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3400 rc = put_user(events, (unsigned int __user *) arg);
3405 * This ioctl returns TOUT and TIN counters based
3406 * upon the values passed in by the RealPort Server.
3407 * It also passes back whether the UART Transmitter is
3410 case DIGI_REALPORT_GETBUFFERS:
3412 struct digi_getbuffer buf;
3416 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3419 * Get data from user first.
3421 if (copy_from_user(&buf, uarg, sizeof(buf))) {
3425 DGNC_LOCK(ch->ch_lock, lock_flags);
3428 * Figure out how much data is in our RX and TX queues.
3430 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
3431 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
3434 * Is the UART empty? Add that value to whats in our TX queue.
3436 count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
3439 * Figure out how much data the RealPort Server believes should
3440 * be in our TX queue.
3442 tdist = (buf.tIn - buf.tOut) & 0xffff;
3445 * If we have more data than the RealPort Server believes we
3446 * should have, reduce our count to its amount.
3448 * This count difference CAN happen because the Linux LD can
3449 * insert more characters into our queue for OPOST processing
3450 * that the RealPort Server doesn't know about.
3452 if (buf.txbuf > tdist) {
3457 * Report whether our queue and UART TX are completely empty.
3465 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3467 if (copy_to_user(uarg, &buf, sizeof(buf))) {
3473 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3475 DPR_IOCTL(("dgnc_tty_ioctl - in default\n"));
3476 DPR_IOCTL(("dgnc_tty_ioctl end - cmd %s (%x), arg %lx\n",
3477 dgnc_ioctl_name(cmd), cmd, arg));
3479 return -ENOIOCTLCMD;
3482 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3484 DPR_IOCTL(("dgnc_tty_ioctl end - cmd %s (%x), arg %lx\n",
3485 dgnc_ioctl_name(cmd), cmd, arg));