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 <linux/uaccess.h> /* For copy_from_user/copy_to_user */
51 #include <linux/pci.h>
53 #include "dgnc_driver.h"
55 #include "dgnc_types.h"
58 #include "dpacompat.h"
59 #include "dgnc_sysfs.h"
60 #include "dgnc_utils.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;
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 = {
97 .c_iflag = (DEFAULT_IFLAGS), /* iflags */
98 .c_oflag = (DEFAULT_OFLAGS), /* oflags */
99 .c_cflag = (DEFAULT_CFLAGS), /* cflags */
100 .c_lflag = (DEFAULT_LFLAGS), /* lflags */
106 /* Our function prototypes */
107 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
108 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
109 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch);
110 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
111 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo);
112 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info);
113 static int dgnc_tty_write_room(struct tty_struct *tty);
114 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
115 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
116 static void dgnc_tty_start(struct tty_struct *tty);
117 static void dgnc_tty_stop(struct tty_struct *tty);
118 static void dgnc_tty_throttle(struct tty_struct *tty);
119 static void dgnc_tty_unthrottle(struct tty_struct *tty);
120 static void dgnc_tty_flush_chars(struct tty_struct *tty);
121 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
122 static void dgnc_tty_hangup(struct tty_struct *tty);
123 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value);
124 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value);
125 static int dgnc_tty_tiocmget(struct tty_struct *tty);
126 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear);
127 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
128 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
129 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf, int count);
130 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios);
131 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
134 static const struct tty_operations dgnc_tty_ops = {
135 .open = dgnc_tty_open,
136 .close = dgnc_tty_close,
137 .write = dgnc_tty_write,
138 .write_room = dgnc_tty_write_room,
139 .flush_buffer = dgnc_tty_flush_buffer,
140 .chars_in_buffer = dgnc_tty_chars_in_buffer,
141 .flush_chars = dgnc_tty_flush_chars,
142 .ioctl = dgnc_tty_ioctl,
143 .set_termios = dgnc_tty_set_termios,
144 .stop = dgnc_tty_stop,
145 .start = dgnc_tty_start,
146 .throttle = dgnc_tty_throttle,
147 .unthrottle = dgnc_tty_unthrottle,
148 .hangup = dgnc_tty_hangup,
149 .put_char = dgnc_tty_put_char,
150 .tiocmget = dgnc_tty_tiocmget,
151 .tiocmset = dgnc_tty_tiocmset,
152 .break_ctl = dgnc_tty_send_break,
153 .wait_until_sent = dgnc_tty_wait_until_sent,
154 .send_xchar = dgnc_tty_send_xchar
157 /************************************************************************
159 * TTY Initialization/Cleanup Functions
161 ************************************************************************/
166 * Initialize any global tty related data before we download any boards.
168 int dgnc_tty_preinit(void)
171 * Allocate a buffer for doing the copy from user space to
172 * kernel space in dgnc_write(). We only use one buffer and
173 * control access to it with a semaphore. If we are paging, we
174 * are already in trouble so one buffer won't hurt much anyway.
176 * We are okay to sleep in the malloc, as this routine
177 * is only called during module load, (not in interrupt context),
178 * and with no locks held.
180 dgnc_TmpWriteBuf = kmalloc(WRITEBUFLEN, GFP_KERNEL);
182 if (!dgnc_TmpWriteBuf)
190 * dgnc_tty_register()
192 * Init the tty subsystem for this board.
194 int dgnc_tty_register(struct dgnc_board *brd)
198 brd->SerialDriver.magic = TTY_DRIVER_MAGIC;
200 snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgnc_%d_", brd->boardnum);
202 brd->SerialDriver.name = brd->SerialName;
203 brd->SerialDriver.name_base = 0;
204 brd->SerialDriver.major = 0;
205 brd->SerialDriver.minor_start = 0;
206 brd->SerialDriver.num = brd->maxports;
207 brd->SerialDriver.type = TTY_DRIVER_TYPE_SERIAL;
208 brd->SerialDriver.subtype = SERIAL_TYPE_NORMAL;
209 brd->SerialDriver.init_termios = DgncDefaultTermios;
210 brd->SerialDriver.driver_name = DRVSTR;
211 brd->SerialDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
214 * The kernel wants space to store pointers to
215 * tty_struct's and termios's.
217 brd->SerialDriver.ttys = kcalloc(brd->maxports, sizeof(*brd->SerialDriver.ttys), GFP_KERNEL);
218 if (!brd->SerialDriver.ttys)
221 kref_init(&brd->SerialDriver.kref);
222 brd->SerialDriver.termios = kcalloc(brd->maxports, sizeof(*brd->SerialDriver.termios), GFP_KERNEL);
223 if (!brd->SerialDriver.termios)
227 * Entry points for driver. Called by the kernel from
228 * tty_io.c and n_tty.c.
230 tty_set_operations(&brd->SerialDriver, &dgnc_tty_ops);
232 if (!brd->dgnc_Major_Serial_Registered) {
233 /* Register tty devices */
234 rc = tty_register_driver(&brd->SerialDriver);
236 APR(("Can't register tty device (%d)\n", rc));
239 brd->dgnc_Major_Serial_Registered = TRUE;
243 * If we're doing transparent print, we have to do all of the above
244 * again, separately so we don't get the LD confused about what major
245 * we are when we get into the dgnc_tty_open() routine.
247 brd->PrintDriver.magic = TTY_DRIVER_MAGIC;
248 snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
250 brd->PrintDriver.name = brd->PrintName;
251 brd->PrintDriver.name_base = 0;
252 brd->PrintDriver.major = brd->SerialDriver.major;
253 brd->PrintDriver.minor_start = 0x80;
254 brd->PrintDriver.num = brd->maxports;
255 brd->PrintDriver.type = TTY_DRIVER_TYPE_SERIAL;
256 brd->PrintDriver.subtype = SERIAL_TYPE_NORMAL;
257 brd->PrintDriver.init_termios = DgncDefaultTermios;
258 brd->PrintDriver.driver_name = DRVSTR;
259 brd->PrintDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
262 * The kernel wants space to store pointers to
263 * tty_struct's and termios's. Must be separated from
264 * the Serial Driver so we don't get confused
266 brd->PrintDriver.ttys = kcalloc(brd->maxports, sizeof(*brd->PrintDriver.ttys), GFP_KERNEL);
267 if (!brd->PrintDriver.ttys)
269 kref_init(&brd->PrintDriver.kref);
270 brd->PrintDriver.termios = kcalloc(brd->maxports, sizeof(*brd->PrintDriver.termios), GFP_KERNEL);
271 if (!brd->PrintDriver.termios)
275 * Entry points for driver. Called by the kernel from
276 * tty_io.c and n_tty.c.
278 tty_set_operations(&brd->PrintDriver, &dgnc_tty_ops);
280 if (!brd->dgnc_Major_TransparentPrint_Registered) {
281 /* Register Transparent Print devices */
282 rc = tty_register_driver(&brd->PrintDriver);
284 APR(("Can't register Transparent Print device (%d)\n", rc));
287 brd->dgnc_Major_TransparentPrint_Registered = TRUE;
290 dgnc_BoardsByMajor[brd->SerialDriver.major] = brd;
291 brd->dgnc_Serial_Major = brd->SerialDriver.major;
292 brd->dgnc_TransparentPrint_Major = brd->PrintDriver.major;
301 * Init the tty subsystem. Called once per board after board has been
302 * downloaded and init'ed.
304 int dgnc_tty_init(struct dgnc_board *brd)
308 struct channel_t *ch;
314 * Initialize board structure elements.
317 vaddr = brd->re_map_membase;
319 brd->nasync = brd->maxports;
322 * Allocate channel memory that might not have been allocated
323 * when the driver was first loaded.
325 for (i = 0; i < brd->nasync; i++) {
326 if (!brd->channels[i]) {
329 * Okay to malloc with GFP_KERNEL, we are not at
330 * interrupt context, and there are no locks held.
332 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]), GFP_KERNEL);
336 ch = brd->channels[0];
337 vaddr = brd->re_map_membase;
339 /* Set up channel variables */
340 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
342 if (!brd->channels[i])
345 DGNC_SPINLOCK_INIT(ch->ch_lock);
347 /* Store all our magic numbers */
348 ch->magic = DGNC_CHANNEL_MAGIC;
349 ch->ch_tun.magic = DGNC_UNIT_MAGIC;
350 ch->ch_tun.un_ch = ch;
351 ch->ch_tun.un_type = DGNC_SERIAL;
352 ch->ch_tun.un_dev = i;
354 ch->ch_pun.magic = DGNC_UNIT_MAGIC;
355 ch->ch_pun.un_ch = ch;
356 ch->ch_pun.un_type = DGNC_PRINT;
357 ch->ch_pun.un_dev = i + 128;
359 if (brd->bd_uart_offset == 0x200)
360 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
362 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
366 ch->ch_digi = dgnc_digi_init;
368 /* .25 second delay */
369 ch->ch_close_delay = 250;
371 init_waitqueue_head(&ch->ch_flags_wait);
372 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
373 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
374 init_waitqueue_head(&ch->ch_sniff_wait);
377 struct device *classp;
378 classp = tty_register_device(&brd->SerialDriver, i,
379 &(ch->ch_bd->pdev->dev));
380 ch->ch_tun.un_sysfs = classp;
381 dgnc_create_tty_sysfs(&ch->ch_tun, classp);
383 classp = tty_register_device(&brd->PrintDriver, i,
384 &(ch->ch_bd->pdev->dev));
385 ch->ch_pun.un_sysfs = classp;
386 dgnc_create_tty_sysfs(&ch->ch_pun, classp);
396 * dgnc_tty_post_uninit()
398 * UnInitialize any global tty related data.
400 void dgnc_tty_post_uninit(void)
402 kfree(dgnc_TmpWriteBuf);
403 dgnc_TmpWriteBuf = NULL;
410 * Uninitialize the TTY portion of this driver. Free all memory and
413 void dgnc_tty_uninit(struct dgnc_board *brd)
417 if (brd->dgnc_Major_Serial_Registered) {
418 dgnc_BoardsByMajor[brd->SerialDriver.major] = NULL;
419 brd->dgnc_Serial_Major = 0;
420 for (i = 0; i < brd->nasync; i++) {
421 dgnc_remove_tty_sysfs(brd->channels[i]->ch_tun.un_sysfs);
422 tty_unregister_device(&brd->SerialDriver, i);
424 tty_unregister_driver(&brd->SerialDriver);
425 brd->dgnc_Major_Serial_Registered = FALSE;
428 if (brd->dgnc_Major_TransparentPrint_Registered) {
429 dgnc_BoardsByMajor[brd->PrintDriver.major] = NULL;
430 brd->dgnc_TransparentPrint_Major = 0;
431 for (i = 0; i < brd->nasync; i++) {
432 dgnc_remove_tty_sysfs(brd->channels[i]->ch_pun.un_sysfs);
433 tty_unregister_device(&brd->PrintDriver, i);
435 tty_unregister_driver(&brd->PrintDriver);
436 brd->dgnc_Major_TransparentPrint_Registered = FALSE;
439 kfree(brd->SerialDriver.ttys);
440 brd->SerialDriver.ttys = NULL;
441 kfree(brd->PrintDriver.ttys);
442 brd->PrintDriver.ttys = NULL;
446 #define TMPBUFLEN (1024)
449 * dgnc_sniff - Dump data out to the "sniff" buffer if the
450 * proc sniff file is opened...
452 void dgnc_sniff_nowait_nolock(struct channel_t *ch, uchar *text, uchar *buf, int len)
464 tmpbuf = kzalloc(TMPBUFLEN, GFP_ATOMIC);
469 /* Leave if sniff not open */
470 if (!(ch->ch_sniff_flags & SNIFF_OPEN))
473 do_gettimeofday(&tv);
475 /* Create our header for data dump */
476 p += sprintf(p, "<%ld %ld><%s><", tv.tv_sec, tv.tv_usec, text);
477 tmpbuflen = p - tmpbuf;
482 for (i = 0; i < len && tmpbuflen < (TMPBUFLEN - 4); i++) {
483 p += sprintf(p, "%02x ", *buf);
485 tmpbuflen = p - tmpbuf;
488 if (tmpbuflen < (TMPBUFLEN - 4)) {
490 p += sprintf(p - 1, "%s\n", ">");
492 p += sprintf(p, "%s\n", ">");
498 nbuf = strlen(tmpbuf);
502 * Loop while data remains.
504 while (nbuf > 0 && ch->ch_sniff_buf) {
506 * Determine the amount of available space left in the
507 * buffer. If there's none, wait until some appears.
509 n = (ch->ch_sniff_out - ch->ch_sniff_in - 1) & SNIFF_MASK;
512 * If there is no space left to write to in our sniff buffer,
513 * we have no choice but to drop the data.
514 * We *cannot* sleep here waiting for space, because this
515 * function was probably called by the interrupt/timer routines!
521 * Copy as much data as will fit.
527 r = SNIFF_MAX - ch->ch_sniff_in;
530 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, r);
538 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, n);
540 ch->ch_sniff_in += n;
545 * Wakeup any thread waiting for data
547 if (ch->ch_sniff_flags & SNIFF_WAIT_DATA) {
548 ch->ch_sniff_flags &= ~SNIFF_WAIT_DATA;
549 wake_up_interruptible(&ch->ch_sniff_wait);
554 * If the user sent us too much data to push into our tmpbuf,
555 * we need to keep looping around on all the data.
562 } while (too_much_data);
569 /*=======================================================================
571 * dgnc_wmove - Write data to transmit queue.
573 * ch - Pointer to channel structure.
574 * buf - Poiter to characters to be moved.
575 * n - Number of characters to move.
577 *=======================================================================*/
578 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
583 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
586 head = ch->ch_w_head & WQUEUEMASK;
589 * If the write wraps over the top of the circular buffer,
590 * move the portion up to the wrap point, and reset the
591 * pointers to the bottom.
593 remain = WQUEUESIZE - head;
597 memcpy(ch->ch_wqueue + head, buf, remain);
607 memcpy(ch->ch_wqueue + head, buf, remain);
612 ch->ch_w_head = head;
618 /*=======================================================================
620 * dgnc_input - Process received data.
622 * ch - Pointer to channel structure.
624 *=======================================================================*/
625 void dgnc_input(struct channel_t *ch)
627 struct dgnc_board *bd;
628 struct tty_struct *tp;
629 struct tty_ldisc *ld;
641 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
644 tp = ch->ch_tun.un_tty;
647 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
650 DGNC_LOCK(ch->ch_lock, lock_flags);
653 * Figure the number of characters in the buffer.
654 * Exit immediately if none.
657 head = ch->ch_r_head & rmask;
658 tail = ch->ch_r_tail & rmask;
659 data_len = (head - tail) & rmask;
662 DGNC_UNLOCK(ch->ch_lock, lock_flags);
667 * If the device is not open, or CREAD is off,
668 * flush input data and return immediately.
670 if (!tp || (tp->magic != TTY_MAGIC) || !(ch->ch_tun.un_flags & UN_ISOPEN) ||
671 !(tp->termios.c_cflag & CREAD) || (ch->ch_tun.un_flags & UN_CLOSING)) {
673 ch->ch_r_head = tail;
675 /* Force queue flow control to be released, if needed */
676 dgnc_check_queue_flow_control(ch);
678 DGNC_UNLOCK(ch->ch_lock, lock_flags);
683 * If we are throttled, simply don't read any data.
685 if (ch->ch_flags & CH_FORCED_STOPI) {
686 DGNC_UNLOCK(ch->ch_lock, lock_flags);
690 flip_len = TTY_FLIPBUF_SIZE;
692 /* Chop down the length, if needed */
693 len = min(data_len, flip_len);
694 len = min(len, (N_TTY_BUF_SIZE - 1));
696 ld = tty_ldisc_ref(tp);
700 * If the DONT_FLIP flag is on, don't flush our buffer, and act
701 * like the ld doesn't have any space to put the data right now.
703 if (test_bit(TTY_DONT_FLIP, &tp->flags))
708 * If we were unable to get a reference to the ld,
709 * don't flush our buffer, and act like the ld doesn't
710 * have any space to put the data right now.
716 * If ld doesn't have a pointer to a receive_buf function,
717 * flush the data, then act like the ld doesn't have any
718 * space to put the data right now.
720 if (!ld->ops->receive_buf) {
721 ch->ch_r_head = ch->ch_r_tail;
727 DGNC_UNLOCK(ch->ch_lock, lock_flags);
734 * The tty layer in the kernel has changed in 2.6.16+.
736 * The flip buffers in the tty structure are no longer exposed,
737 * and probably will be going away eventually.
739 * If we are completely raw, we don't need to go through a lot
740 * of the tty layers that exist.
741 * In this case, we take the shortest and fastest route we
742 * can to relay the data to the user.
744 * On the other hand, if we are not raw, we need to go through
745 * the new 2.6.16+ tty layer, which has its API more well defined.
747 len = tty_buffer_request_room(tp->port, len);
751 * n now contains the most amount of data we can copy,
752 * bounded either by how much the Linux tty layer can handle,
753 * or the amount of data the card actually has pending...
756 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
763 * If conditions are such that ld needs to see all
764 * UART errors, we will have to walk each character
765 * and error byte and send them to the buffer one at
768 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
769 for (i = 0; i < s; i++) {
770 if (*(ch->ch_equeue + tail + i) & UART_LSR_BI)
771 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_BREAK);
772 else if (*(ch->ch_equeue + tail + i) & UART_LSR_PE)
773 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_PARITY);
774 else if (*(ch->ch_equeue + tail + i) & UART_LSR_FE)
775 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_FRAME);
777 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_NORMAL);
780 tty_insert_flip_string(tp->port, ch->ch_rqueue + tail, s);
783 dgnc_sniff_nowait_nolock(ch, "USER READ", ch->ch_rqueue + tail, s);
787 /* Flip queue if needed */
791 ch->ch_r_tail = tail & rmask;
792 ch->ch_e_tail = tail & rmask;
793 dgnc_check_queue_flow_control(ch);
794 DGNC_UNLOCK(ch->ch_lock, lock_flags);
796 /* Tell the tty layer its okay to "eat" the data now */
797 tty_flip_buffer_push(tp->port);
804 /************************************************************************
805 * Determines when CARRIER changes state and takes appropriate
807 ************************************************************************/
808 void dgnc_carrier(struct channel_t *ch)
810 struct dgnc_board *bd;
812 int virt_carrier = 0;
813 int phys_carrier = 0;
815 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
820 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
823 if (ch->ch_mistat & UART_MSR_DCD)
826 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
829 if (ch->ch_c_cflag & CLOCAL)
833 * Test for a VIRTUAL carrier transition to HIGH.
835 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
838 * When carrier rises, wake any threads waiting
839 * for carrier in the open routine.
842 if (waitqueue_active(&(ch->ch_flags_wait)))
843 wake_up_interruptible(&ch->ch_flags_wait);
847 * Test for a PHYSICAL carrier transition to HIGH.
849 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
852 * When carrier rises, wake any threads waiting
853 * for carrier in the open routine.
856 if (waitqueue_active(&(ch->ch_flags_wait)))
857 wake_up_interruptible(&ch->ch_flags_wait);
861 * Test for a PHYSICAL transition to low, so long as we aren't
862 * currently ignoring physical transitions (which is what "virtual
863 * carrier" indicates).
865 * The transition of the virtual carrier to low really doesn't
866 * matter... it really only means "ignore carrier state", not
867 * "make pretend that carrier is there".
869 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
870 (phys_carrier == 0)) {
873 * When carrier drops:
875 * Drop carrier on all open units.
877 * Flush queues, waking up any task waiting in the
880 * Send a hangup to the control terminal.
882 * Enable all select calls.
884 if (waitqueue_active(&(ch->ch_flags_wait)))
885 wake_up_interruptible(&ch->ch_flags_wait);
887 if (ch->ch_tun.un_open_count > 0)
888 tty_hangup(ch->ch_tun.un_tty);
890 if (ch->ch_pun.un_open_count > 0)
891 tty_hangup(ch->ch_pun.un_tty);
895 * Make sure that our cached values reflect the current reality.
897 if (virt_carrier == 1)
898 ch->ch_flags |= CH_FCAR;
900 ch->ch_flags &= ~CH_FCAR;
902 if (phys_carrier == 1)
903 ch->ch_flags |= CH_CD;
905 ch->ch_flags &= ~CH_CD;
909 * Assign the custom baud rate to the channel structure
911 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
920 ch->ch_custom_speed = 0;
925 * Since the divisor is stored in a 16-bit integer, we make sure
926 * we don't allow any rates smaller than a 16-bit integer would allow.
927 * And of course, rates above the dividend won't fly.
929 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
930 newrate = ((ch->ch_bd->bd_dividend / 0xFFFF) + 1);
932 if (newrate && newrate > ch->ch_bd->bd_dividend)
933 newrate = ch->ch_bd->bd_dividend;
936 testdiv = ch->ch_bd->bd_dividend / newrate;
939 * If we try to figure out what rate the board would use
940 * with the test divisor, it will be either equal or higher
941 * than the requested baud rate. If we then determine the
942 * rate with a divisor one higher, we will get the next lower
943 * supported rate below the requested.
945 testrate_high = ch->ch_bd->bd_dividend / testdiv;
946 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
949 * If the rate for the requested divisor is correct, just
950 * use it and be done.
952 if (testrate_high != newrate) {
954 * Otherwise, pick the rate that is closer (i.e. whichever rate
955 * has a smaller delta).
957 deltahigh = testrate_high - newrate;
958 deltalow = newrate - testrate_low;
960 if (deltahigh < deltalow)
961 newrate = testrate_high;
963 newrate = testrate_low;
967 ch->ch_custom_speed = newrate;
971 void dgnc_check_queue_flow_control(struct channel_t *ch)
975 /* Store how much space we have left in the queue */
976 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
978 qleft += RQUEUEMASK + 1;
981 * Check to see if we should enforce flow control on our queue because
982 * the ld (or user) isn't reading data out of our queue fast enuf.
984 * NOTE: This is done based on what the current flow control of the
987 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
988 * This will cause the UART's FIFO to back up, and force
989 * the RTS signal to be dropped.
990 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
991 * the other side, in hopes it will stop sending data to us.
992 * 3) NONE - Nothing we can do. We will simply drop any extra data
993 * that gets sent into us when the queue fills up.
997 if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
998 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
999 ch->ch_bd->bd_ops->disable_receiver(ch);
1000 ch->ch_flags |= (CH_RECEIVER_OFF);
1004 else if (ch->ch_c_iflag & IXOFF) {
1005 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
1006 ch->ch_bd->bd_ops->send_stop_character(ch);
1007 ch->ch_stops_sent++;
1012 /* Empty... Can't do anything about the impending overflow... */
1017 * Check to see if we should unenforce flow control because
1018 * ld (or user) finally read enuf data out of our queue.
1020 * NOTE: This is done based on what the current flow control of the
1023 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
1024 * This will cause the UART's FIFO to raise RTS back up,
1025 * which will allow the other side to start sending data again.
1026 * 2) SWFLOW (IXOFF) - Send a start character to
1027 * the other side, so it will start sending data to us again.
1028 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
1029 * other side, we don't need to do anything now.
1031 if (qleft > (RQUEUESIZE / 2)) {
1033 if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
1034 if (ch->ch_flags & CH_RECEIVER_OFF) {
1035 ch->ch_bd->bd_ops->enable_receiver(ch);
1036 ch->ch_flags &= ~(CH_RECEIVER_OFF);
1040 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
1041 ch->ch_stops_sent = 0;
1042 ch->ch_bd->bd_ops->send_start_character(ch);
1046 /* Nothing needed. */
1052 void dgnc_wakeup_writes(struct channel_t *ch)
1057 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1060 DGNC_LOCK(ch->ch_lock, lock_flags);
1063 * If channel now has space, wake up anyone waiting on the condition.
1065 qlen = ch->ch_w_head - ch->ch_w_tail;
1069 if (qlen >= (WQUEUESIZE - 256)) {
1070 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1074 if (ch->ch_tun.un_flags & UN_ISOPEN) {
1075 if ((ch->ch_tun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1076 ch->ch_tun.un_tty->ldisc->ops->write_wakeup) {
1077 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1078 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
1079 DGNC_LOCK(ch->ch_lock, lock_flags);
1082 wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
1085 * If unit is set to wait until empty, check to make sure
1086 * the queue AND FIFO are both empty.
1088 if (ch->ch_tun.un_flags & UN_EMPTY) {
1089 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
1090 ch->ch_tun.un_flags &= ~(UN_EMPTY);
1093 * If RTS Toggle mode is on, whenever
1094 * the queue and UART is empty, keep RTS low.
1096 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1097 ch->ch_mostat &= ~(UART_MCR_RTS);
1098 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1102 * If DTR Toggle mode is on, whenever
1103 * the queue and UART is empty, keep DTR low.
1105 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1106 ch->ch_mostat &= ~(UART_MCR_DTR);
1107 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1112 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
1115 if (ch->ch_pun.un_flags & UN_ISOPEN) {
1116 if ((ch->ch_pun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1117 ch->ch_pun.un_tty->ldisc->ops->write_wakeup) {
1118 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1119 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
1120 DGNC_LOCK(ch->ch_lock, lock_flags);
1123 wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
1126 * If unit is set to wait until empty, check to make sure
1127 * the queue AND FIFO are both empty.
1129 if (ch->ch_pun.un_flags & UN_EMPTY) {
1130 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
1131 ch->ch_pun.un_flags &= ~(UN_EMPTY);
1135 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1138 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1143 /************************************************************************
1145 * TTY Entry points and helper functions
1147 ************************************************************************/
1153 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
1155 struct dgnc_board *brd;
1156 struct channel_t *ch;
1165 major = MAJOR(tty_devnum(tty));
1166 minor = MINOR(tty_devnum(tty));
1171 /* Get board pointer from our array of majors we have allocated */
1172 brd = dgnc_BoardsByMajor[major];
1177 * If board is not yet up to a state of READY, go to
1178 * sleep waiting for it to happen or they cancel the open.
1180 rc = wait_event_interruptible(brd->state_wait,
1181 (brd->state & BOARD_READY));
1186 DGNC_LOCK(brd->bd_lock, lock_flags);
1188 /* If opened device is greater than our number of ports, bail. */
1189 if (PORT_NUM(minor) > brd->nasync) {
1190 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1194 ch = brd->channels[PORT_NUM(minor)];
1196 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1200 /* Drop board lock */
1201 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1203 /* Grab channel lock */
1204 DGNC_LOCK(ch->ch_lock, lock_flags);
1206 /* Figure out our type */
1207 if (!IS_PRINT(minor)) {
1208 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1209 un->un_type = DGNC_SERIAL;
1210 } else if (IS_PRINT(minor)) {
1211 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1212 un->un_type = DGNC_PRINT;
1214 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1219 * If the port is still in a previous open, and in a state
1220 * where we simply cannot safely keep going, wait until the
1223 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1225 rc = wait_event_interruptible(ch->ch_flags_wait, ((ch->ch_flags & CH_OPENING) == 0));
1227 /* If ret is non-zero, user ctrl-c'ed us */
1232 * If either unit is in the middle of the fragile part of close,
1233 * we just cannot touch the channel safely.
1234 * Go to sleep, knowing that when the channel can be
1235 * touched safely, the close routine will signal the
1236 * ch_flags_wait to wake us back up.
1238 rc = wait_event_interruptible(ch->ch_flags_wait,
1239 (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING) == 0));
1241 /* If ret is non-zero, user ctrl-c'ed us */
1245 DGNC_LOCK(ch->ch_lock, lock_flags);
1248 /* Store our unit into driver_data, so we always have it available. */
1249 tty->driver_data = un;
1255 if (!(un->un_flags & UN_ISOPEN)) {
1256 /* Store important variables. */
1259 /* Maybe do something here to the TTY struct as well? */
1264 * Allocate channel buffers for read/write/error.
1265 * Set flag, so we don't get trounced on.
1267 ch->ch_flags |= (CH_OPENING);
1269 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1270 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1273 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1275 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1277 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1279 DGNC_LOCK(ch->ch_lock, lock_flags);
1281 ch->ch_flags &= ~(CH_OPENING);
1282 wake_up_interruptible(&ch->ch_flags_wait);
1285 * Initialize if neither terminal or printer is open.
1287 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1290 * Flush input queues.
1299 brd->bd_ops->flush_uart_write(ch);
1300 brd->bd_ops->flush_uart_read(ch);
1303 ch->ch_cached_lsr = 0;
1304 ch->ch_stop_sending_break = 0;
1305 ch->ch_stops_sent = 0;
1307 ch->ch_c_cflag = tty->termios.c_cflag;
1308 ch->ch_c_iflag = tty->termios.c_iflag;
1309 ch->ch_c_oflag = tty->termios.c_oflag;
1310 ch->ch_c_lflag = tty->termios.c_lflag;
1311 ch->ch_startc = tty->termios.c_cc[VSTART];
1312 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1315 * Bring up RTS and DTR...
1316 * Also handle RTS or DTR toggle if set.
1318 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1319 ch->ch_mostat |= (UART_MCR_RTS);
1320 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1321 ch->ch_mostat |= (UART_MCR_DTR);
1323 /* Tell UART to init itself */
1324 brd->bd_ops->uart_init(ch);
1328 * Run param in case we changed anything
1330 brd->bd_ops->param(tty);
1335 * follow protocol for opening port
1338 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1340 rc = dgnc_block_til_ready(tty, file, ch);
1342 /* No going back now, increment our unit and channel counters */
1343 DGNC_LOCK(ch->ch_lock, lock_flags);
1344 ch->ch_open_count++;
1345 un->un_open_count++;
1346 un->un_flags |= (UN_ISOPEN);
1347 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1354 * dgnc_block_til_ready()
1356 * Wait for DCD, if needed.
1358 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch)
1361 struct un_t *un = NULL;
1364 int sleep_on_un_flags = 0;
1366 if (!tty || tty->magic != TTY_MAGIC || !file || !ch || ch->magic != DGNC_CHANNEL_MAGIC) {
1370 un = tty->driver_data;
1371 if (!un || un->magic != DGNC_UNIT_MAGIC)
1374 DGNC_LOCK(ch->ch_lock, lock_flags);
1381 sleep_on_un_flags = 0;
1384 * If board has failed somehow during our sleep, bail with error.
1386 if (ch->ch_bd->state == BOARD_FAILED) {
1391 /* If tty was hung up, break out of loop and set error. */
1392 if (tty_hung_up_p(file)) {
1398 * If either unit is in the middle of the fragile part of close,
1399 * we just cannot touch the channel safely.
1400 * Go back to sleep, knowing that when the channel can be
1401 * touched safely, the close routine will signal the
1402 * ch_wait_flags to wake us back up.
1404 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING)) {
1407 * Our conditions to leave cleanly and happily:
1408 * 1) NONBLOCKING on the tty is set.
1410 * 3) DCD (fake or real) is active.
1413 if (file->f_flags & O_NONBLOCK)
1416 if (tty->flags & (1 << TTY_IO_ERROR)) {
1421 if (ch->ch_flags & CH_CD)
1424 if (ch->ch_flags & CH_FCAR)
1427 sleep_on_un_flags = 1;
1431 * If there is a signal pending, the user probably
1432 * interrupted (ctrl-c) us.
1433 * Leave loop with error set.
1435 if (signal_pending(current)) {
1436 retval = -ERESTARTSYS;
1441 * Store the flags before we let go of channel lock
1443 if (sleep_on_un_flags)
1444 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1446 old_flags = ch->ch_flags;
1449 * Let go of channel lock before calling schedule.
1450 * Our poller will get any FEP events and wake us up when DCD
1451 * eventually goes active.
1454 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1457 * Wait for something in the flags to change from the current value.
1459 if (sleep_on_un_flags)
1460 retval = wait_event_interruptible(un->un_flags_wait,
1461 (old_flags != (ch->ch_tun.un_flags | ch->ch_pun.un_flags)));
1463 retval = wait_event_interruptible(ch->ch_flags_wait,
1464 (old_flags != ch->ch_flags));
1467 * We got woken up for some reason.
1468 * Before looping around, grab our channel lock.
1470 DGNC_LOCK(ch->ch_lock, lock_flags);
1475 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1487 * Hangup the port. Like a close, but don't wait for output to drain.
1489 static void dgnc_tty_hangup(struct tty_struct *tty)
1493 if (!tty || tty->magic != TTY_MAGIC)
1496 un = tty->driver_data;
1497 if (!un || un->magic != DGNC_UNIT_MAGIC)
1500 /* flush the transmit queues */
1501 dgnc_tty_flush_buffer(tty);
1510 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1512 struct ktermios *ts;
1513 struct dgnc_board *bd;
1514 struct channel_t *ch;
1519 if (!tty || tty->magic != TTY_MAGIC)
1522 un = tty->driver_data;
1523 if (!un || un->magic != DGNC_UNIT_MAGIC)
1527 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1531 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1536 DGNC_LOCK(ch->ch_lock, lock_flags);
1539 * Determine if this is the last close or not - and if we agree about
1540 * which type of close it is with the Line Discipline
1542 if ((tty->count == 1) && (un->un_open_count != 1)) {
1544 * Uh, oh. tty->count is 1, which means that the tty
1545 * structure will be freed. un_open_count should always
1546 * be one in these conditions. If it's greater than
1547 * one, we've got real problems, since it means the
1548 * serial port won't be shutdown.
1550 APR(("tty->count is 1, un open count is %d\n", un->un_open_count));
1551 un->un_open_count = 1;
1554 if (un->un_open_count)
1555 un->un_open_count--;
1557 APR(("bad serial port open count of %d\n", un->un_open_count));
1559 ch->ch_open_count--;
1561 if (ch->ch_open_count && un->un_open_count) {
1562 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1566 /* OK, its the last close on the unit */
1567 un->un_flags |= UN_CLOSING;
1573 * Only officially close channel if count is 0 and
1574 * DIGI_PRINTER bit is not set.
1576 if ((ch->ch_open_count == 0) && !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1578 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1581 * turn off print device when closing print device.
1583 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1584 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1585 (int) ch->ch_digi.digi_offlen);
1586 ch->ch_flags &= ~CH_PRON;
1589 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1590 /* wait for output to drain */
1591 /* This will also return if we take an interrupt */
1593 rc = bd->bd_ops->drain(tty, 0);
1595 dgnc_tty_flush_buffer(tty);
1596 tty_ldisc_flush(tty);
1598 DGNC_LOCK(ch->ch_lock, lock_flags);
1603 * If we have HUPCL set, lower DTR and RTS
1605 if (ch->ch_c_cflag & HUPCL) {
1608 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1609 bd->bd_ops->assert_modem_signals(ch);
1612 * Go to sleep to ensure RTS/DTR
1613 * have been dropped for modems to see it.
1615 if (ch->ch_close_delay) {
1616 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1617 dgnc_ms_sleep(ch->ch_close_delay);
1618 DGNC_LOCK(ch->ch_lock, lock_flags);
1622 ch->ch_old_baud = 0;
1624 /* Turn off UART interrupts for this port */
1625 ch->ch_bd->bd_ops->uart_off(ch);
1628 * turn off print device when closing print device.
1630 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1631 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1632 (int) ch->ch_digi.digi_offlen);
1633 ch->ch_flags &= ~CH_PRON;
1638 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1640 wake_up_interruptible(&ch->ch_flags_wait);
1641 wake_up_interruptible(&un->un_flags_wait);
1643 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1648 * dgnc_tty_chars_in_buffer()
1650 * Return number of characters that have not been transmitted yet.
1652 * This routine is used by the line discipline to determine if there
1653 * is data waiting to be transmitted/drained/flushed or not.
1655 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1657 struct channel_t *ch = NULL;
1658 struct un_t *un = NULL;
1663 ulong lock_flags = 0;
1668 un = tty->driver_data;
1669 if (!un || un->magic != DGNC_UNIT_MAGIC)
1673 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1676 DGNC_LOCK(ch->ch_lock, lock_flags);
1679 thead = ch->ch_w_head & tmask;
1680 ttail = ch->ch_w_tail & tmask;
1682 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1684 if (ttail == thead) {
1688 chars = thead - ttail;
1690 chars = thead - ttail + WQUEUESIZE;
1700 * Reduces bytes_available to the max number of characters
1701 * that can be sent currently given the maxcps value, and
1702 * returns the new bytes_available. This only affects printer
1705 static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1707 struct channel_t *ch = NULL;
1708 struct un_t *un = NULL;
1711 return bytes_available;
1713 un = tty->driver_data;
1714 if (!un || un->magic != DGNC_UNIT_MAGIC)
1715 return bytes_available;
1718 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1719 return bytes_available;
1722 * If its not the Transparent print device, return
1723 * the full data amount.
1725 if (un->un_type != DGNC_PRINT)
1726 return bytes_available;
1728 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1730 unsigned long current_time = jiffies;
1731 unsigned long buffer_time = current_time +
1732 (HZ * ch->ch_digi.digi_bufsize) / ch->ch_digi.digi_maxcps;
1734 if (ch->ch_cpstime < current_time) {
1735 /* buffer is empty */
1736 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1737 cps_limit = ch->ch_digi.digi_bufsize;
1738 } else if (ch->ch_cpstime < buffer_time) {
1739 /* still room in the buffer */
1740 cps_limit = ((buffer_time - ch->ch_cpstime) * ch->ch_digi.digi_maxcps) / HZ;
1742 /* no room in the buffer */
1746 bytes_available = min(cps_limit, bytes_available);
1749 return bytes_available;
1754 * dgnc_tty_write_room()
1756 * Return space available in Tx buffer
1758 static int dgnc_tty_write_room(struct tty_struct *tty)
1760 struct channel_t *ch = NULL;
1761 struct un_t *un = NULL;
1766 ulong lock_flags = 0;
1768 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1771 un = tty->driver_data;
1772 if (!un || un->magic != DGNC_UNIT_MAGIC)
1776 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1779 DGNC_LOCK(ch->ch_lock, lock_flags);
1782 head = (ch->ch_w_head) & tmask;
1783 tail = (ch->ch_w_tail) & tmask;
1785 ret = tail - head - 1;
1789 /* Limit printer to maxcps */
1790 ret = dgnc_maxcps_room(tty, ret);
1793 * If we are printer device, leave space for
1794 * possibly both the on and off strings.
1796 if (un->un_type == DGNC_PRINT) {
1797 if (!(ch->ch_flags & CH_PRON))
1798 ret -= ch->ch_digi.digi_onlen;
1799 ret -= ch->ch_digi.digi_offlen;
1801 if (ch->ch_flags & CH_PRON)
1802 ret -= ch->ch_digi.digi_offlen;
1808 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1815 * dgnc_tty_put_char()
1817 * Put a character into ch->ch_buf
1819 * - used by the line discipline for OPOST processing
1821 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1824 * Simply call tty_write.
1826 dgnc_tty_write(tty, &c, 1);
1834 * Take data from the user or kernel and send it out to the FEP.
1835 * In here exists all the Transparent Print magic as well.
1837 static int dgnc_tty_write(struct tty_struct *tty,
1838 const unsigned char *buf, int count)
1840 struct channel_t *ch = NULL;
1841 struct un_t *un = NULL;
1842 int bufcount = 0, n = 0;
1851 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1854 un = tty->driver_data;
1855 if (!un || un->magic != DGNC_UNIT_MAGIC)
1859 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1866 * Store original amount of characters passed in.
1867 * This helps to figure out if we should ask the FEP
1868 * to send us an event when it has more space available.
1872 DGNC_LOCK(ch->ch_lock, lock_flags);
1874 /* Get our space available for the channel from the board */
1876 head = (ch->ch_w_head) & tmask;
1877 tail = (ch->ch_w_tail) & tmask;
1879 bufcount = tail - head - 1;
1881 bufcount += WQUEUESIZE;
1884 * Limit printer output to maxcps overall, with bursts allowed
1885 * up to bufsize characters.
1887 bufcount = dgnc_maxcps_room(tty, bufcount);
1890 * Take minimum of what the user wants to send, and the
1891 * space available in the FEP buffer.
1893 count = min(count, bufcount);
1896 * Bail if no space left.
1899 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1904 * Output the printer ON string, if we are in terminal mode, but
1905 * need to be in printer mode.
1907 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1908 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1909 (int) ch->ch_digi.digi_onlen);
1910 head = (ch->ch_w_head) & tmask;
1911 ch->ch_flags |= CH_PRON;
1915 * On the other hand, output the printer OFF string, if we are
1916 * currently in printer mode, but need to output to the terminal.
1918 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1919 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1920 (int) ch->ch_digi.digi_offlen);
1921 head = (ch->ch_w_head) & tmask;
1922 ch->ch_flags &= ~CH_PRON;
1926 * If there is nothing left to copy, or I can't handle any more data, leave.
1929 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1935 count = min(count, WRITEBUFLEN);
1937 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1940 * If data is coming from user space, copy it into a temporary
1941 * buffer so we don't get swapped out while doing the copy to
1944 /* we're allowed to block if it's from_user */
1945 if (down_interruptible(&dgnc_TmpWriteSem))
1949 * copy_from_user() returns the number
1950 * of bytes that could *NOT* be copied.
1952 count -= copy_from_user(dgnc_TmpWriteBuf, (const uchar __user *) buf, count);
1955 up(&dgnc_TmpWriteSem);
1959 DGNC_LOCK(ch->ch_lock, lock_flags);
1961 buf = dgnc_TmpWriteBuf;
1968 * If the write wraps over the top of the circular buffer,
1969 * move the portion up to the wrap point, and reset the
1970 * pointers to the bottom.
1972 remain = WQUEUESIZE - head;
1976 memcpy(ch->ch_wqueue + head, buf, remain);
1977 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
1984 * Move rest of data.
1987 memcpy(ch->ch_wqueue + head, buf, remain);
1988 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
1994 ch->ch_w_head = head;
1997 /* Update printer buffer empty time. */
1998 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
1999 && (ch->ch_digi.digi_bufsize > 0)) {
2000 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
2004 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2005 up(&dgnc_TmpWriteSem);
2007 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2012 * Channel lock is grabbed and then released
2013 * inside this routine.
2015 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
2023 * Return modem signals to ld.
2026 static int dgnc_tty_tiocmget(struct tty_struct *tty)
2028 struct channel_t *ch;
2034 if (!tty || tty->magic != TTY_MAGIC)
2037 un = tty->driver_data;
2038 if (!un || un->magic != DGNC_UNIT_MAGIC)
2042 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2045 DGNC_LOCK(ch->ch_lock, lock_flags);
2047 mstat = (ch->ch_mostat | ch->ch_mistat);
2049 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2053 if (mstat & UART_MCR_DTR)
2054 result |= TIOCM_DTR;
2055 if (mstat & UART_MCR_RTS)
2056 result |= TIOCM_RTS;
2057 if (mstat & UART_MSR_CTS)
2058 result |= TIOCM_CTS;
2059 if (mstat & UART_MSR_DSR)
2060 result |= TIOCM_DSR;
2061 if (mstat & UART_MSR_RI)
2063 if (mstat & UART_MSR_DCD)
2071 * dgnc_tty_tiocmset()
2073 * Set modem signals, called by ld.
2076 static int dgnc_tty_tiocmset(struct tty_struct *tty,
2077 unsigned int set, unsigned int clear)
2079 struct dgnc_board *bd;
2080 struct channel_t *ch;
2085 if (!tty || tty->magic != TTY_MAGIC)
2088 un = tty->driver_data;
2089 if (!un || un->magic != DGNC_UNIT_MAGIC)
2093 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2097 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2100 DGNC_LOCK(ch->ch_lock, lock_flags);
2102 if (set & TIOCM_RTS)
2103 ch->ch_mostat |= UART_MCR_RTS;
2105 if (set & TIOCM_DTR)
2106 ch->ch_mostat |= UART_MCR_DTR;
2108 if (clear & TIOCM_RTS)
2109 ch->ch_mostat &= ~(UART_MCR_RTS);
2111 if (clear & TIOCM_DTR)
2112 ch->ch_mostat &= ~(UART_MCR_DTR);
2114 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2116 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2123 * dgnc_tty_send_break()
2125 * Send a Break, called by ld.
2127 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
2129 struct dgnc_board *bd;
2130 struct channel_t *ch;
2135 if (!tty || tty->magic != TTY_MAGIC)
2138 un = tty->driver_data;
2139 if (!un || un->magic != DGNC_UNIT_MAGIC)
2143 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2147 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2161 DGNC_LOCK(ch->ch_lock, lock_flags);
2163 ch->ch_bd->bd_ops->send_break(ch, msec);
2165 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2173 * dgnc_tty_wait_until_sent()
2175 * wait until data has been transmitted, called by ld.
2177 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
2179 struct dgnc_board *bd;
2180 struct channel_t *ch;
2184 if (!tty || tty->magic != TTY_MAGIC)
2187 un = tty->driver_data;
2188 if (!un || un->magic != DGNC_UNIT_MAGIC)
2192 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2196 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2199 rc = bd->bd_ops->drain(tty, 0);
2208 * send a high priority character, called by ld.
2210 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
2212 struct dgnc_board *bd;
2213 struct channel_t *ch;
2217 if (!tty || tty->magic != TTY_MAGIC)
2220 un = tty->driver_data;
2221 if (!un || un->magic != DGNC_UNIT_MAGIC)
2225 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2229 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2232 dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
2234 DGNC_LOCK(ch->ch_lock, lock_flags);
2235 bd->bd_ops->send_immediate_char(ch, c);
2236 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2238 dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
2246 * Return modem signals to ld.
2248 static inline int dgnc_get_mstat(struct channel_t *ch)
2250 unsigned char mstat;
2254 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2257 DGNC_LOCK(ch->ch_lock, lock_flags);
2259 mstat = (ch->ch_mostat | ch->ch_mistat);
2261 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2265 if (mstat & UART_MCR_DTR)
2266 result |= TIOCM_DTR;
2267 if (mstat & UART_MCR_RTS)
2268 result |= TIOCM_RTS;
2269 if (mstat & UART_MSR_CTS)
2270 result |= TIOCM_CTS;
2271 if (mstat & UART_MSR_DSR)
2272 result |= TIOCM_DSR;
2273 if (mstat & UART_MSR_RI)
2275 if (mstat & UART_MSR_DCD)
2284 * Return modem signals to ld.
2286 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value)
2291 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2294 result = dgnc_get_mstat(ch);
2299 rc = put_user(result, value);
2306 * dgnc_set_modem_info()
2308 * Set modem signals, called by ld.
2310 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value)
2312 struct dgnc_board *bd;
2313 struct channel_t *ch;
2316 unsigned int arg = 0;
2319 if (!tty || tty->magic != TTY_MAGIC)
2322 un = tty->driver_data;
2323 if (!un || un->magic != DGNC_UNIT_MAGIC)
2327 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2331 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2336 ret = get_user(arg, value);
2342 if (arg & TIOCM_RTS)
2343 ch->ch_mostat |= UART_MCR_RTS;
2345 if (arg & TIOCM_DTR)
2346 ch->ch_mostat |= UART_MCR_DTR;
2351 if (arg & TIOCM_RTS)
2352 ch->ch_mostat &= ~(UART_MCR_RTS);
2354 if (arg & TIOCM_DTR)
2355 ch->ch_mostat &= ~(UART_MCR_DTR);
2361 if (arg & TIOCM_RTS)
2362 ch->ch_mostat |= UART_MCR_RTS;
2364 ch->ch_mostat &= ~(UART_MCR_RTS);
2366 if (arg & TIOCM_DTR)
2367 ch->ch_mostat |= UART_MCR_DTR;
2369 ch->ch_mostat &= ~(UART_MCR_DTR);
2377 DGNC_LOCK(ch->ch_lock, lock_flags);
2379 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2381 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2388 * dgnc_tty_digigeta()
2390 * Ioctl to get the information for ditty.
2395 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo)
2397 struct channel_t *ch;
2405 if (!tty || tty->magic != TTY_MAGIC)
2408 un = tty->driver_data;
2409 if (!un || un->magic != DGNC_UNIT_MAGIC)
2413 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2416 memset(&tmp, 0, sizeof(tmp));
2418 DGNC_LOCK(ch->ch_lock, lock_flags);
2419 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2420 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2422 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2430 * dgnc_tty_digiseta()
2432 * Ioctl to set the information for ditty.
2437 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info)
2439 struct dgnc_board *bd;
2440 struct channel_t *ch;
2442 struct digi_t new_digi;
2445 if (!tty || tty->magic != TTY_MAGIC)
2448 un = tty->driver_data;
2449 if (!un || un->magic != DGNC_UNIT_MAGIC)
2453 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2457 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2460 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
2463 DGNC_LOCK(ch->ch_lock, lock_flags);
2466 * Handle transistions to and from RTS Toggle.
2468 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2469 ch->ch_mostat &= ~(UART_MCR_RTS);
2470 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2471 ch->ch_mostat |= (UART_MCR_RTS);
2474 * Handle transistions to and from DTR Toggle.
2476 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2477 ch->ch_mostat &= ~(UART_MCR_DTR);
2478 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2479 ch->ch_mostat |= (UART_MCR_DTR);
2481 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2483 if (ch->ch_digi.digi_maxcps < 1)
2484 ch->ch_digi.digi_maxcps = 1;
2486 if (ch->ch_digi.digi_maxcps > 10000)
2487 ch->ch_digi.digi_maxcps = 10000;
2489 if (ch->ch_digi.digi_bufsize < 10)
2490 ch->ch_digi.digi_bufsize = 10;
2492 if (ch->ch_digi.digi_maxchar < 1)
2493 ch->ch_digi.digi_maxchar = 1;
2495 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2496 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2498 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2499 ch->ch_digi.digi_onlen = DIGI_PLEN;
2501 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2502 ch->ch_digi.digi_offlen = DIGI_PLEN;
2504 ch->ch_bd->bd_ops->param(tty);
2506 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2513 * dgnc_set_termios()
2515 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2517 struct dgnc_board *bd;
2518 struct channel_t *ch;
2520 unsigned long lock_flags;
2522 if (!tty || tty->magic != TTY_MAGIC)
2525 un = tty->driver_data;
2526 if (!un || un->magic != DGNC_UNIT_MAGIC)
2530 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2534 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2537 DGNC_LOCK(ch->ch_lock, lock_flags);
2539 ch->ch_c_cflag = tty->termios.c_cflag;
2540 ch->ch_c_iflag = tty->termios.c_iflag;
2541 ch->ch_c_oflag = tty->termios.c_oflag;
2542 ch->ch_c_lflag = tty->termios.c_lflag;
2543 ch->ch_startc = tty->termios.c_cc[VSTART];
2544 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2546 ch->ch_bd->bd_ops->param(tty);
2549 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2553 static void dgnc_tty_throttle(struct tty_struct *tty)
2555 struct channel_t *ch;
2557 ulong lock_flags = 0;
2559 if (!tty || tty->magic != TTY_MAGIC)
2562 un = tty->driver_data;
2563 if (!un || un->magic != DGNC_UNIT_MAGIC)
2567 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2570 DGNC_LOCK(ch->ch_lock, lock_flags);
2572 ch->ch_flags |= (CH_FORCED_STOPI);
2574 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2578 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2580 struct channel_t *ch;
2584 if (!tty || tty->magic != TTY_MAGIC)
2587 un = tty->driver_data;
2588 if (!un || un->magic != DGNC_UNIT_MAGIC)
2592 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2595 DGNC_LOCK(ch->ch_lock, lock_flags);
2597 ch->ch_flags &= ~(CH_FORCED_STOPI);
2599 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2603 static void dgnc_tty_start(struct tty_struct *tty)
2605 struct dgnc_board *bd;
2606 struct channel_t *ch;
2610 if (!tty || tty->magic != TTY_MAGIC)
2613 un = tty->driver_data;
2614 if (!un || un->magic != DGNC_UNIT_MAGIC)
2618 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2622 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2625 DGNC_LOCK(ch->ch_lock, lock_flags);
2627 ch->ch_flags &= ~(CH_FORCED_STOP);
2629 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2633 static void dgnc_tty_stop(struct tty_struct *tty)
2635 struct dgnc_board *bd;
2636 struct channel_t *ch;
2640 if (!tty || tty->magic != TTY_MAGIC)
2643 un = tty->driver_data;
2644 if (!un || un->magic != DGNC_UNIT_MAGIC)
2648 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2652 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2655 DGNC_LOCK(ch->ch_lock, lock_flags);
2657 ch->ch_flags |= (CH_FORCED_STOP);
2659 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2664 * dgnc_tty_flush_chars()
2666 * Flush the cook buffer
2668 * Note to self, and any other poor souls who venture here:
2670 * flush in this case DOES NOT mean dispose of the data.
2671 * instead, it means "stop buffering and send it if you
2672 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2674 * It is also always called in interrupt context - JAR 8-Sept-99
2676 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2678 struct dgnc_board *bd;
2679 struct channel_t *ch;
2683 if (!tty || tty->magic != TTY_MAGIC)
2686 un = tty->driver_data;
2687 if (!un || un->magic != DGNC_UNIT_MAGIC)
2691 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2695 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2698 DGNC_LOCK(ch->ch_lock, lock_flags);
2700 /* Do something maybe here */
2702 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2708 * dgnc_tty_flush_buffer()
2710 * Flush Tx buffer (make in == out)
2712 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2714 struct channel_t *ch;
2718 if (!tty || tty->magic != TTY_MAGIC)
2721 un = tty->driver_data;
2722 if (!un || un->magic != DGNC_UNIT_MAGIC)
2726 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2729 DGNC_LOCK(ch->ch_lock, lock_flags);
2731 ch->ch_flags &= ~CH_STOP;
2733 /* Flush our write queue */
2734 ch->ch_w_head = ch->ch_w_tail;
2736 /* Flush UARTs transmit FIFO */
2737 ch->ch_bd->bd_ops->flush_uart_write(ch);
2739 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2740 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2741 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2743 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2744 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2745 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2748 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2753 /*****************************************************************************
2755 * The IOCTL function and all of its helpers
2757 *****************************************************************************/
2762 * The usual assortment of ioctl's
2764 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2767 struct dgnc_board *bd;
2768 struct channel_t *ch;
2772 void __user *uarg = (void __user *) arg;
2774 if (!tty || tty->magic != TTY_MAGIC)
2777 un = tty->driver_data;
2778 if (!un || un->magic != DGNC_UNIT_MAGIC)
2782 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2786 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2789 DGNC_LOCK(ch->ch_lock, lock_flags);
2791 if (un->un_open_count <= 0) {
2792 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2798 /* Here are all the standard ioctl's that we MUST implement */
2802 * TCSBRK is SVID version: non-zero arg --> no break
2803 * this behaviour is exploited by tcdrain().
2805 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2806 * between 0.25 and 0.5 seconds so we'll ask for something
2807 * in the middle: 0.375 seconds.
2809 rc = tty_check_change(tty);
2810 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2814 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2819 DGNC_LOCK(ch->ch_lock, lock_flags);
2821 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP)) {
2822 ch->ch_bd->bd_ops->send_break(ch, 250);
2825 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2831 /* support for POSIX tcsendbreak()
2832 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2833 * between 0.25 and 0.5 seconds so we'll ask for something
2834 * in the middle: 0.375 seconds.
2836 rc = tty_check_change(tty);
2837 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2841 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2845 DGNC_LOCK(ch->ch_lock, lock_flags);
2847 ch->ch_bd->bd_ops->send_break(ch, 250);
2849 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2854 rc = tty_check_change(tty);
2855 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2859 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2863 DGNC_LOCK(ch->ch_lock, lock_flags);
2865 ch->ch_bd->bd_ops->send_break(ch, 250);
2867 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2873 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2878 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2880 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
2885 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2886 rc = get_user(arg, (unsigned long __user *) arg);
2890 DGNC_LOCK(ch->ch_lock, lock_flags);
2891 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
2892 ch->ch_bd->bd_ops->param(tty);
2893 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2898 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2899 return dgnc_get_modem_info(ch, uarg);
2904 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2905 return dgnc_set_modem_info(tty, cmd, uarg);
2908 * Here are any additional ioctl's that we want to implement
2913 * The linux tty driver doesn't have a flush
2914 * input routine for the driver, assuming all backed
2915 * up data is in the line disc. buffers. However,
2916 * we all know that's not the case. Here, we
2917 * act on the ioctl, but then lie and say we didn't
2918 * so the line discipline will process the flush
2921 rc = tty_check_change(tty);
2923 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2927 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2928 ch->ch_r_head = ch->ch_r_tail;
2929 ch->ch_bd->bd_ops->flush_uart_read(ch);
2930 /* Force queue flow control to be released, if needed */
2931 dgnc_check_queue_flow_control(ch);
2934 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2935 if (!(un->un_type == DGNC_PRINT)) {
2936 ch->ch_w_head = ch->ch_w_tail;
2937 ch->ch_bd->bd_ops->flush_uart_write(ch);
2939 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2940 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2941 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2944 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2945 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2946 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2952 /* pretend we didn't recognize this IOCTL */
2953 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2954 return -ENOIOCTLCMD;
2958 * The linux tty driver doesn't have a flush
2959 * input routine for the driver, assuming all backed
2960 * up data is in the line disc. buffers. However,
2961 * we all know that's not the case. Here, we
2962 * act on the ioctl, but then lie and say we didn't
2963 * so the line discipline will process the flush
2966 if (cmd == TCSETSF) {
2968 ch->ch_flags &= ~CH_STOP;
2969 ch->ch_r_head = ch->ch_r_tail;
2970 ch->ch_bd->bd_ops->flush_uart_read(ch);
2971 /* Force queue flow control to be released, if needed */
2972 dgnc_check_queue_flow_control(ch);
2975 /* now wait for all the output to drain */
2976 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2977 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2981 /* pretend we didn't recognize this */
2982 return -ENOIOCTLCMD;
2986 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2987 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2991 /* pretend we didn't recognize this */
2992 return -ENOIOCTLCMD;
2995 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2996 /* Make the ld do it */
2997 return -ENOIOCTLCMD;
3000 /* get information for ditty */
3001 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3002 return dgnc_tty_digigeta(tty, uarg);
3007 /* set information for ditty */
3008 if (cmd == (DIGI_SETAW)) {
3010 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3011 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3016 DGNC_LOCK(ch->ch_lock, lock_flags);
3018 tty_ldisc_flush(tty);
3023 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3024 return dgnc_tty_digiseta(tty, uarg);
3029 /* Let go of locks when accessing user space, could sleep */
3030 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3031 rc = get_user(loopback, (unsigned int __user *) arg);
3034 DGNC_LOCK(ch->ch_lock, lock_flags);
3036 /* Enable/disable internal loopback for this port */
3038 ch->ch_flags |= CH_LOOPBACK;
3040 ch->ch_flags &= ~(CH_LOOPBACK);
3042 ch->ch_bd->bd_ops->param(tty);
3043 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3047 case DIGI_GETCUSTOMBAUD:
3048 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3049 rc = put_user(ch->ch_custom_speed, (unsigned int __user *) arg);
3052 case DIGI_SETCUSTOMBAUD:
3055 /* Let go of locks when accessing user space, could sleep */
3056 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3057 rc = get_user(new_rate, (int __user *) arg);
3060 DGNC_LOCK(ch->ch_lock, lock_flags);
3061 dgnc_set_custom_speed(ch, new_rate);
3062 ch->ch_bd->bd_ops->param(tty);
3063 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3068 * This ioctl allows insertion of a character into the front
3069 * of any pending data to be transmitted.
3071 * This ioctl is to satify the "Send Character Immediate"
3072 * call that the RealPort protocol spec requires.
3074 case DIGI_REALPORT_SENDIMMEDIATE:
3077 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3078 rc = get_user(c, (unsigned char __user *) arg);
3081 DGNC_LOCK(ch->ch_lock, lock_flags);
3082 ch->ch_bd->bd_ops->send_immediate_char(ch, c);
3083 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3088 * This ioctl returns all the current counts for the port.
3090 * This ioctl is to satify the "Line Error Counters"
3091 * call that the RealPort protocol spec requires.
3093 case DIGI_REALPORT_GETCOUNTERS:
3095 struct digi_getcounter buf;
3097 buf.norun = ch->ch_err_overrun;
3098 buf.noflow = 0; /* The driver doesn't keep this stat */
3099 buf.nframe = ch->ch_err_frame;
3100 buf.nparity = ch->ch_err_parity;
3101 buf.nbreak = ch->ch_err_break;
3102 buf.rbytes = ch->ch_rxcount;
3103 buf.tbytes = ch->ch_txcount;
3105 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3107 if (copy_to_user(uarg, &buf, sizeof(buf)))
3114 * This ioctl returns all current events.
3116 * This ioctl is to satify the "Event Reporting"
3117 * call that the RealPort protocol spec requires.
3119 case DIGI_REALPORT_GETEVENTS:
3121 unsigned int events = 0;
3123 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
3124 if (ch->ch_flags & CH_BREAK_SENDING)
3126 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP))
3127 events |= (EV_OPU | EV_OPS);
3129 if ((ch->ch_flags & CH_STOPI) || (ch->ch_flags & CH_FORCED_STOPI)) {
3130 events |= (EV_IPU | EV_IPS);
3133 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3134 rc = put_user(events, (unsigned int __user *) arg);
3139 * This ioctl returns TOUT and TIN counters based
3140 * upon the values passed in by the RealPort Server.
3141 * It also passes back whether the UART Transmitter is
3144 case DIGI_REALPORT_GETBUFFERS:
3146 struct digi_getbuffer buf;
3150 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3153 * Get data from user first.
3155 if (copy_from_user(&buf, uarg, sizeof(buf)))
3158 DGNC_LOCK(ch->ch_lock, lock_flags);
3161 * Figure out how much data is in our RX and TX queues.
3163 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
3164 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
3167 * Is the UART empty? Add that value to whats in our TX queue.
3169 count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
3172 * Figure out how much data the RealPort Server believes should
3173 * be in our TX queue.
3175 tdist = (buf.tIn - buf.tOut) & 0xffff;
3178 * If we have more data than the RealPort Server believes we
3179 * should have, reduce our count to its amount.
3181 * This count difference CAN happen because the Linux LD can
3182 * insert more characters into our queue for OPOST processing
3183 * that the RealPort Server doesn't know about.
3185 if (buf.txbuf > tdist)
3189 * Report whether our queue and UART TX are completely empty.
3196 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3198 if (copy_to_user(uarg, &buf, sizeof(buf)))
3204 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3206 return -ENOIOCTLCMD;