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.
17 * This file implements the tty driver functionality for the
18 * Neo and ClassicBoard PCI based product lines.
21 #include <linux/kernel.h>
22 #include <linux/sched.h> /* For jiffies, task states */
23 #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
24 #include <linux/module.h>
25 #include <linux/ctype.h>
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
28 #include <linux/types.h>
29 #include <linux/serial_reg.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h> /* For udelay */
32 #include <linux/uaccess.h> /* For copy_from_user/copy_to_user */
33 #include <linux/pci.h>
34 #include "dgnc_driver.h"
38 #include "dgnc_sysfs.h"
39 #include "dgnc_utils.h"
41 /* Default transparent print information. */
43 static const struct digi_t dgnc_digi_init = {
44 .digi_flags = DIGI_COOK, /* Flags */
45 .digi_maxcps = 100, /* Max CPS */
46 .digi_maxchar = 50, /* Max chars in print queue */
47 .digi_bufsize = 100, /* Printer buffer size */
48 .digi_onlen = 4, /* size of printer on string */
49 .digi_offlen = 4, /* size of printer off string */
50 .digi_onstr = "\033[5i", /* ANSI printer on string ] */
51 .digi_offstr = "\033[4i", /* ANSI printer off string ] */
52 .digi_term = "ansi" /* default terminal type */
56 * Define a local default termios struct. All ports will be created
57 * with this termios initially.
59 * This defines a raw port at 9600 baud, 8 data bits, no parity,
62 static struct ktermios default_termios = {
63 .c_iflag = (DEFAULT_IFLAGS), /* iflags */
64 .c_oflag = (DEFAULT_OFLAGS), /* oflags */
65 .c_cflag = (DEFAULT_CFLAGS), /* cflags */
66 .c_lflag = (DEFAULT_LFLAGS), /* lflags */
71 /* Our function prototypes */
72 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
73 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
74 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file,
75 struct channel_t *ch);
76 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
78 static int dgnc_tty_digigeta(struct tty_struct *tty,
79 struct digi_t __user *retinfo);
80 static int dgnc_tty_digiseta(struct tty_struct *tty,
81 struct digi_t __user *new_info);
82 static int dgnc_tty_write_room(struct tty_struct *tty);
83 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
84 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
85 static void dgnc_tty_start(struct tty_struct *tty);
86 static void dgnc_tty_stop(struct tty_struct *tty);
87 static void dgnc_tty_throttle(struct tty_struct *tty);
88 static void dgnc_tty_unthrottle(struct tty_struct *tty);
89 static void dgnc_tty_flush_chars(struct tty_struct *tty);
90 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
91 static void dgnc_tty_hangup(struct tty_struct *tty);
92 static int dgnc_set_modem_info(struct channel_t *ch, unsigned int command,
93 unsigned int __user *value);
94 static int dgnc_get_modem_info(struct channel_t *ch,
95 unsigned int __user *value);
96 static int dgnc_tty_tiocmget(struct tty_struct *tty);
97 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set,
99 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
100 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
101 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf,
103 static void dgnc_tty_set_termios(struct tty_struct *tty,
104 struct ktermios *old_termios);
105 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
107 static const struct tty_operations dgnc_tty_ops = {
108 .open = dgnc_tty_open,
109 .close = dgnc_tty_close,
110 .write = dgnc_tty_write,
111 .write_room = dgnc_tty_write_room,
112 .flush_buffer = dgnc_tty_flush_buffer,
113 .chars_in_buffer = dgnc_tty_chars_in_buffer,
114 .flush_chars = dgnc_tty_flush_chars,
115 .ioctl = dgnc_tty_ioctl,
116 .set_termios = dgnc_tty_set_termios,
117 .stop = dgnc_tty_stop,
118 .start = dgnc_tty_start,
119 .throttle = dgnc_tty_throttle,
120 .unthrottle = dgnc_tty_unthrottle,
121 .hangup = dgnc_tty_hangup,
122 .put_char = dgnc_tty_put_char,
123 .tiocmget = dgnc_tty_tiocmget,
124 .tiocmset = dgnc_tty_tiocmset,
125 .break_ctl = dgnc_tty_send_break,
126 .wait_until_sent = dgnc_tty_wait_until_sent,
127 .send_xchar = dgnc_tty_send_xchar
130 /* TTY Initialization/Cleanup Functions */
133 * dgnc_tty_register()
135 * Init the tty subsystem for this board.
137 int dgnc_tty_register(struct dgnc_board *brd)
141 brd->serial_driver = tty_alloc_driver(brd->maxports,
142 TTY_DRIVER_REAL_RAW |
143 TTY_DRIVER_DYNAMIC_DEV |
144 TTY_DRIVER_HARDWARE_BREAK);
146 if (IS_ERR(brd->serial_driver))
147 return PTR_ERR(brd->serial_driver);
149 snprintf(brd->serial_name, MAXTTYNAMELEN, "tty_dgnc_%d_",
152 brd->serial_driver->name = brd->serial_name;
153 brd->serial_driver->name_base = 0;
154 brd->serial_driver->major = 0;
155 brd->serial_driver->minor_start = 0;
156 brd->serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
157 brd->serial_driver->subtype = SERIAL_TYPE_NORMAL;
158 brd->serial_driver->init_termios = default_termios;
159 brd->serial_driver->driver_name = DRVSTR;
162 * Entry points for driver. Called by the kernel from
163 * tty_io.c and n_tty.c.
165 tty_set_operations(brd->serial_driver, &dgnc_tty_ops);
167 rc = tty_register_driver(brd->serial_driver);
169 dev_dbg(&brd->pdev->dev,
170 "Can't register tty device (%d)\n", rc);
171 goto free_serial_driver;
175 * If we're doing transparent print, we have to do all of the above
176 * again, separately so we don't get the LD confused about what major
177 * we are when we get into the dgnc_tty_open() routine.
179 brd->print_driver = tty_alloc_driver(brd->maxports,
180 TTY_DRIVER_REAL_RAW |
181 TTY_DRIVER_DYNAMIC_DEV |
182 TTY_DRIVER_HARDWARE_BREAK);
184 if (IS_ERR(brd->print_driver)) {
185 rc = PTR_ERR(brd->print_driver);
186 goto unregister_serial_driver;
189 snprintf(brd->print_name, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
191 brd->print_driver->name = brd->print_name;
192 brd->print_driver->name_base = 0;
193 brd->print_driver->major = brd->serial_driver->major;
194 brd->print_driver->minor_start = 0x80;
195 brd->print_driver->type = TTY_DRIVER_TYPE_SERIAL;
196 brd->print_driver->subtype = SERIAL_TYPE_NORMAL;
197 brd->print_driver->init_termios = default_termios;
198 brd->print_driver->driver_name = DRVSTR;
201 * Entry points for driver. Called by the kernel from
202 * tty_io.c and n_tty.c.
204 tty_set_operations(brd->print_driver, &dgnc_tty_ops);
206 rc = tty_register_driver(brd->print_driver);
208 dev_dbg(&brd->pdev->dev,
209 "Can't register Transparent Print device(%d)\n",
211 goto free_print_driver;
217 put_tty_driver(brd->print_driver);
218 unregister_serial_driver:
219 tty_unregister_driver(brd->serial_driver);
221 put_tty_driver(brd->serial_driver);
226 void dgnc_tty_unregister(struct dgnc_board *brd)
228 tty_unregister_driver(brd->print_driver);
229 tty_unregister_driver(brd->serial_driver);
230 put_tty_driver(brd->print_driver);
231 put_tty_driver(brd->serial_driver);
237 * Init the tty subsystem. Called once per board after board has been
238 * downloaded and init'ed.
240 int dgnc_tty_init(struct dgnc_board *brd)
244 struct channel_t *ch;
249 /* Initialize board structure elements. */
251 vaddr = brd->re_map_membase;
253 brd->nasync = brd->maxports;
255 for (i = 0; i < brd->nasync; i++) {
257 * Okay to malloc with GFP_KERNEL, we are not at
258 * interrupt context, and there are no locks held.
260 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]),
262 if (!brd->channels[i])
263 goto err_free_channels;
266 ch = brd->channels[0];
267 vaddr = brd->re_map_membase;
269 /* Set up channel variables */
270 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
271 spin_lock_init(&ch->ch_lock);
273 /* Store all our magic numbers */
274 ch->magic = DGNC_CHANNEL_MAGIC;
275 ch->ch_tun.magic = DGNC_UNIT_MAGIC;
276 ch->ch_tun.un_ch = ch;
277 ch->ch_tun.un_type = DGNC_SERIAL;
278 ch->ch_tun.un_dev = i;
280 ch->ch_pun.magic = DGNC_UNIT_MAGIC;
281 ch->ch_pun.un_ch = ch;
282 ch->ch_pun.un_type = DGNC_PRINT;
283 ch->ch_pun.un_dev = i + 128;
285 if (brd->bd_uart_offset == 0x200)
286 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
288 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
292 ch->ch_digi = dgnc_digi_init;
294 /* .25 second delay */
295 ch->ch_close_delay = 250;
297 init_waitqueue_head(&ch->ch_flags_wait);
298 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
299 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
302 struct device *classp;
304 classp = tty_register_device(brd->serial_driver, i,
305 &ch->ch_bd->pdev->dev);
306 ch->ch_tun.un_sysfs = classp;
307 dgnc_create_tty_sysfs(&ch->ch_tun, classp);
309 classp = tty_register_device(brd->print_driver, i,
310 &ch->ch_bd->pdev->dev);
311 ch->ch_pun.un_sysfs = classp;
312 dgnc_create_tty_sysfs(&ch->ch_pun, classp);
319 for (i = i - 1; i >= 0; --i) {
320 kfree(brd->channels[i]);
321 brd->channels[i] = NULL;
329 * Uninitialize the TTY portion of this driver. Free all memory and
332 void dgnc_cleanup_tty(struct dgnc_board *brd)
336 for (i = 0; i < brd->nasync; i++) {
337 if (brd->channels[i])
338 dgnc_remove_tty_sysfs(brd->channels[i]->
340 tty_unregister_device(brd->serial_driver, i);
342 tty_unregister_driver(brd->serial_driver);
344 for (i = 0; i < brd->nasync; i++) {
345 if (brd->channels[i])
346 dgnc_remove_tty_sysfs(brd->channels[i]->
348 tty_unregister_device(brd->print_driver, i);
350 tty_unregister_driver(brd->print_driver);
352 put_tty_driver(brd->serial_driver);
353 put_tty_driver(brd->print_driver);
357 * dgnc_wmove - Write data to transmit queue.
359 * ch - Pointer to channel structure.
360 * buf - Pointer to characters to be moved.
361 * n - Number of characters to move.
363 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
368 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
371 head = ch->ch_w_head & WQUEUEMASK;
374 * If the write wraps over the top of the circular buffer,
375 * move the portion up to the wrap point, and reset the
376 * pointers to the bottom.
378 remain = WQUEUESIZE - head;
382 memcpy(ch->ch_wqueue + head, buf, remain);
389 /* Move rest of data. */
392 memcpy(ch->ch_wqueue + head, buf, remain);
397 ch->ch_w_head = head;
401 * dgnc_input - Process received data.
403 * ch - Pointer to channel structure.
405 void dgnc_input(struct channel_t *ch)
407 struct dgnc_board *bd;
408 struct tty_struct *tp;
409 struct tty_ldisc *ld = NULL;
421 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
424 tp = ch->ch_tun.un_tty;
427 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
430 spin_lock_irqsave(&ch->ch_lock, flags);
433 * Figure the number of characters in the buffer.
434 * Exit immediately if none.
437 head = ch->ch_r_head & rmask;
438 tail = ch->ch_r_tail & rmask;
439 data_len = (head - tail) & rmask;
445 * If the device is not open, or CREAD is off,
446 * flush input data and return immediately.
448 if (!tp || (tp->magic != TTY_MAGIC) ||
449 !(ch->ch_tun.un_flags & UN_ISOPEN) ||
451 (ch->ch_tun.un_flags & UN_CLOSING)) {
452 ch->ch_r_head = tail;
454 /* Force queue flow control to be released, if needed */
455 dgnc_check_queue_flow_control(ch);
460 /* If we are throttled, simply don't read any data. */
462 if (ch->ch_flags & CH_FORCED_STOPI)
465 flip_len = TTY_FLIPBUF_SIZE;
467 /* Chop down the length, if needed */
468 len = min(data_len, flip_len);
469 len = min(len, (N_TTY_BUF_SIZE - 1));
471 ld = tty_ldisc_ref(tp);
474 * If we were unable to get a reference to the ld,
475 * don't flush our buffer, and act like the ld doesn't
476 * have any space to put the data right now.
482 * If ld doesn't have a pointer to a receive_buf function,
483 * flush the data, then act like the ld doesn't have any
484 * space to put the data right now.
486 if (!ld->ops->receive_buf) {
487 ch->ch_r_head = ch->ch_r_tail;
496 * The tty layer in the kernel has changed in 2.6.16+.
498 * The flip buffers in the tty structure are no longer exposed,
499 * and probably will be going away eventually.
501 * If we are completely raw, we don't need to go through a lot
502 * of the tty layers that exist.
503 * In this case, we take the shortest and fastest route we
504 * can to relay the data to the user.
506 * On the other hand, if we are not raw, we need to go through
507 * the new 2.6.16+ tty layer, which has its API more well defined.
509 len = tty_buffer_request_room(tp->port, len);
513 * n now contains the most amount of data we can copy,
514 * bounded either by how much the Linux tty layer can handle,
515 * or the amount of data the card actually has pending...
518 unsigned char *ch_pos = ch->ch_equeue + tail;
520 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
527 * If conditions are such that ld needs to see all
528 * UART errors, we will have to walk each character
529 * and error byte and send them to the buffer one at
532 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
533 for (i = 0; i < s; i++) {
534 unsigned char ch = *(ch_pos + i);
535 char flag = TTY_NORMAL;
537 if (ch & UART_LSR_BI)
539 else if (ch & UART_LSR_PE)
541 else if (ch & UART_LSR_FE)
544 tty_insert_flip_char(tp->port, ch, flag);
547 tty_insert_flip_string(tp->port, ch_pos, s);
552 /* Flip queue if needed */
556 ch->ch_r_tail = tail & rmask;
557 ch->ch_e_tail = tail & rmask;
558 dgnc_check_queue_flow_control(ch);
559 spin_unlock_irqrestore(&ch->ch_lock, flags);
561 /* Tell the tty layer its okay to "eat" the data now */
562 tty_flip_buffer_push(tp->port);
569 spin_unlock_irqrestore(&ch->ch_lock, flags);
575 * Determines when CARRIER changes state and takes appropriate
578 void dgnc_carrier(struct channel_t *ch)
580 int virt_carrier = 0;
581 int phys_carrier = 0;
583 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
586 if (ch->ch_mistat & UART_MSR_DCD)
589 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
592 if (ch->ch_c_cflag & CLOCAL)
595 /* Test for a VIRTUAL carrier transition to HIGH. */
597 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
599 * When carrier rises, wake any threads waiting
600 * for carrier in the open routine.
602 if (waitqueue_active(&ch->ch_flags_wait))
603 wake_up_interruptible(&ch->ch_flags_wait);
606 /* Test for a PHYSICAL carrier transition to HIGH. */
608 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
610 * When carrier rises, wake any threads waiting
611 * for carrier in the open routine.
613 if (waitqueue_active(&ch->ch_flags_wait))
614 wake_up_interruptible(&ch->ch_flags_wait);
618 * Test for a PHYSICAL transition to low, so long as we aren't
619 * currently ignoring physical transitions (which is what "virtual
620 * carrier" indicates).
622 * The transition of the virtual carrier to low really doesn't
623 * matter... it really only means "ignore carrier state", not
624 * "make pretend that carrier is there".
626 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
627 (phys_carrier == 0)) {
629 * When carrier drops:
631 * Drop carrier on all open units.
633 * Flush queues, waking up any task waiting in the
636 * Send a hangup to the control terminal.
638 * Enable all select calls.
640 if (waitqueue_active(&ch->ch_flags_wait))
641 wake_up_interruptible(&ch->ch_flags_wait);
643 if (ch->ch_tun.un_open_count > 0)
644 tty_hangup(ch->ch_tun.un_tty);
646 if (ch->ch_pun.un_open_count > 0)
647 tty_hangup(ch->ch_pun.un_tty);
650 /* Make sure that our cached values reflect the current reality. */
652 if (virt_carrier == 1)
653 ch->ch_flags |= CH_FCAR;
655 ch->ch_flags &= ~CH_FCAR;
657 if (phys_carrier == 1)
658 ch->ch_flags |= CH_CD;
660 ch->ch_flags &= ~CH_CD;
663 /* Assign the custom baud rate to the channel structure */
665 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
674 ch->ch_custom_speed = 0;
679 * Since the divisor is stored in a 16-bit integer, we make sure
680 * we don't allow any rates smaller than a 16-bit integer would allow.
681 * And of course, rates above the dividend won't fly.
683 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
684 newrate = (ch->ch_bd->bd_dividend / 0xFFFF) + 1;
686 if (newrate && newrate > ch->ch_bd->bd_dividend)
687 newrate = ch->ch_bd->bd_dividend;
690 testdiv = ch->ch_bd->bd_dividend / newrate;
693 * If we try to figure out what rate the board would use
694 * with the test divisor, it will be either equal or higher
695 * than the requested baud rate. If we then determine the
696 * rate with a divisor one higher, we will get the next lower
697 * supported rate below the requested.
699 testrate_high = ch->ch_bd->bd_dividend / testdiv;
700 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
703 * If the rate for the requested divisor is correct, just
704 * use it and be done.
706 if (testrate_high != newrate) {
708 * Otherwise, pick the rate that is closer
709 * (i.e. whichever rate has a smaller delta).
711 deltahigh = testrate_high - newrate;
712 deltalow = newrate - testrate_low;
714 if (deltahigh < deltalow)
715 newrate = testrate_high;
717 newrate = testrate_low;
721 ch->ch_custom_speed = newrate;
724 void dgnc_check_queue_flow_control(struct channel_t *ch)
728 /* Store how much space we have left in the queue */
729 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
731 qleft += RQUEUEMASK + 1;
734 * Check to see if we should enforce flow control on our queue because
735 * the ld (or user) isn't reading data out of our queue fast enuf.
737 * NOTE: This is done based on what the current flow control of the
740 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
741 * This will cause the UART's FIFO to back up, and force
742 * the RTS signal to be dropped.
743 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
744 * the other side, in hopes it will stop sending data to us.
745 * 3) NONE - Nothing we can do. We will simply drop any extra data
746 * that gets sent into us when the queue fills up.
750 if (ch->ch_digi.digi_flags & CTSPACE ||
751 ch->ch_c_cflag & CRTSCTS) {
752 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
753 ch->ch_bd->bd_ops->disable_receiver(ch);
754 ch->ch_flags |= (CH_RECEIVER_OFF);
758 else if (ch->ch_c_iflag & IXOFF) {
759 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
760 ch->ch_bd->bd_ops->send_stop_character(ch);
767 * Check to see if we should unenforce flow control because
768 * ld (or user) finally read enuf data out of our queue.
770 * NOTE: This is done based on what the current flow control of the
773 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
774 * This will cause the UART's FIFO to raise RTS back up,
775 * which will allow the other side to start sending data again.
776 * 2) SWFLOW (IXOFF) - Send a start character to
777 * the other side, so it will start sending data to us again.
778 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
779 * other side, we don't need to do anything now.
781 if (qleft > (RQUEUESIZE / 2)) {
783 if (ch->ch_digi.digi_flags & RTSPACE ||
784 ch->ch_c_cflag & CRTSCTS) {
785 if (ch->ch_flags & CH_RECEIVER_OFF) {
786 ch->ch_bd->bd_ops->enable_receiver(ch);
787 ch->ch_flags &= ~(CH_RECEIVER_OFF);
791 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
792 ch->ch_stops_sent = 0;
793 ch->ch_bd->bd_ops->send_start_character(ch);
798 void dgnc_wakeup_writes(struct channel_t *ch)
803 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
806 spin_lock_irqsave(&ch->ch_lock, flags);
808 /* If channel now has space, wake up anyone waiting on the condition. */
810 qlen = ch->ch_w_head - ch->ch_w_tail;
814 if (qlen >= (WQUEUESIZE - 256)) {
815 spin_unlock_irqrestore(&ch->ch_lock, flags);
819 if (ch->ch_tun.un_flags & UN_ISOPEN) {
820 tty_wakeup(ch->ch_tun.un_tty);
823 * If unit is set to wait until empty, check to make sure
824 * the queue AND FIFO are both empty.
826 if (ch->ch_tun.un_flags & UN_EMPTY) {
828 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
829 ch->ch_tun.un_flags &= ~(UN_EMPTY);
832 * If RTS Toggle mode is on, whenever
833 * the queue and UART is empty, keep RTS low.
835 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
836 ch->ch_mostat &= ~(UART_MCR_RTS);
837 ch->ch_bd->bd_ops->assert_modem_signals(ch);
841 * If DTR Toggle mode is on, whenever
842 * the queue and UART is empty, keep DTR low.
844 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
845 ch->ch_mostat &= ~(UART_MCR_DTR);
846 ch->ch_bd->bd_ops->assert_modem_signals(ch);
851 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
854 if (ch->ch_pun.un_flags & UN_ISOPEN) {
855 tty_wakeup(ch->ch_pun.un_tty);
858 * If unit is set to wait until empty, check to make sure
859 * the queue AND FIFO are both empty.
861 if (ch->ch_pun.un_flags & UN_EMPTY) {
863 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
864 ch->ch_pun.un_flags &= ~(UN_EMPTY);
867 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
870 spin_unlock_irqrestore(&ch->ch_lock, flags);
873 static struct dgnc_board *find_board_by_major(unsigned int major)
877 for (i = 0; i < MAXBOARDS; i++) {
878 struct dgnc_board *brd = dgnc_board[i];
883 if (major == brd->serial_driver->major ||
884 major == brd->print_driver->major)
891 /* TTY Entry points and helper functions */
893 /* dgnc_tty_open() */
895 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
897 struct dgnc_board *brd;
898 struct channel_t *ch;
907 major = MAJOR(tty_devnum(tty));
908 minor = MINOR(tty_devnum(tty));
913 /* Get board pointer from our array of majors we have allocated */
914 brd = find_board_by_major(major);
919 * If board is not yet up to a state of READY, go to
920 * sleep waiting for it to happen or they cancel the open.
922 rc = wait_event_interruptible(brd->state_wait,
923 (brd->state & BOARD_READY));
928 spin_lock_irqsave(&brd->bd_lock, flags);
930 /* If opened device is greater than our number of ports, bail. */
931 if (PORT_NUM(minor) >= brd->nasync) {
932 spin_unlock_irqrestore(&brd->bd_lock, flags);
936 ch = brd->channels[PORT_NUM(minor)];
938 spin_unlock_irqrestore(&brd->bd_lock, flags);
942 /* Drop board lock */
943 spin_unlock_irqrestore(&brd->bd_lock, flags);
945 /* Grab channel lock */
946 spin_lock_irqsave(&ch->ch_lock, flags);
948 /* Figure out our type */
949 if (!IS_PRINT(minor)) {
950 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
951 un->un_type = DGNC_SERIAL;
952 } else if (IS_PRINT(minor)) {
953 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
954 un->un_type = DGNC_PRINT;
956 spin_unlock_irqrestore(&ch->ch_lock, flags);
961 * If the port is still in a previous open, and in a state
962 * where we simply cannot safely keep going, wait until the
965 spin_unlock_irqrestore(&ch->ch_lock, flags);
967 rc = wait_event_interruptible(ch->ch_flags_wait,
968 ((ch->ch_flags & CH_OPENING) == 0));
970 /* If ret is non-zero, user ctrl-c'ed us */
975 * If either unit is in the middle of the fragile part of close,
976 * we just cannot touch the channel safely.
977 * Go to sleep, knowing that when the channel can be
978 * touched safely, the close routine will signal the
979 * ch_flags_wait to wake us back up.
981 rc = wait_event_interruptible(ch->ch_flags_wait, (((ch->ch_tun.un_flags |
982 ch->ch_pun.un_flags) & UN_CLOSING) == 0));
984 /* If ret is non-zero, user ctrl-c'ed us */
988 spin_lock_irqsave(&ch->ch_lock, flags);
990 /* Store our unit into driver_data, so we always have it available. */
991 tty->driver_data = un;
993 /* Initialize tty's */
995 if (!(un->un_flags & UN_ISOPEN)) {
996 /* Store important variables. */
999 /* Maybe do something here to the TTY struct as well? */
1003 * Allocate channel buffers for read/write/error.
1004 * Set flag, so we don't get trounced on.
1006 ch->ch_flags |= (CH_OPENING);
1008 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1009 spin_unlock_irqrestore(&ch->ch_lock, flags);
1012 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1014 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1016 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1018 if (!ch->ch_rqueue || !ch->ch_equeue || !ch->ch_wqueue) {
1019 kfree(ch->ch_rqueue);
1020 kfree(ch->ch_equeue);
1021 kfree(ch->ch_wqueue);
1026 spin_lock_irqsave(&ch->ch_lock, flags);
1028 ch->ch_flags &= ~(CH_OPENING);
1029 wake_up_interruptible(&ch->ch_flags_wait);
1031 /* Initialize if neither terminal or printer is open. */
1033 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1035 /* Flush input queues. */
1044 brd->bd_ops->flush_uart_write(ch);
1045 brd->bd_ops->flush_uart_read(ch);
1048 ch->ch_cached_lsr = 0;
1049 ch->ch_stop_sending_break = 0;
1050 ch->ch_stops_sent = 0;
1052 ch->ch_c_cflag = tty->termios.c_cflag;
1053 ch->ch_c_iflag = tty->termios.c_iflag;
1054 ch->ch_c_oflag = tty->termios.c_oflag;
1055 ch->ch_c_lflag = tty->termios.c_lflag;
1056 ch->ch_startc = tty->termios.c_cc[VSTART];
1057 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1060 * Bring up RTS and DTR...
1061 * Also handle RTS or DTR toggle if set.
1063 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1064 ch->ch_mostat |= (UART_MCR_RTS);
1065 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1066 ch->ch_mostat |= (UART_MCR_DTR);
1068 /* Tell UART to init itself */
1069 brd->bd_ops->uart_init(ch);
1072 /* Run param in case we changed anything */
1074 brd->bd_ops->param(tty);
1078 /* follow protocol for opening port */
1080 spin_unlock_irqrestore(&ch->ch_lock, flags);
1082 rc = dgnc_block_til_ready(tty, file, ch);
1084 /* No going back now, increment our unit and channel counters */
1085 spin_lock_irqsave(&ch->ch_lock, flags);
1086 ch->ch_open_count++;
1087 un->un_open_count++;
1088 un->un_flags |= (UN_ISOPEN);
1089 spin_unlock_irqrestore(&ch->ch_lock, flags);
1095 * dgnc_block_til_ready()
1097 * Wait for DCD, if needed.
1099 static int dgnc_block_til_ready(struct tty_struct *tty,
1101 struct channel_t *ch)
1104 struct un_t *un = tty->driver_data;
1105 unsigned long flags;
1107 int sleep_on_un_flags = 0;
1112 spin_lock_irqsave(&ch->ch_lock, flags);
1118 sleep_on_un_flags = 0;
1121 * If board has failed somehow during our sleep,
1124 if (ch->ch_bd->state == BOARD_FAILED) {
1129 /* If tty was hung up, break out of loop and set error. */
1130 if (tty_hung_up_p(file)) {
1136 * If either unit is in the middle of the fragile part of close,
1137 * we just cannot touch the channel safely.
1138 * Go back to sleep, knowing that when the channel can be
1139 * touched safely, the close routine will signal the
1140 * ch_wait_flags to wake us back up.
1142 if (!((ch->ch_tun.un_flags |
1143 ch->ch_pun.un_flags) &
1146 * Our conditions to leave cleanly and happily:
1147 * 1) NONBLOCKING on the tty is set.
1149 * 3) DCD (fake or real) is active.
1152 if (file->f_flags & O_NONBLOCK)
1155 if (tty_io_error(tty)) {
1160 if (ch->ch_flags & CH_CD)
1163 if (ch->ch_flags & CH_FCAR)
1166 sleep_on_un_flags = 1;
1170 * If there is a signal pending, the user probably
1171 * interrupted (ctrl-c) us.
1172 * Leave loop with error set.
1174 if (signal_pending(current)) {
1175 retval = -ERESTARTSYS;
1179 /* Store the flags before we let go of channel lock */
1181 if (sleep_on_un_flags)
1182 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1184 old_flags = ch->ch_flags;
1187 * Let go of channel lock before calling schedule.
1188 * Our poller will get any FEP events and wake us up when DCD
1189 * eventually goes active.
1192 spin_unlock_irqrestore(&ch->ch_lock, flags);
1195 * Wait for something in the flags to change
1196 * from the current value.
1198 if (sleep_on_un_flags)
1199 retval = wait_event_interruptible
1200 (un->un_flags_wait, (old_flags != (ch->ch_tun.un_flags |
1201 ch->ch_pun.un_flags)));
1203 retval = wait_event_interruptible(ch->ch_flags_wait,
1204 (old_flags != ch->ch_flags));
1207 * We got woken up for some reason.
1208 * Before looping around, grab our channel lock.
1210 spin_lock_irqsave(&ch->ch_lock, flags);
1215 spin_unlock_irqrestore(&ch->ch_lock, flags);
1223 * Hangup the port. Like a close, but don't wait for output to drain.
1225 static void dgnc_tty_hangup(struct tty_struct *tty)
1227 if (!tty || tty->magic != TTY_MAGIC)
1230 /* flush the transmit queues */
1231 dgnc_tty_flush_buffer(tty);
1234 /* dgnc_tty_close() */
1236 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1238 struct dgnc_board *bd;
1239 struct channel_t *ch;
1241 unsigned long flags;
1243 if (!tty || tty->magic != TTY_MAGIC)
1246 un = tty->driver_data;
1247 if (!un || un->magic != DGNC_UNIT_MAGIC)
1251 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1255 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1258 spin_lock_irqsave(&ch->ch_lock, flags);
1261 * Determine if this is the last close or not - and if we agree about
1262 * which type of close it is with the Line Discipline
1264 if ((tty->count == 1) && (un->un_open_count != 1)) {
1266 * Uh, oh. tty->count is 1, which means that the tty
1267 * structure will be freed. un_open_count should always
1268 * be one in these conditions. If it's greater than
1269 * one, we've got real problems, since it means the
1270 * serial port won't be shutdown.
1273 "tty->count is 1, un open count is %d\n",
1275 un->un_open_count = 1;
1278 if (un->un_open_count)
1279 un->un_open_count--;
1282 "bad serial port open count of %d\n",
1285 ch->ch_open_count--;
1287 if (ch->ch_open_count && un->un_open_count) {
1288 spin_unlock_irqrestore(&ch->ch_lock, flags);
1292 /* OK, its the last close on the unit */
1293 un->un_flags |= UN_CLOSING;
1298 * Only officially close channel if count is 0 and
1299 * DIGI_PRINTER bit is not set.
1301 if ((ch->ch_open_count == 0) &&
1302 !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1303 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1305 /* turn off print device when closing print device. */
1307 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1308 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1309 (int)ch->ch_digi.digi_offlen);
1310 ch->ch_flags &= ~CH_PRON;
1313 spin_unlock_irqrestore(&ch->ch_lock, flags);
1314 /* wait for output to drain */
1315 /* This will also return if we take an interrupt */
1317 bd->bd_ops->drain(tty, 0);
1319 dgnc_tty_flush_buffer(tty);
1320 tty_ldisc_flush(tty);
1322 spin_lock_irqsave(&ch->ch_lock, flags);
1326 /* If we have HUPCL set, lower DTR and RTS */
1328 if (ch->ch_c_cflag & HUPCL) {
1330 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1331 bd->bd_ops->assert_modem_signals(ch);
1334 * Go to sleep to ensure RTS/DTR
1335 * have been dropped for modems to see it.
1337 if (ch->ch_close_delay) {
1338 spin_unlock_irqrestore(&ch->ch_lock,
1340 dgnc_ms_sleep(ch->ch_close_delay);
1341 spin_lock_irqsave(&ch->ch_lock, flags);
1345 ch->ch_old_baud = 0;
1347 /* Turn off UART interrupts for this port */
1348 ch->ch_bd->bd_ops->uart_off(ch);
1350 /* turn off print device when closing print device. */
1352 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1353 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1354 (int)ch->ch_digi.digi_offlen);
1355 ch->ch_flags &= ~CH_PRON;
1360 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1362 wake_up_interruptible(&ch->ch_flags_wait);
1363 wake_up_interruptible(&un->un_flags_wait);
1365 spin_unlock_irqrestore(&ch->ch_lock, flags);
1369 * dgnc_tty_chars_in_buffer()
1371 * Return number of characters that have not been transmitted yet.
1373 * This routine is used by the line discipline to determine if there
1374 * is data waiting to be transmitted/drained/flushed or not.
1376 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1378 struct channel_t *ch = NULL;
1379 struct un_t *un = NULL;
1384 unsigned long flags;
1389 un = tty->driver_data;
1390 if (!un || un->magic != DGNC_UNIT_MAGIC)
1394 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1397 spin_lock_irqsave(&ch->ch_lock, flags);
1400 thead = ch->ch_w_head & tmask;
1401 ttail = ch->ch_w_tail & tmask;
1403 spin_unlock_irqrestore(&ch->ch_lock, flags);
1405 if (ttail == thead) {
1409 chars = thead - ttail;
1411 chars = thead - ttail + WQUEUESIZE;
1420 * Reduces bytes_available to the max number of characters
1421 * that can be sent currently given the maxcps value, and
1422 * returns the new bytes_available. This only affects printer
1425 static int dgnc_maxcps_room(struct channel_t *ch, int bytes_available)
1427 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1429 unsigned long current_time = jiffies;
1430 unsigned long buffer_time = current_time +
1431 (HZ * ch->ch_digi.digi_bufsize) /
1432 ch->ch_digi.digi_maxcps;
1434 if (ch->ch_cpstime < current_time) {
1435 /* buffer is empty */
1436 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1437 cps_limit = ch->ch_digi.digi_bufsize;
1438 } else if (ch->ch_cpstime < buffer_time) {
1439 /* still room in the buffer */
1440 cps_limit = ((buffer_time - ch->ch_cpstime) *
1441 ch->ch_digi.digi_maxcps) / HZ;
1443 /* no room in the buffer */
1447 bytes_available = min(cps_limit, bytes_available);
1450 return bytes_available;
1454 * dgnc_tty_write_room()
1456 * Return space available in Tx buffer
1458 static int dgnc_tty_write_room(struct tty_struct *tty)
1460 struct channel_t *ch = NULL;
1461 struct un_t *un = NULL;
1466 unsigned long flags;
1471 un = tty->driver_data;
1472 if (!un || un->magic != DGNC_UNIT_MAGIC)
1476 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1479 spin_lock_irqsave(&ch->ch_lock, flags);
1482 head = (ch->ch_w_head) & tmask;
1483 tail = (ch->ch_w_tail) & tmask;
1485 ret = tail - head - 1;
1489 /* Limit printer to maxcps */
1490 if (un->un_type != DGNC_PRINT)
1491 ret = dgnc_maxcps_room(ch, ret);
1494 * If we are printer device, leave space for
1495 * possibly both the on and off strings.
1497 if (un->un_type == DGNC_PRINT) {
1498 if (!(ch->ch_flags & CH_PRON))
1499 ret -= ch->ch_digi.digi_onlen;
1500 ret -= ch->ch_digi.digi_offlen;
1502 if (ch->ch_flags & CH_PRON)
1503 ret -= ch->ch_digi.digi_offlen;
1509 spin_unlock_irqrestore(&ch->ch_lock, flags);
1515 * dgnc_tty_put_char()
1517 * Put a character into ch->ch_buf
1519 * - used by the line discipline for OPOST processing
1521 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1523 /* Simply call tty_write. */
1525 dgnc_tty_write(tty, &c, 1);
1532 * Take data from the user or kernel and send it out to the FEP.
1533 * In here exists all the Transparent Print magic as well.
1535 static int dgnc_tty_write(struct tty_struct *tty,
1536 const unsigned char *buf, int count)
1538 struct channel_t *ch = NULL;
1539 struct un_t *un = NULL;
1540 int bufcount = 0, n = 0;
1541 unsigned long flags;
1550 un = tty->driver_data;
1551 if (!un || un->magic != DGNC_UNIT_MAGIC)
1555 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1562 * Store original amount of characters passed in.
1563 * This helps to figure out if we should ask the FEP
1564 * to send us an event when it has more space available.
1567 spin_lock_irqsave(&ch->ch_lock, flags);
1569 /* Get our space available for the channel from the board */
1571 head = (ch->ch_w_head) & tmask;
1572 tail = (ch->ch_w_tail) & tmask;
1574 bufcount = tail - head - 1;
1576 bufcount += WQUEUESIZE;
1579 * Limit printer output to maxcps overall, with bursts allowed
1580 * up to bufsize characters.
1582 if (un->un_type != DGNC_PRINT)
1583 bufcount = dgnc_maxcps_room(ch, bufcount);
1586 * Take minimum of what the user wants to send, and the
1587 * space available in the FEP buffer.
1589 count = min(count, bufcount);
1591 /* Bail if no space left. */
1597 * Output the printer ON string, if we are in terminal mode, but
1598 * need to be in printer mode.
1600 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1601 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1602 (int)ch->ch_digi.digi_onlen);
1603 head = (ch->ch_w_head) & tmask;
1604 ch->ch_flags |= CH_PRON;
1608 * On the other hand, output the printer OFF string, if we are
1609 * currently in printer mode, but need to output to the terminal.
1611 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1612 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1613 (int)ch->ch_digi.digi_offlen);
1614 head = (ch->ch_w_head) & tmask;
1615 ch->ch_flags &= ~CH_PRON;
1621 * If the write wraps over the top of the circular buffer,
1622 * move the portion up to the wrap point, and reset the
1623 * pointers to the bottom.
1625 remain = WQUEUESIZE - head;
1629 memcpy(ch->ch_wqueue + head, buf, remain);
1636 /* Move rest of data. */
1639 memcpy(ch->ch_wqueue + head, buf, remain);
1645 ch->ch_w_head = head;
1648 /* Update printer buffer empty time. */
1649 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0) &&
1650 (ch->ch_digi.digi_bufsize > 0)) {
1651 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1654 spin_unlock_irqrestore(&ch->ch_lock, flags);
1658 * Channel lock is grabbed and then released
1659 * inside this routine.
1661 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1668 spin_unlock_irqrestore(&ch->ch_lock, flags);
1672 /* Return modem signals to ld. */
1674 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1676 struct channel_t *ch;
1679 unsigned char mstat = 0;
1680 unsigned long flags;
1682 if (!tty || tty->magic != TTY_MAGIC)
1685 un = tty->driver_data;
1686 if (!un || un->magic != DGNC_UNIT_MAGIC)
1690 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1693 spin_lock_irqsave(&ch->ch_lock, flags);
1695 mstat = ch->ch_mostat | ch->ch_mistat;
1697 spin_unlock_irqrestore(&ch->ch_lock, flags);
1701 if (mstat & UART_MCR_DTR)
1702 result |= TIOCM_DTR;
1703 if (mstat & UART_MCR_RTS)
1704 result |= TIOCM_RTS;
1705 if (mstat & UART_MSR_CTS)
1706 result |= TIOCM_CTS;
1707 if (mstat & UART_MSR_DSR)
1708 result |= TIOCM_DSR;
1709 if (mstat & UART_MSR_RI)
1711 if (mstat & UART_MSR_DCD)
1718 * dgnc_tty_tiocmset()
1720 * Set modem signals, called by ld.
1723 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1724 unsigned int set, unsigned int clear)
1726 struct dgnc_board *bd;
1727 struct channel_t *ch;
1730 unsigned long flags;
1732 if (!tty || tty->magic != TTY_MAGIC)
1735 un = tty->driver_data;
1736 if (!un || un->magic != DGNC_UNIT_MAGIC)
1740 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1744 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1747 spin_lock_irqsave(&ch->ch_lock, flags);
1749 if (set & TIOCM_RTS)
1750 ch->ch_mostat |= UART_MCR_RTS;
1752 if (set & TIOCM_DTR)
1753 ch->ch_mostat |= UART_MCR_DTR;
1755 if (clear & TIOCM_RTS)
1756 ch->ch_mostat &= ~(UART_MCR_RTS);
1758 if (clear & TIOCM_DTR)
1759 ch->ch_mostat &= ~(UART_MCR_DTR);
1761 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1763 spin_unlock_irqrestore(&ch->ch_lock, flags);
1769 * dgnc_tty_send_break()
1771 * Send a Break, called by ld.
1773 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
1775 struct dgnc_board *bd;
1776 struct channel_t *ch;
1779 unsigned long flags;
1781 if (!tty || tty->magic != TTY_MAGIC)
1784 un = tty->driver_data;
1785 if (!un || un->magic != DGNC_UNIT_MAGIC)
1789 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1793 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1807 spin_lock_irqsave(&ch->ch_lock, flags);
1809 ch->ch_bd->bd_ops->send_break(ch, msec);
1811 spin_unlock_irqrestore(&ch->ch_lock, flags);
1817 * dgnc_tty_wait_until_sent()
1819 * wait until data has been transmitted, called by ld.
1821 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1823 struct dgnc_board *bd;
1824 struct channel_t *ch;
1827 if (!tty || tty->magic != TTY_MAGIC)
1830 un = tty->driver_data;
1831 if (!un || un->magic != DGNC_UNIT_MAGIC)
1835 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1839 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1842 bd->bd_ops->drain(tty, 0);
1848 * send a high priority character, called by ld.
1850 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
1852 struct dgnc_board *bd;
1853 struct channel_t *ch;
1855 unsigned long flags;
1857 if (!tty || tty->magic != TTY_MAGIC)
1860 un = tty->driver_data;
1861 if (!un || un->magic != DGNC_UNIT_MAGIC)
1865 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1869 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1872 dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
1874 spin_lock_irqsave(&ch->ch_lock, flags);
1875 bd->bd_ops->send_immediate_char(ch, c);
1876 spin_unlock_irqrestore(&ch->ch_lock, flags);
1878 dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
1881 /* Return modem signals to ld. */
1883 static inline int dgnc_get_mstat(struct channel_t *ch)
1885 unsigned char mstat;
1887 unsigned long flags;
1889 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1892 spin_lock_irqsave(&ch->ch_lock, flags);
1894 mstat = ch->ch_mostat | ch->ch_mistat;
1896 spin_unlock_irqrestore(&ch->ch_lock, flags);
1898 if (mstat & UART_MCR_DTR)
1899 result |= TIOCM_DTR;
1900 if (mstat & UART_MCR_RTS)
1901 result |= TIOCM_RTS;
1902 if (mstat & UART_MSR_CTS)
1903 result |= TIOCM_CTS;
1904 if (mstat & UART_MSR_DSR)
1905 result |= TIOCM_DSR;
1906 if (mstat & UART_MSR_RI)
1908 if (mstat & UART_MSR_DCD)
1914 /* Return modem signals to ld. */
1916 static int dgnc_get_modem_info(struct channel_t *ch,
1917 unsigned int __user *value)
1919 return put_user(dgnc_get_mstat(ch), value);
1923 * dgnc_set_modem_info()
1925 * Set modem signals, called by ld.
1927 static int dgnc_set_modem_info(struct channel_t *ch,
1928 unsigned int command,
1929 unsigned int __user *value)
1932 unsigned int arg = 0;
1933 unsigned long flags;
1935 ret = get_user(arg, value);
1941 if (arg & TIOCM_RTS)
1942 ch->ch_mostat |= UART_MCR_RTS;
1944 if (arg & TIOCM_DTR)
1945 ch->ch_mostat |= UART_MCR_DTR;
1950 if (arg & TIOCM_RTS)
1951 ch->ch_mostat &= ~(UART_MCR_RTS);
1953 if (arg & TIOCM_DTR)
1954 ch->ch_mostat &= ~(UART_MCR_DTR);
1960 if (arg & TIOCM_RTS)
1961 ch->ch_mostat |= UART_MCR_RTS;
1963 ch->ch_mostat &= ~(UART_MCR_RTS);
1965 if (arg & TIOCM_DTR)
1966 ch->ch_mostat |= UART_MCR_DTR;
1968 ch->ch_mostat &= ~(UART_MCR_DTR);
1976 spin_lock_irqsave(&ch->ch_lock, flags);
1978 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1980 spin_unlock_irqrestore(&ch->ch_lock, flags);
1986 * dgnc_tty_digigeta()
1988 * Ioctl to get the information for ditty.
1990 static int dgnc_tty_digigeta(struct tty_struct *tty,
1991 struct digi_t __user *retinfo)
1993 struct channel_t *ch;
1996 unsigned long flags;
2001 if (!tty || tty->magic != TTY_MAGIC)
2004 un = tty->driver_data;
2005 if (!un || un->magic != DGNC_UNIT_MAGIC)
2009 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2012 memset(&tmp, 0, sizeof(tmp));
2014 spin_lock_irqsave(&ch->ch_lock, flags);
2015 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2016 spin_unlock_irqrestore(&ch->ch_lock, flags);
2018 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2025 * dgnc_tty_digiseta()
2027 * Ioctl to set the information for ditty.
2029 static int dgnc_tty_digiseta(struct tty_struct *tty,
2030 struct digi_t __user *new_info)
2032 struct dgnc_board *bd;
2033 struct channel_t *ch;
2035 struct digi_t new_digi;
2036 unsigned long flags;
2038 if (!tty || tty->magic != TTY_MAGIC)
2041 un = tty->driver_data;
2042 if (!un || un->magic != DGNC_UNIT_MAGIC)
2046 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2050 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2053 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
2056 spin_lock_irqsave(&ch->ch_lock, flags);
2058 /* Handle transitions to and from RTS Toggle. */
2060 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2061 (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2062 ch->ch_mostat &= ~(UART_MCR_RTS);
2063 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2064 !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2065 ch->ch_mostat |= (UART_MCR_RTS);
2067 /* Handle transitions to and from DTR Toggle. */
2069 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2070 (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2071 ch->ch_mostat &= ~(UART_MCR_DTR);
2072 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2073 !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2074 ch->ch_mostat |= (UART_MCR_DTR);
2076 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2078 if (ch->ch_digi.digi_maxcps < 1)
2079 ch->ch_digi.digi_maxcps = 1;
2081 if (ch->ch_digi.digi_maxcps > 10000)
2082 ch->ch_digi.digi_maxcps = 10000;
2084 if (ch->ch_digi.digi_bufsize < 10)
2085 ch->ch_digi.digi_bufsize = 10;
2087 if (ch->ch_digi.digi_maxchar < 1)
2088 ch->ch_digi.digi_maxchar = 1;
2090 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2091 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2093 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2094 ch->ch_digi.digi_onlen = DIGI_PLEN;
2096 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2097 ch->ch_digi.digi_offlen = DIGI_PLEN;
2099 ch->ch_bd->bd_ops->param(tty);
2101 spin_unlock_irqrestore(&ch->ch_lock, flags);
2106 /* dgnc_set_termios() */
2108 static void dgnc_tty_set_termios(struct tty_struct *tty,
2109 struct ktermios *old_termios)
2111 struct dgnc_board *bd;
2112 struct channel_t *ch;
2114 unsigned long flags;
2116 if (!tty || tty->magic != TTY_MAGIC)
2119 un = tty->driver_data;
2120 if (!un || un->magic != DGNC_UNIT_MAGIC)
2124 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2128 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2131 spin_lock_irqsave(&ch->ch_lock, flags);
2133 ch->ch_c_cflag = tty->termios.c_cflag;
2134 ch->ch_c_iflag = tty->termios.c_iflag;
2135 ch->ch_c_oflag = tty->termios.c_oflag;
2136 ch->ch_c_lflag = tty->termios.c_lflag;
2137 ch->ch_startc = tty->termios.c_cc[VSTART];
2138 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2140 ch->ch_bd->bd_ops->param(tty);
2143 spin_unlock_irqrestore(&ch->ch_lock, flags);
2146 static void dgnc_tty_throttle(struct tty_struct *tty)
2148 struct channel_t *ch;
2150 unsigned long flags;
2152 if (!tty || tty->magic != TTY_MAGIC)
2155 un = tty->driver_data;
2156 if (!un || un->magic != DGNC_UNIT_MAGIC)
2160 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2163 spin_lock_irqsave(&ch->ch_lock, flags);
2165 ch->ch_flags |= (CH_FORCED_STOPI);
2167 spin_unlock_irqrestore(&ch->ch_lock, flags);
2170 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2172 struct channel_t *ch;
2174 unsigned long flags;
2176 if (!tty || tty->magic != TTY_MAGIC)
2179 un = tty->driver_data;
2180 if (!un || un->magic != DGNC_UNIT_MAGIC)
2184 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2187 spin_lock_irqsave(&ch->ch_lock, flags);
2189 ch->ch_flags &= ~(CH_FORCED_STOPI);
2191 spin_unlock_irqrestore(&ch->ch_lock, flags);
2194 static void dgnc_tty_start(struct tty_struct *tty)
2196 struct dgnc_board *bd;
2197 struct channel_t *ch;
2199 unsigned long flags;
2201 if (!tty || tty->magic != TTY_MAGIC)
2204 un = tty->driver_data;
2205 if (!un || un->magic != DGNC_UNIT_MAGIC)
2209 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2213 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2216 spin_lock_irqsave(&ch->ch_lock, flags);
2218 ch->ch_flags &= ~(CH_FORCED_STOP);
2220 spin_unlock_irqrestore(&ch->ch_lock, flags);
2223 static void dgnc_tty_stop(struct tty_struct *tty)
2225 struct dgnc_board *bd;
2226 struct channel_t *ch;
2228 unsigned long flags;
2230 if (!tty || tty->magic != TTY_MAGIC)
2233 un = tty->driver_data;
2234 if (!un || un->magic != DGNC_UNIT_MAGIC)
2238 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2242 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2245 spin_lock_irqsave(&ch->ch_lock, flags);
2247 ch->ch_flags |= (CH_FORCED_STOP);
2249 spin_unlock_irqrestore(&ch->ch_lock, flags);
2253 * dgnc_tty_flush_chars()
2255 * Flush the cook buffer
2257 * Note to self, and any other poor souls who venture here:
2259 * flush in this case DOES NOT mean dispose of the data.
2260 * instead, it means "stop buffering and send it if you
2261 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2263 * It is also always called in interrupt context - JAR 8-Sept-99
2265 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2267 struct dgnc_board *bd;
2268 struct channel_t *ch;
2270 unsigned long flags;
2272 if (!tty || tty->magic != TTY_MAGIC)
2275 un = tty->driver_data;
2276 if (!un || un->magic != DGNC_UNIT_MAGIC)
2280 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2284 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2287 spin_lock_irqsave(&ch->ch_lock, flags);
2289 /* Do something maybe here */
2291 spin_unlock_irqrestore(&ch->ch_lock, flags);
2295 * dgnc_tty_flush_buffer()
2297 * Flush Tx buffer (make in == out)
2299 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2301 struct channel_t *ch;
2303 unsigned long flags;
2305 if (!tty || tty->magic != TTY_MAGIC)
2308 un = tty->driver_data;
2309 if (!un || un->magic != DGNC_UNIT_MAGIC)
2313 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2316 spin_lock_irqsave(&ch->ch_lock, flags);
2318 ch->ch_flags &= ~CH_STOP;
2320 /* Flush our write queue */
2321 ch->ch_w_head = ch->ch_w_tail;
2323 /* Flush UARTs transmit FIFO */
2324 ch->ch_bd->bd_ops->flush_uart_write(ch);
2326 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
2327 ch->ch_tun.un_flags &= ~(UN_LOW | UN_EMPTY);
2328 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2330 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
2331 ch->ch_pun.un_flags &= ~(UN_LOW | UN_EMPTY);
2332 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2335 spin_unlock_irqrestore(&ch->ch_lock, flags);
2338 /* The IOCTL function and all of its helpers */
2343 * The usual assortment of ioctl's
2345 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2348 struct dgnc_board *bd;
2349 struct board_ops *ch_bd_ops;
2350 struct channel_t *ch;
2353 unsigned long flags;
2354 void __user *uarg = (void __user *)arg;
2356 if (!tty || tty->magic != TTY_MAGIC)
2359 un = tty->driver_data;
2360 if (!un || un->magic != DGNC_UNIT_MAGIC)
2364 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2368 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2371 ch_bd_ops = bd->bd_ops;
2373 spin_lock_irqsave(&ch->ch_lock, flags);
2375 if (un->un_open_count <= 0) {
2376 spin_unlock_irqrestore(&ch->ch_lock, flags);
2381 /* Here are all the standard ioctl's that we MUST implement */
2385 * TCSBRK is SVID version: non-zero arg --> no break
2386 * this behaviour is exploited by tcdrain().
2388 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2389 * between 0.25 and 0.5 seconds so we'll ask for something
2390 * in the middle: 0.375 seconds.
2392 rc = tty_check_change(tty);
2393 spin_unlock_irqrestore(&ch->ch_lock, flags);
2397 rc = ch_bd_ops->drain(tty, 0);
2402 spin_lock_irqsave(&ch->ch_lock, flags);
2404 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2405 ch_bd_ops->send_break(ch, 250);
2407 spin_unlock_irqrestore(&ch->ch_lock, flags);
2413 * support for POSIX tcsendbreak()
2414 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2415 * between 0.25 and 0.5 seconds so we'll ask for something
2416 * in the middle: 0.375 seconds.
2418 rc = tty_check_change(tty);
2419 spin_unlock_irqrestore(&ch->ch_lock, flags);
2423 rc = ch_bd_ops->drain(tty, 0);
2427 spin_lock_irqsave(&ch->ch_lock, flags);
2429 ch_bd_ops->send_break(ch, 250);
2431 spin_unlock_irqrestore(&ch->ch_lock, flags);
2436 rc = tty_check_change(tty);
2437 spin_unlock_irqrestore(&ch->ch_lock, flags);
2441 rc = ch_bd_ops->drain(tty, 0);
2445 spin_lock_irqsave(&ch->ch_lock, flags);
2447 ch_bd_ops->send_break(ch, 250);
2449 spin_unlock_irqrestore(&ch->ch_lock, flags);
2455 spin_unlock_irqrestore(&ch->ch_lock, flags);
2460 spin_unlock_irqrestore(&ch->ch_lock, flags);
2462 return put_user(C_CLOCAL(tty) ? 1 : 0,
2463 (unsigned long __user *)arg);
2467 spin_unlock_irqrestore(&ch->ch_lock, flags);
2468 rc = get_user(arg, (unsigned long __user *)arg);
2472 spin_lock_irqsave(&ch->ch_lock, flags);
2473 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
2474 (arg ? CLOCAL : 0));
2475 ch_bd_ops->param(tty);
2476 spin_unlock_irqrestore(&ch->ch_lock, flags);
2481 spin_unlock_irqrestore(&ch->ch_lock, flags);
2482 return dgnc_get_modem_info(ch, uarg);
2487 spin_unlock_irqrestore(&ch->ch_lock, flags);
2488 return dgnc_set_modem_info(ch, cmd, uarg);
2490 /* Here are any additional ioctl's that we want to implement */
2494 * The linux tty driver doesn't have a flush
2495 * input routine for the driver, assuming all backed
2496 * up data is in the line disc. buffers. However,
2497 * we all know that's not the case. Here, we
2498 * act on the ioctl, but then lie and say we didn't
2499 * so the line discipline will process the flush
2502 rc = tty_check_change(tty);
2504 spin_unlock_irqrestore(&ch->ch_lock, flags);
2508 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2509 ch->ch_r_head = ch->ch_r_tail;
2510 ch_bd_ops->flush_uart_read(ch);
2511 /* Force queue flow control to be released, if needed */
2512 dgnc_check_queue_flow_control(ch);
2515 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2516 if (!(un->un_type == DGNC_PRINT)) {
2517 ch->ch_w_head = ch->ch_w_tail;
2518 ch_bd_ops->flush_uart_write(ch);
2520 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
2521 ch->ch_tun.un_flags &=
2522 ~(UN_LOW | UN_EMPTY);
2523 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2526 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
2527 ch->ch_pun.un_flags &=
2528 ~(UN_LOW | UN_EMPTY);
2529 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2534 /* pretend we didn't recognize this IOCTL */
2535 spin_unlock_irqrestore(&ch->ch_lock, flags);
2536 return -ENOIOCTLCMD;
2540 * The linux tty driver doesn't have a flush
2541 * input routine for the driver, assuming all backed
2542 * up data is in the line disc. buffers. However,
2543 * we all know that's not the case. Here, we
2544 * act on the ioctl, but then lie and say we didn't
2545 * so the line discipline will process the flush
2548 if (cmd == TCSETSF) {
2550 ch->ch_flags &= ~CH_STOP;
2551 ch->ch_r_head = ch->ch_r_tail;
2552 ch_bd_ops->flush_uart_read(ch);
2553 /* Force queue flow control to be released, if needed */
2554 dgnc_check_queue_flow_control(ch);
2557 /* now wait for all the output to drain */
2558 spin_unlock_irqrestore(&ch->ch_lock, flags);
2559 rc = ch_bd_ops->drain(tty, 0);
2563 /* pretend we didn't recognize this */
2564 return -ENOIOCTLCMD;
2568 spin_unlock_irqrestore(&ch->ch_lock, flags);
2569 rc = ch_bd_ops->drain(tty, 0);
2573 /* pretend we didn't recognize this */
2574 return -ENOIOCTLCMD;
2577 spin_unlock_irqrestore(&ch->ch_lock, flags);
2578 /* Make the ld do it */
2579 return -ENOIOCTLCMD;
2582 /* get information for ditty */
2583 spin_unlock_irqrestore(&ch->ch_lock, flags);
2584 return dgnc_tty_digigeta(tty, uarg);
2589 /* set information for ditty */
2590 if (cmd == (DIGI_SETAW)) {
2591 spin_unlock_irqrestore(&ch->ch_lock, flags);
2592 rc = ch_bd_ops->drain(tty, 0);
2597 spin_lock_irqsave(&ch->ch_lock, flags);
2599 tty_ldisc_flush(tty);
2604 spin_unlock_irqrestore(&ch->ch_lock, flags);
2605 return dgnc_tty_digiseta(tty, uarg);
2611 * Let go of locks when accessing user space,
2614 spin_unlock_irqrestore(&ch->ch_lock, flags);
2615 rc = get_user(loopback, (unsigned int __user *)arg);
2618 spin_lock_irqsave(&ch->ch_lock, flags);
2620 /* Enable/disable internal loopback for this port */
2622 ch->ch_flags |= CH_LOOPBACK;
2624 ch->ch_flags &= ~(CH_LOOPBACK);
2626 ch_bd_ops->param(tty);
2627 spin_unlock_irqrestore(&ch->ch_lock, flags);
2631 case DIGI_GETCUSTOMBAUD:
2632 spin_unlock_irqrestore(&ch->ch_lock, flags);
2633 return put_user(ch->ch_custom_speed,
2634 (unsigned int __user *)arg);
2636 case DIGI_SETCUSTOMBAUD:
2639 /* Let go of locks when accessing user space, could sleep */
2640 spin_unlock_irqrestore(&ch->ch_lock, flags);
2641 rc = get_user(new_rate, (int __user *)arg);
2644 spin_lock_irqsave(&ch->ch_lock, flags);
2645 dgnc_set_custom_speed(ch, new_rate);
2646 ch_bd_ops->param(tty);
2647 spin_unlock_irqrestore(&ch->ch_lock, flags);
2652 * This ioctl allows insertion of a character into the front
2653 * of any pending data to be transmitted.
2655 * This ioctl is to satisfy the "Send Character Immediate"
2656 * call that the RealPort protocol spec requires.
2658 case DIGI_REALPORT_SENDIMMEDIATE:
2662 spin_unlock_irqrestore(&ch->ch_lock, flags);
2663 rc = get_user(c, (unsigned char __user *)arg);
2666 spin_lock_irqsave(&ch->ch_lock, flags);
2667 ch_bd_ops->send_immediate_char(ch, c);
2668 spin_unlock_irqrestore(&ch->ch_lock, flags);
2673 * This ioctl returns all the current counts for the port.
2675 * This ioctl is to satisfy the "Line Error Counters"
2676 * call that the RealPort protocol spec requires.
2678 case DIGI_REALPORT_GETCOUNTERS:
2680 struct digi_getcounter buf;
2682 buf.norun = ch->ch_err_overrun;
2683 buf.noflow = 0; /* The driver doesn't keep this stat */
2684 buf.nframe = ch->ch_err_frame;
2685 buf.nparity = ch->ch_err_parity;
2686 buf.nbreak = ch->ch_err_break;
2687 buf.rbytes = ch->ch_rxcount;
2688 buf.tbytes = ch->ch_txcount;
2690 spin_unlock_irqrestore(&ch->ch_lock, flags);
2692 if (copy_to_user(uarg, &buf, sizeof(buf)))
2699 * This ioctl returns all current events.
2701 * This ioctl is to satisfy the "Event Reporting"
2702 * call that the RealPort protocol spec requires.
2704 case DIGI_REALPORT_GETEVENTS:
2706 unsigned int events = 0;
2708 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2709 if (ch->ch_flags & CH_BREAK_SENDING)
2711 if ((ch->ch_flags & CH_STOP) ||
2712 (ch->ch_flags & CH_FORCED_STOP))
2713 events |= (EV_OPU | EV_OPS);
2715 if ((ch->ch_flags & CH_STOPI) ||
2716 (ch->ch_flags & CH_FORCED_STOPI))
2717 events |= (EV_IPU | EV_IPS);
2719 spin_unlock_irqrestore(&ch->ch_lock, flags);
2720 return put_user(events, (unsigned int __user *)arg);
2724 * This ioctl returns TOUT and TIN counters based
2725 * upon the values passed in by the RealPort Server.
2726 * It also passes back whether the UART Transmitter is
2729 case DIGI_REALPORT_GETBUFFERS:
2731 struct digi_getbuffer buf;
2735 spin_unlock_irqrestore(&ch->ch_lock, flags);
2737 /* Get data from user first. */
2739 if (copy_from_user(&buf, uarg, sizeof(buf)))
2742 spin_lock_irqsave(&ch->ch_lock, flags);
2744 /* Figure out how much data is in our RX and TX queues. */
2746 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
2747 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
2749 /* Is the UART empty? Add that value to whats in our TX queue. */
2751 count = buf.txbuf + ch_bd_ops->get_uart_bytes_left(ch);
2754 * Figure out how much data the RealPort Server believes should
2755 * be in our TX queue.
2757 tdist = (buf.tx_in - buf.tx_out) & 0xffff;
2760 * If we have more data than the RealPort Server believes we
2761 * should have, reduce our count to its amount.
2763 * This count difference CAN happen because the Linux LD can
2764 * insert more characters into our queue for OPOST processing
2765 * that the RealPort Server doesn't know about.
2767 if (buf.txbuf > tdist)
2770 /* Report whether our queue and UART TX are completely empty. */
2777 spin_unlock_irqrestore(&ch->ch_lock, flags);
2779 if (copy_to_user(uarg, &buf, sizeof(buf)))
2785 spin_unlock_irqrestore(&ch->ch_lock, flags);
2787 return -ENOIOCTLCMD;