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 =
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)
191 * dgnc_tty_register()
193 * Init the tty subsystem for this board.
195 int dgnc_tty_register(struct dgnc_board *brd)
199 brd->SerialDriver.magic = TTY_DRIVER_MAGIC;
201 snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgnc_%d_", brd->boardnum);
203 brd->SerialDriver.name = brd->SerialName;
204 brd->SerialDriver.name_base = 0;
205 brd->SerialDriver.major = 0;
206 brd->SerialDriver.minor_start = 0;
207 brd->SerialDriver.num = brd->maxports;
208 brd->SerialDriver.type = TTY_DRIVER_TYPE_SERIAL;
209 brd->SerialDriver.subtype = SERIAL_TYPE_NORMAL;
210 brd->SerialDriver.init_termios = DgncDefaultTermios;
211 brd->SerialDriver.driver_name = DRVSTR;
212 brd->SerialDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
215 * The kernel wants space to store pointers to
216 * tty_struct's and termios's.
218 brd->SerialDriver.ttys = kcalloc(brd->maxports, sizeof(*brd->SerialDriver.ttys), GFP_KERNEL);
219 if (!brd->SerialDriver.ttys)
222 kref_init(&brd->SerialDriver.kref);
223 brd->SerialDriver.termios = kcalloc(brd->maxports, sizeof(*brd->SerialDriver.termios), GFP_KERNEL);
224 if (!brd->SerialDriver.termios)
228 * Entry points for driver. Called by the kernel from
229 * tty_io.c and n_tty.c.
231 tty_set_operations(&brd->SerialDriver, &dgnc_tty_ops);
233 if (!brd->dgnc_Major_Serial_Registered) {
234 /* Register tty devices */
235 rc = tty_register_driver(&brd->SerialDriver);
237 APR(("Can't register tty device (%d)\n", rc));
240 brd->dgnc_Major_Serial_Registered = TRUE;
244 * If we're doing transparent print, we have to do all of the above
245 * again, separately so we don't get the LD confused about what major
246 * we are when we get into the dgnc_tty_open() routine.
248 brd->PrintDriver.magic = TTY_DRIVER_MAGIC;
249 snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
251 brd->PrintDriver.name = brd->PrintName;
252 brd->PrintDriver.name_base = 0;
253 brd->PrintDriver.major = brd->SerialDriver.major;
254 brd->PrintDriver.minor_start = 0x80;
255 brd->PrintDriver.num = brd->maxports;
256 brd->PrintDriver.type = TTY_DRIVER_TYPE_SERIAL;
257 brd->PrintDriver.subtype = SERIAL_TYPE_NORMAL;
258 brd->PrintDriver.init_termios = DgncDefaultTermios;
259 brd->PrintDriver.driver_name = DRVSTR;
260 brd->PrintDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
263 * The kernel wants space to store pointers to
264 * tty_struct's and termios's. Must be separated from
265 * the Serial Driver so we don't get confused
267 brd->PrintDriver.ttys = kcalloc(brd->maxports, sizeof(*brd->PrintDriver.ttys), GFP_KERNEL);
268 if (!brd->PrintDriver.ttys)
270 kref_init(&brd->PrintDriver.kref);
271 brd->PrintDriver.termios = kcalloc(brd->maxports, sizeof(*brd->PrintDriver.termios), GFP_KERNEL);
272 if (!brd->PrintDriver.termios)
276 * Entry points for driver. Called by the kernel from
277 * tty_io.c and n_tty.c.
279 tty_set_operations(&brd->PrintDriver, &dgnc_tty_ops);
281 if (!brd->dgnc_Major_TransparentPrint_Registered) {
282 /* Register Transparent Print devices */
283 rc = tty_register_driver(&brd->PrintDriver);
285 APR(("Can't register Transparent Print device (%d)\n", rc));
288 brd->dgnc_Major_TransparentPrint_Registered = TRUE;
291 dgnc_BoardsByMajor[brd->SerialDriver.major] = brd;
292 brd->dgnc_Serial_Major = brd->SerialDriver.major;
293 brd->dgnc_TransparentPrint_Major = brd->PrintDriver.major;
302 * Init the tty subsystem. Called once per board after board has been
303 * downloaded and init'ed.
305 int dgnc_tty_init(struct dgnc_board *brd)
309 struct channel_t *ch;
315 * Initialize board structure elements.
318 vaddr = brd->re_map_membase;
320 brd->nasync = brd->maxports;
323 * Allocate channel memory that might not have been allocated
324 * when the driver was first loaded.
326 for (i = 0; i < brd->nasync; i++) {
327 if (!brd->channels[i]) {
330 * Okay to malloc with GFP_KERNEL, we are not at
331 * interrupt context, and there are no locks held.
333 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]), GFP_KERNEL);
337 ch = brd->channels[0];
338 vaddr = brd->re_map_membase;
340 /* Set up channel variables */
341 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
343 if (!brd->channels[i])
346 DGNC_SPINLOCK_INIT(ch->ch_lock);
348 /* Store all our magic numbers */
349 ch->magic = DGNC_CHANNEL_MAGIC;
350 ch->ch_tun.magic = DGNC_UNIT_MAGIC;
351 ch->ch_tun.un_ch = ch;
352 ch->ch_tun.un_type = DGNC_SERIAL;
353 ch->ch_tun.un_dev = i;
355 ch->ch_pun.magic = DGNC_UNIT_MAGIC;
356 ch->ch_pun.un_ch = ch;
357 ch->ch_pun.un_type = DGNC_PRINT;
358 ch->ch_pun.un_dev = i + 128;
360 if (brd->bd_uart_offset == 0x200)
361 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
363 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
367 ch->ch_digi = dgnc_digi_init;
369 /* .25 second delay */
370 ch->ch_close_delay = 250;
372 init_waitqueue_head(&ch->ch_flags_wait);
373 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
374 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
375 init_waitqueue_head(&ch->ch_sniff_wait);
378 struct device *classp;
379 classp = tty_register_device(&brd->SerialDriver, i,
380 &(ch->ch_bd->pdev->dev));
381 ch->ch_tun.un_sysfs = classp;
382 dgnc_create_tty_sysfs(&ch->ch_tun, classp);
384 classp = tty_register_device(&brd->PrintDriver, i,
385 &(ch->ch_bd->pdev->dev));
386 ch->ch_pun.un_sysfs = classp;
387 dgnc_create_tty_sysfs(&ch->ch_pun, classp);
397 * dgnc_tty_post_uninit()
399 * UnInitialize any global tty related data.
401 void dgnc_tty_post_uninit(void)
403 kfree(dgnc_TmpWriteBuf);
404 dgnc_TmpWriteBuf = NULL;
411 * Uninitialize the TTY portion of this driver. Free all memory and
414 void dgnc_tty_uninit(struct dgnc_board *brd)
418 if (brd->dgnc_Major_Serial_Registered) {
419 dgnc_BoardsByMajor[brd->SerialDriver.major] = NULL;
420 brd->dgnc_Serial_Major = 0;
421 for (i = 0; i < brd->nasync; i++) {
422 dgnc_remove_tty_sysfs(brd->channels[i]->ch_tun.un_sysfs);
423 tty_unregister_device(&brd->SerialDriver, i);
425 tty_unregister_driver(&brd->SerialDriver);
426 brd->dgnc_Major_Serial_Registered = FALSE;
429 if (brd->dgnc_Major_TransparentPrint_Registered) {
430 dgnc_BoardsByMajor[brd->PrintDriver.major] = NULL;
431 brd->dgnc_TransparentPrint_Major = 0;
432 for (i = 0; i < brd->nasync; i++) {
433 dgnc_remove_tty_sysfs(brd->channels[i]->ch_pun.un_sysfs);
434 tty_unregister_device(&brd->PrintDriver, i);
436 tty_unregister_driver(&brd->PrintDriver);
437 brd->dgnc_Major_TransparentPrint_Registered = FALSE;
440 kfree(brd->SerialDriver.ttys);
441 brd->SerialDriver.ttys = NULL;
442 kfree(brd->PrintDriver.ttys);
443 brd->PrintDriver.ttys = NULL;
447 #define TMPBUFLEN (1024)
450 * dgnc_sniff - Dump data out to the "sniff" buffer if the
451 * proc sniff file is opened...
453 void dgnc_sniff_nowait_nolock(struct channel_t *ch, uchar *text, uchar *buf, int len)
465 tmpbuf = kzalloc(TMPBUFLEN, GFP_ATOMIC);
470 /* Leave if sniff not open */
471 if (!(ch->ch_sniff_flags & SNIFF_OPEN))
474 do_gettimeofday(&tv);
476 /* Create our header for data dump */
477 p += sprintf(p, "<%ld %ld><%s><", tv.tv_sec, tv.tv_usec, text);
478 tmpbuflen = p - tmpbuf;
483 for (i = 0; i < len && tmpbuflen < (TMPBUFLEN - 4); i++) {
484 p += sprintf(p, "%02x ", *buf);
486 tmpbuflen = p - tmpbuf;
489 if (tmpbuflen < (TMPBUFLEN - 4)) {
491 p += sprintf(p - 1, "%s\n", ">");
493 p += sprintf(p, "%s\n", ">");
499 nbuf = strlen(tmpbuf);
503 * Loop while data remains.
505 while (nbuf > 0 && ch->ch_sniff_buf) {
507 * Determine the amount of available space left in the
508 * buffer. If there's none, wait until some appears.
510 n = (ch->ch_sniff_out - ch->ch_sniff_in - 1) & SNIFF_MASK;
513 * If there is no space left to write to in our sniff buffer,
514 * we have no choice but to drop the data.
515 * We *cannot* sleep here waiting for space, because this
516 * function was probably called by the interrupt/timer routines!
522 * Copy as much data as will fit.
528 r = SNIFF_MAX - ch->ch_sniff_in;
531 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, r);
539 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, n);
541 ch->ch_sniff_in += n;
546 * Wakeup any thread waiting for data
548 if (ch->ch_sniff_flags & SNIFF_WAIT_DATA) {
549 ch->ch_sniff_flags &= ~SNIFF_WAIT_DATA;
550 wake_up_interruptible(&ch->ch_sniff_wait);
555 * If the user sent us too much data to push into our tmpbuf,
556 * we need to keep looping around on all the data.
563 } while (too_much_data);
570 /*=======================================================================
572 * dgnc_wmove - Write data to transmit queue.
574 * ch - Pointer to channel structure.
575 * buf - Poiter to characters to be moved.
576 * n - Number of characters to move.
578 *=======================================================================*/
579 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
584 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
587 head = ch->ch_w_head & WQUEUEMASK;
590 * If the write wraps over the top of the circular buffer,
591 * move the portion up to the wrap point, and reset the
592 * pointers to the bottom.
594 remain = WQUEUESIZE - head;
598 memcpy(ch->ch_wqueue + head, buf, remain);
608 memcpy(ch->ch_wqueue + head, buf, remain);
613 ch->ch_w_head = head;
619 /*=======================================================================
621 * dgnc_input - Process received data.
623 * ch - Pointer to channel structure.
625 *=======================================================================*/
626 void dgnc_input(struct channel_t *ch)
628 struct dgnc_board *bd;
629 struct tty_struct *tp;
630 struct tty_ldisc *ld;
642 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
645 tp = ch->ch_tun.un_tty;
648 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
651 DGNC_LOCK(ch->ch_lock, lock_flags);
654 * Figure the number of characters in the buffer.
655 * Exit immediately if none.
658 head = ch->ch_r_head & rmask;
659 tail = ch->ch_r_tail & rmask;
660 data_len = (head - tail) & rmask;
663 DGNC_UNLOCK(ch->ch_lock, lock_flags);
668 * If the device is not open, or CREAD is off,
669 * flush input data and return immediately.
671 if (!tp || (tp->magic != TTY_MAGIC) || !(ch->ch_tun.un_flags & UN_ISOPEN) ||
672 !(tp->termios.c_cflag & CREAD) || (ch->ch_tun.un_flags & UN_CLOSING)) {
674 ch->ch_r_head = tail;
676 /* Force queue flow control to be released, if needed */
677 dgnc_check_queue_flow_control(ch);
679 DGNC_UNLOCK(ch->ch_lock, lock_flags);
684 * If we are throttled, simply don't read any data.
686 if (ch->ch_flags & CH_FORCED_STOPI) {
687 DGNC_UNLOCK(ch->ch_lock, lock_flags);
691 flip_len = TTY_FLIPBUF_SIZE;
693 /* Chop down the length, if needed */
694 len = min(data_len, flip_len);
695 len = min(len, (N_TTY_BUF_SIZE - 1));
697 ld = tty_ldisc_ref(tp);
701 * If the DONT_FLIP flag is on, don't flush our buffer, and act
702 * like the ld doesn't have any space to put the data right now.
704 if (test_bit(TTY_DONT_FLIP, &tp->flags))
709 * If we were unable to get a reference to the ld,
710 * don't flush our buffer, and act like the ld doesn't
711 * have any space to put the data right now.
717 * If ld doesn't have a pointer to a receive_buf function,
718 * flush the data, then act like the ld doesn't have any
719 * space to put the data right now.
721 if (!ld->ops->receive_buf) {
722 ch->ch_r_head = ch->ch_r_tail;
728 DGNC_UNLOCK(ch->ch_lock, lock_flags);
735 * The tty layer in the kernel has changed in 2.6.16+.
737 * The flip buffers in the tty structure are no longer exposed,
738 * and probably will be going away eventually.
740 * If we are completely raw, we don't need to go through a lot
741 * of the tty layers that exist.
742 * In this case, we take the shortest and fastest route we
743 * can to relay the data to the user.
745 * On the other hand, if we are not raw, we need to go through
746 * the new 2.6.16+ tty layer, which has its API more well defined.
748 len = tty_buffer_request_room(tp->port, len);
752 * n now contains the most amount of data we can copy,
753 * bounded either by how much the Linux tty layer can handle,
754 * or the amount of data the card actually has pending...
757 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
764 * If conditions are such that ld needs to see all
765 * UART errors, we will have to walk each character
766 * and error byte and send them to the buffer one at
769 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
770 for (i = 0; i < s; i++) {
771 if (*(ch->ch_equeue + tail + i) & UART_LSR_BI)
772 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_BREAK);
773 else if (*(ch->ch_equeue + tail + i) & UART_LSR_PE)
774 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_PARITY);
775 else if (*(ch->ch_equeue + tail + i) & UART_LSR_FE)
776 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_FRAME);
778 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_NORMAL);
781 tty_insert_flip_string(tp->port, ch->ch_rqueue + tail, s);
784 dgnc_sniff_nowait_nolock(ch, "USER READ", ch->ch_rqueue + tail, s);
788 /* Flip queue if needed */
792 ch->ch_r_tail = tail & rmask;
793 ch->ch_e_tail = tail & rmask;
794 dgnc_check_queue_flow_control(ch);
795 DGNC_UNLOCK(ch->ch_lock, lock_flags);
797 /* Tell the tty layer its okay to "eat" the data now */
798 tty_flip_buffer_push(tp->port);
805 /************************************************************************
806 * Determines when CARRIER changes state and takes appropriate
808 ************************************************************************/
809 void dgnc_carrier(struct channel_t *ch)
811 struct dgnc_board *bd;
813 int virt_carrier = 0;
814 int phys_carrier = 0;
816 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
821 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
824 if (ch->ch_mistat & UART_MSR_DCD)
827 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
830 if (ch->ch_c_cflag & CLOCAL)
834 * Test for a VIRTUAL carrier transition to HIGH.
836 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
839 * When carrier rises, wake any threads waiting
840 * for carrier in the open routine.
843 if (waitqueue_active(&(ch->ch_flags_wait)))
844 wake_up_interruptible(&ch->ch_flags_wait);
848 * Test for a PHYSICAL carrier transition to HIGH.
850 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
853 * When carrier rises, wake any threads waiting
854 * for carrier in the open routine.
857 if (waitqueue_active(&(ch->ch_flags_wait)))
858 wake_up_interruptible(&ch->ch_flags_wait);
862 * Test for a PHYSICAL transition to low, so long as we aren't
863 * currently ignoring physical transitions (which is what "virtual
864 * carrier" indicates).
866 * The transition of the virtual carrier to low really doesn't
867 * matter... it really only means "ignore carrier state", not
868 * "make pretend that carrier is there".
870 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
875 * When carrier drops:
877 * Drop carrier on all open units.
879 * Flush queues, waking up any task waiting in the
882 * Send a hangup to the control terminal.
884 * Enable all select calls.
886 if (waitqueue_active(&(ch->ch_flags_wait)))
887 wake_up_interruptible(&ch->ch_flags_wait);
889 if (ch->ch_tun.un_open_count > 0)
890 tty_hangup(ch->ch_tun.un_tty);
892 if (ch->ch_pun.un_open_count > 0)
893 tty_hangup(ch->ch_pun.un_tty);
897 * Make sure that our cached values reflect the current reality.
899 if (virt_carrier == 1)
900 ch->ch_flags |= CH_FCAR;
902 ch->ch_flags &= ~CH_FCAR;
904 if (phys_carrier == 1)
905 ch->ch_flags |= CH_CD;
907 ch->ch_flags &= ~CH_CD;
911 * Assign the custom baud rate to the channel structure
913 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
922 ch->ch_custom_speed = 0;
927 * Since the divisor is stored in a 16-bit integer, we make sure
928 * we don't allow any rates smaller than a 16-bit integer would allow.
929 * And of course, rates above the dividend won't fly.
931 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
932 newrate = ((ch->ch_bd->bd_dividend / 0xFFFF) + 1);
934 if (newrate && newrate > ch->ch_bd->bd_dividend)
935 newrate = ch->ch_bd->bd_dividend;
938 testdiv = ch->ch_bd->bd_dividend / newrate;
941 * If we try to figure out what rate the board would use
942 * with the test divisor, it will be either equal or higher
943 * than the requested baud rate. If we then determine the
944 * rate with a divisor one higher, we will get the next lower
945 * supported rate below the requested.
947 testrate_high = ch->ch_bd->bd_dividend / testdiv;
948 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
951 * If the rate for the requested divisor is correct, just
952 * use it and be done.
954 if (testrate_high != newrate) {
956 * Otherwise, pick the rate that is closer (i.e. whichever rate
957 * has a smaller delta).
959 deltahigh = testrate_high - newrate;
960 deltalow = newrate - testrate_low;
962 if (deltahigh < deltalow)
963 newrate = testrate_high;
965 newrate = testrate_low;
969 ch->ch_custom_speed = newrate;
973 void dgnc_check_queue_flow_control(struct channel_t *ch)
977 /* Store how much space we have left in the queue */
978 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
980 qleft += RQUEUEMASK + 1;
983 * Check to see if we should enforce flow control on our queue because
984 * the ld (or user) isn't reading data out of our queue fast enuf.
986 * NOTE: This is done based on what the current flow control of the
989 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
990 * This will cause the UART's FIFO to back up, and force
991 * the RTS signal to be dropped.
992 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
993 * the other side, in hopes it will stop sending data to us.
994 * 3) NONE - Nothing we can do. We will simply drop any extra data
995 * that gets sent into us when the queue fills up.
999 if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
1000 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
1001 ch->ch_bd->bd_ops->disable_receiver(ch);
1002 ch->ch_flags |= (CH_RECEIVER_OFF);
1006 else if (ch->ch_c_iflag & IXOFF) {
1007 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
1008 ch->ch_bd->bd_ops->send_stop_character(ch);
1009 ch->ch_stops_sent++;
1014 /* Empty... Can't do anything about the impending overflow... */
1019 * Check to see if we should unenforce flow control because
1020 * ld (or user) finally read enuf data out of our queue.
1022 * NOTE: This is done based on what the current flow control of the
1025 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
1026 * This will cause the UART's FIFO to raise RTS back up,
1027 * which will allow the other side to start sending data again.
1028 * 2) SWFLOW (IXOFF) - Send a start character to
1029 * the other side, so it will start sending data to us again.
1030 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
1031 * other side, we don't need to do anything now.
1033 if (qleft > (RQUEUESIZE / 2)) {
1035 if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
1036 if (ch->ch_flags & CH_RECEIVER_OFF) {
1037 ch->ch_bd->bd_ops->enable_receiver(ch);
1038 ch->ch_flags &= ~(CH_RECEIVER_OFF);
1042 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
1043 ch->ch_stops_sent = 0;
1044 ch->ch_bd->bd_ops->send_start_character(ch);
1048 /* Nothing needed. */
1054 void dgnc_wakeup_writes(struct channel_t *ch)
1059 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1062 DGNC_LOCK(ch->ch_lock, lock_flags);
1065 * If channel now has space, wake up anyone waiting on the condition.
1067 qlen = ch->ch_w_head - ch->ch_w_tail;
1071 if (qlen >= (WQUEUESIZE - 256)) {
1072 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1076 if (ch->ch_tun.un_flags & UN_ISOPEN) {
1077 if ((ch->ch_tun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1078 ch->ch_tun.un_tty->ldisc->ops->write_wakeup)
1080 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1081 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
1082 DGNC_LOCK(ch->ch_lock, lock_flags);
1085 wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
1088 * If unit is set to wait until empty, check to make sure
1089 * the queue AND FIFO are both empty.
1091 if (ch->ch_tun.un_flags & UN_EMPTY) {
1092 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
1093 ch->ch_tun.un_flags &= ~(UN_EMPTY);
1096 * If RTS Toggle mode is on, whenever
1097 * the queue and UART is empty, keep RTS low.
1099 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1100 ch->ch_mostat &= ~(UART_MCR_RTS);
1101 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1105 * If DTR Toggle mode is on, whenever
1106 * the queue and UART is empty, keep DTR low.
1108 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1109 ch->ch_mostat &= ~(UART_MCR_DTR);
1110 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1115 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
1118 if (ch->ch_pun.un_flags & UN_ISOPEN) {
1119 if ((ch->ch_pun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1120 ch->ch_pun.un_tty->ldisc->ops->write_wakeup)
1122 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1123 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
1124 DGNC_LOCK(ch->ch_lock, lock_flags);
1127 wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
1130 * If unit is set to wait until empty, check to make sure
1131 * the queue AND FIFO are both empty.
1133 if (ch->ch_pun.un_flags & UN_EMPTY) {
1134 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
1135 ch->ch_pun.un_flags &= ~(UN_EMPTY);
1139 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1142 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1147 /************************************************************************
1149 * TTY Entry points and helper functions
1151 ************************************************************************/
1157 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
1159 struct dgnc_board *brd;
1160 struct channel_t *ch;
1169 major = MAJOR(tty_devnum(tty));
1170 minor = MINOR(tty_devnum(tty));
1175 /* Get board pointer from our array of majors we have allocated */
1176 brd = dgnc_BoardsByMajor[major];
1181 * If board is not yet up to a state of READY, go to
1182 * sleep waiting for it to happen or they cancel the open.
1184 rc = wait_event_interruptible(brd->state_wait,
1185 (brd->state & BOARD_READY));
1190 DGNC_LOCK(brd->bd_lock, lock_flags);
1192 /* If opened device is greater than our number of ports, bail. */
1193 if (PORT_NUM(minor) > brd->nasync) {
1194 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1198 ch = brd->channels[PORT_NUM(minor)];
1200 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1204 /* Drop board lock */
1205 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1207 /* Grab channel lock */
1208 DGNC_LOCK(ch->ch_lock, lock_flags);
1210 /* Figure out our type */
1211 if (!IS_PRINT(minor)) {
1212 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1213 un->un_type = DGNC_SERIAL;
1214 } else if (IS_PRINT(minor)) {
1215 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1216 un->un_type = DGNC_PRINT;
1218 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1223 * If the port is still in a previous open, and in a state
1224 * where we simply cannot safely keep going, wait until the
1227 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1229 rc = wait_event_interruptible(ch->ch_flags_wait, ((ch->ch_flags & CH_OPENING) == 0));
1231 /* If ret is non-zero, user ctrl-c'ed us */
1236 * If either unit is in the middle of the fragile part of close,
1237 * we just cannot touch the channel safely.
1238 * Go to sleep, knowing that when the channel can be
1239 * touched safely, the close routine will signal the
1240 * ch_flags_wait to wake us back up.
1242 rc = wait_event_interruptible(ch->ch_flags_wait,
1243 (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING) == 0));
1245 /* If ret is non-zero, user ctrl-c'ed us */
1249 DGNC_LOCK(ch->ch_lock, lock_flags);
1252 /* Store our unit into driver_data, so we always have it available. */
1253 tty->driver_data = un;
1259 if (!(un->un_flags & UN_ISOPEN)) {
1260 /* Store important variables. */
1263 /* Maybe do something here to the TTY struct as well? */
1268 * Allocate channel buffers for read/write/error.
1269 * Set flag, so we don't get trounced on.
1271 ch->ch_flags |= (CH_OPENING);
1273 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1274 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1277 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1279 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1281 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1283 DGNC_LOCK(ch->ch_lock, lock_flags);
1285 ch->ch_flags &= ~(CH_OPENING);
1286 wake_up_interruptible(&ch->ch_flags_wait);
1289 * Initialize if neither terminal or printer is open.
1291 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1294 * Flush input queues.
1303 brd->bd_ops->flush_uart_write(ch);
1304 brd->bd_ops->flush_uart_read(ch);
1307 ch->ch_cached_lsr = 0;
1308 ch->ch_stop_sending_break = 0;
1309 ch->ch_stops_sent = 0;
1311 ch->ch_c_cflag = tty->termios.c_cflag;
1312 ch->ch_c_iflag = tty->termios.c_iflag;
1313 ch->ch_c_oflag = tty->termios.c_oflag;
1314 ch->ch_c_lflag = tty->termios.c_lflag;
1315 ch->ch_startc = tty->termios.c_cc[VSTART];
1316 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1319 * Bring up RTS and DTR...
1320 * Also handle RTS or DTR toggle if set.
1322 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1323 ch->ch_mostat |= (UART_MCR_RTS);
1324 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1325 ch->ch_mostat |= (UART_MCR_DTR);
1327 /* Tell UART to init itself */
1328 brd->bd_ops->uart_init(ch);
1332 * Run param in case we changed anything
1334 brd->bd_ops->param(tty);
1339 * follow protocol for opening port
1342 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1344 rc = dgnc_block_til_ready(tty, file, ch);
1346 /* No going back now, increment our unit and channel counters */
1347 DGNC_LOCK(ch->ch_lock, lock_flags);
1348 ch->ch_open_count++;
1349 un->un_open_count++;
1350 un->un_flags |= (UN_ISOPEN);
1351 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1358 * dgnc_block_til_ready()
1360 * Wait for DCD, if needed.
1362 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch)
1365 struct un_t *un = NULL;
1368 int sleep_on_un_flags = 0;
1370 if (!tty || tty->magic != TTY_MAGIC || !file || !ch || ch->magic != DGNC_CHANNEL_MAGIC) {
1374 un = tty->driver_data;
1375 if (!un || un->magic != DGNC_UNIT_MAGIC)
1378 DGNC_LOCK(ch->ch_lock, lock_flags);
1385 sleep_on_un_flags = 0;
1388 * If board has failed somehow during our sleep, bail with error.
1390 if (ch->ch_bd->state == BOARD_FAILED) {
1395 /* If tty was hung up, break out of loop and set error. */
1396 if (tty_hung_up_p(file)) {
1402 * If either unit is in the middle of the fragile part of close,
1403 * we just cannot touch the channel safely.
1404 * Go back to sleep, knowing that when the channel can be
1405 * touched safely, the close routine will signal the
1406 * ch_wait_flags to wake us back up.
1408 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING)) {
1411 * Our conditions to leave cleanly and happily:
1412 * 1) NONBLOCKING on the tty is set.
1414 * 3) DCD (fake or real) is active.
1417 if (file->f_flags & O_NONBLOCK)
1420 if (tty->flags & (1 << TTY_IO_ERROR)) {
1425 if (ch->ch_flags & CH_CD)
1428 if (ch->ch_flags & CH_FCAR)
1431 sleep_on_un_flags = 1;
1435 * If there is a signal pending, the user probably
1436 * interrupted (ctrl-c) us.
1437 * Leave loop with error set.
1439 if (signal_pending(current)) {
1440 retval = -ERESTARTSYS;
1445 * Store the flags before we let go of channel lock
1447 if (sleep_on_un_flags)
1448 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1450 old_flags = ch->ch_flags;
1453 * Let go of channel lock before calling schedule.
1454 * Our poller will get any FEP events and wake us up when DCD
1455 * eventually goes active.
1458 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1461 * Wait for something in the flags to change from the current value.
1463 if (sleep_on_un_flags)
1464 retval = wait_event_interruptible(un->un_flags_wait,
1465 (old_flags != (ch->ch_tun.un_flags | ch->ch_pun.un_flags)));
1467 retval = wait_event_interruptible(ch->ch_flags_wait,
1468 (old_flags != ch->ch_flags));
1471 * We got woken up for some reason.
1472 * Before looping around, grab our channel lock.
1474 DGNC_LOCK(ch->ch_lock, lock_flags);
1479 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1491 * Hangup the port. Like a close, but don't wait for output to drain.
1493 static void dgnc_tty_hangup(struct tty_struct *tty)
1497 if (!tty || tty->magic != TTY_MAGIC)
1500 un = tty->driver_data;
1501 if (!un || un->magic != DGNC_UNIT_MAGIC)
1504 /* flush the transmit queues */
1505 dgnc_tty_flush_buffer(tty);
1514 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1516 struct ktermios *ts;
1517 struct dgnc_board *bd;
1518 struct channel_t *ch;
1523 if (!tty || tty->magic != TTY_MAGIC)
1526 un = tty->driver_data;
1527 if (!un || un->magic != DGNC_UNIT_MAGIC)
1531 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1535 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1540 DGNC_LOCK(ch->ch_lock, lock_flags);
1543 * Determine if this is the last close or not - and if we agree about
1544 * which type of close it is with the Line Discipline
1546 if ((tty->count == 1) && (un->un_open_count != 1)) {
1548 * Uh, oh. tty->count is 1, which means that the tty
1549 * structure will be freed. un_open_count should always
1550 * be one in these conditions. If it's greater than
1551 * one, we've got real problems, since it means the
1552 * serial port won't be shutdown.
1554 APR(("tty->count is 1, un open count is %d\n", un->un_open_count));
1555 un->un_open_count = 1;
1558 if (un->un_open_count)
1559 un->un_open_count--;
1561 APR(("bad serial port open count of %d\n", un->un_open_count));
1563 ch->ch_open_count--;
1565 if (ch->ch_open_count && un->un_open_count) {
1566 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1570 /* OK, its the last close on the unit */
1571 un->un_flags |= UN_CLOSING;
1577 * Only officially close channel if count is 0 and
1578 * DIGI_PRINTER bit is not set.
1580 if ((ch->ch_open_count == 0) && !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1582 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1585 * turn off print device when closing print device.
1587 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1588 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1589 (int) ch->ch_digi.digi_offlen);
1590 ch->ch_flags &= ~CH_PRON;
1593 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1594 /* wait for output to drain */
1595 /* This will also return if we take an interrupt */
1597 rc = bd->bd_ops->drain(tty, 0);
1599 dgnc_tty_flush_buffer(tty);
1600 tty_ldisc_flush(tty);
1602 DGNC_LOCK(ch->ch_lock, lock_flags);
1607 * If we have HUPCL set, lower DTR and RTS
1609 if (ch->ch_c_cflag & HUPCL) {
1612 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1613 bd->bd_ops->assert_modem_signals(ch);
1616 * Go to sleep to ensure RTS/DTR
1617 * have been dropped for modems to see it.
1619 if (ch->ch_close_delay) {
1620 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1621 dgnc_ms_sleep(ch->ch_close_delay);
1622 DGNC_LOCK(ch->ch_lock, lock_flags);
1626 ch->ch_old_baud = 0;
1628 /* Turn off UART interrupts for this port */
1629 ch->ch_bd->bd_ops->uart_off(ch);
1632 * turn off print device when closing print device.
1634 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1635 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1636 (int) ch->ch_digi.digi_offlen);
1637 ch->ch_flags &= ~CH_PRON;
1642 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1644 wake_up_interruptible(&ch->ch_flags_wait);
1645 wake_up_interruptible(&un->un_flags_wait);
1647 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1652 * dgnc_tty_chars_in_buffer()
1654 * Return number of characters that have not been transmitted yet.
1656 * This routine is used by the line discipline to determine if there
1657 * is data waiting to be transmitted/drained/flushed or not.
1659 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1661 struct channel_t *ch = NULL;
1662 struct un_t *un = NULL;
1667 ulong lock_flags = 0;
1672 un = tty->driver_data;
1673 if (!un || un->magic != DGNC_UNIT_MAGIC)
1677 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1680 DGNC_LOCK(ch->ch_lock, lock_flags);
1683 thead = ch->ch_w_head & tmask;
1684 ttail = ch->ch_w_tail & tmask;
1686 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1688 if (ttail == thead) {
1692 chars = thead - ttail;
1694 chars = thead - ttail + WQUEUESIZE;
1704 * Reduces bytes_available to the max number of characters
1705 * that can be sent currently given the maxcps value, and
1706 * returns the new bytes_available. This only affects printer
1709 static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1711 struct channel_t *ch = NULL;
1712 struct un_t *un = NULL;
1715 return bytes_available;
1717 un = tty->driver_data;
1718 if (!un || un->magic != DGNC_UNIT_MAGIC)
1719 return bytes_available;
1722 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1723 return bytes_available;
1726 * If its not the Transparent print device, return
1727 * the full data amount.
1729 if (un->un_type != DGNC_PRINT)
1730 return bytes_available;
1732 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1734 unsigned long current_time = jiffies;
1735 unsigned long buffer_time = current_time +
1736 (HZ * ch->ch_digi.digi_bufsize) / ch->ch_digi.digi_maxcps;
1738 if (ch->ch_cpstime < current_time) {
1739 /* buffer is empty */
1740 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1741 cps_limit = ch->ch_digi.digi_bufsize;
1742 } else if (ch->ch_cpstime < buffer_time) {
1743 /* still room in the buffer */
1744 cps_limit = ((buffer_time - ch->ch_cpstime) * ch->ch_digi.digi_maxcps) / HZ;
1746 /* no room in the buffer */
1750 bytes_available = min(cps_limit, bytes_available);
1753 return bytes_available;
1758 * dgnc_tty_write_room()
1760 * Return space available in Tx buffer
1762 static int dgnc_tty_write_room(struct tty_struct *tty)
1764 struct channel_t *ch = NULL;
1765 struct un_t *un = NULL;
1770 ulong lock_flags = 0;
1772 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1775 un = tty->driver_data;
1776 if (!un || un->magic != DGNC_UNIT_MAGIC)
1780 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1783 DGNC_LOCK(ch->ch_lock, lock_flags);
1786 head = (ch->ch_w_head) & tmask;
1787 tail = (ch->ch_w_tail) & tmask;
1789 ret = tail - head - 1;
1793 /* Limit printer to maxcps */
1794 ret = dgnc_maxcps_room(tty, ret);
1797 * If we are printer device, leave space for
1798 * possibly both the on and off strings.
1800 if (un->un_type == DGNC_PRINT) {
1801 if (!(ch->ch_flags & CH_PRON))
1802 ret -= ch->ch_digi.digi_onlen;
1803 ret -= ch->ch_digi.digi_offlen;
1805 if (ch->ch_flags & CH_PRON)
1806 ret -= ch->ch_digi.digi_offlen;
1812 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1819 * dgnc_tty_put_char()
1821 * Put a character into ch->ch_buf
1823 * - used by the line discipline for OPOST processing
1825 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1828 * Simply call tty_write.
1830 dgnc_tty_write(tty, &c, 1);
1838 * Take data from the user or kernel and send it out to the FEP.
1839 * In here exists all the Transparent Print magic as well.
1841 static int dgnc_tty_write(struct tty_struct *tty,
1842 const unsigned char *buf, int count)
1844 struct channel_t *ch = NULL;
1845 struct un_t *un = NULL;
1846 int bufcount = 0, n = 0;
1855 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1858 un = tty->driver_data;
1859 if (!un || un->magic != DGNC_UNIT_MAGIC)
1863 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1870 * Store original amount of characters passed in.
1871 * This helps to figure out if we should ask the FEP
1872 * to send us an event when it has more space available.
1876 DGNC_LOCK(ch->ch_lock, lock_flags);
1878 /* Get our space available for the channel from the board */
1880 head = (ch->ch_w_head) & tmask;
1881 tail = (ch->ch_w_tail) & tmask;
1883 bufcount = tail - head - 1;
1885 bufcount += WQUEUESIZE;
1888 * Limit printer output to maxcps overall, with bursts allowed
1889 * up to bufsize characters.
1891 bufcount = dgnc_maxcps_room(tty, bufcount);
1894 * Take minimum of what the user wants to send, and the
1895 * space available in the FEP buffer.
1897 count = min(count, bufcount);
1900 * Bail if no space left.
1903 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1908 * Output the printer ON string, if we are in terminal mode, but
1909 * need to be in printer mode.
1911 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1912 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1913 (int) ch->ch_digi.digi_onlen);
1914 head = (ch->ch_w_head) & tmask;
1915 ch->ch_flags |= CH_PRON;
1919 * On the other hand, output the printer OFF string, if we are
1920 * currently in printer mode, but need to output to the terminal.
1922 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1923 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1924 (int) ch->ch_digi.digi_offlen);
1925 head = (ch->ch_w_head) & tmask;
1926 ch->ch_flags &= ~CH_PRON;
1930 * If there is nothing left to copy, or I can't handle any more data, leave.
1933 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1939 count = min(count, WRITEBUFLEN);
1941 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1944 * If data is coming from user space, copy it into a temporary
1945 * buffer so we don't get swapped out while doing the copy to
1948 /* we're allowed to block if it's from_user */
1949 if (down_interruptible(&dgnc_TmpWriteSem))
1953 * copy_from_user() returns the number
1954 * of bytes that could *NOT* be copied.
1956 count -= copy_from_user(dgnc_TmpWriteBuf, (const uchar __user *) buf, count);
1959 up(&dgnc_TmpWriteSem);
1963 DGNC_LOCK(ch->ch_lock, lock_flags);
1965 buf = dgnc_TmpWriteBuf;
1972 * If the write wraps over the top of the circular buffer,
1973 * move the portion up to the wrap point, and reset the
1974 * pointers to the bottom.
1976 remain = WQUEUESIZE - head;
1980 memcpy(ch->ch_wqueue + head, buf, remain);
1981 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
1988 * Move rest of data.
1991 memcpy(ch->ch_wqueue + head, buf, remain);
1992 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
1998 ch->ch_w_head = head;
2001 /* Update printer buffer empty time. */
2002 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
2003 && (ch->ch_digi.digi_bufsize > 0)) {
2004 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
2008 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2009 up(&dgnc_TmpWriteSem);
2011 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2016 * Channel lock is grabbed and then released
2017 * inside this routine.
2019 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
2027 * Return modem signals to ld.
2030 static int dgnc_tty_tiocmget(struct tty_struct *tty)
2032 struct channel_t *ch;
2038 if (!tty || tty->magic != TTY_MAGIC)
2041 un = tty->driver_data;
2042 if (!un || un->magic != DGNC_UNIT_MAGIC)
2046 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2049 DGNC_LOCK(ch->ch_lock, lock_flags);
2051 mstat = (ch->ch_mostat | ch->ch_mistat);
2053 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2057 if (mstat & UART_MCR_DTR)
2058 result |= TIOCM_DTR;
2059 if (mstat & UART_MCR_RTS)
2060 result |= TIOCM_RTS;
2061 if (mstat & UART_MSR_CTS)
2062 result |= TIOCM_CTS;
2063 if (mstat & UART_MSR_DSR)
2064 result |= TIOCM_DSR;
2065 if (mstat & UART_MSR_RI)
2067 if (mstat & UART_MSR_DCD)
2075 * dgnc_tty_tiocmset()
2077 * Set modem signals, called by ld.
2080 static int dgnc_tty_tiocmset(struct tty_struct *tty,
2081 unsigned int set, unsigned int clear)
2083 struct dgnc_board *bd;
2084 struct channel_t *ch;
2089 if (!tty || tty->magic != TTY_MAGIC)
2092 un = tty->driver_data;
2093 if (!un || un->magic != DGNC_UNIT_MAGIC)
2097 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2101 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2104 DGNC_LOCK(ch->ch_lock, lock_flags);
2106 if (set & TIOCM_RTS)
2107 ch->ch_mostat |= UART_MCR_RTS;
2109 if (set & TIOCM_DTR)
2110 ch->ch_mostat |= UART_MCR_DTR;
2112 if (clear & TIOCM_RTS)
2113 ch->ch_mostat &= ~(UART_MCR_RTS);
2115 if (clear & TIOCM_DTR)
2116 ch->ch_mostat &= ~(UART_MCR_DTR);
2118 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2120 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2127 * dgnc_tty_send_break()
2129 * Send a Break, called by ld.
2131 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
2133 struct dgnc_board *bd;
2134 struct channel_t *ch;
2139 if (!tty || tty->magic != TTY_MAGIC)
2142 un = tty->driver_data;
2143 if (!un || un->magic != DGNC_UNIT_MAGIC)
2147 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2151 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2165 DGNC_LOCK(ch->ch_lock, lock_flags);
2167 ch->ch_bd->bd_ops->send_break(ch, msec);
2169 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2177 * dgnc_tty_wait_until_sent()
2179 * wait until data has been transmitted, called by ld.
2181 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
2183 struct dgnc_board *bd;
2184 struct channel_t *ch;
2188 if (!tty || tty->magic != TTY_MAGIC)
2191 un = tty->driver_data;
2192 if (!un || un->magic != DGNC_UNIT_MAGIC)
2196 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2200 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2203 rc = bd->bd_ops->drain(tty, 0);
2212 * send a high priority character, called by ld.
2214 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
2216 struct dgnc_board *bd;
2217 struct channel_t *ch;
2221 if (!tty || tty->magic != TTY_MAGIC)
2224 un = tty->driver_data;
2225 if (!un || un->magic != DGNC_UNIT_MAGIC)
2229 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2233 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2236 dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
2238 DGNC_LOCK(ch->ch_lock, lock_flags);
2239 bd->bd_ops->send_immediate_char(ch, c);
2240 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2242 dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
2250 * Return modem signals to ld.
2252 static inline int dgnc_get_mstat(struct channel_t *ch)
2254 unsigned char mstat;
2258 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2261 DGNC_LOCK(ch->ch_lock, lock_flags);
2263 mstat = (ch->ch_mostat | ch->ch_mistat);
2265 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2269 if (mstat & UART_MCR_DTR)
2270 result |= TIOCM_DTR;
2271 if (mstat & UART_MCR_RTS)
2272 result |= TIOCM_RTS;
2273 if (mstat & UART_MSR_CTS)
2274 result |= TIOCM_CTS;
2275 if (mstat & UART_MSR_DSR)
2276 result |= TIOCM_DSR;
2277 if (mstat & UART_MSR_RI)
2279 if (mstat & UART_MSR_DCD)
2288 * Return modem signals to ld.
2290 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value)
2295 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2298 result = dgnc_get_mstat(ch);
2303 rc = put_user(result, value);
2310 * dgnc_set_modem_info()
2312 * Set modem signals, called by ld.
2314 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value)
2316 struct dgnc_board *bd;
2317 struct channel_t *ch;
2320 unsigned int arg = 0;
2323 if (!tty || tty->magic != TTY_MAGIC)
2326 un = tty->driver_data;
2327 if (!un || un->magic != DGNC_UNIT_MAGIC)
2331 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2335 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2340 ret = get_user(arg, value);
2346 if (arg & TIOCM_RTS)
2347 ch->ch_mostat |= UART_MCR_RTS;
2349 if (arg & TIOCM_DTR)
2350 ch->ch_mostat |= UART_MCR_DTR;
2355 if (arg & TIOCM_RTS)
2356 ch->ch_mostat &= ~(UART_MCR_RTS);
2358 if (arg & TIOCM_DTR)
2359 ch->ch_mostat &= ~(UART_MCR_DTR);
2365 if (arg & TIOCM_RTS)
2366 ch->ch_mostat |= UART_MCR_RTS;
2368 ch->ch_mostat &= ~(UART_MCR_RTS);
2370 if (arg & TIOCM_DTR)
2371 ch->ch_mostat |= UART_MCR_DTR;
2373 ch->ch_mostat &= ~(UART_MCR_DTR);
2381 DGNC_LOCK(ch->ch_lock, lock_flags);
2383 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2385 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2392 * dgnc_tty_digigeta()
2394 * Ioctl to get the information for ditty.
2399 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo)
2401 struct channel_t *ch;
2409 if (!tty || tty->magic != TTY_MAGIC)
2412 un = tty->driver_data;
2413 if (!un || un->magic != DGNC_UNIT_MAGIC)
2417 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2420 memset(&tmp, 0, sizeof(tmp));
2422 DGNC_LOCK(ch->ch_lock, lock_flags);
2423 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2424 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2426 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2434 * dgnc_tty_digiseta()
2436 * Ioctl to set the information for ditty.
2441 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info)
2443 struct dgnc_board *bd;
2444 struct channel_t *ch;
2446 struct digi_t new_digi;
2449 if (!tty || tty->magic != TTY_MAGIC)
2452 un = tty->driver_data;
2453 if (!un || un->magic != DGNC_UNIT_MAGIC)
2457 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2461 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2464 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
2467 DGNC_LOCK(ch->ch_lock, lock_flags);
2470 * Handle transistions to and from RTS Toggle.
2472 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2473 ch->ch_mostat &= ~(UART_MCR_RTS);
2474 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2475 ch->ch_mostat |= (UART_MCR_RTS);
2478 * Handle transistions to and from DTR Toggle.
2480 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2481 ch->ch_mostat &= ~(UART_MCR_DTR);
2482 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2483 ch->ch_mostat |= (UART_MCR_DTR);
2485 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2487 if (ch->ch_digi.digi_maxcps < 1)
2488 ch->ch_digi.digi_maxcps = 1;
2490 if (ch->ch_digi.digi_maxcps > 10000)
2491 ch->ch_digi.digi_maxcps = 10000;
2493 if (ch->ch_digi.digi_bufsize < 10)
2494 ch->ch_digi.digi_bufsize = 10;
2496 if (ch->ch_digi.digi_maxchar < 1)
2497 ch->ch_digi.digi_maxchar = 1;
2499 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2500 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2502 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2503 ch->ch_digi.digi_onlen = DIGI_PLEN;
2505 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2506 ch->ch_digi.digi_offlen = DIGI_PLEN;
2508 ch->ch_bd->bd_ops->param(tty);
2510 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2517 * dgnc_set_termios()
2519 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2521 struct dgnc_board *bd;
2522 struct channel_t *ch;
2524 unsigned long lock_flags;
2526 if (!tty || tty->magic != TTY_MAGIC)
2529 un = tty->driver_data;
2530 if (!un || un->magic != DGNC_UNIT_MAGIC)
2534 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2538 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2541 DGNC_LOCK(ch->ch_lock, lock_flags);
2543 ch->ch_c_cflag = tty->termios.c_cflag;
2544 ch->ch_c_iflag = tty->termios.c_iflag;
2545 ch->ch_c_oflag = tty->termios.c_oflag;
2546 ch->ch_c_lflag = tty->termios.c_lflag;
2547 ch->ch_startc = tty->termios.c_cc[VSTART];
2548 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2550 ch->ch_bd->bd_ops->param(tty);
2553 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2557 static void dgnc_tty_throttle(struct tty_struct *tty)
2559 struct channel_t *ch;
2561 ulong lock_flags = 0;
2563 if (!tty || tty->magic != TTY_MAGIC)
2566 un = tty->driver_data;
2567 if (!un || un->magic != DGNC_UNIT_MAGIC)
2571 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2574 DGNC_LOCK(ch->ch_lock, lock_flags);
2576 ch->ch_flags |= (CH_FORCED_STOPI);
2578 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2582 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2584 struct channel_t *ch;
2588 if (!tty || tty->magic != TTY_MAGIC)
2591 un = tty->driver_data;
2592 if (!un || un->magic != DGNC_UNIT_MAGIC)
2596 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2599 DGNC_LOCK(ch->ch_lock, lock_flags);
2601 ch->ch_flags &= ~(CH_FORCED_STOPI);
2603 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2607 static void dgnc_tty_start(struct tty_struct *tty)
2609 struct dgnc_board *bd;
2610 struct channel_t *ch;
2614 if (!tty || tty->magic != TTY_MAGIC)
2617 un = tty->driver_data;
2618 if (!un || un->magic != DGNC_UNIT_MAGIC)
2622 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2626 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2629 DGNC_LOCK(ch->ch_lock, lock_flags);
2631 ch->ch_flags &= ~(CH_FORCED_STOP);
2633 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2637 static void dgnc_tty_stop(struct tty_struct *tty)
2639 struct dgnc_board *bd;
2640 struct channel_t *ch;
2644 if (!tty || tty->magic != TTY_MAGIC)
2647 un = tty->driver_data;
2648 if (!un || un->magic != DGNC_UNIT_MAGIC)
2652 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2656 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2659 DGNC_LOCK(ch->ch_lock, lock_flags);
2661 ch->ch_flags |= (CH_FORCED_STOP);
2663 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2668 * dgnc_tty_flush_chars()
2670 * Flush the cook buffer
2672 * Note to self, and any other poor souls who venture here:
2674 * flush in this case DOES NOT mean dispose of the data.
2675 * instead, it means "stop buffering and send it if you
2676 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2678 * It is also always called in interrupt context - JAR 8-Sept-99
2680 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2682 struct dgnc_board *bd;
2683 struct channel_t *ch;
2687 if (!tty || tty->magic != TTY_MAGIC)
2690 un = tty->driver_data;
2691 if (!un || un->magic != DGNC_UNIT_MAGIC)
2695 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2699 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2702 DGNC_LOCK(ch->ch_lock, lock_flags);
2704 /* Do something maybe here */
2706 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2712 * dgnc_tty_flush_buffer()
2714 * Flush Tx buffer (make in == out)
2716 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2718 struct channel_t *ch;
2722 if (!tty || tty->magic != TTY_MAGIC)
2725 un = tty->driver_data;
2726 if (!un || un->magic != DGNC_UNIT_MAGIC)
2730 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2733 DGNC_LOCK(ch->ch_lock, lock_flags);
2735 ch->ch_flags &= ~CH_STOP;
2737 /* Flush our write queue */
2738 ch->ch_w_head = ch->ch_w_tail;
2740 /* Flush UARTs transmit FIFO */
2741 ch->ch_bd->bd_ops->flush_uart_write(ch);
2743 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2744 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2745 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2747 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2748 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2749 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2752 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2757 /*****************************************************************************
2759 * The IOCTL function and all of its helpers
2761 *****************************************************************************/
2766 * The usual assortment of ioctl's
2768 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2771 struct dgnc_board *bd;
2772 struct channel_t *ch;
2776 void __user *uarg = (void __user *) arg;
2778 if (!tty || tty->magic != TTY_MAGIC)
2781 un = tty->driver_data;
2782 if (!un || un->magic != DGNC_UNIT_MAGIC)
2786 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2790 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2793 DGNC_LOCK(ch->ch_lock, lock_flags);
2795 if (un->un_open_count <= 0) {
2796 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2802 /* Here are all the standard ioctl's that we MUST implement */
2806 * TCSBRK is SVID version: non-zero arg --> no break
2807 * this behaviour is exploited by tcdrain().
2809 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2810 * between 0.25 and 0.5 seconds so we'll ask for something
2811 * in the middle: 0.375 seconds.
2813 rc = tty_check_change(tty);
2814 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2818 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2823 DGNC_LOCK(ch->ch_lock, lock_flags);
2825 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP)) {
2826 ch->ch_bd->bd_ops->send_break(ch, 250);
2829 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2835 /* support for POSIX tcsendbreak()
2836 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2837 * between 0.25 and 0.5 seconds so we'll ask for something
2838 * in the middle: 0.375 seconds.
2840 rc = tty_check_change(tty);
2841 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2845 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2849 DGNC_LOCK(ch->ch_lock, lock_flags);
2851 ch->ch_bd->bd_ops->send_break(ch, 250);
2853 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2858 rc = tty_check_change(tty);
2859 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2863 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2867 DGNC_LOCK(ch->ch_lock, lock_flags);
2869 ch->ch_bd->bd_ops->send_break(ch, 250);
2871 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2877 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2882 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2884 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
2889 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2890 rc = get_user(arg, (unsigned long __user *) arg);
2894 DGNC_LOCK(ch->ch_lock, lock_flags);
2895 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
2896 ch->ch_bd->bd_ops->param(tty);
2897 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2902 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2903 return dgnc_get_modem_info(ch, uarg);
2908 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2909 return dgnc_set_modem_info(tty, cmd, uarg);
2912 * Here are any additional ioctl's that we want to implement
2917 * The linux tty driver doesn't have a flush
2918 * input routine for the driver, assuming all backed
2919 * up data is in the line disc. buffers. However,
2920 * we all know that's not the case. Here, we
2921 * act on the ioctl, but then lie and say we didn't
2922 * so the line discipline will process the flush
2925 rc = tty_check_change(tty);
2927 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2931 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2932 ch->ch_r_head = ch->ch_r_tail;
2933 ch->ch_bd->bd_ops->flush_uart_read(ch);
2934 /* Force queue flow control to be released, if needed */
2935 dgnc_check_queue_flow_control(ch);
2938 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2939 if (!(un->un_type == DGNC_PRINT)) {
2940 ch->ch_w_head = ch->ch_w_tail;
2941 ch->ch_bd->bd_ops->flush_uart_write(ch);
2943 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2944 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2945 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2948 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2949 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2950 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2956 /* pretend we didn't recognize this IOCTL */
2957 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2958 return -ENOIOCTLCMD;
2962 * The linux tty driver doesn't have a flush
2963 * input routine for the driver, assuming all backed
2964 * up data is in the line disc. buffers. However,
2965 * we all know that's not the case. Here, we
2966 * act on the ioctl, but then lie and say we didn't
2967 * so the line discipline will process the flush
2970 if (cmd == TCSETSF) {
2972 ch->ch_flags &= ~CH_STOP;
2973 ch->ch_r_head = ch->ch_r_tail;
2974 ch->ch_bd->bd_ops->flush_uart_read(ch);
2975 /* Force queue flow control to be released, if needed */
2976 dgnc_check_queue_flow_control(ch);
2979 /* now wait for all the output to drain */
2980 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2981 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2985 /* pretend we didn't recognize this */
2986 return -ENOIOCTLCMD;
2990 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2991 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2995 /* pretend we didn't recognize this */
2996 return -ENOIOCTLCMD;
2999 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3000 /* Make the ld do it */
3001 return -ENOIOCTLCMD;
3004 /* get information for ditty */
3005 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3006 return dgnc_tty_digigeta(tty, uarg);
3011 /* set information for ditty */
3012 if (cmd == (DIGI_SETAW)) {
3014 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3015 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3020 DGNC_LOCK(ch->ch_lock, lock_flags);
3022 tty_ldisc_flush(tty);
3027 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3028 return dgnc_tty_digiseta(tty, uarg);
3033 /* Let go of locks when accessing user space, could sleep */
3034 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3035 rc = get_user(loopback, (unsigned int __user *) arg);
3038 DGNC_LOCK(ch->ch_lock, lock_flags);
3040 /* Enable/disable internal loopback for this port */
3042 ch->ch_flags |= CH_LOOPBACK;
3044 ch->ch_flags &= ~(CH_LOOPBACK);
3046 ch->ch_bd->bd_ops->param(tty);
3047 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3051 case DIGI_GETCUSTOMBAUD:
3052 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3053 rc = put_user(ch->ch_custom_speed, (unsigned int __user *) arg);
3056 case DIGI_SETCUSTOMBAUD:
3059 /* Let go of locks when accessing user space, could sleep */
3060 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3061 rc = get_user(new_rate, (int __user *) arg);
3064 DGNC_LOCK(ch->ch_lock, lock_flags);
3065 dgnc_set_custom_speed(ch, new_rate);
3066 ch->ch_bd->bd_ops->param(tty);
3067 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3072 * This ioctl allows insertion of a character into the front
3073 * of any pending data to be transmitted.
3075 * This ioctl is to satify the "Send Character Immediate"
3076 * call that the RealPort protocol spec requires.
3078 case DIGI_REALPORT_SENDIMMEDIATE:
3081 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3082 rc = get_user(c, (unsigned char __user *) arg);
3085 DGNC_LOCK(ch->ch_lock, lock_flags);
3086 ch->ch_bd->bd_ops->send_immediate_char(ch, c);
3087 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3092 * This ioctl returns all the current counts for the port.
3094 * This ioctl is to satify the "Line Error Counters"
3095 * call that the RealPort protocol spec requires.
3097 case DIGI_REALPORT_GETCOUNTERS:
3099 struct digi_getcounter buf;
3101 buf.norun = ch->ch_err_overrun;
3102 buf.noflow = 0; /* The driver doesn't keep this stat */
3103 buf.nframe = ch->ch_err_frame;
3104 buf.nparity = ch->ch_err_parity;
3105 buf.nbreak = ch->ch_err_break;
3106 buf.rbytes = ch->ch_rxcount;
3107 buf.tbytes = ch->ch_txcount;
3109 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3111 if (copy_to_user(uarg, &buf, sizeof(buf)))
3118 * This ioctl returns all current events.
3120 * This ioctl is to satify the "Event Reporting"
3121 * call that the RealPort protocol spec requires.
3123 case DIGI_REALPORT_GETEVENTS:
3125 unsigned int events = 0;
3127 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
3128 if (ch->ch_flags & CH_BREAK_SENDING)
3130 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP))
3131 events |= (EV_OPU | EV_OPS);
3133 if ((ch->ch_flags & CH_STOPI) || (ch->ch_flags & CH_FORCED_STOPI)) {
3134 events |= (EV_IPU | EV_IPS);
3137 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3138 rc = put_user(events, (unsigned int __user *) arg);
3143 * This ioctl returns TOUT and TIN counters based
3144 * upon the values passed in by the RealPort Server.
3145 * It also passes back whether the UART Transmitter is
3148 case DIGI_REALPORT_GETBUFFERS:
3150 struct digi_getbuffer buf;
3154 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3157 * Get data from user first.
3159 if (copy_from_user(&buf, uarg, sizeof(buf)))
3162 DGNC_LOCK(ch->ch_lock, lock_flags);
3165 * Figure out how much data is in our RX and TX queues.
3167 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
3168 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
3171 * Is the UART empty? Add that value to whats in our TX queue.
3173 count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
3176 * Figure out how much data the RealPort Server believes should
3177 * be in our TX queue.
3179 tdist = (buf.tIn - buf.tOut) & 0xffff;
3182 * If we have more data than the RealPort Server believes we
3183 * should have, reduce our count to its amount.
3185 * This count difference CAN happen because the Linux LD can
3186 * insert more characters into our queue for OPOST processing
3187 * that the RealPort Server doesn't know about.
3189 if (buf.txbuf > tdist)
3193 * Report whether our queue and UART TX are completely empty.
3200 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3202 if (copy_to_user(uarg, &buf, sizeof(buf)))
3208 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3210 return -ENOIOCTLCMD;