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/signal.h> /* For jiffies, task states, etc. */
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_utils.h"
40 /* Default transparent print information. */
42 static const struct digi_t dgnc_digi_init = {
43 .digi_flags = DIGI_COOK, /* Flags */
44 .digi_maxcps = 100, /* Max CPS */
45 .digi_maxchar = 50, /* Max chars in print queue */
46 .digi_bufsize = 100, /* Printer buffer size */
47 .digi_onlen = 4, /* size of printer on string */
48 .digi_offlen = 4, /* size of printer off string */
49 .digi_onstr = "\033[5i", /* ANSI printer on string ] */
50 .digi_offstr = "\033[4i", /* ANSI printer off string ] */
51 .digi_term = "ansi" /* default terminal type */
54 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
55 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
56 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file,
57 struct channel_t *ch);
58 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
60 static int dgnc_tty_digigeta(struct tty_struct *tty,
61 struct digi_t __user *retinfo);
62 static int dgnc_tty_digiseta(struct tty_struct *tty,
63 struct digi_t __user *new_info);
64 static int dgnc_tty_write_room(struct tty_struct *tty);
65 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
66 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
67 static void dgnc_tty_start(struct tty_struct *tty);
68 static void dgnc_tty_stop(struct tty_struct *tty);
69 static void dgnc_tty_throttle(struct tty_struct *tty);
70 static void dgnc_tty_unthrottle(struct tty_struct *tty);
71 static void dgnc_tty_flush_chars(struct tty_struct *tty);
72 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
73 static void dgnc_tty_hangup(struct tty_struct *tty);
74 static int dgnc_set_modem_info(struct channel_t *ch, unsigned int command,
75 unsigned int __user *value);
76 static int dgnc_get_modem_info(struct channel_t *ch,
77 unsigned int __user *value);
78 static int dgnc_tty_tiocmget(struct tty_struct *tty);
79 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set,
81 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
82 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
83 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf,
85 static void dgnc_tty_set_termios(struct tty_struct *tty,
86 struct ktermios *old_termios);
87 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
88 static void dgnc_set_signal_low(struct channel_t *ch, const unsigned char line);
89 static void dgnc_wake_up_unit(struct un_t *unit);
91 static const struct tty_operations dgnc_tty_ops = {
92 .open = dgnc_tty_open,
93 .close = dgnc_tty_close,
94 .write = dgnc_tty_write,
95 .write_room = dgnc_tty_write_room,
96 .flush_buffer = dgnc_tty_flush_buffer,
97 .chars_in_buffer = dgnc_tty_chars_in_buffer,
98 .flush_chars = dgnc_tty_flush_chars,
99 .ioctl = dgnc_tty_ioctl,
100 .set_termios = dgnc_tty_set_termios,
101 .stop = dgnc_tty_stop,
102 .start = dgnc_tty_start,
103 .throttle = dgnc_tty_throttle,
104 .unthrottle = dgnc_tty_unthrottle,
105 .hangup = dgnc_tty_hangup,
106 .put_char = dgnc_tty_put_char,
107 .tiocmget = dgnc_tty_tiocmget,
108 .tiocmset = dgnc_tty_tiocmset,
109 .break_ctl = dgnc_tty_send_break,
110 .wait_until_sent = dgnc_tty_wait_until_sent,
111 .send_xchar = dgnc_tty_send_xchar
114 /* TTY Initialization/Cleanup Functions */
116 static struct tty_driver *dgnc_tty_create(char *serial_name, uint maxports,
117 int major, int minor)
120 struct tty_driver *drv;
122 drv = tty_alloc_driver(maxports,
123 TTY_DRIVER_REAL_RAW |
124 TTY_DRIVER_DYNAMIC_DEV |
125 TTY_DRIVER_HARDWARE_BREAK);
129 drv->name = serial_name;
132 drv->minor_start = minor;
133 drv->type = TTY_DRIVER_TYPE_SERIAL;
134 drv->subtype = SERIAL_TYPE_NORMAL;
135 drv->init_termios = tty_std_termios;
136 drv->init_termios.c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL);
137 drv->init_termios.c_ispeed = 9600;
138 drv->init_termios.c_ospeed = 9600;
139 drv->driver_name = DRVSTR;
141 * Entry points for driver. Called by the kernel from
142 * tty_io.c and n_tty.c.
144 tty_set_operations(drv, &dgnc_tty_ops);
145 rc = tty_register_driver(drv);
153 static void dgnc_tty_free(struct tty_driver *drv)
155 tty_unregister_driver(drv);
160 * dgnc_tty_register() - Init the tty subsystem for this board.
162 int dgnc_tty_register(struct dgnc_board *brd)
166 snprintf(brd->serial_name, MAXTTYNAMELEN, "tty_dgnc_%d_",
169 brd->serial_driver = dgnc_tty_create(brd->serial_name,
170 brd->maxports, 0, 0);
171 if (IS_ERR(brd->serial_driver)) {
172 rc = PTR_ERR(brd->serial_driver);
173 dev_dbg(&brd->pdev->dev, "Can't register tty device (%d)\n",
178 snprintf(brd->print_name, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
179 brd->print_driver = dgnc_tty_create(brd->print_name, brd->maxports,
181 brd->serial_driver->major);
182 if (IS_ERR(brd->print_driver)) {
183 rc = PTR_ERR(brd->print_driver);
184 dev_dbg(&brd->pdev->dev,
185 "Can't register Transparent Print device(%d)\n", rc);
186 dgnc_tty_free(brd->serial_driver);
192 void dgnc_tty_unregister(struct dgnc_board *brd)
194 dgnc_tty_free(brd->print_driver);
195 dgnc_tty_free(brd->serial_driver);
199 * dgnc_tty_init() - Initialize the tty subsystem.
201 * Called once per board after board has been downloaded and initialized.
203 int dgnc_tty_init(struct dgnc_board *brd)
208 struct channel_t *ch;
213 /* Initialize board structure elements. */
215 vaddr = brd->re_map_membase;
217 brd->nasync = brd->maxports;
219 for (i = 0; i < brd->nasync; i++) {
220 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]),
222 if (!brd->channels[i]) {
224 goto err_free_channels;
228 ch = brd->channels[0];
229 vaddr = brd->re_map_membase;
231 /* Set up channel variables */
232 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
233 spin_lock_init(&ch->ch_lock);
235 ch->ch_tun.un_ch = ch;
236 ch->ch_tun.un_type = DGNC_SERIAL;
237 ch->ch_tun.un_dev = i;
239 ch->ch_pun.un_ch = ch;
240 ch->ch_pun.un_type = DGNC_PRINT;
241 ch->ch_pun.un_dev = i + 128;
243 if (brd->bd_uart_offset == 0x200)
244 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
246 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
250 ch->ch_digi = dgnc_digi_init;
252 /* .25 second delay */
253 ch->ch_close_delay = 250;
255 init_waitqueue_head(&ch->ch_flags_wait);
256 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
257 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
260 struct device *classp;
262 classp = tty_register_device(brd->serial_driver, i,
263 &ch->ch_bd->pdev->dev);
264 ch->ch_tun.un_sysfs = classp;
266 classp = tty_register_device(brd->print_driver, i,
267 &ch->ch_bd->pdev->dev);
268 ch->ch_pun.un_sysfs = classp;
275 for (i = i - 1; i >= 0; --i) {
276 kfree(brd->channels[i]);
277 brd->channels[i] = NULL;
284 * dgnc_cleanup_tty() - Cleanup driver.
286 * Uninitialize the TTY portion of this driver. Free all memory and
289 void dgnc_cleanup_tty(struct dgnc_board *brd)
293 for (i = 0; i < brd->nasync; i++)
294 tty_unregister_device(brd->serial_driver, i);
296 tty_unregister_driver(brd->serial_driver);
298 for (i = 0; i < brd->nasync; i++)
299 tty_unregister_device(brd->print_driver, i);
301 tty_unregister_driver(brd->print_driver);
303 put_tty_driver(brd->serial_driver);
304 put_tty_driver(brd->print_driver);
308 * dgnc_wmove() - Write data to transmit queue.
309 * @ch: Pointer to channel structure.
310 * @buf: Pointer to characters to be moved.
311 * @n: Number of characters to move.
313 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
321 head = ch->ch_w_head & WQUEUEMASK;
324 * If the write wraps over the top of the circular buffer,
325 * move the portion up to the wrap point, and reset the
326 * pointers to the bottom.
328 remain = WQUEUESIZE - head;
332 memcpy(ch->ch_wqueue + head, buf, remain);
338 /* Move rest of data. */
340 memcpy(ch->ch_wqueue + head, buf, remain);
345 ch->ch_w_head = head;
349 * dgnc_input() - Process received data.
350 * @ch: Pointer to channel structure.
352 void dgnc_input(struct channel_t *ch)
354 struct dgnc_board *bd;
355 struct tty_struct *tp;
356 struct tty_ldisc *ld = NULL;
371 tp = ch->ch_tun.un_tty;
377 spin_lock_irqsave(&ch->ch_lock, flags);
380 head = ch->ch_r_head & rmask;
381 tail = ch->ch_r_tail & rmask;
382 data_len = (head - tail) & rmask;
388 * If the device is not open, or CREAD is off,
389 * flush input data and return immediately.
392 !(ch->ch_tun.un_flags & UN_ISOPEN) ||
394 (ch->ch_tun.un_flags & UN_CLOSING)) {
395 ch->ch_r_head = tail;
397 /* Force queue flow control to be released, if needed */
398 dgnc_check_queue_flow_control(ch);
403 if (ch->ch_flags & CH_FORCED_STOPI)
406 flip_len = TTY_FLIPBUF_SIZE;
408 len = min(data_len, flip_len);
409 len = min(len, (N_TTY_BUF_SIZE - 1));
411 ld = tty_ldisc_ref(tp);
415 if (!ld->ops->receive_buf) {
416 ch->ch_r_head = ch->ch_r_tail;
425 * The tty layer in the kernel has changed in 2.6.16+.
427 * The flip buffers in the tty structure are no longer exposed,
428 * and probably will be going away eventually.
430 * If we are completely raw, we don't need to go through a lot
431 * of the tty layers that exist.
432 * In this case, we take the shortest and fastest route we
433 * can to relay the data to the user.
435 * On the other hand, if we are not raw, we need to go through
436 * the new 2.6.16+ tty layer, which has its API more well defined.
438 len = tty_buffer_request_room(tp->port, len);
442 * n now contains the most amount of data we can copy,
443 * bounded either by how much the Linux tty layer can handle,
444 * or the amount of data the card actually has pending...
447 unsigned char *ch_pos = ch->ch_equeue + tail;
449 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
456 * If conditions are such that ld needs to see all
457 * UART errors, we will have to walk each character
458 * and error byte and send them to the buffer one at
461 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
462 for (i = 0; i < s; i++) {
463 unsigned char ch = *(ch_pos + i);
464 char flag = TTY_NORMAL;
466 if (ch & UART_LSR_BI)
468 else if (ch & UART_LSR_PE)
470 else if (ch & UART_LSR_FE)
473 tty_insert_flip_char(tp->port, ch, flag);
476 tty_insert_flip_string(tp->port, ch_pos, s);
481 /* Flip queue if needed */
485 ch->ch_r_tail = tail & rmask;
486 ch->ch_e_tail = tail & rmask;
487 dgnc_check_queue_flow_control(ch);
488 spin_unlock_irqrestore(&ch->ch_lock, flags);
490 /* Tell the tty layer its okay to "eat" the data now */
491 tty_flip_buffer_push(tp->port);
498 spin_unlock_irqrestore(&ch->ch_lock, flags);
506 * Determines when CARRIER changes state and takes appropriate
509 void dgnc_carrier(struct channel_t *ch)
511 int virt_carrier = 0;
512 int phys_carrier = 0;
517 if (ch->ch_mistat & UART_MSR_DCD)
520 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
523 if (ch->ch_c_cflag & CLOCAL)
526 /* Test for a VIRTUAL carrier transition to HIGH. */
528 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
530 * When carrier rises, wake any threads waiting
531 * for carrier in the open routine.
533 if (waitqueue_active(&ch->ch_flags_wait))
534 wake_up_interruptible(&ch->ch_flags_wait);
537 /* Test for a PHYSICAL carrier transition to HIGH. */
539 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
541 * When carrier rises, wake any threads waiting
542 * for carrier in the open routine.
544 if (waitqueue_active(&ch->ch_flags_wait))
545 wake_up_interruptible(&ch->ch_flags_wait);
549 * Test for a PHYSICAL transition to low, so long as we aren't
550 * currently ignoring physical transitions (which is what "virtual
551 * carrier" indicates).
553 * The transition of the virtual carrier to low really doesn't
554 * matter... it really only means "ignore carrier state", not
555 * "make pretend that carrier is there".
557 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
558 (phys_carrier == 0)) {
560 * When carrier drops:
562 * Drop carrier on all open units.
564 * Flush queues, waking up any task waiting in the
567 * Send a hangup to the control terminal.
569 * Enable all select calls.
571 if (waitqueue_active(&ch->ch_flags_wait))
572 wake_up_interruptible(&ch->ch_flags_wait);
574 if (ch->ch_tun.un_open_count > 0)
575 tty_hangup(ch->ch_tun.un_tty);
577 if (ch->ch_pun.un_open_count > 0)
578 tty_hangup(ch->ch_pun.un_tty);
581 /* Make sure that our cached values reflect the current reality. */
583 if (virt_carrier == 1)
584 ch->ch_flags |= CH_FCAR;
586 ch->ch_flags &= ~CH_FCAR;
588 if (phys_carrier == 1)
589 ch->ch_flags |= CH_CD;
591 ch->ch_flags &= ~CH_CD;
594 /* Assign the custom baud rate to the channel structure */
595 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
604 ch->ch_custom_speed = 0;
609 * Since the divisor is stored in a 16-bit integer, we make sure
610 * we don't allow any rates smaller than a 16-bit integer would allow.
611 * And of course, rates above the dividend won't fly.
613 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
614 newrate = (ch->ch_bd->bd_dividend / 0xFFFF) + 1;
616 if (newrate && newrate > ch->ch_bd->bd_dividend)
617 newrate = ch->ch_bd->bd_dividend;
620 testdiv = ch->ch_bd->bd_dividend / newrate;
623 * If we try to figure out what rate the board would use
624 * with the test divisor, it will be either equal or higher
625 * than the requested baud rate. If we then determine the
626 * rate with a divisor one higher, we will get the next lower
627 * supported rate below the requested.
629 testrate_high = ch->ch_bd->bd_dividend / testdiv;
630 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
633 * If the rate for the requested divisor is correct, just
634 * use it and be done.
636 if (testrate_high != newrate) {
638 * Otherwise, pick the rate that is closer
639 * (i.e. whichever rate has a smaller delta).
641 deltahigh = testrate_high - newrate;
642 deltalow = newrate - testrate_low;
644 if (deltahigh < deltalow)
645 newrate = testrate_high;
647 newrate = testrate_low;
651 ch->ch_custom_speed = newrate;
654 void dgnc_check_queue_flow_control(struct channel_t *ch)
658 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
660 qleft += RQUEUEMASK + 1;
663 * Check to see if we should enforce flow control on our queue because
664 * the ld (or user) isn't reading data out of our queue fast enuf.
666 * NOTE: This is done based on what the current flow control of the
669 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
670 * This will cause the UART's FIFO to back up, and force
671 * the RTS signal to be dropped.
672 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
673 * the other side, in hopes it will stop sending data to us.
674 * 3) NONE - Nothing we can do. We will simply drop any extra data
675 * that gets sent into us when the queue fills up.
679 if (ch->ch_digi.digi_flags & CTSPACE ||
680 ch->ch_c_cflag & CRTSCTS) {
681 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
682 ch->ch_bd->bd_ops->disable_receiver(ch);
683 ch->ch_flags |= (CH_RECEIVER_OFF);
687 else if (ch->ch_c_iflag & IXOFF) {
688 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
689 ch->ch_bd->bd_ops->send_stop_character(ch);
696 * Check to see if we should unenforce flow control because
697 * ld (or user) finally read enuf data out of our queue.
699 * NOTE: This is done based on what the current flow control of the
702 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
703 * This will cause the UART's FIFO to raise RTS back up,
704 * which will allow the other side to start sending data again.
705 * 2) SWFLOW (IXOFF) - Send a start character to
706 * the other side, so it will start sending data to us again.
707 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
708 * other side, we don't need to do anything now.
710 if (qleft > (RQUEUESIZE / 2)) {
712 if (ch->ch_digi.digi_flags & RTSPACE ||
713 ch->ch_c_cflag & CRTSCTS) {
714 if (ch->ch_flags & CH_RECEIVER_OFF) {
715 ch->ch_bd->bd_ops->enable_receiver(ch);
716 ch->ch_flags &= ~(CH_RECEIVER_OFF);
720 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
721 ch->ch_stops_sent = 0;
722 ch->ch_bd->bd_ops->send_start_character(ch);
727 static void dgnc_set_signal_low(struct channel_t *ch, const unsigned char sig)
729 ch->ch_mostat &= ~(sig);
730 ch->ch_bd->bd_ops->assert_modem_signals(ch);
733 void dgnc_wakeup_writes(struct channel_t *ch)
741 spin_lock_irqsave(&ch->ch_lock, flags);
743 /* If channel now has space, wake up anyone waiting on the condition. */
745 qlen = ch->ch_w_head - ch->ch_w_tail;
749 if (qlen >= (WQUEUESIZE - 256)) {
750 spin_unlock_irqrestore(&ch->ch_lock, flags);
754 if (ch->ch_tun.un_flags & UN_ISOPEN) {
755 tty_wakeup(ch->ch_tun.un_tty);
758 * If unit is set to wait until empty, check to make sure
759 * the queue AND FIFO are both empty.
761 if (ch->ch_tun.un_flags & UN_EMPTY) {
763 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
764 ch->ch_tun.un_flags &= ~(UN_EMPTY);
767 * If RTS Toggle mode is on, whenever
768 * the queue and UART is empty, keep RTS low.
770 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
771 dgnc_set_signal_low(ch, UART_MCR_RTS);
774 * If DTR Toggle mode is on, whenever
775 * the queue and UART is empty, keep DTR low.
777 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE)
778 dgnc_set_signal_low(ch, UART_MCR_DTR);
782 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
785 if (ch->ch_pun.un_flags & UN_ISOPEN) {
786 tty_wakeup(ch->ch_pun.un_tty);
789 * If unit is set to wait until empty, check to make sure
790 * the queue AND FIFO are both empty.
792 if (ch->ch_pun.un_flags & UN_EMPTY) {
794 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
795 ch->ch_pun.un_flags &= ~(UN_EMPTY);
798 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
801 spin_unlock_irqrestore(&ch->ch_lock, flags);
804 static struct dgnc_board *find_board_by_major(unsigned int major)
808 for (i = 0; i < MAXBOARDS; i++) {
809 struct dgnc_board *brd = dgnc_board[i];
814 if (major == brd->serial_driver->major ||
815 major == brd->print_driver->major)
822 /* TTY Entry points and helper functions */
824 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
826 struct dgnc_board *brd;
827 struct channel_t *ch;
836 major = MAJOR(tty_devnum(tty));
837 minor = MINOR(tty_devnum(tty));
842 brd = find_board_by_major(major);
846 rc = wait_event_interruptible(brd->state_wait,
847 (brd->state & BOARD_READY));
851 spin_lock_irqsave(&brd->bd_lock, flags);
853 if (PORT_NUM(minor) >= brd->nasync) {
858 ch = brd->channels[PORT_NUM(minor)];
864 spin_unlock_irqrestore(&brd->bd_lock, flags);
866 spin_lock_irqsave(&ch->ch_lock, flags);
868 /* Figure out our type */
869 if (!IS_PRINT(minor)) {
870 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
871 un->un_type = DGNC_SERIAL;
872 } else if (IS_PRINT(minor)) {
873 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
874 un->un_type = DGNC_PRINT;
881 * If the port is still in a previous open, and in a state
882 * where we simply cannot safely keep going, wait until the
885 spin_unlock_irqrestore(&ch->ch_lock, flags);
887 rc = wait_event_interruptible(ch->ch_flags_wait,
888 ((ch->ch_flags & CH_OPENING) == 0));
889 /* If ret is non-zero, user ctrl-c'ed us */
894 * If either unit is in the middle of the fragile part of close,
895 * we just cannot touch the channel safely.
896 * Go to sleep, knowing that when the channel can be
897 * touched safely, the close routine will signal the
898 * ch_flags_wait to wake us back up.
900 rc = wait_event_interruptible(
902 (((ch->ch_tun.un_flags |
903 ch->ch_pun.un_flags) & UN_CLOSING) == 0));
904 /* If ret is non-zero, user ctrl-c'ed us */
908 spin_lock_irqsave(&ch->ch_lock, flags);
910 tty->driver_data = un;
912 /* Initialize tty's */
914 if (!(un->un_flags & UN_ISOPEN)) {
917 /* Maybe do something here to the TTY struct as well? */
921 * Allocate channel buffers for read/write/error.
922 * Set flag, so we don't get trounced on.
924 ch->ch_flags |= (CH_OPENING);
926 spin_unlock_irqrestore(&ch->ch_lock, flags);
929 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
931 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
933 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
935 if (!ch->ch_rqueue || !ch->ch_equeue || !ch->ch_wqueue) {
936 kfree(ch->ch_rqueue);
937 kfree(ch->ch_equeue);
938 kfree(ch->ch_wqueue);
942 spin_lock_irqsave(&ch->ch_lock, flags);
944 ch->ch_flags &= ~(CH_OPENING);
945 wake_up_interruptible(&ch->ch_flags_wait);
947 /* Initialize if neither terminal or printer is open. */
949 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
950 /* Flush input queues. */
958 brd->bd_ops->flush_uart_write(ch);
959 brd->bd_ops->flush_uart_read(ch);
962 ch->ch_cached_lsr = 0;
963 ch->ch_stop_sending_break = 0;
964 ch->ch_stops_sent = 0;
966 ch->ch_c_cflag = tty->termios.c_cflag;
967 ch->ch_c_iflag = tty->termios.c_iflag;
968 ch->ch_c_oflag = tty->termios.c_oflag;
969 ch->ch_c_lflag = tty->termios.c_lflag;
970 ch->ch_startc = tty->termios.c_cc[VSTART];
971 ch->ch_stopc = tty->termios.c_cc[VSTOP];
974 * Bring up RTS and DTR...
975 * Also handle RTS or DTR toggle if set.
977 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
978 ch->ch_mostat |= (UART_MCR_RTS);
979 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
980 ch->ch_mostat |= (UART_MCR_DTR);
982 /* Tell UART to init itself */
983 brd->bd_ops->uart_init(ch);
986 brd->bd_ops->param(tty);
990 spin_unlock_irqrestore(&ch->ch_lock, flags);
992 rc = dgnc_block_til_ready(tty, file, ch);
994 spin_lock_irqsave(&ch->ch_lock, flags);
997 un->un_flags |= (UN_ISOPEN);
998 spin_unlock_irqrestore(&ch->ch_lock, flags);
1003 spin_unlock_irqrestore(&brd->bd_lock, flags);
1007 spin_unlock_irqrestore(&ch->ch_lock, flags);
1012 /* Wait for DCD, if needed. */
1013 static int dgnc_block_til_ready(struct tty_struct *tty,
1015 struct channel_t *ch)
1018 struct un_t *un = tty->driver_data;
1019 unsigned long flags;
1021 int sleep_on_un_flags = 0;
1026 spin_lock_irqsave(&ch->ch_lock, flags);
1031 sleep_on_un_flags = 0;
1033 if (ch->ch_bd->state == BOARD_FAILED) {
1038 if (tty_hung_up_p(file)) {
1044 * If either unit is in the middle of the fragile part of close,
1045 * we just cannot touch the channel safely.
1046 * Go back to sleep, knowing that when the channel can be
1047 * touched safely, the close routine will signal the
1048 * ch_wait_flags to wake us back up.
1050 if (!((ch->ch_tun.un_flags |
1051 ch->ch_pun.un_flags) &
1054 * Our conditions to leave cleanly and happily:
1055 * 1) NONBLOCKING on the tty is set.
1057 * 3) DCD (fake or real) is active.
1060 if (file->f_flags & O_NONBLOCK)
1063 if (tty_io_error(tty)) {
1068 if (ch->ch_flags & CH_CD)
1071 if (ch->ch_flags & CH_FCAR)
1074 sleep_on_un_flags = 1;
1078 * If there is a signal pending, the user probably
1079 * interrupted (ctrl-c) us.
1081 if (signal_pending(current)) {
1086 if (sleep_on_un_flags)
1087 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1089 old_flags = ch->ch_flags;
1092 * Let go of channel lock before calling schedule.
1093 * Our poller will get any FEP events and wake us up when DCD
1094 * eventually goes active.
1097 spin_unlock_irqrestore(&ch->ch_lock, flags);
1100 * Wait for something in the flags to change
1101 * from the current value.
1103 if (sleep_on_un_flags)
1104 rc = wait_event_interruptible
1106 (old_flags != (ch->ch_tun.un_flags |
1107 ch->ch_pun.un_flags)));
1109 rc = wait_event_interruptible(
1111 (old_flags != ch->ch_flags));
1114 * We got woken up for some reason.
1115 * Before looping around, grab our channel lock.
1117 spin_lock_irqsave(&ch->ch_lock, flags);
1122 spin_unlock_irqrestore(&ch->ch_lock, flags);
1127 /* Hangup the port. Like a close, but don't wait for output to drain. */
1128 static void dgnc_tty_hangup(struct tty_struct *tty)
1133 /* flush the transmit queues */
1134 dgnc_tty_flush_buffer(tty);
1137 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1139 struct dgnc_board *bd;
1140 struct channel_t *ch;
1142 unsigned long flags;
1147 un = tty->driver_data;
1159 spin_lock_irqsave(&ch->ch_lock, flags);
1162 * Determine if this is the last close or not - and if we agree about
1163 * which type of close it is with the Line Discipline
1165 if ((tty->count == 1) && (un->un_open_count != 1)) {
1167 * Uh, oh. tty->count is 1, which means that the tty
1168 * structure will be freed. un_open_count should always
1169 * be one in these conditions. If it's greater than
1170 * one, we've got real problems, since it means the
1171 * serial port won't be shutdown.
1174 "tty->count is 1, un open count is %d\n",
1176 un->un_open_count = 1;
1179 if (un->un_open_count)
1180 un->un_open_count--;
1183 "bad serial port open count of %d\n",
1186 ch->ch_open_count--;
1188 if (ch->ch_open_count && un->un_open_count) {
1189 spin_unlock_irqrestore(&ch->ch_lock, flags);
1193 /* OK, its the last close on the unit */
1194 un->un_flags |= UN_CLOSING;
1199 * Only officially close channel if count is 0 and
1200 * DIGI_PRINTER bit is not set.
1202 if ((ch->ch_open_count == 0) &&
1203 !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1204 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1206 /* turn off print device when closing print device. */
1208 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1209 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1210 (int)ch->ch_digi.digi_offlen);
1211 ch->ch_flags &= ~CH_PRON;
1214 spin_unlock_irqrestore(&ch->ch_lock, flags);
1215 /* wait for output to drain */
1216 /* This will also return if we take an interrupt */
1218 bd->bd_ops->drain(tty, 0);
1220 dgnc_tty_flush_buffer(tty);
1221 tty_ldisc_flush(tty);
1223 spin_lock_irqsave(&ch->ch_lock, flags);
1227 /* If we have HUPCL set, lower DTR and RTS */
1229 if (ch->ch_c_cflag & HUPCL) {
1231 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1232 bd->bd_ops->assert_modem_signals(ch);
1235 * Go to sleep to ensure RTS/DTR
1236 * have been dropped for modems to see it.
1238 if (ch->ch_close_delay) {
1239 spin_unlock_irqrestore(&ch->ch_lock,
1241 dgnc_ms_sleep(ch->ch_close_delay);
1242 spin_lock_irqsave(&ch->ch_lock, flags);
1246 ch->ch_old_baud = 0;
1248 /* Turn off UART interrupts for this port */
1249 ch->ch_bd->bd_ops->uart_off(ch);
1251 /* turn off print device when closing print device. */
1253 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1254 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1255 (int)ch->ch_digi.digi_offlen);
1256 ch->ch_flags &= ~CH_PRON;
1261 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1263 wake_up_interruptible(&ch->ch_flags_wait);
1264 wake_up_interruptible(&un->un_flags_wait);
1266 spin_unlock_irqrestore(&ch->ch_lock, flags);
1270 * Return number of characters that have not been transmitted yet.
1272 * This routine is used by the line discipline to determine if there
1273 * is data waiting to be transmitted/drained/flushed or not.
1275 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1277 struct channel_t *ch = NULL;
1278 struct un_t *un = NULL;
1283 unsigned long flags;
1288 un = tty->driver_data;
1296 spin_lock_irqsave(&ch->ch_lock, flags);
1299 thead = ch->ch_w_head & tmask;
1300 ttail = ch->ch_w_tail & tmask;
1302 spin_unlock_irqrestore(&ch->ch_lock, flags);
1306 else if (thead > ttail)
1307 chars = thead - ttail;
1309 chars = thead - ttail + WQUEUESIZE;
1315 * Reduces bytes_available to the max number of characters
1316 * that can be sent currently given the maxcps value, and
1317 * returns the new bytes_available. This only affects printer
1320 static int dgnc_maxcps_room(struct channel_t *ch, int bytes_available)
1322 int rc = bytes_available;
1324 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1326 unsigned long current_time = jiffies;
1327 unsigned long buffer_time = current_time +
1328 (HZ * ch->ch_digi.digi_bufsize) /
1329 ch->ch_digi.digi_maxcps;
1331 if (ch->ch_cpstime < current_time) {
1332 /* buffer is empty */
1333 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1334 cps_limit = ch->ch_digi.digi_bufsize;
1335 } else if (ch->ch_cpstime < buffer_time) {
1336 /* still room in the buffer */
1337 cps_limit = ((buffer_time - ch->ch_cpstime) *
1338 ch->ch_digi.digi_maxcps) / HZ;
1340 /* no room in the buffer */
1344 rc = min(cps_limit, bytes_available);
1350 /* Return room available in Tx buffer */
1351 static int dgnc_tty_write_room(struct tty_struct *tty)
1353 struct channel_t *ch = NULL;
1354 struct un_t *un = NULL;
1359 unsigned long flags;
1364 un = tty->driver_data;
1372 spin_lock_irqsave(&ch->ch_lock, flags);
1375 head = (ch->ch_w_head) & tmask;
1376 tail = (ch->ch_w_tail) & tmask;
1378 room = tail - head - 1;
1382 /* Limit printer to maxcps */
1383 if (un->un_type != DGNC_PRINT)
1384 room = dgnc_maxcps_room(ch, room);
1387 * If we are printer device, leave room for
1388 * possibly both the on and off strings.
1390 if (un->un_type == DGNC_PRINT) {
1391 if (!(ch->ch_flags & CH_PRON))
1392 room -= ch->ch_digi.digi_onlen;
1393 room -= ch->ch_digi.digi_offlen;
1395 if (ch->ch_flags & CH_PRON)
1396 room -= ch->ch_digi.digi_offlen;
1402 spin_unlock_irqrestore(&ch->ch_lock, flags);
1407 * Put a character into ch->ch_buf
1408 * Used by the line discipline for OPOST processing
1410 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1412 dgnc_tty_write(tty, &c, 1);
1417 * Take data from the user or kernel and send it out to the FEP.
1418 * In here exists all the Transparent Print magic as well.
1420 static int dgnc_tty_write(struct tty_struct *tty,
1421 const unsigned char *buf, int count)
1423 struct channel_t *ch = NULL;
1424 struct un_t *un = NULL;
1425 int bufcount = 0, n = 0;
1426 unsigned long flags;
1435 un = tty->driver_data;
1447 * Store original amount of characters passed in.
1448 * This helps to figure out if we should ask the FEP
1449 * to send us an event when it has more space available.
1452 spin_lock_irqsave(&ch->ch_lock, flags);
1455 head = (ch->ch_w_head) & tmask;
1456 tail = (ch->ch_w_tail) & tmask;
1458 bufcount = tail - head - 1;
1460 bufcount += WQUEUESIZE;
1463 * Limit printer output to maxcps overall, with bursts allowed
1464 * up to bufsize characters.
1466 if (un->un_type != DGNC_PRINT)
1467 bufcount = dgnc_maxcps_room(ch, bufcount);
1469 count = min(count, bufcount);
1474 * Output the printer ON string, if we are in terminal mode, but
1475 * need to be in printer mode.
1477 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1478 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1479 (int)ch->ch_digi.digi_onlen);
1480 head = (ch->ch_w_head) & tmask;
1481 ch->ch_flags |= CH_PRON;
1485 * On the other hand, output the printer OFF string, if we are
1486 * currently in printer mode, but need to output to the terminal.
1488 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1489 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1490 (int)ch->ch_digi.digi_offlen);
1491 head = (ch->ch_w_head) & tmask;
1492 ch->ch_flags &= ~CH_PRON;
1498 * If the write wraps over the top of the circular buffer,
1499 * move the portion up to the wrap point, and reset the
1500 * pointers to the bottom.
1502 remain = WQUEUESIZE - head;
1506 memcpy(ch->ch_wqueue + head, buf, remain);
1512 /* Move rest of data. */
1514 memcpy(ch->ch_wqueue + head, buf, remain);
1520 ch->ch_w_head = head;
1523 /* Update printer buffer empty time. */
1524 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0) &&
1525 (ch->ch_digi.digi_bufsize > 0)) {
1526 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1529 spin_unlock_irqrestore(&ch->ch_lock, flags);
1532 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1537 spin_unlock_irqrestore(&ch->ch_lock, flags);
1542 /* Return modem signals to ld. */
1543 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1545 struct channel_t *ch;
1548 unsigned char mstat = 0;
1549 unsigned long flags;
1554 un = tty->driver_data;
1562 spin_lock_irqsave(&ch->ch_lock, flags);
1564 mstat = ch->ch_mostat | ch->ch_mistat;
1566 spin_unlock_irqrestore(&ch->ch_lock, flags);
1570 if (mstat & UART_MCR_DTR)
1572 if (mstat & UART_MCR_RTS)
1574 if (mstat & UART_MSR_CTS)
1576 if (mstat & UART_MSR_DSR)
1578 if (mstat & UART_MSR_RI)
1580 if (mstat & UART_MSR_DCD)
1586 /* Set modem signals, called by ld. */
1587 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1588 unsigned int set, unsigned int clear)
1590 struct dgnc_board *bd;
1591 struct channel_t *ch;
1593 unsigned long flags;
1598 un = tty->driver_data;
1610 spin_lock_irqsave(&ch->ch_lock, flags);
1612 if (set & TIOCM_RTS)
1613 ch->ch_mostat |= UART_MCR_RTS;
1615 if (set & TIOCM_DTR)
1616 ch->ch_mostat |= UART_MCR_DTR;
1618 if (clear & TIOCM_RTS)
1619 ch->ch_mostat &= ~(UART_MCR_RTS);
1621 if (clear & TIOCM_DTR)
1622 ch->ch_mostat &= ~(UART_MCR_DTR);
1624 bd->bd_ops->assert_modem_signals(ch);
1626 spin_unlock_irqrestore(&ch->ch_lock, flags);
1631 /* Send a Break, called by ld. */
1632 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
1634 struct dgnc_board *bd;
1635 struct channel_t *ch;
1637 unsigned long flags;
1642 un = tty->driver_data;
1657 spin_lock_irqsave(&ch->ch_lock, flags);
1659 bd->bd_ops->send_break(ch, msec);
1661 spin_unlock_irqrestore(&ch->ch_lock, flags);
1666 /* wait until data has been transmitted, called by ld. */
1667 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1669 struct dgnc_board *bd;
1670 struct channel_t *ch;
1676 un = tty->driver_data;
1688 bd->bd_ops->drain(tty, 0);
1691 /* send a high priority character, called by ld. */
1692 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
1694 struct dgnc_board *bd;
1695 struct channel_t *ch;
1697 unsigned long flags;
1702 un = tty->driver_data;
1714 spin_lock_irqsave(&ch->ch_lock, flags);
1715 bd->bd_ops->send_immediate_char(ch, c);
1716 spin_unlock_irqrestore(&ch->ch_lock, flags);
1719 /* Return modem signals to ld. */
1720 static inline int dgnc_get_mstat(struct channel_t *ch)
1722 unsigned char mstat;
1723 unsigned long flags;
1729 spin_lock_irqsave(&ch->ch_lock, flags);
1731 mstat = ch->ch_mostat | ch->ch_mistat;
1733 spin_unlock_irqrestore(&ch->ch_lock, flags);
1737 if (mstat & UART_MCR_DTR)
1739 if (mstat & UART_MCR_RTS)
1741 if (mstat & UART_MSR_CTS)
1743 if (mstat & UART_MSR_DSR)
1745 if (mstat & UART_MSR_RI)
1747 if (mstat & UART_MSR_DCD)
1753 /* Return modem signals to ld. */
1754 static int dgnc_get_modem_info(struct channel_t *ch,
1755 unsigned int __user *value)
1757 return put_user(dgnc_get_mstat(ch), value);
1760 /* Set modem signals, called by ld. */
1761 static int dgnc_set_modem_info(struct channel_t *ch,
1762 unsigned int command,
1763 unsigned int __user *value)
1766 unsigned int arg = 0;
1767 unsigned long flags;
1769 rc = get_user(arg, value);
1775 if (arg & TIOCM_RTS)
1776 ch->ch_mostat |= UART_MCR_RTS;
1778 if (arg & TIOCM_DTR)
1779 ch->ch_mostat |= UART_MCR_DTR;
1784 if (arg & TIOCM_RTS)
1785 ch->ch_mostat &= ~(UART_MCR_RTS);
1787 if (arg & TIOCM_DTR)
1788 ch->ch_mostat &= ~(UART_MCR_DTR);
1794 if (arg & TIOCM_RTS)
1795 ch->ch_mostat |= UART_MCR_RTS;
1797 ch->ch_mostat &= ~(UART_MCR_RTS);
1799 if (arg & TIOCM_DTR)
1800 ch->ch_mostat |= UART_MCR_DTR;
1802 ch->ch_mostat &= ~(UART_MCR_DTR);
1810 spin_lock_irqsave(&ch->ch_lock, flags);
1812 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1814 spin_unlock_irqrestore(&ch->ch_lock, flags);
1819 /* Ioctl to get the information for ditty. */
1820 static int dgnc_tty_digigeta(struct tty_struct *tty,
1821 struct digi_t __user *retinfo)
1823 struct channel_t *ch;
1826 unsigned long flags;
1834 un = tty->driver_data;
1842 memset(&tmp, 0, sizeof(tmp));
1844 spin_lock_irqsave(&ch->ch_lock, flags);
1845 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
1846 spin_unlock_irqrestore(&ch->ch_lock, flags);
1848 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1854 /* Ioctl to set the information for ditty. */
1855 static int dgnc_tty_digiseta(struct tty_struct *tty,
1856 struct digi_t __user *new_info)
1858 struct dgnc_board *bd;
1859 struct channel_t *ch;
1861 struct digi_t new_digi;
1862 unsigned long flags;
1867 un = tty->driver_data;
1879 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
1882 spin_lock_irqsave(&ch->ch_lock, flags);
1884 /* Handle transitions to and from RTS Toggle. */
1886 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
1887 (new_digi.digi_flags & DIGI_RTS_TOGGLE))
1888 ch->ch_mostat &= ~(UART_MCR_RTS);
1889 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
1890 !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
1891 ch->ch_mostat |= (UART_MCR_RTS);
1893 /* Handle transitions to and from DTR Toggle. */
1895 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
1896 (new_digi.digi_flags & DIGI_DTR_TOGGLE))
1897 ch->ch_mostat &= ~(UART_MCR_DTR);
1898 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
1899 !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
1900 ch->ch_mostat |= (UART_MCR_DTR);
1902 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
1904 if (ch->ch_digi.digi_maxcps < 1)
1905 ch->ch_digi.digi_maxcps = 1;
1907 if (ch->ch_digi.digi_maxcps > 10000)
1908 ch->ch_digi.digi_maxcps = 10000;
1910 if (ch->ch_digi.digi_bufsize < 10)
1911 ch->ch_digi.digi_bufsize = 10;
1913 if (ch->ch_digi.digi_maxchar < 1)
1914 ch->ch_digi.digi_maxchar = 1;
1916 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
1917 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
1919 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
1920 ch->ch_digi.digi_onlen = DIGI_PLEN;
1922 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
1923 ch->ch_digi.digi_offlen = DIGI_PLEN;
1925 bd->bd_ops->param(tty);
1927 spin_unlock_irqrestore(&ch->ch_lock, flags);
1932 static void dgnc_tty_set_termios(struct tty_struct *tty,
1933 struct ktermios *old_termios)
1935 struct dgnc_board *bd;
1936 struct channel_t *ch;
1938 unsigned long flags;
1943 un = tty->driver_data;
1955 spin_lock_irqsave(&ch->ch_lock, flags);
1957 ch->ch_c_cflag = tty->termios.c_cflag;
1958 ch->ch_c_iflag = tty->termios.c_iflag;
1959 ch->ch_c_oflag = tty->termios.c_oflag;
1960 ch->ch_c_lflag = tty->termios.c_lflag;
1961 ch->ch_startc = tty->termios.c_cc[VSTART];
1962 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1964 bd->bd_ops->param(tty);
1967 spin_unlock_irqrestore(&ch->ch_lock, flags);
1970 static void dgnc_tty_throttle(struct tty_struct *tty)
1972 struct channel_t *ch;
1974 unsigned long flags;
1979 un = tty->driver_data;
1987 spin_lock_irqsave(&ch->ch_lock, flags);
1989 ch->ch_flags |= (CH_FORCED_STOPI);
1991 spin_unlock_irqrestore(&ch->ch_lock, flags);
1994 static void dgnc_tty_unthrottle(struct tty_struct *tty)
1996 struct channel_t *ch;
1998 unsigned long flags;
2003 un = tty->driver_data;
2011 spin_lock_irqsave(&ch->ch_lock, flags);
2013 ch->ch_flags &= ~(CH_FORCED_STOPI);
2015 spin_unlock_irqrestore(&ch->ch_lock, flags);
2018 static void dgnc_tty_start(struct tty_struct *tty)
2020 struct dgnc_board *bd;
2021 struct channel_t *ch;
2023 unsigned long flags;
2028 un = tty->driver_data;
2040 spin_lock_irqsave(&ch->ch_lock, flags);
2042 ch->ch_flags &= ~(CH_FORCED_STOP);
2044 spin_unlock_irqrestore(&ch->ch_lock, flags);
2047 static void dgnc_tty_stop(struct tty_struct *tty)
2049 struct dgnc_board *bd;
2050 struct channel_t *ch;
2052 unsigned long flags;
2057 un = tty->driver_data;
2069 spin_lock_irqsave(&ch->ch_lock, flags);
2071 ch->ch_flags |= (CH_FORCED_STOP);
2073 spin_unlock_irqrestore(&ch->ch_lock, flags);
2077 * Flush the cook buffer
2079 * Note to self, and any other poor souls who venture here:
2081 * flush in this case DOES NOT mean dispose of the data.
2082 * instead, it means "stop buffering and send it if you
2083 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2085 * It is also always called in interrupt context - JAR 8-Sept-99
2087 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2089 struct dgnc_board *bd;
2090 struct channel_t *ch;
2092 unsigned long flags;
2097 un = tty->driver_data;
2109 spin_lock_irqsave(&ch->ch_lock, flags);
2111 /* Do something maybe here */
2113 spin_unlock_irqrestore(&ch->ch_lock, flags);
2116 /* Flush Tx buffer (make in == out) */
2117 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2119 struct channel_t *ch;
2121 unsigned long flags;
2126 un = tty->driver_data;
2134 spin_lock_irqsave(&ch->ch_lock, flags);
2136 ch->ch_flags &= ~CH_STOP;
2138 /* Flush our write queue */
2139 ch->ch_w_head = ch->ch_w_tail;
2141 /* Flush UARTs transmit FIFO */
2142 ch->ch_bd->bd_ops->flush_uart_write(ch);
2144 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
2145 ch->ch_tun.un_flags &= ~(UN_LOW | UN_EMPTY);
2146 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2148 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
2149 ch->ch_pun.un_flags &= ~(UN_LOW | UN_EMPTY);
2150 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2153 spin_unlock_irqrestore(&ch->ch_lock, flags);
2156 /* Wakes up processes waiting in the unit's (teminal/printer) wait queue */
2157 static void dgnc_wake_up_unit(struct un_t *unit)
2159 unit->un_flags &= ~(UN_LOW | UN_EMPTY);
2160 wake_up_interruptible(&unit->un_flags_wait);
2163 /* The IOCTL function and all of its helpers */
2165 /* The usual assortment of ioctl's */
2166 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2169 struct dgnc_board *bd;
2170 struct board_ops *ch_bd_ops;
2171 struct channel_t *ch;
2174 unsigned long flags;
2175 void __user *uarg = (void __user *)arg;
2180 un = tty->driver_data;
2192 ch_bd_ops = bd->bd_ops;
2194 spin_lock_irqsave(&ch->ch_lock, flags);
2196 if (un->un_open_count <= 0) {
2202 /* Here are all the standard ioctl's that we MUST implement */
2206 * TCSBRK is SVID version: non-zero arg --> no break
2207 * this behaviour is exploited by tcdrain().
2209 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2210 * between 0.25 and 0.5 seconds so we'll ask for something
2211 * in the middle: 0.375 seconds.
2213 rc = tty_check_change(tty);
2214 spin_unlock_irqrestore(&ch->ch_lock, flags);
2218 rc = ch_bd_ops->drain(tty, 0);
2222 spin_lock_irqsave(&ch->ch_lock, flags);
2224 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2225 ch_bd_ops->send_break(ch, 250);
2227 spin_unlock_irqrestore(&ch->ch_lock, flags);
2233 * support for POSIX tcsendbreak()
2234 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2235 * between 0.25 and 0.5 seconds so we'll ask for something
2236 * in the middle: 0.375 seconds.
2238 rc = tty_check_change(tty);
2239 spin_unlock_irqrestore(&ch->ch_lock, flags);
2243 rc = ch_bd_ops->drain(tty, 0);
2247 spin_lock_irqsave(&ch->ch_lock, flags);
2249 ch_bd_ops->send_break(ch, 250);
2251 spin_unlock_irqrestore(&ch->ch_lock, flags);
2256 rc = tty_check_change(tty);
2257 spin_unlock_irqrestore(&ch->ch_lock, flags);
2261 rc = ch_bd_ops->drain(tty, 0);
2265 spin_lock_irqsave(&ch->ch_lock, flags);
2267 ch_bd_ops->send_break(ch, 250);
2269 spin_unlock_irqrestore(&ch->ch_lock, flags);
2275 spin_unlock_irqrestore(&ch->ch_lock, flags);
2280 spin_unlock_irqrestore(&ch->ch_lock, flags);
2282 return put_user(C_CLOCAL(tty) ? 1 : 0,
2283 (unsigned long __user *)arg);
2287 spin_unlock_irqrestore(&ch->ch_lock, flags);
2288 rc = get_user(arg, (unsigned long __user *)arg);
2292 spin_lock_irqsave(&ch->ch_lock, flags);
2293 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
2294 (arg ? CLOCAL : 0));
2295 ch_bd_ops->param(tty);
2296 spin_unlock_irqrestore(&ch->ch_lock, flags);
2301 spin_unlock_irqrestore(&ch->ch_lock, flags);
2302 return dgnc_get_modem_info(ch, uarg);
2307 spin_unlock_irqrestore(&ch->ch_lock, flags);
2308 return dgnc_set_modem_info(ch, cmd, uarg);
2310 /* Here are any additional ioctl's that we want to implement */
2314 * The linux tty driver doesn't have a flush
2315 * input routine for the driver, assuming all backed
2316 * up data is in the line disc. buffers. However,
2317 * we all know that's not the case. Here, we
2318 * act on the ioctl, but then lie and say we didn't
2319 * so the line discipline will process the flush
2322 rc = tty_check_change(tty);
2326 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2327 ch->ch_r_head = ch->ch_r_tail;
2328 ch_bd_ops->flush_uart_read(ch);
2329 /* Force queue flow control to be released, if needed */
2330 dgnc_check_queue_flow_control(ch);
2333 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2334 if (!(un->un_type == DGNC_PRINT)) {
2335 ch->ch_w_head = ch->ch_w_tail;
2336 ch_bd_ops->flush_uart_write(ch);
2338 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY))
2339 dgnc_wake_up_unit(&ch->ch_tun);
2341 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY))
2342 dgnc_wake_up_unit(&ch->ch_pun);
2346 /* pretend we didn't recognize this IOCTL */
2347 spin_unlock_irqrestore(&ch->ch_lock, flags);
2348 return -ENOIOCTLCMD;
2352 * The linux tty driver doesn't have a flush
2353 * input routine for the driver, assuming all backed
2354 * up data is in the line disc. buffers. However,
2355 * we all know that's not the case. Here, we
2356 * act on the ioctl, but then lie and say we didn't
2357 * so the line discipline will process the flush
2360 if (cmd == TCSETSF) {
2362 ch->ch_flags &= ~CH_STOP;
2363 ch->ch_r_head = ch->ch_r_tail;
2364 ch_bd_ops->flush_uart_read(ch);
2365 /* Force queue flow control to be released, if needed */
2366 dgnc_check_queue_flow_control(ch);
2369 /* now wait for all the output to drain */
2370 spin_unlock_irqrestore(&ch->ch_lock, flags);
2371 rc = ch_bd_ops->drain(tty, 0);
2375 /* pretend we didn't recognize this */
2376 return -ENOIOCTLCMD;
2380 spin_unlock_irqrestore(&ch->ch_lock, flags);
2381 rc = ch_bd_ops->drain(tty, 0);
2385 /* pretend we didn't recognize this */
2386 return -ENOIOCTLCMD;
2389 spin_unlock_irqrestore(&ch->ch_lock, flags);
2390 /* Make the ld do it */
2391 return -ENOIOCTLCMD;
2394 /* get information for ditty */
2395 spin_unlock_irqrestore(&ch->ch_lock, flags);
2396 return dgnc_tty_digigeta(tty, uarg);
2401 /* set information for ditty */
2402 if (cmd == (DIGI_SETAW)) {
2403 spin_unlock_irqrestore(&ch->ch_lock, flags);
2404 rc = ch_bd_ops->drain(tty, 0);
2408 spin_lock_irqsave(&ch->ch_lock, flags);
2410 tty_ldisc_flush(tty);
2415 spin_unlock_irqrestore(&ch->ch_lock, flags);
2416 return dgnc_tty_digiseta(tty, uarg);
2422 * Let go of locks when accessing user space,
2425 spin_unlock_irqrestore(&ch->ch_lock, flags);
2426 rc = get_user(loopback, (unsigned int __user *)arg);
2429 spin_lock_irqsave(&ch->ch_lock, flags);
2431 /* Enable/disable internal loopback for this port */
2433 ch->ch_flags |= CH_LOOPBACK;
2435 ch->ch_flags &= ~(CH_LOOPBACK);
2437 ch_bd_ops->param(tty);
2438 spin_unlock_irqrestore(&ch->ch_lock, flags);
2442 case DIGI_GETCUSTOMBAUD:
2443 spin_unlock_irqrestore(&ch->ch_lock, flags);
2444 return put_user(ch->ch_custom_speed,
2445 (unsigned int __user *)arg);
2447 case DIGI_SETCUSTOMBAUD:
2451 spin_unlock_irqrestore(&ch->ch_lock, flags);
2452 rc = get_user(new_rate, (int __user *)arg);
2455 spin_lock_irqsave(&ch->ch_lock, flags);
2456 dgnc_set_custom_speed(ch, new_rate);
2457 ch_bd_ops->param(tty);
2458 spin_unlock_irqrestore(&ch->ch_lock, flags);
2463 * This ioctl allows insertion of a character into the front
2464 * of any pending data to be transmitted.
2466 * This ioctl is to satisfy the "Send Character Immediate"
2467 * call that the RealPort protocol spec requires.
2469 case DIGI_REALPORT_SENDIMMEDIATE:
2473 spin_unlock_irqrestore(&ch->ch_lock, flags);
2474 rc = get_user(c, (unsigned char __user *)arg);
2477 spin_lock_irqsave(&ch->ch_lock, flags);
2478 ch_bd_ops->send_immediate_char(ch, c);
2479 spin_unlock_irqrestore(&ch->ch_lock, flags);
2484 * This ioctl returns all the current counts for the port.
2486 * This ioctl is to satisfy the "Line Error Counters"
2487 * call that the RealPort protocol spec requires.
2489 case DIGI_REALPORT_GETCOUNTERS:
2491 struct digi_getcounter buf;
2493 buf.norun = ch->ch_err_overrun;
2494 buf.noflow = 0; /* The driver doesn't keep this stat */
2495 buf.nframe = ch->ch_err_frame;
2496 buf.nparity = ch->ch_err_parity;
2497 buf.nbreak = ch->ch_err_break;
2498 buf.rbytes = ch->ch_rxcount;
2499 buf.tbytes = ch->ch_txcount;
2501 spin_unlock_irqrestore(&ch->ch_lock, flags);
2503 if (copy_to_user(uarg, &buf, sizeof(buf)))
2510 * This ioctl returns all current events.
2512 * This ioctl is to satisfy the "Event Reporting"
2513 * call that the RealPort protocol spec requires.
2515 case DIGI_REALPORT_GETEVENTS:
2517 unsigned int events = 0;
2519 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2520 if (ch->ch_flags & CH_BREAK_SENDING)
2522 if ((ch->ch_flags & CH_STOP) ||
2523 (ch->ch_flags & CH_FORCED_STOP))
2524 events |= (EV_OPU | EV_OPS);
2526 if ((ch->ch_flags & CH_STOPI) ||
2527 (ch->ch_flags & CH_FORCED_STOPI))
2528 events |= (EV_IPU | EV_IPS);
2530 spin_unlock_irqrestore(&ch->ch_lock, flags);
2531 return put_user(events, (unsigned int __user *)arg);
2535 * This ioctl returns TOUT and TIN counters based
2536 * upon the values passed in by the RealPort Server.
2537 * It also passes back whether the UART Transmitter is
2540 case DIGI_REALPORT_GETBUFFERS:
2542 struct digi_getbuffer buf;
2546 spin_unlock_irqrestore(&ch->ch_lock, flags);
2548 if (copy_from_user(&buf, uarg, sizeof(buf)))
2551 spin_lock_irqsave(&ch->ch_lock, flags);
2553 /* Figure out how much data is in our RX and TX queues. */
2555 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
2556 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
2559 * Is the UART empty?
2560 * Add that value to whats in our TX queue.
2563 count = buf.txbuf + ch_bd_ops->get_uart_bytes_left(ch);
2566 * Figure out how much data the RealPort Server believes should
2567 * be in our TX queue.
2569 tdist = (buf.tx_in - buf.tx_out) & 0xffff;
2572 * If we have more data than the RealPort Server believes we
2573 * should have, reduce our count to its amount.
2575 * This count difference CAN happen because the Linux LD can
2576 * insert more characters into our queue for OPOST processing
2577 * that the RealPort Server doesn't know about.
2579 if (buf.txbuf > tdist)
2582 /* Report whether our queue and UART TX are completely empty. */
2589 spin_unlock_irqrestore(&ch->ch_lock, flags);
2591 if (copy_to_user(uarg, &buf, sizeof(buf)))
2597 spin_unlock_irqrestore(&ch->ch_lock, flags);
2599 return -ENOIOCTLCMD;
2602 spin_unlock_irqrestore(&ch->ch_lock, flags);