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.
16 /************************************************************************
18 * This file implements the tty driver functionality for the
19 * Neo and ClassicBoard PCI based product lines.
21 ************************************************************************
25 #include <linux/kernel.h>
26 #include <linux/sched.h> /* For jiffies, task states */
27 #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
28 #include <linux/module.h>
29 #include <linux/ctype.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/types.h>
33 #include <linux/serial_reg.h>
34 #include <linux/slab.h>
35 #include <linux/delay.h> /* For udelay */
36 #include <linux/uaccess.h> /* For copy_from_user/copy_to_user */
37 #include <linux/pci.h>
38 #include "dgnc_driver.h"
42 #include "dgnc_sysfs.h"
43 #include "dgnc_utils.h"
48 static struct dgnc_board *dgnc_BoardsByMajor[256];
49 static unsigned char *dgnc_TmpWriteBuf;
52 * Default transparent print information.
54 static struct digi_t dgnc_digi_init = {
55 .digi_flags = DIGI_COOK, /* Flags */
56 .digi_maxcps = 100, /* Max CPS */
57 .digi_maxchar = 50, /* Max chars in print queue */
58 .digi_bufsize = 100, /* Printer buffer size */
59 .digi_onlen = 4, /* size of printer on string */
60 .digi_offlen = 4, /* size of printer off string */
61 .digi_onstr = "\033[5i", /* ANSI printer on string ] */
62 .digi_offstr = "\033[4i", /* ANSI printer off string ] */
63 .digi_term = "ansi" /* default terminal type */
68 * Define a local default termios struct. All ports will be created
69 * with this termios initially.
71 * This defines a raw port at 9600 baud, 8 data bits, no parity,
74 static struct ktermios DgncDefaultTermios = {
75 .c_iflag = (DEFAULT_IFLAGS), /* iflags */
76 .c_oflag = (DEFAULT_OFLAGS), /* oflags */
77 .c_cflag = (DEFAULT_CFLAGS), /* cflags */
78 .c_lflag = (DEFAULT_LFLAGS), /* lflags */
84 /* Our function prototypes */
85 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
86 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
87 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file,
88 struct channel_t *ch);
89 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
91 static int dgnc_tty_digigeta(struct tty_struct *tty,
92 struct digi_t __user *retinfo);
93 static int dgnc_tty_digiseta(struct tty_struct *tty,
94 struct digi_t __user *new_info);
95 static int dgnc_tty_write_room(struct tty_struct *tty);
96 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
97 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
98 static void dgnc_tty_start(struct tty_struct *tty);
99 static void dgnc_tty_stop(struct tty_struct *tty);
100 static void dgnc_tty_throttle(struct tty_struct *tty);
101 static void dgnc_tty_unthrottle(struct tty_struct *tty);
102 static void dgnc_tty_flush_chars(struct tty_struct *tty);
103 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
104 static void dgnc_tty_hangup(struct tty_struct *tty);
105 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command,
106 unsigned int __user *value);
107 static int dgnc_get_modem_info(struct channel_t *ch,
108 unsigned int __user *value);
109 static int dgnc_tty_tiocmget(struct tty_struct *tty);
110 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set,
112 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
113 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
114 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf,
116 static void dgnc_tty_set_termios(struct tty_struct *tty,
117 struct ktermios *old_termios);
118 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
121 static const struct tty_operations dgnc_tty_ops = {
122 .open = dgnc_tty_open,
123 .close = dgnc_tty_close,
124 .write = dgnc_tty_write,
125 .write_room = dgnc_tty_write_room,
126 .flush_buffer = dgnc_tty_flush_buffer,
127 .chars_in_buffer = dgnc_tty_chars_in_buffer,
128 .flush_chars = dgnc_tty_flush_chars,
129 .ioctl = dgnc_tty_ioctl,
130 .set_termios = dgnc_tty_set_termios,
131 .stop = dgnc_tty_stop,
132 .start = dgnc_tty_start,
133 .throttle = dgnc_tty_throttle,
134 .unthrottle = dgnc_tty_unthrottle,
135 .hangup = dgnc_tty_hangup,
136 .put_char = dgnc_tty_put_char,
137 .tiocmget = dgnc_tty_tiocmget,
138 .tiocmset = dgnc_tty_tiocmset,
139 .break_ctl = dgnc_tty_send_break,
140 .wait_until_sent = dgnc_tty_wait_until_sent,
141 .send_xchar = dgnc_tty_send_xchar
144 /************************************************************************
146 * TTY Initialization/Cleanup Functions
148 ************************************************************************/
153 * Initialize any global tty related data before we download any boards.
155 int dgnc_tty_preinit(void)
158 * Allocate a buffer for doing the copy from user space to
159 * kernel space in dgnc_write(). We only use one buffer and
160 * control access to it with a semaphore. If we are paging, we
161 * are already in trouble so one buffer won't hurt much anyway.
163 * We are okay to sleep in the malloc, as this routine
164 * is only called during module load, (not in interrupt context),
165 * and with no locks held.
167 dgnc_TmpWriteBuf = kmalloc(WRITEBUFLEN, GFP_KERNEL);
169 if (!dgnc_TmpWriteBuf)
177 * dgnc_tty_register()
179 * Init the tty subsystem for this board.
181 int dgnc_tty_register(struct dgnc_board *brd)
185 brd->SerialDriver.magic = TTY_DRIVER_MAGIC;
187 snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgnc_%d_", brd->boardnum);
189 brd->SerialDriver.name = brd->SerialName;
190 brd->SerialDriver.name_base = 0;
191 brd->SerialDriver.major = 0;
192 brd->SerialDriver.minor_start = 0;
193 brd->SerialDriver.num = brd->maxports;
194 brd->SerialDriver.type = TTY_DRIVER_TYPE_SERIAL;
195 brd->SerialDriver.subtype = SERIAL_TYPE_NORMAL;
196 brd->SerialDriver.init_termios = DgncDefaultTermios;
197 brd->SerialDriver.driver_name = DRVSTR;
198 brd->SerialDriver.flags = (TTY_DRIVER_REAL_RAW |
199 TTY_DRIVER_DYNAMIC_DEV |
200 TTY_DRIVER_HARDWARE_BREAK);
203 * The kernel wants space to store pointers to
204 * tty_struct's and termios's.
206 brd->SerialDriver.ttys = kcalloc(brd->maxports,
207 sizeof(*brd->SerialDriver.ttys),
209 if (!brd->SerialDriver.ttys)
212 kref_init(&brd->SerialDriver.kref);
213 brd->SerialDriver.termios = kcalloc(brd->maxports,
214 sizeof(*brd->SerialDriver.termios),
216 if (!brd->SerialDriver.termios)
220 * Entry points for driver. Called by the kernel from
221 * tty_io.c and n_tty.c.
223 tty_set_operations(&brd->SerialDriver, &dgnc_tty_ops);
225 if (!brd->dgnc_Major_Serial_Registered) {
226 /* Register tty devices */
227 rc = tty_register_driver(&brd->SerialDriver);
229 dev_dbg(&brd->pdev->dev,
230 "Can't register tty device (%d)\n", rc);
233 brd->dgnc_Major_Serial_Registered = true;
237 * If we're doing transparent print, we have to do all of the above
238 * again, separately so we don't get the LD confused about what major
239 * we are when we get into the dgnc_tty_open() routine.
241 brd->PrintDriver.magic = TTY_DRIVER_MAGIC;
242 snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
244 brd->PrintDriver.name = brd->PrintName;
245 brd->PrintDriver.name_base = 0;
246 brd->PrintDriver.major = brd->SerialDriver.major;
247 brd->PrintDriver.minor_start = 0x80;
248 brd->PrintDriver.num = brd->maxports;
249 brd->PrintDriver.type = TTY_DRIVER_TYPE_SERIAL;
250 brd->PrintDriver.subtype = SERIAL_TYPE_NORMAL;
251 brd->PrintDriver.init_termios = DgncDefaultTermios;
252 brd->PrintDriver.driver_name = DRVSTR;
253 brd->PrintDriver.flags = (TTY_DRIVER_REAL_RAW |
254 TTY_DRIVER_DYNAMIC_DEV |
255 TTY_DRIVER_HARDWARE_BREAK);
258 * The kernel wants space to store pointers to
259 * tty_struct's and termios's. Must be separated from
260 * the Serial Driver so we don't get confused
262 brd->PrintDriver.ttys = kcalloc(brd->maxports,
263 sizeof(*brd->PrintDriver.ttys),
265 if (!brd->PrintDriver.ttys)
267 kref_init(&brd->PrintDriver.kref);
268 brd->PrintDriver.termios = kcalloc(brd->maxports,
269 sizeof(*brd->PrintDriver.termios),
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 dev_dbg(&brd->pdev->dev,
285 "Can't register Transparent Print device(%d)\n",
289 brd->dgnc_Major_TransparentPrint_Registered = true;
292 dgnc_BoardsByMajor[brd->SerialDriver.major] = brd;
293 brd->dgnc_Serial_Major = brd->SerialDriver.major;
294 brd->dgnc_TransparentPrint_Major = brd->PrintDriver.major;
303 * Init the tty subsystem. Called once per board after board has been
304 * downloaded and init'ed.
306 int dgnc_tty_init(struct dgnc_board *brd)
310 struct channel_t *ch;
316 * Initialize board structure elements.
319 vaddr = brd->re_map_membase;
321 brd->nasync = brd->maxports;
323 for (i = 0; i < brd->nasync; i++) {
325 * Okay to malloc with GFP_KERNEL, we are not at
326 * interrupt context, and there are no locks held.
328 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]),
330 if (!brd->channels[i])
331 goto err_free_channels;
334 ch = brd->channels[0];
335 vaddr = brd->re_map_membase;
337 /* Set up channel variables */
338 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
339 spin_lock_init(&ch->ch_lock);
341 /* Store all our magic numbers */
342 ch->magic = DGNC_CHANNEL_MAGIC;
343 ch->ch_tun.magic = DGNC_UNIT_MAGIC;
344 ch->ch_tun.un_ch = ch;
345 ch->ch_tun.un_type = DGNC_SERIAL;
346 ch->ch_tun.un_dev = i;
348 ch->ch_pun.magic = DGNC_UNIT_MAGIC;
349 ch->ch_pun.un_ch = ch;
350 ch->ch_pun.un_type = DGNC_PRINT;
351 ch->ch_pun.un_dev = i + 128;
353 if (brd->bd_uart_offset == 0x200)
354 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
356 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
360 ch->ch_digi = dgnc_digi_init;
362 /* .25 second delay */
363 ch->ch_close_delay = 250;
365 init_waitqueue_head(&ch->ch_flags_wait);
366 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
367 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
370 struct device *classp;
372 classp = tty_register_device(&brd->SerialDriver, i,
373 &(ch->ch_bd->pdev->dev));
374 ch->ch_tun.un_sysfs = classp;
375 dgnc_create_tty_sysfs(&ch->ch_tun, classp);
377 classp = tty_register_device(&brd->PrintDriver, i,
378 &(ch->ch_bd->pdev->dev));
379 ch->ch_pun.un_sysfs = classp;
380 dgnc_create_tty_sysfs(&ch->ch_pun, classp);
388 for (i = i - 1; i >= 0; --i) {
389 kfree(brd->channels[i]);
390 brd->channels[i] = NULL;
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 if (brd->channels[i])
423 dgnc_remove_tty_sysfs(brd->channels[i]->
425 tty_unregister_device(&brd->SerialDriver, i);
427 tty_unregister_driver(&brd->SerialDriver);
428 brd->dgnc_Major_Serial_Registered = false;
431 if (brd->dgnc_Major_TransparentPrint_Registered) {
432 dgnc_BoardsByMajor[brd->PrintDriver.major] = NULL;
433 brd->dgnc_TransparentPrint_Major = 0;
434 for (i = 0; i < brd->nasync; i++) {
435 if (brd->channels[i])
436 dgnc_remove_tty_sysfs(brd->channels[i]->
438 tty_unregister_device(&brd->PrintDriver, i);
440 tty_unregister_driver(&brd->PrintDriver);
441 brd->dgnc_Major_TransparentPrint_Registered = false;
444 kfree(brd->SerialDriver.ttys);
445 brd->SerialDriver.ttys = NULL;
446 kfree(brd->SerialDriver.termios);
447 brd->SerialDriver.termios = NULL;
448 kfree(brd->PrintDriver.ttys);
449 brd->PrintDriver.ttys = NULL;
450 kfree(brd->PrintDriver.termios);
451 brd->PrintDriver.termios = NULL;
454 /*=======================================================================
456 * dgnc_wmove - Write data to transmit queue.
458 * ch - Pointer to channel structure.
459 * buf - Poiter to characters to be moved.
460 * n - Number of characters to move.
462 *=======================================================================*/
463 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
468 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
471 head = ch->ch_w_head & WQUEUEMASK;
474 * If the write wraps over the top of the circular buffer,
475 * move the portion up to the wrap point, and reset the
476 * pointers to the bottom.
478 remain = WQUEUESIZE - head;
482 memcpy(ch->ch_wqueue + head, buf, remain);
492 memcpy(ch->ch_wqueue + head, buf, remain);
497 ch->ch_w_head = head;
503 /*=======================================================================
505 * dgnc_input - Process received data.
507 * ch - Pointer to channel structure.
509 *=======================================================================*/
510 void dgnc_input(struct channel_t *ch)
512 struct dgnc_board *bd;
513 struct tty_struct *tp;
514 struct tty_ldisc *ld = NULL;
526 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
529 tp = ch->ch_tun.un_tty;
532 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
535 spin_lock_irqsave(&ch->ch_lock, flags);
538 * Figure the number of characters in the buffer.
539 * Exit immediately if none.
542 head = ch->ch_r_head & rmask;
543 tail = ch->ch_r_tail & rmask;
544 data_len = (head - tail) & rmask;
550 * If the device is not open, or CREAD is off,
551 * flush input data and return immediately.
553 if (!tp || (tp->magic != TTY_MAGIC) ||
554 !(ch->ch_tun.un_flags & UN_ISOPEN) ||
555 !(tp->termios.c_cflag & CREAD) ||
556 (ch->ch_tun.un_flags & UN_CLOSING)) {
558 ch->ch_r_head = tail;
560 /* Force queue flow control to be released, if needed */
561 dgnc_check_queue_flow_control(ch);
567 * If we are throttled, simply don't read any data.
569 if (ch->ch_flags & CH_FORCED_STOPI)
572 flip_len = TTY_FLIPBUF_SIZE;
574 /* Chop down the length, if needed */
575 len = min(data_len, flip_len);
576 len = min(len, (N_TTY_BUF_SIZE - 1));
578 ld = tty_ldisc_ref(tp);
581 * If we were unable to get a reference to the ld,
582 * don't flush our buffer, and act like the ld doesn't
583 * have any space to put the data right now.
589 * If ld doesn't have a pointer to a receive_buf function,
590 * flush the data, then act like the ld doesn't have any
591 * space to put the data right now.
593 if (!ld->ops->receive_buf) {
594 ch->ch_r_head = ch->ch_r_tail;
603 * The tty layer in the kernel has changed in 2.6.16+.
605 * The flip buffers in the tty structure are no longer exposed,
606 * and probably will be going away eventually.
608 * If we are completely raw, we don't need to go through a lot
609 * of the tty layers that exist.
610 * In this case, we take the shortest and fastest route we
611 * can to relay the data to the user.
613 * On the other hand, if we are not raw, we need to go through
614 * the new 2.6.16+ tty layer, which has its API more well defined.
616 len = tty_buffer_request_room(tp->port, len);
620 * n now contains the most amount of data we can copy,
621 * bounded either by how much the Linux tty layer can handle,
622 * or the amount of data the card actually has pending...
625 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
632 * If conditions are such that ld needs to see all
633 * UART errors, we will have to walk each character
634 * and error byte and send them to the buffer one at
637 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
638 for (i = 0; i < s; i++) {
639 if (*(ch->ch_equeue + tail + i) & UART_LSR_BI)
640 tty_insert_flip_char(tp->port,
641 *(ch->ch_rqueue + tail + i),
643 else if (*(ch->ch_equeue + tail + i) &
645 tty_insert_flip_char(tp->port,
646 *(ch->ch_rqueue + tail + i),
648 else if (*(ch->ch_equeue + tail + i) &
650 tty_insert_flip_char(tp->port,
651 *(ch->ch_rqueue + tail + i),
654 tty_insert_flip_char(tp->port,
655 *(ch->ch_rqueue + tail + i),
659 tty_insert_flip_string(tp->port,
660 ch->ch_rqueue + tail,
666 /* Flip queue if needed */
670 ch->ch_r_tail = tail & rmask;
671 ch->ch_e_tail = tail & rmask;
672 dgnc_check_queue_flow_control(ch);
673 spin_unlock_irqrestore(&ch->ch_lock, flags);
675 /* Tell the tty layer its okay to "eat" the data now */
676 tty_flip_buffer_push(tp->port);
683 spin_unlock_irqrestore(&ch->ch_lock, flags);
689 /************************************************************************
690 * Determines when CARRIER changes state and takes appropriate
692 ************************************************************************/
693 void dgnc_carrier(struct channel_t *ch)
695 struct dgnc_board *bd;
697 int virt_carrier = 0;
698 int phys_carrier = 0;
700 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
705 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
708 if (ch->ch_mistat & UART_MSR_DCD)
711 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
714 if (ch->ch_c_cflag & CLOCAL)
718 * Test for a VIRTUAL carrier transition to HIGH.
720 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
723 * When carrier rises, wake any threads waiting
724 * for carrier in the open routine.
727 if (waitqueue_active(&(ch->ch_flags_wait)))
728 wake_up_interruptible(&ch->ch_flags_wait);
732 * Test for a PHYSICAL carrier transition to HIGH.
734 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
737 * When carrier rises, wake any threads waiting
738 * for carrier in the open routine.
741 if (waitqueue_active(&(ch->ch_flags_wait)))
742 wake_up_interruptible(&ch->ch_flags_wait);
746 * Test for a PHYSICAL transition to low, so long as we aren't
747 * currently ignoring physical transitions (which is what "virtual
748 * carrier" indicates).
750 * The transition of the virtual carrier to low really doesn't
751 * matter... it really only means "ignore carrier state", not
752 * "make pretend that carrier is there".
754 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
755 (phys_carrier == 0)) {
758 * When carrier drops:
760 * Drop carrier on all open units.
762 * Flush queues, waking up any task waiting in the
765 * Send a hangup to the control terminal.
767 * Enable all select calls.
769 if (waitqueue_active(&(ch->ch_flags_wait)))
770 wake_up_interruptible(&ch->ch_flags_wait);
772 if (ch->ch_tun.un_open_count > 0)
773 tty_hangup(ch->ch_tun.un_tty);
775 if (ch->ch_pun.un_open_count > 0)
776 tty_hangup(ch->ch_pun.un_tty);
780 * Make sure that our cached values reflect the current reality.
782 if (virt_carrier == 1)
783 ch->ch_flags |= CH_FCAR;
785 ch->ch_flags &= ~CH_FCAR;
787 if (phys_carrier == 1)
788 ch->ch_flags |= CH_CD;
790 ch->ch_flags &= ~CH_CD;
794 * Assign the custom baud rate to the channel structure
796 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
805 ch->ch_custom_speed = 0;
810 * Since the divisor is stored in a 16-bit integer, we make sure
811 * we don't allow any rates smaller than a 16-bit integer would allow.
812 * And of course, rates above the dividend won't fly.
814 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
815 newrate = ((ch->ch_bd->bd_dividend / 0xFFFF) + 1);
817 if (newrate && newrate > ch->ch_bd->bd_dividend)
818 newrate = ch->ch_bd->bd_dividend;
821 testdiv = ch->ch_bd->bd_dividend / newrate;
824 * If we try to figure out what rate the board would use
825 * with the test divisor, it will be either equal or higher
826 * than the requested baud rate. If we then determine the
827 * rate with a divisor one higher, we will get the next lower
828 * supported rate below the requested.
830 testrate_high = ch->ch_bd->bd_dividend / testdiv;
831 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
834 * If the rate for the requested divisor is correct, just
835 * use it and be done.
837 if (testrate_high != newrate) {
839 * Otherwise, pick the rate that is closer
840 * (i.e. whichever rate has a smaller delta).
842 deltahigh = testrate_high - newrate;
843 deltalow = newrate - testrate_low;
845 if (deltahigh < deltalow)
846 newrate = testrate_high;
848 newrate = testrate_low;
852 ch->ch_custom_speed = newrate;
856 void dgnc_check_queue_flow_control(struct channel_t *ch)
860 /* Store how much space we have left in the queue */
861 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
863 qleft += RQUEUEMASK + 1;
866 * Check to see if we should enforce flow control on our queue because
867 * the ld (or user) isn't reading data out of our queue fast enuf.
869 * NOTE: This is done based on what the current flow control of the
872 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
873 * This will cause the UART's FIFO to back up, and force
874 * the RTS signal to be dropped.
875 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
876 * the other side, in hopes it will stop sending data to us.
877 * 3) NONE - Nothing we can do. We will simply drop any extra data
878 * that gets sent into us when the queue fills up.
882 if (ch->ch_digi.digi_flags & CTSPACE ||
883 ch->ch_c_cflag & CRTSCTS) {
884 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
885 ch->ch_bd->bd_ops->disable_receiver(ch);
886 ch->ch_flags |= (CH_RECEIVER_OFF);
890 else if (ch->ch_c_iflag & IXOFF) {
891 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
892 ch->ch_bd->bd_ops->send_stop_character(ch);
899 * Check to see if we should unenforce flow control because
900 * ld (or user) finally read enuf data out of our queue.
902 * NOTE: This is done based on what the current flow control of the
905 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
906 * This will cause the UART's FIFO to raise RTS back up,
907 * which will allow the other side to start sending data again.
908 * 2) SWFLOW (IXOFF) - Send a start character to
909 * the other side, so it will start sending data to us again.
910 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
911 * other side, we don't need to do anything now.
913 if (qleft > (RQUEUESIZE / 2)) {
915 if (ch->ch_digi.digi_flags & RTSPACE ||
916 ch->ch_c_cflag & CRTSCTS) {
917 if (ch->ch_flags & CH_RECEIVER_OFF) {
918 ch->ch_bd->bd_ops->enable_receiver(ch);
919 ch->ch_flags &= ~(CH_RECEIVER_OFF);
923 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
924 ch->ch_stops_sent = 0;
925 ch->ch_bd->bd_ops->send_start_character(ch);
931 void dgnc_wakeup_writes(struct channel_t *ch)
936 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
939 spin_lock_irqsave(&ch->ch_lock, flags);
942 * If channel now has space, wake up anyone waiting on the condition.
944 qlen = ch->ch_w_head - ch->ch_w_tail;
948 if (qlen >= (WQUEUESIZE - 256)) {
949 spin_unlock_irqrestore(&ch->ch_lock, flags);
953 if (ch->ch_tun.un_flags & UN_ISOPEN) {
954 if ((ch->ch_tun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
955 ch->ch_tun.un_tty->ldisc->ops->write_wakeup) {
956 spin_unlock_irqrestore(&ch->ch_lock, flags);
957 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
958 spin_lock_irqsave(&ch->ch_lock, flags);
961 wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
964 * If unit is set to wait until empty, check to make sure
965 * the queue AND FIFO are both empty.
967 if (ch->ch_tun.un_flags & UN_EMPTY) {
969 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
970 ch->ch_tun.un_flags &= ~(UN_EMPTY);
973 * If RTS Toggle mode is on, whenever
974 * the queue and UART is empty, keep RTS low.
976 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
977 ch->ch_mostat &= ~(UART_MCR_RTS);
978 ch->ch_bd->bd_ops->assert_modem_signals(ch);
982 * If DTR Toggle mode is on, whenever
983 * the queue and UART is empty, keep DTR low.
985 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
986 ch->ch_mostat &= ~(UART_MCR_DTR);
987 ch->ch_bd->bd_ops->assert_modem_signals(ch);
992 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
995 if (ch->ch_pun.un_flags & UN_ISOPEN) {
996 if ((ch->ch_pun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
997 ch->ch_pun.un_tty->ldisc->ops->write_wakeup) {
998 spin_unlock_irqrestore(&ch->ch_lock, flags);
999 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
1000 spin_lock_irqsave(&ch->ch_lock, flags);
1003 wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
1006 * If unit is set to wait until empty, check to make sure
1007 * the queue AND FIFO are both empty.
1009 if (ch->ch_pun.un_flags & UN_EMPTY) {
1011 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
1012 ch->ch_pun.un_flags &= ~(UN_EMPTY);
1015 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1018 spin_unlock_irqrestore(&ch->ch_lock, flags);
1023 /************************************************************************
1025 * TTY Entry points and helper functions
1027 ************************************************************************/
1033 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
1035 struct dgnc_board *brd;
1036 struct channel_t *ch;
1041 unsigned long flags;
1045 major = MAJOR(tty_devnum(tty));
1046 minor = MINOR(tty_devnum(tty));
1051 /* Get board pointer from our array of majors we have allocated */
1052 brd = dgnc_BoardsByMajor[major];
1057 * If board is not yet up to a state of READY, go to
1058 * sleep waiting for it to happen or they cancel the open.
1060 rc = wait_event_interruptible(brd->state_wait,
1061 (brd->state & BOARD_READY));
1066 spin_lock_irqsave(&brd->bd_lock, flags);
1068 /* If opened device is greater than our number of ports, bail. */
1069 if (PORT_NUM(minor) >= brd->nasync) {
1070 spin_unlock_irqrestore(&brd->bd_lock, flags);
1074 ch = brd->channels[PORT_NUM(minor)];
1076 spin_unlock_irqrestore(&brd->bd_lock, flags);
1080 /* Drop board lock */
1081 spin_unlock_irqrestore(&brd->bd_lock, flags);
1083 /* Grab channel lock */
1084 spin_lock_irqsave(&ch->ch_lock, flags);
1086 /* Figure out our type */
1087 if (!IS_PRINT(minor)) {
1088 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1089 un->un_type = DGNC_SERIAL;
1090 } else if (IS_PRINT(minor)) {
1091 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1092 un->un_type = DGNC_PRINT;
1094 spin_unlock_irqrestore(&ch->ch_lock, flags);
1099 * If the port is still in a previous open, and in a state
1100 * where we simply cannot safely keep going, wait until the
1103 spin_unlock_irqrestore(&ch->ch_lock, flags);
1105 rc = wait_event_interruptible(ch->ch_flags_wait,
1106 ((ch->ch_flags & CH_OPENING) == 0));
1108 /* If ret is non-zero, user ctrl-c'ed us */
1113 * If either unit is in the middle of the fragile part of close,
1114 * we just cannot touch the channel safely.
1115 * Go to sleep, knowing that when the channel can be
1116 * touched safely, the close routine will signal the
1117 * ch_flags_wait to wake us back up.
1119 rc = wait_event_interruptible(ch->ch_flags_wait,
1120 (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) &
1123 /* If ret is non-zero, user ctrl-c'ed us */
1127 spin_lock_irqsave(&ch->ch_lock, flags);
1130 /* Store our unit into driver_data, so we always have it available. */
1131 tty->driver_data = un;
1137 if (!(un->un_flags & UN_ISOPEN)) {
1138 /* Store important variables. */
1141 /* Maybe do something here to the TTY struct as well? */
1146 * Allocate channel buffers for read/write/error.
1147 * Set flag, so we don't get trounced on.
1149 ch->ch_flags |= (CH_OPENING);
1151 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1152 spin_unlock_irqrestore(&ch->ch_lock, flags);
1155 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1157 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1159 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1161 spin_lock_irqsave(&ch->ch_lock, flags);
1163 ch->ch_flags &= ~(CH_OPENING);
1164 wake_up_interruptible(&ch->ch_flags_wait);
1167 * Initialize if neither terminal or printer is open.
1169 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1172 * Flush input queues.
1181 brd->bd_ops->flush_uart_write(ch);
1182 brd->bd_ops->flush_uart_read(ch);
1185 ch->ch_cached_lsr = 0;
1186 ch->ch_stop_sending_break = 0;
1187 ch->ch_stops_sent = 0;
1189 ch->ch_c_cflag = tty->termios.c_cflag;
1190 ch->ch_c_iflag = tty->termios.c_iflag;
1191 ch->ch_c_oflag = tty->termios.c_oflag;
1192 ch->ch_c_lflag = tty->termios.c_lflag;
1193 ch->ch_startc = tty->termios.c_cc[VSTART];
1194 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1197 * Bring up RTS and DTR...
1198 * Also handle RTS or DTR toggle if set.
1200 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1201 ch->ch_mostat |= (UART_MCR_RTS);
1202 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1203 ch->ch_mostat |= (UART_MCR_DTR);
1205 /* Tell UART to init itself */
1206 brd->bd_ops->uart_init(ch);
1210 * Run param in case we changed anything
1212 brd->bd_ops->param(tty);
1217 * follow protocol for opening port
1220 spin_unlock_irqrestore(&ch->ch_lock, flags);
1222 rc = dgnc_block_til_ready(tty, file, ch);
1224 /* No going back now, increment our unit and channel counters */
1225 spin_lock_irqsave(&ch->ch_lock, flags);
1226 ch->ch_open_count++;
1227 un->un_open_count++;
1228 un->un_flags |= (UN_ISOPEN);
1229 spin_unlock_irqrestore(&ch->ch_lock, flags);
1236 * dgnc_block_til_ready()
1238 * Wait for DCD, if needed.
1240 static int dgnc_block_til_ready(struct tty_struct *tty,
1242 struct channel_t *ch)
1245 struct un_t *un = NULL;
1246 unsigned long flags;
1248 int sleep_on_un_flags = 0;
1250 if (!tty || tty->magic != TTY_MAGIC || !file || !ch ||
1251 ch->magic != DGNC_CHANNEL_MAGIC)
1254 un = tty->driver_data;
1255 if (!un || un->magic != DGNC_UNIT_MAGIC)
1258 spin_lock_irqsave(&ch->ch_lock, flags);
1265 sleep_on_un_flags = 0;
1268 * If board has failed somehow during our sleep,
1271 if (ch->ch_bd->state == BOARD_FAILED) {
1276 /* If tty was hung up, break out of loop and set error. */
1277 if (tty_hung_up_p(file)) {
1283 * If either unit is in the middle of the fragile part of close,
1284 * we just cannot touch the channel safely.
1285 * Go back to sleep, knowing that when the channel can be
1286 * touched safely, the close routine will signal the
1287 * ch_wait_flags to wake us back up.
1289 if (!((ch->ch_tun.un_flags |
1290 ch->ch_pun.un_flags) &
1294 * Our conditions to leave cleanly and happily:
1295 * 1) NONBLOCKING on the tty is set.
1297 * 3) DCD (fake or real) is active.
1300 if (file->f_flags & O_NONBLOCK)
1303 if (tty->flags & (1 << TTY_IO_ERROR)) {
1308 if (ch->ch_flags & CH_CD)
1311 if (ch->ch_flags & CH_FCAR)
1314 sleep_on_un_flags = 1;
1318 * If there is a signal pending, the user probably
1319 * interrupted (ctrl-c) us.
1320 * Leave loop with error set.
1322 if (signal_pending(current)) {
1323 retval = -ERESTARTSYS;
1328 * Store the flags before we let go of channel lock
1330 if (sleep_on_un_flags)
1331 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1333 old_flags = ch->ch_flags;
1336 * Let go of channel lock before calling schedule.
1337 * Our poller will get any FEP events and wake us up when DCD
1338 * eventually goes active.
1341 spin_unlock_irqrestore(&ch->ch_lock, flags);
1344 * Wait for something in the flags to change
1345 * from the current value.
1347 if (sleep_on_un_flags)
1348 retval = wait_event_interruptible(un->un_flags_wait,
1349 (old_flags != (ch->ch_tun.un_flags |
1350 ch->ch_pun.un_flags)));
1352 retval = wait_event_interruptible(ch->ch_flags_wait,
1353 (old_flags != ch->ch_flags));
1356 * We got woken up for some reason.
1357 * Before looping around, grab our channel lock.
1359 spin_lock_irqsave(&ch->ch_lock, flags);
1364 spin_unlock_irqrestore(&ch->ch_lock, flags);
1376 * Hangup the port. Like a close, but don't wait for output to drain.
1378 static void dgnc_tty_hangup(struct tty_struct *tty)
1382 if (!tty || tty->magic != TTY_MAGIC)
1385 un = tty->driver_data;
1386 if (!un || un->magic != DGNC_UNIT_MAGIC)
1389 /* flush the transmit queues */
1390 dgnc_tty_flush_buffer(tty);
1399 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1401 struct ktermios *ts;
1402 struct dgnc_board *bd;
1403 struct channel_t *ch;
1405 unsigned long flags;
1408 if (!tty || tty->magic != TTY_MAGIC)
1411 un = tty->driver_data;
1412 if (!un || un->magic != DGNC_UNIT_MAGIC)
1416 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1420 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1425 spin_lock_irqsave(&ch->ch_lock, flags);
1428 * Determine if this is the last close or not - and if we agree about
1429 * which type of close it is with the Line Discipline
1431 if ((tty->count == 1) && (un->un_open_count != 1)) {
1433 * Uh, oh. tty->count is 1, which means that the tty
1434 * structure will be freed. un_open_count should always
1435 * be one in these conditions. If it's greater than
1436 * one, we've got real problems, since it means the
1437 * serial port won't be shutdown.
1440 "tty->count is 1, un open count is %d\n",
1442 un->un_open_count = 1;
1445 if (un->un_open_count)
1446 un->un_open_count--;
1449 "bad serial port open count of %d\n",
1452 ch->ch_open_count--;
1454 if (ch->ch_open_count && un->un_open_count) {
1455 spin_unlock_irqrestore(&ch->ch_lock, flags);
1459 /* OK, its the last close on the unit */
1460 un->un_flags |= UN_CLOSING;
1466 * Only officially close channel if count is 0 and
1467 * DIGI_PRINTER bit is not set.
1469 if ((ch->ch_open_count == 0) &&
1470 !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1472 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1475 * turn off print device when closing print device.
1477 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1478 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1479 (int) ch->ch_digi.digi_offlen);
1480 ch->ch_flags &= ~CH_PRON;
1483 spin_unlock_irqrestore(&ch->ch_lock, flags);
1484 /* wait for output to drain */
1485 /* This will also return if we take an interrupt */
1487 rc = bd->bd_ops->drain(tty, 0);
1489 dgnc_tty_flush_buffer(tty);
1490 tty_ldisc_flush(tty);
1492 spin_lock_irqsave(&ch->ch_lock, flags);
1497 * If we have HUPCL set, lower DTR and RTS
1499 if (ch->ch_c_cflag & HUPCL) {
1502 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1503 bd->bd_ops->assert_modem_signals(ch);
1506 * Go to sleep to ensure RTS/DTR
1507 * have been dropped for modems to see it.
1509 if (ch->ch_close_delay) {
1510 spin_unlock_irqrestore(&ch->ch_lock,
1512 dgnc_ms_sleep(ch->ch_close_delay);
1513 spin_lock_irqsave(&ch->ch_lock, flags);
1517 ch->ch_old_baud = 0;
1519 /* Turn off UART interrupts for this port */
1520 ch->ch_bd->bd_ops->uart_off(ch);
1523 * turn off print device when closing print device.
1525 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1526 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1527 (int) ch->ch_digi.digi_offlen);
1528 ch->ch_flags &= ~CH_PRON;
1533 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1535 wake_up_interruptible(&ch->ch_flags_wait);
1536 wake_up_interruptible(&un->un_flags_wait);
1538 spin_unlock_irqrestore(&ch->ch_lock, flags);
1543 * dgnc_tty_chars_in_buffer()
1545 * Return number of characters that have not been transmitted yet.
1547 * This routine is used by the line discipline to determine if there
1548 * is data waiting to be transmitted/drained/flushed or not.
1550 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1552 struct channel_t *ch = NULL;
1553 struct un_t *un = NULL;
1558 unsigned long flags;
1563 un = tty->driver_data;
1564 if (!un || un->magic != DGNC_UNIT_MAGIC)
1568 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1571 spin_lock_irqsave(&ch->ch_lock, flags);
1574 thead = ch->ch_w_head & tmask;
1575 ttail = ch->ch_w_tail & tmask;
1577 spin_unlock_irqrestore(&ch->ch_lock, flags);
1579 if (ttail == thead) {
1583 chars = thead - ttail;
1585 chars = thead - ttail + WQUEUESIZE;
1595 * Reduces bytes_available to the max number of characters
1596 * that can be sent currently given the maxcps value, and
1597 * returns the new bytes_available. This only affects printer
1600 static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1602 struct channel_t *ch = NULL;
1603 struct un_t *un = NULL;
1606 return bytes_available;
1608 un = tty->driver_data;
1609 if (!un || un->magic != DGNC_UNIT_MAGIC)
1610 return bytes_available;
1613 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1614 return bytes_available;
1617 * If its not the Transparent print device, return
1618 * the full data amount.
1620 if (un->un_type != DGNC_PRINT)
1621 return bytes_available;
1623 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1625 unsigned long current_time = jiffies;
1626 unsigned long buffer_time = current_time +
1627 (HZ * ch->ch_digi.digi_bufsize) /
1628 ch->ch_digi.digi_maxcps;
1630 if (ch->ch_cpstime < current_time) {
1631 /* buffer is empty */
1632 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1633 cps_limit = ch->ch_digi.digi_bufsize;
1634 } else if (ch->ch_cpstime < buffer_time) {
1635 /* still room in the buffer */
1636 cps_limit = ((buffer_time - ch->ch_cpstime) *
1637 ch->ch_digi.digi_maxcps) / HZ;
1639 /* no room in the buffer */
1643 bytes_available = min(cps_limit, bytes_available);
1646 return bytes_available;
1651 * dgnc_tty_write_room()
1653 * Return space available in Tx buffer
1655 static int dgnc_tty_write_room(struct tty_struct *tty)
1657 struct channel_t *ch = NULL;
1658 struct un_t *un = NULL;
1663 unsigned long flags;
1665 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1668 un = tty->driver_data;
1669 if (!un || un->magic != DGNC_UNIT_MAGIC)
1673 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1676 spin_lock_irqsave(&ch->ch_lock, flags);
1679 head = (ch->ch_w_head) & tmask;
1680 tail = (ch->ch_w_tail) & tmask;
1682 ret = tail - head - 1;
1686 /* Limit printer to maxcps */
1687 ret = dgnc_maxcps_room(tty, ret);
1690 * If we are printer device, leave space for
1691 * possibly both the on and off strings.
1693 if (un->un_type == DGNC_PRINT) {
1694 if (!(ch->ch_flags & CH_PRON))
1695 ret -= ch->ch_digi.digi_onlen;
1696 ret -= ch->ch_digi.digi_offlen;
1698 if (ch->ch_flags & CH_PRON)
1699 ret -= ch->ch_digi.digi_offlen;
1705 spin_unlock_irqrestore(&ch->ch_lock, flags);
1712 * dgnc_tty_put_char()
1714 * Put a character into ch->ch_buf
1716 * - used by the line discipline for OPOST processing
1718 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1721 * Simply call tty_write.
1723 dgnc_tty_write(tty, &c, 1);
1731 * Take data from the user or kernel and send it out to the FEP.
1732 * In here exists all the Transparent Print magic as well.
1734 static int dgnc_tty_write(struct tty_struct *tty,
1735 const unsigned char *buf, int count)
1737 struct channel_t *ch = NULL;
1738 struct un_t *un = NULL;
1739 int bufcount = 0, n = 0;
1741 unsigned long flags;
1747 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1750 un = tty->driver_data;
1751 if (!un || un->magic != DGNC_UNIT_MAGIC)
1755 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1762 * Store original amount of characters passed in.
1763 * This helps to figure out if we should ask the FEP
1764 * to send us an event when it has more space available.
1768 spin_lock_irqsave(&ch->ch_lock, flags);
1770 /* Get our space available for the channel from the board */
1772 head = (ch->ch_w_head) & tmask;
1773 tail = (ch->ch_w_tail) & tmask;
1775 bufcount = tail - head - 1;
1777 bufcount += WQUEUESIZE;
1780 * Limit printer output to maxcps overall, with bursts allowed
1781 * up to bufsize characters.
1783 bufcount = dgnc_maxcps_room(tty, bufcount);
1786 * Take minimum of what the user wants to send, and the
1787 * space available in the FEP buffer.
1789 count = min(count, bufcount);
1792 * Bail if no space left.
1798 * Output the printer ON string, if we are in terminal mode, but
1799 * need to be in printer mode.
1801 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1802 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1803 (int) ch->ch_digi.digi_onlen);
1804 head = (ch->ch_w_head) & tmask;
1805 ch->ch_flags |= CH_PRON;
1809 * On the other hand, output the printer OFF string, if we are
1810 * currently in printer mode, but need to output to the terminal.
1812 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1813 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1814 (int) ch->ch_digi.digi_offlen);
1815 head = (ch->ch_w_head) & tmask;
1816 ch->ch_flags &= ~CH_PRON;
1822 * If the write wraps over the top of the circular buffer,
1823 * move the portion up to the wrap point, and reset the
1824 * pointers to the bottom.
1826 remain = WQUEUESIZE - head;
1830 memcpy(ch->ch_wqueue + head, buf, remain);
1837 * Move rest of data.
1840 memcpy(ch->ch_wqueue + head, buf, remain);
1846 ch->ch_w_head = head;
1849 /* Update printer buffer empty time. */
1850 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
1851 && (ch->ch_digi.digi_bufsize > 0)) {
1852 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1855 spin_unlock_irqrestore(&ch->ch_lock, flags);
1859 * Channel lock is grabbed and then released
1860 * inside this routine.
1862 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1869 spin_unlock_irqrestore(&ch->ch_lock, flags);
1875 * Return modem signals to ld.
1878 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1880 struct channel_t *ch;
1883 unsigned char mstat = 0;
1884 unsigned long flags;
1886 if (!tty || tty->magic != TTY_MAGIC)
1889 un = tty->driver_data;
1890 if (!un || un->magic != DGNC_UNIT_MAGIC)
1894 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1897 spin_lock_irqsave(&ch->ch_lock, flags);
1899 mstat = (ch->ch_mostat | ch->ch_mistat);
1901 spin_unlock_irqrestore(&ch->ch_lock, flags);
1905 if (mstat & UART_MCR_DTR)
1906 result |= TIOCM_DTR;
1907 if (mstat & UART_MCR_RTS)
1908 result |= TIOCM_RTS;
1909 if (mstat & UART_MSR_CTS)
1910 result |= TIOCM_CTS;
1911 if (mstat & UART_MSR_DSR)
1912 result |= TIOCM_DSR;
1913 if (mstat & UART_MSR_RI)
1915 if (mstat & UART_MSR_DCD)
1923 * dgnc_tty_tiocmset()
1925 * Set modem signals, called by ld.
1928 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1929 unsigned int set, unsigned int clear)
1931 struct dgnc_board *bd;
1932 struct channel_t *ch;
1935 unsigned long flags;
1937 if (!tty || tty->magic != TTY_MAGIC)
1940 un = tty->driver_data;
1941 if (!un || un->magic != DGNC_UNIT_MAGIC)
1945 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1949 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1952 spin_lock_irqsave(&ch->ch_lock, flags);
1954 if (set & TIOCM_RTS)
1955 ch->ch_mostat |= UART_MCR_RTS;
1957 if (set & TIOCM_DTR)
1958 ch->ch_mostat |= UART_MCR_DTR;
1960 if (clear & TIOCM_RTS)
1961 ch->ch_mostat &= ~(UART_MCR_RTS);
1963 if (clear & TIOCM_DTR)
1964 ch->ch_mostat &= ~(UART_MCR_DTR);
1966 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1968 spin_unlock_irqrestore(&ch->ch_lock, flags);
1975 * dgnc_tty_send_break()
1977 * Send a Break, called by ld.
1979 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
1981 struct dgnc_board *bd;
1982 struct channel_t *ch;
1985 unsigned long flags;
1987 if (!tty || tty->magic != TTY_MAGIC)
1990 un = tty->driver_data;
1991 if (!un || un->magic != DGNC_UNIT_MAGIC)
1995 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1999 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2013 spin_lock_irqsave(&ch->ch_lock, flags);
2015 ch->ch_bd->bd_ops->send_break(ch, msec);
2017 spin_unlock_irqrestore(&ch->ch_lock, flags);
2025 * dgnc_tty_wait_until_sent()
2027 * wait until data has been transmitted, called by ld.
2029 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
2031 struct dgnc_board *bd;
2032 struct channel_t *ch;
2036 if (!tty || tty->magic != TTY_MAGIC)
2039 un = tty->driver_data;
2040 if (!un || un->magic != DGNC_UNIT_MAGIC)
2044 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2048 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2051 rc = bd->bd_ops->drain(tty, 0);
2058 * send a high priority character, called by ld.
2060 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
2062 struct dgnc_board *bd;
2063 struct channel_t *ch;
2065 unsigned long flags;
2067 if (!tty || tty->magic != TTY_MAGIC)
2070 un = tty->driver_data;
2071 if (!un || un->magic != DGNC_UNIT_MAGIC)
2075 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2079 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2082 dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
2084 spin_lock_irqsave(&ch->ch_lock, flags);
2085 bd->bd_ops->send_immediate_char(ch, c);
2086 spin_unlock_irqrestore(&ch->ch_lock, flags);
2088 dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
2095 * Return modem signals to ld.
2097 static inline int dgnc_get_mstat(struct channel_t *ch)
2099 unsigned char mstat;
2101 unsigned long flags;
2103 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2106 spin_lock_irqsave(&ch->ch_lock, flags);
2108 mstat = (ch->ch_mostat | ch->ch_mistat);
2110 spin_unlock_irqrestore(&ch->ch_lock, flags);
2114 if (mstat & UART_MCR_DTR)
2115 result |= TIOCM_DTR;
2116 if (mstat & UART_MCR_RTS)
2117 result |= TIOCM_RTS;
2118 if (mstat & UART_MSR_CTS)
2119 result |= TIOCM_CTS;
2120 if (mstat & UART_MSR_DSR)
2121 result |= TIOCM_DSR;
2122 if (mstat & UART_MSR_RI)
2124 if (mstat & UART_MSR_DCD)
2133 * Return modem signals to ld.
2135 static int dgnc_get_modem_info(struct channel_t *ch,
2136 unsigned int __user *value)
2140 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2143 result = dgnc_get_mstat(ch);
2148 return put_user(result, value);
2153 * dgnc_set_modem_info()
2155 * Set modem signals, called by ld.
2157 static int dgnc_set_modem_info(struct tty_struct *tty,
2158 unsigned int command,
2159 unsigned int __user *value)
2161 struct dgnc_board *bd;
2162 struct channel_t *ch;
2165 unsigned int arg = 0;
2166 unsigned long flags;
2168 if (!tty || tty->magic != TTY_MAGIC)
2171 un = tty->driver_data;
2172 if (!un || un->magic != DGNC_UNIT_MAGIC)
2176 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2180 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2183 ret = get_user(arg, value);
2189 if (arg & TIOCM_RTS)
2190 ch->ch_mostat |= UART_MCR_RTS;
2192 if (arg & TIOCM_DTR)
2193 ch->ch_mostat |= UART_MCR_DTR;
2198 if (arg & TIOCM_RTS)
2199 ch->ch_mostat &= ~(UART_MCR_RTS);
2201 if (arg & TIOCM_DTR)
2202 ch->ch_mostat &= ~(UART_MCR_DTR);
2208 if (arg & TIOCM_RTS)
2209 ch->ch_mostat |= UART_MCR_RTS;
2211 ch->ch_mostat &= ~(UART_MCR_RTS);
2213 if (arg & TIOCM_DTR)
2214 ch->ch_mostat |= UART_MCR_DTR;
2216 ch->ch_mostat &= ~(UART_MCR_DTR);
2224 spin_lock_irqsave(&ch->ch_lock, flags);
2226 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2228 spin_unlock_irqrestore(&ch->ch_lock, flags);
2235 * dgnc_tty_digigeta()
2237 * Ioctl to get the information for ditty.
2242 static int dgnc_tty_digigeta(struct tty_struct *tty,
2243 struct digi_t __user *retinfo)
2245 struct channel_t *ch;
2248 unsigned long flags;
2253 if (!tty || tty->magic != TTY_MAGIC)
2256 un = tty->driver_data;
2257 if (!un || un->magic != DGNC_UNIT_MAGIC)
2261 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2264 memset(&tmp, 0, sizeof(tmp));
2266 spin_lock_irqsave(&ch->ch_lock, flags);
2267 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2268 spin_unlock_irqrestore(&ch->ch_lock, flags);
2270 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2278 * dgnc_tty_digiseta()
2280 * Ioctl to set the information for ditty.
2285 static int dgnc_tty_digiseta(struct tty_struct *tty,
2286 struct digi_t __user *new_info)
2288 struct dgnc_board *bd;
2289 struct channel_t *ch;
2291 struct digi_t new_digi;
2292 unsigned long flags;
2294 if (!tty || tty->magic != TTY_MAGIC)
2297 un = tty->driver_data;
2298 if (!un || un->magic != DGNC_UNIT_MAGIC)
2302 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2306 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2309 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
2312 spin_lock_irqsave(&ch->ch_lock, flags);
2315 * Handle transistions to and from RTS Toggle.
2317 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2318 (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2319 ch->ch_mostat &= ~(UART_MCR_RTS);
2320 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2321 !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2322 ch->ch_mostat |= (UART_MCR_RTS);
2325 * Handle transistions to and from DTR Toggle.
2327 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2328 (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2329 ch->ch_mostat &= ~(UART_MCR_DTR);
2330 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2331 !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2332 ch->ch_mostat |= (UART_MCR_DTR);
2334 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2336 if (ch->ch_digi.digi_maxcps < 1)
2337 ch->ch_digi.digi_maxcps = 1;
2339 if (ch->ch_digi.digi_maxcps > 10000)
2340 ch->ch_digi.digi_maxcps = 10000;
2342 if (ch->ch_digi.digi_bufsize < 10)
2343 ch->ch_digi.digi_bufsize = 10;
2345 if (ch->ch_digi.digi_maxchar < 1)
2346 ch->ch_digi.digi_maxchar = 1;
2348 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2349 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2351 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2352 ch->ch_digi.digi_onlen = DIGI_PLEN;
2354 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2355 ch->ch_digi.digi_offlen = DIGI_PLEN;
2357 ch->ch_bd->bd_ops->param(tty);
2359 spin_unlock_irqrestore(&ch->ch_lock, flags);
2366 * dgnc_set_termios()
2368 static void dgnc_tty_set_termios(struct tty_struct *tty,
2369 struct ktermios *old_termios)
2371 struct dgnc_board *bd;
2372 struct channel_t *ch;
2374 unsigned long flags;
2376 if (!tty || tty->magic != TTY_MAGIC)
2379 un = tty->driver_data;
2380 if (!un || un->magic != DGNC_UNIT_MAGIC)
2384 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2388 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2391 spin_lock_irqsave(&ch->ch_lock, flags);
2393 ch->ch_c_cflag = tty->termios.c_cflag;
2394 ch->ch_c_iflag = tty->termios.c_iflag;
2395 ch->ch_c_oflag = tty->termios.c_oflag;
2396 ch->ch_c_lflag = tty->termios.c_lflag;
2397 ch->ch_startc = tty->termios.c_cc[VSTART];
2398 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2400 ch->ch_bd->bd_ops->param(tty);
2403 spin_unlock_irqrestore(&ch->ch_lock, flags);
2407 static void dgnc_tty_throttle(struct tty_struct *tty)
2409 struct channel_t *ch;
2411 unsigned long flags;
2413 if (!tty || tty->magic != TTY_MAGIC)
2416 un = tty->driver_data;
2417 if (!un || un->magic != DGNC_UNIT_MAGIC)
2421 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2424 spin_lock_irqsave(&ch->ch_lock, flags);
2426 ch->ch_flags |= (CH_FORCED_STOPI);
2428 spin_unlock_irqrestore(&ch->ch_lock, flags);
2432 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2434 struct channel_t *ch;
2436 unsigned long flags;
2438 if (!tty || tty->magic != TTY_MAGIC)
2441 un = tty->driver_data;
2442 if (!un || un->magic != DGNC_UNIT_MAGIC)
2446 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2449 spin_lock_irqsave(&ch->ch_lock, flags);
2451 ch->ch_flags &= ~(CH_FORCED_STOPI);
2453 spin_unlock_irqrestore(&ch->ch_lock, flags);
2457 static void dgnc_tty_start(struct tty_struct *tty)
2459 struct dgnc_board *bd;
2460 struct channel_t *ch;
2462 unsigned long flags;
2464 if (!tty || tty->magic != TTY_MAGIC)
2467 un = tty->driver_data;
2468 if (!un || un->magic != DGNC_UNIT_MAGIC)
2472 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2476 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2479 spin_lock_irqsave(&ch->ch_lock, flags);
2481 ch->ch_flags &= ~(CH_FORCED_STOP);
2483 spin_unlock_irqrestore(&ch->ch_lock, flags);
2487 static void dgnc_tty_stop(struct tty_struct *tty)
2489 struct dgnc_board *bd;
2490 struct channel_t *ch;
2492 unsigned long flags;
2494 if (!tty || tty->magic != TTY_MAGIC)
2497 un = tty->driver_data;
2498 if (!un || un->magic != DGNC_UNIT_MAGIC)
2502 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2506 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2509 spin_lock_irqsave(&ch->ch_lock, flags);
2511 ch->ch_flags |= (CH_FORCED_STOP);
2513 spin_unlock_irqrestore(&ch->ch_lock, flags);
2518 * dgnc_tty_flush_chars()
2520 * Flush the cook buffer
2522 * Note to self, and any other poor souls who venture here:
2524 * flush in this case DOES NOT mean dispose of the data.
2525 * instead, it means "stop buffering and send it if you
2526 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2528 * It is also always called in interrupt context - JAR 8-Sept-99
2530 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2532 struct dgnc_board *bd;
2533 struct channel_t *ch;
2535 unsigned long flags;
2537 if (!tty || tty->magic != TTY_MAGIC)
2540 un = tty->driver_data;
2541 if (!un || un->magic != DGNC_UNIT_MAGIC)
2545 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2549 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2552 spin_lock_irqsave(&ch->ch_lock, flags);
2554 /* Do something maybe here */
2556 spin_unlock_irqrestore(&ch->ch_lock, flags);
2562 * dgnc_tty_flush_buffer()
2564 * Flush Tx buffer (make in == out)
2566 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2568 struct channel_t *ch;
2570 unsigned long flags;
2572 if (!tty || tty->magic != TTY_MAGIC)
2575 un = tty->driver_data;
2576 if (!un || un->magic != DGNC_UNIT_MAGIC)
2580 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2583 spin_lock_irqsave(&ch->ch_lock, flags);
2585 ch->ch_flags &= ~CH_STOP;
2587 /* Flush our write queue */
2588 ch->ch_w_head = ch->ch_w_tail;
2590 /* Flush UARTs transmit FIFO */
2591 ch->ch_bd->bd_ops->flush_uart_write(ch);
2593 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2594 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2595 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2597 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2598 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2599 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2602 spin_unlock_irqrestore(&ch->ch_lock, flags);
2607 /*****************************************************************************
2609 * The IOCTL function and all of its helpers
2611 *****************************************************************************/
2616 * The usual assortment of ioctl's
2618 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2621 struct dgnc_board *bd;
2622 struct channel_t *ch;
2625 unsigned long flags;
2626 void __user *uarg = (void __user *) arg;
2628 if (!tty || tty->magic != TTY_MAGIC)
2631 un = tty->driver_data;
2632 if (!un || un->magic != DGNC_UNIT_MAGIC)
2636 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2640 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2643 spin_lock_irqsave(&ch->ch_lock, flags);
2645 if (un->un_open_count <= 0) {
2646 spin_unlock_irqrestore(&ch->ch_lock, flags);
2652 /* Here are all the standard ioctl's that we MUST implement */
2656 * TCSBRK is SVID version: non-zero arg --> no break
2657 * this behaviour is exploited by tcdrain().
2659 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2660 * between 0.25 and 0.5 seconds so we'll ask for something
2661 * in the middle: 0.375 seconds.
2663 rc = tty_check_change(tty);
2664 spin_unlock_irqrestore(&ch->ch_lock, flags);
2668 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2673 spin_lock_irqsave(&ch->ch_lock, flags);
2675 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2676 ch->ch_bd->bd_ops->send_break(ch, 250);
2678 spin_unlock_irqrestore(&ch->ch_lock, flags);
2684 /* support for POSIX tcsendbreak()
2685 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2686 * between 0.25 and 0.5 seconds so we'll ask for something
2687 * in the middle: 0.375 seconds.
2689 rc = tty_check_change(tty);
2690 spin_unlock_irqrestore(&ch->ch_lock, flags);
2694 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2698 spin_lock_irqsave(&ch->ch_lock, flags);
2700 ch->ch_bd->bd_ops->send_break(ch, 250);
2702 spin_unlock_irqrestore(&ch->ch_lock, flags);
2707 rc = tty_check_change(tty);
2708 spin_unlock_irqrestore(&ch->ch_lock, flags);
2712 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2716 spin_lock_irqsave(&ch->ch_lock, flags);
2718 ch->ch_bd->bd_ops->send_break(ch, 250);
2720 spin_unlock_irqrestore(&ch->ch_lock, flags);
2726 spin_unlock_irqrestore(&ch->ch_lock, flags);
2731 spin_unlock_irqrestore(&ch->ch_lock, flags);
2733 rc = put_user(C_CLOCAL(tty) ? 1 : 0,
2734 (unsigned long __user *) arg);
2739 spin_unlock_irqrestore(&ch->ch_lock, flags);
2740 rc = get_user(arg, (unsigned long __user *) arg);
2744 spin_lock_irqsave(&ch->ch_lock, flags);
2745 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
2746 (arg ? CLOCAL : 0));
2747 ch->ch_bd->bd_ops->param(tty);
2748 spin_unlock_irqrestore(&ch->ch_lock, flags);
2753 spin_unlock_irqrestore(&ch->ch_lock, flags);
2754 return dgnc_get_modem_info(ch, uarg);
2759 spin_unlock_irqrestore(&ch->ch_lock, flags);
2760 return dgnc_set_modem_info(tty, cmd, uarg);
2763 * Here are any additional ioctl's that we want to implement
2768 * The linux tty driver doesn't have a flush
2769 * input routine for the driver, assuming all backed
2770 * up data is in the line disc. buffers. However,
2771 * we all know that's not the case. Here, we
2772 * act on the ioctl, but then lie and say we didn't
2773 * so the line discipline will process the flush
2776 rc = tty_check_change(tty);
2778 spin_unlock_irqrestore(&ch->ch_lock, flags);
2782 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2783 ch->ch_r_head = ch->ch_r_tail;
2784 ch->ch_bd->bd_ops->flush_uart_read(ch);
2785 /* Force queue flow control to be released, if needed */
2786 dgnc_check_queue_flow_control(ch);
2789 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2790 if (!(un->un_type == DGNC_PRINT)) {
2791 ch->ch_w_head = ch->ch_w_tail;
2792 ch->ch_bd->bd_ops->flush_uart_write(ch);
2794 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2795 ch->ch_tun.un_flags &=
2797 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2800 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2801 ch->ch_pun.un_flags &=
2803 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2809 /* pretend we didn't recognize this IOCTL */
2810 spin_unlock_irqrestore(&ch->ch_lock, flags);
2811 return -ENOIOCTLCMD;
2815 * The linux tty driver doesn't have a flush
2816 * input routine for the driver, assuming all backed
2817 * up data is in the line disc. buffers. However,
2818 * we all know that's not the case. Here, we
2819 * act on the ioctl, but then lie and say we didn't
2820 * so the line discipline will process the flush
2823 if (cmd == TCSETSF) {
2825 ch->ch_flags &= ~CH_STOP;
2826 ch->ch_r_head = ch->ch_r_tail;
2827 ch->ch_bd->bd_ops->flush_uart_read(ch);
2828 /* Force queue flow control to be released, if needed */
2829 dgnc_check_queue_flow_control(ch);
2832 /* now wait for all the output to drain */
2833 spin_unlock_irqrestore(&ch->ch_lock, flags);
2834 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2838 /* pretend we didn't recognize this */
2839 return -ENOIOCTLCMD;
2843 spin_unlock_irqrestore(&ch->ch_lock, flags);
2844 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2848 /* pretend we didn't recognize this */
2849 return -ENOIOCTLCMD;
2852 spin_unlock_irqrestore(&ch->ch_lock, flags);
2853 /* Make the ld do it */
2854 return -ENOIOCTLCMD;
2857 /* get information for ditty */
2858 spin_unlock_irqrestore(&ch->ch_lock, flags);
2859 return dgnc_tty_digigeta(tty, uarg);
2864 /* set information for ditty */
2865 if (cmd == (DIGI_SETAW)) {
2867 spin_unlock_irqrestore(&ch->ch_lock, flags);
2868 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2873 spin_lock_irqsave(&ch->ch_lock, flags);
2875 tty_ldisc_flush(tty);
2880 spin_unlock_irqrestore(&ch->ch_lock, flags);
2881 return dgnc_tty_digiseta(tty, uarg);
2886 /* Let go of locks when accessing user space,
2889 spin_unlock_irqrestore(&ch->ch_lock, flags);
2890 rc = get_user(loopback, (unsigned int __user *) arg);
2893 spin_lock_irqsave(&ch->ch_lock, flags);
2895 /* Enable/disable internal loopback for this port */
2897 ch->ch_flags |= CH_LOOPBACK;
2899 ch->ch_flags &= ~(CH_LOOPBACK);
2901 ch->ch_bd->bd_ops->param(tty);
2902 spin_unlock_irqrestore(&ch->ch_lock, flags);
2906 case DIGI_GETCUSTOMBAUD:
2907 spin_unlock_irqrestore(&ch->ch_lock, flags);
2908 rc = put_user(ch->ch_custom_speed, (unsigned int __user *) arg);
2911 case DIGI_SETCUSTOMBAUD:
2914 /* Let go of locks when accessing user space, could sleep */
2915 spin_unlock_irqrestore(&ch->ch_lock, flags);
2916 rc = get_user(new_rate, (int __user *) arg);
2919 spin_lock_irqsave(&ch->ch_lock, flags);
2920 dgnc_set_custom_speed(ch, new_rate);
2921 ch->ch_bd->bd_ops->param(tty);
2922 spin_unlock_irqrestore(&ch->ch_lock, flags);
2927 * This ioctl allows insertion of a character into the front
2928 * of any pending data to be transmitted.
2930 * This ioctl is to satify the "Send Character Immediate"
2931 * call that the RealPort protocol spec requires.
2933 case DIGI_REALPORT_SENDIMMEDIATE:
2937 spin_unlock_irqrestore(&ch->ch_lock, flags);
2938 rc = get_user(c, (unsigned char __user *) arg);
2941 spin_lock_irqsave(&ch->ch_lock, flags);
2942 ch->ch_bd->bd_ops->send_immediate_char(ch, c);
2943 spin_unlock_irqrestore(&ch->ch_lock, flags);
2948 * This ioctl returns all the current counts for the port.
2950 * This ioctl is to satify the "Line Error Counters"
2951 * call that the RealPort protocol spec requires.
2953 case DIGI_REALPORT_GETCOUNTERS:
2955 struct digi_getcounter buf;
2957 buf.norun = ch->ch_err_overrun;
2958 buf.noflow = 0; /* The driver doesn't keep this stat */
2959 buf.nframe = ch->ch_err_frame;
2960 buf.nparity = ch->ch_err_parity;
2961 buf.nbreak = ch->ch_err_break;
2962 buf.rbytes = ch->ch_rxcount;
2963 buf.tbytes = ch->ch_txcount;
2965 spin_unlock_irqrestore(&ch->ch_lock, flags);
2967 if (copy_to_user(uarg, &buf, sizeof(buf)))
2974 * This ioctl returns all current events.
2976 * This ioctl is to satify the "Event Reporting"
2977 * call that the RealPort protocol spec requires.
2979 case DIGI_REALPORT_GETEVENTS:
2981 unsigned int events = 0;
2983 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2984 if (ch->ch_flags & CH_BREAK_SENDING)
2986 if ((ch->ch_flags & CH_STOP) ||
2987 (ch->ch_flags & CH_FORCED_STOP))
2988 events |= (EV_OPU | EV_OPS);
2990 if ((ch->ch_flags & CH_STOPI) ||
2991 (ch->ch_flags & CH_FORCED_STOPI))
2992 events |= (EV_IPU | EV_IPS);
2994 spin_unlock_irqrestore(&ch->ch_lock, flags);
2995 rc = put_user(events, (unsigned int __user *) arg);
3000 * This ioctl returns TOUT and TIN counters based
3001 * upon the values passed in by the RealPort Server.
3002 * It also passes back whether the UART Transmitter is
3005 case DIGI_REALPORT_GETBUFFERS:
3007 struct digi_getbuffer buf;
3011 spin_unlock_irqrestore(&ch->ch_lock, flags);
3014 * Get data from user first.
3016 if (copy_from_user(&buf, uarg, sizeof(buf)))
3019 spin_lock_irqsave(&ch->ch_lock, flags);
3022 * Figure out how much data is in our RX and TX queues.
3024 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
3025 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
3028 * Is the UART empty? Add that value to whats in our TX queue.
3030 count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
3033 * Figure out how much data the RealPort Server believes should
3034 * be in our TX queue.
3036 tdist = (buf.tIn - buf.tOut) & 0xffff;
3039 * If we have more data than the RealPort Server believes we
3040 * should have, reduce our count to its amount.
3042 * This count difference CAN happen because the Linux LD can
3043 * insert more characters into our queue for OPOST processing
3044 * that the RealPort Server doesn't know about.
3046 if (buf.txbuf > tdist)
3050 * Report whether our queue and UART TX are completely empty.
3057 spin_unlock_irqrestore(&ch->ch_lock, flags);
3059 if (copy_to_user(uarg, &buf, sizeof(buf)))
3065 spin_unlock_irqrestore(&ch->ch_lock, flags);
3067 return -ENOIOCTLCMD;