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"
45 #define init_MUTEX(sem) sema_init(sem, 1)
46 #define DECLARE_MUTEX(name) \
47 struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
52 static struct dgnc_board *dgnc_BoardsByMajor[256];
53 static unsigned char *dgnc_TmpWriteBuf;
54 static DECLARE_MUTEX(dgnc_TmpWriteSem);
57 * Default transparent print information.
59 static struct digi_t dgnc_digi_init = {
60 .digi_flags = DIGI_COOK, /* Flags */
61 .digi_maxcps = 100, /* Max CPS */
62 .digi_maxchar = 50, /* Max chars in print queue */
63 .digi_bufsize = 100, /* Printer buffer size */
64 .digi_onlen = 4, /* size of printer on string */
65 .digi_offlen = 4, /* size of printer off string */
66 .digi_onstr = "\033[5i", /* ANSI printer on string ] */
67 .digi_offstr = "\033[4i", /* ANSI printer off string ] */
68 .digi_term = "ansi" /* default terminal type */
73 * Define a local default termios struct. All ports will be created
74 * with this termios initially.
76 * This defines a raw port at 9600 baud, 8 data bits, no parity,
79 static struct ktermios DgncDefaultTermios = {
80 .c_iflag = (DEFAULT_IFLAGS), /* iflags */
81 .c_oflag = (DEFAULT_OFLAGS), /* oflags */
82 .c_cflag = (DEFAULT_CFLAGS), /* cflags */
83 .c_lflag = (DEFAULT_LFLAGS), /* lflags */
89 /* Our function prototypes */
90 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
91 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
92 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch);
93 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
94 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo);
95 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info);
96 static int dgnc_tty_write_room(struct tty_struct *tty);
97 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
98 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
99 static void dgnc_tty_start(struct tty_struct *tty);
100 static void dgnc_tty_stop(struct tty_struct *tty);
101 static void dgnc_tty_throttle(struct tty_struct *tty);
102 static void dgnc_tty_unthrottle(struct tty_struct *tty);
103 static void dgnc_tty_flush_chars(struct tty_struct *tty);
104 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
105 static void dgnc_tty_hangup(struct tty_struct *tty);
106 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value);
107 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value);
108 static int dgnc_tty_tiocmget(struct tty_struct *tty);
109 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear);
110 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
111 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
112 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf, int count);
113 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios);
114 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
117 static const struct tty_operations dgnc_tty_ops = {
118 .open = dgnc_tty_open,
119 .close = dgnc_tty_close,
120 .write = dgnc_tty_write,
121 .write_room = dgnc_tty_write_room,
122 .flush_buffer = dgnc_tty_flush_buffer,
123 .chars_in_buffer = dgnc_tty_chars_in_buffer,
124 .flush_chars = dgnc_tty_flush_chars,
125 .ioctl = dgnc_tty_ioctl,
126 .set_termios = dgnc_tty_set_termios,
127 .stop = dgnc_tty_stop,
128 .start = dgnc_tty_start,
129 .throttle = dgnc_tty_throttle,
130 .unthrottle = dgnc_tty_unthrottle,
131 .hangup = dgnc_tty_hangup,
132 .put_char = dgnc_tty_put_char,
133 .tiocmget = dgnc_tty_tiocmget,
134 .tiocmset = dgnc_tty_tiocmset,
135 .break_ctl = dgnc_tty_send_break,
136 .wait_until_sent = dgnc_tty_wait_until_sent,
137 .send_xchar = dgnc_tty_send_xchar
140 /************************************************************************
142 * TTY Initialization/Cleanup Functions
144 ************************************************************************/
149 * Initialize any global tty related data before we download any boards.
151 int dgnc_tty_preinit(void)
154 * Allocate a buffer for doing the copy from user space to
155 * kernel space in dgnc_write(). We only use one buffer and
156 * control access to it with a semaphore. If we are paging, we
157 * are already in trouble so one buffer won't hurt much anyway.
159 * We are okay to sleep in the malloc, as this routine
160 * is only called during module load, (not in interrupt context),
161 * and with no locks held.
163 dgnc_TmpWriteBuf = kmalloc(WRITEBUFLEN, GFP_KERNEL);
165 if (!dgnc_TmpWriteBuf)
173 * dgnc_tty_register()
175 * Init the tty subsystem for this board.
177 int dgnc_tty_register(struct dgnc_board *brd)
181 brd->SerialDriver.magic = TTY_DRIVER_MAGIC;
183 snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgnc_%d_", brd->boardnum);
185 brd->SerialDriver.name = brd->SerialName;
186 brd->SerialDriver.name_base = 0;
187 brd->SerialDriver.major = 0;
188 brd->SerialDriver.minor_start = 0;
189 brd->SerialDriver.num = brd->maxports;
190 brd->SerialDriver.type = TTY_DRIVER_TYPE_SERIAL;
191 brd->SerialDriver.subtype = SERIAL_TYPE_NORMAL;
192 brd->SerialDriver.init_termios = DgncDefaultTermios;
193 brd->SerialDriver.driver_name = DRVSTR;
194 brd->SerialDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
197 * The kernel wants space to store pointers to
198 * tty_struct's and termios's.
200 brd->SerialDriver.ttys = kcalloc(brd->maxports, sizeof(*brd->SerialDriver.ttys), GFP_KERNEL);
201 if (!brd->SerialDriver.ttys)
204 kref_init(&brd->SerialDriver.kref);
205 brd->SerialDriver.termios = kcalloc(brd->maxports, sizeof(*brd->SerialDriver.termios), GFP_KERNEL);
206 if (!brd->SerialDriver.termios)
210 * Entry points for driver. Called by the kernel from
211 * tty_io.c and n_tty.c.
213 tty_set_operations(&brd->SerialDriver, &dgnc_tty_ops);
215 if (!brd->dgnc_Major_Serial_Registered) {
216 /* Register tty devices */
217 rc = tty_register_driver(&brd->SerialDriver);
219 dev_dbg(&brd->pdev->dev,
220 "Can't register tty device (%d)\n", rc);
223 brd->dgnc_Major_Serial_Registered = true;
227 * If we're doing transparent print, we have to do all of the above
228 * again, separately so we don't get the LD confused about what major
229 * we are when we get into the dgnc_tty_open() routine.
231 brd->PrintDriver.magic = TTY_DRIVER_MAGIC;
232 snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
234 brd->PrintDriver.name = brd->PrintName;
235 brd->PrintDriver.name_base = 0;
236 brd->PrintDriver.major = brd->SerialDriver.major;
237 brd->PrintDriver.minor_start = 0x80;
238 brd->PrintDriver.num = brd->maxports;
239 brd->PrintDriver.type = TTY_DRIVER_TYPE_SERIAL;
240 brd->PrintDriver.subtype = SERIAL_TYPE_NORMAL;
241 brd->PrintDriver.init_termios = DgncDefaultTermios;
242 brd->PrintDriver.driver_name = DRVSTR;
243 brd->PrintDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
246 * The kernel wants space to store pointers to
247 * tty_struct's and termios's. Must be separated from
248 * the Serial Driver so we don't get confused
250 brd->PrintDriver.ttys = kcalloc(brd->maxports, sizeof(*brd->PrintDriver.ttys), GFP_KERNEL);
251 if (!brd->PrintDriver.ttys)
253 kref_init(&brd->PrintDriver.kref);
254 brd->PrintDriver.termios = kcalloc(brd->maxports, sizeof(*brd->PrintDriver.termios), GFP_KERNEL);
255 if (!brd->PrintDriver.termios)
259 * Entry points for driver. Called by the kernel from
260 * tty_io.c and n_tty.c.
262 tty_set_operations(&brd->PrintDriver, &dgnc_tty_ops);
264 if (!brd->dgnc_Major_TransparentPrint_Registered) {
265 /* Register Transparent Print devices */
266 rc = tty_register_driver(&brd->PrintDriver);
268 dev_dbg(&brd->pdev->dev,
269 "Can't register Transparent Print device(%d)\n",
273 brd->dgnc_Major_TransparentPrint_Registered = true;
276 dgnc_BoardsByMajor[brd->SerialDriver.major] = brd;
277 brd->dgnc_Serial_Major = brd->SerialDriver.major;
278 brd->dgnc_TransparentPrint_Major = brd->PrintDriver.major;
287 * Init the tty subsystem. Called once per board after board has been
288 * downloaded and init'ed.
290 int dgnc_tty_init(struct dgnc_board *brd)
294 struct channel_t *ch;
300 * Initialize board structure elements.
303 vaddr = brd->re_map_membase;
305 brd->nasync = brd->maxports;
308 * Allocate channel memory that might not have been allocated
309 * when the driver was first loaded.
311 for (i = 0; i < brd->nasync; i++) {
312 if (!brd->channels[i]) {
315 * Okay to malloc with GFP_KERNEL, we are not at
316 * interrupt context, and there are no locks held.
318 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]), GFP_KERNEL);
322 ch = brd->channels[0];
323 vaddr = brd->re_map_membase;
325 /* Set up channel variables */
326 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
328 if (!brd->channels[i])
331 spin_lock_init(&ch->ch_lock);
333 /* Store all our magic numbers */
334 ch->magic = DGNC_CHANNEL_MAGIC;
335 ch->ch_tun.magic = DGNC_UNIT_MAGIC;
336 ch->ch_tun.un_ch = ch;
337 ch->ch_tun.un_type = DGNC_SERIAL;
338 ch->ch_tun.un_dev = i;
340 ch->ch_pun.magic = DGNC_UNIT_MAGIC;
341 ch->ch_pun.un_ch = ch;
342 ch->ch_pun.un_type = DGNC_PRINT;
343 ch->ch_pun.un_dev = i + 128;
345 if (brd->bd_uart_offset == 0x200)
346 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
348 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
352 ch->ch_digi = dgnc_digi_init;
354 /* .25 second delay */
355 ch->ch_close_delay = 250;
357 init_waitqueue_head(&ch->ch_flags_wait);
358 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
359 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
362 struct device *classp;
364 classp = tty_register_device(&brd->SerialDriver, i,
365 &(ch->ch_bd->pdev->dev));
366 ch->ch_tun.un_sysfs = classp;
367 dgnc_create_tty_sysfs(&ch->ch_tun, classp);
369 classp = tty_register_device(&brd->PrintDriver, i,
370 &(ch->ch_bd->pdev->dev));
371 ch->ch_pun.un_sysfs = classp;
372 dgnc_create_tty_sysfs(&ch->ch_pun, classp);
382 * dgnc_tty_post_uninit()
384 * UnInitialize any global tty related data.
386 void dgnc_tty_post_uninit(void)
388 kfree(dgnc_TmpWriteBuf);
389 dgnc_TmpWriteBuf = NULL;
396 * Uninitialize the TTY portion of this driver. Free all memory and
399 void dgnc_tty_uninit(struct dgnc_board *brd)
403 if (brd->dgnc_Major_Serial_Registered) {
404 dgnc_BoardsByMajor[brd->SerialDriver.major] = NULL;
405 brd->dgnc_Serial_Major = 0;
406 for (i = 0; i < brd->nasync; i++) {
407 dgnc_remove_tty_sysfs(brd->channels[i]->ch_tun.un_sysfs);
408 tty_unregister_device(&brd->SerialDriver, i);
410 tty_unregister_driver(&brd->SerialDriver);
411 brd->dgnc_Major_Serial_Registered = false;
414 if (brd->dgnc_Major_TransparentPrint_Registered) {
415 dgnc_BoardsByMajor[brd->PrintDriver.major] = NULL;
416 brd->dgnc_TransparentPrint_Major = 0;
417 for (i = 0; i < brd->nasync; i++) {
418 dgnc_remove_tty_sysfs(brd->channels[i]->ch_pun.un_sysfs);
419 tty_unregister_device(&brd->PrintDriver, i);
421 tty_unregister_driver(&brd->PrintDriver);
422 brd->dgnc_Major_TransparentPrint_Registered = false;
425 kfree(brd->SerialDriver.ttys);
426 brd->SerialDriver.ttys = NULL;
427 kfree(brd->PrintDriver.ttys);
428 brd->PrintDriver.ttys = NULL;
432 #define TMPBUFLEN (1024)
434 /*=======================================================================
436 * dgnc_wmove - Write data to transmit queue.
438 * ch - Pointer to channel structure.
439 * buf - Poiter to characters to be moved.
440 * n - Number of characters to move.
442 *=======================================================================*/
443 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
448 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
451 head = ch->ch_w_head & WQUEUEMASK;
454 * If the write wraps over the top of the circular buffer,
455 * move the portion up to the wrap point, and reset the
456 * pointers to the bottom.
458 remain = WQUEUESIZE - head;
462 memcpy(ch->ch_wqueue + head, buf, remain);
472 memcpy(ch->ch_wqueue + head, buf, remain);
477 ch->ch_w_head = head;
483 /*=======================================================================
485 * dgnc_input - Process received data.
487 * ch - Pointer to channel structure.
489 *=======================================================================*/
490 void dgnc_input(struct channel_t *ch)
492 struct dgnc_board *bd;
493 struct tty_struct *tp;
494 struct tty_ldisc *ld = NULL;
506 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
509 tp = ch->ch_tun.un_tty;
512 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
515 spin_lock_irqsave(&ch->ch_lock, flags);
518 * Figure the number of characters in the buffer.
519 * Exit immediately if none.
522 head = ch->ch_r_head & rmask;
523 tail = ch->ch_r_tail & rmask;
524 data_len = (head - tail) & rmask;
530 * If the device is not open, or CREAD is off,
531 * flush input data and return immediately.
533 if (!tp || (tp->magic != TTY_MAGIC) || !(ch->ch_tun.un_flags & UN_ISOPEN) ||
534 !(tp->termios.c_cflag & CREAD) || (ch->ch_tun.un_flags & UN_CLOSING)) {
536 ch->ch_r_head = tail;
538 /* Force queue flow control to be released, if needed */
539 dgnc_check_queue_flow_control(ch);
545 * If we are throttled, simply don't read any data.
547 if (ch->ch_flags & CH_FORCED_STOPI)
550 flip_len = TTY_FLIPBUF_SIZE;
552 /* Chop down the length, if needed */
553 len = min(data_len, flip_len);
554 len = min(len, (N_TTY_BUF_SIZE - 1));
556 ld = tty_ldisc_ref(tp);
560 * If the DONT_FLIP flag is on, don't flush our buffer, and act
561 * like the ld doesn't have any space to put the data right now.
563 if (test_bit(TTY_DONT_FLIP, &tp->flags))
568 * If we were unable to get a reference to the ld,
569 * don't flush our buffer, and act like the ld doesn't
570 * have any space to put the data right now.
576 * If ld doesn't have a pointer to a receive_buf function,
577 * flush the data, then act like the ld doesn't have any
578 * space to put the data right now.
580 if (!ld->ops->receive_buf) {
581 ch->ch_r_head = ch->ch_r_tail;
590 * The tty layer in the kernel has changed in 2.6.16+.
592 * The flip buffers in the tty structure are no longer exposed,
593 * and probably will be going away eventually.
595 * If we are completely raw, we don't need to go through a lot
596 * of the tty layers that exist.
597 * In this case, we take the shortest and fastest route we
598 * can to relay the data to the user.
600 * On the other hand, if we are not raw, we need to go through
601 * the new 2.6.16+ tty layer, which has its API more well defined.
603 len = tty_buffer_request_room(tp->port, len);
607 * n now contains the most amount of data we can copy,
608 * bounded either by how much the Linux tty layer can handle,
609 * or the amount of data the card actually has pending...
612 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
619 * If conditions are such that ld needs to see all
620 * UART errors, we will have to walk each character
621 * and error byte and send them to the buffer one at
624 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
625 for (i = 0; i < s; i++) {
626 if (*(ch->ch_equeue + tail + i) & UART_LSR_BI)
627 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_BREAK);
628 else if (*(ch->ch_equeue + tail + i) & UART_LSR_PE)
629 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_PARITY);
630 else if (*(ch->ch_equeue + tail + i) & UART_LSR_FE)
631 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_FRAME);
633 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_NORMAL);
636 tty_insert_flip_string(tp->port, ch->ch_rqueue + tail, s);
641 /* Flip queue if needed */
645 ch->ch_r_tail = tail & rmask;
646 ch->ch_e_tail = tail & rmask;
647 dgnc_check_queue_flow_control(ch);
648 spin_unlock_irqrestore(&ch->ch_lock, flags);
650 /* Tell the tty layer its okay to "eat" the data now */
651 tty_flip_buffer_push(tp->port);
658 spin_unlock_irqrestore(&ch->ch_lock, flags);
664 /************************************************************************
665 * Determines when CARRIER changes state and takes appropriate
667 ************************************************************************/
668 void dgnc_carrier(struct channel_t *ch)
670 struct dgnc_board *bd;
672 int virt_carrier = 0;
673 int phys_carrier = 0;
675 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
680 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
683 if (ch->ch_mistat & UART_MSR_DCD)
686 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
689 if (ch->ch_c_cflag & CLOCAL)
693 * Test for a VIRTUAL carrier transition to HIGH.
695 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
698 * When carrier rises, wake any threads waiting
699 * for carrier in the open routine.
702 if (waitqueue_active(&(ch->ch_flags_wait)))
703 wake_up_interruptible(&ch->ch_flags_wait);
707 * Test for a PHYSICAL carrier transition to HIGH.
709 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
712 * When carrier rises, wake any threads waiting
713 * for carrier in the open routine.
716 if (waitqueue_active(&(ch->ch_flags_wait)))
717 wake_up_interruptible(&ch->ch_flags_wait);
721 * Test for a PHYSICAL transition to low, so long as we aren't
722 * currently ignoring physical transitions (which is what "virtual
723 * carrier" indicates).
725 * The transition of the virtual carrier to low really doesn't
726 * matter... it really only means "ignore carrier state", not
727 * "make pretend that carrier is there".
729 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
730 (phys_carrier == 0)) {
733 * When carrier drops:
735 * Drop carrier on all open units.
737 * Flush queues, waking up any task waiting in the
740 * Send a hangup to the control terminal.
742 * Enable all select calls.
744 if (waitqueue_active(&(ch->ch_flags_wait)))
745 wake_up_interruptible(&ch->ch_flags_wait);
747 if (ch->ch_tun.un_open_count > 0)
748 tty_hangup(ch->ch_tun.un_tty);
750 if (ch->ch_pun.un_open_count > 0)
751 tty_hangup(ch->ch_pun.un_tty);
755 * Make sure that our cached values reflect the current reality.
757 if (virt_carrier == 1)
758 ch->ch_flags |= CH_FCAR;
760 ch->ch_flags &= ~CH_FCAR;
762 if (phys_carrier == 1)
763 ch->ch_flags |= CH_CD;
765 ch->ch_flags &= ~CH_CD;
769 * Assign the custom baud rate to the channel structure
771 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
780 ch->ch_custom_speed = 0;
785 * Since the divisor is stored in a 16-bit integer, we make sure
786 * we don't allow any rates smaller than a 16-bit integer would allow.
787 * And of course, rates above the dividend won't fly.
789 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
790 newrate = ((ch->ch_bd->bd_dividend / 0xFFFF) + 1);
792 if (newrate && newrate > ch->ch_bd->bd_dividend)
793 newrate = ch->ch_bd->bd_dividend;
796 testdiv = ch->ch_bd->bd_dividend / newrate;
799 * If we try to figure out what rate the board would use
800 * with the test divisor, it will be either equal or higher
801 * than the requested baud rate. If we then determine the
802 * rate with a divisor one higher, we will get the next lower
803 * supported rate below the requested.
805 testrate_high = ch->ch_bd->bd_dividend / testdiv;
806 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
809 * If the rate for the requested divisor is correct, just
810 * use it and be done.
812 if (testrate_high != newrate) {
814 * Otherwise, pick the rate that is closer (i.e. whichever rate
815 * has a smaller delta).
817 deltahigh = testrate_high - newrate;
818 deltalow = newrate - testrate_low;
820 if (deltahigh < deltalow)
821 newrate = testrate_high;
823 newrate = testrate_low;
827 ch->ch_custom_speed = newrate;
831 void dgnc_check_queue_flow_control(struct channel_t *ch)
835 /* Store how much space we have left in the queue */
836 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
838 qleft += RQUEUEMASK + 1;
841 * Check to see if we should enforce flow control on our queue because
842 * the ld (or user) isn't reading data out of our queue fast enuf.
844 * NOTE: This is done based on what the current flow control of the
847 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
848 * This will cause the UART's FIFO to back up, and force
849 * the RTS signal to be dropped.
850 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
851 * the other side, in hopes it will stop sending data to us.
852 * 3) NONE - Nothing we can do. We will simply drop any extra data
853 * that gets sent into us when the queue fills up.
857 if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
858 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
859 ch->ch_bd->bd_ops->disable_receiver(ch);
860 ch->ch_flags |= (CH_RECEIVER_OFF);
864 else if (ch->ch_c_iflag & IXOFF) {
865 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
866 ch->ch_bd->bd_ops->send_stop_character(ch);
873 * Check to see if we should unenforce flow control because
874 * ld (or user) finally read enuf data out of our queue.
876 * NOTE: This is done based on what the current flow control of the
879 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
880 * This will cause the UART's FIFO to raise RTS back up,
881 * which will allow the other side to start sending data again.
882 * 2) SWFLOW (IXOFF) - Send a start character to
883 * the other side, so it will start sending data to us again.
884 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
885 * other side, we don't need to do anything now.
887 if (qleft > (RQUEUESIZE / 2)) {
889 if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
890 if (ch->ch_flags & CH_RECEIVER_OFF) {
891 ch->ch_bd->bd_ops->enable_receiver(ch);
892 ch->ch_flags &= ~(CH_RECEIVER_OFF);
896 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
897 ch->ch_stops_sent = 0;
898 ch->ch_bd->bd_ops->send_start_character(ch);
902 /* Nothing needed. */
908 void dgnc_wakeup_writes(struct channel_t *ch)
913 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
916 spin_lock_irqsave(&ch->ch_lock, flags);
919 * If channel now has space, wake up anyone waiting on the condition.
921 qlen = ch->ch_w_head - ch->ch_w_tail;
925 if (qlen >= (WQUEUESIZE - 256)) {
926 spin_unlock_irqrestore(&ch->ch_lock, flags);
930 if (ch->ch_tun.un_flags & UN_ISOPEN) {
931 if ((ch->ch_tun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
932 ch->ch_tun.un_tty->ldisc->ops->write_wakeup) {
933 spin_unlock_irqrestore(&ch->ch_lock, flags);
934 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
935 spin_lock_irqsave(&ch->ch_lock, flags);
938 wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
941 * If unit is set to wait until empty, check to make sure
942 * the queue AND FIFO are both empty.
944 if (ch->ch_tun.un_flags & UN_EMPTY) {
945 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
946 ch->ch_tun.un_flags &= ~(UN_EMPTY);
949 * If RTS Toggle mode is on, whenever
950 * the queue and UART is empty, keep RTS low.
952 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
953 ch->ch_mostat &= ~(UART_MCR_RTS);
954 ch->ch_bd->bd_ops->assert_modem_signals(ch);
958 * If DTR Toggle mode is on, whenever
959 * the queue and UART is empty, keep DTR low.
961 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
962 ch->ch_mostat &= ~(UART_MCR_DTR);
963 ch->ch_bd->bd_ops->assert_modem_signals(ch);
968 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
971 if (ch->ch_pun.un_flags & UN_ISOPEN) {
972 if ((ch->ch_pun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
973 ch->ch_pun.un_tty->ldisc->ops->write_wakeup) {
974 spin_unlock_irqrestore(&ch->ch_lock, flags);
975 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
976 spin_lock_irqsave(&ch->ch_lock, flags);
979 wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
982 * If unit is set to wait until empty, check to make sure
983 * the queue AND FIFO are both empty.
985 if (ch->ch_pun.un_flags & UN_EMPTY) {
986 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
987 ch->ch_pun.un_flags &= ~(UN_EMPTY);
990 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
993 spin_unlock_irqrestore(&ch->ch_lock, flags);
998 /************************************************************************
1000 * TTY Entry points and helper functions
1002 ************************************************************************/
1008 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
1010 struct dgnc_board *brd;
1011 struct channel_t *ch;
1016 unsigned long flags;
1020 major = MAJOR(tty_devnum(tty));
1021 minor = MINOR(tty_devnum(tty));
1026 /* Get board pointer from our array of majors we have allocated */
1027 brd = dgnc_BoardsByMajor[major];
1032 * If board is not yet up to a state of READY, go to
1033 * sleep waiting for it to happen or they cancel the open.
1035 rc = wait_event_interruptible(brd->state_wait,
1036 (brd->state & BOARD_READY));
1041 spin_lock_irqsave(&brd->bd_lock, flags);
1043 /* If opened device is greater than our number of ports, bail. */
1044 if (PORT_NUM(minor) >= brd->nasync) {
1045 spin_unlock_irqrestore(&brd->bd_lock, flags);
1049 ch = brd->channels[PORT_NUM(minor)];
1051 spin_unlock_irqrestore(&brd->bd_lock, flags);
1055 /* Drop board lock */
1056 spin_unlock_irqrestore(&brd->bd_lock, flags);
1058 /* Grab channel lock */
1059 spin_lock_irqsave(&ch->ch_lock, flags);
1061 /* Figure out our type */
1062 if (!IS_PRINT(minor)) {
1063 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1064 un->un_type = DGNC_SERIAL;
1065 } else if (IS_PRINT(minor)) {
1066 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1067 un->un_type = DGNC_PRINT;
1069 spin_unlock_irqrestore(&ch->ch_lock, flags);
1074 * If the port is still in a previous open, and in a state
1075 * where we simply cannot safely keep going, wait until the
1078 spin_unlock_irqrestore(&ch->ch_lock, flags);
1080 rc = wait_event_interruptible(ch->ch_flags_wait, ((ch->ch_flags & CH_OPENING) == 0));
1082 /* If ret is non-zero, user ctrl-c'ed us */
1087 * If either unit is in the middle of the fragile part of close,
1088 * we just cannot touch the channel safely.
1089 * Go to sleep, knowing that when the channel can be
1090 * touched safely, the close routine will signal the
1091 * ch_flags_wait to wake us back up.
1093 rc = wait_event_interruptible(ch->ch_flags_wait,
1094 (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING) == 0));
1096 /* If ret is non-zero, user ctrl-c'ed us */
1100 spin_lock_irqsave(&ch->ch_lock, flags);
1103 /* Store our unit into driver_data, so we always have it available. */
1104 tty->driver_data = un;
1110 if (!(un->un_flags & UN_ISOPEN)) {
1111 /* Store important variables. */
1114 /* Maybe do something here to the TTY struct as well? */
1119 * Allocate channel buffers for read/write/error.
1120 * Set flag, so we don't get trounced on.
1122 ch->ch_flags |= (CH_OPENING);
1124 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1125 spin_unlock_irqrestore(&ch->ch_lock, flags);
1128 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1130 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1132 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1134 spin_lock_irqsave(&ch->ch_lock, flags);
1136 ch->ch_flags &= ~(CH_OPENING);
1137 wake_up_interruptible(&ch->ch_flags_wait);
1140 * Initialize if neither terminal or printer is open.
1142 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1145 * Flush input queues.
1154 brd->bd_ops->flush_uart_write(ch);
1155 brd->bd_ops->flush_uart_read(ch);
1158 ch->ch_cached_lsr = 0;
1159 ch->ch_stop_sending_break = 0;
1160 ch->ch_stops_sent = 0;
1162 ch->ch_c_cflag = tty->termios.c_cflag;
1163 ch->ch_c_iflag = tty->termios.c_iflag;
1164 ch->ch_c_oflag = tty->termios.c_oflag;
1165 ch->ch_c_lflag = tty->termios.c_lflag;
1166 ch->ch_startc = tty->termios.c_cc[VSTART];
1167 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1170 * Bring up RTS and DTR...
1171 * Also handle RTS or DTR toggle if set.
1173 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1174 ch->ch_mostat |= (UART_MCR_RTS);
1175 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1176 ch->ch_mostat |= (UART_MCR_DTR);
1178 /* Tell UART to init itself */
1179 brd->bd_ops->uart_init(ch);
1183 * Run param in case we changed anything
1185 brd->bd_ops->param(tty);
1190 * follow protocol for opening port
1193 spin_unlock_irqrestore(&ch->ch_lock, flags);
1195 rc = dgnc_block_til_ready(tty, file, ch);
1197 /* No going back now, increment our unit and channel counters */
1198 spin_lock_irqsave(&ch->ch_lock, flags);
1199 ch->ch_open_count++;
1200 un->un_open_count++;
1201 un->un_flags |= (UN_ISOPEN);
1202 spin_unlock_irqrestore(&ch->ch_lock, flags);
1209 * dgnc_block_til_ready()
1211 * Wait for DCD, if needed.
1213 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch)
1216 struct un_t *un = NULL;
1217 unsigned long flags;
1219 int sleep_on_un_flags = 0;
1221 if (!tty || tty->magic != TTY_MAGIC || !file || !ch || ch->magic != DGNC_CHANNEL_MAGIC)
1224 un = tty->driver_data;
1225 if (!un || un->magic != DGNC_UNIT_MAGIC)
1228 spin_lock_irqsave(&ch->ch_lock, flags);
1235 sleep_on_un_flags = 0;
1238 * If board has failed somehow during our sleep, bail with error.
1240 if (ch->ch_bd->state == BOARD_FAILED) {
1245 /* If tty was hung up, break out of loop and set error. */
1246 if (tty_hung_up_p(file)) {
1252 * If either unit is in the middle of the fragile part of close,
1253 * we just cannot touch the channel safely.
1254 * Go back to sleep, knowing that when the channel can be
1255 * touched safely, the close routine will signal the
1256 * ch_wait_flags to wake us back up.
1258 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING)) {
1261 * Our conditions to leave cleanly and happily:
1262 * 1) NONBLOCKING on the tty is set.
1264 * 3) DCD (fake or real) is active.
1267 if (file->f_flags & O_NONBLOCK)
1270 if (tty->flags & (1 << TTY_IO_ERROR)) {
1275 if (ch->ch_flags & CH_CD)
1278 if (ch->ch_flags & CH_FCAR)
1281 sleep_on_un_flags = 1;
1285 * If there is a signal pending, the user probably
1286 * interrupted (ctrl-c) us.
1287 * Leave loop with error set.
1289 if (signal_pending(current)) {
1290 retval = -ERESTARTSYS;
1295 * Store the flags before we let go of channel lock
1297 if (sleep_on_un_flags)
1298 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1300 old_flags = ch->ch_flags;
1303 * Let go of channel lock before calling schedule.
1304 * Our poller will get any FEP events and wake us up when DCD
1305 * eventually goes active.
1308 spin_unlock_irqrestore(&ch->ch_lock, flags);
1311 * Wait for something in the flags to change from the current value.
1313 if (sleep_on_un_flags)
1314 retval = wait_event_interruptible(un->un_flags_wait,
1315 (old_flags != (ch->ch_tun.un_flags | ch->ch_pun.un_flags)));
1317 retval = wait_event_interruptible(ch->ch_flags_wait,
1318 (old_flags != ch->ch_flags));
1321 * We got woken up for some reason.
1322 * Before looping around, grab our channel lock.
1324 spin_lock_irqsave(&ch->ch_lock, flags);
1329 spin_unlock_irqrestore(&ch->ch_lock, flags);
1341 * Hangup the port. Like a close, but don't wait for output to drain.
1343 static void dgnc_tty_hangup(struct tty_struct *tty)
1347 if (!tty || tty->magic != TTY_MAGIC)
1350 un = tty->driver_data;
1351 if (!un || un->magic != DGNC_UNIT_MAGIC)
1354 /* flush the transmit queues */
1355 dgnc_tty_flush_buffer(tty);
1364 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1366 struct ktermios *ts;
1367 struct dgnc_board *bd;
1368 struct channel_t *ch;
1370 unsigned long flags;
1373 if (!tty || tty->magic != TTY_MAGIC)
1376 un = tty->driver_data;
1377 if (!un || un->magic != DGNC_UNIT_MAGIC)
1381 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1385 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1390 spin_lock_irqsave(&ch->ch_lock, flags);
1393 * Determine if this is the last close or not - and if we agree about
1394 * which type of close it is with the Line Discipline
1396 if ((tty->count == 1) && (un->un_open_count != 1)) {
1398 * Uh, oh. tty->count is 1, which means that the tty
1399 * structure will be freed. un_open_count should always
1400 * be one in these conditions. If it's greater than
1401 * one, we've got real problems, since it means the
1402 * serial port won't be shutdown.
1405 "tty->count is 1, un open count is %d\n",
1407 un->un_open_count = 1;
1410 if (un->un_open_count)
1411 un->un_open_count--;
1414 "bad serial port open count of %d\n",
1417 ch->ch_open_count--;
1419 if (ch->ch_open_count && un->un_open_count) {
1420 spin_unlock_irqrestore(&ch->ch_lock, flags);
1424 /* OK, its the last close on the unit */
1425 un->un_flags |= UN_CLOSING;
1431 * Only officially close channel if count is 0 and
1432 * DIGI_PRINTER bit is not set.
1434 if ((ch->ch_open_count == 0) && !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1436 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1439 * turn off print device when closing print device.
1441 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1442 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1443 (int) ch->ch_digi.digi_offlen);
1444 ch->ch_flags &= ~CH_PRON;
1447 spin_unlock_irqrestore(&ch->ch_lock, flags);
1448 /* wait for output to drain */
1449 /* This will also return if we take an interrupt */
1451 rc = bd->bd_ops->drain(tty, 0);
1453 dgnc_tty_flush_buffer(tty);
1454 tty_ldisc_flush(tty);
1456 spin_lock_irqsave(&ch->ch_lock, flags);
1461 * If we have HUPCL set, lower DTR and RTS
1463 if (ch->ch_c_cflag & HUPCL) {
1466 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1467 bd->bd_ops->assert_modem_signals(ch);
1470 * Go to sleep to ensure RTS/DTR
1471 * have been dropped for modems to see it.
1473 if (ch->ch_close_delay) {
1474 spin_unlock_irqrestore(&ch->ch_lock,
1476 dgnc_ms_sleep(ch->ch_close_delay);
1477 spin_lock_irqsave(&ch->ch_lock, flags);
1481 ch->ch_old_baud = 0;
1483 /* Turn off UART interrupts for this port */
1484 ch->ch_bd->bd_ops->uart_off(ch);
1487 * turn off print device when closing print device.
1489 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1490 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1491 (int) ch->ch_digi.digi_offlen);
1492 ch->ch_flags &= ~CH_PRON;
1497 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1499 wake_up_interruptible(&ch->ch_flags_wait);
1500 wake_up_interruptible(&un->un_flags_wait);
1502 spin_unlock_irqrestore(&ch->ch_lock, flags);
1507 * dgnc_tty_chars_in_buffer()
1509 * Return number of characters that have not been transmitted yet.
1511 * This routine is used by the line discipline to determine if there
1512 * is data waiting to be transmitted/drained/flushed or not.
1514 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1516 struct channel_t *ch = NULL;
1517 struct un_t *un = NULL;
1522 unsigned long flags;
1527 un = tty->driver_data;
1528 if (!un || un->magic != DGNC_UNIT_MAGIC)
1532 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1535 spin_lock_irqsave(&ch->ch_lock, flags);
1538 thead = ch->ch_w_head & tmask;
1539 ttail = ch->ch_w_tail & tmask;
1541 spin_unlock_irqrestore(&ch->ch_lock, flags);
1543 if (ttail == thead) {
1547 chars = thead - ttail;
1549 chars = thead - ttail + WQUEUESIZE;
1559 * Reduces bytes_available to the max number of characters
1560 * that can be sent currently given the maxcps value, and
1561 * returns the new bytes_available. This only affects printer
1564 static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1566 struct channel_t *ch = NULL;
1567 struct un_t *un = NULL;
1570 return bytes_available;
1572 un = tty->driver_data;
1573 if (!un || un->magic != DGNC_UNIT_MAGIC)
1574 return bytes_available;
1577 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1578 return bytes_available;
1581 * If its not the Transparent print device, return
1582 * the full data amount.
1584 if (un->un_type != DGNC_PRINT)
1585 return bytes_available;
1587 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1589 unsigned long current_time = jiffies;
1590 unsigned long buffer_time = current_time +
1591 (HZ * ch->ch_digi.digi_bufsize) / ch->ch_digi.digi_maxcps;
1593 if (ch->ch_cpstime < current_time) {
1594 /* buffer is empty */
1595 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1596 cps_limit = ch->ch_digi.digi_bufsize;
1597 } else if (ch->ch_cpstime < buffer_time) {
1598 /* still room in the buffer */
1599 cps_limit = ((buffer_time - ch->ch_cpstime) * ch->ch_digi.digi_maxcps) / HZ;
1601 /* no room in the buffer */
1605 bytes_available = min(cps_limit, bytes_available);
1608 return bytes_available;
1613 * dgnc_tty_write_room()
1615 * Return space available in Tx buffer
1617 static int dgnc_tty_write_room(struct tty_struct *tty)
1619 struct channel_t *ch = NULL;
1620 struct un_t *un = NULL;
1625 unsigned long flags;
1627 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1630 un = tty->driver_data;
1631 if (!un || un->magic != DGNC_UNIT_MAGIC)
1635 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1638 spin_lock_irqsave(&ch->ch_lock, flags);
1641 head = (ch->ch_w_head) & tmask;
1642 tail = (ch->ch_w_tail) & tmask;
1644 ret = tail - head - 1;
1648 /* Limit printer to maxcps */
1649 ret = dgnc_maxcps_room(tty, ret);
1652 * If we are printer device, leave space for
1653 * possibly both the on and off strings.
1655 if (un->un_type == DGNC_PRINT) {
1656 if (!(ch->ch_flags & CH_PRON))
1657 ret -= ch->ch_digi.digi_onlen;
1658 ret -= ch->ch_digi.digi_offlen;
1660 if (ch->ch_flags & CH_PRON)
1661 ret -= ch->ch_digi.digi_offlen;
1667 spin_unlock_irqrestore(&ch->ch_lock, flags);
1674 * dgnc_tty_put_char()
1676 * Put a character into ch->ch_buf
1678 * - used by the line discipline for OPOST processing
1680 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1683 * Simply call tty_write.
1685 dgnc_tty_write(tty, &c, 1);
1693 * Take data from the user or kernel and send it out to the FEP.
1694 * In here exists all the Transparent Print magic as well.
1696 static int dgnc_tty_write(struct tty_struct *tty,
1697 const unsigned char *buf, int count)
1699 struct channel_t *ch = NULL;
1700 struct un_t *un = NULL;
1701 int bufcount = 0, n = 0;
1703 unsigned long flags;
1710 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1713 un = tty->driver_data;
1714 if (!un || un->magic != DGNC_UNIT_MAGIC)
1718 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1725 * Store original amount of characters passed in.
1726 * This helps to figure out if we should ask the FEP
1727 * to send us an event when it has more space available.
1731 spin_lock_irqsave(&ch->ch_lock, flags);
1733 /* Get our space available for the channel from the board */
1735 head = (ch->ch_w_head) & tmask;
1736 tail = (ch->ch_w_tail) & tmask;
1738 bufcount = tail - head - 1;
1740 bufcount += WQUEUESIZE;
1743 * Limit printer output to maxcps overall, with bursts allowed
1744 * up to bufsize characters.
1746 bufcount = dgnc_maxcps_room(tty, bufcount);
1749 * Take minimum of what the user wants to send, and the
1750 * space available in the FEP buffer.
1752 count = min(count, bufcount);
1755 * Bail if no space left.
1761 * Output the printer ON string, if we are in terminal mode, but
1762 * need to be in printer mode.
1764 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1765 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1766 (int) ch->ch_digi.digi_onlen);
1767 head = (ch->ch_w_head) & tmask;
1768 ch->ch_flags |= CH_PRON;
1772 * On the other hand, output the printer OFF string, if we are
1773 * currently in printer mode, but need to output to the terminal.
1775 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1776 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1777 (int) ch->ch_digi.digi_offlen);
1778 head = (ch->ch_w_head) & tmask;
1779 ch->ch_flags &= ~CH_PRON;
1783 * If there is nothing left to copy, or I can't handle any more data, leave.
1790 count = min(count, WRITEBUFLEN);
1792 spin_unlock_irqrestore(&ch->ch_lock, flags);
1795 * If data is coming from user space, copy it into a temporary
1796 * buffer so we don't get swapped out while doing the copy to
1799 /* we're allowed to block if it's from_user */
1800 if (down_interruptible(&dgnc_TmpWriteSem))
1804 * copy_from_user() returns the number
1805 * of bytes that could *NOT* be copied.
1807 count -= copy_from_user(dgnc_TmpWriteBuf, (const unsigned char __user *) buf, count);
1810 up(&dgnc_TmpWriteSem);
1814 spin_lock_irqsave(&ch->ch_lock, flags);
1816 buf = dgnc_TmpWriteBuf;
1823 * If the write wraps over the top of the circular buffer,
1824 * move the portion up to the wrap point, and reset the
1825 * pointers to the bottom.
1827 remain = WQUEUESIZE - head;
1831 memcpy(ch->ch_wqueue + head, buf, remain);
1838 * Move rest of data.
1841 memcpy(ch->ch_wqueue + head, buf, remain);
1847 ch->ch_w_head = head;
1850 /* Update printer buffer empty time. */
1851 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
1852 && (ch->ch_digi.digi_bufsize > 0)) {
1853 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1857 spin_unlock_irqrestore(&ch->ch_lock, flags);
1858 up(&dgnc_TmpWriteSem);
1860 spin_unlock_irqrestore(&ch->ch_lock, flags);
1865 * Channel lock is grabbed and then released
1866 * inside this routine.
1868 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1875 spin_unlock_irqrestore(&ch->ch_lock, flags);
1881 * Return modem signals to ld.
1884 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1886 struct channel_t *ch;
1889 unsigned char mstat = 0;
1890 unsigned long flags;
1892 if (!tty || tty->magic != TTY_MAGIC)
1895 un = tty->driver_data;
1896 if (!un || un->magic != DGNC_UNIT_MAGIC)
1900 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1903 spin_lock_irqsave(&ch->ch_lock, flags);
1905 mstat = (ch->ch_mostat | ch->ch_mistat);
1907 spin_unlock_irqrestore(&ch->ch_lock, flags);
1911 if (mstat & UART_MCR_DTR)
1912 result |= TIOCM_DTR;
1913 if (mstat & UART_MCR_RTS)
1914 result |= TIOCM_RTS;
1915 if (mstat & UART_MSR_CTS)
1916 result |= TIOCM_CTS;
1917 if (mstat & UART_MSR_DSR)
1918 result |= TIOCM_DSR;
1919 if (mstat & UART_MSR_RI)
1921 if (mstat & UART_MSR_DCD)
1929 * dgnc_tty_tiocmset()
1931 * Set modem signals, called by ld.
1934 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1935 unsigned int set, unsigned int clear)
1937 struct dgnc_board *bd;
1938 struct channel_t *ch;
1941 unsigned long flags;
1943 if (!tty || tty->magic != TTY_MAGIC)
1946 un = tty->driver_data;
1947 if (!un || un->magic != DGNC_UNIT_MAGIC)
1951 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1955 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1958 spin_lock_irqsave(&ch->ch_lock, flags);
1960 if (set & TIOCM_RTS)
1961 ch->ch_mostat |= UART_MCR_RTS;
1963 if (set & TIOCM_DTR)
1964 ch->ch_mostat |= UART_MCR_DTR;
1966 if (clear & TIOCM_RTS)
1967 ch->ch_mostat &= ~(UART_MCR_RTS);
1969 if (clear & TIOCM_DTR)
1970 ch->ch_mostat &= ~(UART_MCR_DTR);
1972 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1974 spin_unlock_irqrestore(&ch->ch_lock, flags);
1981 * dgnc_tty_send_break()
1983 * Send a Break, called by ld.
1985 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
1987 struct dgnc_board *bd;
1988 struct channel_t *ch;
1991 unsigned long flags;
1993 if (!tty || tty->magic != TTY_MAGIC)
1996 un = tty->driver_data;
1997 if (!un || un->magic != DGNC_UNIT_MAGIC)
2001 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2005 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2019 spin_lock_irqsave(&ch->ch_lock, flags);
2021 ch->ch_bd->bd_ops->send_break(ch, msec);
2023 spin_unlock_irqrestore(&ch->ch_lock, flags);
2031 * dgnc_tty_wait_until_sent()
2033 * wait until data has been transmitted, called by ld.
2035 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
2037 struct dgnc_board *bd;
2038 struct channel_t *ch;
2042 if (!tty || tty->magic != TTY_MAGIC)
2045 un = tty->driver_data;
2046 if (!un || un->magic != DGNC_UNIT_MAGIC)
2050 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2054 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2057 rc = bd->bd_ops->drain(tty, 0);
2064 * send a high priority character, called by ld.
2066 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
2068 struct dgnc_board *bd;
2069 struct channel_t *ch;
2071 unsigned long flags;
2073 if (!tty || tty->magic != TTY_MAGIC)
2076 un = tty->driver_data;
2077 if (!un || un->magic != DGNC_UNIT_MAGIC)
2081 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2085 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2088 dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
2090 spin_lock_irqsave(&ch->ch_lock, flags);
2091 bd->bd_ops->send_immediate_char(ch, c);
2092 spin_unlock_irqrestore(&ch->ch_lock, flags);
2094 dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
2101 * Return modem signals to ld.
2103 static inline int dgnc_get_mstat(struct channel_t *ch)
2105 unsigned char mstat;
2107 unsigned long flags;
2109 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2112 spin_lock_irqsave(&ch->ch_lock, flags);
2114 mstat = (ch->ch_mostat | ch->ch_mistat);
2116 spin_unlock_irqrestore(&ch->ch_lock, flags);
2120 if (mstat & UART_MCR_DTR)
2121 result |= TIOCM_DTR;
2122 if (mstat & UART_MCR_RTS)
2123 result |= TIOCM_RTS;
2124 if (mstat & UART_MSR_CTS)
2125 result |= TIOCM_CTS;
2126 if (mstat & UART_MSR_DSR)
2127 result |= TIOCM_DSR;
2128 if (mstat & UART_MSR_RI)
2130 if (mstat & UART_MSR_DCD)
2139 * Return modem signals to ld.
2141 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value)
2145 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2148 result = dgnc_get_mstat(ch);
2153 return put_user(result, value);
2158 * dgnc_set_modem_info()
2160 * Set modem signals, called by ld.
2162 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value)
2164 struct dgnc_board *bd;
2165 struct channel_t *ch;
2168 unsigned int arg = 0;
2169 unsigned long flags;
2171 if (!tty || tty->magic != TTY_MAGIC)
2174 un = tty->driver_data;
2175 if (!un || un->magic != DGNC_UNIT_MAGIC)
2179 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2183 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2186 ret = get_user(arg, value);
2192 if (arg & TIOCM_RTS)
2193 ch->ch_mostat |= UART_MCR_RTS;
2195 if (arg & TIOCM_DTR)
2196 ch->ch_mostat |= UART_MCR_DTR;
2201 if (arg & TIOCM_RTS)
2202 ch->ch_mostat &= ~(UART_MCR_RTS);
2204 if (arg & TIOCM_DTR)
2205 ch->ch_mostat &= ~(UART_MCR_DTR);
2211 if (arg & TIOCM_RTS)
2212 ch->ch_mostat |= UART_MCR_RTS;
2214 ch->ch_mostat &= ~(UART_MCR_RTS);
2216 if (arg & TIOCM_DTR)
2217 ch->ch_mostat |= UART_MCR_DTR;
2219 ch->ch_mostat &= ~(UART_MCR_DTR);
2227 spin_lock_irqsave(&ch->ch_lock, flags);
2229 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2231 spin_unlock_irqrestore(&ch->ch_lock, flags);
2238 * dgnc_tty_digigeta()
2240 * Ioctl to get the information for ditty.
2245 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo)
2247 struct channel_t *ch;
2250 unsigned long flags;
2255 if (!tty || tty->magic != TTY_MAGIC)
2258 un = tty->driver_data;
2259 if (!un || un->magic != DGNC_UNIT_MAGIC)
2263 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2266 memset(&tmp, 0, sizeof(tmp));
2268 spin_lock_irqsave(&ch->ch_lock, flags);
2269 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2270 spin_unlock_irqrestore(&ch->ch_lock, flags);
2272 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2280 * dgnc_tty_digiseta()
2282 * Ioctl to set the information for ditty.
2287 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info)
2289 struct dgnc_board *bd;
2290 struct channel_t *ch;
2292 struct digi_t new_digi;
2293 unsigned long flags;
2295 if (!tty || tty->magic != TTY_MAGIC)
2298 un = tty->driver_data;
2299 if (!un || un->magic != DGNC_UNIT_MAGIC)
2303 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2307 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2310 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
2313 spin_lock_irqsave(&ch->ch_lock, flags);
2316 * Handle transistions to and from RTS Toggle.
2318 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2319 ch->ch_mostat &= ~(UART_MCR_RTS);
2320 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2321 ch->ch_mostat |= (UART_MCR_RTS);
2324 * Handle transistions to and from DTR Toggle.
2326 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2327 ch->ch_mostat &= ~(UART_MCR_DTR);
2328 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2329 ch->ch_mostat |= (UART_MCR_DTR);
2331 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2333 if (ch->ch_digi.digi_maxcps < 1)
2334 ch->ch_digi.digi_maxcps = 1;
2336 if (ch->ch_digi.digi_maxcps > 10000)
2337 ch->ch_digi.digi_maxcps = 10000;
2339 if (ch->ch_digi.digi_bufsize < 10)
2340 ch->ch_digi.digi_bufsize = 10;
2342 if (ch->ch_digi.digi_maxchar < 1)
2343 ch->ch_digi.digi_maxchar = 1;
2345 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2346 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2348 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2349 ch->ch_digi.digi_onlen = DIGI_PLEN;
2351 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2352 ch->ch_digi.digi_offlen = DIGI_PLEN;
2354 ch->ch_bd->bd_ops->param(tty);
2356 spin_unlock_irqrestore(&ch->ch_lock, flags);
2363 * dgnc_set_termios()
2365 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2367 struct dgnc_board *bd;
2368 struct channel_t *ch;
2370 unsigned long flags;
2372 if (!tty || tty->magic != TTY_MAGIC)
2375 un = tty->driver_data;
2376 if (!un || un->magic != DGNC_UNIT_MAGIC)
2380 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2384 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2387 spin_lock_irqsave(&ch->ch_lock, flags);
2389 ch->ch_c_cflag = tty->termios.c_cflag;
2390 ch->ch_c_iflag = tty->termios.c_iflag;
2391 ch->ch_c_oflag = tty->termios.c_oflag;
2392 ch->ch_c_lflag = tty->termios.c_lflag;
2393 ch->ch_startc = tty->termios.c_cc[VSTART];
2394 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2396 ch->ch_bd->bd_ops->param(tty);
2399 spin_unlock_irqrestore(&ch->ch_lock, flags);
2403 static void dgnc_tty_throttle(struct tty_struct *tty)
2405 struct channel_t *ch;
2407 unsigned long flags;
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 spin_lock_irqsave(&ch->ch_lock, flags);
2422 ch->ch_flags |= (CH_FORCED_STOPI);
2424 spin_unlock_irqrestore(&ch->ch_lock, flags);
2428 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2430 struct channel_t *ch;
2432 unsigned long flags;
2434 if (!tty || tty->magic != TTY_MAGIC)
2437 un = tty->driver_data;
2438 if (!un || un->magic != DGNC_UNIT_MAGIC)
2442 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2445 spin_lock_irqsave(&ch->ch_lock, flags);
2447 ch->ch_flags &= ~(CH_FORCED_STOPI);
2449 spin_unlock_irqrestore(&ch->ch_lock, flags);
2453 static void dgnc_tty_start(struct tty_struct *tty)
2455 struct dgnc_board *bd;
2456 struct channel_t *ch;
2458 unsigned long flags;
2460 if (!tty || tty->magic != TTY_MAGIC)
2463 un = tty->driver_data;
2464 if (!un || un->magic != DGNC_UNIT_MAGIC)
2468 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2472 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2475 spin_lock_irqsave(&ch->ch_lock, flags);
2477 ch->ch_flags &= ~(CH_FORCED_STOP);
2479 spin_unlock_irqrestore(&ch->ch_lock, flags);
2483 static void dgnc_tty_stop(struct tty_struct *tty)
2485 struct dgnc_board *bd;
2486 struct channel_t *ch;
2488 unsigned long flags;
2490 if (!tty || tty->magic != TTY_MAGIC)
2493 un = tty->driver_data;
2494 if (!un || un->magic != DGNC_UNIT_MAGIC)
2498 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2502 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2505 spin_lock_irqsave(&ch->ch_lock, flags);
2507 ch->ch_flags |= (CH_FORCED_STOP);
2509 spin_unlock_irqrestore(&ch->ch_lock, flags);
2514 * dgnc_tty_flush_chars()
2516 * Flush the cook buffer
2518 * Note to self, and any other poor souls who venture here:
2520 * flush in this case DOES NOT mean dispose of the data.
2521 * instead, it means "stop buffering and send it if you
2522 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2524 * It is also always called in interrupt context - JAR 8-Sept-99
2526 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2528 struct dgnc_board *bd;
2529 struct channel_t *ch;
2531 unsigned long flags;
2533 if (!tty || tty->magic != TTY_MAGIC)
2536 un = tty->driver_data;
2537 if (!un || un->magic != DGNC_UNIT_MAGIC)
2541 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2545 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2548 spin_lock_irqsave(&ch->ch_lock, flags);
2550 /* Do something maybe here */
2552 spin_unlock_irqrestore(&ch->ch_lock, flags);
2558 * dgnc_tty_flush_buffer()
2560 * Flush Tx buffer (make in == out)
2562 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2564 struct channel_t *ch;
2566 unsigned long flags;
2568 if (!tty || tty->magic != TTY_MAGIC)
2571 un = tty->driver_data;
2572 if (!un || un->magic != DGNC_UNIT_MAGIC)
2576 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2579 spin_lock_irqsave(&ch->ch_lock, flags);
2581 ch->ch_flags &= ~CH_STOP;
2583 /* Flush our write queue */
2584 ch->ch_w_head = ch->ch_w_tail;
2586 /* Flush UARTs transmit FIFO */
2587 ch->ch_bd->bd_ops->flush_uart_write(ch);
2589 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2590 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2591 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2593 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2594 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2595 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2598 spin_unlock_irqrestore(&ch->ch_lock, flags);
2603 /*****************************************************************************
2605 * The IOCTL function and all of its helpers
2607 *****************************************************************************/
2612 * The usual assortment of ioctl's
2614 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2617 struct dgnc_board *bd;
2618 struct channel_t *ch;
2621 unsigned long flags;
2622 void __user *uarg = (void __user *) arg;
2624 if (!tty || tty->magic != TTY_MAGIC)
2627 un = tty->driver_data;
2628 if (!un || un->magic != DGNC_UNIT_MAGIC)
2632 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2636 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2639 spin_lock_irqsave(&ch->ch_lock, flags);
2641 if (un->un_open_count <= 0) {
2642 spin_unlock_irqrestore(&ch->ch_lock, flags);
2648 /* Here are all the standard ioctl's that we MUST implement */
2652 * TCSBRK is SVID version: non-zero arg --> no break
2653 * this behaviour is exploited by tcdrain().
2655 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2656 * between 0.25 and 0.5 seconds so we'll ask for something
2657 * in the middle: 0.375 seconds.
2659 rc = tty_check_change(tty);
2660 spin_unlock_irqrestore(&ch->ch_lock, flags);
2664 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2669 spin_lock_irqsave(&ch->ch_lock, flags);
2671 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2672 ch->ch_bd->bd_ops->send_break(ch, 250);
2674 spin_unlock_irqrestore(&ch->ch_lock, flags);
2680 /* support for POSIX tcsendbreak()
2681 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2682 * between 0.25 and 0.5 seconds so we'll ask for something
2683 * in the middle: 0.375 seconds.
2685 rc = tty_check_change(tty);
2686 spin_unlock_irqrestore(&ch->ch_lock, flags);
2690 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2694 spin_lock_irqsave(&ch->ch_lock, flags);
2696 ch->ch_bd->bd_ops->send_break(ch, 250);
2698 spin_unlock_irqrestore(&ch->ch_lock, flags);
2703 rc = tty_check_change(tty);
2704 spin_unlock_irqrestore(&ch->ch_lock, flags);
2708 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2712 spin_lock_irqsave(&ch->ch_lock, flags);
2714 ch->ch_bd->bd_ops->send_break(ch, 250);
2716 spin_unlock_irqrestore(&ch->ch_lock, flags);
2722 spin_unlock_irqrestore(&ch->ch_lock, flags);
2727 spin_unlock_irqrestore(&ch->ch_lock, flags);
2729 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
2734 spin_unlock_irqrestore(&ch->ch_lock, flags);
2735 rc = get_user(arg, (unsigned long __user *) arg);
2739 spin_lock_irqsave(&ch->ch_lock, flags);
2740 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
2741 ch->ch_bd->bd_ops->param(tty);
2742 spin_unlock_irqrestore(&ch->ch_lock, flags);
2747 spin_unlock_irqrestore(&ch->ch_lock, flags);
2748 return dgnc_get_modem_info(ch, uarg);
2753 spin_unlock_irqrestore(&ch->ch_lock, flags);
2754 return dgnc_set_modem_info(tty, cmd, uarg);
2757 * Here are any additional ioctl's that we want to implement
2762 * The linux tty driver doesn't have a flush
2763 * input routine for the driver, assuming all backed
2764 * up data is in the line disc. buffers. However,
2765 * we all know that's not the case. Here, we
2766 * act on the ioctl, but then lie and say we didn't
2767 * so the line discipline will process the flush
2770 rc = tty_check_change(tty);
2772 spin_unlock_irqrestore(&ch->ch_lock, flags);
2776 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2777 ch->ch_r_head = ch->ch_r_tail;
2778 ch->ch_bd->bd_ops->flush_uart_read(ch);
2779 /* Force queue flow control to be released, if needed */
2780 dgnc_check_queue_flow_control(ch);
2783 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2784 if (!(un->un_type == DGNC_PRINT)) {
2785 ch->ch_w_head = ch->ch_w_tail;
2786 ch->ch_bd->bd_ops->flush_uart_write(ch);
2788 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2789 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2790 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2793 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2794 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2795 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2801 /* pretend we didn't recognize this IOCTL */
2802 spin_unlock_irqrestore(&ch->ch_lock, flags);
2803 return -ENOIOCTLCMD;
2807 * The linux tty driver doesn't have a flush
2808 * input routine for the driver, assuming all backed
2809 * up data is in the line disc. buffers. However,
2810 * we all know that's not the case. Here, we
2811 * act on the ioctl, but then lie and say we didn't
2812 * so the line discipline will process the flush
2815 if (cmd == TCSETSF) {
2817 ch->ch_flags &= ~CH_STOP;
2818 ch->ch_r_head = ch->ch_r_tail;
2819 ch->ch_bd->bd_ops->flush_uart_read(ch);
2820 /* Force queue flow control to be released, if needed */
2821 dgnc_check_queue_flow_control(ch);
2824 /* now wait for all the output to drain */
2825 spin_unlock_irqrestore(&ch->ch_lock, flags);
2826 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2830 /* pretend we didn't recognize this */
2831 return -ENOIOCTLCMD;
2835 spin_unlock_irqrestore(&ch->ch_lock, flags);
2836 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2840 /* pretend we didn't recognize this */
2841 return -ENOIOCTLCMD;
2844 spin_unlock_irqrestore(&ch->ch_lock, flags);
2845 /* Make the ld do it */
2846 return -ENOIOCTLCMD;
2849 /* get information for ditty */
2850 spin_unlock_irqrestore(&ch->ch_lock, flags);
2851 return dgnc_tty_digigeta(tty, uarg);
2856 /* set information for ditty */
2857 if (cmd == (DIGI_SETAW)) {
2859 spin_unlock_irqrestore(&ch->ch_lock, flags);
2860 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2865 spin_lock_irqsave(&ch->ch_lock, flags);
2867 tty_ldisc_flush(tty);
2872 spin_unlock_irqrestore(&ch->ch_lock, flags);
2873 return dgnc_tty_digiseta(tty, uarg);
2878 /* Let go of locks when accessing user space, could sleep */
2879 spin_unlock_irqrestore(&ch->ch_lock, flags);
2880 rc = get_user(loopback, (unsigned int __user *) arg);
2883 spin_lock_irqsave(&ch->ch_lock, flags);
2885 /* Enable/disable internal loopback for this port */
2887 ch->ch_flags |= CH_LOOPBACK;
2889 ch->ch_flags &= ~(CH_LOOPBACK);
2891 ch->ch_bd->bd_ops->param(tty);
2892 spin_unlock_irqrestore(&ch->ch_lock, flags);
2896 case DIGI_GETCUSTOMBAUD:
2897 spin_unlock_irqrestore(&ch->ch_lock, flags);
2898 rc = put_user(ch->ch_custom_speed, (unsigned int __user *) arg);
2901 case DIGI_SETCUSTOMBAUD:
2904 /* Let go of locks when accessing user space, could sleep */
2905 spin_unlock_irqrestore(&ch->ch_lock, flags);
2906 rc = get_user(new_rate, (int __user *) arg);
2909 spin_lock_irqsave(&ch->ch_lock, flags);
2910 dgnc_set_custom_speed(ch, new_rate);
2911 ch->ch_bd->bd_ops->param(tty);
2912 spin_unlock_irqrestore(&ch->ch_lock, flags);
2917 * This ioctl allows insertion of a character into the front
2918 * of any pending data to be transmitted.
2920 * This ioctl is to satify the "Send Character Immediate"
2921 * call that the RealPort protocol spec requires.
2923 case DIGI_REALPORT_SENDIMMEDIATE:
2927 spin_unlock_irqrestore(&ch->ch_lock, flags);
2928 rc = get_user(c, (unsigned char __user *) arg);
2931 spin_lock_irqsave(&ch->ch_lock, flags);
2932 ch->ch_bd->bd_ops->send_immediate_char(ch, c);
2933 spin_unlock_irqrestore(&ch->ch_lock, flags);
2938 * This ioctl returns all the current counts for the port.
2940 * This ioctl is to satify the "Line Error Counters"
2941 * call that the RealPort protocol spec requires.
2943 case DIGI_REALPORT_GETCOUNTERS:
2945 struct digi_getcounter buf;
2947 buf.norun = ch->ch_err_overrun;
2948 buf.noflow = 0; /* The driver doesn't keep this stat */
2949 buf.nframe = ch->ch_err_frame;
2950 buf.nparity = ch->ch_err_parity;
2951 buf.nbreak = ch->ch_err_break;
2952 buf.rbytes = ch->ch_rxcount;
2953 buf.tbytes = ch->ch_txcount;
2955 spin_unlock_irqrestore(&ch->ch_lock, flags);
2957 if (copy_to_user(uarg, &buf, sizeof(buf)))
2964 * This ioctl returns all current events.
2966 * This ioctl is to satify the "Event Reporting"
2967 * call that the RealPort protocol spec requires.
2969 case DIGI_REALPORT_GETEVENTS:
2971 unsigned int events = 0;
2973 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2974 if (ch->ch_flags & CH_BREAK_SENDING)
2976 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP))
2977 events |= (EV_OPU | EV_OPS);
2979 if ((ch->ch_flags & CH_STOPI) || (ch->ch_flags & CH_FORCED_STOPI))
2980 events |= (EV_IPU | EV_IPS);
2982 spin_unlock_irqrestore(&ch->ch_lock, flags);
2983 rc = put_user(events, (unsigned int __user *) arg);
2988 * This ioctl returns TOUT and TIN counters based
2989 * upon the values passed in by the RealPort Server.
2990 * It also passes back whether the UART Transmitter is
2993 case DIGI_REALPORT_GETBUFFERS:
2995 struct digi_getbuffer buf;
2999 spin_unlock_irqrestore(&ch->ch_lock, flags);
3002 * Get data from user first.
3004 if (copy_from_user(&buf, uarg, sizeof(buf)))
3007 spin_lock_irqsave(&ch->ch_lock, flags);
3010 * Figure out how much data is in our RX and TX queues.
3012 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
3013 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
3016 * Is the UART empty? Add that value to whats in our TX queue.
3018 count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
3021 * Figure out how much data the RealPort Server believes should
3022 * be in our TX queue.
3024 tdist = (buf.tIn - buf.tOut) & 0xffff;
3027 * If we have more data than the RealPort Server believes we
3028 * should have, reduce our count to its amount.
3030 * This count difference CAN happen because the Linux LD can
3031 * insert more characters into our queue for OPOST processing
3032 * that the RealPort Server doesn't know about.
3034 if (buf.txbuf > tdist)
3038 * Report whether our queue and UART TX are completely empty.
3045 spin_unlock_irqrestore(&ch->ch_lock, flags);
3047 if (copy_to_user(uarg, &buf, sizeof(buf)))
3053 spin_unlock_irqrestore(&ch->ch_lock, flags);
3055 return -ENOIOCTLCMD;