2 * Copyright 2003 Digi International (www.digi.com)
3 * Scott H Kilau <Scott_Kilau at digi dot com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more details.
16 /************************************************************************
18 * This file implements the tty driver functionality for the
19 * Neo and ClassicBoard PCI based product lines.
21 ************************************************************************
25 #include <linux/kernel.h>
26 #include <linux/sched.h> /* For jiffies, task states */
27 #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
28 #include <linux/module.h>
29 #include <linux/ctype.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/types.h>
33 #include <linux/serial_reg.h>
34 #include <linux/slab.h>
35 #include <linux/delay.h> /* For udelay */
36 #include <linux/uaccess.h> /* For copy_from_user/copy_to_user */
37 #include <linux/pci.h>
38 #include "dgnc_driver.h"
42 #include "dgnc_sysfs.h"
43 #include "dgnc_utils.h"
48 static struct dgnc_board *dgnc_BoardsByMajor[256];
49 static unsigned char *dgnc_TmpWriteBuf;
52 * Default transparent print information.
54 static struct digi_t dgnc_digi_init = {
55 .digi_flags = DIGI_COOK, /* Flags */
56 .digi_maxcps = 100, /* Max CPS */
57 .digi_maxchar = 50, /* Max chars in print queue */
58 .digi_bufsize = 100, /* Printer buffer size */
59 .digi_onlen = 4, /* size of printer on string */
60 .digi_offlen = 4, /* size of printer off string */
61 .digi_onstr = "\033[5i", /* ANSI printer on string ] */
62 .digi_offstr = "\033[4i", /* ANSI printer off string ] */
63 .digi_term = "ansi" /* default terminal type */
67 * Define a local default termios struct. All ports will be created
68 * with this termios initially.
70 * This defines a raw port at 9600 baud, 8 data bits, no parity,
73 static struct ktermios DgncDefaultTermios = {
74 .c_iflag = (DEFAULT_IFLAGS), /* iflags */
75 .c_oflag = (DEFAULT_OFLAGS), /* oflags */
76 .c_cflag = (DEFAULT_CFLAGS), /* cflags */
77 .c_lflag = (DEFAULT_LFLAGS), /* lflags */
82 /* Our function prototypes */
83 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
84 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
85 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file,
86 struct channel_t *ch);
87 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
89 static int dgnc_tty_digigeta(struct tty_struct *tty,
90 struct digi_t __user *retinfo);
91 static int dgnc_tty_digiseta(struct tty_struct *tty,
92 struct digi_t __user *new_info);
93 static int dgnc_tty_write_room(struct tty_struct *tty);
94 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
95 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
96 static void dgnc_tty_start(struct tty_struct *tty);
97 static void dgnc_tty_stop(struct tty_struct *tty);
98 static void dgnc_tty_throttle(struct tty_struct *tty);
99 static void dgnc_tty_unthrottle(struct tty_struct *tty);
100 static void dgnc_tty_flush_chars(struct tty_struct *tty);
101 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
102 static void dgnc_tty_hangup(struct tty_struct *tty);
103 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command,
104 unsigned int __user *value);
105 static int dgnc_get_modem_info(struct channel_t *ch,
106 unsigned int __user *value);
107 static int dgnc_tty_tiocmget(struct tty_struct *tty);
108 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set,
110 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
111 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
112 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf,
114 static void dgnc_tty_set_termios(struct tty_struct *tty,
115 struct ktermios *old_termios);
116 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
118 static const struct tty_operations dgnc_tty_ops = {
119 .open = dgnc_tty_open,
120 .close = dgnc_tty_close,
121 .write = dgnc_tty_write,
122 .write_room = dgnc_tty_write_room,
123 .flush_buffer = dgnc_tty_flush_buffer,
124 .chars_in_buffer = dgnc_tty_chars_in_buffer,
125 .flush_chars = dgnc_tty_flush_chars,
126 .ioctl = dgnc_tty_ioctl,
127 .set_termios = dgnc_tty_set_termios,
128 .stop = dgnc_tty_stop,
129 .start = dgnc_tty_start,
130 .throttle = dgnc_tty_throttle,
131 .unthrottle = dgnc_tty_unthrottle,
132 .hangup = dgnc_tty_hangup,
133 .put_char = dgnc_tty_put_char,
134 .tiocmget = dgnc_tty_tiocmget,
135 .tiocmset = dgnc_tty_tiocmset,
136 .break_ctl = dgnc_tty_send_break,
137 .wait_until_sent = dgnc_tty_wait_until_sent,
138 .send_xchar = dgnc_tty_send_xchar
141 /************************************************************************
143 * TTY Initialization/Cleanup Functions
145 ************************************************************************/
150 * Initialize any global tty related data before we download any boards.
152 int dgnc_tty_preinit(void)
155 * Allocate a buffer for doing the copy from user space to
156 * kernel space in dgnc_write(). We only use one buffer and
157 * control access to it with a semaphore. If we are paging, we
158 * are already in trouble so one buffer won't hurt much anyway.
160 * We are okay to sleep in the malloc, as this routine
161 * is only called during module load, (not in interrupt context),
162 * and with no locks held.
164 dgnc_TmpWriteBuf = kmalloc(WRITEBUFLEN, GFP_KERNEL);
166 if (!dgnc_TmpWriteBuf)
173 * dgnc_tty_register()
175 * Init the tty subsystem for this board.
177 int dgnc_tty_register(struct dgnc_board *brd)
181 brd->SerialDriver.magic = TTY_DRIVER_MAGIC;
183 snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgnc_%d_", brd->boardnum);
185 brd->SerialDriver.name = brd->SerialName;
186 brd->SerialDriver.name_base = 0;
187 brd->SerialDriver.major = 0;
188 brd->SerialDriver.minor_start = 0;
189 brd->SerialDriver.num = brd->maxports;
190 brd->SerialDriver.type = TTY_DRIVER_TYPE_SERIAL;
191 brd->SerialDriver.subtype = SERIAL_TYPE_NORMAL;
192 brd->SerialDriver.init_termios = DgncDefaultTermios;
193 brd->SerialDriver.driver_name = DRVSTR;
194 brd->SerialDriver.flags = (TTY_DRIVER_REAL_RAW |
195 TTY_DRIVER_DYNAMIC_DEV |
196 TTY_DRIVER_HARDWARE_BREAK);
199 * The kernel wants space to store pointers to
200 * tty_struct's and termios's.
202 brd->SerialDriver.ttys = kcalloc(brd->maxports,
203 sizeof(*brd->SerialDriver.ttys),
205 if (!brd->SerialDriver.ttys)
208 kref_init(&brd->SerialDriver.kref);
209 brd->SerialDriver.termios = kcalloc(brd->maxports,
210 sizeof(*brd->SerialDriver.termios),
212 if (!brd->SerialDriver.termios)
216 * Entry points for driver. Called by the kernel from
217 * tty_io.c and n_tty.c.
219 tty_set_operations(&brd->SerialDriver, &dgnc_tty_ops);
221 if (!brd->dgnc_Major_Serial_Registered) {
222 /* Register tty devices */
223 rc = tty_register_driver(&brd->SerialDriver);
225 dev_dbg(&brd->pdev->dev,
226 "Can't register tty device (%d)\n", rc);
229 brd->dgnc_Major_Serial_Registered = true;
233 * If we're doing transparent print, we have to do all of the above
234 * again, separately so we don't get the LD confused about what major
235 * we are when we get into the dgnc_tty_open() routine.
237 brd->PrintDriver.magic = TTY_DRIVER_MAGIC;
238 snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
240 brd->PrintDriver.name = brd->PrintName;
241 brd->PrintDriver.name_base = 0;
242 brd->PrintDriver.major = brd->SerialDriver.major;
243 brd->PrintDriver.minor_start = 0x80;
244 brd->PrintDriver.num = brd->maxports;
245 brd->PrintDriver.type = TTY_DRIVER_TYPE_SERIAL;
246 brd->PrintDriver.subtype = SERIAL_TYPE_NORMAL;
247 brd->PrintDriver.init_termios = DgncDefaultTermios;
248 brd->PrintDriver.driver_name = DRVSTR;
249 brd->PrintDriver.flags = (TTY_DRIVER_REAL_RAW |
250 TTY_DRIVER_DYNAMIC_DEV |
251 TTY_DRIVER_HARDWARE_BREAK);
254 * The kernel wants space to store pointers to
255 * tty_struct's and termios's. Must be separated from
256 * the Serial Driver so we don't get confused
258 brd->PrintDriver.ttys = kcalloc(brd->maxports,
259 sizeof(*brd->PrintDriver.ttys),
261 if (!brd->PrintDriver.ttys)
263 kref_init(&brd->PrintDriver.kref);
264 brd->PrintDriver.termios = kcalloc(brd->maxports,
265 sizeof(*brd->PrintDriver.termios),
267 if (!brd->PrintDriver.termios)
271 * Entry points for driver. Called by the kernel from
272 * tty_io.c and n_tty.c.
274 tty_set_operations(&brd->PrintDriver, &dgnc_tty_ops);
276 if (!brd->dgnc_Major_TransparentPrint_Registered) {
277 /* Register Transparent Print devices */
278 rc = tty_register_driver(&brd->PrintDriver);
280 dev_dbg(&brd->pdev->dev,
281 "Can't register Transparent Print device(%d)\n",
285 brd->dgnc_Major_TransparentPrint_Registered = true;
288 dgnc_BoardsByMajor[brd->SerialDriver.major] = brd;
289 brd->dgnc_Serial_Major = brd->SerialDriver.major;
290 brd->dgnc_TransparentPrint_Major = brd->PrintDriver.major;
298 * Init the tty subsystem. Called once per board after board has been
299 * downloaded and init'ed.
301 int dgnc_tty_init(struct dgnc_board *brd)
305 struct channel_t *ch;
311 * Initialize board structure elements.
314 vaddr = brd->re_map_membase;
316 brd->nasync = brd->maxports;
318 for (i = 0; i < brd->nasync; i++) {
320 * Okay to malloc with GFP_KERNEL, we are not at
321 * interrupt context, and there are no locks held.
323 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]),
325 if (!brd->channels[i])
326 goto err_free_channels;
329 ch = brd->channels[0];
330 vaddr = brd->re_map_membase;
332 /* Set up channel variables */
333 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
334 spin_lock_init(&ch->ch_lock);
336 /* Store all our magic numbers */
337 ch->magic = DGNC_CHANNEL_MAGIC;
338 ch->ch_tun.magic = DGNC_UNIT_MAGIC;
339 ch->ch_tun.un_ch = ch;
340 ch->ch_tun.un_type = DGNC_SERIAL;
341 ch->ch_tun.un_dev = i;
343 ch->ch_pun.magic = DGNC_UNIT_MAGIC;
344 ch->ch_pun.un_ch = ch;
345 ch->ch_pun.un_type = DGNC_PRINT;
346 ch->ch_pun.un_dev = i + 128;
348 if (brd->bd_uart_offset == 0x200)
349 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
351 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
355 ch->ch_digi = dgnc_digi_init;
357 /* .25 second delay */
358 ch->ch_close_delay = 250;
360 init_waitqueue_head(&ch->ch_flags_wait);
361 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
362 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
365 struct device *classp;
367 classp = tty_register_device(&brd->SerialDriver, i,
368 &ch->ch_bd->pdev->dev);
369 ch->ch_tun.un_sysfs = classp;
370 dgnc_create_tty_sysfs(&ch->ch_tun, classp);
372 classp = tty_register_device(&brd->PrintDriver, i,
373 &ch->ch_bd->pdev->dev);
374 ch->ch_pun.un_sysfs = classp;
375 dgnc_create_tty_sysfs(&ch->ch_pun, classp);
382 for (i = i - 1; i >= 0; --i) {
383 kfree(brd->channels[i]);
384 brd->channels[i] = NULL;
390 * dgnc_tty_post_uninit()
392 * UnInitialize any global tty related data.
394 void dgnc_tty_post_uninit(void)
396 kfree(dgnc_TmpWriteBuf);
397 dgnc_TmpWriteBuf = NULL;
403 * Uninitialize the TTY portion of this driver. Free all memory and
406 void dgnc_tty_uninit(struct dgnc_board *brd)
410 if (brd->dgnc_Major_Serial_Registered) {
411 dgnc_BoardsByMajor[brd->SerialDriver.major] = NULL;
412 brd->dgnc_Serial_Major = 0;
413 for (i = 0; i < brd->nasync; i++) {
414 if (brd->channels[i])
415 dgnc_remove_tty_sysfs(brd->channels[i]->
417 tty_unregister_device(&brd->SerialDriver, i);
419 tty_unregister_driver(&brd->SerialDriver);
420 brd->dgnc_Major_Serial_Registered = false;
423 if (brd->dgnc_Major_TransparentPrint_Registered) {
424 dgnc_BoardsByMajor[brd->PrintDriver.major] = NULL;
425 brd->dgnc_TransparentPrint_Major = 0;
426 for (i = 0; i < brd->nasync; i++) {
427 if (brd->channels[i])
428 dgnc_remove_tty_sysfs(brd->channels[i]->
430 tty_unregister_device(&brd->PrintDriver, i);
432 tty_unregister_driver(&brd->PrintDriver);
433 brd->dgnc_Major_TransparentPrint_Registered = false;
436 kfree(brd->SerialDriver.ttys);
437 brd->SerialDriver.ttys = NULL;
438 kfree(brd->SerialDriver.termios);
439 brd->SerialDriver.termios = NULL;
440 kfree(brd->PrintDriver.ttys);
441 brd->PrintDriver.ttys = NULL;
442 kfree(brd->PrintDriver.termios);
443 brd->PrintDriver.termios = NULL;
446 /*=======================================================================
448 * dgnc_wmove - Write data to transmit queue.
450 * ch - Pointer to channel structure.
451 * buf - Poiter to characters to be moved.
452 * n - Number of characters to move.
454 *=======================================================================*/
455 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
460 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
463 head = ch->ch_w_head & WQUEUEMASK;
466 * If the write wraps over the top of the circular buffer,
467 * move the portion up to the wrap point, and reset the
468 * pointers to the bottom.
470 remain = WQUEUESIZE - head;
474 memcpy(ch->ch_wqueue + head, buf, remain);
484 memcpy(ch->ch_wqueue + head, buf, remain);
489 ch->ch_w_head = head;
492 /*=======================================================================
494 * dgnc_input - Process received data.
496 * ch - Pointer to channel structure.
498 *=======================================================================*/
499 void dgnc_input(struct channel_t *ch)
501 struct dgnc_board *bd;
502 struct tty_struct *tp;
503 struct tty_ldisc *ld = NULL;
515 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
518 tp = ch->ch_tun.un_tty;
521 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
524 spin_lock_irqsave(&ch->ch_lock, flags);
527 * Figure the number of characters in the buffer.
528 * Exit immediately if none.
531 head = ch->ch_r_head & rmask;
532 tail = ch->ch_r_tail & rmask;
533 data_len = (head - tail) & rmask;
539 * If the device is not open, or CREAD is off,
540 * flush input data and return immediately.
542 if (!tp || (tp->magic != TTY_MAGIC) ||
543 !(ch->ch_tun.un_flags & UN_ISOPEN) ||
544 !(tp->termios.c_cflag & CREAD) ||
545 (ch->ch_tun.un_flags & UN_CLOSING)) {
546 ch->ch_r_head = tail;
548 /* Force queue flow control to be released, if needed */
549 dgnc_check_queue_flow_control(ch);
555 * If we are throttled, simply don't read any data.
557 if (ch->ch_flags & CH_FORCED_STOPI)
560 flip_len = TTY_FLIPBUF_SIZE;
562 /* Chop down the length, if needed */
563 len = min(data_len, flip_len);
564 len = min(len, (N_TTY_BUF_SIZE - 1));
566 ld = tty_ldisc_ref(tp);
569 * If we were unable to get a reference to the ld,
570 * don't flush our buffer, and act like the ld doesn't
571 * have any space to put the data right now.
577 * If ld doesn't have a pointer to a receive_buf function,
578 * flush the data, then act like the ld doesn't have any
579 * space to put the data right now.
581 if (!ld->ops->receive_buf) {
582 ch->ch_r_head = ch->ch_r_tail;
591 * The tty layer in the kernel has changed in 2.6.16+.
593 * The flip buffers in the tty structure are no longer exposed,
594 * and probably will be going away eventually.
596 * If we are completely raw, we don't need to go through a lot
597 * of the tty layers that exist.
598 * In this case, we take the shortest and fastest route we
599 * can to relay the data to the user.
601 * On the other hand, if we are not raw, we need to go through
602 * the new 2.6.16+ tty layer, which has its API more well defined.
604 len = tty_buffer_request_room(tp->port, len);
608 * n now contains the most amount of data we can copy,
609 * bounded either by how much the Linux tty layer can handle,
610 * or the amount of data the card actually has pending...
613 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
620 * If conditions are such that ld needs to see all
621 * UART errors, we will have to walk each character
622 * and error byte and send them to the buffer one at
625 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
626 for (i = 0; i < s; i++) {
627 if (*(ch->ch_equeue + tail + i) & UART_LSR_BI)
628 tty_insert_flip_char(tp->port,
629 *(ch->ch_rqueue + tail + i),
631 else if (*(ch->ch_equeue + tail + i) &
633 tty_insert_flip_char(tp->port,
634 *(ch->ch_rqueue + tail + i),
636 else if (*(ch->ch_equeue + tail + i) &
638 tty_insert_flip_char(tp->port,
639 *(ch->ch_rqueue + tail + i),
642 tty_insert_flip_char(tp->port,
643 *(ch->ch_rqueue + tail + i),
647 tty_insert_flip_string(tp->port,
648 ch->ch_rqueue + tail,
654 /* Flip queue if needed */
658 ch->ch_r_tail = tail & rmask;
659 ch->ch_e_tail = tail & rmask;
660 dgnc_check_queue_flow_control(ch);
661 spin_unlock_irqrestore(&ch->ch_lock, flags);
663 /* Tell the tty layer its okay to "eat" the data now */
664 tty_flip_buffer_push(tp->port);
671 spin_unlock_irqrestore(&ch->ch_lock, flags);
676 /************************************************************************
677 * Determines when CARRIER changes state and takes appropriate
679 ************************************************************************/
680 void dgnc_carrier(struct channel_t *ch)
682 struct dgnc_board *bd;
684 int virt_carrier = 0;
685 int phys_carrier = 0;
687 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
692 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
695 if (ch->ch_mistat & UART_MSR_DCD)
698 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
701 if (ch->ch_c_cflag & CLOCAL)
705 * Test for a VIRTUAL carrier transition to HIGH.
707 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
709 * When carrier rises, wake any threads waiting
710 * for carrier in the open routine.
713 if (waitqueue_active(&ch->ch_flags_wait))
714 wake_up_interruptible(&ch->ch_flags_wait);
718 * Test for a PHYSICAL carrier transition to HIGH.
720 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
722 * When carrier rises, wake any threads waiting
723 * for carrier in the open routine.
726 if (waitqueue_active(&ch->ch_flags_wait))
727 wake_up_interruptible(&ch->ch_flags_wait);
731 * Test for a PHYSICAL transition to low, so long as we aren't
732 * currently ignoring physical transitions (which is what "virtual
733 * carrier" indicates).
735 * The transition of the virtual carrier to low really doesn't
736 * matter... it really only means "ignore carrier state", not
737 * "make pretend that carrier is there".
739 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
740 (phys_carrier == 0)) {
742 * When carrier drops:
744 * Drop carrier on all open units.
746 * Flush queues, waking up any task waiting in the
749 * Send a hangup to the control terminal.
751 * Enable all select calls.
753 if (waitqueue_active(&ch->ch_flags_wait))
754 wake_up_interruptible(&ch->ch_flags_wait);
756 if (ch->ch_tun.un_open_count > 0)
757 tty_hangup(ch->ch_tun.un_tty);
759 if (ch->ch_pun.un_open_count > 0)
760 tty_hangup(ch->ch_pun.un_tty);
764 * Make sure that our cached values reflect the current reality.
766 if (virt_carrier == 1)
767 ch->ch_flags |= CH_FCAR;
769 ch->ch_flags &= ~CH_FCAR;
771 if (phys_carrier == 1)
772 ch->ch_flags |= CH_CD;
774 ch->ch_flags &= ~CH_CD;
778 * Assign the custom baud rate to the channel structure
780 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
789 ch->ch_custom_speed = 0;
794 * Since the divisor is stored in a 16-bit integer, we make sure
795 * we don't allow any rates smaller than a 16-bit integer would allow.
796 * And of course, rates above the dividend won't fly.
798 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
799 newrate = ((ch->ch_bd->bd_dividend / 0xFFFF) + 1);
801 if (newrate && newrate > ch->ch_bd->bd_dividend)
802 newrate = ch->ch_bd->bd_dividend;
805 testdiv = ch->ch_bd->bd_dividend / newrate;
808 * If we try to figure out what rate the board would use
809 * with the test divisor, it will be either equal or higher
810 * than the requested baud rate. If we then determine the
811 * rate with a divisor one higher, we will get the next lower
812 * supported rate below the requested.
814 testrate_high = ch->ch_bd->bd_dividend / testdiv;
815 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
818 * If the rate for the requested divisor is correct, just
819 * use it and be done.
821 if (testrate_high != newrate) {
823 * Otherwise, pick the rate that is closer
824 * (i.e. whichever rate has a smaller delta).
826 deltahigh = testrate_high - newrate;
827 deltalow = newrate - testrate_low;
829 if (deltahigh < deltalow)
830 newrate = testrate_high;
832 newrate = testrate_low;
836 ch->ch_custom_speed = newrate;
839 void dgnc_check_queue_flow_control(struct channel_t *ch)
843 /* Store how much space we have left in the queue */
844 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
846 qleft += RQUEUEMASK + 1;
849 * Check to see if we should enforce flow control on our queue because
850 * the ld (or user) isn't reading data out of our queue fast enuf.
852 * NOTE: This is done based on what the current flow control of the
855 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
856 * This will cause the UART's FIFO to back up, and force
857 * the RTS signal to be dropped.
858 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
859 * the other side, in hopes it will stop sending data to us.
860 * 3) NONE - Nothing we can do. We will simply drop any extra data
861 * that gets sent into us when the queue fills up.
865 if (ch->ch_digi.digi_flags & CTSPACE ||
866 ch->ch_c_cflag & CRTSCTS) {
867 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
868 ch->ch_bd->bd_ops->disable_receiver(ch);
869 ch->ch_flags |= (CH_RECEIVER_OFF);
873 else if (ch->ch_c_iflag & IXOFF) {
874 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
875 ch->ch_bd->bd_ops->send_stop_character(ch);
882 * Check to see if we should unenforce flow control because
883 * ld (or user) finally read enuf data out of our queue.
885 * NOTE: This is done based on what the current flow control of the
888 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
889 * This will cause the UART's FIFO to raise RTS back up,
890 * which will allow the other side to start sending data again.
891 * 2) SWFLOW (IXOFF) - Send a start character to
892 * the other side, so it will start sending data to us again.
893 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
894 * other side, we don't need to do anything now.
896 if (qleft > (RQUEUESIZE / 2)) {
898 if (ch->ch_digi.digi_flags & RTSPACE ||
899 ch->ch_c_cflag & CRTSCTS) {
900 if (ch->ch_flags & CH_RECEIVER_OFF) {
901 ch->ch_bd->bd_ops->enable_receiver(ch);
902 ch->ch_flags &= ~(CH_RECEIVER_OFF);
906 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
907 ch->ch_stops_sent = 0;
908 ch->ch_bd->bd_ops->send_start_character(ch);
913 void dgnc_wakeup_writes(struct channel_t *ch)
918 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
921 spin_lock_irqsave(&ch->ch_lock, flags);
924 * If channel now has space, wake up anyone waiting on the condition.
926 qlen = ch->ch_w_head - ch->ch_w_tail;
930 if (qlen >= (WQUEUESIZE - 256)) {
931 spin_unlock_irqrestore(&ch->ch_lock, flags);
935 if (ch->ch_tun.un_flags & UN_ISOPEN) {
936 if ((ch->ch_tun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
937 ch->ch_tun.un_tty->ldisc->ops->write_wakeup) {
938 spin_unlock_irqrestore(&ch->ch_lock, flags);
939 ch->ch_tun.un_tty->ldisc->ops->write_wakeup(ch->ch_tun.un_tty);
940 spin_lock_irqsave(&ch->ch_lock, flags);
943 wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
946 * If unit is set to wait until empty, check to make sure
947 * the queue AND FIFO are both empty.
949 if (ch->ch_tun.un_flags & UN_EMPTY) {
951 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
952 ch->ch_tun.un_flags &= ~(UN_EMPTY);
955 * If RTS Toggle mode is on, whenever
956 * the queue and UART is empty, keep RTS low.
958 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
959 ch->ch_mostat &= ~(UART_MCR_RTS);
960 ch->ch_bd->bd_ops->assert_modem_signals(ch);
964 * If DTR Toggle mode is on, whenever
965 * the queue and UART is empty, keep DTR low.
967 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
968 ch->ch_mostat &= ~(UART_MCR_DTR);
969 ch->ch_bd->bd_ops->assert_modem_signals(ch);
974 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
977 if (ch->ch_pun.un_flags & UN_ISOPEN) {
978 if ((ch->ch_pun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
979 ch->ch_pun.un_tty->ldisc->ops->write_wakeup) {
980 spin_unlock_irqrestore(&ch->ch_lock, flags);
981 ch->ch_pun.un_tty->ldisc->ops->write_wakeup(ch->ch_pun.un_tty);
982 spin_lock_irqsave(&ch->ch_lock, flags);
985 wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
988 * If unit is set to wait until empty, check to make sure
989 * the queue AND FIFO are both empty.
991 if (ch->ch_pun.un_flags & UN_EMPTY) {
993 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
994 ch->ch_pun.un_flags &= ~(UN_EMPTY);
997 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1000 spin_unlock_irqrestore(&ch->ch_lock, flags);
1003 /************************************************************************
1005 * TTY Entry points and helper functions
1007 ************************************************************************/
1013 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
1015 struct dgnc_board *brd;
1016 struct channel_t *ch;
1021 unsigned long flags;
1025 major = MAJOR(tty_devnum(tty));
1026 minor = MINOR(tty_devnum(tty));
1031 /* Get board pointer from our array of majors we have allocated */
1032 brd = dgnc_BoardsByMajor[major];
1037 * If board is not yet up to a state of READY, go to
1038 * sleep waiting for it to happen or they cancel the open.
1040 rc = wait_event_interruptible(brd->state_wait,
1041 (brd->state & BOARD_READY));
1046 spin_lock_irqsave(&brd->bd_lock, flags);
1048 /* If opened device is greater than our number of ports, bail. */
1049 if (PORT_NUM(minor) >= brd->nasync) {
1050 spin_unlock_irqrestore(&brd->bd_lock, flags);
1054 ch = brd->channels[PORT_NUM(minor)];
1056 spin_unlock_irqrestore(&brd->bd_lock, flags);
1060 /* Drop board lock */
1061 spin_unlock_irqrestore(&brd->bd_lock, flags);
1063 /* Grab channel lock */
1064 spin_lock_irqsave(&ch->ch_lock, flags);
1066 /* Figure out our type */
1067 if (!IS_PRINT(minor)) {
1068 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1069 un->un_type = DGNC_SERIAL;
1070 } else if (IS_PRINT(minor)) {
1071 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1072 un->un_type = DGNC_PRINT;
1074 spin_unlock_irqrestore(&ch->ch_lock, flags);
1079 * If the port is still in a previous open, and in a state
1080 * where we simply cannot safely keep going, wait until the
1083 spin_unlock_irqrestore(&ch->ch_lock, flags);
1085 rc = wait_event_interruptible(ch->ch_flags_wait,
1086 ((ch->ch_flags & CH_OPENING) == 0));
1088 /* If ret is non-zero, user ctrl-c'ed us */
1093 * If either unit is in the middle of the fragile part of close,
1094 * we just cannot touch the channel safely.
1095 * Go to sleep, knowing that when the channel can be
1096 * touched safely, the close routine will signal the
1097 * ch_flags_wait to wake us back up.
1099 rc = wait_event_interruptible(ch->ch_flags_wait,
1100 (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) &
1103 /* If ret is non-zero, user ctrl-c'ed us */
1107 spin_lock_irqsave(&ch->ch_lock, flags);
1109 /* Store our unit into driver_data, so we always have it available. */
1110 tty->driver_data = un;
1115 if (!(un->un_flags & UN_ISOPEN)) {
1116 /* Store important variables. */
1119 /* Maybe do something here to the TTY struct as well? */
1123 * Allocate channel buffers for read/write/error.
1124 * Set flag, so we don't get trounced on.
1126 ch->ch_flags |= (CH_OPENING);
1128 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1129 spin_unlock_irqrestore(&ch->ch_lock, flags);
1132 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1134 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1136 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1138 spin_lock_irqsave(&ch->ch_lock, flags);
1140 ch->ch_flags &= ~(CH_OPENING);
1141 wake_up_interruptible(&ch->ch_flags_wait);
1144 * Initialize if neither terminal or printer is open.
1146 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1148 * Flush input queues.
1157 brd->bd_ops->flush_uart_write(ch);
1158 brd->bd_ops->flush_uart_read(ch);
1161 ch->ch_cached_lsr = 0;
1162 ch->ch_stop_sending_break = 0;
1163 ch->ch_stops_sent = 0;
1165 ch->ch_c_cflag = tty->termios.c_cflag;
1166 ch->ch_c_iflag = tty->termios.c_iflag;
1167 ch->ch_c_oflag = tty->termios.c_oflag;
1168 ch->ch_c_lflag = tty->termios.c_lflag;
1169 ch->ch_startc = tty->termios.c_cc[VSTART];
1170 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1173 * Bring up RTS and DTR...
1174 * Also handle RTS or DTR toggle if set.
1176 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1177 ch->ch_mostat |= (UART_MCR_RTS);
1178 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1179 ch->ch_mostat |= (UART_MCR_DTR);
1181 /* Tell UART to init itself */
1182 brd->bd_ops->uart_init(ch);
1186 * Run param in case we changed anything
1188 brd->bd_ops->param(tty);
1193 * follow protocol for opening port
1196 spin_unlock_irqrestore(&ch->ch_lock, flags);
1198 rc = dgnc_block_til_ready(tty, file, ch);
1200 /* No going back now, increment our unit and channel counters */
1201 spin_lock_irqsave(&ch->ch_lock, flags);
1202 ch->ch_open_count++;
1203 un->un_open_count++;
1204 un->un_flags |= (UN_ISOPEN);
1205 spin_unlock_irqrestore(&ch->ch_lock, flags);
1211 * dgnc_block_til_ready()
1213 * Wait for DCD, if needed.
1215 static int dgnc_block_til_ready(struct tty_struct *tty,
1217 struct channel_t *ch)
1220 struct un_t *un = NULL;
1221 unsigned long flags;
1223 int sleep_on_un_flags = 0;
1225 if (!tty || tty->magic != TTY_MAGIC || !file || !ch ||
1226 ch->magic != DGNC_CHANNEL_MAGIC)
1229 un = tty->driver_data;
1230 if (!un || un->magic != DGNC_UNIT_MAGIC)
1233 spin_lock_irqsave(&ch->ch_lock, flags);
1239 sleep_on_un_flags = 0;
1242 * If board has failed somehow during our sleep,
1245 if (ch->ch_bd->state == BOARD_FAILED) {
1250 /* If tty was hung up, break out of loop and set error. */
1251 if (tty_hung_up_p(file)) {
1257 * If either unit is in the middle of the fragile part of close,
1258 * we just cannot touch the channel safely.
1259 * Go back to sleep, knowing that when the channel can be
1260 * touched safely, the close routine will signal the
1261 * ch_wait_flags to wake us back up.
1263 if (!((ch->ch_tun.un_flags |
1264 ch->ch_pun.un_flags) &
1267 * Our conditions to leave cleanly and happily:
1268 * 1) NONBLOCKING on the tty is set.
1270 * 3) DCD (fake or real) is active.
1273 if (file->f_flags & O_NONBLOCK)
1276 if (tty->flags & (1 << TTY_IO_ERROR)) {
1281 if (ch->ch_flags & CH_CD)
1284 if (ch->ch_flags & CH_FCAR)
1287 sleep_on_un_flags = 1;
1291 * If there is a signal pending, the user probably
1292 * interrupted (ctrl-c) us.
1293 * Leave loop with error set.
1295 if (signal_pending(current)) {
1296 retval = -ERESTARTSYS;
1301 * Store the flags before we let go of channel lock
1303 if (sleep_on_un_flags)
1304 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1306 old_flags = ch->ch_flags;
1309 * Let go of channel lock before calling schedule.
1310 * Our poller will get any FEP events and wake us up when DCD
1311 * eventually goes active.
1314 spin_unlock_irqrestore(&ch->ch_lock, flags);
1317 * Wait for something in the flags to change
1318 * from the current value.
1320 if (sleep_on_un_flags)
1321 retval = wait_event_interruptible(un->un_flags_wait,
1322 (old_flags != (ch->ch_tun.un_flags |
1323 ch->ch_pun.un_flags)));
1325 retval = wait_event_interruptible(ch->ch_flags_wait,
1326 (old_flags != ch->ch_flags));
1329 * We got woken up for some reason.
1330 * Before looping around, grab our channel lock.
1332 spin_lock_irqsave(&ch->ch_lock, flags);
1337 spin_unlock_irqrestore(&ch->ch_lock, flags);
1345 * Hangup the port. Like a close, but don't wait for output to drain.
1347 static void dgnc_tty_hangup(struct tty_struct *tty)
1351 if (!tty || tty->magic != TTY_MAGIC)
1354 un = tty->driver_data;
1355 if (!un || un->magic != DGNC_UNIT_MAGIC)
1358 /* flush the transmit queues */
1359 dgnc_tty_flush_buffer(tty);
1366 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1368 struct dgnc_board *bd;
1369 struct channel_t *ch;
1371 unsigned long flags;
1373 if (!tty || tty->magic != TTY_MAGIC)
1376 un = tty->driver_data;
1377 if (!un || un->magic != DGNC_UNIT_MAGIC)
1381 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1385 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1388 spin_lock_irqsave(&ch->ch_lock, flags);
1391 * Determine if this is the last close or not - and if we agree about
1392 * which type of close it is with the Line Discipline
1394 if ((tty->count == 1) && (un->un_open_count != 1)) {
1396 * Uh, oh. tty->count is 1, which means that the tty
1397 * structure will be freed. un_open_count should always
1398 * be one in these conditions. If it's greater than
1399 * one, we've got real problems, since it means the
1400 * serial port won't be shutdown.
1403 "tty->count is 1, un open count is %d\n",
1405 un->un_open_count = 1;
1408 if (un->un_open_count)
1409 un->un_open_count--;
1412 "bad serial port open count of %d\n",
1415 ch->ch_open_count--;
1417 if (ch->ch_open_count && un->un_open_count) {
1418 spin_unlock_irqrestore(&ch->ch_lock, flags);
1422 /* OK, its the last close on the unit */
1423 un->un_flags |= UN_CLOSING;
1428 * Only officially close channel if count is 0 and
1429 * DIGI_PRINTER bit is not set.
1431 if ((ch->ch_open_count == 0) &&
1432 !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1433 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1436 * turn off print device when closing print device.
1438 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1439 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1440 (int)ch->ch_digi.digi_offlen);
1441 ch->ch_flags &= ~CH_PRON;
1444 spin_unlock_irqrestore(&ch->ch_lock, flags);
1445 /* wait for output to drain */
1446 /* This will also return if we take an interrupt */
1448 bd->bd_ops->drain(tty, 0);
1450 dgnc_tty_flush_buffer(tty);
1451 tty_ldisc_flush(tty);
1453 spin_lock_irqsave(&ch->ch_lock, flags);
1458 * If we have HUPCL set, lower DTR and RTS
1460 if (ch->ch_c_cflag & HUPCL) {
1462 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1463 bd->bd_ops->assert_modem_signals(ch);
1466 * Go to sleep to ensure RTS/DTR
1467 * have been dropped for modems to see it.
1469 if (ch->ch_close_delay) {
1470 spin_unlock_irqrestore(&ch->ch_lock,
1472 dgnc_ms_sleep(ch->ch_close_delay);
1473 spin_lock_irqsave(&ch->ch_lock, flags);
1477 ch->ch_old_baud = 0;
1479 /* Turn off UART interrupts for this port */
1480 ch->ch_bd->bd_ops->uart_off(ch);
1483 * turn off print device when closing print device.
1485 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1486 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1487 (int)ch->ch_digi.digi_offlen);
1488 ch->ch_flags &= ~CH_PRON;
1493 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1495 wake_up_interruptible(&ch->ch_flags_wait);
1496 wake_up_interruptible(&un->un_flags_wait);
1498 spin_unlock_irqrestore(&ch->ch_lock, flags);
1502 * dgnc_tty_chars_in_buffer()
1504 * Return number of characters that have not been transmitted yet.
1506 * This routine is used by the line discipline to determine if there
1507 * is data waiting to be transmitted/drained/flushed or not.
1509 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1511 struct channel_t *ch = NULL;
1512 struct un_t *un = NULL;
1517 unsigned long flags;
1522 un = tty->driver_data;
1523 if (!un || un->magic != DGNC_UNIT_MAGIC)
1527 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1530 spin_lock_irqsave(&ch->ch_lock, flags);
1533 thead = ch->ch_w_head & tmask;
1534 ttail = ch->ch_w_tail & tmask;
1536 spin_unlock_irqrestore(&ch->ch_lock, flags);
1538 if (ttail == thead) {
1542 chars = thead - ttail;
1544 chars = thead - ttail + WQUEUESIZE;
1553 * Reduces bytes_available to the max number of characters
1554 * that can be sent currently given the maxcps value, and
1555 * returns the new bytes_available. This only affects printer
1558 static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1560 struct channel_t *ch = NULL;
1561 struct un_t *un = NULL;
1564 return bytes_available;
1566 un = tty->driver_data;
1567 if (!un || un->magic != DGNC_UNIT_MAGIC)
1568 return bytes_available;
1571 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1572 return bytes_available;
1575 * If its not the Transparent print device, return
1576 * the full data amount.
1578 if (un->un_type != DGNC_PRINT)
1579 return bytes_available;
1581 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1583 unsigned long current_time = jiffies;
1584 unsigned long buffer_time = current_time +
1585 (HZ * ch->ch_digi.digi_bufsize) /
1586 ch->ch_digi.digi_maxcps;
1588 if (ch->ch_cpstime < current_time) {
1589 /* buffer is empty */
1590 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1591 cps_limit = ch->ch_digi.digi_bufsize;
1592 } else if (ch->ch_cpstime < buffer_time) {
1593 /* still room in the buffer */
1594 cps_limit = ((buffer_time - ch->ch_cpstime) *
1595 ch->ch_digi.digi_maxcps) / HZ;
1597 /* no room in the buffer */
1601 bytes_available = min(cps_limit, bytes_available);
1604 return bytes_available;
1608 * dgnc_tty_write_room()
1610 * Return space available in Tx buffer
1612 static int dgnc_tty_write_room(struct tty_struct *tty)
1614 struct channel_t *ch = NULL;
1615 struct un_t *un = NULL;
1620 unsigned long flags;
1622 if (!tty || !dgnc_TmpWriteBuf)
1625 un = tty->driver_data;
1626 if (!un || un->magic != DGNC_UNIT_MAGIC)
1630 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1633 spin_lock_irqsave(&ch->ch_lock, flags);
1636 head = (ch->ch_w_head) & tmask;
1637 tail = (ch->ch_w_tail) & tmask;
1639 ret = tail - head - 1;
1643 /* Limit printer to maxcps */
1644 ret = dgnc_maxcps_room(tty, ret);
1647 * If we are printer device, leave space for
1648 * possibly both the on and off strings.
1650 if (un->un_type == DGNC_PRINT) {
1651 if (!(ch->ch_flags & CH_PRON))
1652 ret -= ch->ch_digi.digi_onlen;
1653 ret -= ch->ch_digi.digi_offlen;
1655 if (ch->ch_flags & CH_PRON)
1656 ret -= ch->ch_digi.digi_offlen;
1662 spin_unlock_irqrestore(&ch->ch_lock, flags);
1668 * dgnc_tty_put_char()
1670 * Put a character into ch->ch_buf
1672 * - used by the line discipline for OPOST processing
1674 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1677 * Simply call tty_write.
1679 dgnc_tty_write(tty, &c, 1);
1686 * Take data from the user or kernel and send it out to the FEP.
1687 * In here exists all the Transparent Print magic as well.
1689 static int dgnc_tty_write(struct tty_struct *tty,
1690 const unsigned char *buf, int count)
1692 struct channel_t *ch = NULL;
1693 struct un_t *un = NULL;
1694 int bufcount = 0, n = 0;
1695 unsigned long flags;
1701 if (!tty || !dgnc_TmpWriteBuf)
1704 un = tty->driver_data;
1705 if (!un || un->magic != DGNC_UNIT_MAGIC)
1709 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1716 * Store original amount of characters passed in.
1717 * This helps to figure out if we should ask the FEP
1718 * to send us an event when it has more space available.
1721 spin_lock_irqsave(&ch->ch_lock, flags);
1723 /* Get our space available for the channel from the board */
1725 head = (ch->ch_w_head) & tmask;
1726 tail = (ch->ch_w_tail) & tmask;
1728 bufcount = tail - head - 1;
1730 bufcount += WQUEUESIZE;
1733 * Limit printer output to maxcps overall, with bursts allowed
1734 * up to bufsize characters.
1736 bufcount = dgnc_maxcps_room(tty, bufcount);
1739 * Take minimum of what the user wants to send, and the
1740 * space available in the FEP buffer.
1742 count = min(count, bufcount);
1745 * Bail if no space left.
1751 * Output the printer ON string, if we are in terminal mode, but
1752 * need to be in printer mode.
1754 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1755 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1756 (int)ch->ch_digi.digi_onlen);
1757 head = (ch->ch_w_head) & tmask;
1758 ch->ch_flags |= CH_PRON;
1762 * On the other hand, output the printer OFF string, if we are
1763 * currently in printer mode, but need to output to the terminal.
1765 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1766 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1767 (int)ch->ch_digi.digi_offlen);
1768 head = (ch->ch_w_head) & tmask;
1769 ch->ch_flags &= ~CH_PRON;
1775 * If the write wraps over the top of the circular buffer,
1776 * move the portion up to the wrap point, and reset the
1777 * pointers to the bottom.
1779 remain = WQUEUESIZE - head;
1783 memcpy(ch->ch_wqueue + head, buf, remain);
1790 * Move rest of data.
1793 memcpy(ch->ch_wqueue + head, buf, remain);
1799 ch->ch_w_head = head;
1802 /* Update printer buffer empty time. */
1803 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
1804 && (ch->ch_digi.digi_bufsize > 0)) {
1805 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1808 spin_unlock_irqrestore(&ch->ch_lock, flags);
1812 * Channel lock is grabbed and then released
1813 * inside this routine.
1815 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1822 spin_unlock_irqrestore(&ch->ch_lock, flags);
1827 * Return modem signals to ld.
1830 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1832 struct channel_t *ch;
1835 unsigned char mstat = 0;
1836 unsigned long flags;
1838 if (!tty || tty->magic != TTY_MAGIC)
1841 un = tty->driver_data;
1842 if (!un || un->magic != DGNC_UNIT_MAGIC)
1846 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1849 spin_lock_irqsave(&ch->ch_lock, flags);
1851 mstat = (ch->ch_mostat | ch->ch_mistat);
1853 spin_unlock_irqrestore(&ch->ch_lock, flags);
1857 if (mstat & UART_MCR_DTR)
1858 result |= TIOCM_DTR;
1859 if (mstat & UART_MCR_RTS)
1860 result |= TIOCM_RTS;
1861 if (mstat & UART_MSR_CTS)
1862 result |= TIOCM_CTS;
1863 if (mstat & UART_MSR_DSR)
1864 result |= TIOCM_DSR;
1865 if (mstat & UART_MSR_RI)
1867 if (mstat & UART_MSR_DCD)
1874 * dgnc_tty_tiocmset()
1876 * Set modem signals, called by ld.
1879 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1880 unsigned int set, unsigned int clear)
1882 struct dgnc_board *bd;
1883 struct channel_t *ch;
1886 unsigned long flags;
1888 if (!tty || tty->magic != TTY_MAGIC)
1891 un = tty->driver_data;
1892 if (!un || un->magic != DGNC_UNIT_MAGIC)
1896 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1900 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1903 spin_lock_irqsave(&ch->ch_lock, flags);
1905 if (set & TIOCM_RTS)
1906 ch->ch_mostat |= UART_MCR_RTS;
1908 if (set & TIOCM_DTR)
1909 ch->ch_mostat |= UART_MCR_DTR;
1911 if (clear & TIOCM_RTS)
1912 ch->ch_mostat &= ~(UART_MCR_RTS);
1914 if (clear & TIOCM_DTR)
1915 ch->ch_mostat &= ~(UART_MCR_DTR);
1917 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1919 spin_unlock_irqrestore(&ch->ch_lock, flags);
1925 * dgnc_tty_send_break()
1927 * Send a Break, called by ld.
1929 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
1931 struct dgnc_board *bd;
1932 struct channel_t *ch;
1935 unsigned long flags;
1937 if (!tty || tty->magic != TTY_MAGIC)
1940 un = tty->driver_data;
1941 if (!un || un->magic != DGNC_UNIT_MAGIC)
1945 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1949 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1963 spin_lock_irqsave(&ch->ch_lock, flags);
1965 ch->ch_bd->bd_ops->send_break(ch, msec);
1967 spin_unlock_irqrestore(&ch->ch_lock, flags);
1973 * dgnc_tty_wait_until_sent()
1975 * wait until data has been transmitted, called by ld.
1977 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1979 struct dgnc_board *bd;
1980 struct channel_t *ch;
1983 if (!tty || tty->magic != TTY_MAGIC)
1986 un = tty->driver_data;
1987 if (!un || un->magic != DGNC_UNIT_MAGIC)
1991 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1995 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1998 bd->bd_ops->drain(tty, 0);
2004 * send a high priority character, called by ld.
2006 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
2008 struct dgnc_board *bd;
2009 struct channel_t *ch;
2011 unsigned long flags;
2013 if (!tty || tty->magic != TTY_MAGIC)
2016 un = tty->driver_data;
2017 if (!un || un->magic != DGNC_UNIT_MAGIC)
2021 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2025 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2028 dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
2030 spin_lock_irqsave(&ch->ch_lock, flags);
2031 bd->bd_ops->send_immediate_char(ch, c);
2032 spin_unlock_irqrestore(&ch->ch_lock, flags);
2034 dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
2038 * Return modem signals to ld.
2040 static inline int dgnc_get_mstat(struct channel_t *ch)
2042 unsigned char mstat;
2044 unsigned long flags;
2046 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2049 spin_lock_irqsave(&ch->ch_lock, flags);
2051 mstat = (ch->ch_mostat | ch->ch_mistat);
2053 spin_unlock_irqrestore(&ch->ch_lock, flags);
2057 if (mstat & UART_MCR_DTR)
2058 result |= TIOCM_DTR;
2059 if (mstat & UART_MCR_RTS)
2060 result |= TIOCM_RTS;
2061 if (mstat & UART_MSR_CTS)
2062 result |= TIOCM_CTS;
2063 if (mstat & UART_MSR_DSR)
2064 result |= TIOCM_DSR;
2065 if (mstat & UART_MSR_RI)
2067 if (mstat & UART_MSR_DCD)
2074 * Return modem signals to ld.
2076 static int dgnc_get_modem_info(struct channel_t *ch,
2077 unsigned int __user *value)
2081 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2084 result = dgnc_get_mstat(ch);
2089 return put_user(result, value);
2093 * dgnc_set_modem_info()
2095 * Set modem signals, called by ld.
2097 static int dgnc_set_modem_info(struct tty_struct *tty,
2098 unsigned int command,
2099 unsigned int __user *value)
2101 struct dgnc_board *bd;
2102 struct channel_t *ch;
2105 unsigned int arg = 0;
2106 unsigned long flags;
2108 if (!tty || tty->magic != TTY_MAGIC)
2111 un = tty->driver_data;
2112 if (!un || un->magic != DGNC_UNIT_MAGIC)
2116 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2120 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2123 ret = get_user(arg, value);
2129 if (arg & TIOCM_RTS)
2130 ch->ch_mostat |= UART_MCR_RTS;
2132 if (arg & TIOCM_DTR)
2133 ch->ch_mostat |= UART_MCR_DTR;
2138 if (arg & TIOCM_RTS)
2139 ch->ch_mostat &= ~(UART_MCR_RTS);
2141 if (arg & TIOCM_DTR)
2142 ch->ch_mostat &= ~(UART_MCR_DTR);
2148 if (arg & TIOCM_RTS)
2149 ch->ch_mostat |= UART_MCR_RTS;
2151 ch->ch_mostat &= ~(UART_MCR_RTS);
2153 if (arg & TIOCM_DTR)
2154 ch->ch_mostat |= UART_MCR_DTR;
2156 ch->ch_mostat &= ~(UART_MCR_DTR);
2164 spin_lock_irqsave(&ch->ch_lock, flags);
2166 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2168 spin_unlock_irqrestore(&ch->ch_lock, flags);
2174 * dgnc_tty_digigeta()
2176 * Ioctl to get the information for ditty.
2181 static int dgnc_tty_digigeta(struct tty_struct *tty,
2182 struct digi_t __user *retinfo)
2184 struct channel_t *ch;
2187 unsigned long flags;
2192 if (!tty || tty->magic != TTY_MAGIC)
2195 un = tty->driver_data;
2196 if (!un || un->magic != DGNC_UNIT_MAGIC)
2200 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2203 memset(&tmp, 0, sizeof(tmp));
2205 spin_lock_irqsave(&ch->ch_lock, flags);
2206 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2207 spin_unlock_irqrestore(&ch->ch_lock, flags);
2209 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2216 * dgnc_tty_digiseta()
2218 * Ioctl to set the information for ditty.
2223 static int dgnc_tty_digiseta(struct tty_struct *tty,
2224 struct digi_t __user *new_info)
2226 struct dgnc_board *bd;
2227 struct channel_t *ch;
2229 struct digi_t new_digi;
2230 unsigned long flags;
2232 if (!tty || tty->magic != TTY_MAGIC)
2235 un = tty->driver_data;
2236 if (!un || un->magic != DGNC_UNIT_MAGIC)
2240 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2244 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2247 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
2250 spin_lock_irqsave(&ch->ch_lock, flags);
2253 * Handle transistions to and from RTS Toggle.
2255 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2256 (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2257 ch->ch_mostat &= ~(UART_MCR_RTS);
2258 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2259 !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2260 ch->ch_mostat |= (UART_MCR_RTS);
2263 * Handle transistions to and from DTR Toggle.
2265 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2266 (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2267 ch->ch_mostat &= ~(UART_MCR_DTR);
2268 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2269 !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2270 ch->ch_mostat |= (UART_MCR_DTR);
2272 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2274 if (ch->ch_digi.digi_maxcps < 1)
2275 ch->ch_digi.digi_maxcps = 1;
2277 if (ch->ch_digi.digi_maxcps > 10000)
2278 ch->ch_digi.digi_maxcps = 10000;
2280 if (ch->ch_digi.digi_bufsize < 10)
2281 ch->ch_digi.digi_bufsize = 10;
2283 if (ch->ch_digi.digi_maxchar < 1)
2284 ch->ch_digi.digi_maxchar = 1;
2286 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2287 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2289 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2290 ch->ch_digi.digi_onlen = DIGI_PLEN;
2292 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2293 ch->ch_digi.digi_offlen = DIGI_PLEN;
2295 ch->ch_bd->bd_ops->param(tty);
2297 spin_unlock_irqrestore(&ch->ch_lock, flags);
2303 * dgnc_set_termios()
2305 static void dgnc_tty_set_termios(struct tty_struct *tty,
2306 struct ktermios *old_termios)
2308 struct dgnc_board *bd;
2309 struct channel_t *ch;
2311 unsigned long flags;
2313 if (!tty || tty->magic != TTY_MAGIC)
2316 un = tty->driver_data;
2317 if (!un || un->magic != DGNC_UNIT_MAGIC)
2321 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2325 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2328 spin_lock_irqsave(&ch->ch_lock, flags);
2330 ch->ch_c_cflag = tty->termios.c_cflag;
2331 ch->ch_c_iflag = tty->termios.c_iflag;
2332 ch->ch_c_oflag = tty->termios.c_oflag;
2333 ch->ch_c_lflag = tty->termios.c_lflag;
2334 ch->ch_startc = tty->termios.c_cc[VSTART];
2335 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2337 ch->ch_bd->bd_ops->param(tty);
2340 spin_unlock_irqrestore(&ch->ch_lock, flags);
2343 static void dgnc_tty_throttle(struct tty_struct *tty)
2345 struct channel_t *ch;
2347 unsigned long flags;
2349 if (!tty || tty->magic != TTY_MAGIC)
2352 un = tty->driver_data;
2353 if (!un || un->magic != DGNC_UNIT_MAGIC)
2357 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2360 spin_lock_irqsave(&ch->ch_lock, flags);
2362 ch->ch_flags |= (CH_FORCED_STOPI);
2364 spin_unlock_irqrestore(&ch->ch_lock, flags);
2367 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2369 struct channel_t *ch;
2371 unsigned long flags;
2373 if (!tty || tty->magic != TTY_MAGIC)
2376 un = tty->driver_data;
2377 if (!un || un->magic != DGNC_UNIT_MAGIC)
2381 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2384 spin_lock_irqsave(&ch->ch_lock, flags);
2386 ch->ch_flags &= ~(CH_FORCED_STOPI);
2388 spin_unlock_irqrestore(&ch->ch_lock, flags);
2391 static void dgnc_tty_start(struct tty_struct *tty)
2393 struct dgnc_board *bd;
2394 struct channel_t *ch;
2396 unsigned long flags;
2398 if (!tty || tty->magic != TTY_MAGIC)
2401 un = tty->driver_data;
2402 if (!un || un->magic != DGNC_UNIT_MAGIC)
2406 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2410 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2413 spin_lock_irqsave(&ch->ch_lock, flags);
2415 ch->ch_flags &= ~(CH_FORCED_STOP);
2417 spin_unlock_irqrestore(&ch->ch_lock, flags);
2420 static void dgnc_tty_stop(struct tty_struct *tty)
2422 struct dgnc_board *bd;
2423 struct channel_t *ch;
2425 unsigned long flags;
2427 if (!tty || tty->magic != TTY_MAGIC)
2430 un = tty->driver_data;
2431 if (!un || un->magic != DGNC_UNIT_MAGIC)
2435 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2439 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2442 spin_lock_irqsave(&ch->ch_lock, flags);
2444 ch->ch_flags |= (CH_FORCED_STOP);
2446 spin_unlock_irqrestore(&ch->ch_lock, flags);
2450 * dgnc_tty_flush_chars()
2452 * Flush the cook buffer
2454 * Note to self, and any other poor souls who venture here:
2456 * flush in this case DOES NOT mean dispose of the data.
2457 * instead, it means "stop buffering and send it if you
2458 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2460 * It is also always called in interrupt context - JAR 8-Sept-99
2462 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2464 struct dgnc_board *bd;
2465 struct channel_t *ch;
2467 unsigned long flags;
2469 if (!tty || tty->magic != TTY_MAGIC)
2472 un = tty->driver_data;
2473 if (!un || un->magic != DGNC_UNIT_MAGIC)
2477 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2481 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2484 spin_lock_irqsave(&ch->ch_lock, flags);
2486 /* Do something maybe here */
2488 spin_unlock_irqrestore(&ch->ch_lock, flags);
2492 * dgnc_tty_flush_buffer()
2494 * Flush Tx buffer (make in == out)
2496 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2498 struct channel_t *ch;
2500 unsigned long flags;
2502 if (!tty || tty->magic != TTY_MAGIC)
2505 un = tty->driver_data;
2506 if (!un || un->magic != DGNC_UNIT_MAGIC)
2510 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2513 spin_lock_irqsave(&ch->ch_lock, flags);
2515 ch->ch_flags &= ~CH_STOP;
2517 /* Flush our write queue */
2518 ch->ch_w_head = ch->ch_w_tail;
2520 /* Flush UARTs transmit FIFO */
2521 ch->ch_bd->bd_ops->flush_uart_write(ch);
2523 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2524 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2525 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2527 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2528 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2529 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2532 spin_unlock_irqrestore(&ch->ch_lock, flags);
2535 /*****************************************************************************
2537 * The IOCTL function and all of its helpers
2539 *****************************************************************************/
2544 * The usual assortment of ioctl's
2546 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2549 struct dgnc_board *bd;
2550 struct channel_t *ch;
2553 unsigned long flags;
2554 void __user *uarg = (void __user *)arg;
2556 if (!tty || tty->magic != TTY_MAGIC)
2559 un = tty->driver_data;
2560 if (!un || un->magic != DGNC_UNIT_MAGIC)
2564 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2568 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2571 spin_lock_irqsave(&ch->ch_lock, flags);
2573 if (un->un_open_count <= 0) {
2574 spin_unlock_irqrestore(&ch->ch_lock, flags);
2579 /* Here are all the standard ioctl's that we MUST implement */
2583 * TCSBRK is SVID version: non-zero arg --> no break
2584 * this behaviour is exploited by tcdrain().
2586 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2587 * between 0.25 and 0.5 seconds so we'll ask for something
2588 * in the middle: 0.375 seconds.
2590 rc = tty_check_change(tty);
2591 spin_unlock_irqrestore(&ch->ch_lock, flags);
2595 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2600 spin_lock_irqsave(&ch->ch_lock, flags);
2602 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2603 ch->ch_bd->bd_ops->send_break(ch, 250);
2605 spin_unlock_irqrestore(&ch->ch_lock, flags);
2610 /* support for POSIX tcsendbreak()
2611 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2612 * between 0.25 and 0.5 seconds so we'll ask for something
2613 * in the middle: 0.375 seconds.
2615 rc = tty_check_change(tty);
2616 spin_unlock_irqrestore(&ch->ch_lock, flags);
2620 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2624 spin_lock_irqsave(&ch->ch_lock, flags);
2626 ch->ch_bd->bd_ops->send_break(ch, 250);
2628 spin_unlock_irqrestore(&ch->ch_lock, flags);
2633 rc = tty_check_change(tty);
2634 spin_unlock_irqrestore(&ch->ch_lock, flags);
2638 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2642 spin_lock_irqsave(&ch->ch_lock, flags);
2644 ch->ch_bd->bd_ops->send_break(ch, 250);
2646 spin_unlock_irqrestore(&ch->ch_lock, flags);
2652 spin_unlock_irqrestore(&ch->ch_lock, flags);
2657 spin_unlock_irqrestore(&ch->ch_lock, flags);
2659 rc = put_user(C_CLOCAL(tty) ? 1 : 0,
2660 (unsigned long __user *)arg);
2665 spin_unlock_irqrestore(&ch->ch_lock, flags);
2666 rc = get_user(arg, (unsigned long __user *)arg);
2670 spin_lock_irqsave(&ch->ch_lock, flags);
2671 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
2672 (arg ? CLOCAL : 0));
2673 ch->ch_bd->bd_ops->param(tty);
2674 spin_unlock_irqrestore(&ch->ch_lock, flags);
2679 spin_unlock_irqrestore(&ch->ch_lock, flags);
2680 return dgnc_get_modem_info(ch, uarg);
2685 spin_unlock_irqrestore(&ch->ch_lock, flags);
2686 return dgnc_set_modem_info(tty, cmd, uarg);
2689 * Here are any additional ioctl's that we want to implement
2694 * The linux tty driver doesn't have a flush
2695 * input routine for the driver, assuming all backed
2696 * up data is in the line disc. buffers. However,
2697 * we all know that's not the case. Here, we
2698 * act on the ioctl, but then lie and say we didn't
2699 * so the line discipline will process the flush
2702 rc = tty_check_change(tty);
2704 spin_unlock_irqrestore(&ch->ch_lock, flags);
2708 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2709 ch->ch_r_head = ch->ch_r_tail;
2710 ch->ch_bd->bd_ops->flush_uart_read(ch);
2711 /* Force queue flow control to be released, if needed */
2712 dgnc_check_queue_flow_control(ch);
2715 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2716 if (!(un->un_type == DGNC_PRINT)) {
2717 ch->ch_w_head = ch->ch_w_tail;
2718 ch->ch_bd->bd_ops->flush_uart_write(ch);
2720 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2721 ch->ch_tun.un_flags &=
2723 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2726 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2727 ch->ch_pun.un_flags &=
2729 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2734 /* pretend we didn't recognize this IOCTL */
2735 spin_unlock_irqrestore(&ch->ch_lock, flags);
2736 return -ENOIOCTLCMD;
2740 * The linux tty driver doesn't have a flush
2741 * input routine for the driver, assuming all backed
2742 * up data is in the line disc. buffers. However,
2743 * we all know that's not the case. Here, we
2744 * act on the ioctl, but then lie and say we didn't
2745 * so the line discipline will process the flush
2748 if (cmd == TCSETSF) {
2750 ch->ch_flags &= ~CH_STOP;
2751 ch->ch_r_head = ch->ch_r_tail;
2752 ch->ch_bd->bd_ops->flush_uart_read(ch);
2753 /* Force queue flow control to be released, if needed */
2754 dgnc_check_queue_flow_control(ch);
2757 /* now wait for all the output to drain */
2758 spin_unlock_irqrestore(&ch->ch_lock, flags);
2759 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2763 /* pretend we didn't recognize this */
2764 return -ENOIOCTLCMD;
2768 spin_unlock_irqrestore(&ch->ch_lock, flags);
2769 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2773 /* pretend we didn't recognize this */
2774 return -ENOIOCTLCMD;
2777 spin_unlock_irqrestore(&ch->ch_lock, flags);
2778 /* Make the ld do it */
2779 return -ENOIOCTLCMD;
2782 /* get information for ditty */
2783 spin_unlock_irqrestore(&ch->ch_lock, flags);
2784 return dgnc_tty_digigeta(tty, uarg);
2789 /* set information for ditty */
2790 if (cmd == (DIGI_SETAW)) {
2791 spin_unlock_irqrestore(&ch->ch_lock, flags);
2792 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2797 spin_lock_irqsave(&ch->ch_lock, flags);
2799 tty_ldisc_flush(tty);
2804 spin_unlock_irqrestore(&ch->ch_lock, flags);
2805 return dgnc_tty_digiseta(tty, uarg);
2810 /* Let go of locks when accessing user space,
2813 spin_unlock_irqrestore(&ch->ch_lock, flags);
2814 rc = get_user(loopback, (unsigned int __user *)arg);
2817 spin_lock_irqsave(&ch->ch_lock, flags);
2819 /* Enable/disable internal loopback for this port */
2821 ch->ch_flags |= CH_LOOPBACK;
2823 ch->ch_flags &= ~(CH_LOOPBACK);
2825 ch->ch_bd->bd_ops->param(tty);
2826 spin_unlock_irqrestore(&ch->ch_lock, flags);
2830 case DIGI_GETCUSTOMBAUD:
2831 spin_unlock_irqrestore(&ch->ch_lock, flags);
2832 rc = put_user(ch->ch_custom_speed, (unsigned int __user *)arg);
2835 case DIGI_SETCUSTOMBAUD:
2838 /* Let go of locks when accessing user space, could sleep */
2839 spin_unlock_irqrestore(&ch->ch_lock, flags);
2840 rc = get_user(new_rate, (int __user *)arg);
2843 spin_lock_irqsave(&ch->ch_lock, flags);
2844 dgnc_set_custom_speed(ch, new_rate);
2845 ch->ch_bd->bd_ops->param(tty);
2846 spin_unlock_irqrestore(&ch->ch_lock, flags);
2851 * This ioctl allows insertion of a character into the front
2852 * of any pending data to be transmitted.
2854 * This ioctl is to satify the "Send Character Immediate"
2855 * call that the RealPort protocol spec requires.
2857 case DIGI_REALPORT_SENDIMMEDIATE:
2861 spin_unlock_irqrestore(&ch->ch_lock, flags);
2862 rc = get_user(c, (unsigned char __user *)arg);
2865 spin_lock_irqsave(&ch->ch_lock, flags);
2866 ch->ch_bd->bd_ops->send_immediate_char(ch, c);
2867 spin_unlock_irqrestore(&ch->ch_lock, flags);
2872 * This ioctl returns all the current counts for the port.
2874 * This ioctl is to satify the "Line Error Counters"
2875 * call that the RealPort protocol spec requires.
2877 case DIGI_REALPORT_GETCOUNTERS:
2879 struct digi_getcounter buf;
2881 buf.norun = ch->ch_err_overrun;
2882 buf.noflow = 0; /* The driver doesn't keep this stat */
2883 buf.nframe = ch->ch_err_frame;
2884 buf.nparity = ch->ch_err_parity;
2885 buf.nbreak = ch->ch_err_break;
2886 buf.rbytes = ch->ch_rxcount;
2887 buf.tbytes = ch->ch_txcount;
2889 spin_unlock_irqrestore(&ch->ch_lock, flags);
2891 if (copy_to_user(uarg, &buf, sizeof(buf)))
2898 * This ioctl returns all current events.
2900 * This ioctl is to satify the "Event Reporting"
2901 * call that the RealPort protocol spec requires.
2903 case DIGI_REALPORT_GETEVENTS:
2905 unsigned int events = 0;
2907 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2908 if (ch->ch_flags & CH_BREAK_SENDING)
2910 if ((ch->ch_flags & CH_STOP) ||
2911 (ch->ch_flags & CH_FORCED_STOP))
2912 events |= (EV_OPU | EV_OPS);
2914 if ((ch->ch_flags & CH_STOPI) ||
2915 (ch->ch_flags & CH_FORCED_STOPI))
2916 events |= (EV_IPU | EV_IPS);
2918 spin_unlock_irqrestore(&ch->ch_lock, flags);
2919 rc = put_user(events, (unsigned int __user *)arg);
2924 * This ioctl returns TOUT and TIN counters based
2925 * upon the values passed in by the RealPort Server.
2926 * It also passes back whether the UART Transmitter is
2929 case DIGI_REALPORT_GETBUFFERS:
2931 struct digi_getbuffer buf;
2935 spin_unlock_irqrestore(&ch->ch_lock, flags);
2938 * Get data from user first.
2940 if (copy_from_user(&buf, uarg, sizeof(buf)))
2943 spin_lock_irqsave(&ch->ch_lock, flags);
2946 * Figure out how much data is in our RX and TX queues.
2948 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
2949 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
2952 * Is the UART empty? Add that value to whats in our TX queue.
2954 count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
2957 * Figure out how much data the RealPort Server believes should
2958 * be in our TX queue.
2960 tdist = (buf.tIn - buf.tOut) & 0xffff;
2963 * If we have more data than the RealPort Server believes we
2964 * should have, reduce our count to its amount.
2966 * This count difference CAN happen because the Linux LD can
2967 * insert more characters into our queue for OPOST processing
2968 * that the RealPort Server doesn't know about.
2970 if (buf.txbuf > tdist)
2974 * Report whether our queue and UART TX are completely empty.
2981 spin_unlock_irqrestore(&ch->ch_lock, flags);
2983 if (copy_to_user(uarg, &buf, sizeof(buf)))
2989 spin_unlock_irqrestore(&ch->ch_lock, flags);
2991 return -ENOIOCTLCMD;