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(
888 (((ch->ch_tun.un_flags |
889 ch->ch_pun.un_flags) & UN_CLOSING) == 0));
890 /* If ret is non-zero, user ctrl-c'ed us */
894 spin_lock_irqsave(&ch->ch_lock, flags);
896 tty->driver_data = un;
898 /* Initialize tty's */
900 if (!(un->un_flags & UN_ISOPEN)) {
903 /* Maybe do something here to the TTY struct as well? */
907 * Allocate channel buffers for read/write/error.
908 * Set flag, so we don't get trounced on.
910 ch->ch_flags |= (CH_OPENING);
912 spin_unlock_irqrestore(&ch->ch_lock, flags);
915 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
917 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
919 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
921 if (!ch->ch_rqueue || !ch->ch_equeue || !ch->ch_wqueue) {
922 kfree(ch->ch_rqueue);
923 kfree(ch->ch_equeue);
924 kfree(ch->ch_wqueue);
928 spin_lock_irqsave(&ch->ch_lock, flags);
930 ch->ch_flags &= ~(CH_OPENING);
931 wake_up_interruptible(&ch->ch_flags_wait);
933 /* Initialize if neither terminal or printer is open. */
935 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
936 /* Flush input queues. */
944 brd->bd_ops->flush_uart_write(ch);
945 brd->bd_ops->flush_uart_read(ch);
948 ch->ch_cached_lsr = 0;
949 ch->ch_stop_sending_break = 0;
950 ch->ch_stops_sent = 0;
952 ch->ch_c_cflag = tty->termios.c_cflag;
953 ch->ch_c_iflag = tty->termios.c_iflag;
954 ch->ch_c_oflag = tty->termios.c_oflag;
955 ch->ch_c_lflag = tty->termios.c_lflag;
956 ch->ch_startc = tty->termios.c_cc[VSTART];
957 ch->ch_stopc = tty->termios.c_cc[VSTOP];
960 * Bring up RTS and DTR...
961 * Also handle RTS or DTR toggle if set.
963 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
964 ch->ch_mostat |= (UART_MCR_RTS);
965 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
966 ch->ch_mostat |= (UART_MCR_DTR);
968 /* Tell UART to init itself */
969 brd->bd_ops->uart_init(ch);
972 brd->bd_ops->param(tty);
976 spin_unlock_irqrestore(&ch->ch_lock, flags);
978 rc = dgnc_block_til_ready(tty, file, ch);
980 spin_lock_irqsave(&ch->ch_lock, flags);
983 un->un_flags |= (UN_ISOPEN);
984 spin_unlock_irqrestore(&ch->ch_lock, flags);
989 spin_unlock_irqrestore(&brd->bd_lock, flags);
993 spin_unlock_irqrestore(&ch->ch_lock, flags);
998 /* Wait for DCD, if needed. */
999 static int dgnc_block_til_ready(struct tty_struct *tty,
1001 struct channel_t *ch)
1004 struct un_t *un = tty->driver_data;
1005 unsigned long flags;
1007 int sleep_on_un_flags = 0;
1012 spin_lock_irqsave(&ch->ch_lock, flags);
1017 sleep_on_un_flags = 0;
1019 if (ch->ch_bd->state == BOARD_FAILED) {
1024 if (tty_hung_up_p(file)) {
1030 * If either unit is in the middle of the fragile part of close,
1031 * we just cannot touch the channel safely.
1032 * Go back to sleep, knowing that when the channel can be
1033 * touched safely, the close routine will signal the
1034 * ch_wait_flags to wake us back up.
1036 if (!((ch->ch_tun.un_flags |
1037 ch->ch_pun.un_flags) &
1040 * Our conditions to leave cleanly and happily:
1041 * 1) NONBLOCKING on the tty is set.
1043 * 3) DCD (fake or real) is active.
1046 if (file->f_flags & O_NONBLOCK)
1049 if (tty_io_error(tty)) {
1054 if (ch->ch_flags & CH_CD)
1057 if (ch->ch_flags & CH_FCAR)
1060 sleep_on_un_flags = 1;
1064 * If there is a signal pending, the user probably
1065 * interrupted (ctrl-c) us.
1067 if (signal_pending(current)) {
1072 if (sleep_on_un_flags)
1073 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1075 old_flags = ch->ch_flags;
1078 * Let go of channel lock before calling schedule.
1079 * Our poller will get any FEP events and wake us up when DCD
1080 * eventually goes active.
1083 spin_unlock_irqrestore(&ch->ch_lock, flags);
1086 * Wait for something in the flags to change
1087 * from the current value.
1089 if (sleep_on_un_flags)
1090 rc = wait_event_interruptible
1092 (old_flags != (ch->ch_tun.un_flags |
1093 ch->ch_pun.un_flags)));
1095 rc = wait_event_interruptible(
1097 (old_flags != ch->ch_flags));
1100 * We got woken up for some reason.
1101 * Before looping around, grab our channel lock.
1103 spin_lock_irqsave(&ch->ch_lock, flags);
1108 spin_unlock_irqrestore(&ch->ch_lock, flags);
1113 /* Hangup the port. Like a close, but don't wait for output to drain. */
1114 static void dgnc_tty_hangup(struct tty_struct *tty)
1119 /* flush the transmit queues */
1120 dgnc_tty_flush_buffer(tty);
1123 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1125 struct dgnc_board *bd;
1126 struct channel_t *ch;
1128 unsigned long flags;
1133 un = tty->driver_data;
1145 spin_lock_irqsave(&ch->ch_lock, flags);
1148 * Determine if this is the last close or not - and if we agree about
1149 * which type of close it is with the Line Discipline
1151 if ((tty->count == 1) && (un->un_open_count != 1)) {
1153 * Uh, oh. tty->count is 1, which means that the tty
1154 * structure will be freed. un_open_count should always
1155 * be one in these conditions. If it's greater than
1156 * one, we've got real problems, since it means the
1157 * serial port won't be shutdown.
1160 "tty->count is 1, un open count is %d\n",
1162 un->un_open_count = 1;
1165 if (un->un_open_count)
1166 un->un_open_count--;
1169 "bad serial port open count of %d\n",
1172 ch->ch_open_count--;
1174 if (ch->ch_open_count && un->un_open_count) {
1175 spin_unlock_irqrestore(&ch->ch_lock, flags);
1179 /* OK, its the last close on the unit */
1180 un->un_flags |= UN_CLOSING;
1185 * Only officially close channel if count is 0 and
1186 * DIGI_PRINTER bit is not set.
1188 if ((ch->ch_open_count == 0) &&
1189 !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1190 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1192 /* turn off print device when closing print device. */
1194 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1195 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1196 (int)ch->ch_digi.digi_offlen);
1197 ch->ch_flags &= ~CH_PRON;
1200 spin_unlock_irqrestore(&ch->ch_lock, flags);
1201 /* wait for output to drain */
1202 /* This will also return if we take an interrupt */
1204 bd->bd_ops->drain(tty, 0);
1206 dgnc_tty_flush_buffer(tty);
1207 tty_ldisc_flush(tty);
1209 spin_lock_irqsave(&ch->ch_lock, flags);
1213 /* If we have HUPCL set, lower DTR and RTS */
1215 if (ch->ch_c_cflag & HUPCL) {
1217 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1218 bd->bd_ops->assert_modem_signals(ch);
1221 * Go to sleep to ensure RTS/DTR
1222 * have been dropped for modems to see it.
1224 if (ch->ch_close_delay) {
1225 spin_unlock_irqrestore(&ch->ch_lock,
1227 msleep_interruptible(ch->ch_close_delay);
1228 spin_lock_irqsave(&ch->ch_lock, flags);
1232 ch->ch_old_baud = 0;
1234 /* Turn off UART interrupts for this port */
1235 ch->ch_bd->bd_ops->uart_off(ch);
1237 /* turn off print device when closing print device. */
1239 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1240 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1241 (int)ch->ch_digi.digi_offlen);
1242 ch->ch_flags &= ~CH_PRON;
1247 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1249 wake_up_interruptible(&ch->ch_flags_wait);
1250 wake_up_interruptible(&un->un_flags_wait);
1252 spin_unlock_irqrestore(&ch->ch_lock, flags);
1256 * Return number of characters that have not been transmitted yet.
1258 * This routine is used by the line discipline to determine if there
1259 * is data waiting to be transmitted/drained/flushed or not.
1261 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1263 struct channel_t *ch = NULL;
1264 struct un_t *un = NULL;
1269 unsigned long flags;
1274 un = tty->driver_data;
1282 spin_lock_irqsave(&ch->ch_lock, flags);
1285 thead = ch->ch_w_head & tmask;
1286 ttail = ch->ch_w_tail & tmask;
1288 spin_unlock_irqrestore(&ch->ch_lock, flags);
1292 else if (thead > ttail)
1293 chars = thead - ttail;
1295 chars = thead - ttail + WQUEUESIZE;
1301 * Reduces bytes_available to the max number of characters
1302 * that can be sent currently given the maxcps value, and
1303 * returns the new bytes_available. This only affects printer
1306 static int dgnc_maxcps_room(struct channel_t *ch, int bytes_available)
1308 int rc = bytes_available;
1310 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1312 unsigned long current_time = jiffies;
1313 unsigned long buffer_time = current_time +
1314 (HZ * ch->ch_digi.digi_bufsize) /
1315 ch->ch_digi.digi_maxcps;
1317 if (ch->ch_cpstime < current_time) {
1318 /* buffer is empty */
1319 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1320 cps_limit = ch->ch_digi.digi_bufsize;
1321 } else if (ch->ch_cpstime < buffer_time) {
1322 /* still room in the buffer */
1323 cps_limit = ((buffer_time - ch->ch_cpstime) *
1324 ch->ch_digi.digi_maxcps) / HZ;
1326 /* no room in the buffer */
1330 rc = min(cps_limit, bytes_available);
1336 /* Return room available in Tx buffer */
1337 static int dgnc_tty_write_room(struct tty_struct *tty)
1339 struct channel_t *ch = NULL;
1340 struct un_t *un = NULL;
1345 unsigned long flags;
1350 un = tty->driver_data;
1358 spin_lock_irqsave(&ch->ch_lock, flags);
1361 head = (ch->ch_w_head) & tmask;
1362 tail = (ch->ch_w_tail) & tmask;
1364 room = tail - head - 1;
1368 /* Limit printer to maxcps */
1369 if (un->un_type != DGNC_PRINT)
1370 room = dgnc_maxcps_room(ch, room);
1373 * If we are printer device, leave room for
1374 * possibly both the on and off strings.
1376 if (un->un_type == DGNC_PRINT) {
1377 if (!(ch->ch_flags & CH_PRON))
1378 room -= ch->ch_digi.digi_onlen;
1379 room -= ch->ch_digi.digi_offlen;
1381 if (ch->ch_flags & CH_PRON)
1382 room -= ch->ch_digi.digi_offlen;
1388 spin_unlock_irqrestore(&ch->ch_lock, flags);
1393 * Put a character into ch->ch_buf
1394 * Used by the line discipline for OPOST processing
1396 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1398 dgnc_tty_write(tty, &c, 1);
1403 * Take data from the user or kernel and send it out to the FEP.
1404 * In here exists all the Transparent Print magic as well.
1406 static int dgnc_tty_write(struct tty_struct *tty,
1407 const unsigned char *buf, int count)
1409 struct channel_t *ch = NULL;
1410 struct un_t *un = NULL;
1411 int bufcount = 0, n = 0;
1412 unsigned long flags;
1421 un = tty->driver_data;
1433 * Store original amount of characters passed in.
1434 * This helps to figure out if we should ask the FEP
1435 * to send us an event when it has more space available.
1438 spin_lock_irqsave(&ch->ch_lock, flags);
1441 head = (ch->ch_w_head) & tmask;
1442 tail = (ch->ch_w_tail) & tmask;
1444 bufcount = tail - head - 1;
1446 bufcount += WQUEUESIZE;
1449 * Limit printer output to maxcps overall, with bursts allowed
1450 * up to bufsize characters.
1452 if (un->un_type != DGNC_PRINT)
1453 bufcount = dgnc_maxcps_room(ch, bufcount);
1455 count = min(count, bufcount);
1460 * Output the printer ON string, if we are in terminal mode, but
1461 * need to be in printer mode.
1463 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1464 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1465 (int)ch->ch_digi.digi_onlen);
1466 head = (ch->ch_w_head) & tmask;
1467 ch->ch_flags |= CH_PRON;
1471 * On the other hand, output the printer OFF string, if we are
1472 * currently in printer mode, but need to output to the terminal.
1474 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1475 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1476 (int)ch->ch_digi.digi_offlen);
1477 head = (ch->ch_w_head) & tmask;
1478 ch->ch_flags &= ~CH_PRON;
1484 * If the write wraps over the top of the circular buffer,
1485 * move the portion up to the wrap point, and reset the
1486 * pointers to the bottom.
1488 remain = WQUEUESIZE - head;
1492 memcpy(ch->ch_wqueue + head, buf, remain);
1498 /* Move rest of data. */
1500 memcpy(ch->ch_wqueue + head, buf, remain);
1506 ch->ch_w_head = head;
1509 /* Update printer buffer empty time. */
1510 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0) &&
1511 (ch->ch_digi.digi_bufsize > 0)) {
1512 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1515 spin_unlock_irqrestore(&ch->ch_lock, flags);
1518 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1523 spin_unlock_irqrestore(&ch->ch_lock, flags);
1528 /* Return modem signals to ld. */
1529 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1531 struct channel_t *ch;
1534 unsigned char mstat = 0;
1535 unsigned long flags;
1540 un = tty->driver_data;
1548 spin_lock_irqsave(&ch->ch_lock, flags);
1550 mstat = ch->ch_mostat | ch->ch_mistat;
1552 spin_unlock_irqrestore(&ch->ch_lock, flags);
1556 if (mstat & UART_MCR_DTR)
1558 if (mstat & UART_MCR_RTS)
1560 if (mstat & UART_MSR_CTS)
1562 if (mstat & UART_MSR_DSR)
1564 if (mstat & UART_MSR_RI)
1566 if (mstat & UART_MSR_DCD)
1572 /* Set modem signals, called by ld. */
1573 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1574 unsigned int set, unsigned int clear)
1576 struct dgnc_board *bd;
1577 struct channel_t *ch;
1579 unsigned long flags;
1584 un = tty->driver_data;
1596 spin_lock_irqsave(&ch->ch_lock, flags);
1598 if (set & TIOCM_RTS)
1599 ch->ch_mostat |= UART_MCR_RTS;
1601 if (set & TIOCM_DTR)
1602 ch->ch_mostat |= UART_MCR_DTR;
1604 if (clear & TIOCM_RTS)
1605 ch->ch_mostat &= ~(UART_MCR_RTS);
1607 if (clear & TIOCM_DTR)
1608 ch->ch_mostat &= ~(UART_MCR_DTR);
1610 bd->bd_ops->assert_modem_signals(ch);
1612 spin_unlock_irqrestore(&ch->ch_lock, flags);
1617 /* Send a Break, called by ld. */
1618 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
1620 struct dgnc_board *bd;
1621 struct channel_t *ch;
1623 unsigned long flags;
1628 un = tty->driver_data;
1643 spin_lock_irqsave(&ch->ch_lock, flags);
1645 bd->bd_ops->send_break(ch, msec);
1647 spin_unlock_irqrestore(&ch->ch_lock, flags);
1652 /* wait until data has been transmitted, called by ld. */
1653 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1655 struct dgnc_board *bd;
1656 struct channel_t *ch;
1662 un = tty->driver_data;
1674 bd->bd_ops->drain(tty, 0);
1677 /* send a high priority character, called by ld. */
1678 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
1680 struct dgnc_board *bd;
1681 struct channel_t *ch;
1683 unsigned long flags;
1688 un = tty->driver_data;
1700 spin_lock_irqsave(&ch->ch_lock, flags);
1701 bd->bd_ops->send_immediate_char(ch, c);
1702 spin_unlock_irqrestore(&ch->ch_lock, flags);
1705 /* Return modem signals to ld. */
1706 static inline int dgnc_get_mstat(struct channel_t *ch)
1708 unsigned char mstat;
1709 unsigned long flags;
1715 spin_lock_irqsave(&ch->ch_lock, flags);
1717 mstat = ch->ch_mostat | ch->ch_mistat;
1719 spin_unlock_irqrestore(&ch->ch_lock, flags);
1723 if (mstat & UART_MCR_DTR)
1725 if (mstat & UART_MCR_RTS)
1727 if (mstat & UART_MSR_CTS)
1729 if (mstat & UART_MSR_DSR)
1731 if (mstat & UART_MSR_RI)
1733 if (mstat & UART_MSR_DCD)
1739 /* Return modem signals to ld. */
1740 static int dgnc_get_modem_info(struct channel_t *ch,
1741 unsigned int __user *value)
1743 return put_user(dgnc_get_mstat(ch), value);
1746 /* Set modem signals, called by ld. */
1747 static int dgnc_set_modem_info(struct channel_t *ch,
1748 unsigned int command,
1749 unsigned int __user *value)
1752 unsigned int arg = 0;
1753 unsigned long flags;
1755 rc = get_user(arg, value);
1761 if (arg & TIOCM_RTS)
1762 ch->ch_mostat |= UART_MCR_RTS;
1764 if (arg & TIOCM_DTR)
1765 ch->ch_mostat |= UART_MCR_DTR;
1770 if (arg & TIOCM_RTS)
1771 ch->ch_mostat &= ~(UART_MCR_RTS);
1773 if (arg & TIOCM_DTR)
1774 ch->ch_mostat &= ~(UART_MCR_DTR);
1780 if (arg & TIOCM_RTS)
1781 ch->ch_mostat |= UART_MCR_RTS;
1783 ch->ch_mostat &= ~(UART_MCR_RTS);
1785 if (arg & TIOCM_DTR)
1786 ch->ch_mostat |= UART_MCR_DTR;
1788 ch->ch_mostat &= ~(UART_MCR_DTR);
1796 spin_lock_irqsave(&ch->ch_lock, flags);
1798 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1800 spin_unlock_irqrestore(&ch->ch_lock, flags);
1805 /* Ioctl to get the information for ditty. */
1806 static int dgnc_tty_digigeta(struct tty_struct *tty,
1807 struct digi_t __user *retinfo)
1809 struct channel_t *ch;
1812 unsigned long flags;
1820 un = tty->driver_data;
1828 memset(&tmp, 0, sizeof(tmp));
1830 spin_lock_irqsave(&ch->ch_lock, flags);
1831 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
1832 spin_unlock_irqrestore(&ch->ch_lock, flags);
1834 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1840 /* Ioctl to set the information for ditty. */
1841 static int dgnc_tty_digiseta(struct tty_struct *tty,
1842 struct digi_t __user *new_info)
1844 struct dgnc_board *bd;
1845 struct channel_t *ch;
1847 struct digi_t new_digi;
1848 unsigned long flags;
1853 un = tty->driver_data;
1865 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
1868 spin_lock_irqsave(&ch->ch_lock, flags);
1870 /* Handle transitions to and from RTS Toggle. */
1872 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
1873 (new_digi.digi_flags & DIGI_RTS_TOGGLE))
1874 ch->ch_mostat &= ~(UART_MCR_RTS);
1875 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
1876 !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
1877 ch->ch_mostat |= (UART_MCR_RTS);
1879 /* Handle transitions to and from DTR Toggle. */
1881 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
1882 (new_digi.digi_flags & DIGI_DTR_TOGGLE))
1883 ch->ch_mostat &= ~(UART_MCR_DTR);
1884 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
1885 !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
1886 ch->ch_mostat |= (UART_MCR_DTR);
1888 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
1890 if (ch->ch_digi.digi_maxcps < 1)
1891 ch->ch_digi.digi_maxcps = 1;
1893 if (ch->ch_digi.digi_maxcps > 10000)
1894 ch->ch_digi.digi_maxcps = 10000;
1896 if (ch->ch_digi.digi_bufsize < 10)
1897 ch->ch_digi.digi_bufsize = 10;
1899 if (ch->ch_digi.digi_maxchar < 1)
1900 ch->ch_digi.digi_maxchar = 1;
1902 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
1903 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
1905 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
1906 ch->ch_digi.digi_onlen = DIGI_PLEN;
1908 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
1909 ch->ch_digi.digi_offlen = DIGI_PLEN;
1911 bd->bd_ops->param(tty);
1913 spin_unlock_irqrestore(&ch->ch_lock, flags);
1918 static void dgnc_tty_set_termios(struct tty_struct *tty,
1919 struct ktermios *old_termios)
1921 struct dgnc_board *bd;
1922 struct channel_t *ch;
1924 unsigned long flags;
1929 un = tty->driver_data;
1941 spin_lock_irqsave(&ch->ch_lock, flags);
1943 ch->ch_c_cflag = tty->termios.c_cflag;
1944 ch->ch_c_iflag = tty->termios.c_iflag;
1945 ch->ch_c_oflag = tty->termios.c_oflag;
1946 ch->ch_c_lflag = tty->termios.c_lflag;
1947 ch->ch_startc = tty->termios.c_cc[VSTART];
1948 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1950 bd->bd_ops->param(tty);
1953 spin_unlock_irqrestore(&ch->ch_lock, flags);
1956 static void dgnc_tty_throttle(struct tty_struct *tty)
1958 struct channel_t *ch;
1960 unsigned long flags;
1965 un = tty->driver_data;
1973 spin_lock_irqsave(&ch->ch_lock, flags);
1975 ch->ch_flags |= (CH_FORCED_STOPI);
1977 spin_unlock_irqrestore(&ch->ch_lock, flags);
1980 static void dgnc_tty_unthrottle(struct tty_struct *tty)
1982 struct channel_t *ch;
1984 unsigned long flags;
1989 un = tty->driver_data;
1997 spin_lock_irqsave(&ch->ch_lock, flags);
1999 ch->ch_flags &= ~(CH_FORCED_STOPI);
2001 spin_unlock_irqrestore(&ch->ch_lock, flags);
2004 static void dgnc_tty_start(struct tty_struct *tty)
2006 struct dgnc_board *bd;
2007 struct channel_t *ch;
2009 unsigned long flags;
2014 un = tty->driver_data;
2026 spin_lock_irqsave(&ch->ch_lock, flags);
2028 ch->ch_flags &= ~(CH_FORCED_STOP);
2030 spin_unlock_irqrestore(&ch->ch_lock, flags);
2033 static void dgnc_tty_stop(struct tty_struct *tty)
2035 struct dgnc_board *bd;
2036 struct channel_t *ch;
2038 unsigned long flags;
2043 un = tty->driver_data;
2055 spin_lock_irqsave(&ch->ch_lock, flags);
2057 ch->ch_flags |= (CH_FORCED_STOP);
2059 spin_unlock_irqrestore(&ch->ch_lock, flags);
2063 * Flush the cook buffer
2065 * Note to self, and any other poor souls who venture here:
2067 * flush in this case DOES NOT mean dispose of the data.
2068 * instead, it means "stop buffering and send it if you
2069 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2071 * It is also always called in interrupt context - JAR 8-Sept-99
2073 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2075 struct dgnc_board *bd;
2076 struct channel_t *ch;
2078 unsigned long flags;
2083 un = tty->driver_data;
2095 spin_lock_irqsave(&ch->ch_lock, flags);
2097 /* Do something maybe here */
2099 spin_unlock_irqrestore(&ch->ch_lock, flags);
2102 /* Flush Tx buffer (make in == out) */
2103 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2105 struct channel_t *ch;
2107 unsigned long flags;
2112 un = tty->driver_data;
2120 spin_lock_irqsave(&ch->ch_lock, flags);
2122 ch->ch_flags &= ~CH_STOP;
2124 /* Flush our write queue */
2125 ch->ch_w_head = ch->ch_w_tail;
2127 /* Flush UARTs transmit FIFO */
2128 ch->ch_bd->bd_ops->flush_uart_write(ch);
2130 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
2131 ch->ch_tun.un_flags &= ~(UN_LOW | UN_EMPTY);
2132 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2134 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
2135 ch->ch_pun.un_flags &= ~(UN_LOW | UN_EMPTY);
2136 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2139 spin_unlock_irqrestore(&ch->ch_lock, flags);
2142 /* Wakes up processes waiting in the unit's (teminal/printer) wait queue */
2143 static void dgnc_wake_up_unit(struct un_t *unit)
2145 unit->un_flags &= ~(UN_LOW | UN_EMPTY);
2146 wake_up_interruptible(&unit->un_flags_wait);
2149 /* The IOCTL function and all of its helpers */
2151 /* The usual assortment of ioctl's */
2152 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2155 struct dgnc_board *bd;
2156 struct board_ops *ch_bd_ops;
2157 struct channel_t *ch;
2160 unsigned long flags;
2161 void __user *uarg = (void __user *)arg;
2166 un = tty->driver_data;
2178 ch_bd_ops = bd->bd_ops;
2180 spin_lock_irqsave(&ch->ch_lock, flags);
2182 if (un->un_open_count <= 0) {
2188 /* Here are all the standard ioctl's that we MUST implement */
2192 * TCSBRK is SVID version: non-zero arg --> no break
2193 * this behaviour is exploited by tcdrain().
2195 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2196 * between 0.25 and 0.5 seconds so we'll ask for something
2197 * in the middle: 0.375 seconds.
2199 rc = tty_check_change(tty);
2200 spin_unlock_irqrestore(&ch->ch_lock, flags);
2204 rc = ch_bd_ops->drain(tty, 0);
2208 spin_lock_irqsave(&ch->ch_lock, flags);
2210 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2211 ch_bd_ops->send_break(ch, 250);
2213 spin_unlock_irqrestore(&ch->ch_lock, flags);
2219 * support for POSIX tcsendbreak()
2220 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2221 * between 0.25 and 0.5 seconds so we'll ask for something
2222 * in the middle: 0.375 seconds.
2224 rc = tty_check_change(tty);
2225 spin_unlock_irqrestore(&ch->ch_lock, flags);
2229 rc = ch_bd_ops->drain(tty, 0);
2233 spin_lock_irqsave(&ch->ch_lock, flags);
2235 ch_bd_ops->send_break(ch, 250);
2237 spin_unlock_irqrestore(&ch->ch_lock, flags);
2242 rc = tty_check_change(tty);
2243 spin_unlock_irqrestore(&ch->ch_lock, flags);
2247 rc = ch_bd_ops->drain(tty, 0);
2251 spin_lock_irqsave(&ch->ch_lock, flags);
2253 ch_bd_ops->send_break(ch, 250);
2255 spin_unlock_irqrestore(&ch->ch_lock, flags);
2261 spin_unlock_irqrestore(&ch->ch_lock, flags);
2266 spin_unlock_irqrestore(&ch->ch_lock, flags);
2268 return put_user(C_CLOCAL(tty) ? 1 : 0,
2269 (unsigned long __user *)arg);
2273 spin_unlock_irqrestore(&ch->ch_lock, flags);
2274 rc = get_user(arg, (unsigned long __user *)arg);
2278 spin_lock_irqsave(&ch->ch_lock, flags);
2279 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
2280 (arg ? CLOCAL : 0));
2281 ch_bd_ops->param(tty);
2282 spin_unlock_irqrestore(&ch->ch_lock, flags);
2287 spin_unlock_irqrestore(&ch->ch_lock, flags);
2288 return dgnc_get_modem_info(ch, uarg);
2293 spin_unlock_irqrestore(&ch->ch_lock, flags);
2294 return dgnc_set_modem_info(ch, cmd, uarg);
2296 /* Here are any additional ioctl's that we want to implement */
2300 * The linux tty driver doesn't have a flush
2301 * input routine for the driver, assuming all backed
2302 * up data is in the line disc. buffers. However,
2303 * we all know that's not the case. Here, we
2304 * act on the ioctl, but then lie and say we didn't
2305 * so the line discipline will process the flush
2308 rc = tty_check_change(tty);
2312 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2313 ch->ch_r_head = ch->ch_r_tail;
2314 ch_bd_ops->flush_uart_read(ch);
2315 /* Force queue flow control to be released, if needed */
2316 dgnc_check_queue_flow_control(ch);
2319 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2320 if (!(un->un_type == DGNC_PRINT)) {
2321 ch->ch_w_head = ch->ch_w_tail;
2322 ch_bd_ops->flush_uart_write(ch);
2324 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY))
2325 dgnc_wake_up_unit(&ch->ch_tun);
2327 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY))
2328 dgnc_wake_up_unit(&ch->ch_pun);
2332 /* pretend we didn't recognize this IOCTL */
2333 spin_unlock_irqrestore(&ch->ch_lock, flags);
2334 return -ENOIOCTLCMD;
2338 * The linux tty driver doesn't have a flush
2339 * input routine for the driver, assuming all backed
2340 * up data is in the line disc. buffers. However,
2341 * we all know that's not the case. Here, we
2342 * act on the ioctl, but then lie and say we didn't
2343 * so the line discipline will process the flush
2346 if (cmd == TCSETSF) {
2348 ch->ch_flags &= ~CH_STOP;
2349 ch->ch_r_head = ch->ch_r_tail;
2350 ch_bd_ops->flush_uart_read(ch);
2351 /* Force queue flow control to be released, if needed */
2352 dgnc_check_queue_flow_control(ch);
2355 /* now wait for all the output to drain */
2356 spin_unlock_irqrestore(&ch->ch_lock, flags);
2357 rc = ch_bd_ops->drain(tty, 0);
2361 /* pretend we didn't recognize this */
2362 return -ENOIOCTLCMD;
2366 spin_unlock_irqrestore(&ch->ch_lock, flags);
2367 rc = ch_bd_ops->drain(tty, 0);
2371 /* pretend we didn't recognize this */
2372 return -ENOIOCTLCMD;
2375 spin_unlock_irqrestore(&ch->ch_lock, flags);
2376 /* Make the ld do it */
2377 return -ENOIOCTLCMD;
2380 /* get information for ditty */
2381 spin_unlock_irqrestore(&ch->ch_lock, flags);
2382 return dgnc_tty_digigeta(tty, uarg);
2387 /* set information for ditty */
2388 if (cmd == (DIGI_SETAW)) {
2389 spin_unlock_irqrestore(&ch->ch_lock, flags);
2390 rc = ch_bd_ops->drain(tty, 0);
2394 spin_lock_irqsave(&ch->ch_lock, flags);
2396 tty_ldisc_flush(tty);
2401 spin_unlock_irqrestore(&ch->ch_lock, flags);
2402 return dgnc_tty_digiseta(tty, uarg);
2408 * Let go of locks when accessing user space,
2411 spin_unlock_irqrestore(&ch->ch_lock, flags);
2412 rc = get_user(loopback, (unsigned int __user *)arg);
2415 spin_lock_irqsave(&ch->ch_lock, flags);
2417 /* Enable/disable internal loopback for this port */
2419 ch->ch_flags |= CH_LOOPBACK;
2421 ch->ch_flags &= ~(CH_LOOPBACK);
2423 ch_bd_ops->param(tty);
2424 spin_unlock_irqrestore(&ch->ch_lock, flags);
2428 case DIGI_GETCUSTOMBAUD:
2429 spin_unlock_irqrestore(&ch->ch_lock, flags);
2430 return put_user(ch->ch_custom_speed,
2431 (unsigned int __user *)arg);
2433 case DIGI_SETCUSTOMBAUD:
2437 spin_unlock_irqrestore(&ch->ch_lock, flags);
2438 rc = get_user(new_rate, (int __user *)arg);
2441 spin_lock_irqsave(&ch->ch_lock, flags);
2442 dgnc_set_custom_speed(ch, new_rate);
2443 ch_bd_ops->param(tty);
2444 spin_unlock_irqrestore(&ch->ch_lock, flags);
2449 * This ioctl allows insertion of a character into the front
2450 * of any pending data to be transmitted.
2452 * This ioctl is to satisfy the "Send Character Immediate"
2453 * call that the RealPort protocol spec requires.
2455 case DIGI_REALPORT_SENDIMMEDIATE:
2459 spin_unlock_irqrestore(&ch->ch_lock, flags);
2460 rc = get_user(c, (unsigned char __user *)arg);
2463 spin_lock_irqsave(&ch->ch_lock, flags);
2464 ch_bd_ops->send_immediate_char(ch, c);
2465 spin_unlock_irqrestore(&ch->ch_lock, flags);
2470 * This ioctl returns all the current counts for the port.
2472 * This ioctl is to satisfy the "Line Error Counters"
2473 * call that the RealPort protocol spec requires.
2475 case DIGI_REALPORT_GETCOUNTERS:
2477 struct digi_getcounter buf;
2479 buf.norun = ch->ch_err_overrun;
2480 buf.noflow = 0; /* The driver doesn't keep this stat */
2481 buf.nframe = ch->ch_err_frame;
2482 buf.nparity = ch->ch_err_parity;
2483 buf.nbreak = ch->ch_err_break;
2484 buf.rbytes = ch->ch_rxcount;
2485 buf.tbytes = ch->ch_txcount;
2487 spin_unlock_irqrestore(&ch->ch_lock, flags);
2489 if (copy_to_user(uarg, &buf, sizeof(buf)))
2496 * This ioctl returns all current events.
2498 * This ioctl is to satisfy the "Event Reporting"
2499 * call that the RealPort protocol spec requires.
2501 case DIGI_REALPORT_GETEVENTS:
2503 unsigned int events = 0;
2505 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2506 if (ch->ch_flags & CH_BREAK_SENDING)
2508 if ((ch->ch_flags & CH_STOP) ||
2509 (ch->ch_flags & CH_FORCED_STOP))
2510 events |= (EV_OPU | EV_OPS);
2512 if ((ch->ch_flags & CH_STOPI) ||
2513 (ch->ch_flags & CH_FORCED_STOPI))
2514 events |= (EV_IPU | EV_IPS);
2516 spin_unlock_irqrestore(&ch->ch_lock, flags);
2517 return put_user(events, (unsigned int __user *)arg);
2521 * This ioctl returns TOUT and TIN counters based
2522 * upon the values passed in by the RealPort Server.
2523 * It also passes back whether the UART Transmitter is
2526 case DIGI_REALPORT_GETBUFFERS:
2528 struct digi_getbuffer buf;
2532 spin_unlock_irqrestore(&ch->ch_lock, flags);
2534 if (copy_from_user(&buf, uarg, sizeof(buf)))
2537 spin_lock_irqsave(&ch->ch_lock, flags);
2539 /* Figure out how much data is in our RX and TX queues. */
2541 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
2542 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
2545 * Is the UART empty?
2546 * Add that value to whats in our TX queue.
2549 count = buf.txbuf + ch_bd_ops->get_uart_bytes_left(ch);
2552 * Figure out how much data the RealPort Server believes should
2553 * be in our TX queue.
2555 tdist = (buf.tx_in - buf.tx_out) & 0xffff;
2558 * If we have more data than the RealPort Server believes we
2559 * should have, reduce our count to its amount.
2561 * This count difference CAN happen because the Linux LD can
2562 * insert more characters into our queue for OPOST processing
2563 * that the RealPort Server doesn't know about.
2565 if (buf.txbuf > tdist)
2568 /* Report whether our queue and UART TX are completely empty. */
2575 spin_unlock_irqrestore(&ch->ch_lock, flags);
2577 if (copy_to_user(uarg, &buf, sizeof(buf)))
2583 spin_unlock_irqrestore(&ch->ch_lock, flags);
2585 return -ENOIOCTLCMD;
2588 spin_unlock_irqrestore(&ch->ch_lock, flags);