1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2003 Digi International (www.digi.com)
4 * Scott H Kilau <Scott_Kilau at digi dot com>
8 * This file implements the tty driver functionality for the
9 * Neo and ClassicBoard PCI based product lines.
12 #include <linux/kernel.h>
13 #include <linux/sched/signal.h> /* For jiffies, task states, etc. */
14 #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
15 #include <linux/module.h>
16 #include <linux/ctype.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/types.h>
20 #include <linux/serial_reg.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h> /* For udelay */
23 #include <linux/uaccess.h> /* For copy_from_user/copy_to_user */
24 #include <linux/pci.h>
25 #include "dgnc_driver.h"
29 /* Default transparent print information. */
31 static const struct digi_t dgnc_digi_init = {
32 .digi_flags = DIGI_COOK, /* Flags */
33 .digi_maxcps = 100, /* Max CPS */
34 .digi_maxchar = 50, /* Max chars in print queue */
35 .digi_bufsize = 100, /* Printer buffer size */
36 .digi_onlen = 4, /* size of printer on string */
37 .digi_offlen = 4, /* size of printer off string */
38 .digi_onstr = "\033[5i", /* ANSI printer on string ] */
39 .digi_offstr = "\033[4i", /* ANSI printer off string ] */
40 .digi_term = "ansi" /* default terminal type */
43 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
44 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
45 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file,
46 struct channel_t *ch);
47 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
49 static int dgnc_tty_digigeta(struct tty_struct *tty,
50 struct digi_t __user *retinfo);
51 static int dgnc_tty_digiseta(struct tty_struct *tty,
52 struct digi_t __user *new_info);
53 static int dgnc_tty_write_room(struct tty_struct *tty);
54 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
55 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
56 static void dgnc_tty_start(struct tty_struct *tty);
57 static void dgnc_tty_stop(struct tty_struct *tty);
58 static void dgnc_tty_throttle(struct tty_struct *tty);
59 static void dgnc_tty_unthrottle(struct tty_struct *tty);
60 static void dgnc_tty_flush_chars(struct tty_struct *tty);
61 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
62 static void dgnc_tty_hangup(struct tty_struct *tty);
63 static int dgnc_set_modem_info(struct channel_t *ch, unsigned int command,
64 unsigned int __user *value);
65 static int dgnc_get_modem_info(struct channel_t *ch,
66 unsigned int __user *value);
67 static int dgnc_tty_tiocmget(struct tty_struct *tty);
68 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set,
70 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
71 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
72 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf,
74 static void dgnc_tty_set_termios(struct tty_struct *tty,
75 struct ktermios *old_termios);
76 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
77 static void dgnc_set_signal_low(struct channel_t *ch, const unsigned char line);
78 static void dgnc_wake_up_unit(struct un_t *unit);
80 static const struct tty_operations dgnc_tty_ops = {
81 .open = dgnc_tty_open,
82 .close = dgnc_tty_close,
83 .write = dgnc_tty_write,
84 .write_room = dgnc_tty_write_room,
85 .flush_buffer = dgnc_tty_flush_buffer,
86 .chars_in_buffer = dgnc_tty_chars_in_buffer,
87 .flush_chars = dgnc_tty_flush_chars,
88 .ioctl = dgnc_tty_ioctl,
89 .set_termios = dgnc_tty_set_termios,
90 .stop = dgnc_tty_stop,
91 .start = dgnc_tty_start,
92 .throttle = dgnc_tty_throttle,
93 .unthrottle = dgnc_tty_unthrottle,
94 .hangup = dgnc_tty_hangup,
95 .put_char = dgnc_tty_put_char,
96 .tiocmget = dgnc_tty_tiocmget,
97 .tiocmset = dgnc_tty_tiocmset,
98 .break_ctl = dgnc_tty_send_break,
99 .wait_until_sent = dgnc_tty_wait_until_sent,
100 .send_xchar = dgnc_tty_send_xchar
103 /* TTY Initialization/Cleanup Functions */
105 static struct tty_driver *dgnc_tty_create(char *serial_name, uint maxports,
106 int major, int minor)
109 struct tty_driver *drv;
111 drv = tty_alloc_driver(maxports,
112 TTY_DRIVER_REAL_RAW |
113 TTY_DRIVER_DYNAMIC_DEV |
114 TTY_DRIVER_HARDWARE_BREAK);
118 drv->name = serial_name;
121 drv->minor_start = minor;
122 drv->type = TTY_DRIVER_TYPE_SERIAL;
123 drv->subtype = SERIAL_TYPE_NORMAL;
124 drv->init_termios = tty_std_termios;
125 drv->init_termios.c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL);
126 drv->init_termios.c_ispeed = 9600;
127 drv->init_termios.c_ospeed = 9600;
128 drv->driver_name = DRVSTR;
130 * Entry points for driver. Called by the kernel from
131 * tty_io.c and n_tty.c.
133 tty_set_operations(drv, &dgnc_tty_ops);
134 rc = tty_register_driver(drv);
142 static void dgnc_tty_free(struct tty_driver *drv)
144 tty_unregister_driver(drv);
149 * dgnc_tty_register() - Init the tty subsystem for this board.
151 int dgnc_tty_register(struct dgnc_board *brd)
155 snprintf(brd->serial_name, MAXTTYNAMELEN, "tty_dgnc_%d_",
158 brd->serial_driver = dgnc_tty_create(brd->serial_name,
159 brd->maxports, 0, 0);
160 if (IS_ERR(brd->serial_driver)) {
161 rc = PTR_ERR(brd->serial_driver);
162 dev_dbg(&brd->pdev->dev, "Can't register tty device (%d)\n",
167 snprintf(brd->print_name, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
168 brd->print_driver = dgnc_tty_create(brd->print_name, brd->maxports,
170 brd->serial_driver->major);
171 if (IS_ERR(brd->print_driver)) {
172 rc = PTR_ERR(brd->print_driver);
173 dev_dbg(&brd->pdev->dev,
174 "Can't register Transparent Print device(%d)\n", rc);
175 dgnc_tty_free(brd->serial_driver);
181 void dgnc_tty_unregister(struct dgnc_board *brd)
183 dgnc_tty_free(brd->print_driver);
184 dgnc_tty_free(brd->serial_driver);
188 * dgnc_tty_init() - Initialize the tty subsystem.
190 * Called once per board after board has been downloaded and initialized.
192 int dgnc_tty_init(struct dgnc_board *brd)
197 struct channel_t *ch;
202 /* Initialize board structure elements. */
204 vaddr = brd->re_map_membase;
206 brd->nasync = brd->maxports;
208 for (i = 0; i < brd->nasync; i++) {
209 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]),
211 if (!brd->channels[i]) {
213 goto err_free_channels;
217 ch = brd->channels[0];
218 vaddr = brd->re_map_membase;
220 /* Set up channel variables */
221 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
222 spin_lock_init(&ch->ch_lock);
224 ch->ch_tun.un_ch = ch;
225 ch->ch_tun.un_type = DGNC_SERIAL;
226 ch->ch_tun.un_dev = i;
228 ch->ch_pun.un_ch = ch;
229 ch->ch_pun.un_type = DGNC_PRINT;
230 ch->ch_pun.un_dev = i + 128;
232 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
236 ch->ch_digi = dgnc_digi_init;
238 /* .25 second delay */
239 ch->ch_close_delay = 250;
241 init_waitqueue_head(&ch->ch_flags_wait);
242 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
243 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
246 struct device *classp;
248 classp = tty_register_device(brd->serial_driver, i,
249 &ch->ch_bd->pdev->dev);
250 ch->ch_tun.un_sysfs = classp;
252 classp = tty_register_device(brd->print_driver, i,
253 &ch->ch_bd->pdev->dev);
254 ch->ch_pun.un_sysfs = classp;
261 for (i = i - 1; i >= 0; --i) {
262 kfree(brd->channels[i]);
263 brd->channels[i] = NULL;
270 * dgnc_cleanup_tty() - Cleanup driver.
272 * Uninitialize the TTY portion of this driver. Free all memory and
275 void dgnc_cleanup_tty(struct dgnc_board *brd)
279 for (i = 0; i < brd->nasync; i++)
280 tty_unregister_device(brd->serial_driver, i);
282 tty_unregister_driver(brd->serial_driver);
284 for (i = 0; i < brd->nasync; i++)
285 tty_unregister_device(brd->print_driver, i);
287 tty_unregister_driver(brd->print_driver);
289 put_tty_driver(brd->serial_driver);
290 put_tty_driver(brd->print_driver);
294 * dgnc_wmove() - Write data to transmit queue.
295 * @ch: Pointer to channel structure.
296 * @buf: Pointer to characters to be moved.
297 * @n: Number of characters to move.
299 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
307 head = ch->ch_w_head & WQUEUEMASK;
310 * If the write wraps over the top of the circular buffer,
311 * move the portion up to the wrap point, and reset the
312 * pointers to the bottom.
314 remain = WQUEUESIZE - head;
318 memcpy(ch->ch_wqueue + head, buf, remain);
324 /* Move rest of data. */
326 memcpy(ch->ch_wqueue + head, buf, remain);
331 ch->ch_w_head = head;
335 * dgnc_input() - Process received data.
336 * @ch: Pointer to channel structure.
338 void dgnc_input(struct channel_t *ch)
340 struct dgnc_board *bd;
341 struct tty_struct *tp;
342 struct tty_ldisc *ld = NULL;
357 tp = ch->ch_tun.un_tty;
363 spin_lock_irqsave(&ch->ch_lock, flags);
366 head = ch->ch_r_head & rmask;
367 tail = ch->ch_r_tail & rmask;
368 data_len = (head - tail) & rmask;
374 * If the device is not open, or CREAD is off,
375 * flush input data and return immediately.
378 !(ch->ch_tun.un_flags & UN_ISOPEN) ||
380 (ch->ch_tun.un_flags & UN_CLOSING)) {
381 ch->ch_r_head = tail;
383 /* Force queue flow control to be released, if needed */
384 dgnc_check_queue_flow_control(ch);
389 if (ch->ch_flags & CH_FORCED_STOPI)
392 flip_len = TTY_FLIPBUF_SIZE;
394 len = min(data_len, flip_len);
395 len = min(len, (N_TTY_BUF_SIZE - 1));
397 ld = tty_ldisc_ref(tp);
401 if (!ld->ops->receive_buf) {
402 ch->ch_r_head = ch->ch_r_tail;
411 * The tty layer in the kernel has changed in 2.6.16+.
413 * The flip buffers in the tty structure are no longer exposed,
414 * and probably will be going away eventually.
416 * If we are completely raw, we don't need to go through a lot
417 * of the tty layers that exist.
418 * In this case, we take the shortest and fastest route we
419 * can to relay the data to the user.
421 * On the other hand, if we are not raw, we need to go through
422 * the new 2.6.16+ tty layer, which has its API more well defined.
424 len = tty_buffer_request_room(tp->port, len);
428 * n now contains the most amount of data we can copy,
429 * bounded either by how much the Linux tty layer can handle,
430 * or the amount of data the card actually has pending...
433 unsigned char *ch_pos = ch->ch_equeue + tail;
435 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
442 * If conditions are such that ld needs to see all
443 * UART errors, we will have to walk each character
444 * and error byte and send them to the buffer one at
447 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
448 for (i = 0; i < s; i++) {
449 unsigned char ch = *(ch_pos + i);
450 char flag = TTY_NORMAL;
452 if (ch & UART_LSR_BI)
454 else if (ch & UART_LSR_PE)
456 else if (ch & UART_LSR_FE)
459 tty_insert_flip_char(tp->port, ch, flag);
462 tty_insert_flip_string(tp->port, ch_pos, s);
467 /* Flip queue if needed */
471 ch->ch_r_tail = tail & rmask;
472 ch->ch_e_tail = tail & rmask;
473 dgnc_check_queue_flow_control(ch);
474 spin_unlock_irqrestore(&ch->ch_lock, flags);
476 /* Tell the tty layer its okay to "eat" the data now */
477 tty_flip_buffer_push(tp->port);
484 spin_unlock_irqrestore(&ch->ch_lock, flags);
492 * Determines when CARRIER changes state and takes appropriate
495 void dgnc_carrier(struct channel_t *ch)
497 int virt_carrier = 0;
498 int phys_carrier = 0;
503 if (ch->ch_mistat & UART_MSR_DCD)
506 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
509 if (ch->ch_c_cflag & CLOCAL)
512 /* Test for a VIRTUAL carrier transition to HIGH. */
514 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
516 * When carrier rises, wake any threads waiting
517 * for carrier in the open routine.
519 if (waitqueue_active(&ch->ch_flags_wait))
520 wake_up_interruptible(&ch->ch_flags_wait);
523 /* Test for a PHYSICAL carrier transition to HIGH. */
525 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
527 * When carrier rises, wake any threads waiting
528 * for carrier in the open routine.
530 if (waitqueue_active(&ch->ch_flags_wait))
531 wake_up_interruptible(&ch->ch_flags_wait);
535 * Test for a PHYSICAL transition to low, so long as we aren't
536 * currently ignoring physical transitions (which is what "virtual
537 * carrier" indicates).
539 * The transition of the virtual carrier to low really doesn't
540 * matter... it really only means "ignore carrier state", not
541 * "make pretend that carrier is there".
543 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
544 (phys_carrier == 0)) {
546 * When carrier drops:
548 * Drop carrier on all open units.
550 * Flush queues, waking up any task waiting in the
553 * Send a hangup to the control terminal.
555 * Enable all select calls.
557 if (waitqueue_active(&ch->ch_flags_wait))
558 wake_up_interruptible(&ch->ch_flags_wait);
560 if (ch->ch_tun.un_open_count > 0)
561 tty_hangup(ch->ch_tun.un_tty);
563 if (ch->ch_pun.un_open_count > 0)
564 tty_hangup(ch->ch_pun.un_tty);
567 /* Make sure that our cached values reflect the current reality. */
569 if (virt_carrier == 1)
570 ch->ch_flags |= CH_FCAR;
572 ch->ch_flags &= ~CH_FCAR;
574 if (phys_carrier == 1)
575 ch->ch_flags |= CH_CD;
577 ch->ch_flags &= ~CH_CD;
580 /* Assign the custom baud rate to the channel structure */
581 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
590 ch->ch_custom_speed = 0;
595 * Since the divisor is stored in a 16-bit integer, we make sure
596 * we don't allow any rates smaller than a 16-bit integer would allow.
597 * And of course, rates above the dividend won't fly.
599 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
600 newrate = (ch->ch_bd->bd_dividend / 0xFFFF) + 1;
602 if (newrate && newrate > ch->ch_bd->bd_dividend)
603 newrate = ch->ch_bd->bd_dividend;
606 testdiv = ch->ch_bd->bd_dividend / newrate;
609 * If we try to figure out what rate the board would use
610 * with the test divisor, it will be either equal or higher
611 * than the requested baud rate. If we then determine the
612 * rate with a divisor one higher, we will get the next lower
613 * supported rate below the requested.
615 testrate_high = ch->ch_bd->bd_dividend / testdiv;
616 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
619 * If the rate for the requested divisor is correct, just
620 * use it and be done.
622 if (testrate_high != newrate) {
624 * Otherwise, pick the rate that is closer
625 * (i.e. whichever rate has a smaller delta).
627 deltahigh = testrate_high - newrate;
628 deltalow = newrate - testrate_low;
630 if (deltahigh < deltalow)
631 newrate = testrate_high;
633 newrate = testrate_low;
637 ch->ch_custom_speed = newrate;
640 void dgnc_check_queue_flow_control(struct channel_t *ch)
644 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
646 qleft += RQUEUEMASK + 1;
649 * Check to see if we should enforce flow control on our queue because
650 * the ld (or user) isn't reading data out of our queue fast enuf.
652 * NOTE: This is done based on what the current flow control of the
655 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
656 * This will cause the UART's FIFO to back up, and force
657 * the RTS signal to be dropped.
658 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
659 * the other side, in hopes it will stop sending data to us.
660 * 3) NONE - Nothing we can do. We will simply drop any extra data
661 * that gets sent into us when the queue fills up.
665 if (ch->ch_digi.digi_flags & CTSPACE ||
666 ch->ch_c_cflag & CRTSCTS) {
667 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
668 ch->ch_bd->bd_ops->disable_receiver(ch);
669 ch->ch_flags |= (CH_RECEIVER_OFF);
673 else if (ch->ch_c_iflag & IXOFF) {
674 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
675 ch->ch_bd->bd_ops->send_stop_character(ch);
682 * Check to see if we should unenforce flow control because
683 * ld (or user) finally read enuf data out of our queue.
685 * NOTE: This is done based on what the current flow control of the
688 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
689 * This will cause the UART's FIFO to raise RTS back up,
690 * which will allow the other side to start sending data again.
691 * 2) SWFLOW (IXOFF) - Send a start character to
692 * the other side, so it will start sending data to us again.
693 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
694 * other side, we don't need to do anything now.
696 if (qleft > (RQUEUESIZE / 2)) {
698 if (ch->ch_digi.digi_flags & RTSPACE ||
699 ch->ch_c_cflag & CRTSCTS) {
700 if (ch->ch_flags & CH_RECEIVER_OFF) {
701 ch->ch_bd->bd_ops->enable_receiver(ch);
702 ch->ch_flags &= ~(CH_RECEIVER_OFF);
706 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
707 ch->ch_stops_sent = 0;
708 ch->ch_bd->bd_ops->send_start_character(ch);
713 static void dgnc_set_signal_low(struct channel_t *ch, const unsigned char sig)
715 ch->ch_mostat &= ~(sig);
716 ch->ch_bd->bd_ops->assert_modem_signals(ch);
719 void dgnc_wakeup_writes(struct channel_t *ch)
727 spin_lock_irqsave(&ch->ch_lock, flags);
729 /* If channel now has space, wake up anyone waiting on the condition. */
731 qlen = ch->ch_w_head - ch->ch_w_tail;
735 if (qlen >= (WQUEUESIZE - 256)) {
736 spin_unlock_irqrestore(&ch->ch_lock, flags);
740 if (ch->ch_tun.un_flags & UN_ISOPEN) {
741 tty_wakeup(ch->ch_tun.un_tty);
744 * If unit is set to wait until empty, check to make sure
745 * the queue AND FIFO are both empty.
747 if (ch->ch_tun.un_flags & UN_EMPTY) {
749 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
750 ch->ch_tun.un_flags &= ~(UN_EMPTY);
753 * If RTS Toggle mode is on, whenever
754 * the queue and UART is empty, keep RTS low.
756 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
757 dgnc_set_signal_low(ch, UART_MCR_RTS);
760 * If DTR Toggle mode is on, whenever
761 * the queue and UART is empty, keep DTR low.
763 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE)
764 dgnc_set_signal_low(ch, UART_MCR_DTR);
768 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
771 if (ch->ch_pun.un_flags & UN_ISOPEN) {
772 tty_wakeup(ch->ch_pun.un_tty);
775 * If unit is set to wait until empty, check to make sure
776 * the queue AND FIFO are both empty.
778 if (ch->ch_pun.un_flags & UN_EMPTY) {
780 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
781 ch->ch_pun.un_flags &= ~(UN_EMPTY);
784 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
787 spin_unlock_irqrestore(&ch->ch_lock, flags);
790 static struct dgnc_board *find_board_by_major(unsigned int major)
794 for (i = 0; i < MAXBOARDS; i++) {
795 struct dgnc_board *brd = dgnc_board[i];
800 if (major == brd->serial_driver->major ||
801 major == brd->print_driver->major)
808 /* TTY Entry points and helper functions */
810 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
812 struct dgnc_board *brd;
813 struct channel_t *ch;
822 major = MAJOR(tty_devnum(tty));
823 minor = MINOR(tty_devnum(tty));
828 brd = find_board_by_major(major);
832 rc = wait_event_interruptible(brd->state_wait,
833 (brd->state & BOARD_READY));
837 spin_lock_irqsave(&brd->bd_lock, flags);
839 if (PORT_NUM(minor) >= brd->nasync) {
844 ch = brd->channels[PORT_NUM(minor)];
850 spin_unlock_irqrestore(&brd->bd_lock, flags);
852 spin_lock_irqsave(&ch->ch_lock, flags);
854 /* Figure out our type */
855 if (!IS_PRINT(minor)) {
856 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
857 un->un_type = DGNC_SERIAL;
858 } else if (IS_PRINT(minor)) {
859 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
860 un->un_type = DGNC_PRINT;
867 * If the port is still in a previous open, and in a state
868 * where we simply cannot safely keep going, wait until the
871 spin_unlock_irqrestore(&ch->ch_lock, flags);
873 rc = wait_event_interruptible(ch->ch_flags_wait,
874 ((ch->ch_flags & CH_OPENING) == 0));
875 /* If ret is non-zero, user ctrl-c'ed us */
880 * If either unit is in the middle of the fragile part of close,
881 * we just cannot touch the channel safely.
882 * Go to sleep, knowing that when the channel can be
883 * touched safely, the close routine will signal the
884 * ch_flags_wait to wake us back up.
886 rc = wait_event_interruptible(ch->ch_flags_wait,
887 !((ch->ch_tun.un_flags |
888 ch->ch_pun.un_flags) & UN_CLOSING));
889 /* If ret is non-zero, user ctrl-c'ed us */
893 spin_lock_irqsave(&ch->ch_lock, flags);
895 tty->driver_data = un;
897 /* Initialize tty's */
899 if (!(un->un_flags & UN_ISOPEN)) {
902 /* Maybe do something here to the TTY struct as well? */
906 * Allocate channel buffers for read/write/error.
907 * Set flag, so we don't get trounced on.
909 ch->ch_flags |= (CH_OPENING);
911 spin_unlock_irqrestore(&ch->ch_lock, flags);
914 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
916 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
918 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
920 if (!ch->ch_rqueue || !ch->ch_equeue || !ch->ch_wqueue) {
921 kfree(ch->ch_rqueue);
922 kfree(ch->ch_equeue);
923 kfree(ch->ch_wqueue);
927 spin_lock_irqsave(&ch->ch_lock, flags);
929 ch->ch_flags &= ~(CH_OPENING);
930 wake_up_interruptible(&ch->ch_flags_wait);
932 /* Initialize if neither terminal or printer is open. */
934 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
935 /* Flush input queues. */
943 brd->bd_ops->flush_uart_write(ch);
944 brd->bd_ops->flush_uart_read(ch);
947 ch->ch_cached_lsr = 0;
948 ch->ch_stop_sending_break = 0;
949 ch->ch_stops_sent = 0;
951 ch->ch_c_cflag = tty->termios.c_cflag;
952 ch->ch_c_iflag = tty->termios.c_iflag;
953 ch->ch_c_oflag = tty->termios.c_oflag;
954 ch->ch_c_lflag = tty->termios.c_lflag;
955 ch->ch_startc = tty->termios.c_cc[VSTART];
956 ch->ch_stopc = tty->termios.c_cc[VSTOP];
959 * Bring up RTS and DTR...
960 * Also handle RTS or DTR toggle if set.
962 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
963 ch->ch_mostat |= (UART_MCR_RTS);
964 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
965 ch->ch_mostat |= (UART_MCR_DTR);
967 /* Tell UART to init itself */
968 brd->bd_ops->uart_init(ch);
971 brd->bd_ops->param(tty);
975 spin_unlock_irqrestore(&ch->ch_lock, flags);
977 rc = dgnc_block_til_ready(tty, file, ch);
979 spin_lock_irqsave(&ch->ch_lock, flags);
982 un->un_flags |= (UN_ISOPEN);
983 spin_unlock_irqrestore(&ch->ch_lock, flags);
988 spin_unlock_irqrestore(&brd->bd_lock, flags);
992 spin_unlock_irqrestore(&ch->ch_lock, flags);
997 /* Wait for DCD, if needed. */
998 static int dgnc_block_til_ready(struct tty_struct *tty,
1000 struct channel_t *ch)
1003 struct un_t *un = tty->driver_data;
1004 unsigned long flags;
1006 int sleep_on_un_flags = 0;
1011 spin_lock_irqsave(&ch->ch_lock, flags);
1016 sleep_on_un_flags = 0;
1018 if (ch->ch_bd->state == BOARD_FAILED) {
1023 if (tty_hung_up_p(file)) {
1029 * If either unit is in the middle of the fragile part of close,
1030 * we just cannot touch the channel safely.
1031 * Go back to sleep, knowing that when the channel can be
1032 * touched safely, the close routine will signal the
1033 * ch_wait_flags to wake us back up.
1035 if (!((ch->ch_tun.un_flags |
1036 ch->ch_pun.un_flags) &
1039 * Our conditions to leave cleanly and happily:
1040 * 1) NONBLOCKING on the tty is set.
1042 * 3) DCD (fake or real) is active.
1045 if (file->f_flags & O_NONBLOCK)
1048 if (tty_io_error(tty)) {
1053 if (ch->ch_flags & CH_CD)
1056 if (ch->ch_flags & CH_FCAR)
1059 sleep_on_un_flags = 1;
1063 * If there is a signal pending, the user probably
1064 * interrupted (ctrl-c) us.
1066 if (signal_pending(current)) {
1071 if (sleep_on_un_flags)
1072 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1074 old_flags = ch->ch_flags;
1077 * Let go of channel lock before calling schedule.
1078 * Our poller will get any FEP events and wake us up when DCD
1079 * eventually goes active.
1082 spin_unlock_irqrestore(&ch->ch_lock, flags);
1085 * Wait for something in the flags to change
1086 * from the current value.
1088 if (sleep_on_un_flags)
1089 rc = wait_event_interruptible
1091 (old_flags != (ch->ch_tun.un_flags |
1092 ch->ch_pun.un_flags)));
1094 rc = wait_event_interruptible(
1096 (old_flags != ch->ch_flags));
1099 * We got woken up for some reason.
1100 * Before looping around, grab our channel lock.
1102 spin_lock_irqsave(&ch->ch_lock, flags);
1107 spin_unlock_irqrestore(&ch->ch_lock, flags);
1112 /* Hangup the port. Like a close, but don't wait for output to drain. */
1113 static void dgnc_tty_hangup(struct tty_struct *tty)
1118 /* flush the transmit queues */
1119 dgnc_tty_flush_buffer(tty);
1122 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1124 struct dgnc_board *bd;
1125 struct channel_t *ch;
1127 unsigned long flags;
1132 un = tty->driver_data;
1144 spin_lock_irqsave(&ch->ch_lock, flags);
1147 * Determine if this is the last close or not - and if we agree about
1148 * which type of close it is with the Line Discipline
1150 if ((tty->count == 1) && (un->un_open_count != 1)) {
1152 * Uh, oh. tty->count is 1, which means that the tty
1153 * structure will be freed. un_open_count should always
1154 * be one in these conditions. If it's greater than
1155 * one, we've got real problems, since it means the
1156 * serial port won't be shutdown.
1159 "tty->count is 1, un open count is %d\n",
1161 un->un_open_count = 1;
1164 if (un->un_open_count)
1165 un->un_open_count--;
1168 "bad serial port open count of %d\n",
1171 ch->ch_open_count--;
1173 if (ch->ch_open_count && un->un_open_count) {
1174 spin_unlock_irqrestore(&ch->ch_lock, flags);
1178 /* OK, its the last close on the unit */
1179 un->un_flags |= UN_CLOSING;
1184 * Only officially close channel if count is 0 and
1185 * DIGI_PRINTER bit is not set.
1187 if ((ch->ch_open_count == 0) &&
1188 !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1189 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1191 /* turn off print device when closing print device. */
1193 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1194 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1195 (int)ch->ch_digi.digi_offlen);
1196 ch->ch_flags &= ~CH_PRON;
1199 spin_unlock_irqrestore(&ch->ch_lock, flags);
1200 /* wait for output to drain */
1201 /* This will also return if we take an interrupt */
1203 bd->bd_ops->drain(tty, 0);
1205 dgnc_tty_flush_buffer(tty);
1206 tty_ldisc_flush(tty);
1208 spin_lock_irqsave(&ch->ch_lock, flags);
1212 /* If we have HUPCL set, lower DTR and RTS */
1214 if (ch->ch_c_cflag & HUPCL) {
1216 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1217 bd->bd_ops->assert_modem_signals(ch);
1220 * Go to sleep to ensure RTS/DTR
1221 * have been dropped for modems to see it.
1223 if (ch->ch_close_delay) {
1224 spin_unlock_irqrestore(&ch->ch_lock,
1226 msleep_interruptible(ch->ch_close_delay);
1227 spin_lock_irqsave(&ch->ch_lock, flags);
1231 ch->ch_old_baud = 0;
1233 /* Turn off UART interrupts for this port */
1234 ch->ch_bd->bd_ops->uart_off(ch);
1236 /* turn off print device when closing print device. */
1238 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1239 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1240 (int)ch->ch_digi.digi_offlen);
1241 ch->ch_flags &= ~CH_PRON;
1246 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1248 wake_up_interruptible(&ch->ch_flags_wait);
1249 wake_up_interruptible(&un->un_flags_wait);
1251 spin_unlock_irqrestore(&ch->ch_lock, flags);
1255 * Return number of characters that have not been transmitted yet.
1257 * This routine is used by the line discipline to determine if there
1258 * is data waiting to be transmitted/drained/flushed or not.
1260 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1262 struct channel_t *ch = NULL;
1263 struct un_t *un = NULL;
1268 unsigned long flags;
1273 un = tty->driver_data;
1281 spin_lock_irqsave(&ch->ch_lock, flags);
1284 thead = ch->ch_w_head & tmask;
1285 ttail = ch->ch_w_tail & tmask;
1287 spin_unlock_irqrestore(&ch->ch_lock, flags);
1291 else if (thead > ttail)
1292 chars = thead - ttail;
1294 chars = thead - ttail + WQUEUESIZE;
1300 * Reduces bytes_available to the max number of characters
1301 * that can be sent currently given the maxcps value, and
1302 * returns the new bytes_available. This only affects printer
1305 static int dgnc_maxcps_room(struct channel_t *ch, int bytes_available)
1307 int rc = bytes_available;
1309 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1311 unsigned long current_time = jiffies;
1312 unsigned long buffer_time = current_time +
1313 (HZ * ch->ch_digi.digi_bufsize) /
1314 ch->ch_digi.digi_maxcps;
1316 if (ch->ch_cpstime < current_time) {
1317 /* buffer is empty */
1318 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1319 cps_limit = ch->ch_digi.digi_bufsize;
1320 } else if (ch->ch_cpstime < buffer_time) {
1321 /* still room in the buffer */
1322 cps_limit = ((buffer_time - ch->ch_cpstime) *
1323 ch->ch_digi.digi_maxcps) / HZ;
1325 /* no room in the buffer */
1329 rc = min(cps_limit, bytes_available);
1335 /* Return room available in Tx buffer */
1336 static int dgnc_tty_write_room(struct tty_struct *tty)
1338 struct channel_t *ch = NULL;
1339 struct un_t *un = NULL;
1344 unsigned long flags;
1349 un = tty->driver_data;
1357 spin_lock_irqsave(&ch->ch_lock, flags);
1360 head = (ch->ch_w_head) & tmask;
1361 tail = (ch->ch_w_tail) & tmask;
1363 room = tail - head - 1;
1367 /* Limit printer to maxcps */
1368 if (un->un_type != DGNC_PRINT)
1369 room = dgnc_maxcps_room(ch, room);
1372 * If we are printer device, leave room for
1373 * possibly both the on and off strings.
1375 if (un->un_type == DGNC_PRINT) {
1376 if (!(ch->ch_flags & CH_PRON))
1377 room -= ch->ch_digi.digi_onlen;
1378 room -= ch->ch_digi.digi_offlen;
1380 if (ch->ch_flags & CH_PRON)
1381 room -= ch->ch_digi.digi_offlen;
1387 spin_unlock_irqrestore(&ch->ch_lock, flags);
1392 * Put a character into ch->ch_buf
1393 * Used by the line discipline for OPOST processing
1395 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1397 dgnc_tty_write(tty, &c, 1);
1402 * Take data from the user or kernel and send it out to the FEP.
1403 * In here exists all the Transparent Print magic as well.
1405 static int dgnc_tty_write(struct tty_struct *tty,
1406 const unsigned char *buf, int count)
1408 struct channel_t *ch = NULL;
1409 struct un_t *un = NULL;
1410 int bufcount = 0, n = 0;
1411 unsigned long flags;
1420 un = tty->driver_data;
1432 * Store original amount of characters passed in.
1433 * This helps to figure out if we should ask the FEP
1434 * to send us an event when it has more space available.
1437 spin_lock_irqsave(&ch->ch_lock, flags);
1440 head = (ch->ch_w_head) & tmask;
1441 tail = (ch->ch_w_tail) & tmask;
1443 bufcount = tail - head - 1;
1445 bufcount += WQUEUESIZE;
1448 * Limit printer output to maxcps overall, with bursts allowed
1449 * up to bufsize characters.
1451 if (un->un_type != DGNC_PRINT)
1452 bufcount = dgnc_maxcps_room(ch, bufcount);
1454 count = min(count, bufcount);
1459 * Output the printer ON string, if we are in terminal mode, but
1460 * need to be in printer mode.
1462 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1463 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1464 (int)ch->ch_digi.digi_onlen);
1465 head = (ch->ch_w_head) & tmask;
1466 ch->ch_flags |= CH_PRON;
1470 * On the other hand, output the printer OFF string, if we are
1471 * currently in printer mode, but need to output to the terminal.
1473 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1474 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1475 (int)ch->ch_digi.digi_offlen);
1476 head = (ch->ch_w_head) & tmask;
1477 ch->ch_flags &= ~CH_PRON;
1483 * If the write wraps over the top of the circular buffer,
1484 * move the portion up to the wrap point, and reset the
1485 * pointers to the bottom.
1487 remain = WQUEUESIZE - head;
1491 memcpy(ch->ch_wqueue + head, buf, remain);
1497 /* Move rest of data. */
1499 memcpy(ch->ch_wqueue + head, buf, remain);
1505 ch->ch_w_head = head;
1508 /* Update printer buffer empty time. */
1509 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0) &&
1510 (ch->ch_digi.digi_bufsize > 0)) {
1511 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1514 spin_unlock_irqrestore(&ch->ch_lock, flags);
1517 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1522 spin_unlock_irqrestore(&ch->ch_lock, flags);
1527 /* Return modem signals to ld. */
1528 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1530 struct channel_t *ch;
1533 unsigned char mstat = 0;
1534 unsigned long flags;
1539 un = tty->driver_data;
1547 spin_lock_irqsave(&ch->ch_lock, flags);
1549 mstat = ch->ch_mostat | ch->ch_mistat;
1551 spin_unlock_irqrestore(&ch->ch_lock, flags);
1555 if (mstat & UART_MCR_DTR)
1557 if (mstat & UART_MCR_RTS)
1559 if (mstat & UART_MSR_CTS)
1561 if (mstat & UART_MSR_DSR)
1563 if (mstat & UART_MSR_RI)
1565 if (mstat & UART_MSR_DCD)
1571 /* Set modem signals, called by ld. */
1572 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1573 unsigned int set, unsigned int clear)
1575 struct dgnc_board *bd;
1576 struct channel_t *ch;
1578 unsigned long flags;
1583 un = tty->driver_data;
1595 spin_lock_irqsave(&ch->ch_lock, flags);
1597 if (set & TIOCM_RTS)
1598 ch->ch_mostat |= UART_MCR_RTS;
1600 if (set & TIOCM_DTR)
1601 ch->ch_mostat |= UART_MCR_DTR;
1603 if (clear & TIOCM_RTS)
1604 ch->ch_mostat &= ~(UART_MCR_RTS);
1606 if (clear & TIOCM_DTR)
1607 ch->ch_mostat &= ~(UART_MCR_DTR);
1609 bd->bd_ops->assert_modem_signals(ch);
1611 spin_unlock_irqrestore(&ch->ch_lock, flags);
1616 /* Send a Break, called by ld. */
1617 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
1619 struct dgnc_board *bd;
1620 struct channel_t *ch;
1622 unsigned long flags;
1627 un = tty->driver_data;
1642 spin_lock_irqsave(&ch->ch_lock, flags);
1644 bd->bd_ops->send_break(ch, msec);
1646 spin_unlock_irqrestore(&ch->ch_lock, flags);
1651 /* wait until data has been transmitted, called by ld. */
1652 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1654 struct dgnc_board *bd;
1655 struct channel_t *ch;
1661 un = tty->driver_data;
1673 bd->bd_ops->drain(tty, 0);
1676 /* send a high priority character, called by ld. */
1677 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
1679 struct dgnc_board *bd;
1680 struct channel_t *ch;
1682 unsigned long flags;
1687 un = tty->driver_data;
1699 spin_lock_irqsave(&ch->ch_lock, flags);
1700 bd->bd_ops->send_immediate_char(ch, c);
1701 spin_unlock_irqrestore(&ch->ch_lock, flags);
1704 /* Return modem signals to ld. */
1705 static inline int dgnc_get_mstat(struct channel_t *ch)
1707 unsigned char mstat;
1708 unsigned long flags;
1714 spin_lock_irqsave(&ch->ch_lock, flags);
1716 mstat = ch->ch_mostat | ch->ch_mistat;
1718 spin_unlock_irqrestore(&ch->ch_lock, flags);
1722 if (mstat & UART_MCR_DTR)
1724 if (mstat & UART_MCR_RTS)
1726 if (mstat & UART_MSR_CTS)
1728 if (mstat & UART_MSR_DSR)
1730 if (mstat & UART_MSR_RI)
1732 if (mstat & UART_MSR_DCD)
1738 /* Return modem signals to ld. */
1739 static int dgnc_get_modem_info(struct channel_t *ch,
1740 unsigned int __user *value)
1742 return put_user(dgnc_get_mstat(ch), value);
1745 /* Set modem signals, called by ld. */
1746 static int dgnc_set_modem_info(struct channel_t *ch,
1747 unsigned int command,
1748 unsigned int __user *value)
1751 unsigned int arg = 0;
1752 unsigned long flags;
1754 rc = get_user(arg, value);
1760 if (arg & TIOCM_RTS)
1761 ch->ch_mostat |= UART_MCR_RTS;
1763 if (arg & TIOCM_DTR)
1764 ch->ch_mostat |= UART_MCR_DTR;
1769 if (arg & TIOCM_RTS)
1770 ch->ch_mostat &= ~(UART_MCR_RTS);
1772 if (arg & TIOCM_DTR)
1773 ch->ch_mostat &= ~(UART_MCR_DTR);
1779 if (arg & TIOCM_RTS)
1780 ch->ch_mostat |= UART_MCR_RTS;
1782 ch->ch_mostat &= ~(UART_MCR_RTS);
1784 if (arg & TIOCM_DTR)
1785 ch->ch_mostat |= UART_MCR_DTR;
1787 ch->ch_mostat &= ~(UART_MCR_DTR);
1795 spin_lock_irqsave(&ch->ch_lock, flags);
1797 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1799 spin_unlock_irqrestore(&ch->ch_lock, flags);
1804 /* Ioctl to get the information for ditty. */
1805 static int dgnc_tty_digigeta(struct tty_struct *tty,
1806 struct digi_t __user *retinfo)
1808 struct channel_t *ch;
1811 unsigned long flags;
1819 un = tty->driver_data;
1827 memset(&tmp, 0, sizeof(tmp));
1829 spin_lock_irqsave(&ch->ch_lock, flags);
1830 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
1831 spin_unlock_irqrestore(&ch->ch_lock, flags);
1833 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1839 /* Ioctl to set the information for ditty. */
1840 static int dgnc_tty_digiseta(struct tty_struct *tty,
1841 struct digi_t __user *new_info)
1843 struct dgnc_board *bd;
1844 struct channel_t *ch;
1846 struct digi_t new_digi;
1847 unsigned long flags;
1852 un = tty->driver_data;
1864 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
1867 spin_lock_irqsave(&ch->ch_lock, flags);
1869 /* Handle transitions to and from RTS Toggle. */
1871 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
1872 (new_digi.digi_flags & DIGI_RTS_TOGGLE))
1873 ch->ch_mostat &= ~(UART_MCR_RTS);
1874 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
1875 !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
1876 ch->ch_mostat |= (UART_MCR_RTS);
1878 /* Handle transitions to and from DTR Toggle. */
1880 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
1881 (new_digi.digi_flags & DIGI_DTR_TOGGLE))
1882 ch->ch_mostat &= ~(UART_MCR_DTR);
1883 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
1884 !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
1885 ch->ch_mostat |= (UART_MCR_DTR);
1887 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
1889 if (ch->ch_digi.digi_maxcps < 1)
1890 ch->ch_digi.digi_maxcps = 1;
1892 if (ch->ch_digi.digi_maxcps > 10000)
1893 ch->ch_digi.digi_maxcps = 10000;
1895 if (ch->ch_digi.digi_bufsize < 10)
1896 ch->ch_digi.digi_bufsize = 10;
1898 if (ch->ch_digi.digi_maxchar < 1)
1899 ch->ch_digi.digi_maxchar = 1;
1901 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
1902 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
1904 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
1905 ch->ch_digi.digi_onlen = DIGI_PLEN;
1907 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
1908 ch->ch_digi.digi_offlen = DIGI_PLEN;
1910 bd->bd_ops->param(tty);
1912 spin_unlock_irqrestore(&ch->ch_lock, flags);
1917 static void dgnc_tty_set_termios(struct tty_struct *tty,
1918 struct ktermios *old_termios)
1920 struct dgnc_board *bd;
1921 struct channel_t *ch;
1923 unsigned long flags;
1928 un = tty->driver_data;
1940 spin_lock_irqsave(&ch->ch_lock, flags);
1942 ch->ch_c_cflag = tty->termios.c_cflag;
1943 ch->ch_c_iflag = tty->termios.c_iflag;
1944 ch->ch_c_oflag = tty->termios.c_oflag;
1945 ch->ch_c_lflag = tty->termios.c_lflag;
1946 ch->ch_startc = tty->termios.c_cc[VSTART];
1947 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1949 bd->bd_ops->param(tty);
1952 spin_unlock_irqrestore(&ch->ch_lock, flags);
1955 static void dgnc_tty_throttle(struct tty_struct *tty)
1957 struct channel_t *ch;
1959 unsigned long flags;
1964 un = tty->driver_data;
1972 spin_lock_irqsave(&ch->ch_lock, flags);
1974 ch->ch_flags |= (CH_FORCED_STOPI);
1976 spin_unlock_irqrestore(&ch->ch_lock, flags);
1979 static void dgnc_tty_unthrottle(struct tty_struct *tty)
1981 struct channel_t *ch;
1983 unsigned long flags;
1988 un = tty->driver_data;
1996 spin_lock_irqsave(&ch->ch_lock, flags);
1998 ch->ch_flags &= ~(CH_FORCED_STOPI);
2000 spin_unlock_irqrestore(&ch->ch_lock, flags);
2003 static void dgnc_tty_start(struct tty_struct *tty)
2005 struct dgnc_board *bd;
2006 struct channel_t *ch;
2008 unsigned long flags;
2013 un = tty->driver_data;
2025 spin_lock_irqsave(&ch->ch_lock, flags);
2027 ch->ch_flags &= ~(CH_FORCED_STOP);
2029 spin_unlock_irqrestore(&ch->ch_lock, flags);
2032 static void dgnc_tty_stop(struct tty_struct *tty)
2034 struct dgnc_board *bd;
2035 struct channel_t *ch;
2037 unsigned long flags;
2042 un = tty->driver_data;
2054 spin_lock_irqsave(&ch->ch_lock, flags);
2056 ch->ch_flags |= (CH_FORCED_STOP);
2058 spin_unlock_irqrestore(&ch->ch_lock, flags);
2062 * Flush the cook buffer
2064 * Note to self, and any other poor souls who venture here:
2066 * flush in this case DOES NOT mean dispose of the data.
2067 * instead, it means "stop buffering and send it if you
2068 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2070 * It is also always called in interrupt context - JAR 8-Sept-99
2072 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2074 struct dgnc_board *bd;
2075 struct channel_t *ch;
2077 unsigned long flags;
2082 un = tty->driver_data;
2094 spin_lock_irqsave(&ch->ch_lock, flags);
2096 /* Do something maybe here */
2098 spin_unlock_irqrestore(&ch->ch_lock, flags);
2101 /* Flush Tx buffer (make in == out) */
2102 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2104 struct channel_t *ch;
2106 unsigned long flags;
2111 un = tty->driver_data;
2119 spin_lock_irqsave(&ch->ch_lock, flags);
2121 ch->ch_flags &= ~CH_STOP;
2123 /* Flush our write queue */
2124 ch->ch_w_head = ch->ch_w_tail;
2126 /* Flush UARTs transmit FIFO */
2127 ch->ch_bd->bd_ops->flush_uart_write(ch);
2129 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
2130 ch->ch_tun.un_flags &= ~(UN_LOW | UN_EMPTY);
2131 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2133 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
2134 ch->ch_pun.un_flags &= ~(UN_LOW | UN_EMPTY);
2135 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2138 spin_unlock_irqrestore(&ch->ch_lock, flags);
2141 /* Wakes up processes waiting in the unit's (teminal/printer) wait queue */
2142 static void dgnc_wake_up_unit(struct un_t *unit)
2144 unit->un_flags &= ~(UN_LOW | UN_EMPTY);
2145 wake_up_interruptible(&unit->un_flags_wait);
2148 /* The IOCTL function and all of its helpers */
2150 /* The usual assortment of ioctl's */
2151 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2154 struct dgnc_board *bd;
2155 struct board_ops *ch_bd_ops;
2156 struct channel_t *ch;
2159 unsigned long flags;
2160 void __user *uarg = (void __user *)arg;
2165 un = tty->driver_data;
2177 ch_bd_ops = bd->bd_ops;
2179 spin_lock_irqsave(&ch->ch_lock, flags);
2181 if (un->un_open_count <= 0) {
2187 /* Here are all the standard ioctl's that we MUST implement */
2191 * TCSBRK is SVID version: non-zero arg --> no break
2192 * this behaviour is exploited by tcdrain().
2194 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2195 * between 0.25 and 0.5 seconds so we'll ask for something
2196 * in the middle: 0.375 seconds.
2198 rc = tty_check_change(tty);
2199 spin_unlock_irqrestore(&ch->ch_lock, flags);
2203 rc = ch_bd_ops->drain(tty, 0);
2207 spin_lock_irqsave(&ch->ch_lock, flags);
2209 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2210 ch_bd_ops->send_break(ch, 250);
2212 spin_unlock_irqrestore(&ch->ch_lock, flags);
2218 * support for POSIX tcsendbreak()
2219 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2220 * between 0.25 and 0.5 seconds so we'll ask for something
2221 * in the middle: 0.375 seconds.
2223 rc = tty_check_change(tty);
2224 spin_unlock_irqrestore(&ch->ch_lock, flags);
2228 rc = ch_bd_ops->drain(tty, 0);
2232 spin_lock_irqsave(&ch->ch_lock, flags);
2234 ch_bd_ops->send_break(ch, 250);
2236 spin_unlock_irqrestore(&ch->ch_lock, flags);
2241 rc = tty_check_change(tty);
2242 spin_unlock_irqrestore(&ch->ch_lock, flags);
2246 rc = ch_bd_ops->drain(tty, 0);
2250 spin_lock_irqsave(&ch->ch_lock, flags);
2252 ch_bd_ops->send_break(ch, 250);
2254 spin_unlock_irqrestore(&ch->ch_lock, flags);
2260 spin_unlock_irqrestore(&ch->ch_lock, flags);
2265 spin_unlock_irqrestore(&ch->ch_lock, flags);
2267 return put_user(C_CLOCAL(tty) ? 1 : 0,
2268 (unsigned long __user *)arg);
2272 spin_unlock_irqrestore(&ch->ch_lock, flags);
2273 rc = get_user(arg, (unsigned long __user *)arg);
2277 spin_lock_irqsave(&ch->ch_lock, flags);
2278 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
2279 (arg ? CLOCAL : 0));
2280 ch_bd_ops->param(tty);
2281 spin_unlock_irqrestore(&ch->ch_lock, flags);
2286 spin_unlock_irqrestore(&ch->ch_lock, flags);
2287 return dgnc_get_modem_info(ch, uarg);
2292 spin_unlock_irqrestore(&ch->ch_lock, flags);
2293 return dgnc_set_modem_info(ch, cmd, uarg);
2295 /* Here are any additional ioctl's that we want to implement */
2299 * The linux tty driver doesn't have a flush
2300 * input routine for the driver, assuming all backed
2301 * up data is in the line disc. buffers. However,
2302 * we all know that's not the case. Here, we
2303 * act on the ioctl, but then lie and say we didn't
2304 * so the line discipline will process the flush
2307 rc = tty_check_change(tty);
2311 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2312 ch->ch_r_head = ch->ch_r_tail;
2313 ch_bd_ops->flush_uart_read(ch);
2314 /* Force queue flow control to be released, if needed */
2315 dgnc_check_queue_flow_control(ch);
2318 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2319 if (!(un->un_type == DGNC_PRINT)) {
2320 ch->ch_w_head = ch->ch_w_tail;
2321 ch_bd_ops->flush_uart_write(ch);
2323 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY))
2324 dgnc_wake_up_unit(&ch->ch_tun);
2326 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY))
2327 dgnc_wake_up_unit(&ch->ch_pun);
2331 /* pretend we didn't recognize this IOCTL */
2332 spin_unlock_irqrestore(&ch->ch_lock, flags);
2333 return -ENOIOCTLCMD;
2337 * The linux tty driver doesn't have a flush
2338 * input routine for the driver, assuming all backed
2339 * up data is in the line disc. buffers. However,
2340 * we all know that's not the case. Here, we
2341 * act on the ioctl, but then lie and say we didn't
2342 * so the line discipline will process the flush
2345 if (cmd == TCSETSF) {
2347 ch->ch_flags &= ~CH_STOP;
2348 ch->ch_r_head = ch->ch_r_tail;
2349 ch_bd_ops->flush_uart_read(ch);
2350 /* Force queue flow control to be released, if needed */
2351 dgnc_check_queue_flow_control(ch);
2354 /* now wait for all the output to drain */
2355 spin_unlock_irqrestore(&ch->ch_lock, flags);
2356 rc = ch_bd_ops->drain(tty, 0);
2360 /* pretend we didn't recognize this */
2361 return -ENOIOCTLCMD;
2365 spin_unlock_irqrestore(&ch->ch_lock, flags);
2366 rc = ch_bd_ops->drain(tty, 0);
2370 /* pretend we didn't recognize this */
2371 return -ENOIOCTLCMD;
2374 spin_unlock_irqrestore(&ch->ch_lock, flags);
2375 /* Make the ld do it */
2376 return -ENOIOCTLCMD;
2379 /* get information for ditty */
2380 spin_unlock_irqrestore(&ch->ch_lock, flags);
2381 return dgnc_tty_digigeta(tty, uarg);
2386 /* set information for ditty */
2387 if (cmd == (DIGI_SETAW)) {
2388 spin_unlock_irqrestore(&ch->ch_lock, flags);
2389 rc = ch_bd_ops->drain(tty, 0);
2393 spin_lock_irqsave(&ch->ch_lock, flags);
2395 tty_ldisc_flush(tty);
2400 spin_unlock_irqrestore(&ch->ch_lock, flags);
2401 return dgnc_tty_digiseta(tty, uarg);
2407 * Let go of locks when accessing user space,
2410 spin_unlock_irqrestore(&ch->ch_lock, flags);
2411 rc = get_user(loopback, (unsigned int __user *)arg);
2414 spin_lock_irqsave(&ch->ch_lock, flags);
2416 /* Enable/disable internal loopback for this port */
2418 ch->ch_flags |= CH_LOOPBACK;
2420 ch->ch_flags &= ~(CH_LOOPBACK);
2422 ch_bd_ops->param(tty);
2423 spin_unlock_irqrestore(&ch->ch_lock, flags);
2427 case DIGI_GETCUSTOMBAUD:
2428 spin_unlock_irqrestore(&ch->ch_lock, flags);
2429 return put_user(ch->ch_custom_speed,
2430 (unsigned int __user *)arg);
2432 case DIGI_SETCUSTOMBAUD:
2436 spin_unlock_irqrestore(&ch->ch_lock, flags);
2437 rc = get_user(new_rate, (int __user *)arg);
2440 spin_lock_irqsave(&ch->ch_lock, flags);
2441 dgnc_set_custom_speed(ch, new_rate);
2442 ch_bd_ops->param(tty);
2443 spin_unlock_irqrestore(&ch->ch_lock, flags);
2448 * This ioctl allows insertion of a character into the front
2449 * of any pending data to be transmitted.
2451 * This ioctl is to satisfy the "Send Character Immediate"
2452 * call that the RealPort protocol spec requires.
2454 case DIGI_REALPORT_SENDIMMEDIATE:
2458 spin_unlock_irqrestore(&ch->ch_lock, flags);
2459 rc = get_user(c, (unsigned char __user *)arg);
2462 spin_lock_irqsave(&ch->ch_lock, flags);
2463 ch_bd_ops->send_immediate_char(ch, c);
2464 spin_unlock_irqrestore(&ch->ch_lock, flags);
2469 * This ioctl returns all the current counts for the port.
2471 * This ioctl is to satisfy the "Line Error Counters"
2472 * call that the RealPort protocol spec requires.
2474 case DIGI_REALPORT_GETCOUNTERS:
2476 struct digi_getcounter buf;
2478 buf.norun = ch->ch_err_overrun;
2479 buf.noflow = 0; /* The driver doesn't keep this stat */
2480 buf.nframe = ch->ch_err_frame;
2481 buf.nparity = ch->ch_err_parity;
2482 buf.nbreak = ch->ch_err_break;
2483 buf.rbytes = ch->ch_rxcount;
2484 buf.tbytes = ch->ch_txcount;
2486 spin_unlock_irqrestore(&ch->ch_lock, flags);
2488 if (copy_to_user(uarg, &buf, sizeof(buf)))
2495 * This ioctl returns all current events.
2497 * This ioctl is to satisfy the "Event Reporting"
2498 * call that the RealPort protocol spec requires.
2500 case DIGI_REALPORT_GETEVENTS:
2502 unsigned int events = 0;
2504 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2505 if (ch->ch_flags & CH_BREAK_SENDING)
2507 if ((ch->ch_flags & CH_STOP) ||
2508 (ch->ch_flags & CH_FORCED_STOP))
2509 events |= (EV_OPU | EV_OPS);
2511 if ((ch->ch_flags & CH_STOPI) ||
2512 (ch->ch_flags & CH_FORCED_STOPI))
2513 events |= (EV_IPU | EV_IPS);
2515 spin_unlock_irqrestore(&ch->ch_lock, flags);
2516 return put_user(events, (unsigned int __user *)arg);
2520 * This ioctl returns TOUT and TIN counters based
2521 * upon the values passed in by the RealPort Server.
2522 * It also passes back whether the UART Transmitter is
2525 case DIGI_REALPORT_GETBUFFERS:
2527 struct digi_getbuffer buf;
2531 spin_unlock_irqrestore(&ch->ch_lock, flags);
2533 if (copy_from_user(&buf, uarg, sizeof(buf)))
2536 spin_lock_irqsave(&ch->ch_lock, flags);
2538 /* Figure out how much data is in our RX and TX queues. */
2540 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
2541 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
2544 * Is the UART empty?
2545 * Add that value to whats in our TX queue.
2548 count = buf.txbuf + ch_bd_ops->get_uart_bytes_left(ch);
2551 * Figure out how much data the RealPort Server believes should
2552 * be in our TX queue.
2554 tdist = (buf.tx_in - buf.tx_out) & 0xffff;
2557 * If we have more data than the RealPort Server believes we
2558 * should have, reduce our count to its amount.
2560 * This count difference CAN happen because the Linux LD can
2561 * insert more characters into our queue for OPOST processing
2562 * that the RealPort Server doesn't know about.
2564 if (buf.txbuf > tdist)
2567 /* Report whether our queue and UART TX are completely empty. */
2574 spin_unlock_irqrestore(&ch->ch_lock, flags);
2576 if (copy_to_user(uarg, &buf, sizeof(buf)))
2582 spin_unlock_irqrestore(&ch->ch_lock, flags);
2584 return -ENOIOCTLCMD;
2587 spin_unlock_irqrestore(&ch->ch_lock, flags);